NuPlayer.h revision a73d9e0b3d171d2bfcd9eb07df9d6d36ae74df57
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    status_t getCurrentPosition(int64_t *mediaUs);
71    void getStats(int64_t *mNumFramesTotal, int64_t *mNumFramesDropped);
72
73    sp<MetaData> getFileMeta();
74
75    static const size_t kAggregateBufferSizeBytes;
76
77protected:
78    virtual ~NuPlayer();
79
80    virtual void onMessageReceived(const sp<AMessage> &msg);
81
82public:
83    struct NuPlayerStreamListener;
84    struct Source;
85
86private:
87    struct Decoder;
88    struct DecoderPassThrough;
89    struct CCDecoder;
90    struct GenericSource;
91    struct HTTPLiveSource;
92    struct Renderer;
93    struct RTSPSource;
94    struct StreamingSource;
95    struct Action;
96    struct SeekAction;
97    struct SetSurfaceAction;
98    struct ShutdownDecoderAction;
99    struct PostMessageAction;
100    struct SimpleAction;
101
102    enum {
103        kWhatSetDataSource              = '=DaS',
104        kWhatPrepare                    = 'prep',
105        kWhatSetVideoNativeWindow       = '=NaW',
106        kWhatSetAudioSink               = '=AuS',
107        kWhatMoreDataQueued             = 'more',
108        kWhatStart                      = 'strt',
109        kWhatScanSources                = 'scan',
110        kWhatVideoNotify                = 'vidN',
111        kWhatAudioNotify                = 'audN',
112        kWhatClosedCaptionNotify        = 'capN',
113        kWhatRendererNotify             = 'renN',
114        kWhatReset                      = 'rset',
115        kWhatSeek                       = 'seek',
116        kWhatPause                      = 'paus',
117        kWhatResume                     = 'rsme',
118        kWhatPollDuration               = 'polD',
119        kWhatSourceNotify               = 'srcN',
120        kWhatGetTrackInfo               = 'gTrI',
121        kWhatGetSelectedTrack           = 'gSel',
122        kWhatSelectTrack                = 'selT',
123    };
124
125    wp<NuPlayerDriver> mDriver;
126    bool mUIDValid;
127    uid_t mUID;
128    sp<Source> mSource;
129    uint32_t mSourceFlags;
130    sp<NativeWindowWrapper> mNativeWindow;
131    sp<MediaPlayerBase::AudioSink> mAudioSink;
132    sp<Decoder> mVideoDecoder;
133    bool mVideoIsAVC;
134    bool mOffloadAudio;
135    audio_offload_info_t mCurrentOffloadInfo;
136    sp<Decoder> mAudioDecoder;
137    sp<CCDecoder> mCCDecoder;
138    sp<Renderer> mRenderer;
139    sp<ALooper> mRendererLooper;
140    int32_t mAudioDecoderGeneration;
141    int32_t mVideoDecoderGeneration;
142    int32_t mRendererGeneration;
143
144    List<sp<Action> > mDeferredActions;
145
146    bool mAudioEOS;
147    bool mVideoEOS;
148
149    bool mScanSourcesPending;
150    int32_t mScanSourcesGeneration;
151
152    int32_t mPollDurationGeneration;
153    int32_t mTimedTextGeneration;
154
155    enum FlushStatus {
156        NONE,
157        FLUSHING_DECODER,
158        FLUSHING_DECODER_SHUTDOWN,
159        SHUTTING_DOWN_DECODER,
160        FLUSHED,
161        SHUT_DOWN,
162    };
163
164    // Once the current flush is complete this indicates whether the
165    // notion of time has changed.
166    bool mTimeDiscontinuityPending;
167
168    // Status of flush responses from the decoder and renderer.
169    bool mFlushComplete[2][2];
170
171    // Used by feedDecoderInputData to aggregate small buffers into
172    // one large buffer.
173    sp<ABuffer> mPendingAudioAccessUnit;
174    status_t    mPendingAudioErr;
175    sp<ABuffer> mAggregateBuffer;
176
177    FlushStatus mFlushingAudio;
178    FlushStatus mFlushingVideo;
179
180    int64_t mSkipRenderingAudioUntilMediaTimeUs;
181    int64_t mSkipRenderingVideoUntilMediaTimeUs;
182
183    int64_t mNumFramesTotal, mNumFramesDropped;
184
185    int32_t mVideoScalingMode;
186
187    bool mStarted;
188
189    inline const sp<Decoder> &getDecoder(bool audio) {
190        return audio ? mAudioDecoder : mVideoDecoder;
191    }
192
193    inline void clearFlushComplete() {
194        mFlushComplete[0][0] = false;
195        mFlushComplete[0][1] = false;
196        mFlushComplete[1][0] = false;
197        mFlushComplete[1][1] = false;
198    }
199
200    void openAudioSink(const sp<AMessage> &format, bool offloadOnly);
201    void closeAudioSink();
202
203    status_t instantiateDecoder(bool audio, sp<Decoder> *decoder);
204
205    void updateVideoSize(
206            const sp<AMessage> &inputFormat,
207            const sp<AMessage> &outputFormat = NULL);
208
209    status_t feedDecoderInputData(bool audio, const sp<AMessage> &msg);
210    void renderBuffer(bool audio, const sp<AMessage> &msg);
211
212    void notifyListener(int msg, int ext1, int ext2, const Parcel *in = NULL);
213
214    void handleFlushComplete(bool audio, bool isDecoder);
215    void finishFlushIfPossible();
216
217    bool audioDecoderStillNeeded();
218
219    void flushDecoder(
220            bool audio, bool needShutdown, const sp<AMessage> &newFormat = NULL);
221    void updateDecoderFormatWithoutFlush(bool audio, const sp<AMessage> &format);
222
223    void postScanSources();
224
225    void schedulePollDuration();
226    void cancelPollDuration();
227
228    void processDeferredActions();
229
230    void performSeek(int64_t seekTimeUs, bool needNotify);
231    void performDecoderFlush();
232    void performDecoderShutdown(bool audio, bool video);
233    void performReset();
234    void performScanSources();
235    void performSetSurface(const sp<NativeWindowWrapper> &wrapper);
236
237    void onSourceNotify(const sp<AMessage> &msg);
238    void onClosedCaptionNotify(const sp<AMessage> &msg);
239
240    void queueDecoderShutdown(
241            bool audio, bool video, const sp<AMessage> &reply);
242
243    void sendSubtitleData(const sp<ABuffer> &buffer, int32_t baseIndex);
244    void sendTimedTextData(const sp<ABuffer> &buffer);
245
246    void writeTrackInfo(Parcel* reply, const sp<AMessage> format) const;
247
248    DISALLOW_EVIL_CONSTRUCTORS(NuPlayer);
249};
250
251}  // namespace android
252
253#endif  // NU_PLAYER_H_
254