blob: 4bb7c96d795fef716c5a83b3cbff073cf444c6e3 [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),
Andreas Huber9b80c2b2011-06-30 15:47:02 -0700158 mAudioEOS(false),
Andreas Huberf9334412010-12-15 15:17:42 -0800159 mVideoEOS(false),
Andreas Huber5bc087c2010-12-23 10:27:40 -0800160 mScanSourcesPending(false),
Andreas Huber1aef2112011-01-04 14:01:29 -0800161 mScanSourcesGeneration(0),
Andreas Huberb7c8e912012-11-27 15:02:53 -0800162 mPollDurationGeneration(0),
Robert Shihd3b0bbb2014-07-23 15:00:25 -0700163 mTimedTextGeneration(0),
Andreas Huber6e3d3112011-11-28 12:36:11 -0800164 mTimeDiscontinuityPending(false),
Andreas Huberf9334412010-12-15 15:17:42 -0800165 mFlushingAudio(NONE),
Andreas Huber1aef2112011-01-04 14:01:29 -0800166 mFlushingVideo(NONE),
Andreas Huber3fe62152011-09-16 15:09:22 -0700167 mSkipRenderingAudioUntilMediaTimeUs(-1ll),
168 mSkipRenderingVideoUntilMediaTimeUs(-1ll),
169 mVideoLateByUs(0ll),
170 mNumFramesTotal(0ll),
James Dong0d268a32012-08-31 12:18:27 -0700171 mNumFramesDropped(0ll),
Andreas Huber57a339c2012-12-03 11:18:00 -0800172 mVideoScalingMode(NATIVE_WINDOW_SCALING_MODE_SCALE_TO_WINDOW),
173 mStarted(false) {
Andreas Huberf9334412010-12-15 15:17:42 -0800174}
175
176NuPlayer::~NuPlayer() {
177}
178
Andreas Huber9b80c2b2011-06-30 15:47:02 -0700179void NuPlayer::setUID(uid_t uid) {
180 mUIDValid = true;
181 mUID = uid;
182}
183
Andreas Huber43c3e6c2011-01-05 12:17:08 -0800184void NuPlayer::setDriver(const wp<NuPlayerDriver> &driver) {
185 mDriver = driver;
Andreas Huberf9334412010-12-15 15:17:42 -0800186}
187
Andreas Huber9575c962013-02-05 13:59:56 -0800188void NuPlayer::setDataSourceAsync(const sp<IStreamSource> &source) {
Andreas Huberf9334412010-12-15 15:17:42 -0800189 sp<AMessage> msg = new AMessage(kWhatSetDataSource, id());
190
Andreas Huberb5f25f02013-02-05 10:14:26 -0800191 sp<AMessage> notify = new AMessage(kWhatSourceNotify, id());
192
Andreas Huber240abcc2014-02-13 13:32:37 -0800193 msg->setObject("source", new StreamingSource(notify, source));
Andreas Huber5bc087c2010-12-23 10:27:40 -0800194 msg->post();
195}
Andreas Huberf9334412010-12-15 15:17:42 -0800196
Andreas Huberafed0e12011-09-20 15:39:58 -0700197static bool IsHTTPLiveURL(const char *url) {
198 if (!strncasecmp("http://", url, 7)
Andreas Huber99759402013-04-01 14:28:31 -0700199 || !strncasecmp("https://", url, 8)
200 || !strncasecmp("file://", url, 7)) {
Andreas Huberafed0e12011-09-20 15:39:58 -0700201 size_t len = strlen(url);
202 if (len >= 5 && !strcasecmp(".m3u8", &url[len - 5])) {
203 return true;
204 }
205
206 if (strstr(url,"m3u8")) {
207 return true;
208 }
209 }
210
211 return false;
212}
213
Andreas Huber9575c962013-02-05 13:59:56 -0800214void NuPlayer::setDataSourceAsync(
Andreas Huber1b86fe02014-01-29 11:13:26 -0800215 const sp<IMediaHTTPService> &httpService,
216 const char *url,
217 const KeyedVector<String8, String8> *headers) {
Chong Zhang3de157d2014-08-05 20:54:44 -0700218
Andreas Huber5bc087c2010-12-23 10:27:40 -0800219 sp<AMessage> msg = new AMessage(kWhatSetDataSource, id());
Oscar Rydhé7a33b772012-02-20 10:15:48 +0100220 size_t len = strlen(url);
Andreas Huber5bc087c2010-12-23 10:27:40 -0800221
Andreas Huberb5f25f02013-02-05 10:14:26 -0800222 sp<AMessage> notify = new AMessage(kWhatSourceNotify, id());
223
Andreas Huberafed0e12011-09-20 15:39:58 -0700224 sp<Source> source;
225 if (IsHTTPLiveURL(url)) {
Andreas Huber81e68442014-02-05 11:52:33 -0800226 source = new HTTPLiveSource(notify, httpService, url, headers);
Andreas Huberafed0e12011-09-20 15:39:58 -0700227 } else if (!strncasecmp(url, "rtsp://", 7)) {
Andreas Huber1b86fe02014-01-29 11:13:26 -0800228 source = new RTSPSource(
229 notify, httpService, url, headers, mUIDValid, mUID);
Oscar Rydhé7a33b772012-02-20 10:15:48 +0100230 } else if ((!strncasecmp(url, "http://", 7)
231 || !strncasecmp(url, "https://", 8))
232 && ((len >= 4 && !strcasecmp(".sdp", &url[len - 4]))
233 || strstr(url, ".sdp?"))) {
Andreas Huber1b86fe02014-01-29 11:13:26 -0800234 source = new RTSPSource(
235 notify, httpService, url, headers, mUIDValid, mUID, true);
Andreas Huber2bfdd422011-10-11 15:24:07 -0700236 } else {
Chong Zhang3de157d2014-08-05 20:54:44 -0700237 sp<GenericSource> genericSource =
238 new GenericSource(notify, mUIDValid, mUID);
239 // Don't set FLAG_SECURE on mSourceFlags here for widevine.
240 // The correct flags will be updated in Source::kWhatFlagsChanged
241 // handler when GenericSource is prepared.
Andreas Huber2bfdd422011-10-11 15:24:07 -0700242
Chong Zhanga19f33e2014-08-07 15:35:07 -0700243 status_t err = genericSource->setDataSource(httpService, url, headers);
Chong Zhang3de157d2014-08-05 20:54:44 -0700244
245 if (err == OK) {
246 source = genericSource;
247 } else {
Chong Zhanga19f33e2014-08-07 15:35:07 -0700248 ALOGE("Failed to set data source!");
Chong Zhang3de157d2014-08-05 20:54:44 -0700249 }
250 }
Andreas Huberafed0e12011-09-20 15:39:58 -0700251 msg->setObject("source", source);
252 msg->post();
253}
254
Andreas Huber9575c962013-02-05 13:59:56 -0800255void NuPlayer::setDataSourceAsync(int fd, int64_t offset, int64_t length) {
Andreas Huberafed0e12011-09-20 15:39:58 -0700256 sp<AMessage> msg = new AMessage(kWhatSetDataSource, id());
257
Andreas Huberb5f25f02013-02-05 10:14:26 -0800258 sp<AMessage> notify = new AMessage(kWhatSourceNotify, id());
259
Chong Zhang3de157d2014-08-05 20:54:44 -0700260 sp<GenericSource> source =
261 new GenericSource(notify, mUIDValid, mUID);
262
Chong Zhanga19f33e2014-08-07 15:35:07 -0700263 status_t err = source->setDataSource(fd, offset, length);
Chong Zhang3de157d2014-08-05 20:54:44 -0700264
265 if (err != OK) {
Chong Zhanga19f33e2014-08-07 15:35:07 -0700266 ALOGE("Failed to set data source!");
Chong Zhang3de157d2014-08-05 20:54:44 -0700267 source = NULL;
268 }
269
Andreas Huberafed0e12011-09-20 15:39:58 -0700270 msg->setObject("source", source);
Andreas Huberf9334412010-12-15 15:17:42 -0800271 msg->post();
272}
273
Andreas Huber9575c962013-02-05 13:59:56 -0800274void NuPlayer::prepareAsync() {
275 (new AMessage(kWhatPrepare, id()))->post();
276}
277
Andreas Huber57a339c2012-12-03 11:18:00 -0800278void NuPlayer::setVideoSurfaceTextureAsync(
Andy McFadden8ba01022012-12-18 09:46:54 -0800279 const sp<IGraphicBufferProducer> &bufferProducer) {
Glenn Kasten11731182011-02-08 17:26:17 -0800280 sp<AMessage> msg = new AMessage(kWhatSetVideoNativeWindow, id());
Andreas Huber57a339c2012-12-03 11:18:00 -0800281
Andy McFadden8ba01022012-12-18 09:46:54 -0800282 if (bufferProducer == NULL) {
Andreas Huber57a339c2012-12-03 11:18:00 -0800283 msg->setObject("native-window", NULL);
284 } else {
285 msg->setObject(
286 "native-window",
287 new NativeWindowWrapper(
Wei Jia9c03a402014-08-26 15:24:43 -0700288 new Surface(bufferProducer, true /* controlledByApp */)));
Andreas Huber57a339c2012-12-03 11:18:00 -0800289 }
290
Andreas Huberf9334412010-12-15 15:17:42 -0800291 msg->post();
292}
293
294void NuPlayer::setAudioSink(const sp<MediaPlayerBase::AudioSink> &sink) {
295 sp<AMessage> msg = new AMessage(kWhatSetAudioSink, id());
296 msg->setObject("sink", sink);
297 msg->post();
298}
299
300void NuPlayer::start() {
301 (new AMessage(kWhatStart, id()))->post();
302}
303
Andreas Huber43c3e6c2011-01-05 12:17:08 -0800304void NuPlayer::pause() {
Andreas Huberb4082222011-01-20 15:23:04 -0800305 (new AMessage(kWhatPause, id()))->post();
Andreas Huber43c3e6c2011-01-05 12:17:08 -0800306}
307
308void NuPlayer::resume() {
Andreas Huberb4082222011-01-20 15:23:04 -0800309 (new AMessage(kWhatResume, id()))->post();
Andreas Huber43c3e6c2011-01-05 12:17:08 -0800310}
311
Andreas Huber1aef2112011-01-04 14:01:29 -0800312void NuPlayer::resetAsync() {
313 (new AMessage(kWhatReset, id()))->post();
314}
315
Andreas Huber43c3e6c2011-01-05 12:17:08 -0800316void NuPlayer::seekToAsync(int64_t seekTimeUs) {
317 sp<AMessage> msg = new AMessage(kWhatSeek, id());
318 msg->setInt64("seekTimeUs", seekTimeUs);
319 msg->post();
320}
321
Andreas Huber53df1a42010-12-22 10:03:04 -0800322// static
Andreas Huber1aef2112011-01-04 14:01:29 -0800323bool NuPlayer::IsFlushingState(FlushStatus state, bool *needShutdown) {
Andreas Huber53df1a42010-12-22 10:03:04 -0800324 switch (state) {
325 case FLUSHING_DECODER:
Andreas Huber1aef2112011-01-04 14:01:29 -0800326 if (needShutdown != NULL) {
327 *needShutdown = false;
Andreas Huber53df1a42010-12-22 10:03:04 -0800328 }
329 return true;
330
Andreas Huber1aef2112011-01-04 14:01:29 -0800331 case FLUSHING_DECODER_SHUTDOWN:
332 if (needShutdown != NULL) {
333 *needShutdown = true;
Andreas Huber53df1a42010-12-22 10:03:04 -0800334 }
335 return true;
336
337 default:
338 return false;
339 }
340}
341
Chong Zhang404fced2014-06-11 14:45:31 -0700342void NuPlayer::writeTrackInfo(
343 Parcel* reply, const sp<AMessage> format) const {
344 int32_t trackType;
345 CHECK(format->findInt32("type", &trackType));
346
347 AString lang;
348 CHECK(format->findString("language", &lang));
349
350 reply->writeInt32(2); // write something non-zero
351 reply->writeInt32(trackType);
352 reply->writeString16(String16(lang.c_str()));
353
354 if (trackType == MEDIA_TRACK_TYPE_SUBTITLE) {
355 AString mime;
356 CHECK(format->findString("mime", &mime));
357
358 int32_t isAuto, isDefault, isForced;
359 CHECK(format->findInt32("auto", &isAuto));
360 CHECK(format->findInt32("default", &isDefault));
361 CHECK(format->findInt32("forced", &isForced));
362
363 reply->writeString16(String16(mime.c_str()));
364 reply->writeInt32(isAuto);
365 reply->writeInt32(isDefault);
366 reply->writeInt32(isForced);
367 }
368}
369
Andreas Huberf9334412010-12-15 15:17:42 -0800370void NuPlayer::onMessageReceived(const sp<AMessage> &msg) {
371 switch (msg->what()) {
372 case kWhatSetDataSource:
373 {
Steve Block3856b092011-10-20 11:56:00 +0100374 ALOGV("kWhatSetDataSource");
Andreas Huberf9334412010-12-15 15:17:42 -0800375
376 CHECK(mSource == NULL);
377
Chong Zhang3de157d2014-08-05 20:54:44 -0700378 status_t err = OK;
Andreas Huber5bc087c2010-12-23 10:27:40 -0800379 sp<RefBase> obj;
380 CHECK(msg->findObject("source", &obj));
Chong Zhang3de157d2014-08-05 20:54:44 -0700381 if (obj != NULL) {
382 mSource = static_cast<Source *>(obj.get());
Chong Zhang3de157d2014-08-05 20:54:44 -0700383 } else {
384 err = UNKNOWN_ERROR;
385 }
Andreas Huber9575c962013-02-05 13:59:56 -0800386
387 CHECK(mDriver != NULL);
388 sp<NuPlayerDriver> driver = mDriver.promote();
389 if (driver != NULL) {
Chong Zhang3de157d2014-08-05 20:54:44 -0700390 driver->notifySetDataSourceCompleted(err);
Andreas Huber9575c962013-02-05 13:59:56 -0800391 }
392 break;
393 }
394
395 case kWhatPrepare:
396 {
397 mSource->prepareAsync();
Andreas Huberf9334412010-12-15 15:17:42 -0800398 break;
399 }
400
Chong Zhangdcb89b32013-08-06 09:44:47 -0700401 case kWhatGetTrackInfo:
402 {
403 uint32_t replyID;
404 CHECK(msg->senderAwaitsResponse(&replyID));
405
Chong Zhang404fced2014-06-11 14:45:31 -0700406 Parcel* reply;
407 CHECK(msg->findPointer("reply", (void**)&reply));
408
409 size_t inbandTracks = 0;
Chong Zhangdcb89b32013-08-06 09:44:47 -0700410 if (mSource != NULL) {
Chong Zhang404fced2014-06-11 14:45:31 -0700411 inbandTracks = mSource->getTrackCount();
412 }
413
Chong Zhanga7fa1d92014-06-11 14:49:23 -0700414 size_t ccTracks = 0;
415 if (mCCDecoder != NULL) {
416 ccTracks = mCCDecoder->getTrackCount();
417 }
418
Chong Zhang404fced2014-06-11 14:45:31 -0700419 // total track count
Chong Zhanga7fa1d92014-06-11 14:49:23 -0700420 reply->writeInt32(inbandTracks + ccTracks);
Chong Zhang404fced2014-06-11 14:45:31 -0700421
422 // write inband tracks
423 for (size_t i = 0; i < inbandTracks; ++i) {
424 writeTrackInfo(reply, mSource->getTrackInfo(i));
Chong Zhangdcb89b32013-08-06 09:44:47 -0700425 }
426
Chong Zhanga7fa1d92014-06-11 14:49:23 -0700427 // write CC track
428 for (size_t i = 0; i < ccTracks; ++i) {
429 writeTrackInfo(reply, mCCDecoder->getTrackInfo(i));
430 }
431
Chong Zhangdcb89b32013-08-06 09:44:47 -0700432 sp<AMessage> response = new AMessage;
Chong Zhangdcb89b32013-08-06 09:44:47 -0700433 response->postReply(replyID);
434 break;
435 }
436
Robert Shih7c4f0d72014-07-09 18:53:31 -0700437 case kWhatGetSelectedTrack:
438 {
439 status_t err = INVALID_OPERATION;
440 if (mSource != NULL) {
441 err = OK;
442
443 int32_t type32;
444 CHECK(msg->findInt32("type", (int32_t*)&type32));
445 media_track_type type = (media_track_type)type32;
446 ssize_t selectedTrack = mSource->getSelectedTrack(type);
447
448 Parcel* reply;
449 CHECK(msg->findPointer("reply", (void**)&reply));
450 reply->writeInt32(selectedTrack);
451 }
452
453 sp<AMessage> response = new AMessage;
454 response->setInt32("err", err);
455
456 uint32_t replyID;
457 CHECK(msg->senderAwaitsResponse(&replyID));
458 response->postReply(replyID);
459 break;
460 }
461
Chong Zhangdcb89b32013-08-06 09:44:47 -0700462 case kWhatSelectTrack:
463 {
464 uint32_t replyID;
465 CHECK(msg->senderAwaitsResponse(&replyID));
466
Chong Zhang404fced2014-06-11 14:45:31 -0700467 size_t trackIndex;
468 int32_t select;
469 CHECK(msg->findSize("trackIndex", &trackIndex));
470 CHECK(msg->findInt32("select", &select));
471
Chong Zhangdcb89b32013-08-06 09:44:47 -0700472 status_t err = INVALID_OPERATION;
Chong Zhang404fced2014-06-11 14:45:31 -0700473
474 size_t inbandTracks = 0;
Chong Zhangdcb89b32013-08-06 09:44:47 -0700475 if (mSource != NULL) {
Chong Zhang404fced2014-06-11 14:45:31 -0700476 inbandTracks = mSource->getTrackCount();
477 }
Chong Zhanga7fa1d92014-06-11 14:49:23 -0700478 size_t ccTracks = 0;
479 if (mCCDecoder != NULL) {
480 ccTracks = mCCDecoder->getTrackCount();
481 }
Chong Zhang404fced2014-06-11 14:45:31 -0700482
483 if (trackIndex < inbandTracks) {
Chong Zhangdcb89b32013-08-06 09:44:47 -0700484 err = mSource->selectTrack(trackIndex, select);
Robert Shihd3b0bbb2014-07-23 15:00:25 -0700485
486 if (!select && err == OK) {
487 int32_t type;
488 sp<AMessage> info = mSource->getTrackInfo(trackIndex);
489 if (info != NULL
490 && info->findInt32("type", &type)
491 && type == MEDIA_TRACK_TYPE_TIMEDTEXT) {
492 ++mTimedTextGeneration;
493 }
494 }
Chong Zhanga7fa1d92014-06-11 14:49:23 -0700495 } else {
496 trackIndex -= inbandTracks;
497
498 if (trackIndex < ccTracks) {
499 err = mCCDecoder->selectTrack(trackIndex, select);
500 }
Chong Zhangdcb89b32013-08-06 09:44:47 -0700501 }
502
503 sp<AMessage> response = new AMessage;
504 response->setInt32("err", err);
505
506 response->postReply(replyID);
507 break;
508 }
509
Andreas Huberb7c8e912012-11-27 15:02:53 -0800510 case kWhatPollDuration:
511 {
512 int32_t generation;
513 CHECK(msg->findInt32("generation", &generation));
514
515 if (generation != mPollDurationGeneration) {
516 // stale
517 break;
518 }
519
520 int64_t durationUs;
521 if (mDriver != NULL && mSource->getDuration(&durationUs) == OK) {
522 sp<NuPlayerDriver> driver = mDriver.promote();
523 if (driver != NULL) {
524 driver->notifyDuration(durationUs);
525 }
526 }
527
528 msg->post(1000000ll); // poll again in a second.
529 break;
530 }
531
Glenn Kasten11731182011-02-08 17:26:17 -0800532 case kWhatSetVideoNativeWindow:
Andreas Huberf9334412010-12-15 15:17:42 -0800533 {
Steve Block3856b092011-10-20 11:56:00 +0100534 ALOGV("kWhatSetVideoNativeWindow");
Andreas Huberf9334412010-12-15 15:17:42 -0800535
Andreas Huber57a339c2012-12-03 11:18:00 -0800536 mDeferredActions.push_back(
Andreas Huber14f76722013-01-15 09:04:18 -0800537 new ShutdownDecoderAction(
538 false /* audio */, true /* video */));
Andreas Huber57a339c2012-12-03 11:18:00 -0800539
Andreas Huberf9334412010-12-15 15:17:42 -0800540 sp<RefBase> obj;
Glenn Kasten11731182011-02-08 17:26:17 -0800541 CHECK(msg->findObject("native-window", &obj));
Andreas Huberf9334412010-12-15 15:17:42 -0800542
Andreas Huber57a339c2012-12-03 11:18:00 -0800543 mDeferredActions.push_back(
544 new SetSurfaceAction(
545 static_cast<NativeWindowWrapper *>(obj.get())));
James Dong0d268a32012-08-31 12:18:27 -0700546
Andreas Huber57a339c2012-12-03 11:18:00 -0800547 if (obj != NULL) {
Andy Hung73535852014-09-05 11:42:58 -0700548 if (mStarted && mVideoDecoder != NULL) {
549 // Issue a seek to refresh the video screen only if started otherwise
550 // the extractor may not yet be started and will assert.
551 // If the video decoder is not set (perhaps audio only in this case)
552 // do not perform a seek as it is not needed.
553 mDeferredActions.push_back(new SeekAction(mCurrentPositionUs));
554 }
Wei Jiaac428aa2014-09-02 19:01:34 -0700555
Andreas Huber57a339c2012-12-03 11:18:00 -0800556 // If there is a new surface texture, instantiate decoders
557 // again if possible.
558 mDeferredActions.push_back(
559 new SimpleAction(&NuPlayer::performScanSources));
560 }
561
562 processDeferredActions();
Andreas Huberf9334412010-12-15 15:17:42 -0800563 break;
564 }
565
566 case kWhatSetAudioSink:
567 {
Steve Block3856b092011-10-20 11:56:00 +0100568 ALOGV("kWhatSetAudioSink");
Andreas Huberf9334412010-12-15 15:17:42 -0800569
570 sp<RefBase> obj;
571 CHECK(msg->findObject("sink", &obj));
572
573 mAudioSink = static_cast<MediaPlayerBase::AudioSink *>(obj.get());
574 break;
575 }
576
577 case kWhatStart:
578 {
Steve Block3856b092011-10-20 11:56:00 +0100579 ALOGV("kWhatStart");
Andreas Huber43c3e6c2011-01-05 12:17:08 -0800580
Andreas Huber3fe62152011-09-16 15:09:22 -0700581 mVideoIsAVC = false;
Wei Jiabc2fb722014-07-08 16:37:57 -0700582 mOffloadAudio = false;
Andreas Huber1aef2112011-01-04 14:01:29 -0800583 mAudioEOS = false;
584 mVideoEOS = false;
Andreas Huber32f3cef2011-03-02 15:34:46 -0800585 mSkipRenderingAudioUntilMediaTimeUs = -1;
586 mSkipRenderingVideoUntilMediaTimeUs = -1;
Andreas Huber3fe62152011-09-16 15:09:22 -0700587 mVideoLateByUs = 0;
588 mNumFramesTotal = 0;
589 mNumFramesDropped = 0;
Andreas Huber57a339c2012-12-03 11:18:00 -0800590 mStarted = true;
Andreas Huber1aef2112011-01-04 14:01:29 -0800591
Lajos Molnar09524832014-07-17 14:29:51 -0700592 /* instantiate decoders now for secure playback */
593 if (mSourceFlags & Source::FLAG_SECURE) {
594 if (mNativeWindow != NULL) {
595 instantiateDecoder(false, &mVideoDecoder);
596 }
597
598 if (mAudioSink != NULL) {
599 instantiateDecoder(true, &mAudioDecoder);
600 }
601 }
602
Andreas Huber5bc087c2010-12-23 10:27:40 -0800603 mSource->start();
Andreas Huberf9334412010-12-15 15:17:42 -0800604
Andreas Huberd5e56232013-03-12 11:01:43 -0700605 uint32_t flags = 0;
606
607 if (mSource->isRealTime()) {
608 flags |= Renderer::FLAG_REAL_TIME;
609 }
610
Wei Jiabc2fb722014-07-08 16:37:57 -0700611 sp<MetaData> audioMeta = mSource->getFormatMeta(true /* audio */);
612 audio_stream_type_t streamType = AUDIO_STREAM_MUSIC;
613 if (mAudioSink != NULL) {
614 streamType = mAudioSink->getAudioStreamType();
615 }
616
617 sp<AMessage> videoFormat = mSource->getFormat(false /* audio */);
618
619 mOffloadAudio =
620 canOffloadStream(audioMeta, (videoFormat != NULL),
621 true /* is_streaming */, streamType);
622 if (mOffloadAudio) {
623 flags |= Renderer::FLAG_OFFLOAD_AUDIO;
624 }
625
Andreas Huberf9334412010-12-15 15:17:42 -0800626 mRenderer = new Renderer(
627 mAudioSink,
Andreas Huberd5e56232013-03-12 11:01:43 -0700628 new AMessage(kWhatRendererNotify, id()),
629 flags);
Andreas Huberf9334412010-12-15 15:17:42 -0800630
Lajos Molnar09524832014-07-17 14:29:51 -0700631 mRendererLooper = new ALooper;
632 mRendererLooper->setName("NuPlayerRenderer");
633 mRendererLooper->start(false, false, ANDROID_PRIORITY_AUDIO);
634 mRendererLooper->registerHandler(mRenderer);
Andreas Huberf9334412010-12-15 15:17:42 -0800635
Andreas Huber1aef2112011-01-04 14:01:29 -0800636 postScanSources();
Andreas Huberf9334412010-12-15 15:17:42 -0800637 break;
638 }
639
640 case kWhatScanSources:
641 {
Andreas Huber1aef2112011-01-04 14:01:29 -0800642 int32_t generation;
643 CHECK(msg->findInt32("generation", &generation));
644 if (generation != mScanSourcesGeneration) {
645 // Drop obsolete msg.
646 break;
647 }
648
Andreas Huber5bc087c2010-12-23 10:27:40 -0800649 mScanSourcesPending = false;
650
Steve Block3856b092011-10-20 11:56:00 +0100651 ALOGV("scanning sources haveAudio=%d, haveVideo=%d",
Andreas Huber43c3e6c2011-01-05 12:17:08 -0800652 mAudioDecoder != NULL, mVideoDecoder != NULL);
653
Andreas Huberb7c8e912012-11-27 15:02:53 -0800654 bool mHadAnySourcesBefore =
655 (mAudioDecoder != NULL) || (mVideoDecoder != NULL);
656
Andy Hung282a7e32014-08-14 15:56:34 -0700657 // initialize video before audio because successful initialization of
658 // video may change deep buffer mode of audio.
Haynes Mathew George5d246ef2012-07-09 10:36:57 -0700659 if (mNativeWindow != NULL) {
660 instantiateDecoder(false, &mVideoDecoder);
661 }
Andreas Huberf9334412010-12-15 15:17:42 -0800662
663 if (mAudioSink != NULL) {
Andy Hung282a7e32014-08-14 15:56:34 -0700664 if (mOffloadAudio) {
665 // open audio sink early under offload mode.
666 sp<AMessage> format = mSource->getFormat(true /*audio*/);
667 openAudioSink(format, true /*offloadOnly*/);
668 }
Andreas Huber5bc087c2010-12-23 10:27:40 -0800669 instantiateDecoder(true, &mAudioDecoder);
Andreas Huberf9334412010-12-15 15:17:42 -0800670 }
671
Andreas Huberb7c8e912012-11-27 15:02:53 -0800672 if (!mHadAnySourcesBefore
673 && (mAudioDecoder != NULL || mVideoDecoder != NULL)) {
674 // This is the first time we've found anything playable.
675
Andreas Huber9575c962013-02-05 13:59:56 -0800676 if (mSourceFlags & Source::FLAG_DYNAMIC_DURATION) {
Andreas Huberb7c8e912012-11-27 15:02:53 -0800677 schedulePollDuration();
678 }
679 }
680
Andreas Hubereac68ba2011-09-27 12:12:25 -0700681 status_t err;
682 if ((err = mSource->feedMoreTSData()) != OK) {
Andreas Huber1aef2112011-01-04 14:01:29 -0800683 if (mAudioDecoder == NULL && mVideoDecoder == NULL) {
684 // We're not currently decoding anything (no audio or
685 // video tracks found) and we just ran out of input data.
Andreas Hubereac68ba2011-09-27 12:12:25 -0700686
687 if (err == ERROR_END_OF_STREAM) {
688 notifyListener(MEDIA_PLAYBACK_COMPLETE, 0, 0);
689 } else {
690 notifyListener(MEDIA_ERROR, MEDIA_ERROR_UNKNOWN, err);
691 }
Andreas Huber1aef2112011-01-04 14:01:29 -0800692 }
Andreas Huberf9334412010-12-15 15:17:42 -0800693 break;
694 }
695
Andreas Huberfbe9d812012-08-31 14:05:27 -0700696 if ((mAudioDecoder == NULL && mAudioSink != NULL)
697 || (mVideoDecoder == NULL && mNativeWindow != NULL)) {
Andreas Huberf9334412010-12-15 15:17:42 -0800698 msg->post(100000ll);
Andreas Huber5bc087c2010-12-23 10:27:40 -0800699 mScanSourcesPending = true;
Andreas Huberf9334412010-12-15 15:17:42 -0800700 }
701 break;
702 }
703
704 case kWhatVideoNotify:
705 case kWhatAudioNotify:
706 {
707 bool audio = msg->what() == kWhatAudioNotify;
708
Wei Jia88703c32014-08-06 11:24:07 -0700709 int32_t currentDecoderGeneration =
710 (audio? mAudioDecoderGeneration : mVideoDecoderGeneration);
711 int32_t requesterGeneration = currentDecoderGeneration - 1;
712 CHECK(msg->findInt32("generation", &requesterGeneration));
713
714 if (requesterGeneration != currentDecoderGeneration) {
715 ALOGV("got message from old %s decoder, generation(%d:%d)",
716 audio ? "audio" : "video", requesterGeneration,
717 currentDecoderGeneration);
718 sp<AMessage> reply;
719 if (!(msg->findMessage("reply", &reply))) {
720 return;
721 }
722
723 reply->setInt32("err", INFO_DISCONTINUITY);
724 reply->post();
725 return;
726 }
727
Andreas Huberf9334412010-12-15 15:17:42 -0800728 int32_t what;
Lajos Molnar1cd13982014-01-17 15:12:51 -0800729 CHECK(msg->findInt32("what", &what));
Andreas Huberf9334412010-12-15 15:17:42 -0800730
Lajos Molnar1cd13982014-01-17 15:12:51 -0800731 if (what == Decoder::kWhatFillThisBuffer) {
Andreas Huberf9334412010-12-15 15:17:42 -0800732 status_t err = feedDecoderInputData(
Lajos Molnar1cd13982014-01-17 15:12:51 -0800733 audio, msg);
Andreas Huberf9334412010-12-15 15:17:42 -0800734
Andreas Huber5bc087c2010-12-23 10:27:40 -0800735 if (err == -EWOULDBLOCK) {
Andreas Hubereac68ba2011-09-27 12:12:25 -0700736 if (mSource->feedMoreTSData() == OK) {
Phil Burkc5cc2e22014-09-09 20:08:39 -0700737 msg->post(10 * 1000ll);
Andreas Huber5bc087c2010-12-23 10:27:40 -0800738 }
Andreas Huberf9334412010-12-15 15:17:42 -0800739 }
Lajos Molnar1cd13982014-01-17 15:12:51 -0800740 } else if (what == Decoder::kWhatEOS) {
Andreas Huberdc9bacd2011-09-26 10:53:29 -0700741 int32_t err;
Lajos Molnar1cd13982014-01-17 15:12:51 -0800742 CHECK(msg->findInt32("err", &err));
Andreas Huberdc9bacd2011-09-26 10:53:29 -0700743
744 if (err == ERROR_END_OF_STREAM) {
Steve Block3856b092011-10-20 11:56:00 +0100745 ALOGV("got %s decoder EOS", audio ? "audio" : "video");
Andreas Huberdc9bacd2011-09-26 10:53:29 -0700746 } else {
Steve Block3856b092011-10-20 11:56:00 +0100747 ALOGV("got %s decoder EOS w/ error %d",
Andreas Huberdc9bacd2011-09-26 10:53:29 -0700748 audio ? "audio" : "video",
749 err);
750 }
751
752 mRenderer->queueEOS(audio, err);
Lajos Molnar1cd13982014-01-17 15:12:51 -0800753 } else if (what == Decoder::kWhatFlushCompleted) {
Andreas Huber1aef2112011-01-04 14:01:29 -0800754 bool needShutdown;
Andreas Huber53df1a42010-12-22 10:03:04 -0800755
Andreas Huberf9334412010-12-15 15:17:42 -0800756 if (audio) {
Andreas Huber1aef2112011-01-04 14:01:29 -0800757 CHECK(IsFlushingState(mFlushingAudio, &needShutdown));
Andreas Huberf9334412010-12-15 15:17:42 -0800758 mFlushingAudio = FLUSHED;
759 } else {
Andreas Huber1aef2112011-01-04 14:01:29 -0800760 CHECK(IsFlushingState(mFlushingVideo, &needShutdown));
Andreas Huberf9334412010-12-15 15:17:42 -0800761 mFlushingVideo = FLUSHED;
Andreas Huber3fe62152011-09-16 15:09:22 -0700762
763 mVideoLateByUs = 0;
Andreas Huberf9334412010-12-15 15:17:42 -0800764 }
765
Steve Block3856b092011-10-20 11:56:00 +0100766 ALOGV("decoder %s flush completed", audio ? "audio" : "video");
Andreas Huberf9334412010-12-15 15:17:42 -0800767
Andreas Huber1aef2112011-01-04 14:01:29 -0800768 if (needShutdown) {
Steve Block3856b092011-10-20 11:56:00 +0100769 ALOGV("initiating %s decoder shutdown",
Andreas Huber53df1a42010-12-22 10:03:04 -0800770 audio ? "audio" : "video");
Andreas Huberf9334412010-12-15 15:17:42 -0800771
Lajos Molnar87603c02014-08-20 19:25:30 -0700772 getDecoder(audio)->initiateShutdown();
Andreas Huberf9334412010-12-15 15:17:42 -0800773
Andreas Huber53df1a42010-12-22 10:03:04 -0800774 if (audio) {
775 mFlushingAudio = SHUTTING_DOWN_DECODER;
776 } else {
777 mFlushingVideo = SHUTTING_DOWN_DECODER;
778 }
Andreas Huberf9334412010-12-15 15:17:42 -0800779 }
Andreas Huber3831a062010-12-21 10:22:33 -0800780
781 finishFlushIfPossible();
Lajos Molnar1cd13982014-01-17 15:12:51 -0800782 } else if (what == Decoder::kWhatOutputFormatChanged) {
783 sp<AMessage> format;
784 CHECK(msg->findMessage("format", &format));
785
Andreas Huber31e25082011-01-10 10:38:31 -0800786 if (audio) {
Andy Hung282a7e32014-08-14 15:56:34 -0700787 openAudioSink(format, false /*offloadOnly*/);
Andreas Huber31e25082011-01-10 10:38:31 -0800788 } else {
789 // video
Chong Zhangced1c2f2014-08-08 15:22:35 -0700790 sp<AMessage> inputFormat =
791 mSource->getFormat(false /* audio */);
Andreas Huber3831a062010-12-21 10:22:33 -0800792
Chong Zhangced1c2f2014-08-08 15:22:35 -0700793 updateVideoSize(inputFormat, format);
Andreas Huber31e25082011-01-10 10:38:31 -0800794 }
Lajos Molnar1cd13982014-01-17 15:12:51 -0800795 } else if (what == Decoder::kWhatShutdownCompleted) {
Steve Block3856b092011-10-20 11:56:00 +0100796 ALOGV("%s shutdown completed", audio ? "audio" : "video");
Andreas Huber3831a062010-12-21 10:22:33 -0800797 if (audio) {
798 mAudioDecoder.clear();
799
800 CHECK_EQ((int)mFlushingAudio, (int)SHUTTING_DOWN_DECODER);
801 mFlushingAudio = SHUT_DOWN;
802 } else {
803 mVideoDecoder.clear();
804
805 CHECK_EQ((int)mFlushingVideo, (int)SHUTTING_DOWN_DECODER);
806 mFlushingVideo = SHUT_DOWN;
807 }
808
809 finishFlushIfPossible();
Lajos Molnar1cd13982014-01-17 15:12:51 -0800810 } else if (what == Decoder::kWhatError) {
Steve Block29357bc2012-01-06 19:20:56 +0000811 ALOGE("Received error from %s decoder, aborting playback.",
Andreas Huberc92fd242011-08-16 13:48:44 -0700812 audio ? "audio" : "video");
813
Chong Zhangf4c0a942014-08-11 15:14:10 -0700814 status_t err;
815 if (!msg->findInt32("err", &err)) {
816 err = UNKNOWN_ERROR;
817 }
818 mRenderer->queueEOS(audio, err);
Marco Nelissen9e2b7912014-08-18 16:13:03 -0700819 if (audio && mFlushingAudio != NONE) {
820 mAudioDecoder.clear();
821 mFlushingAudio = SHUT_DOWN;
822 } else if (!audio && mFlushingVideo != NONE){
823 mVideoDecoder.clear();
824 mFlushingVideo = SHUT_DOWN;
825 }
826 finishFlushIfPossible();
Lajos Molnar1cd13982014-01-17 15:12:51 -0800827 } else if (what == Decoder::kWhatDrainThisBuffer) {
828 renderBuffer(audio, msg);
829 } else {
830 ALOGV("Unhandled decoder notification %d '%c%c%c%c'.",
Andreas Hubera1f8ab02012-11-30 10:53:22 -0800831 what,
832 what >> 24,
833 (what >> 16) & 0xff,
834 (what >> 8) & 0xff,
835 what & 0xff);
Andreas Huberf9334412010-12-15 15:17:42 -0800836 }
837
838 break;
839 }
840
841 case kWhatRendererNotify:
842 {
843 int32_t what;
844 CHECK(msg->findInt32("what", &what));
845
846 if (what == Renderer::kWhatEOS) {
847 int32_t audio;
848 CHECK(msg->findInt32("audio", &audio));
849
Andreas Huberc92fd242011-08-16 13:48:44 -0700850 int32_t finalResult;
851 CHECK(msg->findInt32("finalResult", &finalResult));
852
Andreas Huberf9334412010-12-15 15:17:42 -0800853 if (audio) {
854 mAudioEOS = true;
855 } else {
856 mVideoEOS = true;
857 }
858
Andreas Huberc92fd242011-08-16 13:48:44 -0700859 if (finalResult == ERROR_END_OF_STREAM) {
Steve Block3856b092011-10-20 11:56:00 +0100860 ALOGV("reached %s EOS", audio ? "audio" : "video");
Andreas Huberc92fd242011-08-16 13:48:44 -0700861 } else {
Steve Block29357bc2012-01-06 19:20:56 +0000862 ALOGE("%s track encountered an error (%d)",
Andreas Huberc92fd242011-08-16 13:48:44 -0700863 audio ? "audio" : "video", finalResult);
864
865 notifyListener(
866 MEDIA_ERROR, MEDIA_ERROR_UNKNOWN, finalResult);
867 }
Andreas Huberf9334412010-12-15 15:17:42 -0800868
869 if ((mAudioEOS || mAudioDecoder == NULL)
870 && (mVideoEOS || mVideoDecoder == NULL)) {
871 notifyListener(MEDIA_PLAYBACK_COMPLETE, 0, 0);
872 }
Andreas Huber43c3e6c2011-01-05 12:17:08 -0800873 } else if (what == Renderer::kWhatPosition) {
874 int64_t positionUs;
875 CHECK(msg->findInt64("positionUs", &positionUs));
Wei Jiaac428aa2014-09-02 19:01:34 -0700876 mCurrentPositionUs = positionUs;
Andreas Huber43c3e6c2011-01-05 12:17:08 -0800877
Andreas Huber3fe62152011-09-16 15:09:22 -0700878 CHECK(msg->findInt64("videoLateByUs", &mVideoLateByUs));
879
Andreas Huber43c3e6c2011-01-05 12:17:08 -0800880 if (mDriver != NULL) {
881 sp<NuPlayerDriver> driver = mDriver.promote();
882 if (driver != NULL) {
883 driver->notifyPosition(positionUs);
Andreas Huber3fe62152011-09-16 15:09:22 -0700884
885 driver->notifyFrameStats(
886 mNumFramesTotal, mNumFramesDropped);
Andreas Huber43c3e6c2011-01-05 12:17:08 -0800887 }
888 }
Andreas Huber3fe62152011-09-16 15:09:22 -0700889 } else if (what == Renderer::kWhatFlushComplete) {
Andreas Huberf9334412010-12-15 15:17:42 -0800890 int32_t audio;
891 CHECK(msg->findInt32("audio", &audio));
892
Steve Block3856b092011-10-20 11:56:00 +0100893 ALOGV("renderer %s flush completed.", audio ? "audio" : "video");
James Dongf57b4ea2012-07-20 13:38:36 -0700894 } else if (what == Renderer::kWhatVideoRenderingStart) {
895 notifyListener(MEDIA_INFO, MEDIA_INFO_RENDERING_START, 0);
Lajos Molnarcbaffcf2013-08-14 18:30:38 -0700896 } else if (what == Renderer::kWhatMediaRenderingStart) {
897 ALOGV("media rendering started");
898 notifyListener(MEDIA_STARTED, 0, 0);
Wei Jia3a2956d2014-07-22 16:01:33 -0700899 } else if (what == Renderer::kWhatAudioOffloadTearDown) {
900 ALOGV("Tear down audio offload, fall back to s/w path");
901 int64_t positionUs;
902 CHECK(msg->findInt64("positionUs", &positionUs));
Andy Hung282a7e32014-08-14 15:56:34 -0700903 closeAudioSink();
Wei Jia3a2956d2014-07-22 16:01:33 -0700904 mAudioDecoder.clear();
905 mRenderer->flush(true /* audio */);
906 if (mVideoDecoder != NULL) {
907 mRenderer->flush(false /* audio */);
908 }
909 mRenderer->signalDisableOffloadAudio();
910 mOffloadAudio = false;
911
912 performSeek(positionUs);
913 instantiateDecoder(true /* audio */, &mAudioDecoder);
Andreas Huberf9334412010-12-15 15:17:42 -0800914 }
915 break;
916 }
917
918 case kWhatMoreDataQueued:
919 {
920 break;
921 }
922
Andreas Huber1aef2112011-01-04 14:01:29 -0800923 case kWhatReset:
924 {
Steve Block3856b092011-10-20 11:56:00 +0100925 ALOGV("kWhatReset");
Andreas Huber1aef2112011-01-04 14:01:29 -0800926
Andreas Hubera1f8ab02012-11-30 10:53:22 -0800927 mDeferredActions.push_back(
Andreas Huber14f76722013-01-15 09:04:18 -0800928 new ShutdownDecoderAction(
929 true /* audio */, true /* video */));
Andreas Huberb7c8e912012-11-27 15:02:53 -0800930
Andreas Hubera1f8ab02012-11-30 10:53:22 -0800931 mDeferredActions.push_back(
932 new SimpleAction(&NuPlayer::performReset));
Andreas Huberb58ce9f2011-11-28 16:27:35 -0800933
Andreas Hubera1f8ab02012-11-30 10:53:22 -0800934 processDeferredActions();
Andreas Huber1aef2112011-01-04 14:01:29 -0800935 break;
936 }
937
Andreas Huber43c3e6c2011-01-05 12:17:08 -0800938 case kWhatSeek:
939 {
940 int64_t seekTimeUs;
941 CHECK(msg->findInt64("seekTimeUs", &seekTimeUs));
942
Andreas Hubera1f8ab02012-11-30 10:53:22 -0800943 ALOGV("kWhatSeek seekTimeUs=%lld us", seekTimeUs);
Andreas Huber43c3e6c2011-01-05 12:17:08 -0800944
Andreas Hubera1f8ab02012-11-30 10:53:22 -0800945 mDeferredActions.push_back(
946 new SimpleAction(&NuPlayer::performDecoderFlush));
Andreas Huber43c3e6c2011-01-05 12:17:08 -0800947
Andreas Hubera1f8ab02012-11-30 10:53:22 -0800948 mDeferredActions.push_back(new SeekAction(seekTimeUs));
Andreas Huber43c3e6c2011-01-05 12:17:08 -0800949
Andreas Hubera1f8ab02012-11-30 10:53:22 -0800950 processDeferredActions();
Andreas Huber43c3e6c2011-01-05 12:17:08 -0800951 break;
952 }
953
Andreas Huberb4082222011-01-20 15:23:04 -0800954 case kWhatPause:
955 {
956 CHECK(mRenderer != NULL);
Roger Jönssonfba60da2013-01-21 17:15:45 +0100957 mSource->pause();
Andreas Huberb4082222011-01-20 15:23:04 -0800958 mRenderer->pause();
959 break;
960 }
961
962 case kWhatResume:
963 {
964 CHECK(mRenderer != NULL);
Roger Jönssonfba60da2013-01-21 17:15:45 +0100965 mSource->resume();
Andreas Huberb4082222011-01-20 15:23:04 -0800966 mRenderer->resume();
967 break;
968 }
969
Andreas Huberb5f25f02013-02-05 10:14:26 -0800970 case kWhatSourceNotify:
971 {
Andreas Huber9575c962013-02-05 13:59:56 -0800972 onSourceNotify(msg);
Andreas Huberb5f25f02013-02-05 10:14:26 -0800973 break;
974 }
975
Chong Zhanga7fa1d92014-06-11 14:49:23 -0700976 case kWhatClosedCaptionNotify:
977 {
978 onClosedCaptionNotify(msg);
979 break;
980 }
981
Andreas Huberf9334412010-12-15 15:17:42 -0800982 default:
983 TRESPASS();
984 break;
985 }
986}
987
Andreas Huber3831a062010-12-21 10:22:33 -0800988void NuPlayer::finishFlushIfPossible() {
Wei Jia53904f32014-07-29 10:22:53 -0700989 if (mFlushingAudio != NONE && mFlushingAudio != FLUSHED
990 && mFlushingAudio != SHUT_DOWN) {
Andreas Huber3831a062010-12-21 10:22:33 -0800991 return;
992 }
993
Wei Jia53904f32014-07-29 10:22:53 -0700994 if (mFlushingVideo != NONE && mFlushingVideo != FLUSHED
995 && mFlushingVideo != SHUT_DOWN) {
Andreas Huber3831a062010-12-21 10:22:33 -0800996 return;
997 }
998
Steve Block3856b092011-10-20 11:56:00 +0100999 ALOGV("both audio and video are flushed now.");
Andreas Huber3831a062010-12-21 10:22:33 -08001000
Phil Burk9f526492014-09-03 15:04:12 -07001001 mPendingAudioAccessUnit.clear();
Phil Burkc5cc2e22014-09-09 20:08:39 -07001002 mAggregateBuffer.clear();
Phil Burk9f526492014-09-03 15:04:12 -07001003
Andreas Huber6e3d3112011-11-28 12:36:11 -08001004 if (mTimeDiscontinuityPending) {
1005 mRenderer->signalTimeDiscontinuity();
1006 mTimeDiscontinuityPending = false;
1007 }
Andreas Huber3831a062010-12-21 10:22:33 -08001008
Wei Jia53904f32014-07-29 10:22:53 -07001009 if (mAudioDecoder != NULL && mFlushingAudio == FLUSHED) {
Andreas Huber3831a062010-12-21 10:22:33 -08001010 mAudioDecoder->signalResume();
1011 }
1012
Wei Jia53904f32014-07-29 10:22:53 -07001013 if (mVideoDecoder != NULL && mFlushingVideo == FLUSHED) {
Andreas Huber3831a062010-12-21 10:22:33 -08001014 mVideoDecoder->signalResume();
1015 }
1016
1017 mFlushingAudio = NONE;
1018 mFlushingVideo = NONE;
Andreas Huber3831a062010-12-21 10:22:33 -08001019
Andreas Hubera1f8ab02012-11-30 10:53:22 -08001020 processDeferredActions();
Andreas Huber1aef2112011-01-04 14:01:29 -08001021}
1022
1023void NuPlayer::postScanSources() {
1024 if (mScanSourcesPending) {
1025 return;
1026 }
1027
1028 sp<AMessage> msg = new AMessage(kWhatScanSources, id());
1029 msg->setInt32("generation", mScanSourcesGeneration);
1030 msg->post();
1031
1032 mScanSourcesPending = true;
1033}
1034
Andy Hung282a7e32014-08-14 15:56:34 -07001035void NuPlayer::openAudioSink(const sp<AMessage> &format, bool offloadOnly) {
1036 ALOGV("openAudioSink: offloadOnly(%d) mOffloadAudio(%d)",
1037 offloadOnly, mOffloadAudio);
1038 bool audioSinkChanged = false;
1039
1040 int32_t numChannels;
1041 CHECK(format->findInt32("channel-count", &numChannels));
1042
1043 int32_t channelMask;
1044 if (!format->findInt32("channel-mask", &channelMask)) {
1045 // signal to the AudioSink to derive the mask from count.
1046 channelMask = CHANNEL_MASK_USE_CHANNEL_ORDER;
1047 }
1048
1049 int32_t sampleRate;
1050 CHECK(format->findInt32("sample-rate", &sampleRate));
1051
1052 uint32_t flags;
1053 int64_t durationUs;
1054 // FIXME: we should handle the case where the video decoder
1055 // is created after we receive the format change indication.
1056 // Current code will just make that we select deep buffer
1057 // with video which should not be a problem as it should
1058 // not prevent from keeping A/V sync.
1059 if (mVideoDecoder == NULL &&
1060 mSource->getDuration(&durationUs) == OK &&
1061 durationUs
1062 > AUDIO_SINK_MIN_DEEP_BUFFER_DURATION_US) {
1063 flags = AUDIO_OUTPUT_FLAG_DEEP_BUFFER;
1064 } else {
1065 flags = AUDIO_OUTPUT_FLAG_NONE;
1066 }
1067
1068 if (mOffloadAudio) {
1069 audio_format_t audioFormat = AUDIO_FORMAT_PCM_16_BIT;
1070 AString mime;
1071 CHECK(format->findString("mime", &mime));
1072 status_t err = mapMimeToAudioFormat(audioFormat, mime.c_str());
1073
1074 if (err != OK) {
1075 ALOGE("Couldn't map mime \"%s\" to a valid "
1076 "audio_format", mime.c_str());
1077 mOffloadAudio = false;
1078 } else {
1079 ALOGV("Mime \"%s\" mapped to audio_format 0x%x",
1080 mime.c_str(), audioFormat);
1081
1082 int avgBitRate = -1;
1083 format->findInt32("bit-rate", &avgBitRate);
1084
1085 int32_t aacProfile = -1;
1086 if (audioFormat == AUDIO_FORMAT_AAC
1087 && format->findInt32("aac-profile", &aacProfile)) {
1088 // Redefine AAC format as per aac profile
1089 mapAACProfileToAudioFormat(
1090 audioFormat,
1091 aacProfile);
1092 }
1093
1094 audio_offload_info_t offloadInfo = AUDIO_INFO_INITIALIZER;
1095 offloadInfo.duration_us = -1;
1096 format->findInt64(
1097 "durationUs", &offloadInfo.duration_us);
1098 offloadInfo.sample_rate = sampleRate;
1099 offloadInfo.channel_mask = channelMask;
1100 offloadInfo.format = audioFormat;
1101 offloadInfo.stream_type = AUDIO_STREAM_MUSIC;
1102 offloadInfo.bit_rate = avgBitRate;
1103 offloadInfo.has_video = (mVideoDecoder != NULL);
1104 offloadInfo.is_streaming = true;
1105
1106 if (memcmp(&mCurrentOffloadInfo, &offloadInfo, sizeof(offloadInfo)) == 0) {
1107 ALOGV("openAudioSink: no change in offload mode");
1108 return; // no change from previous configuration, everything ok.
1109 }
1110 ALOGV("openAudioSink: try to open AudioSink in offload mode");
1111 flags |= AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD;
Ronghua Wu1ffb5382014-08-18 15:57:03 -07001112 flags &= ~AUDIO_OUTPUT_FLAG_DEEP_BUFFER;
Andy Hung282a7e32014-08-14 15:56:34 -07001113 audioSinkChanged = true;
1114 mAudioSink->close();
1115 err = mAudioSink->open(
1116 sampleRate,
1117 numChannels,
1118 (audio_channel_mask_t)channelMask,
1119 audioFormat,
1120 8 /* bufferCount */,
1121 &NuPlayer::Renderer::AudioSinkCallback,
1122 mRenderer.get(),
1123 (audio_output_flags_t)flags,
1124 &offloadInfo);
1125
1126 if (err == OK) {
1127 // If the playback is offloaded to h/w, we pass
1128 // the HAL some metadata information.
1129 // We don't want to do this for PCM because it
1130 // will be going through the AudioFlinger mixer
1131 // before reaching the hardware.
1132 sp<MetaData> audioMeta =
1133 mSource->getFormatMeta(true /* audio */);
1134 sendMetaDataToHal(mAudioSink, audioMeta);
1135 mCurrentOffloadInfo = offloadInfo;
1136 err = mAudioSink->start();
1137 ALOGV_IF(err == OK, "openAudioSink: offload succeeded");
1138 }
1139 if (err != OK) {
1140 // Clean up, fall back to non offload mode.
1141 mAudioSink->close();
1142 mRenderer->signalDisableOffloadAudio();
1143 mOffloadAudio = false;
1144 mCurrentOffloadInfo = AUDIO_INFO_INITIALIZER;
1145 ALOGV("openAudioSink: offload failed");
1146 }
1147 }
1148 }
1149 if (!offloadOnly && !mOffloadAudio) {
1150 flags &= ~AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD;
1151 ALOGV("openAudioSink: open AudioSink in NON-offload mode");
1152
1153 audioSinkChanged = true;
1154 mAudioSink->close();
1155 mCurrentOffloadInfo = AUDIO_INFO_INITIALIZER;
1156 CHECK_EQ(mAudioSink->open(
1157 sampleRate,
1158 numChannels,
1159 (audio_channel_mask_t)channelMask,
1160 AUDIO_FORMAT_PCM_16_BIT,
1161 8 /* bufferCount */,
1162 NULL,
1163 NULL,
1164 (audio_output_flags_t)flags),
1165 (status_t)OK);
1166 mAudioSink->start();
1167 }
1168 if (audioSinkChanged) {
1169 mRenderer->signalAudioSinkChanged();
1170 }
1171}
1172
1173void NuPlayer::closeAudioSink() {
1174 mAudioSink->close();
1175 mCurrentOffloadInfo = AUDIO_INFO_INITIALIZER;
1176}
1177
Andreas Huber5bc087c2010-12-23 10:27:40 -08001178status_t NuPlayer::instantiateDecoder(bool audio, sp<Decoder> *decoder) {
Andreas Huberf9334412010-12-15 15:17:42 -08001179 if (*decoder != NULL) {
1180 return OK;
1181 }
1182
Andreas Huber84066782011-08-16 09:34:26 -07001183 sp<AMessage> format = mSource->getFormat(audio);
Andreas Huberf9334412010-12-15 15:17:42 -08001184
Andreas Huber84066782011-08-16 09:34:26 -07001185 if (format == NULL) {
Andreas Huberf9334412010-12-15 15:17:42 -08001186 return -EWOULDBLOCK;
1187 }
1188
Andreas Huber3fe62152011-09-16 15:09:22 -07001189 if (!audio) {
Andreas Huber84066782011-08-16 09:34:26 -07001190 AString mime;
1191 CHECK(format->findString("mime", &mime));
1192 mVideoIsAVC = !strcasecmp(MEDIA_MIMETYPE_VIDEO_AVC, mime.c_str());
Chong Zhanga7fa1d92014-06-11 14:49:23 -07001193
1194 sp<AMessage> ccNotify = new AMessage(kWhatClosedCaptionNotify, id());
1195 mCCDecoder = new CCDecoder(ccNotify);
Lajos Molnar09524832014-07-17 14:29:51 -07001196
1197 if (mSourceFlags & Source::FLAG_SECURE) {
1198 format->setInt32("secure", true);
1199 }
Andreas Huber3fe62152011-09-16 15:09:22 -07001200 }
1201
Wei Jiabc2fb722014-07-08 16:37:57 -07001202 if (audio) {
Wei Jia88703c32014-08-06 11:24:07 -07001203 sp<AMessage> notify = new AMessage(kWhatAudioNotify, id());
1204 ++mAudioDecoderGeneration;
1205 notify->setInt32("generation", mAudioDecoderGeneration);
1206
Wei Jiabc2fb722014-07-08 16:37:57 -07001207 if (mOffloadAudio) {
1208 *decoder = new DecoderPassThrough(notify);
1209 } else {
1210 *decoder = new Decoder(notify);
1211 }
1212 } else {
Wei Jia88703c32014-08-06 11:24:07 -07001213 sp<AMessage> notify = new AMessage(kWhatVideoNotify, id());
1214 ++mVideoDecoderGeneration;
1215 notify->setInt32("generation", mVideoDecoderGeneration);
1216
Wei Jiabc2fb722014-07-08 16:37:57 -07001217 *decoder = new Decoder(notify, mNativeWindow);
1218 }
Lajos Molnar1cd13982014-01-17 15:12:51 -08001219 (*decoder)->init();
Andreas Huber84066782011-08-16 09:34:26 -07001220 (*decoder)->configure(format);
Andreas Huberf9334412010-12-15 15:17:42 -08001221
Lajos Molnar09524832014-07-17 14:29:51 -07001222 // allocate buffers to decrypt widevine source buffers
1223 if (!audio && (mSourceFlags & Source::FLAG_SECURE)) {
1224 Vector<sp<ABuffer> > inputBufs;
1225 CHECK_EQ((*decoder)->getInputBuffers(&inputBufs), (status_t)OK);
1226
1227 Vector<MediaBuffer *> mediaBufs;
1228 for (size_t i = 0; i < inputBufs.size(); i++) {
1229 const sp<ABuffer> &buffer = inputBufs[i];
1230 MediaBuffer *mbuf = new MediaBuffer(buffer->data(), buffer->size());
1231 mediaBufs.push(mbuf);
1232 }
1233
1234 status_t err = mSource->setBuffers(audio, mediaBufs);
1235 if (err != OK) {
1236 for (size_t i = 0; i < mediaBufs.size(); ++i) {
1237 mediaBufs[i]->release();
1238 }
1239 mediaBufs.clear();
1240 ALOGE("Secure source didn't support secure mediaBufs.");
1241 return err;
1242 }
1243 }
Andreas Huberf9334412010-12-15 15:17:42 -08001244 return OK;
1245}
1246
1247status_t NuPlayer::feedDecoderInputData(bool audio, const sp<AMessage> &msg) {
1248 sp<AMessage> reply;
1249 CHECK(msg->findMessage("reply", &reply));
1250
Wei Jia53904f32014-07-29 10:22:53 -07001251 if ((audio && mFlushingAudio != NONE)
Wei Jiaf702d042014-09-09 12:08:47 -07001252 || (!audio && mFlushingVideo != NONE)
1253 || mSource == NULL) {
Wei Jiab189a5b2014-08-07 06:11:39 +00001254 reply->setInt32("err", INFO_DISCONTINUITY);
1255 reply->post();
1256 return OK;
Andreas Huberf9334412010-12-15 15:17:42 -08001257 }
1258
1259 sp<ABuffer> accessUnit;
Andreas Huberf9334412010-12-15 15:17:42 -08001260
Phil Burk9f526492014-09-03 15:04:12 -07001261 // Aggregate smaller buffers into a larger buffer.
1262 // The goal is to reduce power consumption.
1263 // Unfortunately this does not work with the software AAC decoder.
Phil Burkc5cc2e22014-09-09 20:08:39 -07001264 bool doBufferAggregation = (audio && mOffloadAudio);;
Phil Burk9f526492014-09-03 15:04:12 -07001265 bool needMoreData = false;
Phil Burk9f526492014-09-03 15:04:12 -07001266
Andreas Huber3fe62152011-09-16 15:09:22 -07001267 bool dropAccessUnit;
1268 do {
Phil Burk9f526492014-09-03 15:04:12 -07001269 status_t err;
1270 // Did we save an accessUnit earlier because of a discontinuity?
1271 if (audio && (mPendingAudioAccessUnit != NULL)) {
1272 accessUnit = mPendingAudioAccessUnit;
1273 mPendingAudioAccessUnit.clear();
1274 err = mPendingAudioErr;
1275 ALOGV("feedDecoderInputData() use mPendingAudioAccessUnit");
1276 } else {
1277 err = mSource->dequeueAccessUnit(audio, &accessUnit);
1278 }
Andreas Huber5bc087c2010-12-23 10:27:40 -08001279
Andreas Huber3fe62152011-09-16 15:09:22 -07001280 if (err == -EWOULDBLOCK) {
Phil Burkc5cc2e22014-09-09 20:08:39 -07001281 return err;
Andreas Huber3fe62152011-09-16 15:09:22 -07001282 } else if (err != OK) {
1283 if (err == INFO_DISCONTINUITY) {
Phil Burkc5cc2e22014-09-09 20:08:39 -07001284 if (mAggregateBuffer != NULL) {
Phil Burk9f526492014-09-03 15:04:12 -07001285 // We already have some data so save this for later.
1286 mPendingAudioErr = err;
1287 mPendingAudioAccessUnit = accessUnit;
1288 accessUnit.clear();
1289 ALOGD("feedDecoderInputData() save discontinuity for later");
1290 break;
1291 }
Andreas Huber3fe62152011-09-16 15:09:22 -07001292 int32_t type;
1293 CHECK(accessUnit->meta()->findInt32("discontinuity", &type));
Andreas Huber53df1a42010-12-22 10:03:04 -08001294
Andreas Huber3fe62152011-09-16 15:09:22 -07001295 bool formatChange =
Andreas Huber6e3d3112011-11-28 12:36:11 -08001296 (audio &&
1297 (type & ATSParser::DISCONTINUITY_AUDIO_FORMAT))
1298 || (!audio &&
1299 (type & ATSParser::DISCONTINUITY_VIDEO_FORMAT));
Andreas Huber53df1a42010-12-22 10:03:04 -08001300
Andreas Huber6e3d3112011-11-28 12:36:11 -08001301 bool timeChange = (type & ATSParser::DISCONTINUITY_TIME) != 0;
1302
Steve Blockdf64d152012-01-04 20:05:49 +00001303 ALOGI("%s discontinuity (formatChange=%d, time=%d)",
Andreas Huber6e3d3112011-11-28 12:36:11 -08001304 audio ? "audio" : "video", formatChange, timeChange);
Andreas Huber32f3cef2011-03-02 15:34:46 -08001305
Andreas Huber3fe62152011-09-16 15:09:22 -07001306 if (audio) {
1307 mSkipRenderingAudioUntilMediaTimeUs = -1;
1308 } else {
1309 mSkipRenderingVideoUntilMediaTimeUs = -1;
1310 }
Andreas Huber32f3cef2011-03-02 15:34:46 -08001311
Andreas Huber6e3d3112011-11-28 12:36:11 -08001312 if (timeChange) {
1313 sp<AMessage> extra;
1314 if (accessUnit->meta()->findMessage("extra", &extra)
1315 && extra != NULL) {
1316 int64_t resumeAtMediaTimeUs;
1317 if (extra->findInt64(
1318 "resume-at-mediatimeUs", &resumeAtMediaTimeUs)) {
Steve Blockdf64d152012-01-04 20:05:49 +00001319 ALOGI("suppressing rendering of %s until %lld us",
Andreas Huber6e3d3112011-11-28 12:36:11 -08001320 audio ? "audio" : "video", resumeAtMediaTimeUs);
Andreas Huber3fe62152011-09-16 15:09:22 -07001321
Andreas Huber6e3d3112011-11-28 12:36:11 -08001322 if (audio) {
1323 mSkipRenderingAudioUntilMediaTimeUs =
1324 resumeAtMediaTimeUs;
1325 } else {
1326 mSkipRenderingVideoUntilMediaTimeUs =
1327 resumeAtMediaTimeUs;
1328 }
Andreas Huber3fe62152011-09-16 15:09:22 -07001329 }
Andreas Huber32f3cef2011-03-02 15:34:46 -08001330 }
1331 }
Andreas Huber3fe62152011-09-16 15:09:22 -07001332
Andreas Huber6e3d3112011-11-28 12:36:11 -08001333 mTimeDiscontinuityPending =
1334 mTimeDiscontinuityPending || timeChange;
1335
Lajos Molnar87603c02014-08-20 19:25:30 -07001336 bool seamlessFormatChange = false;
1337 sp<AMessage> newFormat = mSource->getFormat(audio);
1338 if (formatChange) {
1339 seamlessFormatChange =
1340 getDecoder(audio)->supportsSeamlessFormatChange(newFormat);
1341 // treat seamless format change separately
1342 formatChange = !seamlessFormatChange;
1343 }
1344 bool shutdownOrFlush = formatChange || timeChange;
1345
1346 // We want to queue up scan-sources only once per discontinuity.
1347 // We control this by doing it only if neither audio nor video are
1348 // flushing or shutting down. (After handling 1st discontinuity, one
1349 // of the flushing states will not be NONE.)
1350 // No need to scan sources if this discontinuity does not result
1351 // in a flush or shutdown, as the flushing state will stay NONE.
1352 if (mFlushingAudio == NONE && mFlushingVideo == NONE &&
1353 shutdownOrFlush) {
Robert Shiha2981012014-07-30 17:41:24 -07001354 // And we'll resume scanning sources once we're done
1355 // flushing.
1356 mDeferredActions.push_front(
1357 new SimpleAction(
1358 &NuPlayer::performScanSources));
1359 }
1360
Lajos Molnar87603c02014-08-20 19:25:30 -07001361 if (formatChange /* not seamless */) {
1362 // must change decoder
1363 flushDecoder(audio, /* needShutdown = */ true);
1364 } else if (timeChange) {
1365 // need to flush
1366 flushDecoder(audio, /* needShutdown = */ false, newFormat);
1367 err = OK;
1368 } else if (seamlessFormatChange) {
1369 // reuse existing decoder and don't flush
1370 updateDecoderFormatWithoutFlush(audio, newFormat);
1371 err = OK;
Andreas Huber6e3d3112011-11-28 12:36:11 -08001372 } else {
1373 // This stream is unaffected by the discontinuity
Andreas Huber6e3d3112011-11-28 12:36:11 -08001374 return -EWOULDBLOCK;
1375 }
Andreas Huber32f3cef2011-03-02 15:34:46 -08001376 }
1377
Andreas Huber3fe62152011-09-16 15:09:22 -07001378 reply->setInt32("err", err);
1379 reply->post();
1380 return OK;
Andreas Huberf9334412010-12-15 15:17:42 -08001381 }
1382
Andreas Huber3fe62152011-09-16 15:09:22 -07001383 if (!audio) {
1384 ++mNumFramesTotal;
1385 }
1386
1387 dropAccessUnit = false;
1388 if (!audio
Lajos Molnar09524832014-07-17 14:29:51 -07001389 && !(mSourceFlags & Source::FLAG_SECURE)
Andreas Huber3fe62152011-09-16 15:09:22 -07001390 && mVideoLateByUs > 100000ll
1391 && mVideoIsAVC
1392 && !IsAVCReferenceFrame(accessUnit)) {
1393 dropAccessUnit = true;
1394 ++mNumFramesDropped;
1395 }
Phil Burk9f526492014-09-03 15:04:12 -07001396
1397 size_t smallSize = accessUnit->size();
1398 needMoreData = false;
Phil Burkc5cc2e22014-09-09 20:08:39 -07001399 if (doBufferAggregation && (mAggregateBuffer == NULL)
Phil Burk9f526492014-09-03 15:04:12 -07001400 // Don't bother if only room for a few small buffers.
Phil Burkc5cc2e22014-09-09 20:08:39 -07001401 && (smallSize < (kAggregateBufferSizeBytes / 3))) {
Phil Burk9f526492014-09-03 15:04:12 -07001402 // Create a larger buffer for combining smaller buffers from the extractor.
Phil Burkc5cc2e22014-09-09 20:08:39 -07001403 mAggregateBuffer = new ABuffer(kAggregateBufferSizeBytes);
1404 mAggregateBuffer->setRange(0, 0); // start empty
Phil Burk9f526492014-09-03 15:04:12 -07001405 }
1406
Phil Burkc5cc2e22014-09-09 20:08:39 -07001407 if (mAggregateBuffer != NULL) {
Phil Burk9f526492014-09-03 15:04:12 -07001408 int64_t timeUs;
Phil Burkc5cc2e22014-09-09 20:08:39 -07001409 int64_t dummy;
Phil Burk9f526492014-09-03 15:04:12 -07001410 bool smallTimestampValid = accessUnit->meta()->findInt64("timeUs", &timeUs);
Phil Burkc5cc2e22014-09-09 20:08:39 -07001411 bool bigTimestampValid = mAggregateBuffer->meta()->findInt64("timeUs", &dummy);
Phil Burk9f526492014-09-03 15:04:12 -07001412 // Will the smaller buffer fit?
Phil Burkc5cc2e22014-09-09 20:08:39 -07001413 size_t bigSize = mAggregateBuffer->size();
1414 size_t roomLeft = mAggregateBuffer->capacity() - bigSize;
Phil Burk9f526492014-09-03 15:04:12 -07001415 // Should we save this small buffer for the next big buffer?
1416 // If the first small buffer did not have a timestamp then save
1417 // any buffer that does have a timestamp until the next big buffer.
1418 if ((smallSize > roomLeft)
Phil Burkc5cc2e22014-09-09 20:08:39 -07001419 || (!bigTimestampValid && (bigSize > 0) && smallTimestampValid)) {
Phil Burk9f526492014-09-03 15:04:12 -07001420 mPendingAudioErr = err;
1421 mPendingAudioAccessUnit = accessUnit;
1422 accessUnit.clear();
1423 } else {
Phil Burkc5cc2e22014-09-09 20:08:39 -07001424 // Grab time from first small buffer if available.
1425 if ((bigSize == 0) && smallTimestampValid) {
1426 mAggregateBuffer->meta()->setInt64("timeUs", timeUs);
1427 }
Phil Burk9f526492014-09-03 15:04:12 -07001428 // Append small buffer to the bigger buffer.
Phil Burkc5cc2e22014-09-09 20:08:39 -07001429 memcpy(mAggregateBuffer->base() + bigSize, accessUnit->data(), smallSize);
Phil Burk9f526492014-09-03 15:04:12 -07001430 bigSize += smallSize;
Phil Burkc5cc2e22014-09-09 20:08:39 -07001431 mAggregateBuffer->setRange(0, bigSize);
Phil Burk9f526492014-09-03 15:04:12 -07001432
Phil Burkc5cc2e22014-09-09 20:08:39 -07001433 // Keep looping until we run out of room in the mAggregateBuffer.
Phil Burk9f526492014-09-03 15:04:12 -07001434 needMoreData = true;
1435
Phil Burkc5cc2e22014-09-09 20:08:39 -07001436 ALOGV("feedDecoderInputData() smallSize = %zu, bigSize = %zu, capacity = %zu",
1437 smallSize, bigSize, mAggregateBuffer->capacity());
Phil Burk9f526492014-09-03 15:04:12 -07001438 }
1439 }
1440 } while (dropAccessUnit || needMoreData);
Andreas Huberf9334412010-12-15 15:17:42 -08001441
Steve Block3856b092011-10-20 11:56:00 +01001442 // ALOGV("returned a valid buffer of %s data", audio ? "audio" : "video");
Andreas Huberf9334412010-12-15 15:17:42 -08001443
1444#if 0
1445 int64_t mediaTimeUs;
1446 CHECK(accessUnit->meta()->findInt64("timeUs", &mediaTimeUs));
Steve Block3856b092011-10-20 11:56:00 +01001447 ALOGV("feeding %s input buffer at media time %.2f secs",
Andreas Huberf9334412010-12-15 15:17:42 -08001448 audio ? "audio" : "video",
1449 mediaTimeUs / 1E6);
1450#endif
1451
Chong Zhanga7fa1d92014-06-11 14:49:23 -07001452 if (!audio) {
1453 mCCDecoder->decode(accessUnit);
1454 }
1455
Phil Burkc5cc2e22014-09-09 20:08:39 -07001456 if (mAggregateBuffer != NULL) {
1457 ALOGV("feedDecoderInputData() reply with aggregated buffer, %zu",
1458 mAggregateBuffer->size());
1459 reply->setBuffer("buffer", mAggregateBuffer);
1460 mAggregateBuffer.clear();
Phil Burk9f526492014-09-03 15:04:12 -07001461 } else {
1462 reply->setBuffer("buffer", accessUnit);
1463 }
1464
Andreas Huberf9334412010-12-15 15:17:42 -08001465 reply->post();
1466
1467 return OK;
1468}
1469
1470void NuPlayer::renderBuffer(bool audio, const sp<AMessage> &msg) {
Steve Block3856b092011-10-20 11:56:00 +01001471 // ALOGV("renderBuffer %s", audio ? "audio" : "video");
Andreas Huberf9334412010-12-15 15:17:42 -08001472
1473 sp<AMessage> reply;
1474 CHECK(msg->findMessage("reply", &reply));
1475
Wei Jia53904f32014-07-29 10:22:53 -07001476 if ((audio && mFlushingAudio != NONE)
1477 || (!audio && mFlushingVideo != NONE)) {
Andreas Huber18ac5402011-08-31 15:04:25 -07001478 // We're currently attempting to flush the decoder, in order
1479 // to complete this, the decoder wants all its buffers back,
1480 // so we don't want any output buffers it sent us (from before
1481 // we initiated the flush) to be stuck in the renderer's queue.
1482
Steve Block3856b092011-10-20 11:56:00 +01001483 ALOGV("we're still flushing the %s decoder, sending its output buffer"
Andreas Huber18ac5402011-08-31 15:04:25 -07001484 " right back.", audio ? "audio" : "video");
1485
1486 reply->post();
1487 return;
1488 }
1489
Andreas Huber2d8bedd2012-02-21 14:38:23 -08001490 sp<ABuffer> buffer;
1491 CHECK(msg->findBuffer("buffer", &buffer));
Andreas Huberf9334412010-12-15 15:17:42 -08001492
Chong Zhanga7fa1d92014-06-11 14:49:23 -07001493 int64_t mediaTimeUs;
1494 CHECK(buffer->meta()->findInt64("timeUs", &mediaTimeUs));
1495
Andreas Huber32f3cef2011-03-02 15:34:46 -08001496 int64_t &skipUntilMediaTimeUs =
1497 audio
1498 ? mSkipRenderingAudioUntilMediaTimeUs
1499 : mSkipRenderingVideoUntilMediaTimeUs;
1500
1501 if (skipUntilMediaTimeUs >= 0) {
Andreas Huber32f3cef2011-03-02 15:34:46 -08001502
1503 if (mediaTimeUs < skipUntilMediaTimeUs) {
Steve Block3856b092011-10-20 11:56:00 +01001504 ALOGV("dropping %s buffer at time %lld as requested.",
Andreas Huber32f3cef2011-03-02 15:34:46 -08001505 audio ? "audio" : "video",
1506 mediaTimeUs);
1507
1508 reply->post();
1509 return;
1510 }
1511
1512 skipUntilMediaTimeUs = -1;
1513 }
1514
Chong Zhanga7fa1d92014-06-11 14:49:23 -07001515 if (!audio && mCCDecoder->isSelected()) {
1516 mCCDecoder->display(mediaTimeUs);
1517 }
1518
Andreas Huberf9334412010-12-15 15:17:42 -08001519 mRenderer->queueBuffer(audio, buffer, reply);
1520}
1521
Chong Zhangced1c2f2014-08-08 15:22:35 -07001522void NuPlayer::updateVideoSize(
1523 const sp<AMessage> &inputFormat,
1524 const sp<AMessage> &outputFormat) {
1525 if (inputFormat == NULL) {
1526 ALOGW("Unknown video size, reporting 0x0!");
1527 notifyListener(MEDIA_SET_VIDEO_SIZE, 0, 0);
1528 return;
1529 }
1530
1531 int32_t displayWidth, displayHeight;
1532 int32_t cropLeft, cropTop, cropRight, cropBottom;
1533
1534 if (outputFormat != NULL) {
1535 int32_t width, height;
1536 CHECK(outputFormat->findInt32("width", &width));
1537 CHECK(outputFormat->findInt32("height", &height));
1538
1539 int32_t cropLeft, cropTop, cropRight, cropBottom;
1540 CHECK(outputFormat->findRect(
1541 "crop",
1542 &cropLeft, &cropTop, &cropRight, &cropBottom));
1543
1544 displayWidth = cropRight - cropLeft + 1;
1545 displayHeight = cropBottom - cropTop + 1;
1546
1547 ALOGV("Video output format changed to %d x %d "
1548 "(crop: %d x %d @ (%d, %d))",
1549 width, height,
1550 displayWidth,
1551 displayHeight,
1552 cropLeft, cropTop);
1553 } else {
1554 CHECK(inputFormat->findInt32("width", &displayWidth));
1555 CHECK(inputFormat->findInt32("height", &displayHeight));
1556
1557 ALOGV("Video input format %d x %d", displayWidth, displayHeight);
1558 }
1559
1560 // Take into account sample aspect ratio if necessary:
1561 int32_t sarWidth, sarHeight;
1562 if (inputFormat->findInt32("sar-width", &sarWidth)
1563 && inputFormat->findInt32("sar-height", &sarHeight)) {
1564 ALOGV("Sample aspect ratio %d : %d", sarWidth, sarHeight);
1565
1566 displayWidth = (displayWidth * sarWidth) / sarHeight;
1567
1568 ALOGV("display dimensions %d x %d", displayWidth, displayHeight);
1569 }
1570
1571 int32_t rotationDegrees;
1572 if (!inputFormat->findInt32("rotation-degrees", &rotationDegrees)) {
1573 rotationDegrees = 0;
1574 }
1575
1576 if (rotationDegrees == 90 || rotationDegrees == 270) {
1577 int32_t tmp = displayWidth;
1578 displayWidth = displayHeight;
1579 displayHeight = tmp;
1580 }
1581
1582 notifyListener(
1583 MEDIA_SET_VIDEO_SIZE,
1584 displayWidth,
1585 displayHeight);
1586}
1587
Chong Zhangdcb89b32013-08-06 09:44:47 -07001588void NuPlayer::notifyListener(int msg, int ext1, int ext2, const Parcel *in) {
Andreas Huber43c3e6c2011-01-05 12:17:08 -08001589 if (mDriver == NULL) {
Andreas Huberf9334412010-12-15 15:17:42 -08001590 return;
1591 }
1592
Andreas Huber43c3e6c2011-01-05 12:17:08 -08001593 sp<NuPlayerDriver> driver = mDriver.promote();
Andreas Huberf9334412010-12-15 15:17:42 -08001594
Andreas Huber43c3e6c2011-01-05 12:17:08 -08001595 if (driver == NULL) {
Andreas Huberf9334412010-12-15 15:17:42 -08001596 return;
1597 }
1598
Chong Zhangdcb89b32013-08-06 09:44:47 -07001599 driver->notifyListener(msg, ext1, ext2, in);
Andreas Huberf9334412010-12-15 15:17:42 -08001600}
1601
Lajos Molnar87603c02014-08-20 19:25:30 -07001602void NuPlayer::flushDecoder(
1603 bool audio, bool needShutdown, const sp<AMessage> &newFormat) {
Andreas Huber14f76722013-01-15 09:04:18 -08001604 ALOGV("[%s] flushDecoder needShutdown=%d",
1605 audio ? "audio" : "video", needShutdown);
1606
Lajos Molnar87603c02014-08-20 19:25:30 -07001607 const sp<Decoder> &decoder = getDecoder(audio);
1608 if (decoder == NULL) {
Steve Blockdf64d152012-01-04 20:05:49 +00001609 ALOGI("flushDecoder %s without decoder present",
Andreas Huber6e3d3112011-11-28 12:36:11 -08001610 audio ? "audio" : "video");
Lajos Molnar87603c02014-08-20 19:25:30 -07001611 return;
Andreas Huber6e3d3112011-11-28 12:36:11 -08001612 }
1613
Andreas Huber1aef2112011-01-04 14:01:29 -08001614 // Make sure we don't continue to scan sources until we finish flushing.
1615 ++mScanSourcesGeneration;
Andreas Huber43c3e6c2011-01-05 12:17:08 -08001616 mScanSourcesPending = false;
Andreas Huber1aef2112011-01-04 14:01:29 -08001617
Lajos Molnar87603c02014-08-20 19:25:30 -07001618 decoder->signalFlush(newFormat);
Andreas Huber1aef2112011-01-04 14:01:29 -08001619 mRenderer->flush(audio);
1620
1621 FlushStatus newStatus =
1622 needShutdown ? FLUSHING_DECODER_SHUTDOWN : FLUSHING_DECODER;
1623
1624 if (audio) {
Wei Jia53904f32014-07-29 10:22:53 -07001625 ALOGE_IF(mFlushingAudio != NONE,
1626 "audio flushDecoder() is called in state %d", mFlushingAudio);
Andreas Huber1aef2112011-01-04 14:01:29 -08001627 mFlushingAudio = newStatus;
Andreas Huber1aef2112011-01-04 14:01:29 -08001628 } else {
Wei Jia53904f32014-07-29 10:22:53 -07001629 ALOGE_IF(mFlushingVideo != NONE,
1630 "video flushDecoder() is called in state %d", mFlushingVideo);
Andreas Huber1aef2112011-01-04 14:01:29 -08001631 mFlushingVideo = newStatus;
Chong Zhangb86e68f2014-08-01 13:46:53 -07001632
1633 if (mCCDecoder != NULL) {
1634 mCCDecoder->flush();
1635 }
Andreas Huber1aef2112011-01-04 14:01:29 -08001636 }
1637}
1638
Lajos Molnar87603c02014-08-20 19:25:30 -07001639void NuPlayer::updateDecoderFormatWithoutFlush(
1640 bool audio, const sp<AMessage> &format) {
1641 ALOGV("[%s] updateDecoderFormatWithoutFlush", audio ? "audio" : "video");
1642
1643 const sp<Decoder> &decoder = getDecoder(audio);
1644 if (decoder == NULL) {
1645 ALOGI("updateDecoderFormatWithoutFlush %s without decoder present",
1646 audio ? "audio" : "video");
1647 return;
1648 }
1649
1650 decoder->signalUpdateFormat(format);
1651}
1652
Chong Zhangced1c2f2014-08-08 15:22:35 -07001653void NuPlayer::queueDecoderShutdown(
1654 bool audio, bool video, const sp<AMessage> &reply) {
1655 ALOGI("queueDecoderShutdown audio=%d, video=%d", audio, video);
Andreas Huber84066782011-08-16 09:34:26 -07001656
Chong Zhangced1c2f2014-08-08 15:22:35 -07001657 mDeferredActions.push_back(
1658 new ShutdownDecoderAction(audio, video));
Andreas Huber84066782011-08-16 09:34:26 -07001659
Chong Zhangced1c2f2014-08-08 15:22:35 -07001660 mDeferredActions.push_back(
1661 new SimpleAction(&NuPlayer::performScanSources));
Andreas Huber84066782011-08-16 09:34:26 -07001662
Chong Zhangced1c2f2014-08-08 15:22:35 -07001663 mDeferredActions.push_back(new PostMessageAction(reply));
1664
1665 processDeferredActions();
Andreas Huber84066782011-08-16 09:34:26 -07001666}
1667
James Dong0d268a32012-08-31 12:18:27 -07001668status_t NuPlayer::setVideoScalingMode(int32_t mode) {
1669 mVideoScalingMode = mode;
Andreas Huber57a339c2012-12-03 11:18:00 -08001670 if (mNativeWindow != NULL) {
James Dong0d268a32012-08-31 12:18:27 -07001671 status_t ret = native_window_set_scaling_mode(
1672 mNativeWindow->getNativeWindow().get(), mVideoScalingMode);
1673 if (ret != OK) {
1674 ALOGE("Failed to set scaling mode (%d): %s",
1675 -ret, strerror(-ret));
1676 return ret;
1677 }
1678 }
1679 return OK;
1680}
1681
Chong Zhangdcb89b32013-08-06 09:44:47 -07001682status_t NuPlayer::getTrackInfo(Parcel* reply) const {
1683 sp<AMessage> msg = new AMessage(kWhatGetTrackInfo, id());
1684 msg->setPointer("reply", reply);
1685
1686 sp<AMessage> response;
1687 status_t err = msg->postAndAwaitResponse(&response);
1688 return err;
1689}
1690
Robert Shih7c4f0d72014-07-09 18:53:31 -07001691status_t NuPlayer::getSelectedTrack(int32_t type, Parcel* reply) const {
1692 sp<AMessage> msg = new AMessage(kWhatGetSelectedTrack, id());
1693 msg->setPointer("reply", reply);
1694 msg->setInt32("type", type);
1695
1696 sp<AMessage> response;
1697 status_t err = msg->postAndAwaitResponse(&response);
1698 if (err == OK && response != NULL) {
1699 CHECK(response->findInt32("err", &err));
1700 }
1701 return err;
1702}
1703
Chong Zhangdcb89b32013-08-06 09:44:47 -07001704status_t NuPlayer::selectTrack(size_t trackIndex, bool select) {
1705 sp<AMessage> msg = new AMessage(kWhatSelectTrack, id());
1706 msg->setSize("trackIndex", trackIndex);
1707 msg->setInt32("select", select);
1708
1709 sp<AMessage> response;
1710 status_t err = msg->postAndAwaitResponse(&response);
1711
Chong Zhang404fced2014-06-11 14:45:31 -07001712 if (err != OK) {
1713 return err;
1714 }
1715
1716 if (!response->findInt32("err", &err)) {
1717 err = OK;
1718 }
1719
Chong Zhangdcb89b32013-08-06 09:44:47 -07001720 return err;
1721}
1722
Marco Nelissenf0b72b52014-09-16 15:43:44 -07001723sp<MetaData> NuPlayer::getFileMeta() {
1724 return mSource->getFileFormatMeta();
1725}
1726
Andreas Huberb7c8e912012-11-27 15:02:53 -08001727void NuPlayer::schedulePollDuration() {
1728 sp<AMessage> msg = new AMessage(kWhatPollDuration, id());
1729 msg->setInt32("generation", mPollDurationGeneration);
1730 msg->post();
1731}
1732
1733void NuPlayer::cancelPollDuration() {
1734 ++mPollDurationGeneration;
1735}
1736
Andreas Hubera1f8ab02012-11-30 10:53:22 -08001737void NuPlayer::processDeferredActions() {
1738 while (!mDeferredActions.empty()) {
1739 // We won't execute any deferred actions until we're no longer in
1740 // an intermediate state, i.e. one more more decoders are currently
1741 // flushing or shutting down.
1742
Andreas Hubera1f8ab02012-11-30 10:53:22 -08001743 if (mFlushingAudio != NONE || mFlushingVideo != NONE) {
1744 // We're currently flushing, postpone the reset until that's
1745 // completed.
1746
1747 ALOGV("postponing action mFlushingAudio=%d, mFlushingVideo=%d",
1748 mFlushingAudio, mFlushingVideo);
1749
1750 break;
1751 }
1752
1753 sp<Action> action = *mDeferredActions.begin();
1754 mDeferredActions.erase(mDeferredActions.begin());
1755
1756 action->execute(this);
1757 }
1758}
1759
1760void NuPlayer::performSeek(int64_t seekTimeUs) {
1761 ALOGV("performSeek seekTimeUs=%lld us (%.2f secs)",
1762 seekTimeUs,
1763 seekTimeUs / 1E6);
1764
Andy Hungadf34bf2014-09-03 18:22:22 -07001765 if (mSource == NULL) {
1766 // This happens when reset occurs right before the loop mode
1767 // asynchronously seeks to the start of the stream.
1768 LOG_ALWAYS_FATAL_IF(mAudioDecoder != NULL || mVideoDecoder != NULL,
1769 "mSource is NULL and decoders not NULL audio(%p) video(%p)",
1770 mAudioDecoder.get(), mVideoDecoder.get());
1771 return;
1772 }
Andreas Hubera1f8ab02012-11-30 10:53:22 -08001773 mSource->seekTo(seekTimeUs);
Robert Shihd3b0bbb2014-07-23 15:00:25 -07001774 ++mTimedTextGeneration;
Andreas Hubera1f8ab02012-11-30 10:53:22 -08001775
1776 if (mDriver != NULL) {
1777 sp<NuPlayerDriver> driver = mDriver.promote();
1778 if (driver != NULL) {
1779 driver->notifyPosition(seekTimeUs);
1780 driver->notifySeekComplete();
1781 }
1782 }
1783
1784 // everything's flushed, continue playback.
1785}
1786
1787void NuPlayer::performDecoderFlush() {
1788 ALOGV("performDecoderFlush");
1789
Andreas Huberda9740e2013-04-16 10:54:03 -07001790 if (mAudioDecoder == NULL && mVideoDecoder == NULL) {
Andreas Hubera1f8ab02012-11-30 10:53:22 -08001791 return;
1792 }
1793
1794 mTimeDiscontinuityPending = true;
1795
1796 if (mAudioDecoder != NULL) {
1797 flushDecoder(true /* audio */, false /* needShutdown */);
1798 }
1799
1800 if (mVideoDecoder != NULL) {
1801 flushDecoder(false /* audio */, false /* needShutdown */);
1802 }
1803}
1804
Andreas Huber14f76722013-01-15 09:04:18 -08001805void NuPlayer::performDecoderShutdown(bool audio, bool video) {
1806 ALOGV("performDecoderShutdown audio=%d, video=%d", audio, video);
Andreas Hubera1f8ab02012-11-30 10:53:22 -08001807
Andreas Huber14f76722013-01-15 09:04:18 -08001808 if ((!audio || mAudioDecoder == NULL)
1809 && (!video || mVideoDecoder == NULL)) {
Andreas Hubera1f8ab02012-11-30 10:53:22 -08001810 return;
1811 }
1812
1813 mTimeDiscontinuityPending = true;
1814
Andreas Huber14f76722013-01-15 09:04:18 -08001815 if (audio && mAudioDecoder != NULL) {
Andreas Hubera1f8ab02012-11-30 10:53:22 -08001816 flushDecoder(true /* audio */, true /* needShutdown */);
1817 }
1818
Andreas Huber14f76722013-01-15 09:04:18 -08001819 if (video && mVideoDecoder != NULL) {
Andreas Hubera1f8ab02012-11-30 10:53:22 -08001820 flushDecoder(false /* audio */, true /* needShutdown */);
1821 }
1822}
1823
1824void NuPlayer::performReset() {
1825 ALOGV("performReset");
1826
1827 CHECK(mAudioDecoder == NULL);
1828 CHECK(mVideoDecoder == NULL);
1829
1830 cancelPollDuration();
1831
1832 ++mScanSourcesGeneration;
1833 mScanSourcesPending = false;
1834
Wei Jia1008e1c2014-09-09 14:49:08 -07001835 ++mAudioDecoderGeneration;
1836 ++mVideoDecoderGeneration;
1837
Lajos Molnar09524832014-07-17 14:29:51 -07001838 if (mRendererLooper != NULL) {
1839 if (mRenderer != NULL) {
1840 mRendererLooper->unregisterHandler(mRenderer->id());
1841 }
1842 mRendererLooper->stop();
1843 mRendererLooper.clear();
1844 }
Andreas Hubera1f8ab02012-11-30 10:53:22 -08001845 mRenderer.clear();
1846
1847 if (mSource != NULL) {
1848 mSource->stop();
Andreas Huberb5f25f02013-02-05 10:14:26 -08001849
Andreas Hubera1f8ab02012-11-30 10:53:22 -08001850 mSource.clear();
1851 }
1852
1853 if (mDriver != NULL) {
1854 sp<NuPlayerDriver> driver = mDriver.promote();
1855 if (driver != NULL) {
1856 driver->notifyResetComplete();
1857 }
1858 }
Andreas Huber57a339c2012-12-03 11:18:00 -08001859
1860 mStarted = false;
Andreas Hubera1f8ab02012-11-30 10:53:22 -08001861}
1862
1863void NuPlayer::performScanSources() {
1864 ALOGV("performScanSources");
1865
Andreas Huber57a339c2012-12-03 11:18:00 -08001866 if (!mStarted) {
1867 return;
1868 }
1869
Andreas Hubera1f8ab02012-11-30 10:53:22 -08001870 if (mAudioDecoder == NULL || mVideoDecoder == NULL) {
1871 postScanSources();
1872 }
1873}
1874
Andreas Huber57a339c2012-12-03 11:18:00 -08001875void NuPlayer::performSetSurface(const sp<NativeWindowWrapper> &wrapper) {
1876 ALOGV("performSetSurface");
1877
1878 mNativeWindow = wrapper;
1879
1880 // XXX - ignore error from setVideoScalingMode for now
1881 setVideoScalingMode(mVideoScalingMode);
Chong Zhang13d6faa2014-08-22 15:35:28 -07001882
1883 if (mDriver != NULL) {
1884 sp<NuPlayerDriver> driver = mDriver.promote();
1885 if (driver != NULL) {
1886 driver->notifySetSurfaceComplete();
1887 }
1888 }
Andreas Huber57a339c2012-12-03 11:18:00 -08001889}
1890
Andreas Huber9575c962013-02-05 13:59:56 -08001891void NuPlayer::onSourceNotify(const sp<AMessage> &msg) {
1892 int32_t what;
1893 CHECK(msg->findInt32("what", &what));
1894
1895 switch (what) {
1896 case Source::kWhatPrepared:
1897 {
Andreas Huberb5f28d42013-04-25 15:11:19 -07001898 if (mSource == NULL) {
1899 // This is a stale notification from a source that was
1900 // asynchronously preparing when the client called reset().
1901 // We handled the reset, the source is gone.
1902 break;
1903 }
1904
Andreas Huberec0c5972013-02-05 14:47:13 -08001905 int32_t err;
1906 CHECK(msg->findInt32("err", &err));
1907
Andreas Huber9575c962013-02-05 13:59:56 -08001908 sp<NuPlayerDriver> driver = mDriver.promote();
1909 if (driver != NULL) {
Marco Nelissendd114d12014-05-28 15:23:14 -07001910 // notify duration first, so that it's definitely set when
1911 // the app received the "prepare complete" callback.
1912 int64_t durationUs;
1913 if (mSource->getDuration(&durationUs) == OK) {
1914 driver->notifyDuration(durationUs);
1915 }
Andreas Huberec0c5972013-02-05 14:47:13 -08001916 driver->notifyPrepareCompleted(err);
Andreas Huber9575c962013-02-05 13:59:56 -08001917 }
Andreas Huber99759402013-04-01 14:28:31 -07001918
Andreas Huber9575c962013-02-05 13:59:56 -08001919 break;
1920 }
1921
1922 case Source::kWhatFlagsChanged:
1923 {
1924 uint32_t flags;
1925 CHECK(msg->findInt32("flags", (int32_t *)&flags));
1926
Chong Zhang4b7069d2013-09-11 12:52:43 -07001927 sp<NuPlayerDriver> driver = mDriver.promote();
1928 if (driver != NULL) {
1929 driver->notifyFlagsChanged(flags);
1930 }
1931
Andreas Huber9575c962013-02-05 13:59:56 -08001932 if ((mSourceFlags & Source::FLAG_DYNAMIC_DURATION)
1933 && (!(flags & Source::FLAG_DYNAMIC_DURATION))) {
1934 cancelPollDuration();
1935 } else if (!(mSourceFlags & Source::FLAG_DYNAMIC_DURATION)
1936 && (flags & Source::FLAG_DYNAMIC_DURATION)
1937 && (mAudioDecoder != NULL || mVideoDecoder != NULL)) {
1938 schedulePollDuration();
1939 }
1940
1941 mSourceFlags = flags;
1942 break;
1943 }
1944
1945 case Source::kWhatVideoSizeChanged:
1946 {
Chong Zhangced1c2f2014-08-08 15:22:35 -07001947 sp<AMessage> format;
1948 CHECK(msg->findMessage("format", &format));
Andreas Huber9575c962013-02-05 13:59:56 -08001949
Chong Zhangced1c2f2014-08-08 15:22:35 -07001950 updateVideoSize(format);
Andreas Huber9575c962013-02-05 13:59:56 -08001951 break;
1952 }
1953
Chong Zhang2a3cc9a2014-08-21 17:48:26 -07001954 case Source::kWhatBufferingUpdate:
1955 {
1956 int32_t percentage;
1957 CHECK(msg->findInt32("percentage", &percentage));
1958
1959 notifyListener(MEDIA_BUFFERING_UPDATE, percentage, 0);
1960 break;
1961 }
1962
Roger Jönssonb50e83e2013-01-21 16:26:41 +01001963 case Source::kWhatBufferingStart:
1964 {
1965 notifyListener(MEDIA_INFO, MEDIA_INFO_BUFFERING_START, 0);
1966 break;
1967 }
1968
1969 case Source::kWhatBufferingEnd:
1970 {
1971 notifyListener(MEDIA_INFO, MEDIA_INFO_BUFFERING_END, 0);
1972 break;
1973 }
1974
Chong Zhangdcb89b32013-08-06 09:44:47 -07001975 case Source::kWhatSubtitleData:
1976 {
1977 sp<ABuffer> buffer;
1978 CHECK(msg->findBuffer("buffer", &buffer));
1979
Chong Zhang404fced2014-06-11 14:45:31 -07001980 sendSubtitleData(buffer, 0 /* baseIndex */);
Chong Zhangdcb89b32013-08-06 09:44:47 -07001981 break;
1982 }
1983
Robert Shihd3b0bbb2014-07-23 15:00:25 -07001984 case Source::kWhatTimedTextData:
1985 {
1986 int32_t generation;
1987 if (msg->findInt32("generation", &generation)
1988 && generation != mTimedTextGeneration) {
1989 break;
1990 }
1991
1992 sp<ABuffer> buffer;
1993 CHECK(msg->findBuffer("buffer", &buffer));
1994
1995 sp<NuPlayerDriver> driver = mDriver.promote();
1996 if (driver == NULL) {
1997 break;
1998 }
1999
2000 int posMs;
2001 int64_t timeUs, posUs;
2002 driver->getCurrentPosition(&posMs);
2003 posUs = posMs * 1000;
2004 CHECK(buffer->meta()->findInt64("timeUs", &timeUs));
2005
2006 if (posUs < timeUs) {
2007 if (!msg->findInt32("generation", &generation)) {
2008 msg->setInt32("generation", mTimedTextGeneration);
2009 }
2010 msg->post(timeUs - posUs);
2011 } else {
2012 sendTimedTextData(buffer);
2013 }
2014 break;
2015 }
2016
Andreas Huber14f76722013-01-15 09:04:18 -08002017 case Source::kWhatQueueDecoderShutdown:
2018 {
2019 int32_t audio, video;
2020 CHECK(msg->findInt32("audio", &audio));
2021 CHECK(msg->findInt32("video", &video));
2022
2023 sp<AMessage> reply;
2024 CHECK(msg->findMessage("reply", &reply));
2025
2026 queueDecoderShutdown(audio, video, reply);
2027 break;
2028 }
2029
Ronghua Wu80276872014-08-28 15:50:29 -07002030 case Source::kWhatDrmNoLicense:
2031 {
2032 notifyListener(MEDIA_ERROR, MEDIA_ERROR_UNKNOWN, ERROR_DRM_NO_LICENSE);
2033 break;
2034 }
2035
Andreas Huber9575c962013-02-05 13:59:56 -08002036 default:
2037 TRESPASS();
2038 }
2039}
2040
Chong Zhanga7fa1d92014-06-11 14:49:23 -07002041void NuPlayer::onClosedCaptionNotify(const sp<AMessage> &msg) {
2042 int32_t what;
2043 CHECK(msg->findInt32("what", &what));
2044
2045 switch (what) {
2046 case NuPlayer::CCDecoder::kWhatClosedCaptionData:
2047 {
2048 sp<ABuffer> buffer;
2049 CHECK(msg->findBuffer("buffer", &buffer));
2050
2051 size_t inbandTracks = 0;
2052 if (mSource != NULL) {
2053 inbandTracks = mSource->getTrackCount();
2054 }
2055
2056 sendSubtitleData(buffer, inbandTracks);
2057 break;
2058 }
2059
2060 case NuPlayer::CCDecoder::kWhatTrackAdded:
2061 {
2062 notifyListener(MEDIA_INFO, MEDIA_INFO_METADATA_UPDATE, 0);
2063
2064 break;
2065 }
2066
2067 default:
2068 TRESPASS();
2069 }
2070
2071
2072}
2073
Chong Zhang404fced2014-06-11 14:45:31 -07002074void NuPlayer::sendSubtitleData(const sp<ABuffer> &buffer, int32_t baseIndex) {
2075 int32_t trackIndex;
2076 int64_t timeUs, durationUs;
2077 CHECK(buffer->meta()->findInt32("trackIndex", &trackIndex));
2078 CHECK(buffer->meta()->findInt64("timeUs", &timeUs));
2079 CHECK(buffer->meta()->findInt64("durationUs", &durationUs));
2080
2081 Parcel in;
2082 in.writeInt32(trackIndex + baseIndex);
2083 in.writeInt64(timeUs);
2084 in.writeInt64(durationUs);
2085 in.writeInt32(buffer->size());
2086 in.writeInt32(buffer->size());
2087 in.write(buffer->data(), buffer->size());
2088
2089 notifyListener(MEDIA_SUBTITLE_DATA, 0, 0, &in);
2090}
Robert Shihd3b0bbb2014-07-23 15:00:25 -07002091
2092void NuPlayer::sendTimedTextData(const sp<ABuffer> &buffer) {
2093 const void *data;
2094 size_t size = 0;
2095 int64_t timeUs;
2096 int32_t flag = TextDescriptions::LOCAL_DESCRIPTIONS;
2097
2098 AString mime;
2099 CHECK(buffer->meta()->findString("mime", &mime));
2100 CHECK(strcasecmp(mime.c_str(), MEDIA_MIMETYPE_TEXT_3GPP) == 0);
2101
2102 data = buffer->data();
2103 size = buffer->size();
2104
2105 Parcel parcel;
2106 if (size > 0) {
2107 CHECK(buffer->meta()->findInt64("timeUs", &timeUs));
2108 flag |= TextDescriptions::IN_BAND_TEXT_3GPP;
2109 TextDescriptions::getParcelOfDescriptions(
2110 (const uint8_t *)data, size, flag, timeUs / 1000, &parcel);
2111 }
2112
2113 if ((parcel.dataSize() > 0)) {
2114 notifyListener(MEDIA_TIMED_TEXT, 0, 0, &parcel);
2115 } else { // send an empty timed text
2116 notifyListener(MEDIA_TIMED_TEXT, 0, 0);
2117 }
2118}
Andreas Huberb5f25f02013-02-05 10:14:26 -08002119////////////////////////////////////////////////////////////////////////////////
2120
Chong Zhangced1c2f2014-08-08 15:22:35 -07002121sp<AMessage> NuPlayer::Source::getFormat(bool audio) {
2122 sp<MetaData> meta = getFormatMeta(audio);
2123
2124 if (meta == NULL) {
2125 return NULL;
2126 }
2127
2128 sp<AMessage> msg = new AMessage;
2129
2130 if(convertMetaDataToMessage(meta, &msg) == OK) {
2131 return msg;
2132 }
2133 return NULL;
2134}
2135
Andreas Huber9575c962013-02-05 13:59:56 -08002136void NuPlayer::Source::notifyFlagsChanged(uint32_t flags) {
2137 sp<AMessage> notify = dupNotify();
2138 notify->setInt32("what", kWhatFlagsChanged);
2139 notify->setInt32("flags", flags);
2140 notify->post();
2141}
2142
Chong Zhangced1c2f2014-08-08 15:22:35 -07002143void NuPlayer::Source::notifyVideoSizeChanged(const sp<AMessage> &format) {
Andreas Huber9575c962013-02-05 13:59:56 -08002144 sp<AMessage> notify = dupNotify();
2145 notify->setInt32("what", kWhatVideoSizeChanged);
Chong Zhangced1c2f2014-08-08 15:22:35 -07002146 notify->setMessage("format", format);
Andreas Huber9575c962013-02-05 13:59:56 -08002147 notify->post();
2148}
2149
Andreas Huberec0c5972013-02-05 14:47:13 -08002150void NuPlayer::Source::notifyPrepared(status_t err) {
Andreas Huber9575c962013-02-05 13:59:56 -08002151 sp<AMessage> notify = dupNotify();
2152 notify->setInt32("what", kWhatPrepared);
Andreas Huberec0c5972013-02-05 14:47:13 -08002153 notify->setInt32("err", err);
Andreas Huber9575c962013-02-05 13:59:56 -08002154 notify->post();
2155}
2156
Andreas Huber84333e02014-02-07 15:36:10 -08002157void NuPlayer::Source::onMessageReceived(const sp<AMessage> & /* msg */) {
Andreas Huberb5f25f02013-02-05 10:14:26 -08002158 TRESPASS();
2159}
2160
Andreas Huberf9334412010-12-15 15:17:42 -08002161} // namespace android