AwesomePlayer.h revision ccb3506ed1845121b94f4e27244e61a46eb19815
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;
42struct ARTPSession;
43struct UDPPusher;
44
45class DrmManagerClinet;
46class DecryptHandle;
47
48struct LiveSession;
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 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 seekTo(int64_t timeUs);
94
95    // This is a mask of MediaExtractor::Flags.
96    uint32_t flags() const;
97
98    void postAudioEOS();
99    void postAudioSeekComplete();
100
101private:
102    friend struct AwesomeEvent;
103    friend struct PreviewPlayer;
104
105    enum {
106        PLAYING             = 1,
107        LOOPING             = 2,
108        FIRST_FRAME         = 4,
109        PREPARING           = 8,
110        PREPARED            = 16,
111        AT_EOS              = 32,
112        PREPARE_CANCELLED   = 64,
113        CACHE_UNDERRUN      = 128,
114        AUDIO_AT_EOS        = 256,
115        VIDEO_AT_EOS        = 512,
116        AUTO_LOOPING        = 1024,
117
118        // We are basically done preparing but are currently buffering
119        // sufficient data to begin playback and finish the preparation phase
120        // for good.
121        PREPARING_CONNECTED = 2048,
122    };
123
124    mutable Mutex mLock;
125    Mutex mMiscStateLock;
126
127    OMXClient mClient;
128    TimedEventQueue mQueue;
129    bool mQueueStarted;
130    wp<MediaPlayerBase> mListener;
131
132    sp<Surface> mSurface;
133    sp<MediaPlayerBase::AudioSink> mAudioSink;
134
135    SystemTimeSource mSystemTimeSource;
136    TimeSource *mTimeSource;
137
138    String8 mUri;
139    KeyedVector<String8, String8> mUriHeaders;
140
141    sp<DataSource> mFileSource;
142
143    sp<MediaSource> mVideoTrack;
144    sp<MediaSource> mVideoSource;
145    sp<AwesomeRenderer> mVideoRenderer;
146    bool mVideoRendererIsPreview;
147
148    sp<MediaSource> mAudioTrack;
149    sp<MediaSource> mAudioSource;
150    AudioPlayer *mAudioPlayer;
151    int64_t mDurationUs;
152
153    uint32_t mFlags;
154    uint32_t mExtractorFlags;
155    uint32_t mSinceLastDropped;
156
157    int64_t mTimeSourceDeltaUs;
158    int64_t mVideoTimeUs;
159
160    bool mSeeking;
161    bool mSeekNotificationSent;
162    int64_t mSeekTimeUs;
163
164    int64_t mBitrate;  // total bitrate of the file (in bps) or -1 if unknown.
165
166    bool mWatchForAudioSeekComplete;
167    bool mWatchForAudioEOS;
168
169    sp<TimedEventQueue::Event> mVideoEvent;
170    bool mVideoEventPending;
171    sp<TimedEventQueue::Event> mStreamDoneEvent;
172    bool mStreamDoneEventPending;
173    sp<TimedEventQueue::Event> mBufferingEvent;
174    bool mBufferingEventPending;
175    sp<TimedEventQueue::Event> mCheckAudioStatusEvent;
176    bool mAudioStatusEventPending;
177    sp<TimedEventQueue::Event> mVideoLagEvent;
178    bool mVideoLagEventPending;
179
180    sp<TimedEventQueue::Event> mAsyncPrepareEvent;
181    Condition mPreparedCondition;
182    bool mIsAsyncPrepare;
183    status_t mPrepareResult;
184    status_t mStreamDoneStatus;
185
186    void postVideoEvent_l(int64_t delayUs = -1);
187    void postBufferingEvent_l();
188    void postStreamDoneEvent_l(status_t status);
189    void postCheckAudioStatusEvent_l();
190    void postVideoLagEvent_l();
191    status_t play_l();
192
193    MediaBuffer *mVideoBuffer;
194
195    sp<NuHTTPDataSource> mConnectingDataSource;
196    sp<NuCachedSource2> mCachedSource;
197
198    sp<ALooper> mLooper;
199    sp<ARTSPController> mRTSPController;
200    sp<ARTPSession> mRTPSession;
201    sp<UDPPusher> mRTPPusher, mRTCPPusher;
202
203    sp<LiveSession> mLiveSession;
204
205    DrmManagerClient *mDrmManagerClient;
206    DecryptHandle *mDecryptHandle;
207
208    status_t setDataSource_l(
209            const char *uri,
210            const KeyedVector<String8, String8> *headers = NULL);
211
212    status_t setDataSource_l(const sp<DataSource> &dataSource);
213    status_t setDataSource_l(const sp<MediaExtractor> &extractor);
214    void reset_l();
215    void partial_reset_l();
216    status_t seekTo_l(int64_t timeUs);
217    status_t pause_l(bool at_eos = false);
218    void initRenderer_l();
219    void notifyVideoSize_l();
220    void seekAudioIfNecessary_l();
221
222    void cancelPlayerEvents(bool keepBufferingGoing = false);
223
224    void setAudioSource(sp<MediaSource> source);
225    status_t initAudioDecoder();
226
227    void setVideoSource(sp<MediaSource> source);
228    status_t initVideoDecoder(uint32_t flags = 0);
229
230    void onStreamDone();
231
232    void notifyListener_l(int msg, int ext1 = 0, int ext2 = 0);
233
234    void onVideoEvent();
235    void onBufferingUpdate();
236    void onCheckAudioStatus();
237    void onPrepareAsyncEvent();
238    void abortPrepare(status_t err);
239    void finishAsyncPrepare_l();
240    void onVideoLagUpdate();
241
242    bool getCachedDuration_l(int64_t *durationUs, bool *eos);
243
244    status_t finishSetDataSource_l();
245
246    static bool ContinuePreparation(void *cookie);
247
248    static void OnRTSPSeekDoneWrapper(void *cookie);
249    void onRTSPSeekDone();
250
251    bool getBitrate(int64_t *bitrate);
252
253    void finishSeekIfNecessary(int64_t videoTimeUs);
254    void ensureCacheIsFetching_l();
255
256    AwesomePlayer(const AwesomePlayer &);
257    AwesomePlayer &operator=(const AwesomePlayer &);
258};
259
260}  // namespace android
261
262#endif  // AWESOME_PLAYER_H_
263
264