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