blob: 59766c8462492a039806abfcb0020ec9447d735c [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() {
309 (new AMessage(kWhatReset, id()))->post();
310}
311
Andreas Huber43c3e6c2011-01-05 12:17:08 -0800312void NuPlayer::seekToAsync(int64_t seekTimeUs) {
313 sp<AMessage> msg = new AMessage(kWhatSeek, id());
314 msg->setInt64("seekTimeUs", seekTimeUs);
315 msg->post();
316}
317
Andreas Huber53df1a42010-12-22 10:03:04 -0800318// static
Andreas Huber1aef2112011-01-04 14:01:29 -0800319bool NuPlayer::IsFlushingState(FlushStatus state, bool *needShutdown) {
Andreas Huber53df1a42010-12-22 10:03:04 -0800320 switch (state) {
321 case FLUSHING_DECODER:
Andreas Huber1aef2112011-01-04 14:01:29 -0800322 if (needShutdown != NULL) {
323 *needShutdown = false;
Andreas Huber53df1a42010-12-22 10:03:04 -0800324 }
325 return true;
326
Andreas Huber1aef2112011-01-04 14:01:29 -0800327 case FLUSHING_DECODER_SHUTDOWN:
328 if (needShutdown != NULL) {
329 *needShutdown = true;
Andreas Huber53df1a42010-12-22 10:03:04 -0800330 }
331 return true;
332
333 default:
334 return false;
335 }
336}
337
Chong Zhang404fced2014-06-11 14:45:31 -0700338void NuPlayer::writeTrackInfo(
339 Parcel* reply, const sp<AMessage> format) const {
340 int32_t trackType;
341 CHECK(format->findInt32("type", &trackType));
342
343 AString lang;
344 CHECK(format->findString("language", &lang));
345
346 reply->writeInt32(2); // write something non-zero
347 reply->writeInt32(trackType);
348 reply->writeString16(String16(lang.c_str()));
349
350 if (trackType == MEDIA_TRACK_TYPE_SUBTITLE) {
351 AString mime;
352 CHECK(format->findString("mime", &mime));
353
354 int32_t isAuto, isDefault, isForced;
355 CHECK(format->findInt32("auto", &isAuto));
356 CHECK(format->findInt32("default", &isDefault));
357 CHECK(format->findInt32("forced", &isForced));
358
359 reply->writeString16(String16(mime.c_str()));
360 reply->writeInt32(isAuto);
361 reply->writeInt32(isDefault);
362 reply->writeInt32(isForced);
363 }
364}
365
Andreas Huberf9334412010-12-15 15:17:42 -0800366void NuPlayer::onMessageReceived(const sp<AMessage> &msg) {
367 switch (msg->what()) {
368 case kWhatSetDataSource:
369 {
Steve Block3856b092011-10-20 11:56:00 +0100370 ALOGV("kWhatSetDataSource");
Andreas Huberf9334412010-12-15 15:17:42 -0800371
372 CHECK(mSource == NULL);
373
Chong Zhang3de157d2014-08-05 20:54:44 -0700374 status_t err = OK;
Andreas Huber5bc087c2010-12-23 10:27:40 -0800375 sp<RefBase> obj;
376 CHECK(msg->findObject("source", &obj));
Chong Zhang3de157d2014-08-05 20:54:44 -0700377 if (obj != NULL) {
378 mSource = static_cast<Source *>(obj.get());
Chong Zhang3de157d2014-08-05 20:54:44 -0700379 } else {
380 err = UNKNOWN_ERROR;
381 }
Andreas Huber9575c962013-02-05 13:59:56 -0800382
383 CHECK(mDriver != NULL);
384 sp<NuPlayerDriver> driver = mDriver.promote();
385 if (driver != NULL) {
Chong Zhang3de157d2014-08-05 20:54:44 -0700386 driver->notifySetDataSourceCompleted(err);
Andreas Huber9575c962013-02-05 13:59:56 -0800387 }
388 break;
389 }
390
391 case kWhatPrepare:
392 {
393 mSource->prepareAsync();
Andreas Huberf9334412010-12-15 15:17:42 -0800394 break;
395 }
396
Chong Zhangdcb89b32013-08-06 09:44:47 -0700397 case kWhatGetTrackInfo:
398 {
399 uint32_t replyID;
400 CHECK(msg->senderAwaitsResponse(&replyID));
401
Chong Zhang404fced2014-06-11 14:45:31 -0700402 Parcel* reply;
403 CHECK(msg->findPointer("reply", (void**)&reply));
404
405 size_t inbandTracks = 0;
Chong Zhangdcb89b32013-08-06 09:44:47 -0700406 if (mSource != NULL) {
Chong Zhang404fced2014-06-11 14:45:31 -0700407 inbandTracks = mSource->getTrackCount();
408 }
409
Chong Zhanga7fa1d92014-06-11 14:49:23 -0700410 size_t ccTracks = 0;
411 if (mCCDecoder != NULL) {
412 ccTracks = mCCDecoder->getTrackCount();
413 }
414
Chong Zhang404fced2014-06-11 14:45:31 -0700415 // total track count
Chong Zhanga7fa1d92014-06-11 14:49:23 -0700416 reply->writeInt32(inbandTracks + ccTracks);
Chong Zhang404fced2014-06-11 14:45:31 -0700417
418 // write inband tracks
419 for (size_t i = 0; i < inbandTracks; ++i) {
420 writeTrackInfo(reply, mSource->getTrackInfo(i));
Chong Zhangdcb89b32013-08-06 09:44:47 -0700421 }
422
Chong Zhanga7fa1d92014-06-11 14:49:23 -0700423 // write CC track
424 for (size_t i = 0; i < ccTracks; ++i) {
425 writeTrackInfo(reply, mCCDecoder->getTrackInfo(i));
426 }
427
Chong Zhangdcb89b32013-08-06 09:44:47 -0700428 sp<AMessage> response = new AMessage;
Chong Zhangdcb89b32013-08-06 09:44:47 -0700429 response->postReply(replyID);
430 break;
431 }
432
Robert Shih7c4f0d72014-07-09 18:53:31 -0700433 case kWhatGetSelectedTrack:
434 {
435 status_t err = INVALID_OPERATION;
436 if (mSource != NULL) {
437 err = OK;
438
439 int32_t type32;
440 CHECK(msg->findInt32("type", (int32_t*)&type32));
441 media_track_type type = (media_track_type)type32;
442 ssize_t selectedTrack = mSource->getSelectedTrack(type);
443
444 Parcel* reply;
445 CHECK(msg->findPointer("reply", (void**)&reply));
446 reply->writeInt32(selectedTrack);
447 }
448
449 sp<AMessage> response = new AMessage;
450 response->setInt32("err", err);
451
452 uint32_t replyID;
453 CHECK(msg->senderAwaitsResponse(&replyID));
454 response->postReply(replyID);
455 break;
456 }
457
Chong Zhangdcb89b32013-08-06 09:44:47 -0700458 case kWhatSelectTrack:
459 {
460 uint32_t replyID;
461 CHECK(msg->senderAwaitsResponse(&replyID));
462
Chong Zhang404fced2014-06-11 14:45:31 -0700463 size_t trackIndex;
464 int32_t select;
465 CHECK(msg->findSize("trackIndex", &trackIndex));
466 CHECK(msg->findInt32("select", &select));
467
Chong Zhangdcb89b32013-08-06 09:44:47 -0700468 status_t err = INVALID_OPERATION;
Chong Zhang404fced2014-06-11 14:45:31 -0700469
470 size_t inbandTracks = 0;
Chong Zhangdcb89b32013-08-06 09:44:47 -0700471 if (mSource != NULL) {
Chong Zhang404fced2014-06-11 14:45:31 -0700472 inbandTracks = mSource->getTrackCount();
473 }
Chong Zhanga7fa1d92014-06-11 14:49:23 -0700474 size_t ccTracks = 0;
475 if (mCCDecoder != NULL) {
476 ccTracks = mCCDecoder->getTrackCount();
477 }
Chong Zhang404fced2014-06-11 14:45:31 -0700478
479 if (trackIndex < inbandTracks) {
Chong Zhangdcb89b32013-08-06 09:44:47 -0700480 err = mSource->selectTrack(trackIndex, select);
Robert Shihd3b0bbb2014-07-23 15:00:25 -0700481
482 if (!select && err == OK) {
483 int32_t type;
484 sp<AMessage> info = mSource->getTrackInfo(trackIndex);
485 if (info != NULL
486 && info->findInt32("type", &type)
487 && type == MEDIA_TRACK_TYPE_TIMEDTEXT) {
488 ++mTimedTextGeneration;
489 }
490 }
Chong Zhanga7fa1d92014-06-11 14:49:23 -0700491 } else {
492 trackIndex -= inbandTracks;
493
494 if (trackIndex < ccTracks) {
495 err = mCCDecoder->selectTrack(trackIndex, select);
496 }
Chong Zhangdcb89b32013-08-06 09:44:47 -0700497 }
498
499 sp<AMessage> response = new AMessage;
500 response->setInt32("err", err);
501
502 response->postReply(replyID);
503 break;
504 }
505
Andreas Huberb7c8e912012-11-27 15:02:53 -0800506 case kWhatPollDuration:
507 {
508 int32_t generation;
509 CHECK(msg->findInt32("generation", &generation));
510
511 if (generation != mPollDurationGeneration) {
512 // stale
513 break;
514 }
515
516 int64_t durationUs;
517 if (mDriver != NULL && mSource->getDuration(&durationUs) == OK) {
518 sp<NuPlayerDriver> driver = mDriver.promote();
519 if (driver != NULL) {
520 driver->notifyDuration(durationUs);
521 }
522 }
523
524 msg->post(1000000ll); // poll again in a second.
525 break;
526 }
527
Glenn Kasten11731182011-02-08 17:26:17 -0800528 case kWhatSetVideoNativeWindow:
Andreas Huberf9334412010-12-15 15:17:42 -0800529 {
Steve Block3856b092011-10-20 11:56:00 +0100530 ALOGV("kWhatSetVideoNativeWindow");
Andreas Huberf9334412010-12-15 15:17:42 -0800531
Andreas Huber57a339c2012-12-03 11:18:00 -0800532 mDeferredActions.push_back(
Andreas Huber14f76722013-01-15 09:04:18 -0800533 new ShutdownDecoderAction(
534 false /* audio */, true /* video */));
Andreas Huber57a339c2012-12-03 11:18:00 -0800535
Andreas Huberf9334412010-12-15 15:17:42 -0800536 sp<RefBase> obj;
Glenn Kasten11731182011-02-08 17:26:17 -0800537 CHECK(msg->findObject("native-window", &obj));
Andreas Huberf9334412010-12-15 15:17:42 -0800538
Andreas Huber57a339c2012-12-03 11:18:00 -0800539 mDeferredActions.push_back(
540 new SetSurfaceAction(
541 static_cast<NativeWindowWrapper *>(obj.get())));
James Dong0d268a32012-08-31 12:18:27 -0700542
Andreas Huber57a339c2012-12-03 11:18:00 -0800543 if (obj != NULL) {
Andy Hung73535852014-09-05 11:42:58 -0700544 if (mStarted && mVideoDecoder != NULL) {
545 // Issue a seek to refresh the video screen only if started otherwise
546 // the extractor may not yet be started and will assert.
547 // If the video decoder is not set (perhaps audio only in this case)
548 // do not perform a seek as it is not needed.
549 mDeferredActions.push_back(new SeekAction(mCurrentPositionUs));
550 }
Wei Jiaac428aa2014-09-02 19:01:34 -0700551
Andreas Huber57a339c2012-12-03 11:18:00 -0800552 // If there is a new surface texture, instantiate decoders
553 // again if possible.
554 mDeferredActions.push_back(
555 new SimpleAction(&NuPlayer::performScanSources));
556 }
557
558 processDeferredActions();
Andreas Huberf9334412010-12-15 15:17:42 -0800559 break;
560 }
561
562 case kWhatSetAudioSink:
563 {
Steve Block3856b092011-10-20 11:56:00 +0100564 ALOGV("kWhatSetAudioSink");
Andreas Huberf9334412010-12-15 15:17:42 -0800565
566 sp<RefBase> obj;
567 CHECK(msg->findObject("sink", &obj));
568
569 mAudioSink = static_cast<MediaPlayerBase::AudioSink *>(obj.get());
570 break;
571 }
572
573 case kWhatStart:
574 {
Steve Block3856b092011-10-20 11:56:00 +0100575 ALOGV("kWhatStart");
Andreas Huber43c3e6c2011-01-05 12:17:08 -0800576
Andreas Huber3fe62152011-09-16 15:09:22 -0700577 mVideoIsAVC = false;
Wei Jiabc2fb722014-07-08 16:37:57 -0700578 mOffloadAudio = false;
Andreas Huber1aef2112011-01-04 14:01:29 -0800579 mAudioEOS = false;
580 mVideoEOS = false;
Andreas Huber32f3cef2011-03-02 15:34:46 -0800581 mSkipRenderingAudioUntilMediaTimeUs = -1;
582 mSkipRenderingVideoUntilMediaTimeUs = -1;
Andreas Huber3fe62152011-09-16 15:09:22 -0700583 mVideoLateByUs = 0;
584 mNumFramesTotal = 0;
585 mNumFramesDropped = 0;
Andreas Huber57a339c2012-12-03 11:18:00 -0800586 mStarted = true;
Andreas Huber1aef2112011-01-04 14:01:29 -0800587
Lajos Molnar09524832014-07-17 14:29:51 -0700588 /* instantiate decoders now for secure playback */
589 if (mSourceFlags & Source::FLAG_SECURE) {
590 if (mNativeWindow != NULL) {
591 instantiateDecoder(false, &mVideoDecoder);
592 }
593
594 if (mAudioSink != NULL) {
595 instantiateDecoder(true, &mAudioDecoder);
596 }
597 }
598
Andreas Huber5bc087c2010-12-23 10:27:40 -0800599 mSource->start();
Andreas Huberf9334412010-12-15 15:17:42 -0800600
Andreas Huberd5e56232013-03-12 11:01:43 -0700601 uint32_t flags = 0;
602
603 if (mSource->isRealTime()) {
604 flags |= Renderer::FLAG_REAL_TIME;
605 }
606
Wei Jiabc2fb722014-07-08 16:37:57 -0700607 sp<MetaData> audioMeta = mSource->getFormatMeta(true /* audio */);
608 audio_stream_type_t streamType = AUDIO_STREAM_MUSIC;
609 if (mAudioSink != NULL) {
610 streamType = mAudioSink->getAudioStreamType();
611 }
612
613 sp<AMessage> videoFormat = mSource->getFormat(false /* audio */);
614
615 mOffloadAudio =
616 canOffloadStream(audioMeta, (videoFormat != NULL),
617 true /* is_streaming */, streamType);
618 if (mOffloadAudio) {
619 flags |= Renderer::FLAG_OFFLOAD_AUDIO;
620 }
621
Andreas Huberf9334412010-12-15 15:17:42 -0800622 mRenderer = new Renderer(
623 mAudioSink,
Andreas Huberd5e56232013-03-12 11:01:43 -0700624 new AMessage(kWhatRendererNotify, id()),
625 flags);
Andreas Huberf9334412010-12-15 15:17:42 -0800626
Lajos Molnar09524832014-07-17 14:29:51 -0700627 mRendererLooper = new ALooper;
628 mRendererLooper->setName("NuPlayerRenderer");
629 mRendererLooper->start(false, false, ANDROID_PRIORITY_AUDIO);
630 mRendererLooper->registerHandler(mRenderer);
Andreas Huberf9334412010-12-15 15:17:42 -0800631
Andreas Huber1aef2112011-01-04 14:01:29 -0800632 postScanSources();
Andreas Huberf9334412010-12-15 15:17:42 -0800633 break;
634 }
635
636 case kWhatScanSources:
637 {
Andreas Huber1aef2112011-01-04 14:01:29 -0800638 int32_t generation;
639 CHECK(msg->findInt32("generation", &generation));
640 if (generation != mScanSourcesGeneration) {
641 // Drop obsolete msg.
642 break;
643 }
644
Andreas Huber5bc087c2010-12-23 10:27:40 -0800645 mScanSourcesPending = false;
646
Steve Block3856b092011-10-20 11:56:00 +0100647 ALOGV("scanning sources haveAudio=%d, haveVideo=%d",
Andreas Huber43c3e6c2011-01-05 12:17:08 -0800648 mAudioDecoder != NULL, mVideoDecoder != NULL);
649
Andreas Huberb7c8e912012-11-27 15:02:53 -0800650 bool mHadAnySourcesBefore =
651 (mAudioDecoder != NULL) || (mVideoDecoder != NULL);
652
Andy Hung282a7e32014-08-14 15:56:34 -0700653 // initialize video before audio because successful initialization of
654 // video may change deep buffer mode of audio.
Haynes Mathew George5d246ef2012-07-09 10:36:57 -0700655 if (mNativeWindow != NULL) {
656 instantiateDecoder(false, &mVideoDecoder);
657 }
Andreas Huberf9334412010-12-15 15:17:42 -0800658
659 if (mAudioSink != NULL) {
Andy Hung282a7e32014-08-14 15:56:34 -0700660 if (mOffloadAudio) {
661 // open audio sink early under offload mode.
662 sp<AMessage> format = mSource->getFormat(true /*audio*/);
663 openAudioSink(format, true /*offloadOnly*/);
664 }
Andreas Huber5bc087c2010-12-23 10:27:40 -0800665 instantiateDecoder(true, &mAudioDecoder);
Andreas Huberf9334412010-12-15 15:17:42 -0800666 }
667
Andreas Huberb7c8e912012-11-27 15:02:53 -0800668 if (!mHadAnySourcesBefore
669 && (mAudioDecoder != NULL || mVideoDecoder != NULL)) {
670 // This is the first time we've found anything playable.
671
Andreas Huber9575c962013-02-05 13:59:56 -0800672 if (mSourceFlags & Source::FLAG_DYNAMIC_DURATION) {
Andreas Huberb7c8e912012-11-27 15:02:53 -0800673 schedulePollDuration();
674 }
675 }
676
Andreas Hubereac68ba2011-09-27 12:12:25 -0700677 status_t err;
678 if ((err = mSource->feedMoreTSData()) != OK) {
Andreas Huber1aef2112011-01-04 14:01:29 -0800679 if (mAudioDecoder == NULL && mVideoDecoder == NULL) {
680 // We're not currently decoding anything (no audio or
681 // video tracks found) and we just ran out of input data.
Andreas Hubereac68ba2011-09-27 12:12:25 -0700682
683 if (err == ERROR_END_OF_STREAM) {
684 notifyListener(MEDIA_PLAYBACK_COMPLETE, 0, 0);
685 } else {
686 notifyListener(MEDIA_ERROR, MEDIA_ERROR_UNKNOWN, err);
687 }
Andreas Huber1aef2112011-01-04 14:01:29 -0800688 }
Andreas Huberf9334412010-12-15 15:17:42 -0800689 break;
690 }
691
Andreas Huberfbe9d812012-08-31 14:05:27 -0700692 if ((mAudioDecoder == NULL && mAudioSink != NULL)
693 || (mVideoDecoder == NULL && mNativeWindow != NULL)) {
Andreas Huberf9334412010-12-15 15:17:42 -0800694 msg->post(100000ll);
Andreas Huber5bc087c2010-12-23 10:27:40 -0800695 mScanSourcesPending = true;
Andreas Huberf9334412010-12-15 15:17:42 -0800696 }
697 break;
698 }
699
700 case kWhatVideoNotify:
701 case kWhatAudioNotify:
702 {
703 bool audio = msg->what() == kWhatAudioNotify;
704
Wei Jia88703c32014-08-06 11:24:07 -0700705 int32_t currentDecoderGeneration =
706 (audio? mAudioDecoderGeneration : mVideoDecoderGeneration);
707 int32_t requesterGeneration = currentDecoderGeneration - 1;
708 CHECK(msg->findInt32("generation", &requesterGeneration));
709
710 if (requesterGeneration != currentDecoderGeneration) {
711 ALOGV("got message from old %s decoder, generation(%d:%d)",
712 audio ? "audio" : "video", requesterGeneration,
713 currentDecoderGeneration);
714 sp<AMessage> reply;
715 if (!(msg->findMessage("reply", &reply))) {
716 return;
717 }
718
719 reply->setInt32("err", INFO_DISCONTINUITY);
720 reply->post();
721 return;
722 }
723
Andreas Huberf9334412010-12-15 15:17:42 -0800724 int32_t what;
Lajos Molnar1cd13982014-01-17 15:12:51 -0800725 CHECK(msg->findInt32("what", &what));
Andreas Huberf9334412010-12-15 15:17:42 -0800726
Lajos Molnar1cd13982014-01-17 15:12:51 -0800727 if (what == Decoder::kWhatFillThisBuffer) {
Andreas Huberf9334412010-12-15 15:17:42 -0800728 status_t err = feedDecoderInputData(
Lajos Molnar1cd13982014-01-17 15:12:51 -0800729 audio, msg);
Andreas Huberf9334412010-12-15 15:17:42 -0800730
Andreas Huber5bc087c2010-12-23 10:27:40 -0800731 if (err == -EWOULDBLOCK) {
Andreas Hubereac68ba2011-09-27 12:12:25 -0700732 if (mSource->feedMoreTSData() == OK) {
Andreas Huber1183a4a2011-11-03 11:00:21 -0700733 msg->post(10000ll);
Andreas Huber5bc087c2010-12-23 10:27:40 -0800734 }
Andreas Huberf9334412010-12-15 15:17:42 -0800735 }
Lajos Molnar1cd13982014-01-17 15:12:51 -0800736 } else if (what == Decoder::kWhatEOS) {
Andreas Huberdc9bacd2011-09-26 10:53:29 -0700737 int32_t err;
Lajos Molnar1cd13982014-01-17 15:12:51 -0800738 CHECK(msg->findInt32("err", &err));
Andreas Huberdc9bacd2011-09-26 10:53:29 -0700739
740 if (err == ERROR_END_OF_STREAM) {
Steve Block3856b092011-10-20 11:56:00 +0100741 ALOGV("got %s decoder EOS", audio ? "audio" : "video");
Andreas Huberdc9bacd2011-09-26 10:53:29 -0700742 } else {
Steve Block3856b092011-10-20 11:56:00 +0100743 ALOGV("got %s decoder EOS w/ error %d",
Andreas Huberdc9bacd2011-09-26 10:53:29 -0700744 audio ? "audio" : "video",
745 err);
746 }
747
748 mRenderer->queueEOS(audio, err);
Lajos Molnar1cd13982014-01-17 15:12:51 -0800749 } else if (what == Decoder::kWhatFlushCompleted) {
Andreas Huber1aef2112011-01-04 14:01:29 -0800750 bool needShutdown;
Andreas Huber53df1a42010-12-22 10:03:04 -0800751
Andreas Huberf9334412010-12-15 15:17:42 -0800752 if (audio) {
Andreas Huber1aef2112011-01-04 14:01:29 -0800753 CHECK(IsFlushingState(mFlushingAudio, &needShutdown));
Andreas Huberf9334412010-12-15 15:17:42 -0800754 mFlushingAudio = FLUSHED;
755 } else {
Andreas Huber1aef2112011-01-04 14:01:29 -0800756 CHECK(IsFlushingState(mFlushingVideo, &needShutdown));
Andreas Huberf9334412010-12-15 15:17:42 -0800757 mFlushingVideo = FLUSHED;
Andreas Huber3fe62152011-09-16 15:09:22 -0700758
759 mVideoLateByUs = 0;
Andreas Huberf9334412010-12-15 15:17:42 -0800760 }
761
Steve Block3856b092011-10-20 11:56:00 +0100762 ALOGV("decoder %s flush completed", audio ? "audio" : "video");
Andreas Huberf9334412010-12-15 15:17:42 -0800763
Andreas Huber1aef2112011-01-04 14:01:29 -0800764 if (needShutdown) {
Steve Block3856b092011-10-20 11:56:00 +0100765 ALOGV("initiating %s decoder shutdown",
Andreas Huber53df1a42010-12-22 10:03:04 -0800766 audio ? "audio" : "video");
Andreas Huberf9334412010-12-15 15:17:42 -0800767
Lajos Molnar87603c02014-08-20 19:25:30 -0700768 getDecoder(audio)->initiateShutdown();
Andreas Huberf9334412010-12-15 15:17:42 -0800769
Andreas Huber53df1a42010-12-22 10:03:04 -0800770 if (audio) {
771 mFlushingAudio = SHUTTING_DOWN_DECODER;
772 } else {
773 mFlushingVideo = SHUTTING_DOWN_DECODER;
774 }
Andreas Huberf9334412010-12-15 15:17:42 -0800775 }
Andreas Huber3831a062010-12-21 10:22:33 -0800776
777 finishFlushIfPossible();
Lajos Molnar1cd13982014-01-17 15:12:51 -0800778 } else if (what == Decoder::kWhatOutputFormatChanged) {
779 sp<AMessage> format;
780 CHECK(msg->findMessage("format", &format));
781
Andreas Huber31e25082011-01-10 10:38:31 -0800782 if (audio) {
Andy Hung282a7e32014-08-14 15:56:34 -0700783 openAudioSink(format, false /*offloadOnly*/);
Andreas Huber31e25082011-01-10 10:38:31 -0800784 } else {
785 // video
Chong Zhangced1c2f2014-08-08 15:22:35 -0700786 sp<AMessage> inputFormat =
787 mSource->getFormat(false /* audio */);
Andreas Huber3831a062010-12-21 10:22:33 -0800788
Chong Zhangced1c2f2014-08-08 15:22:35 -0700789 updateVideoSize(inputFormat, format);
Andreas Huber31e25082011-01-10 10:38:31 -0800790 }
Lajos Molnar1cd13982014-01-17 15:12:51 -0800791 } else if (what == Decoder::kWhatShutdownCompleted) {
Steve Block3856b092011-10-20 11:56:00 +0100792 ALOGV("%s shutdown completed", audio ? "audio" : "video");
Andreas Huber3831a062010-12-21 10:22:33 -0800793 if (audio) {
794 mAudioDecoder.clear();
795
796 CHECK_EQ((int)mFlushingAudio, (int)SHUTTING_DOWN_DECODER);
797 mFlushingAudio = SHUT_DOWN;
798 } else {
799 mVideoDecoder.clear();
800
801 CHECK_EQ((int)mFlushingVideo, (int)SHUTTING_DOWN_DECODER);
802 mFlushingVideo = SHUT_DOWN;
803 }
804
805 finishFlushIfPossible();
Lajos Molnar1cd13982014-01-17 15:12:51 -0800806 } else if (what == Decoder::kWhatError) {
Steve Block29357bc2012-01-06 19:20:56 +0000807 ALOGE("Received error from %s decoder, aborting playback.",
Andreas Huberc92fd242011-08-16 13:48:44 -0700808 audio ? "audio" : "video");
809
Chong Zhangf4c0a942014-08-11 15:14:10 -0700810 status_t err;
811 if (!msg->findInt32("err", &err)) {
812 err = UNKNOWN_ERROR;
813 }
814 mRenderer->queueEOS(audio, err);
Marco Nelissen9e2b7912014-08-18 16:13:03 -0700815 if (audio && mFlushingAudio != NONE) {
816 mAudioDecoder.clear();
817 mFlushingAudio = SHUT_DOWN;
818 } else if (!audio && mFlushingVideo != NONE){
819 mVideoDecoder.clear();
820 mFlushingVideo = SHUT_DOWN;
821 }
822 finishFlushIfPossible();
Lajos Molnar1cd13982014-01-17 15:12:51 -0800823 } else if (what == Decoder::kWhatDrainThisBuffer) {
824 renderBuffer(audio, msg);
825 } else {
826 ALOGV("Unhandled decoder notification %d '%c%c%c%c'.",
Andreas Hubera1f8ab02012-11-30 10:53:22 -0800827 what,
828 what >> 24,
829 (what >> 16) & 0xff,
830 (what >> 8) & 0xff,
831 what & 0xff);
Andreas Huberf9334412010-12-15 15:17:42 -0800832 }
833
834 break;
835 }
836
837 case kWhatRendererNotify:
838 {
839 int32_t what;
840 CHECK(msg->findInt32("what", &what));
841
842 if (what == Renderer::kWhatEOS) {
843 int32_t audio;
844 CHECK(msg->findInt32("audio", &audio));
845
Andreas Huberc92fd242011-08-16 13:48:44 -0700846 int32_t finalResult;
847 CHECK(msg->findInt32("finalResult", &finalResult));
848
Andreas Huberf9334412010-12-15 15:17:42 -0800849 if (audio) {
850 mAudioEOS = true;
851 } else {
852 mVideoEOS = true;
853 }
854
Andreas Huberc92fd242011-08-16 13:48:44 -0700855 if (finalResult == ERROR_END_OF_STREAM) {
Steve Block3856b092011-10-20 11:56:00 +0100856 ALOGV("reached %s EOS", audio ? "audio" : "video");
Andreas Huberc92fd242011-08-16 13:48:44 -0700857 } else {
Steve Block29357bc2012-01-06 19:20:56 +0000858 ALOGE("%s track encountered an error (%d)",
Andreas Huberc92fd242011-08-16 13:48:44 -0700859 audio ? "audio" : "video", finalResult);
860
861 notifyListener(
862 MEDIA_ERROR, MEDIA_ERROR_UNKNOWN, finalResult);
863 }
Andreas Huberf9334412010-12-15 15:17:42 -0800864
865 if ((mAudioEOS || mAudioDecoder == NULL)
866 && (mVideoEOS || mVideoDecoder == NULL)) {
867 notifyListener(MEDIA_PLAYBACK_COMPLETE, 0, 0);
868 }
Andreas Huber43c3e6c2011-01-05 12:17:08 -0800869 } else if (what == Renderer::kWhatPosition) {
870 int64_t positionUs;
871 CHECK(msg->findInt64("positionUs", &positionUs));
Wei Jiaac428aa2014-09-02 19:01:34 -0700872 mCurrentPositionUs = positionUs;
Andreas Huber43c3e6c2011-01-05 12:17:08 -0800873
Andreas Huber3fe62152011-09-16 15:09:22 -0700874 CHECK(msg->findInt64("videoLateByUs", &mVideoLateByUs));
875
Andreas Huber43c3e6c2011-01-05 12:17:08 -0800876 if (mDriver != NULL) {
877 sp<NuPlayerDriver> driver = mDriver.promote();
878 if (driver != NULL) {
879 driver->notifyPosition(positionUs);
Andreas Huber3fe62152011-09-16 15:09:22 -0700880
881 driver->notifyFrameStats(
882 mNumFramesTotal, mNumFramesDropped);
Andreas Huber43c3e6c2011-01-05 12:17:08 -0800883 }
884 }
Andreas Huber3fe62152011-09-16 15:09:22 -0700885 } else if (what == Renderer::kWhatFlushComplete) {
Andreas Huberf9334412010-12-15 15:17:42 -0800886 int32_t audio;
887 CHECK(msg->findInt32("audio", &audio));
888
Steve Block3856b092011-10-20 11:56:00 +0100889 ALOGV("renderer %s flush completed.", audio ? "audio" : "video");
James Dongf57b4ea2012-07-20 13:38:36 -0700890 } else if (what == Renderer::kWhatVideoRenderingStart) {
891 notifyListener(MEDIA_INFO, MEDIA_INFO_RENDERING_START, 0);
Lajos Molnarcbaffcf2013-08-14 18:30:38 -0700892 } else if (what == Renderer::kWhatMediaRenderingStart) {
893 ALOGV("media rendering started");
894 notifyListener(MEDIA_STARTED, 0, 0);
Wei Jia3a2956d2014-07-22 16:01:33 -0700895 } else if (what == Renderer::kWhatAudioOffloadTearDown) {
896 ALOGV("Tear down audio offload, fall back to s/w path");
897 int64_t positionUs;
898 CHECK(msg->findInt64("positionUs", &positionUs));
Andy Hung282a7e32014-08-14 15:56:34 -0700899 closeAudioSink();
Wei Jia3a2956d2014-07-22 16:01:33 -0700900 mAudioDecoder.clear();
901 mRenderer->flush(true /* audio */);
902 if (mVideoDecoder != NULL) {
903 mRenderer->flush(false /* audio */);
904 }
905 mRenderer->signalDisableOffloadAudio();
906 mOffloadAudio = false;
907
908 performSeek(positionUs);
909 instantiateDecoder(true /* audio */, &mAudioDecoder);
Andreas Huberf9334412010-12-15 15:17:42 -0800910 }
911 break;
912 }
913
914 case kWhatMoreDataQueued:
915 {
916 break;
917 }
918
Andreas Huber1aef2112011-01-04 14:01:29 -0800919 case kWhatReset:
920 {
Steve Block3856b092011-10-20 11:56:00 +0100921 ALOGV("kWhatReset");
Andreas Huber1aef2112011-01-04 14:01:29 -0800922
Andreas Hubera1f8ab02012-11-30 10:53:22 -0800923 mDeferredActions.push_back(
Andreas Huber14f76722013-01-15 09:04:18 -0800924 new ShutdownDecoderAction(
925 true /* audio */, true /* video */));
Andreas Huberb7c8e912012-11-27 15:02:53 -0800926
Andreas Hubera1f8ab02012-11-30 10:53:22 -0800927 mDeferredActions.push_back(
928 new SimpleAction(&NuPlayer::performReset));
Andreas Huberb58ce9f2011-11-28 16:27:35 -0800929
Andreas Hubera1f8ab02012-11-30 10:53:22 -0800930 processDeferredActions();
Andreas Huber1aef2112011-01-04 14:01:29 -0800931 break;
932 }
933
Andreas Huber43c3e6c2011-01-05 12:17:08 -0800934 case kWhatSeek:
935 {
936 int64_t seekTimeUs;
937 CHECK(msg->findInt64("seekTimeUs", &seekTimeUs));
938
Andreas Hubera1f8ab02012-11-30 10:53:22 -0800939 ALOGV("kWhatSeek seekTimeUs=%lld us", seekTimeUs);
Andreas Huber43c3e6c2011-01-05 12:17:08 -0800940
Andreas Hubera1f8ab02012-11-30 10:53:22 -0800941 mDeferredActions.push_back(
942 new SimpleAction(&NuPlayer::performDecoderFlush));
Andreas Huber43c3e6c2011-01-05 12:17:08 -0800943
Andreas Hubera1f8ab02012-11-30 10:53:22 -0800944 mDeferredActions.push_back(new SeekAction(seekTimeUs));
Andreas Huber43c3e6c2011-01-05 12:17:08 -0800945
Andreas Hubera1f8ab02012-11-30 10:53:22 -0800946 processDeferredActions();
Andreas Huber43c3e6c2011-01-05 12:17:08 -0800947 break;
948 }
949
Andreas Huberb4082222011-01-20 15:23:04 -0800950 case kWhatPause:
951 {
952 CHECK(mRenderer != NULL);
Roger Jönssonfba60da2013-01-21 17:15:45 +0100953 mSource->pause();
Andreas Huberb4082222011-01-20 15:23:04 -0800954 mRenderer->pause();
955 break;
956 }
957
958 case kWhatResume:
959 {
960 CHECK(mRenderer != NULL);
Roger Jönssonfba60da2013-01-21 17:15:45 +0100961 mSource->resume();
Andreas Huberb4082222011-01-20 15:23:04 -0800962 mRenderer->resume();
963 break;
964 }
965
Andreas Huberb5f25f02013-02-05 10:14:26 -0800966 case kWhatSourceNotify:
967 {
Andreas Huber9575c962013-02-05 13:59:56 -0800968 onSourceNotify(msg);
Andreas Huberb5f25f02013-02-05 10:14:26 -0800969 break;
970 }
971
Chong Zhanga7fa1d92014-06-11 14:49:23 -0700972 case kWhatClosedCaptionNotify:
973 {
974 onClosedCaptionNotify(msg);
975 break;
976 }
977
Andreas Huberf9334412010-12-15 15:17:42 -0800978 default:
979 TRESPASS();
980 break;
981 }
982}
983
Andreas Huber3831a062010-12-21 10:22:33 -0800984void NuPlayer::finishFlushIfPossible() {
Wei Jia53904f32014-07-29 10:22:53 -0700985 if (mFlushingAudio != NONE && mFlushingAudio != FLUSHED
986 && mFlushingAudio != SHUT_DOWN) {
Andreas Huber3831a062010-12-21 10:22:33 -0800987 return;
988 }
989
Wei Jia53904f32014-07-29 10:22:53 -0700990 if (mFlushingVideo != NONE && mFlushingVideo != FLUSHED
991 && mFlushingVideo != SHUT_DOWN) {
Andreas Huber3831a062010-12-21 10:22:33 -0800992 return;
993 }
994
Steve Block3856b092011-10-20 11:56:00 +0100995 ALOGV("both audio and video are flushed now.");
Andreas Huber3831a062010-12-21 10:22:33 -0800996
Phil Burk9f526492014-09-03 15:04:12 -0700997 mPendingAudioAccessUnit.clear();
998
Andreas Huber6e3d3112011-11-28 12:36:11 -0800999 if (mTimeDiscontinuityPending) {
1000 mRenderer->signalTimeDiscontinuity();
1001 mTimeDiscontinuityPending = false;
1002 }
Andreas Huber3831a062010-12-21 10:22:33 -08001003
Wei Jia53904f32014-07-29 10:22:53 -07001004 if (mAudioDecoder != NULL && mFlushingAudio == FLUSHED) {
Andreas Huber3831a062010-12-21 10:22:33 -08001005 mAudioDecoder->signalResume();
1006 }
1007
Wei Jia53904f32014-07-29 10:22:53 -07001008 if (mVideoDecoder != NULL && mFlushingVideo == FLUSHED) {
Andreas Huber3831a062010-12-21 10:22:33 -08001009 mVideoDecoder->signalResume();
1010 }
1011
1012 mFlushingAudio = NONE;
1013 mFlushingVideo = NONE;
Andreas Huber3831a062010-12-21 10:22:33 -08001014
Andreas Hubera1f8ab02012-11-30 10:53:22 -08001015 processDeferredActions();
Andreas Huber1aef2112011-01-04 14:01:29 -08001016}
1017
1018void NuPlayer::postScanSources() {
1019 if (mScanSourcesPending) {
1020 return;
1021 }
1022
1023 sp<AMessage> msg = new AMessage(kWhatScanSources, id());
1024 msg->setInt32("generation", mScanSourcesGeneration);
1025 msg->post();
1026
1027 mScanSourcesPending = true;
1028}
1029
Andy Hung282a7e32014-08-14 15:56:34 -07001030void NuPlayer::openAudioSink(const sp<AMessage> &format, bool offloadOnly) {
1031 ALOGV("openAudioSink: offloadOnly(%d) mOffloadAudio(%d)",
1032 offloadOnly, mOffloadAudio);
1033 bool audioSinkChanged = false;
1034
1035 int32_t numChannels;
1036 CHECK(format->findInt32("channel-count", &numChannels));
1037
1038 int32_t channelMask;
1039 if (!format->findInt32("channel-mask", &channelMask)) {
1040 // signal to the AudioSink to derive the mask from count.
1041 channelMask = CHANNEL_MASK_USE_CHANNEL_ORDER;
1042 }
1043
1044 int32_t sampleRate;
1045 CHECK(format->findInt32("sample-rate", &sampleRate));
1046
1047 uint32_t flags;
1048 int64_t durationUs;
1049 // FIXME: we should handle the case where the video decoder
1050 // is created after we receive the format change indication.
1051 // Current code will just make that we select deep buffer
1052 // with video which should not be a problem as it should
1053 // not prevent from keeping A/V sync.
1054 if (mVideoDecoder == NULL &&
1055 mSource->getDuration(&durationUs) == OK &&
1056 durationUs
1057 > AUDIO_SINK_MIN_DEEP_BUFFER_DURATION_US) {
1058 flags = AUDIO_OUTPUT_FLAG_DEEP_BUFFER;
1059 } else {
1060 flags = AUDIO_OUTPUT_FLAG_NONE;
1061 }
1062
1063 if (mOffloadAudio) {
1064 audio_format_t audioFormat = AUDIO_FORMAT_PCM_16_BIT;
1065 AString mime;
1066 CHECK(format->findString("mime", &mime));
1067 status_t err = mapMimeToAudioFormat(audioFormat, mime.c_str());
1068
1069 if (err != OK) {
1070 ALOGE("Couldn't map mime \"%s\" to a valid "
1071 "audio_format", mime.c_str());
1072 mOffloadAudio = false;
1073 } else {
1074 ALOGV("Mime \"%s\" mapped to audio_format 0x%x",
1075 mime.c_str(), audioFormat);
1076
1077 int avgBitRate = -1;
1078 format->findInt32("bit-rate", &avgBitRate);
1079
1080 int32_t aacProfile = -1;
1081 if (audioFormat == AUDIO_FORMAT_AAC
1082 && format->findInt32("aac-profile", &aacProfile)) {
1083 // Redefine AAC format as per aac profile
1084 mapAACProfileToAudioFormat(
1085 audioFormat,
1086 aacProfile);
1087 }
1088
1089 audio_offload_info_t offloadInfo = AUDIO_INFO_INITIALIZER;
1090 offloadInfo.duration_us = -1;
1091 format->findInt64(
1092 "durationUs", &offloadInfo.duration_us);
1093 offloadInfo.sample_rate = sampleRate;
1094 offloadInfo.channel_mask = channelMask;
1095 offloadInfo.format = audioFormat;
1096 offloadInfo.stream_type = AUDIO_STREAM_MUSIC;
1097 offloadInfo.bit_rate = avgBitRate;
1098 offloadInfo.has_video = (mVideoDecoder != NULL);
1099 offloadInfo.is_streaming = true;
1100
1101 if (memcmp(&mCurrentOffloadInfo, &offloadInfo, sizeof(offloadInfo)) == 0) {
1102 ALOGV("openAudioSink: no change in offload mode");
1103 return; // no change from previous configuration, everything ok.
1104 }
1105 ALOGV("openAudioSink: try to open AudioSink in offload mode");
1106 flags |= AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD;
Ronghua Wu1ffb5382014-08-18 15:57:03 -07001107 flags &= ~AUDIO_OUTPUT_FLAG_DEEP_BUFFER;
Andy Hung282a7e32014-08-14 15:56:34 -07001108 audioSinkChanged = true;
1109 mAudioSink->close();
1110 err = mAudioSink->open(
1111 sampleRate,
1112 numChannels,
1113 (audio_channel_mask_t)channelMask,
1114 audioFormat,
1115 8 /* bufferCount */,
1116 &NuPlayer::Renderer::AudioSinkCallback,
1117 mRenderer.get(),
1118 (audio_output_flags_t)flags,
1119 &offloadInfo);
1120
1121 if (err == OK) {
1122 // If the playback is offloaded to h/w, we pass
1123 // the HAL some metadata information.
1124 // We don't want to do this for PCM because it
1125 // will be going through the AudioFlinger mixer
1126 // before reaching the hardware.
1127 sp<MetaData> audioMeta =
1128 mSource->getFormatMeta(true /* audio */);
1129 sendMetaDataToHal(mAudioSink, audioMeta);
1130 mCurrentOffloadInfo = offloadInfo;
1131 err = mAudioSink->start();
1132 ALOGV_IF(err == OK, "openAudioSink: offload succeeded");
1133 }
1134 if (err != OK) {
1135 // Clean up, fall back to non offload mode.
1136 mAudioSink->close();
1137 mRenderer->signalDisableOffloadAudio();
1138 mOffloadAudio = false;
1139 mCurrentOffloadInfo = AUDIO_INFO_INITIALIZER;
1140 ALOGV("openAudioSink: offload failed");
1141 }
1142 }
1143 }
1144 if (!offloadOnly && !mOffloadAudio) {
1145 flags &= ~AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD;
1146 ALOGV("openAudioSink: open AudioSink in NON-offload mode");
1147
1148 audioSinkChanged = true;
1149 mAudioSink->close();
1150 mCurrentOffloadInfo = AUDIO_INFO_INITIALIZER;
1151 CHECK_EQ(mAudioSink->open(
1152 sampleRate,
1153 numChannels,
1154 (audio_channel_mask_t)channelMask,
1155 AUDIO_FORMAT_PCM_16_BIT,
1156 8 /* bufferCount */,
1157 NULL,
1158 NULL,
1159 (audio_output_flags_t)flags),
1160 (status_t)OK);
1161 mAudioSink->start();
1162 }
1163 if (audioSinkChanged) {
1164 mRenderer->signalAudioSinkChanged();
1165 }
1166}
1167
1168void NuPlayer::closeAudioSink() {
1169 mAudioSink->close();
1170 mCurrentOffloadInfo = AUDIO_INFO_INITIALIZER;
1171}
1172
Andreas Huber5bc087c2010-12-23 10:27:40 -08001173status_t NuPlayer::instantiateDecoder(bool audio, sp<Decoder> *decoder) {
Andreas Huberf9334412010-12-15 15:17:42 -08001174 if (*decoder != NULL) {
1175 return OK;
1176 }
1177
Andreas Huber84066782011-08-16 09:34:26 -07001178 sp<AMessage> format = mSource->getFormat(audio);
Andreas Huberf9334412010-12-15 15:17:42 -08001179
Andreas Huber84066782011-08-16 09:34:26 -07001180 if (format == NULL) {
Andreas Huberf9334412010-12-15 15:17:42 -08001181 return -EWOULDBLOCK;
1182 }
1183
Andreas Huber3fe62152011-09-16 15:09:22 -07001184 if (!audio) {
Andreas Huber84066782011-08-16 09:34:26 -07001185 AString mime;
1186 CHECK(format->findString("mime", &mime));
1187 mVideoIsAVC = !strcasecmp(MEDIA_MIMETYPE_VIDEO_AVC, mime.c_str());
Chong Zhanga7fa1d92014-06-11 14:49:23 -07001188
1189 sp<AMessage> ccNotify = new AMessage(kWhatClosedCaptionNotify, id());
1190 mCCDecoder = new CCDecoder(ccNotify);
Lajos Molnar09524832014-07-17 14:29:51 -07001191
1192 if (mSourceFlags & Source::FLAG_SECURE) {
1193 format->setInt32("secure", true);
1194 }
Andreas Huber3fe62152011-09-16 15:09:22 -07001195 }
1196
Wei Jiabc2fb722014-07-08 16:37:57 -07001197 if (audio) {
Wei Jia88703c32014-08-06 11:24:07 -07001198 sp<AMessage> notify = new AMessage(kWhatAudioNotify, id());
1199 ++mAudioDecoderGeneration;
1200 notify->setInt32("generation", mAudioDecoderGeneration);
1201
Wei Jiabc2fb722014-07-08 16:37:57 -07001202 if (mOffloadAudio) {
1203 *decoder = new DecoderPassThrough(notify);
1204 } else {
1205 *decoder = new Decoder(notify);
1206 }
1207 } else {
Wei Jia88703c32014-08-06 11:24:07 -07001208 sp<AMessage> notify = new AMessage(kWhatVideoNotify, id());
1209 ++mVideoDecoderGeneration;
1210 notify->setInt32("generation", mVideoDecoderGeneration);
1211
Wei Jiabc2fb722014-07-08 16:37:57 -07001212 *decoder = new Decoder(notify, mNativeWindow);
1213 }
Lajos Molnar1cd13982014-01-17 15:12:51 -08001214 (*decoder)->init();
Andreas Huber84066782011-08-16 09:34:26 -07001215 (*decoder)->configure(format);
Andreas Huberf9334412010-12-15 15:17:42 -08001216
Lajos Molnar09524832014-07-17 14:29:51 -07001217 // allocate buffers to decrypt widevine source buffers
1218 if (!audio && (mSourceFlags & Source::FLAG_SECURE)) {
1219 Vector<sp<ABuffer> > inputBufs;
1220 CHECK_EQ((*decoder)->getInputBuffers(&inputBufs), (status_t)OK);
1221
1222 Vector<MediaBuffer *> mediaBufs;
1223 for (size_t i = 0; i < inputBufs.size(); i++) {
1224 const sp<ABuffer> &buffer = inputBufs[i];
1225 MediaBuffer *mbuf = new MediaBuffer(buffer->data(), buffer->size());
1226 mediaBufs.push(mbuf);
1227 }
1228
1229 status_t err = mSource->setBuffers(audio, mediaBufs);
1230 if (err != OK) {
1231 for (size_t i = 0; i < mediaBufs.size(); ++i) {
1232 mediaBufs[i]->release();
1233 }
1234 mediaBufs.clear();
1235 ALOGE("Secure source didn't support secure mediaBufs.");
1236 return err;
1237 }
1238 }
Andreas Huberf9334412010-12-15 15:17:42 -08001239 return OK;
1240}
1241
1242status_t NuPlayer::feedDecoderInputData(bool audio, const sp<AMessage> &msg) {
1243 sp<AMessage> reply;
1244 CHECK(msg->findMessage("reply", &reply));
1245
Wei Jia53904f32014-07-29 10:22:53 -07001246 if ((audio && mFlushingAudio != NONE)
Wei Jiaf702d042014-09-09 12:08:47 -07001247 || (!audio && mFlushingVideo != NONE)
1248 || mSource == NULL) {
Wei Jiab189a5b2014-08-07 06:11:39 +00001249 reply->setInt32("err", INFO_DISCONTINUITY);
1250 reply->post();
1251 return OK;
Andreas Huberf9334412010-12-15 15:17:42 -08001252 }
1253
1254 sp<ABuffer> accessUnit;
Andreas Huberf9334412010-12-15 15:17:42 -08001255
Phil Burk9f526492014-09-03 15:04:12 -07001256 // Aggregate smaller buffers into a larger buffer.
1257 // The goal is to reduce power consumption.
1258 // Unfortunately this does not work with the software AAC decoder.
1259 // TODO optimize buffer size for power consumption
1260 // The offload read buffer size is 32 KB but 24 KB uses less power.
1261 const int kAudioBigBufferSizeBytes = 24 * 1024;
1262 bool doBufferAggregation = (audio && mOffloadAudio);
1263 sp<ABuffer> biggerBuffer;
1264 bool needMoreData = false;
1265 int numSmallBuffers = 0;
1266 bool gotTime = false;
1267
Andreas Huber3fe62152011-09-16 15:09:22 -07001268 bool dropAccessUnit;
1269 do {
Phil Burk9f526492014-09-03 15:04:12 -07001270 status_t err;
1271 // Did we save an accessUnit earlier because of a discontinuity?
1272 if (audio && (mPendingAudioAccessUnit != NULL)) {
1273 accessUnit = mPendingAudioAccessUnit;
1274 mPendingAudioAccessUnit.clear();
1275 err = mPendingAudioErr;
1276 ALOGV("feedDecoderInputData() use mPendingAudioAccessUnit");
1277 } else {
1278 err = mSource->dequeueAccessUnit(audio, &accessUnit);
1279 }
Andreas Huber5bc087c2010-12-23 10:27:40 -08001280
Andreas Huber3fe62152011-09-16 15:09:22 -07001281 if (err == -EWOULDBLOCK) {
Phil Burk9f526492014-09-03 15:04:12 -07001282 ALOGD("feedDecoderInputData() got EWOULDBLOCK");
1283 if (biggerBuffer == NULL) {
1284 return err;
1285 } else {
1286 break; // Reply with data that we already have.
1287 }
Andreas Huber3fe62152011-09-16 15:09:22 -07001288 } else if (err != OK) {
1289 if (err == INFO_DISCONTINUITY) {
Phil Burk9f526492014-09-03 15:04:12 -07001290 if (biggerBuffer != NULL) {
1291 // We already have some data so save this for later.
1292 mPendingAudioErr = err;
1293 mPendingAudioAccessUnit = accessUnit;
1294 accessUnit.clear();
1295 ALOGD("feedDecoderInputData() save discontinuity for later");
1296 break;
1297 }
Andreas Huber3fe62152011-09-16 15:09:22 -07001298 int32_t type;
1299 CHECK(accessUnit->meta()->findInt32("discontinuity", &type));
Andreas Huber53df1a42010-12-22 10:03:04 -08001300
Andreas Huber3fe62152011-09-16 15:09:22 -07001301 bool formatChange =
Andreas Huber6e3d3112011-11-28 12:36:11 -08001302 (audio &&
1303 (type & ATSParser::DISCONTINUITY_AUDIO_FORMAT))
1304 || (!audio &&
1305 (type & ATSParser::DISCONTINUITY_VIDEO_FORMAT));
Andreas Huber53df1a42010-12-22 10:03:04 -08001306
Andreas Huber6e3d3112011-11-28 12:36:11 -08001307 bool timeChange = (type & ATSParser::DISCONTINUITY_TIME) != 0;
1308
Steve Blockdf64d152012-01-04 20:05:49 +00001309 ALOGI("%s discontinuity (formatChange=%d, time=%d)",
Andreas Huber6e3d3112011-11-28 12:36:11 -08001310 audio ? "audio" : "video", formatChange, timeChange);
Andreas Huber32f3cef2011-03-02 15:34:46 -08001311
Andreas Huber3fe62152011-09-16 15:09:22 -07001312 if (audio) {
1313 mSkipRenderingAudioUntilMediaTimeUs = -1;
1314 } else {
1315 mSkipRenderingVideoUntilMediaTimeUs = -1;
1316 }
Andreas Huber32f3cef2011-03-02 15:34:46 -08001317
Andreas Huber6e3d3112011-11-28 12:36:11 -08001318 if (timeChange) {
1319 sp<AMessage> extra;
1320 if (accessUnit->meta()->findMessage("extra", &extra)
1321 && extra != NULL) {
1322 int64_t resumeAtMediaTimeUs;
1323 if (extra->findInt64(
1324 "resume-at-mediatimeUs", &resumeAtMediaTimeUs)) {
Steve Blockdf64d152012-01-04 20:05:49 +00001325 ALOGI("suppressing rendering of %s until %lld us",
Andreas Huber6e3d3112011-11-28 12:36:11 -08001326 audio ? "audio" : "video", resumeAtMediaTimeUs);
Andreas Huber3fe62152011-09-16 15:09:22 -07001327
Andreas Huber6e3d3112011-11-28 12:36:11 -08001328 if (audio) {
1329 mSkipRenderingAudioUntilMediaTimeUs =
1330 resumeAtMediaTimeUs;
1331 } else {
1332 mSkipRenderingVideoUntilMediaTimeUs =
1333 resumeAtMediaTimeUs;
1334 }
Andreas Huber3fe62152011-09-16 15:09:22 -07001335 }
Andreas Huber32f3cef2011-03-02 15:34:46 -08001336 }
1337 }
Andreas Huber3fe62152011-09-16 15:09:22 -07001338
Andreas Huber6e3d3112011-11-28 12:36:11 -08001339 mTimeDiscontinuityPending =
1340 mTimeDiscontinuityPending || timeChange;
1341
Lajos Molnar87603c02014-08-20 19:25:30 -07001342 bool seamlessFormatChange = false;
1343 sp<AMessage> newFormat = mSource->getFormat(audio);
1344 if (formatChange) {
1345 seamlessFormatChange =
1346 getDecoder(audio)->supportsSeamlessFormatChange(newFormat);
1347 // treat seamless format change separately
1348 formatChange = !seamlessFormatChange;
1349 }
1350 bool shutdownOrFlush = formatChange || timeChange;
1351
1352 // We want to queue up scan-sources only once per discontinuity.
1353 // We control this by doing it only if neither audio nor video are
1354 // flushing or shutting down. (After handling 1st discontinuity, one
1355 // of the flushing states will not be NONE.)
1356 // No need to scan sources if this discontinuity does not result
1357 // in a flush or shutdown, as the flushing state will stay NONE.
1358 if (mFlushingAudio == NONE && mFlushingVideo == NONE &&
1359 shutdownOrFlush) {
Robert Shiha2981012014-07-30 17:41:24 -07001360 // And we'll resume scanning sources once we're done
1361 // flushing.
1362 mDeferredActions.push_front(
1363 new SimpleAction(
1364 &NuPlayer::performScanSources));
1365 }
1366
Lajos Molnar87603c02014-08-20 19:25:30 -07001367 if (formatChange /* not seamless */) {
1368 // must change decoder
1369 flushDecoder(audio, /* needShutdown = */ true);
1370 } else if (timeChange) {
1371 // need to flush
1372 flushDecoder(audio, /* needShutdown = */ false, newFormat);
1373 err = OK;
1374 } else if (seamlessFormatChange) {
1375 // reuse existing decoder and don't flush
1376 updateDecoderFormatWithoutFlush(audio, newFormat);
1377 err = OK;
Andreas Huber6e3d3112011-11-28 12:36:11 -08001378 } else {
1379 // This stream is unaffected by the discontinuity
Andreas Huber6e3d3112011-11-28 12:36:11 -08001380 return -EWOULDBLOCK;
1381 }
Andreas Huber32f3cef2011-03-02 15:34:46 -08001382 }
1383
Andreas Huber3fe62152011-09-16 15:09:22 -07001384 reply->setInt32("err", err);
1385 reply->post();
1386 return OK;
Andreas Huberf9334412010-12-15 15:17:42 -08001387 }
1388
Andreas Huber3fe62152011-09-16 15:09:22 -07001389 if (!audio) {
1390 ++mNumFramesTotal;
1391 }
1392
1393 dropAccessUnit = false;
1394 if (!audio
Lajos Molnar09524832014-07-17 14:29:51 -07001395 && !(mSourceFlags & Source::FLAG_SECURE)
Andreas Huber3fe62152011-09-16 15:09:22 -07001396 && mVideoLateByUs > 100000ll
1397 && mVideoIsAVC
1398 && !IsAVCReferenceFrame(accessUnit)) {
1399 dropAccessUnit = true;
1400 ++mNumFramesDropped;
1401 }
Phil Burk9f526492014-09-03 15:04:12 -07001402
1403 size_t smallSize = accessUnit->size();
1404 needMoreData = false;
1405 if (doBufferAggregation && (biggerBuffer == NULL)
1406 // Don't bother if only room for a few small buffers.
1407 && (smallSize < (kAudioBigBufferSizeBytes / 3))) {
1408 // Create a larger buffer for combining smaller buffers from the extractor.
1409 biggerBuffer = new ABuffer(kAudioBigBufferSizeBytes);
1410 biggerBuffer->setRange(0, 0); // start empty
1411 }
1412
1413 if (biggerBuffer != NULL) {
1414 int64_t timeUs;
1415 bool smallTimestampValid = accessUnit->meta()->findInt64("timeUs", &timeUs);
1416 // Will the smaller buffer fit?
1417 size_t bigSize = biggerBuffer->size();
1418 size_t roomLeft = biggerBuffer->capacity() - bigSize;
1419 // Should we save this small buffer for the next big buffer?
1420 // If the first small buffer did not have a timestamp then save
1421 // any buffer that does have a timestamp until the next big buffer.
1422 if ((smallSize > roomLeft)
1423 || (!gotTime && (numSmallBuffers > 0) && smallTimestampValid)) {
1424 mPendingAudioErr = err;
1425 mPendingAudioAccessUnit = accessUnit;
1426 accessUnit.clear();
1427 } else {
1428 // Append small buffer to the bigger buffer.
1429 memcpy(biggerBuffer->base() + bigSize, accessUnit->data(), smallSize);
1430 bigSize += smallSize;
1431 biggerBuffer->setRange(0, bigSize);
1432
1433 // Keep looping until we run out of room in the biggerBuffer.
1434 needMoreData = true;
1435
1436 // Grab time from first small buffer if available.
1437 if ((numSmallBuffers == 0) && smallTimestampValid) {
1438 biggerBuffer->meta()->setInt64("timeUs", timeUs);
1439 gotTime = true;
1440 }
1441
1442 ALOGV("feedDecoderInputData() #%d, smallSize = %zu, bigSize = %zu, capacity = %zu",
1443 numSmallBuffers, smallSize, bigSize, biggerBuffer->capacity());
1444 numSmallBuffers++;
1445 }
1446 }
1447 } while (dropAccessUnit || needMoreData);
Andreas Huberf9334412010-12-15 15:17:42 -08001448
Steve Block3856b092011-10-20 11:56:00 +01001449 // ALOGV("returned a valid buffer of %s data", audio ? "audio" : "video");
Andreas Huberf9334412010-12-15 15:17:42 -08001450
1451#if 0
1452 int64_t mediaTimeUs;
1453 CHECK(accessUnit->meta()->findInt64("timeUs", &mediaTimeUs));
Steve Block3856b092011-10-20 11:56:00 +01001454 ALOGV("feeding %s input buffer at media time %.2f secs",
Andreas Huberf9334412010-12-15 15:17:42 -08001455 audio ? "audio" : "video",
1456 mediaTimeUs / 1E6);
1457#endif
1458
Chong Zhanga7fa1d92014-06-11 14:49:23 -07001459 if (!audio) {
1460 mCCDecoder->decode(accessUnit);
1461 }
1462
Phil Burk9f526492014-09-03 15:04:12 -07001463 if (biggerBuffer != NULL) {
1464 ALOGV("feedDecoderInputData() reply with aggregated buffer, %d", numSmallBuffers);
1465 reply->setBuffer("buffer", biggerBuffer);
1466 } else {
1467 reply->setBuffer("buffer", accessUnit);
1468 }
1469
Andreas Huberf9334412010-12-15 15:17:42 -08001470 reply->post();
1471
1472 return OK;
1473}
1474
1475void NuPlayer::renderBuffer(bool audio, const sp<AMessage> &msg) {
Steve Block3856b092011-10-20 11:56:00 +01001476 // ALOGV("renderBuffer %s", audio ? "audio" : "video");
Andreas Huberf9334412010-12-15 15:17:42 -08001477
1478 sp<AMessage> reply;
1479 CHECK(msg->findMessage("reply", &reply));
1480
Wei Jia53904f32014-07-29 10:22:53 -07001481 if ((audio && mFlushingAudio != NONE)
1482 || (!audio && mFlushingVideo != NONE)) {
Andreas Huber18ac5402011-08-31 15:04:25 -07001483 // We're currently attempting to flush the decoder, in order
1484 // to complete this, the decoder wants all its buffers back,
1485 // so we don't want any output buffers it sent us (from before
1486 // we initiated the flush) to be stuck in the renderer's queue.
1487
Steve Block3856b092011-10-20 11:56:00 +01001488 ALOGV("we're still flushing the %s decoder, sending its output buffer"
Andreas Huber18ac5402011-08-31 15:04:25 -07001489 " right back.", audio ? "audio" : "video");
1490
1491 reply->post();
1492 return;
1493 }
1494
Andreas Huber2d8bedd2012-02-21 14:38:23 -08001495 sp<ABuffer> buffer;
1496 CHECK(msg->findBuffer("buffer", &buffer));
Andreas Huberf9334412010-12-15 15:17:42 -08001497
Chong Zhanga7fa1d92014-06-11 14:49:23 -07001498 int64_t mediaTimeUs;
1499 CHECK(buffer->meta()->findInt64("timeUs", &mediaTimeUs));
1500
Andreas Huber32f3cef2011-03-02 15:34:46 -08001501 int64_t &skipUntilMediaTimeUs =
1502 audio
1503 ? mSkipRenderingAudioUntilMediaTimeUs
1504 : mSkipRenderingVideoUntilMediaTimeUs;
1505
1506 if (skipUntilMediaTimeUs >= 0) {
Andreas Huber32f3cef2011-03-02 15:34:46 -08001507
1508 if (mediaTimeUs < skipUntilMediaTimeUs) {
Steve Block3856b092011-10-20 11:56:00 +01001509 ALOGV("dropping %s buffer at time %lld as requested.",
Andreas Huber32f3cef2011-03-02 15:34:46 -08001510 audio ? "audio" : "video",
1511 mediaTimeUs);
1512
1513 reply->post();
1514 return;
1515 }
1516
1517 skipUntilMediaTimeUs = -1;
1518 }
1519
Chong Zhanga7fa1d92014-06-11 14:49:23 -07001520 if (!audio && mCCDecoder->isSelected()) {
1521 mCCDecoder->display(mediaTimeUs);
1522 }
1523
Andreas Huberf9334412010-12-15 15:17:42 -08001524 mRenderer->queueBuffer(audio, buffer, reply);
1525}
1526
Chong Zhangced1c2f2014-08-08 15:22:35 -07001527void NuPlayer::updateVideoSize(
1528 const sp<AMessage> &inputFormat,
1529 const sp<AMessage> &outputFormat) {
1530 if (inputFormat == NULL) {
1531 ALOGW("Unknown video size, reporting 0x0!");
1532 notifyListener(MEDIA_SET_VIDEO_SIZE, 0, 0);
1533 return;
1534 }
1535
1536 int32_t displayWidth, displayHeight;
1537 int32_t cropLeft, cropTop, cropRight, cropBottom;
1538
1539 if (outputFormat != NULL) {
1540 int32_t width, height;
1541 CHECK(outputFormat->findInt32("width", &width));
1542 CHECK(outputFormat->findInt32("height", &height));
1543
1544 int32_t cropLeft, cropTop, cropRight, cropBottom;
1545 CHECK(outputFormat->findRect(
1546 "crop",
1547 &cropLeft, &cropTop, &cropRight, &cropBottom));
1548
1549 displayWidth = cropRight - cropLeft + 1;
1550 displayHeight = cropBottom - cropTop + 1;
1551
1552 ALOGV("Video output format changed to %d x %d "
1553 "(crop: %d x %d @ (%d, %d))",
1554 width, height,
1555 displayWidth,
1556 displayHeight,
1557 cropLeft, cropTop);
1558 } else {
1559 CHECK(inputFormat->findInt32("width", &displayWidth));
1560 CHECK(inputFormat->findInt32("height", &displayHeight));
1561
1562 ALOGV("Video input format %d x %d", displayWidth, displayHeight);
1563 }
1564
1565 // Take into account sample aspect ratio if necessary:
1566 int32_t sarWidth, sarHeight;
1567 if (inputFormat->findInt32("sar-width", &sarWidth)
1568 && inputFormat->findInt32("sar-height", &sarHeight)) {
1569 ALOGV("Sample aspect ratio %d : %d", sarWidth, sarHeight);
1570
1571 displayWidth = (displayWidth * sarWidth) / sarHeight;
1572
1573 ALOGV("display dimensions %d x %d", displayWidth, displayHeight);
1574 }
1575
1576 int32_t rotationDegrees;
1577 if (!inputFormat->findInt32("rotation-degrees", &rotationDegrees)) {
1578 rotationDegrees = 0;
1579 }
1580
1581 if (rotationDegrees == 90 || rotationDegrees == 270) {
1582 int32_t tmp = displayWidth;
1583 displayWidth = displayHeight;
1584 displayHeight = tmp;
1585 }
1586
1587 notifyListener(
1588 MEDIA_SET_VIDEO_SIZE,
1589 displayWidth,
1590 displayHeight);
1591}
1592
Chong Zhangdcb89b32013-08-06 09:44:47 -07001593void NuPlayer::notifyListener(int msg, int ext1, int ext2, const Parcel *in) {
Andreas Huber43c3e6c2011-01-05 12:17:08 -08001594 if (mDriver == NULL) {
Andreas Huberf9334412010-12-15 15:17:42 -08001595 return;
1596 }
1597
Andreas Huber43c3e6c2011-01-05 12:17:08 -08001598 sp<NuPlayerDriver> driver = mDriver.promote();
Andreas Huberf9334412010-12-15 15:17:42 -08001599
Andreas Huber43c3e6c2011-01-05 12:17:08 -08001600 if (driver == NULL) {
Andreas Huberf9334412010-12-15 15:17:42 -08001601 return;
1602 }
1603
Chong Zhangdcb89b32013-08-06 09:44:47 -07001604 driver->notifyListener(msg, ext1, ext2, in);
Andreas Huberf9334412010-12-15 15:17:42 -08001605}
1606
Lajos Molnar87603c02014-08-20 19:25:30 -07001607void NuPlayer::flushDecoder(
1608 bool audio, bool needShutdown, const sp<AMessage> &newFormat) {
Andreas Huber14f76722013-01-15 09:04:18 -08001609 ALOGV("[%s] flushDecoder needShutdown=%d",
1610 audio ? "audio" : "video", needShutdown);
1611
Lajos Molnar87603c02014-08-20 19:25:30 -07001612 const sp<Decoder> &decoder = getDecoder(audio);
1613 if (decoder == NULL) {
Steve Blockdf64d152012-01-04 20:05:49 +00001614 ALOGI("flushDecoder %s without decoder present",
Andreas Huber6e3d3112011-11-28 12:36:11 -08001615 audio ? "audio" : "video");
Lajos Molnar87603c02014-08-20 19:25:30 -07001616 return;
Andreas Huber6e3d3112011-11-28 12:36:11 -08001617 }
1618
Andreas Huber1aef2112011-01-04 14:01:29 -08001619 // Make sure we don't continue to scan sources until we finish flushing.
1620 ++mScanSourcesGeneration;
Andreas Huber43c3e6c2011-01-05 12:17:08 -08001621 mScanSourcesPending = false;
Andreas Huber1aef2112011-01-04 14:01:29 -08001622
Lajos Molnar87603c02014-08-20 19:25:30 -07001623 decoder->signalFlush(newFormat);
Andreas Huber1aef2112011-01-04 14:01:29 -08001624 mRenderer->flush(audio);
1625
1626 FlushStatus newStatus =
1627 needShutdown ? FLUSHING_DECODER_SHUTDOWN : FLUSHING_DECODER;
1628
1629 if (audio) {
Wei Jia53904f32014-07-29 10:22:53 -07001630 ALOGE_IF(mFlushingAudio != NONE,
1631 "audio flushDecoder() is called in state %d", mFlushingAudio);
Andreas Huber1aef2112011-01-04 14:01:29 -08001632 mFlushingAudio = newStatus;
Andreas Huber1aef2112011-01-04 14:01:29 -08001633 } else {
Wei Jia53904f32014-07-29 10:22:53 -07001634 ALOGE_IF(mFlushingVideo != NONE,
1635 "video flushDecoder() is called in state %d", mFlushingVideo);
Andreas Huber1aef2112011-01-04 14:01:29 -08001636 mFlushingVideo = newStatus;
Chong Zhangb86e68f2014-08-01 13:46:53 -07001637
1638 if (mCCDecoder != NULL) {
1639 mCCDecoder->flush();
1640 }
Andreas Huber1aef2112011-01-04 14:01:29 -08001641 }
1642}
1643
Lajos Molnar87603c02014-08-20 19:25:30 -07001644void NuPlayer::updateDecoderFormatWithoutFlush(
1645 bool audio, const sp<AMessage> &format) {
1646 ALOGV("[%s] updateDecoderFormatWithoutFlush", audio ? "audio" : "video");
1647
1648 const sp<Decoder> &decoder = getDecoder(audio);
1649 if (decoder == NULL) {
1650 ALOGI("updateDecoderFormatWithoutFlush %s without decoder present",
1651 audio ? "audio" : "video");
1652 return;
1653 }
1654
1655 decoder->signalUpdateFormat(format);
1656}
1657
Chong Zhangced1c2f2014-08-08 15:22:35 -07001658void NuPlayer::queueDecoderShutdown(
1659 bool audio, bool video, const sp<AMessage> &reply) {
1660 ALOGI("queueDecoderShutdown audio=%d, video=%d", audio, video);
Andreas Huber84066782011-08-16 09:34:26 -07001661
Chong Zhangced1c2f2014-08-08 15:22:35 -07001662 mDeferredActions.push_back(
1663 new ShutdownDecoderAction(audio, video));
Andreas Huber84066782011-08-16 09:34:26 -07001664
Chong Zhangced1c2f2014-08-08 15:22:35 -07001665 mDeferredActions.push_back(
1666 new SimpleAction(&NuPlayer::performScanSources));
Andreas Huber84066782011-08-16 09:34:26 -07001667
Chong Zhangced1c2f2014-08-08 15:22:35 -07001668 mDeferredActions.push_back(new PostMessageAction(reply));
1669
1670 processDeferredActions();
Andreas Huber84066782011-08-16 09:34:26 -07001671}
1672
James Dong0d268a32012-08-31 12:18:27 -07001673status_t NuPlayer::setVideoScalingMode(int32_t mode) {
1674 mVideoScalingMode = mode;
Andreas Huber57a339c2012-12-03 11:18:00 -08001675 if (mNativeWindow != NULL) {
James Dong0d268a32012-08-31 12:18:27 -07001676 status_t ret = native_window_set_scaling_mode(
1677 mNativeWindow->getNativeWindow().get(), mVideoScalingMode);
1678 if (ret != OK) {
1679 ALOGE("Failed to set scaling mode (%d): %s",
1680 -ret, strerror(-ret));
1681 return ret;
1682 }
1683 }
1684 return OK;
1685}
1686
Chong Zhangdcb89b32013-08-06 09:44:47 -07001687status_t NuPlayer::getTrackInfo(Parcel* reply) const {
1688 sp<AMessage> msg = new AMessage(kWhatGetTrackInfo, id());
1689 msg->setPointer("reply", reply);
1690
1691 sp<AMessage> response;
1692 status_t err = msg->postAndAwaitResponse(&response);
1693 return err;
1694}
1695
Robert Shih7c4f0d72014-07-09 18:53:31 -07001696status_t NuPlayer::getSelectedTrack(int32_t type, Parcel* reply) const {
1697 sp<AMessage> msg = new AMessage(kWhatGetSelectedTrack, id());
1698 msg->setPointer("reply", reply);
1699 msg->setInt32("type", type);
1700
1701 sp<AMessage> response;
1702 status_t err = msg->postAndAwaitResponse(&response);
1703 if (err == OK && response != NULL) {
1704 CHECK(response->findInt32("err", &err));
1705 }
1706 return err;
1707}
1708
Chong Zhangdcb89b32013-08-06 09:44:47 -07001709status_t NuPlayer::selectTrack(size_t trackIndex, bool select) {
1710 sp<AMessage> msg = new AMessage(kWhatSelectTrack, id());
1711 msg->setSize("trackIndex", trackIndex);
1712 msg->setInt32("select", select);
1713
1714 sp<AMessage> response;
1715 status_t err = msg->postAndAwaitResponse(&response);
1716
Chong Zhang404fced2014-06-11 14:45:31 -07001717 if (err != OK) {
1718 return err;
1719 }
1720
1721 if (!response->findInt32("err", &err)) {
1722 err = OK;
1723 }
1724
Chong Zhangdcb89b32013-08-06 09:44:47 -07001725 return err;
1726}
1727
Andreas Huberb7c8e912012-11-27 15:02:53 -08001728void NuPlayer::schedulePollDuration() {
1729 sp<AMessage> msg = new AMessage(kWhatPollDuration, id());
1730 msg->setInt32("generation", mPollDurationGeneration);
1731 msg->post();
1732}
1733
1734void NuPlayer::cancelPollDuration() {
1735 ++mPollDurationGeneration;
1736}
1737
Andreas Hubera1f8ab02012-11-30 10:53:22 -08001738void NuPlayer::processDeferredActions() {
1739 while (!mDeferredActions.empty()) {
1740 // We won't execute any deferred actions until we're no longer in
1741 // an intermediate state, i.e. one more more decoders are currently
1742 // flushing or shutting down.
1743
Andreas Hubera1f8ab02012-11-30 10:53:22 -08001744 if (mFlushingAudio != NONE || mFlushingVideo != NONE) {
1745 // We're currently flushing, postpone the reset until that's
1746 // completed.
1747
1748 ALOGV("postponing action mFlushingAudio=%d, mFlushingVideo=%d",
1749 mFlushingAudio, mFlushingVideo);
1750
1751 break;
1752 }
1753
1754 sp<Action> action = *mDeferredActions.begin();
1755 mDeferredActions.erase(mDeferredActions.begin());
1756
1757 action->execute(this);
1758 }
1759}
1760
1761void NuPlayer::performSeek(int64_t seekTimeUs) {
1762 ALOGV("performSeek seekTimeUs=%lld us (%.2f secs)",
1763 seekTimeUs,
1764 seekTimeUs / 1E6);
1765
Andy Hungadf34bf2014-09-03 18:22:22 -07001766 if (mSource == NULL) {
1767 // This happens when reset occurs right before the loop mode
1768 // asynchronously seeks to the start of the stream.
1769 LOG_ALWAYS_FATAL_IF(mAudioDecoder != NULL || mVideoDecoder != NULL,
1770 "mSource is NULL and decoders not NULL audio(%p) video(%p)",
1771 mAudioDecoder.get(), mVideoDecoder.get());
1772 return;
1773 }
Andreas Hubera1f8ab02012-11-30 10:53:22 -08001774 mSource->seekTo(seekTimeUs);
Robert Shihd3b0bbb2014-07-23 15:00:25 -07001775 ++mTimedTextGeneration;
Andreas Hubera1f8ab02012-11-30 10:53:22 -08001776
1777 if (mDriver != NULL) {
1778 sp<NuPlayerDriver> driver = mDriver.promote();
1779 if (driver != NULL) {
1780 driver->notifyPosition(seekTimeUs);
1781 driver->notifySeekComplete();
1782 }
1783 }
1784
1785 // everything's flushed, continue playback.
1786}
1787
1788void NuPlayer::performDecoderFlush() {
1789 ALOGV("performDecoderFlush");
1790
Andreas Huberda9740e2013-04-16 10:54:03 -07001791 if (mAudioDecoder == NULL && mVideoDecoder == NULL) {
Andreas Hubera1f8ab02012-11-30 10:53:22 -08001792 return;
1793 }
1794
1795 mTimeDiscontinuityPending = true;
1796
1797 if (mAudioDecoder != NULL) {
1798 flushDecoder(true /* audio */, false /* needShutdown */);
1799 }
1800
1801 if (mVideoDecoder != NULL) {
1802 flushDecoder(false /* audio */, false /* needShutdown */);
1803 }
1804}
1805
Andreas Huber14f76722013-01-15 09:04:18 -08001806void NuPlayer::performDecoderShutdown(bool audio, bool video) {
1807 ALOGV("performDecoderShutdown audio=%d, video=%d", audio, video);
Andreas Hubera1f8ab02012-11-30 10:53:22 -08001808
Andreas Huber14f76722013-01-15 09:04:18 -08001809 if ((!audio || mAudioDecoder == NULL)
1810 && (!video || mVideoDecoder == NULL)) {
Andreas Hubera1f8ab02012-11-30 10:53:22 -08001811 return;
1812 }
1813
1814 mTimeDiscontinuityPending = true;
1815
Andreas Huber14f76722013-01-15 09:04:18 -08001816 if (audio && mAudioDecoder != NULL) {
Andreas Hubera1f8ab02012-11-30 10:53:22 -08001817 flushDecoder(true /* audio */, true /* needShutdown */);
1818 }
1819
Andreas Huber14f76722013-01-15 09:04:18 -08001820 if (video && mVideoDecoder != NULL) {
Andreas Hubera1f8ab02012-11-30 10:53:22 -08001821 flushDecoder(false /* audio */, true /* needShutdown */);
1822 }
1823}
1824
1825void NuPlayer::performReset() {
1826 ALOGV("performReset");
1827
1828 CHECK(mAudioDecoder == NULL);
1829 CHECK(mVideoDecoder == NULL);
1830
1831 cancelPollDuration();
1832
1833 ++mScanSourcesGeneration;
1834 mScanSourcesPending = false;
1835
Wei Jia1008e1c2014-09-09 14:49:08 -07001836 ++mAudioDecoderGeneration;
1837 ++mVideoDecoderGeneration;
1838
Lajos Molnar09524832014-07-17 14:29:51 -07001839 if (mRendererLooper != NULL) {
1840 if (mRenderer != NULL) {
1841 mRendererLooper->unregisterHandler(mRenderer->id());
1842 }
1843 mRendererLooper->stop();
1844 mRendererLooper.clear();
1845 }
Andreas Hubera1f8ab02012-11-30 10:53:22 -08001846 mRenderer.clear();
1847
1848 if (mSource != NULL) {
1849 mSource->stop();
Andreas Huberb5f25f02013-02-05 10:14:26 -08001850
Andreas Hubera1f8ab02012-11-30 10:53:22 -08001851 mSource.clear();
1852 }
1853
1854 if (mDriver != NULL) {
1855 sp<NuPlayerDriver> driver = mDriver.promote();
1856 if (driver != NULL) {
1857 driver->notifyResetComplete();
1858 }
1859 }
Andreas Huber57a339c2012-12-03 11:18:00 -08001860
1861 mStarted = false;
Andreas Hubera1f8ab02012-11-30 10:53:22 -08001862}
1863
1864void NuPlayer::performScanSources() {
1865 ALOGV("performScanSources");
1866
Andreas Huber57a339c2012-12-03 11:18:00 -08001867 if (!mStarted) {
1868 return;
1869 }
1870
Andreas Hubera1f8ab02012-11-30 10:53:22 -08001871 if (mAudioDecoder == NULL || mVideoDecoder == NULL) {
1872 postScanSources();
1873 }
1874}
1875
Andreas Huber57a339c2012-12-03 11:18:00 -08001876void NuPlayer::performSetSurface(const sp<NativeWindowWrapper> &wrapper) {
1877 ALOGV("performSetSurface");
1878
1879 mNativeWindow = wrapper;
1880
1881 // XXX - ignore error from setVideoScalingMode for now
1882 setVideoScalingMode(mVideoScalingMode);
Chong Zhang13d6faa2014-08-22 15:35:28 -07001883
1884 if (mDriver != NULL) {
1885 sp<NuPlayerDriver> driver = mDriver.promote();
1886 if (driver != NULL) {
1887 driver->notifySetSurfaceComplete();
1888 }
1889 }
Andreas Huber57a339c2012-12-03 11:18:00 -08001890}
1891
Andreas Huber9575c962013-02-05 13:59:56 -08001892void NuPlayer::onSourceNotify(const sp<AMessage> &msg) {
1893 int32_t what;
1894 CHECK(msg->findInt32("what", &what));
1895
1896 switch (what) {
1897 case Source::kWhatPrepared:
1898 {
Andreas Huberb5f28d42013-04-25 15:11:19 -07001899 if (mSource == NULL) {
1900 // This is a stale notification from a source that was
1901 // asynchronously preparing when the client called reset().
1902 // We handled the reset, the source is gone.
1903 break;
1904 }
1905
Andreas Huberec0c5972013-02-05 14:47:13 -08001906 int32_t err;
1907 CHECK(msg->findInt32("err", &err));
1908
Andreas Huber9575c962013-02-05 13:59:56 -08001909 sp<NuPlayerDriver> driver = mDriver.promote();
1910 if (driver != NULL) {
Marco Nelissendd114d12014-05-28 15:23:14 -07001911 // notify duration first, so that it's definitely set when
1912 // the app received the "prepare complete" callback.
1913 int64_t durationUs;
1914 if (mSource->getDuration(&durationUs) == OK) {
1915 driver->notifyDuration(durationUs);
1916 }
Andreas Huberec0c5972013-02-05 14:47:13 -08001917 driver->notifyPrepareCompleted(err);
Andreas Huber9575c962013-02-05 13:59:56 -08001918 }
Andreas Huber99759402013-04-01 14:28:31 -07001919
Andreas Huber9575c962013-02-05 13:59:56 -08001920 break;
1921 }
1922
1923 case Source::kWhatFlagsChanged:
1924 {
1925 uint32_t flags;
1926 CHECK(msg->findInt32("flags", (int32_t *)&flags));
1927
Chong Zhang4b7069d2013-09-11 12:52:43 -07001928 sp<NuPlayerDriver> driver = mDriver.promote();
1929 if (driver != NULL) {
1930 driver->notifyFlagsChanged(flags);
1931 }
1932
Andreas Huber9575c962013-02-05 13:59:56 -08001933 if ((mSourceFlags & Source::FLAG_DYNAMIC_DURATION)
1934 && (!(flags & Source::FLAG_DYNAMIC_DURATION))) {
1935 cancelPollDuration();
1936 } else if (!(mSourceFlags & Source::FLAG_DYNAMIC_DURATION)
1937 && (flags & Source::FLAG_DYNAMIC_DURATION)
1938 && (mAudioDecoder != NULL || mVideoDecoder != NULL)) {
1939 schedulePollDuration();
1940 }
1941
1942 mSourceFlags = flags;
1943 break;
1944 }
1945
1946 case Source::kWhatVideoSizeChanged:
1947 {
Chong Zhangced1c2f2014-08-08 15:22:35 -07001948 sp<AMessage> format;
1949 CHECK(msg->findMessage("format", &format));
Andreas Huber9575c962013-02-05 13:59:56 -08001950
Chong Zhangced1c2f2014-08-08 15:22:35 -07001951 updateVideoSize(format);
Andreas Huber9575c962013-02-05 13:59:56 -08001952 break;
1953 }
1954
Chong Zhang2a3cc9a2014-08-21 17:48:26 -07001955 case Source::kWhatBufferingUpdate:
1956 {
1957 int32_t percentage;
1958 CHECK(msg->findInt32("percentage", &percentage));
1959
1960 notifyListener(MEDIA_BUFFERING_UPDATE, percentage, 0);
1961 break;
1962 }
1963
Roger Jönssonb50e83e2013-01-21 16:26:41 +01001964 case Source::kWhatBufferingStart:
1965 {
1966 notifyListener(MEDIA_INFO, MEDIA_INFO_BUFFERING_START, 0);
1967 break;
1968 }
1969
1970 case Source::kWhatBufferingEnd:
1971 {
1972 notifyListener(MEDIA_INFO, MEDIA_INFO_BUFFERING_END, 0);
1973 break;
1974 }
1975
Chong Zhangdcb89b32013-08-06 09:44:47 -07001976 case Source::kWhatSubtitleData:
1977 {
1978 sp<ABuffer> buffer;
1979 CHECK(msg->findBuffer("buffer", &buffer));
1980
Chong Zhang404fced2014-06-11 14:45:31 -07001981 sendSubtitleData(buffer, 0 /* baseIndex */);
Chong Zhangdcb89b32013-08-06 09:44:47 -07001982 break;
1983 }
1984
Robert Shihd3b0bbb2014-07-23 15:00:25 -07001985 case Source::kWhatTimedTextData:
1986 {
1987 int32_t generation;
1988 if (msg->findInt32("generation", &generation)
1989 && generation != mTimedTextGeneration) {
1990 break;
1991 }
1992
1993 sp<ABuffer> buffer;
1994 CHECK(msg->findBuffer("buffer", &buffer));
1995
1996 sp<NuPlayerDriver> driver = mDriver.promote();
1997 if (driver == NULL) {
1998 break;
1999 }
2000
2001 int posMs;
2002 int64_t timeUs, posUs;
2003 driver->getCurrentPosition(&posMs);
2004 posUs = posMs * 1000;
2005 CHECK(buffer->meta()->findInt64("timeUs", &timeUs));
2006
2007 if (posUs < timeUs) {
2008 if (!msg->findInt32("generation", &generation)) {
2009 msg->setInt32("generation", mTimedTextGeneration);
2010 }
2011 msg->post(timeUs - posUs);
2012 } else {
2013 sendTimedTextData(buffer);
2014 }
2015 break;
2016 }
2017
Andreas Huber14f76722013-01-15 09:04:18 -08002018 case Source::kWhatQueueDecoderShutdown:
2019 {
2020 int32_t audio, video;
2021 CHECK(msg->findInt32("audio", &audio));
2022 CHECK(msg->findInt32("video", &video));
2023
2024 sp<AMessage> reply;
2025 CHECK(msg->findMessage("reply", &reply));
2026
2027 queueDecoderShutdown(audio, video, reply);
2028 break;
2029 }
2030
Ronghua Wu80276872014-08-28 15:50:29 -07002031 case Source::kWhatDrmNoLicense:
2032 {
2033 notifyListener(MEDIA_ERROR, MEDIA_ERROR_UNKNOWN, ERROR_DRM_NO_LICENSE);
2034 break;
2035 }
2036
Andreas Huber9575c962013-02-05 13:59:56 -08002037 default:
2038 TRESPASS();
2039 }
2040}
2041
Chong Zhanga7fa1d92014-06-11 14:49:23 -07002042void NuPlayer::onClosedCaptionNotify(const sp<AMessage> &msg) {
2043 int32_t what;
2044 CHECK(msg->findInt32("what", &what));
2045
2046 switch (what) {
2047 case NuPlayer::CCDecoder::kWhatClosedCaptionData:
2048 {
2049 sp<ABuffer> buffer;
2050 CHECK(msg->findBuffer("buffer", &buffer));
2051
2052 size_t inbandTracks = 0;
2053 if (mSource != NULL) {
2054 inbandTracks = mSource->getTrackCount();
2055 }
2056
2057 sendSubtitleData(buffer, inbandTracks);
2058 break;
2059 }
2060
2061 case NuPlayer::CCDecoder::kWhatTrackAdded:
2062 {
2063 notifyListener(MEDIA_INFO, MEDIA_INFO_METADATA_UPDATE, 0);
2064
2065 break;
2066 }
2067
2068 default:
2069 TRESPASS();
2070 }
2071
2072
2073}
2074
Chong Zhang404fced2014-06-11 14:45:31 -07002075void NuPlayer::sendSubtitleData(const sp<ABuffer> &buffer, int32_t baseIndex) {
2076 int32_t trackIndex;
2077 int64_t timeUs, durationUs;
2078 CHECK(buffer->meta()->findInt32("trackIndex", &trackIndex));
2079 CHECK(buffer->meta()->findInt64("timeUs", &timeUs));
2080 CHECK(buffer->meta()->findInt64("durationUs", &durationUs));
2081
2082 Parcel in;
2083 in.writeInt32(trackIndex + baseIndex);
2084 in.writeInt64(timeUs);
2085 in.writeInt64(durationUs);
2086 in.writeInt32(buffer->size());
2087 in.writeInt32(buffer->size());
2088 in.write(buffer->data(), buffer->size());
2089
2090 notifyListener(MEDIA_SUBTITLE_DATA, 0, 0, &in);
2091}
Robert Shihd3b0bbb2014-07-23 15:00:25 -07002092
2093void NuPlayer::sendTimedTextData(const sp<ABuffer> &buffer) {
2094 const void *data;
2095 size_t size = 0;
2096 int64_t timeUs;
2097 int32_t flag = TextDescriptions::LOCAL_DESCRIPTIONS;
2098
2099 AString mime;
2100 CHECK(buffer->meta()->findString("mime", &mime));
2101 CHECK(strcasecmp(mime.c_str(), MEDIA_MIMETYPE_TEXT_3GPP) == 0);
2102
2103 data = buffer->data();
2104 size = buffer->size();
2105
2106 Parcel parcel;
2107 if (size > 0) {
2108 CHECK(buffer->meta()->findInt64("timeUs", &timeUs));
2109 flag |= TextDescriptions::IN_BAND_TEXT_3GPP;
2110 TextDescriptions::getParcelOfDescriptions(
2111 (const uint8_t *)data, size, flag, timeUs / 1000, &parcel);
2112 }
2113
2114 if ((parcel.dataSize() > 0)) {
2115 notifyListener(MEDIA_TIMED_TEXT, 0, 0, &parcel);
2116 } else { // send an empty timed text
2117 notifyListener(MEDIA_TIMED_TEXT, 0, 0);
2118 }
2119}
Andreas Huberb5f25f02013-02-05 10:14:26 -08002120////////////////////////////////////////////////////////////////////////////////
2121
Chong Zhangced1c2f2014-08-08 15:22:35 -07002122sp<AMessage> NuPlayer::Source::getFormat(bool audio) {
2123 sp<MetaData> meta = getFormatMeta(audio);
2124
2125 if (meta == NULL) {
2126 return NULL;
2127 }
2128
2129 sp<AMessage> msg = new AMessage;
2130
2131 if(convertMetaDataToMessage(meta, &msg) == OK) {
2132 return msg;
2133 }
2134 return NULL;
2135}
2136
Andreas Huber9575c962013-02-05 13:59:56 -08002137void NuPlayer::Source::notifyFlagsChanged(uint32_t flags) {
2138 sp<AMessage> notify = dupNotify();
2139 notify->setInt32("what", kWhatFlagsChanged);
2140 notify->setInt32("flags", flags);
2141 notify->post();
2142}
2143
Chong Zhangced1c2f2014-08-08 15:22:35 -07002144void NuPlayer::Source::notifyVideoSizeChanged(const sp<AMessage> &format) {
Andreas Huber9575c962013-02-05 13:59:56 -08002145 sp<AMessage> notify = dupNotify();
2146 notify->setInt32("what", kWhatVideoSizeChanged);
Chong Zhangced1c2f2014-08-08 15:22:35 -07002147 notify->setMessage("format", format);
Andreas Huber9575c962013-02-05 13:59:56 -08002148 notify->post();
2149}
2150
Andreas Huberec0c5972013-02-05 14:47:13 -08002151void NuPlayer::Source::notifyPrepared(status_t err) {
Andreas Huber9575c962013-02-05 13:59:56 -08002152 sp<AMessage> notify = dupNotify();
2153 notify->setInt32("what", kWhatPrepared);
Andreas Huberec0c5972013-02-05 14:47:13 -08002154 notify->setInt32("err", err);
Andreas Huber9575c962013-02-05 13:59:56 -08002155 notify->post();
2156}
2157
Andreas Huber84333e02014-02-07 15:36:10 -08002158void NuPlayer::Source::onMessageReceived(const sp<AMessage> & /* msg */) {
Andreas Huberb5f25f02013-02-05 10:14:26 -08002159 TRESPASS();
2160}
2161
Andreas Huberf9334412010-12-15 15:17:42 -08002162} // namespace android