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