blob: 88c59bf0f20c6d5e03d24bd595451de0c2a46d3b [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"
Wei Jiabc2fb722014-07-08 16:37:57 -070025#include "NuPlayerDecoderPassThrough.h"
Andreas Huber43c3e6c2011-01-05 12:17:08 -080026#include "NuPlayerDriver.h"
Andreas Huberf9334412010-12-15 15:17:42 -080027#include "NuPlayerRenderer.h"
Andreas Huber5bc087c2010-12-23 10:27:40 -080028#include "NuPlayerSource.h"
Andreas Huber2bfdd422011-10-11 15:24:07 -070029#include "RTSPSource.h"
Andreas Huber5bc087c2010-12-23 10:27:40 -080030#include "StreamingSource.h"
Andreas Huberafed0e12011-09-20 15:39:58 -070031#include "GenericSource.h"
Andreas Huber5bc087c2010-12-23 10:27:40 -080032
33#include "ATSParser.h"
Andreas Huberf9334412010-12-15 15:17:42 -080034
Andreas Huber3831a062010-12-21 10:22:33 -080035#include <media/stagefright/foundation/hexdump.h>
Andreas Huberf9334412010-12-15 15:17:42 -080036#include <media/stagefright/foundation/ABuffer.h>
37#include <media/stagefright/foundation/ADebug.h>
38#include <media/stagefright/foundation/AMessage.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>
Andy McFadden8ba01022012-12-18 09:46:54 -080042#include <gui/IGraphicBufferProducer.h>
Andreas Huberf9334412010-12-15 15:17:42 -080043
Andreas Huber3fe62152011-09-16 15:09:22 -070044#include "avc_utils.h"
45
Andreas Huber84066782011-08-16 09:34:26 -070046#include "ESDS.h"
47#include <media/stagefright/Utils.h>
48
Andreas Huberf9334412010-12-15 15:17:42 -080049namespace android {
50
Andreas Hubera1f8ab02012-11-30 10:53:22 -080051struct NuPlayer::Action : public RefBase {
52 Action() {}
53
54 virtual void execute(NuPlayer *player) = 0;
55
56private:
57 DISALLOW_EVIL_CONSTRUCTORS(Action);
58};
59
60struct NuPlayer::SeekAction : public Action {
61 SeekAction(int64_t seekTimeUs)
62 : mSeekTimeUs(seekTimeUs) {
63 }
64
65 virtual void execute(NuPlayer *player) {
66 player->performSeek(mSeekTimeUs);
67 }
68
69private:
70 int64_t mSeekTimeUs;
71
72 DISALLOW_EVIL_CONSTRUCTORS(SeekAction);
73};
74
Andreas Huber57a339c2012-12-03 11:18:00 -080075struct NuPlayer::SetSurfaceAction : public Action {
76 SetSurfaceAction(const sp<NativeWindowWrapper> &wrapper)
77 : mWrapper(wrapper) {
78 }
79
80 virtual void execute(NuPlayer *player) {
81 player->performSetSurface(mWrapper);
82 }
83
84private:
85 sp<NativeWindowWrapper> mWrapper;
86
87 DISALLOW_EVIL_CONSTRUCTORS(SetSurfaceAction);
88};
89
Andreas Huber14f76722013-01-15 09:04:18 -080090struct NuPlayer::ShutdownDecoderAction : public Action {
91 ShutdownDecoderAction(bool audio, bool video)
92 : mAudio(audio),
93 mVideo(video) {
94 }
95
96 virtual void execute(NuPlayer *player) {
97 player->performDecoderShutdown(mAudio, mVideo);
98 }
99
100private:
101 bool mAudio;
102 bool mVideo;
103
104 DISALLOW_EVIL_CONSTRUCTORS(ShutdownDecoderAction);
105};
106
107struct NuPlayer::PostMessageAction : public Action {
108 PostMessageAction(const sp<AMessage> &msg)
109 : mMessage(msg) {
110 }
111
112 virtual void execute(NuPlayer *) {
113 mMessage->post();
114 }
115
116private:
117 sp<AMessage> mMessage;
118
119 DISALLOW_EVIL_CONSTRUCTORS(PostMessageAction);
120};
121
Andreas Hubera1f8ab02012-11-30 10:53:22 -0800122// Use this if there's no state necessary to save in order to execute
123// the action.
124struct NuPlayer::SimpleAction : public Action {
125 typedef void (NuPlayer::*ActionFunc)();
126
127 SimpleAction(ActionFunc func)
128 : mFunc(func) {
129 }
130
131 virtual void execute(NuPlayer *player) {
132 (player->*mFunc)();
133 }
134
135private:
136 ActionFunc mFunc;
137
138 DISALLOW_EVIL_CONSTRUCTORS(SimpleAction);
139};
140
Andreas Huberf9334412010-12-15 15:17:42 -0800141////////////////////////////////////////////////////////////////////////////////
142
143NuPlayer::NuPlayer()
Andreas Huber9b80c2b2011-06-30 15:47:02 -0700144 : mUIDValid(false),
Andreas Huber9575c962013-02-05 13:59:56 -0800145 mSourceFlags(0),
Andreas Huber3fe62152011-09-16 15:09:22 -0700146 mVideoIsAVC(false),
Wei Jiabc2fb722014-07-08 16:37:57 -0700147 mOffloadAudio(false),
Andreas Huber9b80c2b2011-06-30 15:47:02 -0700148 mAudioEOS(false),
Andreas Huberf9334412010-12-15 15:17:42 -0800149 mVideoEOS(false),
Andreas Huber5bc087c2010-12-23 10:27:40 -0800150 mScanSourcesPending(false),
Andreas Huber1aef2112011-01-04 14:01:29 -0800151 mScanSourcesGeneration(0),
Andreas Huberb7c8e912012-11-27 15:02:53 -0800152 mPollDurationGeneration(0),
Andreas Huber6e3d3112011-11-28 12:36:11 -0800153 mTimeDiscontinuityPending(false),
Andreas Huberf9334412010-12-15 15:17:42 -0800154 mFlushingAudio(NONE),
Andreas Huber1aef2112011-01-04 14:01:29 -0800155 mFlushingVideo(NONE),
Andreas Huber3fe62152011-09-16 15:09:22 -0700156 mSkipRenderingAudioUntilMediaTimeUs(-1ll),
157 mSkipRenderingVideoUntilMediaTimeUs(-1ll),
158 mVideoLateByUs(0ll),
159 mNumFramesTotal(0ll),
James Dong0d268a32012-08-31 12:18:27 -0700160 mNumFramesDropped(0ll),
Andreas Huber57a339c2012-12-03 11:18:00 -0800161 mVideoScalingMode(NATIVE_WINDOW_SCALING_MODE_SCALE_TO_WINDOW),
162 mStarted(false) {
Andreas Huberf9334412010-12-15 15:17:42 -0800163}
164
165NuPlayer::~NuPlayer() {
166}
167
Andreas Huber9b80c2b2011-06-30 15:47:02 -0700168void NuPlayer::setUID(uid_t uid) {
169 mUIDValid = true;
170 mUID = uid;
171}
172
Andreas Huber43c3e6c2011-01-05 12:17:08 -0800173void NuPlayer::setDriver(const wp<NuPlayerDriver> &driver) {
174 mDriver = driver;
Andreas Huberf9334412010-12-15 15:17:42 -0800175}
176
Andreas Huber9575c962013-02-05 13:59:56 -0800177void NuPlayer::setDataSourceAsync(const sp<IStreamSource> &source) {
Andreas Huberf9334412010-12-15 15:17:42 -0800178 sp<AMessage> msg = new AMessage(kWhatSetDataSource, id());
179
Andreas Huberb5f25f02013-02-05 10:14:26 -0800180 sp<AMessage> notify = new AMessage(kWhatSourceNotify, id());
181
Andreas Huber240abcc2014-02-13 13:32:37 -0800182 msg->setObject("source", new StreamingSource(notify, source));
Andreas Huber5bc087c2010-12-23 10:27:40 -0800183 msg->post();
184}
Andreas Huberf9334412010-12-15 15:17:42 -0800185
Andreas Huberafed0e12011-09-20 15:39:58 -0700186static bool IsHTTPLiveURL(const char *url) {
187 if (!strncasecmp("http://", url, 7)
Andreas Huber99759402013-04-01 14:28:31 -0700188 || !strncasecmp("https://", url, 8)
189 || !strncasecmp("file://", url, 7)) {
Andreas Huberafed0e12011-09-20 15:39:58 -0700190 size_t len = strlen(url);
191 if (len >= 5 && !strcasecmp(".m3u8", &url[len - 5])) {
192 return true;
193 }
194
195 if (strstr(url,"m3u8")) {
196 return true;
197 }
198 }
199
200 return false;
201}
202
Andreas Huber9575c962013-02-05 13:59:56 -0800203void NuPlayer::setDataSourceAsync(
Andreas Huber1b86fe02014-01-29 11:13:26 -0800204 const sp<IMediaHTTPService> &httpService,
205 const char *url,
206 const KeyedVector<String8, String8> *headers) {
Andreas Huber5bc087c2010-12-23 10:27:40 -0800207 sp<AMessage> msg = new AMessage(kWhatSetDataSource, id());
Oscar Rydhé7a33b772012-02-20 10:15:48 +0100208 size_t len = strlen(url);
Andreas Huber5bc087c2010-12-23 10:27:40 -0800209
Andreas Huberb5f25f02013-02-05 10:14:26 -0800210 sp<AMessage> notify = new AMessage(kWhatSourceNotify, id());
211
Andreas Huberafed0e12011-09-20 15:39:58 -0700212 sp<Source> source;
213 if (IsHTTPLiveURL(url)) {
Andreas Huber81e68442014-02-05 11:52:33 -0800214 source = new HTTPLiveSource(notify, httpService, url, headers);
Andreas Huberafed0e12011-09-20 15:39:58 -0700215 } else if (!strncasecmp(url, "rtsp://", 7)) {
Andreas Huber1b86fe02014-01-29 11:13:26 -0800216 source = new RTSPSource(
217 notify, httpService, url, headers, mUIDValid, mUID);
Oscar Rydhé7a33b772012-02-20 10:15:48 +0100218 } else if ((!strncasecmp(url, "http://", 7)
219 || !strncasecmp(url, "https://", 8))
220 && ((len >= 4 && !strcasecmp(".sdp", &url[len - 4]))
221 || strstr(url, ".sdp?"))) {
Andreas Huber1b86fe02014-01-29 11:13:26 -0800222 source = new RTSPSource(
223 notify, httpService, url, headers, mUIDValid, mUID, true);
Andreas Huber2bfdd422011-10-11 15:24:07 -0700224 } else {
Andreas Huber81e68442014-02-05 11:52:33 -0800225 source = new GenericSource(notify, httpService, url, headers);
Andreas Huber2bfdd422011-10-11 15:24:07 -0700226 }
227
Andreas Huberafed0e12011-09-20 15:39:58 -0700228 msg->setObject("source", source);
229 msg->post();
230}
231
Andreas Huber9575c962013-02-05 13:59:56 -0800232void NuPlayer::setDataSourceAsync(int fd, int64_t offset, int64_t length) {
Andreas Huberafed0e12011-09-20 15:39:58 -0700233 sp<AMessage> msg = new AMessage(kWhatSetDataSource, id());
234
Andreas Huberb5f25f02013-02-05 10:14:26 -0800235 sp<AMessage> notify = new AMessage(kWhatSourceNotify, id());
236
237 sp<Source> source = new GenericSource(notify, fd, offset, length);
Andreas Huberafed0e12011-09-20 15:39:58 -0700238 msg->setObject("source", source);
Andreas Huberf9334412010-12-15 15:17:42 -0800239 msg->post();
240}
241
Andreas Huber9575c962013-02-05 13:59:56 -0800242void NuPlayer::prepareAsync() {
243 (new AMessage(kWhatPrepare, id()))->post();
244}
245
Andreas Huber57a339c2012-12-03 11:18:00 -0800246void NuPlayer::setVideoSurfaceTextureAsync(
Andy McFadden8ba01022012-12-18 09:46:54 -0800247 const sp<IGraphicBufferProducer> &bufferProducer) {
Glenn Kasten11731182011-02-08 17:26:17 -0800248 sp<AMessage> msg = new AMessage(kWhatSetVideoNativeWindow, id());
Andreas Huber57a339c2012-12-03 11:18:00 -0800249
Andy McFadden8ba01022012-12-18 09:46:54 -0800250 if (bufferProducer == NULL) {
Andreas Huber57a339c2012-12-03 11:18:00 -0800251 msg->setObject("native-window", NULL);
252 } else {
253 msg->setObject(
254 "native-window",
255 new NativeWindowWrapper(
Mathias Agopian1a2952a2013-02-14 17:11:27 -0800256 new Surface(bufferProducer)));
Andreas Huber57a339c2012-12-03 11:18:00 -0800257 }
258
Andreas Huberf9334412010-12-15 15:17:42 -0800259 msg->post();
260}
261
262void NuPlayer::setAudioSink(const sp<MediaPlayerBase::AudioSink> &sink) {
263 sp<AMessage> msg = new AMessage(kWhatSetAudioSink, id());
264 msg->setObject("sink", sink);
265 msg->post();
266}
267
268void NuPlayer::start() {
269 (new AMessage(kWhatStart, id()))->post();
270}
271
Andreas Huber43c3e6c2011-01-05 12:17:08 -0800272void NuPlayer::pause() {
Andreas Huberb4082222011-01-20 15:23:04 -0800273 (new AMessage(kWhatPause, id()))->post();
Andreas Huber43c3e6c2011-01-05 12:17:08 -0800274}
275
276void NuPlayer::resume() {
Andreas Huberb4082222011-01-20 15:23:04 -0800277 (new AMessage(kWhatResume, id()))->post();
Andreas Huber43c3e6c2011-01-05 12:17:08 -0800278}
279
Andreas Huber1aef2112011-01-04 14:01:29 -0800280void NuPlayer::resetAsync() {
281 (new AMessage(kWhatReset, id()))->post();
282}
283
Andreas Huber43c3e6c2011-01-05 12:17:08 -0800284void NuPlayer::seekToAsync(int64_t seekTimeUs) {
285 sp<AMessage> msg = new AMessage(kWhatSeek, id());
286 msg->setInt64("seekTimeUs", seekTimeUs);
287 msg->post();
288}
289
Andreas Huber53df1a42010-12-22 10:03:04 -0800290// static
Andreas Huber1aef2112011-01-04 14:01:29 -0800291bool NuPlayer::IsFlushingState(FlushStatus state, bool *needShutdown) {
Andreas Huber53df1a42010-12-22 10:03:04 -0800292 switch (state) {
293 case FLUSHING_DECODER:
Andreas Huber1aef2112011-01-04 14:01:29 -0800294 if (needShutdown != NULL) {
295 *needShutdown = false;
Andreas Huber53df1a42010-12-22 10:03:04 -0800296 }
297 return true;
298
Andreas Huber1aef2112011-01-04 14:01:29 -0800299 case FLUSHING_DECODER_SHUTDOWN:
300 if (needShutdown != NULL) {
301 *needShutdown = true;
Andreas Huber53df1a42010-12-22 10:03:04 -0800302 }
303 return true;
304
305 default:
306 return false;
307 }
308}
309
Chong Zhang404fced2014-06-11 14:45:31 -0700310void NuPlayer::writeTrackInfo(
311 Parcel* reply, const sp<AMessage> format) const {
312 int32_t trackType;
313 CHECK(format->findInt32("type", &trackType));
314
315 AString lang;
316 CHECK(format->findString("language", &lang));
317
318 reply->writeInt32(2); // write something non-zero
319 reply->writeInt32(trackType);
320 reply->writeString16(String16(lang.c_str()));
321
322 if (trackType == MEDIA_TRACK_TYPE_SUBTITLE) {
323 AString mime;
324 CHECK(format->findString("mime", &mime));
325
326 int32_t isAuto, isDefault, isForced;
327 CHECK(format->findInt32("auto", &isAuto));
328 CHECK(format->findInt32("default", &isDefault));
329 CHECK(format->findInt32("forced", &isForced));
330
331 reply->writeString16(String16(mime.c_str()));
332 reply->writeInt32(isAuto);
333 reply->writeInt32(isDefault);
334 reply->writeInt32(isForced);
335 }
336}
337
Andreas Huberf9334412010-12-15 15:17:42 -0800338void NuPlayer::onMessageReceived(const sp<AMessage> &msg) {
339 switch (msg->what()) {
340 case kWhatSetDataSource:
341 {
Steve Block3856b092011-10-20 11:56:00 +0100342 ALOGV("kWhatSetDataSource");
Andreas Huberf9334412010-12-15 15:17:42 -0800343
344 CHECK(mSource == NULL);
345
Andreas Huber5bc087c2010-12-23 10:27:40 -0800346 sp<RefBase> obj;
347 CHECK(msg->findObject("source", &obj));
Andreas Huberf9334412010-12-15 15:17:42 -0800348
Andreas Huber5bc087c2010-12-23 10:27:40 -0800349 mSource = static_cast<Source *>(obj.get());
Andreas Huberb5f25f02013-02-05 10:14:26 -0800350
351 looper()->registerHandler(mSource);
Andreas Huber9575c962013-02-05 13:59:56 -0800352
353 CHECK(mDriver != NULL);
354 sp<NuPlayerDriver> driver = mDriver.promote();
355 if (driver != NULL) {
356 driver->notifySetDataSourceCompleted(OK);
357 }
358 break;
359 }
360
361 case kWhatPrepare:
362 {
363 mSource->prepareAsync();
Andreas Huberf9334412010-12-15 15:17:42 -0800364 break;
365 }
366
Chong Zhangdcb89b32013-08-06 09:44:47 -0700367 case kWhatGetTrackInfo:
368 {
369 uint32_t replyID;
370 CHECK(msg->senderAwaitsResponse(&replyID));
371
Chong Zhang404fced2014-06-11 14:45:31 -0700372 Parcel* reply;
373 CHECK(msg->findPointer("reply", (void**)&reply));
374
375 size_t inbandTracks = 0;
Chong Zhangdcb89b32013-08-06 09:44:47 -0700376 if (mSource != NULL) {
Chong Zhang404fced2014-06-11 14:45:31 -0700377 inbandTracks = mSource->getTrackCount();
378 }
379
Chong Zhanga7fa1d92014-06-11 14:49:23 -0700380 size_t ccTracks = 0;
381 if (mCCDecoder != NULL) {
382 ccTracks = mCCDecoder->getTrackCount();
383 }
384
Chong Zhang404fced2014-06-11 14:45:31 -0700385 // total track count
Chong Zhanga7fa1d92014-06-11 14:49:23 -0700386 reply->writeInt32(inbandTracks + ccTracks);
Chong Zhang404fced2014-06-11 14:45:31 -0700387
388 // write inband tracks
389 for (size_t i = 0; i < inbandTracks; ++i) {
390 writeTrackInfo(reply, mSource->getTrackInfo(i));
Chong Zhangdcb89b32013-08-06 09:44:47 -0700391 }
392
Chong Zhanga7fa1d92014-06-11 14:49:23 -0700393 // write CC track
394 for (size_t i = 0; i < ccTracks; ++i) {
395 writeTrackInfo(reply, mCCDecoder->getTrackInfo(i));
396 }
397
Chong Zhangdcb89b32013-08-06 09:44:47 -0700398 sp<AMessage> response = new AMessage;
Chong Zhangdcb89b32013-08-06 09:44:47 -0700399 response->postReply(replyID);
400 break;
401 }
402
403 case kWhatSelectTrack:
404 {
405 uint32_t replyID;
406 CHECK(msg->senderAwaitsResponse(&replyID));
407
Chong Zhang404fced2014-06-11 14:45:31 -0700408 size_t trackIndex;
409 int32_t select;
410 CHECK(msg->findSize("trackIndex", &trackIndex));
411 CHECK(msg->findInt32("select", &select));
412
Chong Zhangdcb89b32013-08-06 09:44:47 -0700413 status_t err = INVALID_OPERATION;
Chong Zhang404fced2014-06-11 14:45:31 -0700414
415 size_t inbandTracks = 0;
Chong Zhangdcb89b32013-08-06 09:44:47 -0700416 if (mSource != NULL) {
Chong Zhang404fced2014-06-11 14:45:31 -0700417 inbandTracks = mSource->getTrackCount();
418 }
Chong Zhanga7fa1d92014-06-11 14:49:23 -0700419 size_t ccTracks = 0;
420 if (mCCDecoder != NULL) {
421 ccTracks = mCCDecoder->getTrackCount();
422 }
Chong Zhang404fced2014-06-11 14:45:31 -0700423
424 if (trackIndex < inbandTracks) {
Chong Zhangdcb89b32013-08-06 09:44:47 -0700425 err = mSource->selectTrack(trackIndex, select);
Chong Zhanga7fa1d92014-06-11 14:49:23 -0700426 } else {
427 trackIndex -= inbandTracks;
428
429 if (trackIndex < ccTracks) {
430 err = mCCDecoder->selectTrack(trackIndex, select);
431 }
Chong Zhangdcb89b32013-08-06 09:44:47 -0700432 }
433
434 sp<AMessage> response = new AMessage;
435 response->setInt32("err", err);
436
437 response->postReply(replyID);
438 break;
439 }
440
Andreas Huberb7c8e912012-11-27 15:02:53 -0800441 case kWhatPollDuration:
442 {
443 int32_t generation;
444 CHECK(msg->findInt32("generation", &generation));
445
446 if (generation != mPollDurationGeneration) {
447 // stale
448 break;
449 }
450
451 int64_t durationUs;
452 if (mDriver != NULL && mSource->getDuration(&durationUs) == OK) {
453 sp<NuPlayerDriver> driver = mDriver.promote();
454 if (driver != NULL) {
455 driver->notifyDuration(durationUs);
456 }
457 }
458
459 msg->post(1000000ll); // poll again in a second.
460 break;
461 }
462
Glenn Kasten11731182011-02-08 17:26:17 -0800463 case kWhatSetVideoNativeWindow:
Andreas Huberf9334412010-12-15 15:17:42 -0800464 {
Steve Block3856b092011-10-20 11:56:00 +0100465 ALOGV("kWhatSetVideoNativeWindow");
Andreas Huberf9334412010-12-15 15:17:42 -0800466
Andreas Huber57a339c2012-12-03 11:18:00 -0800467 mDeferredActions.push_back(
Andreas Huber14f76722013-01-15 09:04:18 -0800468 new ShutdownDecoderAction(
469 false /* audio */, true /* video */));
Andreas Huber57a339c2012-12-03 11:18:00 -0800470
Andreas Huberf9334412010-12-15 15:17:42 -0800471 sp<RefBase> obj;
Glenn Kasten11731182011-02-08 17:26:17 -0800472 CHECK(msg->findObject("native-window", &obj));
Andreas Huberf9334412010-12-15 15:17:42 -0800473
Andreas Huber57a339c2012-12-03 11:18:00 -0800474 mDeferredActions.push_back(
475 new SetSurfaceAction(
476 static_cast<NativeWindowWrapper *>(obj.get())));
James Dong0d268a32012-08-31 12:18:27 -0700477
Andreas Huber57a339c2012-12-03 11:18:00 -0800478 if (obj != NULL) {
479 // If there is a new surface texture, instantiate decoders
480 // again if possible.
481 mDeferredActions.push_back(
482 new SimpleAction(&NuPlayer::performScanSources));
483 }
484
485 processDeferredActions();
Andreas Huberf9334412010-12-15 15:17:42 -0800486 break;
487 }
488
489 case kWhatSetAudioSink:
490 {
Steve Block3856b092011-10-20 11:56:00 +0100491 ALOGV("kWhatSetAudioSink");
Andreas Huberf9334412010-12-15 15:17:42 -0800492
493 sp<RefBase> obj;
494 CHECK(msg->findObject("sink", &obj));
495
496 mAudioSink = static_cast<MediaPlayerBase::AudioSink *>(obj.get());
497 break;
498 }
499
500 case kWhatStart:
501 {
Steve Block3856b092011-10-20 11:56:00 +0100502 ALOGV("kWhatStart");
Andreas Huber43c3e6c2011-01-05 12:17:08 -0800503
Andreas Huber3fe62152011-09-16 15:09:22 -0700504 mVideoIsAVC = false;
Wei Jiabc2fb722014-07-08 16:37:57 -0700505 mOffloadAudio = false;
Andreas Huber1aef2112011-01-04 14:01:29 -0800506 mAudioEOS = false;
507 mVideoEOS = false;
Andreas Huber32f3cef2011-03-02 15:34:46 -0800508 mSkipRenderingAudioUntilMediaTimeUs = -1;
509 mSkipRenderingVideoUntilMediaTimeUs = -1;
Andreas Huber3fe62152011-09-16 15:09:22 -0700510 mVideoLateByUs = 0;
511 mNumFramesTotal = 0;
512 mNumFramesDropped = 0;
Andreas Huber57a339c2012-12-03 11:18:00 -0800513 mStarted = true;
Andreas Huber1aef2112011-01-04 14:01:29 -0800514
Andreas Huber5bc087c2010-12-23 10:27:40 -0800515 mSource->start();
Andreas Huberf9334412010-12-15 15:17:42 -0800516
Andreas Huberd5e56232013-03-12 11:01:43 -0700517 uint32_t flags = 0;
518
519 if (mSource->isRealTime()) {
520 flags |= Renderer::FLAG_REAL_TIME;
521 }
522
Wei Jiabc2fb722014-07-08 16:37:57 -0700523 sp<MetaData> audioMeta = mSource->getFormatMeta(true /* audio */);
524 audio_stream_type_t streamType = AUDIO_STREAM_MUSIC;
525 if (mAudioSink != NULL) {
526 streamType = mAudioSink->getAudioStreamType();
527 }
528
529 sp<AMessage> videoFormat = mSource->getFormat(false /* audio */);
530
531 mOffloadAudio =
532 canOffloadStream(audioMeta, (videoFormat != NULL),
533 true /* is_streaming */, streamType);
534 if (mOffloadAudio) {
535 flags |= Renderer::FLAG_OFFLOAD_AUDIO;
536 }
537
Andreas Huberf9334412010-12-15 15:17:42 -0800538 mRenderer = new Renderer(
539 mAudioSink,
Andreas Huberd5e56232013-03-12 11:01:43 -0700540 new AMessage(kWhatRendererNotify, id()),
541 flags);
Andreas Huberf9334412010-12-15 15:17:42 -0800542
543 looper()->registerHandler(mRenderer);
544
Andreas Huber1aef2112011-01-04 14:01:29 -0800545 postScanSources();
Andreas Huberf9334412010-12-15 15:17:42 -0800546 break;
547 }
548
549 case kWhatScanSources:
550 {
Andreas Huber1aef2112011-01-04 14:01:29 -0800551 int32_t generation;
552 CHECK(msg->findInt32("generation", &generation));
553 if (generation != mScanSourcesGeneration) {
554 // Drop obsolete msg.
555 break;
556 }
557
Andreas Huber5bc087c2010-12-23 10:27:40 -0800558 mScanSourcesPending = false;
559
Steve Block3856b092011-10-20 11:56:00 +0100560 ALOGV("scanning sources haveAudio=%d, haveVideo=%d",
Andreas Huber43c3e6c2011-01-05 12:17:08 -0800561 mAudioDecoder != NULL, mVideoDecoder != NULL);
562
Andreas Huberb7c8e912012-11-27 15:02:53 -0800563 bool mHadAnySourcesBefore =
564 (mAudioDecoder != NULL) || (mVideoDecoder != NULL);
565
Haynes Mathew George5d246ef2012-07-09 10:36:57 -0700566 if (mNativeWindow != NULL) {
567 instantiateDecoder(false, &mVideoDecoder);
568 }
Andreas Huberf9334412010-12-15 15:17:42 -0800569
570 if (mAudioSink != NULL) {
Andreas Huber5bc087c2010-12-23 10:27:40 -0800571 instantiateDecoder(true, &mAudioDecoder);
Andreas Huberf9334412010-12-15 15:17:42 -0800572 }
573
Andreas Huberb7c8e912012-11-27 15:02:53 -0800574 if (!mHadAnySourcesBefore
575 && (mAudioDecoder != NULL || mVideoDecoder != NULL)) {
576 // This is the first time we've found anything playable.
577
Andreas Huber9575c962013-02-05 13:59:56 -0800578 if (mSourceFlags & Source::FLAG_DYNAMIC_DURATION) {
Andreas Huberb7c8e912012-11-27 15:02:53 -0800579 schedulePollDuration();
580 }
581 }
582
Andreas Hubereac68ba2011-09-27 12:12:25 -0700583 status_t err;
584 if ((err = mSource->feedMoreTSData()) != OK) {
Andreas Huber1aef2112011-01-04 14:01:29 -0800585 if (mAudioDecoder == NULL && mVideoDecoder == NULL) {
586 // We're not currently decoding anything (no audio or
587 // video tracks found) and we just ran out of input data.
Andreas Hubereac68ba2011-09-27 12:12:25 -0700588
589 if (err == ERROR_END_OF_STREAM) {
590 notifyListener(MEDIA_PLAYBACK_COMPLETE, 0, 0);
591 } else {
592 notifyListener(MEDIA_ERROR, MEDIA_ERROR_UNKNOWN, err);
593 }
Andreas Huber1aef2112011-01-04 14:01:29 -0800594 }
Andreas Huberf9334412010-12-15 15:17:42 -0800595 break;
596 }
597
Andreas Huberfbe9d812012-08-31 14:05:27 -0700598 if ((mAudioDecoder == NULL && mAudioSink != NULL)
599 || (mVideoDecoder == NULL && mNativeWindow != NULL)) {
Andreas Huberf9334412010-12-15 15:17:42 -0800600 msg->post(100000ll);
Andreas Huber5bc087c2010-12-23 10:27:40 -0800601 mScanSourcesPending = true;
Andreas Huberf9334412010-12-15 15:17:42 -0800602 }
603 break;
604 }
605
606 case kWhatVideoNotify:
607 case kWhatAudioNotify:
608 {
609 bool audio = msg->what() == kWhatAudioNotify;
610
Andreas Huberf9334412010-12-15 15:17:42 -0800611 int32_t what;
Lajos Molnar1cd13982014-01-17 15:12:51 -0800612 CHECK(msg->findInt32("what", &what));
Andreas Huberf9334412010-12-15 15:17:42 -0800613
Lajos Molnar1cd13982014-01-17 15:12:51 -0800614 if (what == Decoder::kWhatFillThisBuffer) {
Andreas Huberf9334412010-12-15 15:17:42 -0800615 status_t err = feedDecoderInputData(
Lajos Molnar1cd13982014-01-17 15:12:51 -0800616 audio, msg);
Andreas Huberf9334412010-12-15 15:17:42 -0800617
Andreas Huber5bc087c2010-12-23 10:27:40 -0800618 if (err == -EWOULDBLOCK) {
Andreas Hubereac68ba2011-09-27 12:12:25 -0700619 if (mSource->feedMoreTSData() == OK) {
Andreas Huber1183a4a2011-11-03 11:00:21 -0700620 msg->post(10000ll);
Andreas Huber5bc087c2010-12-23 10:27:40 -0800621 }
Andreas Huberf9334412010-12-15 15:17:42 -0800622 }
Lajos Molnar1cd13982014-01-17 15:12:51 -0800623 } else if (what == Decoder::kWhatEOS) {
Andreas Huberdc9bacd2011-09-26 10:53:29 -0700624 int32_t err;
Lajos Molnar1cd13982014-01-17 15:12:51 -0800625 CHECK(msg->findInt32("err", &err));
Andreas Huberdc9bacd2011-09-26 10:53:29 -0700626
627 if (err == ERROR_END_OF_STREAM) {
Steve Block3856b092011-10-20 11:56:00 +0100628 ALOGV("got %s decoder EOS", audio ? "audio" : "video");
Andreas Huberdc9bacd2011-09-26 10:53:29 -0700629 } else {
Steve Block3856b092011-10-20 11:56:00 +0100630 ALOGV("got %s decoder EOS w/ error %d",
Andreas Huberdc9bacd2011-09-26 10:53:29 -0700631 audio ? "audio" : "video",
632 err);
633 }
634
635 mRenderer->queueEOS(audio, err);
Lajos Molnar1cd13982014-01-17 15:12:51 -0800636 } else if (what == Decoder::kWhatFlushCompleted) {
Andreas Huber1aef2112011-01-04 14:01:29 -0800637 bool needShutdown;
Andreas Huber53df1a42010-12-22 10:03:04 -0800638
Andreas Huberf9334412010-12-15 15:17:42 -0800639 if (audio) {
Andreas Huber1aef2112011-01-04 14:01:29 -0800640 CHECK(IsFlushingState(mFlushingAudio, &needShutdown));
Andreas Huberf9334412010-12-15 15:17:42 -0800641 mFlushingAudio = FLUSHED;
642 } else {
Andreas Huber1aef2112011-01-04 14:01:29 -0800643 CHECK(IsFlushingState(mFlushingVideo, &needShutdown));
Andreas Huberf9334412010-12-15 15:17:42 -0800644 mFlushingVideo = FLUSHED;
Andreas Huber3fe62152011-09-16 15:09:22 -0700645
646 mVideoLateByUs = 0;
Andreas Huberf9334412010-12-15 15:17:42 -0800647 }
648
Steve Block3856b092011-10-20 11:56:00 +0100649 ALOGV("decoder %s flush completed", audio ? "audio" : "video");
Andreas Huberf9334412010-12-15 15:17:42 -0800650
Andreas Huber1aef2112011-01-04 14:01:29 -0800651 if (needShutdown) {
Steve Block3856b092011-10-20 11:56:00 +0100652 ALOGV("initiating %s decoder shutdown",
Andreas Huber53df1a42010-12-22 10:03:04 -0800653 audio ? "audio" : "video");
Andreas Huberf9334412010-12-15 15:17:42 -0800654
Andreas Huber53df1a42010-12-22 10:03:04 -0800655 (audio ? mAudioDecoder : mVideoDecoder)->initiateShutdown();
Andreas Huberf9334412010-12-15 15:17:42 -0800656
Andreas Huber53df1a42010-12-22 10:03:04 -0800657 if (audio) {
658 mFlushingAudio = SHUTTING_DOWN_DECODER;
659 } else {
660 mFlushingVideo = SHUTTING_DOWN_DECODER;
661 }
Andreas Huberf9334412010-12-15 15:17:42 -0800662 }
Andreas Huber3831a062010-12-21 10:22:33 -0800663
664 finishFlushIfPossible();
Lajos Molnar1cd13982014-01-17 15:12:51 -0800665 } else if (what == Decoder::kWhatOutputFormatChanged) {
666 sp<AMessage> format;
667 CHECK(msg->findMessage("format", &format));
668
Andreas Huber31e25082011-01-10 10:38:31 -0800669 if (audio) {
670 int32_t numChannels;
Lajos Molnar1cd13982014-01-17 15:12:51 -0800671 CHECK(format->findInt32(
Andreas Huber516dacf2012-12-03 15:20:40 -0800672 "channel-count", &numChannels));
Andreas Huber2c2814b2010-12-15 17:18:20 -0800673
Andreas Huber31e25082011-01-10 10:38:31 -0800674 int32_t sampleRate;
Lajos Molnar1cd13982014-01-17 15:12:51 -0800675 CHECK(format->findInt32("sample-rate", &sampleRate));
Andreas Huber2c2814b2010-12-15 17:18:20 -0800676
Steve Block3856b092011-10-20 11:56:00 +0100677 ALOGV("Audio output format changed to %d Hz, %d channels",
Andreas Huber31e25082011-01-10 10:38:31 -0800678 sampleRate, numChannels);
Andreas Huber2c2814b2010-12-15 17:18:20 -0800679
Andreas Huber31e25082011-01-10 10:38:31 -0800680 mAudioSink->close();
Eric Laurent1948eb32012-04-13 16:50:19 -0700681
Wei Jiabc2fb722014-07-08 16:37:57 -0700682 uint32_t flags;
Eric Laurent1948eb32012-04-13 16:50:19 -0700683 int64_t durationUs;
Andreas Huber516dacf2012-12-03 15:20:40 -0800684 // FIXME: we should handle the case where the video decoder
685 // is created after we receive the format change indication.
686 // Current code will just make that we select deep buffer
687 // with video which should not be a problem as it should
Eric Laurent1948eb32012-04-13 16:50:19 -0700688 // not prevent from keeping A/V sync.
689 if (mVideoDecoder == NULL &&
690 mSource->getDuration(&durationUs) == OK &&
Andreas Huber516dacf2012-12-03 15:20:40 -0800691 durationUs
692 > AUDIO_SINK_MIN_DEEP_BUFFER_DURATION_US) {
Eric Laurent1948eb32012-04-13 16:50:19 -0700693 flags = AUDIO_OUTPUT_FLAG_DEEP_BUFFER;
694 } else {
695 flags = AUDIO_OUTPUT_FLAG_NONE;
696 }
697
Andreas Huber98065552012-05-03 11:33:01 -0700698 int32_t channelMask;
Lajos Molnar1cd13982014-01-17 15:12:51 -0800699 if (!format->findInt32("channel-mask", &channelMask)) {
Andreas Huber98065552012-05-03 11:33:01 -0700700 channelMask = CHANNEL_MASK_USE_CHANNEL_ORDER;
701 }
702
Wei Jiabc2fb722014-07-08 16:37:57 -0700703 if (mOffloadAudio) {
704 audio_format_t audioFormat = AUDIO_FORMAT_PCM_16_BIT;
705 audio_offload_info_t offloadInfo =
706 AUDIO_INFO_INITIALIZER;
707
708 AString mime;
709 CHECK(format->findString("mime", &mime));
710
711 status_t err =
712 mapMimeToAudioFormat(audioFormat, mime.c_str());
713 if (err != OK) {
714 ALOGE("Couldn't map mime \"%s\" to a valid "
715 "audio_format", mime.c_str());
716 mOffloadAudio = false;
717 } else {
718 ALOGV("Mime \"%s\" mapped to audio_format 0x%x",
719 mime.c_str(), audioFormat);
720
721 flags |= AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD;
722
723 offloadInfo.duration_us = -1;
724 format->findInt64(
725 "durationUs", &offloadInfo.duration_us);
726
727 int avgBitRate = -1;
728 format->findInt32("bit-rate", &avgBitRate);
729
730 offloadInfo.sample_rate = sampleRate;
731 offloadInfo.channel_mask = channelMask;
732 offloadInfo.format = audioFormat;
733 offloadInfo.stream_type = AUDIO_STREAM_MUSIC;
734 offloadInfo.bit_rate = avgBitRate;
735 offloadInfo.has_video = (mVideoDecoder != NULL);
736 offloadInfo.is_streaming = true;
737
738 err = mAudioSink->open(
739 sampleRate,
740 numChannels,
741 (audio_channel_mask_t)channelMask,
742 audioFormat,
743 8 /* bufferCount */,
744 &NuPlayer::Renderer::AudioSinkCallback,
745 mRenderer.get(),
746 (audio_output_flags_t)flags,
747 &offloadInfo);
748
749 if (err == OK) {
750 // If the playback is offloaded to h/w, we pass
751 // the HAL some metadata information.
752 // We don't want to do this for PCM because it
753 // will be going through the AudioFlinger mixer
754 // before reaching the hardware.
755 sp<MetaData> audioMeta =
756 mSource->getFormatMeta(true /* audio */);
757 sendMetaDataToHal(mAudioSink, audioMeta);
758
759 err = mAudioSink->start();
760 }
761 }
762
763 if (err != OK) {
764 // Clean up, fall back to non offload mode.
765 mAudioSink->close();
766 mAudioDecoder.clear();
767 mRenderer->signalDisableOffloadAudio();
768 mOffloadAudio = false;
769
770 instantiateDecoder(
771 true /* audio */, &mAudioDecoder);
772 }
773 }
774
775 if (!mOffloadAudio) {
776 flags &= ~AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD;
777 CHECK_EQ(mAudioSink->open(
778 sampleRate,
779 numChannels,
780 (audio_channel_mask_t)channelMask,
781 AUDIO_FORMAT_PCM_16_BIT,
782 8 /* bufferCount */,
783 NULL,
784 NULL,
785 (audio_output_flags_t)flags),
786 (status_t)OK);
787 mAudioSink->start();
788 }
Andreas Huber2c2814b2010-12-15 17:18:20 -0800789
Andreas Huber31e25082011-01-10 10:38:31 -0800790 mRenderer->signalAudioSinkChanged();
791 } else {
792 // video
Andreas Huber3831a062010-12-21 10:22:33 -0800793
Andreas Huber31e25082011-01-10 10:38:31 -0800794 int32_t width, height;
Lajos Molnar1cd13982014-01-17 15:12:51 -0800795 CHECK(format->findInt32("width", &width));
796 CHECK(format->findInt32("height", &height));
Andreas Huber31e25082011-01-10 10:38:31 -0800797
798 int32_t cropLeft, cropTop, cropRight, cropBottom;
Lajos Molnar1cd13982014-01-17 15:12:51 -0800799 CHECK(format->findRect(
Andreas Huber31e25082011-01-10 10:38:31 -0800800 "crop",
801 &cropLeft, &cropTop, &cropRight, &cropBottom));
802
Andreas Huber516dacf2012-12-03 15:20:40 -0800803 int32_t displayWidth = cropRight - cropLeft + 1;
804 int32_t displayHeight = cropBottom - cropTop + 1;
805
Steve Block3856b092011-10-20 11:56:00 +0100806 ALOGV("Video output format changed to %d x %d "
Andreas Hubercb67cd12011-08-26 16:02:19 -0700807 "(crop: %d x %d @ (%d, %d))",
Andreas Huber31e25082011-01-10 10:38:31 -0800808 width, height,
Andreas Huber516dacf2012-12-03 15:20:40 -0800809 displayWidth,
810 displayHeight,
Andreas Hubercb67cd12011-08-26 16:02:19 -0700811 cropLeft, cropTop);
Andreas Huber31e25082011-01-10 10:38:31 -0800812
Andreas Huber516dacf2012-12-03 15:20:40 -0800813 sp<AMessage> videoInputFormat =
814 mSource->getFormat(false /* audio */);
815
816 // Take into account sample aspect ratio if necessary:
817 int32_t sarWidth, sarHeight;
818 if (videoInputFormat->findInt32("sar-width", &sarWidth)
819 && videoInputFormat->findInt32(
820 "sar-height", &sarHeight)) {
821 ALOGV("Sample aspect ratio %d : %d",
822 sarWidth, sarHeight);
823
824 displayWidth = (displayWidth * sarWidth) / sarHeight;
825
826 ALOGV("display dimensions %d x %d",
827 displayWidth, displayHeight);
828 }
829
Andreas Huber31e25082011-01-10 10:38:31 -0800830 notifyListener(
Andreas Huber516dacf2012-12-03 15:20:40 -0800831 MEDIA_SET_VIDEO_SIZE, displayWidth, displayHeight);
Andreas Huber31e25082011-01-10 10:38:31 -0800832 }
Lajos Molnar1cd13982014-01-17 15:12:51 -0800833 } else if (what == Decoder::kWhatShutdownCompleted) {
Steve Block3856b092011-10-20 11:56:00 +0100834 ALOGV("%s shutdown completed", audio ? "audio" : "video");
Andreas Huber3831a062010-12-21 10:22:33 -0800835 if (audio) {
836 mAudioDecoder.clear();
837
838 CHECK_EQ((int)mFlushingAudio, (int)SHUTTING_DOWN_DECODER);
839 mFlushingAudio = SHUT_DOWN;
840 } else {
841 mVideoDecoder.clear();
842
843 CHECK_EQ((int)mFlushingVideo, (int)SHUTTING_DOWN_DECODER);
844 mFlushingVideo = SHUT_DOWN;
845 }
846
847 finishFlushIfPossible();
Lajos Molnar1cd13982014-01-17 15:12:51 -0800848 } else if (what == Decoder::kWhatError) {
Steve Block29357bc2012-01-06 19:20:56 +0000849 ALOGE("Received error from %s decoder, aborting playback.",
Andreas Huberc92fd242011-08-16 13:48:44 -0700850 audio ? "audio" : "video");
851
852 mRenderer->queueEOS(audio, UNKNOWN_ERROR);
Lajos Molnar1cd13982014-01-17 15:12:51 -0800853 } else if (what == Decoder::kWhatDrainThisBuffer) {
854 renderBuffer(audio, msg);
855 } else {
856 ALOGV("Unhandled decoder notification %d '%c%c%c%c'.",
Andreas Hubera1f8ab02012-11-30 10:53:22 -0800857 what,
858 what >> 24,
859 (what >> 16) & 0xff,
860 (what >> 8) & 0xff,
861 what & 0xff);
Andreas Huberf9334412010-12-15 15:17:42 -0800862 }
863
864 break;
865 }
866
867 case kWhatRendererNotify:
868 {
869 int32_t what;
870 CHECK(msg->findInt32("what", &what));
871
872 if (what == Renderer::kWhatEOS) {
873 int32_t audio;
874 CHECK(msg->findInt32("audio", &audio));
875
Andreas Huberc92fd242011-08-16 13:48:44 -0700876 int32_t finalResult;
877 CHECK(msg->findInt32("finalResult", &finalResult));
878
Andreas Huberf9334412010-12-15 15:17:42 -0800879 if (audio) {
880 mAudioEOS = true;
881 } else {
882 mVideoEOS = true;
883 }
884
Andreas Huberc92fd242011-08-16 13:48:44 -0700885 if (finalResult == ERROR_END_OF_STREAM) {
Steve Block3856b092011-10-20 11:56:00 +0100886 ALOGV("reached %s EOS", audio ? "audio" : "video");
Andreas Huberc92fd242011-08-16 13:48:44 -0700887 } else {
Steve Block29357bc2012-01-06 19:20:56 +0000888 ALOGE("%s track encountered an error (%d)",
Andreas Huberc92fd242011-08-16 13:48:44 -0700889 audio ? "audio" : "video", finalResult);
890
891 notifyListener(
892 MEDIA_ERROR, MEDIA_ERROR_UNKNOWN, finalResult);
893 }
Andreas Huberf9334412010-12-15 15:17:42 -0800894
895 if ((mAudioEOS || mAudioDecoder == NULL)
896 && (mVideoEOS || mVideoDecoder == NULL)) {
897 notifyListener(MEDIA_PLAYBACK_COMPLETE, 0, 0);
898 }
Andreas Huber43c3e6c2011-01-05 12:17:08 -0800899 } else if (what == Renderer::kWhatPosition) {
900 int64_t positionUs;
901 CHECK(msg->findInt64("positionUs", &positionUs));
902
Andreas Huber3fe62152011-09-16 15:09:22 -0700903 CHECK(msg->findInt64("videoLateByUs", &mVideoLateByUs));
904
Andreas Huber43c3e6c2011-01-05 12:17:08 -0800905 if (mDriver != NULL) {
906 sp<NuPlayerDriver> driver = mDriver.promote();
907 if (driver != NULL) {
908 driver->notifyPosition(positionUs);
Andreas Huber3fe62152011-09-16 15:09:22 -0700909
910 driver->notifyFrameStats(
911 mNumFramesTotal, mNumFramesDropped);
Andreas Huber43c3e6c2011-01-05 12:17:08 -0800912 }
913 }
Andreas Huber3fe62152011-09-16 15:09:22 -0700914 } else if (what == Renderer::kWhatFlushComplete) {
Andreas Huberf9334412010-12-15 15:17:42 -0800915 int32_t audio;
916 CHECK(msg->findInt32("audio", &audio));
917
Steve Block3856b092011-10-20 11:56:00 +0100918 ALOGV("renderer %s flush completed.", audio ? "audio" : "video");
James Dongf57b4ea2012-07-20 13:38:36 -0700919 } else if (what == Renderer::kWhatVideoRenderingStart) {
920 notifyListener(MEDIA_INFO, MEDIA_INFO_RENDERING_START, 0);
Lajos Molnarcbaffcf2013-08-14 18:30:38 -0700921 } else if (what == Renderer::kWhatMediaRenderingStart) {
922 ALOGV("media rendering started");
923 notifyListener(MEDIA_STARTED, 0, 0);
Andreas Huberf9334412010-12-15 15:17:42 -0800924 }
925 break;
926 }
927
928 case kWhatMoreDataQueued:
929 {
930 break;
931 }
932
Andreas Huber1aef2112011-01-04 14:01:29 -0800933 case kWhatReset:
934 {
Steve Block3856b092011-10-20 11:56:00 +0100935 ALOGV("kWhatReset");
Andreas Huber1aef2112011-01-04 14:01:29 -0800936
Andreas Hubera1f8ab02012-11-30 10:53:22 -0800937 mDeferredActions.push_back(
Andreas Huber14f76722013-01-15 09:04:18 -0800938 new ShutdownDecoderAction(
939 true /* audio */, true /* video */));
Andreas Huberb7c8e912012-11-27 15:02:53 -0800940
Andreas Hubera1f8ab02012-11-30 10:53:22 -0800941 mDeferredActions.push_back(
942 new SimpleAction(&NuPlayer::performReset));
Andreas Huberb58ce9f2011-11-28 16:27:35 -0800943
Andreas Hubera1f8ab02012-11-30 10:53:22 -0800944 processDeferredActions();
Andreas Huber1aef2112011-01-04 14:01:29 -0800945 break;
946 }
947
Andreas Huber43c3e6c2011-01-05 12:17:08 -0800948 case kWhatSeek:
949 {
950 int64_t seekTimeUs;
951 CHECK(msg->findInt64("seekTimeUs", &seekTimeUs));
952
Andreas Hubera1f8ab02012-11-30 10:53:22 -0800953 ALOGV("kWhatSeek seekTimeUs=%lld us", seekTimeUs);
Andreas Huber43c3e6c2011-01-05 12:17:08 -0800954
Andreas Hubera1f8ab02012-11-30 10:53:22 -0800955 mDeferredActions.push_back(
956 new SimpleAction(&NuPlayer::performDecoderFlush));
Andreas Huber43c3e6c2011-01-05 12:17:08 -0800957
Andreas Hubera1f8ab02012-11-30 10:53:22 -0800958 mDeferredActions.push_back(new SeekAction(seekTimeUs));
Andreas Huber43c3e6c2011-01-05 12:17:08 -0800959
Andreas Hubera1f8ab02012-11-30 10:53:22 -0800960 processDeferredActions();
Andreas Huber43c3e6c2011-01-05 12:17:08 -0800961 break;
962 }
963
Andreas Huberb4082222011-01-20 15:23:04 -0800964 case kWhatPause:
965 {
966 CHECK(mRenderer != NULL);
Roger Jönssonfba60da2013-01-21 17:15:45 +0100967 mSource->pause();
Andreas Huberb4082222011-01-20 15:23:04 -0800968 mRenderer->pause();
969 break;
970 }
971
972 case kWhatResume:
973 {
974 CHECK(mRenderer != NULL);
Roger Jönssonfba60da2013-01-21 17:15:45 +0100975 mSource->resume();
Andreas Huberb4082222011-01-20 15:23:04 -0800976 mRenderer->resume();
977 break;
978 }
979
Andreas Huberb5f25f02013-02-05 10:14:26 -0800980 case kWhatSourceNotify:
981 {
Andreas Huber9575c962013-02-05 13:59:56 -0800982 onSourceNotify(msg);
Andreas Huberb5f25f02013-02-05 10:14:26 -0800983 break;
984 }
985
Chong Zhanga7fa1d92014-06-11 14:49:23 -0700986 case kWhatClosedCaptionNotify:
987 {
988 onClosedCaptionNotify(msg);
989 break;
990 }
991
Andreas Huberf9334412010-12-15 15:17:42 -0800992 default:
993 TRESPASS();
994 break;
995 }
996}
997
Andreas Huber3831a062010-12-21 10:22:33 -0800998void NuPlayer::finishFlushIfPossible() {
999 if (mFlushingAudio != FLUSHED && mFlushingAudio != SHUT_DOWN) {
1000 return;
1001 }
1002
1003 if (mFlushingVideo != FLUSHED && mFlushingVideo != SHUT_DOWN) {
1004 return;
1005 }
1006
Steve Block3856b092011-10-20 11:56:00 +01001007 ALOGV("both audio and video are flushed now.");
Andreas Huber3831a062010-12-21 10:22:33 -08001008
Andreas Huber6e3d3112011-11-28 12:36:11 -08001009 if (mTimeDiscontinuityPending) {
1010 mRenderer->signalTimeDiscontinuity();
1011 mTimeDiscontinuityPending = false;
1012 }
Andreas Huber3831a062010-12-21 10:22:33 -08001013
Andreas Huber22fc52f2011-01-05 16:24:27 -08001014 if (mAudioDecoder != NULL) {
Andreas Huber3831a062010-12-21 10:22:33 -08001015 mAudioDecoder->signalResume();
1016 }
1017
Andreas Huber22fc52f2011-01-05 16:24:27 -08001018 if (mVideoDecoder != NULL) {
Andreas Huber3831a062010-12-21 10:22:33 -08001019 mVideoDecoder->signalResume();
1020 }
1021
1022 mFlushingAudio = NONE;
1023 mFlushingVideo = NONE;
Andreas Huber3831a062010-12-21 10:22:33 -08001024
Andreas Hubera1f8ab02012-11-30 10:53:22 -08001025 processDeferredActions();
Andreas Huber1aef2112011-01-04 14:01:29 -08001026}
1027
1028void NuPlayer::postScanSources() {
1029 if (mScanSourcesPending) {
1030 return;
1031 }
1032
1033 sp<AMessage> msg = new AMessage(kWhatScanSources, id());
1034 msg->setInt32("generation", mScanSourcesGeneration);
1035 msg->post();
1036
1037 mScanSourcesPending = true;
1038}
1039
Andreas Huber5bc087c2010-12-23 10:27:40 -08001040status_t NuPlayer::instantiateDecoder(bool audio, sp<Decoder> *decoder) {
Andreas Huberf9334412010-12-15 15:17:42 -08001041 if (*decoder != NULL) {
1042 return OK;
1043 }
1044
Andreas Huber84066782011-08-16 09:34:26 -07001045 sp<AMessage> format = mSource->getFormat(audio);
Andreas Huberf9334412010-12-15 15:17:42 -08001046
Andreas Huber84066782011-08-16 09:34:26 -07001047 if (format == NULL) {
Andreas Huberf9334412010-12-15 15:17:42 -08001048 return -EWOULDBLOCK;
1049 }
1050
Andreas Huber3fe62152011-09-16 15:09:22 -07001051 if (!audio) {
Andreas Huber84066782011-08-16 09:34:26 -07001052 AString mime;
1053 CHECK(format->findString("mime", &mime));
1054 mVideoIsAVC = !strcasecmp(MEDIA_MIMETYPE_VIDEO_AVC, mime.c_str());
Chong Zhanga7fa1d92014-06-11 14:49:23 -07001055
1056 sp<AMessage> ccNotify = new AMessage(kWhatClosedCaptionNotify, id());
1057 mCCDecoder = new CCDecoder(ccNotify);
Andreas Huber3fe62152011-09-16 15:09:22 -07001058 }
1059
Andreas Huberf9334412010-12-15 15:17:42 -08001060 sp<AMessage> notify =
1061 new AMessage(audio ? kWhatAudioNotify : kWhatVideoNotify,
1062 id());
1063
Wei Jiabc2fb722014-07-08 16:37:57 -07001064 if (audio) {
1065 if (mOffloadAudio) {
1066 *decoder = new DecoderPassThrough(notify);
1067 } else {
1068 *decoder = new Decoder(notify);
1069 }
1070 } else {
1071 *decoder = new Decoder(notify, mNativeWindow);
1072 }
Lajos Molnar1cd13982014-01-17 15:12:51 -08001073 (*decoder)->init();
Andreas Huber84066782011-08-16 09:34:26 -07001074 (*decoder)->configure(format);
Andreas Huberf9334412010-12-15 15:17:42 -08001075
Andreas Huberf9334412010-12-15 15:17:42 -08001076 return OK;
1077}
1078
1079status_t NuPlayer::feedDecoderInputData(bool audio, const sp<AMessage> &msg) {
1080 sp<AMessage> reply;
1081 CHECK(msg->findMessage("reply", &reply));
1082
Andreas Huber53df1a42010-12-22 10:03:04 -08001083 if ((audio && IsFlushingState(mFlushingAudio))
1084 || (!audio && IsFlushingState(mFlushingVideo))) {
Andreas Huberf9334412010-12-15 15:17:42 -08001085 reply->setInt32("err", INFO_DISCONTINUITY);
1086 reply->post();
1087 return OK;
1088 }
1089
1090 sp<ABuffer> accessUnit;
Andreas Huberf9334412010-12-15 15:17:42 -08001091
Andreas Huber3fe62152011-09-16 15:09:22 -07001092 bool dropAccessUnit;
1093 do {
1094 status_t err = mSource->dequeueAccessUnit(audio, &accessUnit);
Andreas Huber5bc087c2010-12-23 10:27:40 -08001095
Andreas Huber3fe62152011-09-16 15:09:22 -07001096 if (err == -EWOULDBLOCK) {
1097 return err;
1098 } else if (err != OK) {
1099 if (err == INFO_DISCONTINUITY) {
1100 int32_t type;
1101 CHECK(accessUnit->meta()->findInt32("discontinuity", &type));
Andreas Huber53df1a42010-12-22 10:03:04 -08001102
Andreas Huber3fe62152011-09-16 15:09:22 -07001103 bool formatChange =
Andreas Huber6e3d3112011-11-28 12:36:11 -08001104 (audio &&
1105 (type & ATSParser::DISCONTINUITY_AUDIO_FORMAT))
1106 || (!audio &&
1107 (type & ATSParser::DISCONTINUITY_VIDEO_FORMAT));
Andreas Huber53df1a42010-12-22 10:03:04 -08001108
Andreas Huber6e3d3112011-11-28 12:36:11 -08001109 bool timeChange = (type & ATSParser::DISCONTINUITY_TIME) != 0;
1110
Steve Blockdf64d152012-01-04 20:05:49 +00001111 ALOGI("%s discontinuity (formatChange=%d, time=%d)",
Andreas Huber6e3d3112011-11-28 12:36:11 -08001112 audio ? "audio" : "video", formatChange, timeChange);
Andreas Huber32f3cef2011-03-02 15:34:46 -08001113
Andreas Huber3fe62152011-09-16 15:09:22 -07001114 if (audio) {
1115 mSkipRenderingAudioUntilMediaTimeUs = -1;
1116 } else {
1117 mSkipRenderingVideoUntilMediaTimeUs = -1;
1118 }
Andreas Huber32f3cef2011-03-02 15:34:46 -08001119
Andreas Huber6e3d3112011-11-28 12:36:11 -08001120 if (timeChange) {
1121 sp<AMessage> extra;
1122 if (accessUnit->meta()->findMessage("extra", &extra)
1123 && extra != NULL) {
1124 int64_t resumeAtMediaTimeUs;
1125 if (extra->findInt64(
1126 "resume-at-mediatimeUs", &resumeAtMediaTimeUs)) {
Steve Blockdf64d152012-01-04 20:05:49 +00001127 ALOGI("suppressing rendering of %s until %lld us",
Andreas Huber6e3d3112011-11-28 12:36:11 -08001128 audio ? "audio" : "video", resumeAtMediaTimeUs);
Andreas Huber3fe62152011-09-16 15:09:22 -07001129
Andreas Huber6e3d3112011-11-28 12:36:11 -08001130 if (audio) {
1131 mSkipRenderingAudioUntilMediaTimeUs =
1132 resumeAtMediaTimeUs;
1133 } else {
1134 mSkipRenderingVideoUntilMediaTimeUs =
1135 resumeAtMediaTimeUs;
1136 }
Andreas Huber3fe62152011-09-16 15:09:22 -07001137 }
Andreas Huber32f3cef2011-03-02 15:34:46 -08001138 }
1139 }
Andreas Huber3fe62152011-09-16 15:09:22 -07001140
Andreas Huber6e3d3112011-11-28 12:36:11 -08001141 mTimeDiscontinuityPending =
1142 mTimeDiscontinuityPending || timeChange;
1143
1144 if (formatChange || timeChange) {
Andreas Hubera1f8ab02012-11-30 10:53:22 -08001145 if (mFlushingAudio == NONE && mFlushingVideo == NONE) {
1146 // And we'll resume scanning sources once we're done
1147 // flushing.
1148 mDeferredActions.push_front(
1149 new SimpleAction(
1150 &NuPlayer::performScanSources));
1151 }
1152
Robert Shih6d0a94e2014-01-23 16:18:22 -08001153 sp<AMessage> newFormat = mSource->getFormat(audio);
1154 sp<Decoder> &decoder = audio ? mAudioDecoder : mVideoDecoder;
1155 if (formatChange && !decoder->supportsSeamlessFormatChange(newFormat)) {
1156 flushDecoder(audio, /* needShutdown = */ true);
1157 } else {
1158 flushDecoder(audio, /* needShutdown = */ false);
1159 err = OK;
1160 }
Andreas Huber6e3d3112011-11-28 12:36:11 -08001161 } else {
1162 // This stream is unaffected by the discontinuity
1163
1164 if (audio) {
1165 mFlushingAudio = FLUSHED;
1166 } else {
1167 mFlushingVideo = FLUSHED;
1168 }
1169
1170 finishFlushIfPossible();
1171
1172 return -EWOULDBLOCK;
1173 }
Andreas Huber32f3cef2011-03-02 15:34:46 -08001174 }
1175
Andreas Huber3fe62152011-09-16 15:09:22 -07001176 reply->setInt32("err", err);
1177 reply->post();
1178 return OK;
Andreas Huberf9334412010-12-15 15:17:42 -08001179 }
1180
Andreas Huber3fe62152011-09-16 15:09:22 -07001181 if (!audio) {
1182 ++mNumFramesTotal;
1183 }
1184
1185 dropAccessUnit = false;
1186 if (!audio
1187 && mVideoLateByUs > 100000ll
1188 && mVideoIsAVC
1189 && !IsAVCReferenceFrame(accessUnit)) {
1190 dropAccessUnit = true;
1191 ++mNumFramesDropped;
1192 }
1193 } while (dropAccessUnit);
Andreas Huberf9334412010-12-15 15:17:42 -08001194
Steve Block3856b092011-10-20 11:56:00 +01001195 // ALOGV("returned a valid buffer of %s data", audio ? "audio" : "video");
Andreas Huberf9334412010-12-15 15:17:42 -08001196
1197#if 0
1198 int64_t mediaTimeUs;
1199 CHECK(accessUnit->meta()->findInt64("timeUs", &mediaTimeUs));
Steve Block3856b092011-10-20 11:56:00 +01001200 ALOGV("feeding %s input buffer at media time %.2f secs",
Andreas Huberf9334412010-12-15 15:17:42 -08001201 audio ? "audio" : "video",
1202 mediaTimeUs / 1E6);
1203#endif
1204
Chong Zhanga7fa1d92014-06-11 14:49:23 -07001205 if (!audio) {
1206 mCCDecoder->decode(accessUnit);
1207 }
1208
Andreas Huber2d8bedd2012-02-21 14:38:23 -08001209 reply->setBuffer("buffer", accessUnit);
Andreas Huberf9334412010-12-15 15:17:42 -08001210 reply->post();
1211
1212 return OK;
1213}
1214
1215void NuPlayer::renderBuffer(bool audio, const sp<AMessage> &msg) {
Steve Block3856b092011-10-20 11:56:00 +01001216 // ALOGV("renderBuffer %s", audio ? "audio" : "video");
Andreas Huberf9334412010-12-15 15:17:42 -08001217
1218 sp<AMessage> reply;
1219 CHECK(msg->findMessage("reply", &reply));
1220
Andreas Huber18ac5402011-08-31 15:04:25 -07001221 if (IsFlushingState(audio ? mFlushingAudio : mFlushingVideo)) {
1222 // We're currently attempting to flush the decoder, in order
1223 // to complete this, the decoder wants all its buffers back,
1224 // so we don't want any output buffers it sent us (from before
1225 // we initiated the flush) to be stuck in the renderer's queue.
1226
Steve Block3856b092011-10-20 11:56:00 +01001227 ALOGV("we're still flushing the %s decoder, sending its output buffer"
Andreas Huber18ac5402011-08-31 15:04:25 -07001228 " right back.", audio ? "audio" : "video");
1229
1230 reply->post();
1231 return;
1232 }
1233
Andreas Huber2d8bedd2012-02-21 14:38:23 -08001234 sp<ABuffer> buffer;
1235 CHECK(msg->findBuffer("buffer", &buffer));
Andreas Huberf9334412010-12-15 15:17:42 -08001236
Chong Zhanga7fa1d92014-06-11 14:49:23 -07001237 int64_t mediaTimeUs;
1238 CHECK(buffer->meta()->findInt64("timeUs", &mediaTimeUs));
1239
Andreas Huber32f3cef2011-03-02 15:34:46 -08001240 int64_t &skipUntilMediaTimeUs =
1241 audio
1242 ? mSkipRenderingAudioUntilMediaTimeUs
1243 : mSkipRenderingVideoUntilMediaTimeUs;
1244
1245 if (skipUntilMediaTimeUs >= 0) {
Andreas Huber32f3cef2011-03-02 15:34:46 -08001246
1247 if (mediaTimeUs < skipUntilMediaTimeUs) {
Steve Block3856b092011-10-20 11:56:00 +01001248 ALOGV("dropping %s buffer at time %lld as requested.",
Andreas Huber32f3cef2011-03-02 15:34:46 -08001249 audio ? "audio" : "video",
1250 mediaTimeUs);
1251
1252 reply->post();
1253 return;
1254 }
1255
1256 skipUntilMediaTimeUs = -1;
1257 }
1258
Chong Zhanga7fa1d92014-06-11 14:49:23 -07001259 if (!audio && mCCDecoder->isSelected()) {
1260 mCCDecoder->display(mediaTimeUs);
1261 }
1262
Andreas Huberf9334412010-12-15 15:17:42 -08001263 mRenderer->queueBuffer(audio, buffer, reply);
1264}
1265
Chong Zhangdcb89b32013-08-06 09:44:47 -07001266void NuPlayer::notifyListener(int msg, int ext1, int ext2, const Parcel *in) {
Andreas Huber43c3e6c2011-01-05 12:17:08 -08001267 if (mDriver == NULL) {
Andreas Huberf9334412010-12-15 15:17:42 -08001268 return;
1269 }
1270
Andreas Huber43c3e6c2011-01-05 12:17:08 -08001271 sp<NuPlayerDriver> driver = mDriver.promote();
Andreas Huberf9334412010-12-15 15:17:42 -08001272
Andreas Huber43c3e6c2011-01-05 12:17:08 -08001273 if (driver == NULL) {
Andreas Huberf9334412010-12-15 15:17:42 -08001274 return;
1275 }
1276
Chong Zhangdcb89b32013-08-06 09:44:47 -07001277 driver->notifyListener(msg, ext1, ext2, in);
Andreas Huberf9334412010-12-15 15:17:42 -08001278}
1279
Andreas Huber1aef2112011-01-04 14:01:29 -08001280void NuPlayer::flushDecoder(bool audio, bool needShutdown) {
Andreas Huber14f76722013-01-15 09:04:18 -08001281 ALOGV("[%s] flushDecoder needShutdown=%d",
1282 audio ? "audio" : "video", needShutdown);
1283
Andreas Huber6e3d3112011-11-28 12:36:11 -08001284 if ((audio && mAudioDecoder == NULL) || (!audio && mVideoDecoder == NULL)) {
Steve Blockdf64d152012-01-04 20:05:49 +00001285 ALOGI("flushDecoder %s without decoder present",
Andreas Huber6e3d3112011-11-28 12:36:11 -08001286 audio ? "audio" : "video");
1287 }
1288
Andreas Huber1aef2112011-01-04 14:01:29 -08001289 // Make sure we don't continue to scan sources until we finish flushing.
1290 ++mScanSourcesGeneration;
Andreas Huber43c3e6c2011-01-05 12:17:08 -08001291 mScanSourcesPending = false;
Andreas Huber1aef2112011-01-04 14:01:29 -08001292
1293 (audio ? mAudioDecoder : mVideoDecoder)->signalFlush();
1294 mRenderer->flush(audio);
1295
1296 FlushStatus newStatus =
1297 needShutdown ? FLUSHING_DECODER_SHUTDOWN : FLUSHING_DECODER;
1298
1299 if (audio) {
1300 CHECK(mFlushingAudio == NONE
1301 || mFlushingAudio == AWAITING_DISCONTINUITY);
1302
1303 mFlushingAudio = newStatus;
1304
1305 if (mFlushingVideo == NONE) {
1306 mFlushingVideo = (mVideoDecoder != NULL)
1307 ? AWAITING_DISCONTINUITY
1308 : FLUSHED;
1309 }
1310 } else {
1311 CHECK(mFlushingVideo == NONE
1312 || mFlushingVideo == AWAITING_DISCONTINUITY);
1313
1314 mFlushingVideo = newStatus;
1315
1316 if (mFlushingAudio == NONE) {
1317 mFlushingAudio = (mAudioDecoder != NULL)
1318 ? AWAITING_DISCONTINUITY
1319 : FLUSHED;
1320 }
1321 }
1322}
1323
Andreas Huber84066782011-08-16 09:34:26 -07001324sp<AMessage> NuPlayer::Source::getFormat(bool audio) {
1325 sp<MetaData> meta = getFormatMeta(audio);
1326
1327 if (meta == NULL) {
1328 return NULL;
1329 }
1330
1331 sp<AMessage> msg = new AMessage;
1332
1333 if(convertMetaDataToMessage(meta, &msg) == OK) {
1334 return msg;
1335 }
1336 return NULL;
1337}
1338
James Dong0d268a32012-08-31 12:18:27 -07001339status_t NuPlayer::setVideoScalingMode(int32_t mode) {
1340 mVideoScalingMode = mode;
Andreas Huber57a339c2012-12-03 11:18:00 -08001341 if (mNativeWindow != NULL) {
James Dong0d268a32012-08-31 12:18:27 -07001342 status_t ret = native_window_set_scaling_mode(
1343 mNativeWindow->getNativeWindow().get(), mVideoScalingMode);
1344 if (ret != OK) {
1345 ALOGE("Failed to set scaling mode (%d): %s",
1346 -ret, strerror(-ret));
1347 return ret;
1348 }
1349 }
1350 return OK;
1351}
1352
Chong Zhangdcb89b32013-08-06 09:44:47 -07001353status_t NuPlayer::getTrackInfo(Parcel* reply) const {
1354 sp<AMessage> msg = new AMessage(kWhatGetTrackInfo, id());
1355 msg->setPointer("reply", reply);
1356
1357 sp<AMessage> response;
1358 status_t err = msg->postAndAwaitResponse(&response);
1359 return err;
1360}
1361
1362status_t NuPlayer::selectTrack(size_t trackIndex, bool select) {
1363 sp<AMessage> msg = new AMessage(kWhatSelectTrack, id());
1364 msg->setSize("trackIndex", trackIndex);
1365 msg->setInt32("select", select);
1366
1367 sp<AMessage> response;
1368 status_t err = msg->postAndAwaitResponse(&response);
1369
Chong Zhang404fced2014-06-11 14:45:31 -07001370 if (err != OK) {
1371 return err;
1372 }
1373
1374 if (!response->findInt32("err", &err)) {
1375 err = OK;
1376 }
1377
Chong Zhangdcb89b32013-08-06 09:44:47 -07001378 return err;
1379}
1380
Andreas Huberb7c8e912012-11-27 15:02:53 -08001381void NuPlayer::schedulePollDuration() {
1382 sp<AMessage> msg = new AMessage(kWhatPollDuration, id());
1383 msg->setInt32("generation", mPollDurationGeneration);
1384 msg->post();
1385}
1386
1387void NuPlayer::cancelPollDuration() {
1388 ++mPollDurationGeneration;
1389}
1390
Andreas Hubera1f8ab02012-11-30 10:53:22 -08001391void NuPlayer::processDeferredActions() {
1392 while (!mDeferredActions.empty()) {
1393 // We won't execute any deferred actions until we're no longer in
1394 // an intermediate state, i.e. one more more decoders are currently
1395 // flushing or shutting down.
1396
1397 if (mRenderer != NULL) {
1398 // There's an edge case where the renderer owns all output
1399 // buffers and is paused, therefore the decoder will not read
1400 // more input data and will never encounter the matching
1401 // discontinuity. To avoid this, we resume the renderer.
1402
1403 if (mFlushingAudio == AWAITING_DISCONTINUITY
1404 || mFlushingVideo == AWAITING_DISCONTINUITY) {
1405 mRenderer->resume();
1406 }
1407 }
1408
1409 if (mFlushingAudio != NONE || mFlushingVideo != NONE) {
1410 // We're currently flushing, postpone the reset until that's
1411 // completed.
1412
1413 ALOGV("postponing action mFlushingAudio=%d, mFlushingVideo=%d",
1414 mFlushingAudio, mFlushingVideo);
1415
1416 break;
1417 }
1418
1419 sp<Action> action = *mDeferredActions.begin();
1420 mDeferredActions.erase(mDeferredActions.begin());
1421
1422 action->execute(this);
1423 }
1424}
1425
1426void NuPlayer::performSeek(int64_t seekTimeUs) {
1427 ALOGV("performSeek seekTimeUs=%lld us (%.2f secs)",
1428 seekTimeUs,
1429 seekTimeUs / 1E6);
1430
1431 mSource->seekTo(seekTimeUs);
1432
1433 if (mDriver != NULL) {
1434 sp<NuPlayerDriver> driver = mDriver.promote();
1435 if (driver != NULL) {
1436 driver->notifyPosition(seekTimeUs);
1437 driver->notifySeekComplete();
1438 }
1439 }
1440
1441 // everything's flushed, continue playback.
1442}
1443
1444void NuPlayer::performDecoderFlush() {
1445 ALOGV("performDecoderFlush");
1446
Andreas Huberda9740e2013-04-16 10:54:03 -07001447 if (mAudioDecoder == NULL && mVideoDecoder == NULL) {
Andreas Hubera1f8ab02012-11-30 10:53:22 -08001448 return;
1449 }
1450
1451 mTimeDiscontinuityPending = true;
1452
1453 if (mAudioDecoder != NULL) {
1454 flushDecoder(true /* audio */, false /* needShutdown */);
1455 }
1456
1457 if (mVideoDecoder != NULL) {
1458 flushDecoder(false /* audio */, false /* needShutdown */);
1459 }
1460}
1461
Andreas Huber14f76722013-01-15 09:04:18 -08001462void NuPlayer::performDecoderShutdown(bool audio, bool video) {
1463 ALOGV("performDecoderShutdown audio=%d, video=%d", audio, video);
Andreas Hubera1f8ab02012-11-30 10:53:22 -08001464
Andreas Huber14f76722013-01-15 09:04:18 -08001465 if ((!audio || mAudioDecoder == NULL)
1466 && (!video || mVideoDecoder == NULL)) {
Andreas Hubera1f8ab02012-11-30 10:53:22 -08001467 return;
1468 }
1469
1470 mTimeDiscontinuityPending = true;
1471
Andreas Huber14f76722013-01-15 09:04:18 -08001472 if (mFlushingAudio == NONE && (!audio || mAudioDecoder == NULL)) {
1473 mFlushingAudio = FLUSHED;
1474 }
1475
1476 if (mFlushingVideo == NONE && (!video || mVideoDecoder == NULL)) {
1477 mFlushingVideo = FLUSHED;
1478 }
1479
1480 if (audio && mAudioDecoder != NULL) {
Andreas Hubera1f8ab02012-11-30 10:53:22 -08001481 flushDecoder(true /* audio */, true /* needShutdown */);
1482 }
1483
Andreas Huber14f76722013-01-15 09:04:18 -08001484 if (video && mVideoDecoder != NULL) {
Andreas Hubera1f8ab02012-11-30 10:53:22 -08001485 flushDecoder(false /* audio */, true /* needShutdown */);
1486 }
1487}
1488
1489void NuPlayer::performReset() {
1490 ALOGV("performReset");
1491
1492 CHECK(mAudioDecoder == NULL);
1493 CHECK(mVideoDecoder == NULL);
1494
1495 cancelPollDuration();
1496
1497 ++mScanSourcesGeneration;
1498 mScanSourcesPending = false;
1499
1500 mRenderer.clear();
1501
1502 if (mSource != NULL) {
1503 mSource->stop();
Andreas Huberb5f25f02013-02-05 10:14:26 -08001504
1505 looper()->unregisterHandler(mSource->id());
1506
Andreas Hubera1f8ab02012-11-30 10:53:22 -08001507 mSource.clear();
1508 }
1509
1510 if (mDriver != NULL) {
1511 sp<NuPlayerDriver> driver = mDriver.promote();
1512 if (driver != NULL) {
1513 driver->notifyResetComplete();
1514 }
1515 }
Andreas Huber57a339c2012-12-03 11:18:00 -08001516
1517 mStarted = false;
Andreas Hubera1f8ab02012-11-30 10:53:22 -08001518}
1519
1520void NuPlayer::performScanSources() {
1521 ALOGV("performScanSources");
1522
Andreas Huber57a339c2012-12-03 11:18:00 -08001523 if (!mStarted) {
1524 return;
1525 }
1526
Andreas Hubera1f8ab02012-11-30 10:53:22 -08001527 if (mAudioDecoder == NULL || mVideoDecoder == NULL) {
1528 postScanSources();
1529 }
1530}
1531
Andreas Huber57a339c2012-12-03 11:18:00 -08001532void NuPlayer::performSetSurface(const sp<NativeWindowWrapper> &wrapper) {
1533 ALOGV("performSetSurface");
1534
1535 mNativeWindow = wrapper;
1536
1537 // XXX - ignore error from setVideoScalingMode for now
1538 setVideoScalingMode(mVideoScalingMode);
1539
1540 if (mDriver != NULL) {
1541 sp<NuPlayerDriver> driver = mDriver.promote();
1542 if (driver != NULL) {
1543 driver->notifySetSurfaceComplete();
1544 }
1545 }
1546}
1547
Andreas Huber9575c962013-02-05 13:59:56 -08001548void NuPlayer::onSourceNotify(const sp<AMessage> &msg) {
1549 int32_t what;
1550 CHECK(msg->findInt32("what", &what));
1551
1552 switch (what) {
1553 case Source::kWhatPrepared:
1554 {
Andreas Huberb5f28d42013-04-25 15:11:19 -07001555 if (mSource == NULL) {
1556 // This is a stale notification from a source that was
1557 // asynchronously preparing when the client called reset().
1558 // We handled the reset, the source is gone.
1559 break;
1560 }
1561
Andreas Huberec0c5972013-02-05 14:47:13 -08001562 int32_t err;
1563 CHECK(msg->findInt32("err", &err));
1564
Andreas Huber9575c962013-02-05 13:59:56 -08001565 sp<NuPlayerDriver> driver = mDriver.promote();
1566 if (driver != NULL) {
Marco Nelissendd114d12014-05-28 15:23:14 -07001567 // notify duration first, so that it's definitely set when
1568 // the app received the "prepare complete" callback.
1569 int64_t durationUs;
1570 if (mSource->getDuration(&durationUs) == OK) {
1571 driver->notifyDuration(durationUs);
1572 }
Andreas Huberec0c5972013-02-05 14:47:13 -08001573 driver->notifyPrepareCompleted(err);
Andreas Huber9575c962013-02-05 13:59:56 -08001574 }
Andreas Huber99759402013-04-01 14:28:31 -07001575
Andreas Huber9575c962013-02-05 13:59:56 -08001576 break;
1577 }
1578
1579 case Source::kWhatFlagsChanged:
1580 {
1581 uint32_t flags;
1582 CHECK(msg->findInt32("flags", (int32_t *)&flags));
1583
Chong Zhang4b7069d2013-09-11 12:52:43 -07001584 sp<NuPlayerDriver> driver = mDriver.promote();
1585 if (driver != NULL) {
1586 driver->notifyFlagsChanged(flags);
1587 }
1588
Andreas Huber9575c962013-02-05 13:59:56 -08001589 if ((mSourceFlags & Source::FLAG_DYNAMIC_DURATION)
1590 && (!(flags & Source::FLAG_DYNAMIC_DURATION))) {
1591 cancelPollDuration();
1592 } else if (!(mSourceFlags & Source::FLAG_DYNAMIC_DURATION)
1593 && (flags & Source::FLAG_DYNAMIC_DURATION)
1594 && (mAudioDecoder != NULL || mVideoDecoder != NULL)) {
1595 schedulePollDuration();
1596 }
1597
1598 mSourceFlags = flags;
1599 break;
1600 }
1601
1602 case Source::kWhatVideoSizeChanged:
1603 {
1604 int32_t width, height;
1605 CHECK(msg->findInt32("width", &width));
1606 CHECK(msg->findInt32("height", &height));
1607
1608 notifyListener(MEDIA_SET_VIDEO_SIZE, width, height);
1609 break;
1610 }
1611
Roger Jönssonb50e83e2013-01-21 16:26:41 +01001612 case Source::kWhatBufferingStart:
1613 {
1614 notifyListener(MEDIA_INFO, MEDIA_INFO_BUFFERING_START, 0);
1615 break;
1616 }
1617
1618 case Source::kWhatBufferingEnd:
1619 {
1620 notifyListener(MEDIA_INFO, MEDIA_INFO_BUFFERING_END, 0);
1621 break;
1622 }
1623
Chong Zhangdcb89b32013-08-06 09:44:47 -07001624 case Source::kWhatSubtitleData:
1625 {
1626 sp<ABuffer> buffer;
1627 CHECK(msg->findBuffer("buffer", &buffer));
1628
Chong Zhang404fced2014-06-11 14:45:31 -07001629 sendSubtitleData(buffer, 0 /* baseIndex */);
Chong Zhangdcb89b32013-08-06 09:44:47 -07001630 break;
1631 }
1632
Andreas Huber14f76722013-01-15 09:04:18 -08001633 case Source::kWhatQueueDecoderShutdown:
1634 {
1635 int32_t audio, video;
1636 CHECK(msg->findInt32("audio", &audio));
1637 CHECK(msg->findInt32("video", &video));
1638
1639 sp<AMessage> reply;
1640 CHECK(msg->findMessage("reply", &reply));
1641
1642 queueDecoderShutdown(audio, video, reply);
1643 break;
1644 }
1645
Andreas Huber9575c962013-02-05 13:59:56 -08001646 default:
1647 TRESPASS();
1648 }
1649}
1650
Chong Zhanga7fa1d92014-06-11 14:49:23 -07001651void NuPlayer::onClosedCaptionNotify(const sp<AMessage> &msg) {
1652 int32_t what;
1653 CHECK(msg->findInt32("what", &what));
1654
1655 switch (what) {
1656 case NuPlayer::CCDecoder::kWhatClosedCaptionData:
1657 {
1658 sp<ABuffer> buffer;
1659 CHECK(msg->findBuffer("buffer", &buffer));
1660
1661 size_t inbandTracks = 0;
1662 if (mSource != NULL) {
1663 inbandTracks = mSource->getTrackCount();
1664 }
1665
1666 sendSubtitleData(buffer, inbandTracks);
1667 break;
1668 }
1669
1670 case NuPlayer::CCDecoder::kWhatTrackAdded:
1671 {
1672 notifyListener(MEDIA_INFO, MEDIA_INFO_METADATA_UPDATE, 0);
1673
1674 break;
1675 }
1676
1677 default:
1678 TRESPASS();
1679 }
1680
1681
1682}
1683
Chong Zhang404fced2014-06-11 14:45:31 -07001684void NuPlayer::sendSubtitleData(const sp<ABuffer> &buffer, int32_t baseIndex) {
1685 int32_t trackIndex;
1686 int64_t timeUs, durationUs;
1687 CHECK(buffer->meta()->findInt32("trackIndex", &trackIndex));
1688 CHECK(buffer->meta()->findInt64("timeUs", &timeUs));
1689 CHECK(buffer->meta()->findInt64("durationUs", &durationUs));
1690
1691 Parcel in;
1692 in.writeInt32(trackIndex + baseIndex);
1693 in.writeInt64(timeUs);
1694 in.writeInt64(durationUs);
1695 in.writeInt32(buffer->size());
1696 in.writeInt32(buffer->size());
1697 in.write(buffer->data(), buffer->size());
1698
1699 notifyListener(MEDIA_SUBTITLE_DATA, 0, 0, &in);
1700}
Andreas Huberb5f25f02013-02-05 10:14:26 -08001701////////////////////////////////////////////////////////////////////////////////
1702
Andreas Huber9575c962013-02-05 13:59:56 -08001703void NuPlayer::Source::notifyFlagsChanged(uint32_t flags) {
1704 sp<AMessage> notify = dupNotify();
1705 notify->setInt32("what", kWhatFlagsChanged);
1706 notify->setInt32("flags", flags);
1707 notify->post();
1708}
1709
1710void NuPlayer::Source::notifyVideoSizeChanged(int32_t width, int32_t height) {
1711 sp<AMessage> notify = dupNotify();
1712 notify->setInt32("what", kWhatVideoSizeChanged);
1713 notify->setInt32("width", width);
1714 notify->setInt32("height", height);
1715 notify->post();
1716}
1717
Andreas Huberec0c5972013-02-05 14:47:13 -08001718void NuPlayer::Source::notifyPrepared(status_t err) {
Andreas Huber9575c962013-02-05 13:59:56 -08001719 sp<AMessage> notify = dupNotify();
1720 notify->setInt32("what", kWhatPrepared);
Andreas Huberec0c5972013-02-05 14:47:13 -08001721 notify->setInt32("err", err);
Andreas Huber9575c962013-02-05 13:59:56 -08001722 notify->post();
1723}
1724
Andreas Huber84333e02014-02-07 15:36:10 -08001725void NuPlayer::Source::onMessageReceived(const sp<AMessage> & /* msg */) {
Andreas Huberb5f25f02013-02-05 10:14:26 -08001726 TRESPASS();
1727}
1728
Andreas Huber14f76722013-01-15 09:04:18 -08001729void NuPlayer::queueDecoderShutdown(
1730 bool audio, bool video, const sp<AMessage> &reply) {
1731 ALOGI("queueDecoderShutdown audio=%d, video=%d", audio, video);
1732
1733 mDeferredActions.push_back(
1734 new ShutdownDecoderAction(audio, video));
1735
1736 mDeferredActions.push_back(
1737 new SimpleAction(&NuPlayer::performScanSources));
1738
1739 mDeferredActions.push_back(new PostMessageAction(reply));
1740
1741 processDeferredActions();
1742}
1743
Andreas Huberf9334412010-12-15 15:17:42 -08001744} // namespace android