blob: 336a9c5f95c7f5e723724421f2432ba890201697 [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
Wei Jiafef808d2014-10-31 17:57:05 -070098struct NuPlayer::FlushDecoderAction : public Action {
99 FlushDecoderAction(FlushCommand audio, FlushCommand video)
Andreas Huber14f76722013-01-15 09:04:18 -0800100 : mAudio(audio),
101 mVideo(video) {
102 }
103
104 virtual void execute(NuPlayer *player) {
Wei Jiafef808d2014-10-31 17:57:05 -0700105 player->performDecoderFlush(mAudio, mVideo);
Andreas Huber14f76722013-01-15 09:04:18 -0800106 }
107
108private:
Wei Jiafef808d2014-10-31 17:57:05 -0700109 FlushCommand mAudio;
110 FlushCommand mVideo;
Andreas Huber14f76722013-01-15 09:04:18 -0800111
Wei Jiafef808d2014-10-31 17:57:05 -0700112 DISALLOW_EVIL_CONSTRUCTORS(FlushDecoderAction);
Andreas Huber14f76722013-01-15 09:04:18 -0800113};
114
115struct NuPlayer::PostMessageAction : public Action {
116 PostMessageAction(const sp<AMessage> &msg)
117 : mMessage(msg) {
118 }
119
120 virtual void execute(NuPlayer *) {
121 mMessage->post();
122 }
123
124private:
125 sp<AMessage> mMessage;
126
127 DISALLOW_EVIL_CONSTRUCTORS(PostMessageAction);
128};
129
Andreas Hubera1f8ab02012-11-30 10:53:22 -0800130// Use this if there's no state necessary to save in order to execute
131// the action.
132struct NuPlayer::SimpleAction : public Action {
133 typedef void (NuPlayer::*ActionFunc)();
134
135 SimpleAction(ActionFunc func)
136 : mFunc(func) {
137 }
138
139 virtual void execute(NuPlayer *player) {
140 (player->*mFunc)();
141 }
142
143private:
144 ActionFunc mFunc;
145
146 DISALLOW_EVIL_CONSTRUCTORS(SimpleAction);
147};
148
Andreas Huberf9334412010-12-15 15:17:42 -0800149////////////////////////////////////////////////////////////////////////////////
150
151NuPlayer::NuPlayer()
Andreas Huber9b80c2b2011-06-30 15:47:02 -0700152 : mUIDValid(false),
Andreas Huber9575c962013-02-05 13:59:56 -0800153 mSourceFlags(0),
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 mNumFramesTotal(0ll),
James Dong0d268a32012-08-31 12:18:27 -0700169 mNumFramesDropped(0ll),
Andreas Huber57a339c2012-12-03 11:18:00 -0800170 mVideoScalingMode(NATIVE_WINDOW_SCALING_MODE_SCALE_TO_WINDOW),
171 mStarted(false) {
Andy Hung8d121d42014-10-03 09:53:53 -0700172 clearFlushComplete();
Andreas Huberf9334412010-12-15 15:17:42 -0800173}
174
175NuPlayer::~NuPlayer() {
176}
177
Andreas Huber9b80c2b2011-06-30 15:47:02 -0700178void NuPlayer::setUID(uid_t uid) {
179 mUIDValid = true;
180 mUID = uid;
181}
182
Andreas Huber43c3e6c2011-01-05 12:17:08 -0800183void NuPlayer::setDriver(const wp<NuPlayerDriver> &driver) {
184 mDriver = driver;
Andreas Huberf9334412010-12-15 15:17:42 -0800185}
186
Andreas Huber9575c962013-02-05 13:59:56 -0800187void NuPlayer::setDataSourceAsync(const sp<IStreamSource> &source) {
Andreas Huberf9334412010-12-15 15:17:42 -0800188 sp<AMessage> msg = new AMessage(kWhatSetDataSource, id());
189
Andreas Huberb5f25f02013-02-05 10:14:26 -0800190 sp<AMessage> notify = new AMessage(kWhatSourceNotify, id());
191
Andreas Huber240abcc2014-02-13 13:32:37 -0800192 msg->setObject("source", new StreamingSource(notify, source));
Andreas Huber5bc087c2010-12-23 10:27:40 -0800193 msg->post();
194}
Andreas Huberf9334412010-12-15 15:17:42 -0800195
Andreas Huberafed0e12011-09-20 15:39:58 -0700196static bool IsHTTPLiveURL(const char *url) {
197 if (!strncasecmp("http://", url, 7)
Andreas Huber99759402013-04-01 14:28:31 -0700198 || !strncasecmp("https://", url, 8)
199 || !strncasecmp("file://", url, 7)) {
Andreas Huberafed0e12011-09-20 15:39:58 -0700200 size_t len = strlen(url);
201 if (len >= 5 && !strcasecmp(".m3u8", &url[len - 5])) {
202 return true;
203 }
204
205 if (strstr(url,"m3u8")) {
206 return true;
207 }
208 }
209
210 return false;
211}
212
Andreas Huber9575c962013-02-05 13:59:56 -0800213void NuPlayer::setDataSourceAsync(
Andreas Huber1b86fe02014-01-29 11:13:26 -0800214 const sp<IMediaHTTPService> &httpService,
215 const char *url,
216 const KeyedVector<String8, String8> *headers) {
Chong Zhang3de157d2014-08-05 20:54:44 -0700217
Andreas Huber5bc087c2010-12-23 10:27:40 -0800218 sp<AMessage> msg = new AMessage(kWhatSetDataSource, id());
Oscar Rydhé7a33b772012-02-20 10:15:48 +0100219 size_t len = strlen(url);
Andreas Huber5bc087c2010-12-23 10:27:40 -0800220
Andreas Huberb5f25f02013-02-05 10:14:26 -0800221 sp<AMessage> notify = new AMessage(kWhatSourceNotify, id());
222
Andreas Huberafed0e12011-09-20 15:39:58 -0700223 sp<Source> source;
224 if (IsHTTPLiveURL(url)) {
Andreas Huber81e68442014-02-05 11:52:33 -0800225 source = new HTTPLiveSource(notify, httpService, url, headers);
Andreas Huberafed0e12011-09-20 15:39:58 -0700226 } else if (!strncasecmp(url, "rtsp://", 7)) {
Andreas Huber1b86fe02014-01-29 11:13:26 -0800227 source = new RTSPSource(
228 notify, httpService, url, headers, mUIDValid, mUID);
Oscar Rydhé7a33b772012-02-20 10:15:48 +0100229 } else if ((!strncasecmp(url, "http://", 7)
230 || !strncasecmp(url, "https://", 8))
231 && ((len >= 4 && !strcasecmp(".sdp", &url[len - 4]))
232 || strstr(url, ".sdp?"))) {
Andreas Huber1b86fe02014-01-29 11:13:26 -0800233 source = new RTSPSource(
234 notify, httpService, url, headers, mUIDValid, mUID, true);
Andreas Huber2bfdd422011-10-11 15:24:07 -0700235 } else {
Chong Zhang3de157d2014-08-05 20:54:44 -0700236 sp<GenericSource> genericSource =
237 new GenericSource(notify, mUIDValid, mUID);
238 // Don't set FLAG_SECURE on mSourceFlags here for widevine.
239 // The correct flags will be updated in Source::kWhatFlagsChanged
240 // handler when GenericSource is prepared.
Andreas Huber2bfdd422011-10-11 15:24:07 -0700241
Chong Zhanga19f33e2014-08-07 15:35:07 -0700242 status_t err = genericSource->setDataSource(httpService, url, headers);
Chong Zhang3de157d2014-08-05 20:54:44 -0700243
244 if (err == OK) {
245 source = genericSource;
246 } else {
Chong Zhanga19f33e2014-08-07 15:35:07 -0700247 ALOGE("Failed to set data source!");
Chong Zhang3de157d2014-08-05 20:54:44 -0700248 }
249 }
Andreas Huberafed0e12011-09-20 15:39:58 -0700250 msg->setObject("source", source);
251 msg->post();
252}
253
Andreas Huber9575c962013-02-05 13:59:56 -0800254void NuPlayer::setDataSourceAsync(int fd, int64_t offset, int64_t length) {
Andreas Huberafed0e12011-09-20 15:39:58 -0700255 sp<AMessage> msg = new AMessage(kWhatSetDataSource, id());
256
Andreas Huberb5f25f02013-02-05 10:14:26 -0800257 sp<AMessage> notify = new AMessage(kWhatSourceNotify, id());
258
Chong Zhang3de157d2014-08-05 20:54:44 -0700259 sp<GenericSource> source =
260 new GenericSource(notify, mUIDValid, mUID);
261
Chong Zhanga19f33e2014-08-07 15:35:07 -0700262 status_t err = source->setDataSource(fd, offset, length);
Chong Zhang3de157d2014-08-05 20:54:44 -0700263
264 if (err != OK) {
Chong Zhanga19f33e2014-08-07 15:35:07 -0700265 ALOGE("Failed to set data source!");
Chong Zhang3de157d2014-08-05 20:54:44 -0700266 source = NULL;
267 }
268
Andreas Huberafed0e12011-09-20 15:39:58 -0700269 msg->setObject("source", source);
Andreas Huberf9334412010-12-15 15:17:42 -0800270 msg->post();
271}
272
Andreas Huber9575c962013-02-05 13:59:56 -0800273void NuPlayer::prepareAsync() {
274 (new AMessage(kWhatPrepare, id()))->post();
275}
276
Andreas Huber57a339c2012-12-03 11:18:00 -0800277void NuPlayer::setVideoSurfaceTextureAsync(
Andy McFadden8ba01022012-12-18 09:46:54 -0800278 const sp<IGraphicBufferProducer> &bufferProducer) {
Glenn Kasten11731182011-02-08 17:26:17 -0800279 sp<AMessage> msg = new AMessage(kWhatSetVideoNativeWindow, id());
Andreas Huber57a339c2012-12-03 11:18:00 -0800280
Andy McFadden8ba01022012-12-18 09:46:54 -0800281 if (bufferProducer == NULL) {
Andreas Huber57a339c2012-12-03 11:18:00 -0800282 msg->setObject("native-window", NULL);
283 } else {
284 msg->setObject(
285 "native-window",
286 new NativeWindowWrapper(
Wei Jia9c03a402014-08-26 15:24:43 -0700287 new Surface(bufferProducer, true /* controlledByApp */)));
Andreas Huber57a339c2012-12-03 11:18:00 -0800288 }
289
Andreas Huberf9334412010-12-15 15:17:42 -0800290 msg->post();
291}
292
293void NuPlayer::setAudioSink(const sp<MediaPlayerBase::AudioSink> &sink) {
294 sp<AMessage> msg = new AMessage(kWhatSetAudioSink, id());
295 msg->setObject("sink", sink);
296 msg->post();
297}
298
299void NuPlayer::start() {
300 (new AMessage(kWhatStart, id()))->post();
301}
302
Andreas Huber43c3e6c2011-01-05 12:17:08 -0800303void NuPlayer::pause() {
Andreas Huberb4082222011-01-20 15:23:04 -0800304 (new AMessage(kWhatPause, id()))->post();
Andreas Huber43c3e6c2011-01-05 12:17:08 -0800305}
306
Andreas Huber1aef2112011-01-04 14:01:29 -0800307void NuPlayer::resetAsync() {
Chong Zhang48296b72014-09-14 14:28:45 -0700308 if (mSource != NULL) {
309 // During a reset, the data source might be unresponsive already, we need to
310 // disconnect explicitly so that reads exit promptly.
311 // We can't queue the disconnect request to the looper, as it might be
312 // queued behind a stuck read and never gets processed.
313 // Doing a disconnect outside the looper to allows the pending reads to exit
314 // (either successfully or with error).
315 mSource->disconnect();
316 }
317
Andreas Huber1aef2112011-01-04 14:01:29 -0800318 (new AMessage(kWhatReset, id()))->post();
319}
320
Wei Jiae427abf2014-09-22 15:21:11 -0700321void NuPlayer::seekToAsync(int64_t seekTimeUs, bool needNotify) {
Andreas Huber43c3e6c2011-01-05 12:17:08 -0800322 sp<AMessage> msg = new AMessage(kWhatSeek, id());
323 msg->setInt64("seekTimeUs", seekTimeUs);
Wei Jiae427abf2014-09-22 15:21:11 -0700324 msg->setInt32("needNotify", needNotify);
Andreas Huber43c3e6c2011-01-05 12:17:08 -0800325 msg->post();
326}
327
Andreas Huber53df1a42010-12-22 10:03:04 -0800328
Chong Zhang404fced2014-06-11 14:45:31 -0700329void NuPlayer::writeTrackInfo(
330 Parcel* reply, const sp<AMessage> format) const {
331 int32_t trackType;
332 CHECK(format->findInt32("type", &trackType));
333
334 AString lang;
335 CHECK(format->findString("language", &lang));
336
337 reply->writeInt32(2); // write something non-zero
338 reply->writeInt32(trackType);
339 reply->writeString16(String16(lang.c_str()));
340
341 if (trackType == MEDIA_TRACK_TYPE_SUBTITLE) {
342 AString mime;
343 CHECK(format->findString("mime", &mime));
344
345 int32_t isAuto, isDefault, isForced;
346 CHECK(format->findInt32("auto", &isAuto));
347 CHECK(format->findInt32("default", &isDefault));
348 CHECK(format->findInt32("forced", &isForced));
349
350 reply->writeString16(String16(mime.c_str()));
351 reply->writeInt32(isAuto);
352 reply->writeInt32(isDefault);
353 reply->writeInt32(isForced);
354 }
355}
356
Andreas Huberf9334412010-12-15 15:17:42 -0800357void NuPlayer::onMessageReceived(const sp<AMessage> &msg) {
358 switch (msg->what()) {
359 case kWhatSetDataSource:
360 {
Steve Block3856b092011-10-20 11:56:00 +0100361 ALOGV("kWhatSetDataSource");
Andreas Huberf9334412010-12-15 15:17:42 -0800362
363 CHECK(mSource == NULL);
364
Chong Zhang3de157d2014-08-05 20:54:44 -0700365 status_t err = OK;
Andreas Huber5bc087c2010-12-23 10:27:40 -0800366 sp<RefBase> obj;
367 CHECK(msg->findObject("source", &obj));
Chong Zhang3de157d2014-08-05 20:54:44 -0700368 if (obj != NULL) {
369 mSource = static_cast<Source *>(obj.get());
Chong Zhang3de157d2014-08-05 20:54:44 -0700370 } else {
371 err = UNKNOWN_ERROR;
372 }
Andreas Huber9575c962013-02-05 13:59:56 -0800373
374 CHECK(mDriver != NULL);
375 sp<NuPlayerDriver> driver = mDriver.promote();
376 if (driver != NULL) {
Chong Zhang3de157d2014-08-05 20:54:44 -0700377 driver->notifySetDataSourceCompleted(err);
Andreas Huber9575c962013-02-05 13:59:56 -0800378 }
379 break;
380 }
381
382 case kWhatPrepare:
383 {
384 mSource->prepareAsync();
Andreas Huberf9334412010-12-15 15:17:42 -0800385 break;
386 }
387
Chong Zhangdcb89b32013-08-06 09:44:47 -0700388 case kWhatGetTrackInfo:
389 {
390 uint32_t replyID;
391 CHECK(msg->senderAwaitsResponse(&replyID));
392
Chong Zhang404fced2014-06-11 14:45:31 -0700393 Parcel* reply;
394 CHECK(msg->findPointer("reply", (void**)&reply));
395
396 size_t inbandTracks = 0;
Chong Zhangdcb89b32013-08-06 09:44:47 -0700397 if (mSource != NULL) {
Chong Zhang404fced2014-06-11 14:45:31 -0700398 inbandTracks = mSource->getTrackCount();
399 }
400
Chong Zhanga7fa1d92014-06-11 14:49:23 -0700401 size_t ccTracks = 0;
402 if (mCCDecoder != NULL) {
403 ccTracks = mCCDecoder->getTrackCount();
404 }
405
Chong Zhang404fced2014-06-11 14:45:31 -0700406 // total track count
Chong Zhanga7fa1d92014-06-11 14:49:23 -0700407 reply->writeInt32(inbandTracks + ccTracks);
Chong Zhang404fced2014-06-11 14:45:31 -0700408
409 // write inband tracks
410 for (size_t i = 0; i < inbandTracks; ++i) {
411 writeTrackInfo(reply, mSource->getTrackInfo(i));
Chong Zhangdcb89b32013-08-06 09:44:47 -0700412 }
413
Chong Zhanga7fa1d92014-06-11 14:49:23 -0700414 // write CC track
415 for (size_t i = 0; i < ccTracks; ++i) {
416 writeTrackInfo(reply, mCCDecoder->getTrackInfo(i));
417 }
418
Chong Zhangdcb89b32013-08-06 09:44:47 -0700419 sp<AMessage> response = new AMessage;
Chong Zhangdcb89b32013-08-06 09:44:47 -0700420 response->postReply(replyID);
421 break;
422 }
423
Robert Shih7c4f0d72014-07-09 18:53:31 -0700424 case kWhatGetSelectedTrack:
425 {
426 status_t err = INVALID_OPERATION;
427 if (mSource != NULL) {
428 err = OK;
429
430 int32_t type32;
431 CHECK(msg->findInt32("type", (int32_t*)&type32));
432 media_track_type type = (media_track_type)type32;
433 ssize_t selectedTrack = mSource->getSelectedTrack(type);
434
435 Parcel* reply;
436 CHECK(msg->findPointer("reply", (void**)&reply));
437 reply->writeInt32(selectedTrack);
438 }
439
440 sp<AMessage> response = new AMessage;
441 response->setInt32("err", err);
442
443 uint32_t replyID;
444 CHECK(msg->senderAwaitsResponse(&replyID));
445 response->postReply(replyID);
446 break;
447 }
448
Chong Zhangdcb89b32013-08-06 09:44:47 -0700449 case kWhatSelectTrack:
450 {
451 uint32_t replyID;
452 CHECK(msg->senderAwaitsResponse(&replyID));
453
Chong Zhang404fced2014-06-11 14:45:31 -0700454 size_t trackIndex;
455 int32_t select;
456 CHECK(msg->findSize("trackIndex", &trackIndex));
457 CHECK(msg->findInt32("select", &select));
458
Chong Zhangdcb89b32013-08-06 09:44:47 -0700459 status_t err = INVALID_OPERATION;
Chong Zhang404fced2014-06-11 14:45:31 -0700460
461 size_t inbandTracks = 0;
Chong Zhangdcb89b32013-08-06 09:44:47 -0700462 if (mSource != NULL) {
Chong Zhang404fced2014-06-11 14:45:31 -0700463 inbandTracks = mSource->getTrackCount();
464 }
Chong Zhanga7fa1d92014-06-11 14:49:23 -0700465 size_t ccTracks = 0;
466 if (mCCDecoder != NULL) {
467 ccTracks = mCCDecoder->getTrackCount();
468 }
Chong Zhang404fced2014-06-11 14:45:31 -0700469
470 if (trackIndex < inbandTracks) {
Chong Zhangdcb89b32013-08-06 09:44:47 -0700471 err = mSource->selectTrack(trackIndex, select);
Robert Shihd3b0bbb2014-07-23 15:00:25 -0700472
473 if (!select && err == OK) {
474 int32_t type;
475 sp<AMessage> info = mSource->getTrackInfo(trackIndex);
476 if (info != NULL
477 && info->findInt32("type", &type)
478 && type == MEDIA_TRACK_TYPE_TIMEDTEXT) {
479 ++mTimedTextGeneration;
480 }
481 }
Chong Zhanga7fa1d92014-06-11 14:49:23 -0700482 } else {
483 trackIndex -= inbandTracks;
484
485 if (trackIndex < ccTracks) {
486 err = mCCDecoder->selectTrack(trackIndex, select);
487 }
Chong Zhangdcb89b32013-08-06 09:44:47 -0700488 }
489
490 sp<AMessage> response = new AMessage;
491 response->setInt32("err", err);
492
493 response->postReply(replyID);
494 break;
495 }
496
Andreas Huberb7c8e912012-11-27 15:02:53 -0800497 case kWhatPollDuration:
498 {
499 int32_t generation;
500 CHECK(msg->findInt32("generation", &generation));
501
502 if (generation != mPollDurationGeneration) {
503 // stale
504 break;
505 }
506
507 int64_t durationUs;
508 if (mDriver != NULL && mSource->getDuration(&durationUs) == OK) {
509 sp<NuPlayerDriver> driver = mDriver.promote();
510 if (driver != NULL) {
511 driver->notifyDuration(durationUs);
512 }
513 }
514
515 msg->post(1000000ll); // poll again in a second.
516 break;
517 }
518
Glenn Kasten11731182011-02-08 17:26:17 -0800519 case kWhatSetVideoNativeWindow:
Andreas Huberf9334412010-12-15 15:17:42 -0800520 {
Steve Block3856b092011-10-20 11:56:00 +0100521 ALOGV("kWhatSetVideoNativeWindow");
Andreas Huberf9334412010-12-15 15:17:42 -0800522
523 sp<RefBase> obj;
Glenn Kasten11731182011-02-08 17:26:17 -0800524 CHECK(msg->findObject("native-window", &obj));
Andreas Huberf9334412010-12-15 15:17:42 -0800525
Wei Jiafef808d2014-10-31 17:57:05 -0700526 if (mSource->getFormat(false /* audio */) == NULL) {
527 performSetSurface(static_cast<NativeWindowWrapper *>(obj.get()));
528 break;
529 }
530
531 mDeferredActions.push_back(
532 new FlushDecoderAction(FLUSH_CMD_FLUSH /* audio */,
533 FLUSH_CMD_SHUTDOWN /* video */));
534
Andreas Huber57a339c2012-12-03 11:18:00 -0800535 mDeferredActions.push_back(
536 new SetSurfaceAction(
537 static_cast<NativeWindowWrapper *>(obj.get())));
James Dong0d268a32012-08-31 12:18:27 -0700538
Andreas Huber57a339c2012-12-03 11:18:00 -0800539 if (obj != NULL) {
Wei Jiafef808d2014-10-31 17:57:05 -0700540 if (mStarted) {
Andy Hung73535852014-09-05 11:42:58 -0700541 // Issue a seek to refresh the video screen only if started otherwise
542 // the extractor may not yet be started and will assert.
543 // If the video decoder is not set (perhaps audio only in this case)
544 // do not perform a seek as it is not needed.
Ronghua Wua73d9e02014-10-08 15:13:29 -0700545 int64_t currentPositionUs = 0;
546 if (getCurrentPosition(&currentPositionUs) == OK) {
547 mDeferredActions.push_back(
548 new SeekAction(currentPositionUs, false /* needNotify */));
549 }
Andy Hung73535852014-09-05 11:42:58 -0700550 }
Wei Jiaac428aa2014-09-02 19:01:34 -0700551
Andreas Huber57a339c2012-12-03 11:18:00 -0800552 // If there is a new surface texture, instantiate decoders
553 // again if possible.
554 mDeferredActions.push_back(
555 new SimpleAction(&NuPlayer::performScanSources));
556 }
557
558 processDeferredActions();
Andreas Huberf9334412010-12-15 15:17:42 -0800559 break;
560 }
561
562 case kWhatSetAudioSink:
563 {
Steve Block3856b092011-10-20 11:56:00 +0100564 ALOGV("kWhatSetAudioSink");
Andreas Huberf9334412010-12-15 15:17:42 -0800565
566 sp<RefBase> obj;
567 CHECK(msg->findObject("sink", &obj));
568
569 mAudioSink = static_cast<MediaPlayerBase::AudioSink *>(obj.get());
570 break;
571 }
572
573 case kWhatStart:
574 {
Steve Block3856b092011-10-20 11:56:00 +0100575 ALOGV("kWhatStart");
Wei Jia94211742014-10-28 17:09:06 -0700576 if (mStarted) {
577 onResume();
578 } else {
579 onStart();
Lajos Molnar09524832014-07-17 14:29:51 -0700580 }
Andreas Huberf9334412010-12-15 15:17:42 -0800581 break;
582 }
583
584 case kWhatScanSources:
585 {
Andreas Huber1aef2112011-01-04 14:01:29 -0800586 int32_t generation;
587 CHECK(msg->findInt32("generation", &generation));
588 if (generation != mScanSourcesGeneration) {
589 // Drop obsolete msg.
590 break;
591 }
592
Andreas Huber5bc087c2010-12-23 10:27:40 -0800593 mScanSourcesPending = false;
594
Steve Block3856b092011-10-20 11:56:00 +0100595 ALOGV("scanning sources haveAudio=%d, haveVideo=%d",
Andreas Huber43c3e6c2011-01-05 12:17:08 -0800596 mAudioDecoder != NULL, mVideoDecoder != NULL);
597
Andreas Huberb7c8e912012-11-27 15:02:53 -0800598 bool mHadAnySourcesBefore =
599 (mAudioDecoder != NULL) || (mVideoDecoder != NULL);
600
Andy Hung282a7e32014-08-14 15:56:34 -0700601 // initialize video before audio because successful initialization of
602 // video may change deep buffer mode of audio.
Haynes Mathew George5d246ef2012-07-09 10:36:57 -0700603 if (mNativeWindow != NULL) {
604 instantiateDecoder(false, &mVideoDecoder);
605 }
Andreas Huberf9334412010-12-15 15:17:42 -0800606
Ronghua Wua10fd232014-11-06 16:15:20 -0800607 // Don't try to re-open audio sink if there's an existing decoder.
608 if (mAudioSink != NULL && mAudioDecoder == NULL) {
609 sp<MetaData> audioMeta = mSource->getFormatMeta(true /* audio */);
610 sp<AMessage> videoFormat = mSource->getFormat(false /* audio */);
611 audio_stream_type_t streamType = mAudioSink->getAudioStreamType();
612 bool canOffload = canOffloadStream(audioMeta, (videoFormat != NULL),
613 true /* is_streaming */, streamType);
614 if (canOffload) {
615 if (!mOffloadAudio) {
616 mRenderer->signalEnableOffloadAudio();
617 }
Andy Hung282a7e32014-08-14 15:56:34 -0700618 // open audio sink early under offload mode.
619 sp<AMessage> format = mSource->getFormat(true /*audio*/);
620 openAudioSink(format, true /*offloadOnly*/);
621 }
Andreas Huber5bc087c2010-12-23 10:27:40 -0800622 instantiateDecoder(true, &mAudioDecoder);
Andreas Huberf9334412010-12-15 15:17:42 -0800623 }
624
Andreas Huberb7c8e912012-11-27 15:02:53 -0800625 if (!mHadAnySourcesBefore
626 && (mAudioDecoder != NULL || mVideoDecoder != NULL)) {
627 // This is the first time we've found anything playable.
628
Andreas Huber9575c962013-02-05 13:59:56 -0800629 if (mSourceFlags & Source::FLAG_DYNAMIC_DURATION) {
Andreas Huberb7c8e912012-11-27 15:02:53 -0800630 schedulePollDuration();
631 }
632 }
633
Andreas Hubereac68ba2011-09-27 12:12:25 -0700634 status_t err;
635 if ((err = mSource->feedMoreTSData()) != OK) {
Andreas Huber1aef2112011-01-04 14:01:29 -0800636 if (mAudioDecoder == NULL && mVideoDecoder == NULL) {
637 // We're not currently decoding anything (no audio or
638 // video tracks found) and we just ran out of input data.
Andreas Hubereac68ba2011-09-27 12:12:25 -0700639
640 if (err == ERROR_END_OF_STREAM) {
641 notifyListener(MEDIA_PLAYBACK_COMPLETE, 0, 0);
642 } else {
643 notifyListener(MEDIA_ERROR, MEDIA_ERROR_UNKNOWN, err);
644 }
Andreas Huber1aef2112011-01-04 14:01:29 -0800645 }
Andreas Huberf9334412010-12-15 15:17:42 -0800646 break;
647 }
648
Andreas Huberfbe9d812012-08-31 14:05:27 -0700649 if ((mAudioDecoder == NULL && mAudioSink != NULL)
650 || (mVideoDecoder == NULL && mNativeWindow != NULL)) {
Andreas Huberf9334412010-12-15 15:17:42 -0800651 msg->post(100000ll);
Andreas Huber5bc087c2010-12-23 10:27:40 -0800652 mScanSourcesPending = true;
Andreas Huberf9334412010-12-15 15:17:42 -0800653 }
654 break;
655 }
656
657 case kWhatVideoNotify:
658 case kWhatAudioNotify:
659 {
660 bool audio = msg->what() == kWhatAudioNotify;
661
Wei Jia88703c32014-08-06 11:24:07 -0700662 int32_t currentDecoderGeneration =
663 (audio? mAudioDecoderGeneration : mVideoDecoderGeneration);
664 int32_t requesterGeneration = currentDecoderGeneration - 1;
665 CHECK(msg->findInt32("generation", &requesterGeneration));
666
667 if (requesterGeneration != currentDecoderGeneration) {
668 ALOGV("got message from old %s decoder, generation(%d:%d)",
669 audio ? "audio" : "video", requesterGeneration,
670 currentDecoderGeneration);
671 sp<AMessage> reply;
672 if (!(msg->findMessage("reply", &reply))) {
673 return;
674 }
675
676 reply->setInt32("err", INFO_DISCONTINUITY);
677 reply->post();
678 return;
679 }
680
Andreas Huberf9334412010-12-15 15:17:42 -0800681 int32_t what;
Lajos Molnar1cd13982014-01-17 15:12:51 -0800682 CHECK(msg->findInt32("what", &what));
Andreas Huberf9334412010-12-15 15:17:42 -0800683
Lajos Molnar1cd13982014-01-17 15:12:51 -0800684 if (what == Decoder::kWhatFillThisBuffer) {
Andreas Huberf9334412010-12-15 15:17:42 -0800685 status_t err = feedDecoderInputData(
Lajos Molnar1cd13982014-01-17 15:12:51 -0800686 audio, msg);
Andreas Huberf9334412010-12-15 15:17:42 -0800687
Andreas Huber5bc087c2010-12-23 10:27:40 -0800688 if (err == -EWOULDBLOCK) {
Andreas Hubereac68ba2011-09-27 12:12:25 -0700689 if (mSource->feedMoreTSData() == OK) {
Phil Burkc5cc2e22014-09-09 20:08:39 -0700690 msg->post(10 * 1000ll);
Andreas Huber5bc087c2010-12-23 10:27:40 -0800691 }
Andreas Huberf9334412010-12-15 15:17:42 -0800692 }
Lajos Molnar1cd13982014-01-17 15:12:51 -0800693 } else if (what == Decoder::kWhatEOS) {
Andreas Huberdc9bacd2011-09-26 10:53:29 -0700694 int32_t err;
Lajos Molnar1cd13982014-01-17 15:12:51 -0800695 CHECK(msg->findInt32("err", &err));
Andreas Huberdc9bacd2011-09-26 10:53:29 -0700696
697 if (err == ERROR_END_OF_STREAM) {
Steve Block3856b092011-10-20 11:56:00 +0100698 ALOGV("got %s decoder EOS", audio ? "audio" : "video");
Andreas Huberdc9bacd2011-09-26 10:53:29 -0700699 } else {
Steve Block3856b092011-10-20 11:56:00 +0100700 ALOGV("got %s decoder EOS w/ error %d",
Andreas Huberdc9bacd2011-09-26 10:53:29 -0700701 audio ? "audio" : "video",
702 err);
703 }
704
705 mRenderer->queueEOS(audio, err);
Lajos Molnar1cd13982014-01-17 15:12:51 -0800706 } else if (what == Decoder::kWhatFlushCompleted) {
Steve Block3856b092011-10-20 11:56:00 +0100707 ALOGV("decoder %s flush completed", audio ? "audio" : "video");
Andreas Huberf9334412010-12-15 15:17:42 -0800708
Andy Hung8d121d42014-10-03 09:53:53 -0700709 handleFlushComplete(audio, true /* isDecoder */);
Andreas Huber3831a062010-12-21 10:22:33 -0800710 finishFlushIfPossible();
Wei Jiac6cfd702014-11-11 16:33:20 -0800711 } else if (what == Decoder::kWhatVideoSizeChanged) {
Lajos Molnar1cd13982014-01-17 15:12:51 -0800712 sp<AMessage> format;
713 CHECK(msg->findMessage("format", &format));
714
Wei Jiac6cfd702014-11-11 16:33:20 -0800715 sp<AMessage> inputFormat =
716 mSource->getFormat(false /* audio */);
Andreas Huber3831a062010-12-21 10:22:33 -0800717
Wei Jiac6cfd702014-11-11 16:33:20 -0800718 updateVideoSize(inputFormat, format);
Lajos Molnar1cd13982014-01-17 15:12:51 -0800719 } else if (what == Decoder::kWhatShutdownCompleted) {
Steve Block3856b092011-10-20 11:56:00 +0100720 ALOGV("%s shutdown completed", audio ? "audio" : "video");
Andreas Huber3831a062010-12-21 10:22:33 -0800721 if (audio) {
722 mAudioDecoder.clear();
Wei Jia57568df2014-09-22 10:16:29 -0700723 ++mAudioDecoderGeneration;
Andreas Huber3831a062010-12-21 10:22:33 -0800724
725 CHECK_EQ((int)mFlushingAudio, (int)SHUTTING_DOWN_DECODER);
726 mFlushingAudio = SHUT_DOWN;
727 } else {
728 mVideoDecoder.clear();
Wei Jia57568df2014-09-22 10:16:29 -0700729 ++mVideoDecoderGeneration;
Andreas Huber3831a062010-12-21 10:22:33 -0800730
731 CHECK_EQ((int)mFlushingVideo, (int)SHUTTING_DOWN_DECODER);
732 mFlushingVideo = SHUT_DOWN;
733 }
734
735 finishFlushIfPossible();
Lajos Molnar1cd13982014-01-17 15:12:51 -0800736 } else if (what == Decoder::kWhatError) {
Chong Zhangf4c0a942014-08-11 15:14:10 -0700737 status_t err;
Andy Hung2abde2c2014-09-30 14:40:32 -0700738 if (!msg->findInt32("err", &err) || err == OK) {
Chong Zhangf4c0a942014-08-11 15:14:10 -0700739 err = UNKNOWN_ERROR;
740 }
Andy Hungcf31f1e2014-09-23 14:59:01 -0700741
Andy Hung2abde2c2014-09-30 14:40:32 -0700742 // Decoder errors can be due to Source (e.g. from streaming),
743 // or from decoding corrupted bitstreams, or from other decoder
744 // MediaCodec operations (e.g. from an ongoing reset or seek).
745 //
746 // We try to gracefully shut down the affected decoder if possible,
747 // rather than trying to force the shutdown with something
748 // similar to performReset(). This method can lead to a hang
749 // if MediaCodec functions block after an error, but they should
750 // typically return INVALID_OPERATION instead of blocking.
751
752 FlushStatus *flushing = audio ? &mFlushingAudio : &mFlushingVideo;
753 ALOGE("received error(%#x) from %s decoder, flushing(%d), now shutting down",
754 err, audio ? "audio" : "video", *flushing);
755
756 switch (*flushing) {
757 case NONE:
758 mDeferredActions.push_back(
Wei Jiafef808d2014-10-31 17:57:05 -0700759 new FlushDecoderAction(
760 audio ? FLUSH_CMD_SHUTDOWN : FLUSH_CMD_NONE,
761 audio ? FLUSH_CMD_NONE : FLUSH_CMD_SHUTDOWN));
Andy Hung2abde2c2014-09-30 14:40:32 -0700762 processDeferredActions();
763 break;
764 case FLUSHING_DECODER:
765 *flushing = FLUSHING_DECODER_SHUTDOWN; // initiate shutdown after flush.
766 break; // Wait for flush to complete.
767 case FLUSHING_DECODER_SHUTDOWN:
768 break; // Wait for flush to complete.
769 case SHUTTING_DOWN_DECODER:
770 break; // Wait for shutdown to complete.
771 case FLUSHED:
772 // Widevine source reads must stop before releasing the video decoder.
773 if (!audio && mSource != NULL && mSourceFlags & Source::FLAG_SECURE) {
774 mSource->stop();
775 }
776 getDecoder(audio)->initiateShutdown(); // In the middle of a seek.
777 *flushing = SHUTTING_DOWN_DECODER; // Shut down.
778 break;
779 case SHUT_DOWN:
780 finishFlushIfPossible(); // Should not occur.
781 break; // Finish anyways.
Marco Nelissen9e2b7912014-08-18 16:13:03 -0700782 }
Andy Hung2abde2c2014-09-30 14:40:32 -0700783 notifyListener(MEDIA_ERROR, MEDIA_ERROR_UNKNOWN, err);
Wei Jiac6cfd702014-11-11 16:33:20 -0800784 } else if (what == Decoder::kWhatRenderBufferTime) {
Lajos Molnar1cd13982014-01-17 15:12:51 -0800785 renderBuffer(audio, msg);
786 } else {
787 ALOGV("Unhandled decoder notification %d '%c%c%c%c'.",
Andreas Hubera1f8ab02012-11-30 10:53:22 -0800788 what,
789 what >> 24,
790 (what >> 16) & 0xff,
791 (what >> 8) & 0xff,
792 what & 0xff);
Andreas Huberf9334412010-12-15 15:17:42 -0800793 }
794
795 break;
796 }
797
798 case kWhatRendererNotify:
799 {
Wei Jia57568df2014-09-22 10:16:29 -0700800 int32_t requesterGeneration = mRendererGeneration - 1;
801 CHECK(msg->findInt32("generation", &requesterGeneration));
802 if (requesterGeneration != mRendererGeneration) {
803 ALOGV("got message from old renderer, generation(%d:%d)",
804 requesterGeneration, mRendererGeneration);
805 return;
806 }
807
Andreas Huberf9334412010-12-15 15:17:42 -0800808 int32_t what;
809 CHECK(msg->findInt32("what", &what));
810
811 if (what == Renderer::kWhatEOS) {
812 int32_t audio;
813 CHECK(msg->findInt32("audio", &audio));
814
Andreas Huberc92fd242011-08-16 13:48:44 -0700815 int32_t finalResult;
816 CHECK(msg->findInt32("finalResult", &finalResult));
817
Andreas Huberf9334412010-12-15 15:17:42 -0800818 if (audio) {
819 mAudioEOS = true;
820 } else {
821 mVideoEOS = true;
822 }
823
Andreas Huberc92fd242011-08-16 13:48:44 -0700824 if (finalResult == ERROR_END_OF_STREAM) {
Steve Block3856b092011-10-20 11:56:00 +0100825 ALOGV("reached %s EOS", audio ? "audio" : "video");
Andreas Huberc92fd242011-08-16 13:48:44 -0700826 } else {
Steve Block29357bc2012-01-06 19:20:56 +0000827 ALOGE("%s track encountered an error (%d)",
Andreas Huberc92fd242011-08-16 13:48:44 -0700828 audio ? "audio" : "video", finalResult);
829
830 notifyListener(
831 MEDIA_ERROR, MEDIA_ERROR_UNKNOWN, finalResult);
832 }
Andreas Huberf9334412010-12-15 15:17:42 -0800833
834 if ((mAudioEOS || mAudioDecoder == NULL)
835 && (mVideoEOS || mVideoDecoder == NULL)) {
836 notifyListener(MEDIA_PLAYBACK_COMPLETE, 0, 0);
837 }
Andreas Huber3fe62152011-09-16 15:09:22 -0700838 } else if (what == Renderer::kWhatFlushComplete) {
Andreas Huberf9334412010-12-15 15:17:42 -0800839 int32_t audio;
840 CHECK(msg->findInt32("audio", &audio));
841
Steve Block3856b092011-10-20 11:56:00 +0100842 ALOGV("renderer %s flush completed.", audio ? "audio" : "video");
Andy Hung8d121d42014-10-03 09:53:53 -0700843 handleFlushComplete(audio, false /* isDecoder */);
844 finishFlushIfPossible();
James Dongf57b4ea2012-07-20 13:38:36 -0700845 } else if (what == Renderer::kWhatVideoRenderingStart) {
846 notifyListener(MEDIA_INFO, MEDIA_INFO_RENDERING_START, 0);
Lajos Molnarcbaffcf2013-08-14 18:30:38 -0700847 } else if (what == Renderer::kWhatMediaRenderingStart) {
848 ALOGV("media rendering started");
849 notifyListener(MEDIA_STARTED, 0, 0);
Wei Jia3a2956d2014-07-22 16:01:33 -0700850 } else if (what == Renderer::kWhatAudioOffloadTearDown) {
Ronghua Wua10fd232014-11-06 16:15:20 -0800851 ALOGV("Tear down audio offload, fall back to s/w path if due to error.");
Wei Jia3a2956d2014-07-22 16:01:33 -0700852 int64_t positionUs;
853 CHECK(msg->findInt64("positionUs", &positionUs));
Ronghua Wu08529172014-10-02 16:55:52 -0700854 int32_t reason;
855 CHECK(msg->findInt32("reason", &reason));
Andy Hung282a7e32014-08-14 15:56:34 -0700856 closeAudioSink();
Wei Jia3a2956d2014-07-22 16:01:33 -0700857 mAudioDecoder.clear();
Wei Jia57568df2014-09-22 10:16:29 -0700858 ++mAudioDecoderGeneration;
Wei Jia3a2956d2014-07-22 16:01:33 -0700859 mRenderer->flush(true /* audio */);
860 if (mVideoDecoder != NULL) {
861 mRenderer->flush(false /* audio */);
862 }
Wei Jia3a2956d2014-07-22 16:01:33 -0700863
Wei Jiae427abf2014-09-22 15:21:11 -0700864 performSeek(positionUs, false /* needNotify */);
Ronghua Wu08529172014-10-02 16:55:52 -0700865 if (reason == Renderer::kDueToError) {
Ronghua Wua10fd232014-11-06 16:15:20 -0800866 mRenderer->signalDisableOffloadAudio();
867 mOffloadAudio = false;
Ronghua Wu08529172014-10-02 16:55:52 -0700868 instantiateDecoder(true /* audio */, &mAudioDecoder);
869 }
Andreas Huberf9334412010-12-15 15:17:42 -0800870 }
871 break;
872 }
873
874 case kWhatMoreDataQueued:
875 {
876 break;
877 }
878
Andreas Huber1aef2112011-01-04 14:01:29 -0800879 case kWhatReset:
880 {
Steve Block3856b092011-10-20 11:56:00 +0100881 ALOGV("kWhatReset");
Andreas Huber1aef2112011-01-04 14:01:29 -0800882
Andreas Hubera1f8ab02012-11-30 10:53:22 -0800883 mDeferredActions.push_back(
Wei Jiafef808d2014-10-31 17:57:05 -0700884 new FlushDecoderAction(
885 FLUSH_CMD_SHUTDOWN /* audio */,
886 FLUSH_CMD_SHUTDOWN /* video */));
Andreas Huberb7c8e912012-11-27 15:02:53 -0800887
Andreas Hubera1f8ab02012-11-30 10:53:22 -0800888 mDeferredActions.push_back(
889 new SimpleAction(&NuPlayer::performReset));
Andreas Huberb58ce9f2011-11-28 16:27:35 -0800890
Andreas Hubera1f8ab02012-11-30 10:53:22 -0800891 processDeferredActions();
Andreas Huber1aef2112011-01-04 14:01:29 -0800892 break;
893 }
894
Andreas Huber43c3e6c2011-01-05 12:17:08 -0800895 case kWhatSeek:
896 {
897 int64_t seekTimeUs;
Wei Jiae427abf2014-09-22 15:21:11 -0700898 int32_t needNotify;
Andreas Huber43c3e6c2011-01-05 12:17:08 -0800899 CHECK(msg->findInt64("seekTimeUs", &seekTimeUs));
Wei Jiae427abf2014-09-22 15:21:11 -0700900 CHECK(msg->findInt32("needNotify", &needNotify));
Andreas Huber43c3e6c2011-01-05 12:17:08 -0800901
Wei Jiae427abf2014-09-22 15:21:11 -0700902 ALOGV("kWhatSeek seekTimeUs=%lld us, needNotify=%d",
903 seekTimeUs, needNotify);
Andreas Huber43c3e6c2011-01-05 12:17:08 -0800904
Andreas Hubera1f8ab02012-11-30 10:53:22 -0800905 mDeferredActions.push_back(
Wei Jiafef808d2014-10-31 17:57:05 -0700906 new FlushDecoderAction(FLUSH_CMD_FLUSH /* audio */,
907 FLUSH_CMD_FLUSH /* video */));
Andreas Huber43c3e6c2011-01-05 12:17:08 -0800908
Wei Jiae427abf2014-09-22 15:21:11 -0700909 mDeferredActions.push_back(
910 new SeekAction(seekTimeUs, needNotify));
Andreas Huber43c3e6c2011-01-05 12:17:08 -0800911
Andreas Hubera1f8ab02012-11-30 10:53:22 -0800912 processDeferredActions();
Andreas Huber43c3e6c2011-01-05 12:17:08 -0800913 break;
914 }
915
Andreas Huberb4082222011-01-20 15:23:04 -0800916 case kWhatPause:
917 {
Wei Jia7e9f7f72014-09-23 10:55:35 -0700918 if (mSource != NULL) {
919 mSource->pause();
920 } else {
921 ALOGW("pause called when source is gone or not set");
922 }
923 if (mRenderer != NULL) {
924 mRenderer->pause();
925 } else {
926 ALOGW("pause called when renderer is gone or not set");
927 }
Andreas Huberb4082222011-01-20 15:23:04 -0800928 break;
929 }
930
Andreas Huberb5f25f02013-02-05 10:14:26 -0800931 case kWhatSourceNotify:
932 {
Andreas Huber9575c962013-02-05 13:59:56 -0800933 onSourceNotify(msg);
Andreas Huberb5f25f02013-02-05 10:14:26 -0800934 break;
935 }
936
Chong Zhanga7fa1d92014-06-11 14:49:23 -0700937 case kWhatClosedCaptionNotify:
938 {
939 onClosedCaptionNotify(msg);
940 break;
941 }
942
Andreas Huberf9334412010-12-15 15:17:42 -0800943 default:
944 TRESPASS();
945 break;
946 }
947}
948
Wei Jia94211742014-10-28 17:09:06 -0700949void NuPlayer::onResume() {
950 if (mSource != NULL) {
951 mSource->resume();
952 } else {
953 ALOGW("resume called when source is gone or not set");
954 }
955 // |mAudioDecoder| may have been released due to the pause timeout, so re-create it if
956 // needed.
957 if (audioDecoderStillNeeded() && mAudioDecoder == NULL) {
958 instantiateDecoder(true /* audio */, &mAudioDecoder);
959 }
960 if (mRenderer != NULL) {
961 mRenderer->resume();
962 } else {
963 ALOGW("resume called when renderer is gone or not set");
964 }
965}
966
967void NuPlayer::onStart() {
968 mVideoIsAVC = false;
969 mOffloadAudio = false;
970 mAudioEOS = false;
971 mVideoEOS = false;
Wei Jia94211742014-10-28 17:09:06 -0700972 mNumFramesTotal = 0;
973 mNumFramesDropped = 0;
974 mStarted = true;
975
976 /* instantiate decoders now for secure playback */
977 if (mSourceFlags & Source::FLAG_SECURE) {
978 if (mNativeWindow != NULL) {
979 instantiateDecoder(false, &mVideoDecoder);
980 }
981
982 if (mAudioSink != NULL) {
983 instantiateDecoder(true, &mAudioDecoder);
984 }
985 }
986
987 mSource->start();
988
989 uint32_t flags = 0;
990
991 if (mSource->isRealTime()) {
992 flags |= Renderer::FLAG_REAL_TIME;
993 }
994
995 sp<MetaData> audioMeta = mSource->getFormatMeta(true /* audio */);
996 audio_stream_type_t streamType = AUDIO_STREAM_MUSIC;
997 if (mAudioSink != NULL) {
998 streamType = mAudioSink->getAudioStreamType();
999 }
1000
1001 sp<AMessage> videoFormat = mSource->getFormat(false /* audio */);
1002
1003 mOffloadAudio =
1004 canOffloadStream(audioMeta, (videoFormat != NULL),
1005 true /* is_streaming */, streamType);
1006 if (mOffloadAudio) {
1007 flags |= Renderer::FLAG_OFFLOAD_AUDIO;
1008 }
1009
1010 sp<AMessage> notify = new AMessage(kWhatRendererNotify, id());
1011 ++mRendererGeneration;
1012 notify->setInt32("generation", mRendererGeneration);
1013 mRenderer = new Renderer(mAudioSink, notify, flags);
1014
1015 mRendererLooper = new ALooper;
1016 mRendererLooper->setName("NuPlayerRenderer");
1017 mRendererLooper->start(false, false, ANDROID_PRIORITY_AUDIO);
1018 mRendererLooper->registerHandler(mRenderer);
1019
1020 sp<MetaData> meta = getFileMeta();
1021 int32_t rate;
1022 if (meta != NULL
1023 && meta->findInt32(kKeyFrameRate, &rate) && rate > 0) {
1024 mRenderer->setVideoFrameRate(rate);
1025 }
1026
Wei Jiac6cfd702014-11-11 16:33:20 -08001027 if (mVideoDecoder != NULL) {
1028 mVideoDecoder->setRenderer(mRenderer);
1029 }
1030 if (mAudioDecoder != NULL) {
1031 mAudioDecoder->setRenderer(mRenderer);
1032 }
1033
Wei Jia94211742014-10-28 17:09:06 -07001034 postScanSources();
1035}
1036
Ronghua Wud7988b12014-10-03 15:19:10 -07001037bool NuPlayer::audioDecoderStillNeeded() {
1038 // Audio decoder is no longer needed if it's in shut/shutting down status.
1039 return ((mFlushingAudio != SHUT_DOWN) && (mFlushingAudio != SHUTTING_DOWN_DECODER));
1040}
1041
Andy Hung8d121d42014-10-03 09:53:53 -07001042void NuPlayer::handleFlushComplete(bool audio, bool isDecoder) {
1043 // We wait for both the decoder flush and the renderer flush to complete
1044 // before entering either the FLUSHED or the SHUTTING_DOWN_DECODER state.
1045
1046 mFlushComplete[audio][isDecoder] = true;
1047 if (!mFlushComplete[audio][!isDecoder]) {
1048 return;
1049 }
1050
1051 FlushStatus *state = audio ? &mFlushingAudio : &mFlushingVideo;
1052 switch (*state) {
1053 case FLUSHING_DECODER:
1054 {
1055 *state = FLUSHED;
Andy Hung8d121d42014-10-03 09:53:53 -07001056 break;
1057 }
1058
1059 case FLUSHING_DECODER_SHUTDOWN:
1060 {
1061 *state = SHUTTING_DOWN_DECODER;
1062
1063 ALOGV("initiating %s decoder shutdown", audio ? "audio" : "video");
1064 if (!audio) {
Andy Hung8d121d42014-10-03 09:53:53 -07001065 // Widevine source reads must stop before releasing the video decoder.
1066 if (mSource != NULL && mSourceFlags & Source::FLAG_SECURE) {
1067 mSource->stop();
1068 }
1069 }
1070 getDecoder(audio)->initiateShutdown();
1071 break;
1072 }
1073
1074 default:
1075 // decoder flush completes only occur in a flushing state.
1076 LOG_ALWAYS_FATAL_IF(isDecoder, "decoder flush in invalid state %d", *state);
1077 break;
1078 }
1079}
1080
Andreas Huber3831a062010-12-21 10:22:33 -08001081void NuPlayer::finishFlushIfPossible() {
Wei Jia53904f32014-07-29 10:22:53 -07001082 if (mFlushingAudio != NONE && mFlushingAudio != FLUSHED
1083 && mFlushingAudio != SHUT_DOWN) {
Andreas Huber3831a062010-12-21 10:22:33 -08001084 return;
1085 }
1086
Wei Jia53904f32014-07-29 10:22:53 -07001087 if (mFlushingVideo != NONE && mFlushingVideo != FLUSHED
1088 && mFlushingVideo != SHUT_DOWN) {
Andreas Huber3831a062010-12-21 10:22:33 -08001089 return;
1090 }
1091
Steve Block3856b092011-10-20 11:56:00 +01001092 ALOGV("both audio and video are flushed now.");
Andreas Huber3831a062010-12-21 10:22:33 -08001093
Phil Burk9f526492014-09-03 15:04:12 -07001094 mPendingAudioAccessUnit.clear();
Phil Burkc5cc2e22014-09-09 20:08:39 -07001095 mAggregateBuffer.clear();
Phil Burk9f526492014-09-03 15:04:12 -07001096
Andreas Huber6e3d3112011-11-28 12:36:11 -08001097 if (mTimeDiscontinuityPending) {
1098 mRenderer->signalTimeDiscontinuity();
1099 mTimeDiscontinuityPending = false;
1100 }
Andreas Huber3831a062010-12-21 10:22:33 -08001101
Wei Jia53904f32014-07-29 10:22:53 -07001102 if (mAudioDecoder != NULL && mFlushingAudio == FLUSHED) {
Andreas Huber3831a062010-12-21 10:22:33 -08001103 mAudioDecoder->signalResume();
1104 }
1105
Wei Jia53904f32014-07-29 10:22:53 -07001106 if (mVideoDecoder != NULL && mFlushingVideo == FLUSHED) {
Andreas Huber3831a062010-12-21 10:22:33 -08001107 mVideoDecoder->signalResume();
1108 }
1109
1110 mFlushingAudio = NONE;
1111 mFlushingVideo = NONE;
Andreas Huber3831a062010-12-21 10:22:33 -08001112
Andy Hung8d121d42014-10-03 09:53:53 -07001113 clearFlushComplete();
1114
Andreas Hubera1f8ab02012-11-30 10:53:22 -08001115 processDeferredActions();
Andreas Huber1aef2112011-01-04 14:01:29 -08001116}
1117
1118void NuPlayer::postScanSources() {
1119 if (mScanSourcesPending) {
1120 return;
1121 }
1122
1123 sp<AMessage> msg = new AMessage(kWhatScanSources, id());
1124 msg->setInt32("generation", mScanSourcesGeneration);
1125 msg->post();
1126
1127 mScanSourcesPending = true;
1128}
1129
Andy Hung282a7e32014-08-14 15:56:34 -07001130void NuPlayer::openAudioSink(const sp<AMessage> &format, bool offloadOnly) {
Andy Hung282a7e32014-08-14 15:56:34 -07001131 uint32_t flags;
1132 int64_t durationUs;
Chong Zhang3b9eb1f2014-10-15 17:05:08 -07001133 bool hasVideo = (mVideoDecoder != NULL);
Andy Hung282a7e32014-08-14 15:56:34 -07001134 // FIXME: we should handle the case where the video decoder
1135 // is created after we receive the format change indication.
1136 // Current code will just make that we select deep buffer
1137 // with video which should not be a problem as it should
1138 // not prevent from keeping A/V sync.
Eric Laurentd88c3ca2014-10-28 10:52:11 -07001139 if (!hasVideo &&
Andy Hung282a7e32014-08-14 15:56:34 -07001140 mSource->getDuration(&durationUs) == OK &&
1141 durationUs
1142 > AUDIO_SINK_MIN_DEEP_BUFFER_DURATION_US) {
1143 flags = AUDIO_OUTPUT_FLAG_DEEP_BUFFER;
1144 } else {
1145 flags = AUDIO_OUTPUT_FLAG_NONE;
1146 }
1147
Chong Zhang3b9eb1f2014-10-15 17:05:08 -07001148 mOffloadAudio = mRenderer->openAudioSink(
1149 format, offloadOnly, hasVideo, flags);
1150
Andy Hung282a7e32014-08-14 15:56:34 -07001151 if (mOffloadAudio) {
Chong Zhang3b9eb1f2014-10-15 17:05:08 -07001152 sp<MetaData> audioMeta =
1153 mSource->getFormatMeta(true /* audio */);
1154 sendMetaDataToHal(mAudioSink, audioMeta);
Andy Hung282a7e32014-08-14 15:56:34 -07001155 }
1156}
1157
1158void NuPlayer::closeAudioSink() {
Chong Zhang3b9eb1f2014-10-15 17:05:08 -07001159 mRenderer->closeAudioSink();
Andy Hung282a7e32014-08-14 15:56:34 -07001160}
1161
Andreas Huber5bc087c2010-12-23 10:27:40 -08001162status_t NuPlayer::instantiateDecoder(bool audio, sp<Decoder> *decoder) {
Andreas Huberf9334412010-12-15 15:17:42 -08001163 if (*decoder != NULL) {
1164 return OK;
1165 }
1166
Andreas Huber84066782011-08-16 09:34:26 -07001167 sp<AMessage> format = mSource->getFormat(audio);
Andreas Huberf9334412010-12-15 15:17:42 -08001168
Andreas Huber84066782011-08-16 09:34:26 -07001169 if (format == NULL) {
Andreas Huberf9334412010-12-15 15:17:42 -08001170 return -EWOULDBLOCK;
1171 }
1172
Andreas Huber3fe62152011-09-16 15:09:22 -07001173 if (!audio) {
Andreas Huber84066782011-08-16 09:34:26 -07001174 AString mime;
1175 CHECK(format->findString("mime", &mime));
1176 mVideoIsAVC = !strcasecmp(MEDIA_MIMETYPE_VIDEO_AVC, mime.c_str());
Chong Zhanga7fa1d92014-06-11 14:49:23 -07001177
1178 sp<AMessage> ccNotify = new AMessage(kWhatClosedCaptionNotify, id());
1179 mCCDecoder = new CCDecoder(ccNotify);
Lajos Molnar09524832014-07-17 14:29:51 -07001180
1181 if (mSourceFlags & Source::FLAG_SECURE) {
1182 format->setInt32("secure", true);
1183 }
Andreas Huber3fe62152011-09-16 15:09:22 -07001184 }
1185
Wei Jiabc2fb722014-07-08 16:37:57 -07001186 if (audio) {
Wei Jia88703c32014-08-06 11:24:07 -07001187 sp<AMessage> notify = new AMessage(kWhatAudioNotify, id());
1188 ++mAudioDecoderGeneration;
1189 notify->setInt32("generation", mAudioDecoderGeneration);
1190
Wei Jiabc2fb722014-07-08 16:37:57 -07001191 if (mOffloadAudio) {
Wei Jiac6cfd702014-11-11 16:33:20 -08001192 *decoder = new DecoderPassThrough(notify, mSource, mRenderer);
Wei Jiabc2fb722014-07-08 16:37:57 -07001193 } else {
Wei Jiac6cfd702014-11-11 16:33:20 -08001194 *decoder = new Decoder(notify, mSource, mRenderer);
Wei Jiabc2fb722014-07-08 16:37:57 -07001195 }
1196 } else {
Wei Jia88703c32014-08-06 11:24:07 -07001197 sp<AMessage> notify = new AMessage(kWhatVideoNotify, id());
1198 ++mVideoDecoderGeneration;
1199 notify->setInt32("generation", mVideoDecoderGeneration);
1200
Wei Jiac6cfd702014-11-11 16:33:20 -08001201 *decoder = new Decoder(notify, mSource, mRenderer, mNativeWindow);
Wei Jiabc2fb722014-07-08 16:37:57 -07001202 }
Lajos Molnar1cd13982014-01-17 15:12:51 -08001203 (*decoder)->init();
Andreas Huber84066782011-08-16 09:34:26 -07001204 (*decoder)->configure(format);
Andreas Huberf9334412010-12-15 15:17:42 -08001205
Lajos Molnar09524832014-07-17 14:29:51 -07001206 // allocate buffers to decrypt widevine source buffers
1207 if (!audio && (mSourceFlags & Source::FLAG_SECURE)) {
1208 Vector<sp<ABuffer> > inputBufs;
1209 CHECK_EQ((*decoder)->getInputBuffers(&inputBufs), (status_t)OK);
1210
1211 Vector<MediaBuffer *> mediaBufs;
1212 for (size_t i = 0; i < inputBufs.size(); i++) {
1213 const sp<ABuffer> &buffer = inputBufs[i];
1214 MediaBuffer *mbuf = new MediaBuffer(buffer->data(), buffer->size());
1215 mediaBufs.push(mbuf);
1216 }
1217
1218 status_t err = mSource->setBuffers(audio, mediaBufs);
1219 if (err != OK) {
1220 for (size_t i = 0; i < mediaBufs.size(); ++i) {
1221 mediaBufs[i]->release();
1222 }
1223 mediaBufs.clear();
1224 ALOGE("Secure source didn't support secure mediaBufs.");
1225 return err;
1226 }
1227 }
Andreas Huberf9334412010-12-15 15:17:42 -08001228 return OK;
1229}
1230
1231status_t NuPlayer::feedDecoderInputData(bool audio, const sp<AMessage> &msg) {
1232 sp<AMessage> reply;
1233 CHECK(msg->findMessage("reply", &reply));
1234
Wei Jia53904f32014-07-29 10:22:53 -07001235 if ((audio && mFlushingAudio != NONE)
Wei Jiaf702d042014-09-09 12:08:47 -07001236 || (!audio && mFlushingVideo != NONE)
1237 || mSource == NULL) {
Wei Jiab189a5b2014-08-07 06:11:39 +00001238 reply->setInt32("err", INFO_DISCONTINUITY);
1239 reply->post();
1240 return OK;
Andreas Huberf9334412010-12-15 15:17:42 -08001241 }
1242
1243 sp<ABuffer> accessUnit;
Andreas Huberf9334412010-12-15 15:17:42 -08001244
Phil Burk9f526492014-09-03 15:04:12 -07001245 // Aggregate smaller buffers into a larger buffer.
1246 // The goal is to reduce power consumption.
Phil Burk33b51b02014-09-17 16:03:47 -07001247 // Note this will not work if the decoder requires one frame per buffer.
1248 bool doBufferAggregation = (audio && mOffloadAudio);
Phil Burk9f526492014-09-03 15:04:12 -07001249 bool needMoreData = false;
Phil Burk9f526492014-09-03 15:04:12 -07001250
Andreas Huber3fe62152011-09-16 15:09:22 -07001251 bool dropAccessUnit;
1252 do {
Phil Burk9f526492014-09-03 15:04:12 -07001253 status_t err;
1254 // Did we save an accessUnit earlier because of a discontinuity?
1255 if (audio && (mPendingAudioAccessUnit != NULL)) {
1256 accessUnit = mPendingAudioAccessUnit;
1257 mPendingAudioAccessUnit.clear();
1258 err = mPendingAudioErr;
1259 ALOGV("feedDecoderInputData() use mPendingAudioAccessUnit");
1260 } else {
1261 err = mSource->dequeueAccessUnit(audio, &accessUnit);
1262 }
Andreas Huber5bc087c2010-12-23 10:27:40 -08001263
Andreas Huber3fe62152011-09-16 15:09:22 -07001264 if (err == -EWOULDBLOCK) {
Phil Burkc5cc2e22014-09-09 20:08:39 -07001265 return err;
Andreas Huber3fe62152011-09-16 15:09:22 -07001266 } else if (err != OK) {
1267 if (err == INFO_DISCONTINUITY) {
Phil Burk33b51b02014-09-17 16:03:47 -07001268 if (doBufferAggregation && (mAggregateBuffer != NULL)) {
Phil Burk9f526492014-09-03 15:04:12 -07001269 // We already have some data so save this for later.
1270 mPendingAudioErr = err;
1271 mPendingAudioAccessUnit = accessUnit;
1272 accessUnit.clear();
1273 ALOGD("feedDecoderInputData() save discontinuity for later");
1274 break;
1275 }
Andreas Huber3fe62152011-09-16 15:09:22 -07001276 int32_t type;
1277 CHECK(accessUnit->meta()->findInt32("discontinuity", &type));
Andreas Huber53df1a42010-12-22 10:03:04 -08001278
Andreas Huber3fe62152011-09-16 15:09:22 -07001279 bool formatChange =
Andreas Huber6e3d3112011-11-28 12:36:11 -08001280 (audio &&
1281 (type & ATSParser::DISCONTINUITY_AUDIO_FORMAT))
1282 || (!audio &&
1283 (type & ATSParser::DISCONTINUITY_VIDEO_FORMAT));
Andreas Huber53df1a42010-12-22 10:03:04 -08001284
Andreas Huber6e3d3112011-11-28 12:36:11 -08001285 bool timeChange = (type & ATSParser::DISCONTINUITY_TIME) != 0;
1286
Steve Blockdf64d152012-01-04 20:05:49 +00001287 ALOGI("%s discontinuity (formatChange=%d, time=%d)",
Andreas Huber6e3d3112011-11-28 12:36:11 -08001288 audio ? "audio" : "video", formatChange, timeChange);
Andreas Huber32f3cef2011-03-02 15:34:46 -08001289
Andreas Huber6e3d3112011-11-28 12:36:11 -08001290 mTimeDiscontinuityPending =
1291 mTimeDiscontinuityPending || timeChange;
1292
Lajos Molnar87603c02014-08-20 19:25:30 -07001293 bool seamlessFormatChange = false;
1294 sp<AMessage> newFormat = mSource->getFormat(audio);
1295 if (formatChange) {
1296 seamlessFormatChange =
1297 getDecoder(audio)->supportsSeamlessFormatChange(newFormat);
1298 // treat seamless format change separately
1299 formatChange = !seamlessFormatChange;
1300 }
1301 bool shutdownOrFlush = formatChange || timeChange;
1302
1303 // We want to queue up scan-sources only once per discontinuity.
1304 // We control this by doing it only if neither audio nor video are
1305 // flushing or shutting down. (After handling 1st discontinuity, one
1306 // of the flushing states will not be NONE.)
1307 // No need to scan sources if this discontinuity does not result
1308 // in a flush or shutdown, as the flushing state will stay NONE.
1309 if (mFlushingAudio == NONE && mFlushingVideo == NONE &&
1310 shutdownOrFlush) {
Robert Shiha2981012014-07-30 17:41:24 -07001311 // And we'll resume scanning sources once we're done
1312 // flushing.
1313 mDeferredActions.push_front(
1314 new SimpleAction(
1315 &NuPlayer::performScanSources));
1316 }
1317
Lajos Molnar87603c02014-08-20 19:25:30 -07001318 if (formatChange /* not seamless */) {
1319 // must change decoder
1320 flushDecoder(audio, /* needShutdown = */ true);
1321 } else if (timeChange) {
1322 // need to flush
1323 flushDecoder(audio, /* needShutdown = */ false, newFormat);
1324 err = OK;
1325 } else if (seamlessFormatChange) {
1326 // reuse existing decoder and don't flush
1327 updateDecoderFormatWithoutFlush(audio, newFormat);
1328 err = OK;
Andreas Huber6e3d3112011-11-28 12:36:11 -08001329 } else {
1330 // This stream is unaffected by the discontinuity
Andreas Huber6e3d3112011-11-28 12:36:11 -08001331 return -EWOULDBLOCK;
1332 }
Andreas Huber32f3cef2011-03-02 15:34:46 -08001333 }
1334
Andreas Huber3fe62152011-09-16 15:09:22 -07001335 reply->setInt32("err", err);
1336 reply->post();
1337 return OK;
Andreas Huberf9334412010-12-15 15:17:42 -08001338 }
1339
Andreas Huber3fe62152011-09-16 15:09:22 -07001340 if (!audio) {
1341 ++mNumFramesTotal;
1342 }
1343
1344 dropAccessUnit = false;
1345 if (!audio
Lajos Molnar09524832014-07-17 14:29:51 -07001346 && !(mSourceFlags & Source::FLAG_SECURE)
Ronghua Wua73d9e02014-10-08 15:13:29 -07001347 && mRenderer->getVideoLateByUs() > 100000ll
Andreas Huber3fe62152011-09-16 15:09:22 -07001348 && mVideoIsAVC
1349 && !IsAVCReferenceFrame(accessUnit)) {
1350 dropAccessUnit = true;
1351 ++mNumFramesDropped;
1352 }
Phil Burk9f526492014-09-03 15:04:12 -07001353
1354 size_t smallSize = accessUnit->size();
1355 needMoreData = false;
Phil Burkc5cc2e22014-09-09 20:08:39 -07001356 if (doBufferAggregation && (mAggregateBuffer == NULL)
Phil Burk9f526492014-09-03 15:04:12 -07001357 // Don't bother if only room for a few small buffers.
Phil Burkc5cc2e22014-09-09 20:08:39 -07001358 && (smallSize < (kAggregateBufferSizeBytes / 3))) {
Phil Burk9f526492014-09-03 15:04:12 -07001359 // Create a larger buffer for combining smaller buffers from the extractor.
Phil Burkc5cc2e22014-09-09 20:08:39 -07001360 mAggregateBuffer = new ABuffer(kAggregateBufferSizeBytes);
1361 mAggregateBuffer->setRange(0, 0); // start empty
Phil Burk9f526492014-09-03 15:04:12 -07001362 }
1363
Phil Burk33b51b02014-09-17 16:03:47 -07001364 if (doBufferAggregation && (mAggregateBuffer != NULL)) {
Phil Burk9f526492014-09-03 15:04:12 -07001365 int64_t timeUs;
Phil Burkc5cc2e22014-09-09 20:08:39 -07001366 int64_t dummy;
Phil Burk9f526492014-09-03 15:04:12 -07001367 bool smallTimestampValid = accessUnit->meta()->findInt64("timeUs", &timeUs);
Phil Burkc5cc2e22014-09-09 20:08:39 -07001368 bool bigTimestampValid = mAggregateBuffer->meta()->findInt64("timeUs", &dummy);
Phil Burk9f526492014-09-03 15:04:12 -07001369 // Will the smaller buffer fit?
Phil Burkc5cc2e22014-09-09 20:08:39 -07001370 size_t bigSize = mAggregateBuffer->size();
1371 size_t roomLeft = mAggregateBuffer->capacity() - bigSize;
Phil Burk9f526492014-09-03 15:04:12 -07001372 // Should we save this small buffer for the next big buffer?
1373 // If the first small buffer did not have a timestamp then save
1374 // any buffer that does have a timestamp until the next big buffer.
1375 if ((smallSize > roomLeft)
Phil Burkc5cc2e22014-09-09 20:08:39 -07001376 || (!bigTimestampValid && (bigSize > 0) && smallTimestampValid)) {
Phil Burk9f526492014-09-03 15:04:12 -07001377 mPendingAudioErr = err;
1378 mPendingAudioAccessUnit = accessUnit;
1379 accessUnit.clear();
1380 } else {
Phil Burkc5cc2e22014-09-09 20:08:39 -07001381 // Grab time from first small buffer if available.
1382 if ((bigSize == 0) && smallTimestampValid) {
1383 mAggregateBuffer->meta()->setInt64("timeUs", timeUs);
1384 }
Phil Burk9f526492014-09-03 15:04:12 -07001385 // Append small buffer to the bigger buffer.
Phil Burkc5cc2e22014-09-09 20:08:39 -07001386 memcpy(mAggregateBuffer->base() + bigSize, accessUnit->data(), smallSize);
Phil Burk9f526492014-09-03 15:04:12 -07001387 bigSize += smallSize;
Phil Burkc5cc2e22014-09-09 20:08:39 -07001388 mAggregateBuffer->setRange(0, bigSize);
Phil Burk9f526492014-09-03 15:04:12 -07001389
Phil Burkc5cc2e22014-09-09 20:08:39 -07001390 // Keep looping until we run out of room in the mAggregateBuffer.
Phil Burk9f526492014-09-03 15:04:12 -07001391 needMoreData = true;
1392
Phil Burkc5cc2e22014-09-09 20:08:39 -07001393 ALOGV("feedDecoderInputData() smallSize = %zu, bigSize = %zu, capacity = %zu",
1394 smallSize, bigSize, mAggregateBuffer->capacity());
Phil Burk9f526492014-09-03 15:04:12 -07001395 }
1396 }
1397 } while (dropAccessUnit || needMoreData);
Andreas Huberf9334412010-12-15 15:17:42 -08001398
Steve Block3856b092011-10-20 11:56:00 +01001399 // ALOGV("returned a valid buffer of %s data", audio ? "audio" : "video");
Andreas Huberf9334412010-12-15 15:17:42 -08001400
1401#if 0
1402 int64_t mediaTimeUs;
1403 CHECK(accessUnit->meta()->findInt64("timeUs", &mediaTimeUs));
Steve Block3856b092011-10-20 11:56:00 +01001404 ALOGV("feeding %s input buffer at media time %.2f secs",
Andreas Huberf9334412010-12-15 15:17:42 -08001405 audio ? "audio" : "video",
1406 mediaTimeUs / 1E6);
1407#endif
1408
Chong Zhanga7fa1d92014-06-11 14:49:23 -07001409 if (!audio) {
1410 mCCDecoder->decode(accessUnit);
1411 }
1412
Phil Burk33b51b02014-09-17 16:03:47 -07001413 if (doBufferAggregation && (mAggregateBuffer != NULL)) {
Phil Burkc5cc2e22014-09-09 20:08:39 -07001414 ALOGV("feedDecoderInputData() reply with aggregated buffer, %zu",
1415 mAggregateBuffer->size());
1416 reply->setBuffer("buffer", mAggregateBuffer);
1417 mAggregateBuffer.clear();
Phil Burk9f526492014-09-03 15:04:12 -07001418 } else {
1419 reply->setBuffer("buffer", accessUnit);
1420 }
1421
Andreas Huberf9334412010-12-15 15:17:42 -08001422 reply->post();
1423
1424 return OK;
1425}
1426
1427void NuPlayer::renderBuffer(bool audio, const sp<AMessage> &msg) {
Steve Block3856b092011-10-20 11:56:00 +01001428 // ALOGV("renderBuffer %s", audio ? "audio" : "video");
Andreas Huberf9334412010-12-15 15:17:42 -08001429
Wei Jia53904f32014-07-29 10:22:53 -07001430 if ((audio && mFlushingAudio != NONE)
1431 || (!audio && mFlushingVideo != NONE)) {
Andreas Huber18ac5402011-08-31 15:04:25 -07001432 // We're currently attempting to flush the decoder, in order
1433 // to complete this, the decoder wants all its buffers back,
1434 // so we don't want any output buffers it sent us (from before
1435 // we initiated the flush) to be stuck in the renderer's queue.
1436
Steve Block3856b092011-10-20 11:56:00 +01001437 ALOGV("we're still flushing the %s decoder, sending its output buffer"
Andreas Huber18ac5402011-08-31 15:04:25 -07001438 " right back.", audio ? "audio" : "video");
1439
Andreas Huber18ac5402011-08-31 15:04:25 -07001440 return;
1441 }
1442
Chong Zhanga7fa1d92014-06-11 14:49:23 -07001443 int64_t mediaTimeUs;
Wei Jiac6cfd702014-11-11 16:33:20 -08001444 CHECK(msg->findInt64("timeUs", &mediaTimeUs));
Andreas Huber32f3cef2011-03-02 15:34:46 -08001445
Chong Zhanga7fa1d92014-06-11 14:49:23 -07001446 if (!audio && mCCDecoder->isSelected()) {
1447 mCCDecoder->display(mediaTimeUs);
1448 }
Andreas Huberf9334412010-12-15 15:17:42 -08001449}
1450
Chong Zhangced1c2f2014-08-08 15:22:35 -07001451void NuPlayer::updateVideoSize(
1452 const sp<AMessage> &inputFormat,
1453 const sp<AMessage> &outputFormat) {
1454 if (inputFormat == NULL) {
1455 ALOGW("Unknown video size, reporting 0x0!");
1456 notifyListener(MEDIA_SET_VIDEO_SIZE, 0, 0);
1457 return;
1458 }
1459
1460 int32_t displayWidth, displayHeight;
1461 int32_t cropLeft, cropTop, cropRight, cropBottom;
1462
1463 if (outputFormat != NULL) {
1464 int32_t width, height;
1465 CHECK(outputFormat->findInt32("width", &width));
1466 CHECK(outputFormat->findInt32("height", &height));
1467
1468 int32_t cropLeft, cropTop, cropRight, cropBottom;
1469 CHECK(outputFormat->findRect(
1470 "crop",
1471 &cropLeft, &cropTop, &cropRight, &cropBottom));
1472
1473 displayWidth = cropRight - cropLeft + 1;
1474 displayHeight = cropBottom - cropTop + 1;
1475
1476 ALOGV("Video output format changed to %d x %d "
1477 "(crop: %d x %d @ (%d, %d))",
1478 width, height,
1479 displayWidth,
1480 displayHeight,
1481 cropLeft, cropTop);
1482 } else {
1483 CHECK(inputFormat->findInt32("width", &displayWidth));
1484 CHECK(inputFormat->findInt32("height", &displayHeight));
1485
1486 ALOGV("Video input format %d x %d", displayWidth, displayHeight);
1487 }
1488
1489 // Take into account sample aspect ratio if necessary:
1490 int32_t sarWidth, sarHeight;
1491 if (inputFormat->findInt32("sar-width", &sarWidth)
1492 && inputFormat->findInt32("sar-height", &sarHeight)) {
1493 ALOGV("Sample aspect ratio %d : %d", sarWidth, sarHeight);
1494
1495 displayWidth = (displayWidth * sarWidth) / sarHeight;
1496
1497 ALOGV("display dimensions %d x %d", displayWidth, displayHeight);
1498 }
1499
1500 int32_t rotationDegrees;
1501 if (!inputFormat->findInt32("rotation-degrees", &rotationDegrees)) {
1502 rotationDegrees = 0;
1503 }
1504
1505 if (rotationDegrees == 90 || rotationDegrees == 270) {
1506 int32_t tmp = displayWidth;
1507 displayWidth = displayHeight;
1508 displayHeight = tmp;
1509 }
1510
1511 notifyListener(
1512 MEDIA_SET_VIDEO_SIZE,
1513 displayWidth,
1514 displayHeight);
1515}
1516
Chong Zhangdcb89b32013-08-06 09:44:47 -07001517void NuPlayer::notifyListener(int msg, int ext1, int ext2, const Parcel *in) {
Andreas Huber43c3e6c2011-01-05 12:17:08 -08001518 if (mDriver == NULL) {
Andreas Huberf9334412010-12-15 15:17:42 -08001519 return;
1520 }
1521
Andreas Huber43c3e6c2011-01-05 12:17:08 -08001522 sp<NuPlayerDriver> driver = mDriver.promote();
Andreas Huberf9334412010-12-15 15:17:42 -08001523
Andreas Huber43c3e6c2011-01-05 12:17:08 -08001524 if (driver == NULL) {
Andreas Huberf9334412010-12-15 15:17:42 -08001525 return;
1526 }
1527
Chong Zhangdcb89b32013-08-06 09:44:47 -07001528 driver->notifyListener(msg, ext1, ext2, in);
Andreas Huberf9334412010-12-15 15:17:42 -08001529}
1530
Lajos Molnar87603c02014-08-20 19:25:30 -07001531void NuPlayer::flushDecoder(
1532 bool audio, bool needShutdown, const sp<AMessage> &newFormat) {
Andreas Huber14f76722013-01-15 09:04:18 -08001533 ALOGV("[%s] flushDecoder needShutdown=%d",
1534 audio ? "audio" : "video", needShutdown);
1535
Lajos Molnar87603c02014-08-20 19:25:30 -07001536 const sp<Decoder> &decoder = getDecoder(audio);
1537 if (decoder == NULL) {
Steve Blockdf64d152012-01-04 20:05:49 +00001538 ALOGI("flushDecoder %s without decoder present",
Andreas Huber6e3d3112011-11-28 12:36:11 -08001539 audio ? "audio" : "video");
Lajos Molnar87603c02014-08-20 19:25:30 -07001540 return;
Andreas Huber6e3d3112011-11-28 12:36:11 -08001541 }
1542
Andreas Huber1aef2112011-01-04 14:01:29 -08001543 // Make sure we don't continue to scan sources until we finish flushing.
1544 ++mScanSourcesGeneration;
Andreas Huber43c3e6c2011-01-05 12:17:08 -08001545 mScanSourcesPending = false;
Andreas Huber1aef2112011-01-04 14:01:29 -08001546
Lajos Molnar87603c02014-08-20 19:25:30 -07001547 decoder->signalFlush(newFormat);
Andreas Huber1aef2112011-01-04 14:01:29 -08001548
1549 FlushStatus newStatus =
1550 needShutdown ? FLUSHING_DECODER_SHUTDOWN : FLUSHING_DECODER;
1551
Andy Hung8d121d42014-10-03 09:53:53 -07001552 mFlushComplete[audio][false /* isDecoder */] = false;
1553 mFlushComplete[audio][true /* isDecoder */] = false;
Andreas Huber1aef2112011-01-04 14:01:29 -08001554 if (audio) {
Wei Jia53904f32014-07-29 10:22:53 -07001555 ALOGE_IF(mFlushingAudio != NONE,
1556 "audio flushDecoder() is called in state %d", mFlushingAudio);
Andreas Huber1aef2112011-01-04 14:01:29 -08001557 mFlushingAudio = newStatus;
Andreas Huber1aef2112011-01-04 14:01:29 -08001558 } else {
Wei Jia53904f32014-07-29 10:22:53 -07001559 ALOGE_IF(mFlushingVideo != NONE,
1560 "video flushDecoder() is called in state %d", mFlushingVideo);
Andreas Huber1aef2112011-01-04 14:01:29 -08001561 mFlushingVideo = newStatus;
Chong Zhangb86e68f2014-08-01 13:46:53 -07001562
1563 if (mCCDecoder != NULL) {
1564 mCCDecoder->flush();
1565 }
Andreas Huber1aef2112011-01-04 14:01:29 -08001566 }
1567}
1568
Lajos Molnar87603c02014-08-20 19:25:30 -07001569void NuPlayer::updateDecoderFormatWithoutFlush(
1570 bool audio, const sp<AMessage> &format) {
1571 ALOGV("[%s] updateDecoderFormatWithoutFlush", audio ? "audio" : "video");
1572
1573 const sp<Decoder> &decoder = getDecoder(audio);
1574 if (decoder == NULL) {
1575 ALOGI("updateDecoderFormatWithoutFlush %s without decoder present",
1576 audio ? "audio" : "video");
1577 return;
1578 }
1579
1580 decoder->signalUpdateFormat(format);
1581}
1582
Chong Zhangced1c2f2014-08-08 15:22:35 -07001583void NuPlayer::queueDecoderShutdown(
1584 bool audio, bool video, const sp<AMessage> &reply) {
1585 ALOGI("queueDecoderShutdown audio=%d, video=%d", audio, video);
Andreas Huber84066782011-08-16 09:34:26 -07001586
Chong Zhangced1c2f2014-08-08 15:22:35 -07001587 mDeferredActions.push_back(
Wei Jiafef808d2014-10-31 17:57:05 -07001588 new FlushDecoderAction(
1589 audio ? FLUSH_CMD_SHUTDOWN : FLUSH_CMD_NONE,
1590 video ? FLUSH_CMD_SHUTDOWN : FLUSH_CMD_NONE));
Andreas Huber84066782011-08-16 09:34:26 -07001591
Chong Zhangced1c2f2014-08-08 15:22:35 -07001592 mDeferredActions.push_back(
1593 new SimpleAction(&NuPlayer::performScanSources));
Andreas Huber84066782011-08-16 09:34:26 -07001594
Chong Zhangced1c2f2014-08-08 15:22:35 -07001595 mDeferredActions.push_back(new PostMessageAction(reply));
1596
1597 processDeferredActions();
Andreas Huber84066782011-08-16 09:34:26 -07001598}
1599
James Dong0d268a32012-08-31 12:18:27 -07001600status_t NuPlayer::setVideoScalingMode(int32_t mode) {
1601 mVideoScalingMode = mode;
Andreas Huber57a339c2012-12-03 11:18:00 -08001602 if (mNativeWindow != NULL) {
James Dong0d268a32012-08-31 12:18:27 -07001603 status_t ret = native_window_set_scaling_mode(
1604 mNativeWindow->getNativeWindow().get(), mVideoScalingMode);
1605 if (ret != OK) {
1606 ALOGE("Failed to set scaling mode (%d): %s",
1607 -ret, strerror(-ret));
1608 return ret;
1609 }
1610 }
1611 return OK;
1612}
1613
Chong Zhangdcb89b32013-08-06 09:44:47 -07001614status_t NuPlayer::getTrackInfo(Parcel* reply) const {
1615 sp<AMessage> msg = new AMessage(kWhatGetTrackInfo, id());
1616 msg->setPointer("reply", reply);
1617
1618 sp<AMessage> response;
1619 status_t err = msg->postAndAwaitResponse(&response);
1620 return err;
1621}
1622
Robert Shih7c4f0d72014-07-09 18:53:31 -07001623status_t NuPlayer::getSelectedTrack(int32_t type, Parcel* reply) const {
1624 sp<AMessage> msg = new AMessage(kWhatGetSelectedTrack, id());
1625 msg->setPointer("reply", reply);
1626 msg->setInt32("type", type);
1627
1628 sp<AMessage> response;
1629 status_t err = msg->postAndAwaitResponse(&response);
1630 if (err == OK && response != NULL) {
1631 CHECK(response->findInt32("err", &err));
1632 }
1633 return err;
1634}
1635
Chong Zhangdcb89b32013-08-06 09:44:47 -07001636status_t NuPlayer::selectTrack(size_t trackIndex, bool select) {
1637 sp<AMessage> msg = new AMessage(kWhatSelectTrack, id());
1638 msg->setSize("trackIndex", trackIndex);
1639 msg->setInt32("select", select);
1640
1641 sp<AMessage> response;
1642 status_t err = msg->postAndAwaitResponse(&response);
1643
Chong Zhang404fced2014-06-11 14:45:31 -07001644 if (err != OK) {
1645 return err;
1646 }
1647
1648 if (!response->findInt32("err", &err)) {
1649 err = OK;
1650 }
1651
Chong Zhangdcb89b32013-08-06 09:44:47 -07001652 return err;
1653}
1654
Ronghua Wua73d9e02014-10-08 15:13:29 -07001655status_t NuPlayer::getCurrentPosition(int64_t *mediaUs) {
1656 sp<Renderer> renderer = mRenderer;
1657 if (renderer == NULL) {
1658 return NO_INIT;
1659 }
1660
1661 return renderer->getCurrentPosition(mediaUs);
1662}
1663
1664void NuPlayer::getStats(int64_t *numFramesTotal, int64_t *numFramesDropped) {
1665 *numFramesTotal = mNumFramesTotal;
1666 *numFramesDropped = mNumFramesDropped;
1667}
1668
Marco Nelissenf0b72b52014-09-16 15:43:44 -07001669sp<MetaData> NuPlayer::getFileMeta() {
1670 return mSource->getFileFormatMeta();
1671}
1672
Andreas Huberb7c8e912012-11-27 15:02:53 -08001673void NuPlayer::schedulePollDuration() {
1674 sp<AMessage> msg = new AMessage(kWhatPollDuration, id());
1675 msg->setInt32("generation", mPollDurationGeneration);
1676 msg->post();
1677}
1678
1679void NuPlayer::cancelPollDuration() {
1680 ++mPollDurationGeneration;
1681}
1682
Andreas Hubera1f8ab02012-11-30 10:53:22 -08001683void NuPlayer::processDeferredActions() {
1684 while (!mDeferredActions.empty()) {
1685 // We won't execute any deferred actions until we're no longer in
1686 // an intermediate state, i.e. one more more decoders are currently
1687 // flushing or shutting down.
1688
Andreas Hubera1f8ab02012-11-30 10:53:22 -08001689 if (mFlushingAudio != NONE || mFlushingVideo != NONE) {
1690 // We're currently flushing, postpone the reset until that's
1691 // completed.
1692
1693 ALOGV("postponing action mFlushingAudio=%d, mFlushingVideo=%d",
1694 mFlushingAudio, mFlushingVideo);
1695
1696 break;
1697 }
1698
1699 sp<Action> action = *mDeferredActions.begin();
1700 mDeferredActions.erase(mDeferredActions.begin());
1701
1702 action->execute(this);
1703 }
1704}
1705
Wei Jiae427abf2014-09-22 15:21:11 -07001706void NuPlayer::performSeek(int64_t seekTimeUs, bool needNotify) {
1707 ALOGV("performSeek seekTimeUs=%lld us (%.2f secs), needNotify(%d)",
Andreas Hubera1f8ab02012-11-30 10:53:22 -08001708 seekTimeUs,
Wei Jiae427abf2014-09-22 15:21:11 -07001709 seekTimeUs / 1E6,
1710 needNotify);
Andreas Hubera1f8ab02012-11-30 10:53:22 -08001711
Andy Hungadf34bf2014-09-03 18:22:22 -07001712 if (mSource == NULL) {
1713 // This happens when reset occurs right before the loop mode
1714 // asynchronously seeks to the start of the stream.
1715 LOG_ALWAYS_FATAL_IF(mAudioDecoder != NULL || mVideoDecoder != NULL,
1716 "mSource is NULL and decoders not NULL audio(%p) video(%p)",
1717 mAudioDecoder.get(), mVideoDecoder.get());
1718 return;
1719 }
Andreas Hubera1f8ab02012-11-30 10:53:22 -08001720 mSource->seekTo(seekTimeUs);
Robert Shihd3b0bbb2014-07-23 15:00:25 -07001721 ++mTimedTextGeneration;
Andreas Hubera1f8ab02012-11-30 10:53:22 -08001722
1723 if (mDriver != NULL) {
1724 sp<NuPlayerDriver> driver = mDriver.promote();
1725 if (driver != NULL) {
Wei Jiae427abf2014-09-22 15:21:11 -07001726 if (needNotify) {
1727 driver->notifySeekComplete();
1728 }
Andreas Hubera1f8ab02012-11-30 10:53:22 -08001729 }
1730 }
1731
1732 // everything's flushed, continue playback.
1733}
1734
Wei Jiafef808d2014-10-31 17:57:05 -07001735void NuPlayer::performDecoderFlush(FlushCommand audio, FlushCommand video) {
1736 ALOGV("performDecoderFlush audio=%d, video=%d", audio, video);
Andreas Hubera1f8ab02012-11-30 10:53:22 -08001737
Wei Jiafef808d2014-10-31 17:57:05 -07001738 if ((audio == FLUSH_CMD_NONE || mAudioDecoder == NULL)
1739 && (video == FLUSH_CMD_NONE || mVideoDecoder == NULL)) {
Andreas Hubera1f8ab02012-11-30 10:53:22 -08001740 return;
1741 }
1742
1743 mTimeDiscontinuityPending = true;
1744
Wei Jiafef808d2014-10-31 17:57:05 -07001745 if (audio != FLUSH_CMD_NONE && mAudioDecoder != NULL) {
1746 flushDecoder(true /* audio */, (audio == FLUSH_CMD_SHUTDOWN));
Andreas Hubera1f8ab02012-11-30 10:53:22 -08001747 }
1748
Wei Jiafef808d2014-10-31 17:57:05 -07001749 if (video != FLUSH_CMD_NONE && mVideoDecoder != NULL) {
1750 flushDecoder(false /* audio */, (video == FLUSH_CMD_SHUTDOWN));
Andreas Hubera1f8ab02012-11-30 10:53:22 -08001751 }
1752}
1753
1754void NuPlayer::performReset() {
1755 ALOGV("performReset");
1756
1757 CHECK(mAudioDecoder == NULL);
1758 CHECK(mVideoDecoder == NULL);
1759
1760 cancelPollDuration();
1761
1762 ++mScanSourcesGeneration;
1763 mScanSourcesPending = false;
1764
Lajos Molnar09524832014-07-17 14:29:51 -07001765 if (mRendererLooper != NULL) {
1766 if (mRenderer != NULL) {
1767 mRendererLooper->unregisterHandler(mRenderer->id());
1768 }
1769 mRendererLooper->stop();
1770 mRendererLooper.clear();
1771 }
Andreas Hubera1f8ab02012-11-30 10:53:22 -08001772 mRenderer.clear();
Wei Jia57568df2014-09-22 10:16:29 -07001773 ++mRendererGeneration;
Andreas Hubera1f8ab02012-11-30 10:53:22 -08001774
1775 if (mSource != NULL) {
1776 mSource->stop();
Andreas Huberb5f25f02013-02-05 10:14:26 -08001777
Andreas Hubera1f8ab02012-11-30 10:53:22 -08001778 mSource.clear();
1779 }
1780
1781 if (mDriver != NULL) {
1782 sp<NuPlayerDriver> driver = mDriver.promote();
1783 if (driver != NULL) {
1784 driver->notifyResetComplete();
1785 }
1786 }
Andreas Huber57a339c2012-12-03 11:18:00 -08001787
1788 mStarted = false;
Andreas Hubera1f8ab02012-11-30 10:53:22 -08001789}
1790
1791void NuPlayer::performScanSources() {
1792 ALOGV("performScanSources");
1793
Andreas Huber57a339c2012-12-03 11:18:00 -08001794 if (!mStarted) {
1795 return;
1796 }
1797
Andreas Hubera1f8ab02012-11-30 10:53:22 -08001798 if (mAudioDecoder == NULL || mVideoDecoder == NULL) {
1799 postScanSources();
1800 }
1801}
1802
Andreas Huber57a339c2012-12-03 11:18:00 -08001803void NuPlayer::performSetSurface(const sp<NativeWindowWrapper> &wrapper) {
1804 ALOGV("performSetSurface");
1805
1806 mNativeWindow = wrapper;
1807
1808 // XXX - ignore error from setVideoScalingMode for now
1809 setVideoScalingMode(mVideoScalingMode);
Chong Zhang13d6faa2014-08-22 15:35:28 -07001810
1811 if (mDriver != NULL) {
1812 sp<NuPlayerDriver> driver = mDriver.promote();
1813 if (driver != NULL) {
1814 driver->notifySetSurfaceComplete();
1815 }
1816 }
Andreas Huber57a339c2012-12-03 11:18:00 -08001817}
1818
Andreas Huber9575c962013-02-05 13:59:56 -08001819void NuPlayer::onSourceNotify(const sp<AMessage> &msg) {
1820 int32_t what;
1821 CHECK(msg->findInt32("what", &what));
1822
1823 switch (what) {
1824 case Source::kWhatPrepared:
1825 {
Andreas Huberb5f28d42013-04-25 15:11:19 -07001826 if (mSource == NULL) {
1827 // This is a stale notification from a source that was
1828 // asynchronously preparing when the client called reset().
1829 // We handled the reset, the source is gone.
1830 break;
1831 }
1832
Andreas Huberec0c5972013-02-05 14:47:13 -08001833 int32_t err;
1834 CHECK(msg->findInt32("err", &err));
1835
Andreas Huber9575c962013-02-05 13:59:56 -08001836 sp<NuPlayerDriver> driver = mDriver.promote();
1837 if (driver != NULL) {
Marco Nelissendd114d12014-05-28 15:23:14 -07001838 // notify duration first, so that it's definitely set when
1839 // the app received the "prepare complete" callback.
1840 int64_t durationUs;
1841 if (mSource->getDuration(&durationUs) == OK) {
1842 driver->notifyDuration(durationUs);
1843 }
Andreas Huberec0c5972013-02-05 14:47:13 -08001844 driver->notifyPrepareCompleted(err);
Andreas Huber9575c962013-02-05 13:59:56 -08001845 }
Andreas Huber99759402013-04-01 14:28:31 -07001846
Andreas Huber9575c962013-02-05 13:59:56 -08001847 break;
1848 }
1849
1850 case Source::kWhatFlagsChanged:
1851 {
1852 uint32_t flags;
1853 CHECK(msg->findInt32("flags", (int32_t *)&flags));
1854
Chong Zhang4b7069d2013-09-11 12:52:43 -07001855 sp<NuPlayerDriver> driver = mDriver.promote();
1856 if (driver != NULL) {
1857 driver->notifyFlagsChanged(flags);
1858 }
1859
Andreas Huber9575c962013-02-05 13:59:56 -08001860 if ((mSourceFlags & Source::FLAG_DYNAMIC_DURATION)
1861 && (!(flags & Source::FLAG_DYNAMIC_DURATION))) {
1862 cancelPollDuration();
1863 } else if (!(mSourceFlags & Source::FLAG_DYNAMIC_DURATION)
1864 && (flags & Source::FLAG_DYNAMIC_DURATION)
1865 && (mAudioDecoder != NULL || mVideoDecoder != NULL)) {
1866 schedulePollDuration();
1867 }
1868
1869 mSourceFlags = flags;
1870 break;
1871 }
1872
1873 case Source::kWhatVideoSizeChanged:
1874 {
Chong Zhangced1c2f2014-08-08 15:22:35 -07001875 sp<AMessage> format;
1876 CHECK(msg->findMessage("format", &format));
Andreas Huber9575c962013-02-05 13:59:56 -08001877
Chong Zhangced1c2f2014-08-08 15:22:35 -07001878 updateVideoSize(format);
Andreas Huber9575c962013-02-05 13:59:56 -08001879 break;
1880 }
1881
Chong Zhang2a3cc9a2014-08-21 17:48:26 -07001882 case Source::kWhatBufferingUpdate:
1883 {
1884 int32_t percentage;
1885 CHECK(msg->findInt32("percentage", &percentage));
1886
1887 notifyListener(MEDIA_BUFFERING_UPDATE, percentage, 0);
1888 break;
1889 }
1890
Roger Jönssonb50e83e2013-01-21 16:26:41 +01001891 case Source::kWhatBufferingStart:
1892 {
1893 notifyListener(MEDIA_INFO, MEDIA_INFO_BUFFERING_START, 0);
1894 break;
1895 }
1896
1897 case Source::kWhatBufferingEnd:
1898 {
1899 notifyListener(MEDIA_INFO, MEDIA_INFO_BUFFERING_END, 0);
1900 break;
1901 }
1902
Chong Zhangdcb89b32013-08-06 09:44:47 -07001903 case Source::kWhatSubtitleData:
1904 {
1905 sp<ABuffer> buffer;
1906 CHECK(msg->findBuffer("buffer", &buffer));
1907
Chong Zhang404fced2014-06-11 14:45:31 -07001908 sendSubtitleData(buffer, 0 /* baseIndex */);
Chong Zhangdcb89b32013-08-06 09:44:47 -07001909 break;
1910 }
1911
Robert Shihd3b0bbb2014-07-23 15:00:25 -07001912 case Source::kWhatTimedTextData:
1913 {
1914 int32_t generation;
1915 if (msg->findInt32("generation", &generation)
1916 && generation != mTimedTextGeneration) {
1917 break;
1918 }
1919
1920 sp<ABuffer> buffer;
1921 CHECK(msg->findBuffer("buffer", &buffer));
1922
1923 sp<NuPlayerDriver> driver = mDriver.promote();
1924 if (driver == NULL) {
1925 break;
1926 }
1927
1928 int posMs;
1929 int64_t timeUs, posUs;
1930 driver->getCurrentPosition(&posMs);
1931 posUs = posMs * 1000;
1932 CHECK(buffer->meta()->findInt64("timeUs", &timeUs));
1933
1934 if (posUs < timeUs) {
1935 if (!msg->findInt32("generation", &generation)) {
1936 msg->setInt32("generation", mTimedTextGeneration);
1937 }
1938 msg->post(timeUs - posUs);
1939 } else {
1940 sendTimedTextData(buffer);
1941 }
1942 break;
1943 }
1944
Andreas Huber14f76722013-01-15 09:04:18 -08001945 case Source::kWhatQueueDecoderShutdown:
1946 {
1947 int32_t audio, video;
1948 CHECK(msg->findInt32("audio", &audio));
1949 CHECK(msg->findInt32("video", &video));
1950
1951 sp<AMessage> reply;
1952 CHECK(msg->findMessage("reply", &reply));
1953
1954 queueDecoderShutdown(audio, video, reply);
1955 break;
1956 }
1957
Ronghua Wu80276872014-08-28 15:50:29 -07001958 case Source::kWhatDrmNoLicense:
1959 {
1960 notifyListener(MEDIA_ERROR, MEDIA_ERROR_UNKNOWN, ERROR_DRM_NO_LICENSE);
1961 break;
1962 }
1963
Andreas Huber9575c962013-02-05 13:59:56 -08001964 default:
1965 TRESPASS();
1966 }
1967}
1968
Chong Zhanga7fa1d92014-06-11 14:49:23 -07001969void NuPlayer::onClosedCaptionNotify(const sp<AMessage> &msg) {
1970 int32_t what;
1971 CHECK(msg->findInt32("what", &what));
1972
1973 switch (what) {
1974 case NuPlayer::CCDecoder::kWhatClosedCaptionData:
1975 {
1976 sp<ABuffer> buffer;
1977 CHECK(msg->findBuffer("buffer", &buffer));
1978
1979 size_t inbandTracks = 0;
1980 if (mSource != NULL) {
1981 inbandTracks = mSource->getTrackCount();
1982 }
1983
1984 sendSubtitleData(buffer, inbandTracks);
1985 break;
1986 }
1987
1988 case NuPlayer::CCDecoder::kWhatTrackAdded:
1989 {
1990 notifyListener(MEDIA_INFO, MEDIA_INFO_METADATA_UPDATE, 0);
1991
1992 break;
1993 }
1994
1995 default:
1996 TRESPASS();
1997 }
1998
1999
2000}
2001
Chong Zhang404fced2014-06-11 14:45:31 -07002002void NuPlayer::sendSubtitleData(const sp<ABuffer> &buffer, int32_t baseIndex) {
2003 int32_t trackIndex;
2004 int64_t timeUs, durationUs;
2005 CHECK(buffer->meta()->findInt32("trackIndex", &trackIndex));
2006 CHECK(buffer->meta()->findInt64("timeUs", &timeUs));
2007 CHECK(buffer->meta()->findInt64("durationUs", &durationUs));
2008
2009 Parcel in;
2010 in.writeInt32(trackIndex + baseIndex);
2011 in.writeInt64(timeUs);
2012 in.writeInt64(durationUs);
2013 in.writeInt32(buffer->size());
2014 in.writeInt32(buffer->size());
2015 in.write(buffer->data(), buffer->size());
2016
2017 notifyListener(MEDIA_SUBTITLE_DATA, 0, 0, &in);
2018}
Robert Shihd3b0bbb2014-07-23 15:00:25 -07002019
2020void NuPlayer::sendTimedTextData(const sp<ABuffer> &buffer) {
2021 const void *data;
2022 size_t size = 0;
2023 int64_t timeUs;
2024 int32_t flag = TextDescriptions::LOCAL_DESCRIPTIONS;
2025
2026 AString mime;
2027 CHECK(buffer->meta()->findString("mime", &mime));
2028 CHECK(strcasecmp(mime.c_str(), MEDIA_MIMETYPE_TEXT_3GPP) == 0);
2029
2030 data = buffer->data();
2031 size = buffer->size();
2032
2033 Parcel parcel;
2034 if (size > 0) {
2035 CHECK(buffer->meta()->findInt64("timeUs", &timeUs));
2036 flag |= TextDescriptions::IN_BAND_TEXT_3GPP;
2037 TextDescriptions::getParcelOfDescriptions(
2038 (const uint8_t *)data, size, flag, timeUs / 1000, &parcel);
2039 }
2040
2041 if ((parcel.dataSize() > 0)) {
2042 notifyListener(MEDIA_TIMED_TEXT, 0, 0, &parcel);
2043 } else { // send an empty timed text
2044 notifyListener(MEDIA_TIMED_TEXT, 0, 0);
2045 }
2046}
Andreas Huberb5f25f02013-02-05 10:14:26 -08002047////////////////////////////////////////////////////////////////////////////////
2048
Chong Zhangced1c2f2014-08-08 15:22:35 -07002049sp<AMessage> NuPlayer::Source::getFormat(bool audio) {
2050 sp<MetaData> meta = getFormatMeta(audio);
2051
2052 if (meta == NULL) {
2053 return NULL;
2054 }
2055
2056 sp<AMessage> msg = new AMessage;
2057
2058 if(convertMetaDataToMessage(meta, &msg) == OK) {
2059 return msg;
2060 }
2061 return NULL;
2062}
2063
Andreas Huber9575c962013-02-05 13:59:56 -08002064void NuPlayer::Source::notifyFlagsChanged(uint32_t flags) {
2065 sp<AMessage> notify = dupNotify();
2066 notify->setInt32("what", kWhatFlagsChanged);
2067 notify->setInt32("flags", flags);
2068 notify->post();
2069}
2070
Chong Zhangced1c2f2014-08-08 15:22:35 -07002071void NuPlayer::Source::notifyVideoSizeChanged(const sp<AMessage> &format) {
Andreas Huber9575c962013-02-05 13:59:56 -08002072 sp<AMessage> notify = dupNotify();
2073 notify->setInt32("what", kWhatVideoSizeChanged);
Chong Zhangced1c2f2014-08-08 15:22:35 -07002074 notify->setMessage("format", format);
Andreas Huber9575c962013-02-05 13:59:56 -08002075 notify->post();
2076}
2077
Andreas Huberec0c5972013-02-05 14:47:13 -08002078void NuPlayer::Source::notifyPrepared(status_t err) {
Andreas Huber9575c962013-02-05 13:59:56 -08002079 sp<AMessage> notify = dupNotify();
2080 notify->setInt32("what", kWhatPrepared);
Andreas Huberec0c5972013-02-05 14:47:13 -08002081 notify->setInt32("err", err);
Andreas Huber9575c962013-02-05 13:59:56 -08002082 notify->post();
2083}
2084
Andreas Huber84333e02014-02-07 15:36:10 -08002085void NuPlayer::Source::onMessageReceived(const sp<AMessage> & /* msg */) {
Andreas Huberb5f25f02013-02-05 10:14:26 -08002086 TRESPASS();
2087}
2088
Andreas Huberf9334412010-12-15 15:17:42 -08002089} // namespace android