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