blob: b8786d18dabb5c49eb0337163a3822e80d98973d [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
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;
458 CHECK(msg->findSize("trackIndex", &trackIndex));
459 CHECK(msg->findInt32("select", &select));
460
Chong Zhangdcb89b32013-08-06 09:44:47 -0700461 status_t err = INVALID_OPERATION;
Chong Zhang404fced2014-06-11 14:45:31 -0700462
463 size_t inbandTracks = 0;
Chong Zhangdcb89b32013-08-06 09:44:47 -0700464 if (mSource != NULL) {
Chong Zhang404fced2014-06-11 14:45:31 -0700465 inbandTracks = mSource->getTrackCount();
466 }
Chong Zhanga7fa1d92014-06-11 14:49:23 -0700467 size_t ccTracks = 0;
468 if (mCCDecoder != NULL) {
469 ccTracks = mCCDecoder->getTrackCount();
470 }
Chong Zhang404fced2014-06-11 14:45:31 -0700471
472 if (trackIndex < inbandTracks) {
Chong Zhangdcb89b32013-08-06 09:44:47 -0700473 err = mSource->selectTrack(trackIndex, select);
Robert Shihd3b0bbb2014-07-23 15:00:25 -0700474
475 if (!select && err == OK) {
476 int32_t type;
477 sp<AMessage> info = mSource->getTrackInfo(trackIndex);
478 if (info != NULL
479 && info->findInt32("type", &type)
480 && type == MEDIA_TRACK_TYPE_TIMEDTEXT) {
481 ++mTimedTextGeneration;
482 }
483 }
Chong Zhanga7fa1d92014-06-11 14:49:23 -0700484 } else {
485 trackIndex -= inbandTracks;
486
487 if (trackIndex < ccTracks) {
488 err = mCCDecoder->selectTrack(trackIndex, select);
489 }
Chong Zhangdcb89b32013-08-06 09:44:47 -0700490 }
491
492 sp<AMessage> response = new AMessage;
493 response->setInt32("err", err);
494
495 response->postReply(replyID);
496 break;
497 }
498
Andreas Huberb7c8e912012-11-27 15:02:53 -0800499 case kWhatPollDuration:
500 {
501 int32_t generation;
502 CHECK(msg->findInt32("generation", &generation));
503
504 if (generation != mPollDurationGeneration) {
505 // stale
506 break;
507 }
508
509 int64_t durationUs;
510 if (mDriver != NULL && mSource->getDuration(&durationUs) == OK) {
511 sp<NuPlayerDriver> driver = mDriver.promote();
512 if (driver != NULL) {
513 driver->notifyDuration(durationUs);
514 }
515 }
516
517 msg->post(1000000ll); // poll again in a second.
518 break;
519 }
520
Glenn Kasten11731182011-02-08 17:26:17 -0800521 case kWhatSetVideoNativeWindow:
Andreas Huberf9334412010-12-15 15:17:42 -0800522 {
Steve Block3856b092011-10-20 11:56:00 +0100523 ALOGV("kWhatSetVideoNativeWindow");
Andreas Huberf9334412010-12-15 15:17:42 -0800524
Andreas Huber57a339c2012-12-03 11:18:00 -0800525 mDeferredActions.push_back(
Andreas Huber14f76722013-01-15 09:04:18 -0800526 new ShutdownDecoderAction(
527 false /* audio */, true /* video */));
Andreas Huber57a339c2012-12-03 11:18:00 -0800528
Andreas Huberf9334412010-12-15 15:17:42 -0800529 sp<RefBase> obj;
Glenn Kasten11731182011-02-08 17:26:17 -0800530 CHECK(msg->findObject("native-window", &obj));
Andreas Huberf9334412010-12-15 15:17:42 -0800531
Andreas Huber57a339c2012-12-03 11:18:00 -0800532 mDeferredActions.push_back(
533 new SetSurfaceAction(
534 static_cast<NativeWindowWrapper *>(obj.get())));
James Dong0d268a32012-08-31 12:18:27 -0700535
Andreas Huber57a339c2012-12-03 11:18:00 -0800536 if (obj != NULL) {
Wei Jia36f38982014-09-25 15:27:04 -0700537 if (mStarted && mSource->getFormat(false /* audio */) != NULL) {
Andy Hung73535852014-09-05 11:42:58 -0700538 // Issue a seek to refresh the video screen only if started otherwise
539 // the extractor may not yet be started and will assert.
540 // If the video decoder is not set (perhaps audio only in this case)
541 // do not perform a seek as it is not needed.
Ronghua Wua73d9e02014-10-08 15:13:29 -0700542 int64_t currentPositionUs = 0;
543 if (getCurrentPosition(&currentPositionUs) == OK) {
544 mDeferredActions.push_back(
545 new SeekAction(currentPositionUs, false /* needNotify */));
546 }
Andy Hung73535852014-09-05 11:42:58 -0700547 }
Wei Jiaac428aa2014-09-02 19:01:34 -0700548
Andreas Huber57a339c2012-12-03 11:18:00 -0800549 // If there is a new surface texture, instantiate decoders
550 // again if possible.
551 mDeferredActions.push_back(
552 new SimpleAction(&NuPlayer::performScanSources));
553 }
554
555 processDeferredActions();
Andreas Huberf9334412010-12-15 15:17:42 -0800556 break;
557 }
558
559 case kWhatSetAudioSink:
560 {
Steve Block3856b092011-10-20 11:56:00 +0100561 ALOGV("kWhatSetAudioSink");
Andreas Huberf9334412010-12-15 15:17:42 -0800562
563 sp<RefBase> obj;
564 CHECK(msg->findObject("sink", &obj));
565
566 mAudioSink = static_cast<MediaPlayerBase::AudioSink *>(obj.get());
567 break;
568 }
569
570 case kWhatStart:
571 {
Steve Block3856b092011-10-20 11:56:00 +0100572 ALOGV("kWhatStart");
Wei Jia94211742014-10-28 17:09:06 -0700573 if (mStarted) {
574 onResume();
575 } else {
576 onStart();
Lajos Molnar09524832014-07-17 14:29:51 -0700577 }
Andreas Huberf9334412010-12-15 15:17:42 -0800578 break;
579 }
580
581 case kWhatScanSources:
582 {
Andreas Huber1aef2112011-01-04 14:01:29 -0800583 int32_t generation;
584 CHECK(msg->findInt32("generation", &generation));
585 if (generation != mScanSourcesGeneration) {
586 // Drop obsolete msg.
587 break;
588 }
589
Andreas Huber5bc087c2010-12-23 10:27:40 -0800590 mScanSourcesPending = false;
591
Steve Block3856b092011-10-20 11:56:00 +0100592 ALOGV("scanning sources haveAudio=%d, haveVideo=%d",
Andreas Huber43c3e6c2011-01-05 12:17:08 -0800593 mAudioDecoder != NULL, mVideoDecoder != NULL);
594
Andreas Huberb7c8e912012-11-27 15:02:53 -0800595 bool mHadAnySourcesBefore =
596 (mAudioDecoder != NULL) || (mVideoDecoder != NULL);
597
Andy Hung282a7e32014-08-14 15:56:34 -0700598 // initialize video before audio because successful initialization of
599 // video may change deep buffer mode of audio.
Haynes Mathew George5d246ef2012-07-09 10:36:57 -0700600 if (mNativeWindow != NULL) {
601 instantiateDecoder(false, &mVideoDecoder);
602 }
Andreas Huberf9334412010-12-15 15:17:42 -0800603
604 if (mAudioSink != NULL) {
Andy Hung282a7e32014-08-14 15:56:34 -0700605 if (mOffloadAudio) {
606 // open audio sink early under offload mode.
607 sp<AMessage> format = mSource->getFormat(true /*audio*/);
608 openAudioSink(format, true /*offloadOnly*/);
609 }
Andreas Huber5bc087c2010-12-23 10:27:40 -0800610 instantiateDecoder(true, &mAudioDecoder);
Andreas Huberf9334412010-12-15 15:17:42 -0800611 }
612
Andreas Huberb7c8e912012-11-27 15:02:53 -0800613 if (!mHadAnySourcesBefore
614 && (mAudioDecoder != NULL || mVideoDecoder != NULL)) {
615 // This is the first time we've found anything playable.
616
Andreas Huber9575c962013-02-05 13:59:56 -0800617 if (mSourceFlags & Source::FLAG_DYNAMIC_DURATION) {
Andreas Huberb7c8e912012-11-27 15:02:53 -0800618 schedulePollDuration();
619 }
620 }
621
Andreas Hubereac68ba2011-09-27 12:12:25 -0700622 status_t err;
623 if ((err = mSource->feedMoreTSData()) != OK) {
Andreas Huber1aef2112011-01-04 14:01:29 -0800624 if (mAudioDecoder == NULL && mVideoDecoder == NULL) {
625 // We're not currently decoding anything (no audio or
626 // video tracks found) and we just ran out of input data.
Andreas Hubereac68ba2011-09-27 12:12:25 -0700627
628 if (err == ERROR_END_OF_STREAM) {
629 notifyListener(MEDIA_PLAYBACK_COMPLETE, 0, 0);
630 } else {
631 notifyListener(MEDIA_ERROR, MEDIA_ERROR_UNKNOWN, err);
632 }
Andreas Huber1aef2112011-01-04 14:01:29 -0800633 }
Andreas Huberf9334412010-12-15 15:17:42 -0800634 break;
635 }
636
Andreas Huberfbe9d812012-08-31 14:05:27 -0700637 if ((mAudioDecoder == NULL && mAudioSink != NULL)
638 || (mVideoDecoder == NULL && mNativeWindow != NULL)) {
Andreas Huberf9334412010-12-15 15:17:42 -0800639 msg->post(100000ll);
Andreas Huber5bc087c2010-12-23 10:27:40 -0800640 mScanSourcesPending = true;
Andreas Huberf9334412010-12-15 15:17:42 -0800641 }
642 break;
643 }
644
645 case kWhatVideoNotify:
646 case kWhatAudioNotify:
647 {
648 bool audio = msg->what() == kWhatAudioNotify;
649
Wei Jia88703c32014-08-06 11:24:07 -0700650 int32_t currentDecoderGeneration =
651 (audio? mAudioDecoderGeneration : mVideoDecoderGeneration);
652 int32_t requesterGeneration = currentDecoderGeneration - 1;
653 CHECK(msg->findInt32("generation", &requesterGeneration));
654
655 if (requesterGeneration != currentDecoderGeneration) {
656 ALOGV("got message from old %s decoder, generation(%d:%d)",
657 audio ? "audio" : "video", requesterGeneration,
658 currentDecoderGeneration);
659 sp<AMessage> reply;
660 if (!(msg->findMessage("reply", &reply))) {
661 return;
662 }
663
664 reply->setInt32("err", INFO_DISCONTINUITY);
665 reply->post();
666 return;
667 }
668
Andreas Huberf9334412010-12-15 15:17:42 -0800669 int32_t what;
Lajos Molnar1cd13982014-01-17 15:12:51 -0800670 CHECK(msg->findInt32("what", &what));
Andreas Huberf9334412010-12-15 15:17:42 -0800671
Lajos Molnar1cd13982014-01-17 15:12:51 -0800672 if (what == Decoder::kWhatFillThisBuffer) {
Andreas Huberf9334412010-12-15 15:17:42 -0800673 status_t err = feedDecoderInputData(
Lajos Molnar1cd13982014-01-17 15:12:51 -0800674 audio, msg);
Andreas Huberf9334412010-12-15 15:17:42 -0800675
Andreas Huber5bc087c2010-12-23 10:27:40 -0800676 if (err == -EWOULDBLOCK) {
Andreas Hubereac68ba2011-09-27 12:12:25 -0700677 if (mSource->feedMoreTSData() == OK) {
Phil Burkc5cc2e22014-09-09 20:08:39 -0700678 msg->post(10 * 1000ll);
Andreas Huber5bc087c2010-12-23 10:27:40 -0800679 }
Andreas Huberf9334412010-12-15 15:17:42 -0800680 }
Lajos Molnar1cd13982014-01-17 15:12:51 -0800681 } else if (what == Decoder::kWhatEOS) {
Andreas Huberdc9bacd2011-09-26 10:53:29 -0700682 int32_t err;
Lajos Molnar1cd13982014-01-17 15:12:51 -0800683 CHECK(msg->findInt32("err", &err));
Andreas Huberdc9bacd2011-09-26 10:53:29 -0700684
685 if (err == ERROR_END_OF_STREAM) {
Steve Block3856b092011-10-20 11:56:00 +0100686 ALOGV("got %s decoder EOS", audio ? "audio" : "video");
Andreas Huberdc9bacd2011-09-26 10:53:29 -0700687 } else {
Steve Block3856b092011-10-20 11:56:00 +0100688 ALOGV("got %s decoder EOS w/ error %d",
Andreas Huberdc9bacd2011-09-26 10:53:29 -0700689 audio ? "audio" : "video",
690 err);
691 }
692
693 mRenderer->queueEOS(audio, err);
Lajos Molnar1cd13982014-01-17 15:12:51 -0800694 } else if (what == Decoder::kWhatFlushCompleted) {
Steve Block3856b092011-10-20 11:56:00 +0100695 ALOGV("decoder %s flush completed", audio ? "audio" : "video");
Andreas Huberf9334412010-12-15 15:17:42 -0800696
Andy Hung8d121d42014-10-03 09:53:53 -0700697 handleFlushComplete(audio, true /* isDecoder */);
Andreas Huber3831a062010-12-21 10:22:33 -0800698 finishFlushIfPossible();
Lajos Molnar1cd13982014-01-17 15:12:51 -0800699 } else if (what == Decoder::kWhatOutputFormatChanged) {
700 sp<AMessage> format;
701 CHECK(msg->findMessage("format", &format));
702
Andreas Huber31e25082011-01-10 10:38:31 -0800703 if (audio) {
Andy Hung282a7e32014-08-14 15:56:34 -0700704 openAudioSink(format, false /*offloadOnly*/);
Andreas Huber31e25082011-01-10 10:38:31 -0800705 } else {
706 // video
Chong Zhangced1c2f2014-08-08 15:22:35 -0700707 sp<AMessage> inputFormat =
708 mSource->getFormat(false /* audio */);
Andreas Huber3831a062010-12-21 10:22:33 -0800709
Chong Zhangced1c2f2014-08-08 15:22:35 -0700710 updateVideoSize(inputFormat, format);
Andreas Huber31e25082011-01-10 10:38:31 -0800711 }
Lajos Molnar1cd13982014-01-17 15:12:51 -0800712 } else if (what == Decoder::kWhatShutdownCompleted) {
Steve Block3856b092011-10-20 11:56:00 +0100713 ALOGV("%s shutdown completed", audio ? "audio" : "video");
Andreas Huber3831a062010-12-21 10:22:33 -0800714 if (audio) {
715 mAudioDecoder.clear();
Wei Jia57568df2014-09-22 10:16:29 -0700716 ++mAudioDecoderGeneration;
Andreas Huber3831a062010-12-21 10:22:33 -0800717
718 CHECK_EQ((int)mFlushingAudio, (int)SHUTTING_DOWN_DECODER);
719 mFlushingAudio = SHUT_DOWN;
720 } else {
721 mVideoDecoder.clear();
Wei Jia57568df2014-09-22 10:16:29 -0700722 ++mVideoDecoderGeneration;
Andreas Huber3831a062010-12-21 10:22:33 -0800723
724 CHECK_EQ((int)mFlushingVideo, (int)SHUTTING_DOWN_DECODER);
725 mFlushingVideo = SHUT_DOWN;
726 }
727
728 finishFlushIfPossible();
Lajos Molnar1cd13982014-01-17 15:12:51 -0800729 } else if (what == Decoder::kWhatError) {
Chong Zhangf4c0a942014-08-11 15:14:10 -0700730 status_t err;
Andy Hung2abde2c2014-09-30 14:40:32 -0700731 if (!msg->findInt32("err", &err) || err == OK) {
Chong Zhangf4c0a942014-08-11 15:14:10 -0700732 err = UNKNOWN_ERROR;
733 }
Andy Hungcf31f1e2014-09-23 14:59:01 -0700734
Andy Hung2abde2c2014-09-30 14:40:32 -0700735 // Decoder errors can be due to Source (e.g. from streaming),
736 // or from decoding corrupted bitstreams, or from other decoder
737 // MediaCodec operations (e.g. from an ongoing reset or seek).
738 //
739 // We try to gracefully shut down the affected decoder if possible,
740 // rather than trying to force the shutdown with something
741 // similar to performReset(). This method can lead to a hang
742 // if MediaCodec functions block after an error, but they should
743 // typically return INVALID_OPERATION instead of blocking.
744
745 FlushStatus *flushing = audio ? &mFlushingAudio : &mFlushingVideo;
746 ALOGE("received error(%#x) from %s decoder, flushing(%d), now shutting down",
747 err, audio ? "audio" : "video", *flushing);
748
749 switch (*flushing) {
750 case NONE:
751 mDeferredActions.push_back(
752 new ShutdownDecoderAction(audio, !audio /* video */));
753 processDeferredActions();
754 break;
755 case FLUSHING_DECODER:
756 *flushing = FLUSHING_DECODER_SHUTDOWN; // initiate shutdown after flush.
757 break; // Wait for flush to complete.
758 case FLUSHING_DECODER_SHUTDOWN:
759 break; // Wait for flush to complete.
760 case SHUTTING_DOWN_DECODER:
761 break; // Wait for shutdown to complete.
762 case FLUSHED:
763 // Widevine source reads must stop before releasing the video decoder.
764 if (!audio && mSource != NULL && mSourceFlags & Source::FLAG_SECURE) {
765 mSource->stop();
766 }
767 getDecoder(audio)->initiateShutdown(); // In the middle of a seek.
768 *flushing = SHUTTING_DOWN_DECODER; // Shut down.
769 break;
770 case SHUT_DOWN:
771 finishFlushIfPossible(); // Should not occur.
772 break; // Finish anyways.
Marco Nelissen9e2b7912014-08-18 16:13:03 -0700773 }
Andy Hung2abde2c2014-09-30 14:40:32 -0700774 notifyListener(MEDIA_ERROR, MEDIA_ERROR_UNKNOWN, err);
Lajos Molnar1cd13982014-01-17 15:12:51 -0800775 } else if (what == Decoder::kWhatDrainThisBuffer) {
776 renderBuffer(audio, msg);
777 } else {
778 ALOGV("Unhandled decoder notification %d '%c%c%c%c'.",
Andreas Hubera1f8ab02012-11-30 10:53:22 -0800779 what,
780 what >> 24,
781 (what >> 16) & 0xff,
782 (what >> 8) & 0xff,
783 what & 0xff);
Andreas Huberf9334412010-12-15 15:17:42 -0800784 }
785
786 break;
787 }
788
789 case kWhatRendererNotify:
790 {
Wei Jia57568df2014-09-22 10:16:29 -0700791 int32_t requesterGeneration = mRendererGeneration - 1;
792 CHECK(msg->findInt32("generation", &requesterGeneration));
793 if (requesterGeneration != mRendererGeneration) {
794 ALOGV("got message from old renderer, generation(%d:%d)",
795 requesterGeneration, mRendererGeneration);
796 return;
797 }
798
Andreas Huberf9334412010-12-15 15:17:42 -0800799 int32_t what;
800 CHECK(msg->findInt32("what", &what));
801
802 if (what == Renderer::kWhatEOS) {
803 int32_t audio;
804 CHECK(msg->findInt32("audio", &audio));
805
Andreas Huberc92fd242011-08-16 13:48:44 -0700806 int32_t finalResult;
807 CHECK(msg->findInt32("finalResult", &finalResult));
808
Andreas Huberf9334412010-12-15 15:17:42 -0800809 if (audio) {
810 mAudioEOS = true;
811 } else {
812 mVideoEOS = true;
813 }
814
Andreas Huberc92fd242011-08-16 13:48:44 -0700815 if (finalResult == ERROR_END_OF_STREAM) {
Steve Block3856b092011-10-20 11:56:00 +0100816 ALOGV("reached %s EOS", audio ? "audio" : "video");
Andreas Huberc92fd242011-08-16 13:48:44 -0700817 } else {
Steve Block29357bc2012-01-06 19:20:56 +0000818 ALOGE("%s track encountered an error (%d)",
Andreas Huberc92fd242011-08-16 13:48:44 -0700819 audio ? "audio" : "video", finalResult);
820
821 notifyListener(
822 MEDIA_ERROR, MEDIA_ERROR_UNKNOWN, finalResult);
823 }
Andreas Huberf9334412010-12-15 15:17:42 -0800824
825 if ((mAudioEOS || mAudioDecoder == NULL)
826 && (mVideoEOS || mVideoDecoder == NULL)) {
827 notifyListener(MEDIA_PLAYBACK_COMPLETE, 0, 0);
828 }
Andreas Huber3fe62152011-09-16 15:09:22 -0700829 } else if (what == Renderer::kWhatFlushComplete) {
Andreas Huberf9334412010-12-15 15:17:42 -0800830 int32_t audio;
831 CHECK(msg->findInt32("audio", &audio));
832
Steve Block3856b092011-10-20 11:56:00 +0100833 ALOGV("renderer %s flush completed.", audio ? "audio" : "video");
Andy Hung8d121d42014-10-03 09:53:53 -0700834 handleFlushComplete(audio, false /* isDecoder */);
835 finishFlushIfPossible();
James Dongf57b4ea2012-07-20 13:38:36 -0700836 } else if (what == Renderer::kWhatVideoRenderingStart) {
837 notifyListener(MEDIA_INFO, MEDIA_INFO_RENDERING_START, 0);
Lajos Molnarcbaffcf2013-08-14 18:30:38 -0700838 } else if (what == Renderer::kWhatMediaRenderingStart) {
839 ALOGV("media rendering started");
840 notifyListener(MEDIA_STARTED, 0, 0);
Wei Jia3a2956d2014-07-22 16:01:33 -0700841 } else if (what == Renderer::kWhatAudioOffloadTearDown) {
842 ALOGV("Tear down audio offload, fall back to s/w path");
843 int64_t positionUs;
844 CHECK(msg->findInt64("positionUs", &positionUs));
Ronghua Wu08529172014-10-02 16:55:52 -0700845 int32_t reason;
846 CHECK(msg->findInt32("reason", &reason));
Andy Hung282a7e32014-08-14 15:56:34 -0700847 closeAudioSink();
Wei Jia3a2956d2014-07-22 16:01:33 -0700848 mAudioDecoder.clear();
Wei Jia57568df2014-09-22 10:16:29 -0700849 ++mAudioDecoderGeneration;
Wei Jia3a2956d2014-07-22 16:01:33 -0700850 mRenderer->flush(true /* audio */);
851 if (mVideoDecoder != NULL) {
852 mRenderer->flush(false /* audio */);
853 }
854 mRenderer->signalDisableOffloadAudio();
855 mOffloadAudio = false;
856
Wei Jiae427abf2014-09-22 15:21:11 -0700857 performSeek(positionUs, false /* needNotify */);
Ronghua Wu08529172014-10-02 16:55:52 -0700858 if (reason == Renderer::kDueToError) {
859 instantiateDecoder(true /* audio */, &mAudioDecoder);
860 }
Andreas Huberf9334412010-12-15 15:17:42 -0800861 }
862 break;
863 }
864
865 case kWhatMoreDataQueued:
866 {
867 break;
868 }
869
Andreas Huber1aef2112011-01-04 14:01:29 -0800870 case kWhatReset:
871 {
Steve Block3856b092011-10-20 11:56:00 +0100872 ALOGV("kWhatReset");
Andreas Huber1aef2112011-01-04 14:01:29 -0800873
Andreas Hubera1f8ab02012-11-30 10:53:22 -0800874 mDeferredActions.push_back(
Andreas Huber14f76722013-01-15 09:04:18 -0800875 new ShutdownDecoderAction(
876 true /* audio */, true /* video */));
Andreas Huberb7c8e912012-11-27 15:02:53 -0800877
Andreas Hubera1f8ab02012-11-30 10:53:22 -0800878 mDeferredActions.push_back(
879 new SimpleAction(&NuPlayer::performReset));
Andreas Huberb58ce9f2011-11-28 16:27:35 -0800880
Andreas Hubera1f8ab02012-11-30 10:53:22 -0800881 processDeferredActions();
Andreas Huber1aef2112011-01-04 14:01:29 -0800882 break;
883 }
884
Andreas Huber43c3e6c2011-01-05 12:17:08 -0800885 case kWhatSeek:
886 {
887 int64_t seekTimeUs;
Wei Jiae427abf2014-09-22 15:21:11 -0700888 int32_t needNotify;
Andreas Huber43c3e6c2011-01-05 12:17:08 -0800889 CHECK(msg->findInt64("seekTimeUs", &seekTimeUs));
Wei Jiae427abf2014-09-22 15:21:11 -0700890 CHECK(msg->findInt32("needNotify", &needNotify));
Andreas Huber43c3e6c2011-01-05 12:17:08 -0800891
Wei Jiae427abf2014-09-22 15:21:11 -0700892 ALOGV("kWhatSeek seekTimeUs=%lld us, needNotify=%d",
893 seekTimeUs, needNotify);
Andreas Huber43c3e6c2011-01-05 12:17:08 -0800894
Andreas Hubera1f8ab02012-11-30 10:53:22 -0800895 mDeferredActions.push_back(
896 new SimpleAction(&NuPlayer::performDecoderFlush));
Andreas Huber43c3e6c2011-01-05 12:17:08 -0800897
Wei Jiae427abf2014-09-22 15:21:11 -0700898 mDeferredActions.push_back(
899 new SeekAction(seekTimeUs, needNotify));
Andreas Huber43c3e6c2011-01-05 12:17:08 -0800900
Andreas Hubera1f8ab02012-11-30 10:53:22 -0800901 processDeferredActions();
Andreas Huber43c3e6c2011-01-05 12:17:08 -0800902 break;
903 }
904
Andreas Huberb4082222011-01-20 15:23:04 -0800905 case kWhatPause:
906 {
Wei Jia7e9f7f72014-09-23 10:55:35 -0700907 if (mSource != NULL) {
908 mSource->pause();
909 } else {
910 ALOGW("pause called when source is gone or not set");
911 }
912 if (mRenderer != NULL) {
913 mRenderer->pause();
914 } else {
915 ALOGW("pause called when renderer is gone or not set");
916 }
Andreas Huberb4082222011-01-20 15:23:04 -0800917 break;
918 }
919
Andreas Huberb5f25f02013-02-05 10:14:26 -0800920 case kWhatSourceNotify:
921 {
Andreas Huber9575c962013-02-05 13:59:56 -0800922 onSourceNotify(msg);
Andreas Huberb5f25f02013-02-05 10:14:26 -0800923 break;
924 }
925
Chong Zhanga7fa1d92014-06-11 14:49:23 -0700926 case kWhatClosedCaptionNotify:
927 {
928 onClosedCaptionNotify(msg);
929 break;
930 }
931
Andreas Huberf9334412010-12-15 15:17:42 -0800932 default:
933 TRESPASS();
934 break;
935 }
936}
937
Wei Jia94211742014-10-28 17:09:06 -0700938void NuPlayer::onResume() {
939 if (mSource != NULL) {
940 mSource->resume();
941 } else {
942 ALOGW("resume called when source is gone or not set");
943 }
944 // |mAudioDecoder| may have been released due to the pause timeout, so re-create it if
945 // needed.
946 if (audioDecoderStillNeeded() && mAudioDecoder == NULL) {
947 instantiateDecoder(true /* audio */, &mAudioDecoder);
948 }
949 if (mRenderer != NULL) {
950 mRenderer->resume();
951 } else {
952 ALOGW("resume called when renderer is gone or not set");
953 }
954}
955
956void NuPlayer::onStart() {
957 mVideoIsAVC = false;
958 mOffloadAudio = false;
959 mAudioEOS = false;
960 mVideoEOS = false;
961 mSkipRenderingAudioUntilMediaTimeUs = -1;
962 mSkipRenderingVideoUntilMediaTimeUs = -1;
963 mNumFramesTotal = 0;
964 mNumFramesDropped = 0;
965 mStarted = true;
966
967 /* instantiate decoders now for secure playback */
968 if (mSourceFlags & Source::FLAG_SECURE) {
969 if (mNativeWindow != NULL) {
970 instantiateDecoder(false, &mVideoDecoder);
971 }
972
973 if (mAudioSink != NULL) {
974 instantiateDecoder(true, &mAudioDecoder);
975 }
976 }
977
978 mSource->start();
979
980 uint32_t flags = 0;
981
982 if (mSource->isRealTime()) {
983 flags |= Renderer::FLAG_REAL_TIME;
984 }
985
986 sp<MetaData> audioMeta = mSource->getFormatMeta(true /* audio */);
987 audio_stream_type_t streamType = AUDIO_STREAM_MUSIC;
988 if (mAudioSink != NULL) {
989 streamType = mAudioSink->getAudioStreamType();
990 }
991
992 sp<AMessage> videoFormat = mSource->getFormat(false /* audio */);
993
994 mOffloadAudio =
995 canOffloadStream(audioMeta, (videoFormat != NULL),
996 true /* is_streaming */, streamType);
997 if (mOffloadAudio) {
998 flags |= Renderer::FLAG_OFFLOAD_AUDIO;
999 }
1000
1001 sp<AMessage> notify = new AMessage(kWhatRendererNotify, id());
1002 ++mRendererGeneration;
1003 notify->setInt32("generation", mRendererGeneration);
1004 mRenderer = new Renderer(mAudioSink, notify, flags);
1005
1006 mRendererLooper = new ALooper;
1007 mRendererLooper->setName("NuPlayerRenderer");
1008 mRendererLooper->start(false, false, ANDROID_PRIORITY_AUDIO);
1009 mRendererLooper->registerHandler(mRenderer);
1010
1011 sp<MetaData> meta = getFileMeta();
1012 int32_t rate;
1013 if (meta != NULL
1014 && meta->findInt32(kKeyFrameRate, &rate) && rate > 0) {
1015 mRenderer->setVideoFrameRate(rate);
1016 }
1017
1018 postScanSources();
1019}
1020
Ronghua Wud7988b12014-10-03 15:19:10 -07001021bool NuPlayer::audioDecoderStillNeeded() {
1022 // Audio decoder is no longer needed if it's in shut/shutting down status.
1023 return ((mFlushingAudio != SHUT_DOWN) && (mFlushingAudio != SHUTTING_DOWN_DECODER));
1024}
1025
Andy Hung8d121d42014-10-03 09:53:53 -07001026void NuPlayer::handleFlushComplete(bool audio, bool isDecoder) {
1027 // We wait for both the decoder flush and the renderer flush to complete
1028 // before entering either the FLUSHED or the SHUTTING_DOWN_DECODER state.
1029
1030 mFlushComplete[audio][isDecoder] = true;
1031 if (!mFlushComplete[audio][!isDecoder]) {
1032 return;
1033 }
1034
1035 FlushStatus *state = audio ? &mFlushingAudio : &mFlushingVideo;
1036 switch (*state) {
1037 case FLUSHING_DECODER:
1038 {
1039 *state = FLUSHED;
Andy Hung8d121d42014-10-03 09:53:53 -07001040 break;
1041 }
1042
1043 case FLUSHING_DECODER_SHUTDOWN:
1044 {
1045 *state = SHUTTING_DOWN_DECODER;
1046
1047 ALOGV("initiating %s decoder shutdown", audio ? "audio" : "video");
1048 if (!audio) {
Andy Hung8d121d42014-10-03 09:53:53 -07001049 // Widevine source reads must stop before releasing the video decoder.
1050 if (mSource != NULL && mSourceFlags & Source::FLAG_SECURE) {
1051 mSource->stop();
1052 }
1053 }
1054 getDecoder(audio)->initiateShutdown();
1055 break;
1056 }
1057
1058 default:
1059 // decoder flush completes only occur in a flushing state.
1060 LOG_ALWAYS_FATAL_IF(isDecoder, "decoder flush in invalid state %d", *state);
1061 break;
1062 }
1063}
1064
Andreas Huber3831a062010-12-21 10:22:33 -08001065void NuPlayer::finishFlushIfPossible() {
Wei Jia53904f32014-07-29 10:22:53 -07001066 if (mFlushingAudio != NONE && mFlushingAudio != FLUSHED
1067 && mFlushingAudio != SHUT_DOWN) {
Andreas Huber3831a062010-12-21 10:22:33 -08001068 return;
1069 }
1070
Wei Jia53904f32014-07-29 10:22:53 -07001071 if (mFlushingVideo != NONE && mFlushingVideo != FLUSHED
1072 && mFlushingVideo != SHUT_DOWN) {
Andreas Huber3831a062010-12-21 10:22:33 -08001073 return;
1074 }
1075
Steve Block3856b092011-10-20 11:56:00 +01001076 ALOGV("both audio and video are flushed now.");
Andreas Huber3831a062010-12-21 10:22:33 -08001077
Phil Burk9f526492014-09-03 15:04:12 -07001078 mPendingAudioAccessUnit.clear();
Phil Burkc5cc2e22014-09-09 20:08:39 -07001079 mAggregateBuffer.clear();
Phil Burk9f526492014-09-03 15:04:12 -07001080
Andreas Huber6e3d3112011-11-28 12:36:11 -08001081 if (mTimeDiscontinuityPending) {
1082 mRenderer->signalTimeDiscontinuity();
1083 mTimeDiscontinuityPending = false;
1084 }
Andreas Huber3831a062010-12-21 10:22:33 -08001085
Wei Jia53904f32014-07-29 10:22:53 -07001086 if (mAudioDecoder != NULL && mFlushingAudio == FLUSHED) {
Andreas Huber3831a062010-12-21 10:22:33 -08001087 mAudioDecoder->signalResume();
1088 }
1089
Wei Jia53904f32014-07-29 10:22:53 -07001090 if (mVideoDecoder != NULL && mFlushingVideo == FLUSHED) {
Andreas Huber3831a062010-12-21 10:22:33 -08001091 mVideoDecoder->signalResume();
1092 }
1093
1094 mFlushingAudio = NONE;
1095 mFlushingVideo = NONE;
Andreas Huber3831a062010-12-21 10:22:33 -08001096
Andy Hung8d121d42014-10-03 09:53:53 -07001097 clearFlushComplete();
1098
Andreas Hubera1f8ab02012-11-30 10:53:22 -08001099 processDeferredActions();
Andreas Huber1aef2112011-01-04 14:01:29 -08001100}
1101
1102void NuPlayer::postScanSources() {
1103 if (mScanSourcesPending) {
1104 return;
1105 }
1106
1107 sp<AMessage> msg = new AMessage(kWhatScanSources, id());
1108 msg->setInt32("generation", mScanSourcesGeneration);
1109 msg->post();
1110
1111 mScanSourcesPending = true;
1112}
1113
Andy Hung282a7e32014-08-14 15:56:34 -07001114void NuPlayer::openAudioSink(const sp<AMessage> &format, bool offloadOnly) {
Andy Hung282a7e32014-08-14 15:56:34 -07001115 uint32_t flags;
1116 int64_t durationUs;
Chong Zhang3b9eb1f2014-10-15 17:05:08 -07001117 bool hasVideo = (mVideoDecoder != NULL);
Andy Hung282a7e32014-08-14 15:56:34 -07001118 // FIXME: we should handle the case where the video decoder
1119 // is created after we receive the format change indication.
1120 // Current code will just make that we select deep buffer
1121 // with video which should not be a problem as it should
1122 // not prevent from keeping A/V sync.
Chong Zhang3b9eb1f2014-10-15 17:05:08 -07001123 if (hasVideo &&
Andy Hung282a7e32014-08-14 15:56:34 -07001124 mSource->getDuration(&durationUs) == OK &&
1125 durationUs
1126 > AUDIO_SINK_MIN_DEEP_BUFFER_DURATION_US) {
1127 flags = AUDIO_OUTPUT_FLAG_DEEP_BUFFER;
1128 } else {
1129 flags = AUDIO_OUTPUT_FLAG_NONE;
1130 }
1131
Chong Zhang3b9eb1f2014-10-15 17:05:08 -07001132 mOffloadAudio = mRenderer->openAudioSink(
1133 format, offloadOnly, hasVideo, flags);
1134
Andy Hung282a7e32014-08-14 15:56:34 -07001135 if (mOffloadAudio) {
Chong Zhang3b9eb1f2014-10-15 17:05:08 -07001136 sp<MetaData> audioMeta =
1137 mSource->getFormatMeta(true /* audio */);
1138 sendMetaDataToHal(mAudioSink, audioMeta);
Andy Hung282a7e32014-08-14 15:56:34 -07001139 }
1140}
1141
1142void NuPlayer::closeAudioSink() {
Chong Zhang3b9eb1f2014-10-15 17:05:08 -07001143 mRenderer->closeAudioSink();
Andy Hung282a7e32014-08-14 15:56:34 -07001144}
1145
Andreas Huber5bc087c2010-12-23 10:27:40 -08001146status_t NuPlayer::instantiateDecoder(bool audio, sp<Decoder> *decoder) {
Andreas Huberf9334412010-12-15 15:17:42 -08001147 if (*decoder != NULL) {
1148 return OK;
1149 }
1150
Andreas Huber84066782011-08-16 09:34:26 -07001151 sp<AMessage> format = mSource->getFormat(audio);
Andreas Huberf9334412010-12-15 15:17:42 -08001152
Andreas Huber84066782011-08-16 09:34:26 -07001153 if (format == NULL) {
Andreas Huberf9334412010-12-15 15:17:42 -08001154 return -EWOULDBLOCK;
1155 }
1156
Andreas Huber3fe62152011-09-16 15:09:22 -07001157 if (!audio) {
Andreas Huber84066782011-08-16 09:34:26 -07001158 AString mime;
1159 CHECK(format->findString("mime", &mime));
1160 mVideoIsAVC = !strcasecmp(MEDIA_MIMETYPE_VIDEO_AVC, mime.c_str());
Chong Zhanga7fa1d92014-06-11 14:49:23 -07001161
1162 sp<AMessage> ccNotify = new AMessage(kWhatClosedCaptionNotify, id());
1163 mCCDecoder = new CCDecoder(ccNotify);
Lajos Molnar09524832014-07-17 14:29:51 -07001164
1165 if (mSourceFlags & Source::FLAG_SECURE) {
1166 format->setInt32("secure", true);
1167 }
Andreas Huber3fe62152011-09-16 15:09:22 -07001168 }
1169
Wei Jiabc2fb722014-07-08 16:37:57 -07001170 if (audio) {
Wei Jia88703c32014-08-06 11:24:07 -07001171 sp<AMessage> notify = new AMessage(kWhatAudioNotify, id());
1172 ++mAudioDecoderGeneration;
1173 notify->setInt32("generation", mAudioDecoderGeneration);
1174
Wei Jiabc2fb722014-07-08 16:37:57 -07001175 if (mOffloadAudio) {
1176 *decoder = new DecoderPassThrough(notify);
1177 } else {
1178 *decoder = new Decoder(notify);
1179 }
1180 } else {
Wei Jia88703c32014-08-06 11:24:07 -07001181 sp<AMessage> notify = new AMessage(kWhatVideoNotify, id());
1182 ++mVideoDecoderGeneration;
1183 notify->setInt32("generation", mVideoDecoderGeneration);
1184
Wei Jiabc2fb722014-07-08 16:37:57 -07001185 *decoder = new Decoder(notify, mNativeWindow);
1186 }
Lajos Molnar1cd13982014-01-17 15:12:51 -08001187 (*decoder)->init();
Andreas Huber84066782011-08-16 09:34:26 -07001188 (*decoder)->configure(format);
Andreas Huberf9334412010-12-15 15:17:42 -08001189
Lajos Molnar09524832014-07-17 14:29:51 -07001190 // allocate buffers to decrypt widevine source buffers
1191 if (!audio && (mSourceFlags & Source::FLAG_SECURE)) {
1192 Vector<sp<ABuffer> > inputBufs;
1193 CHECK_EQ((*decoder)->getInputBuffers(&inputBufs), (status_t)OK);
1194
1195 Vector<MediaBuffer *> mediaBufs;
1196 for (size_t i = 0; i < inputBufs.size(); i++) {
1197 const sp<ABuffer> &buffer = inputBufs[i];
1198 MediaBuffer *mbuf = new MediaBuffer(buffer->data(), buffer->size());
1199 mediaBufs.push(mbuf);
1200 }
1201
1202 status_t err = mSource->setBuffers(audio, mediaBufs);
1203 if (err != OK) {
1204 for (size_t i = 0; i < mediaBufs.size(); ++i) {
1205 mediaBufs[i]->release();
1206 }
1207 mediaBufs.clear();
1208 ALOGE("Secure source didn't support secure mediaBufs.");
1209 return err;
1210 }
1211 }
Andreas Huberf9334412010-12-15 15:17:42 -08001212 return OK;
1213}
1214
1215status_t NuPlayer::feedDecoderInputData(bool audio, const sp<AMessage> &msg) {
1216 sp<AMessage> reply;
1217 CHECK(msg->findMessage("reply", &reply));
1218
Wei Jia53904f32014-07-29 10:22:53 -07001219 if ((audio && mFlushingAudio != NONE)
Wei Jiaf702d042014-09-09 12:08:47 -07001220 || (!audio && mFlushingVideo != NONE)
1221 || mSource == NULL) {
Wei Jiab189a5b2014-08-07 06:11:39 +00001222 reply->setInt32("err", INFO_DISCONTINUITY);
1223 reply->post();
1224 return OK;
Andreas Huberf9334412010-12-15 15:17:42 -08001225 }
1226
1227 sp<ABuffer> accessUnit;
Andreas Huberf9334412010-12-15 15:17:42 -08001228
Phil Burk9f526492014-09-03 15:04:12 -07001229 // Aggregate smaller buffers into a larger buffer.
1230 // The goal is to reduce power consumption.
Phil Burk33b51b02014-09-17 16:03:47 -07001231 // Note this will not work if the decoder requires one frame per buffer.
1232 bool doBufferAggregation = (audio && mOffloadAudio);
Phil Burk9f526492014-09-03 15:04:12 -07001233 bool needMoreData = false;
Phil Burk9f526492014-09-03 15:04:12 -07001234
Andreas Huber3fe62152011-09-16 15:09:22 -07001235 bool dropAccessUnit;
1236 do {
Phil Burk9f526492014-09-03 15:04:12 -07001237 status_t err;
1238 // Did we save an accessUnit earlier because of a discontinuity?
1239 if (audio && (mPendingAudioAccessUnit != NULL)) {
1240 accessUnit = mPendingAudioAccessUnit;
1241 mPendingAudioAccessUnit.clear();
1242 err = mPendingAudioErr;
1243 ALOGV("feedDecoderInputData() use mPendingAudioAccessUnit");
1244 } else {
1245 err = mSource->dequeueAccessUnit(audio, &accessUnit);
1246 }
Andreas Huber5bc087c2010-12-23 10:27:40 -08001247
Andreas Huber3fe62152011-09-16 15:09:22 -07001248 if (err == -EWOULDBLOCK) {
Phil Burkc5cc2e22014-09-09 20:08:39 -07001249 return err;
Andreas Huber3fe62152011-09-16 15:09:22 -07001250 } else if (err != OK) {
1251 if (err == INFO_DISCONTINUITY) {
Phil Burk33b51b02014-09-17 16:03:47 -07001252 if (doBufferAggregation && (mAggregateBuffer != NULL)) {
Phil Burk9f526492014-09-03 15:04:12 -07001253 // We already have some data so save this for later.
1254 mPendingAudioErr = err;
1255 mPendingAudioAccessUnit = accessUnit;
1256 accessUnit.clear();
1257 ALOGD("feedDecoderInputData() save discontinuity for later");
1258 break;
1259 }
Andreas Huber3fe62152011-09-16 15:09:22 -07001260 int32_t type;
1261 CHECK(accessUnit->meta()->findInt32("discontinuity", &type));
Andreas Huber53df1a42010-12-22 10:03:04 -08001262
Andreas Huber3fe62152011-09-16 15:09:22 -07001263 bool formatChange =
Andreas Huber6e3d3112011-11-28 12:36:11 -08001264 (audio &&
1265 (type & ATSParser::DISCONTINUITY_AUDIO_FORMAT))
1266 || (!audio &&
1267 (type & ATSParser::DISCONTINUITY_VIDEO_FORMAT));
Andreas Huber53df1a42010-12-22 10:03:04 -08001268
Andreas Huber6e3d3112011-11-28 12:36:11 -08001269 bool timeChange = (type & ATSParser::DISCONTINUITY_TIME) != 0;
1270
Steve Blockdf64d152012-01-04 20:05:49 +00001271 ALOGI("%s discontinuity (formatChange=%d, time=%d)",
Andreas Huber6e3d3112011-11-28 12:36:11 -08001272 audio ? "audio" : "video", formatChange, timeChange);
Andreas Huber32f3cef2011-03-02 15:34:46 -08001273
Andreas Huber3fe62152011-09-16 15:09:22 -07001274 if (audio) {
1275 mSkipRenderingAudioUntilMediaTimeUs = -1;
1276 } else {
1277 mSkipRenderingVideoUntilMediaTimeUs = -1;
1278 }
Andreas Huber32f3cef2011-03-02 15:34:46 -08001279
Andreas Huber6e3d3112011-11-28 12:36:11 -08001280 if (timeChange) {
1281 sp<AMessage> extra;
1282 if (accessUnit->meta()->findMessage("extra", &extra)
1283 && extra != NULL) {
1284 int64_t resumeAtMediaTimeUs;
1285 if (extra->findInt64(
1286 "resume-at-mediatimeUs", &resumeAtMediaTimeUs)) {
Steve Blockdf64d152012-01-04 20:05:49 +00001287 ALOGI("suppressing rendering of %s until %lld us",
Andreas Huber6e3d3112011-11-28 12:36:11 -08001288 audio ? "audio" : "video", resumeAtMediaTimeUs);
Andreas Huber3fe62152011-09-16 15:09:22 -07001289
Andreas Huber6e3d3112011-11-28 12:36:11 -08001290 if (audio) {
1291 mSkipRenderingAudioUntilMediaTimeUs =
1292 resumeAtMediaTimeUs;
1293 } else {
1294 mSkipRenderingVideoUntilMediaTimeUs =
1295 resumeAtMediaTimeUs;
1296 }
Andreas Huber3fe62152011-09-16 15:09:22 -07001297 }
Andreas Huber32f3cef2011-03-02 15:34:46 -08001298 }
1299 }
Andreas Huber3fe62152011-09-16 15:09:22 -07001300
Andreas Huber6e3d3112011-11-28 12:36:11 -08001301 mTimeDiscontinuityPending =
1302 mTimeDiscontinuityPending || timeChange;
1303
Lajos Molnar87603c02014-08-20 19:25:30 -07001304 bool seamlessFormatChange = false;
1305 sp<AMessage> newFormat = mSource->getFormat(audio);
1306 if (formatChange) {
1307 seamlessFormatChange =
1308 getDecoder(audio)->supportsSeamlessFormatChange(newFormat);
1309 // treat seamless format change separately
1310 formatChange = !seamlessFormatChange;
1311 }
1312 bool shutdownOrFlush = formatChange || timeChange;
1313
1314 // We want to queue up scan-sources only once per discontinuity.
1315 // We control this by doing it only if neither audio nor video are
1316 // flushing or shutting down. (After handling 1st discontinuity, one
1317 // of the flushing states will not be NONE.)
1318 // No need to scan sources if this discontinuity does not result
1319 // in a flush or shutdown, as the flushing state will stay NONE.
1320 if (mFlushingAudio == NONE && mFlushingVideo == NONE &&
1321 shutdownOrFlush) {
Robert Shiha2981012014-07-30 17:41:24 -07001322 // And we'll resume scanning sources once we're done
1323 // flushing.
1324 mDeferredActions.push_front(
1325 new SimpleAction(
1326 &NuPlayer::performScanSources));
1327 }
1328
Lajos Molnar87603c02014-08-20 19:25:30 -07001329 if (formatChange /* not seamless */) {
1330 // must change decoder
1331 flushDecoder(audio, /* needShutdown = */ true);
1332 } else if (timeChange) {
1333 // need to flush
1334 flushDecoder(audio, /* needShutdown = */ false, newFormat);
1335 err = OK;
1336 } else if (seamlessFormatChange) {
1337 // reuse existing decoder and don't flush
1338 updateDecoderFormatWithoutFlush(audio, newFormat);
1339 err = OK;
Andreas Huber6e3d3112011-11-28 12:36:11 -08001340 } else {
1341 // This stream is unaffected by the discontinuity
Andreas Huber6e3d3112011-11-28 12:36:11 -08001342 return -EWOULDBLOCK;
1343 }
Andreas Huber32f3cef2011-03-02 15:34:46 -08001344 }
1345
Andreas Huber3fe62152011-09-16 15:09:22 -07001346 reply->setInt32("err", err);
1347 reply->post();
1348 return OK;
Andreas Huberf9334412010-12-15 15:17:42 -08001349 }
1350
Andreas Huber3fe62152011-09-16 15:09:22 -07001351 if (!audio) {
1352 ++mNumFramesTotal;
1353 }
1354
1355 dropAccessUnit = false;
1356 if (!audio
Lajos Molnar09524832014-07-17 14:29:51 -07001357 && !(mSourceFlags & Source::FLAG_SECURE)
Ronghua Wua73d9e02014-10-08 15:13:29 -07001358 && mRenderer->getVideoLateByUs() > 100000ll
Andreas Huber3fe62152011-09-16 15:09:22 -07001359 && mVideoIsAVC
1360 && !IsAVCReferenceFrame(accessUnit)) {
1361 dropAccessUnit = true;
1362 ++mNumFramesDropped;
1363 }
Phil Burk9f526492014-09-03 15:04:12 -07001364
1365 size_t smallSize = accessUnit->size();
1366 needMoreData = false;
Phil Burkc5cc2e22014-09-09 20:08:39 -07001367 if (doBufferAggregation && (mAggregateBuffer == NULL)
Phil Burk9f526492014-09-03 15:04:12 -07001368 // Don't bother if only room for a few small buffers.
Phil Burkc5cc2e22014-09-09 20:08:39 -07001369 && (smallSize < (kAggregateBufferSizeBytes / 3))) {
Phil Burk9f526492014-09-03 15:04:12 -07001370 // Create a larger buffer for combining smaller buffers from the extractor.
Phil Burkc5cc2e22014-09-09 20:08:39 -07001371 mAggregateBuffer = new ABuffer(kAggregateBufferSizeBytes);
1372 mAggregateBuffer->setRange(0, 0); // start empty
Phil Burk9f526492014-09-03 15:04:12 -07001373 }
1374
Phil Burk33b51b02014-09-17 16:03:47 -07001375 if (doBufferAggregation && (mAggregateBuffer != NULL)) {
Phil Burk9f526492014-09-03 15:04:12 -07001376 int64_t timeUs;
Phil Burkc5cc2e22014-09-09 20:08:39 -07001377 int64_t dummy;
Phil Burk9f526492014-09-03 15:04:12 -07001378 bool smallTimestampValid = accessUnit->meta()->findInt64("timeUs", &timeUs);
Phil Burkc5cc2e22014-09-09 20:08:39 -07001379 bool bigTimestampValid = mAggregateBuffer->meta()->findInt64("timeUs", &dummy);
Phil Burk9f526492014-09-03 15:04:12 -07001380 // Will the smaller buffer fit?
Phil Burkc5cc2e22014-09-09 20:08:39 -07001381 size_t bigSize = mAggregateBuffer->size();
1382 size_t roomLeft = mAggregateBuffer->capacity() - bigSize;
Phil Burk9f526492014-09-03 15:04:12 -07001383 // Should we save this small buffer for the next big buffer?
1384 // If the first small buffer did not have a timestamp then save
1385 // any buffer that does have a timestamp until the next big buffer.
1386 if ((smallSize > roomLeft)
Phil Burkc5cc2e22014-09-09 20:08:39 -07001387 || (!bigTimestampValid && (bigSize > 0) && smallTimestampValid)) {
Phil Burk9f526492014-09-03 15:04:12 -07001388 mPendingAudioErr = err;
1389 mPendingAudioAccessUnit = accessUnit;
1390 accessUnit.clear();
1391 } else {
Phil Burkc5cc2e22014-09-09 20:08:39 -07001392 // Grab time from first small buffer if available.
1393 if ((bigSize == 0) && smallTimestampValid) {
1394 mAggregateBuffer->meta()->setInt64("timeUs", timeUs);
1395 }
Phil Burk9f526492014-09-03 15:04:12 -07001396 // Append small buffer to the bigger buffer.
Phil Burkc5cc2e22014-09-09 20:08:39 -07001397 memcpy(mAggregateBuffer->base() + bigSize, accessUnit->data(), smallSize);
Phil Burk9f526492014-09-03 15:04:12 -07001398 bigSize += smallSize;
Phil Burkc5cc2e22014-09-09 20:08:39 -07001399 mAggregateBuffer->setRange(0, bigSize);
Phil Burk9f526492014-09-03 15:04:12 -07001400
Phil Burkc5cc2e22014-09-09 20:08:39 -07001401 // Keep looping until we run out of room in the mAggregateBuffer.
Phil Burk9f526492014-09-03 15:04:12 -07001402 needMoreData = true;
1403
Phil Burkc5cc2e22014-09-09 20:08:39 -07001404 ALOGV("feedDecoderInputData() smallSize = %zu, bigSize = %zu, capacity = %zu",
1405 smallSize, bigSize, mAggregateBuffer->capacity());
Phil Burk9f526492014-09-03 15:04:12 -07001406 }
1407 }
1408 } while (dropAccessUnit || needMoreData);
Andreas Huberf9334412010-12-15 15:17:42 -08001409
Steve Block3856b092011-10-20 11:56:00 +01001410 // ALOGV("returned a valid buffer of %s data", audio ? "audio" : "video");
Andreas Huberf9334412010-12-15 15:17:42 -08001411
1412#if 0
1413 int64_t mediaTimeUs;
1414 CHECK(accessUnit->meta()->findInt64("timeUs", &mediaTimeUs));
Steve Block3856b092011-10-20 11:56:00 +01001415 ALOGV("feeding %s input buffer at media time %.2f secs",
Andreas Huberf9334412010-12-15 15:17:42 -08001416 audio ? "audio" : "video",
1417 mediaTimeUs / 1E6);
1418#endif
1419
Chong Zhanga7fa1d92014-06-11 14:49:23 -07001420 if (!audio) {
1421 mCCDecoder->decode(accessUnit);
1422 }
1423
Phil Burk33b51b02014-09-17 16:03:47 -07001424 if (doBufferAggregation && (mAggregateBuffer != NULL)) {
Phil Burkc5cc2e22014-09-09 20:08:39 -07001425 ALOGV("feedDecoderInputData() reply with aggregated buffer, %zu",
1426 mAggregateBuffer->size());
1427 reply->setBuffer("buffer", mAggregateBuffer);
1428 mAggregateBuffer.clear();
Phil Burk9f526492014-09-03 15:04:12 -07001429 } else {
1430 reply->setBuffer("buffer", accessUnit);
1431 }
1432
Andreas Huberf9334412010-12-15 15:17:42 -08001433 reply->post();
1434
1435 return OK;
1436}
1437
1438void NuPlayer::renderBuffer(bool audio, const sp<AMessage> &msg) {
Steve Block3856b092011-10-20 11:56:00 +01001439 // ALOGV("renderBuffer %s", audio ? "audio" : "video");
Andreas Huberf9334412010-12-15 15:17:42 -08001440
1441 sp<AMessage> reply;
1442 CHECK(msg->findMessage("reply", &reply));
1443
Wei Jia53904f32014-07-29 10:22:53 -07001444 if ((audio && mFlushingAudio != NONE)
1445 || (!audio && mFlushingVideo != NONE)) {
Andreas Huber18ac5402011-08-31 15:04:25 -07001446 // We're currently attempting to flush the decoder, in order
1447 // to complete this, the decoder wants all its buffers back,
1448 // so we don't want any output buffers it sent us (from before
1449 // we initiated the flush) to be stuck in the renderer's queue.
1450
Steve Block3856b092011-10-20 11:56:00 +01001451 ALOGV("we're still flushing the %s decoder, sending its output buffer"
Andreas Huber18ac5402011-08-31 15:04:25 -07001452 " right back.", audio ? "audio" : "video");
1453
1454 reply->post();
1455 return;
1456 }
1457
Andreas Huber2d8bedd2012-02-21 14:38:23 -08001458 sp<ABuffer> buffer;
1459 CHECK(msg->findBuffer("buffer", &buffer));
Andreas Huberf9334412010-12-15 15:17:42 -08001460
Chong Zhanga7fa1d92014-06-11 14:49:23 -07001461 int64_t mediaTimeUs;
1462 CHECK(buffer->meta()->findInt64("timeUs", &mediaTimeUs));
1463
Andreas Huber32f3cef2011-03-02 15:34:46 -08001464 int64_t &skipUntilMediaTimeUs =
1465 audio
1466 ? mSkipRenderingAudioUntilMediaTimeUs
1467 : mSkipRenderingVideoUntilMediaTimeUs;
1468
1469 if (skipUntilMediaTimeUs >= 0) {
Andreas Huber32f3cef2011-03-02 15:34:46 -08001470
1471 if (mediaTimeUs < skipUntilMediaTimeUs) {
Steve Block3856b092011-10-20 11:56:00 +01001472 ALOGV("dropping %s buffer at time %lld as requested.",
Andreas Huber32f3cef2011-03-02 15:34:46 -08001473 audio ? "audio" : "video",
1474 mediaTimeUs);
1475
1476 reply->post();
1477 return;
1478 }
1479
1480 skipUntilMediaTimeUs = -1;
1481 }
1482
Chong Zhanga7fa1d92014-06-11 14:49:23 -07001483 if (!audio && mCCDecoder->isSelected()) {
1484 mCCDecoder->display(mediaTimeUs);
1485 }
1486
Andreas Huberf9334412010-12-15 15:17:42 -08001487 mRenderer->queueBuffer(audio, buffer, reply);
1488}
1489
Chong Zhangced1c2f2014-08-08 15:22:35 -07001490void NuPlayer::updateVideoSize(
1491 const sp<AMessage> &inputFormat,
1492 const sp<AMessage> &outputFormat) {
1493 if (inputFormat == NULL) {
1494 ALOGW("Unknown video size, reporting 0x0!");
1495 notifyListener(MEDIA_SET_VIDEO_SIZE, 0, 0);
1496 return;
1497 }
1498
1499 int32_t displayWidth, displayHeight;
1500 int32_t cropLeft, cropTop, cropRight, cropBottom;
1501
1502 if (outputFormat != NULL) {
1503 int32_t width, height;
1504 CHECK(outputFormat->findInt32("width", &width));
1505 CHECK(outputFormat->findInt32("height", &height));
1506
1507 int32_t cropLeft, cropTop, cropRight, cropBottom;
1508 CHECK(outputFormat->findRect(
1509 "crop",
1510 &cropLeft, &cropTop, &cropRight, &cropBottom));
1511
1512 displayWidth = cropRight - cropLeft + 1;
1513 displayHeight = cropBottom - cropTop + 1;
1514
1515 ALOGV("Video output format changed to %d x %d "
1516 "(crop: %d x %d @ (%d, %d))",
1517 width, height,
1518 displayWidth,
1519 displayHeight,
1520 cropLeft, cropTop);
1521 } else {
1522 CHECK(inputFormat->findInt32("width", &displayWidth));
1523 CHECK(inputFormat->findInt32("height", &displayHeight));
1524
1525 ALOGV("Video input format %d x %d", displayWidth, displayHeight);
1526 }
1527
1528 // Take into account sample aspect ratio if necessary:
1529 int32_t sarWidth, sarHeight;
1530 if (inputFormat->findInt32("sar-width", &sarWidth)
1531 && inputFormat->findInt32("sar-height", &sarHeight)) {
1532 ALOGV("Sample aspect ratio %d : %d", sarWidth, sarHeight);
1533
1534 displayWidth = (displayWidth * sarWidth) / sarHeight;
1535
1536 ALOGV("display dimensions %d x %d", displayWidth, displayHeight);
1537 }
1538
1539 int32_t rotationDegrees;
1540 if (!inputFormat->findInt32("rotation-degrees", &rotationDegrees)) {
1541 rotationDegrees = 0;
1542 }
1543
1544 if (rotationDegrees == 90 || rotationDegrees == 270) {
1545 int32_t tmp = displayWidth;
1546 displayWidth = displayHeight;
1547 displayHeight = tmp;
1548 }
1549
1550 notifyListener(
1551 MEDIA_SET_VIDEO_SIZE,
1552 displayWidth,
1553 displayHeight);
1554}
1555
Chong Zhangdcb89b32013-08-06 09:44:47 -07001556void NuPlayer::notifyListener(int msg, int ext1, int ext2, const Parcel *in) {
Andreas Huber43c3e6c2011-01-05 12:17:08 -08001557 if (mDriver == NULL) {
Andreas Huberf9334412010-12-15 15:17:42 -08001558 return;
1559 }
1560
Andreas Huber43c3e6c2011-01-05 12:17:08 -08001561 sp<NuPlayerDriver> driver = mDriver.promote();
Andreas Huberf9334412010-12-15 15:17:42 -08001562
Andreas Huber43c3e6c2011-01-05 12:17:08 -08001563 if (driver == NULL) {
Andreas Huberf9334412010-12-15 15:17:42 -08001564 return;
1565 }
1566
Chong Zhangdcb89b32013-08-06 09:44:47 -07001567 driver->notifyListener(msg, ext1, ext2, in);
Andreas Huberf9334412010-12-15 15:17:42 -08001568}
1569
Lajos Molnar87603c02014-08-20 19:25:30 -07001570void NuPlayer::flushDecoder(
1571 bool audio, bool needShutdown, const sp<AMessage> &newFormat) {
Andreas Huber14f76722013-01-15 09:04:18 -08001572 ALOGV("[%s] flushDecoder needShutdown=%d",
1573 audio ? "audio" : "video", needShutdown);
1574
Lajos Molnar87603c02014-08-20 19:25:30 -07001575 const sp<Decoder> &decoder = getDecoder(audio);
1576 if (decoder == NULL) {
Steve Blockdf64d152012-01-04 20:05:49 +00001577 ALOGI("flushDecoder %s without decoder present",
Andreas Huber6e3d3112011-11-28 12:36:11 -08001578 audio ? "audio" : "video");
Lajos Molnar87603c02014-08-20 19:25:30 -07001579 return;
Andreas Huber6e3d3112011-11-28 12:36:11 -08001580 }
1581
Andreas Huber1aef2112011-01-04 14:01:29 -08001582 // Make sure we don't continue to scan sources until we finish flushing.
1583 ++mScanSourcesGeneration;
Andreas Huber43c3e6c2011-01-05 12:17:08 -08001584 mScanSourcesPending = false;
Andreas Huber1aef2112011-01-04 14:01:29 -08001585
Lajos Molnar87603c02014-08-20 19:25:30 -07001586 decoder->signalFlush(newFormat);
Andreas Huber1aef2112011-01-04 14:01:29 -08001587 mRenderer->flush(audio);
1588
1589 FlushStatus newStatus =
1590 needShutdown ? FLUSHING_DECODER_SHUTDOWN : FLUSHING_DECODER;
1591
Andy Hung8d121d42014-10-03 09:53:53 -07001592 mFlushComplete[audio][false /* isDecoder */] = false;
1593 mFlushComplete[audio][true /* isDecoder */] = false;
Andreas Huber1aef2112011-01-04 14:01:29 -08001594 if (audio) {
Wei Jia53904f32014-07-29 10:22:53 -07001595 ALOGE_IF(mFlushingAudio != NONE,
1596 "audio flushDecoder() is called in state %d", mFlushingAudio);
Andreas Huber1aef2112011-01-04 14:01:29 -08001597 mFlushingAudio = newStatus;
Andreas Huber1aef2112011-01-04 14:01:29 -08001598 } else {
Wei Jia53904f32014-07-29 10:22:53 -07001599 ALOGE_IF(mFlushingVideo != NONE,
1600 "video flushDecoder() is called in state %d", mFlushingVideo);
Andreas Huber1aef2112011-01-04 14:01:29 -08001601 mFlushingVideo = newStatus;
Chong Zhangb86e68f2014-08-01 13:46:53 -07001602
1603 if (mCCDecoder != NULL) {
1604 mCCDecoder->flush();
1605 }
Andreas Huber1aef2112011-01-04 14:01:29 -08001606 }
1607}
1608
Lajos Molnar87603c02014-08-20 19:25:30 -07001609void NuPlayer::updateDecoderFormatWithoutFlush(
1610 bool audio, const sp<AMessage> &format) {
1611 ALOGV("[%s] updateDecoderFormatWithoutFlush", audio ? "audio" : "video");
1612
1613 const sp<Decoder> &decoder = getDecoder(audio);
1614 if (decoder == NULL) {
1615 ALOGI("updateDecoderFormatWithoutFlush %s without decoder present",
1616 audio ? "audio" : "video");
1617 return;
1618 }
1619
1620 decoder->signalUpdateFormat(format);
1621}
1622
Chong Zhangced1c2f2014-08-08 15:22:35 -07001623void NuPlayer::queueDecoderShutdown(
1624 bool audio, bool video, const sp<AMessage> &reply) {
1625 ALOGI("queueDecoderShutdown audio=%d, video=%d", audio, video);
Andreas Huber84066782011-08-16 09:34:26 -07001626
Chong Zhangced1c2f2014-08-08 15:22:35 -07001627 mDeferredActions.push_back(
1628 new ShutdownDecoderAction(audio, video));
Andreas Huber84066782011-08-16 09:34:26 -07001629
Chong Zhangced1c2f2014-08-08 15:22:35 -07001630 mDeferredActions.push_back(
1631 new SimpleAction(&NuPlayer::performScanSources));
Andreas Huber84066782011-08-16 09:34:26 -07001632
Chong Zhangced1c2f2014-08-08 15:22:35 -07001633 mDeferredActions.push_back(new PostMessageAction(reply));
1634
1635 processDeferredActions();
Andreas Huber84066782011-08-16 09:34:26 -07001636}
1637
James Dong0d268a32012-08-31 12:18:27 -07001638status_t NuPlayer::setVideoScalingMode(int32_t mode) {
1639 mVideoScalingMode = mode;
Andreas Huber57a339c2012-12-03 11:18:00 -08001640 if (mNativeWindow != NULL) {
James Dong0d268a32012-08-31 12:18:27 -07001641 status_t ret = native_window_set_scaling_mode(
1642 mNativeWindow->getNativeWindow().get(), mVideoScalingMode);
1643 if (ret != OK) {
1644 ALOGE("Failed to set scaling mode (%d): %s",
1645 -ret, strerror(-ret));
1646 return ret;
1647 }
1648 }
1649 return OK;
1650}
1651
Chong Zhangdcb89b32013-08-06 09:44:47 -07001652status_t NuPlayer::getTrackInfo(Parcel* reply) const {
1653 sp<AMessage> msg = new AMessage(kWhatGetTrackInfo, id());
1654 msg->setPointer("reply", reply);
1655
1656 sp<AMessage> response;
1657 status_t err = msg->postAndAwaitResponse(&response);
1658 return err;
1659}
1660
Robert Shih7c4f0d72014-07-09 18:53:31 -07001661status_t NuPlayer::getSelectedTrack(int32_t type, Parcel* reply) const {
1662 sp<AMessage> msg = new AMessage(kWhatGetSelectedTrack, id());
1663 msg->setPointer("reply", reply);
1664 msg->setInt32("type", type);
1665
1666 sp<AMessage> response;
1667 status_t err = msg->postAndAwaitResponse(&response);
1668 if (err == OK && response != NULL) {
1669 CHECK(response->findInt32("err", &err));
1670 }
1671 return err;
1672}
1673
Chong Zhangdcb89b32013-08-06 09:44:47 -07001674status_t NuPlayer::selectTrack(size_t trackIndex, bool select) {
1675 sp<AMessage> msg = new AMessage(kWhatSelectTrack, id());
1676 msg->setSize("trackIndex", trackIndex);
1677 msg->setInt32("select", select);
1678
1679 sp<AMessage> response;
1680 status_t err = msg->postAndAwaitResponse(&response);
1681
Chong Zhang404fced2014-06-11 14:45:31 -07001682 if (err != OK) {
1683 return err;
1684 }
1685
1686 if (!response->findInt32("err", &err)) {
1687 err = OK;
1688 }
1689
Chong Zhangdcb89b32013-08-06 09:44:47 -07001690 return err;
1691}
1692
Ronghua Wua73d9e02014-10-08 15:13:29 -07001693status_t NuPlayer::getCurrentPosition(int64_t *mediaUs) {
1694 sp<Renderer> renderer = mRenderer;
1695 if (renderer == NULL) {
1696 return NO_INIT;
1697 }
1698
1699 return renderer->getCurrentPosition(mediaUs);
1700}
1701
1702void NuPlayer::getStats(int64_t *numFramesTotal, int64_t *numFramesDropped) {
1703 *numFramesTotal = mNumFramesTotal;
1704 *numFramesDropped = mNumFramesDropped;
1705}
1706
Marco Nelissenf0b72b52014-09-16 15:43:44 -07001707sp<MetaData> NuPlayer::getFileMeta() {
1708 return mSource->getFileFormatMeta();
1709}
1710
Andreas Huberb7c8e912012-11-27 15:02:53 -08001711void NuPlayer::schedulePollDuration() {
1712 sp<AMessage> msg = new AMessage(kWhatPollDuration, id());
1713 msg->setInt32("generation", mPollDurationGeneration);
1714 msg->post();
1715}
1716
1717void NuPlayer::cancelPollDuration() {
1718 ++mPollDurationGeneration;
1719}
1720
Andreas Hubera1f8ab02012-11-30 10:53:22 -08001721void NuPlayer::processDeferredActions() {
1722 while (!mDeferredActions.empty()) {
1723 // We won't execute any deferred actions until we're no longer in
1724 // an intermediate state, i.e. one more more decoders are currently
1725 // flushing or shutting down.
1726
Andreas Hubera1f8ab02012-11-30 10:53:22 -08001727 if (mFlushingAudio != NONE || mFlushingVideo != NONE) {
1728 // We're currently flushing, postpone the reset until that's
1729 // completed.
1730
1731 ALOGV("postponing action mFlushingAudio=%d, mFlushingVideo=%d",
1732 mFlushingAudio, mFlushingVideo);
1733
1734 break;
1735 }
1736
1737 sp<Action> action = *mDeferredActions.begin();
1738 mDeferredActions.erase(mDeferredActions.begin());
1739
1740 action->execute(this);
1741 }
1742}
1743
Wei Jiae427abf2014-09-22 15:21:11 -07001744void NuPlayer::performSeek(int64_t seekTimeUs, bool needNotify) {
1745 ALOGV("performSeek seekTimeUs=%lld us (%.2f secs), needNotify(%d)",
Andreas Hubera1f8ab02012-11-30 10:53:22 -08001746 seekTimeUs,
Wei Jiae427abf2014-09-22 15:21:11 -07001747 seekTimeUs / 1E6,
1748 needNotify);
Andreas Hubera1f8ab02012-11-30 10:53:22 -08001749
Andy Hungadf34bf2014-09-03 18:22:22 -07001750 if (mSource == NULL) {
1751 // This happens when reset occurs right before the loop mode
1752 // asynchronously seeks to the start of the stream.
1753 LOG_ALWAYS_FATAL_IF(mAudioDecoder != NULL || mVideoDecoder != NULL,
1754 "mSource is NULL and decoders not NULL audio(%p) video(%p)",
1755 mAudioDecoder.get(), mVideoDecoder.get());
1756 return;
1757 }
Andreas Hubera1f8ab02012-11-30 10:53:22 -08001758 mSource->seekTo(seekTimeUs);
Robert Shihd3b0bbb2014-07-23 15:00:25 -07001759 ++mTimedTextGeneration;
Andreas Hubera1f8ab02012-11-30 10:53:22 -08001760
1761 if (mDriver != NULL) {
1762 sp<NuPlayerDriver> driver = mDriver.promote();
1763 if (driver != NULL) {
Wei Jiae427abf2014-09-22 15:21:11 -07001764 if (needNotify) {
1765 driver->notifySeekComplete();
1766 }
Andreas Hubera1f8ab02012-11-30 10:53:22 -08001767 }
1768 }
1769
1770 // everything's flushed, continue playback.
1771}
1772
1773void NuPlayer::performDecoderFlush() {
1774 ALOGV("performDecoderFlush");
1775
Andreas Huberda9740e2013-04-16 10:54:03 -07001776 if (mAudioDecoder == NULL && mVideoDecoder == NULL) {
Andreas Hubera1f8ab02012-11-30 10:53:22 -08001777 return;
1778 }
1779
1780 mTimeDiscontinuityPending = true;
1781
1782 if (mAudioDecoder != NULL) {
1783 flushDecoder(true /* audio */, false /* needShutdown */);
1784 }
1785
1786 if (mVideoDecoder != NULL) {
1787 flushDecoder(false /* audio */, false /* needShutdown */);
1788 }
1789}
1790
Andreas Huber14f76722013-01-15 09:04:18 -08001791void NuPlayer::performDecoderShutdown(bool audio, bool video) {
1792 ALOGV("performDecoderShutdown audio=%d, video=%d", audio, video);
Andreas Hubera1f8ab02012-11-30 10:53:22 -08001793
Andreas Huber14f76722013-01-15 09:04:18 -08001794 if ((!audio || mAudioDecoder == NULL)
1795 && (!video || mVideoDecoder == NULL)) {
Andreas Hubera1f8ab02012-11-30 10:53:22 -08001796 return;
1797 }
1798
1799 mTimeDiscontinuityPending = true;
1800
Andreas Huber14f76722013-01-15 09:04:18 -08001801 if (audio && mAudioDecoder != NULL) {
Andreas Hubera1f8ab02012-11-30 10:53:22 -08001802 flushDecoder(true /* audio */, true /* needShutdown */);
1803 }
1804
Andreas Huber14f76722013-01-15 09:04:18 -08001805 if (video && mVideoDecoder != NULL) {
Andreas Hubera1f8ab02012-11-30 10:53:22 -08001806 flushDecoder(false /* audio */, true /* needShutdown */);
1807 }
1808}
1809
1810void NuPlayer::performReset() {
1811 ALOGV("performReset");
1812
1813 CHECK(mAudioDecoder == NULL);
1814 CHECK(mVideoDecoder == NULL);
1815
1816 cancelPollDuration();
1817
1818 ++mScanSourcesGeneration;
1819 mScanSourcesPending = false;
1820
Lajos Molnar09524832014-07-17 14:29:51 -07001821 if (mRendererLooper != NULL) {
1822 if (mRenderer != NULL) {
1823 mRendererLooper->unregisterHandler(mRenderer->id());
1824 }
1825 mRendererLooper->stop();
1826 mRendererLooper.clear();
1827 }
Andreas Hubera1f8ab02012-11-30 10:53:22 -08001828 mRenderer.clear();
Wei Jia57568df2014-09-22 10:16:29 -07001829 ++mRendererGeneration;
Andreas Hubera1f8ab02012-11-30 10:53:22 -08001830
1831 if (mSource != NULL) {
1832 mSource->stop();
Andreas Huberb5f25f02013-02-05 10:14:26 -08001833
Andreas Hubera1f8ab02012-11-30 10:53:22 -08001834 mSource.clear();
1835 }
1836
1837 if (mDriver != NULL) {
1838 sp<NuPlayerDriver> driver = mDriver.promote();
1839 if (driver != NULL) {
1840 driver->notifyResetComplete();
1841 }
1842 }
Andreas Huber57a339c2012-12-03 11:18:00 -08001843
1844 mStarted = false;
Andreas Hubera1f8ab02012-11-30 10:53:22 -08001845}
1846
1847void NuPlayer::performScanSources() {
1848 ALOGV("performScanSources");
1849
Andreas Huber57a339c2012-12-03 11:18:00 -08001850 if (!mStarted) {
1851 return;
1852 }
1853
Andreas Hubera1f8ab02012-11-30 10:53:22 -08001854 if (mAudioDecoder == NULL || mVideoDecoder == NULL) {
1855 postScanSources();
1856 }
1857}
1858
Andreas Huber57a339c2012-12-03 11:18:00 -08001859void NuPlayer::performSetSurface(const sp<NativeWindowWrapper> &wrapper) {
1860 ALOGV("performSetSurface");
1861
1862 mNativeWindow = wrapper;
1863
1864 // XXX - ignore error from setVideoScalingMode for now
1865 setVideoScalingMode(mVideoScalingMode);
Chong Zhang13d6faa2014-08-22 15:35:28 -07001866
1867 if (mDriver != NULL) {
1868 sp<NuPlayerDriver> driver = mDriver.promote();
1869 if (driver != NULL) {
1870 driver->notifySetSurfaceComplete();
1871 }
1872 }
Andreas Huber57a339c2012-12-03 11:18:00 -08001873}
1874
Andreas Huber9575c962013-02-05 13:59:56 -08001875void NuPlayer::onSourceNotify(const sp<AMessage> &msg) {
1876 int32_t what;
1877 CHECK(msg->findInt32("what", &what));
1878
1879 switch (what) {
1880 case Source::kWhatPrepared:
1881 {
Andreas Huberb5f28d42013-04-25 15:11:19 -07001882 if (mSource == NULL) {
1883 // This is a stale notification from a source that was
1884 // asynchronously preparing when the client called reset().
1885 // We handled the reset, the source is gone.
1886 break;
1887 }
1888
Andreas Huberec0c5972013-02-05 14:47:13 -08001889 int32_t err;
1890 CHECK(msg->findInt32("err", &err));
1891
Andreas Huber9575c962013-02-05 13:59:56 -08001892 sp<NuPlayerDriver> driver = mDriver.promote();
1893 if (driver != NULL) {
Marco Nelissendd114d12014-05-28 15:23:14 -07001894 // notify duration first, so that it's definitely set when
1895 // the app received the "prepare complete" callback.
1896 int64_t durationUs;
1897 if (mSource->getDuration(&durationUs) == OK) {
1898 driver->notifyDuration(durationUs);
1899 }
Andreas Huberec0c5972013-02-05 14:47:13 -08001900 driver->notifyPrepareCompleted(err);
Andreas Huber9575c962013-02-05 13:59:56 -08001901 }
Andreas Huber99759402013-04-01 14:28:31 -07001902
Andreas Huber9575c962013-02-05 13:59:56 -08001903 break;
1904 }
1905
1906 case Source::kWhatFlagsChanged:
1907 {
1908 uint32_t flags;
1909 CHECK(msg->findInt32("flags", (int32_t *)&flags));
1910
Chong Zhang4b7069d2013-09-11 12:52:43 -07001911 sp<NuPlayerDriver> driver = mDriver.promote();
1912 if (driver != NULL) {
1913 driver->notifyFlagsChanged(flags);
1914 }
1915
Andreas Huber9575c962013-02-05 13:59:56 -08001916 if ((mSourceFlags & Source::FLAG_DYNAMIC_DURATION)
1917 && (!(flags & Source::FLAG_DYNAMIC_DURATION))) {
1918 cancelPollDuration();
1919 } else if (!(mSourceFlags & Source::FLAG_DYNAMIC_DURATION)
1920 && (flags & Source::FLAG_DYNAMIC_DURATION)
1921 && (mAudioDecoder != NULL || mVideoDecoder != NULL)) {
1922 schedulePollDuration();
1923 }
1924
1925 mSourceFlags = flags;
1926 break;
1927 }
1928
1929 case Source::kWhatVideoSizeChanged:
1930 {
Chong Zhangced1c2f2014-08-08 15:22:35 -07001931 sp<AMessage> format;
1932 CHECK(msg->findMessage("format", &format));
Andreas Huber9575c962013-02-05 13:59:56 -08001933
Chong Zhangced1c2f2014-08-08 15:22:35 -07001934 updateVideoSize(format);
Andreas Huber9575c962013-02-05 13:59:56 -08001935 break;
1936 }
1937
Chong Zhang2a3cc9a2014-08-21 17:48:26 -07001938 case Source::kWhatBufferingUpdate:
1939 {
1940 int32_t percentage;
1941 CHECK(msg->findInt32("percentage", &percentage));
1942
1943 notifyListener(MEDIA_BUFFERING_UPDATE, percentage, 0);
1944 break;
1945 }
1946
Roger Jönssonb50e83e2013-01-21 16:26:41 +01001947 case Source::kWhatBufferingStart:
1948 {
1949 notifyListener(MEDIA_INFO, MEDIA_INFO_BUFFERING_START, 0);
1950 break;
1951 }
1952
1953 case Source::kWhatBufferingEnd:
1954 {
1955 notifyListener(MEDIA_INFO, MEDIA_INFO_BUFFERING_END, 0);
1956 break;
1957 }
1958
Chong Zhangdcb89b32013-08-06 09:44:47 -07001959 case Source::kWhatSubtitleData:
1960 {
1961 sp<ABuffer> buffer;
1962 CHECK(msg->findBuffer("buffer", &buffer));
1963
Chong Zhang404fced2014-06-11 14:45:31 -07001964 sendSubtitleData(buffer, 0 /* baseIndex */);
Chong Zhangdcb89b32013-08-06 09:44:47 -07001965 break;
1966 }
1967
Robert Shihd3b0bbb2014-07-23 15:00:25 -07001968 case Source::kWhatTimedTextData:
1969 {
1970 int32_t generation;
1971 if (msg->findInt32("generation", &generation)
1972 && generation != mTimedTextGeneration) {
1973 break;
1974 }
1975
1976 sp<ABuffer> buffer;
1977 CHECK(msg->findBuffer("buffer", &buffer));
1978
1979 sp<NuPlayerDriver> driver = mDriver.promote();
1980 if (driver == NULL) {
1981 break;
1982 }
1983
1984 int posMs;
1985 int64_t timeUs, posUs;
1986 driver->getCurrentPosition(&posMs);
1987 posUs = posMs * 1000;
1988 CHECK(buffer->meta()->findInt64("timeUs", &timeUs));
1989
1990 if (posUs < timeUs) {
1991 if (!msg->findInt32("generation", &generation)) {
1992 msg->setInt32("generation", mTimedTextGeneration);
1993 }
1994 msg->post(timeUs - posUs);
1995 } else {
1996 sendTimedTextData(buffer);
1997 }
1998 break;
1999 }
2000
Andreas Huber14f76722013-01-15 09:04:18 -08002001 case Source::kWhatQueueDecoderShutdown:
2002 {
2003 int32_t audio, video;
2004 CHECK(msg->findInt32("audio", &audio));
2005 CHECK(msg->findInt32("video", &video));
2006
2007 sp<AMessage> reply;
2008 CHECK(msg->findMessage("reply", &reply));
2009
2010 queueDecoderShutdown(audio, video, reply);
2011 break;
2012 }
2013
Ronghua Wu80276872014-08-28 15:50:29 -07002014 case Source::kWhatDrmNoLicense:
2015 {
2016 notifyListener(MEDIA_ERROR, MEDIA_ERROR_UNKNOWN, ERROR_DRM_NO_LICENSE);
2017 break;
2018 }
2019
Andreas Huber9575c962013-02-05 13:59:56 -08002020 default:
2021 TRESPASS();
2022 }
2023}
2024
Chong Zhanga7fa1d92014-06-11 14:49:23 -07002025void NuPlayer::onClosedCaptionNotify(const sp<AMessage> &msg) {
2026 int32_t what;
2027 CHECK(msg->findInt32("what", &what));
2028
2029 switch (what) {
2030 case NuPlayer::CCDecoder::kWhatClosedCaptionData:
2031 {
2032 sp<ABuffer> buffer;
2033 CHECK(msg->findBuffer("buffer", &buffer));
2034
2035 size_t inbandTracks = 0;
2036 if (mSource != NULL) {
2037 inbandTracks = mSource->getTrackCount();
2038 }
2039
2040 sendSubtitleData(buffer, inbandTracks);
2041 break;
2042 }
2043
2044 case NuPlayer::CCDecoder::kWhatTrackAdded:
2045 {
2046 notifyListener(MEDIA_INFO, MEDIA_INFO_METADATA_UPDATE, 0);
2047
2048 break;
2049 }
2050
2051 default:
2052 TRESPASS();
2053 }
2054
2055
2056}
2057
Chong Zhang404fced2014-06-11 14:45:31 -07002058void NuPlayer::sendSubtitleData(const sp<ABuffer> &buffer, int32_t baseIndex) {
2059 int32_t trackIndex;
2060 int64_t timeUs, durationUs;
2061 CHECK(buffer->meta()->findInt32("trackIndex", &trackIndex));
2062 CHECK(buffer->meta()->findInt64("timeUs", &timeUs));
2063 CHECK(buffer->meta()->findInt64("durationUs", &durationUs));
2064
2065 Parcel in;
2066 in.writeInt32(trackIndex + baseIndex);
2067 in.writeInt64(timeUs);
2068 in.writeInt64(durationUs);
2069 in.writeInt32(buffer->size());
2070 in.writeInt32(buffer->size());
2071 in.write(buffer->data(), buffer->size());
2072
2073 notifyListener(MEDIA_SUBTITLE_DATA, 0, 0, &in);
2074}
Robert Shihd3b0bbb2014-07-23 15:00:25 -07002075
2076void NuPlayer::sendTimedTextData(const sp<ABuffer> &buffer) {
2077 const void *data;
2078 size_t size = 0;
2079 int64_t timeUs;
2080 int32_t flag = TextDescriptions::LOCAL_DESCRIPTIONS;
2081
2082 AString mime;
2083 CHECK(buffer->meta()->findString("mime", &mime));
2084 CHECK(strcasecmp(mime.c_str(), MEDIA_MIMETYPE_TEXT_3GPP) == 0);
2085
2086 data = buffer->data();
2087 size = buffer->size();
2088
2089 Parcel parcel;
2090 if (size > 0) {
2091 CHECK(buffer->meta()->findInt64("timeUs", &timeUs));
2092 flag |= TextDescriptions::IN_BAND_TEXT_3GPP;
2093 TextDescriptions::getParcelOfDescriptions(
2094 (const uint8_t *)data, size, flag, timeUs / 1000, &parcel);
2095 }
2096
2097 if ((parcel.dataSize() > 0)) {
2098 notifyListener(MEDIA_TIMED_TEXT, 0, 0, &parcel);
2099 } else { // send an empty timed text
2100 notifyListener(MEDIA_TIMED_TEXT, 0, 0);
2101 }
2102}
Andreas Huberb5f25f02013-02-05 10:14:26 -08002103////////////////////////////////////////////////////////////////////////////////
2104
Chong Zhangced1c2f2014-08-08 15:22:35 -07002105sp<AMessage> NuPlayer::Source::getFormat(bool audio) {
2106 sp<MetaData> meta = getFormatMeta(audio);
2107
2108 if (meta == NULL) {
2109 return NULL;
2110 }
2111
2112 sp<AMessage> msg = new AMessage;
2113
2114 if(convertMetaDataToMessage(meta, &msg) == OK) {
2115 return msg;
2116 }
2117 return NULL;
2118}
2119
Andreas Huber9575c962013-02-05 13:59:56 -08002120void NuPlayer::Source::notifyFlagsChanged(uint32_t flags) {
2121 sp<AMessage> notify = dupNotify();
2122 notify->setInt32("what", kWhatFlagsChanged);
2123 notify->setInt32("flags", flags);
2124 notify->post();
2125}
2126
Chong Zhangced1c2f2014-08-08 15:22:35 -07002127void NuPlayer::Source::notifyVideoSizeChanged(const sp<AMessage> &format) {
Andreas Huber9575c962013-02-05 13:59:56 -08002128 sp<AMessage> notify = dupNotify();
2129 notify->setInt32("what", kWhatVideoSizeChanged);
Chong Zhangced1c2f2014-08-08 15:22:35 -07002130 notify->setMessage("format", format);
Andreas Huber9575c962013-02-05 13:59:56 -08002131 notify->post();
2132}
2133
Andreas Huberec0c5972013-02-05 14:47:13 -08002134void NuPlayer::Source::notifyPrepared(status_t err) {
Andreas Huber9575c962013-02-05 13:59:56 -08002135 sp<AMessage> notify = dupNotify();
2136 notify->setInt32("what", kWhatPrepared);
Andreas Huberec0c5972013-02-05 14:47:13 -08002137 notify->setInt32("err", err);
Andreas Huber9575c962013-02-05 13:59:56 -08002138 notify->post();
2139}
2140
Andreas Huber84333e02014-02-07 15:36:10 -08002141void NuPlayer::Source::onMessageReceived(const sp<AMessage> & /* msg */) {
Andreas Huberb5f25f02013-02-05 10:14:26 -08002142 TRESPASS();
2143}
2144
Andreas Huberf9334412010-12-15 15:17:42 -08002145} // namespace android