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