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