NuPlayer.h revision fef808d42a9c94b0b5ef3c3d5fb0a090edbc42da
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
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 FlushDecoderAction;
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    enum FlushCommand {
163        FLUSH_CMD_NONE,
164        FLUSH_CMD_FLUSH,
165        FLUSH_CMD_SHUTDOWN,
166    };
167
168    // Once the current flush is complete this indicates whether the
169    // notion of time has changed.
170    bool mTimeDiscontinuityPending;
171
172    // Status of flush responses from the decoder and renderer.
173    bool mFlushComplete[2][2];
174
175    // Used by feedDecoderInputData to aggregate small buffers into
176    // one large buffer.
177    sp<ABuffer> mPendingAudioAccessUnit;
178    status_t    mPendingAudioErr;
179    sp<ABuffer> mAggregateBuffer;
180
181    FlushStatus mFlushingAudio;
182    FlushStatus mFlushingVideo;
183
184    int64_t mSkipRenderingAudioUntilMediaTimeUs;
185    int64_t mSkipRenderingVideoUntilMediaTimeUs;
186
187    int64_t mNumFramesTotal, mNumFramesDropped;
188
189    int32_t mVideoScalingMode;
190
191    bool mStarted;
192
193    inline const sp<Decoder> &getDecoder(bool audio) {
194        return audio ? mAudioDecoder : mVideoDecoder;
195    }
196
197    inline void clearFlushComplete() {
198        mFlushComplete[0][0] = false;
199        mFlushComplete[0][1] = false;
200        mFlushComplete[1][0] = false;
201        mFlushComplete[1][1] = false;
202    }
203
204    void openAudioSink(const sp<AMessage> &format, bool offloadOnly);
205    void closeAudioSink();
206
207    status_t instantiateDecoder(bool audio, sp<Decoder> *decoder);
208
209    void updateVideoSize(
210            const sp<AMessage> &inputFormat,
211            const sp<AMessage> &outputFormat = NULL);
212
213    status_t feedDecoderInputData(bool audio, const sp<AMessage> &msg);
214    void renderBuffer(bool audio, const sp<AMessage> &msg);
215
216    void notifyListener(int msg, int ext1, int ext2, const Parcel *in = NULL);
217
218    void handleFlushComplete(bool audio, bool isDecoder);
219    void finishFlushIfPossible();
220
221    void onStart();
222    void onResume();
223
224    bool audioDecoderStillNeeded();
225
226    void flushDecoder(
227            bool audio, bool needShutdown, const sp<AMessage> &newFormat = NULL);
228    void updateDecoderFormatWithoutFlush(bool audio, const sp<AMessage> &format);
229
230    void postScanSources();
231
232    void schedulePollDuration();
233    void cancelPollDuration();
234
235    void processDeferredActions();
236
237    void performSeek(int64_t seekTimeUs, bool needNotify);
238    void performDecoderFlush(FlushCommand audio, FlushCommand video);
239    void performReset();
240    void performScanSources();
241    void performSetSurface(const sp<NativeWindowWrapper> &wrapper);
242
243    void onSourceNotify(const sp<AMessage> &msg);
244    void onClosedCaptionNotify(const sp<AMessage> &msg);
245
246    void queueDecoderShutdown(
247            bool audio, bool video, const sp<AMessage> &reply);
248
249    void sendSubtitleData(const sp<ABuffer> &buffer, int32_t baseIndex);
250    void sendTimedTextData(const sp<ABuffer> &buffer);
251
252    void writeTrackInfo(Parcel* reply, const sp<AMessage> format) const;
253
254    DISALLOW_EVIL_CONSTRUCTORS(NuPlayer);
255};
256
257}  // namespace android
258
259#endif  // NU_PLAYER_H_
260