blob: b589909346907b816df2155f6d3dac1ff611198a [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 {
67 SeekAction(int64_t seekTimeUs)
68 : mSeekTimeUs(seekTimeUs) {
69 }
70
71 virtual void execute(NuPlayer *player) {
72 player->performSeek(mSeekTimeUs);
73 }
74
75private:
76 int64_t mSeekTimeUs;
77
78 DISALLOW_EVIL_CONSTRUCTORS(SeekAction);
79};
80
Andreas Huber57a339c2012-12-03 11:18:00 -080081struct NuPlayer::SetSurfaceAction : public Action {
82 SetSurfaceAction(const sp<NativeWindowWrapper> &wrapper)
83 : mWrapper(wrapper) {
84 }
85
86 virtual void execute(NuPlayer *player) {
87 player->performSetSurface(mWrapper);
88 }
89
90private:
91 sp<NativeWindowWrapper> mWrapper;
92
93 DISALLOW_EVIL_CONSTRUCTORS(SetSurfaceAction);
94};
95
Andreas Huber14f76722013-01-15 09:04:18 -080096struct NuPlayer::ShutdownDecoderAction : public Action {
97 ShutdownDecoderAction(bool audio, bool video)
98 : mAudio(audio),
99 mVideo(video) {
100 }
101
102 virtual void execute(NuPlayer *player) {
103 player->performDecoderShutdown(mAudio, mVideo);
104 }
105
106private:
107 bool mAudio;
108 bool mVideo;
109
110 DISALLOW_EVIL_CONSTRUCTORS(ShutdownDecoderAction);
111};
112
113struct NuPlayer::PostMessageAction : public Action {
114 PostMessageAction(const sp<AMessage> &msg)
115 : mMessage(msg) {
116 }
117
118 virtual void execute(NuPlayer *) {
119 mMessage->post();
120 }
121
122private:
123 sp<AMessage> mMessage;
124
125 DISALLOW_EVIL_CONSTRUCTORS(PostMessageAction);
126};
127
Andreas Hubera1f8ab02012-11-30 10:53:22 -0800128// Use this if there's no state necessary to save in order to execute
129// the action.
130struct NuPlayer::SimpleAction : public Action {
131 typedef void (NuPlayer::*ActionFunc)();
132
133 SimpleAction(ActionFunc func)
134 : mFunc(func) {
135 }
136
137 virtual void execute(NuPlayer *player) {
138 (player->*mFunc)();
139 }
140
141private:
142 ActionFunc mFunc;
143
144 DISALLOW_EVIL_CONSTRUCTORS(SimpleAction);
145};
146
Andreas Huberf9334412010-12-15 15:17:42 -0800147////////////////////////////////////////////////////////////////////////////////
148
149NuPlayer::NuPlayer()
Andreas Huber9b80c2b2011-06-30 15:47:02 -0700150 : mUIDValid(false),
Andreas Huber9575c962013-02-05 13:59:56 -0800151 mSourceFlags(0),
Wei Jiaac428aa2014-09-02 19:01:34 -0700152 mCurrentPositionUs(0),
Andreas Huber3fe62152011-09-16 15:09:22 -0700153 mVideoIsAVC(false),
Wei Jiabc2fb722014-07-08 16:37:57 -0700154 mOffloadAudio(false),
Andy Hung282a7e32014-08-14 15:56:34 -0700155 mCurrentOffloadInfo(AUDIO_INFO_INITIALIZER),
Wei Jia88703c32014-08-06 11:24:07 -0700156 mAudioDecoderGeneration(0),
157 mVideoDecoderGeneration(0),
Wei Jia57568df2014-09-22 10:16:29 -0700158 mRendererGeneration(0),
Andreas Huber9b80c2b2011-06-30 15:47:02 -0700159 mAudioEOS(false),
Andreas Huberf9334412010-12-15 15:17:42 -0800160 mVideoEOS(false),
Andreas Huber5bc087c2010-12-23 10:27:40 -0800161 mScanSourcesPending(false),
Andreas Huber1aef2112011-01-04 14:01:29 -0800162 mScanSourcesGeneration(0),
Andreas Huberb7c8e912012-11-27 15:02:53 -0800163 mPollDurationGeneration(0),
Robert Shihd3b0bbb2014-07-23 15:00:25 -0700164 mTimedTextGeneration(0),
Andreas Huber6e3d3112011-11-28 12:36:11 -0800165 mTimeDiscontinuityPending(false),
Andreas Huberf9334412010-12-15 15:17:42 -0800166 mFlushingAudio(NONE),
Andreas Huber1aef2112011-01-04 14:01:29 -0800167 mFlushingVideo(NONE),
Andreas Huber3fe62152011-09-16 15:09:22 -0700168 mSkipRenderingAudioUntilMediaTimeUs(-1ll),
169 mSkipRenderingVideoUntilMediaTimeUs(-1ll),
170 mVideoLateByUs(0ll),
171 mNumFramesTotal(0ll),
James Dong0d268a32012-08-31 12:18:27 -0700172 mNumFramesDropped(0ll),
Andreas Huber57a339c2012-12-03 11:18:00 -0800173 mVideoScalingMode(NATIVE_WINDOW_SCALING_MODE_SCALE_TO_WINDOW),
174 mStarted(false) {
Andreas Huberf9334412010-12-15 15:17:42 -0800175}
176
177NuPlayer::~NuPlayer() {
178}
179
Andreas Huber9b80c2b2011-06-30 15:47:02 -0700180void NuPlayer::setUID(uid_t uid) {
181 mUIDValid = true;
182 mUID = uid;
183}
184
Andreas Huber43c3e6c2011-01-05 12:17:08 -0800185void NuPlayer::setDriver(const wp<NuPlayerDriver> &driver) {
186 mDriver = driver;
Andreas Huberf9334412010-12-15 15:17:42 -0800187}
188
Andreas Huber9575c962013-02-05 13:59:56 -0800189void NuPlayer::setDataSourceAsync(const sp<IStreamSource> &source) {
Andreas Huberf9334412010-12-15 15:17:42 -0800190 sp<AMessage> msg = new AMessage(kWhatSetDataSource, id());
191
Andreas Huberb5f25f02013-02-05 10:14:26 -0800192 sp<AMessage> notify = new AMessage(kWhatSourceNotify, id());
193
Andreas Huber240abcc2014-02-13 13:32:37 -0800194 msg->setObject("source", new StreamingSource(notify, source));
Andreas Huber5bc087c2010-12-23 10:27:40 -0800195 msg->post();
196}
Andreas Huberf9334412010-12-15 15:17:42 -0800197
Andreas Huberafed0e12011-09-20 15:39:58 -0700198static bool IsHTTPLiveURL(const char *url) {
199 if (!strncasecmp("http://", url, 7)
Andreas Huber99759402013-04-01 14:28:31 -0700200 || !strncasecmp("https://", url, 8)
201 || !strncasecmp("file://", url, 7)) {
Andreas Huberafed0e12011-09-20 15:39:58 -0700202 size_t len = strlen(url);
203 if (len >= 5 && !strcasecmp(".m3u8", &url[len - 5])) {
204 return true;
205 }
206
207 if (strstr(url,"m3u8")) {
208 return true;
209 }
210 }
211
212 return false;
213}
214
Andreas Huber9575c962013-02-05 13:59:56 -0800215void NuPlayer::setDataSourceAsync(
Andreas Huber1b86fe02014-01-29 11:13:26 -0800216 const sp<IMediaHTTPService> &httpService,
217 const char *url,
218 const KeyedVector<String8, String8> *headers) {
Chong Zhang3de157d2014-08-05 20:54:44 -0700219
Andreas Huber5bc087c2010-12-23 10:27:40 -0800220 sp<AMessage> msg = new AMessage(kWhatSetDataSource, id());
Oscar Rydhé7a33b772012-02-20 10:15:48 +0100221 size_t len = strlen(url);
Andreas Huber5bc087c2010-12-23 10:27:40 -0800222
Andreas Huberb5f25f02013-02-05 10:14:26 -0800223 sp<AMessage> notify = new AMessage(kWhatSourceNotify, id());
224
Andreas Huberafed0e12011-09-20 15:39:58 -0700225 sp<Source> source;
226 if (IsHTTPLiveURL(url)) {
Andreas Huber81e68442014-02-05 11:52:33 -0800227 source = new HTTPLiveSource(notify, httpService, url, headers);
Andreas Huberafed0e12011-09-20 15:39:58 -0700228 } else if (!strncasecmp(url, "rtsp://", 7)) {
Andreas Huber1b86fe02014-01-29 11:13:26 -0800229 source = new RTSPSource(
230 notify, httpService, url, headers, mUIDValid, mUID);
Oscar Rydhé7a33b772012-02-20 10:15:48 +0100231 } else if ((!strncasecmp(url, "http://", 7)
232 || !strncasecmp(url, "https://", 8))
233 && ((len >= 4 && !strcasecmp(".sdp", &url[len - 4]))
234 || strstr(url, ".sdp?"))) {
Andreas Huber1b86fe02014-01-29 11:13:26 -0800235 source = new RTSPSource(
236 notify, httpService, url, headers, mUIDValid, mUID, true);
Andreas Huber2bfdd422011-10-11 15:24:07 -0700237 } else {
Chong Zhang3de157d2014-08-05 20:54:44 -0700238 sp<GenericSource> genericSource =
239 new GenericSource(notify, mUIDValid, mUID);
240 // Don't set FLAG_SECURE on mSourceFlags here for widevine.
241 // The correct flags will be updated in Source::kWhatFlagsChanged
242 // handler when GenericSource is prepared.
Andreas Huber2bfdd422011-10-11 15:24:07 -0700243
Chong Zhanga19f33e2014-08-07 15:35:07 -0700244 status_t err = genericSource->setDataSource(httpService, url, headers);
Chong Zhang3de157d2014-08-05 20:54:44 -0700245
246 if (err == OK) {
247 source = genericSource;
248 } else {
Chong Zhanga19f33e2014-08-07 15:35:07 -0700249 ALOGE("Failed to set data source!");
Chong Zhang3de157d2014-08-05 20:54:44 -0700250 }
251 }
Andreas Huberafed0e12011-09-20 15:39:58 -0700252 msg->setObject("source", source);
253 msg->post();
254}
255
Andreas Huber9575c962013-02-05 13:59:56 -0800256void NuPlayer::setDataSourceAsync(int fd, int64_t offset, int64_t length) {
Andreas Huberafed0e12011-09-20 15:39:58 -0700257 sp<AMessage> msg = new AMessage(kWhatSetDataSource, id());
258
Andreas Huberb5f25f02013-02-05 10:14:26 -0800259 sp<AMessage> notify = new AMessage(kWhatSourceNotify, id());
260
Chong Zhang3de157d2014-08-05 20:54:44 -0700261 sp<GenericSource> source =
262 new GenericSource(notify, mUIDValid, mUID);
263
Chong Zhanga19f33e2014-08-07 15:35:07 -0700264 status_t err = source->setDataSource(fd, offset, length);
Chong Zhang3de157d2014-08-05 20:54:44 -0700265
266 if (err != OK) {
Chong Zhanga19f33e2014-08-07 15:35:07 -0700267 ALOGE("Failed to set data source!");
Chong Zhang3de157d2014-08-05 20:54:44 -0700268 source = NULL;
269 }
270
Andreas Huberafed0e12011-09-20 15:39:58 -0700271 msg->setObject("source", source);
Andreas Huberf9334412010-12-15 15:17:42 -0800272 msg->post();
273}
274
Andreas Huber9575c962013-02-05 13:59:56 -0800275void NuPlayer::prepareAsync() {
276 (new AMessage(kWhatPrepare, id()))->post();
277}
278
Andreas Huber57a339c2012-12-03 11:18:00 -0800279void NuPlayer::setVideoSurfaceTextureAsync(
Andy McFadden8ba01022012-12-18 09:46:54 -0800280 const sp<IGraphicBufferProducer> &bufferProducer) {
Glenn Kasten11731182011-02-08 17:26:17 -0800281 sp<AMessage> msg = new AMessage(kWhatSetVideoNativeWindow, id());
Andreas Huber57a339c2012-12-03 11:18:00 -0800282
Andy McFadden8ba01022012-12-18 09:46:54 -0800283 if (bufferProducer == NULL) {
Andreas Huber57a339c2012-12-03 11:18:00 -0800284 msg->setObject("native-window", NULL);
285 } else {
286 msg->setObject(
287 "native-window",
288 new NativeWindowWrapper(
Wei Jia9c03a402014-08-26 15:24:43 -0700289 new Surface(bufferProducer, true /* controlledByApp */)));
Andreas Huber57a339c2012-12-03 11:18:00 -0800290 }
291
Andreas Huberf9334412010-12-15 15:17:42 -0800292 msg->post();
293}
294
295void NuPlayer::setAudioSink(const sp<MediaPlayerBase::AudioSink> &sink) {
296 sp<AMessage> msg = new AMessage(kWhatSetAudioSink, id());
297 msg->setObject("sink", sink);
298 msg->post();
299}
300
301void NuPlayer::start() {
302 (new AMessage(kWhatStart, id()))->post();
303}
304
Andreas Huber43c3e6c2011-01-05 12:17:08 -0800305void NuPlayer::pause() {
Andreas Huberb4082222011-01-20 15:23:04 -0800306 (new AMessage(kWhatPause, id()))->post();
Andreas Huber43c3e6c2011-01-05 12:17:08 -0800307}
308
309void NuPlayer::resume() {
Andreas Huberb4082222011-01-20 15:23:04 -0800310 (new AMessage(kWhatResume, id()))->post();
Andreas Huber43c3e6c2011-01-05 12:17:08 -0800311}
312
Andreas Huber1aef2112011-01-04 14:01:29 -0800313void NuPlayer::resetAsync() {
Chong Zhang48296b72014-09-14 14:28:45 -0700314 if (mSource != NULL) {
315 // During a reset, the data source might be unresponsive already, we need to
316 // disconnect explicitly so that reads exit promptly.
317 // We can't queue the disconnect request to the looper, as it might be
318 // queued behind a stuck read and never gets processed.
319 // Doing a disconnect outside the looper to allows the pending reads to exit
320 // (either successfully or with error).
321 mSource->disconnect();
322 }
323
Andreas Huber1aef2112011-01-04 14:01:29 -0800324 (new AMessage(kWhatReset, id()))->post();
325}
326
Andreas Huber43c3e6c2011-01-05 12:17:08 -0800327void NuPlayer::seekToAsync(int64_t seekTimeUs) {
328 sp<AMessage> msg = new AMessage(kWhatSeek, id());
329 msg->setInt64("seekTimeUs", seekTimeUs);
330 msg->post();
331}
332
Andreas Huber53df1a42010-12-22 10:03:04 -0800333// static
Andreas Huber1aef2112011-01-04 14:01:29 -0800334bool NuPlayer::IsFlushingState(FlushStatus state, bool *needShutdown) {
Andreas Huber53df1a42010-12-22 10:03:04 -0800335 switch (state) {
336 case FLUSHING_DECODER:
Andreas Huber1aef2112011-01-04 14:01:29 -0800337 if (needShutdown != NULL) {
338 *needShutdown = false;
Andreas Huber53df1a42010-12-22 10:03:04 -0800339 }
340 return true;
341
Andreas Huber1aef2112011-01-04 14:01:29 -0800342 case FLUSHING_DECODER_SHUTDOWN:
343 if (needShutdown != NULL) {
344 *needShutdown = true;
Andreas Huber53df1a42010-12-22 10:03:04 -0800345 }
346 return true;
347
348 default:
349 return false;
350 }
351}
352
Chong Zhang404fced2014-06-11 14:45:31 -0700353void NuPlayer::writeTrackInfo(
354 Parcel* reply, const sp<AMessage> format) const {
355 int32_t trackType;
356 CHECK(format->findInt32("type", &trackType));
357
358 AString lang;
359 CHECK(format->findString("language", &lang));
360
361 reply->writeInt32(2); // write something non-zero
362 reply->writeInt32(trackType);
363 reply->writeString16(String16(lang.c_str()));
364
365 if (trackType == MEDIA_TRACK_TYPE_SUBTITLE) {
366 AString mime;
367 CHECK(format->findString("mime", &mime));
368
369 int32_t isAuto, isDefault, isForced;
370 CHECK(format->findInt32("auto", &isAuto));
371 CHECK(format->findInt32("default", &isDefault));
372 CHECK(format->findInt32("forced", &isForced));
373
374 reply->writeString16(String16(mime.c_str()));
375 reply->writeInt32(isAuto);
376 reply->writeInt32(isDefault);
377 reply->writeInt32(isForced);
378 }
379}
380
Andreas Huberf9334412010-12-15 15:17:42 -0800381void NuPlayer::onMessageReceived(const sp<AMessage> &msg) {
382 switch (msg->what()) {
383 case kWhatSetDataSource:
384 {
Steve Block3856b092011-10-20 11:56:00 +0100385 ALOGV("kWhatSetDataSource");
Andreas Huberf9334412010-12-15 15:17:42 -0800386
387 CHECK(mSource == NULL);
388
Chong Zhang3de157d2014-08-05 20:54:44 -0700389 status_t err = OK;
Andreas Huber5bc087c2010-12-23 10:27:40 -0800390 sp<RefBase> obj;
391 CHECK(msg->findObject("source", &obj));
Chong Zhang3de157d2014-08-05 20:54:44 -0700392 if (obj != NULL) {
393 mSource = static_cast<Source *>(obj.get());
Chong Zhang3de157d2014-08-05 20:54:44 -0700394 } else {
395 err = UNKNOWN_ERROR;
396 }
Andreas Huber9575c962013-02-05 13:59:56 -0800397
398 CHECK(mDriver != NULL);
399 sp<NuPlayerDriver> driver = mDriver.promote();
400 if (driver != NULL) {
Chong Zhang3de157d2014-08-05 20:54:44 -0700401 driver->notifySetDataSourceCompleted(err);
Andreas Huber9575c962013-02-05 13:59:56 -0800402 }
403 break;
404 }
405
406 case kWhatPrepare:
407 {
408 mSource->prepareAsync();
Andreas Huberf9334412010-12-15 15:17:42 -0800409 break;
410 }
411
Chong Zhangdcb89b32013-08-06 09:44:47 -0700412 case kWhatGetTrackInfo:
413 {
414 uint32_t replyID;
415 CHECK(msg->senderAwaitsResponse(&replyID));
416
Chong Zhang404fced2014-06-11 14:45:31 -0700417 Parcel* reply;
418 CHECK(msg->findPointer("reply", (void**)&reply));
419
420 size_t inbandTracks = 0;
Chong Zhangdcb89b32013-08-06 09:44:47 -0700421 if (mSource != NULL) {
Chong Zhang404fced2014-06-11 14:45:31 -0700422 inbandTracks = mSource->getTrackCount();
423 }
424
Chong Zhanga7fa1d92014-06-11 14:49:23 -0700425 size_t ccTracks = 0;
426 if (mCCDecoder != NULL) {
427 ccTracks = mCCDecoder->getTrackCount();
428 }
429
Chong Zhang404fced2014-06-11 14:45:31 -0700430 // total track count
Chong Zhanga7fa1d92014-06-11 14:49:23 -0700431 reply->writeInt32(inbandTracks + ccTracks);
Chong Zhang404fced2014-06-11 14:45:31 -0700432
433 // write inband tracks
434 for (size_t i = 0; i < inbandTracks; ++i) {
435 writeTrackInfo(reply, mSource->getTrackInfo(i));
Chong Zhangdcb89b32013-08-06 09:44:47 -0700436 }
437
Chong Zhanga7fa1d92014-06-11 14:49:23 -0700438 // write CC track
439 for (size_t i = 0; i < ccTracks; ++i) {
440 writeTrackInfo(reply, mCCDecoder->getTrackInfo(i));
441 }
442
Chong Zhangdcb89b32013-08-06 09:44:47 -0700443 sp<AMessage> response = new AMessage;
Chong Zhangdcb89b32013-08-06 09:44:47 -0700444 response->postReply(replyID);
445 break;
446 }
447
Robert Shih7c4f0d72014-07-09 18:53:31 -0700448 case kWhatGetSelectedTrack:
449 {
450 status_t err = INVALID_OPERATION;
451 if (mSource != NULL) {
452 err = OK;
453
454 int32_t type32;
455 CHECK(msg->findInt32("type", (int32_t*)&type32));
456 media_track_type type = (media_track_type)type32;
457 ssize_t selectedTrack = mSource->getSelectedTrack(type);
458
459 Parcel* reply;
460 CHECK(msg->findPointer("reply", (void**)&reply));
461 reply->writeInt32(selectedTrack);
462 }
463
464 sp<AMessage> response = new AMessage;
465 response->setInt32("err", err);
466
467 uint32_t replyID;
468 CHECK(msg->senderAwaitsResponse(&replyID));
469 response->postReply(replyID);
470 break;
471 }
472
Chong Zhangdcb89b32013-08-06 09:44:47 -0700473 case kWhatSelectTrack:
474 {
475 uint32_t replyID;
476 CHECK(msg->senderAwaitsResponse(&replyID));
477
Chong Zhang404fced2014-06-11 14:45:31 -0700478 size_t trackIndex;
479 int32_t select;
480 CHECK(msg->findSize("trackIndex", &trackIndex));
481 CHECK(msg->findInt32("select", &select));
482
Chong Zhangdcb89b32013-08-06 09:44:47 -0700483 status_t err = INVALID_OPERATION;
Chong Zhang404fced2014-06-11 14:45:31 -0700484
485 size_t inbandTracks = 0;
Chong Zhangdcb89b32013-08-06 09:44:47 -0700486 if (mSource != NULL) {
Chong Zhang404fced2014-06-11 14:45:31 -0700487 inbandTracks = mSource->getTrackCount();
488 }
Chong Zhanga7fa1d92014-06-11 14:49:23 -0700489 size_t ccTracks = 0;
490 if (mCCDecoder != NULL) {
491 ccTracks = mCCDecoder->getTrackCount();
492 }
Chong Zhang404fced2014-06-11 14:45:31 -0700493
494 if (trackIndex < inbandTracks) {
Chong Zhangdcb89b32013-08-06 09:44:47 -0700495 err = mSource->selectTrack(trackIndex, select);
Robert Shihd3b0bbb2014-07-23 15:00:25 -0700496
497 if (!select && err == OK) {
498 int32_t type;
499 sp<AMessage> info = mSource->getTrackInfo(trackIndex);
500 if (info != NULL
501 && info->findInt32("type", &type)
502 && type == MEDIA_TRACK_TYPE_TIMEDTEXT) {
503 ++mTimedTextGeneration;
504 }
505 }
Chong Zhanga7fa1d92014-06-11 14:49:23 -0700506 } else {
507 trackIndex -= inbandTracks;
508
509 if (trackIndex < ccTracks) {
510 err = mCCDecoder->selectTrack(trackIndex, select);
511 }
Chong Zhangdcb89b32013-08-06 09:44:47 -0700512 }
513
514 sp<AMessage> response = new AMessage;
515 response->setInt32("err", err);
516
517 response->postReply(replyID);
518 break;
519 }
520
Andreas Huberb7c8e912012-11-27 15:02:53 -0800521 case kWhatPollDuration:
522 {
523 int32_t generation;
524 CHECK(msg->findInt32("generation", &generation));
525
526 if (generation != mPollDurationGeneration) {
527 // stale
528 break;
529 }
530
531 int64_t durationUs;
532 if (mDriver != NULL && mSource->getDuration(&durationUs) == OK) {
533 sp<NuPlayerDriver> driver = mDriver.promote();
534 if (driver != NULL) {
535 driver->notifyDuration(durationUs);
536 }
537 }
538
539 msg->post(1000000ll); // poll again in a second.
540 break;
541 }
542
Glenn Kasten11731182011-02-08 17:26:17 -0800543 case kWhatSetVideoNativeWindow:
Andreas Huberf9334412010-12-15 15:17:42 -0800544 {
Steve Block3856b092011-10-20 11:56:00 +0100545 ALOGV("kWhatSetVideoNativeWindow");
Andreas Huberf9334412010-12-15 15:17:42 -0800546
Andreas Huber57a339c2012-12-03 11:18:00 -0800547 mDeferredActions.push_back(
Andreas Huber14f76722013-01-15 09:04:18 -0800548 new ShutdownDecoderAction(
549 false /* audio */, true /* video */));
Andreas Huber57a339c2012-12-03 11:18:00 -0800550
Andreas Huberf9334412010-12-15 15:17:42 -0800551 sp<RefBase> obj;
Glenn Kasten11731182011-02-08 17:26:17 -0800552 CHECK(msg->findObject("native-window", &obj));
Andreas Huberf9334412010-12-15 15:17:42 -0800553
Andreas Huber57a339c2012-12-03 11:18:00 -0800554 mDeferredActions.push_back(
555 new SetSurfaceAction(
556 static_cast<NativeWindowWrapper *>(obj.get())));
James Dong0d268a32012-08-31 12:18:27 -0700557
Andreas Huber57a339c2012-12-03 11:18:00 -0800558 if (obj != NULL) {
Andy Hung73535852014-09-05 11:42:58 -0700559 if (mStarted && mVideoDecoder != NULL) {
560 // Issue a seek to refresh the video screen only if started otherwise
561 // the extractor may not yet be started and will assert.
562 // If the video decoder is not set (perhaps audio only in this case)
563 // do not perform a seek as it is not needed.
564 mDeferredActions.push_back(new SeekAction(mCurrentPositionUs));
565 }
Wei Jiaac428aa2014-09-02 19:01:34 -0700566
Andreas Huber57a339c2012-12-03 11:18:00 -0800567 // If there is a new surface texture, instantiate decoders
568 // again if possible.
569 mDeferredActions.push_back(
570 new SimpleAction(&NuPlayer::performScanSources));
571 }
572
573 processDeferredActions();
Andreas Huberf9334412010-12-15 15:17:42 -0800574 break;
575 }
576
577 case kWhatSetAudioSink:
578 {
Steve Block3856b092011-10-20 11:56:00 +0100579 ALOGV("kWhatSetAudioSink");
Andreas Huberf9334412010-12-15 15:17:42 -0800580
581 sp<RefBase> obj;
582 CHECK(msg->findObject("sink", &obj));
583
584 mAudioSink = static_cast<MediaPlayerBase::AudioSink *>(obj.get());
585 break;
586 }
587
588 case kWhatStart:
589 {
Steve Block3856b092011-10-20 11:56:00 +0100590 ALOGV("kWhatStart");
Andreas Huber43c3e6c2011-01-05 12:17:08 -0800591
Andreas Huber3fe62152011-09-16 15:09:22 -0700592 mVideoIsAVC = false;
Wei Jiabc2fb722014-07-08 16:37:57 -0700593 mOffloadAudio = false;
Andreas Huber1aef2112011-01-04 14:01:29 -0800594 mAudioEOS = false;
595 mVideoEOS = false;
Andreas Huber32f3cef2011-03-02 15:34:46 -0800596 mSkipRenderingAudioUntilMediaTimeUs = -1;
597 mSkipRenderingVideoUntilMediaTimeUs = -1;
Andreas Huber3fe62152011-09-16 15:09:22 -0700598 mVideoLateByUs = 0;
599 mNumFramesTotal = 0;
600 mNumFramesDropped = 0;
Andreas Huber57a339c2012-12-03 11:18:00 -0800601 mStarted = true;
Andreas Huber1aef2112011-01-04 14:01:29 -0800602
Lajos Molnar09524832014-07-17 14:29:51 -0700603 /* instantiate decoders now for secure playback */
604 if (mSourceFlags & Source::FLAG_SECURE) {
605 if (mNativeWindow != NULL) {
606 instantiateDecoder(false, &mVideoDecoder);
607 }
608
609 if (mAudioSink != NULL) {
610 instantiateDecoder(true, &mAudioDecoder);
611 }
612 }
613
Andreas Huber5bc087c2010-12-23 10:27:40 -0800614 mSource->start();
Andreas Huberf9334412010-12-15 15:17:42 -0800615
Andreas Huberd5e56232013-03-12 11:01:43 -0700616 uint32_t flags = 0;
617
618 if (mSource->isRealTime()) {
619 flags |= Renderer::FLAG_REAL_TIME;
620 }
621
Wei Jiabc2fb722014-07-08 16:37:57 -0700622 sp<MetaData> audioMeta = mSource->getFormatMeta(true /* audio */);
623 audio_stream_type_t streamType = AUDIO_STREAM_MUSIC;
624 if (mAudioSink != NULL) {
625 streamType = mAudioSink->getAudioStreamType();
626 }
627
628 sp<AMessage> videoFormat = mSource->getFormat(false /* audio */);
629
630 mOffloadAudio =
631 canOffloadStream(audioMeta, (videoFormat != NULL),
632 true /* is_streaming */, streamType);
633 if (mOffloadAudio) {
634 flags |= Renderer::FLAG_OFFLOAD_AUDIO;
635 }
636
Wei Jia57568df2014-09-22 10:16:29 -0700637 sp<AMessage> notify = new AMessage(kWhatRendererNotify, id());
638 ++mRendererGeneration;
639 notify->setInt32("generation", mRendererGeneration);
640 mRenderer = new Renderer(mAudioSink, notify, flags);
Andreas Huberf9334412010-12-15 15:17:42 -0800641
Lajos Molnar09524832014-07-17 14:29:51 -0700642 mRendererLooper = new ALooper;
643 mRendererLooper->setName("NuPlayerRenderer");
644 mRendererLooper->start(false, false, ANDROID_PRIORITY_AUDIO);
645 mRendererLooper->registerHandler(mRenderer);
Andreas Huberf9334412010-12-15 15:17:42 -0800646
Lajos Molnarc851b5d2014-09-18 14:14:29 -0700647 sp<MetaData> meta = getFileMeta();
648 int32_t rate;
649 if (meta != NULL
650 && meta->findInt32(kKeyFrameRate, &rate) && rate > 0) {
651 mRenderer->setVideoFrameRate(rate);
652 }
653
Andreas Huber1aef2112011-01-04 14:01:29 -0800654 postScanSources();
Andreas Huberf9334412010-12-15 15:17:42 -0800655 break;
656 }
657
658 case kWhatScanSources:
659 {
Andreas Huber1aef2112011-01-04 14:01:29 -0800660 int32_t generation;
661 CHECK(msg->findInt32("generation", &generation));
662 if (generation != mScanSourcesGeneration) {
663 // Drop obsolete msg.
664 break;
665 }
666
Andreas Huber5bc087c2010-12-23 10:27:40 -0800667 mScanSourcesPending = false;
668
Steve Block3856b092011-10-20 11:56:00 +0100669 ALOGV("scanning sources haveAudio=%d, haveVideo=%d",
Andreas Huber43c3e6c2011-01-05 12:17:08 -0800670 mAudioDecoder != NULL, mVideoDecoder != NULL);
671
Andreas Huberb7c8e912012-11-27 15:02:53 -0800672 bool mHadAnySourcesBefore =
673 (mAudioDecoder != NULL) || (mVideoDecoder != NULL);
674
Andy Hung282a7e32014-08-14 15:56:34 -0700675 // initialize video before audio because successful initialization of
676 // video may change deep buffer mode of audio.
Haynes Mathew George5d246ef2012-07-09 10:36:57 -0700677 if (mNativeWindow != NULL) {
678 instantiateDecoder(false, &mVideoDecoder);
679 }
Andreas Huberf9334412010-12-15 15:17:42 -0800680
681 if (mAudioSink != NULL) {
Andy Hung282a7e32014-08-14 15:56:34 -0700682 if (mOffloadAudio) {
683 // open audio sink early under offload mode.
684 sp<AMessage> format = mSource->getFormat(true /*audio*/);
685 openAudioSink(format, true /*offloadOnly*/);
686 }
Andreas Huber5bc087c2010-12-23 10:27:40 -0800687 instantiateDecoder(true, &mAudioDecoder);
Andreas Huberf9334412010-12-15 15:17:42 -0800688 }
689
Andreas Huberb7c8e912012-11-27 15:02:53 -0800690 if (!mHadAnySourcesBefore
691 && (mAudioDecoder != NULL || mVideoDecoder != NULL)) {
692 // This is the first time we've found anything playable.
693
Andreas Huber9575c962013-02-05 13:59:56 -0800694 if (mSourceFlags & Source::FLAG_DYNAMIC_DURATION) {
Andreas Huberb7c8e912012-11-27 15:02:53 -0800695 schedulePollDuration();
696 }
697 }
698
Andreas Hubereac68ba2011-09-27 12:12:25 -0700699 status_t err;
700 if ((err = mSource->feedMoreTSData()) != OK) {
Andreas Huber1aef2112011-01-04 14:01:29 -0800701 if (mAudioDecoder == NULL && mVideoDecoder == NULL) {
702 // We're not currently decoding anything (no audio or
703 // video tracks found) and we just ran out of input data.
Andreas Hubereac68ba2011-09-27 12:12:25 -0700704
705 if (err == ERROR_END_OF_STREAM) {
706 notifyListener(MEDIA_PLAYBACK_COMPLETE, 0, 0);
707 } else {
708 notifyListener(MEDIA_ERROR, MEDIA_ERROR_UNKNOWN, err);
709 }
Andreas Huber1aef2112011-01-04 14:01:29 -0800710 }
Andreas Huberf9334412010-12-15 15:17:42 -0800711 break;
712 }
713
Andreas Huberfbe9d812012-08-31 14:05:27 -0700714 if ((mAudioDecoder == NULL && mAudioSink != NULL)
715 || (mVideoDecoder == NULL && mNativeWindow != NULL)) {
Andreas Huberf9334412010-12-15 15:17:42 -0800716 msg->post(100000ll);
Andreas Huber5bc087c2010-12-23 10:27:40 -0800717 mScanSourcesPending = true;
Andreas Huberf9334412010-12-15 15:17:42 -0800718 }
719 break;
720 }
721
722 case kWhatVideoNotify:
723 case kWhatAudioNotify:
724 {
725 bool audio = msg->what() == kWhatAudioNotify;
726
Wei Jia88703c32014-08-06 11:24:07 -0700727 int32_t currentDecoderGeneration =
728 (audio? mAudioDecoderGeneration : mVideoDecoderGeneration);
729 int32_t requesterGeneration = currentDecoderGeneration - 1;
730 CHECK(msg->findInt32("generation", &requesterGeneration));
731
732 if (requesterGeneration != currentDecoderGeneration) {
733 ALOGV("got message from old %s decoder, generation(%d:%d)",
734 audio ? "audio" : "video", requesterGeneration,
735 currentDecoderGeneration);
736 sp<AMessage> reply;
737 if (!(msg->findMessage("reply", &reply))) {
738 return;
739 }
740
741 reply->setInt32("err", INFO_DISCONTINUITY);
742 reply->post();
743 return;
744 }
745
Andreas Huberf9334412010-12-15 15:17:42 -0800746 int32_t what;
Lajos Molnar1cd13982014-01-17 15:12:51 -0800747 CHECK(msg->findInt32("what", &what));
Andreas Huberf9334412010-12-15 15:17:42 -0800748
Lajos Molnar1cd13982014-01-17 15:12:51 -0800749 if (what == Decoder::kWhatFillThisBuffer) {
Andreas Huberf9334412010-12-15 15:17:42 -0800750 status_t err = feedDecoderInputData(
Lajos Molnar1cd13982014-01-17 15:12:51 -0800751 audio, msg);
Andreas Huberf9334412010-12-15 15:17:42 -0800752
Andreas Huber5bc087c2010-12-23 10:27:40 -0800753 if (err == -EWOULDBLOCK) {
Andreas Hubereac68ba2011-09-27 12:12:25 -0700754 if (mSource->feedMoreTSData() == OK) {
Phil Burkc5cc2e22014-09-09 20:08:39 -0700755 msg->post(10 * 1000ll);
Andreas Huber5bc087c2010-12-23 10:27:40 -0800756 }
Andreas Huberf9334412010-12-15 15:17:42 -0800757 }
Lajos Molnar1cd13982014-01-17 15:12:51 -0800758 } else if (what == Decoder::kWhatEOS) {
Andreas Huberdc9bacd2011-09-26 10:53:29 -0700759 int32_t err;
Lajos Molnar1cd13982014-01-17 15:12:51 -0800760 CHECK(msg->findInt32("err", &err));
Andreas Huberdc9bacd2011-09-26 10:53:29 -0700761
762 if (err == ERROR_END_OF_STREAM) {
Steve Block3856b092011-10-20 11:56:00 +0100763 ALOGV("got %s decoder EOS", audio ? "audio" : "video");
Andreas Huberdc9bacd2011-09-26 10:53:29 -0700764 } else {
Steve Block3856b092011-10-20 11:56:00 +0100765 ALOGV("got %s decoder EOS w/ error %d",
Andreas Huberdc9bacd2011-09-26 10:53:29 -0700766 audio ? "audio" : "video",
767 err);
768 }
769
770 mRenderer->queueEOS(audio, err);
Lajos Molnar1cd13982014-01-17 15:12:51 -0800771 } else if (what == Decoder::kWhatFlushCompleted) {
Andreas Huber1aef2112011-01-04 14:01:29 -0800772 bool needShutdown;
Andreas Huber53df1a42010-12-22 10:03:04 -0800773
Andreas Huberf9334412010-12-15 15:17:42 -0800774 if (audio) {
Andreas Huber1aef2112011-01-04 14:01:29 -0800775 CHECK(IsFlushingState(mFlushingAudio, &needShutdown));
Andreas Huberf9334412010-12-15 15:17:42 -0800776 mFlushingAudio = FLUSHED;
777 } else {
Andreas Huber1aef2112011-01-04 14:01:29 -0800778 CHECK(IsFlushingState(mFlushingVideo, &needShutdown));
Andreas Huberf9334412010-12-15 15:17:42 -0800779 mFlushingVideo = FLUSHED;
Andreas Huber3fe62152011-09-16 15:09:22 -0700780
781 mVideoLateByUs = 0;
Andreas Huberf9334412010-12-15 15:17:42 -0800782 }
783
Steve Block3856b092011-10-20 11:56:00 +0100784 ALOGV("decoder %s flush completed", audio ? "audio" : "video");
Andreas Huberf9334412010-12-15 15:17:42 -0800785
Andreas Huber1aef2112011-01-04 14:01:29 -0800786 if (needShutdown) {
Steve Block3856b092011-10-20 11:56:00 +0100787 ALOGV("initiating %s decoder shutdown",
Andreas Huber53df1a42010-12-22 10:03:04 -0800788 audio ? "audio" : "video");
Andreas Huberf9334412010-12-15 15:17:42 -0800789
Lajos Molnar87603c02014-08-20 19:25:30 -0700790 getDecoder(audio)->initiateShutdown();
Andreas Huberf9334412010-12-15 15:17:42 -0800791
Andreas Huber53df1a42010-12-22 10:03:04 -0800792 if (audio) {
793 mFlushingAudio = SHUTTING_DOWN_DECODER;
794 } else {
795 mFlushingVideo = SHUTTING_DOWN_DECODER;
796 }
Andreas Huberf9334412010-12-15 15:17:42 -0800797 }
Andreas Huber3831a062010-12-21 10:22:33 -0800798
799 finishFlushIfPossible();
Lajos Molnar1cd13982014-01-17 15:12:51 -0800800 } else if (what == Decoder::kWhatOutputFormatChanged) {
801 sp<AMessage> format;
802 CHECK(msg->findMessage("format", &format));
803
Andreas Huber31e25082011-01-10 10:38:31 -0800804 if (audio) {
Andy Hung282a7e32014-08-14 15:56:34 -0700805 openAudioSink(format, false /*offloadOnly*/);
Andreas Huber31e25082011-01-10 10:38:31 -0800806 } else {
807 // video
Chong Zhangced1c2f2014-08-08 15:22:35 -0700808 sp<AMessage> inputFormat =
809 mSource->getFormat(false /* audio */);
Andreas Huber3831a062010-12-21 10:22:33 -0800810
Chong Zhangced1c2f2014-08-08 15:22:35 -0700811 updateVideoSize(inputFormat, format);
Andreas Huber31e25082011-01-10 10:38:31 -0800812 }
Lajos Molnar1cd13982014-01-17 15:12:51 -0800813 } else if (what == Decoder::kWhatShutdownCompleted) {
Steve Block3856b092011-10-20 11:56:00 +0100814 ALOGV("%s shutdown completed", audio ? "audio" : "video");
Andreas Huber3831a062010-12-21 10:22:33 -0800815 if (audio) {
816 mAudioDecoder.clear();
Wei Jia57568df2014-09-22 10:16:29 -0700817 ++mAudioDecoderGeneration;
Andreas Huber3831a062010-12-21 10:22:33 -0800818
819 CHECK_EQ((int)mFlushingAudio, (int)SHUTTING_DOWN_DECODER);
820 mFlushingAudio = SHUT_DOWN;
821 } else {
822 mVideoDecoder.clear();
Wei Jia57568df2014-09-22 10:16:29 -0700823 ++mVideoDecoderGeneration;
Andreas Huber3831a062010-12-21 10:22:33 -0800824
825 CHECK_EQ((int)mFlushingVideo, (int)SHUTTING_DOWN_DECODER);
826 mFlushingVideo = SHUT_DOWN;
827 }
828
829 finishFlushIfPossible();
Lajos Molnar1cd13982014-01-17 15:12:51 -0800830 } else if (what == Decoder::kWhatError) {
Steve Block29357bc2012-01-06 19:20:56 +0000831 ALOGE("Received error from %s decoder, aborting playback.",
Andreas Huberc92fd242011-08-16 13:48:44 -0700832 audio ? "audio" : "video");
833
Chong Zhangf4c0a942014-08-11 15:14:10 -0700834 status_t err;
835 if (!msg->findInt32("err", &err)) {
836 err = UNKNOWN_ERROR;
837 }
838 mRenderer->queueEOS(audio, err);
Marco Nelissen9e2b7912014-08-18 16:13:03 -0700839 if (audio && mFlushingAudio != NONE) {
840 mAudioDecoder.clear();
Wei Jia57568df2014-09-22 10:16:29 -0700841 ++mAudioDecoderGeneration;
Marco Nelissen9e2b7912014-08-18 16:13:03 -0700842 mFlushingAudio = SHUT_DOWN;
843 } else if (!audio && mFlushingVideo != NONE){
844 mVideoDecoder.clear();
Wei Jia57568df2014-09-22 10:16:29 -0700845 ++mVideoDecoderGeneration;
Marco Nelissen9e2b7912014-08-18 16:13:03 -0700846 mFlushingVideo = SHUT_DOWN;
847 }
848 finishFlushIfPossible();
Lajos Molnar1cd13982014-01-17 15:12:51 -0800849 } else if (what == Decoder::kWhatDrainThisBuffer) {
850 renderBuffer(audio, msg);
851 } else {
852 ALOGV("Unhandled decoder notification %d '%c%c%c%c'.",
Andreas Hubera1f8ab02012-11-30 10:53:22 -0800853 what,
854 what >> 24,
855 (what >> 16) & 0xff,
856 (what >> 8) & 0xff,
857 what & 0xff);
Andreas Huberf9334412010-12-15 15:17:42 -0800858 }
859
860 break;
861 }
862
863 case kWhatRendererNotify:
864 {
Wei Jia57568df2014-09-22 10:16:29 -0700865 int32_t requesterGeneration = mRendererGeneration - 1;
866 CHECK(msg->findInt32("generation", &requesterGeneration));
867 if (requesterGeneration != mRendererGeneration) {
868 ALOGV("got message from old renderer, generation(%d:%d)",
869 requesterGeneration, mRendererGeneration);
870 return;
871 }
872
Andreas Huberf9334412010-12-15 15:17:42 -0800873 int32_t what;
874 CHECK(msg->findInt32("what", &what));
875
876 if (what == Renderer::kWhatEOS) {
877 int32_t audio;
878 CHECK(msg->findInt32("audio", &audio));
879
Andreas Huberc92fd242011-08-16 13:48:44 -0700880 int32_t finalResult;
881 CHECK(msg->findInt32("finalResult", &finalResult));
882
Andreas Huberf9334412010-12-15 15:17:42 -0800883 if (audio) {
884 mAudioEOS = true;
885 } else {
886 mVideoEOS = true;
887 }
888
Andreas Huberc92fd242011-08-16 13:48:44 -0700889 if (finalResult == ERROR_END_OF_STREAM) {
Steve Block3856b092011-10-20 11:56:00 +0100890 ALOGV("reached %s EOS", audio ? "audio" : "video");
Andreas Huberc92fd242011-08-16 13:48:44 -0700891 } else {
Steve Block29357bc2012-01-06 19:20:56 +0000892 ALOGE("%s track encountered an error (%d)",
Andreas Huberc92fd242011-08-16 13:48:44 -0700893 audio ? "audio" : "video", finalResult);
894
895 notifyListener(
896 MEDIA_ERROR, MEDIA_ERROR_UNKNOWN, finalResult);
897 }
Andreas Huberf9334412010-12-15 15:17:42 -0800898
899 if ((mAudioEOS || mAudioDecoder == NULL)
900 && (mVideoEOS || mVideoDecoder == NULL)) {
901 notifyListener(MEDIA_PLAYBACK_COMPLETE, 0, 0);
902 }
Andreas Huber43c3e6c2011-01-05 12:17:08 -0800903 } else if (what == Renderer::kWhatPosition) {
904 int64_t positionUs;
905 CHECK(msg->findInt64("positionUs", &positionUs));
Wei Jiaac428aa2014-09-02 19:01:34 -0700906 mCurrentPositionUs = positionUs;
Andreas Huber43c3e6c2011-01-05 12:17:08 -0800907
Andreas Huber3fe62152011-09-16 15:09:22 -0700908 CHECK(msg->findInt64("videoLateByUs", &mVideoLateByUs));
909
Andreas Huber43c3e6c2011-01-05 12:17:08 -0800910 if (mDriver != NULL) {
911 sp<NuPlayerDriver> driver = mDriver.promote();
912 if (driver != NULL) {
913 driver->notifyPosition(positionUs);
Andreas Huber3fe62152011-09-16 15:09:22 -0700914
915 driver->notifyFrameStats(
916 mNumFramesTotal, mNumFramesDropped);
Andreas Huber43c3e6c2011-01-05 12:17:08 -0800917 }
918 }
Andreas Huber3fe62152011-09-16 15:09:22 -0700919 } else if (what == Renderer::kWhatFlushComplete) {
Andreas Huberf9334412010-12-15 15:17:42 -0800920 int32_t audio;
921 CHECK(msg->findInt32("audio", &audio));
922
Steve Block3856b092011-10-20 11:56:00 +0100923 ALOGV("renderer %s flush completed.", audio ? "audio" : "video");
James Dongf57b4ea2012-07-20 13:38:36 -0700924 } else if (what == Renderer::kWhatVideoRenderingStart) {
925 notifyListener(MEDIA_INFO, MEDIA_INFO_RENDERING_START, 0);
Lajos Molnarcbaffcf2013-08-14 18:30:38 -0700926 } else if (what == Renderer::kWhatMediaRenderingStart) {
927 ALOGV("media rendering started");
928 notifyListener(MEDIA_STARTED, 0, 0);
Wei Jia3a2956d2014-07-22 16:01:33 -0700929 } else if (what == Renderer::kWhatAudioOffloadTearDown) {
930 ALOGV("Tear down audio offload, fall back to s/w path");
931 int64_t positionUs;
932 CHECK(msg->findInt64("positionUs", &positionUs));
Andy Hung282a7e32014-08-14 15:56:34 -0700933 closeAudioSink();
Wei Jia3a2956d2014-07-22 16:01:33 -0700934 mAudioDecoder.clear();
Wei Jia57568df2014-09-22 10:16:29 -0700935 ++mAudioDecoderGeneration;
Wei Jia3a2956d2014-07-22 16:01:33 -0700936 mRenderer->flush(true /* audio */);
937 if (mVideoDecoder != NULL) {
938 mRenderer->flush(false /* audio */);
939 }
940 mRenderer->signalDisableOffloadAudio();
941 mOffloadAudio = false;
942
943 performSeek(positionUs);
944 instantiateDecoder(true /* audio */, &mAudioDecoder);
Andreas Huberf9334412010-12-15 15:17:42 -0800945 }
946 break;
947 }
948
949 case kWhatMoreDataQueued:
950 {
951 break;
952 }
953
Andreas Huber1aef2112011-01-04 14:01:29 -0800954 case kWhatReset:
955 {
Steve Block3856b092011-10-20 11:56:00 +0100956 ALOGV("kWhatReset");
Andreas Huber1aef2112011-01-04 14:01:29 -0800957
Andreas Hubera1f8ab02012-11-30 10:53:22 -0800958 mDeferredActions.push_back(
Andreas Huber14f76722013-01-15 09:04:18 -0800959 new ShutdownDecoderAction(
960 true /* audio */, true /* video */));
Andreas Huberb7c8e912012-11-27 15:02:53 -0800961
Andreas Hubera1f8ab02012-11-30 10:53:22 -0800962 mDeferredActions.push_back(
963 new SimpleAction(&NuPlayer::performReset));
Andreas Huberb58ce9f2011-11-28 16:27:35 -0800964
Andreas Hubera1f8ab02012-11-30 10:53:22 -0800965 processDeferredActions();
Andreas Huber1aef2112011-01-04 14:01:29 -0800966 break;
967 }
968
Andreas Huber43c3e6c2011-01-05 12:17:08 -0800969 case kWhatSeek:
970 {
971 int64_t seekTimeUs;
972 CHECK(msg->findInt64("seekTimeUs", &seekTimeUs));
973
Andreas Hubera1f8ab02012-11-30 10:53:22 -0800974 ALOGV("kWhatSeek seekTimeUs=%lld us", seekTimeUs);
Andreas Huber43c3e6c2011-01-05 12:17:08 -0800975
Andreas Hubera1f8ab02012-11-30 10:53:22 -0800976 mDeferredActions.push_back(
977 new SimpleAction(&NuPlayer::performDecoderFlush));
Andreas Huber43c3e6c2011-01-05 12:17:08 -0800978
Andreas Hubera1f8ab02012-11-30 10:53:22 -0800979 mDeferredActions.push_back(new SeekAction(seekTimeUs));
Andreas Huber43c3e6c2011-01-05 12:17:08 -0800980
Andreas Hubera1f8ab02012-11-30 10:53:22 -0800981 processDeferredActions();
Andreas Huber43c3e6c2011-01-05 12:17:08 -0800982 break;
983 }
984
Andreas Huberb4082222011-01-20 15:23:04 -0800985 case kWhatPause:
986 {
987 CHECK(mRenderer != NULL);
Roger Jönssonfba60da2013-01-21 17:15:45 +0100988 mSource->pause();
Andreas Huberb4082222011-01-20 15:23:04 -0800989 mRenderer->pause();
990 break;
991 }
992
993 case kWhatResume:
994 {
995 CHECK(mRenderer != NULL);
Roger Jönssonfba60da2013-01-21 17:15:45 +0100996 mSource->resume();
Andreas Huberb4082222011-01-20 15:23:04 -0800997 mRenderer->resume();
998 break;
999 }
1000
Andreas Huberb5f25f02013-02-05 10:14:26 -08001001 case kWhatSourceNotify:
1002 {
Andreas Huber9575c962013-02-05 13:59:56 -08001003 onSourceNotify(msg);
Andreas Huberb5f25f02013-02-05 10:14:26 -08001004 break;
1005 }
1006
Chong Zhanga7fa1d92014-06-11 14:49:23 -07001007 case kWhatClosedCaptionNotify:
1008 {
1009 onClosedCaptionNotify(msg);
1010 break;
1011 }
1012
Andreas Huberf9334412010-12-15 15:17:42 -08001013 default:
1014 TRESPASS();
1015 break;
1016 }
1017}
1018
Andreas Huber3831a062010-12-21 10:22:33 -08001019void NuPlayer::finishFlushIfPossible() {
Wei Jia53904f32014-07-29 10:22:53 -07001020 if (mFlushingAudio != NONE && mFlushingAudio != FLUSHED
1021 && mFlushingAudio != SHUT_DOWN) {
Andreas Huber3831a062010-12-21 10:22:33 -08001022 return;
1023 }
1024
Wei Jia53904f32014-07-29 10:22:53 -07001025 if (mFlushingVideo != NONE && mFlushingVideo != FLUSHED
1026 && mFlushingVideo != SHUT_DOWN) {
Andreas Huber3831a062010-12-21 10:22:33 -08001027 return;
1028 }
1029
Steve Block3856b092011-10-20 11:56:00 +01001030 ALOGV("both audio and video are flushed now.");
Andreas Huber3831a062010-12-21 10:22:33 -08001031
Phil Burk9f526492014-09-03 15:04:12 -07001032 mPendingAudioAccessUnit.clear();
Phil Burkc5cc2e22014-09-09 20:08:39 -07001033 mAggregateBuffer.clear();
Phil Burk9f526492014-09-03 15:04:12 -07001034
Andreas Huber6e3d3112011-11-28 12:36:11 -08001035 if (mTimeDiscontinuityPending) {
1036 mRenderer->signalTimeDiscontinuity();
1037 mTimeDiscontinuityPending = false;
1038 }
Andreas Huber3831a062010-12-21 10:22:33 -08001039
Wei Jia53904f32014-07-29 10:22:53 -07001040 if (mAudioDecoder != NULL && mFlushingAudio == FLUSHED) {
Andreas Huber3831a062010-12-21 10:22:33 -08001041 mAudioDecoder->signalResume();
1042 }
1043
Wei Jia53904f32014-07-29 10:22:53 -07001044 if (mVideoDecoder != NULL && mFlushingVideo == FLUSHED) {
Andreas Huber3831a062010-12-21 10:22:33 -08001045 mVideoDecoder->signalResume();
1046 }
1047
1048 mFlushingAudio = NONE;
1049 mFlushingVideo = NONE;
Andreas Huber3831a062010-12-21 10:22:33 -08001050
Andreas Hubera1f8ab02012-11-30 10:53:22 -08001051 processDeferredActions();
Andreas Huber1aef2112011-01-04 14:01:29 -08001052}
1053
1054void NuPlayer::postScanSources() {
1055 if (mScanSourcesPending) {
1056 return;
1057 }
1058
1059 sp<AMessage> msg = new AMessage(kWhatScanSources, id());
1060 msg->setInt32("generation", mScanSourcesGeneration);
1061 msg->post();
1062
1063 mScanSourcesPending = true;
1064}
1065
Andy Hung282a7e32014-08-14 15:56:34 -07001066void NuPlayer::openAudioSink(const sp<AMessage> &format, bool offloadOnly) {
1067 ALOGV("openAudioSink: offloadOnly(%d) mOffloadAudio(%d)",
1068 offloadOnly, mOffloadAudio);
1069 bool audioSinkChanged = false;
1070
1071 int32_t numChannels;
1072 CHECK(format->findInt32("channel-count", &numChannels));
1073
1074 int32_t channelMask;
1075 if (!format->findInt32("channel-mask", &channelMask)) {
1076 // signal to the AudioSink to derive the mask from count.
1077 channelMask = CHANNEL_MASK_USE_CHANNEL_ORDER;
1078 }
1079
1080 int32_t sampleRate;
1081 CHECK(format->findInt32("sample-rate", &sampleRate));
1082
1083 uint32_t flags;
1084 int64_t durationUs;
1085 // FIXME: we should handle the case where the video decoder
1086 // is created after we receive the format change indication.
1087 // Current code will just make that we select deep buffer
1088 // with video which should not be a problem as it should
1089 // not prevent from keeping A/V sync.
1090 if (mVideoDecoder == NULL &&
1091 mSource->getDuration(&durationUs) == OK &&
1092 durationUs
1093 > AUDIO_SINK_MIN_DEEP_BUFFER_DURATION_US) {
1094 flags = AUDIO_OUTPUT_FLAG_DEEP_BUFFER;
1095 } else {
1096 flags = AUDIO_OUTPUT_FLAG_NONE;
1097 }
1098
1099 if (mOffloadAudio) {
1100 audio_format_t audioFormat = AUDIO_FORMAT_PCM_16_BIT;
1101 AString mime;
1102 CHECK(format->findString("mime", &mime));
1103 status_t err = mapMimeToAudioFormat(audioFormat, mime.c_str());
1104
1105 if (err != OK) {
1106 ALOGE("Couldn't map mime \"%s\" to a valid "
1107 "audio_format", mime.c_str());
1108 mOffloadAudio = false;
1109 } else {
1110 ALOGV("Mime \"%s\" mapped to audio_format 0x%x",
1111 mime.c_str(), audioFormat);
1112
1113 int avgBitRate = -1;
1114 format->findInt32("bit-rate", &avgBitRate);
1115
1116 int32_t aacProfile = -1;
1117 if (audioFormat == AUDIO_FORMAT_AAC
1118 && format->findInt32("aac-profile", &aacProfile)) {
1119 // Redefine AAC format as per aac profile
1120 mapAACProfileToAudioFormat(
1121 audioFormat,
1122 aacProfile);
1123 }
1124
1125 audio_offload_info_t offloadInfo = AUDIO_INFO_INITIALIZER;
1126 offloadInfo.duration_us = -1;
1127 format->findInt64(
1128 "durationUs", &offloadInfo.duration_us);
1129 offloadInfo.sample_rate = sampleRate;
1130 offloadInfo.channel_mask = channelMask;
1131 offloadInfo.format = audioFormat;
1132 offloadInfo.stream_type = AUDIO_STREAM_MUSIC;
1133 offloadInfo.bit_rate = avgBitRate;
1134 offloadInfo.has_video = (mVideoDecoder != NULL);
1135 offloadInfo.is_streaming = true;
1136
1137 if (memcmp(&mCurrentOffloadInfo, &offloadInfo, sizeof(offloadInfo)) == 0) {
1138 ALOGV("openAudioSink: no change in offload mode");
1139 return; // no change from previous configuration, everything ok.
1140 }
1141 ALOGV("openAudioSink: try to open AudioSink in offload mode");
1142 flags |= AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD;
Ronghua Wu1ffb5382014-08-18 15:57:03 -07001143 flags &= ~AUDIO_OUTPUT_FLAG_DEEP_BUFFER;
Andy Hung282a7e32014-08-14 15:56:34 -07001144 audioSinkChanged = true;
1145 mAudioSink->close();
1146 err = mAudioSink->open(
1147 sampleRate,
1148 numChannels,
1149 (audio_channel_mask_t)channelMask,
1150 audioFormat,
1151 8 /* bufferCount */,
1152 &NuPlayer::Renderer::AudioSinkCallback,
1153 mRenderer.get(),
1154 (audio_output_flags_t)flags,
1155 &offloadInfo);
1156
1157 if (err == OK) {
1158 // If the playback is offloaded to h/w, we pass
1159 // the HAL some metadata information.
1160 // We don't want to do this for PCM because it
1161 // will be going through the AudioFlinger mixer
1162 // before reaching the hardware.
1163 sp<MetaData> audioMeta =
1164 mSource->getFormatMeta(true /* audio */);
1165 sendMetaDataToHal(mAudioSink, audioMeta);
1166 mCurrentOffloadInfo = offloadInfo;
1167 err = mAudioSink->start();
1168 ALOGV_IF(err == OK, "openAudioSink: offload succeeded");
1169 }
1170 if (err != OK) {
1171 // Clean up, fall back to non offload mode.
1172 mAudioSink->close();
1173 mRenderer->signalDisableOffloadAudio();
1174 mOffloadAudio = false;
1175 mCurrentOffloadInfo = AUDIO_INFO_INITIALIZER;
1176 ALOGV("openAudioSink: offload failed");
1177 }
1178 }
1179 }
1180 if (!offloadOnly && !mOffloadAudio) {
1181 flags &= ~AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD;
1182 ALOGV("openAudioSink: open AudioSink in NON-offload mode");
1183
1184 audioSinkChanged = true;
1185 mAudioSink->close();
1186 mCurrentOffloadInfo = AUDIO_INFO_INITIALIZER;
1187 CHECK_EQ(mAudioSink->open(
1188 sampleRate,
1189 numChannels,
1190 (audio_channel_mask_t)channelMask,
1191 AUDIO_FORMAT_PCM_16_BIT,
1192 8 /* bufferCount */,
1193 NULL,
1194 NULL,
1195 (audio_output_flags_t)flags),
1196 (status_t)OK);
1197 mAudioSink->start();
1198 }
1199 if (audioSinkChanged) {
1200 mRenderer->signalAudioSinkChanged();
1201 }
1202}
1203
1204void NuPlayer::closeAudioSink() {
1205 mAudioSink->close();
1206 mCurrentOffloadInfo = AUDIO_INFO_INITIALIZER;
1207}
1208
Andreas Huber5bc087c2010-12-23 10:27:40 -08001209status_t NuPlayer::instantiateDecoder(bool audio, sp<Decoder> *decoder) {
Andreas Huberf9334412010-12-15 15:17:42 -08001210 if (*decoder != NULL) {
1211 return OK;
1212 }
1213
Andreas Huber84066782011-08-16 09:34:26 -07001214 sp<AMessage> format = mSource->getFormat(audio);
Andreas Huberf9334412010-12-15 15:17:42 -08001215
Andreas Huber84066782011-08-16 09:34:26 -07001216 if (format == NULL) {
Andreas Huberf9334412010-12-15 15:17:42 -08001217 return -EWOULDBLOCK;
1218 }
1219
Andreas Huber3fe62152011-09-16 15:09:22 -07001220 if (!audio) {
Andreas Huber84066782011-08-16 09:34:26 -07001221 AString mime;
1222 CHECK(format->findString("mime", &mime));
1223 mVideoIsAVC = !strcasecmp(MEDIA_MIMETYPE_VIDEO_AVC, mime.c_str());
Chong Zhanga7fa1d92014-06-11 14:49:23 -07001224
1225 sp<AMessage> ccNotify = new AMessage(kWhatClosedCaptionNotify, id());
1226 mCCDecoder = new CCDecoder(ccNotify);
Lajos Molnar09524832014-07-17 14:29:51 -07001227
1228 if (mSourceFlags & Source::FLAG_SECURE) {
1229 format->setInt32("secure", true);
1230 }
Andreas Huber3fe62152011-09-16 15:09:22 -07001231 }
1232
Wei Jiabc2fb722014-07-08 16:37:57 -07001233 if (audio) {
Wei Jia88703c32014-08-06 11:24:07 -07001234 sp<AMessage> notify = new AMessage(kWhatAudioNotify, id());
1235 ++mAudioDecoderGeneration;
1236 notify->setInt32("generation", mAudioDecoderGeneration);
1237
Wei Jiabc2fb722014-07-08 16:37:57 -07001238 if (mOffloadAudio) {
1239 *decoder = new DecoderPassThrough(notify);
1240 } else {
1241 *decoder = new Decoder(notify);
1242 }
1243 } else {
Wei Jia88703c32014-08-06 11:24:07 -07001244 sp<AMessage> notify = new AMessage(kWhatVideoNotify, id());
1245 ++mVideoDecoderGeneration;
1246 notify->setInt32("generation", mVideoDecoderGeneration);
1247
Wei Jiabc2fb722014-07-08 16:37:57 -07001248 *decoder = new Decoder(notify, mNativeWindow);
1249 }
Lajos Molnar1cd13982014-01-17 15:12:51 -08001250 (*decoder)->init();
Andreas Huber84066782011-08-16 09:34:26 -07001251 (*decoder)->configure(format);
Andreas Huberf9334412010-12-15 15:17:42 -08001252
Lajos Molnar09524832014-07-17 14:29:51 -07001253 // allocate buffers to decrypt widevine source buffers
1254 if (!audio && (mSourceFlags & Source::FLAG_SECURE)) {
1255 Vector<sp<ABuffer> > inputBufs;
1256 CHECK_EQ((*decoder)->getInputBuffers(&inputBufs), (status_t)OK);
1257
1258 Vector<MediaBuffer *> mediaBufs;
1259 for (size_t i = 0; i < inputBufs.size(); i++) {
1260 const sp<ABuffer> &buffer = inputBufs[i];
1261 MediaBuffer *mbuf = new MediaBuffer(buffer->data(), buffer->size());
1262 mediaBufs.push(mbuf);
1263 }
1264
1265 status_t err = mSource->setBuffers(audio, mediaBufs);
1266 if (err != OK) {
1267 for (size_t i = 0; i < mediaBufs.size(); ++i) {
1268 mediaBufs[i]->release();
1269 }
1270 mediaBufs.clear();
1271 ALOGE("Secure source didn't support secure mediaBufs.");
1272 return err;
1273 }
1274 }
Andreas Huberf9334412010-12-15 15:17:42 -08001275 return OK;
1276}
1277
1278status_t NuPlayer::feedDecoderInputData(bool audio, const sp<AMessage> &msg) {
1279 sp<AMessage> reply;
1280 CHECK(msg->findMessage("reply", &reply));
1281
Wei Jia53904f32014-07-29 10:22:53 -07001282 if ((audio && mFlushingAudio != NONE)
Wei Jiaf702d042014-09-09 12:08:47 -07001283 || (!audio && mFlushingVideo != NONE)
1284 || mSource == NULL) {
Wei Jiab189a5b2014-08-07 06:11:39 +00001285 reply->setInt32("err", INFO_DISCONTINUITY);
1286 reply->post();
1287 return OK;
Andreas Huberf9334412010-12-15 15:17:42 -08001288 }
1289
1290 sp<ABuffer> accessUnit;
Andreas Huberf9334412010-12-15 15:17:42 -08001291
Phil Burk9f526492014-09-03 15:04:12 -07001292 // Aggregate smaller buffers into a larger buffer.
1293 // The goal is to reduce power consumption.
Phil Burk33b51b02014-09-17 16:03:47 -07001294 // Note this will not work if the decoder requires one frame per buffer.
1295 bool doBufferAggregation = (audio && mOffloadAudio);
Phil Burk9f526492014-09-03 15:04:12 -07001296 bool needMoreData = false;
Phil Burk9f526492014-09-03 15:04:12 -07001297
Andreas Huber3fe62152011-09-16 15:09:22 -07001298 bool dropAccessUnit;
1299 do {
Phil Burk9f526492014-09-03 15:04:12 -07001300 status_t err;
1301 // Did we save an accessUnit earlier because of a discontinuity?
1302 if (audio && (mPendingAudioAccessUnit != NULL)) {
1303 accessUnit = mPendingAudioAccessUnit;
1304 mPendingAudioAccessUnit.clear();
1305 err = mPendingAudioErr;
1306 ALOGV("feedDecoderInputData() use mPendingAudioAccessUnit");
1307 } else {
1308 err = mSource->dequeueAccessUnit(audio, &accessUnit);
1309 }
Andreas Huber5bc087c2010-12-23 10:27:40 -08001310
Andreas Huber3fe62152011-09-16 15:09:22 -07001311 if (err == -EWOULDBLOCK) {
Phil Burkc5cc2e22014-09-09 20:08:39 -07001312 return err;
Andreas Huber3fe62152011-09-16 15:09:22 -07001313 } else if (err != OK) {
1314 if (err == INFO_DISCONTINUITY) {
Phil Burk33b51b02014-09-17 16:03:47 -07001315 if (doBufferAggregation && (mAggregateBuffer != NULL)) {
Phil Burk9f526492014-09-03 15:04:12 -07001316 // We already have some data so save this for later.
1317 mPendingAudioErr = err;
1318 mPendingAudioAccessUnit = accessUnit;
1319 accessUnit.clear();
1320 ALOGD("feedDecoderInputData() save discontinuity for later");
1321 break;
1322 }
Andreas Huber3fe62152011-09-16 15:09:22 -07001323 int32_t type;
1324 CHECK(accessUnit->meta()->findInt32("discontinuity", &type));
Andreas Huber53df1a42010-12-22 10:03:04 -08001325
Andreas Huber3fe62152011-09-16 15:09:22 -07001326 bool formatChange =
Andreas Huber6e3d3112011-11-28 12:36:11 -08001327 (audio &&
1328 (type & ATSParser::DISCONTINUITY_AUDIO_FORMAT))
1329 || (!audio &&
1330 (type & ATSParser::DISCONTINUITY_VIDEO_FORMAT));
Andreas Huber53df1a42010-12-22 10:03:04 -08001331
Andreas Huber6e3d3112011-11-28 12:36:11 -08001332 bool timeChange = (type & ATSParser::DISCONTINUITY_TIME) != 0;
1333
Steve Blockdf64d152012-01-04 20:05:49 +00001334 ALOGI("%s discontinuity (formatChange=%d, time=%d)",
Andreas Huber6e3d3112011-11-28 12:36:11 -08001335 audio ? "audio" : "video", formatChange, timeChange);
Andreas Huber32f3cef2011-03-02 15:34:46 -08001336
Andreas Huber3fe62152011-09-16 15:09:22 -07001337 if (audio) {
1338 mSkipRenderingAudioUntilMediaTimeUs = -1;
1339 } else {
1340 mSkipRenderingVideoUntilMediaTimeUs = -1;
1341 }
Andreas Huber32f3cef2011-03-02 15:34:46 -08001342
Andreas Huber6e3d3112011-11-28 12:36:11 -08001343 if (timeChange) {
1344 sp<AMessage> extra;
1345 if (accessUnit->meta()->findMessage("extra", &extra)
1346 && extra != NULL) {
1347 int64_t resumeAtMediaTimeUs;
1348 if (extra->findInt64(
1349 "resume-at-mediatimeUs", &resumeAtMediaTimeUs)) {
Steve Blockdf64d152012-01-04 20:05:49 +00001350 ALOGI("suppressing rendering of %s until %lld us",
Andreas Huber6e3d3112011-11-28 12:36:11 -08001351 audio ? "audio" : "video", resumeAtMediaTimeUs);
Andreas Huber3fe62152011-09-16 15:09:22 -07001352
Andreas Huber6e3d3112011-11-28 12:36:11 -08001353 if (audio) {
1354 mSkipRenderingAudioUntilMediaTimeUs =
1355 resumeAtMediaTimeUs;
1356 } else {
1357 mSkipRenderingVideoUntilMediaTimeUs =
1358 resumeAtMediaTimeUs;
1359 }
Andreas Huber3fe62152011-09-16 15:09:22 -07001360 }
Andreas Huber32f3cef2011-03-02 15:34:46 -08001361 }
1362 }
Andreas Huber3fe62152011-09-16 15:09:22 -07001363
Andreas Huber6e3d3112011-11-28 12:36:11 -08001364 mTimeDiscontinuityPending =
1365 mTimeDiscontinuityPending || timeChange;
1366
Lajos Molnar87603c02014-08-20 19:25:30 -07001367 bool seamlessFormatChange = false;
1368 sp<AMessage> newFormat = mSource->getFormat(audio);
1369 if (formatChange) {
1370 seamlessFormatChange =
1371 getDecoder(audio)->supportsSeamlessFormatChange(newFormat);
1372 // treat seamless format change separately
1373 formatChange = !seamlessFormatChange;
1374 }
1375 bool shutdownOrFlush = formatChange || timeChange;
1376
1377 // We want to queue up scan-sources only once per discontinuity.
1378 // We control this by doing it only if neither audio nor video are
1379 // flushing or shutting down. (After handling 1st discontinuity, one
1380 // of the flushing states will not be NONE.)
1381 // No need to scan sources if this discontinuity does not result
1382 // in a flush or shutdown, as the flushing state will stay NONE.
1383 if (mFlushingAudio == NONE && mFlushingVideo == NONE &&
1384 shutdownOrFlush) {
Robert Shiha2981012014-07-30 17:41:24 -07001385 // And we'll resume scanning sources once we're done
1386 // flushing.
1387 mDeferredActions.push_front(
1388 new SimpleAction(
1389 &NuPlayer::performScanSources));
1390 }
1391
Lajos Molnar87603c02014-08-20 19:25:30 -07001392 if (formatChange /* not seamless */) {
1393 // must change decoder
1394 flushDecoder(audio, /* needShutdown = */ true);
1395 } else if (timeChange) {
1396 // need to flush
1397 flushDecoder(audio, /* needShutdown = */ false, newFormat);
1398 err = OK;
1399 } else if (seamlessFormatChange) {
1400 // reuse existing decoder and don't flush
1401 updateDecoderFormatWithoutFlush(audio, newFormat);
1402 err = OK;
Andreas Huber6e3d3112011-11-28 12:36:11 -08001403 } else {
1404 // This stream is unaffected by the discontinuity
Andreas Huber6e3d3112011-11-28 12:36:11 -08001405 return -EWOULDBLOCK;
1406 }
Andreas Huber32f3cef2011-03-02 15:34:46 -08001407 }
1408
Andreas Huber3fe62152011-09-16 15:09:22 -07001409 reply->setInt32("err", err);
1410 reply->post();
1411 return OK;
Andreas Huberf9334412010-12-15 15:17:42 -08001412 }
1413
Andreas Huber3fe62152011-09-16 15:09:22 -07001414 if (!audio) {
1415 ++mNumFramesTotal;
1416 }
1417
1418 dropAccessUnit = false;
1419 if (!audio
Lajos Molnar09524832014-07-17 14:29:51 -07001420 && !(mSourceFlags & Source::FLAG_SECURE)
Andreas Huber3fe62152011-09-16 15:09:22 -07001421 && mVideoLateByUs > 100000ll
1422 && mVideoIsAVC
1423 && !IsAVCReferenceFrame(accessUnit)) {
1424 dropAccessUnit = true;
1425 ++mNumFramesDropped;
1426 }
Phil Burk9f526492014-09-03 15:04:12 -07001427
1428 size_t smallSize = accessUnit->size();
1429 needMoreData = false;
Phil Burkc5cc2e22014-09-09 20:08:39 -07001430 if (doBufferAggregation && (mAggregateBuffer == NULL)
Phil Burk9f526492014-09-03 15:04:12 -07001431 // Don't bother if only room for a few small buffers.
Phil Burkc5cc2e22014-09-09 20:08:39 -07001432 && (smallSize < (kAggregateBufferSizeBytes / 3))) {
Phil Burk9f526492014-09-03 15:04:12 -07001433 // Create a larger buffer for combining smaller buffers from the extractor.
Phil Burkc5cc2e22014-09-09 20:08:39 -07001434 mAggregateBuffer = new ABuffer(kAggregateBufferSizeBytes);
1435 mAggregateBuffer->setRange(0, 0); // start empty
Phil Burk9f526492014-09-03 15:04:12 -07001436 }
1437
Phil Burk33b51b02014-09-17 16:03:47 -07001438 if (doBufferAggregation && (mAggregateBuffer != NULL)) {
Phil Burk9f526492014-09-03 15:04:12 -07001439 int64_t timeUs;
Phil Burkc5cc2e22014-09-09 20:08:39 -07001440 int64_t dummy;
Phil Burk9f526492014-09-03 15:04:12 -07001441 bool smallTimestampValid = accessUnit->meta()->findInt64("timeUs", &timeUs);
Phil Burkc5cc2e22014-09-09 20:08:39 -07001442 bool bigTimestampValid = mAggregateBuffer->meta()->findInt64("timeUs", &dummy);
Phil Burk9f526492014-09-03 15:04:12 -07001443 // Will the smaller buffer fit?
Phil Burkc5cc2e22014-09-09 20:08:39 -07001444 size_t bigSize = mAggregateBuffer->size();
1445 size_t roomLeft = mAggregateBuffer->capacity() - bigSize;
Phil Burk9f526492014-09-03 15:04:12 -07001446 // Should we save this small buffer for the next big buffer?
1447 // If the first small buffer did not have a timestamp then save
1448 // any buffer that does have a timestamp until the next big buffer.
1449 if ((smallSize > roomLeft)
Phil Burkc5cc2e22014-09-09 20:08:39 -07001450 || (!bigTimestampValid && (bigSize > 0) && smallTimestampValid)) {
Phil Burk9f526492014-09-03 15:04:12 -07001451 mPendingAudioErr = err;
1452 mPendingAudioAccessUnit = accessUnit;
1453 accessUnit.clear();
1454 } else {
Phil Burkc5cc2e22014-09-09 20:08:39 -07001455 // Grab time from first small buffer if available.
1456 if ((bigSize == 0) && smallTimestampValid) {
1457 mAggregateBuffer->meta()->setInt64("timeUs", timeUs);
1458 }
Phil Burk9f526492014-09-03 15:04:12 -07001459 // Append small buffer to the bigger buffer.
Phil Burkc5cc2e22014-09-09 20:08:39 -07001460 memcpy(mAggregateBuffer->base() + bigSize, accessUnit->data(), smallSize);
Phil Burk9f526492014-09-03 15:04:12 -07001461 bigSize += smallSize;
Phil Burkc5cc2e22014-09-09 20:08:39 -07001462 mAggregateBuffer->setRange(0, bigSize);
Phil Burk9f526492014-09-03 15:04:12 -07001463
Phil Burkc5cc2e22014-09-09 20:08:39 -07001464 // Keep looping until we run out of room in the mAggregateBuffer.
Phil Burk9f526492014-09-03 15:04:12 -07001465 needMoreData = true;
1466
Phil Burkc5cc2e22014-09-09 20:08:39 -07001467 ALOGV("feedDecoderInputData() smallSize = %zu, bigSize = %zu, capacity = %zu",
1468 smallSize, bigSize, mAggregateBuffer->capacity());
Phil Burk9f526492014-09-03 15:04:12 -07001469 }
1470 }
1471 } while (dropAccessUnit || needMoreData);
Andreas Huberf9334412010-12-15 15:17:42 -08001472
Steve Block3856b092011-10-20 11:56:00 +01001473 // ALOGV("returned a valid buffer of %s data", audio ? "audio" : "video");
Andreas Huberf9334412010-12-15 15:17:42 -08001474
1475#if 0
1476 int64_t mediaTimeUs;
1477 CHECK(accessUnit->meta()->findInt64("timeUs", &mediaTimeUs));
Steve Block3856b092011-10-20 11:56:00 +01001478 ALOGV("feeding %s input buffer at media time %.2f secs",
Andreas Huberf9334412010-12-15 15:17:42 -08001479 audio ? "audio" : "video",
1480 mediaTimeUs / 1E6);
1481#endif
1482
Chong Zhanga7fa1d92014-06-11 14:49:23 -07001483 if (!audio) {
1484 mCCDecoder->decode(accessUnit);
1485 }
1486
Phil Burk33b51b02014-09-17 16:03:47 -07001487 if (doBufferAggregation && (mAggregateBuffer != NULL)) {
Phil Burkc5cc2e22014-09-09 20:08:39 -07001488 ALOGV("feedDecoderInputData() reply with aggregated buffer, %zu",
1489 mAggregateBuffer->size());
1490 reply->setBuffer("buffer", mAggregateBuffer);
1491 mAggregateBuffer.clear();
Phil Burk9f526492014-09-03 15:04:12 -07001492 } else {
1493 reply->setBuffer("buffer", accessUnit);
1494 }
1495
Andreas Huberf9334412010-12-15 15:17:42 -08001496 reply->post();
1497
1498 return OK;
1499}
1500
1501void NuPlayer::renderBuffer(bool audio, const sp<AMessage> &msg) {
Steve Block3856b092011-10-20 11:56:00 +01001502 // ALOGV("renderBuffer %s", audio ? "audio" : "video");
Andreas Huberf9334412010-12-15 15:17:42 -08001503
1504 sp<AMessage> reply;
1505 CHECK(msg->findMessage("reply", &reply));
1506
Wei Jia53904f32014-07-29 10:22:53 -07001507 if ((audio && mFlushingAudio != NONE)
1508 || (!audio && mFlushingVideo != NONE)) {
Andreas Huber18ac5402011-08-31 15:04:25 -07001509 // We're currently attempting to flush the decoder, in order
1510 // to complete this, the decoder wants all its buffers back,
1511 // so we don't want any output buffers it sent us (from before
1512 // we initiated the flush) to be stuck in the renderer's queue.
1513
Steve Block3856b092011-10-20 11:56:00 +01001514 ALOGV("we're still flushing the %s decoder, sending its output buffer"
Andreas Huber18ac5402011-08-31 15:04:25 -07001515 " right back.", audio ? "audio" : "video");
1516
1517 reply->post();
1518 return;
1519 }
1520
Andreas Huber2d8bedd2012-02-21 14:38:23 -08001521 sp<ABuffer> buffer;
1522 CHECK(msg->findBuffer("buffer", &buffer));
Andreas Huberf9334412010-12-15 15:17:42 -08001523
Chong Zhanga7fa1d92014-06-11 14:49:23 -07001524 int64_t mediaTimeUs;
1525 CHECK(buffer->meta()->findInt64("timeUs", &mediaTimeUs));
1526
Andreas Huber32f3cef2011-03-02 15:34:46 -08001527 int64_t &skipUntilMediaTimeUs =
1528 audio
1529 ? mSkipRenderingAudioUntilMediaTimeUs
1530 : mSkipRenderingVideoUntilMediaTimeUs;
1531
1532 if (skipUntilMediaTimeUs >= 0) {
Andreas Huber32f3cef2011-03-02 15:34:46 -08001533
1534 if (mediaTimeUs < skipUntilMediaTimeUs) {
Steve Block3856b092011-10-20 11:56:00 +01001535 ALOGV("dropping %s buffer at time %lld as requested.",
Andreas Huber32f3cef2011-03-02 15:34:46 -08001536 audio ? "audio" : "video",
1537 mediaTimeUs);
1538
1539 reply->post();
1540 return;
1541 }
1542
1543 skipUntilMediaTimeUs = -1;
1544 }
1545
Chong Zhanga7fa1d92014-06-11 14:49:23 -07001546 if (!audio && mCCDecoder->isSelected()) {
1547 mCCDecoder->display(mediaTimeUs);
1548 }
1549
Andreas Huberf9334412010-12-15 15:17:42 -08001550 mRenderer->queueBuffer(audio, buffer, reply);
1551}
1552
Chong Zhangced1c2f2014-08-08 15:22:35 -07001553void NuPlayer::updateVideoSize(
1554 const sp<AMessage> &inputFormat,
1555 const sp<AMessage> &outputFormat) {
1556 if (inputFormat == NULL) {
1557 ALOGW("Unknown video size, reporting 0x0!");
1558 notifyListener(MEDIA_SET_VIDEO_SIZE, 0, 0);
1559 return;
1560 }
1561
1562 int32_t displayWidth, displayHeight;
1563 int32_t cropLeft, cropTop, cropRight, cropBottom;
1564
1565 if (outputFormat != NULL) {
1566 int32_t width, height;
1567 CHECK(outputFormat->findInt32("width", &width));
1568 CHECK(outputFormat->findInt32("height", &height));
1569
1570 int32_t cropLeft, cropTop, cropRight, cropBottom;
1571 CHECK(outputFormat->findRect(
1572 "crop",
1573 &cropLeft, &cropTop, &cropRight, &cropBottom));
1574
1575 displayWidth = cropRight - cropLeft + 1;
1576 displayHeight = cropBottom - cropTop + 1;
1577
1578 ALOGV("Video output format changed to %d x %d "
1579 "(crop: %d x %d @ (%d, %d))",
1580 width, height,
1581 displayWidth,
1582 displayHeight,
1583 cropLeft, cropTop);
1584 } else {
1585 CHECK(inputFormat->findInt32("width", &displayWidth));
1586 CHECK(inputFormat->findInt32("height", &displayHeight));
1587
1588 ALOGV("Video input format %d x %d", displayWidth, displayHeight);
1589 }
1590
1591 // Take into account sample aspect ratio if necessary:
1592 int32_t sarWidth, sarHeight;
1593 if (inputFormat->findInt32("sar-width", &sarWidth)
1594 && inputFormat->findInt32("sar-height", &sarHeight)) {
1595 ALOGV("Sample aspect ratio %d : %d", sarWidth, sarHeight);
1596
1597 displayWidth = (displayWidth * sarWidth) / sarHeight;
1598
1599 ALOGV("display dimensions %d x %d", displayWidth, displayHeight);
1600 }
1601
1602 int32_t rotationDegrees;
1603 if (!inputFormat->findInt32("rotation-degrees", &rotationDegrees)) {
1604 rotationDegrees = 0;
1605 }
1606
1607 if (rotationDegrees == 90 || rotationDegrees == 270) {
1608 int32_t tmp = displayWidth;
1609 displayWidth = displayHeight;
1610 displayHeight = tmp;
1611 }
1612
1613 notifyListener(
1614 MEDIA_SET_VIDEO_SIZE,
1615 displayWidth,
1616 displayHeight);
1617}
1618
Chong Zhangdcb89b32013-08-06 09:44:47 -07001619void NuPlayer::notifyListener(int msg, int ext1, int ext2, const Parcel *in) {
Andreas Huber43c3e6c2011-01-05 12:17:08 -08001620 if (mDriver == NULL) {
Andreas Huberf9334412010-12-15 15:17:42 -08001621 return;
1622 }
1623
Andreas Huber43c3e6c2011-01-05 12:17:08 -08001624 sp<NuPlayerDriver> driver = mDriver.promote();
Andreas Huberf9334412010-12-15 15:17:42 -08001625
Andreas Huber43c3e6c2011-01-05 12:17:08 -08001626 if (driver == NULL) {
Andreas Huberf9334412010-12-15 15:17:42 -08001627 return;
1628 }
1629
Chong Zhangdcb89b32013-08-06 09:44:47 -07001630 driver->notifyListener(msg, ext1, ext2, in);
Andreas Huberf9334412010-12-15 15:17:42 -08001631}
1632
Lajos Molnar87603c02014-08-20 19:25:30 -07001633void NuPlayer::flushDecoder(
1634 bool audio, bool needShutdown, const sp<AMessage> &newFormat) {
Andreas Huber14f76722013-01-15 09:04:18 -08001635 ALOGV("[%s] flushDecoder needShutdown=%d",
1636 audio ? "audio" : "video", needShutdown);
1637
Lajos Molnar87603c02014-08-20 19:25:30 -07001638 const sp<Decoder> &decoder = getDecoder(audio);
1639 if (decoder == NULL) {
Steve Blockdf64d152012-01-04 20:05:49 +00001640 ALOGI("flushDecoder %s without decoder present",
Andreas Huber6e3d3112011-11-28 12:36:11 -08001641 audio ? "audio" : "video");
Lajos Molnar87603c02014-08-20 19:25:30 -07001642 return;
Andreas Huber6e3d3112011-11-28 12:36:11 -08001643 }
1644
Andreas Huber1aef2112011-01-04 14:01:29 -08001645 // Make sure we don't continue to scan sources until we finish flushing.
1646 ++mScanSourcesGeneration;
Andreas Huber43c3e6c2011-01-05 12:17:08 -08001647 mScanSourcesPending = false;
Andreas Huber1aef2112011-01-04 14:01:29 -08001648
Lajos Molnar87603c02014-08-20 19:25:30 -07001649 decoder->signalFlush(newFormat);
Andreas Huber1aef2112011-01-04 14:01:29 -08001650 mRenderer->flush(audio);
1651
1652 FlushStatus newStatus =
1653 needShutdown ? FLUSHING_DECODER_SHUTDOWN : FLUSHING_DECODER;
1654
1655 if (audio) {
Wei Jia53904f32014-07-29 10:22:53 -07001656 ALOGE_IF(mFlushingAudio != NONE,
1657 "audio flushDecoder() is called in state %d", mFlushingAudio);
Andreas Huber1aef2112011-01-04 14:01:29 -08001658 mFlushingAudio = newStatus;
Andreas Huber1aef2112011-01-04 14:01:29 -08001659 } else {
Wei Jia53904f32014-07-29 10:22:53 -07001660 ALOGE_IF(mFlushingVideo != NONE,
1661 "video flushDecoder() is called in state %d", mFlushingVideo);
Andreas Huber1aef2112011-01-04 14:01:29 -08001662 mFlushingVideo = newStatus;
Chong Zhangb86e68f2014-08-01 13:46:53 -07001663
1664 if (mCCDecoder != NULL) {
1665 mCCDecoder->flush();
1666 }
Andreas Huber1aef2112011-01-04 14:01:29 -08001667 }
1668}
1669
Lajos Molnar87603c02014-08-20 19:25:30 -07001670void NuPlayer::updateDecoderFormatWithoutFlush(
1671 bool audio, const sp<AMessage> &format) {
1672 ALOGV("[%s] updateDecoderFormatWithoutFlush", audio ? "audio" : "video");
1673
1674 const sp<Decoder> &decoder = getDecoder(audio);
1675 if (decoder == NULL) {
1676 ALOGI("updateDecoderFormatWithoutFlush %s without decoder present",
1677 audio ? "audio" : "video");
1678 return;
1679 }
1680
1681 decoder->signalUpdateFormat(format);
1682}
1683
Chong Zhangced1c2f2014-08-08 15:22:35 -07001684void NuPlayer::queueDecoderShutdown(
1685 bool audio, bool video, const sp<AMessage> &reply) {
1686 ALOGI("queueDecoderShutdown audio=%d, video=%d", audio, video);
Andreas Huber84066782011-08-16 09:34:26 -07001687
Chong Zhangced1c2f2014-08-08 15:22:35 -07001688 mDeferredActions.push_back(
1689 new ShutdownDecoderAction(audio, video));
Andreas Huber84066782011-08-16 09:34:26 -07001690
Chong Zhangced1c2f2014-08-08 15:22:35 -07001691 mDeferredActions.push_back(
1692 new SimpleAction(&NuPlayer::performScanSources));
Andreas Huber84066782011-08-16 09:34:26 -07001693
Chong Zhangced1c2f2014-08-08 15:22:35 -07001694 mDeferredActions.push_back(new PostMessageAction(reply));
1695
1696 processDeferredActions();
Andreas Huber84066782011-08-16 09:34:26 -07001697}
1698
James Dong0d268a32012-08-31 12:18:27 -07001699status_t NuPlayer::setVideoScalingMode(int32_t mode) {
1700 mVideoScalingMode = mode;
Andreas Huber57a339c2012-12-03 11:18:00 -08001701 if (mNativeWindow != NULL) {
James Dong0d268a32012-08-31 12:18:27 -07001702 status_t ret = native_window_set_scaling_mode(
1703 mNativeWindow->getNativeWindow().get(), mVideoScalingMode);
1704 if (ret != OK) {
1705 ALOGE("Failed to set scaling mode (%d): %s",
1706 -ret, strerror(-ret));
1707 return ret;
1708 }
1709 }
1710 return OK;
1711}
1712
Chong Zhangdcb89b32013-08-06 09:44:47 -07001713status_t NuPlayer::getTrackInfo(Parcel* reply) const {
1714 sp<AMessage> msg = new AMessage(kWhatGetTrackInfo, id());
1715 msg->setPointer("reply", reply);
1716
1717 sp<AMessage> response;
1718 status_t err = msg->postAndAwaitResponse(&response);
1719 return err;
1720}
1721
Robert Shih7c4f0d72014-07-09 18:53:31 -07001722status_t NuPlayer::getSelectedTrack(int32_t type, Parcel* reply) const {
1723 sp<AMessage> msg = new AMessage(kWhatGetSelectedTrack, id());
1724 msg->setPointer("reply", reply);
1725 msg->setInt32("type", type);
1726
1727 sp<AMessage> response;
1728 status_t err = msg->postAndAwaitResponse(&response);
1729 if (err == OK && response != NULL) {
1730 CHECK(response->findInt32("err", &err));
1731 }
1732 return err;
1733}
1734
Chong Zhangdcb89b32013-08-06 09:44:47 -07001735status_t NuPlayer::selectTrack(size_t trackIndex, bool select) {
1736 sp<AMessage> msg = new AMessage(kWhatSelectTrack, id());
1737 msg->setSize("trackIndex", trackIndex);
1738 msg->setInt32("select", select);
1739
1740 sp<AMessage> response;
1741 status_t err = msg->postAndAwaitResponse(&response);
1742
Chong Zhang404fced2014-06-11 14:45:31 -07001743 if (err != OK) {
1744 return err;
1745 }
1746
1747 if (!response->findInt32("err", &err)) {
1748 err = OK;
1749 }
1750
Chong Zhangdcb89b32013-08-06 09:44:47 -07001751 return err;
1752}
1753
Marco Nelissenf0b72b52014-09-16 15:43:44 -07001754sp<MetaData> NuPlayer::getFileMeta() {
1755 return mSource->getFileFormatMeta();
1756}
1757
Andreas Huberb7c8e912012-11-27 15:02:53 -08001758void NuPlayer::schedulePollDuration() {
1759 sp<AMessage> msg = new AMessage(kWhatPollDuration, id());
1760 msg->setInt32("generation", mPollDurationGeneration);
1761 msg->post();
1762}
1763
1764void NuPlayer::cancelPollDuration() {
1765 ++mPollDurationGeneration;
1766}
1767
Andreas Hubera1f8ab02012-11-30 10:53:22 -08001768void NuPlayer::processDeferredActions() {
1769 while (!mDeferredActions.empty()) {
1770 // We won't execute any deferred actions until we're no longer in
1771 // an intermediate state, i.e. one more more decoders are currently
1772 // flushing or shutting down.
1773
Andreas Hubera1f8ab02012-11-30 10:53:22 -08001774 if (mFlushingAudio != NONE || mFlushingVideo != NONE) {
1775 // We're currently flushing, postpone the reset until that's
1776 // completed.
1777
1778 ALOGV("postponing action mFlushingAudio=%d, mFlushingVideo=%d",
1779 mFlushingAudio, mFlushingVideo);
1780
1781 break;
1782 }
1783
1784 sp<Action> action = *mDeferredActions.begin();
1785 mDeferredActions.erase(mDeferredActions.begin());
1786
1787 action->execute(this);
1788 }
1789}
1790
1791void NuPlayer::performSeek(int64_t seekTimeUs) {
1792 ALOGV("performSeek seekTimeUs=%lld us (%.2f secs)",
1793 seekTimeUs,
1794 seekTimeUs / 1E6);
1795
Andy Hungadf34bf2014-09-03 18:22:22 -07001796 if (mSource == NULL) {
1797 // This happens when reset occurs right before the loop mode
1798 // asynchronously seeks to the start of the stream.
1799 LOG_ALWAYS_FATAL_IF(mAudioDecoder != NULL || mVideoDecoder != NULL,
1800 "mSource is NULL and decoders not NULL audio(%p) video(%p)",
1801 mAudioDecoder.get(), mVideoDecoder.get());
1802 return;
1803 }
Andreas Hubera1f8ab02012-11-30 10:53:22 -08001804 mSource->seekTo(seekTimeUs);
Robert Shihd3b0bbb2014-07-23 15:00:25 -07001805 ++mTimedTextGeneration;
Andreas Hubera1f8ab02012-11-30 10:53:22 -08001806
1807 if (mDriver != NULL) {
1808 sp<NuPlayerDriver> driver = mDriver.promote();
1809 if (driver != NULL) {
1810 driver->notifyPosition(seekTimeUs);
1811 driver->notifySeekComplete();
1812 }
1813 }
1814
1815 // everything's flushed, continue playback.
1816}
1817
1818void NuPlayer::performDecoderFlush() {
1819 ALOGV("performDecoderFlush");
1820
Andreas Huberda9740e2013-04-16 10:54:03 -07001821 if (mAudioDecoder == NULL && mVideoDecoder == NULL) {
Andreas Hubera1f8ab02012-11-30 10:53:22 -08001822 return;
1823 }
1824
1825 mTimeDiscontinuityPending = true;
1826
1827 if (mAudioDecoder != NULL) {
1828 flushDecoder(true /* audio */, false /* needShutdown */);
1829 }
1830
1831 if (mVideoDecoder != NULL) {
1832 flushDecoder(false /* audio */, false /* needShutdown */);
1833 }
1834}
1835
Andreas Huber14f76722013-01-15 09:04:18 -08001836void NuPlayer::performDecoderShutdown(bool audio, bool video) {
1837 ALOGV("performDecoderShutdown audio=%d, video=%d", audio, video);
Andreas Hubera1f8ab02012-11-30 10:53:22 -08001838
Andreas Huber14f76722013-01-15 09:04:18 -08001839 if ((!audio || mAudioDecoder == NULL)
1840 && (!video || mVideoDecoder == NULL)) {
Andreas Hubera1f8ab02012-11-30 10:53:22 -08001841 return;
1842 }
1843
1844 mTimeDiscontinuityPending = true;
1845
Andreas Huber14f76722013-01-15 09:04:18 -08001846 if (audio && mAudioDecoder != NULL) {
Andreas Hubera1f8ab02012-11-30 10:53:22 -08001847 flushDecoder(true /* audio */, true /* needShutdown */);
1848 }
1849
Andreas Huber14f76722013-01-15 09:04:18 -08001850 if (video && mVideoDecoder != NULL) {
Andreas Hubera1f8ab02012-11-30 10:53:22 -08001851 flushDecoder(false /* audio */, true /* needShutdown */);
1852 }
1853}
1854
1855void NuPlayer::performReset() {
1856 ALOGV("performReset");
1857
1858 CHECK(mAudioDecoder == NULL);
1859 CHECK(mVideoDecoder == NULL);
1860
1861 cancelPollDuration();
1862
1863 ++mScanSourcesGeneration;
1864 mScanSourcesPending = false;
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();
Wei Jia57568df2014-09-22 10:16:29 -07001874 ++mRendererGeneration;
Andreas Hubera1f8ab02012-11-30 10:53:22 -08001875
1876 if (mSource != NULL) {
1877 mSource->stop();
Andreas Huberb5f25f02013-02-05 10:14:26 -08001878
Andreas Hubera1f8ab02012-11-30 10:53:22 -08001879 mSource.clear();
1880 }
1881
1882 if (mDriver != NULL) {
1883 sp<NuPlayerDriver> driver = mDriver.promote();
1884 if (driver != NULL) {
1885 driver->notifyResetComplete();
1886 }
1887 }
Andreas Huber57a339c2012-12-03 11:18:00 -08001888
1889 mStarted = false;
Andreas Hubera1f8ab02012-11-30 10:53:22 -08001890}
1891
1892void NuPlayer::performScanSources() {
1893 ALOGV("performScanSources");
1894
Andreas Huber57a339c2012-12-03 11:18:00 -08001895 if (!mStarted) {
1896 return;
1897 }
1898
Andreas Hubera1f8ab02012-11-30 10:53:22 -08001899 if (mAudioDecoder == NULL || mVideoDecoder == NULL) {
1900 postScanSources();
1901 }
1902}
1903
Andreas Huber57a339c2012-12-03 11:18:00 -08001904void NuPlayer::performSetSurface(const sp<NativeWindowWrapper> &wrapper) {
1905 ALOGV("performSetSurface");
1906
1907 mNativeWindow = wrapper;
1908
1909 // XXX - ignore error from setVideoScalingMode for now
1910 setVideoScalingMode(mVideoScalingMode);
Chong Zhang13d6faa2014-08-22 15:35:28 -07001911
1912 if (mDriver != NULL) {
1913 sp<NuPlayerDriver> driver = mDriver.promote();
1914 if (driver != NULL) {
1915 driver->notifySetSurfaceComplete();
1916 }
1917 }
Andreas Huber57a339c2012-12-03 11:18:00 -08001918}
1919
Andreas Huber9575c962013-02-05 13:59:56 -08001920void NuPlayer::onSourceNotify(const sp<AMessage> &msg) {
1921 int32_t what;
1922 CHECK(msg->findInt32("what", &what));
1923
1924 switch (what) {
1925 case Source::kWhatPrepared:
1926 {
Andreas Huberb5f28d42013-04-25 15:11:19 -07001927 if (mSource == NULL) {
1928 // This is a stale notification from a source that was
1929 // asynchronously preparing when the client called reset().
1930 // We handled the reset, the source is gone.
1931 break;
1932 }
1933
Andreas Huberec0c5972013-02-05 14:47:13 -08001934 int32_t err;
1935 CHECK(msg->findInt32("err", &err));
1936
Andreas Huber9575c962013-02-05 13:59:56 -08001937 sp<NuPlayerDriver> driver = mDriver.promote();
1938 if (driver != NULL) {
Marco Nelissendd114d12014-05-28 15:23:14 -07001939 // notify duration first, so that it's definitely set when
1940 // the app received the "prepare complete" callback.
1941 int64_t durationUs;
1942 if (mSource->getDuration(&durationUs) == OK) {
1943 driver->notifyDuration(durationUs);
1944 }
Andreas Huberec0c5972013-02-05 14:47:13 -08001945 driver->notifyPrepareCompleted(err);
Andreas Huber9575c962013-02-05 13:59:56 -08001946 }
Andreas Huber99759402013-04-01 14:28:31 -07001947
Andreas Huber9575c962013-02-05 13:59:56 -08001948 break;
1949 }
1950
1951 case Source::kWhatFlagsChanged:
1952 {
1953 uint32_t flags;
1954 CHECK(msg->findInt32("flags", (int32_t *)&flags));
1955
Chong Zhang4b7069d2013-09-11 12:52:43 -07001956 sp<NuPlayerDriver> driver = mDriver.promote();
1957 if (driver != NULL) {
1958 driver->notifyFlagsChanged(flags);
1959 }
1960
Andreas Huber9575c962013-02-05 13:59:56 -08001961 if ((mSourceFlags & Source::FLAG_DYNAMIC_DURATION)
1962 && (!(flags & Source::FLAG_DYNAMIC_DURATION))) {
1963 cancelPollDuration();
1964 } else if (!(mSourceFlags & Source::FLAG_DYNAMIC_DURATION)
1965 && (flags & Source::FLAG_DYNAMIC_DURATION)
1966 && (mAudioDecoder != NULL || mVideoDecoder != NULL)) {
1967 schedulePollDuration();
1968 }
1969
1970 mSourceFlags = flags;
1971 break;
1972 }
1973
1974 case Source::kWhatVideoSizeChanged:
1975 {
Chong Zhangced1c2f2014-08-08 15:22:35 -07001976 sp<AMessage> format;
1977 CHECK(msg->findMessage("format", &format));
Andreas Huber9575c962013-02-05 13:59:56 -08001978
Chong Zhangced1c2f2014-08-08 15:22:35 -07001979 updateVideoSize(format);
Andreas Huber9575c962013-02-05 13:59:56 -08001980 break;
1981 }
1982
Chong Zhang2a3cc9a2014-08-21 17:48:26 -07001983 case Source::kWhatBufferingUpdate:
1984 {
1985 int32_t percentage;
1986 CHECK(msg->findInt32("percentage", &percentage));
1987
1988 notifyListener(MEDIA_BUFFERING_UPDATE, percentage, 0);
1989 break;
1990 }
1991
Roger Jönssonb50e83e2013-01-21 16:26:41 +01001992 case Source::kWhatBufferingStart:
1993 {
1994 notifyListener(MEDIA_INFO, MEDIA_INFO_BUFFERING_START, 0);
1995 break;
1996 }
1997
1998 case Source::kWhatBufferingEnd:
1999 {
2000 notifyListener(MEDIA_INFO, MEDIA_INFO_BUFFERING_END, 0);
2001 break;
2002 }
2003
Chong Zhangdcb89b32013-08-06 09:44:47 -07002004 case Source::kWhatSubtitleData:
2005 {
2006 sp<ABuffer> buffer;
2007 CHECK(msg->findBuffer("buffer", &buffer));
2008
Chong Zhang404fced2014-06-11 14:45:31 -07002009 sendSubtitleData(buffer, 0 /* baseIndex */);
Chong Zhangdcb89b32013-08-06 09:44:47 -07002010 break;
2011 }
2012
Robert Shihd3b0bbb2014-07-23 15:00:25 -07002013 case Source::kWhatTimedTextData:
2014 {
2015 int32_t generation;
2016 if (msg->findInt32("generation", &generation)
2017 && generation != mTimedTextGeneration) {
2018 break;
2019 }
2020
2021 sp<ABuffer> buffer;
2022 CHECK(msg->findBuffer("buffer", &buffer));
2023
2024 sp<NuPlayerDriver> driver = mDriver.promote();
2025 if (driver == NULL) {
2026 break;
2027 }
2028
2029 int posMs;
2030 int64_t timeUs, posUs;
2031 driver->getCurrentPosition(&posMs);
2032 posUs = posMs * 1000;
2033 CHECK(buffer->meta()->findInt64("timeUs", &timeUs));
2034
2035 if (posUs < timeUs) {
2036 if (!msg->findInt32("generation", &generation)) {
2037 msg->setInt32("generation", mTimedTextGeneration);
2038 }
2039 msg->post(timeUs - posUs);
2040 } else {
2041 sendTimedTextData(buffer);
2042 }
2043 break;
2044 }
2045
Andreas Huber14f76722013-01-15 09:04:18 -08002046 case Source::kWhatQueueDecoderShutdown:
2047 {
2048 int32_t audio, video;
2049 CHECK(msg->findInt32("audio", &audio));
2050 CHECK(msg->findInt32("video", &video));
2051
2052 sp<AMessage> reply;
2053 CHECK(msg->findMessage("reply", &reply));
2054
2055 queueDecoderShutdown(audio, video, reply);
2056 break;
2057 }
2058
Ronghua Wu80276872014-08-28 15:50:29 -07002059 case Source::kWhatDrmNoLicense:
2060 {
2061 notifyListener(MEDIA_ERROR, MEDIA_ERROR_UNKNOWN, ERROR_DRM_NO_LICENSE);
2062 break;
2063 }
2064
Andreas Huber9575c962013-02-05 13:59:56 -08002065 default:
2066 TRESPASS();
2067 }
2068}
2069
Chong Zhanga7fa1d92014-06-11 14:49:23 -07002070void NuPlayer::onClosedCaptionNotify(const sp<AMessage> &msg) {
2071 int32_t what;
2072 CHECK(msg->findInt32("what", &what));
2073
2074 switch (what) {
2075 case NuPlayer::CCDecoder::kWhatClosedCaptionData:
2076 {
2077 sp<ABuffer> buffer;
2078 CHECK(msg->findBuffer("buffer", &buffer));
2079
2080 size_t inbandTracks = 0;
2081 if (mSource != NULL) {
2082 inbandTracks = mSource->getTrackCount();
2083 }
2084
2085 sendSubtitleData(buffer, inbandTracks);
2086 break;
2087 }
2088
2089 case NuPlayer::CCDecoder::kWhatTrackAdded:
2090 {
2091 notifyListener(MEDIA_INFO, MEDIA_INFO_METADATA_UPDATE, 0);
2092
2093 break;
2094 }
2095
2096 default:
2097 TRESPASS();
2098 }
2099
2100
2101}
2102
Chong Zhang404fced2014-06-11 14:45:31 -07002103void NuPlayer::sendSubtitleData(const sp<ABuffer> &buffer, int32_t baseIndex) {
2104 int32_t trackIndex;
2105 int64_t timeUs, durationUs;
2106 CHECK(buffer->meta()->findInt32("trackIndex", &trackIndex));
2107 CHECK(buffer->meta()->findInt64("timeUs", &timeUs));
2108 CHECK(buffer->meta()->findInt64("durationUs", &durationUs));
2109
2110 Parcel in;
2111 in.writeInt32(trackIndex + baseIndex);
2112 in.writeInt64(timeUs);
2113 in.writeInt64(durationUs);
2114 in.writeInt32(buffer->size());
2115 in.writeInt32(buffer->size());
2116 in.write(buffer->data(), buffer->size());
2117
2118 notifyListener(MEDIA_SUBTITLE_DATA, 0, 0, &in);
2119}
Robert Shihd3b0bbb2014-07-23 15:00:25 -07002120
2121void NuPlayer::sendTimedTextData(const sp<ABuffer> &buffer) {
2122 const void *data;
2123 size_t size = 0;
2124 int64_t timeUs;
2125 int32_t flag = TextDescriptions::LOCAL_DESCRIPTIONS;
2126
2127 AString mime;
2128 CHECK(buffer->meta()->findString("mime", &mime));
2129 CHECK(strcasecmp(mime.c_str(), MEDIA_MIMETYPE_TEXT_3GPP) == 0);
2130
2131 data = buffer->data();
2132 size = buffer->size();
2133
2134 Parcel parcel;
2135 if (size > 0) {
2136 CHECK(buffer->meta()->findInt64("timeUs", &timeUs));
2137 flag |= TextDescriptions::IN_BAND_TEXT_3GPP;
2138 TextDescriptions::getParcelOfDescriptions(
2139 (const uint8_t *)data, size, flag, timeUs / 1000, &parcel);
2140 }
2141
2142 if ((parcel.dataSize() > 0)) {
2143 notifyListener(MEDIA_TIMED_TEXT, 0, 0, &parcel);
2144 } else { // send an empty timed text
2145 notifyListener(MEDIA_TIMED_TEXT, 0, 0);
2146 }
2147}
Andreas Huberb5f25f02013-02-05 10:14:26 -08002148////////////////////////////////////////////////////////////////////////////////
2149
Chong Zhangced1c2f2014-08-08 15:22:35 -07002150sp<AMessage> NuPlayer::Source::getFormat(bool audio) {
2151 sp<MetaData> meta = getFormatMeta(audio);
2152
2153 if (meta == NULL) {
2154 return NULL;
2155 }
2156
2157 sp<AMessage> msg = new AMessage;
2158
2159 if(convertMetaDataToMessage(meta, &msg) == OK) {
2160 return msg;
2161 }
2162 return NULL;
2163}
2164
Andreas Huber9575c962013-02-05 13:59:56 -08002165void NuPlayer::Source::notifyFlagsChanged(uint32_t flags) {
2166 sp<AMessage> notify = dupNotify();
2167 notify->setInt32("what", kWhatFlagsChanged);
2168 notify->setInt32("flags", flags);
2169 notify->post();
2170}
2171
Chong Zhangced1c2f2014-08-08 15:22:35 -07002172void NuPlayer::Source::notifyVideoSizeChanged(const sp<AMessage> &format) {
Andreas Huber9575c962013-02-05 13:59:56 -08002173 sp<AMessage> notify = dupNotify();
2174 notify->setInt32("what", kWhatVideoSizeChanged);
Chong Zhangced1c2f2014-08-08 15:22:35 -07002175 notify->setMessage("format", format);
Andreas Huber9575c962013-02-05 13:59:56 -08002176 notify->post();
2177}
2178
Andreas Huberec0c5972013-02-05 14:47:13 -08002179void NuPlayer::Source::notifyPrepared(status_t err) {
Andreas Huber9575c962013-02-05 13:59:56 -08002180 sp<AMessage> notify = dupNotify();
2181 notify->setInt32("what", kWhatPrepared);
Andreas Huberec0c5972013-02-05 14:47:13 -08002182 notify->setInt32("err", err);
Andreas Huber9575c962013-02-05 13:59:56 -08002183 notify->post();
2184}
2185
Andreas Huber84333e02014-02-07 15:36:10 -08002186void NuPlayer::Source::onMessageReceived(const sp<AMessage> & /* msg */) {
Andreas Huberb5f25f02013-02-05 10:14:26 -08002187 TRESPASS();
2188}
2189
Andreas Huberf9334412010-12-15 15:17:42 -08002190} // namespace android