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