NuPlayer.h revision 9421174a2f002fef31b330fb04e00105a905dca4
19720d5f59b9c1f5d1b9ecbc9173dbcb71bd557beSteve Block/*
29720d5f59b9c1f5d1b9ecbc9173dbcb71bd557beSteve Block * Copyright (C) 2010 The Android Open Source Project
39720d5f59b9c1f5d1b9ecbc9173dbcb71bd557beSteve Block *
49720d5f59b9c1f5d1b9ecbc9173dbcb71bd557beSteve Block * Licensed under the Apache License, Version 2.0 (the "License");
59720d5f59b9c1f5d1b9ecbc9173dbcb71bd557beSteve Block * you may not use this file except in compliance with the License.
69720d5f59b9c1f5d1b9ecbc9173dbcb71bd557beSteve Block * You may obtain a copy of the License at
79720d5f59b9c1f5d1b9ecbc9173dbcb71bd557beSteve Block *
89720d5f59b9c1f5d1b9ecbc9173dbcb71bd557beSteve Block *      http://www.apache.org/licenses/LICENSE-2.0
99720d5f59b9c1f5d1b9ecbc9173dbcb71bd557beSteve Block *
109720d5f59b9c1f5d1b9ecbc9173dbcb71bd557beSteve Block * Unless required by applicable law or agreed to in writing, software
119720d5f59b9c1f5d1b9ecbc9173dbcb71bd557beSteve Block * distributed under the License is distributed on an "AS IS" BASIS,
129720d5f59b9c1f5d1b9ecbc9173dbcb71bd557beSteve Block * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
139720d5f59b9c1f5d1b9ecbc9173dbcb71bd557beSteve Block * See the License for the specific language governing permissions and
149720d5f59b9c1f5d1b9ecbc9173dbcb71bd557beSteve Block * limitations under the License.
159720d5f59b9c1f5d1b9ecbc9173dbcb71bd557beSteve Block */
169720d5f59b9c1f5d1b9ecbc9173dbcb71bd557beSteve Block
179720d5f59b9c1f5d1b9ecbc9173dbcb71bd557beSteve Block#ifndef NU_PLAYER_H_
189720d5f59b9c1f5d1b9ecbc9173dbcb71bd557beSteve Block
199720d5f59b9c1f5d1b9ecbc9173dbcb71bd557beSteve Block#define NU_PLAYER_H_
209720d5f59b9c1f5d1b9ecbc9173dbcb71bd557beSteve Block
219720d5f59b9c1f5d1b9ecbc9173dbcb71bd557beSteve Block#include <media/MediaPlayerInterface.h>
229720d5f59b9c1f5d1b9ecbc9173dbcb71bd557beSteve Block#include <media/stagefright/foundation/AHandler.h>
239720d5f59b9c1f5d1b9ecbc9173dbcb71bd557beSteve Block#include <media/stagefright/NativeWindowWrapper.h>
249720d5f59b9c1f5d1b9ecbc9173dbcb71bd557beSteve Block
259720d5f59b9c1f5d1b9ecbc9173dbcb71bd557beSteve Blocknamespace android {
269720d5f59b9c1f5d1b9ecbc9173dbcb71bd557beSteve Block
279720d5f59b9c1f5d1b9ecbc9173dbcb71bd557beSteve Blockstruct ABuffer;
289720d5f59b9c1f5d1b9ecbc9173dbcb71bd557beSteve Blockstruct AMessage;
299720d5f59b9c1f5d1b9ecbc9173dbcb71bd557beSteve Blockstruct MetaData;
309720d5f59b9c1f5d1b9ecbc9173dbcb71bd557beSteve Blockstruct NuPlayerDriver;
31e46c9386c4f79aa40185f79a19fc5b2a7ef528b3Patrick Scott
329720d5f59b9c1f5d1b9ecbc9173dbcb71bd557beSteve Blockstruct NuPlayer : public AHandler {
339720d5f59b9c1f5d1b9ecbc9173dbcb71bd557beSteve Block    NuPlayer();
34e46c9386c4f79aa40185f79a19fc5b2a7ef528b3Patrick Scott
35e46c9386c4f79aa40185f79a19fc5b2a7ef528b3Patrick Scott    void setUID(uid_t uid);
36e46c9386c4f79aa40185f79a19fc5b2a7ef528b3Patrick Scott
37e46c9386c4f79aa40185f79a19fc5b2a7ef528b3Patrick Scott    void setDriver(const wp<NuPlayerDriver> &driver);
389720d5f59b9c1f5d1b9ecbc9173dbcb71bd557beSteve Block
399720d5f59b9c1f5d1b9ecbc9173dbcb71bd557beSteve Block    void setDataSourceAsync(const sp<IStreamSource> &source);
40e46c9386c4f79aa40185f79a19fc5b2a7ef528b3Patrick Scott
41e46c9386c4f79aa40185f79a19fc5b2a7ef528b3Patrick Scott    void setDataSourceAsync(
42e46c9386c4f79aa40185f79a19fc5b2a7ef528b3Patrick Scott            const sp<IMediaHTTPService> &httpService,
43e46c9386c4f79aa40185f79a19fc5b2a7ef528b3Patrick Scott            const char *url,
44e46c9386c4f79aa40185f79a19fc5b2a7ef528b3Patrick Scott            const KeyedVector<String8, String8> *headers);
45e46c9386c4f79aa40185f79a19fc5b2a7ef528b3Patrick Scott
469720d5f59b9c1f5d1b9ecbc9173dbcb71bd557beSteve Block    void setDataSourceAsync(int fd, int64_t offset, int64_t length);
479720d5f59b9c1f5d1b9ecbc9173dbcb71bd557beSteve Block
489720d5f59b9c1f5d1b9ecbc9173dbcb71bd557beSteve Block    void prepareAsync();
499720d5f59b9c1f5d1b9ecbc9173dbcb71bd557beSteve Block
509720d5f59b9c1f5d1b9ecbc9173dbcb71bd557beSteve Block    void setVideoSurfaceTextureAsync(
519720d5f59b9c1f5d1b9ecbc9173dbcb71bd557beSteve Block            const sp<IGraphicBufferProducer> &bufferProducer);
529720d5f59b9c1f5d1b9ecbc9173dbcb71bd557beSteve Block
539720d5f59b9c1f5d1b9ecbc9173dbcb71bd557beSteve Block    void setAudioSink(const sp<MediaPlayerBase::AudioSink> &sink);
549720d5f59b9c1f5d1b9ecbc9173dbcb71bd557beSteve Block    void start();
559720d5f59b9c1f5d1b9ecbc9173dbcb71bd557beSteve Block
569720d5f59b9c1f5d1b9ecbc9173dbcb71bd557beSteve Block    void pause();
579720d5f59b9c1f5d1b9ecbc9173dbcb71bd557beSteve Block
58    // Will notify the driver through "notifyResetComplete" once finished.
59    void resetAsync();
60
61    // Will notify the driver through "notifySeekComplete" once finished
62    // and needNotify is true.
63    void seekToAsync(int64_t seekTimeUs, bool needNotify = false);
64
65    status_t setVideoScalingMode(int32_t mode);
66    status_t getTrackInfo(Parcel* reply) const;
67    status_t getSelectedTrack(int32_t type, Parcel* reply) const;
68    status_t selectTrack(size_t trackIndex, bool select);
69    status_t getCurrentPosition(int64_t *mediaUs);
70    void getStats(int64_t *mNumFramesTotal, int64_t *mNumFramesDropped);
71
72    sp<MetaData> getFileMeta();
73
74    static const size_t kAggregateBufferSizeBytes;
75
76protected:
77    virtual ~NuPlayer();
78
79    virtual void onMessageReceived(const sp<AMessage> &msg);
80
81public:
82    struct NuPlayerStreamListener;
83    struct Source;
84
85private:
86    struct Decoder;
87    struct DecoderPassThrough;
88    struct CCDecoder;
89    struct GenericSource;
90    struct HTTPLiveSource;
91    struct Renderer;
92    struct RTSPSource;
93    struct StreamingSource;
94    struct Action;
95    struct SeekAction;
96    struct SetSurfaceAction;
97    struct ShutdownDecoderAction;
98    struct PostMessageAction;
99    struct SimpleAction;
100
101    enum {
102        kWhatSetDataSource              = '=DaS',
103        kWhatPrepare                    = 'prep',
104        kWhatSetVideoNativeWindow       = '=NaW',
105        kWhatSetAudioSink               = '=AuS',
106        kWhatMoreDataQueued             = 'more',
107        kWhatStart                      = 'strt',
108        kWhatScanSources                = 'scan',
109        kWhatVideoNotify                = 'vidN',
110        kWhatAudioNotify                = 'audN',
111        kWhatClosedCaptionNotify        = 'capN',
112        kWhatRendererNotify             = 'renN',
113        kWhatReset                      = 'rset',
114        kWhatSeek                       = 'seek',
115        kWhatPause                      = 'paus',
116        kWhatResume                     = 'rsme',
117        kWhatPollDuration               = 'polD',
118        kWhatSourceNotify               = 'srcN',
119        kWhatGetTrackInfo               = 'gTrI',
120        kWhatGetSelectedTrack           = 'gSel',
121        kWhatSelectTrack                = 'selT',
122    };
123
124    wp<NuPlayerDriver> mDriver;
125    bool mUIDValid;
126    uid_t mUID;
127    sp<Source> mSource;
128    uint32_t mSourceFlags;
129    sp<NativeWindowWrapper> mNativeWindow;
130    sp<MediaPlayerBase::AudioSink> mAudioSink;
131    sp<Decoder> mVideoDecoder;
132    bool mVideoIsAVC;
133    bool mOffloadAudio;
134    sp<Decoder> mAudioDecoder;
135    sp<CCDecoder> mCCDecoder;
136    sp<Renderer> mRenderer;
137    sp<ALooper> mRendererLooper;
138    int32_t mAudioDecoderGeneration;
139    int32_t mVideoDecoderGeneration;
140    int32_t mRendererGeneration;
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    // Status of flush responses from the decoder and renderer.
167    bool mFlushComplete[2][2];
168
169    // Used by feedDecoderInputData to aggregate small buffers into
170    // one large buffer.
171    sp<ABuffer> mPendingAudioAccessUnit;
172    status_t    mPendingAudioErr;
173    sp<ABuffer> mAggregateBuffer;
174
175    FlushStatus mFlushingAudio;
176    FlushStatus mFlushingVideo;
177
178    int64_t mSkipRenderingAudioUntilMediaTimeUs;
179    int64_t mSkipRenderingVideoUntilMediaTimeUs;
180
181    int64_t mNumFramesTotal, mNumFramesDropped;
182
183    int32_t mVideoScalingMode;
184
185    bool mStarted;
186
187    inline const sp<Decoder> &getDecoder(bool audio) {
188        return audio ? mAudioDecoder : mVideoDecoder;
189    }
190
191    inline void clearFlushComplete() {
192        mFlushComplete[0][0] = false;
193        mFlushComplete[0][1] = false;
194        mFlushComplete[1][0] = false;
195        mFlushComplete[1][1] = false;
196    }
197
198    void openAudioSink(const sp<AMessage> &format, bool offloadOnly);
199    void closeAudioSink();
200
201    status_t instantiateDecoder(bool audio, sp<Decoder> *decoder);
202
203    void updateVideoSize(
204            const sp<AMessage> &inputFormat,
205            const sp<AMessage> &outputFormat = NULL);
206
207    status_t feedDecoderInputData(bool audio, const sp<AMessage> &msg);
208    void renderBuffer(bool audio, const sp<AMessage> &msg);
209
210    void notifyListener(int msg, int ext1, int ext2, const Parcel *in = NULL);
211
212    void handleFlushComplete(bool audio, bool isDecoder);
213    void finishFlushIfPossible();
214
215    void onStart();
216    void onResume();
217
218    bool audioDecoderStillNeeded();
219
220    void flushDecoder(
221            bool audio, bool needShutdown, const sp<AMessage> &newFormat = NULL);
222    void updateDecoderFormatWithoutFlush(bool audio, const sp<AMessage> &format);
223
224    void postScanSources();
225
226    void schedulePollDuration();
227    void cancelPollDuration();
228
229    void processDeferredActions();
230
231    void performSeek(int64_t seekTimeUs, bool needNotify);
232    void performDecoderFlush();
233    void performDecoderShutdown(bool audio, bool video);
234    void performReset();
235    void performScanSources();
236    void performSetSurface(const sp<NativeWindowWrapper> &wrapper);
237
238    void onSourceNotify(const sp<AMessage> &msg);
239    void onClosedCaptionNotify(const sp<AMessage> &msg);
240
241    void queueDecoderShutdown(
242            bool audio, bool video, const sp<AMessage> &reply);
243
244    void sendSubtitleData(const sp<ABuffer> &buffer, int32_t baseIndex);
245    void sendTimedTextData(const sp<ABuffer> &buffer);
246
247    void writeTrackInfo(Parcel* reply, const sp<AMessage> format) const;
248
249    DISALLOW_EVIL_CONSTRUCTORS(NuPlayer);
250};
251
252}  // namespace android
253
254#endif  // NU_PLAYER_H_
255