NuPlayer.h revision ff63de757653579d751665bc3611571fbcacf53d
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
96    const char *getDataSourceType();
97
98protected:
99    virtual ~NuPlayer();
100
101    virtual void onMessageReceived(const sp<AMessage> &msg);
102
103public:
104    struct NuPlayerStreamListener;
105    struct Source;
106
107private:
108    struct Decoder;
109    struct DecoderBase;
110    struct DecoderPassThrough;
111    struct CCDecoder;
112    struct GenericSource;
113    struct HTTPLiveSource;
114    struct Renderer;
115    struct RTSPSource;
116    struct StreamingSource;
117    struct Action;
118    struct SeekAction;
119    struct SetSurfaceAction;
120    struct ResumeDecoderAction;
121    struct FlushDecoderAction;
122    struct PostMessageAction;
123    struct SimpleAction;
124
125    enum {
126        kWhatSetDataSource              = '=DaS',
127        kWhatPrepare                    = 'prep',
128        kWhatSetVideoSurface            = '=VSu',
129        kWhatSetAudioSink               = '=AuS',
130        kWhatMoreDataQueued             = 'more',
131        kWhatConfigPlayback             = 'cfPB',
132        kWhatConfigSync                 = 'cfSy',
133        kWhatGetPlaybackSettings        = 'gPbS',
134        kWhatGetSyncSettings            = 'gSyS',
135        kWhatStart                      = 'strt',
136        kWhatScanSources                = 'scan',
137        kWhatVideoNotify                = 'vidN',
138        kWhatAudioNotify                = 'audN',
139        kWhatClosedCaptionNotify        = 'capN',
140        kWhatRendererNotify             = 'renN',
141        kWhatReset                      = 'rset',
142        kWhatSeek                       = 'seek',
143        kWhatPause                      = 'paus',
144        kWhatResume                     = 'rsme',
145        kWhatPollDuration               = 'polD',
146        kWhatSourceNotify               = 'srcN',
147        kWhatGetTrackInfo               = 'gTrI',
148        kWhatGetSelectedTrack           = 'gSel',
149        kWhatSelectTrack                = 'selT',
150        kWhatGetDefaultBufferingSettings = 'gDBS',
151        kWhatSetBufferingSettings       = 'sBuS',
152        kWhatPrepareDrm                 = 'pDrm',
153        kWhatReleaseDrm                 = 'rDrm',
154    };
155
156    wp<NuPlayerDriver> mDriver;
157    bool mUIDValid;
158    uid_t mUID;
159    pid_t mPID;
160    Mutex mSourceLock;  // guard |mSource|.
161    sp<Source> mSource;
162    uint32_t mSourceFlags;
163    sp<Surface> mSurface;
164    sp<MediaPlayerBase::AudioSink> mAudioSink;
165    sp<DecoderBase> mVideoDecoder;
166    bool mOffloadAudio;
167    sp<DecoderBase> mAudioDecoder;
168    sp<CCDecoder> mCCDecoder;
169    sp<Renderer> mRenderer;
170    sp<ALooper> mRendererLooper;
171    int32_t mAudioDecoderGeneration;
172    int32_t mVideoDecoderGeneration;
173    int32_t mRendererGeneration;
174
175    int64_t mLastStartedPlayingTimeNs;
176
177    int64_t mPreviousSeekTimeUs;
178
179    List<sp<Action> > mDeferredActions;
180
181    bool mAudioEOS;
182    bool mVideoEOS;
183
184    bool mScanSourcesPending;
185    int32_t mScanSourcesGeneration;
186
187    int32_t mPollDurationGeneration;
188    int32_t mTimedTextGeneration;
189
190    enum FlushStatus {
191        NONE,
192        FLUSHING_DECODER,
193        FLUSHING_DECODER_SHUTDOWN,
194        SHUTTING_DOWN_DECODER,
195        FLUSHED,
196        SHUT_DOWN,
197    };
198
199    enum FlushCommand {
200        FLUSH_CMD_NONE,
201        FLUSH_CMD_FLUSH,
202        FLUSH_CMD_SHUTDOWN,
203    };
204
205    // Status of flush responses from the decoder and renderer.
206    bool mFlushComplete[2][2];
207
208    FlushStatus mFlushingAudio;
209    FlushStatus mFlushingVideo;
210
211    // Status of flush responses from the decoder and renderer.
212    bool mResumePending;
213
214    int32_t mVideoScalingMode;
215
216    AudioPlaybackRate mPlaybackSettings;
217    AVSyncSettings mSyncSettings;
218    float mVideoFpsHint;
219    bool mStarted;
220    bool mPrepared;
221    bool mResetting;
222    bool mSourceStarted;
223    bool mAudioDecoderError;
224    bool mVideoDecoderError;
225
226    // Actual pause state, either as requested by client or due to buffering.
227    bool mPaused;
228
229    // Pause state as requested by client. Note that if mPausedByClient is
230    // true, mPaused is always true; if mPausedByClient is false, mPaused could
231    // still become true, when we pause internally due to buffering.
232    bool mPausedByClient;
233
234    // Pause state as requested by source (internally) due to buffering
235    bool mPausedForBuffering;
236
237    // Modular DRM
238    sp<ICrypto> mCrypto;
239    bool mIsDrmProtected;
240
241    typedef enum {
242        DATA_SOURCE_TYPE_NONE,
243        DATA_SOURCE_TYPE_HTTP_LIVE,
244        DATA_SOURCE_TYPE_RTSP,
245        DATA_SOURCE_TYPE_GENERIC_URL,
246        DATA_SOURCE_TYPE_GENERIC_FD,
247        DATA_SOURCE_TYPE_MEDIA,
248        DATA_SOURCE_TYPE_STREAM,
249    } DATA_SOURCE_TYPE;
250
251    std::atomic<DATA_SOURCE_TYPE> mDataSourceType;
252
253    inline const sp<DecoderBase> &getDecoder(bool audio) {
254        return audio ? mAudioDecoder : mVideoDecoder;
255    }
256
257    inline void clearFlushComplete() {
258        mFlushComplete[0][0] = false;
259        mFlushComplete[0][1] = false;
260        mFlushComplete[1][0] = false;
261        mFlushComplete[1][1] = false;
262    }
263
264    void tryOpenAudioSinkForOffload(
265            const sp<AMessage> &format, const sp<MetaData> &audioMeta, bool hasVideo);
266    void closeAudioSink();
267    void restartAudio(
268            int64_t currentPositionUs, bool forceNonOffload, bool needsToCreateAudioDecoder);
269    void determineAudioModeChange(const sp<AMessage> &audioFormat);
270
271    status_t instantiateDecoder(
272            bool audio, sp<DecoderBase> *decoder, bool checkAudioModeChange = true);
273
274    status_t onInstantiateSecureDecoders();
275
276    void updateVideoSize(
277            const sp<AMessage> &inputFormat,
278            const sp<AMessage> &outputFormat = NULL);
279
280    void notifyListener(int msg, int ext1, int ext2, const Parcel *in = NULL);
281
282    void handleFlushComplete(bool audio, bool isDecoder);
283    void finishFlushIfPossible();
284
285    void onStart(
286            int64_t startPositionUs = -1,
287            MediaPlayerSeekMode mode = MediaPlayerSeekMode::SEEK_PREVIOUS_SYNC);
288    void onResume();
289    void onPause();
290
291    bool audioDecoderStillNeeded();
292
293    void flushDecoder(bool audio, bool needShutdown);
294
295    void finishResume();
296    void notifyDriverSeekComplete();
297
298    void postScanSources();
299
300    void schedulePollDuration();
301    void cancelPollDuration();
302
303    void processDeferredActions();
304
305    void performSeek(int64_t seekTimeUs, MediaPlayerSeekMode mode);
306    void performDecoderFlush(FlushCommand audio, FlushCommand video);
307    void performReset();
308    void performScanSources();
309    void performSetSurface(const sp<Surface> &wrapper);
310    void performResumeDecoders(bool needNotify);
311
312    void onSourceNotify(const sp<AMessage> &msg);
313    void onClosedCaptionNotify(const sp<AMessage> &msg);
314
315    void queueDecoderShutdown(
316            bool audio, bool video, const sp<AMessage> &reply);
317
318    void sendSubtitleData(const sp<ABuffer> &buffer, int32_t baseIndex);
319    void sendTimedMetaData(const sp<ABuffer> &buffer);
320    void sendTimedTextData(const sp<ABuffer> &buffer);
321
322    void writeTrackInfo(Parcel* reply, const sp<AMessage>& format) const;
323
324    status_t onPrepareDrm(const sp<AMessage> &msg);
325    status_t onReleaseDrm();
326
327    DISALLOW_EVIL_CONSTRUCTORS(NuPlayer);
328};
329
330}  // namespace android
331
332#endif  // NU_PLAYER_H_
333