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