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