NuPlayer.h revision ac428aa54d2489705091dd38372bbaade281a92e
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    void resume();
58
59    // Will notify the driver through "notifyResetComplete" once finished.
60    void resetAsync();
61
62    // Will notify the driver through "notifySeekComplete" once finished.
63    void seekToAsync(int64_t seekTimeUs);
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);
69
70protected:
71    virtual ~NuPlayer();
72
73    virtual void onMessageReceived(const sp<AMessage> &msg);
74
75public:
76    struct NuPlayerStreamListener;
77    struct Source;
78
79private:
80    struct Decoder;
81    struct DecoderPassThrough;
82    struct CCDecoder;
83    struct GenericSource;
84    struct HTTPLiveSource;
85    struct Renderer;
86    struct RTSPSource;
87    struct StreamingSource;
88    struct Action;
89    struct SeekAction;
90    struct SetSurfaceAction;
91    struct ShutdownDecoderAction;
92    struct PostMessageAction;
93    struct SimpleAction;
94
95    enum {
96        kWhatSetDataSource              = '=DaS',
97        kWhatPrepare                    = 'prep',
98        kWhatSetVideoNativeWindow       = '=NaW',
99        kWhatSetAudioSink               = '=AuS',
100        kWhatMoreDataQueued             = 'more',
101        kWhatStart                      = 'strt',
102        kWhatScanSources                = 'scan',
103        kWhatVideoNotify                = 'vidN',
104        kWhatAudioNotify                = 'audN',
105        kWhatClosedCaptionNotify        = 'capN',
106        kWhatRendererNotify             = 'renN',
107        kWhatReset                      = 'rset',
108        kWhatSeek                       = 'seek',
109        kWhatPause                      = 'paus',
110        kWhatResume                     = 'rsme',
111        kWhatPollDuration               = 'polD',
112        kWhatSourceNotify               = 'srcN',
113        kWhatGetTrackInfo               = 'gTrI',
114        kWhatGetSelectedTrack           = 'gSel',
115        kWhatSelectTrack                = 'selT',
116    };
117
118    wp<NuPlayerDriver> mDriver;
119    bool mUIDValid;
120    uid_t mUID;
121    sp<Source> mSource;
122    uint32_t mSourceFlags;
123    sp<NativeWindowWrapper> mNativeWindow;
124    int64_t mCurrentPositionUs;
125    sp<MediaPlayerBase::AudioSink> mAudioSink;
126    sp<Decoder> mVideoDecoder;
127    bool mVideoIsAVC;
128    bool mOffloadAudio;
129    audio_offload_info_t mCurrentOffloadInfo;
130    sp<Decoder> mAudioDecoder;
131    sp<CCDecoder> mCCDecoder;
132    sp<Renderer> mRenderer;
133    sp<ALooper> mRendererLooper;
134    int32_t mAudioDecoderGeneration;
135    int32_t mVideoDecoderGeneration;
136
137    List<sp<Action> > mDeferredActions;
138
139    bool mAudioEOS;
140    bool mVideoEOS;
141
142    bool mScanSourcesPending;
143    int32_t mScanSourcesGeneration;
144
145    int32_t mPollDurationGeneration;
146    int32_t mTimedTextGeneration;
147
148    enum FlushStatus {
149        NONE,
150        FLUSHING_DECODER,
151        FLUSHING_DECODER_SHUTDOWN,
152        SHUTTING_DOWN_DECODER,
153        FLUSHED,
154        SHUT_DOWN,
155    };
156
157    // Once the current flush is complete this indicates whether the
158    // notion of time has changed.
159    bool mTimeDiscontinuityPending;
160
161    FlushStatus mFlushingAudio;
162    FlushStatus mFlushingVideo;
163
164    int64_t mSkipRenderingAudioUntilMediaTimeUs;
165    int64_t mSkipRenderingVideoUntilMediaTimeUs;
166
167    int64_t mVideoLateByUs;
168    int64_t mNumFramesTotal, mNumFramesDropped;
169
170    int32_t mVideoScalingMode;
171
172    bool mStarted;
173
174    inline const sp<Decoder> &getDecoder(bool audio) {
175        return audio ? mAudioDecoder : mVideoDecoder;
176    }
177
178    void openAudioSink(const sp<AMessage> &format, bool offloadOnly);
179    void closeAudioSink();
180
181    status_t instantiateDecoder(bool audio, sp<Decoder> *decoder);
182
183    void updateVideoSize(
184            const sp<AMessage> &inputFormat,
185            const sp<AMessage> &outputFormat = NULL);
186
187    status_t feedDecoderInputData(bool audio, const sp<AMessage> &msg);
188    void renderBuffer(bool audio, const sp<AMessage> &msg);
189
190    void notifyListener(int msg, int ext1, int ext2, const Parcel *in = NULL);
191
192    void finishFlushIfPossible();
193
194    void flushDecoder(
195            bool audio, bool needShutdown, const sp<AMessage> &newFormat = NULL);
196    void updateDecoderFormatWithoutFlush(bool audio, const sp<AMessage> &format);
197
198    static bool IsFlushingState(FlushStatus state, bool *needShutdown = NULL);
199
200    void postScanSources();
201
202    void schedulePollDuration();
203    void cancelPollDuration();
204
205    void processDeferredActions();
206
207    void performSeek(int64_t seekTimeUs);
208    void performDecoderFlush();
209    void performDecoderShutdown(bool audio, bool video);
210    void performReset();
211    void performScanSources();
212    void performSetSurface(const sp<NativeWindowWrapper> &wrapper);
213
214    void onSourceNotify(const sp<AMessage> &msg);
215    void onClosedCaptionNotify(const sp<AMessage> &msg);
216
217    void queueDecoderShutdown(
218            bool audio, bool video, const sp<AMessage> &reply);
219
220    void sendSubtitleData(const sp<ABuffer> &buffer, int32_t baseIndex);
221    void sendTimedTextData(const sp<ABuffer> &buffer);
222
223    void writeTrackInfo(Parcel* reply, const sp<AMessage> format) const;
224
225    DISALLOW_EVIL_CONSTRUCTORS(NuPlayer);
226};
227
228}  // namespace android
229
230#endif  // NU_PLAYER_H_
231