AwesomePlayer.h revision 5ac7b5def64625fdc9cfaf1bbdd013f5ada241f3
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    int64_t mRTSPTimeOffset;
186    sp<ARTPSession> mRTPSession;
187    sp<UDPPusher> mRTPPusher, mRTCPPusher;
188
189    struct SuspensionState {
190        String8 mUri;
191        KeyedVector<String8, String8> mUriHeaders;
192        sp<DataSource> mFileSource;
193
194        uint32_t mFlags;
195        int64_t mPositionUs;
196
197        void *mLastVideoFrame;
198        size_t mLastVideoFrameSize;
199        int32_t mColorFormat;
200        int32_t mVideoWidth, mVideoHeight;
201        int32_t mDecodedWidth, mDecodedHeight;
202
203        SuspensionState()
204            : mLastVideoFrame(NULL) {
205        }
206
207        ~SuspensionState() {
208            if (mLastVideoFrame) {
209                free(mLastVideoFrame);
210                mLastVideoFrame = NULL;
211            }
212        }
213    } *mSuspensionState;
214
215    status_t setDataSource_l(
216            const char *uri,
217            const KeyedVector<String8, String8> *headers = NULL);
218
219    status_t setDataSource_l(const sp<DataSource> &dataSource);
220    status_t setDataSource_l(const sp<MediaExtractor> &extractor);
221    void reset_l();
222    status_t seekTo_l(int64_t timeUs);
223    status_t pause_l();
224    void initRenderer_l();
225    void notifyVideoSize_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
246    status_t finishSetDataSource_l();
247
248    static bool ContinuePreparation(void *cookie);
249
250    AwesomePlayer(const AwesomePlayer &);
251    AwesomePlayer &operator=(const AwesomePlayer &);
252};
253
254}  // namespace android
255
256#endif  // AWESOME_PLAYER_H_
257
258