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