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