blob: 1f08a91b432887eec038b102a4c907f8b9dc8f9a [file] [log] [blame]
Andreas Huberf9334412010-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 Huber5bc087c2010-12-23 10:27:40 -080022
23#include "HTTPLiveSource.h"
Andreas Huberf9334412010-12-15 15:17:42 -080024#include "NuPlayerDecoder.h"
Andreas Huber43c3e6c2011-01-05 12:17:08 -080025#include "NuPlayerDriver.h"
Andreas Huberf9334412010-12-15 15:17:42 -080026#include "NuPlayerRenderer.h"
Andreas Huber5bc087c2010-12-23 10:27:40 -080027#include "NuPlayerSource.h"
28#include "StreamingSource.h"
29
30#include "ATSParser.h"
Andreas Huberf9334412010-12-15 15:17:42 -080031
Andreas Huber3831a062010-12-21 10:22:33 -080032#include <media/stagefright/foundation/hexdump.h>
Andreas Huberf9334412010-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 Kasten11731182011-02-08 17:26:17 -080040#include <gui/ISurfaceTexture.h>
Andreas Huberf9334412010-12-15 15:17:42 -080041
42namespace android {
43
44////////////////////////////////////////////////////////////////////////////////
45
46NuPlayer::NuPlayer()
Andreas Huber9b80c2b2011-06-30 15:47:02 -070047 : mUIDValid(false),
48 mAudioEOS(false),
Andreas Huberf9334412010-12-15 15:17:42 -080049 mVideoEOS(false),
Andreas Huber5bc087c2010-12-23 10:27:40 -080050 mScanSourcesPending(false),
Andreas Huber1aef2112011-01-04 14:01:29 -080051 mScanSourcesGeneration(0),
Andreas Huberf9334412010-12-15 15:17:42 -080052 mFlushingAudio(NONE),
Andreas Huber1aef2112011-01-04 14:01:29 -080053 mFlushingVideo(NONE),
54 mResetInProgress(false),
55 mResetPostponed(false) {
Andreas Huberf9334412010-12-15 15:17:42 -080056}
57
58NuPlayer::~NuPlayer() {
59}
60
Andreas Huber9b80c2b2011-06-30 15:47:02 -070061void NuPlayer::setUID(uid_t uid) {
62 mUIDValid = true;
63 mUID = uid;
64}
65
Andreas Huber43c3e6c2011-01-05 12:17:08 -080066void NuPlayer::setDriver(const wp<NuPlayerDriver> &driver) {
67 mDriver = driver;
Andreas Huberf9334412010-12-15 15:17:42 -080068}
69
70void NuPlayer::setDataSource(const sp<IStreamSource> &source) {
71 sp<AMessage> msg = new AMessage(kWhatSetDataSource, id());
72
Andreas Huber5bc087c2010-12-23 10:27:40 -080073 msg->setObject("source", new StreamingSource(source));
74 msg->post();
75}
Andreas Huberf9334412010-12-15 15:17:42 -080076
Andreas Huber5bc087c2010-12-23 10:27:40 -080077void NuPlayer::setDataSource(
78 const char *url, const KeyedVector<String8, String8> *headers) {
79 sp<AMessage> msg = new AMessage(kWhatSetDataSource, id());
80
Andreas Huber9b80c2b2011-06-30 15:47:02 -070081 msg->setObject("source", new HTTPLiveSource(url, headers, mUIDValid, mUID));
Andreas Huberf9334412010-12-15 15:17:42 -080082 msg->post();
83}
84
85void NuPlayer::setVideoSurface(const sp<Surface> &surface) {
Glenn Kasten11731182011-02-08 17:26:17 -080086 sp<AMessage> msg = new AMessage(kWhatSetVideoNativeWindow, id());
87 msg->setObject("native-window", new NativeWindowWrapper(surface));
88 msg->post();
89}
90
91void NuPlayer::setVideoSurfaceTexture(const sp<ISurfaceTexture> &surfaceTexture) {
92 sp<AMessage> msg = new AMessage(kWhatSetVideoNativeWindow, id());
93 sp<SurfaceTextureClient> surfaceTextureClient(surfaceTexture != NULL ?
94 new SurfaceTextureClient(surfaceTexture) : NULL);
95 msg->setObject("native-window", new NativeWindowWrapper(surfaceTextureClient));
Andreas Huberf9334412010-12-15 15:17:42 -080096 msg->post();
97}
98
99void NuPlayer::setAudioSink(const sp<MediaPlayerBase::AudioSink> &sink) {
100 sp<AMessage> msg = new AMessage(kWhatSetAudioSink, id());
101 msg->setObject("sink", sink);
102 msg->post();
103}
104
105void NuPlayer::start() {
106 (new AMessage(kWhatStart, id()))->post();
107}
108
Andreas Huber43c3e6c2011-01-05 12:17:08 -0800109void NuPlayer::pause() {
Andreas Huberb4082222011-01-20 15:23:04 -0800110 (new AMessage(kWhatPause, id()))->post();
Andreas Huber43c3e6c2011-01-05 12:17:08 -0800111}
112
113void NuPlayer::resume() {
Andreas Huberb4082222011-01-20 15:23:04 -0800114 (new AMessage(kWhatResume, id()))->post();
Andreas Huber43c3e6c2011-01-05 12:17:08 -0800115}
116
Andreas Huber1aef2112011-01-04 14:01:29 -0800117void NuPlayer::resetAsync() {
118 (new AMessage(kWhatReset, id()))->post();
119}
120
Andreas Huber43c3e6c2011-01-05 12:17:08 -0800121void NuPlayer::seekToAsync(int64_t seekTimeUs) {
122 sp<AMessage> msg = new AMessage(kWhatSeek, id());
123 msg->setInt64("seekTimeUs", seekTimeUs);
124 msg->post();
125}
126
Andreas Huber53df1a42010-12-22 10:03:04 -0800127// static
Andreas Huber1aef2112011-01-04 14:01:29 -0800128bool NuPlayer::IsFlushingState(FlushStatus state, bool *needShutdown) {
Andreas Huber53df1a42010-12-22 10:03:04 -0800129 switch (state) {
130 case FLUSHING_DECODER:
Andreas Huber1aef2112011-01-04 14:01:29 -0800131 if (needShutdown != NULL) {
132 *needShutdown = false;
Andreas Huber53df1a42010-12-22 10:03:04 -0800133 }
134 return true;
135
Andreas Huber1aef2112011-01-04 14:01:29 -0800136 case FLUSHING_DECODER_SHUTDOWN:
137 if (needShutdown != NULL) {
138 *needShutdown = true;
Andreas Huber53df1a42010-12-22 10:03:04 -0800139 }
140 return true;
141
142 default:
143 return false;
144 }
145}
146
Andreas Huberf9334412010-12-15 15:17:42 -0800147void NuPlayer::onMessageReceived(const sp<AMessage> &msg) {
148 switch (msg->what()) {
149 case kWhatSetDataSource:
150 {
Andreas Huber1aef2112011-01-04 14:01:29 -0800151 LOGV("kWhatSetDataSource");
Andreas Huberf9334412010-12-15 15:17:42 -0800152
153 CHECK(mSource == NULL);
154
Andreas Huber5bc087c2010-12-23 10:27:40 -0800155 sp<RefBase> obj;
156 CHECK(msg->findObject("source", &obj));
Andreas Huberf9334412010-12-15 15:17:42 -0800157
Andreas Huber5bc087c2010-12-23 10:27:40 -0800158 mSource = static_cast<Source *>(obj.get());
Andreas Huberf9334412010-12-15 15:17:42 -0800159 break;
160 }
161
Glenn Kasten11731182011-02-08 17:26:17 -0800162 case kWhatSetVideoNativeWindow:
Andreas Huberf9334412010-12-15 15:17:42 -0800163 {
Glenn Kasten11731182011-02-08 17:26:17 -0800164 LOGV("kWhatSetVideoNativeWindow");
Andreas Huberf9334412010-12-15 15:17:42 -0800165
166 sp<RefBase> obj;
Glenn Kasten11731182011-02-08 17:26:17 -0800167 CHECK(msg->findObject("native-window", &obj));
Andreas Huberf9334412010-12-15 15:17:42 -0800168
Glenn Kasten11731182011-02-08 17:26:17 -0800169 mNativeWindow = static_cast<NativeWindowWrapper *>(obj.get());
Andreas Huberf9334412010-12-15 15:17:42 -0800170 break;
171 }
172
173 case kWhatSetAudioSink:
174 {
Andreas Huber1aef2112011-01-04 14:01:29 -0800175 LOGV("kWhatSetAudioSink");
Andreas Huberf9334412010-12-15 15:17:42 -0800176
177 sp<RefBase> obj;
178 CHECK(msg->findObject("sink", &obj));
179
180 mAudioSink = static_cast<MediaPlayerBase::AudioSink *>(obj.get());
181 break;
182 }
183
184 case kWhatStart:
185 {
Andreas Huber43c3e6c2011-01-05 12:17:08 -0800186 LOGV("kWhatStart");
187
Andreas Huber1aef2112011-01-04 14:01:29 -0800188 mAudioEOS = false;
189 mVideoEOS = false;
Andreas Huber32f3cef2011-03-02 15:34:46 -0800190 mSkipRenderingAudioUntilMediaTimeUs = -1;
191 mSkipRenderingVideoUntilMediaTimeUs = -1;
Andreas Huber1aef2112011-01-04 14:01:29 -0800192
Andreas Huber5bc087c2010-12-23 10:27:40 -0800193 mSource->start();
Andreas Huberf9334412010-12-15 15:17:42 -0800194
195 mRenderer = new Renderer(
196 mAudioSink,
197 new AMessage(kWhatRendererNotify, id()));
198
199 looper()->registerHandler(mRenderer);
200
Andreas Huber1aef2112011-01-04 14:01:29 -0800201 postScanSources();
Andreas Huberf9334412010-12-15 15:17:42 -0800202 break;
203 }
204
205 case kWhatScanSources:
206 {
Andreas Huber1aef2112011-01-04 14:01:29 -0800207 int32_t generation;
208 CHECK(msg->findInt32("generation", &generation));
209 if (generation != mScanSourcesGeneration) {
210 // Drop obsolete msg.
211 break;
212 }
213
Andreas Huber5bc087c2010-12-23 10:27:40 -0800214 mScanSourcesPending = false;
215
Andreas Huber43c3e6c2011-01-05 12:17:08 -0800216 LOGV("scanning sources haveAudio=%d, haveVideo=%d",
217 mAudioDecoder != NULL, mVideoDecoder != NULL);
218
Andreas Huber5bc087c2010-12-23 10:27:40 -0800219 instantiateDecoder(false, &mVideoDecoder);
Andreas Huberf9334412010-12-15 15:17:42 -0800220
221 if (mAudioSink != NULL) {
Andreas Huber5bc087c2010-12-23 10:27:40 -0800222 instantiateDecoder(true, &mAudioDecoder);
Andreas Huberf9334412010-12-15 15:17:42 -0800223 }
224
Andreas Huber5bc087c2010-12-23 10:27:40 -0800225 if (!mSource->feedMoreTSData()) {
Andreas Huber1aef2112011-01-04 14:01:29 -0800226 if (mAudioDecoder == NULL && mVideoDecoder == NULL) {
227 // We're not currently decoding anything (no audio or
228 // video tracks found) and we just ran out of input data.
229 notifyListener(MEDIA_PLAYBACK_COMPLETE, 0, 0);
230 }
Andreas Huberf9334412010-12-15 15:17:42 -0800231 break;
232 }
233
Andreas Huberf9334412010-12-15 15:17:42 -0800234 if (mAudioDecoder == NULL || mVideoDecoder == NULL) {
235 msg->post(100000ll);
Andreas Huber5bc087c2010-12-23 10:27:40 -0800236 mScanSourcesPending = true;
Andreas Huberf9334412010-12-15 15:17:42 -0800237 }
238 break;
239 }
240
241 case kWhatVideoNotify:
242 case kWhatAudioNotify:
243 {
244 bool audio = msg->what() == kWhatAudioNotify;
245
246 sp<AMessage> codecRequest;
247 CHECK(msg->findMessage("codec-request", &codecRequest));
248
249 int32_t what;
250 CHECK(codecRequest->findInt32("what", &what));
251
252 if (what == ACodec::kWhatFillThisBuffer) {
253 status_t err = feedDecoderInputData(
254 audio, codecRequest);
255
Andreas Huber5bc087c2010-12-23 10:27:40 -0800256 if (err == -EWOULDBLOCK) {
257 if (mSource->feedMoreTSData()) {
258 msg->post();
259 }
Andreas Huberf9334412010-12-15 15:17:42 -0800260 }
261 } else if (what == ACodec::kWhatEOS) {
262 mRenderer->queueEOS(audio, ERROR_END_OF_STREAM);
263 } else if (what == ACodec::kWhatFlushCompleted) {
Andreas Huber1aef2112011-01-04 14:01:29 -0800264 bool needShutdown;
Andreas Huber53df1a42010-12-22 10:03:04 -0800265
Andreas Huberf9334412010-12-15 15:17:42 -0800266 if (audio) {
Andreas Huber1aef2112011-01-04 14:01:29 -0800267 CHECK(IsFlushingState(mFlushingAudio, &needShutdown));
Andreas Huberf9334412010-12-15 15:17:42 -0800268 mFlushingAudio = FLUSHED;
269 } else {
Andreas Huber1aef2112011-01-04 14:01:29 -0800270 CHECK(IsFlushingState(mFlushingVideo, &needShutdown));
Andreas Huberf9334412010-12-15 15:17:42 -0800271 mFlushingVideo = FLUSHED;
272 }
273
Andreas Huber1aef2112011-01-04 14:01:29 -0800274 LOGV("decoder %s flush completed", audio ? "audio" : "video");
Andreas Huberf9334412010-12-15 15:17:42 -0800275
Andreas Huber1aef2112011-01-04 14:01:29 -0800276 if (needShutdown) {
277 LOGV("initiating %s decoder shutdown",
Andreas Huber53df1a42010-12-22 10:03:04 -0800278 audio ? "audio" : "video");
Andreas Huberf9334412010-12-15 15:17:42 -0800279
Andreas Huber53df1a42010-12-22 10:03:04 -0800280 (audio ? mAudioDecoder : mVideoDecoder)->initiateShutdown();
Andreas Huberf9334412010-12-15 15:17:42 -0800281
Andreas Huber53df1a42010-12-22 10:03:04 -0800282 if (audio) {
283 mFlushingAudio = SHUTTING_DOWN_DECODER;
284 } else {
285 mFlushingVideo = SHUTTING_DOWN_DECODER;
286 }
Andreas Huberf9334412010-12-15 15:17:42 -0800287 }
Andreas Huber3831a062010-12-21 10:22:33 -0800288
289 finishFlushIfPossible();
Andreas Huber2c2814b2010-12-15 17:18:20 -0800290 } else if (what == ACodec::kWhatOutputFormatChanged) {
Andreas Huber31e25082011-01-10 10:38:31 -0800291 if (audio) {
292 int32_t numChannels;
293 CHECK(codecRequest->findInt32("channel-count", &numChannels));
Andreas Huber2c2814b2010-12-15 17:18:20 -0800294
Andreas Huber31e25082011-01-10 10:38:31 -0800295 int32_t sampleRate;
296 CHECK(codecRequest->findInt32("sample-rate", &sampleRate));
Andreas Huber2c2814b2010-12-15 17:18:20 -0800297
Andreas Huber31e25082011-01-10 10:38:31 -0800298 LOGV("Audio output format changed to %d Hz, %d channels",
299 sampleRate, numChannels);
Andreas Huber2c2814b2010-12-15 17:18:20 -0800300
Andreas Huber31e25082011-01-10 10:38:31 -0800301 mAudioSink->close();
302 CHECK_EQ(mAudioSink->open(sampleRate, numChannels), (status_t)OK);
303 mAudioSink->start();
Andreas Huber2c2814b2010-12-15 17:18:20 -0800304
Andreas Huber31e25082011-01-10 10:38:31 -0800305 mRenderer->signalAudioSinkChanged();
306 } else {
307 // video
Andreas Huber3831a062010-12-21 10:22:33 -0800308
Andreas Huber31e25082011-01-10 10:38:31 -0800309 int32_t width, height;
310 CHECK(codecRequest->findInt32("width", &width));
311 CHECK(codecRequest->findInt32("height", &height));
312
313 int32_t cropLeft, cropTop, cropRight, cropBottom;
314 CHECK(codecRequest->findRect(
315 "crop",
316 &cropLeft, &cropTop, &cropRight, &cropBottom));
317
318 LOGV("Video output format changed to %d x %d "
Andreas Hubercb67cd12011-08-26 16:02:19 -0700319 "(crop: %d x %d @ (%d, %d))",
Andreas Huber31e25082011-01-10 10:38:31 -0800320 width, height,
Andreas Hubercb67cd12011-08-26 16:02:19 -0700321 (cropRight - cropLeft + 1),
322 (cropBottom - cropTop + 1),
323 cropLeft, cropTop);
Andreas Huber31e25082011-01-10 10:38:31 -0800324
325 notifyListener(
326 MEDIA_SET_VIDEO_SIZE,
327 cropRight - cropLeft + 1,
328 cropBottom - cropTop + 1);
329 }
Andreas Huber3831a062010-12-21 10:22:33 -0800330 } else if (what == ACodec::kWhatShutdownCompleted) {
Andreas Huber1aef2112011-01-04 14:01:29 -0800331 LOGV("%s shutdown completed", audio ? "audio" : "video");
Andreas Huber3831a062010-12-21 10:22:33 -0800332 if (audio) {
333 mAudioDecoder.clear();
334
335 CHECK_EQ((int)mFlushingAudio, (int)SHUTTING_DOWN_DECODER);
336 mFlushingAudio = SHUT_DOWN;
337 } else {
338 mVideoDecoder.clear();
339
340 CHECK_EQ((int)mFlushingVideo, (int)SHUTTING_DOWN_DECODER);
341 mFlushingVideo = SHUT_DOWN;
342 }
343
344 finishFlushIfPossible();
Andreas Huberc92fd242011-08-16 13:48:44 -0700345 } else if (what == ACodec::kWhatError) {
346 LOGE("Received error from %s decoder, aborting playback.",
347 audio ? "audio" : "video");
348
349 mRenderer->queueEOS(audio, UNKNOWN_ERROR);
Andreas Huberf9334412010-12-15 15:17:42 -0800350 } else {
351 CHECK_EQ((int)what, (int)ACodec::kWhatDrainThisBuffer);
352
353 renderBuffer(audio, codecRequest);
354 }
355
356 break;
357 }
358
359 case kWhatRendererNotify:
360 {
361 int32_t what;
362 CHECK(msg->findInt32("what", &what));
363
364 if (what == Renderer::kWhatEOS) {
365 int32_t audio;
366 CHECK(msg->findInt32("audio", &audio));
367
Andreas Huberc92fd242011-08-16 13:48:44 -0700368 int32_t finalResult;
369 CHECK(msg->findInt32("finalResult", &finalResult));
370
Andreas Huberf9334412010-12-15 15:17:42 -0800371 if (audio) {
372 mAudioEOS = true;
373 } else {
374 mVideoEOS = true;
375 }
376
Andreas Huberc92fd242011-08-16 13:48:44 -0700377 if (finalResult == ERROR_END_OF_STREAM) {
378 LOGV("reached %s EOS", audio ? "audio" : "video");
379 } else {
380 LOGE("%s track encountered an error (0x%08x)",
381 audio ? "audio" : "video", finalResult);
382
383 notifyListener(
384 MEDIA_ERROR, MEDIA_ERROR_UNKNOWN, finalResult);
385 }
Andreas Huberf9334412010-12-15 15:17:42 -0800386
387 if ((mAudioEOS || mAudioDecoder == NULL)
388 && (mVideoEOS || mVideoDecoder == NULL)) {
389 notifyListener(MEDIA_PLAYBACK_COMPLETE, 0, 0);
390 }
Andreas Huber43c3e6c2011-01-05 12:17:08 -0800391 } else if (what == Renderer::kWhatPosition) {
392 int64_t positionUs;
393 CHECK(msg->findInt64("positionUs", &positionUs));
394
395 if (mDriver != NULL) {
396 sp<NuPlayerDriver> driver = mDriver.promote();
397 if (driver != NULL) {
398 driver->notifyPosition(positionUs);
399 }
400 }
Andreas Huberf9334412010-12-15 15:17:42 -0800401 } else {
402 CHECK_EQ(what, (int32_t)Renderer::kWhatFlushComplete);
403
404 int32_t audio;
405 CHECK(msg->findInt32("audio", &audio));
406
Andreas Huber1aef2112011-01-04 14:01:29 -0800407 LOGV("renderer %s flush completed.", audio ? "audio" : "video");
Andreas Huberf9334412010-12-15 15:17:42 -0800408 }
409 break;
410 }
411
412 case kWhatMoreDataQueued:
413 {
414 break;
415 }
416
Andreas Huber1aef2112011-01-04 14:01:29 -0800417 case kWhatReset:
418 {
419 LOGV("kWhatReset");
420
421 if (mFlushingAudio != NONE || mFlushingVideo != NONE) {
422 // We're currently flushing, postpone the reset until that's
423 // completed.
424
425 LOGV("postponing reset");
426
427 mResetPostponed = true;
428 break;
429 }
430
431 if (mAudioDecoder == NULL && mVideoDecoder == NULL) {
432 finishReset();
433 break;
434 }
435
436 if (mAudioDecoder != NULL) {
437 flushDecoder(true /* audio */, true /* needShutdown */);
438 }
439
440 if (mVideoDecoder != NULL) {
441 flushDecoder(false /* audio */, true /* needShutdown */);
442 }
443
444 mResetInProgress = true;
445 break;
446 }
447
Andreas Huber43c3e6c2011-01-05 12:17:08 -0800448 case kWhatSeek:
449 {
450 int64_t seekTimeUs;
451 CHECK(msg->findInt64("seekTimeUs", &seekTimeUs));
452
Andreas Huber22fc52f2011-01-05 16:24:27 -0800453 LOGV("kWhatSeek seekTimeUs=%lld us (%.2f secs)",
Andreas Huber43c3e6c2011-01-05 12:17:08 -0800454 seekTimeUs, seekTimeUs / 1E6);
455
456 mSource->seekTo(seekTimeUs);
457
458 if (mDriver != NULL) {
459 sp<NuPlayerDriver> driver = mDriver.promote();
460 if (driver != NULL) {
461 driver->notifySeekComplete();
462 }
463 }
464
465 break;
466 }
467
Andreas Huberb4082222011-01-20 15:23:04 -0800468 case kWhatPause:
469 {
470 CHECK(mRenderer != NULL);
471 mRenderer->pause();
472 break;
473 }
474
475 case kWhatResume:
476 {
477 CHECK(mRenderer != NULL);
478 mRenderer->resume();
479 break;
480 }
481
Andreas Huberf9334412010-12-15 15:17:42 -0800482 default:
483 TRESPASS();
484 break;
485 }
486}
487
Andreas Huber3831a062010-12-21 10:22:33 -0800488void NuPlayer::finishFlushIfPossible() {
489 if (mFlushingAudio != FLUSHED && mFlushingAudio != SHUT_DOWN) {
490 return;
491 }
492
493 if (mFlushingVideo != FLUSHED && mFlushingVideo != SHUT_DOWN) {
494 return;
495 }
496
Andreas Huber1aef2112011-01-04 14:01:29 -0800497 LOGV("both audio and video are flushed now.");
Andreas Huber3831a062010-12-21 10:22:33 -0800498
499 mRenderer->signalTimeDiscontinuity();
500
Andreas Huber22fc52f2011-01-05 16:24:27 -0800501 if (mAudioDecoder != NULL) {
Andreas Huber3831a062010-12-21 10:22:33 -0800502 mAudioDecoder->signalResume();
503 }
504
Andreas Huber22fc52f2011-01-05 16:24:27 -0800505 if (mVideoDecoder != NULL) {
Andreas Huber3831a062010-12-21 10:22:33 -0800506 mVideoDecoder->signalResume();
507 }
508
509 mFlushingAudio = NONE;
510 mFlushingVideo = NONE;
Andreas Huber3831a062010-12-21 10:22:33 -0800511
Andreas Huber1aef2112011-01-04 14:01:29 -0800512 if (mResetInProgress) {
513 LOGV("reset completed");
514
515 mResetInProgress = false;
516 finishReset();
517 } else if (mResetPostponed) {
518 (new AMessage(kWhatReset, id()))->post();
519 mResetPostponed = false;
Andreas Huber22fc52f2011-01-05 16:24:27 -0800520 } else if (mAudioDecoder == NULL || mVideoDecoder == NULL) {
Andreas Huber1aef2112011-01-04 14:01:29 -0800521 postScanSources();
Andreas Huberf9334412010-12-15 15:17:42 -0800522 }
523}
524
Andreas Huber1aef2112011-01-04 14:01:29 -0800525void NuPlayer::finishReset() {
526 CHECK(mAudioDecoder == NULL);
527 CHECK(mVideoDecoder == NULL);
528
529 mRenderer.clear();
530 mSource.clear();
531
Andreas Huber43c3e6c2011-01-05 12:17:08 -0800532 if (mDriver != NULL) {
533 sp<NuPlayerDriver> driver = mDriver.promote();
534 if (driver != NULL) {
535 driver->notifyResetComplete();
536 }
537 }
Andreas Huber1aef2112011-01-04 14:01:29 -0800538}
539
540void NuPlayer::postScanSources() {
541 if (mScanSourcesPending) {
542 return;
543 }
544
545 sp<AMessage> msg = new AMessage(kWhatScanSources, id());
546 msg->setInt32("generation", mScanSourcesGeneration);
547 msg->post();
548
549 mScanSourcesPending = true;
550}
551
Andreas Huber5bc087c2010-12-23 10:27:40 -0800552status_t NuPlayer::instantiateDecoder(bool audio, sp<Decoder> *decoder) {
Andreas Huberf9334412010-12-15 15:17:42 -0800553 if (*decoder != NULL) {
554 return OK;
555 }
556
Andreas Huber5bc087c2010-12-23 10:27:40 -0800557 sp<MetaData> meta = mSource->getFormat(audio);
Andreas Huberf9334412010-12-15 15:17:42 -0800558
Andreas Huber5bc087c2010-12-23 10:27:40 -0800559 if (meta == NULL) {
Andreas Huberf9334412010-12-15 15:17:42 -0800560 return -EWOULDBLOCK;
561 }
562
563 sp<AMessage> notify =
564 new AMessage(audio ? kWhatAudioNotify : kWhatVideoNotify,
565 id());
566
Glenn Kasten11731182011-02-08 17:26:17 -0800567 *decoder = audio ? new Decoder(notify) :
568 new Decoder(notify, mNativeWindow);
Andreas Huberf9334412010-12-15 15:17:42 -0800569 looper()->registerHandler(*decoder);
570
Andreas Huber5bc087c2010-12-23 10:27:40 -0800571 (*decoder)->configure(meta);
Andreas Huberf9334412010-12-15 15:17:42 -0800572
Andreas Huber43c3e6c2011-01-05 12:17:08 -0800573 int64_t durationUs;
574 if (mDriver != NULL && mSource->getDuration(&durationUs) == OK) {
575 sp<NuPlayerDriver> driver = mDriver.promote();
576 if (driver != NULL) {
577 driver->notifyDuration(durationUs);
578 }
579 }
580
Andreas Huberf9334412010-12-15 15:17:42 -0800581 return OK;
582}
583
584status_t NuPlayer::feedDecoderInputData(bool audio, const sp<AMessage> &msg) {
585 sp<AMessage> reply;
586 CHECK(msg->findMessage("reply", &reply));
587
Andreas Huber53df1a42010-12-22 10:03:04 -0800588 if ((audio && IsFlushingState(mFlushingAudio))
589 || (!audio && IsFlushingState(mFlushingVideo))) {
Andreas Huberf9334412010-12-15 15:17:42 -0800590 reply->setInt32("err", INFO_DISCONTINUITY);
591 reply->post();
592 return OK;
593 }
594
595 sp<ABuffer> accessUnit;
Andreas Huber5bc087c2010-12-23 10:27:40 -0800596 status_t err = mSource->dequeueAccessUnit(audio, &accessUnit);
Andreas Huberf9334412010-12-15 15:17:42 -0800597
598 if (err == -EWOULDBLOCK) {
599 return err;
600 } else if (err != OK) {
601 if (err == INFO_DISCONTINUITY) {
Andreas Huber5bc087c2010-12-23 10:27:40 -0800602 int32_t type;
603 CHECK(accessUnit->meta()->findInt32("discontinuity", &type));
604
605 bool formatChange =
606 type == ATSParser::DISCONTINUITY_FORMATCHANGE;
Andreas Huber53df1a42010-12-22 10:03:04 -0800607
Andreas Huber1aef2112011-01-04 14:01:29 -0800608 LOGV("%s discontinuity (formatChange=%d)",
Andreas Huber53df1a42010-12-22 10:03:04 -0800609 audio ? "audio" : "video", formatChange);
610
Andreas Huber32f3cef2011-03-02 15:34:46 -0800611 if (audio) {
612 mSkipRenderingAudioUntilMediaTimeUs = -1;
613 } else {
614 mSkipRenderingVideoUntilMediaTimeUs = -1;
615 }
616
617 sp<AMessage> extra;
618 if (accessUnit->meta()->findMessage("extra", &extra)
619 && extra != NULL) {
620 int64_t resumeAtMediaTimeUs;
621 if (extra->findInt64(
622 "resume-at-mediatimeUs", &resumeAtMediaTimeUs)) {
623 LOGI("suppressing rendering of %s until %lld us",
624 audio ? "audio" : "video", resumeAtMediaTimeUs);
625
626 if (audio) {
627 mSkipRenderingAudioUntilMediaTimeUs =
628 resumeAtMediaTimeUs;
629 } else {
630 mSkipRenderingVideoUntilMediaTimeUs =
631 resumeAtMediaTimeUs;
632 }
633 }
634 }
635
Andreas Huber1aef2112011-01-04 14:01:29 -0800636 flushDecoder(audio, formatChange);
Andreas Huberf9334412010-12-15 15:17:42 -0800637 }
638
639 reply->setInt32("err", err);
640 reply->post();
641 return OK;
642 }
643
Andreas Huber43c3e6c2011-01-05 12:17:08 -0800644 // LOGV("returned a valid buffer of %s data", audio ? "audio" : "video");
Andreas Huberf9334412010-12-15 15:17:42 -0800645
646#if 0
647 int64_t mediaTimeUs;
648 CHECK(accessUnit->meta()->findInt64("timeUs", &mediaTimeUs));
Andreas Huber1aef2112011-01-04 14:01:29 -0800649 LOGV("feeding %s input buffer at media time %.2f secs",
Andreas Huberf9334412010-12-15 15:17:42 -0800650 audio ? "audio" : "video",
651 mediaTimeUs / 1E6);
652#endif
653
654 reply->setObject("buffer", accessUnit);
655 reply->post();
656
657 return OK;
658}
659
660void NuPlayer::renderBuffer(bool audio, const sp<AMessage> &msg) {
Andreas Huber43c3e6c2011-01-05 12:17:08 -0800661 // LOGV("renderBuffer %s", audio ? "audio" : "video");
Andreas Huberf9334412010-12-15 15:17:42 -0800662
663 sp<AMessage> reply;
664 CHECK(msg->findMessage("reply", &reply));
665
666 sp<RefBase> obj;
667 CHECK(msg->findObject("buffer", &obj));
668
669 sp<ABuffer> buffer = static_cast<ABuffer *>(obj.get());
670
Andreas Huber32f3cef2011-03-02 15:34:46 -0800671 int64_t &skipUntilMediaTimeUs =
672 audio
673 ? mSkipRenderingAudioUntilMediaTimeUs
674 : mSkipRenderingVideoUntilMediaTimeUs;
675
676 if (skipUntilMediaTimeUs >= 0) {
677 int64_t mediaTimeUs;
678 CHECK(buffer->meta()->findInt64("timeUs", &mediaTimeUs));
679
680 if (mediaTimeUs < skipUntilMediaTimeUs) {
681 LOGV("dropping %s buffer at time %lld as requested.",
682 audio ? "audio" : "video",
683 mediaTimeUs);
684
685 reply->post();
686 return;
687 }
688
689 skipUntilMediaTimeUs = -1;
690 }
691
Andreas Huberf9334412010-12-15 15:17:42 -0800692 mRenderer->queueBuffer(audio, buffer, reply);
693}
694
695void NuPlayer::notifyListener(int msg, int ext1, int ext2) {
Andreas Huber43c3e6c2011-01-05 12:17:08 -0800696 if (mDriver == NULL) {
Andreas Huberf9334412010-12-15 15:17:42 -0800697 return;
698 }
699
Andreas Huber43c3e6c2011-01-05 12:17:08 -0800700 sp<NuPlayerDriver> driver = mDriver.promote();
Andreas Huberf9334412010-12-15 15:17:42 -0800701
Andreas Huber43c3e6c2011-01-05 12:17:08 -0800702 if (driver == NULL) {
Andreas Huberf9334412010-12-15 15:17:42 -0800703 return;
704 }
705
Andreas Huber43c3e6c2011-01-05 12:17:08 -0800706 driver->sendEvent(msg, ext1, ext2);
Andreas Huberf9334412010-12-15 15:17:42 -0800707}
708
Andreas Huber1aef2112011-01-04 14:01:29 -0800709void NuPlayer::flushDecoder(bool audio, bool needShutdown) {
710 // Make sure we don't continue to scan sources until we finish flushing.
711 ++mScanSourcesGeneration;
Andreas Huber43c3e6c2011-01-05 12:17:08 -0800712 mScanSourcesPending = false;
Andreas Huber1aef2112011-01-04 14:01:29 -0800713
714 (audio ? mAudioDecoder : mVideoDecoder)->signalFlush();
715 mRenderer->flush(audio);
716
717 FlushStatus newStatus =
718 needShutdown ? FLUSHING_DECODER_SHUTDOWN : FLUSHING_DECODER;
719
720 if (audio) {
721 CHECK(mFlushingAudio == NONE
722 || mFlushingAudio == AWAITING_DISCONTINUITY);
723
724 mFlushingAudio = newStatus;
725
726 if (mFlushingVideo == NONE) {
727 mFlushingVideo = (mVideoDecoder != NULL)
728 ? AWAITING_DISCONTINUITY
729 : FLUSHED;
730 }
731 } else {
732 CHECK(mFlushingVideo == NONE
733 || mFlushingVideo == AWAITING_DISCONTINUITY);
734
735 mFlushingVideo = newStatus;
736
737 if (mFlushingAudio == NONE) {
738 mFlushingAudio = (mAudioDecoder != NULL)
739 ? AWAITING_DISCONTINUITY
740 : FLUSHED;
741 }
742 }
743}
744
Andreas Huberf9334412010-12-15 15:17:42 -0800745} // namespace android