blob: c01f16a37987a084e7f0e0a9e967b045e00e7905 [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 }
Marco Nelissen6c41e622014-11-19 13:22:23 -08001348 } else if (err == ERROR_END_OF_STREAM
1349 && doBufferAggregation && (mAggregateBuffer != NULL)) {
1350 // send out the last bit of aggregated data
1351 reply->setBuffer("buffer", mAggregateBuffer);
1352 mAggregateBuffer.clear();
1353 err = OK;
Andreas Huber32f3cef2011-03-02 15:34:46 -08001354 }
1355
Andreas Huber3fe62152011-09-16 15:09:22 -07001356 reply->setInt32("err", err);
1357 reply->post();
1358 return OK;
Andreas Huberf9334412010-12-15 15:17:42 -08001359 }
1360
Andreas Huber3fe62152011-09-16 15:09:22 -07001361 if (!audio) {
1362 ++mNumFramesTotal;
1363 }
1364
1365 dropAccessUnit = false;
1366 if (!audio
Lajos Molnar09524832014-07-17 14:29:51 -07001367 && !(mSourceFlags & Source::FLAG_SECURE)
Ronghua Wua73d9e02014-10-08 15:13:29 -07001368 && mRenderer->getVideoLateByUs() > 100000ll
Andreas Huber3fe62152011-09-16 15:09:22 -07001369 && mVideoIsAVC
1370 && !IsAVCReferenceFrame(accessUnit)) {
1371 dropAccessUnit = true;
1372 ++mNumFramesDropped;
1373 }
Phil Burk9f526492014-09-03 15:04:12 -07001374
1375 size_t smallSize = accessUnit->size();
1376 needMoreData = false;
Phil Burkc5cc2e22014-09-09 20:08:39 -07001377 if (doBufferAggregation && (mAggregateBuffer == NULL)
Phil Burk9f526492014-09-03 15:04:12 -07001378 // Don't bother if only room for a few small buffers.
Phil Burkc5cc2e22014-09-09 20:08:39 -07001379 && (smallSize < (kAggregateBufferSizeBytes / 3))) {
Phil Burk9f526492014-09-03 15:04:12 -07001380 // Create a larger buffer for combining smaller buffers from the extractor.
Phil Burkc5cc2e22014-09-09 20:08:39 -07001381 mAggregateBuffer = new ABuffer(kAggregateBufferSizeBytes);
1382 mAggregateBuffer->setRange(0, 0); // start empty
Phil Burk9f526492014-09-03 15:04:12 -07001383 }
1384
Phil Burk33b51b02014-09-17 16:03:47 -07001385 if (doBufferAggregation && (mAggregateBuffer != NULL)) {
Phil Burk9f526492014-09-03 15:04:12 -07001386 int64_t timeUs;
Phil Burkc5cc2e22014-09-09 20:08:39 -07001387 int64_t dummy;
Phil Burk9f526492014-09-03 15:04:12 -07001388 bool smallTimestampValid = accessUnit->meta()->findInt64("timeUs", &timeUs);
Phil Burkc5cc2e22014-09-09 20:08:39 -07001389 bool bigTimestampValid = mAggregateBuffer->meta()->findInt64("timeUs", &dummy);
Phil Burk9f526492014-09-03 15:04:12 -07001390 // Will the smaller buffer fit?
Phil Burkc5cc2e22014-09-09 20:08:39 -07001391 size_t bigSize = mAggregateBuffer->size();
1392 size_t roomLeft = mAggregateBuffer->capacity() - bigSize;
Phil Burk9f526492014-09-03 15:04:12 -07001393 // Should we save this small buffer for the next big buffer?
1394 // If the first small buffer did not have a timestamp then save
1395 // any buffer that does have a timestamp until the next big buffer.
1396 if ((smallSize > roomLeft)
Phil Burkc5cc2e22014-09-09 20:08:39 -07001397 || (!bigTimestampValid && (bigSize > 0) && smallTimestampValid)) {
Phil Burk9f526492014-09-03 15:04:12 -07001398 mPendingAudioErr = err;
1399 mPendingAudioAccessUnit = accessUnit;
1400 accessUnit.clear();
1401 } else {
Phil Burkc5cc2e22014-09-09 20:08:39 -07001402 // Grab time from first small buffer if available.
1403 if ((bigSize == 0) && smallTimestampValid) {
1404 mAggregateBuffer->meta()->setInt64("timeUs", timeUs);
1405 }
Phil Burk9f526492014-09-03 15:04:12 -07001406 // Append small buffer to the bigger buffer.
Phil Burkc5cc2e22014-09-09 20:08:39 -07001407 memcpy(mAggregateBuffer->base() + bigSize, accessUnit->data(), smallSize);
Phil Burk9f526492014-09-03 15:04:12 -07001408 bigSize += smallSize;
Phil Burkc5cc2e22014-09-09 20:08:39 -07001409 mAggregateBuffer->setRange(0, bigSize);
Phil Burk9f526492014-09-03 15:04:12 -07001410
Phil Burkc5cc2e22014-09-09 20:08:39 -07001411 // Keep looping until we run out of room in the mAggregateBuffer.
Phil Burk9f526492014-09-03 15:04:12 -07001412 needMoreData = true;
1413
Phil Burkc5cc2e22014-09-09 20:08:39 -07001414 ALOGV("feedDecoderInputData() smallSize = %zu, bigSize = %zu, capacity = %zu",
1415 smallSize, bigSize, mAggregateBuffer->capacity());
Phil Burk9f526492014-09-03 15:04:12 -07001416 }
1417 }
1418 } while (dropAccessUnit || needMoreData);
Andreas Huberf9334412010-12-15 15:17:42 -08001419
Steve Block3856b092011-10-20 11:56:00 +01001420 // ALOGV("returned a valid buffer of %s data", audio ? "audio" : "video");
Andreas Huberf9334412010-12-15 15:17:42 -08001421
1422#if 0
1423 int64_t mediaTimeUs;
1424 CHECK(accessUnit->meta()->findInt64("timeUs", &mediaTimeUs));
Steve Block3856b092011-10-20 11:56:00 +01001425 ALOGV("feeding %s input buffer at media time %.2f secs",
Andreas Huberf9334412010-12-15 15:17:42 -08001426 audio ? "audio" : "video",
1427 mediaTimeUs / 1E6);
1428#endif
1429
Chong Zhanga7fa1d92014-06-11 14:49:23 -07001430 if (!audio) {
1431 mCCDecoder->decode(accessUnit);
1432 }
1433
Phil Burk33b51b02014-09-17 16:03:47 -07001434 if (doBufferAggregation && (mAggregateBuffer != NULL)) {
Phil Burkc5cc2e22014-09-09 20:08:39 -07001435 ALOGV("feedDecoderInputData() reply with aggregated buffer, %zu",
1436 mAggregateBuffer->size());
1437 reply->setBuffer("buffer", mAggregateBuffer);
1438 mAggregateBuffer.clear();
Phil Burk9f526492014-09-03 15:04:12 -07001439 } else {
1440 reply->setBuffer("buffer", accessUnit);
1441 }
1442
Andreas Huberf9334412010-12-15 15:17:42 -08001443 reply->post();
1444
1445 return OK;
1446}
1447
1448void NuPlayer::renderBuffer(bool audio, const sp<AMessage> &msg) {
Steve Block3856b092011-10-20 11:56:00 +01001449 // ALOGV("renderBuffer %s", audio ? "audio" : "video");
Andreas Huberf9334412010-12-15 15:17:42 -08001450
Wei Jia53904f32014-07-29 10:22:53 -07001451 if ((audio && mFlushingAudio != NONE)
1452 || (!audio && mFlushingVideo != NONE)) {
Andreas Huber18ac5402011-08-31 15:04:25 -07001453 // We're currently attempting to flush the decoder, in order
1454 // to complete this, the decoder wants all its buffers back,
1455 // so we don't want any output buffers it sent us (from before
1456 // we initiated the flush) to be stuck in the renderer's queue.
1457
Steve Block3856b092011-10-20 11:56:00 +01001458 ALOGV("we're still flushing the %s decoder, sending its output buffer"
Andreas Huber18ac5402011-08-31 15:04:25 -07001459 " right back.", audio ? "audio" : "video");
1460
Andreas Huber18ac5402011-08-31 15:04:25 -07001461 return;
1462 }
1463
Chong Zhanga7fa1d92014-06-11 14:49:23 -07001464 int64_t mediaTimeUs;
Wei Jiac6cfd702014-11-11 16:33:20 -08001465 CHECK(msg->findInt64("timeUs", &mediaTimeUs));
Andreas Huber32f3cef2011-03-02 15:34:46 -08001466
Chong Zhanga7fa1d92014-06-11 14:49:23 -07001467 if (!audio && mCCDecoder->isSelected()) {
1468 mCCDecoder->display(mediaTimeUs);
1469 }
Andreas Huberf9334412010-12-15 15:17:42 -08001470}
1471
Chong Zhangced1c2f2014-08-08 15:22:35 -07001472void NuPlayer::updateVideoSize(
1473 const sp<AMessage> &inputFormat,
1474 const sp<AMessage> &outputFormat) {
1475 if (inputFormat == NULL) {
1476 ALOGW("Unknown video size, reporting 0x0!");
1477 notifyListener(MEDIA_SET_VIDEO_SIZE, 0, 0);
1478 return;
1479 }
1480
1481 int32_t displayWidth, displayHeight;
1482 int32_t cropLeft, cropTop, cropRight, cropBottom;
1483
1484 if (outputFormat != NULL) {
1485 int32_t width, height;
1486 CHECK(outputFormat->findInt32("width", &width));
1487 CHECK(outputFormat->findInt32("height", &height));
1488
1489 int32_t cropLeft, cropTop, cropRight, cropBottom;
1490 CHECK(outputFormat->findRect(
1491 "crop",
1492 &cropLeft, &cropTop, &cropRight, &cropBottom));
1493
1494 displayWidth = cropRight - cropLeft + 1;
1495 displayHeight = cropBottom - cropTop + 1;
1496
1497 ALOGV("Video output format changed to %d x %d "
1498 "(crop: %d x %d @ (%d, %d))",
1499 width, height,
1500 displayWidth,
1501 displayHeight,
1502 cropLeft, cropTop);
1503 } else {
1504 CHECK(inputFormat->findInt32("width", &displayWidth));
1505 CHECK(inputFormat->findInt32("height", &displayHeight));
1506
1507 ALOGV("Video input format %d x %d", displayWidth, displayHeight);
1508 }
1509
1510 // Take into account sample aspect ratio if necessary:
1511 int32_t sarWidth, sarHeight;
1512 if (inputFormat->findInt32("sar-width", &sarWidth)
1513 && inputFormat->findInt32("sar-height", &sarHeight)) {
1514 ALOGV("Sample aspect ratio %d : %d", sarWidth, sarHeight);
1515
1516 displayWidth = (displayWidth * sarWidth) / sarHeight;
1517
1518 ALOGV("display dimensions %d x %d", displayWidth, displayHeight);
1519 }
1520
1521 int32_t rotationDegrees;
1522 if (!inputFormat->findInt32("rotation-degrees", &rotationDegrees)) {
1523 rotationDegrees = 0;
1524 }
1525
1526 if (rotationDegrees == 90 || rotationDegrees == 270) {
1527 int32_t tmp = displayWidth;
1528 displayWidth = displayHeight;
1529 displayHeight = tmp;
1530 }
1531
1532 notifyListener(
1533 MEDIA_SET_VIDEO_SIZE,
1534 displayWidth,
1535 displayHeight);
1536}
1537
Chong Zhangdcb89b32013-08-06 09:44:47 -07001538void NuPlayer::notifyListener(int msg, int ext1, int ext2, const Parcel *in) {
Andreas Huber43c3e6c2011-01-05 12:17:08 -08001539 if (mDriver == NULL) {
Andreas Huberf9334412010-12-15 15:17:42 -08001540 return;
1541 }
1542
Andreas Huber43c3e6c2011-01-05 12:17:08 -08001543 sp<NuPlayerDriver> driver = mDriver.promote();
Andreas Huberf9334412010-12-15 15:17:42 -08001544
Andreas Huber43c3e6c2011-01-05 12:17:08 -08001545 if (driver == NULL) {
Andreas Huberf9334412010-12-15 15:17:42 -08001546 return;
1547 }
1548
Chong Zhangdcb89b32013-08-06 09:44:47 -07001549 driver->notifyListener(msg, ext1, ext2, in);
Andreas Huberf9334412010-12-15 15:17:42 -08001550}
1551
Lajos Molnar87603c02014-08-20 19:25:30 -07001552void NuPlayer::flushDecoder(
1553 bool audio, bool needShutdown, const sp<AMessage> &newFormat) {
Andreas Huber14f76722013-01-15 09:04:18 -08001554 ALOGV("[%s] flushDecoder needShutdown=%d",
1555 audio ? "audio" : "video", needShutdown);
1556
Lajos Molnar87603c02014-08-20 19:25:30 -07001557 const sp<Decoder> &decoder = getDecoder(audio);
1558 if (decoder == NULL) {
Steve Blockdf64d152012-01-04 20:05:49 +00001559 ALOGI("flushDecoder %s without decoder present",
Andreas Huber6e3d3112011-11-28 12:36:11 -08001560 audio ? "audio" : "video");
Lajos Molnar87603c02014-08-20 19:25:30 -07001561 return;
Andreas Huber6e3d3112011-11-28 12:36:11 -08001562 }
1563
Andreas Huber1aef2112011-01-04 14:01:29 -08001564 // Make sure we don't continue to scan sources until we finish flushing.
1565 ++mScanSourcesGeneration;
Andreas Huber43c3e6c2011-01-05 12:17:08 -08001566 mScanSourcesPending = false;
Andreas Huber1aef2112011-01-04 14:01:29 -08001567
Lajos Molnar87603c02014-08-20 19:25:30 -07001568 decoder->signalFlush(newFormat);
Andreas Huber1aef2112011-01-04 14:01:29 -08001569
1570 FlushStatus newStatus =
1571 needShutdown ? FLUSHING_DECODER_SHUTDOWN : FLUSHING_DECODER;
1572
Andy Hung8d121d42014-10-03 09:53:53 -07001573 mFlushComplete[audio][false /* isDecoder */] = false;
1574 mFlushComplete[audio][true /* isDecoder */] = false;
Andreas Huber1aef2112011-01-04 14:01:29 -08001575 if (audio) {
Wei Jia53904f32014-07-29 10:22:53 -07001576 ALOGE_IF(mFlushingAudio != NONE,
1577 "audio flushDecoder() is called in state %d", mFlushingAudio);
Andreas Huber1aef2112011-01-04 14:01:29 -08001578 mFlushingAudio = newStatus;
Andreas Huber1aef2112011-01-04 14:01:29 -08001579 } else {
Wei Jia53904f32014-07-29 10:22:53 -07001580 ALOGE_IF(mFlushingVideo != NONE,
1581 "video flushDecoder() is called in state %d", mFlushingVideo);
Andreas Huber1aef2112011-01-04 14:01:29 -08001582 mFlushingVideo = newStatus;
Chong Zhangb86e68f2014-08-01 13:46:53 -07001583
1584 if (mCCDecoder != NULL) {
1585 mCCDecoder->flush();
1586 }
Andreas Huber1aef2112011-01-04 14:01:29 -08001587 }
1588}
1589
Lajos Molnar87603c02014-08-20 19:25:30 -07001590void NuPlayer::updateDecoderFormatWithoutFlush(
1591 bool audio, const sp<AMessage> &format) {
1592 ALOGV("[%s] updateDecoderFormatWithoutFlush", audio ? "audio" : "video");
1593
1594 const sp<Decoder> &decoder = getDecoder(audio);
1595 if (decoder == NULL) {
1596 ALOGI("updateDecoderFormatWithoutFlush %s without decoder present",
1597 audio ? "audio" : "video");
1598 return;
1599 }
1600
1601 decoder->signalUpdateFormat(format);
1602}
1603
Chong Zhangced1c2f2014-08-08 15:22:35 -07001604void NuPlayer::queueDecoderShutdown(
1605 bool audio, bool video, const sp<AMessage> &reply) {
1606 ALOGI("queueDecoderShutdown audio=%d, video=%d", audio, video);
Andreas Huber84066782011-08-16 09:34:26 -07001607
Chong Zhangced1c2f2014-08-08 15:22:35 -07001608 mDeferredActions.push_back(
Wei Jiafef808d2014-10-31 17:57:05 -07001609 new FlushDecoderAction(
1610 audio ? FLUSH_CMD_SHUTDOWN : FLUSH_CMD_NONE,
1611 video ? FLUSH_CMD_SHUTDOWN : FLUSH_CMD_NONE));
Andreas Huber84066782011-08-16 09:34:26 -07001612
Chong Zhangced1c2f2014-08-08 15:22:35 -07001613 mDeferredActions.push_back(
1614 new SimpleAction(&NuPlayer::performScanSources));
Andreas Huber84066782011-08-16 09:34:26 -07001615
Chong Zhangced1c2f2014-08-08 15:22:35 -07001616 mDeferredActions.push_back(new PostMessageAction(reply));
1617
1618 processDeferredActions();
Andreas Huber84066782011-08-16 09:34:26 -07001619}
1620
James Dong0d268a32012-08-31 12:18:27 -07001621status_t NuPlayer::setVideoScalingMode(int32_t mode) {
1622 mVideoScalingMode = mode;
Andreas Huber57a339c2012-12-03 11:18:00 -08001623 if (mNativeWindow != NULL) {
James Dong0d268a32012-08-31 12:18:27 -07001624 status_t ret = native_window_set_scaling_mode(
1625 mNativeWindow->getNativeWindow().get(), mVideoScalingMode);
1626 if (ret != OK) {
1627 ALOGE("Failed to set scaling mode (%d): %s",
1628 -ret, strerror(-ret));
1629 return ret;
1630 }
1631 }
1632 return OK;
1633}
1634
Chong Zhangdcb89b32013-08-06 09:44:47 -07001635status_t NuPlayer::getTrackInfo(Parcel* reply) const {
1636 sp<AMessage> msg = new AMessage(kWhatGetTrackInfo, id());
1637 msg->setPointer("reply", reply);
1638
1639 sp<AMessage> response;
1640 status_t err = msg->postAndAwaitResponse(&response);
1641 return err;
1642}
1643
Robert Shih7c4f0d72014-07-09 18:53:31 -07001644status_t NuPlayer::getSelectedTrack(int32_t type, Parcel* reply) const {
1645 sp<AMessage> msg = new AMessage(kWhatGetSelectedTrack, id());
1646 msg->setPointer("reply", reply);
1647 msg->setInt32("type", type);
1648
1649 sp<AMessage> response;
1650 status_t err = msg->postAndAwaitResponse(&response);
1651 if (err == OK && response != NULL) {
1652 CHECK(response->findInt32("err", &err));
1653 }
1654 return err;
1655}
1656
Robert Shih6ffb1fd2014-10-29 16:24:32 -07001657status_t NuPlayer::selectTrack(size_t trackIndex, bool select, int64_t timeUs) {
Chong Zhangdcb89b32013-08-06 09:44:47 -07001658 sp<AMessage> msg = new AMessage(kWhatSelectTrack, id());
1659 msg->setSize("trackIndex", trackIndex);
1660 msg->setInt32("select", select);
Robert Shih6ffb1fd2014-10-29 16:24:32 -07001661 msg->setInt64("timeUs", timeUs);
Chong Zhangdcb89b32013-08-06 09:44:47 -07001662
1663 sp<AMessage> response;
1664 status_t err = msg->postAndAwaitResponse(&response);
1665
Chong Zhang404fced2014-06-11 14:45:31 -07001666 if (err != OK) {
1667 return err;
1668 }
1669
1670 if (!response->findInt32("err", &err)) {
1671 err = OK;
1672 }
1673
Chong Zhangdcb89b32013-08-06 09:44:47 -07001674 return err;
1675}
1676
Ronghua Wua73d9e02014-10-08 15:13:29 -07001677status_t NuPlayer::getCurrentPosition(int64_t *mediaUs) {
1678 sp<Renderer> renderer = mRenderer;
1679 if (renderer == NULL) {
1680 return NO_INIT;
1681 }
1682
1683 return renderer->getCurrentPosition(mediaUs);
1684}
1685
1686void NuPlayer::getStats(int64_t *numFramesTotal, int64_t *numFramesDropped) {
1687 *numFramesTotal = mNumFramesTotal;
1688 *numFramesDropped = mNumFramesDropped;
1689}
1690
Marco Nelissenf0b72b52014-09-16 15:43:44 -07001691sp<MetaData> NuPlayer::getFileMeta() {
1692 return mSource->getFileFormatMeta();
1693}
1694
Andreas Huberb7c8e912012-11-27 15:02:53 -08001695void NuPlayer::schedulePollDuration() {
1696 sp<AMessage> msg = new AMessage(kWhatPollDuration, id());
1697 msg->setInt32("generation", mPollDurationGeneration);
1698 msg->post();
1699}
1700
1701void NuPlayer::cancelPollDuration() {
1702 ++mPollDurationGeneration;
1703}
1704
Andreas Hubera1f8ab02012-11-30 10:53:22 -08001705void NuPlayer::processDeferredActions() {
1706 while (!mDeferredActions.empty()) {
1707 // We won't execute any deferred actions until we're no longer in
1708 // an intermediate state, i.e. one more more decoders are currently
1709 // flushing or shutting down.
1710
Andreas Hubera1f8ab02012-11-30 10:53:22 -08001711 if (mFlushingAudio != NONE || mFlushingVideo != NONE) {
1712 // We're currently flushing, postpone the reset until that's
1713 // completed.
1714
1715 ALOGV("postponing action mFlushingAudio=%d, mFlushingVideo=%d",
1716 mFlushingAudio, mFlushingVideo);
1717
1718 break;
1719 }
1720
1721 sp<Action> action = *mDeferredActions.begin();
1722 mDeferredActions.erase(mDeferredActions.begin());
1723
1724 action->execute(this);
1725 }
1726}
1727
Wei Jiae427abf2014-09-22 15:21:11 -07001728void NuPlayer::performSeek(int64_t seekTimeUs, bool needNotify) {
1729 ALOGV("performSeek seekTimeUs=%lld us (%.2f secs), needNotify(%d)",
Andreas Hubera1f8ab02012-11-30 10:53:22 -08001730 seekTimeUs,
Wei Jiae427abf2014-09-22 15:21:11 -07001731 seekTimeUs / 1E6,
1732 needNotify);
Andreas Hubera1f8ab02012-11-30 10:53:22 -08001733
Andy Hungadf34bf2014-09-03 18:22:22 -07001734 if (mSource == NULL) {
1735 // This happens when reset occurs right before the loop mode
1736 // asynchronously seeks to the start of the stream.
1737 LOG_ALWAYS_FATAL_IF(mAudioDecoder != NULL || mVideoDecoder != NULL,
1738 "mSource is NULL and decoders not NULL audio(%p) video(%p)",
1739 mAudioDecoder.get(), mVideoDecoder.get());
1740 return;
1741 }
Andreas Hubera1f8ab02012-11-30 10:53:22 -08001742 mSource->seekTo(seekTimeUs);
Robert Shihd3b0bbb2014-07-23 15:00:25 -07001743 ++mTimedTextGeneration;
Andreas Hubera1f8ab02012-11-30 10:53:22 -08001744
1745 if (mDriver != NULL) {
1746 sp<NuPlayerDriver> driver = mDriver.promote();
1747 if (driver != NULL) {
Wei Jiae427abf2014-09-22 15:21:11 -07001748 if (needNotify) {
1749 driver->notifySeekComplete();
1750 }
Andreas Hubera1f8ab02012-11-30 10:53:22 -08001751 }
1752 }
1753
1754 // everything's flushed, continue playback.
1755}
1756
Wei Jiafef808d2014-10-31 17:57:05 -07001757void NuPlayer::performDecoderFlush(FlushCommand audio, FlushCommand video) {
1758 ALOGV("performDecoderFlush audio=%d, video=%d", audio, video);
Andreas Hubera1f8ab02012-11-30 10:53:22 -08001759
Wei Jiafef808d2014-10-31 17:57:05 -07001760 if ((audio == FLUSH_CMD_NONE || mAudioDecoder == NULL)
1761 && (video == FLUSH_CMD_NONE || mVideoDecoder == NULL)) {
Andreas Hubera1f8ab02012-11-30 10:53:22 -08001762 return;
1763 }
1764
1765 mTimeDiscontinuityPending = true;
1766
Wei Jiafef808d2014-10-31 17:57:05 -07001767 if (audio != FLUSH_CMD_NONE && mAudioDecoder != NULL) {
1768 flushDecoder(true /* audio */, (audio == FLUSH_CMD_SHUTDOWN));
Andreas Hubera1f8ab02012-11-30 10:53:22 -08001769 }
1770
Wei Jiafef808d2014-10-31 17:57:05 -07001771 if (video != FLUSH_CMD_NONE && mVideoDecoder != NULL) {
1772 flushDecoder(false /* audio */, (video == FLUSH_CMD_SHUTDOWN));
Andreas Hubera1f8ab02012-11-30 10:53:22 -08001773 }
1774}
1775
1776void NuPlayer::performReset() {
1777 ALOGV("performReset");
1778
1779 CHECK(mAudioDecoder == NULL);
1780 CHECK(mVideoDecoder == NULL);
1781
1782 cancelPollDuration();
1783
1784 ++mScanSourcesGeneration;
1785 mScanSourcesPending = false;
1786
Lajos Molnar09524832014-07-17 14:29:51 -07001787 if (mRendererLooper != NULL) {
1788 if (mRenderer != NULL) {
1789 mRendererLooper->unregisterHandler(mRenderer->id());
1790 }
1791 mRendererLooper->stop();
1792 mRendererLooper.clear();
1793 }
Andreas Hubera1f8ab02012-11-30 10:53:22 -08001794 mRenderer.clear();
Wei Jia57568df2014-09-22 10:16:29 -07001795 ++mRendererGeneration;
Andreas Hubera1f8ab02012-11-30 10:53:22 -08001796
1797 if (mSource != NULL) {
1798 mSource->stop();
Andreas Huberb5f25f02013-02-05 10:14:26 -08001799
Andreas Hubera1f8ab02012-11-30 10:53:22 -08001800 mSource.clear();
1801 }
1802
1803 if (mDriver != NULL) {
1804 sp<NuPlayerDriver> driver = mDriver.promote();
1805 if (driver != NULL) {
1806 driver->notifyResetComplete();
1807 }
1808 }
Andreas Huber57a339c2012-12-03 11:18:00 -08001809
1810 mStarted = false;
Andreas Hubera1f8ab02012-11-30 10:53:22 -08001811}
1812
1813void NuPlayer::performScanSources() {
1814 ALOGV("performScanSources");
1815
Andreas Huber57a339c2012-12-03 11:18:00 -08001816 if (!mStarted) {
1817 return;
1818 }
1819
Andreas Hubera1f8ab02012-11-30 10:53:22 -08001820 if (mAudioDecoder == NULL || mVideoDecoder == NULL) {
1821 postScanSources();
1822 }
1823}
1824
Andreas Huber57a339c2012-12-03 11:18:00 -08001825void NuPlayer::performSetSurface(const sp<NativeWindowWrapper> &wrapper) {
1826 ALOGV("performSetSurface");
1827
1828 mNativeWindow = wrapper;
1829
1830 // XXX - ignore error from setVideoScalingMode for now
1831 setVideoScalingMode(mVideoScalingMode);
Chong Zhang13d6faa2014-08-22 15:35:28 -07001832
1833 if (mDriver != NULL) {
1834 sp<NuPlayerDriver> driver = mDriver.promote();
1835 if (driver != NULL) {
1836 driver->notifySetSurfaceComplete();
1837 }
1838 }
Andreas Huber57a339c2012-12-03 11:18:00 -08001839}
1840
Andreas Huber9575c962013-02-05 13:59:56 -08001841void NuPlayer::onSourceNotify(const sp<AMessage> &msg) {
1842 int32_t what;
1843 CHECK(msg->findInt32("what", &what));
1844
1845 switch (what) {
1846 case Source::kWhatPrepared:
1847 {
Andreas Huberb5f28d42013-04-25 15:11:19 -07001848 if (mSource == NULL) {
1849 // This is a stale notification from a source that was
1850 // asynchronously preparing when the client called reset().
1851 // We handled the reset, the source is gone.
1852 break;
1853 }
1854
Andreas Huberec0c5972013-02-05 14:47:13 -08001855 int32_t err;
1856 CHECK(msg->findInt32("err", &err));
1857
Andreas Huber9575c962013-02-05 13:59:56 -08001858 sp<NuPlayerDriver> driver = mDriver.promote();
1859 if (driver != NULL) {
Marco Nelissendd114d12014-05-28 15:23:14 -07001860 // notify duration first, so that it's definitely set when
1861 // the app received the "prepare complete" callback.
1862 int64_t durationUs;
1863 if (mSource->getDuration(&durationUs) == OK) {
1864 driver->notifyDuration(durationUs);
1865 }
Andreas Huberec0c5972013-02-05 14:47:13 -08001866 driver->notifyPrepareCompleted(err);
Andreas Huber9575c962013-02-05 13:59:56 -08001867 }
Andreas Huber99759402013-04-01 14:28:31 -07001868
Andreas Huber9575c962013-02-05 13:59:56 -08001869 break;
1870 }
1871
1872 case Source::kWhatFlagsChanged:
1873 {
1874 uint32_t flags;
1875 CHECK(msg->findInt32("flags", (int32_t *)&flags));
1876
Chong Zhang4b7069d2013-09-11 12:52:43 -07001877 sp<NuPlayerDriver> driver = mDriver.promote();
1878 if (driver != NULL) {
1879 driver->notifyFlagsChanged(flags);
1880 }
1881
Andreas Huber9575c962013-02-05 13:59:56 -08001882 if ((mSourceFlags & Source::FLAG_DYNAMIC_DURATION)
1883 && (!(flags & Source::FLAG_DYNAMIC_DURATION))) {
1884 cancelPollDuration();
1885 } else if (!(mSourceFlags & Source::FLAG_DYNAMIC_DURATION)
1886 && (flags & Source::FLAG_DYNAMIC_DURATION)
1887 && (mAudioDecoder != NULL || mVideoDecoder != NULL)) {
1888 schedulePollDuration();
1889 }
1890
1891 mSourceFlags = flags;
1892 break;
1893 }
1894
1895 case Source::kWhatVideoSizeChanged:
1896 {
Chong Zhangced1c2f2014-08-08 15:22:35 -07001897 sp<AMessage> format;
1898 CHECK(msg->findMessage("format", &format));
Andreas Huber9575c962013-02-05 13:59:56 -08001899
Chong Zhangced1c2f2014-08-08 15:22:35 -07001900 updateVideoSize(format);
Andreas Huber9575c962013-02-05 13:59:56 -08001901 break;
1902 }
1903
Chong Zhang2a3cc9a2014-08-21 17:48:26 -07001904 case Source::kWhatBufferingUpdate:
1905 {
1906 int32_t percentage;
1907 CHECK(msg->findInt32("percentage", &percentage));
1908
1909 notifyListener(MEDIA_BUFFERING_UPDATE, percentage, 0);
1910 break;
1911 }
1912
Roger Jönssonb50e83e2013-01-21 16:26:41 +01001913 case Source::kWhatBufferingStart:
1914 {
1915 notifyListener(MEDIA_INFO, MEDIA_INFO_BUFFERING_START, 0);
1916 break;
1917 }
1918
1919 case Source::kWhatBufferingEnd:
1920 {
1921 notifyListener(MEDIA_INFO, MEDIA_INFO_BUFFERING_END, 0);
1922 break;
1923 }
1924
Chong Zhangdcb89b32013-08-06 09:44:47 -07001925 case Source::kWhatSubtitleData:
1926 {
1927 sp<ABuffer> buffer;
1928 CHECK(msg->findBuffer("buffer", &buffer));
1929
Chong Zhang404fced2014-06-11 14:45:31 -07001930 sendSubtitleData(buffer, 0 /* baseIndex */);
Chong Zhangdcb89b32013-08-06 09:44:47 -07001931 break;
1932 }
1933
Robert Shihd3b0bbb2014-07-23 15:00:25 -07001934 case Source::kWhatTimedTextData:
1935 {
1936 int32_t generation;
1937 if (msg->findInt32("generation", &generation)
1938 && generation != mTimedTextGeneration) {
1939 break;
1940 }
1941
1942 sp<ABuffer> buffer;
1943 CHECK(msg->findBuffer("buffer", &buffer));
1944
1945 sp<NuPlayerDriver> driver = mDriver.promote();
1946 if (driver == NULL) {
1947 break;
1948 }
1949
1950 int posMs;
1951 int64_t timeUs, posUs;
1952 driver->getCurrentPosition(&posMs);
1953 posUs = posMs * 1000;
1954 CHECK(buffer->meta()->findInt64("timeUs", &timeUs));
1955
1956 if (posUs < timeUs) {
1957 if (!msg->findInt32("generation", &generation)) {
1958 msg->setInt32("generation", mTimedTextGeneration);
1959 }
1960 msg->post(timeUs - posUs);
1961 } else {
1962 sendTimedTextData(buffer);
1963 }
1964 break;
1965 }
1966
Andreas Huber14f76722013-01-15 09:04:18 -08001967 case Source::kWhatQueueDecoderShutdown:
1968 {
1969 int32_t audio, video;
1970 CHECK(msg->findInt32("audio", &audio));
1971 CHECK(msg->findInt32("video", &video));
1972
1973 sp<AMessage> reply;
1974 CHECK(msg->findMessage("reply", &reply));
1975
1976 queueDecoderShutdown(audio, video, reply);
1977 break;
1978 }
1979
Ronghua Wu80276872014-08-28 15:50:29 -07001980 case Source::kWhatDrmNoLicense:
1981 {
1982 notifyListener(MEDIA_ERROR, MEDIA_ERROR_UNKNOWN, ERROR_DRM_NO_LICENSE);
1983 break;
1984 }
1985
Andreas Huber9575c962013-02-05 13:59:56 -08001986 default:
1987 TRESPASS();
1988 }
1989}
1990
Chong Zhanga7fa1d92014-06-11 14:49:23 -07001991void NuPlayer::onClosedCaptionNotify(const sp<AMessage> &msg) {
1992 int32_t what;
1993 CHECK(msg->findInt32("what", &what));
1994
1995 switch (what) {
1996 case NuPlayer::CCDecoder::kWhatClosedCaptionData:
1997 {
1998 sp<ABuffer> buffer;
1999 CHECK(msg->findBuffer("buffer", &buffer));
2000
2001 size_t inbandTracks = 0;
2002 if (mSource != NULL) {
2003 inbandTracks = mSource->getTrackCount();
2004 }
2005
2006 sendSubtitleData(buffer, inbandTracks);
2007 break;
2008 }
2009
2010 case NuPlayer::CCDecoder::kWhatTrackAdded:
2011 {
2012 notifyListener(MEDIA_INFO, MEDIA_INFO_METADATA_UPDATE, 0);
2013
2014 break;
2015 }
2016
2017 default:
2018 TRESPASS();
2019 }
2020
2021
2022}
2023
Chong Zhang404fced2014-06-11 14:45:31 -07002024void NuPlayer::sendSubtitleData(const sp<ABuffer> &buffer, int32_t baseIndex) {
2025 int32_t trackIndex;
2026 int64_t timeUs, durationUs;
2027 CHECK(buffer->meta()->findInt32("trackIndex", &trackIndex));
2028 CHECK(buffer->meta()->findInt64("timeUs", &timeUs));
2029 CHECK(buffer->meta()->findInt64("durationUs", &durationUs));
2030
2031 Parcel in;
2032 in.writeInt32(trackIndex + baseIndex);
2033 in.writeInt64(timeUs);
2034 in.writeInt64(durationUs);
2035 in.writeInt32(buffer->size());
2036 in.writeInt32(buffer->size());
2037 in.write(buffer->data(), buffer->size());
2038
2039 notifyListener(MEDIA_SUBTITLE_DATA, 0, 0, &in);
2040}
Robert Shihd3b0bbb2014-07-23 15:00:25 -07002041
2042void NuPlayer::sendTimedTextData(const sp<ABuffer> &buffer) {
2043 const void *data;
2044 size_t size = 0;
2045 int64_t timeUs;
2046 int32_t flag = TextDescriptions::LOCAL_DESCRIPTIONS;
2047
2048 AString mime;
2049 CHECK(buffer->meta()->findString("mime", &mime));
2050 CHECK(strcasecmp(mime.c_str(), MEDIA_MIMETYPE_TEXT_3GPP) == 0);
2051
2052 data = buffer->data();
2053 size = buffer->size();
2054
2055 Parcel parcel;
2056 if (size > 0) {
2057 CHECK(buffer->meta()->findInt64("timeUs", &timeUs));
2058 flag |= TextDescriptions::IN_BAND_TEXT_3GPP;
2059 TextDescriptions::getParcelOfDescriptions(
2060 (const uint8_t *)data, size, flag, timeUs / 1000, &parcel);
2061 }
2062
2063 if ((parcel.dataSize() > 0)) {
2064 notifyListener(MEDIA_TIMED_TEXT, 0, 0, &parcel);
2065 } else { // send an empty timed text
2066 notifyListener(MEDIA_TIMED_TEXT, 0, 0);
2067 }
2068}
Andreas Huberb5f25f02013-02-05 10:14:26 -08002069////////////////////////////////////////////////////////////////////////////////
2070
Chong Zhangced1c2f2014-08-08 15:22:35 -07002071sp<AMessage> NuPlayer::Source::getFormat(bool audio) {
2072 sp<MetaData> meta = getFormatMeta(audio);
2073
2074 if (meta == NULL) {
2075 return NULL;
2076 }
2077
2078 sp<AMessage> msg = new AMessage;
2079
2080 if(convertMetaDataToMessage(meta, &msg) == OK) {
2081 return msg;
2082 }
2083 return NULL;
2084}
2085
Andreas Huber9575c962013-02-05 13:59:56 -08002086void NuPlayer::Source::notifyFlagsChanged(uint32_t flags) {
2087 sp<AMessage> notify = dupNotify();
2088 notify->setInt32("what", kWhatFlagsChanged);
2089 notify->setInt32("flags", flags);
2090 notify->post();
2091}
2092
Chong Zhangced1c2f2014-08-08 15:22:35 -07002093void NuPlayer::Source::notifyVideoSizeChanged(const sp<AMessage> &format) {
Andreas Huber9575c962013-02-05 13:59:56 -08002094 sp<AMessage> notify = dupNotify();
2095 notify->setInt32("what", kWhatVideoSizeChanged);
Chong Zhangced1c2f2014-08-08 15:22:35 -07002096 notify->setMessage("format", format);
Andreas Huber9575c962013-02-05 13:59:56 -08002097 notify->post();
2098}
2099
Andreas Huberec0c5972013-02-05 14:47:13 -08002100void NuPlayer::Source::notifyPrepared(status_t err) {
Andreas Huber9575c962013-02-05 13:59:56 -08002101 sp<AMessage> notify = dupNotify();
2102 notify->setInt32("what", kWhatPrepared);
Andreas Huberec0c5972013-02-05 14:47:13 -08002103 notify->setInt32("err", err);
Andreas Huber9575c962013-02-05 13:59:56 -08002104 notify->post();
2105}
2106
Andreas Huber84333e02014-02-07 15:36:10 -08002107void NuPlayer::Source::onMessageReceived(const sp<AMessage> & /* msg */) {
Andreas Huberb5f25f02013-02-05 10:14:26 -08002108 TRESPASS();
2109}
2110
Andreas Huberf9334412010-12-15 15:17:42 -08002111} // namespace android