NuPlayer.h revision 2bfdd428c56c7524d1a11979f200a1762866032d
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#include <gui/SurfaceTextureClient.h>
25#include <surfaceflinger/Surface.h>
26
27namespace android {
28
29struct ACodec;
30struct MetaData;
31struct NuPlayerDriver;
32
33struct NuPlayer : public AHandler {
34    NuPlayer();
35
36    void setUID(uid_t uid);
37
38    void setDriver(const wp<NuPlayerDriver> &driver);
39
40    void setDataSource(const sp<IStreamSource> &source);
41
42    void setDataSource(
43            const char *url, const KeyedVector<String8, String8> *headers);
44
45    void setVideoSurface(const sp<Surface> &surface);
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 HTTPLiveSource;
67    struct NuPlayerStreamListener;
68    struct Renderer;
69    struct Source;
70    struct StreamingSource;
71    struct RTSPSource;
72
73    enum {
74        kWhatSetDataSource              = '=DaS',
75        kWhatSetVideoNativeWindow       = '=NaW',
76        kWhatSetAudioSink               = '=AuS',
77        kWhatMoreDataQueued             = 'more',
78        kWhatStart                      = 'strt',
79        kWhatScanSources                = 'scan',
80        kWhatVideoNotify                = 'vidN',
81        kWhatAudioNotify                = 'audN',
82        kWhatRendererNotify             = 'renN',
83        kWhatReset                      = 'rset',
84        kWhatSeek                       = 'seek',
85        kWhatPause                      = 'paus',
86        kWhatResume                     = 'rsme',
87    };
88
89    wp<NuPlayerDriver> mDriver;
90    bool mUIDValid;
91    uid_t mUID;
92    sp<Source> mSource;
93    sp<NativeWindowWrapper> mNativeWindow;
94    sp<MediaPlayerBase::AudioSink> mAudioSink;
95    sp<Decoder> mVideoDecoder;
96    bool mVideoIsAVC;
97    sp<Decoder> mAudioDecoder;
98    sp<Renderer> mRenderer;
99
100    bool mAudioEOS;
101    bool mVideoEOS;
102
103    bool mScanSourcesPending;
104    int32_t mScanSourcesGeneration;
105
106    enum FlushStatus {
107        NONE,
108        AWAITING_DISCONTINUITY,
109        FLUSHING_DECODER,
110        FLUSHING_DECODER_SHUTDOWN,
111        SHUTTING_DOWN_DECODER,
112        FLUSHED,
113        SHUT_DOWN,
114    };
115
116    FlushStatus mFlushingAudio;
117    FlushStatus mFlushingVideo;
118    bool mResetInProgress;
119    bool mResetPostponed;
120
121    int64_t mSkipRenderingAudioUntilMediaTimeUs;
122    int64_t mSkipRenderingVideoUntilMediaTimeUs;
123
124    int64_t mVideoLateByUs;
125    int64_t mNumFramesTotal, mNumFramesDropped;
126
127    status_t instantiateDecoder(bool audio, sp<Decoder> *decoder);
128
129    status_t feedDecoderInputData(bool audio, const sp<AMessage> &msg);
130    void renderBuffer(bool audio, const sp<AMessage> &msg);
131
132    void notifyListener(int msg, int ext1, int ext2);
133
134    void finishFlushIfPossible();
135
136    void flushDecoder(bool audio, bool needShutdown);
137
138    static bool IsFlushingState(FlushStatus state, bool *needShutdown = NULL);
139
140    void finishReset();
141    void postScanSources();
142
143    DISALLOW_EVIL_CONSTRUCTORS(NuPlayer);
144};
145
146}  // namespace android
147
148#endif  // NU_PLAYER_H_
149