AwesomePlayer.h revision 0a5ca668c6f7d45706e9aec4a1dfec0aacc6d233
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;
42struct ARTPSession;
43struct UDPPusher;
44
45class DrmManagerClinet;
46class DecryptHandle;
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    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    status_t getVideoDimensions(int32_t *width, int32_t *height) const;
92
93    status_t suspend();
94    status_t resume();
95
96    // This is a mask of MediaExtractor::Flags.
97    uint32_t flags() const;
98
99    void postAudioEOS();
100    void postAudioSeekComplete();
101
102private:
103    friend struct AwesomeEvent;
104
105    enum {
106        PLAYING             = 1,
107        LOOPING             = 2,
108        FIRST_FRAME         = 4,
109        PREPARING           = 8,
110        PREPARED            = 16,
111        AT_EOS              = 32,
112        PREPARE_CANCELLED   = 64,
113        CACHE_UNDERRUN      = 128,
114        AUDIO_AT_EOS        = 256,
115        VIDEO_AT_EOS        = 512,
116        AUTO_LOOPING        = 1024,
117
118        // We are basically done preparing but are currently buffering
119        // sufficient data to begin playback and finish the preparation phase
120        // for good.
121        PREPARING_CONNECTED = 2048,
122    };
123
124    mutable Mutex mLock;
125    Mutex mMiscStateLock;
126
127    OMXClient mClient;
128    TimedEventQueue mQueue;
129    bool mQueueStarted;
130    wp<MediaPlayerBase> mListener;
131
132    sp<Surface> mSurface;
133    sp<MediaPlayerBase::AudioSink> mAudioSink;
134
135    SystemTimeSource mSystemTimeSource;
136    TimeSource *mTimeSource;
137
138    String8 mUri;
139    KeyedVector<String8, String8> mUriHeaders;
140
141    sp<DataSource> mFileSource;
142
143    sp<MediaSource> mVideoTrack;
144    sp<MediaSource> mVideoSource;
145    sp<AwesomeRenderer> mVideoRenderer;
146    bool mVideoRendererIsPreview;
147
148    sp<MediaSource> mAudioTrack;
149    sp<MediaSource> mAudioSource;
150    AudioPlayer *mAudioPlayer;
151    int64_t mDurationUs;
152
153    uint32_t mFlags;
154    uint32_t mExtractorFlags;
155
156    int32_t mVideoWidth, mVideoHeight;
157    int64_t mTimeSourceDeltaUs;
158    int64_t mVideoTimeUs;
159
160    bool mSeeking;
161    bool mSeekNotificationSent;
162    int64_t mSeekTimeUs;
163
164    int64_t mBitrate;  // total bitrate of the file (in bps) or -1 if unknown.
165
166    bool mWatchForAudioSeekComplete;
167    bool mWatchForAudioEOS;
168
169    sp<TimedEventQueue::Event> mVideoEvent;
170    bool mVideoEventPending;
171    sp<TimedEventQueue::Event> mStreamDoneEvent;
172    bool mStreamDoneEventPending;
173    sp<TimedEventQueue::Event> mBufferingEvent;
174    bool mBufferingEventPending;
175    sp<TimedEventQueue::Event> mCheckAudioStatusEvent;
176    bool mAudioStatusEventPending;
177
178    sp<TimedEventQueue::Event> mAsyncPrepareEvent;
179    Condition mPreparedCondition;
180    bool mIsAsyncPrepare;
181    status_t mPrepareResult;
182    status_t mStreamDoneStatus;
183
184    void postVideoEvent_l(int64_t delayUs = -1);
185    void postBufferingEvent_l();
186    void postStreamDoneEvent_l(status_t status);
187    void postCheckAudioStatusEvent_l();
188    status_t play_l();
189
190    MediaBuffer *mLastVideoBuffer;
191    MediaBuffer *mVideoBuffer;
192
193    sp<NuHTTPDataSource> mConnectingDataSource;
194    sp<NuCachedSource2> mCachedSource;
195
196    sp<ALooper> mLooper;
197    sp<ARTSPController> mRTSPController;
198    sp<ARTPSession> mRTPSession;
199    sp<UDPPusher> mRTPPusher, mRTCPPusher;
200
201    struct SuspensionState {
202        String8 mUri;
203        KeyedVector<String8, String8> mUriHeaders;
204        sp<DataSource> mFileSource;
205
206        uint32_t mFlags;
207        int64_t mPositionUs;
208
209        void *mLastVideoFrame;
210        size_t mLastVideoFrameSize;
211        int32_t mColorFormat;
212        int32_t mVideoWidth, mVideoHeight;
213        int32_t mDecodedWidth, mDecodedHeight;
214
215        SuspensionState()
216            : mLastVideoFrame(NULL) {
217        }
218
219        ~SuspensionState() {
220            if (mLastVideoFrame) {
221                free(mLastVideoFrame);
222                mLastVideoFrame = NULL;
223            }
224        }
225    } *mSuspensionState;
226
227    DrmManagerClient *mDrmManagerClient;
228    DecryptHandle *mDecryptHandle;
229
230    status_t setDataSource_l(
231            const char *uri,
232            const KeyedVector<String8, String8> *headers = NULL);
233
234    status_t setDataSource_l(const sp<DataSource> &dataSource);
235    status_t setDataSource_l(const sp<MediaExtractor> &extractor);
236    void reset_l();
237    void partial_reset_l();
238    status_t seekTo_l(int64_t timeUs);
239    status_t pause_l(bool at_eos = false);
240    void initRenderer_l();
241    void notifyVideoSize_l();
242    void seekAudioIfNecessary_l();
243
244    void cancelPlayerEvents(bool keepBufferingGoing = false);
245
246    void setAudioSource(sp<MediaSource> source);
247    status_t initAudioDecoder();
248
249    void setVideoSource(sp<MediaSource> source);
250    status_t initVideoDecoder(uint32_t flags = 0);
251
252    void onStreamDone();
253
254    void notifyListener_l(int msg, int ext1 = 0, int ext2 = 0);
255
256    void onVideoEvent();
257    void onBufferingUpdate();
258    void onCheckAudioStatus();
259    void onPrepareAsyncEvent();
260    void abortPrepare(status_t err);
261    void finishAsyncPrepare_l();
262
263    bool getCachedDuration_l(int64_t *durationUs, bool *eos);
264
265    status_t finishSetDataSource_l();
266
267    static bool ContinuePreparation(void *cookie);
268
269    static void OnRTSPSeekDoneWrapper(void *cookie);
270    void onRTSPSeekDone();
271
272    bool getBitrate(int64_t *bitrate);
273
274    void finishSeekIfNecessary(int64_t videoTimeUs);
275    void ensureCacheIsFetching_l();
276
277    AwesomePlayer(const AwesomePlayer &);
278    AwesomePlayer &operator=(const AwesomePlayer &);
279};
280
281}  // namespace android
282
283#endif  // AWESOME_PLAYER_H_
284
285