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