NuPlayer.h revision cefac14261a32fb856b0d1ab31541787112e306e
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
222    // Actual pause state, either as requested by client or due to buffering.
223    bool mPaused;
224
225    // Pause state as requested by client. Note that if mPausedByClient is
226    // true, mPaused is always true; if mPausedByClient is false, mPaused could
227    // still become true, when we pause internally due to buffering.
228    bool mPausedByClient;
229
230    // Pause state as requested by source (internally) due to buffering
231    bool mPausedForBuffering;
232
233    // Modular DRM
234    sp<ICrypto> mCrypto;
235    bool mIsDrmProtected;
236
237    inline const sp<DecoderBase> &getDecoder(bool audio) {
238        return audio ? mAudioDecoder : mVideoDecoder;
239    }
240
241    inline void clearFlushComplete() {
242        mFlushComplete[0][0] = false;
243        mFlushComplete[0][1] = false;
244        mFlushComplete[1][0] = false;
245        mFlushComplete[1][1] = false;
246    }
247
248    void tryOpenAudioSinkForOffload(
249            const sp<AMessage> &format, const sp<MetaData> &audioMeta, bool hasVideo);
250    void closeAudioSink();
251    void restartAudio(
252            int64_t currentPositionUs, bool forceNonOffload, bool needsToCreateAudioDecoder);
253    void determineAudioModeChange(const sp<AMessage> &audioFormat);
254
255    status_t instantiateDecoder(
256            bool audio, sp<DecoderBase> *decoder, bool checkAudioModeChange = true);
257
258    status_t onInstantiateSecureDecoders();
259
260    void updateVideoSize(
261            const sp<AMessage> &inputFormat,
262            const sp<AMessage> &outputFormat = NULL);
263
264    void notifyListener(int msg, int ext1, int ext2, const Parcel *in = NULL);
265
266    void handleFlushComplete(bool audio, bool isDecoder);
267    void finishFlushIfPossible();
268
269    void onStart(
270            int64_t startPositionUs = -1,
271            MediaPlayerSeekMode mode = MediaPlayerSeekMode::SEEK_PREVIOUS_SYNC);
272    void onResume();
273    void onPause();
274
275    bool audioDecoderStillNeeded();
276
277    void flushDecoder(bool audio, bool needShutdown);
278
279    void finishResume();
280    void notifyDriverSeekComplete();
281
282    void postScanSources();
283
284    void schedulePollDuration();
285    void cancelPollDuration();
286
287    void processDeferredActions();
288
289    void performSeek(int64_t seekTimeUs, MediaPlayerSeekMode mode);
290    void performDecoderFlush(FlushCommand audio, FlushCommand video);
291    void performReset();
292    void performScanSources();
293    void performSetSurface(const sp<Surface> &wrapper);
294    void performResumeDecoders(bool needNotify);
295
296    void onSourceNotify(const sp<AMessage> &msg);
297    void onClosedCaptionNotify(const sp<AMessage> &msg);
298
299    void queueDecoderShutdown(
300            bool audio, bool video, const sp<AMessage> &reply);
301
302    void sendSubtitleData(const sp<ABuffer> &buffer, int32_t baseIndex);
303    void sendTimedMetaData(const sp<ABuffer> &buffer);
304    void sendTimedTextData(const sp<ABuffer> &buffer);
305
306    void writeTrackInfo(Parcel* reply, const sp<AMessage>& format) const;
307
308    status_t onPrepareDrm(const sp<AMessage> &msg);
309    status_t onReleaseDrm();
310
311    DISALLOW_EVIL_CONSTRUCTORS(NuPlayer);
312};
313
314}  // namespace android
315
316#endif  // NU_PLAYER_H_
317