NuPlayer.h revision 14f7672b5d450ed26a06fd3bb3ce045ea78b11b2
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 char *url, const KeyedVector<String8, String8> *headers);
42
43    void setDataSourceAsync(int fd, int64_t offset, int64_t length);
44
45    void prepareAsync();
46
47    void setVideoSurfaceTextureAsync(
48            const sp<IGraphicBufferProducer> &bufferProducer);
49
50    void setAudioSink(const sp<MediaPlayerBase::AudioSink> &sink);
51    void start();
52
53    void pause();
54    void resume();
55
56    // Will notify the driver through "notifyResetComplete" once finished.
57    void resetAsync();
58
59    // Will notify the driver through "notifySeekComplete" once finished.
60    void seekToAsync(int64_t seekTimeUs);
61
62    status_t setVideoScalingMode(int32_t mode);
63
64protected:
65    virtual ~NuPlayer();
66
67    virtual void onMessageReceived(const sp<AMessage> &msg);
68
69public:
70    struct NuPlayerStreamListener;
71    struct Source;
72
73private:
74    struct Decoder;
75    struct GenericSource;
76    struct HTTPLiveSource;
77    struct Renderer;
78    struct RTSPSource;
79    struct StreamingSource;
80    struct Action;
81    struct SeekAction;
82    struct SetSurfaceAction;
83    struct ShutdownDecoderAction;
84    struct PostMessageAction;
85    struct SimpleAction;
86
87    enum {
88        kWhatSetDataSource              = '=DaS',
89        kWhatPrepare                    = 'prep',
90        kWhatSetVideoNativeWindow       = '=NaW',
91        kWhatSetAudioSink               = '=AuS',
92        kWhatMoreDataQueued             = 'more',
93        kWhatStart                      = 'strt',
94        kWhatScanSources                = 'scan',
95        kWhatVideoNotify                = 'vidN',
96        kWhatAudioNotify                = 'audN',
97        kWhatRendererNotify             = 'renN',
98        kWhatReset                      = 'rset',
99        kWhatSeek                       = 'seek',
100        kWhatPause                      = 'paus',
101        kWhatResume                     = 'rsme',
102        kWhatPollDuration               = 'polD',
103        kWhatSourceNotify               = 'srcN',
104    };
105
106    wp<NuPlayerDriver> mDriver;
107    bool mUIDValid;
108    uid_t mUID;
109    sp<Source> mSource;
110    uint32_t mSourceFlags;
111    sp<NativeWindowWrapper> mNativeWindow;
112    sp<MediaPlayerBase::AudioSink> mAudioSink;
113    sp<Decoder> mVideoDecoder;
114    bool mVideoIsAVC;
115    sp<Decoder> mAudioDecoder;
116    sp<Renderer> mRenderer;
117
118    List<sp<Action> > mDeferredActions;
119
120    bool mAudioEOS;
121    bool mVideoEOS;
122
123    bool mScanSourcesPending;
124    int32_t mScanSourcesGeneration;
125
126    int32_t mPollDurationGeneration;
127
128    enum FlushStatus {
129        NONE,
130        AWAITING_DISCONTINUITY,
131        FLUSHING_DECODER,
132        FLUSHING_DECODER_SHUTDOWN,
133        SHUTTING_DOWN_DECODER,
134        FLUSHED,
135        SHUT_DOWN,
136    };
137
138    // Once the current flush is complete this indicates whether the
139    // notion of time has changed.
140    bool mTimeDiscontinuityPending;
141
142    FlushStatus mFlushingAudio;
143    FlushStatus mFlushingVideo;
144
145    int64_t mSkipRenderingAudioUntilMediaTimeUs;
146    int64_t mSkipRenderingVideoUntilMediaTimeUs;
147
148    int64_t mVideoLateByUs;
149    int64_t mNumFramesTotal, mNumFramesDropped;
150
151    int32_t mVideoScalingMode;
152
153    bool mStarted;
154
155    status_t instantiateDecoder(bool audio, sp<Decoder> *decoder);
156
157    status_t feedDecoderInputData(bool audio, const sp<AMessage> &msg);
158    void renderBuffer(bool audio, const sp<AMessage> &msg);
159
160    void notifyListener(int msg, int ext1, int ext2);
161
162    void finishFlushIfPossible();
163
164    void flushDecoder(bool audio, bool needShutdown);
165
166    static bool IsFlushingState(FlushStatus state, bool *needShutdown = NULL);
167
168    void postScanSources();
169
170    void schedulePollDuration();
171    void cancelPollDuration();
172
173    void processDeferredActions();
174
175    void performSeek(int64_t seekTimeUs);
176    void performDecoderFlush();
177    void performDecoderShutdown(bool audio, bool video);
178    void performReset();
179    void performScanSources();
180    void performSetSurface(const sp<NativeWindowWrapper> &wrapper);
181
182    void onSourceNotify(const sp<AMessage> &msg);
183
184    void queueDecoderShutdown(
185            bool audio, bool video, const sp<AMessage> &reply);
186
187    DISALLOW_EVIL_CONSTRUCTORS(NuPlayer);
188};
189
190}  // namespace android
191
192#endif  // NU_PLAYER_H_
193