AwesomePlayer.h revision 5b1b8a93a07326f1cbc627f09e02988375189e0a
1/*
2 * Copyright (C) 2009 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 AWESOME_PLAYER_H_
18
19#define AWESOME_PLAYER_H_
20
21#include "HTTPBase.h"
22#include "TimedEventQueue.h"
23
24#include <media/MediaPlayerInterface.h>
25#include <media/stagefright/DataSource.h>
26#include <media/stagefright/OMXClient.h>
27#include <media/stagefright/TimeSource.h>
28#include <utils/threads.h>
29#include <drm/DrmManagerClient.h>
30
31namespace android {
32
33struct AudioPlayer;
34struct DataSource;
35struct MediaBuffer;
36struct MediaExtractor;
37struct MediaSource;
38struct NuCachedSource2;
39struct ISurfaceTexture;
40
41struct ALooper;
42struct ARTSPController;
43
44class DrmManagerClinet;
45class DecryptHandle;
46
47class TimedTextPlayer;
48struct WVMExtractor;
49
50struct AwesomeRenderer : public RefBase {
51    AwesomeRenderer() {}
52
53    virtual void render(MediaBuffer *buffer) = 0;
54
55private:
56    AwesomeRenderer(const AwesomeRenderer &);
57    AwesomeRenderer &operator=(const AwesomeRenderer &);
58};
59
60struct AwesomePlayer {
61    AwesomePlayer();
62    ~AwesomePlayer();
63
64    void setListener(const wp<MediaPlayerBase> &listener);
65
66    status_t setDataSource(
67            const char *uri,
68            const KeyedVector<String8, String8> *headers = NULL);
69
70    status_t setDataSource(int fd, int64_t offset, int64_t length);
71
72    status_t setDataSource(const sp<IStreamSource> &source);
73
74    void reset();
75
76    status_t prepare();
77    status_t prepare_l();
78    status_t prepareAsync();
79    status_t prepareAsync_l();
80
81    status_t play();
82    status_t pause();
83
84    bool isPlaying() const;
85
86    void setSurface(const sp<Surface> &surface);
87    void setSurfaceTexture(const sp<ISurfaceTexture> &surfaceTexture);
88    void setAudioSink(const sp<MediaPlayerBase::AudioSink> &audioSink);
89    status_t setLooping(bool shouldLoop);
90
91    status_t getDuration(int64_t *durationUs);
92    status_t getPosition(int64_t *positionUs);
93
94    status_t setParameter(int key, const Parcel &request);
95    status_t getParameter(int key, Parcel *reply);
96    status_t setCacheStatCollectFreq(const Parcel &request);
97
98    status_t seekTo(int64_t timeUs);
99
100    // This is a mask of MediaExtractor::Flags.
101    uint32_t flags() const;
102
103    void postAudioEOS(int64_t delayUs = 0ll);
104    void postAudioSeekComplete();
105
106    status_t setTimedTextTrackIndex(int32_t index);
107
108private:
109    friend struct AwesomeEvent;
110    friend struct PreviewPlayer;
111
112    enum {
113        PLAYING             = 0x01,
114        LOOPING             = 0x02,
115        FIRST_FRAME         = 0x04,
116        PREPARING           = 0x08,
117        PREPARED            = 0x10,
118        AT_EOS              = 0x20,
119        PREPARE_CANCELLED   = 0x40,
120        CACHE_UNDERRUN      = 0x80,
121        AUDIO_AT_EOS        = 0x0100,
122        VIDEO_AT_EOS        = 0x0200,
123        AUTO_LOOPING        = 0x0400,
124
125        // We are basically done preparing but are currently buffering
126        // sufficient data to begin playback and finish the preparation phase
127        // for good.
128        PREPARING_CONNECTED = 0x0800,
129
130        // We're triggering a single video event to display the first frame
131        // after the seekpoint.
132        SEEK_PREVIEW        = 0x1000,
133
134        AUDIO_RUNNING       = 0x2000,
135        AUDIOPLAYER_STARTED = 0x4000,
136
137        INCOGNITO           = 0x8000,
138
139        TEXT_RUNNING        = 0x10000,
140        TEXTPLAYER_STARTED  = 0x20000,
141    };
142
143    mutable Mutex mLock;
144    Mutex mMiscStateLock;
145
146    OMXClient mClient;
147    TimedEventQueue mQueue;
148    bool mQueueStarted;
149    wp<MediaPlayerBase> mListener;
150
151    sp<Surface> mSurface;
152    sp<ANativeWindow> mNativeWindow;
153    sp<MediaPlayerBase::AudioSink> mAudioSink;
154
155    SystemTimeSource mSystemTimeSource;
156    TimeSource *mTimeSource;
157
158    String8 mUri;
159    KeyedVector<String8, String8> mUriHeaders;
160
161    sp<DataSource> mFileSource;
162
163    sp<MediaSource> mVideoTrack;
164    sp<MediaSource> mVideoSource;
165    sp<AwesomeRenderer> mVideoRenderer;
166    bool mVideoRendererIsPreview;
167
168    sp<MediaSource> mAudioTrack;
169    sp<MediaSource> mAudioSource;
170    AudioPlayer *mAudioPlayer;
171    int64_t mDurationUs;
172
173    int32_t mDisplayWidth;
174    int32_t mDisplayHeight;
175
176    uint32_t mFlags;
177    uint32_t mExtractorFlags;
178
179    int64_t mTimeSourceDeltaUs;
180    int64_t mVideoTimeUs;
181
182    enum SeekType {
183        NO_SEEK,
184        SEEK,
185        SEEK_VIDEO_ONLY
186    };
187    SeekType mSeeking;
188
189    bool mSeekNotificationSent;
190    int64_t mSeekTimeUs;
191
192    int64_t mBitrate;  // total bitrate of the file (in bps) or -1 if unknown.
193
194    bool mWatchForAudioSeekComplete;
195    bool mWatchForAudioEOS;
196
197    sp<TimedEventQueue::Event> mVideoEvent;
198    bool mVideoEventPending;
199    sp<TimedEventQueue::Event> mStreamDoneEvent;
200    bool mStreamDoneEventPending;
201    sp<TimedEventQueue::Event> mBufferingEvent;
202    bool mBufferingEventPending;
203    sp<TimedEventQueue::Event> mCheckAudioStatusEvent;
204    bool mAudioStatusEventPending;
205    sp<TimedEventQueue::Event> mVideoLagEvent;
206    bool mVideoLagEventPending;
207
208    sp<TimedEventQueue::Event> mAsyncPrepareEvent;
209    Condition mPreparedCondition;
210    bool mIsAsyncPrepare;
211    status_t mPrepareResult;
212    status_t mStreamDoneStatus;
213
214    void postVideoEvent_l(int64_t delayUs = -1);
215    void postBufferingEvent_l();
216    void postStreamDoneEvent_l(status_t status);
217    void postCheckAudioStatusEvent_l(int64_t delayUs);
218    void postVideoLagEvent_l();
219    status_t play_l();
220
221    MediaBuffer *mVideoBuffer;
222
223    sp<HTTPBase> mConnectingDataSource;
224    sp<NuCachedSource2> mCachedSource;
225
226    sp<ALooper> mLooper;
227    sp<ARTSPController> mRTSPController;
228    sp<ARTSPController> mConnectingRTSPController;
229
230    DrmManagerClient *mDrmManagerClient;
231    sp<DecryptHandle> mDecryptHandle;
232
233    int64_t mLastVideoTimeUs;
234    TimedTextPlayer *mTextPlayer;
235    mutable Mutex mTimedTextLock;
236
237    sp<WVMExtractor> mWVMExtractor;
238
239    status_t setDataSource_l(
240            const char *uri,
241            const KeyedVector<String8, String8> *headers = NULL);
242
243    status_t setDataSource_l(const sp<DataSource> &dataSource);
244    status_t setDataSource_l(const sp<MediaExtractor> &extractor);
245    void reset_l();
246    status_t seekTo_l(int64_t timeUs);
247    status_t pause_l(bool at_eos = false);
248    void initRenderer_l();
249    void notifyVideoSize_l();
250    void seekAudioIfNecessary_l();
251
252    void cancelPlayerEvents(bool keepBufferingGoing = false);
253
254    void setAudioSource(sp<MediaSource> source);
255    status_t initAudioDecoder();
256
257    void setVideoSource(sp<MediaSource> source);
258    status_t initVideoDecoder(uint32_t flags = 0);
259
260    void addTextSource(sp<MediaSource> source);
261
262    void onStreamDone();
263
264    void notifyListener_l(int msg, int ext1 = 0, int ext2 = 0);
265
266    void onVideoEvent();
267    void onBufferingUpdate();
268    void onCheckAudioStatus();
269    void onPrepareAsyncEvent();
270    void abortPrepare(status_t err);
271    void finishAsyncPrepare_l();
272    void onVideoLagUpdate();
273
274    bool getCachedDuration_l(int64_t *durationUs, bool *eos);
275
276    status_t finishSetDataSource_l();
277
278    static bool ContinuePreparation(void *cookie);
279
280    static void OnRTSPSeekDoneWrapper(void *cookie);
281    void onRTSPSeekDone();
282
283    bool getBitrate(int64_t *bitrate);
284
285    void finishSeekIfNecessary(int64_t videoTimeUs);
286    void ensureCacheIsFetching_l();
287
288    status_t startAudioPlayer_l();
289    void postAudioSeekComplete_l();
290
291    void shutdownVideoDecoder_l();
292    void setNativeWindow_l(const sp<ANativeWindow> &native);
293
294    bool isStreamingHTTP() const;
295    void sendCacheStats();
296
297    AwesomePlayer(const AwesomePlayer &);
298    AwesomePlayer &operator=(const AwesomePlayer &);
299};
300
301}  // namespace android
302
303#endif  // AWESOME_PLAYER_H_
304
305