blob: b06f20d55c80e5d55469993ded3c6fca43c9fbe6 [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 Huberf9334412010-12-15 15:17:42 -0800343 } else {
344 CHECK_EQ((int)what, (int)ACodec::kWhatDrainThisBuffer);
345
346 renderBuffer(audio, codecRequest);
347 }
348
349 break;
350 }
351
352 case kWhatRendererNotify:
353 {
354 int32_t what;
355 CHECK(msg->findInt32("what", &what));
356
357 if (what == Renderer::kWhatEOS) {
358 int32_t audio;
359 CHECK(msg->findInt32("audio", &audio));
360
361 if (audio) {
362 mAudioEOS = true;
363 } else {
364 mVideoEOS = true;
365 }
366
Andreas Huber1aef2112011-01-04 14:01:29 -0800367 LOGV("reached %s EOS", audio ? "audio" : "video");
Andreas Huberf9334412010-12-15 15:17:42 -0800368
369 if ((mAudioEOS || mAudioDecoder == NULL)
370 && (mVideoEOS || mVideoDecoder == NULL)) {
371 notifyListener(MEDIA_PLAYBACK_COMPLETE, 0, 0);
372 }
Andreas Huber43c3e6c2011-01-05 12:17:08 -0800373 } else if (what == Renderer::kWhatPosition) {
374 int64_t positionUs;
375 CHECK(msg->findInt64("positionUs", &positionUs));
376
377 if (mDriver != NULL) {
378 sp<NuPlayerDriver> driver = mDriver.promote();
379 if (driver != NULL) {
380 driver->notifyPosition(positionUs);
381 }
382 }
Andreas Huberf9334412010-12-15 15:17:42 -0800383 } else {
384 CHECK_EQ(what, (int32_t)Renderer::kWhatFlushComplete);
385
386 int32_t audio;
387 CHECK(msg->findInt32("audio", &audio));
388
Andreas Huber1aef2112011-01-04 14:01:29 -0800389 LOGV("renderer %s flush completed.", audio ? "audio" : "video");
Andreas Huberf9334412010-12-15 15:17:42 -0800390 }
391 break;
392 }
393
394 case kWhatMoreDataQueued:
395 {
396 break;
397 }
398
Andreas Huber1aef2112011-01-04 14:01:29 -0800399 case kWhatReset:
400 {
401 LOGV("kWhatReset");
402
403 if (mFlushingAudio != NONE || mFlushingVideo != NONE) {
404 // We're currently flushing, postpone the reset until that's
405 // completed.
406
407 LOGV("postponing reset");
408
409 mResetPostponed = true;
410 break;
411 }
412
413 if (mAudioDecoder == NULL && mVideoDecoder == NULL) {
414 finishReset();
415 break;
416 }
417
418 if (mAudioDecoder != NULL) {
419 flushDecoder(true /* audio */, true /* needShutdown */);
420 }
421
422 if (mVideoDecoder != NULL) {
423 flushDecoder(false /* audio */, true /* needShutdown */);
424 }
425
426 mResetInProgress = true;
427 break;
428 }
429
Andreas Huber43c3e6c2011-01-05 12:17:08 -0800430 case kWhatSeek:
431 {
432 int64_t seekTimeUs;
433 CHECK(msg->findInt64("seekTimeUs", &seekTimeUs));
434
Andreas Huber22fc52f2011-01-05 16:24:27 -0800435 LOGV("kWhatSeek seekTimeUs=%lld us (%.2f secs)",
Andreas Huber43c3e6c2011-01-05 12:17:08 -0800436 seekTimeUs, seekTimeUs / 1E6);
437
438 mSource->seekTo(seekTimeUs);
439
440 if (mDriver != NULL) {
441 sp<NuPlayerDriver> driver = mDriver.promote();
442 if (driver != NULL) {
443 driver->notifySeekComplete();
444 }
445 }
446
447 break;
448 }
449
Andreas Huberb4082222011-01-20 15:23:04 -0800450 case kWhatPause:
451 {
452 CHECK(mRenderer != NULL);
453 mRenderer->pause();
454 break;
455 }
456
457 case kWhatResume:
458 {
459 CHECK(mRenderer != NULL);
460 mRenderer->resume();
461 break;
462 }
463
Andreas Huberf9334412010-12-15 15:17:42 -0800464 default:
465 TRESPASS();
466 break;
467 }
468}
469
Andreas Huber3831a062010-12-21 10:22:33 -0800470void NuPlayer::finishFlushIfPossible() {
471 if (mFlushingAudio != FLUSHED && mFlushingAudio != SHUT_DOWN) {
472 return;
473 }
474
475 if (mFlushingVideo != FLUSHED && mFlushingVideo != SHUT_DOWN) {
476 return;
477 }
478
Andreas Huber1aef2112011-01-04 14:01:29 -0800479 LOGV("both audio and video are flushed now.");
Andreas Huber3831a062010-12-21 10:22:33 -0800480
481 mRenderer->signalTimeDiscontinuity();
482
Andreas Huber22fc52f2011-01-05 16:24:27 -0800483 if (mAudioDecoder != NULL) {
Andreas Huber3831a062010-12-21 10:22:33 -0800484 mAudioDecoder->signalResume();
485 }
486
Andreas Huber22fc52f2011-01-05 16:24:27 -0800487 if (mVideoDecoder != NULL) {
Andreas Huber3831a062010-12-21 10:22:33 -0800488 mVideoDecoder->signalResume();
489 }
490
491 mFlushingAudio = NONE;
492 mFlushingVideo = NONE;
Andreas Huber3831a062010-12-21 10:22:33 -0800493
Andreas Huber1aef2112011-01-04 14:01:29 -0800494 if (mResetInProgress) {
495 LOGV("reset completed");
496
497 mResetInProgress = false;
498 finishReset();
499 } else if (mResetPostponed) {
500 (new AMessage(kWhatReset, id()))->post();
501 mResetPostponed = false;
Andreas Huber22fc52f2011-01-05 16:24:27 -0800502 } else if (mAudioDecoder == NULL || mVideoDecoder == NULL) {
Andreas Huber1aef2112011-01-04 14:01:29 -0800503 postScanSources();
Andreas Huberf9334412010-12-15 15:17:42 -0800504 }
505}
506
Andreas Huber1aef2112011-01-04 14:01:29 -0800507void NuPlayer::finishReset() {
508 CHECK(mAudioDecoder == NULL);
509 CHECK(mVideoDecoder == NULL);
510
511 mRenderer.clear();
512 mSource.clear();
513
Andreas Huber43c3e6c2011-01-05 12:17:08 -0800514 if (mDriver != NULL) {
515 sp<NuPlayerDriver> driver = mDriver.promote();
516 if (driver != NULL) {
517 driver->notifyResetComplete();
518 }
519 }
Andreas Huber1aef2112011-01-04 14:01:29 -0800520}
521
522void NuPlayer::postScanSources() {
523 if (mScanSourcesPending) {
524 return;
525 }
526
527 sp<AMessage> msg = new AMessage(kWhatScanSources, id());
528 msg->setInt32("generation", mScanSourcesGeneration);
529 msg->post();
530
531 mScanSourcesPending = true;
532}
533
Andreas Huber5bc087c2010-12-23 10:27:40 -0800534status_t NuPlayer::instantiateDecoder(bool audio, sp<Decoder> *decoder) {
Andreas Huberf9334412010-12-15 15:17:42 -0800535 if (*decoder != NULL) {
536 return OK;
537 }
538
Andreas Huber5bc087c2010-12-23 10:27:40 -0800539 sp<MetaData> meta = mSource->getFormat(audio);
Andreas Huberf9334412010-12-15 15:17:42 -0800540
Andreas Huber5bc087c2010-12-23 10:27:40 -0800541 if (meta == NULL) {
Andreas Huberf9334412010-12-15 15:17:42 -0800542 return -EWOULDBLOCK;
543 }
544
545 sp<AMessage> notify =
546 new AMessage(audio ? kWhatAudioNotify : kWhatVideoNotify,
547 id());
548
Glenn Kasten11731182011-02-08 17:26:17 -0800549 *decoder = audio ? new Decoder(notify) :
550 new Decoder(notify, mNativeWindow);
Andreas Huberf9334412010-12-15 15:17:42 -0800551 looper()->registerHandler(*decoder);
552
Andreas Huber5bc087c2010-12-23 10:27:40 -0800553 (*decoder)->configure(meta);
Andreas Huberf9334412010-12-15 15:17:42 -0800554
Andreas Huber43c3e6c2011-01-05 12:17:08 -0800555 int64_t durationUs;
556 if (mDriver != NULL && mSource->getDuration(&durationUs) == OK) {
557 sp<NuPlayerDriver> driver = mDriver.promote();
558 if (driver != NULL) {
559 driver->notifyDuration(durationUs);
560 }
561 }
562
Andreas Huberf9334412010-12-15 15:17:42 -0800563 return OK;
564}
565
566status_t NuPlayer::feedDecoderInputData(bool audio, const sp<AMessage> &msg) {
567 sp<AMessage> reply;
568 CHECK(msg->findMessage("reply", &reply));
569
Andreas Huber53df1a42010-12-22 10:03:04 -0800570 if ((audio && IsFlushingState(mFlushingAudio))
571 || (!audio && IsFlushingState(mFlushingVideo))) {
Andreas Huberf9334412010-12-15 15:17:42 -0800572 reply->setInt32("err", INFO_DISCONTINUITY);
573 reply->post();
574 return OK;
575 }
576
577 sp<ABuffer> accessUnit;
Andreas Huber5bc087c2010-12-23 10:27:40 -0800578 status_t err = mSource->dequeueAccessUnit(audio, &accessUnit);
Andreas Huberf9334412010-12-15 15:17:42 -0800579
580 if (err == -EWOULDBLOCK) {
581 return err;
582 } else if (err != OK) {
583 if (err == INFO_DISCONTINUITY) {
Andreas Huber5bc087c2010-12-23 10:27:40 -0800584 int32_t type;
585 CHECK(accessUnit->meta()->findInt32("discontinuity", &type));
586
587 bool formatChange =
588 type == ATSParser::DISCONTINUITY_FORMATCHANGE;
Andreas Huber53df1a42010-12-22 10:03:04 -0800589
Andreas Huber1aef2112011-01-04 14:01:29 -0800590 LOGV("%s discontinuity (formatChange=%d)",
Andreas Huber53df1a42010-12-22 10:03:04 -0800591 audio ? "audio" : "video", formatChange);
592
Andreas Huber32f3cef2011-03-02 15:34:46 -0800593 if (audio) {
594 mSkipRenderingAudioUntilMediaTimeUs = -1;
595 } else {
596 mSkipRenderingVideoUntilMediaTimeUs = -1;
597 }
598
599 sp<AMessage> extra;
600 if (accessUnit->meta()->findMessage("extra", &extra)
601 && extra != NULL) {
602 int64_t resumeAtMediaTimeUs;
603 if (extra->findInt64(
604 "resume-at-mediatimeUs", &resumeAtMediaTimeUs)) {
605 LOGI("suppressing rendering of %s until %lld us",
606 audio ? "audio" : "video", resumeAtMediaTimeUs);
607
608 if (audio) {
609 mSkipRenderingAudioUntilMediaTimeUs =
610 resumeAtMediaTimeUs;
611 } else {
612 mSkipRenderingVideoUntilMediaTimeUs =
613 resumeAtMediaTimeUs;
614 }
615 }
616 }
617
Andreas Huber1aef2112011-01-04 14:01:29 -0800618 flushDecoder(audio, formatChange);
Andreas Huberf9334412010-12-15 15:17:42 -0800619 }
620
621 reply->setInt32("err", err);
622 reply->post();
623 return OK;
624 }
625
Andreas Huber43c3e6c2011-01-05 12:17:08 -0800626 // LOGV("returned a valid buffer of %s data", audio ? "audio" : "video");
Andreas Huberf9334412010-12-15 15:17:42 -0800627
628#if 0
629 int64_t mediaTimeUs;
630 CHECK(accessUnit->meta()->findInt64("timeUs", &mediaTimeUs));
Andreas Huber1aef2112011-01-04 14:01:29 -0800631 LOGV("feeding %s input buffer at media time %.2f secs",
Andreas Huberf9334412010-12-15 15:17:42 -0800632 audio ? "audio" : "video",
633 mediaTimeUs / 1E6);
634#endif
635
636 reply->setObject("buffer", accessUnit);
637 reply->post();
638
639 return OK;
640}
641
642void NuPlayer::renderBuffer(bool audio, const sp<AMessage> &msg) {
Andreas Huber43c3e6c2011-01-05 12:17:08 -0800643 // LOGV("renderBuffer %s", audio ? "audio" : "video");
Andreas Huberf9334412010-12-15 15:17:42 -0800644
645 sp<AMessage> reply;
646 CHECK(msg->findMessage("reply", &reply));
647
648 sp<RefBase> obj;
649 CHECK(msg->findObject("buffer", &obj));
650
651 sp<ABuffer> buffer = static_cast<ABuffer *>(obj.get());
652
Andreas Huber32f3cef2011-03-02 15:34:46 -0800653 int64_t &skipUntilMediaTimeUs =
654 audio
655 ? mSkipRenderingAudioUntilMediaTimeUs
656 : mSkipRenderingVideoUntilMediaTimeUs;
657
658 if (skipUntilMediaTimeUs >= 0) {
659 int64_t mediaTimeUs;
660 CHECK(buffer->meta()->findInt64("timeUs", &mediaTimeUs));
661
662 if (mediaTimeUs < skipUntilMediaTimeUs) {
663 LOGV("dropping %s buffer at time %lld as requested.",
664 audio ? "audio" : "video",
665 mediaTimeUs);
666
667 reply->post();
668 return;
669 }
670
671 skipUntilMediaTimeUs = -1;
672 }
673
Andreas Huberf9334412010-12-15 15:17:42 -0800674 mRenderer->queueBuffer(audio, buffer, reply);
675}
676
677void NuPlayer::notifyListener(int msg, int ext1, int ext2) {
Andreas Huber43c3e6c2011-01-05 12:17:08 -0800678 if (mDriver == NULL) {
Andreas Huberf9334412010-12-15 15:17:42 -0800679 return;
680 }
681
Andreas Huber43c3e6c2011-01-05 12:17:08 -0800682 sp<NuPlayerDriver> driver = mDriver.promote();
Andreas Huberf9334412010-12-15 15:17:42 -0800683
Andreas Huber43c3e6c2011-01-05 12:17:08 -0800684 if (driver == NULL) {
Andreas Huberf9334412010-12-15 15:17:42 -0800685 return;
686 }
687
Andreas Huber43c3e6c2011-01-05 12:17:08 -0800688 driver->sendEvent(msg, ext1, ext2);
Andreas Huberf9334412010-12-15 15:17:42 -0800689}
690
Andreas Huber1aef2112011-01-04 14:01:29 -0800691void NuPlayer::flushDecoder(bool audio, bool needShutdown) {
692 // Make sure we don't continue to scan sources until we finish flushing.
693 ++mScanSourcesGeneration;
Andreas Huber43c3e6c2011-01-05 12:17:08 -0800694 mScanSourcesPending = false;
Andreas Huber1aef2112011-01-04 14:01:29 -0800695
696 (audio ? mAudioDecoder : mVideoDecoder)->signalFlush();
697 mRenderer->flush(audio);
698
699 FlushStatus newStatus =
700 needShutdown ? FLUSHING_DECODER_SHUTDOWN : FLUSHING_DECODER;
701
702 if (audio) {
703 CHECK(mFlushingAudio == NONE
704 || mFlushingAudio == AWAITING_DISCONTINUITY);
705
706 mFlushingAudio = newStatus;
707
708 if (mFlushingVideo == NONE) {
709 mFlushingVideo = (mVideoDecoder != NULL)
710 ? AWAITING_DISCONTINUITY
711 : FLUSHED;
712 }
713 } else {
714 CHECK(mFlushingVideo == NONE
715 || mFlushingVideo == AWAITING_DISCONTINUITY);
716
717 mFlushingVideo = newStatus;
718
719 if (mFlushingAudio == NONE) {
720 mFlushingAudio = (mAudioDecoder != NULL)
721 ? AWAITING_DISCONTINUITY
722 : FLUSHED;
723 }
724 }
725}
726
Andreas Huberf9334412010-12-15 15:17:42 -0800727} // namespace android