2 * Copyright (C) 2009 The Android Open Source Project
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 #ifndef AWESOME_PLAYER_H_
19 #define AWESOME_PLAYER_H_
21 #include "NuHTTPDataSource.h"
22 #include "TimedEventQueue.h"
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>
35 struct MediaExtractor;
37 struct NuCachedSource2;
40 struct ARTSPController;
44 struct AwesomeRenderer : public RefBase {
47 virtual status_t initCheck() const = 0;
48 virtual void render(MediaBuffer *buffer) = 0;
51 AwesomeRenderer(const AwesomeRenderer &);
52 AwesomeRenderer &operator=(const AwesomeRenderer &);
55 struct AwesomePlayer {
59 void setListener(const wp<MediaPlayerBase> &listener);
61 status_t setDataSource(
63 const KeyedVector<String8, String8> *headers = NULL);
65 status_t setDataSource(int fd, int64_t offset, int64_t length);
71 status_t prepareAsync();
72 status_t prepareAsync_l();
77 bool isPlaying() const;
79 void setISurface(const sp<ISurface> &isurface);
80 void setAudioSink(const sp<MediaPlayerBase::AudioSink> &audioSink);
81 status_t setLooping(bool shouldLoop);
83 status_t getDuration(int64_t *durationUs);
84 status_t getPosition(int64_t *positionUs);
86 status_t seekTo(int64_t timeUs);
88 status_t getVideoDimensions(int32_t *width, int32_t *height) const;
93 // This is a mask of MediaExtractor::Flags.
94 uint32_t flags() const;
97 void postAudioSeekComplete();
100 friend struct AwesomeEvent;
109 PREPARE_CANCELLED = 64,
110 CACHE_UNDERRUN = 128,
117 Mutex mMiscStateLock;
120 TimedEventQueue mQueue;
122 wp<MediaPlayerBase> mListener;
124 sp<ISurface> mISurface;
125 sp<MediaPlayerBase::AudioSink> mAudioSink;
127 SystemTimeSource mSystemTimeSource;
128 TimeSource *mTimeSource;
131 KeyedVector<String8, String8> mUriHeaders;
133 sp<DataSource> mFileSource;
135 sp<MediaSource> mVideoTrack;
136 sp<MediaSource> mVideoSource;
137 sp<AwesomeRenderer> mVideoRenderer;
138 bool mVideoRendererIsPreview;
140 sp<MediaSource> mAudioTrack;
141 sp<MediaSource> mAudioSource;
142 AudioPlayer *mAudioPlayer;
146 uint32_t mExtractorFlags;
148 int32_t mVideoWidth, mVideoHeight;
149 int64_t mTimeSourceDeltaUs;
150 int64_t mVideoTimeUs;
153 bool mSeekNotificationSent;
156 int64_t mBitrate; // total bitrate of the file (in bps) or -1 if unknown.
158 bool mWatchForAudioSeekComplete;
159 bool mWatchForAudioEOS;
161 sp<TimedEventQueue::Event> mVideoEvent;
162 bool mVideoEventPending;
163 sp<TimedEventQueue::Event> mStreamDoneEvent;
164 bool mStreamDoneEventPending;
165 sp<TimedEventQueue::Event> mBufferingEvent;
166 bool mBufferingEventPending;
167 sp<TimedEventQueue::Event> mCheckAudioStatusEvent;
168 bool mAudioStatusEventPending;
170 sp<TimedEventQueue::Event> mAsyncPrepareEvent;
171 Condition mPreparedCondition;
172 bool mIsAsyncPrepare;
173 status_t mPrepareResult;
174 status_t mStreamDoneStatus;
176 void postVideoEvent_l(int64_t delayUs = -1);
177 void postBufferingEvent_l();
178 void postStreamDoneEvent_l(status_t status);
179 void postCheckAudioStatusEvent_l();
182 MediaBuffer *mLastVideoBuffer;
183 MediaBuffer *mVideoBuffer;
185 sp<NuHTTPDataSource> mConnectingDataSource;
186 sp<NuCachedSource2> mCachedSource;
189 sp<ARTSPController> mRTSPController;
190 sp<ARTPSession> mRTPSession;
191 sp<UDPPusher> mRTPPusher, mRTCPPusher;
193 struct SuspensionState {
195 KeyedVector<String8, String8> mUriHeaders;
196 sp<DataSource> mFileSource;
201 void *mLastVideoFrame;
202 size_t mLastVideoFrameSize;
203 int32_t mColorFormat;
204 int32_t mVideoWidth, mVideoHeight;
205 int32_t mDecodedWidth, mDecodedHeight;
208 : mLastVideoFrame(NULL) {
212 if (mLastVideoFrame) {
213 free(mLastVideoFrame);
214 mLastVideoFrame = NULL;
219 status_t setDataSource_l(
221 const KeyedVector<String8, String8> *headers = NULL);
223 status_t setDataSource_l(const sp<DataSource> &dataSource);
224 status_t setDataSource_l(const sp<MediaExtractor> &extractor);
226 void partial_reset_l();
227 status_t seekTo_l(int64_t timeUs);
228 status_t pause_l(bool at_eos = false);
229 status_t initRenderer_l();
230 void seekAudioIfNecessary_l();
232 void cancelPlayerEvents(bool keepBufferingGoing = false);
234 void setAudioSource(sp<MediaSource> source);
235 status_t initAudioDecoder();
237 void setVideoSource(sp<MediaSource> source);
238 status_t initVideoDecoder(uint32_t flags = 0);
242 void notifyListener_l(int msg, int ext1 = 0, int ext2 = 0);
245 void onBufferingUpdate();
246 void onCheckAudioStatus();
247 void onPrepareAsyncEvent();
248 void abortPrepare(status_t err);
249 void finishAsyncPrepare_l();
251 bool getCachedDuration_l(int64_t *durationUs, bool *eos);
253 status_t finishSetDataSource_l();
255 static bool ContinuePreparation(void *cookie);
257 static void OnRTSPSeekDoneWrapper(void *cookie);
258 void onRTSPSeekDone();
260 bool getBitrate(int64_t *bitrate);
262 void finishSeekIfNecessary(int64_t videoTimeUs);
264 AwesomePlayer(const AwesomePlayer &);
265 AwesomePlayer &operator=(const AwesomePlayer &);
268 } // namespace android
270 #endif // AWESOME_PLAYER_H_