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