AwesomePlayer.h revision 7fbdb0903dfbf70b314a74e64e28fb880cdb9247
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
30namespace android {
31
32struct AudioPlayer;
33struct DataSource;
34struct MediaBuffer;
35struct MediaExtractor;
36struct MediaSource;
37struct NuCachedSource2;
38
39struct ALooper;
40struct ARTSPController;
41struct ARTPSession;
42struct UDPPusher;
43
44struct AwesomeRenderer : public RefBase {
45    AwesomeRenderer() {}
46
47    virtual void render(MediaBuffer *buffer) = 0;
48
49private:
50    AwesomeRenderer(const AwesomeRenderer &);
51    AwesomeRenderer &operator=(const AwesomeRenderer &);
52};
53
54struct AwesomePlayer {
55    AwesomePlayer();
56    ~AwesomePlayer();
57
58    void setListener(const wp<MediaPlayerBase> &listener);
59
60    status_t setDataSource(
61            const char *uri,
62            const KeyedVector<String8, String8> *headers = NULL);
63
64    status_t setDataSource(int fd, int64_t offset, int64_t length);
65
66    void reset();
67
68    status_t prepare();
69    status_t prepare_l();
70    status_t prepareAsync();
71    status_t prepareAsync_l();
72
73    status_t play();
74    status_t pause();
75
76    bool isPlaying() const;
77
78    void setISurface(const sp<ISurface> &isurface);
79    void setAudioSink(const sp<MediaPlayerBase::AudioSink> &audioSink);
80    status_t setLooping(bool shouldLoop);
81
82    status_t getDuration(int64_t *durationUs);
83    status_t getPosition(int64_t *positionUs);
84
85    status_t seekTo(int64_t timeUs);
86
87    status_t getVideoDimensions(int32_t *width, int32_t *height) const;
88
89    status_t suspend();
90    status_t resume();
91
92    // This is a mask of MediaExtractor::Flags.
93    uint32_t flags() const;
94
95    void postAudioEOS();
96    void postAudioSeekComplete();
97
98private:
99    friend struct AwesomeEvent;
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
115    mutable Mutex mLock;
116    Mutex mMiscStateLock;
117
118    OMXClient mClient;
119    TimedEventQueue mQueue;
120    bool mQueueStarted;
121    wp<MediaPlayerBase> mListener;
122
123    sp<ISurface> mISurface;
124    sp<MediaPlayerBase::AudioSink> mAudioSink;
125
126    SystemTimeSource mSystemTimeSource;
127    TimeSource *mTimeSource;
128
129    String8 mUri;
130    KeyedVector<String8, String8> mUriHeaders;
131
132    sp<DataSource> mFileSource;
133
134    sp<MediaSource> mVideoTrack;
135    sp<MediaSource> mVideoSource;
136    sp<AwesomeRenderer> mVideoRenderer;
137    bool mVideoRendererIsPreview;
138
139    sp<MediaSource> mAudioTrack;
140    sp<MediaSource> mAudioSource;
141    AudioPlayer *mAudioPlayer;
142    int64_t mDurationUs;
143
144    uint32_t mFlags;
145    uint32_t mExtractorFlags;
146
147    int32_t mVideoWidth, mVideoHeight;
148    int64_t mTimeSourceDeltaUs;
149    int64_t mVideoTimeUs;
150
151    bool mSeeking;
152    bool mSeekNotificationSent;
153    int64_t mSeekTimeUs;
154
155    int64_t mBitrate;  // total bitrate of the file (in bps) or -1 if unknown.
156
157    bool mWatchForAudioSeekComplete;
158    bool mWatchForAudioEOS;
159
160    sp<TimedEventQueue::Event> mVideoEvent;
161    bool mVideoEventPending;
162    sp<TimedEventQueue::Event> mStreamDoneEvent;
163    bool mStreamDoneEventPending;
164    sp<TimedEventQueue::Event> mBufferingEvent;
165    bool mBufferingEventPending;
166    sp<TimedEventQueue::Event> mCheckAudioStatusEvent;
167    bool mAudioStatusEventPending;
168
169    sp<TimedEventQueue::Event> mAsyncPrepareEvent;
170    Condition mPreparedCondition;
171    bool mIsAsyncPrepare;
172    status_t mPrepareResult;
173    status_t mStreamDoneStatus;
174
175    void postVideoEvent_l(int64_t delayUs = -1);
176    void postBufferingEvent_l();
177    void postStreamDoneEvent_l(status_t status);
178    void postCheckAudioStatusEvent_l();
179    status_t play_l();
180
181    MediaBuffer *mLastVideoBuffer;
182    MediaBuffer *mVideoBuffer;
183
184    sp<NuHTTPDataSource> mConnectingDataSource;
185    sp<NuCachedSource2> mCachedSource;
186
187    sp<ALooper> mLooper;
188    sp<ARTSPController> mRTSPController;
189    sp<ARTPSession> mRTPSession;
190    sp<UDPPusher> mRTPPusher, mRTCPPusher;
191
192    struct SuspensionState {
193        String8 mUri;
194        KeyedVector<String8, String8> mUriHeaders;
195        sp<DataSource> mFileSource;
196
197        uint32_t mFlags;
198        int64_t mPositionUs;
199
200        void *mLastVideoFrame;
201        size_t mLastVideoFrameSize;
202        int32_t mColorFormat;
203        int32_t mVideoWidth, mVideoHeight;
204        int32_t mDecodedWidth, mDecodedHeight;
205
206        SuspensionState()
207            : mLastVideoFrame(NULL) {
208        }
209
210        ~SuspensionState() {
211            if (mLastVideoFrame) {
212                free(mLastVideoFrame);
213                mLastVideoFrame = NULL;
214            }
215        }
216    } *mSuspensionState;
217
218    status_t setDataSource_l(
219            const char *uri,
220            const KeyedVector<String8, String8> *headers = NULL);
221
222    status_t setDataSource_l(const sp<DataSource> &dataSource);
223    status_t setDataSource_l(const sp<MediaExtractor> &extractor);
224    void reset_l();
225    void partial_reset_l();
226    status_t seekTo_l(int64_t timeUs);
227    status_t pause_l(bool at_eos = false);
228    void initRenderer_l();
229    void seekAudioIfNecessary_l();
230
231    void cancelPlayerEvents(bool keepBufferingGoing = false);
232
233    void setAudioSource(sp<MediaSource> source);
234    status_t initAudioDecoder();
235
236    void setVideoSource(sp<MediaSource> source);
237    status_t initVideoDecoder(uint32_t flags = 0);
238
239    void onStreamDone();
240
241    void notifyListener_l(int msg, int ext1 = 0, int ext2 = 0);
242
243    void onVideoEvent();
244    void onBufferingUpdate();
245    void onCheckAudioStatus();
246    void onPrepareAsyncEvent();
247    void abortPrepare(status_t err);
248    void finishAsyncPrepare_l();
249
250    bool getCachedDuration_l(int64_t *durationUs, bool *eos);
251
252    status_t finishSetDataSource_l();
253
254    static bool ContinuePreparation(void *cookie);
255
256    static void OnRTSPSeekDoneWrapper(void *cookie);
257    void onRTSPSeekDone();
258
259    bool getBitrate(int64_t *bitrate);
260
261    AwesomePlayer(const AwesomePlayer &);
262    AwesomePlayer &operator=(const AwesomePlayer &);
263};
264
265}  // namespace android
266
267#endif  // AWESOME_PLAYER_H_
268
269