blob: 59b070cfb9f5635a2b99ac518541ffbf26fdcb9f [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
Ronghua Wua10fd232014-11-06 16:15:20 -0800609 // Don't try to re-open audio sink if there's an existing decoder.
610 if (mAudioSink != NULL && mAudioDecoder == NULL) {
611 sp<MetaData> audioMeta = mSource->getFormatMeta(true /* audio */);
612 sp<AMessage> videoFormat = mSource->getFormat(false /* audio */);
613 audio_stream_type_t streamType = mAudioSink->getAudioStreamType();
614 bool canOffload = canOffloadStream(audioMeta, (videoFormat != NULL),
615 true /* is_streaming */, streamType);
616 if (canOffload) {
617 if (!mOffloadAudio) {
618 mRenderer->signalEnableOffloadAudio();
619 }
Andy Hung282a7e32014-08-14 15:56:34 -0700620 // open audio sink early under offload mode.
621 sp<AMessage> format = mSource->getFormat(true /*audio*/);
622 openAudioSink(format, true /*offloadOnly*/);
623 }
Andreas Huber5bc087c2010-12-23 10:27:40 -0800624 instantiateDecoder(true, &mAudioDecoder);
Andreas Huberf9334412010-12-15 15:17:42 -0800625 }
626
Andreas Huberb7c8e912012-11-27 15:02:53 -0800627 if (!mHadAnySourcesBefore
628 && (mAudioDecoder != NULL || mVideoDecoder != NULL)) {
629 // This is the first time we've found anything playable.
630
Andreas Huber9575c962013-02-05 13:59:56 -0800631 if (mSourceFlags & Source::FLAG_DYNAMIC_DURATION) {
Andreas Huberb7c8e912012-11-27 15:02:53 -0800632 schedulePollDuration();
633 }
634 }
635
Andreas Hubereac68ba2011-09-27 12:12:25 -0700636 status_t err;
637 if ((err = mSource->feedMoreTSData()) != OK) {
Andreas Huber1aef2112011-01-04 14:01:29 -0800638 if (mAudioDecoder == NULL && mVideoDecoder == NULL) {
639 // We're not currently decoding anything (no audio or
640 // video tracks found) and we just ran out of input data.
Andreas Hubereac68ba2011-09-27 12:12:25 -0700641
642 if (err == ERROR_END_OF_STREAM) {
643 notifyListener(MEDIA_PLAYBACK_COMPLETE, 0, 0);
644 } else {
645 notifyListener(MEDIA_ERROR, MEDIA_ERROR_UNKNOWN, err);
646 }
Andreas Huber1aef2112011-01-04 14:01:29 -0800647 }
Andreas Huberf9334412010-12-15 15:17:42 -0800648 break;
649 }
650
Andreas Huberfbe9d812012-08-31 14:05:27 -0700651 if ((mAudioDecoder == NULL && mAudioSink != NULL)
652 || (mVideoDecoder == NULL && mNativeWindow != NULL)) {
Andreas Huberf9334412010-12-15 15:17:42 -0800653 msg->post(100000ll);
Andreas Huber5bc087c2010-12-23 10:27:40 -0800654 mScanSourcesPending = true;
Andreas Huberf9334412010-12-15 15:17:42 -0800655 }
656 break;
657 }
658
659 case kWhatVideoNotify:
660 case kWhatAudioNotify:
661 {
662 bool audio = msg->what() == kWhatAudioNotify;
663
Wei Jia88703c32014-08-06 11:24:07 -0700664 int32_t currentDecoderGeneration =
665 (audio? mAudioDecoderGeneration : mVideoDecoderGeneration);
666 int32_t requesterGeneration = currentDecoderGeneration - 1;
667 CHECK(msg->findInt32("generation", &requesterGeneration));
668
669 if (requesterGeneration != currentDecoderGeneration) {
670 ALOGV("got message from old %s decoder, generation(%d:%d)",
671 audio ? "audio" : "video", requesterGeneration,
672 currentDecoderGeneration);
673 sp<AMessage> reply;
674 if (!(msg->findMessage("reply", &reply))) {
675 return;
676 }
677
678 reply->setInt32("err", INFO_DISCONTINUITY);
679 reply->post();
680 return;
681 }
682
Andreas Huberf9334412010-12-15 15:17:42 -0800683 int32_t what;
Lajos Molnar1cd13982014-01-17 15:12:51 -0800684 CHECK(msg->findInt32("what", &what));
Andreas Huberf9334412010-12-15 15:17:42 -0800685
Lajos Molnar1cd13982014-01-17 15:12:51 -0800686 if (what == Decoder::kWhatFillThisBuffer) {
Andreas Huberf9334412010-12-15 15:17:42 -0800687 status_t err = feedDecoderInputData(
Lajos Molnar1cd13982014-01-17 15:12:51 -0800688 audio, msg);
Andreas Huberf9334412010-12-15 15:17:42 -0800689
Andreas Huber5bc087c2010-12-23 10:27:40 -0800690 if (err == -EWOULDBLOCK) {
Andreas Hubereac68ba2011-09-27 12:12:25 -0700691 if (mSource->feedMoreTSData() == OK) {
Phil Burkc5cc2e22014-09-09 20:08:39 -0700692 msg->post(10 * 1000ll);
Andreas Huber5bc087c2010-12-23 10:27:40 -0800693 }
Andreas Huberf9334412010-12-15 15:17:42 -0800694 }
Lajos Molnar1cd13982014-01-17 15:12:51 -0800695 } else if (what == Decoder::kWhatEOS) {
Andreas Huberdc9bacd2011-09-26 10:53:29 -0700696 int32_t err;
Lajos Molnar1cd13982014-01-17 15:12:51 -0800697 CHECK(msg->findInt32("err", &err));
Andreas Huberdc9bacd2011-09-26 10:53:29 -0700698
699 if (err == ERROR_END_OF_STREAM) {
Steve Block3856b092011-10-20 11:56:00 +0100700 ALOGV("got %s decoder EOS", audio ? "audio" : "video");
Andreas Huberdc9bacd2011-09-26 10:53:29 -0700701 } else {
Steve Block3856b092011-10-20 11:56:00 +0100702 ALOGV("got %s decoder EOS w/ error %d",
Andreas Huberdc9bacd2011-09-26 10:53:29 -0700703 audio ? "audio" : "video",
704 err);
705 }
706
707 mRenderer->queueEOS(audio, err);
Lajos Molnar1cd13982014-01-17 15:12:51 -0800708 } else if (what == Decoder::kWhatFlushCompleted) {
Steve Block3856b092011-10-20 11:56:00 +0100709 ALOGV("decoder %s flush completed", audio ? "audio" : "video");
Andreas Huberf9334412010-12-15 15:17:42 -0800710
Andy Hung8d121d42014-10-03 09:53:53 -0700711 handleFlushComplete(audio, true /* isDecoder */);
Andreas Huber3831a062010-12-21 10:22:33 -0800712 finishFlushIfPossible();
Wei Jiac6cfd702014-11-11 16:33:20 -0800713 } else if (what == Decoder::kWhatVideoSizeChanged) {
Lajos Molnar1cd13982014-01-17 15:12:51 -0800714 sp<AMessage> format;
715 CHECK(msg->findMessage("format", &format));
716
Wei Jiac6cfd702014-11-11 16:33:20 -0800717 sp<AMessage> inputFormat =
718 mSource->getFormat(false /* audio */);
Andreas Huber3831a062010-12-21 10:22:33 -0800719
Wei Jiac6cfd702014-11-11 16:33:20 -0800720 updateVideoSize(inputFormat, format);
Lajos Molnar1cd13982014-01-17 15:12:51 -0800721 } else if (what == Decoder::kWhatShutdownCompleted) {
Steve Block3856b092011-10-20 11:56:00 +0100722 ALOGV("%s shutdown completed", audio ? "audio" : "video");
Andreas Huber3831a062010-12-21 10:22:33 -0800723 if (audio) {
724 mAudioDecoder.clear();
Wei Jia57568df2014-09-22 10:16:29 -0700725 ++mAudioDecoderGeneration;
Andreas Huber3831a062010-12-21 10:22:33 -0800726
727 CHECK_EQ((int)mFlushingAudio, (int)SHUTTING_DOWN_DECODER);
728 mFlushingAudio = SHUT_DOWN;
729 } else {
730 mVideoDecoder.clear();
Wei Jia57568df2014-09-22 10:16:29 -0700731 ++mVideoDecoderGeneration;
Andreas Huber3831a062010-12-21 10:22:33 -0800732
733 CHECK_EQ((int)mFlushingVideo, (int)SHUTTING_DOWN_DECODER);
734 mFlushingVideo = SHUT_DOWN;
735 }
736
737 finishFlushIfPossible();
Lajos Molnar1cd13982014-01-17 15:12:51 -0800738 } else if (what == Decoder::kWhatError) {
Chong Zhangf4c0a942014-08-11 15:14:10 -0700739 status_t err;
Andy Hung2abde2c2014-09-30 14:40:32 -0700740 if (!msg->findInt32("err", &err) || err == OK) {
Chong Zhangf4c0a942014-08-11 15:14:10 -0700741 err = UNKNOWN_ERROR;
742 }
Andy Hungcf31f1e2014-09-23 14:59:01 -0700743
Andy Hung2abde2c2014-09-30 14:40:32 -0700744 // Decoder errors can be due to Source (e.g. from streaming),
745 // or from decoding corrupted bitstreams, or from other decoder
746 // MediaCodec operations (e.g. from an ongoing reset or seek).
747 //
748 // We try to gracefully shut down the affected decoder if possible,
749 // rather than trying to force the shutdown with something
750 // similar to performReset(). This method can lead to a hang
751 // if MediaCodec functions block after an error, but they should
752 // typically return INVALID_OPERATION instead of blocking.
753
754 FlushStatus *flushing = audio ? &mFlushingAudio : &mFlushingVideo;
755 ALOGE("received error(%#x) from %s decoder, flushing(%d), now shutting down",
756 err, audio ? "audio" : "video", *flushing);
757
758 switch (*flushing) {
759 case NONE:
760 mDeferredActions.push_back(
Wei Jiafef808d2014-10-31 17:57:05 -0700761 new FlushDecoderAction(
762 audio ? FLUSH_CMD_SHUTDOWN : FLUSH_CMD_NONE,
763 audio ? FLUSH_CMD_NONE : FLUSH_CMD_SHUTDOWN));
Andy Hung2abde2c2014-09-30 14:40:32 -0700764 processDeferredActions();
765 break;
766 case FLUSHING_DECODER:
767 *flushing = FLUSHING_DECODER_SHUTDOWN; // initiate shutdown after flush.
768 break; // Wait for flush to complete.
769 case FLUSHING_DECODER_SHUTDOWN:
770 break; // Wait for flush to complete.
771 case SHUTTING_DOWN_DECODER:
772 break; // Wait for shutdown to complete.
773 case FLUSHED:
774 // Widevine source reads must stop before releasing the video decoder.
775 if (!audio && mSource != NULL && mSourceFlags & Source::FLAG_SECURE) {
776 mSource->stop();
777 }
778 getDecoder(audio)->initiateShutdown(); // In the middle of a seek.
779 *flushing = SHUTTING_DOWN_DECODER; // Shut down.
780 break;
781 case SHUT_DOWN:
782 finishFlushIfPossible(); // Should not occur.
783 break; // Finish anyways.
Marco Nelissen9e2b7912014-08-18 16:13:03 -0700784 }
Andy Hung2abde2c2014-09-30 14:40:32 -0700785 notifyListener(MEDIA_ERROR, MEDIA_ERROR_UNKNOWN, err);
Wei Jiac6cfd702014-11-11 16:33:20 -0800786 } else if (what == Decoder::kWhatRenderBufferTime) {
Lajos Molnar1cd13982014-01-17 15:12:51 -0800787 renderBuffer(audio, msg);
788 } else {
789 ALOGV("Unhandled decoder notification %d '%c%c%c%c'.",
Andreas Hubera1f8ab02012-11-30 10:53:22 -0800790 what,
791 what >> 24,
792 (what >> 16) & 0xff,
793 (what >> 8) & 0xff,
794 what & 0xff);
Andreas Huberf9334412010-12-15 15:17:42 -0800795 }
796
797 break;
798 }
799
800 case kWhatRendererNotify:
801 {
Wei Jia57568df2014-09-22 10:16:29 -0700802 int32_t requesterGeneration = mRendererGeneration - 1;
803 CHECK(msg->findInt32("generation", &requesterGeneration));
804 if (requesterGeneration != mRendererGeneration) {
805 ALOGV("got message from old renderer, generation(%d:%d)",
806 requesterGeneration, mRendererGeneration);
807 return;
808 }
809
Andreas Huberf9334412010-12-15 15:17:42 -0800810 int32_t what;
811 CHECK(msg->findInt32("what", &what));
812
813 if (what == Renderer::kWhatEOS) {
814 int32_t audio;
815 CHECK(msg->findInt32("audio", &audio));
816
Andreas Huberc92fd242011-08-16 13:48:44 -0700817 int32_t finalResult;
818 CHECK(msg->findInt32("finalResult", &finalResult));
819
Andreas Huberf9334412010-12-15 15:17:42 -0800820 if (audio) {
821 mAudioEOS = true;
822 } else {
823 mVideoEOS = true;
824 }
825
Andreas Huberc92fd242011-08-16 13:48:44 -0700826 if (finalResult == ERROR_END_OF_STREAM) {
Steve Block3856b092011-10-20 11:56:00 +0100827 ALOGV("reached %s EOS", audio ? "audio" : "video");
Andreas Huberc92fd242011-08-16 13:48:44 -0700828 } else {
Steve Block29357bc2012-01-06 19:20:56 +0000829 ALOGE("%s track encountered an error (%d)",
Andreas Huberc92fd242011-08-16 13:48:44 -0700830 audio ? "audio" : "video", finalResult);
831
832 notifyListener(
833 MEDIA_ERROR, MEDIA_ERROR_UNKNOWN, finalResult);
834 }
Andreas Huberf9334412010-12-15 15:17:42 -0800835
836 if ((mAudioEOS || mAudioDecoder == NULL)
837 && (mVideoEOS || mVideoDecoder == NULL)) {
838 notifyListener(MEDIA_PLAYBACK_COMPLETE, 0, 0);
839 }
Andreas Huber3fe62152011-09-16 15:09:22 -0700840 } else if (what == Renderer::kWhatFlushComplete) {
Andreas Huberf9334412010-12-15 15:17:42 -0800841 int32_t audio;
842 CHECK(msg->findInt32("audio", &audio));
843
Steve Block3856b092011-10-20 11:56:00 +0100844 ALOGV("renderer %s flush completed.", audio ? "audio" : "video");
Andy Hung8d121d42014-10-03 09:53:53 -0700845 handleFlushComplete(audio, false /* isDecoder */);
846 finishFlushIfPossible();
James Dongf57b4ea2012-07-20 13:38:36 -0700847 } else if (what == Renderer::kWhatVideoRenderingStart) {
848 notifyListener(MEDIA_INFO, MEDIA_INFO_RENDERING_START, 0);
Lajos Molnarcbaffcf2013-08-14 18:30:38 -0700849 } else if (what == Renderer::kWhatMediaRenderingStart) {
850 ALOGV("media rendering started");
851 notifyListener(MEDIA_STARTED, 0, 0);
Wei Jia3a2956d2014-07-22 16:01:33 -0700852 } else if (what == Renderer::kWhatAudioOffloadTearDown) {
Ronghua Wua10fd232014-11-06 16:15:20 -0800853 ALOGV("Tear down audio offload, fall back to s/w path if due to error.");
Wei Jia3a2956d2014-07-22 16:01:33 -0700854 int64_t positionUs;
855 CHECK(msg->findInt64("positionUs", &positionUs));
Ronghua Wu08529172014-10-02 16:55:52 -0700856 int32_t reason;
857 CHECK(msg->findInt32("reason", &reason));
Andy Hung282a7e32014-08-14 15:56:34 -0700858 closeAudioSink();
Wei Jia3a2956d2014-07-22 16:01:33 -0700859 mAudioDecoder.clear();
Wei Jia57568df2014-09-22 10:16:29 -0700860 ++mAudioDecoderGeneration;
Wei Jia3a2956d2014-07-22 16:01:33 -0700861 mRenderer->flush(true /* audio */);
862 if (mVideoDecoder != NULL) {
863 mRenderer->flush(false /* audio */);
864 }
Wei Jia3a2956d2014-07-22 16:01:33 -0700865
Wei Jiae427abf2014-09-22 15:21:11 -0700866 performSeek(positionUs, false /* needNotify */);
Ronghua Wu08529172014-10-02 16:55:52 -0700867 if (reason == Renderer::kDueToError) {
Ronghua Wua10fd232014-11-06 16:15:20 -0800868 mRenderer->signalDisableOffloadAudio();
869 mOffloadAudio = false;
Ronghua Wu08529172014-10-02 16:55:52 -0700870 instantiateDecoder(true /* audio */, &mAudioDecoder);
871 }
Andreas Huberf9334412010-12-15 15:17:42 -0800872 }
873 break;
874 }
875
876 case kWhatMoreDataQueued:
877 {
878 break;
879 }
880
Andreas Huber1aef2112011-01-04 14:01:29 -0800881 case kWhatReset:
882 {
Steve Block3856b092011-10-20 11:56:00 +0100883 ALOGV("kWhatReset");
Andreas Huber1aef2112011-01-04 14:01:29 -0800884
Andreas Hubera1f8ab02012-11-30 10:53:22 -0800885 mDeferredActions.push_back(
Wei Jiafef808d2014-10-31 17:57:05 -0700886 new FlushDecoderAction(
887 FLUSH_CMD_SHUTDOWN /* audio */,
888 FLUSH_CMD_SHUTDOWN /* video */));
Andreas Huberb7c8e912012-11-27 15:02:53 -0800889
Andreas Hubera1f8ab02012-11-30 10:53:22 -0800890 mDeferredActions.push_back(
891 new SimpleAction(&NuPlayer::performReset));
Andreas Huberb58ce9f2011-11-28 16:27:35 -0800892
Andreas Hubera1f8ab02012-11-30 10:53:22 -0800893 processDeferredActions();
Andreas Huber1aef2112011-01-04 14:01:29 -0800894 break;
895 }
896
Andreas Huber43c3e6c2011-01-05 12:17:08 -0800897 case kWhatSeek:
898 {
899 int64_t seekTimeUs;
Wei Jiae427abf2014-09-22 15:21:11 -0700900 int32_t needNotify;
Andreas Huber43c3e6c2011-01-05 12:17:08 -0800901 CHECK(msg->findInt64("seekTimeUs", &seekTimeUs));
Wei Jiae427abf2014-09-22 15:21:11 -0700902 CHECK(msg->findInt32("needNotify", &needNotify));
Andreas Huber43c3e6c2011-01-05 12:17:08 -0800903
Wei Jiae427abf2014-09-22 15:21:11 -0700904 ALOGV("kWhatSeek seekTimeUs=%lld us, needNotify=%d",
905 seekTimeUs, needNotify);
Andreas Huber43c3e6c2011-01-05 12:17:08 -0800906
Andreas Hubera1f8ab02012-11-30 10:53:22 -0800907 mDeferredActions.push_back(
Wei Jiafef808d2014-10-31 17:57:05 -0700908 new FlushDecoderAction(FLUSH_CMD_FLUSH /* audio */,
909 FLUSH_CMD_FLUSH /* video */));
Andreas Huber43c3e6c2011-01-05 12:17:08 -0800910
Wei Jiae427abf2014-09-22 15:21:11 -0700911 mDeferredActions.push_back(
912 new SeekAction(seekTimeUs, needNotify));
Andreas Huber43c3e6c2011-01-05 12:17:08 -0800913
Andreas Hubera1f8ab02012-11-30 10:53:22 -0800914 processDeferredActions();
Andreas Huber43c3e6c2011-01-05 12:17:08 -0800915 break;
916 }
917
Andreas Huberb4082222011-01-20 15:23:04 -0800918 case kWhatPause:
919 {
Wei Jia7e9f7f72014-09-23 10:55:35 -0700920 if (mSource != NULL) {
921 mSource->pause();
922 } else {
923 ALOGW("pause called when source is gone or not set");
924 }
925 if (mRenderer != NULL) {
926 mRenderer->pause();
927 } else {
928 ALOGW("pause called when renderer is gone or not set");
929 }
Andreas Huberb4082222011-01-20 15:23:04 -0800930 break;
931 }
932
Andreas Huberb5f25f02013-02-05 10:14:26 -0800933 case kWhatSourceNotify:
934 {
Andreas Huber9575c962013-02-05 13:59:56 -0800935 onSourceNotify(msg);
Andreas Huberb5f25f02013-02-05 10:14:26 -0800936 break;
937 }
938
Chong Zhanga7fa1d92014-06-11 14:49:23 -0700939 case kWhatClosedCaptionNotify:
940 {
941 onClosedCaptionNotify(msg);
942 break;
943 }
944
Andreas Huberf9334412010-12-15 15:17:42 -0800945 default:
946 TRESPASS();
947 break;
948 }
949}
950
Wei Jia94211742014-10-28 17:09:06 -0700951void NuPlayer::onResume() {
952 if (mSource != NULL) {
953 mSource->resume();
954 } else {
955 ALOGW("resume called when source is gone or not set");
956 }
957 // |mAudioDecoder| may have been released due to the pause timeout, so re-create it if
958 // needed.
959 if (audioDecoderStillNeeded() && mAudioDecoder == NULL) {
960 instantiateDecoder(true /* audio */, &mAudioDecoder);
961 }
962 if (mRenderer != NULL) {
963 mRenderer->resume();
964 } else {
965 ALOGW("resume called when renderer is gone or not set");
966 }
967}
968
969void NuPlayer::onStart() {
970 mVideoIsAVC = false;
971 mOffloadAudio = false;
972 mAudioEOS = false;
973 mVideoEOS = false;
Wei Jia94211742014-10-28 17:09:06 -0700974 mNumFramesTotal = 0;
975 mNumFramesDropped = 0;
976 mStarted = true;
977
978 /* instantiate decoders now for secure playback */
979 if (mSourceFlags & Source::FLAG_SECURE) {
980 if (mNativeWindow != NULL) {
981 instantiateDecoder(false, &mVideoDecoder);
982 }
983
984 if (mAudioSink != NULL) {
985 instantiateDecoder(true, &mAudioDecoder);
986 }
987 }
988
989 mSource->start();
990
991 uint32_t flags = 0;
992
993 if (mSource->isRealTime()) {
994 flags |= Renderer::FLAG_REAL_TIME;
995 }
996
997 sp<MetaData> audioMeta = mSource->getFormatMeta(true /* audio */);
998 audio_stream_type_t streamType = AUDIO_STREAM_MUSIC;
999 if (mAudioSink != NULL) {
1000 streamType = mAudioSink->getAudioStreamType();
1001 }
1002
1003 sp<AMessage> videoFormat = mSource->getFormat(false /* audio */);
1004
1005 mOffloadAudio =
1006 canOffloadStream(audioMeta, (videoFormat != NULL),
1007 true /* is_streaming */, streamType);
1008 if (mOffloadAudio) {
1009 flags |= Renderer::FLAG_OFFLOAD_AUDIO;
1010 }
1011
1012 sp<AMessage> notify = new AMessage(kWhatRendererNotify, id());
1013 ++mRendererGeneration;
1014 notify->setInt32("generation", mRendererGeneration);
1015 mRenderer = new Renderer(mAudioSink, notify, flags);
1016
1017 mRendererLooper = new ALooper;
1018 mRendererLooper->setName("NuPlayerRenderer");
1019 mRendererLooper->start(false, false, ANDROID_PRIORITY_AUDIO);
1020 mRendererLooper->registerHandler(mRenderer);
1021
1022 sp<MetaData> meta = getFileMeta();
1023 int32_t rate;
1024 if (meta != NULL
1025 && meta->findInt32(kKeyFrameRate, &rate) && rate > 0) {
1026 mRenderer->setVideoFrameRate(rate);
1027 }
1028
Wei Jiac6cfd702014-11-11 16:33:20 -08001029 if (mVideoDecoder != NULL) {
1030 mVideoDecoder->setRenderer(mRenderer);
1031 }
1032 if (mAudioDecoder != NULL) {
1033 mAudioDecoder->setRenderer(mRenderer);
1034 }
1035
Wei Jia94211742014-10-28 17:09:06 -07001036 postScanSources();
1037}
1038
Ronghua Wud7988b12014-10-03 15:19:10 -07001039bool NuPlayer::audioDecoderStillNeeded() {
1040 // Audio decoder is no longer needed if it's in shut/shutting down status.
1041 return ((mFlushingAudio != SHUT_DOWN) && (mFlushingAudio != SHUTTING_DOWN_DECODER));
1042}
1043
Andy Hung8d121d42014-10-03 09:53:53 -07001044void NuPlayer::handleFlushComplete(bool audio, bool isDecoder) {
1045 // We wait for both the decoder flush and the renderer flush to complete
1046 // before entering either the FLUSHED or the SHUTTING_DOWN_DECODER state.
1047
1048 mFlushComplete[audio][isDecoder] = true;
1049 if (!mFlushComplete[audio][!isDecoder]) {
1050 return;
1051 }
1052
1053 FlushStatus *state = audio ? &mFlushingAudio : &mFlushingVideo;
1054 switch (*state) {
1055 case FLUSHING_DECODER:
1056 {
1057 *state = FLUSHED;
Andy Hung8d121d42014-10-03 09:53:53 -07001058 break;
1059 }
1060
1061 case FLUSHING_DECODER_SHUTDOWN:
1062 {
1063 *state = SHUTTING_DOWN_DECODER;
1064
1065 ALOGV("initiating %s decoder shutdown", audio ? "audio" : "video");
1066 if (!audio) {
Andy Hung8d121d42014-10-03 09:53:53 -07001067 // Widevine source reads must stop before releasing the video decoder.
1068 if (mSource != NULL && mSourceFlags & Source::FLAG_SECURE) {
1069 mSource->stop();
1070 }
1071 }
1072 getDecoder(audio)->initiateShutdown();
1073 break;
1074 }
1075
1076 default:
1077 // decoder flush completes only occur in a flushing state.
1078 LOG_ALWAYS_FATAL_IF(isDecoder, "decoder flush in invalid state %d", *state);
1079 break;
1080 }
1081}
1082
Andreas Huber3831a062010-12-21 10:22:33 -08001083void NuPlayer::finishFlushIfPossible() {
Wei Jia53904f32014-07-29 10:22:53 -07001084 if (mFlushingAudio != NONE && mFlushingAudio != FLUSHED
1085 && mFlushingAudio != SHUT_DOWN) {
Andreas Huber3831a062010-12-21 10:22:33 -08001086 return;
1087 }
1088
Wei Jia53904f32014-07-29 10:22:53 -07001089 if (mFlushingVideo != NONE && mFlushingVideo != FLUSHED
1090 && mFlushingVideo != SHUT_DOWN) {
Andreas Huber3831a062010-12-21 10:22:33 -08001091 return;
1092 }
1093
Steve Block3856b092011-10-20 11:56:00 +01001094 ALOGV("both audio and video are flushed now.");
Andreas Huber3831a062010-12-21 10:22:33 -08001095
Phil Burk9f526492014-09-03 15:04:12 -07001096 mPendingAudioAccessUnit.clear();
Phil Burkc5cc2e22014-09-09 20:08:39 -07001097 mAggregateBuffer.clear();
Phil Burk9f526492014-09-03 15:04:12 -07001098
Andreas Huber6e3d3112011-11-28 12:36:11 -08001099 if (mTimeDiscontinuityPending) {
1100 mRenderer->signalTimeDiscontinuity();
1101 mTimeDiscontinuityPending = false;
1102 }
Andreas Huber3831a062010-12-21 10:22:33 -08001103
Wei Jia53904f32014-07-29 10:22:53 -07001104 if (mAudioDecoder != NULL && mFlushingAudio == FLUSHED) {
Andreas Huber3831a062010-12-21 10:22:33 -08001105 mAudioDecoder->signalResume();
1106 }
1107
Wei Jia53904f32014-07-29 10:22:53 -07001108 if (mVideoDecoder != NULL && mFlushingVideo == FLUSHED) {
Andreas Huber3831a062010-12-21 10:22:33 -08001109 mVideoDecoder->signalResume();
1110 }
1111
1112 mFlushingAudio = NONE;
1113 mFlushingVideo = NONE;
Andreas Huber3831a062010-12-21 10:22:33 -08001114
Andy Hung8d121d42014-10-03 09:53:53 -07001115 clearFlushComplete();
1116
Andreas Hubera1f8ab02012-11-30 10:53:22 -08001117 processDeferredActions();
Andreas Huber1aef2112011-01-04 14:01:29 -08001118}
1119
1120void NuPlayer::postScanSources() {
1121 if (mScanSourcesPending) {
1122 return;
1123 }
1124
1125 sp<AMessage> msg = new AMessage(kWhatScanSources, id());
1126 msg->setInt32("generation", mScanSourcesGeneration);
1127 msg->post();
1128
1129 mScanSourcesPending = true;
1130}
1131
Andy Hung282a7e32014-08-14 15:56:34 -07001132void NuPlayer::openAudioSink(const sp<AMessage> &format, bool offloadOnly) {
Andy Hung282a7e32014-08-14 15:56:34 -07001133 uint32_t flags;
1134 int64_t durationUs;
Chong Zhang3b9eb1f2014-10-15 17:05:08 -07001135 bool hasVideo = (mVideoDecoder != NULL);
Andy Hung282a7e32014-08-14 15:56:34 -07001136 // FIXME: we should handle the case where the video decoder
1137 // is created after we receive the format change indication.
1138 // Current code will just make that we select deep buffer
1139 // with video which should not be a problem as it should
1140 // not prevent from keeping A/V sync.
Eric Laurentd88c3ca2014-10-28 10:52:11 -07001141 if (!hasVideo &&
Andy Hung282a7e32014-08-14 15:56:34 -07001142 mSource->getDuration(&durationUs) == OK &&
1143 durationUs
1144 > AUDIO_SINK_MIN_DEEP_BUFFER_DURATION_US) {
1145 flags = AUDIO_OUTPUT_FLAG_DEEP_BUFFER;
1146 } else {
1147 flags = AUDIO_OUTPUT_FLAG_NONE;
1148 }
1149
Chong Zhang3b9eb1f2014-10-15 17:05:08 -07001150 mOffloadAudio = mRenderer->openAudioSink(
1151 format, offloadOnly, hasVideo, flags);
1152
Andy Hung282a7e32014-08-14 15:56:34 -07001153 if (mOffloadAudio) {
Chong Zhang3b9eb1f2014-10-15 17:05:08 -07001154 sp<MetaData> audioMeta =
1155 mSource->getFormatMeta(true /* audio */);
1156 sendMetaDataToHal(mAudioSink, audioMeta);
Andy Hung282a7e32014-08-14 15:56:34 -07001157 }
1158}
1159
1160void NuPlayer::closeAudioSink() {
Chong Zhang3b9eb1f2014-10-15 17:05:08 -07001161 mRenderer->closeAudioSink();
Andy Hung282a7e32014-08-14 15:56:34 -07001162}
1163
Andreas Huber5bc087c2010-12-23 10:27:40 -08001164status_t NuPlayer::instantiateDecoder(bool audio, sp<Decoder> *decoder) {
Andreas Huberf9334412010-12-15 15:17:42 -08001165 if (*decoder != NULL) {
1166 return OK;
1167 }
1168
Andreas Huber84066782011-08-16 09:34:26 -07001169 sp<AMessage> format = mSource->getFormat(audio);
Andreas Huberf9334412010-12-15 15:17:42 -08001170
Andreas Huber84066782011-08-16 09:34:26 -07001171 if (format == NULL) {
Andreas Huberf9334412010-12-15 15:17:42 -08001172 return -EWOULDBLOCK;
1173 }
1174
Andreas Huber3fe62152011-09-16 15:09:22 -07001175 if (!audio) {
Andreas Huber84066782011-08-16 09:34:26 -07001176 AString mime;
1177 CHECK(format->findString("mime", &mime));
1178 mVideoIsAVC = !strcasecmp(MEDIA_MIMETYPE_VIDEO_AVC, mime.c_str());
Chong Zhanga7fa1d92014-06-11 14:49:23 -07001179
1180 sp<AMessage> ccNotify = new AMessage(kWhatClosedCaptionNotify, id());
1181 mCCDecoder = new CCDecoder(ccNotify);
Lajos Molnar09524832014-07-17 14:29:51 -07001182
1183 if (mSourceFlags & Source::FLAG_SECURE) {
1184 format->setInt32("secure", true);
1185 }
Andreas Huber3fe62152011-09-16 15:09:22 -07001186 }
1187
Wei Jiabc2fb722014-07-08 16:37:57 -07001188 if (audio) {
Wei Jia88703c32014-08-06 11:24:07 -07001189 sp<AMessage> notify = new AMessage(kWhatAudioNotify, id());
1190 ++mAudioDecoderGeneration;
1191 notify->setInt32("generation", mAudioDecoderGeneration);
1192
Wei Jiabc2fb722014-07-08 16:37:57 -07001193 if (mOffloadAudio) {
Wei Jiac6cfd702014-11-11 16:33:20 -08001194 *decoder = new DecoderPassThrough(notify, mSource, mRenderer);
Wei Jiabc2fb722014-07-08 16:37:57 -07001195 } else {
Wei Jiac6cfd702014-11-11 16:33:20 -08001196 *decoder = new Decoder(notify, mSource, mRenderer);
Wei Jiabc2fb722014-07-08 16:37:57 -07001197 }
1198 } else {
Wei Jia88703c32014-08-06 11:24:07 -07001199 sp<AMessage> notify = new AMessage(kWhatVideoNotify, id());
1200 ++mVideoDecoderGeneration;
1201 notify->setInt32("generation", mVideoDecoderGeneration);
1202
Wei Jiac6cfd702014-11-11 16:33:20 -08001203 *decoder = new Decoder(notify, mSource, mRenderer, mNativeWindow);
Wei Jiabc2fb722014-07-08 16:37:57 -07001204 }
Lajos Molnar1cd13982014-01-17 15:12:51 -08001205 (*decoder)->init();
Andreas Huber84066782011-08-16 09:34:26 -07001206 (*decoder)->configure(format);
Andreas Huberf9334412010-12-15 15:17:42 -08001207
Lajos Molnar09524832014-07-17 14:29:51 -07001208 // allocate buffers to decrypt widevine source buffers
1209 if (!audio && (mSourceFlags & Source::FLAG_SECURE)) {
1210 Vector<sp<ABuffer> > inputBufs;
1211 CHECK_EQ((*decoder)->getInputBuffers(&inputBufs), (status_t)OK);
1212
1213 Vector<MediaBuffer *> mediaBufs;
1214 for (size_t i = 0; i < inputBufs.size(); i++) {
1215 const sp<ABuffer> &buffer = inputBufs[i];
1216 MediaBuffer *mbuf = new MediaBuffer(buffer->data(), buffer->size());
1217 mediaBufs.push(mbuf);
1218 }
1219
1220 status_t err = mSource->setBuffers(audio, mediaBufs);
1221 if (err != OK) {
1222 for (size_t i = 0; i < mediaBufs.size(); ++i) {
1223 mediaBufs[i]->release();
1224 }
1225 mediaBufs.clear();
1226 ALOGE("Secure source didn't support secure mediaBufs.");
1227 return err;
1228 }
1229 }
Andreas Huberf9334412010-12-15 15:17:42 -08001230 return OK;
1231}
1232
1233status_t NuPlayer::feedDecoderInputData(bool audio, const sp<AMessage> &msg) {
1234 sp<AMessage> reply;
1235 CHECK(msg->findMessage("reply", &reply));
1236
Wei Jia53904f32014-07-29 10:22:53 -07001237 if ((audio && mFlushingAudio != NONE)
Wei Jiaf702d042014-09-09 12:08:47 -07001238 || (!audio && mFlushingVideo != NONE)
1239 || mSource == NULL) {
Wei Jiab189a5b2014-08-07 06:11:39 +00001240 reply->setInt32("err", INFO_DISCONTINUITY);
1241 reply->post();
1242 return OK;
Andreas Huberf9334412010-12-15 15:17:42 -08001243 }
1244
1245 sp<ABuffer> accessUnit;
Andreas Huberf9334412010-12-15 15:17:42 -08001246
Phil Burk9f526492014-09-03 15:04:12 -07001247 // Aggregate smaller buffers into a larger buffer.
1248 // The goal is to reduce power consumption.
Phil Burk33b51b02014-09-17 16:03:47 -07001249 // Note this will not work if the decoder requires one frame per buffer.
1250 bool doBufferAggregation = (audio && mOffloadAudio);
Phil Burk9f526492014-09-03 15:04:12 -07001251 bool needMoreData = false;
Phil Burk9f526492014-09-03 15:04:12 -07001252
Andreas Huber3fe62152011-09-16 15:09:22 -07001253 bool dropAccessUnit;
1254 do {
Phil Burk9f526492014-09-03 15:04:12 -07001255 status_t err;
1256 // Did we save an accessUnit earlier because of a discontinuity?
1257 if (audio && (mPendingAudioAccessUnit != NULL)) {
1258 accessUnit = mPendingAudioAccessUnit;
1259 mPendingAudioAccessUnit.clear();
1260 err = mPendingAudioErr;
1261 ALOGV("feedDecoderInputData() use mPendingAudioAccessUnit");
1262 } else {
1263 err = mSource->dequeueAccessUnit(audio, &accessUnit);
1264 }
Andreas Huber5bc087c2010-12-23 10:27:40 -08001265
Andreas Huber3fe62152011-09-16 15:09:22 -07001266 if (err == -EWOULDBLOCK) {
Phil Burkc5cc2e22014-09-09 20:08:39 -07001267 return err;
Andreas Huber3fe62152011-09-16 15:09:22 -07001268 } else if (err != OK) {
1269 if (err == INFO_DISCONTINUITY) {
Phil Burk33b51b02014-09-17 16:03:47 -07001270 if (doBufferAggregation && (mAggregateBuffer != NULL)) {
Phil Burk9f526492014-09-03 15:04:12 -07001271 // We already have some data so save this for later.
1272 mPendingAudioErr = err;
1273 mPendingAudioAccessUnit = accessUnit;
1274 accessUnit.clear();
1275 ALOGD("feedDecoderInputData() save discontinuity for later");
1276 break;
1277 }
Andreas Huber3fe62152011-09-16 15:09:22 -07001278 int32_t type;
1279 CHECK(accessUnit->meta()->findInt32("discontinuity", &type));
Andreas Huber53df1a42010-12-22 10:03:04 -08001280
Andreas Huber3fe62152011-09-16 15:09:22 -07001281 bool formatChange =
Andreas Huber6e3d3112011-11-28 12:36:11 -08001282 (audio &&
1283 (type & ATSParser::DISCONTINUITY_AUDIO_FORMAT))
1284 || (!audio &&
1285 (type & ATSParser::DISCONTINUITY_VIDEO_FORMAT));
Andreas Huber53df1a42010-12-22 10:03:04 -08001286
Andreas Huber6e3d3112011-11-28 12:36:11 -08001287 bool timeChange = (type & ATSParser::DISCONTINUITY_TIME) != 0;
1288
Steve Blockdf64d152012-01-04 20:05:49 +00001289 ALOGI("%s discontinuity (formatChange=%d, time=%d)",
Andreas Huber6e3d3112011-11-28 12:36:11 -08001290 audio ? "audio" : "video", formatChange, timeChange);
Andreas Huber32f3cef2011-03-02 15:34:46 -08001291
Andreas Huber6e3d3112011-11-28 12:36:11 -08001292 mTimeDiscontinuityPending =
1293 mTimeDiscontinuityPending || timeChange;
1294
Lajos Molnar87603c02014-08-20 19:25:30 -07001295 bool seamlessFormatChange = false;
1296 sp<AMessage> newFormat = mSource->getFormat(audio);
1297 if (formatChange) {
1298 seamlessFormatChange =
1299 getDecoder(audio)->supportsSeamlessFormatChange(newFormat);
1300 // treat seamless format change separately
1301 formatChange = !seamlessFormatChange;
1302 }
1303 bool shutdownOrFlush = formatChange || timeChange;
1304
1305 // We want to queue up scan-sources only once per discontinuity.
1306 // We control this by doing it only if neither audio nor video are
1307 // flushing or shutting down. (After handling 1st discontinuity, one
1308 // of the flushing states will not be NONE.)
1309 // No need to scan sources if this discontinuity does not result
1310 // in a flush or shutdown, as the flushing state will stay NONE.
1311 if (mFlushingAudio == NONE && mFlushingVideo == NONE &&
1312 shutdownOrFlush) {
Robert Shiha2981012014-07-30 17:41:24 -07001313 // And we'll resume scanning sources once we're done
1314 // flushing.
1315 mDeferredActions.push_front(
1316 new SimpleAction(
1317 &NuPlayer::performScanSources));
1318 }
1319
Lajos Molnar87603c02014-08-20 19:25:30 -07001320 if (formatChange /* not seamless */) {
1321 // must change decoder
1322 flushDecoder(audio, /* needShutdown = */ true);
1323 } else if (timeChange) {
1324 // need to flush
1325 flushDecoder(audio, /* needShutdown = */ false, newFormat);
1326 err = OK;
1327 } else if (seamlessFormatChange) {
1328 // reuse existing decoder and don't flush
1329 updateDecoderFormatWithoutFlush(audio, newFormat);
1330 err = OK;
Andreas Huber6e3d3112011-11-28 12:36:11 -08001331 } else {
1332 // This stream is unaffected by the discontinuity
Andreas Huber6e3d3112011-11-28 12:36:11 -08001333 return -EWOULDBLOCK;
1334 }
Andreas Huber32f3cef2011-03-02 15:34:46 -08001335 }
1336
Andreas Huber3fe62152011-09-16 15:09:22 -07001337 reply->setInt32("err", err);
1338 reply->post();
1339 return OK;
Andreas Huberf9334412010-12-15 15:17:42 -08001340 }
1341
Andreas Huber3fe62152011-09-16 15:09:22 -07001342 if (!audio) {
1343 ++mNumFramesTotal;
1344 }
1345
1346 dropAccessUnit = false;
1347 if (!audio
Lajos Molnar09524832014-07-17 14:29:51 -07001348 && !(mSourceFlags & Source::FLAG_SECURE)
Ronghua Wua73d9e02014-10-08 15:13:29 -07001349 && mRenderer->getVideoLateByUs() > 100000ll
Andreas Huber3fe62152011-09-16 15:09:22 -07001350 && mVideoIsAVC
1351 && !IsAVCReferenceFrame(accessUnit)) {
1352 dropAccessUnit = true;
1353 ++mNumFramesDropped;
1354 }
Phil Burk9f526492014-09-03 15:04:12 -07001355
1356 size_t smallSize = accessUnit->size();
1357 needMoreData = false;
Phil Burkc5cc2e22014-09-09 20:08:39 -07001358 if (doBufferAggregation && (mAggregateBuffer == NULL)
Phil Burk9f526492014-09-03 15:04:12 -07001359 // Don't bother if only room for a few small buffers.
Phil Burkc5cc2e22014-09-09 20:08:39 -07001360 && (smallSize < (kAggregateBufferSizeBytes / 3))) {
Phil Burk9f526492014-09-03 15:04:12 -07001361 // Create a larger buffer for combining smaller buffers from the extractor.
Phil Burkc5cc2e22014-09-09 20:08:39 -07001362 mAggregateBuffer = new ABuffer(kAggregateBufferSizeBytes);
1363 mAggregateBuffer->setRange(0, 0); // start empty
Phil Burk9f526492014-09-03 15:04:12 -07001364 }
1365
Phil Burk33b51b02014-09-17 16:03:47 -07001366 if (doBufferAggregation && (mAggregateBuffer != NULL)) {
Phil Burk9f526492014-09-03 15:04:12 -07001367 int64_t timeUs;
Phil Burkc5cc2e22014-09-09 20:08:39 -07001368 int64_t dummy;
Phil Burk9f526492014-09-03 15:04:12 -07001369 bool smallTimestampValid = accessUnit->meta()->findInt64("timeUs", &timeUs);
Phil Burkc5cc2e22014-09-09 20:08:39 -07001370 bool bigTimestampValid = mAggregateBuffer->meta()->findInt64("timeUs", &dummy);
Phil Burk9f526492014-09-03 15:04:12 -07001371 // Will the smaller buffer fit?
Phil Burkc5cc2e22014-09-09 20:08:39 -07001372 size_t bigSize = mAggregateBuffer->size();
1373 size_t roomLeft = mAggregateBuffer->capacity() - bigSize;
Phil Burk9f526492014-09-03 15:04:12 -07001374 // Should we save this small buffer for the next big buffer?
1375 // If the first small buffer did not have a timestamp then save
1376 // any buffer that does have a timestamp until the next big buffer.
1377 if ((smallSize > roomLeft)
Phil Burkc5cc2e22014-09-09 20:08:39 -07001378 || (!bigTimestampValid && (bigSize > 0) && smallTimestampValid)) {
Phil Burk9f526492014-09-03 15:04:12 -07001379 mPendingAudioErr = err;
1380 mPendingAudioAccessUnit = accessUnit;
1381 accessUnit.clear();
1382 } else {
Phil Burkc5cc2e22014-09-09 20:08:39 -07001383 // Grab time from first small buffer if available.
1384 if ((bigSize == 0) && smallTimestampValid) {
1385 mAggregateBuffer->meta()->setInt64("timeUs", timeUs);
1386 }
Phil Burk9f526492014-09-03 15:04:12 -07001387 // Append small buffer to the bigger buffer.
Phil Burkc5cc2e22014-09-09 20:08:39 -07001388 memcpy(mAggregateBuffer->base() + bigSize, accessUnit->data(), smallSize);
Phil Burk9f526492014-09-03 15:04:12 -07001389 bigSize += smallSize;
Phil Burkc5cc2e22014-09-09 20:08:39 -07001390 mAggregateBuffer->setRange(0, bigSize);
Phil Burk9f526492014-09-03 15:04:12 -07001391
Phil Burkc5cc2e22014-09-09 20:08:39 -07001392 // Keep looping until we run out of room in the mAggregateBuffer.
Phil Burk9f526492014-09-03 15:04:12 -07001393 needMoreData = true;
1394
Phil Burkc5cc2e22014-09-09 20:08:39 -07001395 ALOGV("feedDecoderInputData() smallSize = %zu, bigSize = %zu, capacity = %zu",
1396 smallSize, bigSize, mAggregateBuffer->capacity());
Phil Burk9f526492014-09-03 15:04:12 -07001397 }
1398 }
1399 } while (dropAccessUnit || needMoreData);
Andreas Huberf9334412010-12-15 15:17:42 -08001400
Steve Block3856b092011-10-20 11:56:00 +01001401 // ALOGV("returned a valid buffer of %s data", audio ? "audio" : "video");
Andreas Huberf9334412010-12-15 15:17:42 -08001402
1403#if 0
1404 int64_t mediaTimeUs;
1405 CHECK(accessUnit->meta()->findInt64("timeUs", &mediaTimeUs));
Steve Block3856b092011-10-20 11:56:00 +01001406 ALOGV("feeding %s input buffer at media time %.2f secs",
Andreas Huberf9334412010-12-15 15:17:42 -08001407 audio ? "audio" : "video",
1408 mediaTimeUs / 1E6);
1409#endif
1410
Chong Zhanga7fa1d92014-06-11 14:49:23 -07001411 if (!audio) {
1412 mCCDecoder->decode(accessUnit);
1413 }
1414
Phil Burk33b51b02014-09-17 16:03:47 -07001415 if (doBufferAggregation && (mAggregateBuffer != NULL)) {
Phil Burkc5cc2e22014-09-09 20:08:39 -07001416 ALOGV("feedDecoderInputData() reply with aggregated buffer, %zu",
1417 mAggregateBuffer->size());
1418 reply->setBuffer("buffer", mAggregateBuffer);
1419 mAggregateBuffer.clear();
Phil Burk9f526492014-09-03 15:04:12 -07001420 } else {
1421 reply->setBuffer("buffer", accessUnit);
1422 }
1423
Andreas Huberf9334412010-12-15 15:17:42 -08001424 reply->post();
1425
1426 return OK;
1427}
1428
1429void NuPlayer::renderBuffer(bool audio, const sp<AMessage> &msg) {
Steve Block3856b092011-10-20 11:56:00 +01001430 // ALOGV("renderBuffer %s", audio ? "audio" : "video");
Andreas Huberf9334412010-12-15 15:17:42 -08001431
Wei Jia53904f32014-07-29 10:22:53 -07001432 if ((audio && mFlushingAudio != NONE)
1433 || (!audio && mFlushingVideo != NONE)) {
Andreas Huber18ac5402011-08-31 15:04:25 -07001434 // We're currently attempting to flush the decoder, in order
1435 // to complete this, the decoder wants all its buffers back,
1436 // so we don't want any output buffers it sent us (from before
1437 // we initiated the flush) to be stuck in the renderer's queue.
1438
Steve Block3856b092011-10-20 11:56:00 +01001439 ALOGV("we're still flushing the %s decoder, sending its output buffer"
Andreas Huber18ac5402011-08-31 15:04:25 -07001440 " right back.", audio ? "audio" : "video");
1441
Andreas Huber18ac5402011-08-31 15:04:25 -07001442 return;
1443 }
1444
Chong Zhanga7fa1d92014-06-11 14:49:23 -07001445 int64_t mediaTimeUs;
Wei Jiac6cfd702014-11-11 16:33:20 -08001446 CHECK(msg->findInt64("timeUs", &mediaTimeUs));
Andreas Huber32f3cef2011-03-02 15:34:46 -08001447
Chong Zhanga7fa1d92014-06-11 14:49:23 -07001448 if (!audio && mCCDecoder->isSelected()) {
1449 mCCDecoder->display(mediaTimeUs);
1450 }
Andreas Huberf9334412010-12-15 15:17:42 -08001451}
1452
Chong Zhangced1c2f2014-08-08 15:22:35 -07001453void NuPlayer::updateVideoSize(
1454 const sp<AMessage> &inputFormat,
1455 const sp<AMessage> &outputFormat) {
1456 if (inputFormat == NULL) {
1457 ALOGW("Unknown video size, reporting 0x0!");
1458 notifyListener(MEDIA_SET_VIDEO_SIZE, 0, 0);
1459 return;
1460 }
1461
1462 int32_t displayWidth, displayHeight;
1463 int32_t cropLeft, cropTop, cropRight, cropBottom;
1464
1465 if (outputFormat != NULL) {
1466 int32_t width, height;
1467 CHECK(outputFormat->findInt32("width", &width));
1468 CHECK(outputFormat->findInt32("height", &height));
1469
1470 int32_t cropLeft, cropTop, cropRight, cropBottom;
1471 CHECK(outputFormat->findRect(
1472 "crop",
1473 &cropLeft, &cropTop, &cropRight, &cropBottom));
1474
1475 displayWidth = cropRight - cropLeft + 1;
1476 displayHeight = cropBottom - cropTop + 1;
1477
1478 ALOGV("Video output format changed to %d x %d "
1479 "(crop: %d x %d @ (%d, %d))",
1480 width, height,
1481 displayWidth,
1482 displayHeight,
1483 cropLeft, cropTop);
1484 } else {
1485 CHECK(inputFormat->findInt32("width", &displayWidth));
1486 CHECK(inputFormat->findInt32("height", &displayHeight));
1487
1488 ALOGV("Video input format %d x %d", displayWidth, displayHeight);
1489 }
1490
1491 // Take into account sample aspect ratio if necessary:
1492 int32_t sarWidth, sarHeight;
1493 if (inputFormat->findInt32("sar-width", &sarWidth)
1494 && inputFormat->findInt32("sar-height", &sarHeight)) {
1495 ALOGV("Sample aspect ratio %d : %d", sarWidth, sarHeight);
1496
1497 displayWidth = (displayWidth * sarWidth) / sarHeight;
1498
1499 ALOGV("display dimensions %d x %d", displayWidth, displayHeight);
1500 }
1501
1502 int32_t rotationDegrees;
1503 if (!inputFormat->findInt32("rotation-degrees", &rotationDegrees)) {
1504 rotationDegrees = 0;
1505 }
1506
1507 if (rotationDegrees == 90 || rotationDegrees == 270) {
1508 int32_t tmp = displayWidth;
1509 displayWidth = displayHeight;
1510 displayHeight = tmp;
1511 }
1512
1513 notifyListener(
1514 MEDIA_SET_VIDEO_SIZE,
1515 displayWidth,
1516 displayHeight);
1517}
1518
Chong Zhangdcb89b32013-08-06 09:44:47 -07001519void NuPlayer::notifyListener(int msg, int ext1, int ext2, const Parcel *in) {
Andreas Huber43c3e6c2011-01-05 12:17:08 -08001520 if (mDriver == NULL) {
Andreas Huberf9334412010-12-15 15:17:42 -08001521 return;
1522 }
1523
Andreas Huber43c3e6c2011-01-05 12:17:08 -08001524 sp<NuPlayerDriver> driver = mDriver.promote();
Andreas Huberf9334412010-12-15 15:17:42 -08001525
Andreas Huber43c3e6c2011-01-05 12:17:08 -08001526 if (driver == NULL) {
Andreas Huberf9334412010-12-15 15:17:42 -08001527 return;
1528 }
1529
Chong Zhangdcb89b32013-08-06 09:44:47 -07001530 driver->notifyListener(msg, ext1, ext2, in);
Andreas Huberf9334412010-12-15 15:17:42 -08001531}
1532
Lajos Molnar87603c02014-08-20 19:25:30 -07001533void NuPlayer::flushDecoder(
1534 bool audio, bool needShutdown, const sp<AMessage> &newFormat) {
Andreas Huber14f76722013-01-15 09:04:18 -08001535 ALOGV("[%s] flushDecoder needShutdown=%d",
1536 audio ? "audio" : "video", needShutdown);
1537
Lajos Molnar87603c02014-08-20 19:25:30 -07001538 const sp<Decoder> &decoder = getDecoder(audio);
1539 if (decoder == NULL) {
Steve Blockdf64d152012-01-04 20:05:49 +00001540 ALOGI("flushDecoder %s without decoder present",
Andreas Huber6e3d3112011-11-28 12:36:11 -08001541 audio ? "audio" : "video");
Lajos Molnar87603c02014-08-20 19:25:30 -07001542 return;
Andreas Huber6e3d3112011-11-28 12:36:11 -08001543 }
1544
Andreas Huber1aef2112011-01-04 14:01:29 -08001545 // Make sure we don't continue to scan sources until we finish flushing.
1546 ++mScanSourcesGeneration;
Andreas Huber43c3e6c2011-01-05 12:17:08 -08001547 mScanSourcesPending = false;
Andreas Huber1aef2112011-01-04 14:01:29 -08001548
Lajos Molnar87603c02014-08-20 19:25:30 -07001549 decoder->signalFlush(newFormat);
Andreas Huber1aef2112011-01-04 14:01:29 -08001550
1551 FlushStatus newStatus =
1552 needShutdown ? FLUSHING_DECODER_SHUTDOWN : FLUSHING_DECODER;
1553
Andy Hung8d121d42014-10-03 09:53:53 -07001554 mFlushComplete[audio][false /* isDecoder */] = false;
1555 mFlushComplete[audio][true /* isDecoder */] = false;
Andreas Huber1aef2112011-01-04 14:01:29 -08001556 if (audio) {
Wei Jia53904f32014-07-29 10:22:53 -07001557 ALOGE_IF(mFlushingAudio != NONE,
1558 "audio flushDecoder() is called in state %d", mFlushingAudio);
Andreas Huber1aef2112011-01-04 14:01:29 -08001559 mFlushingAudio = newStatus;
Andreas Huber1aef2112011-01-04 14:01:29 -08001560 } else {
Wei Jia53904f32014-07-29 10:22:53 -07001561 ALOGE_IF(mFlushingVideo != NONE,
1562 "video flushDecoder() is called in state %d", mFlushingVideo);
Andreas Huber1aef2112011-01-04 14:01:29 -08001563 mFlushingVideo = newStatus;
Chong Zhangb86e68f2014-08-01 13:46:53 -07001564
1565 if (mCCDecoder != NULL) {
1566 mCCDecoder->flush();
1567 }
Andreas Huber1aef2112011-01-04 14:01:29 -08001568 }
1569}
1570
Lajos Molnar87603c02014-08-20 19:25:30 -07001571void NuPlayer::updateDecoderFormatWithoutFlush(
1572 bool audio, const sp<AMessage> &format) {
1573 ALOGV("[%s] updateDecoderFormatWithoutFlush", audio ? "audio" : "video");
1574
1575 const sp<Decoder> &decoder = getDecoder(audio);
1576 if (decoder == NULL) {
1577 ALOGI("updateDecoderFormatWithoutFlush %s without decoder present",
1578 audio ? "audio" : "video");
1579 return;
1580 }
1581
1582 decoder->signalUpdateFormat(format);
1583}
1584
Chong Zhangced1c2f2014-08-08 15:22:35 -07001585void NuPlayer::queueDecoderShutdown(
1586 bool audio, bool video, const sp<AMessage> &reply) {
1587 ALOGI("queueDecoderShutdown audio=%d, video=%d", audio, video);
Andreas Huber84066782011-08-16 09:34:26 -07001588
Chong Zhangced1c2f2014-08-08 15:22:35 -07001589 mDeferredActions.push_back(
Wei Jiafef808d2014-10-31 17:57:05 -07001590 new FlushDecoderAction(
1591 audio ? FLUSH_CMD_SHUTDOWN : FLUSH_CMD_NONE,
1592 video ? FLUSH_CMD_SHUTDOWN : FLUSH_CMD_NONE));
Andreas Huber84066782011-08-16 09:34:26 -07001593
Chong Zhangced1c2f2014-08-08 15:22:35 -07001594 mDeferredActions.push_back(
1595 new SimpleAction(&NuPlayer::performScanSources));
Andreas Huber84066782011-08-16 09:34:26 -07001596
Chong Zhangced1c2f2014-08-08 15:22:35 -07001597 mDeferredActions.push_back(new PostMessageAction(reply));
1598
1599 processDeferredActions();
Andreas Huber84066782011-08-16 09:34:26 -07001600}
1601
James Dong0d268a32012-08-31 12:18:27 -07001602status_t NuPlayer::setVideoScalingMode(int32_t mode) {
1603 mVideoScalingMode = mode;
Andreas Huber57a339c2012-12-03 11:18:00 -08001604 if (mNativeWindow != NULL) {
James Dong0d268a32012-08-31 12:18:27 -07001605 status_t ret = native_window_set_scaling_mode(
1606 mNativeWindow->getNativeWindow().get(), mVideoScalingMode);
1607 if (ret != OK) {
1608 ALOGE("Failed to set scaling mode (%d): %s",
1609 -ret, strerror(-ret));
1610 return ret;
1611 }
1612 }
1613 return OK;
1614}
1615
Chong Zhangdcb89b32013-08-06 09:44:47 -07001616status_t NuPlayer::getTrackInfo(Parcel* reply) const {
1617 sp<AMessage> msg = new AMessage(kWhatGetTrackInfo, id());
1618 msg->setPointer("reply", reply);
1619
1620 sp<AMessage> response;
1621 status_t err = msg->postAndAwaitResponse(&response);
1622 return err;
1623}
1624
Robert Shih7c4f0d72014-07-09 18:53:31 -07001625status_t NuPlayer::getSelectedTrack(int32_t type, Parcel* reply) const {
1626 sp<AMessage> msg = new AMessage(kWhatGetSelectedTrack, id());
1627 msg->setPointer("reply", reply);
1628 msg->setInt32("type", type);
1629
1630 sp<AMessage> response;
1631 status_t err = msg->postAndAwaitResponse(&response);
1632 if (err == OK && response != NULL) {
1633 CHECK(response->findInt32("err", &err));
1634 }
1635 return err;
1636}
1637
Robert Shih6ffb1fd2014-10-29 16:24:32 -07001638status_t NuPlayer::selectTrack(size_t trackIndex, bool select, int64_t timeUs) {
Chong Zhangdcb89b32013-08-06 09:44:47 -07001639 sp<AMessage> msg = new AMessage(kWhatSelectTrack, id());
1640 msg->setSize("trackIndex", trackIndex);
1641 msg->setInt32("select", select);
Robert Shih6ffb1fd2014-10-29 16:24:32 -07001642 msg->setInt64("timeUs", timeUs);
Chong Zhangdcb89b32013-08-06 09:44:47 -07001643
1644 sp<AMessage> response;
1645 status_t err = msg->postAndAwaitResponse(&response);
1646
Chong Zhang404fced2014-06-11 14:45:31 -07001647 if (err != OK) {
1648 return err;
1649 }
1650
1651 if (!response->findInt32("err", &err)) {
1652 err = OK;
1653 }
1654
Chong Zhangdcb89b32013-08-06 09:44:47 -07001655 return err;
1656}
1657
Ronghua Wua73d9e02014-10-08 15:13:29 -07001658status_t NuPlayer::getCurrentPosition(int64_t *mediaUs) {
1659 sp<Renderer> renderer = mRenderer;
1660 if (renderer == NULL) {
1661 return NO_INIT;
1662 }
1663
1664 return renderer->getCurrentPosition(mediaUs);
1665}
1666
1667void NuPlayer::getStats(int64_t *numFramesTotal, int64_t *numFramesDropped) {
1668 *numFramesTotal = mNumFramesTotal;
1669 *numFramesDropped = mNumFramesDropped;
1670}
1671
Marco Nelissenf0b72b52014-09-16 15:43:44 -07001672sp<MetaData> NuPlayer::getFileMeta() {
1673 return mSource->getFileFormatMeta();
1674}
1675
Andreas Huberb7c8e912012-11-27 15:02:53 -08001676void NuPlayer::schedulePollDuration() {
1677 sp<AMessage> msg = new AMessage(kWhatPollDuration, id());
1678 msg->setInt32("generation", mPollDurationGeneration);
1679 msg->post();
1680}
1681
1682void NuPlayer::cancelPollDuration() {
1683 ++mPollDurationGeneration;
1684}
1685
Andreas Hubera1f8ab02012-11-30 10:53:22 -08001686void NuPlayer::processDeferredActions() {
1687 while (!mDeferredActions.empty()) {
1688 // We won't execute any deferred actions until we're no longer in
1689 // an intermediate state, i.e. one more more decoders are currently
1690 // flushing or shutting down.
1691
Andreas Hubera1f8ab02012-11-30 10:53:22 -08001692 if (mFlushingAudio != NONE || mFlushingVideo != NONE) {
1693 // We're currently flushing, postpone the reset until that's
1694 // completed.
1695
1696 ALOGV("postponing action mFlushingAudio=%d, mFlushingVideo=%d",
1697 mFlushingAudio, mFlushingVideo);
1698
1699 break;
1700 }
1701
1702 sp<Action> action = *mDeferredActions.begin();
1703 mDeferredActions.erase(mDeferredActions.begin());
1704
1705 action->execute(this);
1706 }
1707}
1708
Wei Jiae427abf2014-09-22 15:21:11 -07001709void NuPlayer::performSeek(int64_t seekTimeUs, bool needNotify) {
1710 ALOGV("performSeek seekTimeUs=%lld us (%.2f secs), needNotify(%d)",
Andreas Hubera1f8ab02012-11-30 10:53:22 -08001711 seekTimeUs,
Wei Jiae427abf2014-09-22 15:21:11 -07001712 seekTimeUs / 1E6,
1713 needNotify);
Andreas Hubera1f8ab02012-11-30 10:53:22 -08001714
Andy Hungadf34bf2014-09-03 18:22:22 -07001715 if (mSource == NULL) {
1716 // This happens when reset occurs right before the loop mode
1717 // asynchronously seeks to the start of the stream.
1718 LOG_ALWAYS_FATAL_IF(mAudioDecoder != NULL || mVideoDecoder != NULL,
1719 "mSource is NULL and decoders not NULL audio(%p) video(%p)",
1720 mAudioDecoder.get(), mVideoDecoder.get());
1721 return;
1722 }
Andreas Hubera1f8ab02012-11-30 10:53:22 -08001723 mSource->seekTo(seekTimeUs);
Robert Shihd3b0bbb2014-07-23 15:00:25 -07001724 ++mTimedTextGeneration;
Andreas Hubera1f8ab02012-11-30 10:53:22 -08001725
1726 if (mDriver != NULL) {
1727 sp<NuPlayerDriver> driver = mDriver.promote();
1728 if (driver != NULL) {
Wei Jiae427abf2014-09-22 15:21:11 -07001729 if (needNotify) {
1730 driver->notifySeekComplete();
1731 }
Andreas Hubera1f8ab02012-11-30 10:53:22 -08001732 }
1733 }
1734
1735 // everything's flushed, continue playback.
1736}
1737
Wei Jiafef808d2014-10-31 17:57:05 -07001738void NuPlayer::performDecoderFlush(FlushCommand audio, FlushCommand video) {
1739 ALOGV("performDecoderFlush audio=%d, video=%d", audio, video);
Andreas Hubera1f8ab02012-11-30 10:53:22 -08001740
Wei Jiafef808d2014-10-31 17:57:05 -07001741 if ((audio == FLUSH_CMD_NONE || mAudioDecoder == NULL)
1742 && (video == FLUSH_CMD_NONE || mVideoDecoder == NULL)) {
Andreas Hubera1f8ab02012-11-30 10:53:22 -08001743 return;
1744 }
1745
1746 mTimeDiscontinuityPending = true;
1747
Wei Jiafef808d2014-10-31 17:57:05 -07001748 if (audio != FLUSH_CMD_NONE && mAudioDecoder != NULL) {
1749 flushDecoder(true /* audio */, (audio == FLUSH_CMD_SHUTDOWN));
Andreas Hubera1f8ab02012-11-30 10:53:22 -08001750 }
1751
Wei Jiafef808d2014-10-31 17:57:05 -07001752 if (video != FLUSH_CMD_NONE && mVideoDecoder != NULL) {
1753 flushDecoder(false /* audio */, (video == FLUSH_CMD_SHUTDOWN));
Andreas Hubera1f8ab02012-11-30 10:53:22 -08001754 }
1755}
1756
1757void NuPlayer::performReset() {
1758 ALOGV("performReset");
1759
1760 CHECK(mAudioDecoder == NULL);
1761 CHECK(mVideoDecoder == NULL);
1762
1763 cancelPollDuration();
1764
1765 ++mScanSourcesGeneration;
1766 mScanSourcesPending = false;
1767
Lajos Molnar09524832014-07-17 14:29:51 -07001768 if (mRendererLooper != NULL) {
1769 if (mRenderer != NULL) {
1770 mRendererLooper->unregisterHandler(mRenderer->id());
1771 }
1772 mRendererLooper->stop();
1773 mRendererLooper.clear();
1774 }
Andreas Hubera1f8ab02012-11-30 10:53:22 -08001775 mRenderer.clear();
Wei Jia57568df2014-09-22 10:16:29 -07001776 ++mRendererGeneration;
Andreas Hubera1f8ab02012-11-30 10:53:22 -08001777
1778 if (mSource != NULL) {
1779 mSource->stop();
Andreas Huberb5f25f02013-02-05 10:14:26 -08001780
Andreas Hubera1f8ab02012-11-30 10:53:22 -08001781 mSource.clear();
1782 }
1783
1784 if (mDriver != NULL) {
1785 sp<NuPlayerDriver> driver = mDriver.promote();
1786 if (driver != NULL) {
1787 driver->notifyResetComplete();
1788 }
1789 }
Andreas Huber57a339c2012-12-03 11:18:00 -08001790
1791 mStarted = false;
Andreas Hubera1f8ab02012-11-30 10:53:22 -08001792}
1793
1794void NuPlayer::performScanSources() {
1795 ALOGV("performScanSources");
1796
Andreas Huber57a339c2012-12-03 11:18:00 -08001797 if (!mStarted) {
1798 return;
1799 }
1800
Andreas Hubera1f8ab02012-11-30 10:53:22 -08001801 if (mAudioDecoder == NULL || mVideoDecoder == NULL) {
1802 postScanSources();
1803 }
1804}
1805
Andreas Huber57a339c2012-12-03 11:18:00 -08001806void NuPlayer::performSetSurface(const sp<NativeWindowWrapper> &wrapper) {
1807 ALOGV("performSetSurface");
1808
1809 mNativeWindow = wrapper;
1810
1811 // XXX - ignore error from setVideoScalingMode for now
1812 setVideoScalingMode(mVideoScalingMode);
Chong Zhang13d6faa2014-08-22 15:35:28 -07001813
1814 if (mDriver != NULL) {
1815 sp<NuPlayerDriver> driver = mDriver.promote();
1816 if (driver != NULL) {
1817 driver->notifySetSurfaceComplete();
1818 }
1819 }
Andreas Huber57a339c2012-12-03 11:18:00 -08001820}
1821
Andreas Huber9575c962013-02-05 13:59:56 -08001822void NuPlayer::onSourceNotify(const sp<AMessage> &msg) {
1823 int32_t what;
1824 CHECK(msg->findInt32("what", &what));
1825
1826 switch (what) {
1827 case Source::kWhatPrepared:
1828 {
Andreas Huberb5f28d42013-04-25 15:11:19 -07001829 if (mSource == NULL) {
1830 // This is a stale notification from a source that was
1831 // asynchronously preparing when the client called reset().
1832 // We handled the reset, the source is gone.
1833 break;
1834 }
1835
Andreas Huberec0c5972013-02-05 14:47:13 -08001836 int32_t err;
1837 CHECK(msg->findInt32("err", &err));
1838
Andreas Huber9575c962013-02-05 13:59:56 -08001839 sp<NuPlayerDriver> driver = mDriver.promote();
1840 if (driver != NULL) {
Marco Nelissendd114d12014-05-28 15:23:14 -07001841 // notify duration first, so that it's definitely set when
1842 // the app received the "prepare complete" callback.
1843 int64_t durationUs;
1844 if (mSource->getDuration(&durationUs) == OK) {
1845 driver->notifyDuration(durationUs);
1846 }
Andreas Huberec0c5972013-02-05 14:47:13 -08001847 driver->notifyPrepareCompleted(err);
Andreas Huber9575c962013-02-05 13:59:56 -08001848 }
Andreas Huber99759402013-04-01 14:28:31 -07001849
Andreas Huber9575c962013-02-05 13:59:56 -08001850 break;
1851 }
1852
1853 case Source::kWhatFlagsChanged:
1854 {
1855 uint32_t flags;
1856 CHECK(msg->findInt32("flags", (int32_t *)&flags));
1857
Chong Zhang4b7069d2013-09-11 12:52:43 -07001858 sp<NuPlayerDriver> driver = mDriver.promote();
1859 if (driver != NULL) {
1860 driver->notifyFlagsChanged(flags);
1861 }
1862
Andreas Huber9575c962013-02-05 13:59:56 -08001863 if ((mSourceFlags & Source::FLAG_DYNAMIC_DURATION)
1864 && (!(flags & Source::FLAG_DYNAMIC_DURATION))) {
1865 cancelPollDuration();
1866 } else if (!(mSourceFlags & Source::FLAG_DYNAMIC_DURATION)
1867 && (flags & Source::FLAG_DYNAMIC_DURATION)
1868 && (mAudioDecoder != NULL || mVideoDecoder != NULL)) {
1869 schedulePollDuration();
1870 }
1871
1872 mSourceFlags = flags;
1873 break;
1874 }
1875
1876 case Source::kWhatVideoSizeChanged:
1877 {
Chong Zhangced1c2f2014-08-08 15:22:35 -07001878 sp<AMessage> format;
1879 CHECK(msg->findMessage("format", &format));
Andreas Huber9575c962013-02-05 13:59:56 -08001880
Chong Zhangced1c2f2014-08-08 15:22:35 -07001881 updateVideoSize(format);
Andreas Huber9575c962013-02-05 13:59:56 -08001882 break;
1883 }
1884
Chong Zhang2a3cc9a2014-08-21 17:48:26 -07001885 case Source::kWhatBufferingUpdate:
1886 {
1887 int32_t percentage;
1888 CHECK(msg->findInt32("percentage", &percentage));
1889
1890 notifyListener(MEDIA_BUFFERING_UPDATE, percentage, 0);
1891 break;
1892 }
1893
Roger Jönssonb50e83e2013-01-21 16:26:41 +01001894 case Source::kWhatBufferingStart:
1895 {
1896 notifyListener(MEDIA_INFO, MEDIA_INFO_BUFFERING_START, 0);
1897 break;
1898 }
1899
1900 case Source::kWhatBufferingEnd:
1901 {
1902 notifyListener(MEDIA_INFO, MEDIA_INFO_BUFFERING_END, 0);
1903 break;
1904 }
1905
Chong Zhangdcb89b32013-08-06 09:44:47 -07001906 case Source::kWhatSubtitleData:
1907 {
1908 sp<ABuffer> buffer;
1909 CHECK(msg->findBuffer("buffer", &buffer));
1910
Chong Zhang404fced2014-06-11 14:45:31 -07001911 sendSubtitleData(buffer, 0 /* baseIndex */);
Chong Zhangdcb89b32013-08-06 09:44:47 -07001912 break;
1913 }
1914
Robert Shihd3b0bbb2014-07-23 15:00:25 -07001915 case Source::kWhatTimedTextData:
1916 {
1917 int32_t generation;
1918 if (msg->findInt32("generation", &generation)
1919 && generation != mTimedTextGeneration) {
1920 break;
1921 }
1922
1923 sp<ABuffer> buffer;
1924 CHECK(msg->findBuffer("buffer", &buffer));
1925
1926 sp<NuPlayerDriver> driver = mDriver.promote();
1927 if (driver == NULL) {
1928 break;
1929 }
1930
1931 int posMs;
1932 int64_t timeUs, posUs;
1933 driver->getCurrentPosition(&posMs);
1934 posUs = posMs * 1000;
1935 CHECK(buffer->meta()->findInt64("timeUs", &timeUs));
1936
1937 if (posUs < timeUs) {
1938 if (!msg->findInt32("generation", &generation)) {
1939 msg->setInt32("generation", mTimedTextGeneration);
1940 }
1941 msg->post(timeUs - posUs);
1942 } else {
1943 sendTimedTextData(buffer);
1944 }
1945 break;
1946 }
1947
Andreas Huber14f76722013-01-15 09:04:18 -08001948 case Source::kWhatQueueDecoderShutdown:
1949 {
1950 int32_t audio, video;
1951 CHECK(msg->findInt32("audio", &audio));
1952 CHECK(msg->findInt32("video", &video));
1953
1954 sp<AMessage> reply;
1955 CHECK(msg->findMessage("reply", &reply));
1956
1957 queueDecoderShutdown(audio, video, reply);
1958 break;
1959 }
1960
Ronghua Wu80276872014-08-28 15:50:29 -07001961 case Source::kWhatDrmNoLicense:
1962 {
1963 notifyListener(MEDIA_ERROR, MEDIA_ERROR_UNKNOWN, ERROR_DRM_NO_LICENSE);
1964 break;
1965 }
1966
Andreas Huber9575c962013-02-05 13:59:56 -08001967 default:
1968 TRESPASS();
1969 }
1970}
1971
Chong Zhanga7fa1d92014-06-11 14:49:23 -07001972void NuPlayer::onClosedCaptionNotify(const sp<AMessage> &msg) {
1973 int32_t what;
1974 CHECK(msg->findInt32("what", &what));
1975
1976 switch (what) {
1977 case NuPlayer::CCDecoder::kWhatClosedCaptionData:
1978 {
1979 sp<ABuffer> buffer;
1980 CHECK(msg->findBuffer("buffer", &buffer));
1981
1982 size_t inbandTracks = 0;
1983 if (mSource != NULL) {
1984 inbandTracks = mSource->getTrackCount();
1985 }
1986
1987 sendSubtitleData(buffer, inbandTracks);
1988 break;
1989 }
1990
1991 case NuPlayer::CCDecoder::kWhatTrackAdded:
1992 {
1993 notifyListener(MEDIA_INFO, MEDIA_INFO_METADATA_UPDATE, 0);
1994
1995 break;
1996 }
1997
1998 default:
1999 TRESPASS();
2000 }
2001
2002
2003}
2004
Chong Zhang404fced2014-06-11 14:45:31 -07002005void NuPlayer::sendSubtitleData(const sp<ABuffer> &buffer, int32_t baseIndex) {
2006 int32_t trackIndex;
2007 int64_t timeUs, durationUs;
2008 CHECK(buffer->meta()->findInt32("trackIndex", &trackIndex));
2009 CHECK(buffer->meta()->findInt64("timeUs", &timeUs));
2010 CHECK(buffer->meta()->findInt64("durationUs", &durationUs));
2011
2012 Parcel in;
2013 in.writeInt32(trackIndex + baseIndex);
2014 in.writeInt64(timeUs);
2015 in.writeInt64(durationUs);
2016 in.writeInt32(buffer->size());
2017 in.writeInt32(buffer->size());
2018 in.write(buffer->data(), buffer->size());
2019
2020 notifyListener(MEDIA_SUBTITLE_DATA, 0, 0, &in);
2021}
Robert Shihd3b0bbb2014-07-23 15:00:25 -07002022
2023void NuPlayer::sendTimedTextData(const sp<ABuffer> &buffer) {
2024 const void *data;
2025 size_t size = 0;
2026 int64_t timeUs;
2027 int32_t flag = TextDescriptions::LOCAL_DESCRIPTIONS;
2028
2029 AString mime;
2030 CHECK(buffer->meta()->findString("mime", &mime));
2031 CHECK(strcasecmp(mime.c_str(), MEDIA_MIMETYPE_TEXT_3GPP) == 0);
2032
2033 data = buffer->data();
2034 size = buffer->size();
2035
2036 Parcel parcel;
2037 if (size > 0) {
2038 CHECK(buffer->meta()->findInt64("timeUs", &timeUs));
2039 flag |= TextDescriptions::IN_BAND_TEXT_3GPP;
2040 TextDescriptions::getParcelOfDescriptions(
2041 (const uint8_t *)data, size, flag, timeUs / 1000, &parcel);
2042 }
2043
2044 if ((parcel.dataSize() > 0)) {
2045 notifyListener(MEDIA_TIMED_TEXT, 0, 0, &parcel);
2046 } else { // send an empty timed text
2047 notifyListener(MEDIA_TIMED_TEXT, 0, 0);
2048 }
2049}
Andreas Huberb5f25f02013-02-05 10:14:26 -08002050////////////////////////////////////////////////////////////////////////////////
2051
Chong Zhangced1c2f2014-08-08 15:22:35 -07002052sp<AMessage> NuPlayer::Source::getFormat(bool audio) {
2053 sp<MetaData> meta = getFormatMeta(audio);
2054
2055 if (meta == NULL) {
2056 return NULL;
2057 }
2058
2059 sp<AMessage> msg = new AMessage;
2060
2061 if(convertMetaDataToMessage(meta, &msg) == OK) {
2062 return msg;
2063 }
2064 return NULL;
2065}
2066
Andreas Huber9575c962013-02-05 13:59:56 -08002067void NuPlayer::Source::notifyFlagsChanged(uint32_t flags) {
2068 sp<AMessage> notify = dupNotify();
2069 notify->setInt32("what", kWhatFlagsChanged);
2070 notify->setInt32("flags", flags);
2071 notify->post();
2072}
2073
Chong Zhangced1c2f2014-08-08 15:22:35 -07002074void NuPlayer::Source::notifyVideoSizeChanged(const sp<AMessage> &format) {
Andreas Huber9575c962013-02-05 13:59:56 -08002075 sp<AMessage> notify = dupNotify();
2076 notify->setInt32("what", kWhatVideoSizeChanged);
Chong Zhangced1c2f2014-08-08 15:22:35 -07002077 notify->setMessage("format", format);
Andreas Huber9575c962013-02-05 13:59:56 -08002078 notify->post();
2079}
2080
Andreas Huberec0c5972013-02-05 14:47:13 -08002081void NuPlayer::Source::notifyPrepared(status_t err) {
Andreas Huber9575c962013-02-05 13:59:56 -08002082 sp<AMessage> notify = dupNotify();
2083 notify->setInt32("what", kWhatPrepared);
Andreas Huberec0c5972013-02-05 14:47:13 -08002084 notify->setInt32("err", err);
Andreas Huber9575c962013-02-05 13:59:56 -08002085 notify->post();
2086}
2087
Andreas Huber84333e02014-02-07 15:36:10 -08002088void NuPlayer::Source::onMessageReceived(const sp<AMessage> & /* msg */) {
Andreas Huberb5f25f02013-02-05 10:14:26 -08002089 TRESPASS();
2090}
2091
Andreas Huberf9334412010-12-15 15:17:42 -08002092} // namespace android