NuPlayer.h revision 1b86fe063badb5f28c467ade39be0f4008688947
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    bool mNeedsSwRenderer;
122    sp<Decoder> mAudioDecoder;
123    sp<Renderer> mRenderer;
124
125    List<sp<Action> > mDeferredActions;
126
127    bool mAudioEOS;
128    bool mVideoEOS;
129
130    bool mScanSourcesPending;
131    int32_t mScanSourcesGeneration;
132
133    int32_t mPollDurationGeneration;
134
135    enum FlushStatus {
136        NONE,
137        AWAITING_DISCONTINUITY,
138        FLUSHING_DECODER,
139        FLUSHING_DECODER_SHUTDOWN,
140        SHUTTING_DOWN_DECODER,
141        FLUSHED,
142        SHUT_DOWN,
143    };
144
145    // Once the current flush is complete this indicates whether the
146    // notion of time has changed.
147    bool mTimeDiscontinuityPending;
148
149    FlushStatus mFlushingAudio;
150    FlushStatus mFlushingVideo;
151
152    int64_t mSkipRenderingAudioUntilMediaTimeUs;
153    int64_t mSkipRenderingVideoUntilMediaTimeUs;
154
155    int64_t mVideoLateByUs;
156    int64_t mNumFramesTotal, mNumFramesDropped;
157
158    int32_t mVideoScalingMode;
159
160    bool mStarted;
161
162    status_t instantiateDecoder(bool audio, sp<Decoder> *decoder);
163
164    status_t feedDecoderInputData(bool audio, const sp<AMessage> &msg);
165    void renderBuffer(bool audio, const sp<AMessage> &msg);
166
167    void notifyListener(int msg, int ext1, int ext2, const Parcel *in = NULL);
168
169    void finishFlushIfPossible();
170
171    void flushDecoder(bool audio, bool needShutdown);
172
173    static bool IsFlushingState(FlushStatus state, bool *needShutdown = NULL);
174
175    void postScanSources();
176
177    void schedulePollDuration();
178    void cancelPollDuration();
179
180    void processDeferredActions();
181
182    void performSeek(int64_t seekTimeUs);
183    void performDecoderFlush();
184    void performDecoderShutdown(bool audio, bool video);
185    void performReset();
186    void performScanSources();
187    void performSetSurface(const sp<NativeWindowWrapper> &wrapper);
188
189    void onSourceNotify(const sp<AMessage> &msg);
190
191    void queueDecoderShutdown(
192            bool audio, bool video, const sp<AMessage> &reply);
193
194    DISALLOW_EVIL_CONSTRUCTORS(NuPlayer);
195};
196
197}  // namespace android
198
199#endif  // NU_PLAYER_H_
200