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