AwesomePlayer.h revision 7314fa17093d514199fedcb55ac41136a1b31cb3
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        INCOGNITO           = 32768,
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    DrmManagerClient *mDrmManagerClient;
211    DecryptHandle *mDecryptHandle;
212
213    status_t setDataSource_l(
214            const char *uri,
215            const KeyedVector<String8, String8> *headers = NULL);
216
217    status_t setDataSource_l(const sp<DataSource> &dataSource);
218    status_t setDataSource_l(const sp<MediaExtractor> &extractor);
219    void reset_l();
220    status_t seekTo_l(int64_t timeUs);
221    status_t pause_l(bool at_eos = false);
222    void initRenderer_l();
223    void notifyVideoSize_l();
224    void seekAudioIfNecessary_l();
225
226    void cancelPlayerEvents(bool keepBufferingGoing = false);
227
228    void setAudioSource(sp<MediaSource> source);
229    status_t initAudioDecoder();
230
231    void setVideoSource(sp<MediaSource> source);
232    status_t initVideoDecoder(uint32_t flags = 0);
233
234    void onStreamDone();
235
236    void notifyListener_l(int msg, int ext1 = 0, int ext2 = 0);
237
238    void onVideoEvent();
239    void onBufferingUpdate();
240    void onCheckAudioStatus();
241    void onPrepareAsyncEvent();
242    void abortPrepare(status_t err);
243    void finishAsyncPrepare_l();
244    void onVideoLagUpdate();
245
246    bool getCachedDuration_l(int64_t *durationUs, bool *eos);
247
248    status_t finishSetDataSource_l();
249
250    static bool ContinuePreparation(void *cookie);
251
252    static void OnRTSPSeekDoneWrapper(void *cookie);
253    void onRTSPSeekDone();
254
255    bool getBitrate(int64_t *bitrate);
256
257    void finishSeekIfNecessary(int64_t videoTimeUs);
258    void ensureCacheIsFetching_l();
259
260    status_t startAudioPlayer_l();
261
262    AwesomePlayer(const AwesomePlayer &);
263    AwesomePlayer &operator=(const AwesomePlayer &);
264};
265
266}  // namespace android
267
268#endif  // AWESOME_PLAYER_H_
269
270