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