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