AwesomePlayer.h revision f1958f9442bc937e1f8c8d9175901500b944b021
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 LiveSession;
47
48struct AwesomeRenderer : public RefBase {
49    AwesomeRenderer() {}
50
51    virtual void render(MediaBuffer *buffer) = 0;
52
53private:
54    AwesomeRenderer(const AwesomeRenderer &);
55    AwesomeRenderer &operator=(const AwesomeRenderer &);
56};
57
58struct AwesomePlayer {
59    AwesomePlayer();
60    ~AwesomePlayer();
61
62    void setListener(const wp<MediaPlayerBase> &listener);
63
64    status_t setDataSource(
65            const char *uri,
66            const KeyedVector<String8, String8> *headers = NULL);
67
68    status_t setDataSource(int fd, int64_t offset, int64_t length);
69
70    status_t setDataSource(const sp<IStreamSource> &source);
71
72    void reset();
73
74    status_t prepare();
75    status_t prepare_l();
76    status_t prepareAsync();
77    status_t prepareAsync_l();
78
79    status_t play();
80    status_t pause();
81
82    bool isPlaying() const;
83
84    void setSurface(const sp<Surface> &surface);
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
129    mutable Mutex mLock;
130    Mutex mMiscStateLock;
131
132    OMXClient mClient;
133    TimedEventQueue mQueue;
134    bool mQueueStarted;
135    wp<MediaPlayerBase> mListener;
136
137    sp<Surface> mSurface;
138    sp<MediaPlayerBase::AudioSink> mAudioSink;
139
140    SystemTimeSource mSystemTimeSource;
141    TimeSource *mTimeSource;
142
143    String8 mUri;
144    KeyedVector<String8, String8> mUriHeaders;
145
146    sp<DataSource> mFileSource;
147
148    sp<MediaSource> mVideoTrack;
149    sp<MediaSource> mVideoSource;
150    sp<AwesomeRenderer> mVideoRenderer;
151    bool mVideoRendererIsPreview;
152
153    sp<MediaSource> mAudioTrack;
154    sp<MediaSource> mAudioSource;
155    AudioPlayer *mAudioPlayer;
156    int64_t mDurationUs;
157
158    int32_t mDisplayWidth;
159    int32_t mDisplayHeight;
160
161    uint32_t mFlags;
162    uint32_t mExtractorFlags;
163    uint32_t mSinceLastDropped;
164
165    int64_t mTimeSourceDeltaUs;
166    int64_t mVideoTimeUs;
167
168    bool mSeeking;
169    bool mSeekNotificationSent;
170    int64_t mSeekTimeUs;
171
172    int64_t mBitrate;  // total bitrate of the file (in bps) or -1 if unknown.
173
174    bool mWatchForAudioSeekComplete;
175    bool mWatchForAudioEOS;
176
177    sp<TimedEventQueue::Event> mVideoEvent;
178    bool mVideoEventPending;
179    sp<TimedEventQueue::Event> mStreamDoneEvent;
180    bool mStreamDoneEventPending;
181    sp<TimedEventQueue::Event> mBufferingEvent;
182    bool mBufferingEventPending;
183    sp<TimedEventQueue::Event> mCheckAudioStatusEvent;
184    bool mAudioStatusEventPending;
185    sp<TimedEventQueue::Event> mVideoLagEvent;
186    bool mVideoLagEventPending;
187
188    sp<TimedEventQueue::Event> mAsyncPrepareEvent;
189    Condition mPreparedCondition;
190    bool mIsAsyncPrepare;
191    status_t mPrepareResult;
192    status_t mStreamDoneStatus;
193
194    void postVideoEvent_l(int64_t delayUs = -1);
195    void postBufferingEvent_l();
196    void postStreamDoneEvent_l(status_t status);
197    void postCheckAudioStatusEvent_l();
198    void postVideoLagEvent_l();
199    status_t play_l();
200
201    MediaBuffer *mVideoBuffer;
202
203    sp<NuHTTPDataSource> mConnectingDataSource;
204    sp<NuCachedSource2> mCachedSource;
205
206    sp<ALooper> mLooper;
207    sp<ARTSPController> mRTSPController;
208    sp<ARTSPController> mConnectingRTSPController;
209
210    sp<LiveSession> mLiveSession;
211
212    DrmManagerClient *mDrmManagerClient;
213    DecryptHandle *mDecryptHandle;
214
215    status_t setDataSource_l(
216            const char *uri,
217            const KeyedVector<String8, String8> *headers = NULL);
218
219    status_t setDataSource_l(const sp<DataSource> &dataSource);
220    status_t setDataSource_l(const sp<MediaExtractor> &extractor);
221    void reset_l();
222    void partial_reset_l();
223    status_t seekTo_l(int64_t timeUs);
224    status_t pause_l(bool at_eos = false);
225    void initRenderer_l();
226    void notifyVideoSize_l();
227    void seekAudioIfNecessary_l();
228
229    void cancelPlayerEvents(bool keepBufferingGoing = false);
230
231    void setAudioSource(sp<MediaSource> source);
232    status_t initAudioDecoder();
233
234    void setVideoSource(sp<MediaSource> source);
235    status_t initVideoDecoder(uint32_t flags = 0);
236
237    void onStreamDone();
238
239    void notifyListener_l(int msg, int ext1 = 0, int ext2 = 0);
240
241    void onVideoEvent();
242    void onBufferingUpdate();
243    void onCheckAudioStatus();
244    void onPrepareAsyncEvent();
245    void abortPrepare(status_t err);
246    void finishAsyncPrepare_l();
247    void onVideoLagUpdate();
248
249    bool getCachedDuration_l(int64_t *durationUs, bool *eos);
250
251    status_t finishSetDataSource_l();
252
253    static bool ContinuePreparation(void *cookie);
254
255    static void OnRTSPSeekDoneWrapper(void *cookie);
256    void onRTSPSeekDone();
257
258    bool getBitrate(int64_t *bitrate);
259
260    void finishSeekIfNecessary(int64_t videoTimeUs);
261    void ensureCacheIsFetching_l();
262
263    status_t startAudioPlayer_l();
264
265    AwesomePlayer(const AwesomePlayer &);
266    AwesomePlayer &operator=(const AwesomePlayer &);
267};
268
269}  // namespace android
270
271#endif  // AWESOME_PLAYER_H_
272
273