blob: ceedb40eb99af9bc4a3ebc008f67beb97401135a [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),
Wei Jia57568df2014-09-22 10:16:29 -0700160 mRendererGeneration(0),
Andreas Huber9b80c2b2011-06-30 15:47:02 -0700161 mAudioEOS(false),
Andreas Huberf9334412010-12-15 15:17:42 -0800162 mVideoEOS(false),
Andreas Huber5bc087c2010-12-23 10:27:40 -0800163 mScanSourcesPending(false),
Andreas Huber1aef2112011-01-04 14:01:29 -0800164 mScanSourcesGeneration(0),
Andreas Huberb7c8e912012-11-27 15:02:53 -0800165 mPollDurationGeneration(0),
Robert Shihd3b0bbb2014-07-23 15:00:25 -0700166 mTimedTextGeneration(0),
Andreas Huber6e3d3112011-11-28 12:36:11 -0800167 mTimeDiscontinuityPending(false),
Andreas Huberf9334412010-12-15 15:17:42 -0800168 mFlushingAudio(NONE),
Andreas Huber1aef2112011-01-04 14:01:29 -0800169 mFlushingVideo(NONE),
Andreas Huber3fe62152011-09-16 15:09:22 -0700170 mSkipRenderingAudioUntilMediaTimeUs(-1ll),
171 mSkipRenderingVideoUntilMediaTimeUs(-1ll),
172 mVideoLateByUs(0ll),
173 mNumFramesTotal(0ll),
James Dong0d268a32012-08-31 12:18:27 -0700174 mNumFramesDropped(0ll),
Andreas Huber57a339c2012-12-03 11:18:00 -0800175 mVideoScalingMode(NATIVE_WINDOW_SCALING_MODE_SCALE_TO_WINDOW),
176 mStarted(false) {
Andreas Huberf9334412010-12-15 15:17:42 -0800177}
178
179NuPlayer::~NuPlayer() {
180}
181
Andreas Huber9b80c2b2011-06-30 15:47:02 -0700182void NuPlayer::setUID(uid_t uid) {
183 mUIDValid = true;
184 mUID = uid;
185}
186
Andreas Huber43c3e6c2011-01-05 12:17:08 -0800187void NuPlayer::setDriver(const wp<NuPlayerDriver> &driver) {
188 mDriver = driver;
Andreas Huberf9334412010-12-15 15:17:42 -0800189}
190
Andreas Huber9575c962013-02-05 13:59:56 -0800191void NuPlayer::setDataSourceAsync(const sp<IStreamSource> &source) {
Andreas Huberf9334412010-12-15 15:17:42 -0800192 sp<AMessage> msg = new AMessage(kWhatSetDataSource, id());
193
Andreas Huberb5f25f02013-02-05 10:14:26 -0800194 sp<AMessage> notify = new AMessage(kWhatSourceNotify, id());
195
Andreas Huber240abcc2014-02-13 13:32:37 -0800196 msg->setObject("source", new StreamingSource(notify, source));
Andreas Huber5bc087c2010-12-23 10:27:40 -0800197 msg->post();
198}
Andreas Huberf9334412010-12-15 15:17:42 -0800199
Andreas Huberafed0e12011-09-20 15:39:58 -0700200static bool IsHTTPLiveURL(const char *url) {
201 if (!strncasecmp("http://", url, 7)
Andreas Huber99759402013-04-01 14:28:31 -0700202 || !strncasecmp("https://", url, 8)
203 || !strncasecmp("file://", url, 7)) {
Andreas Huberafed0e12011-09-20 15:39:58 -0700204 size_t len = strlen(url);
205 if (len >= 5 && !strcasecmp(".m3u8", &url[len - 5])) {
206 return true;
207 }
208
209 if (strstr(url,"m3u8")) {
210 return true;
211 }
212 }
213
214 return false;
215}
216
Andreas Huber9575c962013-02-05 13:59:56 -0800217void NuPlayer::setDataSourceAsync(
Andreas Huber1b86fe02014-01-29 11:13:26 -0800218 const sp<IMediaHTTPService> &httpService,
219 const char *url,
220 const KeyedVector<String8, String8> *headers) {
Chong Zhang3de157d2014-08-05 20:54:44 -0700221
Andreas Huber5bc087c2010-12-23 10:27:40 -0800222 sp<AMessage> msg = new AMessage(kWhatSetDataSource, id());
Oscar Rydhé7a33b772012-02-20 10:15:48 +0100223 size_t len = strlen(url);
Andreas Huber5bc087c2010-12-23 10:27:40 -0800224
Andreas Huberb5f25f02013-02-05 10:14:26 -0800225 sp<AMessage> notify = new AMessage(kWhatSourceNotify, id());
226
Andreas Huberafed0e12011-09-20 15:39:58 -0700227 sp<Source> source;
228 if (IsHTTPLiveURL(url)) {
Andreas Huber81e68442014-02-05 11:52:33 -0800229 source = new HTTPLiveSource(notify, httpService, url, headers);
Andreas Huberafed0e12011-09-20 15:39:58 -0700230 } else if (!strncasecmp(url, "rtsp://", 7)) {
Andreas Huber1b86fe02014-01-29 11:13:26 -0800231 source = new RTSPSource(
232 notify, httpService, url, headers, mUIDValid, mUID);
Oscar Rydhé7a33b772012-02-20 10:15:48 +0100233 } else if ((!strncasecmp(url, "http://", 7)
234 || !strncasecmp(url, "https://", 8))
235 && ((len >= 4 && !strcasecmp(".sdp", &url[len - 4]))
236 || strstr(url, ".sdp?"))) {
Andreas Huber1b86fe02014-01-29 11:13:26 -0800237 source = new RTSPSource(
238 notify, httpService, url, headers, mUIDValid, mUID, true);
Andreas Huber2bfdd422011-10-11 15:24:07 -0700239 } else {
Chong Zhang3de157d2014-08-05 20:54:44 -0700240 sp<GenericSource> genericSource =
241 new GenericSource(notify, mUIDValid, mUID);
242 // Don't set FLAG_SECURE on mSourceFlags here for widevine.
243 // The correct flags will be updated in Source::kWhatFlagsChanged
244 // handler when GenericSource is prepared.
Andreas Huber2bfdd422011-10-11 15:24:07 -0700245
Chong Zhanga19f33e2014-08-07 15:35:07 -0700246 status_t err = genericSource->setDataSource(httpService, url, headers);
Chong Zhang3de157d2014-08-05 20:54:44 -0700247
248 if (err == OK) {
249 source = genericSource;
250 } else {
Chong Zhanga19f33e2014-08-07 15:35:07 -0700251 ALOGE("Failed to set data source!");
Chong Zhang3de157d2014-08-05 20:54:44 -0700252 }
253 }
Andreas Huberafed0e12011-09-20 15:39:58 -0700254 msg->setObject("source", source);
255 msg->post();
256}
257
Andreas Huber9575c962013-02-05 13:59:56 -0800258void NuPlayer::setDataSourceAsync(int fd, int64_t offset, int64_t length) {
Andreas Huberafed0e12011-09-20 15:39:58 -0700259 sp<AMessage> msg = new AMessage(kWhatSetDataSource, id());
260
Andreas Huberb5f25f02013-02-05 10:14:26 -0800261 sp<AMessage> notify = new AMessage(kWhatSourceNotify, id());
262
Chong Zhang3de157d2014-08-05 20:54:44 -0700263 sp<GenericSource> source =
264 new GenericSource(notify, mUIDValid, mUID);
265
Chong Zhanga19f33e2014-08-07 15:35:07 -0700266 status_t err = source->setDataSource(fd, offset, length);
Chong Zhang3de157d2014-08-05 20:54:44 -0700267
268 if (err != OK) {
Chong Zhanga19f33e2014-08-07 15:35:07 -0700269 ALOGE("Failed to set data source!");
Chong Zhang3de157d2014-08-05 20:54:44 -0700270 source = NULL;
271 }
272
Andreas Huberafed0e12011-09-20 15:39:58 -0700273 msg->setObject("source", source);
Andreas Huberf9334412010-12-15 15:17:42 -0800274 msg->post();
275}
276
Andreas Huber9575c962013-02-05 13:59:56 -0800277void NuPlayer::prepareAsync() {
278 (new AMessage(kWhatPrepare, id()))->post();
279}
280
Andreas Huber57a339c2012-12-03 11:18:00 -0800281void NuPlayer::setVideoSurfaceTextureAsync(
Andy McFadden8ba01022012-12-18 09:46:54 -0800282 const sp<IGraphicBufferProducer> &bufferProducer) {
Glenn Kasten11731182011-02-08 17:26:17 -0800283 sp<AMessage> msg = new AMessage(kWhatSetVideoNativeWindow, id());
Andreas Huber57a339c2012-12-03 11:18:00 -0800284
Andy McFadden8ba01022012-12-18 09:46:54 -0800285 if (bufferProducer == NULL) {
Andreas Huber57a339c2012-12-03 11:18:00 -0800286 msg->setObject("native-window", NULL);
287 } else {
288 msg->setObject(
289 "native-window",
290 new NativeWindowWrapper(
Wei Jia9c03a402014-08-26 15:24:43 -0700291 new Surface(bufferProducer, true /* controlledByApp */)));
Andreas Huber57a339c2012-12-03 11:18:00 -0800292 }
293
Andreas Huberf9334412010-12-15 15:17:42 -0800294 msg->post();
295}
296
297void NuPlayer::setAudioSink(const sp<MediaPlayerBase::AudioSink> &sink) {
298 sp<AMessage> msg = new AMessage(kWhatSetAudioSink, id());
299 msg->setObject("sink", sink);
300 msg->post();
301}
302
303void NuPlayer::start() {
304 (new AMessage(kWhatStart, id()))->post();
305}
306
Andreas Huber43c3e6c2011-01-05 12:17:08 -0800307void NuPlayer::pause() {
Andreas Huberb4082222011-01-20 15:23:04 -0800308 (new AMessage(kWhatPause, id()))->post();
Andreas Huber43c3e6c2011-01-05 12:17:08 -0800309}
310
311void NuPlayer::resume() {
Andreas Huberb4082222011-01-20 15:23:04 -0800312 (new AMessage(kWhatResume, id()))->post();
Andreas Huber43c3e6c2011-01-05 12:17:08 -0800313}
314
Andreas Huber1aef2112011-01-04 14:01:29 -0800315void NuPlayer::resetAsync() {
Chong Zhang48296b72014-09-14 14:28:45 -0700316 if (mSource != NULL) {
317 // During a reset, the data source might be unresponsive already, we need to
318 // disconnect explicitly so that reads exit promptly.
319 // We can't queue the disconnect request to the looper, as it might be
320 // queued behind a stuck read and never gets processed.
321 // Doing a disconnect outside the looper to allows the pending reads to exit
322 // (either successfully or with error).
323 mSource->disconnect();
324 }
325
Andreas Huber1aef2112011-01-04 14:01:29 -0800326 (new AMessage(kWhatReset, id()))->post();
327}
328
Wei Jiae427abf2014-09-22 15:21:11 -0700329void NuPlayer::seekToAsync(int64_t seekTimeUs, bool needNotify) {
Andreas Huber43c3e6c2011-01-05 12:17:08 -0800330 sp<AMessage> msg = new AMessage(kWhatSeek, id());
331 msg->setInt64("seekTimeUs", seekTimeUs);
Wei Jiae427abf2014-09-22 15:21:11 -0700332 msg->setInt32("needNotify", needNotify);
Andreas Huber43c3e6c2011-01-05 12:17:08 -0800333 msg->post();
334}
335
Andreas Huber53df1a42010-12-22 10:03:04 -0800336// static
Andreas Huber1aef2112011-01-04 14:01:29 -0800337bool NuPlayer::IsFlushingState(FlushStatus state, bool *needShutdown) {
Andreas Huber53df1a42010-12-22 10:03:04 -0800338 switch (state) {
339 case FLUSHING_DECODER:
Andreas Huber1aef2112011-01-04 14:01:29 -0800340 if (needShutdown != NULL) {
341 *needShutdown = false;
Andreas Huber53df1a42010-12-22 10:03:04 -0800342 }
343 return true;
344
Andreas Huber1aef2112011-01-04 14:01:29 -0800345 case FLUSHING_DECODER_SHUTDOWN:
346 if (needShutdown != NULL) {
347 *needShutdown = true;
Andreas Huber53df1a42010-12-22 10:03:04 -0800348 }
349 return true;
350
351 default:
352 return false;
353 }
354}
355
Chong Zhang404fced2014-06-11 14:45:31 -0700356void NuPlayer::writeTrackInfo(
357 Parcel* reply, const sp<AMessage> format) const {
358 int32_t trackType;
359 CHECK(format->findInt32("type", &trackType));
360
361 AString lang;
362 CHECK(format->findString("language", &lang));
363
364 reply->writeInt32(2); // write something non-zero
365 reply->writeInt32(trackType);
366 reply->writeString16(String16(lang.c_str()));
367
368 if (trackType == MEDIA_TRACK_TYPE_SUBTITLE) {
369 AString mime;
370 CHECK(format->findString("mime", &mime));
371
372 int32_t isAuto, isDefault, isForced;
373 CHECK(format->findInt32("auto", &isAuto));
374 CHECK(format->findInt32("default", &isDefault));
375 CHECK(format->findInt32("forced", &isForced));
376
377 reply->writeString16(String16(mime.c_str()));
378 reply->writeInt32(isAuto);
379 reply->writeInt32(isDefault);
380 reply->writeInt32(isForced);
381 }
382}
383
Andreas Huberf9334412010-12-15 15:17:42 -0800384void NuPlayer::onMessageReceived(const sp<AMessage> &msg) {
385 switch (msg->what()) {
386 case kWhatSetDataSource:
387 {
Steve Block3856b092011-10-20 11:56:00 +0100388 ALOGV("kWhatSetDataSource");
Andreas Huberf9334412010-12-15 15:17:42 -0800389
390 CHECK(mSource == NULL);
391
Chong Zhang3de157d2014-08-05 20:54:44 -0700392 status_t err = OK;
Andreas Huber5bc087c2010-12-23 10:27:40 -0800393 sp<RefBase> obj;
394 CHECK(msg->findObject("source", &obj));
Chong Zhang3de157d2014-08-05 20:54:44 -0700395 if (obj != NULL) {
396 mSource = static_cast<Source *>(obj.get());
Chong Zhang3de157d2014-08-05 20:54:44 -0700397 } else {
398 err = UNKNOWN_ERROR;
399 }
Andreas Huber9575c962013-02-05 13:59:56 -0800400
401 CHECK(mDriver != NULL);
402 sp<NuPlayerDriver> driver = mDriver.promote();
403 if (driver != NULL) {
Chong Zhang3de157d2014-08-05 20:54:44 -0700404 driver->notifySetDataSourceCompleted(err);
Andreas Huber9575c962013-02-05 13:59:56 -0800405 }
406 break;
407 }
408
409 case kWhatPrepare:
410 {
411 mSource->prepareAsync();
Andreas Huberf9334412010-12-15 15:17:42 -0800412 break;
413 }
414
Chong Zhangdcb89b32013-08-06 09:44:47 -0700415 case kWhatGetTrackInfo:
416 {
417 uint32_t replyID;
418 CHECK(msg->senderAwaitsResponse(&replyID));
419
Chong Zhang404fced2014-06-11 14:45:31 -0700420 Parcel* reply;
421 CHECK(msg->findPointer("reply", (void**)&reply));
422
423 size_t inbandTracks = 0;
Chong Zhangdcb89b32013-08-06 09:44:47 -0700424 if (mSource != NULL) {
Chong Zhang404fced2014-06-11 14:45:31 -0700425 inbandTracks = mSource->getTrackCount();
426 }
427
Chong Zhanga7fa1d92014-06-11 14:49:23 -0700428 size_t ccTracks = 0;
429 if (mCCDecoder != NULL) {
430 ccTracks = mCCDecoder->getTrackCount();
431 }
432
Chong Zhang404fced2014-06-11 14:45:31 -0700433 // total track count
Chong Zhanga7fa1d92014-06-11 14:49:23 -0700434 reply->writeInt32(inbandTracks + ccTracks);
Chong Zhang404fced2014-06-11 14:45:31 -0700435
436 // write inband tracks
437 for (size_t i = 0; i < inbandTracks; ++i) {
438 writeTrackInfo(reply, mSource->getTrackInfo(i));
Chong Zhangdcb89b32013-08-06 09:44:47 -0700439 }
440
Chong Zhanga7fa1d92014-06-11 14:49:23 -0700441 // write CC track
442 for (size_t i = 0; i < ccTracks; ++i) {
443 writeTrackInfo(reply, mCCDecoder->getTrackInfo(i));
444 }
445
Chong Zhangdcb89b32013-08-06 09:44:47 -0700446 sp<AMessage> response = new AMessage;
Chong Zhangdcb89b32013-08-06 09:44:47 -0700447 response->postReply(replyID);
448 break;
449 }
450
Robert Shih7c4f0d72014-07-09 18:53:31 -0700451 case kWhatGetSelectedTrack:
452 {
453 status_t err = INVALID_OPERATION;
454 if (mSource != NULL) {
455 err = OK;
456
457 int32_t type32;
458 CHECK(msg->findInt32("type", (int32_t*)&type32));
459 media_track_type type = (media_track_type)type32;
460 ssize_t selectedTrack = mSource->getSelectedTrack(type);
461
462 Parcel* reply;
463 CHECK(msg->findPointer("reply", (void**)&reply));
464 reply->writeInt32(selectedTrack);
465 }
466
467 sp<AMessage> response = new AMessage;
468 response->setInt32("err", err);
469
470 uint32_t replyID;
471 CHECK(msg->senderAwaitsResponse(&replyID));
472 response->postReply(replyID);
473 break;
474 }
475
Chong Zhangdcb89b32013-08-06 09:44:47 -0700476 case kWhatSelectTrack:
477 {
478 uint32_t replyID;
479 CHECK(msg->senderAwaitsResponse(&replyID));
480
Chong Zhang404fced2014-06-11 14:45:31 -0700481 size_t trackIndex;
482 int32_t select;
483 CHECK(msg->findSize("trackIndex", &trackIndex));
484 CHECK(msg->findInt32("select", &select));
485
Chong Zhangdcb89b32013-08-06 09:44:47 -0700486 status_t err = INVALID_OPERATION;
Chong Zhang404fced2014-06-11 14:45:31 -0700487
488 size_t inbandTracks = 0;
Chong Zhangdcb89b32013-08-06 09:44:47 -0700489 if (mSource != NULL) {
Chong Zhang404fced2014-06-11 14:45:31 -0700490 inbandTracks = mSource->getTrackCount();
491 }
Chong Zhanga7fa1d92014-06-11 14:49:23 -0700492 size_t ccTracks = 0;
493 if (mCCDecoder != NULL) {
494 ccTracks = mCCDecoder->getTrackCount();
495 }
Chong Zhang404fced2014-06-11 14:45:31 -0700496
497 if (trackIndex < inbandTracks) {
Chong Zhangdcb89b32013-08-06 09:44:47 -0700498 err = mSource->selectTrack(trackIndex, select);
Robert Shihd3b0bbb2014-07-23 15:00:25 -0700499
500 if (!select && err == OK) {
501 int32_t type;
502 sp<AMessage> info = mSource->getTrackInfo(trackIndex);
503 if (info != NULL
504 && info->findInt32("type", &type)
505 && type == MEDIA_TRACK_TYPE_TIMEDTEXT) {
506 ++mTimedTextGeneration;
507 }
508 }
Chong Zhanga7fa1d92014-06-11 14:49:23 -0700509 } else {
510 trackIndex -= inbandTracks;
511
512 if (trackIndex < ccTracks) {
513 err = mCCDecoder->selectTrack(trackIndex, select);
514 }
Chong Zhangdcb89b32013-08-06 09:44:47 -0700515 }
516
517 sp<AMessage> response = new AMessage;
518 response->setInt32("err", err);
519
520 response->postReply(replyID);
521 break;
522 }
523
Andreas Huberb7c8e912012-11-27 15:02:53 -0800524 case kWhatPollDuration:
525 {
526 int32_t generation;
527 CHECK(msg->findInt32("generation", &generation));
528
529 if (generation != mPollDurationGeneration) {
530 // stale
531 break;
532 }
533
534 int64_t durationUs;
535 if (mDriver != NULL && mSource->getDuration(&durationUs) == OK) {
536 sp<NuPlayerDriver> driver = mDriver.promote();
537 if (driver != NULL) {
538 driver->notifyDuration(durationUs);
539 }
540 }
541
542 msg->post(1000000ll); // poll again in a second.
543 break;
544 }
545
Glenn Kasten11731182011-02-08 17:26:17 -0800546 case kWhatSetVideoNativeWindow:
Andreas Huberf9334412010-12-15 15:17:42 -0800547 {
Steve Block3856b092011-10-20 11:56:00 +0100548 ALOGV("kWhatSetVideoNativeWindow");
Andreas Huberf9334412010-12-15 15:17:42 -0800549
Andreas Huber57a339c2012-12-03 11:18:00 -0800550 mDeferredActions.push_back(
Andreas Huber14f76722013-01-15 09:04:18 -0800551 new ShutdownDecoderAction(
552 false /* audio */, true /* video */));
Andreas Huber57a339c2012-12-03 11:18:00 -0800553
Andreas Huberf9334412010-12-15 15:17:42 -0800554 sp<RefBase> obj;
Glenn Kasten11731182011-02-08 17:26:17 -0800555 CHECK(msg->findObject("native-window", &obj));
Andreas Huberf9334412010-12-15 15:17:42 -0800556
Andreas Huber57a339c2012-12-03 11:18:00 -0800557 mDeferredActions.push_back(
558 new SetSurfaceAction(
559 static_cast<NativeWindowWrapper *>(obj.get())));
James Dong0d268a32012-08-31 12:18:27 -0700560
Andreas Huber57a339c2012-12-03 11:18:00 -0800561 if (obj != NULL) {
Andy Hung73535852014-09-05 11:42:58 -0700562 if (mStarted && mVideoDecoder != NULL) {
563 // Issue a seek to refresh the video screen only if started otherwise
564 // the extractor may not yet be started and will assert.
565 // If the video decoder is not set (perhaps audio only in this case)
566 // do not perform a seek as it is not needed.
Wei Jiae427abf2014-09-22 15:21:11 -0700567 mDeferredActions.push_back(
568 new SeekAction(mCurrentPositionUs, false /* needNotify */));
Andy Hung73535852014-09-05 11:42:58 -0700569 }
Wei Jiaac428aa2014-09-02 19:01:34 -0700570
Andreas Huber57a339c2012-12-03 11:18:00 -0800571 // If there is a new surface texture, instantiate decoders
572 // again if possible.
573 mDeferredActions.push_back(
574 new SimpleAction(&NuPlayer::performScanSources));
575 }
576
577 processDeferredActions();
Andreas Huberf9334412010-12-15 15:17:42 -0800578 break;
579 }
580
581 case kWhatSetAudioSink:
582 {
Steve Block3856b092011-10-20 11:56:00 +0100583 ALOGV("kWhatSetAudioSink");
Andreas Huberf9334412010-12-15 15:17:42 -0800584
585 sp<RefBase> obj;
586 CHECK(msg->findObject("sink", &obj));
587
588 mAudioSink = static_cast<MediaPlayerBase::AudioSink *>(obj.get());
589 break;
590 }
591
592 case kWhatStart:
593 {
Steve Block3856b092011-10-20 11:56:00 +0100594 ALOGV("kWhatStart");
Andreas Huber43c3e6c2011-01-05 12:17:08 -0800595
Andreas Huber3fe62152011-09-16 15:09:22 -0700596 mVideoIsAVC = false;
Wei Jiabc2fb722014-07-08 16:37:57 -0700597 mOffloadAudio = false;
Andreas Huber1aef2112011-01-04 14:01:29 -0800598 mAudioEOS = false;
599 mVideoEOS = false;
Andreas Huber32f3cef2011-03-02 15:34:46 -0800600 mSkipRenderingAudioUntilMediaTimeUs = -1;
601 mSkipRenderingVideoUntilMediaTimeUs = -1;
Andreas Huber3fe62152011-09-16 15:09:22 -0700602 mVideoLateByUs = 0;
603 mNumFramesTotal = 0;
604 mNumFramesDropped = 0;
Andreas Huber57a339c2012-12-03 11:18:00 -0800605 mStarted = true;
Andreas Huber1aef2112011-01-04 14:01:29 -0800606
Lajos Molnar09524832014-07-17 14:29:51 -0700607 /* instantiate decoders now for secure playback */
608 if (mSourceFlags & Source::FLAG_SECURE) {
609 if (mNativeWindow != NULL) {
610 instantiateDecoder(false, &mVideoDecoder);
611 }
612
613 if (mAudioSink != NULL) {
614 instantiateDecoder(true, &mAudioDecoder);
615 }
616 }
617
Andreas Huber5bc087c2010-12-23 10:27:40 -0800618 mSource->start();
Andreas Huberf9334412010-12-15 15:17:42 -0800619
Andreas Huberd5e56232013-03-12 11:01:43 -0700620 uint32_t flags = 0;
621
622 if (mSource->isRealTime()) {
623 flags |= Renderer::FLAG_REAL_TIME;
624 }
625
Wei Jiabc2fb722014-07-08 16:37:57 -0700626 sp<MetaData> audioMeta = mSource->getFormatMeta(true /* audio */);
627 audio_stream_type_t streamType = AUDIO_STREAM_MUSIC;
628 if (mAudioSink != NULL) {
629 streamType = mAudioSink->getAudioStreamType();
630 }
631
632 sp<AMessage> videoFormat = mSource->getFormat(false /* audio */);
633
634 mOffloadAudio =
635 canOffloadStream(audioMeta, (videoFormat != NULL),
636 true /* is_streaming */, streamType);
637 if (mOffloadAudio) {
638 flags |= Renderer::FLAG_OFFLOAD_AUDIO;
639 }
640
Wei Jia57568df2014-09-22 10:16:29 -0700641 sp<AMessage> notify = new AMessage(kWhatRendererNotify, id());
642 ++mRendererGeneration;
643 notify->setInt32("generation", mRendererGeneration);
644 mRenderer = new Renderer(mAudioSink, notify, flags);
Andreas Huberf9334412010-12-15 15:17:42 -0800645
Lajos Molnar09524832014-07-17 14:29:51 -0700646 mRendererLooper = new ALooper;
647 mRendererLooper->setName("NuPlayerRenderer");
648 mRendererLooper->start(false, false, ANDROID_PRIORITY_AUDIO);
649 mRendererLooper->registerHandler(mRenderer);
Andreas Huberf9334412010-12-15 15:17:42 -0800650
Lajos Molnarc851b5d2014-09-18 14:14:29 -0700651 sp<MetaData> meta = getFileMeta();
652 int32_t rate;
653 if (meta != NULL
654 && meta->findInt32(kKeyFrameRate, &rate) && rate > 0) {
655 mRenderer->setVideoFrameRate(rate);
656 }
657
Andreas Huber1aef2112011-01-04 14:01:29 -0800658 postScanSources();
Andreas Huberf9334412010-12-15 15:17:42 -0800659 break;
660 }
661
662 case kWhatScanSources:
663 {
Andreas Huber1aef2112011-01-04 14:01:29 -0800664 int32_t generation;
665 CHECK(msg->findInt32("generation", &generation));
666 if (generation != mScanSourcesGeneration) {
667 // Drop obsolete msg.
668 break;
669 }
670
Andreas Huber5bc087c2010-12-23 10:27:40 -0800671 mScanSourcesPending = false;
672
Steve Block3856b092011-10-20 11:56:00 +0100673 ALOGV("scanning sources haveAudio=%d, haveVideo=%d",
Andreas Huber43c3e6c2011-01-05 12:17:08 -0800674 mAudioDecoder != NULL, mVideoDecoder != NULL);
675
Andreas Huberb7c8e912012-11-27 15:02:53 -0800676 bool mHadAnySourcesBefore =
677 (mAudioDecoder != NULL) || (mVideoDecoder != NULL);
678
Andy Hung282a7e32014-08-14 15:56:34 -0700679 // initialize video before audio because successful initialization of
680 // video may change deep buffer mode of audio.
Haynes Mathew George5d246ef2012-07-09 10:36:57 -0700681 if (mNativeWindow != NULL) {
682 instantiateDecoder(false, &mVideoDecoder);
683 }
Andreas Huberf9334412010-12-15 15:17:42 -0800684
685 if (mAudioSink != NULL) {
Andy Hung282a7e32014-08-14 15:56:34 -0700686 if (mOffloadAudio) {
687 // open audio sink early under offload mode.
688 sp<AMessage> format = mSource->getFormat(true /*audio*/);
689 openAudioSink(format, true /*offloadOnly*/);
690 }
Andreas Huber5bc087c2010-12-23 10:27:40 -0800691 instantiateDecoder(true, &mAudioDecoder);
Andreas Huberf9334412010-12-15 15:17:42 -0800692 }
693
Andreas Huberb7c8e912012-11-27 15:02:53 -0800694 if (!mHadAnySourcesBefore
695 && (mAudioDecoder != NULL || mVideoDecoder != NULL)) {
696 // This is the first time we've found anything playable.
697
Andreas Huber9575c962013-02-05 13:59:56 -0800698 if (mSourceFlags & Source::FLAG_DYNAMIC_DURATION) {
Andreas Huberb7c8e912012-11-27 15:02:53 -0800699 schedulePollDuration();
700 }
701 }
702
Andreas Hubereac68ba2011-09-27 12:12:25 -0700703 status_t err;
704 if ((err = mSource->feedMoreTSData()) != OK) {
Andreas Huber1aef2112011-01-04 14:01:29 -0800705 if (mAudioDecoder == NULL && mVideoDecoder == NULL) {
706 // We're not currently decoding anything (no audio or
707 // video tracks found) and we just ran out of input data.
Andreas Hubereac68ba2011-09-27 12:12:25 -0700708
709 if (err == ERROR_END_OF_STREAM) {
710 notifyListener(MEDIA_PLAYBACK_COMPLETE, 0, 0);
711 } else {
712 notifyListener(MEDIA_ERROR, MEDIA_ERROR_UNKNOWN, err);
713 }
Andreas Huber1aef2112011-01-04 14:01:29 -0800714 }
Andreas Huberf9334412010-12-15 15:17:42 -0800715 break;
716 }
717
Andreas Huberfbe9d812012-08-31 14:05:27 -0700718 if ((mAudioDecoder == NULL && mAudioSink != NULL)
719 || (mVideoDecoder == NULL && mNativeWindow != NULL)) {
Andreas Huberf9334412010-12-15 15:17:42 -0800720 msg->post(100000ll);
Andreas Huber5bc087c2010-12-23 10:27:40 -0800721 mScanSourcesPending = true;
Andreas Huberf9334412010-12-15 15:17:42 -0800722 }
723 break;
724 }
725
726 case kWhatVideoNotify:
727 case kWhatAudioNotify:
728 {
729 bool audio = msg->what() == kWhatAudioNotify;
730
Wei Jia88703c32014-08-06 11:24:07 -0700731 int32_t currentDecoderGeneration =
732 (audio? mAudioDecoderGeneration : mVideoDecoderGeneration);
733 int32_t requesterGeneration = currentDecoderGeneration - 1;
734 CHECK(msg->findInt32("generation", &requesterGeneration));
735
736 if (requesterGeneration != currentDecoderGeneration) {
737 ALOGV("got message from old %s decoder, generation(%d:%d)",
738 audio ? "audio" : "video", requesterGeneration,
739 currentDecoderGeneration);
740 sp<AMessage> reply;
741 if (!(msg->findMessage("reply", &reply))) {
742 return;
743 }
744
745 reply->setInt32("err", INFO_DISCONTINUITY);
746 reply->post();
747 return;
748 }
749
Andreas Huberf9334412010-12-15 15:17:42 -0800750 int32_t what;
Lajos Molnar1cd13982014-01-17 15:12:51 -0800751 CHECK(msg->findInt32("what", &what));
Andreas Huberf9334412010-12-15 15:17:42 -0800752
Lajos Molnar1cd13982014-01-17 15:12:51 -0800753 if (what == Decoder::kWhatFillThisBuffer) {
Andreas Huberf9334412010-12-15 15:17:42 -0800754 status_t err = feedDecoderInputData(
Lajos Molnar1cd13982014-01-17 15:12:51 -0800755 audio, msg);
Andreas Huberf9334412010-12-15 15:17:42 -0800756
Andreas Huber5bc087c2010-12-23 10:27:40 -0800757 if (err == -EWOULDBLOCK) {
Andreas Hubereac68ba2011-09-27 12:12:25 -0700758 if (mSource->feedMoreTSData() == OK) {
Phil Burkc5cc2e22014-09-09 20:08:39 -0700759 msg->post(10 * 1000ll);
Andreas Huber5bc087c2010-12-23 10:27:40 -0800760 }
Andreas Huberf9334412010-12-15 15:17:42 -0800761 }
Lajos Molnar1cd13982014-01-17 15:12:51 -0800762 } else if (what == Decoder::kWhatEOS) {
Andreas Huberdc9bacd2011-09-26 10:53:29 -0700763 int32_t err;
Lajos Molnar1cd13982014-01-17 15:12:51 -0800764 CHECK(msg->findInt32("err", &err));
Andreas Huberdc9bacd2011-09-26 10:53:29 -0700765
766 if (err == ERROR_END_OF_STREAM) {
Steve Block3856b092011-10-20 11:56:00 +0100767 ALOGV("got %s decoder EOS", audio ? "audio" : "video");
Andreas Huberdc9bacd2011-09-26 10:53:29 -0700768 } else {
Steve Block3856b092011-10-20 11:56:00 +0100769 ALOGV("got %s decoder EOS w/ error %d",
Andreas Huberdc9bacd2011-09-26 10:53:29 -0700770 audio ? "audio" : "video",
771 err);
772 }
773
774 mRenderer->queueEOS(audio, err);
Lajos Molnar1cd13982014-01-17 15:12:51 -0800775 } else if (what == Decoder::kWhatFlushCompleted) {
Andreas Huber1aef2112011-01-04 14:01:29 -0800776 bool needShutdown;
Andreas Huber53df1a42010-12-22 10:03:04 -0800777
Andreas Huberf9334412010-12-15 15:17:42 -0800778 if (audio) {
Andreas Huber1aef2112011-01-04 14:01:29 -0800779 CHECK(IsFlushingState(mFlushingAudio, &needShutdown));
Andreas Huberf9334412010-12-15 15:17:42 -0800780 mFlushingAudio = FLUSHED;
781 } else {
Andreas Huber1aef2112011-01-04 14:01:29 -0800782 CHECK(IsFlushingState(mFlushingVideo, &needShutdown));
Andreas Huberf9334412010-12-15 15:17:42 -0800783 mFlushingVideo = FLUSHED;
Andreas Huber3fe62152011-09-16 15:09:22 -0700784
785 mVideoLateByUs = 0;
Andreas Huberf9334412010-12-15 15:17:42 -0800786 }
787
Steve Block3856b092011-10-20 11:56:00 +0100788 ALOGV("decoder %s flush completed", audio ? "audio" : "video");
Andreas Huberf9334412010-12-15 15:17:42 -0800789
Andreas Huber1aef2112011-01-04 14:01:29 -0800790 if (needShutdown) {
Steve Block3856b092011-10-20 11:56:00 +0100791 ALOGV("initiating %s decoder shutdown",
Andreas Huber53df1a42010-12-22 10:03:04 -0800792 audio ? "audio" : "video");
Andreas Huberf9334412010-12-15 15:17:42 -0800793
Lajos Molnar87603c02014-08-20 19:25:30 -0700794 getDecoder(audio)->initiateShutdown();
Andreas Huberf9334412010-12-15 15:17:42 -0800795
Andreas Huber53df1a42010-12-22 10:03:04 -0800796 if (audio) {
797 mFlushingAudio = SHUTTING_DOWN_DECODER;
798 } else {
799 mFlushingVideo = SHUTTING_DOWN_DECODER;
800 }
Andreas Huberf9334412010-12-15 15:17:42 -0800801 }
Andreas Huber3831a062010-12-21 10:22:33 -0800802
803 finishFlushIfPossible();
Lajos Molnar1cd13982014-01-17 15:12:51 -0800804 } else if (what == Decoder::kWhatOutputFormatChanged) {
805 sp<AMessage> format;
806 CHECK(msg->findMessage("format", &format));
807
Andreas Huber31e25082011-01-10 10:38:31 -0800808 if (audio) {
Andy Hung282a7e32014-08-14 15:56:34 -0700809 openAudioSink(format, false /*offloadOnly*/);
Andreas Huber31e25082011-01-10 10:38:31 -0800810 } else {
811 // video
Chong Zhangced1c2f2014-08-08 15:22:35 -0700812 sp<AMessage> inputFormat =
813 mSource->getFormat(false /* audio */);
Andreas Huber3831a062010-12-21 10:22:33 -0800814
Chong Zhangced1c2f2014-08-08 15:22:35 -0700815 updateVideoSize(inputFormat, format);
Andreas Huber31e25082011-01-10 10:38:31 -0800816 }
Lajos Molnar1cd13982014-01-17 15:12:51 -0800817 } else if (what == Decoder::kWhatShutdownCompleted) {
Steve Block3856b092011-10-20 11:56:00 +0100818 ALOGV("%s shutdown completed", audio ? "audio" : "video");
Andreas Huber3831a062010-12-21 10:22:33 -0800819 if (audio) {
820 mAudioDecoder.clear();
Wei Jia57568df2014-09-22 10:16:29 -0700821 ++mAudioDecoderGeneration;
Andreas Huber3831a062010-12-21 10:22:33 -0800822
823 CHECK_EQ((int)mFlushingAudio, (int)SHUTTING_DOWN_DECODER);
824 mFlushingAudio = SHUT_DOWN;
825 } else {
826 mVideoDecoder.clear();
Wei Jia57568df2014-09-22 10:16:29 -0700827 ++mVideoDecoderGeneration;
Andreas Huber3831a062010-12-21 10:22:33 -0800828
829 CHECK_EQ((int)mFlushingVideo, (int)SHUTTING_DOWN_DECODER);
830 mFlushingVideo = SHUT_DOWN;
831 }
832
833 finishFlushIfPossible();
Lajos Molnar1cd13982014-01-17 15:12:51 -0800834 } else if (what == Decoder::kWhatError) {
Steve Block29357bc2012-01-06 19:20:56 +0000835 ALOGE("Received error from %s decoder, aborting playback.",
Andreas Huberc92fd242011-08-16 13:48:44 -0700836 audio ? "audio" : "video");
837
Chong Zhangf4c0a942014-08-11 15:14:10 -0700838 status_t err;
839 if (!msg->findInt32("err", &err)) {
840 err = UNKNOWN_ERROR;
841 }
842 mRenderer->queueEOS(audio, err);
Marco Nelissen9e2b7912014-08-18 16:13:03 -0700843 if (audio && mFlushingAudio != NONE) {
844 mAudioDecoder.clear();
Wei Jia57568df2014-09-22 10:16:29 -0700845 ++mAudioDecoderGeneration;
Marco Nelissen9e2b7912014-08-18 16:13:03 -0700846 mFlushingAudio = SHUT_DOWN;
847 } else if (!audio && mFlushingVideo != NONE){
848 mVideoDecoder.clear();
Wei Jia57568df2014-09-22 10:16:29 -0700849 ++mVideoDecoderGeneration;
Marco Nelissen9e2b7912014-08-18 16:13:03 -0700850 mFlushingVideo = SHUT_DOWN;
851 }
852 finishFlushIfPossible();
Lajos Molnar1cd13982014-01-17 15:12:51 -0800853 } else if (what == Decoder::kWhatDrainThisBuffer) {
854 renderBuffer(audio, msg);
855 } else {
856 ALOGV("Unhandled decoder notification %d '%c%c%c%c'.",
Andreas Hubera1f8ab02012-11-30 10:53:22 -0800857 what,
858 what >> 24,
859 (what >> 16) & 0xff,
860 (what >> 8) & 0xff,
861 what & 0xff);
Andreas Huberf9334412010-12-15 15:17:42 -0800862 }
863
864 break;
865 }
866
867 case kWhatRendererNotify:
868 {
Wei Jia57568df2014-09-22 10:16:29 -0700869 int32_t requesterGeneration = mRendererGeneration - 1;
870 CHECK(msg->findInt32("generation", &requesterGeneration));
871 if (requesterGeneration != mRendererGeneration) {
872 ALOGV("got message from old renderer, generation(%d:%d)",
873 requesterGeneration, mRendererGeneration);
874 return;
875 }
876
Andreas Huberf9334412010-12-15 15:17:42 -0800877 int32_t what;
878 CHECK(msg->findInt32("what", &what));
879
880 if (what == Renderer::kWhatEOS) {
881 int32_t audio;
882 CHECK(msg->findInt32("audio", &audio));
883
Andreas Huberc92fd242011-08-16 13:48:44 -0700884 int32_t finalResult;
885 CHECK(msg->findInt32("finalResult", &finalResult));
886
Andreas Huberf9334412010-12-15 15:17:42 -0800887 if (audio) {
888 mAudioEOS = true;
889 } else {
890 mVideoEOS = true;
891 }
892
Andreas Huberc92fd242011-08-16 13:48:44 -0700893 if (finalResult == ERROR_END_OF_STREAM) {
Steve Block3856b092011-10-20 11:56:00 +0100894 ALOGV("reached %s EOS", audio ? "audio" : "video");
Andreas Huberc92fd242011-08-16 13:48:44 -0700895 } else {
Steve Block29357bc2012-01-06 19:20:56 +0000896 ALOGE("%s track encountered an error (%d)",
Andreas Huberc92fd242011-08-16 13:48:44 -0700897 audio ? "audio" : "video", finalResult);
898
899 notifyListener(
900 MEDIA_ERROR, MEDIA_ERROR_UNKNOWN, finalResult);
901 }
Andreas Huberf9334412010-12-15 15:17:42 -0800902
903 if ((mAudioEOS || mAudioDecoder == NULL)
904 && (mVideoEOS || mVideoDecoder == NULL)) {
905 notifyListener(MEDIA_PLAYBACK_COMPLETE, 0, 0);
906 }
Andreas Huber43c3e6c2011-01-05 12:17:08 -0800907 } else if (what == Renderer::kWhatPosition) {
908 int64_t positionUs;
909 CHECK(msg->findInt64("positionUs", &positionUs));
Wei Jiaac428aa2014-09-02 19:01:34 -0700910 mCurrentPositionUs = positionUs;
Andreas Huber43c3e6c2011-01-05 12:17:08 -0800911
Andreas Huber3fe62152011-09-16 15:09:22 -0700912 CHECK(msg->findInt64("videoLateByUs", &mVideoLateByUs));
913
Andreas Huber43c3e6c2011-01-05 12:17:08 -0800914 if (mDriver != NULL) {
915 sp<NuPlayerDriver> driver = mDriver.promote();
916 if (driver != NULL) {
917 driver->notifyPosition(positionUs);
Andreas Huber3fe62152011-09-16 15:09:22 -0700918
919 driver->notifyFrameStats(
920 mNumFramesTotal, mNumFramesDropped);
Andreas Huber43c3e6c2011-01-05 12:17:08 -0800921 }
922 }
Andreas Huber3fe62152011-09-16 15:09:22 -0700923 } else if (what == Renderer::kWhatFlushComplete) {
Andreas Huberf9334412010-12-15 15:17:42 -0800924 int32_t audio;
925 CHECK(msg->findInt32("audio", &audio));
926
Steve Block3856b092011-10-20 11:56:00 +0100927 ALOGV("renderer %s flush completed.", audio ? "audio" : "video");
James Dongf57b4ea2012-07-20 13:38:36 -0700928 } else if (what == Renderer::kWhatVideoRenderingStart) {
929 notifyListener(MEDIA_INFO, MEDIA_INFO_RENDERING_START, 0);
Lajos Molnarcbaffcf2013-08-14 18:30:38 -0700930 } else if (what == Renderer::kWhatMediaRenderingStart) {
931 ALOGV("media rendering started");
932 notifyListener(MEDIA_STARTED, 0, 0);
Wei Jia3a2956d2014-07-22 16:01:33 -0700933 } else if (what == Renderer::kWhatAudioOffloadTearDown) {
934 ALOGV("Tear down audio offload, fall back to s/w path");
935 int64_t positionUs;
936 CHECK(msg->findInt64("positionUs", &positionUs));
Andy Hung282a7e32014-08-14 15:56:34 -0700937 closeAudioSink();
Wei Jia3a2956d2014-07-22 16:01:33 -0700938 mAudioDecoder.clear();
Wei Jia57568df2014-09-22 10:16:29 -0700939 ++mAudioDecoderGeneration;
Wei Jia3a2956d2014-07-22 16:01:33 -0700940 mRenderer->flush(true /* audio */);
941 if (mVideoDecoder != NULL) {
942 mRenderer->flush(false /* audio */);
943 }
944 mRenderer->signalDisableOffloadAudio();
945 mOffloadAudio = false;
946
Wei Jiae427abf2014-09-22 15:21:11 -0700947 performSeek(positionUs, false /* needNotify */);
Wei Jia3a2956d2014-07-22 16:01:33 -0700948 instantiateDecoder(true /* audio */, &mAudioDecoder);
Andreas Huberf9334412010-12-15 15:17:42 -0800949 }
950 break;
951 }
952
953 case kWhatMoreDataQueued:
954 {
955 break;
956 }
957
Andreas Huber1aef2112011-01-04 14:01:29 -0800958 case kWhatReset:
959 {
Steve Block3856b092011-10-20 11:56:00 +0100960 ALOGV("kWhatReset");
Andreas Huber1aef2112011-01-04 14:01:29 -0800961
Andreas Hubera1f8ab02012-11-30 10:53:22 -0800962 mDeferredActions.push_back(
Andreas Huber14f76722013-01-15 09:04:18 -0800963 new ShutdownDecoderAction(
964 true /* audio */, true /* video */));
Andreas Huberb7c8e912012-11-27 15:02:53 -0800965
Andreas Hubera1f8ab02012-11-30 10:53:22 -0800966 mDeferredActions.push_back(
967 new SimpleAction(&NuPlayer::performReset));
Andreas Huberb58ce9f2011-11-28 16:27:35 -0800968
Andreas Hubera1f8ab02012-11-30 10:53:22 -0800969 processDeferredActions();
Andreas Huber1aef2112011-01-04 14:01:29 -0800970 break;
971 }
972
Andreas Huber43c3e6c2011-01-05 12:17:08 -0800973 case kWhatSeek:
974 {
975 int64_t seekTimeUs;
Wei Jiae427abf2014-09-22 15:21:11 -0700976 int32_t needNotify;
Andreas Huber43c3e6c2011-01-05 12:17:08 -0800977 CHECK(msg->findInt64("seekTimeUs", &seekTimeUs));
Wei Jiae427abf2014-09-22 15:21:11 -0700978 CHECK(msg->findInt32("needNotify", &needNotify));
Andreas Huber43c3e6c2011-01-05 12:17:08 -0800979
Wei Jiae427abf2014-09-22 15:21:11 -0700980 ALOGV("kWhatSeek seekTimeUs=%lld us, needNotify=%d",
981 seekTimeUs, needNotify);
Andreas Huber43c3e6c2011-01-05 12:17:08 -0800982
Andreas Hubera1f8ab02012-11-30 10:53:22 -0800983 mDeferredActions.push_back(
984 new SimpleAction(&NuPlayer::performDecoderFlush));
Andreas Huber43c3e6c2011-01-05 12:17:08 -0800985
Wei Jiae427abf2014-09-22 15:21:11 -0700986 mDeferredActions.push_back(
987 new SeekAction(seekTimeUs, needNotify));
Andreas Huber43c3e6c2011-01-05 12:17:08 -0800988
Andreas Hubera1f8ab02012-11-30 10:53:22 -0800989 processDeferredActions();
Andreas Huber43c3e6c2011-01-05 12:17:08 -0800990 break;
991 }
992
Andreas Huberb4082222011-01-20 15:23:04 -0800993 case kWhatPause:
994 {
Wei Jia7e9f7f72014-09-23 10:55:35 -0700995 if (mSource != NULL) {
996 mSource->pause();
997 } else {
998 ALOGW("pause called when source is gone or not set");
999 }
1000 if (mRenderer != NULL) {
1001 mRenderer->pause();
1002 } else {
1003 ALOGW("pause called when renderer is gone or not set");
1004 }
Andreas Huberb4082222011-01-20 15:23:04 -08001005 break;
1006 }
1007
1008 case kWhatResume:
1009 {
Wei Jia7e9f7f72014-09-23 10:55:35 -07001010 if (mSource != NULL) {
1011 mSource->resume();
1012 } else {
1013 ALOGW("resume called when source is gone or not set");
1014 }
1015 if (mRenderer != NULL) {
1016 mRenderer->resume();
1017 } else {
1018 ALOGW("resume called when renderer is gone or not set");
1019 }
Andreas Huberb4082222011-01-20 15:23:04 -08001020 break;
1021 }
1022
Andreas Huberb5f25f02013-02-05 10:14:26 -08001023 case kWhatSourceNotify:
1024 {
Andreas Huber9575c962013-02-05 13:59:56 -08001025 onSourceNotify(msg);
Andreas Huberb5f25f02013-02-05 10:14:26 -08001026 break;
1027 }
1028
Chong Zhanga7fa1d92014-06-11 14:49:23 -07001029 case kWhatClosedCaptionNotify:
1030 {
1031 onClosedCaptionNotify(msg);
1032 break;
1033 }
1034
Andreas Huberf9334412010-12-15 15:17:42 -08001035 default:
1036 TRESPASS();
1037 break;
1038 }
1039}
1040
Andreas Huber3831a062010-12-21 10:22:33 -08001041void NuPlayer::finishFlushIfPossible() {
Wei Jia53904f32014-07-29 10:22:53 -07001042 if (mFlushingAudio != NONE && mFlushingAudio != FLUSHED
1043 && mFlushingAudio != SHUT_DOWN) {
Andreas Huber3831a062010-12-21 10:22:33 -08001044 return;
1045 }
1046
Wei Jia53904f32014-07-29 10:22:53 -07001047 if (mFlushingVideo != NONE && mFlushingVideo != FLUSHED
1048 && mFlushingVideo != SHUT_DOWN) {
Andreas Huber3831a062010-12-21 10:22:33 -08001049 return;
1050 }
1051
Steve Block3856b092011-10-20 11:56:00 +01001052 ALOGV("both audio and video are flushed now.");
Andreas Huber3831a062010-12-21 10:22:33 -08001053
Phil Burk9f526492014-09-03 15:04:12 -07001054 mPendingAudioAccessUnit.clear();
Phil Burkc5cc2e22014-09-09 20:08:39 -07001055 mAggregateBuffer.clear();
Phil Burk9f526492014-09-03 15:04:12 -07001056
Andreas Huber6e3d3112011-11-28 12:36:11 -08001057 if (mTimeDiscontinuityPending) {
1058 mRenderer->signalTimeDiscontinuity();
1059 mTimeDiscontinuityPending = false;
1060 }
Andreas Huber3831a062010-12-21 10:22:33 -08001061
Wei Jia53904f32014-07-29 10:22:53 -07001062 if (mAudioDecoder != NULL && mFlushingAudio == FLUSHED) {
Andreas Huber3831a062010-12-21 10:22:33 -08001063 mAudioDecoder->signalResume();
1064 }
1065
Wei Jia53904f32014-07-29 10:22:53 -07001066 if (mVideoDecoder != NULL && mFlushingVideo == FLUSHED) {
Andreas Huber3831a062010-12-21 10:22:33 -08001067 mVideoDecoder->signalResume();
1068 }
1069
1070 mFlushingAudio = NONE;
1071 mFlushingVideo = NONE;
Andreas Huber3831a062010-12-21 10:22:33 -08001072
Andreas Hubera1f8ab02012-11-30 10:53:22 -08001073 processDeferredActions();
Andreas Huber1aef2112011-01-04 14:01:29 -08001074}
1075
1076void NuPlayer::postScanSources() {
1077 if (mScanSourcesPending) {
1078 return;
1079 }
1080
1081 sp<AMessage> msg = new AMessage(kWhatScanSources, id());
1082 msg->setInt32("generation", mScanSourcesGeneration);
1083 msg->post();
1084
1085 mScanSourcesPending = true;
1086}
1087
Andy Hung282a7e32014-08-14 15:56:34 -07001088void NuPlayer::openAudioSink(const sp<AMessage> &format, bool offloadOnly) {
1089 ALOGV("openAudioSink: offloadOnly(%d) mOffloadAudio(%d)",
1090 offloadOnly, mOffloadAudio);
1091 bool audioSinkChanged = false;
1092
1093 int32_t numChannels;
1094 CHECK(format->findInt32("channel-count", &numChannels));
1095
1096 int32_t channelMask;
1097 if (!format->findInt32("channel-mask", &channelMask)) {
1098 // signal to the AudioSink to derive the mask from count.
1099 channelMask = CHANNEL_MASK_USE_CHANNEL_ORDER;
1100 }
1101
1102 int32_t sampleRate;
1103 CHECK(format->findInt32("sample-rate", &sampleRate));
1104
1105 uint32_t flags;
1106 int64_t durationUs;
1107 // FIXME: we should handle the case where the video decoder
1108 // is created after we receive the format change indication.
1109 // Current code will just make that we select deep buffer
1110 // with video which should not be a problem as it should
1111 // not prevent from keeping A/V sync.
1112 if (mVideoDecoder == NULL &&
1113 mSource->getDuration(&durationUs) == OK &&
1114 durationUs
1115 > AUDIO_SINK_MIN_DEEP_BUFFER_DURATION_US) {
1116 flags = AUDIO_OUTPUT_FLAG_DEEP_BUFFER;
1117 } else {
1118 flags = AUDIO_OUTPUT_FLAG_NONE;
1119 }
1120
1121 if (mOffloadAudio) {
1122 audio_format_t audioFormat = AUDIO_FORMAT_PCM_16_BIT;
1123 AString mime;
1124 CHECK(format->findString("mime", &mime));
1125 status_t err = mapMimeToAudioFormat(audioFormat, mime.c_str());
1126
1127 if (err != OK) {
1128 ALOGE("Couldn't map mime \"%s\" to a valid "
1129 "audio_format", mime.c_str());
1130 mOffloadAudio = false;
1131 } else {
1132 ALOGV("Mime \"%s\" mapped to audio_format 0x%x",
1133 mime.c_str(), audioFormat);
1134
1135 int avgBitRate = -1;
1136 format->findInt32("bit-rate", &avgBitRate);
1137
1138 int32_t aacProfile = -1;
1139 if (audioFormat == AUDIO_FORMAT_AAC
1140 && format->findInt32("aac-profile", &aacProfile)) {
1141 // Redefine AAC format as per aac profile
1142 mapAACProfileToAudioFormat(
1143 audioFormat,
1144 aacProfile);
1145 }
1146
1147 audio_offload_info_t offloadInfo = AUDIO_INFO_INITIALIZER;
1148 offloadInfo.duration_us = -1;
1149 format->findInt64(
1150 "durationUs", &offloadInfo.duration_us);
1151 offloadInfo.sample_rate = sampleRate;
1152 offloadInfo.channel_mask = channelMask;
1153 offloadInfo.format = audioFormat;
1154 offloadInfo.stream_type = AUDIO_STREAM_MUSIC;
1155 offloadInfo.bit_rate = avgBitRate;
1156 offloadInfo.has_video = (mVideoDecoder != NULL);
1157 offloadInfo.is_streaming = true;
1158
1159 if (memcmp(&mCurrentOffloadInfo, &offloadInfo, sizeof(offloadInfo)) == 0) {
1160 ALOGV("openAudioSink: no change in offload mode");
1161 return; // no change from previous configuration, everything ok.
1162 }
1163 ALOGV("openAudioSink: try to open AudioSink in offload mode");
1164 flags |= AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD;
Ronghua Wu1ffb5382014-08-18 15:57:03 -07001165 flags &= ~AUDIO_OUTPUT_FLAG_DEEP_BUFFER;
Andy Hung282a7e32014-08-14 15:56:34 -07001166 audioSinkChanged = true;
1167 mAudioSink->close();
1168 err = mAudioSink->open(
1169 sampleRate,
1170 numChannels,
1171 (audio_channel_mask_t)channelMask,
1172 audioFormat,
1173 8 /* bufferCount */,
1174 &NuPlayer::Renderer::AudioSinkCallback,
1175 mRenderer.get(),
1176 (audio_output_flags_t)flags,
1177 &offloadInfo);
1178
1179 if (err == OK) {
1180 // If the playback is offloaded to h/w, we pass
1181 // the HAL some metadata information.
1182 // We don't want to do this for PCM because it
1183 // will be going through the AudioFlinger mixer
1184 // before reaching the hardware.
1185 sp<MetaData> audioMeta =
1186 mSource->getFormatMeta(true /* audio */);
1187 sendMetaDataToHal(mAudioSink, audioMeta);
1188 mCurrentOffloadInfo = offloadInfo;
1189 err = mAudioSink->start();
1190 ALOGV_IF(err == OK, "openAudioSink: offload succeeded");
1191 }
1192 if (err != OK) {
1193 // Clean up, fall back to non offload mode.
1194 mAudioSink->close();
1195 mRenderer->signalDisableOffloadAudio();
1196 mOffloadAudio = false;
1197 mCurrentOffloadInfo = AUDIO_INFO_INITIALIZER;
1198 ALOGV("openAudioSink: offload failed");
1199 }
1200 }
1201 }
1202 if (!offloadOnly && !mOffloadAudio) {
1203 flags &= ~AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD;
1204 ALOGV("openAudioSink: open AudioSink in NON-offload mode");
1205
1206 audioSinkChanged = true;
1207 mAudioSink->close();
1208 mCurrentOffloadInfo = AUDIO_INFO_INITIALIZER;
1209 CHECK_EQ(mAudioSink->open(
1210 sampleRate,
1211 numChannels,
1212 (audio_channel_mask_t)channelMask,
1213 AUDIO_FORMAT_PCM_16_BIT,
1214 8 /* bufferCount */,
1215 NULL,
1216 NULL,
1217 (audio_output_flags_t)flags),
1218 (status_t)OK);
1219 mAudioSink->start();
1220 }
1221 if (audioSinkChanged) {
1222 mRenderer->signalAudioSinkChanged();
1223 }
1224}
1225
1226void NuPlayer::closeAudioSink() {
1227 mAudioSink->close();
1228 mCurrentOffloadInfo = AUDIO_INFO_INITIALIZER;
1229}
1230
Andreas Huber5bc087c2010-12-23 10:27:40 -08001231status_t NuPlayer::instantiateDecoder(bool audio, sp<Decoder> *decoder) {
Andreas Huberf9334412010-12-15 15:17:42 -08001232 if (*decoder != NULL) {
1233 return OK;
1234 }
1235
Andreas Huber84066782011-08-16 09:34:26 -07001236 sp<AMessage> format = mSource->getFormat(audio);
Andreas Huberf9334412010-12-15 15:17:42 -08001237
Andreas Huber84066782011-08-16 09:34:26 -07001238 if (format == NULL) {
Andreas Huberf9334412010-12-15 15:17:42 -08001239 return -EWOULDBLOCK;
1240 }
1241
Andreas Huber3fe62152011-09-16 15:09:22 -07001242 if (!audio) {
Andreas Huber84066782011-08-16 09:34:26 -07001243 AString mime;
1244 CHECK(format->findString("mime", &mime));
1245 mVideoIsAVC = !strcasecmp(MEDIA_MIMETYPE_VIDEO_AVC, mime.c_str());
Chong Zhanga7fa1d92014-06-11 14:49:23 -07001246
1247 sp<AMessage> ccNotify = new AMessage(kWhatClosedCaptionNotify, id());
1248 mCCDecoder = new CCDecoder(ccNotify);
Lajos Molnar09524832014-07-17 14:29:51 -07001249
1250 if (mSourceFlags & Source::FLAG_SECURE) {
1251 format->setInt32("secure", true);
1252 }
Andreas Huber3fe62152011-09-16 15:09:22 -07001253 }
1254
Wei Jiabc2fb722014-07-08 16:37:57 -07001255 if (audio) {
Wei Jia88703c32014-08-06 11:24:07 -07001256 sp<AMessage> notify = new AMessage(kWhatAudioNotify, id());
1257 ++mAudioDecoderGeneration;
1258 notify->setInt32("generation", mAudioDecoderGeneration);
1259
Wei Jiabc2fb722014-07-08 16:37:57 -07001260 if (mOffloadAudio) {
1261 *decoder = new DecoderPassThrough(notify);
1262 } else {
1263 *decoder = new Decoder(notify);
1264 }
1265 } else {
Wei Jia88703c32014-08-06 11:24:07 -07001266 sp<AMessage> notify = new AMessage(kWhatVideoNotify, id());
1267 ++mVideoDecoderGeneration;
1268 notify->setInt32("generation", mVideoDecoderGeneration);
1269
Wei Jiabc2fb722014-07-08 16:37:57 -07001270 *decoder = new Decoder(notify, mNativeWindow);
1271 }
Lajos Molnar1cd13982014-01-17 15:12:51 -08001272 (*decoder)->init();
Andreas Huber84066782011-08-16 09:34:26 -07001273 (*decoder)->configure(format);
Andreas Huberf9334412010-12-15 15:17:42 -08001274
Lajos Molnar09524832014-07-17 14:29:51 -07001275 // allocate buffers to decrypt widevine source buffers
1276 if (!audio && (mSourceFlags & Source::FLAG_SECURE)) {
1277 Vector<sp<ABuffer> > inputBufs;
1278 CHECK_EQ((*decoder)->getInputBuffers(&inputBufs), (status_t)OK);
1279
1280 Vector<MediaBuffer *> mediaBufs;
1281 for (size_t i = 0; i < inputBufs.size(); i++) {
1282 const sp<ABuffer> &buffer = inputBufs[i];
1283 MediaBuffer *mbuf = new MediaBuffer(buffer->data(), buffer->size());
1284 mediaBufs.push(mbuf);
1285 }
1286
1287 status_t err = mSource->setBuffers(audio, mediaBufs);
1288 if (err != OK) {
1289 for (size_t i = 0; i < mediaBufs.size(); ++i) {
1290 mediaBufs[i]->release();
1291 }
1292 mediaBufs.clear();
1293 ALOGE("Secure source didn't support secure mediaBufs.");
1294 return err;
1295 }
1296 }
Andreas Huberf9334412010-12-15 15:17:42 -08001297 return OK;
1298}
1299
1300status_t NuPlayer::feedDecoderInputData(bool audio, const sp<AMessage> &msg) {
1301 sp<AMessage> reply;
1302 CHECK(msg->findMessage("reply", &reply));
1303
Wei Jia53904f32014-07-29 10:22:53 -07001304 if ((audio && mFlushingAudio != NONE)
Wei Jiaf702d042014-09-09 12:08:47 -07001305 || (!audio && mFlushingVideo != NONE)
1306 || mSource == NULL) {
Wei Jiab189a5b2014-08-07 06:11:39 +00001307 reply->setInt32("err", INFO_DISCONTINUITY);
1308 reply->post();
1309 return OK;
Andreas Huberf9334412010-12-15 15:17:42 -08001310 }
1311
1312 sp<ABuffer> accessUnit;
Andreas Huberf9334412010-12-15 15:17:42 -08001313
Phil Burk9f526492014-09-03 15:04:12 -07001314 // Aggregate smaller buffers into a larger buffer.
1315 // The goal is to reduce power consumption.
Phil Burk33b51b02014-09-17 16:03:47 -07001316 // Note this will not work if the decoder requires one frame per buffer.
1317 bool doBufferAggregation = (audio && mOffloadAudio);
Phil Burk9f526492014-09-03 15:04:12 -07001318 bool needMoreData = false;
Phil Burk9f526492014-09-03 15:04:12 -07001319
Andreas Huber3fe62152011-09-16 15:09:22 -07001320 bool dropAccessUnit;
1321 do {
Phil Burk9f526492014-09-03 15:04:12 -07001322 status_t err;
1323 // Did we save an accessUnit earlier because of a discontinuity?
1324 if (audio && (mPendingAudioAccessUnit != NULL)) {
1325 accessUnit = mPendingAudioAccessUnit;
1326 mPendingAudioAccessUnit.clear();
1327 err = mPendingAudioErr;
1328 ALOGV("feedDecoderInputData() use mPendingAudioAccessUnit");
1329 } else {
1330 err = mSource->dequeueAccessUnit(audio, &accessUnit);
1331 }
Andreas Huber5bc087c2010-12-23 10:27:40 -08001332
Andreas Huber3fe62152011-09-16 15:09:22 -07001333 if (err == -EWOULDBLOCK) {
Phil Burkc5cc2e22014-09-09 20:08:39 -07001334 return err;
Andreas Huber3fe62152011-09-16 15:09:22 -07001335 } else if (err != OK) {
1336 if (err == INFO_DISCONTINUITY) {
Phil Burk33b51b02014-09-17 16:03:47 -07001337 if (doBufferAggregation && (mAggregateBuffer != NULL)) {
Phil Burk9f526492014-09-03 15:04:12 -07001338 // We already have some data so save this for later.
1339 mPendingAudioErr = err;
1340 mPendingAudioAccessUnit = accessUnit;
1341 accessUnit.clear();
1342 ALOGD("feedDecoderInputData() save discontinuity for later");
1343 break;
1344 }
Andreas Huber3fe62152011-09-16 15:09:22 -07001345 int32_t type;
1346 CHECK(accessUnit->meta()->findInt32("discontinuity", &type));
Andreas Huber53df1a42010-12-22 10:03:04 -08001347
Andreas Huber3fe62152011-09-16 15:09:22 -07001348 bool formatChange =
Andreas Huber6e3d3112011-11-28 12:36:11 -08001349 (audio &&
1350 (type & ATSParser::DISCONTINUITY_AUDIO_FORMAT))
1351 || (!audio &&
1352 (type & ATSParser::DISCONTINUITY_VIDEO_FORMAT));
Andreas Huber53df1a42010-12-22 10:03:04 -08001353
Andreas Huber6e3d3112011-11-28 12:36:11 -08001354 bool timeChange = (type & ATSParser::DISCONTINUITY_TIME) != 0;
1355
Steve Blockdf64d152012-01-04 20:05:49 +00001356 ALOGI("%s discontinuity (formatChange=%d, time=%d)",
Andreas Huber6e3d3112011-11-28 12:36:11 -08001357 audio ? "audio" : "video", formatChange, timeChange);
Andreas Huber32f3cef2011-03-02 15:34:46 -08001358
Andreas Huber3fe62152011-09-16 15:09:22 -07001359 if (audio) {
1360 mSkipRenderingAudioUntilMediaTimeUs = -1;
1361 } else {
1362 mSkipRenderingVideoUntilMediaTimeUs = -1;
1363 }
Andreas Huber32f3cef2011-03-02 15:34:46 -08001364
Andreas Huber6e3d3112011-11-28 12:36:11 -08001365 if (timeChange) {
1366 sp<AMessage> extra;
1367 if (accessUnit->meta()->findMessage("extra", &extra)
1368 && extra != NULL) {
1369 int64_t resumeAtMediaTimeUs;
1370 if (extra->findInt64(
1371 "resume-at-mediatimeUs", &resumeAtMediaTimeUs)) {
Steve Blockdf64d152012-01-04 20:05:49 +00001372 ALOGI("suppressing rendering of %s until %lld us",
Andreas Huber6e3d3112011-11-28 12:36:11 -08001373 audio ? "audio" : "video", resumeAtMediaTimeUs);
Andreas Huber3fe62152011-09-16 15:09:22 -07001374
Andreas Huber6e3d3112011-11-28 12:36:11 -08001375 if (audio) {
1376 mSkipRenderingAudioUntilMediaTimeUs =
1377 resumeAtMediaTimeUs;
1378 } else {
1379 mSkipRenderingVideoUntilMediaTimeUs =
1380 resumeAtMediaTimeUs;
1381 }
Andreas Huber3fe62152011-09-16 15:09:22 -07001382 }
Andreas Huber32f3cef2011-03-02 15:34:46 -08001383 }
1384 }
Andreas Huber3fe62152011-09-16 15:09:22 -07001385
Andreas Huber6e3d3112011-11-28 12:36:11 -08001386 mTimeDiscontinuityPending =
1387 mTimeDiscontinuityPending || timeChange;
1388
Lajos Molnar87603c02014-08-20 19:25:30 -07001389 bool seamlessFormatChange = false;
1390 sp<AMessage> newFormat = mSource->getFormat(audio);
1391 if (formatChange) {
1392 seamlessFormatChange =
1393 getDecoder(audio)->supportsSeamlessFormatChange(newFormat);
1394 // treat seamless format change separately
1395 formatChange = !seamlessFormatChange;
1396 }
1397 bool shutdownOrFlush = formatChange || timeChange;
1398
1399 // We want to queue up scan-sources only once per discontinuity.
1400 // We control this by doing it only if neither audio nor video are
1401 // flushing or shutting down. (After handling 1st discontinuity, one
1402 // of the flushing states will not be NONE.)
1403 // No need to scan sources if this discontinuity does not result
1404 // in a flush or shutdown, as the flushing state will stay NONE.
1405 if (mFlushingAudio == NONE && mFlushingVideo == NONE &&
1406 shutdownOrFlush) {
Robert Shiha2981012014-07-30 17:41:24 -07001407 // And we'll resume scanning sources once we're done
1408 // flushing.
1409 mDeferredActions.push_front(
1410 new SimpleAction(
1411 &NuPlayer::performScanSources));
1412 }
1413
Lajos Molnar87603c02014-08-20 19:25:30 -07001414 if (formatChange /* not seamless */) {
1415 // must change decoder
1416 flushDecoder(audio, /* needShutdown = */ true);
1417 } else if (timeChange) {
1418 // need to flush
1419 flushDecoder(audio, /* needShutdown = */ false, newFormat);
1420 err = OK;
1421 } else if (seamlessFormatChange) {
1422 // reuse existing decoder and don't flush
1423 updateDecoderFormatWithoutFlush(audio, newFormat);
1424 err = OK;
Andreas Huber6e3d3112011-11-28 12:36:11 -08001425 } else {
1426 // This stream is unaffected by the discontinuity
Andreas Huber6e3d3112011-11-28 12:36:11 -08001427 return -EWOULDBLOCK;
1428 }
Andreas Huber32f3cef2011-03-02 15:34:46 -08001429 }
1430
Andreas Huber3fe62152011-09-16 15:09:22 -07001431 reply->setInt32("err", err);
1432 reply->post();
1433 return OK;
Andreas Huberf9334412010-12-15 15:17:42 -08001434 }
1435
Andreas Huber3fe62152011-09-16 15:09:22 -07001436 if (!audio) {
1437 ++mNumFramesTotal;
1438 }
1439
1440 dropAccessUnit = false;
1441 if (!audio
Lajos Molnar09524832014-07-17 14:29:51 -07001442 && !(mSourceFlags & Source::FLAG_SECURE)
Andreas Huber3fe62152011-09-16 15:09:22 -07001443 && mVideoLateByUs > 100000ll
1444 && mVideoIsAVC
1445 && !IsAVCReferenceFrame(accessUnit)) {
1446 dropAccessUnit = true;
1447 ++mNumFramesDropped;
1448 }
Phil Burk9f526492014-09-03 15:04:12 -07001449
1450 size_t smallSize = accessUnit->size();
1451 needMoreData = false;
Phil Burkc5cc2e22014-09-09 20:08:39 -07001452 if (doBufferAggregation && (mAggregateBuffer == NULL)
Phil Burk9f526492014-09-03 15:04:12 -07001453 // Don't bother if only room for a few small buffers.
Phil Burkc5cc2e22014-09-09 20:08:39 -07001454 && (smallSize < (kAggregateBufferSizeBytes / 3))) {
Phil Burk9f526492014-09-03 15:04:12 -07001455 // Create a larger buffer for combining smaller buffers from the extractor.
Phil Burkc5cc2e22014-09-09 20:08:39 -07001456 mAggregateBuffer = new ABuffer(kAggregateBufferSizeBytes);
1457 mAggregateBuffer->setRange(0, 0); // start empty
Phil Burk9f526492014-09-03 15:04:12 -07001458 }
1459
Phil Burk33b51b02014-09-17 16:03:47 -07001460 if (doBufferAggregation && (mAggregateBuffer != NULL)) {
Phil Burk9f526492014-09-03 15:04:12 -07001461 int64_t timeUs;
Phil Burkc5cc2e22014-09-09 20:08:39 -07001462 int64_t dummy;
Phil Burk9f526492014-09-03 15:04:12 -07001463 bool smallTimestampValid = accessUnit->meta()->findInt64("timeUs", &timeUs);
Phil Burkc5cc2e22014-09-09 20:08:39 -07001464 bool bigTimestampValid = mAggregateBuffer->meta()->findInt64("timeUs", &dummy);
Phil Burk9f526492014-09-03 15:04:12 -07001465 // Will the smaller buffer fit?
Phil Burkc5cc2e22014-09-09 20:08:39 -07001466 size_t bigSize = mAggregateBuffer->size();
1467 size_t roomLeft = mAggregateBuffer->capacity() - bigSize;
Phil Burk9f526492014-09-03 15:04:12 -07001468 // Should we save this small buffer for the next big buffer?
1469 // If the first small buffer did not have a timestamp then save
1470 // any buffer that does have a timestamp until the next big buffer.
1471 if ((smallSize > roomLeft)
Phil Burkc5cc2e22014-09-09 20:08:39 -07001472 || (!bigTimestampValid && (bigSize > 0) && smallTimestampValid)) {
Phil Burk9f526492014-09-03 15:04:12 -07001473 mPendingAudioErr = err;
1474 mPendingAudioAccessUnit = accessUnit;
1475 accessUnit.clear();
1476 } else {
Phil Burkc5cc2e22014-09-09 20:08:39 -07001477 // Grab time from first small buffer if available.
1478 if ((bigSize == 0) && smallTimestampValid) {
1479 mAggregateBuffer->meta()->setInt64("timeUs", timeUs);
1480 }
Phil Burk9f526492014-09-03 15:04:12 -07001481 // Append small buffer to the bigger buffer.
Phil Burkc5cc2e22014-09-09 20:08:39 -07001482 memcpy(mAggregateBuffer->base() + bigSize, accessUnit->data(), smallSize);
Phil Burk9f526492014-09-03 15:04:12 -07001483 bigSize += smallSize;
Phil Burkc5cc2e22014-09-09 20:08:39 -07001484 mAggregateBuffer->setRange(0, bigSize);
Phil Burk9f526492014-09-03 15:04:12 -07001485
Phil Burkc5cc2e22014-09-09 20:08:39 -07001486 // Keep looping until we run out of room in the mAggregateBuffer.
Phil Burk9f526492014-09-03 15:04:12 -07001487 needMoreData = true;
1488
Phil Burkc5cc2e22014-09-09 20:08:39 -07001489 ALOGV("feedDecoderInputData() smallSize = %zu, bigSize = %zu, capacity = %zu",
1490 smallSize, bigSize, mAggregateBuffer->capacity());
Phil Burk9f526492014-09-03 15:04:12 -07001491 }
1492 }
1493 } while (dropAccessUnit || needMoreData);
Andreas Huberf9334412010-12-15 15:17:42 -08001494
Steve Block3856b092011-10-20 11:56:00 +01001495 // ALOGV("returned a valid buffer of %s data", audio ? "audio" : "video");
Andreas Huberf9334412010-12-15 15:17:42 -08001496
1497#if 0
1498 int64_t mediaTimeUs;
1499 CHECK(accessUnit->meta()->findInt64("timeUs", &mediaTimeUs));
Steve Block3856b092011-10-20 11:56:00 +01001500 ALOGV("feeding %s input buffer at media time %.2f secs",
Andreas Huberf9334412010-12-15 15:17:42 -08001501 audio ? "audio" : "video",
1502 mediaTimeUs / 1E6);
1503#endif
1504
Chong Zhanga7fa1d92014-06-11 14:49:23 -07001505 if (!audio) {
1506 mCCDecoder->decode(accessUnit);
1507 }
1508
Phil Burk33b51b02014-09-17 16:03:47 -07001509 if (doBufferAggregation && (mAggregateBuffer != NULL)) {
Phil Burkc5cc2e22014-09-09 20:08:39 -07001510 ALOGV("feedDecoderInputData() reply with aggregated buffer, %zu",
1511 mAggregateBuffer->size());
1512 reply->setBuffer("buffer", mAggregateBuffer);
1513 mAggregateBuffer.clear();
Phil Burk9f526492014-09-03 15:04:12 -07001514 } else {
1515 reply->setBuffer("buffer", accessUnit);
1516 }
1517
Andreas Huberf9334412010-12-15 15:17:42 -08001518 reply->post();
1519
1520 return OK;
1521}
1522
1523void NuPlayer::renderBuffer(bool audio, const sp<AMessage> &msg) {
Steve Block3856b092011-10-20 11:56:00 +01001524 // ALOGV("renderBuffer %s", audio ? "audio" : "video");
Andreas Huberf9334412010-12-15 15:17:42 -08001525
1526 sp<AMessage> reply;
1527 CHECK(msg->findMessage("reply", &reply));
1528
Wei Jia53904f32014-07-29 10:22:53 -07001529 if ((audio && mFlushingAudio != NONE)
1530 || (!audio && mFlushingVideo != NONE)) {
Andreas Huber18ac5402011-08-31 15:04:25 -07001531 // We're currently attempting to flush the decoder, in order
1532 // to complete this, the decoder wants all its buffers back,
1533 // so we don't want any output buffers it sent us (from before
1534 // we initiated the flush) to be stuck in the renderer's queue.
1535
Steve Block3856b092011-10-20 11:56:00 +01001536 ALOGV("we're still flushing the %s decoder, sending its output buffer"
Andreas Huber18ac5402011-08-31 15:04:25 -07001537 " right back.", audio ? "audio" : "video");
1538
1539 reply->post();
1540 return;
1541 }
1542
Andreas Huber2d8bedd2012-02-21 14:38:23 -08001543 sp<ABuffer> buffer;
1544 CHECK(msg->findBuffer("buffer", &buffer));
Andreas Huberf9334412010-12-15 15:17:42 -08001545
Chong Zhanga7fa1d92014-06-11 14:49:23 -07001546 int64_t mediaTimeUs;
1547 CHECK(buffer->meta()->findInt64("timeUs", &mediaTimeUs));
1548
Andreas Huber32f3cef2011-03-02 15:34:46 -08001549 int64_t &skipUntilMediaTimeUs =
1550 audio
1551 ? mSkipRenderingAudioUntilMediaTimeUs
1552 : mSkipRenderingVideoUntilMediaTimeUs;
1553
1554 if (skipUntilMediaTimeUs >= 0) {
Andreas Huber32f3cef2011-03-02 15:34:46 -08001555
1556 if (mediaTimeUs < skipUntilMediaTimeUs) {
Steve Block3856b092011-10-20 11:56:00 +01001557 ALOGV("dropping %s buffer at time %lld as requested.",
Andreas Huber32f3cef2011-03-02 15:34:46 -08001558 audio ? "audio" : "video",
1559 mediaTimeUs);
1560
1561 reply->post();
1562 return;
1563 }
1564
1565 skipUntilMediaTimeUs = -1;
1566 }
1567
Chong Zhanga7fa1d92014-06-11 14:49:23 -07001568 if (!audio && mCCDecoder->isSelected()) {
1569 mCCDecoder->display(mediaTimeUs);
1570 }
1571
Andreas Huberf9334412010-12-15 15:17:42 -08001572 mRenderer->queueBuffer(audio, buffer, reply);
1573}
1574
Chong Zhangced1c2f2014-08-08 15:22:35 -07001575void NuPlayer::updateVideoSize(
1576 const sp<AMessage> &inputFormat,
1577 const sp<AMessage> &outputFormat) {
1578 if (inputFormat == NULL) {
1579 ALOGW("Unknown video size, reporting 0x0!");
1580 notifyListener(MEDIA_SET_VIDEO_SIZE, 0, 0);
1581 return;
1582 }
1583
1584 int32_t displayWidth, displayHeight;
1585 int32_t cropLeft, cropTop, cropRight, cropBottom;
1586
1587 if (outputFormat != NULL) {
1588 int32_t width, height;
1589 CHECK(outputFormat->findInt32("width", &width));
1590 CHECK(outputFormat->findInt32("height", &height));
1591
1592 int32_t cropLeft, cropTop, cropRight, cropBottom;
1593 CHECK(outputFormat->findRect(
1594 "crop",
1595 &cropLeft, &cropTop, &cropRight, &cropBottom));
1596
1597 displayWidth = cropRight - cropLeft + 1;
1598 displayHeight = cropBottom - cropTop + 1;
1599
1600 ALOGV("Video output format changed to %d x %d "
1601 "(crop: %d x %d @ (%d, %d))",
1602 width, height,
1603 displayWidth,
1604 displayHeight,
1605 cropLeft, cropTop);
1606 } else {
1607 CHECK(inputFormat->findInt32("width", &displayWidth));
1608 CHECK(inputFormat->findInt32("height", &displayHeight));
1609
1610 ALOGV("Video input format %d x %d", displayWidth, displayHeight);
1611 }
1612
1613 // Take into account sample aspect ratio if necessary:
1614 int32_t sarWidth, sarHeight;
1615 if (inputFormat->findInt32("sar-width", &sarWidth)
1616 && inputFormat->findInt32("sar-height", &sarHeight)) {
1617 ALOGV("Sample aspect ratio %d : %d", sarWidth, sarHeight);
1618
1619 displayWidth = (displayWidth * sarWidth) / sarHeight;
1620
1621 ALOGV("display dimensions %d x %d", displayWidth, displayHeight);
1622 }
1623
1624 int32_t rotationDegrees;
1625 if (!inputFormat->findInt32("rotation-degrees", &rotationDegrees)) {
1626 rotationDegrees = 0;
1627 }
1628
1629 if (rotationDegrees == 90 || rotationDegrees == 270) {
1630 int32_t tmp = displayWidth;
1631 displayWidth = displayHeight;
1632 displayHeight = tmp;
1633 }
1634
1635 notifyListener(
1636 MEDIA_SET_VIDEO_SIZE,
1637 displayWidth,
1638 displayHeight);
1639}
1640
Chong Zhangdcb89b32013-08-06 09:44:47 -07001641void NuPlayer::notifyListener(int msg, int ext1, int ext2, const Parcel *in) {
Andreas Huber43c3e6c2011-01-05 12:17:08 -08001642 if (mDriver == NULL) {
Andreas Huberf9334412010-12-15 15:17:42 -08001643 return;
1644 }
1645
Andreas Huber43c3e6c2011-01-05 12:17:08 -08001646 sp<NuPlayerDriver> driver = mDriver.promote();
Andreas Huberf9334412010-12-15 15:17:42 -08001647
Andreas Huber43c3e6c2011-01-05 12:17:08 -08001648 if (driver == NULL) {
Andreas Huberf9334412010-12-15 15:17:42 -08001649 return;
1650 }
1651
Chong Zhangdcb89b32013-08-06 09:44:47 -07001652 driver->notifyListener(msg, ext1, ext2, in);
Andreas Huberf9334412010-12-15 15:17:42 -08001653}
1654
Lajos Molnar87603c02014-08-20 19:25:30 -07001655void NuPlayer::flushDecoder(
1656 bool audio, bool needShutdown, const sp<AMessage> &newFormat) {
Andreas Huber14f76722013-01-15 09:04:18 -08001657 ALOGV("[%s] flushDecoder needShutdown=%d",
1658 audio ? "audio" : "video", needShutdown);
1659
Lajos Molnar87603c02014-08-20 19:25:30 -07001660 const sp<Decoder> &decoder = getDecoder(audio);
1661 if (decoder == NULL) {
Steve Blockdf64d152012-01-04 20:05:49 +00001662 ALOGI("flushDecoder %s without decoder present",
Andreas Huber6e3d3112011-11-28 12:36:11 -08001663 audio ? "audio" : "video");
Lajos Molnar87603c02014-08-20 19:25:30 -07001664 return;
Andreas Huber6e3d3112011-11-28 12:36:11 -08001665 }
1666
Andreas Huber1aef2112011-01-04 14:01:29 -08001667 // Make sure we don't continue to scan sources until we finish flushing.
1668 ++mScanSourcesGeneration;
Andreas Huber43c3e6c2011-01-05 12:17:08 -08001669 mScanSourcesPending = false;
Andreas Huber1aef2112011-01-04 14:01:29 -08001670
Lajos Molnar87603c02014-08-20 19:25:30 -07001671 decoder->signalFlush(newFormat);
Andreas Huber1aef2112011-01-04 14:01:29 -08001672 mRenderer->flush(audio);
1673
1674 FlushStatus newStatus =
1675 needShutdown ? FLUSHING_DECODER_SHUTDOWN : FLUSHING_DECODER;
1676
1677 if (audio) {
Wei Jia53904f32014-07-29 10:22:53 -07001678 ALOGE_IF(mFlushingAudio != NONE,
1679 "audio flushDecoder() is called in state %d", mFlushingAudio);
Andreas Huber1aef2112011-01-04 14:01:29 -08001680 mFlushingAudio = newStatus;
Andreas Huber1aef2112011-01-04 14:01:29 -08001681 } else {
Wei Jia53904f32014-07-29 10:22:53 -07001682 ALOGE_IF(mFlushingVideo != NONE,
1683 "video flushDecoder() is called in state %d", mFlushingVideo);
Andreas Huber1aef2112011-01-04 14:01:29 -08001684 mFlushingVideo = newStatus;
Chong Zhangb86e68f2014-08-01 13:46:53 -07001685
1686 if (mCCDecoder != NULL) {
1687 mCCDecoder->flush();
1688 }
Andreas Huber1aef2112011-01-04 14:01:29 -08001689 }
1690}
1691
Lajos Molnar87603c02014-08-20 19:25:30 -07001692void NuPlayer::updateDecoderFormatWithoutFlush(
1693 bool audio, const sp<AMessage> &format) {
1694 ALOGV("[%s] updateDecoderFormatWithoutFlush", audio ? "audio" : "video");
1695
1696 const sp<Decoder> &decoder = getDecoder(audio);
1697 if (decoder == NULL) {
1698 ALOGI("updateDecoderFormatWithoutFlush %s without decoder present",
1699 audio ? "audio" : "video");
1700 return;
1701 }
1702
1703 decoder->signalUpdateFormat(format);
1704}
1705
Chong Zhangced1c2f2014-08-08 15:22:35 -07001706void NuPlayer::queueDecoderShutdown(
1707 bool audio, bool video, const sp<AMessage> &reply) {
1708 ALOGI("queueDecoderShutdown audio=%d, video=%d", audio, video);
Andreas Huber84066782011-08-16 09:34:26 -07001709
Chong Zhangced1c2f2014-08-08 15:22:35 -07001710 mDeferredActions.push_back(
1711 new ShutdownDecoderAction(audio, video));
Andreas Huber84066782011-08-16 09:34:26 -07001712
Chong Zhangced1c2f2014-08-08 15:22:35 -07001713 mDeferredActions.push_back(
1714 new SimpleAction(&NuPlayer::performScanSources));
Andreas Huber84066782011-08-16 09:34:26 -07001715
Chong Zhangced1c2f2014-08-08 15:22:35 -07001716 mDeferredActions.push_back(new PostMessageAction(reply));
1717
1718 processDeferredActions();
Andreas Huber84066782011-08-16 09:34:26 -07001719}
1720
James Dong0d268a32012-08-31 12:18:27 -07001721status_t NuPlayer::setVideoScalingMode(int32_t mode) {
1722 mVideoScalingMode = mode;
Andreas Huber57a339c2012-12-03 11:18:00 -08001723 if (mNativeWindow != NULL) {
James Dong0d268a32012-08-31 12:18:27 -07001724 status_t ret = native_window_set_scaling_mode(
1725 mNativeWindow->getNativeWindow().get(), mVideoScalingMode);
1726 if (ret != OK) {
1727 ALOGE("Failed to set scaling mode (%d): %s",
1728 -ret, strerror(-ret));
1729 return ret;
1730 }
1731 }
1732 return OK;
1733}
1734
Chong Zhangdcb89b32013-08-06 09:44:47 -07001735status_t NuPlayer::getTrackInfo(Parcel* reply) const {
1736 sp<AMessage> msg = new AMessage(kWhatGetTrackInfo, id());
1737 msg->setPointer("reply", reply);
1738
1739 sp<AMessage> response;
1740 status_t err = msg->postAndAwaitResponse(&response);
1741 return err;
1742}
1743
Robert Shih7c4f0d72014-07-09 18:53:31 -07001744status_t NuPlayer::getSelectedTrack(int32_t type, Parcel* reply) const {
1745 sp<AMessage> msg = new AMessage(kWhatGetSelectedTrack, id());
1746 msg->setPointer("reply", reply);
1747 msg->setInt32("type", type);
1748
1749 sp<AMessage> response;
1750 status_t err = msg->postAndAwaitResponse(&response);
1751 if (err == OK && response != NULL) {
1752 CHECK(response->findInt32("err", &err));
1753 }
1754 return err;
1755}
1756
Chong Zhangdcb89b32013-08-06 09:44:47 -07001757status_t NuPlayer::selectTrack(size_t trackIndex, bool select) {
1758 sp<AMessage> msg = new AMessage(kWhatSelectTrack, id());
1759 msg->setSize("trackIndex", trackIndex);
1760 msg->setInt32("select", select);
1761
1762 sp<AMessage> response;
1763 status_t err = msg->postAndAwaitResponse(&response);
1764
Chong Zhang404fced2014-06-11 14:45:31 -07001765 if (err != OK) {
1766 return err;
1767 }
1768
1769 if (!response->findInt32("err", &err)) {
1770 err = OK;
1771 }
1772
Chong Zhangdcb89b32013-08-06 09:44:47 -07001773 return err;
1774}
1775
Marco Nelissenf0b72b52014-09-16 15:43:44 -07001776sp<MetaData> NuPlayer::getFileMeta() {
1777 return mSource->getFileFormatMeta();
1778}
1779
Andreas Huberb7c8e912012-11-27 15:02:53 -08001780void NuPlayer::schedulePollDuration() {
1781 sp<AMessage> msg = new AMessage(kWhatPollDuration, id());
1782 msg->setInt32("generation", mPollDurationGeneration);
1783 msg->post();
1784}
1785
1786void NuPlayer::cancelPollDuration() {
1787 ++mPollDurationGeneration;
1788}
1789
Andreas Hubera1f8ab02012-11-30 10:53:22 -08001790void NuPlayer::processDeferredActions() {
1791 while (!mDeferredActions.empty()) {
1792 // We won't execute any deferred actions until we're no longer in
1793 // an intermediate state, i.e. one more more decoders are currently
1794 // flushing or shutting down.
1795
Andreas Hubera1f8ab02012-11-30 10:53:22 -08001796 if (mFlushingAudio != NONE || mFlushingVideo != NONE) {
1797 // We're currently flushing, postpone the reset until that's
1798 // completed.
1799
1800 ALOGV("postponing action mFlushingAudio=%d, mFlushingVideo=%d",
1801 mFlushingAudio, mFlushingVideo);
1802
1803 break;
1804 }
1805
1806 sp<Action> action = *mDeferredActions.begin();
1807 mDeferredActions.erase(mDeferredActions.begin());
1808
1809 action->execute(this);
1810 }
1811}
1812
Wei Jiae427abf2014-09-22 15:21:11 -07001813void NuPlayer::performSeek(int64_t seekTimeUs, bool needNotify) {
1814 ALOGV("performSeek seekTimeUs=%lld us (%.2f secs), needNotify(%d)",
Andreas Hubera1f8ab02012-11-30 10:53:22 -08001815 seekTimeUs,
Wei Jiae427abf2014-09-22 15:21:11 -07001816 seekTimeUs / 1E6,
1817 needNotify);
Andreas Hubera1f8ab02012-11-30 10:53:22 -08001818
Andy Hungadf34bf2014-09-03 18:22:22 -07001819 if (mSource == NULL) {
1820 // This happens when reset occurs right before the loop mode
1821 // asynchronously seeks to the start of the stream.
1822 LOG_ALWAYS_FATAL_IF(mAudioDecoder != NULL || mVideoDecoder != NULL,
1823 "mSource is NULL and decoders not NULL audio(%p) video(%p)",
1824 mAudioDecoder.get(), mVideoDecoder.get());
1825 return;
1826 }
Andreas Hubera1f8ab02012-11-30 10:53:22 -08001827 mSource->seekTo(seekTimeUs);
Robert Shihd3b0bbb2014-07-23 15:00:25 -07001828 ++mTimedTextGeneration;
Andreas Hubera1f8ab02012-11-30 10:53:22 -08001829
1830 if (mDriver != NULL) {
1831 sp<NuPlayerDriver> driver = mDriver.promote();
1832 if (driver != NULL) {
1833 driver->notifyPosition(seekTimeUs);
Wei Jiae427abf2014-09-22 15:21:11 -07001834 if (needNotify) {
1835 driver->notifySeekComplete();
1836 }
Andreas Hubera1f8ab02012-11-30 10:53:22 -08001837 }
1838 }
1839
1840 // everything's flushed, continue playback.
1841}
1842
1843void NuPlayer::performDecoderFlush() {
1844 ALOGV("performDecoderFlush");
1845
Andreas Huberda9740e2013-04-16 10:54:03 -07001846 if (mAudioDecoder == NULL && mVideoDecoder == NULL) {
Andreas Hubera1f8ab02012-11-30 10:53:22 -08001847 return;
1848 }
1849
1850 mTimeDiscontinuityPending = true;
1851
1852 if (mAudioDecoder != NULL) {
1853 flushDecoder(true /* audio */, false /* needShutdown */);
1854 }
1855
1856 if (mVideoDecoder != NULL) {
1857 flushDecoder(false /* audio */, false /* needShutdown */);
1858 }
1859}
1860
Andreas Huber14f76722013-01-15 09:04:18 -08001861void NuPlayer::performDecoderShutdown(bool audio, bool video) {
1862 ALOGV("performDecoderShutdown audio=%d, video=%d", audio, video);
Andreas Hubera1f8ab02012-11-30 10:53:22 -08001863
Andreas Huber14f76722013-01-15 09:04:18 -08001864 if ((!audio || mAudioDecoder == NULL)
1865 && (!video || mVideoDecoder == NULL)) {
Andreas Hubera1f8ab02012-11-30 10:53:22 -08001866 return;
1867 }
1868
1869 mTimeDiscontinuityPending = true;
1870
Andreas Huber14f76722013-01-15 09:04:18 -08001871 if (audio && mAudioDecoder != NULL) {
Andreas Hubera1f8ab02012-11-30 10:53:22 -08001872 flushDecoder(true /* audio */, true /* needShutdown */);
1873 }
1874
Andreas Huber14f76722013-01-15 09:04:18 -08001875 if (video && mVideoDecoder != NULL) {
Andreas Hubera1f8ab02012-11-30 10:53:22 -08001876 flushDecoder(false /* audio */, true /* needShutdown */);
1877 }
1878}
1879
1880void NuPlayer::performReset() {
1881 ALOGV("performReset");
1882
1883 CHECK(mAudioDecoder == NULL);
1884 CHECK(mVideoDecoder == NULL);
1885
1886 cancelPollDuration();
1887
1888 ++mScanSourcesGeneration;
1889 mScanSourcesPending = false;
1890
Lajos Molnar09524832014-07-17 14:29:51 -07001891 if (mRendererLooper != NULL) {
1892 if (mRenderer != NULL) {
1893 mRendererLooper->unregisterHandler(mRenderer->id());
1894 }
1895 mRendererLooper->stop();
1896 mRendererLooper.clear();
1897 }
Andreas Hubera1f8ab02012-11-30 10:53:22 -08001898 mRenderer.clear();
Wei Jia57568df2014-09-22 10:16:29 -07001899 ++mRendererGeneration;
Andreas Hubera1f8ab02012-11-30 10:53:22 -08001900
1901 if (mSource != NULL) {
1902 mSource->stop();
Andreas Huberb5f25f02013-02-05 10:14:26 -08001903
Andreas Hubera1f8ab02012-11-30 10:53:22 -08001904 mSource.clear();
1905 }
1906
1907 if (mDriver != NULL) {
1908 sp<NuPlayerDriver> driver = mDriver.promote();
1909 if (driver != NULL) {
1910 driver->notifyResetComplete();
1911 }
1912 }
Andreas Huber57a339c2012-12-03 11:18:00 -08001913
1914 mStarted = false;
Andreas Hubera1f8ab02012-11-30 10:53:22 -08001915}
1916
1917void NuPlayer::performScanSources() {
1918 ALOGV("performScanSources");
1919
Andreas Huber57a339c2012-12-03 11:18:00 -08001920 if (!mStarted) {
1921 return;
1922 }
1923
Andreas Hubera1f8ab02012-11-30 10:53:22 -08001924 if (mAudioDecoder == NULL || mVideoDecoder == NULL) {
1925 postScanSources();
1926 }
1927}
1928
Andreas Huber57a339c2012-12-03 11:18:00 -08001929void NuPlayer::performSetSurface(const sp<NativeWindowWrapper> &wrapper) {
1930 ALOGV("performSetSurface");
1931
1932 mNativeWindow = wrapper;
1933
1934 // XXX - ignore error from setVideoScalingMode for now
1935 setVideoScalingMode(mVideoScalingMode);
Chong Zhang13d6faa2014-08-22 15:35:28 -07001936
1937 if (mDriver != NULL) {
1938 sp<NuPlayerDriver> driver = mDriver.promote();
1939 if (driver != NULL) {
1940 driver->notifySetSurfaceComplete();
1941 }
1942 }
Andreas Huber57a339c2012-12-03 11:18:00 -08001943}
1944
Andreas Huber9575c962013-02-05 13:59:56 -08001945void NuPlayer::onSourceNotify(const sp<AMessage> &msg) {
1946 int32_t what;
1947 CHECK(msg->findInt32("what", &what));
1948
1949 switch (what) {
1950 case Source::kWhatPrepared:
1951 {
Andreas Huberb5f28d42013-04-25 15:11:19 -07001952 if (mSource == NULL) {
1953 // This is a stale notification from a source that was
1954 // asynchronously preparing when the client called reset().
1955 // We handled the reset, the source is gone.
1956 break;
1957 }
1958
Andreas Huberec0c5972013-02-05 14:47:13 -08001959 int32_t err;
1960 CHECK(msg->findInt32("err", &err));
1961
Andreas Huber9575c962013-02-05 13:59:56 -08001962 sp<NuPlayerDriver> driver = mDriver.promote();
1963 if (driver != NULL) {
Marco Nelissendd114d12014-05-28 15:23:14 -07001964 // notify duration first, so that it's definitely set when
1965 // the app received the "prepare complete" callback.
1966 int64_t durationUs;
1967 if (mSource->getDuration(&durationUs) == OK) {
1968 driver->notifyDuration(durationUs);
1969 }
Andreas Huberec0c5972013-02-05 14:47:13 -08001970 driver->notifyPrepareCompleted(err);
Andreas Huber9575c962013-02-05 13:59:56 -08001971 }
Andreas Huber99759402013-04-01 14:28:31 -07001972
Andreas Huber9575c962013-02-05 13:59:56 -08001973 break;
1974 }
1975
1976 case Source::kWhatFlagsChanged:
1977 {
1978 uint32_t flags;
1979 CHECK(msg->findInt32("flags", (int32_t *)&flags));
1980
Chong Zhang4b7069d2013-09-11 12:52:43 -07001981 sp<NuPlayerDriver> driver = mDriver.promote();
1982 if (driver != NULL) {
1983 driver->notifyFlagsChanged(flags);
1984 }
1985
Andreas Huber9575c962013-02-05 13:59:56 -08001986 if ((mSourceFlags & Source::FLAG_DYNAMIC_DURATION)
1987 && (!(flags & Source::FLAG_DYNAMIC_DURATION))) {
1988 cancelPollDuration();
1989 } else if (!(mSourceFlags & Source::FLAG_DYNAMIC_DURATION)
1990 && (flags & Source::FLAG_DYNAMIC_DURATION)
1991 && (mAudioDecoder != NULL || mVideoDecoder != NULL)) {
1992 schedulePollDuration();
1993 }
1994
1995 mSourceFlags = flags;
1996 break;
1997 }
1998
1999 case Source::kWhatVideoSizeChanged:
2000 {
Chong Zhangced1c2f2014-08-08 15:22:35 -07002001 sp<AMessage> format;
2002 CHECK(msg->findMessage("format", &format));
Andreas Huber9575c962013-02-05 13:59:56 -08002003
Chong Zhangced1c2f2014-08-08 15:22:35 -07002004 updateVideoSize(format);
Andreas Huber9575c962013-02-05 13:59:56 -08002005 break;
2006 }
2007
Chong Zhang2a3cc9a2014-08-21 17:48:26 -07002008 case Source::kWhatBufferingUpdate:
2009 {
2010 int32_t percentage;
2011 CHECK(msg->findInt32("percentage", &percentage));
2012
2013 notifyListener(MEDIA_BUFFERING_UPDATE, percentage, 0);
2014 break;
2015 }
2016
Roger Jönssonb50e83e2013-01-21 16:26:41 +01002017 case Source::kWhatBufferingStart:
2018 {
2019 notifyListener(MEDIA_INFO, MEDIA_INFO_BUFFERING_START, 0);
2020 break;
2021 }
2022
2023 case Source::kWhatBufferingEnd:
2024 {
2025 notifyListener(MEDIA_INFO, MEDIA_INFO_BUFFERING_END, 0);
2026 break;
2027 }
2028
Chong Zhangdcb89b32013-08-06 09:44:47 -07002029 case Source::kWhatSubtitleData:
2030 {
2031 sp<ABuffer> buffer;
2032 CHECK(msg->findBuffer("buffer", &buffer));
2033
Chong Zhang404fced2014-06-11 14:45:31 -07002034 sendSubtitleData(buffer, 0 /* baseIndex */);
Chong Zhangdcb89b32013-08-06 09:44:47 -07002035 break;
2036 }
2037
Robert Shihd3b0bbb2014-07-23 15:00:25 -07002038 case Source::kWhatTimedTextData:
2039 {
2040 int32_t generation;
2041 if (msg->findInt32("generation", &generation)
2042 && generation != mTimedTextGeneration) {
2043 break;
2044 }
2045
2046 sp<ABuffer> buffer;
2047 CHECK(msg->findBuffer("buffer", &buffer));
2048
2049 sp<NuPlayerDriver> driver = mDriver.promote();
2050 if (driver == NULL) {
2051 break;
2052 }
2053
2054 int posMs;
2055 int64_t timeUs, posUs;
2056 driver->getCurrentPosition(&posMs);
2057 posUs = posMs * 1000;
2058 CHECK(buffer->meta()->findInt64("timeUs", &timeUs));
2059
2060 if (posUs < timeUs) {
2061 if (!msg->findInt32("generation", &generation)) {
2062 msg->setInt32("generation", mTimedTextGeneration);
2063 }
2064 msg->post(timeUs - posUs);
2065 } else {
2066 sendTimedTextData(buffer);
2067 }
2068 break;
2069 }
2070
Andreas Huber14f76722013-01-15 09:04:18 -08002071 case Source::kWhatQueueDecoderShutdown:
2072 {
2073 int32_t audio, video;
2074 CHECK(msg->findInt32("audio", &audio));
2075 CHECK(msg->findInt32("video", &video));
2076
2077 sp<AMessage> reply;
2078 CHECK(msg->findMessage("reply", &reply));
2079
2080 queueDecoderShutdown(audio, video, reply);
2081 break;
2082 }
2083
Ronghua Wu80276872014-08-28 15:50:29 -07002084 case Source::kWhatDrmNoLicense:
2085 {
2086 notifyListener(MEDIA_ERROR, MEDIA_ERROR_UNKNOWN, ERROR_DRM_NO_LICENSE);
2087 break;
2088 }
2089
Andreas Huber9575c962013-02-05 13:59:56 -08002090 default:
2091 TRESPASS();
2092 }
2093}
2094
Chong Zhanga7fa1d92014-06-11 14:49:23 -07002095void NuPlayer::onClosedCaptionNotify(const sp<AMessage> &msg) {
2096 int32_t what;
2097 CHECK(msg->findInt32("what", &what));
2098
2099 switch (what) {
2100 case NuPlayer::CCDecoder::kWhatClosedCaptionData:
2101 {
2102 sp<ABuffer> buffer;
2103 CHECK(msg->findBuffer("buffer", &buffer));
2104
2105 size_t inbandTracks = 0;
2106 if (mSource != NULL) {
2107 inbandTracks = mSource->getTrackCount();
2108 }
2109
2110 sendSubtitleData(buffer, inbandTracks);
2111 break;
2112 }
2113
2114 case NuPlayer::CCDecoder::kWhatTrackAdded:
2115 {
2116 notifyListener(MEDIA_INFO, MEDIA_INFO_METADATA_UPDATE, 0);
2117
2118 break;
2119 }
2120
2121 default:
2122 TRESPASS();
2123 }
2124
2125
2126}
2127
Chong Zhang404fced2014-06-11 14:45:31 -07002128void NuPlayer::sendSubtitleData(const sp<ABuffer> &buffer, int32_t baseIndex) {
2129 int32_t trackIndex;
2130 int64_t timeUs, durationUs;
2131 CHECK(buffer->meta()->findInt32("trackIndex", &trackIndex));
2132 CHECK(buffer->meta()->findInt64("timeUs", &timeUs));
2133 CHECK(buffer->meta()->findInt64("durationUs", &durationUs));
2134
2135 Parcel in;
2136 in.writeInt32(trackIndex + baseIndex);
2137 in.writeInt64(timeUs);
2138 in.writeInt64(durationUs);
2139 in.writeInt32(buffer->size());
2140 in.writeInt32(buffer->size());
2141 in.write(buffer->data(), buffer->size());
2142
2143 notifyListener(MEDIA_SUBTITLE_DATA, 0, 0, &in);
2144}
Robert Shihd3b0bbb2014-07-23 15:00:25 -07002145
2146void NuPlayer::sendTimedTextData(const sp<ABuffer> &buffer) {
2147 const void *data;
2148 size_t size = 0;
2149 int64_t timeUs;
2150 int32_t flag = TextDescriptions::LOCAL_DESCRIPTIONS;
2151
2152 AString mime;
2153 CHECK(buffer->meta()->findString("mime", &mime));
2154 CHECK(strcasecmp(mime.c_str(), MEDIA_MIMETYPE_TEXT_3GPP) == 0);
2155
2156 data = buffer->data();
2157 size = buffer->size();
2158
2159 Parcel parcel;
2160 if (size > 0) {
2161 CHECK(buffer->meta()->findInt64("timeUs", &timeUs));
2162 flag |= TextDescriptions::IN_BAND_TEXT_3GPP;
2163 TextDescriptions::getParcelOfDescriptions(
2164 (const uint8_t *)data, size, flag, timeUs / 1000, &parcel);
2165 }
2166
2167 if ((parcel.dataSize() > 0)) {
2168 notifyListener(MEDIA_TIMED_TEXT, 0, 0, &parcel);
2169 } else { // send an empty timed text
2170 notifyListener(MEDIA_TIMED_TEXT, 0, 0);
2171 }
2172}
Andreas Huberb5f25f02013-02-05 10:14:26 -08002173////////////////////////////////////////////////////////////////////////////////
2174
Chong Zhangced1c2f2014-08-08 15:22:35 -07002175sp<AMessage> NuPlayer::Source::getFormat(bool audio) {
2176 sp<MetaData> meta = getFormatMeta(audio);
2177
2178 if (meta == NULL) {
2179 return NULL;
2180 }
2181
2182 sp<AMessage> msg = new AMessage;
2183
2184 if(convertMetaDataToMessage(meta, &msg) == OK) {
2185 return msg;
2186 }
2187 return NULL;
2188}
2189
Andreas Huber9575c962013-02-05 13:59:56 -08002190void NuPlayer::Source::notifyFlagsChanged(uint32_t flags) {
2191 sp<AMessage> notify = dupNotify();
2192 notify->setInt32("what", kWhatFlagsChanged);
2193 notify->setInt32("flags", flags);
2194 notify->post();
2195}
2196
Chong Zhangced1c2f2014-08-08 15:22:35 -07002197void NuPlayer::Source::notifyVideoSizeChanged(const sp<AMessage> &format) {
Andreas Huber9575c962013-02-05 13:59:56 -08002198 sp<AMessage> notify = dupNotify();
2199 notify->setInt32("what", kWhatVideoSizeChanged);
Chong Zhangced1c2f2014-08-08 15:22:35 -07002200 notify->setMessage("format", format);
Andreas Huber9575c962013-02-05 13:59:56 -08002201 notify->post();
2202}
2203
Andreas Huberec0c5972013-02-05 14:47:13 -08002204void NuPlayer::Source::notifyPrepared(status_t err) {
Andreas Huber9575c962013-02-05 13:59:56 -08002205 sp<AMessage> notify = dupNotify();
2206 notify->setInt32("what", kWhatPrepared);
Andreas Huberec0c5972013-02-05 14:47:13 -08002207 notify->setInt32("err", err);
Andreas Huber9575c962013-02-05 13:59:56 -08002208 notify->post();
2209}
2210
Andreas Huber84333e02014-02-07 15:36:10 -08002211void NuPlayer::Source::onMessageReceived(const sp<AMessage> & /* msg */) {
Andreas Huberb5f25f02013-02-05 10:14:26 -08002212 TRESPASS();
2213}
2214
Andreas Huberf9334412010-12-15 15:17:42 -08002215} // namespace android