blob: effa7038fe6dd722643c031d3b9e92ea5366284b [file] [log] [blame]
Andreas Hubera1587462010-12-15 15:17:42 -08001/*
2 * Copyright (C) 2010 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 "NuPlayer"
19#include <utils/Log.h>
20
21#include "NuPlayer.h"
Andreas Huber54e66492010-12-23 10:27:40 -080022
23#include "HTTPLiveSource.h"
Andreas Hubera1587462010-12-15 15:17:42 -080024#include "NuPlayerDecoder.h"
Andreas Huber08e10cb2011-01-05 12:17:08 -080025#include "NuPlayerDriver.h"
Andreas Hubera1587462010-12-15 15:17:42 -080026#include "NuPlayerRenderer.h"
Andreas Huber54e66492010-12-23 10:27:40 -080027#include "NuPlayerSource.h"
28#include "StreamingSource.h"
29
30#include "ATSParser.h"
Andreas Hubera1587462010-12-15 15:17:42 -080031
Andreas Huber41c3f742010-12-21 10:22:33 -080032#include <media/stagefright/foundation/hexdump.h>
Andreas Hubera1587462010-12-15 15:17:42 -080033#include <media/stagefright/foundation/ABuffer.h>
34#include <media/stagefright/foundation/ADebug.h>
35#include <media/stagefright/foundation/AMessage.h>
36#include <media/stagefright/ACodec.h>
37#include <media/stagefright/MediaErrors.h>
38#include <media/stagefright/MetaData.h>
39#include <surfaceflinger/Surface.h>
Glenn Kastencc562a32011-02-08 17:26:17 -080040#include <gui/ISurfaceTexture.h>
Andreas Hubera1587462010-12-15 15:17:42 -080041
42namespace android {
43
44////////////////////////////////////////////////////////////////////////////////
45
46NuPlayer::NuPlayer()
Andreas Huber54e66492010-12-23 10:27:40 -080047 : mAudioEOS(false),
Andreas Hubera1587462010-12-15 15:17:42 -080048 mVideoEOS(false),
Andreas Huber54e66492010-12-23 10:27:40 -080049 mScanSourcesPending(false),
Andreas Hubercbeaca72011-01-04 14:01:29 -080050 mScanSourcesGeneration(0),
Andreas Hubera1587462010-12-15 15:17:42 -080051 mFlushingAudio(NONE),
Andreas Hubercbeaca72011-01-04 14:01:29 -080052 mFlushingVideo(NONE),
53 mResetInProgress(false),
54 mResetPostponed(false) {
Andreas Hubera1587462010-12-15 15:17:42 -080055}
56
57NuPlayer::~NuPlayer() {
58}
59
Andreas Huber08e10cb2011-01-05 12:17:08 -080060void NuPlayer::setDriver(const wp<NuPlayerDriver> &driver) {
61 mDriver = driver;
Andreas Hubera1587462010-12-15 15:17:42 -080062}
63
64void NuPlayer::setDataSource(const sp<IStreamSource> &source) {
65 sp<AMessage> msg = new AMessage(kWhatSetDataSource, id());
66
Andreas Huber54e66492010-12-23 10:27:40 -080067 msg->setObject("source", new StreamingSource(source));
68 msg->post();
69}
Andreas Hubera1587462010-12-15 15:17:42 -080070
Andreas Huber54e66492010-12-23 10:27:40 -080071void NuPlayer::setDataSource(
72 const char *url, const KeyedVector<String8, String8> *headers) {
73 sp<AMessage> msg = new AMessage(kWhatSetDataSource, id());
74
Andreas Huber50874942011-04-19 11:50:27 -070075 msg->setObject("source", new HTTPLiveSource(url, headers));
Andreas Hubera1587462010-12-15 15:17:42 -080076 msg->post();
77}
78
79void NuPlayer::setVideoSurface(const sp<Surface> &surface) {
Glenn Kastencc562a32011-02-08 17:26:17 -080080 sp<AMessage> msg = new AMessage(kWhatSetVideoNativeWindow, id());
81 msg->setObject("native-window", new NativeWindowWrapper(surface));
82 msg->post();
83}
84
85void NuPlayer::setVideoSurfaceTexture(const sp<ISurfaceTexture> &surfaceTexture) {
86 sp<AMessage> msg = new AMessage(kWhatSetVideoNativeWindow, id());
87 sp<SurfaceTextureClient> surfaceTextureClient(surfaceTexture != NULL ?
88 new SurfaceTextureClient(surfaceTexture) : NULL);
89 msg->setObject("native-window", new NativeWindowWrapper(surfaceTextureClient));
Andreas Hubera1587462010-12-15 15:17:42 -080090 msg->post();
91}
92
93void NuPlayer::setAudioSink(const sp<MediaPlayerBase::AudioSink> &sink) {
94 sp<AMessage> msg = new AMessage(kWhatSetAudioSink, id());
95 msg->setObject("sink", sink);
96 msg->post();
97}
98
99void NuPlayer::start() {
100 (new AMessage(kWhatStart, id()))->post();
101}
102
Andreas Huber08e10cb2011-01-05 12:17:08 -0800103void NuPlayer::pause() {
Andreas Huber601fe0e2011-01-20 15:23:04 -0800104 (new AMessage(kWhatPause, id()))->post();
Andreas Huber08e10cb2011-01-05 12:17:08 -0800105}
106
107void NuPlayer::resume() {
Andreas Huber601fe0e2011-01-20 15:23:04 -0800108 (new AMessage(kWhatResume, id()))->post();
Andreas Huber08e10cb2011-01-05 12:17:08 -0800109}
110
Andreas Hubercbeaca72011-01-04 14:01:29 -0800111void NuPlayer::resetAsync() {
112 (new AMessage(kWhatReset, id()))->post();
113}
114
Andreas Huber08e10cb2011-01-05 12:17:08 -0800115void NuPlayer::seekToAsync(int64_t seekTimeUs) {
116 sp<AMessage> msg = new AMessage(kWhatSeek, id());
117 msg->setInt64("seekTimeUs", seekTimeUs);
118 msg->post();
119}
120
Andreas Huber222e6892010-12-22 10:03:04 -0800121// static
Andreas Hubercbeaca72011-01-04 14:01:29 -0800122bool NuPlayer::IsFlushingState(FlushStatus state, bool *needShutdown) {
Andreas Huber222e6892010-12-22 10:03:04 -0800123 switch (state) {
124 case FLUSHING_DECODER:
Andreas Hubercbeaca72011-01-04 14:01:29 -0800125 if (needShutdown != NULL) {
126 *needShutdown = false;
Andreas Huber222e6892010-12-22 10:03:04 -0800127 }
128 return true;
129
Andreas Hubercbeaca72011-01-04 14:01:29 -0800130 case FLUSHING_DECODER_SHUTDOWN:
131 if (needShutdown != NULL) {
132 *needShutdown = true;
Andreas Huber222e6892010-12-22 10:03:04 -0800133 }
134 return true;
135
136 default:
137 return false;
138 }
139}
140
Andreas Hubera1587462010-12-15 15:17:42 -0800141void NuPlayer::onMessageReceived(const sp<AMessage> &msg) {
142 switch (msg->what()) {
143 case kWhatSetDataSource:
144 {
Andreas Hubercbeaca72011-01-04 14:01:29 -0800145 LOGV("kWhatSetDataSource");
Andreas Hubera1587462010-12-15 15:17:42 -0800146
147 CHECK(mSource == NULL);
148
Andreas Huber54e66492010-12-23 10:27:40 -0800149 sp<RefBase> obj;
150 CHECK(msg->findObject("source", &obj));
Andreas Hubera1587462010-12-15 15:17:42 -0800151
Andreas Huber54e66492010-12-23 10:27:40 -0800152 mSource = static_cast<Source *>(obj.get());
Andreas Hubera1587462010-12-15 15:17:42 -0800153 break;
154 }
155
Glenn Kastencc562a32011-02-08 17:26:17 -0800156 case kWhatSetVideoNativeWindow:
Andreas Hubera1587462010-12-15 15:17:42 -0800157 {
Glenn Kastencc562a32011-02-08 17:26:17 -0800158 LOGV("kWhatSetVideoNativeWindow");
Andreas Hubera1587462010-12-15 15:17:42 -0800159
160 sp<RefBase> obj;
Glenn Kastencc562a32011-02-08 17:26:17 -0800161 CHECK(msg->findObject("native-window", &obj));
Andreas Hubera1587462010-12-15 15:17:42 -0800162
Glenn Kastencc562a32011-02-08 17:26:17 -0800163 mNativeWindow = static_cast<NativeWindowWrapper *>(obj.get());
Andreas Hubera1587462010-12-15 15:17:42 -0800164 break;
165 }
166
167 case kWhatSetAudioSink:
168 {
Andreas Hubercbeaca72011-01-04 14:01:29 -0800169 LOGV("kWhatSetAudioSink");
Andreas Hubera1587462010-12-15 15:17:42 -0800170
171 sp<RefBase> obj;
172 CHECK(msg->findObject("sink", &obj));
173
174 mAudioSink = static_cast<MediaPlayerBase::AudioSink *>(obj.get());
175 break;
176 }
177
178 case kWhatStart:
179 {
Andreas Huber08e10cb2011-01-05 12:17:08 -0800180 LOGV("kWhatStart");
181
Andreas Hubercbeaca72011-01-04 14:01:29 -0800182 mAudioEOS = false;
183 mVideoEOS = false;
Andreas Huber669ad132011-03-02 15:34:46 -0800184 mSkipRenderingAudioUntilMediaTimeUs = -1;
185 mSkipRenderingVideoUntilMediaTimeUs = -1;
Andreas Hubercbeaca72011-01-04 14:01:29 -0800186
Andreas Huber54e66492010-12-23 10:27:40 -0800187 mSource->start();
Andreas Hubera1587462010-12-15 15:17:42 -0800188
189 mRenderer = new Renderer(
190 mAudioSink,
191 new AMessage(kWhatRendererNotify, id()));
192
193 looper()->registerHandler(mRenderer);
194
Andreas Hubercbeaca72011-01-04 14:01:29 -0800195 postScanSources();
Andreas Hubera1587462010-12-15 15:17:42 -0800196 break;
197 }
198
199 case kWhatScanSources:
200 {
Andreas Hubercbeaca72011-01-04 14:01:29 -0800201 int32_t generation;
202 CHECK(msg->findInt32("generation", &generation));
203 if (generation != mScanSourcesGeneration) {
204 // Drop obsolete msg.
205 break;
206 }
207
Andreas Huber54e66492010-12-23 10:27:40 -0800208 mScanSourcesPending = false;
209
Andreas Huber08e10cb2011-01-05 12:17:08 -0800210 LOGV("scanning sources haveAudio=%d, haveVideo=%d",
211 mAudioDecoder != NULL, mVideoDecoder != NULL);
212
Andreas Huber54e66492010-12-23 10:27:40 -0800213 instantiateDecoder(false, &mVideoDecoder);
Andreas Hubera1587462010-12-15 15:17:42 -0800214
215 if (mAudioSink != NULL) {
Andreas Huber54e66492010-12-23 10:27:40 -0800216 instantiateDecoder(true, &mAudioDecoder);
Andreas Hubera1587462010-12-15 15:17:42 -0800217 }
218
Andreas Huber54e66492010-12-23 10:27:40 -0800219 if (!mSource->feedMoreTSData()) {
Andreas Hubercbeaca72011-01-04 14:01:29 -0800220 if (mAudioDecoder == NULL && mVideoDecoder == NULL) {
221 // We're not currently decoding anything (no audio or
222 // video tracks found) and we just ran out of input data.
223 notifyListener(MEDIA_PLAYBACK_COMPLETE, 0, 0);
224 }
Andreas Hubera1587462010-12-15 15:17:42 -0800225 break;
226 }
227
Andreas Hubera1587462010-12-15 15:17:42 -0800228 if (mAudioDecoder == NULL || mVideoDecoder == NULL) {
229 msg->post(100000ll);
Andreas Huber54e66492010-12-23 10:27:40 -0800230 mScanSourcesPending = true;
Andreas Hubera1587462010-12-15 15:17:42 -0800231 }
232 break;
233 }
234
235 case kWhatVideoNotify:
236 case kWhatAudioNotify:
237 {
238 bool audio = msg->what() == kWhatAudioNotify;
239
240 sp<AMessage> codecRequest;
241 CHECK(msg->findMessage("codec-request", &codecRequest));
242
243 int32_t what;
244 CHECK(codecRequest->findInt32("what", &what));
245
246 if (what == ACodec::kWhatFillThisBuffer) {
247 status_t err = feedDecoderInputData(
248 audio, codecRequest);
249
Andreas Huber54e66492010-12-23 10:27:40 -0800250 if (err == -EWOULDBLOCK) {
251 if (mSource->feedMoreTSData()) {
252 msg->post();
253 }
Andreas Hubera1587462010-12-15 15:17:42 -0800254 }
255 } else if (what == ACodec::kWhatEOS) {
256 mRenderer->queueEOS(audio, ERROR_END_OF_STREAM);
257 } else if (what == ACodec::kWhatFlushCompleted) {
Andreas Hubercbeaca72011-01-04 14:01:29 -0800258 bool needShutdown;
Andreas Huber222e6892010-12-22 10:03:04 -0800259
Andreas Hubera1587462010-12-15 15:17:42 -0800260 if (audio) {
Andreas Hubercbeaca72011-01-04 14:01:29 -0800261 CHECK(IsFlushingState(mFlushingAudio, &needShutdown));
Andreas Hubera1587462010-12-15 15:17:42 -0800262 mFlushingAudio = FLUSHED;
263 } else {
Andreas Hubercbeaca72011-01-04 14:01:29 -0800264 CHECK(IsFlushingState(mFlushingVideo, &needShutdown));
Andreas Hubera1587462010-12-15 15:17:42 -0800265 mFlushingVideo = FLUSHED;
266 }
267
Andreas Hubercbeaca72011-01-04 14:01:29 -0800268 LOGV("decoder %s flush completed", audio ? "audio" : "video");
Andreas Hubera1587462010-12-15 15:17:42 -0800269
Andreas Hubercbeaca72011-01-04 14:01:29 -0800270 if (needShutdown) {
271 LOGV("initiating %s decoder shutdown",
Andreas Huber222e6892010-12-22 10:03:04 -0800272 audio ? "audio" : "video");
Andreas Hubera1587462010-12-15 15:17:42 -0800273
Andreas Huber222e6892010-12-22 10:03:04 -0800274 (audio ? mAudioDecoder : mVideoDecoder)->initiateShutdown();
Andreas Hubera1587462010-12-15 15:17:42 -0800275
Andreas Huber222e6892010-12-22 10:03:04 -0800276 if (audio) {
277 mFlushingAudio = SHUTTING_DOWN_DECODER;
278 } else {
279 mFlushingVideo = SHUTTING_DOWN_DECODER;
280 }
Andreas Hubera1587462010-12-15 15:17:42 -0800281 }
Andreas Huber41c3f742010-12-21 10:22:33 -0800282
283 finishFlushIfPossible();
Andreas Huber687b32d2010-12-15 17:18:20 -0800284 } else if (what == ACodec::kWhatOutputFormatChanged) {
Andreas Huber7caa1302011-01-10 10:38:31 -0800285 if (audio) {
286 int32_t numChannels;
287 CHECK(codecRequest->findInt32("channel-count", &numChannels));
Andreas Huber687b32d2010-12-15 17:18:20 -0800288
Andreas Huber7caa1302011-01-10 10:38:31 -0800289 int32_t sampleRate;
290 CHECK(codecRequest->findInt32("sample-rate", &sampleRate));
Andreas Huber687b32d2010-12-15 17:18:20 -0800291
Andreas Huber7caa1302011-01-10 10:38:31 -0800292 LOGV("Audio output format changed to %d Hz, %d channels",
293 sampleRate, numChannels);
Andreas Huber687b32d2010-12-15 17:18:20 -0800294
Andreas Huber7caa1302011-01-10 10:38:31 -0800295 mAudioSink->close();
296 CHECK_EQ(mAudioSink->open(sampleRate, numChannels), (status_t)OK);
297 mAudioSink->start();
Andreas Huber687b32d2010-12-15 17:18:20 -0800298
Andreas Huber7caa1302011-01-10 10:38:31 -0800299 mRenderer->signalAudioSinkChanged();
300 } else {
301 // video
Andreas Huber41c3f742010-12-21 10:22:33 -0800302
Andreas Huber7caa1302011-01-10 10:38:31 -0800303 int32_t width, height;
304 CHECK(codecRequest->findInt32("width", &width));
305 CHECK(codecRequest->findInt32("height", &height));
306
307 int32_t cropLeft, cropTop, cropRight, cropBottom;
308 CHECK(codecRequest->findRect(
309 "crop",
310 &cropLeft, &cropTop, &cropRight, &cropBottom));
311
312 LOGV("Video output format changed to %d x %d "
313 "(crop: %d, %d, %d, %d)",
314 width, height,
315 cropLeft, cropTop, cropRight, cropBottom);
316
317 notifyListener(
318 MEDIA_SET_VIDEO_SIZE,
319 cropRight - cropLeft + 1,
320 cropBottom - cropTop + 1);
321 }
Andreas Huber41c3f742010-12-21 10:22:33 -0800322 } else if (what == ACodec::kWhatShutdownCompleted) {
Andreas Hubercbeaca72011-01-04 14:01:29 -0800323 LOGV("%s shutdown completed", audio ? "audio" : "video");
Andreas Huber41c3f742010-12-21 10:22:33 -0800324 if (audio) {
325 mAudioDecoder.clear();
326
327 CHECK_EQ((int)mFlushingAudio, (int)SHUTTING_DOWN_DECODER);
328 mFlushingAudio = SHUT_DOWN;
329 } else {
330 mVideoDecoder.clear();
331
332 CHECK_EQ((int)mFlushingVideo, (int)SHUTTING_DOWN_DECODER);
333 mFlushingVideo = SHUT_DOWN;
334 }
335
336 finishFlushIfPossible();
Andreas Hubera1587462010-12-15 15:17:42 -0800337 } else {
338 CHECK_EQ((int)what, (int)ACodec::kWhatDrainThisBuffer);
339
340 renderBuffer(audio, codecRequest);
341 }
342
343 break;
344 }
345
346 case kWhatRendererNotify:
347 {
348 int32_t what;
349 CHECK(msg->findInt32("what", &what));
350
351 if (what == Renderer::kWhatEOS) {
352 int32_t audio;
353 CHECK(msg->findInt32("audio", &audio));
354
355 if (audio) {
356 mAudioEOS = true;
357 } else {
358 mVideoEOS = true;
359 }
360
Andreas Hubercbeaca72011-01-04 14:01:29 -0800361 LOGV("reached %s EOS", audio ? "audio" : "video");
Andreas Hubera1587462010-12-15 15:17:42 -0800362
363 if ((mAudioEOS || mAudioDecoder == NULL)
364 && (mVideoEOS || mVideoDecoder == NULL)) {
365 notifyListener(MEDIA_PLAYBACK_COMPLETE, 0, 0);
366 }
Andreas Huber08e10cb2011-01-05 12:17:08 -0800367 } else if (what == Renderer::kWhatPosition) {
368 int64_t positionUs;
369 CHECK(msg->findInt64("positionUs", &positionUs));
370
371 if (mDriver != NULL) {
372 sp<NuPlayerDriver> driver = mDriver.promote();
373 if (driver != NULL) {
374 driver->notifyPosition(positionUs);
375 }
376 }
Andreas Hubera1587462010-12-15 15:17:42 -0800377 } else {
378 CHECK_EQ(what, (int32_t)Renderer::kWhatFlushComplete);
379
380 int32_t audio;
381 CHECK(msg->findInt32("audio", &audio));
382
Andreas Hubercbeaca72011-01-04 14:01:29 -0800383 LOGV("renderer %s flush completed.", audio ? "audio" : "video");
Andreas Hubera1587462010-12-15 15:17:42 -0800384 }
385 break;
386 }
387
388 case kWhatMoreDataQueued:
389 {
390 break;
391 }
392
Andreas Hubercbeaca72011-01-04 14:01:29 -0800393 case kWhatReset:
394 {
395 LOGV("kWhatReset");
396
397 if (mFlushingAudio != NONE || mFlushingVideo != NONE) {
398 // We're currently flushing, postpone the reset until that's
399 // completed.
400
401 LOGV("postponing reset");
402
403 mResetPostponed = true;
404 break;
405 }
406
407 if (mAudioDecoder == NULL && mVideoDecoder == NULL) {
408 finishReset();
409 break;
410 }
411
412 if (mAudioDecoder != NULL) {
413 flushDecoder(true /* audio */, true /* needShutdown */);
414 }
415
416 if (mVideoDecoder != NULL) {
417 flushDecoder(false /* audio */, true /* needShutdown */);
418 }
419
420 mResetInProgress = true;
421 break;
422 }
423
Andreas Huber08e10cb2011-01-05 12:17:08 -0800424 case kWhatSeek:
425 {
426 int64_t seekTimeUs;
427 CHECK(msg->findInt64("seekTimeUs", &seekTimeUs));
428
Andreas Huber847551c2011-01-05 16:24:27 -0800429 LOGV("kWhatSeek seekTimeUs=%lld us (%.2f secs)",
Andreas Huber08e10cb2011-01-05 12:17:08 -0800430 seekTimeUs, seekTimeUs / 1E6);
431
432 mSource->seekTo(seekTimeUs);
433
434 if (mDriver != NULL) {
435 sp<NuPlayerDriver> driver = mDriver.promote();
436 if (driver != NULL) {
437 driver->notifySeekComplete();
438 }
439 }
440
441 break;
442 }
443
Andreas Huber601fe0e2011-01-20 15:23:04 -0800444 case kWhatPause:
445 {
446 CHECK(mRenderer != NULL);
447 mRenderer->pause();
448 break;
449 }
450
451 case kWhatResume:
452 {
453 CHECK(mRenderer != NULL);
454 mRenderer->resume();
455 break;
456 }
457
Andreas Hubera1587462010-12-15 15:17:42 -0800458 default:
459 TRESPASS();
460 break;
461 }
462}
463
Andreas Huber41c3f742010-12-21 10:22:33 -0800464void NuPlayer::finishFlushIfPossible() {
465 if (mFlushingAudio != FLUSHED && mFlushingAudio != SHUT_DOWN) {
466 return;
467 }
468
469 if (mFlushingVideo != FLUSHED && mFlushingVideo != SHUT_DOWN) {
470 return;
471 }
472
Andreas Hubercbeaca72011-01-04 14:01:29 -0800473 LOGV("both audio and video are flushed now.");
Andreas Huber41c3f742010-12-21 10:22:33 -0800474
475 mRenderer->signalTimeDiscontinuity();
476
Andreas Huber847551c2011-01-05 16:24:27 -0800477 if (mAudioDecoder != NULL) {
Andreas Huber41c3f742010-12-21 10:22:33 -0800478 mAudioDecoder->signalResume();
479 }
480
Andreas Huber847551c2011-01-05 16:24:27 -0800481 if (mVideoDecoder != NULL) {
Andreas Huber41c3f742010-12-21 10:22:33 -0800482 mVideoDecoder->signalResume();
483 }
484
485 mFlushingAudio = NONE;
486 mFlushingVideo = NONE;
Andreas Huber41c3f742010-12-21 10:22:33 -0800487
Andreas Hubercbeaca72011-01-04 14:01:29 -0800488 if (mResetInProgress) {
489 LOGV("reset completed");
490
491 mResetInProgress = false;
492 finishReset();
493 } else if (mResetPostponed) {
494 (new AMessage(kWhatReset, id()))->post();
495 mResetPostponed = false;
Andreas Huber847551c2011-01-05 16:24:27 -0800496 } else if (mAudioDecoder == NULL || mVideoDecoder == NULL) {
Andreas Hubercbeaca72011-01-04 14:01:29 -0800497 postScanSources();
Andreas Hubera1587462010-12-15 15:17:42 -0800498 }
499}
500
Andreas Hubercbeaca72011-01-04 14:01:29 -0800501void NuPlayer::finishReset() {
502 CHECK(mAudioDecoder == NULL);
503 CHECK(mVideoDecoder == NULL);
504
505 mRenderer.clear();
506 mSource.clear();
507
Andreas Huber08e10cb2011-01-05 12:17:08 -0800508 if (mDriver != NULL) {
509 sp<NuPlayerDriver> driver = mDriver.promote();
510 if (driver != NULL) {
511 driver->notifyResetComplete();
512 }
513 }
Andreas Hubercbeaca72011-01-04 14:01:29 -0800514}
515
516void NuPlayer::postScanSources() {
517 if (mScanSourcesPending) {
518 return;
519 }
520
521 sp<AMessage> msg = new AMessage(kWhatScanSources, id());
522 msg->setInt32("generation", mScanSourcesGeneration);
523 msg->post();
524
525 mScanSourcesPending = true;
526}
527
Andreas Huber54e66492010-12-23 10:27:40 -0800528status_t NuPlayer::instantiateDecoder(bool audio, sp<Decoder> *decoder) {
Andreas Hubera1587462010-12-15 15:17:42 -0800529 if (*decoder != NULL) {
530 return OK;
531 }
532
Andreas Huber54e66492010-12-23 10:27:40 -0800533 sp<MetaData> meta = mSource->getFormat(audio);
Andreas Hubera1587462010-12-15 15:17:42 -0800534
Andreas Huber54e66492010-12-23 10:27:40 -0800535 if (meta == NULL) {
Andreas Hubera1587462010-12-15 15:17:42 -0800536 return -EWOULDBLOCK;
537 }
538
539 sp<AMessage> notify =
540 new AMessage(audio ? kWhatAudioNotify : kWhatVideoNotify,
541 id());
542
Glenn Kastencc562a32011-02-08 17:26:17 -0800543 *decoder = audio ? new Decoder(notify) :
544 new Decoder(notify, mNativeWindow);
Andreas Hubera1587462010-12-15 15:17:42 -0800545 looper()->registerHandler(*decoder);
546
Andreas Huber54e66492010-12-23 10:27:40 -0800547 (*decoder)->configure(meta);
Andreas Hubera1587462010-12-15 15:17:42 -0800548
Andreas Huber08e10cb2011-01-05 12:17:08 -0800549 int64_t durationUs;
550 if (mDriver != NULL && mSource->getDuration(&durationUs) == OK) {
551 sp<NuPlayerDriver> driver = mDriver.promote();
552 if (driver != NULL) {
553 driver->notifyDuration(durationUs);
554 }
555 }
556
Andreas Hubera1587462010-12-15 15:17:42 -0800557 return OK;
558}
559
560status_t NuPlayer::feedDecoderInputData(bool audio, const sp<AMessage> &msg) {
561 sp<AMessage> reply;
562 CHECK(msg->findMessage("reply", &reply));
563
Andreas Huber222e6892010-12-22 10:03:04 -0800564 if ((audio && IsFlushingState(mFlushingAudio))
565 || (!audio && IsFlushingState(mFlushingVideo))) {
Andreas Hubera1587462010-12-15 15:17:42 -0800566 reply->setInt32("err", INFO_DISCONTINUITY);
567 reply->post();
568 return OK;
569 }
570
571 sp<ABuffer> accessUnit;
Andreas Huber54e66492010-12-23 10:27:40 -0800572 status_t err = mSource->dequeueAccessUnit(audio, &accessUnit);
Andreas Hubera1587462010-12-15 15:17:42 -0800573
574 if (err == -EWOULDBLOCK) {
575 return err;
576 } else if (err != OK) {
577 if (err == INFO_DISCONTINUITY) {
Andreas Huber54e66492010-12-23 10:27:40 -0800578 int32_t type;
579 CHECK(accessUnit->meta()->findInt32("discontinuity", &type));
580
581 bool formatChange =
582 type == ATSParser::DISCONTINUITY_FORMATCHANGE;
Andreas Huber222e6892010-12-22 10:03:04 -0800583
Andreas Hubercbeaca72011-01-04 14:01:29 -0800584 LOGV("%s discontinuity (formatChange=%d)",
Andreas Huber222e6892010-12-22 10:03:04 -0800585 audio ? "audio" : "video", formatChange);
586
Andreas Huber669ad132011-03-02 15:34:46 -0800587 if (audio) {
588 mSkipRenderingAudioUntilMediaTimeUs = -1;
589 } else {
590 mSkipRenderingVideoUntilMediaTimeUs = -1;
591 }
592
593 sp<AMessage> extra;
594 if (accessUnit->meta()->findMessage("extra", &extra)
595 && extra != NULL) {
596 int64_t resumeAtMediaTimeUs;
597 if (extra->findInt64(
598 "resume-at-mediatimeUs", &resumeAtMediaTimeUs)) {
599 LOGI("suppressing rendering of %s until %lld us",
600 audio ? "audio" : "video", resumeAtMediaTimeUs);
601
602 if (audio) {
603 mSkipRenderingAudioUntilMediaTimeUs =
604 resumeAtMediaTimeUs;
605 } else {
606 mSkipRenderingVideoUntilMediaTimeUs =
607 resumeAtMediaTimeUs;
608 }
609 }
610 }
611
Andreas Hubercbeaca72011-01-04 14:01:29 -0800612 flushDecoder(audio, formatChange);
Andreas Hubera1587462010-12-15 15:17:42 -0800613 }
614
615 reply->setInt32("err", err);
616 reply->post();
617 return OK;
618 }
619
Andreas Huber08e10cb2011-01-05 12:17:08 -0800620 // LOGV("returned a valid buffer of %s data", audio ? "audio" : "video");
Andreas Hubera1587462010-12-15 15:17:42 -0800621
622#if 0
623 int64_t mediaTimeUs;
624 CHECK(accessUnit->meta()->findInt64("timeUs", &mediaTimeUs));
Andreas Hubercbeaca72011-01-04 14:01:29 -0800625 LOGV("feeding %s input buffer at media time %.2f secs",
Andreas Hubera1587462010-12-15 15:17:42 -0800626 audio ? "audio" : "video",
627 mediaTimeUs / 1E6);
628#endif
629
630 reply->setObject("buffer", accessUnit);
631 reply->post();
632
633 return OK;
634}
635
636void NuPlayer::renderBuffer(bool audio, const sp<AMessage> &msg) {
Andreas Huber08e10cb2011-01-05 12:17:08 -0800637 // LOGV("renderBuffer %s", audio ? "audio" : "video");
Andreas Hubera1587462010-12-15 15:17:42 -0800638
639 sp<AMessage> reply;
640 CHECK(msg->findMessage("reply", &reply));
641
642 sp<RefBase> obj;
643 CHECK(msg->findObject("buffer", &obj));
644
645 sp<ABuffer> buffer = static_cast<ABuffer *>(obj.get());
646
Andreas Huber669ad132011-03-02 15:34:46 -0800647 int64_t &skipUntilMediaTimeUs =
648 audio
649 ? mSkipRenderingAudioUntilMediaTimeUs
650 : mSkipRenderingVideoUntilMediaTimeUs;
651
652 if (skipUntilMediaTimeUs >= 0) {
653 int64_t mediaTimeUs;
654 CHECK(buffer->meta()->findInt64("timeUs", &mediaTimeUs));
655
656 if (mediaTimeUs < skipUntilMediaTimeUs) {
657 LOGV("dropping %s buffer at time %lld as requested.",
658 audio ? "audio" : "video",
659 mediaTimeUs);
660
661 reply->post();
662 return;
663 }
664
665 skipUntilMediaTimeUs = -1;
666 }
667
Andreas Hubera1587462010-12-15 15:17:42 -0800668 mRenderer->queueBuffer(audio, buffer, reply);
669}
670
671void NuPlayer::notifyListener(int msg, int ext1, int ext2) {
Andreas Huber08e10cb2011-01-05 12:17:08 -0800672 if (mDriver == NULL) {
Andreas Hubera1587462010-12-15 15:17:42 -0800673 return;
674 }
675
Andreas Huber08e10cb2011-01-05 12:17:08 -0800676 sp<NuPlayerDriver> driver = mDriver.promote();
Andreas Hubera1587462010-12-15 15:17:42 -0800677
Andreas Huber08e10cb2011-01-05 12:17:08 -0800678 if (driver == NULL) {
Andreas Hubera1587462010-12-15 15:17:42 -0800679 return;
680 }
681
Andreas Huber08e10cb2011-01-05 12:17:08 -0800682 driver->sendEvent(msg, ext1, ext2);
Andreas Hubera1587462010-12-15 15:17:42 -0800683}
684
Andreas Hubercbeaca72011-01-04 14:01:29 -0800685void NuPlayer::flushDecoder(bool audio, bool needShutdown) {
686 // Make sure we don't continue to scan sources until we finish flushing.
687 ++mScanSourcesGeneration;
Andreas Huber08e10cb2011-01-05 12:17:08 -0800688 mScanSourcesPending = false;
Andreas Hubercbeaca72011-01-04 14:01:29 -0800689
690 (audio ? mAudioDecoder : mVideoDecoder)->signalFlush();
691 mRenderer->flush(audio);
692
693 FlushStatus newStatus =
694 needShutdown ? FLUSHING_DECODER_SHUTDOWN : FLUSHING_DECODER;
695
696 if (audio) {
697 CHECK(mFlushingAudio == NONE
698 || mFlushingAudio == AWAITING_DISCONTINUITY);
699
700 mFlushingAudio = newStatus;
701
702 if (mFlushingVideo == NONE) {
703 mFlushingVideo = (mVideoDecoder != NULL)
704 ? AWAITING_DISCONTINUITY
705 : FLUSHED;
706 }
707 } else {
708 CHECK(mFlushingVideo == NONE
709 || mFlushingVideo == AWAITING_DISCONTINUITY);
710
711 mFlushingVideo = newStatus;
712
713 if (mFlushingAudio == NONE) {
714 mFlushingAudio = (mAudioDecoder != NULL)
715 ? AWAITING_DISCONTINUITY
716 : FLUSHED;
717 }
718 }
719}
720
Andreas Hubera1587462010-12-15 15:17:42 -0800721} // namespace android