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