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