blob: 6f6c9d97428e5b4705eaf6a2eacf14eda3db479d [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
879 mRenderer->queueEOS(audio, UNKNOWN_ERROR);
Lajos Molnar1cd13982014-01-17 15:12:51 -0800880 } else if (what == Decoder::kWhatDrainThisBuffer) {
881 renderBuffer(audio, msg);
882 } else {
883 ALOGV("Unhandled decoder notification %d '%c%c%c%c'.",
Andreas Hubera1f8ab02012-11-30 10:53:22 -0800884 what,
885 what >> 24,
886 (what >> 16) & 0xff,
887 (what >> 8) & 0xff,
888 what & 0xff);
Andreas Huberf9334412010-12-15 15:17:42 -0800889 }
890
891 break;
892 }
893
894 case kWhatRendererNotify:
895 {
896 int32_t what;
897 CHECK(msg->findInt32("what", &what));
898
899 if (what == Renderer::kWhatEOS) {
900 int32_t audio;
901 CHECK(msg->findInt32("audio", &audio));
902
Andreas Huberc92fd242011-08-16 13:48:44 -0700903 int32_t finalResult;
904 CHECK(msg->findInt32("finalResult", &finalResult));
905
Andreas Huberf9334412010-12-15 15:17:42 -0800906 if (audio) {
907 mAudioEOS = true;
908 } else {
909 mVideoEOS = true;
910 }
911
Andreas Huberc92fd242011-08-16 13:48:44 -0700912 if (finalResult == ERROR_END_OF_STREAM) {
Steve Block3856b092011-10-20 11:56:00 +0100913 ALOGV("reached %s EOS", audio ? "audio" : "video");
Andreas Huberc92fd242011-08-16 13:48:44 -0700914 } else {
Steve Block29357bc2012-01-06 19:20:56 +0000915 ALOGE("%s track encountered an error (%d)",
Andreas Huberc92fd242011-08-16 13:48:44 -0700916 audio ? "audio" : "video", finalResult);
917
918 notifyListener(
919 MEDIA_ERROR, MEDIA_ERROR_UNKNOWN, finalResult);
920 }
Andreas Huberf9334412010-12-15 15:17:42 -0800921
922 if ((mAudioEOS || mAudioDecoder == NULL)
923 && (mVideoEOS || mVideoDecoder == NULL)) {
924 notifyListener(MEDIA_PLAYBACK_COMPLETE, 0, 0);
925 }
Andreas Huber43c3e6c2011-01-05 12:17:08 -0800926 } else if (what == Renderer::kWhatPosition) {
927 int64_t positionUs;
928 CHECK(msg->findInt64("positionUs", &positionUs));
929
Andreas Huber3fe62152011-09-16 15:09:22 -0700930 CHECK(msg->findInt64("videoLateByUs", &mVideoLateByUs));
931
Andreas Huber43c3e6c2011-01-05 12:17:08 -0800932 if (mDriver != NULL) {
933 sp<NuPlayerDriver> driver = mDriver.promote();
934 if (driver != NULL) {
935 driver->notifyPosition(positionUs);
Andreas Huber3fe62152011-09-16 15:09:22 -0700936
937 driver->notifyFrameStats(
938 mNumFramesTotal, mNumFramesDropped);
Andreas Huber43c3e6c2011-01-05 12:17:08 -0800939 }
940 }
Andreas Huber3fe62152011-09-16 15:09:22 -0700941 } else if (what == Renderer::kWhatFlushComplete) {
Andreas Huberf9334412010-12-15 15:17:42 -0800942 int32_t audio;
943 CHECK(msg->findInt32("audio", &audio));
944
Steve Block3856b092011-10-20 11:56:00 +0100945 ALOGV("renderer %s flush completed.", audio ? "audio" : "video");
James Dongf57b4ea2012-07-20 13:38:36 -0700946 } else if (what == Renderer::kWhatVideoRenderingStart) {
947 notifyListener(MEDIA_INFO, MEDIA_INFO_RENDERING_START, 0);
Lajos Molnarcbaffcf2013-08-14 18:30:38 -0700948 } else if (what == Renderer::kWhatMediaRenderingStart) {
949 ALOGV("media rendering started");
950 notifyListener(MEDIA_STARTED, 0, 0);
Wei Jia3a2956d2014-07-22 16:01:33 -0700951 } else if (what == Renderer::kWhatAudioOffloadTearDown) {
952 ALOGV("Tear down audio offload, fall back to s/w path");
953 int64_t positionUs;
954 CHECK(msg->findInt64("positionUs", &positionUs));
955 mAudioSink->close();
956 mAudioDecoder.clear();
957 mRenderer->flush(true /* audio */);
958 if (mVideoDecoder != NULL) {
959 mRenderer->flush(false /* audio */);
960 }
961 mRenderer->signalDisableOffloadAudio();
962 mOffloadAudio = false;
963
964 performSeek(positionUs);
965 instantiateDecoder(true /* audio */, &mAudioDecoder);
Andreas Huberf9334412010-12-15 15:17:42 -0800966 }
967 break;
968 }
969
970 case kWhatMoreDataQueued:
971 {
972 break;
973 }
974
Andreas Huber1aef2112011-01-04 14:01:29 -0800975 case kWhatReset:
976 {
Steve Block3856b092011-10-20 11:56:00 +0100977 ALOGV("kWhatReset");
Andreas Huber1aef2112011-01-04 14:01:29 -0800978
Andreas Hubera1f8ab02012-11-30 10:53:22 -0800979 mDeferredActions.push_back(
Andreas Huber14f76722013-01-15 09:04:18 -0800980 new ShutdownDecoderAction(
981 true /* audio */, true /* video */));
Andreas Huberb7c8e912012-11-27 15:02:53 -0800982
Andreas Hubera1f8ab02012-11-30 10:53:22 -0800983 mDeferredActions.push_back(
984 new SimpleAction(&NuPlayer::performReset));
Andreas Huberb58ce9f2011-11-28 16:27:35 -0800985
Andreas Hubera1f8ab02012-11-30 10:53:22 -0800986 processDeferredActions();
Andreas Huber1aef2112011-01-04 14:01:29 -0800987 break;
988 }
989
Andreas Huber43c3e6c2011-01-05 12:17:08 -0800990 case kWhatSeek:
991 {
992 int64_t seekTimeUs;
993 CHECK(msg->findInt64("seekTimeUs", &seekTimeUs));
994
Andreas Hubera1f8ab02012-11-30 10:53:22 -0800995 ALOGV("kWhatSeek seekTimeUs=%lld us", seekTimeUs);
Andreas Huber43c3e6c2011-01-05 12:17:08 -0800996
Andreas Hubera1f8ab02012-11-30 10:53:22 -0800997 mDeferredActions.push_back(
998 new SimpleAction(&NuPlayer::performDecoderFlush));
Andreas Huber43c3e6c2011-01-05 12:17:08 -0800999
Andreas Hubera1f8ab02012-11-30 10:53:22 -08001000 mDeferredActions.push_back(new SeekAction(seekTimeUs));
Andreas Huber43c3e6c2011-01-05 12:17:08 -08001001
Andreas Hubera1f8ab02012-11-30 10:53:22 -08001002 processDeferredActions();
Andreas Huber43c3e6c2011-01-05 12:17:08 -08001003 break;
1004 }
1005
Andreas Huberb4082222011-01-20 15:23:04 -08001006 case kWhatPause:
1007 {
1008 CHECK(mRenderer != NULL);
Roger Jönssonfba60da2013-01-21 17:15:45 +01001009 mSource->pause();
Andreas Huberb4082222011-01-20 15:23:04 -08001010 mRenderer->pause();
1011 break;
1012 }
1013
1014 case kWhatResume:
1015 {
1016 CHECK(mRenderer != NULL);
Roger Jönssonfba60da2013-01-21 17:15:45 +01001017 mSource->resume();
Andreas Huberb4082222011-01-20 15:23:04 -08001018 mRenderer->resume();
1019 break;
1020 }
1021
Andreas Huberb5f25f02013-02-05 10:14:26 -08001022 case kWhatSourceNotify:
1023 {
Andreas Huber9575c962013-02-05 13:59:56 -08001024 onSourceNotify(msg);
Andreas Huberb5f25f02013-02-05 10:14:26 -08001025 break;
1026 }
1027
Chong Zhanga7fa1d92014-06-11 14:49:23 -07001028 case kWhatClosedCaptionNotify:
1029 {
1030 onClosedCaptionNotify(msg);
1031 break;
1032 }
1033
Andreas Huberf9334412010-12-15 15:17:42 -08001034 default:
1035 TRESPASS();
1036 break;
1037 }
1038}
1039
Andreas Huber3831a062010-12-21 10:22:33 -08001040void NuPlayer::finishFlushIfPossible() {
1041 if (mFlushingAudio != FLUSHED && mFlushingAudio != SHUT_DOWN) {
1042 return;
1043 }
1044
1045 if (mFlushingVideo != FLUSHED && mFlushingVideo != SHUT_DOWN) {
1046 return;
1047 }
1048
Steve Block3856b092011-10-20 11:56:00 +01001049 ALOGV("both audio and video are flushed now.");
Andreas Huber3831a062010-12-21 10:22:33 -08001050
Andreas Huber6e3d3112011-11-28 12:36:11 -08001051 if (mTimeDiscontinuityPending) {
1052 mRenderer->signalTimeDiscontinuity();
1053 mTimeDiscontinuityPending = false;
1054 }
Andreas Huber3831a062010-12-21 10:22:33 -08001055
Andreas Huber22fc52f2011-01-05 16:24:27 -08001056 if (mAudioDecoder != NULL) {
Andreas Huber3831a062010-12-21 10:22:33 -08001057 mAudioDecoder->signalResume();
1058 }
1059
Andreas Huber22fc52f2011-01-05 16:24:27 -08001060 if (mVideoDecoder != NULL) {
Andreas Huber3831a062010-12-21 10:22:33 -08001061 mVideoDecoder->signalResume();
1062 }
1063
1064 mFlushingAudio = NONE;
1065 mFlushingVideo = NONE;
Andreas Huber3831a062010-12-21 10:22:33 -08001066
Andreas Hubera1f8ab02012-11-30 10:53:22 -08001067 processDeferredActions();
Andreas Huber1aef2112011-01-04 14:01:29 -08001068}
1069
1070void NuPlayer::postScanSources() {
1071 if (mScanSourcesPending) {
1072 return;
1073 }
1074
1075 sp<AMessage> msg = new AMessage(kWhatScanSources, id());
1076 msg->setInt32("generation", mScanSourcesGeneration);
1077 msg->post();
1078
1079 mScanSourcesPending = true;
1080}
1081
Andreas Huber5bc087c2010-12-23 10:27:40 -08001082status_t NuPlayer::instantiateDecoder(bool audio, sp<Decoder> *decoder) {
Andreas Huberf9334412010-12-15 15:17:42 -08001083 if (*decoder != NULL) {
1084 return OK;
1085 }
1086
Andreas Huber84066782011-08-16 09:34:26 -07001087 sp<AMessage> format = mSource->getFormat(audio);
Andreas Huberf9334412010-12-15 15:17:42 -08001088
Andreas Huber84066782011-08-16 09:34:26 -07001089 if (format == NULL) {
Andreas Huberf9334412010-12-15 15:17:42 -08001090 return -EWOULDBLOCK;
1091 }
1092
Andreas Huber3fe62152011-09-16 15:09:22 -07001093 if (!audio) {
Andreas Huber84066782011-08-16 09:34:26 -07001094 AString mime;
1095 CHECK(format->findString("mime", &mime));
1096 mVideoIsAVC = !strcasecmp(MEDIA_MIMETYPE_VIDEO_AVC, mime.c_str());
Chong Zhanga7fa1d92014-06-11 14:49:23 -07001097
1098 sp<AMessage> ccNotify = new AMessage(kWhatClosedCaptionNotify, id());
1099 mCCDecoder = new CCDecoder(ccNotify);
Lajos Molnar09524832014-07-17 14:29:51 -07001100
1101 if (mSourceFlags & Source::FLAG_SECURE) {
1102 format->setInt32("secure", true);
1103 }
Andreas Huber3fe62152011-09-16 15:09:22 -07001104 }
1105
Andreas Huberf9334412010-12-15 15:17:42 -08001106 sp<AMessage> notify =
1107 new AMessage(audio ? kWhatAudioNotify : kWhatVideoNotify,
1108 id());
1109
Wei Jiabc2fb722014-07-08 16:37:57 -07001110 if (audio) {
1111 if (mOffloadAudio) {
1112 *decoder = new DecoderPassThrough(notify);
1113 } else {
1114 *decoder = new Decoder(notify);
1115 }
1116 } else {
1117 *decoder = new Decoder(notify, mNativeWindow);
1118 }
Lajos Molnar1cd13982014-01-17 15:12:51 -08001119 (*decoder)->init();
Andreas Huber84066782011-08-16 09:34:26 -07001120 (*decoder)->configure(format);
Andreas Huberf9334412010-12-15 15:17:42 -08001121
Lajos Molnar09524832014-07-17 14:29:51 -07001122 // allocate buffers to decrypt widevine source buffers
1123 if (!audio && (mSourceFlags & Source::FLAG_SECURE)) {
1124 Vector<sp<ABuffer> > inputBufs;
1125 CHECK_EQ((*decoder)->getInputBuffers(&inputBufs), (status_t)OK);
1126
1127 Vector<MediaBuffer *> mediaBufs;
1128 for (size_t i = 0; i < inputBufs.size(); i++) {
1129 const sp<ABuffer> &buffer = inputBufs[i];
1130 MediaBuffer *mbuf = new MediaBuffer(buffer->data(), buffer->size());
1131 mediaBufs.push(mbuf);
1132 }
1133
1134 status_t err = mSource->setBuffers(audio, mediaBufs);
1135 if (err != OK) {
1136 for (size_t i = 0; i < mediaBufs.size(); ++i) {
1137 mediaBufs[i]->release();
1138 }
1139 mediaBufs.clear();
1140 ALOGE("Secure source didn't support secure mediaBufs.");
1141 return err;
1142 }
1143 }
Andreas Huberf9334412010-12-15 15:17:42 -08001144 return OK;
1145}
1146
1147status_t NuPlayer::feedDecoderInputData(bool audio, const sp<AMessage> &msg) {
1148 sp<AMessage> reply;
1149 CHECK(msg->findMessage("reply", &reply));
1150
Wei Jiab189a5b2014-08-07 06:11:39 +00001151 if ((audio && IsFlushingState(mFlushingAudio))
1152 || (!audio && IsFlushingState(mFlushingVideo))) {
1153 reply->setInt32("err", INFO_DISCONTINUITY);
1154 reply->post();
1155 return OK;
Andreas Huberf9334412010-12-15 15:17:42 -08001156 }
1157
1158 sp<ABuffer> accessUnit;
Andreas Huberf9334412010-12-15 15:17:42 -08001159
Andreas Huber3fe62152011-09-16 15:09:22 -07001160 bool dropAccessUnit;
1161 do {
1162 status_t err = mSource->dequeueAccessUnit(audio, &accessUnit);
Andreas Huber5bc087c2010-12-23 10:27:40 -08001163
Andreas Huber3fe62152011-09-16 15:09:22 -07001164 if (err == -EWOULDBLOCK) {
1165 return err;
1166 } else if (err != OK) {
1167 if (err == INFO_DISCONTINUITY) {
1168 int32_t type;
1169 CHECK(accessUnit->meta()->findInt32("discontinuity", &type));
Andreas Huber53df1a42010-12-22 10:03:04 -08001170
Andreas Huber3fe62152011-09-16 15:09:22 -07001171 bool formatChange =
Andreas Huber6e3d3112011-11-28 12:36:11 -08001172 (audio &&
1173 (type & ATSParser::DISCONTINUITY_AUDIO_FORMAT))
1174 || (!audio &&
1175 (type & ATSParser::DISCONTINUITY_VIDEO_FORMAT));
Andreas Huber53df1a42010-12-22 10:03:04 -08001176
Andreas Huber6e3d3112011-11-28 12:36:11 -08001177 bool timeChange = (type & ATSParser::DISCONTINUITY_TIME) != 0;
1178
Steve Blockdf64d152012-01-04 20:05:49 +00001179 ALOGI("%s discontinuity (formatChange=%d, time=%d)",
Andreas Huber6e3d3112011-11-28 12:36:11 -08001180 audio ? "audio" : "video", formatChange, timeChange);
Andreas Huber32f3cef2011-03-02 15:34:46 -08001181
Andreas Huber3fe62152011-09-16 15:09:22 -07001182 if (audio) {
1183 mSkipRenderingAudioUntilMediaTimeUs = -1;
1184 } else {
1185 mSkipRenderingVideoUntilMediaTimeUs = -1;
1186 }
Andreas Huber32f3cef2011-03-02 15:34:46 -08001187
Andreas Huber6e3d3112011-11-28 12:36:11 -08001188 if (timeChange) {
1189 sp<AMessage> extra;
1190 if (accessUnit->meta()->findMessage("extra", &extra)
1191 && extra != NULL) {
1192 int64_t resumeAtMediaTimeUs;
1193 if (extra->findInt64(
1194 "resume-at-mediatimeUs", &resumeAtMediaTimeUs)) {
Steve Blockdf64d152012-01-04 20:05:49 +00001195 ALOGI("suppressing rendering of %s until %lld us",
Andreas Huber6e3d3112011-11-28 12:36:11 -08001196 audio ? "audio" : "video", resumeAtMediaTimeUs);
Andreas Huber3fe62152011-09-16 15:09:22 -07001197
Andreas Huber6e3d3112011-11-28 12:36:11 -08001198 if (audio) {
1199 mSkipRenderingAudioUntilMediaTimeUs =
1200 resumeAtMediaTimeUs;
1201 } else {
1202 mSkipRenderingVideoUntilMediaTimeUs =
1203 resumeAtMediaTimeUs;
1204 }
Andreas Huber3fe62152011-09-16 15:09:22 -07001205 }
Andreas Huber32f3cef2011-03-02 15:34:46 -08001206 }
1207 }
Andreas Huber3fe62152011-09-16 15:09:22 -07001208
Andreas Huber6e3d3112011-11-28 12:36:11 -08001209 mTimeDiscontinuityPending =
1210 mTimeDiscontinuityPending || timeChange;
1211
Robert Shiha2981012014-07-30 17:41:24 -07001212 if (mFlushingAudio == NONE && mFlushingVideo == NONE) {
1213 // And we'll resume scanning sources once we're done
1214 // flushing.
1215 mDeferredActions.push_front(
1216 new SimpleAction(
1217 &NuPlayer::performScanSources));
1218 }
1219
Andreas Huber6e3d3112011-11-28 12:36:11 -08001220 if (formatChange || timeChange) {
Andreas Hubera1f8ab02012-11-30 10:53:22 -08001221
Robert Shih6d0a94e2014-01-23 16:18:22 -08001222 sp<AMessage> newFormat = mSource->getFormat(audio);
1223 sp<Decoder> &decoder = audio ? mAudioDecoder : mVideoDecoder;
1224 if (formatChange && !decoder->supportsSeamlessFormatChange(newFormat)) {
1225 flushDecoder(audio, /* needShutdown = */ true);
1226 } else {
1227 flushDecoder(audio, /* needShutdown = */ false);
1228 err = OK;
1229 }
Andreas Huber6e3d3112011-11-28 12:36:11 -08001230 } else {
1231 // This stream is unaffected by the discontinuity
1232
1233 if (audio) {
1234 mFlushingAudio = FLUSHED;
1235 } else {
1236 mFlushingVideo = FLUSHED;
1237 }
1238
1239 finishFlushIfPossible();
1240
1241 return -EWOULDBLOCK;
1242 }
Andreas Huber32f3cef2011-03-02 15:34:46 -08001243 }
1244
Andreas Huber3fe62152011-09-16 15:09:22 -07001245 reply->setInt32("err", err);
1246 reply->post();
1247 return OK;
Andreas Huberf9334412010-12-15 15:17:42 -08001248 }
1249
Andreas Huber3fe62152011-09-16 15:09:22 -07001250 if (!audio) {
1251 ++mNumFramesTotal;
1252 }
1253
1254 dropAccessUnit = false;
1255 if (!audio
Lajos Molnar09524832014-07-17 14:29:51 -07001256 && !(mSourceFlags & Source::FLAG_SECURE)
Andreas Huber3fe62152011-09-16 15:09:22 -07001257 && mVideoLateByUs > 100000ll
1258 && mVideoIsAVC
1259 && !IsAVCReferenceFrame(accessUnit)) {
1260 dropAccessUnit = true;
1261 ++mNumFramesDropped;
1262 }
1263 } while (dropAccessUnit);
Andreas Huberf9334412010-12-15 15:17:42 -08001264
Steve Block3856b092011-10-20 11:56:00 +01001265 // ALOGV("returned a valid buffer of %s data", audio ? "audio" : "video");
Andreas Huberf9334412010-12-15 15:17:42 -08001266
1267#if 0
1268 int64_t mediaTimeUs;
1269 CHECK(accessUnit->meta()->findInt64("timeUs", &mediaTimeUs));
Steve Block3856b092011-10-20 11:56:00 +01001270 ALOGV("feeding %s input buffer at media time %.2f secs",
Andreas Huberf9334412010-12-15 15:17:42 -08001271 audio ? "audio" : "video",
1272 mediaTimeUs / 1E6);
1273#endif
1274
Chong Zhanga7fa1d92014-06-11 14:49:23 -07001275 if (!audio) {
1276 mCCDecoder->decode(accessUnit);
1277 }
1278
Andreas Huber2d8bedd2012-02-21 14:38:23 -08001279 reply->setBuffer("buffer", accessUnit);
Andreas Huberf9334412010-12-15 15:17:42 -08001280 reply->post();
1281
1282 return OK;
1283}
1284
1285void NuPlayer::renderBuffer(bool audio, const sp<AMessage> &msg) {
Steve Block3856b092011-10-20 11:56:00 +01001286 // ALOGV("renderBuffer %s", audio ? "audio" : "video");
Andreas Huberf9334412010-12-15 15:17:42 -08001287
1288 sp<AMessage> reply;
1289 CHECK(msg->findMessage("reply", &reply));
1290
Andreas Huber18ac5402011-08-31 15:04:25 -07001291 if (IsFlushingState(audio ? mFlushingAudio : mFlushingVideo)) {
1292 // We're currently attempting to flush the decoder, in order
1293 // to complete this, the decoder wants all its buffers back,
1294 // so we don't want any output buffers it sent us (from before
1295 // we initiated the flush) to be stuck in the renderer's queue.
1296
Steve Block3856b092011-10-20 11:56:00 +01001297 ALOGV("we're still flushing the %s decoder, sending its output buffer"
Andreas Huber18ac5402011-08-31 15:04:25 -07001298 " right back.", audio ? "audio" : "video");
1299
1300 reply->post();
1301 return;
1302 }
1303
Andreas Huber2d8bedd2012-02-21 14:38:23 -08001304 sp<ABuffer> buffer;
1305 CHECK(msg->findBuffer("buffer", &buffer));
Andreas Huberf9334412010-12-15 15:17:42 -08001306
Chong Zhanga7fa1d92014-06-11 14:49:23 -07001307 int64_t mediaTimeUs;
1308 CHECK(buffer->meta()->findInt64("timeUs", &mediaTimeUs));
1309
Andreas Huber32f3cef2011-03-02 15:34:46 -08001310 int64_t &skipUntilMediaTimeUs =
1311 audio
1312 ? mSkipRenderingAudioUntilMediaTimeUs
1313 : mSkipRenderingVideoUntilMediaTimeUs;
1314
1315 if (skipUntilMediaTimeUs >= 0) {
Andreas Huber32f3cef2011-03-02 15:34:46 -08001316
1317 if (mediaTimeUs < skipUntilMediaTimeUs) {
Steve Block3856b092011-10-20 11:56:00 +01001318 ALOGV("dropping %s buffer at time %lld as requested.",
Andreas Huber32f3cef2011-03-02 15:34:46 -08001319 audio ? "audio" : "video",
1320 mediaTimeUs);
1321
1322 reply->post();
1323 return;
1324 }
1325
1326 skipUntilMediaTimeUs = -1;
1327 }
1328
Chong Zhanga7fa1d92014-06-11 14:49:23 -07001329 if (!audio && mCCDecoder->isSelected()) {
1330 mCCDecoder->display(mediaTimeUs);
1331 }
1332
Andreas Huberf9334412010-12-15 15:17:42 -08001333 mRenderer->queueBuffer(audio, buffer, reply);
1334}
1335
Chong Zhangced1c2f2014-08-08 15:22:35 -07001336void NuPlayer::updateVideoSize(
1337 const sp<AMessage> &inputFormat,
1338 const sp<AMessage> &outputFormat) {
1339 if (inputFormat == NULL) {
1340 ALOGW("Unknown video size, reporting 0x0!");
1341 notifyListener(MEDIA_SET_VIDEO_SIZE, 0, 0);
1342 return;
1343 }
1344
1345 int32_t displayWidth, displayHeight;
1346 int32_t cropLeft, cropTop, cropRight, cropBottom;
1347
1348 if (outputFormat != NULL) {
1349 int32_t width, height;
1350 CHECK(outputFormat->findInt32("width", &width));
1351 CHECK(outputFormat->findInt32("height", &height));
1352
1353 int32_t cropLeft, cropTop, cropRight, cropBottom;
1354 CHECK(outputFormat->findRect(
1355 "crop",
1356 &cropLeft, &cropTop, &cropRight, &cropBottom));
1357
1358 displayWidth = cropRight - cropLeft + 1;
1359 displayHeight = cropBottom - cropTop + 1;
1360
1361 ALOGV("Video output format changed to %d x %d "
1362 "(crop: %d x %d @ (%d, %d))",
1363 width, height,
1364 displayWidth,
1365 displayHeight,
1366 cropLeft, cropTop);
1367 } else {
1368 CHECK(inputFormat->findInt32("width", &displayWidth));
1369 CHECK(inputFormat->findInt32("height", &displayHeight));
1370
1371 ALOGV("Video input format %d x %d", displayWidth, displayHeight);
1372 }
1373
1374 // Take into account sample aspect ratio if necessary:
1375 int32_t sarWidth, sarHeight;
1376 if (inputFormat->findInt32("sar-width", &sarWidth)
1377 && inputFormat->findInt32("sar-height", &sarHeight)) {
1378 ALOGV("Sample aspect ratio %d : %d", sarWidth, sarHeight);
1379
1380 displayWidth = (displayWidth * sarWidth) / sarHeight;
1381
1382 ALOGV("display dimensions %d x %d", displayWidth, displayHeight);
1383 }
1384
1385 int32_t rotationDegrees;
1386 if (!inputFormat->findInt32("rotation-degrees", &rotationDegrees)) {
1387 rotationDegrees = 0;
1388 }
1389
1390 if (rotationDegrees == 90 || rotationDegrees == 270) {
1391 int32_t tmp = displayWidth;
1392 displayWidth = displayHeight;
1393 displayHeight = tmp;
1394 }
1395
1396 notifyListener(
1397 MEDIA_SET_VIDEO_SIZE,
1398 displayWidth,
1399 displayHeight);
1400}
1401
Chong Zhangdcb89b32013-08-06 09:44:47 -07001402void NuPlayer::notifyListener(int msg, int ext1, int ext2, const Parcel *in) {
Andreas Huber43c3e6c2011-01-05 12:17:08 -08001403 if (mDriver == NULL) {
Andreas Huberf9334412010-12-15 15:17:42 -08001404 return;
1405 }
1406
Andreas Huber43c3e6c2011-01-05 12:17:08 -08001407 sp<NuPlayerDriver> driver = mDriver.promote();
Andreas Huberf9334412010-12-15 15:17:42 -08001408
Andreas Huber43c3e6c2011-01-05 12:17:08 -08001409 if (driver == NULL) {
Andreas Huberf9334412010-12-15 15:17:42 -08001410 return;
1411 }
1412
Chong Zhangdcb89b32013-08-06 09:44:47 -07001413 driver->notifyListener(msg, ext1, ext2, in);
Andreas Huberf9334412010-12-15 15:17:42 -08001414}
1415
Andreas Huber1aef2112011-01-04 14:01:29 -08001416void NuPlayer::flushDecoder(bool audio, bool needShutdown) {
Andreas Huber14f76722013-01-15 09:04:18 -08001417 ALOGV("[%s] flushDecoder needShutdown=%d",
1418 audio ? "audio" : "video", needShutdown);
1419
Andreas Huber6e3d3112011-11-28 12:36:11 -08001420 if ((audio && mAudioDecoder == NULL) || (!audio && mVideoDecoder == NULL)) {
Steve Blockdf64d152012-01-04 20:05:49 +00001421 ALOGI("flushDecoder %s without decoder present",
Andreas Huber6e3d3112011-11-28 12:36:11 -08001422 audio ? "audio" : "video");
1423 }
1424
Andreas Huber1aef2112011-01-04 14:01:29 -08001425 // Make sure we don't continue to scan sources until we finish flushing.
1426 ++mScanSourcesGeneration;
Andreas Huber43c3e6c2011-01-05 12:17:08 -08001427 mScanSourcesPending = false;
Andreas Huber1aef2112011-01-04 14:01:29 -08001428
1429 (audio ? mAudioDecoder : mVideoDecoder)->signalFlush();
1430 mRenderer->flush(audio);
1431
1432 FlushStatus newStatus =
1433 needShutdown ? FLUSHING_DECODER_SHUTDOWN : FLUSHING_DECODER;
1434
1435 if (audio) {
1436 CHECK(mFlushingAudio == NONE
1437 || mFlushingAudio == AWAITING_DISCONTINUITY);
1438
1439 mFlushingAudio = newStatus;
1440
1441 if (mFlushingVideo == NONE) {
1442 mFlushingVideo = (mVideoDecoder != NULL)
1443 ? AWAITING_DISCONTINUITY
1444 : FLUSHED;
1445 }
1446 } else {
1447 CHECK(mFlushingVideo == NONE
1448 || mFlushingVideo == AWAITING_DISCONTINUITY);
1449
1450 mFlushingVideo = newStatus;
1451
1452 if (mFlushingAudio == NONE) {
1453 mFlushingAudio = (mAudioDecoder != NULL)
1454 ? AWAITING_DISCONTINUITY
1455 : FLUSHED;
1456 }
1457 }
1458}
1459
Chong Zhangced1c2f2014-08-08 15:22:35 -07001460void NuPlayer::queueDecoderShutdown(
1461 bool audio, bool video, const sp<AMessage> &reply) {
1462 ALOGI("queueDecoderShutdown audio=%d, video=%d", audio, video);
Andreas Huber84066782011-08-16 09:34:26 -07001463
Chong Zhangced1c2f2014-08-08 15:22:35 -07001464 mDeferredActions.push_back(
1465 new ShutdownDecoderAction(audio, video));
Andreas Huber84066782011-08-16 09:34:26 -07001466
Chong Zhangced1c2f2014-08-08 15:22:35 -07001467 mDeferredActions.push_back(
1468 new SimpleAction(&NuPlayer::performScanSources));
Andreas Huber84066782011-08-16 09:34:26 -07001469
Chong Zhangced1c2f2014-08-08 15:22:35 -07001470 mDeferredActions.push_back(new PostMessageAction(reply));
1471
1472 processDeferredActions();
Andreas Huber84066782011-08-16 09:34:26 -07001473}
1474
James Dong0d268a32012-08-31 12:18:27 -07001475status_t NuPlayer::setVideoScalingMode(int32_t mode) {
1476 mVideoScalingMode = mode;
Andreas Huber57a339c2012-12-03 11:18:00 -08001477 if (mNativeWindow != NULL) {
James Dong0d268a32012-08-31 12:18:27 -07001478 status_t ret = native_window_set_scaling_mode(
1479 mNativeWindow->getNativeWindow().get(), mVideoScalingMode);
1480 if (ret != OK) {
1481 ALOGE("Failed to set scaling mode (%d): %s",
1482 -ret, strerror(-ret));
1483 return ret;
1484 }
1485 }
1486 return OK;
1487}
1488
Chong Zhangdcb89b32013-08-06 09:44:47 -07001489status_t NuPlayer::getTrackInfo(Parcel* reply) const {
1490 sp<AMessage> msg = new AMessage(kWhatGetTrackInfo, id());
1491 msg->setPointer("reply", reply);
1492
1493 sp<AMessage> response;
1494 status_t err = msg->postAndAwaitResponse(&response);
1495 return err;
1496}
1497
1498status_t NuPlayer::selectTrack(size_t trackIndex, bool select) {
1499 sp<AMessage> msg = new AMessage(kWhatSelectTrack, id());
1500 msg->setSize("trackIndex", trackIndex);
1501 msg->setInt32("select", select);
1502
1503 sp<AMessage> response;
1504 status_t err = msg->postAndAwaitResponse(&response);
1505
Chong Zhang404fced2014-06-11 14:45:31 -07001506 if (err != OK) {
1507 return err;
1508 }
1509
1510 if (!response->findInt32("err", &err)) {
1511 err = OK;
1512 }
1513
Chong Zhangdcb89b32013-08-06 09:44:47 -07001514 return err;
1515}
1516
Andreas Huberb7c8e912012-11-27 15:02:53 -08001517void NuPlayer::schedulePollDuration() {
1518 sp<AMessage> msg = new AMessage(kWhatPollDuration, id());
1519 msg->setInt32("generation", mPollDurationGeneration);
1520 msg->post();
1521}
1522
1523void NuPlayer::cancelPollDuration() {
1524 ++mPollDurationGeneration;
1525}
1526
Andreas Hubera1f8ab02012-11-30 10:53:22 -08001527void NuPlayer::processDeferredActions() {
1528 while (!mDeferredActions.empty()) {
1529 // We won't execute any deferred actions until we're no longer in
1530 // an intermediate state, i.e. one more more decoders are currently
1531 // flushing or shutting down.
1532
1533 if (mRenderer != NULL) {
1534 // There's an edge case where the renderer owns all output
1535 // buffers and is paused, therefore the decoder will not read
1536 // more input data and will never encounter the matching
1537 // discontinuity. To avoid this, we resume the renderer.
1538
1539 if (mFlushingAudio == AWAITING_DISCONTINUITY
1540 || mFlushingVideo == AWAITING_DISCONTINUITY) {
1541 mRenderer->resume();
1542 }
1543 }
1544
1545 if (mFlushingAudio != NONE || mFlushingVideo != NONE) {
1546 // We're currently flushing, postpone the reset until that's
1547 // completed.
1548
1549 ALOGV("postponing action mFlushingAudio=%d, mFlushingVideo=%d",
1550 mFlushingAudio, mFlushingVideo);
1551
1552 break;
1553 }
1554
1555 sp<Action> action = *mDeferredActions.begin();
1556 mDeferredActions.erase(mDeferredActions.begin());
1557
1558 action->execute(this);
1559 }
1560}
1561
1562void NuPlayer::performSeek(int64_t seekTimeUs) {
1563 ALOGV("performSeek seekTimeUs=%lld us (%.2f secs)",
1564 seekTimeUs,
1565 seekTimeUs / 1E6);
1566
1567 mSource->seekTo(seekTimeUs);
Robert Shihd3b0bbb2014-07-23 15:00:25 -07001568 ++mTimedTextGeneration;
Andreas Hubera1f8ab02012-11-30 10:53:22 -08001569
1570 if (mDriver != NULL) {
1571 sp<NuPlayerDriver> driver = mDriver.promote();
1572 if (driver != NULL) {
1573 driver->notifyPosition(seekTimeUs);
1574 driver->notifySeekComplete();
1575 }
1576 }
1577
1578 // everything's flushed, continue playback.
1579}
1580
1581void NuPlayer::performDecoderFlush() {
1582 ALOGV("performDecoderFlush");
1583
Andreas Huberda9740e2013-04-16 10:54:03 -07001584 if (mAudioDecoder == NULL && mVideoDecoder == NULL) {
Andreas Hubera1f8ab02012-11-30 10:53:22 -08001585 return;
1586 }
1587
1588 mTimeDiscontinuityPending = true;
1589
1590 if (mAudioDecoder != NULL) {
1591 flushDecoder(true /* audio */, false /* needShutdown */);
1592 }
1593
1594 if (mVideoDecoder != NULL) {
1595 flushDecoder(false /* audio */, false /* needShutdown */);
1596 }
1597}
1598
Andreas Huber14f76722013-01-15 09:04:18 -08001599void NuPlayer::performDecoderShutdown(bool audio, bool video) {
1600 ALOGV("performDecoderShutdown audio=%d, video=%d", audio, video);
Andreas Hubera1f8ab02012-11-30 10:53:22 -08001601
Andreas Huber14f76722013-01-15 09:04:18 -08001602 if ((!audio || mAudioDecoder == NULL)
1603 && (!video || mVideoDecoder == NULL)) {
Andreas Hubera1f8ab02012-11-30 10:53:22 -08001604 return;
1605 }
1606
1607 mTimeDiscontinuityPending = true;
1608
Andreas Huber14f76722013-01-15 09:04:18 -08001609 if (mFlushingAudio == NONE && (!audio || mAudioDecoder == NULL)) {
1610 mFlushingAudio = FLUSHED;
1611 }
1612
1613 if (mFlushingVideo == NONE && (!video || mVideoDecoder == NULL)) {
1614 mFlushingVideo = FLUSHED;
1615 }
1616
1617 if (audio && mAudioDecoder != NULL) {
Andreas Hubera1f8ab02012-11-30 10:53:22 -08001618 flushDecoder(true /* audio */, true /* needShutdown */);
1619 }
1620
Andreas Huber14f76722013-01-15 09:04:18 -08001621 if (video && mVideoDecoder != NULL) {
Andreas Hubera1f8ab02012-11-30 10:53:22 -08001622 flushDecoder(false /* audio */, true /* needShutdown */);
1623 }
1624}
1625
1626void NuPlayer::performReset() {
1627 ALOGV("performReset");
1628
1629 CHECK(mAudioDecoder == NULL);
1630 CHECK(mVideoDecoder == NULL);
1631
1632 cancelPollDuration();
1633
1634 ++mScanSourcesGeneration;
1635 mScanSourcesPending = false;
1636
Lajos Molnar09524832014-07-17 14:29:51 -07001637 if (mRendererLooper != NULL) {
1638 if (mRenderer != NULL) {
1639 mRendererLooper->unregisterHandler(mRenderer->id());
1640 }
1641 mRendererLooper->stop();
1642 mRendererLooper.clear();
1643 }
Andreas Hubera1f8ab02012-11-30 10:53:22 -08001644 mRenderer.clear();
1645
1646 if (mSource != NULL) {
1647 mSource->stop();
Andreas Huberb5f25f02013-02-05 10:14:26 -08001648
1649 looper()->unregisterHandler(mSource->id());
1650
Andreas Hubera1f8ab02012-11-30 10:53:22 -08001651 mSource.clear();
1652 }
1653
1654 if (mDriver != NULL) {
1655 sp<NuPlayerDriver> driver = mDriver.promote();
1656 if (driver != NULL) {
1657 driver->notifyResetComplete();
1658 }
1659 }
Andreas Huber57a339c2012-12-03 11:18:00 -08001660
1661 mStarted = false;
Andreas Hubera1f8ab02012-11-30 10:53:22 -08001662}
1663
1664void NuPlayer::performScanSources() {
1665 ALOGV("performScanSources");
1666
Andreas Huber57a339c2012-12-03 11:18:00 -08001667 if (!mStarted) {
1668 return;
1669 }
1670
Andreas Hubera1f8ab02012-11-30 10:53:22 -08001671 if (mAudioDecoder == NULL || mVideoDecoder == NULL) {
1672 postScanSources();
1673 }
1674}
1675
Andreas Huber57a339c2012-12-03 11:18:00 -08001676void NuPlayer::performSetSurface(const sp<NativeWindowWrapper> &wrapper) {
1677 ALOGV("performSetSurface");
1678
1679 mNativeWindow = wrapper;
1680
1681 // XXX - ignore error from setVideoScalingMode for now
1682 setVideoScalingMode(mVideoScalingMode);
1683
1684 if (mDriver != NULL) {
1685 sp<NuPlayerDriver> driver = mDriver.promote();
1686 if (driver != NULL) {
1687 driver->notifySetSurfaceComplete();
1688 }
1689 }
1690}
1691
Andreas Huber9575c962013-02-05 13:59:56 -08001692void NuPlayer::onSourceNotify(const sp<AMessage> &msg) {
1693 int32_t what;
1694 CHECK(msg->findInt32("what", &what));
1695
1696 switch (what) {
1697 case Source::kWhatPrepared:
1698 {
Andreas Huberb5f28d42013-04-25 15:11:19 -07001699 if (mSource == NULL) {
1700 // This is a stale notification from a source that was
1701 // asynchronously preparing when the client called reset().
1702 // We handled the reset, the source is gone.
1703 break;
1704 }
1705
Andreas Huberec0c5972013-02-05 14:47:13 -08001706 int32_t err;
1707 CHECK(msg->findInt32("err", &err));
1708
Andreas Huber9575c962013-02-05 13:59:56 -08001709 sp<NuPlayerDriver> driver = mDriver.promote();
1710 if (driver != NULL) {
Marco Nelissendd114d12014-05-28 15:23:14 -07001711 // notify duration first, so that it's definitely set when
1712 // the app received the "prepare complete" callback.
1713 int64_t durationUs;
1714 if (mSource->getDuration(&durationUs) == OK) {
1715 driver->notifyDuration(durationUs);
1716 }
Andreas Huberec0c5972013-02-05 14:47:13 -08001717 driver->notifyPrepareCompleted(err);
Andreas Huber9575c962013-02-05 13:59:56 -08001718 }
Andreas Huber99759402013-04-01 14:28:31 -07001719
Andreas Huber9575c962013-02-05 13:59:56 -08001720 break;
1721 }
1722
1723 case Source::kWhatFlagsChanged:
1724 {
1725 uint32_t flags;
1726 CHECK(msg->findInt32("flags", (int32_t *)&flags));
1727
Chong Zhang4b7069d2013-09-11 12:52:43 -07001728 sp<NuPlayerDriver> driver = mDriver.promote();
1729 if (driver != NULL) {
1730 driver->notifyFlagsChanged(flags);
1731 }
1732
Andreas Huber9575c962013-02-05 13:59:56 -08001733 if ((mSourceFlags & Source::FLAG_DYNAMIC_DURATION)
1734 && (!(flags & Source::FLAG_DYNAMIC_DURATION))) {
1735 cancelPollDuration();
1736 } else if (!(mSourceFlags & Source::FLAG_DYNAMIC_DURATION)
1737 && (flags & Source::FLAG_DYNAMIC_DURATION)
1738 && (mAudioDecoder != NULL || mVideoDecoder != NULL)) {
1739 schedulePollDuration();
1740 }
1741
1742 mSourceFlags = flags;
1743 break;
1744 }
1745
1746 case Source::kWhatVideoSizeChanged:
1747 {
Chong Zhangced1c2f2014-08-08 15:22:35 -07001748 sp<AMessage> format;
1749 CHECK(msg->findMessage("format", &format));
Andreas Huber9575c962013-02-05 13:59:56 -08001750
Chong Zhangced1c2f2014-08-08 15:22:35 -07001751 updateVideoSize(format);
Andreas Huber9575c962013-02-05 13:59:56 -08001752 break;
1753 }
1754
Roger Jönssonb50e83e2013-01-21 16:26:41 +01001755 case Source::kWhatBufferingStart:
1756 {
1757 notifyListener(MEDIA_INFO, MEDIA_INFO_BUFFERING_START, 0);
1758 break;
1759 }
1760
1761 case Source::kWhatBufferingEnd:
1762 {
1763 notifyListener(MEDIA_INFO, MEDIA_INFO_BUFFERING_END, 0);
1764 break;
1765 }
1766
Chong Zhangdcb89b32013-08-06 09:44:47 -07001767 case Source::kWhatSubtitleData:
1768 {
1769 sp<ABuffer> buffer;
1770 CHECK(msg->findBuffer("buffer", &buffer));
1771
Chong Zhang404fced2014-06-11 14:45:31 -07001772 sendSubtitleData(buffer, 0 /* baseIndex */);
Chong Zhangdcb89b32013-08-06 09:44:47 -07001773 break;
1774 }
1775
Robert Shihd3b0bbb2014-07-23 15:00:25 -07001776 case Source::kWhatTimedTextData:
1777 {
1778 int32_t generation;
1779 if (msg->findInt32("generation", &generation)
1780 && generation != mTimedTextGeneration) {
1781 break;
1782 }
1783
1784 sp<ABuffer> buffer;
1785 CHECK(msg->findBuffer("buffer", &buffer));
1786
1787 sp<NuPlayerDriver> driver = mDriver.promote();
1788 if (driver == NULL) {
1789 break;
1790 }
1791
1792 int posMs;
1793 int64_t timeUs, posUs;
1794 driver->getCurrentPosition(&posMs);
1795 posUs = posMs * 1000;
1796 CHECK(buffer->meta()->findInt64("timeUs", &timeUs));
1797
1798 if (posUs < timeUs) {
1799 if (!msg->findInt32("generation", &generation)) {
1800 msg->setInt32("generation", mTimedTextGeneration);
1801 }
1802 msg->post(timeUs - posUs);
1803 } else {
1804 sendTimedTextData(buffer);
1805 }
1806 break;
1807 }
1808
Andreas Huber14f76722013-01-15 09:04:18 -08001809 case Source::kWhatQueueDecoderShutdown:
1810 {
1811 int32_t audio, video;
1812 CHECK(msg->findInt32("audio", &audio));
1813 CHECK(msg->findInt32("video", &video));
1814
1815 sp<AMessage> reply;
1816 CHECK(msg->findMessage("reply", &reply));
1817
1818 queueDecoderShutdown(audio, video, reply);
1819 break;
1820 }
1821
Andreas Huber9575c962013-02-05 13:59:56 -08001822 default:
1823 TRESPASS();
1824 }
1825}
1826
Chong Zhanga7fa1d92014-06-11 14:49:23 -07001827void NuPlayer::onClosedCaptionNotify(const sp<AMessage> &msg) {
1828 int32_t what;
1829 CHECK(msg->findInt32("what", &what));
1830
1831 switch (what) {
1832 case NuPlayer::CCDecoder::kWhatClosedCaptionData:
1833 {
1834 sp<ABuffer> buffer;
1835 CHECK(msg->findBuffer("buffer", &buffer));
1836
1837 size_t inbandTracks = 0;
1838 if (mSource != NULL) {
1839 inbandTracks = mSource->getTrackCount();
1840 }
1841
1842 sendSubtitleData(buffer, inbandTracks);
1843 break;
1844 }
1845
1846 case NuPlayer::CCDecoder::kWhatTrackAdded:
1847 {
1848 notifyListener(MEDIA_INFO, MEDIA_INFO_METADATA_UPDATE, 0);
1849
1850 break;
1851 }
1852
1853 default:
1854 TRESPASS();
1855 }
1856
1857
1858}
1859
Chong Zhang404fced2014-06-11 14:45:31 -07001860void NuPlayer::sendSubtitleData(const sp<ABuffer> &buffer, int32_t baseIndex) {
1861 int32_t trackIndex;
1862 int64_t timeUs, durationUs;
1863 CHECK(buffer->meta()->findInt32("trackIndex", &trackIndex));
1864 CHECK(buffer->meta()->findInt64("timeUs", &timeUs));
1865 CHECK(buffer->meta()->findInt64("durationUs", &durationUs));
1866
1867 Parcel in;
1868 in.writeInt32(trackIndex + baseIndex);
1869 in.writeInt64(timeUs);
1870 in.writeInt64(durationUs);
1871 in.writeInt32(buffer->size());
1872 in.writeInt32(buffer->size());
1873 in.write(buffer->data(), buffer->size());
1874
1875 notifyListener(MEDIA_SUBTITLE_DATA, 0, 0, &in);
1876}
Robert Shihd3b0bbb2014-07-23 15:00:25 -07001877
1878void NuPlayer::sendTimedTextData(const sp<ABuffer> &buffer) {
1879 const void *data;
1880 size_t size = 0;
1881 int64_t timeUs;
1882 int32_t flag = TextDescriptions::LOCAL_DESCRIPTIONS;
1883
1884 AString mime;
1885 CHECK(buffer->meta()->findString("mime", &mime));
1886 CHECK(strcasecmp(mime.c_str(), MEDIA_MIMETYPE_TEXT_3GPP) == 0);
1887
1888 data = buffer->data();
1889 size = buffer->size();
1890
1891 Parcel parcel;
1892 if (size > 0) {
1893 CHECK(buffer->meta()->findInt64("timeUs", &timeUs));
1894 flag |= TextDescriptions::IN_BAND_TEXT_3GPP;
1895 TextDescriptions::getParcelOfDescriptions(
1896 (const uint8_t *)data, size, flag, timeUs / 1000, &parcel);
1897 }
1898
1899 if ((parcel.dataSize() > 0)) {
1900 notifyListener(MEDIA_TIMED_TEXT, 0, 0, &parcel);
1901 } else { // send an empty timed text
1902 notifyListener(MEDIA_TIMED_TEXT, 0, 0);
1903 }
1904}
Andreas Huberb5f25f02013-02-05 10:14:26 -08001905////////////////////////////////////////////////////////////////////////////////
1906
Chong Zhangced1c2f2014-08-08 15:22:35 -07001907sp<AMessage> NuPlayer::Source::getFormat(bool audio) {
1908 sp<MetaData> meta = getFormatMeta(audio);
1909
1910 if (meta == NULL) {
1911 return NULL;
1912 }
1913
1914 sp<AMessage> msg = new AMessage;
1915
1916 if(convertMetaDataToMessage(meta, &msg) == OK) {
1917 return msg;
1918 }
1919 return NULL;
1920}
1921
Andreas Huber9575c962013-02-05 13:59:56 -08001922void NuPlayer::Source::notifyFlagsChanged(uint32_t flags) {
1923 sp<AMessage> notify = dupNotify();
1924 notify->setInt32("what", kWhatFlagsChanged);
1925 notify->setInt32("flags", flags);
1926 notify->post();
1927}
1928
Chong Zhangced1c2f2014-08-08 15:22:35 -07001929void NuPlayer::Source::notifyVideoSizeChanged(const sp<AMessage> &format) {
Andreas Huber9575c962013-02-05 13:59:56 -08001930 sp<AMessage> notify = dupNotify();
1931 notify->setInt32("what", kWhatVideoSizeChanged);
Chong Zhangced1c2f2014-08-08 15:22:35 -07001932 notify->setMessage("format", format);
Andreas Huber9575c962013-02-05 13:59:56 -08001933 notify->post();
1934}
1935
Andreas Huberec0c5972013-02-05 14:47:13 -08001936void NuPlayer::Source::notifyPrepared(status_t err) {
Andreas Huber9575c962013-02-05 13:59:56 -08001937 sp<AMessage> notify = dupNotify();
1938 notify->setInt32("what", kWhatPrepared);
Andreas Huberec0c5972013-02-05 14:47:13 -08001939 notify->setInt32("err", err);
Andreas Huber9575c962013-02-05 13:59:56 -08001940 notify->post();
1941}
1942
Andreas Huber84333e02014-02-07 15:36:10 -08001943void NuPlayer::Source::onMessageReceived(const sp<AMessage> & /* msg */) {
Andreas Huberb5f25f02013-02-05 10:14:26 -08001944 TRESPASS();
1945}
1946
Andreas Huberf9334412010-12-15 15:17:42 -08001947} // namespace android