blob: 7fb141a02124880bf59bf3c8602dc4f44d3ed58c [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 "
319 "(crop: %d, %d, %d, %d)",
320 width, height,
321 cropLeft, cropTop, cropRight, cropBottom);
322
323 notifyListener(
324 MEDIA_SET_VIDEO_SIZE,
325 cropRight - cropLeft + 1,
326 cropBottom - cropTop + 1);
327 }
Andreas Huber3831a062010-12-21 10:22:33 -0800328 } else if (what == ACodec::kWhatShutdownCompleted) {
Andreas Huber1aef2112011-01-04 14:01:29 -0800329 LOGV("%s shutdown completed", audio ? "audio" : "video");
Andreas Huber3831a062010-12-21 10:22:33 -0800330 if (audio) {
331 mAudioDecoder.clear();
332
333 CHECK_EQ((int)mFlushingAudio, (int)SHUTTING_DOWN_DECODER);
334 mFlushingAudio = SHUT_DOWN;
335 } else {
336 mVideoDecoder.clear();
337
338 CHECK_EQ((int)mFlushingVideo, (int)SHUTTING_DOWN_DECODER);
339 mFlushingVideo = SHUT_DOWN;
340 }
341
342 finishFlushIfPossible();
Andreas Huberc92fd242011-08-16 13:48:44 -0700343 } else if (what == ACodec::kWhatError) {
344 LOGE("Received error from %s decoder, aborting playback.",
345 audio ? "audio" : "video");
346
347 mRenderer->queueEOS(audio, UNKNOWN_ERROR);
Andreas Huberf9334412010-12-15 15:17:42 -0800348 } else {
349 CHECK_EQ((int)what, (int)ACodec::kWhatDrainThisBuffer);
350
351 renderBuffer(audio, codecRequest);
352 }
353
354 break;
355 }
356
357 case kWhatRendererNotify:
358 {
359 int32_t what;
360 CHECK(msg->findInt32("what", &what));
361
362 if (what == Renderer::kWhatEOS) {
363 int32_t audio;
364 CHECK(msg->findInt32("audio", &audio));
365
Andreas Huberc92fd242011-08-16 13:48:44 -0700366 int32_t finalResult;
367 CHECK(msg->findInt32("finalResult", &finalResult));
368
Andreas Huberf9334412010-12-15 15:17:42 -0800369 if (audio) {
370 mAudioEOS = true;
371 } else {
372 mVideoEOS = true;
373 }
374
Andreas Huberc92fd242011-08-16 13:48:44 -0700375 if (finalResult == ERROR_END_OF_STREAM) {
376 LOGV("reached %s EOS", audio ? "audio" : "video");
377 } else {
378 LOGE("%s track encountered an error (0x%08x)",
379 audio ? "audio" : "video", finalResult);
380
381 notifyListener(
382 MEDIA_ERROR, MEDIA_ERROR_UNKNOWN, finalResult);
383 }
Andreas Huberf9334412010-12-15 15:17:42 -0800384
385 if ((mAudioEOS || mAudioDecoder == NULL)
386 && (mVideoEOS || mVideoDecoder == NULL)) {
387 notifyListener(MEDIA_PLAYBACK_COMPLETE, 0, 0);
388 }
Andreas Huber43c3e6c2011-01-05 12:17:08 -0800389 } else if (what == Renderer::kWhatPosition) {
390 int64_t positionUs;
391 CHECK(msg->findInt64("positionUs", &positionUs));
392
393 if (mDriver != NULL) {
394 sp<NuPlayerDriver> driver = mDriver.promote();
395 if (driver != NULL) {
396 driver->notifyPosition(positionUs);
397 }
398 }
Andreas Huberf9334412010-12-15 15:17:42 -0800399 } else {
400 CHECK_EQ(what, (int32_t)Renderer::kWhatFlushComplete);
401
402 int32_t audio;
403 CHECK(msg->findInt32("audio", &audio));
404
Andreas Huber1aef2112011-01-04 14:01:29 -0800405 LOGV("renderer %s flush completed.", audio ? "audio" : "video");
Andreas Huberf9334412010-12-15 15:17:42 -0800406 }
407 break;
408 }
409
410 case kWhatMoreDataQueued:
411 {
412 break;
413 }
414
Andreas Huber1aef2112011-01-04 14:01:29 -0800415 case kWhatReset:
416 {
417 LOGV("kWhatReset");
418
419 if (mFlushingAudio != NONE || mFlushingVideo != NONE) {
420 // We're currently flushing, postpone the reset until that's
421 // completed.
422
423 LOGV("postponing reset");
424
425 mResetPostponed = true;
426 break;
427 }
428
429 if (mAudioDecoder == NULL && mVideoDecoder == NULL) {
430 finishReset();
431 break;
432 }
433
434 if (mAudioDecoder != NULL) {
435 flushDecoder(true /* audio */, true /* needShutdown */);
436 }
437
438 if (mVideoDecoder != NULL) {
439 flushDecoder(false /* audio */, true /* needShutdown */);
440 }
441
442 mResetInProgress = true;
443 break;
444 }
445
Andreas Huber43c3e6c2011-01-05 12:17:08 -0800446 case kWhatSeek:
447 {
448 int64_t seekTimeUs;
449 CHECK(msg->findInt64("seekTimeUs", &seekTimeUs));
450
Andreas Huber22fc52f2011-01-05 16:24:27 -0800451 LOGV("kWhatSeek seekTimeUs=%lld us (%.2f secs)",
Andreas Huber43c3e6c2011-01-05 12:17:08 -0800452 seekTimeUs, seekTimeUs / 1E6);
453
454 mSource->seekTo(seekTimeUs);
455
456 if (mDriver != NULL) {
457 sp<NuPlayerDriver> driver = mDriver.promote();
458 if (driver != NULL) {
459 driver->notifySeekComplete();
460 }
461 }
462
463 break;
464 }
465
Andreas Huberb4082222011-01-20 15:23:04 -0800466 case kWhatPause:
467 {
468 CHECK(mRenderer != NULL);
469 mRenderer->pause();
470 break;
471 }
472
473 case kWhatResume:
474 {
475 CHECK(mRenderer != NULL);
476 mRenderer->resume();
477 break;
478 }
479
Andreas Huberf9334412010-12-15 15:17:42 -0800480 default:
481 TRESPASS();
482 break;
483 }
484}
485
Andreas Huber3831a062010-12-21 10:22:33 -0800486void NuPlayer::finishFlushIfPossible() {
487 if (mFlushingAudio != FLUSHED && mFlushingAudio != SHUT_DOWN) {
488 return;
489 }
490
491 if (mFlushingVideo != FLUSHED && mFlushingVideo != SHUT_DOWN) {
492 return;
493 }
494
Andreas Huber1aef2112011-01-04 14:01:29 -0800495 LOGV("both audio and video are flushed now.");
Andreas Huber3831a062010-12-21 10:22:33 -0800496
497 mRenderer->signalTimeDiscontinuity();
498
Andreas Huber22fc52f2011-01-05 16:24:27 -0800499 if (mAudioDecoder != NULL) {
Andreas Huber3831a062010-12-21 10:22:33 -0800500 mAudioDecoder->signalResume();
501 }
502
Andreas Huber22fc52f2011-01-05 16:24:27 -0800503 if (mVideoDecoder != NULL) {
Andreas Huber3831a062010-12-21 10:22:33 -0800504 mVideoDecoder->signalResume();
505 }
506
507 mFlushingAudio = NONE;
508 mFlushingVideo = NONE;
Andreas Huber3831a062010-12-21 10:22:33 -0800509
Andreas Huber1aef2112011-01-04 14:01:29 -0800510 if (mResetInProgress) {
511 LOGV("reset completed");
512
513 mResetInProgress = false;
514 finishReset();
515 } else if (mResetPostponed) {
516 (new AMessage(kWhatReset, id()))->post();
517 mResetPostponed = false;
Andreas Huber22fc52f2011-01-05 16:24:27 -0800518 } else if (mAudioDecoder == NULL || mVideoDecoder == NULL) {
Andreas Huber1aef2112011-01-04 14:01:29 -0800519 postScanSources();
Andreas Huberf9334412010-12-15 15:17:42 -0800520 }
521}
522
Andreas Huber1aef2112011-01-04 14:01:29 -0800523void NuPlayer::finishReset() {
524 CHECK(mAudioDecoder == NULL);
525 CHECK(mVideoDecoder == NULL);
526
527 mRenderer.clear();
528 mSource.clear();
529
Andreas Huber43c3e6c2011-01-05 12:17:08 -0800530 if (mDriver != NULL) {
531 sp<NuPlayerDriver> driver = mDriver.promote();
532 if (driver != NULL) {
533 driver->notifyResetComplete();
534 }
535 }
Andreas Huber1aef2112011-01-04 14:01:29 -0800536}
537
538void NuPlayer::postScanSources() {
539 if (mScanSourcesPending) {
540 return;
541 }
542
543 sp<AMessage> msg = new AMessage(kWhatScanSources, id());
544 msg->setInt32("generation", mScanSourcesGeneration);
545 msg->post();
546
547 mScanSourcesPending = true;
548}
549
Andreas Huber5bc087c2010-12-23 10:27:40 -0800550status_t NuPlayer::instantiateDecoder(bool audio, sp<Decoder> *decoder) {
Andreas Huberf9334412010-12-15 15:17:42 -0800551 if (*decoder != NULL) {
552 return OK;
553 }
554
Andreas Huber5bc087c2010-12-23 10:27:40 -0800555 sp<MetaData> meta = mSource->getFormat(audio);
Andreas Huberf9334412010-12-15 15:17:42 -0800556
Andreas Huber5bc087c2010-12-23 10:27:40 -0800557 if (meta == NULL) {
Andreas Huberf9334412010-12-15 15:17:42 -0800558 return -EWOULDBLOCK;
559 }
560
561 sp<AMessage> notify =
562 new AMessage(audio ? kWhatAudioNotify : kWhatVideoNotify,
563 id());
564
Glenn Kasten11731182011-02-08 17:26:17 -0800565 *decoder = audio ? new Decoder(notify) :
566 new Decoder(notify, mNativeWindow);
Andreas Huberf9334412010-12-15 15:17:42 -0800567 looper()->registerHandler(*decoder);
568
Andreas Huber5bc087c2010-12-23 10:27:40 -0800569 (*decoder)->configure(meta);
Andreas Huberf9334412010-12-15 15:17:42 -0800570
Andreas Huber43c3e6c2011-01-05 12:17:08 -0800571 int64_t durationUs;
572 if (mDriver != NULL && mSource->getDuration(&durationUs) == OK) {
573 sp<NuPlayerDriver> driver = mDriver.promote();
574 if (driver != NULL) {
575 driver->notifyDuration(durationUs);
576 }
577 }
578
Andreas Huberf9334412010-12-15 15:17:42 -0800579 return OK;
580}
581
582status_t NuPlayer::feedDecoderInputData(bool audio, const sp<AMessage> &msg) {
583 sp<AMessage> reply;
584 CHECK(msg->findMessage("reply", &reply));
585
Andreas Huber53df1a42010-12-22 10:03:04 -0800586 if ((audio && IsFlushingState(mFlushingAudio))
587 || (!audio && IsFlushingState(mFlushingVideo))) {
Andreas Huberf9334412010-12-15 15:17:42 -0800588 reply->setInt32("err", INFO_DISCONTINUITY);
589 reply->post();
590 return OK;
591 }
592
593 sp<ABuffer> accessUnit;
Andreas Huber5bc087c2010-12-23 10:27:40 -0800594 status_t err = mSource->dequeueAccessUnit(audio, &accessUnit);
Andreas Huberf9334412010-12-15 15:17:42 -0800595
596 if (err == -EWOULDBLOCK) {
597 return err;
598 } else if (err != OK) {
599 if (err == INFO_DISCONTINUITY) {
Andreas Huber5bc087c2010-12-23 10:27:40 -0800600 int32_t type;
601 CHECK(accessUnit->meta()->findInt32("discontinuity", &type));
602
603 bool formatChange =
604 type == ATSParser::DISCONTINUITY_FORMATCHANGE;
Andreas Huber53df1a42010-12-22 10:03:04 -0800605
Andreas Huber1aef2112011-01-04 14:01:29 -0800606 LOGV("%s discontinuity (formatChange=%d)",
Andreas Huber53df1a42010-12-22 10:03:04 -0800607 audio ? "audio" : "video", formatChange);
608
Andreas Huber32f3cef2011-03-02 15:34:46 -0800609 if (audio) {
610 mSkipRenderingAudioUntilMediaTimeUs = -1;
611 } else {
612 mSkipRenderingVideoUntilMediaTimeUs = -1;
613 }
614
615 sp<AMessage> extra;
616 if (accessUnit->meta()->findMessage("extra", &extra)
617 && extra != NULL) {
618 int64_t resumeAtMediaTimeUs;
619 if (extra->findInt64(
620 "resume-at-mediatimeUs", &resumeAtMediaTimeUs)) {
621 LOGI("suppressing rendering of %s until %lld us",
622 audio ? "audio" : "video", resumeAtMediaTimeUs);
623
624 if (audio) {
625 mSkipRenderingAudioUntilMediaTimeUs =
626 resumeAtMediaTimeUs;
627 } else {
628 mSkipRenderingVideoUntilMediaTimeUs =
629 resumeAtMediaTimeUs;
630 }
631 }
632 }
633
Andreas Huber1aef2112011-01-04 14:01:29 -0800634 flushDecoder(audio, formatChange);
Andreas Huberf9334412010-12-15 15:17:42 -0800635 }
636
637 reply->setInt32("err", err);
638 reply->post();
639 return OK;
640 }
641
Andreas Huber43c3e6c2011-01-05 12:17:08 -0800642 // LOGV("returned a valid buffer of %s data", audio ? "audio" : "video");
Andreas Huberf9334412010-12-15 15:17:42 -0800643
644#if 0
645 int64_t mediaTimeUs;
646 CHECK(accessUnit->meta()->findInt64("timeUs", &mediaTimeUs));
Andreas Huber1aef2112011-01-04 14:01:29 -0800647 LOGV("feeding %s input buffer at media time %.2f secs",
Andreas Huberf9334412010-12-15 15:17:42 -0800648 audio ? "audio" : "video",
649 mediaTimeUs / 1E6);
650#endif
651
652 reply->setObject("buffer", accessUnit);
653 reply->post();
654
655 return OK;
656}
657
658void NuPlayer::renderBuffer(bool audio, const sp<AMessage> &msg) {
Andreas Huber43c3e6c2011-01-05 12:17:08 -0800659 // LOGV("renderBuffer %s", audio ? "audio" : "video");
Andreas Huberf9334412010-12-15 15:17:42 -0800660
661 sp<AMessage> reply;
662 CHECK(msg->findMessage("reply", &reply));
663
664 sp<RefBase> obj;
665 CHECK(msg->findObject("buffer", &obj));
666
667 sp<ABuffer> buffer = static_cast<ABuffer *>(obj.get());
668
Andreas Huber32f3cef2011-03-02 15:34:46 -0800669 int64_t &skipUntilMediaTimeUs =
670 audio
671 ? mSkipRenderingAudioUntilMediaTimeUs
672 : mSkipRenderingVideoUntilMediaTimeUs;
673
674 if (skipUntilMediaTimeUs >= 0) {
675 int64_t mediaTimeUs;
676 CHECK(buffer->meta()->findInt64("timeUs", &mediaTimeUs));
677
678 if (mediaTimeUs < skipUntilMediaTimeUs) {
679 LOGV("dropping %s buffer at time %lld as requested.",
680 audio ? "audio" : "video",
681 mediaTimeUs);
682
683 reply->post();
684 return;
685 }
686
687 skipUntilMediaTimeUs = -1;
688 }
689
Andreas Huberf9334412010-12-15 15:17:42 -0800690 mRenderer->queueBuffer(audio, buffer, reply);
691}
692
693void NuPlayer::notifyListener(int msg, int ext1, int ext2) {
Andreas Huber43c3e6c2011-01-05 12:17:08 -0800694 if (mDriver == NULL) {
Andreas Huberf9334412010-12-15 15:17:42 -0800695 return;
696 }
697
Andreas Huber43c3e6c2011-01-05 12:17:08 -0800698 sp<NuPlayerDriver> driver = mDriver.promote();
Andreas Huberf9334412010-12-15 15:17:42 -0800699
Andreas Huber43c3e6c2011-01-05 12:17:08 -0800700 if (driver == NULL) {
Andreas Huberf9334412010-12-15 15:17:42 -0800701 return;
702 }
703
Andreas Huber43c3e6c2011-01-05 12:17:08 -0800704 driver->sendEvent(msg, ext1, ext2);
Andreas Huberf9334412010-12-15 15:17:42 -0800705}
706
Andreas Huber1aef2112011-01-04 14:01:29 -0800707void NuPlayer::flushDecoder(bool audio, bool needShutdown) {
708 // Make sure we don't continue to scan sources until we finish flushing.
709 ++mScanSourcesGeneration;
Andreas Huber43c3e6c2011-01-05 12:17:08 -0800710 mScanSourcesPending = false;
Andreas Huber1aef2112011-01-04 14:01:29 -0800711
712 (audio ? mAudioDecoder : mVideoDecoder)->signalFlush();
713 mRenderer->flush(audio);
714
715 FlushStatus newStatus =
716 needShutdown ? FLUSHING_DECODER_SHUTDOWN : FLUSHING_DECODER;
717
718 if (audio) {
719 CHECK(mFlushingAudio == NONE
720 || mFlushingAudio == AWAITING_DISCONTINUITY);
721
722 mFlushingAudio = newStatus;
723
724 if (mFlushingVideo == NONE) {
725 mFlushingVideo = (mVideoDecoder != NULL)
726 ? AWAITING_DISCONTINUITY
727 : FLUSHED;
728 }
729 } else {
730 CHECK(mFlushingVideo == NONE
731 || mFlushingVideo == AWAITING_DISCONTINUITY);
732
733 mFlushingVideo = newStatus;
734
735 if (mFlushingAudio == NONE) {
736 mFlushingAudio = (mAudioDecoder != NULL)
737 ? AWAITING_DISCONTINUITY
738 : FLUSHED;
739 }
740 }
741}
742
Andreas Huberf9334412010-12-15 15:17:42 -0800743} // namespace android