NuPlayer.h revision e427abf1ea252ff305fc33aacdd2e83cf34891b5
1/*
2 * Copyright (C) 2010 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 *      http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#ifndef NU_PLAYER_H_
18
19#define NU_PLAYER_H_
20
21#include <media/MediaPlayerInterface.h>
22#include <media/stagefright/foundation/AHandler.h>
23#include <media/stagefright/NativeWindowWrapper.h>
24
25namespace android {
26
27struct ABuffer;
28struct AMessage;
29struct MetaData;
30struct NuPlayerDriver;
31
32struct NuPlayer : public AHandler {
33    NuPlayer();
34
35    void setUID(uid_t uid);
36
37    void setDriver(const wp<NuPlayerDriver> &driver);
38
39    void setDataSourceAsync(const sp<IStreamSource> &source);
40
41    void setDataSourceAsync(
42            const sp<IMediaHTTPService> &httpService,
43            const char *url,
44            const KeyedVector<String8, String8> *headers);
45
46    void setDataSourceAsync(int fd, int64_t offset, int64_t length);
47
48    void prepareAsync();
49
50    void setVideoSurfaceTextureAsync(
51            const sp<IGraphicBufferProducer> &bufferProducer);
52
53    void setAudioSink(const sp<MediaPlayerBase::AudioSink> &sink);
54    void start();
55
56    void pause();
57    void resume();
58
59    // Will notify the driver through "notifyResetComplete" once finished.
60    void resetAsync();
61
62    // Will notify the driver through "notifySeekComplete" once finished
63    // and needNotify is true.
64    void seekToAsync(int64_t seekTimeUs, bool needNotify = false);
65
66    status_t setVideoScalingMode(int32_t mode);
67    status_t getTrackInfo(Parcel* reply) const;
68    status_t getSelectedTrack(int32_t type, Parcel* reply) const;
69    status_t selectTrack(size_t trackIndex, bool select);
70
71    sp<MetaData> getFileMeta();
72
73    static const size_t kAggregateBufferSizeBytes;
74
75protected:
76    virtual ~NuPlayer();
77
78    virtual void onMessageReceived(const sp<AMessage> &msg);
79
80public:
81    struct NuPlayerStreamListener;
82    struct Source;
83
84private:
85    struct Decoder;
86    struct DecoderPassThrough;
87    struct CCDecoder;
88    struct GenericSource;
89    struct HTTPLiveSource;
90    struct Renderer;
91    struct RTSPSource;
92    struct StreamingSource;
93    struct Action;
94    struct SeekAction;
95    struct SetSurfaceAction;
96    struct ShutdownDecoderAction;
97    struct PostMessageAction;
98    struct SimpleAction;
99
100    enum {
101        kWhatSetDataSource              = '=DaS',
102        kWhatPrepare                    = 'prep',
103        kWhatSetVideoNativeWindow       = '=NaW',
104        kWhatSetAudioSink               = '=AuS',
105        kWhatMoreDataQueued             = 'more',
106        kWhatStart                      = 'strt',
107        kWhatScanSources                = 'scan',
108        kWhatVideoNotify                = 'vidN',
109        kWhatAudioNotify                = 'audN',
110        kWhatClosedCaptionNotify        = 'capN',
111        kWhatRendererNotify             = 'renN',
112        kWhatReset                      = 'rset',
113        kWhatSeek                       = 'seek',
114        kWhatPause                      = 'paus',
115        kWhatResume                     = 'rsme',
116        kWhatPollDuration               = 'polD',
117        kWhatSourceNotify               = 'srcN',
118        kWhatGetTrackInfo               = 'gTrI',
119        kWhatGetSelectedTrack           = 'gSel',
120        kWhatSelectTrack                = 'selT',
121    };
122
123    wp<NuPlayerDriver> mDriver;
124    bool mUIDValid;
125    uid_t mUID;
126    sp<Source> mSource;
127    uint32_t mSourceFlags;
128    sp<NativeWindowWrapper> mNativeWindow;
129    int64_t mCurrentPositionUs;
130    sp<MediaPlayerBase::AudioSink> mAudioSink;
131    sp<Decoder> mVideoDecoder;
132    bool mVideoIsAVC;
133    bool mOffloadAudio;
134    audio_offload_info_t mCurrentOffloadInfo;
135    sp<Decoder> mAudioDecoder;
136    sp<CCDecoder> mCCDecoder;
137    sp<Renderer> mRenderer;
138    sp<ALooper> mRendererLooper;
139    int32_t mAudioDecoderGeneration;
140    int32_t mVideoDecoderGeneration;
141
142    List<sp<Action> > mDeferredActions;
143
144    bool mAudioEOS;
145    bool mVideoEOS;
146
147    bool mScanSourcesPending;
148    int32_t mScanSourcesGeneration;
149
150    int32_t mPollDurationGeneration;
151    int32_t mTimedTextGeneration;
152
153    enum FlushStatus {
154        NONE,
155        FLUSHING_DECODER,
156        FLUSHING_DECODER_SHUTDOWN,
157        SHUTTING_DOWN_DECODER,
158        FLUSHED,
159        SHUT_DOWN,
160    };
161
162    // Once the current flush is complete this indicates whether the
163    // notion of time has changed.
164    bool mTimeDiscontinuityPending;
165
166    // Used by feedDecoderInputData to aggregate small buffers into
167    // one large buffer.
168    sp<ABuffer> mPendingAudioAccessUnit;
169    status_t    mPendingAudioErr;
170    sp<ABuffer> mAggregateBuffer;
171
172    FlushStatus mFlushingAudio;
173    FlushStatus mFlushingVideo;
174
175    int64_t mSkipRenderingAudioUntilMediaTimeUs;
176    int64_t mSkipRenderingVideoUntilMediaTimeUs;
177
178    int64_t mVideoLateByUs;
179    int64_t mNumFramesTotal, mNumFramesDropped;
180
181    int32_t mVideoScalingMode;
182
183    bool mStarted;
184
185    inline const sp<Decoder> &getDecoder(bool audio) {
186        return audio ? mAudioDecoder : mVideoDecoder;
187    }
188
189    void openAudioSink(const sp<AMessage> &format, bool offloadOnly);
190    void closeAudioSink();
191
192    status_t instantiateDecoder(bool audio, sp<Decoder> *decoder);
193
194    void updateVideoSize(
195            const sp<AMessage> &inputFormat,
196            const sp<AMessage> &outputFormat = NULL);
197
198    status_t feedDecoderInputData(bool audio, const sp<AMessage> &msg);
199    void renderBuffer(bool audio, const sp<AMessage> &msg);
200
201    void notifyListener(int msg, int ext1, int ext2, const Parcel *in = NULL);
202
203    void finishFlushIfPossible();
204
205    void flushDecoder(
206            bool audio, bool needShutdown, const sp<AMessage> &newFormat = NULL);
207    void updateDecoderFormatWithoutFlush(bool audio, const sp<AMessage> &format);
208
209    static bool IsFlushingState(FlushStatus state, bool *needShutdown = NULL);
210
211    void postScanSources();
212
213    void schedulePollDuration();
214    void cancelPollDuration();
215
216    void processDeferredActions();
217
218    void performSeek(int64_t seekTimeUs, bool needNotify);
219    void performDecoderFlush();
220    void performDecoderShutdown(bool audio, bool video);
221    void performReset();
222    void performScanSources();
223    void performSetSurface(const sp<NativeWindowWrapper> &wrapper);
224
225    void onSourceNotify(const sp<AMessage> &msg);
226    void onClosedCaptionNotify(const sp<AMessage> &msg);
227
228    void queueDecoderShutdown(
229            bool audio, bool video, const sp<AMessage> &reply);
230
231    void sendSubtitleData(const sp<ABuffer> &buffer, int32_t baseIndex);
232    void sendTimedTextData(const sp<ABuffer> &buffer);
233
234    void writeTrackInfo(Parcel* reply, const sp<AMessage> format) const;
235
236    DISALLOW_EVIL_CONSTRUCTORS(NuPlayer);
237};
238
239}  // namespace android
240
241#endif  // NU_PLAYER_H_
242