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