AwesomePlayer.h revision 2bfdd428c56c7524d1a11979f200a1762866032d
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 "HTTPBase.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;
39struct ISurfaceTexture;
40
41class DrmManagerClinet;
42class DecryptHandle;
43
44class TimedTextPlayer;
45struct WVMExtractor;
46
47struct AwesomeRenderer : public RefBase {
48    AwesomeRenderer() {}
49
50    virtual void render(MediaBuffer *buffer) = 0;
51
52private:
53    AwesomeRenderer(const AwesomeRenderer &);
54    AwesomeRenderer &operator=(const AwesomeRenderer &);
55};
56
57struct AwesomePlayer {
58    AwesomePlayer();
59    ~AwesomePlayer();
60
61    void setListener(const wp<MediaPlayerBase> &listener);
62    void setUID(uid_t uid);
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    status_t setSurface(const sp<Surface> &surface);
85    status_t setSurfaceTexture(const sp<ISurfaceTexture> &surfaceTexture);
86    void setAudioSink(const sp<MediaPlayerBase::AudioSink> &audioSink);
87    status_t setLooping(bool shouldLoop);
88
89    status_t getDuration(int64_t *durationUs);
90    status_t getPosition(int64_t *positionUs);
91
92    status_t setParameter(int key, const Parcel &request);
93    status_t getParameter(int key, Parcel *reply);
94    status_t setCacheStatCollectFreq(const Parcel &request);
95
96    status_t seekTo(int64_t timeUs);
97
98    // This is a mask of MediaExtractor::Flags.
99    uint32_t flags() const;
100
101    void postAudioEOS(int64_t delayUs = 0ll);
102    void postAudioSeekComplete();
103
104    status_t setTimedTextTrackIndex(int32_t index);
105
106    status_t dump(int fd, const Vector<String16> &args) const;
107
108private:
109    friend struct AwesomeEvent;
110    friend struct PreviewPlayer;
111
112    enum {
113        PLAYING             = 0x01,
114        LOOPING             = 0x02,
115        FIRST_FRAME         = 0x04,
116        PREPARING           = 0x08,
117        PREPARED            = 0x10,
118        AT_EOS              = 0x20,
119        PREPARE_CANCELLED   = 0x40,
120        CACHE_UNDERRUN      = 0x80,
121        AUDIO_AT_EOS        = 0x0100,
122        VIDEO_AT_EOS        = 0x0200,
123        AUTO_LOOPING        = 0x0400,
124
125        // We are basically done preparing but are currently buffering
126        // sufficient data to begin playback and finish the preparation phase
127        // for good.
128        PREPARING_CONNECTED = 0x0800,
129
130        // We're triggering a single video event to display the first frame
131        // after the seekpoint.
132        SEEK_PREVIEW        = 0x1000,
133
134        AUDIO_RUNNING       = 0x2000,
135        AUDIOPLAYER_STARTED = 0x4000,
136
137        INCOGNITO           = 0x8000,
138
139        TEXT_RUNNING        = 0x10000,
140        TEXTPLAYER_STARTED  = 0x20000,
141
142        SLOW_DECODER_HACK   = 0x40000,
143    };
144
145    mutable Mutex mLock;
146    Mutex mMiscStateLock;
147    mutable Mutex mStatsLock;
148    Mutex mAudioLock;
149
150    OMXClient mClient;
151    TimedEventQueue mQueue;
152    bool mQueueStarted;
153    wp<MediaPlayerBase> mListener;
154    bool mUIDValid;
155    uid_t mUID;
156
157    sp<Surface> mSurface;
158    sp<ANativeWindow> mNativeWindow;
159    sp<MediaPlayerBase::AudioSink> mAudioSink;
160
161    SystemTimeSource mSystemTimeSource;
162    TimeSource *mTimeSource;
163
164    String8 mUri;
165    KeyedVector<String8, String8> mUriHeaders;
166
167    sp<DataSource> mFileSource;
168
169    sp<MediaSource> mVideoTrack;
170    sp<MediaSource> mVideoSource;
171    sp<AwesomeRenderer> mVideoRenderer;
172    bool mVideoRendererIsPreview;
173
174    sp<MediaSource> mAudioTrack;
175    sp<MediaSource> mAudioSource;
176    AudioPlayer *mAudioPlayer;
177    int64_t mDurationUs;
178
179    int32_t mDisplayWidth;
180    int32_t mDisplayHeight;
181
182    uint32_t mFlags;
183    uint32_t mExtractorFlags;
184    uint32_t mSinceLastDropped;
185
186    int64_t mTimeSourceDeltaUs;
187    int64_t mVideoTimeUs;
188
189    enum SeekType {
190        NO_SEEK,
191        SEEK,
192        SEEK_VIDEO_ONLY
193    };
194    SeekType mSeeking;
195
196    bool mSeekNotificationSent;
197    int64_t mSeekTimeUs;
198
199    int64_t mBitrate;  // total bitrate of the file (in bps) or -1 if unknown.
200
201    bool mWatchForAudioSeekComplete;
202    bool mWatchForAudioEOS;
203
204    sp<TimedEventQueue::Event> mVideoEvent;
205    bool mVideoEventPending;
206    sp<TimedEventQueue::Event> mStreamDoneEvent;
207    bool mStreamDoneEventPending;
208    sp<TimedEventQueue::Event> mBufferingEvent;
209    bool mBufferingEventPending;
210    sp<TimedEventQueue::Event> mCheckAudioStatusEvent;
211    bool mAudioStatusEventPending;
212    sp<TimedEventQueue::Event> mVideoLagEvent;
213    bool mVideoLagEventPending;
214
215    sp<TimedEventQueue::Event> mAsyncPrepareEvent;
216    Condition mPreparedCondition;
217    bool mIsAsyncPrepare;
218    status_t mPrepareResult;
219    status_t mStreamDoneStatus;
220
221    void postVideoEvent_l(int64_t delayUs = -1);
222    void postBufferingEvent_l();
223    void postStreamDoneEvent_l(status_t status);
224    void postCheckAudioStatusEvent(int64_t delayUs);
225    void postVideoLagEvent_l();
226    status_t play_l();
227
228    MediaBuffer *mVideoBuffer;
229
230    sp<HTTPBase> mConnectingDataSource;
231    sp<NuCachedSource2> mCachedSource;
232
233    DrmManagerClient *mDrmManagerClient;
234    sp<DecryptHandle> mDecryptHandle;
235
236    int64_t mLastVideoTimeUs;
237    TimedTextPlayer *mTextPlayer;
238    mutable Mutex mTimedTextLock;
239
240    sp<WVMExtractor> mWVMExtractor;
241
242    status_t setDataSource_l(
243            const char *uri,
244            const KeyedVector<String8, String8> *headers = NULL);
245
246    status_t setDataSource_l(const sp<DataSource> &dataSource);
247    status_t setDataSource_l(const sp<MediaExtractor> &extractor);
248    void reset_l();
249    status_t seekTo_l(int64_t timeUs);
250    status_t pause_l(bool at_eos = false);
251    void initRenderer_l();
252    void notifyVideoSize_l();
253    void seekAudioIfNecessary_l();
254
255    void cancelPlayerEvents(bool keepBufferingGoing = false);
256
257    void setAudioSource(sp<MediaSource> source);
258    status_t initAudioDecoder();
259
260    void setVideoSource(sp<MediaSource> source);
261    status_t initVideoDecoder(uint32_t flags = 0);
262
263    void addTextSource(sp<MediaSource> source);
264
265    void onStreamDone();
266
267    void notifyListener_l(int msg, int ext1 = 0, int ext2 = 0);
268
269    void onVideoEvent();
270    void onBufferingUpdate();
271    void onCheckAudioStatus();
272    void onPrepareAsyncEvent();
273    void abortPrepare(status_t err);
274    void finishAsyncPrepare_l();
275    void onVideoLagUpdate();
276
277    bool getCachedDuration_l(int64_t *durationUs, bool *eos);
278
279    status_t finishSetDataSource_l();
280
281    static bool ContinuePreparation(void *cookie);
282
283    bool getBitrate(int64_t *bitrate);
284
285    void finishSeekIfNecessary(int64_t videoTimeUs);
286    void ensureCacheIsFetching_l();
287
288    status_t startAudioPlayer_l(bool sendErrorNotification = true);
289
290    void shutdownVideoDecoder_l();
291    status_t setNativeWindow_l(const sp<ANativeWindow> &native);
292
293    bool isStreamingHTTP() const;
294    void sendCacheStats();
295
296    enum FlagMode {
297        SET,
298        CLEAR,
299        ASSIGN
300    };
301    void modifyFlags(unsigned value, FlagMode mode);
302
303    struct TrackStat {
304        String8 mMIME;
305        String8 mDecoderName;
306    };
307
308    // protected by mStatsLock
309    struct Stats {
310        int mFd;
311        String8 mURI;
312        int64_t mBitrate;
313        ssize_t mAudioTrackIndex;
314        ssize_t mVideoTrackIndex;
315        int64_t mNumVideoFramesDecoded;
316        int64_t mNumVideoFramesDropped;
317        int32_t mVideoWidth;
318        int32_t mVideoHeight;
319        uint32_t mFlags;
320        Vector<TrackStat> mTracks;
321    } mStats;
322
323    AwesomePlayer(const AwesomePlayer &);
324    AwesomePlayer &operator=(const AwesomePlayer &);
325};
326
327}  // namespace android
328
329#endif  // AWESOME_PLAYER_H_
330
331