blob: dbc0441facb11e44a5a16ce0c02d96b30d561286 [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
Lajos Molnard9fd6312014-11-06 11:00:00 -080036#include <cutils/properties.h>
37
Andreas Huber3831a062010-12-21 10:22:33 -080038#include <media/stagefright/foundation/hexdump.h>
Andreas Huberf9334412010-12-15 15:17:42 -080039#include <media/stagefright/foundation/ABuffer.h>
40#include <media/stagefright/foundation/ADebug.h>
41#include <media/stagefright/foundation/AMessage.h>
Lajos Molnar09524832014-07-17 14:29:51 -070042#include <media/stagefright/MediaBuffer.h>
Andreas Huber3fe62152011-09-16 15:09:22 -070043#include <media/stagefright/MediaDefs.h>
Andreas Huberf9334412010-12-15 15:17:42 -080044#include <media/stagefright/MediaErrors.h>
45#include <media/stagefright/MetaData.h>
Andy McFadden8ba01022012-12-18 09:46:54 -080046#include <gui/IGraphicBufferProducer.h>
Andreas Huberf9334412010-12-15 15:17:42 -080047
Andreas Huber3fe62152011-09-16 15:09:22 -070048#include "avc_utils.h"
49
Andreas Huber84066782011-08-16 09:34:26 -070050#include "ESDS.h"
51#include <media/stagefright/Utils.h>
52
Andreas Huberf9334412010-12-15 15:17:42 -080053namespace android {
54
Phil Burkc5cc2e22014-09-09 20:08:39 -070055// TODO optimize buffer size for power consumption
56// The offload read buffer size is 32 KB but 24 KB uses less power.
57const size_t NuPlayer::kAggregateBufferSizeBytes = 24 * 1024;
58
Andreas Hubera1f8ab02012-11-30 10:53:22 -080059struct NuPlayer::Action : public RefBase {
60 Action() {}
61
62 virtual void execute(NuPlayer *player) = 0;
63
64private:
65 DISALLOW_EVIL_CONSTRUCTORS(Action);
66};
67
68struct NuPlayer::SeekAction : public Action {
Wei Jiae427abf2014-09-22 15:21:11 -070069 SeekAction(int64_t seekTimeUs, bool needNotify)
70 : mSeekTimeUs(seekTimeUs),
71 mNeedNotify(needNotify) {
Andreas Hubera1f8ab02012-11-30 10:53:22 -080072 }
73
74 virtual void execute(NuPlayer *player) {
Wei Jiae427abf2014-09-22 15:21:11 -070075 player->performSeek(mSeekTimeUs, mNeedNotify);
Andreas Hubera1f8ab02012-11-30 10:53:22 -080076 }
77
78private:
79 int64_t mSeekTimeUs;
Wei Jiae427abf2014-09-22 15:21:11 -070080 bool mNeedNotify;
Andreas Hubera1f8ab02012-11-30 10:53:22 -080081
82 DISALLOW_EVIL_CONSTRUCTORS(SeekAction);
83};
84
Andreas Huber57a339c2012-12-03 11:18:00 -080085struct NuPlayer::SetSurfaceAction : public Action {
86 SetSurfaceAction(const sp<NativeWindowWrapper> &wrapper)
87 : mWrapper(wrapper) {
88 }
89
90 virtual void execute(NuPlayer *player) {
91 player->performSetSurface(mWrapper);
92 }
93
94private:
95 sp<NativeWindowWrapper> mWrapper;
96
97 DISALLOW_EVIL_CONSTRUCTORS(SetSurfaceAction);
98};
99
Wei Jiafef808d2014-10-31 17:57:05 -0700100struct NuPlayer::FlushDecoderAction : public Action {
101 FlushDecoderAction(FlushCommand audio, FlushCommand video)
Andreas Huber14f76722013-01-15 09:04:18 -0800102 : mAudio(audio),
103 mVideo(video) {
104 }
105
106 virtual void execute(NuPlayer *player) {
Wei Jiafef808d2014-10-31 17:57:05 -0700107 player->performDecoderFlush(mAudio, mVideo);
Andreas Huber14f76722013-01-15 09:04:18 -0800108 }
109
110private:
Wei Jiafef808d2014-10-31 17:57:05 -0700111 FlushCommand mAudio;
112 FlushCommand mVideo;
Andreas Huber14f76722013-01-15 09:04:18 -0800113
Wei Jiafef808d2014-10-31 17:57:05 -0700114 DISALLOW_EVIL_CONSTRUCTORS(FlushDecoderAction);
Andreas Huber14f76722013-01-15 09:04:18 -0800115};
116
117struct NuPlayer::PostMessageAction : public Action {
118 PostMessageAction(const sp<AMessage> &msg)
119 : mMessage(msg) {
120 }
121
122 virtual void execute(NuPlayer *) {
123 mMessage->post();
124 }
125
126private:
127 sp<AMessage> mMessage;
128
129 DISALLOW_EVIL_CONSTRUCTORS(PostMessageAction);
130};
131
Andreas Hubera1f8ab02012-11-30 10:53:22 -0800132// Use this if there's no state necessary to save in order to execute
133// the action.
134struct NuPlayer::SimpleAction : public Action {
135 typedef void (NuPlayer::*ActionFunc)();
136
137 SimpleAction(ActionFunc func)
138 : mFunc(func) {
139 }
140
141 virtual void execute(NuPlayer *player) {
142 (player->*mFunc)();
143 }
144
145private:
146 ActionFunc mFunc;
147
148 DISALLOW_EVIL_CONSTRUCTORS(SimpleAction);
149};
150
Andreas Huberf9334412010-12-15 15:17:42 -0800151////////////////////////////////////////////////////////////////////////////////
152
153NuPlayer::NuPlayer()
Andreas Huber9b80c2b2011-06-30 15:47:02 -0700154 : mUIDValid(false),
Andreas Huber9575c962013-02-05 13:59:56 -0800155 mSourceFlags(0),
Andreas Huber3fe62152011-09-16 15:09:22 -0700156 mVideoIsAVC(false),
Wei Jiabc2fb722014-07-08 16:37:57 -0700157 mOffloadAudio(false),
Wei Jia88703c32014-08-06 11:24:07 -0700158 mAudioDecoderGeneration(0),
159 mVideoDecoderGeneration(0),
Wei Jia57568df2014-09-22 10:16:29 -0700160 mRendererGeneration(0),
Andreas Huber9b80c2b2011-06-30 15:47:02 -0700161 mAudioEOS(false),
Andreas Huberf9334412010-12-15 15:17:42 -0800162 mVideoEOS(false),
Andreas Huber5bc087c2010-12-23 10:27:40 -0800163 mScanSourcesPending(false),
Andreas Huber1aef2112011-01-04 14:01:29 -0800164 mScanSourcesGeneration(0),
Andreas Huberb7c8e912012-11-27 15:02:53 -0800165 mPollDurationGeneration(0),
Robert Shihd3b0bbb2014-07-23 15:00:25 -0700166 mTimedTextGeneration(0),
Andreas Huber6e3d3112011-11-28 12:36:11 -0800167 mTimeDiscontinuityPending(false),
Andreas Huberf9334412010-12-15 15:17:42 -0800168 mFlushingAudio(NONE),
Andreas Huber1aef2112011-01-04 14:01:29 -0800169 mFlushingVideo(NONE),
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
Andreas Huber1aef2112011-01-04 14:01:29 -0800309void NuPlayer::resetAsync() {
Chong Zhang48296b72014-09-14 14:28:45 -0700310 if (mSource != NULL) {
311 // During a reset, the data source might be unresponsive already, we need to
312 // disconnect explicitly so that reads exit promptly.
313 // We can't queue the disconnect request to the looper, as it might be
314 // queued behind a stuck read and never gets processed.
315 // Doing a disconnect outside the looper to allows the pending reads to exit
316 // (either successfully or with error).
317 mSource->disconnect();
318 }
319
Andreas Huber1aef2112011-01-04 14:01:29 -0800320 (new AMessage(kWhatReset, id()))->post();
321}
322
Wei Jiae427abf2014-09-22 15:21:11 -0700323void NuPlayer::seekToAsync(int64_t seekTimeUs, bool needNotify) {
Andreas Huber43c3e6c2011-01-05 12:17:08 -0800324 sp<AMessage> msg = new AMessage(kWhatSeek, id());
325 msg->setInt64("seekTimeUs", seekTimeUs);
Wei Jiae427abf2014-09-22 15:21:11 -0700326 msg->setInt32("needNotify", needNotify);
Andreas Huber43c3e6c2011-01-05 12:17:08 -0800327 msg->post();
328}
329
Andreas Huber53df1a42010-12-22 10:03:04 -0800330
Chong Zhang404fced2014-06-11 14:45:31 -0700331void NuPlayer::writeTrackInfo(
332 Parcel* reply, const sp<AMessage> format) const {
333 int32_t trackType;
334 CHECK(format->findInt32("type", &trackType));
335
336 AString lang;
337 CHECK(format->findString("language", &lang));
338
339 reply->writeInt32(2); // write something non-zero
340 reply->writeInt32(trackType);
341 reply->writeString16(String16(lang.c_str()));
342
343 if (trackType == MEDIA_TRACK_TYPE_SUBTITLE) {
344 AString mime;
345 CHECK(format->findString("mime", &mime));
346
347 int32_t isAuto, isDefault, isForced;
348 CHECK(format->findInt32("auto", &isAuto));
349 CHECK(format->findInt32("default", &isDefault));
350 CHECK(format->findInt32("forced", &isForced));
351
352 reply->writeString16(String16(mime.c_str()));
353 reply->writeInt32(isAuto);
354 reply->writeInt32(isDefault);
355 reply->writeInt32(isForced);
356 }
357}
358
Andreas Huberf9334412010-12-15 15:17:42 -0800359void NuPlayer::onMessageReceived(const sp<AMessage> &msg) {
360 switch (msg->what()) {
361 case kWhatSetDataSource:
362 {
Steve Block3856b092011-10-20 11:56:00 +0100363 ALOGV("kWhatSetDataSource");
Andreas Huberf9334412010-12-15 15:17:42 -0800364
365 CHECK(mSource == NULL);
366
Chong Zhang3de157d2014-08-05 20:54:44 -0700367 status_t err = OK;
Andreas Huber5bc087c2010-12-23 10:27:40 -0800368 sp<RefBase> obj;
369 CHECK(msg->findObject("source", &obj));
Chong Zhang3de157d2014-08-05 20:54:44 -0700370 if (obj != NULL) {
371 mSource = static_cast<Source *>(obj.get());
Chong Zhang3de157d2014-08-05 20:54:44 -0700372 } else {
373 err = UNKNOWN_ERROR;
374 }
Andreas Huber9575c962013-02-05 13:59:56 -0800375
376 CHECK(mDriver != NULL);
377 sp<NuPlayerDriver> driver = mDriver.promote();
378 if (driver != NULL) {
Chong Zhang3de157d2014-08-05 20:54:44 -0700379 driver->notifySetDataSourceCompleted(err);
Andreas Huber9575c962013-02-05 13:59:56 -0800380 }
381 break;
382 }
383
384 case kWhatPrepare:
385 {
386 mSource->prepareAsync();
Andreas Huberf9334412010-12-15 15:17:42 -0800387 break;
388 }
389
Chong Zhangdcb89b32013-08-06 09:44:47 -0700390 case kWhatGetTrackInfo:
391 {
392 uint32_t replyID;
393 CHECK(msg->senderAwaitsResponse(&replyID));
394
Chong Zhang404fced2014-06-11 14:45:31 -0700395 Parcel* reply;
396 CHECK(msg->findPointer("reply", (void**)&reply));
397
398 size_t inbandTracks = 0;
Chong Zhangdcb89b32013-08-06 09:44:47 -0700399 if (mSource != NULL) {
Chong Zhang404fced2014-06-11 14:45:31 -0700400 inbandTracks = mSource->getTrackCount();
401 }
402
Chong Zhanga7fa1d92014-06-11 14:49:23 -0700403 size_t ccTracks = 0;
404 if (mCCDecoder != NULL) {
405 ccTracks = mCCDecoder->getTrackCount();
406 }
407
Chong Zhang404fced2014-06-11 14:45:31 -0700408 // total track count
Chong Zhanga7fa1d92014-06-11 14:49:23 -0700409 reply->writeInt32(inbandTracks + ccTracks);
Chong Zhang404fced2014-06-11 14:45:31 -0700410
411 // write inband tracks
412 for (size_t i = 0; i < inbandTracks; ++i) {
413 writeTrackInfo(reply, mSource->getTrackInfo(i));
Chong Zhangdcb89b32013-08-06 09:44:47 -0700414 }
415
Chong Zhanga7fa1d92014-06-11 14:49:23 -0700416 // write CC track
417 for (size_t i = 0; i < ccTracks; ++i) {
418 writeTrackInfo(reply, mCCDecoder->getTrackInfo(i));
419 }
420
Chong Zhangdcb89b32013-08-06 09:44:47 -0700421 sp<AMessage> response = new AMessage;
Chong Zhangdcb89b32013-08-06 09:44:47 -0700422 response->postReply(replyID);
423 break;
424 }
425
Robert Shih7c4f0d72014-07-09 18:53:31 -0700426 case kWhatGetSelectedTrack:
427 {
428 status_t err = INVALID_OPERATION;
429 if (mSource != NULL) {
430 err = OK;
431
432 int32_t type32;
433 CHECK(msg->findInt32("type", (int32_t*)&type32));
434 media_track_type type = (media_track_type)type32;
435 ssize_t selectedTrack = mSource->getSelectedTrack(type);
436
437 Parcel* reply;
438 CHECK(msg->findPointer("reply", (void**)&reply));
439 reply->writeInt32(selectedTrack);
440 }
441
442 sp<AMessage> response = new AMessage;
443 response->setInt32("err", err);
444
445 uint32_t replyID;
446 CHECK(msg->senderAwaitsResponse(&replyID));
447 response->postReply(replyID);
448 break;
449 }
450
Chong Zhangdcb89b32013-08-06 09:44:47 -0700451 case kWhatSelectTrack:
452 {
453 uint32_t replyID;
454 CHECK(msg->senderAwaitsResponse(&replyID));
455
Chong Zhang404fced2014-06-11 14:45:31 -0700456 size_t trackIndex;
457 int32_t select;
Robert Shih6ffb1fd2014-10-29 16:24:32 -0700458 int64_t timeUs;
Chong Zhang404fced2014-06-11 14:45:31 -0700459 CHECK(msg->findSize("trackIndex", &trackIndex));
460 CHECK(msg->findInt32("select", &select));
Robert Shih6ffb1fd2014-10-29 16:24:32 -0700461 CHECK(msg->findInt64("timeUs", &timeUs));
Chong Zhang404fced2014-06-11 14:45:31 -0700462
Chong Zhangdcb89b32013-08-06 09:44:47 -0700463 status_t err = INVALID_OPERATION;
Chong Zhang404fced2014-06-11 14:45:31 -0700464
465 size_t inbandTracks = 0;
Chong Zhangdcb89b32013-08-06 09:44:47 -0700466 if (mSource != NULL) {
Chong Zhang404fced2014-06-11 14:45:31 -0700467 inbandTracks = mSource->getTrackCount();
468 }
Chong Zhanga7fa1d92014-06-11 14:49:23 -0700469 size_t ccTracks = 0;
470 if (mCCDecoder != NULL) {
471 ccTracks = mCCDecoder->getTrackCount();
472 }
Chong Zhang404fced2014-06-11 14:45:31 -0700473
474 if (trackIndex < inbandTracks) {
Robert Shih6ffb1fd2014-10-29 16:24:32 -0700475 err = mSource->selectTrack(trackIndex, select, timeUs);
Robert Shihd3b0bbb2014-07-23 15:00:25 -0700476
477 if (!select && err == OK) {
478 int32_t type;
479 sp<AMessage> info = mSource->getTrackInfo(trackIndex);
480 if (info != NULL
481 && info->findInt32("type", &type)
482 && type == MEDIA_TRACK_TYPE_TIMEDTEXT) {
483 ++mTimedTextGeneration;
484 }
485 }
Chong Zhanga7fa1d92014-06-11 14:49:23 -0700486 } else {
487 trackIndex -= inbandTracks;
488
489 if (trackIndex < ccTracks) {
490 err = mCCDecoder->selectTrack(trackIndex, select);
491 }
Chong Zhangdcb89b32013-08-06 09:44:47 -0700492 }
493
494 sp<AMessage> response = new AMessage;
495 response->setInt32("err", err);
496
497 response->postReply(replyID);
498 break;
499 }
500
Andreas Huberb7c8e912012-11-27 15:02:53 -0800501 case kWhatPollDuration:
502 {
503 int32_t generation;
504 CHECK(msg->findInt32("generation", &generation));
505
506 if (generation != mPollDurationGeneration) {
507 // stale
508 break;
509 }
510
511 int64_t durationUs;
512 if (mDriver != NULL && mSource->getDuration(&durationUs) == OK) {
513 sp<NuPlayerDriver> driver = mDriver.promote();
514 if (driver != NULL) {
515 driver->notifyDuration(durationUs);
516 }
517 }
518
519 msg->post(1000000ll); // poll again in a second.
520 break;
521 }
522
Glenn Kasten11731182011-02-08 17:26:17 -0800523 case kWhatSetVideoNativeWindow:
Andreas Huberf9334412010-12-15 15:17:42 -0800524 {
Steve Block3856b092011-10-20 11:56:00 +0100525 ALOGV("kWhatSetVideoNativeWindow");
Andreas Huberf9334412010-12-15 15:17:42 -0800526
527 sp<RefBase> obj;
Glenn Kasten11731182011-02-08 17:26:17 -0800528 CHECK(msg->findObject("native-window", &obj));
Andreas Huberf9334412010-12-15 15:17:42 -0800529
Wei Jiafef808d2014-10-31 17:57:05 -0700530 if (mSource->getFormat(false /* audio */) == NULL) {
531 performSetSurface(static_cast<NativeWindowWrapper *>(obj.get()));
532 break;
533 }
534
535 mDeferredActions.push_back(
536 new FlushDecoderAction(FLUSH_CMD_FLUSH /* audio */,
537 FLUSH_CMD_SHUTDOWN /* video */));
538
Andreas Huber57a339c2012-12-03 11:18:00 -0800539 mDeferredActions.push_back(
540 new SetSurfaceAction(
541 static_cast<NativeWindowWrapper *>(obj.get())));
James Dong0d268a32012-08-31 12:18:27 -0700542
Andreas Huber57a339c2012-12-03 11:18:00 -0800543 if (obj != NULL) {
Wei Jiafef808d2014-10-31 17:57:05 -0700544 if (mStarted) {
Andy Hung73535852014-09-05 11:42:58 -0700545 // Issue a seek to refresh the video screen only if started otherwise
546 // the extractor may not yet be started and will assert.
547 // If the video decoder is not set (perhaps audio only in this case)
548 // do not perform a seek as it is not needed.
Ronghua Wua73d9e02014-10-08 15:13:29 -0700549 int64_t currentPositionUs = 0;
550 if (getCurrentPosition(&currentPositionUs) == OK) {
551 mDeferredActions.push_back(
552 new SeekAction(currentPositionUs, false /* needNotify */));
553 }
Andy Hung73535852014-09-05 11:42:58 -0700554 }
Wei Jiaac428aa2014-09-02 19:01:34 -0700555
Andreas Huber57a339c2012-12-03 11:18:00 -0800556 // If there is a new surface texture, instantiate decoders
557 // again if possible.
558 mDeferredActions.push_back(
559 new SimpleAction(&NuPlayer::performScanSources));
560 }
561
562 processDeferredActions();
Andreas Huberf9334412010-12-15 15:17:42 -0800563 break;
564 }
565
566 case kWhatSetAudioSink:
567 {
Steve Block3856b092011-10-20 11:56:00 +0100568 ALOGV("kWhatSetAudioSink");
Andreas Huberf9334412010-12-15 15:17:42 -0800569
570 sp<RefBase> obj;
571 CHECK(msg->findObject("sink", &obj));
572
573 mAudioSink = static_cast<MediaPlayerBase::AudioSink *>(obj.get());
574 break;
575 }
576
577 case kWhatStart:
578 {
Steve Block3856b092011-10-20 11:56:00 +0100579 ALOGV("kWhatStart");
Wei Jia94211742014-10-28 17:09:06 -0700580 if (mStarted) {
581 onResume();
582 } else {
583 onStart();
Lajos Molnar09524832014-07-17 14:29:51 -0700584 }
Andreas Huberf9334412010-12-15 15:17:42 -0800585 break;
586 }
587
588 case kWhatScanSources:
589 {
Andreas Huber1aef2112011-01-04 14:01:29 -0800590 int32_t generation;
591 CHECK(msg->findInt32("generation", &generation));
592 if (generation != mScanSourcesGeneration) {
593 // Drop obsolete msg.
594 break;
595 }
596
Andreas Huber5bc087c2010-12-23 10:27:40 -0800597 mScanSourcesPending = false;
598
Steve Block3856b092011-10-20 11:56:00 +0100599 ALOGV("scanning sources haveAudio=%d, haveVideo=%d",
Andreas Huber43c3e6c2011-01-05 12:17:08 -0800600 mAudioDecoder != NULL, mVideoDecoder != NULL);
601
Andreas Huberb7c8e912012-11-27 15:02:53 -0800602 bool mHadAnySourcesBefore =
603 (mAudioDecoder != NULL) || (mVideoDecoder != NULL);
604
Andy Hung282a7e32014-08-14 15:56:34 -0700605 // initialize video before audio because successful initialization of
606 // video may change deep buffer mode of audio.
Haynes Mathew George5d246ef2012-07-09 10:36:57 -0700607 if (mNativeWindow != NULL) {
608 instantiateDecoder(false, &mVideoDecoder);
609 }
Andreas Huberf9334412010-12-15 15:17:42 -0800610
Ronghua Wua10fd232014-11-06 16:15:20 -0800611 // Don't try to re-open audio sink if there's an existing decoder.
612 if (mAudioSink != NULL && mAudioDecoder == NULL) {
613 sp<MetaData> audioMeta = mSource->getFormatMeta(true /* audio */);
614 sp<AMessage> videoFormat = mSource->getFormat(false /* audio */);
615 audio_stream_type_t streamType = mAudioSink->getAudioStreamType();
616 bool canOffload = canOffloadStream(audioMeta, (videoFormat != NULL),
617 true /* is_streaming */, streamType);
618 if (canOffload) {
619 if (!mOffloadAudio) {
620 mRenderer->signalEnableOffloadAudio();
621 }
Andy Hung282a7e32014-08-14 15:56:34 -0700622 // open audio sink early under offload mode.
623 sp<AMessage> format = mSource->getFormat(true /*audio*/);
624 openAudioSink(format, true /*offloadOnly*/);
625 }
Andreas Huber5bc087c2010-12-23 10:27:40 -0800626 instantiateDecoder(true, &mAudioDecoder);
Andreas Huberf9334412010-12-15 15:17:42 -0800627 }
628
Andreas Huberb7c8e912012-11-27 15:02:53 -0800629 if (!mHadAnySourcesBefore
630 && (mAudioDecoder != NULL || mVideoDecoder != NULL)) {
631 // This is the first time we've found anything playable.
632
Andreas Huber9575c962013-02-05 13:59:56 -0800633 if (mSourceFlags & Source::FLAG_DYNAMIC_DURATION) {
Andreas Huberb7c8e912012-11-27 15:02:53 -0800634 schedulePollDuration();
635 }
636 }
637
Andreas Hubereac68ba2011-09-27 12:12:25 -0700638 status_t err;
639 if ((err = mSource->feedMoreTSData()) != OK) {
Andreas Huber1aef2112011-01-04 14:01:29 -0800640 if (mAudioDecoder == NULL && mVideoDecoder == NULL) {
641 // We're not currently decoding anything (no audio or
642 // video tracks found) and we just ran out of input data.
Andreas Hubereac68ba2011-09-27 12:12:25 -0700643
644 if (err == ERROR_END_OF_STREAM) {
645 notifyListener(MEDIA_PLAYBACK_COMPLETE, 0, 0);
646 } else {
647 notifyListener(MEDIA_ERROR, MEDIA_ERROR_UNKNOWN, err);
648 }
Andreas Huber1aef2112011-01-04 14:01:29 -0800649 }
Andreas Huberf9334412010-12-15 15:17:42 -0800650 break;
651 }
652
Andreas Huberfbe9d812012-08-31 14:05:27 -0700653 if ((mAudioDecoder == NULL && mAudioSink != NULL)
654 || (mVideoDecoder == NULL && mNativeWindow != NULL)) {
Andreas Huberf9334412010-12-15 15:17:42 -0800655 msg->post(100000ll);
Andreas Huber5bc087c2010-12-23 10:27:40 -0800656 mScanSourcesPending = true;
Andreas Huberf9334412010-12-15 15:17:42 -0800657 }
658 break;
659 }
660
661 case kWhatVideoNotify:
662 case kWhatAudioNotify:
663 {
664 bool audio = msg->what() == kWhatAudioNotify;
665
Wei Jia88703c32014-08-06 11:24:07 -0700666 int32_t currentDecoderGeneration =
667 (audio? mAudioDecoderGeneration : mVideoDecoderGeneration);
668 int32_t requesterGeneration = currentDecoderGeneration - 1;
669 CHECK(msg->findInt32("generation", &requesterGeneration));
670
671 if (requesterGeneration != currentDecoderGeneration) {
672 ALOGV("got message from old %s decoder, generation(%d:%d)",
673 audio ? "audio" : "video", requesterGeneration,
674 currentDecoderGeneration);
675 sp<AMessage> reply;
676 if (!(msg->findMessage("reply", &reply))) {
677 return;
678 }
679
680 reply->setInt32("err", INFO_DISCONTINUITY);
681 reply->post();
682 return;
683 }
684
Andreas Huberf9334412010-12-15 15:17:42 -0800685 int32_t what;
Lajos Molnar1cd13982014-01-17 15:12:51 -0800686 CHECK(msg->findInt32("what", &what));
Andreas Huberf9334412010-12-15 15:17:42 -0800687
Lajos Molnar1cd13982014-01-17 15:12:51 -0800688 if (what == Decoder::kWhatFillThisBuffer) {
Andreas Huberf9334412010-12-15 15:17:42 -0800689 status_t err = feedDecoderInputData(
Lajos Molnar1cd13982014-01-17 15:12:51 -0800690 audio, msg);
Andreas Huberf9334412010-12-15 15:17:42 -0800691
Andreas Huber5bc087c2010-12-23 10:27:40 -0800692 if (err == -EWOULDBLOCK) {
Andreas Hubereac68ba2011-09-27 12:12:25 -0700693 if (mSource->feedMoreTSData() == OK) {
Phil Burkc5cc2e22014-09-09 20:08:39 -0700694 msg->post(10 * 1000ll);
Andreas Huber5bc087c2010-12-23 10:27:40 -0800695 }
Andreas Huberf9334412010-12-15 15:17:42 -0800696 }
Lajos Molnar1cd13982014-01-17 15:12:51 -0800697 } else if (what == Decoder::kWhatEOS) {
Andreas Huberdc9bacd2011-09-26 10:53:29 -0700698 int32_t err;
Lajos Molnar1cd13982014-01-17 15:12:51 -0800699 CHECK(msg->findInt32("err", &err));
Andreas Huberdc9bacd2011-09-26 10:53:29 -0700700
701 if (err == ERROR_END_OF_STREAM) {
Steve Block3856b092011-10-20 11:56:00 +0100702 ALOGV("got %s decoder EOS", audio ? "audio" : "video");
Andreas Huberdc9bacd2011-09-26 10:53:29 -0700703 } else {
Steve Block3856b092011-10-20 11:56:00 +0100704 ALOGV("got %s decoder EOS w/ error %d",
Andreas Huberdc9bacd2011-09-26 10:53:29 -0700705 audio ? "audio" : "video",
706 err);
707 }
708
709 mRenderer->queueEOS(audio, err);
Lajos Molnar1cd13982014-01-17 15:12:51 -0800710 } else if (what == Decoder::kWhatFlushCompleted) {
Steve Block3856b092011-10-20 11:56:00 +0100711 ALOGV("decoder %s flush completed", audio ? "audio" : "video");
Andreas Huberf9334412010-12-15 15:17:42 -0800712
Andy Hung8d121d42014-10-03 09:53:53 -0700713 handleFlushComplete(audio, true /* isDecoder */);
Andreas Huber3831a062010-12-21 10:22:33 -0800714 finishFlushIfPossible();
Wei Jiac6cfd702014-11-11 16:33:20 -0800715 } else if (what == Decoder::kWhatVideoSizeChanged) {
Lajos Molnar1cd13982014-01-17 15:12:51 -0800716 sp<AMessage> format;
717 CHECK(msg->findMessage("format", &format));
718
Wei Jiac6cfd702014-11-11 16:33:20 -0800719 sp<AMessage> inputFormat =
720 mSource->getFormat(false /* audio */);
Andreas Huber3831a062010-12-21 10:22:33 -0800721
Wei Jiac6cfd702014-11-11 16:33:20 -0800722 updateVideoSize(inputFormat, format);
Lajos Molnar1cd13982014-01-17 15:12:51 -0800723 } else if (what == Decoder::kWhatShutdownCompleted) {
Steve Block3856b092011-10-20 11:56:00 +0100724 ALOGV("%s shutdown completed", audio ? "audio" : "video");
Andreas Huber3831a062010-12-21 10:22:33 -0800725 if (audio) {
726 mAudioDecoder.clear();
Wei Jia57568df2014-09-22 10:16:29 -0700727 ++mAudioDecoderGeneration;
Andreas Huber3831a062010-12-21 10:22:33 -0800728
729 CHECK_EQ((int)mFlushingAudio, (int)SHUTTING_DOWN_DECODER);
730 mFlushingAudio = SHUT_DOWN;
731 } else {
732 mVideoDecoder.clear();
Wei Jia57568df2014-09-22 10:16:29 -0700733 ++mVideoDecoderGeneration;
Andreas Huber3831a062010-12-21 10:22:33 -0800734
735 CHECK_EQ((int)mFlushingVideo, (int)SHUTTING_DOWN_DECODER);
736 mFlushingVideo = SHUT_DOWN;
737 }
738
739 finishFlushIfPossible();
Lajos Molnar1cd13982014-01-17 15:12:51 -0800740 } else if (what == Decoder::kWhatError) {
Chong Zhangf4c0a942014-08-11 15:14:10 -0700741 status_t err;
Andy Hung2abde2c2014-09-30 14:40:32 -0700742 if (!msg->findInt32("err", &err) || err == OK) {
Chong Zhangf4c0a942014-08-11 15:14:10 -0700743 err = UNKNOWN_ERROR;
744 }
Andy Hungcf31f1e2014-09-23 14:59:01 -0700745
Andy Hung2abde2c2014-09-30 14:40:32 -0700746 // Decoder errors can be due to Source (e.g. from streaming),
747 // or from decoding corrupted bitstreams, or from other decoder
748 // MediaCodec operations (e.g. from an ongoing reset or seek).
749 //
750 // We try to gracefully shut down the affected decoder if possible,
751 // rather than trying to force the shutdown with something
752 // similar to performReset(). This method can lead to a hang
753 // if MediaCodec functions block after an error, but they should
754 // typically return INVALID_OPERATION instead of blocking.
755
756 FlushStatus *flushing = audio ? &mFlushingAudio : &mFlushingVideo;
757 ALOGE("received error(%#x) from %s decoder, flushing(%d), now shutting down",
758 err, audio ? "audio" : "video", *flushing);
759
760 switch (*flushing) {
761 case NONE:
762 mDeferredActions.push_back(
Wei Jiafef808d2014-10-31 17:57:05 -0700763 new FlushDecoderAction(
764 audio ? FLUSH_CMD_SHUTDOWN : FLUSH_CMD_NONE,
765 audio ? FLUSH_CMD_NONE : FLUSH_CMD_SHUTDOWN));
Andy Hung2abde2c2014-09-30 14:40:32 -0700766 processDeferredActions();
767 break;
768 case FLUSHING_DECODER:
769 *flushing = FLUSHING_DECODER_SHUTDOWN; // initiate shutdown after flush.
770 break; // Wait for flush to complete.
771 case FLUSHING_DECODER_SHUTDOWN:
772 break; // Wait for flush to complete.
773 case SHUTTING_DOWN_DECODER:
774 break; // Wait for shutdown to complete.
775 case FLUSHED:
776 // Widevine source reads must stop before releasing the video decoder.
777 if (!audio && mSource != NULL && mSourceFlags & Source::FLAG_SECURE) {
778 mSource->stop();
779 }
780 getDecoder(audio)->initiateShutdown(); // In the middle of a seek.
781 *flushing = SHUTTING_DOWN_DECODER; // Shut down.
782 break;
783 case SHUT_DOWN:
784 finishFlushIfPossible(); // Should not occur.
785 break; // Finish anyways.
Marco Nelissen9e2b7912014-08-18 16:13:03 -0700786 }
Andy Hung2abde2c2014-09-30 14:40:32 -0700787 notifyListener(MEDIA_ERROR, MEDIA_ERROR_UNKNOWN, err);
Wei Jiac6cfd702014-11-11 16:33:20 -0800788 } else if (what == Decoder::kWhatRenderBufferTime) {
Lajos Molnar1cd13982014-01-17 15:12:51 -0800789 renderBuffer(audio, msg);
790 } else {
791 ALOGV("Unhandled decoder notification %d '%c%c%c%c'.",
Andreas Hubera1f8ab02012-11-30 10:53:22 -0800792 what,
793 what >> 24,
794 (what >> 16) & 0xff,
795 (what >> 8) & 0xff,
796 what & 0xff);
Andreas Huberf9334412010-12-15 15:17:42 -0800797 }
798
799 break;
800 }
801
802 case kWhatRendererNotify:
803 {
Wei Jia57568df2014-09-22 10:16:29 -0700804 int32_t requesterGeneration = mRendererGeneration - 1;
805 CHECK(msg->findInt32("generation", &requesterGeneration));
806 if (requesterGeneration != mRendererGeneration) {
807 ALOGV("got message from old renderer, generation(%d:%d)",
808 requesterGeneration, mRendererGeneration);
809 return;
810 }
811
Andreas Huberf9334412010-12-15 15:17:42 -0800812 int32_t what;
813 CHECK(msg->findInt32("what", &what));
814
815 if (what == Renderer::kWhatEOS) {
816 int32_t audio;
817 CHECK(msg->findInt32("audio", &audio));
818
Andreas Huberc92fd242011-08-16 13:48:44 -0700819 int32_t finalResult;
820 CHECK(msg->findInt32("finalResult", &finalResult));
821
Andreas Huberf9334412010-12-15 15:17:42 -0800822 if (audio) {
823 mAudioEOS = true;
824 } else {
825 mVideoEOS = true;
826 }
827
Andreas Huberc92fd242011-08-16 13:48:44 -0700828 if (finalResult == ERROR_END_OF_STREAM) {
Steve Block3856b092011-10-20 11:56:00 +0100829 ALOGV("reached %s EOS", audio ? "audio" : "video");
Andreas Huberc92fd242011-08-16 13:48:44 -0700830 } else {
Steve Block29357bc2012-01-06 19:20:56 +0000831 ALOGE("%s track encountered an error (%d)",
Andreas Huberc92fd242011-08-16 13:48:44 -0700832 audio ? "audio" : "video", finalResult);
833
834 notifyListener(
835 MEDIA_ERROR, MEDIA_ERROR_UNKNOWN, finalResult);
836 }
Andreas Huberf9334412010-12-15 15:17:42 -0800837
838 if ((mAudioEOS || mAudioDecoder == NULL)
839 && (mVideoEOS || mVideoDecoder == NULL)) {
840 notifyListener(MEDIA_PLAYBACK_COMPLETE, 0, 0);
841 }
Andreas Huber3fe62152011-09-16 15:09:22 -0700842 } else if (what == Renderer::kWhatFlushComplete) {
Andreas Huberf9334412010-12-15 15:17:42 -0800843 int32_t audio;
844 CHECK(msg->findInt32("audio", &audio));
845
Steve Block3856b092011-10-20 11:56:00 +0100846 ALOGV("renderer %s flush completed.", audio ? "audio" : "video");
Andy Hung8d121d42014-10-03 09:53:53 -0700847 handleFlushComplete(audio, false /* isDecoder */);
848 finishFlushIfPossible();
James Dongf57b4ea2012-07-20 13:38:36 -0700849 } else if (what == Renderer::kWhatVideoRenderingStart) {
850 notifyListener(MEDIA_INFO, MEDIA_INFO_RENDERING_START, 0);
Lajos Molnarcbaffcf2013-08-14 18:30:38 -0700851 } else if (what == Renderer::kWhatMediaRenderingStart) {
852 ALOGV("media rendering started");
853 notifyListener(MEDIA_STARTED, 0, 0);
Wei Jia3a2956d2014-07-22 16:01:33 -0700854 } else if (what == Renderer::kWhatAudioOffloadTearDown) {
Ronghua Wua10fd232014-11-06 16:15:20 -0800855 ALOGV("Tear down audio offload, fall back to s/w path if due to error.");
Wei Jia3a2956d2014-07-22 16:01:33 -0700856 int64_t positionUs;
857 CHECK(msg->findInt64("positionUs", &positionUs));
Ronghua Wu08529172014-10-02 16:55:52 -0700858 int32_t reason;
859 CHECK(msg->findInt32("reason", &reason));
Andy Hung282a7e32014-08-14 15:56:34 -0700860 closeAudioSink();
Wei Jia3a2956d2014-07-22 16:01:33 -0700861 mAudioDecoder.clear();
Wei Jia57568df2014-09-22 10:16:29 -0700862 ++mAudioDecoderGeneration;
Wei Jia3a2956d2014-07-22 16:01:33 -0700863 mRenderer->flush(true /* audio */);
864 if (mVideoDecoder != NULL) {
865 mRenderer->flush(false /* audio */);
866 }
Wei Jia3a2956d2014-07-22 16:01:33 -0700867
Wei Jiae427abf2014-09-22 15:21:11 -0700868 performSeek(positionUs, false /* needNotify */);
Ronghua Wu08529172014-10-02 16:55:52 -0700869 if (reason == Renderer::kDueToError) {
Ronghua Wua10fd232014-11-06 16:15:20 -0800870 mRenderer->signalDisableOffloadAudio();
871 mOffloadAudio = false;
Ronghua Wu08529172014-10-02 16:55:52 -0700872 instantiateDecoder(true /* audio */, &mAudioDecoder);
873 }
Andreas Huberf9334412010-12-15 15:17:42 -0800874 }
875 break;
876 }
877
878 case kWhatMoreDataQueued:
879 {
880 break;
881 }
882
Andreas Huber1aef2112011-01-04 14:01:29 -0800883 case kWhatReset:
884 {
Steve Block3856b092011-10-20 11:56:00 +0100885 ALOGV("kWhatReset");
Andreas Huber1aef2112011-01-04 14:01:29 -0800886
Andreas Hubera1f8ab02012-11-30 10:53:22 -0800887 mDeferredActions.push_back(
Wei Jiafef808d2014-10-31 17:57:05 -0700888 new FlushDecoderAction(
889 FLUSH_CMD_SHUTDOWN /* audio */,
890 FLUSH_CMD_SHUTDOWN /* video */));
Andreas Huberb7c8e912012-11-27 15:02:53 -0800891
Andreas Hubera1f8ab02012-11-30 10:53:22 -0800892 mDeferredActions.push_back(
893 new SimpleAction(&NuPlayer::performReset));
Andreas Huberb58ce9f2011-11-28 16:27:35 -0800894
Andreas Hubera1f8ab02012-11-30 10:53:22 -0800895 processDeferredActions();
Andreas Huber1aef2112011-01-04 14:01:29 -0800896 break;
897 }
898
Andreas Huber43c3e6c2011-01-05 12:17:08 -0800899 case kWhatSeek:
900 {
901 int64_t seekTimeUs;
Wei Jiae427abf2014-09-22 15:21:11 -0700902 int32_t needNotify;
Andreas Huber43c3e6c2011-01-05 12:17:08 -0800903 CHECK(msg->findInt64("seekTimeUs", &seekTimeUs));
Wei Jiae427abf2014-09-22 15:21:11 -0700904 CHECK(msg->findInt32("needNotify", &needNotify));
Andreas Huber43c3e6c2011-01-05 12:17:08 -0800905
Wei Jiae427abf2014-09-22 15:21:11 -0700906 ALOGV("kWhatSeek seekTimeUs=%lld us, needNotify=%d",
907 seekTimeUs, needNotify);
Andreas Huber43c3e6c2011-01-05 12:17:08 -0800908
Andreas Hubera1f8ab02012-11-30 10:53:22 -0800909 mDeferredActions.push_back(
Wei Jiafef808d2014-10-31 17:57:05 -0700910 new FlushDecoderAction(FLUSH_CMD_FLUSH /* audio */,
911 FLUSH_CMD_FLUSH /* video */));
Andreas Huber43c3e6c2011-01-05 12:17:08 -0800912
Wei Jiae427abf2014-09-22 15:21:11 -0700913 mDeferredActions.push_back(
914 new SeekAction(seekTimeUs, needNotify));
Andreas Huber43c3e6c2011-01-05 12:17:08 -0800915
Andreas Hubera1f8ab02012-11-30 10:53:22 -0800916 processDeferredActions();
Andreas Huber43c3e6c2011-01-05 12:17:08 -0800917 break;
918 }
919
Andreas Huberb4082222011-01-20 15:23:04 -0800920 case kWhatPause:
921 {
Wei Jia7e9f7f72014-09-23 10:55:35 -0700922 if (mSource != NULL) {
923 mSource->pause();
924 } else {
925 ALOGW("pause called when source is gone or not set");
926 }
927 if (mRenderer != NULL) {
928 mRenderer->pause();
929 } else {
930 ALOGW("pause called when renderer is gone or not set");
931 }
Andreas Huberb4082222011-01-20 15:23:04 -0800932 break;
933 }
934
Andreas Huberb5f25f02013-02-05 10:14:26 -0800935 case kWhatSourceNotify:
936 {
Andreas Huber9575c962013-02-05 13:59:56 -0800937 onSourceNotify(msg);
Andreas Huberb5f25f02013-02-05 10:14:26 -0800938 break;
939 }
940
Chong Zhanga7fa1d92014-06-11 14:49:23 -0700941 case kWhatClosedCaptionNotify:
942 {
943 onClosedCaptionNotify(msg);
944 break;
945 }
946
Andreas Huberf9334412010-12-15 15:17:42 -0800947 default:
948 TRESPASS();
949 break;
950 }
951}
952
Wei Jia94211742014-10-28 17:09:06 -0700953void NuPlayer::onResume() {
954 if (mSource != NULL) {
955 mSource->resume();
956 } else {
957 ALOGW("resume called when source is gone or not set");
958 }
959 // |mAudioDecoder| may have been released due to the pause timeout, so re-create it if
960 // needed.
961 if (audioDecoderStillNeeded() && mAudioDecoder == NULL) {
962 instantiateDecoder(true /* audio */, &mAudioDecoder);
963 }
964 if (mRenderer != NULL) {
965 mRenderer->resume();
966 } else {
967 ALOGW("resume called when renderer is gone or not set");
968 }
969}
970
971void NuPlayer::onStart() {
972 mVideoIsAVC = false;
973 mOffloadAudio = false;
974 mAudioEOS = false;
975 mVideoEOS = false;
Wei Jia94211742014-10-28 17:09:06 -0700976 mNumFramesTotal = 0;
977 mNumFramesDropped = 0;
978 mStarted = true;
979
980 /* instantiate decoders now for secure playback */
981 if (mSourceFlags & Source::FLAG_SECURE) {
982 if (mNativeWindow != NULL) {
983 instantiateDecoder(false, &mVideoDecoder);
984 }
985
986 if (mAudioSink != NULL) {
987 instantiateDecoder(true, &mAudioDecoder);
988 }
989 }
990
991 mSource->start();
992
993 uint32_t flags = 0;
994
995 if (mSource->isRealTime()) {
996 flags |= Renderer::FLAG_REAL_TIME;
997 }
998
999 sp<MetaData> audioMeta = mSource->getFormatMeta(true /* audio */);
1000 audio_stream_type_t streamType = AUDIO_STREAM_MUSIC;
1001 if (mAudioSink != NULL) {
1002 streamType = mAudioSink->getAudioStreamType();
1003 }
1004
1005 sp<AMessage> videoFormat = mSource->getFormat(false /* audio */);
1006
1007 mOffloadAudio =
1008 canOffloadStream(audioMeta, (videoFormat != NULL),
1009 true /* is_streaming */, streamType);
1010 if (mOffloadAudio) {
1011 flags |= Renderer::FLAG_OFFLOAD_AUDIO;
1012 }
1013
1014 sp<AMessage> notify = new AMessage(kWhatRendererNotify, id());
1015 ++mRendererGeneration;
1016 notify->setInt32("generation", mRendererGeneration);
1017 mRenderer = new Renderer(mAudioSink, notify, flags);
1018
1019 mRendererLooper = new ALooper;
1020 mRendererLooper->setName("NuPlayerRenderer");
1021 mRendererLooper->start(false, false, ANDROID_PRIORITY_AUDIO);
1022 mRendererLooper->registerHandler(mRenderer);
1023
1024 sp<MetaData> meta = getFileMeta();
1025 int32_t rate;
1026 if (meta != NULL
1027 && meta->findInt32(kKeyFrameRate, &rate) && rate > 0) {
1028 mRenderer->setVideoFrameRate(rate);
1029 }
1030
Wei Jiac6cfd702014-11-11 16:33:20 -08001031 if (mVideoDecoder != NULL) {
1032 mVideoDecoder->setRenderer(mRenderer);
1033 }
1034 if (mAudioDecoder != NULL) {
1035 mAudioDecoder->setRenderer(mRenderer);
1036 }
1037
Wei Jia94211742014-10-28 17:09:06 -07001038 postScanSources();
1039}
1040
Ronghua Wud7988b12014-10-03 15:19:10 -07001041bool NuPlayer::audioDecoderStillNeeded() {
1042 // Audio decoder is no longer needed if it's in shut/shutting down status.
1043 return ((mFlushingAudio != SHUT_DOWN) && (mFlushingAudio != SHUTTING_DOWN_DECODER));
1044}
1045
Andy Hung8d121d42014-10-03 09:53:53 -07001046void NuPlayer::handleFlushComplete(bool audio, bool isDecoder) {
1047 // We wait for both the decoder flush and the renderer flush to complete
1048 // before entering either the FLUSHED or the SHUTTING_DOWN_DECODER state.
1049
1050 mFlushComplete[audio][isDecoder] = true;
1051 if (!mFlushComplete[audio][!isDecoder]) {
1052 return;
1053 }
1054
1055 FlushStatus *state = audio ? &mFlushingAudio : &mFlushingVideo;
1056 switch (*state) {
1057 case FLUSHING_DECODER:
1058 {
1059 *state = FLUSHED;
Andy Hung8d121d42014-10-03 09:53:53 -07001060 break;
1061 }
1062
1063 case FLUSHING_DECODER_SHUTDOWN:
1064 {
1065 *state = SHUTTING_DOWN_DECODER;
1066
1067 ALOGV("initiating %s decoder shutdown", audio ? "audio" : "video");
1068 if (!audio) {
Andy Hung8d121d42014-10-03 09:53:53 -07001069 // Widevine source reads must stop before releasing the video decoder.
1070 if (mSource != NULL && mSourceFlags & Source::FLAG_SECURE) {
1071 mSource->stop();
1072 }
1073 }
1074 getDecoder(audio)->initiateShutdown();
1075 break;
1076 }
1077
1078 default:
1079 // decoder flush completes only occur in a flushing state.
1080 LOG_ALWAYS_FATAL_IF(isDecoder, "decoder flush in invalid state %d", *state);
1081 break;
1082 }
1083}
1084
Andreas Huber3831a062010-12-21 10:22:33 -08001085void NuPlayer::finishFlushIfPossible() {
Wei Jia53904f32014-07-29 10:22:53 -07001086 if (mFlushingAudio != NONE && mFlushingAudio != FLUSHED
1087 && mFlushingAudio != SHUT_DOWN) {
Andreas Huber3831a062010-12-21 10:22:33 -08001088 return;
1089 }
1090
Wei Jia53904f32014-07-29 10:22:53 -07001091 if (mFlushingVideo != NONE && mFlushingVideo != FLUSHED
1092 && mFlushingVideo != SHUT_DOWN) {
Andreas Huber3831a062010-12-21 10:22:33 -08001093 return;
1094 }
1095
Steve Block3856b092011-10-20 11:56:00 +01001096 ALOGV("both audio and video are flushed now.");
Andreas Huber3831a062010-12-21 10:22:33 -08001097
Phil Burk9f526492014-09-03 15:04:12 -07001098 mPendingAudioAccessUnit.clear();
Phil Burkc5cc2e22014-09-09 20:08:39 -07001099 mAggregateBuffer.clear();
Phil Burk9f526492014-09-03 15:04:12 -07001100
Andreas Huber6e3d3112011-11-28 12:36:11 -08001101 if (mTimeDiscontinuityPending) {
1102 mRenderer->signalTimeDiscontinuity();
1103 mTimeDiscontinuityPending = false;
1104 }
Andreas Huber3831a062010-12-21 10:22:33 -08001105
Wei Jia53904f32014-07-29 10:22:53 -07001106 if (mAudioDecoder != NULL && mFlushingAudio == FLUSHED) {
Andreas Huber3831a062010-12-21 10:22:33 -08001107 mAudioDecoder->signalResume();
1108 }
1109
Wei Jia53904f32014-07-29 10:22:53 -07001110 if (mVideoDecoder != NULL && mFlushingVideo == FLUSHED) {
Andreas Huber3831a062010-12-21 10:22:33 -08001111 mVideoDecoder->signalResume();
1112 }
1113
1114 mFlushingAudio = NONE;
1115 mFlushingVideo = NONE;
Andreas Huber3831a062010-12-21 10:22:33 -08001116
Andy Hung8d121d42014-10-03 09:53:53 -07001117 clearFlushComplete();
1118
Andreas Hubera1f8ab02012-11-30 10:53:22 -08001119 processDeferredActions();
Andreas Huber1aef2112011-01-04 14:01:29 -08001120}
1121
1122void NuPlayer::postScanSources() {
1123 if (mScanSourcesPending) {
1124 return;
1125 }
1126
1127 sp<AMessage> msg = new AMessage(kWhatScanSources, id());
1128 msg->setInt32("generation", mScanSourcesGeneration);
1129 msg->post();
1130
1131 mScanSourcesPending = true;
1132}
1133
Andy Hung282a7e32014-08-14 15:56:34 -07001134void NuPlayer::openAudioSink(const sp<AMessage> &format, bool offloadOnly) {
Andy Hung282a7e32014-08-14 15:56:34 -07001135 uint32_t flags;
1136 int64_t durationUs;
Chong Zhang3b9eb1f2014-10-15 17:05:08 -07001137 bool hasVideo = (mVideoDecoder != NULL);
Andy Hung282a7e32014-08-14 15:56:34 -07001138 // FIXME: we should handle the case where the video decoder
1139 // is created after we receive the format change indication.
1140 // Current code will just make that we select deep buffer
1141 // with video which should not be a problem as it should
1142 // not prevent from keeping A/V sync.
Eric Laurentd88c3ca2014-10-28 10:52:11 -07001143 if (!hasVideo &&
Andy Hung282a7e32014-08-14 15:56:34 -07001144 mSource->getDuration(&durationUs) == OK &&
1145 durationUs
1146 > AUDIO_SINK_MIN_DEEP_BUFFER_DURATION_US) {
1147 flags = AUDIO_OUTPUT_FLAG_DEEP_BUFFER;
1148 } else {
1149 flags = AUDIO_OUTPUT_FLAG_NONE;
1150 }
1151
Chong Zhang3b9eb1f2014-10-15 17:05:08 -07001152 mOffloadAudio = mRenderer->openAudioSink(
1153 format, offloadOnly, hasVideo, flags);
1154
Andy Hung282a7e32014-08-14 15:56:34 -07001155 if (mOffloadAudio) {
Chong Zhang3b9eb1f2014-10-15 17:05:08 -07001156 sp<MetaData> audioMeta =
1157 mSource->getFormatMeta(true /* audio */);
1158 sendMetaDataToHal(mAudioSink, audioMeta);
Andy Hung282a7e32014-08-14 15:56:34 -07001159 }
1160}
1161
1162void NuPlayer::closeAudioSink() {
Chong Zhang3b9eb1f2014-10-15 17:05:08 -07001163 mRenderer->closeAudioSink();
Andy Hung282a7e32014-08-14 15:56:34 -07001164}
1165
Andreas Huber5bc087c2010-12-23 10:27:40 -08001166status_t NuPlayer::instantiateDecoder(bool audio, sp<Decoder> *decoder) {
Andreas Huberf9334412010-12-15 15:17:42 -08001167 if (*decoder != NULL) {
1168 return OK;
1169 }
1170
Andreas Huber84066782011-08-16 09:34:26 -07001171 sp<AMessage> format = mSource->getFormat(audio);
Andreas Huberf9334412010-12-15 15:17:42 -08001172
Andreas Huber84066782011-08-16 09:34:26 -07001173 if (format == NULL) {
Andreas Huberf9334412010-12-15 15:17:42 -08001174 return -EWOULDBLOCK;
1175 }
1176
Andreas Huber3fe62152011-09-16 15:09:22 -07001177 if (!audio) {
Andreas Huber84066782011-08-16 09:34:26 -07001178 AString mime;
1179 CHECK(format->findString("mime", &mime));
1180 mVideoIsAVC = !strcasecmp(MEDIA_MIMETYPE_VIDEO_AVC, mime.c_str());
Chong Zhanga7fa1d92014-06-11 14:49:23 -07001181
1182 sp<AMessage> ccNotify = new AMessage(kWhatClosedCaptionNotify, id());
1183 mCCDecoder = new CCDecoder(ccNotify);
Lajos Molnar09524832014-07-17 14:29:51 -07001184
1185 if (mSourceFlags & Source::FLAG_SECURE) {
1186 format->setInt32("secure", true);
1187 }
Andreas Huber3fe62152011-09-16 15:09:22 -07001188 }
1189
Wei Jiabc2fb722014-07-08 16:37:57 -07001190 if (audio) {
Wei Jia88703c32014-08-06 11:24:07 -07001191 sp<AMessage> notify = new AMessage(kWhatAudioNotify, id());
1192 ++mAudioDecoderGeneration;
1193 notify->setInt32("generation", mAudioDecoderGeneration);
1194
Wei Jiabc2fb722014-07-08 16:37:57 -07001195 if (mOffloadAudio) {
Wei Jiac6cfd702014-11-11 16:33:20 -08001196 *decoder = new DecoderPassThrough(notify, mSource, mRenderer);
Wei Jiabc2fb722014-07-08 16:37:57 -07001197 } else {
Wei Jiac6cfd702014-11-11 16:33:20 -08001198 *decoder = new Decoder(notify, mSource, mRenderer);
Wei Jiabc2fb722014-07-08 16:37:57 -07001199 }
1200 } else {
Wei Jia88703c32014-08-06 11:24:07 -07001201 sp<AMessage> notify = new AMessage(kWhatVideoNotify, id());
1202 ++mVideoDecoderGeneration;
1203 notify->setInt32("generation", mVideoDecoderGeneration);
1204
Wei Jiac6cfd702014-11-11 16:33:20 -08001205 *decoder = new Decoder(notify, mSource, mRenderer, mNativeWindow);
Lajos Molnard9fd6312014-11-06 11:00:00 -08001206
1207 // enable FRC if high-quality AV sync is requested, even if not
1208 // queuing to native window, as this will even improve textureview
1209 // playback.
1210 {
1211 char value[PROPERTY_VALUE_MAX];
1212 if (property_get("persist.sys.media.avsync", value, NULL) &&
1213 (!strcmp("1", value) || !strcasecmp("true", value))) {
1214 format->setInt32("auto-frc", 1);
1215 }
1216 }
Wei Jiabc2fb722014-07-08 16:37:57 -07001217 }
Lajos Molnar1cd13982014-01-17 15:12:51 -08001218 (*decoder)->init();
Andreas Huber84066782011-08-16 09:34:26 -07001219 (*decoder)->configure(format);
Andreas Huberf9334412010-12-15 15:17:42 -08001220
Lajos Molnar09524832014-07-17 14:29:51 -07001221 // allocate buffers to decrypt widevine source buffers
1222 if (!audio && (mSourceFlags & Source::FLAG_SECURE)) {
1223 Vector<sp<ABuffer> > inputBufs;
1224 CHECK_EQ((*decoder)->getInputBuffers(&inputBufs), (status_t)OK);
1225
1226 Vector<MediaBuffer *> mediaBufs;
1227 for (size_t i = 0; i < inputBufs.size(); i++) {
1228 const sp<ABuffer> &buffer = inputBufs[i];
1229 MediaBuffer *mbuf = new MediaBuffer(buffer->data(), buffer->size());
1230 mediaBufs.push(mbuf);
1231 }
1232
1233 status_t err = mSource->setBuffers(audio, mediaBufs);
1234 if (err != OK) {
1235 for (size_t i = 0; i < mediaBufs.size(); ++i) {
1236 mediaBufs[i]->release();
1237 }
1238 mediaBufs.clear();
1239 ALOGE("Secure source didn't support secure mediaBufs.");
1240 return err;
1241 }
1242 }
Andreas Huberf9334412010-12-15 15:17:42 -08001243 return OK;
1244}
1245
1246status_t NuPlayer::feedDecoderInputData(bool audio, const sp<AMessage> &msg) {
1247 sp<AMessage> reply;
1248 CHECK(msg->findMessage("reply", &reply));
1249
Wei Jia53904f32014-07-29 10:22:53 -07001250 if ((audio && mFlushingAudio != NONE)
Wei Jiaf702d042014-09-09 12:08:47 -07001251 || (!audio && mFlushingVideo != NONE)
1252 || mSource == NULL) {
Wei Jiab189a5b2014-08-07 06:11:39 +00001253 reply->setInt32("err", INFO_DISCONTINUITY);
1254 reply->post();
1255 return OK;
Andreas Huberf9334412010-12-15 15:17:42 -08001256 }
1257
1258 sp<ABuffer> accessUnit;
Andreas Huberf9334412010-12-15 15:17:42 -08001259
Phil Burk9f526492014-09-03 15:04:12 -07001260 // Aggregate smaller buffers into a larger buffer.
1261 // The goal is to reduce power consumption.
Phil Burk33b51b02014-09-17 16:03:47 -07001262 // Note this will not work if the decoder requires one frame per buffer.
1263 bool doBufferAggregation = (audio && mOffloadAudio);
Phil Burk9f526492014-09-03 15:04:12 -07001264 bool needMoreData = false;
Phil Burk9f526492014-09-03 15:04:12 -07001265
Andreas Huber3fe62152011-09-16 15:09:22 -07001266 bool dropAccessUnit;
1267 do {
Phil Burk9f526492014-09-03 15:04:12 -07001268 status_t err;
1269 // Did we save an accessUnit earlier because of a discontinuity?
1270 if (audio && (mPendingAudioAccessUnit != NULL)) {
1271 accessUnit = mPendingAudioAccessUnit;
1272 mPendingAudioAccessUnit.clear();
1273 err = mPendingAudioErr;
1274 ALOGV("feedDecoderInputData() use mPendingAudioAccessUnit");
1275 } else {
1276 err = mSource->dequeueAccessUnit(audio, &accessUnit);
1277 }
Andreas Huber5bc087c2010-12-23 10:27:40 -08001278
Andreas Huber3fe62152011-09-16 15:09:22 -07001279 if (err == -EWOULDBLOCK) {
Phil Burkc5cc2e22014-09-09 20:08:39 -07001280 return err;
Andreas Huber3fe62152011-09-16 15:09:22 -07001281 } else if (err != OK) {
1282 if (err == INFO_DISCONTINUITY) {
Phil Burk33b51b02014-09-17 16:03:47 -07001283 if (doBufferAggregation && (mAggregateBuffer != NULL)) {
Phil Burk9f526492014-09-03 15:04:12 -07001284 // We already have some data so save this for later.
1285 mPendingAudioErr = err;
1286 mPendingAudioAccessUnit = accessUnit;
1287 accessUnit.clear();
1288 ALOGD("feedDecoderInputData() save discontinuity for later");
1289 break;
1290 }
Andreas Huber3fe62152011-09-16 15:09:22 -07001291 int32_t type;
1292 CHECK(accessUnit->meta()->findInt32("discontinuity", &type));
Andreas Huber53df1a42010-12-22 10:03:04 -08001293
Andreas Huber3fe62152011-09-16 15:09:22 -07001294 bool formatChange =
Andreas Huber6e3d3112011-11-28 12:36:11 -08001295 (audio &&
1296 (type & ATSParser::DISCONTINUITY_AUDIO_FORMAT))
1297 || (!audio &&
1298 (type & ATSParser::DISCONTINUITY_VIDEO_FORMAT));
Andreas Huber53df1a42010-12-22 10:03:04 -08001299
Andreas Huber6e3d3112011-11-28 12:36:11 -08001300 bool timeChange = (type & ATSParser::DISCONTINUITY_TIME) != 0;
1301
Steve Blockdf64d152012-01-04 20:05:49 +00001302 ALOGI("%s discontinuity (formatChange=%d, time=%d)",
Andreas Huber6e3d3112011-11-28 12:36:11 -08001303 audio ? "audio" : "video", formatChange, timeChange);
Andreas Huber32f3cef2011-03-02 15:34:46 -08001304
Andreas Huber6e3d3112011-11-28 12:36:11 -08001305 mTimeDiscontinuityPending =
1306 mTimeDiscontinuityPending || timeChange;
1307
Lajos Molnar87603c02014-08-20 19:25:30 -07001308 bool seamlessFormatChange = false;
1309 sp<AMessage> newFormat = mSource->getFormat(audio);
1310 if (formatChange) {
1311 seamlessFormatChange =
1312 getDecoder(audio)->supportsSeamlessFormatChange(newFormat);
1313 // treat seamless format change separately
1314 formatChange = !seamlessFormatChange;
1315 }
1316 bool shutdownOrFlush = formatChange || timeChange;
1317
1318 // We want to queue up scan-sources only once per discontinuity.
1319 // We control this by doing it only if neither audio nor video are
1320 // flushing or shutting down. (After handling 1st discontinuity, one
1321 // of the flushing states will not be NONE.)
1322 // No need to scan sources if this discontinuity does not result
1323 // in a flush or shutdown, as the flushing state will stay NONE.
1324 if (mFlushingAudio == NONE && mFlushingVideo == NONE &&
1325 shutdownOrFlush) {
Robert Shiha2981012014-07-30 17:41:24 -07001326 // And we'll resume scanning sources once we're done
1327 // flushing.
1328 mDeferredActions.push_front(
1329 new SimpleAction(
1330 &NuPlayer::performScanSources));
1331 }
1332
Lajos Molnar87603c02014-08-20 19:25:30 -07001333 if (formatChange /* not seamless */) {
1334 // must change decoder
1335 flushDecoder(audio, /* needShutdown = */ true);
1336 } else if (timeChange) {
1337 // need to flush
1338 flushDecoder(audio, /* needShutdown = */ false, newFormat);
1339 err = OK;
1340 } else if (seamlessFormatChange) {
1341 // reuse existing decoder and don't flush
1342 updateDecoderFormatWithoutFlush(audio, newFormat);
1343 err = OK;
Andreas Huber6e3d3112011-11-28 12:36:11 -08001344 } else {
1345 // This stream is unaffected by the discontinuity
Andreas Huber6e3d3112011-11-28 12:36:11 -08001346 return -EWOULDBLOCK;
1347 }
Andreas Huber32f3cef2011-03-02 15:34:46 -08001348 }
1349
Andreas Huber3fe62152011-09-16 15:09:22 -07001350 reply->setInt32("err", err);
1351 reply->post();
1352 return OK;
Andreas Huberf9334412010-12-15 15:17:42 -08001353 }
1354
Andreas Huber3fe62152011-09-16 15:09:22 -07001355 if (!audio) {
1356 ++mNumFramesTotal;
1357 }
1358
1359 dropAccessUnit = false;
1360 if (!audio
Lajos Molnar09524832014-07-17 14:29:51 -07001361 && !(mSourceFlags & Source::FLAG_SECURE)
Ronghua Wua73d9e02014-10-08 15:13:29 -07001362 && mRenderer->getVideoLateByUs() > 100000ll
Andreas Huber3fe62152011-09-16 15:09:22 -07001363 && mVideoIsAVC
1364 && !IsAVCReferenceFrame(accessUnit)) {
1365 dropAccessUnit = true;
1366 ++mNumFramesDropped;
1367 }
Phil Burk9f526492014-09-03 15:04:12 -07001368
1369 size_t smallSize = accessUnit->size();
1370 needMoreData = false;
Phil Burkc5cc2e22014-09-09 20:08:39 -07001371 if (doBufferAggregation && (mAggregateBuffer == NULL)
Phil Burk9f526492014-09-03 15:04:12 -07001372 // Don't bother if only room for a few small buffers.
Phil Burkc5cc2e22014-09-09 20:08:39 -07001373 && (smallSize < (kAggregateBufferSizeBytes / 3))) {
Phil Burk9f526492014-09-03 15:04:12 -07001374 // Create a larger buffer for combining smaller buffers from the extractor.
Phil Burkc5cc2e22014-09-09 20:08:39 -07001375 mAggregateBuffer = new ABuffer(kAggregateBufferSizeBytes);
1376 mAggregateBuffer->setRange(0, 0); // start empty
Phil Burk9f526492014-09-03 15:04:12 -07001377 }
1378
Phil Burk33b51b02014-09-17 16:03:47 -07001379 if (doBufferAggregation && (mAggregateBuffer != NULL)) {
Phil Burk9f526492014-09-03 15:04:12 -07001380 int64_t timeUs;
Phil Burkc5cc2e22014-09-09 20:08:39 -07001381 int64_t dummy;
Phil Burk9f526492014-09-03 15:04:12 -07001382 bool smallTimestampValid = accessUnit->meta()->findInt64("timeUs", &timeUs);
Phil Burkc5cc2e22014-09-09 20:08:39 -07001383 bool bigTimestampValid = mAggregateBuffer->meta()->findInt64("timeUs", &dummy);
Phil Burk9f526492014-09-03 15:04:12 -07001384 // Will the smaller buffer fit?
Phil Burkc5cc2e22014-09-09 20:08:39 -07001385 size_t bigSize = mAggregateBuffer->size();
1386 size_t roomLeft = mAggregateBuffer->capacity() - bigSize;
Phil Burk9f526492014-09-03 15:04:12 -07001387 // Should we save this small buffer for the next big buffer?
1388 // If the first small buffer did not have a timestamp then save
1389 // any buffer that does have a timestamp until the next big buffer.
1390 if ((smallSize > roomLeft)
Phil Burkc5cc2e22014-09-09 20:08:39 -07001391 || (!bigTimestampValid && (bigSize > 0) && smallTimestampValid)) {
Phil Burk9f526492014-09-03 15:04:12 -07001392 mPendingAudioErr = err;
1393 mPendingAudioAccessUnit = accessUnit;
1394 accessUnit.clear();
1395 } else {
Phil Burkc5cc2e22014-09-09 20:08:39 -07001396 // Grab time from first small buffer if available.
1397 if ((bigSize == 0) && smallTimestampValid) {
1398 mAggregateBuffer->meta()->setInt64("timeUs", timeUs);
1399 }
Phil Burk9f526492014-09-03 15:04:12 -07001400 // Append small buffer to the bigger buffer.
Phil Burkc5cc2e22014-09-09 20:08:39 -07001401 memcpy(mAggregateBuffer->base() + bigSize, accessUnit->data(), smallSize);
Phil Burk9f526492014-09-03 15:04:12 -07001402 bigSize += smallSize;
Phil Burkc5cc2e22014-09-09 20:08:39 -07001403 mAggregateBuffer->setRange(0, bigSize);
Phil Burk9f526492014-09-03 15:04:12 -07001404
Phil Burkc5cc2e22014-09-09 20:08:39 -07001405 // Keep looping until we run out of room in the mAggregateBuffer.
Phil Burk9f526492014-09-03 15:04:12 -07001406 needMoreData = true;
1407
Phil Burkc5cc2e22014-09-09 20:08:39 -07001408 ALOGV("feedDecoderInputData() smallSize = %zu, bigSize = %zu, capacity = %zu",
1409 smallSize, bigSize, mAggregateBuffer->capacity());
Phil Burk9f526492014-09-03 15:04:12 -07001410 }
1411 }
1412 } while (dropAccessUnit || needMoreData);
Andreas Huberf9334412010-12-15 15:17:42 -08001413
Steve Block3856b092011-10-20 11:56:00 +01001414 // ALOGV("returned a valid buffer of %s data", audio ? "audio" : "video");
Andreas Huberf9334412010-12-15 15:17:42 -08001415
1416#if 0
1417 int64_t mediaTimeUs;
1418 CHECK(accessUnit->meta()->findInt64("timeUs", &mediaTimeUs));
Steve Block3856b092011-10-20 11:56:00 +01001419 ALOGV("feeding %s input buffer at media time %.2f secs",
Andreas Huberf9334412010-12-15 15:17:42 -08001420 audio ? "audio" : "video",
1421 mediaTimeUs / 1E6);
1422#endif
1423
Chong Zhanga7fa1d92014-06-11 14:49:23 -07001424 if (!audio) {
1425 mCCDecoder->decode(accessUnit);
1426 }
1427
Phil Burk33b51b02014-09-17 16:03:47 -07001428 if (doBufferAggregation && (mAggregateBuffer != NULL)) {
Phil Burkc5cc2e22014-09-09 20:08:39 -07001429 ALOGV("feedDecoderInputData() reply with aggregated buffer, %zu",
1430 mAggregateBuffer->size());
1431 reply->setBuffer("buffer", mAggregateBuffer);
1432 mAggregateBuffer.clear();
Phil Burk9f526492014-09-03 15:04:12 -07001433 } else {
1434 reply->setBuffer("buffer", accessUnit);
1435 }
1436
Andreas Huberf9334412010-12-15 15:17:42 -08001437 reply->post();
1438
1439 return OK;
1440}
1441
1442void NuPlayer::renderBuffer(bool audio, const sp<AMessage> &msg) {
Steve Block3856b092011-10-20 11:56:00 +01001443 // ALOGV("renderBuffer %s", audio ? "audio" : "video");
Andreas Huberf9334412010-12-15 15:17:42 -08001444
Wei Jia53904f32014-07-29 10:22:53 -07001445 if ((audio && mFlushingAudio != NONE)
1446 || (!audio && mFlushingVideo != NONE)) {
Andreas Huber18ac5402011-08-31 15:04:25 -07001447 // We're currently attempting to flush the decoder, in order
1448 // to complete this, the decoder wants all its buffers back,
1449 // so we don't want any output buffers it sent us (from before
1450 // we initiated the flush) to be stuck in the renderer's queue.
1451
Steve Block3856b092011-10-20 11:56:00 +01001452 ALOGV("we're still flushing the %s decoder, sending its output buffer"
Andreas Huber18ac5402011-08-31 15:04:25 -07001453 " right back.", audio ? "audio" : "video");
1454
Andreas Huber18ac5402011-08-31 15:04:25 -07001455 return;
1456 }
1457
Chong Zhanga7fa1d92014-06-11 14:49:23 -07001458 int64_t mediaTimeUs;
Wei Jiac6cfd702014-11-11 16:33:20 -08001459 CHECK(msg->findInt64("timeUs", &mediaTimeUs));
Andreas Huber32f3cef2011-03-02 15:34:46 -08001460
Chong Zhanga7fa1d92014-06-11 14:49:23 -07001461 if (!audio && mCCDecoder->isSelected()) {
1462 mCCDecoder->display(mediaTimeUs);
1463 }
Andreas Huberf9334412010-12-15 15:17:42 -08001464}
1465
Chong Zhangced1c2f2014-08-08 15:22:35 -07001466void NuPlayer::updateVideoSize(
1467 const sp<AMessage> &inputFormat,
1468 const sp<AMessage> &outputFormat) {
1469 if (inputFormat == NULL) {
1470 ALOGW("Unknown video size, reporting 0x0!");
1471 notifyListener(MEDIA_SET_VIDEO_SIZE, 0, 0);
1472 return;
1473 }
1474
1475 int32_t displayWidth, displayHeight;
1476 int32_t cropLeft, cropTop, cropRight, cropBottom;
1477
1478 if (outputFormat != NULL) {
1479 int32_t width, height;
1480 CHECK(outputFormat->findInt32("width", &width));
1481 CHECK(outputFormat->findInt32("height", &height));
1482
1483 int32_t cropLeft, cropTop, cropRight, cropBottom;
1484 CHECK(outputFormat->findRect(
1485 "crop",
1486 &cropLeft, &cropTop, &cropRight, &cropBottom));
1487
1488 displayWidth = cropRight - cropLeft + 1;
1489 displayHeight = cropBottom - cropTop + 1;
1490
1491 ALOGV("Video output format changed to %d x %d "
1492 "(crop: %d x %d @ (%d, %d))",
1493 width, height,
1494 displayWidth,
1495 displayHeight,
1496 cropLeft, cropTop);
1497 } else {
1498 CHECK(inputFormat->findInt32("width", &displayWidth));
1499 CHECK(inputFormat->findInt32("height", &displayHeight));
1500
1501 ALOGV("Video input format %d x %d", displayWidth, displayHeight);
1502 }
1503
1504 // Take into account sample aspect ratio if necessary:
1505 int32_t sarWidth, sarHeight;
1506 if (inputFormat->findInt32("sar-width", &sarWidth)
1507 && inputFormat->findInt32("sar-height", &sarHeight)) {
1508 ALOGV("Sample aspect ratio %d : %d", sarWidth, sarHeight);
1509
1510 displayWidth = (displayWidth * sarWidth) / sarHeight;
1511
1512 ALOGV("display dimensions %d x %d", displayWidth, displayHeight);
1513 }
1514
1515 int32_t rotationDegrees;
1516 if (!inputFormat->findInt32("rotation-degrees", &rotationDegrees)) {
1517 rotationDegrees = 0;
1518 }
1519
1520 if (rotationDegrees == 90 || rotationDegrees == 270) {
1521 int32_t tmp = displayWidth;
1522 displayWidth = displayHeight;
1523 displayHeight = tmp;
1524 }
1525
1526 notifyListener(
1527 MEDIA_SET_VIDEO_SIZE,
1528 displayWidth,
1529 displayHeight);
1530}
1531
Chong Zhangdcb89b32013-08-06 09:44:47 -07001532void NuPlayer::notifyListener(int msg, int ext1, int ext2, const Parcel *in) {
Andreas Huber43c3e6c2011-01-05 12:17:08 -08001533 if (mDriver == NULL) {
Andreas Huberf9334412010-12-15 15:17:42 -08001534 return;
1535 }
1536
Andreas Huber43c3e6c2011-01-05 12:17:08 -08001537 sp<NuPlayerDriver> driver = mDriver.promote();
Andreas Huberf9334412010-12-15 15:17:42 -08001538
Andreas Huber43c3e6c2011-01-05 12:17:08 -08001539 if (driver == NULL) {
Andreas Huberf9334412010-12-15 15:17:42 -08001540 return;
1541 }
1542
Chong Zhangdcb89b32013-08-06 09:44:47 -07001543 driver->notifyListener(msg, ext1, ext2, in);
Andreas Huberf9334412010-12-15 15:17:42 -08001544}
1545
Lajos Molnar87603c02014-08-20 19:25:30 -07001546void NuPlayer::flushDecoder(
1547 bool audio, bool needShutdown, const sp<AMessage> &newFormat) {
Andreas Huber14f76722013-01-15 09:04:18 -08001548 ALOGV("[%s] flushDecoder needShutdown=%d",
1549 audio ? "audio" : "video", needShutdown);
1550
Lajos Molnar87603c02014-08-20 19:25:30 -07001551 const sp<Decoder> &decoder = getDecoder(audio);
1552 if (decoder == NULL) {
Steve Blockdf64d152012-01-04 20:05:49 +00001553 ALOGI("flushDecoder %s without decoder present",
Andreas Huber6e3d3112011-11-28 12:36:11 -08001554 audio ? "audio" : "video");
Lajos Molnar87603c02014-08-20 19:25:30 -07001555 return;
Andreas Huber6e3d3112011-11-28 12:36:11 -08001556 }
1557
Andreas Huber1aef2112011-01-04 14:01:29 -08001558 // Make sure we don't continue to scan sources until we finish flushing.
1559 ++mScanSourcesGeneration;
Andreas Huber43c3e6c2011-01-05 12:17:08 -08001560 mScanSourcesPending = false;
Andreas Huber1aef2112011-01-04 14:01:29 -08001561
Lajos Molnar87603c02014-08-20 19:25:30 -07001562 decoder->signalFlush(newFormat);
Andreas Huber1aef2112011-01-04 14:01:29 -08001563
1564 FlushStatus newStatus =
1565 needShutdown ? FLUSHING_DECODER_SHUTDOWN : FLUSHING_DECODER;
1566
Andy Hung8d121d42014-10-03 09:53:53 -07001567 mFlushComplete[audio][false /* isDecoder */] = false;
1568 mFlushComplete[audio][true /* isDecoder */] = false;
Andreas Huber1aef2112011-01-04 14:01:29 -08001569 if (audio) {
Wei Jia53904f32014-07-29 10:22:53 -07001570 ALOGE_IF(mFlushingAudio != NONE,
1571 "audio flushDecoder() is called in state %d", mFlushingAudio);
Andreas Huber1aef2112011-01-04 14:01:29 -08001572 mFlushingAudio = newStatus;
Andreas Huber1aef2112011-01-04 14:01:29 -08001573 } else {
Wei Jia53904f32014-07-29 10:22:53 -07001574 ALOGE_IF(mFlushingVideo != NONE,
1575 "video flushDecoder() is called in state %d", mFlushingVideo);
Andreas Huber1aef2112011-01-04 14:01:29 -08001576 mFlushingVideo = newStatus;
Chong Zhangb86e68f2014-08-01 13:46:53 -07001577
1578 if (mCCDecoder != NULL) {
1579 mCCDecoder->flush();
1580 }
Andreas Huber1aef2112011-01-04 14:01:29 -08001581 }
1582}
1583
Lajos Molnar87603c02014-08-20 19:25:30 -07001584void NuPlayer::updateDecoderFormatWithoutFlush(
1585 bool audio, const sp<AMessage> &format) {
1586 ALOGV("[%s] updateDecoderFormatWithoutFlush", audio ? "audio" : "video");
1587
1588 const sp<Decoder> &decoder = getDecoder(audio);
1589 if (decoder == NULL) {
1590 ALOGI("updateDecoderFormatWithoutFlush %s without decoder present",
1591 audio ? "audio" : "video");
1592 return;
1593 }
1594
1595 decoder->signalUpdateFormat(format);
1596}
1597
Chong Zhangced1c2f2014-08-08 15:22:35 -07001598void NuPlayer::queueDecoderShutdown(
1599 bool audio, bool video, const sp<AMessage> &reply) {
1600 ALOGI("queueDecoderShutdown audio=%d, video=%d", audio, video);
Andreas Huber84066782011-08-16 09:34:26 -07001601
Chong Zhangced1c2f2014-08-08 15:22:35 -07001602 mDeferredActions.push_back(
Wei Jiafef808d2014-10-31 17:57:05 -07001603 new FlushDecoderAction(
1604 audio ? FLUSH_CMD_SHUTDOWN : FLUSH_CMD_NONE,
1605 video ? FLUSH_CMD_SHUTDOWN : FLUSH_CMD_NONE));
Andreas Huber84066782011-08-16 09:34:26 -07001606
Chong Zhangced1c2f2014-08-08 15:22:35 -07001607 mDeferredActions.push_back(
1608 new SimpleAction(&NuPlayer::performScanSources));
Andreas Huber84066782011-08-16 09:34:26 -07001609
Chong Zhangced1c2f2014-08-08 15:22:35 -07001610 mDeferredActions.push_back(new PostMessageAction(reply));
1611
1612 processDeferredActions();
Andreas Huber84066782011-08-16 09:34:26 -07001613}
1614
James Dong0d268a32012-08-31 12:18:27 -07001615status_t NuPlayer::setVideoScalingMode(int32_t mode) {
1616 mVideoScalingMode = mode;
Andreas Huber57a339c2012-12-03 11:18:00 -08001617 if (mNativeWindow != NULL) {
James Dong0d268a32012-08-31 12:18:27 -07001618 status_t ret = native_window_set_scaling_mode(
1619 mNativeWindow->getNativeWindow().get(), mVideoScalingMode);
1620 if (ret != OK) {
1621 ALOGE("Failed to set scaling mode (%d): %s",
1622 -ret, strerror(-ret));
1623 return ret;
1624 }
1625 }
1626 return OK;
1627}
1628
Chong Zhangdcb89b32013-08-06 09:44:47 -07001629status_t NuPlayer::getTrackInfo(Parcel* reply) const {
1630 sp<AMessage> msg = new AMessage(kWhatGetTrackInfo, id());
1631 msg->setPointer("reply", reply);
1632
1633 sp<AMessage> response;
1634 status_t err = msg->postAndAwaitResponse(&response);
1635 return err;
1636}
1637
Robert Shih7c4f0d72014-07-09 18:53:31 -07001638status_t NuPlayer::getSelectedTrack(int32_t type, Parcel* reply) const {
1639 sp<AMessage> msg = new AMessage(kWhatGetSelectedTrack, id());
1640 msg->setPointer("reply", reply);
1641 msg->setInt32("type", type);
1642
1643 sp<AMessage> response;
1644 status_t err = msg->postAndAwaitResponse(&response);
1645 if (err == OK && response != NULL) {
1646 CHECK(response->findInt32("err", &err));
1647 }
1648 return err;
1649}
1650
Robert Shih6ffb1fd2014-10-29 16:24:32 -07001651status_t NuPlayer::selectTrack(size_t trackIndex, bool select, int64_t timeUs) {
Chong Zhangdcb89b32013-08-06 09:44:47 -07001652 sp<AMessage> msg = new AMessage(kWhatSelectTrack, id());
1653 msg->setSize("trackIndex", trackIndex);
1654 msg->setInt32("select", select);
Robert Shih6ffb1fd2014-10-29 16:24:32 -07001655 msg->setInt64("timeUs", timeUs);
Chong Zhangdcb89b32013-08-06 09:44:47 -07001656
1657 sp<AMessage> response;
1658 status_t err = msg->postAndAwaitResponse(&response);
1659
Chong Zhang404fced2014-06-11 14:45:31 -07001660 if (err != OK) {
1661 return err;
1662 }
1663
1664 if (!response->findInt32("err", &err)) {
1665 err = OK;
1666 }
1667
Chong Zhangdcb89b32013-08-06 09:44:47 -07001668 return err;
1669}
1670
Ronghua Wua73d9e02014-10-08 15:13:29 -07001671status_t NuPlayer::getCurrentPosition(int64_t *mediaUs) {
1672 sp<Renderer> renderer = mRenderer;
1673 if (renderer == NULL) {
1674 return NO_INIT;
1675 }
1676
1677 return renderer->getCurrentPosition(mediaUs);
1678}
1679
1680void NuPlayer::getStats(int64_t *numFramesTotal, int64_t *numFramesDropped) {
1681 *numFramesTotal = mNumFramesTotal;
1682 *numFramesDropped = mNumFramesDropped;
1683}
1684
Marco Nelissenf0b72b52014-09-16 15:43:44 -07001685sp<MetaData> NuPlayer::getFileMeta() {
1686 return mSource->getFileFormatMeta();
1687}
1688
Andreas Huberb7c8e912012-11-27 15:02:53 -08001689void NuPlayer::schedulePollDuration() {
1690 sp<AMessage> msg = new AMessage(kWhatPollDuration, id());
1691 msg->setInt32("generation", mPollDurationGeneration);
1692 msg->post();
1693}
1694
1695void NuPlayer::cancelPollDuration() {
1696 ++mPollDurationGeneration;
1697}
1698
Andreas Hubera1f8ab02012-11-30 10:53:22 -08001699void NuPlayer::processDeferredActions() {
1700 while (!mDeferredActions.empty()) {
1701 // We won't execute any deferred actions until we're no longer in
1702 // an intermediate state, i.e. one more more decoders are currently
1703 // flushing or shutting down.
1704
Andreas Hubera1f8ab02012-11-30 10:53:22 -08001705 if (mFlushingAudio != NONE || mFlushingVideo != NONE) {
1706 // We're currently flushing, postpone the reset until that's
1707 // completed.
1708
1709 ALOGV("postponing action mFlushingAudio=%d, mFlushingVideo=%d",
1710 mFlushingAudio, mFlushingVideo);
1711
1712 break;
1713 }
1714
1715 sp<Action> action = *mDeferredActions.begin();
1716 mDeferredActions.erase(mDeferredActions.begin());
1717
1718 action->execute(this);
1719 }
1720}
1721
Wei Jiae427abf2014-09-22 15:21:11 -07001722void NuPlayer::performSeek(int64_t seekTimeUs, bool needNotify) {
1723 ALOGV("performSeek seekTimeUs=%lld us (%.2f secs), needNotify(%d)",
Andreas Hubera1f8ab02012-11-30 10:53:22 -08001724 seekTimeUs,
Wei Jiae427abf2014-09-22 15:21:11 -07001725 seekTimeUs / 1E6,
1726 needNotify);
Andreas Hubera1f8ab02012-11-30 10:53:22 -08001727
Andy Hungadf34bf2014-09-03 18:22:22 -07001728 if (mSource == NULL) {
1729 // This happens when reset occurs right before the loop mode
1730 // asynchronously seeks to the start of the stream.
1731 LOG_ALWAYS_FATAL_IF(mAudioDecoder != NULL || mVideoDecoder != NULL,
1732 "mSource is NULL and decoders not NULL audio(%p) video(%p)",
1733 mAudioDecoder.get(), mVideoDecoder.get());
1734 return;
1735 }
Andreas Hubera1f8ab02012-11-30 10:53:22 -08001736 mSource->seekTo(seekTimeUs);
Robert Shihd3b0bbb2014-07-23 15:00:25 -07001737 ++mTimedTextGeneration;
Andreas Hubera1f8ab02012-11-30 10:53:22 -08001738
1739 if (mDriver != NULL) {
1740 sp<NuPlayerDriver> driver = mDriver.promote();
1741 if (driver != NULL) {
Wei Jiae427abf2014-09-22 15:21:11 -07001742 if (needNotify) {
1743 driver->notifySeekComplete();
1744 }
Andreas Hubera1f8ab02012-11-30 10:53:22 -08001745 }
1746 }
1747
1748 // everything's flushed, continue playback.
1749}
1750
Wei Jiafef808d2014-10-31 17:57:05 -07001751void NuPlayer::performDecoderFlush(FlushCommand audio, FlushCommand video) {
1752 ALOGV("performDecoderFlush audio=%d, video=%d", audio, video);
Andreas Hubera1f8ab02012-11-30 10:53:22 -08001753
Wei Jiafef808d2014-10-31 17:57:05 -07001754 if ((audio == FLUSH_CMD_NONE || mAudioDecoder == NULL)
1755 && (video == FLUSH_CMD_NONE || mVideoDecoder == NULL)) {
Andreas Hubera1f8ab02012-11-30 10:53:22 -08001756 return;
1757 }
1758
1759 mTimeDiscontinuityPending = true;
1760
Wei Jiafef808d2014-10-31 17:57:05 -07001761 if (audio != FLUSH_CMD_NONE && mAudioDecoder != NULL) {
1762 flushDecoder(true /* audio */, (audio == FLUSH_CMD_SHUTDOWN));
Andreas Hubera1f8ab02012-11-30 10:53:22 -08001763 }
1764
Wei Jiafef808d2014-10-31 17:57:05 -07001765 if (video != FLUSH_CMD_NONE && mVideoDecoder != NULL) {
1766 flushDecoder(false /* audio */, (video == FLUSH_CMD_SHUTDOWN));
Andreas Hubera1f8ab02012-11-30 10:53:22 -08001767 }
1768}
1769
1770void NuPlayer::performReset() {
1771 ALOGV("performReset");
1772
1773 CHECK(mAudioDecoder == NULL);
1774 CHECK(mVideoDecoder == NULL);
1775
1776 cancelPollDuration();
1777
1778 ++mScanSourcesGeneration;
1779 mScanSourcesPending = false;
1780
Lajos Molnar09524832014-07-17 14:29:51 -07001781 if (mRendererLooper != NULL) {
1782 if (mRenderer != NULL) {
1783 mRendererLooper->unregisterHandler(mRenderer->id());
1784 }
1785 mRendererLooper->stop();
1786 mRendererLooper.clear();
1787 }
Andreas Hubera1f8ab02012-11-30 10:53:22 -08001788 mRenderer.clear();
Wei Jia57568df2014-09-22 10:16:29 -07001789 ++mRendererGeneration;
Andreas Hubera1f8ab02012-11-30 10:53:22 -08001790
1791 if (mSource != NULL) {
1792 mSource->stop();
Andreas Huberb5f25f02013-02-05 10:14:26 -08001793
Andreas Hubera1f8ab02012-11-30 10:53:22 -08001794 mSource.clear();
1795 }
1796
1797 if (mDriver != NULL) {
1798 sp<NuPlayerDriver> driver = mDriver.promote();
1799 if (driver != NULL) {
1800 driver->notifyResetComplete();
1801 }
1802 }
Andreas Huber57a339c2012-12-03 11:18:00 -08001803
1804 mStarted = false;
Andreas Hubera1f8ab02012-11-30 10:53:22 -08001805}
1806
1807void NuPlayer::performScanSources() {
1808 ALOGV("performScanSources");
1809
Andreas Huber57a339c2012-12-03 11:18:00 -08001810 if (!mStarted) {
1811 return;
1812 }
1813
Andreas Hubera1f8ab02012-11-30 10:53:22 -08001814 if (mAudioDecoder == NULL || mVideoDecoder == NULL) {
1815 postScanSources();
1816 }
1817}
1818
Andreas Huber57a339c2012-12-03 11:18:00 -08001819void NuPlayer::performSetSurface(const sp<NativeWindowWrapper> &wrapper) {
1820 ALOGV("performSetSurface");
1821
1822 mNativeWindow = wrapper;
1823
1824 // XXX - ignore error from setVideoScalingMode for now
1825 setVideoScalingMode(mVideoScalingMode);
Chong Zhang13d6faa2014-08-22 15:35:28 -07001826
1827 if (mDriver != NULL) {
1828 sp<NuPlayerDriver> driver = mDriver.promote();
1829 if (driver != NULL) {
1830 driver->notifySetSurfaceComplete();
1831 }
1832 }
Andreas Huber57a339c2012-12-03 11:18:00 -08001833}
1834
Andreas Huber9575c962013-02-05 13:59:56 -08001835void NuPlayer::onSourceNotify(const sp<AMessage> &msg) {
1836 int32_t what;
1837 CHECK(msg->findInt32("what", &what));
1838
1839 switch (what) {
1840 case Source::kWhatPrepared:
1841 {
Andreas Huberb5f28d42013-04-25 15:11:19 -07001842 if (mSource == NULL) {
1843 // This is a stale notification from a source that was
1844 // asynchronously preparing when the client called reset().
1845 // We handled the reset, the source is gone.
1846 break;
1847 }
1848
Andreas Huberec0c5972013-02-05 14:47:13 -08001849 int32_t err;
1850 CHECK(msg->findInt32("err", &err));
1851
Andreas Huber9575c962013-02-05 13:59:56 -08001852 sp<NuPlayerDriver> driver = mDriver.promote();
1853 if (driver != NULL) {
Marco Nelissendd114d12014-05-28 15:23:14 -07001854 // notify duration first, so that it's definitely set when
1855 // the app received the "prepare complete" callback.
1856 int64_t durationUs;
1857 if (mSource->getDuration(&durationUs) == OK) {
1858 driver->notifyDuration(durationUs);
1859 }
Andreas Huberec0c5972013-02-05 14:47:13 -08001860 driver->notifyPrepareCompleted(err);
Andreas Huber9575c962013-02-05 13:59:56 -08001861 }
Andreas Huber99759402013-04-01 14:28:31 -07001862
Andreas Huber9575c962013-02-05 13:59:56 -08001863 break;
1864 }
1865
1866 case Source::kWhatFlagsChanged:
1867 {
1868 uint32_t flags;
1869 CHECK(msg->findInt32("flags", (int32_t *)&flags));
1870
Chong Zhang4b7069d2013-09-11 12:52:43 -07001871 sp<NuPlayerDriver> driver = mDriver.promote();
1872 if (driver != NULL) {
1873 driver->notifyFlagsChanged(flags);
1874 }
1875
Andreas Huber9575c962013-02-05 13:59:56 -08001876 if ((mSourceFlags & Source::FLAG_DYNAMIC_DURATION)
1877 && (!(flags & Source::FLAG_DYNAMIC_DURATION))) {
1878 cancelPollDuration();
1879 } else if (!(mSourceFlags & Source::FLAG_DYNAMIC_DURATION)
1880 && (flags & Source::FLAG_DYNAMIC_DURATION)
1881 && (mAudioDecoder != NULL || mVideoDecoder != NULL)) {
1882 schedulePollDuration();
1883 }
1884
1885 mSourceFlags = flags;
1886 break;
1887 }
1888
1889 case Source::kWhatVideoSizeChanged:
1890 {
Chong Zhangced1c2f2014-08-08 15:22:35 -07001891 sp<AMessage> format;
1892 CHECK(msg->findMessage("format", &format));
Andreas Huber9575c962013-02-05 13:59:56 -08001893
Chong Zhangced1c2f2014-08-08 15:22:35 -07001894 updateVideoSize(format);
Andreas Huber9575c962013-02-05 13:59:56 -08001895 break;
1896 }
1897
Chong Zhang2a3cc9a2014-08-21 17:48:26 -07001898 case Source::kWhatBufferingUpdate:
1899 {
1900 int32_t percentage;
1901 CHECK(msg->findInt32("percentage", &percentage));
1902
1903 notifyListener(MEDIA_BUFFERING_UPDATE, percentage, 0);
1904 break;
1905 }
1906
Roger Jönssonb50e83e2013-01-21 16:26:41 +01001907 case Source::kWhatBufferingStart:
1908 {
1909 notifyListener(MEDIA_INFO, MEDIA_INFO_BUFFERING_START, 0);
1910 break;
1911 }
1912
1913 case Source::kWhatBufferingEnd:
1914 {
1915 notifyListener(MEDIA_INFO, MEDIA_INFO_BUFFERING_END, 0);
1916 break;
1917 }
1918
Chong Zhangdcb89b32013-08-06 09:44:47 -07001919 case Source::kWhatSubtitleData:
1920 {
1921 sp<ABuffer> buffer;
1922 CHECK(msg->findBuffer("buffer", &buffer));
1923
Chong Zhang404fced2014-06-11 14:45:31 -07001924 sendSubtitleData(buffer, 0 /* baseIndex */);
Chong Zhangdcb89b32013-08-06 09:44:47 -07001925 break;
1926 }
1927
Robert Shihd3b0bbb2014-07-23 15:00:25 -07001928 case Source::kWhatTimedTextData:
1929 {
1930 int32_t generation;
1931 if (msg->findInt32("generation", &generation)
1932 && generation != mTimedTextGeneration) {
1933 break;
1934 }
1935
1936 sp<ABuffer> buffer;
1937 CHECK(msg->findBuffer("buffer", &buffer));
1938
1939 sp<NuPlayerDriver> driver = mDriver.promote();
1940 if (driver == NULL) {
1941 break;
1942 }
1943
1944 int posMs;
1945 int64_t timeUs, posUs;
1946 driver->getCurrentPosition(&posMs);
1947 posUs = posMs * 1000;
1948 CHECK(buffer->meta()->findInt64("timeUs", &timeUs));
1949
1950 if (posUs < timeUs) {
1951 if (!msg->findInt32("generation", &generation)) {
1952 msg->setInt32("generation", mTimedTextGeneration);
1953 }
1954 msg->post(timeUs - posUs);
1955 } else {
1956 sendTimedTextData(buffer);
1957 }
1958 break;
1959 }
1960
Andreas Huber14f76722013-01-15 09:04:18 -08001961 case Source::kWhatQueueDecoderShutdown:
1962 {
1963 int32_t audio, video;
1964 CHECK(msg->findInt32("audio", &audio));
1965 CHECK(msg->findInt32("video", &video));
1966
1967 sp<AMessage> reply;
1968 CHECK(msg->findMessage("reply", &reply));
1969
1970 queueDecoderShutdown(audio, video, reply);
1971 break;
1972 }
1973
Ronghua Wu80276872014-08-28 15:50:29 -07001974 case Source::kWhatDrmNoLicense:
1975 {
1976 notifyListener(MEDIA_ERROR, MEDIA_ERROR_UNKNOWN, ERROR_DRM_NO_LICENSE);
1977 break;
1978 }
1979
Andreas Huber9575c962013-02-05 13:59:56 -08001980 default:
1981 TRESPASS();
1982 }
1983}
1984
Chong Zhanga7fa1d92014-06-11 14:49:23 -07001985void NuPlayer::onClosedCaptionNotify(const sp<AMessage> &msg) {
1986 int32_t what;
1987 CHECK(msg->findInt32("what", &what));
1988
1989 switch (what) {
1990 case NuPlayer::CCDecoder::kWhatClosedCaptionData:
1991 {
1992 sp<ABuffer> buffer;
1993 CHECK(msg->findBuffer("buffer", &buffer));
1994
1995 size_t inbandTracks = 0;
1996 if (mSource != NULL) {
1997 inbandTracks = mSource->getTrackCount();
1998 }
1999
2000 sendSubtitleData(buffer, inbandTracks);
2001 break;
2002 }
2003
2004 case NuPlayer::CCDecoder::kWhatTrackAdded:
2005 {
2006 notifyListener(MEDIA_INFO, MEDIA_INFO_METADATA_UPDATE, 0);
2007
2008 break;
2009 }
2010
2011 default:
2012 TRESPASS();
2013 }
2014
2015
2016}
2017
Chong Zhang404fced2014-06-11 14:45:31 -07002018void NuPlayer::sendSubtitleData(const sp<ABuffer> &buffer, int32_t baseIndex) {
2019 int32_t trackIndex;
2020 int64_t timeUs, durationUs;
2021 CHECK(buffer->meta()->findInt32("trackIndex", &trackIndex));
2022 CHECK(buffer->meta()->findInt64("timeUs", &timeUs));
2023 CHECK(buffer->meta()->findInt64("durationUs", &durationUs));
2024
2025 Parcel in;
2026 in.writeInt32(trackIndex + baseIndex);
2027 in.writeInt64(timeUs);
2028 in.writeInt64(durationUs);
2029 in.writeInt32(buffer->size());
2030 in.writeInt32(buffer->size());
2031 in.write(buffer->data(), buffer->size());
2032
2033 notifyListener(MEDIA_SUBTITLE_DATA, 0, 0, &in);
2034}
Robert Shihd3b0bbb2014-07-23 15:00:25 -07002035
2036void NuPlayer::sendTimedTextData(const sp<ABuffer> &buffer) {
2037 const void *data;
2038 size_t size = 0;
2039 int64_t timeUs;
2040 int32_t flag = TextDescriptions::LOCAL_DESCRIPTIONS;
2041
2042 AString mime;
2043 CHECK(buffer->meta()->findString("mime", &mime));
2044 CHECK(strcasecmp(mime.c_str(), MEDIA_MIMETYPE_TEXT_3GPP) == 0);
2045
2046 data = buffer->data();
2047 size = buffer->size();
2048
2049 Parcel parcel;
2050 if (size > 0) {
2051 CHECK(buffer->meta()->findInt64("timeUs", &timeUs));
2052 flag |= TextDescriptions::IN_BAND_TEXT_3GPP;
2053 TextDescriptions::getParcelOfDescriptions(
2054 (const uint8_t *)data, size, flag, timeUs / 1000, &parcel);
2055 }
2056
2057 if ((parcel.dataSize() > 0)) {
2058 notifyListener(MEDIA_TIMED_TEXT, 0, 0, &parcel);
2059 } else { // send an empty timed text
2060 notifyListener(MEDIA_TIMED_TEXT, 0, 0);
2061 }
2062}
Andreas Huberb5f25f02013-02-05 10:14:26 -08002063////////////////////////////////////////////////////////////////////////////////
2064
Chong Zhangced1c2f2014-08-08 15:22:35 -07002065sp<AMessage> NuPlayer::Source::getFormat(bool audio) {
2066 sp<MetaData> meta = getFormatMeta(audio);
2067
2068 if (meta == NULL) {
2069 return NULL;
2070 }
2071
2072 sp<AMessage> msg = new AMessage;
2073
2074 if(convertMetaDataToMessage(meta, &msg) == OK) {
2075 return msg;
2076 }
2077 return NULL;
2078}
2079
Andreas Huber9575c962013-02-05 13:59:56 -08002080void NuPlayer::Source::notifyFlagsChanged(uint32_t flags) {
2081 sp<AMessage> notify = dupNotify();
2082 notify->setInt32("what", kWhatFlagsChanged);
2083 notify->setInt32("flags", flags);
2084 notify->post();
2085}
2086
Chong Zhangced1c2f2014-08-08 15:22:35 -07002087void NuPlayer::Source::notifyVideoSizeChanged(const sp<AMessage> &format) {
Andreas Huber9575c962013-02-05 13:59:56 -08002088 sp<AMessage> notify = dupNotify();
2089 notify->setInt32("what", kWhatVideoSizeChanged);
Chong Zhangced1c2f2014-08-08 15:22:35 -07002090 notify->setMessage("format", format);
Andreas Huber9575c962013-02-05 13:59:56 -08002091 notify->post();
2092}
2093
Andreas Huberec0c5972013-02-05 14:47:13 -08002094void NuPlayer::Source::notifyPrepared(status_t err) {
Andreas Huber9575c962013-02-05 13:59:56 -08002095 sp<AMessage> notify = dupNotify();
2096 notify->setInt32("what", kWhatPrepared);
Andreas Huberec0c5972013-02-05 14:47:13 -08002097 notify->setInt32("err", err);
Andreas Huber9575c962013-02-05 13:59:56 -08002098 notify->post();
2099}
2100
Andreas Huber84333e02014-02-07 15:36:10 -08002101void NuPlayer::Source::onMessageReceived(const sp<AMessage> & /* msg */) {
Andreas Huberb5f25f02013-02-05 10:14:26 -08002102 TRESPASS();
2103}
2104
Andreas Huberf9334412010-12-15 15:17:42 -08002105} // namespace android