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