AwesomePlayer.h revision 2eb16c2250c57a8009ab58e9aeb1606ab62bbbbc
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    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    status_t seekTo_l(int64_t timeUs);
226    status_t pause_l(bool at_eos = false);
227    void initRenderer_l();
228    void notifyVideoSize_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();
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    AwesomePlayer(const AwesomePlayer &);
257    AwesomePlayer &operator=(const AwesomePlayer &);
258};
259
260}  // namespace android
261
262#endif  // AWESOME_PLAYER_H_
263
264