blob: a63a9406e6b16db6b343ed02ecd275668e3c87c6 [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 {
Wei Jiae427abf2014-09-22 15:21:11 -070067 SeekAction(int64_t seekTimeUs, bool needNotify)
68 : mSeekTimeUs(seekTimeUs),
69 mNeedNotify(needNotify) {
Andreas Hubera1f8ab02012-11-30 10:53:22 -080070 }
71
72 virtual void execute(NuPlayer *player) {
Wei Jiae427abf2014-09-22 15:21:11 -070073 player->performSeek(mSeekTimeUs, mNeedNotify);
Andreas Hubera1f8ab02012-11-30 10:53:22 -080074 }
75
76private:
77 int64_t mSeekTimeUs;
Wei Jiae427abf2014-09-22 15:21:11 -070078 bool mNeedNotify;
Andreas Hubera1f8ab02012-11-30 10:53:22 -080079
80 DISALLOW_EVIL_CONSTRUCTORS(SeekAction);
81};
82
Andreas Huber57a339c2012-12-03 11:18:00 -080083struct NuPlayer::SetSurfaceAction : public Action {
84 SetSurfaceAction(const sp<NativeWindowWrapper> &wrapper)
85 : mWrapper(wrapper) {
86 }
87
88 virtual void execute(NuPlayer *player) {
89 player->performSetSurface(mWrapper);
90 }
91
92private:
93 sp<NativeWindowWrapper> mWrapper;
94
95 DISALLOW_EVIL_CONSTRUCTORS(SetSurfaceAction);
96};
97
Andreas Huber14f76722013-01-15 09:04:18 -080098struct NuPlayer::ShutdownDecoderAction : public Action {
99 ShutdownDecoderAction(bool audio, bool video)
100 : mAudio(audio),
101 mVideo(video) {
102 }
103
104 virtual void execute(NuPlayer *player) {
105 player->performDecoderShutdown(mAudio, mVideo);
106 }
107
108private:
109 bool mAudio;
110 bool mVideo;
111
112 DISALLOW_EVIL_CONSTRUCTORS(ShutdownDecoderAction);
113};
114
115struct NuPlayer::PostMessageAction : public Action {
116 PostMessageAction(const sp<AMessage> &msg)
117 : mMessage(msg) {
118 }
119
120 virtual void execute(NuPlayer *) {
121 mMessage->post();
122 }
123
124private:
125 sp<AMessage> mMessage;
126
127 DISALLOW_EVIL_CONSTRUCTORS(PostMessageAction);
128};
129
Andreas Hubera1f8ab02012-11-30 10:53:22 -0800130// Use this if there's no state necessary to save in order to execute
131// the action.
132struct NuPlayer::SimpleAction : public Action {
133 typedef void (NuPlayer::*ActionFunc)();
134
135 SimpleAction(ActionFunc func)
136 : mFunc(func) {
137 }
138
139 virtual void execute(NuPlayer *player) {
140 (player->*mFunc)();
141 }
142
143private:
144 ActionFunc mFunc;
145
146 DISALLOW_EVIL_CONSTRUCTORS(SimpleAction);
147};
148
Andreas Huberf9334412010-12-15 15:17:42 -0800149////////////////////////////////////////////////////////////////////////////////
150
151NuPlayer::NuPlayer()
Andreas Huber9b80c2b2011-06-30 15:47:02 -0700152 : mUIDValid(false),
Andreas Huber9575c962013-02-05 13:59:56 -0800153 mSourceFlags(0),
Andreas Huber3fe62152011-09-16 15:09:22 -0700154 mVideoIsAVC(false),
Wei Jiabc2fb722014-07-08 16:37:57 -0700155 mOffloadAudio(false),
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),
Andreas Huber3fe62152011-09-16 15:09:22 -0700170 mNumFramesTotal(0ll),
James Dong0d268a32012-08-31 12:18:27 -0700171 mNumFramesDropped(0ll),
Andreas Huber57a339c2012-12-03 11:18:00 -0800172 mVideoScalingMode(NATIVE_WINDOW_SCALING_MODE_SCALE_TO_WINDOW),
173 mStarted(false) {
Andy Hung8d121d42014-10-03 09:53:53 -0700174 clearFlushComplete();
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
Wei Jiae427abf2014-09-22 15:21:11 -0700327void NuPlayer::seekToAsync(int64_t seekTimeUs, bool needNotify) {
Andreas Huber43c3e6c2011-01-05 12:17:08 -0800328 sp<AMessage> msg = new AMessage(kWhatSeek, id());
329 msg->setInt64("seekTimeUs", seekTimeUs);
Wei Jiae427abf2014-09-22 15:21:11 -0700330 msg->setInt32("needNotify", needNotify);
Andreas Huber43c3e6c2011-01-05 12:17:08 -0800331 msg->post();
332}
333
Andreas Huber53df1a42010-12-22 10:03:04 -0800334
Chong Zhang404fced2014-06-11 14:45:31 -0700335void NuPlayer::writeTrackInfo(
336 Parcel* reply, const sp<AMessage> format) const {
337 int32_t trackType;
338 CHECK(format->findInt32("type", &trackType));
339
340 AString lang;
341 CHECK(format->findString("language", &lang));
342
343 reply->writeInt32(2); // write something non-zero
344 reply->writeInt32(trackType);
345 reply->writeString16(String16(lang.c_str()));
346
347 if (trackType == MEDIA_TRACK_TYPE_SUBTITLE) {
348 AString mime;
349 CHECK(format->findString("mime", &mime));
350
351 int32_t isAuto, isDefault, isForced;
352 CHECK(format->findInt32("auto", &isAuto));
353 CHECK(format->findInt32("default", &isDefault));
354 CHECK(format->findInt32("forced", &isForced));
355
356 reply->writeString16(String16(mime.c_str()));
357 reply->writeInt32(isAuto);
358 reply->writeInt32(isDefault);
359 reply->writeInt32(isForced);
360 }
361}
362
Andreas Huberf9334412010-12-15 15:17:42 -0800363void NuPlayer::onMessageReceived(const sp<AMessage> &msg) {
364 switch (msg->what()) {
365 case kWhatSetDataSource:
366 {
Steve Block3856b092011-10-20 11:56:00 +0100367 ALOGV("kWhatSetDataSource");
Andreas Huberf9334412010-12-15 15:17:42 -0800368
369 CHECK(mSource == NULL);
370
Chong Zhang3de157d2014-08-05 20:54:44 -0700371 status_t err = OK;
Andreas Huber5bc087c2010-12-23 10:27:40 -0800372 sp<RefBase> obj;
373 CHECK(msg->findObject("source", &obj));
Chong Zhang3de157d2014-08-05 20:54:44 -0700374 if (obj != NULL) {
375 mSource = static_cast<Source *>(obj.get());
Chong Zhang3de157d2014-08-05 20:54:44 -0700376 } else {
377 err = UNKNOWN_ERROR;
378 }
Andreas Huber9575c962013-02-05 13:59:56 -0800379
380 CHECK(mDriver != NULL);
381 sp<NuPlayerDriver> driver = mDriver.promote();
382 if (driver != NULL) {
Chong Zhang3de157d2014-08-05 20:54:44 -0700383 driver->notifySetDataSourceCompleted(err);
Andreas Huber9575c962013-02-05 13:59:56 -0800384 }
385 break;
386 }
387
388 case kWhatPrepare:
389 {
390 mSource->prepareAsync();
Andreas Huberf9334412010-12-15 15:17:42 -0800391 break;
392 }
393
Chong Zhangdcb89b32013-08-06 09:44:47 -0700394 case kWhatGetTrackInfo:
395 {
396 uint32_t replyID;
397 CHECK(msg->senderAwaitsResponse(&replyID));
398
Chong Zhang404fced2014-06-11 14:45:31 -0700399 Parcel* reply;
400 CHECK(msg->findPointer("reply", (void**)&reply));
401
402 size_t inbandTracks = 0;
Chong Zhangdcb89b32013-08-06 09:44:47 -0700403 if (mSource != NULL) {
Chong Zhang404fced2014-06-11 14:45:31 -0700404 inbandTracks = mSource->getTrackCount();
405 }
406
Chong Zhanga7fa1d92014-06-11 14:49:23 -0700407 size_t ccTracks = 0;
408 if (mCCDecoder != NULL) {
409 ccTracks = mCCDecoder->getTrackCount();
410 }
411
Chong Zhang404fced2014-06-11 14:45:31 -0700412 // total track count
Chong Zhanga7fa1d92014-06-11 14:49:23 -0700413 reply->writeInt32(inbandTracks + ccTracks);
Chong Zhang404fced2014-06-11 14:45:31 -0700414
415 // write inband tracks
416 for (size_t i = 0; i < inbandTracks; ++i) {
417 writeTrackInfo(reply, mSource->getTrackInfo(i));
Chong Zhangdcb89b32013-08-06 09:44:47 -0700418 }
419
Chong Zhanga7fa1d92014-06-11 14:49:23 -0700420 // write CC track
421 for (size_t i = 0; i < ccTracks; ++i) {
422 writeTrackInfo(reply, mCCDecoder->getTrackInfo(i));
423 }
424
Chong Zhangdcb89b32013-08-06 09:44:47 -0700425 sp<AMessage> response = new AMessage;
Chong Zhangdcb89b32013-08-06 09:44:47 -0700426 response->postReply(replyID);
427 break;
428 }
429
Robert Shih7c4f0d72014-07-09 18:53:31 -0700430 case kWhatGetSelectedTrack:
431 {
432 status_t err = INVALID_OPERATION;
433 if (mSource != NULL) {
434 err = OK;
435
436 int32_t type32;
437 CHECK(msg->findInt32("type", (int32_t*)&type32));
438 media_track_type type = (media_track_type)type32;
439 ssize_t selectedTrack = mSource->getSelectedTrack(type);
440
441 Parcel* reply;
442 CHECK(msg->findPointer("reply", (void**)&reply));
443 reply->writeInt32(selectedTrack);
444 }
445
446 sp<AMessage> response = new AMessage;
447 response->setInt32("err", err);
448
449 uint32_t replyID;
450 CHECK(msg->senderAwaitsResponse(&replyID));
451 response->postReply(replyID);
452 break;
453 }
454
Chong Zhangdcb89b32013-08-06 09:44:47 -0700455 case kWhatSelectTrack:
456 {
457 uint32_t replyID;
458 CHECK(msg->senderAwaitsResponse(&replyID));
459
Chong Zhang404fced2014-06-11 14:45:31 -0700460 size_t trackIndex;
461 int32_t select;
462 CHECK(msg->findSize("trackIndex", &trackIndex));
463 CHECK(msg->findInt32("select", &select));
464
Chong Zhangdcb89b32013-08-06 09:44:47 -0700465 status_t err = INVALID_OPERATION;
Chong Zhang404fced2014-06-11 14:45:31 -0700466
467 size_t inbandTracks = 0;
Chong Zhangdcb89b32013-08-06 09:44:47 -0700468 if (mSource != NULL) {
Chong Zhang404fced2014-06-11 14:45:31 -0700469 inbandTracks = mSource->getTrackCount();
470 }
Chong Zhanga7fa1d92014-06-11 14:49:23 -0700471 size_t ccTracks = 0;
472 if (mCCDecoder != NULL) {
473 ccTracks = mCCDecoder->getTrackCount();
474 }
Chong Zhang404fced2014-06-11 14:45:31 -0700475
476 if (trackIndex < inbandTracks) {
Chong Zhangdcb89b32013-08-06 09:44:47 -0700477 err = mSource->selectTrack(trackIndex, select);
Robert Shihd3b0bbb2014-07-23 15:00:25 -0700478
479 if (!select && err == OK) {
480 int32_t type;
481 sp<AMessage> info = mSource->getTrackInfo(trackIndex);
482 if (info != NULL
483 && info->findInt32("type", &type)
484 && type == MEDIA_TRACK_TYPE_TIMEDTEXT) {
485 ++mTimedTextGeneration;
486 }
487 }
Chong Zhanga7fa1d92014-06-11 14:49:23 -0700488 } else {
489 trackIndex -= inbandTracks;
490
491 if (trackIndex < ccTracks) {
492 err = mCCDecoder->selectTrack(trackIndex, select);
493 }
Chong Zhangdcb89b32013-08-06 09:44:47 -0700494 }
495
496 sp<AMessage> response = new AMessage;
497 response->setInt32("err", err);
498
499 response->postReply(replyID);
500 break;
501 }
502
Andreas Huberb7c8e912012-11-27 15:02:53 -0800503 case kWhatPollDuration:
504 {
505 int32_t generation;
506 CHECK(msg->findInt32("generation", &generation));
507
508 if (generation != mPollDurationGeneration) {
509 // stale
510 break;
511 }
512
513 int64_t durationUs;
514 if (mDriver != NULL && mSource->getDuration(&durationUs) == OK) {
515 sp<NuPlayerDriver> driver = mDriver.promote();
516 if (driver != NULL) {
517 driver->notifyDuration(durationUs);
518 }
519 }
520
521 msg->post(1000000ll); // poll again in a second.
522 break;
523 }
524
Glenn Kasten11731182011-02-08 17:26:17 -0800525 case kWhatSetVideoNativeWindow:
Andreas Huberf9334412010-12-15 15:17:42 -0800526 {
Steve Block3856b092011-10-20 11:56:00 +0100527 ALOGV("kWhatSetVideoNativeWindow");
Andreas Huberf9334412010-12-15 15:17:42 -0800528
Andreas Huber57a339c2012-12-03 11:18:00 -0800529 mDeferredActions.push_back(
Andreas Huber14f76722013-01-15 09:04:18 -0800530 new ShutdownDecoderAction(
531 false /* audio */, true /* video */));
Andreas Huber57a339c2012-12-03 11:18:00 -0800532
Andreas Huberf9334412010-12-15 15:17:42 -0800533 sp<RefBase> obj;
Glenn Kasten11731182011-02-08 17:26:17 -0800534 CHECK(msg->findObject("native-window", &obj));
Andreas Huberf9334412010-12-15 15:17:42 -0800535
Andreas Huber57a339c2012-12-03 11:18:00 -0800536 mDeferredActions.push_back(
537 new SetSurfaceAction(
538 static_cast<NativeWindowWrapper *>(obj.get())));
James Dong0d268a32012-08-31 12:18:27 -0700539
Andreas Huber57a339c2012-12-03 11:18:00 -0800540 if (obj != NULL) {
Wei Jia36f38982014-09-25 15:27:04 -0700541 if (mStarted && mSource->getFormat(false /* audio */) != NULL) {
Andy Hung73535852014-09-05 11:42:58 -0700542 // Issue a seek to refresh the video screen only if started otherwise
543 // the extractor may not yet be started and will assert.
544 // If the video decoder is not set (perhaps audio only in this case)
545 // do not perform a seek as it is not needed.
Ronghua Wua73d9e02014-10-08 15:13:29 -0700546 int64_t currentPositionUs = 0;
547 if (getCurrentPosition(&currentPositionUs) == OK) {
548 mDeferredActions.push_back(
549 new SeekAction(currentPositionUs, false /* needNotify */));
550 }
Andy Hung73535852014-09-05 11:42:58 -0700551 }
Wei Jiaac428aa2014-09-02 19:01:34 -0700552
Andreas Huber57a339c2012-12-03 11:18:00 -0800553 // If there is a new surface texture, instantiate decoders
554 // again if possible.
555 mDeferredActions.push_back(
556 new SimpleAction(&NuPlayer::performScanSources));
557 }
558
559 processDeferredActions();
Andreas Huberf9334412010-12-15 15:17:42 -0800560 break;
561 }
562
563 case kWhatSetAudioSink:
564 {
Steve Block3856b092011-10-20 11:56:00 +0100565 ALOGV("kWhatSetAudioSink");
Andreas Huberf9334412010-12-15 15:17:42 -0800566
567 sp<RefBase> obj;
568 CHECK(msg->findObject("sink", &obj));
569
570 mAudioSink = static_cast<MediaPlayerBase::AudioSink *>(obj.get());
571 break;
572 }
573
574 case kWhatStart:
575 {
Steve Block3856b092011-10-20 11:56:00 +0100576 ALOGV("kWhatStart");
Andreas Huber43c3e6c2011-01-05 12:17:08 -0800577
Andreas Huber3fe62152011-09-16 15:09:22 -0700578 mVideoIsAVC = false;
Wei Jiabc2fb722014-07-08 16:37:57 -0700579 mOffloadAudio = false;
Andreas Huber1aef2112011-01-04 14:01:29 -0800580 mAudioEOS = false;
581 mVideoEOS = false;
Andreas Huber32f3cef2011-03-02 15:34:46 -0800582 mSkipRenderingAudioUntilMediaTimeUs = -1;
583 mSkipRenderingVideoUntilMediaTimeUs = -1;
Andreas Huber3fe62152011-09-16 15:09:22 -0700584 mNumFramesTotal = 0;
585 mNumFramesDropped = 0;
Andreas Huber57a339c2012-12-03 11:18:00 -0800586 mStarted = true;
Andreas Huber1aef2112011-01-04 14:01:29 -0800587
Lajos Molnar09524832014-07-17 14:29:51 -0700588 /* instantiate decoders now for secure playback */
589 if (mSourceFlags & Source::FLAG_SECURE) {
590 if (mNativeWindow != NULL) {
591 instantiateDecoder(false, &mVideoDecoder);
592 }
593
594 if (mAudioSink != NULL) {
595 instantiateDecoder(true, &mAudioDecoder);
596 }
597 }
598
Andreas Huber5bc087c2010-12-23 10:27:40 -0800599 mSource->start();
Andreas Huberf9334412010-12-15 15:17:42 -0800600
Andreas Huberd5e56232013-03-12 11:01:43 -0700601 uint32_t flags = 0;
602
603 if (mSource->isRealTime()) {
604 flags |= Renderer::FLAG_REAL_TIME;
605 }
606
Wei Jiabc2fb722014-07-08 16:37:57 -0700607 sp<MetaData> audioMeta = mSource->getFormatMeta(true /* audio */);
608 audio_stream_type_t streamType = AUDIO_STREAM_MUSIC;
609 if (mAudioSink != NULL) {
610 streamType = mAudioSink->getAudioStreamType();
611 }
612
613 sp<AMessage> videoFormat = mSource->getFormat(false /* audio */);
614
615 mOffloadAudio =
616 canOffloadStream(audioMeta, (videoFormat != NULL),
617 true /* is_streaming */, streamType);
618 if (mOffloadAudio) {
619 flags |= Renderer::FLAG_OFFLOAD_AUDIO;
620 }
621
Wei Jia57568df2014-09-22 10:16:29 -0700622 sp<AMessage> notify = new AMessage(kWhatRendererNotify, id());
623 ++mRendererGeneration;
624 notify->setInt32("generation", mRendererGeneration);
625 mRenderer = new Renderer(mAudioSink, notify, flags);
Andreas Huberf9334412010-12-15 15:17:42 -0800626
Lajos Molnar09524832014-07-17 14:29:51 -0700627 mRendererLooper = new ALooper;
628 mRendererLooper->setName("NuPlayerRenderer");
629 mRendererLooper->start(false, false, ANDROID_PRIORITY_AUDIO);
630 mRendererLooper->registerHandler(mRenderer);
Andreas Huberf9334412010-12-15 15:17:42 -0800631
Lajos Molnarc851b5d2014-09-18 14:14:29 -0700632 sp<MetaData> meta = getFileMeta();
633 int32_t rate;
634 if (meta != NULL
635 && meta->findInt32(kKeyFrameRate, &rate) && rate > 0) {
636 mRenderer->setVideoFrameRate(rate);
637 }
638
Andreas Huber1aef2112011-01-04 14:01:29 -0800639 postScanSources();
Andreas Huberf9334412010-12-15 15:17:42 -0800640 break;
641 }
642
643 case kWhatScanSources:
644 {
Andreas Huber1aef2112011-01-04 14:01:29 -0800645 int32_t generation;
646 CHECK(msg->findInt32("generation", &generation));
647 if (generation != mScanSourcesGeneration) {
648 // Drop obsolete msg.
649 break;
650 }
651
Andreas Huber5bc087c2010-12-23 10:27:40 -0800652 mScanSourcesPending = false;
653
Steve Block3856b092011-10-20 11:56:00 +0100654 ALOGV("scanning sources haveAudio=%d, haveVideo=%d",
Andreas Huber43c3e6c2011-01-05 12:17:08 -0800655 mAudioDecoder != NULL, mVideoDecoder != NULL);
656
Andreas Huberb7c8e912012-11-27 15:02:53 -0800657 bool mHadAnySourcesBefore =
658 (mAudioDecoder != NULL) || (mVideoDecoder != NULL);
659
Andy Hung282a7e32014-08-14 15:56:34 -0700660 // initialize video before audio because successful initialization of
661 // video may change deep buffer mode of audio.
Haynes Mathew George5d246ef2012-07-09 10:36:57 -0700662 if (mNativeWindow != NULL) {
663 instantiateDecoder(false, &mVideoDecoder);
664 }
Andreas Huberf9334412010-12-15 15:17:42 -0800665
666 if (mAudioSink != NULL) {
Andy Hung282a7e32014-08-14 15:56:34 -0700667 if (mOffloadAudio) {
668 // open audio sink early under offload mode.
669 sp<AMessage> format = mSource->getFormat(true /*audio*/);
670 openAudioSink(format, true /*offloadOnly*/);
671 }
Andreas Huber5bc087c2010-12-23 10:27:40 -0800672 instantiateDecoder(true, &mAudioDecoder);
Andreas Huberf9334412010-12-15 15:17:42 -0800673 }
674
Andreas Huberb7c8e912012-11-27 15:02:53 -0800675 if (!mHadAnySourcesBefore
676 && (mAudioDecoder != NULL || mVideoDecoder != NULL)) {
677 // This is the first time we've found anything playable.
678
Andreas Huber9575c962013-02-05 13:59:56 -0800679 if (mSourceFlags & Source::FLAG_DYNAMIC_DURATION) {
Andreas Huberb7c8e912012-11-27 15:02:53 -0800680 schedulePollDuration();
681 }
682 }
683
Andreas Hubereac68ba2011-09-27 12:12:25 -0700684 status_t err;
685 if ((err = mSource->feedMoreTSData()) != OK) {
Andreas Huber1aef2112011-01-04 14:01:29 -0800686 if (mAudioDecoder == NULL && mVideoDecoder == NULL) {
687 // We're not currently decoding anything (no audio or
688 // video tracks found) and we just ran out of input data.
Andreas Hubereac68ba2011-09-27 12:12:25 -0700689
690 if (err == ERROR_END_OF_STREAM) {
691 notifyListener(MEDIA_PLAYBACK_COMPLETE, 0, 0);
692 } else {
693 notifyListener(MEDIA_ERROR, MEDIA_ERROR_UNKNOWN, err);
694 }
Andreas Huber1aef2112011-01-04 14:01:29 -0800695 }
Andreas Huberf9334412010-12-15 15:17:42 -0800696 break;
697 }
698
Andreas Huberfbe9d812012-08-31 14:05:27 -0700699 if ((mAudioDecoder == NULL && mAudioSink != NULL)
700 || (mVideoDecoder == NULL && mNativeWindow != NULL)) {
Andreas Huberf9334412010-12-15 15:17:42 -0800701 msg->post(100000ll);
Andreas Huber5bc087c2010-12-23 10:27:40 -0800702 mScanSourcesPending = true;
Andreas Huberf9334412010-12-15 15:17:42 -0800703 }
704 break;
705 }
706
707 case kWhatVideoNotify:
708 case kWhatAudioNotify:
709 {
710 bool audio = msg->what() == kWhatAudioNotify;
711
Wei Jia88703c32014-08-06 11:24:07 -0700712 int32_t currentDecoderGeneration =
713 (audio? mAudioDecoderGeneration : mVideoDecoderGeneration);
714 int32_t requesterGeneration = currentDecoderGeneration - 1;
715 CHECK(msg->findInt32("generation", &requesterGeneration));
716
717 if (requesterGeneration != currentDecoderGeneration) {
718 ALOGV("got message from old %s decoder, generation(%d:%d)",
719 audio ? "audio" : "video", requesterGeneration,
720 currentDecoderGeneration);
721 sp<AMessage> reply;
722 if (!(msg->findMessage("reply", &reply))) {
723 return;
724 }
725
726 reply->setInt32("err", INFO_DISCONTINUITY);
727 reply->post();
728 return;
729 }
730
Andreas Huberf9334412010-12-15 15:17:42 -0800731 int32_t what;
Lajos Molnar1cd13982014-01-17 15:12:51 -0800732 CHECK(msg->findInt32("what", &what));
Andreas Huberf9334412010-12-15 15:17:42 -0800733
Lajos Molnar1cd13982014-01-17 15:12:51 -0800734 if (what == Decoder::kWhatFillThisBuffer) {
Andreas Huberf9334412010-12-15 15:17:42 -0800735 status_t err = feedDecoderInputData(
Lajos Molnar1cd13982014-01-17 15:12:51 -0800736 audio, msg);
Andreas Huberf9334412010-12-15 15:17:42 -0800737
Andreas Huber5bc087c2010-12-23 10:27:40 -0800738 if (err == -EWOULDBLOCK) {
Andreas Hubereac68ba2011-09-27 12:12:25 -0700739 if (mSource->feedMoreTSData() == OK) {
Phil Burkc5cc2e22014-09-09 20:08:39 -0700740 msg->post(10 * 1000ll);
Andreas Huber5bc087c2010-12-23 10:27:40 -0800741 }
Andreas Huberf9334412010-12-15 15:17:42 -0800742 }
Lajos Molnar1cd13982014-01-17 15:12:51 -0800743 } else if (what == Decoder::kWhatEOS) {
Andreas Huberdc9bacd2011-09-26 10:53:29 -0700744 int32_t err;
Lajos Molnar1cd13982014-01-17 15:12:51 -0800745 CHECK(msg->findInt32("err", &err));
Andreas Huberdc9bacd2011-09-26 10:53:29 -0700746
747 if (err == ERROR_END_OF_STREAM) {
Steve Block3856b092011-10-20 11:56:00 +0100748 ALOGV("got %s decoder EOS", audio ? "audio" : "video");
Andreas Huberdc9bacd2011-09-26 10:53:29 -0700749 } else {
Steve Block3856b092011-10-20 11:56:00 +0100750 ALOGV("got %s decoder EOS w/ error %d",
Andreas Huberdc9bacd2011-09-26 10:53:29 -0700751 audio ? "audio" : "video",
752 err);
753 }
754
755 mRenderer->queueEOS(audio, err);
Lajos Molnar1cd13982014-01-17 15:12:51 -0800756 } else if (what == Decoder::kWhatFlushCompleted) {
Steve Block3856b092011-10-20 11:56:00 +0100757 ALOGV("decoder %s flush completed", audio ? "audio" : "video");
Andreas Huberf9334412010-12-15 15:17:42 -0800758
Andy Hung8d121d42014-10-03 09:53:53 -0700759 handleFlushComplete(audio, true /* isDecoder */);
Andreas Huber3831a062010-12-21 10:22:33 -0800760 finishFlushIfPossible();
Lajos Molnar1cd13982014-01-17 15:12:51 -0800761 } else if (what == Decoder::kWhatOutputFormatChanged) {
762 sp<AMessage> format;
763 CHECK(msg->findMessage("format", &format));
764
Andreas Huber31e25082011-01-10 10:38:31 -0800765 if (audio) {
Andy Hung282a7e32014-08-14 15:56:34 -0700766 openAudioSink(format, false /*offloadOnly*/);
Andreas Huber31e25082011-01-10 10:38:31 -0800767 } else {
768 // video
Chong Zhangced1c2f2014-08-08 15:22:35 -0700769 sp<AMessage> inputFormat =
770 mSource->getFormat(false /* audio */);
Andreas Huber3831a062010-12-21 10:22:33 -0800771
Chong Zhangced1c2f2014-08-08 15:22:35 -0700772 updateVideoSize(inputFormat, format);
Andreas Huber31e25082011-01-10 10:38:31 -0800773 }
Lajos Molnar1cd13982014-01-17 15:12:51 -0800774 } else if (what == Decoder::kWhatShutdownCompleted) {
Steve Block3856b092011-10-20 11:56:00 +0100775 ALOGV("%s shutdown completed", audio ? "audio" : "video");
Andreas Huber3831a062010-12-21 10:22:33 -0800776 if (audio) {
777 mAudioDecoder.clear();
Wei Jia57568df2014-09-22 10:16:29 -0700778 ++mAudioDecoderGeneration;
Andreas Huber3831a062010-12-21 10:22:33 -0800779
780 CHECK_EQ((int)mFlushingAudio, (int)SHUTTING_DOWN_DECODER);
781 mFlushingAudio = SHUT_DOWN;
782 } else {
783 mVideoDecoder.clear();
Wei Jia57568df2014-09-22 10:16:29 -0700784 ++mVideoDecoderGeneration;
Andreas Huber3831a062010-12-21 10:22:33 -0800785
786 CHECK_EQ((int)mFlushingVideo, (int)SHUTTING_DOWN_DECODER);
787 mFlushingVideo = SHUT_DOWN;
788 }
789
790 finishFlushIfPossible();
Lajos Molnar1cd13982014-01-17 15:12:51 -0800791 } else if (what == Decoder::kWhatError) {
Chong Zhangf4c0a942014-08-11 15:14:10 -0700792 status_t err;
Andy Hung2abde2c2014-09-30 14:40:32 -0700793 if (!msg->findInt32("err", &err) || err == OK) {
Chong Zhangf4c0a942014-08-11 15:14:10 -0700794 err = UNKNOWN_ERROR;
795 }
Andy Hungcf31f1e2014-09-23 14:59:01 -0700796
Andy Hung2abde2c2014-09-30 14:40:32 -0700797 // Decoder errors can be due to Source (e.g. from streaming),
798 // or from decoding corrupted bitstreams, or from other decoder
799 // MediaCodec operations (e.g. from an ongoing reset or seek).
800 //
801 // We try to gracefully shut down the affected decoder if possible,
802 // rather than trying to force the shutdown with something
803 // similar to performReset(). This method can lead to a hang
804 // if MediaCodec functions block after an error, but they should
805 // typically return INVALID_OPERATION instead of blocking.
806
807 FlushStatus *flushing = audio ? &mFlushingAudio : &mFlushingVideo;
808 ALOGE("received error(%#x) from %s decoder, flushing(%d), now shutting down",
809 err, audio ? "audio" : "video", *flushing);
810
811 switch (*flushing) {
812 case NONE:
813 mDeferredActions.push_back(
814 new ShutdownDecoderAction(audio, !audio /* video */));
815 processDeferredActions();
816 break;
817 case FLUSHING_DECODER:
818 *flushing = FLUSHING_DECODER_SHUTDOWN; // initiate shutdown after flush.
819 break; // Wait for flush to complete.
820 case FLUSHING_DECODER_SHUTDOWN:
821 break; // Wait for flush to complete.
822 case SHUTTING_DOWN_DECODER:
823 break; // Wait for shutdown to complete.
824 case FLUSHED:
825 // Widevine source reads must stop before releasing the video decoder.
826 if (!audio && mSource != NULL && mSourceFlags & Source::FLAG_SECURE) {
827 mSource->stop();
828 }
829 getDecoder(audio)->initiateShutdown(); // In the middle of a seek.
830 *flushing = SHUTTING_DOWN_DECODER; // Shut down.
831 break;
832 case SHUT_DOWN:
833 finishFlushIfPossible(); // Should not occur.
834 break; // Finish anyways.
Marco Nelissen9e2b7912014-08-18 16:13:03 -0700835 }
Andy Hung2abde2c2014-09-30 14:40:32 -0700836 notifyListener(MEDIA_ERROR, MEDIA_ERROR_UNKNOWN, err);
Lajos Molnar1cd13982014-01-17 15:12:51 -0800837 } else if (what == Decoder::kWhatDrainThisBuffer) {
838 renderBuffer(audio, msg);
839 } else {
840 ALOGV("Unhandled decoder notification %d '%c%c%c%c'.",
Andreas Hubera1f8ab02012-11-30 10:53:22 -0800841 what,
842 what >> 24,
843 (what >> 16) & 0xff,
844 (what >> 8) & 0xff,
845 what & 0xff);
Andreas Huberf9334412010-12-15 15:17:42 -0800846 }
847
848 break;
849 }
850
851 case kWhatRendererNotify:
852 {
Wei Jia57568df2014-09-22 10:16:29 -0700853 int32_t requesterGeneration = mRendererGeneration - 1;
854 CHECK(msg->findInt32("generation", &requesterGeneration));
855 if (requesterGeneration != mRendererGeneration) {
856 ALOGV("got message from old renderer, generation(%d:%d)",
857 requesterGeneration, mRendererGeneration);
858 return;
859 }
860
Andreas Huberf9334412010-12-15 15:17:42 -0800861 int32_t what;
862 CHECK(msg->findInt32("what", &what));
863
864 if (what == Renderer::kWhatEOS) {
865 int32_t audio;
866 CHECK(msg->findInt32("audio", &audio));
867
Andreas Huberc92fd242011-08-16 13:48:44 -0700868 int32_t finalResult;
869 CHECK(msg->findInt32("finalResult", &finalResult));
870
Andreas Huberf9334412010-12-15 15:17:42 -0800871 if (audio) {
872 mAudioEOS = true;
873 } else {
874 mVideoEOS = true;
875 }
876
Andreas Huberc92fd242011-08-16 13:48:44 -0700877 if (finalResult == ERROR_END_OF_STREAM) {
Steve Block3856b092011-10-20 11:56:00 +0100878 ALOGV("reached %s EOS", audio ? "audio" : "video");
Andreas Huberc92fd242011-08-16 13:48:44 -0700879 } else {
Steve Block29357bc2012-01-06 19:20:56 +0000880 ALOGE("%s track encountered an error (%d)",
Andreas Huberc92fd242011-08-16 13:48:44 -0700881 audio ? "audio" : "video", finalResult);
882
883 notifyListener(
884 MEDIA_ERROR, MEDIA_ERROR_UNKNOWN, finalResult);
885 }
Andreas Huberf9334412010-12-15 15:17:42 -0800886
887 if ((mAudioEOS || mAudioDecoder == NULL)
888 && (mVideoEOS || mVideoDecoder == NULL)) {
889 notifyListener(MEDIA_PLAYBACK_COMPLETE, 0, 0);
890 }
Andreas Huber3fe62152011-09-16 15:09:22 -0700891 } else if (what == Renderer::kWhatFlushComplete) {
Andreas Huberf9334412010-12-15 15:17:42 -0800892 int32_t audio;
893 CHECK(msg->findInt32("audio", &audio));
894
Steve Block3856b092011-10-20 11:56:00 +0100895 ALOGV("renderer %s flush completed.", audio ? "audio" : "video");
Andy Hung8d121d42014-10-03 09:53:53 -0700896 handleFlushComplete(audio, false /* isDecoder */);
897 finishFlushIfPossible();
James Dongf57b4ea2012-07-20 13:38:36 -0700898 } else if (what == Renderer::kWhatVideoRenderingStart) {
899 notifyListener(MEDIA_INFO, MEDIA_INFO_RENDERING_START, 0);
Lajos Molnarcbaffcf2013-08-14 18:30:38 -0700900 } else if (what == Renderer::kWhatMediaRenderingStart) {
901 ALOGV("media rendering started");
902 notifyListener(MEDIA_STARTED, 0, 0);
Wei Jia3a2956d2014-07-22 16:01:33 -0700903 } else if (what == Renderer::kWhatAudioOffloadTearDown) {
904 ALOGV("Tear down audio offload, fall back to s/w path");
905 int64_t positionUs;
906 CHECK(msg->findInt64("positionUs", &positionUs));
Ronghua Wu08529172014-10-02 16:55:52 -0700907 int32_t reason;
908 CHECK(msg->findInt32("reason", &reason));
Andy Hung282a7e32014-08-14 15:56:34 -0700909 closeAudioSink();
Wei Jia3a2956d2014-07-22 16:01:33 -0700910 mAudioDecoder.clear();
Wei Jia57568df2014-09-22 10:16:29 -0700911 ++mAudioDecoderGeneration;
Wei Jia3a2956d2014-07-22 16:01:33 -0700912 mRenderer->flush(true /* audio */);
913 if (mVideoDecoder != NULL) {
914 mRenderer->flush(false /* audio */);
915 }
916 mRenderer->signalDisableOffloadAudio();
917 mOffloadAudio = false;
918
Wei Jiae427abf2014-09-22 15:21:11 -0700919 performSeek(positionUs, false /* needNotify */);
Ronghua Wu08529172014-10-02 16:55:52 -0700920 if (reason == Renderer::kDueToError) {
921 instantiateDecoder(true /* audio */, &mAudioDecoder);
922 }
Andreas Huberf9334412010-12-15 15:17:42 -0800923 }
924 break;
925 }
926
927 case kWhatMoreDataQueued:
928 {
929 break;
930 }
931
Andreas Huber1aef2112011-01-04 14:01:29 -0800932 case kWhatReset:
933 {
Steve Block3856b092011-10-20 11:56:00 +0100934 ALOGV("kWhatReset");
Andreas Huber1aef2112011-01-04 14:01:29 -0800935
Andreas Hubera1f8ab02012-11-30 10:53:22 -0800936 mDeferredActions.push_back(
Andreas Huber14f76722013-01-15 09:04:18 -0800937 new ShutdownDecoderAction(
938 true /* audio */, true /* video */));
Andreas Huberb7c8e912012-11-27 15:02:53 -0800939
Andreas Hubera1f8ab02012-11-30 10:53:22 -0800940 mDeferredActions.push_back(
941 new SimpleAction(&NuPlayer::performReset));
Andreas Huberb58ce9f2011-11-28 16:27:35 -0800942
Andreas Hubera1f8ab02012-11-30 10:53:22 -0800943 processDeferredActions();
Andreas Huber1aef2112011-01-04 14:01:29 -0800944 break;
945 }
946
Andreas Huber43c3e6c2011-01-05 12:17:08 -0800947 case kWhatSeek:
948 {
949 int64_t seekTimeUs;
Wei Jiae427abf2014-09-22 15:21:11 -0700950 int32_t needNotify;
Andreas Huber43c3e6c2011-01-05 12:17:08 -0800951 CHECK(msg->findInt64("seekTimeUs", &seekTimeUs));
Wei Jiae427abf2014-09-22 15:21:11 -0700952 CHECK(msg->findInt32("needNotify", &needNotify));
Andreas Huber43c3e6c2011-01-05 12:17:08 -0800953
Wei Jiae427abf2014-09-22 15:21:11 -0700954 ALOGV("kWhatSeek seekTimeUs=%lld us, needNotify=%d",
955 seekTimeUs, needNotify);
Andreas Huber43c3e6c2011-01-05 12:17:08 -0800956
Andreas Hubera1f8ab02012-11-30 10:53:22 -0800957 mDeferredActions.push_back(
958 new SimpleAction(&NuPlayer::performDecoderFlush));
Andreas Huber43c3e6c2011-01-05 12:17:08 -0800959
Wei Jiae427abf2014-09-22 15:21:11 -0700960 mDeferredActions.push_back(
961 new SeekAction(seekTimeUs, needNotify));
Andreas Huber43c3e6c2011-01-05 12:17:08 -0800962
Andreas Hubera1f8ab02012-11-30 10:53:22 -0800963 processDeferredActions();
Andreas Huber43c3e6c2011-01-05 12:17:08 -0800964 break;
965 }
966
Andreas Huberb4082222011-01-20 15:23:04 -0800967 case kWhatPause:
968 {
Wei Jia7e9f7f72014-09-23 10:55:35 -0700969 if (mSource != NULL) {
970 mSource->pause();
971 } else {
972 ALOGW("pause called when source is gone or not set");
973 }
974 if (mRenderer != NULL) {
975 mRenderer->pause();
976 } else {
977 ALOGW("pause called when renderer is gone or not set");
978 }
Andreas Huberb4082222011-01-20 15:23:04 -0800979 break;
980 }
981
982 case kWhatResume:
983 {
Wei Jia7e9f7f72014-09-23 10:55:35 -0700984 if (mSource != NULL) {
985 mSource->resume();
986 } else {
987 ALOGW("resume called when source is gone or not set");
988 }
Ronghua Wud7988b12014-10-03 15:19:10 -0700989 // |mAudioDecoder| may have been released due to the pause timeout, so re-create it if
990 // needed.
991 if (audioDecoderStillNeeded() && mAudioDecoder == NULL) {
Ronghua Wu08529172014-10-02 16:55:52 -0700992 instantiateDecoder(true /* audio */, &mAudioDecoder);
993 }
Wei Jia7e9f7f72014-09-23 10:55:35 -0700994 if (mRenderer != NULL) {
995 mRenderer->resume();
996 } else {
997 ALOGW("resume called when renderer is gone or not set");
998 }
Andreas Huberb4082222011-01-20 15:23:04 -0800999 break;
1000 }
1001
Andreas Huberb5f25f02013-02-05 10:14:26 -08001002 case kWhatSourceNotify:
1003 {
Andreas Huber9575c962013-02-05 13:59:56 -08001004 onSourceNotify(msg);
Andreas Huberb5f25f02013-02-05 10:14:26 -08001005 break;
1006 }
1007
Chong Zhanga7fa1d92014-06-11 14:49:23 -07001008 case kWhatClosedCaptionNotify:
1009 {
1010 onClosedCaptionNotify(msg);
1011 break;
1012 }
1013
Andreas Huberf9334412010-12-15 15:17:42 -08001014 default:
1015 TRESPASS();
1016 break;
1017 }
1018}
1019
Ronghua Wud7988b12014-10-03 15:19:10 -07001020bool NuPlayer::audioDecoderStillNeeded() {
1021 // Audio decoder is no longer needed if it's in shut/shutting down status.
1022 return ((mFlushingAudio != SHUT_DOWN) && (mFlushingAudio != SHUTTING_DOWN_DECODER));
1023}
1024
Andy Hung8d121d42014-10-03 09:53:53 -07001025void NuPlayer::handleFlushComplete(bool audio, bool isDecoder) {
1026 // We wait for both the decoder flush and the renderer flush to complete
1027 // before entering either the FLUSHED or the SHUTTING_DOWN_DECODER state.
1028
1029 mFlushComplete[audio][isDecoder] = true;
1030 if (!mFlushComplete[audio][!isDecoder]) {
1031 return;
1032 }
1033
1034 FlushStatus *state = audio ? &mFlushingAudio : &mFlushingVideo;
1035 switch (*state) {
1036 case FLUSHING_DECODER:
1037 {
1038 *state = FLUSHED;
Andy Hung8d121d42014-10-03 09:53:53 -07001039 break;
1040 }
1041
1042 case FLUSHING_DECODER_SHUTDOWN:
1043 {
1044 *state = SHUTTING_DOWN_DECODER;
1045
1046 ALOGV("initiating %s decoder shutdown", audio ? "audio" : "video");
1047 if (!audio) {
Andy Hung8d121d42014-10-03 09:53:53 -07001048 // Widevine source reads must stop before releasing the video decoder.
1049 if (mSource != NULL && mSourceFlags & Source::FLAG_SECURE) {
1050 mSource->stop();
1051 }
1052 }
1053 getDecoder(audio)->initiateShutdown();
1054 break;
1055 }
1056
1057 default:
1058 // decoder flush completes only occur in a flushing state.
1059 LOG_ALWAYS_FATAL_IF(isDecoder, "decoder flush in invalid state %d", *state);
1060 break;
1061 }
1062}
1063
Andreas Huber3831a062010-12-21 10:22:33 -08001064void NuPlayer::finishFlushIfPossible() {
Wei Jia53904f32014-07-29 10:22:53 -07001065 if (mFlushingAudio != NONE && mFlushingAudio != FLUSHED
1066 && mFlushingAudio != SHUT_DOWN) {
Andreas Huber3831a062010-12-21 10:22:33 -08001067 return;
1068 }
1069
Wei Jia53904f32014-07-29 10:22:53 -07001070 if (mFlushingVideo != NONE && mFlushingVideo != FLUSHED
1071 && mFlushingVideo != SHUT_DOWN) {
Andreas Huber3831a062010-12-21 10:22:33 -08001072 return;
1073 }
1074
Steve Block3856b092011-10-20 11:56:00 +01001075 ALOGV("both audio and video are flushed now.");
Andreas Huber3831a062010-12-21 10:22:33 -08001076
Phil Burk9f526492014-09-03 15:04:12 -07001077 mPendingAudioAccessUnit.clear();
Phil Burkc5cc2e22014-09-09 20:08:39 -07001078 mAggregateBuffer.clear();
Phil Burk9f526492014-09-03 15:04:12 -07001079
Andreas Huber6e3d3112011-11-28 12:36:11 -08001080 if (mTimeDiscontinuityPending) {
1081 mRenderer->signalTimeDiscontinuity();
1082 mTimeDiscontinuityPending = false;
1083 }
Andreas Huber3831a062010-12-21 10:22:33 -08001084
Wei Jia53904f32014-07-29 10:22:53 -07001085 if (mAudioDecoder != NULL && mFlushingAudio == FLUSHED) {
Andreas Huber3831a062010-12-21 10:22:33 -08001086 mAudioDecoder->signalResume();
1087 }
1088
Wei Jia53904f32014-07-29 10:22:53 -07001089 if (mVideoDecoder != NULL && mFlushingVideo == FLUSHED) {
Andreas Huber3831a062010-12-21 10:22:33 -08001090 mVideoDecoder->signalResume();
1091 }
1092
1093 mFlushingAudio = NONE;
1094 mFlushingVideo = NONE;
Andreas Huber3831a062010-12-21 10:22:33 -08001095
Andy Hung8d121d42014-10-03 09:53:53 -07001096 clearFlushComplete();
1097
Andreas Hubera1f8ab02012-11-30 10:53:22 -08001098 processDeferredActions();
Andreas Huber1aef2112011-01-04 14:01:29 -08001099}
1100
1101void NuPlayer::postScanSources() {
1102 if (mScanSourcesPending) {
1103 return;
1104 }
1105
1106 sp<AMessage> msg = new AMessage(kWhatScanSources, id());
1107 msg->setInt32("generation", mScanSourcesGeneration);
1108 msg->post();
1109
1110 mScanSourcesPending = true;
1111}
1112
Andy Hung282a7e32014-08-14 15:56:34 -07001113void NuPlayer::openAudioSink(const sp<AMessage> &format, bool offloadOnly) {
Andy Hung282a7e32014-08-14 15:56:34 -07001114 uint32_t flags;
1115 int64_t durationUs;
Chong Zhang3b9eb1f2014-10-15 17:05:08 -07001116 bool hasVideo = (mVideoDecoder != NULL);
Andy Hung282a7e32014-08-14 15:56:34 -07001117 // FIXME: we should handle the case where the video decoder
1118 // is created after we receive the format change indication.
1119 // Current code will just make that we select deep buffer
1120 // with video which should not be a problem as it should
1121 // not prevent from keeping A/V sync.
Chong Zhang3b9eb1f2014-10-15 17:05:08 -07001122 if (hasVideo &&
Andy Hung282a7e32014-08-14 15:56:34 -07001123 mSource->getDuration(&durationUs) == OK &&
1124 durationUs
1125 > AUDIO_SINK_MIN_DEEP_BUFFER_DURATION_US) {
1126 flags = AUDIO_OUTPUT_FLAG_DEEP_BUFFER;
1127 } else {
1128 flags = AUDIO_OUTPUT_FLAG_NONE;
1129 }
1130
Chong Zhang3b9eb1f2014-10-15 17:05:08 -07001131 mOffloadAudio = mRenderer->openAudioSink(
1132 format, offloadOnly, hasVideo, flags);
1133
Andy Hung282a7e32014-08-14 15:56:34 -07001134 if (mOffloadAudio) {
Chong Zhang3b9eb1f2014-10-15 17:05:08 -07001135 sp<MetaData> audioMeta =
1136 mSource->getFormatMeta(true /* audio */);
1137 sendMetaDataToHal(mAudioSink, audioMeta);
Andy Hung282a7e32014-08-14 15:56:34 -07001138 }
1139}
1140
1141void NuPlayer::closeAudioSink() {
Chong Zhang3b9eb1f2014-10-15 17:05:08 -07001142 mRenderer->closeAudioSink();
Andy Hung282a7e32014-08-14 15:56:34 -07001143}
1144
Andreas Huber5bc087c2010-12-23 10:27:40 -08001145status_t NuPlayer::instantiateDecoder(bool audio, sp<Decoder> *decoder) {
Andreas Huberf9334412010-12-15 15:17:42 -08001146 if (*decoder != NULL) {
1147 return OK;
1148 }
1149
Andreas Huber84066782011-08-16 09:34:26 -07001150 sp<AMessage> format = mSource->getFormat(audio);
Andreas Huberf9334412010-12-15 15:17:42 -08001151
Andreas Huber84066782011-08-16 09:34:26 -07001152 if (format == NULL) {
Andreas Huberf9334412010-12-15 15:17:42 -08001153 return -EWOULDBLOCK;
1154 }
1155
Andreas Huber3fe62152011-09-16 15:09:22 -07001156 if (!audio) {
Andreas Huber84066782011-08-16 09:34:26 -07001157 AString mime;
1158 CHECK(format->findString("mime", &mime));
1159 mVideoIsAVC = !strcasecmp(MEDIA_MIMETYPE_VIDEO_AVC, mime.c_str());
Chong Zhanga7fa1d92014-06-11 14:49:23 -07001160
1161 sp<AMessage> ccNotify = new AMessage(kWhatClosedCaptionNotify, id());
1162 mCCDecoder = new CCDecoder(ccNotify);
Lajos Molnar09524832014-07-17 14:29:51 -07001163
1164 if (mSourceFlags & Source::FLAG_SECURE) {
1165 format->setInt32("secure", true);
1166 }
Andreas Huber3fe62152011-09-16 15:09:22 -07001167 }
1168
Wei Jiabc2fb722014-07-08 16:37:57 -07001169 if (audio) {
Wei Jia88703c32014-08-06 11:24:07 -07001170 sp<AMessage> notify = new AMessage(kWhatAudioNotify, id());
1171 ++mAudioDecoderGeneration;
1172 notify->setInt32("generation", mAudioDecoderGeneration);
1173
Wei Jiabc2fb722014-07-08 16:37:57 -07001174 if (mOffloadAudio) {
1175 *decoder = new DecoderPassThrough(notify);
1176 } else {
1177 *decoder = new Decoder(notify);
1178 }
1179 } else {
Wei Jia88703c32014-08-06 11:24:07 -07001180 sp<AMessage> notify = new AMessage(kWhatVideoNotify, id());
1181 ++mVideoDecoderGeneration;
1182 notify->setInt32("generation", mVideoDecoderGeneration);
1183
Wei Jiabc2fb722014-07-08 16:37:57 -07001184 *decoder = new Decoder(notify, mNativeWindow);
1185 }
Lajos Molnar1cd13982014-01-17 15:12:51 -08001186 (*decoder)->init();
Andreas Huber84066782011-08-16 09:34:26 -07001187 (*decoder)->configure(format);
Andreas Huberf9334412010-12-15 15:17:42 -08001188
Lajos Molnar09524832014-07-17 14:29:51 -07001189 // allocate buffers to decrypt widevine source buffers
1190 if (!audio && (mSourceFlags & Source::FLAG_SECURE)) {
1191 Vector<sp<ABuffer> > inputBufs;
1192 CHECK_EQ((*decoder)->getInputBuffers(&inputBufs), (status_t)OK);
1193
1194 Vector<MediaBuffer *> mediaBufs;
1195 for (size_t i = 0; i < inputBufs.size(); i++) {
1196 const sp<ABuffer> &buffer = inputBufs[i];
1197 MediaBuffer *mbuf = new MediaBuffer(buffer->data(), buffer->size());
1198 mediaBufs.push(mbuf);
1199 }
1200
1201 status_t err = mSource->setBuffers(audio, mediaBufs);
1202 if (err != OK) {
1203 for (size_t i = 0; i < mediaBufs.size(); ++i) {
1204 mediaBufs[i]->release();
1205 }
1206 mediaBufs.clear();
1207 ALOGE("Secure source didn't support secure mediaBufs.");
1208 return err;
1209 }
1210 }
Andreas Huberf9334412010-12-15 15:17:42 -08001211 return OK;
1212}
1213
1214status_t NuPlayer::feedDecoderInputData(bool audio, const sp<AMessage> &msg) {
1215 sp<AMessage> reply;
1216 CHECK(msg->findMessage("reply", &reply));
1217
Wei Jia53904f32014-07-29 10:22:53 -07001218 if ((audio && mFlushingAudio != NONE)
Wei Jiaf702d042014-09-09 12:08:47 -07001219 || (!audio && mFlushingVideo != NONE)
1220 || mSource == NULL) {
Wei Jiab189a5b2014-08-07 06:11:39 +00001221 reply->setInt32("err", INFO_DISCONTINUITY);
1222 reply->post();
1223 return OK;
Andreas Huberf9334412010-12-15 15:17:42 -08001224 }
1225
1226 sp<ABuffer> accessUnit;
Andreas Huberf9334412010-12-15 15:17:42 -08001227
Phil Burk9f526492014-09-03 15:04:12 -07001228 // Aggregate smaller buffers into a larger buffer.
1229 // The goal is to reduce power consumption.
Phil Burk33b51b02014-09-17 16:03:47 -07001230 // Note this will not work if the decoder requires one frame per buffer.
1231 bool doBufferAggregation = (audio && mOffloadAudio);
Phil Burk9f526492014-09-03 15:04:12 -07001232 bool needMoreData = false;
Phil Burk9f526492014-09-03 15:04:12 -07001233
Andreas Huber3fe62152011-09-16 15:09:22 -07001234 bool dropAccessUnit;
1235 do {
Phil Burk9f526492014-09-03 15:04:12 -07001236 status_t err;
1237 // Did we save an accessUnit earlier because of a discontinuity?
1238 if (audio && (mPendingAudioAccessUnit != NULL)) {
1239 accessUnit = mPendingAudioAccessUnit;
1240 mPendingAudioAccessUnit.clear();
1241 err = mPendingAudioErr;
1242 ALOGV("feedDecoderInputData() use mPendingAudioAccessUnit");
1243 } else {
1244 err = mSource->dequeueAccessUnit(audio, &accessUnit);
1245 }
Andreas Huber5bc087c2010-12-23 10:27:40 -08001246
Andreas Huber3fe62152011-09-16 15:09:22 -07001247 if (err == -EWOULDBLOCK) {
Phil Burkc5cc2e22014-09-09 20:08:39 -07001248 return err;
Andreas Huber3fe62152011-09-16 15:09:22 -07001249 } else if (err != OK) {
1250 if (err == INFO_DISCONTINUITY) {
Phil Burk33b51b02014-09-17 16:03:47 -07001251 if (doBufferAggregation && (mAggregateBuffer != NULL)) {
Phil Burk9f526492014-09-03 15:04:12 -07001252 // We already have some data so save this for later.
1253 mPendingAudioErr = err;
1254 mPendingAudioAccessUnit = accessUnit;
1255 accessUnit.clear();
1256 ALOGD("feedDecoderInputData() save discontinuity for later");
1257 break;
1258 }
Andreas Huber3fe62152011-09-16 15:09:22 -07001259 int32_t type;
1260 CHECK(accessUnit->meta()->findInt32("discontinuity", &type));
Andreas Huber53df1a42010-12-22 10:03:04 -08001261
Andreas Huber3fe62152011-09-16 15:09:22 -07001262 bool formatChange =
Andreas Huber6e3d3112011-11-28 12:36:11 -08001263 (audio &&
1264 (type & ATSParser::DISCONTINUITY_AUDIO_FORMAT))
1265 || (!audio &&
1266 (type & ATSParser::DISCONTINUITY_VIDEO_FORMAT));
Andreas Huber53df1a42010-12-22 10:03:04 -08001267
Andreas Huber6e3d3112011-11-28 12:36:11 -08001268 bool timeChange = (type & ATSParser::DISCONTINUITY_TIME) != 0;
1269
Steve Blockdf64d152012-01-04 20:05:49 +00001270 ALOGI("%s discontinuity (formatChange=%d, time=%d)",
Andreas Huber6e3d3112011-11-28 12:36:11 -08001271 audio ? "audio" : "video", formatChange, timeChange);
Andreas Huber32f3cef2011-03-02 15:34:46 -08001272
Andreas Huber3fe62152011-09-16 15:09:22 -07001273 if (audio) {
1274 mSkipRenderingAudioUntilMediaTimeUs = -1;
1275 } else {
1276 mSkipRenderingVideoUntilMediaTimeUs = -1;
1277 }
Andreas Huber32f3cef2011-03-02 15:34:46 -08001278
Andreas Huber6e3d3112011-11-28 12:36:11 -08001279 if (timeChange) {
1280 sp<AMessage> extra;
1281 if (accessUnit->meta()->findMessage("extra", &extra)
1282 && extra != NULL) {
1283 int64_t resumeAtMediaTimeUs;
1284 if (extra->findInt64(
1285 "resume-at-mediatimeUs", &resumeAtMediaTimeUs)) {
Steve Blockdf64d152012-01-04 20:05:49 +00001286 ALOGI("suppressing rendering of %s until %lld us",
Andreas Huber6e3d3112011-11-28 12:36:11 -08001287 audio ? "audio" : "video", resumeAtMediaTimeUs);
Andreas Huber3fe62152011-09-16 15:09:22 -07001288
Andreas Huber6e3d3112011-11-28 12:36:11 -08001289 if (audio) {
1290 mSkipRenderingAudioUntilMediaTimeUs =
1291 resumeAtMediaTimeUs;
1292 } else {
1293 mSkipRenderingVideoUntilMediaTimeUs =
1294 resumeAtMediaTimeUs;
1295 }
Andreas Huber3fe62152011-09-16 15:09:22 -07001296 }
Andreas Huber32f3cef2011-03-02 15:34:46 -08001297 }
1298 }
Andreas Huber3fe62152011-09-16 15:09:22 -07001299
Andreas Huber6e3d3112011-11-28 12:36:11 -08001300 mTimeDiscontinuityPending =
1301 mTimeDiscontinuityPending || timeChange;
1302
Lajos Molnar87603c02014-08-20 19:25:30 -07001303 bool seamlessFormatChange = false;
1304 sp<AMessage> newFormat = mSource->getFormat(audio);
1305 if (formatChange) {
1306 seamlessFormatChange =
1307 getDecoder(audio)->supportsSeamlessFormatChange(newFormat);
1308 // treat seamless format change separately
1309 formatChange = !seamlessFormatChange;
1310 }
1311 bool shutdownOrFlush = formatChange || timeChange;
1312
1313 // We want to queue up scan-sources only once per discontinuity.
1314 // We control this by doing it only if neither audio nor video are
1315 // flushing or shutting down. (After handling 1st discontinuity, one
1316 // of the flushing states will not be NONE.)
1317 // No need to scan sources if this discontinuity does not result
1318 // in a flush or shutdown, as the flushing state will stay NONE.
1319 if (mFlushingAudio == NONE && mFlushingVideo == NONE &&
1320 shutdownOrFlush) {
Robert Shiha2981012014-07-30 17:41:24 -07001321 // And we'll resume scanning sources once we're done
1322 // flushing.
1323 mDeferredActions.push_front(
1324 new SimpleAction(
1325 &NuPlayer::performScanSources));
1326 }
1327
Lajos Molnar87603c02014-08-20 19:25:30 -07001328 if (formatChange /* not seamless */) {
1329 // must change decoder
1330 flushDecoder(audio, /* needShutdown = */ true);
1331 } else if (timeChange) {
1332 // need to flush
1333 flushDecoder(audio, /* needShutdown = */ false, newFormat);
1334 err = OK;
1335 } else if (seamlessFormatChange) {
1336 // reuse existing decoder and don't flush
1337 updateDecoderFormatWithoutFlush(audio, newFormat);
1338 err = OK;
Andreas Huber6e3d3112011-11-28 12:36:11 -08001339 } else {
1340 // This stream is unaffected by the discontinuity
Andreas Huber6e3d3112011-11-28 12:36:11 -08001341 return -EWOULDBLOCK;
1342 }
Andreas Huber32f3cef2011-03-02 15:34:46 -08001343 }
1344
Andreas Huber3fe62152011-09-16 15:09:22 -07001345 reply->setInt32("err", err);
1346 reply->post();
1347 return OK;
Andreas Huberf9334412010-12-15 15:17:42 -08001348 }
1349
Andreas Huber3fe62152011-09-16 15:09:22 -07001350 if (!audio) {
1351 ++mNumFramesTotal;
1352 }
1353
1354 dropAccessUnit = false;
1355 if (!audio
Lajos Molnar09524832014-07-17 14:29:51 -07001356 && !(mSourceFlags & Source::FLAG_SECURE)
Ronghua Wua73d9e02014-10-08 15:13:29 -07001357 && mRenderer->getVideoLateByUs() > 100000ll
Andreas Huber3fe62152011-09-16 15:09:22 -07001358 && mVideoIsAVC
1359 && !IsAVCReferenceFrame(accessUnit)) {
1360 dropAccessUnit = true;
1361 ++mNumFramesDropped;
1362 }
Phil Burk9f526492014-09-03 15:04:12 -07001363
1364 size_t smallSize = accessUnit->size();
1365 needMoreData = false;
Phil Burkc5cc2e22014-09-09 20:08:39 -07001366 if (doBufferAggregation && (mAggregateBuffer == NULL)
Phil Burk9f526492014-09-03 15:04:12 -07001367 // Don't bother if only room for a few small buffers.
Phil Burkc5cc2e22014-09-09 20:08:39 -07001368 && (smallSize < (kAggregateBufferSizeBytes / 3))) {
Phil Burk9f526492014-09-03 15:04:12 -07001369 // Create a larger buffer for combining smaller buffers from the extractor.
Phil Burkc5cc2e22014-09-09 20:08:39 -07001370 mAggregateBuffer = new ABuffer(kAggregateBufferSizeBytes);
1371 mAggregateBuffer->setRange(0, 0); // start empty
Phil Burk9f526492014-09-03 15:04:12 -07001372 }
1373
Phil Burk33b51b02014-09-17 16:03:47 -07001374 if (doBufferAggregation && (mAggregateBuffer != NULL)) {
Phil Burk9f526492014-09-03 15:04:12 -07001375 int64_t timeUs;
Phil Burkc5cc2e22014-09-09 20:08:39 -07001376 int64_t dummy;
Phil Burk9f526492014-09-03 15:04:12 -07001377 bool smallTimestampValid = accessUnit->meta()->findInt64("timeUs", &timeUs);
Phil Burkc5cc2e22014-09-09 20:08:39 -07001378 bool bigTimestampValid = mAggregateBuffer->meta()->findInt64("timeUs", &dummy);
Phil Burk9f526492014-09-03 15:04:12 -07001379 // Will the smaller buffer fit?
Phil Burkc5cc2e22014-09-09 20:08:39 -07001380 size_t bigSize = mAggregateBuffer->size();
1381 size_t roomLeft = mAggregateBuffer->capacity() - bigSize;
Phil Burk9f526492014-09-03 15:04:12 -07001382 // Should we save this small buffer for the next big buffer?
1383 // If the first small buffer did not have a timestamp then save
1384 // any buffer that does have a timestamp until the next big buffer.
1385 if ((smallSize > roomLeft)
Phil Burkc5cc2e22014-09-09 20:08:39 -07001386 || (!bigTimestampValid && (bigSize > 0) && smallTimestampValid)) {
Phil Burk9f526492014-09-03 15:04:12 -07001387 mPendingAudioErr = err;
1388 mPendingAudioAccessUnit = accessUnit;
1389 accessUnit.clear();
1390 } else {
Phil Burkc5cc2e22014-09-09 20:08:39 -07001391 // Grab time from first small buffer if available.
1392 if ((bigSize == 0) && smallTimestampValid) {
1393 mAggregateBuffer->meta()->setInt64("timeUs", timeUs);
1394 }
Phil Burk9f526492014-09-03 15:04:12 -07001395 // Append small buffer to the bigger buffer.
Phil Burkc5cc2e22014-09-09 20:08:39 -07001396 memcpy(mAggregateBuffer->base() + bigSize, accessUnit->data(), smallSize);
Phil Burk9f526492014-09-03 15:04:12 -07001397 bigSize += smallSize;
Phil Burkc5cc2e22014-09-09 20:08:39 -07001398 mAggregateBuffer->setRange(0, bigSize);
Phil Burk9f526492014-09-03 15:04:12 -07001399
Phil Burkc5cc2e22014-09-09 20:08:39 -07001400 // Keep looping until we run out of room in the mAggregateBuffer.
Phil Burk9f526492014-09-03 15:04:12 -07001401 needMoreData = true;
1402
Phil Burkc5cc2e22014-09-09 20:08:39 -07001403 ALOGV("feedDecoderInputData() smallSize = %zu, bigSize = %zu, capacity = %zu",
1404 smallSize, bigSize, mAggregateBuffer->capacity());
Phil Burk9f526492014-09-03 15:04:12 -07001405 }
1406 }
1407 } while (dropAccessUnit || needMoreData);
Andreas Huberf9334412010-12-15 15:17:42 -08001408
Steve Block3856b092011-10-20 11:56:00 +01001409 // ALOGV("returned a valid buffer of %s data", audio ? "audio" : "video");
Andreas Huberf9334412010-12-15 15:17:42 -08001410
1411#if 0
1412 int64_t mediaTimeUs;
1413 CHECK(accessUnit->meta()->findInt64("timeUs", &mediaTimeUs));
Steve Block3856b092011-10-20 11:56:00 +01001414 ALOGV("feeding %s input buffer at media time %.2f secs",
Andreas Huberf9334412010-12-15 15:17:42 -08001415 audio ? "audio" : "video",
1416 mediaTimeUs / 1E6);
1417#endif
1418
Chong Zhanga7fa1d92014-06-11 14:49:23 -07001419 if (!audio) {
1420 mCCDecoder->decode(accessUnit);
1421 }
1422
Phil Burk33b51b02014-09-17 16:03:47 -07001423 if (doBufferAggregation && (mAggregateBuffer != NULL)) {
Phil Burkc5cc2e22014-09-09 20:08:39 -07001424 ALOGV("feedDecoderInputData() reply with aggregated buffer, %zu",
1425 mAggregateBuffer->size());
1426 reply->setBuffer("buffer", mAggregateBuffer);
1427 mAggregateBuffer.clear();
Phil Burk9f526492014-09-03 15:04:12 -07001428 } else {
1429 reply->setBuffer("buffer", accessUnit);
1430 }
1431
Andreas Huberf9334412010-12-15 15:17:42 -08001432 reply->post();
1433
1434 return OK;
1435}
1436
1437void NuPlayer::renderBuffer(bool audio, const sp<AMessage> &msg) {
Steve Block3856b092011-10-20 11:56:00 +01001438 // ALOGV("renderBuffer %s", audio ? "audio" : "video");
Andreas Huberf9334412010-12-15 15:17:42 -08001439
1440 sp<AMessage> reply;
1441 CHECK(msg->findMessage("reply", &reply));
1442
Wei Jia53904f32014-07-29 10:22:53 -07001443 if ((audio && mFlushingAudio != NONE)
1444 || (!audio && mFlushingVideo != NONE)) {
Andreas Huber18ac5402011-08-31 15:04:25 -07001445 // We're currently attempting to flush the decoder, in order
1446 // to complete this, the decoder wants all its buffers back,
1447 // so we don't want any output buffers it sent us (from before
1448 // we initiated the flush) to be stuck in the renderer's queue.
1449
Steve Block3856b092011-10-20 11:56:00 +01001450 ALOGV("we're still flushing the %s decoder, sending its output buffer"
Andreas Huber18ac5402011-08-31 15:04:25 -07001451 " right back.", audio ? "audio" : "video");
1452
1453 reply->post();
1454 return;
1455 }
1456
Andreas Huber2d8bedd2012-02-21 14:38:23 -08001457 sp<ABuffer> buffer;
1458 CHECK(msg->findBuffer("buffer", &buffer));
Andreas Huberf9334412010-12-15 15:17:42 -08001459
Chong Zhanga7fa1d92014-06-11 14:49:23 -07001460 int64_t mediaTimeUs;
1461 CHECK(buffer->meta()->findInt64("timeUs", &mediaTimeUs));
1462
Andreas Huber32f3cef2011-03-02 15:34:46 -08001463 int64_t &skipUntilMediaTimeUs =
1464 audio
1465 ? mSkipRenderingAudioUntilMediaTimeUs
1466 : mSkipRenderingVideoUntilMediaTimeUs;
1467
1468 if (skipUntilMediaTimeUs >= 0) {
Andreas Huber32f3cef2011-03-02 15:34:46 -08001469
1470 if (mediaTimeUs < skipUntilMediaTimeUs) {
Steve Block3856b092011-10-20 11:56:00 +01001471 ALOGV("dropping %s buffer at time %lld as requested.",
Andreas Huber32f3cef2011-03-02 15:34:46 -08001472 audio ? "audio" : "video",
1473 mediaTimeUs);
1474
1475 reply->post();
1476 return;
1477 }
1478
1479 skipUntilMediaTimeUs = -1;
1480 }
1481
Chong Zhanga7fa1d92014-06-11 14:49:23 -07001482 if (!audio && mCCDecoder->isSelected()) {
1483 mCCDecoder->display(mediaTimeUs);
1484 }
1485
Andreas Huberf9334412010-12-15 15:17:42 -08001486 mRenderer->queueBuffer(audio, buffer, reply);
1487}
1488
Chong Zhangced1c2f2014-08-08 15:22:35 -07001489void NuPlayer::updateVideoSize(
1490 const sp<AMessage> &inputFormat,
1491 const sp<AMessage> &outputFormat) {
1492 if (inputFormat == NULL) {
1493 ALOGW("Unknown video size, reporting 0x0!");
1494 notifyListener(MEDIA_SET_VIDEO_SIZE, 0, 0);
1495 return;
1496 }
1497
1498 int32_t displayWidth, displayHeight;
1499 int32_t cropLeft, cropTop, cropRight, cropBottom;
1500
1501 if (outputFormat != NULL) {
1502 int32_t width, height;
1503 CHECK(outputFormat->findInt32("width", &width));
1504 CHECK(outputFormat->findInt32("height", &height));
1505
1506 int32_t cropLeft, cropTop, cropRight, cropBottom;
1507 CHECK(outputFormat->findRect(
1508 "crop",
1509 &cropLeft, &cropTop, &cropRight, &cropBottom));
1510
1511 displayWidth = cropRight - cropLeft + 1;
1512 displayHeight = cropBottom - cropTop + 1;
1513
1514 ALOGV("Video output format changed to %d x %d "
1515 "(crop: %d x %d @ (%d, %d))",
1516 width, height,
1517 displayWidth,
1518 displayHeight,
1519 cropLeft, cropTop);
1520 } else {
1521 CHECK(inputFormat->findInt32("width", &displayWidth));
1522 CHECK(inputFormat->findInt32("height", &displayHeight));
1523
1524 ALOGV("Video input format %d x %d", displayWidth, displayHeight);
1525 }
1526
1527 // Take into account sample aspect ratio if necessary:
1528 int32_t sarWidth, sarHeight;
1529 if (inputFormat->findInt32("sar-width", &sarWidth)
1530 && inputFormat->findInt32("sar-height", &sarHeight)) {
1531 ALOGV("Sample aspect ratio %d : %d", sarWidth, sarHeight);
1532
1533 displayWidth = (displayWidth * sarWidth) / sarHeight;
1534
1535 ALOGV("display dimensions %d x %d", displayWidth, displayHeight);
1536 }
1537
1538 int32_t rotationDegrees;
1539 if (!inputFormat->findInt32("rotation-degrees", &rotationDegrees)) {
1540 rotationDegrees = 0;
1541 }
1542
1543 if (rotationDegrees == 90 || rotationDegrees == 270) {
1544 int32_t tmp = displayWidth;
1545 displayWidth = displayHeight;
1546 displayHeight = tmp;
1547 }
1548
1549 notifyListener(
1550 MEDIA_SET_VIDEO_SIZE,
1551 displayWidth,
1552 displayHeight);
1553}
1554
Chong Zhangdcb89b32013-08-06 09:44:47 -07001555void NuPlayer::notifyListener(int msg, int ext1, int ext2, const Parcel *in) {
Andreas Huber43c3e6c2011-01-05 12:17:08 -08001556 if (mDriver == NULL) {
Andreas Huberf9334412010-12-15 15:17:42 -08001557 return;
1558 }
1559
Andreas Huber43c3e6c2011-01-05 12:17:08 -08001560 sp<NuPlayerDriver> driver = mDriver.promote();
Andreas Huberf9334412010-12-15 15:17:42 -08001561
Andreas Huber43c3e6c2011-01-05 12:17:08 -08001562 if (driver == NULL) {
Andreas Huberf9334412010-12-15 15:17:42 -08001563 return;
1564 }
1565
Chong Zhangdcb89b32013-08-06 09:44:47 -07001566 driver->notifyListener(msg, ext1, ext2, in);
Andreas Huberf9334412010-12-15 15:17:42 -08001567}
1568
Lajos Molnar87603c02014-08-20 19:25:30 -07001569void NuPlayer::flushDecoder(
1570 bool audio, bool needShutdown, const sp<AMessage> &newFormat) {
Andreas Huber14f76722013-01-15 09:04:18 -08001571 ALOGV("[%s] flushDecoder needShutdown=%d",
1572 audio ? "audio" : "video", needShutdown);
1573
Lajos Molnar87603c02014-08-20 19:25:30 -07001574 const sp<Decoder> &decoder = getDecoder(audio);
1575 if (decoder == NULL) {
Steve Blockdf64d152012-01-04 20:05:49 +00001576 ALOGI("flushDecoder %s without decoder present",
Andreas Huber6e3d3112011-11-28 12:36:11 -08001577 audio ? "audio" : "video");
Lajos Molnar87603c02014-08-20 19:25:30 -07001578 return;
Andreas Huber6e3d3112011-11-28 12:36:11 -08001579 }
1580
Andreas Huber1aef2112011-01-04 14:01:29 -08001581 // Make sure we don't continue to scan sources until we finish flushing.
1582 ++mScanSourcesGeneration;
Andreas Huber43c3e6c2011-01-05 12:17:08 -08001583 mScanSourcesPending = false;
Andreas Huber1aef2112011-01-04 14:01:29 -08001584
Lajos Molnar87603c02014-08-20 19:25:30 -07001585 decoder->signalFlush(newFormat);
Andreas Huber1aef2112011-01-04 14:01:29 -08001586 mRenderer->flush(audio);
1587
1588 FlushStatus newStatus =
1589 needShutdown ? FLUSHING_DECODER_SHUTDOWN : FLUSHING_DECODER;
1590
Andy Hung8d121d42014-10-03 09:53:53 -07001591 mFlushComplete[audio][false /* isDecoder */] = false;
1592 mFlushComplete[audio][true /* isDecoder */] = false;
Andreas Huber1aef2112011-01-04 14:01:29 -08001593 if (audio) {
Wei Jia53904f32014-07-29 10:22:53 -07001594 ALOGE_IF(mFlushingAudio != NONE,
1595 "audio flushDecoder() is called in state %d", mFlushingAudio);
Andreas Huber1aef2112011-01-04 14:01:29 -08001596 mFlushingAudio = newStatus;
Andreas Huber1aef2112011-01-04 14:01:29 -08001597 } else {
Wei Jia53904f32014-07-29 10:22:53 -07001598 ALOGE_IF(mFlushingVideo != NONE,
1599 "video flushDecoder() is called in state %d", mFlushingVideo);
Andreas Huber1aef2112011-01-04 14:01:29 -08001600 mFlushingVideo = newStatus;
Chong Zhangb86e68f2014-08-01 13:46:53 -07001601
1602 if (mCCDecoder != NULL) {
1603 mCCDecoder->flush();
1604 }
Andreas Huber1aef2112011-01-04 14:01:29 -08001605 }
1606}
1607
Lajos Molnar87603c02014-08-20 19:25:30 -07001608void NuPlayer::updateDecoderFormatWithoutFlush(
1609 bool audio, const sp<AMessage> &format) {
1610 ALOGV("[%s] updateDecoderFormatWithoutFlush", audio ? "audio" : "video");
1611
1612 const sp<Decoder> &decoder = getDecoder(audio);
1613 if (decoder == NULL) {
1614 ALOGI("updateDecoderFormatWithoutFlush %s without decoder present",
1615 audio ? "audio" : "video");
1616 return;
1617 }
1618
1619 decoder->signalUpdateFormat(format);
1620}
1621
Chong Zhangced1c2f2014-08-08 15:22:35 -07001622void NuPlayer::queueDecoderShutdown(
1623 bool audio, bool video, const sp<AMessage> &reply) {
1624 ALOGI("queueDecoderShutdown audio=%d, video=%d", audio, video);
Andreas Huber84066782011-08-16 09:34:26 -07001625
Chong Zhangced1c2f2014-08-08 15:22:35 -07001626 mDeferredActions.push_back(
1627 new ShutdownDecoderAction(audio, video));
Andreas Huber84066782011-08-16 09:34:26 -07001628
Chong Zhangced1c2f2014-08-08 15:22:35 -07001629 mDeferredActions.push_back(
1630 new SimpleAction(&NuPlayer::performScanSources));
Andreas Huber84066782011-08-16 09:34:26 -07001631
Chong Zhangced1c2f2014-08-08 15:22:35 -07001632 mDeferredActions.push_back(new PostMessageAction(reply));
1633
1634 processDeferredActions();
Andreas Huber84066782011-08-16 09:34:26 -07001635}
1636
James Dong0d268a32012-08-31 12:18:27 -07001637status_t NuPlayer::setVideoScalingMode(int32_t mode) {
1638 mVideoScalingMode = mode;
Andreas Huber57a339c2012-12-03 11:18:00 -08001639 if (mNativeWindow != NULL) {
James Dong0d268a32012-08-31 12:18:27 -07001640 status_t ret = native_window_set_scaling_mode(
1641 mNativeWindow->getNativeWindow().get(), mVideoScalingMode);
1642 if (ret != OK) {
1643 ALOGE("Failed to set scaling mode (%d): %s",
1644 -ret, strerror(-ret));
1645 return ret;
1646 }
1647 }
1648 return OK;
1649}
1650
Chong Zhangdcb89b32013-08-06 09:44:47 -07001651status_t NuPlayer::getTrackInfo(Parcel* reply) const {
1652 sp<AMessage> msg = new AMessage(kWhatGetTrackInfo, id());
1653 msg->setPointer("reply", reply);
1654
1655 sp<AMessage> response;
1656 status_t err = msg->postAndAwaitResponse(&response);
1657 return err;
1658}
1659
Robert Shih7c4f0d72014-07-09 18:53:31 -07001660status_t NuPlayer::getSelectedTrack(int32_t type, Parcel* reply) const {
1661 sp<AMessage> msg = new AMessage(kWhatGetSelectedTrack, id());
1662 msg->setPointer("reply", reply);
1663 msg->setInt32("type", type);
1664
1665 sp<AMessage> response;
1666 status_t err = msg->postAndAwaitResponse(&response);
1667 if (err == OK && response != NULL) {
1668 CHECK(response->findInt32("err", &err));
1669 }
1670 return err;
1671}
1672
Chong Zhangdcb89b32013-08-06 09:44:47 -07001673status_t NuPlayer::selectTrack(size_t trackIndex, bool select) {
1674 sp<AMessage> msg = new AMessage(kWhatSelectTrack, id());
1675 msg->setSize("trackIndex", trackIndex);
1676 msg->setInt32("select", select);
1677
1678 sp<AMessage> response;
1679 status_t err = msg->postAndAwaitResponse(&response);
1680
Chong Zhang404fced2014-06-11 14:45:31 -07001681 if (err != OK) {
1682 return err;
1683 }
1684
1685 if (!response->findInt32("err", &err)) {
1686 err = OK;
1687 }
1688
Chong Zhangdcb89b32013-08-06 09:44:47 -07001689 return err;
1690}
1691
Ronghua Wua73d9e02014-10-08 15:13:29 -07001692status_t NuPlayer::getCurrentPosition(int64_t *mediaUs) {
1693 sp<Renderer> renderer = mRenderer;
1694 if (renderer == NULL) {
1695 return NO_INIT;
1696 }
1697
1698 return renderer->getCurrentPosition(mediaUs);
1699}
1700
1701void NuPlayer::getStats(int64_t *numFramesTotal, int64_t *numFramesDropped) {
1702 *numFramesTotal = mNumFramesTotal;
1703 *numFramesDropped = mNumFramesDropped;
1704}
1705
Marco Nelissenf0b72b52014-09-16 15:43:44 -07001706sp<MetaData> NuPlayer::getFileMeta() {
1707 return mSource->getFileFormatMeta();
1708}
1709
Andreas Huberb7c8e912012-11-27 15:02:53 -08001710void NuPlayer::schedulePollDuration() {
1711 sp<AMessage> msg = new AMessage(kWhatPollDuration, id());
1712 msg->setInt32("generation", mPollDurationGeneration);
1713 msg->post();
1714}
1715
1716void NuPlayer::cancelPollDuration() {
1717 ++mPollDurationGeneration;
1718}
1719
Andreas Hubera1f8ab02012-11-30 10:53:22 -08001720void NuPlayer::processDeferredActions() {
1721 while (!mDeferredActions.empty()) {
1722 // We won't execute any deferred actions until we're no longer in
1723 // an intermediate state, i.e. one more more decoders are currently
1724 // flushing or shutting down.
1725
Andreas Hubera1f8ab02012-11-30 10:53:22 -08001726 if (mFlushingAudio != NONE || mFlushingVideo != NONE) {
1727 // We're currently flushing, postpone the reset until that's
1728 // completed.
1729
1730 ALOGV("postponing action mFlushingAudio=%d, mFlushingVideo=%d",
1731 mFlushingAudio, mFlushingVideo);
1732
1733 break;
1734 }
1735
1736 sp<Action> action = *mDeferredActions.begin();
1737 mDeferredActions.erase(mDeferredActions.begin());
1738
1739 action->execute(this);
1740 }
1741}
1742
Wei Jiae427abf2014-09-22 15:21:11 -07001743void NuPlayer::performSeek(int64_t seekTimeUs, bool needNotify) {
1744 ALOGV("performSeek seekTimeUs=%lld us (%.2f secs), needNotify(%d)",
Andreas Hubera1f8ab02012-11-30 10:53:22 -08001745 seekTimeUs,
Wei Jiae427abf2014-09-22 15:21:11 -07001746 seekTimeUs / 1E6,
1747 needNotify);
Andreas Hubera1f8ab02012-11-30 10:53:22 -08001748
Andy Hungadf34bf2014-09-03 18:22:22 -07001749 if (mSource == NULL) {
1750 // This happens when reset occurs right before the loop mode
1751 // asynchronously seeks to the start of the stream.
1752 LOG_ALWAYS_FATAL_IF(mAudioDecoder != NULL || mVideoDecoder != NULL,
1753 "mSource is NULL and decoders not NULL audio(%p) video(%p)",
1754 mAudioDecoder.get(), mVideoDecoder.get());
1755 return;
1756 }
Andreas Hubera1f8ab02012-11-30 10:53:22 -08001757 mSource->seekTo(seekTimeUs);
Robert Shihd3b0bbb2014-07-23 15:00:25 -07001758 ++mTimedTextGeneration;
Andreas Hubera1f8ab02012-11-30 10:53:22 -08001759
1760 if (mDriver != NULL) {
1761 sp<NuPlayerDriver> driver = mDriver.promote();
1762 if (driver != NULL) {
Wei Jiae427abf2014-09-22 15:21:11 -07001763 if (needNotify) {
1764 driver->notifySeekComplete();
1765 }
Andreas Hubera1f8ab02012-11-30 10:53:22 -08001766 }
1767 }
1768
1769 // everything's flushed, continue playback.
1770}
1771
1772void NuPlayer::performDecoderFlush() {
1773 ALOGV("performDecoderFlush");
1774
Andreas Huberda9740e2013-04-16 10:54:03 -07001775 if (mAudioDecoder == NULL && mVideoDecoder == NULL) {
Andreas Hubera1f8ab02012-11-30 10:53:22 -08001776 return;
1777 }
1778
1779 mTimeDiscontinuityPending = true;
1780
1781 if (mAudioDecoder != NULL) {
1782 flushDecoder(true /* audio */, false /* needShutdown */);
1783 }
1784
1785 if (mVideoDecoder != NULL) {
1786 flushDecoder(false /* audio */, false /* needShutdown */);
1787 }
1788}
1789
Andreas Huber14f76722013-01-15 09:04:18 -08001790void NuPlayer::performDecoderShutdown(bool audio, bool video) {
1791 ALOGV("performDecoderShutdown audio=%d, video=%d", audio, video);
Andreas Hubera1f8ab02012-11-30 10:53:22 -08001792
Andreas Huber14f76722013-01-15 09:04:18 -08001793 if ((!audio || mAudioDecoder == NULL)
1794 && (!video || mVideoDecoder == NULL)) {
Andreas Hubera1f8ab02012-11-30 10:53:22 -08001795 return;
1796 }
1797
1798 mTimeDiscontinuityPending = true;
1799
Andreas Huber14f76722013-01-15 09:04:18 -08001800 if (audio && mAudioDecoder != NULL) {
Andreas Hubera1f8ab02012-11-30 10:53:22 -08001801 flushDecoder(true /* audio */, true /* needShutdown */);
1802 }
1803
Andreas Huber14f76722013-01-15 09:04:18 -08001804 if (video && mVideoDecoder != NULL) {
Andreas Hubera1f8ab02012-11-30 10:53:22 -08001805 flushDecoder(false /* audio */, true /* needShutdown */);
1806 }
1807}
1808
1809void NuPlayer::performReset() {
1810 ALOGV("performReset");
1811
1812 CHECK(mAudioDecoder == NULL);
1813 CHECK(mVideoDecoder == NULL);
1814
1815 cancelPollDuration();
1816
1817 ++mScanSourcesGeneration;
1818 mScanSourcesPending = false;
1819
Lajos Molnar09524832014-07-17 14:29:51 -07001820 if (mRendererLooper != NULL) {
1821 if (mRenderer != NULL) {
1822 mRendererLooper->unregisterHandler(mRenderer->id());
1823 }
1824 mRendererLooper->stop();
1825 mRendererLooper.clear();
1826 }
Andreas Hubera1f8ab02012-11-30 10:53:22 -08001827 mRenderer.clear();
Wei Jia57568df2014-09-22 10:16:29 -07001828 ++mRendererGeneration;
Andreas Hubera1f8ab02012-11-30 10:53:22 -08001829
1830 if (mSource != NULL) {
1831 mSource->stop();
Andreas Huberb5f25f02013-02-05 10:14:26 -08001832
Andreas Hubera1f8ab02012-11-30 10:53:22 -08001833 mSource.clear();
1834 }
1835
1836 if (mDriver != NULL) {
1837 sp<NuPlayerDriver> driver = mDriver.promote();
1838 if (driver != NULL) {
1839 driver->notifyResetComplete();
1840 }
1841 }
Andreas Huber57a339c2012-12-03 11:18:00 -08001842
1843 mStarted = false;
Andreas Hubera1f8ab02012-11-30 10:53:22 -08001844}
1845
1846void NuPlayer::performScanSources() {
1847 ALOGV("performScanSources");
1848
Andreas Huber57a339c2012-12-03 11:18:00 -08001849 if (!mStarted) {
1850 return;
1851 }
1852
Andreas Hubera1f8ab02012-11-30 10:53:22 -08001853 if (mAudioDecoder == NULL || mVideoDecoder == NULL) {
1854 postScanSources();
1855 }
1856}
1857
Andreas Huber57a339c2012-12-03 11:18:00 -08001858void NuPlayer::performSetSurface(const sp<NativeWindowWrapper> &wrapper) {
1859 ALOGV("performSetSurface");
1860
1861 mNativeWindow = wrapper;
1862
1863 // XXX - ignore error from setVideoScalingMode for now
1864 setVideoScalingMode(mVideoScalingMode);
Chong Zhang13d6faa2014-08-22 15:35:28 -07001865
1866 if (mDriver != NULL) {
1867 sp<NuPlayerDriver> driver = mDriver.promote();
1868 if (driver != NULL) {
1869 driver->notifySetSurfaceComplete();
1870 }
1871 }
Andreas Huber57a339c2012-12-03 11:18:00 -08001872}
1873
Andreas Huber9575c962013-02-05 13:59:56 -08001874void NuPlayer::onSourceNotify(const sp<AMessage> &msg) {
1875 int32_t what;
1876 CHECK(msg->findInt32("what", &what));
1877
1878 switch (what) {
1879 case Source::kWhatPrepared:
1880 {
Andreas Huberb5f28d42013-04-25 15:11:19 -07001881 if (mSource == NULL) {
1882 // This is a stale notification from a source that was
1883 // asynchronously preparing when the client called reset().
1884 // We handled the reset, the source is gone.
1885 break;
1886 }
1887
Andreas Huberec0c5972013-02-05 14:47:13 -08001888 int32_t err;
1889 CHECK(msg->findInt32("err", &err));
1890
Andreas Huber9575c962013-02-05 13:59:56 -08001891 sp<NuPlayerDriver> driver = mDriver.promote();
1892 if (driver != NULL) {
Marco Nelissendd114d12014-05-28 15:23:14 -07001893 // notify duration first, so that it's definitely set when
1894 // the app received the "prepare complete" callback.
1895 int64_t durationUs;
1896 if (mSource->getDuration(&durationUs) == OK) {
1897 driver->notifyDuration(durationUs);
1898 }
Andreas Huberec0c5972013-02-05 14:47:13 -08001899 driver->notifyPrepareCompleted(err);
Andreas Huber9575c962013-02-05 13:59:56 -08001900 }
Andreas Huber99759402013-04-01 14:28:31 -07001901
Andreas Huber9575c962013-02-05 13:59:56 -08001902 break;
1903 }
1904
1905 case Source::kWhatFlagsChanged:
1906 {
1907 uint32_t flags;
1908 CHECK(msg->findInt32("flags", (int32_t *)&flags));
1909
Chong Zhang4b7069d2013-09-11 12:52:43 -07001910 sp<NuPlayerDriver> driver = mDriver.promote();
1911 if (driver != NULL) {
1912 driver->notifyFlagsChanged(flags);
1913 }
1914
Andreas Huber9575c962013-02-05 13:59:56 -08001915 if ((mSourceFlags & Source::FLAG_DYNAMIC_DURATION)
1916 && (!(flags & Source::FLAG_DYNAMIC_DURATION))) {
1917 cancelPollDuration();
1918 } else if (!(mSourceFlags & Source::FLAG_DYNAMIC_DURATION)
1919 && (flags & Source::FLAG_DYNAMIC_DURATION)
1920 && (mAudioDecoder != NULL || mVideoDecoder != NULL)) {
1921 schedulePollDuration();
1922 }
1923
1924 mSourceFlags = flags;
1925 break;
1926 }
1927
1928 case Source::kWhatVideoSizeChanged:
1929 {
Chong Zhangced1c2f2014-08-08 15:22:35 -07001930 sp<AMessage> format;
1931 CHECK(msg->findMessage("format", &format));
Andreas Huber9575c962013-02-05 13:59:56 -08001932
Chong Zhangced1c2f2014-08-08 15:22:35 -07001933 updateVideoSize(format);
Andreas Huber9575c962013-02-05 13:59:56 -08001934 break;
1935 }
1936
Chong Zhang2a3cc9a2014-08-21 17:48:26 -07001937 case Source::kWhatBufferingUpdate:
1938 {
1939 int32_t percentage;
1940 CHECK(msg->findInt32("percentage", &percentage));
1941
1942 notifyListener(MEDIA_BUFFERING_UPDATE, percentage, 0);
1943 break;
1944 }
1945
Roger Jönssonb50e83e2013-01-21 16:26:41 +01001946 case Source::kWhatBufferingStart:
1947 {
1948 notifyListener(MEDIA_INFO, MEDIA_INFO_BUFFERING_START, 0);
1949 break;
1950 }
1951
1952 case Source::kWhatBufferingEnd:
1953 {
1954 notifyListener(MEDIA_INFO, MEDIA_INFO_BUFFERING_END, 0);
1955 break;
1956 }
1957
Chong Zhangdcb89b32013-08-06 09:44:47 -07001958 case Source::kWhatSubtitleData:
1959 {
1960 sp<ABuffer> buffer;
1961 CHECK(msg->findBuffer("buffer", &buffer));
1962
Chong Zhang404fced2014-06-11 14:45:31 -07001963 sendSubtitleData(buffer, 0 /* baseIndex */);
Chong Zhangdcb89b32013-08-06 09:44:47 -07001964 break;
1965 }
1966
Robert Shihd3b0bbb2014-07-23 15:00:25 -07001967 case Source::kWhatTimedTextData:
1968 {
1969 int32_t generation;
1970 if (msg->findInt32("generation", &generation)
1971 && generation != mTimedTextGeneration) {
1972 break;
1973 }
1974
1975 sp<ABuffer> buffer;
1976 CHECK(msg->findBuffer("buffer", &buffer));
1977
1978 sp<NuPlayerDriver> driver = mDriver.promote();
1979 if (driver == NULL) {
1980 break;
1981 }
1982
1983 int posMs;
1984 int64_t timeUs, posUs;
1985 driver->getCurrentPosition(&posMs);
1986 posUs = posMs * 1000;
1987 CHECK(buffer->meta()->findInt64("timeUs", &timeUs));
1988
1989 if (posUs < timeUs) {
1990 if (!msg->findInt32("generation", &generation)) {
1991 msg->setInt32("generation", mTimedTextGeneration);
1992 }
1993 msg->post(timeUs - posUs);
1994 } else {
1995 sendTimedTextData(buffer);
1996 }
1997 break;
1998 }
1999
Andreas Huber14f76722013-01-15 09:04:18 -08002000 case Source::kWhatQueueDecoderShutdown:
2001 {
2002 int32_t audio, video;
2003 CHECK(msg->findInt32("audio", &audio));
2004 CHECK(msg->findInt32("video", &video));
2005
2006 sp<AMessage> reply;
2007 CHECK(msg->findMessage("reply", &reply));
2008
2009 queueDecoderShutdown(audio, video, reply);
2010 break;
2011 }
2012
Ronghua Wu80276872014-08-28 15:50:29 -07002013 case Source::kWhatDrmNoLicense:
2014 {
2015 notifyListener(MEDIA_ERROR, MEDIA_ERROR_UNKNOWN, ERROR_DRM_NO_LICENSE);
2016 break;
2017 }
2018
Andreas Huber9575c962013-02-05 13:59:56 -08002019 default:
2020 TRESPASS();
2021 }
2022}
2023
Chong Zhanga7fa1d92014-06-11 14:49:23 -07002024void NuPlayer::onClosedCaptionNotify(const sp<AMessage> &msg) {
2025 int32_t what;
2026 CHECK(msg->findInt32("what", &what));
2027
2028 switch (what) {
2029 case NuPlayer::CCDecoder::kWhatClosedCaptionData:
2030 {
2031 sp<ABuffer> buffer;
2032 CHECK(msg->findBuffer("buffer", &buffer));
2033
2034 size_t inbandTracks = 0;
2035 if (mSource != NULL) {
2036 inbandTracks = mSource->getTrackCount();
2037 }
2038
2039 sendSubtitleData(buffer, inbandTracks);
2040 break;
2041 }
2042
2043 case NuPlayer::CCDecoder::kWhatTrackAdded:
2044 {
2045 notifyListener(MEDIA_INFO, MEDIA_INFO_METADATA_UPDATE, 0);
2046
2047 break;
2048 }
2049
2050 default:
2051 TRESPASS();
2052 }
2053
2054
2055}
2056
Chong Zhang404fced2014-06-11 14:45:31 -07002057void NuPlayer::sendSubtitleData(const sp<ABuffer> &buffer, int32_t baseIndex) {
2058 int32_t trackIndex;
2059 int64_t timeUs, durationUs;
2060 CHECK(buffer->meta()->findInt32("trackIndex", &trackIndex));
2061 CHECK(buffer->meta()->findInt64("timeUs", &timeUs));
2062 CHECK(buffer->meta()->findInt64("durationUs", &durationUs));
2063
2064 Parcel in;
2065 in.writeInt32(trackIndex + baseIndex);
2066 in.writeInt64(timeUs);
2067 in.writeInt64(durationUs);
2068 in.writeInt32(buffer->size());
2069 in.writeInt32(buffer->size());
2070 in.write(buffer->data(), buffer->size());
2071
2072 notifyListener(MEDIA_SUBTITLE_DATA, 0, 0, &in);
2073}
Robert Shihd3b0bbb2014-07-23 15:00:25 -07002074
2075void NuPlayer::sendTimedTextData(const sp<ABuffer> &buffer) {
2076 const void *data;
2077 size_t size = 0;
2078 int64_t timeUs;
2079 int32_t flag = TextDescriptions::LOCAL_DESCRIPTIONS;
2080
2081 AString mime;
2082 CHECK(buffer->meta()->findString("mime", &mime));
2083 CHECK(strcasecmp(mime.c_str(), MEDIA_MIMETYPE_TEXT_3GPP) == 0);
2084
2085 data = buffer->data();
2086 size = buffer->size();
2087
2088 Parcel parcel;
2089 if (size > 0) {
2090 CHECK(buffer->meta()->findInt64("timeUs", &timeUs));
2091 flag |= TextDescriptions::IN_BAND_TEXT_3GPP;
2092 TextDescriptions::getParcelOfDescriptions(
2093 (const uint8_t *)data, size, flag, timeUs / 1000, &parcel);
2094 }
2095
2096 if ((parcel.dataSize() > 0)) {
2097 notifyListener(MEDIA_TIMED_TEXT, 0, 0, &parcel);
2098 } else { // send an empty timed text
2099 notifyListener(MEDIA_TIMED_TEXT, 0, 0);
2100 }
2101}
Andreas Huberb5f25f02013-02-05 10:14:26 -08002102////////////////////////////////////////////////////////////////////////////////
2103
Chong Zhangced1c2f2014-08-08 15:22:35 -07002104sp<AMessage> NuPlayer::Source::getFormat(bool audio) {
2105 sp<MetaData> meta = getFormatMeta(audio);
2106
2107 if (meta == NULL) {
2108 return NULL;
2109 }
2110
2111 sp<AMessage> msg = new AMessage;
2112
2113 if(convertMetaDataToMessage(meta, &msg) == OK) {
2114 return msg;
2115 }
2116 return NULL;
2117}
2118
Andreas Huber9575c962013-02-05 13:59:56 -08002119void NuPlayer::Source::notifyFlagsChanged(uint32_t flags) {
2120 sp<AMessage> notify = dupNotify();
2121 notify->setInt32("what", kWhatFlagsChanged);
2122 notify->setInt32("flags", flags);
2123 notify->post();
2124}
2125
Chong Zhangced1c2f2014-08-08 15:22:35 -07002126void NuPlayer::Source::notifyVideoSizeChanged(const sp<AMessage> &format) {
Andreas Huber9575c962013-02-05 13:59:56 -08002127 sp<AMessage> notify = dupNotify();
2128 notify->setInt32("what", kWhatVideoSizeChanged);
Chong Zhangced1c2f2014-08-08 15:22:35 -07002129 notify->setMessage("format", format);
Andreas Huber9575c962013-02-05 13:59:56 -08002130 notify->post();
2131}
2132
Andreas Huberec0c5972013-02-05 14:47:13 -08002133void NuPlayer::Source::notifyPrepared(status_t err) {
Andreas Huber9575c962013-02-05 13:59:56 -08002134 sp<AMessage> notify = dupNotify();
2135 notify->setInt32("what", kWhatPrepared);
Andreas Huberec0c5972013-02-05 14:47:13 -08002136 notify->setInt32("err", err);
Andreas Huber9575c962013-02-05 13:59:56 -08002137 notify->post();
2138}
2139
Andreas Huber84333e02014-02-07 15:36:10 -08002140void NuPlayer::Source::onMessageReceived(const sp<AMessage> & /* msg */) {
Andreas Huberb5f25f02013-02-05 10:14:26 -08002141 TRESPASS();
2142}
2143
Andreas Huberf9334412010-12-15 15:17:42 -08002144} // namespace android