blob: cdbea9fe6bf4826205e845c2a882f007aec1d890 [file] [log] [blame]
Andreas Huberf9334412010-12-15 15:17:42 -08001/*
2 * Copyright (C) 2010 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17//#define LOG_NDEBUG 0
18#define LOG_TAG "NuPlayer"
19#include <utils/Log.h>
20
21#include "NuPlayer.h"
Andreas Huber5bc087c2010-12-23 10:27:40 -080022
23#include "HTTPLiveSource.h"
Andreas Huberf9334412010-12-15 15:17:42 -080024#include "NuPlayerDecoder.h"
Wei Jiabc2fb722014-07-08 16:37:57 -070025#include "NuPlayerDecoderPassThrough.h"
Andreas Huber43c3e6c2011-01-05 12:17:08 -080026#include "NuPlayerDriver.h"
Andreas Huberf9334412010-12-15 15:17:42 -080027#include "NuPlayerRenderer.h"
Andreas Huber5bc087c2010-12-23 10:27:40 -080028#include "NuPlayerSource.h"
Andreas Huber2bfdd422011-10-11 15:24:07 -070029#include "RTSPSource.h"
Andreas Huber5bc087c2010-12-23 10:27:40 -080030#include "StreamingSource.h"
Andreas Huberafed0e12011-09-20 15:39:58 -070031#include "GenericSource.h"
Robert Shihd3b0bbb2014-07-23 15:00:25 -070032#include "TextDescriptions.h"
Andreas Huber5bc087c2010-12-23 10:27:40 -080033
34#include "ATSParser.h"
Andreas Huberf9334412010-12-15 15:17:42 -080035
Andreas Huber3831a062010-12-21 10:22:33 -080036#include <media/stagefright/foundation/hexdump.h>
Andreas Huberf9334412010-12-15 15:17:42 -080037#include <media/stagefright/foundation/ABuffer.h>
38#include <media/stagefright/foundation/ADebug.h>
39#include <media/stagefright/foundation/AMessage.h>
Lajos Molnar09524832014-07-17 14:29:51 -070040#include <media/stagefright/MediaBuffer.h>
Andreas Huber3fe62152011-09-16 15:09:22 -070041#include <media/stagefright/MediaDefs.h>
Andreas Huberf9334412010-12-15 15:17:42 -080042#include <media/stagefright/MediaErrors.h>
43#include <media/stagefright/MetaData.h>
Andy McFadden8ba01022012-12-18 09:46:54 -080044#include <gui/IGraphicBufferProducer.h>
Andreas Huberf9334412010-12-15 15:17:42 -080045
Andreas Huber3fe62152011-09-16 15:09:22 -070046#include "avc_utils.h"
47
Andreas Huber84066782011-08-16 09:34:26 -070048#include "ESDS.h"
49#include <media/stagefright/Utils.h>
50
Andreas Huberf9334412010-12-15 15:17:42 -080051namespace android {
52
Phil Burkc5cc2e22014-09-09 20:08:39 -070053// TODO optimize buffer size for power consumption
54// The offload read buffer size is 32 KB but 24 KB uses less power.
55const size_t NuPlayer::kAggregateBufferSizeBytes = 24 * 1024;
56
Andreas Hubera1f8ab02012-11-30 10:53:22 -080057struct NuPlayer::Action : public RefBase {
58 Action() {}
59
60 virtual void execute(NuPlayer *player) = 0;
61
62private:
63 DISALLOW_EVIL_CONSTRUCTORS(Action);
64};
65
66struct NuPlayer::SeekAction : public Action {
67 SeekAction(int64_t seekTimeUs)
68 : mSeekTimeUs(seekTimeUs) {
69 }
70
71 virtual void execute(NuPlayer *player) {
72 player->performSeek(mSeekTimeUs);
73 }
74
75private:
76 int64_t mSeekTimeUs;
77
78 DISALLOW_EVIL_CONSTRUCTORS(SeekAction);
79};
80
Andreas Huber57a339c2012-12-03 11:18:00 -080081struct NuPlayer::SetSurfaceAction : public Action {
82 SetSurfaceAction(const sp<NativeWindowWrapper> &wrapper)
83 : mWrapper(wrapper) {
84 }
85
86 virtual void execute(NuPlayer *player) {
87 player->performSetSurface(mWrapper);
88 }
89
90private:
91 sp<NativeWindowWrapper> mWrapper;
92
93 DISALLOW_EVIL_CONSTRUCTORS(SetSurfaceAction);
94};
95
Andreas Huber14f76722013-01-15 09:04:18 -080096struct NuPlayer::ShutdownDecoderAction : public Action {
97 ShutdownDecoderAction(bool audio, bool video)
98 : mAudio(audio),
99 mVideo(video) {
100 }
101
102 virtual void execute(NuPlayer *player) {
103 player->performDecoderShutdown(mAudio, mVideo);
104 }
105
106private:
107 bool mAudio;
108 bool mVideo;
109
110 DISALLOW_EVIL_CONSTRUCTORS(ShutdownDecoderAction);
111};
112
113struct NuPlayer::PostMessageAction : public Action {
114 PostMessageAction(const sp<AMessage> &msg)
115 : mMessage(msg) {
116 }
117
118 virtual void execute(NuPlayer *) {
119 mMessage->post();
120 }
121
122private:
123 sp<AMessage> mMessage;
124
125 DISALLOW_EVIL_CONSTRUCTORS(PostMessageAction);
126};
127
Andreas Hubera1f8ab02012-11-30 10:53:22 -0800128// Use this if there's no state necessary to save in order to execute
129// the action.
130struct NuPlayer::SimpleAction : public Action {
131 typedef void (NuPlayer::*ActionFunc)();
132
133 SimpleAction(ActionFunc func)
134 : mFunc(func) {
135 }
136
137 virtual void execute(NuPlayer *player) {
138 (player->*mFunc)();
139 }
140
141private:
142 ActionFunc mFunc;
143
144 DISALLOW_EVIL_CONSTRUCTORS(SimpleAction);
145};
146
Andreas Huberf9334412010-12-15 15:17:42 -0800147////////////////////////////////////////////////////////////////////////////////
148
149NuPlayer::NuPlayer()
Andreas Huber9b80c2b2011-06-30 15:47:02 -0700150 : mUIDValid(false),
Andreas Huber9575c962013-02-05 13:59:56 -0800151 mSourceFlags(0),
Wei Jiaac428aa2014-09-02 19:01:34 -0700152 mCurrentPositionUs(0),
Andreas Huber3fe62152011-09-16 15:09:22 -0700153 mVideoIsAVC(false),
Wei Jiabc2fb722014-07-08 16:37:57 -0700154 mOffloadAudio(false),
Andy Hung282a7e32014-08-14 15:56:34 -0700155 mCurrentOffloadInfo(AUDIO_INFO_INITIALIZER),
Wei Jia88703c32014-08-06 11:24:07 -0700156 mAudioDecoderGeneration(0),
157 mVideoDecoderGeneration(0),
Andreas Huber9b80c2b2011-06-30 15:47:02 -0700158 mAudioEOS(false),
Andreas Huberf9334412010-12-15 15:17:42 -0800159 mVideoEOS(false),
Andreas Huber5bc087c2010-12-23 10:27:40 -0800160 mScanSourcesPending(false),
Andreas Huber1aef2112011-01-04 14:01:29 -0800161 mScanSourcesGeneration(0),
Andreas Huberb7c8e912012-11-27 15:02:53 -0800162 mPollDurationGeneration(0),
Robert Shihd3b0bbb2014-07-23 15:00:25 -0700163 mTimedTextGeneration(0),
Andreas Huber6e3d3112011-11-28 12:36:11 -0800164 mTimeDiscontinuityPending(false),
Andreas Huberf9334412010-12-15 15:17:42 -0800165 mFlushingAudio(NONE),
Andreas Huber1aef2112011-01-04 14:01:29 -0800166 mFlushingVideo(NONE),
Andreas Huber3fe62152011-09-16 15:09:22 -0700167 mSkipRenderingAudioUntilMediaTimeUs(-1ll),
168 mSkipRenderingVideoUntilMediaTimeUs(-1ll),
169 mVideoLateByUs(0ll),
170 mNumFramesTotal(0ll),
James Dong0d268a32012-08-31 12:18:27 -0700171 mNumFramesDropped(0ll),
Andreas Huber57a339c2012-12-03 11:18:00 -0800172 mVideoScalingMode(NATIVE_WINDOW_SCALING_MODE_SCALE_TO_WINDOW),
173 mStarted(false) {
Andreas Huberf9334412010-12-15 15:17:42 -0800174}
175
176NuPlayer::~NuPlayer() {
177}
178
Andreas Huber9b80c2b2011-06-30 15:47:02 -0700179void NuPlayer::setUID(uid_t uid) {
180 mUIDValid = true;
181 mUID = uid;
182}
183
Andreas Huber43c3e6c2011-01-05 12:17:08 -0800184void NuPlayer::setDriver(const wp<NuPlayerDriver> &driver) {
185 mDriver = driver;
Andreas Huberf9334412010-12-15 15:17:42 -0800186}
187
Andreas Huber9575c962013-02-05 13:59:56 -0800188void NuPlayer::setDataSourceAsync(const sp<IStreamSource> &source) {
Andreas Huberf9334412010-12-15 15:17:42 -0800189 sp<AMessage> msg = new AMessage(kWhatSetDataSource, id());
190
Andreas Huberb5f25f02013-02-05 10:14:26 -0800191 sp<AMessage> notify = new AMessage(kWhatSourceNotify, id());
192
Andreas Huber240abcc2014-02-13 13:32:37 -0800193 msg->setObject("source", new StreamingSource(notify, source));
Andreas Huber5bc087c2010-12-23 10:27:40 -0800194 msg->post();
195}
Andreas Huberf9334412010-12-15 15:17:42 -0800196
Andreas Huberafed0e12011-09-20 15:39:58 -0700197static bool IsHTTPLiveURL(const char *url) {
198 if (!strncasecmp("http://", url, 7)
Andreas Huber99759402013-04-01 14:28:31 -0700199 || !strncasecmp("https://", url, 8)
200 || !strncasecmp("file://", url, 7)) {
Andreas Huberafed0e12011-09-20 15:39:58 -0700201 size_t len = strlen(url);
202 if (len >= 5 && !strcasecmp(".m3u8", &url[len - 5])) {
203 return true;
204 }
205
206 if (strstr(url,"m3u8")) {
207 return true;
208 }
209 }
210
211 return false;
212}
213
Andreas Huber9575c962013-02-05 13:59:56 -0800214void NuPlayer::setDataSourceAsync(
Andreas Huber1b86fe02014-01-29 11:13:26 -0800215 const sp<IMediaHTTPService> &httpService,
216 const char *url,
217 const KeyedVector<String8, String8> *headers) {
Chong Zhang3de157d2014-08-05 20:54:44 -0700218
Andreas Huber5bc087c2010-12-23 10:27:40 -0800219 sp<AMessage> msg = new AMessage(kWhatSetDataSource, id());
Oscar Rydhé7a33b772012-02-20 10:15:48 +0100220 size_t len = strlen(url);
Andreas Huber5bc087c2010-12-23 10:27:40 -0800221
Andreas Huberb5f25f02013-02-05 10:14:26 -0800222 sp<AMessage> notify = new AMessage(kWhatSourceNotify, id());
223
Andreas Huberafed0e12011-09-20 15:39:58 -0700224 sp<Source> source;
225 if (IsHTTPLiveURL(url)) {
Andreas Huber81e68442014-02-05 11:52:33 -0800226 source = new HTTPLiveSource(notify, httpService, url, headers);
Andreas Huberafed0e12011-09-20 15:39:58 -0700227 } else if (!strncasecmp(url, "rtsp://", 7)) {
Andreas Huber1b86fe02014-01-29 11:13:26 -0800228 source = new RTSPSource(
229 notify, httpService, url, headers, mUIDValid, mUID);
Oscar Rydhé7a33b772012-02-20 10:15:48 +0100230 } else if ((!strncasecmp(url, "http://", 7)
231 || !strncasecmp(url, "https://", 8))
232 && ((len >= 4 && !strcasecmp(".sdp", &url[len - 4]))
233 || strstr(url, ".sdp?"))) {
Andreas Huber1b86fe02014-01-29 11:13:26 -0800234 source = new RTSPSource(
235 notify, httpService, url, headers, mUIDValid, mUID, true);
Andreas Huber2bfdd422011-10-11 15:24:07 -0700236 } else {
Chong Zhang3de157d2014-08-05 20:54:44 -0700237 sp<GenericSource> genericSource =
238 new GenericSource(notify, mUIDValid, mUID);
239 // Don't set FLAG_SECURE on mSourceFlags here for widevine.
240 // The correct flags will be updated in Source::kWhatFlagsChanged
241 // handler when GenericSource is prepared.
Andreas Huber2bfdd422011-10-11 15:24:07 -0700242
Chong Zhanga19f33e2014-08-07 15:35:07 -0700243 status_t err = genericSource->setDataSource(httpService, url, headers);
Chong Zhang3de157d2014-08-05 20:54:44 -0700244
245 if (err == OK) {
246 source = genericSource;
247 } else {
Chong Zhanga19f33e2014-08-07 15:35:07 -0700248 ALOGE("Failed to set data source!");
Chong Zhang3de157d2014-08-05 20:54:44 -0700249 }
250 }
Andreas Huberafed0e12011-09-20 15:39:58 -0700251 msg->setObject("source", source);
252 msg->post();
253}
254
Andreas Huber9575c962013-02-05 13:59:56 -0800255void NuPlayer::setDataSourceAsync(int fd, int64_t offset, int64_t length) {
Andreas Huberafed0e12011-09-20 15:39:58 -0700256 sp<AMessage> msg = new AMessage(kWhatSetDataSource, id());
257
Andreas Huberb5f25f02013-02-05 10:14:26 -0800258 sp<AMessage> notify = new AMessage(kWhatSourceNotify, id());
259
Chong Zhang3de157d2014-08-05 20:54:44 -0700260 sp<GenericSource> source =
261 new GenericSource(notify, mUIDValid, mUID);
262
Chong Zhanga19f33e2014-08-07 15:35:07 -0700263 status_t err = source->setDataSource(fd, offset, length);
Chong Zhang3de157d2014-08-05 20:54:44 -0700264
265 if (err != OK) {
Chong Zhanga19f33e2014-08-07 15:35:07 -0700266 ALOGE("Failed to set data source!");
Chong Zhang3de157d2014-08-05 20:54:44 -0700267 source = NULL;
268 }
269
Andreas Huberafed0e12011-09-20 15:39:58 -0700270 msg->setObject("source", source);
Andreas Huberf9334412010-12-15 15:17:42 -0800271 msg->post();
272}
273
Andreas Huber9575c962013-02-05 13:59:56 -0800274void NuPlayer::prepareAsync() {
275 (new AMessage(kWhatPrepare, id()))->post();
276}
277
Andreas Huber57a339c2012-12-03 11:18:00 -0800278void NuPlayer::setVideoSurfaceTextureAsync(
Andy McFadden8ba01022012-12-18 09:46:54 -0800279 const sp<IGraphicBufferProducer> &bufferProducer) {
Glenn Kasten11731182011-02-08 17:26:17 -0800280 sp<AMessage> msg = new AMessage(kWhatSetVideoNativeWindow, id());
Andreas Huber57a339c2012-12-03 11:18:00 -0800281
Andy McFadden8ba01022012-12-18 09:46:54 -0800282 if (bufferProducer == NULL) {
Andreas Huber57a339c2012-12-03 11:18:00 -0800283 msg->setObject("native-window", NULL);
284 } else {
285 msg->setObject(
286 "native-window",
287 new NativeWindowWrapper(
Wei Jia9c03a402014-08-26 15:24:43 -0700288 new Surface(bufferProducer, true /* controlledByApp */)));
Andreas Huber57a339c2012-12-03 11:18:00 -0800289 }
290
Andreas Huberf9334412010-12-15 15:17:42 -0800291 msg->post();
292}
293
294void NuPlayer::setAudioSink(const sp<MediaPlayerBase::AudioSink> &sink) {
295 sp<AMessage> msg = new AMessage(kWhatSetAudioSink, id());
296 msg->setObject("sink", sink);
297 msg->post();
298}
299
300void NuPlayer::start() {
301 (new AMessage(kWhatStart, id()))->post();
302}
303
Andreas Huber43c3e6c2011-01-05 12:17:08 -0800304void NuPlayer::pause() {
Andreas Huberb4082222011-01-20 15:23:04 -0800305 (new AMessage(kWhatPause, id()))->post();
Andreas Huber43c3e6c2011-01-05 12:17:08 -0800306}
307
308void NuPlayer::resume() {
Andreas Huberb4082222011-01-20 15:23:04 -0800309 (new AMessage(kWhatResume, id()))->post();
Andreas Huber43c3e6c2011-01-05 12:17:08 -0800310}
311
Andreas Huber1aef2112011-01-04 14:01:29 -0800312void NuPlayer::resetAsync() {
Chong Zhang48296b72014-09-14 14:28:45 -0700313 if (mSource != NULL) {
314 // During a reset, the data source might be unresponsive already, we need to
315 // disconnect explicitly so that reads exit promptly.
316 // We can't queue the disconnect request to the looper, as it might be
317 // queued behind a stuck read and never gets processed.
318 // Doing a disconnect outside the looper to allows the pending reads to exit
319 // (either successfully or with error).
320 mSource->disconnect();
321 }
322
Andreas Huber1aef2112011-01-04 14:01:29 -0800323 (new AMessage(kWhatReset, id()))->post();
324}
325
Andreas Huber43c3e6c2011-01-05 12:17:08 -0800326void NuPlayer::seekToAsync(int64_t seekTimeUs) {
327 sp<AMessage> msg = new AMessage(kWhatSeek, id());
328 msg->setInt64("seekTimeUs", seekTimeUs);
329 msg->post();
330}
331
Andreas Huber53df1a42010-12-22 10:03:04 -0800332// static
Andreas Huber1aef2112011-01-04 14:01:29 -0800333bool NuPlayer::IsFlushingState(FlushStatus state, bool *needShutdown) {
Andreas Huber53df1a42010-12-22 10:03:04 -0800334 switch (state) {
335 case FLUSHING_DECODER:
Andreas Huber1aef2112011-01-04 14:01:29 -0800336 if (needShutdown != NULL) {
337 *needShutdown = false;
Andreas Huber53df1a42010-12-22 10:03:04 -0800338 }
339 return true;
340
Andreas Huber1aef2112011-01-04 14:01:29 -0800341 case FLUSHING_DECODER_SHUTDOWN:
342 if (needShutdown != NULL) {
343 *needShutdown = true;
Andreas Huber53df1a42010-12-22 10:03:04 -0800344 }
345 return true;
346
347 default:
348 return false;
349 }
350}
351
Chong Zhang404fced2014-06-11 14:45:31 -0700352void NuPlayer::writeTrackInfo(
353 Parcel* reply, const sp<AMessage> format) const {
354 int32_t trackType;
355 CHECK(format->findInt32("type", &trackType));
356
357 AString lang;
358 CHECK(format->findString("language", &lang));
359
360 reply->writeInt32(2); // write something non-zero
361 reply->writeInt32(trackType);
362 reply->writeString16(String16(lang.c_str()));
363
364 if (trackType == MEDIA_TRACK_TYPE_SUBTITLE) {
365 AString mime;
366 CHECK(format->findString("mime", &mime));
367
368 int32_t isAuto, isDefault, isForced;
369 CHECK(format->findInt32("auto", &isAuto));
370 CHECK(format->findInt32("default", &isDefault));
371 CHECK(format->findInt32("forced", &isForced));
372
373 reply->writeString16(String16(mime.c_str()));
374 reply->writeInt32(isAuto);
375 reply->writeInt32(isDefault);
376 reply->writeInt32(isForced);
377 }
378}
379
Andreas Huberf9334412010-12-15 15:17:42 -0800380void NuPlayer::onMessageReceived(const sp<AMessage> &msg) {
381 switch (msg->what()) {
382 case kWhatSetDataSource:
383 {
Steve Block3856b092011-10-20 11:56:00 +0100384 ALOGV("kWhatSetDataSource");
Andreas Huberf9334412010-12-15 15:17:42 -0800385
386 CHECK(mSource == NULL);
387
Chong Zhang3de157d2014-08-05 20:54:44 -0700388 status_t err = OK;
Andreas Huber5bc087c2010-12-23 10:27:40 -0800389 sp<RefBase> obj;
390 CHECK(msg->findObject("source", &obj));
Chong Zhang3de157d2014-08-05 20:54:44 -0700391 if (obj != NULL) {
392 mSource = static_cast<Source *>(obj.get());
Chong Zhang3de157d2014-08-05 20:54:44 -0700393 } else {
394 err = UNKNOWN_ERROR;
395 }
Andreas Huber9575c962013-02-05 13:59:56 -0800396
397 CHECK(mDriver != NULL);
398 sp<NuPlayerDriver> driver = mDriver.promote();
399 if (driver != NULL) {
Chong Zhang3de157d2014-08-05 20:54:44 -0700400 driver->notifySetDataSourceCompleted(err);
Andreas Huber9575c962013-02-05 13:59:56 -0800401 }
402 break;
403 }
404
405 case kWhatPrepare:
406 {
407 mSource->prepareAsync();
Andreas Huberf9334412010-12-15 15:17:42 -0800408 break;
409 }
410
Chong Zhangdcb89b32013-08-06 09:44:47 -0700411 case kWhatGetTrackInfo:
412 {
413 uint32_t replyID;
414 CHECK(msg->senderAwaitsResponse(&replyID));
415
Chong Zhang404fced2014-06-11 14:45:31 -0700416 Parcel* reply;
417 CHECK(msg->findPointer("reply", (void**)&reply));
418
419 size_t inbandTracks = 0;
Chong Zhangdcb89b32013-08-06 09:44:47 -0700420 if (mSource != NULL) {
Chong Zhang404fced2014-06-11 14:45:31 -0700421 inbandTracks = mSource->getTrackCount();
422 }
423
Chong Zhanga7fa1d92014-06-11 14:49:23 -0700424 size_t ccTracks = 0;
425 if (mCCDecoder != NULL) {
426 ccTracks = mCCDecoder->getTrackCount();
427 }
428
Chong Zhang404fced2014-06-11 14:45:31 -0700429 // total track count
Chong Zhanga7fa1d92014-06-11 14:49:23 -0700430 reply->writeInt32(inbandTracks + ccTracks);
Chong Zhang404fced2014-06-11 14:45:31 -0700431
432 // write inband tracks
433 for (size_t i = 0; i < inbandTracks; ++i) {
434 writeTrackInfo(reply, mSource->getTrackInfo(i));
Chong Zhangdcb89b32013-08-06 09:44:47 -0700435 }
436
Chong Zhanga7fa1d92014-06-11 14:49:23 -0700437 // write CC track
438 for (size_t i = 0; i < ccTracks; ++i) {
439 writeTrackInfo(reply, mCCDecoder->getTrackInfo(i));
440 }
441
Chong Zhangdcb89b32013-08-06 09:44:47 -0700442 sp<AMessage> response = new AMessage;
Chong Zhangdcb89b32013-08-06 09:44:47 -0700443 response->postReply(replyID);
444 break;
445 }
446
Robert Shih7c4f0d72014-07-09 18:53:31 -0700447 case kWhatGetSelectedTrack:
448 {
449 status_t err = INVALID_OPERATION;
450 if (mSource != NULL) {
451 err = OK;
452
453 int32_t type32;
454 CHECK(msg->findInt32("type", (int32_t*)&type32));
455 media_track_type type = (media_track_type)type32;
456 ssize_t selectedTrack = mSource->getSelectedTrack(type);
457
458 Parcel* reply;
459 CHECK(msg->findPointer("reply", (void**)&reply));
460 reply->writeInt32(selectedTrack);
461 }
462
463 sp<AMessage> response = new AMessage;
464 response->setInt32("err", err);
465
466 uint32_t replyID;
467 CHECK(msg->senderAwaitsResponse(&replyID));
468 response->postReply(replyID);
469 break;
470 }
471
Chong Zhangdcb89b32013-08-06 09:44:47 -0700472 case kWhatSelectTrack:
473 {
474 uint32_t replyID;
475 CHECK(msg->senderAwaitsResponse(&replyID));
476
Chong Zhang404fced2014-06-11 14:45:31 -0700477 size_t trackIndex;
478 int32_t select;
479 CHECK(msg->findSize("trackIndex", &trackIndex));
480 CHECK(msg->findInt32("select", &select));
481
Chong Zhangdcb89b32013-08-06 09:44:47 -0700482 status_t err = INVALID_OPERATION;
Chong Zhang404fced2014-06-11 14:45:31 -0700483
484 size_t inbandTracks = 0;
Chong Zhangdcb89b32013-08-06 09:44:47 -0700485 if (mSource != NULL) {
Chong Zhang404fced2014-06-11 14:45:31 -0700486 inbandTracks = mSource->getTrackCount();
487 }
Chong Zhanga7fa1d92014-06-11 14:49:23 -0700488 size_t ccTracks = 0;
489 if (mCCDecoder != NULL) {
490 ccTracks = mCCDecoder->getTrackCount();
491 }
Chong Zhang404fced2014-06-11 14:45:31 -0700492
493 if (trackIndex < inbandTracks) {
Chong Zhangdcb89b32013-08-06 09:44:47 -0700494 err = mSource->selectTrack(trackIndex, select);
Robert Shihd3b0bbb2014-07-23 15:00:25 -0700495
496 if (!select && err == OK) {
497 int32_t type;
498 sp<AMessage> info = mSource->getTrackInfo(trackIndex);
499 if (info != NULL
500 && info->findInt32("type", &type)
501 && type == MEDIA_TRACK_TYPE_TIMEDTEXT) {
502 ++mTimedTextGeneration;
503 }
504 }
Chong Zhanga7fa1d92014-06-11 14:49:23 -0700505 } else {
506 trackIndex -= inbandTracks;
507
508 if (trackIndex < ccTracks) {
509 err = mCCDecoder->selectTrack(trackIndex, select);
510 }
Chong Zhangdcb89b32013-08-06 09:44:47 -0700511 }
512
513 sp<AMessage> response = new AMessage;
514 response->setInt32("err", err);
515
516 response->postReply(replyID);
517 break;
518 }
519
Andreas Huberb7c8e912012-11-27 15:02:53 -0800520 case kWhatPollDuration:
521 {
522 int32_t generation;
523 CHECK(msg->findInt32("generation", &generation));
524
525 if (generation != mPollDurationGeneration) {
526 // stale
527 break;
528 }
529
530 int64_t durationUs;
531 if (mDriver != NULL && mSource->getDuration(&durationUs) == OK) {
532 sp<NuPlayerDriver> driver = mDriver.promote();
533 if (driver != NULL) {
534 driver->notifyDuration(durationUs);
535 }
536 }
537
538 msg->post(1000000ll); // poll again in a second.
539 break;
540 }
541
Glenn Kasten11731182011-02-08 17:26:17 -0800542 case kWhatSetVideoNativeWindow:
Andreas Huberf9334412010-12-15 15:17:42 -0800543 {
Steve Block3856b092011-10-20 11:56:00 +0100544 ALOGV("kWhatSetVideoNativeWindow");
Andreas Huberf9334412010-12-15 15:17:42 -0800545
Andreas Huber57a339c2012-12-03 11:18:00 -0800546 mDeferredActions.push_back(
Andreas Huber14f76722013-01-15 09:04:18 -0800547 new ShutdownDecoderAction(
548 false /* audio */, true /* video */));
Andreas Huber57a339c2012-12-03 11:18:00 -0800549
Andreas Huberf9334412010-12-15 15:17:42 -0800550 sp<RefBase> obj;
Glenn Kasten11731182011-02-08 17:26:17 -0800551 CHECK(msg->findObject("native-window", &obj));
Andreas Huberf9334412010-12-15 15:17:42 -0800552
Andreas Huber57a339c2012-12-03 11:18:00 -0800553 mDeferredActions.push_back(
554 new SetSurfaceAction(
555 static_cast<NativeWindowWrapper *>(obj.get())));
James Dong0d268a32012-08-31 12:18:27 -0700556
Andreas Huber57a339c2012-12-03 11:18:00 -0800557 if (obj != NULL) {
Andy Hung73535852014-09-05 11:42:58 -0700558 if (mStarted && mVideoDecoder != NULL) {
559 // Issue a seek to refresh the video screen only if started otherwise
560 // the extractor may not yet be started and will assert.
561 // If the video decoder is not set (perhaps audio only in this case)
562 // do not perform a seek as it is not needed.
563 mDeferredActions.push_back(new SeekAction(mCurrentPositionUs));
564 }
Wei Jiaac428aa2014-09-02 19:01:34 -0700565
Andreas Huber57a339c2012-12-03 11:18:00 -0800566 // If there is a new surface texture, instantiate decoders
567 // again if possible.
568 mDeferredActions.push_back(
569 new SimpleAction(&NuPlayer::performScanSources));
570 }
571
572 processDeferredActions();
Andreas Huberf9334412010-12-15 15:17:42 -0800573 break;
574 }
575
576 case kWhatSetAudioSink:
577 {
Steve Block3856b092011-10-20 11:56:00 +0100578 ALOGV("kWhatSetAudioSink");
Andreas Huberf9334412010-12-15 15:17:42 -0800579
580 sp<RefBase> obj;
581 CHECK(msg->findObject("sink", &obj));
582
583 mAudioSink = static_cast<MediaPlayerBase::AudioSink *>(obj.get());
584 break;
585 }
586
587 case kWhatStart:
588 {
Steve Block3856b092011-10-20 11:56:00 +0100589 ALOGV("kWhatStart");
Andreas Huber43c3e6c2011-01-05 12:17:08 -0800590
Andreas Huber3fe62152011-09-16 15:09:22 -0700591 mVideoIsAVC = false;
Wei Jiabc2fb722014-07-08 16:37:57 -0700592 mOffloadAudio = false;
Andreas Huber1aef2112011-01-04 14:01:29 -0800593 mAudioEOS = false;
594 mVideoEOS = false;
Andreas Huber32f3cef2011-03-02 15:34:46 -0800595 mSkipRenderingAudioUntilMediaTimeUs = -1;
596 mSkipRenderingVideoUntilMediaTimeUs = -1;
Andreas Huber3fe62152011-09-16 15:09:22 -0700597 mVideoLateByUs = 0;
598 mNumFramesTotal = 0;
599 mNumFramesDropped = 0;
Andreas Huber57a339c2012-12-03 11:18:00 -0800600 mStarted = true;
Andreas Huber1aef2112011-01-04 14:01:29 -0800601
Lajos Molnar09524832014-07-17 14:29:51 -0700602 /* instantiate decoders now for secure playback */
603 if (mSourceFlags & Source::FLAG_SECURE) {
604 if (mNativeWindow != NULL) {
605 instantiateDecoder(false, &mVideoDecoder);
606 }
607
608 if (mAudioSink != NULL) {
609 instantiateDecoder(true, &mAudioDecoder);
610 }
611 }
612
Andreas Huber5bc087c2010-12-23 10:27:40 -0800613 mSource->start();
Andreas Huberf9334412010-12-15 15:17:42 -0800614
Andreas Huberd5e56232013-03-12 11:01:43 -0700615 uint32_t flags = 0;
616
617 if (mSource->isRealTime()) {
618 flags |= Renderer::FLAG_REAL_TIME;
619 }
620
Wei Jiabc2fb722014-07-08 16:37:57 -0700621 sp<MetaData> audioMeta = mSource->getFormatMeta(true /* audio */);
622 audio_stream_type_t streamType = AUDIO_STREAM_MUSIC;
623 if (mAudioSink != NULL) {
624 streamType = mAudioSink->getAudioStreamType();
625 }
626
627 sp<AMessage> videoFormat = mSource->getFormat(false /* audio */);
628
629 mOffloadAudio =
630 canOffloadStream(audioMeta, (videoFormat != NULL),
631 true /* is_streaming */, streamType);
632 if (mOffloadAudio) {
633 flags |= Renderer::FLAG_OFFLOAD_AUDIO;
634 }
635
Andreas Huberf9334412010-12-15 15:17:42 -0800636 mRenderer = new Renderer(
637 mAudioSink,
Andreas Huberd5e56232013-03-12 11:01:43 -0700638 new AMessage(kWhatRendererNotify, id()),
639 flags);
Andreas Huberf9334412010-12-15 15:17:42 -0800640
Lajos Molnar09524832014-07-17 14:29:51 -0700641 mRendererLooper = new ALooper;
642 mRendererLooper->setName("NuPlayerRenderer");
643 mRendererLooper->start(false, false, ANDROID_PRIORITY_AUDIO);
644 mRendererLooper->registerHandler(mRenderer);
Andreas Huberf9334412010-12-15 15:17:42 -0800645
Andreas Huber1aef2112011-01-04 14:01:29 -0800646 postScanSources();
Andreas Huberf9334412010-12-15 15:17:42 -0800647 break;
648 }
649
650 case kWhatScanSources:
651 {
Andreas Huber1aef2112011-01-04 14:01:29 -0800652 int32_t generation;
653 CHECK(msg->findInt32("generation", &generation));
654 if (generation != mScanSourcesGeneration) {
655 // Drop obsolete msg.
656 break;
657 }
658
Andreas Huber5bc087c2010-12-23 10:27:40 -0800659 mScanSourcesPending = false;
660
Steve Block3856b092011-10-20 11:56:00 +0100661 ALOGV("scanning sources haveAudio=%d, haveVideo=%d",
Andreas Huber43c3e6c2011-01-05 12:17:08 -0800662 mAudioDecoder != NULL, mVideoDecoder != NULL);
663
Andreas Huberb7c8e912012-11-27 15:02:53 -0800664 bool mHadAnySourcesBefore =
665 (mAudioDecoder != NULL) || (mVideoDecoder != NULL);
666
Andy Hung282a7e32014-08-14 15:56:34 -0700667 // initialize video before audio because successful initialization of
668 // video may change deep buffer mode of audio.
Haynes Mathew George5d246ef2012-07-09 10:36:57 -0700669 if (mNativeWindow != NULL) {
670 instantiateDecoder(false, &mVideoDecoder);
671 }
Andreas Huberf9334412010-12-15 15:17:42 -0800672
673 if (mAudioSink != NULL) {
Andy Hung282a7e32014-08-14 15:56:34 -0700674 if (mOffloadAudio) {
675 // open audio sink early under offload mode.
676 sp<AMessage> format = mSource->getFormat(true /*audio*/);
677 openAudioSink(format, true /*offloadOnly*/);
678 }
Andreas Huber5bc087c2010-12-23 10:27:40 -0800679 instantiateDecoder(true, &mAudioDecoder);
Andreas Huberf9334412010-12-15 15:17:42 -0800680 }
681
Andreas Huberb7c8e912012-11-27 15:02:53 -0800682 if (!mHadAnySourcesBefore
683 && (mAudioDecoder != NULL || mVideoDecoder != NULL)) {
684 // This is the first time we've found anything playable.
685
Andreas Huber9575c962013-02-05 13:59:56 -0800686 if (mSourceFlags & Source::FLAG_DYNAMIC_DURATION) {
Andreas Huberb7c8e912012-11-27 15:02:53 -0800687 schedulePollDuration();
688 }
689 }
690
Andreas Hubereac68ba2011-09-27 12:12:25 -0700691 status_t err;
692 if ((err = mSource->feedMoreTSData()) != OK) {
Andreas Huber1aef2112011-01-04 14:01:29 -0800693 if (mAudioDecoder == NULL && mVideoDecoder == NULL) {
694 // We're not currently decoding anything (no audio or
695 // video tracks found) and we just ran out of input data.
Andreas Hubereac68ba2011-09-27 12:12:25 -0700696
697 if (err == ERROR_END_OF_STREAM) {
698 notifyListener(MEDIA_PLAYBACK_COMPLETE, 0, 0);
699 } else {
700 notifyListener(MEDIA_ERROR, MEDIA_ERROR_UNKNOWN, err);
701 }
Andreas Huber1aef2112011-01-04 14:01:29 -0800702 }
Andreas Huberf9334412010-12-15 15:17:42 -0800703 break;
704 }
705
Andreas Huberfbe9d812012-08-31 14:05:27 -0700706 if ((mAudioDecoder == NULL && mAudioSink != NULL)
707 || (mVideoDecoder == NULL && mNativeWindow != NULL)) {
Andreas Huberf9334412010-12-15 15:17:42 -0800708 msg->post(100000ll);
Andreas Huber5bc087c2010-12-23 10:27:40 -0800709 mScanSourcesPending = true;
Andreas Huberf9334412010-12-15 15:17:42 -0800710 }
711 break;
712 }
713
714 case kWhatVideoNotify:
715 case kWhatAudioNotify:
716 {
717 bool audio = msg->what() == kWhatAudioNotify;
718
Wei Jia88703c32014-08-06 11:24:07 -0700719 int32_t currentDecoderGeneration =
720 (audio? mAudioDecoderGeneration : mVideoDecoderGeneration);
721 int32_t requesterGeneration = currentDecoderGeneration - 1;
722 CHECK(msg->findInt32("generation", &requesterGeneration));
723
724 if (requesterGeneration != currentDecoderGeneration) {
725 ALOGV("got message from old %s decoder, generation(%d:%d)",
726 audio ? "audio" : "video", requesterGeneration,
727 currentDecoderGeneration);
728 sp<AMessage> reply;
729 if (!(msg->findMessage("reply", &reply))) {
730 return;
731 }
732
733 reply->setInt32("err", INFO_DISCONTINUITY);
734 reply->post();
735 return;
736 }
737
Andreas Huberf9334412010-12-15 15:17:42 -0800738 int32_t what;
Lajos Molnar1cd13982014-01-17 15:12:51 -0800739 CHECK(msg->findInt32("what", &what));
Andreas Huberf9334412010-12-15 15:17:42 -0800740
Lajos Molnar1cd13982014-01-17 15:12:51 -0800741 if (what == Decoder::kWhatFillThisBuffer) {
Andreas Huberf9334412010-12-15 15:17:42 -0800742 status_t err = feedDecoderInputData(
Lajos Molnar1cd13982014-01-17 15:12:51 -0800743 audio, msg);
Andreas Huberf9334412010-12-15 15:17:42 -0800744
Andreas Huber5bc087c2010-12-23 10:27:40 -0800745 if (err == -EWOULDBLOCK) {
Andreas Hubereac68ba2011-09-27 12:12:25 -0700746 if (mSource->feedMoreTSData() == OK) {
Phil Burkc5cc2e22014-09-09 20:08:39 -0700747 msg->post(10 * 1000ll);
Andreas Huber5bc087c2010-12-23 10:27:40 -0800748 }
Andreas Huberf9334412010-12-15 15:17:42 -0800749 }
Lajos Molnar1cd13982014-01-17 15:12:51 -0800750 } else if (what == Decoder::kWhatEOS) {
Andreas Huberdc9bacd2011-09-26 10:53:29 -0700751 int32_t err;
Lajos Molnar1cd13982014-01-17 15:12:51 -0800752 CHECK(msg->findInt32("err", &err));
Andreas Huberdc9bacd2011-09-26 10:53:29 -0700753
754 if (err == ERROR_END_OF_STREAM) {
Steve Block3856b092011-10-20 11:56:00 +0100755 ALOGV("got %s decoder EOS", audio ? "audio" : "video");
Andreas Huberdc9bacd2011-09-26 10:53:29 -0700756 } else {
Steve Block3856b092011-10-20 11:56:00 +0100757 ALOGV("got %s decoder EOS w/ error %d",
Andreas Huberdc9bacd2011-09-26 10:53:29 -0700758 audio ? "audio" : "video",
759 err);
760 }
761
762 mRenderer->queueEOS(audio, err);
Lajos Molnar1cd13982014-01-17 15:12:51 -0800763 } else if (what == Decoder::kWhatFlushCompleted) {
Andreas Huber1aef2112011-01-04 14:01:29 -0800764 bool needShutdown;
Andreas Huber53df1a42010-12-22 10:03:04 -0800765
Andreas Huberf9334412010-12-15 15:17:42 -0800766 if (audio) {
Andreas Huber1aef2112011-01-04 14:01:29 -0800767 CHECK(IsFlushingState(mFlushingAudio, &needShutdown));
Andreas Huberf9334412010-12-15 15:17:42 -0800768 mFlushingAudio = FLUSHED;
769 } else {
Andreas Huber1aef2112011-01-04 14:01:29 -0800770 CHECK(IsFlushingState(mFlushingVideo, &needShutdown));
Andreas Huberf9334412010-12-15 15:17:42 -0800771 mFlushingVideo = FLUSHED;
Andreas Huber3fe62152011-09-16 15:09:22 -0700772
773 mVideoLateByUs = 0;
Andreas Huberf9334412010-12-15 15:17:42 -0800774 }
775
Steve Block3856b092011-10-20 11:56:00 +0100776 ALOGV("decoder %s flush completed", audio ? "audio" : "video");
Andreas Huberf9334412010-12-15 15:17:42 -0800777
Andreas Huber1aef2112011-01-04 14:01:29 -0800778 if (needShutdown) {
Steve Block3856b092011-10-20 11:56:00 +0100779 ALOGV("initiating %s decoder shutdown",
Andreas Huber53df1a42010-12-22 10:03:04 -0800780 audio ? "audio" : "video");
Andreas Huberf9334412010-12-15 15:17:42 -0800781
Lajos Molnar87603c02014-08-20 19:25:30 -0700782 getDecoder(audio)->initiateShutdown();
Andreas Huberf9334412010-12-15 15:17:42 -0800783
Andreas Huber53df1a42010-12-22 10:03:04 -0800784 if (audio) {
785 mFlushingAudio = SHUTTING_DOWN_DECODER;
786 } else {
787 mFlushingVideo = SHUTTING_DOWN_DECODER;
788 }
Andreas Huberf9334412010-12-15 15:17:42 -0800789 }
Andreas Huber3831a062010-12-21 10:22:33 -0800790
791 finishFlushIfPossible();
Lajos Molnar1cd13982014-01-17 15:12:51 -0800792 } else if (what == Decoder::kWhatOutputFormatChanged) {
793 sp<AMessage> format;
794 CHECK(msg->findMessage("format", &format));
795
Andreas Huber31e25082011-01-10 10:38:31 -0800796 if (audio) {
Andy Hung282a7e32014-08-14 15:56:34 -0700797 openAudioSink(format, false /*offloadOnly*/);
Andreas Huber31e25082011-01-10 10:38:31 -0800798 } else {
799 // video
Chong Zhangced1c2f2014-08-08 15:22:35 -0700800 sp<AMessage> inputFormat =
801 mSource->getFormat(false /* audio */);
Andreas Huber3831a062010-12-21 10:22:33 -0800802
Chong Zhangced1c2f2014-08-08 15:22:35 -0700803 updateVideoSize(inputFormat, format);
Andreas Huber31e25082011-01-10 10:38:31 -0800804 }
Lajos Molnar1cd13982014-01-17 15:12:51 -0800805 } else if (what == Decoder::kWhatShutdownCompleted) {
Steve Block3856b092011-10-20 11:56:00 +0100806 ALOGV("%s shutdown completed", audio ? "audio" : "video");
Andreas Huber3831a062010-12-21 10:22:33 -0800807 if (audio) {
808 mAudioDecoder.clear();
809
810 CHECK_EQ((int)mFlushingAudio, (int)SHUTTING_DOWN_DECODER);
811 mFlushingAudio = SHUT_DOWN;
812 } else {
813 mVideoDecoder.clear();
814
815 CHECK_EQ((int)mFlushingVideo, (int)SHUTTING_DOWN_DECODER);
816 mFlushingVideo = SHUT_DOWN;
817 }
818
819 finishFlushIfPossible();
Lajos Molnar1cd13982014-01-17 15:12:51 -0800820 } else if (what == Decoder::kWhatError) {
Steve Block29357bc2012-01-06 19:20:56 +0000821 ALOGE("Received error from %s decoder, aborting playback.",
Andreas Huberc92fd242011-08-16 13:48:44 -0700822 audio ? "audio" : "video");
823
Chong Zhangf4c0a942014-08-11 15:14:10 -0700824 status_t err;
825 if (!msg->findInt32("err", &err)) {
826 err = UNKNOWN_ERROR;
827 }
828 mRenderer->queueEOS(audio, err);
Marco Nelissen9e2b7912014-08-18 16:13:03 -0700829 if (audio && mFlushingAudio != NONE) {
830 mAudioDecoder.clear();
831 mFlushingAudio = SHUT_DOWN;
832 } else if (!audio && mFlushingVideo != NONE){
833 mVideoDecoder.clear();
834 mFlushingVideo = SHUT_DOWN;
835 }
836 finishFlushIfPossible();
Lajos Molnar1cd13982014-01-17 15:12:51 -0800837 } else if (what == Decoder::kWhatDrainThisBuffer) {
838 renderBuffer(audio, msg);
839 } else {
840 ALOGV("Unhandled decoder notification %d '%c%c%c%c'.",
Andreas Hubera1f8ab02012-11-30 10:53:22 -0800841 what,
842 what >> 24,
843 (what >> 16) & 0xff,
844 (what >> 8) & 0xff,
845 what & 0xff);
Andreas Huberf9334412010-12-15 15:17:42 -0800846 }
847
848 break;
849 }
850
851 case kWhatRendererNotify:
852 {
853 int32_t what;
854 CHECK(msg->findInt32("what", &what));
855
856 if (what == Renderer::kWhatEOS) {
857 int32_t audio;
858 CHECK(msg->findInt32("audio", &audio));
859
Andreas Huberc92fd242011-08-16 13:48:44 -0700860 int32_t finalResult;
861 CHECK(msg->findInt32("finalResult", &finalResult));
862
Andreas Huberf9334412010-12-15 15:17:42 -0800863 if (audio) {
864 mAudioEOS = true;
865 } else {
866 mVideoEOS = true;
867 }
868
Andreas Huberc92fd242011-08-16 13:48:44 -0700869 if (finalResult == ERROR_END_OF_STREAM) {
Steve Block3856b092011-10-20 11:56:00 +0100870 ALOGV("reached %s EOS", audio ? "audio" : "video");
Andreas Huberc92fd242011-08-16 13:48:44 -0700871 } else {
Steve Block29357bc2012-01-06 19:20:56 +0000872 ALOGE("%s track encountered an error (%d)",
Andreas Huberc92fd242011-08-16 13:48:44 -0700873 audio ? "audio" : "video", finalResult);
874
875 notifyListener(
876 MEDIA_ERROR, MEDIA_ERROR_UNKNOWN, finalResult);
877 }
Andreas Huberf9334412010-12-15 15:17:42 -0800878
879 if ((mAudioEOS || mAudioDecoder == NULL)
880 && (mVideoEOS || mVideoDecoder == NULL)) {
881 notifyListener(MEDIA_PLAYBACK_COMPLETE, 0, 0);
882 }
Andreas Huber43c3e6c2011-01-05 12:17:08 -0800883 } else if (what == Renderer::kWhatPosition) {
884 int64_t positionUs;
885 CHECK(msg->findInt64("positionUs", &positionUs));
Wei Jiaac428aa2014-09-02 19:01:34 -0700886 mCurrentPositionUs = positionUs;
Andreas Huber43c3e6c2011-01-05 12:17:08 -0800887
Andreas Huber3fe62152011-09-16 15:09:22 -0700888 CHECK(msg->findInt64("videoLateByUs", &mVideoLateByUs));
889
Andreas Huber43c3e6c2011-01-05 12:17:08 -0800890 if (mDriver != NULL) {
891 sp<NuPlayerDriver> driver = mDriver.promote();
892 if (driver != NULL) {
893 driver->notifyPosition(positionUs);
Andreas Huber3fe62152011-09-16 15:09:22 -0700894
895 driver->notifyFrameStats(
896 mNumFramesTotal, mNumFramesDropped);
Andreas Huber43c3e6c2011-01-05 12:17:08 -0800897 }
898 }
Andreas Huber3fe62152011-09-16 15:09:22 -0700899 } else if (what == Renderer::kWhatFlushComplete) {
Andreas Huberf9334412010-12-15 15:17:42 -0800900 int32_t audio;
901 CHECK(msg->findInt32("audio", &audio));
902
Steve Block3856b092011-10-20 11:56:00 +0100903 ALOGV("renderer %s flush completed.", audio ? "audio" : "video");
James Dongf57b4ea2012-07-20 13:38:36 -0700904 } else if (what == Renderer::kWhatVideoRenderingStart) {
905 notifyListener(MEDIA_INFO, MEDIA_INFO_RENDERING_START, 0);
Lajos Molnarcbaffcf2013-08-14 18:30:38 -0700906 } else if (what == Renderer::kWhatMediaRenderingStart) {
907 ALOGV("media rendering started");
908 notifyListener(MEDIA_STARTED, 0, 0);
Wei Jia3a2956d2014-07-22 16:01:33 -0700909 } else if (what == Renderer::kWhatAudioOffloadTearDown) {
910 ALOGV("Tear down audio offload, fall back to s/w path");
911 int64_t positionUs;
912 CHECK(msg->findInt64("positionUs", &positionUs));
Andy Hung282a7e32014-08-14 15:56:34 -0700913 closeAudioSink();
Wei Jia3a2956d2014-07-22 16:01:33 -0700914 mAudioDecoder.clear();
915 mRenderer->flush(true /* audio */);
916 if (mVideoDecoder != NULL) {
917 mRenderer->flush(false /* audio */);
918 }
919 mRenderer->signalDisableOffloadAudio();
920 mOffloadAudio = false;
921
922 performSeek(positionUs);
923 instantiateDecoder(true /* audio */, &mAudioDecoder);
Andreas Huberf9334412010-12-15 15:17:42 -0800924 }
925 break;
926 }
927
928 case kWhatMoreDataQueued:
929 {
930 break;
931 }
932
Andreas Huber1aef2112011-01-04 14:01:29 -0800933 case kWhatReset:
934 {
Steve Block3856b092011-10-20 11:56:00 +0100935 ALOGV("kWhatReset");
Andreas Huber1aef2112011-01-04 14:01:29 -0800936
Andreas Hubera1f8ab02012-11-30 10:53:22 -0800937 mDeferredActions.push_back(
Andreas Huber14f76722013-01-15 09:04:18 -0800938 new ShutdownDecoderAction(
939 true /* audio */, true /* video */));
Andreas Huberb7c8e912012-11-27 15:02:53 -0800940
Andreas Hubera1f8ab02012-11-30 10:53:22 -0800941 mDeferredActions.push_back(
942 new SimpleAction(&NuPlayer::performReset));
Andreas Huberb58ce9f2011-11-28 16:27:35 -0800943
Andreas Hubera1f8ab02012-11-30 10:53:22 -0800944 processDeferredActions();
Andreas Huber1aef2112011-01-04 14:01:29 -0800945 break;
946 }
947
Andreas Huber43c3e6c2011-01-05 12:17:08 -0800948 case kWhatSeek:
949 {
950 int64_t seekTimeUs;
951 CHECK(msg->findInt64("seekTimeUs", &seekTimeUs));
952
Andreas Hubera1f8ab02012-11-30 10:53:22 -0800953 ALOGV("kWhatSeek seekTimeUs=%lld us", seekTimeUs);
Andreas Huber43c3e6c2011-01-05 12:17:08 -0800954
Andreas Hubera1f8ab02012-11-30 10:53:22 -0800955 mDeferredActions.push_back(
956 new SimpleAction(&NuPlayer::performDecoderFlush));
Andreas Huber43c3e6c2011-01-05 12:17:08 -0800957
Andreas Hubera1f8ab02012-11-30 10:53:22 -0800958 mDeferredActions.push_back(new SeekAction(seekTimeUs));
Andreas Huber43c3e6c2011-01-05 12:17:08 -0800959
Andreas Hubera1f8ab02012-11-30 10:53:22 -0800960 processDeferredActions();
Andreas Huber43c3e6c2011-01-05 12:17:08 -0800961 break;
962 }
963
Andreas Huberb4082222011-01-20 15:23:04 -0800964 case kWhatPause:
965 {
966 CHECK(mRenderer != NULL);
Roger Jönssonfba60da2013-01-21 17:15:45 +0100967 mSource->pause();
Andreas Huberb4082222011-01-20 15:23:04 -0800968 mRenderer->pause();
969 break;
970 }
971
972 case kWhatResume:
973 {
974 CHECK(mRenderer != NULL);
Roger Jönssonfba60da2013-01-21 17:15:45 +0100975 mSource->resume();
Andreas Huberb4082222011-01-20 15:23:04 -0800976 mRenderer->resume();
977 break;
978 }
979
Andreas Huberb5f25f02013-02-05 10:14:26 -0800980 case kWhatSourceNotify:
981 {
Andreas Huber9575c962013-02-05 13:59:56 -0800982 onSourceNotify(msg);
Andreas Huberb5f25f02013-02-05 10:14:26 -0800983 break;
984 }
985
Chong Zhanga7fa1d92014-06-11 14:49:23 -0700986 case kWhatClosedCaptionNotify:
987 {
988 onClosedCaptionNotify(msg);
989 break;
990 }
991
Andreas Huberf9334412010-12-15 15:17:42 -0800992 default:
993 TRESPASS();
994 break;
995 }
996}
997
Andreas Huber3831a062010-12-21 10:22:33 -0800998void NuPlayer::finishFlushIfPossible() {
Wei Jia53904f32014-07-29 10:22:53 -0700999 if (mFlushingAudio != NONE && mFlushingAudio != FLUSHED
1000 && mFlushingAudio != SHUT_DOWN) {
Andreas Huber3831a062010-12-21 10:22:33 -08001001 return;
1002 }
1003
Wei Jia53904f32014-07-29 10:22:53 -07001004 if (mFlushingVideo != NONE && mFlushingVideo != FLUSHED
1005 && mFlushingVideo != SHUT_DOWN) {
Andreas Huber3831a062010-12-21 10:22:33 -08001006 return;
1007 }
1008
Steve Block3856b092011-10-20 11:56:00 +01001009 ALOGV("both audio and video are flushed now.");
Andreas Huber3831a062010-12-21 10:22:33 -08001010
Phil Burk9f526492014-09-03 15:04:12 -07001011 mPendingAudioAccessUnit.clear();
Phil Burkc5cc2e22014-09-09 20:08:39 -07001012 mAggregateBuffer.clear();
Phil Burk9f526492014-09-03 15:04:12 -07001013
Andreas Huber6e3d3112011-11-28 12:36:11 -08001014 if (mTimeDiscontinuityPending) {
1015 mRenderer->signalTimeDiscontinuity();
1016 mTimeDiscontinuityPending = false;
1017 }
Andreas Huber3831a062010-12-21 10:22:33 -08001018
Wei Jia53904f32014-07-29 10:22:53 -07001019 if (mAudioDecoder != NULL && mFlushingAudio == FLUSHED) {
Andreas Huber3831a062010-12-21 10:22:33 -08001020 mAudioDecoder->signalResume();
1021 }
1022
Wei Jia53904f32014-07-29 10:22:53 -07001023 if (mVideoDecoder != NULL && mFlushingVideo == FLUSHED) {
Andreas Huber3831a062010-12-21 10:22:33 -08001024 mVideoDecoder->signalResume();
1025 }
1026
1027 mFlushingAudio = NONE;
1028 mFlushingVideo = NONE;
Andreas Huber3831a062010-12-21 10:22:33 -08001029
Andreas Hubera1f8ab02012-11-30 10:53:22 -08001030 processDeferredActions();
Andreas Huber1aef2112011-01-04 14:01:29 -08001031}
1032
1033void NuPlayer::postScanSources() {
1034 if (mScanSourcesPending) {
1035 return;
1036 }
1037
1038 sp<AMessage> msg = new AMessage(kWhatScanSources, id());
1039 msg->setInt32("generation", mScanSourcesGeneration);
1040 msg->post();
1041
1042 mScanSourcesPending = true;
1043}
1044
Andy Hung282a7e32014-08-14 15:56:34 -07001045void NuPlayer::openAudioSink(const sp<AMessage> &format, bool offloadOnly) {
1046 ALOGV("openAudioSink: offloadOnly(%d) mOffloadAudio(%d)",
1047 offloadOnly, mOffloadAudio);
1048 bool audioSinkChanged = false;
1049
1050 int32_t numChannels;
1051 CHECK(format->findInt32("channel-count", &numChannels));
1052
1053 int32_t channelMask;
1054 if (!format->findInt32("channel-mask", &channelMask)) {
1055 // signal to the AudioSink to derive the mask from count.
1056 channelMask = CHANNEL_MASK_USE_CHANNEL_ORDER;
1057 }
1058
1059 int32_t sampleRate;
1060 CHECK(format->findInt32("sample-rate", &sampleRate));
1061
1062 uint32_t flags;
1063 int64_t durationUs;
1064 // FIXME: we should handle the case where the video decoder
1065 // is created after we receive the format change indication.
1066 // Current code will just make that we select deep buffer
1067 // with video which should not be a problem as it should
1068 // not prevent from keeping A/V sync.
1069 if (mVideoDecoder == NULL &&
1070 mSource->getDuration(&durationUs) == OK &&
1071 durationUs
1072 > AUDIO_SINK_MIN_DEEP_BUFFER_DURATION_US) {
1073 flags = AUDIO_OUTPUT_FLAG_DEEP_BUFFER;
1074 } else {
1075 flags = AUDIO_OUTPUT_FLAG_NONE;
1076 }
1077
1078 if (mOffloadAudio) {
1079 audio_format_t audioFormat = AUDIO_FORMAT_PCM_16_BIT;
1080 AString mime;
1081 CHECK(format->findString("mime", &mime));
1082 status_t err = mapMimeToAudioFormat(audioFormat, mime.c_str());
1083
1084 if (err != OK) {
1085 ALOGE("Couldn't map mime \"%s\" to a valid "
1086 "audio_format", mime.c_str());
1087 mOffloadAudio = false;
1088 } else {
1089 ALOGV("Mime \"%s\" mapped to audio_format 0x%x",
1090 mime.c_str(), audioFormat);
1091
1092 int avgBitRate = -1;
1093 format->findInt32("bit-rate", &avgBitRate);
1094
1095 int32_t aacProfile = -1;
1096 if (audioFormat == AUDIO_FORMAT_AAC
1097 && format->findInt32("aac-profile", &aacProfile)) {
1098 // Redefine AAC format as per aac profile
1099 mapAACProfileToAudioFormat(
1100 audioFormat,
1101 aacProfile);
1102 }
1103
1104 audio_offload_info_t offloadInfo = AUDIO_INFO_INITIALIZER;
1105 offloadInfo.duration_us = -1;
1106 format->findInt64(
1107 "durationUs", &offloadInfo.duration_us);
1108 offloadInfo.sample_rate = sampleRate;
1109 offloadInfo.channel_mask = channelMask;
1110 offloadInfo.format = audioFormat;
1111 offloadInfo.stream_type = AUDIO_STREAM_MUSIC;
1112 offloadInfo.bit_rate = avgBitRate;
1113 offloadInfo.has_video = (mVideoDecoder != NULL);
1114 offloadInfo.is_streaming = true;
1115
1116 if (memcmp(&mCurrentOffloadInfo, &offloadInfo, sizeof(offloadInfo)) == 0) {
1117 ALOGV("openAudioSink: no change in offload mode");
1118 return; // no change from previous configuration, everything ok.
1119 }
1120 ALOGV("openAudioSink: try to open AudioSink in offload mode");
1121 flags |= AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD;
Ronghua Wu1ffb5382014-08-18 15:57:03 -07001122 flags &= ~AUDIO_OUTPUT_FLAG_DEEP_BUFFER;
Andy Hung282a7e32014-08-14 15:56:34 -07001123 audioSinkChanged = true;
1124 mAudioSink->close();
1125 err = mAudioSink->open(
1126 sampleRate,
1127 numChannels,
1128 (audio_channel_mask_t)channelMask,
1129 audioFormat,
1130 8 /* bufferCount */,
1131 &NuPlayer::Renderer::AudioSinkCallback,
1132 mRenderer.get(),
1133 (audio_output_flags_t)flags,
1134 &offloadInfo);
1135
1136 if (err == OK) {
1137 // If the playback is offloaded to h/w, we pass
1138 // the HAL some metadata information.
1139 // We don't want to do this for PCM because it
1140 // will be going through the AudioFlinger mixer
1141 // before reaching the hardware.
1142 sp<MetaData> audioMeta =
1143 mSource->getFormatMeta(true /* audio */);
1144 sendMetaDataToHal(mAudioSink, audioMeta);
1145 mCurrentOffloadInfo = offloadInfo;
1146 err = mAudioSink->start();
1147 ALOGV_IF(err == OK, "openAudioSink: offload succeeded");
1148 }
1149 if (err != OK) {
1150 // Clean up, fall back to non offload mode.
1151 mAudioSink->close();
1152 mRenderer->signalDisableOffloadAudio();
1153 mOffloadAudio = false;
1154 mCurrentOffloadInfo = AUDIO_INFO_INITIALIZER;
1155 ALOGV("openAudioSink: offload failed");
1156 }
1157 }
1158 }
1159 if (!offloadOnly && !mOffloadAudio) {
1160 flags &= ~AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD;
1161 ALOGV("openAudioSink: open AudioSink in NON-offload mode");
1162
1163 audioSinkChanged = true;
1164 mAudioSink->close();
1165 mCurrentOffloadInfo = AUDIO_INFO_INITIALIZER;
1166 CHECK_EQ(mAudioSink->open(
1167 sampleRate,
1168 numChannels,
1169 (audio_channel_mask_t)channelMask,
1170 AUDIO_FORMAT_PCM_16_BIT,
1171 8 /* bufferCount */,
1172 NULL,
1173 NULL,
1174 (audio_output_flags_t)flags),
1175 (status_t)OK);
1176 mAudioSink->start();
1177 }
1178 if (audioSinkChanged) {
1179 mRenderer->signalAudioSinkChanged();
1180 }
1181}
1182
1183void NuPlayer::closeAudioSink() {
1184 mAudioSink->close();
1185 mCurrentOffloadInfo = AUDIO_INFO_INITIALIZER;
1186}
1187
Andreas Huber5bc087c2010-12-23 10:27:40 -08001188status_t NuPlayer::instantiateDecoder(bool audio, sp<Decoder> *decoder) {
Andreas Huberf9334412010-12-15 15:17:42 -08001189 if (*decoder != NULL) {
1190 return OK;
1191 }
1192
Andreas Huber84066782011-08-16 09:34:26 -07001193 sp<AMessage> format = mSource->getFormat(audio);
Andreas Huberf9334412010-12-15 15:17:42 -08001194
Andreas Huber84066782011-08-16 09:34:26 -07001195 if (format == NULL) {
Andreas Huberf9334412010-12-15 15:17:42 -08001196 return -EWOULDBLOCK;
1197 }
1198
Andreas Huber3fe62152011-09-16 15:09:22 -07001199 if (!audio) {
Andreas Huber84066782011-08-16 09:34:26 -07001200 AString mime;
1201 CHECK(format->findString("mime", &mime));
1202 mVideoIsAVC = !strcasecmp(MEDIA_MIMETYPE_VIDEO_AVC, mime.c_str());
Chong Zhanga7fa1d92014-06-11 14:49:23 -07001203
1204 sp<AMessage> ccNotify = new AMessage(kWhatClosedCaptionNotify, id());
1205 mCCDecoder = new CCDecoder(ccNotify);
Lajos Molnar09524832014-07-17 14:29:51 -07001206
1207 if (mSourceFlags & Source::FLAG_SECURE) {
1208 format->setInt32("secure", true);
1209 }
Andreas Huber3fe62152011-09-16 15:09:22 -07001210 }
1211
Wei Jiabc2fb722014-07-08 16:37:57 -07001212 if (audio) {
Wei Jia88703c32014-08-06 11:24:07 -07001213 sp<AMessage> notify = new AMessage(kWhatAudioNotify, id());
1214 ++mAudioDecoderGeneration;
1215 notify->setInt32("generation", mAudioDecoderGeneration);
1216
Wei Jiabc2fb722014-07-08 16:37:57 -07001217 if (mOffloadAudio) {
1218 *decoder = new DecoderPassThrough(notify);
1219 } else {
1220 *decoder = new Decoder(notify);
1221 }
1222 } else {
Wei Jia88703c32014-08-06 11:24:07 -07001223 sp<AMessage> notify = new AMessage(kWhatVideoNotify, id());
1224 ++mVideoDecoderGeneration;
1225 notify->setInt32("generation", mVideoDecoderGeneration);
1226
Wei Jiabc2fb722014-07-08 16:37:57 -07001227 *decoder = new Decoder(notify, mNativeWindow);
1228 }
Lajos Molnar1cd13982014-01-17 15:12:51 -08001229 (*decoder)->init();
Andreas Huber84066782011-08-16 09:34:26 -07001230 (*decoder)->configure(format);
Andreas Huberf9334412010-12-15 15:17:42 -08001231
Lajos Molnar09524832014-07-17 14:29:51 -07001232 // allocate buffers to decrypt widevine source buffers
1233 if (!audio && (mSourceFlags & Source::FLAG_SECURE)) {
1234 Vector<sp<ABuffer> > inputBufs;
1235 CHECK_EQ((*decoder)->getInputBuffers(&inputBufs), (status_t)OK);
1236
1237 Vector<MediaBuffer *> mediaBufs;
1238 for (size_t i = 0; i < inputBufs.size(); i++) {
1239 const sp<ABuffer> &buffer = inputBufs[i];
1240 MediaBuffer *mbuf = new MediaBuffer(buffer->data(), buffer->size());
1241 mediaBufs.push(mbuf);
1242 }
1243
1244 status_t err = mSource->setBuffers(audio, mediaBufs);
1245 if (err != OK) {
1246 for (size_t i = 0; i < mediaBufs.size(); ++i) {
1247 mediaBufs[i]->release();
1248 }
1249 mediaBufs.clear();
1250 ALOGE("Secure source didn't support secure mediaBufs.");
1251 return err;
1252 }
1253 }
Andreas Huberf9334412010-12-15 15:17:42 -08001254 return OK;
1255}
1256
1257status_t NuPlayer::feedDecoderInputData(bool audio, const sp<AMessage> &msg) {
1258 sp<AMessage> reply;
1259 CHECK(msg->findMessage("reply", &reply));
1260
Wei Jia53904f32014-07-29 10:22:53 -07001261 if ((audio && mFlushingAudio != NONE)
Wei Jiaf702d042014-09-09 12:08:47 -07001262 || (!audio && mFlushingVideo != NONE)
1263 || mSource == NULL) {
Wei Jiab189a5b2014-08-07 06:11:39 +00001264 reply->setInt32("err", INFO_DISCONTINUITY);
1265 reply->post();
1266 return OK;
Andreas Huberf9334412010-12-15 15:17:42 -08001267 }
1268
1269 sp<ABuffer> accessUnit;
Andreas Huberf9334412010-12-15 15:17:42 -08001270
Phil Burk9f526492014-09-03 15:04:12 -07001271 // Aggregate smaller buffers into a larger buffer.
1272 // The goal is to reduce power consumption.
Phil Burk33b51b02014-09-17 16:03:47 -07001273 // Note this will not work if the decoder requires one frame per buffer.
1274 bool doBufferAggregation = (audio && mOffloadAudio);
Phil Burk9f526492014-09-03 15:04:12 -07001275 bool needMoreData = false;
Phil Burk9f526492014-09-03 15:04:12 -07001276
Andreas Huber3fe62152011-09-16 15:09:22 -07001277 bool dropAccessUnit;
1278 do {
Phil Burk9f526492014-09-03 15:04:12 -07001279 status_t err;
1280 // Did we save an accessUnit earlier because of a discontinuity?
1281 if (audio && (mPendingAudioAccessUnit != NULL)) {
1282 accessUnit = mPendingAudioAccessUnit;
1283 mPendingAudioAccessUnit.clear();
1284 err = mPendingAudioErr;
1285 ALOGV("feedDecoderInputData() use mPendingAudioAccessUnit");
1286 } else {
1287 err = mSource->dequeueAccessUnit(audio, &accessUnit);
1288 }
Andreas Huber5bc087c2010-12-23 10:27:40 -08001289
Andreas Huber3fe62152011-09-16 15:09:22 -07001290 if (err == -EWOULDBLOCK) {
Phil Burkc5cc2e22014-09-09 20:08:39 -07001291 return err;
Andreas Huber3fe62152011-09-16 15:09:22 -07001292 } else if (err != OK) {
1293 if (err == INFO_DISCONTINUITY) {
Phil Burk33b51b02014-09-17 16:03:47 -07001294 if (doBufferAggregation && (mAggregateBuffer != NULL)) {
Phil Burk9f526492014-09-03 15:04:12 -07001295 // We already have some data so save this for later.
1296 mPendingAudioErr = err;
1297 mPendingAudioAccessUnit = accessUnit;
1298 accessUnit.clear();
1299 ALOGD("feedDecoderInputData() save discontinuity for later");
1300 break;
1301 }
Andreas Huber3fe62152011-09-16 15:09:22 -07001302 int32_t type;
1303 CHECK(accessUnit->meta()->findInt32("discontinuity", &type));
Andreas Huber53df1a42010-12-22 10:03:04 -08001304
Andreas Huber3fe62152011-09-16 15:09:22 -07001305 bool formatChange =
Andreas Huber6e3d3112011-11-28 12:36:11 -08001306 (audio &&
1307 (type & ATSParser::DISCONTINUITY_AUDIO_FORMAT))
1308 || (!audio &&
1309 (type & ATSParser::DISCONTINUITY_VIDEO_FORMAT));
Andreas Huber53df1a42010-12-22 10:03:04 -08001310
Andreas Huber6e3d3112011-11-28 12:36:11 -08001311 bool timeChange = (type & ATSParser::DISCONTINUITY_TIME) != 0;
1312
Steve Blockdf64d152012-01-04 20:05:49 +00001313 ALOGI("%s discontinuity (formatChange=%d, time=%d)",
Andreas Huber6e3d3112011-11-28 12:36:11 -08001314 audio ? "audio" : "video", formatChange, timeChange);
Andreas Huber32f3cef2011-03-02 15:34:46 -08001315
Andreas Huber3fe62152011-09-16 15:09:22 -07001316 if (audio) {
1317 mSkipRenderingAudioUntilMediaTimeUs = -1;
1318 } else {
1319 mSkipRenderingVideoUntilMediaTimeUs = -1;
1320 }
Andreas Huber32f3cef2011-03-02 15:34:46 -08001321
Andreas Huber6e3d3112011-11-28 12:36:11 -08001322 if (timeChange) {
1323 sp<AMessage> extra;
1324 if (accessUnit->meta()->findMessage("extra", &extra)
1325 && extra != NULL) {
1326 int64_t resumeAtMediaTimeUs;
1327 if (extra->findInt64(
1328 "resume-at-mediatimeUs", &resumeAtMediaTimeUs)) {
Steve Blockdf64d152012-01-04 20:05:49 +00001329 ALOGI("suppressing rendering of %s until %lld us",
Andreas Huber6e3d3112011-11-28 12:36:11 -08001330 audio ? "audio" : "video", resumeAtMediaTimeUs);
Andreas Huber3fe62152011-09-16 15:09:22 -07001331
Andreas Huber6e3d3112011-11-28 12:36:11 -08001332 if (audio) {
1333 mSkipRenderingAudioUntilMediaTimeUs =
1334 resumeAtMediaTimeUs;
1335 } else {
1336 mSkipRenderingVideoUntilMediaTimeUs =
1337 resumeAtMediaTimeUs;
1338 }
Andreas Huber3fe62152011-09-16 15:09:22 -07001339 }
Andreas Huber32f3cef2011-03-02 15:34:46 -08001340 }
1341 }
Andreas Huber3fe62152011-09-16 15:09:22 -07001342
Andreas Huber6e3d3112011-11-28 12:36:11 -08001343 mTimeDiscontinuityPending =
1344 mTimeDiscontinuityPending || timeChange;
1345
Lajos Molnar87603c02014-08-20 19:25:30 -07001346 bool seamlessFormatChange = false;
1347 sp<AMessage> newFormat = mSource->getFormat(audio);
1348 if (formatChange) {
1349 seamlessFormatChange =
1350 getDecoder(audio)->supportsSeamlessFormatChange(newFormat);
1351 // treat seamless format change separately
1352 formatChange = !seamlessFormatChange;
1353 }
1354 bool shutdownOrFlush = formatChange || timeChange;
1355
1356 // We want to queue up scan-sources only once per discontinuity.
1357 // We control this by doing it only if neither audio nor video are
1358 // flushing or shutting down. (After handling 1st discontinuity, one
1359 // of the flushing states will not be NONE.)
1360 // No need to scan sources if this discontinuity does not result
1361 // in a flush or shutdown, as the flushing state will stay NONE.
1362 if (mFlushingAudio == NONE && mFlushingVideo == NONE &&
1363 shutdownOrFlush) {
Robert Shiha2981012014-07-30 17:41:24 -07001364 // And we'll resume scanning sources once we're done
1365 // flushing.
1366 mDeferredActions.push_front(
1367 new SimpleAction(
1368 &NuPlayer::performScanSources));
1369 }
1370
Lajos Molnar87603c02014-08-20 19:25:30 -07001371 if (formatChange /* not seamless */) {
1372 // must change decoder
1373 flushDecoder(audio, /* needShutdown = */ true);
1374 } else if (timeChange) {
1375 // need to flush
1376 flushDecoder(audio, /* needShutdown = */ false, newFormat);
1377 err = OK;
1378 } else if (seamlessFormatChange) {
1379 // reuse existing decoder and don't flush
1380 updateDecoderFormatWithoutFlush(audio, newFormat);
1381 err = OK;
Andreas Huber6e3d3112011-11-28 12:36:11 -08001382 } else {
1383 // This stream is unaffected by the discontinuity
Andreas Huber6e3d3112011-11-28 12:36:11 -08001384 return -EWOULDBLOCK;
1385 }
Andreas Huber32f3cef2011-03-02 15:34:46 -08001386 }
1387
Andreas Huber3fe62152011-09-16 15:09:22 -07001388 reply->setInt32("err", err);
1389 reply->post();
1390 return OK;
Andreas Huberf9334412010-12-15 15:17:42 -08001391 }
1392
Andreas Huber3fe62152011-09-16 15:09:22 -07001393 if (!audio) {
1394 ++mNumFramesTotal;
1395 }
1396
1397 dropAccessUnit = false;
1398 if (!audio
Lajos Molnar09524832014-07-17 14:29:51 -07001399 && !(mSourceFlags & Source::FLAG_SECURE)
Andreas Huber3fe62152011-09-16 15:09:22 -07001400 && mVideoLateByUs > 100000ll
1401 && mVideoIsAVC
1402 && !IsAVCReferenceFrame(accessUnit)) {
1403 dropAccessUnit = true;
1404 ++mNumFramesDropped;
1405 }
Phil Burk9f526492014-09-03 15:04:12 -07001406
1407 size_t smallSize = accessUnit->size();
1408 needMoreData = false;
Phil Burkc5cc2e22014-09-09 20:08:39 -07001409 if (doBufferAggregation && (mAggregateBuffer == NULL)
Phil Burk9f526492014-09-03 15:04:12 -07001410 // Don't bother if only room for a few small buffers.
Phil Burkc5cc2e22014-09-09 20:08:39 -07001411 && (smallSize < (kAggregateBufferSizeBytes / 3))) {
Phil Burk9f526492014-09-03 15:04:12 -07001412 // Create a larger buffer for combining smaller buffers from the extractor.
Phil Burkc5cc2e22014-09-09 20:08:39 -07001413 mAggregateBuffer = new ABuffer(kAggregateBufferSizeBytes);
1414 mAggregateBuffer->setRange(0, 0); // start empty
Phil Burk9f526492014-09-03 15:04:12 -07001415 }
1416
Phil Burk33b51b02014-09-17 16:03:47 -07001417 if (doBufferAggregation && (mAggregateBuffer != NULL)) {
Phil Burk9f526492014-09-03 15:04:12 -07001418 int64_t timeUs;
Phil Burkc5cc2e22014-09-09 20:08:39 -07001419 int64_t dummy;
Phil Burk9f526492014-09-03 15:04:12 -07001420 bool smallTimestampValid = accessUnit->meta()->findInt64("timeUs", &timeUs);
Phil Burkc5cc2e22014-09-09 20:08:39 -07001421 bool bigTimestampValid = mAggregateBuffer->meta()->findInt64("timeUs", &dummy);
Phil Burk9f526492014-09-03 15:04:12 -07001422 // Will the smaller buffer fit?
Phil Burkc5cc2e22014-09-09 20:08:39 -07001423 size_t bigSize = mAggregateBuffer->size();
1424 size_t roomLeft = mAggregateBuffer->capacity() - bigSize;
Phil Burk9f526492014-09-03 15:04:12 -07001425 // Should we save this small buffer for the next big buffer?
1426 // If the first small buffer did not have a timestamp then save
1427 // any buffer that does have a timestamp until the next big buffer.
1428 if ((smallSize > roomLeft)
Phil Burkc5cc2e22014-09-09 20:08:39 -07001429 || (!bigTimestampValid && (bigSize > 0) && smallTimestampValid)) {
Phil Burk9f526492014-09-03 15:04:12 -07001430 mPendingAudioErr = err;
1431 mPendingAudioAccessUnit = accessUnit;
1432 accessUnit.clear();
1433 } else {
Phil Burkc5cc2e22014-09-09 20:08:39 -07001434 // Grab time from first small buffer if available.
1435 if ((bigSize == 0) && smallTimestampValid) {
1436 mAggregateBuffer->meta()->setInt64("timeUs", timeUs);
1437 }
Phil Burk9f526492014-09-03 15:04:12 -07001438 // Append small buffer to the bigger buffer.
Phil Burkc5cc2e22014-09-09 20:08:39 -07001439 memcpy(mAggregateBuffer->base() + bigSize, accessUnit->data(), smallSize);
Phil Burk9f526492014-09-03 15:04:12 -07001440 bigSize += smallSize;
Phil Burkc5cc2e22014-09-09 20:08:39 -07001441 mAggregateBuffer->setRange(0, bigSize);
Phil Burk9f526492014-09-03 15:04:12 -07001442
Phil Burkc5cc2e22014-09-09 20:08:39 -07001443 // Keep looping until we run out of room in the mAggregateBuffer.
Phil Burk9f526492014-09-03 15:04:12 -07001444 needMoreData = true;
1445
Phil Burkc5cc2e22014-09-09 20:08:39 -07001446 ALOGV("feedDecoderInputData() smallSize = %zu, bigSize = %zu, capacity = %zu",
1447 smallSize, bigSize, mAggregateBuffer->capacity());
Phil Burk9f526492014-09-03 15:04:12 -07001448 }
1449 }
1450 } while (dropAccessUnit || needMoreData);
Andreas Huberf9334412010-12-15 15:17:42 -08001451
Steve Block3856b092011-10-20 11:56:00 +01001452 // ALOGV("returned a valid buffer of %s data", audio ? "audio" : "video");
Andreas Huberf9334412010-12-15 15:17:42 -08001453
1454#if 0
1455 int64_t mediaTimeUs;
1456 CHECK(accessUnit->meta()->findInt64("timeUs", &mediaTimeUs));
Steve Block3856b092011-10-20 11:56:00 +01001457 ALOGV("feeding %s input buffer at media time %.2f secs",
Andreas Huberf9334412010-12-15 15:17:42 -08001458 audio ? "audio" : "video",
1459 mediaTimeUs / 1E6);
1460#endif
1461
Chong Zhanga7fa1d92014-06-11 14:49:23 -07001462 if (!audio) {
1463 mCCDecoder->decode(accessUnit);
1464 }
1465
Phil Burk33b51b02014-09-17 16:03:47 -07001466 if (doBufferAggregation && (mAggregateBuffer != NULL)) {
Phil Burkc5cc2e22014-09-09 20:08:39 -07001467 ALOGV("feedDecoderInputData() reply with aggregated buffer, %zu",
1468 mAggregateBuffer->size());
1469 reply->setBuffer("buffer", mAggregateBuffer);
1470 mAggregateBuffer.clear();
Phil Burk9f526492014-09-03 15:04:12 -07001471 } else {
1472 reply->setBuffer("buffer", accessUnit);
1473 }
1474
Andreas Huberf9334412010-12-15 15:17:42 -08001475 reply->post();
1476
1477 return OK;
1478}
1479
1480void NuPlayer::renderBuffer(bool audio, const sp<AMessage> &msg) {
Steve Block3856b092011-10-20 11:56:00 +01001481 // ALOGV("renderBuffer %s", audio ? "audio" : "video");
Andreas Huberf9334412010-12-15 15:17:42 -08001482
1483 sp<AMessage> reply;
1484 CHECK(msg->findMessage("reply", &reply));
1485
Wei Jia53904f32014-07-29 10:22:53 -07001486 if ((audio && mFlushingAudio != NONE)
1487 || (!audio && mFlushingVideo != NONE)) {
Andreas Huber18ac5402011-08-31 15:04:25 -07001488 // We're currently attempting to flush the decoder, in order
1489 // to complete this, the decoder wants all its buffers back,
1490 // so we don't want any output buffers it sent us (from before
1491 // we initiated the flush) to be stuck in the renderer's queue.
1492
Steve Block3856b092011-10-20 11:56:00 +01001493 ALOGV("we're still flushing the %s decoder, sending its output buffer"
Andreas Huber18ac5402011-08-31 15:04:25 -07001494 " right back.", audio ? "audio" : "video");
1495
1496 reply->post();
1497 return;
1498 }
1499
Andreas Huber2d8bedd2012-02-21 14:38:23 -08001500 sp<ABuffer> buffer;
1501 CHECK(msg->findBuffer("buffer", &buffer));
Andreas Huberf9334412010-12-15 15:17:42 -08001502
Chong Zhanga7fa1d92014-06-11 14:49:23 -07001503 int64_t mediaTimeUs;
1504 CHECK(buffer->meta()->findInt64("timeUs", &mediaTimeUs));
1505
Andreas Huber32f3cef2011-03-02 15:34:46 -08001506 int64_t &skipUntilMediaTimeUs =
1507 audio
1508 ? mSkipRenderingAudioUntilMediaTimeUs
1509 : mSkipRenderingVideoUntilMediaTimeUs;
1510
1511 if (skipUntilMediaTimeUs >= 0) {
Andreas Huber32f3cef2011-03-02 15:34:46 -08001512
1513 if (mediaTimeUs < skipUntilMediaTimeUs) {
Steve Block3856b092011-10-20 11:56:00 +01001514 ALOGV("dropping %s buffer at time %lld as requested.",
Andreas Huber32f3cef2011-03-02 15:34:46 -08001515 audio ? "audio" : "video",
1516 mediaTimeUs);
1517
1518 reply->post();
1519 return;
1520 }
1521
1522 skipUntilMediaTimeUs = -1;
1523 }
1524
Chong Zhanga7fa1d92014-06-11 14:49:23 -07001525 if (!audio && mCCDecoder->isSelected()) {
1526 mCCDecoder->display(mediaTimeUs);
1527 }
1528
Andreas Huberf9334412010-12-15 15:17:42 -08001529 mRenderer->queueBuffer(audio, buffer, reply);
1530}
1531
Chong Zhangced1c2f2014-08-08 15:22:35 -07001532void NuPlayer::updateVideoSize(
1533 const sp<AMessage> &inputFormat,
1534 const sp<AMessage> &outputFormat) {
1535 if (inputFormat == NULL) {
1536 ALOGW("Unknown video size, reporting 0x0!");
1537 notifyListener(MEDIA_SET_VIDEO_SIZE, 0, 0);
1538 return;
1539 }
1540
1541 int32_t displayWidth, displayHeight;
1542 int32_t cropLeft, cropTop, cropRight, cropBottom;
1543
1544 if (outputFormat != NULL) {
1545 int32_t width, height;
1546 CHECK(outputFormat->findInt32("width", &width));
1547 CHECK(outputFormat->findInt32("height", &height));
1548
1549 int32_t cropLeft, cropTop, cropRight, cropBottom;
1550 CHECK(outputFormat->findRect(
1551 "crop",
1552 &cropLeft, &cropTop, &cropRight, &cropBottom));
1553
1554 displayWidth = cropRight - cropLeft + 1;
1555 displayHeight = cropBottom - cropTop + 1;
1556
1557 ALOGV("Video output format changed to %d x %d "
1558 "(crop: %d x %d @ (%d, %d))",
1559 width, height,
1560 displayWidth,
1561 displayHeight,
1562 cropLeft, cropTop);
1563 } else {
1564 CHECK(inputFormat->findInt32("width", &displayWidth));
1565 CHECK(inputFormat->findInt32("height", &displayHeight));
1566
1567 ALOGV("Video input format %d x %d", displayWidth, displayHeight);
1568 }
1569
1570 // Take into account sample aspect ratio if necessary:
1571 int32_t sarWidth, sarHeight;
1572 if (inputFormat->findInt32("sar-width", &sarWidth)
1573 && inputFormat->findInt32("sar-height", &sarHeight)) {
1574 ALOGV("Sample aspect ratio %d : %d", sarWidth, sarHeight);
1575
1576 displayWidth = (displayWidth * sarWidth) / sarHeight;
1577
1578 ALOGV("display dimensions %d x %d", displayWidth, displayHeight);
1579 }
1580
1581 int32_t rotationDegrees;
1582 if (!inputFormat->findInt32("rotation-degrees", &rotationDegrees)) {
1583 rotationDegrees = 0;
1584 }
1585
1586 if (rotationDegrees == 90 || rotationDegrees == 270) {
1587 int32_t tmp = displayWidth;
1588 displayWidth = displayHeight;
1589 displayHeight = tmp;
1590 }
1591
1592 notifyListener(
1593 MEDIA_SET_VIDEO_SIZE,
1594 displayWidth,
1595 displayHeight);
1596}
1597
Chong Zhangdcb89b32013-08-06 09:44:47 -07001598void NuPlayer::notifyListener(int msg, int ext1, int ext2, const Parcel *in) {
Andreas Huber43c3e6c2011-01-05 12:17:08 -08001599 if (mDriver == NULL) {
Andreas Huberf9334412010-12-15 15:17:42 -08001600 return;
1601 }
1602
Andreas Huber43c3e6c2011-01-05 12:17:08 -08001603 sp<NuPlayerDriver> driver = mDriver.promote();
Andreas Huberf9334412010-12-15 15:17:42 -08001604
Andreas Huber43c3e6c2011-01-05 12:17:08 -08001605 if (driver == NULL) {
Andreas Huberf9334412010-12-15 15:17:42 -08001606 return;
1607 }
1608
Chong Zhangdcb89b32013-08-06 09:44:47 -07001609 driver->notifyListener(msg, ext1, ext2, in);
Andreas Huberf9334412010-12-15 15:17:42 -08001610}
1611
Lajos Molnar87603c02014-08-20 19:25:30 -07001612void NuPlayer::flushDecoder(
1613 bool audio, bool needShutdown, const sp<AMessage> &newFormat) {
Andreas Huber14f76722013-01-15 09:04:18 -08001614 ALOGV("[%s] flushDecoder needShutdown=%d",
1615 audio ? "audio" : "video", needShutdown);
1616
Lajos Molnar87603c02014-08-20 19:25:30 -07001617 const sp<Decoder> &decoder = getDecoder(audio);
1618 if (decoder == NULL) {
Steve Blockdf64d152012-01-04 20:05:49 +00001619 ALOGI("flushDecoder %s without decoder present",
Andreas Huber6e3d3112011-11-28 12:36:11 -08001620 audio ? "audio" : "video");
Lajos Molnar87603c02014-08-20 19:25:30 -07001621 return;
Andreas Huber6e3d3112011-11-28 12:36:11 -08001622 }
1623
Andreas Huber1aef2112011-01-04 14:01:29 -08001624 // Make sure we don't continue to scan sources until we finish flushing.
1625 ++mScanSourcesGeneration;
Andreas Huber43c3e6c2011-01-05 12:17:08 -08001626 mScanSourcesPending = false;
Andreas Huber1aef2112011-01-04 14:01:29 -08001627
Lajos Molnar87603c02014-08-20 19:25:30 -07001628 decoder->signalFlush(newFormat);
Andreas Huber1aef2112011-01-04 14:01:29 -08001629 mRenderer->flush(audio);
1630
1631 FlushStatus newStatus =
1632 needShutdown ? FLUSHING_DECODER_SHUTDOWN : FLUSHING_DECODER;
1633
1634 if (audio) {
Wei Jia53904f32014-07-29 10:22:53 -07001635 ALOGE_IF(mFlushingAudio != NONE,
1636 "audio flushDecoder() is called in state %d", mFlushingAudio);
Andreas Huber1aef2112011-01-04 14:01:29 -08001637 mFlushingAudio = newStatus;
Andreas Huber1aef2112011-01-04 14:01:29 -08001638 } else {
Wei Jia53904f32014-07-29 10:22:53 -07001639 ALOGE_IF(mFlushingVideo != NONE,
1640 "video flushDecoder() is called in state %d", mFlushingVideo);
Andreas Huber1aef2112011-01-04 14:01:29 -08001641 mFlushingVideo = newStatus;
Chong Zhangb86e68f2014-08-01 13:46:53 -07001642
1643 if (mCCDecoder != NULL) {
1644 mCCDecoder->flush();
1645 }
Andreas Huber1aef2112011-01-04 14:01:29 -08001646 }
1647}
1648
Lajos Molnar87603c02014-08-20 19:25:30 -07001649void NuPlayer::updateDecoderFormatWithoutFlush(
1650 bool audio, const sp<AMessage> &format) {
1651 ALOGV("[%s] updateDecoderFormatWithoutFlush", audio ? "audio" : "video");
1652
1653 const sp<Decoder> &decoder = getDecoder(audio);
1654 if (decoder == NULL) {
1655 ALOGI("updateDecoderFormatWithoutFlush %s without decoder present",
1656 audio ? "audio" : "video");
1657 return;
1658 }
1659
1660 decoder->signalUpdateFormat(format);
1661}
1662
Chong Zhangced1c2f2014-08-08 15:22:35 -07001663void NuPlayer::queueDecoderShutdown(
1664 bool audio, bool video, const sp<AMessage> &reply) {
1665 ALOGI("queueDecoderShutdown audio=%d, video=%d", audio, video);
Andreas Huber84066782011-08-16 09:34:26 -07001666
Chong Zhangced1c2f2014-08-08 15:22:35 -07001667 mDeferredActions.push_back(
1668 new ShutdownDecoderAction(audio, video));
Andreas Huber84066782011-08-16 09:34:26 -07001669
Chong Zhangced1c2f2014-08-08 15:22:35 -07001670 mDeferredActions.push_back(
1671 new SimpleAction(&NuPlayer::performScanSources));
Andreas Huber84066782011-08-16 09:34:26 -07001672
Chong Zhangced1c2f2014-08-08 15:22:35 -07001673 mDeferredActions.push_back(new PostMessageAction(reply));
1674
1675 processDeferredActions();
Andreas Huber84066782011-08-16 09:34:26 -07001676}
1677
James Dong0d268a32012-08-31 12:18:27 -07001678status_t NuPlayer::setVideoScalingMode(int32_t mode) {
1679 mVideoScalingMode = mode;
Andreas Huber57a339c2012-12-03 11:18:00 -08001680 if (mNativeWindow != NULL) {
James Dong0d268a32012-08-31 12:18:27 -07001681 status_t ret = native_window_set_scaling_mode(
1682 mNativeWindow->getNativeWindow().get(), mVideoScalingMode);
1683 if (ret != OK) {
1684 ALOGE("Failed to set scaling mode (%d): %s",
1685 -ret, strerror(-ret));
1686 return ret;
1687 }
1688 }
1689 return OK;
1690}
1691
Chong Zhangdcb89b32013-08-06 09:44:47 -07001692status_t NuPlayer::getTrackInfo(Parcel* reply) const {
1693 sp<AMessage> msg = new AMessage(kWhatGetTrackInfo, id());
1694 msg->setPointer("reply", reply);
1695
1696 sp<AMessage> response;
1697 status_t err = msg->postAndAwaitResponse(&response);
1698 return err;
1699}
1700
Robert Shih7c4f0d72014-07-09 18:53:31 -07001701status_t NuPlayer::getSelectedTrack(int32_t type, Parcel* reply) const {
1702 sp<AMessage> msg = new AMessage(kWhatGetSelectedTrack, id());
1703 msg->setPointer("reply", reply);
1704 msg->setInt32("type", type);
1705
1706 sp<AMessage> response;
1707 status_t err = msg->postAndAwaitResponse(&response);
1708 if (err == OK && response != NULL) {
1709 CHECK(response->findInt32("err", &err));
1710 }
1711 return err;
1712}
1713
Chong Zhangdcb89b32013-08-06 09:44:47 -07001714status_t NuPlayer::selectTrack(size_t trackIndex, bool select) {
1715 sp<AMessage> msg = new AMessage(kWhatSelectTrack, id());
1716 msg->setSize("trackIndex", trackIndex);
1717 msg->setInt32("select", select);
1718
1719 sp<AMessage> response;
1720 status_t err = msg->postAndAwaitResponse(&response);
1721
Chong Zhang404fced2014-06-11 14:45:31 -07001722 if (err != OK) {
1723 return err;
1724 }
1725
1726 if (!response->findInt32("err", &err)) {
1727 err = OK;
1728 }
1729
Chong Zhangdcb89b32013-08-06 09:44:47 -07001730 return err;
1731}
1732
Marco Nelissenf0b72b52014-09-16 15:43:44 -07001733sp<MetaData> NuPlayer::getFileMeta() {
1734 return mSource->getFileFormatMeta();
1735}
1736
Andreas Huberb7c8e912012-11-27 15:02:53 -08001737void NuPlayer::schedulePollDuration() {
1738 sp<AMessage> msg = new AMessage(kWhatPollDuration, id());
1739 msg->setInt32("generation", mPollDurationGeneration);
1740 msg->post();
1741}
1742
1743void NuPlayer::cancelPollDuration() {
1744 ++mPollDurationGeneration;
1745}
1746
Andreas Hubera1f8ab02012-11-30 10:53:22 -08001747void NuPlayer::processDeferredActions() {
1748 while (!mDeferredActions.empty()) {
1749 // We won't execute any deferred actions until we're no longer in
1750 // an intermediate state, i.e. one more more decoders are currently
1751 // flushing or shutting down.
1752
Andreas Hubera1f8ab02012-11-30 10:53:22 -08001753 if (mFlushingAudio != NONE || mFlushingVideo != NONE) {
1754 // We're currently flushing, postpone the reset until that's
1755 // completed.
1756
1757 ALOGV("postponing action mFlushingAudio=%d, mFlushingVideo=%d",
1758 mFlushingAudio, mFlushingVideo);
1759
1760 break;
1761 }
1762
1763 sp<Action> action = *mDeferredActions.begin();
1764 mDeferredActions.erase(mDeferredActions.begin());
1765
1766 action->execute(this);
1767 }
1768}
1769
1770void NuPlayer::performSeek(int64_t seekTimeUs) {
1771 ALOGV("performSeek seekTimeUs=%lld us (%.2f secs)",
1772 seekTimeUs,
1773 seekTimeUs / 1E6);
1774
Andy Hungadf34bf2014-09-03 18:22:22 -07001775 if (mSource == NULL) {
1776 // This happens when reset occurs right before the loop mode
1777 // asynchronously seeks to the start of the stream.
1778 LOG_ALWAYS_FATAL_IF(mAudioDecoder != NULL || mVideoDecoder != NULL,
1779 "mSource is NULL and decoders not NULL audio(%p) video(%p)",
1780 mAudioDecoder.get(), mVideoDecoder.get());
1781 return;
1782 }
Andreas Hubera1f8ab02012-11-30 10:53:22 -08001783 mSource->seekTo(seekTimeUs);
Robert Shihd3b0bbb2014-07-23 15:00:25 -07001784 ++mTimedTextGeneration;
Andreas Hubera1f8ab02012-11-30 10:53:22 -08001785
1786 if (mDriver != NULL) {
1787 sp<NuPlayerDriver> driver = mDriver.promote();
1788 if (driver != NULL) {
1789 driver->notifyPosition(seekTimeUs);
1790 driver->notifySeekComplete();
1791 }
1792 }
1793
1794 // everything's flushed, continue playback.
1795}
1796
1797void NuPlayer::performDecoderFlush() {
1798 ALOGV("performDecoderFlush");
1799
Andreas Huberda9740e2013-04-16 10:54:03 -07001800 if (mAudioDecoder == NULL && mVideoDecoder == NULL) {
Andreas Hubera1f8ab02012-11-30 10:53:22 -08001801 return;
1802 }
1803
1804 mTimeDiscontinuityPending = true;
1805
1806 if (mAudioDecoder != NULL) {
1807 flushDecoder(true /* audio */, false /* needShutdown */);
1808 }
1809
1810 if (mVideoDecoder != NULL) {
1811 flushDecoder(false /* audio */, false /* needShutdown */);
1812 }
1813}
1814
Andreas Huber14f76722013-01-15 09:04:18 -08001815void NuPlayer::performDecoderShutdown(bool audio, bool video) {
1816 ALOGV("performDecoderShutdown audio=%d, video=%d", audio, video);
Andreas Hubera1f8ab02012-11-30 10:53:22 -08001817
Andreas Huber14f76722013-01-15 09:04:18 -08001818 if ((!audio || mAudioDecoder == NULL)
1819 && (!video || mVideoDecoder == NULL)) {
Andreas Hubera1f8ab02012-11-30 10:53:22 -08001820 return;
1821 }
1822
1823 mTimeDiscontinuityPending = true;
1824
Andreas Huber14f76722013-01-15 09:04:18 -08001825 if (audio && mAudioDecoder != NULL) {
Andreas Hubera1f8ab02012-11-30 10:53:22 -08001826 flushDecoder(true /* audio */, true /* needShutdown */);
1827 }
1828
Andreas Huber14f76722013-01-15 09:04:18 -08001829 if (video && mVideoDecoder != NULL) {
Andreas Hubera1f8ab02012-11-30 10:53:22 -08001830 flushDecoder(false /* audio */, true /* needShutdown */);
1831 }
1832}
1833
1834void NuPlayer::performReset() {
1835 ALOGV("performReset");
1836
1837 CHECK(mAudioDecoder == NULL);
1838 CHECK(mVideoDecoder == NULL);
1839
1840 cancelPollDuration();
1841
1842 ++mScanSourcesGeneration;
1843 mScanSourcesPending = false;
1844
Wei Jia1008e1c2014-09-09 14:49:08 -07001845 ++mAudioDecoderGeneration;
1846 ++mVideoDecoderGeneration;
1847
Lajos Molnar09524832014-07-17 14:29:51 -07001848 if (mRendererLooper != NULL) {
1849 if (mRenderer != NULL) {
1850 mRendererLooper->unregisterHandler(mRenderer->id());
1851 }
1852 mRendererLooper->stop();
1853 mRendererLooper.clear();
1854 }
Andreas Hubera1f8ab02012-11-30 10:53:22 -08001855 mRenderer.clear();
1856
1857 if (mSource != NULL) {
1858 mSource->stop();
Andreas Huberb5f25f02013-02-05 10:14:26 -08001859
Andreas Hubera1f8ab02012-11-30 10:53:22 -08001860 mSource.clear();
1861 }
1862
1863 if (mDriver != NULL) {
1864 sp<NuPlayerDriver> driver = mDriver.promote();
1865 if (driver != NULL) {
1866 driver->notifyResetComplete();
1867 }
1868 }
Andreas Huber57a339c2012-12-03 11:18:00 -08001869
1870 mStarted = false;
Andreas Hubera1f8ab02012-11-30 10:53:22 -08001871}
1872
1873void NuPlayer::performScanSources() {
1874 ALOGV("performScanSources");
1875
Andreas Huber57a339c2012-12-03 11:18:00 -08001876 if (!mStarted) {
1877 return;
1878 }
1879
Andreas Hubera1f8ab02012-11-30 10:53:22 -08001880 if (mAudioDecoder == NULL || mVideoDecoder == NULL) {
1881 postScanSources();
1882 }
1883}
1884
Andreas Huber57a339c2012-12-03 11:18:00 -08001885void NuPlayer::performSetSurface(const sp<NativeWindowWrapper> &wrapper) {
1886 ALOGV("performSetSurface");
1887
1888 mNativeWindow = wrapper;
1889
1890 // XXX - ignore error from setVideoScalingMode for now
1891 setVideoScalingMode(mVideoScalingMode);
Chong Zhang13d6faa2014-08-22 15:35:28 -07001892
1893 if (mDriver != NULL) {
1894 sp<NuPlayerDriver> driver = mDriver.promote();
1895 if (driver != NULL) {
1896 driver->notifySetSurfaceComplete();
1897 }
1898 }
Andreas Huber57a339c2012-12-03 11:18:00 -08001899}
1900
Andreas Huber9575c962013-02-05 13:59:56 -08001901void NuPlayer::onSourceNotify(const sp<AMessage> &msg) {
1902 int32_t what;
1903 CHECK(msg->findInt32("what", &what));
1904
1905 switch (what) {
1906 case Source::kWhatPrepared:
1907 {
Andreas Huberb5f28d42013-04-25 15:11:19 -07001908 if (mSource == NULL) {
1909 // This is a stale notification from a source that was
1910 // asynchronously preparing when the client called reset().
1911 // We handled the reset, the source is gone.
1912 break;
1913 }
1914
Andreas Huberec0c5972013-02-05 14:47:13 -08001915 int32_t err;
1916 CHECK(msg->findInt32("err", &err));
1917
Andreas Huber9575c962013-02-05 13:59:56 -08001918 sp<NuPlayerDriver> driver = mDriver.promote();
1919 if (driver != NULL) {
Marco Nelissendd114d12014-05-28 15:23:14 -07001920 // notify duration first, so that it's definitely set when
1921 // the app received the "prepare complete" callback.
1922 int64_t durationUs;
1923 if (mSource->getDuration(&durationUs) == OK) {
1924 driver->notifyDuration(durationUs);
1925 }
Andreas Huberec0c5972013-02-05 14:47:13 -08001926 driver->notifyPrepareCompleted(err);
Andreas Huber9575c962013-02-05 13:59:56 -08001927 }
Andreas Huber99759402013-04-01 14:28:31 -07001928
Andreas Huber9575c962013-02-05 13:59:56 -08001929 break;
1930 }
1931
1932 case Source::kWhatFlagsChanged:
1933 {
1934 uint32_t flags;
1935 CHECK(msg->findInt32("flags", (int32_t *)&flags));
1936
Chong Zhang4b7069d2013-09-11 12:52:43 -07001937 sp<NuPlayerDriver> driver = mDriver.promote();
1938 if (driver != NULL) {
1939 driver->notifyFlagsChanged(flags);
1940 }
1941
Andreas Huber9575c962013-02-05 13:59:56 -08001942 if ((mSourceFlags & Source::FLAG_DYNAMIC_DURATION)
1943 && (!(flags & Source::FLAG_DYNAMIC_DURATION))) {
1944 cancelPollDuration();
1945 } else if (!(mSourceFlags & Source::FLAG_DYNAMIC_DURATION)
1946 && (flags & Source::FLAG_DYNAMIC_DURATION)
1947 && (mAudioDecoder != NULL || mVideoDecoder != NULL)) {
1948 schedulePollDuration();
1949 }
1950
1951 mSourceFlags = flags;
1952 break;
1953 }
1954
1955 case Source::kWhatVideoSizeChanged:
1956 {
Chong Zhangced1c2f2014-08-08 15:22:35 -07001957 sp<AMessage> format;
1958 CHECK(msg->findMessage("format", &format));
Andreas Huber9575c962013-02-05 13:59:56 -08001959
Chong Zhangced1c2f2014-08-08 15:22:35 -07001960 updateVideoSize(format);
Andreas Huber9575c962013-02-05 13:59:56 -08001961 break;
1962 }
1963
Chong Zhang2a3cc9a2014-08-21 17:48:26 -07001964 case Source::kWhatBufferingUpdate:
1965 {
1966 int32_t percentage;
1967 CHECK(msg->findInt32("percentage", &percentage));
1968
1969 notifyListener(MEDIA_BUFFERING_UPDATE, percentage, 0);
1970 break;
1971 }
1972
Roger Jönssonb50e83e2013-01-21 16:26:41 +01001973 case Source::kWhatBufferingStart:
1974 {
1975 notifyListener(MEDIA_INFO, MEDIA_INFO_BUFFERING_START, 0);
1976 break;
1977 }
1978
1979 case Source::kWhatBufferingEnd:
1980 {
1981 notifyListener(MEDIA_INFO, MEDIA_INFO_BUFFERING_END, 0);
1982 break;
1983 }
1984
Chong Zhangdcb89b32013-08-06 09:44:47 -07001985 case Source::kWhatSubtitleData:
1986 {
1987 sp<ABuffer> buffer;
1988 CHECK(msg->findBuffer("buffer", &buffer));
1989
Chong Zhang404fced2014-06-11 14:45:31 -07001990 sendSubtitleData(buffer, 0 /* baseIndex */);
Chong Zhangdcb89b32013-08-06 09:44:47 -07001991 break;
1992 }
1993
Robert Shihd3b0bbb2014-07-23 15:00:25 -07001994 case Source::kWhatTimedTextData:
1995 {
1996 int32_t generation;
1997 if (msg->findInt32("generation", &generation)
1998 && generation != mTimedTextGeneration) {
1999 break;
2000 }
2001
2002 sp<ABuffer> buffer;
2003 CHECK(msg->findBuffer("buffer", &buffer));
2004
2005 sp<NuPlayerDriver> driver = mDriver.promote();
2006 if (driver == NULL) {
2007 break;
2008 }
2009
2010 int posMs;
2011 int64_t timeUs, posUs;
2012 driver->getCurrentPosition(&posMs);
2013 posUs = posMs * 1000;
2014 CHECK(buffer->meta()->findInt64("timeUs", &timeUs));
2015
2016 if (posUs < timeUs) {
2017 if (!msg->findInt32("generation", &generation)) {
2018 msg->setInt32("generation", mTimedTextGeneration);
2019 }
2020 msg->post(timeUs - posUs);
2021 } else {
2022 sendTimedTextData(buffer);
2023 }
2024 break;
2025 }
2026
Andreas Huber14f76722013-01-15 09:04:18 -08002027 case Source::kWhatQueueDecoderShutdown:
2028 {
2029 int32_t audio, video;
2030 CHECK(msg->findInt32("audio", &audio));
2031 CHECK(msg->findInt32("video", &video));
2032
2033 sp<AMessage> reply;
2034 CHECK(msg->findMessage("reply", &reply));
2035
2036 queueDecoderShutdown(audio, video, reply);
2037 break;
2038 }
2039
Ronghua Wu80276872014-08-28 15:50:29 -07002040 case Source::kWhatDrmNoLicense:
2041 {
2042 notifyListener(MEDIA_ERROR, MEDIA_ERROR_UNKNOWN, ERROR_DRM_NO_LICENSE);
2043 break;
2044 }
2045
Andreas Huber9575c962013-02-05 13:59:56 -08002046 default:
2047 TRESPASS();
2048 }
2049}
2050
Chong Zhanga7fa1d92014-06-11 14:49:23 -07002051void NuPlayer::onClosedCaptionNotify(const sp<AMessage> &msg) {
2052 int32_t what;
2053 CHECK(msg->findInt32("what", &what));
2054
2055 switch (what) {
2056 case NuPlayer::CCDecoder::kWhatClosedCaptionData:
2057 {
2058 sp<ABuffer> buffer;
2059 CHECK(msg->findBuffer("buffer", &buffer));
2060
2061 size_t inbandTracks = 0;
2062 if (mSource != NULL) {
2063 inbandTracks = mSource->getTrackCount();
2064 }
2065
2066 sendSubtitleData(buffer, inbandTracks);
2067 break;
2068 }
2069
2070 case NuPlayer::CCDecoder::kWhatTrackAdded:
2071 {
2072 notifyListener(MEDIA_INFO, MEDIA_INFO_METADATA_UPDATE, 0);
2073
2074 break;
2075 }
2076
2077 default:
2078 TRESPASS();
2079 }
2080
2081
2082}
2083
Chong Zhang404fced2014-06-11 14:45:31 -07002084void NuPlayer::sendSubtitleData(const sp<ABuffer> &buffer, int32_t baseIndex) {
2085 int32_t trackIndex;
2086 int64_t timeUs, durationUs;
2087 CHECK(buffer->meta()->findInt32("trackIndex", &trackIndex));
2088 CHECK(buffer->meta()->findInt64("timeUs", &timeUs));
2089 CHECK(buffer->meta()->findInt64("durationUs", &durationUs));
2090
2091 Parcel in;
2092 in.writeInt32(trackIndex + baseIndex);
2093 in.writeInt64(timeUs);
2094 in.writeInt64(durationUs);
2095 in.writeInt32(buffer->size());
2096 in.writeInt32(buffer->size());
2097 in.write(buffer->data(), buffer->size());
2098
2099 notifyListener(MEDIA_SUBTITLE_DATA, 0, 0, &in);
2100}
Robert Shihd3b0bbb2014-07-23 15:00:25 -07002101
2102void NuPlayer::sendTimedTextData(const sp<ABuffer> &buffer) {
2103 const void *data;
2104 size_t size = 0;
2105 int64_t timeUs;
2106 int32_t flag = TextDescriptions::LOCAL_DESCRIPTIONS;
2107
2108 AString mime;
2109 CHECK(buffer->meta()->findString("mime", &mime));
2110 CHECK(strcasecmp(mime.c_str(), MEDIA_MIMETYPE_TEXT_3GPP) == 0);
2111
2112 data = buffer->data();
2113 size = buffer->size();
2114
2115 Parcel parcel;
2116 if (size > 0) {
2117 CHECK(buffer->meta()->findInt64("timeUs", &timeUs));
2118 flag |= TextDescriptions::IN_BAND_TEXT_3GPP;
2119 TextDescriptions::getParcelOfDescriptions(
2120 (const uint8_t *)data, size, flag, timeUs / 1000, &parcel);
2121 }
2122
2123 if ((parcel.dataSize() > 0)) {
2124 notifyListener(MEDIA_TIMED_TEXT, 0, 0, &parcel);
2125 } else { // send an empty timed text
2126 notifyListener(MEDIA_TIMED_TEXT, 0, 0);
2127 }
2128}
Andreas Huberb5f25f02013-02-05 10:14:26 -08002129////////////////////////////////////////////////////////////////////////////////
2130
Chong Zhangced1c2f2014-08-08 15:22:35 -07002131sp<AMessage> NuPlayer::Source::getFormat(bool audio) {
2132 sp<MetaData> meta = getFormatMeta(audio);
2133
2134 if (meta == NULL) {
2135 return NULL;
2136 }
2137
2138 sp<AMessage> msg = new AMessage;
2139
2140 if(convertMetaDataToMessage(meta, &msg) == OK) {
2141 return msg;
2142 }
2143 return NULL;
2144}
2145
Andreas Huber9575c962013-02-05 13:59:56 -08002146void NuPlayer::Source::notifyFlagsChanged(uint32_t flags) {
2147 sp<AMessage> notify = dupNotify();
2148 notify->setInt32("what", kWhatFlagsChanged);
2149 notify->setInt32("flags", flags);
2150 notify->post();
2151}
2152
Chong Zhangced1c2f2014-08-08 15:22:35 -07002153void NuPlayer::Source::notifyVideoSizeChanged(const sp<AMessage> &format) {
Andreas Huber9575c962013-02-05 13:59:56 -08002154 sp<AMessage> notify = dupNotify();
2155 notify->setInt32("what", kWhatVideoSizeChanged);
Chong Zhangced1c2f2014-08-08 15:22:35 -07002156 notify->setMessage("format", format);
Andreas Huber9575c962013-02-05 13:59:56 -08002157 notify->post();
2158}
2159
Andreas Huberec0c5972013-02-05 14:47:13 -08002160void NuPlayer::Source::notifyPrepared(status_t err) {
Andreas Huber9575c962013-02-05 13:59:56 -08002161 sp<AMessage> notify = dupNotify();
2162 notify->setInt32("what", kWhatPrepared);
Andreas Huberec0c5972013-02-05 14:47:13 -08002163 notify->setInt32("err", err);
Andreas Huber9575c962013-02-05 13:59:56 -08002164 notify->post();
2165}
2166
Andreas Huber84333e02014-02-07 15:36:10 -08002167void NuPlayer::Source::onMessageReceived(const sp<AMessage> & /* msg */) {
Andreas Huberb5f25f02013-02-05 10:14:26 -08002168 TRESPASS();
2169}
2170
Andreas Huberf9334412010-12-15 15:17:42 -08002171} // namespace android