blob: 7218fafdc684ea747baa8aaead66112e1e6d489a [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>
Andreas Huber3fe62152011-09-16 15:09:22 -070037#include <media/stagefright/MediaDefs.h>
Andreas Huberf9334412010-12-15 15:17:42 -080038#include <media/stagefright/MediaErrors.h>
39#include <media/stagefright/MetaData.h>
40#include <surfaceflinger/Surface.h>
Glenn Kasten11731182011-02-08 17:26:17 -080041#include <gui/ISurfaceTexture.h>
Andreas Huberf9334412010-12-15 15:17:42 -080042
Andreas Huber3fe62152011-09-16 15:09:22 -070043#include "avc_utils.h"
44
Andreas Huberf9334412010-12-15 15:17:42 -080045namespace android {
46
47////////////////////////////////////////////////////////////////////////////////
48
49NuPlayer::NuPlayer()
Andreas Huber9b80c2b2011-06-30 15:47:02 -070050 : mUIDValid(false),
Andreas Huber3fe62152011-09-16 15:09:22 -070051 mVideoIsAVC(false),
Andreas Huber9b80c2b2011-06-30 15:47:02 -070052 mAudioEOS(false),
Andreas Huberf9334412010-12-15 15:17:42 -080053 mVideoEOS(false),
Andreas Huber5bc087c2010-12-23 10:27:40 -080054 mScanSourcesPending(false),
Andreas Huber1aef2112011-01-04 14:01:29 -080055 mScanSourcesGeneration(0),
Andreas Huberf9334412010-12-15 15:17:42 -080056 mFlushingAudio(NONE),
Andreas Huber1aef2112011-01-04 14:01:29 -080057 mFlushingVideo(NONE),
58 mResetInProgress(false),
Andreas Huber3fe62152011-09-16 15:09:22 -070059 mResetPostponed(false),
60 mSkipRenderingAudioUntilMediaTimeUs(-1ll),
61 mSkipRenderingVideoUntilMediaTimeUs(-1ll),
62 mVideoLateByUs(0ll),
63 mNumFramesTotal(0ll),
64 mNumFramesDropped(0ll) {
Andreas Huberf9334412010-12-15 15:17:42 -080065}
66
67NuPlayer::~NuPlayer() {
68}
69
Andreas Huber9b80c2b2011-06-30 15:47:02 -070070void NuPlayer::setUID(uid_t uid) {
71 mUIDValid = true;
72 mUID = uid;
73}
74
Andreas Huber43c3e6c2011-01-05 12:17:08 -080075void NuPlayer::setDriver(const wp<NuPlayerDriver> &driver) {
76 mDriver = driver;
Andreas Huberf9334412010-12-15 15:17:42 -080077}
78
79void NuPlayer::setDataSource(const sp<IStreamSource> &source) {
80 sp<AMessage> msg = new AMessage(kWhatSetDataSource, id());
81
Andreas Huber5bc087c2010-12-23 10:27:40 -080082 msg->setObject("source", new StreamingSource(source));
83 msg->post();
84}
Andreas Huberf9334412010-12-15 15:17:42 -080085
Andreas Huber5bc087c2010-12-23 10:27:40 -080086void NuPlayer::setDataSource(
87 const char *url, const KeyedVector<String8, String8> *headers) {
88 sp<AMessage> msg = new AMessage(kWhatSetDataSource, id());
89
Andreas Huber9b80c2b2011-06-30 15:47:02 -070090 msg->setObject("source", new HTTPLiveSource(url, headers, mUIDValid, mUID));
Andreas Huberf9334412010-12-15 15:17:42 -080091 msg->post();
92}
93
94void NuPlayer::setVideoSurface(const sp<Surface> &surface) {
Glenn Kasten11731182011-02-08 17:26:17 -080095 sp<AMessage> msg = new AMessage(kWhatSetVideoNativeWindow, id());
96 msg->setObject("native-window", new NativeWindowWrapper(surface));
97 msg->post();
98}
99
100void NuPlayer::setVideoSurfaceTexture(const sp<ISurfaceTexture> &surfaceTexture) {
101 sp<AMessage> msg = new AMessage(kWhatSetVideoNativeWindow, id());
102 sp<SurfaceTextureClient> surfaceTextureClient(surfaceTexture != NULL ?
103 new SurfaceTextureClient(surfaceTexture) : NULL);
104 msg->setObject("native-window", new NativeWindowWrapper(surfaceTextureClient));
Andreas Huberf9334412010-12-15 15:17:42 -0800105 msg->post();
106}
107
108void NuPlayer::setAudioSink(const sp<MediaPlayerBase::AudioSink> &sink) {
109 sp<AMessage> msg = new AMessage(kWhatSetAudioSink, id());
110 msg->setObject("sink", sink);
111 msg->post();
112}
113
114void NuPlayer::start() {
115 (new AMessage(kWhatStart, id()))->post();
116}
117
Andreas Huber43c3e6c2011-01-05 12:17:08 -0800118void NuPlayer::pause() {
Andreas Huberb4082222011-01-20 15:23:04 -0800119 (new AMessage(kWhatPause, id()))->post();
Andreas Huber43c3e6c2011-01-05 12:17:08 -0800120}
121
122void NuPlayer::resume() {
Andreas Huberb4082222011-01-20 15:23:04 -0800123 (new AMessage(kWhatResume, id()))->post();
Andreas Huber43c3e6c2011-01-05 12:17:08 -0800124}
125
Andreas Huber1aef2112011-01-04 14:01:29 -0800126void NuPlayer::resetAsync() {
127 (new AMessage(kWhatReset, id()))->post();
128}
129
Andreas Huber43c3e6c2011-01-05 12:17:08 -0800130void NuPlayer::seekToAsync(int64_t seekTimeUs) {
131 sp<AMessage> msg = new AMessage(kWhatSeek, id());
132 msg->setInt64("seekTimeUs", seekTimeUs);
133 msg->post();
134}
135
Andreas Huber53df1a42010-12-22 10:03:04 -0800136// static
Andreas Huber1aef2112011-01-04 14:01:29 -0800137bool NuPlayer::IsFlushingState(FlushStatus state, bool *needShutdown) {
Andreas Huber53df1a42010-12-22 10:03:04 -0800138 switch (state) {
139 case FLUSHING_DECODER:
Andreas Huber1aef2112011-01-04 14:01:29 -0800140 if (needShutdown != NULL) {
141 *needShutdown = false;
Andreas Huber53df1a42010-12-22 10:03:04 -0800142 }
143 return true;
144
Andreas Huber1aef2112011-01-04 14:01:29 -0800145 case FLUSHING_DECODER_SHUTDOWN:
146 if (needShutdown != NULL) {
147 *needShutdown = true;
Andreas Huber53df1a42010-12-22 10:03:04 -0800148 }
149 return true;
150
151 default:
152 return false;
153 }
154}
155
Andreas Huberf9334412010-12-15 15:17:42 -0800156void NuPlayer::onMessageReceived(const sp<AMessage> &msg) {
157 switch (msg->what()) {
158 case kWhatSetDataSource:
159 {
Andreas Huber1aef2112011-01-04 14:01:29 -0800160 LOGV("kWhatSetDataSource");
Andreas Huberf9334412010-12-15 15:17:42 -0800161
162 CHECK(mSource == NULL);
163
Andreas Huber5bc087c2010-12-23 10:27:40 -0800164 sp<RefBase> obj;
165 CHECK(msg->findObject("source", &obj));
Andreas Huberf9334412010-12-15 15:17:42 -0800166
Andreas Huber5bc087c2010-12-23 10:27:40 -0800167 mSource = static_cast<Source *>(obj.get());
Andreas Huberf9334412010-12-15 15:17:42 -0800168 break;
169 }
170
Glenn Kasten11731182011-02-08 17:26:17 -0800171 case kWhatSetVideoNativeWindow:
Andreas Huberf9334412010-12-15 15:17:42 -0800172 {
Glenn Kasten11731182011-02-08 17:26:17 -0800173 LOGV("kWhatSetVideoNativeWindow");
Andreas Huberf9334412010-12-15 15:17:42 -0800174
175 sp<RefBase> obj;
Glenn Kasten11731182011-02-08 17:26:17 -0800176 CHECK(msg->findObject("native-window", &obj));
Andreas Huberf9334412010-12-15 15:17:42 -0800177
Glenn Kasten11731182011-02-08 17:26:17 -0800178 mNativeWindow = static_cast<NativeWindowWrapper *>(obj.get());
Andreas Huberf9334412010-12-15 15:17:42 -0800179 break;
180 }
181
182 case kWhatSetAudioSink:
183 {
Andreas Huber1aef2112011-01-04 14:01:29 -0800184 LOGV("kWhatSetAudioSink");
Andreas Huberf9334412010-12-15 15:17:42 -0800185
186 sp<RefBase> obj;
187 CHECK(msg->findObject("sink", &obj));
188
189 mAudioSink = static_cast<MediaPlayerBase::AudioSink *>(obj.get());
190 break;
191 }
192
193 case kWhatStart:
194 {
Andreas Huber43c3e6c2011-01-05 12:17:08 -0800195 LOGV("kWhatStart");
196
Andreas Huber3fe62152011-09-16 15:09:22 -0700197 mVideoIsAVC = false;
Andreas Huber1aef2112011-01-04 14:01:29 -0800198 mAudioEOS = false;
199 mVideoEOS = false;
Andreas Huber32f3cef2011-03-02 15:34:46 -0800200 mSkipRenderingAudioUntilMediaTimeUs = -1;
201 mSkipRenderingVideoUntilMediaTimeUs = -1;
Andreas Huber3fe62152011-09-16 15:09:22 -0700202 mVideoLateByUs = 0;
203 mNumFramesTotal = 0;
204 mNumFramesDropped = 0;
Andreas Huber1aef2112011-01-04 14:01:29 -0800205
Andreas Huber5bc087c2010-12-23 10:27:40 -0800206 mSource->start();
Andreas Huberf9334412010-12-15 15:17:42 -0800207
208 mRenderer = new Renderer(
209 mAudioSink,
210 new AMessage(kWhatRendererNotify, id()));
211
212 looper()->registerHandler(mRenderer);
213
Andreas Huber1aef2112011-01-04 14:01:29 -0800214 postScanSources();
Andreas Huberf9334412010-12-15 15:17:42 -0800215 break;
216 }
217
218 case kWhatScanSources:
219 {
Andreas Huber1aef2112011-01-04 14:01:29 -0800220 int32_t generation;
221 CHECK(msg->findInt32("generation", &generation));
222 if (generation != mScanSourcesGeneration) {
223 // Drop obsolete msg.
224 break;
225 }
226
Andreas Huber5bc087c2010-12-23 10:27:40 -0800227 mScanSourcesPending = false;
228
Andreas Huber43c3e6c2011-01-05 12:17:08 -0800229 LOGV("scanning sources haveAudio=%d, haveVideo=%d",
230 mAudioDecoder != NULL, mVideoDecoder != NULL);
231
Andreas Huber5bc087c2010-12-23 10:27:40 -0800232 instantiateDecoder(false, &mVideoDecoder);
Andreas Huberf9334412010-12-15 15:17:42 -0800233
234 if (mAudioSink != NULL) {
Andreas Huber5bc087c2010-12-23 10:27:40 -0800235 instantiateDecoder(true, &mAudioDecoder);
Andreas Huberf9334412010-12-15 15:17:42 -0800236 }
237
Andreas Huber5bc087c2010-12-23 10:27:40 -0800238 if (!mSource->feedMoreTSData()) {
Andreas Huber1aef2112011-01-04 14:01:29 -0800239 if (mAudioDecoder == NULL && mVideoDecoder == NULL) {
240 // We're not currently decoding anything (no audio or
241 // video tracks found) and we just ran out of input data.
242 notifyListener(MEDIA_PLAYBACK_COMPLETE, 0, 0);
243 }
Andreas Huberf9334412010-12-15 15:17:42 -0800244 break;
245 }
246
Andreas Huberf9334412010-12-15 15:17:42 -0800247 if (mAudioDecoder == NULL || mVideoDecoder == NULL) {
248 msg->post(100000ll);
Andreas Huber5bc087c2010-12-23 10:27:40 -0800249 mScanSourcesPending = true;
Andreas Huberf9334412010-12-15 15:17:42 -0800250 }
251 break;
252 }
253
254 case kWhatVideoNotify:
255 case kWhatAudioNotify:
256 {
257 bool audio = msg->what() == kWhatAudioNotify;
258
259 sp<AMessage> codecRequest;
260 CHECK(msg->findMessage("codec-request", &codecRequest));
261
262 int32_t what;
263 CHECK(codecRequest->findInt32("what", &what));
264
265 if (what == ACodec::kWhatFillThisBuffer) {
266 status_t err = feedDecoderInputData(
267 audio, codecRequest);
268
Andreas Huber5bc087c2010-12-23 10:27:40 -0800269 if (err == -EWOULDBLOCK) {
270 if (mSource->feedMoreTSData()) {
271 msg->post();
272 }
Andreas Huberf9334412010-12-15 15:17:42 -0800273 }
274 } else if (what == ACodec::kWhatEOS) {
275 mRenderer->queueEOS(audio, ERROR_END_OF_STREAM);
276 } else if (what == ACodec::kWhatFlushCompleted) {
Andreas Huber1aef2112011-01-04 14:01:29 -0800277 bool needShutdown;
Andreas Huber53df1a42010-12-22 10:03:04 -0800278
Andreas Huberf9334412010-12-15 15:17:42 -0800279 if (audio) {
Andreas Huber1aef2112011-01-04 14:01:29 -0800280 CHECK(IsFlushingState(mFlushingAudio, &needShutdown));
Andreas Huberf9334412010-12-15 15:17:42 -0800281 mFlushingAudio = FLUSHED;
282 } else {
Andreas Huber1aef2112011-01-04 14:01:29 -0800283 CHECK(IsFlushingState(mFlushingVideo, &needShutdown));
Andreas Huberf9334412010-12-15 15:17:42 -0800284 mFlushingVideo = FLUSHED;
Andreas Huber3fe62152011-09-16 15:09:22 -0700285
286 mVideoLateByUs = 0;
Andreas Huberf9334412010-12-15 15:17:42 -0800287 }
288
Andreas Huber1aef2112011-01-04 14:01:29 -0800289 LOGV("decoder %s flush completed", audio ? "audio" : "video");
Andreas Huberf9334412010-12-15 15:17:42 -0800290
Andreas Huber1aef2112011-01-04 14:01:29 -0800291 if (needShutdown) {
292 LOGV("initiating %s decoder shutdown",
Andreas Huber53df1a42010-12-22 10:03:04 -0800293 audio ? "audio" : "video");
Andreas Huberf9334412010-12-15 15:17:42 -0800294
Andreas Huber53df1a42010-12-22 10:03:04 -0800295 (audio ? mAudioDecoder : mVideoDecoder)->initiateShutdown();
Andreas Huberf9334412010-12-15 15:17:42 -0800296
Andreas Huber53df1a42010-12-22 10:03:04 -0800297 if (audio) {
298 mFlushingAudio = SHUTTING_DOWN_DECODER;
299 } else {
300 mFlushingVideo = SHUTTING_DOWN_DECODER;
301 }
Andreas Huberf9334412010-12-15 15:17:42 -0800302 }
Andreas Huber3831a062010-12-21 10:22:33 -0800303
304 finishFlushIfPossible();
Andreas Huber2c2814b2010-12-15 17:18:20 -0800305 } else if (what == ACodec::kWhatOutputFormatChanged) {
Andreas Huber31e25082011-01-10 10:38:31 -0800306 if (audio) {
307 int32_t numChannels;
308 CHECK(codecRequest->findInt32("channel-count", &numChannels));
Andreas Huber2c2814b2010-12-15 17:18:20 -0800309
Andreas Huber31e25082011-01-10 10:38:31 -0800310 int32_t sampleRate;
311 CHECK(codecRequest->findInt32("sample-rate", &sampleRate));
Andreas Huber2c2814b2010-12-15 17:18:20 -0800312
Andreas Huber31e25082011-01-10 10:38:31 -0800313 LOGV("Audio output format changed to %d Hz, %d channels",
314 sampleRate, numChannels);
Andreas Huber2c2814b2010-12-15 17:18:20 -0800315
Andreas Huber31e25082011-01-10 10:38:31 -0800316 mAudioSink->close();
Andreas Huber078cfcf2011-09-15 12:25:04 -0700317 CHECK_EQ(mAudioSink->open(
318 sampleRate,
319 numChannels,
320 AUDIO_FORMAT_PCM_16_BIT,
321 8 /* bufferCount */),
322 (status_t)OK);
Andreas Huber31e25082011-01-10 10:38:31 -0800323 mAudioSink->start();
Andreas Huber2c2814b2010-12-15 17:18:20 -0800324
Andreas Huber31e25082011-01-10 10:38:31 -0800325 mRenderer->signalAudioSinkChanged();
326 } else {
327 // video
Andreas Huber3831a062010-12-21 10:22:33 -0800328
Andreas Huber31e25082011-01-10 10:38:31 -0800329 int32_t width, height;
330 CHECK(codecRequest->findInt32("width", &width));
331 CHECK(codecRequest->findInt32("height", &height));
332
333 int32_t cropLeft, cropTop, cropRight, cropBottom;
334 CHECK(codecRequest->findRect(
335 "crop",
336 &cropLeft, &cropTop, &cropRight, &cropBottom));
337
338 LOGV("Video output format changed to %d x %d "
Andreas Hubercb67cd12011-08-26 16:02:19 -0700339 "(crop: %d x %d @ (%d, %d))",
Andreas Huber31e25082011-01-10 10:38:31 -0800340 width, height,
Andreas Hubercb67cd12011-08-26 16:02:19 -0700341 (cropRight - cropLeft + 1),
342 (cropBottom - cropTop + 1),
343 cropLeft, cropTop);
Andreas Huber31e25082011-01-10 10:38:31 -0800344
345 notifyListener(
346 MEDIA_SET_VIDEO_SIZE,
347 cropRight - cropLeft + 1,
348 cropBottom - cropTop + 1);
349 }
Andreas Huber3831a062010-12-21 10:22:33 -0800350 } else if (what == ACodec::kWhatShutdownCompleted) {
Andreas Huber1aef2112011-01-04 14:01:29 -0800351 LOGV("%s shutdown completed", audio ? "audio" : "video");
Andreas Huber3831a062010-12-21 10:22:33 -0800352 if (audio) {
353 mAudioDecoder.clear();
354
355 CHECK_EQ((int)mFlushingAudio, (int)SHUTTING_DOWN_DECODER);
356 mFlushingAudio = SHUT_DOWN;
357 } else {
358 mVideoDecoder.clear();
359
360 CHECK_EQ((int)mFlushingVideo, (int)SHUTTING_DOWN_DECODER);
361 mFlushingVideo = SHUT_DOWN;
362 }
363
364 finishFlushIfPossible();
Andreas Huberc92fd242011-08-16 13:48:44 -0700365 } else if (what == ACodec::kWhatError) {
366 LOGE("Received error from %s decoder, aborting playback.",
367 audio ? "audio" : "video");
368
369 mRenderer->queueEOS(audio, UNKNOWN_ERROR);
Andreas Huberf9334412010-12-15 15:17:42 -0800370 } else {
371 CHECK_EQ((int)what, (int)ACodec::kWhatDrainThisBuffer);
372
373 renderBuffer(audio, codecRequest);
374 }
375
376 break;
377 }
378
379 case kWhatRendererNotify:
380 {
381 int32_t what;
382 CHECK(msg->findInt32("what", &what));
383
384 if (what == Renderer::kWhatEOS) {
385 int32_t audio;
386 CHECK(msg->findInt32("audio", &audio));
387
Andreas Huberc92fd242011-08-16 13:48:44 -0700388 int32_t finalResult;
389 CHECK(msg->findInt32("finalResult", &finalResult));
390
Andreas Huberf9334412010-12-15 15:17:42 -0800391 if (audio) {
392 mAudioEOS = true;
393 } else {
394 mVideoEOS = true;
395 }
396
Andreas Huberc92fd242011-08-16 13:48:44 -0700397 if (finalResult == ERROR_END_OF_STREAM) {
398 LOGV("reached %s EOS", audio ? "audio" : "video");
399 } else {
400 LOGE("%s track encountered an error (0x%08x)",
401 audio ? "audio" : "video", finalResult);
402
403 notifyListener(
404 MEDIA_ERROR, MEDIA_ERROR_UNKNOWN, finalResult);
405 }
Andreas Huberf9334412010-12-15 15:17:42 -0800406
407 if ((mAudioEOS || mAudioDecoder == NULL)
408 && (mVideoEOS || mVideoDecoder == NULL)) {
409 notifyListener(MEDIA_PLAYBACK_COMPLETE, 0, 0);
410 }
Andreas Huber43c3e6c2011-01-05 12:17:08 -0800411 } else if (what == Renderer::kWhatPosition) {
412 int64_t positionUs;
413 CHECK(msg->findInt64("positionUs", &positionUs));
414
Andreas Huber3fe62152011-09-16 15:09:22 -0700415 CHECK(msg->findInt64("videoLateByUs", &mVideoLateByUs));
416
Andreas Huber43c3e6c2011-01-05 12:17:08 -0800417 if (mDriver != NULL) {
418 sp<NuPlayerDriver> driver = mDriver.promote();
419 if (driver != NULL) {
420 driver->notifyPosition(positionUs);
Andreas Huber3fe62152011-09-16 15:09:22 -0700421
422 driver->notifyFrameStats(
423 mNumFramesTotal, mNumFramesDropped);
Andreas Huber43c3e6c2011-01-05 12:17:08 -0800424 }
425 }
Andreas Huber3fe62152011-09-16 15:09:22 -0700426 } else if (what == Renderer::kWhatFlushComplete) {
Andreas Huberf9334412010-12-15 15:17:42 -0800427 CHECK_EQ(what, (int32_t)Renderer::kWhatFlushComplete);
428
429 int32_t audio;
430 CHECK(msg->findInt32("audio", &audio));
431
Andreas Huber1aef2112011-01-04 14:01:29 -0800432 LOGV("renderer %s flush completed.", audio ? "audio" : "video");
Andreas Huberf9334412010-12-15 15:17:42 -0800433 }
434 break;
435 }
436
437 case kWhatMoreDataQueued:
438 {
439 break;
440 }
441
Andreas Huber1aef2112011-01-04 14:01:29 -0800442 case kWhatReset:
443 {
444 LOGV("kWhatReset");
445
446 if (mFlushingAudio != NONE || mFlushingVideo != NONE) {
447 // We're currently flushing, postpone the reset until that's
448 // completed.
449
450 LOGV("postponing reset");
451
452 mResetPostponed = true;
453 break;
454 }
455
456 if (mAudioDecoder == NULL && mVideoDecoder == NULL) {
457 finishReset();
458 break;
459 }
460
461 if (mAudioDecoder != NULL) {
462 flushDecoder(true /* audio */, true /* needShutdown */);
463 }
464
465 if (mVideoDecoder != NULL) {
466 flushDecoder(false /* audio */, true /* needShutdown */);
467 }
468
469 mResetInProgress = true;
470 break;
471 }
472
Andreas Huber43c3e6c2011-01-05 12:17:08 -0800473 case kWhatSeek:
474 {
475 int64_t seekTimeUs;
476 CHECK(msg->findInt64("seekTimeUs", &seekTimeUs));
477
Andreas Huber22fc52f2011-01-05 16:24:27 -0800478 LOGV("kWhatSeek seekTimeUs=%lld us (%.2f secs)",
Andreas Huber43c3e6c2011-01-05 12:17:08 -0800479 seekTimeUs, seekTimeUs / 1E6);
480
481 mSource->seekTo(seekTimeUs);
482
483 if (mDriver != NULL) {
484 sp<NuPlayerDriver> driver = mDriver.promote();
485 if (driver != NULL) {
486 driver->notifySeekComplete();
487 }
488 }
489
490 break;
491 }
492
Andreas Huberb4082222011-01-20 15:23:04 -0800493 case kWhatPause:
494 {
495 CHECK(mRenderer != NULL);
496 mRenderer->pause();
497 break;
498 }
499
500 case kWhatResume:
501 {
502 CHECK(mRenderer != NULL);
503 mRenderer->resume();
504 break;
505 }
506
Andreas Huberf9334412010-12-15 15:17:42 -0800507 default:
508 TRESPASS();
509 break;
510 }
511}
512
Andreas Huber3831a062010-12-21 10:22:33 -0800513void NuPlayer::finishFlushIfPossible() {
514 if (mFlushingAudio != FLUSHED && mFlushingAudio != SHUT_DOWN) {
515 return;
516 }
517
518 if (mFlushingVideo != FLUSHED && mFlushingVideo != SHUT_DOWN) {
519 return;
520 }
521
Andreas Huber1aef2112011-01-04 14:01:29 -0800522 LOGV("both audio and video are flushed now.");
Andreas Huber3831a062010-12-21 10:22:33 -0800523
524 mRenderer->signalTimeDiscontinuity();
525
Andreas Huber22fc52f2011-01-05 16:24:27 -0800526 if (mAudioDecoder != NULL) {
Andreas Huber3831a062010-12-21 10:22:33 -0800527 mAudioDecoder->signalResume();
528 }
529
Andreas Huber22fc52f2011-01-05 16:24:27 -0800530 if (mVideoDecoder != NULL) {
Andreas Huber3831a062010-12-21 10:22:33 -0800531 mVideoDecoder->signalResume();
532 }
533
534 mFlushingAudio = NONE;
535 mFlushingVideo = NONE;
Andreas Huber3831a062010-12-21 10:22:33 -0800536
Andreas Huber1aef2112011-01-04 14:01:29 -0800537 if (mResetInProgress) {
538 LOGV("reset completed");
539
540 mResetInProgress = false;
541 finishReset();
542 } else if (mResetPostponed) {
543 (new AMessage(kWhatReset, id()))->post();
544 mResetPostponed = false;
Andreas Huber22fc52f2011-01-05 16:24:27 -0800545 } else if (mAudioDecoder == NULL || mVideoDecoder == NULL) {
Andreas Huber1aef2112011-01-04 14:01:29 -0800546 postScanSources();
Andreas Huberf9334412010-12-15 15:17:42 -0800547 }
548}
549
Andreas Huber1aef2112011-01-04 14:01:29 -0800550void NuPlayer::finishReset() {
551 CHECK(mAudioDecoder == NULL);
552 CHECK(mVideoDecoder == NULL);
553
554 mRenderer.clear();
555 mSource.clear();
556
Andreas Huber43c3e6c2011-01-05 12:17:08 -0800557 if (mDriver != NULL) {
558 sp<NuPlayerDriver> driver = mDriver.promote();
559 if (driver != NULL) {
560 driver->notifyResetComplete();
561 }
562 }
Andreas Huber1aef2112011-01-04 14:01:29 -0800563}
564
565void NuPlayer::postScanSources() {
566 if (mScanSourcesPending) {
567 return;
568 }
569
570 sp<AMessage> msg = new AMessage(kWhatScanSources, id());
571 msg->setInt32("generation", mScanSourcesGeneration);
572 msg->post();
573
574 mScanSourcesPending = true;
575}
576
Andreas Huber5bc087c2010-12-23 10:27:40 -0800577status_t NuPlayer::instantiateDecoder(bool audio, sp<Decoder> *decoder) {
Andreas Huberf9334412010-12-15 15:17:42 -0800578 if (*decoder != NULL) {
579 return OK;
580 }
581
Andreas Huber5bc087c2010-12-23 10:27:40 -0800582 sp<MetaData> meta = mSource->getFormat(audio);
Andreas Huberf9334412010-12-15 15:17:42 -0800583
Andreas Huber5bc087c2010-12-23 10:27:40 -0800584 if (meta == NULL) {
Andreas Huberf9334412010-12-15 15:17:42 -0800585 return -EWOULDBLOCK;
586 }
587
Andreas Huber3fe62152011-09-16 15:09:22 -0700588 if (!audio) {
589 const char *mime;
590 CHECK(meta->findCString(kKeyMIMEType, &mime));
591 mVideoIsAVC = !strcasecmp(MEDIA_MIMETYPE_VIDEO_AVC, mime);
592 }
593
Andreas Huberf9334412010-12-15 15:17:42 -0800594 sp<AMessage> notify =
595 new AMessage(audio ? kWhatAudioNotify : kWhatVideoNotify,
596 id());
597
Glenn Kasten11731182011-02-08 17:26:17 -0800598 *decoder = audio ? new Decoder(notify) :
599 new Decoder(notify, mNativeWindow);
Andreas Huberf9334412010-12-15 15:17:42 -0800600 looper()->registerHandler(*decoder);
601
Andreas Huber5bc087c2010-12-23 10:27:40 -0800602 (*decoder)->configure(meta);
Andreas Huberf9334412010-12-15 15:17:42 -0800603
Andreas Huber43c3e6c2011-01-05 12:17:08 -0800604 int64_t durationUs;
605 if (mDriver != NULL && mSource->getDuration(&durationUs) == OK) {
606 sp<NuPlayerDriver> driver = mDriver.promote();
607 if (driver != NULL) {
608 driver->notifyDuration(durationUs);
609 }
610 }
611
Andreas Huberf9334412010-12-15 15:17:42 -0800612 return OK;
613}
614
615status_t NuPlayer::feedDecoderInputData(bool audio, const sp<AMessage> &msg) {
616 sp<AMessage> reply;
617 CHECK(msg->findMessage("reply", &reply));
618
Andreas Huber53df1a42010-12-22 10:03:04 -0800619 if ((audio && IsFlushingState(mFlushingAudio))
620 || (!audio && IsFlushingState(mFlushingVideo))) {
Andreas Huberf9334412010-12-15 15:17:42 -0800621 reply->setInt32("err", INFO_DISCONTINUITY);
622 reply->post();
623 return OK;
624 }
625
626 sp<ABuffer> accessUnit;
Andreas Huberf9334412010-12-15 15:17:42 -0800627
Andreas Huber3fe62152011-09-16 15:09:22 -0700628 bool dropAccessUnit;
629 do {
630 status_t err = mSource->dequeueAccessUnit(audio, &accessUnit);
Andreas Huber5bc087c2010-12-23 10:27:40 -0800631
Andreas Huber3fe62152011-09-16 15:09:22 -0700632 if (err == -EWOULDBLOCK) {
633 return err;
634 } else if (err != OK) {
635 if (err == INFO_DISCONTINUITY) {
636 int32_t type;
637 CHECK(accessUnit->meta()->findInt32("discontinuity", &type));
Andreas Huber53df1a42010-12-22 10:03:04 -0800638
Andreas Huber3fe62152011-09-16 15:09:22 -0700639 bool formatChange =
640 type == ATSParser::DISCONTINUITY_FORMATCHANGE;
Andreas Huber53df1a42010-12-22 10:03:04 -0800641
Andreas Huber3fe62152011-09-16 15:09:22 -0700642 LOGV("%s discontinuity (formatChange=%d)",
643 audio ? "audio" : "video", formatChange);
Andreas Huber32f3cef2011-03-02 15:34:46 -0800644
Andreas Huber3fe62152011-09-16 15:09:22 -0700645 if (audio) {
646 mSkipRenderingAudioUntilMediaTimeUs = -1;
647 } else {
648 mSkipRenderingVideoUntilMediaTimeUs = -1;
649 }
Andreas Huber32f3cef2011-03-02 15:34:46 -0800650
Andreas Huber3fe62152011-09-16 15:09:22 -0700651 sp<AMessage> extra;
652 if (accessUnit->meta()->findMessage("extra", &extra)
653 && extra != NULL) {
654 int64_t resumeAtMediaTimeUs;
655 if (extra->findInt64(
656 "resume-at-mediatimeUs", &resumeAtMediaTimeUs)) {
657 LOGI("suppressing rendering of %s until %lld us",
658 audio ? "audio" : "video", resumeAtMediaTimeUs);
659
660 if (audio) {
661 mSkipRenderingAudioUntilMediaTimeUs =
662 resumeAtMediaTimeUs;
663 } else {
664 mSkipRenderingVideoUntilMediaTimeUs =
665 resumeAtMediaTimeUs;
666 }
Andreas Huber32f3cef2011-03-02 15:34:46 -0800667 }
668 }
Andreas Huber3fe62152011-09-16 15:09:22 -0700669
670 flushDecoder(audio, formatChange);
Andreas Huber32f3cef2011-03-02 15:34:46 -0800671 }
672
Andreas Huber3fe62152011-09-16 15:09:22 -0700673 reply->setInt32("err", err);
674 reply->post();
675 return OK;
Andreas Huberf9334412010-12-15 15:17:42 -0800676 }
677
Andreas Huber3fe62152011-09-16 15:09:22 -0700678 if (!audio) {
679 ++mNumFramesTotal;
680 }
681
682 dropAccessUnit = false;
683 if (!audio
684 && mVideoLateByUs > 100000ll
685 && mVideoIsAVC
686 && !IsAVCReferenceFrame(accessUnit)) {
687 dropAccessUnit = true;
688 ++mNumFramesDropped;
689 }
690 } while (dropAccessUnit);
Andreas Huberf9334412010-12-15 15:17:42 -0800691
Andreas Huber43c3e6c2011-01-05 12:17:08 -0800692 // LOGV("returned a valid buffer of %s data", audio ? "audio" : "video");
Andreas Huberf9334412010-12-15 15:17:42 -0800693
694#if 0
695 int64_t mediaTimeUs;
696 CHECK(accessUnit->meta()->findInt64("timeUs", &mediaTimeUs));
Andreas Huber1aef2112011-01-04 14:01:29 -0800697 LOGV("feeding %s input buffer at media time %.2f secs",
Andreas Huberf9334412010-12-15 15:17:42 -0800698 audio ? "audio" : "video",
699 mediaTimeUs / 1E6);
700#endif
701
702 reply->setObject("buffer", accessUnit);
703 reply->post();
704
705 return OK;
706}
707
708void NuPlayer::renderBuffer(bool audio, const sp<AMessage> &msg) {
Andreas Huber43c3e6c2011-01-05 12:17:08 -0800709 // LOGV("renderBuffer %s", audio ? "audio" : "video");
Andreas Huberf9334412010-12-15 15:17:42 -0800710
711 sp<AMessage> reply;
712 CHECK(msg->findMessage("reply", &reply));
713
Andreas Huber18ac5402011-08-31 15:04:25 -0700714 if (IsFlushingState(audio ? mFlushingAudio : mFlushingVideo)) {
715 // We're currently attempting to flush the decoder, in order
716 // to complete this, the decoder wants all its buffers back,
717 // so we don't want any output buffers it sent us (from before
718 // we initiated the flush) to be stuck in the renderer's queue.
719
720 LOGV("we're still flushing the %s decoder, sending its output buffer"
721 " right back.", audio ? "audio" : "video");
722
723 reply->post();
724 return;
725 }
726
Andreas Huberf9334412010-12-15 15:17:42 -0800727 sp<RefBase> obj;
728 CHECK(msg->findObject("buffer", &obj));
729
730 sp<ABuffer> buffer = static_cast<ABuffer *>(obj.get());
731
Andreas Huber32f3cef2011-03-02 15:34:46 -0800732 int64_t &skipUntilMediaTimeUs =
733 audio
734 ? mSkipRenderingAudioUntilMediaTimeUs
735 : mSkipRenderingVideoUntilMediaTimeUs;
736
737 if (skipUntilMediaTimeUs >= 0) {
738 int64_t mediaTimeUs;
739 CHECK(buffer->meta()->findInt64("timeUs", &mediaTimeUs));
740
741 if (mediaTimeUs < skipUntilMediaTimeUs) {
742 LOGV("dropping %s buffer at time %lld as requested.",
743 audio ? "audio" : "video",
744 mediaTimeUs);
745
746 reply->post();
747 return;
748 }
749
750 skipUntilMediaTimeUs = -1;
751 }
752
Andreas Huberf9334412010-12-15 15:17:42 -0800753 mRenderer->queueBuffer(audio, buffer, reply);
754}
755
756void NuPlayer::notifyListener(int msg, int ext1, int ext2) {
Andreas Huber43c3e6c2011-01-05 12:17:08 -0800757 if (mDriver == NULL) {
Andreas Huberf9334412010-12-15 15:17:42 -0800758 return;
759 }
760
Andreas Huber43c3e6c2011-01-05 12:17:08 -0800761 sp<NuPlayerDriver> driver = mDriver.promote();
Andreas Huberf9334412010-12-15 15:17:42 -0800762
Andreas Huber43c3e6c2011-01-05 12:17:08 -0800763 if (driver == NULL) {
Andreas Huberf9334412010-12-15 15:17:42 -0800764 return;
765 }
766
Andreas Huber43c3e6c2011-01-05 12:17:08 -0800767 driver->sendEvent(msg, ext1, ext2);
Andreas Huberf9334412010-12-15 15:17:42 -0800768}
769
Andreas Huber1aef2112011-01-04 14:01:29 -0800770void NuPlayer::flushDecoder(bool audio, bool needShutdown) {
771 // Make sure we don't continue to scan sources until we finish flushing.
772 ++mScanSourcesGeneration;
Andreas Huber43c3e6c2011-01-05 12:17:08 -0800773 mScanSourcesPending = false;
Andreas Huber1aef2112011-01-04 14:01:29 -0800774
775 (audio ? mAudioDecoder : mVideoDecoder)->signalFlush();
776 mRenderer->flush(audio);
777
778 FlushStatus newStatus =
779 needShutdown ? FLUSHING_DECODER_SHUTDOWN : FLUSHING_DECODER;
780
781 if (audio) {
782 CHECK(mFlushingAudio == NONE
783 || mFlushingAudio == AWAITING_DISCONTINUITY);
784
785 mFlushingAudio = newStatus;
786
787 if (mFlushingVideo == NONE) {
788 mFlushingVideo = (mVideoDecoder != NULL)
789 ? AWAITING_DISCONTINUITY
790 : FLUSHED;
791 }
792 } else {
793 CHECK(mFlushingVideo == NONE
794 || mFlushingVideo == AWAITING_DISCONTINUITY);
795
796 mFlushingVideo = newStatus;
797
798 if (mFlushingAudio == NONE) {
799 mFlushingAudio = (mAudioDecoder != NULL)
800 ? AWAITING_DISCONTINUITY
801 : FLUSHED;
802 }
803 }
804}
805
Andreas Huberf9334412010-12-15 15:17:42 -0800806} // namespace android