blob: 7f534c07115b9dfcffed8ea03eb454042879bcbf [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>
40
41namespace android {
42
43////////////////////////////////////////////////////////////////////////////////
44
45NuPlayer::NuPlayer()
Andreas Huber5bc087c2010-12-23 10:27:40 -080046 : mAudioEOS(false),
Andreas Huberf9334412010-12-15 15:17:42 -080047 mVideoEOS(false),
Andreas Huber5bc087c2010-12-23 10:27:40 -080048 mScanSourcesPending(false),
Andreas Huber1aef2112011-01-04 14:01:29 -080049 mScanSourcesGeneration(0),
Andreas Huberf9334412010-12-15 15:17:42 -080050 mFlushingAudio(NONE),
Andreas Huber1aef2112011-01-04 14:01:29 -080051 mFlushingVideo(NONE),
52 mResetInProgress(false),
53 mResetPostponed(false) {
Andreas Huberf9334412010-12-15 15:17:42 -080054}
55
56NuPlayer::~NuPlayer() {
57}
58
Andreas Huber43c3e6c2011-01-05 12:17:08 -080059void NuPlayer::setDriver(const wp<NuPlayerDriver> &driver) {
60 mDriver = driver;
Andreas Huberf9334412010-12-15 15:17:42 -080061}
62
63void NuPlayer::setDataSource(const sp<IStreamSource> &source) {
64 sp<AMessage> msg = new AMessage(kWhatSetDataSource, id());
65
Andreas Huber5bc087c2010-12-23 10:27:40 -080066 msg->setObject("source", new StreamingSource(source));
67 msg->post();
68}
Andreas Huberf9334412010-12-15 15:17:42 -080069
Andreas Huber5bc087c2010-12-23 10:27:40 -080070void NuPlayer::setDataSource(
71 const char *url, const KeyedVector<String8, String8> *headers) {
72 sp<AMessage> msg = new AMessage(kWhatSetDataSource, id());
73
74 msg->setObject("source", new HTTPLiveSource(url));
Andreas Huberf9334412010-12-15 15:17:42 -080075 msg->post();
76}
77
78void NuPlayer::setVideoSurface(const sp<Surface> &surface) {
79 sp<AMessage> msg = new AMessage(kWhatSetVideoSurface, id());
80 msg->setObject("surface", surface);
81 msg->post();
82}
83
84void NuPlayer::setAudioSink(const sp<MediaPlayerBase::AudioSink> &sink) {
85 sp<AMessage> msg = new AMessage(kWhatSetAudioSink, id());
86 msg->setObject("sink", sink);
87 msg->post();
88}
89
90void NuPlayer::start() {
91 (new AMessage(kWhatStart, id()))->post();
92}
93
Andreas Huber43c3e6c2011-01-05 12:17:08 -080094void NuPlayer::pause() {
95 // XXX to be implemented
96}
97
98void NuPlayer::resume() {
99 // XXX to be implemented
100}
101
Andreas Huber1aef2112011-01-04 14:01:29 -0800102void NuPlayer::resetAsync() {
103 (new AMessage(kWhatReset, id()))->post();
104}
105
Andreas Huber43c3e6c2011-01-05 12:17:08 -0800106void NuPlayer::seekToAsync(int64_t seekTimeUs) {
107 sp<AMessage> msg = new AMessage(kWhatSeek, id());
108 msg->setInt64("seekTimeUs", seekTimeUs);
109 msg->post();
110}
111
Andreas Huber53df1a42010-12-22 10:03:04 -0800112// static
Andreas Huber1aef2112011-01-04 14:01:29 -0800113bool NuPlayer::IsFlushingState(FlushStatus state, bool *needShutdown) {
Andreas Huber53df1a42010-12-22 10:03:04 -0800114 switch (state) {
115 case FLUSHING_DECODER:
Andreas Huber1aef2112011-01-04 14:01:29 -0800116 if (needShutdown != NULL) {
117 *needShutdown = false;
Andreas Huber53df1a42010-12-22 10:03:04 -0800118 }
119 return true;
120
Andreas Huber1aef2112011-01-04 14:01:29 -0800121 case FLUSHING_DECODER_SHUTDOWN:
122 if (needShutdown != NULL) {
123 *needShutdown = true;
Andreas Huber53df1a42010-12-22 10:03:04 -0800124 }
125 return true;
126
127 default:
128 return false;
129 }
130}
131
Andreas Huberf9334412010-12-15 15:17:42 -0800132void NuPlayer::onMessageReceived(const sp<AMessage> &msg) {
133 switch (msg->what()) {
134 case kWhatSetDataSource:
135 {
Andreas Huber1aef2112011-01-04 14:01:29 -0800136 LOGV("kWhatSetDataSource");
Andreas Huberf9334412010-12-15 15:17:42 -0800137
138 CHECK(mSource == NULL);
139
Andreas Huber5bc087c2010-12-23 10:27:40 -0800140 sp<RefBase> obj;
141 CHECK(msg->findObject("source", &obj));
Andreas Huberf9334412010-12-15 15:17:42 -0800142
Andreas Huber5bc087c2010-12-23 10:27:40 -0800143 mSource = static_cast<Source *>(obj.get());
Andreas Huberf9334412010-12-15 15:17:42 -0800144 break;
145 }
146
147 case kWhatSetVideoSurface:
148 {
Andreas Huber1aef2112011-01-04 14:01:29 -0800149 LOGV("kWhatSetVideoSurface");
Andreas Huberf9334412010-12-15 15:17:42 -0800150
151 sp<RefBase> obj;
152 CHECK(msg->findObject("surface", &obj));
153
154 mSurface = static_cast<Surface *>(obj.get());
155 break;
156 }
157
158 case kWhatSetAudioSink:
159 {
Andreas Huber1aef2112011-01-04 14:01:29 -0800160 LOGV("kWhatSetAudioSink");
Andreas Huberf9334412010-12-15 15:17:42 -0800161
162 sp<RefBase> obj;
163 CHECK(msg->findObject("sink", &obj));
164
165 mAudioSink = static_cast<MediaPlayerBase::AudioSink *>(obj.get());
166 break;
167 }
168
169 case kWhatStart:
170 {
Andreas Huber43c3e6c2011-01-05 12:17:08 -0800171 LOGV("kWhatStart");
172
Andreas Huber1aef2112011-01-04 14:01:29 -0800173 mAudioEOS = false;
174 mVideoEOS = false;
175
Andreas Huber5bc087c2010-12-23 10:27:40 -0800176 mSource->start();
Andreas Huberf9334412010-12-15 15:17:42 -0800177
178 mRenderer = new Renderer(
179 mAudioSink,
180 new AMessage(kWhatRendererNotify, id()));
181
182 looper()->registerHandler(mRenderer);
183
Andreas Huber1aef2112011-01-04 14:01:29 -0800184 postScanSources();
Andreas Huberf9334412010-12-15 15:17:42 -0800185 break;
186 }
187
188 case kWhatScanSources:
189 {
Andreas Huber1aef2112011-01-04 14:01:29 -0800190 int32_t generation;
191 CHECK(msg->findInt32("generation", &generation));
192 if (generation != mScanSourcesGeneration) {
193 // Drop obsolete msg.
194 break;
195 }
196
Andreas Huber5bc087c2010-12-23 10:27:40 -0800197 mScanSourcesPending = false;
198
Andreas Huber43c3e6c2011-01-05 12:17:08 -0800199 LOGV("scanning sources haveAudio=%d, haveVideo=%d",
200 mAudioDecoder != NULL, mVideoDecoder != NULL);
201
Andreas Huber5bc087c2010-12-23 10:27:40 -0800202 instantiateDecoder(false, &mVideoDecoder);
Andreas Huberf9334412010-12-15 15:17:42 -0800203
204 if (mAudioSink != NULL) {
Andreas Huber5bc087c2010-12-23 10:27:40 -0800205 instantiateDecoder(true, &mAudioDecoder);
Andreas Huberf9334412010-12-15 15:17:42 -0800206 }
207
Andreas Huber5bc087c2010-12-23 10:27:40 -0800208 if (!mSource->feedMoreTSData()) {
Andreas Huber1aef2112011-01-04 14:01:29 -0800209 if (mAudioDecoder == NULL && mVideoDecoder == NULL) {
210 // We're not currently decoding anything (no audio or
211 // video tracks found) and we just ran out of input data.
212 notifyListener(MEDIA_PLAYBACK_COMPLETE, 0, 0);
213 }
Andreas Huberf9334412010-12-15 15:17:42 -0800214 break;
215 }
216
Andreas Huberf9334412010-12-15 15:17:42 -0800217 if (mAudioDecoder == NULL || mVideoDecoder == NULL) {
218 msg->post(100000ll);
Andreas Huber5bc087c2010-12-23 10:27:40 -0800219 mScanSourcesPending = true;
Andreas Huberf9334412010-12-15 15:17:42 -0800220 }
221 break;
222 }
223
224 case kWhatVideoNotify:
225 case kWhatAudioNotify:
226 {
227 bool audio = msg->what() == kWhatAudioNotify;
228
229 sp<AMessage> codecRequest;
230 CHECK(msg->findMessage("codec-request", &codecRequest));
231
232 int32_t what;
233 CHECK(codecRequest->findInt32("what", &what));
234
235 if (what == ACodec::kWhatFillThisBuffer) {
236 status_t err = feedDecoderInputData(
237 audio, codecRequest);
238
Andreas Huber5bc087c2010-12-23 10:27:40 -0800239 if (err == -EWOULDBLOCK) {
240 if (mSource->feedMoreTSData()) {
241 msg->post();
242 }
Andreas Huberf9334412010-12-15 15:17:42 -0800243 }
244 } else if (what == ACodec::kWhatEOS) {
245 mRenderer->queueEOS(audio, ERROR_END_OF_STREAM);
246 } else if (what == ACodec::kWhatFlushCompleted) {
Andreas Huber1aef2112011-01-04 14:01:29 -0800247 bool needShutdown;
Andreas Huber53df1a42010-12-22 10:03:04 -0800248
Andreas Huberf9334412010-12-15 15:17:42 -0800249 if (audio) {
Andreas Huber1aef2112011-01-04 14:01:29 -0800250 CHECK(IsFlushingState(mFlushingAudio, &needShutdown));
Andreas Huberf9334412010-12-15 15:17:42 -0800251 mFlushingAudio = FLUSHED;
252 } else {
Andreas Huber1aef2112011-01-04 14:01:29 -0800253 CHECK(IsFlushingState(mFlushingVideo, &needShutdown));
Andreas Huberf9334412010-12-15 15:17:42 -0800254 mFlushingVideo = FLUSHED;
255 }
256
Andreas Huber1aef2112011-01-04 14:01:29 -0800257 LOGV("decoder %s flush completed", audio ? "audio" : "video");
Andreas Huberf9334412010-12-15 15:17:42 -0800258
Andreas Huber1aef2112011-01-04 14:01:29 -0800259 if (needShutdown) {
260 LOGV("initiating %s decoder shutdown",
Andreas Huber53df1a42010-12-22 10:03:04 -0800261 audio ? "audio" : "video");
Andreas Huberf9334412010-12-15 15:17:42 -0800262
Andreas Huber53df1a42010-12-22 10:03:04 -0800263 (audio ? mAudioDecoder : mVideoDecoder)->initiateShutdown();
Andreas Huberf9334412010-12-15 15:17:42 -0800264
Andreas Huber53df1a42010-12-22 10:03:04 -0800265 if (audio) {
266 mFlushingAudio = SHUTTING_DOWN_DECODER;
267 } else {
268 mFlushingVideo = SHUTTING_DOWN_DECODER;
269 }
Andreas Huberf9334412010-12-15 15:17:42 -0800270 }
Andreas Huber3831a062010-12-21 10:22:33 -0800271
272 finishFlushIfPossible();
Andreas Huber2c2814b2010-12-15 17:18:20 -0800273 } else if (what == ACodec::kWhatOutputFormatChanged) {
Andreas Huber31e25082011-01-10 10:38:31 -0800274 if (audio) {
275 int32_t numChannels;
276 CHECK(codecRequest->findInt32("channel-count", &numChannels));
Andreas Huber2c2814b2010-12-15 17:18:20 -0800277
Andreas Huber31e25082011-01-10 10:38:31 -0800278 int32_t sampleRate;
279 CHECK(codecRequest->findInt32("sample-rate", &sampleRate));
Andreas Huber2c2814b2010-12-15 17:18:20 -0800280
Andreas Huber31e25082011-01-10 10:38:31 -0800281 LOGV("Audio output format changed to %d Hz, %d channels",
282 sampleRate, numChannels);
Andreas Huber2c2814b2010-12-15 17:18:20 -0800283
Andreas Huber31e25082011-01-10 10:38:31 -0800284 mAudioSink->close();
285 CHECK_EQ(mAudioSink->open(sampleRate, numChannels), (status_t)OK);
286 mAudioSink->start();
Andreas Huber2c2814b2010-12-15 17:18:20 -0800287
Andreas Huber31e25082011-01-10 10:38:31 -0800288 mRenderer->signalAudioSinkChanged();
289 } else {
290 // video
Andreas Huber3831a062010-12-21 10:22:33 -0800291
Andreas Huber31e25082011-01-10 10:38:31 -0800292 int32_t width, height;
293 CHECK(codecRequest->findInt32("width", &width));
294 CHECK(codecRequest->findInt32("height", &height));
295
296 int32_t cropLeft, cropTop, cropRight, cropBottom;
297 CHECK(codecRequest->findRect(
298 "crop",
299 &cropLeft, &cropTop, &cropRight, &cropBottom));
300
301 LOGV("Video output format changed to %d x %d "
302 "(crop: %d, %d, %d, %d)",
303 width, height,
304 cropLeft, cropTop, cropRight, cropBottom);
305
306 notifyListener(
307 MEDIA_SET_VIDEO_SIZE,
308 cropRight - cropLeft + 1,
309 cropBottom - cropTop + 1);
310 }
Andreas Huber3831a062010-12-21 10:22:33 -0800311 } else if (what == ACodec::kWhatShutdownCompleted) {
Andreas Huber1aef2112011-01-04 14:01:29 -0800312 LOGV("%s shutdown completed", audio ? "audio" : "video");
Andreas Huber3831a062010-12-21 10:22:33 -0800313 if (audio) {
314 mAudioDecoder.clear();
315
316 CHECK_EQ((int)mFlushingAudio, (int)SHUTTING_DOWN_DECODER);
317 mFlushingAudio = SHUT_DOWN;
318 } else {
319 mVideoDecoder.clear();
320
321 CHECK_EQ((int)mFlushingVideo, (int)SHUTTING_DOWN_DECODER);
322 mFlushingVideo = SHUT_DOWN;
323 }
324
325 finishFlushIfPossible();
Andreas Huberf9334412010-12-15 15:17:42 -0800326 } else {
327 CHECK_EQ((int)what, (int)ACodec::kWhatDrainThisBuffer);
328
329 renderBuffer(audio, codecRequest);
330 }
331
332 break;
333 }
334
335 case kWhatRendererNotify:
336 {
337 int32_t what;
338 CHECK(msg->findInt32("what", &what));
339
340 if (what == Renderer::kWhatEOS) {
341 int32_t audio;
342 CHECK(msg->findInt32("audio", &audio));
343
344 if (audio) {
345 mAudioEOS = true;
346 } else {
347 mVideoEOS = true;
348 }
349
Andreas Huber1aef2112011-01-04 14:01:29 -0800350 LOGV("reached %s EOS", audio ? "audio" : "video");
Andreas Huberf9334412010-12-15 15:17:42 -0800351
352 if ((mAudioEOS || mAudioDecoder == NULL)
353 && (mVideoEOS || mVideoDecoder == NULL)) {
354 notifyListener(MEDIA_PLAYBACK_COMPLETE, 0, 0);
355 }
Andreas Huber43c3e6c2011-01-05 12:17:08 -0800356 } else if (what == Renderer::kWhatPosition) {
357 int64_t positionUs;
358 CHECK(msg->findInt64("positionUs", &positionUs));
359
360 if (mDriver != NULL) {
361 sp<NuPlayerDriver> driver = mDriver.promote();
362 if (driver != NULL) {
363 driver->notifyPosition(positionUs);
364 }
365 }
Andreas Huberf9334412010-12-15 15:17:42 -0800366 } else {
367 CHECK_EQ(what, (int32_t)Renderer::kWhatFlushComplete);
368
369 int32_t audio;
370 CHECK(msg->findInt32("audio", &audio));
371
Andreas Huber1aef2112011-01-04 14:01:29 -0800372 LOGV("renderer %s flush completed.", audio ? "audio" : "video");
Andreas Huberf9334412010-12-15 15:17:42 -0800373 }
374 break;
375 }
376
377 case kWhatMoreDataQueued:
378 {
379 break;
380 }
381
Andreas Huber1aef2112011-01-04 14:01:29 -0800382 case kWhatReset:
383 {
384 LOGV("kWhatReset");
385
386 if (mFlushingAudio != NONE || mFlushingVideo != NONE) {
387 // We're currently flushing, postpone the reset until that's
388 // completed.
389
390 LOGV("postponing reset");
391
392 mResetPostponed = true;
393 break;
394 }
395
396 if (mAudioDecoder == NULL && mVideoDecoder == NULL) {
397 finishReset();
398 break;
399 }
400
401 if (mAudioDecoder != NULL) {
402 flushDecoder(true /* audio */, true /* needShutdown */);
403 }
404
405 if (mVideoDecoder != NULL) {
406 flushDecoder(false /* audio */, true /* needShutdown */);
407 }
408
409 mResetInProgress = true;
410 break;
411 }
412
Andreas Huber43c3e6c2011-01-05 12:17:08 -0800413 case kWhatSeek:
414 {
415 int64_t seekTimeUs;
416 CHECK(msg->findInt64("seekTimeUs", &seekTimeUs));
417
Andreas Huber22fc52f2011-01-05 16:24:27 -0800418 LOGV("kWhatSeek seekTimeUs=%lld us (%.2f secs)",
Andreas Huber43c3e6c2011-01-05 12:17:08 -0800419 seekTimeUs, seekTimeUs / 1E6);
420
421 mSource->seekTo(seekTimeUs);
422
423 if (mDriver != NULL) {
424 sp<NuPlayerDriver> driver = mDriver.promote();
425 if (driver != NULL) {
426 driver->notifySeekComplete();
427 }
428 }
429
430 break;
431 }
432
Andreas Huberf9334412010-12-15 15:17:42 -0800433 default:
434 TRESPASS();
435 break;
436 }
437}
438
Andreas Huber3831a062010-12-21 10:22:33 -0800439void NuPlayer::finishFlushIfPossible() {
440 if (mFlushingAudio != FLUSHED && mFlushingAudio != SHUT_DOWN) {
441 return;
442 }
443
444 if (mFlushingVideo != FLUSHED && mFlushingVideo != SHUT_DOWN) {
445 return;
446 }
447
Andreas Huber1aef2112011-01-04 14:01:29 -0800448 LOGV("both audio and video are flushed now.");
Andreas Huber3831a062010-12-21 10:22:33 -0800449
450 mRenderer->signalTimeDiscontinuity();
451
Andreas Huber22fc52f2011-01-05 16:24:27 -0800452 if (mAudioDecoder != NULL) {
Andreas Huber3831a062010-12-21 10:22:33 -0800453 mAudioDecoder->signalResume();
454 }
455
Andreas Huber22fc52f2011-01-05 16:24:27 -0800456 if (mVideoDecoder != NULL) {
Andreas Huber3831a062010-12-21 10:22:33 -0800457 mVideoDecoder->signalResume();
458 }
459
460 mFlushingAudio = NONE;
461 mFlushingVideo = NONE;
Andreas Huber3831a062010-12-21 10:22:33 -0800462
Andreas Huber1aef2112011-01-04 14:01:29 -0800463 if (mResetInProgress) {
464 LOGV("reset completed");
465
466 mResetInProgress = false;
467 finishReset();
468 } else if (mResetPostponed) {
469 (new AMessage(kWhatReset, id()))->post();
470 mResetPostponed = false;
Andreas Huber22fc52f2011-01-05 16:24:27 -0800471 } else if (mAudioDecoder == NULL || mVideoDecoder == NULL) {
Andreas Huber1aef2112011-01-04 14:01:29 -0800472 postScanSources();
Andreas Huberf9334412010-12-15 15:17:42 -0800473 }
474}
475
Andreas Huber1aef2112011-01-04 14:01:29 -0800476void NuPlayer::finishReset() {
477 CHECK(mAudioDecoder == NULL);
478 CHECK(mVideoDecoder == NULL);
479
480 mRenderer.clear();
481 mSource.clear();
482
Andreas Huber43c3e6c2011-01-05 12:17:08 -0800483 if (mDriver != NULL) {
484 sp<NuPlayerDriver> driver = mDriver.promote();
485 if (driver != NULL) {
486 driver->notifyResetComplete();
487 }
488 }
Andreas Huber1aef2112011-01-04 14:01:29 -0800489}
490
491void NuPlayer::postScanSources() {
492 if (mScanSourcesPending) {
493 return;
494 }
495
496 sp<AMessage> msg = new AMessage(kWhatScanSources, id());
497 msg->setInt32("generation", mScanSourcesGeneration);
498 msg->post();
499
500 mScanSourcesPending = true;
501}
502
Andreas Huber5bc087c2010-12-23 10:27:40 -0800503status_t NuPlayer::instantiateDecoder(bool audio, sp<Decoder> *decoder) {
Andreas Huberf9334412010-12-15 15:17:42 -0800504 if (*decoder != NULL) {
505 return OK;
506 }
507
Andreas Huber5bc087c2010-12-23 10:27:40 -0800508 sp<MetaData> meta = mSource->getFormat(audio);
Andreas Huberf9334412010-12-15 15:17:42 -0800509
Andreas Huber5bc087c2010-12-23 10:27:40 -0800510 if (meta == NULL) {
Andreas Huberf9334412010-12-15 15:17:42 -0800511 return -EWOULDBLOCK;
512 }
513
514 sp<AMessage> notify =
515 new AMessage(audio ? kWhatAudioNotify : kWhatVideoNotify,
516 id());
517
518 *decoder = new Decoder(notify, audio ? NULL : mSurface);
519 looper()->registerHandler(*decoder);
520
Andreas Huber5bc087c2010-12-23 10:27:40 -0800521 (*decoder)->configure(meta);
Andreas Huberf9334412010-12-15 15:17:42 -0800522
Andreas Huber43c3e6c2011-01-05 12:17:08 -0800523 int64_t durationUs;
524 if (mDriver != NULL && mSource->getDuration(&durationUs) == OK) {
525 sp<NuPlayerDriver> driver = mDriver.promote();
526 if (driver != NULL) {
527 driver->notifyDuration(durationUs);
528 }
529 }
530
Andreas Huberf9334412010-12-15 15:17:42 -0800531 return OK;
532}
533
534status_t NuPlayer::feedDecoderInputData(bool audio, const sp<AMessage> &msg) {
535 sp<AMessage> reply;
536 CHECK(msg->findMessage("reply", &reply));
537
Andreas Huber53df1a42010-12-22 10:03:04 -0800538 if ((audio && IsFlushingState(mFlushingAudio))
539 || (!audio && IsFlushingState(mFlushingVideo))) {
Andreas Huberf9334412010-12-15 15:17:42 -0800540 reply->setInt32("err", INFO_DISCONTINUITY);
541 reply->post();
542 return OK;
543 }
544
545 sp<ABuffer> accessUnit;
Andreas Huber5bc087c2010-12-23 10:27:40 -0800546 status_t err = mSource->dequeueAccessUnit(audio, &accessUnit);
Andreas Huberf9334412010-12-15 15:17:42 -0800547
548 if (err == -EWOULDBLOCK) {
549 return err;
550 } else if (err != OK) {
551 if (err == INFO_DISCONTINUITY) {
Andreas Huber5bc087c2010-12-23 10:27:40 -0800552 int32_t type;
553 CHECK(accessUnit->meta()->findInt32("discontinuity", &type));
554
555 bool formatChange =
556 type == ATSParser::DISCONTINUITY_FORMATCHANGE;
Andreas Huber53df1a42010-12-22 10:03:04 -0800557
Andreas Huber1aef2112011-01-04 14:01:29 -0800558 LOGV("%s discontinuity (formatChange=%d)",
Andreas Huber53df1a42010-12-22 10:03:04 -0800559 audio ? "audio" : "video", formatChange);
560
Andreas Huber1aef2112011-01-04 14:01:29 -0800561 flushDecoder(audio, formatChange);
Andreas Huberf9334412010-12-15 15:17:42 -0800562 }
563
564 reply->setInt32("err", err);
565 reply->post();
566 return OK;
567 }
568
Andreas Huber43c3e6c2011-01-05 12:17:08 -0800569 // LOGV("returned a valid buffer of %s data", audio ? "audio" : "video");
Andreas Huberf9334412010-12-15 15:17:42 -0800570
571#if 0
572 int64_t mediaTimeUs;
573 CHECK(accessUnit->meta()->findInt64("timeUs", &mediaTimeUs));
Andreas Huber1aef2112011-01-04 14:01:29 -0800574 LOGV("feeding %s input buffer at media time %.2f secs",
Andreas Huberf9334412010-12-15 15:17:42 -0800575 audio ? "audio" : "video",
576 mediaTimeUs / 1E6);
577#endif
578
579 reply->setObject("buffer", accessUnit);
580 reply->post();
581
582 return OK;
583}
584
585void NuPlayer::renderBuffer(bool audio, const sp<AMessage> &msg) {
Andreas Huber43c3e6c2011-01-05 12:17:08 -0800586 // LOGV("renderBuffer %s", audio ? "audio" : "video");
Andreas Huberf9334412010-12-15 15:17:42 -0800587
588 sp<AMessage> reply;
589 CHECK(msg->findMessage("reply", &reply));
590
591 sp<RefBase> obj;
592 CHECK(msg->findObject("buffer", &obj));
593
594 sp<ABuffer> buffer = static_cast<ABuffer *>(obj.get());
595
596 mRenderer->queueBuffer(audio, buffer, reply);
597}
598
599void NuPlayer::notifyListener(int msg, int ext1, int ext2) {
Andreas Huber43c3e6c2011-01-05 12:17:08 -0800600 if (mDriver == NULL) {
Andreas Huberf9334412010-12-15 15:17:42 -0800601 return;
602 }
603
Andreas Huber43c3e6c2011-01-05 12:17:08 -0800604 sp<NuPlayerDriver> driver = mDriver.promote();
Andreas Huberf9334412010-12-15 15:17:42 -0800605
Andreas Huber43c3e6c2011-01-05 12:17:08 -0800606 if (driver == NULL) {
Andreas Huberf9334412010-12-15 15:17:42 -0800607 return;
608 }
609
Andreas Huber43c3e6c2011-01-05 12:17:08 -0800610 driver->sendEvent(msg, ext1, ext2);
Andreas Huberf9334412010-12-15 15:17:42 -0800611}
612
Andreas Huber1aef2112011-01-04 14:01:29 -0800613void NuPlayer::flushDecoder(bool audio, bool needShutdown) {
614 // Make sure we don't continue to scan sources until we finish flushing.
615 ++mScanSourcesGeneration;
Andreas Huber43c3e6c2011-01-05 12:17:08 -0800616 mScanSourcesPending = false;
Andreas Huber1aef2112011-01-04 14:01:29 -0800617
618 (audio ? mAudioDecoder : mVideoDecoder)->signalFlush();
619 mRenderer->flush(audio);
620
621 FlushStatus newStatus =
622 needShutdown ? FLUSHING_DECODER_SHUTDOWN : FLUSHING_DECODER;
623
624 if (audio) {
625 CHECK(mFlushingAudio == NONE
626 || mFlushingAudio == AWAITING_DISCONTINUITY);
627
628 mFlushingAudio = newStatus;
629
630 if (mFlushingVideo == NONE) {
631 mFlushingVideo = (mVideoDecoder != NULL)
632 ? AWAITING_DISCONTINUITY
633 : FLUSHED;
634 }
635 } else {
636 CHECK(mFlushingVideo == NONE
637 || mFlushingVideo == AWAITING_DISCONTINUITY);
638
639 mFlushingVideo = newStatus;
640
641 if (mFlushingAudio == NONE) {
642 mFlushingAudio = (mAudioDecoder != NULL)
643 ? AWAITING_DISCONTINUITY
644 : FLUSHED;
645 }
646 }
647}
648
Andreas Huberf9334412010-12-15 15:17:42 -0800649} // namespace android