blob: e618f6700195ea489323edde06e089aa23b407fe [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"
Andreas Huber2bfdd422011-10-11 15:24:07 -070028#include "RTSPSource.h"
Andreas Huber5bc087c2010-12-23 10:27:40 -080029#include "StreamingSource.h"
30
31#include "ATSParser.h"
Andreas Huberf9334412010-12-15 15:17:42 -080032
Andreas Huber3831a062010-12-21 10:22:33 -080033#include <media/stagefright/foundation/hexdump.h>
Andreas Huberf9334412010-12-15 15:17:42 -080034#include <media/stagefright/foundation/ABuffer.h>
35#include <media/stagefright/foundation/ADebug.h>
36#include <media/stagefright/foundation/AMessage.h>
37#include <media/stagefright/ACodec.h>
Andreas Huber3fe62152011-09-16 15:09:22 -070038#include <media/stagefright/MediaDefs.h>
Andreas Huberf9334412010-12-15 15:17:42 -080039#include <media/stagefright/MediaErrors.h>
40#include <media/stagefright/MetaData.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 Huber6e3d3112011-11-28 12:36:11 -080056 mTimeDiscontinuityPending(false),
Andreas Huberf9334412010-12-15 15:17:42 -080057 mFlushingAudio(NONE),
Andreas Huber1aef2112011-01-04 14:01:29 -080058 mFlushingVideo(NONE),
59 mResetInProgress(false),
Andreas Huber3fe62152011-09-16 15:09:22 -070060 mResetPostponed(false),
61 mSkipRenderingAudioUntilMediaTimeUs(-1ll),
62 mSkipRenderingVideoUntilMediaTimeUs(-1ll),
63 mVideoLateByUs(0ll),
64 mNumFramesTotal(0ll),
65 mNumFramesDropped(0ll) {
Andreas Huberf9334412010-12-15 15:17:42 -080066}
67
68NuPlayer::~NuPlayer() {
69}
70
Andreas Huber9b80c2b2011-06-30 15:47:02 -070071void NuPlayer::setUID(uid_t uid) {
72 mUIDValid = true;
73 mUID = uid;
74}
75
Andreas Huber43c3e6c2011-01-05 12:17:08 -080076void NuPlayer::setDriver(const wp<NuPlayerDriver> &driver) {
77 mDriver = driver;
Andreas Huberf9334412010-12-15 15:17:42 -080078}
79
80void NuPlayer::setDataSource(const sp<IStreamSource> &source) {
81 sp<AMessage> msg = new AMessage(kWhatSetDataSource, id());
82
Andreas Huber5bc087c2010-12-23 10:27:40 -080083 msg->setObject("source", new StreamingSource(source));
84 msg->post();
85}
Andreas Huberf9334412010-12-15 15:17:42 -080086
Andreas Huber5bc087c2010-12-23 10:27:40 -080087void NuPlayer::setDataSource(
88 const char *url, const KeyedVector<String8, String8> *headers) {
89 sp<AMessage> msg = new AMessage(kWhatSetDataSource, id());
90
Andreas Huber2bfdd422011-10-11 15:24:07 -070091 if (!strncasecmp(url, "rtsp://", 7)) {
92 msg->setObject(
93 "source", new RTSPSource(url, headers, mUIDValid, mUID));
94 } else {
95 msg->setObject(
96 "source", new HTTPLiveSource(url, headers, mUIDValid, mUID));
97 }
98
Andreas Huberf9334412010-12-15 15:17:42 -080099 msg->post();
100}
101
Glenn Kasten11731182011-02-08 17:26:17 -0800102void NuPlayer::setVideoSurfaceTexture(const sp<ISurfaceTexture> &surfaceTexture) {
103 sp<AMessage> msg = new AMessage(kWhatSetVideoNativeWindow, id());
104 sp<SurfaceTextureClient> surfaceTextureClient(surfaceTexture != NULL ?
105 new SurfaceTextureClient(surfaceTexture) : NULL);
106 msg->setObject("native-window", new NativeWindowWrapper(surfaceTextureClient));
Andreas Huberf9334412010-12-15 15:17:42 -0800107 msg->post();
108}
109
110void NuPlayer::setAudioSink(const sp<MediaPlayerBase::AudioSink> &sink) {
111 sp<AMessage> msg = new AMessage(kWhatSetAudioSink, id());
112 msg->setObject("sink", sink);
113 msg->post();
114}
115
116void NuPlayer::start() {
117 (new AMessage(kWhatStart, id()))->post();
118}
119
Andreas Huber43c3e6c2011-01-05 12:17:08 -0800120void NuPlayer::pause() {
Andreas Huberb4082222011-01-20 15:23:04 -0800121 (new AMessage(kWhatPause, id()))->post();
Andreas Huber43c3e6c2011-01-05 12:17:08 -0800122}
123
124void NuPlayer::resume() {
Andreas Huberb4082222011-01-20 15:23:04 -0800125 (new AMessage(kWhatResume, id()))->post();
Andreas Huber43c3e6c2011-01-05 12:17:08 -0800126}
127
Andreas Huber1aef2112011-01-04 14:01:29 -0800128void NuPlayer::resetAsync() {
129 (new AMessage(kWhatReset, id()))->post();
130}
131
Andreas Huber43c3e6c2011-01-05 12:17:08 -0800132void NuPlayer::seekToAsync(int64_t seekTimeUs) {
133 sp<AMessage> msg = new AMessage(kWhatSeek, id());
134 msg->setInt64("seekTimeUs", seekTimeUs);
135 msg->post();
136}
137
Andreas Huber53df1a42010-12-22 10:03:04 -0800138// static
Andreas Huber1aef2112011-01-04 14:01:29 -0800139bool NuPlayer::IsFlushingState(FlushStatus state, bool *needShutdown) {
Andreas Huber53df1a42010-12-22 10:03:04 -0800140 switch (state) {
141 case FLUSHING_DECODER:
Andreas Huber1aef2112011-01-04 14:01:29 -0800142 if (needShutdown != NULL) {
143 *needShutdown = false;
Andreas Huber53df1a42010-12-22 10:03:04 -0800144 }
145 return true;
146
Andreas Huber1aef2112011-01-04 14:01:29 -0800147 case FLUSHING_DECODER_SHUTDOWN:
148 if (needShutdown != NULL) {
149 *needShutdown = true;
Andreas Huber53df1a42010-12-22 10:03:04 -0800150 }
151 return true;
152
153 default:
154 return false;
155 }
156}
157
Andreas Huberf9334412010-12-15 15:17:42 -0800158void NuPlayer::onMessageReceived(const sp<AMessage> &msg) {
159 switch (msg->what()) {
160 case kWhatSetDataSource:
161 {
Steve Block3856b092011-10-20 11:56:00 +0100162 ALOGV("kWhatSetDataSource");
Andreas Huberf9334412010-12-15 15:17:42 -0800163
164 CHECK(mSource == NULL);
165
Andreas Huber5bc087c2010-12-23 10:27:40 -0800166 sp<RefBase> obj;
167 CHECK(msg->findObject("source", &obj));
Andreas Huberf9334412010-12-15 15:17:42 -0800168
Andreas Huber5bc087c2010-12-23 10:27:40 -0800169 mSource = static_cast<Source *>(obj.get());
Andreas Huberf9334412010-12-15 15:17:42 -0800170 break;
171 }
172
Glenn Kasten11731182011-02-08 17:26:17 -0800173 case kWhatSetVideoNativeWindow:
Andreas Huberf9334412010-12-15 15:17:42 -0800174 {
Steve Block3856b092011-10-20 11:56:00 +0100175 ALOGV("kWhatSetVideoNativeWindow");
Andreas Huberf9334412010-12-15 15:17:42 -0800176
177 sp<RefBase> obj;
Glenn Kasten11731182011-02-08 17:26:17 -0800178 CHECK(msg->findObject("native-window", &obj));
Andreas Huberf9334412010-12-15 15:17:42 -0800179
Glenn Kasten11731182011-02-08 17:26:17 -0800180 mNativeWindow = static_cast<NativeWindowWrapper *>(obj.get());
Andreas Huberf9334412010-12-15 15:17:42 -0800181 break;
182 }
183
184 case kWhatSetAudioSink:
185 {
Steve Block3856b092011-10-20 11:56:00 +0100186 ALOGV("kWhatSetAudioSink");
Andreas Huberf9334412010-12-15 15:17:42 -0800187
188 sp<RefBase> obj;
189 CHECK(msg->findObject("sink", &obj));
190
191 mAudioSink = static_cast<MediaPlayerBase::AudioSink *>(obj.get());
192 break;
193 }
194
195 case kWhatStart:
196 {
Steve Block3856b092011-10-20 11:56:00 +0100197 ALOGV("kWhatStart");
Andreas Huber43c3e6c2011-01-05 12:17:08 -0800198
Andreas Huber3fe62152011-09-16 15:09:22 -0700199 mVideoIsAVC = false;
Andreas Huber1aef2112011-01-04 14:01:29 -0800200 mAudioEOS = false;
201 mVideoEOS = false;
Andreas Huber32f3cef2011-03-02 15:34:46 -0800202 mSkipRenderingAudioUntilMediaTimeUs = -1;
203 mSkipRenderingVideoUntilMediaTimeUs = -1;
Andreas Huber3fe62152011-09-16 15:09:22 -0700204 mVideoLateByUs = 0;
205 mNumFramesTotal = 0;
206 mNumFramesDropped = 0;
Andreas Huber1aef2112011-01-04 14:01:29 -0800207
Andreas Huber5bc087c2010-12-23 10:27:40 -0800208 mSource->start();
Andreas Huberf9334412010-12-15 15:17:42 -0800209
210 mRenderer = new Renderer(
211 mAudioSink,
212 new AMessage(kWhatRendererNotify, id()));
213
214 looper()->registerHandler(mRenderer);
215
Andreas Huber1aef2112011-01-04 14:01:29 -0800216 postScanSources();
Andreas Huberf9334412010-12-15 15:17:42 -0800217 break;
218 }
219
220 case kWhatScanSources:
221 {
Andreas Huber1aef2112011-01-04 14:01:29 -0800222 int32_t generation;
223 CHECK(msg->findInt32("generation", &generation));
224 if (generation != mScanSourcesGeneration) {
225 // Drop obsolete msg.
226 break;
227 }
228
Andreas Huber5bc087c2010-12-23 10:27:40 -0800229 mScanSourcesPending = false;
230
Steve Block3856b092011-10-20 11:56:00 +0100231 ALOGV("scanning sources haveAudio=%d, haveVideo=%d",
Andreas Huber43c3e6c2011-01-05 12:17:08 -0800232 mAudioDecoder != NULL, mVideoDecoder != NULL);
233
Andreas Huber5bc087c2010-12-23 10:27:40 -0800234 instantiateDecoder(false, &mVideoDecoder);
Andreas Huberf9334412010-12-15 15:17:42 -0800235
236 if (mAudioSink != NULL) {
Andreas Huber5bc087c2010-12-23 10:27:40 -0800237 instantiateDecoder(true, &mAudioDecoder);
Andreas Huberf9334412010-12-15 15:17:42 -0800238 }
239
Andreas Hubereac68ba2011-09-27 12:12:25 -0700240 status_t err;
241 if ((err = mSource->feedMoreTSData()) != OK) {
Andreas Huber1aef2112011-01-04 14:01:29 -0800242 if (mAudioDecoder == NULL && mVideoDecoder == NULL) {
243 // We're not currently decoding anything (no audio or
244 // video tracks found) and we just ran out of input data.
Andreas Hubereac68ba2011-09-27 12:12:25 -0700245
246 if (err == ERROR_END_OF_STREAM) {
247 notifyListener(MEDIA_PLAYBACK_COMPLETE, 0, 0);
248 } else {
249 notifyListener(MEDIA_ERROR, MEDIA_ERROR_UNKNOWN, err);
250 }
Andreas Huber1aef2112011-01-04 14:01:29 -0800251 }
Andreas Huberf9334412010-12-15 15:17:42 -0800252 break;
253 }
254
Andreas Huberf9334412010-12-15 15:17:42 -0800255 if (mAudioDecoder == NULL || mVideoDecoder == NULL) {
256 msg->post(100000ll);
Andreas Huber5bc087c2010-12-23 10:27:40 -0800257 mScanSourcesPending = true;
Andreas Huberf9334412010-12-15 15:17:42 -0800258 }
259 break;
260 }
261
262 case kWhatVideoNotify:
263 case kWhatAudioNotify:
264 {
265 bool audio = msg->what() == kWhatAudioNotify;
266
267 sp<AMessage> codecRequest;
268 CHECK(msg->findMessage("codec-request", &codecRequest));
269
270 int32_t what;
271 CHECK(codecRequest->findInt32("what", &what));
272
273 if (what == ACodec::kWhatFillThisBuffer) {
274 status_t err = feedDecoderInputData(
275 audio, codecRequest);
276
Andreas Huber5bc087c2010-12-23 10:27:40 -0800277 if (err == -EWOULDBLOCK) {
Andreas Hubereac68ba2011-09-27 12:12:25 -0700278 if (mSource->feedMoreTSData() == OK) {
Andreas Huber1183a4a2011-11-03 11:00:21 -0700279 msg->post(10000ll);
Andreas Huber5bc087c2010-12-23 10:27:40 -0800280 }
Andreas Huberf9334412010-12-15 15:17:42 -0800281 }
282 } else if (what == ACodec::kWhatEOS) {
Andreas Huberdc9bacd2011-09-26 10:53:29 -0700283 int32_t err;
284 CHECK(codecRequest->findInt32("err", &err));
285
286 if (err == ERROR_END_OF_STREAM) {
Steve Block3856b092011-10-20 11:56:00 +0100287 ALOGV("got %s decoder EOS", audio ? "audio" : "video");
Andreas Huberdc9bacd2011-09-26 10:53:29 -0700288 } else {
Steve Block3856b092011-10-20 11:56:00 +0100289 ALOGV("got %s decoder EOS w/ error %d",
Andreas Huberdc9bacd2011-09-26 10:53:29 -0700290 audio ? "audio" : "video",
291 err);
292 }
293
294 mRenderer->queueEOS(audio, err);
Andreas Huberf9334412010-12-15 15:17:42 -0800295 } else if (what == ACodec::kWhatFlushCompleted) {
Andreas Huber1aef2112011-01-04 14:01:29 -0800296 bool needShutdown;
Andreas Huber53df1a42010-12-22 10:03:04 -0800297
Andreas Huberf9334412010-12-15 15:17:42 -0800298 if (audio) {
Andreas Huber1aef2112011-01-04 14:01:29 -0800299 CHECK(IsFlushingState(mFlushingAudio, &needShutdown));
Andreas Huberf9334412010-12-15 15:17:42 -0800300 mFlushingAudio = FLUSHED;
301 } else {
Andreas Huber1aef2112011-01-04 14:01:29 -0800302 CHECK(IsFlushingState(mFlushingVideo, &needShutdown));
Andreas Huberf9334412010-12-15 15:17:42 -0800303 mFlushingVideo = FLUSHED;
Andreas Huber3fe62152011-09-16 15:09:22 -0700304
305 mVideoLateByUs = 0;
Andreas Huberf9334412010-12-15 15:17:42 -0800306 }
307
Steve Block3856b092011-10-20 11:56:00 +0100308 ALOGV("decoder %s flush completed", audio ? "audio" : "video");
Andreas Huberf9334412010-12-15 15:17:42 -0800309
Andreas Huber1aef2112011-01-04 14:01:29 -0800310 if (needShutdown) {
Steve Block3856b092011-10-20 11:56:00 +0100311 ALOGV("initiating %s decoder shutdown",
Andreas Huber53df1a42010-12-22 10:03:04 -0800312 audio ? "audio" : "video");
Andreas Huberf9334412010-12-15 15:17:42 -0800313
Andreas Huber53df1a42010-12-22 10:03:04 -0800314 (audio ? mAudioDecoder : mVideoDecoder)->initiateShutdown();
Andreas Huberf9334412010-12-15 15:17:42 -0800315
Andreas Huber53df1a42010-12-22 10:03:04 -0800316 if (audio) {
317 mFlushingAudio = SHUTTING_DOWN_DECODER;
318 } else {
319 mFlushingVideo = SHUTTING_DOWN_DECODER;
320 }
Andreas Huberf9334412010-12-15 15:17:42 -0800321 }
Andreas Huber3831a062010-12-21 10:22:33 -0800322
323 finishFlushIfPossible();
Andreas Huber2c2814b2010-12-15 17:18:20 -0800324 } else if (what == ACodec::kWhatOutputFormatChanged) {
Andreas Huber31e25082011-01-10 10:38:31 -0800325 if (audio) {
326 int32_t numChannels;
327 CHECK(codecRequest->findInt32("channel-count", &numChannels));
Andreas Huber2c2814b2010-12-15 17:18:20 -0800328
Andreas Huber31e25082011-01-10 10:38:31 -0800329 int32_t sampleRate;
330 CHECK(codecRequest->findInt32("sample-rate", &sampleRate));
Andreas Huber2c2814b2010-12-15 17:18:20 -0800331
Steve Block3856b092011-10-20 11:56:00 +0100332 ALOGV("Audio output format changed to %d Hz, %d channels",
Andreas Huber31e25082011-01-10 10:38:31 -0800333 sampleRate, numChannels);
Andreas Huber2c2814b2010-12-15 17:18:20 -0800334
Andreas Huber31e25082011-01-10 10:38:31 -0800335 mAudioSink->close();
Andreas Huber078cfcf2011-09-15 12:25:04 -0700336 CHECK_EQ(mAudioSink->open(
337 sampleRate,
338 numChannels,
339 AUDIO_FORMAT_PCM_16_BIT,
340 8 /* bufferCount */),
341 (status_t)OK);
Andreas Huber31e25082011-01-10 10:38:31 -0800342 mAudioSink->start();
Andreas Huber2c2814b2010-12-15 17:18:20 -0800343
Andreas Huber31e25082011-01-10 10:38:31 -0800344 mRenderer->signalAudioSinkChanged();
345 } else {
346 // video
Andreas Huber3831a062010-12-21 10:22:33 -0800347
Andreas Huber31e25082011-01-10 10:38:31 -0800348 int32_t width, height;
349 CHECK(codecRequest->findInt32("width", &width));
350 CHECK(codecRequest->findInt32("height", &height));
351
352 int32_t cropLeft, cropTop, cropRight, cropBottom;
353 CHECK(codecRequest->findRect(
354 "crop",
355 &cropLeft, &cropTop, &cropRight, &cropBottom));
356
Steve Block3856b092011-10-20 11:56:00 +0100357 ALOGV("Video output format changed to %d x %d "
Andreas Hubercb67cd12011-08-26 16:02:19 -0700358 "(crop: %d x %d @ (%d, %d))",
Andreas Huber31e25082011-01-10 10:38:31 -0800359 width, height,
Andreas Hubercb67cd12011-08-26 16:02:19 -0700360 (cropRight - cropLeft + 1),
361 (cropBottom - cropTop + 1),
362 cropLeft, cropTop);
Andreas Huber31e25082011-01-10 10:38:31 -0800363
364 notifyListener(
365 MEDIA_SET_VIDEO_SIZE,
366 cropRight - cropLeft + 1,
367 cropBottom - cropTop + 1);
368 }
Andreas Huber3831a062010-12-21 10:22:33 -0800369 } else if (what == ACodec::kWhatShutdownCompleted) {
Steve Block3856b092011-10-20 11:56:00 +0100370 ALOGV("%s shutdown completed", audio ? "audio" : "video");
Andreas Huber3831a062010-12-21 10:22:33 -0800371 if (audio) {
372 mAudioDecoder.clear();
373
374 CHECK_EQ((int)mFlushingAudio, (int)SHUTTING_DOWN_DECODER);
375 mFlushingAudio = SHUT_DOWN;
376 } else {
377 mVideoDecoder.clear();
378
379 CHECK_EQ((int)mFlushingVideo, (int)SHUTTING_DOWN_DECODER);
380 mFlushingVideo = SHUT_DOWN;
381 }
382
383 finishFlushIfPossible();
Andreas Huberc92fd242011-08-16 13:48:44 -0700384 } else if (what == ACodec::kWhatError) {
Steve Block29357bc2012-01-06 19:20:56 +0000385 ALOGE("Received error from %s decoder, aborting playback.",
Andreas Huberc92fd242011-08-16 13:48:44 -0700386 audio ? "audio" : "video");
387
388 mRenderer->queueEOS(audio, UNKNOWN_ERROR);
Andreas Huber57788222012-02-21 11:47:18 -0800389 } else if (what == ACodec::kWhatDrainThisBuffer) {
Andreas Huberf9334412010-12-15 15:17:42 -0800390 renderBuffer(audio, codecRequest);
Andreas Huber57788222012-02-21 11:47:18 -0800391 } else {
392 ALOGV("Unhandled codec notification %d.", what);
Andreas Huberf9334412010-12-15 15:17:42 -0800393 }
394
395 break;
396 }
397
398 case kWhatRendererNotify:
399 {
400 int32_t what;
401 CHECK(msg->findInt32("what", &what));
402
403 if (what == Renderer::kWhatEOS) {
404 int32_t audio;
405 CHECK(msg->findInt32("audio", &audio));
406
Andreas Huberc92fd242011-08-16 13:48:44 -0700407 int32_t finalResult;
408 CHECK(msg->findInt32("finalResult", &finalResult));
409
Andreas Huberf9334412010-12-15 15:17:42 -0800410 if (audio) {
411 mAudioEOS = true;
412 } else {
413 mVideoEOS = true;
414 }
415
Andreas Huberc92fd242011-08-16 13:48:44 -0700416 if (finalResult == ERROR_END_OF_STREAM) {
Steve Block3856b092011-10-20 11:56:00 +0100417 ALOGV("reached %s EOS", audio ? "audio" : "video");
Andreas Huberc92fd242011-08-16 13:48:44 -0700418 } else {
Steve Block29357bc2012-01-06 19:20:56 +0000419 ALOGE("%s track encountered an error (%d)",
Andreas Huberc92fd242011-08-16 13:48:44 -0700420 audio ? "audio" : "video", finalResult);
421
422 notifyListener(
423 MEDIA_ERROR, MEDIA_ERROR_UNKNOWN, finalResult);
424 }
Andreas Huberf9334412010-12-15 15:17:42 -0800425
426 if ((mAudioEOS || mAudioDecoder == NULL)
427 && (mVideoEOS || mVideoDecoder == NULL)) {
428 notifyListener(MEDIA_PLAYBACK_COMPLETE, 0, 0);
429 }
Andreas Huber43c3e6c2011-01-05 12:17:08 -0800430 } else if (what == Renderer::kWhatPosition) {
431 int64_t positionUs;
432 CHECK(msg->findInt64("positionUs", &positionUs));
433
Andreas Huber3fe62152011-09-16 15:09:22 -0700434 CHECK(msg->findInt64("videoLateByUs", &mVideoLateByUs));
435
Andreas Huber43c3e6c2011-01-05 12:17:08 -0800436 if (mDriver != NULL) {
437 sp<NuPlayerDriver> driver = mDriver.promote();
438 if (driver != NULL) {
439 driver->notifyPosition(positionUs);
Andreas Huber3fe62152011-09-16 15:09:22 -0700440
441 driver->notifyFrameStats(
442 mNumFramesTotal, mNumFramesDropped);
Andreas Huber43c3e6c2011-01-05 12:17:08 -0800443 }
444 }
Andreas Huber3fe62152011-09-16 15:09:22 -0700445 } else if (what == Renderer::kWhatFlushComplete) {
Andreas Huberf9334412010-12-15 15:17:42 -0800446 CHECK_EQ(what, (int32_t)Renderer::kWhatFlushComplete);
447
448 int32_t audio;
449 CHECK(msg->findInt32("audio", &audio));
450
Steve Block3856b092011-10-20 11:56:00 +0100451 ALOGV("renderer %s flush completed.", audio ? "audio" : "video");
Andreas Huberf9334412010-12-15 15:17:42 -0800452 }
453 break;
454 }
455
456 case kWhatMoreDataQueued:
457 {
458 break;
459 }
460
Andreas Huber1aef2112011-01-04 14:01:29 -0800461 case kWhatReset:
462 {
Steve Block3856b092011-10-20 11:56:00 +0100463 ALOGV("kWhatReset");
Andreas Huber1aef2112011-01-04 14:01:29 -0800464
Andreas Huberb58ce9f2011-11-28 16:27:35 -0800465 if (mRenderer != NULL) {
466 // There's an edge case where the renderer owns all output
467 // buffers and is paused, therefore the decoder will not read
468 // more input data and will never encounter the matching
469 // discontinuity. To avoid this, we resume the renderer.
470
471 if (mFlushingAudio == AWAITING_DISCONTINUITY
472 || mFlushingVideo == AWAITING_DISCONTINUITY) {
473 mRenderer->resume();
474 }
475 }
476
Andreas Huber1aef2112011-01-04 14:01:29 -0800477 if (mFlushingAudio != NONE || mFlushingVideo != NONE) {
478 // We're currently flushing, postpone the reset until that's
479 // completed.
480
Andreas Huberea9d51b2011-11-30 09:53:40 -0800481 ALOGV("postponing reset mFlushingAudio=%d, mFlushingVideo=%d",
482 mFlushingAudio, mFlushingVideo);
Andreas Huber1aef2112011-01-04 14:01:29 -0800483
484 mResetPostponed = true;
485 break;
486 }
487
488 if (mAudioDecoder == NULL && mVideoDecoder == NULL) {
489 finishReset();
490 break;
491 }
492
Andreas Huber6e3d3112011-11-28 12:36:11 -0800493 mTimeDiscontinuityPending = true;
494
Andreas Huber1aef2112011-01-04 14:01:29 -0800495 if (mAudioDecoder != NULL) {
496 flushDecoder(true /* audio */, true /* needShutdown */);
497 }
498
499 if (mVideoDecoder != NULL) {
500 flushDecoder(false /* audio */, true /* needShutdown */);
501 }
502
503 mResetInProgress = true;
504 break;
505 }
506
Andreas Huber43c3e6c2011-01-05 12:17:08 -0800507 case kWhatSeek:
508 {
509 int64_t seekTimeUs;
510 CHECK(msg->findInt64("seekTimeUs", &seekTimeUs));
511
Steve Block3856b092011-10-20 11:56:00 +0100512 ALOGV("kWhatSeek seekTimeUs=%lld us (%.2f secs)",
Andreas Huber43c3e6c2011-01-05 12:17:08 -0800513 seekTimeUs, seekTimeUs / 1E6);
514
515 mSource->seekTo(seekTimeUs);
516
517 if (mDriver != NULL) {
518 sp<NuPlayerDriver> driver = mDriver.promote();
519 if (driver != NULL) {
520 driver->notifySeekComplete();
521 }
522 }
523
524 break;
525 }
526
Andreas Huberb4082222011-01-20 15:23:04 -0800527 case kWhatPause:
528 {
529 CHECK(mRenderer != NULL);
530 mRenderer->pause();
531 break;
532 }
533
534 case kWhatResume:
535 {
536 CHECK(mRenderer != NULL);
537 mRenderer->resume();
538 break;
539 }
540
Andreas Huberf9334412010-12-15 15:17:42 -0800541 default:
542 TRESPASS();
543 break;
544 }
545}
546
Andreas Huber3831a062010-12-21 10:22:33 -0800547void NuPlayer::finishFlushIfPossible() {
548 if (mFlushingAudio != FLUSHED && mFlushingAudio != SHUT_DOWN) {
549 return;
550 }
551
552 if (mFlushingVideo != FLUSHED && mFlushingVideo != SHUT_DOWN) {
553 return;
554 }
555
Steve Block3856b092011-10-20 11:56:00 +0100556 ALOGV("both audio and video are flushed now.");
Andreas Huber3831a062010-12-21 10:22:33 -0800557
Andreas Huber6e3d3112011-11-28 12:36:11 -0800558 if (mTimeDiscontinuityPending) {
559 mRenderer->signalTimeDiscontinuity();
560 mTimeDiscontinuityPending = false;
561 }
Andreas Huber3831a062010-12-21 10:22:33 -0800562
Andreas Huber22fc52f2011-01-05 16:24:27 -0800563 if (mAudioDecoder != NULL) {
Andreas Huber3831a062010-12-21 10:22:33 -0800564 mAudioDecoder->signalResume();
565 }
566
Andreas Huber22fc52f2011-01-05 16:24:27 -0800567 if (mVideoDecoder != NULL) {
Andreas Huber3831a062010-12-21 10:22:33 -0800568 mVideoDecoder->signalResume();
569 }
570
571 mFlushingAudio = NONE;
572 mFlushingVideo = NONE;
Andreas Huber3831a062010-12-21 10:22:33 -0800573
Andreas Huber1aef2112011-01-04 14:01:29 -0800574 if (mResetInProgress) {
Steve Block3856b092011-10-20 11:56:00 +0100575 ALOGV("reset completed");
Andreas Huber1aef2112011-01-04 14:01:29 -0800576
577 mResetInProgress = false;
578 finishReset();
579 } else if (mResetPostponed) {
580 (new AMessage(kWhatReset, id()))->post();
581 mResetPostponed = false;
Andreas Huber22fc52f2011-01-05 16:24:27 -0800582 } else if (mAudioDecoder == NULL || mVideoDecoder == NULL) {
Andreas Huber1aef2112011-01-04 14:01:29 -0800583 postScanSources();
Andreas Huberf9334412010-12-15 15:17:42 -0800584 }
585}
586
Andreas Huber1aef2112011-01-04 14:01:29 -0800587void NuPlayer::finishReset() {
588 CHECK(mAudioDecoder == NULL);
589 CHECK(mVideoDecoder == NULL);
590
Andreas Huber2bfdd422011-10-11 15:24:07 -0700591 ++mScanSourcesGeneration;
592 mScanSourcesPending = false;
593
Andreas Huber1aef2112011-01-04 14:01:29 -0800594 mRenderer.clear();
Andreas Huber2bfdd422011-10-11 15:24:07 -0700595
596 if (mSource != NULL) {
597 mSource->stop();
598 mSource.clear();
599 }
Andreas Huber1aef2112011-01-04 14:01:29 -0800600
Andreas Huber43c3e6c2011-01-05 12:17:08 -0800601 if (mDriver != NULL) {
602 sp<NuPlayerDriver> driver = mDriver.promote();
603 if (driver != NULL) {
604 driver->notifyResetComplete();
605 }
606 }
Andreas Huber1aef2112011-01-04 14:01:29 -0800607}
608
609void NuPlayer::postScanSources() {
610 if (mScanSourcesPending) {
611 return;
612 }
613
614 sp<AMessage> msg = new AMessage(kWhatScanSources, id());
615 msg->setInt32("generation", mScanSourcesGeneration);
616 msg->post();
617
618 mScanSourcesPending = true;
619}
620
Andreas Huber5bc087c2010-12-23 10:27:40 -0800621status_t NuPlayer::instantiateDecoder(bool audio, sp<Decoder> *decoder) {
Andreas Huberf9334412010-12-15 15:17:42 -0800622 if (*decoder != NULL) {
623 return OK;
624 }
625
Andreas Huber5bc087c2010-12-23 10:27:40 -0800626 sp<MetaData> meta = mSource->getFormat(audio);
Andreas Huberf9334412010-12-15 15:17:42 -0800627
Andreas Huber5bc087c2010-12-23 10:27:40 -0800628 if (meta == NULL) {
Andreas Huberf9334412010-12-15 15:17:42 -0800629 return -EWOULDBLOCK;
630 }
631
Andreas Huber3fe62152011-09-16 15:09:22 -0700632 if (!audio) {
633 const char *mime;
634 CHECK(meta->findCString(kKeyMIMEType, &mime));
635 mVideoIsAVC = !strcasecmp(MEDIA_MIMETYPE_VIDEO_AVC, mime);
636 }
637
Andreas Huberf9334412010-12-15 15:17:42 -0800638 sp<AMessage> notify =
639 new AMessage(audio ? kWhatAudioNotify : kWhatVideoNotify,
640 id());
641
Glenn Kasten11731182011-02-08 17:26:17 -0800642 *decoder = audio ? new Decoder(notify) :
643 new Decoder(notify, mNativeWindow);
Andreas Huberf9334412010-12-15 15:17:42 -0800644 looper()->registerHandler(*decoder);
645
Andreas Huber5bc087c2010-12-23 10:27:40 -0800646 (*decoder)->configure(meta);
Andreas Huberf9334412010-12-15 15:17:42 -0800647
Andreas Huber43c3e6c2011-01-05 12:17:08 -0800648 int64_t durationUs;
649 if (mDriver != NULL && mSource->getDuration(&durationUs) == OK) {
650 sp<NuPlayerDriver> driver = mDriver.promote();
651 if (driver != NULL) {
652 driver->notifyDuration(durationUs);
653 }
654 }
655
Andreas Huberf9334412010-12-15 15:17:42 -0800656 return OK;
657}
658
659status_t NuPlayer::feedDecoderInputData(bool audio, const sp<AMessage> &msg) {
660 sp<AMessage> reply;
661 CHECK(msg->findMessage("reply", &reply));
662
Andreas Huber53df1a42010-12-22 10:03:04 -0800663 if ((audio && IsFlushingState(mFlushingAudio))
664 || (!audio && IsFlushingState(mFlushingVideo))) {
Andreas Huberf9334412010-12-15 15:17:42 -0800665 reply->setInt32("err", INFO_DISCONTINUITY);
666 reply->post();
667 return OK;
668 }
669
670 sp<ABuffer> accessUnit;
Andreas Huberf9334412010-12-15 15:17:42 -0800671
Andreas Huber3fe62152011-09-16 15:09:22 -0700672 bool dropAccessUnit;
673 do {
674 status_t err = mSource->dequeueAccessUnit(audio, &accessUnit);
Andreas Huber5bc087c2010-12-23 10:27:40 -0800675
Andreas Huber3fe62152011-09-16 15:09:22 -0700676 if (err == -EWOULDBLOCK) {
677 return err;
678 } else if (err != OK) {
679 if (err == INFO_DISCONTINUITY) {
680 int32_t type;
681 CHECK(accessUnit->meta()->findInt32("discontinuity", &type));
Andreas Huber53df1a42010-12-22 10:03:04 -0800682
Andreas Huber3fe62152011-09-16 15:09:22 -0700683 bool formatChange =
Andreas Huber6e3d3112011-11-28 12:36:11 -0800684 (audio &&
685 (type & ATSParser::DISCONTINUITY_AUDIO_FORMAT))
686 || (!audio &&
687 (type & ATSParser::DISCONTINUITY_VIDEO_FORMAT));
Andreas Huber53df1a42010-12-22 10:03:04 -0800688
Andreas Huber6e3d3112011-11-28 12:36:11 -0800689 bool timeChange = (type & ATSParser::DISCONTINUITY_TIME) != 0;
690
Steve Blockdf64d152012-01-04 20:05:49 +0000691 ALOGI("%s discontinuity (formatChange=%d, time=%d)",
Andreas Huber6e3d3112011-11-28 12:36:11 -0800692 audio ? "audio" : "video", formatChange, timeChange);
Andreas Huber32f3cef2011-03-02 15:34:46 -0800693
Andreas Huber3fe62152011-09-16 15:09:22 -0700694 if (audio) {
695 mSkipRenderingAudioUntilMediaTimeUs = -1;
696 } else {
697 mSkipRenderingVideoUntilMediaTimeUs = -1;
698 }
Andreas Huber32f3cef2011-03-02 15:34:46 -0800699
Andreas Huber6e3d3112011-11-28 12:36:11 -0800700 if (timeChange) {
701 sp<AMessage> extra;
702 if (accessUnit->meta()->findMessage("extra", &extra)
703 && extra != NULL) {
704 int64_t resumeAtMediaTimeUs;
705 if (extra->findInt64(
706 "resume-at-mediatimeUs", &resumeAtMediaTimeUs)) {
Steve Blockdf64d152012-01-04 20:05:49 +0000707 ALOGI("suppressing rendering of %s until %lld us",
Andreas Huber6e3d3112011-11-28 12:36:11 -0800708 audio ? "audio" : "video", resumeAtMediaTimeUs);
Andreas Huber3fe62152011-09-16 15:09:22 -0700709
Andreas Huber6e3d3112011-11-28 12:36:11 -0800710 if (audio) {
711 mSkipRenderingAudioUntilMediaTimeUs =
712 resumeAtMediaTimeUs;
713 } else {
714 mSkipRenderingVideoUntilMediaTimeUs =
715 resumeAtMediaTimeUs;
716 }
Andreas Huber3fe62152011-09-16 15:09:22 -0700717 }
Andreas Huber32f3cef2011-03-02 15:34:46 -0800718 }
719 }
Andreas Huber3fe62152011-09-16 15:09:22 -0700720
Andreas Huber6e3d3112011-11-28 12:36:11 -0800721 mTimeDiscontinuityPending =
722 mTimeDiscontinuityPending || timeChange;
723
724 if (formatChange || timeChange) {
725 flushDecoder(audio, formatChange);
726 } else {
727 // This stream is unaffected by the discontinuity
728
729 if (audio) {
730 mFlushingAudio = FLUSHED;
731 } else {
732 mFlushingVideo = FLUSHED;
733 }
734
735 finishFlushIfPossible();
736
737 return -EWOULDBLOCK;
738 }
Andreas Huber32f3cef2011-03-02 15:34:46 -0800739 }
740
Andreas Huber3fe62152011-09-16 15:09:22 -0700741 reply->setInt32("err", err);
742 reply->post();
743 return OK;
Andreas Huberf9334412010-12-15 15:17:42 -0800744 }
745
Andreas Huber3fe62152011-09-16 15:09:22 -0700746 if (!audio) {
747 ++mNumFramesTotal;
748 }
749
750 dropAccessUnit = false;
751 if (!audio
752 && mVideoLateByUs > 100000ll
753 && mVideoIsAVC
754 && !IsAVCReferenceFrame(accessUnit)) {
755 dropAccessUnit = true;
756 ++mNumFramesDropped;
757 }
758 } while (dropAccessUnit);
Andreas Huberf9334412010-12-15 15:17:42 -0800759
Steve Block3856b092011-10-20 11:56:00 +0100760 // ALOGV("returned a valid buffer of %s data", audio ? "audio" : "video");
Andreas Huberf9334412010-12-15 15:17:42 -0800761
762#if 0
763 int64_t mediaTimeUs;
764 CHECK(accessUnit->meta()->findInt64("timeUs", &mediaTimeUs));
Steve Block3856b092011-10-20 11:56:00 +0100765 ALOGV("feeding %s input buffer at media time %.2f secs",
Andreas Huberf9334412010-12-15 15:17:42 -0800766 audio ? "audio" : "video",
767 mediaTimeUs / 1E6);
768#endif
769
Andreas Huber2d8bedd2012-02-21 14:38:23 -0800770 reply->setBuffer("buffer", accessUnit);
Andreas Huberf9334412010-12-15 15:17:42 -0800771 reply->post();
772
773 return OK;
774}
775
776void NuPlayer::renderBuffer(bool audio, const sp<AMessage> &msg) {
Steve Block3856b092011-10-20 11:56:00 +0100777 // ALOGV("renderBuffer %s", audio ? "audio" : "video");
Andreas Huberf9334412010-12-15 15:17:42 -0800778
779 sp<AMessage> reply;
780 CHECK(msg->findMessage("reply", &reply));
781
Andreas Huber18ac5402011-08-31 15:04:25 -0700782 if (IsFlushingState(audio ? mFlushingAudio : mFlushingVideo)) {
783 // We're currently attempting to flush the decoder, in order
784 // to complete this, the decoder wants all its buffers back,
785 // so we don't want any output buffers it sent us (from before
786 // we initiated the flush) to be stuck in the renderer's queue.
787
Steve Block3856b092011-10-20 11:56:00 +0100788 ALOGV("we're still flushing the %s decoder, sending its output buffer"
Andreas Huber18ac5402011-08-31 15:04:25 -0700789 " right back.", audio ? "audio" : "video");
790
791 reply->post();
792 return;
793 }
794
Andreas Huber2d8bedd2012-02-21 14:38:23 -0800795 sp<ABuffer> buffer;
796 CHECK(msg->findBuffer("buffer", &buffer));
Andreas Huberf9334412010-12-15 15:17:42 -0800797
Andreas Huber32f3cef2011-03-02 15:34:46 -0800798 int64_t &skipUntilMediaTimeUs =
799 audio
800 ? mSkipRenderingAudioUntilMediaTimeUs
801 : mSkipRenderingVideoUntilMediaTimeUs;
802
803 if (skipUntilMediaTimeUs >= 0) {
804 int64_t mediaTimeUs;
805 CHECK(buffer->meta()->findInt64("timeUs", &mediaTimeUs));
806
807 if (mediaTimeUs < skipUntilMediaTimeUs) {
Steve Block3856b092011-10-20 11:56:00 +0100808 ALOGV("dropping %s buffer at time %lld as requested.",
Andreas Huber32f3cef2011-03-02 15:34:46 -0800809 audio ? "audio" : "video",
810 mediaTimeUs);
811
812 reply->post();
813 return;
814 }
815
816 skipUntilMediaTimeUs = -1;
817 }
818
Andreas Huberf9334412010-12-15 15:17:42 -0800819 mRenderer->queueBuffer(audio, buffer, reply);
820}
821
822void NuPlayer::notifyListener(int msg, int ext1, int ext2) {
Andreas Huber43c3e6c2011-01-05 12:17:08 -0800823 if (mDriver == NULL) {
Andreas Huberf9334412010-12-15 15:17:42 -0800824 return;
825 }
826
Andreas Huber43c3e6c2011-01-05 12:17:08 -0800827 sp<NuPlayerDriver> driver = mDriver.promote();
Andreas Huberf9334412010-12-15 15:17:42 -0800828
Andreas Huber43c3e6c2011-01-05 12:17:08 -0800829 if (driver == NULL) {
Andreas Huberf9334412010-12-15 15:17:42 -0800830 return;
831 }
832
Andreas Hubera4af2142011-10-26 15:23:31 -0700833 driver->notifyListener(msg, ext1, ext2);
Andreas Huberf9334412010-12-15 15:17:42 -0800834}
835
Andreas Huber1aef2112011-01-04 14:01:29 -0800836void NuPlayer::flushDecoder(bool audio, bool needShutdown) {
Andreas Huber6e3d3112011-11-28 12:36:11 -0800837 if ((audio && mAudioDecoder == NULL) || (!audio && mVideoDecoder == NULL)) {
Steve Blockdf64d152012-01-04 20:05:49 +0000838 ALOGI("flushDecoder %s without decoder present",
Andreas Huber6e3d3112011-11-28 12:36:11 -0800839 audio ? "audio" : "video");
840 }
841
Andreas Huber1aef2112011-01-04 14:01:29 -0800842 // Make sure we don't continue to scan sources until we finish flushing.
843 ++mScanSourcesGeneration;
Andreas Huber43c3e6c2011-01-05 12:17:08 -0800844 mScanSourcesPending = false;
Andreas Huber1aef2112011-01-04 14:01:29 -0800845
846 (audio ? mAudioDecoder : mVideoDecoder)->signalFlush();
847 mRenderer->flush(audio);
848
849 FlushStatus newStatus =
850 needShutdown ? FLUSHING_DECODER_SHUTDOWN : FLUSHING_DECODER;
851
852 if (audio) {
853 CHECK(mFlushingAudio == NONE
854 || mFlushingAudio == AWAITING_DISCONTINUITY);
855
856 mFlushingAudio = newStatus;
857
858 if (mFlushingVideo == NONE) {
859 mFlushingVideo = (mVideoDecoder != NULL)
860 ? AWAITING_DISCONTINUITY
861 : FLUSHED;
862 }
863 } else {
864 CHECK(mFlushingVideo == NONE
865 || mFlushingVideo == AWAITING_DISCONTINUITY);
866
867 mFlushingVideo = newStatus;
868
869 if (mFlushingAudio == NONE) {
870 mFlushingAudio = (mAudioDecoder != NULL)
871 ? AWAITING_DISCONTINUITY
872 : FLUSHED;
873 }
874 }
875}
876
Andreas Huberf9334412010-12-15 15:17:42 -0800877} // namespace android