NuPlayer.h revision 7137ec7e005a5a6e3c0edb91cfacf16a31f4bf6a
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 FlushDecoderAction;
97    struct PostMessageAction;
98    struct SimpleAction;
99
100    enum {
101        kWhatSetDataSource              = '=DaS',
102        kWhatPrepare                    = 'prep',
103        kWhatSetVideoNativeWindow       = '=NaW',
104        kWhatSetAudioSink               = '=AuS',
105        kWhatMoreDataQueued             = 'more',
106        kWhatStart                      = 'strt',
107        kWhatScanSources                = 'scan',
108        kWhatVideoNotify                = 'vidN',
109        kWhatAudioNotify                = 'audN',
110        kWhatClosedCaptionNotify        = 'capN',
111        kWhatRendererNotify             = 'renN',
112        kWhatReset                      = 'rset',
113        kWhatSeek                       = 'seek',
114        kWhatPause                      = 'paus',
115        kWhatResume                     = 'rsme',
116        kWhatPollDuration               = 'polD',
117        kWhatSourceNotify               = 'srcN',
118        kWhatGetTrackInfo               = 'gTrI',
119        kWhatGetSelectedTrack           = 'gSel',
120        kWhatSelectTrack                = 'selT',
121    };
122
123    wp<NuPlayerDriver> mDriver;
124    bool mUIDValid;
125    uid_t mUID;
126    sp<Source> mSource;
127    uint32_t mSourceFlags;
128    sp<NativeWindowWrapper> mNativeWindow;
129    sp<MediaPlayerBase::AudioSink> mAudioSink;
130    sp<DecoderBase> mVideoDecoder;
131    bool mOffloadAudio;
132    sp<DecoderBase> mAudioDecoder;
133    sp<CCDecoder> mCCDecoder;
134    sp<Renderer> mRenderer;
135    sp<ALooper> mRendererLooper;
136    int32_t mAudioDecoderGeneration;
137    int32_t mVideoDecoderGeneration;
138    int32_t mRendererGeneration;
139
140    List<sp<Action> > mDeferredActions;
141
142    bool mAudioEOS;
143    bool mVideoEOS;
144
145    bool mScanSourcesPending;
146    int32_t mScanSourcesGeneration;
147
148    int32_t mPollDurationGeneration;
149    int32_t mTimedTextGeneration;
150
151    enum FlushStatus {
152        NONE,
153        FLUSHING_DECODER,
154        FLUSHING_DECODER_SHUTDOWN,
155        SHUTTING_DOWN_DECODER,
156        FLUSHED,
157        SHUT_DOWN,
158    };
159
160    enum FlushCommand {
161        FLUSH_CMD_NONE,
162        FLUSH_CMD_FLUSH,
163        FLUSH_CMD_SHUTDOWN,
164    };
165
166    // Status of flush responses from the decoder and renderer.
167    bool mFlushComplete[2][2];
168
169    FlushStatus mFlushingAudio;
170    FlushStatus mFlushingVideo;
171
172    int32_t mVideoScalingMode;
173
174    bool mStarted;
175
176    inline const sp<DecoderBase> &getDecoder(bool audio) {
177        return audio ? mAudioDecoder : mVideoDecoder;
178    }
179
180    inline void clearFlushComplete() {
181        mFlushComplete[0][0] = false;
182        mFlushComplete[0][1] = false;
183        mFlushComplete[1][0] = false;
184        mFlushComplete[1][1] = false;
185    }
186
187    void openAudioSink(const sp<AMessage> &format, bool offloadOnly);
188    void closeAudioSink();
189
190    status_t instantiateDecoder(bool audio, sp<DecoderBase> *decoder);
191
192    void updateVideoSize(
193            const sp<AMessage> &inputFormat,
194            const sp<AMessage> &outputFormat = NULL);
195
196    void notifyListener(int msg, int ext1, int ext2, const Parcel *in = NULL);
197
198    void handleFlushComplete(bool audio, bool isDecoder);
199    void finishFlushIfPossible();
200
201    void onStart();
202    void onResume();
203
204    bool audioDecoderStillNeeded();
205
206    void flushDecoder(bool audio, bool needShutdown);
207
208    void postScanSources();
209
210    void schedulePollDuration();
211    void cancelPollDuration();
212
213    void processDeferredActions();
214
215    void performSeek(int64_t seekTimeUs, bool needNotify);
216    void performDecoderFlush(FlushCommand audio, FlushCommand video);
217    void performReset();
218    void performScanSources();
219    void performSetSurface(const sp<NativeWindowWrapper> &wrapper);
220    void performResumeDecoders();
221
222    void onSourceNotify(const sp<AMessage> &msg);
223    void onClosedCaptionNotify(const sp<AMessage> &msg);
224
225    void queueDecoderShutdown(
226            bool audio, bool video, const sp<AMessage> &reply);
227
228    void sendSubtitleData(const sp<ABuffer> &buffer, int32_t baseIndex);
229    void sendTimedTextData(const sp<ABuffer> &buffer);
230
231    void writeTrackInfo(Parcel* reply, const sp<AMessage> format) const;
232
233    DISALLOW_EVIL_CONSTRUCTORS(NuPlayer);
234};
235
236}  // namespace android
237
238#endif  // NU_PLAYER_H_
239