AwesomePlayer.h revision ed54ad0f8619ae416b0968ade6248894cbfc4dba
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    bool mWatchForAudioSeekComplete;
156    bool mWatchForAudioEOS;
157
158    sp<TimedEventQueue::Event> mVideoEvent;
159    bool mVideoEventPending;
160    sp<TimedEventQueue::Event> mStreamDoneEvent;
161    bool mStreamDoneEventPending;
162    sp<TimedEventQueue::Event> mBufferingEvent;
163    bool mBufferingEventPending;
164    sp<TimedEventQueue::Event> mCheckAudioStatusEvent;
165    bool mAudioStatusEventPending;
166
167    sp<TimedEventQueue::Event> mAsyncPrepareEvent;
168    Condition mPreparedCondition;
169    bool mIsAsyncPrepare;
170    status_t mPrepareResult;
171    status_t mStreamDoneStatus;
172
173    void postVideoEvent_l(int64_t delayUs = -1);
174    void postBufferingEvent_l();
175    void postStreamDoneEvent_l(status_t status);
176    void postCheckAudioStatusEvent_l();
177    status_t play_l();
178
179    MediaBuffer *mLastVideoBuffer;
180    MediaBuffer *mVideoBuffer;
181
182    sp<NuHTTPDataSource> mConnectingDataSource;
183    sp<NuCachedSource2> mCachedSource;
184
185    sp<ALooper> mLooper;
186    sp<ARTSPController> mRTSPController;
187    sp<ARTPSession> mRTPSession;
188    sp<UDPPusher> mRTPPusher, mRTCPPusher;
189
190    struct SuspensionState {
191        String8 mUri;
192        KeyedVector<String8, String8> mUriHeaders;
193        sp<DataSource> mFileSource;
194
195        uint32_t mFlags;
196        int64_t mPositionUs;
197
198        void *mLastVideoFrame;
199        size_t mLastVideoFrameSize;
200        int32_t mColorFormat;
201        int32_t mVideoWidth, mVideoHeight;
202        int32_t mDecodedWidth, mDecodedHeight;
203
204        SuspensionState()
205            : mLastVideoFrame(NULL) {
206        }
207
208        ~SuspensionState() {
209            if (mLastVideoFrame) {
210                free(mLastVideoFrame);
211                mLastVideoFrame = NULL;
212            }
213        }
214    } *mSuspensionState;
215
216    status_t setDataSource_l(
217            const char *uri,
218            const KeyedVector<String8, String8> *headers = NULL);
219
220    status_t setDataSource_l(const sp<DataSource> &dataSource);
221    status_t setDataSource_l(const sp<MediaExtractor> &extractor);
222    void reset_l();
223    status_t seekTo_l(int64_t timeUs);
224    status_t pause_l();
225    void initRenderer_l();
226    void seekAudioIfNecessary_l();
227
228    void cancelPlayerEvents(bool keepBufferingGoing = false);
229
230    void setAudioSource(sp<MediaSource> source);
231    status_t initAudioDecoder();
232
233    void setVideoSource(sp<MediaSource> source);
234    status_t initVideoDecoder();
235
236    void onStreamDone();
237
238    void notifyListener_l(int msg, int ext1 = 0, int ext2 = 0);
239
240    void onVideoEvent();
241    void onBufferingUpdate();
242    void onCheckAudioStatus();
243    void onPrepareAsyncEvent();
244    void abortPrepare(status_t err);
245    void finishAsyncPrepare_l();
246
247    bool getCachedDuration_l(int64_t *durationUs, bool *eos);
248
249    status_t finishSetDataSource_l();
250
251    static bool ContinuePreparation(void *cookie);
252
253    AwesomePlayer(const AwesomePlayer &);
254    AwesomePlayer &operator=(const AwesomePlayer &);
255};
256
257}  // namespace android
258
259#endif  // AWESOME_PLAYER_H_
260
261