blob: d2caf6531f4edf2621496660de3cb2b02ce24257 [file] [log] [blame]
Andreas Huberf9334412010-12-15 15:17:42 -08001/*
2 * Copyright (C) 2010 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17//#define LOG_NDEBUG 0
18#define LOG_TAG "NuPlayer"
19#include <utils/Log.h>
20
21#include "NuPlayer.h"
Andreas Huber5bc087c2010-12-23 10:27:40 -080022
23#include "HTTPLiveSource.h"
Andreas Huberf9334412010-12-15 15:17:42 -080024#include "NuPlayerDecoder.h"
Wei Jiabc2fb722014-07-08 16:37:57 -070025#include "NuPlayerDecoderPassThrough.h"
Andreas Huber43c3e6c2011-01-05 12:17:08 -080026#include "NuPlayerDriver.h"
Andreas Huberf9334412010-12-15 15:17:42 -080027#include "NuPlayerRenderer.h"
Andreas Huber5bc087c2010-12-23 10:27:40 -080028#include "NuPlayerSource.h"
Andreas Huber2bfdd422011-10-11 15:24:07 -070029#include "RTSPSource.h"
Andreas Huber5bc087c2010-12-23 10:27:40 -080030#include "StreamingSource.h"
Andreas Huberafed0e12011-09-20 15:39:58 -070031#include "GenericSource.h"
Robert Shihd3b0bbb2014-07-23 15:00:25 -070032#include "TextDescriptions.h"
Andreas Huber5bc087c2010-12-23 10:27:40 -080033
34#include "ATSParser.h"
Andreas Huberf9334412010-12-15 15:17:42 -080035
Andreas Huber3831a062010-12-21 10:22:33 -080036#include <media/stagefright/foundation/hexdump.h>
Andreas Huberf9334412010-12-15 15:17:42 -080037#include <media/stagefright/foundation/ABuffer.h>
38#include <media/stagefright/foundation/ADebug.h>
39#include <media/stagefright/foundation/AMessage.h>
Lajos Molnar09524832014-07-17 14:29:51 -070040#include <media/stagefright/MediaBuffer.h>
Andreas Huber3fe62152011-09-16 15:09:22 -070041#include <media/stagefright/MediaDefs.h>
Andreas Huberf9334412010-12-15 15:17:42 -080042#include <media/stagefright/MediaErrors.h>
43#include <media/stagefright/MetaData.h>
Andy McFadden8ba01022012-12-18 09:46:54 -080044#include <gui/IGraphicBufferProducer.h>
Andreas Huberf9334412010-12-15 15:17:42 -080045
Andreas Huber3fe62152011-09-16 15:09:22 -070046#include "avc_utils.h"
47
Andreas Huber84066782011-08-16 09:34:26 -070048#include "ESDS.h"
49#include <media/stagefright/Utils.h>
50
Andreas Huberf9334412010-12-15 15:17:42 -080051namespace android {
52
Phil Burkc5cc2e22014-09-09 20:08:39 -070053// TODO optimize buffer size for power consumption
54// The offload read buffer size is 32 KB but 24 KB uses less power.
55const size_t NuPlayer::kAggregateBufferSizeBytes = 24 * 1024;
56
Andreas Hubera1f8ab02012-11-30 10:53:22 -080057struct NuPlayer::Action : public RefBase {
58 Action() {}
59
60 virtual void execute(NuPlayer *player) = 0;
61
62private:
63 DISALLOW_EVIL_CONSTRUCTORS(Action);
64};
65
66struct NuPlayer::SeekAction : public Action {
Wei Jiae427abf2014-09-22 15:21:11 -070067 SeekAction(int64_t seekTimeUs, bool needNotify)
68 : mSeekTimeUs(seekTimeUs),
69 mNeedNotify(needNotify) {
Andreas Hubera1f8ab02012-11-30 10:53:22 -080070 }
71
72 virtual void execute(NuPlayer *player) {
Wei Jiae427abf2014-09-22 15:21:11 -070073 player->performSeek(mSeekTimeUs, mNeedNotify);
Andreas Hubera1f8ab02012-11-30 10:53:22 -080074 }
75
76private:
77 int64_t mSeekTimeUs;
Wei Jiae427abf2014-09-22 15:21:11 -070078 bool mNeedNotify;
Andreas Hubera1f8ab02012-11-30 10:53:22 -080079
80 DISALLOW_EVIL_CONSTRUCTORS(SeekAction);
81};
82
Andreas Huber57a339c2012-12-03 11:18:00 -080083struct NuPlayer::SetSurfaceAction : public Action {
84 SetSurfaceAction(const sp<NativeWindowWrapper> &wrapper)
85 : mWrapper(wrapper) {
86 }
87
88 virtual void execute(NuPlayer *player) {
89 player->performSetSurface(mWrapper);
90 }
91
92private:
93 sp<NativeWindowWrapper> mWrapper;
94
95 DISALLOW_EVIL_CONSTRUCTORS(SetSurfaceAction);
96};
97
Andreas Huber14f76722013-01-15 09:04:18 -080098struct NuPlayer::ShutdownDecoderAction : public Action {
99 ShutdownDecoderAction(bool audio, bool video)
100 : mAudio(audio),
101 mVideo(video) {
102 }
103
104 virtual void execute(NuPlayer *player) {
105 player->performDecoderShutdown(mAudio, mVideo);
106 }
107
108private:
109 bool mAudio;
110 bool mVideo;
111
112 DISALLOW_EVIL_CONSTRUCTORS(ShutdownDecoderAction);
113};
114
115struct NuPlayer::PostMessageAction : public Action {
116 PostMessageAction(const sp<AMessage> &msg)
117 : mMessage(msg) {
118 }
119
120 virtual void execute(NuPlayer *) {
121 mMessage->post();
122 }
123
124private:
125 sp<AMessage> mMessage;
126
127 DISALLOW_EVIL_CONSTRUCTORS(PostMessageAction);
128};
129
Andreas Hubera1f8ab02012-11-30 10:53:22 -0800130// Use this if there's no state necessary to save in order to execute
131// the action.
132struct NuPlayer::SimpleAction : public Action {
133 typedef void (NuPlayer::*ActionFunc)();
134
135 SimpleAction(ActionFunc func)
136 : mFunc(func) {
137 }
138
139 virtual void execute(NuPlayer *player) {
140 (player->*mFunc)();
141 }
142
143private:
144 ActionFunc mFunc;
145
146 DISALLOW_EVIL_CONSTRUCTORS(SimpleAction);
147};
148
Andreas Huberf9334412010-12-15 15:17:42 -0800149////////////////////////////////////////////////////////////////////////////////
150
151NuPlayer::NuPlayer()
Andreas Huber9b80c2b2011-06-30 15:47:02 -0700152 : mUIDValid(false),
Andreas Huber9575c962013-02-05 13:59:56 -0800153 mSourceFlags(0),
Wei Jiaac428aa2014-09-02 19:01:34 -0700154 mCurrentPositionUs(0),
Andreas Huber3fe62152011-09-16 15:09:22 -0700155 mVideoIsAVC(false),
Wei Jiabc2fb722014-07-08 16:37:57 -0700156 mOffloadAudio(false),
Andy Hung282a7e32014-08-14 15:56:34 -0700157 mCurrentOffloadInfo(AUDIO_INFO_INITIALIZER),
Wei Jia88703c32014-08-06 11:24:07 -0700158 mAudioDecoderGeneration(0),
159 mVideoDecoderGeneration(0),
Andreas Huber9b80c2b2011-06-30 15:47:02 -0700160 mAudioEOS(false),
Andreas Huberf9334412010-12-15 15:17:42 -0800161 mVideoEOS(false),
Andreas Huber5bc087c2010-12-23 10:27:40 -0800162 mScanSourcesPending(false),
Andreas Huber1aef2112011-01-04 14:01:29 -0800163 mScanSourcesGeneration(0),
Andreas Huberb7c8e912012-11-27 15:02:53 -0800164 mPollDurationGeneration(0),
Robert Shihd3b0bbb2014-07-23 15:00:25 -0700165 mTimedTextGeneration(0),
Andreas Huber6e3d3112011-11-28 12:36:11 -0800166 mTimeDiscontinuityPending(false),
Andreas Huberf9334412010-12-15 15:17:42 -0800167 mFlushingAudio(NONE),
Andreas Huber1aef2112011-01-04 14:01:29 -0800168 mFlushingVideo(NONE),
Andreas Huber3fe62152011-09-16 15:09:22 -0700169 mSkipRenderingAudioUntilMediaTimeUs(-1ll),
170 mSkipRenderingVideoUntilMediaTimeUs(-1ll),
171 mVideoLateByUs(0ll),
172 mNumFramesTotal(0ll),
James Dong0d268a32012-08-31 12:18:27 -0700173 mNumFramesDropped(0ll),
Andreas Huber57a339c2012-12-03 11:18:00 -0800174 mVideoScalingMode(NATIVE_WINDOW_SCALING_MODE_SCALE_TO_WINDOW),
175 mStarted(false) {
Andreas Huberf9334412010-12-15 15:17:42 -0800176}
177
178NuPlayer::~NuPlayer() {
179}
180
Andreas Huber9b80c2b2011-06-30 15:47:02 -0700181void NuPlayer::setUID(uid_t uid) {
182 mUIDValid = true;
183 mUID = uid;
184}
185
Andreas Huber43c3e6c2011-01-05 12:17:08 -0800186void NuPlayer::setDriver(const wp<NuPlayerDriver> &driver) {
187 mDriver = driver;
Andreas Huberf9334412010-12-15 15:17:42 -0800188}
189
Andreas Huber9575c962013-02-05 13:59:56 -0800190void NuPlayer::setDataSourceAsync(const sp<IStreamSource> &source) {
Andreas Huberf9334412010-12-15 15:17:42 -0800191 sp<AMessage> msg = new AMessage(kWhatSetDataSource, id());
192
Andreas Huberb5f25f02013-02-05 10:14:26 -0800193 sp<AMessage> notify = new AMessage(kWhatSourceNotify, id());
194
Andreas Huber240abcc2014-02-13 13:32:37 -0800195 msg->setObject("source", new StreamingSource(notify, source));
Andreas Huber5bc087c2010-12-23 10:27:40 -0800196 msg->post();
197}
Andreas Huberf9334412010-12-15 15:17:42 -0800198
Andreas Huberafed0e12011-09-20 15:39:58 -0700199static bool IsHTTPLiveURL(const char *url) {
200 if (!strncasecmp("http://", url, 7)
Andreas Huber99759402013-04-01 14:28:31 -0700201 || !strncasecmp("https://", url, 8)
202 || !strncasecmp("file://", url, 7)) {
Andreas Huberafed0e12011-09-20 15:39:58 -0700203 size_t len = strlen(url);
204 if (len >= 5 && !strcasecmp(".m3u8", &url[len - 5])) {
205 return true;
206 }
207
208 if (strstr(url,"m3u8")) {
209 return true;
210 }
211 }
212
213 return false;
214}
215
Andreas Huber9575c962013-02-05 13:59:56 -0800216void NuPlayer::setDataSourceAsync(
Andreas Huber1b86fe02014-01-29 11:13:26 -0800217 const sp<IMediaHTTPService> &httpService,
218 const char *url,
219 const KeyedVector<String8, String8> *headers) {
Chong Zhang3de157d2014-08-05 20:54:44 -0700220
Andreas Huber5bc087c2010-12-23 10:27:40 -0800221 sp<AMessage> msg = new AMessage(kWhatSetDataSource, id());
Oscar Rydhé7a33b772012-02-20 10:15:48 +0100222 size_t len = strlen(url);
Andreas Huber5bc087c2010-12-23 10:27:40 -0800223
Andreas Huberb5f25f02013-02-05 10:14:26 -0800224 sp<AMessage> notify = new AMessage(kWhatSourceNotify, id());
225
Andreas Huberafed0e12011-09-20 15:39:58 -0700226 sp<Source> source;
227 if (IsHTTPLiveURL(url)) {
Andreas Huber81e68442014-02-05 11:52:33 -0800228 source = new HTTPLiveSource(notify, httpService, url, headers);
Andreas Huberafed0e12011-09-20 15:39:58 -0700229 } else if (!strncasecmp(url, "rtsp://", 7)) {
Andreas Huber1b86fe02014-01-29 11:13:26 -0800230 source = new RTSPSource(
231 notify, httpService, url, headers, mUIDValid, mUID);
Oscar Rydhé7a33b772012-02-20 10:15:48 +0100232 } else if ((!strncasecmp(url, "http://", 7)
233 || !strncasecmp(url, "https://", 8))
234 && ((len >= 4 && !strcasecmp(".sdp", &url[len - 4]))
235 || strstr(url, ".sdp?"))) {
Andreas Huber1b86fe02014-01-29 11:13:26 -0800236 source = new RTSPSource(
237 notify, httpService, url, headers, mUIDValid, mUID, true);
Andreas Huber2bfdd422011-10-11 15:24:07 -0700238 } else {
Chong Zhang3de157d2014-08-05 20:54:44 -0700239 sp<GenericSource> genericSource =
240 new GenericSource(notify, mUIDValid, mUID);
241 // Don't set FLAG_SECURE on mSourceFlags here for widevine.
242 // The correct flags will be updated in Source::kWhatFlagsChanged
243 // handler when GenericSource is prepared.
Andreas Huber2bfdd422011-10-11 15:24:07 -0700244
Chong Zhanga19f33e2014-08-07 15:35:07 -0700245 status_t err = genericSource->setDataSource(httpService, url, headers);
Chong Zhang3de157d2014-08-05 20:54:44 -0700246
247 if (err == OK) {
248 source = genericSource;
249 } else {
Chong Zhanga19f33e2014-08-07 15:35:07 -0700250 ALOGE("Failed to set data source!");
Chong Zhang3de157d2014-08-05 20:54:44 -0700251 }
252 }
Andreas Huberafed0e12011-09-20 15:39:58 -0700253 msg->setObject("source", source);
254 msg->post();
255}
256
Andreas Huber9575c962013-02-05 13:59:56 -0800257void NuPlayer::setDataSourceAsync(int fd, int64_t offset, int64_t length) {
Andreas Huberafed0e12011-09-20 15:39:58 -0700258 sp<AMessage> msg = new AMessage(kWhatSetDataSource, id());
259
Andreas Huberb5f25f02013-02-05 10:14:26 -0800260 sp<AMessage> notify = new AMessage(kWhatSourceNotify, id());
261
Chong Zhang3de157d2014-08-05 20:54:44 -0700262 sp<GenericSource> source =
263 new GenericSource(notify, mUIDValid, mUID);
264
Chong Zhanga19f33e2014-08-07 15:35:07 -0700265 status_t err = source->setDataSource(fd, offset, length);
Chong Zhang3de157d2014-08-05 20:54:44 -0700266
267 if (err != OK) {
Chong Zhanga19f33e2014-08-07 15:35:07 -0700268 ALOGE("Failed to set data source!");
Chong Zhang3de157d2014-08-05 20:54:44 -0700269 source = NULL;
270 }
271
Andreas Huberafed0e12011-09-20 15:39:58 -0700272 msg->setObject("source", source);
Andreas Huberf9334412010-12-15 15:17:42 -0800273 msg->post();
274}
275
Andreas Huber9575c962013-02-05 13:59:56 -0800276void NuPlayer::prepareAsync() {
277 (new AMessage(kWhatPrepare, id()))->post();
278}
279
Andreas Huber57a339c2012-12-03 11:18:00 -0800280void NuPlayer::setVideoSurfaceTextureAsync(
Andy McFadden8ba01022012-12-18 09:46:54 -0800281 const sp<IGraphicBufferProducer> &bufferProducer) {
Glenn Kasten11731182011-02-08 17:26:17 -0800282 sp<AMessage> msg = new AMessage(kWhatSetVideoNativeWindow, id());
Andreas Huber57a339c2012-12-03 11:18:00 -0800283
Andy McFadden8ba01022012-12-18 09:46:54 -0800284 if (bufferProducer == NULL) {
Andreas Huber57a339c2012-12-03 11:18:00 -0800285 msg->setObject("native-window", NULL);
286 } else {
287 msg->setObject(
288 "native-window",
289 new NativeWindowWrapper(
Wei Jia9c03a402014-08-26 15:24:43 -0700290 new Surface(bufferProducer, true /* controlledByApp */)));
Andreas Huber57a339c2012-12-03 11:18:00 -0800291 }
292
Andreas Huberf9334412010-12-15 15:17:42 -0800293 msg->post();
294}
295
296void NuPlayer::setAudioSink(const sp<MediaPlayerBase::AudioSink> &sink) {
297 sp<AMessage> msg = new AMessage(kWhatSetAudioSink, id());
298 msg->setObject("sink", sink);
299 msg->post();
300}
301
302void NuPlayer::start() {
303 (new AMessage(kWhatStart, id()))->post();
304}
305
Andreas Huber43c3e6c2011-01-05 12:17:08 -0800306void NuPlayer::pause() {
Andreas Huberb4082222011-01-20 15:23:04 -0800307 (new AMessage(kWhatPause, id()))->post();
Andreas Huber43c3e6c2011-01-05 12:17:08 -0800308}
309
310void NuPlayer::resume() {
Andreas Huberb4082222011-01-20 15:23:04 -0800311 (new AMessage(kWhatResume, id()))->post();
Andreas Huber43c3e6c2011-01-05 12:17:08 -0800312}
313
Andreas Huber1aef2112011-01-04 14:01:29 -0800314void NuPlayer::resetAsync() {
Chong Zhang48296b72014-09-14 14:28:45 -0700315 if (mSource != NULL) {
316 // During a reset, the data source might be unresponsive already, we need to
317 // disconnect explicitly so that reads exit promptly.
318 // We can't queue the disconnect request to the looper, as it might be
319 // queued behind a stuck read and never gets processed.
320 // Doing a disconnect outside the looper to allows the pending reads to exit
321 // (either successfully or with error).
322 mSource->disconnect();
323 }
324
Andreas Huber1aef2112011-01-04 14:01:29 -0800325 (new AMessage(kWhatReset, id()))->post();
326}
327
Wei Jiae427abf2014-09-22 15:21:11 -0700328void NuPlayer::seekToAsync(int64_t seekTimeUs, bool needNotify) {
Andreas Huber43c3e6c2011-01-05 12:17:08 -0800329 sp<AMessage> msg = new AMessage(kWhatSeek, id());
330 msg->setInt64("seekTimeUs", seekTimeUs);
Wei Jiae427abf2014-09-22 15:21:11 -0700331 msg->setInt32("needNotify", needNotify);
Andreas Huber43c3e6c2011-01-05 12:17:08 -0800332 msg->post();
333}
334
Andreas Huber53df1a42010-12-22 10:03:04 -0800335// static
Andreas Huber1aef2112011-01-04 14:01:29 -0800336bool NuPlayer::IsFlushingState(FlushStatus state, bool *needShutdown) {
Andreas Huber53df1a42010-12-22 10:03:04 -0800337 switch (state) {
338 case FLUSHING_DECODER:
Andreas Huber1aef2112011-01-04 14:01:29 -0800339 if (needShutdown != NULL) {
340 *needShutdown = false;
Andreas Huber53df1a42010-12-22 10:03:04 -0800341 }
342 return true;
343
Andreas Huber1aef2112011-01-04 14:01:29 -0800344 case FLUSHING_DECODER_SHUTDOWN:
345 if (needShutdown != NULL) {
346 *needShutdown = true;
Andreas Huber53df1a42010-12-22 10:03:04 -0800347 }
348 return true;
349
350 default:
351 return false;
352 }
353}
354
Chong Zhang404fced2014-06-11 14:45:31 -0700355void NuPlayer::writeTrackInfo(
356 Parcel* reply, const sp<AMessage> format) const {
357 int32_t trackType;
358 CHECK(format->findInt32("type", &trackType));
359
360 AString lang;
361 CHECK(format->findString("language", &lang));
362
363 reply->writeInt32(2); // write something non-zero
364 reply->writeInt32(trackType);
365 reply->writeString16(String16(lang.c_str()));
366
367 if (trackType == MEDIA_TRACK_TYPE_SUBTITLE) {
368 AString mime;
369 CHECK(format->findString("mime", &mime));
370
371 int32_t isAuto, isDefault, isForced;
372 CHECK(format->findInt32("auto", &isAuto));
373 CHECK(format->findInt32("default", &isDefault));
374 CHECK(format->findInt32("forced", &isForced));
375
376 reply->writeString16(String16(mime.c_str()));
377 reply->writeInt32(isAuto);
378 reply->writeInt32(isDefault);
379 reply->writeInt32(isForced);
380 }
381}
382
Andreas Huberf9334412010-12-15 15:17:42 -0800383void NuPlayer::onMessageReceived(const sp<AMessage> &msg) {
384 switch (msg->what()) {
385 case kWhatSetDataSource:
386 {
Steve Block3856b092011-10-20 11:56:00 +0100387 ALOGV("kWhatSetDataSource");
Andreas Huberf9334412010-12-15 15:17:42 -0800388
389 CHECK(mSource == NULL);
390
Chong Zhang3de157d2014-08-05 20:54:44 -0700391 status_t err = OK;
Andreas Huber5bc087c2010-12-23 10:27:40 -0800392 sp<RefBase> obj;
393 CHECK(msg->findObject("source", &obj));
Chong Zhang3de157d2014-08-05 20:54:44 -0700394 if (obj != NULL) {
395 mSource = static_cast<Source *>(obj.get());
Chong Zhang3de157d2014-08-05 20:54:44 -0700396 } else {
397 err = UNKNOWN_ERROR;
398 }
Andreas Huber9575c962013-02-05 13:59:56 -0800399
400 CHECK(mDriver != NULL);
401 sp<NuPlayerDriver> driver = mDriver.promote();
402 if (driver != NULL) {
Chong Zhang3de157d2014-08-05 20:54:44 -0700403 driver->notifySetDataSourceCompleted(err);
Andreas Huber9575c962013-02-05 13:59:56 -0800404 }
405 break;
406 }
407
408 case kWhatPrepare:
409 {
410 mSource->prepareAsync();
Andreas Huberf9334412010-12-15 15:17:42 -0800411 break;
412 }
413
Chong Zhangdcb89b32013-08-06 09:44:47 -0700414 case kWhatGetTrackInfo:
415 {
416 uint32_t replyID;
417 CHECK(msg->senderAwaitsResponse(&replyID));
418
Chong Zhang404fced2014-06-11 14:45:31 -0700419 Parcel* reply;
420 CHECK(msg->findPointer("reply", (void**)&reply));
421
422 size_t inbandTracks = 0;
Chong Zhangdcb89b32013-08-06 09:44:47 -0700423 if (mSource != NULL) {
Chong Zhang404fced2014-06-11 14:45:31 -0700424 inbandTracks = mSource->getTrackCount();
425 }
426
Chong Zhanga7fa1d92014-06-11 14:49:23 -0700427 size_t ccTracks = 0;
428 if (mCCDecoder != NULL) {
429 ccTracks = mCCDecoder->getTrackCount();
430 }
431
Chong Zhang404fced2014-06-11 14:45:31 -0700432 // total track count
Chong Zhanga7fa1d92014-06-11 14:49:23 -0700433 reply->writeInt32(inbandTracks + ccTracks);
Chong Zhang404fced2014-06-11 14:45:31 -0700434
435 // write inband tracks
436 for (size_t i = 0; i < inbandTracks; ++i) {
437 writeTrackInfo(reply, mSource->getTrackInfo(i));
Chong Zhangdcb89b32013-08-06 09:44:47 -0700438 }
439
Chong Zhanga7fa1d92014-06-11 14:49:23 -0700440 // write CC track
441 for (size_t i = 0; i < ccTracks; ++i) {
442 writeTrackInfo(reply, mCCDecoder->getTrackInfo(i));
443 }
444
Chong Zhangdcb89b32013-08-06 09:44:47 -0700445 sp<AMessage> response = new AMessage;
Chong Zhangdcb89b32013-08-06 09:44:47 -0700446 response->postReply(replyID);
447 break;
448 }
449
Robert Shih7c4f0d72014-07-09 18:53:31 -0700450 case kWhatGetSelectedTrack:
451 {
452 status_t err = INVALID_OPERATION;
453 if (mSource != NULL) {
454 err = OK;
455
456 int32_t type32;
457 CHECK(msg->findInt32("type", (int32_t*)&type32));
458 media_track_type type = (media_track_type)type32;
459 ssize_t selectedTrack = mSource->getSelectedTrack(type);
460
461 Parcel* reply;
462 CHECK(msg->findPointer("reply", (void**)&reply));
463 reply->writeInt32(selectedTrack);
464 }
465
466 sp<AMessage> response = new AMessage;
467 response->setInt32("err", err);
468
469 uint32_t replyID;
470 CHECK(msg->senderAwaitsResponse(&replyID));
471 response->postReply(replyID);
472 break;
473 }
474
Chong Zhangdcb89b32013-08-06 09:44:47 -0700475 case kWhatSelectTrack:
476 {
477 uint32_t replyID;
478 CHECK(msg->senderAwaitsResponse(&replyID));
479
Chong Zhang404fced2014-06-11 14:45:31 -0700480 size_t trackIndex;
481 int32_t select;
482 CHECK(msg->findSize("trackIndex", &trackIndex));
483 CHECK(msg->findInt32("select", &select));
484
Chong Zhangdcb89b32013-08-06 09:44:47 -0700485 status_t err = INVALID_OPERATION;
Chong Zhang404fced2014-06-11 14:45:31 -0700486
487 size_t inbandTracks = 0;
Chong Zhangdcb89b32013-08-06 09:44:47 -0700488 if (mSource != NULL) {
Chong Zhang404fced2014-06-11 14:45:31 -0700489 inbandTracks = mSource->getTrackCount();
490 }
Chong Zhanga7fa1d92014-06-11 14:49:23 -0700491 size_t ccTracks = 0;
492 if (mCCDecoder != NULL) {
493 ccTracks = mCCDecoder->getTrackCount();
494 }
Chong Zhang404fced2014-06-11 14:45:31 -0700495
496 if (trackIndex < inbandTracks) {
Chong Zhangdcb89b32013-08-06 09:44:47 -0700497 err = mSource->selectTrack(trackIndex, select);
Robert Shihd3b0bbb2014-07-23 15:00:25 -0700498
499 if (!select && err == OK) {
500 int32_t type;
501 sp<AMessage> info = mSource->getTrackInfo(trackIndex);
502 if (info != NULL
503 && info->findInt32("type", &type)
504 && type == MEDIA_TRACK_TYPE_TIMEDTEXT) {
505 ++mTimedTextGeneration;
506 }
507 }
Chong Zhanga7fa1d92014-06-11 14:49:23 -0700508 } else {
509 trackIndex -= inbandTracks;
510
511 if (trackIndex < ccTracks) {
512 err = mCCDecoder->selectTrack(trackIndex, select);
513 }
Chong Zhangdcb89b32013-08-06 09:44:47 -0700514 }
515
516 sp<AMessage> response = new AMessage;
517 response->setInt32("err", err);
518
519 response->postReply(replyID);
520 break;
521 }
522
Andreas Huberb7c8e912012-11-27 15:02:53 -0800523 case kWhatPollDuration:
524 {
525 int32_t generation;
526 CHECK(msg->findInt32("generation", &generation));
527
528 if (generation != mPollDurationGeneration) {
529 // stale
530 break;
531 }
532
533 int64_t durationUs;
534 if (mDriver != NULL && mSource->getDuration(&durationUs) == OK) {
535 sp<NuPlayerDriver> driver = mDriver.promote();
536 if (driver != NULL) {
537 driver->notifyDuration(durationUs);
538 }
539 }
540
541 msg->post(1000000ll); // poll again in a second.
542 break;
543 }
544
Glenn Kasten11731182011-02-08 17:26:17 -0800545 case kWhatSetVideoNativeWindow:
Andreas Huberf9334412010-12-15 15:17:42 -0800546 {
Steve Block3856b092011-10-20 11:56:00 +0100547 ALOGV("kWhatSetVideoNativeWindow");
Andreas Huberf9334412010-12-15 15:17:42 -0800548
Andreas Huber57a339c2012-12-03 11:18:00 -0800549 mDeferredActions.push_back(
Andreas Huber14f76722013-01-15 09:04:18 -0800550 new ShutdownDecoderAction(
551 false /* audio */, true /* video */));
Andreas Huber57a339c2012-12-03 11:18:00 -0800552
Andreas Huberf9334412010-12-15 15:17:42 -0800553 sp<RefBase> obj;
Glenn Kasten11731182011-02-08 17:26:17 -0800554 CHECK(msg->findObject("native-window", &obj));
Andreas Huberf9334412010-12-15 15:17:42 -0800555
Andreas Huber57a339c2012-12-03 11:18:00 -0800556 mDeferredActions.push_back(
557 new SetSurfaceAction(
558 static_cast<NativeWindowWrapper *>(obj.get())));
James Dong0d268a32012-08-31 12:18:27 -0700559
Andreas Huber57a339c2012-12-03 11:18:00 -0800560 if (obj != NULL) {
Andy Hung73535852014-09-05 11:42:58 -0700561 if (mStarted && mVideoDecoder != NULL) {
562 // Issue a seek to refresh the video screen only if started otherwise
563 // the extractor may not yet be started and will assert.
564 // If the video decoder is not set (perhaps audio only in this case)
565 // do not perform a seek as it is not needed.
Wei Jiae427abf2014-09-22 15:21:11 -0700566 mDeferredActions.push_back(
567 new SeekAction(mCurrentPositionUs, false /* needNotify */));
Andy Hung73535852014-09-05 11:42:58 -0700568 }
Wei Jiaac428aa2014-09-02 19:01:34 -0700569
Andreas Huber57a339c2012-12-03 11:18:00 -0800570 // If there is a new surface texture, instantiate decoders
571 // again if possible.
572 mDeferredActions.push_back(
573 new SimpleAction(&NuPlayer::performScanSources));
574 }
575
576 processDeferredActions();
Andreas Huberf9334412010-12-15 15:17:42 -0800577 break;
578 }
579
580 case kWhatSetAudioSink:
581 {
Steve Block3856b092011-10-20 11:56:00 +0100582 ALOGV("kWhatSetAudioSink");
Andreas Huberf9334412010-12-15 15:17:42 -0800583
584 sp<RefBase> obj;
585 CHECK(msg->findObject("sink", &obj));
586
587 mAudioSink = static_cast<MediaPlayerBase::AudioSink *>(obj.get());
588 break;
589 }
590
591 case kWhatStart:
592 {
Steve Block3856b092011-10-20 11:56:00 +0100593 ALOGV("kWhatStart");
Andreas Huber43c3e6c2011-01-05 12:17:08 -0800594
Andreas Huber3fe62152011-09-16 15:09:22 -0700595 mVideoIsAVC = false;
Wei Jiabc2fb722014-07-08 16:37:57 -0700596 mOffloadAudio = false;
Andreas Huber1aef2112011-01-04 14:01:29 -0800597 mAudioEOS = false;
598 mVideoEOS = false;
Andreas Huber32f3cef2011-03-02 15:34:46 -0800599 mSkipRenderingAudioUntilMediaTimeUs = -1;
600 mSkipRenderingVideoUntilMediaTimeUs = -1;
Andreas Huber3fe62152011-09-16 15:09:22 -0700601 mVideoLateByUs = 0;
602 mNumFramesTotal = 0;
603 mNumFramesDropped = 0;
Andreas Huber57a339c2012-12-03 11:18:00 -0800604 mStarted = true;
Andreas Huber1aef2112011-01-04 14:01:29 -0800605
Lajos Molnar09524832014-07-17 14:29:51 -0700606 /* instantiate decoders now for secure playback */
607 if (mSourceFlags & Source::FLAG_SECURE) {
608 if (mNativeWindow != NULL) {
609 instantiateDecoder(false, &mVideoDecoder);
610 }
611
612 if (mAudioSink != NULL) {
613 instantiateDecoder(true, &mAudioDecoder);
614 }
615 }
616
Andreas Huber5bc087c2010-12-23 10:27:40 -0800617 mSource->start();
Andreas Huberf9334412010-12-15 15:17:42 -0800618
Andreas Huberd5e56232013-03-12 11:01:43 -0700619 uint32_t flags = 0;
620
621 if (mSource->isRealTime()) {
622 flags |= Renderer::FLAG_REAL_TIME;
623 }
624
Wei Jiabc2fb722014-07-08 16:37:57 -0700625 sp<MetaData> audioMeta = mSource->getFormatMeta(true /* audio */);
626 audio_stream_type_t streamType = AUDIO_STREAM_MUSIC;
627 if (mAudioSink != NULL) {
628 streamType = mAudioSink->getAudioStreamType();
629 }
630
631 sp<AMessage> videoFormat = mSource->getFormat(false /* audio */);
632
633 mOffloadAudio =
634 canOffloadStream(audioMeta, (videoFormat != NULL),
635 true /* is_streaming */, streamType);
636 if (mOffloadAudio) {
637 flags |= Renderer::FLAG_OFFLOAD_AUDIO;
638 }
639
Andreas Huberf9334412010-12-15 15:17:42 -0800640 mRenderer = new Renderer(
641 mAudioSink,
Andreas Huberd5e56232013-03-12 11:01:43 -0700642 new AMessage(kWhatRendererNotify, id()),
643 flags);
Andreas Huberf9334412010-12-15 15:17:42 -0800644
Lajos Molnar09524832014-07-17 14:29:51 -0700645 mRendererLooper = new ALooper;
646 mRendererLooper->setName("NuPlayerRenderer");
647 mRendererLooper->start(false, false, ANDROID_PRIORITY_AUDIO);
648 mRendererLooper->registerHandler(mRenderer);
Andreas Huberf9334412010-12-15 15:17:42 -0800649
Lajos Molnarc851b5d2014-09-18 14:14:29 -0700650 sp<MetaData> meta = getFileMeta();
651 int32_t rate;
652 if (meta != NULL
653 && meta->findInt32(kKeyFrameRate, &rate) && rate > 0) {
654 mRenderer->setVideoFrameRate(rate);
655 }
656
Andreas Huber1aef2112011-01-04 14:01:29 -0800657 postScanSources();
Andreas Huberf9334412010-12-15 15:17:42 -0800658 break;
659 }
660
661 case kWhatScanSources:
662 {
Andreas Huber1aef2112011-01-04 14:01:29 -0800663 int32_t generation;
664 CHECK(msg->findInt32("generation", &generation));
665 if (generation != mScanSourcesGeneration) {
666 // Drop obsolete msg.
667 break;
668 }
669
Andreas Huber5bc087c2010-12-23 10:27:40 -0800670 mScanSourcesPending = false;
671
Steve Block3856b092011-10-20 11:56:00 +0100672 ALOGV("scanning sources haveAudio=%d, haveVideo=%d",
Andreas Huber43c3e6c2011-01-05 12:17:08 -0800673 mAudioDecoder != NULL, mVideoDecoder != NULL);
674
Andreas Huberb7c8e912012-11-27 15:02:53 -0800675 bool mHadAnySourcesBefore =
676 (mAudioDecoder != NULL) || (mVideoDecoder != NULL);
677
Andy Hung282a7e32014-08-14 15:56:34 -0700678 // initialize video before audio because successful initialization of
679 // video may change deep buffer mode of audio.
Haynes Mathew George5d246ef2012-07-09 10:36:57 -0700680 if (mNativeWindow != NULL) {
681 instantiateDecoder(false, &mVideoDecoder);
682 }
Andreas Huberf9334412010-12-15 15:17:42 -0800683
684 if (mAudioSink != NULL) {
Andy Hung282a7e32014-08-14 15:56:34 -0700685 if (mOffloadAudio) {
686 // open audio sink early under offload mode.
687 sp<AMessage> format = mSource->getFormat(true /*audio*/);
688 openAudioSink(format, true /*offloadOnly*/);
689 }
Andreas Huber5bc087c2010-12-23 10:27:40 -0800690 instantiateDecoder(true, &mAudioDecoder);
Andreas Huberf9334412010-12-15 15:17:42 -0800691 }
692
Andreas Huberb7c8e912012-11-27 15:02:53 -0800693 if (!mHadAnySourcesBefore
694 && (mAudioDecoder != NULL || mVideoDecoder != NULL)) {
695 // This is the first time we've found anything playable.
696
Andreas Huber9575c962013-02-05 13:59:56 -0800697 if (mSourceFlags & Source::FLAG_DYNAMIC_DURATION) {
Andreas Huberb7c8e912012-11-27 15:02:53 -0800698 schedulePollDuration();
699 }
700 }
701
Andreas Hubereac68ba2011-09-27 12:12:25 -0700702 status_t err;
703 if ((err = mSource->feedMoreTSData()) != OK) {
Andreas Huber1aef2112011-01-04 14:01:29 -0800704 if (mAudioDecoder == NULL && mVideoDecoder == NULL) {
705 // We're not currently decoding anything (no audio or
706 // video tracks found) and we just ran out of input data.
Andreas Hubereac68ba2011-09-27 12:12:25 -0700707
708 if (err == ERROR_END_OF_STREAM) {
709 notifyListener(MEDIA_PLAYBACK_COMPLETE, 0, 0);
710 } else {
711 notifyListener(MEDIA_ERROR, MEDIA_ERROR_UNKNOWN, err);
712 }
Andreas Huber1aef2112011-01-04 14:01:29 -0800713 }
Andreas Huberf9334412010-12-15 15:17:42 -0800714 break;
715 }
716
Andreas Huberfbe9d812012-08-31 14:05:27 -0700717 if ((mAudioDecoder == NULL && mAudioSink != NULL)
718 || (mVideoDecoder == NULL && mNativeWindow != NULL)) {
Andreas Huberf9334412010-12-15 15:17:42 -0800719 msg->post(100000ll);
Andreas Huber5bc087c2010-12-23 10:27:40 -0800720 mScanSourcesPending = true;
Andreas Huberf9334412010-12-15 15:17:42 -0800721 }
722 break;
723 }
724
725 case kWhatVideoNotify:
726 case kWhatAudioNotify:
727 {
728 bool audio = msg->what() == kWhatAudioNotify;
729
Wei Jia88703c32014-08-06 11:24:07 -0700730 int32_t currentDecoderGeneration =
731 (audio? mAudioDecoderGeneration : mVideoDecoderGeneration);
732 int32_t requesterGeneration = currentDecoderGeneration - 1;
733 CHECK(msg->findInt32("generation", &requesterGeneration));
734
735 if (requesterGeneration != currentDecoderGeneration) {
736 ALOGV("got message from old %s decoder, generation(%d:%d)",
737 audio ? "audio" : "video", requesterGeneration,
738 currentDecoderGeneration);
739 sp<AMessage> reply;
740 if (!(msg->findMessage("reply", &reply))) {
741 return;
742 }
743
744 reply->setInt32("err", INFO_DISCONTINUITY);
745 reply->post();
746 return;
747 }
748
Andreas Huberf9334412010-12-15 15:17:42 -0800749 int32_t what;
Lajos Molnar1cd13982014-01-17 15:12:51 -0800750 CHECK(msg->findInt32("what", &what));
Andreas Huberf9334412010-12-15 15:17:42 -0800751
Lajos Molnar1cd13982014-01-17 15:12:51 -0800752 if (what == Decoder::kWhatFillThisBuffer) {
Andreas Huberf9334412010-12-15 15:17:42 -0800753 status_t err = feedDecoderInputData(
Lajos Molnar1cd13982014-01-17 15:12:51 -0800754 audio, msg);
Andreas Huberf9334412010-12-15 15:17:42 -0800755
Andreas Huber5bc087c2010-12-23 10:27:40 -0800756 if (err == -EWOULDBLOCK) {
Andreas Hubereac68ba2011-09-27 12:12:25 -0700757 if (mSource->feedMoreTSData() == OK) {
Phil Burkc5cc2e22014-09-09 20:08:39 -0700758 msg->post(10 * 1000ll);
Andreas Huber5bc087c2010-12-23 10:27:40 -0800759 }
Andreas Huberf9334412010-12-15 15:17:42 -0800760 }
Lajos Molnar1cd13982014-01-17 15:12:51 -0800761 } else if (what == Decoder::kWhatEOS) {
Andreas Huberdc9bacd2011-09-26 10:53:29 -0700762 int32_t err;
Lajos Molnar1cd13982014-01-17 15:12:51 -0800763 CHECK(msg->findInt32("err", &err));
Andreas Huberdc9bacd2011-09-26 10:53:29 -0700764
765 if (err == ERROR_END_OF_STREAM) {
Steve Block3856b092011-10-20 11:56:00 +0100766 ALOGV("got %s decoder EOS", audio ? "audio" : "video");
Andreas Huberdc9bacd2011-09-26 10:53:29 -0700767 } else {
Steve Block3856b092011-10-20 11:56:00 +0100768 ALOGV("got %s decoder EOS w/ error %d",
Andreas Huberdc9bacd2011-09-26 10:53:29 -0700769 audio ? "audio" : "video",
770 err);
771 }
772
773 mRenderer->queueEOS(audio, err);
Lajos Molnar1cd13982014-01-17 15:12:51 -0800774 } else if (what == Decoder::kWhatFlushCompleted) {
Andreas Huber1aef2112011-01-04 14:01:29 -0800775 bool needShutdown;
Andreas Huber53df1a42010-12-22 10:03:04 -0800776
Andreas Huberf9334412010-12-15 15:17:42 -0800777 if (audio) {
Andreas Huber1aef2112011-01-04 14:01:29 -0800778 CHECK(IsFlushingState(mFlushingAudio, &needShutdown));
Andreas Huberf9334412010-12-15 15:17:42 -0800779 mFlushingAudio = FLUSHED;
780 } else {
Andreas Huber1aef2112011-01-04 14:01:29 -0800781 CHECK(IsFlushingState(mFlushingVideo, &needShutdown));
Andreas Huberf9334412010-12-15 15:17:42 -0800782 mFlushingVideo = FLUSHED;
Andreas Huber3fe62152011-09-16 15:09:22 -0700783
784 mVideoLateByUs = 0;
Andreas Huberf9334412010-12-15 15:17:42 -0800785 }
786
Steve Block3856b092011-10-20 11:56:00 +0100787 ALOGV("decoder %s flush completed", audio ? "audio" : "video");
Andreas Huberf9334412010-12-15 15:17:42 -0800788
Andreas Huber1aef2112011-01-04 14:01:29 -0800789 if (needShutdown) {
Steve Block3856b092011-10-20 11:56:00 +0100790 ALOGV("initiating %s decoder shutdown",
Andreas Huber53df1a42010-12-22 10:03:04 -0800791 audio ? "audio" : "video");
Andreas Huberf9334412010-12-15 15:17:42 -0800792
Lajos Molnar87603c02014-08-20 19:25:30 -0700793 getDecoder(audio)->initiateShutdown();
Andreas Huberf9334412010-12-15 15:17:42 -0800794
Andreas Huber53df1a42010-12-22 10:03:04 -0800795 if (audio) {
796 mFlushingAudio = SHUTTING_DOWN_DECODER;
797 } else {
798 mFlushingVideo = SHUTTING_DOWN_DECODER;
799 }
Andreas Huberf9334412010-12-15 15:17:42 -0800800 }
Andreas Huber3831a062010-12-21 10:22:33 -0800801
802 finishFlushIfPossible();
Lajos Molnar1cd13982014-01-17 15:12:51 -0800803 } else if (what == Decoder::kWhatOutputFormatChanged) {
804 sp<AMessage> format;
805 CHECK(msg->findMessage("format", &format));
806
Andreas Huber31e25082011-01-10 10:38:31 -0800807 if (audio) {
Andy Hung282a7e32014-08-14 15:56:34 -0700808 openAudioSink(format, false /*offloadOnly*/);
Andreas Huber31e25082011-01-10 10:38:31 -0800809 } else {
810 // video
Chong Zhangced1c2f2014-08-08 15:22:35 -0700811 sp<AMessage> inputFormat =
812 mSource->getFormat(false /* audio */);
Andreas Huber3831a062010-12-21 10:22:33 -0800813
Chong Zhangced1c2f2014-08-08 15:22:35 -0700814 updateVideoSize(inputFormat, format);
Andreas Huber31e25082011-01-10 10:38:31 -0800815 }
Lajos Molnar1cd13982014-01-17 15:12:51 -0800816 } else if (what == Decoder::kWhatShutdownCompleted) {
Steve Block3856b092011-10-20 11:56:00 +0100817 ALOGV("%s shutdown completed", audio ? "audio" : "video");
Andreas Huber3831a062010-12-21 10:22:33 -0800818 if (audio) {
819 mAudioDecoder.clear();
820
821 CHECK_EQ((int)mFlushingAudio, (int)SHUTTING_DOWN_DECODER);
822 mFlushingAudio = SHUT_DOWN;
823 } else {
824 mVideoDecoder.clear();
825
826 CHECK_EQ((int)mFlushingVideo, (int)SHUTTING_DOWN_DECODER);
827 mFlushingVideo = SHUT_DOWN;
828 }
829
830 finishFlushIfPossible();
Lajos Molnar1cd13982014-01-17 15:12:51 -0800831 } else if (what == Decoder::kWhatError) {
Steve Block29357bc2012-01-06 19:20:56 +0000832 ALOGE("Received error from %s decoder, aborting playback.",
Andreas Huberc92fd242011-08-16 13:48:44 -0700833 audio ? "audio" : "video");
834
Chong Zhangf4c0a942014-08-11 15:14:10 -0700835 status_t err;
836 if (!msg->findInt32("err", &err)) {
837 err = UNKNOWN_ERROR;
838 }
839 mRenderer->queueEOS(audio, err);
Marco Nelissen9e2b7912014-08-18 16:13:03 -0700840 if (audio && mFlushingAudio != NONE) {
841 mAudioDecoder.clear();
842 mFlushingAudio = SHUT_DOWN;
843 } else if (!audio && mFlushingVideo != NONE){
844 mVideoDecoder.clear();
845 mFlushingVideo = SHUT_DOWN;
846 }
847 finishFlushIfPossible();
Lajos Molnar1cd13982014-01-17 15:12:51 -0800848 } else if (what == Decoder::kWhatDrainThisBuffer) {
849 renderBuffer(audio, msg);
850 } else {
851 ALOGV("Unhandled decoder notification %d '%c%c%c%c'.",
Andreas Hubera1f8ab02012-11-30 10:53:22 -0800852 what,
853 what >> 24,
854 (what >> 16) & 0xff,
855 (what >> 8) & 0xff,
856 what & 0xff);
Andreas Huberf9334412010-12-15 15:17:42 -0800857 }
858
859 break;
860 }
861
862 case kWhatRendererNotify:
863 {
864 int32_t what;
865 CHECK(msg->findInt32("what", &what));
866
867 if (what == Renderer::kWhatEOS) {
868 int32_t audio;
869 CHECK(msg->findInt32("audio", &audio));
870
Andreas Huberc92fd242011-08-16 13:48:44 -0700871 int32_t finalResult;
872 CHECK(msg->findInt32("finalResult", &finalResult));
873
Andreas Huberf9334412010-12-15 15:17:42 -0800874 if (audio) {
875 mAudioEOS = true;
876 } else {
877 mVideoEOS = true;
878 }
879
Andreas Huberc92fd242011-08-16 13:48:44 -0700880 if (finalResult == ERROR_END_OF_STREAM) {
Steve Block3856b092011-10-20 11:56:00 +0100881 ALOGV("reached %s EOS", audio ? "audio" : "video");
Andreas Huberc92fd242011-08-16 13:48:44 -0700882 } else {
Steve Block29357bc2012-01-06 19:20:56 +0000883 ALOGE("%s track encountered an error (%d)",
Andreas Huberc92fd242011-08-16 13:48:44 -0700884 audio ? "audio" : "video", finalResult);
885
886 notifyListener(
887 MEDIA_ERROR, MEDIA_ERROR_UNKNOWN, finalResult);
888 }
Andreas Huberf9334412010-12-15 15:17:42 -0800889
890 if ((mAudioEOS || mAudioDecoder == NULL)
891 && (mVideoEOS || mVideoDecoder == NULL)) {
892 notifyListener(MEDIA_PLAYBACK_COMPLETE, 0, 0);
893 }
Andreas Huber43c3e6c2011-01-05 12:17:08 -0800894 } else if (what == Renderer::kWhatPosition) {
895 int64_t positionUs;
896 CHECK(msg->findInt64("positionUs", &positionUs));
Wei Jiaac428aa2014-09-02 19:01:34 -0700897 mCurrentPositionUs = positionUs;
Andreas Huber43c3e6c2011-01-05 12:17:08 -0800898
Andreas Huber3fe62152011-09-16 15:09:22 -0700899 CHECK(msg->findInt64("videoLateByUs", &mVideoLateByUs));
900
Andreas Huber43c3e6c2011-01-05 12:17:08 -0800901 if (mDriver != NULL) {
902 sp<NuPlayerDriver> driver = mDriver.promote();
903 if (driver != NULL) {
904 driver->notifyPosition(positionUs);
Andreas Huber3fe62152011-09-16 15:09:22 -0700905
906 driver->notifyFrameStats(
907 mNumFramesTotal, mNumFramesDropped);
Andreas Huber43c3e6c2011-01-05 12:17:08 -0800908 }
909 }
Andreas Huber3fe62152011-09-16 15:09:22 -0700910 } else if (what == Renderer::kWhatFlushComplete) {
Andreas Huberf9334412010-12-15 15:17:42 -0800911 int32_t audio;
912 CHECK(msg->findInt32("audio", &audio));
913
Steve Block3856b092011-10-20 11:56:00 +0100914 ALOGV("renderer %s flush completed.", audio ? "audio" : "video");
James Dongf57b4ea2012-07-20 13:38:36 -0700915 } else if (what == Renderer::kWhatVideoRenderingStart) {
916 notifyListener(MEDIA_INFO, MEDIA_INFO_RENDERING_START, 0);
Lajos Molnarcbaffcf2013-08-14 18:30:38 -0700917 } else if (what == Renderer::kWhatMediaRenderingStart) {
918 ALOGV("media rendering started");
919 notifyListener(MEDIA_STARTED, 0, 0);
Wei Jia3a2956d2014-07-22 16:01:33 -0700920 } else if (what == Renderer::kWhatAudioOffloadTearDown) {
921 ALOGV("Tear down audio offload, fall back to s/w path");
922 int64_t positionUs;
923 CHECK(msg->findInt64("positionUs", &positionUs));
Andy Hung282a7e32014-08-14 15:56:34 -0700924 closeAudioSink();
Wei Jia3a2956d2014-07-22 16:01:33 -0700925 mAudioDecoder.clear();
926 mRenderer->flush(true /* audio */);
927 if (mVideoDecoder != NULL) {
928 mRenderer->flush(false /* audio */);
929 }
930 mRenderer->signalDisableOffloadAudio();
931 mOffloadAudio = false;
932
Wei Jiae427abf2014-09-22 15:21:11 -0700933 performSeek(positionUs, false /* needNotify */);
Wei Jia3a2956d2014-07-22 16:01:33 -0700934 instantiateDecoder(true /* audio */, &mAudioDecoder);
Andreas Huberf9334412010-12-15 15:17:42 -0800935 }
936 break;
937 }
938
939 case kWhatMoreDataQueued:
940 {
941 break;
942 }
943
Andreas Huber1aef2112011-01-04 14:01:29 -0800944 case kWhatReset:
945 {
Steve Block3856b092011-10-20 11:56:00 +0100946 ALOGV("kWhatReset");
Andreas Huber1aef2112011-01-04 14:01:29 -0800947
Andreas Hubera1f8ab02012-11-30 10:53:22 -0800948 mDeferredActions.push_back(
Andreas Huber14f76722013-01-15 09:04:18 -0800949 new ShutdownDecoderAction(
950 true /* audio */, true /* video */));
Andreas Huberb7c8e912012-11-27 15:02:53 -0800951
Andreas Hubera1f8ab02012-11-30 10:53:22 -0800952 mDeferredActions.push_back(
953 new SimpleAction(&NuPlayer::performReset));
Andreas Huberb58ce9f2011-11-28 16:27:35 -0800954
Andreas Hubera1f8ab02012-11-30 10:53:22 -0800955 processDeferredActions();
Andreas Huber1aef2112011-01-04 14:01:29 -0800956 break;
957 }
958
Andreas Huber43c3e6c2011-01-05 12:17:08 -0800959 case kWhatSeek:
960 {
961 int64_t seekTimeUs;
Wei Jiae427abf2014-09-22 15:21:11 -0700962 int32_t needNotify;
Andreas Huber43c3e6c2011-01-05 12:17:08 -0800963 CHECK(msg->findInt64("seekTimeUs", &seekTimeUs));
Wei Jiae427abf2014-09-22 15:21:11 -0700964 CHECK(msg->findInt32("needNotify", &needNotify));
Andreas Huber43c3e6c2011-01-05 12:17:08 -0800965
Wei Jiae427abf2014-09-22 15:21:11 -0700966 ALOGV("kWhatSeek seekTimeUs=%lld us, needNotify=%d",
967 seekTimeUs, needNotify);
Andreas Huber43c3e6c2011-01-05 12:17:08 -0800968
Andreas Hubera1f8ab02012-11-30 10:53:22 -0800969 mDeferredActions.push_back(
970 new SimpleAction(&NuPlayer::performDecoderFlush));
Andreas Huber43c3e6c2011-01-05 12:17:08 -0800971
Wei Jiae427abf2014-09-22 15:21:11 -0700972 mDeferredActions.push_back(
973 new SeekAction(seekTimeUs, needNotify));
Andreas Huber43c3e6c2011-01-05 12:17:08 -0800974
Andreas Hubera1f8ab02012-11-30 10:53:22 -0800975 processDeferredActions();
Andreas Huber43c3e6c2011-01-05 12:17:08 -0800976 break;
977 }
978
Andreas Huberb4082222011-01-20 15:23:04 -0800979 case kWhatPause:
980 {
981 CHECK(mRenderer != NULL);
Roger Jönssonfba60da2013-01-21 17:15:45 +0100982 mSource->pause();
Andreas Huberb4082222011-01-20 15:23:04 -0800983 mRenderer->pause();
984 break;
985 }
986
987 case kWhatResume:
988 {
989 CHECK(mRenderer != NULL);
Roger Jönssonfba60da2013-01-21 17:15:45 +0100990 mSource->resume();
Andreas Huberb4082222011-01-20 15:23:04 -0800991 mRenderer->resume();
992 break;
993 }
994
Andreas Huberb5f25f02013-02-05 10:14:26 -0800995 case kWhatSourceNotify:
996 {
Andreas Huber9575c962013-02-05 13:59:56 -0800997 onSourceNotify(msg);
Andreas Huberb5f25f02013-02-05 10:14:26 -0800998 break;
999 }
1000
Chong Zhanga7fa1d92014-06-11 14:49:23 -07001001 case kWhatClosedCaptionNotify:
1002 {
1003 onClosedCaptionNotify(msg);
1004 break;
1005 }
1006
Andreas Huberf9334412010-12-15 15:17:42 -08001007 default:
1008 TRESPASS();
1009 break;
1010 }
1011}
1012
Andreas Huber3831a062010-12-21 10:22:33 -08001013void NuPlayer::finishFlushIfPossible() {
Wei Jia53904f32014-07-29 10:22:53 -07001014 if (mFlushingAudio != NONE && mFlushingAudio != FLUSHED
1015 && mFlushingAudio != SHUT_DOWN) {
Andreas Huber3831a062010-12-21 10:22:33 -08001016 return;
1017 }
1018
Wei Jia53904f32014-07-29 10:22:53 -07001019 if (mFlushingVideo != NONE && mFlushingVideo != FLUSHED
1020 && mFlushingVideo != SHUT_DOWN) {
Andreas Huber3831a062010-12-21 10:22:33 -08001021 return;
1022 }
1023
Steve Block3856b092011-10-20 11:56:00 +01001024 ALOGV("both audio and video are flushed now.");
Andreas Huber3831a062010-12-21 10:22:33 -08001025
Phil Burk9f526492014-09-03 15:04:12 -07001026 mPendingAudioAccessUnit.clear();
Phil Burkc5cc2e22014-09-09 20:08:39 -07001027 mAggregateBuffer.clear();
Phil Burk9f526492014-09-03 15:04:12 -07001028
Andreas Huber6e3d3112011-11-28 12:36:11 -08001029 if (mTimeDiscontinuityPending) {
1030 mRenderer->signalTimeDiscontinuity();
1031 mTimeDiscontinuityPending = false;
1032 }
Andreas Huber3831a062010-12-21 10:22:33 -08001033
Wei Jia53904f32014-07-29 10:22:53 -07001034 if (mAudioDecoder != NULL && mFlushingAudio == FLUSHED) {
Andreas Huber3831a062010-12-21 10:22:33 -08001035 mAudioDecoder->signalResume();
1036 }
1037
Wei Jia53904f32014-07-29 10:22:53 -07001038 if (mVideoDecoder != NULL && mFlushingVideo == FLUSHED) {
Andreas Huber3831a062010-12-21 10:22:33 -08001039 mVideoDecoder->signalResume();
1040 }
1041
1042 mFlushingAudio = NONE;
1043 mFlushingVideo = NONE;
Andreas Huber3831a062010-12-21 10:22:33 -08001044
Andreas Hubera1f8ab02012-11-30 10:53:22 -08001045 processDeferredActions();
Andreas Huber1aef2112011-01-04 14:01:29 -08001046}
1047
1048void NuPlayer::postScanSources() {
1049 if (mScanSourcesPending) {
1050 return;
1051 }
1052
1053 sp<AMessage> msg = new AMessage(kWhatScanSources, id());
1054 msg->setInt32("generation", mScanSourcesGeneration);
1055 msg->post();
1056
1057 mScanSourcesPending = true;
1058}
1059
Andy Hung282a7e32014-08-14 15:56:34 -07001060void NuPlayer::openAudioSink(const sp<AMessage> &format, bool offloadOnly) {
1061 ALOGV("openAudioSink: offloadOnly(%d) mOffloadAudio(%d)",
1062 offloadOnly, mOffloadAudio);
1063 bool audioSinkChanged = false;
1064
1065 int32_t numChannels;
1066 CHECK(format->findInt32("channel-count", &numChannels));
1067
1068 int32_t channelMask;
1069 if (!format->findInt32("channel-mask", &channelMask)) {
1070 // signal to the AudioSink to derive the mask from count.
1071 channelMask = CHANNEL_MASK_USE_CHANNEL_ORDER;
1072 }
1073
1074 int32_t sampleRate;
1075 CHECK(format->findInt32("sample-rate", &sampleRate));
1076
1077 uint32_t flags;
1078 int64_t durationUs;
1079 // FIXME: we should handle the case where the video decoder
1080 // is created after we receive the format change indication.
1081 // Current code will just make that we select deep buffer
1082 // with video which should not be a problem as it should
1083 // not prevent from keeping A/V sync.
1084 if (mVideoDecoder == NULL &&
1085 mSource->getDuration(&durationUs) == OK &&
1086 durationUs
1087 > AUDIO_SINK_MIN_DEEP_BUFFER_DURATION_US) {
1088 flags = AUDIO_OUTPUT_FLAG_DEEP_BUFFER;
1089 } else {
1090 flags = AUDIO_OUTPUT_FLAG_NONE;
1091 }
1092
1093 if (mOffloadAudio) {
1094 audio_format_t audioFormat = AUDIO_FORMAT_PCM_16_BIT;
1095 AString mime;
1096 CHECK(format->findString("mime", &mime));
1097 status_t err = mapMimeToAudioFormat(audioFormat, mime.c_str());
1098
1099 if (err != OK) {
1100 ALOGE("Couldn't map mime \"%s\" to a valid "
1101 "audio_format", mime.c_str());
1102 mOffloadAudio = false;
1103 } else {
1104 ALOGV("Mime \"%s\" mapped to audio_format 0x%x",
1105 mime.c_str(), audioFormat);
1106
1107 int avgBitRate = -1;
1108 format->findInt32("bit-rate", &avgBitRate);
1109
1110 int32_t aacProfile = -1;
1111 if (audioFormat == AUDIO_FORMAT_AAC
1112 && format->findInt32("aac-profile", &aacProfile)) {
1113 // Redefine AAC format as per aac profile
1114 mapAACProfileToAudioFormat(
1115 audioFormat,
1116 aacProfile);
1117 }
1118
1119 audio_offload_info_t offloadInfo = AUDIO_INFO_INITIALIZER;
1120 offloadInfo.duration_us = -1;
1121 format->findInt64(
1122 "durationUs", &offloadInfo.duration_us);
1123 offloadInfo.sample_rate = sampleRate;
1124 offloadInfo.channel_mask = channelMask;
1125 offloadInfo.format = audioFormat;
1126 offloadInfo.stream_type = AUDIO_STREAM_MUSIC;
1127 offloadInfo.bit_rate = avgBitRate;
1128 offloadInfo.has_video = (mVideoDecoder != NULL);
1129 offloadInfo.is_streaming = true;
1130
1131 if (memcmp(&mCurrentOffloadInfo, &offloadInfo, sizeof(offloadInfo)) == 0) {
1132 ALOGV("openAudioSink: no change in offload mode");
1133 return; // no change from previous configuration, everything ok.
1134 }
1135 ALOGV("openAudioSink: try to open AudioSink in offload mode");
1136 flags |= AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD;
Ronghua Wu1ffb5382014-08-18 15:57:03 -07001137 flags &= ~AUDIO_OUTPUT_FLAG_DEEP_BUFFER;
Andy Hung282a7e32014-08-14 15:56:34 -07001138 audioSinkChanged = true;
1139 mAudioSink->close();
1140 err = mAudioSink->open(
1141 sampleRate,
1142 numChannels,
1143 (audio_channel_mask_t)channelMask,
1144 audioFormat,
1145 8 /* bufferCount */,
1146 &NuPlayer::Renderer::AudioSinkCallback,
1147 mRenderer.get(),
1148 (audio_output_flags_t)flags,
1149 &offloadInfo);
1150
1151 if (err == OK) {
1152 // If the playback is offloaded to h/w, we pass
1153 // the HAL some metadata information.
1154 // We don't want to do this for PCM because it
1155 // will be going through the AudioFlinger mixer
1156 // before reaching the hardware.
1157 sp<MetaData> audioMeta =
1158 mSource->getFormatMeta(true /* audio */);
1159 sendMetaDataToHal(mAudioSink, audioMeta);
1160 mCurrentOffloadInfo = offloadInfo;
1161 err = mAudioSink->start();
1162 ALOGV_IF(err == OK, "openAudioSink: offload succeeded");
1163 }
1164 if (err != OK) {
1165 // Clean up, fall back to non offload mode.
1166 mAudioSink->close();
1167 mRenderer->signalDisableOffloadAudio();
1168 mOffloadAudio = false;
1169 mCurrentOffloadInfo = AUDIO_INFO_INITIALIZER;
1170 ALOGV("openAudioSink: offload failed");
1171 }
1172 }
1173 }
1174 if (!offloadOnly && !mOffloadAudio) {
1175 flags &= ~AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD;
1176 ALOGV("openAudioSink: open AudioSink in NON-offload mode");
1177
1178 audioSinkChanged = true;
1179 mAudioSink->close();
1180 mCurrentOffloadInfo = AUDIO_INFO_INITIALIZER;
1181 CHECK_EQ(mAudioSink->open(
1182 sampleRate,
1183 numChannels,
1184 (audio_channel_mask_t)channelMask,
1185 AUDIO_FORMAT_PCM_16_BIT,
1186 8 /* bufferCount */,
1187 NULL,
1188 NULL,
1189 (audio_output_flags_t)flags),
1190 (status_t)OK);
1191 mAudioSink->start();
1192 }
1193 if (audioSinkChanged) {
1194 mRenderer->signalAudioSinkChanged();
1195 }
1196}
1197
1198void NuPlayer::closeAudioSink() {
1199 mAudioSink->close();
1200 mCurrentOffloadInfo = AUDIO_INFO_INITIALIZER;
1201}
1202
Andreas Huber5bc087c2010-12-23 10:27:40 -08001203status_t NuPlayer::instantiateDecoder(bool audio, sp<Decoder> *decoder) {
Andreas Huberf9334412010-12-15 15:17:42 -08001204 if (*decoder != NULL) {
1205 return OK;
1206 }
1207
Andreas Huber84066782011-08-16 09:34:26 -07001208 sp<AMessage> format = mSource->getFormat(audio);
Andreas Huberf9334412010-12-15 15:17:42 -08001209
Andreas Huber84066782011-08-16 09:34:26 -07001210 if (format == NULL) {
Andreas Huberf9334412010-12-15 15:17:42 -08001211 return -EWOULDBLOCK;
1212 }
1213
Andreas Huber3fe62152011-09-16 15:09:22 -07001214 if (!audio) {
Andreas Huber84066782011-08-16 09:34:26 -07001215 AString mime;
1216 CHECK(format->findString("mime", &mime));
1217 mVideoIsAVC = !strcasecmp(MEDIA_MIMETYPE_VIDEO_AVC, mime.c_str());
Chong Zhanga7fa1d92014-06-11 14:49:23 -07001218
1219 sp<AMessage> ccNotify = new AMessage(kWhatClosedCaptionNotify, id());
1220 mCCDecoder = new CCDecoder(ccNotify);
Lajos Molnar09524832014-07-17 14:29:51 -07001221
1222 if (mSourceFlags & Source::FLAG_SECURE) {
1223 format->setInt32("secure", true);
1224 }
Andreas Huber3fe62152011-09-16 15:09:22 -07001225 }
1226
Wei Jiabc2fb722014-07-08 16:37:57 -07001227 if (audio) {
Wei Jia88703c32014-08-06 11:24:07 -07001228 sp<AMessage> notify = new AMessage(kWhatAudioNotify, id());
1229 ++mAudioDecoderGeneration;
1230 notify->setInt32("generation", mAudioDecoderGeneration);
1231
Wei Jiabc2fb722014-07-08 16:37:57 -07001232 if (mOffloadAudio) {
1233 *decoder = new DecoderPassThrough(notify);
1234 } else {
1235 *decoder = new Decoder(notify);
1236 }
1237 } else {
Wei Jia88703c32014-08-06 11:24:07 -07001238 sp<AMessage> notify = new AMessage(kWhatVideoNotify, id());
1239 ++mVideoDecoderGeneration;
1240 notify->setInt32("generation", mVideoDecoderGeneration);
1241
Wei Jiabc2fb722014-07-08 16:37:57 -07001242 *decoder = new Decoder(notify, mNativeWindow);
1243 }
Lajos Molnar1cd13982014-01-17 15:12:51 -08001244 (*decoder)->init();
Andreas Huber84066782011-08-16 09:34:26 -07001245 (*decoder)->configure(format);
Andreas Huberf9334412010-12-15 15:17:42 -08001246
Lajos Molnar09524832014-07-17 14:29:51 -07001247 // allocate buffers to decrypt widevine source buffers
1248 if (!audio && (mSourceFlags & Source::FLAG_SECURE)) {
1249 Vector<sp<ABuffer> > inputBufs;
1250 CHECK_EQ((*decoder)->getInputBuffers(&inputBufs), (status_t)OK);
1251
1252 Vector<MediaBuffer *> mediaBufs;
1253 for (size_t i = 0; i < inputBufs.size(); i++) {
1254 const sp<ABuffer> &buffer = inputBufs[i];
1255 MediaBuffer *mbuf = new MediaBuffer(buffer->data(), buffer->size());
1256 mediaBufs.push(mbuf);
1257 }
1258
1259 status_t err = mSource->setBuffers(audio, mediaBufs);
1260 if (err != OK) {
1261 for (size_t i = 0; i < mediaBufs.size(); ++i) {
1262 mediaBufs[i]->release();
1263 }
1264 mediaBufs.clear();
1265 ALOGE("Secure source didn't support secure mediaBufs.");
1266 return err;
1267 }
1268 }
Andreas Huberf9334412010-12-15 15:17:42 -08001269 return OK;
1270}
1271
1272status_t NuPlayer::feedDecoderInputData(bool audio, const sp<AMessage> &msg) {
1273 sp<AMessage> reply;
1274 CHECK(msg->findMessage("reply", &reply));
1275
Wei Jia53904f32014-07-29 10:22:53 -07001276 if ((audio && mFlushingAudio != NONE)
Wei Jiaf702d042014-09-09 12:08:47 -07001277 || (!audio && mFlushingVideo != NONE)
1278 || mSource == NULL) {
Wei Jiab189a5b2014-08-07 06:11:39 +00001279 reply->setInt32("err", INFO_DISCONTINUITY);
1280 reply->post();
1281 return OK;
Andreas Huberf9334412010-12-15 15:17:42 -08001282 }
1283
1284 sp<ABuffer> accessUnit;
Andreas Huberf9334412010-12-15 15:17:42 -08001285
Phil Burk9f526492014-09-03 15:04:12 -07001286 // Aggregate smaller buffers into a larger buffer.
1287 // The goal is to reduce power consumption.
Phil Burk33b51b02014-09-17 16:03:47 -07001288 // Note this will not work if the decoder requires one frame per buffer.
1289 bool doBufferAggregation = (audio && mOffloadAudio);
Phil Burk9f526492014-09-03 15:04:12 -07001290 bool needMoreData = false;
Phil Burk9f526492014-09-03 15:04:12 -07001291
Andreas Huber3fe62152011-09-16 15:09:22 -07001292 bool dropAccessUnit;
1293 do {
Phil Burk9f526492014-09-03 15:04:12 -07001294 status_t err;
1295 // Did we save an accessUnit earlier because of a discontinuity?
1296 if (audio && (mPendingAudioAccessUnit != NULL)) {
1297 accessUnit = mPendingAudioAccessUnit;
1298 mPendingAudioAccessUnit.clear();
1299 err = mPendingAudioErr;
1300 ALOGV("feedDecoderInputData() use mPendingAudioAccessUnit");
1301 } else {
1302 err = mSource->dequeueAccessUnit(audio, &accessUnit);
1303 }
Andreas Huber5bc087c2010-12-23 10:27:40 -08001304
Andreas Huber3fe62152011-09-16 15:09:22 -07001305 if (err == -EWOULDBLOCK) {
Phil Burkc5cc2e22014-09-09 20:08:39 -07001306 return err;
Andreas Huber3fe62152011-09-16 15:09:22 -07001307 } else if (err != OK) {
1308 if (err == INFO_DISCONTINUITY) {
Phil Burk33b51b02014-09-17 16:03:47 -07001309 if (doBufferAggregation && (mAggregateBuffer != NULL)) {
Phil Burk9f526492014-09-03 15:04:12 -07001310 // We already have some data so save this for later.
1311 mPendingAudioErr = err;
1312 mPendingAudioAccessUnit = accessUnit;
1313 accessUnit.clear();
1314 ALOGD("feedDecoderInputData() save discontinuity for later");
1315 break;
1316 }
Andreas Huber3fe62152011-09-16 15:09:22 -07001317 int32_t type;
1318 CHECK(accessUnit->meta()->findInt32("discontinuity", &type));
Andreas Huber53df1a42010-12-22 10:03:04 -08001319
Andreas Huber3fe62152011-09-16 15:09:22 -07001320 bool formatChange =
Andreas Huber6e3d3112011-11-28 12:36:11 -08001321 (audio &&
1322 (type & ATSParser::DISCONTINUITY_AUDIO_FORMAT))
1323 || (!audio &&
1324 (type & ATSParser::DISCONTINUITY_VIDEO_FORMAT));
Andreas Huber53df1a42010-12-22 10:03:04 -08001325
Andreas Huber6e3d3112011-11-28 12:36:11 -08001326 bool timeChange = (type & ATSParser::DISCONTINUITY_TIME) != 0;
1327
Steve Blockdf64d152012-01-04 20:05:49 +00001328 ALOGI("%s discontinuity (formatChange=%d, time=%d)",
Andreas Huber6e3d3112011-11-28 12:36:11 -08001329 audio ? "audio" : "video", formatChange, timeChange);
Andreas Huber32f3cef2011-03-02 15:34:46 -08001330
Andreas Huber3fe62152011-09-16 15:09:22 -07001331 if (audio) {
1332 mSkipRenderingAudioUntilMediaTimeUs = -1;
1333 } else {
1334 mSkipRenderingVideoUntilMediaTimeUs = -1;
1335 }
Andreas Huber32f3cef2011-03-02 15:34:46 -08001336
Andreas Huber6e3d3112011-11-28 12:36:11 -08001337 if (timeChange) {
1338 sp<AMessage> extra;
1339 if (accessUnit->meta()->findMessage("extra", &extra)
1340 && extra != NULL) {
1341 int64_t resumeAtMediaTimeUs;
1342 if (extra->findInt64(
1343 "resume-at-mediatimeUs", &resumeAtMediaTimeUs)) {
Steve Blockdf64d152012-01-04 20:05:49 +00001344 ALOGI("suppressing rendering of %s until %lld us",
Andreas Huber6e3d3112011-11-28 12:36:11 -08001345 audio ? "audio" : "video", resumeAtMediaTimeUs);
Andreas Huber3fe62152011-09-16 15:09:22 -07001346
Andreas Huber6e3d3112011-11-28 12:36:11 -08001347 if (audio) {
1348 mSkipRenderingAudioUntilMediaTimeUs =
1349 resumeAtMediaTimeUs;
1350 } else {
1351 mSkipRenderingVideoUntilMediaTimeUs =
1352 resumeAtMediaTimeUs;
1353 }
Andreas Huber3fe62152011-09-16 15:09:22 -07001354 }
Andreas Huber32f3cef2011-03-02 15:34:46 -08001355 }
1356 }
Andreas Huber3fe62152011-09-16 15:09:22 -07001357
Andreas Huber6e3d3112011-11-28 12:36:11 -08001358 mTimeDiscontinuityPending =
1359 mTimeDiscontinuityPending || timeChange;
1360
Lajos Molnar87603c02014-08-20 19:25:30 -07001361 bool seamlessFormatChange = false;
1362 sp<AMessage> newFormat = mSource->getFormat(audio);
1363 if (formatChange) {
1364 seamlessFormatChange =
1365 getDecoder(audio)->supportsSeamlessFormatChange(newFormat);
1366 // treat seamless format change separately
1367 formatChange = !seamlessFormatChange;
1368 }
1369 bool shutdownOrFlush = formatChange || timeChange;
1370
1371 // We want to queue up scan-sources only once per discontinuity.
1372 // We control this by doing it only if neither audio nor video are
1373 // flushing or shutting down. (After handling 1st discontinuity, one
1374 // of the flushing states will not be NONE.)
1375 // No need to scan sources if this discontinuity does not result
1376 // in a flush or shutdown, as the flushing state will stay NONE.
1377 if (mFlushingAudio == NONE && mFlushingVideo == NONE &&
1378 shutdownOrFlush) {
Robert Shiha2981012014-07-30 17:41:24 -07001379 // And we'll resume scanning sources once we're done
1380 // flushing.
1381 mDeferredActions.push_front(
1382 new SimpleAction(
1383 &NuPlayer::performScanSources));
1384 }
1385
Lajos Molnar87603c02014-08-20 19:25:30 -07001386 if (formatChange /* not seamless */) {
1387 // must change decoder
1388 flushDecoder(audio, /* needShutdown = */ true);
1389 } else if (timeChange) {
1390 // need to flush
1391 flushDecoder(audio, /* needShutdown = */ false, newFormat);
1392 err = OK;
1393 } else if (seamlessFormatChange) {
1394 // reuse existing decoder and don't flush
1395 updateDecoderFormatWithoutFlush(audio, newFormat);
1396 err = OK;
Andreas Huber6e3d3112011-11-28 12:36:11 -08001397 } else {
1398 // This stream is unaffected by the discontinuity
Andreas Huber6e3d3112011-11-28 12:36:11 -08001399 return -EWOULDBLOCK;
1400 }
Andreas Huber32f3cef2011-03-02 15:34:46 -08001401 }
1402
Andreas Huber3fe62152011-09-16 15:09:22 -07001403 reply->setInt32("err", err);
1404 reply->post();
1405 return OK;
Andreas Huberf9334412010-12-15 15:17:42 -08001406 }
1407
Andreas Huber3fe62152011-09-16 15:09:22 -07001408 if (!audio) {
1409 ++mNumFramesTotal;
1410 }
1411
1412 dropAccessUnit = false;
1413 if (!audio
Lajos Molnar09524832014-07-17 14:29:51 -07001414 && !(mSourceFlags & Source::FLAG_SECURE)
Andreas Huber3fe62152011-09-16 15:09:22 -07001415 && mVideoLateByUs > 100000ll
1416 && mVideoIsAVC
1417 && !IsAVCReferenceFrame(accessUnit)) {
1418 dropAccessUnit = true;
1419 ++mNumFramesDropped;
1420 }
Phil Burk9f526492014-09-03 15:04:12 -07001421
1422 size_t smallSize = accessUnit->size();
1423 needMoreData = false;
Phil Burkc5cc2e22014-09-09 20:08:39 -07001424 if (doBufferAggregation && (mAggregateBuffer == NULL)
Phil Burk9f526492014-09-03 15:04:12 -07001425 // Don't bother if only room for a few small buffers.
Phil Burkc5cc2e22014-09-09 20:08:39 -07001426 && (smallSize < (kAggregateBufferSizeBytes / 3))) {
Phil Burk9f526492014-09-03 15:04:12 -07001427 // Create a larger buffer for combining smaller buffers from the extractor.
Phil Burkc5cc2e22014-09-09 20:08:39 -07001428 mAggregateBuffer = new ABuffer(kAggregateBufferSizeBytes);
1429 mAggregateBuffer->setRange(0, 0); // start empty
Phil Burk9f526492014-09-03 15:04:12 -07001430 }
1431
Phil Burk33b51b02014-09-17 16:03:47 -07001432 if (doBufferAggregation && (mAggregateBuffer != NULL)) {
Phil Burk9f526492014-09-03 15:04:12 -07001433 int64_t timeUs;
Phil Burkc5cc2e22014-09-09 20:08:39 -07001434 int64_t dummy;
Phil Burk9f526492014-09-03 15:04:12 -07001435 bool smallTimestampValid = accessUnit->meta()->findInt64("timeUs", &timeUs);
Phil Burkc5cc2e22014-09-09 20:08:39 -07001436 bool bigTimestampValid = mAggregateBuffer->meta()->findInt64("timeUs", &dummy);
Phil Burk9f526492014-09-03 15:04:12 -07001437 // Will the smaller buffer fit?
Phil Burkc5cc2e22014-09-09 20:08:39 -07001438 size_t bigSize = mAggregateBuffer->size();
1439 size_t roomLeft = mAggregateBuffer->capacity() - bigSize;
Phil Burk9f526492014-09-03 15:04:12 -07001440 // Should we save this small buffer for the next big buffer?
1441 // If the first small buffer did not have a timestamp then save
1442 // any buffer that does have a timestamp until the next big buffer.
1443 if ((smallSize > roomLeft)
Phil Burkc5cc2e22014-09-09 20:08:39 -07001444 || (!bigTimestampValid && (bigSize > 0) && smallTimestampValid)) {
Phil Burk9f526492014-09-03 15:04:12 -07001445 mPendingAudioErr = err;
1446 mPendingAudioAccessUnit = accessUnit;
1447 accessUnit.clear();
1448 } else {
Phil Burkc5cc2e22014-09-09 20:08:39 -07001449 // Grab time from first small buffer if available.
1450 if ((bigSize == 0) && smallTimestampValid) {
1451 mAggregateBuffer->meta()->setInt64("timeUs", timeUs);
1452 }
Phil Burk9f526492014-09-03 15:04:12 -07001453 // Append small buffer to the bigger buffer.
Phil Burkc5cc2e22014-09-09 20:08:39 -07001454 memcpy(mAggregateBuffer->base() + bigSize, accessUnit->data(), smallSize);
Phil Burk9f526492014-09-03 15:04:12 -07001455 bigSize += smallSize;
Phil Burkc5cc2e22014-09-09 20:08:39 -07001456 mAggregateBuffer->setRange(0, bigSize);
Phil Burk9f526492014-09-03 15:04:12 -07001457
Phil Burkc5cc2e22014-09-09 20:08:39 -07001458 // Keep looping until we run out of room in the mAggregateBuffer.
Phil Burk9f526492014-09-03 15:04:12 -07001459 needMoreData = true;
1460
Phil Burkc5cc2e22014-09-09 20:08:39 -07001461 ALOGV("feedDecoderInputData() smallSize = %zu, bigSize = %zu, capacity = %zu",
1462 smallSize, bigSize, mAggregateBuffer->capacity());
Phil Burk9f526492014-09-03 15:04:12 -07001463 }
1464 }
1465 } while (dropAccessUnit || needMoreData);
Andreas Huberf9334412010-12-15 15:17:42 -08001466
Steve Block3856b092011-10-20 11:56:00 +01001467 // ALOGV("returned a valid buffer of %s data", audio ? "audio" : "video");
Andreas Huberf9334412010-12-15 15:17:42 -08001468
1469#if 0
1470 int64_t mediaTimeUs;
1471 CHECK(accessUnit->meta()->findInt64("timeUs", &mediaTimeUs));
Steve Block3856b092011-10-20 11:56:00 +01001472 ALOGV("feeding %s input buffer at media time %.2f secs",
Andreas Huberf9334412010-12-15 15:17:42 -08001473 audio ? "audio" : "video",
1474 mediaTimeUs / 1E6);
1475#endif
1476
Chong Zhanga7fa1d92014-06-11 14:49:23 -07001477 if (!audio) {
1478 mCCDecoder->decode(accessUnit);
1479 }
1480
Phil Burk33b51b02014-09-17 16:03:47 -07001481 if (doBufferAggregation && (mAggregateBuffer != NULL)) {
Phil Burkc5cc2e22014-09-09 20:08:39 -07001482 ALOGV("feedDecoderInputData() reply with aggregated buffer, %zu",
1483 mAggregateBuffer->size());
1484 reply->setBuffer("buffer", mAggregateBuffer);
1485 mAggregateBuffer.clear();
Phil Burk9f526492014-09-03 15:04:12 -07001486 } else {
1487 reply->setBuffer("buffer", accessUnit);
1488 }
1489
Andreas Huberf9334412010-12-15 15:17:42 -08001490 reply->post();
1491
1492 return OK;
1493}
1494
1495void NuPlayer::renderBuffer(bool audio, const sp<AMessage> &msg) {
Steve Block3856b092011-10-20 11:56:00 +01001496 // ALOGV("renderBuffer %s", audio ? "audio" : "video");
Andreas Huberf9334412010-12-15 15:17:42 -08001497
1498 sp<AMessage> reply;
1499 CHECK(msg->findMessage("reply", &reply));
1500
Wei Jia53904f32014-07-29 10:22:53 -07001501 if ((audio && mFlushingAudio != NONE)
1502 || (!audio && mFlushingVideo != NONE)) {
Andreas Huber18ac5402011-08-31 15:04:25 -07001503 // We're currently attempting to flush the decoder, in order
1504 // to complete this, the decoder wants all its buffers back,
1505 // so we don't want any output buffers it sent us (from before
1506 // we initiated the flush) to be stuck in the renderer's queue.
1507
Steve Block3856b092011-10-20 11:56:00 +01001508 ALOGV("we're still flushing the %s decoder, sending its output buffer"
Andreas Huber18ac5402011-08-31 15:04:25 -07001509 " right back.", audio ? "audio" : "video");
1510
1511 reply->post();
1512 return;
1513 }
1514
Andreas Huber2d8bedd2012-02-21 14:38:23 -08001515 sp<ABuffer> buffer;
1516 CHECK(msg->findBuffer("buffer", &buffer));
Andreas Huberf9334412010-12-15 15:17:42 -08001517
Chong Zhanga7fa1d92014-06-11 14:49:23 -07001518 int64_t mediaTimeUs;
1519 CHECK(buffer->meta()->findInt64("timeUs", &mediaTimeUs));
1520
Andreas Huber32f3cef2011-03-02 15:34:46 -08001521 int64_t &skipUntilMediaTimeUs =
1522 audio
1523 ? mSkipRenderingAudioUntilMediaTimeUs
1524 : mSkipRenderingVideoUntilMediaTimeUs;
1525
1526 if (skipUntilMediaTimeUs >= 0) {
Andreas Huber32f3cef2011-03-02 15:34:46 -08001527
1528 if (mediaTimeUs < skipUntilMediaTimeUs) {
Steve Block3856b092011-10-20 11:56:00 +01001529 ALOGV("dropping %s buffer at time %lld as requested.",
Andreas Huber32f3cef2011-03-02 15:34:46 -08001530 audio ? "audio" : "video",
1531 mediaTimeUs);
1532
1533 reply->post();
1534 return;
1535 }
1536
1537 skipUntilMediaTimeUs = -1;
1538 }
1539
Chong Zhanga7fa1d92014-06-11 14:49:23 -07001540 if (!audio && mCCDecoder->isSelected()) {
1541 mCCDecoder->display(mediaTimeUs);
1542 }
1543
Andreas Huberf9334412010-12-15 15:17:42 -08001544 mRenderer->queueBuffer(audio, buffer, reply);
1545}
1546
Chong Zhangced1c2f2014-08-08 15:22:35 -07001547void NuPlayer::updateVideoSize(
1548 const sp<AMessage> &inputFormat,
1549 const sp<AMessage> &outputFormat) {
1550 if (inputFormat == NULL) {
1551 ALOGW("Unknown video size, reporting 0x0!");
1552 notifyListener(MEDIA_SET_VIDEO_SIZE, 0, 0);
1553 return;
1554 }
1555
1556 int32_t displayWidth, displayHeight;
1557 int32_t cropLeft, cropTop, cropRight, cropBottom;
1558
1559 if (outputFormat != NULL) {
1560 int32_t width, height;
1561 CHECK(outputFormat->findInt32("width", &width));
1562 CHECK(outputFormat->findInt32("height", &height));
1563
1564 int32_t cropLeft, cropTop, cropRight, cropBottom;
1565 CHECK(outputFormat->findRect(
1566 "crop",
1567 &cropLeft, &cropTop, &cropRight, &cropBottom));
1568
1569 displayWidth = cropRight - cropLeft + 1;
1570 displayHeight = cropBottom - cropTop + 1;
1571
1572 ALOGV("Video output format changed to %d x %d "
1573 "(crop: %d x %d @ (%d, %d))",
1574 width, height,
1575 displayWidth,
1576 displayHeight,
1577 cropLeft, cropTop);
1578 } else {
1579 CHECK(inputFormat->findInt32("width", &displayWidth));
1580 CHECK(inputFormat->findInt32("height", &displayHeight));
1581
1582 ALOGV("Video input format %d x %d", displayWidth, displayHeight);
1583 }
1584
1585 // Take into account sample aspect ratio if necessary:
1586 int32_t sarWidth, sarHeight;
1587 if (inputFormat->findInt32("sar-width", &sarWidth)
1588 && inputFormat->findInt32("sar-height", &sarHeight)) {
1589 ALOGV("Sample aspect ratio %d : %d", sarWidth, sarHeight);
1590
1591 displayWidth = (displayWidth * sarWidth) / sarHeight;
1592
1593 ALOGV("display dimensions %d x %d", displayWidth, displayHeight);
1594 }
1595
1596 int32_t rotationDegrees;
1597 if (!inputFormat->findInt32("rotation-degrees", &rotationDegrees)) {
1598 rotationDegrees = 0;
1599 }
1600
1601 if (rotationDegrees == 90 || rotationDegrees == 270) {
1602 int32_t tmp = displayWidth;
1603 displayWidth = displayHeight;
1604 displayHeight = tmp;
1605 }
1606
1607 notifyListener(
1608 MEDIA_SET_VIDEO_SIZE,
1609 displayWidth,
1610 displayHeight);
1611}
1612
Chong Zhangdcb89b32013-08-06 09:44:47 -07001613void NuPlayer::notifyListener(int msg, int ext1, int ext2, const Parcel *in) {
Andreas Huber43c3e6c2011-01-05 12:17:08 -08001614 if (mDriver == NULL) {
Andreas Huberf9334412010-12-15 15:17:42 -08001615 return;
1616 }
1617
Andreas Huber43c3e6c2011-01-05 12:17:08 -08001618 sp<NuPlayerDriver> driver = mDriver.promote();
Andreas Huberf9334412010-12-15 15:17:42 -08001619
Andreas Huber43c3e6c2011-01-05 12:17:08 -08001620 if (driver == NULL) {
Andreas Huberf9334412010-12-15 15:17:42 -08001621 return;
1622 }
1623
Chong Zhangdcb89b32013-08-06 09:44:47 -07001624 driver->notifyListener(msg, ext1, ext2, in);
Andreas Huberf9334412010-12-15 15:17:42 -08001625}
1626
Lajos Molnar87603c02014-08-20 19:25:30 -07001627void NuPlayer::flushDecoder(
1628 bool audio, bool needShutdown, const sp<AMessage> &newFormat) {
Andreas Huber14f76722013-01-15 09:04:18 -08001629 ALOGV("[%s] flushDecoder needShutdown=%d",
1630 audio ? "audio" : "video", needShutdown);
1631
Lajos Molnar87603c02014-08-20 19:25:30 -07001632 const sp<Decoder> &decoder = getDecoder(audio);
1633 if (decoder == NULL) {
Steve Blockdf64d152012-01-04 20:05:49 +00001634 ALOGI("flushDecoder %s without decoder present",
Andreas Huber6e3d3112011-11-28 12:36:11 -08001635 audio ? "audio" : "video");
Lajos Molnar87603c02014-08-20 19:25:30 -07001636 return;
Andreas Huber6e3d3112011-11-28 12:36:11 -08001637 }
1638
Andreas Huber1aef2112011-01-04 14:01:29 -08001639 // Make sure we don't continue to scan sources until we finish flushing.
1640 ++mScanSourcesGeneration;
Andreas Huber43c3e6c2011-01-05 12:17:08 -08001641 mScanSourcesPending = false;
Andreas Huber1aef2112011-01-04 14:01:29 -08001642
Lajos Molnar87603c02014-08-20 19:25:30 -07001643 decoder->signalFlush(newFormat);
Andreas Huber1aef2112011-01-04 14:01:29 -08001644 mRenderer->flush(audio);
1645
1646 FlushStatus newStatus =
1647 needShutdown ? FLUSHING_DECODER_SHUTDOWN : FLUSHING_DECODER;
1648
1649 if (audio) {
Wei Jia53904f32014-07-29 10:22:53 -07001650 ALOGE_IF(mFlushingAudio != NONE,
1651 "audio flushDecoder() is called in state %d", mFlushingAudio);
Andreas Huber1aef2112011-01-04 14:01:29 -08001652 mFlushingAudio = newStatus;
Andreas Huber1aef2112011-01-04 14:01:29 -08001653 } else {
Wei Jia53904f32014-07-29 10:22:53 -07001654 ALOGE_IF(mFlushingVideo != NONE,
1655 "video flushDecoder() is called in state %d", mFlushingVideo);
Andreas Huber1aef2112011-01-04 14:01:29 -08001656 mFlushingVideo = newStatus;
Chong Zhangb86e68f2014-08-01 13:46:53 -07001657
1658 if (mCCDecoder != NULL) {
1659 mCCDecoder->flush();
1660 }
Andreas Huber1aef2112011-01-04 14:01:29 -08001661 }
1662}
1663
Lajos Molnar87603c02014-08-20 19:25:30 -07001664void NuPlayer::updateDecoderFormatWithoutFlush(
1665 bool audio, const sp<AMessage> &format) {
1666 ALOGV("[%s] updateDecoderFormatWithoutFlush", audio ? "audio" : "video");
1667
1668 const sp<Decoder> &decoder = getDecoder(audio);
1669 if (decoder == NULL) {
1670 ALOGI("updateDecoderFormatWithoutFlush %s without decoder present",
1671 audio ? "audio" : "video");
1672 return;
1673 }
1674
1675 decoder->signalUpdateFormat(format);
1676}
1677
Chong Zhangced1c2f2014-08-08 15:22:35 -07001678void NuPlayer::queueDecoderShutdown(
1679 bool audio, bool video, const sp<AMessage> &reply) {
1680 ALOGI("queueDecoderShutdown audio=%d, video=%d", audio, video);
Andreas Huber84066782011-08-16 09:34:26 -07001681
Chong Zhangced1c2f2014-08-08 15:22:35 -07001682 mDeferredActions.push_back(
1683 new ShutdownDecoderAction(audio, video));
Andreas Huber84066782011-08-16 09:34:26 -07001684
Chong Zhangced1c2f2014-08-08 15:22:35 -07001685 mDeferredActions.push_back(
1686 new SimpleAction(&NuPlayer::performScanSources));
Andreas Huber84066782011-08-16 09:34:26 -07001687
Chong Zhangced1c2f2014-08-08 15:22:35 -07001688 mDeferredActions.push_back(new PostMessageAction(reply));
1689
1690 processDeferredActions();
Andreas Huber84066782011-08-16 09:34:26 -07001691}
1692
James Dong0d268a32012-08-31 12:18:27 -07001693status_t NuPlayer::setVideoScalingMode(int32_t mode) {
1694 mVideoScalingMode = mode;
Andreas Huber57a339c2012-12-03 11:18:00 -08001695 if (mNativeWindow != NULL) {
James Dong0d268a32012-08-31 12:18:27 -07001696 status_t ret = native_window_set_scaling_mode(
1697 mNativeWindow->getNativeWindow().get(), mVideoScalingMode);
1698 if (ret != OK) {
1699 ALOGE("Failed to set scaling mode (%d): %s",
1700 -ret, strerror(-ret));
1701 return ret;
1702 }
1703 }
1704 return OK;
1705}
1706
Chong Zhangdcb89b32013-08-06 09:44:47 -07001707status_t NuPlayer::getTrackInfo(Parcel* reply) const {
1708 sp<AMessage> msg = new AMessage(kWhatGetTrackInfo, id());
1709 msg->setPointer("reply", reply);
1710
1711 sp<AMessage> response;
1712 status_t err = msg->postAndAwaitResponse(&response);
1713 return err;
1714}
1715
Robert Shih7c4f0d72014-07-09 18:53:31 -07001716status_t NuPlayer::getSelectedTrack(int32_t type, Parcel* reply) const {
1717 sp<AMessage> msg = new AMessage(kWhatGetSelectedTrack, id());
1718 msg->setPointer("reply", reply);
1719 msg->setInt32("type", type);
1720
1721 sp<AMessage> response;
1722 status_t err = msg->postAndAwaitResponse(&response);
1723 if (err == OK && response != NULL) {
1724 CHECK(response->findInt32("err", &err));
1725 }
1726 return err;
1727}
1728
Chong Zhangdcb89b32013-08-06 09:44:47 -07001729status_t NuPlayer::selectTrack(size_t trackIndex, bool select) {
1730 sp<AMessage> msg = new AMessage(kWhatSelectTrack, id());
1731 msg->setSize("trackIndex", trackIndex);
1732 msg->setInt32("select", select);
1733
1734 sp<AMessage> response;
1735 status_t err = msg->postAndAwaitResponse(&response);
1736
Chong Zhang404fced2014-06-11 14:45:31 -07001737 if (err != OK) {
1738 return err;
1739 }
1740
1741 if (!response->findInt32("err", &err)) {
1742 err = OK;
1743 }
1744
Chong Zhangdcb89b32013-08-06 09:44:47 -07001745 return err;
1746}
1747
Marco Nelissenf0b72b52014-09-16 15:43:44 -07001748sp<MetaData> NuPlayer::getFileMeta() {
1749 return mSource->getFileFormatMeta();
1750}
1751
Andreas Huberb7c8e912012-11-27 15:02:53 -08001752void NuPlayer::schedulePollDuration() {
1753 sp<AMessage> msg = new AMessage(kWhatPollDuration, id());
1754 msg->setInt32("generation", mPollDurationGeneration);
1755 msg->post();
1756}
1757
1758void NuPlayer::cancelPollDuration() {
1759 ++mPollDurationGeneration;
1760}
1761
Andreas Hubera1f8ab02012-11-30 10:53:22 -08001762void NuPlayer::processDeferredActions() {
1763 while (!mDeferredActions.empty()) {
1764 // We won't execute any deferred actions until we're no longer in
1765 // an intermediate state, i.e. one more more decoders are currently
1766 // flushing or shutting down.
1767
Andreas Hubera1f8ab02012-11-30 10:53:22 -08001768 if (mFlushingAudio != NONE || mFlushingVideo != NONE) {
1769 // We're currently flushing, postpone the reset until that's
1770 // completed.
1771
1772 ALOGV("postponing action mFlushingAudio=%d, mFlushingVideo=%d",
1773 mFlushingAudio, mFlushingVideo);
1774
1775 break;
1776 }
1777
1778 sp<Action> action = *mDeferredActions.begin();
1779 mDeferredActions.erase(mDeferredActions.begin());
1780
1781 action->execute(this);
1782 }
1783}
1784
Wei Jiae427abf2014-09-22 15:21:11 -07001785void NuPlayer::performSeek(int64_t seekTimeUs, bool needNotify) {
1786 ALOGV("performSeek seekTimeUs=%lld us (%.2f secs), needNotify(%d)",
Andreas Hubera1f8ab02012-11-30 10:53:22 -08001787 seekTimeUs,
Wei Jiae427abf2014-09-22 15:21:11 -07001788 seekTimeUs / 1E6,
1789 needNotify);
Andreas Hubera1f8ab02012-11-30 10:53:22 -08001790
Andy Hungadf34bf2014-09-03 18:22:22 -07001791 if (mSource == NULL) {
1792 // This happens when reset occurs right before the loop mode
1793 // asynchronously seeks to the start of the stream.
1794 LOG_ALWAYS_FATAL_IF(mAudioDecoder != NULL || mVideoDecoder != NULL,
1795 "mSource is NULL and decoders not NULL audio(%p) video(%p)",
1796 mAudioDecoder.get(), mVideoDecoder.get());
1797 return;
1798 }
Andreas Hubera1f8ab02012-11-30 10:53:22 -08001799 mSource->seekTo(seekTimeUs);
Robert Shihd3b0bbb2014-07-23 15:00:25 -07001800 ++mTimedTextGeneration;
Andreas Hubera1f8ab02012-11-30 10:53:22 -08001801
1802 if (mDriver != NULL) {
1803 sp<NuPlayerDriver> driver = mDriver.promote();
1804 if (driver != NULL) {
1805 driver->notifyPosition(seekTimeUs);
Wei Jiae427abf2014-09-22 15:21:11 -07001806 if (needNotify) {
1807 driver->notifySeekComplete();
1808 }
Andreas Hubera1f8ab02012-11-30 10:53:22 -08001809 }
1810 }
1811
1812 // everything's flushed, continue playback.
1813}
1814
1815void NuPlayer::performDecoderFlush() {
1816 ALOGV("performDecoderFlush");
1817
Andreas Huberda9740e2013-04-16 10:54:03 -07001818 if (mAudioDecoder == NULL && mVideoDecoder == NULL) {
Andreas Hubera1f8ab02012-11-30 10:53:22 -08001819 return;
1820 }
1821
1822 mTimeDiscontinuityPending = true;
1823
1824 if (mAudioDecoder != NULL) {
1825 flushDecoder(true /* audio */, false /* needShutdown */);
1826 }
1827
1828 if (mVideoDecoder != NULL) {
1829 flushDecoder(false /* audio */, false /* needShutdown */);
1830 }
1831}
1832
Andreas Huber14f76722013-01-15 09:04:18 -08001833void NuPlayer::performDecoderShutdown(bool audio, bool video) {
1834 ALOGV("performDecoderShutdown audio=%d, video=%d", audio, video);
Andreas Hubera1f8ab02012-11-30 10:53:22 -08001835
Andreas Huber14f76722013-01-15 09:04:18 -08001836 if ((!audio || mAudioDecoder == NULL)
1837 && (!video || mVideoDecoder == NULL)) {
Andreas Hubera1f8ab02012-11-30 10:53:22 -08001838 return;
1839 }
1840
1841 mTimeDiscontinuityPending = true;
1842
Andreas Huber14f76722013-01-15 09:04:18 -08001843 if (audio && mAudioDecoder != NULL) {
Andreas Hubera1f8ab02012-11-30 10:53:22 -08001844 flushDecoder(true /* audio */, true /* needShutdown */);
1845 }
1846
Andreas Huber14f76722013-01-15 09:04:18 -08001847 if (video && mVideoDecoder != NULL) {
Andreas Hubera1f8ab02012-11-30 10:53:22 -08001848 flushDecoder(false /* audio */, true /* needShutdown */);
1849 }
1850}
1851
1852void NuPlayer::performReset() {
1853 ALOGV("performReset");
1854
1855 CHECK(mAudioDecoder == NULL);
1856 CHECK(mVideoDecoder == NULL);
1857
1858 cancelPollDuration();
1859
1860 ++mScanSourcesGeneration;
1861 mScanSourcesPending = false;
1862
Wei Jia1008e1c2014-09-09 14:49:08 -07001863 ++mAudioDecoderGeneration;
1864 ++mVideoDecoderGeneration;
1865
Lajos Molnar09524832014-07-17 14:29:51 -07001866 if (mRendererLooper != NULL) {
1867 if (mRenderer != NULL) {
1868 mRendererLooper->unregisterHandler(mRenderer->id());
1869 }
1870 mRendererLooper->stop();
1871 mRendererLooper.clear();
1872 }
Andreas Hubera1f8ab02012-11-30 10:53:22 -08001873 mRenderer.clear();
1874
1875 if (mSource != NULL) {
1876 mSource->stop();
Andreas Huberb5f25f02013-02-05 10:14:26 -08001877
Andreas Hubera1f8ab02012-11-30 10:53:22 -08001878 mSource.clear();
1879 }
1880
1881 if (mDriver != NULL) {
1882 sp<NuPlayerDriver> driver = mDriver.promote();
1883 if (driver != NULL) {
1884 driver->notifyResetComplete();
1885 }
1886 }
Andreas Huber57a339c2012-12-03 11:18:00 -08001887
1888 mStarted = false;
Andreas Hubera1f8ab02012-11-30 10:53:22 -08001889}
1890
1891void NuPlayer::performScanSources() {
1892 ALOGV("performScanSources");
1893
Andreas Huber57a339c2012-12-03 11:18:00 -08001894 if (!mStarted) {
1895 return;
1896 }
1897
Andreas Hubera1f8ab02012-11-30 10:53:22 -08001898 if (mAudioDecoder == NULL || mVideoDecoder == NULL) {
1899 postScanSources();
1900 }
1901}
1902
Andreas Huber57a339c2012-12-03 11:18:00 -08001903void NuPlayer::performSetSurface(const sp<NativeWindowWrapper> &wrapper) {
1904 ALOGV("performSetSurface");
1905
1906 mNativeWindow = wrapper;
1907
1908 // XXX - ignore error from setVideoScalingMode for now
1909 setVideoScalingMode(mVideoScalingMode);
Chong Zhang13d6faa2014-08-22 15:35:28 -07001910
1911 if (mDriver != NULL) {
1912 sp<NuPlayerDriver> driver = mDriver.promote();
1913 if (driver != NULL) {
1914 driver->notifySetSurfaceComplete();
1915 }
1916 }
Andreas Huber57a339c2012-12-03 11:18:00 -08001917}
1918
Andreas Huber9575c962013-02-05 13:59:56 -08001919void NuPlayer::onSourceNotify(const sp<AMessage> &msg) {
1920 int32_t what;
1921 CHECK(msg->findInt32("what", &what));
1922
1923 switch (what) {
1924 case Source::kWhatPrepared:
1925 {
Andreas Huberb5f28d42013-04-25 15:11:19 -07001926 if (mSource == NULL) {
1927 // This is a stale notification from a source that was
1928 // asynchronously preparing when the client called reset().
1929 // We handled the reset, the source is gone.
1930 break;
1931 }
1932
Andreas Huberec0c5972013-02-05 14:47:13 -08001933 int32_t err;
1934 CHECK(msg->findInt32("err", &err));
1935
Andreas Huber9575c962013-02-05 13:59:56 -08001936 sp<NuPlayerDriver> driver = mDriver.promote();
1937 if (driver != NULL) {
Marco Nelissendd114d12014-05-28 15:23:14 -07001938 // notify duration first, so that it's definitely set when
1939 // the app received the "prepare complete" callback.
1940 int64_t durationUs;
1941 if (mSource->getDuration(&durationUs) == OK) {
1942 driver->notifyDuration(durationUs);
1943 }
Andreas Huberec0c5972013-02-05 14:47:13 -08001944 driver->notifyPrepareCompleted(err);
Andreas Huber9575c962013-02-05 13:59:56 -08001945 }
Andreas Huber99759402013-04-01 14:28:31 -07001946
Andreas Huber9575c962013-02-05 13:59:56 -08001947 break;
1948 }
1949
1950 case Source::kWhatFlagsChanged:
1951 {
1952 uint32_t flags;
1953 CHECK(msg->findInt32("flags", (int32_t *)&flags));
1954
Chong Zhang4b7069d2013-09-11 12:52:43 -07001955 sp<NuPlayerDriver> driver = mDriver.promote();
1956 if (driver != NULL) {
1957 driver->notifyFlagsChanged(flags);
1958 }
1959
Andreas Huber9575c962013-02-05 13:59:56 -08001960 if ((mSourceFlags & Source::FLAG_DYNAMIC_DURATION)
1961 && (!(flags & Source::FLAG_DYNAMIC_DURATION))) {
1962 cancelPollDuration();
1963 } else if (!(mSourceFlags & Source::FLAG_DYNAMIC_DURATION)
1964 && (flags & Source::FLAG_DYNAMIC_DURATION)
1965 && (mAudioDecoder != NULL || mVideoDecoder != NULL)) {
1966 schedulePollDuration();
1967 }
1968
1969 mSourceFlags = flags;
1970 break;
1971 }
1972
1973 case Source::kWhatVideoSizeChanged:
1974 {
Chong Zhangced1c2f2014-08-08 15:22:35 -07001975 sp<AMessage> format;
1976 CHECK(msg->findMessage("format", &format));
Andreas Huber9575c962013-02-05 13:59:56 -08001977
Chong Zhangced1c2f2014-08-08 15:22:35 -07001978 updateVideoSize(format);
Andreas Huber9575c962013-02-05 13:59:56 -08001979 break;
1980 }
1981
Chong Zhang2a3cc9a2014-08-21 17:48:26 -07001982 case Source::kWhatBufferingUpdate:
1983 {
1984 int32_t percentage;
1985 CHECK(msg->findInt32("percentage", &percentage));
1986
1987 notifyListener(MEDIA_BUFFERING_UPDATE, percentage, 0);
1988 break;
1989 }
1990
Roger Jönssonb50e83e2013-01-21 16:26:41 +01001991 case Source::kWhatBufferingStart:
1992 {
1993 notifyListener(MEDIA_INFO, MEDIA_INFO_BUFFERING_START, 0);
1994 break;
1995 }
1996
1997 case Source::kWhatBufferingEnd:
1998 {
1999 notifyListener(MEDIA_INFO, MEDIA_INFO_BUFFERING_END, 0);
2000 break;
2001 }
2002
Chong Zhangdcb89b32013-08-06 09:44:47 -07002003 case Source::kWhatSubtitleData:
2004 {
2005 sp<ABuffer> buffer;
2006 CHECK(msg->findBuffer("buffer", &buffer));
2007
Chong Zhang404fced2014-06-11 14:45:31 -07002008 sendSubtitleData(buffer, 0 /* baseIndex */);
Chong Zhangdcb89b32013-08-06 09:44:47 -07002009 break;
2010 }
2011
Robert Shihd3b0bbb2014-07-23 15:00:25 -07002012 case Source::kWhatTimedTextData:
2013 {
2014 int32_t generation;
2015 if (msg->findInt32("generation", &generation)
2016 && generation != mTimedTextGeneration) {
2017 break;
2018 }
2019
2020 sp<ABuffer> buffer;
2021 CHECK(msg->findBuffer("buffer", &buffer));
2022
2023 sp<NuPlayerDriver> driver = mDriver.promote();
2024 if (driver == NULL) {
2025 break;
2026 }
2027
2028 int posMs;
2029 int64_t timeUs, posUs;
2030 driver->getCurrentPosition(&posMs);
2031 posUs = posMs * 1000;
2032 CHECK(buffer->meta()->findInt64("timeUs", &timeUs));
2033
2034 if (posUs < timeUs) {
2035 if (!msg->findInt32("generation", &generation)) {
2036 msg->setInt32("generation", mTimedTextGeneration);
2037 }
2038 msg->post(timeUs - posUs);
2039 } else {
2040 sendTimedTextData(buffer);
2041 }
2042 break;
2043 }
2044
Andreas Huber14f76722013-01-15 09:04:18 -08002045 case Source::kWhatQueueDecoderShutdown:
2046 {
2047 int32_t audio, video;
2048 CHECK(msg->findInt32("audio", &audio));
2049 CHECK(msg->findInt32("video", &video));
2050
2051 sp<AMessage> reply;
2052 CHECK(msg->findMessage("reply", &reply));
2053
2054 queueDecoderShutdown(audio, video, reply);
2055 break;
2056 }
2057
Ronghua Wu80276872014-08-28 15:50:29 -07002058 case Source::kWhatDrmNoLicense:
2059 {
2060 notifyListener(MEDIA_ERROR, MEDIA_ERROR_UNKNOWN, ERROR_DRM_NO_LICENSE);
2061 break;
2062 }
2063
Andreas Huber9575c962013-02-05 13:59:56 -08002064 default:
2065 TRESPASS();
2066 }
2067}
2068
Chong Zhanga7fa1d92014-06-11 14:49:23 -07002069void NuPlayer::onClosedCaptionNotify(const sp<AMessage> &msg) {
2070 int32_t what;
2071 CHECK(msg->findInt32("what", &what));
2072
2073 switch (what) {
2074 case NuPlayer::CCDecoder::kWhatClosedCaptionData:
2075 {
2076 sp<ABuffer> buffer;
2077 CHECK(msg->findBuffer("buffer", &buffer));
2078
2079 size_t inbandTracks = 0;
2080 if (mSource != NULL) {
2081 inbandTracks = mSource->getTrackCount();
2082 }
2083
2084 sendSubtitleData(buffer, inbandTracks);
2085 break;
2086 }
2087
2088 case NuPlayer::CCDecoder::kWhatTrackAdded:
2089 {
2090 notifyListener(MEDIA_INFO, MEDIA_INFO_METADATA_UPDATE, 0);
2091
2092 break;
2093 }
2094
2095 default:
2096 TRESPASS();
2097 }
2098
2099
2100}
2101
Chong Zhang404fced2014-06-11 14:45:31 -07002102void NuPlayer::sendSubtitleData(const sp<ABuffer> &buffer, int32_t baseIndex) {
2103 int32_t trackIndex;
2104 int64_t timeUs, durationUs;
2105 CHECK(buffer->meta()->findInt32("trackIndex", &trackIndex));
2106 CHECK(buffer->meta()->findInt64("timeUs", &timeUs));
2107 CHECK(buffer->meta()->findInt64("durationUs", &durationUs));
2108
2109 Parcel in;
2110 in.writeInt32(trackIndex + baseIndex);
2111 in.writeInt64(timeUs);
2112 in.writeInt64(durationUs);
2113 in.writeInt32(buffer->size());
2114 in.writeInt32(buffer->size());
2115 in.write(buffer->data(), buffer->size());
2116
2117 notifyListener(MEDIA_SUBTITLE_DATA, 0, 0, &in);
2118}
Robert Shihd3b0bbb2014-07-23 15:00:25 -07002119
2120void NuPlayer::sendTimedTextData(const sp<ABuffer> &buffer) {
2121 const void *data;
2122 size_t size = 0;
2123 int64_t timeUs;
2124 int32_t flag = TextDescriptions::LOCAL_DESCRIPTIONS;
2125
2126 AString mime;
2127 CHECK(buffer->meta()->findString("mime", &mime));
2128 CHECK(strcasecmp(mime.c_str(), MEDIA_MIMETYPE_TEXT_3GPP) == 0);
2129
2130 data = buffer->data();
2131 size = buffer->size();
2132
2133 Parcel parcel;
2134 if (size > 0) {
2135 CHECK(buffer->meta()->findInt64("timeUs", &timeUs));
2136 flag |= TextDescriptions::IN_BAND_TEXT_3GPP;
2137 TextDescriptions::getParcelOfDescriptions(
2138 (const uint8_t *)data, size, flag, timeUs / 1000, &parcel);
2139 }
2140
2141 if ((parcel.dataSize() > 0)) {
2142 notifyListener(MEDIA_TIMED_TEXT, 0, 0, &parcel);
2143 } else { // send an empty timed text
2144 notifyListener(MEDIA_TIMED_TEXT, 0, 0);
2145 }
2146}
Andreas Huberb5f25f02013-02-05 10:14:26 -08002147////////////////////////////////////////////////////////////////////////////////
2148
Chong Zhangced1c2f2014-08-08 15:22:35 -07002149sp<AMessage> NuPlayer::Source::getFormat(bool audio) {
2150 sp<MetaData> meta = getFormatMeta(audio);
2151
2152 if (meta == NULL) {
2153 return NULL;
2154 }
2155
2156 sp<AMessage> msg = new AMessage;
2157
2158 if(convertMetaDataToMessage(meta, &msg) == OK) {
2159 return msg;
2160 }
2161 return NULL;
2162}
2163
Andreas Huber9575c962013-02-05 13:59:56 -08002164void NuPlayer::Source::notifyFlagsChanged(uint32_t flags) {
2165 sp<AMessage> notify = dupNotify();
2166 notify->setInt32("what", kWhatFlagsChanged);
2167 notify->setInt32("flags", flags);
2168 notify->post();
2169}
2170
Chong Zhangced1c2f2014-08-08 15:22:35 -07002171void NuPlayer::Source::notifyVideoSizeChanged(const sp<AMessage> &format) {
Andreas Huber9575c962013-02-05 13:59:56 -08002172 sp<AMessage> notify = dupNotify();
2173 notify->setInt32("what", kWhatVideoSizeChanged);
Chong Zhangced1c2f2014-08-08 15:22:35 -07002174 notify->setMessage("format", format);
Andreas Huber9575c962013-02-05 13:59:56 -08002175 notify->post();
2176}
2177
Andreas Huberec0c5972013-02-05 14:47:13 -08002178void NuPlayer::Source::notifyPrepared(status_t err) {
Andreas Huber9575c962013-02-05 13:59:56 -08002179 sp<AMessage> notify = dupNotify();
2180 notify->setInt32("what", kWhatPrepared);
Andreas Huberec0c5972013-02-05 14:47:13 -08002181 notify->setInt32("err", err);
Andreas Huber9575c962013-02-05 13:59:56 -08002182 notify->post();
2183}
2184
Andreas Huber84333e02014-02-07 15:36:10 -08002185void NuPlayer::Source::onMessageReceived(const sp<AMessage> & /* msg */) {
Andreas Huberb5f25f02013-02-05 10:14:26 -08002186 TRESPASS();
2187}
2188
Andreas Huberf9334412010-12-15 15:17:42 -08002189} // namespace android