AwesomePlayer.h revision e39350924f4e743ec04cc9640526e06990c8cbda
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
97    status_t seekTo(int64_t timeUs);
98
99    // This is a mask of MediaExtractor::Flags.
100    uint32_t flags() const;
101
102    void postAudioEOS(int64_t delayUs = 0ll);
103    void postAudioSeekComplete();
104
105    status_t setTimedTextTrackIndex(int32_t index);
106
107private:
108    friend struct AwesomeEvent;
109    friend struct PreviewPlayer;
110
111    enum {
112        PLAYING             = 0x01,
113        LOOPING             = 0x02,
114        FIRST_FRAME         = 0x04,
115        PREPARING           = 0x08,
116        PREPARED            = 0x10,
117        AT_EOS              = 0x20,
118        PREPARE_CANCELLED   = 0x40,
119        CACHE_UNDERRUN      = 0x80,
120        AUDIO_AT_EOS        = 0x0100,
121        VIDEO_AT_EOS        = 0x0200,
122        AUTO_LOOPING        = 0x0400,
123
124        // We are basically done preparing but are currently buffering
125        // sufficient data to begin playback and finish the preparation phase
126        // for good.
127        PREPARING_CONNECTED = 0x0800,
128
129        // We're triggering a single video event to display the first frame
130        // after the seekpoint.
131        SEEK_PREVIEW        = 0x1000,
132
133        AUDIO_RUNNING       = 0x2000,
134        AUDIOPLAYER_STARTED = 0x4000,
135
136        INCOGNITO           = 0x8000,
137
138        TEXT_RUNNING        = 0x10000,
139        TEXTPLAYER_STARTED  = 0x20000,
140    };
141
142    mutable Mutex mLock;
143    Mutex mMiscStateLock;
144
145    OMXClient mClient;
146    TimedEventQueue mQueue;
147    bool mQueueStarted;
148    wp<MediaPlayerBase> mListener;
149
150    sp<Surface> mSurface;
151    sp<ANativeWindow> mNativeWindow;
152    sp<MediaPlayerBase::AudioSink> mAudioSink;
153
154    SystemTimeSource mSystemTimeSource;
155    TimeSource *mTimeSource;
156
157    String8 mUri;
158    KeyedVector<String8, String8> mUriHeaders;
159
160    sp<DataSource> mFileSource;
161
162    sp<MediaSource> mVideoTrack;
163    sp<MediaSource> mVideoSource;
164    sp<AwesomeRenderer> mVideoRenderer;
165    bool mVideoRendererIsPreview;
166
167    sp<MediaSource> mAudioTrack;
168    sp<MediaSource> mAudioSource;
169    AudioPlayer *mAudioPlayer;
170    int64_t mDurationUs;
171
172    int32_t mDisplayWidth;
173    int32_t mDisplayHeight;
174
175    uint32_t mFlags;
176    uint32_t mExtractorFlags;
177
178    int64_t mTimeSourceDeltaUs;
179    int64_t mVideoTimeUs;
180
181    enum SeekType {
182        NO_SEEK,
183        SEEK,
184        SEEK_VIDEO_ONLY
185    };
186    SeekType mSeeking;
187
188    bool mSeekNotificationSent;
189    int64_t mSeekTimeUs;
190
191    int64_t mBitrate;  // total bitrate of the file (in bps) or -1 if unknown.
192
193    bool mWatchForAudioSeekComplete;
194    bool mWatchForAudioEOS;
195
196    sp<TimedEventQueue::Event> mVideoEvent;
197    bool mVideoEventPending;
198    sp<TimedEventQueue::Event> mStreamDoneEvent;
199    bool mStreamDoneEventPending;
200    sp<TimedEventQueue::Event> mBufferingEvent;
201    bool mBufferingEventPending;
202    sp<TimedEventQueue::Event> mCheckAudioStatusEvent;
203    bool mAudioStatusEventPending;
204    sp<TimedEventQueue::Event> mVideoLagEvent;
205    bool mVideoLagEventPending;
206
207    sp<TimedEventQueue::Event> mAsyncPrepareEvent;
208    Condition mPreparedCondition;
209    bool mIsAsyncPrepare;
210    status_t mPrepareResult;
211    status_t mStreamDoneStatus;
212
213    void postVideoEvent_l(int64_t delayUs = -1);
214    void postBufferingEvent_l();
215    void postStreamDoneEvent_l(status_t status);
216    void postCheckAudioStatusEvent_l(int64_t delayUs);
217    void postVideoLagEvent_l();
218    status_t play_l();
219
220    MediaBuffer *mVideoBuffer;
221
222    sp<HTTPBase> mConnectingDataSource;
223    sp<NuCachedSource2> mCachedSource;
224
225    sp<ALooper> mLooper;
226    sp<ARTSPController> mRTSPController;
227    sp<ARTSPController> mConnectingRTSPController;
228
229    DrmManagerClient *mDrmManagerClient;
230    sp<DecryptHandle> mDecryptHandle;
231
232    int64_t mLastVideoTimeUs;
233    TimedTextPlayer *mTextPlayer;
234
235    sp<WVMExtractor> mWVMExtractor;
236
237    status_t setDataSource_l(
238            const char *uri,
239            const KeyedVector<String8, String8> *headers = NULL);
240
241    status_t setDataSource_l(const sp<DataSource> &dataSource);
242    status_t setDataSource_l(const sp<MediaExtractor> &extractor);
243    void reset_l();
244    status_t seekTo_l(int64_t timeUs);
245    status_t pause_l(bool at_eos = false);
246    void initRenderer_l();
247    void notifyVideoSize_l();
248    void seekAudioIfNecessary_l();
249
250    void cancelPlayerEvents(bool keepBufferingGoing = false);
251
252    void setAudioSource(sp<MediaSource> source);
253    status_t initAudioDecoder();
254
255    void setVideoSource(sp<MediaSource> source);
256    status_t initVideoDecoder(uint32_t flags = 0);
257
258    void addTextSource(sp<MediaSource> source);
259
260    void onStreamDone();
261
262    void notifyListener_l(int msg, int ext1 = 0, int ext2 = 0);
263
264    void onVideoEvent();
265    void onBufferingUpdate();
266    void onCheckAudioStatus();
267    void onPrepareAsyncEvent();
268    void abortPrepare(status_t err);
269    void finishAsyncPrepare_l();
270    void onVideoLagUpdate();
271
272    bool getCachedDuration_l(int64_t *durationUs, bool *eos);
273
274    status_t finishSetDataSource_l();
275
276    static bool ContinuePreparation(void *cookie);
277
278    static void OnRTSPSeekDoneWrapper(void *cookie);
279    void onRTSPSeekDone();
280
281    bool getBitrate(int64_t *bitrate);
282
283    void finishSeekIfNecessary(int64_t videoTimeUs);
284    void ensureCacheIsFetching_l();
285
286    status_t startAudioPlayer_l();
287    void postAudioSeekComplete_l();
288
289    void shutdownVideoDecoder_l();
290    void setNativeWindow_l(const sp<ANativeWindow> &native);
291
292    bool isStreamingHTTP() const;
293
294    AwesomePlayer(const AwesomePlayer &);
295    AwesomePlayer &operator=(const AwesomePlayer &);
296};
297
298}  // namespace android
299
300#endif  // AWESOME_PLAYER_H_
301
302