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