blob: 39758669c7583e616dd9ab7221626f86cb86bb49 [file] [log] [blame]
Andreas Huber27366fc2009-11-20 09:32:46 -08001/*
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//#define LOG_NDEBUG 0
18#define LOG_TAG "AwesomePlayer"
19#include <utils/Log.h>
20
Andreas Huber4ab5a6f2010-02-11 11:00:26 -080021#include <dlfcn.h>
22
Andreas Huber7a747b82010-06-07 15:19:40 -070023#include "include/ARTSPController.h"
Andreas Huber27366fc2009-11-20 09:32:46 -080024#include "include/AwesomePlayer.h"
Andreas Huber7a747b82010-06-07 15:19:40 -070025#include "include/LiveSource.h"
Andreas Huber1314e732009-12-14 14:18:22 -080026#include "include/SoftwareRenderer.h"
Andreas Huber4d61f602010-06-10 11:17:50 -070027#include "include/NuCachedSource2.h"
28#include "include/ThrottledSource.h"
Andreas Huber27366fc2009-11-20 09:32:46 -080029
Andreas Huber57648e42010-08-04 10:14:30 -070030#include "ARTPSession.h"
31#include "APacketSource.h"
32#include "ASessionDescription.h"
33#include "UDPPusher.h"
34
Andreas Hubera67d5382009-12-10 15:32:12 -080035#include <binder/IPCThreadState.h>
Andreas Huber27366fc2009-11-20 09:32:46 -080036#include <media/stagefright/AudioPlayer.h>
37#include <media/stagefright/DataSource.h>
38#include <media/stagefright/FileSource.h>
39#include <media/stagefright/MediaBuffer.h>
Andreas Huberc79827a2010-01-05 10:54:55 -080040#include <media/stagefright/MediaDefs.h>
Andreas Huber27366fc2009-11-20 09:32:46 -080041#include <media/stagefright/MediaExtractor.h>
42#include <media/stagefright/MediaDebug.h>
43#include <media/stagefright/MediaSource.h>
44#include <media/stagefright/MetaData.h>
45#include <media/stagefright/OMXCodec.h>
Andreas Huberc79827a2010-01-05 10:54:55 -080046
Mathias Agopian000479f2010-02-09 17:46:37 -080047#include <surfaceflinger/ISurface.h>
48
Andreas Huber7a747b82010-06-07 15:19:40 -070049#include <media/stagefright/foundation/ALooper.h>
Andreas Huber202348e2010-06-07 14:35:29 -070050
Andreas Huber27366fc2009-11-20 09:32:46 -080051namespace android {
52
Andreas Huber87ab9cd2010-09-03 13:20:33 -070053static int64_t kLowWaterMarkUs = 2000000ll; // 2secs
54static int64_t kHighWaterMarkUs = 10000000ll; // 10secs
55
Andreas Huber27366fc2009-11-20 09:32:46 -080056struct AwesomeEvent : public TimedEventQueue::Event {
Andreas Huber6be780e2010-02-08 14:40:30 -080057 AwesomeEvent(
58 AwesomePlayer *player,
59 void (AwesomePlayer::*method)())
Andreas Huber27366fc2009-11-20 09:32:46 -080060 : mPlayer(player),
Andreas Huber6be780e2010-02-08 14:40:30 -080061 mMethod(method) {
Andreas Huber27366fc2009-11-20 09:32:46 -080062 }
63
64protected:
65 virtual ~AwesomeEvent() {}
66
67 virtual void fire(TimedEventQueue *queue, int64_t /* now_us */) {
Andreas Huber6be780e2010-02-08 14:40:30 -080068 (mPlayer->*mMethod)();
Andreas Huber27366fc2009-11-20 09:32:46 -080069 }
70
71private:
72 AwesomePlayer *mPlayer;
Andreas Huber6be780e2010-02-08 14:40:30 -080073 void (AwesomePlayer::*mMethod)();
Andreas Huber27366fc2009-11-20 09:32:46 -080074
75 AwesomeEvent(const AwesomeEvent &);
76 AwesomeEvent &operator=(const AwesomeEvent &);
77};
78
Andreas Huber1314e732009-12-14 14:18:22 -080079struct AwesomeRemoteRenderer : public AwesomeRenderer {
80 AwesomeRemoteRenderer(const sp<IOMXRenderer> &target)
81 : mTarget(target) {
82 }
83
84 virtual void render(MediaBuffer *buffer) {
85 void *id;
86 if (buffer->meta_data()->findPointer(kKeyBufferID, &id)) {
87 mTarget->render((IOMX::buffer_id)id);
88 }
89 }
90
91private:
92 sp<IOMXRenderer> mTarget;
93
94 AwesomeRemoteRenderer(const AwesomeRemoteRenderer &);
95 AwesomeRemoteRenderer &operator=(const AwesomeRemoteRenderer &);
96};
97
98struct AwesomeLocalRenderer : public AwesomeRenderer {
99 AwesomeLocalRenderer(
Andreas Huber7b73cfc2010-02-12 14:40:08 -0800100 bool previewOnly,
Andreas Huber4ab5a6f2010-02-11 11:00:26 -0800101 const char *componentName,
Andreas Huber1314e732009-12-14 14:18:22 -0800102 OMX_COLOR_FORMATTYPE colorFormat,
103 const sp<ISurface> &surface,
104 size_t displayWidth, size_t displayHeight,
105 size_t decodedWidth, size_t decodedHeight)
Andreas Huber4ab5a6f2010-02-11 11:00:26 -0800106 : mTarget(NULL),
107 mLibHandle(NULL) {
Andreas Huber7b73cfc2010-02-12 14:40:08 -0800108 init(previewOnly, componentName,
Andreas Huber4ab5a6f2010-02-11 11:00:26 -0800109 colorFormat, surface, displayWidth,
110 displayHeight, decodedWidth, decodedHeight);
Andreas Huber1314e732009-12-14 14:18:22 -0800111 }
112
113 virtual void render(MediaBuffer *buffer) {
Andreas Huber7b73cfc2010-02-12 14:40:08 -0800114 render((const uint8_t *)buffer->data() + buffer->range_offset(),
115 buffer->range_length());
116 }
117
118 void render(const void *data, size_t size) {
119 mTarget->render(data, size, NULL);
Andreas Huber1314e732009-12-14 14:18:22 -0800120 }
121
122protected:
123 virtual ~AwesomeLocalRenderer() {
124 delete mTarget;
125 mTarget = NULL;
Andreas Huber4ab5a6f2010-02-11 11:00:26 -0800126
127 if (mLibHandle) {
128 dlclose(mLibHandle);
129 mLibHandle = NULL;
130 }
Andreas Huber1314e732009-12-14 14:18:22 -0800131 }
132
133private:
Andreas Huber4ab5a6f2010-02-11 11:00:26 -0800134 VideoRenderer *mTarget;
135 void *mLibHandle;
136
137 void init(
Andreas Huber7b73cfc2010-02-12 14:40:08 -0800138 bool previewOnly,
Andreas Huber4ab5a6f2010-02-11 11:00:26 -0800139 const char *componentName,
140 OMX_COLOR_FORMATTYPE colorFormat,
141 const sp<ISurface> &surface,
142 size_t displayWidth, size_t displayHeight,
143 size_t decodedWidth, size_t decodedHeight);
Andreas Huber1314e732009-12-14 14:18:22 -0800144
145 AwesomeLocalRenderer(const AwesomeLocalRenderer &);
146 AwesomeLocalRenderer &operator=(const AwesomeLocalRenderer &);;
147};
148
Andreas Huber4ab5a6f2010-02-11 11:00:26 -0800149void AwesomeLocalRenderer::init(
Andreas Huber7b73cfc2010-02-12 14:40:08 -0800150 bool previewOnly,
Andreas Huber4ab5a6f2010-02-11 11:00:26 -0800151 const char *componentName,
152 OMX_COLOR_FORMATTYPE colorFormat,
153 const sp<ISurface> &surface,
154 size_t displayWidth, size_t displayHeight,
155 size_t decodedWidth, size_t decodedHeight) {
Andreas Huber7b73cfc2010-02-12 14:40:08 -0800156 if (!previewOnly) {
157 // We will stick to the vanilla software-color-converting renderer
158 // for "previewOnly" mode, to avoid unneccessarily switching overlays
159 // more often than necessary.
Andreas Huber4ab5a6f2010-02-11 11:00:26 -0800160
Andreas Huber7b73cfc2010-02-12 14:40:08 -0800161 mLibHandle = dlopen("libstagefrighthw.so", RTLD_NOW);
Andreas Huber4ab5a6f2010-02-11 11:00:26 -0800162
Andreas Huber7b73cfc2010-02-12 14:40:08 -0800163 if (mLibHandle) {
164 typedef VideoRenderer *(*CreateRendererFunc)(
165 const sp<ISurface> &surface,
166 const char *componentName,
167 OMX_COLOR_FORMATTYPE colorFormat,
168 size_t displayWidth, size_t displayHeight,
169 size_t decodedWidth, size_t decodedHeight);
Andreas Huber4ab5a6f2010-02-11 11:00:26 -0800170
Andreas Huber7b73cfc2010-02-12 14:40:08 -0800171 CreateRendererFunc func =
172 (CreateRendererFunc)dlsym(
173 mLibHandle,
174 "_Z14createRendererRKN7android2spINS_8ISurfaceEEEPKc20"
175 "OMX_COLOR_FORMATTYPEjjjj");
176
177 if (func) {
178 mTarget =
179 (*func)(surface, componentName, colorFormat,
180 displayWidth, displayHeight,
181 decodedWidth, decodedHeight);
182 }
Andreas Huber4ab5a6f2010-02-11 11:00:26 -0800183 }
184 }
185
186 if (mTarget == NULL) {
187 mTarget = new SoftwareRenderer(
188 colorFormat, surface, displayWidth, displayHeight,
189 decodedWidth, decodedHeight);
190 }
191}
192
Andreas Huber27366fc2009-11-20 09:32:46 -0800193AwesomePlayer::AwesomePlayer()
Andreas Huber406a18b2010-02-18 16:45:13 -0800194 : mQueueStarted(false),
195 mTimeSource(NULL),
Andreas Huber7b73cfc2010-02-12 14:40:08 -0800196 mVideoRendererIsPreview(false),
Andreas Huber27366fc2009-11-20 09:32:46 -0800197 mAudioPlayer(NULL),
Andreas Huberffdf4782010-02-09 14:05:43 -0800198 mFlags(0),
Andreas Huber62f7ffe2010-05-06 10:18:05 -0700199 mExtractorFlags(0),
Andreas Huber27366fc2009-11-20 09:32:46 -0800200 mLastVideoBuffer(NULL),
Andreas Huberba7ec912010-02-12 10:42:02 -0800201 mVideoBuffer(NULL),
202 mSuspensionState(NULL) {
Andreas Huber27366fc2009-11-20 09:32:46 -0800203 CHECK_EQ(mClient.connect(), OK);
204
205 DataSource::RegisterDefaultSniffers();
206
Andreas Huber6be780e2010-02-08 14:40:30 -0800207 mVideoEvent = new AwesomeEvent(this, &AwesomePlayer::onVideoEvent);
Andreas Huber27366fc2009-11-20 09:32:46 -0800208 mVideoEventPending = false;
Andreas Huber6be780e2010-02-08 14:40:30 -0800209 mStreamDoneEvent = new AwesomeEvent(this, &AwesomePlayer::onStreamDone);
Andreas Huber27366fc2009-11-20 09:32:46 -0800210 mStreamDoneEventPending = false;
Andreas Huber6be780e2010-02-08 14:40:30 -0800211 mBufferingEvent = new AwesomeEvent(this, &AwesomePlayer::onBufferingUpdate);
Andreas Huberb9e63832010-01-26 16:20:10 -0800212 mBufferingEventPending = false;
Andreas Huber6be780e2010-02-08 14:40:30 -0800213
214 mCheckAudioStatusEvent = new AwesomeEvent(
215 this, &AwesomePlayer::onCheckAudioStatus);
216
Andreas Huber70d10c02010-02-03 11:37:29 -0800217 mAudioStatusEventPending = false;
Andreas Huber27366fc2009-11-20 09:32:46 -0800218
Andreas Huber27366fc2009-11-20 09:32:46 -0800219 reset();
220}
221
222AwesomePlayer::~AwesomePlayer() {
Andreas Huber406a18b2010-02-18 16:45:13 -0800223 if (mQueueStarted) {
224 mQueue.stop();
225 }
Andreas Huber27366fc2009-11-20 09:32:46 -0800226
227 reset();
228
229 mClient.disconnect();
230}
231
Andreas Huberb9e63832010-01-26 16:20:10 -0800232void AwesomePlayer::cancelPlayerEvents(bool keepBufferingGoing) {
Andreas Huber27366fc2009-11-20 09:32:46 -0800233 mQueue.cancelEvent(mVideoEvent->eventID());
234 mVideoEventPending = false;
235 mQueue.cancelEvent(mStreamDoneEvent->eventID());
236 mStreamDoneEventPending = false;
Andreas Huber70d10c02010-02-03 11:37:29 -0800237 mQueue.cancelEvent(mCheckAudioStatusEvent->eventID());
238 mAudioStatusEventPending = false;
Andreas Huberb9e63832010-01-26 16:20:10 -0800239
240 if (!keepBufferingGoing) {
241 mQueue.cancelEvent(mBufferingEvent->eventID());
242 mBufferingEventPending = false;
243 }
Andreas Huber27366fc2009-11-20 09:32:46 -0800244}
245
Andreas Hubera3f43842010-01-21 10:28:45 -0800246void AwesomePlayer::setListener(const wp<MediaPlayerBase> &listener) {
Andreas Huber27366fc2009-11-20 09:32:46 -0800247 Mutex::Autolock autoLock(mLock);
248 mListener = listener;
249}
250
Andreas Huber433c9ac2010-01-27 16:49:05 -0800251status_t AwesomePlayer::setDataSource(
252 const char *uri, const KeyedVector<String8, String8> *headers) {
Andreas Huber27366fc2009-11-20 09:32:46 -0800253 Mutex::Autolock autoLock(mLock);
Andreas Huberba7ec912010-02-12 10:42:02 -0800254 return setDataSource_l(uri, headers);
255}
Andreas Huber27366fc2009-11-20 09:32:46 -0800256
Andreas Huberba7ec912010-02-12 10:42:02 -0800257status_t AwesomePlayer::setDataSource_l(
258 const char *uri, const KeyedVector<String8, String8> *headers) {
Andreas Huber27366fc2009-11-20 09:32:46 -0800259 reset_l();
260
Andreas Huberffdf4782010-02-09 14:05:43 -0800261 mUri = uri;
Andreas Huberb9e63832010-01-26 16:20:10 -0800262
Andreas Huberffdf4782010-02-09 14:05:43 -0800263 if (headers) {
264 mUriHeaders = *headers;
Andreas Huberb9e63832010-01-26 16:20:10 -0800265 }
266
Andreas Huberffdf4782010-02-09 14:05:43 -0800267 // The actual work will be done during preparation in the call to
268 // ::finishSetDataSource_l to avoid blocking the calling thread in
269 // setDataSource for any significant time.
Andreas Huber27366fc2009-11-20 09:32:46 -0800270
Andreas Huberffdf4782010-02-09 14:05:43 -0800271 return OK;
Andreas Huber27366fc2009-11-20 09:32:46 -0800272}
273
274status_t AwesomePlayer::setDataSource(
275 int fd, int64_t offset, int64_t length) {
276 Mutex::Autolock autoLock(mLock);
277
278 reset_l();
279
Andreas Huberba7ec912010-02-12 10:42:02 -0800280 sp<DataSource> dataSource = new FileSource(fd, offset, length);
Andreas Huber27366fc2009-11-20 09:32:46 -0800281
Andreas Huberba7ec912010-02-12 10:42:02 -0800282 status_t err = dataSource->initCheck();
Andreas Huber27366fc2009-11-20 09:32:46 -0800283
284 if (err != OK) {
285 return err;
286 }
287
Andreas Huberba7ec912010-02-12 10:42:02 -0800288 mFileSource = dataSource;
289
290 return setDataSource_l(dataSource);
291}
292
293status_t AwesomePlayer::setDataSource_l(
294 const sp<DataSource> &dataSource) {
295 sp<MediaExtractor> extractor = MediaExtractor::Create(dataSource);
Andreas Huber27366fc2009-11-20 09:32:46 -0800296
297 if (extractor == NULL) {
298 return UNKNOWN_ERROR;
299 }
300
301 return setDataSource_l(extractor);
302}
303
304status_t AwesomePlayer::setDataSource_l(const sp<MediaExtractor> &extractor) {
Andreas Huber27366fc2009-11-20 09:32:46 -0800305 bool haveAudio = false;
306 bool haveVideo = false;
307 for (size_t i = 0; i < extractor->countTracks(); ++i) {
308 sp<MetaData> meta = extractor->getTrackMetaData(i);
309
310 const char *mime;
311 CHECK(meta->findCString(kKeyMIMEType, &mime));
312
313 if (!haveVideo && !strncasecmp(mime, "video/", 6)) {
Andreas Huber3ac94ef2010-03-05 10:42:10 -0800314 setVideoSource(extractor->getTrack(i));
315 haveVideo = true;
Andreas Huber27366fc2009-11-20 09:32:46 -0800316 } else if (!haveAudio && !strncasecmp(mime, "audio/", 6)) {
Andreas Huber3ac94ef2010-03-05 10:42:10 -0800317 setAudioSource(extractor->getTrack(i));
318 haveAudio = true;
Andreas Huber9fee0b22010-09-03 14:09:21 -0700319
Andreas Huber1913c1a2010-10-04 11:09:31 -0700320 if (!strcasecmp(mime, MEDIA_MIMETYPE_AUDIO_VORBIS)) {
321 // Only do this for vorbis audio, none of the other audio
322 // formats even support this ringtone specific hack and
323 // retrieving the metadata on some extractors may turn out
324 // to be very expensive.
325 sp<MetaData> fileMeta = extractor->getMetaData();
326 int32_t loop;
327 if (fileMeta != NULL
328 && fileMeta->findInt32(kKeyAutoLoop, &loop) && loop != 0) {
329 mFlags |= AUTO_LOOPING;
330 }
Andreas Huber9fee0b22010-09-03 14:09:21 -0700331 }
Andreas Huber27366fc2009-11-20 09:32:46 -0800332 }
333
334 if (haveAudio && haveVideo) {
335 break;
336 }
337 }
338
Andreas Huber62f7ffe2010-05-06 10:18:05 -0700339 if (!haveAudio && !haveVideo) {
340 return UNKNOWN_ERROR;
341 }
342
343 mExtractorFlags = extractor->flags();
344
345 return OK;
Andreas Huber27366fc2009-11-20 09:32:46 -0800346}
347
348void AwesomePlayer::reset() {
349 Mutex::Autolock autoLock(mLock);
350 reset_l();
351}
352
353void AwesomePlayer::reset_l() {
Andreas Huberedbb4d82010-03-12 08:59:22 -0800354 if (mFlags & PREPARING) {
355 mFlags |= PREPARE_CANCELLED;
356 if (mConnectingDataSource != NULL) {
357 LOGI("interrupting the connection process");
358 mConnectingDataSource->disconnect();
359 }
360 }
361
Andreas Huberffdf4782010-02-09 14:05:43 -0800362 while (mFlags & PREPARING) {
363 mPreparedCondition.wait(mLock);
364 }
365
Andreas Huber27366fc2009-11-20 09:32:46 -0800366 cancelPlayerEvents();
367
Andreas Huber4d61f602010-06-10 11:17:50 -0700368 mCachedSource.clear();
Andreas Huber3ac94ef2010-03-05 10:42:10 -0800369 mAudioTrack.clear();
370 mVideoTrack.clear();
371
Andreas Huberba7ec912010-02-12 10:42:02 -0800372 // Shutdown audio first, so that the respone to the reset request
373 // appears to happen instantaneously as far as the user is concerned
374 // If we did this later, audio would continue playing while we
375 // shutdown the video-related resources and the player appear to
376 // not be as responsive to a reset request.
Andreas Huberedbb4d82010-03-12 08:59:22 -0800377 if (mAudioPlayer == NULL && mAudioSource != NULL) {
378 // If we had an audio player, it would have effectively
379 // taken possession of the audio source and stopped it when
380 // _it_ is stopped. Otherwise this is still our responsibility.
381 mAudioSource->stop();
382 }
Andreas Huberba7ec912010-02-12 10:42:02 -0800383 mAudioSource.clear();
384
Andreas Huberba7ec912010-02-12 10:42:02 -0800385 mTimeSource = NULL;
386
387 delete mAudioPlayer;
388 mAudioPlayer = NULL;
389
Andreas Huber3522b5a52010-01-22 14:36:53 -0800390 mVideoRenderer.clear();
391
Andreas Huber27366fc2009-11-20 09:32:46 -0800392 if (mLastVideoBuffer) {
393 mLastVideoBuffer->release();
394 mLastVideoBuffer = NULL;
395 }
396
397 if (mVideoBuffer) {
398 mVideoBuffer->release();
399 mVideoBuffer = NULL;
400 }
401
Andreas Hubere0dd7d32010-08-24 14:33:58 -0700402 if (mRTSPController != NULL) {
403 mRTSPController->disconnect();
404 mRTSPController.clear();
405 }
406
Andreas Huber57648e42010-08-04 10:14:30 -0700407 mRTPPusher.clear();
408 mRTCPPusher.clear();
409 mRTPSession.clear();
Andreas Huber7a747b82010-06-07 15:19:40 -0700410
Andreas Huber27366fc2009-11-20 09:32:46 -0800411 if (mVideoSource != NULL) {
412 mVideoSource->stop();
Andreas Huber98b48de2010-01-29 10:10:22 -0800413
414 // The following hack is necessary to ensure that the OMX
415 // component is completely released by the time we may try
416 // to instantiate it again.
417 wp<MediaSource> tmp = mVideoSource;
Andreas Huber27366fc2009-11-20 09:32:46 -0800418 mVideoSource.clear();
Andreas Huber98b48de2010-01-29 10:10:22 -0800419 while (tmp.promote() != NULL) {
420 usleep(1000);
421 }
422 IPCThreadState::self()->flushCommands();
Andreas Huber27366fc2009-11-20 09:32:46 -0800423 }
424
Andreas Huber27366fc2009-11-20 09:32:46 -0800425 mDurationUs = -1;
426 mFlags = 0;
Andreas Huber62f7ffe2010-05-06 10:18:05 -0700427 mExtractorFlags = 0;
Andreas Huber27366fc2009-11-20 09:32:46 -0800428 mVideoWidth = mVideoHeight = -1;
429 mTimeSourceDeltaUs = 0;
430 mVideoTimeUs = 0;
431
432 mSeeking = false;
Andreas Huber8e2b9412010-03-31 09:40:15 -0700433 mSeekNotificationSent = false;
Andreas Huber27366fc2009-11-20 09:32:46 -0800434 mSeekTimeUs = 0;
Andreas Huberb9e63832010-01-26 16:20:10 -0800435
Andreas Huberffdf4782010-02-09 14:05:43 -0800436 mUri.setTo("");
437 mUriHeaders.clear();
Andreas Huberba7ec912010-02-12 10:42:02 -0800438
439 mFileSource.clear();
440
441 delete mSuspensionState;
442 mSuspensionState = NULL;
Andreas Huber27366fc2009-11-20 09:32:46 -0800443}
444
Andreas Huber6be780e2010-02-08 14:40:30 -0800445void AwesomePlayer::notifyListener_l(int msg, int ext1, int ext2) {
Andreas Hubera3f43842010-01-21 10:28:45 -0800446 if (mListener != NULL) {
447 sp<MediaPlayerBase> listener = mListener.promote();
448
449 if (listener != NULL) {
Andreas Huber6be780e2010-02-08 14:40:30 -0800450 listener->sendEvent(msg, ext1, ext2);
Andreas Hubera3f43842010-01-21 10:28:45 -0800451 }
452 }
453}
454
Andreas Huber87ab9cd2010-09-03 13:20:33 -0700455// Returns true iff cached duration is available/applicable.
456bool AwesomePlayer::getCachedDuration_l(int64_t *durationUs, bool *eos) {
457 off_t totalSize;
458
459 if (mRTSPController != NULL) {
460 *durationUs = mRTSPController->getQueueDurationUs(eos);
461 return true;
462 } else if (mCachedSource != NULL && mDurationUs >= 0
463 && mCachedSource->getSize(&totalSize) == OK) {
464 int64_t bitrate = totalSize * 8000000ll / mDurationUs; // in bits/sec
465
466 size_t cachedDataRemaining = mCachedSource->approxDataRemaining(eos);
467 *durationUs = cachedDataRemaining * 8000000ll / bitrate;
468 return true;
469 }
470
471 return false;
472}
473
Andreas Huberb9e63832010-01-26 16:20:10 -0800474void AwesomePlayer::onBufferingUpdate() {
475 Mutex::Autolock autoLock(mLock);
Andreas Huberc0178f12010-02-17 15:58:57 -0800476 if (!mBufferingEventPending) {
477 return;
478 }
Andreas Huberb9e63832010-01-26 16:20:10 -0800479 mBufferingEventPending = false;
480
Andreas Huber87ab9cd2010-09-03 13:20:33 -0700481 if (mCachedSource != NULL) {
Andreas Huber4d8f66b2010-09-01 15:05:28 -0700482 bool eos;
Andreas Huber87ab9cd2010-09-03 13:20:33 -0700483 size_t cachedDataRemaining = mCachedSource->approxDataRemaining(&eos);
Andreas Huber4d8f66b2010-09-01 15:05:28 -0700484
Andreas Huber87ab9cd2010-09-03 13:20:33 -0700485 if (eos) {
486 notifyListener_l(MEDIA_BUFFERING_UPDATE, 100);
487 } else {
488 off_t size;
489 if (mDurationUs >= 0 && mCachedSource->getSize(&size) == OK) {
490 int64_t bitrate = size * 8000000ll / mDurationUs; // in bits/sec
Andreas Huber4d8f66b2010-09-01 15:05:28 -0700491
Andreas Huber87ab9cd2010-09-03 13:20:33 -0700492 size_t cachedSize = mCachedSource->cachedSize();
493 int64_t cachedDurationUs = cachedSize * 8000000ll / bitrate;
494
495 int percentage = 100.0 * (double)cachedDurationUs / mDurationUs;
496 if (percentage > 100) {
497 percentage = 100;
498 }
499
500 notifyListener_l(MEDIA_BUFFERING_UPDATE, percentage);
501 } else {
502 // We don't know the bitrate of the stream, use absolute size
503 // limits to maintain the cache.
504
505 const size_t kLowWaterMarkBytes = 400000;
506 const size_t kHighWaterMarkBytes = 1000000;
507
508 if ((mFlags & PLAYING) && !eos
509 && (cachedDataRemaining < kLowWaterMarkBytes)) {
510 LOGI("cache is running low (< %d) , pausing.",
511 kLowWaterMarkBytes);
512 mFlags |= CACHE_UNDERRUN;
513 pause_l();
514 notifyListener_l(MEDIA_INFO, MEDIA_INFO_BUFFERING_START);
515 } else if (eos || cachedDataRemaining > kHighWaterMarkBytes) {
516 if (mFlags & CACHE_UNDERRUN) {
517 LOGI("cache has filled up (> %d), resuming.",
518 kHighWaterMarkBytes);
519 mFlags &= ~CACHE_UNDERRUN;
520 play_l();
521 notifyListener_l(MEDIA_INFO, MEDIA_INFO_BUFFERING_END);
522 } else if (mFlags & PREPARING) {
523 LOGV("cache has filled up (> %d), prepare is done",
524 kHighWaterMarkBytes);
525 finishAsyncPrepare_l();
526 }
527 }
528 }
529 }
530 }
531
532 int64_t cachedDurationUs;
533 bool eos;
534 if (getCachedDuration_l(&cachedDurationUs, &eos)) {
Andreas Huber4d8f66b2010-09-01 15:05:28 -0700535 if ((mFlags & PLAYING) && !eos
Andreas Huber87ab9cd2010-09-03 13:20:33 -0700536 && (cachedDurationUs < kLowWaterMarkUs)) {
537 LOGI("cache is running low (%.2f secs) , pausing.",
538 cachedDurationUs / 1E6);
Andreas Huber4d8f66b2010-09-01 15:05:28 -0700539 mFlags |= CACHE_UNDERRUN;
540 pause_l();
541 notifyListener_l(MEDIA_INFO, MEDIA_INFO_BUFFERING_START);
Andreas Huber87ab9cd2010-09-03 13:20:33 -0700542 } else if (eos || cachedDurationUs > kHighWaterMarkUs) {
543 if (mFlags & CACHE_UNDERRUN) {
544 LOGI("cache has filled up (%.2f secs), resuming.",
545 cachedDurationUs / 1E6);
546 mFlags &= ~CACHE_UNDERRUN;
547 play_l();
548 notifyListener_l(MEDIA_INFO, MEDIA_INFO_BUFFERING_END);
549 } else if (mFlags & PREPARING) {
550 LOGV("cache has filled up (%.2f secs), prepare is done",
551 cachedDurationUs / 1E6);
552 finishAsyncPrepare_l();
Andreas Huberc23296e2010-08-25 12:31:48 -0700553 }
Andreas Huberc23296e2010-08-25 12:31:48 -0700554 }
Andreas Huberb9e63832010-01-26 16:20:10 -0800555 }
Andreas Huber4d61f602010-06-10 11:17:50 -0700556
Andreas Huber4d61f602010-06-10 11:17:50 -0700557 postBufferingEvent_l();
Andreas Huberb9e63832010-01-26 16:20:10 -0800558}
559
Andreas Huber27366fc2009-11-20 09:32:46 -0800560void AwesomePlayer::onStreamDone() {
561 // Posted whenever any stream finishes playing.
562
563 Mutex::Autolock autoLock(mLock);
Andreas Huberc0178f12010-02-17 15:58:57 -0800564 if (!mStreamDoneEventPending) {
565 return;
566 }
Andreas Huber27366fc2009-11-20 09:32:46 -0800567 mStreamDoneEventPending = false;
568
Andreas Huber971305d2010-07-07 13:35:27 -0700569 if (mStreamDoneStatus != ERROR_END_OF_STREAM) {
570 LOGV("MEDIA_ERROR %d", mStreamDoneStatus);
571
572 notifyListener_l(
573 MEDIA_ERROR, MEDIA_ERROR_UNKNOWN, mStreamDoneStatus);
574
575 pause_l();
576
577 mFlags |= AT_EOS;
578 return;
579 }
580
581 const bool allDone =
582 (mVideoSource == NULL || (mFlags & VIDEO_AT_EOS))
583 && (mAudioSource == NULL || (mFlags & AUDIO_AT_EOS));
584
585 if (!allDone) {
586 return;
587 }
588
Andreas Huber9fee0b22010-09-03 14:09:21 -0700589 if (mFlags & (LOOPING | AUTO_LOOPING)) {
Andreas Huber27366fc2009-11-20 09:32:46 -0800590 seekTo_l(0);
591
Andreas Huber7085b6842010-02-03 16:02:02 -0800592 if (mVideoSource != NULL) {
Andreas Huber27366fc2009-11-20 09:32:46 -0800593 postVideoEvent_l();
594 }
595 } else {
Andreas Huber971305d2010-07-07 13:35:27 -0700596 LOGV("MEDIA_PLAYBACK_COMPLETE");
597 notifyListener_l(MEDIA_PLAYBACK_COMPLETE);
Andreas Huber27366fc2009-11-20 09:32:46 -0800598
599 pause_l();
Andreas Huber406a18b2010-02-18 16:45:13 -0800600
601 mFlags |= AT_EOS;
Andreas Huber27366fc2009-11-20 09:32:46 -0800602 }
603}
604
605status_t AwesomePlayer::play() {
606 Mutex::Autolock autoLock(mLock);
Andreas Huber4d61f602010-06-10 11:17:50 -0700607
608 mFlags &= ~CACHE_UNDERRUN;
609
Andreas Huberba7ec912010-02-12 10:42:02 -0800610 return play_l();
611}
Andreas Huber27366fc2009-11-20 09:32:46 -0800612
Andreas Huberba7ec912010-02-12 10:42:02 -0800613status_t AwesomePlayer::play_l() {
Andreas Huber27366fc2009-11-20 09:32:46 -0800614 if (mFlags & PLAYING) {
615 return OK;
616 }
617
Andreas Huberffdf4782010-02-09 14:05:43 -0800618 if (!(mFlags & PREPARED)) {
619 status_t err = prepare_l();
620
621 if (err != OK) {
622 return err;
623 }
624 }
625
Andreas Huber27366fc2009-11-20 09:32:46 -0800626 mFlags |= PLAYING;
627 mFlags |= FIRST_FRAME;
628
Andreas Huberc1d5c922009-12-10 15:49:04 -0800629 bool deferredAudioSeek = false;
630
Andreas Huber27366fc2009-11-20 09:32:46 -0800631 if (mAudioSource != NULL) {
632 if (mAudioPlayer == NULL) {
633 if (mAudioSink != NULL) {
Andreas Huber2b359ed2010-09-28 11:56:39 -0700634 mAudioPlayer = new AudioPlayer(mAudioSink, this);
Andreas Huber27366fc2009-11-20 09:32:46 -0800635 mAudioPlayer->setSource(mAudioSource);
Andreas Huberdc9927d2010-03-08 15:46:13 -0800636
637 // We've already started the MediaSource in order to enable
638 // the prefetcher to read its data.
639 status_t err = mAudioPlayer->start(
640 true /* sourceAlreadyStarted */);
Andreas Huber62eac002010-01-29 13:24:58 -0800641
642 if (err != OK) {
643 delete mAudioPlayer;
644 mAudioPlayer = NULL;
645
646 mFlags &= ~(PLAYING | FIRST_FRAME);
647
648 return err;
649 }
Andreas Huber27366fc2009-11-20 09:32:46 -0800650
Andreas Huber27366fc2009-11-20 09:32:46 -0800651 mTimeSource = mAudioPlayer;
652
Andreas Huberc1d5c922009-12-10 15:49:04 -0800653 deferredAudioSeek = true;
Andreas Huber70d10c02010-02-03 11:37:29 -0800654
655 mWatchForAudioSeekComplete = false;
656 mWatchForAudioEOS = true;
Andreas Huber27366fc2009-11-20 09:32:46 -0800657 }
658 } else {
659 mAudioPlayer->resume();
660 }
661 }
662
663 if (mTimeSource == NULL && mAudioPlayer == NULL) {
Andreas Huber971305d2010-07-07 13:35:27 -0700664 mTimeSource = &mSystemTimeSource;
Andreas Huber27366fc2009-11-20 09:32:46 -0800665 }
666
667 if (mVideoSource != NULL) {
Andreas Huber7085b6842010-02-03 16:02:02 -0800668 // Kick off video playback
669 postVideoEvent_l();
Andreas Huber27366fc2009-11-20 09:32:46 -0800670 }
671
Andreas Huberc1d5c922009-12-10 15:49:04 -0800672 if (deferredAudioSeek) {
673 // If there was a seek request while we were paused
674 // and we're just starting up again, honor the request now.
675 seekAudioIfNecessary_l();
676 }
677
Andreas Huber406a18b2010-02-18 16:45:13 -0800678 if (mFlags & AT_EOS) {
679 // Legacy behaviour, if a stream finishes playing and then
680 // is started again, we play from the start...
681 seekTo_l(0);
682 }
683
Andreas Huber27366fc2009-11-20 09:32:46 -0800684 return OK;
685}
686
687void AwesomePlayer::initRenderer_l() {
688 if (mISurface != NULL) {
689 sp<MetaData> meta = mVideoSource->getFormat();
690
691 int32_t format;
692 const char *component;
693 int32_t decodedWidth, decodedHeight;
694 CHECK(meta->findInt32(kKeyColorFormat, &format));
695 CHECK(meta->findCString(kKeyDecoderComponent, &component));
696 CHECK(meta->findInt32(kKeyWidth, &decodedWidth));
697 CHECK(meta->findInt32(kKeyHeight, &decodedHeight));
698
Andreas Hubera67d5382009-12-10 15:32:12 -0800699 mVideoRenderer.clear();
700
701 // Must ensure that mVideoRenderer's destructor is actually executed
702 // before creating a new one.
703 IPCThreadState::self()->flushCommands();
704
Andreas Huber1314e732009-12-14 14:18:22 -0800705 if (!strncmp("OMX.", component, 4)) {
706 // Our OMX codecs allocate buffers on the media_server side
707 // therefore they require a remote IOMXRenderer that knows how
708 // to display them.
709 mVideoRenderer = new AwesomeRemoteRenderer(
710 mClient.interface()->createRenderer(
711 mISurface, component,
712 (OMX_COLOR_FORMATTYPE)format,
713 decodedWidth, decodedHeight,
714 mVideoWidth, mVideoHeight));
715 } else {
716 // Other decoders are instantiated locally and as a consequence
717 // allocate their buffers in local address space.
718 mVideoRenderer = new AwesomeLocalRenderer(
Andreas Huber7b73cfc2010-02-12 14:40:08 -0800719 false, // previewOnly
Andreas Huber4ab5a6f2010-02-11 11:00:26 -0800720 component,
Andreas Huber1314e732009-12-14 14:18:22 -0800721 (OMX_COLOR_FORMATTYPE)format,
722 mISurface,
723 mVideoWidth, mVideoHeight,
724 decodedWidth, decodedHeight);
725 }
Andreas Huber27366fc2009-11-20 09:32:46 -0800726 }
727}
728
729status_t AwesomePlayer::pause() {
730 Mutex::Autolock autoLock(mLock);
Andreas Huber4d61f602010-06-10 11:17:50 -0700731
732 mFlags &= ~CACHE_UNDERRUN;
733
Andreas Huber27366fc2009-11-20 09:32:46 -0800734 return pause_l();
735}
736
737status_t AwesomePlayer::pause_l() {
738 if (!(mFlags & PLAYING)) {
739 return OK;
740 }
741
Andreas Huberb9e63832010-01-26 16:20:10 -0800742 cancelPlayerEvents(true /* keepBufferingGoing */);
Andreas Huber27366fc2009-11-20 09:32:46 -0800743
744 if (mAudioPlayer != NULL) {
745 mAudioPlayer->pause();
746 }
747
748 mFlags &= ~PLAYING;
749
750 return OK;
751}
752
753bool AwesomePlayer::isPlaying() const {
Andreas Huber4d61f602010-06-10 11:17:50 -0700754 return (mFlags & PLAYING) || (mFlags & CACHE_UNDERRUN);
Andreas Huber27366fc2009-11-20 09:32:46 -0800755}
756
757void AwesomePlayer::setISurface(const sp<ISurface> &isurface) {
758 Mutex::Autolock autoLock(mLock);
759
760 mISurface = isurface;
761}
762
763void AwesomePlayer::setAudioSink(
764 const sp<MediaPlayerBase::AudioSink> &audioSink) {
765 Mutex::Autolock autoLock(mLock);
766
767 mAudioSink = audioSink;
768}
769
770status_t AwesomePlayer::setLooping(bool shouldLoop) {
771 Mutex::Autolock autoLock(mLock);
772
773 mFlags = mFlags & ~LOOPING;
774
775 if (shouldLoop) {
776 mFlags |= LOOPING;
777 }
778
779 return OK;
780}
781
782status_t AwesomePlayer::getDuration(int64_t *durationUs) {
Andreas Huber252573c2010-03-26 10:17:17 -0700783 Mutex::Autolock autoLock(mMiscStateLock);
Andreas Huber27366fc2009-11-20 09:32:46 -0800784
785 if (mDurationUs < 0) {
786 return UNKNOWN_ERROR;
787 }
788
789 *durationUs = mDurationUs;
790
791 return OK;
792}
793
794status_t AwesomePlayer::getPosition(int64_t *positionUs) {
Andreas Hubereeb97d92010-08-27 13:29:08 -0700795 if (mRTSPController != NULL) {
796 *positionUs = mRTSPController->getNormalPlayTimeUs();
797 }
798 else if (mSeeking) {
Andreas Huberddb709c2010-04-07 10:24:35 -0700799 *positionUs = mSeekTimeUs;
800 } else if (mVideoSource != NULL) {
Andreas Huber252573c2010-03-26 10:17:17 -0700801 Mutex::Autolock autoLock(mMiscStateLock);
Andreas Huber27366fc2009-11-20 09:32:46 -0800802 *positionUs = mVideoTimeUs;
803 } else if (mAudioPlayer != NULL) {
804 *positionUs = mAudioPlayer->getMediaTimeUs();
805 } else {
806 *positionUs = 0;
807 }
808
809 return OK;
810}
811
812status_t AwesomePlayer::seekTo(int64_t timeUs) {
Andreas Huber62f7ffe2010-05-06 10:18:05 -0700813 if (mExtractorFlags
814 & (MediaExtractor::CAN_SEEK_FORWARD
815 | MediaExtractor::CAN_SEEK_BACKWARD)) {
816 Mutex::Autolock autoLock(mLock);
817 return seekTo_l(timeUs);
818 }
819
820 return OK;
Andreas Huber27366fc2009-11-20 09:32:46 -0800821}
822
823status_t AwesomePlayer::seekTo_l(int64_t timeUs) {
Andreas Hubere0dd7d32010-08-24 14:33:58 -0700824 if (mRTSPController != NULL) {
Andreas Hubere0dd7d32010-08-24 14:33:58 -0700825 mRTSPController->seek(timeUs);
Andreas Hubere0dd7d32010-08-24 14:33:58 -0700826
827 notifyListener_l(MEDIA_SEEK_COMPLETE);
828 mSeekNotificationSent = true;
Andreas Hubere0dd7d32010-08-24 14:33:58 -0700829 return OK;
830 }
831
Andreas Huber4d61f602010-06-10 11:17:50 -0700832 if (mFlags & CACHE_UNDERRUN) {
833 mFlags &= ~CACHE_UNDERRUN;
834 play_l();
835 }
836
Andreas Huber27366fc2009-11-20 09:32:46 -0800837 mSeeking = true;
Andreas Huber8e2b9412010-03-31 09:40:15 -0700838 mSeekNotificationSent = false;
Andreas Huber27366fc2009-11-20 09:32:46 -0800839 mSeekTimeUs = timeUs;
Andreas Huber971305d2010-07-07 13:35:27 -0700840 mFlags &= ~(AT_EOS | AUDIO_AT_EOS | VIDEO_AT_EOS);
Andreas Huber27366fc2009-11-20 09:32:46 -0800841
842 seekAudioIfNecessary_l();
843
Andreas Huber8e2b9412010-03-31 09:40:15 -0700844 if (!(mFlags & PLAYING)) {
845 LOGV("seeking while paused, sending SEEK_COMPLETE notification"
846 " immediately.");
847
848 notifyListener_l(MEDIA_SEEK_COMPLETE);
849 mSeekNotificationSent = true;
850 }
851
Andreas Huber27366fc2009-11-20 09:32:46 -0800852 return OK;
853}
854
855void AwesomePlayer::seekAudioIfNecessary_l() {
Andreas Huber7085b6842010-02-03 16:02:02 -0800856 if (mSeeking && mVideoSource == NULL && mAudioPlayer != NULL) {
Andreas Huber27366fc2009-11-20 09:32:46 -0800857 mAudioPlayer->seekTo(mSeekTimeUs);
858
Andreas Huber70d10c02010-02-03 11:37:29 -0800859 mWatchForAudioSeekComplete = true;
860 mWatchForAudioEOS = true;
Andreas Huber8e2b9412010-03-31 09:40:15 -0700861 mSeekNotificationSent = false;
Andreas Huber27366fc2009-11-20 09:32:46 -0800862 }
863}
864
865status_t AwesomePlayer::getVideoDimensions(
866 int32_t *width, int32_t *height) const {
867 Mutex::Autolock autoLock(mLock);
868
869 if (mVideoWidth < 0 || mVideoHeight < 0) {
870 return UNKNOWN_ERROR;
871 }
872
873 *width = mVideoWidth;
874 *height = mVideoHeight;
875
876 return OK;
877}
878
Andreas Huber3ac94ef2010-03-05 10:42:10 -0800879void AwesomePlayer::setAudioSource(sp<MediaSource> source) {
880 CHECK(source != NULL);
Andreas Huber27366fc2009-11-20 09:32:46 -0800881
Andreas Huber3ac94ef2010-03-05 10:42:10 -0800882 mAudioTrack = source;
883}
884
885status_t AwesomePlayer::initAudioDecoder() {
886 sp<MetaData> meta = mAudioTrack->getFormat();
Andreas Huberc79827a2010-01-05 10:54:55 -0800887
888 const char *mime;
889 CHECK(meta->findCString(kKeyMIMEType, &mime));
890
891 if (!strcasecmp(mime, MEDIA_MIMETYPE_AUDIO_RAW)) {
Andreas Huber3ac94ef2010-03-05 10:42:10 -0800892 mAudioSource = mAudioTrack;
Andreas Huberc79827a2010-01-05 10:54:55 -0800893 } else {
894 mAudioSource = OMXCodec::Create(
Andreas Huber3ac94ef2010-03-05 10:42:10 -0800895 mClient.interface(), mAudioTrack->getFormat(),
Andreas Huberc79827a2010-01-05 10:54:55 -0800896 false, // createEncoder
Andreas Huber3ac94ef2010-03-05 10:42:10 -0800897 mAudioTrack);
Andreas Huberc79827a2010-01-05 10:54:55 -0800898 }
Andreas Huber27366fc2009-11-20 09:32:46 -0800899
900 if (mAudioSource != NULL) {
901 int64_t durationUs;
Andreas Huber3ac94ef2010-03-05 10:42:10 -0800902 if (mAudioTrack->getFormat()->findInt64(kKeyDuration, &durationUs)) {
Andreas Huber252573c2010-03-26 10:17:17 -0700903 Mutex::Autolock autoLock(mMiscStateLock);
Andreas Huber27366fc2009-11-20 09:32:46 -0800904 if (mDurationUs < 0 || durationUs > mDurationUs) {
905 mDurationUs = durationUs;
906 }
907 }
Andreas Huber27366fc2009-11-20 09:32:46 -0800908
Andreas Huber3c78a1b2010-05-13 09:15:21 -0700909 status_t err = mAudioSource->start();
910
911 if (err != OK) {
912 mAudioSource.clear();
913 return err;
914 }
Andreas Huberd0332ad2010-04-12 16:05:57 -0700915 } else if (!strcasecmp(mime, MEDIA_MIMETYPE_AUDIO_QCELP)) {
916 // For legacy reasons we're simply going to ignore the absence
917 // of an audio decoder for QCELP instead of aborting playback
918 // altogether.
919 return OK;
920 }
Andreas Huberdc9927d2010-03-08 15:46:13 -0800921
Andreas Huber27366fc2009-11-20 09:32:46 -0800922 return mAudioSource != NULL ? OK : UNKNOWN_ERROR;
923}
924
Andreas Huber3ac94ef2010-03-05 10:42:10 -0800925void AwesomePlayer::setVideoSource(sp<MediaSource> source) {
926 CHECK(source != NULL);
Andreas Huber27366fc2009-11-20 09:32:46 -0800927
Andreas Huber3ac94ef2010-03-05 10:42:10 -0800928 mVideoTrack = source;
929}
930
931status_t AwesomePlayer::initVideoDecoder() {
Andreas Huber57648e42010-08-04 10:14:30 -0700932 uint32_t flags = 0;
Andreas Huber27366fc2009-11-20 09:32:46 -0800933 mVideoSource = OMXCodec::Create(
Andreas Huber3ac94ef2010-03-05 10:42:10 -0800934 mClient.interface(), mVideoTrack->getFormat(),
Andreas Huber27366fc2009-11-20 09:32:46 -0800935 false, // createEncoder
Andreas Huber57648e42010-08-04 10:14:30 -0700936 mVideoTrack,
937 NULL, flags);
Andreas Huber27366fc2009-11-20 09:32:46 -0800938
939 if (mVideoSource != NULL) {
940 int64_t durationUs;
Andreas Huber3ac94ef2010-03-05 10:42:10 -0800941 if (mVideoTrack->getFormat()->findInt64(kKeyDuration, &durationUs)) {
Andreas Huber252573c2010-03-26 10:17:17 -0700942 Mutex::Autolock autoLock(mMiscStateLock);
Andreas Huber27366fc2009-11-20 09:32:46 -0800943 if (mDurationUs < 0 || durationUs > mDurationUs) {
944 mDurationUs = durationUs;
945 }
946 }
947
Andreas Huber3ac94ef2010-03-05 10:42:10 -0800948 CHECK(mVideoTrack->getFormat()->findInt32(kKeyWidth, &mVideoWidth));
949 CHECK(mVideoTrack->getFormat()->findInt32(kKeyHeight, &mVideoHeight));
Andreas Huber27366fc2009-11-20 09:32:46 -0800950
Andreas Huber1919e5a2010-05-20 10:37:06 -0700951 status_t err = mVideoSource->start();
952
953 if (err != OK) {
954 mVideoSource.clear();
955 return err;
956 }
Andreas Huber27366fc2009-11-20 09:32:46 -0800957 }
958
959 return mVideoSource != NULL ? OK : UNKNOWN_ERROR;
960}
961
Andreas Huber6be780e2010-02-08 14:40:30 -0800962void AwesomePlayer::onVideoEvent() {
Andreas Huber27366fc2009-11-20 09:32:46 -0800963 Mutex::Autolock autoLock(mLock);
Andreas Huberba7ec912010-02-12 10:42:02 -0800964 if (!mVideoEventPending) {
965 // The event has been cancelled in reset_l() but had already
966 // been scheduled for execution at that time.
967 return;
968 }
Andreas Huber27366fc2009-11-20 09:32:46 -0800969 mVideoEventPending = false;
970
971 if (mSeeking) {
972 if (mLastVideoBuffer) {
973 mLastVideoBuffer->release();
974 mLastVideoBuffer = NULL;
975 }
976
977 if (mVideoBuffer) {
978 mVideoBuffer->release();
979 mVideoBuffer = NULL;
980 }
Andreas Huber4d61f602010-06-10 11:17:50 -0700981
982 if (mCachedSource != NULL && mAudioSource != NULL) {
983 // We're going to seek the video source first, followed by
984 // the audio source.
985 // In order to avoid jumps in the DataSource offset caused by
986 // the audio codec prefetching data from the old locations
987 // while the video codec is already reading data from the new
988 // locations, we'll "pause" the audio source, causing it to
989 // stop reading input data until a subsequent seek.
990
991 if (mAudioPlayer != NULL) {
992 mAudioPlayer->pause();
993 }
994 mAudioSource->pause();
995 }
Andreas Huber27366fc2009-11-20 09:32:46 -0800996 }
997
998 if (!mVideoBuffer) {
999 MediaSource::ReadOptions options;
1000 if (mSeeking) {
1001 LOGV("seeking to %lld us (%.2f secs)", mSeekTimeUs, mSeekTimeUs / 1E6);
1002
Andreas Huber6624c9f2010-07-20 15:04:28 -07001003 options.setSeekTo(
1004 mSeekTimeUs, MediaSource::ReadOptions::SEEK_CLOSEST_SYNC);
Andreas Huber27366fc2009-11-20 09:32:46 -08001005 }
1006 for (;;) {
1007 status_t err = mVideoSource->read(&mVideoBuffer, &options);
Andreas Huberb1f5ee42009-12-14 15:34:11 -08001008 options.clearSeekTo();
Andreas Huber27366fc2009-11-20 09:32:46 -08001009
1010 if (err != OK) {
1011 CHECK_EQ(mVideoBuffer, NULL);
1012
1013 if (err == INFO_FORMAT_CHANGED) {
1014 LOGV("VideoSource signalled format change.");
1015
Andreas Huber7085b6842010-02-03 16:02:02 -08001016 if (mVideoRenderer != NULL) {
Andreas Huber7b73cfc2010-02-12 14:40:08 -08001017 mVideoRendererIsPreview = false;
Andreas Huber7085b6842010-02-03 16:02:02 -08001018 initRenderer_l();
1019 }
Andreas Huber27366fc2009-11-20 09:32:46 -08001020 continue;
1021 }
1022
Andreas Huber971305d2010-07-07 13:35:27 -07001023 mFlags |= VIDEO_AT_EOS;
Andreas Huberd7d22eb2010-02-23 13:45:33 -08001024 postStreamDoneEvent_l(err);
Andreas Huber27366fc2009-11-20 09:32:46 -08001025 return;
1026 }
1027
Andreas Hubera67d5382009-12-10 15:32:12 -08001028 if (mVideoBuffer->range_length() == 0) {
Andreas Huber6ddcf012009-12-10 15:32:12 -08001029 // Some decoders, notably the PV AVC software decoder
1030 // return spurious empty buffers that we just want to ignore.
1031
Andreas Hubera67d5382009-12-10 15:32:12 -08001032 mVideoBuffer->release();
1033 mVideoBuffer = NULL;
1034 continue;
1035 }
1036
Andreas Huber27366fc2009-11-20 09:32:46 -08001037 break;
1038 }
1039 }
1040
1041 int64_t timeUs;
1042 CHECK(mVideoBuffer->meta_data()->findInt64(kKeyTime, &timeUs));
1043
Andreas Huber252573c2010-03-26 10:17:17 -07001044 {
1045 Mutex::Autolock autoLock(mMiscStateLock);
1046 mVideoTimeUs = timeUs;
1047 }
Andreas Huber27366fc2009-11-20 09:32:46 -08001048
1049 if (mSeeking) {
1050 if (mAudioPlayer != NULL) {
1051 LOGV("seeking audio to %lld us (%.2f secs).", timeUs, timeUs / 1E6);
1052
1053 mAudioPlayer->seekTo(timeUs);
Andreas Huber4d61f602010-06-10 11:17:50 -07001054 mAudioPlayer->resume();
Andreas Huber70d10c02010-02-03 11:37:29 -08001055 mWatchForAudioSeekComplete = true;
1056 mWatchForAudioEOS = true;
Andreas Huber8e2b9412010-03-31 09:40:15 -07001057 } else if (!mSeekNotificationSent) {
Andreas Huber27366fc2009-11-20 09:32:46 -08001058 // If we're playing video only, report seek complete now,
1059 // otherwise audio player will notify us later.
Andreas Hubera3f43842010-01-21 10:28:45 -08001060 notifyListener_l(MEDIA_SEEK_COMPLETE);
Andreas Huber27366fc2009-11-20 09:32:46 -08001061 }
1062
1063 mFlags |= FIRST_FRAME;
1064 mSeeking = false;
Andreas Huber8e2b9412010-03-31 09:40:15 -07001065 mSeekNotificationSent = false;
Andreas Huber27366fc2009-11-20 09:32:46 -08001066 }
1067
Andreas Huber971305d2010-07-07 13:35:27 -07001068 TimeSource *ts = (mFlags & AUDIO_AT_EOS) ? &mSystemTimeSource : mTimeSource;
1069
Andreas Huber27366fc2009-11-20 09:32:46 -08001070 if (mFlags & FIRST_FRAME) {
1071 mFlags &= ~FIRST_FRAME;
1072
Andreas Huber971305d2010-07-07 13:35:27 -07001073 mTimeSourceDeltaUs = ts->getRealTimeUs() - timeUs;
Andreas Huber27366fc2009-11-20 09:32:46 -08001074 }
1075
1076 int64_t realTimeUs, mediaTimeUs;
Andreas Huber971305d2010-07-07 13:35:27 -07001077 if (!(mFlags & AUDIO_AT_EOS) && mAudioPlayer != NULL
Andreas Huber27366fc2009-11-20 09:32:46 -08001078 && mAudioPlayer->getMediaTimeMapping(&realTimeUs, &mediaTimeUs)) {
1079 mTimeSourceDeltaUs = realTimeUs - mediaTimeUs;
1080 }
1081
Andreas Huber971305d2010-07-07 13:35:27 -07001082 int64_t nowUs = ts->getRealTimeUs() - mTimeSourceDeltaUs;
Andreas Huber27366fc2009-11-20 09:32:46 -08001083
1084 int64_t latenessUs = nowUs - timeUs;
1085
Andreas Huberf88f8442010-08-10 11:18:36 -07001086 if (mRTPSession != NULL) {
1087 // We'll completely ignore timestamps for gtalk videochat
1088 // and we'll play incoming video as fast as we get it.
1089 latenessUs = 0;
1090 }
1091
Andreas Huber24b0a952009-11-23 14:02:00 -08001092 if (latenessUs > 40000) {
1093 // We're more than 40ms late.
Andreas Huber4a9375e2010-02-09 11:54:33 -08001094 LOGV("we're late by %lld us (%.2f secs)", latenessUs, latenessUs / 1E6);
Andreas Huber27366fc2009-11-20 09:32:46 -08001095
1096 mVideoBuffer->release();
1097 mVideoBuffer = NULL;
1098
1099 postVideoEvent_l();
1100 return;
1101 }
1102
1103 if (latenessUs < -10000) {
1104 // We're more than 10ms early.
1105
1106 postVideoEvent_l(10000);
1107 return;
1108 }
1109
Andreas Huber7b73cfc2010-02-12 14:40:08 -08001110 if (mVideoRendererIsPreview || mVideoRenderer == NULL) {
1111 mVideoRendererIsPreview = false;
1112
Andreas Huber7085b6842010-02-03 16:02:02 -08001113 initRenderer_l();
1114 }
1115
1116 if (mVideoRenderer != NULL) {
1117 mVideoRenderer->render(mVideoBuffer);
1118 }
Andreas Huber27366fc2009-11-20 09:32:46 -08001119
1120 if (mLastVideoBuffer) {
1121 mLastVideoBuffer->release();
1122 mLastVideoBuffer = NULL;
1123 }
1124 mLastVideoBuffer = mVideoBuffer;
1125 mVideoBuffer = NULL;
1126
1127 postVideoEvent_l();
1128}
1129
1130void AwesomePlayer::postVideoEvent_l(int64_t delayUs) {
1131 if (mVideoEventPending) {
1132 return;
1133 }
1134
1135 mVideoEventPending = true;
1136 mQueue.postEventWithDelay(mVideoEvent, delayUs < 0 ? 10000 : delayUs);
1137}
1138
Andreas Huberd7d22eb2010-02-23 13:45:33 -08001139void AwesomePlayer::postStreamDoneEvent_l(status_t status) {
Andreas Huber27366fc2009-11-20 09:32:46 -08001140 if (mStreamDoneEventPending) {
1141 return;
1142 }
1143 mStreamDoneEventPending = true;
Andreas Huberd7d22eb2010-02-23 13:45:33 -08001144
1145 mStreamDoneStatus = status;
Andreas Huber27366fc2009-11-20 09:32:46 -08001146 mQueue.postEvent(mStreamDoneEvent);
1147}
1148
Andreas Huberb9e63832010-01-26 16:20:10 -08001149void AwesomePlayer::postBufferingEvent_l() {
Andreas Huberb9e63832010-01-26 16:20:10 -08001150 if (mBufferingEventPending) {
1151 return;
1152 }
1153 mBufferingEventPending = true;
1154 mQueue.postEventWithDelay(mBufferingEvent, 1000000ll);
1155}
1156
Andreas Huber70d10c02010-02-03 11:37:29 -08001157void AwesomePlayer::postCheckAudioStatusEvent_l() {
1158 if (mAudioStatusEventPending) {
1159 return;
1160 }
1161 mAudioStatusEventPending = true;
Andreas Huber2b359ed2010-09-28 11:56:39 -07001162 mQueue.postEvent(mCheckAudioStatusEvent);
Andreas Huber70d10c02010-02-03 11:37:29 -08001163}
1164
1165void AwesomePlayer::onCheckAudioStatus() {
1166 Mutex::Autolock autoLock(mLock);
Andreas Huberc0178f12010-02-17 15:58:57 -08001167 if (!mAudioStatusEventPending) {
1168 // Event was dispatched and while we were blocking on the mutex,
1169 // has already been cancelled.
1170 return;
1171 }
1172
Andreas Huber70d10c02010-02-03 11:37:29 -08001173 mAudioStatusEventPending = false;
1174
1175 if (mWatchForAudioSeekComplete && !mAudioPlayer->isSeeking()) {
1176 mWatchForAudioSeekComplete = false;
Andreas Huber8e2b9412010-03-31 09:40:15 -07001177
1178 if (!mSeekNotificationSent) {
1179 notifyListener_l(MEDIA_SEEK_COMPLETE);
1180 mSeekNotificationSent = true;
1181 }
Andreas Huberddb709c2010-04-07 10:24:35 -07001182
1183 mSeeking = false;
Andreas Huber70d10c02010-02-03 11:37:29 -08001184 }
1185
Andreas Huberd7d22eb2010-02-23 13:45:33 -08001186 status_t finalStatus;
1187 if (mWatchForAudioEOS && mAudioPlayer->reachedEOS(&finalStatus)) {
Andreas Huber70d10c02010-02-03 11:37:29 -08001188 mWatchForAudioEOS = false;
Andreas Huber971305d2010-07-07 13:35:27 -07001189 mFlags |= AUDIO_AT_EOS;
1190 mFlags |= FIRST_FRAME;
Andreas Huberd7d22eb2010-02-23 13:45:33 -08001191 postStreamDoneEvent_l(finalStatus);
Andreas Huber70d10c02010-02-03 11:37:29 -08001192 }
Andreas Huber70d10c02010-02-03 11:37:29 -08001193}
1194
Andreas Huber6be780e2010-02-08 14:40:30 -08001195status_t AwesomePlayer::prepare() {
1196 Mutex::Autolock autoLock(mLock);
Andreas Huberffdf4782010-02-09 14:05:43 -08001197 return prepare_l();
1198}
Andreas Huber6be780e2010-02-08 14:40:30 -08001199
Andreas Huberffdf4782010-02-09 14:05:43 -08001200status_t AwesomePlayer::prepare_l() {
1201 if (mFlags & PREPARED) {
1202 return OK;
1203 }
1204
1205 if (mFlags & PREPARING) {
1206 return UNKNOWN_ERROR;
1207 }
1208
1209 mIsAsyncPrepare = false;
Andreas Huber6be780e2010-02-08 14:40:30 -08001210 status_t err = prepareAsync_l();
1211
1212 if (err != OK) {
1213 return err;
1214 }
1215
Andreas Huberffdf4782010-02-09 14:05:43 -08001216 while (mFlags & PREPARING) {
Andreas Huber6be780e2010-02-08 14:40:30 -08001217 mPreparedCondition.wait(mLock);
1218 }
1219
Andreas Huberffdf4782010-02-09 14:05:43 -08001220 return mPrepareResult;
Andreas Huber6be780e2010-02-08 14:40:30 -08001221}
1222
1223status_t AwesomePlayer::prepareAsync() {
1224 Mutex::Autolock autoLock(mLock);
Andreas Huberffdf4782010-02-09 14:05:43 -08001225
1226 if (mFlags & PREPARING) {
1227 return UNKNOWN_ERROR; // async prepare already pending
1228 }
1229
1230 mIsAsyncPrepare = true;
Andreas Huber6be780e2010-02-08 14:40:30 -08001231 return prepareAsync_l();
1232}
1233
1234status_t AwesomePlayer::prepareAsync_l() {
Andreas Huberffdf4782010-02-09 14:05:43 -08001235 if (mFlags & PREPARING) {
1236 return UNKNOWN_ERROR; // async prepare already pending
Andreas Huber6be780e2010-02-08 14:40:30 -08001237 }
1238
Andreas Huber406a18b2010-02-18 16:45:13 -08001239 if (!mQueueStarted) {
1240 mQueue.start();
1241 mQueueStarted = true;
1242 }
1243
Andreas Huberffdf4782010-02-09 14:05:43 -08001244 mFlags |= PREPARING;
Andreas Huber6be780e2010-02-08 14:40:30 -08001245 mAsyncPrepareEvent = new AwesomeEvent(
1246 this, &AwesomePlayer::onPrepareAsyncEvent);
1247
1248 mQueue.postEvent(mAsyncPrepareEvent);
1249
1250 return OK;
1251}
1252
Andreas Huberffdf4782010-02-09 14:05:43 -08001253status_t AwesomePlayer::finishSetDataSource_l() {
Andreas Huberedbb4d82010-03-12 08:59:22 -08001254 sp<DataSource> dataSource;
1255
1256 if (!strncasecmp("http://", mUri.string(), 7)) {
Andreas Huber4d61f602010-06-10 11:17:50 -07001257 mConnectingDataSource = new NuHTTPDataSource;
Andreas Huberedbb4d82010-03-12 08:59:22 -08001258
1259 mLock.unlock();
Andreas Huber3a53dc52010-06-11 09:57:46 -07001260 status_t err = mConnectingDataSource->connect(mUri, &mUriHeaders);
Andreas Huberedbb4d82010-03-12 08:59:22 -08001261 mLock.lock();
1262
1263 if (err != OK) {
1264 mConnectingDataSource.clear();
1265
1266 LOGI("mConnectingDataSource->connect() returned %d", err);
1267 return err;
1268 }
1269
Andreas Huber4d61f602010-06-10 11:17:50 -07001270#if 0
1271 mCachedSource = new NuCachedSource2(
1272 new ThrottledSource(
1273 mConnectingDataSource, 50 * 1024 /* bytes/sec */));
1274#else
1275 mCachedSource = new NuCachedSource2(mConnectingDataSource);
1276#endif
Andreas Huberedbb4d82010-03-12 08:59:22 -08001277 mConnectingDataSource.clear();
Andreas Huber4d61f602010-06-10 11:17:50 -07001278
1279 dataSource = mCachedSource;
Andreas Huber202348e2010-06-07 14:35:29 -07001280 } else if (!strncasecmp(mUri.string(), "httplive://", 11)) {
1281 String8 uri("http://");
1282 uri.append(mUri.string() + 11);
1283
1284 dataSource = new LiveSource(uri.string());
1285
Andreas Huber4d61f602010-06-10 11:17:50 -07001286 mCachedSource = new NuCachedSource2(dataSource);
1287 dataSource = mCachedSource;
Andreas Huber202348e2010-06-07 14:35:29 -07001288
1289 sp<MediaExtractor> extractor =
1290 MediaExtractor::Create(dataSource, MEDIA_MIMETYPE_CONTAINER_MPEG2TS);
Andreas Huber4d61f602010-06-10 11:17:50 -07001291
1292 return setDataSource_l(extractor);
Mike Dodd8741dfa2010-08-12 16:04:35 -07001293 } else if (!strncmp("rtsp://gtalk/", mUri.string(), 13)) {
Andreas Huber57648e42010-08-04 10:14:30 -07001294 if (mLooper == NULL) {
1295 mLooper = new ALooper;
Andreas Huberc4e0b702010-08-27 15:21:07 -07001296 mLooper->setName("gtalk rtp");
Andreas Huber3eaa3002010-08-05 09:22:25 -07001297 mLooper->start(
1298 false /* runOnCallingThread */,
1299 false /* canCallJava */,
1300 PRIORITY_HIGHEST);
Andreas Huber57648e42010-08-04 10:14:30 -07001301 }
1302
Mike Dodd8741dfa2010-08-12 16:04:35 -07001303 const char *startOfCodecString = &mUri.string()[13];
1304 const char *startOfSlash1 = strchr(startOfCodecString, '/');
1305 if (startOfSlash1 == NULL) {
1306 return BAD_VALUE;
1307 }
1308 const char *startOfWidthString = &startOfSlash1[1];
1309 const char *startOfSlash2 = strchr(startOfWidthString, '/');
1310 if (startOfSlash2 == NULL) {
1311 return BAD_VALUE;
1312 }
1313 const char *startOfHeightString = &startOfSlash2[1];
1314
1315 String8 codecString(startOfCodecString, startOfSlash1 - startOfCodecString);
1316 String8 widthString(startOfWidthString, startOfSlash2 - startOfWidthString);
1317 String8 heightString(startOfHeightString);
1318
Andreas Huber57648e42010-08-04 10:14:30 -07001319#if 0
1320 mRTPPusher = new UDPPusher("/data/misc/rtpout.bin", 5434);
1321 mLooper->registerHandler(mRTPPusher);
1322
1323 mRTCPPusher = new UDPPusher("/data/misc/rtcpout.bin", 5435);
1324 mLooper->registerHandler(mRTCPPusher);
1325#endif
1326
1327 mRTPSession = new ARTPSession;
1328 mLooper->registerHandler(mRTPSession);
1329
1330#if 0
Andreas Huber57648e42010-08-04 10:14:30 -07001331 // My AMR SDP
1332 static const char *raw =
1333 "v=0\r\n"
1334 "o=- 64 233572944 IN IP4 127.0.0.0\r\n"
1335 "s=QuickTime\r\n"
1336 "t=0 0\r\n"
1337 "a=range:npt=0-315\r\n"
1338 "a=isma-compliance:2,2.0,2\r\n"
1339 "m=audio 5434 RTP/AVP 97\r\n"
1340 "c=IN IP4 127.0.0.1\r\n"
1341 "b=AS:30\r\n"
1342 "a=rtpmap:97 AMR/8000/1\r\n"
1343 "a=fmtp:97 octet-align\r\n";
1344#elif 1
Mike Dodd8741dfa2010-08-12 16:04:35 -07001345 String8 sdp;
1346 sdp.appendFormat(
Andreas Huber57648e42010-08-04 10:14:30 -07001347 "v=0\r\n"
1348 "o=- 64 233572944 IN IP4 127.0.0.0\r\n"
1349 "s=QuickTime\r\n"
1350 "t=0 0\r\n"
1351 "a=range:npt=0-315\r\n"
1352 "a=isma-compliance:2,2.0,2\r\n"
1353 "m=video 5434 RTP/AVP 97\r\n"
1354 "c=IN IP4 127.0.0.1\r\n"
1355 "b=AS:30\r\n"
Mike Dodd8741dfa2010-08-12 16:04:35 -07001356 "a=rtpmap:97 %s/90000\r\n"
1357 "a=cliprect:0,0,%s,%s\r\n"
1358 "a=framesize:97 %s-%s\r\n",
1359
1360 codecString.string(),
1361 heightString.string(), widthString.string(),
1362 widthString.string(), heightString.string()
1363 );
1364 const char *raw = sdp.string();
1365
Andreas Huber57648e42010-08-04 10:14:30 -07001366#endif
1367
1368 sp<ASessionDescription> desc = new ASessionDescription;
1369 CHECK(desc->setTo(raw, strlen(raw)));
1370
1371 CHECK_EQ(mRTPSession->setup(desc), (status_t)OK);
1372
1373 if (mRTPPusher != NULL) {
1374 mRTPPusher->start();
1375 }
1376
1377 if (mRTCPPusher != NULL) {
1378 mRTCPPusher->start();
1379 }
1380
1381 CHECK_EQ(mRTPSession->countTracks(), 1u);
1382 sp<MediaSource> source = mRTPSession->trackAt(0);
1383
1384#if 0
1385 bool eos;
1386 while (((APacketSource *)source.get())
1387 ->getQueuedDuration(&eos) < 5000000ll && !eos) {
1388 usleep(100000ll);
1389 }
1390#endif
1391
1392 const char *mime;
1393 CHECK(source->getFormat()->findCString(kKeyMIMEType, &mime));
1394
1395 if (!strncasecmp("video/", mime, 6)) {
1396 setVideoSource(source);
1397 } else {
1398 CHECK(!strncasecmp("audio/", mime, 6));
1399 setAudioSource(source);
1400 }
1401
1402 mExtractorFlags = MediaExtractor::CAN_PAUSE;
1403
1404 return OK;
Andreas Huber7a747b82010-06-07 15:19:40 -07001405 } else if (!strncasecmp("rtsp://", mUri.string(), 7)) {
1406 if (mLooper == NULL) {
1407 mLooper = new ALooper;
Andreas Huberc4e0b702010-08-27 15:21:07 -07001408 mLooper->setName("rtsp");
Andreas Huber7a747b82010-06-07 15:19:40 -07001409 mLooper->start();
1410 }
1411 mRTSPController = new ARTSPController(mLooper);
1412 status_t err = mRTSPController->connect(mUri.string());
Andreas Huber202348e2010-06-07 14:35:29 -07001413
Andreas Huber7a747b82010-06-07 15:19:40 -07001414 LOGI("ARTSPController::connect returned %d", err);
1415
1416 if (err != OK) {
1417 mRTSPController.clear();
1418 return err;
1419 }
1420
1421 sp<MediaExtractor> extractor = mRTSPController.get();
Andreas Huber202348e2010-06-07 14:35:29 -07001422 return setDataSource_l(extractor);
Andreas Huberedbb4d82010-03-12 08:59:22 -08001423 } else {
1424 dataSource = DataSource::CreateFromURI(mUri.string(), &mUriHeaders);
1425 }
Andreas Huberffdf4782010-02-09 14:05:43 -08001426
1427 if (dataSource == NULL) {
1428 return UNKNOWN_ERROR;
1429 }
1430
1431 sp<MediaExtractor> extractor = MediaExtractor::Create(dataSource);
1432
1433 if (extractor == NULL) {
1434 return UNKNOWN_ERROR;
1435 }
1436
Andreas Huberffdf4782010-02-09 14:05:43 -08001437 return setDataSource_l(extractor);
1438}
1439
Andreas Huber3ac94ef2010-03-05 10:42:10 -08001440void AwesomePlayer::abortPrepare(status_t err) {
1441 CHECK(err != OK);
1442
1443 if (mIsAsyncPrepare) {
1444 notifyListener_l(MEDIA_ERROR, MEDIA_ERROR_UNKNOWN, err);
1445 }
1446
1447 mPrepareResult = err;
Andreas Huberedbb4d82010-03-12 08:59:22 -08001448 mFlags &= ~(PREPARING|PREPARE_CANCELLED);
Andreas Huber3ac94ef2010-03-05 10:42:10 -08001449 mAsyncPrepareEvent = NULL;
1450 mPreparedCondition.broadcast();
1451}
1452
Andreas Huberf71daba2010-03-24 09:24:40 -07001453// static
1454bool AwesomePlayer::ContinuePreparation(void *cookie) {
1455 AwesomePlayer *me = static_cast<AwesomePlayer *>(cookie);
1456
1457 return (me->mFlags & PREPARE_CANCELLED) == 0;
1458}
1459
Andreas Huber6be780e2010-02-08 14:40:30 -08001460void AwesomePlayer::onPrepareAsyncEvent() {
Andreas Huber87ab9cd2010-09-03 13:20:33 -07001461 Mutex::Autolock autoLock(mLock);
Andreas Huberffdf4782010-02-09 14:05:43 -08001462
Andreas Huber87ab9cd2010-09-03 13:20:33 -07001463 if (mFlags & PREPARE_CANCELLED) {
1464 LOGI("prepare was cancelled before doing anything");
1465 abortPrepare(UNKNOWN_ERROR);
1466 return;
1467 }
1468
1469 if (mUri.size() > 0) {
1470 status_t err = finishSetDataSource_l();
1471
1472 if (err != OK) {
1473 abortPrepare(err);
Andreas Huberedbb4d82010-03-12 08:59:22 -08001474 return;
1475 }
Andreas Huber6be780e2010-02-08 14:40:30 -08001476 }
1477
Andreas Huber87ab9cd2010-09-03 13:20:33 -07001478 if (mVideoTrack != NULL && mVideoSource == NULL) {
1479 status_t err = initVideoDecoder();
Andreas Huber6be780e2010-02-08 14:40:30 -08001480
Andreas Huber87ab9cd2010-09-03 13:20:33 -07001481 if (err != OK) {
1482 abortPrepare(err);
1483 return;
1484 }
1485 }
1486
1487 if (mAudioTrack != NULL && mAudioSource == NULL) {
1488 status_t err = initAudioDecoder();
1489
1490 if (err != OK) {
1491 abortPrepare(err);
1492 return;
1493 }
1494 }
1495
1496 if (mCachedSource != NULL || mRTSPController != NULL) {
1497 postBufferingEvent_l();
1498 } else {
1499 finishAsyncPrepare_l();
1500 }
1501}
1502
1503void AwesomePlayer::finishAsyncPrepare_l() {
Andreas Huberffdf4782010-02-09 14:05:43 -08001504 if (mIsAsyncPrepare) {
1505 if (mVideoWidth < 0 || mVideoHeight < 0) {
1506 notifyListener_l(MEDIA_SET_VIDEO_SIZE, 0, 0);
1507 } else {
1508 notifyListener_l(MEDIA_SET_VIDEO_SIZE, mVideoWidth, mVideoHeight);
1509 }
1510
1511 notifyListener_l(MEDIA_PREPARED);
Andreas Huber6be780e2010-02-08 14:40:30 -08001512 }
1513
Andreas Huberffdf4782010-02-09 14:05:43 -08001514 mPrepareResult = OK;
Andreas Huberedbb4d82010-03-12 08:59:22 -08001515 mFlags &= ~(PREPARING|PREPARE_CANCELLED);
Andreas Huberffdf4782010-02-09 14:05:43 -08001516 mFlags |= PREPARED;
Andreas Huber6be780e2010-02-08 14:40:30 -08001517 mAsyncPrepareEvent = NULL;
Andreas Huberffdf4782010-02-09 14:05:43 -08001518 mPreparedCondition.broadcast();
Andreas Huber6be780e2010-02-08 14:40:30 -08001519}
1520
Andreas Huberba7ec912010-02-12 10:42:02 -08001521status_t AwesomePlayer::suspend() {
Andreas Huberedbb4d82010-03-12 08:59:22 -08001522 LOGV("suspend");
Andreas Huberba7ec912010-02-12 10:42:02 -08001523 Mutex::Autolock autoLock(mLock);
1524
1525 if (mSuspensionState != NULL) {
Gloria Wangb19da8e2010-04-12 17:13:06 -07001526 if (mLastVideoBuffer == NULL) {
1527 //go into here if video is suspended again
1528 //after resuming without being played between
1529 //them
1530 SuspensionState *state = mSuspensionState;
1531 mSuspensionState = NULL;
1532 reset_l();
1533 mSuspensionState = state;
1534 return OK;
1535 }
1536
1537 delete mSuspensionState;
1538 mSuspensionState = NULL;
Andreas Huberba7ec912010-02-12 10:42:02 -08001539 }
1540
Andreas Huberedbb4d82010-03-12 08:59:22 -08001541 if (mFlags & PREPARING) {
1542 mFlags |= PREPARE_CANCELLED;
1543 if (mConnectingDataSource != NULL) {
1544 LOGI("interrupting the connection process");
1545 mConnectingDataSource->disconnect();
1546 }
1547 }
1548
Andreas Huberba7ec912010-02-12 10:42:02 -08001549 while (mFlags & PREPARING) {
1550 mPreparedCondition.wait(mLock);
1551 }
1552
1553 SuspensionState *state = new SuspensionState;
1554 state->mUri = mUri;
1555 state->mUriHeaders = mUriHeaders;
1556 state->mFileSource = mFileSource;
1557
Andreas Huber9fee0b22010-09-03 14:09:21 -07001558 state->mFlags = mFlags & (PLAYING | AUTO_LOOPING | LOOPING | AT_EOS);
Andreas Huber252573c2010-03-26 10:17:17 -07001559 getPosition(&state->mPositionUs);
Andreas Huberba7ec912010-02-12 10:42:02 -08001560
Andreas Huber7b73cfc2010-02-12 14:40:08 -08001561 if (mLastVideoBuffer) {
1562 size_t size = mLastVideoBuffer->range_length();
1563 if (size) {
1564 state->mLastVideoFrameSize = size;
1565 state->mLastVideoFrame = malloc(size);
1566 memcpy(state->mLastVideoFrame,
1567 (const uint8_t *)mLastVideoBuffer->data()
1568 + mLastVideoBuffer->range_offset(),
1569 size);
1570
1571 state->mVideoWidth = mVideoWidth;
1572 state->mVideoHeight = mVideoHeight;
1573
1574 sp<MetaData> meta = mVideoSource->getFormat();
1575 CHECK(meta->findInt32(kKeyColorFormat, &state->mColorFormat));
1576 CHECK(meta->findInt32(kKeyWidth, &state->mDecodedWidth));
1577 CHECK(meta->findInt32(kKeyHeight, &state->mDecodedHeight));
1578 }
1579 }
1580
Andreas Huberba7ec912010-02-12 10:42:02 -08001581 reset_l();
1582
1583 mSuspensionState = state;
1584
1585 return OK;
1586}
1587
1588status_t AwesomePlayer::resume() {
Andreas Huberedbb4d82010-03-12 08:59:22 -08001589 LOGV("resume");
Andreas Huberba7ec912010-02-12 10:42:02 -08001590 Mutex::Autolock autoLock(mLock);
1591
1592 if (mSuspensionState == NULL) {
1593 return INVALID_OPERATION;
1594 }
1595
1596 SuspensionState *state = mSuspensionState;
1597 mSuspensionState = NULL;
1598
1599 status_t err;
1600 if (state->mFileSource != NULL) {
1601 err = setDataSource_l(state->mFileSource);
1602
1603 if (err == OK) {
1604 mFileSource = state->mFileSource;
1605 }
1606 } else {
1607 err = setDataSource_l(state->mUri, &state->mUriHeaders);
1608 }
1609
1610 if (err != OK) {
1611 delete state;
1612 state = NULL;
1613
1614 return err;
1615 }
1616
1617 seekTo_l(state->mPositionUs);
1618
Andreas Huber9fee0b22010-09-03 14:09:21 -07001619 mFlags = state->mFlags & (AUTO_LOOPING | LOOPING | AT_EOS);
Andreas Huberba7ec912010-02-12 10:42:02 -08001620
Andreas Huber7b73cfc2010-02-12 14:40:08 -08001621 if (state->mLastVideoFrame && mISurface != NULL) {
1622 mVideoRenderer =
1623 new AwesomeLocalRenderer(
1624 true, // previewOnly
1625 "",
1626 (OMX_COLOR_FORMATTYPE)state->mColorFormat,
1627 mISurface,
1628 state->mVideoWidth,
1629 state->mVideoHeight,
1630 state->mDecodedWidth,
1631 state->mDecodedHeight);
1632
1633 mVideoRendererIsPreview = true;
1634
1635 ((AwesomeLocalRenderer *)mVideoRenderer.get())->render(
1636 state->mLastVideoFrame, state->mLastVideoFrameSize);
1637 }
1638
Andreas Huberba7ec912010-02-12 10:42:02 -08001639 if (state->mFlags & PLAYING) {
1640 play_l();
1641 }
1642
Gloria Wangb19da8e2010-04-12 17:13:06 -07001643 mSuspensionState = state;
Andreas Huberba7ec912010-02-12 10:42:02 -08001644 state = NULL;
1645
1646 return OK;
1647}
1648
Andreas Huber62f7ffe2010-05-06 10:18:05 -07001649uint32_t AwesomePlayer::flags() const {
1650 return mExtractorFlags;
1651}
1652
Andreas Huber2b359ed2010-09-28 11:56:39 -07001653void AwesomePlayer::postAudioEOS() {
1654 postCheckAudioStatusEvent_l();
1655}
1656
1657void AwesomePlayer::postAudioSeekComplete() {
1658 postCheckAudioStatusEvent_l();
1659}
1660
Andreas Huber27366fc2009-11-20 09:32:46 -08001661} // namespace android
1662