blob: 4889dbcda2811d8ad8246e31eb397da26b5e982d [file] [log] [blame]
Andreas Huberf9334412010-12-15 15:17:42 -08001/*
2 * Copyright (C) 2010 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17//#define LOG_NDEBUG 0
18#define LOG_TAG "NuPlayer"
19#include <utils/Log.h>
20
21#include "NuPlayer.h"
Andreas Huber5bc087c2010-12-23 10:27:40 -080022
23#include "HTTPLiveSource.h"
Andreas Huberf9334412010-12-15 15:17:42 -080024#include "NuPlayerDecoder.h"
Wei Jiabc2fb722014-07-08 16:37:57 -070025#include "NuPlayerDecoderPassThrough.h"
Andreas Huber43c3e6c2011-01-05 12:17:08 -080026#include "NuPlayerDriver.h"
Andreas Huberf9334412010-12-15 15:17:42 -080027#include "NuPlayerRenderer.h"
Andreas Huber5bc087c2010-12-23 10:27:40 -080028#include "NuPlayerSource.h"
Andreas Huber2bfdd422011-10-11 15:24:07 -070029#include "RTSPSource.h"
Andreas Huber5bc087c2010-12-23 10:27:40 -080030#include "StreamingSource.h"
Andreas Huberafed0e12011-09-20 15:39:58 -070031#include "GenericSource.h"
Robert Shihd3b0bbb2014-07-23 15:00:25 -070032#include "TextDescriptions.h"
Andreas Huber5bc087c2010-12-23 10:27:40 -080033
34#include "ATSParser.h"
Andreas Huberf9334412010-12-15 15:17:42 -080035
Andreas Huber3831a062010-12-21 10:22:33 -080036#include <media/stagefright/foundation/hexdump.h>
Andreas Huberf9334412010-12-15 15:17:42 -080037#include <media/stagefright/foundation/ABuffer.h>
38#include <media/stagefright/foundation/ADebug.h>
39#include <media/stagefright/foundation/AMessage.h>
Lajos Molnar09524832014-07-17 14:29:51 -070040#include <media/stagefright/MediaBuffer.h>
Andreas Huber3fe62152011-09-16 15:09:22 -070041#include <media/stagefright/MediaDefs.h>
Andreas Huberf9334412010-12-15 15:17:42 -080042#include <media/stagefright/MediaErrors.h>
43#include <media/stagefright/MetaData.h>
Andy McFadden8ba01022012-12-18 09:46:54 -080044#include <gui/IGraphicBufferProducer.h>
Andreas Huberf9334412010-12-15 15:17:42 -080045
Andreas Huber3fe62152011-09-16 15:09:22 -070046#include "avc_utils.h"
47
Andreas Huber84066782011-08-16 09:34:26 -070048#include "ESDS.h"
49#include <media/stagefright/Utils.h>
50
Andreas Huberf9334412010-12-15 15:17:42 -080051namespace android {
52
Andreas Hubera1f8ab02012-11-30 10:53:22 -080053struct NuPlayer::Action : public RefBase {
54 Action() {}
55
56 virtual void execute(NuPlayer *player) = 0;
57
58private:
59 DISALLOW_EVIL_CONSTRUCTORS(Action);
60};
61
62struct NuPlayer::SeekAction : public Action {
63 SeekAction(int64_t seekTimeUs)
64 : mSeekTimeUs(seekTimeUs) {
65 }
66
67 virtual void execute(NuPlayer *player) {
68 player->performSeek(mSeekTimeUs);
69 }
70
71private:
72 int64_t mSeekTimeUs;
73
74 DISALLOW_EVIL_CONSTRUCTORS(SeekAction);
75};
76
Andreas Huber57a339c2012-12-03 11:18:00 -080077struct NuPlayer::SetSurfaceAction : public Action {
78 SetSurfaceAction(const sp<NativeWindowWrapper> &wrapper)
79 : mWrapper(wrapper) {
80 }
81
82 virtual void execute(NuPlayer *player) {
83 player->performSetSurface(mWrapper);
84 }
85
86private:
87 sp<NativeWindowWrapper> mWrapper;
88
89 DISALLOW_EVIL_CONSTRUCTORS(SetSurfaceAction);
90};
91
Andreas Huber14f76722013-01-15 09:04:18 -080092struct NuPlayer::ShutdownDecoderAction : public Action {
93 ShutdownDecoderAction(bool audio, bool video)
94 : mAudio(audio),
95 mVideo(video) {
96 }
97
98 virtual void execute(NuPlayer *player) {
99 player->performDecoderShutdown(mAudio, mVideo);
100 }
101
102private:
103 bool mAudio;
104 bool mVideo;
105
106 DISALLOW_EVIL_CONSTRUCTORS(ShutdownDecoderAction);
107};
108
109struct NuPlayer::PostMessageAction : public Action {
110 PostMessageAction(const sp<AMessage> &msg)
111 : mMessage(msg) {
112 }
113
114 virtual void execute(NuPlayer *) {
115 mMessage->post();
116 }
117
118private:
119 sp<AMessage> mMessage;
120
121 DISALLOW_EVIL_CONSTRUCTORS(PostMessageAction);
122};
123
Andreas Hubera1f8ab02012-11-30 10:53:22 -0800124// Use this if there's no state necessary to save in order to execute
125// the action.
126struct NuPlayer::SimpleAction : public Action {
127 typedef void (NuPlayer::*ActionFunc)();
128
129 SimpleAction(ActionFunc func)
130 : mFunc(func) {
131 }
132
133 virtual void execute(NuPlayer *player) {
134 (player->*mFunc)();
135 }
136
137private:
138 ActionFunc mFunc;
139
140 DISALLOW_EVIL_CONSTRUCTORS(SimpleAction);
141};
142
Andreas Huberf9334412010-12-15 15:17:42 -0800143////////////////////////////////////////////////////////////////////////////////
144
145NuPlayer::NuPlayer()
Andreas Huber9b80c2b2011-06-30 15:47:02 -0700146 : mUIDValid(false),
Andreas Huber9575c962013-02-05 13:59:56 -0800147 mSourceFlags(0),
Andreas Huber3fe62152011-09-16 15:09:22 -0700148 mVideoIsAVC(false),
Wei Jiabc2fb722014-07-08 16:37:57 -0700149 mOffloadAudio(false),
Andreas Huber9b80c2b2011-06-30 15:47:02 -0700150 mAudioEOS(false),
Andreas Huberf9334412010-12-15 15:17:42 -0800151 mVideoEOS(false),
Andreas Huber5bc087c2010-12-23 10:27:40 -0800152 mScanSourcesPending(false),
Andreas Huber1aef2112011-01-04 14:01:29 -0800153 mScanSourcesGeneration(0),
Andreas Huberb7c8e912012-11-27 15:02:53 -0800154 mPollDurationGeneration(0),
Robert Shihd3b0bbb2014-07-23 15:00:25 -0700155 mTimedTextGeneration(0),
Andreas Huber6e3d3112011-11-28 12:36:11 -0800156 mTimeDiscontinuityPending(false),
Andreas Huberf9334412010-12-15 15:17:42 -0800157 mFlushingAudio(NONE),
Andreas Huber1aef2112011-01-04 14:01:29 -0800158 mFlushingVideo(NONE),
Andreas Huber3fe62152011-09-16 15:09:22 -0700159 mSkipRenderingAudioUntilMediaTimeUs(-1ll),
160 mSkipRenderingVideoUntilMediaTimeUs(-1ll),
161 mVideoLateByUs(0ll),
162 mNumFramesTotal(0ll),
James Dong0d268a32012-08-31 12:18:27 -0700163 mNumFramesDropped(0ll),
Andreas Huber57a339c2012-12-03 11:18:00 -0800164 mVideoScalingMode(NATIVE_WINDOW_SCALING_MODE_SCALE_TO_WINDOW),
165 mStarted(false) {
Andreas Huberf9334412010-12-15 15:17:42 -0800166}
167
168NuPlayer::~NuPlayer() {
169}
170
Andreas Huber9b80c2b2011-06-30 15:47:02 -0700171void NuPlayer::setUID(uid_t uid) {
172 mUIDValid = true;
173 mUID = uid;
174}
175
Andreas Huber43c3e6c2011-01-05 12:17:08 -0800176void NuPlayer::setDriver(const wp<NuPlayerDriver> &driver) {
177 mDriver = driver;
Andreas Huberf9334412010-12-15 15:17:42 -0800178}
179
Andreas Huber9575c962013-02-05 13:59:56 -0800180void NuPlayer::setDataSourceAsync(const sp<IStreamSource> &source) {
Andreas Huberf9334412010-12-15 15:17:42 -0800181 sp<AMessage> msg = new AMessage(kWhatSetDataSource, id());
182
Andreas Huberb5f25f02013-02-05 10:14:26 -0800183 sp<AMessage> notify = new AMessage(kWhatSourceNotify, id());
184
Andreas Huber240abcc2014-02-13 13:32:37 -0800185 msg->setObject("source", new StreamingSource(notify, source));
Andreas Huber5bc087c2010-12-23 10:27:40 -0800186 msg->post();
187}
Andreas Huberf9334412010-12-15 15:17:42 -0800188
Andreas Huberafed0e12011-09-20 15:39:58 -0700189static bool IsHTTPLiveURL(const char *url) {
190 if (!strncasecmp("http://", url, 7)
Andreas Huber99759402013-04-01 14:28:31 -0700191 || !strncasecmp("https://", url, 8)
192 || !strncasecmp("file://", url, 7)) {
Andreas Huberafed0e12011-09-20 15:39:58 -0700193 size_t len = strlen(url);
194 if (len >= 5 && !strcasecmp(".m3u8", &url[len - 5])) {
195 return true;
196 }
197
198 if (strstr(url,"m3u8")) {
199 return true;
200 }
201 }
202
203 return false;
204}
205
Andreas Huber9575c962013-02-05 13:59:56 -0800206void NuPlayer::setDataSourceAsync(
Andreas Huber1b86fe02014-01-29 11:13:26 -0800207 const sp<IMediaHTTPService> &httpService,
208 const char *url,
209 const KeyedVector<String8, String8> *headers) {
Chong Zhang3de157d2014-08-05 20:54:44 -0700210
Andreas Huber5bc087c2010-12-23 10:27:40 -0800211 sp<AMessage> msg = new AMessage(kWhatSetDataSource, id());
Oscar Rydhé7a33b772012-02-20 10:15:48 +0100212 size_t len = strlen(url);
Andreas Huber5bc087c2010-12-23 10:27:40 -0800213
Andreas Huberb5f25f02013-02-05 10:14:26 -0800214 sp<AMessage> notify = new AMessage(kWhatSourceNotify, id());
215
Andreas Huberafed0e12011-09-20 15:39:58 -0700216 sp<Source> source;
217 if (IsHTTPLiveURL(url)) {
Andreas Huber81e68442014-02-05 11:52:33 -0800218 source = new HTTPLiveSource(notify, httpService, url, headers);
Andreas Huberafed0e12011-09-20 15:39:58 -0700219 } else if (!strncasecmp(url, "rtsp://", 7)) {
Andreas Huber1b86fe02014-01-29 11:13:26 -0800220 source = new RTSPSource(
221 notify, httpService, url, headers, mUIDValid, mUID);
Oscar Rydhé7a33b772012-02-20 10:15:48 +0100222 } else if ((!strncasecmp(url, "http://", 7)
223 || !strncasecmp(url, "https://", 8))
224 && ((len >= 4 && !strcasecmp(".sdp", &url[len - 4]))
225 || strstr(url, ".sdp?"))) {
Andreas Huber1b86fe02014-01-29 11:13:26 -0800226 source = new RTSPSource(
227 notify, httpService, url, headers, mUIDValid, mUID, true);
Andreas Huber2bfdd422011-10-11 15:24:07 -0700228 } else {
Chong Zhang3de157d2014-08-05 20:54:44 -0700229 sp<GenericSource> genericSource =
230 new GenericSource(notify, mUIDValid, mUID);
231 // Don't set FLAG_SECURE on mSourceFlags here for widevine.
232 // The correct flags will be updated in Source::kWhatFlagsChanged
233 // handler when GenericSource is prepared.
Andreas Huber2bfdd422011-10-11 15:24:07 -0700234
Chong Zhanga19f33e2014-08-07 15:35:07 -0700235 status_t err = genericSource->setDataSource(httpService, url, headers);
Chong Zhang3de157d2014-08-05 20:54:44 -0700236
237 if (err == OK) {
238 source = genericSource;
239 } else {
Chong Zhanga19f33e2014-08-07 15:35:07 -0700240 ALOGE("Failed to set data source!");
Chong Zhang3de157d2014-08-05 20:54:44 -0700241 }
242 }
Andreas Huberafed0e12011-09-20 15:39:58 -0700243 msg->setObject("source", source);
244 msg->post();
245}
246
Andreas Huber9575c962013-02-05 13:59:56 -0800247void NuPlayer::setDataSourceAsync(int fd, int64_t offset, int64_t length) {
Andreas Huberafed0e12011-09-20 15:39:58 -0700248 sp<AMessage> msg = new AMessage(kWhatSetDataSource, id());
249
Andreas Huberb5f25f02013-02-05 10:14:26 -0800250 sp<AMessage> notify = new AMessage(kWhatSourceNotify, id());
251
Chong Zhang3de157d2014-08-05 20:54:44 -0700252 sp<GenericSource> source =
253 new GenericSource(notify, mUIDValid, mUID);
254
Chong Zhanga19f33e2014-08-07 15:35:07 -0700255 status_t err = source->setDataSource(fd, offset, length);
Chong Zhang3de157d2014-08-05 20:54:44 -0700256
257 if (err != OK) {
Chong Zhanga19f33e2014-08-07 15:35:07 -0700258 ALOGE("Failed to set data source!");
Chong Zhang3de157d2014-08-05 20:54:44 -0700259 source = NULL;
260 }
261
Andreas Huberafed0e12011-09-20 15:39:58 -0700262 msg->setObject("source", source);
Andreas Huberf9334412010-12-15 15:17:42 -0800263 msg->post();
264}
265
Andreas Huber9575c962013-02-05 13:59:56 -0800266void NuPlayer::prepareAsync() {
267 (new AMessage(kWhatPrepare, id()))->post();
268}
269
Andreas Huber57a339c2012-12-03 11:18:00 -0800270void NuPlayer::setVideoSurfaceTextureAsync(
Andy McFadden8ba01022012-12-18 09:46:54 -0800271 const sp<IGraphicBufferProducer> &bufferProducer) {
Glenn Kasten11731182011-02-08 17:26:17 -0800272 sp<AMessage> msg = new AMessage(kWhatSetVideoNativeWindow, id());
Andreas Huber57a339c2012-12-03 11:18:00 -0800273
Andy McFadden8ba01022012-12-18 09:46:54 -0800274 if (bufferProducer == NULL) {
Andreas Huber57a339c2012-12-03 11:18:00 -0800275 msg->setObject("native-window", NULL);
276 } else {
277 msg->setObject(
278 "native-window",
279 new NativeWindowWrapper(
Mathias Agopian1a2952a2013-02-14 17:11:27 -0800280 new Surface(bufferProducer)));
Andreas Huber57a339c2012-12-03 11:18:00 -0800281 }
282
Andreas Huberf9334412010-12-15 15:17:42 -0800283 msg->post();
284}
285
286void NuPlayer::setAudioSink(const sp<MediaPlayerBase::AudioSink> &sink) {
287 sp<AMessage> msg = new AMessage(kWhatSetAudioSink, id());
288 msg->setObject("sink", sink);
289 msg->post();
290}
291
292void NuPlayer::start() {
293 (new AMessage(kWhatStart, id()))->post();
294}
295
Andreas Huber43c3e6c2011-01-05 12:17:08 -0800296void NuPlayer::pause() {
Andreas Huberb4082222011-01-20 15:23:04 -0800297 (new AMessage(kWhatPause, id()))->post();
Andreas Huber43c3e6c2011-01-05 12:17:08 -0800298}
299
300void NuPlayer::resume() {
Andreas Huberb4082222011-01-20 15:23:04 -0800301 (new AMessage(kWhatResume, id()))->post();
Andreas Huber43c3e6c2011-01-05 12:17:08 -0800302}
303
Andreas Huber1aef2112011-01-04 14:01:29 -0800304void NuPlayer::resetAsync() {
305 (new AMessage(kWhatReset, id()))->post();
306}
307
Andreas Huber43c3e6c2011-01-05 12:17:08 -0800308void NuPlayer::seekToAsync(int64_t seekTimeUs) {
309 sp<AMessage> msg = new AMessage(kWhatSeek, id());
310 msg->setInt64("seekTimeUs", seekTimeUs);
311 msg->post();
312}
313
Andreas Huber53df1a42010-12-22 10:03:04 -0800314// static
Andreas Huber1aef2112011-01-04 14:01:29 -0800315bool NuPlayer::IsFlushingState(FlushStatus state, bool *needShutdown) {
Andreas Huber53df1a42010-12-22 10:03:04 -0800316 switch (state) {
317 case FLUSHING_DECODER:
Andreas Huber1aef2112011-01-04 14:01:29 -0800318 if (needShutdown != NULL) {
319 *needShutdown = false;
Andreas Huber53df1a42010-12-22 10:03:04 -0800320 }
321 return true;
322
Andreas Huber1aef2112011-01-04 14:01:29 -0800323 case FLUSHING_DECODER_SHUTDOWN:
324 if (needShutdown != NULL) {
325 *needShutdown = true;
Andreas Huber53df1a42010-12-22 10:03:04 -0800326 }
327 return true;
328
329 default:
330 return false;
331 }
332}
333
Chong Zhang404fced2014-06-11 14:45:31 -0700334void NuPlayer::writeTrackInfo(
335 Parcel* reply, const sp<AMessage> format) const {
336 int32_t trackType;
337 CHECK(format->findInt32("type", &trackType));
338
339 AString lang;
340 CHECK(format->findString("language", &lang));
341
342 reply->writeInt32(2); // write something non-zero
343 reply->writeInt32(trackType);
344 reply->writeString16(String16(lang.c_str()));
345
346 if (trackType == MEDIA_TRACK_TYPE_SUBTITLE) {
347 AString mime;
348 CHECK(format->findString("mime", &mime));
349
350 int32_t isAuto, isDefault, isForced;
351 CHECK(format->findInt32("auto", &isAuto));
352 CHECK(format->findInt32("default", &isDefault));
353 CHECK(format->findInt32("forced", &isForced));
354
355 reply->writeString16(String16(mime.c_str()));
356 reply->writeInt32(isAuto);
357 reply->writeInt32(isDefault);
358 reply->writeInt32(isForced);
359 }
360}
361
Andreas Huberf9334412010-12-15 15:17:42 -0800362void NuPlayer::onMessageReceived(const sp<AMessage> &msg) {
363 switch (msg->what()) {
364 case kWhatSetDataSource:
365 {
Steve Block3856b092011-10-20 11:56:00 +0100366 ALOGV("kWhatSetDataSource");
Andreas Huberf9334412010-12-15 15:17:42 -0800367
368 CHECK(mSource == NULL);
369
Chong Zhang3de157d2014-08-05 20:54:44 -0700370 status_t err = OK;
Andreas Huber5bc087c2010-12-23 10:27:40 -0800371 sp<RefBase> obj;
372 CHECK(msg->findObject("source", &obj));
Chong Zhang3de157d2014-08-05 20:54:44 -0700373 if (obj != NULL) {
374 mSource = static_cast<Source *>(obj.get());
375 looper()->registerHandler(mSource);
376 } else {
377 err = UNKNOWN_ERROR;
378 }
Andreas Huber9575c962013-02-05 13:59:56 -0800379
380 CHECK(mDriver != NULL);
381 sp<NuPlayerDriver> driver = mDriver.promote();
382 if (driver != NULL) {
Chong Zhang3de157d2014-08-05 20:54:44 -0700383 driver->notifySetDataSourceCompleted(err);
Andreas Huber9575c962013-02-05 13:59:56 -0800384 }
385 break;
386 }
387
388 case kWhatPrepare:
389 {
390 mSource->prepareAsync();
Andreas Huberf9334412010-12-15 15:17:42 -0800391 break;
392 }
393
Chong Zhangdcb89b32013-08-06 09:44:47 -0700394 case kWhatGetTrackInfo:
395 {
396 uint32_t replyID;
397 CHECK(msg->senderAwaitsResponse(&replyID));
398
Chong Zhang404fced2014-06-11 14:45:31 -0700399 Parcel* reply;
400 CHECK(msg->findPointer("reply", (void**)&reply));
401
402 size_t inbandTracks = 0;
Chong Zhangdcb89b32013-08-06 09:44:47 -0700403 if (mSource != NULL) {
Chong Zhang404fced2014-06-11 14:45:31 -0700404 inbandTracks = mSource->getTrackCount();
405 }
406
Chong Zhanga7fa1d92014-06-11 14:49:23 -0700407 size_t ccTracks = 0;
408 if (mCCDecoder != NULL) {
409 ccTracks = mCCDecoder->getTrackCount();
410 }
411
Chong Zhang404fced2014-06-11 14:45:31 -0700412 // total track count
Chong Zhanga7fa1d92014-06-11 14:49:23 -0700413 reply->writeInt32(inbandTracks + ccTracks);
Chong Zhang404fced2014-06-11 14:45:31 -0700414
415 // write inband tracks
416 for (size_t i = 0; i < inbandTracks; ++i) {
417 writeTrackInfo(reply, mSource->getTrackInfo(i));
Chong Zhangdcb89b32013-08-06 09:44:47 -0700418 }
419
Chong Zhanga7fa1d92014-06-11 14:49:23 -0700420 // write CC track
421 for (size_t i = 0; i < ccTracks; ++i) {
422 writeTrackInfo(reply, mCCDecoder->getTrackInfo(i));
423 }
424
Chong Zhangdcb89b32013-08-06 09:44:47 -0700425 sp<AMessage> response = new AMessage;
Chong Zhangdcb89b32013-08-06 09:44:47 -0700426 response->postReply(replyID);
427 break;
428 }
429
430 case kWhatSelectTrack:
431 {
432 uint32_t replyID;
433 CHECK(msg->senderAwaitsResponse(&replyID));
434
Chong Zhang404fced2014-06-11 14:45:31 -0700435 size_t trackIndex;
436 int32_t select;
437 CHECK(msg->findSize("trackIndex", &trackIndex));
438 CHECK(msg->findInt32("select", &select));
439
Chong Zhangdcb89b32013-08-06 09:44:47 -0700440 status_t err = INVALID_OPERATION;
Chong Zhang404fced2014-06-11 14:45:31 -0700441
442 size_t inbandTracks = 0;
Chong Zhangdcb89b32013-08-06 09:44:47 -0700443 if (mSource != NULL) {
Chong Zhang404fced2014-06-11 14:45:31 -0700444 inbandTracks = mSource->getTrackCount();
445 }
Chong Zhanga7fa1d92014-06-11 14:49:23 -0700446 size_t ccTracks = 0;
447 if (mCCDecoder != NULL) {
448 ccTracks = mCCDecoder->getTrackCount();
449 }
Chong Zhang404fced2014-06-11 14:45:31 -0700450
451 if (trackIndex < inbandTracks) {
Chong Zhangdcb89b32013-08-06 09:44:47 -0700452 err = mSource->selectTrack(trackIndex, select);
Robert Shihd3b0bbb2014-07-23 15:00:25 -0700453
454 if (!select && err == OK) {
455 int32_t type;
456 sp<AMessage> info = mSource->getTrackInfo(trackIndex);
457 if (info != NULL
458 && info->findInt32("type", &type)
459 && type == MEDIA_TRACK_TYPE_TIMEDTEXT) {
460 ++mTimedTextGeneration;
461 }
462 }
Chong Zhanga7fa1d92014-06-11 14:49:23 -0700463 } else {
464 trackIndex -= inbandTracks;
465
466 if (trackIndex < ccTracks) {
467 err = mCCDecoder->selectTrack(trackIndex, select);
468 }
Chong Zhangdcb89b32013-08-06 09:44:47 -0700469 }
470
471 sp<AMessage> response = new AMessage;
472 response->setInt32("err", err);
473
474 response->postReply(replyID);
475 break;
476 }
477
Andreas Huberb7c8e912012-11-27 15:02:53 -0800478 case kWhatPollDuration:
479 {
480 int32_t generation;
481 CHECK(msg->findInt32("generation", &generation));
482
483 if (generation != mPollDurationGeneration) {
484 // stale
485 break;
486 }
487
488 int64_t durationUs;
489 if (mDriver != NULL && mSource->getDuration(&durationUs) == OK) {
490 sp<NuPlayerDriver> driver = mDriver.promote();
491 if (driver != NULL) {
492 driver->notifyDuration(durationUs);
493 }
494 }
495
496 msg->post(1000000ll); // poll again in a second.
497 break;
498 }
499
Glenn Kasten11731182011-02-08 17:26:17 -0800500 case kWhatSetVideoNativeWindow:
Andreas Huberf9334412010-12-15 15:17:42 -0800501 {
Steve Block3856b092011-10-20 11:56:00 +0100502 ALOGV("kWhatSetVideoNativeWindow");
Andreas Huberf9334412010-12-15 15:17:42 -0800503
Andreas Huber57a339c2012-12-03 11:18:00 -0800504 mDeferredActions.push_back(
Andreas Huber14f76722013-01-15 09:04:18 -0800505 new ShutdownDecoderAction(
506 false /* audio */, true /* video */));
Andreas Huber57a339c2012-12-03 11:18:00 -0800507
Andreas Huberf9334412010-12-15 15:17:42 -0800508 sp<RefBase> obj;
Glenn Kasten11731182011-02-08 17:26:17 -0800509 CHECK(msg->findObject("native-window", &obj));
Andreas Huberf9334412010-12-15 15:17:42 -0800510
Andreas Huber57a339c2012-12-03 11:18:00 -0800511 mDeferredActions.push_back(
512 new SetSurfaceAction(
513 static_cast<NativeWindowWrapper *>(obj.get())));
James Dong0d268a32012-08-31 12:18:27 -0700514
Andreas Huber57a339c2012-12-03 11:18:00 -0800515 if (obj != NULL) {
516 // If there is a new surface texture, instantiate decoders
517 // again if possible.
518 mDeferredActions.push_back(
519 new SimpleAction(&NuPlayer::performScanSources));
520 }
521
522 processDeferredActions();
Andreas Huberf9334412010-12-15 15:17:42 -0800523 break;
524 }
525
526 case kWhatSetAudioSink:
527 {
Steve Block3856b092011-10-20 11:56:00 +0100528 ALOGV("kWhatSetAudioSink");
Andreas Huberf9334412010-12-15 15:17:42 -0800529
530 sp<RefBase> obj;
531 CHECK(msg->findObject("sink", &obj));
532
533 mAudioSink = static_cast<MediaPlayerBase::AudioSink *>(obj.get());
534 break;
535 }
536
537 case kWhatStart:
538 {
Steve Block3856b092011-10-20 11:56:00 +0100539 ALOGV("kWhatStart");
Andreas Huber43c3e6c2011-01-05 12:17:08 -0800540
Andreas Huber3fe62152011-09-16 15:09:22 -0700541 mVideoIsAVC = false;
Wei Jiabc2fb722014-07-08 16:37:57 -0700542 mOffloadAudio = false;
Andreas Huber1aef2112011-01-04 14:01:29 -0800543 mAudioEOS = false;
544 mVideoEOS = false;
Andreas Huber32f3cef2011-03-02 15:34:46 -0800545 mSkipRenderingAudioUntilMediaTimeUs = -1;
546 mSkipRenderingVideoUntilMediaTimeUs = -1;
Andreas Huber3fe62152011-09-16 15:09:22 -0700547 mVideoLateByUs = 0;
548 mNumFramesTotal = 0;
549 mNumFramesDropped = 0;
Andreas Huber57a339c2012-12-03 11:18:00 -0800550 mStarted = true;
Andreas Huber1aef2112011-01-04 14:01:29 -0800551
Lajos Molnar09524832014-07-17 14:29:51 -0700552 /* instantiate decoders now for secure playback */
553 if (mSourceFlags & Source::FLAG_SECURE) {
554 if (mNativeWindow != NULL) {
555 instantiateDecoder(false, &mVideoDecoder);
556 }
557
558 if (mAudioSink != NULL) {
559 instantiateDecoder(true, &mAudioDecoder);
560 }
561 }
562
Andreas Huber5bc087c2010-12-23 10:27:40 -0800563 mSource->start();
Andreas Huberf9334412010-12-15 15:17:42 -0800564
Andreas Huberd5e56232013-03-12 11:01:43 -0700565 uint32_t flags = 0;
566
567 if (mSource->isRealTime()) {
568 flags |= Renderer::FLAG_REAL_TIME;
569 }
570
Wei Jiabc2fb722014-07-08 16:37:57 -0700571 sp<MetaData> audioMeta = mSource->getFormatMeta(true /* audio */);
572 audio_stream_type_t streamType = AUDIO_STREAM_MUSIC;
573 if (mAudioSink != NULL) {
574 streamType = mAudioSink->getAudioStreamType();
575 }
576
577 sp<AMessage> videoFormat = mSource->getFormat(false /* audio */);
578
579 mOffloadAudio =
580 canOffloadStream(audioMeta, (videoFormat != NULL),
581 true /* is_streaming */, streamType);
582 if (mOffloadAudio) {
583 flags |= Renderer::FLAG_OFFLOAD_AUDIO;
584 }
585
Andreas Huberf9334412010-12-15 15:17:42 -0800586 mRenderer = new Renderer(
587 mAudioSink,
Andreas Huberd5e56232013-03-12 11:01:43 -0700588 new AMessage(kWhatRendererNotify, id()),
589 flags);
Andreas Huberf9334412010-12-15 15:17:42 -0800590
Lajos Molnar09524832014-07-17 14:29:51 -0700591 mRendererLooper = new ALooper;
592 mRendererLooper->setName("NuPlayerRenderer");
593 mRendererLooper->start(false, false, ANDROID_PRIORITY_AUDIO);
594 mRendererLooper->registerHandler(mRenderer);
Andreas Huberf9334412010-12-15 15:17:42 -0800595
Andreas Huber1aef2112011-01-04 14:01:29 -0800596 postScanSources();
Andreas Huberf9334412010-12-15 15:17:42 -0800597 break;
598 }
599
600 case kWhatScanSources:
601 {
Andreas Huber1aef2112011-01-04 14:01:29 -0800602 int32_t generation;
603 CHECK(msg->findInt32("generation", &generation));
604 if (generation != mScanSourcesGeneration) {
605 // Drop obsolete msg.
606 break;
607 }
608
Andreas Huber5bc087c2010-12-23 10:27:40 -0800609 mScanSourcesPending = false;
610
Steve Block3856b092011-10-20 11:56:00 +0100611 ALOGV("scanning sources haveAudio=%d, haveVideo=%d",
Andreas Huber43c3e6c2011-01-05 12:17:08 -0800612 mAudioDecoder != NULL, mVideoDecoder != NULL);
613
Andreas Huberb7c8e912012-11-27 15:02:53 -0800614 bool mHadAnySourcesBefore =
615 (mAudioDecoder != NULL) || (mVideoDecoder != NULL);
616
Haynes Mathew George5d246ef2012-07-09 10:36:57 -0700617 if (mNativeWindow != NULL) {
618 instantiateDecoder(false, &mVideoDecoder);
619 }
Andreas Huberf9334412010-12-15 15:17:42 -0800620
621 if (mAudioSink != NULL) {
Andreas Huber5bc087c2010-12-23 10:27:40 -0800622 instantiateDecoder(true, &mAudioDecoder);
Andreas Huberf9334412010-12-15 15:17:42 -0800623 }
624
Andreas Huberb7c8e912012-11-27 15:02:53 -0800625 if (!mHadAnySourcesBefore
626 && (mAudioDecoder != NULL || mVideoDecoder != NULL)) {
627 // This is the first time we've found anything playable.
628
Andreas Huber9575c962013-02-05 13:59:56 -0800629 if (mSourceFlags & Source::FLAG_DYNAMIC_DURATION) {
Andreas Huberb7c8e912012-11-27 15:02:53 -0800630 schedulePollDuration();
631 }
632 }
633
Andreas Hubereac68ba2011-09-27 12:12:25 -0700634 status_t err;
635 if ((err = mSource->feedMoreTSData()) != OK) {
Andreas Huber1aef2112011-01-04 14:01:29 -0800636 if (mAudioDecoder == NULL && mVideoDecoder == NULL) {
637 // We're not currently decoding anything (no audio or
638 // video tracks found) and we just ran out of input data.
Andreas Hubereac68ba2011-09-27 12:12:25 -0700639
640 if (err == ERROR_END_OF_STREAM) {
641 notifyListener(MEDIA_PLAYBACK_COMPLETE, 0, 0);
642 } else {
643 notifyListener(MEDIA_ERROR, MEDIA_ERROR_UNKNOWN, err);
644 }
Andreas Huber1aef2112011-01-04 14:01:29 -0800645 }
Andreas Huberf9334412010-12-15 15:17:42 -0800646 break;
647 }
648
Andreas Huberfbe9d812012-08-31 14:05:27 -0700649 if ((mAudioDecoder == NULL && mAudioSink != NULL)
650 || (mVideoDecoder == NULL && mNativeWindow != NULL)) {
Andreas Huberf9334412010-12-15 15:17:42 -0800651 msg->post(100000ll);
Andreas Huber5bc087c2010-12-23 10:27:40 -0800652 mScanSourcesPending = true;
Andreas Huberf9334412010-12-15 15:17:42 -0800653 }
654 break;
655 }
656
657 case kWhatVideoNotify:
658 case kWhatAudioNotify:
659 {
660 bool audio = msg->what() == kWhatAudioNotify;
661
Andreas Huberf9334412010-12-15 15:17:42 -0800662 int32_t what;
Lajos Molnar1cd13982014-01-17 15:12:51 -0800663 CHECK(msg->findInt32("what", &what));
Andreas Huberf9334412010-12-15 15:17:42 -0800664
Lajos Molnar1cd13982014-01-17 15:12:51 -0800665 if (what == Decoder::kWhatFillThisBuffer) {
Andreas Huberf9334412010-12-15 15:17:42 -0800666 status_t err = feedDecoderInputData(
Lajos Molnar1cd13982014-01-17 15:12:51 -0800667 audio, msg);
Andreas Huberf9334412010-12-15 15:17:42 -0800668
Andreas Huber5bc087c2010-12-23 10:27:40 -0800669 if (err == -EWOULDBLOCK) {
Andreas Hubereac68ba2011-09-27 12:12:25 -0700670 if (mSource->feedMoreTSData() == OK) {
Andreas Huber1183a4a2011-11-03 11:00:21 -0700671 msg->post(10000ll);
Andreas Huber5bc087c2010-12-23 10:27:40 -0800672 }
Andreas Huberf9334412010-12-15 15:17:42 -0800673 }
Lajos Molnar1cd13982014-01-17 15:12:51 -0800674 } else if (what == Decoder::kWhatEOS) {
Andreas Huberdc9bacd2011-09-26 10:53:29 -0700675 int32_t err;
Lajos Molnar1cd13982014-01-17 15:12:51 -0800676 CHECK(msg->findInt32("err", &err));
Andreas Huberdc9bacd2011-09-26 10:53:29 -0700677
678 if (err == ERROR_END_OF_STREAM) {
Steve Block3856b092011-10-20 11:56:00 +0100679 ALOGV("got %s decoder EOS", audio ? "audio" : "video");
Andreas Huberdc9bacd2011-09-26 10:53:29 -0700680 } else {
Steve Block3856b092011-10-20 11:56:00 +0100681 ALOGV("got %s decoder EOS w/ error %d",
Andreas Huberdc9bacd2011-09-26 10:53:29 -0700682 audio ? "audio" : "video",
683 err);
684 }
685
686 mRenderer->queueEOS(audio, err);
Lajos Molnar1cd13982014-01-17 15:12:51 -0800687 } else if (what == Decoder::kWhatFlushCompleted) {
Andreas Huber1aef2112011-01-04 14:01:29 -0800688 bool needShutdown;
Andreas Huber53df1a42010-12-22 10:03:04 -0800689
Andreas Huberf9334412010-12-15 15:17:42 -0800690 if (audio) {
Andreas Huber1aef2112011-01-04 14:01:29 -0800691 CHECK(IsFlushingState(mFlushingAudio, &needShutdown));
Andreas Huberf9334412010-12-15 15:17:42 -0800692 mFlushingAudio = FLUSHED;
693 } else {
Andreas Huber1aef2112011-01-04 14:01:29 -0800694 CHECK(IsFlushingState(mFlushingVideo, &needShutdown));
Andreas Huberf9334412010-12-15 15:17:42 -0800695 mFlushingVideo = FLUSHED;
Andreas Huber3fe62152011-09-16 15:09:22 -0700696
697 mVideoLateByUs = 0;
Andreas Huberf9334412010-12-15 15:17:42 -0800698 }
699
Steve Block3856b092011-10-20 11:56:00 +0100700 ALOGV("decoder %s flush completed", audio ? "audio" : "video");
Andreas Huberf9334412010-12-15 15:17:42 -0800701
Andreas Huber1aef2112011-01-04 14:01:29 -0800702 if (needShutdown) {
Steve Block3856b092011-10-20 11:56:00 +0100703 ALOGV("initiating %s decoder shutdown",
Andreas Huber53df1a42010-12-22 10:03:04 -0800704 audio ? "audio" : "video");
Andreas Huberf9334412010-12-15 15:17:42 -0800705
Andreas Huber53df1a42010-12-22 10:03:04 -0800706 (audio ? mAudioDecoder : mVideoDecoder)->initiateShutdown();
Andreas Huberf9334412010-12-15 15:17:42 -0800707
Andreas Huber53df1a42010-12-22 10:03:04 -0800708 if (audio) {
709 mFlushingAudio = SHUTTING_DOWN_DECODER;
710 } else {
711 mFlushingVideo = SHUTTING_DOWN_DECODER;
712 }
Andreas Huberf9334412010-12-15 15:17:42 -0800713 }
Andreas Huber3831a062010-12-21 10:22:33 -0800714
715 finishFlushIfPossible();
Lajos Molnar1cd13982014-01-17 15:12:51 -0800716 } else if (what == Decoder::kWhatOutputFormatChanged) {
717 sp<AMessage> format;
718 CHECK(msg->findMessage("format", &format));
719
Andreas Huber31e25082011-01-10 10:38:31 -0800720 if (audio) {
721 int32_t numChannels;
Lajos Molnar1cd13982014-01-17 15:12:51 -0800722 CHECK(format->findInt32(
Andreas Huber516dacf2012-12-03 15:20:40 -0800723 "channel-count", &numChannels));
Andreas Huber2c2814b2010-12-15 17:18:20 -0800724
Andreas Huber31e25082011-01-10 10:38:31 -0800725 int32_t sampleRate;
Lajos Molnar1cd13982014-01-17 15:12:51 -0800726 CHECK(format->findInt32("sample-rate", &sampleRate));
Andreas Huber2c2814b2010-12-15 17:18:20 -0800727
Steve Block3856b092011-10-20 11:56:00 +0100728 ALOGV("Audio output format changed to %d Hz, %d channels",
Andreas Huber31e25082011-01-10 10:38:31 -0800729 sampleRate, numChannels);
Andreas Huber2c2814b2010-12-15 17:18:20 -0800730
Andreas Huber31e25082011-01-10 10:38:31 -0800731 mAudioSink->close();
Eric Laurent1948eb32012-04-13 16:50:19 -0700732
Wei Jiabc2fb722014-07-08 16:37:57 -0700733 uint32_t flags;
Eric Laurent1948eb32012-04-13 16:50:19 -0700734 int64_t durationUs;
Andreas Huber516dacf2012-12-03 15:20:40 -0800735 // FIXME: we should handle the case where the video decoder
736 // is created after we receive the format change indication.
737 // Current code will just make that we select deep buffer
738 // with video which should not be a problem as it should
Eric Laurent1948eb32012-04-13 16:50:19 -0700739 // not prevent from keeping A/V sync.
740 if (mVideoDecoder == NULL &&
741 mSource->getDuration(&durationUs) == OK &&
Andreas Huber516dacf2012-12-03 15:20:40 -0800742 durationUs
743 > AUDIO_SINK_MIN_DEEP_BUFFER_DURATION_US) {
Eric Laurent1948eb32012-04-13 16:50:19 -0700744 flags = AUDIO_OUTPUT_FLAG_DEEP_BUFFER;
745 } else {
746 flags = AUDIO_OUTPUT_FLAG_NONE;
747 }
748
Andreas Huber98065552012-05-03 11:33:01 -0700749 int32_t channelMask;
Lajos Molnar1cd13982014-01-17 15:12:51 -0800750 if (!format->findInt32("channel-mask", &channelMask)) {
Andreas Huber98065552012-05-03 11:33:01 -0700751 channelMask = CHANNEL_MASK_USE_CHANNEL_ORDER;
752 }
753
Wei Jiabc2fb722014-07-08 16:37:57 -0700754 if (mOffloadAudio) {
755 audio_format_t audioFormat = AUDIO_FORMAT_PCM_16_BIT;
756 audio_offload_info_t offloadInfo =
757 AUDIO_INFO_INITIALIZER;
758
759 AString mime;
760 CHECK(format->findString("mime", &mime));
761
762 status_t err =
763 mapMimeToAudioFormat(audioFormat, mime.c_str());
764 if (err != OK) {
765 ALOGE("Couldn't map mime \"%s\" to a valid "
766 "audio_format", mime.c_str());
767 mOffloadAudio = false;
768 } else {
769 ALOGV("Mime \"%s\" mapped to audio_format 0x%x",
770 mime.c_str(), audioFormat);
771
aarti jadhav-gaikwadccad7862014-08-02 16:21:32 +0530772 int32_t aacProfile = -1;
773 if (audioFormat == AUDIO_FORMAT_AAC
774 && format->findInt32("aac-profile", &aacProfile)) {
775 // Redefine AAC format as per aac profile
776 mapAACProfileToAudioFormat(
777 audioFormat,
778 aacProfile);
779 }
780
Wei Jiabc2fb722014-07-08 16:37:57 -0700781 flags |= AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD;
782
783 offloadInfo.duration_us = -1;
784 format->findInt64(
785 "durationUs", &offloadInfo.duration_us);
786
787 int avgBitRate = -1;
788 format->findInt32("bit-rate", &avgBitRate);
789
790 offloadInfo.sample_rate = sampleRate;
791 offloadInfo.channel_mask = channelMask;
792 offloadInfo.format = audioFormat;
793 offloadInfo.stream_type = AUDIO_STREAM_MUSIC;
794 offloadInfo.bit_rate = avgBitRate;
795 offloadInfo.has_video = (mVideoDecoder != NULL);
796 offloadInfo.is_streaming = true;
797
Wei Jia3a2956d2014-07-22 16:01:33 -0700798 ALOGV("try to open AudioSink in offload mode");
Wei Jiabc2fb722014-07-08 16:37:57 -0700799 err = mAudioSink->open(
800 sampleRate,
801 numChannels,
802 (audio_channel_mask_t)channelMask,
803 audioFormat,
804 8 /* bufferCount */,
805 &NuPlayer::Renderer::AudioSinkCallback,
806 mRenderer.get(),
807 (audio_output_flags_t)flags,
808 &offloadInfo);
809
810 if (err == OK) {
811 // If the playback is offloaded to h/w, we pass
812 // the HAL some metadata information.
813 // We don't want to do this for PCM because it
814 // will be going through the AudioFlinger mixer
815 // before reaching the hardware.
816 sp<MetaData> audioMeta =
817 mSource->getFormatMeta(true /* audio */);
818 sendMetaDataToHal(mAudioSink, audioMeta);
819
820 err = mAudioSink->start();
821 }
822 }
823
824 if (err != OK) {
825 // Clean up, fall back to non offload mode.
826 mAudioSink->close();
827 mAudioDecoder.clear();
828 mRenderer->signalDisableOffloadAudio();
829 mOffloadAudio = false;
830
831 instantiateDecoder(
832 true /* audio */, &mAudioDecoder);
833 }
834 }
835
836 if (!mOffloadAudio) {
837 flags &= ~AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD;
Wei Jia3a2956d2014-07-22 16:01:33 -0700838 ALOGV("open AudioSink in NON-offload mode");
Wei Jiabc2fb722014-07-08 16:37:57 -0700839 CHECK_EQ(mAudioSink->open(
840 sampleRate,
841 numChannels,
842 (audio_channel_mask_t)channelMask,
843 AUDIO_FORMAT_PCM_16_BIT,
844 8 /* bufferCount */,
845 NULL,
846 NULL,
847 (audio_output_flags_t)flags),
848 (status_t)OK);
849 mAudioSink->start();
850 }
Andreas Huber2c2814b2010-12-15 17:18:20 -0800851
Andreas Huber31e25082011-01-10 10:38:31 -0800852 mRenderer->signalAudioSinkChanged();
853 } else {
854 // video
Chong Zhangced1c2f2014-08-08 15:22:35 -0700855 sp<AMessage> inputFormat =
856 mSource->getFormat(false /* audio */);
Andreas Huber3831a062010-12-21 10:22:33 -0800857
Chong Zhangced1c2f2014-08-08 15:22:35 -0700858 updateVideoSize(inputFormat, format);
Andreas Huber31e25082011-01-10 10:38:31 -0800859 }
Lajos Molnar1cd13982014-01-17 15:12:51 -0800860 } else if (what == Decoder::kWhatShutdownCompleted) {
Steve Block3856b092011-10-20 11:56:00 +0100861 ALOGV("%s shutdown completed", audio ? "audio" : "video");
Andreas Huber3831a062010-12-21 10:22:33 -0800862 if (audio) {
863 mAudioDecoder.clear();
864
865 CHECK_EQ((int)mFlushingAudio, (int)SHUTTING_DOWN_DECODER);
866 mFlushingAudio = SHUT_DOWN;
867 } else {
868 mVideoDecoder.clear();
869
870 CHECK_EQ((int)mFlushingVideo, (int)SHUTTING_DOWN_DECODER);
871 mFlushingVideo = SHUT_DOWN;
872 }
873
874 finishFlushIfPossible();
Lajos Molnar1cd13982014-01-17 15:12:51 -0800875 } else if (what == Decoder::kWhatError) {
Steve Block29357bc2012-01-06 19:20:56 +0000876 ALOGE("Received error from %s decoder, aborting playback.",
Andreas Huberc92fd242011-08-16 13:48:44 -0700877 audio ? "audio" : "video");
878
Chong Zhangf4c0a942014-08-11 15:14:10 -0700879 status_t err;
880 if (!msg->findInt32("err", &err)) {
881 err = UNKNOWN_ERROR;
882 }
883 mRenderer->queueEOS(audio, err);
Lajos Molnar1cd13982014-01-17 15:12:51 -0800884 } else if (what == Decoder::kWhatDrainThisBuffer) {
885 renderBuffer(audio, msg);
886 } else {
887 ALOGV("Unhandled decoder notification %d '%c%c%c%c'.",
Andreas Hubera1f8ab02012-11-30 10:53:22 -0800888 what,
889 what >> 24,
890 (what >> 16) & 0xff,
891 (what >> 8) & 0xff,
892 what & 0xff);
Andreas Huberf9334412010-12-15 15:17:42 -0800893 }
894
895 break;
896 }
897
898 case kWhatRendererNotify:
899 {
900 int32_t what;
901 CHECK(msg->findInt32("what", &what));
902
903 if (what == Renderer::kWhatEOS) {
904 int32_t audio;
905 CHECK(msg->findInt32("audio", &audio));
906
Andreas Huberc92fd242011-08-16 13:48:44 -0700907 int32_t finalResult;
908 CHECK(msg->findInt32("finalResult", &finalResult));
909
Andreas Huberf9334412010-12-15 15:17:42 -0800910 if (audio) {
911 mAudioEOS = true;
912 } else {
913 mVideoEOS = true;
914 }
915
Andreas Huberc92fd242011-08-16 13:48:44 -0700916 if (finalResult == ERROR_END_OF_STREAM) {
Steve Block3856b092011-10-20 11:56:00 +0100917 ALOGV("reached %s EOS", audio ? "audio" : "video");
Andreas Huberc92fd242011-08-16 13:48:44 -0700918 } else {
Steve Block29357bc2012-01-06 19:20:56 +0000919 ALOGE("%s track encountered an error (%d)",
Andreas Huberc92fd242011-08-16 13:48:44 -0700920 audio ? "audio" : "video", finalResult);
921
922 notifyListener(
923 MEDIA_ERROR, MEDIA_ERROR_UNKNOWN, finalResult);
924 }
Andreas Huberf9334412010-12-15 15:17:42 -0800925
926 if ((mAudioEOS || mAudioDecoder == NULL)
927 && (mVideoEOS || mVideoDecoder == NULL)) {
928 notifyListener(MEDIA_PLAYBACK_COMPLETE, 0, 0);
929 }
Andreas Huber43c3e6c2011-01-05 12:17:08 -0800930 } else if (what == Renderer::kWhatPosition) {
931 int64_t positionUs;
932 CHECK(msg->findInt64("positionUs", &positionUs));
933
Andreas Huber3fe62152011-09-16 15:09:22 -0700934 CHECK(msg->findInt64("videoLateByUs", &mVideoLateByUs));
935
Andreas Huber43c3e6c2011-01-05 12:17:08 -0800936 if (mDriver != NULL) {
937 sp<NuPlayerDriver> driver = mDriver.promote();
938 if (driver != NULL) {
939 driver->notifyPosition(positionUs);
Andreas Huber3fe62152011-09-16 15:09:22 -0700940
941 driver->notifyFrameStats(
942 mNumFramesTotal, mNumFramesDropped);
Andreas Huber43c3e6c2011-01-05 12:17:08 -0800943 }
944 }
Andreas Huber3fe62152011-09-16 15:09:22 -0700945 } else if (what == Renderer::kWhatFlushComplete) {
Andreas Huberf9334412010-12-15 15:17:42 -0800946 int32_t audio;
947 CHECK(msg->findInt32("audio", &audio));
948
Steve Block3856b092011-10-20 11:56:00 +0100949 ALOGV("renderer %s flush completed.", audio ? "audio" : "video");
James Dongf57b4ea2012-07-20 13:38:36 -0700950 } else if (what == Renderer::kWhatVideoRenderingStart) {
951 notifyListener(MEDIA_INFO, MEDIA_INFO_RENDERING_START, 0);
Lajos Molnarcbaffcf2013-08-14 18:30:38 -0700952 } else if (what == Renderer::kWhatMediaRenderingStart) {
953 ALOGV("media rendering started");
954 notifyListener(MEDIA_STARTED, 0, 0);
Wei Jia3a2956d2014-07-22 16:01:33 -0700955 } else if (what == Renderer::kWhatAudioOffloadTearDown) {
956 ALOGV("Tear down audio offload, fall back to s/w path");
957 int64_t positionUs;
958 CHECK(msg->findInt64("positionUs", &positionUs));
959 mAudioSink->close();
960 mAudioDecoder.clear();
961 mRenderer->flush(true /* audio */);
962 if (mVideoDecoder != NULL) {
963 mRenderer->flush(false /* audio */);
964 }
965 mRenderer->signalDisableOffloadAudio();
966 mOffloadAudio = false;
967
968 performSeek(positionUs);
969 instantiateDecoder(true /* audio */, &mAudioDecoder);
Andreas Huberf9334412010-12-15 15:17:42 -0800970 }
971 break;
972 }
973
974 case kWhatMoreDataQueued:
975 {
976 break;
977 }
978
Andreas Huber1aef2112011-01-04 14:01:29 -0800979 case kWhatReset:
980 {
Steve Block3856b092011-10-20 11:56:00 +0100981 ALOGV("kWhatReset");
Andreas Huber1aef2112011-01-04 14:01:29 -0800982
Andreas Hubera1f8ab02012-11-30 10:53:22 -0800983 mDeferredActions.push_back(
Andreas Huber14f76722013-01-15 09:04:18 -0800984 new ShutdownDecoderAction(
985 true /* audio */, true /* video */));
Andreas Huberb7c8e912012-11-27 15:02:53 -0800986
Andreas Hubera1f8ab02012-11-30 10:53:22 -0800987 mDeferredActions.push_back(
988 new SimpleAction(&NuPlayer::performReset));
Andreas Huberb58ce9f2011-11-28 16:27:35 -0800989
Andreas Hubera1f8ab02012-11-30 10:53:22 -0800990 processDeferredActions();
Andreas Huber1aef2112011-01-04 14:01:29 -0800991 break;
992 }
993
Andreas Huber43c3e6c2011-01-05 12:17:08 -0800994 case kWhatSeek:
995 {
996 int64_t seekTimeUs;
997 CHECK(msg->findInt64("seekTimeUs", &seekTimeUs));
998
Andreas Hubera1f8ab02012-11-30 10:53:22 -0800999 ALOGV("kWhatSeek seekTimeUs=%lld us", seekTimeUs);
Andreas Huber43c3e6c2011-01-05 12:17:08 -08001000
Andreas Hubera1f8ab02012-11-30 10:53:22 -08001001 mDeferredActions.push_back(
1002 new SimpleAction(&NuPlayer::performDecoderFlush));
Andreas Huber43c3e6c2011-01-05 12:17:08 -08001003
Andreas Hubera1f8ab02012-11-30 10:53:22 -08001004 mDeferredActions.push_back(new SeekAction(seekTimeUs));
Andreas Huber43c3e6c2011-01-05 12:17:08 -08001005
Andreas Hubera1f8ab02012-11-30 10:53:22 -08001006 processDeferredActions();
Andreas Huber43c3e6c2011-01-05 12:17:08 -08001007 break;
1008 }
1009
Andreas Huberb4082222011-01-20 15:23:04 -08001010 case kWhatPause:
1011 {
1012 CHECK(mRenderer != NULL);
Roger Jönssonfba60da2013-01-21 17:15:45 +01001013 mSource->pause();
Andreas Huberb4082222011-01-20 15:23:04 -08001014 mRenderer->pause();
1015 break;
1016 }
1017
1018 case kWhatResume:
1019 {
1020 CHECK(mRenderer != NULL);
Roger Jönssonfba60da2013-01-21 17:15:45 +01001021 mSource->resume();
Andreas Huberb4082222011-01-20 15:23:04 -08001022 mRenderer->resume();
1023 break;
1024 }
1025
Andreas Huberb5f25f02013-02-05 10:14:26 -08001026 case kWhatSourceNotify:
1027 {
Andreas Huber9575c962013-02-05 13:59:56 -08001028 onSourceNotify(msg);
Andreas Huberb5f25f02013-02-05 10:14:26 -08001029 break;
1030 }
1031
Chong Zhanga7fa1d92014-06-11 14:49:23 -07001032 case kWhatClosedCaptionNotify:
1033 {
1034 onClosedCaptionNotify(msg);
1035 break;
1036 }
1037
Andreas Huberf9334412010-12-15 15:17:42 -08001038 default:
1039 TRESPASS();
1040 break;
1041 }
1042}
1043
Andreas Huber3831a062010-12-21 10:22:33 -08001044void NuPlayer::finishFlushIfPossible() {
1045 if (mFlushingAudio != FLUSHED && mFlushingAudio != SHUT_DOWN) {
1046 return;
1047 }
1048
1049 if (mFlushingVideo != FLUSHED && mFlushingVideo != SHUT_DOWN) {
1050 return;
1051 }
1052
Steve Block3856b092011-10-20 11:56:00 +01001053 ALOGV("both audio and video are flushed now.");
Andreas Huber3831a062010-12-21 10:22:33 -08001054
Andreas Huber6e3d3112011-11-28 12:36:11 -08001055 if (mTimeDiscontinuityPending) {
1056 mRenderer->signalTimeDiscontinuity();
1057 mTimeDiscontinuityPending = false;
1058 }
Andreas Huber3831a062010-12-21 10:22:33 -08001059
Andreas Huber22fc52f2011-01-05 16:24:27 -08001060 if (mAudioDecoder != NULL) {
Andreas Huber3831a062010-12-21 10:22:33 -08001061 mAudioDecoder->signalResume();
1062 }
1063
Andreas Huber22fc52f2011-01-05 16:24:27 -08001064 if (mVideoDecoder != NULL) {
Andreas Huber3831a062010-12-21 10:22:33 -08001065 mVideoDecoder->signalResume();
1066 }
1067
1068 mFlushingAudio = NONE;
1069 mFlushingVideo = NONE;
Andreas Huber3831a062010-12-21 10:22:33 -08001070
Andreas Hubera1f8ab02012-11-30 10:53:22 -08001071 processDeferredActions();
Andreas Huber1aef2112011-01-04 14:01:29 -08001072}
1073
1074void NuPlayer::postScanSources() {
1075 if (mScanSourcesPending) {
1076 return;
1077 }
1078
1079 sp<AMessage> msg = new AMessage(kWhatScanSources, id());
1080 msg->setInt32("generation", mScanSourcesGeneration);
1081 msg->post();
1082
1083 mScanSourcesPending = true;
1084}
1085
Andreas Huber5bc087c2010-12-23 10:27:40 -08001086status_t NuPlayer::instantiateDecoder(bool audio, sp<Decoder> *decoder) {
Andreas Huberf9334412010-12-15 15:17:42 -08001087 if (*decoder != NULL) {
1088 return OK;
1089 }
1090
Andreas Huber84066782011-08-16 09:34:26 -07001091 sp<AMessage> format = mSource->getFormat(audio);
Andreas Huberf9334412010-12-15 15:17:42 -08001092
Andreas Huber84066782011-08-16 09:34:26 -07001093 if (format == NULL) {
Andreas Huberf9334412010-12-15 15:17:42 -08001094 return -EWOULDBLOCK;
1095 }
1096
Andreas Huber3fe62152011-09-16 15:09:22 -07001097 if (!audio) {
Andreas Huber84066782011-08-16 09:34:26 -07001098 AString mime;
1099 CHECK(format->findString("mime", &mime));
1100 mVideoIsAVC = !strcasecmp(MEDIA_MIMETYPE_VIDEO_AVC, mime.c_str());
Chong Zhanga7fa1d92014-06-11 14:49:23 -07001101
1102 sp<AMessage> ccNotify = new AMessage(kWhatClosedCaptionNotify, id());
1103 mCCDecoder = new CCDecoder(ccNotify);
Lajos Molnar09524832014-07-17 14:29:51 -07001104
1105 if (mSourceFlags & Source::FLAG_SECURE) {
1106 format->setInt32("secure", true);
1107 }
Andreas Huber3fe62152011-09-16 15:09:22 -07001108 }
1109
Andreas Huberf9334412010-12-15 15:17:42 -08001110 sp<AMessage> notify =
1111 new AMessage(audio ? kWhatAudioNotify : kWhatVideoNotify,
1112 id());
1113
Wei Jiabc2fb722014-07-08 16:37:57 -07001114 if (audio) {
1115 if (mOffloadAudio) {
1116 *decoder = new DecoderPassThrough(notify);
1117 } else {
1118 *decoder = new Decoder(notify);
1119 }
1120 } else {
1121 *decoder = new Decoder(notify, mNativeWindow);
1122 }
Lajos Molnar1cd13982014-01-17 15:12:51 -08001123 (*decoder)->init();
Andreas Huber84066782011-08-16 09:34:26 -07001124 (*decoder)->configure(format);
Andreas Huberf9334412010-12-15 15:17:42 -08001125
Lajos Molnar09524832014-07-17 14:29:51 -07001126 // allocate buffers to decrypt widevine source buffers
1127 if (!audio && (mSourceFlags & Source::FLAG_SECURE)) {
1128 Vector<sp<ABuffer> > inputBufs;
1129 CHECK_EQ((*decoder)->getInputBuffers(&inputBufs), (status_t)OK);
1130
1131 Vector<MediaBuffer *> mediaBufs;
1132 for (size_t i = 0; i < inputBufs.size(); i++) {
1133 const sp<ABuffer> &buffer = inputBufs[i];
1134 MediaBuffer *mbuf = new MediaBuffer(buffer->data(), buffer->size());
1135 mediaBufs.push(mbuf);
1136 }
1137
1138 status_t err = mSource->setBuffers(audio, mediaBufs);
1139 if (err != OK) {
1140 for (size_t i = 0; i < mediaBufs.size(); ++i) {
1141 mediaBufs[i]->release();
1142 }
1143 mediaBufs.clear();
1144 ALOGE("Secure source didn't support secure mediaBufs.");
1145 return err;
1146 }
1147 }
Andreas Huberf9334412010-12-15 15:17:42 -08001148 return OK;
1149}
1150
1151status_t NuPlayer::feedDecoderInputData(bool audio, const sp<AMessage> &msg) {
1152 sp<AMessage> reply;
1153 CHECK(msg->findMessage("reply", &reply));
1154
Wei Jiab189a5b2014-08-07 06:11:39 +00001155 if ((audio && IsFlushingState(mFlushingAudio))
1156 || (!audio && IsFlushingState(mFlushingVideo))) {
1157 reply->setInt32("err", INFO_DISCONTINUITY);
1158 reply->post();
1159 return OK;
Andreas Huberf9334412010-12-15 15:17:42 -08001160 }
1161
1162 sp<ABuffer> accessUnit;
Andreas Huberf9334412010-12-15 15:17:42 -08001163
Andreas Huber3fe62152011-09-16 15:09:22 -07001164 bool dropAccessUnit;
1165 do {
1166 status_t err = mSource->dequeueAccessUnit(audio, &accessUnit);
Andreas Huber5bc087c2010-12-23 10:27:40 -08001167
Andreas Huber3fe62152011-09-16 15:09:22 -07001168 if (err == -EWOULDBLOCK) {
1169 return err;
1170 } else if (err != OK) {
1171 if (err == INFO_DISCONTINUITY) {
1172 int32_t type;
1173 CHECK(accessUnit->meta()->findInt32("discontinuity", &type));
Andreas Huber53df1a42010-12-22 10:03:04 -08001174
Andreas Huber3fe62152011-09-16 15:09:22 -07001175 bool formatChange =
Andreas Huber6e3d3112011-11-28 12:36:11 -08001176 (audio &&
1177 (type & ATSParser::DISCONTINUITY_AUDIO_FORMAT))
1178 || (!audio &&
1179 (type & ATSParser::DISCONTINUITY_VIDEO_FORMAT));
Andreas Huber53df1a42010-12-22 10:03:04 -08001180
Andreas Huber6e3d3112011-11-28 12:36:11 -08001181 bool timeChange = (type & ATSParser::DISCONTINUITY_TIME) != 0;
1182
Steve Blockdf64d152012-01-04 20:05:49 +00001183 ALOGI("%s discontinuity (formatChange=%d, time=%d)",
Andreas Huber6e3d3112011-11-28 12:36:11 -08001184 audio ? "audio" : "video", formatChange, timeChange);
Andreas Huber32f3cef2011-03-02 15:34:46 -08001185
Andreas Huber3fe62152011-09-16 15:09:22 -07001186 if (audio) {
1187 mSkipRenderingAudioUntilMediaTimeUs = -1;
1188 } else {
1189 mSkipRenderingVideoUntilMediaTimeUs = -1;
1190 }
Andreas Huber32f3cef2011-03-02 15:34:46 -08001191
Andreas Huber6e3d3112011-11-28 12:36:11 -08001192 if (timeChange) {
1193 sp<AMessage> extra;
1194 if (accessUnit->meta()->findMessage("extra", &extra)
1195 && extra != NULL) {
1196 int64_t resumeAtMediaTimeUs;
1197 if (extra->findInt64(
1198 "resume-at-mediatimeUs", &resumeAtMediaTimeUs)) {
Steve Blockdf64d152012-01-04 20:05:49 +00001199 ALOGI("suppressing rendering of %s until %lld us",
Andreas Huber6e3d3112011-11-28 12:36:11 -08001200 audio ? "audio" : "video", resumeAtMediaTimeUs);
Andreas Huber3fe62152011-09-16 15:09:22 -07001201
Andreas Huber6e3d3112011-11-28 12:36:11 -08001202 if (audio) {
1203 mSkipRenderingAudioUntilMediaTimeUs =
1204 resumeAtMediaTimeUs;
1205 } else {
1206 mSkipRenderingVideoUntilMediaTimeUs =
1207 resumeAtMediaTimeUs;
1208 }
Andreas Huber3fe62152011-09-16 15:09:22 -07001209 }
Andreas Huber32f3cef2011-03-02 15:34:46 -08001210 }
1211 }
Andreas Huber3fe62152011-09-16 15:09:22 -07001212
Andreas Huber6e3d3112011-11-28 12:36:11 -08001213 mTimeDiscontinuityPending =
1214 mTimeDiscontinuityPending || timeChange;
1215
Robert Shiha2981012014-07-30 17:41:24 -07001216 if (mFlushingAudio == NONE && mFlushingVideo == NONE) {
1217 // And we'll resume scanning sources once we're done
1218 // flushing.
1219 mDeferredActions.push_front(
1220 new SimpleAction(
1221 &NuPlayer::performScanSources));
1222 }
1223
Andreas Huber6e3d3112011-11-28 12:36:11 -08001224 if (formatChange || timeChange) {
Andreas Hubera1f8ab02012-11-30 10:53:22 -08001225
Robert Shih6d0a94e2014-01-23 16:18:22 -08001226 sp<AMessage> newFormat = mSource->getFormat(audio);
1227 sp<Decoder> &decoder = audio ? mAudioDecoder : mVideoDecoder;
1228 if (formatChange && !decoder->supportsSeamlessFormatChange(newFormat)) {
1229 flushDecoder(audio, /* needShutdown = */ true);
1230 } else {
1231 flushDecoder(audio, /* needShutdown = */ false);
1232 err = OK;
1233 }
Andreas Huber6e3d3112011-11-28 12:36:11 -08001234 } else {
1235 // This stream is unaffected by the discontinuity
1236
1237 if (audio) {
1238 mFlushingAudio = FLUSHED;
1239 } else {
1240 mFlushingVideo = FLUSHED;
1241 }
1242
1243 finishFlushIfPossible();
1244
1245 return -EWOULDBLOCK;
1246 }
Andreas Huber32f3cef2011-03-02 15:34:46 -08001247 }
1248
Andreas Huber3fe62152011-09-16 15:09:22 -07001249 reply->setInt32("err", err);
1250 reply->post();
1251 return OK;
Andreas Huberf9334412010-12-15 15:17:42 -08001252 }
1253
Andreas Huber3fe62152011-09-16 15:09:22 -07001254 if (!audio) {
1255 ++mNumFramesTotal;
1256 }
1257
1258 dropAccessUnit = false;
1259 if (!audio
Lajos Molnar09524832014-07-17 14:29:51 -07001260 && !(mSourceFlags & Source::FLAG_SECURE)
Andreas Huber3fe62152011-09-16 15:09:22 -07001261 && mVideoLateByUs > 100000ll
1262 && mVideoIsAVC
1263 && !IsAVCReferenceFrame(accessUnit)) {
1264 dropAccessUnit = true;
1265 ++mNumFramesDropped;
1266 }
1267 } while (dropAccessUnit);
Andreas Huberf9334412010-12-15 15:17:42 -08001268
Steve Block3856b092011-10-20 11:56:00 +01001269 // ALOGV("returned a valid buffer of %s data", audio ? "audio" : "video");
Andreas Huberf9334412010-12-15 15:17:42 -08001270
1271#if 0
1272 int64_t mediaTimeUs;
1273 CHECK(accessUnit->meta()->findInt64("timeUs", &mediaTimeUs));
Steve Block3856b092011-10-20 11:56:00 +01001274 ALOGV("feeding %s input buffer at media time %.2f secs",
Andreas Huberf9334412010-12-15 15:17:42 -08001275 audio ? "audio" : "video",
1276 mediaTimeUs / 1E6);
1277#endif
1278
Chong Zhanga7fa1d92014-06-11 14:49:23 -07001279 if (!audio) {
1280 mCCDecoder->decode(accessUnit);
1281 }
1282
Andreas Huber2d8bedd2012-02-21 14:38:23 -08001283 reply->setBuffer("buffer", accessUnit);
Andreas Huberf9334412010-12-15 15:17:42 -08001284 reply->post();
1285
1286 return OK;
1287}
1288
1289void NuPlayer::renderBuffer(bool audio, const sp<AMessage> &msg) {
Steve Block3856b092011-10-20 11:56:00 +01001290 // ALOGV("renderBuffer %s", audio ? "audio" : "video");
Andreas Huberf9334412010-12-15 15:17:42 -08001291
1292 sp<AMessage> reply;
1293 CHECK(msg->findMessage("reply", &reply));
1294
Andreas Huber18ac5402011-08-31 15:04:25 -07001295 if (IsFlushingState(audio ? mFlushingAudio : mFlushingVideo)) {
1296 // We're currently attempting to flush the decoder, in order
1297 // to complete this, the decoder wants all its buffers back,
1298 // so we don't want any output buffers it sent us (from before
1299 // we initiated the flush) to be stuck in the renderer's queue.
1300
Steve Block3856b092011-10-20 11:56:00 +01001301 ALOGV("we're still flushing the %s decoder, sending its output buffer"
Andreas Huber18ac5402011-08-31 15:04:25 -07001302 " right back.", audio ? "audio" : "video");
1303
1304 reply->post();
1305 return;
1306 }
1307
Andreas Huber2d8bedd2012-02-21 14:38:23 -08001308 sp<ABuffer> buffer;
1309 CHECK(msg->findBuffer("buffer", &buffer));
Andreas Huberf9334412010-12-15 15:17:42 -08001310
Chong Zhanga7fa1d92014-06-11 14:49:23 -07001311 int64_t mediaTimeUs;
1312 CHECK(buffer->meta()->findInt64("timeUs", &mediaTimeUs));
1313
Andreas Huber32f3cef2011-03-02 15:34:46 -08001314 int64_t &skipUntilMediaTimeUs =
1315 audio
1316 ? mSkipRenderingAudioUntilMediaTimeUs
1317 : mSkipRenderingVideoUntilMediaTimeUs;
1318
1319 if (skipUntilMediaTimeUs >= 0) {
Andreas Huber32f3cef2011-03-02 15:34:46 -08001320
1321 if (mediaTimeUs < skipUntilMediaTimeUs) {
Steve Block3856b092011-10-20 11:56:00 +01001322 ALOGV("dropping %s buffer at time %lld as requested.",
Andreas Huber32f3cef2011-03-02 15:34:46 -08001323 audio ? "audio" : "video",
1324 mediaTimeUs);
1325
1326 reply->post();
1327 return;
1328 }
1329
1330 skipUntilMediaTimeUs = -1;
1331 }
1332
Chong Zhanga7fa1d92014-06-11 14:49:23 -07001333 if (!audio && mCCDecoder->isSelected()) {
1334 mCCDecoder->display(mediaTimeUs);
1335 }
1336
Andreas Huberf9334412010-12-15 15:17:42 -08001337 mRenderer->queueBuffer(audio, buffer, reply);
1338}
1339
Chong Zhangced1c2f2014-08-08 15:22:35 -07001340void NuPlayer::updateVideoSize(
1341 const sp<AMessage> &inputFormat,
1342 const sp<AMessage> &outputFormat) {
1343 if (inputFormat == NULL) {
1344 ALOGW("Unknown video size, reporting 0x0!");
1345 notifyListener(MEDIA_SET_VIDEO_SIZE, 0, 0);
1346 return;
1347 }
1348
1349 int32_t displayWidth, displayHeight;
1350 int32_t cropLeft, cropTop, cropRight, cropBottom;
1351
1352 if (outputFormat != NULL) {
1353 int32_t width, height;
1354 CHECK(outputFormat->findInt32("width", &width));
1355 CHECK(outputFormat->findInt32("height", &height));
1356
1357 int32_t cropLeft, cropTop, cropRight, cropBottom;
1358 CHECK(outputFormat->findRect(
1359 "crop",
1360 &cropLeft, &cropTop, &cropRight, &cropBottom));
1361
1362 displayWidth = cropRight - cropLeft + 1;
1363 displayHeight = cropBottom - cropTop + 1;
1364
1365 ALOGV("Video output format changed to %d x %d "
1366 "(crop: %d x %d @ (%d, %d))",
1367 width, height,
1368 displayWidth,
1369 displayHeight,
1370 cropLeft, cropTop);
1371 } else {
1372 CHECK(inputFormat->findInt32("width", &displayWidth));
1373 CHECK(inputFormat->findInt32("height", &displayHeight));
1374
1375 ALOGV("Video input format %d x %d", displayWidth, displayHeight);
1376 }
1377
1378 // Take into account sample aspect ratio if necessary:
1379 int32_t sarWidth, sarHeight;
1380 if (inputFormat->findInt32("sar-width", &sarWidth)
1381 && inputFormat->findInt32("sar-height", &sarHeight)) {
1382 ALOGV("Sample aspect ratio %d : %d", sarWidth, sarHeight);
1383
1384 displayWidth = (displayWidth * sarWidth) / sarHeight;
1385
1386 ALOGV("display dimensions %d x %d", displayWidth, displayHeight);
1387 }
1388
1389 int32_t rotationDegrees;
1390 if (!inputFormat->findInt32("rotation-degrees", &rotationDegrees)) {
1391 rotationDegrees = 0;
1392 }
1393
1394 if (rotationDegrees == 90 || rotationDegrees == 270) {
1395 int32_t tmp = displayWidth;
1396 displayWidth = displayHeight;
1397 displayHeight = tmp;
1398 }
1399
1400 notifyListener(
1401 MEDIA_SET_VIDEO_SIZE,
1402 displayWidth,
1403 displayHeight);
1404}
1405
Chong Zhangdcb89b32013-08-06 09:44:47 -07001406void NuPlayer::notifyListener(int msg, int ext1, int ext2, const Parcel *in) {
Andreas Huber43c3e6c2011-01-05 12:17:08 -08001407 if (mDriver == NULL) {
Andreas Huberf9334412010-12-15 15:17:42 -08001408 return;
1409 }
1410
Andreas Huber43c3e6c2011-01-05 12:17:08 -08001411 sp<NuPlayerDriver> driver = mDriver.promote();
Andreas Huberf9334412010-12-15 15:17:42 -08001412
Andreas Huber43c3e6c2011-01-05 12:17:08 -08001413 if (driver == NULL) {
Andreas Huberf9334412010-12-15 15:17:42 -08001414 return;
1415 }
1416
Chong Zhangdcb89b32013-08-06 09:44:47 -07001417 driver->notifyListener(msg, ext1, ext2, in);
Andreas Huberf9334412010-12-15 15:17:42 -08001418}
1419
Andreas Huber1aef2112011-01-04 14:01:29 -08001420void NuPlayer::flushDecoder(bool audio, bool needShutdown) {
Andreas Huber14f76722013-01-15 09:04:18 -08001421 ALOGV("[%s] flushDecoder needShutdown=%d",
1422 audio ? "audio" : "video", needShutdown);
1423
Andreas Huber6e3d3112011-11-28 12:36:11 -08001424 if ((audio && mAudioDecoder == NULL) || (!audio && mVideoDecoder == NULL)) {
Steve Blockdf64d152012-01-04 20:05:49 +00001425 ALOGI("flushDecoder %s without decoder present",
Andreas Huber6e3d3112011-11-28 12:36:11 -08001426 audio ? "audio" : "video");
1427 }
1428
Andreas Huber1aef2112011-01-04 14:01:29 -08001429 // Make sure we don't continue to scan sources until we finish flushing.
1430 ++mScanSourcesGeneration;
Andreas Huber43c3e6c2011-01-05 12:17:08 -08001431 mScanSourcesPending = false;
Andreas Huber1aef2112011-01-04 14:01:29 -08001432
1433 (audio ? mAudioDecoder : mVideoDecoder)->signalFlush();
1434 mRenderer->flush(audio);
1435
1436 FlushStatus newStatus =
1437 needShutdown ? FLUSHING_DECODER_SHUTDOWN : FLUSHING_DECODER;
1438
1439 if (audio) {
1440 CHECK(mFlushingAudio == NONE
1441 || mFlushingAudio == AWAITING_DISCONTINUITY);
1442
1443 mFlushingAudio = newStatus;
1444
1445 if (mFlushingVideo == NONE) {
1446 mFlushingVideo = (mVideoDecoder != NULL)
1447 ? AWAITING_DISCONTINUITY
1448 : FLUSHED;
1449 }
1450 } else {
1451 CHECK(mFlushingVideo == NONE
1452 || mFlushingVideo == AWAITING_DISCONTINUITY);
1453
1454 mFlushingVideo = newStatus;
1455
1456 if (mFlushingAudio == NONE) {
1457 mFlushingAudio = (mAudioDecoder != NULL)
1458 ? AWAITING_DISCONTINUITY
1459 : FLUSHED;
1460 }
1461 }
1462}
1463
Chong Zhangced1c2f2014-08-08 15:22:35 -07001464void NuPlayer::queueDecoderShutdown(
1465 bool audio, bool video, const sp<AMessage> &reply) {
1466 ALOGI("queueDecoderShutdown audio=%d, video=%d", audio, video);
Andreas Huber84066782011-08-16 09:34:26 -07001467
Chong Zhangced1c2f2014-08-08 15:22:35 -07001468 mDeferredActions.push_back(
1469 new ShutdownDecoderAction(audio, video));
Andreas Huber84066782011-08-16 09:34:26 -07001470
Chong Zhangced1c2f2014-08-08 15:22:35 -07001471 mDeferredActions.push_back(
1472 new SimpleAction(&NuPlayer::performScanSources));
Andreas Huber84066782011-08-16 09:34:26 -07001473
Chong Zhangced1c2f2014-08-08 15:22:35 -07001474 mDeferredActions.push_back(new PostMessageAction(reply));
1475
1476 processDeferredActions();
Andreas Huber84066782011-08-16 09:34:26 -07001477}
1478
James Dong0d268a32012-08-31 12:18:27 -07001479status_t NuPlayer::setVideoScalingMode(int32_t mode) {
1480 mVideoScalingMode = mode;
Andreas Huber57a339c2012-12-03 11:18:00 -08001481 if (mNativeWindow != NULL) {
James Dong0d268a32012-08-31 12:18:27 -07001482 status_t ret = native_window_set_scaling_mode(
1483 mNativeWindow->getNativeWindow().get(), mVideoScalingMode);
1484 if (ret != OK) {
1485 ALOGE("Failed to set scaling mode (%d): %s",
1486 -ret, strerror(-ret));
1487 return ret;
1488 }
1489 }
1490 return OK;
1491}
1492
Chong Zhangdcb89b32013-08-06 09:44:47 -07001493status_t NuPlayer::getTrackInfo(Parcel* reply) const {
1494 sp<AMessage> msg = new AMessage(kWhatGetTrackInfo, id());
1495 msg->setPointer("reply", reply);
1496
1497 sp<AMessage> response;
1498 status_t err = msg->postAndAwaitResponse(&response);
1499 return err;
1500}
1501
1502status_t NuPlayer::selectTrack(size_t trackIndex, bool select) {
1503 sp<AMessage> msg = new AMessage(kWhatSelectTrack, id());
1504 msg->setSize("trackIndex", trackIndex);
1505 msg->setInt32("select", select);
1506
1507 sp<AMessage> response;
1508 status_t err = msg->postAndAwaitResponse(&response);
1509
Chong Zhang404fced2014-06-11 14:45:31 -07001510 if (err != OK) {
1511 return err;
1512 }
1513
1514 if (!response->findInt32("err", &err)) {
1515 err = OK;
1516 }
1517
Chong Zhangdcb89b32013-08-06 09:44:47 -07001518 return err;
1519}
1520
Andreas Huberb7c8e912012-11-27 15:02:53 -08001521void NuPlayer::schedulePollDuration() {
1522 sp<AMessage> msg = new AMessage(kWhatPollDuration, id());
1523 msg->setInt32("generation", mPollDurationGeneration);
1524 msg->post();
1525}
1526
1527void NuPlayer::cancelPollDuration() {
1528 ++mPollDurationGeneration;
1529}
1530
Andreas Hubera1f8ab02012-11-30 10:53:22 -08001531void NuPlayer::processDeferredActions() {
1532 while (!mDeferredActions.empty()) {
1533 // We won't execute any deferred actions until we're no longer in
1534 // an intermediate state, i.e. one more more decoders are currently
1535 // flushing or shutting down.
1536
1537 if (mRenderer != NULL) {
1538 // There's an edge case where the renderer owns all output
1539 // buffers and is paused, therefore the decoder will not read
1540 // more input data and will never encounter the matching
1541 // discontinuity. To avoid this, we resume the renderer.
1542
1543 if (mFlushingAudio == AWAITING_DISCONTINUITY
1544 || mFlushingVideo == AWAITING_DISCONTINUITY) {
1545 mRenderer->resume();
1546 }
1547 }
1548
1549 if (mFlushingAudio != NONE || mFlushingVideo != NONE) {
1550 // We're currently flushing, postpone the reset until that's
1551 // completed.
1552
1553 ALOGV("postponing action mFlushingAudio=%d, mFlushingVideo=%d",
1554 mFlushingAudio, mFlushingVideo);
1555
1556 break;
1557 }
1558
1559 sp<Action> action = *mDeferredActions.begin();
1560 mDeferredActions.erase(mDeferredActions.begin());
1561
1562 action->execute(this);
1563 }
1564}
1565
1566void NuPlayer::performSeek(int64_t seekTimeUs) {
1567 ALOGV("performSeek seekTimeUs=%lld us (%.2f secs)",
1568 seekTimeUs,
1569 seekTimeUs / 1E6);
1570
1571 mSource->seekTo(seekTimeUs);
Robert Shihd3b0bbb2014-07-23 15:00:25 -07001572 ++mTimedTextGeneration;
Andreas Hubera1f8ab02012-11-30 10:53:22 -08001573
1574 if (mDriver != NULL) {
1575 sp<NuPlayerDriver> driver = mDriver.promote();
1576 if (driver != NULL) {
1577 driver->notifyPosition(seekTimeUs);
1578 driver->notifySeekComplete();
1579 }
1580 }
1581
1582 // everything's flushed, continue playback.
1583}
1584
1585void NuPlayer::performDecoderFlush() {
1586 ALOGV("performDecoderFlush");
1587
Andreas Huberda9740e2013-04-16 10:54:03 -07001588 if (mAudioDecoder == NULL && mVideoDecoder == NULL) {
Andreas Hubera1f8ab02012-11-30 10:53:22 -08001589 return;
1590 }
1591
1592 mTimeDiscontinuityPending = true;
1593
1594 if (mAudioDecoder != NULL) {
1595 flushDecoder(true /* audio */, false /* needShutdown */);
1596 }
1597
1598 if (mVideoDecoder != NULL) {
1599 flushDecoder(false /* audio */, false /* needShutdown */);
1600 }
1601}
1602
Andreas Huber14f76722013-01-15 09:04:18 -08001603void NuPlayer::performDecoderShutdown(bool audio, bool video) {
1604 ALOGV("performDecoderShutdown audio=%d, video=%d", audio, video);
Andreas Hubera1f8ab02012-11-30 10:53:22 -08001605
Andreas Huber14f76722013-01-15 09:04:18 -08001606 if ((!audio || mAudioDecoder == NULL)
1607 && (!video || mVideoDecoder == NULL)) {
Andreas Hubera1f8ab02012-11-30 10:53:22 -08001608 return;
1609 }
1610
1611 mTimeDiscontinuityPending = true;
1612
Andreas Huber14f76722013-01-15 09:04:18 -08001613 if (mFlushingAudio == NONE && (!audio || mAudioDecoder == NULL)) {
1614 mFlushingAudio = FLUSHED;
1615 }
1616
1617 if (mFlushingVideo == NONE && (!video || mVideoDecoder == NULL)) {
1618 mFlushingVideo = FLUSHED;
1619 }
1620
1621 if (audio && mAudioDecoder != NULL) {
Andreas Hubera1f8ab02012-11-30 10:53:22 -08001622 flushDecoder(true /* audio */, true /* needShutdown */);
1623 }
1624
Andreas Huber14f76722013-01-15 09:04:18 -08001625 if (video && mVideoDecoder != NULL) {
Andreas Hubera1f8ab02012-11-30 10:53:22 -08001626 flushDecoder(false /* audio */, true /* needShutdown */);
1627 }
1628}
1629
1630void NuPlayer::performReset() {
1631 ALOGV("performReset");
1632
1633 CHECK(mAudioDecoder == NULL);
1634 CHECK(mVideoDecoder == NULL);
1635
1636 cancelPollDuration();
1637
1638 ++mScanSourcesGeneration;
1639 mScanSourcesPending = false;
1640
Lajos Molnar09524832014-07-17 14:29:51 -07001641 if (mRendererLooper != NULL) {
1642 if (mRenderer != NULL) {
1643 mRendererLooper->unregisterHandler(mRenderer->id());
1644 }
1645 mRendererLooper->stop();
1646 mRendererLooper.clear();
1647 }
Andreas Hubera1f8ab02012-11-30 10:53:22 -08001648 mRenderer.clear();
1649
1650 if (mSource != NULL) {
1651 mSource->stop();
Andreas Huberb5f25f02013-02-05 10:14:26 -08001652
1653 looper()->unregisterHandler(mSource->id());
1654
Andreas Hubera1f8ab02012-11-30 10:53:22 -08001655 mSource.clear();
1656 }
1657
1658 if (mDriver != NULL) {
1659 sp<NuPlayerDriver> driver = mDriver.promote();
1660 if (driver != NULL) {
1661 driver->notifyResetComplete();
1662 }
1663 }
Andreas Huber57a339c2012-12-03 11:18:00 -08001664
1665 mStarted = false;
Andreas Hubera1f8ab02012-11-30 10:53:22 -08001666}
1667
1668void NuPlayer::performScanSources() {
1669 ALOGV("performScanSources");
1670
Andreas Huber57a339c2012-12-03 11:18:00 -08001671 if (!mStarted) {
1672 return;
1673 }
1674
Andreas Hubera1f8ab02012-11-30 10:53:22 -08001675 if (mAudioDecoder == NULL || mVideoDecoder == NULL) {
1676 postScanSources();
1677 }
1678}
1679
Andreas Huber57a339c2012-12-03 11:18:00 -08001680void NuPlayer::performSetSurface(const sp<NativeWindowWrapper> &wrapper) {
1681 ALOGV("performSetSurface");
1682
1683 mNativeWindow = wrapper;
1684
1685 // XXX - ignore error from setVideoScalingMode for now
1686 setVideoScalingMode(mVideoScalingMode);
1687
1688 if (mDriver != NULL) {
1689 sp<NuPlayerDriver> driver = mDriver.promote();
1690 if (driver != NULL) {
1691 driver->notifySetSurfaceComplete();
1692 }
1693 }
1694}
1695
Andreas Huber9575c962013-02-05 13:59:56 -08001696void NuPlayer::onSourceNotify(const sp<AMessage> &msg) {
1697 int32_t what;
1698 CHECK(msg->findInt32("what", &what));
1699
1700 switch (what) {
1701 case Source::kWhatPrepared:
1702 {
Andreas Huberb5f28d42013-04-25 15:11:19 -07001703 if (mSource == NULL) {
1704 // This is a stale notification from a source that was
1705 // asynchronously preparing when the client called reset().
1706 // We handled the reset, the source is gone.
1707 break;
1708 }
1709
Andreas Huberec0c5972013-02-05 14:47:13 -08001710 int32_t err;
1711 CHECK(msg->findInt32("err", &err));
1712
Andreas Huber9575c962013-02-05 13:59:56 -08001713 sp<NuPlayerDriver> driver = mDriver.promote();
1714 if (driver != NULL) {
Marco Nelissendd114d12014-05-28 15:23:14 -07001715 // notify duration first, so that it's definitely set when
1716 // the app received the "prepare complete" callback.
1717 int64_t durationUs;
1718 if (mSource->getDuration(&durationUs) == OK) {
1719 driver->notifyDuration(durationUs);
1720 }
Andreas Huberec0c5972013-02-05 14:47:13 -08001721 driver->notifyPrepareCompleted(err);
Andreas Huber9575c962013-02-05 13:59:56 -08001722 }
Andreas Huber99759402013-04-01 14:28:31 -07001723
Andreas Huber9575c962013-02-05 13:59:56 -08001724 break;
1725 }
1726
1727 case Source::kWhatFlagsChanged:
1728 {
1729 uint32_t flags;
1730 CHECK(msg->findInt32("flags", (int32_t *)&flags));
1731
Chong Zhang4b7069d2013-09-11 12:52:43 -07001732 sp<NuPlayerDriver> driver = mDriver.promote();
1733 if (driver != NULL) {
1734 driver->notifyFlagsChanged(flags);
1735 }
1736
Andreas Huber9575c962013-02-05 13:59:56 -08001737 if ((mSourceFlags & Source::FLAG_DYNAMIC_DURATION)
1738 && (!(flags & Source::FLAG_DYNAMIC_DURATION))) {
1739 cancelPollDuration();
1740 } else if (!(mSourceFlags & Source::FLAG_DYNAMIC_DURATION)
1741 && (flags & Source::FLAG_DYNAMIC_DURATION)
1742 && (mAudioDecoder != NULL || mVideoDecoder != NULL)) {
1743 schedulePollDuration();
1744 }
1745
1746 mSourceFlags = flags;
1747 break;
1748 }
1749
1750 case Source::kWhatVideoSizeChanged:
1751 {
Chong Zhangced1c2f2014-08-08 15:22:35 -07001752 sp<AMessage> format;
1753 CHECK(msg->findMessage("format", &format));
Andreas Huber9575c962013-02-05 13:59:56 -08001754
Chong Zhangced1c2f2014-08-08 15:22:35 -07001755 updateVideoSize(format);
Andreas Huber9575c962013-02-05 13:59:56 -08001756 break;
1757 }
1758
Roger Jönssonb50e83e2013-01-21 16:26:41 +01001759 case Source::kWhatBufferingStart:
1760 {
1761 notifyListener(MEDIA_INFO, MEDIA_INFO_BUFFERING_START, 0);
1762 break;
1763 }
1764
1765 case Source::kWhatBufferingEnd:
1766 {
1767 notifyListener(MEDIA_INFO, MEDIA_INFO_BUFFERING_END, 0);
1768 break;
1769 }
1770
Chong Zhangdcb89b32013-08-06 09:44:47 -07001771 case Source::kWhatSubtitleData:
1772 {
1773 sp<ABuffer> buffer;
1774 CHECK(msg->findBuffer("buffer", &buffer));
1775
Chong Zhang404fced2014-06-11 14:45:31 -07001776 sendSubtitleData(buffer, 0 /* baseIndex */);
Chong Zhangdcb89b32013-08-06 09:44:47 -07001777 break;
1778 }
1779
Robert Shihd3b0bbb2014-07-23 15:00:25 -07001780 case Source::kWhatTimedTextData:
1781 {
1782 int32_t generation;
1783 if (msg->findInt32("generation", &generation)
1784 && generation != mTimedTextGeneration) {
1785 break;
1786 }
1787
1788 sp<ABuffer> buffer;
1789 CHECK(msg->findBuffer("buffer", &buffer));
1790
1791 sp<NuPlayerDriver> driver = mDriver.promote();
1792 if (driver == NULL) {
1793 break;
1794 }
1795
1796 int posMs;
1797 int64_t timeUs, posUs;
1798 driver->getCurrentPosition(&posMs);
1799 posUs = posMs * 1000;
1800 CHECK(buffer->meta()->findInt64("timeUs", &timeUs));
1801
1802 if (posUs < timeUs) {
1803 if (!msg->findInt32("generation", &generation)) {
1804 msg->setInt32("generation", mTimedTextGeneration);
1805 }
1806 msg->post(timeUs - posUs);
1807 } else {
1808 sendTimedTextData(buffer);
1809 }
1810 break;
1811 }
1812
Andreas Huber14f76722013-01-15 09:04:18 -08001813 case Source::kWhatQueueDecoderShutdown:
1814 {
1815 int32_t audio, video;
1816 CHECK(msg->findInt32("audio", &audio));
1817 CHECK(msg->findInt32("video", &video));
1818
1819 sp<AMessage> reply;
1820 CHECK(msg->findMessage("reply", &reply));
1821
1822 queueDecoderShutdown(audio, video, reply);
1823 break;
1824 }
1825
Andreas Huber9575c962013-02-05 13:59:56 -08001826 default:
1827 TRESPASS();
1828 }
1829}
1830
Chong Zhanga7fa1d92014-06-11 14:49:23 -07001831void NuPlayer::onClosedCaptionNotify(const sp<AMessage> &msg) {
1832 int32_t what;
1833 CHECK(msg->findInt32("what", &what));
1834
1835 switch (what) {
1836 case NuPlayer::CCDecoder::kWhatClosedCaptionData:
1837 {
1838 sp<ABuffer> buffer;
1839 CHECK(msg->findBuffer("buffer", &buffer));
1840
1841 size_t inbandTracks = 0;
1842 if (mSource != NULL) {
1843 inbandTracks = mSource->getTrackCount();
1844 }
1845
1846 sendSubtitleData(buffer, inbandTracks);
1847 break;
1848 }
1849
1850 case NuPlayer::CCDecoder::kWhatTrackAdded:
1851 {
1852 notifyListener(MEDIA_INFO, MEDIA_INFO_METADATA_UPDATE, 0);
1853
1854 break;
1855 }
1856
1857 default:
1858 TRESPASS();
1859 }
1860
1861
1862}
1863
Chong Zhang404fced2014-06-11 14:45:31 -07001864void NuPlayer::sendSubtitleData(const sp<ABuffer> &buffer, int32_t baseIndex) {
1865 int32_t trackIndex;
1866 int64_t timeUs, durationUs;
1867 CHECK(buffer->meta()->findInt32("trackIndex", &trackIndex));
1868 CHECK(buffer->meta()->findInt64("timeUs", &timeUs));
1869 CHECK(buffer->meta()->findInt64("durationUs", &durationUs));
1870
1871 Parcel in;
1872 in.writeInt32(trackIndex + baseIndex);
1873 in.writeInt64(timeUs);
1874 in.writeInt64(durationUs);
1875 in.writeInt32(buffer->size());
1876 in.writeInt32(buffer->size());
1877 in.write(buffer->data(), buffer->size());
1878
1879 notifyListener(MEDIA_SUBTITLE_DATA, 0, 0, &in);
1880}
Robert Shihd3b0bbb2014-07-23 15:00:25 -07001881
1882void NuPlayer::sendTimedTextData(const sp<ABuffer> &buffer) {
1883 const void *data;
1884 size_t size = 0;
1885 int64_t timeUs;
1886 int32_t flag = TextDescriptions::LOCAL_DESCRIPTIONS;
1887
1888 AString mime;
1889 CHECK(buffer->meta()->findString("mime", &mime));
1890 CHECK(strcasecmp(mime.c_str(), MEDIA_MIMETYPE_TEXT_3GPP) == 0);
1891
1892 data = buffer->data();
1893 size = buffer->size();
1894
1895 Parcel parcel;
1896 if (size > 0) {
1897 CHECK(buffer->meta()->findInt64("timeUs", &timeUs));
1898 flag |= TextDescriptions::IN_BAND_TEXT_3GPP;
1899 TextDescriptions::getParcelOfDescriptions(
1900 (const uint8_t *)data, size, flag, timeUs / 1000, &parcel);
1901 }
1902
1903 if ((parcel.dataSize() > 0)) {
1904 notifyListener(MEDIA_TIMED_TEXT, 0, 0, &parcel);
1905 } else { // send an empty timed text
1906 notifyListener(MEDIA_TIMED_TEXT, 0, 0);
1907 }
1908}
Andreas Huberb5f25f02013-02-05 10:14:26 -08001909////////////////////////////////////////////////////////////////////////////////
1910
Chong Zhangced1c2f2014-08-08 15:22:35 -07001911sp<AMessage> NuPlayer::Source::getFormat(bool audio) {
1912 sp<MetaData> meta = getFormatMeta(audio);
1913
1914 if (meta == NULL) {
1915 return NULL;
1916 }
1917
1918 sp<AMessage> msg = new AMessage;
1919
1920 if(convertMetaDataToMessage(meta, &msg) == OK) {
1921 return msg;
1922 }
1923 return NULL;
1924}
1925
Andreas Huber9575c962013-02-05 13:59:56 -08001926void NuPlayer::Source::notifyFlagsChanged(uint32_t flags) {
1927 sp<AMessage> notify = dupNotify();
1928 notify->setInt32("what", kWhatFlagsChanged);
1929 notify->setInt32("flags", flags);
1930 notify->post();
1931}
1932
Chong Zhangced1c2f2014-08-08 15:22:35 -07001933void NuPlayer::Source::notifyVideoSizeChanged(const sp<AMessage> &format) {
Andreas Huber9575c962013-02-05 13:59:56 -08001934 sp<AMessage> notify = dupNotify();
1935 notify->setInt32("what", kWhatVideoSizeChanged);
Chong Zhangced1c2f2014-08-08 15:22:35 -07001936 notify->setMessage("format", format);
Andreas Huber9575c962013-02-05 13:59:56 -08001937 notify->post();
1938}
1939
Andreas Huberec0c5972013-02-05 14:47:13 -08001940void NuPlayer::Source::notifyPrepared(status_t err) {
Andreas Huber9575c962013-02-05 13:59:56 -08001941 sp<AMessage> notify = dupNotify();
1942 notify->setInt32("what", kWhatPrepared);
Andreas Huberec0c5972013-02-05 14:47:13 -08001943 notify->setInt32("err", err);
Andreas Huber9575c962013-02-05 13:59:56 -08001944 notify->post();
1945}
1946
Andreas Huber84333e02014-02-07 15:36:10 -08001947void NuPlayer::Source::onMessageReceived(const sp<AMessage> & /* msg */) {
Andreas Huberb5f25f02013-02-05 10:14:26 -08001948 TRESPASS();
1949}
1950
Andreas Huberf9334412010-12-15 15:17:42 -08001951} // namespace android