blob: 5800757c39f412f5c50e0b26f57d19de59fabc2c [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;
Robert Shih6ffb1fd2014-10-29 16:24:32 -0700456 int64_t timeUs;
Chong Zhang404fced2014-06-11 14:45:31 -0700457 CHECK(msg->findSize("trackIndex", &trackIndex));
458 CHECK(msg->findInt32("select", &select));
Robert Shih6ffb1fd2014-10-29 16:24:32 -0700459 CHECK(msg->findInt64("timeUs", &timeUs));
Chong Zhang404fced2014-06-11 14:45:31 -0700460
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) {
Robert Shih6ffb1fd2014-10-29 16:24:32 -0700473 err = mSource->selectTrack(trackIndex, select, timeUs);
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
525 sp<RefBase> obj;
Glenn Kasten11731182011-02-08 17:26:17 -0800526 CHECK(msg->findObject("native-window", &obj));
Andreas Huberf9334412010-12-15 15:17:42 -0800527
Wei Jiafef808d2014-10-31 17:57:05 -0700528 if (mSource->getFormat(false /* audio */) == NULL) {
529 performSetSurface(static_cast<NativeWindowWrapper *>(obj.get()));
530 break;
531 }
532
533 mDeferredActions.push_back(
534 new FlushDecoderAction(FLUSH_CMD_FLUSH /* audio */,
535 FLUSH_CMD_SHUTDOWN /* video */));
536
Andreas Huber57a339c2012-12-03 11:18:00 -0800537 mDeferredActions.push_back(
538 new SetSurfaceAction(
539 static_cast<NativeWindowWrapper *>(obj.get())));
James Dong0d268a32012-08-31 12:18:27 -0700540
Andreas Huber57a339c2012-12-03 11:18:00 -0800541 if (obj != NULL) {
Wei Jiafef808d2014-10-31 17:57:05 -0700542 if (mStarted) {
Andy Hung73535852014-09-05 11:42:58 -0700543 // Issue a seek to refresh the video screen only if started otherwise
544 // the extractor may not yet be started and will assert.
545 // If the video decoder is not set (perhaps audio only in this case)
546 // do not perform a seek as it is not needed.
Ronghua Wua73d9e02014-10-08 15:13:29 -0700547 int64_t currentPositionUs = 0;
548 if (getCurrentPosition(&currentPositionUs) == OK) {
549 mDeferredActions.push_back(
550 new SeekAction(currentPositionUs, false /* needNotify */));
551 }
Andy Hung73535852014-09-05 11:42:58 -0700552 }
Wei Jiaac428aa2014-09-02 19:01:34 -0700553
Andreas Huber57a339c2012-12-03 11:18:00 -0800554 // If there is a new surface texture, instantiate decoders
555 // again if possible.
556 mDeferredActions.push_back(
557 new SimpleAction(&NuPlayer::performScanSources));
558 }
559
560 processDeferredActions();
Andreas Huberf9334412010-12-15 15:17:42 -0800561 break;
562 }
563
564 case kWhatSetAudioSink:
565 {
Steve Block3856b092011-10-20 11:56:00 +0100566 ALOGV("kWhatSetAudioSink");
Andreas Huberf9334412010-12-15 15:17:42 -0800567
568 sp<RefBase> obj;
569 CHECK(msg->findObject("sink", &obj));
570
571 mAudioSink = static_cast<MediaPlayerBase::AudioSink *>(obj.get());
572 break;
573 }
574
575 case kWhatStart:
576 {
Steve Block3856b092011-10-20 11:56:00 +0100577 ALOGV("kWhatStart");
Wei Jia94211742014-10-28 17:09:06 -0700578 if (mStarted) {
579 onResume();
580 } else {
581 onStart();
Lajos Molnar09524832014-07-17 14:29:51 -0700582 }
Andreas Huberf9334412010-12-15 15:17:42 -0800583 break;
584 }
585
586 case kWhatScanSources:
587 {
Andreas Huber1aef2112011-01-04 14:01:29 -0800588 int32_t generation;
589 CHECK(msg->findInt32("generation", &generation));
590 if (generation != mScanSourcesGeneration) {
591 // Drop obsolete msg.
592 break;
593 }
594
Andreas Huber5bc087c2010-12-23 10:27:40 -0800595 mScanSourcesPending = false;
596
Steve Block3856b092011-10-20 11:56:00 +0100597 ALOGV("scanning sources haveAudio=%d, haveVideo=%d",
Andreas Huber43c3e6c2011-01-05 12:17:08 -0800598 mAudioDecoder != NULL, mVideoDecoder != NULL);
599
Andreas Huberb7c8e912012-11-27 15:02:53 -0800600 bool mHadAnySourcesBefore =
601 (mAudioDecoder != NULL) || (mVideoDecoder != NULL);
602
Andy Hung282a7e32014-08-14 15:56:34 -0700603 // initialize video before audio because successful initialization of
604 // video may change deep buffer mode of audio.
Haynes Mathew George5d246ef2012-07-09 10:36:57 -0700605 if (mNativeWindow != NULL) {
606 instantiateDecoder(false, &mVideoDecoder);
607 }
Andreas Huberf9334412010-12-15 15:17:42 -0800608
609 if (mAudioSink != NULL) {
Andy Hung282a7e32014-08-14 15:56:34 -0700610 if (mOffloadAudio) {
611 // open audio sink early under offload mode.
612 sp<AMessage> format = mSource->getFormat(true /*audio*/);
613 openAudioSink(format, true /*offloadOnly*/);
614 }
Andreas Huber5bc087c2010-12-23 10:27:40 -0800615 instantiateDecoder(true, &mAudioDecoder);
Andreas Huberf9334412010-12-15 15:17:42 -0800616 }
617
Andreas Huberb7c8e912012-11-27 15:02:53 -0800618 if (!mHadAnySourcesBefore
619 && (mAudioDecoder != NULL || mVideoDecoder != NULL)) {
620 // This is the first time we've found anything playable.
621
Andreas Huber9575c962013-02-05 13:59:56 -0800622 if (mSourceFlags & Source::FLAG_DYNAMIC_DURATION) {
Andreas Huberb7c8e912012-11-27 15:02:53 -0800623 schedulePollDuration();
624 }
625 }
626
Andreas Hubereac68ba2011-09-27 12:12:25 -0700627 status_t err;
628 if ((err = mSource->feedMoreTSData()) != OK) {
Andreas Huber1aef2112011-01-04 14:01:29 -0800629 if (mAudioDecoder == NULL && mVideoDecoder == NULL) {
630 // We're not currently decoding anything (no audio or
631 // video tracks found) and we just ran out of input data.
Andreas Hubereac68ba2011-09-27 12:12:25 -0700632
633 if (err == ERROR_END_OF_STREAM) {
634 notifyListener(MEDIA_PLAYBACK_COMPLETE, 0, 0);
635 } else {
636 notifyListener(MEDIA_ERROR, MEDIA_ERROR_UNKNOWN, err);
637 }
Andreas Huber1aef2112011-01-04 14:01:29 -0800638 }
Andreas Huberf9334412010-12-15 15:17:42 -0800639 break;
640 }
641
Andreas Huberfbe9d812012-08-31 14:05:27 -0700642 if ((mAudioDecoder == NULL && mAudioSink != NULL)
643 || (mVideoDecoder == NULL && mNativeWindow != NULL)) {
Andreas Huberf9334412010-12-15 15:17:42 -0800644 msg->post(100000ll);
Andreas Huber5bc087c2010-12-23 10:27:40 -0800645 mScanSourcesPending = true;
Andreas Huberf9334412010-12-15 15:17:42 -0800646 }
647 break;
648 }
649
650 case kWhatVideoNotify:
651 case kWhatAudioNotify:
652 {
653 bool audio = msg->what() == kWhatAudioNotify;
654
Wei Jia88703c32014-08-06 11:24:07 -0700655 int32_t currentDecoderGeneration =
656 (audio? mAudioDecoderGeneration : mVideoDecoderGeneration);
657 int32_t requesterGeneration = currentDecoderGeneration - 1;
658 CHECK(msg->findInt32("generation", &requesterGeneration));
659
660 if (requesterGeneration != currentDecoderGeneration) {
661 ALOGV("got message from old %s decoder, generation(%d:%d)",
662 audio ? "audio" : "video", requesterGeneration,
663 currentDecoderGeneration);
664 sp<AMessage> reply;
665 if (!(msg->findMessage("reply", &reply))) {
666 return;
667 }
668
669 reply->setInt32("err", INFO_DISCONTINUITY);
670 reply->post();
671 return;
672 }
673
Andreas Huberf9334412010-12-15 15:17:42 -0800674 int32_t what;
Lajos Molnar1cd13982014-01-17 15:12:51 -0800675 CHECK(msg->findInt32("what", &what));
Andreas Huberf9334412010-12-15 15:17:42 -0800676
Lajos Molnar1cd13982014-01-17 15:12:51 -0800677 if (what == Decoder::kWhatFillThisBuffer) {
Andreas Huberf9334412010-12-15 15:17:42 -0800678 status_t err = feedDecoderInputData(
Lajos Molnar1cd13982014-01-17 15:12:51 -0800679 audio, msg);
Andreas Huberf9334412010-12-15 15:17:42 -0800680
Andreas Huber5bc087c2010-12-23 10:27:40 -0800681 if (err == -EWOULDBLOCK) {
Andreas Hubereac68ba2011-09-27 12:12:25 -0700682 if (mSource->feedMoreTSData() == OK) {
Phil Burkc5cc2e22014-09-09 20:08:39 -0700683 msg->post(10 * 1000ll);
Andreas Huber5bc087c2010-12-23 10:27:40 -0800684 }
Andreas Huberf9334412010-12-15 15:17:42 -0800685 }
Lajos Molnar1cd13982014-01-17 15:12:51 -0800686 } else if (what == Decoder::kWhatEOS) {
Andreas Huberdc9bacd2011-09-26 10:53:29 -0700687 int32_t err;
Lajos Molnar1cd13982014-01-17 15:12:51 -0800688 CHECK(msg->findInt32("err", &err));
Andreas Huberdc9bacd2011-09-26 10:53:29 -0700689
690 if (err == ERROR_END_OF_STREAM) {
Steve Block3856b092011-10-20 11:56:00 +0100691 ALOGV("got %s decoder EOS", audio ? "audio" : "video");
Andreas Huberdc9bacd2011-09-26 10:53:29 -0700692 } else {
Steve Block3856b092011-10-20 11:56:00 +0100693 ALOGV("got %s decoder EOS w/ error %d",
Andreas Huberdc9bacd2011-09-26 10:53:29 -0700694 audio ? "audio" : "video",
695 err);
696 }
697
698 mRenderer->queueEOS(audio, err);
Lajos Molnar1cd13982014-01-17 15:12:51 -0800699 } else if (what == Decoder::kWhatFlushCompleted) {
Steve Block3856b092011-10-20 11:56:00 +0100700 ALOGV("decoder %s flush completed", audio ? "audio" : "video");
Andreas Huberf9334412010-12-15 15:17:42 -0800701
Andy Hung8d121d42014-10-03 09:53:53 -0700702 handleFlushComplete(audio, true /* isDecoder */);
Andreas Huber3831a062010-12-21 10:22:33 -0800703 finishFlushIfPossible();
Wei Jiac6cfd702014-11-11 16:33:20 -0800704 } else if (what == Decoder::kWhatVideoSizeChanged) {
Lajos Molnar1cd13982014-01-17 15:12:51 -0800705 sp<AMessage> format;
706 CHECK(msg->findMessage("format", &format));
707
Wei Jiac6cfd702014-11-11 16:33:20 -0800708 sp<AMessage> inputFormat =
709 mSource->getFormat(false /* audio */);
Andreas Huber3831a062010-12-21 10:22:33 -0800710
Wei Jiac6cfd702014-11-11 16:33:20 -0800711 updateVideoSize(inputFormat, format);
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(
Wei Jiafef808d2014-10-31 17:57:05 -0700752 new FlushDecoderAction(
753 audio ? FLUSH_CMD_SHUTDOWN : FLUSH_CMD_NONE,
754 audio ? FLUSH_CMD_NONE : FLUSH_CMD_SHUTDOWN));
Andy Hung2abde2c2014-09-30 14:40:32 -0700755 processDeferredActions();
756 break;
757 case FLUSHING_DECODER:
758 *flushing = FLUSHING_DECODER_SHUTDOWN; // initiate shutdown after flush.
759 break; // Wait for flush to complete.
760 case FLUSHING_DECODER_SHUTDOWN:
761 break; // Wait for flush to complete.
762 case SHUTTING_DOWN_DECODER:
763 break; // Wait for shutdown to complete.
764 case FLUSHED:
765 // Widevine source reads must stop before releasing the video decoder.
766 if (!audio && mSource != NULL && mSourceFlags & Source::FLAG_SECURE) {
767 mSource->stop();
768 }
769 getDecoder(audio)->initiateShutdown(); // In the middle of a seek.
770 *flushing = SHUTTING_DOWN_DECODER; // Shut down.
771 break;
772 case SHUT_DOWN:
773 finishFlushIfPossible(); // Should not occur.
774 break; // Finish anyways.
Marco Nelissen9e2b7912014-08-18 16:13:03 -0700775 }
Andy Hung2abde2c2014-09-30 14:40:32 -0700776 notifyListener(MEDIA_ERROR, MEDIA_ERROR_UNKNOWN, err);
Wei Jiac6cfd702014-11-11 16:33:20 -0800777 } else if (what == Decoder::kWhatRenderBufferTime) {
Lajos Molnar1cd13982014-01-17 15:12:51 -0800778 renderBuffer(audio, msg);
779 } else {
780 ALOGV("Unhandled decoder notification %d '%c%c%c%c'.",
Andreas Hubera1f8ab02012-11-30 10:53:22 -0800781 what,
782 what >> 24,
783 (what >> 16) & 0xff,
784 (what >> 8) & 0xff,
785 what & 0xff);
Andreas Huberf9334412010-12-15 15:17:42 -0800786 }
787
788 break;
789 }
790
791 case kWhatRendererNotify:
792 {
Wei Jia57568df2014-09-22 10:16:29 -0700793 int32_t requesterGeneration = mRendererGeneration - 1;
794 CHECK(msg->findInt32("generation", &requesterGeneration));
795 if (requesterGeneration != mRendererGeneration) {
796 ALOGV("got message from old renderer, generation(%d:%d)",
797 requesterGeneration, mRendererGeneration);
798 return;
799 }
800
Andreas Huberf9334412010-12-15 15:17:42 -0800801 int32_t what;
802 CHECK(msg->findInt32("what", &what));
803
804 if (what == Renderer::kWhatEOS) {
805 int32_t audio;
806 CHECK(msg->findInt32("audio", &audio));
807
Andreas Huberc92fd242011-08-16 13:48:44 -0700808 int32_t finalResult;
809 CHECK(msg->findInt32("finalResult", &finalResult));
810
Andreas Huberf9334412010-12-15 15:17:42 -0800811 if (audio) {
812 mAudioEOS = true;
813 } else {
814 mVideoEOS = true;
815 }
816
Andreas Huberc92fd242011-08-16 13:48:44 -0700817 if (finalResult == ERROR_END_OF_STREAM) {
Steve Block3856b092011-10-20 11:56:00 +0100818 ALOGV("reached %s EOS", audio ? "audio" : "video");
Andreas Huberc92fd242011-08-16 13:48:44 -0700819 } else {
Steve Block29357bc2012-01-06 19:20:56 +0000820 ALOGE("%s track encountered an error (%d)",
Andreas Huberc92fd242011-08-16 13:48:44 -0700821 audio ? "audio" : "video", finalResult);
822
823 notifyListener(
824 MEDIA_ERROR, MEDIA_ERROR_UNKNOWN, finalResult);
825 }
Andreas Huberf9334412010-12-15 15:17:42 -0800826
827 if ((mAudioEOS || mAudioDecoder == NULL)
828 && (mVideoEOS || mVideoDecoder == NULL)) {
829 notifyListener(MEDIA_PLAYBACK_COMPLETE, 0, 0);
830 }
Andreas Huber3fe62152011-09-16 15:09:22 -0700831 } else if (what == Renderer::kWhatFlushComplete) {
Andreas Huberf9334412010-12-15 15:17:42 -0800832 int32_t audio;
833 CHECK(msg->findInt32("audio", &audio));
834
Steve Block3856b092011-10-20 11:56:00 +0100835 ALOGV("renderer %s flush completed.", audio ? "audio" : "video");
Andy Hung8d121d42014-10-03 09:53:53 -0700836 handleFlushComplete(audio, false /* isDecoder */);
837 finishFlushIfPossible();
James Dongf57b4ea2012-07-20 13:38:36 -0700838 } else if (what == Renderer::kWhatVideoRenderingStart) {
839 notifyListener(MEDIA_INFO, MEDIA_INFO_RENDERING_START, 0);
Lajos Molnarcbaffcf2013-08-14 18:30:38 -0700840 } else if (what == Renderer::kWhatMediaRenderingStart) {
841 ALOGV("media rendering started");
842 notifyListener(MEDIA_STARTED, 0, 0);
Wei Jia3a2956d2014-07-22 16:01:33 -0700843 } else if (what == Renderer::kWhatAudioOffloadTearDown) {
844 ALOGV("Tear down audio offload, fall back to s/w path");
845 int64_t positionUs;
846 CHECK(msg->findInt64("positionUs", &positionUs));
Ronghua Wu08529172014-10-02 16:55:52 -0700847 int32_t reason;
848 CHECK(msg->findInt32("reason", &reason));
Andy Hung282a7e32014-08-14 15:56:34 -0700849 closeAudioSink();
Wei Jia3a2956d2014-07-22 16:01:33 -0700850 mAudioDecoder.clear();
Wei Jia57568df2014-09-22 10:16:29 -0700851 ++mAudioDecoderGeneration;
Wei Jia3a2956d2014-07-22 16:01:33 -0700852 mRenderer->flush(true /* audio */);
853 if (mVideoDecoder != NULL) {
854 mRenderer->flush(false /* audio */);
855 }
856 mRenderer->signalDisableOffloadAudio();
857 mOffloadAudio = false;
858
Wei Jiae427abf2014-09-22 15:21:11 -0700859 performSeek(positionUs, false /* needNotify */);
Ronghua Wu08529172014-10-02 16:55:52 -0700860 if (reason == Renderer::kDueToError) {
861 instantiateDecoder(true /* audio */, &mAudioDecoder);
862 }
Andreas Huberf9334412010-12-15 15:17:42 -0800863 }
864 break;
865 }
866
867 case kWhatMoreDataQueued:
868 {
869 break;
870 }
871
Andreas Huber1aef2112011-01-04 14:01:29 -0800872 case kWhatReset:
873 {
Steve Block3856b092011-10-20 11:56:00 +0100874 ALOGV("kWhatReset");
Andreas Huber1aef2112011-01-04 14:01:29 -0800875
Andreas Hubera1f8ab02012-11-30 10:53:22 -0800876 mDeferredActions.push_back(
Wei Jiafef808d2014-10-31 17:57:05 -0700877 new FlushDecoderAction(
878 FLUSH_CMD_SHUTDOWN /* audio */,
879 FLUSH_CMD_SHUTDOWN /* video */));
Andreas Huberb7c8e912012-11-27 15:02:53 -0800880
Andreas Hubera1f8ab02012-11-30 10:53:22 -0800881 mDeferredActions.push_back(
882 new SimpleAction(&NuPlayer::performReset));
Andreas Huberb58ce9f2011-11-28 16:27:35 -0800883
Andreas Hubera1f8ab02012-11-30 10:53:22 -0800884 processDeferredActions();
Andreas Huber1aef2112011-01-04 14:01:29 -0800885 break;
886 }
887
Andreas Huber43c3e6c2011-01-05 12:17:08 -0800888 case kWhatSeek:
889 {
890 int64_t seekTimeUs;
Wei Jiae427abf2014-09-22 15:21:11 -0700891 int32_t needNotify;
Andreas Huber43c3e6c2011-01-05 12:17:08 -0800892 CHECK(msg->findInt64("seekTimeUs", &seekTimeUs));
Wei Jiae427abf2014-09-22 15:21:11 -0700893 CHECK(msg->findInt32("needNotify", &needNotify));
Andreas Huber43c3e6c2011-01-05 12:17:08 -0800894
Wei Jiae427abf2014-09-22 15:21:11 -0700895 ALOGV("kWhatSeek seekTimeUs=%lld us, needNotify=%d",
896 seekTimeUs, needNotify);
Andreas Huber43c3e6c2011-01-05 12:17:08 -0800897
Andreas Hubera1f8ab02012-11-30 10:53:22 -0800898 mDeferredActions.push_back(
Wei Jiafef808d2014-10-31 17:57:05 -0700899 new FlushDecoderAction(FLUSH_CMD_FLUSH /* audio */,
900 FLUSH_CMD_FLUSH /* video */));
Andreas Huber43c3e6c2011-01-05 12:17:08 -0800901
Wei Jiae427abf2014-09-22 15:21:11 -0700902 mDeferredActions.push_back(
903 new SeekAction(seekTimeUs, needNotify));
Andreas Huber43c3e6c2011-01-05 12:17:08 -0800904
Andreas Hubera1f8ab02012-11-30 10:53:22 -0800905 processDeferredActions();
Andreas Huber43c3e6c2011-01-05 12:17:08 -0800906 break;
907 }
908
Andreas Huberb4082222011-01-20 15:23:04 -0800909 case kWhatPause:
910 {
Wei Jia7e9f7f72014-09-23 10:55:35 -0700911 if (mSource != NULL) {
912 mSource->pause();
913 } else {
914 ALOGW("pause called when source is gone or not set");
915 }
916 if (mRenderer != NULL) {
917 mRenderer->pause();
918 } else {
919 ALOGW("pause called when renderer is gone or not set");
920 }
Andreas Huberb4082222011-01-20 15:23:04 -0800921 break;
922 }
923
Andreas Huberb5f25f02013-02-05 10:14:26 -0800924 case kWhatSourceNotify:
925 {
Andreas Huber9575c962013-02-05 13:59:56 -0800926 onSourceNotify(msg);
Andreas Huberb5f25f02013-02-05 10:14:26 -0800927 break;
928 }
929
Chong Zhanga7fa1d92014-06-11 14:49:23 -0700930 case kWhatClosedCaptionNotify:
931 {
932 onClosedCaptionNotify(msg);
933 break;
934 }
935
Andreas Huberf9334412010-12-15 15:17:42 -0800936 default:
937 TRESPASS();
938 break;
939 }
940}
941
Wei Jia94211742014-10-28 17:09:06 -0700942void NuPlayer::onResume() {
943 if (mSource != NULL) {
944 mSource->resume();
945 } else {
946 ALOGW("resume called when source is gone or not set");
947 }
948 // |mAudioDecoder| may have been released due to the pause timeout, so re-create it if
949 // needed.
950 if (audioDecoderStillNeeded() && mAudioDecoder == NULL) {
951 instantiateDecoder(true /* audio */, &mAudioDecoder);
952 }
953 if (mRenderer != NULL) {
954 mRenderer->resume();
955 } else {
956 ALOGW("resume called when renderer is gone or not set");
957 }
958}
959
960void NuPlayer::onStart() {
961 mVideoIsAVC = false;
962 mOffloadAudio = false;
963 mAudioEOS = false;
964 mVideoEOS = false;
Wei Jia94211742014-10-28 17:09:06 -0700965 mNumFramesTotal = 0;
966 mNumFramesDropped = 0;
967 mStarted = true;
968
969 /* instantiate decoders now for secure playback */
970 if (mSourceFlags & Source::FLAG_SECURE) {
971 if (mNativeWindow != NULL) {
972 instantiateDecoder(false, &mVideoDecoder);
973 }
974
975 if (mAudioSink != NULL) {
976 instantiateDecoder(true, &mAudioDecoder);
977 }
978 }
979
980 mSource->start();
981
982 uint32_t flags = 0;
983
984 if (mSource->isRealTime()) {
985 flags |= Renderer::FLAG_REAL_TIME;
986 }
987
988 sp<MetaData> audioMeta = mSource->getFormatMeta(true /* audio */);
989 audio_stream_type_t streamType = AUDIO_STREAM_MUSIC;
990 if (mAudioSink != NULL) {
991 streamType = mAudioSink->getAudioStreamType();
992 }
993
994 sp<AMessage> videoFormat = mSource->getFormat(false /* audio */);
995
996 mOffloadAudio =
997 canOffloadStream(audioMeta, (videoFormat != NULL),
998 true /* is_streaming */, streamType);
999 if (mOffloadAudio) {
1000 flags |= Renderer::FLAG_OFFLOAD_AUDIO;
1001 }
1002
1003 sp<AMessage> notify = new AMessage(kWhatRendererNotify, id());
1004 ++mRendererGeneration;
1005 notify->setInt32("generation", mRendererGeneration);
1006 mRenderer = new Renderer(mAudioSink, notify, flags);
1007
1008 mRendererLooper = new ALooper;
1009 mRendererLooper->setName("NuPlayerRenderer");
1010 mRendererLooper->start(false, false, ANDROID_PRIORITY_AUDIO);
1011 mRendererLooper->registerHandler(mRenderer);
1012
1013 sp<MetaData> meta = getFileMeta();
1014 int32_t rate;
1015 if (meta != NULL
1016 && meta->findInt32(kKeyFrameRate, &rate) && rate > 0) {
1017 mRenderer->setVideoFrameRate(rate);
1018 }
1019
Wei Jiac6cfd702014-11-11 16:33:20 -08001020 if (mVideoDecoder != NULL) {
1021 mVideoDecoder->setRenderer(mRenderer);
1022 }
1023 if (mAudioDecoder != NULL) {
1024 mAudioDecoder->setRenderer(mRenderer);
1025 }
1026
Wei Jia94211742014-10-28 17:09:06 -07001027 postScanSources();
1028}
1029
Ronghua Wud7988b12014-10-03 15:19:10 -07001030bool NuPlayer::audioDecoderStillNeeded() {
1031 // Audio decoder is no longer needed if it's in shut/shutting down status.
1032 return ((mFlushingAudio != SHUT_DOWN) && (mFlushingAudio != SHUTTING_DOWN_DECODER));
1033}
1034
Andy Hung8d121d42014-10-03 09:53:53 -07001035void NuPlayer::handleFlushComplete(bool audio, bool isDecoder) {
1036 // We wait for both the decoder flush and the renderer flush to complete
1037 // before entering either the FLUSHED or the SHUTTING_DOWN_DECODER state.
1038
1039 mFlushComplete[audio][isDecoder] = true;
1040 if (!mFlushComplete[audio][!isDecoder]) {
1041 return;
1042 }
1043
1044 FlushStatus *state = audio ? &mFlushingAudio : &mFlushingVideo;
1045 switch (*state) {
1046 case FLUSHING_DECODER:
1047 {
1048 *state = FLUSHED;
Andy Hung8d121d42014-10-03 09:53:53 -07001049 break;
1050 }
1051
1052 case FLUSHING_DECODER_SHUTDOWN:
1053 {
1054 *state = SHUTTING_DOWN_DECODER;
1055
1056 ALOGV("initiating %s decoder shutdown", audio ? "audio" : "video");
1057 if (!audio) {
Andy Hung8d121d42014-10-03 09:53:53 -07001058 // Widevine source reads must stop before releasing the video decoder.
1059 if (mSource != NULL && mSourceFlags & Source::FLAG_SECURE) {
1060 mSource->stop();
1061 }
1062 }
1063 getDecoder(audio)->initiateShutdown();
1064 break;
1065 }
1066
1067 default:
1068 // decoder flush completes only occur in a flushing state.
1069 LOG_ALWAYS_FATAL_IF(isDecoder, "decoder flush in invalid state %d", *state);
1070 break;
1071 }
1072}
1073
Andreas Huber3831a062010-12-21 10:22:33 -08001074void NuPlayer::finishFlushIfPossible() {
Wei Jia53904f32014-07-29 10:22:53 -07001075 if (mFlushingAudio != NONE && mFlushingAudio != FLUSHED
1076 && mFlushingAudio != SHUT_DOWN) {
Andreas Huber3831a062010-12-21 10:22:33 -08001077 return;
1078 }
1079
Wei Jia53904f32014-07-29 10:22:53 -07001080 if (mFlushingVideo != NONE && mFlushingVideo != FLUSHED
1081 && mFlushingVideo != SHUT_DOWN) {
Andreas Huber3831a062010-12-21 10:22:33 -08001082 return;
1083 }
1084
Steve Block3856b092011-10-20 11:56:00 +01001085 ALOGV("both audio and video are flushed now.");
Andreas Huber3831a062010-12-21 10:22:33 -08001086
Phil Burk9f526492014-09-03 15:04:12 -07001087 mPendingAudioAccessUnit.clear();
Phil Burkc5cc2e22014-09-09 20:08:39 -07001088 mAggregateBuffer.clear();
Phil Burk9f526492014-09-03 15:04:12 -07001089
Andreas Huber6e3d3112011-11-28 12:36:11 -08001090 if (mTimeDiscontinuityPending) {
1091 mRenderer->signalTimeDiscontinuity();
1092 mTimeDiscontinuityPending = false;
1093 }
Andreas Huber3831a062010-12-21 10:22:33 -08001094
Wei Jia53904f32014-07-29 10:22:53 -07001095 if (mAudioDecoder != NULL && mFlushingAudio == FLUSHED) {
Andreas Huber3831a062010-12-21 10:22:33 -08001096 mAudioDecoder->signalResume();
1097 }
1098
Wei Jia53904f32014-07-29 10:22:53 -07001099 if (mVideoDecoder != NULL && mFlushingVideo == FLUSHED) {
Andreas Huber3831a062010-12-21 10:22:33 -08001100 mVideoDecoder->signalResume();
1101 }
1102
1103 mFlushingAudio = NONE;
1104 mFlushingVideo = NONE;
Andreas Huber3831a062010-12-21 10:22:33 -08001105
Andy Hung8d121d42014-10-03 09:53:53 -07001106 clearFlushComplete();
1107
Andreas Hubera1f8ab02012-11-30 10:53:22 -08001108 processDeferredActions();
Andreas Huber1aef2112011-01-04 14:01:29 -08001109}
1110
1111void NuPlayer::postScanSources() {
1112 if (mScanSourcesPending) {
1113 return;
1114 }
1115
1116 sp<AMessage> msg = new AMessage(kWhatScanSources, id());
1117 msg->setInt32("generation", mScanSourcesGeneration);
1118 msg->post();
1119
1120 mScanSourcesPending = true;
1121}
1122
Andy Hung282a7e32014-08-14 15:56:34 -07001123void NuPlayer::openAudioSink(const sp<AMessage> &format, bool offloadOnly) {
Andy Hung282a7e32014-08-14 15:56:34 -07001124 uint32_t flags;
1125 int64_t durationUs;
Chong Zhang3b9eb1f2014-10-15 17:05:08 -07001126 bool hasVideo = (mVideoDecoder != NULL);
Andy Hung282a7e32014-08-14 15:56:34 -07001127 // FIXME: we should handle the case where the video decoder
1128 // is created after we receive the format change indication.
1129 // Current code will just make that we select deep buffer
1130 // with video which should not be a problem as it should
1131 // not prevent from keeping A/V sync.
Eric Laurentd88c3ca2014-10-28 10:52:11 -07001132 if (!hasVideo &&
Andy Hung282a7e32014-08-14 15:56:34 -07001133 mSource->getDuration(&durationUs) == OK &&
1134 durationUs
1135 > AUDIO_SINK_MIN_DEEP_BUFFER_DURATION_US) {
1136 flags = AUDIO_OUTPUT_FLAG_DEEP_BUFFER;
1137 } else {
1138 flags = AUDIO_OUTPUT_FLAG_NONE;
1139 }
1140
Chong Zhang3b9eb1f2014-10-15 17:05:08 -07001141 mOffloadAudio = mRenderer->openAudioSink(
1142 format, offloadOnly, hasVideo, flags);
1143
Andy Hung282a7e32014-08-14 15:56:34 -07001144 if (mOffloadAudio) {
Chong Zhang3b9eb1f2014-10-15 17:05:08 -07001145 sp<MetaData> audioMeta =
1146 mSource->getFormatMeta(true /* audio */);
1147 sendMetaDataToHal(mAudioSink, audioMeta);
Andy Hung282a7e32014-08-14 15:56:34 -07001148 }
1149}
1150
1151void NuPlayer::closeAudioSink() {
Chong Zhang3b9eb1f2014-10-15 17:05:08 -07001152 mRenderer->closeAudioSink();
Andy Hung282a7e32014-08-14 15:56:34 -07001153}
1154
Andreas Huber5bc087c2010-12-23 10:27:40 -08001155status_t NuPlayer::instantiateDecoder(bool audio, sp<Decoder> *decoder) {
Andreas Huberf9334412010-12-15 15:17:42 -08001156 if (*decoder != NULL) {
1157 return OK;
1158 }
1159
Andreas Huber84066782011-08-16 09:34:26 -07001160 sp<AMessage> format = mSource->getFormat(audio);
Andreas Huberf9334412010-12-15 15:17:42 -08001161
Andreas Huber84066782011-08-16 09:34:26 -07001162 if (format == NULL) {
Andreas Huberf9334412010-12-15 15:17:42 -08001163 return -EWOULDBLOCK;
1164 }
1165
Andreas Huber3fe62152011-09-16 15:09:22 -07001166 if (!audio) {
Andreas Huber84066782011-08-16 09:34:26 -07001167 AString mime;
1168 CHECK(format->findString("mime", &mime));
1169 mVideoIsAVC = !strcasecmp(MEDIA_MIMETYPE_VIDEO_AVC, mime.c_str());
Chong Zhanga7fa1d92014-06-11 14:49:23 -07001170
1171 sp<AMessage> ccNotify = new AMessage(kWhatClosedCaptionNotify, id());
1172 mCCDecoder = new CCDecoder(ccNotify);
Lajos Molnar09524832014-07-17 14:29:51 -07001173
1174 if (mSourceFlags & Source::FLAG_SECURE) {
1175 format->setInt32("secure", true);
1176 }
Andreas Huber3fe62152011-09-16 15:09:22 -07001177 }
1178
Wei Jiabc2fb722014-07-08 16:37:57 -07001179 if (audio) {
Wei Jia88703c32014-08-06 11:24:07 -07001180 sp<AMessage> notify = new AMessage(kWhatAudioNotify, id());
1181 ++mAudioDecoderGeneration;
1182 notify->setInt32("generation", mAudioDecoderGeneration);
1183
Wei Jiabc2fb722014-07-08 16:37:57 -07001184 if (mOffloadAudio) {
Wei Jiac6cfd702014-11-11 16:33:20 -08001185 *decoder = new DecoderPassThrough(notify, mSource, mRenderer);
Wei Jiabc2fb722014-07-08 16:37:57 -07001186 } else {
Wei Jiac6cfd702014-11-11 16:33:20 -08001187 *decoder = new Decoder(notify, mSource, mRenderer);
Wei Jiabc2fb722014-07-08 16:37:57 -07001188 }
1189 } else {
Wei Jia88703c32014-08-06 11:24:07 -07001190 sp<AMessage> notify = new AMessage(kWhatVideoNotify, id());
1191 ++mVideoDecoderGeneration;
1192 notify->setInt32("generation", mVideoDecoderGeneration);
1193
Wei Jiac6cfd702014-11-11 16:33:20 -08001194 *decoder = new Decoder(notify, mSource, mRenderer, mNativeWindow);
Wei Jiabc2fb722014-07-08 16:37:57 -07001195 }
Lajos Molnar1cd13982014-01-17 15:12:51 -08001196 (*decoder)->init();
Andreas Huber84066782011-08-16 09:34:26 -07001197 (*decoder)->configure(format);
Andreas Huberf9334412010-12-15 15:17:42 -08001198
Lajos Molnar09524832014-07-17 14:29:51 -07001199 // allocate buffers to decrypt widevine source buffers
1200 if (!audio && (mSourceFlags & Source::FLAG_SECURE)) {
1201 Vector<sp<ABuffer> > inputBufs;
1202 CHECK_EQ((*decoder)->getInputBuffers(&inputBufs), (status_t)OK);
1203
1204 Vector<MediaBuffer *> mediaBufs;
1205 for (size_t i = 0; i < inputBufs.size(); i++) {
1206 const sp<ABuffer> &buffer = inputBufs[i];
1207 MediaBuffer *mbuf = new MediaBuffer(buffer->data(), buffer->size());
1208 mediaBufs.push(mbuf);
1209 }
1210
1211 status_t err = mSource->setBuffers(audio, mediaBufs);
1212 if (err != OK) {
1213 for (size_t i = 0; i < mediaBufs.size(); ++i) {
1214 mediaBufs[i]->release();
1215 }
1216 mediaBufs.clear();
1217 ALOGE("Secure source didn't support secure mediaBufs.");
1218 return err;
1219 }
1220 }
Andreas Huberf9334412010-12-15 15:17:42 -08001221 return OK;
1222}
1223
1224status_t NuPlayer::feedDecoderInputData(bool audio, const sp<AMessage> &msg) {
1225 sp<AMessage> reply;
1226 CHECK(msg->findMessage("reply", &reply));
1227
Wei Jia53904f32014-07-29 10:22:53 -07001228 if ((audio && mFlushingAudio != NONE)
Wei Jiaf702d042014-09-09 12:08:47 -07001229 || (!audio && mFlushingVideo != NONE)
1230 || mSource == NULL) {
Wei Jiab189a5b2014-08-07 06:11:39 +00001231 reply->setInt32("err", INFO_DISCONTINUITY);
1232 reply->post();
1233 return OK;
Andreas Huberf9334412010-12-15 15:17:42 -08001234 }
1235
1236 sp<ABuffer> accessUnit;
Andreas Huberf9334412010-12-15 15:17:42 -08001237
Phil Burk9f526492014-09-03 15:04:12 -07001238 // Aggregate smaller buffers into a larger buffer.
1239 // The goal is to reduce power consumption.
Phil Burk33b51b02014-09-17 16:03:47 -07001240 // Note this will not work if the decoder requires one frame per buffer.
1241 bool doBufferAggregation = (audio && mOffloadAudio);
Phil Burk9f526492014-09-03 15:04:12 -07001242 bool needMoreData = false;
Phil Burk9f526492014-09-03 15:04:12 -07001243
Andreas Huber3fe62152011-09-16 15:09:22 -07001244 bool dropAccessUnit;
1245 do {
Phil Burk9f526492014-09-03 15:04:12 -07001246 status_t err;
1247 // Did we save an accessUnit earlier because of a discontinuity?
1248 if (audio && (mPendingAudioAccessUnit != NULL)) {
1249 accessUnit = mPendingAudioAccessUnit;
1250 mPendingAudioAccessUnit.clear();
1251 err = mPendingAudioErr;
1252 ALOGV("feedDecoderInputData() use mPendingAudioAccessUnit");
1253 } else {
1254 err = mSource->dequeueAccessUnit(audio, &accessUnit);
1255 }
Andreas Huber5bc087c2010-12-23 10:27:40 -08001256
Andreas Huber3fe62152011-09-16 15:09:22 -07001257 if (err == -EWOULDBLOCK) {
Phil Burkc5cc2e22014-09-09 20:08:39 -07001258 return err;
Andreas Huber3fe62152011-09-16 15:09:22 -07001259 } else if (err != OK) {
1260 if (err == INFO_DISCONTINUITY) {
Phil Burk33b51b02014-09-17 16:03:47 -07001261 if (doBufferAggregation && (mAggregateBuffer != NULL)) {
Phil Burk9f526492014-09-03 15:04:12 -07001262 // We already have some data so save this for later.
1263 mPendingAudioErr = err;
1264 mPendingAudioAccessUnit = accessUnit;
1265 accessUnit.clear();
1266 ALOGD("feedDecoderInputData() save discontinuity for later");
1267 break;
1268 }
Andreas Huber3fe62152011-09-16 15:09:22 -07001269 int32_t type;
1270 CHECK(accessUnit->meta()->findInt32("discontinuity", &type));
Andreas Huber53df1a42010-12-22 10:03:04 -08001271
Andreas Huber3fe62152011-09-16 15:09:22 -07001272 bool formatChange =
Andreas Huber6e3d3112011-11-28 12:36:11 -08001273 (audio &&
1274 (type & ATSParser::DISCONTINUITY_AUDIO_FORMAT))
1275 || (!audio &&
1276 (type & ATSParser::DISCONTINUITY_VIDEO_FORMAT));
Andreas Huber53df1a42010-12-22 10:03:04 -08001277
Andreas Huber6e3d3112011-11-28 12:36:11 -08001278 bool timeChange = (type & ATSParser::DISCONTINUITY_TIME) != 0;
1279
Steve Blockdf64d152012-01-04 20:05:49 +00001280 ALOGI("%s discontinuity (formatChange=%d, time=%d)",
Andreas Huber6e3d3112011-11-28 12:36:11 -08001281 audio ? "audio" : "video", formatChange, timeChange);
Andreas Huber32f3cef2011-03-02 15:34:46 -08001282
Andreas Huber6e3d3112011-11-28 12:36:11 -08001283 mTimeDiscontinuityPending =
1284 mTimeDiscontinuityPending || timeChange;
1285
Lajos Molnar87603c02014-08-20 19:25:30 -07001286 bool seamlessFormatChange = false;
1287 sp<AMessage> newFormat = mSource->getFormat(audio);
1288 if (formatChange) {
1289 seamlessFormatChange =
1290 getDecoder(audio)->supportsSeamlessFormatChange(newFormat);
1291 // treat seamless format change separately
1292 formatChange = !seamlessFormatChange;
1293 }
1294 bool shutdownOrFlush = formatChange || timeChange;
1295
1296 // We want to queue up scan-sources only once per discontinuity.
1297 // We control this by doing it only if neither audio nor video are
1298 // flushing or shutting down. (After handling 1st discontinuity, one
1299 // of the flushing states will not be NONE.)
1300 // No need to scan sources if this discontinuity does not result
1301 // in a flush or shutdown, as the flushing state will stay NONE.
1302 if (mFlushingAudio == NONE && mFlushingVideo == NONE &&
1303 shutdownOrFlush) {
Robert Shiha2981012014-07-30 17:41:24 -07001304 // And we'll resume scanning sources once we're done
1305 // flushing.
1306 mDeferredActions.push_front(
1307 new SimpleAction(
1308 &NuPlayer::performScanSources));
1309 }
1310
Lajos Molnar87603c02014-08-20 19:25:30 -07001311 if (formatChange /* not seamless */) {
1312 // must change decoder
1313 flushDecoder(audio, /* needShutdown = */ true);
1314 } else if (timeChange) {
1315 // need to flush
1316 flushDecoder(audio, /* needShutdown = */ false, newFormat);
1317 err = OK;
1318 } else if (seamlessFormatChange) {
1319 // reuse existing decoder and don't flush
1320 updateDecoderFormatWithoutFlush(audio, newFormat);
1321 err = OK;
Andreas Huber6e3d3112011-11-28 12:36:11 -08001322 } else {
1323 // This stream is unaffected by the discontinuity
Andreas Huber6e3d3112011-11-28 12:36:11 -08001324 return -EWOULDBLOCK;
1325 }
Andreas Huber32f3cef2011-03-02 15:34:46 -08001326 }
1327
Andreas Huber3fe62152011-09-16 15:09:22 -07001328 reply->setInt32("err", err);
1329 reply->post();
1330 return OK;
Andreas Huberf9334412010-12-15 15:17:42 -08001331 }
1332
Andreas Huber3fe62152011-09-16 15:09:22 -07001333 if (!audio) {
1334 ++mNumFramesTotal;
1335 }
1336
1337 dropAccessUnit = false;
1338 if (!audio
Lajos Molnar09524832014-07-17 14:29:51 -07001339 && !(mSourceFlags & Source::FLAG_SECURE)
Ronghua Wua73d9e02014-10-08 15:13:29 -07001340 && mRenderer->getVideoLateByUs() > 100000ll
Andreas Huber3fe62152011-09-16 15:09:22 -07001341 && mVideoIsAVC
1342 && !IsAVCReferenceFrame(accessUnit)) {
1343 dropAccessUnit = true;
1344 ++mNumFramesDropped;
1345 }
Phil Burk9f526492014-09-03 15:04:12 -07001346
1347 size_t smallSize = accessUnit->size();
1348 needMoreData = false;
Phil Burkc5cc2e22014-09-09 20:08:39 -07001349 if (doBufferAggregation && (mAggregateBuffer == NULL)
Phil Burk9f526492014-09-03 15:04:12 -07001350 // Don't bother if only room for a few small buffers.
Phil Burkc5cc2e22014-09-09 20:08:39 -07001351 && (smallSize < (kAggregateBufferSizeBytes / 3))) {
Phil Burk9f526492014-09-03 15:04:12 -07001352 // Create a larger buffer for combining smaller buffers from the extractor.
Phil Burkc5cc2e22014-09-09 20:08:39 -07001353 mAggregateBuffer = new ABuffer(kAggregateBufferSizeBytes);
1354 mAggregateBuffer->setRange(0, 0); // start empty
Phil Burk9f526492014-09-03 15:04:12 -07001355 }
1356
Phil Burk33b51b02014-09-17 16:03:47 -07001357 if (doBufferAggregation && (mAggregateBuffer != NULL)) {
Phil Burk9f526492014-09-03 15:04:12 -07001358 int64_t timeUs;
Phil Burkc5cc2e22014-09-09 20:08:39 -07001359 int64_t dummy;
Phil Burk9f526492014-09-03 15:04:12 -07001360 bool smallTimestampValid = accessUnit->meta()->findInt64("timeUs", &timeUs);
Phil Burkc5cc2e22014-09-09 20:08:39 -07001361 bool bigTimestampValid = mAggregateBuffer->meta()->findInt64("timeUs", &dummy);
Phil Burk9f526492014-09-03 15:04:12 -07001362 // Will the smaller buffer fit?
Phil Burkc5cc2e22014-09-09 20:08:39 -07001363 size_t bigSize = mAggregateBuffer->size();
1364 size_t roomLeft = mAggregateBuffer->capacity() - bigSize;
Phil Burk9f526492014-09-03 15:04:12 -07001365 // Should we save this small buffer for the next big buffer?
1366 // If the first small buffer did not have a timestamp then save
1367 // any buffer that does have a timestamp until the next big buffer.
1368 if ((smallSize > roomLeft)
Phil Burkc5cc2e22014-09-09 20:08:39 -07001369 || (!bigTimestampValid && (bigSize > 0) && smallTimestampValid)) {
Phil Burk9f526492014-09-03 15:04:12 -07001370 mPendingAudioErr = err;
1371 mPendingAudioAccessUnit = accessUnit;
1372 accessUnit.clear();
1373 } else {
Phil Burkc5cc2e22014-09-09 20:08:39 -07001374 // Grab time from first small buffer if available.
1375 if ((bigSize == 0) && smallTimestampValid) {
1376 mAggregateBuffer->meta()->setInt64("timeUs", timeUs);
1377 }
Phil Burk9f526492014-09-03 15:04:12 -07001378 // Append small buffer to the bigger buffer.
Phil Burkc5cc2e22014-09-09 20:08:39 -07001379 memcpy(mAggregateBuffer->base() + bigSize, accessUnit->data(), smallSize);
Phil Burk9f526492014-09-03 15:04:12 -07001380 bigSize += smallSize;
Phil Burkc5cc2e22014-09-09 20:08:39 -07001381 mAggregateBuffer->setRange(0, bigSize);
Phil Burk9f526492014-09-03 15:04:12 -07001382
Phil Burkc5cc2e22014-09-09 20:08:39 -07001383 // Keep looping until we run out of room in the mAggregateBuffer.
Phil Burk9f526492014-09-03 15:04:12 -07001384 needMoreData = true;
1385
Phil Burkc5cc2e22014-09-09 20:08:39 -07001386 ALOGV("feedDecoderInputData() smallSize = %zu, bigSize = %zu, capacity = %zu",
1387 smallSize, bigSize, mAggregateBuffer->capacity());
Phil Burk9f526492014-09-03 15:04:12 -07001388 }
1389 }
1390 } while (dropAccessUnit || needMoreData);
Andreas Huberf9334412010-12-15 15:17:42 -08001391
Steve Block3856b092011-10-20 11:56:00 +01001392 // ALOGV("returned a valid buffer of %s data", audio ? "audio" : "video");
Andreas Huberf9334412010-12-15 15:17:42 -08001393
1394#if 0
1395 int64_t mediaTimeUs;
1396 CHECK(accessUnit->meta()->findInt64("timeUs", &mediaTimeUs));
Steve Block3856b092011-10-20 11:56:00 +01001397 ALOGV("feeding %s input buffer at media time %.2f secs",
Andreas Huberf9334412010-12-15 15:17:42 -08001398 audio ? "audio" : "video",
1399 mediaTimeUs / 1E6);
1400#endif
1401
Chong Zhanga7fa1d92014-06-11 14:49:23 -07001402 if (!audio) {
1403 mCCDecoder->decode(accessUnit);
1404 }
1405
Phil Burk33b51b02014-09-17 16:03:47 -07001406 if (doBufferAggregation && (mAggregateBuffer != NULL)) {
Phil Burkc5cc2e22014-09-09 20:08:39 -07001407 ALOGV("feedDecoderInputData() reply with aggregated buffer, %zu",
1408 mAggregateBuffer->size());
1409 reply->setBuffer("buffer", mAggregateBuffer);
1410 mAggregateBuffer.clear();
Phil Burk9f526492014-09-03 15:04:12 -07001411 } else {
1412 reply->setBuffer("buffer", accessUnit);
1413 }
1414
Andreas Huberf9334412010-12-15 15:17:42 -08001415 reply->post();
1416
1417 return OK;
1418}
1419
1420void NuPlayer::renderBuffer(bool audio, const sp<AMessage> &msg) {
Steve Block3856b092011-10-20 11:56:00 +01001421 // ALOGV("renderBuffer %s", audio ? "audio" : "video");
Andreas Huberf9334412010-12-15 15:17:42 -08001422
Wei Jia53904f32014-07-29 10:22:53 -07001423 if ((audio && mFlushingAudio != NONE)
1424 || (!audio && mFlushingVideo != NONE)) {
Andreas Huber18ac5402011-08-31 15:04:25 -07001425 // We're currently attempting to flush the decoder, in order
1426 // to complete this, the decoder wants all its buffers back,
1427 // so we don't want any output buffers it sent us (from before
1428 // we initiated the flush) to be stuck in the renderer's queue.
1429
Steve Block3856b092011-10-20 11:56:00 +01001430 ALOGV("we're still flushing the %s decoder, sending its output buffer"
Andreas Huber18ac5402011-08-31 15:04:25 -07001431 " right back.", audio ? "audio" : "video");
1432
Andreas Huber18ac5402011-08-31 15:04:25 -07001433 return;
1434 }
1435
Chong Zhanga7fa1d92014-06-11 14:49:23 -07001436 int64_t mediaTimeUs;
Wei Jiac6cfd702014-11-11 16:33:20 -08001437 CHECK(msg->findInt64("timeUs", &mediaTimeUs));
Andreas Huber32f3cef2011-03-02 15:34:46 -08001438
Chong Zhanga7fa1d92014-06-11 14:49:23 -07001439 if (!audio && mCCDecoder->isSelected()) {
1440 mCCDecoder->display(mediaTimeUs);
1441 }
Andreas Huberf9334412010-12-15 15:17:42 -08001442}
1443
Chong Zhangced1c2f2014-08-08 15:22:35 -07001444void NuPlayer::updateVideoSize(
1445 const sp<AMessage> &inputFormat,
1446 const sp<AMessage> &outputFormat) {
1447 if (inputFormat == NULL) {
1448 ALOGW("Unknown video size, reporting 0x0!");
1449 notifyListener(MEDIA_SET_VIDEO_SIZE, 0, 0);
1450 return;
1451 }
1452
1453 int32_t displayWidth, displayHeight;
1454 int32_t cropLeft, cropTop, cropRight, cropBottom;
1455
1456 if (outputFormat != NULL) {
1457 int32_t width, height;
1458 CHECK(outputFormat->findInt32("width", &width));
1459 CHECK(outputFormat->findInt32("height", &height));
1460
1461 int32_t cropLeft, cropTop, cropRight, cropBottom;
1462 CHECK(outputFormat->findRect(
1463 "crop",
1464 &cropLeft, &cropTop, &cropRight, &cropBottom));
1465
1466 displayWidth = cropRight - cropLeft + 1;
1467 displayHeight = cropBottom - cropTop + 1;
1468
1469 ALOGV("Video output format changed to %d x %d "
1470 "(crop: %d x %d @ (%d, %d))",
1471 width, height,
1472 displayWidth,
1473 displayHeight,
1474 cropLeft, cropTop);
1475 } else {
1476 CHECK(inputFormat->findInt32("width", &displayWidth));
1477 CHECK(inputFormat->findInt32("height", &displayHeight));
1478
1479 ALOGV("Video input format %d x %d", displayWidth, displayHeight);
1480 }
1481
1482 // Take into account sample aspect ratio if necessary:
1483 int32_t sarWidth, sarHeight;
1484 if (inputFormat->findInt32("sar-width", &sarWidth)
1485 && inputFormat->findInt32("sar-height", &sarHeight)) {
1486 ALOGV("Sample aspect ratio %d : %d", sarWidth, sarHeight);
1487
1488 displayWidth = (displayWidth * sarWidth) / sarHeight;
1489
1490 ALOGV("display dimensions %d x %d", displayWidth, displayHeight);
1491 }
1492
1493 int32_t rotationDegrees;
1494 if (!inputFormat->findInt32("rotation-degrees", &rotationDegrees)) {
1495 rotationDegrees = 0;
1496 }
1497
1498 if (rotationDegrees == 90 || rotationDegrees == 270) {
1499 int32_t tmp = displayWidth;
1500 displayWidth = displayHeight;
1501 displayHeight = tmp;
1502 }
1503
1504 notifyListener(
1505 MEDIA_SET_VIDEO_SIZE,
1506 displayWidth,
1507 displayHeight);
1508}
1509
Chong Zhangdcb89b32013-08-06 09:44:47 -07001510void NuPlayer::notifyListener(int msg, int ext1, int ext2, const Parcel *in) {
Andreas Huber43c3e6c2011-01-05 12:17:08 -08001511 if (mDriver == NULL) {
Andreas Huberf9334412010-12-15 15:17:42 -08001512 return;
1513 }
1514
Andreas Huber43c3e6c2011-01-05 12:17:08 -08001515 sp<NuPlayerDriver> driver = mDriver.promote();
Andreas Huberf9334412010-12-15 15:17:42 -08001516
Andreas Huber43c3e6c2011-01-05 12:17:08 -08001517 if (driver == NULL) {
Andreas Huberf9334412010-12-15 15:17:42 -08001518 return;
1519 }
1520
Chong Zhangdcb89b32013-08-06 09:44:47 -07001521 driver->notifyListener(msg, ext1, ext2, in);
Andreas Huberf9334412010-12-15 15:17:42 -08001522}
1523
Lajos Molnar87603c02014-08-20 19:25:30 -07001524void NuPlayer::flushDecoder(
1525 bool audio, bool needShutdown, const sp<AMessage> &newFormat) {
Andreas Huber14f76722013-01-15 09:04:18 -08001526 ALOGV("[%s] flushDecoder needShutdown=%d",
1527 audio ? "audio" : "video", needShutdown);
1528
Lajos Molnar87603c02014-08-20 19:25:30 -07001529 const sp<Decoder> &decoder = getDecoder(audio);
1530 if (decoder == NULL) {
Steve Blockdf64d152012-01-04 20:05:49 +00001531 ALOGI("flushDecoder %s without decoder present",
Andreas Huber6e3d3112011-11-28 12:36:11 -08001532 audio ? "audio" : "video");
Lajos Molnar87603c02014-08-20 19:25:30 -07001533 return;
Andreas Huber6e3d3112011-11-28 12:36:11 -08001534 }
1535
Andreas Huber1aef2112011-01-04 14:01:29 -08001536 // Make sure we don't continue to scan sources until we finish flushing.
1537 ++mScanSourcesGeneration;
Andreas Huber43c3e6c2011-01-05 12:17:08 -08001538 mScanSourcesPending = false;
Andreas Huber1aef2112011-01-04 14:01:29 -08001539
Lajos Molnar87603c02014-08-20 19:25:30 -07001540 decoder->signalFlush(newFormat);
Andreas Huber1aef2112011-01-04 14:01:29 -08001541
1542 FlushStatus newStatus =
1543 needShutdown ? FLUSHING_DECODER_SHUTDOWN : FLUSHING_DECODER;
1544
Andy Hung8d121d42014-10-03 09:53:53 -07001545 mFlushComplete[audio][false /* isDecoder */] = false;
1546 mFlushComplete[audio][true /* isDecoder */] = false;
Andreas Huber1aef2112011-01-04 14:01:29 -08001547 if (audio) {
Wei Jia53904f32014-07-29 10:22:53 -07001548 ALOGE_IF(mFlushingAudio != NONE,
1549 "audio flushDecoder() is called in state %d", mFlushingAudio);
Andreas Huber1aef2112011-01-04 14:01:29 -08001550 mFlushingAudio = newStatus;
Andreas Huber1aef2112011-01-04 14:01:29 -08001551 } else {
Wei Jia53904f32014-07-29 10:22:53 -07001552 ALOGE_IF(mFlushingVideo != NONE,
1553 "video flushDecoder() is called in state %d", mFlushingVideo);
Andreas Huber1aef2112011-01-04 14:01:29 -08001554 mFlushingVideo = newStatus;
Chong Zhangb86e68f2014-08-01 13:46:53 -07001555
1556 if (mCCDecoder != NULL) {
1557 mCCDecoder->flush();
1558 }
Andreas Huber1aef2112011-01-04 14:01:29 -08001559 }
1560}
1561
Lajos Molnar87603c02014-08-20 19:25:30 -07001562void NuPlayer::updateDecoderFormatWithoutFlush(
1563 bool audio, const sp<AMessage> &format) {
1564 ALOGV("[%s] updateDecoderFormatWithoutFlush", audio ? "audio" : "video");
1565
1566 const sp<Decoder> &decoder = getDecoder(audio);
1567 if (decoder == NULL) {
1568 ALOGI("updateDecoderFormatWithoutFlush %s without decoder present",
1569 audio ? "audio" : "video");
1570 return;
1571 }
1572
1573 decoder->signalUpdateFormat(format);
1574}
1575
Chong Zhangced1c2f2014-08-08 15:22:35 -07001576void NuPlayer::queueDecoderShutdown(
1577 bool audio, bool video, const sp<AMessage> &reply) {
1578 ALOGI("queueDecoderShutdown audio=%d, video=%d", audio, video);
Andreas Huber84066782011-08-16 09:34:26 -07001579
Chong Zhangced1c2f2014-08-08 15:22:35 -07001580 mDeferredActions.push_back(
Wei Jiafef808d2014-10-31 17:57:05 -07001581 new FlushDecoderAction(
1582 audio ? FLUSH_CMD_SHUTDOWN : FLUSH_CMD_NONE,
1583 video ? FLUSH_CMD_SHUTDOWN : FLUSH_CMD_NONE));
Andreas Huber84066782011-08-16 09:34:26 -07001584
Chong Zhangced1c2f2014-08-08 15:22:35 -07001585 mDeferredActions.push_back(
1586 new SimpleAction(&NuPlayer::performScanSources));
Andreas Huber84066782011-08-16 09:34:26 -07001587
Chong Zhangced1c2f2014-08-08 15:22:35 -07001588 mDeferredActions.push_back(new PostMessageAction(reply));
1589
1590 processDeferredActions();
Andreas Huber84066782011-08-16 09:34:26 -07001591}
1592
James Dong0d268a32012-08-31 12:18:27 -07001593status_t NuPlayer::setVideoScalingMode(int32_t mode) {
1594 mVideoScalingMode = mode;
Andreas Huber57a339c2012-12-03 11:18:00 -08001595 if (mNativeWindow != NULL) {
James Dong0d268a32012-08-31 12:18:27 -07001596 status_t ret = native_window_set_scaling_mode(
1597 mNativeWindow->getNativeWindow().get(), mVideoScalingMode);
1598 if (ret != OK) {
1599 ALOGE("Failed to set scaling mode (%d): %s",
1600 -ret, strerror(-ret));
1601 return ret;
1602 }
1603 }
1604 return OK;
1605}
1606
Chong Zhangdcb89b32013-08-06 09:44:47 -07001607status_t NuPlayer::getTrackInfo(Parcel* reply) const {
1608 sp<AMessage> msg = new AMessage(kWhatGetTrackInfo, id());
1609 msg->setPointer("reply", reply);
1610
1611 sp<AMessage> response;
1612 status_t err = msg->postAndAwaitResponse(&response);
1613 return err;
1614}
1615
Robert Shih7c4f0d72014-07-09 18:53:31 -07001616status_t NuPlayer::getSelectedTrack(int32_t type, Parcel* reply) const {
1617 sp<AMessage> msg = new AMessage(kWhatGetSelectedTrack, id());
1618 msg->setPointer("reply", reply);
1619 msg->setInt32("type", type);
1620
1621 sp<AMessage> response;
1622 status_t err = msg->postAndAwaitResponse(&response);
1623 if (err == OK && response != NULL) {
1624 CHECK(response->findInt32("err", &err));
1625 }
1626 return err;
1627}
1628
Robert Shih6ffb1fd2014-10-29 16:24:32 -07001629status_t NuPlayer::selectTrack(size_t trackIndex, bool select, int64_t timeUs) {
Chong Zhangdcb89b32013-08-06 09:44:47 -07001630 sp<AMessage> msg = new AMessage(kWhatSelectTrack, id());
1631 msg->setSize("trackIndex", trackIndex);
1632 msg->setInt32("select", select);
Robert Shih6ffb1fd2014-10-29 16:24:32 -07001633 msg->setInt64("timeUs", timeUs);
Chong Zhangdcb89b32013-08-06 09:44:47 -07001634
1635 sp<AMessage> response;
1636 status_t err = msg->postAndAwaitResponse(&response);
1637
Chong Zhang404fced2014-06-11 14:45:31 -07001638 if (err != OK) {
1639 return err;
1640 }
1641
1642 if (!response->findInt32("err", &err)) {
1643 err = OK;
1644 }
1645
Chong Zhangdcb89b32013-08-06 09:44:47 -07001646 return err;
1647}
1648
Ronghua Wua73d9e02014-10-08 15:13:29 -07001649status_t NuPlayer::getCurrentPosition(int64_t *mediaUs) {
1650 sp<Renderer> renderer = mRenderer;
1651 if (renderer == NULL) {
1652 return NO_INIT;
1653 }
1654
1655 return renderer->getCurrentPosition(mediaUs);
1656}
1657
1658void NuPlayer::getStats(int64_t *numFramesTotal, int64_t *numFramesDropped) {
1659 *numFramesTotal = mNumFramesTotal;
1660 *numFramesDropped = mNumFramesDropped;
1661}
1662
Marco Nelissenf0b72b52014-09-16 15:43:44 -07001663sp<MetaData> NuPlayer::getFileMeta() {
1664 return mSource->getFileFormatMeta();
1665}
1666
Andreas Huberb7c8e912012-11-27 15:02:53 -08001667void NuPlayer::schedulePollDuration() {
1668 sp<AMessage> msg = new AMessage(kWhatPollDuration, id());
1669 msg->setInt32("generation", mPollDurationGeneration);
1670 msg->post();
1671}
1672
1673void NuPlayer::cancelPollDuration() {
1674 ++mPollDurationGeneration;
1675}
1676
Andreas Hubera1f8ab02012-11-30 10:53:22 -08001677void NuPlayer::processDeferredActions() {
1678 while (!mDeferredActions.empty()) {
1679 // We won't execute any deferred actions until we're no longer in
1680 // an intermediate state, i.e. one more more decoders are currently
1681 // flushing or shutting down.
1682
Andreas Hubera1f8ab02012-11-30 10:53:22 -08001683 if (mFlushingAudio != NONE || mFlushingVideo != NONE) {
1684 // We're currently flushing, postpone the reset until that's
1685 // completed.
1686
1687 ALOGV("postponing action mFlushingAudio=%d, mFlushingVideo=%d",
1688 mFlushingAudio, mFlushingVideo);
1689
1690 break;
1691 }
1692
1693 sp<Action> action = *mDeferredActions.begin();
1694 mDeferredActions.erase(mDeferredActions.begin());
1695
1696 action->execute(this);
1697 }
1698}
1699
Wei Jiae427abf2014-09-22 15:21:11 -07001700void NuPlayer::performSeek(int64_t seekTimeUs, bool needNotify) {
1701 ALOGV("performSeek seekTimeUs=%lld us (%.2f secs), needNotify(%d)",
Andreas Hubera1f8ab02012-11-30 10:53:22 -08001702 seekTimeUs,
Wei Jiae427abf2014-09-22 15:21:11 -07001703 seekTimeUs / 1E6,
1704 needNotify);
Andreas Hubera1f8ab02012-11-30 10:53:22 -08001705
Andy Hungadf34bf2014-09-03 18:22:22 -07001706 if (mSource == NULL) {
1707 // This happens when reset occurs right before the loop mode
1708 // asynchronously seeks to the start of the stream.
1709 LOG_ALWAYS_FATAL_IF(mAudioDecoder != NULL || mVideoDecoder != NULL,
1710 "mSource is NULL and decoders not NULL audio(%p) video(%p)",
1711 mAudioDecoder.get(), mVideoDecoder.get());
1712 return;
1713 }
Andreas Hubera1f8ab02012-11-30 10:53:22 -08001714 mSource->seekTo(seekTimeUs);
Robert Shihd3b0bbb2014-07-23 15:00:25 -07001715 ++mTimedTextGeneration;
Andreas Hubera1f8ab02012-11-30 10:53:22 -08001716
1717 if (mDriver != NULL) {
1718 sp<NuPlayerDriver> driver = mDriver.promote();
1719 if (driver != NULL) {
Wei Jiae427abf2014-09-22 15:21:11 -07001720 if (needNotify) {
1721 driver->notifySeekComplete();
1722 }
Andreas Hubera1f8ab02012-11-30 10:53:22 -08001723 }
1724 }
1725
1726 // everything's flushed, continue playback.
1727}
1728
Wei Jiafef808d2014-10-31 17:57:05 -07001729void NuPlayer::performDecoderFlush(FlushCommand audio, FlushCommand video) {
1730 ALOGV("performDecoderFlush audio=%d, video=%d", audio, video);
Andreas Hubera1f8ab02012-11-30 10:53:22 -08001731
Wei Jiafef808d2014-10-31 17:57:05 -07001732 if ((audio == FLUSH_CMD_NONE || mAudioDecoder == NULL)
1733 && (video == FLUSH_CMD_NONE || mVideoDecoder == NULL)) {
Andreas Hubera1f8ab02012-11-30 10:53:22 -08001734 return;
1735 }
1736
1737 mTimeDiscontinuityPending = true;
1738
Wei Jiafef808d2014-10-31 17:57:05 -07001739 if (audio != FLUSH_CMD_NONE && mAudioDecoder != NULL) {
1740 flushDecoder(true /* audio */, (audio == FLUSH_CMD_SHUTDOWN));
Andreas Hubera1f8ab02012-11-30 10:53:22 -08001741 }
1742
Wei Jiafef808d2014-10-31 17:57:05 -07001743 if (video != FLUSH_CMD_NONE && mVideoDecoder != NULL) {
1744 flushDecoder(false /* audio */, (video == FLUSH_CMD_SHUTDOWN));
Andreas Hubera1f8ab02012-11-30 10:53:22 -08001745 }
1746}
1747
1748void NuPlayer::performReset() {
1749 ALOGV("performReset");
1750
1751 CHECK(mAudioDecoder == NULL);
1752 CHECK(mVideoDecoder == NULL);
1753
1754 cancelPollDuration();
1755
1756 ++mScanSourcesGeneration;
1757 mScanSourcesPending = false;
1758
Lajos Molnar09524832014-07-17 14:29:51 -07001759 if (mRendererLooper != NULL) {
1760 if (mRenderer != NULL) {
1761 mRendererLooper->unregisterHandler(mRenderer->id());
1762 }
1763 mRendererLooper->stop();
1764 mRendererLooper.clear();
1765 }
Andreas Hubera1f8ab02012-11-30 10:53:22 -08001766 mRenderer.clear();
Wei Jia57568df2014-09-22 10:16:29 -07001767 ++mRendererGeneration;
Andreas Hubera1f8ab02012-11-30 10:53:22 -08001768
1769 if (mSource != NULL) {
1770 mSource->stop();
Andreas Huberb5f25f02013-02-05 10:14:26 -08001771
Andreas Hubera1f8ab02012-11-30 10:53:22 -08001772 mSource.clear();
1773 }
1774
1775 if (mDriver != NULL) {
1776 sp<NuPlayerDriver> driver = mDriver.promote();
1777 if (driver != NULL) {
1778 driver->notifyResetComplete();
1779 }
1780 }
Andreas Huber57a339c2012-12-03 11:18:00 -08001781
1782 mStarted = false;
Andreas Hubera1f8ab02012-11-30 10:53:22 -08001783}
1784
1785void NuPlayer::performScanSources() {
1786 ALOGV("performScanSources");
1787
Andreas Huber57a339c2012-12-03 11:18:00 -08001788 if (!mStarted) {
1789 return;
1790 }
1791
Andreas Hubera1f8ab02012-11-30 10:53:22 -08001792 if (mAudioDecoder == NULL || mVideoDecoder == NULL) {
1793 postScanSources();
1794 }
1795}
1796
Andreas Huber57a339c2012-12-03 11:18:00 -08001797void NuPlayer::performSetSurface(const sp<NativeWindowWrapper> &wrapper) {
1798 ALOGV("performSetSurface");
1799
1800 mNativeWindow = wrapper;
1801
1802 // XXX - ignore error from setVideoScalingMode for now
1803 setVideoScalingMode(mVideoScalingMode);
Chong Zhang13d6faa2014-08-22 15:35:28 -07001804
1805 if (mDriver != NULL) {
1806 sp<NuPlayerDriver> driver = mDriver.promote();
1807 if (driver != NULL) {
1808 driver->notifySetSurfaceComplete();
1809 }
1810 }
Andreas Huber57a339c2012-12-03 11:18:00 -08001811}
1812
Andreas Huber9575c962013-02-05 13:59:56 -08001813void NuPlayer::onSourceNotify(const sp<AMessage> &msg) {
1814 int32_t what;
1815 CHECK(msg->findInt32("what", &what));
1816
1817 switch (what) {
1818 case Source::kWhatPrepared:
1819 {
Andreas Huberb5f28d42013-04-25 15:11:19 -07001820 if (mSource == NULL) {
1821 // This is a stale notification from a source that was
1822 // asynchronously preparing when the client called reset().
1823 // We handled the reset, the source is gone.
1824 break;
1825 }
1826
Andreas Huberec0c5972013-02-05 14:47:13 -08001827 int32_t err;
1828 CHECK(msg->findInt32("err", &err));
1829
Andreas Huber9575c962013-02-05 13:59:56 -08001830 sp<NuPlayerDriver> driver = mDriver.promote();
1831 if (driver != NULL) {
Marco Nelissendd114d12014-05-28 15:23:14 -07001832 // notify duration first, so that it's definitely set when
1833 // the app received the "prepare complete" callback.
1834 int64_t durationUs;
1835 if (mSource->getDuration(&durationUs) == OK) {
1836 driver->notifyDuration(durationUs);
1837 }
Andreas Huberec0c5972013-02-05 14:47:13 -08001838 driver->notifyPrepareCompleted(err);
Andreas Huber9575c962013-02-05 13:59:56 -08001839 }
Andreas Huber99759402013-04-01 14:28:31 -07001840
Andreas Huber9575c962013-02-05 13:59:56 -08001841 break;
1842 }
1843
1844 case Source::kWhatFlagsChanged:
1845 {
1846 uint32_t flags;
1847 CHECK(msg->findInt32("flags", (int32_t *)&flags));
1848
Chong Zhang4b7069d2013-09-11 12:52:43 -07001849 sp<NuPlayerDriver> driver = mDriver.promote();
1850 if (driver != NULL) {
1851 driver->notifyFlagsChanged(flags);
1852 }
1853
Andreas Huber9575c962013-02-05 13:59:56 -08001854 if ((mSourceFlags & Source::FLAG_DYNAMIC_DURATION)
1855 && (!(flags & Source::FLAG_DYNAMIC_DURATION))) {
1856 cancelPollDuration();
1857 } else if (!(mSourceFlags & Source::FLAG_DYNAMIC_DURATION)
1858 && (flags & Source::FLAG_DYNAMIC_DURATION)
1859 && (mAudioDecoder != NULL || mVideoDecoder != NULL)) {
1860 schedulePollDuration();
1861 }
1862
1863 mSourceFlags = flags;
1864 break;
1865 }
1866
1867 case Source::kWhatVideoSizeChanged:
1868 {
Chong Zhangced1c2f2014-08-08 15:22:35 -07001869 sp<AMessage> format;
1870 CHECK(msg->findMessage("format", &format));
Andreas Huber9575c962013-02-05 13:59:56 -08001871
Chong Zhangced1c2f2014-08-08 15:22:35 -07001872 updateVideoSize(format);
Andreas Huber9575c962013-02-05 13:59:56 -08001873 break;
1874 }
1875
Chong Zhang2a3cc9a2014-08-21 17:48:26 -07001876 case Source::kWhatBufferingUpdate:
1877 {
1878 int32_t percentage;
1879 CHECK(msg->findInt32("percentage", &percentage));
1880
1881 notifyListener(MEDIA_BUFFERING_UPDATE, percentage, 0);
1882 break;
1883 }
1884
Roger Jönssonb50e83e2013-01-21 16:26:41 +01001885 case Source::kWhatBufferingStart:
1886 {
1887 notifyListener(MEDIA_INFO, MEDIA_INFO_BUFFERING_START, 0);
1888 break;
1889 }
1890
1891 case Source::kWhatBufferingEnd:
1892 {
1893 notifyListener(MEDIA_INFO, MEDIA_INFO_BUFFERING_END, 0);
1894 break;
1895 }
1896
Chong Zhangdcb89b32013-08-06 09:44:47 -07001897 case Source::kWhatSubtitleData:
1898 {
1899 sp<ABuffer> buffer;
1900 CHECK(msg->findBuffer("buffer", &buffer));
1901
Chong Zhang404fced2014-06-11 14:45:31 -07001902 sendSubtitleData(buffer, 0 /* baseIndex */);
Chong Zhangdcb89b32013-08-06 09:44:47 -07001903 break;
1904 }
1905
Robert Shihd3b0bbb2014-07-23 15:00:25 -07001906 case Source::kWhatTimedTextData:
1907 {
1908 int32_t generation;
1909 if (msg->findInt32("generation", &generation)
1910 && generation != mTimedTextGeneration) {
1911 break;
1912 }
1913
1914 sp<ABuffer> buffer;
1915 CHECK(msg->findBuffer("buffer", &buffer));
1916
1917 sp<NuPlayerDriver> driver = mDriver.promote();
1918 if (driver == NULL) {
1919 break;
1920 }
1921
1922 int posMs;
1923 int64_t timeUs, posUs;
1924 driver->getCurrentPosition(&posMs);
1925 posUs = posMs * 1000;
1926 CHECK(buffer->meta()->findInt64("timeUs", &timeUs));
1927
1928 if (posUs < timeUs) {
1929 if (!msg->findInt32("generation", &generation)) {
1930 msg->setInt32("generation", mTimedTextGeneration);
1931 }
1932 msg->post(timeUs - posUs);
1933 } else {
1934 sendTimedTextData(buffer);
1935 }
1936 break;
1937 }
1938
Andreas Huber14f76722013-01-15 09:04:18 -08001939 case Source::kWhatQueueDecoderShutdown:
1940 {
1941 int32_t audio, video;
1942 CHECK(msg->findInt32("audio", &audio));
1943 CHECK(msg->findInt32("video", &video));
1944
1945 sp<AMessage> reply;
1946 CHECK(msg->findMessage("reply", &reply));
1947
1948 queueDecoderShutdown(audio, video, reply);
1949 break;
1950 }
1951
Ronghua Wu80276872014-08-28 15:50:29 -07001952 case Source::kWhatDrmNoLicense:
1953 {
1954 notifyListener(MEDIA_ERROR, MEDIA_ERROR_UNKNOWN, ERROR_DRM_NO_LICENSE);
1955 break;
1956 }
1957
Andreas Huber9575c962013-02-05 13:59:56 -08001958 default:
1959 TRESPASS();
1960 }
1961}
1962
Chong Zhanga7fa1d92014-06-11 14:49:23 -07001963void NuPlayer::onClosedCaptionNotify(const sp<AMessage> &msg) {
1964 int32_t what;
1965 CHECK(msg->findInt32("what", &what));
1966
1967 switch (what) {
1968 case NuPlayer::CCDecoder::kWhatClosedCaptionData:
1969 {
1970 sp<ABuffer> buffer;
1971 CHECK(msg->findBuffer("buffer", &buffer));
1972
1973 size_t inbandTracks = 0;
1974 if (mSource != NULL) {
1975 inbandTracks = mSource->getTrackCount();
1976 }
1977
1978 sendSubtitleData(buffer, inbandTracks);
1979 break;
1980 }
1981
1982 case NuPlayer::CCDecoder::kWhatTrackAdded:
1983 {
1984 notifyListener(MEDIA_INFO, MEDIA_INFO_METADATA_UPDATE, 0);
1985
1986 break;
1987 }
1988
1989 default:
1990 TRESPASS();
1991 }
1992
1993
1994}
1995
Chong Zhang404fced2014-06-11 14:45:31 -07001996void NuPlayer::sendSubtitleData(const sp<ABuffer> &buffer, int32_t baseIndex) {
1997 int32_t trackIndex;
1998 int64_t timeUs, durationUs;
1999 CHECK(buffer->meta()->findInt32("trackIndex", &trackIndex));
2000 CHECK(buffer->meta()->findInt64("timeUs", &timeUs));
2001 CHECK(buffer->meta()->findInt64("durationUs", &durationUs));
2002
2003 Parcel in;
2004 in.writeInt32(trackIndex + baseIndex);
2005 in.writeInt64(timeUs);
2006 in.writeInt64(durationUs);
2007 in.writeInt32(buffer->size());
2008 in.writeInt32(buffer->size());
2009 in.write(buffer->data(), buffer->size());
2010
2011 notifyListener(MEDIA_SUBTITLE_DATA, 0, 0, &in);
2012}
Robert Shihd3b0bbb2014-07-23 15:00:25 -07002013
2014void NuPlayer::sendTimedTextData(const sp<ABuffer> &buffer) {
2015 const void *data;
2016 size_t size = 0;
2017 int64_t timeUs;
2018 int32_t flag = TextDescriptions::LOCAL_DESCRIPTIONS;
2019
2020 AString mime;
2021 CHECK(buffer->meta()->findString("mime", &mime));
2022 CHECK(strcasecmp(mime.c_str(), MEDIA_MIMETYPE_TEXT_3GPP) == 0);
2023
2024 data = buffer->data();
2025 size = buffer->size();
2026
2027 Parcel parcel;
2028 if (size > 0) {
2029 CHECK(buffer->meta()->findInt64("timeUs", &timeUs));
2030 flag |= TextDescriptions::IN_BAND_TEXT_3GPP;
2031 TextDescriptions::getParcelOfDescriptions(
2032 (const uint8_t *)data, size, flag, timeUs / 1000, &parcel);
2033 }
2034
2035 if ((parcel.dataSize() > 0)) {
2036 notifyListener(MEDIA_TIMED_TEXT, 0, 0, &parcel);
2037 } else { // send an empty timed text
2038 notifyListener(MEDIA_TIMED_TEXT, 0, 0);
2039 }
2040}
Andreas Huberb5f25f02013-02-05 10:14:26 -08002041////////////////////////////////////////////////////////////////////////////////
2042
Chong Zhangced1c2f2014-08-08 15:22:35 -07002043sp<AMessage> NuPlayer::Source::getFormat(bool audio) {
2044 sp<MetaData> meta = getFormatMeta(audio);
2045
2046 if (meta == NULL) {
2047 return NULL;
2048 }
2049
2050 sp<AMessage> msg = new AMessage;
2051
2052 if(convertMetaDataToMessage(meta, &msg) == OK) {
2053 return msg;
2054 }
2055 return NULL;
2056}
2057
Andreas Huber9575c962013-02-05 13:59:56 -08002058void NuPlayer::Source::notifyFlagsChanged(uint32_t flags) {
2059 sp<AMessage> notify = dupNotify();
2060 notify->setInt32("what", kWhatFlagsChanged);
2061 notify->setInt32("flags", flags);
2062 notify->post();
2063}
2064
Chong Zhangced1c2f2014-08-08 15:22:35 -07002065void NuPlayer::Source::notifyVideoSizeChanged(const sp<AMessage> &format) {
Andreas Huber9575c962013-02-05 13:59:56 -08002066 sp<AMessage> notify = dupNotify();
2067 notify->setInt32("what", kWhatVideoSizeChanged);
Chong Zhangced1c2f2014-08-08 15:22:35 -07002068 notify->setMessage("format", format);
Andreas Huber9575c962013-02-05 13:59:56 -08002069 notify->post();
2070}
2071
Andreas Huberec0c5972013-02-05 14:47:13 -08002072void NuPlayer::Source::notifyPrepared(status_t err) {
Andreas Huber9575c962013-02-05 13:59:56 -08002073 sp<AMessage> notify = dupNotify();
2074 notify->setInt32("what", kWhatPrepared);
Andreas Huberec0c5972013-02-05 14:47:13 -08002075 notify->setInt32("err", err);
Andreas Huber9575c962013-02-05 13:59:56 -08002076 notify->post();
2077}
2078
Andreas Huber84333e02014-02-07 15:36:10 -08002079void NuPlayer::Source::onMessageReceived(const sp<AMessage> & /* msg */) {
Andreas Huberb5f25f02013-02-05 10:14:26 -08002080 TRESPASS();
2081}
2082
Andreas Huberf9334412010-12-15 15:17:42 -08002083} // namespace android