blob: 2bffed1b3d2e9c9de042639f6688ae4bec82da57 [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"
Robert Shihd3b0bbb2014-07-23 15:00:25 -070032#include "TextDescriptions.h"
Andreas Huber5bc087c2010-12-23 10:27:40 -080033
34#include "ATSParser.h"
Andreas Huberf9334412010-12-15 15:17:42 -080035
Andreas Huber3831a062010-12-21 10:22:33 -080036#include <media/stagefright/foundation/hexdump.h>
Andreas Huberf9334412010-12-15 15:17:42 -080037#include <media/stagefright/foundation/ABuffer.h>
38#include <media/stagefright/foundation/ADebug.h>
39#include <media/stagefright/foundation/AMessage.h>
Lajos Molnar09524832014-07-17 14:29:51 -070040#include <media/stagefright/MediaBuffer.h>
Andreas Huber3fe62152011-09-16 15:09:22 -070041#include <media/stagefright/MediaDefs.h>
Andreas Huberf9334412010-12-15 15:17:42 -080042#include <media/stagefright/MediaErrors.h>
43#include <media/stagefright/MetaData.h>
Andy McFadden8ba01022012-12-18 09:46:54 -080044#include <gui/IGraphicBufferProducer.h>
Andreas Huberf9334412010-12-15 15:17:42 -080045
Andreas Huber3fe62152011-09-16 15:09:22 -070046#include "avc_utils.h"
47
Andreas Huber84066782011-08-16 09:34:26 -070048#include "ESDS.h"
49#include <media/stagefright/Utils.h>
50
Andreas Huberf9334412010-12-15 15:17:42 -080051namespace android {
52
Andreas Hubera1f8ab02012-11-30 10:53:22 -080053struct NuPlayer::Action : public RefBase {
54 Action() {}
55
56 virtual void execute(NuPlayer *player) = 0;
57
58private:
59 DISALLOW_EVIL_CONSTRUCTORS(Action);
60};
61
62struct NuPlayer::SeekAction : public Action {
63 SeekAction(int64_t seekTimeUs)
64 : mSeekTimeUs(seekTimeUs) {
65 }
66
67 virtual void execute(NuPlayer *player) {
68 player->performSeek(mSeekTimeUs);
69 }
70
71private:
72 int64_t mSeekTimeUs;
73
74 DISALLOW_EVIL_CONSTRUCTORS(SeekAction);
75};
76
Andreas Huber57a339c2012-12-03 11:18:00 -080077struct NuPlayer::SetSurfaceAction : public Action {
78 SetSurfaceAction(const sp<NativeWindowWrapper> &wrapper)
79 : mWrapper(wrapper) {
80 }
81
82 virtual void execute(NuPlayer *player) {
83 player->performSetSurface(mWrapper);
84 }
85
86private:
87 sp<NativeWindowWrapper> mWrapper;
88
89 DISALLOW_EVIL_CONSTRUCTORS(SetSurfaceAction);
90};
91
Andreas Huber14f76722013-01-15 09:04:18 -080092struct NuPlayer::ShutdownDecoderAction : public Action {
93 ShutdownDecoderAction(bool audio, bool video)
94 : mAudio(audio),
95 mVideo(video) {
96 }
97
98 virtual void execute(NuPlayer *player) {
99 player->performDecoderShutdown(mAudio, mVideo);
100 }
101
102private:
103 bool mAudio;
104 bool mVideo;
105
106 DISALLOW_EVIL_CONSTRUCTORS(ShutdownDecoderAction);
107};
108
109struct NuPlayer::PostMessageAction : public Action {
110 PostMessageAction(const sp<AMessage> &msg)
111 : mMessage(msg) {
112 }
113
114 virtual void execute(NuPlayer *) {
115 mMessage->post();
116 }
117
118private:
119 sp<AMessage> mMessage;
120
121 DISALLOW_EVIL_CONSTRUCTORS(PostMessageAction);
122};
123
Andreas Hubera1f8ab02012-11-30 10:53:22 -0800124// Use this if there's no state necessary to save in order to execute
125// the action.
126struct NuPlayer::SimpleAction : public Action {
127 typedef void (NuPlayer::*ActionFunc)();
128
129 SimpleAction(ActionFunc func)
130 : mFunc(func) {
131 }
132
133 virtual void execute(NuPlayer *player) {
134 (player->*mFunc)();
135 }
136
137private:
138 ActionFunc mFunc;
139
140 DISALLOW_EVIL_CONSTRUCTORS(SimpleAction);
141};
142
Andreas Huberf9334412010-12-15 15:17:42 -0800143////////////////////////////////////////////////////////////////////////////////
144
145NuPlayer::NuPlayer()
Andreas Huber9b80c2b2011-06-30 15:47:02 -0700146 : mUIDValid(false),
Andreas Huber9575c962013-02-05 13:59:56 -0800147 mSourceFlags(0),
Andreas Huber3fe62152011-09-16 15:09:22 -0700148 mVideoIsAVC(false),
Wei Jiabc2fb722014-07-08 16:37:57 -0700149 mOffloadAudio(false),
Andy Hung282a7e32014-08-14 15:56:34 -0700150 mCurrentOffloadInfo(AUDIO_INFO_INITIALIZER),
Wei Jia88703c32014-08-06 11:24:07 -0700151 mAudioDecoderGeneration(0),
152 mVideoDecoderGeneration(0),
Andreas Huber9b80c2b2011-06-30 15:47:02 -0700153 mAudioEOS(false),
Andreas Huberf9334412010-12-15 15:17:42 -0800154 mVideoEOS(false),
Andreas Huber5bc087c2010-12-23 10:27:40 -0800155 mScanSourcesPending(false),
Andreas Huber1aef2112011-01-04 14:01:29 -0800156 mScanSourcesGeneration(0),
Andreas Huberb7c8e912012-11-27 15:02:53 -0800157 mPollDurationGeneration(0),
Robert Shihd3b0bbb2014-07-23 15:00:25 -0700158 mTimedTextGeneration(0),
Andreas Huber6e3d3112011-11-28 12:36:11 -0800159 mTimeDiscontinuityPending(false),
Andreas Huberf9334412010-12-15 15:17:42 -0800160 mFlushingAudio(NONE),
Andreas Huber1aef2112011-01-04 14:01:29 -0800161 mFlushingVideo(NONE),
Andreas Huber3fe62152011-09-16 15:09:22 -0700162 mSkipRenderingAudioUntilMediaTimeUs(-1ll),
163 mSkipRenderingVideoUntilMediaTimeUs(-1ll),
164 mVideoLateByUs(0ll),
165 mNumFramesTotal(0ll),
James Dong0d268a32012-08-31 12:18:27 -0700166 mNumFramesDropped(0ll),
Andreas Huber57a339c2012-12-03 11:18:00 -0800167 mVideoScalingMode(NATIVE_WINDOW_SCALING_MODE_SCALE_TO_WINDOW),
168 mStarted(false) {
Andreas Huberf9334412010-12-15 15:17:42 -0800169}
170
171NuPlayer::~NuPlayer() {
172}
173
Andreas Huber9b80c2b2011-06-30 15:47:02 -0700174void NuPlayer::setUID(uid_t uid) {
175 mUIDValid = true;
176 mUID = uid;
177}
178
Andreas Huber43c3e6c2011-01-05 12:17:08 -0800179void NuPlayer::setDriver(const wp<NuPlayerDriver> &driver) {
180 mDriver = driver;
Andreas Huberf9334412010-12-15 15:17:42 -0800181}
182
Andreas Huber9575c962013-02-05 13:59:56 -0800183void NuPlayer::setDataSourceAsync(const sp<IStreamSource> &source) {
Andreas Huberf9334412010-12-15 15:17:42 -0800184 sp<AMessage> msg = new AMessage(kWhatSetDataSource, id());
185
Andreas Huberb5f25f02013-02-05 10:14:26 -0800186 sp<AMessage> notify = new AMessage(kWhatSourceNotify, id());
187
Andreas Huber240abcc2014-02-13 13:32:37 -0800188 msg->setObject("source", new StreamingSource(notify, source));
Andreas Huber5bc087c2010-12-23 10:27:40 -0800189 msg->post();
190}
Andreas Huberf9334412010-12-15 15:17:42 -0800191
Andreas Huberafed0e12011-09-20 15:39:58 -0700192static bool IsHTTPLiveURL(const char *url) {
193 if (!strncasecmp("http://", url, 7)
Andreas Huber99759402013-04-01 14:28:31 -0700194 || !strncasecmp("https://", url, 8)
195 || !strncasecmp("file://", url, 7)) {
Andreas Huberafed0e12011-09-20 15:39:58 -0700196 size_t len = strlen(url);
197 if (len >= 5 && !strcasecmp(".m3u8", &url[len - 5])) {
198 return true;
199 }
200
201 if (strstr(url,"m3u8")) {
202 return true;
203 }
204 }
205
206 return false;
207}
208
Andreas Huber9575c962013-02-05 13:59:56 -0800209void NuPlayer::setDataSourceAsync(
Andreas Huber1b86fe02014-01-29 11:13:26 -0800210 const sp<IMediaHTTPService> &httpService,
211 const char *url,
212 const KeyedVector<String8, String8> *headers) {
Chong Zhang3de157d2014-08-05 20:54:44 -0700213
Andreas Huber5bc087c2010-12-23 10:27:40 -0800214 sp<AMessage> msg = new AMessage(kWhatSetDataSource, id());
Oscar Rydhé7a33b772012-02-20 10:15:48 +0100215 size_t len = strlen(url);
Andreas Huber5bc087c2010-12-23 10:27:40 -0800216
Andreas Huberb5f25f02013-02-05 10:14:26 -0800217 sp<AMessage> notify = new AMessage(kWhatSourceNotify, id());
218
Andreas Huberafed0e12011-09-20 15:39:58 -0700219 sp<Source> source;
220 if (IsHTTPLiveURL(url)) {
Andreas Huber81e68442014-02-05 11:52:33 -0800221 source = new HTTPLiveSource(notify, httpService, url, headers);
Andreas Huberafed0e12011-09-20 15:39:58 -0700222 } else if (!strncasecmp(url, "rtsp://", 7)) {
Andreas Huber1b86fe02014-01-29 11:13:26 -0800223 source = new RTSPSource(
224 notify, httpService, url, headers, mUIDValid, mUID);
Oscar Rydhé7a33b772012-02-20 10:15:48 +0100225 } else if ((!strncasecmp(url, "http://", 7)
226 || !strncasecmp(url, "https://", 8))
227 && ((len >= 4 && !strcasecmp(".sdp", &url[len - 4]))
228 || strstr(url, ".sdp?"))) {
Andreas Huber1b86fe02014-01-29 11:13:26 -0800229 source = new RTSPSource(
230 notify, httpService, url, headers, mUIDValid, mUID, true);
Andreas Huber2bfdd422011-10-11 15:24:07 -0700231 } else {
Chong Zhang3de157d2014-08-05 20:54:44 -0700232 sp<GenericSource> genericSource =
233 new GenericSource(notify, mUIDValid, mUID);
234 // Don't set FLAG_SECURE on mSourceFlags here for widevine.
235 // The correct flags will be updated in Source::kWhatFlagsChanged
236 // handler when GenericSource is prepared.
Andreas Huber2bfdd422011-10-11 15:24:07 -0700237
Chong Zhanga19f33e2014-08-07 15:35:07 -0700238 status_t err = genericSource->setDataSource(httpService, url, headers);
Chong Zhang3de157d2014-08-05 20:54:44 -0700239
240 if (err == OK) {
241 source = genericSource;
242 } else {
Chong Zhanga19f33e2014-08-07 15:35:07 -0700243 ALOGE("Failed to set data source!");
Chong Zhang3de157d2014-08-05 20:54:44 -0700244 }
245 }
Andreas Huberafed0e12011-09-20 15:39:58 -0700246 msg->setObject("source", source);
247 msg->post();
248}
249
Andreas Huber9575c962013-02-05 13:59:56 -0800250void NuPlayer::setDataSourceAsync(int fd, int64_t offset, int64_t length) {
Andreas Huberafed0e12011-09-20 15:39:58 -0700251 sp<AMessage> msg = new AMessage(kWhatSetDataSource, id());
252
Andreas Huberb5f25f02013-02-05 10:14:26 -0800253 sp<AMessage> notify = new AMessage(kWhatSourceNotify, id());
254
Chong Zhang3de157d2014-08-05 20:54:44 -0700255 sp<GenericSource> source =
256 new GenericSource(notify, mUIDValid, mUID);
257
Chong Zhanga19f33e2014-08-07 15:35:07 -0700258 status_t err = source->setDataSource(fd, offset, length);
Chong Zhang3de157d2014-08-05 20:54:44 -0700259
260 if (err != OK) {
Chong Zhanga19f33e2014-08-07 15:35:07 -0700261 ALOGE("Failed to set data source!");
Chong Zhang3de157d2014-08-05 20:54:44 -0700262 source = NULL;
263 }
264
Andreas Huberafed0e12011-09-20 15:39:58 -0700265 msg->setObject("source", source);
Andreas Huberf9334412010-12-15 15:17:42 -0800266 msg->post();
267}
268
Andreas Huber9575c962013-02-05 13:59:56 -0800269void NuPlayer::prepareAsync() {
270 (new AMessage(kWhatPrepare, id()))->post();
271}
272
Andreas Huber57a339c2012-12-03 11:18:00 -0800273void NuPlayer::setVideoSurfaceTextureAsync(
Andy McFadden8ba01022012-12-18 09:46:54 -0800274 const sp<IGraphicBufferProducer> &bufferProducer) {
Glenn Kasten11731182011-02-08 17:26:17 -0800275 sp<AMessage> msg = new AMessage(kWhatSetVideoNativeWindow, id());
Andreas Huber57a339c2012-12-03 11:18:00 -0800276
Andy McFadden8ba01022012-12-18 09:46:54 -0800277 if (bufferProducer == NULL) {
Andreas Huber57a339c2012-12-03 11:18:00 -0800278 msg->setObject("native-window", NULL);
279 } else {
280 msg->setObject(
281 "native-window",
282 new NativeWindowWrapper(
Wei Jia9c03a402014-08-26 15:24:43 -0700283 new Surface(bufferProducer, true /* controlledByApp */)));
Andreas Huber57a339c2012-12-03 11:18:00 -0800284 }
285
Andreas Huberf9334412010-12-15 15:17:42 -0800286 msg->post();
287}
288
289void NuPlayer::setAudioSink(const sp<MediaPlayerBase::AudioSink> &sink) {
290 sp<AMessage> msg = new AMessage(kWhatSetAudioSink, id());
291 msg->setObject("sink", sink);
292 msg->post();
293}
294
295void NuPlayer::start() {
296 (new AMessage(kWhatStart, id()))->post();
297}
298
Andreas Huber43c3e6c2011-01-05 12:17:08 -0800299void NuPlayer::pause() {
Andreas Huberb4082222011-01-20 15:23:04 -0800300 (new AMessage(kWhatPause, id()))->post();
Andreas Huber43c3e6c2011-01-05 12:17:08 -0800301}
302
303void NuPlayer::resume() {
Andreas Huberb4082222011-01-20 15:23:04 -0800304 (new AMessage(kWhatResume, id()))->post();
Andreas Huber43c3e6c2011-01-05 12:17:08 -0800305}
306
Andreas Huber1aef2112011-01-04 14:01:29 -0800307void NuPlayer::resetAsync() {
308 (new AMessage(kWhatReset, id()))->post();
309}
310
Andreas Huber43c3e6c2011-01-05 12:17:08 -0800311void NuPlayer::seekToAsync(int64_t seekTimeUs) {
312 sp<AMessage> msg = new AMessage(kWhatSeek, id());
313 msg->setInt64("seekTimeUs", seekTimeUs);
314 msg->post();
315}
316
Andreas Huber53df1a42010-12-22 10:03:04 -0800317// static
Andreas Huber1aef2112011-01-04 14:01:29 -0800318bool NuPlayer::IsFlushingState(FlushStatus state, bool *needShutdown) {
Andreas Huber53df1a42010-12-22 10:03:04 -0800319 switch (state) {
320 case FLUSHING_DECODER:
Andreas Huber1aef2112011-01-04 14:01:29 -0800321 if (needShutdown != NULL) {
322 *needShutdown = false;
Andreas Huber53df1a42010-12-22 10:03:04 -0800323 }
324 return true;
325
Andreas Huber1aef2112011-01-04 14:01:29 -0800326 case FLUSHING_DECODER_SHUTDOWN:
327 if (needShutdown != NULL) {
328 *needShutdown = true;
Andreas Huber53df1a42010-12-22 10:03:04 -0800329 }
330 return true;
331
332 default:
333 return false;
334 }
335}
336
Chong Zhang404fced2014-06-11 14:45:31 -0700337void NuPlayer::writeTrackInfo(
338 Parcel* reply, const sp<AMessage> format) const {
339 int32_t trackType;
340 CHECK(format->findInt32("type", &trackType));
341
342 AString lang;
343 CHECK(format->findString("language", &lang));
344
345 reply->writeInt32(2); // write something non-zero
346 reply->writeInt32(trackType);
347 reply->writeString16(String16(lang.c_str()));
348
349 if (trackType == MEDIA_TRACK_TYPE_SUBTITLE) {
350 AString mime;
351 CHECK(format->findString("mime", &mime));
352
353 int32_t isAuto, isDefault, isForced;
354 CHECK(format->findInt32("auto", &isAuto));
355 CHECK(format->findInt32("default", &isDefault));
356 CHECK(format->findInt32("forced", &isForced));
357
358 reply->writeString16(String16(mime.c_str()));
359 reply->writeInt32(isAuto);
360 reply->writeInt32(isDefault);
361 reply->writeInt32(isForced);
362 }
363}
364
Andreas Huberf9334412010-12-15 15:17:42 -0800365void NuPlayer::onMessageReceived(const sp<AMessage> &msg) {
366 switch (msg->what()) {
367 case kWhatSetDataSource:
368 {
Steve Block3856b092011-10-20 11:56:00 +0100369 ALOGV("kWhatSetDataSource");
Andreas Huberf9334412010-12-15 15:17:42 -0800370
371 CHECK(mSource == NULL);
372
Chong Zhang3de157d2014-08-05 20:54:44 -0700373 status_t err = OK;
Andreas Huber5bc087c2010-12-23 10:27:40 -0800374 sp<RefBase> obj;
375 CHECK(msg->findObject("source", &obj));
Chong Zhang3de157d2014-08-05 20:54:44 -0700376 if (obj != NULL) {
377 mSource = static_cast<Source *>(obj.get());
Chong Zhang3de157d2014-08-05 20:54:44 -0700378 } else {
379 err = UNKNOWN_ERROR;
380 }
Andreas Huber9575c962013-02-05 13:59:56 -0800381
382 CHECK(mDriver != NULL);
383 sp<NuPlayerDriver> driver = mDriver.promote();
384 if (driver != NULL) {
Chong Zhang3de157d2014-08-05 20:54:44 -0700385 driver->notifySetDataSourceCompleted(err);
Andreas Huber9575c962013-02-05 13:59:56 -0800386 }
387 break;
388 }
389
390 case kWhatPrepare:
391 {
392 mSource->prepareAsync();
Andreas Huberf9334412010-12-15 15:17:42 -0800393 break;
394 }
395
Chong Zhangdcb89b32013-08-06 09:44:47 -0700396 case kWhatGetTrackInfo:
397 {
398 uint32_t replyID;
399 CHECK(msg->senderAwaitsResponse(&replyID));
400
Chong Zhang404fced2014-06-11 14:45:31 -0700401 Parcel* reply;
402 CHECK(msg->findPointer("reply", (void**)&reply));
403
404 size_t inbandTracks = 0;
Chong Zhangdcb89b32013-08-06 09:44:47 -0700405 if (mSource != NULL) {
Chong Zhang404fced2014-06-11 14:45:31 -0700406 inbandTracks = mSource->getTrackCount();
407 }
408
Chong Zhanga7fa1d92014-06-11 14:49:23 -0700409 size_t ccTracks = 0;
410 if (mCCDecoder != NULL) {
411 ccTracks = mCCDecoder->getTrackCount();
412 }
413
Chong Zhang404fced2014-06-11 14:45:31 -0700414 // total track count
Chong Zhanga7fa1d92014-06-11 14:49:23 -0700415 reply->writeInt32(inbandTracks + ccTracks);
Chong Zhang404fced2014-06-11 14:45:31 -0700416
417 // write inband tracks
418 for (size_t i = 0; i < inbandTracks; ++i) {
419 writeTrackInfo(reply, mSource->getTrackInfo(i));
Chong Zhangdcb89b32013-08-06 09:44:47 -0700420 }
421
Chong Zhanga7fa1d92014-06-11 14:49:23 -0700422 // write CC track
423 for (size_t i = 0; i < ccTracks; ++i) {
424 writeTrackInfo(reply, mCCDecoder->getTrackInfo(i));
425 }
426
Chong Zhangdcb89b32013-08-06 09:44:47 -0700427 sp<AMessage> response = new AMessage;
Chong Zhangdcb89b32013-08-06 09:44:47 -0700428 response->postReply(replyID);
429 break;
430 }
431
Robert Shih7c4f0d72014-07-09 18:53:31 -0700432 case kWhatGetSelectedTrack:
433 {
434 status_t err = INVALID_OPERATION;
435 if (mSource != NULL) {
436 err = OK;
437
438 int32_t type32;
439 CHECK(msg->findInt32("type", (int32_t*)&type32));
440 media_track_type type = (media_track_type)type32;
441 ssize_t selectedTrack = mSource->getSelectedTrack(type);
442
443 Parcel* reply;
444 CHECK(msg->findPointer("reply", (void**)&reply));
445 reply->writeInt32(selectedTrack);
446 }
447
448 sp<AMessage> response = new AMessage;
449 response->setInt32("err", err);
450
451 uint32_t replyID;
452 CHECK(msg->senderAwaitsResponse(&replyID));
453 response->postReply(replyID);
454 break;
455 }
456
Chong Zhangdcb89b32013-08-06 09:44:47 -0700457 case kWhatSelectTrack:
458 {
459 uint32_t replyID;
460 CHECK(msg->senderAwaitsResponse(&replyID));
461
Chong Zhang404fced2014-06-11 14:45:31 -0700462 size_t trackIndex;
463 int32_t select;
464 CHECK(msg->findSize("trackIndex", &trackIndex));
465 CHECK(msg->findInt32("select", &select));
466
Chong Zhangdcb89b32013-08-06 09:44:47 -0700467 status_t err = INVALID_OPERATION;
Chong Zhang404fced2014-06-11 14:45:31 -0700468
469 size_t inbandTracks = 0;
Chong Zhangdcb89b32013-08-06 09:44:47 -0700470 if (mSource != NULL) {
Chong Zhang404fced2014-06-11 14:45:31 -0700471 inbandTracks = mSource->getTrackCount();
472 }
Chong Zhanga7fa1d92014-06-11 14:49:23 -0700473 size_t ccTracks = 0;
474 if (mCCDecoder != NULL) {
475 ccTracks = mCCDecoder->getTrackCount();
476 }
Chong Zhang404fced2014-06-11 14:45:31 -0700477
478 if (trackIndex < inbandTracks) {
Chong Zhangdcb89b32013-08-06 09:44:47 -0700479 err = mSource->selectTrack(trackIndex, select);
Robert Shihd3b0bbb2014-07-23 15:00:25 -0700480
481 if (!select && err == OK) {
482 int32_t type;
483 sp<AMessage> info = mSource->getTrackInfo(trackIndex);
484 if (info != NULL
485 && info->findInt32("type", &type)
486 && type == MEDIA_TRACK_TYPE_TIMEDTEXT) {
487 ++mTimedTextGeneration;
488 }
489 }
Chong Zhanga7fa1d92014-06-11 14:49:23 -0700490 } else {
491 trackIndex -= inbandTracks;
492
493 if (trackIndex < ccTracks) {
494 err = mCCDecoder->selectTrack(trackIndex, select);
495 }
Chong Zhangdcb89b32013-08-06 09:44:47 -0700496 }
497
498 sp<AMessage> response = new AMessage;
499 response->setInt32("err", err);
500
501 response->postReply(replyID);
502 break;
503 }
504
Andreas Huberb7c8e912012-11-27 15:02:53 -0800505 case kWhatPollDuration:
506 {
507 int32_t generation;
508 CHECK(msg->findInt32("generation", &generation));
509
510 if (generation != mPollDurationGeneration) {
511 // stale
512 break;
513 }
514
515 int64_t durationUs;
516 if (mDriver != NULL && mSource->getDuration(&durationUs) == OK) {
517 sp<NuPlayerDriver> driver = mDriver.promote();
518 if (driver != NULL) {
519 driver->notifyDuration(durationUs);
520 }
521 }
522
523 msg->post(1000000ll); // poll again in a second.
524 break;
525 }
526
Glenn Kasten11731182011-02-08 17:26:17 -0800527 case kWhatSetVideoNativeWindow:
Andreas Huberf9334412010-12-15 15:17:42 -0800528 {
Steve Block3856b092011-10-20 11:56:00 +0100529 ALOGV("kWhatSetVideoNativeWindow");
Andreas Huberf9334412010-12-15 15:17:42 -0800530
Andreas Huber57a339c2012-12-03 11:18:00 -0800531 mDeferredActions.push_back(
Andreas Huber14f76722013-01-15 09:04:18 -0800532 new ShutdownDecoderAction(
533 false /* audio */, true /* video */));
Andreas Huber57a339c2012-12-03 11:18:00 -0800534
Andreas Huberf9334412010-12-15 15:17:42 -0800535 sp<RefBase> obj;
Glenn Kasten11731182011-02-08 17:26:17 -0800536 CHECK(msg->findObject("native-window", &obj));
Andreas Huberf9334412010-12-15 15:17:42 -0800537
Andreas Huber57a339c2012-12-03 11:18:00 -0800538 mDeferredActions.push_back(
539 new SetSurfaceAction(
540 static_cast<NativeWindowWrapper *>(obj.get())));
James Dong0d268a32012-08-31 12:18:27 -0700541
Andreas Huber57a339c2012-12-03 11:18:00 -0800542 if (obj != NULL) {
543 // If there is a new surface texture, instantiate decoders
544 // again if possible.
545 mDeferredActions.push_back(
546 new SimpleAction(&NuPlayer::performScanSources));
547 }
548
549 processDeferredActions();
Andreas Huberf9334412010-12-15 15:17:42 -0800550 break;
551 }
552
553 case kWhatSetAudioSink:
554 {
Steve Block3856b092011-10-20 11:56:00 +0100555 ALOGV("kWhatSetAudioSink");
Andreas Huberf9334412010-12-15 15:17:42 -0800556
557 sp<RefBase> obj;
558 CHECK(msg->findObject("sink", &obj));
559
560 mAudioSink = static_cast<MediaPlayerBase::AudioSink *>(obj.get());
561 break;
562 }
563
564 case kWhatStart:
565 {
Steve Block3856b092011-10-20 11:56:00 +0100566 ALOGV("kWhatStart");
Andreas Huber43c3e6c2011-01-05 12:17:08 -0800567
Andreas Huber3fe62152011-09-16 15:09:22 -0700568 mVideoIsAVC = false;
Wei Jiabc2fb722014-07-08 16:37:57 -0700569 mOffloadAudio = false;
Andreas Huber1aef2112011-01-04 14:01:29 -0800570 mAudioEOS = false;
571 mVideoEOS = false;
Andreas Huber32f3cef2011-03-02 15:34:46 -0800572 mSkipRenderingAudioUntilMediaTimeUs = -1;
573 mSkipRenderingVideoUntilMediaTimeUs = -1;
Andreas Huber3fe62152011-09-16 15:09:22 -0700574 mVideoLateByUs = 0;
575 mNumFramesTotal = 0;
576 mNumFramesDropped = 0;
Andreas Huber57a339c2012-12-03 11:18:00 -0800577 mStarted = true;
Andreas Huber1aef2112011-01-04 14:01:29 -0800578
Lajos Molnar09524832014-07-17 14:29:51 -0700579 /* instantiate decoders now for secure playback */
580 if (mSourceFlags & Source::FLAG_SECURE) {
581 if (mNativeWindow != NULL) {
582 instantiateDecoder(false, &mVideoDecoder);
583 }
584
585 if (mAudioSink != NULL) {
586 instantiateDecoder(true, &mAudioDecoder);
587 }
588 }
589
Andreas Huber5bc087c2010-12-23 10:27:40 -0800590 mSource->start();
Andreas Huberf9334412010-12-15 15:17:42 -0800591
Andreas Huberd5e56232013-03-12 11:01:43 -0700592 uint32_t flags = 0;
593
594 if (mSource->isRealTime()) {
595 flags |= Renderer::FLAG_REAL_TIME;
596 }
597
Wei Jiabc2fb722014-07-08 16:37:57 -0700598 sp<MetaData> audioMeta = mSource->getFormatMeta(true /* audio */);
599 audio_stream_type_t streamType = AUDIO_STREAM_MUSIC;
600 if (mAudioSink != NULL) {
601 streamType = mAudioSink->getAudioStreamType();
602 }
603
604 sp<AMessage> videoFormat = mSource->getFormat(false /* audio */);
605
606 mOffloadAudio =
607 canOffloadStream(audioMeta, (videoFormat != NULL),
608 true /* is_streaming */, streamType);
609 if (mOffloadAudio) {
610 flags |= Renderer::FLAG_OFFLOAD_AUDIO;
611 }
612
Andreas Huberf9334412010-12-15 15:17:42 -0800613 mRenderer = new Renderer(
614 mAudioSink,
Andreas Huberd5e56232013-03-12 11:01:43 -0700615 new AMessage(kWhatRendererNotify, id()),
616 flags);
Andreas Huberf9334412010-12-15 15:17:42 -0800617
Lajos Molnar09524832014-07-17 14:29:51 -0700618 mRendererLooper = new ALooper;
619 mRendererLooper->setName("NuPlayerRenderer");
620 mRendererLooper->start(false, false, ANDROID_PRIORITY_AUDIO);
621 mRendererLooper->registerHandler(mRenderer);
Andreas Huberf9334412010-12-15 15:17:42 -0800622
Andreas Huber1aef2112011-01-04 14:01:29 -0800623 postScanSources();
Andreas Huberf9334412010-12-15 15:17:42 -0800624 break;
625 }
626
627 case kWhatScanSources:
628 {
Andreas Huber1aef2112011-01-04 14:01:29 -0800629 int32_t generation;
630 CHECK(msg->findInt32("generation", &generation));
631 if (generation != mScanSourcesGeneration) {
632 // Drop obsolete msg.
633 break;
634 }
635
Andreas Huber5bc087c2010-12-23 10:27:40 -0800636 mScanSourcesPending = false;
637
Steve Block3856b092011-10-20 11:56:00 +0100638 ALOGV("scanning sources haveAudio=%d, haveVideo=%d",
Andreas Huber43c3e6c2011-01-05 12:17:08 -0800639 mAudioDecoder != NULL, mVideoDecoder != NULL);
640
Andreas Huberb7c8e912012-11-27 15:02:53 -0800641 bool mHadAnySourcesBefore =
642 (mAudioDecoder != NULL) || (mVideoDecoder != NULL);
643
Andy Hung282a7e32014-08-14 15:56:34 -0700644 // initialize video before audio because successful initialization of
645 // video may change deep buffer mode of audio.
Haynes Mathew George5d246ef2012-07-09 10:36:57 -0700646 if (mNativeWindow != NULL) {
647 instantiateDecoder(false, &mVideoDecoder);
648 }
Andreas Huberf9334412010-12-15 15:17:42 -0800649
650 if (mAudioSink != NULL) {
Andy Hung282a7e32014-08-14 15:56:34 -0700651 if (mOffloadAudio) {
652 // open audio sink early under offload mode.
653 sp<AMessage> format = mSource->getFormat(true /*audio*/);
654 openAudioSink(format, true /*offloadOnly*/);
655 }
Andreas Huber5bc087c2010-12-23 10:27:40 -0800656 instantiateDecoder(true, &mAudioDecoder);
Andreas Huberf9334412010-12-15 15:17:42 -0800657 }
658
Andreas Huberb7c8e912012-11-27 15:02:53 -0800659 if (!mHadAnySourcesBefore
660 && (mAudioDecoder != NULL || mVideoDecoder != NULL)) {
661 // This is the first time we've found anything playable.
662
Andreas Huber9575c962013-02-05 13:59:56 -0800663 if (mSourceFlags & Source::FLAG_DYNAMIC_DURATION) {
Andreas Huberb7c8e912012-11-27 15:02:53 -0800664 schedulePollDuration();
665 }
666 }
667
Andreas Hubereac68ba2011-09-27 12:12:25 -0700668 status_t err;
669 if ((err = mSource->feedMoreTSData()) != OK) {
Andreas Huber1aef2112011-01-04 14:01:29 -0800670 if (mAudioDecoder == NULL && mVideoDecoder == NULL) {
671 // We're not currently decoding anything (no audio or
672 // video tracks found) and we just ran out of input data.
Andreas Hubereac68ba2011-09-27 12:12:25 -0700673
674 if (err == ERROR_END_OF_STREAM) {
675 notifyListener(MEDIA_PLAYBACK_COMPLETE, 0, 0);
676 } else {
677 notifyListener(MEDIA_ERROR, MEDIA_ERROR_UNKNOWN, err);
678 }
Andreas Huber1aef2112011-01-04 14:01:29 -0800679 }
Andreas Huberf9334412010-12-15 15:17:42 -0800680 break;
681 }
682
Andreas Huberfbe9d812012-08-31 14:05:27 -0700683 if ((mAudioDecoder == NULL && mAudioSink != NULL)
684 || (mVideoDecoder == NULL && mNativeWindow != NULL)) {
Andreas Huberf9334412010-12-15 15:17:42 -0800685 msg->post(100000ll);
Andreas Huber5bc087c2010-12-23 10:27:40 -0800686 mScanSourcesPending = true;
Andreas Huberf9334412010-12-15 15:17:42 -0800687 }
688 break;
689 }
690
691 case kWhatVideoNotify:
692 case kWhatAudioNotify:
693 {
694 bool audio = msg->what() == kWhatAudioNotify;
695
Wei Jia88703c32014-08-06 11:24:07 -0700696 int32_t currentDecoderGeneration =
697 (audio? mAudioDecoderGeneration : mVideoDecoderGeneration);
698 int32_t requesterGeneration = currentDecoderGeneration - 1;
699 CHECK(msg->findInt32("generation", &requesterGeneration));
700
701 if (requesterGeneration != currentDecoderGeneration) {
702 ALOGV("got message from old %s decoder, generation(%d:%d)",
703 audio ? "audio" : "video", requesterGeneration,
704 currentDecoderGeneration);
705 sp<AMessage> reply;
706 if (!(msg->findMessage("reply", &reply))) {
707 return;
708 }
709
710 reply->setInt32("err", INFO_DISCONTINUITY);
711 reply->post();
712 return;
713 }
714
Andreas Huberf9334412010-12-15 15:17:42 -0800715 int32_t what;
Lajos Molnar1cd13982014-01-17 15:12:51 -0800716 CHECK(msg->findInt32("what", &what));
Andreas Huberf9334412010-12-15 15:17:42 -0800717
Lajos Molnar1cd13982014-01-17 15:12:51 -0800718 if (what == Decoder::kWhatFillThisBuffer) {
Andreas Huberf9334412010-12-15 15:17:42 -0800719 status_t err = feedDecoderInputData(
Lajos Molnar1cd13982014-01-17 15:12:51 -0800720 audio, msg);
Andreas Huberf9334412010-12-15 15:17:42 -0800721
Andreas Huber5bc087c2010-12-23 10:27:40 -0800722 if (err == -EWOULDBLOCK) {
Andreas Hubereac68ba2011-09-27 12:12:25 -0700723 if (mSource->feedMoreTSData() == OK) {
Andreas Huber1183a4a2011-11-03 11:00:21 -0700724 msg->post(10000ll);
Andreas Huber5bc087c2010-12-23 10:27:40 -0800725 }
Andreas Huberf9334412010-12-15 15:17:42 -0800726 }
Lajos Molnar1cd13982014-01-17 15:12:51 -0800727 } else if (what == Decoder::kWhatEOS) {
Andreas Huberdc9bacd2011-09-26 10:53:29 -0700728 int32_t err;
Lajos Molnar1cd13982014-01-17 15:12:51 -0800729 CHECK(msg->findInt32("err", &err));
Andreas Huberdc9bacd2011-09-26 10:53:29 -0700730
731 if (err == ERROR_END_OF_STREAM) {
Steve Block3856b092011-10-20 11:56:00 +0100732 ALOGV("got %s decoder EOS", audio ? "audio" : "video");
Andreas Huberdc9bacd2011-09-26 10:53:29 -0700733 } else {
Steve Block3856b092011-10-20 11:56:00 +0100734 ALOGV("got %s decoder EOS w/ error %d",
Andreas Huberdc9bacd2011-09-26 10:53:29 -0700735 audio ? "audio" : "video",
736 err);
737 }
738
739 mRenderer->queueEOS(audio, err);
Lajos Molnar1cd13982014-01-17 15:12:51 -0800740 } else if (what == Decoder::kWhatFlushCompleted) {
Andreas Huber1aef2112011-01-04 14:01:29 -0800741 bool needShutdown;
Andreas Huber53df1a42010-12-22 10:03:04 -0800742
Andreas Huberf9334412010-12-15 15:17:42 -0800743 if (audio) {
Andreas Huber1aef2112011-01-04 14:01:29 -0800744 CHECK(IsFlushingState(mFlushingAudio, &needShutdown));
Andreas Huberf9334412010-12-15 15:17:42 -0800745 mFlushingAudio = FLUSHED;
746 } else {
Andreas Huber1aef2112011-01-04 14:01:29 -0800747 CHECK(IsFlushingState(mFlushingVideo, &needShutdown));
Andreas Huberf9334412010-12-15 15:17:42 -0800748 mFlushingVideo = FLUSHED;
Andreas Huber3fe62152011-09-16 15:09:22 -0700749
750 mVideoLateByUs = 0;
Andreas Huberf9334412010-12-15 15:17:42 -0800751 }
752
Steve Block3856b092011-10-20 11:56:00 +0100753 ALOGV("decoder %s flush completed", audio ? "audio" : "video");
Andreas Huberf9334412010-12-15 15:17:42 -0800754
Andreas Huber1aef2112011-01-04 14:01:29 -0800755 if (needShutdown) {
Steve Block3856b092011-10-20 11:56:00 +0100756 ALOGV("initiating %s decoder shutdown",
Andreas Huber53df1a42010-12-22 10:03:04 -0800757 audio ? "audio" : "video");
Andreas Huberf9334412010-12-15 15:17:42 -0800758
Lajos Molnar87603c02014-08-20 19:25:30 -0700759 getDecoder(audio)->initiateShutdown();
Andreas Huberf9334412010-12-15 15:17:42 -0800760
Andreas Huber53df1a42010-12-22 10:03:04 -0800761 if (audio) {
762 mFlushingAudio = SHUTTING_DOWN_DECODER;
763 } else {
764 mFlushingVideo = SHUTTING_DOWN_DECODER;
765 }
Andreas Huberf9334412010-12-15 15:17:42 -0800766 }
Andreas Huber3831a062010-12-21 10:22:33 -0800767
768 finishFlushIfPossible();
Lajos Molnar1cd13982014-01-17 15:12:51 -0800769 } else if (what == Decoder::kWhatOutputFormatChanged) {
770 sp<AMessage> format;
771 CHECK(msg->findMessage("format", &format));
772
Andreas Huber31e25082011-01-10 10:38:31 -0800773 if (audio) {
Andy Hung282a7e32014-08-14 15:56:34 -0700774 openAudioSink(format, false /*offloadOnly*/);
Andreas Huber31e25082011-01-10 10:38:31 -0800775 } else {
776 // video
Chong Zhangced1c2f2014-08-08 15:22:35 -0700777 sp<AMessage> inputFormat =
778 mSource->getFormat(false /* audio */);
Andreas Huber3831a062010-12-21 10:22:33 -0800779
Chong Zhangced1c2f2014-08-08 15:22:35 -0700780 updateVideoSize(inputFormat, format);
Andreas Huber31e25082011-01-10 10:38:31 -0800781 }
Lajos Molnar1cd13982014-01-17 15:12:51 -0800782 } else if (what == Decoder::kWhatShutdownCompleted) {
Steve Block3856b092011-10-20 11:56:00 +0100783 ALOGV("%s shutdown completed", audio ? "audio" : "video");
Andreas Huber3831a062010-12-21 10:22:33 -0800784 if (audio) {
785 mAudioDecoder.clear();
786
787 CHECK_EQ((int)mFlushingAudio, (int)SHUTTING_DOWN_DECODER);
788 mFlushingAudio = SHUT_DOWN;
789 } else {
790 mVideoDecoder.clear();
791
792 CHECK_EQ((int)mFlushingVideo, (int)SHUTTING_DOWN_DECODER);
793 mFlushingVideo = SHUT_DOWN;
794 }
795
796 finishFlushIfPossible();
Lajos Molnar1cd13982014-01-17 15:12:51 -0800797 } else if (what == Decoder::kWhatError) {
Steve Block29357bc2012-01-06 19:20:56 +0000798 ALOGE("Received error from %s decoder, aborting playback.",
Andreas Huberc92fd242011-08-16 13:48:44 -0700799 audio ? "audio" : "video");
800
Chong Zhangf4c0a942014-08-11 15:14:10 -0700801 status_t err;
802 if (!msg->findInt32("err", &err)) {
803 err = UNKNOWN_ERROR;
804 }
805 mRenderer->queueEOS(audio, err);
Marco Nelissen9e2b7912014-08-18 16:13:03 -0700806 if (audio && mFlushingAudio != NONE) {
807 mAudioDecoder.clear();
808 mFlushingAudio = SHUT_DOWN;
809 } else if (!audio && mFlushingVideo != NONE){
810 mVideoDecoder.clear();
811 mFlushingVideo = SHUT_DOWN;
812 }
813 finishFlushIfPossible();
Lajos Molnar1cd13982014-01-17 15:12:51 -0800814 } else if (what == Decoder::kWhatDrainThisBuffer) {
815 renderBuffer(audio, msg);
816 } else {
817 ALOGV("Unhandled decoder notification %d '%c%c%c%c'.",
Andreas Hubera1f8ab02012-11-30 10:53:22 -0800818 what,
819 what >> 24,
820 (what >> 16) & 0xff,
821 (what >> 8) & 0xff,
822 what & 0xff);
Andreas Huberf9334412010-12-15 15:17:42 -0800823 }
824
825 break;
826 }
827
828 case kWhatRendererNotify:
829 {
830 int32_t what;
831 CHECK(msg->findInt32("what", &what));
832
833 if (what == Renderer::kWhatEOS) {
834 int32_t audio;
835 CHECK(msg->findInt32("audio", &audio));
836
Andreas Huberc92fd242011-08-16 13:48:44 -0700837 int32_t finalResult;
838 CHECK(msg->findInt32("finalResult", &finalResult));
839
Andreas Huberf9334412010-12-15 15:17:42 -0800840 if (audio) {
841 mAudioEOS = true;
842 } else {
843 mVideoEOS = true;
844 }
845
Andreas Huberc92fd242011-08-16 13:48:44 -0700846 if (finalResult == ERROR_END_OF_STREAM) {
Steve Block3856b092011-10-20 11:56:00 +0100847 ALOGV("reached %s EOS", audio ? "audio" : "video");
Andreas Huberc92fd242011-08-16 13:48:44 -0700848 } else {
Steve Block29357bc2012-01-06 19:20:56 +0000849 ALOGE("%s track encountered an error (%d)",
Andreas Huberc92fd242011-08-16 13:48:44 -0700850 audio ? "audio" : "video", finalResult);
851
852 notifyListener(
853 MEDIA_ERROR, MEDIA_ERROR_UNKNOWN, finalResult);
854 }
Andreas Huberf9334412010-12-15 15:17:42 -0800855
856 if ((mAudioEOS || mAudioDecoder == NULL)
857 && (mVideoEOS || mVideoDecoder == NULL)) {
858 notifyListener(MEDIA_PLAYBACK_COMPLETE, 0, 0);
859 }
Andreas Huber43c3e6c2011-01-05 12:17:08 -0800860 } else if (what == Renderer::kWhatPosition) {
861 int64_t positionUs;
862 CHECK(msg->findInt64("positionUs", &positionUs));
863
Andreas Huber3fe62152011-09-16 15:09:22 -0700864 CHECK(msg->findInt64("videoLateByUs", &mVideoLateByUs));
865
Andreas Huber43c3e6c2011-01-05 12:17:08 -0800866 if (mDriver != NULL) {
867 sp<NuPlayerDriver> driver = mDriver.promote();
868 if (driver != NULL) {
869 driver->notifyPosition(positionUs);
Andreas Huber3fe62152011-09-16 15:09:22 -0700870
871 driver->notifyFrameStats(
872 mNumFramesTotal, mNumFramesDropped);
Andreas Huber43c3e6c2011-01-05 12:17:08 -0800873 }
874 }
Andreas Huber3fe62152011-09-16 15:09:22 -0700875 } else if (what == Renderer::kWhatFlushComplete) {
Andreas Huberf9334412010-12-15 15:17:42 -0800876 int32_t audio;
877 CHECK(msg->findInt32("audio", &audio));
878
Steve Block3856b092011-10-20 11:56:00 +0100879 ALOGV("renderer %s flush completed.", audio ? "audio" : "video");
James Dongf57b4ea2012-07-20 13:38:36 -0700880 } else if (what == Renderer::kWhatVideoRenderingStart) {
881 notifyListener(MEDIA_INFO, MEDIA_INFO_RENDERING_START, 0);
Lajos Molnarcbaffcf2013-08-14 18:30:38 -0700882 } else if (what == Renderer::kWhatMediaRenderingStart) {
883 ALOGV("media rendering started");
884 notifyListener(MEDIA_STARTED, 0, 0);
Wei Jia3a2956d2014-07-22 16:01:33 -0700885 } else if (what == Renderer::kWhatAudioOffloadTearDown) {
886 ALOGV("Tear down audio offload, fall back to s/w path");
887 int64_t positionUs;
888 CHECK(msg->findInt64("positionUs", &positionUs));
Andy Hung282a7e32014-08-14 15:56:34 -0700889 closeAudioSink();
Wei Jia3a2956d2014-07-22 16:01:33 -0700890 mAudioDecoder.clear();
891 mRenderer->flush(true /* audio */);
892 if (mVideoDecoder != NULL) {
893 mRenderer->flush(false /* audio */);
894 }
895 mRenderer->signalDisableOffloadAudio();
896 mOffloadAudio = false;
897
898 performSeek(positionUs);
899 instantiateDecoder(true /* audio */, &mAudioDecoder);
Andreas Huberf9334412010-12-15 15:17:42 -0800900 }
901 break;
902 }
903
904 case kWhatMoreDataQueued:
905 {
906 break;
907 }
908
Andreas Huber1aef2112011-01-04 14:01:29 -0800909 case kWhatReset:
910 {
Steve Block3856b092011-10-20 11:56:00 +0100911 ALOGV("kWhatReset");
Andreas Huber1aef2112011-01-04 14:01:29 -0800912
Andreas Hubera1f8ab02012-11-30 10:53:22 -0800913 mDeferredActions.push_back(
Andreas Huber14f76722013-01-15 09:04:18 -0800914 new ShutdownDecoderAction(
915 true /* audio */, true /* video */));
Andreas Huberb7c8e912012-11-27 15:02:53 -0800916
Andreas Hubera1f8ab02012-11-30 10:53:22 -0800917 mDeferredActions.push_back(
918 new SimpleAction(&NuPlayer::performReset));
Andreas Huberb58ce9f2011-11-28 16:27:35 -0800919
Andreas Hubera1f8ab02012-11-30 10:53:22 -0800920 processDeferredActions();
Andreas Huber1aef2112011-01-04 14:01:29 -0800921 break;
922 }
923
Andreas Huber43c3e6c2011-01-05 12:17:08 -0800924 case kWhatSeek:
925 {
926 int64_t seekTimeUs;
927 CHECK(msg->findInt64("seekTimeUs", &seekTimeUs));
928
Andreas Hubera1f8ab02012-11-30 10:53:22 -0800929 ALOGV("kWhatSeek seekTimeUs=%lld us", seekTimeUs);
Andreas Huber43c3e6c2011-01-05 12:17:08 -0800930
Andreas Hubera1f8ab02012-11-30 10:53:22 -0800931 mDeferredActions.push_back(
932 new SimpleAction(&NuPlayer::performDecoderFlush));
Andreas Huber43c3e6c2011-01-05 12:17:08 -0800933
Andreas Hubera1f8ab02012-11-30 10:53:22 -0800934 mDeferredActions.push_back(new SeekAction(seekTimeUs));
Andreas Huber43c3e6c2011-01-05 12:17:08 -0800935
Andreas Hubera1f8ab02012-11-30 10:53:22 -0800936 processDeferredActions();
Andreas Huber43c3e6c2011-01-05 12:17:08 -0800937 break;
938 }
939
Andreas Huberb4082222011-01-20 15:23:04 -0800940 case kWhatPause:
941 {
942 CHECK(mRenderer != NULL);
Roger Jönssonfba60da2013-01-21 17:15:45 +0100943 mSource->pause();
Andreas Huberb4082222011-01-20 15:23:04 -0800944 mRenderer->pause();
945 break;
946 }
947
948 case kWhatResume:
949 {
950 CHECK(mRenderer != NULL);
Roger Jönssonfba60da2013-01-21 17:15:45 +0100951 mSource->resume();
Andreas Huberb4082222011-01-20 15:23:04 -0800952 mRenderer->resume();
953 break;
954 }
955
Andreas Huberb5f25f02013-02-05 10:14:26 -0800956 case kWhatSourceNotify:
957 {
Andreas Huber9575c962013-02-05 13:59:56 -0800958 onSourceNotify(msg);
Andreas Huberb5f25f02013-02-05 10:14:26 -0800959 break;
960 }
961
Chong Zhanga7fa1d92014-06-11 14:49:23 -0700962 case kWhatClosedCaptionNotify:
963 {
964 onClosedCaptionNotify(msg);
965 break;
966 }
967
Andreas Huberf9334412010-12-15 15:17:42 -0800968 default:
969 TRESPASS();
970 break;
971 }
972}
973
Andreas Huber3831a062010-12-21 10:22:33 -0800974void NuPlayer::finishFlushIfPossible() {
Wei Jia53904f32014-07-29 10:22:53 -0700975 if (mFlushingAudio != NONE && mFlushingAudio != FLUSHED
976 && mFlushingAudio != SHUT_DOWN) {
Andreas Huber3831a062010-12-21 10:22:33 -0800977 return;
978 }
979
Wei Jia53904f32014-07-29 10:22:53 -0700980 if (mFlushingVideo != NONE && mFlushingVideo != FLUSHED
981 && mFlushingVideo != SHUT_DOWN) {
Andreas Huber3831a062010-12-21 10:22:33 -0800982 return;
983 }
984
Steve Block3856b092011-10-20 11:56:00 +0100985 ALOGV("both audio and video are flushed now.");
Andreas Huber3831a062010-12-21 10:22:33 -0800986
Phil Burk9f526492014-09-03 15:04:12 -0700987 mPendingAudioAccessUnit.clear();
988
Andreas Huber6e3d3112011-11-28 12:36:11 -0800989 if (mTimeDiscontinuityPending) {
990 mRenderer->signalTimeDiscontinuity();
991 mTimeDiscontinuityPending = false;
992 }
Andreas Huber3831a062010-12-21 10:22:33 -0800993
Wei Jia53904f32014-07-29 10:22:53 -0700994 if (mAudioDecoder != NULL && mFlushingAudio == FLUSHED) {
Andreas Huber3831a062010-12-21 10:22:33 -0800995 mAudioDecoder->signalResume();
996 }
997
Wei Jia53904f32014-07-29 10:22:53 -0700998 if (mVideoDecoder != NULL && mFlushingVideo == FLUSHED) {
Andreas Huber3831a062010-12-21 10:22:33 -0800999 mVideoDecoder->signalResume();
1000 }
1001
1002 mFlushingAudio = NONE;
1003 mFlushingVideo = NONE;
Andreas Huber3831a062010-12-21 10:22:33 -08001004
Andreas Hubera1f8ab02012-11-30 10:53:22 -08001005 processDeferredActions();
Andreas Huber1aef2112011-01-04 14:01:29 -08001006}
1007
1008void NuPlayer::postScanSources() {
1009 if (mScanSourcesPending) {
1010 return;
1011 }
1012
1013 sp<AMessage> msg = new AMessage(kWhatScanSources, id());
1014 msg->setInt32("generation", mScanSourcesGeneration);
1015 msg->post();
1016
1017 mScanSourcesPending = true;
1018}
1019
Andy Hung282a7e32014-08-14 15:56:34 -07001020void NuPlayer::openAudioSink(const sp<AMessage> &format, bool offloadOnly) {
1021 ALOGV("openAudioSink: offloadOnly(%d) mOffloadAudio(%d)",
1022 offloadOnly, mOffloadAudio);
1023 bool audioSinkChanged = false;
1024
1025 int32_t numChannels;
1026 CHECK(format->findInt32("channel-count", &numChannels));
1027
1028 int32_t channelMask;
1029 if (!format->findInt32("channel-mask", &channelMask)) {
1030 // signal to the AudioSink to derive the mask from count.
1031 channelMask = CHANNEL_MASK_USE_CHANNEL_ORDER;
1032 }
1033
1034 int32_t sampleRate;
1035 CHECK(format->findInt32("sample-rate", &sampleRate));
1036
1037 uint32_t flags;
1038 int64_t durationUs;
1039 // FIXME: we should handle the case where the video decoder
1040 // is created after we receive the format change indication.
1041 // Current code will just make that we select deep buffer
1042 // with video which should not be a problem as it should
1043 // not prevent from keeping A/V sync.
1044 if (mVideoDecoder == NULL &&
1045 mSource->getDuration(&durationUs) == OK &&
1046 durationUs
1047 > AUDIO_SINK_MIN_DEEP_BUFFER_DURATION_US) {
1048 flags = AUDIO_OUTPUT_FLAG_DEEP_BUFFER;
1049 } else {
1050 flags = AUDIO_OUTPUT_FLAG_NONE;
1051 }
1052
1053 if (mOffloadAudio) {
1054 audio_format_t audioFormat = AUDIO_FORMAT_PCM_16_BIT;
1055 AString mime;
1056 CHECK(format->findString("mime", &mime));
1057 status_t err = mapMimeToAudioFormat(audioFormat, mime.c_str());
1058
1059 if (err != OK) {
1060 ALOGE("Couldn't map mime \"%s\" to a valid "
1061 "audio_format", mime.c_str());
1062 mOffloadAudio = false;
1063 } else {
1064 ALOGV("Mime \"%s\" mapped to audio_format 0x%x",
1065 mime.c_str(), audioFormat);
1066
1067 int avgBitRate = -1;
1068 format->findInt32("bit-rate", &avgBitRate);
1069
1070 int32_t aacProfile = -1;
1071 if (audioFormat == AUDIO_FORMAT_AAC
1072 && format->findInt32("aac-profile", &aacProfile)) {
1073 // Redefine AAC format as per aac profile
1074 mapAACProfileToAudioFormat(
1075 audioFormat,
1076 aacProfile);
1077 }
1078
1079 audio_offload_info_t offloadInfo = AUDIO_INFO_INITIALIZER;
1080 offloadInfo.duration_us = -1;
1081 format->findInt64(
1082 "durationUs", &offloadInfo.duration_us);
1083 offloadInfo.sample_rate = sampleRate;
1084 offloadInfo.channel_mask = channelMask;
1085 offloadInfo.format = audioFormat;
1086 offloadInfo.stream_type = AUDIO_STREAM_MUSIC;
1087 offloadInfo.bit_rate = avgBitRate;
1088 offloadInfo.has_video = (mVideoDecoder != NULL);
1089 offloadInfo.is_streaming = true;
1090
1091 if (memcmp(&mCurrentOffloadInfo, &offloadInfo, sizeof(offloadInfo)) == 0) {
1092 ALOGV("openAudioSink: no change in offload mode");
1093 return; // no change from previous configuration, everything ok.
1094 }
1095 ALOGV("openAudioSink: try to open AudioSink in offload mode");
1096 flags |= AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD;
Ronghua Wu1ffb5382014-08-18 15:57:03 -07001097 flags &= ~AUDIO_OUTPUT_FLAG_DEEP_BUFFER;
Andy Hung282a7e32014-08-14 15:56:34 -07001098 audioSinkChanged = true;
1099 mAudioSink->close();
1100 err = mAudioSink->open(
1101 sampleRate,
1102 numChannels,
1103 (audio_channel_mask_t)channelMask,
1104 audioFormat,
1105 8 /* bufferCount */,
1106 &NuPlayer::Renderer::AudioSinkCallback,
1107 mRenderer.get(),
1108 (audio_output_flags_t)flags,
1109 &offloadInfo);
1110
1111 if (err == OK) {
1112 // If the playback is offloaded to h/w, we pass
1113 // the HAL some metadata information.
1114 // We don't want to do this for PCM because it
1115 // will be going through the AudioFlinger mixer
1116 // before reaching the hardware.
1117 sp<MetaData> audioMeta =
1118 mSource->getFormatMeta(true /* audio */);
1119 sendMetaDataToHal(mAudioSink, audioMeta);
1120 mCurrentOffloadInfo = offloadInfo;
1121 err = mAudioSink->start();
1122 ALOGV_IF(err == OK, "openAudioSink: offload succeeded");
1123 }
1124 if (err != OK) {
1125 // Clean up, fall back to non offload mode.
1126 mAudioSink->close();
1127 mRenderer->signalDisableOffloadAudio();
1128 mOffloadAudio = false;
1129 mCurrentOffloadInfo = AUDIO_INFO_INITIALIZER;
1130 ALOGV("openAudioSink: offload failed");
1131 }
1132 }
1133 }
1134 if (!offloadOnly && !mOffloadAudio) {
1135 flags &= ~AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD;
1136 ALOGV("openAudioSink: open AudioSink in NON-offload mode");
1137
1138 audioSinkChanged = true;
1139 mAudioSink->close();
1140 mCurrentOffloadInfo = AUDIO_INFO_INITIALIZER;
1141 CHECK_EQ(mAudioSink->open(
1142 sampleRate,
1143 numChannels,
1144 (audio_channel_mask_t)channelMask,
1145 AUDIO_FORMAT_PCM_16_BIT,
1146 8 /* bufferCount */,
1147 NULL,
1148 NULL,
1149 (audio_output_flags_t)flags),
1150 (status_t)OK);
1151 mAudioSink->start();
1152 }
1153 if (audioSinkChanged) {
1154 mRenderer->signalAudioSinkChanged();
1155 }
1156}
1157
1158void NuPlayer::closeAudioSink() {
1159 mAudioSink->close();
1160 mCurrentOffloadInfo = AUDIO_INFO_INITIALIZER;
1161}
1162
Andreas Huber5bc087c2010-12-23 10:27:40 -08001163status_t NuPlayer::instantiateDecoder(bool audio, sp<Decoder> *decoder) {
Andreas Huberf9334412010-12-15 15:17:42 -08001164 if (*decoder != NULL) {
1165 return OK;
1166 }
1167
Andreas Huber84066782011-08-16 09:34:26 -07001168 sp<AMessage> format = mSource->getFormat(audio);
Andreas Huberf9334412010-12-15 15:17:42 -08001169
Andreas Huber84066782011-08-16 09:34:26 -07001170 if (format == NULL) {
Andreas Huberf9334412010-12-15 15:17:42 -08001171 return -EWOULDBLOCK;
1172 }
1173
Andreas Huber3fe62152011-09-16 15:09:22 -07001174 if (!audio) {
Andreas Huber84066782011-08-16 09:34:26 -07001175 AString mime;
1176 CHECK(format->findString("mime", &mime));
1177 mVideoIsAVC = !strcasecmp(MEDIA_MIMETYPE_VIDEO_AVC, mime.c_str());
Chong Zhanga7fa1d92014-06-11 14:49:23 -07001178
1179 sp<AMessage> ccNotify = new AMessage(kWhatClosedCaptionNotify, id());
1180 mCCDecoder = new CCDecoder(ccNotify);
Lajos Molnar09524832014-07-17 14:29:51 -07001181
1182 if (mSourceFlags & Source::FLAG_SECURE) {
1183 format->setInt32("secure", true);
1184 }
Andreas Huber3fe62152011-09-16 15:09:22 -07001185 }
1186
Wei Jiabc2fb722014-07-08 16:37:57 -07001187 if (audio) {
Wei Jia88703c32014-08-06 11:24:07 -07001188 sp<AMessage> notify = new AMessage(kWhatAudioNotify, id());
1189 ++mAudioDecoderGeneration;
1190 notify->setInt32("generation", mAudioDecoderGeneration);
1191
Wei Jiabc2fb722014-07-08 16:37:57 -07001192 if (mOffloadAudio) {
1193 *decoder = new DecoderPassThrough(notify);
1194 } else {
1195 *decoder = new Decoder(notify);
1196 }
1197 } else {
Wei Jia88703c32014-08-06 11:24:07 -07001198 sp<AMessage> notify = new AMessage(kWhatVideoNotify, id());
1199 ++mVideoDecoderGeneration;
1200 notify->setInt32("generation", mVideoDecoderGeneration);
1201
Wei Jiabc2fb722014-07-08 16:37:57 -07001202 *decoder = new Decoder(notify, mNativeWindow);
1203 }
Lajos Molnar1cd13982014-01-17 15:12:51 -08001204 (*decoder)->init();
Andreas Huber84066782011-08-16 09:34:26 -07001205 (*decoder)->configure(format);
Andreas Huberf9334412010-12-15 15:17:42 -08001206
Lajos Molnar09524832014-07-17 14:29:51 -07001207 // allocate buffers to decrypt widevine source buffers
1208 if (!audio && (mSourceFlags & Source::FLAG_SECURE)) {
1209 Vector<sp<ABuffer> > inputBufs;
1210 CHECK_EQ((*decoder)->getInputBuffers(&inputBufs), (status_t)OK);
1211
1212 Vector<MediaBuffer *> mediaBufs;
1213 for (size_t i = 0; i < inputBufs.size(); i++) {
1214 const sp<ABuffer> &buffer = inputBufs[i];
1215 MediaBuffer *mbuf = new MediaBuffer(buffer->data(), buffer->size());
1216 mediaBufs.push(mbuf);
1217 }
1218
1219 status_t err = mSource->setBuffers(audio, mediaBufs);
1220 if (err != OK) {
1221 for (size_t i = 0; i < mediaBufs.size(); ++i) {
1222 mediaBufs[i]->release();
1223 }
1224 mediaBufs.clear();
1225 ALOGE("Secure source didn't support secure mediaBufs.");
1226 return err;
1227 }
1228 }
Andreas Huberf9334412010-12-15 15:17:42 -08001229 return OK;
1230}
1231
1232status_t NuPlayer::feedDecoderInputData(bool audio, const sp<AMessage> &msg) {
1233 sp<AMessage> reply;
1234 CHECK(msg->findMessage("reply", &reply));
1235
Wei Jia53904f32014-07-29 10:22:53 -07001236 if ((audio && mFlushingAudio != NONE)
1237 || (!audio && mFlushingVideo != NONE)) {
Wei Jiab189a5b2014-08-07 06:11:39 +00001238 reply->setInt32("err", INFO_DISCONTINUITY);
1239 reply->post();
1240 return OK;
Andreas Huberf9334412010-12-15 15:17:42 -08001241 }
1242
1243 sp<ABuffer> accessUnit;
Andreas Huberf9334412010-12-15 15:17:42 -08001244
Phil Burk9f526492014-09-03 15:04:12 -07001245 // Aggregate smaller buffers into a larger buffer.
1246 // The goal is to reduce power consumption.
1247 // Unfortunately this does not work with the software AAC decoder.
1248 // TODO optimize buffer size for power consumption
1249 // The offload read buffer size is 32 KB but 24 KB uses less power.
1250 const int kAudioBigBufferSizeBytes = 24 * 1024;
1251 bool doBufferAggregation = (audio && mOffloadAudio);
1252 sp<ABuffer> biggerBuffer;
1253 bool needMoreData = false;
1254 int numSmallBuffers = 0;
1255 bool gotTime = false;
1256
Andreas Huber3fe62152011-09-16 15:09:22 -07001257 bool dropAccessUnit;
1258 do {
Phil Burk9f526492014-09-03 15:04:12 -07001259 status_t err;
1260 // Did we save an accessUnit earlier because of a discontinuity?
1261 if (audio && (mPendingAudioAccessUnit != NULL)) {
1262 accessUnit = mPendingAudioAccessUnit;
1263 mPendingAudioAccessUnit.clear();
1264 err = mPendingAudioErr;
1265 ALOGV("feedDecoderInputData() use mPendingAudioAccessUnit");
1266 } else {
1267 err = mSource->dequeueAccessUnit(audio, &accessUnit);
1268 }
Andreas Huber5bc087c2010-12-23 10:27:40 -08001269
Andreas Huber3fe62152011-09-16 15:09:22 -07001270 if (err == -EWOULDBLOCK) {
Phil Burk9f526492014-09-03 15:04:12 -07001271 ALOGD("feedDecoderInputData() got EWOULDBLOCK");
1272 if (biggerBuffer == NULL) {
1273 return err;
1274 } else {
1275 break; // Reply with data that we already have.
1276 }
Andreas Huber3fe62152011-09-16 15:09:22 -07001277 } else if (err != OK) {
1278 if (err == INFO_DISCONTINUITY) {
Phil Burk9f526492014-09-03 15:04:12 -07001279 if (biggerBuffer != NULL) {
1280 // We already have some data so save this for later.
1281 mPendingAudioErr = err;
1282 mPendingAudioAccessUnit = accessUnit;
1283 accessUnit.clear();
1284 ALOGD("feedDecoderInputData() save discontinuity for later");
1285 break;
1286 }
Andreas Huber3fe62152011-09-16 15:09:22 -07001287 int32_t type;
1288 CHECK(accessUnit->meta()->findInt32("discontinuity", &type));
Andreas Huber53df1a42010-12-22 10:03:04 -08001289
Andreas Huber3fe62152011-09-16 15:09:22 -07001290 bool formatChange =
Andreas Huber6e3d3112011-11-28 12:36:11 -08001291 (audio &&
1292 (type & ATSParser::DISCONTINUITY_AUDIO_FORMAT))
1293 || (!audio &&
1294 (type & ATSParser::DISCONTINUITY_VIDEO_FORMAT));
Andreas Huber53df1a42010-12-22 10:03:04 -08001295
Andreas Huber6e3d3112011-11-28 12:36:11 -08001296 bool timeChange = (type & ATSParser::DISCONTINUITY_TIME) != 0;
1297
Steve Blockdf64d152012-01-04 20:05:49 +00001298 ALOGI("%s discontinuity (formatChange=%d, time=%d)",
Andreas Huber6e3d3112011-11-28 12:36:11 -08001299 audio ? "audio" : "video", formatChange, timeChange);
Andreas Huber32f3cef2011-03-02 15:34:46 -08001300
Andreas Huber3fe62152011-09-16 15:09:22 -07001301 if (audio) {
1302 mSkipRenderingAudioUntilMediaTimeUs = -1;
1303 } else {
1304 mSkipRenderingVideoUntilMediaTimeUs = -1;
1305 }
Andreas Huber32f3cef2011-03-02 15:34:46 -08001306
Andreas Huber6e3d3112011-11-28 12:36:11 -08001307 if (timeChange) {
1308 sp<AMessage> extra;
1309 if (accessUnit->meta()->findMessage("extra", &extra)
1310 && extra != NULL) {
1311 int64_t resumeAtMediaTimeUs;
1312 if (extra->findInt64(
1313 "resume-at-mediatimeUs", &resumeAtMediaTimeUs)) {
Steve Blockdf64d152012-01-04 20:05:49 +00001314 ALOGI("suppressing rendering of %s until %lld us",
Andreas Huber6e3d3112011-11-28 12:36:11 -08001315 audio ? "audio" : "video", resumeAtMediaTimeUs);
Andreas Huber3fe62152011-09-16 15:09:22 -07001316
Andreas Huber6e3d3112011-11-28 12:36:11 -08001317 if (audio) {
1318 mSkipRenderingAudioUntilMediaTimeUs =
1319 resumeAtMediaTimeUs;
1320 } else {
1321 mSkipRenderingVideoUntilMediaTimeUs =
1322 resumeAtMediaTimeUs;
1323 }
Andreas Huber3fe62152011-09-16 15:09:22 -07001324 }
Andreas Huber32f3cef2011-03-02 15:34:46 -08001325 }
1326 }
Andreas Huber3fe62152011-09-16 15:09:22 -07001327
Andreas Huber6e3d3112011-11-28 12:36:11 -08001328 mTimeDiscontinuityPending =
1329 mTimeDiscontinuityPending || timeChange;
1330
Lajos Molnar87603c02014-08-20 19:25:30 -07001331 bool seamlessFormatChange = false;
1332 sp<AMessage> newFormat = mSource->getFormat(audio);
1333 if (formatChange) {
1334 seamlessFormatChange =
1335 getDecoder(audio)->supportsSeamlessFormatChange(newFormat);
1336 // treat seamless format change separately
1337 formatChange = !seamlessFormatChange;
1338 }
1339 bool shutdownOrFlush = formatChange || timeChange;
1340
1341 // We want to queue up scan-sources only once per discontinuity.
1342 // We control this by doing it only if neither audio nor video are
1343 // flushing or shutting down. (After handling 1st discontinuity, one
1344 // of the flushing states will not be NONE.)
1345 // No need to scan sources if this discontinuity does not result
1346 // in a flush or shutdown, as the flushing state will stay NONE.
1347 if (mFlushingAudio == NONE && mFlushingVideo == NONE &&
1348 shutdownOrFlush) {
Robert Shiha2981012014-07-30 17:41:24 -07001349 // And we'll resume scanning sources once we're done
1350 // flushing.
1351 mDeferredActions.push_front(
1352 new SimpleAction(
1353 &NuPlayer::performScanSources));
1354 }
1355
Lajos Molnar87603c02014-08-20 19:25:30 -07001356 if (formatChange /* not seamless */) {
1357 // must change decoder
1358 flushDecoder(audio, /* needShutdown = */ true);
1359 } else if (timeChange) {
1360 // need to flush
1361 flushDecoder(audio, /* needShutdown = */ false, newFormat);
1362 err = OK;
1363 } else if (seamlessFormatChange) {
1364 // reuse existing decoder and don't flush
1365 updateDecoderFormatWithoutFlush(audio, newFormat);
1366 err = OK;
Andreas Huber6e3d3112011-11-28 12:36:11 -08001367 } else {
1368 // This stream is unaffected by the discontinuity
Andreas Huber6e3d3112011-11-28 12:36:11 -08001369 return -EWOULDBLOCK;
1370 }
Andreas Huber32f3cef2011-03-02 15:34:46 -08001371 }
1372
Andreas Huber3fe62152011-09-16 15:09:22 -07001373 reply->setInt32("err", err);
1374 reply->post();
1375 return OK;
Andreas Huberf9334412010-12-15 15:17:42 -08001376 }
1377
Andreas Huber3fe62152011-09-16 15:09:22 -07001378 if (!audio) {
1379 ++mNumFramesTotal;
1380 }
1381
1382 dropAccessUnit = false;
1383 if (!audio
Lajos Molnar09524832014-07-17 14:29:51 -07001384 && !(mSourceFlags & Source::FLAG_SECURE)
Andreas Huber3fe62152011-09-16 15:09:22 -07001385 && mVideoLateByUs > 100000ll
1386 && mVideoIsAVC
1387 && !IsAVCReferenceFrame(accessUnit)) {
1388 dropAccessUnit = true;
1389 ++mNumFramesDropped;
1390 }
Phil Burk9f526492014-09-03 15:04:12 -07001391
1392 size_t smallSize = accessUnit->size();
1393 needMoreData = false;
1394 if (doBufferAggregation && (biggerBuffer == NULL)
1395 // Don't bother if only room for a few small buffers.
1396 && (smallSize < (kAudioBigBufferSizeBytes / 3))) {
1397 // Create a larger buffer for combining smaller buffers from the extractor.
1398 biggerBuffer = new ABuffer(kAudioBigBufferSizeBytes);
1399 biggerBuffer->setRange(0, 0); // start empty
1400 }
1401
1402 if (biggerBuffer != NULL) {
1403 int64_t timeUs;
1404 bool smallTimestampValid = accessUnit->meta()->findInt64("timeUs", &timeUs);
1405 // Will the smaller buffer fit?
1406 size_t bigSize = biggerBuffer->size();
1407 size_t roomLeft = biggerBuffer->capacity() - bigSize;
1408 // Should we save this small buffer for the next big buffer?
1409 // If the first small buffer did not have a timestamp then save
1410 // any buffer that does have a timestamp until the next big buffer.
1411 if ((smallSize > roomLeft)
1412 || (!gotTime && (numSmallBuffers > 0) && smallTimestampValid)) {
1413 mPendingAudioErr = err;
1414 mPendingAudioAccessUnit = accessUnit;
1415 accessUnit.clear();
1416 } else {
1417 // Append small buffer to the bigger buffer.
1418 memcpy(biggerBuffer->base() + bigSize, accessUnit->data(), smallSize);
1419 bigSize += smallSize;
1420 biggerBuffer->setRange(0, bigSize);
1421
1422 // Keep looping until we run out of room in the biggerBuffer.
1423 needMoreData = true;
1424
1425 // Grab time from first small buffer if available.
1426 if ((numSmallBuffers == 0) && smallTimestampValid) {
1427 biggerBuffer->meta()->setInt64("timeUs", timeUs);
1428 gotTime = true;
1429 }
1430
1431 ALOGV("feedDecoderInputData() #%d, smallSize = %zu, bigSize = %zu, capacity = %zu",
1432 numSmallBuffers, smallSize, bigSize, biggerBuffer->capacity());
1433 numSmallBuffers++;
1434 }
1435 }
1436 } while (dropAccessUnit || needMoreData);
Andreas Huberf9334412010-12-15 15:17:42 -08001437
Steve Block3856b092011-10-20 11:56:00 +01001438 // ALOGV("returned a valid buffer of %s data", audio ? "audio" : "video");
Andreas Huberf9334412010-12-15 15:17:42 -08001439
1440#if 0
1441 int64_t mediaTimeUs;
1442 CHECK(accessUnit->meta()->findInt64("timeUs", &mediaTimeUs));
Steve Block3856b092011-10-20 11:56:00 +01001443 ALOGV("feeding %s input buffer at media time %.2f secs",
Andreas Huberf9334412010-12-15 15:17:42 -08001444 audio ? "audio" : "video",
1445 mediaTimeUs / 1E6);
1446#endif
1447
Chong Zhanga7fa1d92014-06-11 14:49:23 -07001448 if (!audio) {
1449 mCCDecoder->decode(accessUnit);
1450 }
1451
Phil Burk9f526492014-09-03 15:04:12 -07001452 if (biggerBuffer != NULL) {
1453 ALOGV("feedDecoderInputData() reply with aggregated buffer, %d", numSmallBuffers);
1454 reply->setBuffer("buffer", biggerBuffer);
1455 } else {
1456 reply->setBuffer("buffer", accessUnit);
1457 }
1458
Andreas Huberf9334412010-12-15 15:17:42 -08001459 reply->post();
1460
1461 return OK;
1462}
1463
1464void NuPlayer::renderBuffer(bool audio, const sp<AMessage> &msg) {
Steve Block3856b092011-10-20 11:56:00 +01001465 // ALOGV("renderBuffer %s", audio ? "audio" : "video");
Andreas Huberf9334412010-12-15 15:17:42 -08001466
1467 sp<AMessage> reply;
1468 CHECK(msg->findMessage("reply", &reply));
1469
Wei Jia53904f32014-07-29 10:22:53 -07001470 if ((audio && mFlushingAudio != NONE)
1471 || (!audio && mFlushingVideo != NONE)) {
Andreas Huber18ac5402011-08-31 15:04:25 -07001472 // We're currently attempting to flush the decoder, in order
1473 // to complete this, the decoder wants all its buffers back,
1474 // so we don't want any output buffers it sent us (from before
1475 // we initiated the flush) to be stuck in the renderer's queue.
1476
Steve Block3856b092011-10-20 11:56:00 +01001477 ALOGV("we're still flushing the %s decoder, sending its output buffer"
Andreas Huber18ac5402011-08-31 15:04:25 -07001478 " right back.", audio ? "audio" : "video");
1479
1480 reply->post();
1481 return;
1482 }
1483
Andreas Huber2d8bedd2012-02-21 14:38:23 -08001484 sp<ABuffer> buffer;
1485 CHECK(msg->findBuffer("buffer", &buffer));
Andreas Huberf9334412010-12-15 15:17:42 -08001486
Chong Zhanga7fa1d92014-06-11 14:49:23 -07001487 int64_t mediaTimeUs;
1488 CHECK(buffer->meta()->findInt64("timeUs", &mediaTimeUs));
1489
Andreas Huber32f3cef2011-03-02 15:34:46 -08001490 int64_t &skipUntilMediaTimeUs =
1491 audio
1492 ? mSkipRenderingAudioUntilMediaTimeUs
1493 : mSkipRenderingVideoUntilMediaTimeUs;
1494
1495 if (skipUntilMediaTimeUs >= 0) {
Andreas Huber32f3cef2011-03-02 15:34:46 -08001496
1497 if (mediaTimeUs < skipUntilMediaTimeUs) {
Steve Block3856b092011-10-20 11:56:00 +01001498 ALOGV("dropping %s buffer at time %lld as requested.",
Andreas Huber32f3cef2011-03-02 15:34:46 -08001499 audio ? "audio" : "video",
1500 mediaTimeUs);
1501
1502 reply->post();
1503 return;
1504 }
1505
1506 skipUntilMediaTimeUs = -1;
1507 }
1508
Chong Zhanga7fa1d92014-06-11 14:49:23 -07001509 if (!audio && mCCDecoder->isSelected()) {
1510 mCCDecoder->display(mediaTimeUs);
1511 }
1512
Andreas Huberf9334412010-12-15 15:17:42 -08001513 mRenderer->queueBuffer(audio, buffer, reply);
1514}
1515
Chong Zhangced1c2f2014-08-08 15:22:35 -07001516void NuPlayer::updateVideoSize(
1517 const sp<AMessage> &inputFormat,
1518 const sp<AMessage> &outputFormat) {
1519 if (inputFormat == NULL) {
1520 ALOGW("Unknown video size, reporting 0x0!");
1521 notifyListener(MEDIA_SET_VIDEO_SIZE, 0, 0);
1522 return;
1523 }
1524
1525 int32_t displayWidth, displayHeight;
1526 int32_t cropLeft, cropTop, cropRight, cropBottom;
1527
1528 if (outputFormat != NULL) {
1529 int32_t width, height;
1530 CHECK(outputFormat->findInt32("width", &width));
1531 CHECK(outputFormat->findInt32("height", &height));
1532
1533 int32_t cropLeft, cropTop, cropRight, cropBottom;
1534 CHECK(outputFormat->findRect(
1535 "crop",
1536 &cropLeft, &cropTop, &cropRight, &cropBottom));
1537
1538 displayWidth = cropRight - cropLeft + 1;
1539 displayHeight = cropBottom - cropTop + 1;
1540
1541 ALOGV("Video output format changed to %d x %d "
1542 "(crop: %d x %d @ (%d, %d))",
1543 width, height,
1544 displayWidth,
1545 displayHeight,
1546 cropLeft, cropTop);
1547 } else {
1548 CHECK(inputFormat->findInt32("width", &displayWidth));
1549 CHECK(inputFormat->findInt32("height", &displayHeight));
1550
1551 ALOGV("Video input format %d x %d", displayWidth, displayHeight);
1552 }
1553
1554 // Take into account sample aspect ratio if necessary:
1555 int32_t sarWidth, sarHeight;
1556 if (inputFormat->findInt32("sar-width", &sarWidth)
1557 && inputFormat->findInt32("sar-height", &sarHeight)) {
1558 ALOGV("Sample aspect ratio %d : %d", sarWidth, sarHeight);
1559
1560 displayWidth = (displayWidth * sarWidth) / sarHeight;
1561
1562 ALOGV("display dimensions %d x %d", displayWidth, displayHeight);
1563 }
1564
1565 int32_t rotationDegrees;
1566 if (!inputFormat->findInt32("rotation-degrees", &rotationDegrees)) {
1567 rotationDegrees = 0;
1568 }
1569
1570 if (rotationDegrees == 90 || rotationDegrees == 270) {
1571 int32_t tmp = displayWidth;
1572 displayWidth = displayHeight;
1573 displayHeight = tmp;
1574 }
1575
1576 notifyListener(
1577 MEDIA_SET_VIDEO_SIZE,
1578 displayWidth,
1579 displayHeight);
1580}
1581
Chong Zhangdcb89b32013-08-06 09:44:47 -07001582void NuPlayer::notifyListener(int msg, int ext1, int ext2, const Parcel *in) {
Andreas Huber43c3e6c2011-01-05 12:17:08 -08001583 if (mDriver == NULL) {
Andreas Huberf9334412010-12-15 15:17:42 -08001584 return;
1585 }
1586
Andreas Huber43c3e6c2011-01-05 12:17:08 -08001587 sp<NuPlayerDriver> driver = mDriver.promote();
Andreas Huberf9334412010-12-15 15:17:42 -08001588
Andreas Huber43c3e6c2011-01-05 12:17:08 -08001589 if (driver == NULL) {
Andreas Huberf9334412010-12-15 15:17:42 -08001590 return;
1591 }
1592
Chong Zhangdcb89b32013-08-06 09:44:47 -07001593 driver->notifyListener(msg, ext1, ext2, in);
Andreas Huberf9334412010-12-15 15:17:42 -08001594}
1595
Lajos Molnar87603c02014-08-20 19:25:30 -07001596void NuPlayer::flushDecoder(
1597 bool audio, bool needShutdown, const sp<AMessage> &newFormat) {
Andreas Huber14f76722013-01-15 09:04:18 -08001598 ALOGV("[%s] flushDecoder needShutdown=%d",
1599 audio ? "audio" : "video", needShutdown);
1600
Lajos Molnar87603c02014-08-20 19:25:30 -07001601 const sp<Decoder> &decoder = getDecoder(audio);
1602 if (decoder == NULL) {
Steve Blockdf64d152012-01-04 20:05:49 +00001603 ALOGI("flushDecoder %s without decoder present",
Andreas Huber6e3d3112011-11-28 12:36:11 -08001604 audio ? "audio" : "video");
Lajos Molnar87603c02014-08-20 19:25:30 -07001605 return;
Andreas Huber6e3d3112011-11-28 12:36:11 -08001606 }
1607
Andreas Huber1aef2112011-01-04 14:01:29 -08001608 // Make sure we don't continue to scan sources until we finish flushing.
1609 ++mScanSourcesGeneration;
Andreas Huber43c3e6c2011-01-05 12:17:08 -08001610 mScanSourcesPending = false;
Andreas Huber1aef2112011-01-04 14:01:29 -08001611
Lajos Molnar87603c02014-08-20 19:25:30 -07001612 decoder->signalFlush(newFormat);
Andreas Huber1aef2112011-01-04 14:01:29 -08001613 mRenderer->flush(audio);
1614
1615 FlushStatus newStatus =
1616 needShutdown ? FLUSHING_DECODER_SHUTDOWN : FLUSHING_DECODER;
1617
1618 if (audio) {
Wei Jia53904f32014-07-29 10:22:53 -07001619 ALOGE_IF(mFlushingAudio != NONE,
1620 "audio flushDecoder() is called in state %d", mFlushingAudio);
Andreas Huber1aef2112011-01-04 14:01:29 -08001621 mFlushingAudio = newStatus;
Andreas Huber1aef2112011-01-04 14:01:29 -08001622 } else {
Wei Jia53904f32014-07-29 10:22:53 -07001623 ALOGE_IF(mFlushingVideo != NONE,
1624 "video flushDecoder() is called in state %d", mFlushingVideo);
Andreas Huber1aef2112011-01-04 14:01:29 -08001625 mFlushingVideo = newStatus;
Andreas Huber1aef2112011-01-04 14:01:29 -08001626 }
1627}
1628
Lajos Molnar87603c02014-08-20 19:25:30 -07001629void NuPlayer::updateDecoderFormatWithoutFlush(
1630 bool audio, const sp<AMessage> &format) {
1631 ALOGV("[%s] updateDecoderFormatWithoutFlush", audio ? "audio" : "video");
1632
1633 const sp<Decoder> &decoder = getDecoder(audio);
1634 if (decoder == NULL) {
1635 ALOGI("updateDecoderFormatWithoutFlush %s without decoder present",
1636 audio ? "audio" : "video");
1637 return;
1638 }
1639
1640 decoder->signalUpdateFormat(format);
1641}
1642
Chong Zhangced1c2f2014-08-08 15:22:35 -07001643void NuPlayer::queueDecoderShutdown(
1644 bool audio, bool video, const sp<AMessage> &reply) {
1645 ALOGI("queueDecoderShutdown audio=%d, video=%d", audio, video);
Andreas Huber84066782011-08-16 09:34:26 -07001646
Chong Zhangced1c2f2014-08-08 15:22:35 -07001647 mDeferredActions.push_back(
1648 new ShutdownDecoderAction(audio, video));
Andreas Huber84066782011-08-16 09:34:26 -07001649
Chong Zhangced1c2f2014-08-08 15:22:35 -07001650 mDeferredActions.push_back(
1651 new SimpleAction(&NuPlayer::performScanSources));
Andreas Huber84066782011-08-16 09:34:26 -07001652
Chong Zhangced1c2f2014-08-08 15:22:35 -07001653 mDeferredActions.push_back(new PostMessageAction(reply));
1654
1655 processDeferredActions();
Andreas Huber84066782011-08-16 09:34:26 -07001656}
1657
James Dong0d268a32012-08-31 12:18:27 -07001658status_t NuPlayer::setVideoScalingMode(int32_t mode) {
1659 mVideoScalingMode = mode;
Andreas Huber57a339c2012-12-03 11:18:00 -08001660 if (mNativeWindow != NULL) {
James Dong0d268a32012-08-31 12:18:27 -07001661 status_t ret = native_window_set_scaling_mode(
1662 mNativeWindow->getNativeWindow().get(), mVideoScalingMode);
1663 if (ret != OK) {
1664 ALOGE("Failed to set scaling mode (%d): %s",
1665 -ret, strerror(-ret));
1666 return ret;
1667 }
1668 }
1669 return OK;
1670}
1671
Chong Zhangdcb89b32013-08-06 09:44:47 -07001672status_t NuPlayer::getTrackInfo(Parcel* reply) const {
1673 sp<AMessage> msg = new AMessage(kWhatGetTrackInfo, id());
1674 msg->setPointer("reply", reply);
1675
1676 sp<AMessage> response;
1677 status_t err = msg->postAndAwaitResponse(&response);
1678 return err;
1679}
1680
Robert Shih7c4f0d72014-07-09 18:53:31 -07001681status_t NuPlayer::getSelectedTrack(int32_t type, Parcel* reply) const {
1682 sp<AMessage> msg = new AMessage(kWhatGetSelectedTrack, id());
1683 msg->setPointer("reply", reply);
1684 msg->setInt32("type", type);
1685
1686 sp<AMessage> response;
1687 status_t err = msg->postAndAwaitResponse(&response);
1688 if (err == OK && response != NULL) {
1689 CHECK(response->findInt32("err", &err));
1690 }
1691 return err;
1692}
1693
Chong Zhangdcb89b32013-08-06 09:44:47 -07001694status_t NuPlayer::selectTrack(size_t trackIndex, bool select) {
1695 sp<AMessage> msg = new AMessage(kWhatSelectTrack, id());
1696 msg->setSize("trackIndex", trackIndex);
1697 msg->setInt32("select", select);
1698
1699 sp<AMessage> response;
1700 status_t err = msg->postAndAwaitResponse(&response);
1701
Chong Zhang404fced2014-06-11 14:45:31 -07001702 if (err != OK) {
1703 return err;
1704 }
1705
1706 if (!response->findInt32("err", &err)) {
1707 err = OK;
1708 }
1709
Chong Zhangdcb89b32013-08-06 09:44:47 -07001710 return err;
1711}
1712
Andreas Huberb7c8e912012-11-27 15:02:53 -08001713void NuPlayer::schedulePollDuration() {
1714 sp<AMessage> msg = new AMessage(kWhatPollDuration, id());
1715 msg->setInt32("generation", mPollDurationGeneration);
1716 msg->post();
1717}
1718
1719void NuPlayer::cancelPollDuration() {
1720 ++mPollDurationGeneration;
1721}
1722
Andreas Hubera1f8ab02012-11-30 10:53:22 -08001723void NuPlayer::processDeferredActions() {
1724 while (!mDeferredActions.empty()) {
1725 // We won't execute any deferred actions until we're no longer in
1726 // an intermediate state, i.e. one more more decoders are currently
1727 // flushing or shutting down.
1728
Andreas Hubera1f8ab02012-11-30 10:53:22 -08001729 if (mFlushingAudio != NONE || mFlushingVideo != NONE) {
1730 // We're currently flushing, postpone the reset until that's
1731 // completed.
1732
1733 ALOGV("postponing action mFlushingAudio=%d, mFlushingVideo=%d",
1734 mFlushingAudio, mFlushingVideo);
1735
1736 break;
1737 }
1738
1739 sp<Action> action = *mDeferredActions.begin();
1740 mDeferredActions.erase(mDeferredActions.begin());
1741
1742 action->execute(this);
1743 }
1744}
1745
1746void NuPlayer::performSeek(int64_t seekTimeUs) {
1747 ALOGV("performSeek seekTimeUs=%lld us (%.2f secs)",
1748 seekTimeUs,
1749 seekTimeUs / 1E6);
1750
1751 mSource->seekTo(seekTimeUs);
Robert Shihd3b0bbb2014-07-23 15:00:25 -07001752 ++mTimedTextGeneration;
Andreas Hubera1f8ab02012-11-30 10:53:22 -08001753
1754 if (mDriver != NULL) {
1755 sp<NuPlayerDriver> driver = mDriver.promote();
1756 if (driver != NULL) {
1757 driver->notifyPosition(seekTimeUs);
1758 driver->notifySeekComplete();
1759 }
1760 }
1761
1762 // everything's flushed, continue playback.
1763}
1764
1765void NuPlayer::performDecoderFlush() {
1766 ALOGV("performDecoderFlush");
1767
Andreas Huberda9740e2013-04-16 10:54:03 -07001768 if (mAudioDecoder == NULL && mVideoDecoder == NULL) {
Andreas Hubera1f8ab02012-11-30 10:53:22 -08001769 return;
1770 }
1771
1772 mTimeDiscontinuityPending = true;
1773
1774 if (mAudioDecoder != NULL) {
1775 flushDecoder(true /* audio */, false /* needShutdown */);
1776 }
1777
1778 if (mVideoDecoder != NULL) {
1779 flushDecoder(false /* audio */, false /* needShutdown */);
1780 }
1781}
1782
Andreas Huber14f76722013-01-15 09:04:18 -08001783void NuPlayer::performDecoderShutdown(bool audio, bool video) {
1784 ALOGV("performDecoderShutdown audio=%d, video=%d", audio, video);
Andreas Hubera1f8ab02012-11-30 10:53:22 -08001785
Andreas Huber14f76722013-01-15 09:04:18 -08001786 if ((!audio || mAudioDecoder == NULL)
1787 && (!video || mVideoDecoder == NULL)) {
Andreas Hubera1f8ab02012-11-30 10:53:22 -08001788 return;
1789 }
1790
1791 mTimeDiscontinuityPending = true;
1792
Andreas Huber14f76722013-01-15 09:04:18 -08001793 if (audio && mAudioDecoder != NULL) {
Andreas Hubera1f8ab02012-11-30 10:53:22 -08001794 flushDecoder(true /* audio */, true /* needShutdown */);
1795 }
1796
Andreas Huber14f76722013-01-15 09:04:18 -08001797 if (video && mVideoDecoder != NULL) {
Andreas Hubera1f8ab02012-11-30 10:53:22 -08001798 flushDecoder(false /* audio */, true /* needShutdown */);
1799 }
1800}
1801
1802void NuPlayer::performReset() {
1803 ALOGV("performReset");
1804
1805 CHECK(mAudioDecoder == NULL);
1806 CHECK(mVideoDecoder == NULL);
1807
1808 cancelPollDuration();
1809
1810 ++mScanSourcesGeneration;
1811 mScanSourcesPending = false;
1812
Lajos Molnar09524832014-07-17 14:29:51 -07001813 if (mRendererLooper != NULL) {
1814 if (mRenderer != NULL) {
1815 mRendererLooper->unregisterHandler(mRenderer->id());
1816 }
1817 mRendererLooper->stop();
1818 mRendererLooper.clear();
1819 }
Andreas Hubera1f8ab02012-11-30 10:53:22 -08001820 mRenderer.clear();
1821
1822 if (mSource != NULL) {
1823 mSource->stop();
Andreas Huberb5f25f02013-02-05 10:14:26 -08001824
Andreas Hubera1f8ab02012-11-30 10:53:22 -08001825 mSource.clear();
1826 }
1827
1828 if (mDriver != NULL) {
1829 sp<NuPlayerDriver> driver = mDriver.promote();
1830 if (driver != NULL) {
1831 driver->notifyResetComplete();
1832 }
1833 }
Andreas Huber57a339c2012-12-03 11:18:00 -08001834
1835 mStarted = false;
Andreas Hubera1f8ab02012-11-30 10:53:22 -08001836}
1837
1838void NuPlayer::performScanSources() {
1839 ALOGV("performScanSources");
1840
Andreas Huber57a339c2012-12-03 11:18:00 -08001841 if (!mStarted) {
1842 return;
1843 }
1844
Andreas Hubera1f8ab02012-11-30 10:53:22 -08001845 if (mAudioDecoder == NULL || mVideoDecoder == NULL) {
1846 postScanSources();
1847 }
1848}
1849
Andreas Huber57a339c2012-12-03 11:18:00 -08001850void NuPlayer::performSetSurface(const sp<NativeWindowWrapper> &wrapper) {
1851 ALOGV("performSetSurface");
1852
1853 mNativeWindow = wrapper;
1854
1855 // XXX - ignore error from setVideoScalingMode for now
1856 setVideoScalingMode(mVideoScalingMode);
Chong Zhang13d6faa2014-08-22 15:35:28 -07001857
1858 if (mDriver != NULL) {
1859 sp<NuPlayerDriver> driver = mDriver.promote();
1860 if (driver != NULL) {
1861 driver->notifySetSurfaceComplete();
1862 }
1863 }
Andreas Huber57a339c2012-12-03 11:18:00 -08001864}
1865
Andreas Huber9575c962013-02-05 13:59:56 -08001866void NuPlayer::onSourceNotify(const sp<AMessage> &msg) {
1867 int32_t what;
1868 CHECK(msg->findInt32("what", &what));
1869
1870 switch (what) {
1871 case Source::kWhatPrepared:
1872 {
Andreas Huberb5f28d42013-04-25 15:11:19 -07001873 if (mSource == NULL) {
1874 // This is a stale notification from a source that was
1875 // asynchronously preparing when the client called reset().
1876 // We handled the reset, the source is gone.
1877 break;
1878 }
1879
Andreas Huberec0c5972013-02-05 14:47:13 -08001880 int32_t err;
1881 CHECK(msg->findInt32("err", &err));
1882
Andreas Huber9575c962013-02-05 13:59:56 -08001883 sp<NuPlayerDriver> driver = mDriver.promote();
1884 if (driver != NULL) {
Marco Nelissendd114d12014-05-28 15:23:14 -07001885 // notify duration first, so that it's definitely set when
1886 // the app received the "prepare complete" callback.
1887 int64_t durationUs;
1888 if (mSource->getDuration(&durationUs) == OK) {
1889 driver->notifyDuration(durationUs);
1890 }
Andreas Huberec0c5972013-02-05 14:47:13 -08001891 driver->notifyPrepareCompleted(err);
Andreas Huber9575c962013-02-05 13:59:56 -08001892 }
Andreas Huber99759402013-04-01 14:28:31 -07001893
Andreas Huber9575c962013-02-05 13:59:56 -08001894 break;
1895 }
1896
1897 case Source::kWhatFlagsChanged:
1898 {
1899 uint32_t flags;
1900 CHECK(msg->findInt32("flags", (int32_t *)&flags));
1901
Chong Zhang4b7069d2013-09-11 12:52:43 -07001902 sp<NuPlayerDriver> driver = mDriver.promote();
1903 if (driver != NULL) {
1904 driver->notifyFlagsChanged(flags);
1905 }
1906
Andreas Huber9575c962013-02-05 13:59:56 -08001907 if ((mSourceFlags & Source::FLAG_DYNAMIC_DURATION)
1908 && (!(flags & Source::FLAG_DYNAMIC_DURATION))) {
1909 cancelPollDuration();
1910 } else if (!(mSourceFlags & Source::FLAG_DYNAMIC_DURATION)
1911 && (flags & Source::FLAG_DYNAMIC_DURATION)
1912 && (mAudioDecoder != NULL || mVideoDecoder != NULL)) {
1913 schedulePollDuration();
1914 }
1915
1916 mSourceFlags = flags;
1917 break;
1918 }
1919
1920 case Source::kWhatVideoSizeChanged:
1921 {
Chong Zhangced1c2f2014-08-08 15:22:35 -07001922 sp<AMessage> format;
1923 CHECK(msg->findMessage("format", &format));
Andreas Huber9575c962013-02-05 13:59:56 -08001924
Chong Zhangced1c2f2014-08-08 15:22:35 -07001925 updateVideoSize(format);
Andreas Huber9575c962013-02-05 13:59:56 -08001926 break;
1927 }
1928
Chong Zhang2a3cc9a2014-08-21 17:48:26 -07001929 case Source::kWhatBufferingUpdate:
1930 {
1931 int32_t percentage;
1932 CHECK(msg->findInt32("percentage", &percentage));
1933
1934 notifyListener(MEDIA_BUFFERING_UPDATE, percentage, 0);
1935 break;
1936 }
1937
Roger Jönssonb50e83e2013-01-21 16:26:41 +01001938 case Source::kWhatBufferingStart:
1939 {
1940 notifyListener(MEDIA_INFO, MEDIA_INFO_BUFFERING_START, 0);
1941 break;
1942 }
1943
1944 case Source::kWhatBufferingEnd:
1945 {
1946 notifyListener(MEDIA_INFO, MEDIA_INFO_BUFFERING_END, 0);
1947 break;
1948 }
1949
Chong Zhangdcb89b32013-08-06 09:44:47 -07001950 case Source::kWhatSubtitleData:
1951 {
1952 sp<ABuffer> buffer;
1953 CHECK(msg->findBuffer("buffer", &buffer));
1954
Chong Zhang404fced2014-06-11 14:45:31 -07001955 sendSubtitleData(buffer, 0 /* baseIndex */);
Chong Zhangdcb89b32013-08-06 09:44:47 -07001956 break;
1957 }
1958
Robert Shihd3b0bbb2014-07-23 15:00:25 -07001959 case Source::kWhatTimedTextData:
1960 {
1961 int32_t generation;
1962 if (msg->findInt32("generation", &generation)
1963 && generation != mTimedTextGeneration) {
1964 break;
1965 }
1966
1967 sp<ABuffer> buffer;
1968 CHECK(msg->findBuffer("buffer", &buffer));
1969
1970 sp<NuPlayerDriver> driver = mDriver.promote();
1971 if (driver == NULL) {
1972 break;
1973 }
1974
1975 int posMs;
1976 int64_t timeUs, posUs;
1977 driver->getCurrentPosition(&posMs);
1978 posUs = posMs * 1000;
1979 CHECK(buffer->meta()->findInt64("timeUs", &timeUs));
1980
1981 if (posUs < timeUs) {
1982 if (!msg->findInt32("generation", &generation)) {
1983 msg->setInt32("generation", mTimedTextGeneration);
1984 }
1985 msg->post(timeUs - posUs);
1986 } else {
1987 sendTimedTextData(buffer);
1988 }
1989 break;
1990 }
1991
Andreas Huber14f76722013-01-15 09:04:18 -08001992 case Source::kWhatQueueDecoderShutdown:
1993 {
1994 int32_t audio, video;
1995 CHECK(msg->findInt32("audio", &audio));
1996 CHECK(msg->findInt32("video", &video));
1997
1998 sp<AMessage> reply;
1999 CHECK(msg->findMessage("reply", &reply));
2000
2001 queueDecoderShutdown(audio, video, reply);
2002 break;
2003 }
2004
Ronghua Wu80276872014-08-28 15:50:29 -07002005 case Source::kWhatDrmNoLicense:
2006 {
2007 notifyListener(MEDIA_ERROR, MEDIA_ERROR_UNKNOWN, ERROR_DRM_NO_LICENSE);
2008 break;
2009 }
2010
Andreas Huber9575c962013-02-05 13:59:56 -08002011 default:
2012 TRESPASS();
2013 }
2014}
2015
Chong Zhanga7fa1d92014-06-11 14:49:23 -07002016void NuPlayer::onClosedCaptionNotify(const sp<AMessage> &msg) {
2017 int32_t what;
2018 CHECK(msg->findInt32("what", &what));
2019
2020 switch (what) {
2021 case NuPlayer::CCDecoder::kWhatClosedCaptionData:
2022 {
2023 sp<ABuffer> buffer;
2024 CHECK(msg->findBuffer("buffer", &buffer));
2025
2026 size_t inbandTracks = 0;
2027 if (mSource != NULL) {
2028 inbandTracks = mSource->getTrackCount();
2029 }
2030
2031 sendSubtitleData(buffer, inbandTracks);
2032 break;
2033 }
2034
2035 case NuPlayer::CCDecoder::kWhatTrackAdded:
2036 {
2037 notifyListener(MEDIA_INFO, MEDIA_INFO_METADATA_UPDATE, 0);
2038
2039 break;
2040 }
2041
2042 default:
2043 TRESPASS();
2044 }
2045
2046
2047}
2048
Chong Zhang404fced2014-06-11 14:45:31 -07002049void NuPlayer::sendSubtitleData(const sp<ABuffer> &buffer, int32_t baseIndex) {
2050 int32_t trackIndex;
2051 int64_t timeUs, durationUs;
2052 CHECK(buffer->meta()->findInt32("trackIndex", &trackIndex));
2053 CHECK(buffer->meta()->findInt64("timeUs", &timeUs));
2054 CHECK(buffer->meta()->findInt64("durationUs", &durationUs));
2055
2056 Parcel in;
2057 in.writeInt32(trackIndex + baseIndex);
2058 in.writeInt64(timeUs);
2059 in.writeInt64(durationUs);
2060 in.writeInt32(buffer->size());
2061 in.writeInt32(buffer->size());
2062 in.write(buffer->data(), buffer->size());
2063
2064 notifyListener(MEDIA_SUBTITLE_DATA, 0, 0, &in);
2065}
Robert Shihd3b0bbb2014-07-23 15:00:25 -07002066
2067void NuPlayer::sendTimedTextData(const sp<ABuffer> &buffer) {
2068 const void *data;
2069 size_t size = 0;
2070 int64_t timeUs;
2071 int32_t flag = TextDescriptions::LOCAL_DESCRIPTIONS;
2072
2073 AString mime;
2074 CHECK(buffer->meta()->findString("mime", &mime));
2075 CHECK(strcasecmp(mime.c_str(), MEDIA_MIMETYPE_TEXT_3GPP) == 0);
2076
2077 data = buffer->data();
2078 size = buffer->size();
2079
2080 Parcel parcel;
2081 if (size > 0) {
2082 CHECK(buffer->meta()->findInt64("timeUs", &timeUs));
2083 flag |= TextDescriptions::IN_BAND_TEXT_3GPP;
2084 TextDescriptions::getParcelOfDescriptions(
2085 (const uint8_t *)data, size, flag, timeUs / 1000, &parcel);
2086 }
2087
2088 if ((parcel.dataSize() > 0)) {
2089 notifyListener(MEDIA_TIMED_TEXT, 0, 0, &parcel);
2090 } else { // send an empty timed text
2091 notifyListener(MEDIA_TIMED_TEXT, 0, 0);
2092 }
2093}
Andreas Huberb5f25f02013-02-05 10:14:26 -08002094////////////////////////////////////////////////////////////////////////////////
2095
Chong Zhangced1c2f2014-08-08 15:22:35 -07002096sp<AMessage> NuPlayer::Source::getFormat(bool audio) {
2097 sp<MetaData> meta = getFormatMeta(audio);
2098
2099 if (meta == NULL) {
2100 return NULL;
2101 }
2102
2103 sp<AMessage> msg = new AMessage;
2104
2105 if(convertMetaDataToMessage(meta, &msg) == OK) {
2106 return msg;
2107 }
2108 return NULL;
2109}
2110
Andreas Huber9575c962013-02-05 13:59:56 -08002111void NuPlayer::Source::notifyFlagsChanged(uint32_t flags) {
2112 sp<AMessage> notify = dupNotify();
2113 notify->setInt32("what", kWhatFlagsChanged);
2114 notify->setInt32("flags", flags);
2115 notify->post();
2116}
2117
Chong Zhangced1c2f2014-08-08 15:22:35 -07002118void NuPlayer::Source::notifyVideoSizeChanged(const sp<AMessage> &format) {
Andreas Huber9575c962013-02-05 13:59:56 -08002119 sp<AMessage> notify = dupNotify();
2120 notify->setInt32("what", kWhatVideoSizeChanged);
Chong Zhangced1c2f2014-08-08 15:22:35 -07002121 notify->setMessage("format", format);
Andreas Huber9575c962013-02-05 13:59:56 -08002122 notify->post();
2123}
2124
Andreas Huberec0c5972013-02-05 14:47:13 -08002125void NuPlayer::Source::notifyPrepared(status_t err) {
Andreas Huber9575c962013-02-05 13:59:56 -08002126 sp<AMessage> notify = dupNotify();
2127 notify->setInt32("what", kWhatPrepared);
Andreas Huberec0c5972013-02-05 14:47:13 -08002128 notify->setInt32("err", err);
Andreas Huber9575c962013-02-05 13:59:56 -08002129 notify->post();
2130}
2131
Andreas Huber84333e02014-02-07 15:36:10 -08002132void NuPlayer::Source::onMessageReceived(const sp<AMessage> & /* msg */) {
Andreas Huberb5f25f02013-02-05 10:14:26 -08002133 TRESPASS();
2134}
2135
Andreas Huberf9334412010-12-15 15:17:42 -08002136} // namespace android