blob: 405278c781e8951b667cfc2ce4b06c3b2ace3f92 [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"
Chong Zhang7137ec72014-11-12 16:41:05 -080024#include "NuPlayerCCDecoder.h"
Andreas Huberf9334412010-12-15 15:17:42 -080025#include "NuPlayerDecoder.h"
Chong Zhang7137ec72014-11-12 16:41:05 -080026#include "NuPlayerDecoderBase.h"
Wei Jiabc2fb722014-07-08 16:37:57 -070027#include "NuPlayerDecoderPassThrough.h"
Andreas Huber43c3e6c2011-01-05 12:17:08 -080028#include "NuPlayerDriver.h"
Andreas Huberf9334412010-12-15 15:17:42 -080029#include "NuPlayerRenderer.h"
Andreas Huber5bc087c2010-12-23 10:27:40 -080030#include "NuPlayerSource.h"
Andreas Huber2bfdd422011-10-11 15:24:07 -070031#include "RTSPSource.h"
Andreas Huber5bc087c2010-12-23 10:27:40 -080032#include "StreamingSource.h"
Andreas Huberafed0e12011-09-20 15:39:58 -070033#include "GenericSource.h"
Robert Shihd3b0bbb2014-07-23 15:00:25 -070034#include "TextDescriptions.h"
Andreas Huber5bc087c2010-12-23 10:27:40 -080035
36#include "ATSParser.h"
Andreas Huberf9334412010-12-15 15:17:42 -080037
Lajos Molnard9fd6312014-11-06 11:00:00 -080038#include <cutils/properties.h>
39
Andreas Huber3831a062010-12-21 10:22:33 -080040#include <media/stagefright/foundation/hexdump.h>
Andreas Huberf9334412010-12-15 15:17:42 -080041#include <media/stagefright/foundation/ABuffer.h>
42#include <media/stagefright/foundation/ADebug.h>
43#include <media/stagefright/foundation/AMessage.h>
Lajos Molnar09524832014-07-17 14:29:51 -070044#include <media/stagefright/MediaBuffer.h>
Andreas Huber3fe62152011-09-16 15:09:22 -070045#include <media/stagefright/MediaDefs.h>
Andreas Huberf9334412010-12-15 15:17:42 -080046#include <media/stagefright/MediaErrors.h>
47#include <media/stagefright/MetaData.h>
Andy McFadden8ba01022012-12-18 09:46:54 -080048#include <gui/IGraphicBufferProducer.h>
Andreas Huberf9334412010-12-15 15:17:42 -080049
Andreas Huber3fe62152011-09-16 15:09:22 -070050#include "avc_utils.h"
51
Andreas Huber84066782011-08-16 09:34:26 -070052#include "ESDS.h"
53#include <media/stagefright/Utils.h>
54
Andreas Huberf9334412010-12-15 15:17:42 -080055namespace android {
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
Wei Jiafef808d2014-10-31 17:57:05 -070098struct NuPlayer::FlushDecoderAction : public Action {
99 FlushDecoderAction(FlushCommand audio, FlushCommand video)
Andreas Huber14f76722013-01-15 09:04:18 -0800100 : mAudio(audio),
101 mVideo(video) {
102 }
103
104 virtual void execute(NuPlayer *player) {
Wei Jiafef808d2014-10-31 17:57:05 -0700105 player->performDecoderFlush(mAudio, mVideo);
Andreas Huber14f76722013-01-15 09:04:18 -0800106 }
107
108private:
Wei Jiafef808d2014-10-31 17:57:05 -0700109 FlushCommand mAudio;
110 FlushCommand mVideo;
Andreas Huber14f76722013-01-15 09:04:18 -0800111
Wei Jiafef808d2014-10-31 17:57:05 -0700112 DISALLOW_EVIL_CONSTRUCTORS(FlushDecoderAction);
Andreas Huber14f76722013-01-15 09:04:18 -0800113};
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),
Wei Jiabc2fb722014-07-08 16:37:57 -0700154 mOffloadAudio(false),
Wei Jia88703c32014-08-06 11:24:07 -0700155 mAudioDecoderGeneration(0),
156 mVideoDecoderGeneration(0),
Wei Jia57568df2014-09-22 10:16:29 -0700157 mRendererGeneration(0),
Andreas Huber9b80c2b2011-06-30 15:47:02 -0700158 mAudioEOS(false),
Andreas Huberf9334412010-12-15 15:17:42 -0800159 mVideoEOS(false),
Andreas Huber5bc087c2010-12-23 10:27:40 -0800160 mScanSourcesPending(false),
Andreas Huber1aef2112011-01-04 14:01:29 -0800161 mScanSourcesGeneration(0),
Andreas Huberb7c8e912012-11-27 15:02:53 -0800162 mPollDurationGeneration(0),
Robert Shihd3b0bbb2014-07-23 15:00:25 -0700163 mTimedTextGeneration(0),
Andreas Huberf9334412010-12-15 15:17:42 -0800164 mFlushingAudio(NONE),
Andreas Huber1aef2112011-01-04 14:01:29 -0800165 mFlushingVideo(NONE),
Andreas Huber57a339c2012-12-03 11:18:00 -0800166 mVideoScalingMode(NATIVE_WINDOW_SCALING_MODE_SCALE_TO_WINDOW),
167 mStarted(false) {
Andy Hung8d121d42014-10-03 09:53:53 -0700168 clearFlushComplete();
Andreas Huberf9334412010-12-15 15:17:42 -0800169}
170
171NuPlayer::~NuPlayer() {
172}
173
Andreas Huber9b80c2b2011-06-30 15:47:02 -0700174void NuPlayer::setUID(uid_t uid) {
175 mUIDValid = true;
176 mUID = uid;
177}
178
Andreas Huber43c3e6c2011-01-05 12:17:08 -0800179void NuPlayer::setDriver(const wp<NuPlayerDriver> &driver) {
180 mDriver = driver;
Andreas Huberf9334412010-12-15 15:17:42 -0800181}
182
Andreas Huber9575c962013-02-05 13:59:56 -0800183void NuPlayer::setDataSourceAsync(const sp<IStreamSource> &source) {
Andreas Huberf9334412010-12-15 15:17:42 -0800184 sp<AMessage> msg = new AMessage(kWhatSetDataSource, id());
185
Andreas Huberb5f25f02013-02-05 10:14:26 -0800186 sp<AMessage> notify = new AMessage(kWhatSourceNotify, id());
187
Andreas Huber240abcc2014-02-13 13:32:37 -0800188 msg->setObject("source", new StreamingSource(notify, source));
Andreas Huber5bc087c2010-12-23 10:27:40 -0800189 msg->post();
190}
Andreas Huberf9334412010-12-15 15:17:42 -0800191
Andreas Huberafed0e12011-09-20 15:39:58 -0700192static bool IsHTTPLiveURL(const char *url) {
193 if (!strncasecmp("http://", url, 7)
Andreas Huber99759402013-04-01 14:28:31 -0700194 || !strncasecmp("https://", url, 8)
195 || !strncasecmp("file://", url, 7)) {
Andreas Huberafed0e12011-09-20 15:39:58 -0700196 size_t len = strlen(url);
197 if (len >= 5 && !strcasecmp(".m3u8", &url[len - 5])) {
198 return true;
199 }
200
201 if (strstr(url,"m3u8")) {
202 return true;
203 }
204 }
205
206 return false;
207}
208
Andreas Huber9575c962013-02-05 13:59:56 -0800209void NuPlayer::setDataSourceAsync(
Andreas Huber1b86fe02014-01-29 11:13:26 -0800210 const sp<IMediaHTTPService> &httpService,
211 const char *url,
212 const KeyedVector<String8, String8> *headers) {
Chong Zhang3de157d2014-08-05 20:54:44 -0700213
Andreas Huber5bc087c2010-12-23 10:27:40 -0800214 sp<AMessage> msg = new AMessage(kWhatSetDataSource, id());
Oscar Rydhé7a33b772012-02-20 10:15:48 +0100215 size_t len = strlen(url);
Andreas Huber5bc087c2010-12-23 10:27:40 -0800216
Andreas Huberb5f25f02013-02-05 10:14:26 -0800217 sp<AMessage> notify = new AMessage(kWhatSourceNotify, id());
218
Andreas Huberafed0e12011-09-20 15:39:58 -0700219 sp<Source> source;
220 if (IsHTTPLiveURL(url)) {
Andreas Huber81e68442014-02-05 11:52:33 -0800221 source = new HTTPLiveSource(notify, httpService, url, headers);
Andreas Huberafed0e12011-09-20 15:39:58 -0700222 } else if (!strncasecmp(url, "rtsp://", 7)) {
Andreas Huber1b86fe02014-01-29 11:13:26 -0800223 source = new RTSPSource(
224 notify, httpService, url, headers, mUIDValid, mUID);
Oscar Rydhé7a33b772012-02-20 10:15:48 +0100225 } else if ((!strncasecmp(url, "http://", 7)
226 || !strncasecmp(url, "https://", 8))
227 && ((len >= 4 && !strcasecmp(".sdp", &url[len - 4]))
228 || strstr(url, ".sdp?"))) {
Andreas Huber1b86fe02014-01-29 11:13:26 -0800229 source = new RTSPSource(
230 notify, httpService, url, headers, mUIDValid, mUID, true);
Andreas Huber2bfdd422011-10-11 15:24:07 -0700231 } else {
Chong Zhang3de157d2014-08-05 20:54:44 -0700232 sp<GenericSource> genericSource =
233 new GenericSource(notify, mUIDValid, mUID);
234 // Don't set FLAG_SECURE on mSourceFlags here for widevine.
235 // The correct flags will be updated in Source::kWhatFlagsChanged
236 // handler when GenericSource is prepared.
Andreas Huber2bfdd422011-10-11 15:24:07 -0700237
Chong Zhanga19f33e2014-08-07 15:35:07 -0700238 status_t err = genericSource->setDataSource(httpService, url, headers);
Chong Zhang3de157d2014-08-05 20:54:44 -0700239
240 if (err == OK) {
241 source = genericSource;
242 } else {
Chong Zhanga19f33e2014-08-07 15:35:07 -0700243 ALOGE("Failed to set data source!");
Chong Zhang3de157d2014-08-05 20:54:44 -0700244 }
245 }
Andreas Huberafed0e12011-09-20 15:39:58 -0700246 msg->setObject("source", source);
247 msg->post();
248}
249
Andreas Huber9575c962013-02-05 13:59:56 -0800250void NuPlayer::setDataSourceAsync(int fd, int64_t offset, int64_t length) {
Andreas Huberafed0e12011-09-20 15:39:58 -0700251 sp<AMessage> msg = new AMessage(kWhatSetDataSource, id());
252
Andreas Huberb5f25f02013-02-05 10:14:26 -0800253 sp<AMessage> notify = new AMessage(kWhatSourceNotify, id());
254
Chong Zhang3de157d2014-08-05 20:54:44 -0700255 sp<GenericSource> source =
256 new GenericSource(notify, mUIDValid, mUID);
257
Chong Zhanga19f33e2014-08-07 15:35:07 -0700258 status_t err = source->setDataSource(fd, offset, length);
Chong Zhang3de157d2014-08-05 20:54:44 -0700259
260 if (err != OK) {
Chong Zhanga19f33e2014-08-07 15:35:07 -0700261 ALOGE("Failed to set data source!");
Chong Zhang3de157d2014-08-05 20:54:44 -0700262 source = NULL;
263 }
264
Andreas Huberafed0e12011-09-20 15:39:58 -0700265 msg->setObject("source", source);
Andreas Huberf9334412010-12-15 15:17:42 -0800266 msg->post();
267}
268
Andreas Huber9575c962013-02-05 13:59:56 -0800269void NuPlayer::prepareAsync() {
270 (new AMessage(kWhatPrepare, id()))->post();
271}
272
Andreas Huber57a339c2012-12-03 11:18:00 -0800273void NuPlayer::setVideoSurfaceTextureAsync(
Andy McFadden8ba01022012-12-18 09:46:54 -0800274 const sp<IGraphicBufferProducer> &bufferProducer) {
Glenn Kasten11731182011-02-08 17:26:17 -0800275 sp<AMessage> msg = new AMessage(kWhatSetVideoNativeWindow, id());
Andreas Huber57a339c2012-12-03 11:18:00 -0800276
Andy McFadden8ba01022012-12-18 09:46:54 -0800277 if (bufferProducer == NULL) {
Andreas Huber57a339c2012-12-03 11:18:00 -0800278 msg->setObject("native-window", NULL);
279 } else {
280 msg->setObject(
281 "native-window",
282 new NativeWindowWrapper(
Wei Jia9c03a402014-08-26 15:24:43 -0700283 new Surface(bufferProducer, true /* controlledByApp */)));
Andreas Huber57a339c2012-12-03 11:18:00 -0800284 }
285
Andreas Huberf9334412010-12-15 15:17:42 -0800286 msg->post();
287}
288
289void NuPlayer::setAudioSink(const sp<MediaPlayerBase::AudioSink> &sink) {
290 sp<AMessage> msg = new AMessage(kWhatSetAudioSink, id());
291 msg->setObject("sink", sink);
292 msg->post();
293}
294
295void NuPlayer::start() {
296 (new AMessage(kWhatStart, id()))->post();
297}
298
Andreas Huber43c3e6c2011-01-05 12:17:08 -0800299void NuPlayer::pause() {
Andreas Huberb4082222011-01-20 15:23:04 -0800300 (new AMessage(kWhatPause, id()))->post();
Andreas Huber43c3e6c2011-01-05 12:17:08 -0800301}
302
Andreas Huber1aef2112011-01-04 14:01:29 -0800303void NuPlayer::resetAsync() {
Chong Zhang48296b72014-09-14 14:28:45 -0700304 if (mSource != NULL) {
305 // During a reset, the data source might be unresponsive already, we need to
306 // disconnect explicitly so that reads exit promptly.
307 // We can't queue the disconnect request to the looper, as it might be
308 // queued behind a stuck read and never gets processed.
309 // Doing a disconnect outside the looper to allows the pending reads to exit
310 // (either successfully or with error).
311 mSource->disconnect();
312 }
313
Andreas Huber1aef2112011-01-04 14:01:29 -0800314 (new AMessage(kWhatReset, id()))->post();
315}
316
Wei Jiae427abf2014-09-22 15:21:11 -0700317void NuPlayer::seekToAsync(int64_t seekTimeUs, bool needNotify) {
Andreas Huber43c3e6c2011-01-05 12:17:08 -0800318 sp<AMessage> msg = new AMessage(kWhatSeek, id());
319 msg->setInt64("seekTimeUs", seekTimeUs);
Wei Jiae427abf2014-09-22 15:21:11 -0700320 msg->setInt32("needNotify", needNotify);
Andreas Huber43c3e6c2011-01-05 12:17:08 -0800321 msg->post();
322}
323
Andreas Huber53df1a42010-12-22 10:03:04 -0800324
Chong Zhang404fced2014-06-11 14:45:31 -0700325void NuPlayer::writeTrackInfo(
326 Parcel* reply, const sp<AMessage> format) const {
327 int32_t trackType;
328 CHECK(format->findInt32("type", &trackType));
329
330 AString lang;
331 CHECK(format->findString("language", &lang));
332
333 reply->writeInt32(2); // write something non-zero
334 reply->writeInt32(trackType);
335 reply->writeString16(String16(lang.c_str()));
336
337 if (trackType == MEDIA_TRACK_TYPE_SUBTITLE) {
338 AString mime;
339 CHECK(format->findString("mime", &mime));
340
341 int32_t isAuto, isDefault, isForced;
342 CHECK(format->findInt32("auto", &isAuto));
343 CHECK(format->findInt32("default", &isDefault));
344 CHECK(format->findInt32("forced", &isForced));
345
346 reply->writeString16(String16(mime.c_str()));
347 reply->writeInt32(isAuto);
348 reply->writeInt32(isDefault);
349 reply->writeInt32(isForced);
350 }
351}
352
Andreas Huberf9334412010-12-15 15:17:42 -0800353void NuPlayer::onMessageReceived(const sp<AMessage> &msg) {
354 switch (msg->what()) {
355 case kWhatSetDataSource:
356 {
Steve Block3856b092011-10-20 11:56:00 +0100357 ALOGV("kWhatSetDataSource");
Andreas Huberf9334412010-12-15 15:17:42 -0800358
359 CHECK(mSource == NULL);
360
Chong Zhang3de157d2014-08-05 20:54:44 -0700361 status_t err = OK;
Andreas Huber5bc087c2010-12-23 10:27:40 -0800362 sp<RefBase> obj;
363 CHECK(msg->findObject("source", &obj));
Chong Zhang3de157d2014-08-05 20:54:44 -0700364 if (obj != NULL) {
365 mSource = static_cast<Source *>(obj.get());
Chong Zhang3de157d2014-08-05 20:54:44 -0700366 } else {
367 err = UNKNOWN_ERROR;
368 }
Andreas Huber9575c962013-02-05 13:59:56 -0800369
370 CHECK(mDriver != NULL);
371 sp<NuPlayerDriver> driver = mDriver.promote();
372 if (driver != NULL) {
Chong Zhang3de157d2014-08-05 20:54:44 -0700373 driver->notifySetDataSourceCompleted(err);
Andreas Huber9575c962013-02-05 13:59:56 -0800374 }
375 break;
376 }
377
378 case kWhatPrepare:
379 {
380 mSource->prepareAsync();
Andreas Huberf9334412010-12-15 15:17:42 -0800381 break;
382 }
383
Chong Zhangdcb89b32013-08-06 09:44:47 -0700384 case kWhatGetTrackInfo:
385 {
386 uint32_t replyID;
387 CHECK(msg->senderAwaitsResponse(&replyID));
388
Chong Zhang404fced2014-06-11 14:45:31 -0700389 Parcel* reply;
390 CHECK(msg->findPointer("reply", (void**)&reply));
391
392 size_t inbandTracks = 0;
Chong Zhangdcb89b32013-08-06 09:44:47 -0700393 if (mSource != NULL) {
Chong Zhang404fced2014-06-11 14:45:31 -0700394 inbandTracks = mSource->getTrackCount();
395 }
396
Chong Zhanga7fa1d92014-06-11 14:49:23 -0700397 size_t ccTracks = 0;
398 if (mCCDecoder != NULL) {
399 ccTracks = mCCDecoder->getTrackCount();
400 }
401
Chong Zhang404fced2014-06-11 14:45:31 -0700402 // total track count
Chong Zhanga7fa1d92014-06-11 14:49:23 -0700403 reply->writeInt32(inbandTracks + ccTracks);
Chong Zhang404fced2014-06-11 14:45:31 -0700404
405 // write inband tracks
406 for (size_t i = 0; i < inbandTracks; ++i) {
407 writeTrackInfo(reply, mSource->getTrackInfo(i));
Chong Zhangdcb89b32013-08-06 09:44:47 -0700408 }
409
Chong Zhanga7fa1d92014-06-11 14:49:23 -0700410 // write CC track
411 for (size_t i = 0; i < ccTracks; ++i) {
412 writeTrackInfo(reply, mCCDecoder->getTrackInfo(i));
413 }
414
Chong Zhangdcb89b32013-08-06 09:44:47 -0700415 sp<AMessage> response = new AMessage;
Chong Zhangdcb89b32013-08-06 09:44:47 -0700416 response->postReply(replyID);
417 break;
418 }
419
Robert Shih7c4f0d72014-07-09 18:53:31 -0700420 case kWhatGetSelectedTrack:
421 {
422 status_t err = INVALID_OPERATION;
423 if (mSource != NULL) {
424 err = OK;
425
426 int32_t type32;
427 CHECK(msg->findInt32("type", (int32_t*)&type32));
428 media_track_type type = (media_track_type)type32;
429 ssize_t selectedTrack = mSource->getSelectedTrack(type);
430
431 Parcel* reply;
432 CHECK(msg->findPointer("reply", (void**)&reply));
433 reply->writeInt32(selectedTrack);
434 }
435
436 sp<AMessage> response = new AMessage;
437 response->setInt32("err", err);
438
439 uint32_t replyID;
440 CHECK(msg->senderAwaitsResponse(&replyID));
441 response->postReply(replyID);
442 break;
443 }
444
Chong Zhangdcb89b32013-08-06 09:44:47 -0700445 case kWhatSelectTrack:
446 {
447 uint32_t replyID;
448 CHECK(msg->senderAwaitsResponse(&replyID));
449
Chong Zhang404fced2014-06-11 14:45:31 -0700450 size_t trackIndex;
451 int32_t select;
Robert Shih6ffb1fd2014-10-29 16:24:32 -0700452 int64_t timeUs;
Chong Zhang404fced2014-06-11 14:45:31 -0700453 CHECK(msg->findSize("trackIndex", &trackIndex));
454 CHECK(msg->findInt32("select", &select));
Robert Shih6ffb1fd2014-10-29 16:24:32 -0700455 CHECK(msg->findInt64("timeUs", &timeUs));
Chong Zhang404fced2014-06-11 14:45:31 -0700456
Chong Zhangdcb89b32013-08-06 09:44:47 -0700457 status_t err = INVALID_OPERATION;
Chong Zhang404fced2014-06-11 14:45:31 -0700458
459 size_t inbandTracks = 0;
Chong Zhangdcb89b32013-08-06 09:44:47 -0700460 if (mSource != NULL) {
Chong Zhang404fced2014-06-11 14:45:31 -0700461 inbandTracks = mSource->getTrackCount();
462 }
Chong Zhanga7fa1d92014-06-11 14:49:23 -0700463 size_t ccTracks = 0;
464 if (mCCDecoder != NULL) {
465 ccTracks = mCCDecoder->getTrackCount();
466 }
Chong Zhang404fced2014-06-11 14:45:31 -0700467
468 if (trackIndex < inbandTracks) {
Robert Shih6ffb1fd2014-10-29 16:24:32 -0700469 err = mSource->selectTrack(trackIndex, select, timeUs);
Robert Shihd3b0bbb2014-07-23 15:00:25 -0700470
471 if (!select && err == OK) {
472 int32_t type;
473 sp<AMessage> info = mSource->getTrackInfo(trackIndex);
474 if (info != NULL
475 && info->findInt32("type", &type)
476 && type == MEDIA_TRACK_TYPE_TIMEDTEXT) {
477 ++mTimedTextGeneration;
478 }
479 }
Chong Zhanga7fa1d92014-06-11 14:49:23 -0700480 } else {
481 trackIndex -= inbandTracks;
482
483 if (trackIndex < ccTracks) {
484 err = mCCDecoder->selectTrack(trackIndex, select);
485 }
Chong Zhangdcb89b32013-08-06 09:44:47 -0700486 }
487
488 sp<AMessage> response = new AMessage;
489 response->setInt32("err", err);
490
491 response->postReply(replyID);
492 break;
493 }
494
Andreas Huberb7c8e912012-11-27 15:02:53 -0800495 case kWhatPollDuration:
496 {
497 int32_t generation;
498 CHECK(msg->findInt32("generation", &generation));
499
500 if (generation != mPollDurationGeneration) {
501 // stale
502 break;
503 }
504
505 int64_t durationUs;
506 if (mDriver != NULL && mSource->getDuration(&durationUs) == OK) {
507 sp<NuPlayerDriver> driver = mDriver.promote();
508 if (driver != NULL) {
509 driver->notifyDuration(durationUs);
510 }
511 }
512
513 msg->post(1000000ll); // poll again in a second.
514 break;
515 }
516
Glenn Kasten11731182011-02-08 17:26:17 -0800517 case kWhatSetVideoNativeWindow:
Andreas Huberf9334412010-12-15 15:17:42 -0800518 {
Steve Block3856b092011-10-20 11:56:00 +0100519 ALOGV("kWhatSetVideoNativeWindow");
Andreas Huberf9334412010-12-15 15:17:42 -0800520
521 sp<RefBase> obj;
Glenn Kasten11731182011-02-08 17:26:17 -0800522 CHECK(msg->findObject("native-window", &obj));
Andreas Huberf9334412010-12-15 15:17:42 -0800523
Wei Jiafef808d2014-10-31 17:57:05 -0700524 if (mSource->getFormat(false /* audio */) == NULL) {
525 performSetSurface(static_cast<NativeWindowWrapper *>(obj.get()));
526 break;
527 }
528
529 mDeferredActions.push_back(
530 new FlushDecoderAction(FLUSH_CMD_FLUSH /* audio */,
531 FLUSH_CMD_SHUTDOWN /* video */));
532
Andreas Huber57a339c2012-12-03 11:18:00 -0800533 mDeferredActions.push_back(
534 new SetSurfaceAction(
535 static_cast<NativeWindowWrapper *>(obj.get())));
James Dong0d268a32012-08-31 12:18:27 -0700536
Andreas Huber57a339c2012-12-03 11:18:00 -0800537 if (obj != NULL) {
Wei Jiafef808d2014-10-31 17:57:05 -0700538 if (mStarted) {
Andy Hung73535852014-09-05 11:42:58 -0700539 // Issue a seek to refresh the video screen only if started otherwise
540 // the extractor may not yet be started and will assert.
541 // If the video decoder is not set (perhaps audio only in this case)
542 // do not perform a seek as it is not needed.
Ronghua Wua73d9e02014-10-08 15:13:29 -0700543 int64_t currentPositionUs = 0;
544 if (getCurrentPosition(&currentPositionUs) == OK) {
545 mDeferredActions.push_back(
546 new SeekAction(currentPositionUs, false /* needNotify */));
547 }
Andy Hung73535852014-09-05 11:42:58 -0700548 }
Wei Jiaac428aa2014-09-02 19:01:34 -0700549
Andreas Huber57a339c2012-12-03 11:18:00 -0800550 // If there is a new surface texture, instantiate decoders
551 // again if possible.
552 mDeferredActions.push_back(
553 new SimpleAction(&NuPlayer::performScanSources));
554 }
555
Chong Zhang7137ec72014-11-12 16:41:05 -0800556 // After a flush wihtout shutdown, decoder is paused.
557 // Don't resume it until source is seeked, otherwise it could
558 // start pulling stale data too soon.
559 mDeferredActions.push_back(
560 new SimpleAction(&NuPlayer::performResumeDecoders));
561
Andreas Huber57a339c2012-12-03 11:18:00 -0800562 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
Chong Zhang7137ec72014-11-12 16:41:05 -0800688 if (what == DecoderBase::kWhatInputDiscontinuity) {
689 int32_t formatChange;
690 CHECK(msg->findInt32("formatChange", &formatChange));
Andreas Huberf9334412010-12-15 15:17:42 -0800691
Chong Zhang7137ec72014-11-12 16:41:05 -0800692 ALOGV("%s discontinuity: formatChange %d",
693 audio ? "audio" : "video", formatChange);
694
695 if (formatChange) {
696 mDeferredActions.push_back(
697 new FlushDecoderAction(
698 audio ? FLUSH_CMD_SHUTDOWN : FLUSH_CMD_NONE,
699 audio ? FLUSH_CMD_NONE : FLUSH_CMD_SHUTDOWN));
Andreas Huberf9334412010-12-15 15:17:42 -0800700 }
Chong Zhang7137ec72014-11-12 16:41:05 -0800701
702 mDeferredActions.push_back(
703 new SimpleAction(
704 &NuPlayer::performScanSources));
705
706 processDeferredActions();
707 } else if (what == DecoderBase::kWhatEOS) {
Andreas Huberdc9bacd2011-09-26 10:53:29 -0700708 int32_t err;
Lajos Molnar1cd13982014-01-17 15:12:51 -0800709 CHECK(msg->findInt32("err", &err));
Andreas Huberdc9bacd2011-09-26 10:53:29 -0700710
711 if (err == ERROR_END_OF_STREAM) {
Steve Block3856b092011-10-20 11:56:00 +0100712 ALOGV("got %s decoder EOS", audio ? "audio" : "video");
Andreas Huberdc9bacd2011-09-26 10:53:29 -0700713 } else {
Steve Block3856b092011-10-20 11:56:00 +0100714 ALOGV("got %s decoder EOS w/ error %d",
Andreas Huberdc9bacd2011-09-26 10:53:29 -0700715 audio ? "audio" : "video",
716 err);
717 }
718
719 mRenderer->queueEOS(audio, err);
Chong Zhang7137ec72014-11-12 16:41:05 -0800720 } else if (what == DecoderBase::kWhatFlushCompleted) {
Steve Block3856b092011-10-20 11:56:00 +0100721 ALOGV("decoder %s flush completed", audio ? "audio" : "video");
Andreas Huberf9334412010-12-15 15:17:42 -0800722
Andy Hung8d121d42014-10-03 09:53:53 -0700723 handleFlushComplete(audio, true /* isDecoder */);
Andreas Huber3831a062010-12-21 10:22:33 -0800724 finishFlushIfPossible();
Chong Zhang7137ec72014-11-12 16:41:05 -0800725 } else if (what == DecoderBase::kWhatVideoSizeChanged) {
Lajos Molnar1cd13982014-01-17 15:12:51 -0800726 sp<AMessage> format;
727 CHECK(msg->findMessage("format", &format));
728
Wei Jiac6cfd702014-11-11 16:33:20 -0800729 sp<AMessage> inputFormat =
730 mSource->getFormat(false /* audio */);
Andreas Huber3831a062010-12-21 10:22:33 -0800731
Wei Jiac6cfd702014-11-11 16:33:20 -0800732 updateVideoSize(inputFormat, format);
Chong Zhang7137ec72014-11-12 16:41:05 -0800733 } else if (what == DecoderBase::kWhatShutdownCompleted) {
Steve Block3856b092011-10-20 11:56:00 +0100734 ALOGV("%s shutdown completed", audio ? "audio" : "video");
Andreas Huber3831a062010-12-21 10:22:33 -0800735 if (audio) {
736 mAudioDecoder.clear();
Wei Jia57568df2014-09-22 10:16:29 -0700737 ++mAudioDecoderGeneration;
Andreas Huber3831a062010-12-21 10:22:33 -0800738
739 CHECK_EQ((int)mFlushingAudio, (int)SHUTTING_DOWN_DECODER);
740 mFlushingAudio = SHUT_DOWN;
741 } else {
742 mVideoDecoder.clear();
Wei Jia57568df2014-09-22 10:16:29 -0700743 ++mVideoDecoderGeneration;
Andreas Huber3831a062010-12-21 10:22:33 -0800744
745 CHECK_EQ((int)mFlushingVideo, (int)SHUTTING_DOWN_DECODER);
746 mFlushingVideo = SHUT_DOWN;
747 }
748
749 finishFlushIfPossible();
Chong Zhang7137ec72014-11-12 16:41:05 -0800750 } else if (what == DecoderBase::kWhatError) {
Chong Zhangf4c0a942014-08-11 15:14:10 -0700751 status_t err;
Andy Hung2abde2c2014-09-30 14:40:32 -0700752 if (!msg->findInt32("err", &err) || err == OK) {
Chong Zhangf4c0a942014-08-11 15:14:10 -0700753 err = UNKNOWN_ERROR;
754 }
Andy Hungcf31f1e2014-09-23 14:59:01 -0700755
Andy Hung2abde2c2014-09-30 14:40:32 -0700756 // Decoder errors can be due to Source (e.g. from streaming),
757 // or from decoding corrupted bitstreams, or from other decoder
758 // MediaCodec operations (e.g. from an ongoing reset or seek).
759 //
760 // We try to gracefully shut down the affected decoder if possible,
761 // rather than trying to force the shutdown with something
762 // similar to performReset(). This method can lead to a hang
763 // if MediaCodec functions block after an error, but they should
764 // typically return INVALID_OPERATION instead of blocking.
765
766 FlushStatus *flushing = audio ? &mFlushingAudio : &mFlushingVideo;
767 ALOGE("received error(%#x) from %s decoder, flushing(%d), now shutting down",
768 err, audio ? "audio" : "video", *flushing);
769
770 switch (*flushing) {
771 case NONE:
772 mDeferredActions.push_back(
Wei Jiafef808d2014-10-31 17:57:05 -0700773 new FlushDecoderAction(
774 audio ? FLUSH_CMD_SHUTDOWN : FLUSH_CMD_NONE,
775 audio ? FLUSH_CMD_NONE : FLUSH_CMD_SHUTDOWN));
Andy Hung2abde2c2014-09-30 14:40:32 -0700776 processDeferredActions();
777 break;
778 case FLUSHING_DECODER:
779 *flushing = FLUSHING_DECODER_SHUTDOWN; // initiate shutdown after flush.
780 break; // Wait for flush to complete.
781 case FLUSHING_DECODER_SHUTDOWN:
782 break; // Wait for flush to complete.
783 case SHUTTING_DOWN_DECODER:
784 break; // Wait for shutdown to complete.
785 case FLUSHED:
786 // Widevine source reads must stop before releasing the video decoder.
787 if (!audio && mSource != NULL && mSourceFlags & Source::FLAG_SECURE) {
788 mSource->stop();
789 }
790 getDecoder(audio)->initiateShutdown(); // In the middle of a seek.
791 *flushing = SHUTTING_DOWN_DECODER; // Shut down.
792 break;
793 case SHUT_DOWN:
794 finishFlushIfPossible(); // Should not occur.
795 break; // Finish anyways.
Marco Nelissen9e2b7912014-08-18 16:13:03 -0700796 }
Andy Hung2abde2c2014-09-30 14:40:32 -0700797 notifyListener(MEDIA_ERROR, MEDIA_ERROR_UNKNOWN, err);
Lajos Molnar1cd13982014-01-17 15:12:51 -0800798 } else {
799 ALOGV("Unhandled decoder notification %d '%c%c%c%c'.",
Andreas Hubera1f8ab02012-11-30 10:53:22 -0800800 what,
801 what >> 24,
802 (what >> 16) & 0xff,
803 (what >> 8) & 0xff,
804 what & 0xff);
Andreas Huberf9334412010-12-15 15:17:42 -0800805 }
806
807 break;
808 }
809
810 case kWhatRendererNotify:
811 {
Wei Jia57568df2014-09-22 10:16:29 -0700812 int32_t requesterGeneration = mRendererGeneration - 1;
813 CHECK(msg->findInt32("generation", &requesterGeneration));
814 if (requesterGeneration != mRendererGeneration) {
815 ALOGV("got message from old renderer, generation(%d:%d)",
816 requesterGeneration, mRendererGeneration);
817 return;
818 }
819
Andreas Huberf9334412010-12-15 15:17:42 -0800820 int32_t what;
821 CHECK(msg->findInt32("what", &what));
822
823 if (what == Renderer::kWhatEOS) {
824 int32_t audio;
825 CHECK(msg->findInt32("audio", &audio));
826
Andreas Huberc92fd242011-08-16 13:48:44 -0700827 int32_t finalResult;
828 CHECK(msg->findInt32("finalResult", &finalResult));
829
Andreas Huberf9334412010-12-15 15:17:42 -0800830 if (audio) {
831 mAudioEOS = true;
832 } else {
833 mVideoEOS = true;
834 }
835
Andreas Huberc92fd242011-08-16 13:48:44 -0700836 if (finalResult == ERROR_END_OF_STREAM) {
Steve Block3856b092011-10-20 11:56:00 +0100837 ALOGV("reached %s EOS", audio ? "audio" : "video");
Andreas Huberc92fd242011-08-16 13:48:44 -0700838 } else {
Steve Block29357bc2012-01-06 19:20:56 +0000839 ALOGE("%s track encountered an error (%d)",
Andreas Huberc92fd242011-08-16 13:48:44 -0700840 audio ? "audio" : "video", finalResult);
841
842 notifyListener(
843 MEDIA_ERROR, MEDIA_ERROR_UNKNOWN, finalResult);
844 }
Andreas Huberf9334412010-12-15 15:17:42 -0800845
846 if ((mAudioEOS || mAudioDecoder == NULL)
847 && (mVideoEOS || mVideoDecoder == NULL)) {
848 notifyListener(MEDIA_PLAYBACK_COMPLETE, 0, 0);
849 }
Andreas Huber3fe62152011-09-16 15:09:22 -0700850 } else if (what == Renderer::kWhatFlushComplete) {
Andreas Huberf9334412010-12-15 15:17:42 -0800851 int32_t audio;
852 CHECK(msg->findInt32("audio", &audio));
853
Steve Block3856b092011-10-20 11:56:00 +0100854 ALOGV("renderer %s flush completed.", audio ? "audio" : "video");
Andy Hung8d121d42014-10-03 09:53:53 -0700855 handleFlushComplete(audio, false /* isDecoder */);
856 finishFlushIfPossible();
James Dongf57b4ea2012-07-20 13:38:36 -0700857 } else if (what == Renderer::kWhatVideoRenderingStart) {
858 notifyListener(MEDIA_INFO, MEDIA_INFO_RENDERING_START, 0);
Lajos Molnarcbaffcf2013-08-14 18:30:38 -0700859 } else if (what == Renderer::kWhatMediaRenderingStart) {
860 ALOGV("media rendering started");
861 notifyListener(MEDIA_STARTED, 0, 0);
Wei Jia3a2956d2014-07-22 16:01:33 -0700862 } else if (what == Renderer::kWhatAudioOffloadTearDown) {
Ronghua Wua10fd232014-11-06 16:15:20 -0800863 ALOGV("Tear down audio offload, fall back to s/w path if due to error.");
Wei Jia3a2956d2014-07-22 16:01:33 -0700864 int64_t positionUs;
865 CHECK(msg->findInt64("positionUs", &positionUs));
Ronghua Wu08529172014-10-02 16:55:52 -0700866 int32_t reason;
867 CHECK(msg->findInt32("reason", &reason));
Andy Hung282a7e32014-08-14 15:56:34 -0700868 closeAudioSink();
Wei Jia3a2956d2014-07-22 16:01:33 -0700869 mAudioDecoder.clear();
Wei Jia57568df2014-09-22 10:16:29 -0700870 ++mAudioDecoderGeneration;
Chong Zhang7137ec72014-11-12 16:41:05 -0800871 mRenderer->flush(
872 true /* audio */, false /* notifyComplete */);
Wei Jia3a2956d2014-07-22 16:01:33 -0700873 if (mVideoDecoder != NULL) {
Chong Zhang7137ec72014-11-12 16:41:05 -0800874 mRenderer->flush(
875 false /* audio */, false /* notifyComplete */);
Wei Jia3a2956d2014-07-22 16:01:33 -0700876 }
Wei Jia3a2956d2014-07-22 16:01:33 -0700877
Wei Jiae427abf2014-09-22 15:21:11 -0700878 performSeek(positionUs, false /* needNotify */);
Ronghua Wu08529172014-10-02 16:55:52 -0700879 if (reason == Renderer::kDueToError) {
Ronghua Wua10fd232014-11-06 16:15:20 -0800880 mRenderer->signalDisableOffloadAudio();
881 mOffloadAudio = false;
Ronghua Wu08529172014-10-02 16:55:52 -0700882 instantiateDecoder(true /* audio */, &mAudioDecoder);
883 }
Andreas Huberf9334412010-12-15 15:17:42 -0800884 }
885 break;
886 }
887
888 case kWhatMoreDataQueued:
889 {
890 break;
891 }
892
Andreas Huber1aef2112011-01-04 14:01:29 -0800893 case kWhatReset:
894 {
Steve Block3856b092011-10-20 11:56:00 +0100895 ALOGV("kWhatReset");
Andreas Huber1aef2112011-01-04 14:01:29 -0800896
Andreas Hubera1f8ab02012-11-30 10:53:22 -0800897 mDeferredActions.push_back(
Wei Jiafef808d2014-10-31 17:57:05 -0700898 new FlushDecoderAction(
899 FLUSH_CMD_SHUTDOWN /* audio */,
900 FLUSH_CMD_SHUTDOWN /* video */));
Andreas Huberb7c8e912012-11-27 15:02:53 -0800901
Andreas Hubera1f8ab02012-11-30 10:53:22 -0800902 mDeferredActions.push_back(
903 new SimpleAction(&NuPlayer::performReset));
Andreas Huberb58ce9f2011-11-28 16:27:35 -0800904
Andreas Hubera1f8ab02012-11-30 10:53:22 -0800905 processDeferredActions();
Andreas Huber1aef2112011-01-04 14:01:29 -0800906 break;
907 }
908
Andreas Huber43c3e6c2011-01-05 12:17:08 -0800909 case kWhatSeek:
910 {
911 int64_t seekTimeUs;
Wei Jiae427abf2014-09-22 15:21:11 -0700912 int32_t needNotify;
Andreas Huber43c3e6c2011-01-05 12:17:08 -0800913 CHECK(msg->findInt64("seekTimeUs", &seekTimeUs));
Wei Jiae427abf2014-09-22 15:21:11 -0700914 CHECK(msg->findInt32("needNotify", &needNotify));
Andreas Huber43c3e6c2011-01-05 12:17:08 -0800915
Wei Jiae427abf2014-09-22 15:21:11 -0700916 ALOGV("kWhatSeek seekTimeUs=%lld us, needNotify=%d",
917 seekTimeUs, needNotify);
Andreas Huber43c3e6c2011-01-05 12:17:08 -0800918
Andreas Hubera1f8ab02012-11-30 10:53:22 -0800919 mDeferredActions.push_back(
Wei Jiafef808d2014-10-31 17:57:05 -0700920 new FlushDecoderAction(FLUSH_CMD_FLUSH /* audio */,
921 FLUSH_CMD_FLUSH /* video */));
Andreas Huber43c3e6c2011-01-05 12:17:08 -0800922
Wei Jiae427abf2014-09-22 15:21:11 -0700923 mDeferredActions.push_back(
924 new SeekAction(seekTimeUs, needNotify));
Andreas Huber43c3e6c2011-01-05 12:17:08 -0800925
Chong Zhang7137ec72014-11-12 16:41:05 -0800926 // After a flush wihtout shutdown, decoder is paused.
927 // Don't resume it until source is seeked, otherwise it could
928 // start pulling stale data too soon.
929 mDeferredActions.push_back(
930 new SimpleAction(&NuPlayer::performResumeDecoders));
931
Andreas Hubera1f8ab02012-11-30 10:53:22 -0800932 processDeferredActions();
Andreas Huber43c3e6c2011-01-05 12:17:08 -0800933 break;
934 }
935
Andreas Huberb4082222011-01-20 15:23:04 -0800936 case kWhatPause:
937 {
Wei Jia7e9f7f72014-09-23 10:55:35 -0700938 if (mSource != NULL) {
939 mSource->pause();
940 } else {
941 ALOGW("pause called when source is gone or not set");
942 }
943 if (mRenderer != NULL) {
944 mRenderer->pause();
945 } else {
946 ALOGW("pause called when renderer is gone or not set");
947 }
Andreas Huberb4082222011-01-20 15:23:04 -0800948 break;
949 }
950
Andreas Huberb5f25f02013-02-05 10:14:26 -0800951 case kWhatSourceNotify:
952 {
Andreas Huber9575c962013-02-05 13:59:56 -0800953 onSourceNotify(msg);
Andreas Huberb5f25f02013-02-05 10:14:26 -0800954 break;
955 }
956
Chong Zhanga7fa1d92014-06-11 14:49:23 -0700957 case kWhatClosedCaptionNotify:
958 {
959 onClosedCaptionNotify(msg);
960 break;
961 }
962
Andreas Huberf9334412010-12-15 15:17:42 -0800963 default:
964 TRESPASS();
965 break;
966 }
967}
968
Wei Jia94211742014-10-28 17:09:06 -0700969void NuPlayer::onResume() {
970 if (mSource != NULL) {
971 mSource->resume();
972 } else {
973 ALOGW("resume called when source is gone or not set");
974 }
975 // |mAudioDecoder| may have been released due to the pause timeout, so re-create it if
976 // needed.
977 if (audioDecoderStillNeeded() && mAudioDecoder == NULL) {
978 instantiateDecoder(true /* audio */, &mAudioDecoder);
979 }
980 if (mRenderer != NULL) {
981 mRenderer->resume();
982 } else {
983 ALOGW("resume called when renderer is gone or not set");
984 }
985}
986
987void NuPlayer::onStart() {
Wei Jia94211742014-10-28 17:09:06 -0700988 mOffloadAudio = false;
989 mAudioEOS = false;
990 mVideoEOS = false;
Wei Jia94211742014-10-28 17:09:06 -0700991 mStarted = true;
992
993 /* instantiate decoders now for secure playback */
994 if (mSourceFlags & Source::FLAG_SECURE) {
995 if (mNativeWindow != NULL) {
996 instantiateDecoder(false, &mVideoDecoder);
997 }
998
999 if (mAudioSink != NULL) {
1000 instantiateDecoder(true, &mAudioDecoder);
1001 }
1002 }
1003
1004 mSource->start();
1005
1006 uint32_t flags = 0;
1007
1008 if (mSource->isRealTime()) {
1009 flags |= Renderer::FLAG_REAL_TIME;
1010 }
1011
1012 sp<MetaData> audioMeta = mSource->getFormatMeta(true /* audio */);
1013 audio_stream_type_t streamType = AUDIO_STREAM_MUSIC;
1014 if (mAudioSink != NULL) {
1015 streamType = mAudioSink->getAudioStreamType();
1016 }
1017
1018 sp<AMessage> videoFormat = mSource->getFormat(false /* audio */);
1019
1020 mOffloadAudio =
1021 canOffloadStream(audioMeta, (videoFormat != NULL),
1022 true /* is_streaming */, streamType);
1023 if (mOffloadAudio) {
1024 flags |= Renderer::FLAG_OFFLOAD_AUDIO;
1025 }
1026
1027 sp<AMessage> notify = new AMessage(kWhatRendererNotify, id());
1028 ++mRendererGeneration;
1029 notify->setInt32("generation", mRendererGeneration);
1030 mRenderer = new Renderer(mAudioSink, notify, flags);
1031
1032 mRendererLooper = new ALooper;
1033 mRendererLooper->setName("NuPlayerRenderer");
1034 mRendererLooper->start(false, false, ANDROID_PRIORITY_AUDIO);
1035 mRendererLooper->registerHandler(mRenderer);
1036
1037 sp<MetaData> meta = getFileMeta();
1038 int32_t rate;
1039 if (meta != NULL
1040 && meta->findInt32(kKeyFrameRate, &rate) && rate > 0) {
1041 mRenderer->setVideoFrameRate(rate);
1042 }
1043
Wei Jiac6cfd702014-11-11 16:33:20 -08001044 if (mVideoDecoder != NULL) {
1045 mVideoDecoder->setRenderer(mRenderer);
1046 }
1047 if (mAudioDecoder != NULL) {
1048 mAudioDecoder->setRenderer(mRenderer);
1049 }
1050
Wei Jia94211742014-10-28 17:09:06 -07001051 postScanSources();
1052}
1053
Ronghua Wud7988b12014-10-03 15:19:10 -07001054bool NuPlayer::audioDecoderStillNeeded() {
1055 // Audio decoder is no longer needed if it's in shut/shutting down status.
1056 return ((mFlushingAudio != SHUT_DOWN) && (mFlushingAudio != SHUTTING_DOWN_DECODER));
1057}
1058
Andy Hung8d121d42014-10-03 09:53:53 -07001059void NuPlayer::handleFlushComplete(bool audio, bool isDecoder) {
1060 // We wait for both the decoder flush and the renderer flush to complete
1061 // before entering either the FLUSHED or the SHUTTING_DOWN_DECODER state.
1062
1063 mFlushComplete[audio][isDecoder] = true;
1064 if (!mFlushComplete[audio][!isDecoder]) {
1065 return;
1066 }
1067
1068 FlushStatus *state = audio ? &mFlushingAudio : &mFlushingVideo;
1069 switch (*state) {
1070 case FLUSHING_DECODER:
1071 {
1072 *state = FLUSHED;
Andy Hung8d121d42014-10-03 09:53:53 -07001073 break;
1074 }
1075
1076 case FLUSHING_DECODER_SHUTDOWN:
1077 {
1078 *state = SHUTTING_DOWN_DECODER;
1079
1080 ALOGV("initiating %s decoder shutdown", audio ? "audio" : "video");
1081 if (!audio) {
Andy Hung8d121d42014-10-03 09:53:53 -07001082 // Widevine source reads must stop before releasing the video decoder.
1083 if (mSource != NULL && mSourceFlags & Source::FLAG_SECURE) {
1084 mSource->stop();
1085 }
1086 }
1087 getDecoder(audio)->initiateShutdown();
1088 break;
1089 }
1090
1091 default:
1092 // decoder flush completes only occur in a flushing state.
1093 LOG_ALWAYS_FATAL_IF(isDecoder, "decoder flush in invalid state %d", *state);
1094 break;
1095 }
1096}
1097
Andreas Huber3831a062010-12-21 10:22:33 -08001098void NuPlayer::finishFlushIfPossible() {
Wei Jia53904f32014-07-29 10:22:53 -07001099 if (mFlushingAudio != NONE && mFlushingAudio != FLUSHED
1100 && mFlushingAudio != SHUT_DOWN) {
Andreas Huber3831a062010-12-21 10:22:33 -08001101 return;
1102 }
1103
Wei Jia53904f32014-07-29 10:22:53 -07001104 if (mFlushingVideo != NONE && mFlushingVideo != FLUSHED
1105 && mFlushingVideo != SHUT_DOWN) {
Andreas Huber3831a062010-12-21 10:22:33 -08001106 return;
1107 }
1108
Steve Block3856b092011-10-20 11:56:00 +01001109 ALOGV("both audio and video are flushed now.");
Andreas Huber3831a062010-12-21 10:22:33 -08001110
Andreas Huber3831a062010-12-21 10:22:33 -08001111 mFlushingAudio = NONE;
1112 mFlushingVideo = NONE;
Andreas Huber3831a062010-12-21 10:22:33 -08001113
Andy Hung8d121d42014-10-03 09:53:53 -07001114 clearFlushComplete();
1115
Andreas Hubera1f8ab02012-11-30 10:53:22 -08001116 processDeferredActions();
Andreas Huber1aef2112011-01-04 14:01:29 -08001117}
1118
1119void NuPlayer::postScanSources() {
1120 if (mScanSourcesPending) {
1121 return;
1122 }
1123
1124 sp<AMessage> msg = new AMessage(kWhatScanSources, id());
1125 msg->setInt32("generation", mScanSourcesGeneration);
1126 msg->post();
1127
1128 mScanSourcesPending = true;
1129}
1130
Andy Hung282a7e32014-08-14 15:56:34 -07001131void NuPlayer::openAudioSink(const sp<AMessage> &format, bool offloadOnly) {
Andy Hung282a7e32014-08-14 15:56:34 -07001132 uint32_t flags;
1133 int64_t durationUs;
Chong Zhang3b9eb1f2014-10-15 17:05:08 -07001134 bool hasVideo = (mVideoDecoder != NULL);
Andy Hung282a7e32014-08-14 15:56:34 -07001135 // FIXME: we should handle the case where the video decoder
1136 // is created after we receive the format change indication.
1137 // Current code will just make that we select deep buffer
1138 // with video which should not be a problem as it should
1139 // not prevent from keeping A/V sync.
Eric Laurentd88c3ca2014-10-28 10:52:11 -07001140 if (!hasVideo &&
Andy Hung282a7e32014-08-14 15:56:34 -07001141 mSource->getDuration(&durationUs) == OK &&
1142 durationUs
1143 > AUDIO_SINK_MIN_DEEP_BUFFER_DURATION_US) {
1144 flags = AUDIO_OUTPUT_FLAG_DEEP_BUFFER;
1145 } else {
1146 flags = AUDIO_OUTPUT_FLAG_NONE;
1147 }
1148
Chong Zhang3b9eb1f2014-10-15 17:05:08 -07001149 mOffloadAudio = mRenderer->openAudioSink(
1150 format, offloadOnly, hasVideo, flags);
1151
Andy Hung282a7e32014-08-14 15:56:34 -07001152 if (mOffloadAudio) {
Chong Zhang3b9eb1f2014-10-15 17:05:08 -07001153 sp<MetaData> audioMeta =
1154 mSource->getFormatMeta(true /* audio */);
1155 sendMetaDataToHal(mAudioSink, audioMeta);
Andy Hung282a7e32014-08-14 15:56:34 -07001156 }
1157}
1158
1159void NuPlayer::closeAudioSink() {
Chong Zhang3b9eb1f2014-10-15 17:05:08 -07001160 mRenderer->closeAudioSink();
Andy Hung282a7e32014-08-14 15:56:34 -07001161}
1162
Chong Zhang7137ec72014-11-12 16:41:05 -08001163status_t NuPlayer::instantiateDecoder(bool audio, sp<DecoderBase> *decoder) {
Andreas Huberf9334412010-12-15 15:17:42 -08001164 if (*decoder != NULL) {
1165 return OK;
1166 }
1167
Andreas Huber84066782011-08-16 09:34:26 -07001168 sp<AMessage> format = mSource->getFormat(audio);
Andreas Huberf9334412010-12-15 15:17:42 -08001169
Andreas Huber84066782011-08-16 09:34:26 -07001170 if (format == NULL) {
Andreas Huberf9334412010-12-15 15:17:42 -08001171 return -EWOULDBLOCK;
1172 }
1173
Andreas Huber3fe62152011-09-16 15:09:22 -07001174 if (!audio) {
Andreas Huber84066782011-08-16 09:34:26 -07001175 AString mime;
1176 CHECK(format->findString("mime", &mime));
Chong Zhanga7fa1d92014-06-11 14:49:23 -07001177
1178 sp<AMessage> ccNotify = new AMessage(kWhatClosedCaptionNotify, id());
1179 mCCDecoder = new CCDecoder(ccNotify);
Lajos Molnar09524832014-07-17 14:29:51 -07001180
1181 if (mSourceFlags & Source::FLAG_SECURE) {
1182 format->setInt32("secure", true);
1183 }
Andreas Huber3fe62152011-09-16 15:09:22 -07001184 }
1185
Wei Jiabc2fb722014-07-08 16:37:57 -07001186 if (audio) {
Wei Jia88703c32014-08-06 11:24:07 -07001187 sp<AMessage> notify = new AMessage(kWhatAudioNotify, id());
1188 ++mAudioDecoderGeneration;
1189 notify->setInt32("generation", mAudioDecoderGeneration);
1190
Wei Jiabc2fb722014-07-08 16:37:57 -07001191 if (mOffloadAudio) {
Wei Jiac6cfd702014-11-11 16:33:20 -08001192 *decoder = new DecoderPassThrough(notify, mSource, mRenderer);
Wei Jiabc2fb722014-07-08 16:37:57 -07001193 } else {
Wei Jiac6cfd702014-11-11 16:33:20 -08001194 *decoder = new Decoder(notify, mSource, mRenderer);
Wei Jiabc2fb722014-07-08 16:37:57 -07001195 }
1196 } else {
Wei Jia88703c32014-08-06 11:24:07 -07001197 sp<AMessage> notify = new AMessage(kWhatVideoNotify, id());
1198 ++mVideoDecoderGeneration;
1199 notify->setInt32("generation", mVideoDecoderGeneration);
1200
Chong Zhang7137ec72014-11-12 16:41:05 -08001201 *decoder = new Decoder(
1202 notify, mSource, mRenderer, mNativeWindow, mCCDecoder);
Lajos Molnard9fd6312014-11-06 11:00:00 -08001203
1204 // enable FRC if high-quality AV sync is requested, even if not
1205 // queuing to native window, as this will even improve textureview
1206 // playback.
1207 {
1208 char value[PROPERTY_VALUE_MAX];
1209 if (property_get("persist.sys.media.avsync", value, NULL) &&
1210 (!strcmp("1", value) || !strcasecmp("true", value))) {
1211 format->setInt32("auto-frc", 1);
1212 }
1213 }
Wei Jiabc2fb722014-07-08 16:37:57 -07001214 }
Lajos Molnar1cd13982014-01-17 15:12:51 -08001215 (*decoder)->init();
Andreas Huber84066782011-08-16 09:34:26 -07001216 (*decoder)->configure(format);
Andreas Huberf9334412010-12-15 15:17:42 -08001217
Lajos Molnar09524832014-07-17 14:29:51 -07001218 // allocate buffers to decrypt widevine source buffers
1219 if (!audio && (mSourceFlags & Source::FLAG_SECURE)) {
1220 Vector<sp<ABuffer> > inputBufs;
1221 CHECK_EQ((*decoder)->getInputBuffers(&inputBufs), (status_t)OK);
1222
1223 Vector<MediaBuffer *> mediaBufs;
1224 for (size_t i = 0; i < inputBufs.size(); i++) {
1225 const sp<ABuffer> &buffer = inputBufs[i];
1226 MediaBuffer *mbuf = new MediaBuffer(buffer->data(), buffer->size());
1227 mediaBufs.push(mbuf);
1228 }
1229
1230 status_t err = mSource->setBuffers(audio, mediaBufs);
1231 if (err != OK) {
1232 for (size_t i = 0; i < mediaBufs.size(); ++i) {
1233 mediaBufs[i]->release();
1234 }
1235 mediaBufs.clear();
1236 ALOGE("Secure source didn't support secure mediaBufs.");
1237 return err;
1238 }
1239 }
Andreas Huberf9334412010-12-15 15:17:42 -08001240 return OK;
1241}
1242
Chong Zhangced1c2f2014-08-08 15:22:35 -07001243void NuPlayer::updateVideoSize(
1244 const sp<AMessage> &inputFormat,
1245 const sp<AMessage> &outputFormat) {
1246 if (inputFormat == NULL) {
1247 ALOGW("Unknown video size, reporting 0x0!");
1248 notifyListener(MEDIA_SET_VIDEO_SIZE, 0, 0);
1249 return;
1250 }
1251
1252 int32_t displayWidth, displayHeight;
1253 int32_t cropLeft, cropTop, cropRight, cropBottom;
1254
1255 if (outputFormat != NULL) {
1256 int32_t width, height;
1257 CHECK(outputFormat->findInt32("width", &width));
1258 CHECK(outputFormat->findInt32("height", &height));
1259
1260 int32_t cropLeft, cropTop, cropRight, cropBottom;
1261 CHECK(outputFormat->findRect(
1262 "crop",
1263 &cropLeft, &cropTop, &cropRight, &cropBottom));
1264
1265 displayWidth = cropRight - cropLeft + 1;
1266 displayHeight = cropBottom - cropTop + 1;
1267
1268 ALOGV("Video output format changed to %d x %d "
1269 "(crop: %d x %d @ (%d, %d))",
1270 width, height,
1271 displayWidth,
1272 displayHeight,
1273 cropLeft, cropTop);
1274 } else {
1275 CHECK(inputFormat->findInt32("width", &displayWidth));
1276 CHECK(inputFormat->findInt32("height", &displayHeight));
1277
1278 ALOGV("Video input format %d x %d", displayWidth, displayHeight);
1279 }
1280
1281 // Take into account sample aspect ratio if necessary:
1282 int32_t sarWidth, sarHeight;
1283 if (inputFormat->findInt32("sar-width", &sarWidth)
1284 && inputFormat->findInt32("sar-height", &sarHeight)) {
1285 ALOGV("Sample aspect ratio %d : %d", sarWidth, sarHeight);
1286
1287 displayWidth = (displayWidth * sarWidth) / sarHeight;
1288
1289 ALOGV("display dimensions %d x %d", displayWidth, displayHeight);
1290 }
1291
1292 int32_t rotationDegrees;
1293 if (!inputFormat->findInt32("rotation-degrees", &rotationDegrees)) {
1294 rotationDegrees = 0;
1295 }
1296
1297 if (rotationDegrees == 90 || rotationDegrees == 270) {
1298 int32_t tmp = displayWidth;
1299 displayWidth = displayHeight;
1300 displayHeight = tmp;
1301 }
1302
1303 notifyListener(
1304 MEDIA_SET_VIDEO_SIZE,
1305 displayWidth,
1306 displayHeight);
1307}
1308
Chong Zhangdcb89b32013-08-06 09:44:47 -07001309void NuPlayer::notifyListener(int msg, int ext1, int ext2, const Parcel *in) {
Andreas Huber43c3e6c2011-01-05 12:17:08 -08001310 if (mDriver == NULL) {
Andreas Huberf9334412010-12-15 15:17:42 -08001311 return;
1312 }
1313
Andreas Huber43c3e6c2011-01-05 12:17:08 -08001314 sp<NuPlayerDriver> driver = mDriver.promote();
Andreas Huberf9334412010-12-15 15:17:42 -08001315
Andreas Huber43c3e6c2011-01-05 12:17:08 -08001316 if (driver == NULL) {
Andreas Huberf9334412010-12-15 15:17:42 -08001317 return;
1318 }
1319
Chong Zhangdcb89b32013-08-06 09:44:47 -07001320 driver->notifyListener(msg, ext1, ext2, in);
Andreas Huberf9334412010-12-15 15:17:42 -08001321}
1322
Chong Zhang7137ec72014-11-12 16:41:05 -08001323void NuPlayer::flushDecoder(bool audio, bool needShutdown) {
Andreas Huber14f76722013-01-15 09:04:18 -08001324 ALOGV("[%s] flushDecoder needShutdown=%d",
1325 audio ? "audio" : "video", needShutdown);
1326
Chong Zhang7137ec72014-11-12 16:41:05 -08001327 const sp<DecoderBase> &decoder = getDecoder(audio);
Lajos Molnar87603c02014-08-20 19:25:30 -07001328 if (decoder == NULL) {
Steve Blockdf64d152012-01-04 20:05:49 +00001329 ALOGI("flushDecoder %s without decoder present",
Andreas Huber6e3d3112011-11-28 12:36:11 -08001330 audio ? "audio" : "video");
Lajos Molnar87603c02014-08-20 19:25:30 -07001331 return;
Andreas Huber6e3d3112011-11-28 12:36:11 -08001332 }
1333
Andreas Huber1aef2112011-01-04 14:01:29 -08001334 // Make sure we don't continue to scan sources until we finish flushing.
1335 ++mScanSourcesGeneration;
Andreas Huber43c3e6c2011-01-05 12:17:08 -08001336 mScanSourcesPending = false;
Andreas Huber1aef2112011-01-04 14:01:29 -08001337
Chong Zhang7137ec72014-11-12 16:41:05 -08001338 decoder->signalFlush();
Andreas Huber1aef2112011-01-04 14:01:29 -08001339
1340 FlushStatus newStatus =
1341 needShutdown ? FLUSHING_DECODER_SHUTDOWN : FLUSHING_DECODER;
1342
Andy Hung8d121d42014-10-03 09:53:53 -07001343 mFlushComplete[audio][false /* isDecoder */] = false;
1344 mFlushComplete[audio][true /* isDecoder */] = false;
Andreas Huber1aef2112011-01-04 14:01:29 -08001345 if (audio) {
Wei Jia53904f32014-07-29 10:22:53 -07001346 ALOGE_IF(mFlushingAudio != NONE,
1347 "audio flushDecoder() is called in state %d", mFlushingAudio);
Andreas Huber1aef2112011-01-04 14:01:29 -08001348 mFlushingAudio = newStatus;
Andreas Huber1aef2112011-01-04 14:01:29 -08001349 } else {
Wei Jia53904f32014-07-29 10:22:53 -07001350 ALOGE_IF(mFlushingVideo != NONE,
1351 "video flushDecoder() is called in state %d", mFlushingVideo);
Andreas Huber1aef2112011-01-04 14:01:29 -08001352 mFlushingVideo = newStatus;
Andreas Huber1aef2112011-01-04 14:01:29 -08001353 }
1354}
1355
Chong Zhangced1c2f2014-08-08 15:22:35 -07001356void NuPlayer::queueDecoderShutdown(
1357 bool audio, bool video, const sp<AMessage> &reply) {
1358 ALOGI("queueDecoderShutdown audio=%d, video=%d", audio, video);
Andreas Huber84066782011-08-16 09:34:26 -07001359
Chong Zhangced1c2f2014-08-08 15:22:35 -07001360 mDeferredActions.push_back(
Wei Jiafef808d2014-10-31 17:57:05 -07001361 new FlushDecoderAction(
1362 audio ? FLUSH_CMD_SHUTDOWN : FLUSH_CMD_NONE,
1363 video ? FLUSH_CMD_SHUTDOWN : FLUSH_CMD_NONE));
Andreas Huber84066782011-08-16 09:34:26 -07001364
Chong Zhangced1c2f2014-08-08 15:22:35 -07001365 mDeferredActions.push_back(
1366 new SimpleAction(&NuPlayer::performScanSources));
Andreas Huber84066782011-08-16 09:34:26 -07001367
Chong Zhangced1c2f2014-08-08 15:22:35 -07001368 mDeferredActions.push_back(new PostMessageAction(reply));
1369
1370 processDeferredActions();
Andreas Huber84066782011-08-16 09:34:26 -07001371}
1372
James Dong0d268a32012-08-31 12:18:27 -07001373status_t NuPlayer::setVideoScalingMode(int32_t mode) {
1374 mVideoScalingMode = mode;
Andreas Huber57a339c2012-12-03 11:18:00 -08001375 if (mNativeWindow != NULL) {
James Dong0d268a32012-08-31 12:18:27 -07001376 status_t ret = native_window_set_scaling_mode(
1377 mNativeWindow->getNativeWindow().get(), mVideoScalingMode);
1378 if (ret != OK) {
1379 ALOGE("Failed to set scaling mode (%d): %s",
1380 -ret, strerror(-ret));
1381 return ret;
1382 }
1383 }
1384 return OK;
1385}
1386
Chong Zhangdcb89b32013-08-06 09:44:47 -07001387status_t NuPlayer::getTrackInfo(Parcel* reply) const {
1388 sp<AMessage> msg = new AMessage(kWhatGetTrackInfo, id());
1389 msg->setPointer("reply", reply);
1390
1391 sp<AMessage> response;
1392 status_t err = msg->postAndAwaitResponse(&response);
1393 return err;
1394}
1395
Robert Shih7c4f0d72014-07-09 18:53:31 -07001396status_t NuPlayer::getSelectedTrack(int32_t type, Parcel* reply) const {
1397 sp<AMessage> msg = new AMessage(kWhatGetSelectedTrack, id());
1398 msg->setPointer("reply", reply);
1399 msg->setInt32("type", type);
1400
1401 sp<AMessage> response;
1402 status_t err = msg->postAndAwaitResponse(&response);
1403 if (err == OK && response != NULL) {
1404 CHECK(response->findInt32("err", &err));
1405 }
1406 return err;
1407}
1408
Robert Shih6ffb1fd2014-10-29 16:24:32 -07001409status_t NuPlayer::selectTrack(size_t trackIndex, bool select, int64_t timeUs) {
Chong Zhangdcb89b32013-08-06 09:44:47 -07001410 sp<AMessage> msg = new AMessage(kWhatSelectTrack, id());
1411 msg->setSize("trackIndex", trackIndex);
1412 msg->setInt32("select", select);
Robert Shih6ffb1fd2014-10-29 16:24:32 -07001413 msg->setInt64("timeUs", timeUs);
Chong Zhangdcb89b32013-08-06 09:44:47 -07001414
1415 sp<AMessage> response;
1416 status_t err = msg->postAndAwaitResponse(&response);
1417
Chong Zhang404fced2014-06-11 14:45:31 -07001418 if (err != OK) {
1419 return err;
1420 }
1421
1422 if (!response->findInt32("err", &err)) {
1423 err = OK;
1424 }
1425
Chong Zhangdcb89b32013-08-06 09:44:47 -07001426 return err;
1427}
1428
Ronghua Wua73d9e02014-10-08 15:13:29 -07001429status_t NuPlayer::getCurrentPosition(int64_t *mediaUs) {
1430 sp<Renderer> renderer = mRenderer;
1431 if (renderer == NULL) {
1432 return NO_INIT;
1433 }
1434
1435 return renderer->getCurrentPosition(mediaUs);
1436}
1437
1438void NuPlayer::getStats(int64_t *numFramesTotal, int64_t *numFramesDropped) {
Chong Zhang7137ec72014-11-12 16:41:05 -08001439 sp<DecoderBase> decoder = getDecoder(false /* audio */);
1440 if (decoder != NULL) {
1441 decoder->getStats(numFramesTotal, numFramesDropped);
1442 } else {
1443 *numFramesTotal = 0;
1444 *numFramesDropped = 0;
1445 }
Ronghua Wua73d9e02014-10-08 15:13:29 -07001446}
1447
Marco Nelissenf0b72b52014-09-16 15:43:44 -07001448sp<MetaData> NuPlayer::getFileMeta() {
1449 return mSource->getFileFormatMeta();
1450}
1451
Andreas Huberb7c8e912012-11-27 15:02:53 -08001452void NuPlayer::schedulePollDuration() {
1453 sp<AMessage> msg = new AMessage(kWhatPollDuration, id());
1454 msg->setInt32("generation", mPollDurationGeneration);
1455 msg->post();
1456}
1457
1458void NuPlayer::cancelPollDuration() {
1459 ++mPollDurationGeneration;
1460}
1461
Andreas Hubera1f8ab02012-11-30 10:53:22 -08001462void NuPlayer::processDeferredActions() {
1463 while (!mDeferredActions.empty()) {
1464 // We won't execute any deferred actions until we're no longer in
1465 // an intermediate state, i.e. one more more decoders are currently
1466 // flushing or shutting down.
1467
Andreas Hubera1f8ab02012-11-30 10:53:22 -08001468 if (mFlushingAudio != NONE || mFlushingVideo != NONE) {
1469 // We're currently flushing, postpone the reset until that's
1470 // completed.
1471
1472 ALOGV("postponing action mFlushingAudio=%d, mFlushingVideo=%d",
1473 mFlushingAudio, mFlushingVideo);
1474
1475 break;
1476 }
1477
1478 sp<Action> action = *mDeferredActions.begin();
1479 mDeferredActions.erase(mDeferredActions.begin());
1480
1481 action->execute(this);
1482 }
1483}
1484
Wei Jiae427abf2014-09-22 15:21:11 -07001485void NuPlayer::performSeek(int64_t seekTimeUs, bool needNotify) {
1486 ALOGV("performSeek seekTimeUs=%lld us (%.2f secs), needNotify(%d)",
Andreas Hubera1f8ab02012-11-30 10:53:22 -08001487 seekTimeUs,
Wei Jiae427abf2014-09-22 15:21:11 -07001488 seekTimeUs / 1E6,
1489 needNotify);
Andreas Hubera1f8ab02012-11-30 10:53:22 -08001490
Andy Hungadf34bf2014-09-03 18:22:22 -07001491 if (mSource == NULL) {
1492 // This happens when reset occurs right before the loop mode
1493 // asynchronously seeks to the start of the stream.
1494 LOG_ALWAYS_FATAL_IF(mAudioDecoder != NULL || mVideoDecoder != NULL,
1495 "mSource is NULL and decoders not NULL audio(%p) video(%p)",
1496 mAudioDecoder.get(), mVideoDecoder.get());
1497 return;
1498 }
Andreas Hubera1f8ab02012-11-30 10:53:22 -08001499 mSource->seekTo(seekTimeUs);
Robert Shihd3b0bbb2014-07-23 15:00:25 -07001500 ++mTimedTextGeneration;
Andreas Hubera1f8ab02012-11-30 10:53:22 -08001501
1502 if (mDriver != NULL) {
1503 sp<NuPlayerDriver> driver = mDriver.promote();
1504 if (driver != NULL) {
Wei Jiae427abf2014-09-22 15:21:11 -07001505 if (needNotify) {
1506 driver->notifySeekComplete();
1507 }
Andreas Hubera1f8ab02012-11-30 10:53:22 -08001508 }
1509 }
1510
1511 // everything's flushed, continue playback.
1512}
1513
Wei Jiafef808d2014-10-31 17:57:05 -07001514void NuPlayer::performDecoderFlush(FlushCommand audio, FlushCommand video) {
1515 ALOGV("performDecoderFlush audio=%d, video=%d", audio, video);
Andreas Hubera1f8ab02012-11-30 10:53:22 -08001516
Wei Jiafef808d2014-10-31 17:57:05 -07001517 if ((audio == FLUSH_CMD_NONE || mAudioDecoder == NULL)
1518 && (video == FLUSH_CMD_NONE || mVideoDecoder == NULL)) {
Andreas Hubera1f8ab02012-11-30 10:53:22 -08001519 return;
1520 }
1521
Wei Jiafef808d2014-10-31 17:57:05 -07001522 if (audio != FLUSH_CMD_NONE && mAudioDecoder != NULL) {
1523 flushDecoder(true /* audio */, (audio == FLUSH_CMD_SHUTDOWN));
Andreas Hubera1f8ab02012-11-30 10:53:22 -08001524 }
1525
Wei Jiafef808d2014-10-31 17:57:05 -07001526 if (video != FLUSH_CMD_NONE && mVideoDecoder != NULL) {
1527 flushDecoder(false /* audio */, (video == FLUSH_CMD_SHUTDOWN));
Andreas Hubera1f8ab02012-11-30 10:53:22 -08001528 }
1529}
1530
1531void NuPlayer::performReset() {
1532 ALOGV("performReset");
1533
1534 CHECK(mAudioDecoder == NULL);
1535 CHECK(mVideoDecoder == NULL);
1536
1537 cancelPollDuration();
1538
1539 ++mScanSourcesGeneration;
1540 mScanSourcesPending = false;
1541
Lajos Molnar09524832014-07-17 14:29:51 -07001542 if (mRendererLooper != NULL) {
1543 if (mRenderer != NULL) {
1544 mRendererLooper->unregisterHandler(mRenderer->id());
1545 }
1546 mRendererLooper->stop();
1547 mRendererLooper.clear();
1548 }
Andreas Hubera1f8ab02012-11-30 10:53:22 -08001549 mRenderer.clear();
Wei Jia57568df2014-09-22 10:16:29 -07001550 ++mRendererGeneration;
Andreas Hubera1f8ab02012-11-30 10:53:22 -08001551
1552 if (mSource != NULL) {
1553 mSource->stop();
Andreas Huberb5f25f02013-02-05 10:14:26 -08001554
Andreas Hubera1f8ab02012-11-30 10:53:22 -08001555 mSource.clear();
1556 }
1557
1558 if (mDriver != NULL) {
1559 sp<NuPlayerDriver> driver = mDriver.promote();
1560 if (driver != NULL) {
1561 driver->notifyResetComplete();
1562 }
1563 }
Andreas Huber57a339c2012-12-03 11:18:00 -08001564
1565 mStarted = false;
Andreas Hubera1f8ab02012-11-30 10:53:22 -08001566}
1567
1568void NuPlayer::performScanSources() {
1569 ALOGV("performScanSources");
1570
Andreas Huber57a339c2012-12-03 11:18:00 -08001571 if (!mStarted) {
1572 return;
1573 }
1574
Andreas Hubera1f8ab02012-11-30 10:53:22 -08001575 if (mAudioDecoder == NULL || mVideoDecoder == NULL) {
1576 postScanSources();
1577 }
1578}
1579
Andreas Huber57a339c2012-12-03 11:18:00 -08001580void NuPlayer::performSetSurface(const sp<NativeWindowWrapper> &wrapper) {
1581 ALOGV("performSetSurface");
1582
1583 mNativeWindow = wrapper;
1584
1585 // XXX - ignore error from setVideoScalingMode for now
1586 setVideoScalingMode(mVideoScalingMode);
Chong Zhang13d6faa2014-08-22 15:35:28 -07001587
1588 if (mDriver != NULL) {
1589 sp<NuPlayerDriver> driver = mDriver.promote();
1590 if (driver != NULL) {
1591 driver->notifySetSurfaceComplete();
1592 }
1593 }
Andreas Huber57a339c2012-12-03 11:18:00 -08001594}
1595
Chong Zhang7137ec72014-11-12 16:41:05 -08001596void NuPlayer::performResumeDecoders() {
1597 if (mVideoDecoder != NULL) {
1598 mVideoDecoder->signalResume();
1599 }
1600
1601 if (mAudioDecoder != NULL) {
1602 mAudioDecoder->signalResume();
1603 }
1604}
1605
Andreas Huber9575c962013-02-05 13:59:56 -08001606void NuPlayer::onSourceNotify(const sp<AMessage> &msg) {
1607 int32_t what;
1608 CHECK(msg->findInt32("what", &what));
1609
1610 switch (what) {
1611 case Source::kWhatPrepared:
1612 {
Andreas Huberb5f28d42013-04-25 15:11:19 -07001613 if (mSource == NULL) {
1614 // This is a stale notification from a source that was
1615 // asynchronously preparing when the client called reset().
1616 // We handled the reset, the source is gone.
1617 break;
1618 }
1619
Andreas Huberec0c5972013-02-05 14:47:13 -08001620 int32_t err;
1621 CHECK(msg->findInt32("err", &err));
1622
Andreas Huber9575c962013-02-05 13:59:56 -08001623 sp<NuPlayerDriver> driver = mDriver.promote();
1624 if (driver != NULL) {
Marco Nelissendd114d12014-05-28 15:23:14 -07001625 // notify duration first, so that it's definitely set when
1626 // the app received the "prepare complete" callback.
1627 int64_t durationUs;
1628 if (mSource->getDuration(&durationUs) == OK) {
1629 driver->notifyDuration(durationUs);
1630 }
Andreas Huberec0c5972013-02-05 14:47:13 -08001631 driver->notifyPrepareCompleted(err);
Andreas Huber9575c962013-02-05 13:59:56 -08001632 }
Andreas Huber99759402013-04-01 14:28:31 -07001633
Andreas Huber9575c962013-02-05 13:59:56 -08001634 break;
1635 }
1636
1637 case Source::kWhatFlagsChanged:
1638 {
1639 uint32_t flags;
1640 CHECK(msg->findInt32("flags", (int32_t *)&flags));
1641
Chong Zhang4b7069d2013-09-11 12:52:43 -07001642 sp<NuPlayerDriver> driver = mDriver.promote();
1643 if (driver != NULL) {
1644 driver->notifyFlagsChanged(flags);
1645 }
1646
Andreas Huber9575c962013-02-05 13:59:56 -08001647 if ((mSourceFlags & Source::FLAG_DYNAMIC_DURATION)
1648 && (!(flags & Source::FLAG_DYNAMIC_DURATION))) {
1649 cancelPollDuration();
1650 } else if (!(mSourceFlags & Source::FLAG_DYNAMIC_DURATION)
1651 && (flags & Source::FLAG_DYNAMIC_DURATION)
1652 && (mAudioDecoder != NULL || mVideoDecoder != NULL)) {
1653 schedulePollDuration();
1654 }
1655
1656 mSourceFlags = flags;
1657 break;
1658 }
1659
1660 case Source::kWhatVideoSizeChanged:
1661 {
Chong Zhangced1c2f2014-08-08 15:22:35 -07001662 sp<AMessage> format;
1663 CHECK(msg->findMessage("format", &format));
Andreas Huber9575c962013-02-05 13:59:56 -08001664
Chong Zhangced1c2f2014-08-08 15:22:35 -07001665 updateVideoSize(format);
Andreas Huber9575c962013-02-05 13:59:56 -08001666 break;
1667 }
1668
Chong Zhang2a3cc9a2014-08-21 17:48:26 -07001669 case Source::kWhatBufferingUpdate:
1670 {
1671 int32_t percentage;
1672 CHECK(msg->findInt32("percentage", &percentage));
1673
1674 notifyListener(MEDIA_BUFFERING_UPDATE, percentage, 0);
1675 break;
1676 }
1677
Roger Jönssonb50e83e2013-01-21 16:26:41 +01001678 case Source::kWhatBufferingStart:
1679 {
1680 notifyListener(MEDIA_INFO, MEDIA_INFO_BUFFERING_START, 0);
1681 break;
1682 }
1683
1684 case Source::kWhatBufferingEnd:
1685 {
1686 notifyListener(MEDIA_INFO, MEDIA_INFO_BUFFERING_END, 0);
1687 break;
1688 }
1689
Chong Zhangdcb89b32013-08-06 09:44:47 -07001690 case Source::kWhatSubtitleData:
1691 {
1692 sp<ABuffer> buffer;
1693 CHECK(msg->findBuffer("buffer", &buffer));
1694
Chong Zhang404fced2014-06-11 14:45:31 -07001695 sendSubtitleData(buffer, 0 /* baseIndex */);
Chong Zhangdcb89b32013-08-06 09:44:47 -07001696 break;
1697 }
1698
Robert Shihd3b0bbb2014-07-23 15:00:25 -07001699 case Source::kWhatTimedTextData:
1700 {
1701 int32_t generation;
1702 if (msg->findInt32("generation", &generation)
1703 && generation != mTimedTextGeneration) {
1704 break;
1705 }
1706
1707 sp<ABuffer> buffer;
1708 CHECK(msg->findBuffer("buffer", &buffer));
1709
1710 sp<NuPlayerDriver> driver = mDriver.promote();
1711 if (driver == NULL) {
1712 break;
1713 }
1714
1715 int posMs;
1716 int64_t timeUs, posUs;
1717 driver->getCurrentPosition(&posMs);
1718 posUs = posMs * 1000;
1719 CHECK(buffer->meta()->findInt64("timeUs", &timeUs));
1720
1721 if (posUs < timeUs) {
1722 if (!msg->findInt32("generation", &generation)) {
1723 msg->setInt32("generation", mTimedTextGeneration);
1724 }
1725 msg->post(timeUs - posUs);
1726 } else {
1727 sendTimedTextData(buffer);
1728 }
1729 break;
1730 }
1731
Andreas Huber14f76722013-01-15 09:04:18 -08001732 case Source::kWhatQueueDecoderShutdown:
1733 {
1734 int32_t audio, video;
1735 CHECK(msg->findInt32("audio", &audio));
1736 CHECK(msg->findInt32("video", &video));
1737
1738 sp<AMessage> reply;
1739 CHECK(msg->findMessage("reply", &reply));
1740
1741 queueDecoderShutdown(audio, video, reply);
1742 break;
1743 }
1744
Ronghua Wu80276872014-08-28 15:50:29 -07001745 case Source::kWhatDrmNoLicense:
1746 {
1747 notifyListener(MEDIA_ERROR, MEDIA_ERROR_UNKNOWN, ERROR_DRM_NO_LICENSE);
1748 break;
1749 }
1750
Andreas Huber9575c962013-02-05 13:59:56 -08001751 default:
1752 TRESPASS();
1753 }
1754}
1755
Chong Zhanga7fa1d92014-06-11 14:49:23 -07001756void NuPlayer::onClosedCaptionNotify(const sp<AMessage> &msg) {
1757 int32_t what;
1758 CHECK(msg->findInt32("what", &what));
1759
1760 switch (what) {
1761 case NuPlayer::CCDecoder::kWhatClosedCaptionData:
1762 {
1763 sp<ABuffer> buffer;
1764 CHECK(msg->findBuffer("buffer", &buffer));
1765
1766 size_t inbandTracks = 0;
1767 if (mSource != NULL) {
1768 inbandTracks = mSource->getTrackCount();
1769 }
1770
1771 sendSubtitleData(buffer, inbandTracks);
1772 break;
1773 }
1774
1775 case NuPlayer::CCDecoder::kWhatTrackAdded:
1776 {
1777 notifyListener(MEDIA_INFO, MEDIA_INFO_METADATA_UPDATE, 0);
1778
1779 break;
1780 }
1781
1782 default:
1783 TRESPASS();
1784 }
1785
1786
1787}
1788
Chong Zhang404fced2014-06-11 14:45:31 -07001789void NuPlayer::sendSubtitleData(const sp<ABuffer> &buffer, int32_t baseIndex) {
1790 int32_t trackIndex;
1791 int64_t timeUs, durationUs;
1792 CHECK(buffer->meta()->findInt32("trackIndex", &trackIndex));
1793 CHECK(buffer->meta()->findInt64("timeUs", &timeUs));
1794 CHECK(buffer->meta()->findInt64("durationUs", &durationUs));
1795
1796 Parcel in;
1797 in.writeInt32(trackIndex + baseIndex);
1798 in.writeInt64(timeUs);
1799 in.writeInt64(durationUs);
1800 in.writeInt32(buffer->size());
1801 in.writeInt32(buffer->size());
1802 in.write(buffer->data(), buffer->size());
1803
1804 notifyListener(MEDIA_SUBTITLE_DATA, 0, 0, &in);
1805}
Robert Shihd3b0bbb2014-07-23 15:00:25 -07001806
1807void NuPlayer::sendTimedTextData(const sp<ABuffer> &buffer) {
1808 const void *data;
1809 size_t size = 0;
1810 int64_t timeUs;
1811 int32_t flag = TextDescriptions::LOCAL_DESCRIPTIONS;
1812
1813 AString mime;
1814 CHECK(buffer->meta()->findString("mime", &mime));
1815 CHECK(strcasecmp(mime.c_str(), MEDIA_MIMETYPE_TEXT_3GPP) == 0);
1816
1817 data = buffer->data();
1818 size = buffer->size();
1819
1820 Parcel parcel;
1821 if (size > 0) {
1822 CHECK(buffer->meta()->findInt64("timeUs", &timeUs));
1823 flag |= TextDescriptions::IN_BAND_TEXT_3GPP;
1824 TextDescriptions::getParcelOfDescriptions(
1825 (const uint8_t *)data, size, flag, timeUs / 1000, &parcel);
1826 }
1827
1828 if ((parcel.dataSize() > 0)) {
1829 notifyListener(MEDIA_TIMED_TEXT, 0, 0, &parcel);
1830 } else { // send an empty timed text
1831 notifyListener(MEDIA_TIMED_TEXT, 0, 0);
1832 }
1833}
Andreas Huberb5f25f02013-02-05 10:14:26 -08001834////////////////////////////////////////////////////////////////////////////////
1835
Chong Zhangced1c2f2014-08-08 15:22:35 -07001836sp<AMessage> NuPlayer::Source::getFormat(bool audio) {
1837 sp<MetaData> meta = getFormatMeta(audio);
1838
1839 if (meta == NULL) {
1840 return NULL;
1841 }
1842
1843 sp<AMessage> msg = new AMessage;
1844
1845 if(convertMetaDataToMessage(meta, &msg) == OK) {
1846 return msg;
1847 }
1848 return NULL;
1849}
1850
Andreas Huber9575c962013-02-05 13:59:56 -08001851void NuPlayer::Source::notifyFlagsChanged(uint32_t flags) {
1852 sp<AMessage> notify = dupNotify();
1853 notify->setInt32("what", kWhatFlagsChanged);
1854 notify->setInt32("flags", flags);
1855 notify->post();
1856}
1857
Chong Zhangced1c2f2014-08-08 15:22:35 -07001858void NuPlayer::Source::notifyVideoSizeChanged(const sp<AMessage> &format) {
Andreas Huber9575c962013-02-05 13:59:56 -08001859 sp<AMessage> notify = dupNotify();
1860 notify->setInt32("what", kWhatVideoSizeChanged);
Chong Zhangced1c2f2014-08-08 15:22:35 -07001861 notify->setMessage("format", format);
Andreas Huber9575c962013-02-05 13:59:56 -08001862 notify->post();
1863}
1864
Andreas Huberec0c5972013-02-05 14:47:13 -08001865void NuPlayer::Source::notifyPrepared(status_t err) {
Andreas Huber9575c962013-02-05 13:59:56 -08001866 sp<AMessage> notify = dupNotify();
1867 notify->setInt32("what", kWhatPrepared);
Andreas Huberec0c5972013-02-05 14:47:13 -08001868 notify->setInt32("err", err);
Andreas Huber9575c962013-02-05 13:59:56 -08001869 notify->post();
1870}
1871
Andreas Huber84333e02014-02-07 15:36:10 -08001872void NuPlayer::Source::onMessageReceived(const sp<AMessage> & /* msg */) {
Andreas Huberb5f25f02013-02-05 10:14:26 -08001873 TRESPASS();
1874}
1875
Andreas Huberf9334412010-12-15 15:17:42 -08001876} // namespace android