blob: 4f88f026327ca276136414eb55efdc781df600c6 [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
607 if (mAudioSink != NULL) {
Andy Hung282a7e32014-08-14 15:56:34 -0700608 if (mOffloadAudio) {
609 // open audio sink early under offload mode.
610 sp<AMessage> format = mSource->getFormat(true /*audio*/);
611 openAudioSink(format, true /*offloadOnly*/);
612 }
Andreas Huber5bc087c2010-12-23 10:27:40 -0800613 instantiateDecoder(true, &mAudioDecoder);
Andreas Huberf9334412010-12-15 15:17:42 -0800614 }
615
Andreas Huberb7c8e912012-11-27 15:02:53 -0800616 if (!mHadAnySourcesBefore
617 && (mAudioDecoder != NULL || mVideoDecoder != NULL)) {
618 // This is the first time we've found anything playable.
619
Andreas Huber9575c962013-02-05 13:59:56 -0800620 if (mSourceFlags & Source::FLAG_DYNAMIC_DURATION) {
Andreas Huberb7c8e912012-11-27 15:02:53 -0800621 schedulePollDuration();
622 }
623 }
624
Andreas Hubereac68ba2011-09-27 12:12:25 -0700625 status_t err;
626 if ((err = mSource->feedMoreTSData()) != OK) {
Andreas Huber1aef2112011-01-04 14:01:29 -0800627 if (mAudioDecoder == NULL && mVideoDecoder == NULL) {
628 // We're not currently decoding anything (no audio or
629 // video tracks found) and we just ran out of input data.
Andreas Hubereac68ba2011-09-27 12:12:25 -0700630
631 if (err == ERROR_END_OF_STREAM) {
632 notifyListener(MEDIA_PLAYBACK_COMPLETE, 0, 0);
633 } else {
634 notifyListener(MEDIA_ERROR, MEDIA_ERROR_UNKNOWN, err);
635 }
Andreas Huber1aef2112011-01-04 14:01:29 -0800636 }
Andreas Huberf9334412010-12-15 15:17:42 -0800637 break;
638 }
639
Andreas Huberfbe9d812012-08-31 14:05:27 -0700640 if ((mAudioDecoder == NULL && mAudioSink != NULL)
641 || (mVideoDecoder == NULL && mNativeWindow != NULL)) {
Andreas Huberf9334412010-12-15 15:17:42 -0800642 msg->post(100000ll);
Andreas Huber5bc087c2010-12-23 10:27:40 -0800643 mScanSourcesPending = true;
Andreas Huberf9334412010-12-15 15:17:42 -0800644 }
645 break;
646 }
647
648 case kWhatVideoNotify:
649 case kWhatAudioNotify:
650 {
651 bool audio = msg->what() == kWhatAudioNotify;
652
Wei Jia88703c32014-08-06 11:24:07 -0700653 int32_t currentDecoderGeneration =
654 (audio? mAudioDecoderGeneration : mVideoDecoderGeneration);
655 int32_t requesterGeneration = currentDecoderGeneration - 1;
656 CHECK(msg->findInt32("generation", &requesterGeneration));
657
658 if (requesterGeneration != currentDecoderGeneration) {
659 ALOGV("got message from old %s decoder, generation(%d:%d)",
660 audio ? "audio" : "video", requesterGeneration,
661 currentDecoderGeneration);
662 sp<AMessage> reply;
663 if (!(msg->findMessage("reply", &reply))) {
664 return;
665 }
666
667 reply->setInt32("err", INFO_DISCONTINUITY);
668 reply->post();
669 return;
670 }
671
Andreas Huberf9334412010-12-15 15:17:42 -0800672 int32_t what;
Lajos Molnar1cd13982014-01-17 15:12:51 -0800673 CHECK(msg->findInt32("what", &what));
Andreas Huberf9334412010-12-15 15:17:42 -0800674
Lajos Molnar1cd13982014-01-17 15:12:51 -0800675 if (what == Decoder::kWhatFillThisBuffer) {
Andreas Huberf9334412010-12-15 15:17:42 -0800676 status_t err = feedDecoderInputData(
Lajos Molnar1cd13982014-01-17 15:12:51 -0800677 audio, msg);
Andreas Huberf9334412010-12-15 15:17:42 -0800678
Andreas Huber5bc087c2010-12-23 10:27:40 -0800679 if (err == -EWOULDBLOCK) {
Andreas Hubereac68ba2011-09-27 12:12:25 -0700680 if (mSource->feedMoreTSData() == OK) {
Phil Burkc5cc2e22014-09-09 20:08:39 -0700681 msg->post(10 * 1000ll);
Andreas Huber5bc087c2010-12-23 10:27:40 -0800682 }
Andreas Huberf9334412010-12-15 15:17:42 -0800683 }
Lajos Molnar1cd13982014-01-17 15:12:51 -0800684 } else if (what == Decoder::kWhatEOS) {
Andreas Huberdc9bacd2011-09-26 10:53:29 -0700685 int32_t err;
Lajos Molnar1cd13982014-01-17 15:12:51 -0800686 CHECK(msg->findInt32("err", &err));
Andreas Huberdc9bacd2011-09-26 10:53:29 -0700687
688 if (err == ERROR_END_OF_STREAM) {
Steve Block3856b092011-10-20 11:56:00 +0100689 ALOGV("got %s decoder EOS", audio ? "audio" : "video");
Andreas Huberdc9bacd2011-09-26 10:53:29 -0700690 } else {
Steve Block3856b092011-10-20 11:56:00 +0100691 ALOGV("got %s decoder EOS w/ error %d",
Andreas Huberdc9bacd2011-09-26 10:53:29 -0700692 audio ? "audio" : "video",
693 err);
694 }
695
696 mRenderer->queueEOS(audio, err);
Lajos Molnar1cd13982014-01-17 15:12:51 -0800697 } else if (what == Decoder::kWhatFlushCompleted) {
Steve Block3856b092011-10-20 11:56:00 +0100698 ALOGV("decoder %s flush completed", audio ? "audio" : "video");
Andreas Huberf9334412010-12-15 15:17:42 -0800699
Andy Hung8d121d42014-10-03 09:53:53 -0700700 handleFlushComplete(audio, true /* isDecoder */);
Andreas Huber3831a062010-12-21 10:22:33 -0800701 finishFlushIfPossible();
Wei Jiac6cfd702014-11-11 16:33:20 -0800702 } else if (what == Decoder::kWhatVideoSizeChanged) {
Lajos Molnar1cd13982014-01-17 15:12:51 -0800703 sp<AMessage> format;
704 CHECK(msg->findMessage("format", &format));
705
Wei Jiac6cfd702014-11-11 16:33:20 -0800706 sp<AMessage> inputFormat =
707 mSource->getFormat(false /* audio */);
Andreas Huber3831a062010-12-21 10:22:33 -0800708
Wei Jiac6cfd702014-11-11 16:33:20 -0800709 updateVideoSize(inputFormat, format);
Lajos Molnar1cd13982014-01-17 15:12:51 -0800710 } else if (what == Decoder::kWhatShutdownCompleted) {
Steve Block3856b092011-10-20 11:56:00 +0100711 ALOGV("%s shutdown completed", audio ? "audio" : "video");
Andreas Huber3831a062010-12-21 10:22:33 -0800712 if (audio) {
713 mAudioDecoder.clear();
Wei Jia57568df2014-09-22 10:16:29 -0700714 ++mAudioDecoderGeneration;
Andreas Huber3831a062010-12-21 10:22:33 -0800715
716 CHECK_EQ((int)mFlushingAudio, (int)SHUTTING_DOWN_DECODER);
717 mFlushingAudio = SHUT_DOWN;
718 } else {
719 mVideoDecoder.clear();
Wei Jia57568df2014-09-22 10:16:29 -0700720 ++mVideoDecoderGeneration;
Andreas Huber3831a062010-12-21 10:22:33 -0800721
722 CHECK_EQ((int)mFlushingVideo, (int)SHUTTING_DOWN_DECODER);
723 mFlushingVideo = SHUT_DOWN;
724 }
725
726 finishFlushIfPossible();
Lajos Molnar1cd13982014-01-17 15:12:51 -0800727 } else if (what == Decoder::kWhatError) {
Chong Zhangf4c0a942014-08-11 15:14:10 -0700728 status_t err;
Andy Hung2abde2c2014-09-30 14:40:32 -0700729 if (!msg->findInt32("err", &err) || err == OK) {
Chong Zhangf4c0a942014-08-11 15:14:10 -0700730 err = UNKNOWN_ERROR;
731 }
Andy Hungcf31f1e2014-09-23 14:59:01 -0700732
Andy Hung2abde2c2014-09-30 14:40:32 -0700733 // Decoder errors can be due to Source (e.g. from streaming),
734 // or from decoding corrupted bitstreams, or from other decoder
735 // MediaCodec operations (e.g. from an ongoing reset or seek).
736 //
737 // We try to gracefully shut down the affected decoder if possible,
738 // rather than trying to force the shutdown with something
739 // similar to performReset(). This method can lead to a hang
740 // if MediaCodec functions block after an error, but they should
741 // typically return INVALID_OPERATION instead of blocking.
742
743 FlushStatus *flushing = audio ? &mFlushingAudio : &mFlushingVideo;
744 ALOGE("received error(%#x) from %s decoder, flushing(%d), now shutting down",
745 err, audio ? "audio" : "video", *flushing);
746
747 switch (*flushing) {
748 case NONE:
749 mDeferredActions.push_back(
Wei Jiafef808d2014-10-31 17:57:05 -0700750 new FlushDecoderAction(
751 audio ? FLUSH_CMD_SHUTDOWN : FLUSH_CMD_NONE,
752 audio ? FLUSH_CMD_NONE : FLUSH_CMD_SHUTDOWN));
Andy Hung2abde2c2014-09-30 14:40:32 -0700753 processDeferredActions();
754 break;
755 case FLUSHING_DECODER:
756 *flushing = FLUSHING_DECODER_SHUTDOWN; // initiate shutdown after flush.
757 break; // Wait for flush to complete.
758 case FLUSHING_DECODER_SHUTDOWN:
759 break; // Wait for flush to complete.
760 case SHUTTING_DOWN_DECODER:
761 break; // Wait for shutdown to complete.
762 case FLUSHED:
763 // Widevine source reads must stop before releasing the video decoder.
764 if (!audio && mSource != NULL && mSourceFlags & Source::FLAG_SECURE) {
765 mSource->stop();
766 }
767 getDecoder(audio)->initiateShutdown(); // In the middle of a seek.
768 *flushing = SHUTTING_DOWN_DECODER; // Shut down.
769 break;
770 case SHUT_DOWN:
771 finishFlushIfPossible(); // Should not occur.
772 break; // Finish anyways.
Marco Nelissen9e2b7912014-08-18 16:13:03 -0700773 }
Andy Hung2abde2c2014-09-30 14:40:32 -0700774 notifyListener(MEDIA_ERROR, MEDIA_ERROR_UNKNOWN, err);
Wei Jiac6cfd702014-11-11 16:33:20 -0800775 } else if (what == Decoder::kWhatRenderBufferTime) {
Lajos Molnar1cd13982014-01-17 15:12:51 -0800776 renderBuffer(audio, msg);
777 } else {
778 ALOGV("Unhandled decoder notification %d '%c%c%c%c'.",
Andreas Hubera1f8ab02012-11-30 10:53:22 -0800779 what,
780 what >> 24,
781 (what >> 16) & 0xff,
782 (what >> 8) & 0xff,
783 what & 0xff);
Andreas Huberf9334412010-12-15 15:17:42 -0800784 }
785
786 break;
787 }
788
789 case kWhatRendererNotify:
790 {
Wei Jia57568df2014-09-22 10:16:29 -0700791 int32_t requesterGeneration = mRendererGeneration - 1;
792 CHECK(msg->findInt32("generation", &requesterGeneration));
793 if (requesterGeneration != mRendererGeneration) {
794 ALOGV("got message from old renderer, generation(%d:%d)",
795 requesterGeneration, mRendererGeneration);
796 return;
797 }
798
Andreas Huberf9334412010-12-15 15:17:42 -0800799 int32_t what;
800 CHECK(msg->findInt32("what", &what));
801
802 if (what == Renderer::kWhatEOS) {
803 int32_t audio;
804 CHECK(msg->findInt32("audio", &audio));
805
Andreas Huberc92fd242011-08-16 13:48:44 -0700806 int32_t finalResult;
807 CHECK(msg->findInt32("finalResult", &finalResult));
808
Andreas Huberf9334412010-12-15 15:17:42 -0800809 if (audio) {
810 mAudioEOS = true;
811 } else {
812 mVideoEOS = true;
813 }
814
Andreas Huberc92fd242011-08-16 13:48:44 -0700815 if (finalResult == ERROR_END_OF_STREAM) {
Steve Block3856b092011-10-20 11:56:00 +0100816 ALOGV("reached %s EOS", audio ? "audio" : "video");
Andreas Huberc92fd242011-08-16 13:48:44 -0700817 } else {
Steve Block29357bc2012-01-06 19:20:56 +0000818 ALOGE("%s track encountered an error (%d)",
Andreas Huberc92fd242011-08-16 13:48:44 -0700819 audio ? "audio" : "video", finalResult);
820
821 notifyListener(
822 MEDIA_ERROR, MEDIA_ERROR_UNKNOWN, finalResult);
823 }
Andreas Huberf9334412010-12-15 15:17:42 -0800824
825 if ((mAudioEOS || mAudioDecoder == NULL)
826 && (mVideoEOS || mVideoDecoder == NULL)) {
827 notifyListener(MEDIA_PLAYBACK_COMPLETE, 0, 0);
828 }
Andreas Huber3fe62152011-09-16 15:09:22 -0700829 } else if (what == Renderer::kWhatFlushComplete) {
Andreas Huberf9334412010-12-15 15:17:42 -0800830 int32_t audio;
831 CHECK(msg->findInt32("audio", &audio));
832
Steve Block3856b092011-10-20 11:56:00 +0100833 ALOGV("renderer %s flush completed.", audio ? "audio" : "video");
Andy Hung8d121d42014-10-03 09:53:53 -0700834 handleFlushComplete(audio, false /* isDecoder */);
835 finishFlushIfPossible();
James Dongf57b4ea2012-07-20 13:38:36 -0700836 } else if (what == Renderer::kWhatVideoRenderingStart) {
837 notifyListener(MEDIA_INFO, MEDIA_INFO_RENDERING_START, 0);
Lajos Molnarcbaffcf2013-08-14 18:30:38 -0700838 } else if (what == Renderer::kWhatMediaRenderingStart) {
839 ALOGV("media rendering started");
840 notifyListener(MEDIA_STARTED, 0, 0);
Wei Jia3a2956d2014-07-22 16:01:33 -0700841 } else if (what == Renderer::kWhatAudioOffloadTearDown) {
842 ALOGV("Tear down audio offload, fall back to s/w path");
843 int64_t positionUs;
844 CHECK(msg->findInt64("positionUs", &positionUs));
Ronghua Wu08529172014-10-02 16:55:52 -0700845 int32_t reason;
846 CHECK(msg->findInt32("reason", &reason));
Andy Hung282a7e32014-08-14 15:56:34 -0700847 closeAudioSink();
Wei Jia3a2956d2014-07-22 16:01:33 -0700848 mAudioDecoder.clear();
Wei Jia57568df2014-09-22 10:16:29 -0700849 ++mAudioDecoderGeneration;
Wei Jia3a2956d2014-07-22 16:01:33 -0700850 mRenderer->flush(true /* audio */);
851 if (mVideoDecoder != NULL) {
852 mRenderer->flush(false /* audio */);
853 }
854 mRenderer->signalDisableOffloadAudio();
855 mOffloadAudio = false;
856
Wei Jiae427abf2014-09-22 15:21:11 -0700857 performSeek(positionUs, false /* needNotify */);
Ronghua Wu08529172014-10-02 16:55:52 -0700858 if (reason == Renderer::kDueToError) {
859 instantiateDecoder(true /* audio */, &mAudioDecoder);
860 }
Andreas Huberf9334412010-12-15 15:17:42 -0800861 }
862 break;
863 }
864
865 case kWhatMoreDataQueued:
866 {
867 break;
868 }
869
Andreas Huber1aef2112011-01-04 14:01:29 -0800870 case kWhatReset:
871 {
Steve Block3856b092011-10-20 11:56:00 +0100872 ALOGV("kWhatReset");
Andreas Huber1aef2112011-01-04 14:01:29 -0800873
Andreas Hubera1f8ab02012-11-30 10:53:22 -0800874 mDeferredActions.push_back(
Wei Jiafef808d2014-10-31 17:57:05 -0700875 new FlushDecoderAction(
876 FLUSH_CMD_SHUTDOWN /* audio */,
877 FLUSH_CMD_SHUTDOWN /* video */));
Andreas Huberb7c8e912012-11-27 15:02:53 -0800878
Andreas Hubera1f8ab02012-11-30 10:53:22 -0800879 mDeferredActions.push_back(
880 new SimpleAction(&NuPlayer::performReset));
Andreas Huberb58ce9f2011-11-28 16:27:35 -0800881
Andreas Hubera1f8ab02012-11-30 10:53:22 -0800882 processDeferredActions();
Andreas Huber1aef2112011-01-04 14:01:29 -0800883 break;
884 }
885
Andreas Huber43c3e6c2011-01-05 12:17:08 -0800886 case kWhatSeek:
887 {
888 int64_t seekTimeUs;
Wei Jiae427abf2014-09-22 15:21:11 -0700889 int32_t needNotify;
Andreas Huber43c3e6c2011-01-05 12:17:08 -0800890 CHECK(msg->findInt64("seekTimeUs", &seekTimeUs));
Wei Jiae427abf2014-09-22 15:21:11 -0700891 CHECK(msg->findInt32("needNotify", &needNotify));
Andreas Huber43c3e6c2011-01-05 12:17:08 -0800892
Wei Jiae427abf2014-09-22 15:21:11 -0700893 ALOGV("kWhatSeek seekTimeUs=%lld us, needNotify=%d",
894 seekTimeUs, needNotify);
Andreas Huber43c3e6c2011-01-05 12:17:08 -0800895
Andreas Hubera1f8ab02012-11-30 10:53:22 -0800896 mDeferredActions.push_back(
Wei Jiafef808d2014-10-31 17:57:05 -0700897 new FlushDecoderAction(FLUSH_CMD_FLUSH /* audio */,
898 FLUSH_CMD_FLUSH /* video */));
Andreas Huber43c3e6c2011-01-05 12:17:08 -0800899
Wei Jiae427abf2014-09-22 15:21:11 -0700900 mDeferredActions.push_back(
901 new SeekAction(seekTimeUs, needNotify));
Andreas Huber43c3e6c2011-01-05 12:17:08 -0800902
Andreas Hubera1f8ab02012-11-30 10:53:22 -0800903 processDeferredActions();
Andreas Huber43c3e6c2011-01-05 12:17:08 -0800904 break;
905 }
906
Andreas Huberb4082222011-01-20 15:23:04 -0800907 case kWhatPause:
908 {
Wei Jia7e9f7f72014-09-23 10:55:35 -0700909 if (mSource != NULL) {
910 mSource->pause();
911 } else {
912 ALOGW("pause called when source is gone or not set");
913 }
914 if (mRenderer != NULL) {
915 mRenderer->pause();
916 } else {
917 ALOGW("pause called when renderer is gone or not set");
918 }
Andreas Huberb4082222011-01-20 15:23:04 -0800919 break;
920 }
921
Andreas Huberb5f25f02013-02-05 10:14:26 -0800922 case kWhatSourceNotify:
923 {
Andreas Huber9575c962013-02-05 13:59:56 -0800924 onSourceNotify(msg);
Andreas Huberb5f25f02013-02-05 10:14:26 -0800925 break;
926 }
927
Chong Zhanga7fa1d92014-06-11 14:49:23 -0700928 case kWhatClosedCaptionNotify:
929 {
930 onClosedCaptionNotify(msg);
931 break;
932 }
933
Andreas Huberf9334412010-12-15 15:17:42 -0800934 default:
935 TRESPASS();
936 break;
937 }
938}
939
Wei Jia94211742014-10-28 17:09:06 -0700940void NuPlayer::onResume() {
941 if (mSource != NULL) {
942 mSource->resume();
943 } else {
944 ALOGW("resume called when source is gone or not set");
945 }
946 // |mAudioDecoder| may have been released due to the pause timeout, so re-create it if
947 // needed.
948 if (audioDecoderStillNeeded() && mAudioDecoder == NULL) {
949 instantiateDecoder(true /* audio */, &mAudioDecoder);
950 }
951 if (mRenderer != NULL) {
952 mRenderer->resume();
953 } else {
954 ALOGW("resume called when renderer is gone or not set");
955 }
956}
957
958void NuPlayer::onStart() {
959 mVideoIsAVC = false;
960 mOffloadAudio = false;
961 mAudioEOS = false;
962 mVideoEOS = false;
Wei Jia94211742014-10-28 17:09:06 -0700963 mNumFramesTotal = 0;
964 mNumFramesDropped = 0;
965 mStarted = true;
966
967 /* instantiate decoders now for secure playback */
968 if (mSourceFlags & Source::FLAG_SECURE) {
969 if (mNativeWindow != NULL) {
970 instantiateDecoder(false, &mVideoDecoder);
971 }
972
973 if (mAudioSink != NULL) {
974 instantiateDecoder(true, &mAudioDecoder);
975 }
976 }
977
978 mSource->start();
979
980 uint32_t flags = 0;
981
982 if (mSource->isRealTime()) {
983 flags |= Renderer::FLAG_REAL_TIME;
984 }
985
986 sp<MetaData> audioMeta = mSource->getFormatMeta(true /* audio */);
987 audio_stream_type_t streamType = AUDIO_STREAM_MUSIC;
988 if (mAudioSink != NULL) {
989 streamType = mAudioSink->getAudioStreamType();
990 }
991
992 sp<AMessage> videoFormat = mSource->getFormat(false /* audio */);
993
994 mOffloadAudio =
995 canOffloadStream(audioMeta, (videoFormat != NULL),
996 true /* is_streaming */, streamType);
997 if (mOffloadAudio) {
998 flags |= Renderer::FLAG_OFFLOAD_AUDIO;
999 }
1000
1001 sp<AMessage> notify = new AMessage(kWhatRendererNotify, id());
1002 ++mRendererGeneration;
1003 notify->setInt32("generation", mRendererGeneration);
1004 mRenderer = new Renderer(mAudioSink, notify, flags);
1005
1006 mRendererLooper = new ALooper;
1007 mRendererLooper->setName("NuPlayerRenderer");
1008 mRendererLooper->start(false, false, ANDROID_PRIORITY_AUDIO);
1009 mRendererLooper->registerHandler(mRenderer);
1010
1011 sp<MetaData> meta = getFileMeta();
1012 int32_t rate;
1013 if (meta != NULL
1014 && meta->findInt32(kKeyFrameRate, &rate) && rate > 0) {
1015 mRenderer->setVideoFrameRate(rate);
1016 }
1017
Wei Jiac6cfd702014-11-11 16:33:20 -08001018 if (mVideoDecoder != NULL) {
1019 mVideoDecoder->setRenderer(mRenderer);
1020 }
1021 if (mAudioDecoder != NULL) {
1022 mAudioDecoder->setRenderer(mRenderer);
1023 }
1024
Wei Jia94211742014-10-28 17:09:06 -07001025 postScanSources();
1026}
1027
Ronghua Wud7988b12014-10-03 15:19:10 -07001028bool NuPlayer::audioDecoderStillNeeded() {
1029 // Audio decoder is no longer needed if it's in shut/shutting down status.
1030 return ((mFlushingAudio != SHUT_DOWN) && (mFlushingAudio != SHUTTING_DOWN_DECODER));
1031}
1032
Andy Hung8d121d42014-10-03 09:53:53 -07001033void NuPlayer::handleFlushComplete(bool audio, bool isDecoder) {
1034 // We wait for both the decoder flush and the renderer flush to complete
1035 // before entering either the FLUSHED or the SHUTTING_DOWN_DECODER state.
1036
1037 mFlushComplete[audio][isDecoder] = true;
1038 if (!mFlushComplete[audio][!isDecoder]) {
1039 return;
1040 }
1041
1042 FlushStatus *state = audio ? &mFlushingAudio : &mFlushingVideo;
1043 switch (*state) {
1044 case FLUSHING_DECODER:
1045 {
1046 *state = FLUSHED;
Andy Hung8d121d42014-10-03 09:53:53 -07001047 break;
1048 }
1049
1050 case FLUSHING_DECODER_SHUTDOWN:
1051 {
1052 *state = SHUTTING_DOWN_DECODER;
1053
1054 ALOGV("initiating %s decoder shutdown", audio ? "audio" : "video");
1055 if (!audio) {
Andy Hung8d121d42014-10-03 09:53:53 -07001056 // Widevine source reads must stop before releasing the video decoder.
1057 if (mSource != NULL && mSourceFlags & Source::FLAG_SECURE) {
1058 mSource->stop();
1059 }
1060 }
1061 getDecoder(audio)->initiateShutdown();
1062 break;
1063 }
1064
1065 default:
1066 // decoder flush completes only occur in a flushing state.
1067 LOG_ALWAYS_FATAL_IF(isDecoder, "decoder flush in invalid state %d", *state);
1068 break;
1069 }
1070}
1071
Andreas Huber3831a062010-12-21 10:22:33 -08001072void NuPlayer::finishFlushIfPossible() {
Wei Jia53904f32014-07-29 10:22:53 -07001073 if (mFlushingAudio != NONE && mFlushingAudio != FLUSHED
1074 && mFlushingAudio != SHUT_DOWN) {
Andreas Huber3831a062010-12-21 10:22:33 -08001075 return;
1076 }
1077
Wei Jia53904f32014-07-29 10:22:53 -07001078 if (mFlushingVideo != NONE && mFlushingVideo != FLUSHED
1079 && mFlushingVideo != SHUT_DOWN) {
Andreas Huber3831a062010-12-21 10:22:33 -08001080 return;
1081 }
1082
Steve Block3856b092011-10-20 11:56:00 +01001083 ALOGV("both audio and video are flushed now.");
Andreas Huber3831a062010-12-21 10:22:33 -08001084
Phil Burk9f526492014-09-03 15:04:12 -07001085 mPendingAudioAccessUnit.clear();
Phil Burkc5cc2e22014-09-09 20:08:39 -07001086 mAggregateBuffer.clear();
Phil Burk9f526492014-09-03 15:04:12 -07001087
Andreas Huber6e3d3112011-11-28 12:36:11 -08001088 if (mTimeDiscontinuityPending) {
1089 mRenderer->signalTimeDiscontinuity();
1090 mTimeDiscontinuityPending = false;
1091 }
Andreas Huber3831a062010-12-21 10:22:33 -08001092
Wei Jia53904f32014-07-29 10:22:53 -07001093 if (mAudioDecoder != NULL && mFlushingAudio == FLUSHED) {
Andreas Huber3831a062010-12-21 10:22:33 -08001094 mAudioDecoder->signalResume();
1095 }
1096
Wei Jia53904f32014-07-29 10:22:53 -07001097 if (mVideoDecoder != NULL && mFlushingVideo == FLUSHED) {
Andreas Huber3831a062010-12-21 10:22:33 -08001098 mVideoDecoder->signalResume();
1099 }
1100
1101 mFlushingAudio = NONE;
1102 mFlushingVideo = NONE;
Andreas Huber3831a062010-12-21 10:22:33 -08001103
Andy Hung8d121d42014-10-03 09:53:53 -07001104 clearFlushComplete();
1105
Andreas Hubera1f8ab02012-11-30 10:53:22 -08001106 processDeferredActions();
Andreas Huber1aef2112011-01-04 14:01:29 -08001107}
1108
1109void NuPlayer::postScanSources() {
1110 if (mScanSourcesPending) {
1111 return;
1112 }
1113
1114 sp<AMessage> msg = new AMessage(kWhatScanSources, id());
1115 msg->setInt32("generation", mScanSourcesGeneration);
1116 msg->post();
1117
1118 mScanSourcesPending = true;
1119}
1120
Andy Hung282a7e32014-08-14 15:56:34 -07001121void NuPlayer::openAudioSink(const sp<AMessage> &format, bool offloadOnly) {
Andy Hung282a7e32014-08-14 15:56:34 -07001122 uint32_t flags;
1123 int64_t durationUs;
Chong Zhang3b9eb1f2014-10-15 17:05:08 -07001124 bool hasVideo = (mVideoDecoder != NULL);
Andy Hung282a7e32014-08-14 15:56:34 -07001125 // FIXME: we should handle the case where the video decoder
1126 // is created after we receive the format change indication.
1127 // Current code will just make that we select deep buffer
1128 // with video which should not be a problem as it should
1129 // not prevent from keeping A/V sync.
Eric Laurentd88c3ca2014-10-28 10:52:11 -07001130 if (!hasVideo &&
Andy Hung282a7e32014-08-14 15:56:34 -07001131 mSource->getDuration(&durationUs) == OK &&
1132 durationUs
1133 > AUDIO_SINK_MIN_DEEP_BUFFER_DURATION_US) {
1134 flags = AUDIO_OUTPUT_FLAG_DEEP_BUFFER;
1135 } else {
1136 flags = AUDIO_OUTPUT_FLAG_NONE;
1137 }
1138
Chong Zhang3b9eb1f2014-10-15 17:05:08 -07001139 mOffloadAudio = mRenderer->openAudioSink(
1140 format, offloadOnly, hasVideo, flags);
1141
Andy Hung282a7e32014-08-14 15:56:34 -07001142 if (mOffloadAudio) {
Chong Zhang3b9eb1f2014-10-15 17:05:08 -07001143 sp<MetaData> audioMeta =
1144 mSource->getFormatMeta(true /* audio */);
1145 sendMetaDataToHal(mAudioSink, audioMeta);
Andy Hung282a7e32014-08-14 15:56:34 -07001146 }
1147}
1148
1149void NuPlayer::closeAudioSink() {
Chong Zhang3b9eb1f2014-10-15 17:05:08 -07001150 mRenderer->closeAudioSink();
Andy Hung282a7e32014-08-14 15:56:34 -07001151}
1152
Andreas Huber5bc087c2010-12-23 10:27:40 -08001153status_t NuPlayer::instantiateDecoder(bool audio, sp<Decoder> *decoder) {
Andreas Huberf9334412010-12-15 15:17:42 -08001154 if (*decoder != NULL) {
1155 return OK;
1156 }
1157
Andreas Huber84066782011-08-16 09:34:26 -07001158 sp<AMessage> format = mSource->getFormat(audio);
Andreas Huberf9334412010-12-15 15:17:42 -08001159
Andreas Huber84066782011-08-16 09:34:26 -07001160 if (format == NULL) {
Andreas Huberf9334412010-12-15 15:17:42 -08001161 return -EWOULDBLOCK;
1162 }
1163
Andreas Huber3fe62152011-09-16 15:09:22 -07001164 if (!audio) {
Andreas Huber84066782011-08-16 09:34:26 -07001165 AString mime;
1166 CHECK(format->findString("mime", &mime));
1167 mVideoIsAVC = !strcasecmp(MEDIA_MIMETYPE_VIDEO_AVC, mime.c_str());
Chong Zhanga7fa1d92014-06-11 14:49:23 -07001168
1169 sp<AMessage> ccNotify = new AMessage(kWhatClosedCaptionNotify, id());
1170 mCCDecoder = new CCDecoder(ccNotify);
Lajos Molnar09524832014-07-17 14:29:51 -07001171
1172 if (mSourceFlags & Source::FLAG_SECURE) {
1173 format->setInt32("secure", true);
1174 }
Andreas Huber3fe62152011-09-16 15:09:22 -07001175 }
1176
Wei Jiabc2fb722014-07-08 16:37:57 -07001177 if (audio) {
Wei Jia88703c32014-08-06 11:24:07 -07001178 sp<AMessage> notify = new AMessage(kWhatAudioNotify, id());
1179 ++mAudioDecoderGeneration;
1180 notify->setInt32("generation", mAudioDecoderGeneration);
1181
Wei Jiabc2fb722014-07-08 16:37:57 -07001182 if (mOffloadAudio) {
Wei Jiac6cfd702014-11-11 16:33:20 -08001183 *decoder = new DecoderPassThrough(notify, mSource, mRenderer);
Wei Jiabc2fb722014-07-08 16:37:57 -07001184 } else {
Wei Jiac6cfd702014-11-11 16:33:20 -08001185 *decoder = new Decoder(notify, mSource, mRenderer);
Wei Jiabc2fb722014-07-08 16:37:57 -07001186 }
1187 } else {
Wei Jia88703c32014-08-06 11:24:07 -07001188 sp<AMessage> notify = new AMessage(kWhatVideoNotify, id());
1189 ++mVideoDecoderGeneration;
1190 notify->setInt32("generation", mVideoDecoderGeneration);
1191
Wei Jiac6cfd702014-11-11 16:33:20 -08001192 *decoder = new Decoder(notify, mSource, mRenderer, mNativeWindow);
Wei Jiabc2fb722014-07-08 16:37:57 -07001193 }
Lajos Molnar1cd13982014-01-17 15:12:51 -08001194 (*decoder)->init();
Andreas Huber84066782011-08-16 09:34:26 -07001195 (*decoder)->configure(format);
Andreas Huberf9334412010-12-15 15:17:42 -08001196
Lajos Molnar09524832014-07-17 14:29:51 -07001197 // allocate buffers to decrypt widevine source buffers
1198 if (!audio && (mSourceFlags & Source::FLAG_SECURE)) {
1199 Vector<sp<ABuffer> > inputBufs;
1200 CHECK_EQ((*decoder)->getInputBuffers(&inputBufs), (status_t)OK);
1201
1202 Vector<MediaBuffer *> mediaBufs;
1203 for (size_t i = 0; i < inputBufs.size(); i++) {
1204 const sp<ABuffer> &buffer = inputBufs[i];
1205 MediaBuffer *mbuf = new MediaBuffer(buffer->data(), buffer->size());
1206 mediaBufs.push(mbuf);
1207 }
1208
1209 status_t err = mSource->setBuffers(audio, mediaBufs);
1210 if (err != OK) {
1211 for (size_t i = 0; i < mediaBufs.size(); ++i) {
1212 mediaBufs[i]->release();
1213 }
1214 mediaBufs.clear();
1215 ALOGE("Secure source didn't support secure mediaBufs.");
1216 return err;
1217 }
1218 }
Andreas Huberf9334412010-12-15 15:17:42 -08001219 return OK;
1220}
1221
1222status_t NuPlayer::feedDecoderInputData(bool audio, const sp<AMessage> &msg) {
1223 sp<AMessage> reply;
1224 CHECK(msg->findMessage("reply", &reply));
1225
Wei Jia53904f32014-07-29 10:22:53 -07001226 if ((audio && mFlushingAudio != NONE)
Wei Jiaf702d042014-09-09 12:08:47 -07001227 || (!audio && mFlushingVideo != NONE)
1228 || mSource == NULL) {
Wei Jiab189a5b2014-08-07 06:11:39 +00001229 reply->setInt32("err", INFO_DISCONTINUITY);
1230 reply->post();
1231 return OK;
Andreas Huberf9334412010-12-15 15:17:42 -08001232 }
1233
1234 sp<ABuffer> accessUnit;
Andreas Huberf9334412010-12-15 15:17:42 -08001235
Phil Burk9f526492014-09-03 15:04:12 -07001236 // Aggregate smaller buffers into a larger buffer.
1237 // The goal is to reduce power consumption.
Phil Burk33b51b02014-09-17 16:03:47 -07001238 // Note this will not work if the decoder requires one frame per buffer.
1239 bool doBufferAggregation = (audio && mOffloadAudio);
Phil Burk9f526492014-09-03 15:04:12 -07001240 bool needMoreData = false;
Phil Burk9f526492014-09-03 15:04:12 -07001241
Andreas Huber3fe62152011-09-16 15:09:22 -07001242 bool dropAccessUnit;
1243 do {
Phil Burk9f526492014-09-03 15:04:12 -07001244 status_t err;
1245 // Did we save an accessUnit earlier because of a discontinuity?
1246 if (audio && (mPendingAudioAccessUnit != NULL)) {
1247 accessUnit = mPendingAudioAccessUnit;
1248 mPendingAudioAccessUnit.clear();
1249 err = mPendingAudioErr;
1250 ALOGV("feedDecoderInputData() use mPendingAudioAccessUnit");
1251 } else {
1252 err = mSource->dequeueAccessUnit(audio, &accessUnit);
1253 }
Andreas Huber5bc087c2010-12-23 10:27:40 -08001254
Andreas Huber3fe62152011-09-16 15:09:22 -07001255 if (err == -EWOULDBLOCK) {
Phil Burkc5cc2e22014-09-09 20:08:39 -07001256 return err;
Andreas Huber3fe62152011-09-16 15:09:22 -07001257 } else if (err != OK) {
1258 if (err == INFO_DISCONTINUITY) {
Phil Burk33b51b02014-09-17 16:03:47 -07001259 if (doBufferAggregation && (mAggregateBuffer != NULL)) {
Phil Burk9f526492014-09-03 15:04:12 -07001260 // We already have some data so save this for later.
1261 mPendingAudioErr = err;
1262 mPendingAudioAccessUnit = accessUnit;
1263 accessUnit.clear();
1264 ALOGD("feedDecoderInputData() save discontinuity for later");
1265 break;
1266 }
Andreas Huber3fe62152011-09-16 15:09:22 -07001267 int32_t type;
1268 CHECK(accessUnit->meta()->findInt32("discontinuity", &type));
Andreas Huber53df1a42010-12-22 10:03:04 -08001269
Andreas Huber3fe62152011-09-16 15:09:22 -07001270 bool formatChange =
Andreas Huber6e3d3112011-11-28 12:36:11 -08001271 (audio &&
1272 (type & ATSParser::DISCONTINUITY_AUDIO_FORMAT))
1273 || (!audio &&
1274 (type & ATSParser::DISCONTINUITY_VIDEO_FORMAT));
Andreas Huber53df1a42010-12-22 10:03:04 -08001275
Andreas Huber6e3d3112011-11-28 12:36:11 -08001276 bool timeChange = (type & ATSParser::DISCONTINUITY_TIME) != 0;
1277
Steve Blockdf64d152012-01-04 20:05:49 +00001278 ALOGI("%s discontinuity (formatChange=%d, time=%d)",
Andreas Huber6e3d3112011-11-28 12:36:11 -08001279 audio ? "audio" : "video", formatChange, timeChange);
Andreas Huber32f3cef2011-03-02 15:34:46 -08001280
Andreas Huber6e3d3112011-11-28 12:36:11 -08001281 mTimeDiscontinuityPending =
1282 mTimeDiscontinuityPending || timeChange;
1283
Lajos Molnar87603c02014-08-20 19:25:30 -07001284 bool seamlessFormatChange = false;
1285 sp<AMessage> newFormat = mSource->getFormat(audio);
1286 if (formatChange) {
1287 seamlessFormatChange =
1288 getDecoder(audio)->supportsSeamlessFormatChange(newFormat);
1289 // treat seamless format change separately
1290 formatChange = !seamlessFormatChange;
1291 }
1292 bool shutdownOrFlush = formatChange || timeChange;
1293
1294 // We want to queue up scan-sources only once per discontinuity.
1295 // We control this by doing it only if neither audio nor video are
1296 // flushing or shutting down. (After handling 1st discontinuity, one
1297 // of the flushing states will not be NONE.)
1298 // No need to scan sources if this discontinuity does not result
1299 // in a flush or shutdown, as the flushing state will stay NONE.
1300 if (mFlushingAudio == NONE && mFlushingVideo == NONE &&
1301 shutdownOrFlush) {
Robert Shiha2981012014-07-30 17:41:24 -07001302 // And we'll resume scanning sources once we're done
1303 // flushing.
1304 mDeferredActions.push_front(
1305 new SimpleAction(
1306 &NuPlayer::performScanSources));
1307 }
1308
Lajos Molnar87603c02014-08-20 19:25:30 -07001309 if (formatChange /* not seamless */) {
1310 // must change decoder
1311 flushDecoder(audio, /* needShutdown = */ true);
1312 } else if (timeChange) {
1313 // need to flush
1314 flushDecoder(audio, /* needShutdown = */ false, newFormat);
1315 err = OK;
1316 } else if (seamlessFormatChange) {
1317 // reuse existing decoder and don't flush
1318 updateDecoderFormatWithoutFlush(audio, newFormat);
1319 err = OK;
Andreas Huber6e3d3112011-11-28 12:36:11 -08001320 } else {
1321 // This stream is unaffected by the discontinuity
Andreas Huber6e3d3112011-11-28 12:36:11 -08001322 return -EWOULDBLOCK;
1323 }
Andreas Huber32f3cef2011-03-02 15:34:46 -08001324 }
1325
Andreas Huber3fe62152011-09-16 15:09:22 -07001326 reply->setInt32("err", err);
1327 reply->post();
1328 return OK;
Andreas Huberf9334412010-12-15 15:17:42 -08001329 }
1330
Andreas Huber3fe62152011-09-16 15:09:22 -07001331 if (!audio) {
1332 ++mNumFramesTotal;
1333 }
1334
1335 dropAccessUnit = false;
1336 if (!audio
Lajos Molnar09524832014-07-17 14:29:51 -07001337 && !(mSourceFlags & Source::FLAG_SECURE)
Ronghua Wua73d9e02014-10-08 15:13:29 -07001338 && mRenderer->getVideoLateByUs() > 100000ll
Andreas Huber3fe62152011-09-16 15:09:22 -07001339 && mVideoIsAVC
1340 && !IsAVCReferenceFrame(accessUnit)) {
1341 dropAccessUnit = true;
1342 ++mNumFramesDropped;
1343 }
Phil Burk9f526492014-09-03 15:04:12 -07001344
1345 size_t smallSize = accessUnit->size();
1346 needMoreData = false;
Phil Burkc5cc2e22014-09-09 20:08:39 -07001347 if (doBufferAggregation && (mAggregateBuffer == NULL)
Phil Burk9f526492014-09-03 15:04:12 -07001348 // Don't bother if only room for a few small buffers.
Phil Burkc5cc2e22014-09-09 20:08:39 -07001349 && (smallSize < (kAggregateBufferSizeBytes / 3))) {
Phil Burk9f526492014-09-03 15:04:12 -07001350 // Create a larger buffer for combining smaller buffers from the extractor.
Phil Burkc5cc2e22014-09-09 20:08:39 -07001351 mAggregateBuffer = new ABuffer(kAggregateBufferSizeBytes);
1352 mAggregateBuffer->setRange(0, 0); // start empty
Phil Burk9f526492014-09-03 15:04:12 -07001353 }
1354
Phil Burk33b51b02014-09-17 16:03:47 -07001355 if (doBufferAggregation && (mAggregateBuffer != NULL)) {
Phil Burk9f526492014-09-03 15:04:12 -07001356 int64_t timeUs;
Phil Burkc5cc2e22014-09-09 20:08:39 -07001357 int64_t dummy;
Phil Burk9f526492014-09-03 15:04:12 -07001358 bool smallTimestampValid = accessUnit->meta()->findInt64("timeUs", &timeUs);
Phil Burkc5cc2e22014-09-09 20:08:39 -07001359 bool bigTimestampValid = mAggregateBuffer->meta()->findInt64("timeUs", &dummy);
Phil Burk9f526492014-09-03 15:04:12 -07001360 // Will the smaller buffer fit?
Phil Burkc5cc2e22014-09-09 20:08:39 -07001361 size_t bigSize = mAggregateBuffer->size();
1362 size_t roomLeft = mAggregateBuffer->capacity() - bigSize;
Phil Burk9f526492014-09-03 15:04:12 -07001363 // Should we save this small buffer for the next big buffer?
1364 // If the first small buffer did not have a timestamp then save
1365 // any buffer that does have a timestamp until the next big buffer.
1366 if ((smallSize > roomLeft)
Phil Burkc5cc2e22014-09-09 20:08:39 -07001367 || (!bigTimestampValid && (bigSize > 0) && smallTimestampValid)) {
Phil Burk9f526492014-09-03 15:04:12 -07001368 mPendingAudioErr = err;
1369 mPendingAudioAccessUnit = accessUnit;
1370 accessUnit.clear();
1371 } else {
Phil Burkc5cc2e22014-09-09 20:08:39 -07001372 // Grab time from first small buffer if available.
1373 if ((bigSize == 0) && smallTimestampValid) {
1374 mAggregateBuffer->meta()->setInt64("timeUs", timeUs);
1375 }
Phil Burk9f526492014-09-03 15:04:12 -07001376 // Append small buffer to the bigger buffer.
Phil Burkc5cc2e22014-09-09 20:08:39 -07001377 memcpy(mAggregateBuffer->base() + bigSize, accessUnit->data(), smallSize);
Phil Burk9f526492014-09-03 15:04:12 -07001378 bigSize += smallSize;
Phil Burkc5cc2e22014-09-09 20:08:39 -07001379 mAggregateBuffer->setRange(0, bigSize);
Phil Burk9f526492014-09-03 15:04:12 -07001380
Phil Burkc5cc2e22014-09-09 20:08:39 -07001381 // Keep looping until we run out of room in the mAggregateBuffer.
Phil Burk9f526492014-09-03 15:04:12 -07001382 needMoreData = true;
1383
Phil Burkc5cc2e22014-09-09 20:08:39 -07001384 ALOGV("feedDecoderInputData() smallSize = %zu, bigSize = %zu, capacity = %zu",
1385 smallSize, bigSize, mAggregateBuffer->capacity());
Phil Burk9f526492014-09-03 15:04:12 -07001386 }
1387 }
1388 } while (dropAccessUnit || needMoreData);
Andreas Huberf9334412010-12-15 15:17:42 -08001389
Steve Block3856b092011-10-20 11:56:00 +01001390 // ALOGV("returned a valid buffer of %s data", audio ? "audio" : "video");
Andreas Huberf9334412010-12-15 15:17:42 -08001391
1392#if 0
1393 int64_t mediaTimeUs;
1394 CHECK(accessUnit->meta()->findInt64("timeUs", &mediaTimeUs));
Steve Block3856b092011-10-20 11:56:00 +01001395 ALOGV("feeding %s input buffer at media time %.2f secs",
Andreas Huberf9334412010-12-15 15:17:42 -08001396 audio ? "audio" : "video",
1397 mediaTimeUs / 1E6);
1398#endif
1399
Chong Zhanga7fa1d92014-06-11 14:49:23 -07001400 if (!audio) {
1401 mCCDecoder->decode(accessUnit);
1402 }
1403
Phil Burk33b51b02014-09-17 16:03:47 -07001404 if (doBufferAggregation && (mAggregateBuffer != NULL)) {
Phil Burkc5cc2e22014-09-09 20:08:39 -07001405 ALOGV("feedDecoderInputData() reply with aggregated buffer, %zu",
1406 mAggregateBuffer->size());
1407 reply->setBuffer("buffer", mAggregateBuffer);
1408 mAggregateBuffer.clear();
Phil Burk9f526492014-09-03 15:04:12 -07001409 } else {
1410 reply->setBuffer("buffer", accessUnit);
1411 }
1412
Andreas Huberf9334412010-12-15 15:17:42 -08001413 reply->post();
1414
1415 return OK;
1416}
1417
1418void NuPlayer::renderBuffer(bool audio, const sp<AMessage> &msg) {
Steve Block3856b092011-10-20 11:56:00 +01001419 // ALOGV("renderBuffer %s", audio ? "audio" : "video");
Andreas Huberf9334412010-12-15 15:17:42 -08001420
Wei Jia53904f32014-07-29 10:22:53 -07001421 if ((audio && mFlushingAudio != NONE)
1422 || (!audio && mFlushingVideo != NONE)) {
Andreas Huber18ac5402011-08-31 15:04:25 -07001423 // We're currently attempting to flush the decoder, in order
1424 // to complete this, the decoder wants all its buffers back,
1425 // so we don't want any output buffers it sent us (from before
1426 // we initiated the flush) to be stuck in the renderer's queue.
1427
Steve Block3856b092011-10-20 11:56:00 +01001428 ALOGV("we're still flushing the %s decoder, sending its output buffer"
Andreas Huber18ac5402011-08-31 15:04:25 -07001429 " right back.", audio ? "audio" : "video");
1430
Andreas Huber18ac5402011-08-31 15:04:25 -07001431 return;
1432 }
1433
Chong Zhanga7fa1d92014-06-11 14:49:23 -07001434 int64_t mediaTimeUs;
Wei Jiac6cfd702014-11-11 16:33:20 -08001435 CHECK(msg->findInt64("timeUs", &mediaTimeUs));
Andreas Huber32f3cef2011-03-02 15:34:46 -08001436
Chong Zhanga7fa1d92014-06-11 14:49:23 -07001437 if (!audio && mCCDecoder->isSelected()) {
1438 mCCDecoder->display(mediaTimeUs);
1439 }
Andreas Huberf9334412010-12-15 15:17:42 -08001440}
1441
Chong Zhangced1c2f2014-08-08 15:22:35 -07001442void NuPlayer::updateVideoSize(
1443 const sp<AMessage> &inputFormat,
1444 const sp<AMessage> &outputFormat) {
1445 if (inputFormat == NULL) {
1446 ALOGW("Unknown video size, reporting 0x0!");
1447 notifyListener(MEDIA_SET_VIDEO_SIZE, 0, 0);
1448 return;
1449 }
1450
1451 int32_t displayWidth, displayHeight;
1452 int32_t cropLeft, cropTop, cropRight, cropBottom;
1453
1454 if (outputFormat != NULL) {
1455 int32_t width, height;
1456 CHECK(outputFormat->findInt32("width", &width));
1457 CHECK(outputFormat->findInt32("height", &height));
1458
1459 int32_t cropLeft, cropTop, cropRight, cropBottom;
1460 CHECK(outputFormat->findRect(
1461 "crop",
1462 &cropLeft, &cropTop, &cropRight, &cropBottom));
1463
1464 displayWidth = cropRight - cropLeft + 1;
1465 displayHeight = cropBottom - cropTop + 1;
1466
1467 ALOGV("Video output format changed to %d x %d "
1468 "(crop: %d x %d @ (%d, %d))",
1469 width, height,
1470 displayWidth,
1471 displayHeight,
1472 cropLeft, cropTop);
1473 } else {
1474 CHECK(inputFormat->findInt32("width", &displayWidth));
1475 CHECK(inputFormat->findInt32("height", &displayHeight));
1476
1477 ALOGV("Video input format %d x %d", displayWidth, displayHeight);
1478 }
1479
1480 // Take into account sample aspect ratio if necessary:
1481 int32_t sarWidth, sarHeight;
1482 if (inputFormat->findInt32("sar-width", &sarWidth)
1483 && inputFormat->findInt32("sar-height", &sarHeight)) {
1484 ALOGV("Sample aspect ratio %d : %d", sarWidth, sarHeight);
1485
1486 displayWidth = (displayWidth * sarWidth) / sarHeight;
1487
1488 ALOGV("display dimensions %d x %d", displayWidth, displayHeight);
1489 }
1490
1491 int32_t rotationDegrees;
1492 if (!inputFormat->findInt32("rotation-degrees", &rotationDegrees)) {
1493 rotationDegrees = 0;
1494 }
1495
1496 if (rotationDegrees == 90 || rotationDegrees == 270) {
1497 int32_t tmp = displayWidth;
1498 displayWidth = displayHeight;
1499 displayHeight = tmp;
1500 }
1501
1502 notifyListener(
1503 MEDIA_SET_VIDEO_SIZE,
1504 displayWidth,
1505 displayHeight);
1506}
1507
Chong Zhangdcb89b32013-08-06 09:44:47 -07001508void NuPlayer::notifyListener(int msg, int ext1, int ext2, const Parcel *in) {
Andreas Huber43c3e6c2011-01-05 12:17:08 -08001509 if (mDriver == NULL) {
Andreas Huberf9334412010-12-15 15:17:42 -08001510 return;
1511 }
1512
Andreas Huber43c3e6c2011-01-05 12:17:08 -08001513 sp<NuPlayerDriver> driver = mDriver.promote();
Andreas Huberf9334412010-12-15 15:17:42 -08001514
Andreas Huber43c3e6c2011-01-05 12:17:08 -08001515 if (driver == NULL) {
Andreas Huberf9334412010-12-15 15:17:42 -08001516 return;
1517 }
1518
Chong Zhangdcb89b32013-08-06 09:44:47 -07001519 driver->notifyListener(msg, ext1, ext2, in);
Andreas Huberf9334412010-12-15 15:17:42 -08001520}
1521
Lajos Molnar87603c02014-08-20 19:25:30 -07001522void NuPlayer::flushDecoder(
1523 bool audio, bool needShutdown, const sp<AMessage> &newFormat) {
Andreas Huber14f76722013-01-15 09:04:18 -08001524 ALOGV("[%s] flushDecoder needShutdown=%d",
1525 audio ? "audio" : "video", needShutdown);
1526
Lajos Molnar87603c02014-08-20 19:25:30 -07001527 const sp<Decoder> &decoder = getDecoder(audio);
1528 if (decoder == NULL) {
Steve Blockdf64d152012-01-04 20:05:49 +00001529 ALOGI("flushDecoder %s without decoder present",
Andreas Huber6e3d3112011-11-28 12:36:11 -08001530 audio ? "audio" : "video");
Lajos Molnar87603c02014-08-20 19:25:30 -07001531 return;
Andreas Huber6e3d3112011-11-28 12:36:11 -08001532 }
1533
Andreas Huber1aef2112011-01-04 14:01:29 -08001534 // Make sure we don't continue to scan sources until we finish flushing.
1535 ++mScanSourcesGeneration;
Andreas Huber43c3e6c2011-01-05 12:17:08 -08001536 mScanSourcesPending = false;
Andreas Huber1aef2112011-01-04 14:01:29 -08001537
Lajos Molnar87603c02014-08-20 19:25:30 -07001538 decoder->signalFlush(newFormat);
Andreas Huber1aef2112011-01-04 14:01:29 -08001539
1540 FlushStatus newStatus =
1541 needShutdown ? FLUSHING_DECODER_SHUTDOWN : FLUSHING_DECODER;
1542
Andy Hung8d121d42014-10-03 09:53:53 -07001543 mFlushComplete[audio][false /* isDecoder */] = false;
1544 mFlushComplete[audio][true /* isDecoder */] = false;
Andreas Huber1aef2112011-01-04 14:01:29 -08001545 if (audio) {
Wei Jia53904f32014-07-29 10:22:53 -07001546 ALOGE_IF(mFlushingAudio != NONE,
1547 "audio flushDecoder() is called in state %d", mFlushingAudio);
Andreas Huber1aef2112011-01-04 14:01:29 -08001548 mFlushingAudio = newStatus;
Andreas Huber1aef2112011-01-04 14:01:29 -08001549 } else {
Wei Jia53904f32014-07-29 10:22:53 -07001550 ALOGE_IF(mFlushingVideo != NONE,
1551 "video flushDecoder() is called in state %d", mFlushingVideo);
Andreas Huber1aef2112011-01-04 14:01:29 -08001552 mFlushingVideo = newStatus;
Chong Zhangb86e68f2014-08-01 13:46:53 -07001553
1554 if (mCCDecoder != NULL) {
1555 mCCDecoder->flush();
1556 }
Andreas Huber1aef2112011-01-04 14:01:29 -08001557 }
1558}
1559
Lajos Molnar87603c02014-08-20 19:25:30 -07001560void NuPlayer::updateDecoderFormatWithoutFlush(
1561 bool audio, const sp<AMessage> &format) {
1562 ALOGV("[%s] updateDecoderFormatWithoutFlush", audio ? "audio" : "video");
1563
1564 const sp<Decoder> &decoder = getDecoder(audio);
1565 if (decoder == NULL) {
1566 ALOGI("updateDecoderFormatWithoutFlush %s without decoder present",
1567 audio ? "audio" : "video");
1568 return;
1569 }
1570
1571 decoder->signalUpdateFormat(format);
1572}
1573
Chong Zhangced1c2f2014-08-08 15:22:35 -07001574void NuPlayer::queueDecoderShutdown(
1575 bool audio, bool video, const sp<AMessage> &reply) {
1576 ALOGI("queueDecoderShutdown audio=%d, video=%d", audio, video);
Andreas Huber84066782011-08-16 09:34:26 -07001577
Chong Zhangced1c2f2014-08-08 15:22:35 -07001578 mDeferredActions.push_back(
Wei Jiafef808d2014-10-31 17:57:05 -07001579 new FlushDecoderAction(
1580 audio ? FLUSH_CMD_SHUTDOWN : FLUSH_CMD_NONE,
1581 video ? FLUSH_CMD_SHUTDOWN : FLUSH_CMD_NONE));
Andreas Huber84066782011-08-16 09:34:26 -07001582
Chong Zhangced1c2f2014-08-08 15:22:35 -07001583 mDeferredActions.push_back(
1584 new SimpleAction(&NuPlayer::performScanSources));
Andreas Huber84066782011-08-16 09:34:26 -07001585
Chong Zhangced1c2f2014-08-08 15:22:35 -07001586 mDeferredActions.push_back(new PostMessageAction(reply));
1587
1588 processDeferredActions();
Andreas Huber84066782011-08-16 09:34:26 -07001589}
1590
James Dong0d268a32012-08-31 12:18:27 -07001591status_t NuPlayer::setVideoScalingMode(int32_t mode) {
1592 mVideoScalingMode = mode;
Andreas Huber57a339c2012-12-03 11:18:00 -08001593 if (mNativeWindow != NULL) {
James Dong0d268a32012-08-31 12:18:27 -07001594 status_t ret = native_window_set_scaling_mode(
1595 mNativeWindow->getNativeWindow().get(), mVideoScalingMode);
1596 if (ret != OK) {
1597 ALOGE("Failed to set scaling mode (%d): %s",
1598 -ret, strerror(-ret));
1599 return ret;
1600 }
1601 }
1602 return OK;
1603}
1604
Chong Zhangdcb89b32013-08-06 09:44:47 -07001605status_t NuPlayer::getTrackInfo(Parcel* reply) const {
1606 sp<AMessage> msg = new AMessage(kWhatGetTrackInfo, id());
1607 msg->setPointer("reply", reply);
1608
1609 sp<AMessage> response;
1610 status_t err = msg->postAndAwaitResponse(&response);
1611 return err;
1612}
1613
Robert Shih7c4f0d72014-07-09 18:53:31 -07001614status_t NuPlayer::getSelectedTrack(int32_t type, Parcel* reply) const {
1615 sp<AMessage> msg = new AMessage(kWhatGetSelectedTrack, id());
1616 msg->setPointer("reply", reply);
1617 msg->setInt32("type", type);
1618
1619 sp<AMessage> response;
1620 status_t err = msg->postAndAwaitResponse(&response);
1621 if (err == OK && response != NULL) {
1622 CHECK(response->findInt32("err", &err));
1623 }
1624 return err;
1625}
1626
Chong Zhangdcb89b32013-08-06 09:44:47 -07001627status_t NuPlayer::selectTrack(size_t trackIndex, bool select) {
1628 sp<AMessage> msg = new AMessage(kWhatSelectTrack, id());
1629 msg->setSize("trackIndex", trackIndex);
1630 msg->setInt32("select", select);
1631
1632 sp<AMessage> response;
1633 status_t err = msg->postAndAwaitResponse(&response);
1634
Chong Zhang404fced2014-06-11 14:45:31 -07001635 if (err != OK) {
1636 return err;
1637 }
1638
1639 if (!response->findInt32("err", &err)) {
1640 err = OK;
1641 }
1642
Chong Zhangdcb89b32013-08-06 09:44:47 -07001643 return err;
1644}
1645
Ronghua Wua73d9e02014-10-08 15:13:29 -07001646status_t NuPlayer::getCurrentPosition(int64_t *mediaUs) {
1647 sp<Renderer> renderer = mRenderer;
1648 if (renderer == NULL) {
1649 return NO_INIT;
1650 }
1651
1652 return renderer->getCurrentPosition(mediaUs);
1653}
1654
1655void NuPlayer::getStats(int64_t *numFramesTotal, int64_t *numFramesDropped) {
1656 *numFramesTotal = mNumFramesTotal;
1657 *numFramesDropped = mNumFramesDropped;
1658}
1659
Marco Nelissenf0b72b52014-09-16 15:43:44 -07001660sp<MetaData> NuPlayer::getFileMeta() {
1661 return mSource->getFileFormatMeta();
1662}
1663
Andreas Huberb7c8e912012-11-27 15:02:53 -08001664void NuPlayer::schedulePollDuration() {
1665 sp<AMessage> msg = new AMessage(kWhatPollDuration, id());
1666 msg->setInt32("generation", mPollDurationGeneration);
1667 msg->post();
1668}
1669
1670void NuPlayer::cancelPollDuration() {
1671 ++mPollDurationGeneration;
1672}
1673
Andreas Hubera1f8ab02012-11-30 10:53:22 -08001674void NuPlayer::processDeferredActions() {
1675 while (!mDeferredActions.empty()) {
1676 // We won't execute any deferred actions until we're no longer in
1677 // an intermediate state, i.e. one more more decoders are currently
1678 // flushing or shutting down.
1679
Andreas Hubera1f8ab02012-11-30 10:53:22 -08001680 if (mFlushingAudio != NONE || mFlushingVideo != NONE) {
1681 // We're currently flushing, postpone the reset until that's
1682 // completed.
1683
1684 ALOGV("postponing action mFlushingAudio=%d, mFlushingVideo=%d",
1685 mFlushingAudio, mFlushingVideo);
1686
1687 break;
1688 }
1689
1690 sp<Action> action = *mDeferredActions.begin();
1691 mDeferredActions.erase(mDeferredActions.begin());
1692
1693 action->execute(this);
1694 }
1695}
1696
Wei Jiae427abf2014-09-22 15:21:11 -07001697void NuPlayer::performSeek(int64_t seekTimeUs, bool needNotify) {
1698 ALOGV("performSeek seekTimeUs=%lld us (%.2f secs), needNotify(%d)",
Andreas Hubera1f8ab02012-11-30 10:53:22 -08001699 seekTimeUs,
Wei Jiae427abf2014-09-22 15:21:11 -07001700 seekTimeUs / 1E6,
1701 needNotify);
Andreas Hubera1f8ab02012-11-30 10:53:22 -08001702
Andy Hungadf34bf2014-09-03 18:22:22 -07001703 if (mSource == NULL) {
1704 // This happens when reset occurs right before the loop mode
1705 // asynchronously seeks to the start of the stream.
1706 LOG_ALWAYS_FATAL_IF(mAudioDecoder != NULL || mVideoDecoder != NULL,
1707 "mSource is NULL and decoders not NULL audio(%p) video(%p)",
1708 mAudioDecoder.get(), mVideoDecoder.get());
1709 return;
1710 }
Andreas Hubera1f8ab02012-11-30 10:53:22 -08001711 mSource->seekTo(seekTimeUs);
Robert Shihd3b0bbb2014-07-23 15:00:25 -07001712 ++mTimedTextGeneration;
Andreas Hubera1f8ab02012-11-30 10:53:22 -08001713
1714 if (mDriver != NULL) {
1715 sp<NuPlayerDriver> driver = mDriver.promote();
1716 if (driver != NULL) {
Wei Jiae427abf2014-09-22 15:21:11 -07001717 if (needNotify) {
1718 driver->notifySeekComplete();
1719 }
Andreas Hubera1f8ab02012-11-30 10:53:22 -08001720 }
1721 }
1722
1723 // everything's flushed, continue playback.
1724}
1725
Wei Jiafef808d2014-10-31 17:57:05 -07001726void NuPlayer::performDecoderFlush(FlushCommand audio, FlushCommand video) {
1727 ALOGV("performDecoderFlush audio=%d, video=%d", audio, video);
Andreas Hubera1f8ab02012-11-30 10:53:22 -08001728
Wei Jiafef808d2014-10-31 17:57:05 -07001729 if ((audio == FLUSH_CMD_NONE || mAudioDecoder == NULL)
1730 && (video == FLUSH_CMD_NONE || mVideoDecoder == NULL)) {
Andreas Hubera1f8ab02012-11-30 10:53:22 -08001731 return;
1732 }
1733
1734 mTimeDiscontinuityPending = true;
1735
Wei Jiafef808d2014-10-31 17:57:05 -07001736 if (audio != FLUSH_CMD_NONE && mAudioDecoder != NULL) {
1737 flushDecoder(true /* audio */, (audio == FLUSH_CMD_SHUTDOWN));
Andreas Hubera1f8ab02012-11-30 10:53:22 -08001738 }
1739
Wei Jiafef808d2014-10-31 17:57:05 -07001740 if (video != FLUSH_CMD_NONE && mVideoDecoder != NULL) {
1741 flushDecoder(false /* audio */, (video == FLUSH_CMD_SHUTDOWN));
Andreas Hubera1f8ab02012-11-30 10:53:22 -08001742 }
1743}
1744
1745void NuPlayer::performReset() {
1746 ALOGV("performReset");
1747
1748 CHECK(mAudioDecoder == NULL);
1749 CHECK(mVideoDecoder == NULL);
1750
1751 cancelPollDuration();
1752
1753 ++mScanSourcesGeneration;
1754 mScanSourcesPending = false;
1755
Lajos Molnar09524832014-07-17 14:29:51 -07001756 if (mRendererLooper != NULL) {
1757 if (mRenderer != NULL) {
1758 mRendererLooper->unregisterHandler(mRenderer->id());
1759 }
1760 mRendererLooper->stop();
1761 mRendererLooper.clear();
1762 }
Andreas Hubera1f8ab02012-11-30 10:53:22 -08001763 mRenderer.clear();
Wei Jia57568df2014-09-22 10:16:29 -07001764 ++mRendererGeneration;
Andreas Hubera1f8ab02012-11-30 10:53:22 -08001765
1766 if (mSource != NULL) {
1767 mSource->stop();
Andreas Huberb5f25f02013-02-05 10:14:26 -08001768
Andreas Hubera1f8ab02012-11-30 10:53:22 -08001769 mSource.clear();
1770 }
1771
1772 if (mDriver != NULL) {
1773 sp<NuPlayerDriver> driver = mDriver.promote();
1774 if (driver != NULL) {
1775 driver->notifyResetComplete();
1776 }
1777 }
Andreas Huber57a339c2012-12-03 11:18:00 -08001778
1779 mStarted = false;
Andreas Hubera1f8ab02012-11-30 10:53:22 -08001780}
1781
1782void NuPlayer::performScanSources() {
1783 ALOGV("performScanSources");
1784
Andreas Huber57a339c2012-12-03 11:18:00 -08001785 if (!mStarted) {
1786 return;
1787 }
1788
Andreas Hubera1f8ab02012-11-30 10:53:22 -08001789 if (mAudioDecoder == NULL || mVideoDecoder == NULL) {
1790 postScanSources();
1791 }
1792}
1793
Andreas Huber57a339c2012-12-03 11:18:00 -08001794void NuPlayer::performSetSurface(const sp<NativeWindowWrapper> &wrapper) {
1795 ALOGV("performSetSurface");
1796
1797 mNativeWindow = wrapper;
1798
1799 // XXX - ignore error from setVideoScalingMode for now
1800 setVideoScalingMode(mVideoScalingMode);
Chong Zhang13d6faa2014-08-22 15:35:28 -07001801
1802 if (mDriver != NULL) {
1803 sp<NuPlayerDriver> driver = mDriver.promote();
1804 if (driver != NULL) {
1805 driver->notifySetSurfaceComplete();
1806 }
1807 }
Andreas Huber57a339c2012-12-03 11:18:00 -08001808}
1809
Andreas Huber9575c962013-02-05 13:59:56 -08001810void NuPlayer::onSourceNotify(const sp<AMessage> &msg) {
1811 int32_t what;
1812 CHECK(msg->findInt32("what", &what));
1813
1814 switch (what) {
1815 case Source::kWhatPrepared:
1816 {
Andreas Huberb5f28d42013-04-25 15:11:19 -07001817 if (mSource == NULL) {
1818 // This is a stale notification from a source that was
1819 // asynchronously preparing when the client called reset().
1820 // We handled the reset, the source is gone.
1821 break;
1822 }
1823
Andreas Huberec0c5972013-02-05 14:47:13 -08001824 int32_t err;
1825 CHECK(msg->findInt32("err", &err));
1826
Andreas Huber9575c962013-02-05 13:59:56 -08001827 sp<NuPlayerDriver> driver = mDriver.promote();
1828 if (driver != NULL) {
Marco Nelissendd114d12014-05-28 15:23:14 -07001829 // notify duration first, so that it's definitely set when
1830 // the app received the "prepare complete" callback.
1831 int64_t durationUs;
1832 if (mSource->getDuration(&durationUs) == OK) {
1833 driver->notifyDuration(durationUs);
1834 }
Andreas Huberec0c5972013-02-05 14:47:13 -08001835 driver->notifyPrepareCompleted(err);
Andreas Huber9575c962013-02-05 13:59:56 -08001836 }
Andreas Huber99759402013-04-01 14:28:31 -07001837
Andreas Huber9575c962013-02-05 13:59:56 -08001838 break;
1839 }
1840
1841 case Source::kWhatFlagsChanged:
1842 {
1843 uint32_t flags;
1844 CHECK(msg->findInt32("flags", (int32_t *)&flags));
1845
Chong Zhang4b7069d2013-09-11 12:52:43 -07001846 sp<NuPlayerDriver> driver = mDriver.promote();
1847 if (driver != NULL) {
1848 driver->notifyFlagsChanged(flags);
1849 }
1850
Andreas Huber9575c962013-02-05 13:59:56 -08001851 if ((mSourceFlags & Source::FLAG_DYNAMIC_DURATION)
1852 && (!(flags & Source::FLAG_DYNAMIC_DURATION))) {
1853 cancelPollDuration();
1854 } else if (!(mSourceFlags & Source::FLAG_DYNAMIC_DURATION)
1855 && (flags & Source::FLAG_DYNAMIC_DURATION)
1856 && (mAudioDecoder != NULL || mVideoDecoder != NULL)) {
1857 schedulePollDuration();
1858 }
1859
1860 mSourceFlags = flags;
1861 break;
1862 }
1863
1864 case Source::kWhatVideoSizeChanged:
1865 {
Chong Zhangced1c2f2014-08-08 15:22:35 -07001866 sp<AMessage> format;
1867 CHECK(msg->findMessage("format", &format));
Andreas Huber9575c962013-02-05 13:59:56 -08001868
Chong Zhangced1c2f2014-08-08 15:22:35 -07001869 updateVideoSize(format);
Andreas Huber9575c962013-02-05 13:59:56 -08001870 break;
1871 }
1872
Chong Zhang2a3cc9a2014-08-21 17:48:26 -07001873 case Source::kWhatBufferingUpdate:
1874 {
1875 int32_t percentage;
1876 CHECK(msg->findInt32("percentage", &percentage));
1877
1878 notifyListener(MEDIA_BUFFERING_UPDATE, percentage, 0);
1879 break;
1880 }
1881
Roger Jönssonb50e83e2013-01-21 16:26:41 +01001882 case Source::kWhatBufferingStart:
1883 {
1884 notifyListener(MEDIA_INFO, MEDIA_INFO_BUFFERING_START, 0);
1885 break;
1886 }
1887
1888 case Source::kWhatBufferingEnd:
1889 {
1890 notifyListener(MEDIA_INFO, MEDIA_INFO_BUFFERING_END, 0);
1891 break;
1892 }
1893
Chong Zhangdcb89b32013-08-06 09:44:47 -07001894 case Source::kWhatSubtitleData:
1895 {
1896 sp<ABuffer> buffer;
1897 CHECK(msg->findBuffer("buffer", &buffer));
1898
Chong Zhang404fced2014-06-11 14:45:31 -07001899 sendSubtitleData(buffer, 0 /* baseIndex */);
Chong Zhangdcb89b32013-08-06 09:44:47 -07001900 break;
1901 }
1902
Robert Shihd3b0bbb2014-07-23 15:00:25 -07001903 case Source::kWhatTimedTextData:
1904 {
1905 int32_t generation;
1906 if (msg->findInt32("generation", &generation)
1907 && generation != mTimedTextGeneration) {
1908 break;
1909 }
1910
1911 sp<ABuffer> buffer;
1912 CHECK(msg->findBuffer("buffer", &buffer));
1913
1914 sp<NuPlayerDriver> driver = mDriver.promote();
1915 if (driver == NULL) {
1916 break;
1917 }
1918
1919 int posMs;
1920 int64_t timeUs, posUs;
1921 driver->getCurrentPosition(&posMs);
1922 posUs = posMs * 1000;
1923 CHECK(buffer->meta()->findInt64("timeUs", &timeUs));
1924
1925 if (posUs < timeUs) {
1926 if (!msg->findInt32("generation", &generation)) {
1927 msg->setInt32("generation", mTimedTextGeneration);
1928 }
1929 msg->post(timeUs - posUs);
1930 } else {
1931 sendTimedTextData(buffer);
1932 }
1933 break;
1934 }
1935
Andreas Huber14f76722013-01-15 09:04:18 -08001936 case Source::kWhatQueueDecoderShutdown:
1937 {
1938 int32_t audio, video;
1939 CHECK(msg->findInt32("audio", &audio));
1940 CHECK(msg->findInt32("video", &video));
1941
1942 sp<AMessage> reply;
1943 CHECK(msg->findMessage("reply", &reply));
1944
1945 queueDecoderShutdown(audio, video, reply);
1946 break;
1947 }
1948
Ronghua Wu80276872014-08-28 15:50:29 -07001949 case Source::kWhatDrmNoLicense:
1950 {
1951 notifyListener(MEDIA_ERROR, MEDIA_ERROR_UNKNOWN, ERROR_DRM_NO_LICENSE);
1952 break;
1953 }
1954
Andreas Huber9575c962013-02-05 13:59:56 -08001955 default:
1956 TRESPASS();
1957 }
1958}
1959
Chong Zhanga7fa1d92014-06-11 14:49:23 -07001960void NuPlayer::onClosedCaptionNotify(const sp<AMessage> &msg) {
1961 int32_t what;
1962 CHECK(msg->findInt32("what", &what));
1963
1964 switch (what) {
1965 case NuPlayer::CCDecoder::kWhatClosedCaptionData:
1966 {
1967 sp<ABuffer> buffer;
1968 CHECK(msg->findBuffer("buffer", &buffer));
1969
1970 size_t inbandTracks = 0;
1971 if (mSource != NULL) {
1972 inbandTracks = mSource->getTrackCount();
1973 }
1974
1975 sendSubtitleData(buffer, inbandTracks);
1976 break;
1977 }
1978
1979 case NuPlayer::CCDecoder::kWhatTrackAdded:
1980 {
1981 notifyListener(MEDIA_INFO, MEDIA_INFO_METADATA_UPDATE, 0);
1982
1983 break;
1984 }
1985
1986 default:
1987 TRESPASS();
1988 }
1989
1990
1991}
1992
Chong Zhang404fced2014-06-11 14:45:31 -07001993void NuPlayer::sendSubtitleData(const sp<ABuffer> &buffer, int32_t baseIndex) {
1994 int32_t trackIndex;
1995 int64_t timeUs, durationUs;
1996 CHECK(buffer->meta()->findInt32("trackIndex", &trackIndex));
1997 CHECK(buffer->meta()->findInt64("timeUs", &timeUs));
1998 CHECK(buffer->meta()->findInt64("durationUs", &durationUs));
1999
2000 Parcel in;
2001 in.writeInt32(trackIndex + baseIndex);
2002 in.writeInt64(timeUs);
2003 in.writeInt64(durationUs);
2004 in.writeInt32(buffer->size());
2005 in.writeInt32(buffer->size());
2006 in.write(buffer->data(), buffer->size());
2007
2008 notifyListener(MEDIA_SUBTITLE_DATA, 0, 0, &in);
2009}
Robert Shihd3b0bbb2014-07-23 15:00:25 -07002010
2011void NuPlayer::sendTimedTextData(const sp<ABuffer> &buffer) {
2012 const void *data;
2013 size_t size = 0;
2014 int64_t timeUs;
2015 int32_t flag = TextDescriptions::LOCAL_DESCRIPTIONS;
2016
2017 AString mime;
2018 CHECK(buffer->meta()->findString("mime", &mime));
2019 CHECK(strcasecmp(mime.c_str(), MEDIA_MIMETYPE_TEXT_3GPP) == 0);
2020
2021 data = buffer->data();
2022 size = buffer->size();
2023
2024 Parcel parcel;
2025 if (size > 0) {
2026 CHECK(buffer->meta()->findInt64("timeUs", &timeUs));
2027 flag |= TextDescriptions::IN_BAND_TEXT_3GPP;
2028 TextDescriptions::getParcelOfDescriptions(
2029 (const uint8_t *)data, size, flag, timeUs / 1000, &parcel);
2030 }
2031
2032 if ((parcel.dataSize() > 0)) {
2033 notifyListener(MEDIA_TIMED_TEXT, 0, 0, &parcel);
2034 } else { // send an empty timed text
2035 notifyListener(MEDIA_TIMED_TEXT, 0, 0);
2036 }
2037}
Andreas Huberb5f25f02013-02-05 10:14:26 -08002038////////////////////////////////////////////////////////////////////////////////
2039
Chong Zhangced1c2f2014-08-08 15:22:35 -07002040sp<AMessage> NuPlayer::Source::getFormat(bool audio) {
2041 sp<MetaData> meta = getFormatMeta(audio);
2042
2043 if (meta == NULL) {
2044 return NULL;
2045 }
2046
2047 sp<AMessage> msg = new AMessage;
2048
2049 if(convertMetaDataToMessage(meta, &msg) == OK) {
2050 return msg;
2051 }
2052 return NULL;
2053}
2054
Andreas Huber9575c962013-02-05 13:59:56 -08002055void NuPlayer::Source::notifyFlagsChanged(uint32_t flags) {
2056 sp<AMessage> notify = dupNotify();
2057 notify->setInt32("what", kWhatFlagsChanged);
2058 notify->setInt32("flags", flags);
2059 notify->post();
2060}
2061
Chong Zhangced1c2f2014-08-08 15:22:35 -07002062void NuPlayer::Source::notifyVideoSizeChanged(const sp<AMessage> &format) {
Andreas Huber9575c962013-02-05 13:59:56 -08002063 sp<AMessage> notify = dupNotify();
2064 notify->setInt32("what", kWhatVideoSizeChanged);
Chong Zhangced1c2f2014-08-08 15:22:35 -07002065 notify->setMessage("format", format);
Andreas Huber9575c962013-02-05 13:59:56 -08002066 notify->post();
2067}
2068
Andreas Huberec0c5972013-02-05 14:47:13 -08002069void NuPlayer::Source::notifyPrepared(status_t err) {
Andreas Huber9575c962013-02-05 13:59:56 -08002070 sp<AMessage> notify = dupNotify();
2071 notify->setInt32("what", kWhatPrepared);
Andreas Huberec0c5972013-02-05 14:47:13 -08002072 notify->setInt32("err", err);
Andreas Huber9575c962013-02-05 13:59:56 -08002073 notify->post();
2074}
2075
Andreas Huber84333e02014-02-07 15:36:10 -08002076void NuPlayer::Source::onMessageReceived(const sp<AMessage> & /* msg */) {
Andreas Huberb5f25f02013-02-05 10:14:26 -08002077 TRESPASS();
2078}
2079
Andreas Huberf9334412010-12-15 15:17:42 -08002080} // namespace android