blob: 078e78b9338d3fa2524fc6f0dcc134c1e4dd1954 [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
Andreas Hubera1f8ab02012-11-30 10:53:22 -080053struct NuPlayer::Action : public RefBase {
54 Action() {}
55
56 virtual void execute(NuPlayer *player) = 0;
57
58private:
59 DISALLOW_EVIL_CONSTRUCTORS(Action);
60};
61
62struct NuPlayer::SeekAction : public Action {
63 SeekAction(int64_t seekTimeUs)
64 : mSeekTimeUs(seekTimeUs) {
65 }
66
67 virtual void execute(NuPlayer *player) {
68 player->performSeek(mSeekTimeUs);
69 }
70
71private:
72 int64_t mSeekTimeUs;
73
74 DISALLOW_EVIL_CONSTRUCTORS(SeekAction);
75};
76
Andreas Huber57a339c2012-12-03 11:18:00 -080077struct NuPlayer::SetSurfaceAction : public Action {
78 SetSurfaceAction(const sp<NativeWindowWrapper> &wrapper)
79 : mWrapper(wrapper) {
80 }
81
82 virtual void execute(NuPlayer *player) {
83 player->performSetSurface(mWrapper);
84 }
85
86private:
87 sp<NativeWindowWrapper> mWrapper;
88
89 DISALLOW_EVIL_CONSTRUCTORS(SetSurfaceAction);
90};
91
Andreas Huber14f76722013-01-15 09:04:18 -080092struct NuPlayer::ShutdownDecoderAction : public Action {
93 ShutdownDecoderAction(bool audio, bool video)
94 : mAudio(audio),
95 mVideo(video) {
96 }
97
98 virtual void execute(NuPlayer *player) {
99 player->performDecoderShutdown(mAudio, mVideo);
100 }
101
102private:
103 bool mAudio;
104 bool mVideo;
105
106 DISALLOW_EVIL_CONSTRUCTORS(ShutdownDecoderAction);
107};
108
109struct NuPlayer::PostMessageAction : public Action {
110 PostMessageAction(const sp<AMessage> &msg)
111 : mMessage(msg) {
112 }
113
114 virtual void execute(NuPlayer *) {
115 mMessage->post();
116 }
117
118private:
119 sp<AMessage> mMessage;
120
121 DISALLOW_EVIL_CONSTRUCTORS(PostMessageAction);
122};
123
Andreas Hubera1f8ab02012-11-30 10:53:22 -0800124// Use this if there's no state necessary to save in order to execute
125// the action.
126struct NuPlayer::SimpleAction : public Action {
127 typedef void (NuPlayer::*ActionFunc)();
128
129 SimpleAction(ActionFunc func)
130 : mFunc(func) {
131 }
132
133 virtual void execute(NuPlayer *player) {
134 (player->*mFunc)();
135 }
136
137private:
138 ActionFunc mFunc;
139
140 DISALLOW_EVIL_CONSTRUCTORS(SimpleAction);
141};
142
Andreas Huberf9334412010-12-15 15:17:42 -0800143////////////////////////////////////////////////////////////////////////////////
144
145NuPlayer::NuPlayer()
Andreas Huber9b80c2b2011-06-30 15:47:02 -0700146 : mUIDValid(false),
Andreas Huber9575c962013-02-05 13:59:56 -0800147 mSourceFlags(0),
Wei Jiaac428aa2014-09-02 19:01:34 -0700148 mCurrentPositionUs(0),
Andreas Huber3fe62152011-09-16 15:09:22 -0700149 mVideoIsAVC(false),
Wei Jiabc2fb722014-07-08 16:37:57 -0700150 mOffloadAudio(false),
Andy Hung282a7e32014-08-14 15:56:34 -0700151 mCurrentOffloadInfo(AUDIO_INFO_INITIALIZER),
Wei Jia88703c32014-08-06 11:24:07 -0700152 mAudioDecoderGeneration(0),
153 mVideoDecoderGeneration(0),
Andreas Huber9b80c2b2011-06-30 15:47:02 -0700154 mAudioEOS(false),
Andreas Huberf9334412010-12-15 15:17:42 -0800155 mVideoEOS(false),
Andreas Huber5bc087c2010-12-23 10:27:40 -0800156 mScanSourcesPending(false),
Andreas Huber1aef2112011-01-04 14:01:29 -0800157 mScanSourcesGeneration(0),
Andreas Huberb7c8e912012-11-27 15:02:53 -0800158 mPollDurationGeneration(0),
Robert Shihd3b0bbb2014-07-23 15:00:25 -0700159 mTimedTextGeneration(0),
Andreas Huber6e3d3112011-11-28 12:36:11 -0800160 mTimeDiscontinuityPending(false),
Andreas Huberf9334412010-12-15 15:17:42 -0800161 mFlushingAudio(NONE),
Andreas Huber1aef2112011-01-04 14:01:29 -0800162 mFlushingVideo(NONE),
Andreas Huber3fe62152011-09-16 15:09:22 -0700163 mSkipRenderingAudioUntilMediaTimeUs(-1ll),
164 mSkipRenderingVideoUntilMediaTimeUs(-1ll),
165 mVideoLateByUs(0ll),
166 mNumFramesTotal(0ll),
James Dong0d268a32012-08-31 12:18:27 -0700167 mNumFramesDropped(0ll),
Andreas Huber57a339c2012-12-03 11:18:00 -0800168 mVideoScalingMode(NATIVE_WINDOW_SCALING_MODE_SCALE_TO_WINDOW),
169 mStarted(false) {
Andreas Huberf9334412010-12-15 15:17:42 -0800170}
171
172NuPlayer::~NuPlayer() {
173}
174
Andreas Huber9b80c2b2011-06-30 15:47:02 -0700175void NuPlayer::setUID(uid_t uid) {
176 mUIDValid = true;
177 mUID = uid;
178}
179
Andreas Huber43c3e6c2011-01-05 12:17:08 -0800180void NuPlayer::setDriver(const wp<NuPlayerDriver> &driver) {
181 mDriver = driver;
Andreas Huberf9334412010-12-15 15:17:42 -0800182}
183
Andreas Huber9575c962013-02-05 13:59:56 -0800184void NuPlayer::setDataSourceAsync(const sp<IStreamSource> &source) {
Andreas Huberf9334412010-12-15 15:17:42 -0800185 sp<AMessage> msg = new AMessage(kWhatSetDataSource, id());
186
Andreas Huberb5f25f02013-02-05 10:14:26 -0800187 sp<AMessage> notify = new AMessage(kWhatSourceNotify, id());
188
Andreas Huber240abcc2014-02-13 13:32:37 -0800189 msg->setObject("source", new StreamingSource(notify, source));
Andreas Huber5bc087c2010-12-23 10:27:40 -0800190 msg->post();
191}
Andreas Huberf9334412010-12-15 15:17:42 -0800192
Andreas Huberafed0e12011-09-20 15:39:58 -0700193static bool IsHTTPLiveURL(const char *url) {
194 if (!strncasecmp("http://", url, 7)
Andreas Huber99759402013-04-01 14:28:31 -0700195 || !strncasecmp("https://", url, 8)
196 || !strncasecmp("file://", url, 7)) {
Andreas Huberafed0e12011-09-20 15:39:58 -0700197 size_t len = strlen(url);
198 if (len >= 5 && !strcasecmp(".m3u8", &url[len - 5])) {
199 return true;
200 }
201
202 if (strstr(url,"m3u8")) {
203 return true;
204 }
205 }
206
207 return false;
208}
209
Andreas Huber9575c962013-02-05 13:59:56 -0800210void NuPlayer::setDataSourceAsync(
Andreas Huber1b86fe02014-01-29 11:13:26 -0800211 const sp<IMediaHTTPService> &httpService,
212 const char *url,
213 const KeyedVector<String8, String8> *headers) {
Chong Zhang3de157d2014-08-05 20:54:44 -0700214
Andreas Huber5bc087c2010-12-23 10:27:40 -0800215 sp<AMessage> msg = new AMessage(kWhatSetDataSource, id());
Oscar Rydhé7a33b772012-02-20 10:15:48 +0100216 size_t len = strlen(url);
Andreas Huber5bc087c2010-12-23 10:27:40 -0800217
Andreas Huberb5f25f02013-02-05 10:14:26 -0800218 sp<AMessage> notify = new AMessage(kWhatSourceNotify, id());
219
Andreas Huberafed0e12011-09-20 15:39:58 -0700220 sp<Source> source;
221 if (IsHTTPLiveURL(url)) {
Andreas Huber81e68442014-02-05 11:52:33 -0800222 source = new HTTPLiveSource(notify, httpService, url, headers);
Andreas Huberafed0e12011-09-20 15:39:58 -0700223 } else if (!strncasecmp(url, "rtsp://", 7)) {
Andreas Huber1b86fe02014-01-29 11:13:26 -0800224 source = new RTSPSource(
225 notify, httpService, url, headers, mUIDValid, mUID);
Oscar Rydhé7a33b772012-02-20 10:15:48 +0100226 } else if ((!strncasecmp(url, "http://", 7)
227 || !strncasecmp(url, "https://", 8))
228 && ((len >= 4 && !strcasecmp(".sdp", &url[len - 4]))
229 || strstr(url, ".sdp?"))) {
Andreas Huber1b86fe02014-01-29 11:13:26 -0800230 source = new RTSPSource(
231 notify, httpService, url, headers, mUIDValid, mUID, true);
Andreas Huber2bfdd422011-10-11 15:24:07 -0700232 } else {
Chong Zhang3de157d2014-08-05 20:54:44 -0700233 sp<GenericSource> genericSource =
234 new GenericSource(notify, mUIDValid, mUID);
235 // Don't set FLAG_SECURE on mSourceFlags here for widevine.
236 // The correct flags will be updated in Source::kWhatFlagsChanged
237 // handler when GenericSource is prepared.
Andreas Huber2bfdd422011-10-11 15:24:07 -0700238
Chong Zhanga19f33e2014-08-07 15:35:07 -0700239 status_t err = genericSource->setDataSource(httpService, url, headers);
Chong Zhang3de157d2014-08-05 20:54:44 -0700240
241 if (err == OK) {
242 source = genericSource;
243 } else {
Chong Zhanga19f33e2014-08-07 15:35:07 -0700244 ALOGE("Failed to set data source!");
Chong Zhang3de157d2014-08-05 20:54:44 -0700245 }
246 }
Andreas Huberafed0e12011-09-20 15:39:58 -0700247 msg->setObject("source", source);
248 msg->post();
249}
250
Andreas Huber9575c962013-02-05 13:59:56 -0800251void NuPlayer::setDataSourceAsync(int fd, int64_t offset, int64_t length) {
Andreas Huberafed0e12011-09-20 15:39:58 -0700252 sp<AMessage> msg = new AMessage(kWhatSetDataSource, id());
253
Andreas Huberb5f25f02013-02-05 10:14:26 -0800254 sp<AMessage> notify = new AMessage(kWhatSourceNotify, id());
255
Chong Zhang3de157d2014-08-05 20:54:44 -0700256 sp<GenericSource> source =
257 new GenericSource(notify, mUIDValid, mUID);
258
Chong Zhanga19f33e2014-08-07 15:35:07 -0700259 status_t err = source->setDataSource(fd, offset, length);
Chong Zhang3de157d2014-08-05 20:54:44 -0700260
261 if (err != OK) {
Chong Zhanga19f33e2014-08-07 15:35:07 -0700262 ALOGE("Failed to set data source!");
Chong Zhang3de157d2014-08-05 20:54:44 -0700263 source = NULL;
264 }
265
Andreas Huberafed0e12011-09-20 15:39:58 -0700266 msg->setObject("source", source);
Andreas Huberf9334412010-12-15 15:17:42 -0800267 msg->post();
268}
269
Andreas Huber9575c962013-02-05 13:59:56 -0800270void NuPlayer::prepareAsync() {
271 (new AMessage(kWhatPrepare, id()))->post();
272}
273
Andreas Huber57a339c2012-12-03 11:18:00 -0800274void NuPlayer::setVideoSurfaceTextureAsync(
Andy McFadden8ba01022012-12-18 09:46:54 -0800275 const sp<IGraphicBufferProducer> &bufferProducer) {
Glenn Kasten11731182011-02-08 17:26:17 -0800276 sp<AMessage> msg = new AMessage(kWhatSetVideoNativeWindow, id());
Andreas Huber57a339c2012-12-03 11:18:00 -0800277
Andy McFadden8ba01022012-12-18 09:46:54 -0800278 if (bufferProducer == NULL) {
Andreas Huber57a339c2012-12-03 11:18:00 -0800279 msg->setObject("native-window", NULL);
280 } else {
281 msg->setObject(
282 "native-window",
283 new NativeWindowWrapper(
Wei Jia9c03a402014-08-26 15:24:43 -0700284 new Surface(bufferProducer, true /* controlledByApp */)));
Andreas Huber57a339c2012-12-03 11:18:00 -0800285 }
286
Andreas Huberf9334412010-12-15 15:17:42 -0800287 msg->post();
288}
289
290void NuPlayer::setAudioSink(const sp<MediaPlayerBase::AudioSink> &sink) {
291 sp<AMessage> msg = new AMessage(kWhatSetAudioSink, id());
292 msg->setObject("sink", sink);
293 msg->post();
294}
295
296void NuPlayer::start() {
297 (new AMessage(kWhatStart, id()))->post();
298}
299
Andreas Huber43c3e6c2011-01-05 12:17:08 -0800300void NuPlayer::pause() {
Andreas Huberb4082222011-01-20 15:23:04 -0800301 (new AMessage(kWhatPause, id()))->post();
Andreas Huber43c3e6c2011-01-05 12:17:08 -0800302}
303
304void NuPlayer::resume() {
Andreas Huberb4082222011-01-20 15:23:04 -0800305 (new AMessage(kWhatResume, id()))->post();
Andreas Huber43c3e6c2011-01-05 12:17:08 -0800306}
307
Andreas Huber1aef2112011-01-04 14:01:29 -0800308void NuPlayer::resetAsync() {
Chong Zhang48296b72014-09-14 14:28:45 -0700309 if (mSource != NULL) {
310 // During a reset, the data source might be unresponsive already, we need to
311 // disconnect explicitly so that reads exit promptly.
312 // We can't queue the disconnect request to the looper, as it might be
313 // queued behind a stuck read and never gets processed.
314 // Doing a disconnect outside the looper to allows the pending reads to exit
315 // (either successfully or with error).
316 mSource->disconnect();
317 }
318
Andreas Huber1aef2112011-01-04 14:01:29 -0800319 (new AMessage(kWhatReset, id()))->post();
320}
321
Andreas Huber43c3e6c2011-01-05 12:17:08 -0800322void NuPlayer::seekToAsync(int64_t seekTimeUs) {
323 sp<AMessage> msg = new AMessage(kWhatSeek, id());
324 msg->setInt64("seekTimeUs", seekTimeUs);
325 msg->post();
326}
327
Andreas Huber53df1a42010-12-22 10:03:04 -0800328// static
Andreas Huber1aef2112011-01-04 14:01:29 -0800329bool NuPlayer::IsFlushingState(FlushStatus state, bool *needShutdown) {
Andreas Huber53df1a42010-12-22 10:03:04 -0800330 switch (state) {
331 case FLUSHING_DECODER:
Andreas Huber1aef2112011-01-04 14:01:29 -0800332 if (needShutdown != NULL) {
333 *needShutdown = false;
Andreas Huber53df1a42010-12-22 10:03:04 -0800334 }
335 return true;
336
Andreas Huber1aef2112011-01-04 14:01:29 -0800337 case FLUSHING_DECODER_SHUTDOWN:
338 if (needShutdown != NULL) {
339 *needShutdown = true;
Andreas Huber53df1a42010-12-22 10:03:04 -0800340 }
341 return true;
342
343 default:
344 return false;
345 }
346}
347
Chong Zhang404fced2014-06-11 14:45:31 -0700348void NuPlayer::writeTrackInfo(
349 Parcel* reply, const sp<AMessage> format) const {
350 int32_t trackType;
351 CHECK(format->findInt32("type", &trackType));
352
353 AString lang;
354 CHECK(format->findString("language", &lang));
355
356 reply->writeInt32(2); // write something non-zero
357 reply->writeInt32(trackType);
358 reply->writeString16(String16(lang.c_str()));
359
360 if (trackType == MEDIA_TRACK_TYPE_SUBTITLE) {
361 AString mime;
362 CHECK(format->findString("mime", &mime));
363
364 int32_t isAuto, isDefault, isForced;
365 CHECK(format->findInt32("auto", &isAuto));
366 CHECK(format->findInt32("default", &isDefault));
367 CHECK(format->findInt32("forced", &isForced));
368
369 reply->writeString16(String16(mime.c_str()));
370 reply->writeInt32(isAuto);
371 reply->writeInt32(isDefault);
372 reply->writeInt32(isForced);
373 }
374}
375
Andreas Huberf9334412010-12-15 15:17:42 -0800376void NuPlayer::onMessageReceived(const sp<AMessage> &msg) {
377 switch (msg->what()) {
378 case kWhatSetDataSource:
379 {
Steve Block3856b092011-10-20 11:56:00 +0100380 ALOGV("kWhatSetDataSource");
Andreas Huberf9334412010-12-15 15:17:42 -0800381
382 CHECK(mSource == NULL);
383
Chong Zhang3de157d2014-08-05 20:54:44 -0700384 status_t err = OK;
Andreas Huber5bc087c2010-12-23 10:27:40 -0800385 sp<RefBase> obj;
386 CHECK(msg->findObject("source", &obj));
Chong Zhang3de157d2014-08-05 20:54:44 -0700387 if (obj != NULL) {
388 mSource = static_cast<Source *>(obj.get());
Chong Zhang3de157d2014-08-05 20:54:44 -0700389 } else {
390 err = UNKNOWN_ERROR;
391 }
Andreas Huber9575c962013-02-05 13:59:56 -0800392
393 CHECK(mDriver != NULL);
394 sp<NuPlayerDriver> driver = mDriver.promote();
395 if (driver != NULL) {
Chong Zhang3de157d2014-08-05 20:54:44 -0700396 driver->notifySetDataSourceCompleted(err);
Andreas Huber9575c962013-02-05 13:59:56 -0800397 }
398 break;
399 }
400
401 case kWhatPrepare:
402 {
403 mSource->prepareAsync();
Andreas Huberf9334412010-12-15 15:17:42 -0800404 break;
405 }
406
Chong Zhangdcb89b32013-08-06 09:44:47 -0700407 case kWhatGetTrackInfo:
408 {
409 uint32_t replyID;
410 CHECK(msg->senderAwaitsResponse(&replyID));
411
Chong Zhang404fced2014-06-11 14:45:31 -0700412 Parcel* reply;
413 CHECK(msg->findPointer("reply", (void**)&reply));
414
415 size_t inbandTracks = 0;
Chong Zhangdcb89b32013-08-06 09:44:47 -0700416 if (mSource != NULL) {
Chong Zhang404fced2014-06-11 14:45:31 -0700417 inbandTracks = mSource->getTrackCount();
418 }
419
Chong Zhanga7fa1d92014-06-11 14:49:23 -0700420 size_t ccTracks = 0;
421 if (mCCDecoder != NULL) {
422 ccTracks = mCCDecoder->getTrackCount();
423 }
424
Chong Zhang404fced2014-06-11 14:45:31 -0700425 // total track count
Chong Zhanga7fa1d92014-06-11 14:49:23 -0700426 reply->writeInt32(inbandTracks + ccTracks);
Chong Zhang404fced2014-06-11 14:45:31 -0700427
428 // write inband tracks
429 for (size_t i = 0; i < inbandTracks; ++i) {
430 writeTrackInfo(reply, mSource->getTrackInfo(i));
Chong Zhangdcb89b32013-08-06 09:44:47 -0700431 }
432
Chong Zhanga7fa1d92014-06-11 14:49:23 -0700433 // write CC track
434 for (size_t i = 0; i < ccTracks; ++i) {
435 writeTrackInfo(reply, mCCDecoder->getTrackInfo(i));
436 }
437
Chong Zhangdcb89b32013-08-06 09:44:47 -0700438 sp<AMessage> response = new AMessage;
Chong Zhangdcb89b32013-08-06 09:44:47 -0700439 response->postReply(replyID);
440 break;
441 }
442
Robert Shih7c4f0d72014-07-09 18:53:31 -0700443 case kWhatGetSelectedTrack:
444 {
445 status_t err = INVALID_OPERATION;
446 if (mSource != NULL) {
447 err = OK;
448
449 int32_t type32;
450 CHECK(msg->findInt32("type", (int32_t*)&type32));
451 media_track_type type = (media_track_type)type32;
452 ssize_t selectedTrack = mSource->getSelectedTrack(type);
453
454 Parcel* reply;
455 CHECK(msg->findPointer("reply", (void**)&reply));
456 reply->writeInt32(selectedTrack);
457 }
458
459 sp<AMessage> response = new AMessage;
460 response->setInt32("err", err);
461
462 uint32_t replyID;
463 CHECK(msg->senderAwaitsResponse(&replyID));
464 response->postReply(replyID);
465 break;
466 }
467
Chong Zhangdcb89b32013-08-06 09:44:47 -0700468 case kWhatSelectTrack:
469 {
470 uint32_t replyID;
471 CHECK(msg->senderAwaitsResponse(&replyID));
472
Chong Zhang404fced2014-06-11 14:45:31 -0700473 size_t trackIndex;
474 int32_t select;
475 CHECK(msg->findSize("trackIndex", &trackIndex));
476 CHECK(msg->findInt32("select", &select));
477
Chong Zhangdcb89b32013-08-06 09:44:47 -0700478 status_t err = INVALID_OPERATION;
Chong Zhang404fced2014-06-11 14:45:31 -0700479
480 size_t inbandTracks = 0;
Chong Zhangdcb89b32013-08-06 09:44:47 -0700481 if (mSource != NULL) {
Chong Zhang404fced2014-06-11 14:45:31 -0700482 inbandTracks = mSource->getTrackCount();
483 }
Chong Zhanga7fa1d92014-06-11 14:49:23 -0700484 size_t ccTracks = 0;
485 if (mCCDecoder != NULL) {
486 ccTracks = mCCDecoder->getTrackCount();
487 }
Chong Zhang404fced2014-06-11 14:45:31 -0700488
489 if (trackIndex < inbandTracks) {
Chong Zhangdcb89b32013-08-06 09:44:47 -0700490 err = mSource->selectTrack(trackIndex, select);
Robert Shihd3b0bbb2014-07-23 15:00:25 -0700491
492 if (!select && err == OK) {
493 int32_t type;
494 sp<AMessage> info = mSource->getTrackInfo(trackIndex);
495 if (info != NULL
496 && info->findInt32("type", &type)
497 && type == MEDIA_TRACK_TYPE_TIMEDTEXT) {
498 ++mTimedTextGeneration;
499 }
500 }
Chong Zhanga7fa1d92014-06-11 14:49:23 -0700501 } else {
502 trackIndex -= inbandTracks;
503
504 if (trackIndex < ccTracks) {
505 err = mCCDecoder->selectTrack(trackIndex, select);
506 }
Chong Zhangdcb89b32013-08-06 09:44:47 -0700507 }
508
509 sp<AMessage> response = new AMessage;
510 response->setInt32("err", err);
511
512 response->postReply(replyID);
513 break;
514 }
515
Andreas Huberb7c8e912012-11-27 15:02:53 -0800516 case kWhatPollDuration:
517 {
518 int32_t generation;
519 CHECK(msg->findInt32("generation", &generation));
520
521 if (generation != mPollDurationGeneration) {
522 // stale
523 break;
524 }
525
526 int64_t durationUs;
527 if (mDriver != NULL && mSource->getDuration(&durationUs) == OK) {
528 sp<NuPlayerDriver> driver = mDriver.promote();
529 if (driver != NULL) {
530 driver->notifyDuration(durationUs);
531 }
532 }
533
534 msg->post(1000000ll); // poll again in a second.
535 break;
536 }
537
Glenn Kasten11731182011-02-08 17:26:17 -0800538 case kWhatSetVideoNativeWindow:
Andreas Huberf9334412010-12-15 15:17:42 -0800539 {
Steve Block3856b092011-10-20 11:56:00 +0100540 ALOGV("kWhatSetVideoNativeWindow");
Andreas Huberf9334412010-12-15 15:17:42 -0800541
Andreas Huber57a339c2012-12-03 11:18:00 -0800542 mDeferredActions.push_back(
Andreas Huber14f76722013-01-15 09:04:18 -0800543 new ShutdownDecoderAction(
544 false /* audio */, true /* video */));
Andreas Huber57a339c2012-12-03 11:18:00 -0800545
Andreas Huberf9334412010-12-15 15:17:42 -0800546 sp<RefBase> obj;
Glenn Kasten11731182011-02-08 17:26:17 -0800547 CHECK(msg->findObject("native-window", &obj));
Andreas Huberf9334412010-12-15 15:17:42 -0800548
Andreas Huber57a339c2012-12-03 11:18:00 -0800549 mDeferredActions.push_back(
550 new SetSurfaceAction(
551 static_cast<NativeWindowWrapper *>(obj.get())));
James Dong0d268a32012-08-31 12:18:27 -0700552
Andreas Huber57a339c2012-12-03 11:18:00 -0800553 if (obj != NULL) {
Andy Hung73535852014-09-05 11:42:58 -0700554 if (mStarted && mVideoDecoder != NULL) {
555 // Issue a seek to refresh the video screen only if started otherwise
556 // the extractor may not yet be started and will assert.
557 // If the video decoder is not set (perhaps audio only in this case)
558 // do not perform a seek as it is not needed.
559 mDeferredActions.push_back(new SeekAction(mCurrentPositionUs));
560 }
Wei Jiaac428aa2014-09-02 19:01:34 -0700561
Andreas Huber57a339c2012-12-03 11:18:00 -0800562 // If there is a new surface texture, instantiate decoders
563 // again if possible.
564 mDeferredActions.push_back(
565 new SimpleAction(&NuPlayer::performScanSources));
566 }
567
568 processDeferredActions();
Andreas Huberf9334412010-12-15 15:17:42 -0800569 break;
570 }
571
572 case kWhatSetAudioSink:
573 {
Steve Block3856b092011-10-20 11:56:00 +0100574 ALOGV("kWhatSetAudioSink");
Andreas Huberf9334412010-12-15 15:17:42 -0800575
576 sp<RefBase> obj;
577 CHECK(msg->findObject("sink", &obj));
578
579 mAudioSink = static_cast<MediaPlayerBase::AudioSink *>(obj.get());
580 break;
581 }
582
583 case kWhatStart:
584 {
Steve Block3856b092011-10-20 11:56:00 +0100585 ALOGV("kWhatStart");
Andreas Huber43c3e6c2011-01-05 12:17:08 -0800586
Andreas Huber3fe62152011-09-16 15:09:22 -0700587 mVideoIsAVC = false;
Wei Jiabc2fb722014-07-08 16:37:57 -0700588 mOffloadAudio = false;
Andreas Huber1aef2112011-01-04 14:01:29 -0800589 mAudioEOS = false;
590 mVideoEOS = false;
Andreas Huber32f3cef2011-03-02 15:34:46 -0800591 mSkipRenderingAudioUntilMediaTimeUs = -1;
592 mSkipRenderingVideoUntilMediaTimeUs = -1;
Andreas Huber3fe62152011-09-16 15:09:22 -0700593 mVideoLateByUs = 0;
594 mNumFramesTotal = 0;
595 mNumFramesDropped = 0;
Andreas Huber57a339c2012-12-03 11:18:00 -0800596 mStarted = true;
Andreas Huber1aef2112011-01-04 14:01:29 -0800597
Lajos Molnar09524832014-07-17 14:29:51 -0700598 /* instantiate decoders now for secure playback */
599 if (mSourceFlags & Source::FLAG_SECURE) {
600 if (mNativeWindow != NULL) {
601 instantiateDecoder(false, &mVideoDecoder);
602 }
603
604 if (mAudioSink != NULL) {
605 instantiateDecoder(true, &mAudioDecoder);
606 }
607 }
608
Andreas Huber5bc087c2010-12-23 10:27:40 -0800609 mSource->start();
Andreas Huberf9334412010-12-15 15:17:42 -0800610
Andreas Huberd5e56232013-03-12 11:01:43 -0700611 uint32_t flags = 0;
612
613 if (mSource->isRealTime()) {
614 flags |= Renderer::FLAG_REAL_TIME;
615 }
616
Wei Jiabc2fb722014-07-08 16:37:57 -0700617 sp<MetaData> audioMeta = mSource->getFormatMeta(true /* audio */);
618 audio_stream_type_t streamType = AUDIO_STREAM_MUSIC;
619 if (mAudioSink != NULL) {
620 streamType = mAudioSink->getAudioStreamType();
621 }
622
623 sp<AMessage> videoFormat = mSource->getFormat(false /* audio */);
624
625 mOffloadAudio =
626 canOffloadStream(audioMeta, (videoFormat != NULL),
627 true /* is_streaming */, streamType);
628 if (mOffloadAudio) {
629 flags |= Renderer::FLAG_OFFLOAD_AUDIO;
630 }
631
Andreas Huberf9334412010-12-15 15:17:42 -0800632 mRenderer = new Renderer(
633 mAudioSink,
Andreas Huberd5e56232013-03-12 11:01:43 -0700634 new AMessage(kWhatRendererNotify, id()),
635 flags);
Andreas Huberf9334412010-12-15 15:17:42 -0800636
Lajos Molnar09524832014-07-17 14:29:51 -0700637 mRendererLooper = new ALooper;
638 mRendererLooper->setName("NuPlayerRenderer");
639 mRendererLooper->start(false, false, ANDROID_PRIORITY_AUDIO);
640 mRendererLooper->registerHandler(mRenderer);
Andreas Huberf9334412010-12-15 15:17:42 -0800641
Andreas Huber1aef2112011-01-04 14:01:29 -0800642 postScanSources();
Andreas Huberf9334412010-12-15 15:17:42 -0800643 break;
644 }
645
646 case kWhatScanSources:
647 {
Andreas Huber1aef2112011-01-04 14:01:29 -0800648 int32_t generation;
649 CHECK(msg->findInt32("generation", &generation));
650 if (generation != mScanSourcesGeneration) {
651 // Drop obsolete msg.
652 break;
653 }
654
Andreas Huber5bc087c2010-12-23 10:27:40 -0800655 mScanSourcesPending = false;
656
Steve Block3856b092011-10-20 11:56:00 +0100657 ALOGV("scanning sources haveAudio=%d, haveVideo=%d",
Andreas Huber43c3e6c2011-01-05 12:17:08 -0800658 mAudioDecoder != NULL, mVideoDecoder != NULL);
659
Andreas Huberb7c8e912012-11-27 15:02:53 -0800660 bool mHadAnySourcesBefore =
661 (mAudioDecoder != NULL) || (mVideoDecoder != NULL);
662
Andy Hung282a7e32014-08-14 15:56:34 -0700663 // initialize video before audio because successful initialization of
664 // video may change deep buffer mode of audio.
Haynes Mathew George5d246ef2012-07-09 10:36:57 -0700665 if (mNativeWindow != NULL) {
666 instantiateDecoder(false, &mVideoDecoder);
667 }
Andreas Huberf9334412010-12-15 15:17:42 -0800668
669 if (mAudioSink != NULL) {
Andy Hung282a7e32014-08-14 15:56:34 -0700670 if (mOffloadAudio) {
671 // open audio sink early under offload mode.
672 sp<AMessage> format = mSource->getFormat(true /*audio*/);
673 openAudioSink(format, true /*offloadOnly*/);
674 }
Andreas Huber5bc087c2010-12-23 10:27:40 -0800675 instantiateDecoder(true, &mAudioDecoder);
Andreas Huberf9334412010-12-15 15:17:42 -0800676 }
677
Andreas Huberb7c8e912012-11-27 15:02:53 -0800678 if (!mHadAnySourcesBefore
679 && (mAudioDecoder != NULL || mVideoDecoder != NULL)) {
680 // This is the first time we've found anything playable.
681
Andreas Huber9575c962013-02-05 13:59:56 -0800682 if (mSourceFlags & Source::FLAG_DYNAMIC_DURATION) {
Andreas Huberb7c8e912012-11-27 15:02:53 -0800683 schedulePollDuration();
684 }
685 }
686
Andreas Hubereac68ba2011-09-27 12:12:25 -0700687 status_t err;
688 if ((err = mSource->feedMoreTSData()) != OK) {
Andreas Huber1aef2112011-01-04 14:01:29 -0800689 if (mAudioDecoder == NULL && mVideoDecoder == NULL) {
690 // We're not currently decoding anything (no audio or
691 // video tracks found) and we just ran out of input data.
Andreas Hubereac68ba2011-09-27 12:12:25 -0700692
693 if (err == ERROR_END_OF_STREAM) {
694 notifyListener(MEDIA_PLAYBACK_COMPLETE, 0, 0);
695 } else {
696 notifyListener(MEDIA_ERROR, MEDIA_ERROR_UNKNOWN, err);
697 }
Andreas Huber1aef2112011-01-04 14:01:29 -0800698 }
Andreas Huberf9334412010-12-15 15:17:42 -0800699 break;
700 }
701
Andreas Huberfbe9d812012-08-31 14:05:27 -0700702 if ((mAudioDecoder == NULL && mAudioSink != NULL)
703 || (mVideoDecoder == NULL && mNativeWindow != NULL)) {
Andreas Huberf9334412010-12-15 15:17:42 -0800704 msg->post(100000ll);
Andreas Huber5bc087c2010-12-23 10:27:40 -0800705 mScanSourcesPending = true;
Andreas Huberf9334412010-12-15 15:17:42 -0800706 }
707 break;
708 }
709
710 case kWhatVideoNotify:
711 case kWhatAudioNotify:
712 {
713 bool audio = msg->what() == kWhatAudioNotify;
714
Wei Jia88703c32014-08-06 11:24:07 -0700715 int32_t currentDecoderGeneration =
716 (audio? mAudioDecoderGeneration : mVideoDecoderGeneration);
717 int32_t requesterGeneration = currentDecoderGeneration - 1;
718 CHECK(msg->findInt32("generation", &requesterGeneration));
719
720 if (requesterGeneration != currentDecoderGeneration) {
721 ALOGV("got message from old %s decoder, generation(%d:%d)",
722 audio ? "audio" : "video", requesterGeneration,
723 currentDecoderGeneration);
724 sp<AMessage> reply;
725 if (!(msg->findMessage("reply", &reply))) {
726 return;
727 }
728
729 reply->setInt32("err", INFO_DISCONTINUITY);
730 reply->post();
731 return;
732 }
733
Andreas Huberf9334412010-12-15 15:17:42 -0800734 int32_t what;
Lajos Molnar1cd13982014-01-17 15:12:51 -0800735 CHECK(msg->findInt32("what", &what));
Andreas Huberf9334412010-12-15 15:17:42 -0800736
Lajos Molnar1cd13982014-01-17 15:12:51 -0800737 if (what == Decoder::kWhatFillThisBuffer) {
Andreas Huberf9334412010-12-15 15:17:42 -0800738 status_t err = feedDecoderInputData(
Lajos Molnar1cd13982014-01-17 15:12:51 -0800739 audio, msg);
Andreas Huberf9334412010-12-15 15:17:42 -0800740
Andreas Huber5bc087c2010-12-23 10:27:40 -0800741 if (err == -EWOULDBLOCK) {
Andreas Hubereac68ba2011-09-27 12:12:25 -0700742 if (mSource->feedMoreTSData() == OK) {
Andreas Huber1183a4a2011-11-03 11:00:21 -0700743 msg->post(10000ll);
Andreas Huber5bc087c2010-12-23 10:27:40 -0800744 }
Andreas Huberf9334412010-12-15 15:17:42 -0800745 }
Lajos Molnar1cd13982014-01-17 15:12:51 -0800746 } else if (what == Decoder::kWhatEOS) {
Andreas Huberdc9bacd2011-09-26 10:53:29 -0700747 int32_t err;
Lajos Molnar1cd13982014-01-17 15:12:51 -0800748 CHECK(msg->findInt32("err", &err));
Andreas Huberdc9bacd2011-09-26 10:53:29 -0700749
750 if (err == ERROR_END_OF_STREAM) {
Steve Block3856b092011-10-20 11:56:00 +0100751 ALOGV("got %s decoder EOS", audio ? "audio" : "video");
Andreas Huberdc9bacd2011-09-26 10:53:29 -0700752 } else {
Steve Block3856b092011-10-20 11:56:00 +0100753 ALOGV("got %s decoder EOS w/ error %d",
Andreas Huberdc9bacd2011-09-26 10:53:29 -0700754 audio ? "audio" : "video",
755 err);
756 }
757
758 mRenderer->queueEOS(audio, err);
Lajos Molnar1cd13982014-01-17 15:12:51 -0800759 } else if (what == Decoder::kWhatFlushCompleted) {
Andreas Huber1aef2112011-01-04 14:01:29 -0800760 bool needShutdown;
Andreas Huber53df1a42010-12-22 10:03:04 -0800761
Andreas Huberf9334412010-12-15 15:17:42 -0800762 if (audio) {
Andreas Huber1aef2112011-01-04 14:01:29 -0800763 CHECK(IsFlushingState(mFlushingAudio, &needShutdown));
Andreas Huberf9334412010-12-15 15:17:42 -0800764 mFlushingAudio = FLUSHED;
765 } else {
Andreas Huber1aef2112011-01-04 14:01:29 -0800766 CHECK(IsFlushingState(mFlushingVideo, &needShutdown));
Andreas Huberf9334412010-12-15 15:17:42 -0800767 mFlushingVideo = FLUSHED;
Andreas Huber3fe62152011-09-16 15:09:22 -0700768
769 mVideoLateByUs = 0;
Andreas Huberf9334412010-12-15 15:17:42 -0800770 }
771
Steve Block3856b092011-10-20 11:56:00 +0100772 ALOGV("decoder %s flush completed", audio ? "audio" : "video");
Andreas Huberf9334412010-12-15 15:17:42 -0800773
Andreas Huber1aef2112011-01-04 14:01:29 -0800774 if (needShutdown) {
Steve Block3856b092011-10-20 11:56:00 +0100775 ALOGV("initiating %s decoder shutdown",
Andreas Huber53df1a42010-12-22 10:03:04 -0800776 audio ? "audio" : "video");
Andreas Huberf9334412010-12-15 15:17:42 -0800777
Lajos Molnar87603c02014-08-20 19:25:30 -0700778 getDecoder(audio)->initiateShutdown();
Andreas Huberf9334412010-12-15 15:17:42 -0800779
Andreas Huber53df1a42010-12-22 10:03:04 -0800780 if (audio) {
781 mFlushingAudio = SHUTTING_DOWN_DECODER;
782 } else {
783 mFlushingVideo = SHUTTING_DOWN_DECODER;
784 }
Andreas Huberf9334412010-12-15 15:17:42 -0800785 }
Andreas Huber3831a062010-12-21 10:22:33 -0800786
787 finishFlushIfPossible();
Lajos Molnar1cd13982014-01-17 15:12:51 -0800788 } else if (what == Decoder::kWhatOutputFormatChanged) {
789 sp<AMessage> format;
790 CHECK(msg->findMessage("format", &format));
791
Andreas Huber31e25082011-01-10 10:38:31 -0800792 if (audio) {
Andy Hung282a7e32014-08-14 15:56:34 -0700793 openAudioSink(format, false /*offloadOnly*/);
Andreas Huber31e25082011-01-10 10:38:31 -0800794 } else {
795 // video
Chong Zhangced1c2f2014-08-08 15:22:35 -0700796 sp<AMessage> inputFormat =
797 mSource->getFormat(false /* audio */);
Andreas Huber3831a062010-12-21 10:22:33 -0800798
Chong Zhangced1c2f2014-08-08 15:22:35 -0700799 updateVideoSize(inputFormat, format);
Andreas Huber31e25082011-01-10 10:38:31 -0800800 }
Lajos Molnar1cd13982014-01-17 15:12:51 -0800801 } else if (what == Decoder::kWhatShutdownCompleted) {
Steve Block3856b092011-10-20 11:56:00 +0100802 ALOGV("%s shutdown completed", audio ? "audio" : "video");
Andreas Huber3831a062010-12-21 10:22:33 -0800803 if (audio) {
804 mAudioDecoder.clear();
805
806 CHECK_EQ((int)mFlushingAudio, (int)SHUTTING_DOWN_DECODER);
807 mFlushingAudio = SHUT_DOWN;
808 } else {
809 mVideoDecoder.clear();
810
811 CHECK_EQ((int)mFlushingVideo, (int)SHUTTING_DOWN_DECODER);
812 mFlushingVideo = SHUT_DOWN;
813 }
814
815 finishFlushIfPossible();
Lajos Molnar1cd13982014-01-17 15:12:51 -0800816 } else if (what == Decoder::kWhatError) {
Steve Block29357bc2012-01-06 19:20:56 +0000817 ALOGE("Received error from %s decoder, aborting playback.",
Andreas Huberc92fd242011-08-16 13:48:44 -0700818 audio ? "audio" : "video");
819
Chong Zhangf4c0a942014-08-11 15:14:10 -0700820 status_t err;
821 if (!msg->findInt32("err", &err)) {
822 err = UNKNOWN_ERROR;
823 }
824 mRenderer->queueEOS(audio, err);
Marco Nelissen9e2b7912014-08-18 16:13:03 -0700825 if (audio && mFlushingAudio != NONE) {
826 mAudioDecoder.clear();
827 mFlushingAudio = SHUT_DOWN;
828 } else if (!audio && mFlushingVideo != NONE){
829 mVideoDecoder.clear();
830 mFlushingVideo = SHUT_DOWN;
831 }
832 finishFlushIfPossible();
Lajos Molnar1cd13982014-01-17 15:12:51 -0800833 } else if (what == Decoder::kWhatDrainThisBuffer) {
834 renderBuffer(audio, msg);
835 } else {
836 ALOGV("Unhandled decoder notification %d '%c%c%c%c'.",
Andreas Hubera1f8ab02012-11-30 10:53:22 -0800837 what,
838 what >> 24,
839 (what >> 16) & 0xff,
840 (what >> 8) & 0xff,
841 what & 0xff);
Andreas Huberf9334412010-12-15 15:17:42 -0800842 }
843
844 break;
845 }
846
847 case kWhatRendererNotify:
848 {
849 int32_t what;
850 CHECK(msg->findInt32("what", &what));
851
852 if (what == Renderer::kWhatEOS) {
853 int32_t audio;
854 CHECK(msg->findInt32("audio", &audio));
855
Andreas Huberc92fd242011-08-16 13:48:44 -0700856 int32_t finalResult;
857 CHECK(msg->findInt32("finalResult", &finalResult));
858
Andreas Huberf9334412010-12-15 15:17:42 -0800859 if (audio) {
860 mAudioEOS = true;
861 } else {
862 mVideoEOS = true;
863 }
864
Andreas Huberc92fd242011-08-16 13:48:44 -0700865 if (finalResult == ERROR_END_OF_STREAM) {
Steve Block3856b092011-10-20 11:56:00 +0100866 ALOGV("reached %s EOS", audio ? "audio" : "video");
Andreas Huberc92fd242011-08-16 13:48:44 -0700867 } else {
Steve Block29357bc2012-01-06 19:20:56 +0000868 ALOGE("%s track encountered an error (%d)",
Andreas Huberc92fd242011-08-16 13:48:44 -0700869 audio ? "audio" : "video", finalResult);
870
871 notifyListener(
872 MEDIA_ERROR, MEDIA_ERROR_UNKNOWN, finalResult);
873 }
Andreas Huberf9334412010-12-15 15:17:42 -0800874
875 if ((mAudioEOS || mAudioDecoder == NULL)
876 && (mVideoEOS || mVideoDecoder == NULL)) {
877 notifyListener(MEDIA_PLAYBACK_COMPLETE, 0, 0);
878 }
Andreas Huber43c3e6c2011-01-05 12:17:08 -0800879 } else if (what == Renderer::kWhatPosition) {
880 int64_t positionUs;
881 CHECK(msg->findInt64("positionUs", &positionUs));
Wei Jiaac428aa2014-09-02 19:01:34 -0700882 mCurrentPositionUs = positionUs;
Andreas Huber43c3e6c2011-01-05 12:17:08 -0800883
Andreas Huber3fe62152011-09-16 15:09:22 -0700884 CHECK(msg->findInt64("videoLateByUs", &mVideoLateByUs));
885
Andreas Huber43c3e6c2011-01-05 12:17:08 -0800886 if (mDriver != NULL) {
887 sp<NuPlayerDriver> driver = mDriver.promote();
888 if (driver != NULL) {
889 driver->notifyPosition(positionUs);
Andreas Huber3fe62152011-09-16 15:09:22 -0700890
891 driver->notifyFrameStats(
892 mNumFramesTotal, mNumFramesDropped);
Andreas Huber43c3e6c2011-01-05 12:17:08 -0800893 }
894 }
Andreas Huber3fe62152011-09-16 15:09:22 -0700895 } else if (what == Renderer::kWhatFlushComplete) {
Andreas Huberf9334412010-12-15 15:17:42 -0800896 int32_t audio;
897 CHECK(msg->findInt32("audio", &audio));
898
Steve Block3856b092011-10-20 11:56:00 +0100899 ALOGV("renderer %s flush completed.", audio ? "audio" : "video");
James Dongf57b4ea2012-07-20 13:38:36 -0700900 } else if (what == Renderer::kWhatVideoRenderingStart) {
901 notifyListener(MEDIA_INFO, MEDIA_INFO_RENDERING_START, 0);
Lajos Molnarcbaffcf2013-08-14 18:30:38 -0700902 } else if (what == Renderer::kWhatMediaRenderingStart) {
903 ALOGV("media rendering started");
904 notifyListener(MEDIA_STARTED, 0, 0);
Wei Jia3a2956d2014-07-22 16:01:33 -0700905 } else if (what == Renderer::kWhatAudioOffloadTearDown) {
906 ALOGV("Tear down audio offload, fall back to s/w path");
907 int64_t positionUs;
908 CHECK(msg->findInt64("positionUs", &positionUs));
Andy Hung282a7e32014-08-14 15:56:34 -0700909 closeAudioSink();
Wei Jia3a2956d2014-07-22 16:01:33 -0700910 mAudioDecoder.clear();
911 mRenderer->flush(true /* audio */);
912 if (mVideoDecoder != NULL) {
913 mRenderer->flush(false /* audio */);
914 }
915 mRenderer->signalDisableOffloadAudio();
916 mOffloadAudio = false;
917
918 performSeek(positionUs);
919 instantiateDecoder(true /* audio */, &mAudioDecoder);
Andreas Huberf9334412010-12-15 15:17:42 -0800920 }
921 break;
922 }
923
924 case kWhatMoreDataQueued:
925 {
926 break;
927 }
928
Andreas Huber1aef2112011-01-04 14:01:29 -0800929 case kWhatReset:
930 {
Steve Block3856b092011-10-20 11:56:00 +0100931 ALOGV("kWhatReset");
Andreas Huber1aef2112011-01-04 14:01:29 -0800932
Andreas Hubera1f8ab02012-11-30 10:53:22 -0800933 mDeferredActions.push_back(
Andreas Huber14f76722013-01-15 09:04:18 -0800934 new ShutdownDecoderAction(
935 true /* audio */, true /* video */));
Andreas Huberb7c8e912012-11-27 15:02:53 -0800936
Andreas Hubera1f8ab02012-11-30 10:53:22 -0800937 mDeferredActions.push_back(
938 new SimpleAction(&NuPlayer::performReset));
Andreas Huberb58ce9f2011-11-28 16:27:35 -0800939
Andreas Hubera1f8ab02012-11-30 10:53:22 -0800940 processDeferredActions();
Andreas Huber1aef2112011-01-04 14:01:29 -0800941 break;
942 }
943
Andreas Huber43c3e6c2011-01-05 12:17:08 -0800944 case kWhatSeek:
945 {
946 int64_t seekTimeUs;
947 CHECK(msg->findInt64("seekTimeUs", &seekTimeUs));
948
Andreas Hubera1f8ab02012-11-30 10:53:22 -0800949 ALOGV("kWhatSeek seekTimeUs=%lld us", seekTimeUs);
Andreas Huber43c3e6c2011-01-05 12:17:08 -0800950
Andreas Hubera1f8ab02012-11-30 10:53:22 -0800951 mDeferredActions.push_back(
952 new SimpleAction(&NuPlayer::performDecoderFlush));
Andreas Huber43c3e6c2011-01-05 12:17:08 -0800953
Andreas Hubera1f8ab02012-11-30 10:53:22 -0800954 mDeferredActions.push_back(new SeekAction(seekTimeUs));
Andreas Huber43c3e6c2011-01-05 12:17:08 -0800955
Andreas Hubera1f8ab02012-11-30 10:53:22 -0800956 processDeferredActions();
Andreas Huber43c3e6c2011-01-05 12:17:08 -0800957 break;
958 }
959
Andreas Huberb4082222011-01-20 15:23:04 -0800960 case kWhatPause:
961 {
962 CHECK(mRenderer != NULL);
Roger Jönssonfba60da2013-01-21 17:15:45 +0100963 mSource->pause();
Andreas Huberb4082222011-01-20 15:23:04 -0800964 mRenderer->pause();
965 break;
966 }
967
968 case kWhatResume:
969 {
970 CHECK(mRenderer != NULL);
Roger Jönssonfba60da2013-01-21 17:15:45 +0100971 mSource->resume();
Andreas Huberb4082222011-01-20 15:23:04 -0800972 mRenderer->resume();
973 break;
974 }
975
Andreas Huberb5f25f02013-02-05 10:14:26 -0800976 case kWhatSourceNotify:
977 {
Andreas Huber9575c962013-02-05 13:59:56 -0800978 onSourceNotify(msg);
Andreas Huberb5f25f02013-02-05 10:14:26 -0800979 break;
980 }
981
Chong Zhanga7fa1d92014-06-11 14:49:23 -0700982 case kWhatClosedCaptionNotify:
983 {
984 onClosedCaptionNotify(msg);
985 break;
986 }
987
Andreas Huberf9334412010-12-15 15:17:42 -0800988 default:
989 TRESPASS();
990 break;
991 }
992}
993
Andreas Huber3831a062010-12-21 10:22:33 -0800994void NuPlayer::finishFlushIfPossible() {
Wei Jia53904f32014-07-29 10:22:53 -0700995 if (mFlushingAudio != NONE && mFlushingAudio != FLUSHED
996 && mFlushingAudio != SHUT_DOWN) {
Andreas Huber3831a062010-12-21 10:22:33 -0800997 return;
998 }
999
Wei Jia53904f32014-07-29 10:22:53 -07001000 if (mFlushingVideo != NONE && mFlushingVideo != FLUSHED
1001 && mFlushingVideo != SHUT_DOWN) {
Andreas Huber3831a062010-12-21 10:22:33 -08001002 return;
1003 }
1004
Steve Block3856b092011-10-20 11:56:00 +01001005 ALOGV("both audio and video are flushed now.");
Andreas Huber3831a062010-12-21 10:22:33 -08001006
Phil Burk9f526492014-09-03 15:04:12 -07001007 mPendingAudioAccessUnit.clear();
1008
Andreas Huber6e3d3112011-11-28 12:36:11 -08001009 if (mTimeDiscontinuityPending) {
1010 mRenderer->signalTimeDiscontinuity();
1011 mTimeDiscontinuityPending = false;
1012 }
Andreas Huber3831a062010-12-21 10:22:33 -08001013
Wei Jia53904f32014-07-29 10:22:53 -07001014 if (mAudioDecoder != NULL && mFlushingAudio == FLUSHED) {
Andreas Huber3831a062010-12-21 10:22:33 -08001015 mAudioDecoder->signalResume();
1016 }
1017
Wei Jia53904f32014-07-29 10:22:53 -07001018 if (mVideoDecoder != NULL && mFlushingVideo == FLUSHED) {
Andreas Huber3831a062010-12-21 10:22:33 -08001019 mVideoDecoder->signalResume();
1020 }
1021
1022 mFlushingAudio = NONE;
1023 mFlushingVideo = NONE;
Andreas Huber3831a062010-12-21 10:22:33 -08001024
Andreas Hubera1f8ab02012-11-30 10:53:22 -08001025 processDeferredActions();
Andreas Huber1aef2112011-01-04 14:01:29 -08001026}
1027
1028void NuPlayer::postScanSources() {
1029 if (mScanSourcesPending) {
1030 return;
1031 }
1032
1033 sp<AMessage> msg = new AMessage(kWhatScanSources, id());
1034 msg->setInt32("generation", mScanSourcesGeneration);
1035 msg->post();
1036
1037 mScanSourcesPending = true;
1038}
1039
Andy Hung282a7e32014-08-14 15:56:34 -07001040void NuPlayer::openAudioSink(const sp<AMessage> &format, bool offloadOnly) {
1041 ALOGV("openAudioSink: offloadOnly(%d) mOffloadAudio(%d)",
1042 offloadOnly, mOffloadAudio);
1043 bool audioSinkChanged = false;
1044
1045 int32_t numChannels;
1046 CHECK(format->findInt32("channel-count", &numChannels));
1047
1048 int32_t channelMask;
1049 if (!format->findInt32("channel-mask", &channelMask)) {
1050 // signal to the AudioSink to derive the mask from count.
1051 channelMask = CHANNEL_MASK_USE_CHANNEL_ORDER;
1052 }
1053
1054 int32_t sampleRate;
1055 CHECK(format->findInt32("sample-rate", &sampleRate));
1056
1057 uint32_t flags;
1058 int64_t durationUs;
1059 // FIXME: we should handle the case where the video decoder
1060 // is created after we receive the format change indication.
1061 // Current code will just make that we select deep buffer
1062 // with video which should not be a problem as it should
1063 // not prevent from keeping A/V sync.
1064 if (mVideoDecoder == NULL &&
1065 mSource->getDuration(&durationUs) == OK &&
1066 durationUs
1067 > AUDIO_SINK_MIN_DEEP_BUFFER_DURATION_US) {
1068 flags = AUDIO_OUTPUT_FLAG_DEEP_BUFFER;
1069 } else {
1070 flags = AUDIO_OUTPUT_FLAG_NONE;
1071 }
1072
1073 if (mOffloadAudio) {
1074 audio_format_t audioFormat = AUDIO_FORMAT_PCM_16_BIT;
1075 AString mime;
1076 CHECK(format->findString("mime", &mime));
1077 status_t err = mapMimeToAudioFormat(audioFormat, mime.c_str());
1078
1079 if (err != OK) {
1080 ALOGE("Couldn't map mime \"%s\" to a valid "
1081 "audio_format", mime.c_str());
1082 mOffloadAudio = false;
1083 } else {
1084 ALOGV("Mime \"%s\" mapped to audio_format 0x%x",
1085 mime.c_str(), audioFormat);
1086
1087 int avgBitRate = -1;
1088 format->findInt32("bit-rate", &avgBitRate);
1089
1090 int32_t aacProfile = -1;
1091 if (audioFormat == AUDIO_FORMAT_AAC
1092 && format->findInt32("aac-profile", &aacProfile)) {
1093 // Redefine AAC format as per aac profile
1094 mapAACProfileToAudioFormat(
1095 audioFormat,
1096 aacProfile);
1097 }
1098
1099 audio_offload_info_t offloadInfo = AUDIO_INFO_INITIALIZER;
1100 offloadInfo.duration_us = -1;
1101 format->findInt64(
1102 "durationUs", &offloadInfo.duration_us);
1103 offloadInfo.sample_rate = sampleRate;
1104 offloadInfo.channel_mask = channelMask;
1105 offloadInfo.format = audioFormat;
1106 offloadInfo.stream_type = AUDIO_STREAM_MUSIC;
1107 offloadInfo.bit_rate = avgBitRate;
1108 offloadInfo.has_video = (mVideoDecoder != NULL);
1109 offloadInfo.is_streaming = true;
1110
1111 if (memcmp(&mCurrentOffloadInfo, &offloadInfo, sizeof(offloadInfo)) == 0) {
1112 ALOGV("openAudioSink: no change in offload mode");
1113 return; // no change from previous configuration, everything ok.
1114 }
1115 ALOGV("openAudioSink: try to open AudioSink in offload mode");
1116 flags |= AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD;
Ronghua Wu1ffb5382014-08-18 15:57:03 -07001117 flags &= ~AUDIO_OUTPUT_FLAG_DEEP_BUFFER;
Andy Hung282a7e32014-08-14 15:56:34 -07001118 audioSinkChanged = true;
1119 mAudioSink->close();
1120 err = mAudioSink->open(
1121 sampleRate,
1122 numChannels,
1123 (audio_channel_mask_t)channelMask,
1124 audioFormat,
1125 8 /* bufferCount */,
1126 &NuPlayer::Renderer::AudioSinkCallback,
1127 mRenderer.get(),
1128 (audio_output_flags_t)flags,
1129 &offloadInfo);
1130
1131 if (err == OK) {
1132 // If the playback is offloaded to h/w, we pass
1133 // the HAL some metadata information.
1134 // We don't want to do this for PCM because it
1135 // will be going through the AudioFlinger mixer
1136 // before reaching the hardware.
1137 sp<MetaData> audioMeta =
1138 mSource->getFormatMeta(true /* audio */);
1139 sendMetaDataToHal(mAudioSink, audioMeta);
1140 mCurrentOffloadInfo = offloadInfo;
1141 err = mAudioSink->start();
1142 ALOGV_IF(err == OK, "openAudioSink: offload succeeded");
1143 }
1144 if (err != OK) {
1145 // Clean up, fall back to non offload mode.
1146 mAudioSink->close();
1147 mRenderer->signalDisableOffloadAudio();
1148 mOffloadAudio = false;
1149 mCurrentOffloadInfo = AUDIO_INFO_INITIALIZER;
1150 ALOGV("openAudioSink: offload failed");
1151 }
1152 }
1153 }
1154 if (!offloadOnly && !mOffloadAudio) {
1155 flags &= ~AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD;
1156 ALOGV("openAudioSink: open AudioSink in NON-offload mode");
1157
1158 audioSinkChanged = true;
1159 mAudioSink->close();
1160 mCurrentOffloadInfo = AUDIO_INFO_INITIALIZER;
1161 CHECK_EQ(mAudioSink->open(
1162 sampleRate,
1163 numChannels,
1164 (audio_channel_mask_t)channelMask,
1165 AUDIO_FORMAT_PCM_16_BIT,
1166 8 /* bufferCount */,
1167 NULL,
1168 NULL,
1169 (audio_output_flags_t)flags),
1170 (status_t)OK);
1171 mAudioSink->start();
1172 }
1173 if (audioSinkChanged) {
1174 mRenderer->signalAudioSinkChanged();
1175 }
1176}
1177
1178void NuPlayer::closeAudioSink() {
1179 mAudioSink->close();
1180 mCurrentOffloadInfo = AUDIO_INFO_INITIALIZER;
1181}
1182
Andreas Huber5bc087c2010-12-23 10:27:40 -08001183status_t NuPlayer::instantiateDecoder(bool audio, sp<Decoder> *decoder) {
Andreas Huberf9334412010-12-15 15:17:42 -08001184 if (*decoder != NULL) {
1185 return OK;
1186 }
1187
Andreas Huber84066782011-08-16 09:34:26 -07001188 sp<AMessage> format = mSource->getFormat(audio);
Andreas Huberf9334412010-12-15 15:17:42 -08001189
Andreas Huber84066782011-08-16 09:34:26 -07001190 if (format == NULL) {
Andreas Huberf9334412010-12-15 15:17:42 -08001191 return -EWOULDBLOCK;
1192 }
1193
Andreas Huber3fe62152011-09-16 15:09:22 -07001194 if (!audio) {
Andreas Huber84066782011-08-16 09:34:26 -07001195 AString mime;
1196 CHECK(format->findString("mime", &mime));
1197 mVideoIsAVC = !strcasecmp(MEDIA_MIMETYPE_VIDEO_AVC, mime.c_str());
Chong Zhanga7fa1d92014-06-11 14:49:23 -07001198
1199 sp<AMessage> ccNotify = new AMessage(kWhatClosedCaptionNotify, id());
1200 mCCDecoder = new CCDecoder(ccNotify);
Lajos Molnar09524832014-07-17 14:29:51 -07001201
1202 if (mSourceFlags & Source::FLAG_SECURE) {
1203 format->setInt32("secure", true);
1204 }
Andreas Huber3fe62152011-09-16 15:09:22 -07001205 }
1206
Wei Jiabc2fb722014-07-08 16:37:57 -07001207 if (audio) {
Wei Jia88703c32014-08-06 11:24:07 -07001208 sp<AMessage> notify = new AMessage(kWhatAudioNotify, id());
1209 ++mAudioDecoderGeneration;
1210 notify->setInt32("generation", mAudioDecoderGeneration);
1211
Wei Jiabc2fb722014-07-08 16:37:57 -07001212 if (mOffloadAudio) {
1213 *decoder = new DecoderPassThrough(notify);
1214 } else {
1215 *decoder = new Decoder(notify);
1216 }
1217 } else {
Wei Jia88703c32014-08-06 11:24:07 -07001218 sp<AMessage> notify = new AMessage(kWhatVideoNotify, id());
1219 ++mVideoDecoderGeneration;
1220 notify->setInt32("generation", mVideoDecoderGeneration);
1221
Wei Jiabc2fb722014-07-08 16:37:57 -07001222 *decoder = new Decoder(notify, mNativeWindow);
1223 }
Lajos Molnar1cd13982014-01-17 15:12:51 -08001224 (*decoder)->init();
Andreas Huber84066782011-08-16 09:34:26 -07001225 (*decoder)->configure(format);
Andreas Huberf9334412010-12-15 15:17:42 -08001226
Lajos Molnar09524832014-07-17 14:29:51 -07001227 // allocate buffers to decrypt widevine source buffers
1228 if (!audio && (mSourceFlags & Source::FLAG_SECURE)) {
1229 Vector<sp<ABuffer> > inputBufs;
1230 CHECK_EQ((*decoder)->getInputBuffers(&inputBufs), (status_t)OK);
1231
1232 Vector<MediaBuffer *> mediaBufs;
1233 for (size_t i = 0; i < inputBufs.size(); i++) {
1234 const sp<ABuffer> &buffer = inputBufs[i];
1235 MediaBuffer *mbuf = new MediaBuffer(buffer->data(), buffer->size());
1236 mediaBufs.push(mbuf);
1237 }
1238
1239 status_t err = mSource->setBuffers(audio, mediaBufs);
1240 if (err != OK) {
1241 for (size_t i = 0; i < mediaBufs.size(); ++i) {
1242 mediaBufs[i]->release();
1243 }
1244 mediaBufs.clear();
1245 ALOGE("Secure source didn't support secure mediaBufs.");
1246 return err;
1247 }
1248 }
Andreas Huberf9334412010-12-15 15:17:42 -08001249 return OK;
1250}
1251
1252status_t NuPlayer::feedDecoderInputData(bool audio, const sp<AMessage> &msg) {
1253 sp<AMessage> reply;
1254 CHECK(msg->findMessage("reply", &reply));
1255
Wei Jia53904f32014-07-29 10:22:53 -07001256 if ((audio && mFlushingAudio != NONE)
Wei Jiaf702d042014-09-09 12:08:47 -07001257 || (!audio && mFlushingVideo != NONE)
1258 || mSource == NULL) {
Wei Jiab189a5b2014-08-07 06:11:39 +00001259 reply->setInt32("err", INFO_DISCONTINUITY);
1260 reply->post();
1261 return OK;
Andreas Huberf9334412010-12-15 15:17:42 -08001262 }
1263
1264 sp<ABuffer> accessUnit;
Andreas Huberf9334412010-12-15 15:17:42 -08001265
Phil Burk9f526492014-09-03 15:04:12 -07001266 // Aggregate smaller buffers into a larger buffer.
1267 // The goal is to reduce power consumption.
1268 // Unfortunately this does not work with the software AAC decoder.
1269 // TODO optimize buffer size for power consumption
1270 // The offload read buffer size is 32 KB but 24 KB uses less power.
1271 const int kAudioBigBufferSizeBytes = 24 * 1024;
1272 bool doBufferAggregation = (audio && mOffloadAudio);
1273 sp<ABuffer> biggerBuffer;
1274 bool needMoreData = false;
1275 int numSmallBuffers = 0;
1276 bool gotTime = false;
1277
Andreas Huber3fe62152011-09-16 15:09:22 -07001278 bool dropAccessUnit;
1279 do {
Phil Burk9f526492014-09-03 15:04:12 -07001280 status_t err;
1281 // Did we save an accessUnit earlier because of a discontinuity?
1282 if (audio && (mPendingAudioAccessUnit != NULL)) {
1283 accessUnit = mPendingAudioAccessUnit;
1284 mPendingAudioAccessUnit.clear();
1285 err = mPendingAudioErr;
1286 ALOGV("feedDecoderInputData() use mPendingAudioAccessUnit");
1287 } else {
1288 err = mSource->dequeueAccessUnit(audio, &accessUnit);
1289 }
Andreas Huber5bc087c2010-12-23 10:27:40 -08001290
Andreas Huber3fe62152011-09-16 15:09:22 -07001291 if (err == -EWOULDBLOCK) {
Phil Burk9f526492014-09-03 15:04:12 -07001292 if (biggerBuffer == NULL) {
1293 return err;
1294 } else {
1295 break; // Reply with data that we already have.
1296 }
Andreas Huber3fe62152011-09-16 15:09:22 -07001297 } else if (err != OK) {
1298 if (err == INFO_DISCONTINUITY) {
Phil Burk9f526492014-09-03 15:04:12 -07001299 if (biggerBuffer != NULL) {
1300 // We already have some data so save this for later.
1301 mPendingAudioErr = err;
1302 mPendingAudioAccessUnit = accessUnit;
1303 accessUnit.clear();
1304 ALOGD("feedDecoderInputData() save discontinuity for later");
1305 break;
1306 }
Andreas Huber3fe62152011-09-16 15:09:22 -07001307 int32_t type;
1308 CHECK(accessUnit->meta()->findInt32("discontinuity", &type));
Andreas Huber53df1a42010-12-22 10:03:04 -08001309
Andreas Huber3fe62152011-09-16 15:09:22 -07001310 bool formatChange =
Andreas Huber6e3d3112011-11-28 12:36:11 -08001311 (audio &&
1312 (type & ATSParser::DISCONTINUITY_AUDIO_FORMAT))
1313 || (!audio &&
1314 (type & ATSParser::DISCONTINUITY_VIDEO_FORMAT));
Andreas Huber53df1a42010-12-22 10:03:04 -08001315
Andreas Huber6e3d3112011-11-28 12:36:11 -08001316 bool timeChange = (type & ATSParser::DISCONTINUITY_TIME) != 0;
1317
Steve Blockdf64d152012-01-04 20:05:49 +00001318 ALOGI("%s discontinuity (formatChange=%d, time=%d)",
Andreas Huber6e3d3112011-11-28 12:36:11 -08001319 audio ? "audio" : "video", formatChange, timeChange);
Andreas Huber32f3cef2011-03-02 15:34:46 -08001320
Andreas Huber3fe62152011-09-16 15:09:22 -07001321 if (audio) {
1322 mSkipRenderingAudioUntilMediaTimeUs = -1;
1323 } else {
1324 mSkipRenderingVideoUntilMediaTimeUs = -1;
1325 }
Andreas Huber32f3cef2011-03-02 15:34:46 -08001326
Andreas Huber6e3d3112011-11-28 12:36:11 -08001327 if (timeChange) {
1328 sp<AMessage> extra;
1329 if (accessUnit->meta()->findMessage("extra", &extra)
1330 && extra != NULL) {
1331 int64_t resumeAtMediaTimeUs;
1332 if (extra->findInt64(
1333 "resume-at-mediatimeUs", &resumeAtMediaTimeUs)) {
Steve Blockdf64d152012-01-04 20:05:49 +00001334 ALOGI("suppressing rendering of %s until %lld us",
Andreas Huber6e3d3112011-11-28 12:36:11 -08001335 audio ? "audio" : "video", resumeAtMediaTimeUs);
Andreas Huber3fe62152011-09-16 15:09:22 -07001336
Andreas Huber6e3d3112011-11-28 12:36:11 -08001337 if (audio) {
1338 mSkipRenderingAudioUntilMediaTimeUs =
1339 resumeAtMediaTimeUs;
1340 } else {
1341 mSkipRenderingVideoUntilMediaTimeUs =
1342 resumeAtMediaTimeUs;
1343 }
Andreas Huber3fe62152011-09-16 15:09:22 -07001344 }
Andreas Huber32f3cef2011-03-02 15:34:46 -08001345 }
1346 }
Andreas Huber3fe62152011-09-16 15:09:22 -07001347
Andreas Huber6e3d3112011-11-28 12:36:11 -08001348 mTimeDiscontinuityPending =
1349 mTimeDiscontinuityPending || timeChange;
1350
Lajos Molnar87603c02014-08-20 19:25:30 -07001351 bool seamlessFormatChange = false;
1352 sp<AMessage> newFormat = mSource->getFormat(audio);
1353 if (formatChange) {
1354 seamlessFormatChange =
1355 getDecoder(audio)->supportsSeamlessFormatChange(newFormat);
1356 // treat seamless format change separately
1357 formatChange = !seamlessFormatChange;
1358 }
1359 bool shutdownOrFlush = formatChange || timeChange;
1360
1361 // We want to queue up scan-sources only once per discontinuity.
1362 // We control this by doing it only if neither audio nor video are
1363 // flushing or shutting down. (After handling 1st discontinuity, one
1364 // of the flushing states will not be NONE.)
1365 // No need to scan sources if this discontinuity does not result
1366 // in a flush or shutdown, as the flushing state will stay NONE.
1367 if (mFlushingAudio == NONE && mFlushingVideo == NONE &&
1368 shutdownOrFlush) {
Robert Shiha2981012014-07-30 17:41:24 -07001369 // And we'll resume scanning sources once we're done
1370 // flushing.
1371 mDeferredActions.push_front(
1372 new SimpleAction(
1373 &NuPlayer::performScanSources));
1374 }
1375
Lajos Molnar87603c02014-08-20 19:25:30 -07001376 if (formatChange /* not seamless */) {
1377 // must change decoder
1378 flushDecoder(audio, /* needShutdown = */ true);
1379 } else if (timeChange) {
1380 // need to flush
1381 flushDecoder(audio, /* needShutdown = */ false, newFormat);
1382 err = OK;
1383 } else if (seamlessFormatChange) {
1384 // reuse existing decoder and don't flush
1385 updateDecoderFormatWithoutFlush(audio, newFormat);
1386 err = OK;
Andreas Huber6e3d3112011-11-28 12:36:11 -08001387 } else {
1388 // This stream is unaffected by the discontinuity
Andreas Huber6e3d3112011-11-28 12:36:11 -08001389 return -EWOULDBLOCK;
1390 }
Andreas Huber32f3cef2011-03-02 15:34:46 -08001391 }
1392
Andreas Huber3fe62152011-09-16 15:09:22 -07001393 reply->setInt32("err", err);
1394 reply->post();
1395 return OK;
Andreas Huberf9334412010-12-15 15:17:42 -08001396 }
1397
Andreas Huber3fe62152011-09-16 15:09:22 -07001398 if (!audio) {
1399 ++mNumFramesTotal;
1400 }
1401
1402 dropAccessUnit = false;
1403 if (!audio
Lajos Molnar09524832014-07-17 14:29:51 -07001404 && !(mSourceFlags & Source::FLAG_SECURE)
Andreas Huber3fe62152011-09-16 15:09:22 -07001405 && mVideoLateByUs > 100000ll
1406 && mVideoIsAVC
1407 && !IsAVCReferenceFrame(accessUnit)) {
1408 dropAccessUnit = true;
1409 ++mNumFramesDropped;
1410 }
Phil Burk9f526492014-09-03 15:04:12 -07001411
1412 size_t smallSize = accessUnit->size();
1413 needMoreData = false;
1414 if (doBufferAggregation && (biggerBuffer == NULL)
1415 // Don't bother if only room for a few small buffers.
1416 && (smallSize < (kAudioBigBufferSizeBytes / 3))) {
1417 // Create a larger buffer for combining smaller buffers from the extractor.
1418 biggerBuffer = new ABuffer(kAudioBigBufferSizeBytes);
1419 biggerBuffer->setRange(0, 0); // start empty
1420 }
1421
1422 if (biggerBuffer != NULL) {
1423 int64_t timeUs;
1424 bool smallTimestampValid = accessUnit->meta()->findInt64("timeUs", &timeUs);
1425 // Will the smaller buffer fit?
1426 size_t bigSize = biggerBuffer->size();
1427 size_t roomLeft = biggerBuffer->capacity() - bigSize;
1428 // Should we save this small buffer for the next big buffer?
1429 // If the first small buffer did not have a timestamp then save
1430 // any buffer that does have a timestamp until the next big buffer.
1431 if ((smallSize > roomLeft)
1432 || (!gotTime && (numSmallBuffers > 0) && smallTimestampValid)) {
1433 mPendingAudioErr = err;
1434 mPendingAudioAccessUnit = accessUnit;
1435 accessUnit.clear();
1436 } else {
1437 // Append small buffer to the bigger buffer.
1438 memcpy(biggerBuffer->base() + bigSize, accessUnit->data(), smallSize);
1439 bigSize += smallSize;
1440 biggerBuffer->setRange(0, bigSize);
1441
1442 // Keep looping until we run out of room in the biggerBuffer.
1443 needMoreData = true;
1444
1445 // Grab time from first small buffer if available.
1446 if ((numSmallBuffers == 0) && smallTimestampValid) {
1447 biggerBuffer->meta()->setInt64("timeUs", timeUs);
1448 gotTime = true;
1449 }
1450
1451 ALOGV("feedDecoderInputData() #%d, smallSize = %zu, bigSize = %zu, capacity = %zu",
1452 numSmallBuffers, smallSize, bigSize, biggerBuffer->capacity());
1453 numSmallBuffers++;
1454 }
1455 }
1456 } while (dropAccessUnit || needMoreData);
Andreas Huberf9334412010-12-15 15:17:42 -08001457
Steve Block3856b092011-10-20 11:56:00 +01001458 // ALOGV("returned a valid buffer of %s data", audio ? "audio" : "video");
Andreas Huberf9334412010-12-15 15:17:42 -08001459
1460#if 0
1461 int64_t mediaTimeUs;
1462 CHECK(accessUnit->meta()->findInt64("timeUs", &mediaTimeUs));
Steve Block3856b092011-10-20 11:56:00 +01001463 ALOGV("feeding %s input buffer at media time %.2f secs",
Andreas Huberf9334412010-12-15 15:17:42 -08001464 audio ? "audio" : "video",
1465 mediaTimeUs / 1E6);
1466#endif
1467
Chong Zhanga7fa1d92014-06-11 14:49:23 -07001468 if (!audio) {
1469 mCCDecoder->decode(accessUnit);
1470 }
1471
Phil Burk9f526492014-09-03 15:04:12 -07001472 if (biggerBuffer != NULL) {
1473 ALOGV("feedDecoderInputData() reply with aggregated buffer, %d", numSmallBuffers);
1474 reply->setBuffer("buffer", biggerBuffer);
1475 } else {
1476 reply->setBuffer("buffer", accessUnit);
1477 }
1478
Andreas Huberf9334412010-12-15 15:17:42 -08001479 reply->post();
1480
1481 return OK;
1482}
1483
1484void NuPlayer::renderBuffer(bool audio, const sp<AMessage> &msg) {
Steve Block3856b092011-10-20 11:56:00 +01001485 // ALOGV("renderBuffer %s", audio ? "audio" : "video");
Andreas Huberf9334412010-12-15 15:17:42 -08001486
1487 sp<AMessage> reply;
1488 CHECK(msg->findMessage("reply", &reply));
1489
Wei Jia53904f32014-07-29 10:22:53 -07001490 if ((audio && mFlushingAudio != NONE)
1491 || (!audio && mFlushingVideo != NONE)) {
Andreas Huber18ac5402011-08-31 15:04:25 -07001492 // We're currently attempting to flush the decoder, in order
1493 // to complete this, the decoder wants all its buffers back,
1494 // so we don't want any output buffers it sent us (from before
1495 // we initiated the flush) to be stuck in the renderer's queue.
1496
Steve Block3856b092011-10-20 11:56:00 +01001497 ALOGV("we're still flushing the %s decoder, sending its output buffer"
Andreas Huber18ac5402011-08-31 15:04:25 -07001498 " right back.", audio ? "audio" : "video");
1499
1500 reply->post();
1501 return;
1502 }
1503
Andreas Huber2d8bedd2012-02-21 14:38:23 -08001504 sp<ABuffer> buffer;
1505 CHECK(msg->findBuffer("buffer", &buffer));
Andreas Huberf9334412010-12-15 15:17:42 -08001506
Chong Zhanga7fa1d92014-06-11 14:49:23 -07001507 int64_t mediaTimeUs;
1508 CHECK(buffer->meta()->findInt64("timeUs", &mediaTimeUs));
1509
Andreas Huber32f3cef2011-03-02 15:34:46 -08001510 int64_t &skipUntilMediaTimeUs =
1511 audio
1512 ? mSkipRenderingAudioUntilMediaTimeUs
1513 : mSkipRenderingVideoUntilMediaTimeUs;
1514
1515 if (skipUntilMediaTimeUs >= 0) {
Andreas Huber32f3cef2011-03-02 15:34:46 -08001516
1517 if (mediaTimeUs < skipUntilMediaTimeUs) {
Steve Block3856b092011-10-20 11:56:00 +01001518 ALOGV("dropping %s buffer at time %lld as requested.",
Andreas Huber32f3cef2011-03-02 15:34:46 -08001519 audio ? "audio" : "video",
1520 mediaTimeUs);
1521
1522 reply->post();
1523 return;
1524 }
1525
1526 skipUntilMediaTimeUs = -1;
1527 }
1528
Chong Zhanga7fa1d92014-06-11 14:49:23 -07001529 if (!audio && mCCDecoder->isSelected()) {
1530 mCCDecoder->display(mediaTimeUs);
1531 }
1532
Andreas Huberf9334412010-12-15 15:17:42 -08001533 mRenderer->queueBuffer(audio, buffer, reply);
1534}
1535
Chong Zhangced1c2f2014-08-08 15:22:35 -07001536void NuPlayer::updateVideoSize(
1537 const sp<AMessage> &inputFormat,
1538 const sp<AMessage> &outputFormat) {
1539 if (inputFormat == NULL) {
1540 ALOGW("Unknown video size, reporting 0x0!");
1541 notifyListener(MEDIA_SET_VIDEO_SIZE, 0, 0);
1542 return;
1543 }
1544
1545 int32_t displayWidth, displayHeight;
1546 int32_t cropLeft, cropTop, cropRight, cropBottom;
1547
1548 if (outputFormat != NULL) {
1549 int32_t width, height;
1550 CHECK(outputFormat->findInt32("width", &width));
1551 CHECK(outputFormat->findInt32("height", &height));
1552
1553 int32_t cropLeft, cropTop, cropRight, cropBottom;
1554 CHECK(outputFormat->findRect(
1555 "crop",
1556 &cropLeft, &cropTop, &cropRight, &cropBottom));
1557
1558 displayWidth = cropRight - cropLeft + 1;
1559 displayHeight = cropBottom - cropTop + 1;
1560
1561 ALOGV("Video output format changed to %d x %d "
1562 "(crop: %d x %d @ (%d, %d))",
1563 width, height,
1564 displayWidth,
1565 displayHeight,
1566 cropLeft, cropTop);
1567 } else {
1568 CHECK(inputFormat->findInt32("width", &displayWidth));
1569 CHECK(inputFormat->findInt32("height", &displayHeight));
1570
1571 ALOGV("Video input format %d x %d", displayWidth, displayHeight);
1572 }
1573
1574 // Take into account sample aspect ratio if necessary:
1575 int32_t sarWidth, sarHeight;
1576 if (inputFormat->findInt32("sar-width", &sarWidth)
1577 && inputFormat->findInt32("sar-height", &sarHeight)) {
1578 ALOGV("Sample aspect ratio %d : %d", sarWidth, sarHeight);
1579
1580 displayWidth = (displayWidth * sarWidth) / sarHeight;
1581
1582 ALOGV("display dimensions %d x %d", displayWidth, displayHeight);
1583 }
1584
1585 int32_t rotationDegrees;
1586 if (!inputFormat->findInt32("rotation-degrees", &rotationDegrees)) {
1587 rotationDegrees = 0;
1588 }
1589
1590 if (rotationDegrees == 90 || rotationDegrees == 270) {
1591 int32_t tmp = displayWidth;
1592 displayWidth = displayHeight;
1593 displayHeight = tmp;
1594 }
1595
1596 notifyListener(
1597 MEDIA_SET_VIDEO_SIZE,
1598 displayWidth,
1599 displayHeight);
1600}
1601
Chong Zhangdcb89b32013-08-06 09:44:47 -07001602void NuPlayer::notifyListener(int msg, int ext1, int ext2, const Parcel *in) {
Andreas Huber43c3e6c2011-01-05 12:17:08 -08001603 if (mDriver == NULL) {
Andreas Huberf9334412010-12-15 15:17:42 -08001604 return;
1605 }
1606
Andreas Huber43c3e6c2011-01-05 12:17:08 -08001607 sp<NuPlayerDriver> driver = mDriver.promote();
Andreas Huberf9334412010-12-15 15:17:42 -08001608
Andreas Huber43c3e6c2011-01-05 12:17:08 -08001609 if (driver == NULL) {
Andreas Huberf9334412010-12-15 15:17:42 -08001610 return;
1611 }
1612
Chong Zhangdcb89b32013-08-06 09:44:47 -07001613 driver->notifyListener(msg, ext1, ext2, in);
Andreas Huberf9334412010-12-15 15:17:42 -08001614}
1615
Lajos Molnar87603c02014-08-20 19:25:30 -07001616void NuPlayer::flushDecoder(
1617 bool audio, bool needShutdown, const sp<AMessage> &newFormat) {
Andreas Huber14f76722013-01-15 09:04:18 -08001618 ALOGV("[%s] flushDecoder needShutdown=%d",
1619 audio ? "audio" : "video", needShutdown);
1620
Lajos Molnar87603c02014-08-20 19:25:30 -07001621 const sp<Decoder> &decoder = getDecoder(audio);
1622 if (decoder == NULL) {
Steve Blockdf64d152012-01-04 20:05:49 +00001623 ALOGI("flushDecoder %s without decoder present",
Andreas Huber6e3d3112011-11-28 12:36:11 -08001624 audio ? "audio" : "video");
Lajos Molnar87603c02014-08-20 19:25:30 -07001625 return;
Andreas Huber6e3d3112011-11-28 12:36:11 -08001626 }
1627
Andreas Huber1aef2112011-01-04 14:01:29 -08001628 // Make sure we don't continue to scan sources until we finish flushing.
1629 ++mScanSourcesGeneration;
Andreas Huber43c3e6c2011-01-05 12:17:08 -08001630 mScanSourcesPending = false;
Andreas Huber1aef2112011-01-04 14:01:29 -08001631
Lajos Molnar87603c02014-08-20 19:25:30 -07001632 decoder->signalFlush(newFormat);
Andreas Huber1aef2112011-01-04 14:01:29 -08001633 mRenderer->flush(audio);
1634
1635 FlushStatus newStatus =
1636 needShutdown ? FLUSHING_DECODER_SHUTDOWN : FLUSHING_DECODER;
1637
1638 if (audio) {
Wei Jia53904f32014-07-29 10:22:53 -07001639 ALOGE_IF(mFlushingAudio != NONE,
1640 "audio flushDecoder() is called in state %d", mFlushingAudio);
Andreas Huber1aef2112011-01-04 14:01:29 -08001641 mFlushingAudio = newStatus;
Andreas Huber1aef2112011-01-04 14:01:29 -08001642 } else {
Wei Jia53904f32014-07-29 10:22:53 -07001643 ALOGE_IF(mFlushingVideo != NONE,
1644 "video flushDecoder() is called in state %d", mFlushingVideo);
Andreas Huber1aef2112011-01-04 14:01:29 -08001645 mFlushingVideo = newStatus;
Chong Zhangb86e68f2014-08-01 13:46:53 -07001646
1647 if (mCCDecoder != NULL) {
1648 mCCDecoder->flush();
1649 }
Andreas Huber1aef2112011-01-04 14:01:29 -08001650 }
1651}
1652
Lajos Molnar87603c02014-08-20 19:25:30 -07001653void NuPlayer::updateDecoderFormatWithoutFlush(
1654 bool audio, const sp<AMessage> &format) {
1655 ALOGV("[%s] updateDecoderFormatWithoutFlush", audio ? "audio" : "video");
1656
1657 const sp<Decoder> &decoder = getDecoder(audio);
1658 if (decoder == NULL) {
1659 ALOGI("updateDecoderFormatWithoutFlush %s without decoder present",
1660 audio ? "audio" : "video");
1661 return;
1662 }
1663
1664 decoder->signalUpdateFormat(format);
1665}
1666
Chong Zhangced1c2f2014-08-08 15:22:35 -07001667void NuPlayer::queueDecoderShutdown(
1668 bool audio, bool video, const sp<AMessage> &reply) {
1669 ALOGI("queueDecoderShutdown audio=%d, video=%d", audio, video);
Andreas Huber84066782011-08-16 09:34:26 -07001670
Chong Zhangced1c2f2014-08-08 15:22:35 -07001671 mDeferredActions.push_back(
1672 new ShutdownDecoderAction(audio, video));
Andreas Huber84066782011-08-16 09:34:26 -07001673
Chong Zhangced1c2f2014-08-08 15:22:35 -07001674 mDeferredActions.push_back(
1675 new SimpleAction(&NuPlayer::performScanSources));
Andreas Huber84066782011-08-16 09:34:26 -07001676
Chong Zhangced1c2f2014-08-08 15:22:35 -07001677 mDeferredActions.push_back(new PostMessageAction(reply));
1678
1679 processDeferredActions();
Andreas Huber84066782011-08-16 09:34:26 -07001680}
1681
James Dong0d268a32012-08-31 12:18:27 -07001682status_t NuPlayer::setVideoScalingMode(int32_t mode) {
1683 mVideoScalingMode = mode;
Andreas Huber57a339c2012-12-03 11:18:00 -08001684 if (mNativeWindow != NULL) {
James Dong0d268a32012-08-31 12:18:27 -07001685 status_t ret = native_window_set_scaling_mode(
1686 mNativeWindow->getNativeWindow().get(), mVideoScalingMode);
1687 if (ret != OK) {
1688 ALOGE("Failed to set scaling mode (%d): %s",
1689 -ret, strerror(-ret));
1690 return ret;
1691 }
1692 }
1693 return OK;
1694}
1695
Chong Zhangdcb89b32013-08-06 09:44:47 -07001696status_t NuPlayer::getTrackInfo(Parcel* reply) const {
1697 sp<AMessage> msg = new AMessage(kWhatGetTrackInfo, id());
1698 msg->setPointer("reply", reply);
1699
1700 sp<AMessage> response;
1701 status_t err = msg->postAndAwaitResponse(&response);
1702 return err;
1703}
1704
Robert Shih7c4f0d72014-07-09 18:53:31 -07001705status_t NuPlayer::getSelectedTrack(int32_t type, Parcel* reply) const {
1706 sp<AMessage> msg = new AMessage(kWhatGetSelectedTrack, id());
1707 msg->setPointer("reply", reply);
1708 msg->setInt32("type", type);
1709
1710 sp<AMessage> response;
1711 status_t err = msg->postAndAwaitResponse(&response);
1712 if (err == OK && response != NULL) {
1713 CHECK(response->findInt32("err", &err));
1714 }
1715 return err;
1716}
1717
Chong Zhangdcb89b32013-08-06 09:44:47 -07001718status_t NuPlayer::selectTrack(size_t trackIndex, bool select) {
1719 sp<AMessage> msg = new AMessage(kWhatSelectTrack, id());
1720 msg->setSize("trackIndex", trackIndex);
1721 msg->setInt32("select", select);
1722
1723 sp<AMessage> response;
1724 status_t err = msg->postAndAwaitResponse(&response);
1725
Chong Zhang404fced2014-06-11 14:45:31 -07001726 if (err != OK) {
1727 return err;
1728 }
1729
1730 if (!response->findInt32("err", &err)) {
1731 err = OK;
1732 }
1733
Chong Zhangdcb89b32013-08-06 09:44:47 -07001734 return err;
1735}
1736
Andreas Huberb7c8e912012-11-27 15:02:53 -08001737void NuPlayer::schedulePollDuration() {
1738 sp<AMessage> msg = new AMessage(kWhatPollDuration, id());
1739 msg->setInt32("generation", mPollDurationGeneration);
1740 msg->post();
1741}
1742
1743void NuPlayer::cancelPollDuration() {
1744 ++mPollDurationGeneration;
1745}
1746
Andreas Hubera1f8ab02012-11-30 10:53:22 -08001747void NuPlayer::processDeferredActions() {
1748 while (!mDeferredActions.empty()) {
1749 // We won't execute any deferred actions until we're no longer in
1750 // an intermediate state, i.e. one more more decoders are currently
1751 // flushing or shutting down.
1752
Andreas Hubera1f8ab02012-11-30 10:53:22 -08001753 if (mFlushingAudio != NONE || mFlushingVideo != NONE) {
1754 // We're currently flushing, postpone the reset until that's
1755 // completed.
1756
1757 ALOGV("postponing action mFlushingAudio=%d, mFlushingVideo=%d",
1758 mFlushingAudio, mFlushingVideo);
1759
1760 break;
1761 }
1762
1763 sp<Action> action = *mDeferredActions.begin();
1764 mDeferredActions.erase(mDeferredActions.begin());
1765
1766 action->execute(this);
1767 }
1768}
1769
1770void NuPlayer::performSeek(int64_t seekTimeUs) {
1771 ALOGV("performSeek seekTimeUs=%lld us (%.2f secs)",
1772 seekTimeUs,
1773 seekTimeUs / 1E6);
1774
Andy Hungadf34bf2014-09-03 18:22:22 -07001775 if (mSource == NULL) {
1776 // This happens when reset occurs right before the loop mode
1777 // asynchronously seeks to the start of the stream.
1778 LOG_ALWAYS_FATAL_IF(mAudioDecoder != NULL || mVideoDecoder != NULL,
1779 "mSource is NULL and decoders not NULL audio(%p) video(%p)",
1780 mAudioDecoder.get(), mVideoDecoder.get());
1781 return;
1782 }
Andreas Hubera1f8ab02012-11-30 10:53:22 -08001783 mSource->seekTo(seekTimeUs);
Robert Shihd3b0bbb2014-07-23 15:00:25 -07001784 ++mTimedTextGeneration;
Andreas Hubera1f8ab02012-11-30 10:53:22 -08001785
1786 if (mDriver != NULL) {
1787 sp<NuPlayerDriver> driver = mDriver.promote();
1788 if (driver != NULL) {
1789 driver->notifyPosition(seekTimeUs);
1790 driver->notifySeekComplete();
1791 }
1792 }
1793
1794 // everything's flushed, continue playback.
1795}
1796
1797void NuPlayer::performDecoderFlush() {
1798 ALOGV("performDecoderFlush");
1799
Andreas Huberda9740e2013-04-16 10:54:03 -07001800 if (mAudioDecoder == NULL && mVideoDecoder == NULL) {
Andreas Hubera1f8ab02012-11-30 10:53:22 -08001801 return;
1802 }
1803
1804 mTimeDiscontinuityPending = true;
1805
1806 if (mAudioDecoder != NULL) {
1807 flushDecoder(true /* audio */, false /* needShutdown */);
1808 }
1809
1810 if (mVideoDecoder != NULL) {
1811 flushDecoder(false /* audio */, false /* needShutdown */);
1812 }
1813}
1814
Andreas Huber14f76722013-01-15 09:04:18 -08001815void NuPlayer::performDecoderShutdown(bool audio, bool video) {
1816 ALOGV("performDecoderShutdown audio=%d, video=%d", audio, video);
Andreas Hubera1f8ab02012-11-30 10:53:22 -08001817
Andreas Huber14f76722013-01-15 09:04:18 -08001818 if ((!audio || mAudioDecoder == NULL)
1819 && (!video || mVideoDecoder == NULL)) {
Andreas Hubera1f8ab02012-11-30 10:53:22 -08001820 return;
1821 }
1822
1823 mTimeDiscontinuityPending = true;
1824
Andreas Huber14f76722013-01-15 09:04:18 -08001825 if (audio && mAudioDecoder != NULL) {
Andreas Hubera1f8ab02012-11-30 10:53:22 -08001826 flushDecoder(true /* audio */, true /* needShutdown */);
1827 }
1828
Andreas Huber14f76722013-01-15 09:04:18 -08001829 if (video && mVideoDecoder != NULL) {
Andreas Hubera1f8ab02012-11-30 10:53:22 -08001830 flushDecoder(false /* audio */, true /* needShutdown */);
1831 }
1832}
1833
1834void NuPlayer::performReset() {
1835 ALOGV("performReset");
1836
1837 CHECK(mAudioDecoder == NULL);
1838 CHECK(mVideoDecoder == NULL);
1839
1840 cancelPollDuration();
1841
1842 ++mScanSourcesGeneration;
1843 mScanSourcesPending = false;
1844
Wei Jia1008e1c2014-09-09 14:49:08 -07001845 ++mAudioDecoderGeneration;
1846 ++mVideoDecoderGeneration;
1847
Lajos Molnar09524832014-07-17 14:29:51 -07001848 if (mRendererLooper != NULL) {
1849 if (mRenderer != NULL) {
1850 mRendererLooper->unregisterHandler(mRenderer->id());
1851 }
1852 mRendererLooper->stop();
1853 mRendererLooper.clear();
1854 }
Andreas Hubera1f8ab02012-11-30 10:53:22 -08001855 mRenderer.clear();
1856
1857 if (mSource != NULL) {
1858 mSource->stop();
Andreas Huberb5f25f02013-02-05 10:14:26 -08001859
Andreas Hubera1f8ab02012-11-30 10:53:22 -08001860 mSource.clear();
1861 }
1862
1863 if (mDriver != NULL) {
1864 sp<NuPlayerDriver> driver = mDriver.promote();
1865 if (driver != NULL) {
1866 driver->notifyResetComplete();
1867 }
1868 }
Andreas Huber57a339c2012-12-03 11:18:00 -08001869
1870 mStarted = false;
Andreas Hubera1f8ab02012-11-30 10:53:22 -08001871}
1872
1873void NuPlayer::performScanSources() {
1874 ALOGV("performScanSources");
1875
Andreas Huber57a339c2012-12-03 11:18:00 -08001876 if (!mStarted) {
1877 return;
1878 }
1879
Andreas Hubera1f8ab02012-11-30 10:53:22 -08001880 if (mAudioDecoder == NULL || mVideoDecoder == NULL) {
1881 postScanSources();
1882 }
1883}
1884
Andreas Huber57a339c2012-12-03 11:18:00 -08001885void NuPlayer::performSetSurface(const sp<NativeWindowWrapper> &wrapper) {
1886 ALOGV("performSetSurface");
1887
1888 mNativeWindow = wrapper;
1889
1890 // XXX - ignore error from setVideoScalingMode for now
1891 setVideoScalingMode(mVideoScalingMode);
Chong Zhang13d6faa2014-08-22 15:35:28 -07001892
1893 if (mDriver != NULL) {
1894 sp<NuPlayerDriver> driver = mDriver.promote();
1895 if (driver != NULL) {
1896 driver->notifySetSurfaceComplete();
1897 }
1898 }
Andreas Huber57a339c2012-12-03 11:18:00 -08001899}
1900
Andreas Huber9575c962013-02-05 13:59:56 -08001901void NuPlayer::onSourceNotify(const sp<AMessage> &msg) {
1902 int32_t what;
1903 CHECK(msg->findInt32("what", &what));
1904
1905 switch (what) {
1906 case Source::kWhatPrepared:
1907 {
Andreas Huberb5f28d42013-04-25 15:11:19 -07001908 if (mSource == NULL) {
1909 // This is a stale notification from a source that was
1910 // asynchronously preparing when the client called reset().
1911 // We handled the reset, the source is gone.
1912 break;
1913 }
1914
Andreas Huberec0c5972013-02-05 14:47:13 -08001915 int32_t err;
1916 CHECK(msg->findInt32("err", &err));
1917
Andreas Huber9575c962013-02-05 13:59:56 -08001918 sp<NuPlayerDriver> driver = mDriver.promote();
1919 if (driver != NULL) {
Marco Nelissendd114d12014-05-28 15:23:14 -07001920 // notify duration first, so that it's definitely set when
1921 // the app received the "prepare complete" callback.
1922 int64_t durationUs;
1923 if (mSource->getDuration(&durationUs) == OK) {
1924 driver->notifyDuration(durationUs);
1925 }
Andreas Huberec0c5972013-02-05 14:47:13 -08001926 driver->notifyPrepareCompleted(err);
Andreas Huber9575c962013-02-05 13:59:56 -08001927 }
Andreas Huber99759402013-04-01 14:28:31 -07001928
Andreas Huber9575c962013-02-05 13:59:56 -08001929 break;
1930 }
1931
1932 case Source::kWhatFlagsChanged:
1933 {
1934 uint32_t flags;
1935 CHECK(msg->findInt32("flags", (int32_t *)&flags));
1936
Chong Zhang4b7069d2013-09-11 12:52:43 -07001937 sp<NuPlayerDriver> driver = mDriver.promote();
1938 if (driver != NULL) {
1939 driver->notifyFlagsChanged(flags);
1940 }
1941
Andreas Huber9575c962013-02-05 13:59:56 -08001942 if ((mSourceFlags & Source::FLAG_DYNAMIC_DURATION)
1943 && (!(flags & Source::FLAG_DYNAMIC_DURATION))) {
1944 cancelPollDuration();
1945 } else if (!(mSourceFlags & Source::FLAG_DYNAMIC_DURATION)
1946 && (flags & Source::FLAG_DYNAMIC_DURATION)
1947 && (mAudioDecoder != NULL || mVideoDecoder != NULL)) {
1948 schedulePollDuration();
1949 }
1950
1951 mSourceFlags = flags;
1952 break;
1953 }
1954
1955 case Source::kWhatVideoSizeChanged:
1956 {
Chong Zhangced1c2f2014-08-08 15:22:35 -07001957 sp<AMessage> format;
1958 CHECK(msg->findMessage("format", &format));
Andreas Huber9575c962013-02-05 13:59:56 -08001959
Chong Zhangced1c2f2014-08-08 15:22:35 -07001960 updateVideoSize(format);
Andreas Huber9575c962013-02-05 13:59:56 -08001961 break;
1962 }
1963
Chong Zhang2a3cc9a2014-08-21 17:48:26 -07001964 case Source::kWhatBufferingUpdate:
1965 {
1966 int32_t percentage;
1967 CHECK(msg->findInt32("percentage", &percentage));
1968
1969 notifyListener(MEDIA_BUFFERING_UPDATE, percentage, 0);
1970 break;
1971 }
1972
Roger Jönssonb50e83e2013-01-21 16:26:41 +01001973 case Source::kWhatBufferingStart:
1974 {
1975 notifyListener(MEDIA_INFO, MEDIA_INFO_BUFFERING_START, 0);
1976 break;
1977 }
1978
1979 case Source::kWhatBufferingEnd:
1980 {
1981 notifyListener(MEDIA_INFO, MEDIA_INFO_BUFFERING_END, 0);
1982 break;
1983 }
1984
Chong Zhangdcb89b32013-08-06 09:44:47 -07001985 case Source::kWhatSubtitleData:
1986 {
1987 sp<ABuffer> buffer;
1988 CHECK(msg->findBuffer("buffer", &buffer));
1989
Chong Zhang404fced2014-06-11 14:45:31 -07001990 sendSubtitleData(buffer, 0 /* baseIndex */);
Chong Zhangdcb89b32013-08-06 09:44:47 -07001991 break;
1992 }
1993
Robert Shihd3b0bbb2014-07-23 15:00:25 -07001994 case Source::kWhatTimedTextData:
1995 {
1996 int32_t generation;
1997 if (msg->findInt32("generation", &generation)
1998 && generation != mTimedTextGeneration) {
1999 break;
2000 }
2001
2002 sp<ABuffer> buffer;
2003 CHECK(msg->findBuffer("buffer", &buffer));
2004
2005 sp<NuPlayerDriver> driver = mDriver.promote();
2006 if (driver == NULL) {
2007 break;
2008 }
2009
2010 int posMs;
2011 int64_t timeUs, posUs;
2012 driver->getCurrentPosition(&posMs);
2013 posUs = posMs * 1000;
2014 CHECK(buffer->meta()->findInt64("timeUs", &timeUs));
2015
2016 if (posUs < timeUs) {
2017 if (!msg->findInt32("generation", &generation)) {
2018 msg->setInt32("generation", mTimedTextGeneration);
2019 }
2020 msg->post(timeUs - posUs);
2021 } else {
2022 sendTimedTextData(buffer);
2023 }
2024 break;
2025 }
2026
Andreas Huber14f76722013-01-15 09:04:18 -08002027 case Source::kWhatQueueDecoderShutdown:
2028 {
2029 int32_t audio, video;
2030 CHECK(msg->findInt32("audio", &audio));
2031 CHECK(msg->findInt32("video", &video));
2032
2033 sp<AMessage> reply;
2034 CHECK(msg->findMessage("reply", &reply));
2035
2036 queueDecoderShutdown(audio, video, reply);
2037 break;
2038 }
2039
Ronghua Wu80276872014-08-28 15:50:29 -07002040 case Source::kWhatDrmNoLicense:
2041 {
2042 notifyListener(MEDIA_ERROR, MEDIA_ERROR_UNKNOWN, ERROR_DRM_NO_LICENSE);
2043 break;
2044 }
2045
Andreas Huber9575c962013-02-05 13:59:56 -08002046 default:
2047 TRESPASS();
2048 }
2049}
2050
Chong Zhanga7fa1d92014-06-11 14:49:23 -07002051void NuPlayer::onClosedCaptionNotify(const sp<AMessage> &msg) {
2052 int32_t what;
2053 CHECK(msg->findInt32("what", &what));
2054
2055 switch (what) {
2056 case NuPlayer::CCDecoder::kWhatClosedCaptionData:
2057 {
2058 sp<ABuffer> buffer;
2059 CHECK(msg->findBuffer("buffer", &buffer));
2060
2061 size_t inbandTracks = 0;
2062 if (mSource != NULL) {
2063 inbandTracks = mSource->getTrackCount();
2064 }
2065
2066 sendSubtitleData(buffer, inbandTracks);
2067 break;
2068 }
2069
2070 case NuPlayer::CCDecoder::kWhatTrackAdded:
2071 {
2072 notifyListener(MEDIA_INFO, MEDIA_INFO_METADATA_UPDATE, 0);
2073
2074 break;
2075 }
2076
2077 default:
2078 TRESPASS();
2079 }
2080
2081
2082}
2083
Chong Zhang404fced2014-06-11 14:45:31 -07002084void NuPlayer::sendSubtitleData(const sp<ABuffer> &buffer, int32_t baseIndex) {
2085 int32_t trackIndex;
2086 int64_t timeUs, durationUs;
2087 CHECK(buffer->meta()->findInt32("trackIndex", &trackIndex));
2088 CHECK(buffer->meta()->findInt64("timeUs", &timeUs));
2089 CHECK(buffer->meta()->findInt64("durationUs", &durationUs));
2090
2091 Parcel in;
2092 in.writeInt32(trackIndex + baseIndex);
2093 in.writeInt64(timeUs);
2094 in.writeInt64(durationUs);
2095 in.writeInt32(buffer->size());
2096 in.writeInt32(buffer->size());
2097 in.write(buffer->data(), buffer->size());
2098
2099 notifyListener(MEDIA_SUBTITLE_DATA, 0, 0, &in);
2100}
Robert Shihd3b0bbb2014-07-23 15:00:25 -07002101
2102void NuPlayer::sendTimedTextData(const sp<ABuffer> &buffer) {
2103 const void *data;
2104 size_t size = 0;
2105 int64_t timeUs;
2106 int32_t flag = TextDescriptions::LOCAL_DESCRIPTIONS;
2107
2108 AString mime;
2109 CHECK(buffer->meta()->findString("mime", &mime));
2110 CHECK(strcasecmp(mime.c_str(), MEDIA_MIMETYPE_TEXT_3GPP) == 0);
2111
2112 data = buffer->data();
2113 size = buffer->size();
2114
2115 Parcel parcel;
2116 if (size > 0) {
2117 CHECK(buffer->meta()->findInt64("timeUs", &timeUs));
2118 flag |= TextDescriptions::IN_BAND_TEXT_3GPP;
2119 TextDescriptions::getParcelOfDescriptions(
2120 (const uint8_t *)data, size, flag, timeUs / 1000, &parcel);
2121 }
2122
2123 if ((parcel.dataSize() > 0)) {
2124 notifyListener(MEDIA_TIMED_TEXT, 0, 0, &parcel);
2125 } else { // send an empty timed text
2126 notifyListener(MEDIA_TIMED_TEXT, 0, 0);
2127 }
2128}
Andreas Huberb5f25f02013-02-05 10:14:26 -08002129////////////////////////////////////////////////////////////////////////////////
2130
Chong Zhangced1c2f2014-08-08 15:22:35 -07002131sp<AMessage> NuPlayer::Source::getFormat(bool audio) {
2132 sp<MetaData> meta = getFormatMeta(audio);
2133
2134 if (meta == NULL) {
2135 return NULL;
2136 }
2137
2138 sp<AMessage> msg = new AMessage;
2139
2140 if(convertMetaDataToMessage(meta, &msg) == OK) {
2141 return msg;
2142 }
2143 return NULL;
2144}
2145
Andreas Huber9575c962013-02-05 13:59:56 -08002146void NuPlayer::Source::notifyFlagsChanged(uint32_t flags) {
2147 sp<AMessage> notify = dupNotify();
2148 notify->setInt32("what", kWhatFlagsChanged);
2149 notify->setInt32("flags", flags);
2150 notify->post();
2151}
2152
Chong Zhangced1c2f2014-08-08 15:22:35 -07002153void NuPlayer::Source::notifyVideoSizeChanged(const sp<AMessage> &format) {
Andreas Huber9575c962013-02-05 13:59:56 -08002154 sp<AMessage> notify = dupNotify();
2155 notify->setInt32("what", kWhatVideoSizeChanged);
Chong Zhangced1c2f2014-08-08 15:22:35 -07002156 notify->setMessage("format", format);
Andreas Huber9575c962013-02-05 13:59:56 -08002157 notify->post();
2158}
2159
Andreas Huberec0c5972013-02-05 14:47:13 -08002160void NuPlayer::Source::notifyPrepared(status_t err) {
Andreas Huber9575c962013-02-05 13:59:56 -08002161 sp<AMessage> notify = dupNotify();
2162 notify->setInt32("what", kWhatPrepared);
Andreas Huberec0c5972013-02-05 14:47:13 -08002163 notify->setInt32("err", err);
Andreas Huber9575c962013-02-05 13:59:56 -08002164 notify->post();
2165}
2166
Andreas Huber84333e02014-02-07 15:36:10 -08002167void NuPlayer::Source::onMessageReceived(const sp<AMessage> & /* msg */) {
Andreas Huberb5f25f02013-02-05 10:14:26 -08002168 TRESPASS();
2169}
2170
Andreas Huberf9334412010-12-15 15:17:42 -08002171} // namespace android