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