blob: 42977ca6925786d0a69b64a4b99eda4cd076617e [file] [log] [blame]
Andreas Huberf9334412010-12-15 15:17:42 -08001/*
2 * Copyright (C) 2010 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17//#define LOG_NDEBUG 0
18#define LOG_TAG "NuPlayer"
19#include <utils/Log.h>
20
21#include "NuPlayer.h"
Andreas Huber5bc087c2010-12-23 10:27:40 -080022
23#include "HTTPLiveSource.h"
Andreas Huberf9334412010-12-15 15:17:42 -080024#include "NuPlayerDecoder.h"
Wei Jiabc2fb722014-07-08 16:37:57 -070025#include "NuPlayerDecoderPassThrough.h"
Andreas Huber43c3e6c2011-01-05 12:17:08 -080026#include "NuPlayerDriver.h"
Andreas Huberf9334412010-12-15 15:17:42 -080027#include "NuPlayerRenderer.h"
Andreas Huber5bc087c2010-12-23 10:27:40 -080028#include "NuPlayerSource.h"
Andreas Huber2bfdd422011-10-11 15:24:07 -070029#include "RTSPSource.h"
Andreas Huber5bc087c2010-12-23 10:27:40 -080030#include "StreamingSource.h"
Andreas Huberafed0e12011-09-20 15:39:58 -070031#include "GenericSource.h"
Robert Shihd3b0bbb2014-07-23 15:00:25 -070032#include "TextDescriptions.h"
Andreas Huber5bc087c2010-12-23 10:27:40 -080033
34#include "ATSParser.h"
Andreas Huberf9334412010-12-15 15:17:42 -080035
Andreas Huber3831a062010-12-21 10:22:33 -080036#include <media/stagefright/foundation/hexdump.h>
Andreas Huberf9334412010-12-15 15:17:42 -080037#include <media/stagefright/foundation/ABuffer.h>
38#include <media/stagefright/foundation/ADebug.h>
39#include <media/stagefright/foundation/AMessage.h>
Lajos Molnar09524832014-07-17 14:29:51 -070040#include <media/stagefright/MediaBuffer.h>
Andreas Huber3fe62152011-09-16 15:09:22 -070041#include <media/stagefright/MediaDefs.h>
Andreas Huberf9334412010-12-15 15:17:42 -080042#include <media/stagefright/MediaErrors.h>
43#include <media/stagefright/MetaData.h>
Andy McFadden8ba01022012-12-18 09:46:54 -080044#include <gui/IGraphicBufferProducer.h>
Andreas Huberf9334412010-12-15 15:17:42 -080045
Andreas Huber3fe62152011-09-16 15:09:22 -070046#include "avc_utils.h"
47
Andreas Huber84066782011-08-16 09:34:26 -070048#include "ESDS.h"
49#include <media/stagefright/Utils.h>
50
Andreas Huberf9334412010-12-15 15:17:42 -080051namespace android {
52
Phil Burkc5cc2e22014-09-09 20:08:39 -070053// TODO optimize buffer size for power consumption
54// The offload read buffer size is 32 KB but 24 KB uses less power.
55const size_t NuPlayer::kAggregateBufferSizeBytes = 24 * 1024;
56
Andreas Hubera1f8ab02012-11-30 10:53:22 -080057struct NuPlayer::Action : public RefBase {
58 Action() {}
59
60 virtual void execute(NuPlayer *player) = 0;
61
62private:
63 DISALLOW_EVIL_CONSTRUCTORS(Action);
64};
65
66struct NuPlayer::SeekAction : public Action {
Wei Jiae427abf2014-09-22 15:21:11 -070067 SeekAction(int64_t seekTimeUs, bool needNotify)
68 : mSeekTimeUs(seekTimeUs),
69 mNeedNotify(needNotify) {
Andreas Hubera1f8ab02012-11-30 10:53:22 -080070 }
71
72 virtual void execute(NuPlayer *player) {
Wei Jiae427abf2014-09-22 15:21:11 -070073 player->performSeek(mSeekTimeUs, mNeedNotify);
Andreas Hubera1f8ab02012-11-30 10:53:22 -080074 }
75
76private:
77 int64_t mSeekTimeUs;
Wei Jiae427abf2014-09-22 15:21:11 -070078 bool mNeedNotify;
Andreas Hubera1f8ab02012-11-30 10:53:22 -080079
80 DISALLOW_EVIL_CONSTRUCTORS(SeekAction);
81};
82
Andreas Huber57a339c2012-12-03 11:18:00 -080083struct NuPlayer::SetSurfaceAction : public Action {
84 SetSurfaceAction(const sp<NativeWindowWrapper> &wrapper)
85 : mWrapper(wrapper) {
86 }
87
88 virtual void execute(NuPlayer *player) {
89 player->performSetSurface(mWrapper);
90 }
91
92private:
93 sp<NativeWindowWrapper> mWrapper;
94
95 DISALLOW_EVIL_CONSTRUCTORS(SetSurfaceAction);
96};
97
Andreas Huber14f76722013-01-15 09:04:18 -080098struct NuPlayer::ShutdownDecoderAction : public Action {
99 ShutdownDecoderAction(bool audio, bool video)
100 : mAudio(audio),
101 mVideo(video) {
102 }
103
104 virtual void execute(NuPlayer *player) {
105 player->performDecoderShutdown(mAudio, mVideo);
106 }
107
108private:
109 bool mAudio;
110 bool mVideo;
111
112 DISALLOW_EVIL_CONSTRUCTORS(ShutdownDecoderAction);
113};
114
115struct NuPlayer::PostMessageAction : public Action {
116 PostMessageAction(const sp<AMessage> &msg)
117 : mMessage(msg) {
118 }
119
120 virtual void execute(NuPlayer *) {
121 mMessage->post();
122 }
123
124private:
125 sp<AMessage> mMessage;
126
127 DISALLOW_EVIL_CONSTRUCTORS(PostMessageAction);
128};
129
Andreas Hubera1f8ab02012-11-30 10:53:22 -0800130// Use this if there's no state necessary to save in order to execute
131// the action.
132struct NuPlayer::SimpleAction : public Action {
133 typedef void (NuPlayer::*ActionFunc)();
134
135 SimpleAction(ActionFunc func)
136 : mFunc(func) {
137 }
138
139 virtual void execute(NuPlayer *player) {
140 (player->*mFunc)();
141 }
142
143private:
144 ActionFunc mFunc;
145
146 DISALLOW_EVIL_CONSTRUCTORS(SimpleAction);
147};
148
Andreas Huberf9334412010-12-15 15:17:42 -0800149////////////////////////////////////////////////////////////////////////////////
150
151NuPlayer::NuPlayer()
Andreas Huber9b80c2b2011-06-30 15:47:02 -0700152 : mUIDValid(false),
Andreas Huber9575c962013-02-05 13:59:56 -0800153 mSourceFlags(0),
Andreas Huber3fe62152011-09-16 15:09:22 -0700154 mVideoIsAVC(false),
Wei Jiabc2fb722014-07-08 16:37:57 -0700155 mOffloadAudio(false),
Wei Jia88703c32014-08-06 11:24:07 -0700156 mAudioDecoderGeneration(0),
157 mVideoDecoderGeneration(0),
Wei Jia57568df2014-09-22 10:16:29 -0700158 mRendererGeneration(0),
Andreas Huber9b80c2b2011-06-30 15:47:02 -0700159 mAudioEOS(false),
Andreas Huberf9334412010-12-15 15:17:42 -0800160 mVideoEOS(false),
Andreas Huber5bc087c2010-12-23 10:27:40 -0800161 mScanSourcesPending(false),
Andreas Huber1aef2112011-01-04 14:01:29 -0800162 mScanSourcesGeneration(0),
Andreas Huberb7c8e912012-11-27 15:02:53 -0800163 mPollDurationGeneration(0),
Robert Shihd3b0bbb2014-07-23 15:00:25 -0700164 mTimedTextGeneration(0),
Andreas Huber6e3d3112011-11-28 12:36:11 -0800165 mTimeDiscontinuityPending(false),
Andreas Huberf9334412010-12-15 15:17:42 -0800166 mFlushingAudio(NONE),
Andreas Huber1aef2112011-01-04 14:01:29 -0800167 mFlushingVideo(NONE),
Andreas Huber3fe62152011-09-16 15:09:22 -0700168 mSkipRenderingAudioUntilMediaTimeUs(-1ll),
169 mSkipRenderingVideoUntilMediaTimeUs(-1ll),
Andreas Huber3fe62152011-09-16 15:09:22 -0700170 mNumFramesTotal(0ll),
James Dong0d268a32012-08-31 12:18:27 -0700171 mNumFramesDropped(0ll),
Andreas Huber57a339c2012-12-03 11:18:00 -0800172 mVideoScalingMode(NATIVE_WINDOW_SCALING_MODE_SCALE_TO_WINDOW),
173 mStarted(false) {
Andy Hung8d121d42014-10-03 09:53:53 -0700174 clearFlushComplete();
Andreas Huberf9334412010-12-15 15:17:42 -0800175}
176
177NuPlayer::~NuPlayer() {
178}
179
Andreas Huber9b80c2b2011-06-30 15:47:02 -0700180void NuPlayer::setUID(uid_t uid) {
181 mUIDValid = true;
182 mUID = uid;
183}
184
Andreas Huber43c3e6c2011-01-05 12:17:08 -0800185void NuPlayer::setDriver(const wp<NuPlayerDriver> &driver) {
186 mDriver = driver;
Andreas Huberf9334412010-12-15 15:17:42 -0800187}
188
Andreas Huber9575c962013-02-05 13:59:56 -0800189void NuPlayer::setDataSourceAsync(const sp<IStreamSource> &source) {
Andreas Huberf9334412010-12-15 15:17:42 -0800190 sp<AMessage> msg = new AMessage(kWhatSetDataSource, id());
191
Andreas Huberb5f25f02013-02-05 10:14:26 -0800192 sp<AMessage> notify = new AMessage(kWhatSourceNotify, id());
193
Andreas Huber240abcc2014-02-13 13:32:37 -0800194 msg->setObject("source", new StreamingSource(notify, source));
Andreas Huber5bc087c2010-12-23 10:27:40 -0800195 msg->post();
196}
Andreas Huberf9334412010-12-15 15:17:42 -0800197
Andreas Huberafed0e12011-09-20 15:39:58 -0700198static bool IsHTTPLiveURL(const char *url) {
199 if (!strncasecmp("http://", url, 7)
Andreas Huber99759402013-04-01 14:28:31 -0700200 || !strncasecmp("https://", url, 8)
201 || !strncasecmp("file://", url, 7)) {
Andreas Huberafed0e12011-09-20 15:39:58 -0700202 size_t len = strlen(url);
203 if (len >= 5 && !strcasecmp(".m3u8", &url[len - 5])) {
204 return true;
205 }
206
207 if (strstr(url,"m3u8")) {
208 return true;
209 }
210 }
211
212 return false;
213}
214
Andreas Huber9575c962013-02-05 13:59:56 -0800215void NuPlayer::setDataSourceAsync(
Andreas Huber1b86fe02014-01-29 11:13:26 -0800216 const sp<IMediaHTTPService> &httpService,
217 const char *url,
218 const KeyedVector<String8, String8> *headers) {
Chong Zhang3de157d2014-08-05 20:54:44 -0700219
Andreas Huber5bc087c2010-12-23 10:27:40 -0800220 sp<AMessage> msg = new AMessage(kWhatSetDataSource, id());
Oscar Rydhé7a33b772012-02-20 10:15:48 +0100221 size_t len = strlen(url);
Andreas Huber5bc087c2010-12-23 10:27:40 -0800222
Andreas Huberb5f25f02013-02-05 10:14:26 -0800223 sp<AMessage> notify = new AMessage(kWhatSourceNotify, id());
224
Andreas Huberafed0e12011-09-20 15:39:58 -0700225 sp<Source> source;
226 if (IsHTTPLiveURL(url)) {
Andreas Huber81e68442014-02-05 11:52:33 -0800227 source = new HTTPLiveSource(notify, httpService, url, headers);
Andreas Huberafed0e12011-09-20 15:39:58 -0700228 } else if (!strncasecmp(url, "rtsp://", 7)) {
Andreas Huber1b86fe02014-01-29 11:13:26 -0800229 source = new RTSPSource(
230 notify, httpService, url, headers, mUIDValid, mUID);
Oscar Rydhé7a33b772012-02-20 10:15:48 +0100231 } else if ((!strncasecmp(url, "http://", 7)
232 || !strncasecmp(url, "https://", 8))
233 && ((len >= 4 && !strcasecmp(".sdp", &url[len - 4]))
234 || strstr(url, ".sdp?"))) {
Andreas Huber1b86fe02014-01-29 11:13:26 -0800235 source = new RTSPSource(
236 notify, httpService, url, headers, mUIDValid, mUID, true);
Andreas Huber2bfdd422011-10-11 15:24:07 -0700237 } else {
Chong Zhang3de157d2014-08-05 20:54:44 -0700238 sp<GenericSource> genericSource =
239 new GenericSource(notify, mUIDValid, mUID);
240 // Don't set FLAG_SECURE on mSourceFlags here for widevine.
241 // The correct flags will be updated in Source::kWhatFlagsChanged
242 // handler when GenericSource is prepared.
Andreas Huber2bfdd422011-10-11 15:24:07 -0700243
Chong Zhanga19f33e2014-08-07 15:35:07 -0700244 status_t err = genericSource->setDataSource(httpService, url, headers);
Chong Zhang3de157d2014-08-05 20:54:44 -0700245
246 if (err == OK) {
247 source = genericSource;
248 } else {
Chong Zhanga19f33e2014-08-07 15:35:07 -0700249 ALOGE("Failed to set data source!");
Chong Zhang3de157d2014-08-05 20:54:44 -0700250 }
251 }
Andreas Huberafed0e12011-09-20 15:39:58 -0700252 msg->setObject("source", source);
253 msg->post();
254}
255
Andreas Huber9575c962013-02-05 13:59:56 -0800256void NuPlayer::setDataSourceAsync(int fd, int64_t offset, int64_t length) {
Andreas Huberafed0e12011-09-20 15:39:58 -0700257 sp<AMessage> msg = new AMessage(kWhatSetDataSource, id());
258
Andreas Huberb5f25f02013-02-05 10:14:26 -0800259 sp<AMessage> notify = new AMessage(kWhatSourceNotify, id());
260
Chong Zhang3de157d2014-08-05 20:54:44 -0700261 sp<GenericSource> source =
262 new GenericSource(notify, mUIDValid, mUID);
263
Chong Zhanga19f33e2014-08-07 15:35:07 -0700264 status_t err = source->setDataSource(fd, offset, length);
Chong Zhang3de157d2014-08-05 20:54:44 -0700265
266 if (err != OK) {
Chong Zhanga19f33e2014-08-07 15:35:07 -0700267 ALOGE("Failed to set data source!");
Chong Zhang3de157d2014-08-05 20:54:44 -0700268 source = NULL;
269 }
270
Andreas Huberafed0e12011-09-20 15:39:58 -0700271 msg->setObject("source", source);
Andreas Huberf9334412010-12-15 15:17:42 -0800272 msg->post();
273}
274
Andreas Huber9575c962013-02-05 13:59:56 -0800275void NuPlayer::prepareAsync() {
276 (new AMessage(kWhatPrepare, id()))->post();
277}
278
Andreas Huber57a339c2012-12-03 11:18:00 -0800279void NuPlayer::setVideoSurfaceTextureAsync(
Andy McFadden8ba01022012-12-18 09:46:54 -0800280 const sp<IGraphicBufferProducer> &bufferProducer) {
Glenn Kasten11731182011-02-08 17:26:17 -0800281 sp<AMessage> msg = new AMessage(kWhatSetVideoNativeWindow, id());
Andreas Huber57a339c2012-12-03 11:18:00 -0800282
Andy McFadden8ba01022012-12-18 09:46:54 -0800283 if (bufferProducer == NULL) {
Andreas Huber57a339c2012-12-03 11:18:00 -0800284 msg->setObject("native-window", NULL);
285 } else {
286 msg->setObject(
287 "native-window",
288 new NativeWindowWrapper(
Wei Jia9c03a402014-08-26 15:24:43 -0700289 new Surface(bufferProducer, true /* controlledByApp */)));
Andreas Huber57a339c2012-12-03 11:18:00 -0800290 }
291
Andreas Huberf9334412010-12-15 15:17:42 -0800292 msg->post();
293}
294
295void NuPlayer::setAudioSink(const sp<MediaPlayerBase::AudioSink> &sink) {
296 sp<AMessage> msg = new AMessage(kWhatSetAudioSink, id());
297 msg->setObject("sink", sink);
298 msg->post();
299}
300
301void NuPlayer::start() {
302 (new AMessage(kWhatStart, id()))->post();
303}
304
Andreas Huber43c3e6c2011-01-05 12:17:08 -0800305void NuPlayer::pause() {
Andreas Huberb4082222011-01-20 15:23:04 -0800306 (new AMessage(kWhatPause, id()))->post();
Andreas Huber43c3e6c2011-01-05 12:17:08 -0800307}
308
309void NuPlayer::resume() {
Andreas Huberb4082222011-01-20 15:23:04 -0800310 (new AMessage(kWhatResume, id()))->post();
Andreas Huber43c3e6c2011-01-05 12:17:08 -0800311}
312
Andreas Huber1aef2112011-01-04 14:01:29 -0800313void NuPlayer::resetAsync() {
Chong Zhang48296b72014-09-14 14:28:45 -0700314 if (mSource != NULL) {
315 // During a reset, the data source might be unresponsive already, we need to
316 // disconnect explicitly so that reads exit promptly.
317 // We can't queue the disconnect request to the looper, as it might be
318 // queued behind a stuck read and never gets processed.
319 // Doing a disconnect outside the looper to allows the pending reads to exit
320 // (either successfully or with error).
321 mSource->disconnect();
322 }
323
Andreas Huber1aef2112011-01-04 14:01:29 -0800324 (new AMessage(kWhatReset, id()))->post();
325}
326
Wei Jiae427abf2014-09-22 15:21:11 -0700327void NuPlayer::seekToAsync(int64_t seekTimeUs, bool needNotify) {
Andreas Huber43c3e6c2011-01-05 12:17:08 -0800328 sp<AMessage> msg = new AMessage(kWhatSeek, id());
329 msg->setInt64("seekTimeUs", seekTimeUs);
Wei Jiae427abf2014-09-22 15:21:11 -0700330 msg->setInt32("needNotify", needNotify);
Andreas Huber43c3e6c2011-01-05 12:17:08 -0800331 msg->post();
332}
333
Andreas Huber53df1a42010-12-22 10:03:04 -0800334
Chong Zhang404fced2014-06-11 14:45:31 -0700335void NuPlayer::writeTrackInfo(
336 Parcel* reply, const sp<AMessage> format) const {
337 int32_t trackType;
338 CHECK(format->findInt32("type", &trackType));
339
340 AString lang;
341 CHECK(format->findString("language", &lang));
342
343 reply->writeInt32(2); // write something non-zero
344 reply->writeInt32(trackType);
345 reply->writeString16(String16(lang.c_str()));
346
347 if (trackType == MEDIA_TRACK_TYPE_SUBTITLE) {
348 AString mime;
349 CHECK(format->findString("mime", &mime));
350
351 int32_t isAuto, isDefault, isForced;
352 CHECK(format->findInt32("auto", &isAuto));
353 CHECK(format->findInt32("default", &isDefault));
354 CHECK(format->findInt32("forced", &isForced));
355
356 reply->writeString16(String16(mime.c_str()));
357 reply->writeInt32(isAuto);
358 reply->writeInt32(isDefault);
359 reply->writeInt32(isForced);
360 }
361}
362
Andreas Huberf9334412010-12-15 15:17:42 -0800363void NuPlayer::onMessageReceived(const sp<AMessage> &msg) {
364 switch (msg->what()) {
365 case kWhatSetDataSource:
366 {
Steve Block3856b092011-10-20 11:56:00 +0100367 ALOGV("kWhatSetDataSource");
Andreas Huberf9334412010-12-15 15:17:42 -0800368
369 CHECK(mSource == NULL);
370
Chong Zhang3de157d2014-08-05 20:54:44 -0700371 status_t err = OK;
Andreas Huber5bc087c2010-12-23 10:27:40 -0800372 sp<RefBase> obj;
373 CHECK(msg->findObject("source", &obj));
Chong Zhang3de157d2014-08-05 20:54:44 -0700374 if (obj != NULL) {
375 mSource = static_cast<Source *>(obj.get());
Chong Zhang3de157d2014-08-05 20:54:44 -0700376 } else {
377 err = UNKNOWN_ERROR;
378 }
Andreas Huber9575c962013-02-05 13:59:56 -0800379
380 CHECK(mDriver != NULL);
381 sp<NuPlayerDriver> driver = mDriver.promote();
382 if (driver != NULL) {
Chong Zhang3de157d2014-08-05 20:54:44 -0700383 driver->notifySetDataSourceCompleted(err);
Andreas Huber9575c962013-02-05 13:59:56 -0800384 }
385 break;
386 }
387
388 case kWhatPrepare:
389 {
390 mSource->prepareAsync();
Andreas Huberf9334412010-12-15 15:17:42 -0800391 break;
392 }
393
Chong Zhangdcb89b32013-08-06 09:44:47 -0700394 case kWhatGetTrackInfo:
395 {
396 uint32_t replyID;
397 CHECK(msg->senderAwaitsResponse(&replyID));
398
Chong Zhang404fced2014-06-11 14:45:31 -0700399 Parcel* reply;
400 CHECK(msg->findPointer("reply", (void**)&reply));
401
402 size_t inbandTracks = 0;
Chong Zhangdcb89b32013-08-06 09:44:47 -0700403 if (mSource != NULL) {
Chong Zhang404fced2014-06-11 14:45:31 -0700404 inbandTracks = mSource->getTrackCount();
405 }
406
Chong Zhanga7fa1d92014-06-11 14:49:23 -0700407 size_t ccTracks = 0;
408 if (mCCDecoder != NULL) {
409 ccTracks = mCCDecoder->getTrackCount();
410 }
411
Chong Zhang404fced2014-06-11 14:45:31 -0700412 // total track count
Chong Zhanga7fa1d92014-06-11 14:49:23 -0700413 reply->writeInt32(inbandTracks + ccTracks);
Chong Zhang404fced2014-06-11 14:45:31 -0700414
415 // write inband tracks
416 for (size_t i = 0; i < inbandTracks; ++i) {
417 writeTrackInfo(reply, mSource->getTrackInfo(i));
Chong Zhangdcb89b32013-08-06 09:44:47 -0700418 }
419
Chong Zhanga7fa1d92014-06-11 14:49:23 -0700420 // write CC track
421 for (size_t i = 0; i < ccTracks; ++i) {
422 writeTrackInfo(reply, mCCDecoder->getTrackInfo(i));
423 }
424
Chong Zhangdcb89b32013-08-06 09:44:47 -0700425 sp<AMessage> response = new AMessage;
Chong Zhangdcb89b32013-08-06 09:44:47 -0700426 response->postReply(replyID);
427 break;
428 }
429
Robert Shih7c4f0d72014-07-09 18:53:31 -0700430 case kWhatGetSelectedTrack:
431 {
432 status_t err = INVALID_OPERATION;
433 if (mSource != NULL) {
434 err = OK;
435
436 int32_t type32;
437 CHECK(msg->findInt32("type", (int32_t*)&type32));
438 media_track_type type = (media_track_type)type32;
439 ssize_t selectedTrack = mSource->getSelectedTrack(type);
440
441 Parcel* reply;
442 CHECK(msg->findPointer("reply", (void**)&reply));
443 reply->writeInt32(selectedTrack);
444 }
445
446 sp<AMessage> response = new AMessage;
447 response->setInt32("err", err);
448
449 uint32_t replyID;
450 CHECK(msg->senderAwaitsResponse(&replyID));
451 response->postReply(replyID);
452 break;
453 }
454
Chong Zhangdcb89b32013-08-06 09:44:47 -0700455 case kWhatSelectTrack:
456 {
457 uint32_t replyID;
458 CHECK(msg->senderAwaitsResponse(&replyID));
459
Chong Zhang404fced2014-06-11 14:45:31 -0700460 size_t trackIndex;
461 int32_t select;
462 CHECK(msg->findSize("trackIndex", &trackIndex));
463 CHECK(msg->findInt32("select", &select));
464
Chong Zhangdcb89b32013-08-06 09:44:47 -0700465 status_t err = INVALID_OPERATION;
Chong Zhang404fced2014-06-11 14:45:31 -0700466
467 size_t inbandTracks = 0;
Chong Zhangdcb89b32013-08-06 09:44:47 -0700468 if (mSource != NULL) {
Chong Zhang404fced2014-06-11 14:45:31 -0700469 inbandTracks = mSource->getTrackCount();
470 }
Chong Zhanga7fa1d92014-06-11 14:49:23 -0700471 size_t ccTracks = 0;
472 if (mCCDecoder != NULL) {
473 ccTracks = mCCDecoder->getTrackCount();
474 }
Chong Zhang404fced2014-06-11 14:45:31 -0700475
476 if (trackIndex < inbandTracks) {
Chong Zhangdcb89b32013-08-06 09:44:47 -0700477 err = mSource->selectTrack(trackIndex, select);
Robert Shihd3b0bbb2014-07-23 15:00:25 -0700478
479 if (!select && err == OK) {
480 int32_t type;
481 sp<AMessage> info = mSource->getTrackInfo(trackIndex);
482 if (info != NULL
483 && info->findInt32("type", &type)
484 && type == MEDIA_TRACK_TYPE_TIMEDTEXT) {
485 ++mTimedTextGeneration;
486 }
487 }
Chong Zhanga7fa1d92014-06-11 14:49:23 -0700488 } else {
489 trackIndex -= inbandTracks;
490
491 if (trackIndex < ccTracks) {
492 err = mCCDecoder->selectTrack(trackIndex, select);
493 }
Chong Zhangdcb89b32013-08-06 09:44:47 -0700494 }
495
496 sp<AMessage> response = new AMessage;
497 response->setInt32("err", err);
498
499 response->postReply(replyID);
500 break;
501 }
502
Andreas Huberb7c8e912012-11-27 15:02:53 -0800503 case kWhatPollDuration:
504 {
505 int32_t generation;
506 CHECK(msg->findInt32("generation", &generation));
507
508 if (generation != mPollDurationGeneration) {
509 // stale
510 break;
511 }
512
513 int64_t durationUs;
514 if (mDriver != NULL && mSource->getDuration(&durationUs) == OK) {
515 sp<NuPlayerDriver> driver = mDriver.promote();
516 if (driver != NULL) {
517 driver->notifyDuration(durationUs);
518 }
519 }
520
521 msg->post(1000000ll); // poll again in a second.
522 break;
523 }
524
Glenn Kasten11731182011-02-08 17:26:17 -0800525 case kWhatSetVideoNativeWindow:
Andreas Huberf9334412010-12-15 15:17:42 -0800526 {
Steve Block3856b092011-10-20 11:56:00 +0100527 ALOGV("kWhatSetVideoNativeWindow");
Andreas Huberf9334412010-12-15 15:17:42 -0800528
Andreas Huber57a339c2012-12-03 11:18:00 -0800529 mDeferredActions.push_back(
Andreas Huber14f76722013-01-15 09:04:18 -0800530 new ShutdownDecoderAction(
531 false /* audio */, true /* video */));
Andreas Huber57a339c2012-12-03 11:18:00 -0800532
Andreas Huberf9334412010-12-15 15:17:42 -0800533 sp<RefBase> obj;
Glenn Kasten11731182011-02-08 17:26:17 -0800534 CHECK(msg->findObject("native-window", &obj));
Andreas Huberf9334412010-12-15 15:17:42 -0800535
Andreas Huber57a339c2012-12-03 11:18:00 -0800536 mDeferredActions.push_back(
537 new SetSurfaceAction(
538 static_cast<NativeWindowWrapper *>(obj.get())));
James Dong0d268a32012-08-31 12:18:27 -0700539
Andreas Huber57a339c2012-12-03 11:18:00 -0800540 if (obj != NULL) {
Wei Jia36f38982014-09-25 15:27:04 -0700541 if (mStarted && mSource->getFormat(false /* audio */) != NULL) {
Andy Hung73535852014-09-05 11:42:58 -0700542 // Issue a seek to refresh the video screen only if started otherwise
543 // the extractor may not yet be started and will assert.
544 // If the video decoder is not set (perhaps audio only in this case)
545 // do not perform a seek as it is not needed.
Ronghua Wua73d9e02014-10-08 15:13:29 -0700546 int64_t currentPositionUs = 0;
547 if (getCurrentPosition(&currentPositionUs) == OK) {
548 mDeferredActions.push_back(
549 new SeekAction(currentPositionUs, false /* needNotify */));
550 }
Andy Hung73535852014-09-05 11:42:58 -0700551 }
Wei Jiaac428aa2014-09-02 19:01:34 -0700552
Andreas Huber57a339c2012-12-03 11:18:00 -0800553 // If there is a new surface texture, instantiate decoders
554 // again if possible.
555 mDeferredActions.push_back(
556 new SimpleAction(&NuPlayer::performScanSources));
557 }
558
559 processDeferredActions();
Andreas Huberf9334412010-12-15 15:17:42 -0800560 break;
561 }
562
563 case kWhatSetAudioSink:
564 {
Steve Block3856b092011-10-20 11:56:00 +0100565 ALOGV("kWhatSetAudioSink");
Andreas Huberf9334412010-12-15 15:17:42 -0800566
567 sp<RefBase> obj;
568 CHECK(msg->findObject("sink", &obj));
569
570 mAudioSink = static_cast<MediaPlayerBase::AudioSink *>(obj.get());
571 break;
572 }
573
574 case kWhatStart:
575 {
Steve Block3856b092011-10-20 11:56:00 +0100576 ALOGV("kWhatStart");
Andreas Huber43c3e6c2011-01-05 12:17:08 -0800577
Andreas Huber3fe62152011-09-16 15:09:22 -0700578 mVideoIsAVC = false;
Wei Jiabc2fb722014-07-08 16:37:57 -0700579 mOffloadAudio = false;
Andreas Huber1aef2112011-01-04 14:01:29 -0800580 mAudioEOS = false;
581 mVideoEOS = false;
Andreas Huber32f3cef2011-03-02 15:34:46 -0800582 mSkipRenderingAudioUntilMediaTimeUs = -1;
583 mSkipRenderingVideoUntilMediaTimeUs = -1;
Andreas Huber3fe62152011-09-16 15:09:22 -0700584 mNumFramesTotal = 0;
585 mNumFramesDropped = 0;
Andreas Huber57a339c2012-12-03 11:18:00 -0800586 mStarted = true;
Andreas Huber1aef2112011-01-04 14:01:29 -0800587
Lajos Molnar09524832014-07-17 14:29:51 -0700588 /* instantiate decoders now for secure playback */
589 if (mSourceFlags & Source::FLAG_SECURE) {
590 if (mNativeWindow != NULL) {
591 instantiateDecoder(false, &mVideoDecoder);
592 }
593
594 if (mAudioSink != NULL) {
595 instantiateDecoder(true, &mAudioDecoder);
596 }
597 }
598
Andreas Huber5bc087c2010-12-23 10:27:40 -0800599 mSource->start();
Andreas Huberf9334412010-12-15 15:17:42 -0800600
Andreas Huberd5e56232013-03-12 11:01:43 -0700601 uint32_t flags = 0;
602
603 if (mSource->isRealTime()) {
604 flags |= Renderer::FLAG_REAL_TIME;
605 }
606
Wei Jiabc2fb722014-07-08 16:37:57 -0700607 sp<MetaData> audioMeta = mSource->getFormatMeta(true /* audio */);
608 audio_stream_type_t streamType = AUDIO_STREAM_MUSIC;
609 if (mAudioSink != NULL) {
610 streamType = mAudioSink->getAudioStreamType();
611 }
612
613 sp<AMessage> videoFormat = mSource->getFormat(false /* audio */);
614
615 mOffloadAudio =
616 canOffloadStream(audioMeta, (videoFormat != NULL),
617 true /* is_streaming */, streamType);
618 if (mOffloadAudio) {
619 flags |= Renderer::FLAG_OFFLOAD_AUDIO;
620 }
621
Wei Jia57568df2014-09-22 10:16:29 -0700622 sp<AMessage> notify = new AMessage(kWhatRendererNotify, id());
623 ++mRendererGeneration;
624 notify->setInt32("generation", mRendererGeneration);
625 mRenderer = new Renderer(mAudioSink, notify, flags);
Andreas Huberf9334412010-12-15 15:17:42 -0800626
Lajos Molnar09524832014-07-17 14:29:51 -0700627 mRendererLooper = new ALooper;
628 mRendererLooper->setName("NuPlayerRenderer");
629 mRendererLooper->start(false, false, ANDROID_PRIORITY_AUDIO);
630 mRendererLooper->registerHandler(mRenderer);
Andreas Huberf9334412010-12-15 15:17:42 -0800631
Lajos Molnarc851b5d2014-09-18 14:14:29 -0700632 sp<MetaData> meta = getFileMeta();
633 int32_t rate;
634 if (meta != NULL
635 && meta->findInt32(kKeyFrameRate, &rate) && rate > 0) {
636 mRenderer->setVideoFrameRate(rate);
637 }
638
Andreas Huber1aef2112011-01-04 14:01:29 -0800639 postScanSources();
Andreas Huberf9334412010-12-15 15:17:42 -0800640 break;
641 }
642
643 case kWhatScanSources:
644 {
Andreas Huber1aef2112011-01-04 14:01:29 -0800645 int32_t generation;
646 CHECK(msg->findInt32("generation", &generation));
647 if (generation != mScanSourcesGeneration) {
648 // Drop obsolete msg.
649 break;
650 }
651
Andreas Huber5bc087c2010-12-23 10:27:40 -0800652 mScanSourcesPending = false;
653
Steve Block3856b092011-10-20 11:56:00 +0100654 ALOGV("scanning sources haveAudio=%d, haveVideo=%d",
Andreas Huber43c3e6c2011-01-05 12:17:08 -0800655 mAudioDecoder != NULL, mVideoDecoder != NULL);
656
Andreas Huberb7c8e912012-11-27 15:02:53 -0800657 bool mHadAnySourcesBefore =
658 (mAudioDecoder != NULL) || (mVideoDecoder != NULL);
659
Andy Hung282a7e32014-08-14 15:56:34 -0700660 // initialize video before audio because successful initialization of
661 // video may change deep buffer mode of audio.
Haynes Mathew George5d246ef2012-07-09 10:36:57 -0700662 if (mNativeWindow != NULL) {
663 instantiateDecoder(false, &mVideoDecoder);
664 }
Andreas Huberf9334412010-12-15 15:17:42 -0800665
Ronghua Wuf06060f2014-11-06 16:15:20 -0800666 // Don't try to re-open audio sink if there's an existing decoder.
667 if (mAudioSink != NULL && mAudioDecoder == NULL) {
668 sp<MetaData> audioMeta = mSource->getFormatMeta(true /* audio */);
669 sp<AMessage> videoFormat = mSource->getFormat(false /* audio */);
670 audio_stream_type_t streamType = mAudioSink->getAudioStreamType();
671 bool canOffload = canOffloadStream(audioMeta, (videoFormat != NULL),
672 true /* is_streaming */, streamType);
673 if (canOffload) {
674 if (!mOffloadAudio) {
675 mRenderer->signalEnableOffloadAudio();
676 }
Andy Hung282a7e32014-08-14 15:56:34 -0700677 // open audio sink early under offload mode.
678 sp<AMessage> format = mSource->getFormat(true /*audio*/);
679 openAudioSink(format, true /*offloadOnly*/);
680 }
Andreas Huber5bc087c2010-12-23 10:27:40 -0800681 instantiateDecoder(true, &mAudioDecoder);
Andreas Huberf9334412010-12-15 15:17:42 -0800682 }
683
Andreas Huberb7c8e912012-11-27 15:02:53 -0800684 if (!mHadAnySourcesBefore
685 && (mAudioDecoder != NULL || mVideoDecoder != NULL)) {
686 // This is the first time we've found anything playable.
687
Andreas Huber9575c962013-02-05 13:59:56 -0800688 if (mSourceFlags & Source::FLAG_DYNAMIC_DURATION) {
Andreas Huberb7c8e912012-11-27 15:02:53 -0800689 schedulePollDuration();
690 }
691 }
692
Andreas Hubereac68ba2011-09-27 12:12:25 -0700693 status_t err;
694 if ((err = mSource->feedMoreTSData()) != OK) {
Andreas Huber1aef2112011-01-04 14:01:29 -0800695 if (mAudioDecoder == NULL && mVideoDecoder == NULL) {
696 // We're not currently decoding anything (no audio or
697 // video tracks found) and we just ran out of input data.
Andreas Hubereac68ba2011-09-27 12:12:25 -0700698
699 if (err == ERROR_END_OF_STREAM) {
700 notifyListener(MEDIA_PLAYBACK_COMPLETE, 0, 0);
701 } else {
702 notifyListener(MEDIA_ERROR, MEDIA_ERROR_UNKNOWN, err);
703 }
Andreas Huber1aef2112011-01-04 14:01:29 -0800704 }
Andreas Huberf9334412010-12-15 15:17:42 -0800705 break;
706 }
707
Andreas Huberfbe9d812012-08-31 14:05:27 -0700708 if ((mAudioDecoder == NULL && mAudioSink != NULL)
709 || (mVideoDecoder == NULL && mNativeWindow != NULL)) {
Andreas Huberf9334412010-12-15 15:17:42 -0800710 msg->post(100000ll);
Andreas Huber5bc087c2010-12-23 10:27:40 -0800711 mScanSourcesPending = true;
Andreas Huberf9334412010-12-15 15:17:42 -0800712 }
713 break;
714 }
715
716 case kWhatVideoNotify:
717 case kWhatAudioNotify:
718 {
719 bool audio = msg->what() == kWhatAudioNotify;
720
Wei Jia88703c32014-08-06 11:24:07 -0700721 int32_t currentDecoderGeneration =
722 (audio? mAudioDecoderGeneration : mVideoDecoderGeneration);
723 int32_t requesterGeneration = currentDecoderGeneration - 1;
724 CHECK(msg->findInt32("generation", &requesterGeneration));
725
726 if (requesterGeneration != currentDecoderGeneration) {
727 ALOGV("got message from old %s decoder, generation(%d:%d)",
728 audio ? "audio" : "video", requesterGeneration,
729 currentDecoderGeneration);
730 sp<AMessage> reply;
731 if (!(msg->findMessage("reply", &reply))) {
732 return;
733 }
734
735 reply->setInt32("err", INFO_DISCONTINUITY);
736 reply->post();
737 return;
738 }
739
Andreas Huberf9334412010-12-15 15:17:42 -0800740 int32_t what;
Lajos Molnar1cd13982014-01-17 15:12:51 -0800741 CHECK(msg->findInt32("what", &what));
Andreas Huberf9334412010-12-15 15:17:42 -0800742
Lajos Molnar1cd13982014-01-17 15:12:51 -0800743 if (what == Decoder::kWhatFillThisBuffer) {
Andreas Huberf9334412010-12-15 15:17:42 -0800744 status_t err = feedDecoderInputData(
Lajos Molnar1cd13982014-01-17 15:12:51 -0800745 audio, msg);
Andreas Huberf9334412010-12-15 15:17:42 -0800746
Andreas Huber5bc087c2010-12-23 10:27:40 -0800747 if (err == -EWOULDBLOCK) {
Andreas Hubereac68ba2011-09-27 12:12:25 -0700748 if (mSource->feedMoreTSData() == OK) {
Phil Burkc5cc2e22014-09-09 20:08:39 -0700749 msg->post(10 * 1000ll);
Andreas Huber5bc087c2010-12-23 10:27:40 -0800750 }
Andreas Huberf9334412010-12-15 15:17:42 -0800751 }
Lajos Molnar1cd13982014-01-17 15:12:51 -0800752 } else if (what == Decoder::kWhatEOS) {
Andreas Huberdc9bacd2011-09-26 10:53:29 -0700753 int32_t err;
Lajos Molnar1cd13982014-01-17 15:12:51 -0800754 CHECK(msg->findInt32("err", &err));
Andreas Huberdc9bacd2011-09-26 10:53:29 -0700755
756 if (err == ERROR_END_OF_STREAM) {
Steve Block3856b092011-10-20 11:56:00 +0100757 ALOGV("got %s decoder EOS", audio ? "audio" : "video");
Andreas Huberdc9bacd2011-09-26 10:53:29 -0700758 } else {
Steve Block3856b092011-10-20 11:56:00 +0100759 ALOGV("got %s decoder EOS w/ error %d",
Andreas Huberdc9bacd2011-09-26 10:53:29 -0700760 audio ? "audio" : "video",
761 err);
762 }
763
764 mRenderer->queueEOS(audio, err);
Lajos Molnar1cd13982014-01-17 15:12:51 -0800765 } else if (what == Decoder::kWhatFlushCompleted) {
Steve Block3856b092011-10-20 11:56:00 +0100766 ALOGV("decoder %s flush completed", audio ? "audio" : "video");
Andreas Huberf9334412010-12-15 15:17:42 -0800767
Andy Hung8d121d42014-10-03 09:53:53 -0700768 handleFlushComplete(audio, true /* isDecoder */);
Andreas Huber3831a062010-12-21 10:22:33 -0800769 finishFlushIfPossible();
Lajos Molnar1cd13982014-01-17 15:12:51 -0800770 } else if (what == Decoder::kWhatOutputFormatChanged) {
771 sp<AMessage> format;
772 CHECK(msg->findMessage("format", &format));
773
Andreas Huber31e25082011-01-10 10:38:31 -0800774 if (audio) {
Andy Hung282a7e32014-08-14 15:56:34 -0700775 openAudioSink(format, false /*offloadOnly*/);
Andreas Huber31e25082011-01-10 10:38:31 -0800776 } else {
777 // video
Chong Zhangced1c2f2014-08-08 15:22:35 -0700778 sp<AMessage> inputFormat =
779 mSource->getFormat(false /* audio */);
Andreas Huber3831a062010-12-21 10:22:33 -0800780
Chong Zhangced1c2f2014-08-08 15:22:35 -0700781 updateVideoSize(inputFormat, format);
Andreas Huber31e25082011-01-10 10:38:31 -0800782 }
Lajos Molnar1cd13982014-01-17 15:12:51 -0800783 } else if (what == Decoder::kWhatShutdownCompleted) {
Steve Block3856b092011-10-20 11:56:00 +0100784 ALOGV("%s shutdown completed", audio ? "audio" : "video");
Andreas Huber3831a062010-12-21 10:22:33 -0800785 if (audio) {
786 mAudioDecoder.clear();
Wei Jia57568df2014-09-22 10:16:29 -0700787 ++mAudioDecoderGeneration;
Andreas Huber3831a062010-12-21 10:22:33 -0800788
789 CHECK_EQ((int)mFlushingAudio, (int)SHUTTING_DOWN_DECODER);
790 mFlushingAudio = SHUT_DOWN;
791 } else {
792 mVideoDecoder.clear();
Wei Jia57568df2014-09-22 10:16:29 -0700793 ++mVideoDecoderGeneration;
Andreas Huber3831a062010-12-21 10:22:33 -0800794
795 CHECK_EQ((int)mFlushingVideo, (int)SHUTTING_DOWN_DECODER);
796 mFlushingVideo = SHUT_DOWN;
797 }
798
799 finishFlushIfPossible();
Lajos Molnar1cd13982014-01-17 15:12:51 -0800800 } else if (what == Decoder::kWhatError) {
Chong Zhangf4c0a942014-08-11 15:14:10 -0700801 status_t err;
Andy Hung2abde2c2014-09-30 14:40:32 -0700802 if (!msg->findInt32("err", &err) || err == OK) {
Chong Zhangf4c0a942014-08-11 15:14:10 -0700803 err = UNKNOWN_ERROR;
804 }
Andy Hungcf31f1e2014-09-23 14:59:01 -0700805
Andy Hung2abde2c2014-09-30 14:40:32 -0700806 // Decoder errors can be due to Source (e.g. from streaming),
807 // or from decoding corrupted bitstreams, or from other decoder
808 // MediaCodec operations (e.g. from an ongoing reset or seek).
809 //
810 // We try to gracefully shut down the affected decoder if possible,
811 // rather than trying to force the shutdown with something
812 // similar to performReset(). This method can lead to a hang
813 // if MediaCodec functions block after an error, but they should
814 // typically return INVALID_OPERATION instead of blocking.
815
816 FlushStatus *flushing = audio ? &mFlushingAudio : &mFlushingVideo;
817 ALOGE("received error(%#x) from %s decoder, flushing(%d), now shutting down",
818 err, audio ? "audio" : "video", *flushing);
819
820 switch (*flushing) {
821 case NONE:
822 mDeferredActions.push_back(
823 new ShutdownDecoderAction(audio, !audio /* video */));
824 processDeferredActions();
825 break;
826 case FLUSHING_DECODER:
827 *flushing = FLUSHING_DECODER_SHUTDOWN; // initiate shutdown after flush.
828 break; // Wait for flush to complete.
829 case FLUSHING_DECODER_SHUTDOWN:
830 break; // Wait for flush to complete.
831 case SHUTTING_DOWN_DECODER:
832 break; // Wait for shutdown to complete.
833 case FLUSHED:
834 // Widevine source reads must stop before releasing the video decoder.
835 if (!audio && mSource != NULL && mSourceFlags & Source::FLAG_SECURE) {
836 mSource->stop();
837 }
838 getDecoder(audio)->initiateShutdown(); // In the middle of a seek.
839 *flushing = SHUTTING_DOWN_DECODER; // Shut down.
840 break;
841 case SHUT_DOWN:
842 finishFlushIfPossible(); // Should not occur.
843 break; // Finish anyways.
Marco Nelissen9e2b7912014-08-18 16:13:03 -0700844 }
Andy Hung2abde2c2014-09-30 14:40:32 -0700845 notifyListener(MEDIA_ERROR, MEDIA_ERROR_UNKNOWN, err);
Lajos Molnar1cd13982014-01-17 15:12:51 -0800846 } else if (what == Decoder::kWhatDrainThisBuffer) {
847 renderBuffer(audio, msg);
848 } else {
849 ALOGV("Unhandled decoder notification %d '%c%c%c%c'.",
Andreas Hubera1f8ab02012-11-30 10:53:22 -0800850 what,
851 what >> 24,
852 (what >> 16) & 0xff,
853 (what >> 8) & 0xff,
854 what & 0xff);
Andreas Huberf9334412010-12-15 15:17:42 -0800855 }
856
857 break;
858 }
859
860 case kWhatRendererNotify:
861 {
Wei Jia57568df2014-09-22 10:16:29 -0700862 int32_t requesterGeneration = mRendererGeneration - 1;
863 CHECK(msg->findInt32("generation", &requesterGeneration));
864 if (requesterGeneration != mRendererGeneration) {
865 ALOGV("got message from old renderer, generation(%d:%d)",
866 requesterGeneration, mRendererGeneration);
867 return;
868 }
869
Andreas Huberf9334412010-12-15 15:17:42 -0800870 int32_t what;
871 CHECK(msg->findInt32("what", &what));
872
873 if (what == Renderer::kWhatEOS) {
874 int32_t audio;
875 CHECK(msg->findInt32("audio", &audio));
876
Andreas Huberc92fd242011-08-16 13:48:44 -0700877 int32_t finalResult;
878 CHECK(msg->findInt32("finalResult", &finalResult));
879
Andreas Huberf9334412010-12-15 15:17:42 -0800880 if (audio) {
881 mAudioEOS = true;
882 } else {
883 mVideoEOS = true;
884 }
885
Andreas Huberc92fd242011-08-16 13:48:44 -0700886 if (finalResult == ERROR_END_OF_STREAM) {
Steve Block3856b092011-10-20 11:56:00 +0100887 ALOGV("reached %s EOS", audio ? "audio" : "video");
Andreas Huberc92fd242011-08-16 13:48:44 -0700888 } else {
Steve Block29357bc2012-01-06 19:20:56 +0000889 ALOGE("%s track encountered an error (%d)",
Andreas Huberc92fd242011-08-16 13:48:44 -0700890 audio ? "audio" : "video", finalResult);
891
892 notifyListener(
893 MEDIA_ERROR, MEDIA_ERROR_UNKNOWN, finalResult);
894 }
Andreas Huberf9334412010-12-15 15:17:42 -0800895
896 if ((mAudioEOS || mAudioDecoder == NULL)
897 && (mVideoEOS || mVideoDecoder == NULL)) {
898 notifyListener(MEDIA_PLAYBACK_COMPLETE, 0, 0);
899 }
Andreas Huber3fe62152011-09-16 15:09:22 -0700900 } else if (what == Renderer::kWhatFlushComplete) {
Andreas Huberf9334412010-12-15 15:17:42 -0800901 int32_t audio;
902 CHECK(msg->findInt32("audio", &audio));
903
Steve Block3856b092011-10-20 11:56:00 +0100904 ALOGV("renderer %s flush completed.", audio ? "audio" : "video");
Andy Hung8d121d42014-10-03 09:53:53 -0700905 handleFlushComplete(audio, false /* isDecoder */);
906 finishFlushIfPossible();
James Dongf57b4ea2012-07-20 13:38:36 -0700907 } else if (what == Renderer::kWhatVideoRenderingStart) {
908 notifyListener(MEDIA_INFO, MEDIA_INFO_RENDERING_START, 0);
Lajos Molnarcbaffcf2013-08-14 18:30:38 -0700909 } else if (what == Renderer::kWhatMediaRenderingStart) {
910 ALOGV("media rendering started");
911 notifyListener(MEDIA_STARTED, 0, 0);
Wei Jia3a2956d2014-07-22 16:01:33 -0700912 } else if (what == Renderer::kWhatAudioOffloadTearDown) {
Ronghua Wuf06060f2014-11-06 16:15:20 -0800913 ALOGV("Tear down audio offload, fall back to s/w path if due to error.");
Wei Jia3a2956d2014-07-22 16:01:33 -0700914 int64_t positionUs;
915 CHECK(msg->findInt64("positionUs", &positionUs));
Ronghua Wu08529172014-10-02 16:55:52 -0700916 int32_t reason;
917 CHECK(msg->findInt32("reason", &reason));
Andy Hung282a7e32014-08-14 15:56:34 -0700918 closeAudioSink();
Wei Jia3a2956d2014-07-22 16:01:33 -0700919 mAudioDecoder.clear();
Wei Jia57568df2014-09-22 10:16:29 -0700920 ++mAudioDecoderGeneration;
Wei Jia3a2956d2014-07-22 16:01:33 -0700921 mRenderer->flush(true /* audio */);
922 if (mVideoDecoder != NULL) {
923 mRenderer->flush(false /* audio */);
924 }
Wei Jia3a2956d2014-07-22 16:01:33 -0700925
Wei Jiae427abf2014-09-22 15:21:11 -0700926 performSeek(positionUs, false /* needNotify */);
Ronghua Wu08529172014-10-02 16:55:52 -0700927 if (reason == Renderer::kDueToError) {
Ronghua Wuf06060f2014-11-06 16:15:20 -0800928 mRenderer->signalDisableOffloadAudio();
929 mOffloadAudio = false;
Ronghua Wu08529172014-10-02 16:55:52 -0700930 instantiateDecoder(true /* audio */, &mAudioDecoder);
931 }
Andreas Huberf9334412010-12-15 15:17:42 -0800932 }
933 break;
934 }
935
936 case kWhatMoreDataQueued:
937 {
938 break;
939 }
940
Andreas Huber1aef2112011-01-04 14:01:29 -0800941 case kWhatReset:
942 {
Steve Block3856b092011-10-20 11:56:00 +0100943 ALOGV("kWhatReset");
Andreas Huber1aef2112011-01-04 14:01:29 -0800944
Andreas Hubera1f8ab02012-11-30 10:53:22 -0800945 mDeferredActions.push_back(
Andreas Huber14f76722013-01-15 09:04:18 -0800946 new ShutdownDecoderAction(
947 true /* audio */, true /* video */));
Andreas Huberb7c8e912012-11-27 15:02:53 -0800948
Andreas Hubera1f8ab02012-11-30 10:53:22 -0800949 mDeferredActions.push_back(
950 new SimpleAction(&NuPlayer::performReset));
Andreas Huberb58ce9f2011-11-28 16:27:35 -0800951
Andreas Hubera1f8ab02012-11-30 10:53:22 -0800952 processDeferredActions();
Andreas Huber1aef2112011-01-04 14:01:29 -0800953 break;
954 }
955
Andreas Huber43c3e6c2011-01-05 12:17:08 -0800956 case kWhatSeek:
957 {
958 int64_t seekTimeUs;
Wei Jiae427abf2014-09-22 15:21:11 -0700959 int32_t needNotify;
Andreas Huber43c3e6c2011-01-05 12:17:08 -0800960 CHECK(msg->findInt64("seekTimeUs", &seekTimeUs));
Wei Jiae427abf2014-09-22 15:21:11 -0700961 CHECK(msg->findInt32("needNotify", &needNotify));
Andreas Huber43c3e6c2011-01-05 12:17:08 -0800962
Wei Jiae427abf2014-09-22 15:21:11 -0700963 ALOGV("kWhatSeek seekTimeUs=%lld us, needNotify=%d",
964 seekTimeUs, needNotify);
Andreas Huber43c3e6c2011-01-05 12:17:08 -0800965
Andreas Hubera1f8ab02012-11-30 10:53:22 -0800966 mDeferredActions.push_back(
967 new SimpleAction(&NuPlayer::performDecoderFlush));
Andreas Huber43c3e6c2011-01-05 12:17:08 -0800968
Wei Jiae427abf2014-09-22 15:21:11 -0700969 mDeferredActions.push_back(
970 new SeekAction(seekTimeUs, needNotify));
Andreas Huber43c3e6c2011-01-05 12:17:08 -0800971
Andreas Hubera1f8ab02012-11-30 10:53:22 -0800972 processDeferredActions();
Andreas Huber43c3e6c2011-01-05 12:17:08 -0800973 break;
974 }
975
Andreas Huberb4082222011-01-20 15:23:04 -0800976 case kWhatPause:
977 {
Wei Jia7e9f7f72014-09-23 10:55:35 -0700978 if (mSource != NULL) {
979 mSource->pause();
980 } else {
981 ALOGW("pause called when source is gone or not set");
982 }
983 if (mRenderer != NULL) {
984 mRenderer->pause();
985 } else {
986 ALOGW("pause called when renderer is gone or not set");
987 }
Andreas Huberb4082222011-01-20 15:23:04 -0800988 break;
989 }
990
991 case kWhatResume:
992 {
Wei Jia7e9f7f72014-09-23 10:55:35 -0700993 if (mSource != NULL) {
994 mSource->resume();
995 } else {
996 ALOGW("resume called when source is gone or not set");
997 }
Ronghua Wud7988b12014-10-03 15:19:10 -0700998 // |mAudioDecoder| may have been released due to the pause timeout, so re-create it if
999 // needed.
1000 if (audioDecoderStillNeeded() && mAudioDecoder == NULL) {
Ronghua Wu08529172014-10-02 16:55:52 -07001001 instantiateDecoder(true /* audio */, &mAudioDecoder);
1002 }
Wei Jia7e9f7f72014-09-23 10:55:35 -07001003 if (mRenderer != NULL) {
1004 mRenderer->resume();
1005 } else {
1006 ALOGW("resume called when renderer is gone or not set");
1007 }
Andreas Huberb4082222011-01-20 15:23:04 -08001008 break;
1009 }
1010
Andreas Huberb5f25f02013-02-05 10:14:26 -08001011 case kWhatSourceNotify:
1012 {
Andreas Huber9575c962013-02-05 13:59:56 -08001013 onSourceNotify(msg);
Andreas Huberb5f25f02013-02-05 10:14:26 -08001014 break;
1015 }
1016
Chong Zhanga7fa1d92014-06-11 14:49:23 -07001017 case kWhatClosedCaptionNotify:
1018 {
1019 onClosedCaptionNotify(msg);
1020 break;
1021 }
1022
Andreas Huberf9334412010-12-15 15:17:42 -08001023 default:
1024 TRESPASS();
1025 break;
1026 }
1027}
1028
Ronghua Wud7988b12014-10-03 15:19:10 -07001029bool NuPlayer::audioDecoderStillNeeded() {
1030 // Audio decoder is no longer needed if it's in shut/shutting down status.
1031 return ((mFlushingAudio != SHUT_DOWN) && (mFlushingAudio != SHUTTING_DOWN_DECODER));
1032}
1033
Andy Hung8d121d42014-10-03 09:53:53 -07001034void NuPlayer::handleFlushComplete(bool audio, bool isDecoder) {
1035 // We wait for both the decoder flush and the renderer flush to complete
1036 // before entering either the FLUSHED or the SHUTTING_DOWN_DECODER state.
1037
1038 mFlushComplete[audio][isDecoder] = true;
1039 if (!mFlushComplete[audio][!isDecoder]) {
1040 return;
1041 }
1042
1043 FlushStatus *state = audio ? &mFlushingAudio : &mFlushingVideo;
1044 switch (*state) {
1045 case FLUSHING_DECODER:
1046 {
1047 *state = FLUSHED;
Andy Hung8d121d42014-10-03 09:53:53 -07001048 break;
1049 }
1050
1051 case FLUSHING_DECODER_SHUTDOWN:
1052 {
1053 *state = SHUTTING_DOWN_DECODER;
1054
1055 ALOGV("initiating %s decoder shutdown", audio ? "audio" : "video");
1056 if (!audio) {
Andy Hung8d121d42014-10-03 09:53:53 -07001057 // Widevine source reads must stop before releasing the video decoder.
1058 if (mSource != NULL && mSourceFlags & Source::FLAG_SECURE) {
1059 mSource->stop();
1060 }
1061 }
1062 getDecoder(audio)->initiateShutdown();
1063 break;
1064 }
1065
1066 default:
1067 // decoder flush completes only occur in a flushing state.
1068 LOG_ALWAYS_FATAL_IF(isDecoder, "decoder flush in invalid state %d", *state);
1069 break;
1070 }
1071}
1072
Andreas Huber3831a062010-12-21 10:22:33 -08001073void NuPlayer::finishFlushIfPossible() {
Wei Jia53904f32014-07-29 10:22:53 -07001074 if (mFlushingAudio != NONE && mFlushingAudio != FLUSHED
1075 && mFlushingAudio != SHUT_DOWN) {
Andreas Huber3831a062010-12-21 10:22:33 -08001076 return;
1077 }
1078
Wei Jia53904f32014-07-29 10:22:53 -07001079 if (mFlushingVideo != NONE && mFlushingVideo != FLUSHED
1080 && mFlushingVideo != SHUT_DOWN) {
Andreas Huber3831a062010-12-21 10:22:33 -08001081 return;
1082 }
1083
Steve Block3856b092011-10-20 11:56:00 +01001084 ALOGV("both audio and video are flushed now.");
Andreas Huber3831a062010-12-21 10:22:33 -08001085
Phil Burk9f526492014-09-03 15:04:12 -07001086 mPendingAudioAccessUnit.clear();
Phil Burkc5cc2e22014-09-09 20:08:39 -07001087 mAggregateBuffer.clear();
Phil Burk9f526492014-09-03 15:04:12 -07001088
Andreas Huber6e3d3112011-11-28 12:36:11 -08001089 if (mTimeDiscontinuityPending) {
1090 mRenderer->signalTimeDiscontinuity();
1091 mTimeDiscontinuityPending = false;
1092 }
Andreas Huber3831a062010-12-21 10:22:33 -08001093
Wei Jia53904f32014-07-29 10:22:53 -07001094 if (mAudioDecoder != NULL && mFlushingAudio == FLUSHED) {
Andreas Huber3831a062010-12-21 10:22:33 -08001095 mAudioDecoder->signalResume();
1096 }
1097
Wei Jia53904f32014-07-29 10:22:53 -07001098 if (mVideoDecoder != NULL && mFlushingVideo == FLUSHED) {
Andreas Huber3831a062010-12-21 10:22:33 -08001099 mVideoDecoder->signalResume();
1100 }
1101
1102 mFlushingAudio = NONE;
1103 mFlushingVideo = NONE;
Andreas Huber3831a062010-12-21 10:22:33 -08001104
Andy Hung8d121d42014-10-03 09:53:53 -07001105 clearFlushComplete();
1106
Andreas Hubera1f8ab02012-11-30 10:53:22 -08001107 processDeferredActions();
Andreas Huber1aef2112011-01-04 14:01:29 -08001108}
1109
1110void NuPlayer::postScanSources() {
1111 if (mScanSourcesPending) {
1112 return;
1113 }
1114
1115 sp<AMessage> msg = new AMessage(kWhatScanSources, id());
1116 msg->setInt32("generation", mScanSourcesGeneration);
1117 msg->post();
1118
1119 mScanSourcesPending = true;
1120}
1121
Andy Hung282a7e32014-08-14 15:56:34 -07001122void NuPlayer::openAudioSink(const sp<AMessage> &format, bool offloadOnly) {
Andy Hung282a7e32014-08-14 15:56:34 -07001123 uint32_t flags;
1124 int64_t durationUs;
Chong Zhang3b9eb1f2014-10-15 17:05:08 -07001125 bool hasVideo = (mVideoDecoder != NULL);
Andy Hung282a7e32014-08-14 15:56:34 -07001126 // FIXME: we should handle the case where the video decoder
1127 // is created after we receive the format change indication.
1128 // Current code will just make that we select deep buffer
1129 // with video which should not be a problem as it should
1130 // not prevent from keeping A/V sync.
Chong Zhang3b9eb1f2014-10-15 17:05:08 -07001131 if (hasVideo &&
Andy Hung282a7e32014-08-14 15:56:34 -07001132 mSource->getDuration(&durationUs) == OK &&
1133 durationUs
1134 > AUDIO_SINK_MIN_DEEP_BUFFER_DURATION_US) {
1135 flags = AUDIO_OUTPUT_FLAG_DEEP_BUFFER;
1136 } else {
1137 flags = AUDIO_OUTPUT_FLAG_NONE;
1138 }
1139
Chong Zhang3b9eb1f2014-10-15 17:05:08 -07001140 mOffloadAudio = mRenderer->openAudioSink(
1141 format, offloadOnly, hasVideo, flags);
1142
Andy Hung282a7e32014-08-14 15:56:34 -07001143 if (mOffloadAudio) {
Chong Zhang3b9eb1f2014-10-15 17:05:08 -07001144 sp<MetaData> audioMeta =
1145 mSource->getFormatMeta(true /* audio */);
1146 sendMetaDataToHal(mAudioSink, audioMeta);
Andy Hung282a7e32014-08-14 15:56:34 -07001147 }
1148}
1149
1150void NuPlayer::closeAudioSink() {
Chong Zhang3b9eb1f2014-10-15 17:05:08 -07001151 mRenderer->closeAudioSink();
Andy Hung282a7e32014-08-14 15:56:34 -07001152}
1153
Andreas Huber5bc087c2010-12-23 10:27:40 -08001154status_t NuPlayer::instantiateDecoder(bool audio, sp<Decoder> *decoder) {
Andreas Huberf9334412010-12-15 15:17:42 -08001155 if (*decoder != NULL) {
1156 return OK;
1157 }
1158
Andreas Huber84066782011-08-16 09:34:26 -07001159 sp<AMessage> format = mSource->getFormat(audio);
Andreas Huberf9334412010-12-15 15:17:42 -08001160
Andreas Huber84066782011-08-16 09:34:26 -07001161 if (format == NULL) {
Andreas Huberf9334412010-12-15 15:17:42 -08001162 return -EWOULDBLOCK;
1163 }
1164
Andreas Huber3fe62152011-09-16 15:09:22 -07001165 if (!audio) {
Andreas Huber84066782011-08-16 09:34:26 -07001166 AString mime;
1167 CHECK(format->findString("mime", &mime));
1168 mVideoIsAVC = !strcasecmp(MEDIA_MIMETYPE_VIDEO_AVC, mime.c_str());
Chong Zhanga7fa1d92014-06-11 14:49:23 -07001169
1170 sp<AMessage> ccNotify = new AMessage(kWhatClosedCaptionNotify, id());
1171 mCCDecoder = new CCDecoder(ccNotify);
Lajos Molnar09524832014-07-17 14:29:51 -07001172
1173 if (mSourceFlags & Source::FLAG_SECURE) {
1174 format->setInt32("secure", true);
1175 }
Andreas Huber3fe62152011-09-16 15:09:22 -07001176 }
1177
Wei Jiabc2fb722014-07-08 16:37:57 -07001178 if (audio) {
Wei Jia88703c32014-08-06 11:24:07 -07001179 sp<AMessage> notify = new AMessage(kWhatAudioNotify, id());
1180 ++mAudioDecoderGeneration;
1181 notify->setInt32("generation", mAudioDecoderGeneration);
1182
Wei Jiabc2fb722014-07-08 16:37:57 -07001183 if (mOffloadAudio) {
1184 *decoder = new DecoderPassThrough(notify);
1185 } else {
1186 *decoder = new Decoder(notify);
1187 }
1188 } else {
Wei Jia88703c32014-08-06 11:24:07 -07001189 sp<AMessage> notify = new AMessage(kWhatVideoNotify, id());
1190 ++mVideoDecoderGeneration;
1191 notify->setInt32("generation", mVideoDecoderGeneration);
1192
Wei Jiabc2fb722014-07-08 16:37:57 -07001193 *decoder = new Decoder(notify, mNativeWindow);
1194 }
Lajos Molnar1cd13982014-01-17 15:12:51 -08001195 (*decoder)->init();
Andreas Huber84066782011-08-16 09:34:26 -07001196 (*decoder)->configure(format);
Andreas Huberf9334412010-12-15 15:17:42 -08001197
Lajos Molnar09524832014-07-17 14:29:51 -07001198 // allocate buffers to decrypt widevine source buffers
1199 if (!audio && (mSourceFlags & Source::FLAG_SECURE)) {
1200 Vector<sp<ABuffer> > inputBufs;
1201 CHECK_EQ((*decoder)->getInputBuffers(&inputBufs), (status_t)OK);
1202
1203 Vector<MediaBuffer *> mediaBufs;
1204 for (size_t i = 0; i < inputBufs.size(); i++) {
1205 const sp<ABuffer> &buffer = inputBufs[i];
1206 MediaBuffer *mbuf = new MediaBuffer(buffer->data(), buffer->size());
1207 mediaBufs.push(mbuf);
1208 }
1209
1210 status_t err = mSource->setBuffers(audio, mediaBufs);
1211 if (err != OK) {
1212 for (size_t i = 0; i < mediaBufs.size(); ++i) {
1213 mediaBufs[i]->release();
1214 }
1215 mediaBufs.clear();
1216 ALOGE("Secure source didn't support secure mediaBufs.");
1217 return err;
1218 }
1219 }
Andreas Huberf9334412010-12-15 15:17:42 -08001220 return OK;
1221}
1222
1223status_t NuPlayer::feedDecoderInputData(bool audio, const sp<AMessage> &msg) {
1224 sp<AMessage> reply;
1225 CHECK(msg->findMessage("reply", &reply));
1226
Wei Jia53904f32014-07-29 10:22:53 -07001227 if ((audio && mFlushingAudio != NONE)
Wei Jiaf702d042014-09-09 12:08:47 -07001228 || (!audio && mFlushingVideo != NONE)
1229 || mSource == NULL) {
Wei Jiab189a5b2014-08-07 06:11:39 +00001230 reply->setInt32("err", INFO_DISCONTINUITY);
1231 reply->post();
1232 return OK;
Andreas Huberf9334412010-12-15 15:17:42 -08001233 }
1234
1235 sp<ABuffer> accessUnit;
Andreas Huberf9334412010-12-15 15:17:42 -08001236
Phil Burk9f526492014-09-03 15:04:12 -07001237 // Aggregate smaller buffers into a larger buffer.
1238 // The goal is to reduce power consumption.
Phil Burk33b51b02014-09-17 16:03:47 -07001239 // Note this will not work if the decoder requires one frame per buffer.
1240 bool doBufferAggregation = (audio && mOffloadAudio);
Phil Burk9f526492014-09-03 15:04:12 -07001241 bool needMoreData = false;
Phil Burk9f526492014-09-03 15:04:12 -07001242
Andreas Huber3fe62152011-09-16 15:09:22 -07001243 bool dropAccessUnit;
1244 do {
Phil Burk9f526492014-09-03 15:04:12 -07001245 status_t err;
1246 // Did we save an accessUnit earlier because of a discontinuity?
1247 if (audio && (mPendingAudioAccessUnit != NULL)) {
1248 accessUnit = mPendingAudioAccessUnit;
1249 mPendingAudioAccessUnit.clear();
1250 err = mPendingAudioErr;
1251 ALOGV("feedDecoderInputData() use mPendingAudioAccessUnit");
1252 } else {
1253 err = mSource->dequeueAccessUnit(audio, &accessUnit);
1254 }
Andreas Huber5bc087c2010-12-23 10:27:40 -08001255
Andreas Huber3fe62152011-09-16 15:09:22 -07001256 if (err == -EWOULDBLOCK) {
Phil Burkc5cc2e22014-09-09 20:08:39 -07001257 return err;
Andreas Huber3fe62152011-09-16 15:09:22 -07001258 } else if (err != OK) {
1259 if (err == INFO_DISCONTINUITY) {
Phil Burk33b51b02014-09-17 16:03:47 -07001260 if (doBufferAggregation && (mAggregateBuffer != NULL)) {
Phil Burk9f526492014-09-03 15:04:12 -07001261 // We already have some data so save this for later.
1262 mPendingAudioErr = err;
1263 mPendingAudioAccessUnit = accessUnit;
1264 accessUnit.clear();
1265 ALOGD("feedDecoderInputData() save discontinuity for later");
1266 break;
1267 }
Andreas Huber3fe62152011-09-16 15:09:22 -07001268 int32_t type;
1269 CHECK(accessUnit->meta()->findInt32("discontinuity", &type));
Andreas Huber53df1a42010-12-22 10:03:04 -08001270
Andreas Huber3fe62152011-09-16 15:09:22 -07001271 bool formatChange =
Andreas Huber6e3d3112011-11-28 12:36:11 -08001272 (audio &&
1273 (type & ATSParser::DISCONTINUITY_AUDIO_FORMAT))
1274 || (!audio &&
1275 (type & ATSParser::DISCONTINUITY_VIDEO_FORMAT));
Andreas Huber53df1a42010-12-22 10:03:04 -08001276
Andreas Huber6e3d3112011-11-28 12:36:11 -08001277 bool timeChange = (type & ATSParser::DISCONTINUITY_TIME) != 0;
1278
Steve Blockdf64d152012-01-04 20:05:49 +00001279 ALOGI("%s discontinuity (formatChange=%d, time=%d)",
Andreas Huber6e3d3112011-11-28 12:36:11 -08001280 audio ? "audio" : "video", formatChange, timeChange);
Andreas Huber32f3cef2011-03-02 15:34:46 -08001281
Andreas Huber3fe62152011-09-16 15:09:22 -07001282 if (audio) {
1283 mSkipRenderingAudioUntilMediaTimeUs = -1;
1284 } else {
1285 mSkipRenderingVideoUntilMediaTimeUs = -1;
1286 }
Andreas Huber32f3cef2011-03-02 15:34:46 -08001287
Andreas Huber6e3d3112011-11-28 12:36:11 -08001288 if (timeChange) {
1289 sp<AMessage> extra;
1290 if (accessUnit->meta()->findMessage("extra", &extra)
1291 && extra != NULL) {
1292 int64_t resumeAtMediaTimeUs;
1293 if (extra->findInt64(
1294 "resume-at-mediatimeUs", &resumeAtMediaTimeUs)) {
Steve Blockdf64d152012-01-04 20:05:49 +00001295 ALOGI("suppressing rendering of %s until %lld us",
Andreas Huber6e3d3112011-11-28 12:36:11 -08001296 audio ? "audio" : "video", resumeAtMediaTimeUs);
Andreas Huber3fe62152011-09-16 15:09:22 -07001297
Andreas Huber6e3d3112011-11-28 12:36:11 -08001298 if (audio) {
1299 mSkipRenderingAudioUntilMediaTimeUs =
1300 resumeAtMediaTimeUs;
1301 } else {
1302 mSkipRenderingVideoUntilMediaTimeUs =
1303 resumeAtMediaTimeUs;
1304 }
Andreas Huber3fe62152011-09-16 15:09:22 -07001305 }
Andreas Huber32f3cef2011-03-02 15:34:46 -08001306 }
1307 }
Andreas Huber3fe62152011-09-16 15:09:22 -07001308
Andreas Huber6e3d3112011-11-28 12:36:11 -08001309 mTimeDiscontinuityPending =
1310 mTimeDiscontinuityPending || timeChange;
1311
Lajos Molnar87603c02014-08-20 19:25:30 -07001312 bool seamlessFormatChange = false;
1313 sp<AMessage> newFormat = mSource->getFormat(audio);
1314 if (formatChange) {
1315 seamlessFormatChange =
1316 getDecoder(audio)->supportsSeamlessFormatChange(newFormat);
1317 // treat seamless format change separately
1318 formatChange = !seamlessFormatChange;
1319 }
1320 bool shutdownOrFlush = formatChange || timeChange;
1321
1322 // We want to queue up scan-sources only once per discontinuity.
1323 // We control this by doing it only if neither audio nor video are
1324 // flushing or shutting down. (After handling 1st discontinuity, one
1325 // of the flushing states will not be NONE.)
1326 // No need to scan sources if this discontinuity does not result
1327 // in a flush or shutdown, as the flushing state will stay NONE.
1328 if (mFlushingAudio == NONE && mFlushingVideo == NONE &&
1329 shutdownOrFlush) {
Robert Shiha2981012014-07-30 17:41:24 -07001330 // And we'll resume scanning sources once we're done
1331 // flushing.
1332 mDeferredActions.push_front(
1333 new SimpleAction(
1334 &NuPlayer::performScanSources));
1335 }
1336
Lajos Molnar87603c02014-08-20 19:25:30 -07001337 if (formatChange /* not seamless */) {
1338 // must change decoder
1339 flushDecoder(audio, /* needShutdown = */ true);
1340 } else if (timeChange) {
1341 // need to flush
1342 flushDecoder(audio, /* needShutdown = */ false, newFormat);
1343 err = OK;
1344 } else if (seamlessFormatChange) {
1345 // reuse existing decoder and don't flush
1346 updateDecoderFormatWithoutFlush(audio, newFormat);
1347 err = OK;
Andreas Huber6e3d3112011-11-28 12:36:11 -08001348 } else {
1349 // This stream is unaffected by the discontinuity
Andreas Huber6e3d3112011-11-28 12:36:11 -08001350 return -EWOULDBLOCK;
1351 }
Andreas Huber32f3cef2011-03-02 15:34:46 -08001352 }
1353
Andreas Huber3fe62152011-09-16 15:09:22 -07001354 reply->setInt32("err", err);
1355 reply->post();
1356 return OK;
Andreas Huberf9334412010-12-15 15:17:42 -08001357 }
1358
Andreas Huber3fe62152011-09-16 15:09:22 -07001359 if (!audio) {
1360 ++mNumFramesTotal;
1361 }
1362
1363 dropAccessUnit = false;
1364 if (!audio
Lajos Molnar09524832014-07-17 14:29:51 -07001365 && !(mSourceFlags & Source::FLAG_SECURE)
Ronghua Wua73d9e02014-10-08 15:13:29 -07001366 && mRenderer->getVideoLateByUs() > 100000ll
Andreas Huber3fe62152011-09-16 15:09:22 -07001367 && mVideoIsAVC
1368 && !IsAVCReferenceFrame(accessUnit)) {
1369 dropAccessUnit = true;
1370 ++mNumFramesDropped;
1371 }
Phil Burk9f526492014-09-03 15:04:12 -07001372
1373 size_t smallSize = accessUnit->size();
1374 needMoreData = false;
Phil Burkc5cc2e22014-09-09 20:08:39 -07001375 if (doBufferAggregation && (mAggregateBuffer == NULL)
Phil Burk9f526492014-09-03 15:04:12 -07001376 // Don't bother if only room for a few small buffers.
Phil Burkc5cc2e22014-09-09 20:08:39 -07001377 && (smallSize < (kAggregateBufferSizeBytes / 3))) {
Phil Burk9f526492014-09-03 15:04:12 -07001378 // Create a larger buffer for combining smaller buffers from the extractor.
Phil Burkc5cc2e22014-09-09 20:08:39 -07001379 mAggregateBuffer = new ABuffer(kAggregateBufferSizeBytes);
1380 mAggregateBuffer->setRange(0, 0); // start empty
Phil Burk9f526492014-09-03 15:04:12 -07001381 }
1382
Phil Burk33b51b02014-09-17 16:03:47 -07001383 if (doBufferAggregation && (mAggregateBuffer != NULL)) {
Phil Burk9f526492014-09-03 15:04:12 -07001384 int64_t timeUs;
Phil Burkc5cc2e22014-09-09 20:08:39 -07001385 int64_t dummy;
Phil Burk9f526492014-09-03 15:04:12 -07001386 bool smallTimestampValid = accessUnit->meta()->findInt64("timeUs", &timeUs);
Phil Burkc5cc2e22014-09-09 20:08:39 -07001387 bool bigTimestampValid = mAggregateBuffer->meta()->findInt64("timeUs", &dummy);
Phil Burk9f526492014-09-03 15:04:12 -07001388 // Will the smaller buffer fit?
Phil Burkc5cc2e22014-09-09 20:08:39 -07001389 size_t bigSize = mAggregateBuffer->size();
1390 size_t roomLeft = mAggregateBuffer->capacity() - bigSize;
Phil Burk9f526492014-09-03 15:04:12 -07001391 // Should we save this small buffer for the next big buffer?
1392 // If the first small buffer did not have a timestamp then save
1393 // any buffer that does have a timestamp until the next big buffer.
1394 if ((smallSize > roomLeft)
Phil Burkc5cc2e22014-09-09 20:08:39 -07001395 || (!bigTimestampValid && (bigSize > 0) && smallTimestampValid)) {
Phil Burk9f526492014-09-03 15:04:12 -07001396 mPendingAudioErr = err;
1397 mPendingAudioAccessUnit = accessUnit;
1398 accessUnit.clear();
1399 } else {
Phil Burkc5cc2e22014-09-09 20:08:39 -07001400 // Grab time from first small buffer if available.
1401 if ((bigSize == 0) && smallTimestampValid) {
1402 mAggregateBuffer->meta()->setInt64("timeUs", timeUs);
1403 }
Phil Burk9f526492014-09-03 15:04:12 -07001404 // Append small buffer to the bigger buffer.
Phil Burkc5cc2e22014-09-09 20:08:39 -07001405 memcpy(mAggregateBuffer->base() + bigSize, accessUnit->data(), smallSize);
Phil Burk9f526492014-09-03 15:04:12 -07001406 bigSize += smallSize;
Phil Burkc5cc2e22014-09-09 20:08:39 -07001407 mAggregateBuffer->setRange(0, bigSize);
Phil Burk9f526492014-09-03 15:04:12 -07001408
Phil Burkc5cc2e22014-09-09 20:08:39 -07001409 // Keep looping until we run out of room in the mAggregateBuffer.
Phil Burk9f526492014-09-03 15:04:12 -07001410 needMoreData = true;
1411
Phil Burkc5cc2e22014-09-09 20:08:39 -07001412 ALOGV("feedDecoderInputData() smallSize = %zu, bigSize = %zu, capacity = %zu",
1413 smallSize, bigSize, mAggregateBuffer->capacity());
Phil Burk9f526492014-09-03 15:04:12 -07001414 }
1415 }
1416 } while (dropAccessUnit || needMoreData);
Andreas Huberf9334412010-12-15 15:17:42 -08001417
Steve Block3856b092011-10-20 11:56:00 +01001418 // ALOGV("returned a valid buffer of %s data", audio ? "audio" : "video");
Andreas Huberf9334412010-12-15 15:17:42 -08001419
1420#if 0
1421 int64_t mediaTimeUs;
1422 CHECK(accessUnit->meta()->findInt64("timeUs", &mediaTimeUs));
Steve Block3856b092011-10-20 11:56:00 +01001423 ALOGV("feeding %s input buffer at media time %.2f secs",
Andreas Huberf9334412010-12-15 15:17:42 -08001424 audio ? "audio" : "video",
1425 mediaTimeUs / 1E6);
1426#endif
1427
Chong Zhanga7fa1d92014-06-11 14:49:23 -07001428 if (!audio) {
1429 mCCDecoder->decode(accessUnit);
1430 }
1431
Phil Burk33b51b02014-09-17 16:03:47 -07001432 if (doBufferAggregation && (mAggregateBuffer != NULL)) {
Phil Burkc5cc2e22014-09-09 20:08:39 -07001433 ALOGV("feedDecoderInputData() reply with aggregated buffer, %zu",
1434 mAggregateBuffer->size());
1435 reply->setBuffer("buffer", mAggregateBuffer);
1436 mAggregateBuffer.clear();
Phil Burk9f526492014-09-03 15:04:12 -07001437 } else {
1438 reply->setBuffer("buffer", accessUnit);
1439 }
1440
Andreas Huberf9334412010-12-15 15:17:42 -08001441 reply->post();
1442
1443 return OK;
1444}
1445
1446void NuPlayer::renderBuffer(bool audio, const sp<AMessage> &msg) {
Steve Block3856b092011-10-20 11:56:00 +01001447 // ALOGV("renderBuffer %s", audio ? "audio" : "video");
Andreas Huberf9334412010-12-15 15:17:42 -08001448
1449 sp<AMessage> reply;
1450 CHECK(msg->findMessage("reply", &reply));
1451
Wei Jia53904f32014-07-29 10:22:53 -07001452 if ((audio && mFlushingAudio != NONE)
1453 || (!audio && mFlushingVideo != NONE)) {
Andreas Huber18ac5402011-08-31 15:04:25 -07001454 // We're currently attempting to flush the decoder, in order
1455 // to complete this, the decoder wants all its buffers back,
1456 // so we don't want any output buffers it sent us (from before
1457 // we initiated the flush) to be stuck in the renderer's queue.
1458
Steve Block3856b092011-10-20 11:56:00 +01001459 ALOGV("we're still flushing the %s decoder, sending its output buffer"
Andreas Huber18ac5402011-08-31 15:04:25 -07001460 " right back.", audio ? "audio" : "video");
1461
1462 reply->post();
1463 return;
1464 }
1465
Andreas Huber2d8bedd2012-02-21 14:38:23 -08001466 sp<ABuffer> buffer;
1467 CHECK(msg->findBuffer("buffer", &buffer));
Andreas Huberf9334412010-12-15 15:17:42 -08001468
Chong Zhanga7fa1d92014-06-11 14:49:23 -07001469 int64_t mediaTimeUs;
1470 CHECK(buffer->meta()->findInt64("timeUs", &mediaTimeUs));
1471
Andreas Huber32f3cef2011-03-02 15:34:46 -08001472 int64_t &skipUntilMediaTimeUs =
1473 audio
1474 ? mSkipRenderingAudioUntilMediaTimeUs
1475 : mSkipRenderingVideoUntilMediaTimeUs;
1476
1477 if (skipUntilMediaTimeUs >= 0) {
Andreas Huber32f3cef2011-03-02 15:34:46 -08001478
1479 if (mediaTimeUs < skipUntilMediaTimeUs) {
Steve Block3856b092011-10-20 11:56:00 +01001480 ALOGV("dropping %s buffer at time %lld as requested.",
Andreas Huber32f3cef2011-03-02 15:34:46 -08001481 audio ? "audio" : "video",
1482 mediaTimeUs);
1483
1484 reply->post();
1485 return;
1486 }
1487
1488 skipUntilMediaTimeUs = -1;
1489 }
1490
Chong Zhanga7fa1d92014-06-11 14:49:23 -07001491 if (!audio && mCCDecoder->isSelected()) {
1492 mCCDecoder->display(mediaTimeUs);
1493 }
1494
Andreas Huberf9334412010-12-15 15:17:42 -08001495 mRenderer->queueBuffer(audio, buffer, reply);
1496}
1497
Chong Zhangced1c2f2014-08-08 15:22:35 -07001498void NuPlayer::updateVideoSize(
1499 const sp<AMessage> &inputFormat,
1500 const sp<AMessage> &outputFormat) {
1501 if (inputFormat == NULL) {
1502 ALOGW("Unknown video size, reporting 0x0!");
1503 notifyListener(MEDIA_SET_VIDEO_SIZE, 0, 0);
1504 return;
1505 }
1506
1507 int32_t displayWidth, displayHeight;
1508 int32_t cropLeft, cropTop, cropRight, cropBottom;
1509
1510 if (outputFormat != NULL) {
1511 int32_t width, height;
1512 CHECK(outputFormat->findInt32("width", &width));
1513 CHECK(outputFormat->findInt32("height", &height));
1514
1515 int32_t cropLeft, cropTop, cropRight, cropBottom;
1516 CHECK(outputFormat->findRect(
1517 "crop",
1518 &cropLeft, &cropTop, &cropRight, &cropBottom));
1519
1520 displayWidth = cropRight - cropLeft + 1;
1521 displayHeight = cropBottom - cropTop + 1;
1522
1523 ALOGV("Video output format changed to %d x %d "
1524 "(crop: %d x %d @ (%d, %d))",
1525 width, height,
1526 displayWidth,
1527 displayHeight,
1528 cropLeft, cropTop);
1529 } else {
1530 CHECK(inputFormat->findInt32("width", &displayWidth));
1531 CHECK(inputFormat->findInt32("height", &displayHeight));
1532
1533 ALOGV("Video input format %d x %d", displayWidth, displayHeight);
1534 }
1535
1536 // Take into account sample aspect ratio if necessary:
1537 int32_t sarWidth, sarHeight;
1538 if (inputFormat->findInt32("sar-width", &sarWidth)
1539 && inputFormat->findInt32("sar-height", &sarHeight)) {
1540 ALOGV("Sample aspect ratio %d : %d", sarWidth, sarHeight);
1541
1542 displayWidth = (displayWidth * sarWidth) / sarHeight;
1543
1544 ALOGV("display dimensions %d x %d", displayWidth, displayHeight);
1545 }
1546
1547 int32_t rotationDegrees;
1548 if (!inputFormat->findInt32("rotation-degrees", &rotationDegrees)) {
1549 rotationDegrees = 0;
1550 }
1551
1552 if (rotationDegrees == 90 || rotationDegrees == 270) {
1553 int32_t tmp = displayWidth;
1554 displayWidth = displayHeight;
1555 displayHeight = tmp;
1556 }
1557
1558 notifyListener(
1559 MEDIA_SET_VIDEO_SIZE,
1560 displayWidth,
1561 displayHeight);
1562}
1563
Chong Zhangdcb89b32013-08-06 09:44:47 -07001564void NuPlayer::notifyListener(int msg, int ext1, int ext2, const Parcel *in) {
Andreas Huber43c3e6c2011-01-05 12:17:08 -08001565 if (mDriver == NULL) {
Andreas Huberf9334412010-12-15 15:17:42 -08001566 return;
1567 }
1568
Andreas Huber43c3e6c2011-01-05 12:17:08 -08001569 sp<NuPlayerDriver> driver = mDriver.promote();
Andreas Huberf9334412010-12-15 15:17:42 -08001570
Andreas Huber43c3e6c2011-01-05 12:17:08 -08001571 if (driver == NULL) {
Andreas Huberf9334412010-12-15 15:17:42 -08001572 return;
1573 }
1574
Chong Zhangdcb89b32013-08-06 09:44:47 -07001575 driver->notifyListener(msg, ext1, ext2, in);
Andreas Huberf9334412010-12-15 15:17:42 -08001576}
1577
Lajos Molnar87603c02014-08-20 19:25:30 -07001578void NuPlayer::flushDecoder(
1579 bool audio, bool needShutdown, const sp<AMessage> &newFormat) {
Andreas Huber14f76722013-01-15 09:04:18 -08001580 ALOGV("[%s] flushDecoder needShutdown=%d",
1581 audio ? "audio" : "video", needShutdown);
1582
Lajos Molnar87603c02014-08-20 19:25:30 -07001583 const sp<Decoder> &decoder = getDecoder(audio);
1584 if (decoder == NULL) {
Steve Blockdf64d152012-01-04 20:05:49 +00001585 ALOGI("flushDecoder %s without decoder present",
Andreas Huber6e3d3112011-11-28 12:36:11 -08001586 audio ? "audio" : "video");
Lajos Molnar87603c02014-08-20 19:25:30 -07001587 return;
Andreas Huber6e3d3112011-11-28 12:36:11 -08001588 }
1589
Andreas Huber1aef2112011-01-04 14:01:29 -08001590 // Make sure we don't continue to scan sources until we finish flushing.
1591 ++mScanSourcesGeneration;
Andreas Huber43c3e6c2011-01-05 12:17:08 -08001592 mScanSourcesPending = false;
Andreas Huber1aef2112011-01-04 14:01:29 -08001593
Lajos Molnar87603c02014-08-20 19:25:30 -07001594 decoder->signalFlush(newFormat);
Andreas Huber1aef2112011-01-04 14:01:29 -08001595 mRenderer->flush(audio);
1596
1597 FlushStatus newStatus =
1598 needShutdown ? FLUSHING_DECODER_SHUTDOWN : FLUSHING_DECODER;
1599
Andy Hung8d121d42014-10-03 09:53:53 -07001600 mFlushComplete[audio][false /* isDecoder */] = false;
1601 mFlushComplete[audio][true /* isDecoder */] = false;
Andreas Huber1aef2112011-01-04 14:01:29 -08001602 if (audio) {
Wei Jia53904f32014-07-29 10:22:53 -07001603 ALOGE_IF(mFlushingAudio != NONE,
1604 "audio flushDecoder() is called in state %d", mFlushingAudio);
Andreas Huber1aef2112011-01-04 14:01:29 -08001605 mFlushingAudio = newStatus;
Andreas Huber1aef2112011-01-04 14:01:29 -08001606 } else {
Wei Jia53904f32014-07-29 10:22:53 -07001607 ALOGE_IF(mFlushingVideo != NONE,
1608 "video flushDecoder() is called in state %d", mFlushingVideo);
Andreas Huber1aef2112011-01-04 14:01:29 -08001609 mFlushingVideo = newStatus;
Chong Zhangb86e68f2014-08-01 13:46:53 -07001610
1611 if (mCCDecoder != NULL) {
1612 mCCDecoder->flush();
1613 }
Andreas Huber1aef2112011-01-04 14:01:29 -08001614 }
1615}
1616
Lajos Molnar87603c02014-08-20 19:25:30 -07001617void NuPlayer::updateDecoderFormatWithoutFlush(
1618 bool audio, const sp<AMessage> &format) {
1619 ALOGV("[%s] updateDecoderFormatWithoutFlush", audio ? "audio" : "video");
1620
1621 const sp<Decoder> &decoder = getDecoder(audio);
1622 if (decoder == NULL) {
1623 ALOGI("updateDecoderFormatWithoutFlush %s without decoder present",
1624 audio ? "audio" : "video");
1625 return;
1626 }
1627
1628 decoder->signalUpdateFormat(format);
1629}
1630
Chong Zhangced1c2f2014-08-08 15:22:35 -07001631void NuPlayer::queueDecoderShutdown(
1632 bool audio, bool video, const sp<AMessage> &reply) {
1633 ALOGI("queueDecoderShutdown audio=%d, video=%d", audio, video);
Andreas Huber84066782011-08-16 09:34:26 -07001634
Chong Zhangced1c2f2014-08-08 15:22:35 -07001635 mDeferredActions.push_back(
1636 new ShutdownDecoderAction(audio, video));
Andreas Huber84066782011-08-16 09:34:26 -07001637
Chong Zhangced1c2f2014-08-08 15:22:35 -07001638 mDeferredActions.push_back(
1639 new SimpleAction(&NuPlayer::performScanSources));
Andreas Huber84066782011-08-16 09:34:26 -07001640
Chong Zhangced1c2f2014-08-08 15:22:35 -07001641 mDeferredActions.push_back(new PostMessageAction(reply));
1642
1643 processDeferredActions();
Andreas Huber84066782011-08-16 09:34:26 -07001644}
1645
James Dong0d268a32012-08-31 12:18:27 -07001646status_t NuPlayer::setVideoScalingMode(int32_t mode) {
1647 mVideoScalingMode = mode;
Andreas Huber57a339c2012-12-03 11:18:00 -08001648 if (mNativeWindow != NULL) {
James Dong0d268a32012-08-31 12:18:27 -07001649 status_t ret = native_window_set_scaling_mode(
1650 mNativeWindow->getNativeWindow().get(), mVideoScalingMode);
1651 if (ret != OK) {
1652 ALOGE("Failed to set scaling mode (%d): %s",
1653 -ret, strerror(-ret));
1654 return ret;
1655 }
1656 }
1657 return OK;
1658}
1659
Chong Zhangdcb89b32013-08-06 09:44:47 -07001660status_t NuPlayer::getTrackInfo(Parcel* reply) const {
1661 sp<AMessage> msg = new AMessage(kWhatGetTrackInfo, id());
1662 msg->setPointer("reply", reply);
1663
1664 sp<AMessage> response;
1665 status_t err = msg->postAndAwaitResponse(&response);
1666 return err;
1667}
1668
Robert Shih7c4f0d72014-07-09 18:53:31 -07001669status_t NuPlayer::getSelectedTrack(int32_t type, Parcel* reply) const {
1670 sp<AMessage> msg = new AMessage(kWhatGetSelectedTrack, id());
1671 msg->setPointer("reply", reply);
1672 msg->setInt32("type", type);
1673
1674 sp<AMessage> response;
1675 status_t err = msg->postAndAwaitResponse(&response);
1676 if (err == OK && response != NULL) {
1677 CHECK(response->findInt32("err", &err));
1678 }
1679 return err;
1680}
1681
Chong Zhangdcb89b32013-08-06 09:44:47 -07001682status_t NuPlayer::selectTrack(size_t trackIndex, bool select) {
1683 sp<AMessage> msg = new AMessage(kWhatSelectTrack, id());
1684 msg->setSize("trackIndex", trackIndex);
1685 msg->setInt32("select", select);
1686
1687 sp<AMessage> response;
1688 status_t err = msg->postAndAwaitResponse(&response);
1689
Chong Zhang404fced2014-06-11 14:45:31 -07001690 if (err != OK) {
1691 return err;
1692 }
1693
1694 if (!response->findInt32("err", &err)) {
1695 err = OK;
1696 }
1697
Chong Zhangdcb89b32013-08-06 09:44:47 -07001698 return err;
1699}
1700
Ronghua Wua73d9e02014-10-08 15:13:29 -07001701status_t NuPlayer::getCurrentPosition(int64_t *mediaUs) {
1702 sp<Renderer> renderer = mRenderer;
1703 if (renderer == NULL) {
1704 return NO_INIT;
1705 }
1706
1707 return renderer->getCurrentPosition(mediaUs);
1708}
1709
1710void NuPlayer::getStats(int64_t *numFramesTotal, int64_t *numFramesDropped) {
1711 *numFramesTotal = mNumFramesTotal;
1712 *numFramesDropped = mNumFramesDropped;
1713}
1714
Marco Nelissenf0b72b52014-09-16 15:43:44 -07001715sp<MetaData> NuPlayer::getFileMeta() {
1716 return mSource->getFileFormatMeta();
1717}
1718
Andreas Huberb7c8e912012-11-27 15:02:53 -08001719void NuPlayer::schedulePollDuration() {
1720 sp<AMessage> msg = new AMessage(kWhatPollDuration, id());
1721 msg->setInt32("generation", mPollDurationGeneration);
1722 msg->post();
1723}
1724
1725void NuPlayer::cancelPollDuration() {
1726 ++mPollDurationGeneration;
1727}
1728
Andreas Hubera1f8ab02012-11-30 10:53:22 -08001729void NuPlayer::processDeferredActions() {
1730 while (!mDeferredActions.empty()) {
1731 // We won't execute any deferred actions until we're no longer in
1732 // an intermediate state, i.e. one more more decoders are currently
1733 // flushing or shutting down.
1734
Andreas Hubera1f8ab02012-11-30 10:53:22 -08001735 if (mFlushingAudio != NONE || mFlushingVideo != NONE) {
1736 // We're currently flushing, postpone the reset until that's
1737 // completed.
1738
1739 ALOGV("postponing action mFlushingAudio=%d, mFlushingVideo=%d",
1740 mFlushingAudio, mFlushingVideo);
1741
1742 break;
1743 }
1744
1745 sp<Action> action = *mDeferredActions.begin();
1746 mDeferredActions.erase(mDeferredActions.begin());
1747
1748 action->execute(this);
1749 }
1750}
1751
Wei Jiae427abf2014-09-22 15:21:11 -07001752void NuPlayer::performSeek(int64_t seekTimeUs, bool needNotify) {
1753 ALOGV("performSeek seekTimeUs=%lld us (%.2f secs), needNotify(%d)",
Andreas Hubera1f8ab02012-11-30 10:53:22 -08001754 seekTimeUs,
Wei Jiae427abf2014-09-22 15:21:11 -07001755 seekTimeUs / 1E6,
1756 needNotify);
Andreas Hubera1f8ab02012-11-30 10:53:22 -08001757
Andy Hungadf34bf2014-09-03 18:22:22 -07001758 if (mSource == NULL) {
1759 // This happens when reset occurs right before the loop mode
1760 // asynchronously seeks to the start of the stream.
1761 LOG_ALWAYS_FATAL_IF(mAudioDecoder != NULL || mVideoDecoder != NULL,
1762 "mSource is NULL and decoders not NULL audio(%p) video(%p)",
1763 mAudioDecoder.get(), mVideoDecoder.get());
1764 return;
1765 }
Andreas Hubera1f8ab02012-11-30 10:53:22 -08001766 mSource->seekTo(seekTimeUs);
Robert Shihd3b0bbb2014-07-23 15:00:25 -07001767 ++mTimedTextGeneration;
Andreas Hubera1f8ab02012-11-30 10:53:22 -08001768
1769 if (mDriver != NULL) {
1770 sp<NuPlayerDriver> driver = mDriver.promote();
1771 if (driver != NULL) {
Wei Jiae427abf2014-09-22 15:21:11 -07001772 if (needNotify) {
1773 driver->notifySeekComplete();
1774 }
Andreas Hubera1f8ab02012-11-30 10:53:22 -08001775 }
1776 }
1777
1778 // everything's flushed, continue playback.
1779}
1780
1781void NuPlayer::performDecoderFlush() {
1782 ALOGV("performDecoderFlush");
1783
Andreas Huberda9740e2013-04-16 10:54:03 -07001784 if (mAudioDecoder == NULL && mVideoDecoder == NULL) {
Andreas Hubera1f8ab02012-11-30 10:53:22 -08001785 return;
1786 }
1787
1788 mTimeDiscontinuityPending = true;
1789
1790 if (mAudioDecoder != NULL) {
1791 flushDecoder(true /* audio */, false /* needShutdown */);
1792 }
1793
1794 if (mVideoDecoder != NULL) {
1795 flushDecoder(false /* audio */, false /* needShutdown */);
1796 }
1797}
1798
Andreas Huber14f76722013-01-15 09:04:18 -08001799void NuPlayer::performDecoderShutdown(bool audio, bool video) {
1800 ALOGV("performDecoderShutdown audio=%d, video=%d", audio, video);
Andreas Hubera1f8ab02012-11-30 10:53:22 -08001801
Andreas Huber14f76722013-01-15 09:04:18 -08001802 if ((!audio || mAudioDecoder == NULL)
1803 && (!video || mVideoDecoder == NULL)) {
Andreas Hubera1f8ab02012-11-30 10:53:22 -08001804 return;
1805 }
1806
1807 mTimeDiscontinuityPending = true;
1808
Andreas Huber14f76722013-01-15 09:04:18 -08001809 if (audio && mAudioDecoder != NULL) {
Andreas Hubera1f8ab02012-11-30 10:53:22 -08001810 flushDecoder(true /* audio */, true /* needShutdown */);
1811 }
1812
Andreas Huber14f76722013-01-15 09:04:18 -08001813 if (video && mVideoDecoder != NULL) {
Andreas Hubera1f8ab02012-11-30 10:53:22 -08001814 flushDecoder(false /* audio */, true /* needShutdown */);
1815 }
1816}
1817
1818void NuPlayer::performReset() {
1819 ALOGV("performReset");
1820
1821 CHECK(mAudioDecoder == NULL);
1822 CHECK(mVideoDecoder == NULL);
1823
1824 cancelPollDuration();
1825
1826 ++mScanSourcesGeneration;
1827 mScanSourcesPending = false;
1828
Lajos Molnar09524832014-07-17 14:29:51 -07001829 if (mRendererLooper != NULL) {
1830 if (mRenderer != NULL) {
1831 mRendererLooper->unregisterHandler(mRenderer->id());
1832 }
1833 mRendererLooper->stop();
1834 mRendererLooper.clear();
1835 }
Andreas Hubera1f8ab02012-11-30 10:53:22 -08001836 mRenderer.clear();
Wei Jia57568df2014-09-22 10:16:29 -07001837 ++mRendererGeneration;
Andreas Hubera1f8ab02012-11-30 10:53:22 -08001838
1839 if (mSource != NULL) {
1840 mSource->stop();
Andreas Huberb5f25f02013-02-05 10:14:26 -08001841
Andreas Hubera1f8ab02012-11-30 10:53:22 -08001842 mSource.clear();
1843 }
1844
1845 if (mDriver != NULL) {
1846 sp<NuPlayerDriver> driver = mDriver.promote();
1847 if (driver != NULL) {
1848 driver->notifyResetComplete();
1849 }
1850 }
Andreas Huber57a339c2012-12-03 11:18:00 -08001851
1852 mStarted = false;
Andreas Hubera1f8ab02012-11-30 10:53:22 -08001853}
1854
1855void NuPlayer::performScanSources() {
1856 ALOGV("performScanSources");
1857
Andreas Huber57a339c2012-12-03 11:18:00 -08001858 if (!mStarted) {
1859 return;
1860 }
1861
Andreas Hubera1f8ab02012-11-30 10:53:22 -08001862 if (mAudioDecoder == NULL || mVideoDecoder == NULL) {
1863 postScanSources();
1864 }
1865}
1866
Andreas Huber57a339c2012-12-03 11:18:00 -08001867void NuPlayer::performSetSurface(const sp<NativeWindowWrapper> &wrapper) {
1868 ALOGV("performSetSurface");
1869
1870 mNativeWindow = wrapper;
1871
1872 // XXX - ignore error from setVideoScalingMode for now
1873 setVideoScalingMode(mVideoScalingMode);
Chong Zhang13d6faa2014-08-22 15:35:28 -07001874
1875 if (mDriver != NULL) {
1876 sp<NuPlayerDriver> driver = mDriver.promote();
1877 if (driver != NULL) {
1878 driver->notifySetSurfaceComplete();
1879 }
1880 }
Andreas Huber57a339c2012-12-03 11:18:00 -08001881}
1882
Andreas Huber9575c962013-02-05 13:59:56 -08001883void NuPlayer::onSourceNotify(const sp<AMessage> &msg) {
1884 int32_t what;
1885 CHECK(msg->findInt32("what", &what));
1886
1887 switch (what) {
1888 case Source::kWhatPrepared:
1889 {
Andreas Huberb5f28d42013-04-25 15:11:19 -07001890 if (mSource == NULL) {
1891 // This is a stale notification from a source that was
1892 // asynchronously preparing when the client called reset().
1893 // We handled the reset, the source is gone.
1894 break;
1895 }
1896
Andreas Huberec0c5972013-02-05 14:47:13 -08001897 int32_t err;
1898 CHECK(msg->findInt32("err", &err));
1899
Andreas Huber9575c962013-02-05 13:59:56 -08001900 sp<NuPlayerDriver> driver = mDriver.promote();
1901 if (driver != NULL) {
Marco Nelissendd114d12014-05-28 15:23:14 -07001902 // notify duration first, so that it's definitely set when
1903 // the app received the "prepare complete" callback.
1904 int64_t durationUs;
1905 if (mSource->getDuration(&durationUs) == OK) {
1906 driver->notifyDuration(durationUs);
1907 }
Andreas Huberec0c5972013-02-05 14:47:13 -08001908 driver->notifyPrepareCompleted(err);
Andreas Huber9575c962013-02-05 13:59:56 -08001909 }
Andreas Huber99759402013-04-01 14:28:31 -07001910
Andreas Huber9575c962013-02-05 13:59:56 -08001911 break;
1912 }
1913
1914 case Source::kWhatFlagsChanged:
1915 {
1916 uint32_t flags;
1917 CHECK(msg->findInt32("flags", (int32_t *)&flags));
1918
Chong Zhang4b7069d2013-09-11 12:52:43 -07001919 sp<NuPlayerDriver> driver = mDriver.promote();
1920 if (driver != NULL) {
1921 driver->notifyFlagsChanged(flags);
1922 }
1923
Andreas Huber9575c962013-02-05 13:59:56 -08001924 if ((mSourceFlags & Source::FLAG_DYNAMIC_DURATION)
1925 && (!(flags & Source::FLAG_DYNAMIC_DURATION))) {
1926 cancelPollDuration();
1927 } else if (!(mSourceFlags & Source::FLAG_DYNAMIC_DURATION)
1928 && (flags & Source::FLAG_DYNAMIC_DURATION)
1929 && (mAudioDecoder != NULL || mVideoDecoder != NULL)) {
1930 schedulePollDuration();
1931 }
1932
1933 mSourceFlags = flags;
1934 break;
1935 }
1936
1937 case Source::kWhatVideoSizeChanged:
1938 {
Chong Zhangced1c2f2014-08-08 15:22:35 -07001939 sp<AMessage> format;
1940 CHECK(msg->findMessage("format", &format));
Andreas Huber9575c962013-02-05 13:59:56 -08001941
Chong Zhangced1c2f2014-08-08 15:22:35 -07001942 updateVideoSize(format);
Andreas Huber9575c962013-02-05 13:59:56 -08001943 break;
1944 }
1945
Chong Zhang2a3cc9a2014-08-21 17:48:26 -07001946 case Source::kWhatBufferingUpdate:
1947 {
1948 int32_t percentage;
1949 CHECK(msg->findInt32("percentage", &percentage));
1950
1951 notifyListener(MEDIA_BUFFERING_UPDATE, percentage, 0);
1952 break;
1953 }
1954
Roger Jönssonb50e83e2013-01-21 16:26:41 +01001955 case Source::kWhatBufferingStart:
1956 {
1957 notifyListener(MEDIA_INFO, MEDIA_INFO_BUFFERING_START, 0);
1958 break;
1959 }
1960
1961 case Source::kWhatBufferingEnd:
1962 {
1963 notifyListener(MEDIA_INFO, MEDIA_INFO_BUFFERING_END, 0);
1964 break;
1965 }
1966
Chong Zhangdcb89b32013-08-06 09:44:47 -07001967 case Source::kWhatSubtitleData:
1968 {
1969 sp<ABuffer> buffer;
1970 CHECK(msg->findBuffer("buffer", &buffer));
1971
Chong Zhang404fced2014-06-11 14:45:31 -07001972 sendSubtitleData(buffer, 0 /* baseIndex */);
Chong Zhangdcb89b32013-08-06 09:44:47 -07001973 break;
1974 }
1975
Robert Shihd3b0bbb2014-07-23 15:00:25 -07001976 case Source::kWhatTimedTextData:
1977 {
1978 int32_t generation;
1979 if (msg->findInt32("generation", &generation)
1980 && generation != mTimedTextGeneration) {
1981 break;
1982 }
1983
1984 sp<ABuffer> buffer;
1985 CHECK(msg->findBuffer("buffer", &buffer));
1986
1987 sp<NuPlayerDriver> driver = mDriver.promote();
1988 if (driver == NULL) {
1989 break;
1990 }
1991
1992 int posMs;
1993 int64_t timeUs, posUs;
1994 driver->getCurrentPosition(&posMs);
1995 posUs = posMs * 1000;
1996 CHECK(buffer->meta()->findInt64("timeUs", &timeUs));
1997
1998 if (posUs < timeUs) {
1999 if (!msg->findInt32("generation", &generation)) {
2000 msg->setInt32("generation", mTimedTextGeneration);
2001 }
2002 msg->post(timeUs - posUs);
2003 } else {
2004 sendTimedTextData(buffer);
2005 }
2006 break;
2007 }
2008
Andreas Huber14f76722013-01-15 09:04:18 -08002009 case Source::kWhatQueueDecoderShutdown:
2010 {
2011 int32_t audio, video;
2012 CHECK(msg->findInt32("audio", &audio));
2013 CHECK(msg->findInt32("video", &video));
2014
2015 sp<AMessage> reply;
2016 CHECK(msg->findMessage("reply", &reply));
2017
2018 queueDecoderShutdown(audio, video, reply);
2019 break;
2020 }
2021
Ronghua Wu80276872014-08-28 15:50:29 -07002022 case Source::kWhatDrmNoLicense:
2023 {
2024 notifyListener(MEDIA_ERROR, MEDIA_ERROR_UNKNOWN, ERROR_DRM_NO_LICENSE);
2025 break;
2026 }
2027
Andreas Huber9575c962013-02-05 13:59:56 -08002028 default:
2029 TRESPASS();
2030 }
2031}
2032
Chong Zhanga7fa1d92014-06-11 14:49:23 -07002033void NuPlayer::onClosedCaptionNotify(const sp<AMessage> &msg) {
2034 int32_t what;
2035 CHECK(msg->findInt32("what", &what));
2036
2037 switch (what) {
2038 case NuPlayer::CCDecoder::kWhatClosedCaptionData:
2039 {
2040 sp<ABuffer> buffer;
2041 CHECK(msg->findBuffer("buffer", &buffer));
2042
2043 size_t inbandTracks = 0;
2044 if (mSource != NULL) {
2045 inbandTracks = mSource->getTrackCount();
2046 }
2047
2048 sendSubtitleData(buffer, inbandTracks);
2049 break;
2050 }
2051
2052 case NuPlayer::CCDecoder::kWhatTrackAdded:
2053 {
2054 notifyListener(MEDIA_INFO, MEDIA_INFO_METADATA_UPDATE, 0);
2055
2056 break;
2057 }
2058
2059 default:
2060 TRESPASS();
2061 }
2062
2063
2064}
2065
Chong Zhang404fced2014-06-11 14:45:31 -07002066void NuPlayer::sendSubtitleData(const sp<ABuffer> &buffer, int32_t baseIndex) {
2067 int32_t trackIndex;
2068 int64_t timeUs, durationUs;
2069 CHECK(buffer->meta()->findInt32("trackIndex", &trackIndex));
2070 CHECK(buffer->meta()->findInt64("timeUs", &timeUs));
2071 CHECK(buffer->meta()->findInt64("durationUs", &durationUs));
2072
2073 Parcel in;
2074 in.writeInt32(trackIndex + baseIndex);
2075 in.writeInt64(timeUs);
2076 in.writeInt64(durationUs);
2077 in.writeInt32(buffer->size());
2078 in.writeInt32(buffer->size());
2079 in.write(buffer->data(), buffer->size());
2080
2081 notifyListener(MEDIA_SUBTITLE_DATA, 0, 0, &in);
2082}
Robert Shihd3b0bbb2014-07-23 15:00:25 -07002083
2084void NuPlayer::sendTimedTextData(const sp<ABuffer> &buffer) {
2085 const void *data;
2086 size_t size = 0;
2087 int64_t timeUs;
2088 int32_t flag = TextDescriptions::LOCAL_DESCRIPTIONS;
2089
2090 AString mime;
2091 CHECK(buffer->meta()->findString("mime", &mime));
2092 CHECK(strcasecmp(mime.c_str(), MEDIA_MIMETYPE_TEXT_3GPP) == 0);
2093
2094 data = buffer->data();
2095 size = buffer->size();
2096
2097 Parcel parcel;
2098 if (size > 0) {
2099 CHECK(buffer->meta()->findInt64("timeUs", &timeUs));
2100 flag |= TextDescriptions::IN_BAND_TEXT_3GPP;
2101 TextDescriptions::getParcelOfDescriptions(
2102 (const uint8_t *)data, size, flag, timeUs / 1000, &parcel);
2103 }
2104
2105 if ((parcel.dataSize() > 0)) {
2106 notifyListener(MEDIA_TIMED_TEXT, 0, 0, &parcel);
2107 } else { // send an empty timed text
2108 notifyListener(MEDIA_TIMED_TEXT, 0, 0);
2109 }
2110}
Andreas Huberb5f25f02013-02-05 10:14:26 -08002111////////////////////////////////////////////////////////////////////////////////
2112
Chong Zhangced1c2f2014-08-08 15:22:35 -07002113sp<AMessage> NuPlayer::Source::getFormat(bool audio) {
2114 sp<MetaData> meta = getFormatMeta(audio);
2115
2116 if (meta == NULL) {
2117 return NULL;
2118 }
2119
2120 sp<AMessage> msg = new AMessage;
2121
2122 if(convertMetaDataToMessage(meta, &msg) == OK) {
2123 return msg;
2124 }
2125 return NULL;
2126}
2127
Andreas Huber9575c962013-02-05 13:59:56 -08002128void NuPlayer::Source::notifyFlagsChanged(uint32_t flags) {
2129 sp<AMessage> notify = dupNotify();
2130 notify->setInt32("what", kWhatFlagsChanged);
2131 notify->setInt32("flags", flags);
2132 notify->post();
2133}
2134
Chong Zhangced1c2f2014-08-08 15:22:35 -07002135void NuPlayer::Source::notifyVideoSizeChanged(const sp<AMessage> &format) {
Andreas Huber9575c962013-02-05 13:59:56 -08002136 sp<AMessage> notify = dupNotify();
2137 notify->setInt32("what", kWhatVideoSizeChanged);
Chong Zhangced1c2f2014-08-08 15:22:35 -07002138 notify->setMessage("format", format);
Andreas Huber9575c962013-02-05 13:59:56 -08002139 notify->post();
2140}
2141
Andreas Huberec0c5972013-02-05 14:47:13 -08002142void NuPlayer::Source::notifyPrepared(status_t err) {
Andreas Huber9575c962013-02-05 13:59:56 -08002143 sp<AMessage> notify = dupNotify();
2144 notify->setInt32("what", kWhatPrepared);
Andreas Huberec0c5972013-02-05 14:47:13 -08002145 notify->setInt32("err", err);
Andreas Huber9575c962013-02-05 13:59:56 -08002146 notify->post();
2147}
2148
Andreas Huber84333e02014-02-07 15:36:10 -08002149void NuPlayer::Source::onMessageReceived(const sp<AMessage> & /* msg */) {
Andreas Huberb5f25f02013-02-05 10:14:26 -08002150 TRESPASS();
2151}
2152
Andreas Huberf9334412010-12-15 15:17:42 -08002153} // namespace android