blob: 47bd9898ca136861113c0d413352252623dd7fae [file] [log] [blame]
Andreas Huberf9334412010-12-15 15:17:42 -08001/*
2 * Copyright (C) 2010 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17//#define LOG_NDEBUG 0
18#define LOG_TAG "NuPlayer"
19#include <utils/Log.h>
20
21#include "NuPlayer.h"
Andreas Huber5bc087c2010-12-23 10:27:40 -080022
23#include "HTTPLiveSource.h"
Andreas Huberf9334412010-12-15 15:17:42 -080024#include "NuPlayerDecoder.h"
Wei Jiabc2fb722014-07-08 16:37:57 -070025#include "NuPlayerDecoderPassThrough.h"
Andreas Huber43c3e6c2011-01-05 12:17:08 -080026#include "NuPlayerDriver.h"
Andreas Huberf9334412010-12-15 15:17:42 -080027#include "NuPlayerRenderer.h"
Andreas Huber5bc087c2010-12-23 10:27:40 -080028#include "NuPlayerSource.h"
Andreas Huber2bfdd422011-10-11 15:24:07 -070029#include "RTSPSource.h"
Andreas Huber5bc087c2010-12-23 10:27:40 -080030#include "StreamingSource.h"
Andreas Huberafed0e12011-09-20 15:39:58 -070031#include "GenericSource.h"
Robert Shihd3b0bbb2014-07-23 15:00:25 -070032#include "TextDescriptions.h"
Andreas Huber5bc087c2010-12-23 10:27:40 -080033
34#include "ATSParser.h"
Andreas Huberf9334412010-12-15 15:17:42 -080035
Andreas Huber3831a062010-12-21 10:22:33 -080036#include <media/stagefright/foundation/hexdump.h>
Andreas Huberf9334412010-12-15 15:17:42 -080037#include <media/stagefright/foundation/ABuffer.h>
38#include <media/stagefright/foundation/ADebug.h>
39#include <media/stagefright/foundation/AMessage.h>
Lajos Molnar09524832014-07-17 14:29:51 -070040#include <media/stagefright/MediaBuffer.h>
Andreas Huber3fe62152011-09-16 15:09:22 -070041#include <media/stagefright/MediaDefs.h>
Andreas Huberf9334412010-12-15 15:17:42 -080042#include <media/stagefright/MediaErrors.h>
43#include <media/stagefright/MetaData.h>
Andy McFadden8ba01022012-12-18 09:46:54 -080044#include <gui/IGraphicBufferProducer.h>
Andreas Huberf9334412010-12-15 15:17:42 -080045
Andreas Huber3fe62152011-09-16 15:09:22 -070046#include "avc_utils.h"
47
Andreas Huber84066782011-08-16 09:34:26 -070048#include "ESDS.h"
49#include <media/stagefright/Utils.h>
50
Andreas Huberf9334412010-12-15 15:17:42 -080051namespace android {
52
Phil Burkc5cc2e22014-09-09 20:08:39 -070053// TODO optimize buffer size for power consumption
54// The offload read buffer size is 32 KB but 24 KB uses less power.
55const size_t NuPlayer::kAggregateBufferSizeBytes = 24 * 1024;
56
Andreas Hubera1f8ab02012-11-30 10:53:22 -080057struct NuPlayer::Action : public RefBase {
58 Action() {}
59
60 virtual void execute(NuPlayer *player) = 0;
61
62private:
63 DISALLOW_EVIL_CONSTRUCTORS(Action);
64};
65
66struct NuPlayer::SeekAction : public Action {
Wei Jiae427abf2014-09-22 15:21:11 -070067 SeekAction(int64_t seekTimeUs, bool needNotify)
68 : mSeekTimeUs(seekTimeUs),
69 mNeedNotify(needNotify) {
Andreas Hubera1f8ab02012-11-30 10:53:22 -080070 }
71
72 virtual void execute(NuPlayer *player) {
Wei Jiae427abf2014-09-22 15:21:11 -070073 player->performSeek(mSeekTimeUs, mNeedNotify);
Andreas Hubera1f8ab02012-11-30 10:53:22 -080074 }
75
76private:
77 int64_t mSeekTimeUs;
Wei Jiae427abf2014-09-22 15:21:11 -070078 bool mNeedNotify;
Andreas Hubera1f8ab02012-11-30 10:53:22 -080079
80 DISALLOW_EVIL_CONSTRUCTORS(SeekAction);
81};
82
Andreas Huber57a339c2012-12-03 11:18:00 -080083struct NuPlayer::SetSurfaceAction : public Action {
84 SetSurfaceAction(const sp<NativeWindowWrapper> &wrapper)
85 : mWrapper(wrapper) {
86 }
87
88 virtual void execute(NuPlayer *player) {
89 player->performSetSurface(mWrapper);
90 }
91
92private:
93 sp<NativeWindowWrapper> mWrapper;
94
95 DISALLOW_EVIL_CONSTRUCTORS(SetSurfaceAction);
96};
97
Wei Jiafef808d2014-10-31 17:57:05 -070098struct NuPlayer::FlushDecoderAction : public Action {
99 FlushDecoderAction(FlushCommand audio, FlushCommand video)
Andreas Huber14f76722013-01-15 09:04:18 -0800100 : mAudio(audio),
101 mVideo(video) {
102 }
103
104 virtual void execute(NuPlayer *player) {
Wei Jiafef808d2014-10-31 17:57:05 -0700105 player->performDecoderFlush(mAudio, mVideo);
Andreas Huber14f76722013-01-15 09:04:18 -0800106 }
107
108private:
Wei Jiafef808d2014-10-31 17:57:05 -0700109 FlushCommand mAudio;
110 FlushCommand mVideo;
Andreas Huber14f76722013-01-15 09:04:18 -0800111
Wei Jiafef808d2014-10-31 17:57:05 -0700112 DISALLOW_EVIL_CONSTRUCTORS(FlushDecoderAction);
Andreas Huber14f76722013-01-15 09:04:18 -0800113};
114
115struct NuPlayer::PostMessageAction : public Action {
116 PostMessageAction(const sp<AMessage> &msg)
117 : mMessage(msg) {
118 }
119
120 virtual void execute(NuPlayer *) {
121 mMessage->post();
122 }
123
124private:
125 sp<AMessage> mMessage;
126
127 DISALLOW_EVIL_CONSTRUCTORS(PostMessageAction);
128};
129
Andreas Hubera1f8ab02012-11-30 10:53:22 -0800130// Use this if there's no state necessary to save in order to execute
131// the action.
132struct NuPlayer::SimpleAction : public Action {
133 typedef void (NuPlayer::*ActionFunc)();
134
135 SimpleAction(ActionFunc func)
136 : mFunc(func) {
137 }
138
139 virtual void execute(NuPlayer *player) {
140 (player->*mFunc)();
141 }
142
143private:
144 ActionFunc mFunc;
145
146 DISALLOW_EVIL_CONSTRUCTORS(SimpleAction);
147};
148
Andreas Huberf9334412010-12-15 15:17:42 -0800149////////////////////////////////////////////////////////////////////////////////
150
151NuPlayer::NuPlayer()
Andreas Huber9b80c2b2011-06-30 15:47:02 -0700152 : mUIDValid(false),
Andreas Huber9575c962013-02-05 13:59:56 -0800153 mSourceFlags(0),
Andreas Huber3fe62152011-09-16 15:09:22 -0700154 mVideoIsAVC(false),
Wei Jiabc2fb722014-07-08 16:37:57 -0700155 mOffloadAudio(false),
Wei Jia88703c32014-08-06 11:24:07 -0700156 mAudioDecoderGeneration(0),
157 mVideoDecoderGeneration(0),
Wei Jia57568df2014-09-22 10:16:29 -0700158 mRendererGeneration(0),
Andreas Huber9b80c2b2011-06-30 15:47:02 -0700159 mAudioEOS(false),
Andreas Huberf9334412010-12-15 15:17:42 -0800160 mVideoEOS(false),
Andreas Huber5bc087c2010-12-23 10:27:40 -0800161 mScanSourcesPending(false),
Andreas Huber1aef2112011-01-04 14:01:29 -0800162 mScanSourcesGeneration(0),
Andreas Huberb7c8e912012-11-27 15:02:53 -0800163 mPollDurationGeneration(0),
Robert Shihd3b0bbb2014-07-23 15:00:25 -0700164 mTimedTextGeneration(0),
Andreas Huber6e3d3112011-11-28 12:36:11 -0800165 mTimeDiscontinuityPending(false),
Andreas Huberf9334412010-12-15 15:17:42 -0800166 mFlushingAudio(NONE),
Andreas Huber1aef2112011-01-04 14:01:29 -0800167 mFlushingVideo(NONE),
Andreas Huber3fe62152011-09-16 15:09:22 -0700168 mSkipRenderingAudioUntilMediaTimeUs(-1ll),
169 mSkipRenderingVideoUntilMediaTimeUs(-1ll),
Andreas Huber3fe62152011-09-16 15:09:22 -0700170 mNumFramesTotal(0ll),
James Dong0d268a32012-08-31 12:18:27 -0700171 mNumFramesDropped(0ll),
Andreas Huber57a339c2012-12-03 11:18:00 -0800172 mVideoScalingMode(NATIVE_WINDOW_SCALING_MODE_SCALE_TO_WINDOW),
173 mStarted(false) {
Andy Hung8d121d42014-10-03 09:53:53 -0700174 clearFlushComplete();
Andreas Huberf9334412010-12-15 15:17:42 -0800175}
176
177NuPlayer::~NuPlayer() {
178}
179
Andreas Huber9b80c2b2011-06-30 15:47:02 -0700180void NuPlayer::setUID(uid_t uid) {
181 mUIDValid = true;
182 mUID = uid;
183}
184
Andreas Huber43c3e6c2011-01-05 12:17:08 -0800185void NuPlayer::setDriver(const wp<NuPlayerDriver> &driver) {
186 mDriver = driver;
Andreas Huberf9334412010-12-15 15:17:42 -0800187}
188
Andreas Huber9575c962013-02-05 13:59:56 -0800189void NuPlayer::setDataSourceAsync(const sp<IStreamSource> &source) {
Andreas Huberf9334412010-12-15 15:17:42 -0800190 sp<AMessage> msg = new AMessage(kWhatSetDataSource, id());
191
Andreas Huberb5f25f02013-02-05 10:14:26 -0800192 sp<AMessage> notify = new AMessage(kWhatSourceNotify, id());
193
Andreas Huber240abcc2014-02-13 13:32:37 -0800194 msg->setObject("source", new StreamingSource(notify, source));
Andreas Huber5bc087c2010-12-23 10:27:40 -0800195 msg->post();
196}
Andreas Huberf9334412010-12-15 15:17:42 -0800197
Andreas Huberafed0e12011-09-20 15:39:58 -0700198static bool IsHTTPLiveURL(const char *url) {
199 if (!strncasecmp("http://", url, 7)
Andreas Huber99759402013-04-01 14:28:31 -0700200 || !strncasecmp("https://", url, 8)
201 || !strncasecmp("file://", url, 7)) {
Andreas Huberafed0e12011-09-20 15:39:58 -0700202 size_t len = strlen(url);
203 if (len >= 5 && !strcasecmp(".m3u8", &url[len - 5])) {
204 return true;
205 }
206
207 if (strstr(url,"m3u8")) {
208 return true;
209 }
210 }
211
212 return false;
213}
214
Andreas Huber9575c962013-02-05 13:59:56 -0800215void NuPlayer::setDataSourceAsync(
Andreas Huber1b86fe02014-01-29 11:13:26 -0800216 const sp<IMediaHTTPService> &httpService,
217 const char *url,
218 const KeyedVector<String8, String8> *headers) {
Chong Zhang3de157d2014-08-05 20:54:44 -0700219
Andreas Huber5bc087c2010-12-23 10:27:40 -0800220 sp<AMessage> msg = new AMessage(kWhatSetDataSource, id());
Oscar Rydhé7a33b772012-02-20 10:15:48 +0100221 size_t len = strlen(url);
Andreas Huber5bc087c2010-12-23 10:27:40 -0800222
Andreas Huberb5f25f02013-02-05 10:14:26 -0800223 sp<AMessage> notify = new AMessage(kWhatSourceNotify, id());
224
Andreas Huberafed0e12011-09-20 15:39:58 -0700225 sp<Source> source;
226 if (IsHTTPLiveURL(url)) {
Andreas Huber81e68442014-02-05 11:52:33 -0800227 source = new HTTPLiveSource(notify, httpService, url, headers);
Andreas Huberafed0e12011-09-20 15:39:58 -0700228 } else if (!strncasecmp(url, "rtsp://", 7)) {
Andreas Huber1b86fe02014-01-29 11:13:26 -0800229 source = new RTSPSource(
230 notify, httpService, url, headers, mUIDValid, mUID);
Oscar Rydhé7a33b772012-02-20 10:15:48 +0100231 } else if ((!strncasecmp(url, "http://", 7)
232 || !strncasecmp(url, "https://", 8))
233 && ((len >= 4 && !strcasecmp(".sdp", &url[len - 4]))
234 || strstr(url, ".sdp?"))) {
Andreas Huber1b86fe02014-01-29 11:13:26 -0800235 source = new RTSPSource(
236 notify, httpService, url, headers, mUIDValid, mUID, true);
Andreas Huber2bfdd422011-10-11 15:24:07 -0700237 } else {
Chong Zhang3de157d2014-08-05 20:54:44 -0700238 sp<GenericSource> genericSource =
239 new GenericSource(notify, mUIDValid, mUID);
240 // Don't set FLAG_SECURE on mSourceFlags here for widevine.
241 // The correct flags will be updated in Source::kWhatFlagsChanged
242 // handler when GenericSource is prepared.
Andreas Huber2bfdd422011-10-11 15:24:07 -0700243
Chong Zhanga19f33e2014-08-07 15:35:07 -0700244 status_t err = genericSource->setDataSource(httpService, url, headers);
Chong Zhang3de157d2014-08-05 20:54:44 -0700245
246 if (err == OK) {
247 source = genericSource;
248 } else {
Chong Zhanga19f33e2014-08-07 15:35:07 -0700249 ALOGE("Failed to set data source!");
Chong Zhang3de157d2014-08-05 20:54:44 -0700250 }
251 }
Andreas Huberafed0e12011-09-20 15:39:58 -0700252 msg->setObject("source", source);
253 msg->post();
254}
255
Andreas Huber9575c962013-02-05 13:59:56 -0800256void NuPlayer::setDataSourceAsync(int fd, int64_t offset, int64_t length) {
Andreas Huberafed0e12011-09-20 15:39:58 -0700257 sp<AMessage> msg = new AMessage(kWhatSetDataSource, id());
258
Andreas Huberb5f25f02013-02-05 10:14:26 -0800259 sp<AMessage> notify = new AMessage(kWhatSourceNotify, id());
260
Chong Zhang3de157d2014-08-05 20:54:44 -0700261 sp<GenericSource> source =
262 new GenericSource(notify, mUIDValid, mUID);
263
Chong Zhanga19f33e2014-08-07 15:35:07 -0700264 status_t err = source->setDataSource(fd, offset, length);
Chong Zhang3de157d2014-08-05 20:54:44 -0700265
266 if (err != OK) {
Chong Zhanga19f33e2014-08-07 15:35:07 -0700267 ALOGE("Failed to set data source!");
Chong Zhang3de157d2014-08-05 20:54:44 -0700268 source = NULL;
269 }
270
Andreas Huberafed0e12011-09-20 15:39:58 -0700271 msg->setObject("source", source);
Andreas Huberf9334412010-12-15 15:17:42 -0800272 msg->post();
273}
274
Andreas Huber9575c962013-02-05 13:59:56 -0800275void NuPlayer::prepareAsync() {
276 (new AMessage(kWhatPrepare, id()))->post();
277}
278
Andreas Huber57a339c2012-12-03 11:18:00 -0800279void NuPlayer::setVideoSurfaceTextureAsync(
Andy McFadden8ba01022012-12-18 09:46:54 -0800280 const sp<IGraphicBufferProducer> &bufferProducer) {
Glenn Kasten11731182011-02-08 17:26:17 -0800281 sp<AMessage> msg = new AMessage(kWhatSetVideoNativeWindow, id());
Andreas Huber57a339c2012-12-03 11:18:00 -0800282
Andy McFadden8ba01022012-12-18 09:46:54 -0800283 if (bufferProducer == NULL) {
Andreas Huber57a339c2012-12-03 11:18:00 -0800284 msg->setObject("native-window", NULL);
285 } else {
286 msg->setObject(
287 "native-window",
288 new NativeWindowWrapper(
Wei Jia9c03a402014-08-26 15:24:43 -0700289 new Surface(bufferProducer, true /* controlledByApp */)));
Andreas Huber57a339c2012-12-03 11:18:00 -0800290 }
291
Andreas Huberf9334412010-12-15 15:17:42 -0800292 msg->post();
293}
294
295void NuPlayer::setAudioSink(const sp<MediaPlayerBase::AudioSink> &sink) {
296 sp<AMessage> msg = new AMessage(kWhatSetAudioSink, id());
297 msg->setObject("sink", sink);
298 msg->post();
299}
300
301void NuPlayer::start() {
302 (new AMessage(kWhatStart, id()))->post();
303}
304
Andreas Huber43c3e6c2011-01-05 12:17:08 -0800305void NuPlayer::pause() {
Andreas Huberb4082222011-01-20 15:23:04 -0800306 (new AMessage(kWhatPause, id()))->post();
Andreas Huber43c3e6c2011-01-05 12:17:08 -0800307}
308
Andreas Huber1aef2112011-01-04 14:01:29 -0800309void NuPlayer::resetAsync() {
Chong Zhang48296b72014-09-14 14:28:45 -0700310 if (mSource != NULL) {
311 // During a reset, the data source might be unresponsive already, we need to
312 // disconnect explicitly so that reads exit promptly.
313 // We can't queue the disconnect request to the looper, as it might be
314 // queued behind a stuck read and never gets processed.
315 // Doing a disconnect outside the looper to allows the pending reads to exit
316 // (either successfully or with error).
317 mSource->disconnect();
318 }
319
Andreas Huber1aef2112011-01-04 14:01:29 -0800320 (new AMessage(kWhatReset, id()))->post();
321}
322
Wei Jiae427abf2014-09-22 15:21:11 -0700323void NuPlayer::seekToAsync(int64_t seekTimeUs, bool needNotify) {
Andreas Huber43c3e6c2011-01-05 12:17:08 -0800324 sp<AMessage> msg = new AMessage(kWhatSeek, id());
325 msg->setInt64("seekTimeUs", seekTimeUs);
Wei Jiae427abf2014-09-22 15:21:11 -0700326 msg->setInt32("needNotify", needNotify);
Andreas Huber43c3e6c2011-01-05 12:17:08 -0800327 msg->post();
328}
329
Andreas Huber53df1a42010-12-22 10:03:04 -0800330
Chong Zhang404fced2014-06-11 14:45:31 -0700331void NuPlayer::writeTrackInfo(
332 Parcel* reply, const sp<AMessage> format) const {
333 int32_t trackType;
334 CHECK(format->findInt32("type", &trackType));
335
336 AString lang;
337 CHECK(format->findString("language", &lang));
338
339 reply->writeInt32(2); // write something non-zero
340 reply->writeInt32(trackType);
341 reply->writeString16(String16(lang.c_str()));
342
343 if (trackType == MEDIA_TRACK_TYPE_SUBTITLE) {
344 AString mime;
345 CHECK(format->findString("mime", &mime));
346
347 int32_t isAuto, isDefault, isForced;
348 CHECK(format->findInt32("auto", &isAuto));
349 CHECK(format->findInt32("default", &isDefault));
350 CHECK(format->findInt32("forced", &isForced));
351
352 reply->writeString16(String16(mime.c_str()));
353 reply->writeInt32(isAuto);
354 reply->writeInt32(isDefault);
355 reply->writeInt32(isForced);
356 }
357}
358
Andreas Huberf9334412010-12-15 15:17:42 -0800359void NuPlayer::onMessageReceived(const sp<AMessage> &msg) {
360 switch (msg->what()) {
361 case kWhatSetDataSource:
362 {
Steve Block3856b092011-10-20 11:56:00 +0100363 ALOGV("kWhatSetDataSource");
Andreas Huberf9334412010-12-15 15:17:42 -0800364
365 CHECK(mSource == NULL);
366
Chong Zhang3de157d2014-08-05 20:54:44 -0700367 status_t err = OK;
Andreas Huber5bc087c2010-12-23 10:27:40 -0800368 sp<RefBase> obj;
369 CHECK(msg->findObject("source", &obj));
Chong Zhang3de157d2014-08-05 20:54:44 -0700370 if (obj != NULL) {
371 mSource = static_cast<Source *>(obj.get());
Chong Zhang3de157d2014-08-05 20:54:44 -0700372 } else {
373 err = UNKNOWN_ERROR;
374 }
Andreas Huber9575c962013-02-05 13:59:56 -0800375
376 CHECK(mDriver != NULL);
377 sp<NuPlayerDriver> driver = mDriver.promote();
378 if (driver != NULL) {
Chong Zhang3de157d2014-08-05 20:54:44 -0700379 driver->notifySetDataSourceCompleted(err);
Andreas Huber9575c962013-02-05 13:59:56 -0800380 }
381 break;
382 }
383
384 case kWhatPrepare:
385 {
386 mSource->prepareAsync();
Andreas Huberf9334412010-12-15 15:17:42 -0800387 break;
388 }
389
Chong Zhangdcb89b32013-08-06 09:44:47 -0700390 case kWhatGetTrackInfo:
391 {
392 uint32_t replyID;
393 CHECK(msg->senderAwaitsResponse(&replyID));
394
Chong Zhang404fced2014-06-11 14:45:31 -0700395 Parcel* reply;
396 CHECK(msg->findPointer("reply", (void**)&reply));
397
398 size_t inbandTracks = 0;
Chong Zhangdcb89b32013-08-06 09:44:47 -0700399 if (mSource != NULL) {
Chong Zhang404fced2014-06-11 14:45:31 -0700400 inbandTracks = mSource->getTrackCount();
401 }
402
Chong Zhanga7fa1d92014-06-11 14:49:23 -0700403 size_t ccTracks = 0;
404 if (mCCDecoder != NULL) {
405 ccTracks = mCCDecoder->getTrackCount();
406 }
407
Chong Zhang404fced2014-06-11 14:45:31 -0700408 // total track count
Chong Zhanga7fa1d92014-06-11 14:49:23 -0700409 reply->writeInt32(inbandTracks + ccTracks);
Chong Zhang404fced2014-06-11 14:45:31 -0700410
411 // write inband tracks
412 for (size_t i = 0; i < inbandTracks; ++i) {
413 writeTrackInfo(reply, mSource->getTrackInfo(i));
Chong Zhangdcb89b32013-08-06 09:44:47 -0700414 }
415
Chong Zhanga7fa1d92014-06-11 14:49:23 -0700416 // write CC track
417 for (size_t i = 0; i < ccTracks; ++i) {
418 writeTrackInfo(reply, mCCDecoder->getTrackInfo(i));
419 }
420
Chong Zhangdcb89b32013-08-06 09:44:47 -0700421 sp<AMessage> response = new AMessage;
Chong Zhangdcb89b32013-08-06 09:44:47 -0700422 response->postReply(replyID);
423 break;
424 }
425
Robert Shih7c4f0d72014-07-09 18:53:31 -0700426 case kWhatGetSelectedTrack:
427 {
428 status_t err = INVALID_OPERATION;
429 if (mSource != NULL) {
430 err = OK;
431
432 int32_t type32;
433 CHECK(msg->findInt32("type", (int32_t*)&type32));
434 media_track_type type = (media_track_type)type32;
435 ssize_t selectedTrack = mSource->getSelectedTrack(type);
436
437 Parcel* reply;
438 CHECK(msg->findPointer("reply", (void**)&reply));
439 reply->writeInt32(selectedTrack);
440 }
441
442 sp<AMessage> response = new AMessage;
443 response->setInt32("err", err);
444
445 uint32_t replyID;
446 CHECK(msg->senderAwaitsResponse(&replyID));
447 response->postReply(replyID);
448 break;
449 }
450
Chong Zhangdcb89b32013-08-06 09:44:47 -0700451 case kWhatSelectTrack:
452 {
453 uint32_t replyID;
454 CHECK(msg->senderAwaitsResponse(&replyID));
455
Chong Zhang404fced2014-06-11 14:45:31 -0700456 size_t trackIndex;
457 int32_t select;
458 CHECK(msg->findSize("trackIndex", &trackIndex));
459 CHECK(msg->findInt32("select", &select));
460
Chong Zhangdcb89b32013-08-06 09:44:47 -0700461 status_t err = INVALID_OPERATION;
Chong Zhang404fced2014-06-11 14:45:31 -0700462
463 size_t inbandTracks = 0;
Chong Zhangdcb89b32013-08-06 09:44:47 -0700464 if (mSource != NULL) {
Chong Zhang404fced2014-06-11 14:45:31 -0700465 inbandTracks = mSource->getTrackCount();
466 }
Chong Zhanga7fa1d92014-06-11 14:49:23 -0700467 size_t ccTracks = 0;
468 if (mCCDecoder != NULL) {
469 ccTracks = mCCDecoder->getTrackCount();
470 }
Chong Zhang404fced2014-06-11 14:45:31 -0700471
472 if (trackIndex < inbandTracks) {
Chong Zhangdcb89b32013-08-06 09:44:47 -0700473 err = mSource->selectTrack(trackIndex, select);
Robert Shihd3b0bbb2014-07-23 15:00:25 -0700474
475 if (!select && err == OK) {
476 int32_t type;
477 sp<AMessage> info = mSource->getTrackInfo(trackIndex);
478 if (info != NULL
479 && info->findInt32("type", &type)
480 && type == MEDIA_TRACK_TYPE_TIMEDTEXT) {
481 ++mTimedTextGeneration;
482 }
483 }
Chong Zhanga7fa1d92014-06-11 14:49:23 -0700484 } else {
485 trackIndex -= inbandTracks;
486
487 if (trackIndex < ccTracks) {
488 err = mCCDecoder->selectTrack(trackIndex, select);
489 }
Chong Zhangdcb89b32013-08-06 09:44:47 -0700490 }
491
492 sp<AMessage> response = new AMessage;
493 response->setInt32("err", err);
494
495 response->postReply(replyID);
496 break;
497 }
498
Andreas Huberb7c8e912012-11-27 15:02:53 -0800499 case kWhatPollDuration:
500 {
501 int32_t generation;
502 CHECK(msg->findInt32("generation", &generation));
503
504 if (generation != mPollDurationGeneration) {
505 // stale
506 break;
507 }
508
509 int64_t durationUs;
510 if (mDriver != NULL && mSource->getDuration(&durationUs) == OK) {
511 sp<NuPlayerDriver> driver = mDriver.promote();
512 if (driver != NULL) {
513 driver->notifyDuration(durationUs);
514 }
515 }
516
517 msg->post(1000000ll); // poll again in a second.
518 break;
519 }
520
Glenn Kasten11731182011-02-08 17:26:17 -0800521 case kWhatSetVideoNativeWindow:
Andreas Huberf9334412010-12-15 15:17:42 -0800522 {
Steve Block3856b092011-10-20 11:56:00 +0100523 ALOGV("kWhatSetVideoNativeWindow");
Andreas Huberf9334412010-12-15 15:17:42 -0800524
525 sp<RefBase> obj;
Glenn Kasten11731182011-02-08 17:26:17 -0800526 CHECK(msg->findObject("native-window", &obj));
Andreas Huberf9334412010-12-15 15:17:42 -0800527
Wei Jiafef808d2014-10-31 17:57:05 -0700528 if (mSource->getFormat(false /* audio */) == NULL) {
529 performSetSurface(static_cast<NativeWindowWrapper *>(obj.get()));
530 break;
531 }
532
533 mDeferredActions.push_back(
534 new FlushDecoderAction(FLUSH_CMD_FLUSH /* audio */,
535 FLUSH_CMD_SHUTDOWN /* video */));
536
Andreas Huber57a339c2012-12-03 11:18:00 -0800537 mDeferredActions.push_back(
538 new SetSurfaceAction(
539 static_cast<NativeWindowWrapper *>(obj.get())));
James Dong0d268a32012-08-31 12:18:27 -0700540
Andreas Huber57a339c2012-12-03 11:18:00 -0800541 if (obj != NULL) {
Wei Jiafef808d2014-10-31 17:57:05 -0700542 if (mStarted) {
Andy Hung73535852014-09-05 11:42:58 -0700543 // Issue a seek to refresh the video screen only if started otherwise
544 // the extractor may not yet be started and will assert.
545 // If the video decoder is not set (perhaps audio only in this case)
546 // do not perform a seek as it is not needed.
Ronghua Wua73d9e02014-10-08 15:13:29 -0700547 int64_t currentPositionUs = 0;
548 if (getCurrentPosition(&currentPositionUs) == OK) {
549 mDeferredActions.push_back(
550 new SeekAction(currentPositionUs, false /* needNotify */));
551 }
Andy Hung73535852014-09-05 11:42:58 -0700552 }
Wei Jiaac428aa2014-09-02 19:01:34 -0700553
Andreas Huber57a339c2012-12-03 11:18:00 -0800554 // If there is a new surface texture, instantiate decoders
555 // again if possible.
556 mDeferredActions.push_back(
557 new SimpleAction(&NuPlayer::performScanSources));
558 }
559
560 processDeferredActions();
Andreas Huberf9334412010-12-15 15:17:42 -0800561 break;
562 }
563
564 case kWhatSetAudioSink:
565 {
Steve Block3856b092011-10-20 11:56:00 +0100566 ALOGV("kWhatSetAudioSink");
Andreas Huberf9334412010-12-15 15:17:42 -0800567
568 sp<RefBase> obj;
569 CHECK(msg->findObject("sink", &obj));
570
571 mAudioSink = static_cast<MediaPlayerBase::AudioSink *>(obj.get());
572 break;
573 }
574
575 case kWhatStart:
576 {
Steve Block3856b092011-10-20 11:56:00 +0100577 ALOGV("kWhatStart");
Wei Jia94211742014-10-28 17:09:06 -0700578 if (mStarted) {
579 onResume();
580 } else {
581 onStart();
Lajos Molnar09524832014-07-17 14:29:51 -0700582 }
Andreas Huberf9334412010-12-15 15:17:42 -0800583 break;
584 }
585
586 case kWhatScanSources:
587 {
Andreas Huber1aef2112011-01-04 14:01:29 -0800588 int32_t generation;
589 CHECK(msg->findInt32("generation", &generation));
590 if (generation != mScanSourcesGeneration) {
591 // Drop obsolete msg.
592 break;
593 }
594
Andreas Huber5bc087c2010-12-23 10:27:40 -0800595 mScanSourcesPending = false;
596
Steve Block3856b092011-10-20 11:56:00 +0100597 ALOGV("scanning sources haveAudio=%d, haveVideo=%d",
Andreas Huber43c3e6c2011-01-05 12:17:08 -0800598 mAudioDecoder != NULL, mVideoDecoder != NULL);
599
Andreas Huberb7c8e912012-11-27 15:02:53 -0800600 bool mHadAnySourcesBefore =
601 (mAudioDecoder != NULL) || (mVideoDecoder != NULL);
602
Andy Hung282a7e32014-08-14 15:56:34 -0700603 // initialize video before audio because successful initialization of
604 // video may change deep buffer mode of audio.
Haynes Mathew George5d246ef2012-07-09 10:36:57 -0700605 if (mNativeWindow != NULL) {
606 instantiateDecoder(false, &mVideoDecoder);
607 }
Andreas Huberf9334412010-12-15 15:17:42 -0800608
609 if (mAudioSink != NULL) {
Andy Hung282a7e32014-08-14 15:56:34 -0700610 if (mOffloadAudio) {
611 // open audio sink early under offload mode.
612 sp<AMessage> format = mSource->getFormat(true /*audio*/);
613 openAudioSink(format, true /*offloadOnly*/);
614 }
Andreas Huber5bc087c2010-12-23 10:27:40 -0800615 instantiateDecoder(true, &mAudioDecoder);
Andreas Huberf9334412010-12-15 15:17:42 -0800616 }
617
Andreas Huberb7c8e912012-11-27 15:02:53 -0800618 if (!mHadAnySourcesBefore
619 && (mAudioDecoder != NULL || mVideoDecoder != NULL)) {
620 // This is the first time we've found anything playable.
621
Andreas Huber9575c962013-02-05 13:59:56 -0800622 if (mSourceFlags & Source::FLAG_DYNAMIC_DURATION) {
Andreas Huberb7c8e912012-11-27 15:02:53 -0800623 schedulePollDuration();
624 }
625 }
626
Andreas Hubereac68ba2011-09-27 12:12:25 -0700627 status_t err;
628 if ((err = mSource->feedMoreTSData()) != OK) {
Andreas Huber1aef2112011-01-04 14:01:29 -0800629 if (mAudioDecoder == NULL && mVideoDecoder == NULL) {
630 // We're not currently decoding anything (no audio or
631 // video tracks found) and we just ran out of input data.
Andreas Hubereac68ba2011-09-27 12:12:25 -0700632
633 if (err == ERROR_END_OF_STREAM) {
634 notifyListener(MEDIA_PLAYBACK_COMPLETE, 0, 0);
635 } else {
636 notifyListener(MEDIA_ERROR, MEDIA_ERROR_UNKNOWN, err);
637 }
Andreas Huber1aef2112011-01-04 14:01:29 -0800638 }
Andreas Huberf9334412010-12-15 15:17:42 -0800639 break;
640 }
641
Andreas Huberfbe9d812012-08-31 14:05:27 -0700642 if ((mAudioDecoder == NULL && mAudioSink != NULL)
643 || (mVideoDecoder == NULL && mNativeWindow != NULL)) {
Andreas Huberf9334412010-12-15 15:17:42 -0800644 msg->post(100000ll);
Andreas Huber5bc087c2010-12-23 10:27:40 -0800645 mScanSourcesPending = true;
Andreas Huberf9334412010-12-15 15:17:42 -0800646 }
647 break;
648 }
649
650 case kWhatVideoNotify:
651 case kWhatAudioNotify:
652 {
653 bool audio = msg->what() == kWhatAudioNotify;
654
Wei Jia88703c32014-08-06 11:24:07 -0700655 int32_t currentDecoderGeneration =
656 (audio? mAudioDecoderGeneration : mVideoDecoderGeneration);
657 int32_t requesterGeneration = currentDecoderGeneration - 1;
658 CHECK(msg->findInt32("generation", &requesterGeneration));
659
660 if (requesterGeneration != currentDecoderGeneration) {
661 ALOGV("got message from old %s decoder, generation(%d:%d)",
662 audio ? "audio" : "video", requesterGeneration,
663 currentDecoderGeneration);
664 sp<AMessage> reply;
665 if (!(msg->findMessage("reply", &reply))) {
666 return;
667 }
668
669 reply->setInt32("err", INFO_DISCONTINUITY);
670 reply->post();
671 return;
672 }
673
Andreas Huberf9334412010-12-15 15:17:42 -0800674 int32_t what;
Lajos Molnar1cd13982014-01-17 15:12:51 -0800675 CHECK(msg->findInt32("what", &what));
Andreas Huberf9334412010-12-15 15:17:42 -0800676
Lajos Molnar1cd13982014-01-17 15:12:51 -0800677 if (what == Decoder::kWhatFillThisBuffer) {
Andreas Huberf9334412010-12-15 15:17:42 -0800678 status_t err = feedDecoderInputData(
Lajos Molnar1cd13982014-01-17 15:12:51 -0800679 audio, msg);
Andreas Huberf9334412010-12-15 15:17:42 -0800680
Andreas Huber5bc087c2010-12-23 10:27:40 -0800681 if (err == -EWOULDBLOCK) {
Andreas Hubereac68ba2011-09-27 12:12:25 -0700682 if (mSource->feedMoreTSData() == OK) {
Phil Burkc5cc2e22014-09-09 20:08:39 -0700683 msg->post(10 * 1000ll);
Andreas Huber5bc087c2010-12-23 10:27:40 -0800684 }
Andreas Huberf9334412010-12-15 15:17:42 -0800685 }
Lajos Molnar1cd13982014-01-17 15:12:51 -0800686 } else if (what == Decoder::kWhatEOS) {
Andreas Huberdc9bacd2011-09-26 10:53:29 -0700687 int32_t err;
Lajos Molnar1cd13982014-01-17 15:12:51 -0800688 CHECK(msg->findInt32("err", &err));
Andreas Huberdc9bacd2011-09-26 10:53:29 -0700689
690 if (err == ERROR_END_OF_STREAM) {
Steve Block3856b092011-10-20 11:56:00 +0100691 ALOGV("got %s decoder EOS", audio ? "audio" : "video");
Andreas Huberdc9bacd2011-09-26 10:53:29 -0700692 } else {
Steve Block3856b092011-10-20 11:56:00 +0100693 ALOGV("got %s decoder EOS w/ error %d",
Andreas Huberdc9bacd2011-09-26 10:53:29 -0700694 audio ? "audio" : "video",
695 err);
696 }
697
698 mRenderer->queueEOS(audio, err);
Lajos Molnar1cd13982014-01-17 15:12:51 -0800699 } else if (what == Decoder::kWhatFlushCompleted) {
Steve Block3856b092011-10-20 11:56:00 +0100700 ALOGV("decoder %s flush completed", audio ? "audio" : "video");
Andreas Huberf9334412010-12-15 15:17:42 -0800701
Andy Hung8d121d42014-10-03 09:53:53 -0700702 handleFlushComplete(audio, true /* isDecoder */);
Andreas Huber3831a062010-12-21 10:22:33 -0800703 finishFlushIfPossible();
Lajos Molnar1cd13982014-01-17 15:12:51 -0800704 } else if (what == Decoder::kWhatOutputFormatChanged) {
705 sp<AMessage> format;
706 CHECK(msg->findMessage("format", &format));
707
Andreas Huber31e25082011-01-10 10:38:31 -0800708 if (audio) {
Andy Hung282a7e32014-08-14 15:56:34 -0700709 openAudioSink(format, false /*offloadOnly*/);
Andreas Huber31e25082011-01-10 10:38:31 -0800710 } else {
711 // video
Chong Zhangced1c2f2014-08-08 15:22:35 -0700712 sp<AMessage> inputFormat =
713 mSource->getFormat(false /* audio */);
Andreas Huber3831a062010-12-21 10:22:33 -0800714
Chong Zhangced1c2f2014-08-08 15:22:35 -0700715 updateVideoSize(inputFormat, format);
Andreas Huber31e25082011-01-10 10:38:31 -0800716 }
Lajos Molnar1cd13982014-01-17 15:12:51 -0800717 } else if (what == Decoder::kWhatShutdownCompleted) {
Steve Block3856b092011-10-20 11:56:00 +0100718 ALOGV("%s shutdown completed", audio ? "audio" : "video");
Andreas Huber3831a062010-12-21 10:22:33 -0800719 if (audio) {
720 mAudioDecoder.clear();
Wei Jia57568df2014-09-22 10:16:29 -0700721 ++mAudioDecoderGeneration;
Andreas Huber3831a062010-12-21 10:22:33 -0800722
723 CHECK_EQ((int)mFlushingAudio, (int)SHUTTING_DOWN_DECODER);
724 mFlushingAudio = SHUT_DOWN;
725 } else {
726 mVideoDecoder.clear();
Wei Jia57568df2014-09-22 10:16:29 -0700727 ++mVideoDecoderGeneration;
Andreas Huber3831a062010-12-21 10:22:33 -0800728
729 CHECK_EQ((int)mFlushingVideo, (int)SHUTTING_DOWN_DECODER);
730 mFlushingVideo = SHUT_DOWN;
731 }
732
733 finishFlushIfPossible();
Lajos Molnar1cd13982014-01-17 15:12:51 -0800734 } else if (what == Decoder::kWhatError) {
Chong Zhangf4c0a942014-08-11 15:14:10 -0700735 status_t err;
Andy Hung2abde2c2014-09-30 14:40:32 -0700736 if (!msg->findInt32("err", &err) || err == OK) {
Chong Zhangf4c0a942014-08-11 15:14:10 -0700737 err = UNKNOWN_ERROR;
738 }
Andy Hungcf31f1e2014-09-23 14:59:01 -0700739
Andy Hung2abde2c2014-09-30 14:40:32 -0700740 // Decoder errors can be due to Source (e.g. from streaming),
741 // or from decoding corrupted bitstreams, or from other decoder
742 // MediaCodec operations (e.g. from an ongoing reset or seek).
743 //
744 // We try to gracefully shut down the affected decoder if possible,
745 // rather than trying to force the shutdown with something
746 // similar to performReset(). This method can lead to a hang
747 // if MediaCodec functions block after an error, but they should
748 // typically return INVALID_OPERATION instead of blocking.
749
750 FlushStatus *flushing = audio ? &mFlushingAudio : &mFlushingVideo;
751 ALOGE("received error(%#x) from %s decoder, flushing(%d), now shutting down",
752 err, audio ? "audio" : "video", *flushing);
753
754 switch (*flushing) {
755 case NONE:
756 mDeferredActions.push_back(
Wei Jiafef808d2014-10-31 17:57:05 -0700757 new FlushDecoderAction(
758 audio ? FLUSH_CMD_SHUTDOWN : FLUSH_CMD_NONE,
759 audio ? FLUSH_CMD_NONE : FLUSH_CMD_SHUTDOWN));
Andy Hung2abde2c2014-09-30 14:40:32 -0700760 processDeferredActions();
761 break;
762 case FLUSHING_DECODER:
763 *flushing = FLUSHING_DECODER_SHUTDOWN; // initiate shutdown after flush.
764 break; // Wait for flush to complete.
765 case FLUSHING_DECODER_SHUTDOWN:
766 break; // Wait for flush to complete.
767 case SHUTTING_DOWN_DECODER:
768 break; // Wait for shutdown to complete.
769 case FLUSHED:
770 // Widevine source reads must stop before releasing the video decoder.
771 if (!audio && mSource != NULL && mSourceFlags & Source::FLAG_SECURE) {
772 mSource->stop();
773 }
774 getDecoder(audio)->initiateShutdown(); // In the middle of a seek.
775 *flushing = SHUTTING_DOWN_DECODER; // Shut down.
776 break;
777 case SHUT_DOWN:
778 finishFlushIfPossible(); // Should not occur.
779 break; // Finish anyways.
Marco Nelissen9e2b7912014-08-18 16:13:03 -0700780 }
Andy Hung2abde2c2014-09-30 14:40:32 -0700781 notifyListener(MEDIA_ERROR, MEDIA_ERROR_UNKNOWN, err);
Lajos Molnar1cd13982014-01-17 15:12:51 -0800782 } else if (what == Decoder::kWhatDrainThisBuffer) {
783 renderBuffer(audio, msg);
784 } else {
785 ALOGV("Unhandled decoder notification %d '%c%c%c%c'.",
Andreas Hubera1f8ab02012-11-30 10:53:22 -0800786 what,
787 what >> 24,
788 (what >> 16) & 0xff,
789 (what >> 8) & 0xff,
790 what & 0xff);
Andreas Huberf9334412010-12-15 15:17:42 -0800791 }
792
793 break;
794 }
795
796 case kWhatRendererNotify:
797 {
Wei Jia57568df2014-09-22 10:16:29 -0700798 int32_t requesterGeneration = mRendererGeneration - 1;
799 CHECK(msg->findInt32("generation", &requesterGeneration));
800 if (requesterGeneration != mRendererGeneration) {
801 ALOGV("got message from old renderer, generation(%d:%d)",
802 requesterGeneration, mRendererGeneration);
803 return;
804 }
805
Andreas Huberf9334412010-12-15 15:17:42 -0800806 int32_t what;
807 CHECK(msg->findInt32("what", &what));
808
809 if (what == Renderer::kWhatEOS) {
810 int32_t audio;
811 CHECK(msg->findInt32("audio", &audio));
812
Andreas Huberc92fd242011-08-16 13:48:44 -0700813 int32_t finalResult;
814 CHECK(msg->findInt32("finalResult", &finalResult));
815
Andreas Huberf9334412010-12-15 15:17:42 -0800816 if (audio) {
817 mAudioEOS = true;
818 } else {
819 mVideoEOS = true;
820 }
821
Andreas Huberc92fd242011-08-16 13:48:44 -0700822 if (finalResult == ERROR_END_OF_STREAM) {
Steve Block3856b092011-10-20 11:56:00 +0100823 ALOGV("reached %s EOS", audio ? "audio" : "video");
Andreas Huberc92fd242011-08-16 13:48:44 -0700824 } else {
Steve Block29357bc2012-01-06 19:20:56 +0000825 ALOGE("%s track encountered an error (%d)",
Andreas Huberc92fd242011-08-16 13:48:44 -0700826 audio ? "audio" : "video", finalResult);
827
828 notifyListener(
829 MEDIA_ERROR, MEDIA_ERROR_UNKNOWN, finalResult);
830 }
Andreas Huberf9334412010-12-15 15:17:42 -0800831
832 if ((mAudioEOS || mAudioDecoder == NULL)
833 && (mVideoEOS || mVideoDecoder == NULL)) {
834 notifyListener(MEDIA_PLAYBACK_COMPLETE, 0, 0);
835 }
Andreas Huber3fe62152011-09-16 15:09:22 -0700836 } else if (what == Renderer::kWhatFlushComplete) {
Andreas Huberf9334412010-12-15 15:17:42 -0800837 int32_t audio;
838 CHECK(msg->findInt32("audio", &audio));
839
Steve Block3856b092011-10-20 11:56:00 +0100840 ALOGV("renderer %s flush completed.", audio ? "audio" : "video");
Andy Hung8d121d42014-10-03 09:53:53 -0700841 handleFlushComplete(audio, false /* isDecoder */);
842 finishFlushIfPossible();
James Dongf57b4ea2012-07-20 13:38:36 -0700843 } else if (what == Renderer::kWhatVideoRenderingStart) {
844 notifyListener(MEDIA_INFO, MEDIA_INFO_RENDERING_START, 0);
Lajos Molnarcbaffcf2013-08-14 18:30:38 -0700845 } else if (what == Renderer::kWhatMediaRenderingStart) {
846 ALOGV("media rendering started");
847 notifyListener(MEDIA_STARTED, 0, 0);
Wei Jia3a2956d2014-07-22 16:01:33 -0700848 } else if (what == Renderer::kWhatAudioOffloadTearDown) {
849 ALOGV("Tear down audio offload, fall back to s/w path");
850 int64_t positionUs;
851 CHECK(msg->findInt64("positionUs", &positionUs));
Ronghua Wu08529172014-10-02 16:55:52 -0700852 int32_t reason;
853 CHECK(msg->findInt32("reason", &reason));
Andy Hung282a7e32014-08-14 15:56:34 -0700854 closeAudioSink();
Wei Jia3a2956d2014-07-22 16:01:33 -0700855 mAudioDecoder.clear();
Wei Jia57568df2014-09-22 10:16:29 -0700856 ++mAudioDecoderGeneration;
Wei Jia3a2956d2014-07-22 16:01:33 -0700857 mRenderer->flush(true /* audio */);
858 if (mVideoDecoder != NULL) {
859 mRenderer->flush(false /* audio */);
860 }
861 mRenderer->signalDisableOffloadAudio();
862 mOffloadAudio = false;
863
Wei Jiae427abf2014-09-22 15:21:11 -0700864 performSeek(positionUs, false /* needNotify */);
Ronghua Wu08529172014-10-02 16:55:52 -0700865 if (reason == Renderer::kDueToError) {
866 instantiateDecoder(true /* audio */, &mAudioDecoder);
867 }
Andreas Huberf9334412010-12-15 15:17:42 -0800868 }
869 break;
870 }
871
872 case kWhatMoreDataQueued:
873 {
874 break;
875 }
876
Andreas Huber1aef2112011-01-04 14:01:29 -0800877 case kWhatReset:
878 {
Steve Block3856b092011-10-20 11:56:00 +0100879 ALOGV("kWhatReset");
Andreas Huber1aef2112011-01-04 14:01:29 -0800880
Andreas Hubera1f8ab02012-11-30 10:53:22 -0800881 mDeferredActions.push_back(
Wei Jiafef808d2014-10-31 17:57:05 -0700882 new FlushDecoderAction(
883 FLUSH_CMD_SHUTDOWN /* audio */,
884 FLUSH_CMD_SHUTDOWN /* video */));
Andreas Huberb7c8e912012-11-27 15:02:53 -0800885
Andreas Hubera1f8ab02012-11-30 10:53:22 -0800886 mDeferredActions.push_back(
887 new SimpleAction(&NuPlayer::performReset));
Andreas Huberb58ce9f2011-11-28 16:27:35 -0800888
Andreas Hubera1f8ab02012-11-30 10:53:22 -0800889 processDeferredActions();
Andreas Huber1aef2112011-01-04 14:01:29 -0800890 break;
891 }
892
Andreas Huber43c3e6c2011-01-05 12:17:08 -0800893 case kWhatSeek:
894 {
895 int64_t seekTimeUs;
Wei Jiae427abf2014-09-22 15:21:11 -0700896 int32_t needNotify;
Andreas Huber43c3e6c2011-01-05 12:17:08 -0800897 CHECK(msg->findInt64("seekTimeUs", &seekTimeUs));
Wei Jiae427abf2014-09-22 15:21:11 -0700898 CHECK(msg->findInt32("needNotify", &needNotify));
Andreas Huber43c3e6c2011-01-05 12:17:08 -0800899
Wei Jiae427abf2014-09-22 15:21:11 -0700900 ALOGV("kWhatSeek seekTimeUs=%lld us, needNotify=%d",
901 seekTimeUs, needNotify);
Andreas Huber43c3e6c2011-01-05 12:17:08 -0800902
Andreas Hubera1f8ab02012-11-30 10:53:22 -0800903 mDeferredActions.push_back(
Wei Jiafef808d2014-10-31 17:57:05 -0700904 new FlushDecoderAction(FLUSH_CMD_FLUSH /* audio */,
905 FLUSH_CMD_FLUSH /* video */));
Andreas Huber43c3e6c2011-01-05 12:17:08 -0800906
Wei Jiae427abf2014-09-22 15:21:11 -0700907 mDeferredActions.push_back(
908 new SeekAction(seekTimeUs, needNotify));
Andreas Huber43c3e6c2011-01-05 12:17:08 -0800909
Andreas Hubera1f8ab02012-11-30 10:53:22 -0800910 processDeferredActions();
Andreas Huber43c3e6c2011-01-05 12:17:08 -0800911 break;
912 }
913
Andreas Huberb4082222011-01-20 15:23:04 -0800914 case kWhatPause:
915 {
Wei Jia7e9f7f72014-09-23 10:55:35 -0700916 if (mSource != NULL) {
917 mSource->pause();
918 } else {
919 ALOGW("pause called when source is gone or not set");
920 }
921 if (mRenderer != NULL) {
922 mRenderer->pause();
923 } else {
924 ALOGW("pause called when renderer is gone or not set");
925 }
Andreas Huberb4082222011-01-20 15:23:04 -0800926 break;
927 }
928
Andreas Huberb5f25f02013-02-05 10:14:26 -0800929 case kWhatSourceNotify:
930 {
Andreas Huber9575c962013-02-05 13:59:56 -0800931 onSourceNotify(msg);
Andreas Huberb5f25f02013-02-05 10:14:26 -0800932 break;
933 }
934
Chong Zhanga7fa1d92014-06-11 14:49:23 -0700935 case kWhatClosedCaptionNotify:
936 {
937 onClosedCaptionNotify(msg);
938 break;
939 }
940
Andreas Huberf9334412010-12-15 15:17:42 -0800941 default:
942 TRESPASS();
943 break;
944 }
945}
946
Wei Jia94211742014-10-28 17:09:06 -0700947void NuPlayer::onResume() {
948 if (mSource != NULL) {
949 mSource->resume();
950 } else {
951 ALOGW("resume called when source is gone or not set");
952 }
953 // |mAudioDecoder| may have been released due to the pause timeout, so re-create it if
954 // needed.
955 if (audioDecoderStillNeeded() && mAudioDecoder == NULL) {
956 instantiateDecoder(true /* audio */, &mAudioDecoder);
957 }
958 if (mRenderer != NULL) {
959 mRenderer->resume();
960 } else {
961 ALOGW("resume called when renderer is gone or not set");
962 }
963}
964
965void NuPlayer::onStart() {
966 mVideoIsAVC = false;
967 mOffloadAudio = false;
968 mAudioEOS = false;
969 mVideoEOS = false;
970 mSkipRenderingAudioUntilMediaTimeUs = -1;
971 mSkipRenderingVideoUntilMediaTimeUs = -1;
972 mNumFramesTotal = 0;
973 mNumFramesDropped = 0;
974 mStarted = true;
975
976 /* instantiate decoders now for secure playback */
977 if (mSourceFlags & Source::FLAG_SECURE) {
978 if (mNativeWindow != NULL) {
979 instantiateDecoder(false, &mVideoDecoder);
980 }
981
982 if (mAudioSink != NULL) {
983 instantiateDecoder(true, &mAudioDecoder);
984 }
985 }
986
987 mSource->start();
988
989 uint32_t flags = 0;
990
991 if (mSource->isRealTime()) {
992 flags |= Renderer::FLAG_REAL_TIME;
993 }
994
995 sp<MetaData> audioMeta = mSource->getFormatMeta(true /* audio */);
996 audio_stream_type_t streamType = AUDIO_STREAM_MUSIC;
997 if (mAudioSink != NULL) {
998 streamType = mAudioSink->getAudioStreamType();
999 }
1000
1001 sp<AMessage> videoFormat = mSource->getFormat(false /* audio */);
1002
1003 mOffloadAudio =
1004 canOffloadStream(audioMeta, (videoFormat != NULL),
1005 true /* is_streaming */, streamType);
1006 if (mOffloadAudio) {
1007 flags |= Renderer::FLAG_OFFLOAD_AUDIO;
1008 }
1009
1010 sp<AMessage> notify = new AMessage(kWhatRendererNotify, id());
1011 ++mRendererGeneration;
1012 notify->setInt32("generation", mRendererGeneration);
1013 mRenderer = new Renderer(mAudioSink, notify, flags);
1014
1015 mRendererLooper = new ALooper;
1016 mRendererLooper->setName("NuPlayerRenderer");
1017 mRendererLooper->start(false, false, ANDROID_PRIORITY_AUDIO);
1018 mRendererLooper->registerHandler(mRenderer);
1019
1020 sp<MetaData> meta = getFileMeta();
1021 int32_t rate;
1022 if (meta != NULL
1023 && meta->findInt32(kKeyFrameRate, &rate) && rate > 0) {
1024 mRenderer->setVideoFrameRate(rate);
1025 }
1026
1027 postScanSources();
1028}
1029
Ronghua Wud7988b12014-10-03 15:19:10 -07001030bool NuPlayer::audioDecoderStillNeeded() {
1031 // Audio decoder is no longer needed if it's in shut/shutting down status.
1032 return ((mFlushingAudio != SHUT_DOWN) && (mFlushingAudio != SHUTTING_DOWN_DECODER));
1033}
1034
Andy Hung8d121d42014-10-03 09:53:53 -07001035void NuPlayer::handleFlushComplete(bool audio, bool isDecoder) {
1036 // We wait for both the decoder flush and the renderer flush to complete
1037 // before entering either the FLUSHED or the SHUTTING_DOWN_DECODER state.
1038
1039 mFlushComplete[audio][isDecoder] = true;
1040 if (!mFlushComplete[audio][!isDecoder]) {
1041 return;
1042 }
1043
1044 FlushStatus *state = audio ? &mFlushingAudio : &mFlushingVideo;
1045 switch (*state) {
1046 case FLUSHING_DECODER:
1047 {
1048 *state = FLUSHED;
Andy Hung8d121d42014-10-03 09:53:53 -07001049 break;
1050 }
1051
1052 case FLUSHING_DECODER_SHUTDOWN:
1053 {
1054 *state = SHUTTING_DOWN_DECODER;
1055
1056 ALOGV("initiating %s decoder shutdown", audio ? "audio" : "video");
1057 if (!audio) {
Andy Hung8d121d42014-10-03 09:53:53 -07001058 // Widevine source reads must stop before releasing the video decoder.
1059 if (mSource != NULL && mSourceFlags & Source::FLAG_SECURE) {
1060 mSource->stop();
1061 }
1062 }
1063 getDecoder(audio)->initiateShutdown();
1064 break;
1065 }
1066
1067 default:
1068 // decoder flush completes only occur in a flushing state.
1069 LOG_ALWAYS_FATAL_IF(isDecoder, "decoder flush in invalid state %d", *state);
1070 break;
1071 }
1072}
1073
Andreas Huber3831a062010-12-21 10:22:33 -08001074void NuPlayer::finishFlushIfPossible() {
Wei Jia53904f32014-07-29 10:22:53 -07001075 if (mFlushingAudio != NONE && mFlushingAudio != FLUSHED
1076 && mFlushingAudio != SHUT_DOWN) {
Andreas Huber3831a062010-12-21 10:22:33 -08001077 return;
1078 }
1079
Wei Jia53904f32014-07-29 10:22:53 -07001080 if (mFlushingVideo != NONE && mFlushingVideo != FLUSHED
1081 && mFlushingVideo != SHUT_DOWN) {
Andreas Huber3831a062010-12-21 10:22:33 -08001082 return;
1083 }
1084
Steve Block3856b092011-10-20 11:56:00 +01001085 ALOGV("both audio and video are flushed now.");
Andreas Huber3831a062010-12-21 10:22:33 -08001086
Phil Burk9f526492014-09-03 15:04:12 -07001087 mPendingAudioAccessUnit.clear();
Phil Burkc5cc2e22014-09-09 20:08:39 -07001088 mAggregateBuffer.clear();
Phil Burk9f526492014-09-03 15:04:12 -07001089
Andreas Huber6e3d3112011-11-28 12:36:11 -08001090 if (mTimeDiscontinuityPending) {
1091 mRenderer->signalTimeDiscontinuity();
1092 mTimeDiscontinuityPending = false;
1093 }
Andreas Huber3831a062010-12-21 10:22:33 -08001094
Wei Jia53904f32014-07-29 10:22:53 -07001095 if (mAudioDecoder != NULL && mFlushingAudio == FLUSHED) {
Andreas Huber3831a062010-12-21 10:22:33 -08001096 mAudioDecoder->signalResume();
1097 }
1098
Wei Jia53904f32014-07-29 10:22:53 -07001099 if (mVideoDecoder != NULL && mFlushingVideo == FLUSHED) {
Andreas Huber3831a062010-12-21 10:22:33 -08001100 mVideoDecoder->signalResume();
1101 }
1102
1103 mFlushingAudio = NONE;
1104 mFlushingVideo = NONE;
Andreas Huber3831a062010-12-21 10:22:33 -08001105
Andy Hung8d121d42014-10-03 09:53:53 -07001106 clearFlushComplete();
1107
Andreas Hubera1f8ab02012-11-30 10:53:22 -08001108 processDeferredActions();
Andreas Huber1aef2112011-01-04 14:01:29 -08001109}
1110
1111void NuPlayer::postScanSources() {
1112 if (mScanSourcesPending) {
1113 return;
1114 }
1115
1116 sp<AMessage> msg = new AMessage(kWhatScanSources, id());
1117 msg->setInt32("generation", mScanSourcesGeneration);
1118 msg->post();
1119
1120 mScanSourcesPending = true;
1121}
1122
Andy Hung282a7e32014-08-14 15:56:34 -07001123void NuPlayer::openAudioSink(const sp<AMessage> &format, bool offloadOnly) {
Andy Hung282a7e32014-08-14 15:56:34 -07001124 uint32_t flags;
1125 int64_t durationUs;
Chong Zhang3b9eb1f2014-10-15 17:05:08 -07001126 bool hasVideo = (mVideoDecoder != NULL);
Andy Hung282a7e32014-08-14 15:56:34 -07001127 // FIXME: we should handle the case where the video decoder
1128 // is created after we receive the format change indication.
1129 // Current code will just make that we select deep buffer
1130 // with video which should not be a problem as it should
1131 // not prevent from keeping A/V sync.
Eric Laurentd88c3ca2014-10-28 10:52:11 -07001132 if (!hasVideo &&
Andy Hung282a7e32014-08-14 15:56:34 -07001133 mSource->getDuration(&durationUs) == OK &&
1134 durationUs
1135 > AUDIO_SINK_MIN_DEEP_BUFFER_DURATION_US) {
1136 flags = AUDIO_OUTPUT_FLAG_DEEP_BUFFER;
1137 } else {
1138 flags = AUDIO_OUTPUT_FLAG_NONE;
1139 }
1140
Chong Zhang3b9eb1f2014-10-15 17:05:08 -07001141 mOffloadAudio = mRenderer->openAudioSink(
1142 format, offloadOnly, hasVideo, flags);
1143
Andy Hung282a7e32014-08-14 15:56:34 -07001144 if (mOffloadAudio) {
Chong Zhang3b9eb1f2014-10-15 17:05:08 -07001145 sp<MetaData> audioMeta =
1146 mSource->getFormatMeta(true /* audio */);
1147 sendMetaDataToHal(mAudioSink, audioMeta);
Andy Hung282a7e32014-08-14 15:56:34 -07001148 }
1149}
1150
1151void NuPlayer::closeAudioSink() {
Chong Zhang3b9eb1f2014-10-15 17:05:08 -07001152 mRenderer->closeAudioSink();
Andy Hung282a7e32014-08-14 15:56:34 -07001153}
1154
Andreas Huber5bc087c2010-12-23 10:27:40 -08001155status_t NuPlayer::instantiateDecoder(bool audio, sp<Decoder> *decoder) {
Andreas Huberf9334412010-12-15 15:17:42 -08001156 if (*decoder != NULL) {
1157 return OK;
1158 }
1159
Andreas Huber84066782011-08-16 09:34:26 -07001160 sp<AMessage> format = mSource->getFormat(audio);
Andreas Huberf9334412010-12-15 15:17:42 -08001161
Andreas Huber84066782011-08-16 09:34:26 -07001162 if (format == NULL) {
Andreas Huberf9334412010-12-15 15:17:42 -08001163 return -EWOULDBLOCK;
1164 }
1165
Andreas Huber3fe62152011-09-16 15:09:22 -07001166 if (!audio) {
Andreas Huber84066782011-08-16 09:34:26 -07001167 AString mime;
1168 CHECK(format->findString("mime", &mime));
1169 mVideoIsAVC = !strcasecmp(MEDIA_MIMETYPE_VIDEO_AVC, mime.c_str());
Chong Zhanga7fa1d92014-06-11 14:49:23 -07001170
1171 sp<AMessage> ccNotify = new AMessage(kWhatClosedCaptionNotify, id());
1172 mCCDecoder = new CCDecoder(ccNotify);
Lajos Molnar09524832014-07-17 14:29:51 -07001173
1174 if (mSourceFlags & Source::FLAG_SECURE) {
1175 format->setInt32("secure", true);
1176 }
Andreas Huber3fe62152011-09-16 15:09:22 -07001177 }
1178
Wei Jiabc2fb722014-07-08 16:37:57 -07001179 if (audio) {
Wei Jia88703c32014-08-06 11:24:07 -07001180 sp<AMessage> notify = new AMessage(kWhatAudioNotify, id());
1181 ++mAudioDecoderGeneration;
1182 notify->setInt32("generation", mAudioDecoderGeneration);
1183
Wei Jiabc2fb722014-07-08 16:37:57 -07001184 if (mOffloadAudio) {
1185 *decoder = new DecoderPassThrough(notify);
1186 } else {
1187 *decoder = new Decoder(notify);
1188 }
1189 } else {
Wei Jia88703c32014-08-06 11:24:07 -07001190 sp<AMessage> notify = new AMessage(kWhatVideoNotify, id());
1191 ++mVideoDecoderGeneration;
1192 notify->setInt32("generation", mVideoDecoderGeneration);
1193
Wei Jiabc2fb722014-07-08 16:37:57 -07001194 *decoder = new Decoder(notify, mNativeWindow);
1195 }
Lajos Molnar1cd13982014-01-17 15:12:51 -08001196 (*decoder)->init();
Andreas Huber84066782011-08-16 09:34:26 -07001197 (*decoder)->configure(format);
Andreas Huberf9334412010-12-15 15:17:42 -08001198
Lajos Molnar09524832014-07-17 14:29:51 -07001199 // allocate buffers to decrypt widevine source buffers
1200 if (!audio && (mSourceFlags & Source::FLAG_SECURE)) {
1201 Vector<sp<ABuffer> > inputBufs;
1202 CHECK_EQ((*decoder)->getInputBuffers(&inputBufs), (status_t)OK);
1203
1204 Vector<MediaBuffer *> mediaBufs;
1205 for (size_t i = 0; i < inputBufs.size(); i++) {
1206 const sp<ABuffer> &buffer = inputBufs[i];
1207 MediaBuffer *mbuf = new MediaBuffer(buffer->data(), buffer->size());
1208 mediaBufs.push(mbuf);
1209 }
1210
1211 status_t err = mSource->setBuffers(audio, mediaBufs);
1212 if (err != OK) {
1213 for (size_t i = 0; i < mediaBufs.size(); ++i) {
1214 mediaBufs[i]->release();
1215 }
1216 mediaBufs.clear();
1217 ALOGE("Secure source didn't support secure mediaBufs.");
1218 return err;
1219 }
1220 }
Andreas Huberf9334412010-12-15 15:17:42 -08001221 return OK;
1222}
1223
1224status_t NuPlayer::feedDecoderInputData(bool audio, const sp<AMessage> &msg) {
1225 sp<AMessage> reply;
1226 CHECK(msg->findMessage("reply", &reply));
1227
Wei Jia53904f32014-07-29 10:22:53 -07001228 if ((audio && mFlushingAudio != NONE)
Wei Jiaf702d042014-09-09 12:08:47 -07001229 || (!audio && mFlushingVideo != NONE)
1230 || mSource == NULL) {
Wei Jiab189a5b2014-08-07 06:11:39 +00001231 reply->setInt32("err", INFO_DISCONTINUITY);
1232 reply->post();
1233 return OK;
Andreas Huberf9334412010-12-15 15:17:42 -08001234 }
1235
1236 sp<ABuffer> accessUnit;
Andreas Huberf9334412010-12-15 15:17:42 -08001237
Phil Burk9f526492014-09-03 15:04:12 -07001238 // Aggregate smaller buffers into a larger buffer.
1239 // The goal is to reduce power consumption.
Phil Burk33b51b02014-09-17 16:03:47 -07001240 // Note this will not work if the decoder requires one frame per buffer.
1241 bool doBufferAggregation = (audio && mOffloadAudio);
Phil Burk9f526492014-09-03 15:04:12 -07001242 bool needMoreData = false;
Phil Burk9f526492014-09-03 15:04:12 -07001243
Andreas Huber3fe62152011-09-16 15:09:22 -07001244 bool dropAccessUnit;
1245 do {
Phil Burk9f526492014-09-03 15:04:12 -07001246 status_t err;
1247 // Did we save an accessUnit earlier because of a discontinuity?
1248 if (audio && (mPendingAudioAccessUnit != NULL)) {
1249 accessUnit = mPendingAudioAccessUnit;
1250 mPendingAudioAccessUnit.clear();
1251 err = mPendingAudioErr;
1252 ALOGV("feedDecoderInputData() use mPendingAudioAccessUnit");
1253 } else {
1254 err = mSource->dequeueAccessUnit(audio, &accessUnit);
1255 }
Andreas Huber5bc087c2010-12-23 10:27:40 -08001256
Andreas Huber3fe62152011-09-16 15:09:22 -07001257 if (err == -EWOULDBLOCK) {
Phil Burkc5cc2e22014-09-09 20:08:39 -07001258 return err;
Andreas Huber3fe62152011-09-16 15:09:22 -07001259 } else if (err != OK) {
1260 if (err == INFO_DISCONTINUITY) {
Phil Burk33b51b02014-09-17 16:03:47 -07001261 if (doBufferAggregation && (mAggregateBuffer != NULL)) {
Phil Burk9f526492014-09-03 15:04:12 -07001262 // We already have some data so save this for later.
1263 mPendingAudioErr = err;
1264 mPendingAudioAccessUnit = accessUnit;
1265 accessUnit.clear();
1266 ALOGD("feedDecoderInputData() save discontinuity for later");
1267 break;
1268 }
Andreas Huber3fe62152011-09-16 15:09:22 -07001269 int32_t type;
1270 CHECK(accessUnit->meta()->findInt32("discontinuity", &type));
Andreas Huber53df1a42010-12-22 10:03:04 -08001271
Andreas Huber3fe62152011-09-16 15:09:22 -07001272 bool formatChange =
Andreas Huber6e3d3112011-11-28 12:36:11 -08001273 (audio &&
1274 (type & ATSParser::DISCONTINUITY_AUDIO_FORMAT))
1275 || (!audio &&
1276 (type & ATSParser::DISCONTINUITY_VIDEO_FORMAT));
Andreas Huber53df1a42010-12-22 10:03:04 -08001277
Andreas Huber6e3d3112011-11-28 12:36:11 -08001278 bool timeChange = (type & ATSParser::DISCONTINUITY_TIME) != 0;
1279
Steve Blockdf64d152012-01-04 20:05:49 +00001280 ALOGI("%s discontinuity (formatChange=%d, time=%d)",
Andreas Huber6e3d3112011-11-28 12:36:11 -08001281 audio ? "audio" : "video", formatChange, timeChange);
Andreas Huber32f3cef2011-03-02 15:34:46 -08001282
Andreas Huber3fe62152011-09-16 15:09:22 -07001283 if (audio) {
1284 mSkipRenderingAudioUntilMediaTimeUs = -1;
1285 } else {
1286 mSkipRenderingVideoUntilMediaTimeUs = -1;
1287 }
Andreas Huber32f3cef2011-03-02 15:34:46 -08001288
Andreas Huber6e3d3112011-11-28 12:36:11 -08001289 if (timeChange) {
1290 sp<AMessage> extra;
1291 if (accessUnit->meta()->findMessage("extra", &extra)
1292 && extra != NULL) {
1293 int64_t resumeAtMediaTimeUs;
1294 if (extra->findInt64(
1295 "resume-at-mediatimeUs", &resumeAtMediaTimeUs)) {
Steve Blockdf64d152012-01-04 20:05:49 +00001296 ALOGI("suppressing rendering of %s until %lld us",
Andreas Huber6e3d3112011-11-28 12:36:11 -08001297 audio ? "audio" : "video", resumeAtMediaTimeUs);
Andreas Huber3fe62152011-09-16 15:09:22 -07001298
Andreas Huber6e3d3112011-11-28 12:36:11 -08001299 if (audio) {
1300 mSkipRenderingAudioUntilMediaTimeUs =
1301 resumeAtMediaTimeUs;
1302 } else {
1303 mSkipRenderingVideoUntilMediaTimeUs =
1304 resumeAtMediaTimeUs;
1305 }
Andreas Huber3fe62152011-09-16 15:09:22 -07001306 }
Andreas Huber32f3cef2011-03-02 15:34:46 -08001307 }
1308 }
Andreas Huber3fe62152011-09-16 15:09:22 -07001309
Andreas Huber6e3d3112011-11-28 12:36:11 -08001310 mTimeDiscontinuityPending =
1311 mTimeDiscontinuityPending || timeChange;
1312
Lajos Molnar87603c02014-08-20 19:25:30 -07001313 bool seamlessFormatChange = false;
1314 sp<AMessage> newFormat = mSource->getFormat(audio);
1315 if (formatChange) {
1316 seamlessFormatChange =
1317 getDecoder(audio)->supportsSeamlessFormatChange(newFormat);
1318 // treat seamless format change separately
1319 formatChange = !seamlessFormatChange;
1320 }
1321 bool shutdownOrFlush = formatChange || timeChange;
1322
1323 // We want to queue up scan-sources only once per discontinuity.
1324 // We control this by doing it only if neither audio nor video are
1325 // flushing or shutting down. (After handling 1st discontinuity, one
1326 // of the flushing states will not be NONE.)
1327 // No need to scan sources if this discontinuity does not result
1328 // in a flush or shutdown, as the flushing state will stay NONE.
1329 if (mFlushingAudio == NONE && mFlushingVideo == NONE &&
1330 shutdownOrFlush) {
Robert Shiha2981012014-07-30 17:41:24 -07001331 // And we'll resume scanning sources once we're done
1332 // flushing.
1333 mDeferredActions.push_front(
1334 new SimpleAction(
1335 &NuPlayer::performScanSources));
1336 }
1337
Lajos Molnar87603c02014-08-20 19:25:30 -07001338 if (formatChange /* not seamless */) {
1339 // must change decoder
1340 flushDecoder(audio, /* needShutdown = */ true);
1341 } else if (timeChange) {
1342 // need to flush
1343 flushDecoder(audio, /* needShutdown = */ false, newFormat);
1344 err = OK;
1345 } else if (seamlessFormatChange) {
1346 // reuse existing decoder and don't flush
1347 updateDecoderFormatWithoutFlush(audio, newFormat);
1348 err = OK;
Andreas Huber6e3d3112011-11-28 12:36:11 -08001349 } else {
1350 // This stream is unaffected by the discontinuity
Andreas Huber6e3d3112011-11-28 12:36:11 -08001351 return -EWOULDBLOCK;
1352 }
Andreas Huber32f3cef2011-03-02 15:34:46 -08001353 }
1354
Andreas Huber3fe62152011-09-16 15:09:22 -07001355 reply->setInt32("err", err);
1356 reply->post();
1357 return OK;
Andreas Huberf9334412010-12-15 15:17:42 -08001358 }
1359
Andreas Huber3fe62152011-09-16 15:09:22 -07001360 if (!audio) {
1361 ++mNumFramesTotal;
1362 }
1363
1364 dropAccessUnit = false;
1365 if (!audio
Lajos Molnar09524832014-07-17 14:29:51 -07001366 && !(mSourceFlags & Source::FLAG_SECURE)
Ronghua Wua73d9e02014-10-08 15:13:29 -07001367 && mRenderer->getVideoLateByUs() > 100000ll
Andreas Huber3fe62152011-09-16 15:09:22 -07001368 && mVideoIsAVC
1369 && !IsAVCReferenceFrame(accessUnit)) {
1370 dropAccessUnit = true;
1371 ++mNumFramesDropped;
1372 }
Phil Burk9f526492014-09-03 15:04:12 -07001373
1374 size_t smallSize = accessUnit->size();
1375 needMoreData = false;
Phil Burkc5cc2e22014-09-09 20:08:39 -07001376 if (doBufferAggregation && (mAggregateBuffer == NULL)
Phil Burk9f526492014-09-03 15:04:12 -07001377 // Don't bother if only room for a few small buffers.
Phil Burkc5cc2e22014-09-09 20:08:39 -07001378 && (smallSize < (kAggregateBufferSizeBytes / 3))) {
Phil Burk9f526492014-09-03 15:04:12 -07001379 // Create a larger buffer for combining smaller buffers from the extractor.
Phil Burkc5cc2e22014-09-09 20:08:39 -07001380 mAggregateBuffer = new ABuffer(kAggregateBufferSizeBytes);
1381 mAggregateBuffer->setRange(0, 0); // start empty
Phil Burk9f526492014-09-03 15:04:12 -07001382 }
1383
Phil Burk33b51b02014-09-17 16:03:47 -07001384 if (doBufferAggregation && (mAggregateBuffer != NULL)) {
Phil Burk9f526492014-09-03 15:04:12 -07001385 int64_t timeUs;
Phil Burkc5cc2e22014-09-09 20:08:39 -07001386 int64_t dummy;
Phil Burk9f526492014-09-03 15:04:12 -07001387 bool smallTimestampValid = accessUnit->meta()->findInt64("timeUs", &timeUs);
Phil Burkc5cc2e22014-09-09 20:08:39 -07001388 bool bigTimestampValid = mAggregateBuffer->meta()->findInt64("timeUs", &dummy);
Phil Burk9f526492014-09-03 15:04:12 -07001389 // Will the smaller buffer fit?
Phil Burkc5cc2e22014-09-09 20:08:39 -07001390 size_t bigSize = mAggregateBuffer->size();
1391 size_t roomLeft = mAggregateBuffer->capacity() - bigSize;
Phil Burk9f526492014-09-03 15:04:12 -07001392 // Should we save this small buffer for the next big buffer?
1393 // If the first small buffer did not have a timestamp then save
1394 // any buffer that does have a timestamp until the next big buffer.
1395 if ((smallSize > roomLeft)
Phil Burkc5cc2e22014-09-09 20:08:39 -07001396 || (!bigTimestampValid && (bigSize > 0) && smallTimestampValid)) {
Phil Burk9f526492014-09-03 15:04:12 -07001397 mPendingAudioErr = err;
1398 mPendingAudioAccessUnit = accessUnit;
1399 accessUnit.clear();
1400 } else {
Phil Burkc5cc2e22014-09-09 20:08:39 -07001401 // Grab time from first small buffer if available.
1402 if ((bigSize == 0) && smallTimestampValid) {
1403 mAggregateBuffer->meta()->setInt64("timeUs", timeUs);
1404 }
Phil Burk9f526492014-09-03 15:04:12 -07001405 // Append small buffer to the bigger buffer.
Phil Burkc5cc2e22014-09-09 20:08:39 -07001406 memcpy(mAggregateBuffer->base() + bigSize, accessUnit->data(), smallSize);
Phil Burk9f526492014-09-03 15:04:12 -07001407 bigSize += smallSize;
Phil Burkc5cc2e22014-09-09 20:08:39 -07001408 mAggregateBuffer->setRange(0, bigSize);
Phil Burk9f526492014-09-03 15:04:12 -07001409
Phil Burkc5cc2e22014-09-09 20:08:39 -07001410 // Keep looping until we run out of room in the mAggregateBuffer.
Phil Burk9f526492014-09-03 15:04:12 -07001411 needMoreData = true;
1412
Phil Burkc5cc2e22014-09-09 20:08:39 -07001413 ALOGV("feedDecoderInputData() smallSize = %zu, bigSize = %zu, capacity = %zu",
1414 smallSize, bigSize, mAggregateBuffer->capacity());
Phil Burk9f526492014-09-03 15:04:12 -07001415 }
1416 }
1417 } while (dropAccessUnit || needMoreData);
Andreas Huberf9334412010-12-15 15:17:42 -08001418
Steve Block3856b092011-10-20 11:56:00 +01001419 // ALOGV("returned a valid buffer of %s data", audio ? "audio" : "video");
Andreas Huberf9334412010-12-15 15:17:42 -08001420
1421#if 0
1422 int64_t mediaTimeUs;
1423 CHECK(accessUnit->meta()->findInt64("timeUs", &mediaTimeUs));
Steve Block3856b092011-10-20 11:56:00 +01001424 ALOGV("feeding %s input buffer at media time %.2f secs",
Andreas Huberf9334412010-12-15 15:17:42 -08001425 audio ? "audio" : "video",
1426 mediaTimeUs / 1E6);
1427#endif
1428
Chong Zhanga7fa1d92014-06-11 14:49:23 -07001429 if (!audio) {
1430 mCCDecoder->decode(accessUnit);
1431 }
1432
Phil Burk33b51b02014-09-17 16:03:47 -07001433 if (doBufferAggregation && (mAggregateBuffer != NULL)) {
Phil Burkc5cc2e22014-09-09 20:08:39 -07001434 ALOGV("feedDecoderInputData() reply with aggregated buffer, %zu",
1435 mAggregateBuffer->size());
1436 reply->setBuffer("buffer", mAggregateBuffer);
1437 mAggregateBuffer.clear();
Phil Burk9f526492014-09-03 15:04:12 -07001438 } else {
1439 reply->setBuffer("buffer", accessUnit);
1440 }
1441
Andreas Huberf9334412010-12-15 15:17:42 -08001442 reply->post();
1443
1444 return OK;
1445}
1446
1447void NuPlayer::renderBuffer(bool audio, const sp<AMessage> &msg) {
Steve Block3856b092011-10-20 11:56:00 +01001448 // ALOGV("renderBuffer %s", audio ? "audio" : "video");
Andreas Huberf9334412010-12-15 15:17:42 -08001449
1450 sp<AMessage> reply;
1451 CHECK(msg->findMessage("reply", &reply));
1452
Wei Jia53904f32014-07-29 10:22:53 -07001453 if ((audio && mFlushingAudio != NONE)
1454 || (!audio && mFlushingVideo != NONE)) {
Andreas Huber18ac5402011-08-31 15:04:25 -07001455 // We're currently attempting to flush the decoder, in order
1456 // to complete this, the decoder wants all its buffers back,
1457 // so we don't want any output buffers it sent us (from before
1458 // we initiated the flush) to be stuck in the renderer's queue.
1459
Steve Block3856b092011-10-20 11:56:00 +01001460 ALOGV("we're still flushing the %s decoder, sending its output buffer"
Andreas Huber18ac5402011-08-31 15:04:25 -07001461 " right back.", audio ? "audio" : "video");
1462
1463 reply->post();
1464 return;
1465 }
1466
Andreas Huber2d8bedd2012-02-21 14:38:23 -08001467 sp<ABuffer> buffer;
1468 CHECK(msg->findBuffer("buffer", &buffer));
Andreas Huberf9334412010-12-15 15:17:42 -08001469
Chong Zhanga7fa1d92014-06-11 14:49:23 -07001470 int64_t mediaTimeUs;
1471 CHECK(buffer->meta()->findInt64("timeUs", &mediaTimeUs));
1472
Andreas Huber32f3cef2011-03-02 15:34:46 -08001473 int64_t &skipUntilMediaTimeUs =
1474 audio
1475 ? mSkipRenderingAudioUntilMediaTimeUs
1476 : mSkipRenderingVideoUntilMediaTimeUs;
1477
1478 if (skipUntilMediaTimeUs >= 0) {
Andreas Huber32f3cef2011-03-02 15:34:46 -08001479
1480 if (mediaTimeUs < skipUntilMediaTimeUs) {
Steve Block3856b092011-10-20 11:56:00 +01001481 ALOGV("dropping %s buffer at time %lld as requested.",
Andreas Huber32f3cef2011-03-02 15:34:46 -08001482 audio ? "audio" : "video",
1483 mediaTimeUs);
1484
1485 reply->post();
1486 return;
1487 }
1488
1489 skipUntilMediaTimeUs = -1;
1490 }
1491
Chong Zhanga7fa1d92014-06-11 14:49:23 -07001492 if (!audio && mCCDecoder->isSelected()) {
1493 mCCDecoder->display(mediaTimeUs);
1494 }
1495
Andreas Huberf9334412010-12-15 15:17:42 -08001496 mRenderer->queueBuffer(audio, buffer, reply);
1497}
1498
Chong Zhangced1c2f2014-08-08 15:22:35 -07001499void NuPlayer::updateVideoSize(
1500 const sp<AMessage> &inputFormat,
1501 const sp<AMessage> &outputFormat) {
1502 if (inputFormat == NULL) {
1503 ALOGW("Unknown video size, reporting 0x0!");
1504 notifyListener(MEDIA_SET_VIDEO_SIZE, 0, 0);
1505 return;
1506 }
1507
1508 int32_t displayWidth, displayHeight;
1509 int32_t cropLeft, cropTop, cropRight, cropBottom;
1510
1511 if (outputFormat != NULL) {
1512 int32_t width, height;
1513 CHECK(outputFormat->findInt32("width", &width));
1514 CHECK(outputFormat->findInt32("height", &height));
1515
1516 int32_t cropLeft, cropTop, cropRight, cropBottom;
1517 CHECK(outputFormat->findRect(
1518 "crop",
1519 &cropLeft, &cropTop, &cropRight, &cropBottom));
1520
1521 displayWidth = cropRight - cropLeft + 1;
1522 displayHeight = cropBottom - cropTop + 1;
1523
1524 ALOGV("Video output format changed to %d x %d "
1525 "(crop: %d x %d @ (%d, %d))",
1526 width, height,
1527 displayWidth,
1528 displayHeight,
1529 cropLeft, cropTop);
1530 } else {
1531 CHECK(inputFormat->findInt32("width", &displayWidth));
1532 CHECK(inputFormat->findInt32("height", &displayHeight));
1533
1534 ALOGV("Video input format %d x %d", displayWidth, displayHeight);
1535 }
1536
1537 // Take into account sample aspect ratio if necessary:
1538 int32_t sarWidth, sarHeight;
1539 if (inputFormat->findInt32("sar-width", &sarWidth)
1540 && inputFormat->findInt32("sar-height", &sarHeight)) {
1541 ALOGV("Sample aspect ratio %d : %d", sarWidth, sarHeight);
1542
1543 displayWidth = (displayWidth * sarWidth) / sarHeight;
1544
1545 ALOGV("display dimensions %d x %d", displayWidth, displayHeight);
1546 }
1547
1548 int32_t rotationDegrees;
1549 if (!inputFormat->findInt32("rotation-degrees", &rotationDegrees)) {
1550 rotationDegrees = 0;
1551 }
1552
1553 if (rotationDegrees == 90 || rotationDegrees == 270) {
1554 int32_t tmp = displayWidth;
1555 displayWidth = displayHeight;
1556 displayHeight = tmp;
1557 }
1558
1559 notifyListener(
1560 MEDIA_SET_VIDEO_SIZE,
1561 displayWidth,
1562 displayHeight);
1563}
1564
Chong Zhangdcb89b32013-08-06 09:44:47 -07001565void NuPlayer::notifyListener(int msg, int ext1, int ext2, const Parcel *in) {
Andreas Huber43c3e6c2011-01-05 12:17:08 -08001566 if (mDriver == NULL) {
Andreas Huberf9334412010-12-15 15:17:42 -08001567 return;
1568 }
1569
Andreas Huber43c3e6c2011-01-05 12:17:08 -08001570 sp<NuPlayerDriver> driver = mDriver.promote();
Andreas Huberf9334412010-12-15 15:17:42 -08001571
Andreas Huber43c3e6c2011-01-05 12:17:08 -08001572 if (driver == NULL) {
Andreas Huberf9334412010-12-15 15:17:42 -08001573 return;
1574 }
1575
Chong Zhangdcb89b32013-08-06 09:44:47 -07001576 driver->notifyListener(msg, ext1, ext2, in);
Andreas Huberf9334412010-12-15 15:17:42 -08001577}
1578
Lajos Molnar87603c02014-08-20 19:25:30 -07001579void NuPlayer::flushDecoder(
1580 bool audio, bool needShutdown, const sp<AMessage> &newFormat) {
Andreas Huber14f76722013-01-15 09:04:18 -08001581 ALOGV("[%s] flushDecoder needShutdown=%d",
1582 audio ? "audio" : "video", needShutdown);
1583
Lajos Molnar87603c02014-08-20 19:25:30 -07001584 const sp<Decoder> &decoder = getDecoder(audio);
1585 if (decoder == NULL) {
Steve Blockdf64d152012-01-04 20:05:49 +00001586 ALOGI("flushDecoder %s without decoder present",
Andreas Huber6e3d3112011-11-28 12:36:11 -08001587 audio ? "audio" : "video");
Lajos Molnar87603c02014-08-20 19:25:30 -07001588 return;
Andreas Huber6e3d3112011-11-28 12:36:11 -08001589 }
1590
Andreas Huber1aef2112011-01-04 14:01:29 -08001591 // Make sure we don't continue to scan sources until we finish flushing.
1592 ++mScanSourcesGeneration;
Andreas Huber43c3e6c2011-01-05 12:17:08 -08001593 mScanSourcesPending = false;
Andreas Huber1aef2112011-01-04 14:01:29 -08001594
Lajos Molnar87603c02014-08-20 19:25:30 -07001595 decoder->signalFlush(newFormat);
Andreas Huber1aef2112011-01-04 14:01:29 -08001596 mRenderer->flush(audio);
1597
1598 FlushStatus newStatus =
1599 needShutdown ? FLUSHING_DECODER_SHUTDOWN : FLUSHING_DECODER;
1600
Andy Hung8d121d42014-10-03 09:53:53 -07001601 mFlushComplete[audio][false /* isDecoder */] = false;
1602 mFlushComplete[audio][true /* isDecoder */] = false;
Andreas Huber1aef2112011-01-04 14:01:29 -08001603 if (audio) {
Wei Jia53904f32014-07-29 10:22:53 -07001604 ALOGE_IF(mFlushingAudio != NONE,
1605 "audio flushDecoder() is called in state %d", mFlushingAudio);
Andreas Huber1aef2112011-01-04 14:01:29 -08001606 mFlushingAudio = newStatus;
Andreas Huber1aef2112011-01-04 14:01:29 -08001607 } else {
Wei Jia53904f32014-07-29 10:22:53 -07001608 ALOGE_IF(mFlushingVideo != NONE,
1609 "video flushDecoder() is called in state %d", mFlushingVideo);
Andreas Huber1aef2112011-01-04 14:01:29 -08001610 mFlushingVideo = newStatus;
Chong Zhangb86e68f2014-08-01 13:46:53 -07001611
1612 if (mCCDecoder != NULL) {
1613 mCCDecoder->flush();
1614 }
Andreas Huber1aef2112011-01-04 14:01:29 -08001615 }
1616}
1617
Lajos Molnar87603c02014-08-20 19:25:30 -07001618void NuPlayer::updateDecoderFormatWithoutFlush(
1619 bool audio, const sp<AMessage> &format) {
1620 ALOGV("[%s] updateDecoderFormatWithoutFlush", audio ? "audio" : "video");
1621
1622 const sp<Decoder> &decoder = getDecoder(audio);
1623 if (decoder == NULL) {
1624 ALOGI("updateDecoderFormatWithoutFlush %s without decoder present",
1625 audio ? "audio" : "video");
1626 return;
1627 }
1628
1629 decoder->signalUpdateFormat(format);
1630}
1631
Chong Zhangced1c2f2014-08-08 15:22:35 -07001632void NuPlayer::queueDecoderShutdown(
1633 bool audio, bool video, const sp<AMessage> &reply) {
1634 ALOGI("queueDecoderShutdown audio=%d, video=%d", audio, video);
Andreas Huber84066782011-08-16 09:34:26 -07001635
Chong Zhangced1c2f2014-08-08 15:22:35 -07001636 mDeferredActions.push_back(
Wei Jiafef808d2014-10-31 17:57:05 -07001637 new FlushDecoderAction(
1638 audio ? FLUSH_CMD_SHUTDOWN : FLUSH_CMD_NONE,
1639 video ? FLUSH_CMD_SHUTDOWN : FLUSH_CMD_NONE));
Andreas Huber84066782011-08-16 09:34:26 -07001640
Chong Zhangced1c2f2014-08-08 15:22:35 -07001641 mDeferredActions.push_back(
1642 new SimpleAction(&NuPlayer::performScanSources));
Andreas Huber84066782011-08-16 09:34:26 -07001643
Chong Zhangced1c2f2014-08-08 15:22:35 -07001644 mDeferredActions.push_back(new PostMessageAction(reply));
1645
1646 processDeferredActions();
Andreas Huber84066782011-08-16 09:34:26 -07001647}
1648
James Dong0d268a32012-08-31 12:18:27 -07001649status_t NuPlayer::setVideoScalingMode(int32_t mode) {
1650 mVideoScalingMode = mode;
Andreas Huber57a339c2012-12-03 11:18:00 -08001651 if (mNativeWindow != NULL) {
James Dong0d268a32012-08-31 12:18:27 -07001652 status_t ret = native_window_set_scaling_mode(
1653 mNativeWindow->getNativeWindow().get(), mVideoScalingMode);
1654 if (ret != OK) {
1655 ALOGE("Failed to set scaling mode (%d): %s",
1656 -ret, strerror(-ret));
1657 return ret;
1658 }
1659 }
1660 return OK;
1661}
1662
Chong Zhangdcb89b32013-08-06 09:44:47 -07001663status_t NuPlayer::getTrackInfo(Parcel* reply) const {
1664 sp<AMessage> msg = new AMessage(kWhatGetTrackInfo, id());
1665 msg->setPointer("reply", reply);
1666
1667 sp<AMessage> response;
1668 status_t err = msg->postAndAwaitResponse(&response);
1669 return err;
1670}
1671
Robert Shih7c4f0d72014-07-09 18:53:31 -07001672status_t NuPlayer::getSelectedTrack(int32_t type, Parcel* reply) const {
1673 sp<AMessage> msg = new AMessage(kWhatGetSelectedTrack, id());
1674 msg->setPointer("reply", reply);
1675 msg->setInt32("type", type);
1676
1677 sp<AMessage> response;
1678 status_t err = msg->postAndAwaitResponse(&response);
1679 if (err == OK && response != NULL) {
1680 CHECK(response->findInt32("err", &err));
1681 }
1682 return err;
1683}
1684
Chong Zhangdcb89b32013-08-06 09:44:47 -07001685status_t NuPlayer::selectTrack(size_t trackIndex, bool select) {
1686 sp<AMessage> msg = new AMessage(kWhatSelectTrack, id());
1687 msg->setSize("trackIndex", trackIndex);
1688 msg->setInt32("select", select);
1689
1690 sp<AMessage> response;
1691 status_t err = msg->postAndAwaitResponse(&response);
1692
Chong Zhang404fced2014-06-11 14:45:31 -07001693 if (err != OK) {
1694 return err;
1695 }
1696
1697 if (!response->findInt32("err", &err)) {
1698 err = OK;
1699 }
1700
Chong Zhangdcb89b32013-08-06 09:44:47 -07001701 return err;
1702}
1703
Ronghua Wua73d9e02014-10-08 15:13:29 -07001704status_t NuPlayer::getCurrentPosition(int64_t *mediaUs) {
1705 sp<Renderer> renderer = mRenderer;
1706 if (renderer == NULL) {
1707 return NO_INIT;
1708 }
1709
1710 return renderer->getCurrentPosition(mediaUs);
1711}
1712
1713void NuPlayer::getStats(int64_t *numFramesTotal, int64_t *numFramesDropped) {
1714 *numFramesTotal = mNumFramesTotal;
1715 *numFramesDropped = mNumFramesDropped;
1716}
1717
Marco Nelissenf0b72b52014-09-16 15:43:44 -07001718sp<MetaData> NuPlayer::getFileMeta() {
1719 return mSource->getFileFormatMeta();
1720}
1721
Andreas Huberb7c8e912012-11-27 15:02:53 -08001722void NuPlayer::schedulePollDuration() {
1723 sp<AMessage> msg = new AMessage(kWhatPollDuration, id());
1724 msg->setInt32("generation", mPollDurationGeneration);
1725 msg->post();
1726}
1727
1728void NuPlayer::cancelPollDuration() {
1729 ++mPollDurationGeneration;
1730}
1731
Andreas Hubera1f8ab02012-11-30 10:53:22 -08001732void NuPlayer::processDeferredActions() {
1733 while (!mDeferredActions.empty()) {
1734 // We won't execute any deferred actions until we're no longer in
1735 // an intermediate state, i.e. one more more decoders are currently
1736 // flushing or shutting down.
1737
Andreas Hubera1f8ab02012-11-30 10:53:22 -08001738 if (mFlushingAudio != NONE || mFlushingVideo != NONE) {
1739 // We're currently flushing, postpone the reset until that's
1740 // completed.
1741
1742 ALOGV("postponing action mFlushingAudio=%d, mFlushingVideo=%d",
1743 mFlushingAudio, mFlushingVideo);
1744
1745 break;
1746 }
1747
1748 sp<Action> action = *mDeferredActions.begin();
1749 mDeferredActions.erase(mDeferredActions.begin());
1750
1751 action->execute(this);
1752 }
1753}
1754
Wei Jiae427abf2014-09-22 15:21:11 -07001755void NuPlayer::performSeek(int64_t seekTimeUs, bool needNotify) {
1756 ALOGV("performSeek seekTimeUs=%lld us (%.2f secs), needNotify(%d)",
Andreas Hubera1f8ab02012-11-30 10:53:22 -08001757 seekTimeUs,
Wei Jiae427abf2014-09-22 15:21:11 -07001758 seekTimeUs / 1E6,
1759 needNotify);
Andreas Hubera1f8ab02012-11-30 10:53:22 -08001760
Andy Hungadf34bf2014-09-03 18:22:22 -07001761 if (mSource == NULL) {
1762 // This happens when reset occurs right before the loop mode
1763 // asynchronously seeks to the start of the stream.
1764 LOG_ALWAYS_FATAL_IF(mAudioDecoder != NULL || mVideoDecoder != NULL,
1765 "mSource is NULL and decoders not NULL audio(%p) video(%p)",
1766 mAudioDecoder.get(), mVideoDecoder.get());
1767 return;
1768 }
Andreas Hubera1f8ab02012-11-30 10:53:22 -08001769 mSource->seekTo(seekTimeUs);
Robert Shihd3b0bbb2014-07-23 15:00:25 -07001770 ++mTimedTextGeneration;
Andreas Hubera1f8ab02012-11-30 10:53:22 -08001771
1772 if (mDriver != NULL) {
1773 sp<NuPlayerDriver> driver = mDriver.promote();
1774 if (driver != NULL) {
Wei Jiae427abf2014-09-22 15:21:11 -07001775 if (needNotify) {
1776 driver->notifySeekComplete();
1777 }
Andreas Hubera1f8ab02012-11-30 10:53:22 -08001778 }
1779 }
1780
1781 // everything's flushed, continue playback.
1782}
1783
Wei Jiafef808d2014-10-31 17:57:05 -07001784void NuPlayer::performDecoderFlush(FlushCommand audio, FlushCommand video) {
1785 ALOGV("performDecoderFlush audio=%d, video=%d", audio, video);
Andreas Hubera1f8ab02012-11-30 10:53:22 -08001786
Wei Jiafef808d2014-10-31 17:57:05 -07001787 if ((audio == FLUSH_CMD_NONE || mAudioDecoder == NULL)
1788 && (video == FLUSH_CMD_NONE || mVideoDecoder == NULL)) {
Andreas Hubera1f8ab02012-11-30 10:53:22 -08001789 return;
1790 }
1791
1792 mTimeDiscontinuityPending = true;
1793
Wei Jiafef808d2014-10-31 17:57:05 -07001794 if (audio != FLUSH_CMD_NONE && mAudioDecoder != NULL) {
1795 flushDecoder(true /* audio */, (audio == FLUSH_CMD_SHUTDOWN));
Andreas Hubera1f8ab02012-11-30 10:53:22 -08001796 }
1797
Wei Jiafef808d2014-10-31 17:57:05 -07001798 if (video != FLUSH_CMD_NONE && mVideoDecoder != NULL) {
1799 flushDecoder(false /* audio */, (video == FLUSH_CMD_SHUTDOWN));
Andreas Hubera1f8ab02012-11-30 10:53:22 -08001800 }
1801}
1802
1803void NuPlayer::performReset() {
1804 ALOGV("performReset");
1805
1806 CHECK(mAudioDecoder == NULL);
1807 CHECK(mVideoDecoder == NULL);
1808
1809 cancelPollDuration();
1810
1811 ++mScanSourcesGeneration;
1812 mScanSourcesPending = false;
1813
Lajos Molnar09524832014-07-17 14:29:51 -07001814 if (mRendererLooper != NULL) {
1815 if (mRenderer != NULL) {
1816 mRendererLooper->unregisterHandler(mRenderer->id());
1817 }
1818 mRendererLooper->stop();
1819 mRendererLooper.clear();
1820 }
Andreas Hubera1f8ab02012-11-30 10:53:22 -08001821 mRenderer.clear();
Wei Jia57568df2014-09-22 10:16:29 -07001822 ++mRendererGeneration;
Andreas Hubera1f8ab02012-11-30 10:53:22 -08001823
1824 if (mSource != NULL) {
1825 mSource->stop();
Andreas Huberb5f25f02013-02-05 10:14:26 -08001826
Andreas Hubera1f8ab02012-11-30 10:53:22 -08001827 mSource.clear();
1828 }
1829
1830 if (mDriver != NULL) {
1831 sp<NuPlayerDriver> driver = mDriver.promote();
1832 if (driver != NULL) {
1833 driver->notifyResetComplete();
1834 }
1835 }
Andreas Huber57a339c2012-12-03 11:18:00 -08001836
1837 mStarted = false;
Andreas Hubera1f8ab02012-11-30 10:53:22 -08001838}
1839
1840void NuPlayer::performScanSources() {
1841 ALOGV("performScanSources");
1842
Andreas Huber57a339c2012-12-03 11:18:00 -08001843 if (!mStarted) {
1844 return;
1845 }
1846
Andreas Hubera1f8ab02012-11-30 10:53:22 -08001847 if (mAudioDecoder == NULL || mVideoDecoder == NULL) {
1848 postScanSources();
1849 }
1850}
1851
Andreas Huber57a339c2012-12-03 11:18:00 -08001852void NuPlayer::performSetSurface(const sp<NativeWindowWrapper> &wrapper) {
1853 ALOGV("performSetSurface");
1854
1855 mNativeWindow = wrapper;
1856
1857 // XXX - ignore error from setVideoScalingMode for now
1858 setVideoScalingMode(mVideoScalingMode);
Chong Zhang13d6faa2014-08-22 15:35:28 -07001859
1860 if (mDriver != NULL) {
1861 sp<NuPlayerDriver> driver = mDriver.promote();
1862 if (driver != NULL) {
1863 driver->notifySetSurfaceComplete();
1864 }
1865 }
Andreas Huber57a339c2012-12-03 11:18:00 -08001866}
1867
Andreas Huber9575c962013-02-05 13:59:56 -08001868void NuPlayer::onSourceNotify(const sp<AMessage> &msg) {
1869 int32_t what;
1870 CHECK(msg->findInt32("what", &what));
1871
1872 switch (what) {
1873 case Source::kWhatPrepared:
1874 {
Andreas Huberb5f28d42013-04-25 15:11:19 -07001875 if (mSource == NULL) {
1876 // This is a stale notification from a source that was
1877 // asynchronously preparing when the client called reset().
1878 // We handled the reset, the source is gone.
1879 break;
1880 }
1881
Andreas Huberec0c5972013-02-05 14:47:13 -08001882 int32_t err;
1883 CHECK(msg->findInt32("err", &err));
1884
Andreas Huber9575c962013-02-05 13:59:56 -08001885 sp<NuPlayerDriver> driver = mDriver.promote();
1886 if (driver != NULL) {
Marco Nelissendd114d12014-05-28 15:23:14 -07001887 // notify duration first, so that it's definitely set when
1888 // the app received the "prepare complete" callback.
1889 int64_t durationUs;
1890 if (mSource->getDuration(&durationUs) == OK) {
1891 driver->notifyDuration(durationUs);
1892 }
Andreas Huberec0c5972013-02-05 14:47:13 -08001893 driver->notifyPrepareCompleted(err);
Andreas Huber9575c962013-02-05 13:59:56 -08001894 }
Andreas Huber99759402013-04-01 14:28:31 -07001895
Andreas Huber9575c962013-02-05 13:59:56 -08001896 break;
1897 }
1898
1899 case Source::kWhatFlagsChanged:
1900 {
1901 uint32_t flags;
1902 CHECK(msg->findInt32("flags", (int32_t *)&flags));
1903
Chong Zhang4b7069d2013-09-11 12:52:43 -07001904 sp<NuPlayerDriver> driver = mDriver.promote();
1905 if (driver != NULL) {
1906 driver->notifyFlagsChanged(flags);
1907 }
1908
Andreas Huber9575c962013-02-05 13:59:56 -08001909 if ((mSourceFlags & Source::FLAG_DYNAMIC_DURATION)
1910 && (!(flags & Source::FLAG_DYNAMIC_DURATION))) {
1911 cancelPollDuration();
1912 } else if (!(mSourceFlags & Source::FLAG_DYNAMIC_DURATION)
1913 && (flags & Source::FLAG_DYNAMIC_DURATION)
1914 && (mAudioDecoder != NULL || mVideoDecoder != NULL)) {
1915 schedulePollDuration();
1916 }
1917
1918 mSourceFlags = flags;
1919 break;
1920 }
1921
1922 case Source::kWhatVideoSizeChanged:
1923 {
Chong Zhangced1c2f2014-08-08 15:22:35 -07001924 sp<AMessage> format;
1925 CHECK(msg->findMessage("format", &format));
Andreas Huber9575c962013-02-05 13:59:56 -08001926
Chong Zhangced1c2f2014-08-08 15:22:35 -07001927 updateVideoSize(format);
Andreas Huber9575c962013-02-05 13:59:56 -08001928 break;
1929 }
1930
Chong Zhang2a3cc9a2014-08-21 17:48:26 -07001931 case Source::kWhatBufferingUpdate:
1932 {
1933 int32_t percentage;
1934 CHECK(msg->findInt32("percentage", &percentage));
1935
1936 notifyListener(MEDIA_BUFFERING_UPDATE, percentage, 0);
1937 break;
1938 }
1939
Roger Jönssonb50e83e2013-01-21 16:26:41 +01001940 case Source::kWhatBufferingStart:
1941 {
1942 notifyListener(MEDIA_INFO, MEDIA_INFO_BUFFERING_START, 0);
1943 break;
1944 }
1945
1946 case Source::kWhatBufferingEnd:
1947 {
1948 notifyListener(MEDIA_INFO, MEDIA_INFO_BUFFERING_END, 0);
1949 break;
1950 }
1951
Chong Zhangdcb89b32013-08-06 09:44:47 -07001952 case Source::kWhatSubtitleData:
1953 {
1954 sp<ABuffer> buffer;
1955 CHECK(msg->findBuffer("buffer", &buffer));
1956
Chong Zhang404fced2014-06-11 14:45:31 -07001957 sendSubtitleData(buffer, 0 /* baseIndex */);
Chong Zhangdcb89b32013-08-06 09:44:47 -07001958 break;
1959 }
1960
Robert Shihd3b0bbb2014-07-23 15:00:25 -07001961 case Source::kWhatTimedTextData:
1962 {
1963 int32_t generation;
1964 if (msg->findInt32("generation", &generation)
1965 && generation != mTimedTextGeneration) {
1966 break;
1967 }
1968
1969 sp<ABuffer> buffer;
1970 CHECK(msg->findBuffer("buffer", &buffer));
1971
1972 sp<NuPlayerDriver> driver = mDriver.promote();
1973 if (driver == NULL) {
1974 break;
1975 }
1976
1977 int posMs;
1978 int64_t timeUs, posUs;
1979 driver->getCurrentPosition(&posMs);
1980 posUs = posMs * 1000;
1981 CHECK(buffer->meta()->findInt64("timeUs", &timeUs));
1982
1983 if (posUs < timeUs) {
1984 if (!msg->findInt32("generation", &generation)) {
1985 msg->setInt32("generation", mTimedTextGeneration);
1986 }
1987 msg->post(timeUs - posUs);
1988 } else {
1989 sendTimedTextData(buffer);
1990 }
1991 break;
1992 }
1993
Andreas Huber14f76722013-01-15 09:04:18 -08001994 case Source::kWhatQueueDecoderShutdown:
1995 {
1996 int32_t audio, video;
1997 CHECK(msg->findInt32("audio", &audio));
1998 CHECK(msg->findInt32("video", &video));
1999
2000 sp<AMessage> reply;
2001 CHECK(msg->findMessage("reply", &reply));
2002
2003 queueDecoderShutdown(audio, video, reply);
2004 break;
2005 }
2006
Ronghua Wu80276872014-08-28 15:50:29 -07002007 case Source::kWhatDrmNoLicense:
2008 {
2009 notifyListener(MEDIA_ERROR, MEDIA_ERROR_UNKNOWN, ERROR_DRM_NO_LICENSE);
2010 break;
2011 }
2012
Andreas Huber9575c962013-02-05 13:59:56 -08002013 default:
2014 TRESPASS();
2015 }
2016}
2017
Chong Zhanga7fa1d92014-06-11 14:49:23 -07002018void NuPlayer::onClosedCaptionNotify(const sp<AMessage> &msg) {
2019 int32_t what;
2020 CHECK(msg->findInt32("what", &what));
2021
2022 switch (what) {
2023 case NuPlayer::CCDecoder::kWhatClosedCaptionData:
2024 {
2025 sp<ABuffer> buffer;
2026 CHECK(msg->findBuffer("buffer", &buffer));
2027
2028 size_t inbandTracks = 0;
2029 if (mSource != NULL) {
2030 inbandTracks = mSource->getTrackCount();
2031 }
2032
2033 sendSubtitleData(buffer, inbandTracks);
2034 break;
2035 }
2036
2037 case NuPlayer::CCDecoder::kWhatTrackAdded:
2038 {
2039 notifyListener(MEDIA_INFO, MEDIA_INFO_METADATA_UPDATE, 0);
2040
2041 break;
2042 }
2043
2044 default:
2045 TRESPASS();
2046 }
2047
2048
2049}
2050
Chong Zhang404fced2014-06-11 14:45:31 -07002051void NuPlayer::sendSubtitleData(const sp<ABuffer> &buffer, int32_t baseIndex) {
2052 int32_t trackIndex;
2053 int64_t timeUs, durationUs;
2054 CHECK(buffer->meta()->findInt32("trackIndex", &trackIndex));
2055 CHECK(buffer->meta()->findInt64("timeUs", &timeUs));
2056 CHECK(buffer->meta()->findInt64("durationUs", &durationUs));
2057
2058 Parcel in;
2059 in.writeInt32(trackIndex + baseIndex);
2060 in.writeInt64(timeUs);
2061 in.writeInt64(durationUs);
2062 in.writeInt32(buffer->size());
2063 in.writeInt32(buffer->size());
2064 in.write(buffer->data(), buffer->size());
2065
2066 notifyListener(MEDIA_SUBTITLE_DATA, 0, 0, &in);
2067}
Robert Shihd3b0bbb2014-07-23 15:00:25 -07002068
2069void NuPlayer::sendTimedTextData(const sp<ABuffer> &buffer) {
2070 const void *data;
2071 size_t size = 0;
2072 int64_t timeUs;
2073 int32_t flag = TextDescriptions::LOCAL_DESCRIPTIONS;
2074
2075 AString mime;
2076 CHECK(buffer->meta()->findString("mime", &mime));
2077 CHECK(strcasecmp(mime.c_str(), MEDIA_MIMETYPE_TEXT_3GPP) == 0);
2078
2079 data = buffer->data();
2080 size = buffer->size();
2081
2082 Parcel parcel;
2083 if (size > 0) {
2084 CHECK(buffer->meta()->findInt64("timeUs", &timeUs));
2085 flag |= TextDescriptions::IN_BAND_TEXT_3GPP;
2086 TextDescriptions::getParcelOfDescriptions(
2087 (const uint8_t *)data, size, flag, timeUs / 1000, &parcel);
2088 }
2089
2090 if ((parcel.dataSize() > 0)) {
2091 notifyListener(MEDIA_TIMED_TEXT, 0, 0, &parcel);
2092 } else { // send an empty timed text
2093 notifyListener(MEDIA_TIMED_TEXT, 0, 0);
2094 }
2095}
Andreas Huberb5f25f02013-02-05 10:14:26 -08002096////////////////////////////////////////////////////////////////////////////////
2097
Chong Zhangced1c2f2014-08-08 15:22:35 -07002098sp<AMessage> NuPlayer::Source::getFormat(bool audio) {
2099 sp<MetaData> meta = getFormatMeta(audio);
2100
2101 if (meta == NULL) {
2102 return NULL;
2103 }
2104
2105 sp<AMessage> msg = new AMessage;
2106
2107 if(convertMetaDataToMessage(meta, &msg) == OK) {
2108 return msg;
2109 }
2110 return NULL;
2111}
2112
Andreas Huber9575c962013-02-05 13:59:56 -08002113void NuPlayer::Source::notifyFlagsChanged(uint32_t flags) {
2114 sp<AMessage> notify = dupNotify();
2115 notify->setInt32("what", kWhatFlagsChanged);
2116 notify->setInt32("flags", flags);
2117 notify->post();
2118}
2119
Chong Zhangced1c2f2014-08-08 15:22:35 -07002120void NuPlayer::Source::notifyVideoSizeChanged(const sp<AMessage> &format) {
Andreas Huber9575c962013-02-05 13:59:56 -08002121 sp<AMessage> notify = dupNotify();
2122 notify->setInt32("what", kWhatVideoSizeChanged);
Chong Zhangced1c2f2014-08-08 15:22:35 -07002123 notify->setMessage("format", format);
Andreas Huber9575c962013-02-05 13:59:56 -08002124 notify->post();
2125}
2126
Andreas Huberec0c5972013-02-05 14:47:13 -08002127void NuPlayer::Source::notifyPrepared(status_t err) {
Andreas Huber9575c962013-02-05 13:59:56 -08002128 sp<AMessage> notify = dupNotify();
2129 notify->setInt32("what", kWhatPrepared);
Andreas Huberec0c5972013-02-05 14:47:13 -08002130 notify->setInt32("err", err);
Andreas Huber9575c962013-02-05 13:59:56 -08002131 notify->post();
2132}
2133
Andreas Huber84333e02014-02-07 15:36:10 -08002134void NuPlayer::Source::onMessageReceived(const sp<AMessage> & /* msg */) {
Andreas Huberb5f25f02013-02-05 10:14:26 -08002135 TRESPASS();
2136}
2137
Andreas Huberf9334412010-12-15 15:17:42 -08002138} // namespace android