NuPlayer.h revision 1de1e25cba872bd4c077c2e394f8ca9c70b65856
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/AudioResamplerPublic.h>
22#include <media/MediaPlayerInterface.h>
23#include <media/stagefright/foundation/AHandler.h>
24
25namespace android {
26
27struct ABuffer;
28struct AMessage;
29struct AudioPlaybackRate;
30struct AVSyncSettings;
31class IDataSource;
32class MetaData;
33struct NuPlayerDriver;
34
35struct NuPlayer : public AHandler {
36    NuPlayer();
37
38    void setUID(uid_t uid);
39
40    void setDriver(const wp<NuPlayerDriver> &driver);
41
42    void setDataSourceAsync(const sp<IStreamSource> &source);
43
44    void setDataSourceAsync(
45            const sp<IMediaHTTPService> &httpService,
46            const char *url,
47            const KeyedVector<String8, String8> *headers);
48
49    void setDataSourceAsync(int fd, int64_t offset, int64_t length);
50
51    void setDataSourceAsync(const sp<DataSource> &source);
52
53    void prepareAsync();
54
55    void setVideoSurfaceTextureAsync(
56            const sp<IGraphicBufferProducer> &bufferProducer);
57
58    void setAudioSink(const sp<MediaPlayerBase::AudioSink> &sink);
59    status_t setPlaybackSettings(const AudioPlaybackRate &rate);
60    status_t getPlaybackSettings(AudioPlaybackRate *rate /* nonnull */);
61    status_t setSyncSettings(const AVSyncSettings &sync, float videoFpsHint);
62    status_t getSyncSettings(AVSyncSettings *sync /* nonnull */, float *videoFps /* nonnull */);
63
64    void start();
65
66    void pause();
67
68    // Will notify the driver through "notifyResetComplete" once finished.
69    void resetAsync();
70
71    // Will notify the driver through "notifySeekComplete" once finished
72    // and needNotify is true.
73    void seekToAsync(int64_t seekTimeUs, bool needNotify = false);
74
75    status_t setVideoScalingMode(int32_t mode);
76    status_t getTrackInfo(Parcel* reply) const;
77    status_t getSelectedTrack(int32_t type, Parcel* reply) const;
78    status_t selectTrack(size_t trackIndex, bool select, int64_t timeUs);
79    status_t getCurrentPosition(int64_t *mediaUs);
80    void getStats(int64_t *mNumFramesTotal, int64_t *mNumFramesDropped);
81
82    sp<MetaData> getFileMeta();
83
84protected:
85    virtual ~NuPlayer();
86
87    virtual void onMessageReceived(const sp<AMessage> &msg);
88
89public:
90    struct NuPlayerStreamListener;
91    struct Source;
92
93private:
94    struct Decoder;
95    struct DecoderBase;
96    struct DecoderPassThrough;
97    struct CCDecoder;
98    struct GenericSource;
99    struct HTTPLiveSource;
100    struct Renderer;
101    struct RTSPSource;
102    struct StreamingSource;
103    struct Action;
104    struct SeekAction;
105    struct SetSurfaceAction;
106    struct ResumeDecoderAction;
107    struct FlushDecoderAction;
108    struct PostMessageAction;
109    struct SimpleAction;
110
111    enum {
112        kWhatSetDataSource              = '=DaS',
113        kWhatPrepare                    = 'prep',
114        kWhatSetVideoSurface            = '=VSu',
115        kWhatSetAudioSink               = '=AuS',
116        kWhatMoreDataQueued             = 'more',
117        kWhatConfigPlayback             = 'cfPB',
118        kWhatConfigSync                 = 'cfSy',
119        kWhatGetPlaybackSettings        = 'gPbS',
120        kWhatGetSyncSettings            = 'gSyS',
121        kWhatStart                      = 'strt',
122        kWhatScanSources                = 'scan',
123        kWhatVideoNotify                = 'vidN',
124        kWhatAudioNotify                = 'audN',
125        kWhatClosedCaptionNotify        = 'capN',
126        kWhatRendererNotify             = 'renN',
127        kWhatReset                      = 'rset',
128        kWhatSeek                       = 'seek',
129        kWhatPause                      = 'paus',
130        kWhatResume                     = 'rsme',
131        kWhatPollDuration               = 'polD',
132        kWhatSourceNotify               = 'srcN',
133        kWhatGetTrackInfo               = 'gTrI',
134        kWhatGetSelectedTrack           = 'gSel',
135        kWhatSelectTrack                = 'selT',
136    };
137
138    wp<NuPlayerDriver> mDriver;
139    bool mUIDValid;
140    uid_t mUID;
141    sp<Source> mSource;
142    uint32_t mSourceFlags;
143    sp<Surface> mSurface;
144    sp<MediaPlayerBase::AudioSink> mAudioSink;
145    sp<DecoderBase> mVideoDecoder;
146    bool mOffloadAudio;
147    sp<DecoderBase> mAudioDecoder;
148    sp<CCDecoder> mCCDecoder;
149    sp<Renderer> mRenderer;
150    sp<ALooper> mRendererLooper;
151    int32_t mAudioDecoderGeneration;
152    int32_t mVideoDecoderGeneration;
153    int32_t mRendererGeneration;
154
155    List<sp<Action> > mDeferredActions;
156
157    bool mAudioEOS;
158    bool mVideoEOS;
159
160    bool mScanSourcesPending;
161    int32_t mScanSourcesGeneration;
162
163    int32_t mPollDurationGeneration;
164    int32_t mTimedTextGeneration;
165
166    enum FlushStatus {
167        NONE,
168        FLUSHING_DECODER,
169        FLUSHING_DECODER_SHUTDOWN,
170        SHUTTING_DOWN_DECODER,
171        FLUSHED,
172        SHUT_DOWN,
173    };
174
175    enum FlushCommand {
176        FLUSH_CMD_NONE,
177        FLUSH_CMD_FLUSH,
178        FLUSH_CMD_SHUTDOWN,
179    };
180
181    // Status of flush responses from the decoder and renderer.
182    bool mFlushComplete[2][2];
183
184    FlushStatus mFlushingAudio;
185    FlushStatus mFlushingVideo;
186
187    // Status of flush responses from the decoder and renderer.
188    bool mResumePending;
189
190    int32_t mVideoScalingMode;
191
192    AudioPlaybackRate mPlaybackSettings;
193    AVSyncSettings mSyncSettings;
194    float mVideoFpsHint;
195    bool mStarted;
196
197    // Actual pause state, either as requested by client or due to buffering.
198    bool mPaused;
199
200    // Pause state as requested by client. Note that if mPausedByClient is
201    // true, mPaused is always true; if mPausedByClient is false, mPaused could
202    // still become true, when we pause internally due to buffering.
203    bool mPausedByClient;
204
205    inline const sp<DecoderBase> &getDecoder(bool audio) {
206        return audio ? mAudioDecoder : mVideoDecoder;
207    }
208
209    inline void clearFlushComplete() {
210        mFlushComplete[0][0] = false;
211        mFlushComplete[0][1] = false;
212        mFlushComplete[1][0] = false;
213        mFlushComplete[1][1] = false;
214    }
215
216    void tryOpenAudioSinkForOffload(const sp<AMessage> &format, bool hasVideo);
217    void closeAudioSink();
218
219    status_t instantiateDecoder(bool audio, sp<DecoderBase> *decoder);
220
221    status_t onInstantiateSecureDecoders();
222
223    void updateVideoSize(
224            const sp<AMessage> &inputFormat,
225            const sp<AMessage> &outputFormat = NULL);
226
227    void notifyListener(int msg, int ext1, int ext2, const Parcel *in = NULL);
228
229    void handleFlushComplete(bool audio, bool isDecoder);
230    void finishFlushIfPossible();
231
232    void onStart();
233    void onResume();
234    void onPause();
235
236    bool audioDecoderStillNeeded();
237
238    void flushDecoder(bool audio, bool needShutdown);
239
240    void finishResume();
241
242    void postScanSources();
243
244    void schedulePollDuration();
245    void cancelPollDuration();
246
247    void processDeferredActions();
248
249    void performSeek(int64_t seekTimeUs, bool needNotify);
250    void performDecoderFlush(FlushCommand audio, FlushCommand video);
251    void performReset();
252    void performScanSources();
253    void performSetSurface(const sp<Surface> &wrapper);
254    void performResumeDecoders(bool needNotify);
255
256    void onSourceNotify(const sp<AMessage> &msg);
257    void onClosedCaptionNotify(const sp<AMessage> &msg);
258
259    void queueDecoderShutdown(
260            bool audio, bool video, const sp<AMessage> &reply);
261
262    void sendSubtitleData(const sp<ABuffer> &buffer, int32_t baseIndex);
263    void sendTimedMetaData(const sp<ABuffer> &buffer);
264    void sendTimedTextData(const sp<ABuffer> &buffer);
265
266    void writeTrackInfo(Parcel* reply, const sp<AMessage> format) const;
267
268    DISALLOW_EVIL_CONSTRUCTORS(NuPlayer);
269};
270
271}  // namespace android
272
273#endif  // NU_PLAYER_H_
274