blob: d56b1f0dd74242103c10eb53a0a731b0bf95146d [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 Zhang3de157d2014-08-05 20:54:44 -0700235 status_t err = genericSource->init(httpService, url, headers);
236
237 if (err == OK) {
238 source = genericSource;
239 } else {
240 ALOGE("Failed to initialize generic source!");
241 }
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
255 status_t err = source->init(fd, offset, length);
256
257 if (err != OK) {
258 ALOGE("Failed to initialize generic source!");
259 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
Andreas Huber3831a062010-12-21 10:22:33 -0800855
Andreas Huber31e25082011-01-10 10:38:31 -0800856 int32_t width, height;
Lajos Molnar1cd13982014-01-17 15:12:51 -0800857 CHECK(format->findInt32("width", &width));
858 CHECK(format->findInt32("height", &height));
Andreas Huber31e25082011-01-10 10:38:31 -0800859
860 int32_t cropLeft, cropTop, cropRight, cropBottom;
Lajos Molnar1cd13982014-01-17 15:12:51 -0800861 CHECK(format->findRect(
Andreas Huber31e25082011-01-10 10:38:31 -0800862 "crop",
863 &cropLeft, &cropTop, &cropRight, &cropBottom));
864
Andreas Huber516dacf2012-12-03 15:20:40 -0800865 int32_t displayWidth = cropRight - cropLeft + 1;
866 int32_t displayHeight = cropBottom - cropTop + 1;
867
Steve Block3856b092011-10-20 11:56:00 +0100868 ALOGV("Video output format changed to %d x %d "
Andreas Hubercb67cd12011-08-26 16:02:19 -0700869 "(crop: %d x %d @ (%d, %d))",
Andreas Huber31e25082011-01-10 10:38:31 -0800870 width, height,
Andreas Huber516dacf2012-12-03 15:20:40 -0800871 displayWidth,
872 displayHeight,
Andreas Hubercb67cd12011-08-26 16:02:19 -0700873 cropLeft, cropTop);
Andreas Huber31e25082011-01-10 10:38:31 -0800874
Andreas Huber516dacf2012-12-03 15:20:40 -0800875 sp<AMessage> videoInputFormat =
876 mSource->getFormat(false /* audio */);
877
878 // Take into account sample aspect ratio if necessary:
879 int32_t sarWidth, sarHeight;
880 if (videoInputFormat->findInt32("sar-width", &sarWidth)
881 && videoInputFormat->findInt32(
882 "sar-height", &sarHeight)) {
883 ALOGV("Sample aspect ratio %d : %d",
884 sarWidth, sarHeight);
885
886 displayWidth = (displayWidth * sarWidth) / sarHeight;
887
888 ALOGV("display dimensions %d x %d",
889 displayWidth, displayHeight);
890 }
891
Chong Zhange9e63bc2014-07-30 17:25:06 -0700892 int32_t rotationDegrees;
893 if (!videoInputFormat->findInt32(
894 "rotation-degrees", &rotationDegrees)) {
895 rotationDegrees = 0;
896 }
897
898 if (rotationDegrees == 90 || rotationDegrees == 270) {
899 notifyListener(
900 MEDIA_SET_VIDEO_SIZE,
901 displayHeight,
902 displayWidth);
903 } else {
904 notifyListener(
905 MEDIA_SET_VIDEO_SIZE,
906 displayWidth,
907 displayHeight);
908 }
Andreas Huber31e25082011-01-10 10:38:31 -0800909 }
Lajos Molnar1cd13982014-01-17 15:12:51 -0800910 } else if (what == Decoder::kWhatShutdownCompleted) {
Steve Block3856b092011-10-20 11:56:00 +0100911 ALOGV("%s shutdown completed", audio ? "audio" : "video");
Andreas Huber3831a062010-12-21 10:22:33 -0800912 if (audio) {
913 mAudioDecoder.clear();
914
915 CHECK_EQ((int)mFlushingAudio, (int)SHUTTING_DOWN_DECODER);
916 mFlushingAudio = SHUT_DOWN;
917 } else {
918 mVideoDecoder.clear();
919
920 CHECK_EQ((int)mFlushingVideo, (int)SHUTTING_DOWN_DECODER);
921 mFlushingVideo = SHUT_DOWN;
922 }
923
924 finishFlushIfPossible();
Lajos Molnar1cd13982014-01-17 15:12:51 -0800925 } else if (what == Decoder::kWhatError) {
Steve Block29357bc2012-01-06 19:20:56 +0000926 ALOGE("Received error from %s decoder, aborting playback.",
Andreas Huberc92fd242011-08-16 13:48:44 -0700927 audio ? "audio" : "video");
928
929 mRenderer->queueEOS(audio, UNKNOWN_ERROR);
Lajos Molnar1cd13982014-01-17 15:12:51 -0800930 } else if (what == Decoder::kWhatDrainThisBuffer) {
931 renderBuffer(audio, msg);
932 } else {
933 ALOGV("Unhandled decoder notification %d '%c%c%c%c'.",
Andreas Hubera1f8ab02012-11-30 10:53:22 -0800934 what,
935 what >> 24,
936 (what >> 16) & 0xff,
937 (what >> 8) & 0xff,
938 what & 0xff);
Andreas Huberf9334412010-12-15 15:17:42 -0800939 }
940
941 break;
942 }
943
944 case kWhatRendererNotify:
945 {
946 int32_t what;
947 CHECK(msg->findInt32("what", &what));
948
949 if (what == Renderer::kWhatEOS) {
950 int32_t audio;
951 CHECK(msg->findInt32("audio", &audio));
952
Andreas Huberc92fd242011-08-16 13:48:44 -0700953 int32_t finalResult;
954 CHECK(msg->findInt32("finalResult", &finalResult));
955
Andreas Huberf9334412010-12-15 15:17:42 -0800956 if (audio) {
957 mAudioEOS = true;
958 } else {
959 mVideoEOS = true;
960 }
961
Andreas Huberc92fd242011-08-16 13:48:44 -0700962 if (finalResult == ERROR_END_OF_STREAM) {
Steve Block3856b092011-10-20 11:56:00 +0100963 ALOGV("reached %s EOS", audio ? "audio" : "video");
Andreas Huberc92fd242011-08-16 13:48:44 -0700964 } else {
Steve Block29357bc2012-01-06 19:20:56 +0000965 ALOGE("%s track encountered an error (%d)",
Andreas Huberc92fd242011-08-16 13:48:44 -0700966 audio ? "audio" : "video", finalResult);
967
968 notifyListener(
969 MEDIA_ERROR, MEDIA_ERROR_UNKNOWN, finalResult);
970 }
Andreas Huberf9334412010-12-15 15:17:42 -0800971
972 if ((mAudioEOS || mAudioDecoder == NULL)
973 && (mVideoEOS || mVideoDecoder == NULL)) {
974 notifyListener(MEDIA_PLAYBACK_COMPLETE, 0, 0);
975 }
Andreas Huber43c3e6c2011-01-05 12:17:08 -0800976 } else if (what == Renderer::kWhatPosition) {
977 int64_t positionUs;
978 CHECK(msg->findInt64("positionUs", &positionUs));
979
Andreas Huber3fe62152011-09-16 15:09:22 -0700980 CHECK(msg->findInt64("videoLateByUs", &mVideoLateByUs));
981
Andreas Huber43c3e6c2011-01-05 12:17:08 -0800982 if (mDriver != NULL) {
983 sp<NuPlayerDriver> driver = mDriver.promote();
984 if (driver != NULL) {
985 driver->notifyPosition(positionUs);
Andreas Huber3fe62152011-09-16 15:09:22 -0700986
987 driver->notifyFrameStats(
988 mNumFramesTotal, mNumFramesDropped);
Andreas Huber43c3e6c2011-01-05 12:17:08 -0800989 }
990 }
Andreas Huber3fe62152011-09-16 15:09:22 -0700991 } else if (what == Renderer::kWhatFlushComplete) {
Andreas Huberf9334412010-12-15 15:17:42 -0800992 int32_t audio;
993 CHECK(msg->findInt32("audio", &audio));
994
Steve Block3856b092011-10-20 11:56:00 +0100995 ALOGV("renderer %s flush completed.", audio ? "audio" : "video");
James Dongf57b4ea2012-07-20 13:38:36 -0700996 } else if (what == Renderer::kWhatVideoRenderingStart) {
997 notifyListener(MEDIA_INFO, MEDIA_INFO_RENDERING_START, 0);
Lajos Molnarcbaffcf2013-08-14 18:30:38 -0700998 } else if (what == Renderer::kWhatMediaRenderingStart) {
999 ALOGV("media rendering started");
1000 notifyListener(MEDIA_STARTED, 0, 0);
Wei Jia3a2956d2014-07-22 16:01:33 -07001001 } else if (what == Renderer::kWhatAudioOffloadTearDown) {
1002 ALOGV("Tear down audio offload, fall back to s/w path");
1003 int64_t positionUs;
1004 CHECK(msg->findInt64("positionUs", &positionUs));
1005 mAudioSink->close();
1006 mAudioDecoder.clear();
1007 mRenderer->flush(true /* audio */);
1008 if (mVideoDecoder != NULL) {
1009 mRenderer->flush(false /* audio */);
1010 }
1011 mRenderer->signalDisableOffloadAudio();
1012 mOffloadAudio = false;
1013
1014 performSeek(positionUs);
1015 instantiateDecoder(true /* audio */, &mAudioDecoder);
Andreas Huberf9334412010-12-15 15:17:42 -08001016 }
1017 break;
1018 }
1019
1020 case kWhatMoreDataQueued:
1021 {
1022 break;
1023 }
1024
Andreas Huber1aef2112011-01-04 14:01:29 -08001025 case kWhatReset:
1026 {
Steve Block3856b092011-10-20 11:56:00 +01001027 ALOGV("kWhatReset");
Andreas Huber1aef2112011-01-04 14:01:29 -08001028
Andreas Hubera1f8ab02012-11-30 10:53:22 -08001029 mDeferredActions.push_back(
Andreas Huber14f76722013-01-15 09:04:18 -08001030 new ShutdownDecoderAction(
1031 true /* audio */, true /* video */));
Andreas Huberb7c8e912012-11-27 15:02:53 -08001032
Andreas Hubera1f8ab02012-11-30 10:53:22 -08001033 mDeferredActions.push_back(
1034 new SimpleAction(&NuPlayer::performReset));
Andreas Huberb58ce9f2011-11-28 16:27:35 -08001035
Andreas Hubera1f8ab02012-11-30 10:53:22 -08001036 processDeferredActions();
Andreas Huber1aef2112011-01-04 14:01:29 -08001037 break;
1038 }
1039
Andreas Huber43c3e6c2011-01-05 12:17:08 -08001040 case kWhatSeek:
1041 {
1042 int64_t seekTimeUs;
1043 CHECK(msg->findInt64("seekTimeUs", &seekTimeUs));
1044
Andreas Hubera1f8ab02012-11-30 10:53:22 -08001045 ALOGV("kWhatSeek seekTimeUs=%lld us", seekTimeUs);
Andreas Huber43c3e6c2011-01-05 12:17:08 -08001046
Andreas Hubera1f8ab02012-11-30 10:53:22 -08001047 mDeferredActions.push_back(
1048 new SimpleAction(&NuPlayer::performDecoderFlush));
Andreas Huber43c3e6c2011-01-05 12:17:08 -08001049
Andreas Hubera1f8ab02012-11-30 10:53:22 -08001050 mDeferredActions.push_back(new SeekAction(seekTimeUs));
Andreas Huber43c3e6c2011-01-05 12:17:08 -08001051
Andreas Hubera1f8ab02012-11-30 10:53:22 -08001052 processDeferredActions();
Andreas Huber43c3e6c2011-01-05 12:17:08 -08001053 break;
1054 }
1055
Andreas Huberb4082222011-01-20 15:23:04 -08001056 case kWhatPause:
1057 {
1058 CHECK(mRenderer != NULL);
Roger Jönssonfba60da2013-01-21 17:15:45 +01001059 mSource->pause();
Andreas Huberb4082222011-01-20 15:23:04 -08001060 mRenderer->pause();
1061 break;
1062 }
1063
1064 case kWhatResume:
1065 {
1066 CHECK(mRenderer != NULL);
Roger Jönssonfba60da2013-01-21 17:15:45 +01001067 mSource->resume();
Andreas Huberb4082222011-01-20 15:23:04 -08001068 mRenderer->resume();
1069 break;
1070 }
1071
Andreas Huberb5f25f02013-02-05 10:14:26 -08001072 case kWhatSourceNotify:
1073 {
Andreas Huber9575c962013-02-05 13:59:56 -08001074 onSourceNotify(msg);
Andreas Huberb5f25f02013-02-05 10:14:26 -08001075 break;
1076 }
1077
Chong Zhanga7fa1d92014-06-11 14:49:23 -07001078 case kWhatClosedCaptionNotify:
1079 {
1080 onClosedCaptionNotify(msg);
1081 break;
1082 }
1083
Andreas Huberf9334412010-12-15 15:17:42 -08001084 default:
1085 TRESPASS();
1086 break;
1087 }
1088}
1089
Andreas Huber3831a062010-12-21 10:22:33 -08001090void NuPlayer::finishFlushIfPossible() {
1091 if (mFlushingAudio != FLUSHED && mFlushingAudio != SHUT_DOWN) {
1092 return;
1093 }
1094
1095 if (mFlushingVideo != FLUSHED && mFlushingVideo != SHUT_DOWN) {
1096 return;
1097 }
1098
Steve Block3856b092011-10-20 11:56:00 +01001099 ALOGV("both audio and video are flushed now.");
Andreas Huber3831a062010-12-21 10:22:33 -08001100
Andreas Huber6e3d3112011-11-28 12:36:11 -08001101 if (mTimeDiscontinuityPending) {
1102 mRenderer->signalTimeDiscontinuity();
1103 mTimeDiscontinuityPending = false;
1104 }
Andreas Huber3831a062010-12-21 10:22:33 -08001105
Andreas Huber22fc52f2011-01-05 16:24:27 -08001106 if (mAudioDecoder != NULL) {
Andreas Huber3831a062010-12-21 10:22:33 -08001107 mAudioDecoder->signalResume();
1108 }
1109
Andreas Huber22fc52f2011-01-05 16:24:27 -08001110 if (mVideoDecoder != NULL) {
Andreas Huber3831a062010-12-21 10:22:33 -08001111 mVideoDecoder->signalResume();
1112 }
1113
1114 mFlushingAudio = NONE;
1115 mFlushingVideo = NONE;
Andreas Huber3831a062010-12-21 10:22:33 -08001116
Andreas Hubera1f8ab02012-11-30 10:53:22 -08001117 processDeferredActions();
Andreas Huber1aef2112011-01-04 14:01:29 -08001118}
1119
1120void NuPlayer::postScanSources() {
1121 if (mScanSourcesPending) {
1122 return;
1123 }
1124
1125 sp<AMessage> msg = new AMessage(kWhatScanSources, id());
1126 msg->setInt32("generation", mScanSourcesGeneration);
1127 msg->post();
1128
1129 mScanSourcesPending = true;
1130}
1131
Andreas Huber5bc087c2010-12-23 10:27:40 -08001132status_t NuPlayer::instantiateDecoder(bool audio, sp<Decoder> *decoder) {
Andreas Huberf9334412010-12-15 15:17:42 -08001133 if (*decoder != NULL) {
1134 return OK;
1135 }
1136
Andreas Huber84066782011-08-16 09:34:26 -07001137 sp<AMessage> format = mSource->getFormat(audio);
Andreas Huberf9334412010-12-15 15:17:42 -08001138
Andreas Huber84066782011-08-16 09:34:26 -07001139 if (format == NULL) {
Andreas Huberf9334412010-12-15 15:17:42 -08001140 return -EWOULDBLOCK;
1141 }
1142
Andreas Huber3fe62152011-09-16 15:09:22 -07001143 if (!audio) {
Andreas Huber84066782011-08-16 09:34:26 -07001144 AString mime;
1145 CHECK(format->findString("mime", &mime));
1146 mVideoIsAVC = !strcasecmp(MEDIA_MIMETYPE_VIDEO_AVC, mime.c_str());
Chong Zhanga7fa1d92014-06-11 14:49:23 -07001147
1148 sp<AMessage> ccNotify = new AMessage(kWhatClosedCaptionNotify, id());
1149 mCCDecoder = new CCDecoder(ccNotify);
Lajos Molnar09524832014-07-17 14:29:51 -07001150
1151 if (mSourceFlags & Source::FLAG_SECURE) {
1152 format->setInt32("secure", true);
1153 }
Andreas Huber3fe62152011-09-16 15:09:22 -07001154 }
1155
Andreas Huberf9334412010-12-15 15:17:42 -08001156 sp<AMessage> notify =
1157 new AMessage(audio ? kWhatAudioNotify : kWhatVideoNotify,
1158 id());
1159
Wei Jiabc2fb722014-07-08 16:37:57 -07001160 if (audio) {
1161 if (mOffloadAudio) {
1162 *decoder = new DecoderPassThrough(notify);
1163 } else {
1164 *decoder = new Decoder(notify);
1165 }
1166 } else {
1167 *decoder = new Decoder(notify, mNativeWindow);
1168 }
Lajos Molnar1cd13982014-01-17 15:12:51 -08001169 (*decoder)->init();
Andreas Huber84066782011-08-16 09:34:26 -07001170 (*decoder)->configure(format);
Andreas Huberf9334412010-12-15 15:17:42 -08001171
Lajos Molnar09524832014-07-17 14:29:51 -07001172 // allocate buffers to decrypt widevine source buffers
1173 if (!audio && (mSourceFlags & Source::FLAG_SECURE)) {
1174 Vector<sp<ABuffer> > inputBufs;
1175 CHECK_EQ((*decoder)->getInputBuffers(&inputBufs), (status_t)OK);
1176
1177 Vector<MediaBuffer *> mediaBufs;
1178 for (size_t i = 0; i < inputBufs.size(); i++) {
1179 const sp<ABuffer> &buffer = inputBufs[i];
1180 MediaBuffer *mbuf = new MediaBuffer(buffer->data(), buffer->size());
1181 mediaBufs.push(mbuf);
1182 }
1183
1184 status_t err = mSource->setBuffers(audio, mediaBufs);
1185 if (err != OK) {
1186 for (size_t i = 0; i < mediaBufs.size(); ++i) {
1187 mediaBufs[i]->release();
1188 }
1189 mediaBufs.clear();
1190 ALOGE("Secure source didn't support secure mediaBufs.");
1191 return err;
1192 }
1193 }
Andreas Huberf9334412010-12-15 15:17:42 -08001194 return OK;
1195}
1196
1197status_t NuPlayer::feedDecoderInputData(bool audio, const sp<AMessage> &msg) {
1198 sp<AMessage> reply;
1199 CHECK(msg->findMessage("reply", &reply));
1200
Wei Jia69a85b72014-08-04 18:32:43 -07001201 if ((audio && mFlushingAudio != NONE
1202 && mFlushingAudio != AWAITING_DISCONTINUITY)
1203 || (!audio && mFlushingVideo != NONE
1204 && mFlushingVideo != AWAITING_DISCONTINUITY)) {
1205 return -EWOULDBLOCK;
Andreas Huberf9334412010-12-15 15:17:42 -08001206 }
1207
1208 sp<ABuffer> accessUnit;
Andreas Huberf9334412010-12-15 15:17:42 -08001209
Andreas Huber3fe62152011-09-16 15:09:22 -07001210 bool dropAccessUnit;
1211 do {
1212 status_t err = mSource->dequeueAccessUnit(audio, &accessUnit);
Andreas Huber5bc087c2010-12-23 10:27:40 -08001213
Andreas Huber3fe62152011-09-16 15:09:22 -07001214 if (err == -EWOULDBLOCK) {
1215 return err;
1216 } else if (err != OK) {
1217 if (err == INFO_DISCONTINUITY) {
1218 int32_t type;
1219 CHECK(accessUnit->meta()->findInt32("discontinuity", &type));
Andreas Huber53df1a42010-12-22 10:03:04 -08001220
Andreas Huber3fe62152011-09-16 15:09:22 -07001221 bool formatChange =
Andreas Huber6e3d3112011-11-28 12:36:11 -08001222 (audio &&
1223 (type & ATSParser::DISCONTINUITY_AUDIO_FORMAT))
1224 || (!audio &&
1225 (type & ATSParser::DISCONTINUITY_VIDEO_FORMAT));
Andreas Huber53df1a42010-12-22 10:03:04 -08001226
Andreas Huber6e3d3112011-11-28 12:36:11 -08001227 bool timeChange = (type & ATSParser::DISCONTINUITY_TIME) != 0;
1228
Steve Blockdf64d152012-01-04 20:05:49 +00001229 ALOGI("%s discontinuity (formatChange=%d, time=%d)",
Andreas Huber6e3d3112011-11-28 12:36:11 -08001230 audio ? "audio" : "video", formatChange, timeChange);
Andreas Huber32f3cef2011-03-02 15:34:46 -08001231
Andreas Huber3fe62152011-09-16 15:09:22 -07001232 if (audio) {
1233 mSkipRenderingAudioUntilMediaTimeUs = -1;
1234 } else {
1235 mSkipRenderingVideoUntilMediaTimeUs = -1;
1236 }
Andreas Huber32f3cef2011-03-02 15:34:46 -08001237
Andreas Huber6e3d3112011-11-28 12:36:11 -08001238 if (timeChange) {
1239 sp<AMessage> extra;
1240 if (accessUnit->meta()->findMessage("extra", &extra)
1241 && extra != NULL) {
1242 int64_t resumeAtMediaTimeUs;
1243 if (extra->findInt64(
1244 "resume-at-mediatimeUs", &resumeAtMediaTimeUs)) {
Steve Blockdf64d152012-01-04 20:05:49 +00001245 ALOGI("suppressing rendering of %s until %lld us",
Andreas Huber6e3d3112011-11-28 12:36:11 -08001246 audio ? "audio" : "video", resumeAtMediaTimeUs);
Andreas Huber3fe62152011-09-16 15:09:22 -07001247
Andreas Huber6e3d3112011-11-28 12:36:11 -08001248 if (audio) {
1249 mSkipRenderingAudioUntilMediaTimeUs =
1250 resumeAtMediaTimeUs;
1251 } else {
1252 mSkipRenderingVideoUntilMediaTimeUs =
1253 resumeAtMediaTimeUs;
1254 }
Andreas Huber3fe62152011-09-16 15:09:22 -07001255 }
Andreas Huber32f3cef2011-03-02 15:34:46 -08001256 }
1257 }
Andreas Huber3fe62152011-09-16 15:09:22 -07001258
Andreas Huber6e3d3112011-11-28 12:36:11 -08001259 mTimeDiscontinuityPending =
1260 mTimeDiscontinuityPending || timeChange;
1261
Robert Shiha2981012014-07-30 17:41:24 -07001262 if (mFlushingAudio == NONE && mFlushingVideo == NONE) {
1263 // And we'll resume scanning sources once we're done
1264 // flushing.
1265 mDeferredActions.push_front(
1266 new SimpleAction(
1267 &NuPlayer::performScanSources));
1268 }
1269
Andreas Huber6e3d3112011-11-28 12:36:11 -08001270 if (formatChange || timeChange) {
Andreas Hubera1f8ab02012-11-30 10:53:22 -08001271
Robert Shih6d0a94e2014-01-23 16:18:22 -08001272 sp<AMessage> newFormat = mSource->getFormat(audio);
1273 sp<Decoder> &decoder = audio ? mAudioDecoder : mVideoDecoder;
1274 if (formatChange && !decoder->supportsSeamlessFormatChange(newFormat)) {
1275 flushDecoder(audio, /* needShutdown = */ true);
1276 } else {
1277 flushDecoder(audio, /* needShutdown = */ false);
1278 err = OK;
1279 }
Andreas Huber6e3d3112011-11-28 12:36:11 -08001280 } else {
1281 // This stream is unaffected by the discontinuity
1282
1283 if (audio) {
1284 mFlushingAudio = FLUSHED;
1285 } else {
1286 mFlushingVideo = FLUSHED;
1287 }
1288
1289 finishFlushIfPossible();
1290
1291 return -EWOULDBLOCK;
1292 }
Andreas Huber32f3cef2011-03-02 15:34:46 -08001293 }
1294
Andreas Huber3fe62152011-09-16 15:09:22 -07001295 reply->setInt32("err", err);
1296 reply->post();
1297 return OK;
Andreas Huberf9334412010-12-15 15:17:42 -08001298 }
1299
Andreas Huber3fe62152011-09-16 15:09:22 -07001300 if (!audio) {
1301 ++mNumFramesTotal;
1302 }
1303
1304 dropAccessUnit = false;
1305 if (!audio
Lajos Molnar09524832014-07-17 14:29:51 -07001306 && !(mSourceFlags & Source::FLAG_SECURE)
Andreas Huber3fe62152011-09-16 15:09:22 -07001307 && mVideoLateByUs > 100000ll
1308 && mVideoIsAVC
1309 && !IsAVCReferenceFrame(accessUnit)) {
1310 dropAccessUnit = true;
1311 ++mNumFramesDropped;
1312 }
1313 } while (dropAccessUnit);
Andreas Huberf9334412010-12-15 15:17:42 -08001314
Steve Block3856b092011-10-20 11:56:00 +01001315 // ALOGV("returned a valid buffer of %s data", audio ? "audio" : "video");
Andreas Huberf9334412010-12-15 15:17:42 -08001316
1317#if 0
1318 int64_t mediaTimeUs;
1319 CHECK(accessUnit->meta()->findInt64("timeUs", &mediaTimeUs));
Steve Block3856b092011-10-20 11:56:00 +01001320 ALOGV("feeding %s input buffer at media time %.2f secs",
Andreas Huberf9334412010-12-15 15:17:42 -08001321 audio ? "audio" : "video",
1322 mediaTimeUs / 1E6);
1323#endif
1324
Chong Zhanga7fa1d92014-06-11 14:49:23 -07001325 if (!audio) {
1326 mCCDecoder->decode(accessUnit);
1327 }
1328
Andreas Huber2d8bedd2012-02-21 14:38:23 -08001329 reply->setBuffer("buffer", accessUnit);
Andreas Huberf9334412010-12-15 15:17:42 -08001330 reply->post();
1331
1332 return OK;
1333}
1334
1335void NuPlayer::renderBuffer(bool audio, const sp<AMessage> &msg) {
Steve Block3856b092011-10-20 11:56:00 +01001336 // ALOGV("renderBuffer %s", audio ? "audio" : "video");
Andreas Huberf9334412010-12-15 15:17:42 -08001337
1338 sp<AMessage> reply;
1339 CHECK(msg->findMessage("reply", &reply));
1340
Andreas Huber18ac5402011-08-31 15:04:25 -07001341 if (IsFlushingState(audio ? mFlushingAudio : mFlushingVideo)) {
1342 // We're currently attempting to flush the decoder, in order
1343 // to complete this, the decoder wants all its buffers back,
1344 // so we don't want any output buffers it sent us (from before
1345 // we initiated the flush) to be stuck in the renderer's queue.
1346
Steve Block3856b092011-10-20 11:56:00 +01001347 ALOGV("we're still flushing the %s decoder, sending its output buffer"
Andreas Huber18ac5402011-08-31 15:04:25 -07001348 " right back.", audio ? "audio" : "video");
1349
1350 reply->post();
1351 return;
1352 }
1353
Andreas Huber2d8bedd2012-02-21 14:38:23 -08001354 sp<ABuffer> buffer;
1355 CHECK(msg->findBuffer("buffer", &buffer));
Andreas Huberf9334412010-12-15 15:17:42 -08001356
Chong Zhanga7fa1d92014-06-11 14:49:23 -07001357 int64_t mediaTimeUs;
1358 CHECK(buffer->meta()->findInt64("timeUs", &mediaTimeUs));
1359
Andreas Huber32f3cef2011-03-02 15:34:46 -08001360 int64_t &skipUntilMediaTimeUs =
1361 audio
1362 ? mSkipRenderingAudioUntilMediaTimeUs
1363 : mSkipRenderingVideoUntilMediaTimeUs;
1364
1365 if (skipUntilMediaTimeUs >= 0) {
Andreas Huber32f3cef2011-03-02 15:34:46 -08001366
1367 if (mediaTimeUs < skipUntilMediaTimeUs) {
Steve Block3856b092011-10-20 11:56:00 +01001368 ALOGV("dropping %s buffer at time %lld as requested.",
Andreas Huber32f3cef2011-03-02 15:34:46 -08001369 audio ? "audio" : "video",
1370 mediaTimeUs);
1371
1372 reply->post();
1373 return;
1374 }
1375
1376 skipUntilMediaTimeUs = -1;
1377 }
1378
Chong Zhanga7fa1d92014-06-11 14:49:23 -07001379 if (!audio && mCCDecoder->isSelected()) {
1380 mCCDecoder->display(mediaTimeUs);
1381 }
1382
Andreas Huberf9334412010-12-15 15:17:42 -08001383 mRenderer->queueBuffer(audio, buffer, reply);
1384}
1385
Chong Zhangdcb89b32013-08-06 09:44:47 -07001386void NuPlayer::notifyListener(int msg, int ext1, int ext2, const Parcel *in) {
Andreas Huber43c3e6c2011-01-05 12:17:08 -08001387 if (mDriver == NULL) {
Andreas Huberf9334412010-12-15 15:17:42 -08001388 return;
1389 }
1390
Andreas Huber43c3e6c2011-01-05 12:17:08 -08001391 sp<NuPlayerDriver> driver = mDriver.promote();
Andreas Huberf9334412010-12-15 15:17:42 -08001392
Andreas Huber43c3e6c2011-01-05 12:17:08 -08001393 if (driver == NULL) {
Andreas Huberf9334412010-12-15 15:17:42 -08001394 return;
1395 }
1396
Chong Zhangdcb89b32013-08-06 09:44:47 -07001397 driver->notifyListener(msg, ext1, ext2, in);
Andreas Huberf9334412010-12-15 15:17:42 -08001398}
1399
Andreas Huber1aef2112011-01-04 14:01:29 -08001400void NuPlayer::flushDecoder(bool audio, bool needShutdown) {
Andreas Huber14f76722013-01-15 09:04:18 -08001401 ALOGV("[%s] flushDecoder needShutdown=%d",
1402 audio ? "audio" : "video", needShutdown);
1403
Andreas Huber6e3d3112011-11-28 12:36:11 -08001404 if ((audio && mAudioDecoder == NULL) || (!audio && mVideoDecoder == NULL)) {
Steve Blockdf64d152012-01-04 20:05:49 +00001405 ALOGI("flushDecoder %s without decoder present",
Andreas Huber6e3d3112011-11-28 12:36:11 -08001406 audio ? "audio" : "video");
1407 }
1408
Andreas Huber1aef2112011-01-04 14:01:29 -08001409 // Make sure we don't continue to scan sources until we finish flushing.
1410 ++mScanSourcesGeneration;
Andreas Huber43c3e6c2011-01-05 12:17:08 -08001411 mScanSourcesPending = false;
Andreas Huber1aef2112011-01-04 14:01:29 -08001412
1413 (audio ? mAudioDecoder : mVideoDecoder)->signalFlush();
1414 mRenderer->flush(audio);
1415
1416 FlushStatus newStatus =
1417 needShutdown ? FLUSHING_DECODER_SHUTDOWN : FLUSHING_DECODER;
1418
1419 if (audio) {
1420 CHECK(mFlushingAudio == NONE
1421 || mFlushingAudio == AWAITING_DISCONTINUITY);
1422
1423 mFlushingAudio = newStatus;
1424
1425 if (mFlushingVideo == NONE) {
1426 mFlushingVideo = (mVideoDecoder != NULL)
1427 ? AWAITING_DISCONTINUITY
1428 : FLUSHED;
1429 }
1430 } else {
1431 CHECK(mFlushingVideo == NONE
1432 || mFlushingVideo == AWAITING_DISCONTINUITY);
1433
1434 mFlushingVideo = newStatus;
1435
1436 if (mFlushingAudio == NONE) {
1437 mFlushingAudio = (mAudioDecoder != NULL)
1438 ? AWAITING_DISCONTINUITY
1439 : FLUSHED;
1440 }
1441 }
1442}
1443
Andreas Huber84066782011-08-16 09:34:26 -07001444sp<AMessage> NuPlayer::Source::getFormat(bool audio) {
1445 sp<MetaData> meta = getFormatMeta(audio);
1446
1447 if (meta == NULL) {
1448 return NULL;
1449 }
1450
1451 sp<AMessage> msg = new AMessage;
1452
1453 if(convertMetaDataToMessage(meta, &msg) == OK) {
1454 return msg;
1455 }
1456 return NULL;
1457}
1458
James Dong0d268a32012-08-31 12:18:27 -07001459status_t NuPlayer::setVideoScalingMode(int32_t mode) {
1460 mVideoScalingMode = mode;
Andreas Huber57a339c2012-12-03 11:18:00 -08001461 if (mNativeWindow != NULL) {
James Dong0d268a32012-08-31 12:18:27 -07001462 status_t ret = native_window_set_scaling_mode(
1463 mNativeWindow->getNativeWindow().get(), mVideoScalingMode);
1464 if (ret != OK) {
1465 ALOGE("Failed to set scaling mode (%d): %s",
1466 -ret, strerror(-ret));
1467 return ret;
1468 }
1469 }
1470 return OK;
1471}
1472
Chong Zhangdcb89b32013-08-06 09:44:47 -07001473status_t NuPlayer::getTrackInfo(Parcel* reply) const {
1474 sp<AMessage> msg = new AMessage(kWhatGetTrackInfo, id());
1475 msg->setPointer("reply", reply);
1476
1477 sp<AMessage> response;
1478 status_t err = msg->postAndAwaitResponse(&response);
1479 return err;
1480}
1481
1482status_t NuPlayer::selectTrack(size_t trackIndex, bool select) {
1483 sp<AMessage> msg = new AMessage(kWhatSelectTrack, id());
1484 msg->setSize("trackIndex", trackIndex);
1485 msg->setInt32("select", select);
1486
1487 sp<AMessage> response;
1488 status_t err = msg->postAndAwaitResponse(&response);
1489
Chong Zhang404fced2014-06-11 14:45:31 -07001490 if (err != OK) {
1491 return err;
1492 }
1493
1494 if (!response->findInt32("err", &err)) {
1495 err = OK;
1496 }
1497
Chong Zhangdcb89b32013-08-06 09:44:47 -07001498 return err;
1499}
1500
Andreas Huberb7c8e912012-11-27 15:02:53 -08001501void NuPlayer::schedulePollDuration() {
1502 sp<AMessage> msg = new AMessage(kWhatPollDuration, id());
1503 msg->setInt32("generation", mPollDurationGeneration);
1504 msg->post();
1505}
1506
1507void NuPlayer::cancelPollDuration() {
1508 ++mPollDurationGeneration;
1509}
1510
Andreas Hubera1f8ab02012-11-30 10:53:22 -08001511void NuPlayer::processDeferredActions() {
1512 while (!mDeferredActions.empty()) {
1513 // We won't execute any deferred actions until we're no longer in
1514 // an intermediate state, i.e. one more more decoders are currently
1515 // flushing or shutting down.
1516
1517 if (mRenderer != NULL) {
1518 // There's an edge case where the renderer owns all output
1519 // buffers and is paused, therefore the decoder will not read
1520 // more input data and will never encounter the matching
1521 // discontinuity. To avoid this, we resume the renderer.
1522
1523 if (mFlushingAudio == AWAITING_DISCONTINUITY
1524 || mFlushingVideo == AWAITING_DISCONTINUITY) {
1525 mRenderer->resume();
1526 }
1527 }
1528
1529 if (mFlushingAudio != NONE || mFlushingVideo != NONE) {
1530 // We're currently flushing, postpone the reset until that's
1531 // completed.
1532
1533 ALOGV("postponing action mFlushingAudio=%d, mFlushingVideo=%d",
1534 mFlushingAudio, mFlushingVideo);
1535
1536 break;
1537 }
1538
1539 sp<Action> action = *mDeferredActions.begin();
1540 mDeferredActions.erase(mDeferredActions.begin());
1541
1542 action->execute(this);
1543 }
1544}
1545
1546void NuPlayer::performSeek(int64_t seekTimeUs) {
1547 ALOGV("performSeek seekTimeUs=%lld us (%.2f secs)",
1548 seekTimeUs,
1549 seekTimeUs / 1E6);
1550
1551 mSource->seekTo(seekTimeUs);
Robert Shihd3b0bbb2014-07-23 15:00:25 -07001552 ++mTimedTextGeneration;
Andreas Hubera1f8ab02012-11-30 10:53:22 -08001553
1554 if (mDriver != NULL) {
1555 sp<NuPlayerDriver> driver = mDriver.promote();
1556 if (driver != NULL) {
1557 driver->notifyPosition(seekTimeUs);
1558 driver->notifySeekComplete();
1559 }
1560 }
1561
1562 // everything's flushed, continue playback.
1563}
1564
1565void NuPlayer::performDecoderFlush() {
1566 ALOGV("performDecoderFlush");
1567
Andreas Huberda9740e2013-04-16 10:54:03 -07001568 if (mAudioDecoder == NULL && mVideoDecoder == NULL) {
Andreas Hubera1f8ab02012-11-30 10:53:22 -08001569 return;
1570 }
1571
1572 mTimeDiscontinuityPending = true;
1573
1574 if (mAudioDecoder != NULL) {
1575 flushDecoder(true /* audio */, false /* needShutdown */);
1576 }
1577
1578 if (mVideoDecoder != NULL) {
1579 flushDecoder(false /* audio */, false /* needShutdown */);
1580 }
1581}
1582
Andreas Huber14f76722013-01-15 09:04:18 -08001583void NuPlayer::performDecoderShutdown(bool audio, bool video) {
1584 ALOGV("performDecoderShutdown audio=%d, video=%d", audio, video);
Andreas Hubera1f8ab02012-11-30 10:53:22 -08001585
Andreas Huber14f76722013-01-15 09:04:18 -08001586 if ((!audio || mAudioDecoder == NULL)
1587 && (!video || mVideoDecoder == NULL)) {
Andreas Hubera1f8ab02012-11-30 10:53:22 -08001588 return;
1589 }
1590
1591 mTimeDiscontinuityPending = true;
1592
Andreas Huber14f76722013-01-15 09:04:18 -08001593 if (mFlushingAudio == NONE && (!audio || mAudioDecoder == NULL)) {
1594 mFlushingAudio = FLUSHED;
1595 }
1596
1597 if (mFlushingVideo == NONE && (!video || mVideoDecoder == NULL)) {
1598 mFlushingVideo = FLUSHED;
1599 }
1600
1601 if (audio && mAudioDecoder != NULL) {
Andreas Hubera1f8ab02012-11-30 10:53:22 -08001602 flushDecoder(true /* audio */, true /* needShutdown */);
1603 }
1604
Andreas Huber14f76722013-01-15 09:04:18 -08001605 if (video && mVideoDecoder != NULL) {
Andreas Hubera1f8ab02012-11-30 10:53:22 -08001606 flushDecoder(false /* audio */, true /* needShutdown */);
1607 }
1608}
1609
1610void NuPlayer::performReset() {
1611 ALOGV("performReset");
1612
1613 CHECK(mAudioDecoder == NULL);
1614 CHECK(mVideoDecoder == NULL);
1615
1616 cancelPollDuration();
1617
1618 ++mScanSourcesGeneration;
1619 mScanSourcesPending = false;
1620
Lajos Molnar09524832014-07-17 14:29:51 -07001621 if (mRendererLooper != NULL) {
1622 if (mRenderer != NULL) {
1623 mRendererLooper->unregisterHandler(mRenderer->id());
1624 }
1625 mRendererLooper->stop();
1626 mRendererLooper.clear();
1627 }
Andreas Hubera1f8ab02012-11-30 10:53:22 -08001628 mRenderer.clear();
1629
1630 if (mSource != NULL) {
1631 mSource->stop();
Andreas Huberb5f25f02013-02-05 10:14:26 -08001632
1633 looper()->unregisterHandler(mSource->id());
1634
Andreas Hubera1f8ab02012-11-30 10:53:22 -08001635 mSource.clear();
1636 }
1637
1638 if (mDriver != NULL) {
1639 sp<NuPlayerDriver> driver = mDriver.promote();
1640 if (driver != NULL) {
1641 driver->notifyResetComplete();
1642 }
1643 }
Andreas Huber57a339c2012-12-03 11:18:00 -08001644
1645 mStarted = false;
Andreas Hubera1f8ab02012-11-30 10:53:22 -08001646}
1647
1648void NuPlayer::performScanSources() {
1649 ALOGV("performScanSources");
1650
Andreas Huber57a339c2012-12-03 11:18:00 -08001651 if (!mStarted) {
1652 return;
1653 }
1654
Andreas Hubera1f8ab02012-11-30 10:53:22 -08001655 if (mAudioDecoder == NULL || mVideoDecoder == NULL) {
1656 postScanSources();
1657 }
1658}
1659
Andreas Huber57a339c2012-12-03 11:18:00 -08001660void NuPlayer::performSetSurface(const sp<NativeWindowWrapper> &wrapper) {
1661 ALOGV("performSetSurface");
1662
1663 mNativeWindow = wrapper;
1664
1665 // XXX - ignore error from setVideoScalingMode for now
1666 setVideoScalingMode(mVideoScalingMode);
1667
1668 if (mDriver != NULL) {
1669 sp<NuPlayerDriver> driver = mDriver.promote();
1670 if (driver != NULL) {
1671 driver->notifySetSurfaceComplete();
1672 }
1673 }
1674}
1675
Andreas Huber9575c962013-02-05 13:59:56 -08001676void NuPlayer::onSourceNotify(const sp<AMessage> &msg) {
1677 int32_t what;
1678 CHECK(msg->findInt32("what", &what));
1679
1680 switch (what) {
1681 case Source::kWhatPrepared:
1682 {
Andreas Huberb5f28d42013-04-25 15:11:19 -07001683 if (mSource == NULL) {
1684 // This is a stale notification from a source that was
1685 // asynchronously preparing when the client called reset().
1686 // We handled the reset, the source is gone.
1687 break;
1688 }
1689
Andreas Huberec0c5972013-02-05 14:47:13 -08001690 int32_t err;
1691 CHECK(msg->findInt32("err", &err));
1692
Andreas Huber9575c962013-02-05 13:59:56 -08001693 sp<NuPlayerDriver> driver = mDriver.promote();
1694 if (driver != NULL) {
Marco Nelissendd114d12014-05-28 15:23:14 -07001695 // notify duration first, so that it's definitely set when
1696 // the app received the "prepare complete" callback.
1697 int64_t durationUs;
1698 if (mSource->getDuration(&durationUs) == OK) {
1699 driver->notifyDuration(durationUs);
1700 }
Andreas Huberec0c5972013-02-05 14:47:13 -08001701 driver->notifyPrepareCompleted(err);
Andreas Huber9575c962013-02-05 13:59:56 -08001702 }
Andreas Huber99759402013-04-01 14:28:31 -07001703
Andreas Huber9575c962013-02-05 13:59:56 -08001704 break;
1705 }
1706
1707 case Source::kWhatFlagsChanged:
1708 {
1709 uint32_t flags;
1710 CHECK(msg->findInt32("flags", (int32_t *)&flags));
1711
Chong Zhang4b7069d2013-09-11 12:52:43 -07001712 sp<NuPlayerDriver> driver = mDriver.promote();
1713 if (driver != NULL) {
1714 driver->notifyFlagsChanged(flags);
1715 }
1716
Andreas Huber9575c962013-02-05 13:59:56 -08001717 if ((mSourceFlags & Source::FLAG_DYNAMIC_DURATION)
1718 && (!(flags & Source::FLAG_DYNAMIC_DURATION))) {
1719 cancelPollDuration();
1720 } else if (!(mSourceFlags & Source::FLAG_DYNAMIC_DURATION)
1721 && (flags & Source::FLAG_DYNAMIC_DURATION)
1722 && (mAudioDecoder != NULL || mVideoDecoder != NULL)) {
1723 schedulePollDuration();
1724 }
1725
1726 mSourceFlags = flags;
1727 break;
1728 }
1729
1730 case Source::kWhatVideoSizeChanged:
1731 {
1732 int32_t width, height;
1733 CHECK(msg->findInt32("width", &width));
1734 CHECK(msg->findInt32("height", &height));
1735
1736 notifyListener(MEDIA_SET_VIDEO_SIZE, width, height);
1737 break;
1738 }
1739
Roger Jönssonb50e83e2013-01-21 16:26:41 +01001740 case Source::kWhatBufferingStart:
1741 {
1742 notifyListener(MEDIA_INFO, MEDIA_INFO_BUFFERING_START, 0);
1743 break;
1744 }
1745
1746 case Source::kWhatBufferingEnd:
1747 {
1748 notifyListener(MEDIA_INFO, MEDIA_INFO_BUFFERING_END, 0);
1749 break;
1750 }
1751
Chong Zhangdcb89b32013-08-06 09:44:47 -07001752 case Source::kWhatSubtitleData:
1753 {
1754 sp<ABuffer> buffer;
1755 CHECK(msg->findBuffer("buffer", &buffer));
1756
Chong Zhang404fced2014-06-11 14:45:31 -07001757 sendSubtitleData(buffer, 0 /* baseIndex */);
Chong Zhangdcb89b32013-08-06 09:44:47 -07001758 break;
1759 }
1760
Robert Shihd3b0bbb2014-07-23 15:00:25 -07001761 case Source::kWhatTimedTextData:
1762 {
1763 int32_t generation;
1764 if (msg->findInt32("generation", &generation)
1765 && generation != mTimedTextGeneration) {
1766 break;
1767 }
1768
1769 sp<ABuffer> buffer;
1770 CHECK(msg->findBuffer("buffer", &buffer));
1771
1772 sp<NuPlayerDriver> driver = mDriver.promote();
1773 if (driver == NULL) {
1774 break;
1775 }
1776
1777 int posMs;
1778 int64_t timeUs, posUs;
1779 driver->getCurrentPosition(&posMs);
1780 posUs = posMs * 1000;
1781 CHECK(buffer->meta()->findInt64("timeUs", &timeUs));
1782
1783 if (posUs < timeUs) {
1784 if (!msg->findInt32("generation", &generation)) {
1785 msg->setInt32("generation", mTimedTextGeneration);
1786 }
1787 msg->post(timeUs - posUs);
1788 } else {
1789 sendTimedTextData(buffer);
1790 }
1791 break;
1792 }
1793
Andreas Huber14f76722013-01-15 09:04:18 -08001794 case Source::kWhatQueueDecoderShutdown:
1795 {
1796 int32_t audio, video;
1797 CHECK(msg->findInt32("audio", &audio));
1798 CHECK(msg->findInt32("video", &video));
1799
1800 sp<AMessage> reply;
1801 CHECK(msg->findMessage("reply", &reply));
1802
1803 queueDecoderShutdown(audio, video, reply);
1804 break;
1805 }
1806
Andreas Huber9575c962013-02-05 13:59:56 -08001807 default:
1808 TRESPASS();
1809 }
1810}
1811
Chong Zhanga7fa1d92014-06-11 14:49:23 -07001812void NuPlayer::onClosedCaptionNotify(const sp<AMessage> &msg) {
1813 int32_t what;
1814 CHECK(msg->findInt32("what", &what));
1815
1816 switch (what) {
1817 case NuPlayer::CCDecoder::kWhatClosedCaptionData:
1818 {
1819 sp<ABuffer> buffer;
1820 CHECK(msg->findBuffer("buffer", &buffer));
1821
1822 size_t inbandTracks = 0;
1823 if (mSource != NULL) {
1824 inbandTracks = mSource->getTrackCount();
1825 }
1826
1827 sendSubtitleData(buffer, inbandTracks);
1828 break;
1829 }
1830
1831 case NuPlayer::CCDecoder::kWhatTrackAdded:
1832 {
1833 notifyListener(MEDIA_INFO, MEDIA_INFO_METADATA_UPDATE, 0);
1834
1835 break;
1836 }
1837
1838 default:
1839 TRESPASS();
1840 }
1841
1842
1843}
1844
Chong Zhang404fced2014-06-11 14:45:31 -07001845void NuPlayer::sendSubtitleData(const sp<ABuffer> &buffer, int32_t baseIndex) {
1846 int32_t trackIndex;
1847 int64_t timeUs, durationUs;
1848 CHECK(buffer->meta()->findInt32("trackIndex", &trackIndex));
1849 CHECK(buffer->meta()->findInt64("timeUs", &timeUs));
1850 CHECK(buffer->meta()->findInt64("durationUs", &durationUs));
1851
1852 Parcel in;
1853 in.writeInt32(trackIndex + baseIndex);
1854 in.writeInt64(timeUs);
1855 in.writeInt64(durationUs);
1856 in.writeInt32(buffer->size());
1857 in.writeInt32(buffer->size());
1858 in.write(buffer->data(), buffer->size());
1859
1860 notifyListener(MEDIA_SUBTITLE_DATA, 0, 0, &in);
1861}
Robert Shihd3b0bbb2014-07-23 15:00:25 -07001862
1863void NuPlayer::sendTimedTextData(const sp<ABuffer> &buffer) {
1864 const void *data;
1865 size_t size = 0;
1866 int64_t timeUs;
1867 int32_t flag = TextDescriptions::LOCAL_DESCRIPTIONS;
1868
1869 AString mime;
1870 CHECK(buffer->meta()->findString("mime", &mime));
1871 CHECK(strcasecmp(mime.c_str(), MEDIA_MIMETYPE_TEXT_3GPP) == 0);
1872
1873 data = buffer->data();
1874 size = buffer->size();
1875
1876 Parcel parcel;
1877 if (size > 0) {
1878 CHECK(buffer->meta()->findInt64("timeUs", &timeUs));
1879 flag |= TextDescriptions::IN_BAND_TEXT_3GPP;
1880 TextDescriptions::getParcelOfDescriptions(
1881 (const uint8_t *)data, size, flag, timeUs / 1000, &parcel);
1882 }
1883
1884 if ((parcel.dataSize() > 0)) {
1885 notifyListener(MEDIA_TIMED_TEXT, 0, 0, &parcel);
1886 } else { // send an empty timed text
1887 notifyListener(MEDIA_TIMED_TEXT, 0, 0);
1888 }
1889}
Andreas Huberb5f25f02013-02-05 10:14:26 -08001890////////////////////////////////////////////////////////////////////////////////
1891
Andreas Huber9575c962013-02-05 13:59:56 -08001892void NuPlayer::Source::notifyFlagsChanged(uint32_t flags) {
1893 sp<AMessage> notify = dupNotify();
1894 notify->setInt32("what", kWhatFlagsChanged);
1895 notify->setInt32("flags", flags);
1896 notify->post();
1897}
1898
1899void NuPlayer::Source::notifyVideoSizeChanged(int32_t width, int32_t height) {
1900 sp<AMessage> notify = dupNotify();
1901 notify->setInt32("what", kWhatVideoSizeChanged);
1902 notify->setInt32("width", width);
1903 notify->setInt32("height", height);
1904 notify->post();
1905}
1906
Andreas Huberec0c5972013-02-05 14:47:13 -08001907void NuPlayer::Source::notifyPrepared(status_t err) {
Andreas Huber9575c962013-02-05 13:59:56 -08001908 sp<AMessage> notify = dupNotify();
1909 notify->setInt32("what", kWhatPrepared);
Andreas Huberec0c5972013-02-05 14:47:13 -08001910 notify->setInt32("err", err);
Andreas Huber9575c962013-02-05 13:59:56 -08001911 notify->post();
1912}
1913
Andreas Huber84333e02014-02-07 15:36:10 -08001914void NuPlayer::Source::onMessageReceived(const sp<AMessage> & /* msg */) {
Andreas Huberb5f25f02013-02-05 10:14:26 -08001915 TRESPASS();
1916}
1917
Andreas Huber14f76722013-01-15 09:04:18 -08001918void NuPlayer::queueDecoderShutdown(
1919 bool audio, bool video, const sp<AMessage> &reply) {
1920 ALOGI("queueDecoderShutdown audio=%d, video=%d", audio, video);
1921
1922 mDeferredActions.push_back(
1923 new ShutdownDecoderAction(audio, video));
1924
1925 mDeferredActions.push_back(
1926 new SimpleAction(&NuPlayer::performScanSources));
1927
1928 mDeferredActions.push_back(new PostMessageAction(reply));
1929
1930 processDeferredActions();
1931}
1932
Andreas Huberf9334412010-12-15 15:17:42 -08001933} // namespace android