blob: 2ea12ae8a3644120423f879bb1d2f39191e07458 [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
Phil Burkc5cc2e22014-09-09 20:08:39 -070053// TODO optimize buffer size for power consumption
54// The offload read buffer size is 32 KB but 24 KB uses less power.
55const size_t NuPlayer::kAggregateBufferSizeBytes = 24 * 1024;
56
Andreas Hubera1f8ab02012-11-30 10:53:22 -080057struct NuPlayer::Action : public RefBase {
58 Action() {}
59
60 virtual void execute(NuPlayer *player) = 0;
61
62private:
63 DISALLOW_EVIL_CONSTRUCTORS(Action);
64};
65
66struct NuPlayer::SeekAction : public Action {
67 SeekAction(int64_t seekTimeUs)
68 : mSeekTimeUs(seekTimeUs) {
69 }
70
71 virtual void execute(NuPlayer *player) {
72 player->performSeek(mSeekTimeUs);
73 }
74
75private:
76 int64_t mSeekTimeUs;
77
78 DISALLOW_EVIL_CONSTRUCTORS(SeekAction);
79};
80
Andreas Huber57a339c2012-12-03 11:18:00 -080081struct NuPlayer::SetSurfaceAction : public Action {
82 SetSurfaceAction(const sp<NativeWindowWrapper> &wrapper)
83 : mWrapper(wrapper) {
84 }
85
86 virtual void execute(NuPlayer *player) {
87 player->performSetSurface(mWrapper);
88 }
89
90private:
91 sp<NativeWindowWrapper> mWrapper;
92
93 DISALLOW_EVIL_CONSTRUCTORS(SetSurfaceAction);
94};
95
Andreas Huber14f76722013-01-15 09:04:18 -080096struct NuPlayer::ShutdownDecoderAction : public Action {
97 ShutdownDecoderAction(bool audio, bool video)
98 : mAudio(audio),
99 mVideo(video) {
100 }
101
102 virtual void execute(NuPlayer *player) {
103 player->performDecoderShutdown(mAudio, mVideo);
104 }
105
106private:
107 bool mAudio;
108 bool mVideo;
109
110 DISALLOW_EVIL_CONSTRUCTORS(ShutdownDecoderAction);
111};
112
113struct NuPlayer::PostMessageAction : public Action {
114 PostMessageAction(const sp<AMessage> &msg)
115 : mMessage(msg) {
116 }
117
118 virtual void execute(NuPlayer *) {
119 mMessage->post();
120 }
121
122private:
123 sp<AMessage> mMessage;
124
125 DISALLOW_EVIL_CONSTRUCTORS(PostMessageAction);
126};
127
Andreas Hubera1f8ab02012-11-30 10:53:22 -0800128// Use this if there's no state necessary to save in order to execute
129// the action.
130struct NuPlayer::SimpleAction : public Action {
131 typedef void (NuPlayer::*ActionFunc)();
132
133 SimpleAction(ActionFunc func)
134 : mFunc(func) {
135 }
136
137 virtual void execute(NuPlayer *player) {
138 (player->*mFunc)();
139 }
140
141private:
142 ActionFunc mFunc;
143
144 DISALLOW_EVIL_CONSTRUCTORS(SimpleAction);
145};
146
Andreas Huberf9334412010-12-15 15:17:42 -0800147////////////////////////////////////////////////////////////////////////////////
148
149NuPlayer::NuPlayer()
Andreas Huber9b80c2b2011-06-30 15:47:02 -0700150 : mUIDValid(false),
Andreas Huber9575c962013-02-05 13:59:56 -0800151 mSourceFlags(0),
Wei Jiaac428aa2014-09-02 19:01:34 -0700152 mCurrentPositionUs(0),
Andreas Huber3fe62152011-09-16 15:09:22 -0700153 mVideoIsAVC(false),
Wei Jiabc2fb722014-07-08 16:37:57 -0700154 mOffloadAudio(false),
Andy Hung282a7e32014-08-14 15:56:34 -0700155 mCurrentOffloadInfo(AUDIO_INFO_INITIALIZER),
Wei Jia88703c32014-08-06 11:24:07 -0700156 mAudioDecoderGeneration(0),
157 mVideoDecoderGeneration(0),
Wei Jia57568df2014-09-22 10:16:29 -0700158 mRendererGeneration(0),
Andreas Huber9b80c2b2011-06-30 15:47:02 -0700159 mAudioEOS(false),
Andreas Huberf9334412010-12-15 15:17:42 -0800160 mVideoEOS(false),
Andreas Huber5bc087c2010-12-23 10:27:40 -0800161 mScanSourcesPending(false),
Andreas Huber1aef2112011-01-04 14:01:29 -0800162 mScanSourcesGeneration(0),
Andreas Huberb7c8e912012-11-27 15:02:53 -0800163 mPollDurationGeneration(0),
Robert Shihd3b0bbb2014-07-23 15:00:25 -0700164 mTimedTextGeneration(0),
Andreas Huber6e3d3112011-11-28 12:36:11 -0800165 mTimeDiscontinuityPending(false),
Andreas Huberf9334412010-12-15 15:17:42 -0800166 mFlushingAudio(NONE),
Andreas Huber1aef2112011-01-04 14:01:29 -0800167 mFlushingVideo(NONE),
Andreas Huber3fe62152011-09-16 15:09:22 -0700168 mSkipRenderingAudioUntilMediaTimeUs(-1ll),
169 mSkipRenderingVideoUntilMediaTimeUs(-1ll),
170 mVideoLateByUs(0ll),
171 mNumFramesTotal(0ll),
James Dong0d268a32012-08-31 12:18:27 -0700172 mNumFramesDropped(0ll),
Andreas Huber57a339c2012-12-03 11:18:00 -0800173 mVideoScalingMode(NATIVE_WINDOW_SCALING_MODE_SCALE_TO_WINDOW),
174 mStarted(false) {
Andreas Huberf9334412010-12-15 15:17:42 -0800175}
176
177NuPlayer::~NuPlayer() {
178}
179
Andreas Huber9b80c2b2011-06-30 15:47:02 -0700180void NuPlayer::setUID(uid_t uid) {
181 mUIDValid = true;
182 mUID = uid;
183}
184
Andreas Huber43c3e6c2011-01-05 12:17:08 -0800185void NuPlayer::setDriver(const wp<NuPlayerDriver> &driver) {
186 mDriver = driver;
Andreas Huberf9334412010-12-15 15:17:42 -0800187}
188
Andreas Huber9575c962013-02-05 13:59:56 -0800189void NuPlayer::setDataSourceAsync(const sp<IStreamSource> &source) {
Andreas Huberf9334412010-12-15 15:17:42 -0800190 sp<AMessage> msg = new AMessage(kWhatSetDataSource, id());
191
Andreas Huberb5f25f02013-02-05 10:14:26 -0800192 sp<AMessage> notify = new AMessage(kWhatSourceNotify, id());
193
Andreas Huber240abcc2014-02-13 13:32:37 -0800194 msg->setObject("source", new StreamingSource(notify, source));
Andreas Huber5bc087c2010-12-23 10:27:40 -0800195 msg->post();
196}
Andreas Huberf9334412010-12-15 15:17:42 -0800197
Andreas Huberafed0e12011-09-20 15:39:58 -0700198static bool IsHTTPLiveURL(const char *url) {
199 if (!strncasecmp("http://", url, 7)
Andreas Huber99759402013-04-01 14:28:31 -0700200 || !strncasecmp("https://", url, 8)
201 || !strncasecmp("file://", url, 7)) {
Andreas Huberafed0e12011-09-20 15:39:58 -0700202 size_t len = strlen(url);
203 if (len >= 5 && !strcasecmp(".m3u8", &url[len - 5])) {
204 return true;
205 }
206
207 if (strstr(url,"m3u8")) {
208 return true;
209 }
210 }
211
212 return false;
213}
214
Andreas Huber9575c962013-02-05 13:59:56 -0800215void NuPlayer::setDataSourceAsync(
Andreas Huber1b86fe02014-01-29 11:13:26 -0800216 const sp<IMediaHTTPService> &httpService,
217 const char *url,
218 const KeyedVector<String8, String8> *headers) {
Chong Zhang3de157d2014-08-05 20:54:44 -0700219
Andreas Huber5bc087c2010-12-23 10:27:40 -0800220 sp<AMessage> msg = new AMessage(kWhatSetDataSource, id());
Oscar Rydhé7a33b772012-02-20 10:15:48 +0100221 size_t len = strlen(url);
Andreas Huber5bc087c2010-12-23 10:27:40 -0800222
Andreas Huberb5f25f02013-02-05 10:14:26 -0800223 sp<AMessage> notify = new AMessage(kWhatSourceNotify, id());
224
Andreas Huberafed0e12011-09-20 15:39:58 -0700225 sp<Source> source;
226 if (IsHTTPLiveURL(url)) {
Andreas Huber81e68442014-02-05 11:52:33 -0800227 source = new HTTPLiveSource(notify, httpService, url, headers);
Andreas Huberafed0e12011-09-20 15:39:58 -0700228 } else if (!strncasecmp(url, "rtsp://", 7)) {
Andreas Huber1b86fe02014-01-29 11:13:26 -0800229 source = new RTSPSource(
230 notify, httpService, url, headers, mUIDValid, mUID);
Oscar Rydhé7a33b772012-02-20 10:15:48 +0100231 } else if ((!strncasecmp(url, "http://", 7)
232 || !strncasecmp(url, "https://", 8))
233 && ((len >= 4 && !strcasecmp(".sdp", &url[len - 4]))
234 || strstr(url, ".sdp?"))) {
Andreas Huber1b86fe02014-01-29 11:13:26 -0800235 source = new RTSPSource(
236 notify, httpService, url, headers, mUIDValid, mUID, true);
Andreas Huber2bfdd422011-10-11 15:24:07 -0700237 } else {
Chong Zhang3de157d2014-08-05 20:54:44 -0700238 sp<GenericSource> genericSource =
239 new GenericSource(notify, mUIDValid, mUID);
240 // Don't set FLAG_SECURE on mSourceFlags here for widevine.
241 // The correct flags will be updated in Source::kWhatFlagsChanged
242 // handler when GenericSource is prepared.
Andreas Huber2bfdd422011-10-11 15:24:07 -0700243
Chong Zhanga19f33e2014-08-07 15:35:07 -0700244 status_t err = genericSource->setDataSource(httpService, url, headers);
Chong Zhang3de157d2014-08-05 20:54:44 -0700245
246 if (err == OK) {
247 source = genericSource;
248 } else {
Chong Zhanga19f33e2014-08-07 15:35:07 -0700249 ALOGE("Failed to set data source!");
Chong Zhang3de157d2014-08-05 20:54:44 -0700250 }
251 }
Andreas Huberafed0e12011-09-20 15:39:58 -0700252 msg->setObject("source", source);
253 msg->post();
254}
255
Andreas Huber9575c962013-02-05 13:59:56 -0800256void NuPlayer::setDataSourceAsync(int fd, int64_t offset, int64_t length) {
Andreas Huberafed0e12011-09-20 15:39:58 -0700257 sp<AMessage> msg = new AMessage(kWhatSetDataSource, id());
258
Andreas Huberb5f25f02013-02-05 10:14:26 -0800259 sp<AMessage> notify = new AMessage(kWhatSourceNotify, id());
260
Chong Zhang3de157d2014-08-05 20:54:44 -0700261 sp<GenericSource> source =
262 new GenericSource(notify, mUIDValid, mUID);
263
Chong Zhanga19f33e2014-08-07 15:35:07 -0700264 status_t err = source->setDataSource(fd, offset, length);
Chong Zhang3de157d2014-08-05 20:54:44 -0700265
266 if (err != OK) {
Chong Zhanga19f33e2014-08-07 15:35:07 -0700267 ALOGE("Failed to set data source!");
Chong Zhang3de157d2014-08-05 20:54:44 -0700268 source = NULL;
269 }
270
Andreas Huberafed0e12011-09-20 15:39:58 -0700271 msg->setObject("source", source);
Andreas Huberf9334412010-12-15 15:17:42 -0800272 msg->post();
273}
274
Andreas Huber9575c962013-02-05 13:59:56 -0800275void NuPlayer::prepareAsync() {
276 (new AMessage(kWhatPrepare, id()))->post();
277}
278
Andreas Huber57a339c2012-12-03 11:18:00 -0800279void NuPlayer::setVideoSurfaceTextureAsync(
Andy McFadden8ba01022012-12-18 09:46:54 -0800280 const sp<IGraphicBufferProducer> &bufferProducer) {
Glenn Kasten11731182011-02-08 17:26:17 -0800281 sp<AMessage> msg = new AMessage(kWhatSetVideoNativeWindow, id());
Andreas Huber57a339c2012-12-03 11:18:00 -0800282
Andy McFadden8ba01022012-12-18 09:46:54 -0800283 if (bufferProducer == NULL) {
Andreas Huber57a339c2012-12-03 11:18:00 -0800284 msg->setObject("native-window", NULL);
285 } else {
286 msg->setObject(
287 "native-window",
288 new NativeWindowWrapper(
Wei Jia9c03a402014-08-26 15:24:43 -0700289 new Surface(bufferProducer, true /* controlledByApp */)));
Andreas Huber57a339c2012-12-03 11:18:00 -0800290 }
291
Andreas Huberf9334412010-12-15 15:17:42 -0800292 msg->post();
293}
294
295void NuPlayer::setAudioSink(const sp<MediaPlayerBase::AudioSink> &sink) {
296 sp<AMessage> msg = new AMessage(kWhatSetAudioSink, id());
297 msg->setObject("sink", sink);
298 msg->post();
299}
300
301void NuPlayer::start() {
302 (new AMessage(kWhatStart, id()))->post();
303}
304
Andreas Huber43c3e6c2011-01-05 12:17:08 -0800305void NuPlayer::pause() {
Andreas Huberb4082222011-01-20 15:23:04 -0800306 (new AMessage(kWhatPause, id()))->post();
Andreas Huber43c3e6c2011-01-05 12:17:08 -0800307}
308
309void NuPlayer::resume() {
Andreas Huberb4082222011-01-20 15:23:04 -0800310 (new AMessage(kWhatResume, id()))->post();
Andreas Huber43c3e6c2011-01-05 12:17:08 -0800311}
312
Andreas Huber1aef2112011-01-04 14:01:29 -0800313void NuPlayer::resetAsync() {
Chong Zhang48296b72014-09-14 14:28:45 -0700314 if (mSource != NULL) {
315 // During a reset, the data source might be unresponsive already, we need to
316 // disconnect explicitly so that reads exit promptly.
317 // We can't queue the disconnect request to the looper, as it might be
318 // queued behind a stuck read and never gets processed.
319 // Doing a disconnect outside the looper to allows the pending reads to exit
320 // (either successfully or with error).
321 mSource->disconnect();
322 }
323
Andreas Huber1aef2112011-01-04 14:01:29 -0800324 (new AMessage(kWhatReset, id()))->post();
325}
326
Andreas Huber43c3e6c2011-01-05 12:17:08 -0800327void NuPlayer::seekToAsync(int64_t seekTimeUs) {
328 sp<AMessage> msg = new AMessage(kWhatSeek, id());
329 msg->setInt64("seekTimeUs", seekTimeUs);
330 msg->post();
331}
332
Andreas Huber53df1a42010-12-22 10:03:04 -0800333// static
Andreas Huber1aef2112011-01-04 14:01:29 -0800334bool NuPlayer::IsFlushingState(FlushStatus state, bool *needShutdown) {
Andreas Huber53df1a42010-12-22 10:03:04 -0800335 switch (state) {
336 case FLUSHING_DECODER:
Andreas Huber1aef2112011-01-04 14:01:29 -0800337 if (needShutdown != NULL) {
338 *needShutdown = false;
Andreas Huber53df1a42010-12-22 10:03:04 -0800339 }
340 return true;
341
Andreas Huber1aef2112011-01-04 14:01:29 -0800342 case FLUSHING_DECODER_SHUTDOWN:
343 if (needShutdown != NULL) {
344 *needShutdown = true;
Andreas Huber53df1a42010-12-22 10:03:04 -0800345 }
346 return true;
347
348 default:
349 return false;
350 }
351}
352
Chong Zhang404fced2014-06-11 14:45:31 -0700353void NuPlayer::writeTrackInfo(
354 Parcel* reply, const sp<AMessage> format) const {
355 int32_t trackType;
356 CHECK(format->findInt32("type", &trackType));
357
358 AString lang;
359 CHECK(format->findString("language", &lang));
360
361 reply->writeInt32(2); // write something non-zero
362 reply->writeInt32(trackType);
363 reply->writeString16(String16(lang.c_str()));
364
365 if (trackType == MEDIA_TRACK_TYPE_SUBTITLE) {
366 AString mime;
367 CHECK(format->findString("mime", &mime));
368
369 int32_t isAuto, isDefault, isForced;
370 CHECK(format->findInt32("auto", &isAuto));
371 CHECK(format->findInt32("default", &isDefault));
372 CHECK(format->findInt32("forced", &isForced));
373
374 reply->writeString16(String16(mime.c_str()));
375 reply->writeInt32(isAuto);
376 reply->writeInt32(isDefault);
377 reply->writeInt32(isForced);
378 }
379}
380
Andreas Huberf9334412010-12-15 15:17:42 -0800381void NuPlayer::onMessageReceived(const sp<AMessage> &msg) {
382 switch (msg->what()) {
383 case kWhatSetDataSource:
384 {
Steve Block3856b092011-10-20 11:56:00 +0100385 ALOGV("kWhatSetDataSource");
Andreas Huberf9334412010-12-15 15:17:42 -0800386
387 CHECK(mSource == NULL);
388
Chong Zhang3de157d2014-08-05 20:54:44 -0700389 status_t err = OK;
Andreas Huber5bc087c2010-12-23 10:27:40 -0800390 sp<RefBase> obj;
391 CHECK(msg->findObject("source", &obj));
Chong Zhang3de157d2014-08-05 20:54:44 -0700392 if (obj != NULL) {
393 mSource = static_cast<Source *>(obj.get());
Chong Zhang3de157d2014-08-05 20:54:44 -0700394 } else {
395 err = UNKNOWN_ERROR;
396 }
Andreas Huber9575c962013-02-05 13:59:56 -0800397
398 CHECK(mDriver != NULL);
399 sp<NuPlayerDriver> driver = mDriver.promote();
400 if (driver != NULL) {
Chong Zhang3de157d2014-08-05 20:54:44 -0700401 driver->notifySetDataSourceCompleted(err);
Andreas Huber9575c962013-02-05 13:59:56 -0800402 }
403 break;
404 }
405
406 case kWhatPrepare:
407 {
408 mSource->prepareAsync();
Andreas Huberf9334412010-12-15 15:17:42 -0800409 break;
410 }
411
Chong Zhangdcb89b32013-08-06 09:44:47 -0700412 case kWhatGetTrackInfo:
413 {
414 uint32_t replyID;
415 CHECK(msg->senderAwaitsResponse(&replyID));
416
Chong Zhang404fced2014-06-11 14:45:31 -0700417 Parcel* reply;
418 CHECK(msg->findPointer("reply", (void**)&reply));
419
420 size_t inbandTracks = 0;
Chong Zhangdcb89b32013-08-06 09:44:47 -0700421 if (mSource != NULL) {
Chong Zhang404fced2014-06-11 14:45:31 -0700422 inbandTracks = mSource->getTrackCount();
423 }
424
Chong Zhanga7fa1d92014-06-11 14:49:23 -0700425 size_t ccTracks = 0;
426 if (mCCDecoder != NULL) {
427 ccTracks = mCCDecoder->getTrackCount();
428 }
429
Chong Zhang404fced2014-06-11 14:45:31 -0700430 // total track count
Chong Zhanga7fa1d92014-06-11 14:49:23 -0700431 reply->writeInt32(inbandTracks + ccTracks);
Chong Zhang404fced2014-06-11 14:45:31 -0700432
433 // write inband tracks
434 for (size_t i = 0; i < inbandTracks; ++i) {
435 writeTrackInfo(reply, mSource->getTrackInfo(i));
Chong Zhangdcb89b32013-08-06 09:44:47 -0700436 }
437
Chong Zhanga7fa1d92014-06-11 14:49:23 -0700438 // write CC track
439 for (size_t i = 0; i < ccTracks; ++i) {
440 writeTrackInfo(reply, mCCDecoder->getTrackInfo(i));
441 }
442
Chong Zhangdcb89b32013-08-06 09:44:47 -0700443 sp<AMessage> response = new AMessage;
Chong Zhangdcb89b32013-08-06 09:44:47 -0700444 response->postReply(replyID);
445 break;
446 }
447
Robert Shih7c4f0d72014-07-09 18:53:31 -0700448 case kWhatGetSelectedTrack:
449 {
450 status_t err = INVALID_OPERATION;
451 if (mSource != NULL) {
452 err = OK;
453
454 int32_t type32;
455 CHECK(msg->findInt32("type", (int32_t*)&type32));
456 media_track_type type = (media_track_type)type32;
457 ssize_t selectedTrack = mSource->getSelectedTrack(type);
458
459 Parcel* reply;
460 CHECK(msg->findPointer("reply", (void**)&reply));
461 reply->writeInt32(selectedTrack);
462 }
463
464 sp<AMessage> response = new AMessage;
465 response->setInt32("err", err);
466
467 uint32_t replyID;
468 CHECK(msg->senderAwaitsResponse(&replyID));
469 response->postReply(replyID);
470 break;
471 }
472
Chong Zhangdcb89b32013-08-06 09:44:47 -0700473 case kWhatSelectTrack:
474 {
475 uint32_t replyID;
476 CHECK(msg->senderAwaitsResponse(&replyID));
477
Chong Zhang404fced2014-06-11 14:45:31 -0700478 size_t trackIndex;
479 int32_t select;
480 CHECK(msg->findSize("trackIndex", &trackIndex));
481 CHECK(msg->findInt32("select", &select));
482
Chong Zhangdcb89b32013-08-06 09:44:47 -0700483 status_t err = INVALID_OPERATION;
Chong Zhang404fced2014-06-11 14:45:31 -0700484
485 size_t inbandTracks = 0;
Chong Zhangdcb89b32013-08-06 09:44:47 -0700486 if (mSource != NULL) {
Chong Zhang404fced2014-06-11 14:45:31 -0700487 inbandTracks = mSource->getTrackCount();
488 }
Chong Zhanga7fa1d92014-06-11 14:49:23 -0700489 size_t ccTracks = 0;
490 if (mCCDecoder != NULL) {
491 ccTracks = mCCDecoder->getTrackCount();
492 }
Chong Zhang404fced2014-06-11 14:45:31 -0700493
494 if (trackIndex < inbandTracks) {
Chong Zhangdcb89b32013-08-06 09:44:47 -0700495 err = mSource->selectTrack(trackIndex, select);
Robert Shihd3b0bbb2014-07-23 15:00:25 -0700496
497 if (!select && err == OK) {
498 int32_t type;
499 sp<AMessage> info = mSource->getTrackInfo(trackIndex);
500 if (info != NULL
501 && info->findInt32("type", &type)
502 && type == MEDIA_TRACK_TYPE_TIMEDTEXT) {
503 ++mTimedTextGeneration;
504 }
505 }
Chong Zhanga7fa1d92014-06-11 14:49:23 -0700506 } else {
507 trackIndex -= inbandTracks;
508
509 if (trackIndex < ccTracks) {
510 err = mCCDecoder->selectTrack(trackIndex, select);
511 }
Chong Zhangdcb89b32013-08-06 09:44:47 -0700512 }
513
514 sp<AMessage> response = new AMessage;
515 response->setInt32("err", err);
516
517 response->postReply(replyID);
518 break;
519 }
520
Andreas Huberb7c8e912012-11-27 15:02:53 -0800521 case kWhatPollDuration:
522 {
523 int32_t generation;
524 CHECK(msg->findInt32("generation", &generation));
525
526 if (generation != mPollDurationGeneration) {
527 // stale
528 break;
529 }
530
531 int64_t durationUs;
532 if (mDriver != NULL && mSource->getDuration(&durationUs) == OK) {
533 sp<NuPlayerDriver> driver = mDriver.promote();
534 if (driver != NULL) {
535 driver->notifyDuration(durationUs);
536 }
537 }
538
539 msg->post(1000000ll); // poll again in a second.
540 break;
541 }
542
Glenn Kasten11731182011-02-08 17:26:17 -0800543 case kWhatSetVideoNativeWindow:
Andreas Huberf9334412010-12-15 15:17:42 -0800544 {
Steve Block3856b092011-10-20 11:56:00 +0100545 ALOGV("kWhatSetVideoNativeWindow");
Andreas Huberf9334412010-12-15 15:17:42 -0800546
Andreas Huber57a339c2012-12-03 11:18:00 -0800547 mDeferredActions.push_back(
Andreas Huber14f76722013-01-15 09:04:18 -0800548 new ShutdownDecoderAction(
549 false /* audio */, true /* video */));
Andreas Huber57a339c2012-12-03 11:18:00 -0800550
Andreas Huberf9334412010-12-15 15:17:42 -0800551 sp<RefBase> obj;
Glenn Kasten11731182011-02-08 17:26:17 -0800552 CHECK(msg->findObject("native-window", &obj));
Andreas Huberf9334412010-12-15 15:17:42 -0800553
Andreas Huber57a339c2012-12-03 11:18:00 -0800554 mDeferredActions.push_back(
555 new SetSurfaceAction(
556 static_cast<NativeWindowWrapper *>(obj.get())));
James Dong0d268a32012-08-31 12:18:27 -0700557
Andreas Huber57a339c2012-12-03 11:18:00 -0800558 if (obj != NULL) {
Andy Hung73535852014-09-05 11:42:58 -0700559 if (mStarted && mVideoDecoder != NULL) {
560 // Issue a seek to refresh the video screen only if started otherwise
561 // the extractor may not yet be started and will assert.
562 // If the video decoder is not set (perhaps audio only in this case)
563 // do not perform a seek as it is not needed.
564 mDeferredActions.push_back(new SeekAction(mCurrentPositionUs));
565 }
Wei Jiaac428aa2014-09-02 19:01:34 -0700566
Andreas Huber57a339c2012-12-03 11:18:00 -0800567 // If there is a new surface texture, instantiate decoders
568 // again if possible.
569 mDeferredActions.push_back(
570 new SimpleAction(&NuPlayer::performScanSources));
571 }
572
573 processDeferredActions();
Andreas Huberf9334412010-12-15 15:17:42 -0800574 break;
575 }
576
577 case kWhatSetAudioSink:
578 {
Steve Block3856b092011-10-20 11:56:00 +0100579 ALOGV("kWhatSetAudioSink");
Andreas Huberf9334412010-12-15 15:17:42 -0800580
581 sp<RefBase> obj;
582 CHECK(msg->findObject("sink", &obj));
583
584 mAudioSink = static_cast<MediaPlayerBase::AudioSink *>(obj.get());
585 break;
586 }
587
588 case kWhatStart:
589 {
Steve Block3856b092011-10-20 11:56:00 +0100590 ALOGV("kWhatStart");
Andreas Huber43c3e6c2011-01-05 12:17:08 -0800591
Andreas Huber3fe62152011-09-16 15:09:22 -0700592 mVideoIsAVC = false;
Wei Jiabc2fb722014-07-08 16:37:57 -0700593 mOffloadAudio = false;
Andreas Huber1aef2112011-01-04 14:01:29 -0800594 mAudioEOS = false;
595 mVideoEOS = false;
Andreas Huber32f3cef2011-03-02 15:34:46 -0800596 mSkipRenderingAudioUntilMediaTimeUs = -1;
597 mSkipRenderingVideoUntilMediaTimeUs = -1;
Andreas Huber3fe62152011-09-16 15:09:22 -0700598 mVideoLateByUs = 0;
599 mNumFramesTotal = 0;
600 mNumFramesDropped = 0;
Andreas Huber57a339c2012-12-03 11:18:00 -0800601 mStarted = true;
Andreas Huber1aef2112011-01-04 14:01:29 -0800602
Lajos Molnar09524832014-07-17 14:29:51 -0700603 /* instantiate decoders now for secure playback */
604 if (mSourceFlags & Source::FLAG_SECURE) {
605 if (mNativeWindow != NULL) {
606 instantiateDecoder(false, &mVideoDecoder);
607 }
608
609 if (mAudioSink != NULL) {
610 instantiateDecoder(true, &mAudioDecoder);
611 }
612 }
613
Andreas Huber5bc087c2010-12-23 10:27:40 -0800614 mSource->start();
Andreas Huberf9334412010-12-15 15:17:42 -0800615
Andreas Huberd5e56232013-03-12 11:01:43 -0700616 uint32_t flags = 0;
617
618 if (mSource->isRealTime()) {
619 flags |= Renderer::FLAG_REAL_TIME;
620 }
621
Wei Jiabc2fb722014-07-08 16:37:57 -0700622 sp<MetaData> audioMeta = mSource->getFormatMeta(true /* audio */);
623 audio_stream_type_t streamType = AUDIO_STREAM_MUSIC;
624 if (mAudioSink != NULL) {
625 streamType = mAudioSink->getAudioStreamType();
626 }
627
628 sp<AMessage> videoFormat = mSource->getFormat(false /* audio */);
629
630 mOffloadAudio =
631 canOffloadStream(audioMeta, (videoFormat != NULL),
632 true /* is_streaming */, streamType);
633 if (mOffloadAudio) {
634 flags |= Renderer::FLAG_OFFLOAD_AUDIO;
635 }
636
Wei Jia57568df2014-09-22 10:16:29 -0700637 sp<AMessage> notify = new AMessage(kWhatRendererNotify, id());
638 ++mRendererGeneration;
639 notify->setInt32("generation", mRendererGeneration);
640 mRenderer = new Renderer(mAudioSink, notify, flags);
Andreas Huberf9334412010-12-15 15:17:42 -0800641
Lajos Molnar09524832014-07-17 14:29:51 -0700642 mRendererLooper = new ALooper;
643 mRendererLooper->setName("NuPlayerRenderer");
644 mRendererLooper->start(false, false, ANDROID_PRIORITY_AUDIO);
645 mRendererLooper->registerHandler(mRenderer);
Andreas Huberf9334412010-12-15 15:17:42 -0800646
Lajos Molnarc851b5d2014-09-18 14:14:29 -0700647 sp<MetaData> meta = getFileMeta();
648 int32_t rate;
649 if (meta != NULL
650 && meta->findInt32(kKeyFrameRate, &rate) && rate > 0) {
651 mRenderer->setVideoFrameRate(rate);
652 }
653
Andreas Huber1aef2112011-01-04 14:01:29 -0800654 postScanSources();
Andreas Huberf9334412010-12-15 15:17:42 -0800655 break;
656 }
657
658 case kWhatScanSources:
659 {
Andreas Huber1aef2112011-01-04 14:01:29 -0800660 int32_t generation;
661 CHECK(msg->findInt32("generation", &generation));
662 if (generation != mScanSourcesGeneration) {
663 // Drop obsolete msg.
664 break;
665 }
666
Andreas Huber5bc087c2010-12-23 10:27:40 -0800667 mScanSourcesPending = false;
668
Steve Block3856b092011-10-20 11:56:00 +0100669 ALOGV("scanning sources haveAudio=%d, haveVideo=%d",
Andreas Huber43c3e6c2011-01-05 12:17:08 -0800670 mAudioDecoder != NULL, mVideoDecoder != NULL);
671
Andreas Huberb7c8e912012-11-27 15:02:53 -0800672 bool mHadAnySourcesBefore =
673 (mAudioDecoder != NULL) || (mVideoDecoder != NULL);
674
Andy Hung282a7e32014-08-14 15:56:34 -0700675 // initialize video before audio because successful initialization of
676 // video may change deep buffer mode of audio.
Haynes Mathew George5d246ef2012-07-09 10:36:57 -0700677 if (mNativeWindow != NULL) {
678 instantiateDecoder(false, &mVideoDecoder);
679 }
Andreas Huberf9334412010-12-15 15:17:42 -0800680
681 if (mAudioSink != NULL) {
Andy Hung282a7e32014-08-14 15:56:34 -0700682 if (mOffloadAudio) {
683 // open audio sink early under offload mode.
684 sp<AMessage> format = mSource->getFormat(true /*audio*/);
685 openAudioSink(format, true /*offloadOnly*/);
686 }
Andreas Huber5bc087c2010-12-23 10:27:40 -0800687 instantiateDecoder(true, &mAudioDecoder);
Andreas Huberf9334412010-12-15 15:17:42 -0800688 }
689
Andreas Huberb7c8e912012-11-27 15:02:53 -0800690 if (!mHadAnySourcesBefore
691 && (mAudioDecoder != NULL || mVideoDecoder != NULL)) {
692 // This is the first time we've found anything playable.
693
Andreas Huber9575c962013-02-05 13:59:56 -0800694 if (mSourceFlags & Source::FLAG_DYNAMIC_DURATION) {
Andreas Huberb7c8e912012-11-27 15:02:53 -0800695 schedulePollDuration();
696 }
697 }
698
Andreas Hubereac68ba2011-09-27 12:12:25 -0700699 status_t err;
700 if ((err = mSource->feedMoreTSData()) != OK) {
Andreas Huber1aef2112011-01-04 14:01:29 -0800701 if (mAudioDecoder == NULL && mVideoDecoder == NULL) {
702 // We're not currently decoding anything (no audio or
703 // video tracks found) and we just ran out of input data.
Andreas Hubereac68ba2011-09-27 12:12:25 -0700704
705 if (err == ERROR_END_OF_STREAM) {
706 notifyListener(MEDIA_PLAYBACK_COMPLETE, 0, 0);
707 } else {
708 notifyListener(MEDIA_ERROR, MEDIA_ERROR_UNKNOWN, err);
709 }
Andreas Huber1aef2112011-01-04 14:01:29 -0800710 }
Andreas Huberf9334412010-12-15 15:17:42 -0800711 break;
712 }
713
Andreas Huberfbe9d812012-08-31 14:05:27 -0700714 if ((mAudioDecoder == NULL && mAudioSink != NULL)
715 || (mVideoDecoder == NULL && mNativeWindow != NULL)) {
Andreas Huberf9334412010-12-15 15:17:42 -0800716 msg->post(100000ll);
Andreas Huber5bc087c2010-12-23 10:27:40 -0800717 mScanSourcesPending = true;
Andreas Huberf9334412010-12-15 15:17:42 -0800718 }
719 break;
720 }
721
722 case kWhatVideoNotify:
723 case kWhatAudioNotify:
724 {
725 bool audio = msg->what() == kWhatAudioNotify;
726
Wei Jia88703c32014-08-06 11:24:07 -0700727 int32_t currentDecoderGeneration =
728 (audio? mAudioDecoderGeneration : mVideoDecoderGeneration);
729 int32_t requesterGeneration = currentDecoderGeneration - 1;
730 CHECK(msg->findInt32("generation", &requesterGeneration));
731
732 if (requesterGeneration != currentDecoderGeneration) {
733 ALOGV("got message from old %s decoder, generation(%d:%d)",
734 audio ? "audio" : "video", requesterGeneration,
735 currentDecoderGeneration);
736 sp<AMessage> reply;
737 if (!(msg->findMessage("reply", &reply))) {
738 return;
739 }
740
741 reply->setInt32("err", INFO_DISCONTINUITY);
742 reply->post();
743 return;
744 }
745
Andreas Huberf9334412010-12-15 15:17:42 -0800746 int32_t what;
Lajos Molnar1cd13982014-01-17 15:12:51 -0800747 CHECK(msg->findInt32("what", &what));
Andreas Huberf9334412010-12-15 15:17:42 -0800748
Lajos Molnar1cd13982014-01-17 15:12:51 -0800749 if (what == Decoder::kWhatFillThisBuffer) {
Andreas Huberf9334412010-12-15 15:17:42 -0800750 status_t err = feedDecoderInputData(
Lajos Molnar1cd13982014-01-17 15:12:51 -0800751 audio, msg);
Andreas Huberf9334412010-12-15 15:17:42 -0800752
Andreas Huber5bc087c2010-12-23 10:27:40 -0800753 if (err == -EWOULDBLOCK) {
Andreas Hubereac68ba2011-09-27 12:12:25 -0700754 if (mSource->feedMoreTSData() == OK) {
Phil Burkc5cc2e22014-09-09 20:08:39 -0700755 msg->post(10 * 1000ll);
Andreas Huber5bc087c2010-12-23 10:27:40 -0800756 }
Andreas Huberf9334412010-12-15 15:17:42 -0800757 }
Lajos Molnar1cd13982014-01-17 15:12:51 -0800758 } else if (what == Decoder::kWhatEOS) {
Andreas Huberdc9bacd2011-09-26 10:53:29 -0700759 int32_t err;
Lajos Molnar1cd13982014-01-17 15:12:51 -0800760 CHECK(msg->findInt32("err", &err));
Andreas Huberdc9bacd2011-09-26 10:53:29 -0700761
762 if (err == ERROR_END_OF_STREAM) {
Steve Block3856b092011-10-20 11:56:00 +0100763 ALOGV("got %s decoder EOS", audio ? "audio" : "video");
Andreas Huberdc9bacd2011-09-26 10:53:29 -0700764 } else {
Steve Block3856b092011-10-20 11:56:00 +0100765 ALOGV("got %s decoder EOS w/ error %d",
Andreas Huberdc9bacd2011-09-26 10:53:29 -0700766 audio ? "audio" : "video",
767 err);
768 }
769
770 mRenderer->queueEOS(audio, err);
Lajos Molnar1cd13982014-01-17 15:12:51 -0800771 } else if (what == Decoder::kWhatFlushCompleted) {
Andreas Huber1aef2112011-01-04 14:01:29 -0800772 bool needShutdown;
Andreas Huber53df1a42010-12-22 10:03:04 -0800773
Andreas Huberf9334412010-12-15 15:17:42 -0800774 if (audio) {
Andreas Huber1aef2112011-01-04 14:01:29 -0800775 CHECK(IsFlushingState(mFlushingAudio, &needShutdown));
Andreas Huberf9334412010-12-15 15:17:42 -0800776 mFlushingAudio = FLUSHED;
777 } else {
Andreas Huber1aef2112011-01-04 14:01:29 -0800778 CHECK(IsFlushingState(mFlushingVideo, &needShutdown));
Andreas Huberf9334412010-12-15 15:17:42 -0800779 mFlushingVideo = FLUSHED;
Andreas Huber3fe62152011-09-16 15:09:22 -0700780
781 mVideoLateByUs = 0;
Andreas Huberf9334412010-12-15 15:17:42 -0800782 }
783
Steve Block3856b092011-10-20 11:56:00 +0100784 ALOGV("decoder %s flush completed", audio ? "audio" : "video");
Andreas Huberf9334412010-12-15 15:17:42 -0800785
Andreas Huber1aef2112011-01-04 14:01:29 -0800786 if (needShutdown) {
Steve Block3856b092011-10-20 11:56:00 +0100787 ALOGV("initiating %s decoder shutdown",
Andreas Huber53df1a42010-12-22 10:03:04 -0800788 audio ? "audio" : "video");
Andreas Huberf9334412010-12-15 15:17:42 -0800789
Lajos Molnar87603c02014-08-20 19:25:30 -0700790 getDecoder(audio)->initiateShutdown();
Andreas Huberf9334412010-12-15 15:17:42 -0800791
Andreas Huber53df1a42010-12-22 10:03:04 -0800792 if (audio) {
793 mFlushingAudio = SHUTTING_DOWN_DECODER;
794 } else {
795 mFlushingVideo = SHUTTING_DOWN_DECODER;
796 }
Andreas Huberf9334412010-12-15 15:17:42 -0800797 }
Andreas Huber3831a062010-12-21 10:22:33 -0800798
799 finishFlushIfPossible();
Lajos Molnar1cd13982014-01-17 15:12:51 -0800800 } else if (what == Decoder::kWhatOutputFormatChanged) {
801 sp<AMessage> format;
802 CHECK(msg->findMessage("format", &format));
803
Andreas Huber31e25082011-01-10 10:38:31 -0800804 if (audio) {
Andy Hung282a7e32014-08-14 15:56:34 -0700805 openAudioSink(format, false /*offloadOnly*/);
Andreas Huber31e25082011-01-10 10:38:31 -0800806 } else {
807 // video
Chong Zhangced1c2f2014-08-08 15:22:35 -0700808 sp<AMessage> inputFormat =
809 mSource->getFormat(false /* audio */);
Andreas Huber3831a062010-12-21 10:22:33 -0800810
Chong Zhangced1c2f2014-08-08 15:22:35 -0700811 updateVideoSize(inputFormat, format);
Andreas Huber31e25082011-01-10 10:38:31 -0800812 }
Lajos Molnar1cd13982014-01-17 15:12:51 -0800813 } else if (what == Decoder::kWhatShutdownCompleted) {
Steve Block3856b092011-10-20 11:56:00 +0100814 ALOGV("%s shutdown completed", audio ? "audio" : "video");
Andreas Huber3831a062010-12-21 10:22:33 -0800815 if (audio) {
816 mAudioDecoder.clear();
Wei Jia57568df2014-09-22 10:16:29 -0700817 ++mAudioDecoderGeneration;
Andreas Huber3831a062010-12-21 10:22:33 -0800818
819 CHECK_EQ((int)mFlushingAudio, (int)SHUTTING_DOWN_DECODER);
820 mFlushingAudio = SHUT_DOWN;
821 } else {
822 mVideoDecoder.clear();
Wei Jia57568df2014-09-22 10:16:29 -0700823 ++mVideoDecoderGeneration;
Andreas Huber3831a062010-12-21 10:22:33 -0800824
825 CHECK_EQ((int)mFlushingVideo, (int)SHUTTING_DOWN_DECODER);
826 mFlushingVideo = SHUT_DOWN;
827 }
828
829 finishFlushIfPossible();
Lajos Molnar1cd13982014-01-17 15:12:51 -0800830 } else if (what == Decoder::kWhatError) {
Steve Block29357bc2012-01-06 19:20:56 +0000831 ALOGE("Received error from %s decoder, aborting playback.",
Andreas Huberc92fd242011-08-16 13:48:44 -0700832 audio ? "audio" : "video");
833
Chong Zhangf4c0a942014-08-11 15:14:10 -0700834 status_t err;
835 if (!msg->findInt32("err", &err)) {
836 err = UNKNOWN_ERROR;
837 }
838 mRenderer->queueEOS(audio, err);
Marco Nelissen9e2b7912014-08-18 16:13:03 -0700839 if (audio && mFlushingAudio != NONE) {
840 mAudioDecoder.clear();
Wei Jia57568df2014-09-22 10:16:29 -0700841 ++mAudioDecoderGeneration;
Marco Nelissen9e2b7912014-08-18 16:13:03 -0700842 mFlushingAudio = SHUT_DOWN;
843 } else if (!audio && mFlushingVideo != NONE){
844 mVideoDecoder.clear();
Wei Jia57568df2014-09-22 10:16:29 -0700845 ++mVideoDecoderGeneration;
Marco Nelissen9e2b7912014-08-18 16:13:03 -0700846 mFlushingVideo = SHUT_DOWN;
847 }
848 finishFlushIfPossible();
Lajos Molnar1cd13982014-01-17 15:12:51 -0800849 } else if (what == Decoder::kWhatDrainThisBuffer) {
850 renderBuffer(audio, msg);
851 } else {
852 ALOGV("Unhandled decoder notification %d '%c%c%c%c'.",
Andreas Hubera1f8ab02012-11-30 10:53:22 -0800853 what,
854 what >> 24,
855 (what >> 16) & 0xff,
856 (what >> 8) & 0xff,
857 what & 0xff);
Andreas Huberf9334412010-12-15 15:17:42 -0800858 }
859
860 break;
861 }
862
863 case kWhatRendererNotify:
864 {
Wei Jia57568df2014-09-22 10:16:29 -0700865 int32_t requesterGeneration = mRendererGeneration - 1;
866 CHECK(msg->findInt32("generation", &requesterGeneration));
867 if (requesterGeneration != mRendererGeneration) {
868 ALOGV("got message from old renderer, generation(%d:%d)",
869 requesterGeneration, mRendererGeneration);
870 return;
871 }
872
Andreas Huberf9334412010-12-15 15:17:42 -0800873 int32_t what;
874 CHECK(msg->findInt32("what", &what));
875
876 if (what == Renderer::kWhatEOS) {
877 int32_t audio;
878 CHECK(msg->findInt32("audio", &audio));
879
Andreas Huberc92fd242011-08-16 13:48:44 -0700880 int32_t finalResult;
881 CHECK(msg->findInt32("finalResult", &finalResult));
882
Andreas Huberf9334412010-12-15 15:17:42 -0800883 if (audio) {
884 mAudioEOS = true;
885 } else {
886 mVideoEOS = true;
887 }
888
Andreas Huberc92fd242011-08-16 13:48:44 -0700889 if (finalResult == ERROR_END_OF_STREAM) {
Steve Block3856b092011-10-20 11:56:00 +0100890 ALOGV("reached %s EOS", audio ? "audio" : "video");
Andreas Huberc92fd242011-08-16 13:48:44 -0700891 } else {
Steve Block29357bc2012-01-06 19:20:56 +0000892 ALOGE("%s track encountered an error (%d)",
Andreas Huberc92fd242011-08-16 13:48:44 -0700893 audio ? "audio" : "video", finalResult);
894
895 notifyListener(
896 MEDIA_ERROR, MEDIA_ERROR_UNKNOWN, finalResult);
897 }
Andreas Huberf9334412010-12-15 15:17:42 -0800898
899 if ((mAudioEOS || mAudioDecoder == NULL)
900 && (mVideoEOS || mVideoDecoder == NULL)) {
901 notifyListener(MEDIA_PLAYBACK_COMPLETE, 0, 0);
902 }
Andreas Huber43c3e6c2011-01-05 12:17:08 -0800903 } else if (what == Renderer::kWhatPosition) {
904 int64_t positionUs;
905 CHECK(msg->findInt64("positionUs", &positionUs));
Wei Jiaac428aa2014-09-02 19:01:34 -0700906 mCurrentPositionUs = positionUs;
Andreas Huber43c3e6c2011-01-05 12:17:08 -0800907
Andreas Huber3fe62152011-09-16 15:09:22 -0700908 CHECK(msg->findInt64("videoLateByUs", &mVideoLateByUs));
909
Andreas Huber43c3e6c2011-01-05 12:17:08 -0800910 if (mDriver != NULL) {
911 sp<NuPlayerDriver> driver = mDriver.promote();
912 if (driver != NULL) {
913 driver->notifyPosition(positionUs);
Andreas Huber3fe62152011-09-16 15:09:22 -0700914
915 driver->notifyFrameStats(
916 mNumFramesTotal, mNumFramesDropped);
Andreas Huber43c3e6c2011-01-05 12:17:08 -0800917 }
918 }
Andreas Huber3fe62152011-09-16 15:09:22 -0700919 } else if (what == Renderer::kWhatFlushComplete) {
Andreas Huberf9334412010-12-15 15:17:42 -0800920 int32_t audio;
921 CHECK(msg->findInt32("audio", &audio));
922
Steve Block3856b092011-10-20 11:56:00 +0100923 ALOGV("renderer %s flush completed.", audio ? "audio" : "video");
James Dongf57b4ea2012-07-20 13:38:36 -0700924 } else if (what == Renderer::kWhatVideoRenderingStart) {
925 notifyListener(MEDIA_INFO, MEDIA_INFO_RENDERING_START, 0);
Lajos Molnarcbaffcf2013-08-14 18:30:38 -0700926 } else if (what == Renderer::kWhatMediaRenderingStart) {
927 ALOGV("media rendering started");
928 notifyListener(MEDIA_STARTED, 0, 0);
Wei Jia3a2956d2014-07-22 16:01:33 -0700929 } else if (what == Renderer::kWhatAudioOffloadTearDown) {
930 ALOGV("Tear down audio offload, fall back to s/w path");
931 int64_t positionUs;
932 CHECK(msg->findInt64("positionUs", &positionUs));
Andy Hung282a7e32014-08-14 15:56:34 -0700933 closeAudioSink();
Wei Jia3a2956d2014-07-22 16:01:33 -0700934 mAudioDecoder.clear();
Wei Jia57568df2014-09-22 10:16:29 -0700935 ++mAudioDecoderGeneration;
Wei Jia3a2956d2014-07-22 16:01:33 -0700936 mRenderer->flush(true /* audio */);
937 if (mVideoDecoder != NULL) {
938 mRenderer->flush(false /* audio */);
939 }
940 mRenderer->signalDisableOffloadAudio();
941 mOffloadAudio = false;
942
943 performSeek(positionUs);
944 instantiateDecoder(true /* audio */, &mAudioDecoder);
Andreas Huberf9334412010-12-15 15:17:42 -0800945 }
946 break;
947 }
948
949 case kWhatMoreDataQueued:
950 {
951 break;
952 }
953
Andreas Huber1aef2112011-01-04 14:01:29 -0800954 case kWhatReset:
955 {
Steve Block3856b092011-10-20 11:56:00 +0100956 ALOGV("kWhatReset");
Andreas Huber1aef2112011-01-04 14:01:29 -0800957
Andreas Hubera1f8ab02012-11-30 10:53:22 -0800958 mDeferredActions.push_back(
Andreas Huber14f76722013-01-15 09:04:18 -0800959 new ShutdownDecoderAction(
960 true /* audio */, true /* video */));
Andreas Huberb7c8e912012-11-27 15:02:53 -0800961
Andreas Hubera1f8ab02012-11-30 10:53:22 -0800962 mDeferredActions.push_back(
963 new SimpleAction(&NuPlayer::performReset));
Andreas Huberb58ce9f2011-11-28 16:27:35 -0800964
Andreas Hubera1f8ab02012-11-30 10:53:22 -0800965 processDeferredActions();
Andreas Huber1aef2112011-01-04 14:01:29 -0800966 break;
967 }
968
Andreas Huber43c3e6c2011-01-05 12:17:08 -0800969 case kWhatSeek:
970 {
971 int64_t seekTimeUs;
972 CHECK(msg->findInt64("seekTimeUs", &seekTimeUs));
973
Andreas Hubera1f8ab02012-11-30 10:53:22 -0800974 ALOGV("kWhatSeek seekTimeUs=%lld us", seekTimeUs);
Andreas Huber43c3e6c2011-01-05 12:17:08 -0800975
Andreas Hubera1f8ab02012-11-30 10:53:22 -0800976 mDeferredActions.push_back(
977 new SimpleAction(&NuPlayer::performDecoderFlush));
Andreas Huber43c3e6c2011-01-05 12:17:08 -0800978
Andreas Hubera1f8ab02012-11-30 10:53:22 -0800979 mDeferredActions.push_back(new SeekAction(seekTimeUs));
Andreas Huber43c3e6c2011-01-05 12:17:08 -0800980
Andreas Hubera1f8ab02012-11-30 10:53:22 -0800981 processDeferredActions();
Andreas Huber43c3e6c2011-01-05 12:17:08 -0800982 break;
983 }
984
Andreas Huberb4082222011-01-20 15:23:04 -0800985 case kWhatPause:
986 {
Wei Jia7e9f7f72014-09-23 10:55:35 -0700987 if (mSource != NULL) {
988 mSource->pause();
989 } else {
990 ALOGW("pause called when source is gone or not set");
991 }
992 if (mRenderer != NULL) {
993 mRenderer->pause();
994 } else {
995 ALOGW("pause called when renderer is gone or not set");
996 }
Andreas Huberb4082222011-01-20 15:23:04 -0800997 break;
998 }
999
1000 case kWhatResume:
1001 {
Wei Jia7e9f7f72014-09-23 10:55:35 -07001002 if (mSource != NULL) {
1003 mSource->resume();
1004 } else {
1005 ALOGW("resume called when source is gone or not set");
1006 }
1007 if (mRenderer != NULL) {
1008 mRenderer->resume();
1009 } else {
1010 ALOGW("resume called when renderer is gone or not set");
1011 }
Andreas Huberb4082222011-01-20 15:23:04 -08001012 break;
1013 }
1014
Andreas Huberb5f25f02013-02-05 10:14:26 -08001015 case kWhatSourceNotify:
1016 {
Andreas Huber9575c962013-02-05 13:59:56 -08001017 onSourceNotify(msg);
Andreas Huberb5f25f02013-02-05 10:14:26 -08001018 break;
1019 }
1020
Chong Zhanga7fa1d92014-06-11 14:49:23 -07001021 case kWhatClosedCaptionNotify:
1022 {
1023 onClosedCaptionNotify(msg);
1024 break;
1025 }
1026
Andreas Huberf9334412010-12-15 15:17:42 -08001027 default:
1028 TRESPASS();
1029 break;
1030 }
1031}
1032
Andreas Huber3831a062010-12-21 10:22:33 -08001033void NuPlayer::finishFlushIfPossible() {
Wei Jia53904f32014-07-29 10:22:53 -07001034 if (mFlushingAudio != NONE && mFlushingAudio != FLUSHED
1035 && mFlushingAudio != SHUT_DOWN) {
Andreas Huber3831a062010-12-21 10:22:33 -08001036 return;
1037 }
1038
Wei Jia53904f32014-07-29 10:22:53 -07001039 if (mFlushingVideo != NONE && mFlushingVideo != FLUSHED
1040 && mFlushingVideo != SHUT_DOWN) {
Andreas Huber3831a062010-12-21 10:22:33 -08001041 return;
1042 }
1043
Steve Block3856b092011-10-20 11:56:00 +01001044 ALOGV("both audio and video are flushed now.");
Andreas Huber3831a062010-12-21 10:22:33 -08001045
Phil Burk9f526492014-09-03 15:04:12 -07001046 mPendingAudioAccessUnit.clear();
Phil Burkc5cc2e22014-09-09 20:08:39 -07001047 mAggregateBuffer.clear();
Phil Burk9f526492014-09-03 15:04:12 -07001048
Andreas Huber6e3d3112011-11-28 12:36:11 -08001049 if (mTimeDiscontinuityPending) {
1050 mRenderer->signalTimeDiscontinuity();
1051 mTimeDiscontinuityPending = false;
1052 }
Andreas Huber3831a062010-12-21 10:22:33 -08001053
Wei Jia53904f32014-07-29 10:22:53 -07001054 if (mAudioDecoder != NULL && mFlushingAudio == FLUSHED) {
Andreas Huber3831a062010-12-21 10:22:33 -08001055 mAudioDecoder->signalResume();
1056 }
1057
Wei Jia53904f32014-07-29 10:22:53 -07001058 if (mVideoDecoder != NULL && mFlushingVideo == FLUSHED) {
Andreas Huber3831a062010-12-21 10:22:33 -08001059 mVideoDecoder->signalResume();
1060 }
1061
1062 mFlushingAudio = NONE;
1063 mFlushingVideo = NONE;
Andreas Huber3831a062010-12-21 10:22:33 -08001064
Andreas Hubera1f8ab02012-11-30 10:53:22 -08001065 processDeferredActions();
Andreas Huber1aef2112011-01-04 14:01:29 -08001066}
1067
1068void NuPlayer::postScanSources() {
1069 if (mScanSourcesPending) {
1070 return;
1071 }
1072
1073 sp<AMessage> msg = new AMessage(kWhatScanSources, id());
1074 msg->setInt32("generation", mScanSourcesGeneration);
1075 msg->post();
1076
1077 mScanSourcesPending = true;
1078}
1079
Andy Hung282a7e32014-08-14 15:56:34 -07001080void NuPlayer::openAudioSink(const sp<AMessage> &format, bool offloadOnly) {
1081 ALOGV("openAudioSink: offloadOnly(%d) mOffloadAudio(%d)",
1082 offloadOnly, mOffloadAudio);
1083 bool audioSinkChanged = false;
1084
1085 int32_t numChannels;
1086 CHECK(format->findInt32("channel-count", &numChannels));
1087
1088 int32_t channelMask;
1089 if (!format->findInt32("channel-mask", &channelMask)) {
1090 // signal to the AudioSink to derive the mask from count.
1091 channelMask = CHANNEL_MASK_USE_CHANNEL_ORDER;
1092 }
1093
1094 int32_t sampleRate;
1095 CHECK(format->findInt32("sample-rate", &sampleRate));
1096
1097 uint32_t flags;
1098 int64_t durationUs;
1099 // FIXME: we should handle the case where the video decoder
1100 // is created after we receive the format change indication.
1101 // Current code will just make that we select deep buffer
1102 // with video which should not be a problem as it should
1103 // not prevent from keeping A/V sync.
1104 if (mVideoDecoder == NULL &&
1105 mSource->getDuration(&durationUs) == OK &&
1106 durationUs
1107 > AUDIO_SINK_MIN_DEEP_BUFFER_DURATION_US) {
1108 flags = AUDIO_OUTPUT_FLAG_DEEP_BUFFER;
1109 } else {
1110 flags = AUDIO_OUTPUT_FLAG_NONE;
1111 }
1112
1113 if (mOffloadAudio) {
1114 audio_format_t audioFormat = AUDIO_FORMAT_PCM_16_BIT;
1115 AString mime;
1116 CHECK(format->findString("mime", &mime));
1117 status_t err = mapMimeToAudioFormat(audioFormat, mime.c_str());
1118
1119 if (err != OK) {
1120 ALOGE("Couldn't map mime \"%s\" to a valid "
1121 "audio_format", mime.c_str());
1122 mOffloadAudio = false;
1123 } else {
1124 ALOGV("Mime \"%s\" mapped to audio_format 0x%x",
1125 mime.c_str(), audioFormat);
1126
1127 int avgBitRate = -1;
1128 format->findInt32("bit-rate", &avgBitRate);
1129
1130 int32_t aacProfile = -1;
1131 if (audioFormat == AUDIO_FORMAT_AAC
1132 && format->findInt32("aac-profile", &aacProfile)) {
1133 // Redefine AAC format as per aac profile
1134 mapAACProfileToAudioFormat(
1135 audioFormat,
1136 aacProfile);
1137 }
1138
1139 audio_offload_info_t offloadInfo = AUDIO_INFO_INITIALIZER;
1140 offloadInfo.duration_us = -1;
1141 format->findInt64(
1142 "durationUs", &offloadInfo.duration_us);
1143 offloadInfo.sample_rate = sampleRate;
1144 offloadInfo.channel_mask = channelMask;
1145 offloadInfo.format = audioFormat;
1146 offloadInfo.stream_type = AUDIO_STREAM_MUSIC;
1147 offloadInfo.bit_rate = avgBitRate;
1148 offloadInfo.has_video = (mVideoDecoder != NULL);
1149 offloadInfo.is_streaming = true;
1150
1151 if (memcmp(&mCurrentOffloadInfo, &offloadInfo, sizeof(offloadInfo)) == 0) {
1152 ALOGV("openAudioSink: no change in offload mode");
1153 return; // no change from previous configuration, everything ok.
1154 }
1155 ALOGV("openAudioSink: try to open AudioSink in offload mode");
1156 flags |= AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD;
Ronghua Wu1ffb5382014-08-18 15:57:03 -07001157 flags &= ~AUDIO_OUTPUT_FLAG_DEEP_BUFFER;
Andy Hung282a7e32014-08-14 15:56:34 -07001158 audioSinkChanged = true;
1159 mAudioSink->close();
1160 err = mAudioSink->open(
1161 sampleRate,
1162 numChannels,
1163 (audio_channel_mask_t)channelMask,
1164 audioFormat,
1165 8 /* bufferCount */,
1166 &NuPlayer::Renderer::AudioSinkCallback,
1167 mRenderer.get(),
1168 (audio_output_flags_t)flags,
1169 &offloadInfo);
1170
1171 if (err == OK) {
1172 // If the playback is offloaded to h/w, we pass
1173 // the HAL some metadata information.
1174 // We don't want to do this for PCM because it
1175 // will be going through the AudioFlinger mixer
1176 // before reaching the hardware.
1177 sp<MetaData> audioMeta =
1178 mSource->getFormatMeta(true /* audio */);
1179 sendMetaDataToHal(mAudioSink, audioMeta);
1180 mCurrentOffloadInfo = offloadInfo;
1181 err = mAudioSink->start();
1182 ALOGV_IF(err == OK, "openAudioSink: offload succeeded");
1183 }
1184 if (err != OK) {
1185 // Clean up, fall back to non offload mode.
1186 mAudioSink->close();
1187 mRenderer->signalDisableOffloadAudio();
1188 mOffloadAudio = false;
1189 mCurrentOffloadInfo = AUDIO_INFO_INITIALIZER;
1190 ALOGV("openAudioSink: offload failed");
1191 }
1192 }
1193 }
1194 if (!offloadOnly && !mOffloadAudio) {
1195 flags &= ~AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD;
1196 ALOGV("openAudioSink: open AudioSink in NON-offload mode");
1197
1198 audioSinkChanged = true;
1199 mAudioSink->close();
1200 mCurrentOffloadInfo = AUDIO_INFO_INITIALIZER;
1201 CHECK_EQ(mAudioSink->open(
1202 sampleRate,
1203 numChannels,
1204 (audio_channel_mask_t)channelMask,
1205 AUDIO_FORMAT_PCM_16_BIT,
1206 8 /* bufferCount */,
1207 NULL,
1208 NULL,
1209 (audio_output_flags_t)flags),
1210 (status_t)OK);
1211 mAudioSink->start();
1212 }
1213 if (audioSinkChanged) {
1214 mRenderer->signalAudioSinkChanged();
1215 }
1216}
1217
1218void NuPlayer::closeAudioSink() {
1219 mAudioSink->close();
1220 mCurrentOffloadInfo = AUDIO_INFO_INITIALIZER;
1221}
1222
Andreas Huber5bc087c2010-12-23 10:27:40 -08001223status_t NuPlayer::instantiateDecoder(bool audio, sp<Decoder> *decoder) {
Andreas Huberf9334412010-12-15 15:17:42 -08001224 if (*decoder != NULL) {
1225 return OK;
1226 }
1227
Andreas Huber84066782011-08-16 09:34:26 -07001228 sp<AMessage> format = mSource->getFormat(audio);
Andreas Huberf9334412010-12-15 15:17:42 -08001229
Andreas Huber84066782011-08-16 09:34:26 -07001230 if (format == NULL) {
Andreas Huberf9334412010-12-15 15:17:42 -08001231 return -EWOULDBLOCK;
1232 }
1233
Andreas Huber3fe62152011-09-16 15:09:22 -07001234 if (!audio) {
Andreas Huber84066782011-08-16 09:34:26 -07001235 AString mime;
1236 CHECK(format->findString("mime", &mime));
1237 mVideoIsAVC = !strcasecmp(MEDIA_MIMETYPE_VIDEO_AVC, mime.c_str());
Chong Zhanga7fa1d92014-06-11 14:49:23 -07001238
1239 sp<AMessage> ccNotify = new AMessage(kWhatClosedCaptionNotify, id());
1240 mCCDecoder = new CCDecoder(ccNotify);
Lajos Molnar09524832014-07-17 14:29:51 -07001241
1242 if (mSourceFlags & Source::FLAG_SECURE) {
1243 format->setInt32("secure", true);
1244 }
Andreas Huber3fe62152011-09-16 15:09:22 -07001245 }
1246
Wei Jiabc2fb722014-07-08 16:37:57 -07001247 if (audio) {
Wei Jia88703c32014-08-06 11:24:07 -07001248 sp<AMessage> notify = new AMessage(kWhatAudioNotify, id());
1249 ++mAudioDecoderGeneration;
1250 notify->setInt32("generation", mAudioDecoderGeneration);
1251
Wei Jiabc2fb722014-07-08 16:37:57 -07001252 if (mOffloadAudio) {
1253 *decoder = new DecoderPassThrough(notify);
1254 } else {
1255 *decoder = new Decoder(notify);
1256 }
1257 } else {
Wei Jia88703c32014-08-06 11:24:07 -07001258 sp<AMessage> notify = new AMessage(kWhatVideoNotify, id());
1259 ++mVideoDecoderGeneration;
1260 notify->setInt32("generation", mVideoDecoderGeneration);
1261
Wei Jiabc2fb722014-07-08 16:37:57 -07001262 *decoder = new Decoder(notify, mNativeWindow);
1263 }
Lajos Molnar1cd13982014-01-17 15:12:51 -08001264 (*decoder)->init();
Andreas Huber84066782011-08-16 09:34:26 -07001265 (*decoder)->configure(format);
Andreas Huberf9334412010-12-15 15:17:42 -08001266
Lajos Molnar09524832014-07-17 14:29:51 -07001267 // allocate buffers to decrypt widevine source buffers
1268 if (!audio && (mSourceFlags & Source::FLAG_SECURE)) {
1269 Vector<sp<ABuffer> > inputBufs;
1270 CHECK_EQ((*decoder)->getInputBuffers(&inputBufs), (status_t)OK);
1271
1272 Vector<MediaBuffer *> mediaBufs;
1273 for (size_t i = 0; i < inputBufs.size(); i++) {
1274 const sp<ABuffer> &buffer = inputBufs[i];
1275 MediaBuffer *mbuf = new MediaBuffer(buffer->data(), buffer->size());
1276 mediaBufs.push(mbuf);
1277 }
1278
1279 status_t err = mSource->setBuffers(audio, mediaBufs);
1280 if (err != OK) {
1281 for (size_t i = 0; i < mediaBufs.size(); ++i) {
1282 mediaBufs[i]->release();
1283 }
1284 mediaBufs.clear();
1285 ALOGE("Secure source didn't support secure mediaBufs.");
1286 return err;
1287 }
1288 }
Andreas Huberf9334412010-12-15 15:17:42 -08001289 return OK;
1290}
1291
1292status_t NuPlayer::feedDecoderInputData(bool audio, const sp<AMessage> &msg) {
1293 sp<AMessage> reply;
1294 CHECK(msg->findMessage("reply", &reply));
1295
Wei Jia53904f32014-07-29 10:22:53 -07001296 if ((audio && mFlushingAudio != NONE)
Wei Jiaf702d042014-09-09 12:08:47 -07001297 || (!audio && mFlushingVideo != NONE)
1298 || mSource == NULL) {
Wei Jiab189a5b2014-08-07 06:11:39 +00001299 reply->setInt32("err", INFO_DISCONTINUITY);
1300 reply->post();
1301 return OK;
Andreas Huberf9334412010-12-15 15:17:42 -08001302 }
1303
1304 sp<ABuffer> accessUnit;
Andreas Huberf9334412010-12-15 15:17:42 -08001305
Phil Burk9f526492014-09-03 15:04:12 -07001306 // Aggregate smaller buffers into a larger buffer.
1307 // The goal is to reduce power consumption.
Phil Burk33b51b02014-09-17 16:03:47 -07001308 // Note this will not work if the decoder requires one frame per buffer.
1309 bool doBufferAggregation = (audio && mOffloadAudio);
Phil Burk9f526492014-09-03 15:04:12 -07001310 bool needMoreData = false;
Phil Burk9f526492014-09-03 15:04:12 -07001311
Andreas Huber3fe62152011-09-16 15:09:22 -07001312 bool dropAccessUnit;
1313 do {
Phil Burk9f526492014-09-03 15:04:12 -07001314 status_t err;
1315 // Did we save an accessUnit earlier because of a discontinuity?
1316 if (audio && (mPendingAudioAccessUnit != NULL)) {
1317 accessUnit = mPendingAudioAccessUnit;
1318 mPendingAudioAccessUnit.clear();
1319 err = mPendingAudioErr;
1320 ALOGV("feedDecoderInputData() use mPendingAudioAccessUnit");
1321 } else {
1322 err = mSource->dequeueAccessUnit(audio, &accessUnit);
1323 }
Andreas Huber5bc087c2010-12-23 10:27:40 -08001324
Andreas Huber3fe62152011-09-16 15:09:22 -07001325 if (err == -EWOULDBLOCK) {
Phil Burkc5cc2e22014-09-09 20:08:39 -07001326 return err;
Andreas Huber3fe62152011-09-16 15:09:22 -07001327 } else if (err != OK) {
1328 if (err == INFO_DISCONTINUITY) {
Phil Burk33b51b02014-09-17 16:03:47 -07001329 if (doBufferAggregation && (mAggregateBuffer != NULL)) {
Phil Burk9f526492014-09-03 15:04:12 -07001330 // We already have some data so save this for later.
1331 mPendingAudioErr = err;
1332 mPendingAudioAccessUnit = accessUnit;
1333 accessUnit.clear();
1334 ALOGD("feedDecoderInputData() save discontinuity for later");
1335 break;
1336 }
Andreas Huber3fe62152011-09-16 15:09:22 -07001337 int32_t type;
1338 CHECK(accessUnit->meta()->findInt32("discontinuity", &type));
Andreas Huber53df1a42010-12-22 10:03:04 -08001339
Andreas Huber3fe62152011-09-16 15:09:22 -07001340 bool formatChange =
Andreas Huber6e3d3112011-11-28 12:36:11 -08001341 (audio &&
1342 (type & ATSParser::DISCONTINUITY_AUDIO_FORMAT))
1343 || (!audio &&
1344 (type & ATSParser::DISCONTINUITY_VIDEO_FORMAT));
Andreas Huber53df1a42010-12-22 10:03:04 -08001345
Andreas Huber6e3d3112011-11-28 12:36:11 -08001346 bool timeChange = (type & ATSParser::DISCONTINUITY_TIME) != 0;
1347
Steve Blockdf64d152012-01-04 20:05:49 +00001348 ALOGI("%s discontinuity (formatChange=%d, time=%d)",
Andreas Huber6e3d3112011-11-28 12:36:11 -08001349 audio ? "audio" : "video", formatChange, timeChange);
Andreas Huber32f3cef2011-03-02 15:34:46 -08001350
Andreas Huber3fe62152011-09-16 15:09:22 -07001351 if (audio) {
1352 mSkipRenderingAudioUntilMediaTimeUs = -1;
1353 } else {
1354 mSkipRenderingVideoUntilMediaTimeUs = -1;
1355 }
Andreas Huber32f3cef2011-03-02 15:34:46 -08001356
Andreas Huber6e3d3112011-11-28 12:36:11 -08001357 if (timeChange) {
1358 sp<AMessage> extra;
1359 if (accessUnit->meta()->findMessage("extra", &extra)
1360 && extra != NULL) {
1361 int64_t resumeAtMediaTimeUs;
1362 if (extra->findInt64(
1363 "resume-at-mediatimeUs", &resumeAtMediaTimeUs)) {
Steve Blockdf64d152012-01-04 20:05:49 +00001364 ALOGI("suppressing rendering of %s until %lld us",
Andreas Huber6e3d3112011-11-28 12:36:11 -08001365 audio ? "audio" : "video", resumeAtMediaTimeUs);
Andreas Huber3fe62152011-09-16 15:09:22 -07001366
Andreas Huber6e3d3112011-11-28 12:36:11 -08001367 if (audio) {
1368 mSkipRenderingAudioUntilMediaTimeUs =
1369 resumeAtMediaTimeUs;
1370 } else {
1371 mSkipRenderingVideoUntilMediaTimeUs =
1372 resumeAtMediaTimeUs;
1373 }
Andreas Huber3fe62152011-09-16 15:09:22 -07001374 }
Andreas Huber32f3cef2011-03-02 15:34:46 -08001375 }
1376 }
Andreas Huber3fe62152011-09-16 15:09:22 -07001377
Andreas Huber6e3d3112011-11-28 12:36:11 -08001378 mTimeDiscontinuityPending =
1379 mTimeDiscontinuityPending || timeChange;
1380
Lajos Molnar87603c02014-08-20 19:25:30 -07001381 bool seamlessFormatChange = false;
1382 sp<AMessage> newFormat = mSource->getFormat(audio);
1383 if (formatChange) {
1384 seamlessFormatChange =
1385 getDecoder(audio)->supportsSeamlessFormatChange(newFormat);
1386 // treat seamless format change separately
1387 formatChange = !seamlessFormatChange;
1388 }
1389 bool shutdownOrFlush = formatChange || timeChange;
1390
1391 // We want to queue up scan-sources only once per discontinuity.
1392 // We control this by doing it only if neither audio nor video are
1393 // flushing or shutting down. (After handling 1st discontinuity, one
1394 // of the flushing states will not be NONE.)
1395 // No need to scan sources if this discontinuity does not result
1396 // in a flush or shutdown, as the flushing state will stay NONE.
1397 if (mFlushingAudio == NONE && mFlushingVideo == NONE &&
1398 shutdownOrFlush) {
Robert Shiha2981012014-07-30 17:41:24 -07001399 // And we'll resume scanning sources once we're done
1400 // flushing.
1401 mDeferredActions.push_front(
1402 new SimpleAction(
1403 &NuPlayer::performScanSources));
1404 }
1405
Lajos Molnar87603c02014-08-20 19:25:30 -07001406 if (formatChange /* not seamless */) {
1407 // must change decoder
1408 flushDecoder(audio, /* needShutdown = */ true);
1409 } else if (timeChange) {
1410 // need to flush
1411 flushDecoder(audio, /* needShutdown = */ false, newFormat);
1412 err = OK;
1413 } else if (seamlessFormatChange) {
1414 // reuse existing decoder and don't flush
1415 updateDecoderFormatWithoutFlush(audio, newFormat);
1416 err = OK;
Andreas Huber6e3d3112011-11-28 12:36:11 -08001417 } else {
1418 // This stream is unaffected by the discontinuity
Andreas Huber6e3d3112011-11-28 12:36:11 -08001419 return -EWOULDBLOCK;
1420 }
Andreas Huber32f3cef2011-03-02 15:34:46 -08001421 }
1422
Andreas Huber3fe62152011-09-16 15:09:22 -07001423 reply->setInt32("err", err);
1424 reply->post();
1425 return OK;
Andreas Huberf9334412010-12-15 15:17:42 -08001426 }
1427
Andreas Huber3fe62152011-09-16 15:09:22 -07001428 if (!audio) {
1429 ++mNumFramesTotal;
1430 }
1431
1432 dropAccessUnit = false;
1433 if (!audio
Lajos Molnar09524832014-07-17 14:29:51 -07001434 && !(mSourceFlags & Source::FLAG_SECURE)
Andreas Huber3fe62152011-09-16 15:09:22 -07001435 && mVideoLateByUs > 100000ll
1436 && mVideoIsAVC
1437 && !IsAVCReferenceFrame(accessUnit)) {
1438 dropAccessUnit = true;
1439 ++mNumFramesDropped;
1440 }
Phil Burk9f526492014-09-03 15:04:12 -07001441
1442 size_t smallSize = accessUnit->size();
1443 needMoreData = false;
Phil Burkc5cc2e22014-09-09 20:08:39 -07001444 if (doBufferAggregation && (mAggregateBuffer == NULL)
Phil Burk9f526492014-09-03 15:04:12 -07001445 // Don't bother if only room for a few small buffers.
Phil Burkc5cc2e22014-09-09 20:08:39 -07001446 && (smallSize < (kAggregateBufferSizeBytes / 3))) {
Phil Burk9f526492014-09-03 15:04:12 -07001447 // Create a larger buffer for combining smaller buffers from the extractor.
Phil Burkc5cc2e22014-09-09 20:08:39 -07001448 mAggregateBuffer = new ABuffer(kAggregateBufferSizeBytes);
1449 mAggregateBuffer->setRange(0, 0); // start empty
Phil Burk9f526492014-09-03 15:04:12 -07001450 }
1451
Phil Burk33b51b02014-09-17 16:03:47 -07001452 if (doBufferAggregation && (mAggregateBuffer != NULL)) {
Phil Burk9f526492014-09-03 15:04:12 -07001453 int64_t timeUs;
Phil Burkc5cc2e22014-09-09 20:08:39 -07001454 int64_t dummy;
Phil Burk9f526492014-09-03 15:04:12 -07001455 bool smallTimestampValid = accessUnit->meta()->findInt64("timeUs", &timeUs);
Phil Burkc5cc2e22014-09-09 20:08:39 -07001456 bool bigTimestampValid = mAggregateBuffer->meta()->findInt64("timeUs", &dummy);
Phil Burk9f526492014-09-03 15:04:12 -07001457 // Will the smaller buffer fit?
Phil Burkc5cc2e22014-09-09 20:08:39 -07001458 size_t bigSize = mAggregateBuffer->size();
1459 size_t roomLeft = mAggregateBuffer->capacity() - bigSize;
Phil Burk9f526492014-09-03 15:04:12 -07001460 // Should we save this small buffer for the next big buffer?
1461 // If the first small buffer did not have a timestamp then save
1462 // any buffer that does have a timestamp until the next big buffer.
1463 if ((smallSize > roomLeft)
Phil Burkc5cc2e22014-09-09 20:08:39 -07001464 || (!bigTimestampValid && (bigSize > 0) && smallTimestampValid)) {
Phil Burk9f526492014-09-03 15:04:12 -07001465 mPendingAudioErr = err;
1466 mPendingAudioAccessUnit = accessUnit;
1467 accessUnit.clear();
1468 } else {
Phil Burkc5cc2e22014-09-09 20:08:39 -07001469 // Grab time from first small buffer if available.
1470 if ((bigSize == 0) && smallTimestampValid) {
1471 mAggregateBuffer->meta()->setInt64("timeUs", timeUs);
1472 }
Phil Burk9f526492014-09-03 15:04:12 -07001473 // Append small buffer to the bigger buffer.
Phil Burkc5cc2e22014-09-09 20:08:39 -07001474 memcpy(mAggregateBuffer->base() + bigSize, accessUnit->data(), smallSize);
Phil Burk9f526492014-09-03 15:04:12 -07001475 bigSize += smallSize;
Phil Burkc5cc2e22014-09-09 20:08:39 -07001476 mAggregateBuffer->setRange(0, bigSize);
Phil Burk9f526492014-09-03 15:04:12 -07001477
Phil Burkc5cc2e22014-09-09 20:08:39 -07001478 // Keep looping until we run out of room in the mAggregateBuffer.
Phil Burk9f526492014-09-03 15:04:12 -07001479 needMoreData = true;
1480
Phil Burkc5cc2e22014-09-09 20:08:39 -07001481 ALOGV("feedDecoderInputData() smallSize = %zu, bigSize = %zu, capacity = %zu",
1482 smallSize, bigSize, mAggregateBuffer->capacity());
Phil Burk9f526492014-09-03 15:04:12 -07001483 }
1484 }
1485 } while (dropAccessUnit || needMoreData);
Andreas Huberf9334412010-12-15 15:17:42 -08001486
Steve Block3856b092011-10-20 11:56:00 +01001487 // ALOGV("returned a valid buffer of %s data", audio ? "audio" : "video");
Andreas Huberf9334412010-12-15 15:17:42 -08001488
1489#if 0
1490 int64_t mediaTimeUs;
1491 CHECK(accessUnit->meta()->findInt64("timeUs", &mediaTimeUs));
Steve Block3856b092011-10-20 11:56:00 +01001492 ALOGV("feeding %s input buffer at media time %.2f secs",
Andreas Huberf9334412010-12-15 15:17:42 -08001493 audio ? "audio" : "video",
1494 mediaTimeUs / 1E6);
1495#endif
1496
Chong Zhanga7fa1d92014-06-11 14:49:23 -07001497 if (!audio) {
1498 mCCDecoder->decode(accessUnit);
1499 }
1500
Phil Burk33b51b02014-09-17 16:03:47 -07001501 if (doBufferAggregation && (mAggregateBuffer != NULL)) {
Phil Burkc5cc2e22014-09-09 20:08:39 -07001502 ALOGV("feedDecoderInputData() reply with aggregated buffer, %zu",
1503 mAggregateBuffer->size());
1504 reply->setBuffer("buffer", mAggregateBuffer);
1505 mAggregateBuffer.clear();
Phil Burk9f526492014-09-03 15:04:12 -07001506 } else {
1507 reply->setBuffer("buffer", accessUnit);
1508 }
1509
Andreas Huberf9334412010-12-15 15:17:42 -08001510 reply->post();
1511
1512 return OK;
1513}
1514
1515void NuPlayer::renderBuffer(bool audio, const sp<AMessage> &msg) {
Steve Block3856b092011-10-20 11:56:00 +01001516 // ALOGV("renderBuffer %s", audio ? "audio" : "video");
Andreas Huberf9334412010-12-15 15:17:42 -08001517
1518 sp<AMessage> reply;
1519 CHECK(msg->findMessage("reply", &reply));
1520
Wei Jia53904f32014-07-29 10:22:53 -07001521 if ((audio && mFlushingAudio != NONE)
1522 || (!audio && mFlushingVideo != NONE)) {
Andreas Huber18ac5402011-08-31 15:04:25 -07001523 // We're currently attempting to flush the decoder, in order
1524 // to complete this, the decoder wants all its buffers back,
1525 // so we don't want any output buffers it sent us (from before
1526 // we initiated the flush) to be stuck in the renderer's queue.
1527
Steve Block3856b092011-10-20 11:56:00 +01001528 ALOGV("we're still flushing the %s decoder, sending its output buffer"
Andreas Huber18ac5402011-08-31 15:04:25 -07001529 " right back.", audio ? "audio" : "video");
1530
1531 reply->post();
1532 return;
1533 }
1534
Andreas Huber2d8bedd2012-02-21 14:38:23 -08001535 sp<ABuffer> buffer;
1536 CHECK(msg->findBuffer("buffer", &buffer));
Andreas Huberf9334412010-12-15 15:17:42 -08001537
Chong Zhanga7fa1d92014-06-11 14:49:23 -07001538 int64_t mediaTimeUs;
1539 CHECK(buffer->meta()->findInt64("timeUs", &mediaTimeUs));
1540
Andreas Huber32f3cef2011-03-02 15:34:46 -08001541 int64_t &skipUntilMediaTimeUs =
1542 audio
1543 ? mSkipRenderingAudioUntilMediaTimeUs
1544 : mSkipRenderingVideoUntilMediaTimeUs;
1545
1546 if (skipUntilMediaTimeUs >= 0) {
Andreas Huber32f3cef2011-03-02 15:34:46 -08001547
1548 if (mediaTimeUs < skipUntilMediaTimeUs) {
Steve Block3856b092011-10-20 11:56:00 +01001549 ALOGV("dropping %s buffer at time %lld as requested.",
Andreas Huber32f3cef2011-03-02 15:34:46 -08001550 audio ? "audio" : "video",
1551 mediaTimeUs);
1552
1553 reply->post();
1554 return;
1555 }
1556
1557 skipUntilMediaTimeUs = -1;
1558 }
1559
Chong Zhanga7fa1d92014-06-11 14:49:23 -07001560 if (!audio && mCCDecoder->isSelected()) {
1561 mCCDecoder->display(mediaTimeUs);
1562 }
1563
Andreas Huberf9334412010-12-15 15:17:42 -08001564 mRenderer->queueBuffer(audio, buffer, reply);
1565}
1566
Chong Zhangced1c2f2014-08-08 15:22:35 -07001567void NuPlayer::updateVideoSize(
1568 const sp<AMessage> &inputFormat,
1569 const sp<AMessage> &outputFormat) {
1570 if (inputFormat == NULL) {
1571 ALOGW("Unknown video size, reporting 0x0!");
1572 notifyListener(MEDIA_SET_VIDEO_SIZE, 0, 0);
1573 return;
1574 }
1575
1576 int32_t displayWidth, displayHeight;
1577 int32_t cropLeft, cropTop, cropRight, cropBottom;
1578
1579 if (outputFormat != NULL) {
1580 int32_t width, height;
1581 CHECK(outputFormat->findInt32("width", &width));
1582 CHECK(outputFormat->findInt32("height", &height));
1583
1584 int32_t cropLeft, cropTop, cropRight, cropBottom;
1585 CHECK(outputFormat->findRect(
1586 "crop",
1587 &cropLeft, &cropTop, &cropRight, &cropBottom));
1588
1589 displayWidth = cropRight - cropLeft + 1;
1590 displayHeight = cropBottom - cropTop + 1;
1591
1592 ALOGV("Video output format changed to %d x %d "
1593 "(crop: %d x %d @ (%d, %d))",
1594 width, height,
1595 displayWidth,
1596 displayHeight,
1597 cropLeft, cropTop);
1598 } else {
1599 CHECK(inputFormat->findInt32("width", &displayWidth));
1600 CHECK(inputFormat->findInt32("height", &displayHeight));
1601
1602 ALOGV("Video input format %d x %d", displayWidth, displayHeight);
1603 }
1604
1605 // Take into account sample aspect ratio if necessary:
1606 int32_t sarWidth, sarHeight;
1607 if (inputFormat->findInt32("sar-width", &sarWidth)
1608 && inputFormat->findInt32("sar-height", &sarHeight)) {
1609 ALOGV("Sample aspect ratio %d : %d", sarWidth, sarHeight);
1610
1611 displayWidth = (displayWidth * sarWidth) / sarHeight;
1612
1613 ALOGV("display dimensions %d x %d", displayWidth, displayHeight);
1614 }
1615
1616 int32_t rotationDegrees;
1617 if (!inputFormat->findInt32("rotation-degrees", &rotationDegrees)) {
1618 rotationDegrees = 0;
1619 }
1620
1621 if (rotationDegrees == 90 || rotationDegrees == 270) {
1622 int32_t tmp = displayWidth;
1623 displayWidth = displayHeight;
1624 displayHeight = tmp;
1625 }
1626
1627 notifyListener(
1628 MEDIA_SET_VIDEO_SIZE,
1629 displayWidth,
1630 displayHeight);
1631}
1632
Chong Zhangdcb89b32013-08-06 09:44:47 -07001633void NuPlayer::notifyListener(int msg, int ext1, int ext2, const Parcel *in) {
Andreas Huber43c3e6c2011-01-05 12:17:08 -08001634 if (mDriver == NULL) {
Andreas Huberf9334412010-12-15 15:17:42 -08001635 return;
1636 }
1637
Andreas Huber43c3e6c2011-01-05 12:17:08 -08001638 sp<NuPlayerDriver> driver = mDriver.promote();
Andreas Huberf9334412010-12-15 15:17:42 -08001639
Andreas Huber43c3e6c2011-01-05 12:17:08 -08001640 if (driver == NULL) {
Andreas Huberf9334412010-12-15 15:17:42 -08001641 return;
1642 }
1643
Chong Zhangdcb89b32013-08-06 09:44:47 -07001644 driver->notifyListener(msg, ext1, ext2, in);
Andreas Huberf9334412010-12-15 15:17:42 -08001645}
1646
Lajos Molnar87603c02014-08-20 19:25:30 -07001647void NuPlayer::flushDecoder(
1648 bool audio, bool needShutdown, const sp<AMessage> &newFormat) {
Andreas Huber14f76722013-01-15 09:04:18 -08001649 ALOGV("[%s] flushDecoder needShutdown=%d",
1650 audio ? "audio" : "video", needShutdown);
1651
Lajos Molnar87603c02014-08-20 19:25:30 -07001652 const sp<Decoder> &decoder = getDecoder(audio);
1653 if (decoder == NULL) {
Steve Blockdf64d152012-01-04 20:05:49 +00001654 ALOGI("flushDecoder %s without decoder present",
Andreas Huber6e3d3112011-11-28 12:36:11 -08001655 audio ? "audio" : "video");
Lajos Molnar87603c02014-08-20 19:25:30 -07001656 return;
Andreas Huber6e3d3112011-11-28 12:36:11 -08001657 }
1658
Andreas Huber1aef2112011-01-04 14:01:29 -08001659 // Make sure we don't continue to scan sources until we finish flushing.
1660 ++mScanSourcesGeneration;
Andreas Huber43c3e6c2011-01-05 12:17:08 -08001661 mScanSourcesPending = false;
Andreas Huber1aef2112011-01-04 14:01:29 -08001662
Lajos Molnar87603c02014-08-20 19:25:30 -07001663 decoder->signalFlush(newFormat);
Andreas Huber1aef2112011-01-04 14:01:29 -08001664 mRenderer->flush(audio);
1665
1666 FlushStatus newStatus =
1667 needShutdown ? FLUSHING_DECODER_SHUTDOWN : FLUSHING_DECODER;
1668
1669 if (audio) {
Wei Jia53904f32014-07-29 10:22:53 -07001670 ALOGE_IF(mFlushingAudio != NONE,
1671 "audio flushDecoder() is called in state %d", mFlushingAudio);
Andreas Huber1aef2112011-01-04 14:01:29 -08001672 mFlushingAudio = newStatus;
Andreas Huber1aef2112011-01-04 14:01:29 -08001673 } else {
Wei Jia53904f32014-07-29 10:22:53 -07001674 ALOGE_IF(mFlushingVideo != NONE,
1675 "video flushDecoder() is called in state %d", mFlushingVideo);
Andreas Huber1aef2112011-01-04 14:01:29 -08001676 mFlushingVideo = newStatus;
Chong Zhangb86e68f2014-08-01 13:46:53 -07001677
1678 if (mCCDecoder != NULL) {
1679 mCCDecoder->flush();
1680 }
Andreas Huber1aef2112011-01-04 14:01:29 -08001681 }
1682}
1683
Lajos Molnar87603c02014-08-20 19:25:30 -07001684void NuPlayer::updateDecoderFormatWithoutFlush(
1685 bool audio, const sp<AMessage> &format) {
1686 ALOGV("[%s] updateDecoderFormatWithoutFlush", audio ? "audio" : "video");
1687
1688 const sp<Decoder> &decoder = getDecoder(audio);
1689 if (decoder == NULL) {
1690 ALOGI("updateDecoderFormatWithoutFlush %s without decoder present",
1691 audio ? "audio" : "video");
1692 return;
1693 }
1694
1695 decoder->signalUpdateFormat(format);
1696}
1697
Chong Zhangced1c2f2014-08-08 15:22:35 -07001698void NuPlayer::queueDecoderShutdown(
1699 bool audio, bool video, const sp<AMessage> &reply) {
1700 ALOGI("queueDecoderShutdown audio=%d, video=%d", audio, video);
Andreas Huber84066782011-08-16 09:34:26 -07001701
Chong Zhangced1c2f2014-08-08 15:22:35 -07001702 mDeferredActions.push_back(
1703 new ShutdownDecoderAction(audio, video));
Andreas Huber84066782011-08-16 09:34:26 -07001704
Chong Zhangced1c2f2014-08-08 15:22:35 -07001705 mDeferredActions.push_back(
1706 new SimpleAction(&NuPlayer::performScanSources));
Andreas Huber84066782011-08-16 09:34:26 -07001707
Chong Zhangced1c2f2014-08-08 15:22:35 -07001708 mDeferredActions.push_back(new PostMessageAction(reply));
1709
1710 processDeferredActions();
Andreas Huber84066782011-08-16 09:34:26 -07001711}
1712
James Dong0d268a32012-08-31 12:18:27 -07001713status_t NuPlayer::setVideoScalingMode(int32_t mode) {
1714 mVideoScalingMode = mode;
Andreas Huber57a339c2012-12-03 11:18:00 -08001715 if (mNativeWindow != NULL) {
James Dong0d268a32012-08-31 12:18:27 -07001716 status_t ret = native_window_set_scaling_mode(
1717 mNativeWindow->getNativeWindow().get(), mVideoScalingMode);
1718 if (ret != OK) {
1719 ALOGE("Failed to set scaling mode (%d): %s",
1720 -ret, strerror(-ret));
1721 return ret;
1722 }
1723 }
1724 return OK;
1725}
1726
Chong Zhangdcb89b32013-08-06 09:44:47 -07001727status_t NuPlayer::getTrackInfo(Parcel* reply) const {
1728 sp<AMessage> msg = new AMessage(kWhatGetTrackInfo, id());
1729 msg->setPointer("reply", reply);
1730
1731 sp<AMessage> response;
1732 status_t err = msg->postAndAwaitResponse(&response);
1733 return err;
1734}
1735
Robert Shih7c4f0d72014-07-09 18:53:31 -07001736status_t NuPlayer::getSelectedTrack(int32_t type, Parcel* reply) const {
1737 sp<AMessage> msg = new AMessage(kWhatGetSelectedTrack, id());
1738 msg->setPointer("reply", reply);
1739 msg->setInt32("type", type);
1740
1741 sp<AMessage> response;
1742 status_t err = msg->postAndAwaitResponse(&response);
1743 if (err == OK && response != NULL) {
1744 CHECK(response->findInt32("err", &err));
1745 }
1746 return err;
1747}
1748
Chong Zhangdcb89b32013-08-06 09:44:47 -07001749status_t NuPlayer::selectTrack(size_t trackIndex, bool select) {
1750 sp<AMessage> msg = new AMessage(kWhatSelectTrack, id());
1751 msg->setSize("trackIndex", trackIndex);
1752 msg->setInt32("select", select);
1753
1754 sp<AMessage> response;
1755 status_t err = msg->postAndAwaitResponse(&response);
1756
Chong Zhang404fced2014-06-11 14:45:31 -07001757 if (err != OK) {
1758 return err;
1759 }
1760
1761 if (!response->findInt32("err", &err)) {
1762 err = OK;
1763 }
1764
Chong Zhangdcb89b32013-08-06 09:44:47 -07001765 return err;
1766}
1767
Marco Nelissenf0b72b52014-09-16 15:43:44 -07001768sp<MetaData> NuPlayer::getFileMeta() {
1769 return mSource->getFileFormatMeta();
1770}
1771
Andreas Huberb7c8e912012-11-27 15:02:53 -08001772void NuPlayer::schedulePollDuration() {
1773 sp<AMessage> msg = new AMessage(kWhatPollDuration, id());
1774 msg->setInt32("generation", mPollDurationGeneration);
1775 msg->post();
1776}
1777
1778void NuPlayer::cancelPollDuration() {
1779 ++mPollDurationGeneration;
1780}
1781
Andreas Hubera1f8ab02012-11-30 10:53:22 -08001782void NuPlayer::processDeferredActions() {
1783 while (!mDeferredActions.empty()) {
1784 // We won't execute any deferred actions until we're no longer in
1785 // an intermediate state, i.e. one more more decoders are currently
1786 // flushing or shutting down.
1787
Andreas Hubera1f8ab02012-11-30 10:53:22 -08001788 if (mFlushingAudio != NONE || mFlushingVideo != NONE) {
1789 // We're currently flushing, postpone the reset until that's
1790 // completed.
1791
1792 ALOGV("postponing action mFlushingAudio=%d, mFlushingVideo=%d",
1793 mFlushingAudio, mFlushingVideo);
1794
1795 break;
1796 }
1797
1798 sp<Action> action = *mDeferredActions.begin();
1799 mDeferredActions.erase(mDeferredActions.begin());
1800
1801 action->execute(this);
1802 }
1803}
1804
1805void NuPlayer::performSeek(int64_t seekTimeUs) {
1806 ALOGV("performSeek seekTimeUs=%lld us (%.2f secs)",
1807 seekTimeUs,
1808 seekTimeUs / 1E6);
1809
Andy Hungadf34bf2014-09-03 18:22:22 -07001810 if (mSource == NULL) {
1811 // This happens when reset occurs right before the loop mode
1812 // asynchronously seeks to the start of the stream.
1813 LOG_ALWAYS_FATAL_IF(mAudioDecoder != NULL || mVideoDecoder != NULL,
1814 "mSource is NULL and decoders not NULL audio(%p) video(%p)",
1815 mAudioDecoder.get(), mVideoDecoder.get());
1816 return;
1817 }
Andreas Hubera1f8ab02012-11-30 10:53:22 -08001818 mSource->seekTo(seekTimeUs);
Robert Shihd3b0bbb2014-07-23 15:00:25 -07001819 ++mTimedTextGeneration;
Andreas Hubera1f8ab02012-11-30 10:53:22 -08001820
1821 if (mDriver != NULL) {
1822 sp<NuPlayerDriver> driver = mDriver.promote();
1823 if (driver != NULL) {
1824 driver->notifyPosition(seekTimeUs);
1825 driver->notifySeekComplete();
1826 }
1827 }
1828
1829 // everything's flushed, continue playback.
1830}
1831
1832void NuPlayer::performDecoderFlush() {
1833 ALOGV("performDecoderFlush");
1834
Andreas Huberda9740e2013-04-16 10:54:03 -07001835 if (mAudioDecoder == NULL && mVideoDecoder == NULL) {
Andreas Hubera1f8ab02012-11-30 10:53:22 -08001836 return;
1837 }
1838
1839 mTimeDiscontinuityPending = true;
1840
1841 if (mAudioDecoder != NULL) {
1842 flushDecoder(true /* audio */, false /* needShutdown */);
1843 }
1844
1845 if (mVideoDecoder != NULL) {
1846 flushDecoder(false /* audio */, false /* needShutdown */);
1847 }
1848}
1849
Andreas Huber14f76722013-01-15 09:04:18 -08001850void NuPlayer::performDecoderShutdown(bool audio, bool video) {
1851 ALOGV("performDecoderShutdown audio=%d, video=%d", audio, video);
Andreas Hubera1f8ab02012-11-30 10:53:22 -08001852
Andreas Huber14f76722013-01-15 09:04:18 -08001853 if ((!audio || mAudioDecoder == NULL)
1854 && (!video || mVideoDecoder == NULL)) {
Andreas Hubera1f8ab02012-11-30 10:53:22 -08001855 return;
1856 }
1857
1858 mTimeDiscontinuityPending = true;
1859
Andreas Huber14f76722013-01-15 09:04:18 -08001860 if (audio && mAudioDecoder != NULL) {
Andreas Hubera1f8ab02012-11-30 10:53:22 -08001861 flushDecoder(true /* audio */, true /* needShutdown */);
1862 }
1863
Andreas Huber14f76722013-01-15 09:04:18 -08001864 if (video && mVideoDecoder != NULL) {
Andreas Hubera1f8ab02012-11-30 10:53:22 -08001865 flushDecoder(false /* audio */, true /* needShutdown */);
1866 }
1867}
1868
1869void NuPlayer::performReset() {
1870 ALOGV("performReset");
1871
1872 CHECK(mAudioDecoder == NULL);
1873 CHECK(mVideoDecoder == NULL);
1874
1875 cancelPollDuration();
1876
1877 ++mScanSourcesGeneration;
1878 mScanSourcesPending = false;
1879
Lajos Molnar09524832014-07-17 14:29:51 -07001880 if (mRendererLooper != NULL) {
1881 if (mRenderer != NULL) {
1882 mRendererLooper->unregisterHandler(mRenderer->id());
1883 }
1884 mRendererLooper->stop();
1885 mRendererLooper.clear();
1886 }
Andreas Hubera1f8ab02012-11-30 10:53:22 -08001887 mRenderer.clear();
Wei Jia57568df2014-09-22 10:16:29 -07001888 ++mRendererGeneration;
Andreas Hubera1f8ab02012-11-30 10:53:22 -08001889
1890 if (mSource != NULL) {
1891 mSource->stop();
Andreas Huberb5f25f02013-02-05 10:14:26 -08001892
Andreas Hubera1f8ab02012-11-30 10:53:22 -08001893 mSource.clear();
1894 }
1895
1896 if (mDriver != NULL) {
1897 sp<NuPlayerDriver> driver = mDriver.promote();
1898 if (driver != NULL) {
1899 driver->notifyResetComplete();
1900 }
1901 }
Andreas Huber57a339c2012-12-03 11:18:00 -08001902
1903 mStarted = false;
Andreas Hubera1f8ab02012-11-30 10:53:22 -08001904}
1905
1906void NuPlayer::performScanSources() {
1907 ALOGV("performScanSources");
1908
Andreas Huber57a339c2012-12-03 11:18:00 -08001909 if (!mStarted) {
1910 return;
1911 }
1912
Andreas Hubera1f8ab02012-11-30 10:53:22 -08001913 if (mAudioDecoder == NULL || mVideoDecoder == NULL) {
1914 postScanSources();
1915 }
1916}
1917
Andreas Huber57a339c2012-12-03 11:18:00 -08001918void NuPlayer::performSetSurface(const sp<NativeWindowWrapper> &wrapper) {
1919 ALOGV("performSetSurface");
1920
1921 mNativeWindow = wrapper;
1922
1923 // XXX - ignore error from setVideoScalingMode for now
1924 setVideoScalingMode(mVideoScalingMode);
Chong Zhang13d6faa2014-08-22 15:35:28 -07001925
1926 if (mDriver != NULL) {
1927 sp<NuPlayerDriver> driver = mDriver.promote();
1928 if (driver != NULL) {
1929 driver->notifySetSurfaceComplete();
1930 }
1931 }
Andreas Huber57a339c2012-12-03 11:18:00 -08001932}
1933
Andreas Huber9575c962013-02-05 13:59:56 -08001934void NuPlayer::onSourceNotify(const sp<AMessage> &msg) {
1935 int32_t what;
1936 CHECK(msg->findInt32("what", &what));
1937
1938 switch (what) {
1939 case Source::kWhatPrepared:
1940 {
Andreas Huberb5f28d42013-04-25 15:11:19 -07001941 if (mSource == NULL) {
1942 // This is a stale notification from a source that was
1943 // asynchronously preparing when the client called reset().
1944 // We handled the reset, the source is gone.
1945 break;
1946 }
1947
Andreas Huberec0c5972013-02-05 14:47:13 -08001948 int32_t err;
1949 CHECK(msg->findInt32("err", &err));
1950
Andreas Huber9575c962013-02-05 13:59:56 -08001951 sp<NuPlayerDriver> driver = mDriver.promote();
1952 if (driver != NULL) {
Marco Nelissendd114d12014-05-28 15:23:14 -07001953 // notify duration first, so that it's definitely set when
1954 // the app received the "prepare complete" callback.
1955 int64_t durationUs;
1956 if (mSource->getDuration(&durationUs) == OK) {
1957 driver->notifyDuration(durationUs);
1958 }
Andreas Huberec0c5972013-02-05 14:47:13 -08001959 driver->notifyPrepareCompleted(err);
Andreas Huber9575c962013-02-05 13:59:56 -08001960 }
Andreas Huber99759402013-04-01 14:28:31 -07001961
Andreas Huber9575c962013-02-05 13:59:56 -08001962 break;
1963 }
1964
1965 case Source::kWhatFlagsChanged:
1966 {
1967 uint32_t flags;
1968 CHECK(msg->findInt32("flags", (int32_t *)&flags));
1969
Chong Zhang4b7069d2013-09-11 12:52:43 -07001970 sp<NuPlayerDriver> driver = mDriver.promote();
1971 if (driver != NULL) {
1972 driver->notifyFlagsChanged(flags);
1973 }
1974
Andreas Huber9575c962013-02-05 13:59:56 -08001975 if ((mSourceFlags & Source::FLAG_DYNAMIC_DURATION)
1976 && (!(flags & Source::FLAG_DYNAMIC_DURATION))) {
1977 cancelPollDuration();
1978 } else if (!(mSourceFlags & Source::FLAG_DYNAMIC_DURATION)
1979 && (flags & Source::FLAG_DYNAMIC_DURATION)
1980 && (mAudioDecoder != NULL || mVideoDecoder != NULL)) {
1981 schedulePollDuration();
1982 }
1983
1984 mSourceFlags = flags;
1985 break;
1986 }
1987
1988 case Source::kWhatVideoSizeChanged:
1989 {
Chong Zhangced1c2f2014-08-08 15:22:35 -07001990 sp<AMessage> format;
1991 CHECK(msg->findMessage("format", &format));
Andreas Huber9575c962013-02-05 13:59:56 -08001992
Chong Zhangced1c2f2014-08-08 15:22:35 -07001993 updateVideoSize(format);
Andreas Huber9575c962013-02-05 13:59:56 -08001994 break;
1995 }
1996
Chong Zhang2a3cc9a2014-08-21 17:48:26 -07001997 case Source::kWhatBufferingUpdate:
1998 {
1999 int32_t percentage;
2000 CHECK(msg->findInt32("percentage", &percentage));
2001
2002 notifyListener(MEDIA_BUFFERING_UPDATE, percentage, 0);
2003 break;
2004 }
2005
Roger Jönssonb50e83e2013-01-21 16:26:41 +01002006 case Source::kWhatBufferingStart:
2007 {
2008 notifyListener(MEDIA_INFO, MEDIA_INFO_BUFFERING_START, 0);
2009 break;
2010 }
2011
2012 case Source::kWhatBufferingEnd:
2013 {
2014 notifyListener(MEDIA_INFO, MEDIA_INFO_BUFFERING_END, 0);
2015 break;
2016 }
2017
Chong Zhangdcb89b32013-08-06 09:44:47 -07002018 case Source::kWhatSubtitleData:
2019 {
2020 sp<ABuffer> buffer;
2021 CHECK(msg->findBuffer("buffer", &buffer));
2022
Chong Zhang404fced2014-06-11 14:45:31 -07002023 sendSubtitleData(buffer, 0 /* baseIndex */);
Chong Zhangdcb89b32013-08-06 09:44:47 -07002024 break;
2025 }
2026
Robert Shihd3b0bbb2014-07-23 15:00:25 -07002027 case Source::kWhatTimedTextData:
2028 {
2029 int32_t generation;
2030 if (msg->findInt32("generation", &generation)
2031 && generation != mTimedTextGeneration) {
2032 break;
2033 }
2034
2035 sp<ABuffer> buffer;
2036 CHECK(msg->findBuffer("buffer", &buffer));
2037
2038 sp<NuPlayerDriver> driver = mDriver.promote();
2039 if (driver == NULL) {
2040 break;
2041 }
2042
2043 int posMs;
2044 int64_t timeUs, posUs;
2045 driver->getCurrentPosition(&posMs);
2046 posUs = posMs * 1000;
2047 CHECK(buffer->meta()->findInt64("timeUs", &timeUs));
2048
2049 if (posUs < timeUs) {
2050 if (!msg->findInt32("generation", &generation)) {
2051 msg->setInt32("generation", mTimedTextGeneration);
2052 }
2053 msg->post(timeUs - posUs);
2054 } else {
2055 sendTimedTextData(buffer);
2056 }
2057 break;
2058 }
2059
Andreas Huber14f76722013-01-15 09:04:18 -08002060 case Source::kWhatQueueDecoderShutdown:
2061 {
2062 int32_t audio, video;
2063 CHECK(msg->findInt32("audio", &audio));
2064 CHECK(msg->findInt32("video", &video));
2065
2066 sp<AMessage> reply;
2067 CHECK(msg->findMessage("reply", &reply));
2068
2069 queueDecoderShutdown(audio, video, reply);
2070 break;
2071 }
2072
Ronghua Wu80276872014-08-28 15:50:29 -07002073 case Source::kWhatDrmNoLicense:
2074 {
2075 notifyListener(MEDIA_ERROR, MEDIA_ERROR_UNKNOWN, ERROR_DRM_NO_LICENSE);
2076 break;
2077 }
2078
Andreas Huber9575c962013-02-05 13:59:56 -08002079 default:
2080 TRESPASS();
2081 }
2082}
2083
Chong Zhanga7fa1d92014-06-11 14:49:23 -07002084void NuPlayer::onClosedCaptionNotify(const sp<AMessage> &msg) {
2085 int32_t what;
2086 CHECK(msg->findInt32("what", &what));
2087
2088 switch (what) {
2089 case NuPlayer::CCDecoder::kWhatClosedCaptionData:
2090 {
2091 sp<ABuffer> buffer;
2092 CHECK(msg->findBuffer("buffer", &buffer));
2093
2094 size_t inbandTracks = 0;
2095 if (mSource != NULL) {
2096 inbandTracks = mSource->getTrackCount();
2097 }
2098
2099 sendSubtitleData(buffer, inbandTracks);
2100 break;
2101 }
2102
2103 case NuPlayer::CCDecoder::kWhatTrackAdded:
2104 {
2105 notifyListener(MEDIA_INFO, MEDIA_INFO_METADATA_UPDATE, 0);
2106
2107 break;
2108 }
2109
2110 default:
2111 TRESPASS();
2112 }
2113
2114
2115}
2116
Chong Zhang404fced2014-06-11 14:45:31 -07002117void NuPlayer::sendSubtitleData(const sp<ABuffer> &buffer, int32_t baseIndex) {
2118 int32_t trackIndex;
2119 int64_t timeUs, durationUs;
2120 CHECK(buffer->meta()->findInt32("trackIndex", &trackIndex));
2121 CHECK(buffer->meta()->findInt64("timeUs", &timeUs));
2122 CHECK(buffer->meta()->findInt64("durationUs", &durationUs));
2123
2124 Parcel in;
2125 in.writeInt32(trackIndex + baseIndex);
2126 in.writeInt64(timeUs);
2127 in.writeInt64(durationUs);
2128 in.writeInt32(buffer->size());
2129 in.writeInt32(buffer->size());
2130 in.write(buffer->data(), buffer->size());
2131
2132 notifyListener(MEDIA_SUBTITLE_DATA, 0, 0, &in);
2133}
Robert Shihd3b0bbb2014-07-23 15:00:25 -07002134
2135void NuPlayer::sendTimedTextData(const sp<ABuffer> &buffer) {
2136 const void *data;
2137 size_t size = 0;
2138 int64_t timeUs;
2139 int32_t flag = TextDescriptions::LOCAL_DESCRIPTIONS;
2140
2141 AString mime;
2142 CHECK(buffer->meta()->findString("mime", &mime));
2143 CHECK(strcasecmp(mime.c_str(), MEDIA_MIMETYPE_TEXT_3GPP) == 0);
2144
2145 data = buffer->data();
2146 size = buffer->size();
2147
2148 Parcel parcel;
2149 if (size > 0) {
2150 CHECK(buffer->meta()->findInt64("timeUs", &timeUs));
2151 flag |= TextDescriptions::IN_BAND_TEXT_3GPP;
2152 TextDescriptions::getParcelOfDescriptions(
2153 (const uint8_t *)data, size, flag, timeUs / 1000, &parcel);
2154 }
2155
2156 if ((parcel.dataSize() > 0)) {
2157 notifyListener(MEDIA_TIMED_TEXT, 0, 0, &parcel);
2158 } else { // send an empty timed text
2159 notifyListener(MEDIA_TIMED_TEXT, 0, 0);
2160 }
2161}
Andreas Huberb5f25f02013-02-05 10:14:26 -08002162////////////////////////////////////////////////////////////////////////////////
2163
Chong Zhangced1c2f2014-08-08 15:22:35 -07002164sp<AMessage> NuPlayer::Source::getFormat(bool audio) {
2165 sp<MetaData> meta = getFormatMeta(audio);
2166
2167 if (meta == NULL) {
2168 return NULL;
2169 }
2170
2171 sp<AMessage> msg = new AMessage;
2172
2173 if(convertMetaDataToMessage(meta, &msg) == OK) {
2174 return msg;
2175 }
2176 return NULL;
2177}
2178
Andreas Huber9575c962013-02-05 13:59:56 -08002179void NuPlayer::Source::notifyFlagsChanged(uint32_t flags) {
2180 sp<AMessage> notify = dupNotify();
2181 notify->setInt32("what", kWhatFlagsChanged);
2182 notify->setInt32("flags", flags);
2183 notify->post();
2184}
2185
Chong Zhangced1c2f2014-08-08 15:22:35 -07002186void NuPlayer::Source::notifyVideoSizeChanged(const sp<AMessage> &format) {
Andreas Huber9575c962013-02-05 13:59:56 -08002187 sp<AMessage> notify = dupNotify();
2188 notify->setInt32("what", kWhatVideoSizeChanged);
Chong Zhangced1c2f2014-08-08 15:22:35 -07002189 notify->setMessage("format", format);
Andreas Huber9575c962013-02-05 13:59:56 -08002190 notify->post();
2191}
2192
Andreas Huberec0c5972013-02-05 14:47:13 -08002193void NuPlayer::Source::notifyPrepared(status_t err) {
Andreas Huber9575c962013-02-05 13:59:56 -08002194 sp<AMessage> notify = dupNotify();
2195 notify->setInt32("what", kWhatPrepared);
Andreas Huberec0c5972013-02-05 14:47:13 -08002196 notify->setInt32("err", err);
Andreas Huber9575c962013-02-05 13:59:56 -08002197 notify->post();
2198}
2199
Andreas Huber84333e02014-02-07 15:36:10 -08002200void NuPlayer::Source::onMessageReceived(const sp<AMessage> & /* msg */) {
Andreas Huberb5f25f02013-02-05 10:14:26 -08002201 TRESPASS();
2202}
2203
Andreas Huberf9334412010-12-15 15:17:42 -08002204} // namespace android