blob: 544d501fb3ea0f39fc54349b709482c3030f17af [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"
Andreas Huberafed0e12011-09-20 15:39:58 -070030#include "GenericSource.h"
Andreas Huber5bc087c2010-12-23 10:27:40 -080031
32#include "ATSParser.h"
Andreas Huberf9334412010-12-15 15:17:42 -080033
Andreas Huber3831a062010-12-21 10:22:33 -080034#include <media/stagefright/foundation/hexdump.h>
Andreas Huberf9334412010-12-15 15:17:42 -080035#include <media/stagefright/foundation/ABuffer.h>
36#include <media/stagefright/foundation/ADebug.h>
37#include <media/stagefright/foundation/AMessage.h>
38#include <media/stagefright/ACodec.h>
Andreas Huber3fe62152011-09-16 15:09:22 -070039#include <media/stagefright/MediaDefs.h>
Andreas Huberf9334412010-12-15 15:17:42 -080040#include <media/stagefright/MediaErrors.h>
41#include <media/stagefright/MetaData.h>
Glenn Kasten11731182011-02-08 17:26:17 -080042#include <gui/ISurfaceTexture.h>
Andreas Huberf9334412010-12-15 15:17:42 -080043
Andreas Huber3fe62152011-09-16 15:09:22 -070044#include "avc_utils.h"
45
Andreas Huberf9334412010-12-15 15:17:42 -080046namespace android {
47
48////////////////////////////////////////////////////////////////////////////////
49
50NuPlayer::NuPlayer()
Andreas Huber9b80c2b2011-06-30 15:47:02 -070051 : mUIDValid(false),
Andreas Huber3fe62152011-09-16 15:09:22 -070052 mVideoIsAVC(false),
Andreas Huber9b80c2b2011-06-30 15:47:02 -070053 mAudioEOS(false),
Andreas Huberf9334412010-12-15 15:17:42 -080054 mVideoEOS(false),
Andreas Huber5bc087c2010-12-23 10:27:40 -080055 mScanSourcesPending(false),
Andreas Huber1aef2112011-01-04 14:01:29 -080056 mScanSourcesGeneration(0),
Andreas Huber6e3d3112011-11-28 12:36:11 -080057 mTimeDiscontinuityPending(false),
Andreas Huberf9334412010-12-15 15:17:42 -080058 mFlushingAudio(NONE),
Andreas Huber1aef2112011-01-04 14:01:29 -080059 mFlushingVideo(NONE),
60 mResetInProgress(false),
Andreas Huber3fe62152011-09-16 15:09:22 -070061 mResetPostponed(false),
62 mSkipRenderingAudioUntilMediaTimeUs(-1ll),
63 mSkipRenderingVideoUntilMediaTimeUs(-1ll),
64 mVideoLateByUs(0ll),
65 mNumFramesTotal(0ll),
66 mNumFramesDropped(0ll) {
Andreas Huberf9334412010-12-15 15:17:42 -080067}
68
69NuPlayer::~NuPlayer() {
70}
71
Andreas Huber9b80c2b2011-06-30 15:47:02 -070072void NuPlayer::setUID(uid_t uid) {
73 mUIDValid = true;
74 mUID = uid;
75}
76
Andreas Huber43c3e6c2011-01-05 12:17:08 -080077void NuPlayer::setDriver(const wp<NuPlayerDriver> &driver) {
78 mDriver = driver;
Andreas Huberf9334412010-12-15 15:17:42 -080079}
80
81void NuPlayer::setDataSource(const sp<IStreamSource> &source) {
82 sp<AMessage> msg = new AMessage(kWhatSetDataSource, id());
83
Andreas Huber5bc087c2010-12-23 10:27:40 -080084 msg->setObject("source", new StreamingSource(source));
85 msg->post();
86}
Andreas Huberf9334412010-12-15 15:17:42 -080087
Andreas Huberafed0e12011-09-20 15:39:58 -070088static bool IsHTTPLiveURL(const char *url) {
89 if (!strncasecmp("http://", url, 7)
90 || !strncasecmp("https://", url, 8)) {
91 size_t len = strlen(url);
92 if (len >= 5 && !strcasecmp(".m3u8", &url[len - 5])) {
93 return true;
94 }
95
96 if (strstr(url,"m3u8")) {
97 return true;
98 }
99 }
100
101 return false;
102}
103
Andreas Huber5bc087c2010-12-23 10:27:40 -0800104void NuPlayer::setDataSource(
105 const char *url, const KeyedVector<String8, String8> *headers) {
106 sp<AMessage> msg = new AMessage(kWhatSetDataSource, id());
107
Andreas Huberafed0e12011-09-20 15:39:58 -0700108 sp<Source> source;
109 if (IsHTTPLiveURL(url)) {
110 source = new HTTPLiveSource(url, headers, mUIDValid, mUID);
111 } else if (!strncasecmp(url, "rtsp://", 7)) {
112 source = new RTSPSource(url, headers, mUIDValid, mUID);
Andreas Huber2bfdd422011-10-11 15:24:07 -0700113 } else {
Andreas Huberafed0e12011-09-20 15:39:58 -0700114 source = new GenericSource(url, headers, mUIDValid, mUID);
Andreas Huber2bfdd422011-10-11 15:24:07 -0700115 }
116
Andreas Huberafed0e12011-09-20 15:39:58 -0700117 msg->setObject("source", source);
118 msg->post();
119}
120
121void NuPlayer::setDataSource(int fd, int64_t offset, int64_t length) {
122 sp<AMessage> msg = new AMessage(kWhatSetDataSource, id());
123
124 sp<Source> source = new GenericSource(fd, offset, length);
125 msg->setObject("source", source);
Andreas Huberf9334412010-12-15 15:17:42 -0800126 msg->post();
127}
128
Glenn Kasten11731182011-02-08 17:26:17 -0800129void NuPlayer::setVideoSurfaceTexture(const sp<ISurfaceTexture> &surfaceTexture) {
130 sp<AMessage> msg = new AMessage(kWhatSetVideoNativeWindow, id());
131 sp<SurfaceTextureClient> surfaceTextureClient(surfaceTexture != NULL ?
132 new SurfaceTextureClient(surfaceTexture) : NULL);
133 msg->setObject("native-window", new NativeWindowWrapper(surfaceTextureClient));
Andreas Huberf9334412010-12-15 15:17:42 -0800134 msg->post();
135}
136
137void NuPlayer::setAudioSink(const sp<MediaPlayerBase::AudioSink> &sink) {
138 sp<AMessage> msg = new AMessage(kWhatSetAudioSink, id());
139 msg->setObject("sink", sink);
140 msg->post();
141}
142
143void NuPlayer::start() {
144 (new AMessage(kWhatStart, id()))->post();
145}
146
Andreas Huber43c3e6c2011-01-05 12:17:08 -0800147void NuPlayer::pause() {
Andreas Huberb4082222011-01-20 15:23:04 -0800148 (new AMessage(kWhatPause, id()))->post();
Andreas Huber43c3e6c2011-01-05 12:17:08 -0800149}
150
151void NuPlayer::resume() {
Andreas Huberb4082222011-01-20 15:23:04 -0800152 (new AMessage(kWhatResume, id()))->post();
Andreas Huber43c3e6c2011-01-05 12:17:08 -0800153}
154
Andreas Huber1aef2112011-01-04 14:01:29 -0800155void NuPlayer::resetAsync() {
156 (new AMessage(kWhatReset, id()))->post();
157}
158
Andreas Huber43c3e6c2011-01-05 12:17:08 -0800159void NuPlayer::seekToAsync(int64_t seekTimeUs) {
160 sp<AMessage> msg = new AMessage(kWhatSeek, id());
161 msg->setInt64("seekTimeUs", seekTimeUs);
162 msg->post();
163}
164
Andreas Huber53df1a42010-12-22 10:03:04 -0800165// static
Andreas Huber1aef2112011-01-04 14:01:29 -0800166bool NuPlayer::IsFlushingState(FlushStatus state, bool *needShutdown) {
Andreas Huber53df1a42010-12-22 10:03:04 -0800167 switch (state) {
168 case FLUSHING_DECODER:
Andreas Huber1aef2112011-01-04 14:01:29 -0800169 if (needShutdown != NULL) {
170 *needShutdown = false;
Andreas Huber53df1a42010-12-22 10:03:04 -0800171 }
172 return true;
173
Andreas Huber1aef2112011-01-04 14:01:29 -0800174 case FLUSHING_DECODER_SHUTDOWN:
175 if (needShutdown != NULL) {
176 *needShutdown = true;
Andreas Huber53df1a42010-12-22 10:03:04 -0800177 }
178 return true;
179
180 default:
181 return false;
182 }
183}
184
Andreas Huberf9334412010-12-15 15:17:42 -0800185void NuPlayer::onMessageReceived(const sp<AMessage> &msg) {
186 switch (msg->what()) {
187 case kWhatSetDataSource:
188 {
Steve Block3856b092011-10-20 11:56:00 +0100189 ALOGV("kWhatSetDataSource");
Andreas Huberf9334412010-12-15 15:17:42 -0800190
191 CHECK(mSource == NULL);
192
Andreas Huber5bc087c2010-12-23 10:27:40 -0800193 sp<RefBase> obj;
194 CHECK(msg->findObject("source", &obj));
Andreas Huberf9334412010-12-15 15:17:42 -0800195
Andreas Huber5bc087c2010-12-23 10:27:40 -0800196 mSource = static_cast<Source *>(obj.get());
Andreas Huberf9334412010-12-15 15:17:42 -0800197 break;
198 }
199
Glenn Kasten11731182011-02-08 17:26:17 -0800200 case kWhatSetVideoNativeWindow:
Andreas Huberf9334412010-12-15 15:17:42 -0800201 {
Steve Block3856b092011-10-20 11:56:00 +0100202 ALOGV("kWhatSetVideoNativeWindow");
Andreas Huberf9334412010-12-15 15:17:42 -0800203
204 sp<RefBase> obj;
Glenn Kasten11731182011-02-08 17:26:17 -0800205 CHECK(msg->findObject("native-window", &obj));
Andreas Huberf9334412010-12-15 15:17:42 -0800206
Glenn Kasten11731182011-02-08 17:26:17 -0800207 mNativeWindow = static_cast<NativeWindowWrapper *>(obj.get());
Andreas Huberf9334412010-12-15 15:17:42 -0800208 break;
209 }
210
211 case kWhatSetAudioSink:
212 {
Steve Block3856b092011-10-20 11:56:00 +0100213 ALOGV("kWhatSetAudioSink");
Andreas Huberf9334412010-12-15 15:17:42 -0800214
215 sp<RefBase> obj;
216 CHECK(msg->findObject("sink", &obj));
217
218 mAudioSink = static_cast<MediaPlayerBase::AudioSink *>(obj.get());
219 break;
220 }
221
222 case kWhatStart:
223 {
Steve Block3856b092011-10-20 11:56:00 +0100224 ALOGV("kWhatStart");
Andreas Huber43c3e6c2011-01-05 12:17:08 -0800225
Andreas Huber3fe62152011-09-16 15:09:22 -0700226 mVideoIsAVC = false;
Andreas Huber1aef2112011-01-04 14:01:29 -0800227 mAudioEOS = false;
228 mVideoEOS = false;
Andreas Huber32f3cef2011-03-02 15:34:46 -0800229 mSkipRenderingAudioUntilMediaTimeUs = -1;
230 mSkipRenderingVideoUntilMediaTimeUs = -1;
Andreas Huber3fe62152011-09-16 15:09:22 -0700231 mVideoLateByUs = 0;
232 mNumFramesTotal = 0;
233 mNumFramesDropped = 0;
Andreas Huber1aef2112011-01-04 14:01:29 -0800234
Andreas Huber5bc087c2010-12-23 10:27:40 -0800235 mSource->start();
Andreas Huberf9334412010-12-15 15:17:42 -0800236
237 mRenderer = new Renderer(
238 mAudioSink,
239 new AMessage(kWhatRendererNotify, id()));
240
241 looper()->registerHandler(mRenderer);
242
Andreas Huber1aef2112011-01-04 14:01:29 -0800243 postScanSources();
Andreas Huberf9334412010-12-15 15:17:42 -0800244 break;
245 }
246
247 case kWhatScanSources:
248 {
Andreas Huber1aef2112011-01-04 14:01:29 -0800249 int32_t generation;
250 CHECK(msg->findInt32("generation", &generation));
251 if (generation != mScanSourcesGeneration) {
252 // Drop obsolete msg.
253 break;
254 }
255
Andreas Huber5bc087c2010-12-23 10:27:40 -0800256 mScanSourcesPending = false;
257
Steve Block3856b092011-10-20 11:56:00 +0100258 ALOGV("scanning sources haveAudio=%d, haveVideo=%d",
Andreas Huber43c3e6c2011-01-05 12:17:08 -0800259 mAudioDecoder != NULL, mVideoDecoder != NULL);
260
Andreas Huber5bc087c2010-12-23 10:27:40 -0800261 instantiateDecoder(false, &mVideoDecoder);
Andreas Huberf9334412010-12-15 15:17:42 -0800262
263 if (mAudioSink != NULL) {
Andreas Huber5bc087c2010-12-23 10:27:40 -0800264 instantiateDecoder(true, &mAudioDecoder);
Andreas Huberf9334412010-12-15 15:17:42 -0800265 }
266
Andreas Hubereac68ba2011-09-27 12:12:25 -0700267 status_t err;
268 if ((err = mSource->feedMoreTSData()) != OK) {
Andreas Huber1aef2112011-01-04 14:01:29 -0800269 if (mAudioDecoder == NULL && mVideoDecoder == NULL) {
270 // We're not currently decoding anything (no audio or
271 // video tracks found) and we just ran out of input data.
Andreas Hubereac68ba2011-09-27 12:12:25 -0700272
273 if (err == ERROR_END_OF_STREAM) {
274 notifyListener(MEDIA_PLAYBACK_COMPLETE, 0, 0);
275 } else {
276 notifyListener(MEDIA_ERROR, MEDIA_ERROR_UNKNOWN, err);
277 }
Andreas Huber1aef2112011-01-04 14:01:29 -0800278 }
Andreas Huberf9334412010-12-15 15:17:42 -0800279 break;
280 }
281
Andreas Huberf9334412010-12-15 15:17:42 -0800282 if (mAudioDecoder == NULL || mVideoDecoder == NULL) {
283 msg->post(100000ll);
Andreas Huber5bc087c2010-12-23 10:27:40 -0800284 mScanSourcesPending = true;
Andreas Huberf9334412010-12-15 15:17:42 -0800285 }
286 break;
287 }
288
289 case kWhatVideoNotify:
290 case kWhatAudioNotify:
291 {
292 bool audio = msg->what() == kWhatAudioNotify;
293
294 sp<AMessage> codecRequest;
295 CHECK(msg->findMessage("codec-request", &codecRequest));
296
297 int32_t what;
298 CHECK(codecRequest->findInt32("what", &what));
299
300 if (what == ACodec::kWhatFillThisBuffer) {
301 status_t err = feedDecoderInputData(
302 audio, codecRequest);
303
Andreas Huber5bc087c2010-12-23 10:27:40 -0800304 if (err == -EWOULDBLOCK) {
Andreas Hubereac68ba2011-09-27 12:12:25 -0700305 if (mSource->feedMoreTSData() == OK) {
Andreas Huber1183a4a2011-11-03 11:00:21 -0700306 msg->post(10000ll);
Andreas Huber5bc087c2010-12-23 10:27:40 -0800307 }
Andreas Huberf9334412010-12-15 15:17:42 -0800308 }
309 } else if (what == ACodec::kWhatEOS) {
Andreas Huberdc9bacd2011-09-26 10:53:29 -0700310 int32_t err;
311 CHECK(codecRequest->findInt32("err", &err));
312
313 if (err == ERROR_END_OF_STREAM) {
Steve Block3856b092011-10-20 11:56:00 +0100314 ALOGV("got %s decoder EOS", audio ? "audio" : "video");
Andreas Huberdc9bacd2011-09-26 10:53:29 -0700315 } else {
Steve Block3856b092011-10-20 11:56:00 +0100316 ALOGV("got %s decoder EOS w/ error %d",
Andreas Huberdc9bacd2011-09-26 10:53:29 -0700317 audio ? "audio" : "video",
318 err);
319 }
320
321 mRenderer->queueEOS(audio, err);
Andreas Huberf9334412010-12-15 15:17:42 -0800322 } else if (what == ACodec::kWhatFlushCompleted) {
Andreas Huber1aef2112011-01-04 14:01:29 -0800323 bool needShutdown;
Andreas Huber53df1a42010-12-22 10:03:04 -0800324
Andreas Huberf9334412010-12-15 15:17:42 -0800325 if (audio) {
Andreas Huber1aef2112011-01-04 14:01:29 -0800326 CHECK(IsFlushingState(mFlushingAudio, &needShutdown));
Andreas Huberf9334412010-12-15 15:17:42 -0800327 mFlushingAudio = FLUSHED;
328 } else {
Andreas Huber1aef2112011-01-04 14:01:29 -0800329 CHECK(IsFlushingState(mFlushingVideo, &needShutdown));
Andreas Huberf9334412010-12-15 15:17:42 -0800330 mFlushingVideo = FLUSHED;
Andreas Huber3fe62152011-09-16 15:09:22 -0700331
332 mVideoLateByUs = 0;
Andreas Huberf9334412010-12-15 15:17:42 -0800333 }
334
Steve Block3856b092011-10-20 11:56:00 +0100335 ALOGV("decoder %s flush completed", audio ? "audio" : "video");
Andreas Huberf9334412010-12-15 15:17:42 -0800336
Andreas Huber1aef2112011-01-04 14:01:29 -0800337 if (needShutdown) {
Steve Block3856b092011-10-20 11:56:00 +0100338 ALOGV("initiating %s decoder shutdown",
Andreas Huber53df1a42010-12-22 10:03:04 -0800339 audio ? "audio" : "video");
Andreas Huberf9334412010-12-15 15:17:42 -0800340
Andreas Huber53df1a42010-12-22 10:03:04 -0800341 (audio ? mAudioDecoder : mVideoDecoder)->initiateShutdown();
Andreas Huberf9334412010-12-15 15:17:42 -0800342
Andreas Huber53df1a42010-12-22 10:03:04 -0800343 if (audio) {
344 mFlushingAudio = SHUTTING_DOWN_DECODER;
345 } else {
346 mFlushingVideo = SHUTTING_DOWN_DECODER;
347 }
Andreas Huberf9334412010-12-15 15:17:42 -0800348 }
Andreas Huber3831a062010-12-21 10:22:33 -0800349
350 finishFlushIfPossible();
Andreas Huber2c2814b2010-12-15 17:18:20 -0800351 } else if (what == ACodec::kWhatOutputFormatChanged) {
Andreas Huber31e25082011-01-10 10:38:31 -0800352 if (audio) {
353 int32_t numChannels;
354 CHECK(codecRequest->findInt32("channel-count", &numChannels));
Andreas Huber2c2814b2010-12-15 17:18:20 -0800355
Andreas Huber31e25082011-01-10 10:38:31 -0800356 int32_t sampleRate;
357 CHECK(codecRequest->findInt32("sample-rate", &sampleRate));
Andreas Huber2c2814b2010-12-15 17:18:20 -0800358
Steve Block3856b092011-10-20 11:56:00 +0100359 ALOGV("Audio output format changed to %d Hz, %d channels",
Andreas Huber31e25082011-01-10 10:38:31 -0800360 sampleRate, numChannels);
Andreas Huber2c2814b2010-12-15 17:18:20 -0800361
Andreas Huber31e25082011-01-10 10:38:31 -0800362 mAudioSink->close();
Andreas Huber078cfcf2011-09-15 12:25:04 -0700363 CHECK_EQ(mAudioSink->open(
364 sampleRate,
365 numChannels,
Jean-Michel Trivi786618f2012-03-02 14:54:07 -0800366 CHANNEL_MASK_USE_CHANNEL_ORDER,
Andreas Huber078cfcf2011-09-15 12:25:04 -0700367 AUDIO_FORMAT_PCM_16_BIT,
368 8 /* bufferCount */),
369 (status_t)OK);
Andreas Huber31e25082011-01-10 10:38:31 -0800370 mAudioSink->start();
Andreas Huber2c2814b2010-12-15 17:18:20 -0800371
Andreas Huber31e25082011-01-10 10:38:31 -0800372 mRenderer->signalAudioSinkChanged();
373 } else {
374 // video
Andreas Huber3831a062010-12-21 10:22:33 -0800375
Andreas Huber31e25082011-01-10 10:38:31 -0800376 int32_t width, height;
377 CHECK(codecRequest->findInt32("width", &width));
378 CHECK(codecRequest->findInt32("height", &height));
379
380 int32_t cropLeft, cropTop, cropRight, cropBottom;
381 CHECK(codecRequest->findRect(
382 "crop",
383 &cropLeft, &cropTop, &cropRight, &cropBottom));
384
Steve Block3856b092011-10-20 11:56:00 +0100385 ALOGV("Video output format changed to %d x %d "
Andreas Hubercb67cd12011-08-26 16:02:19 -0700386 "(crop: %d x %d @ (%d, %d))",
Andreas Huber31e25082011-01-10 10:38:31 -0800387 width, height,
Andreas Hubercb67cd12011-08-26 16:02:19 -0700388 (cropRight - cropLeft + 1),
389 (cropBottom - cropTop + 1),
390 cropLeft, cropTop);
Andreas Huber31e25082011-01-10 10:38:31 -0800391
392 notifyListener(
393 MEDIA_SET_VIDEO_SIZE,
394 cropRight - cropLeft + 1,
395 cropBottom - cropTop + 1);
396 }
Andreas Huber3831a062010-12-21 10:22:33 -0800397 } else if (what == ACodec::kWhatShutdownCompleted) {
Steve Block3856b092011-10-20 11:56:00 +0100398 ALOGV("%s shutdown completed", audio ? "audio" : "video");
Andreas Huber3831a062010-12-21 10:22:33 -0800399 if (audio) {
400 mAudioDecoder.clear();
401
402 CHECK_EQ((int)mFlushingAudio, (int)SHUTTING_DOWN_DECODER);
403 mFlushingAudio = SHUT_DOWN;
404 } else {
405 mVideoDecoder.clear();
406
407 CHECK_EQ((int)mFlushingVideo, (int)SHUTTING_DOWN_DECODER);
408 mFlushingVideo = SHUT_DOWN;
409 }
410
411 finishFlushIfPossible();
Andreas Huberc92fd242011-08-16 13:48:44 -0700412 } else if (what == ACodec::kWhatError) {
Steve Block29357bc2012-01-06 19:20:56 +0000413 ALOGE("Received error from %s decoder, aborting playback.",
Andreas Huberc92fd242011-08-16 13:48:44 -0700414 audio ? "audio" : "video");
415
416 mRenderer->queueEOS(audio, UNKNOWN_ERROR);
Andreas Huber57788222012-02-21 11:47:18 -0800417 } else if (what == ACodec::kWhatDrainThisBuffer) {
Andreas Huberf9334412010-12-15 15:17:42 -0800418 renderBuffer(audio, codecRequest);
Andreas Huber57788222012-02-21 11:47:18 -0800419 } else {
420 ALOGV("Unhandled codec notification %d.", what);
Andreas Huberf9334412010-12-15 15:17:42 -0800421 }
422
423 break;
424 }
425
426 case kWhatRendererNotify:
427 {
428 int32_t what;
429 CHECK(msg->findInt32("what", &what));
430
431 if (what == Renderer::kWhatEOS) {
432 int32_t audio;
433 CHECK(msg->findInt32("audio", &audio));
434
Andreas Huberc92fd242011-08-16 13:48:44 -0700435 int32_t finalResult;
436 CHECK(msg->findInt32("finalResult", &finalResult));
437
Andreas Huberf9334412010-12-15 15:17:42 -0800438 if (audio) {
439 mAudioEOS = true;
440 } else {
441 mVideoEOS = true;
442 }
443
Andreas Huberc92fd242011-08-16 13:48:44 -0700444 if (finalResult == ERROR_END_OF_STREAM) {
Steve Block3856b092011-10-20 11:56:00 +0100445 ALOGV("reached %s EOS", audio ? "audio" : "video");
Andreas Huberc92fd242011-08-16 13:48:44 -0700446 } else {
Steve Block29357bc2012-01-06 19:20:56 +0000447 ALOGE("%s track encountered an error (%d)",
Andreas Huberc92fd242011-08-16 13:48:44 -0700448 audio ? "audio" : "video", finalResult);
449
450 notifyListener(
451 MEDIA_ERROR, MEDIA_ERROR_UNKNOWN, finalResult);
452 }
Andreas Huberf9334412010-12-15 15:17:42 -0800453
454 if ((mAudioEOS || mAudioDecoder == NULL)
455 && (mVideoEOS || mVideoDecoder == NULL)) {
456 notifyListener(MEDIA_PLAYBACK_COMPLETE, 0, 0);
457 }
Andreas Huber43c3e6c2011-01-05 12:17:08 -0800458 } else if (what == Renderer::kWhatPosition) {
459 int64_t positionUs;
460 CHECK(msg->findInt64("positionUs", &positionUs));
461
Andreas Huber3fe62152011-09-16 15:09:22 -0700462 CHECK(msg->findInt64("videoLateByUs", &mVideoLateByUs));
463
Andreas Huber43c3e6c2011-01-05 12:17:08 -0800464 if (mDriver != NULL) {
465 sp<NuPlayerDriver> driver = mDriver.promote();
466 if (driver != NULL) {
467 driver->notifyPosition(positionUs);
Andreas Huber3fe62152011-09-16 15:09:22 -0700468
469 driver->notifyFrameStats(
470 mNumFramesTotal, mNumFramesDropped);
Andreas Huber43c3e6c2011-01-05 12:17:08 -0800471 }
472 }
Andreas Huber3fe62152011-09-16 15:09:22 -0700473 } else if (what == Renderer::kWhatFlushComplete) {
Andreas Huberf9334412010-12-15 15:17:42 -0800474 CHECK_EQ(what, (int32_t)Renderer::kWhatFlushComplete);
475
476 int32_t audio;
477 CHECK(msg->findInt32("audio", &audio));
478
Steve Block3856b092011-10-20 11:56:00 +0100479 ALOGV("renderer %s flush completed.", audio ? "audio" : "video");
Andreas Huberf9334412010-12-15 15:17:42 -0800480 }
481 break;
482 }
483
484 case kWhatMoreDataQueued:
485 {
486 break;
487 }
488
Andreas Huber1aef2112011-01-04 14:01:29 -0800489 case kWhatReset:
490 {
Steve Block3856b092011-10-20 11:56:00 +0100491 ALOGV("kWhatReset");
Andreas Huber1aef2112011-01-04 14:01:29 -0800492
Andreas Huberb58ce9f2011-11-28 16:27:35 -0800493 if (mRenderer != NULL) {
494 // There's an edge case where the renderer owns all output
495 // buffers and is paused, therefore the decoder will not read
496 // more input data and will never encounter the matching
497 // discontinuity. To avoid this, we resume the renderer.
498
499 if (mFlushingAudio == AWAITING_DISCONTINUITY
500 || mFlushingVideo == AWAITING_DISCONTINUITY) {
501 mRenderer->resume();
502 }
503 }
504
Andreas Huber1aef2112011-01-04 14:01:29 -0800505 if (mFlushingAudio != NONE || mFlushingVideo != NONE) {
506 // We're currently flushing, postpone the reset until that's
507 // completed.
508
Andreas Huberea9d51b2011-11-30 09:53:40 -0800509 ALOGV("postponing reset mFlushingAudio=%d, mFlushingVideo=%d",
510 mFlushingAudio, mFlushingVideo);
Andreas Huber1aef2112011-01-04 14:01:29 -0800511
512 mResetPostponed = true;
513 break;
514 }
515
516 if (mAudioDecoder == NULL && mVideoDecoder == NULL) {
517 finishReset();
518 break;
519 }
520
Andreas Huber6e3d3112011-11-28 12:36:11 -0800521 mTimeDiscontinuityPending = true;
522
Andreas Huber1aef2112011-01-04 14:01:29 -0800523 if (mAudioDecoder != NULL) {
524 flushDecoder(true /* audio */, true /* needShutdown */);
525 }
526
527 if (mVideoDecoder != NULL) {
528 flushDecoder(false /* audio */, true /* needShutdown */);
529 }
530
531 mResetInProgress = true;
532 break;
533 }
534
Andreas Huber43c3e6c2011-01-05 12:17:08 -0800535 case kWhatSeek:
536 {
537 int64_t seekTimeUs;
538 CHECK(msg->findInt64("seekTimeUs", &seekTimeUs));
539
Steve Block3856b092011-10-20 11:56:00 +0100540 ALOGV("kWhatSeek seekTimeUs=%lld us (%.2f secs)",
Andreas Huber43c3e6c2011-01-05 12:17:08 -0800541 seekTimeUs, seekTimeUs / 1E6);
542
543 mSource->seekTo(seekTimeUs);
544
545 if (mDriver != NULL) {
546 sp<NuPlayerDriver> driver = mDriver.promote();
547 if (driver != NULL) {
548 driver->notifySeekComplete();
549 }
550 }
551
552 break;
553 }
554
Andreas Huberb4082222011-01-20 15:23:04 -0800555 case kWhatPause:
556 {
557 CHECK(mRenderer != NULL);
558 mRenderer->pause();
559 break;
560 }
561
562 case kWhatResume:
563 {
564 CHECK(mRenderer != NULL);
565 mRenderer->resume();
566 break;
567 }
568
Andreas Huberf9334412010-12-15 15:17:42 -0800569 default:
570 TRESPASS();
571 break;
572 }
573}
574
Andreas Huber3831a062010-12-21 10:22:33 -0800575void NuPlayer::finishFlushIfPossible() {
576 if (mFlushingAudio != FLUSHED && mFlushingAudio != SHUT_DOWN) {
577 return;
578 }
579
580 if (mFlushingVideo != FLUSHED && mFlushingVideo != SHUT_DOWN) {
581 return;
582 }
583
Steve Block3856b092011-10-20 11:56:00 +0100584 ALOGV("both audio and video are flushed now.");
Andreas Huber3831a062010-12-21 10:22:33 -0800585
Andreas Huber6e3d3112011-11-28 12:36:11 -0800586 if (mTimeDiscontinuityPending) {
587 mRenderer->signalTimeDiscontinuity();
588 mTimeDiscontinuityPending = false;
589 }
Andreas Huber3831a062010-12-21 10:22:33 -0800590
Andreas Huber22fc52f2011-01-05 16:24:27 -0800591 if (mAudioDecoder != NULL) {
Andreas Huber3831a062010-12-21 10:22:33 -0800592 mAudioDecoder->signalResume();
593 }
594
Andreas Huber22fc52f2011-01-05 16:24:27 -0800595 if (mVideoDecoder != NULL) {
Andreas Huber3831a062010-12-21 10:22:33 -0800596 mVideoDecoder->signalResume();
597 }
598
599 mFlushingAudio = NONE;
600 mFlushingVideo = NONE;
Andreas Huber3831a062010-12-21 10:22:33 -0800601
Andreas Huber1aef2112011-01-04 14:01:29 -0800602 if (mResetInProgress) {
Steve Block3856b092011-10-20 11:56:00 +0100603 ALOGV("reset completed");
Andreas Huber1aef2112011-01-04 14:01:29 -0800604
605 mResetInProgress = false;
606 finishReset();
607 } else if (mResetPostponed) {
608 (new AMessage(kWhatReset, id()))->post();
609 mResetPostponed = false;
Andreas Huber22fc52f2011-01-05 16:24:27 -0800610 } else if (mAudioDecoder == NULL || mVideoDecoder == NULL) {
Andreas Huber1aef2112011-01-04 14:01:29 -0800611 postScanSources();
Andreas Huberf9334412010-12-15 15:17:42 -0800612 }
613}
614
Andreas Huber1aef2112011-01-04 14:01:29 -0800615void NuPlayer::finishReset() {
616 CHECK(mAudioDecoder == NULL);
617 CHECK(mVideoDecoder == NULL);
618
Andreas Huber2bfdd422011-10-11 15:24:07 -0700619 ++mScanSourcesGeneration;
620 mScanSourcesPending = false;
621
Andreas Huber1aef2112011-01-04 14:01:29 -0800622 mRenderer.clear();
Andreas Huber2bfdd422011-10-11 15:24:07 -0700623
624 if (mSource != NULL) {
625 mSource->stop();
626 mSource.clear();
627 }
Andreas Huber1aef2112011-01-04 14:01:29 -0800628
Andreas Huber43c3e6c2011-01-05 12:17:08 -0800629 if (mDriver != NULL) {
630 sp<NuPlayerDriver> driver = mDriver.promote();
631 if (driver != NULL) {
632 driver->notifyResetComplete();
633 }
634 }
Andreas Huber1aef2112011-01-04 14:01:29 -0800635}
636
637void NuPlayer::postScanSources() {
638 if (mScanSourcesPending) {
639 return;
640 }
641
642 sp<AMessage> msg = new AMessage(kWhatScanSources, id());
643 msg->setInt32("generation", mScanSourcesGeneration);
644 msg->post();
645
646 mScanSourcesPending = true;
647}
648
Andreas Huber5bc087c2010-12-23 10:27:40 -0800649status_t NuPlayer::instantiateDecoder(bool audio, sp<Decoder> *decoder) {
Andreas Huberf9334412010-12-15 15:17:42 -0800650 if (*decoder != NULL) {
651 return OK;
652 }
653
Andreas Huber5bc087c2010-12-23 10:27:40 -0800654 sp<MetaData> meta = mSource->getFormat(audio);
Andreas Huberf9334412010-12-15 15:17:42 -0800655
Andreas Huber5bc087c2010-12-23 10:27:40 -0800656 if (meta == NULL) {
Andreas Huberf9334412010-12-15 15:17:42 -0800657 return -EWOULDBLOCK;
658 }
659
Andreas Huber3fe62152011-09-16 15:09:22 -0700660 if (!audio) {
661 const char *mime;
662 CHECK(meta->findCString(kKeyMIMEType, &mime));
663 mVideoIsAVC = !strcasecmp(MEDIA_MIMETYPE_VIDEO_AVC, mime);
664 }
665
Andreas Huberf9334412010-12-15 15:17:42 -0800666 sp<AMessage> notify =
667 new AMessage(audio ? kWhatAudioNotify : kWhatVideoNotify,
668 id());
669
Glenn Kasten11731182011-02-08 17:26:17 -0800670 *decoder = audio ? new Decoder(notify) :
671 new Decoder(notify, mNativeWindow);
Andreas Huberf9334412010-12-15 15:17:42 -0800672 looper()->registerHandler(*decoder);
673
Andreas Huber5bc087c2010-12-23 10:27:40 -0800674 (*decoder)->configure(meta);
Andreas Huberf9334412010-12-15 15:17:42 -0800675
Andreas Huber43c3e6c2011-01-05 12:17:08 -0800676 int64_t durationUs;
677 if (mDriver != NULL && mSource->getDuration(&durationUs) == OK) {
678 sp<NuPlayerDriver> driver = mDriver.promote();
679 if (driver != NULL) {
680 driver->notifyDuration(durationUs);
681 }
682 }
683
Andreas Huberf9334412010-12-15 15:17:42 -0800684 return OK;
685}
686
687status_t NuPlayer::feedDecoderInputData(bool audio, const sp<AMessage> &msg) {
688 sp<AMessage> reply;
689 CHECK(msg->findMessage("reply", &reply));
690
Andreas Huber53df1a42010-12-22 10:03:04 -0800691 if ((audio && IsFlushingState(mFlushingAudio))
692 || (!audio && IsFlushingState(mFlushingVideo))) {
Andreas Huberf9334412010-12-15 15:17:42 -0800693 reply->setInt32("err", INFO_DISCONTINUITY);
694 reply->post();
695 return OK;
696 }
697
698 sp<ABuffer> accessUnit;
Andreas Huberf9334412010-12-15 15:17:42 -0800699
Andreas Huber3fe62152011-09-16 15:09:22 -0700700 bool dropAccessUnit;
701 do {
702 status_t err = mSource->dequeueAccessUnit(audio, &accessUnit);
Andreas Huber5bc087c2010-12-23 10:27:40 -0800703
Andreas Huber3fe62152011-09-16 15:09:22 -0700704 if (err == -EWOULDBLOCK) {
705 return err;
706 } else if (err != OK) {
707 if (err == INFO_DISCONTINUITY) {
708 int32_t type;
709 CHECK(accessUnit->meta()->findInt32("discontinuity", &type));
Andreas Huber53df1a42010-12-22 10:03:04 -0800710
Andreas Huber3fe62152011-09-16 15:09:22 -0700711 bool formatChange =
Andreas Huber6e3d3112011-11-28 12:36:11 -0800712 (audio &&
713 (type & ATSParser::DISCONTINUITY_AUDIO_FORMAT))
714 || (!audio &&
715 (type & ATSParser::DISCONTINUITY_VIDEO_FORMAT));
Andreas Huber53df1a42010-12-22 10:03:04 -0800716
Andreas Huber6e3d3112011-11-28 12:36:11 -0800717 bool timeChange = (type & ATSParser::DISCONTINUITY_TIME) != 0;
718
Steve Blockdf64d152012-01-04 20:05:49 +0000719 ALOGI("%s discontinuity (formatChange=%d, time=%d)",
Andreas Huber6e3d3112011-11-28 12:36:11 -0800720 audio ? "audio" : "video", formatChange, timeChange);
Andreas Huber32f3cef2011-03-02 15:34:46 -0800721
Andreas Huber3fe62152011-09-16 15:09:22 -0700722 if (audio) {
723 mSkipRenderingAudioUntilMediaTimeUs = -1;
724 } else {
725 mSkipRenderingVideoUntilMediaTimeUs = -1;
726 }
Andreas Huber32f3cef2011-03-02 15:34:46 -0800727
Andreas Huber6e3d3112011-11-28 12:36:11 -0800728 if (timeChange) {
729 sp<AMessage> extra;
730 if (accessUnit->meta()->findMessage("extra", &extra)
731 && extra != NULL) {
732 int64_t resumeAtMediaTimeUs;
733 if (extra->findInt64(
734 "resume-at-mediatimeUs", &resumeAtMediaTimeUs)) {
Steve Blockdf64d152012-01-04 20:05:49 +0000735 ALOGI("suppressing rendering of %s until %lld us",
Andreas Huber6e3d3112011-11-28 12:36:11 -0800736 audio ? "audio" : "video", resumeAtMediaTimeUs);
Andreas Huber3fe62152011-09-16 15:09:22 -0700737
Andreas Huber6e3d3112011-11-28 12:36:11 -0800738 if (audio) {
739 mSkipRenderingAudioUntilMediaTimeUs =
740 resumeAtMediaTimeUs;
741 } else {
742 mSkipRenderingVideoUntilMediaTimeUs =
743 resumeAtMediaTimeUs;
744 }
Andreas Huber3fe62152011-09-16 15:09:22 -0700745 }
Andreas Huber32f3cef2011-03-02 15:34:46 -0800746 }
747 }
Andreas Huber3fe62152011-09-16 15:09:22 -0700748
Andreas Huber6e3d3112011-11-28 12:36:11 -0800749 mTimeDiscontinuityPending =
750 mTimeDiscontinuityPending || timeChange;
751
752 if (formatChange || timeChange) {
753 flushDecoder(audio, formatChange);
754 } else {
755 // This stream is unaffected by the discontinuity
756
757 if (audio) {
758 mFlushingAudio = FLUSHED;
759 } else {
760 mFlushingVideo = FLUSHED;
761 }
762
763 finishFlushIfPossible();
764
765 return -EWOULDBLOCK;
766 }
Andreas Huber32f3cef2011-03-02 15:34:46 -0800767 }
768
Andreas Huber3fe62152011-09-16 15:09:22 -0700769 reply->setInt32("err", err);
770 reply->post();
771 return OK;
Andreas Huberf9334412010-12-15 15:17:42 -0800772 }
773
Andreas Huber3fe62152011-09-16 15:09:22 -0700774 if (!audio) {
775 ++mNumFramesTotal;
776 }
777
778 dropAccessUnit = false;
779 if (!audio
780 && mVideoLateByUs > 100000ll
781 && mVideoIsAVC
782 && !IsAVCReferenceFrame(accessUnit)) {
783 dropAccessUnit = true;
784 ++mNumFramesDropped;
785 }
786 } while (dropAccessUnit);
Andreas Huberf9334412010-12-15 15:17:42 -0800787
Steve Block3856b092011-10-20 11:56:00 +0100788 // ALOGV("returned a valid buffer of %s data", audio ? "audio" : "video");
Andreas Huberf9334412010-12-15 15:17:42 -0800789
790#if 0
791 int64_t mediaTimeUs;
792 CHECK(accessUnit->meta()->findInt64("timeUs", &mediaTimeUs));
Steve Block3856b092011-10-20 11:56:00 +0100793 ALOGV("feeding %s input buffer at media time %.2f secs",
Andreas Huberf9334412010-12-15 15:17:42 -0800794 audio ? "audio" : "video",
795 mediaTimeUs / 1E6);
796#endif
797
Andreas Huber2d8bedd2012-02-21 14:38:23 -0800798 reply->setBuffer("buffer", accessUnit);
Andreas Huberf9334412010-12-15 15:17:42 -0800799 reply->post();
800
801 return OK;
802}
803
804void NuPlayer::renderBuffer(bool audio, const sp<AMessage> &msg) {
Steve Block3856b092011-10-20 11:56:00 +0100805 // ALOGV("renderBuffer %s", audio ? "audio" : "video");
Andreas Huberf9334412010-12-15 15:17:42 -0800806
807 sp<AMessage> reply;
808 CHECK(msg->findMessage("reply", &reply));
809
Andreas Huber18ac5402011-08-31 15:04:25 -0700810 if (IsFlushingState(audio ? mFlushingAudio : mFlushingVideo)) {
811 // We're currently attempting to flush the decoder, in order
812 // to complete this, the decoder wants all its buffers back,
813 // so we don't want any output buffers it sent us (from before
814 // we initiated the flush) to be stuck in the renderer's queue.
815
Steve Block3856b092011-10-20 11:56:00 +0100816 ALOGV("we're still flushing the %s decoder, sending its output buffer"
Andreas Huber18ac5402011-08-31 15:04:25 -0700817 " right back.", audio ? "audio" : "video");
818
819 reply->post();
820 return;
821 }
822
Andreas Huber2d8bedd2012-02-21 14:38:23 -0800823 sp<ABuffer> buffer;
824 CHECK(msg->findBuffer("buffer", &buffer));
Andreas Huberf9334412010-12-15 15:17:42 -0800825
Andreas Huber32f3cef2011-03-02 15:34:46 -0800826 int64_t &skipUntilMediaTimeUs =
827 audio
828 ? mSkipRenderingAudioUntilMediaTimeUs
829 : mSkipRenderingVideoUntilMediaTimeUs;
830
831 if (skipUntilMediaTimeUs >= 0) {
832 int64_t mediaTimeUs;
833 CHECK(buffer->meta()->findInt64("timeUs", &mediaTimeUs));
834
835 if (mediaTimeUs < skipUntilMediaTimeUs) {
Steve Block3856b092011-10-20 11:56:00 +0100836 ALOGV("dropping %s buffer at time %lld as requested.",
Andreas Huber32f3cef2011-03-02 15:34:46 -0800837 audio ? "audio" : "video",
838 mediaTimeUs);
839
840 reply->post();
841 return;
842 }
843
844 skipUntilMediaTimeUs = -1;
845 }
846
Andreas Huberf9334412010-12-15 15:17:42 -0800847 mRenderer->queueBuffer(audio, buffer, reply);
848}
849
850void NuPlayer::notifyListener(int msg, int ext1, int ext2) {
Andreas Huber43c3e6c2011-01-05 12:17:08 -0800851 if (mDriver == NULL) {
Andreas Huberf9334412010-12-15 15:17:42 -0800852 return;
853 }
854
Andreas Huber43c3e6c2011-01-05 12:17:08 -0800855 sp<NuPlayerDriver> driver = mDriver.promote();
Andreas Huberf9334412010-12-15 15:17:42 -0800856
Andreas Huber43c3e6c2011-01-05 12:17:08 -0800857 if (driver == NULL) {
Andreas Huberf9334412010-12-15 15:17:42 -0800858 return;
859 }
860
Andreas Hubera4af2142011-10-26 15:23:31 -0700861 driver->notifyListener(msg, ext1, ext2);
Andreas Huberf9334412010-12-15 15:17:42 -0800862}
863
Andreas Huber1aef2112011-01-04 14:01:29 -0800864void NuPlayer::flushDecoder(bool audio, bool needShutdown) {
Andreas Huber6e3d3112011-11-28 12:36:11 -0800865 if ((audio && mAudioDecoder == NULL) || (!audio && mVideoDecoder == NULL)) {
Steve Blockdf64d152012-01-04 20:05:49 +0000866 ALOGI("flushDecoder %s without decoder present",
Andreas Huber6e3d3112011-11-28 12:36:11 -0800867 audio ? "audio" : "video");
868 }
869
Andreas Huber1aef2112011-01-04 14:01:29 -0800870 // Make sure we don't continue to scan sources until we finish flushing.
871 ++mScanSourcesGeneration;
Andreas Huber43c3e6c2011-01-05 12:17:08 -0800872 mScanSourcesPending = false;
Andreas Huber1aef2112011-01-04 14:01:29 -0800873
874 (audio ? mAudioDecoder : mVideoDecoder)->signalFlush();
875 mRenderer->flush(audio);
876
877 FlushStatus newStatus =
878 needShutdown ? FLUSHING_DECODER_SHUTDOWN : FLUSHING_DECODER;
879
880 if (audio) {
881 CHECK(mFlushingAudio == NONE
882 || mFlushingAudio == AWAITING_DISCONTINUITY);
883
884 mFlushingAudio = newStatus;
885
886 if (mFlushingVideo == NONE) {
887 mFlushingVideo = (mVideoDecoder != NULL)
888 ? AWAITING_DISCONTINUITY
889 : FLUSHED;
890 }
891 } else {
892 CHECK(mFlushingVideo == NONE
893 || mFlushingVideo == AWAITING_DISCONTINUITY);
894
895 mFlushingVideo = newStatus;
896
897 if (mFlushingAudio == NONE) {
898 mFlushingAudio = (mAudioDecoder != NULL)
899 ? AWAITING_DISCONTINUITY
900 : FLUSHED;
901 }
902 }
903}
904
Andreas Huberf9334412010-12-15 15:17:42 -0800905} // namespace android