blob: 29f57d6dd70a0ed8ed355bc4cf09575d01d4fcd6 [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) {
Andreas Huber5bc087c2010-12-23 10:27:40 -0800210 sp<AMessage> msg = new AMessage(kWhatSetDataSource, id());
Oscar Rydhé7a33b772012-02-20 10:15:48 +0100211 size_t len = strlen(url);
Andreas Huber5bc087c2010-12-23 10:27:40 -0800212
Andreas Huberb5f25f02013-02-05 10:14:26 -0800213 sp<AMessage> notify = new AMessage(kWhatSourceNotify, id());
214
Andreas Huberafed0e12011-09-20 15:39:58 -0700215 sp<Source> source;
216 if (IsHTTPLiveURL(url)) {
Andreas Huber81e68442014-02-05 11:52:33 -0800217 source = new HTTPLiveSource(notify, httpService, url, headers);
Andreas Huberafed0e12011-09-20 15:39:58 -0700218 } else if (!strncasecmp(url, "rtsp://", 7)) {
Andreas Huber1b86fe02014-01-29 11:13:26 -0800219 source = new RTSPSource(
220 notify, httpService, url, headers, mUIDValid, mUID);
Oscar Rydhé7a33b772012-02-20 10:15:48 +0100221 } else if ((!strncasecmp(url, "http://", 7)
222 || !strncasecmp(url, "https://", 8))
223 && ((len >= 4 && !strcasecmp(".sdp", &url[len - 4]))
224 || strstr(url, ".sdp?"))) {
Andreas Huber1b86fe02014-01-29 11:13:26 -0800225 source = new RTSPSource(
226 notify, httpService, url, headers, mUIDValid, mUID, true);
Lajos Molnar09524832014-07-17 14:29:51 -0700227 } else if ((!strncasecmp(url, "widevine://", 11))) {
228 source = new GenericSource(notify, httpService, url, headers,
229 true /* isWidevine */, mUIDValid, mUID);
230 mSourceFlags |= Source::FLAG_SECURE;
Andreas Huber2bfdd422011-10-11 15:24:07 -0700231 } else {
Andreas Huber81e68442014-02-05 11:52:33 -0800232 source = new GenericSource(notify, httpService, url, headers);
Andreas Huber2bfdd422011-10-11 15:24:07 -0700233 }
234
Andreas Huberafed0e12011-09-20 15:39:58 -0700235 msg->setObject("source", source);
236 msg->post();
237}
238
Andreas Huber9575c962013-02-05 13:59:56 -0800239void NuPlayer::setDataSourceAsync(int fd, int64_t offset, int64_t length) {
Andreas Huberafed0e12011-09-20 15:39:58 -0700240 sp<AMessage> msg = new AMessage(kWhatSetDataSource, id());
241
Andreas Huberb5f25f02013-02-05 10:14:26 -0800242 sp<AMessage> notify = new AMessage(kWhatSourceNotify, id());
243
244 sp<Source> source = new GenericSource(notify, fd, offset, length);
Andreas Huberafed0e12011-09-20 15:39:58 -0700245 msg->setObject("source", source);
Andreas Huberf9334412010-12-15 15:17:42 -0800246 msg->post();
247}
248
Andreas Huber9575c962013-02-05 13:59:56 -0800249void NuPlayer::prepareAsync() {
250 (new AMessage(kWhatPrepare, id()))->post();
251}
252
Andreas Huber57a339c2012-12-03 11:18:00 -0800253void NuPlayer::setVideoSurfaceTextureAsync(
Andy McFadden8ba01022012-12-18 09:46:54 -0800254 const sp<IGraphicBufferProducer> &bufferProducer) {
Glenn Kasten11731182011-02-08 17:26:17 -0800255 sp<AMessage> msg = new AMessage(kWhatSetVideoNativeWindow, id());
Andreas Huber57a339c2012-12-03 11:18:00 -0800256
Andy McFadden8ba01022012-12-18 09:46:54 -0800257 if (bufferProducer == NULL) {
Andreas Huber57a339c2012-12-03 11:18:00 -0800258 msg->setObject("native-window", NULL);
259 } else {
260 msg->setObject(
261 "native-window",
262 new NativeWindowWrapper(
Mathias Agopian1a2952a2013-02-14 17:11:27 -0800263 new Surface(bufferProducer)));
Andreas Huber57a339c2012-12-03 11:18:00 -0800264 }
265
Andreas Huberf9334412010-12-15 15:17:42 -0800266 msg->post();
267}
268
269void NuPlayer::setAudioSink(const sp<MediaPlayerBase::AudioSink> &sink) {
270 sp<AMessage> msg = new AMessage(kWhatSetAudioSink, id());
271 msg->setObject("sink", sink);
272 msg->post();
273}
274
275void NuPlayer::start() {
276 (new AMessage(kWhatStart, id()))->post();
277}
278
Andreas Huber43c3e6c2011-01-05 12:17:08 -0800279void NuPlayer::pause() {
Andreas Huberb4082222011-01-20 15:23:04 -0800280 (new AMessage(kWhatPause, id()))->post();
Andreas Huber43c3e6c2011-01-05 12:17:08 -0800281}
282
283void NuPlayer::resume() {
Andreas Huberb4082222011-01-20 15:23:04 -0800284 (new AMessage(kWhatResume, id()))->post();
Andreas Huber43c3e6c2011-01-05 12:17:08 -0800285}
286
Andreas Huber1aef2112011-01-04 14:01:29 -0800287void NuPlayer::resetAsync() {
288 (new AMessage(kWhatReset, id()))->post();
289}
290
Andreas Huber43c3e6c2011-01-05 12:17:08 -0800291void NuPlayer::seekToAsync(int64_t seekTimeUs) {
292 sp<AMessage> msg = new AMessage(kWhatSeek, id());
293 msg->setInt64("seekTimeUs", seekTimeUs);
294 msg->post();
295}
296
Andreas Huber53df1a42010-12-22 10:03:04 -0800297// static
Andreas Huber1aef2112011-01-04 14:01:29 -0800298bool NuPlayer::IsFlushingState(FlushStatus state, bool *needShutdown) {
Andreas Huber53df1a42010-12-22 10:03:04 -0800299 switch (state) {
300 case FLUSHING_DECODER:
Andreas Huber1aef2112011-01-04 14:01:29 -0800301 if (needShutdown != NULL) {
302 *needShutdown = false;
Andreas Huber53df1a42010-12-22 10:03:04 -0800303 }
304 return true;
305
Andreas Huber1aef2112011-01-04 14:01:29 -0800306 case FLUSHING_DECODER_SHUTDOWN:
307 if (needShutdown != NULL) {
308 *needShutdown = true;
Andreas Huber53df1a42010-12-22 10:03:04 -0800309 }
310 return true;
311
312 default:
313 return false;
314 }
315}
316
Chong Zhang404fced2014-06-11 14:45:31 -0700317void NuPlayer::writeTrackInfo(
318 Parcel* reply, const sp<AMessage> format) const {
319 int32_t trackType;
320 CHECK(format->findInt32("type", &trackType));
321
322 AString lang;
323 CHECK(format->findString("language", &lang));
324
325 reply->writeInt32(2); // write something non-zero
326 reply->writeInt32(trackType);
327 reply->writeString16(String16(lang.c_str()));
328
329 if (trackType == MEDIA_TRACK_TYPE_SUBTITLE) {
330 AString mime;
331 CHECK(format->findString("mime", &mime));
332
333 int32_t isAuto, isDefault, isForced;
334 CHECK(format->findInt32("auto", &isAuto));
335 CHECK(format->findInt32("default", &isDefault));
336 CHECK(format->findInt32("forced", &isForced));
337
338 reply->writeString16(String16(mime.c_str()));
339 reply->writeInt32(isAuto);
340 reply->writeInt32(isDefault);
341 reply->writeInt32(isForced);
342 }
343}
344
Andreas Huberf9334412010-12-15 15:17:42 -0800345void NuPlayer::onMessageReceived(const sp<AMessage> &msg) {
346 switch (msg->what()) {
347 case kWhatSetDataSource:
348 {
Steve Block3856b092011-10-20 11:56:00 +0100349 ALOGV("kWhatSetDataSource");
Andreas Huberf9334412010-12-15 15:17:42 -0800350
351 CHECK(mSource == NULL);
352
Andreas Huber5bc087c2010-12-23 10:27:40 -0800353 sp<RefBase> obj;
354 CHECK(msg->findObject("source", &obj));
Andreas Huberf9334412010-12-15 15:17:42 -0800355
Andreas Huber5bc087c2010-12-23 10:27:40 -0800356 mSource = static_cast<Source *>(obj.get());
Andreas Huberb5f25f02013-02-05 10:14:26 -0800357
358 looper()->registerHandler(mSource);
Andreas Huber9575c962013-02-05 13:59:56 -0800359
360 CHECK(mDriver != NULL);
361 sp<NuPlayerDriver> driver = mDriver.promote();
362 if (driver != NULL) {
363 driver->notifySetDataSourceCompleted(OK);
364 }
365 break;
366 }
367
368 case kWhatPrepare:
369 {
370 mSource->prepareAsync();
Andreas Huberf9334412010-12-15 15:17:42 -0800371 break;
372 }
373
Chong Zhangdcb89b32013-08-06 09:44:47 -0700374 case kWhatGetTrackInfo:
375 {
376 uint32_t replyID;
377 CHECK(msg->senderAwaitsResponse(&replyID));
378
Chong Zhang404fced2014-06-11 14:45:31 -0700379 Parcel* reply;
380 CHECK(msg->findPointer("reply", (void**)&reply));
381
382 size_t inbandTracks = 0;
Chong Zhangdcb89b32013-08-06 09:44:47 -0700383 if (mSource != NULL) {
Chong Zhang404fced2014-06-11 14:45:31 -0700384 inbandTracks = mSource->getTrackCount();
385 }
386
Chong Zhanga7fa1d92014-06-11 14:49:23 -0700387 size_t ccTracks = 0;
388 if (mCCDecoder != NULL) {
389 ccTracks = mCCDecoder->getTrackCount();
390 }
391
Chong Zhang404fced2014-06-11 14:45:31 -0700392 // total track count
Chong Zhanga7fa1d92014-06-11 14:49:23 -0700393 reply->writeInt32(inbandTracks + ccTracks);
Chong Zhang404fced2014-06-11 14:45:31 -0700394
395 // write inband tracks
396 for (size_t i = 0; i < inbandTracks; ++i) {
397 writeTrackInfo(reply, mSource->getTrackInfo(i));
Chong Zhangdcb89b32013-08-06 09:44:47 -0700398 }
399
Chong Zhanga7fa1d92014-06-11 14:49:23 -0700400 // write CC track
401 for (size_t i = 0; i < ccTracks; ++i) {
402 writeTrackInfo(reply, mCCDecoder->getTrackInfo(i));
403 }
404
Chong Zhangdcb89b32013-08-06 09:44:47 -0700405 sp<AMessage> response = new AMessage;
Chong Zhangdcb89b32013-08-06 09:44:47 -0700406 response->postReply(replyID);
407 break;
408 }
409
410 case kWhatSelectTrack:
411 {
412 uint32_t replyID;
413 CHECK(msg->senderAwaitsResponse(&replyID));
414
Chong Zhang404fced2014-06-11 14:45:31 -0700415 size_t trackIndex;
416 int32_t select;
417 CHECK(msg->findSize("trackIndex", &trackIndex));
418 CHECK(msg->findInt32("select", &select));
419
Chong Zhangdcb89b32013-08-06 09:44:47 -0700420 status_t err = INVALID_OPERATION;
Chong Zhang404fced2014-06-11 14:45:31 -0700421
422 size_t inbandTracks = 0;
Chong Zhangdcb89b32013-08-06 09:44:47 -0700423 if (mSource != NULL) {
Chong Zhang404fced2014-06-11 14:45:31 -0700424 inbandTracks = mSource->getTrackCount();
425 }
Chong Zhanga7fa1d92014-06-11 14:49:23 -0700426 size_t ccTracks = 0;
427 if (mCCDecoder != NULL) {
428 ccTracks = mCCDecoder->getTrackCount();
429 }
Chong Zhang404fced2014-06-11 14:45:31 -0700430
431 if (trackIndex < inbandTracks) {
Chong Zhangdcb89b32013-08-06 09:44:47 -0700432 err = mSource->selectTrack(trackIndex, select);
Robert Shihd3b0bbb2014-07-23 15:00:25 -0700433
434 if (!select && err == OK) {
435 int32_t type;
436 sp<AMessage> info = mSource->getTrackInfo(trackIndex);
437 if (info != NULL
438 && info->findInt32("type", &type)
439 && type == MEDIA_TRACK_TYPE_TIMEDTEXT) {
440 ++mTimedTextGeneration;
441 }
442 }
Chong Zhanga7fa1d92014-06-11 14:49:23 -0700443 } else {
444 trackIndex -= inbandTracks;
445
446 if (trackIndex < ccTracks) {
447 err = mCCDecoder->selectTrack(trackIndex, select);
448 }
Chong Zhangdcb89b32013-08-06 09:44:47 -0700449 }
450
451 sp<AMessage> response = new AMessage;
452 response->setInt32("err", err);
453
454 response->postReply(replyID);
455 break;
456 }
457
Andreas Huberb7c8e912012-11-27 15:02:53 -0800458 case kWhatPollDuration:
459 {
460 int32_t generation;
461 CHECK(msg->findInt32("generation", &generation));
462
463 if (generation != mPollDurationGeneration) {
464 // stale
465 break;
466 }
467
468 int64_t durationUs;
469 if (mDriver != NULL && mSource->getDuration(&durationUs) == OK) {
470 sp<NuPlayerDriver> driver = mDriver.promote();
471 if (driver != NULL) {
472 driver->notifyDuration(durationUs);
473 }
474 }
475
476 msg->post(1000000ll); // poll again in a second.
477 break;
478 }
479
Glenn Kasten11731182011-02-08 17:26:17 -0800480 case kWhatSetVideoNativeWindow:
Andreas Huberf9334412010-12-15 15:17:42 -0800481 {
Steve Block3856b092011-10-20 11:56:00 +0100482 ALOGV("kWhatSetVideoNativeWindow");
Andreas Huberf9334412010-12-15 15:17:42 -0800483
Andreas Huber57a339c2012-12-03 11:18:00 -0800484 mDeferredActions.push_back(
Andreas Huber14f76722013-01-15 09:04:18 -0800485 new ShutdownDecoderAction(
486 false /* audio */, true /* video */));
Andreas Huber57a339c2012-12-03 11:18:00 -0800487
Andreas Huberf9334412010-12-15 15:17:42 -0800488 sp<RefBase> obj;
Glenn Kasten11731182011-02-08 17:26:17 -0800489 CHECK(msg->findObject("native-window", &obj));
Andreas Huberf9334412010-12-15 15:17:42 -0800490
Andreas Huber57a339c2012-12-03 11:18:00 -0800491 mDeferredActions.push_back(
492 new SetSurfaceAction(
493 static_cast<NativeWindowWrapper *>(obj.get())));
James Dong0d268a32012-08-31 12:18:27 -0700494
Andreas Huber57a339c2012-12-03 11:18:00 -0800495 if (obj != NULL) {
496 // If there is a new surface texture, instantiate decoders
497 // again if possible.
498 mDeferredActions.push_back(
499 new SimpleAction(&NuPlayer::performScanSources));
500 }
501
502 processDeferredActions();
Andreas Huberf9334412010-12-15 15:17:42 -0800503 break;
504 }
505
506 case kWhatSetAudioSink:
507 {
Steve Block3856b092011-10-20 11:56:00 +0100508 ALOGV("kWhatSetAudioSink");
Andreas Huberf9334412010-12-15 15:17:42 -0800509
510 sp<RefBase> obj;
511 CHECK(msg->findObject("sink", &obj));
512
513 mAudioSink = static_cast<MediaPlayerBase::AudioSink *>(obj.get());
514 break;
515 }
516
517 case kWhatStart:
518 {
Steve Block3856b092011-10-20 11:56:00 +0100519 ALOGV("kWhatStart");
Andreas Huber43c3e6c2011-01-05 12:17:08 -0800520
Andreas Huber3fe62152011-09-16 15:09:22 -0700521 mVideoIsAVC = false;
Wei Jiabc2fb722014-07-08 16:37:57 -0700522 mOffloadAudio = false;
Andreas Huber1aef2112011-01-04 14:01:29 -0800523 mAudioEOS = false;
524 mVideoEOS = false;
Andreas Huber32f3cef2011-03-02 15:34:46 -0800525 mSkipRenderingAudioUntilMediaTimeUs = -1;
526 mSkipRenderingVideoUntilMediaTimeUs = -1;
Andreas Huber3fe62152011-09-16 15:09:22 -0700527 mVideoLateByUs = 0;
528 mNumFramesTotal = 0;
529 mNumFramesDropped = 0;
Andreas Huber57a339c2012-12-03 11:18:00 -0800530 mStarted = true;
Andreas Huber1aef2112011-01-04 14:01:29 -0800531
Lajos Molnar09524832014-07-17 14:29:51 -0700532 /* instantiate decoders now for secure playback */
533 if (mSourceFlags & Source::FLAG_SECURE) {
534 if (mNativeWindow != NULL) {
535 instantiateDecoder(false, &mVideoDecoder);
536 }
537
538 if (mAudioSink != NULL) {
539 instantiateDecoder(true, &mAudioDecoder);
540 }
541 }
542
Andreas Huber5bc087c2010-12-23 10:27:40 -0800543 mSource->start();
Andreas Huberf9334412010-12-15 15:17:42 -0800544
Andreas Huberd5e56232013-03-12 11:01:43 -0700545 uint32_t flags = 0;
546
547 if (mSource->isRealTime()) {
548 flags |= Renderer::FLAG_REAL_TIME;
549 }
550
Wei Jiabc2fb722014-07-08 16:37:57 -0700551 sp<MetaData> audioMeta = mSource->getFormatMeta(true /* audio */);
552 audio_stream_type_t streamType = AUDIO_STREAM_MUSIC;
553 if (mAudioSink != NULL) {
554 streamType = mAudioSink->getAudioStreamType();
555 }
556
557 sp<AMessage> videoFormat = mSource->getFormat(false /* audio */);
558
559 mOffloadAudio =
560 canOffloadStream(audioMeta, (videoFormat != NULL),
561 true /* is_streaming */, streamType);
562 if (mOffloadAudio) {
563 flags |= Renderer::FLAG_OFFLOAD_AUDIO;
564 }
565
Andreas Huberf9334412010-12-15 15:17:42 -0800566 mRenderer = new Renderer(
567 mAudioSink,
Andreas Huberd5e56232013-03-12 11:01:43 -0700568 new AMessage(kWhatRendererNotify, id()),
569 flags);
Andreas Huberf9334412010-12-15 15:17:42 -0800570
Lajos Molnar09524832014-07-17 14:29:51 -0700571 mRendererLooper = new ALooper;
572 mRendererLooper->setName("NuPlayerRenderer");
573 mRendererLooper->start(false, false, ANDROID_PRIORITY_AUDIO);
574 mRendererLooper->registerHandler(mRenderer);
Andreas Huberf9334412010-12-15 15:17:42 -0800575
Andreas Huber1aef2112011-01-04 14:01:29 -0800576 postScanSources();
Andreas Huberf9334412010-12-15 15:17:42 -0800577 break;
578 }
579
580 case kWhatScanSources:
581 {
Andreas Huber1aef2112011-01-04 14:01:29 -0800582 int32_t generation;
583 CHECK(msg->findInt32("generation", &generation));
584 if (generation != mScanSourcesGeneration) {
585 // Drop obsolete msg.
586 break;
587 }
588
Andreas Huber5bc087c2010-12-23 10:27:40 -0800589 mScanSourcesPending = false;
590
Steve Block3856b092011-10-20 11:56:00 +0100591 ALOGV("scanning sources haveAudio=%d, haveVideo=%d",
Andreas Huber43c3e6c2011-01-05 12:17:08 -0800592 mAudioDecoder != NULL, mVideoDecoder != NULL);
593
Andreas Huberb7c8e912012-11-27 15:02:53 -0800594 bool mHadAnySourcesBefore =
595 (mAudioDecoder != NULL) || (mVideoDecoder != NULL);
596
Haynes Mathew George5d246ef2012-07-09 10:36:57 -0700597 if (mNativeWindow != NULL) {
598 instantiateDecoder(false, &mVideoDecoder);
599 }
Andreas Huberf9334412010-12-15 15:17:42 -0800600
601 if (mAudioSink != NULL) {
Andreas Huber5bc087c2010-12-23 10:27:40 -0800602 instantiateDecoder(true, &mAudioDecoder);
Andreas Huberf9334412010-12-15 15:17:42 -0800603 }
604
Andreas Huberb7c8e912012-11-27 15:02:53 -0800605 if (!mHadAnySourcesBefore
606 && (mAudioDecoder != NULL || mVideoDecoder != NULL)) {
607 // This is the first time we've found anything playable.
608
Andreas Huber9575c962013-02-05 13:59:56 -0800609 if (mSourceFlags & Source::FLAG_DYNAMIC_DURATION) {
Andreas Huberb7c8e912012-11-27 15:02:53 -0800610 schedulePollDuration();
611 }
612 }
613
Andreas Hubereac68ba2011-09-27 12:12:25 -0700614 status_t err;
615 if ((err = mSource->feedMoreTSData()) != OK) {
Andreas Huber1aef2112011-01-04 14:01:29 -0800616 if (mAudioDecoder == NULL && mVideoDecoder == NULL) {
617 // We're not currently decoding anything (no audio or
618 // video tracks found) and we just ran out of input data.
Andreas Hubereac68ba2011-09-27 12:12:25 -0700619
620 if (err == ERROR_END_OF_STREAM) {
621 notifyListener(MEDIA_PLAYBACK_COMPLETE, 0, 0);
622 } else {
623 notifyListener(MEDIA_ERROR, MEDIA_ERROR_UNKNOWN, err);
624 }
Andreas Huber1aef2112011-01-04 14:01:29 -0800625 }
Andreas Huberf9334412010-12-15 15:17:42 -0800626 break;
627 }
628
Andreas Huberfbe9d812012-08-31 14:05:27 -0700629 if ((mAudioDecoder == NULL && mAudioSink != NULL)
630 || (mVideoDecoder == NULL && mNativeWindow != NULL)) {
Andreas Huberf9334412010-12-15 15:17:42 -0800631 msg->post(100000ll);
Andreas Huber5bc087c2010-12-23 10:27:40 -0800632 mScanSourcesPending = true;
Andreas Huberf9334412010-12-15 15:17:42 -0800633 }
634 break;
635 }
636
637 case kWhatVideoNotify:
638 case kWhatAudioNotify:
639 {
640 bool audio = msg->what() == kWhatAudioNotify;
641
Andreas Huberf9334412010-12-15 15:17:42 -0800642 int32_t what;
Lajos Molnar1cd13982014-01-17 15:12:51 -0800643 CHECK(msg->findInt32("what", &what));
Andreas Huberf9334412010-12-15 15:17:42 -0800644
Lajos Molnar1cd13982014-01-17 15:12:51 -0800645 if (what == Decoder::kWhatFillThisBuffer) {
Andreas Huberf9334412010-12-15 15:17:42 -0800646 status_t err = feedDecoderInputData(
Lajos Molnar1cd13982014-01-17 15:12:51 -0800647 audio, msg);
Andreas Huberf9334412010-12-15 15:17:42 -0800648
Andreas Huber5bc087c2010-12-23 10:27:40 -0800649 if (err == -EWOULDBLOCK) {
Andreas Hubereac68ba2011-09-27 12:12:25 -0700650 if (mSource->feedMoreTSData() == OK) {
Andreas Huber1183a4a2011-11-03 11:00:21 -0700651 msg->post(10000ll);
Andreas Huber5bc087c2010-12-23 10:27:40 -0800652 }
Andreas Huberf9334412010-12-15 15:17:42 -0800653 }
Lajos Molnar1cd13982014-01-17 15:12:51 -0800654 } else if (what == Decoder::kWhatEOS) {
Andreas Huberdc9bacd2011-09-26 10:53:29 -0700655 int32_t err;
Lajos Molnar1cd13982014-01-17 15:12:51 -0800656 CHECK(msg->findInt32("err", &err));
Andreas Huberdc9bacd2011-09-26 10:53:29 -0700657
658 if (err == ERROR_END_OF_STREAM) {
Steve Block3856b092011-10-20 11:56:00 +0100659 ALOGV("got %s decoder EOS", audio ? "audio" : "video");
Andreas Huberdc9bacd2011-09-26 10:53:29 -0700660 } else {
Steve Block3856b092011-10-20 11:56:00 +0100661 ALOGV("got %s decoder EOS w/ error %d",
Andreas Huberdc9bacd2011-09-26 10:53:29 -0700662 audio ? "audio" : "video",
663 err);
664 }
665
666 mRenderer->queueEOS(audio, err);
Lajos Molnar1cd13982014-01-17 15:12:51 -0800667 } else if (what == Decoder::kWhatFlushCompleted) {
Andreas Huber1aef2112011-01-04 14:01:29 -0800668 bool needShutdown;
Andreas Huber53df1a42010-12-22 10:03:04 -0800669
Andreas Huberf9334412010-12-15 15:17:42 -0800670 if (audio) {
Andreas Huber1aef2112011-01-04 14:01:29 -0800671 CHECK(IsFlushingState(mFlushingAudio, &needShutdown));
Andreas Huberf9334412010-12-15 15:17:42 -0800672 mFlushingAudio = FLUSHED;
673 } else {
Andreas Huber1aef2112011-01-04 14:01:29 -0800674 CHECK(IsFlushingState(mFlushingVideo, &needShutdown));
Andreas Huberf9334412010-12-15 15:17:42 -0800675 mFlushingVideo = FLUSHED;
Andreas Huber3fe62152011-09-16 15:09:22 -0700676
677 mVideoLateByUs = 0;
Andreas Huberf9334412010-12-15 15:17:42 -0800678 }
679
Steve Block3856b092011-10-20 11:56:00 +0100680 ALOGV("decoder %s flush completed", audio ? "audio" : "video");
Andreas Huberf9334412010-12-15 15:17:42 -0800681
Andreas Huber1aef2112011-01-04 14:01:29 -0800682 if (needShutdown) {
Steve Block3856b092011-10-20 11:56:00 +0100683 ALOGV("initiating %s decoder shutdown",
Andreas Huber53df1a42010-12-22 10:03:04 -0800684 audio ? "audio" : "video");
Andreas Huberf9334412010-12-15 15:17:42 -0800685
Andreas Huber53df1a42010-12-22 10:03:04 -0800686 (audio ? mAudioDecoder : mVideoDecoder)->initiateShutdown();
Andreas Huberf9334412010-12-15 15:17:42 -0800687
Andreas Huber53df1a42010-12-22 10:03:04 -0800688 if (audio) {
689 mFlushingAudio = SHUTTING_DOWN_DECODER;
690 } else {
691 mFlushingVideo = SHUTTING_DOWN_DECODER;
692 }
Andreas Huberf9334412010-12-15 15:17:42 -0800693 }
Andreas Huber3831a062010-12-21 10:22:33 -0800694
695 finishFlushIfPossible();
Lajos Molnar1cd13982014-01-17 15:12:51 -0800696 } else if (what == Decoder::kWhatOutputFormatChanged) {
697 sp<AMessage> format;
698 CHECK(msg->findMessage("format", &format));
699
Andreas Huber31e25082011-01-10 10:38:31 -0800700 if (audio) {
701 int32_t numChannels;
Lajos Molnar1cd13982014-01-17 15:12:51 -0800702 CHECK(format->findInt32(
Andreas Huber516dacf2012-12-03 15:20:40 -0800703 "channel-count", &numChannels));
Andreas Huber2c2814b2010-12-15 17:18:20 -0800704
Andreas Huber31e25082011-01-10 10:38:31 -0800705 int32_t sampleRate;
Lajos Molnar1cd13982014-01-17 15:12:51 -0800706 CHECK(format->findInt32("sample-rate", &sampleRate));
Andreas Huber2c2814b2010-12-15 17:18:20 -0800707
Steve Block3856b092011-10-20 11:56:00 +0100708 ALOGV("Audio output format changed to %d Hz, %d channels",
Andreas Huber31e25082011-01-10 10:38:31 -0800709 sampleRate, numChannels);
Andreas Huber2c2814b2010-12-15 17:18:20 -0800710
Andreas Huber31e25082011-01-10 10:38:31 -0800711 mAudioSink->close();
Eric Laurent1948eb32012-04-13 16:50:19 -0700712
Wei Jiabc2fb722014-07-08 16:37:57 -0700713 uint32_t flags;
Eric Laurent1948eb32012-04-13 16:50:19 -0700714 int64_t durationUs;
Andreas Huber516dacf2012-12-03 15:20:40 -0800715 // FIXME: we should handle the case where the video decoder
716 // is created after we receive the format change indication.
717 // Current code will just make that we select deep buffer
718 // with video which should not be a problem as it should
Eric Laurent1948eb32012-04-13 16:50:19 -0700719 // not prevent from keeping A/V sync.
720 if (mVideoDecoder == NULL &&
721 mSource->getDuration(&durationUs) == OK &&
Andreas Huber516dacf2012-12-03 15:20:40 -0800722 durationUs
723 > AUDIO_SINK_MIN_DEEP_BUFFER_DURATION_US) {
Eric Laurent1948eb32012-04-13 16:50:19 -0700724 flags = AUDIO_OUTPUT_FLAG_DEEP_BUFFER;
725 } else {
726 flags = AUDIO_OUTPUT_FLAG_NONE;
727 }
728
Andreas Huber98065552012-05-03 11:33:01 -0700729 int32_t channelMask;
Lajos Molnar1cd13982014-01-17 15:12:51 -0800730 if (!format->findInt32("channel-mask", &channelMask)) {
Andreas Huber98065552012-05-03 11:33:01 -0700731 channelMask = CHANNEL_MASK_USE_CHANNEL_ORDER;
732 }
733
Wei Jiabc2fb722014-07-08 16:37:57 -0700734 if (mOffloadAudio) {
735 audio_format_t audioFormat = AUDIO_FORMAT_PCM_16_BIT;
736 audio_offload_info_t offloadInfo =
737 AUDIO_INFO_INITIALIZER;
738
739 AString mime;
740 CHECK(format->findString("mime", &mime));
741
742 status_t err =
743 mapMimeToAudioFormat(audioFormat, mime.c_str());
744 if (err != OK) {
745 ALOGE("Couldn't map mime \"%s\" to a valid "
746 "audio_format", mime.c_str());
747 mOffloadAudio = false;
748 } else {
749 ALOGV("Mime \"%s\" mapped to audio_format 0x%x",
750 mime.c_str(), audioFormat);
751
aarti jadhav-gaikwadccad7862014-08-02 16:21:32 +0530752 int32_t aacProfile = -1;
753 if (audioFormat == AUDIO_FORMAT_AAC
754 && format->findInt32("aac-profile", &aacProfile)) {
755 // Redefine AAC format as per aac profile
756 mapAACProfileToAudioFormat(
757 audioFormat,
758 aacProfile);
759 }
760
Wei Jiabc2fb722014-07-08 16:37:57 -0700761 flags |= AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD;
762
763 offloadInfo.duration_us = -1;
764 format->findInt64(
765 "durationUs", &offloadInfo.duration_us);
766
767 int avgBitRate = -1;
768 format->findInt32("bit-rate", &avgBitRate);
769
770 offloadInfo.sample_rate = sampleRate;
771 offloadInfo.channel_mask = channelMask;
772 offloadInfo.format = audioFormat;
773 offloadInfo.stream_type = AUDIO_STREAM_MUSIC;
774 offloadInfo.bit_rate = avgBitRate;
775 offloadInfo.has_video = (mVideoDecoder != NULL);
776 offloadInfo.is_streaming = true;
777
Wei Jia3a2956d2014-07-22 16:01:33 -0700778 ALOGV("try to open AudioSink in offload mode");
Wei Jiabc2fb722014-07-08 16:37:57 -0700779 err = mAudioSink->open(
780 sampleRate,
781 numChannels,
782 (audio_channel_mask_t)channelMask,
783 audioFormat,
784 8 /* bufferCount */,
785 &NuPlayer::Renderer::AudioSinkCallback,
786 mRenderer.get(),
787 (audio_output_flags_t)flags,
788 &offloadInfo);
789
790 if (err == OK) {
791 // If the playback is offloaded to h/w, we pass
792 // the HAL some metadata information.
793 // We don't want to do this for PCM because it
794 // will be going through the AudioFlinger mixer
795 // before reaching the hardware.
796 sp<MetaData> audioMeta =
797 mSource->getFormatMeta(true /* audio */);
798 sendMetaDataToHal(mAudioSink, audioMeta);
799
800 err = mAudioSink->start();
801 }
802 }
803
804 if (err != OK) {
805 // Clean up, fall back to non offload mode.
806 mAudioSink->close();
807 mAudioDecoder.clear();
808 mRenderer->signalDisableOffloadAudio();
809 mOffloadAudio = false;
810
811 instantiateDecoder(
812 true /* audio */, &mAudioDecoder);
813 }
814 }
815
816 if (!mOffloadAudio) {
817 flags &= ~AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD;
Wei Jia3a2956d2014-07-22 16:01:33 -0700818 ALOGV("open AudioSink in NON-offload mode");
Wei Jiabc2fb722014-07-08 16:37:57 -0700819 CHECK_EQ(mAudioSink->open(
820 sampleRate,
821 numChannels,
822 (audio_channel_mask_t)channelMask,
823 AUDIO_FORMAT_PCM_16_BIT,
824 8 /* bufferCount */,
825 NULL,
826 NULL,
827 (audio_output_flags_t)flags),
828 (status_t)OK);
829 mAudioSink->start();
830 }
Andreas Huber2c2814b2010-12-15 17:18:20 -0800831
Andreas Huber31e25082011-01-10 10:38:31 -0800832 mRenderer->signalAudioSinkChanged();
833 } else {
834 // video
Andreas Huber3831a062010-12-21 10:22:33 -0800835
Andreas Huber31e25082011-01-10 10:38:31 -0800836 int32_t width, height;
Lajos Molnar1cd13982014-01-17 15:12:51 -0800837 CHECK(format->findInt32("width", &width));
838 CHECK(format->findInt32("height", &height));
Andreas Huber31e25082011-01-10 10:38:31 -0800839
840 int32_t cropLeft, cropTop, cropRight, cropBottom;
Lajos Molnar1cd13982014-01-17 15:12:51 -0800841 CHECK(format->findRect(
Andreas Huber31e25082011-01-10 10:38:31 -0800842 "crop",
843 &cropLeft, &cropTop, &cropRight, &cropBottom));
844
Andreas Huber516dacf2012-12-03 15:20:40 -0800845 int32_t displayWidth = cropRight - cropLeft + 1;
846 int32_t displayHeight = cropBottom - cropTop + 1;
847
Steve Block3856b092011-10-20 11:56:00 +0100848 ALOGV("Video output format changed to %d x %d "
Andreas Hubercb67cd12011-08-26 16:02:19 -0700849 "(crop: %d x %d @ (%d, %d))",
Andreas Huber31e25082011-01-10 10:38:31 -0800850 width, height,
Andreas Huber516dacf2012-12-03 15:20:40 -0800851 displayWidth,
852 displayHeight,
Andreas Hubercb67cd12011-08-26 16:02:19 -0700853 cropLeft, cropTop);
Andreas Huber31e25082011-01-10 10:38:31 -0800854
Andreas Huber516dacf2012-12-03 15:20:40 -0800855 sp<AMessage> videoInputFormat =
856 mSource->getFormat(false /* audio */);
857
858 // Take into account sample aspect ratio if necessary:
859 int32_t sarWidth, sarHeight;
860 if (videoInputFormat->findInt32("sar-width", &sarWidth)
861 && videoInputFormat->findInt32(
862 "sar-height", &sarHeight)) {
863 ALOGV("Sample aspect ratio %d : %d",
864 sarWidth, sarHeight);
865
866 displayWidth = (displayWidth * sarWidth) / sarHeight;
867
868 ALOGV("display dimensions %d x %d",
869 displayWidth, displayHeight);
870 }
871
Chong Zhange9e63bc2014-07-30 17:25:06 -0700872 int32_t rotationDegrees;
873 if (!videoInputFormat->findInt32(
874 "rotation-degrees", &rotationDegrees)) {
875 rotationDegrees = 0;
876 }
877
878 if (rotationDegrees == 90 || rotationDegrees == 270) {
879 notifyListener(
880 MEDIA_SET_VIDEO_SIZE,
881 displayHeight,
882 displayWidth);
883 } else {
884 notifyListener(
885 MEDIA_SET_VIDEO_SIZE,
886 displayWidth,
887 displayHeight);
888 }
Andreas Huber31e25082011-01-10 10:38:31 -0800889 }
Lajos Molnar1cd13982014-01-17 15:12:51 -0800890 } else if (what == Decoder::kWhatShutdownCompleted) {
Steve Block3856b092011-10-20 11:56:00 +0100891 ALOGV("%s shutdown completed", audio ? "audio" : "video");
Andreas Huber3831a062010-12-21 10:22:33 -0800892 if (audio) {
893 mAudioDecoder.clear();
894
895 CHECK_EQ((int)mFlushingAudio, (int)SHUTTING_DOWN_DECODER);
896 mFlushingAudio = SHUT_DOWN;
897 } else {
898 mVideoDecoder.clear();
899
900 CHECK_EQ((int)mFlushingVideo, (int)SHUTTING_DOWN_DECODER);
901 mFlushingVideo = SHUT_DOWN;
902 }
903
904 finishFlushIfPossible();
Lajos Molnar1cd13982014-01-17 15:12:51 -0800905 } else if (what == Decoder::kWhatError) {
Steve Block29357bc2012-01-06 19:20:56 +0000906 ALOGE("Received error from %s decoder, aborting playback.",
Andreas Huberc92fd242011-08-16 13:48:44 -0700907 audio ? "audio" : "video");
908
909 mRenderer->queueEOS(audio, UNKNOWN_ERROR);
Lajos Molnar1cd13982014-01-17 15:12:51 -0800910 } else if (what == Decoder::kWhatDrainThisBuffer) {
911 renderBuffer(audio, msg);
912 } else {
913 ALOGV("Unhandled decoder notification %d '%c%c%c%c'.",
Andreas Hubera1f8ab02012-11-30 10:53:22 -0800914 what,
915 what >> 24,
916 (what >> 16) & 0xff,
917 (what >> 8) & 0xff,
918 what & 0xff);
Andreas Huberf9334412010-12-15 15:17:42 -0800919 }
920
921 break;
922 }
923
924 case kWhatRendererNotify:
925 {
926 int32_t what;
927 CHECK(msg->findInt32("what", &what));
928
929 if (what == Renderer::kWhatEOS) {
930 int32_t audio;
931 CHECK(msg->findInt32("audio", &audio));
932
Andreas Huberc92fd242011-08-16 13:48:44 -0700933 int32_t finalResult;
934 CHECK(msg->findInt32("finalResult", &finalResult));
935
Andreas Huberf9334412010-12-15 15:17:42 -0800936 if (audio) {
937 mAudioEOS = true;
938 } else {
939 mVideoEOS = true;
940 }
941
Andreas Huberc92fd242011-08-16 13:48:44 -0700942 if (finalResult == ERROR_END_OF_STREAM) {
Steve Block3856b092011-10-20 11:56:00 +0100943 ALOGV("reached %s EOS", audio ? "audio" : "video");
Andreas Huberc92fd242011-08-16 13:48:44 -0700944 } else {
Steve Block29357bc2012-01-06 19:20:56 +0000945 ALOGE("%s track encountered an error (%d)",
Andreas Huberc92fd242011-08-16 13:48:44 -0700946 audio ? "audio" : "video", finalResult);
947
948 notifyListener(
949 MEDIA_ERROR, MEDIA_ERROR_UNKNOWN, finalResult);
950 }
Andreas Huberf9334412010-12-15 15:17:42 -0800951
952 if ((mAudioEOS || mAudioDecoder == NULL)
953 && (mVideoEOS || mVideoDecoder == NULL)) {
954 notifyListener(MEDIA_PLAYBACK_COMPLETE, 0, 0);
955 }
Andreas Huber43c3e6c2011-01-05 12:17:08 -0800956 } else if (what == Renderer::kWhatPosition) {
957 int64_t positionUs;
958 CHECK(msg->findInt64("positionUs", &positionUs));
959
Andreas Huber3fe62152011-09-16 15:09:22 -0700960 CHECK(msg->findInt64("videoLateByUs", &mVideoLateByUs));
961
Andreas Huber43c3e6c2011-01-05 12:17:08 -0800962 if (mDriver != NULL) {
963 sp<NuPlayerDriver> driver = mDriver.promote();
964 if (driver != NULL) {
965 driver->notifyPosition(positionUs);
Andreas Huber3fe62152011-09-16 15:09:22 -0700966
967 driver->notifyFrameStats(
968 mNumFramesTotal, mNumFramesDropped);
Andreas Huber43c3e6c2011-01-05 12:17:08 -0800969 }
970 }
Andreas Huber3fe62152011-09-16 15:09:22 -0700971 } else if (what == Renderer::kWhatFlushComplete) {
Andreas Huberf9334412010-12-15 15:17:42 -0800972 int32_t audio;
973 CHECK(msg->findInt32("audio", &audio));
974
Steve Block3856b092011-10-20 11:56:00 +0100975 ALOGV("renderer %s flush completed.", audio ? "audio" : "video");
James Dongf57b4ea2012-07-20 13:38:36 -0700976 } else if (what == Renderer::kWhatVideoRenderingStart) {
977 notifyListener(MEDIA_INFO, MEDIA_INFO_RENDERING_START, 0);
Lajos Molnarcbaffcf2013-08-14 18:30:38 -0700978 } else if (what == Renderer::kWhatMediaRenderingStart) {
979 ALOGV("media rendering started");
980 notifyListener(MEDIA_STARTED, 0, 0);
Wei Jia3a2956d2014-07-22 16:01:33 -0700981 } else if (what == Renderer::kWhatAudioOffloadTearDown) {
982 ALOGV("Tear down audio offload, fall back to s/w path");
983 int64_t positionUs;
984 CHECK(msg->findInt64("positionUs", &positionUs));
985 mAudioSink->close();
986 mAudioDecoder.clear();
987 mRenderer->flush(true /* audio */);
988 if (mVideoDecoder != NULL) {
989 mRenderer->flush(false /* audio */);
990 }
991 mRenderer->signalDisableOffloadAudio();
992 mOffloadAudio = false;
993
994 performSeek(positionUs);
995 instantiateDecoder(true /* audio */, &mAudioDecoder);
Andreas Huberf9334412010-12-15 15:17:42 -0800996 }
997 break;
998 }
999
1000 case kWhatMoreDataQueued:
1001 {
1002 break;
1003 }
1004
Andreas Huber1aef2112011-01-04 14:01:29 -08001005 case kWhatReset:
1006 {
Steve Block3856b092011-10-20 11:56:00 +01001007 ALOGV("kWhatReset");
Andreas Huber1aef2112011-01-04 14:01:29 -08001008
Andreas Hubera1f8ab02012-11-30 10:53:22 -08001009 mDeferredActions.push_back(
Andreas Huber14f76722013-01-15 09:04:18 -08001010 new ShutdownDecoderAction(
1011 true /* audio */, true /* video */));
Andreas Huberb7c8e912012-11-27 15:02:53 -08001012
Andreas Hubera1f8ab02012-11-30 10:53:22 -08001013 mDeferredActions.push_back(
1014 new SimpleAction(&NuPlayer::performReset));
Andreas Huberb58ce9f2011-11-28 16:27:35 -08001015
Andreas Hubera1f8ab02012-11-30 10:53:22 -08001016 processDeferredActions();
Andreas Huber1aef2112011-01-04 14:01:29 -08001017 break;
1018 }
1019
Andreas Huber43c3e6c2011-01-05 12:17:08 -08001020 case kWhatSeek:
1021 {
1022 int64_t seekTimeUs;
1023 CHECK(msg->findInt64("seekTimeUs", &seekTimeUs));
1024
Andreas Hubera1f8ab02012-11-30 10:53:22 -08001025 ALOGV("kWhatSeek seekTimeUs=%lld us", seekTimeUs);
Andreas Huber43c3e6c2011-01-05 12:17:08 -08001026
Andreas Hubera1f8ab02012-11-30 10:53:22 -08001027 mDeferredActions.push_back(
1028 new SimpleAction(&NuPlayer::performDecoderFlush));
Andreas Huber43c3e6c2011-01-05 12:17:08 -08001029
Andreas Hubera1f8ab02012-11-30 10:53:22 -08001030 mDeferredActions.push_back(new SeekAction(seekTimeUs));
Andreas Huber43c3e6c2011-01-05 12:17:08 -08001031
Andreas Hubera1f8ab02012-11-30 10:53:22 -08001032 processDeferredActions();
Andreas Huber43c3e6c2011-01-05 12:17:08 -08001033 break;
1034 }
1035
Andreas Huberb4082222011-01-20 15:23:04 -08001036 case kWhatPause:
1037 {
1038 CHECK(mRenderer != NULL);
Roger Jönssonfba60da2013-01-21 17:15:45 +01001039 mSource->pause();
Andreas Huberb4082222011-01-20 15:23:04 -08001040 mRenderer->pause();
1041 break;
1042 }
1043
1044 case kWhatResume:
1045 {
1046 CHECK(mRenderer != NULL);
Roger Jönssonfba60da2013-01-21 17:15:45 +01001047 mSource->resume();
Andreas Huberb4082222011-01-20 15:23:04 -08001048 mRenderer->resume();
1049 break;
1050 }
1051
Andreas Huberb5f25f02013-02-05 10:14:26 -08001052 case kWhatSourceNotify:
1053 {
Andreas Huber9575c962013-02-05 13:59:56 -08001054 onSourceNotify(msg);
Andreas Huberb5f25f02013-02-05 10:14:26 -08001055 break;
1056 }
1057
Chong Zhanga7fa1d92014-06-11 14:49:23 -07001058 case kWhatClosedCaptionNotify:
1059 {
1060 onClosedCaptionNotify(msg);
1061 break;
1062 }
1063
Andreas Huberf9334412010-12-15 15:17:42 -08001064 default:
1065 TRESPASS();
1066 break;
1067 }
1068}
1069
Andreas Huber3831a062010-12-21 10:22:33 -08001070void NuPlayer::finishFlushIfPossible() {
1071 if (mFlushingAudio != FLUSHED && mFlushingAudio != SHUT_DOWN) {
1072 return;
1073 }
1074
1075 if (mFlushingVideo != FLUSHED && mFlushingVideo != SHUT_DOWN) {
1076 return;
1077 }
1078
Steve Block3856b092011-10-20 11:56:00 +01001079 ALOGV("both audio and video are flushed now.");
Andreas Huber3831a062010-12-21 10:22:33 -08001080
Andreas Huber6e3d3112011-11-28 12:36:11 -08001081 if (mTimeDiscontinuityPending) {
1082 mRenderer->signalTimeDiscontinuity();
1083 mTimeDiscontinuityPending = false;
1084 }
Andreas Huber3831a062010-12-21 10:22:33 -08001085
Andreas Huber22fc52f2011-01-05 16:24:27 -08001086 if (mAudioDecoder != NULL) {
Andreas Huber3831a062010-12-21 10:22:33 -08001087 mAudioDecoder->signalResume();
1088 }
1089
Andreas Huber22fc52f2011-01-05 16:24:27 -08001090 if (mVideoDecoder != NULL) {
Andreas Huber3831a062010-12-21 10:22:33 -08001091 mVideoDecoder->signalResume();
1092 }
1093
1094 mFlushingAudio = NONE;
1095 mFlushingVideo = NONE;
Andreas Huber3831a062010-12-21 10:22:33 -08001096
Andreas Hubera1f8ab02012-11-30 10:53:22 -08001097 processDeferredActions();
Andreas Huber1aef2112011-01-04 14:01:29 -08001098}
1099
1100void NuPlayer::postScanSources() {
1101 if (mScanSourcesPending) {
1102 return;
1103 }
1104
1105 sp<AMessage> msg = new AMessage(kWhatScanSources, id());
1106 msg->setInt32("generation", mScanSourcesGeneration);
1107 msg->post();
1108
1109 mScanSourcesPending = true;
1110}
1111
Andreas Huber5bc087c2010-12-23 10:27:40 -08001112status_t NuPlayer::instantiateDecoder(bool audio, sp<Decoder> *decoder) {
Andreas Huberf9334412010-12-15 15:17:42 -08001113 if (*decoder != NULL) {
1114 return OK;
1115 }
1116
Andreas Huber84066782011-08-16 09:34:26 -07001117 sp<AMessage> format = mSource->getFormat(audio);
Andreas Huberf9334412010-12-15 15:17:42 -08001118
Andreas Huber84066782011-08-16 09:34:26 -07001119 if (format == NULL) {
Andreas Huberf9334412010-12-15 15:17:42 -08001120 return -EWOULDBLOCK;
1121 }
1122
Andreas Huber3fe62152011-09-16 15:09:22 -07001123 if (!audio) {
Andreas Huber84066782011-08-16 09:34:26 -07001124 AString mime;
1125 CHECK(format->findString("mime", &mime));
1126 mVideoIsAVC = !strcasecmp(MEDIA_MIMETYPE_VIDEO_AVC, mime.c_str());
Chong Zhanga7fa1d92014-06-11 14:49:23 -07001127
1128 sp<AMessage> ccNotify = new AMessage(kWhatClosedCaptionNotify, id());
1129 mCCDecoder = new CCDecoder(ccNotify);
Lajos Molnar09524832014-07-17 14:29:51 -07001130
1131 if (mSourceFlags & Source::FLAG_SECURE) {
1132 format->setInt32("secure", true);
1133 }
Andreas Huber3fe62152011-09-16 15:09:22 -07001134 }
1135
Andreas Huberf9334412010-12-15 15:17:42 -08001136 sp<AMessage> notify =
1137 new AMessage(audio ? kWhatAudioNotify : kWhatVideoNotify,
1138 id());
1139
Wei Jiabc2fb722014-07-08 16:37:57 -07001140 if (audio) {
1141 if (mOffloadAudio) {
1142 *decoder = new DecoderPassThrough(notify);
1143 } else {
1144 *decoder = new Decoder(notify);
1145 }
1146 } else {
1147 *decoder = new Decoder(notify, mNativeWindow);
1148 }
Lajos Molnar1cd13982014-01-17 15:12:51 -08001149 (*decoder)->init();
Andreas Huber84066782011-08-16 09:34:26 -07001150 (*decoder)->configure(format);
Andreas Huberf9334412010-12-15 15:17:42 -08001151
Lajos Molnar09524832014-07-17 14:29:51 -07001152 // allocate buffers to decrypt widevine source buffers
1153 if (!audio && (mSourceFlags & Source::FLAG_SECURE)) {
1154 Vector<sp<ABuffer> > inputBufs;
1155 CHECK_EQ((*decoder)->getInputBuffers(&inputBufs), (status_t)OK);
1156
1157 Vector<MediaBuffer *> mediaBufs;
1158 for (size_t i = 0; i < inputBufs.size(); i++) {
1159 const sp<ABuffer> &buffer = inputBufs[i];
1160 MediaBuffer *mbuf = new MediaBuffer(buffer->data(), buffer->size());
1161 mediaBufs.push(mbuf);
1162 }
1163
1164 status_t err = mSource->setBuffers(audio, mediaBufs);
1165 if (err != OK) {
1166 for (size_t i = 0; i < mediaBufs.size(); ++i) {
1167 mediaBufs[i]->release();
1168 }
1169 mediaBufs.clear();
1170 ALOGE("Secure source didn't support secure mediaBufs.");
1171 return err;
1172 }
1173 }
Andreas Huberf9334412010-12-15 15:17:42 -08001174 return OK;
1175}
1176
1177status_t NuPlayer::feedDecoderInputData(bool audio, const sp<AMessage> &msg) {
1178 sp<AMessage> reply;
1179 CHECK(msg->findMessage("reply", &reply));
1180
Andreas Huber53df1a42010-12-22 10:03:04 -08001181 if ((audio && IsFlushingState(mFlushingAudio))
1182 || (!audio && IsFlushingState(mFlushingVideo))) {
Andreas Huberf9334412010-12-15 15:17:42 -08001183 reply->setInt32("err", INFO_DISCONTINUITY);
1184 reply->post();
1185 return OK;
1186 }
1187
1188 sp<ABuffer> accessUnit;
Andreas Huberf9334412010-12-15 15:17:42 -08001189
Andreas Huber3fe62152011-09-16 15:09:22 -07001190 bool dropAccessUnit;
1191 do {
1192 status_t err = mSource->dequeueAccessUnit(audio, &accessUnit);
Andreas Huber5bc087c2010-12-23 10:27:40 -08001193
Andreas Huber3fe62152011-09-16 15:09:22 -07001194 if (err == -EWOULDBLOCK) {
1195 return err;
1196 } else if (err != OK) {
1197 if (err == INFO_DISCONTINUITY) {
1198 int32_t type;
1199 CHECK(accessUnit->meta()->findInt32("discontinuity", &type));
Andreas Huber53df1a42010-12-22 10:03:04 -08001200
Andreas Huber3fe62152011-09-16 15:09:22 -07001201 bool formatChange =
Andreas Huber6e3d3112011-11-28 12:36:11 -08001202 (audio &&
1203 (type & ATSParser::DISCONTINUITY_AUDIO_FORMAT))
1204 || (!audio &&
1205 (type & ATSParser::DISCONTINUITY_VIDEO_FORMAT));
Andreas Huber53df1a42010-12-22 10:03:04 -08001206
Andreas Huber6e3d3112011-11-28 12:36:11 -08001207 bool timeChange = (type & ATSParser::DISCONTINUITY_TIME) != 0;
1208
Steve Blockdf64d152012-01-04 20:05:49 +00001209 ALOGI("%s discontinuity (formatChange=%d, time=%d)",
Andreas Huber6e3d3112011-11-28 12:36:11 -08001210 audio ? "audio" : "video", formatChange, timeChange);
Andreas Huber32f3cef2011-03-02 15:34:46 -08001211
Andreas Huber3fe62152011-09-16 15:09:22 -07001212 if (audio) {
1213 mSkipRenderingAudioUntilMediaTimeUs = -1;
1214 } else {
1215 mSkipRenderingVideoUntilMediaTimeUs = -1;
1216 }
Andreas Huber32f3cef2011-03-02 15:34:46 -08001217
Andreas Huber6e3d3112011-11-28 12:36:11 -08001218 if (timeChange) {
1219 sp<AMessage> extra;
1220 if (accessUnit->meta()->findMessage("extra", &extra)
1221 && extra != NULL) {
1222 int64_t resumeAtMediaTimeUs;
1223 if (extra->findInt64(
1224 "resume-at-mediatimeUs", &resumeAtMediaTimeUs)) {
Steve Blockdf64d152012-01-04 20:05:49 +00001225 ALOGI("suppressing rendering of %s until %lld us",
Andreas Huber6e3d3112011-11-28 12:36:11 -08001226 audio ? "audio" : "video", resumeAtMediaTimeUs);
Andreas Huber3fe62152011-09-16 15:09:22 -07001227
Andreas Huber6e3d3112011-11-28 12:36:11 -08001228 if (audio) {
1229 mSkipRenderingAudioUntilMediaTimeUs =
1230 resumeAtMediaTimeUs;
1231 } else {
1232 mSkipRenderingVideoUntilMediaTimeUs =
1233 resumeAtMediaTimeUs;
1234 }
Andreas Huber3fe62152011-09-16 15:09:22 -07001235 }
Andreas Huber32f3cef2011-03-02 15:34:46 -08001236 }
1237 }
Andreas Huber3fe62152011-09-16 15:09:22 -07001238
Andreas Huber6e3d3112011-11-28 12:36:11 -08001239 mTimeDiscontinuityPending =
1240 mTimeDiscontinuityPending || timeChange;
1241
Robert Shiha2981012014-07-30 17:41:24 -07001242 if (mFlushingAudio == NONE && mFlushingVideo == NONE) {
1243 // And we'll resume scanning sources once we're done
1244 // flushing.
1245 mDeferredActions.push_front(
1246 new SimpleAction(
1247 &NuPlayer::performScanSources));
1248 }
1249
Andreas Huber6e3d3112011-11-28 12:36:11 -08001250 if (formatChange || timeChange) {
Andreas Hubera1f8ab02012-11-30 10:53:22 -08001251
Robert Shih6d0a94e2014-01-23 16:18:22 -08001252 sp<AMessage> newFormat = mSource->getFormat(audio);
1253 sp<Decoder> &decoder = audio ? mAudioDecoder : mVideoDecoder;
1254 if (formatChange && !decoder->supportsSeamlessFormatChange(newFormat)) {
1255 flushDecoder(audio, /* needShutdown = */ true);
1256 } else {
1257 flushDecoder(audio, /* needShutdown = */ false);
1258 err = OK;
1259 }
Andreas Huber6e3d3112011-11-28 12:36:11 -08001260 } else {
1261 // This stream is unaffected by the discontinuity
1262
1263 if (audio) {
1264 mFlushingAudio = FLUSHED;
1265 } else {
1266 mFlushingVideo = FLUSHED;
1267 }
1268
1269 finishFlushIfPossible();
1270
1271 return -EWOULDBLOCK;
1272 }
Andreas Huber32f3cef2011-03-02 15:34:46 -08001273 }
1274
Andreas Huber3fe62152011-09-16 15:09:22 -07001275 reply->setInt32("err", err);
1276 reply->post();
1277 return OK;
Andreas Huberf9334412010-12-15 15:17:42 -08001278 }
1279
Andreas Huber3fe62152011-09-16 15:09:22 -07001280 if (!audio) {
1281 ++mNumFramesTotal;
1282 }
1283
1284 dropAccessUnit = false;
1285 if (!audio
Lajos Molnar09524832014-07-17 14:29:51 -07001286 && !(mSourceFlags & Source::FLAG_SECURE)
Andreas Huber3fe62152011-09-16 15:09:22 -07001287 && mVideoLateByUs > 100000ll
1288 && mVideoIsAVC
1289 && !IsAVCReferenceFrame(accessUnit)) {
1290 dropAccessUnit = true;
1291 ++mNumFramesDropped;
1292 }
1293 } while (dropAccessUnit);
Andreas Huberf9334412010-12-15 15:17:42 -08001294
Steve Block3856b092011-10-20 11:56:00 +01001295 // ALOGV("returned a valid buffer of %s data", audio ? "audio" : "video");
Andreas Huberf9334412010-12-15 15:17:42 -08001296
1297#if 0
1298 int64_t mediaTimeUs;
1299 CHECK(accessUnit->meta()->findInt64("timeUs", &mediaTimeUs));
Steve Block3856b092011-10-20 11:56:00 +01001300 ALOGV("feeding %s input buffer at media time %.2f secs",
Andreas Huberf9334412010-12-15 15:17:42 -08001301 audio ? "audio" : "video",
1302 mediaTimeUs / 1E6);
1303#endif
1304
Chong Zhanga7fa1d92014-06-11 14:49:23 -07001305 if (!audio) {
1306 mCCDecoder->decode(accessUnit);
1307 }
1308
Andreas Huber2d8bedd2012-02-21 14:38:23 -08001309 reply->setBuffer("buffer", accessUnit);
Andreas Huberf9334412010-12-15 15:17:42 -08001310 reply->post();
1311
1312 return OK;
1313}
1314
1315void NuPlayer::renderBuffer(bool audio, const sp<AMessage> &msg) {
Steve Block3856b092011-10-20 11:56:00 +01001316 // ALOGV("renderBuffer %s", audio ? "audio" : "video");
Andreas Huberf9334412010-12-15 15:17:42 -08001317
1318 sp<AMessage> reply;
1319 CHECK(msg->findMessage("reply", &reply));
1320
Andreas Huber18ac5402011-08-31 15:04:25 -07001321 if (IsFlushingState(audio ? mFlushingAudio : mFlushingVideo)) {
1322 // We're currently attempting to flush the decoder, in order
1323 // to complete this, the decoder wants all its buffers back,
1324 // so we don't want any output buffers it sent us (from before
1325 // we initiated the flush) to be stuck in the renderer's queue.
1326
Steve Block3856b092011-10-20 11:56:00 +01001327 ALOGV("we're still flushing the %s decoder, sending its output buffer"
Andreas Huber18ac5402011-08-31 15:04:25 -07001328 " right back.", audio ? "audio" : "video");
1329
1330 reply->post();
1331 return;
1332 }
1333
Andreas Huber2d8bedd2012-02-21 14:38:23 -08001334 sp<ABuffer> buffer;
1335 CHECK(msg->findBuffer("buffer", &buffer));
Andreas Huberf9334412010-12-15 15:17:42 -08001336
Chong Zhanga7fa1d92014-06-11 14:49:23 -07001337 int64_t mediaTimeUs;
1338 CHECK(buffer->meta()->findInt64("timeUs", &mediaTimeUs));
1339
Andreas Huber32f3cef2011-03-02 15:34:46 -08001340 int64_t &skipUntilMediaTimeUs =
1341 audio
1342 ? mSkipRenderingAudioUntilMediaTimeUs
1343 : mSkipRenderingVideoUntilMediaTimeUs;
1344
1345 if (skipUntilMediaTimeUs >= 0) {
Andreas Huber32f3cef2011-03-02 15:34:46 -08001346
1347 if (mediaTimeUs < skipUntilMediaTimeUs) {
Steve Block3856b092011-10-20 11:56:00 +01001348 ALOGV("dropping %s buffer at time %lld as requested.",
Andreas Huber32f3cef2011-03-02 15:34:46 -08001349 audio ? "audio" : "video",
1350 mediaTimeUs);
1351
1352 reply->post();
1353 return;
1354 }
1355
1356 skipUntilMediaTimeUs = -1;
1357 }
1358
Chong Zhanga7fa1d92014-06-11 14:49:23 -07001359 if (!audio && mCCDecoder->isSelected()) {
1360 mCCDecoder->display(mediaTimeUs);
1361 }
1362
Andreas Huberf9334412010-12-15 15:17:42 -08001363 mRenderer->queueBuffer(audio, buffer, reply);
1364}
1365
Chong Zhangdcb89b32013-08-06 09:44:47 -07001366void NuPlayer::notifyListener(int msg, int ext1, int ext2, const Parcel *in) {
Andreas Huber43c3e6c2011-01-05 12:17:08 -08001367 if (mDriver == NULL) {
Andreas Huberf9334412010-12-15 15:17:42 -08001368 return;
1369 }
1370
Andreas Huber43c3e6c2011-01-05 12:17:08 -08001371 sp<NuPlayerDriver> driver = mDriver.promote();
Andreas Huberf9334412010-12-15 15:17:42 -08001372
Andreas Huber43c3e6c2011-01-05 12:17:08 -08001373 if (driver == NULL) {
Andreas Huberf9334412010-12-15 15:17:42 -08001374 return;
1375 }
1376
Chong Zhangdcb89b32013-08-06 09:44:47 -07001377 driver->notifyListener(msg, ext1, ext2, in);
Andreas Huberf9334412010-12-15 15:17:42 -08001378}
1379
Andreas Huber1aef2112011-01-04 14:01:29 -08001380void NuPlayer::flushDecoder(bool audio, bool needShutdown) {
Andreas Huber14f76722013-01-15 09:04:18 -08001381 ALOGV("[%s] flushDecoder needShutdown=%d",
1382 audio ? "audio" : "video", needShutdown);
1383
Andreas Huber6e3d3112011-11-28 12:36:11 -08001384 if ((audio && mAudioDecoder == NULL) || (!audio && mVideoDecoder == NULL)) {
Steve Blockdf64d152012-01-04 20:05:49 +00001385 ALOGI("flushDecoder %s without decoder present",
Andreas Huber6e3d3112011-11-28 12:36:11 -08001386 audio ? "audio" : "video");
1387 }
1388
Andreas Huber1aef2112011-01-04 14:01:29 -08001389 // Make sure we don't continue to scan sources until we finish flushing.
1390 ++mScanSourcesGeneration;
Andreas Huber43c3e6c2011-01-05 12:17:08 -08001391 mScanSourcesPending = false;
Andreas Huber1aef2112011-01-04 14:01:29 -08001392
1393 (audio ? mAudioDecoder : mVideoDecoder)->signalFlush();
1394 mRenderer->flush(audio);
1395
1396 FlushStatus newStatus =
1397 needShutdown ? FLUSHING_DECODER_SHUTDOWN : FLUSHING_DECODER;
1398
1399 if (audio) {
1400 CHECK(mFlushingAudio == NONE
1401 || mFlushingAudio == AWAITING_DISCONTINUITY);
1402
1403 mFlushingAudio = newStatus;
1404
1405 if (mFlushingVideo == NONE) {
1406 mFlushingVideo = (mVideoDecoder != NULL)
1407 ? AWAITING_DISCONTINUITY
1408 : FLUSHED;
1409 }
1410 } else {
1411 CHECK(mFlushingVideo == NONE
1412 || mFlushingVideo == AWAITING_DISCONTINUITY);
1413
1414 mFlushingVideo = newStatus;
1415
1416 if (mFlushingAudio == NONE) {
1417 mFlushingAudio = (mAudioDecoder != NULL)
1418 ? AWAITING_DISCONTINUITY
1419 : FLUSHED;
1420 }
1421 }
1422}
1423
Andreas Huber84066782011-08-16 09:34:26 -07001424sp<AMessage> NuPlayer::Source::getFormat(bool audio) {
1425 sp<MetaData> meta = getFormatMeta(audio);
1426
1427 if (meta == NULL) {
1428 return NULL;
1429 }
1430
1431 sp<AMessage> msg = new AMessage;
1432
1433 if(convertMetaDataToMessage(meta, &msg) == OK) {
1434 return msg;
1435 }
1436 return NULL;
1437}
1438
James Dong0d268a32012-08-31 12:18:27 -07001439status_t NuPlayer::setVideoScalingMode(int32_t mode) {
1440 mVideoScalingMode = mode;
Andreas Huber57a339c2012-12-03 11:18:00 -08001441 if (mNativeWindow != NULL) {
James Dong0d268a32012-08-31 12:18:27 -07001442 status_t ret = native_window_set_scaling_mode(
1443 mNativeWindow->getNativeWindow().get(), mVideoScalingMode);
1444 if (ret != OK) {
1445 ALOGE("Failed to set scaling mode (%d): %s",
1446 -ret, strerror(-ret));
1447 return ret;
1448 }
1449 }
1450 return OK;
1451}
1452
Chong Zhangdcb89b32013-08-06 09:44:47 -07001453status_t NuPlayer::getTrackInfo(Parcel* reply) const {
1454 sp<AMessage> msg = new AMessage(kWhatGetTrackInfo, id());
1455 msg->setPointer("reply", reply);
1456
1457 sp<AMessage> response;
1458 status_t err = msg->postAndAwaitResponse(&response);
1459 return err;
1460}
1461
1462status_t NuPlayer::selectTrack(size_t trackIndex, bool select) {
1463 sp<AMessage> msg = new AMessage(kWhatSelectTrack, id());
1464 msg->setSize("trackIndex", trackIndex);
1465 msg->setInt32("select", select);
1466
1467 sp<AMessage> response;
1468 status_t err = msg->postAndAwaitResponse(&response);
1469
Chong Zhang404fced2014-06-11 14:45:31 -07001470 if (err != OK) {
1471 return err;
1472 }
1473
1474 if (!response->findInt32("err", &err)) {
1475 err = OK;
1476 }
1477
Chong Zhangdcb89b32013-08-06 09:44:47 -07001478 return err;
1479}
1480
Andreas Huberb7c8e912012-11-27 15:02:53 -08001481void NuPlayer::schedulePollDuration() {
1482 sp<AMessage> msg = new AMessage(kWhatPollDuration, id());
1483 msg->setInt32("generation", mPollDurationGeneration);
1484 msg->post();
1485}
1486
1487void NuPlayer::cancelPollDuration() {
1488 ++mPollDurationGeneration;
1489}
1490
Andreas Hubera1f8ab02012-11-30 10:53:22 -08001491void NuPlayer::processDeferredActions() {
1492 while (!mDeferredActions.empty()) {
1493 // We won't execute any deferred actions until we're no longer in
1494 // an intermediate state, i.e. one more more decoders are currently
1495 // flushing or shutting down.
1496
1497 if (mRenderer != NULL) {
1498 // There's an edge case where the renderer owns all output
1499 // buffers and is paused, therefore the decoder will not read
1500 // more input data and will never encounter the matching
1501 // discontinuity. To avoid this, we resume the renderer.
1502
1503 if (mFlushingAudio == AWAITING_DISCONTINUITY
1504 || mFlushingVideo == AWAITING_DISCONTINUITY) {
1505 mRenderer->resume();
1506 }
1507 }
1508
1509 if (mFlushingAudio != NONE || mFlushingVideo != NONE) {
1510 // We're currently flushing, postpone the reset until that's
1511 // completed.
1512
1513 ALOGV("postponing action mFlushingAudio=%d, mFlushingVideo=%d",
1514 mFlushingAudio, mFlushingVideo);
1515
1516 break;
1517 }
1518
1519 sp<Action> action = *mDeferredActions.begin();
1520 mDeferredActions.erase(mDeferredActions.begin());
1521
1522 action->execute(this);
1523 }
1524}
1525
1526void NuPlayer::performSeek(int64_t seekTimeUs) {
1527 ALOGV("performSeek seekTimeUs=%lld us (%.2f secs)",
1528 seekTimeUs,
1529 seekTimeUs / 1E6);
1530
1531 mSource->seekTo(seekTimeUs);
Robert Shihd3b0bbb2014-07-23 15:00:25 -07001532 ++mTimedTextGeneration;
Andreas Hubera1f8ab02012-11-30 10:53:22 -08001533
1534 if (mDriver != NULL) {
1535 sp<NuPlayerDriver> driver = mDriver.promote();
1536 if (driver != NULL) {
1537 driver->notifyPosition(seekTimeUs);
1538 driver->notifySeekComplete();
1539 }
1540 }
1541
1542 // everything's flushed, continue playback.
1543}
1544
1545void NuPlayer::performDecoderFlush() {
1546 ALOGV("performDecoderFlush");
1547
Andreas Huberda9740e2013-04-16 10:54:03 -07001548 if (mAudioDecoder == NULL && mVideoDecoder == NULL) {
Andreas Hubera1f8ab02012-11-30 10:53:22 -08001549 return;
1550 }
1551
1552 mTimeDiscontinuityPending = true;
1553
1554 if (mAudioDecoder != NULL) {
1555 flushDecoder(true /* audio */, false /* needShutdown */);
1556 }
1557
1558 if (mVideoDecoder != NULL) {
1559 flushDecoder(false /* audio */, false /* needShutdown */);
1560 }
1561}
1562
Andreas Huber14f76722013-01-15 09:04:18 -08001563void NuPlayer::performDecoderShutdown(bool audio, bool video) {
1564 ALOGV("performDecoderShutdown audio=%d, video=%d", audio, video);
Andreas Hubera1f8ab02012-11-30 10:53:22 -08001565
Andreas Huber14f76722013-01-15 09:04:18 -08001566 if ((!audio || mAudioDecoder == NULL)
1567 && (!video || mVideoDecoder == NULL)) {
Andreas Hubera1f8ab02012-11-30 10:53:22 -08001568 return;
1569 }
1570
1571 mTimeDiscontinuityPending = true;
1572
Andreas Huber14f76722013-01-15 09:04:18 -08001573 if (mFlushingAudio == NONE && (!audio || mAudioDecoder == NULL)) {
1574 mFlushingAudio = FLUSHED;
1575 }
1576
1577 if (mFlushingVideo == NONE && (!video || mVideoDecoder == NULL)) {
1578 mFlushingVideo = FLUSHED;
1579 }
1580
1581 if (audio && mAudioDecoder != NULL) {
Andreas Hubera1f8ab02012-11-30 10:53:22 -08001582 flushDecoder(true /* audio */, true /* needShutdown */);
1583 }
1584
Andreas Huber14f76722013-01-15 09:04:18 -08001585 if (video && mVideoDecoder != NULL) {
Andreas Hubera1f8ab02012-11-30 10:53:22 -08001586 flushDecoder(false /* audio */, true /* needShutdown */);
1587 }
1588}
1589
1590void NuPlayer::performReset() {
1591 ALOGV("performReset");
1592
1593 CHECK(mAudioDecoder == NULL);
1594 CHECK(mVideoDecoder == NULL);
1595
1596 cancelPollDuration();
1597
1598 ++mScanSourcesGeneration;
1599 mScanSourcesPending = false;
1600
Lajos Molnar09524832014-07-17 14:29:51 -07001601 if (mRendererLooper != NULL) {
1602 if (mRenderer != NULL) {
1603 mRendererLooper->unregisterHandler(mRenderer->id());
1604 }
1605 mRendererLooper->stop();
1606 mRendererLooper.clear();
1607 }
Andreas Hubera1f8ab02012-11-30 10:53:22 -08001608 mRenderer.clear();
1609
1610 if (mSource != NULL) {
1611 mSource->stop();
Andreas Huberb5f25f02013-02-05 10:14:26 -08001612
1613 looper()->unregisterHandler(mSource->id());
1614
Andreas Hubera1f8ab02012-11-30 10:53:22 -08001615 mSource.clear();
1616 }
1617
1618 if (mDriver != NULL) {
1619 sp<NuPlayerDriver> driver = mDriver.promote();
1620 if (driver != NULL) {
1621 driver->notifyResetComplete();
1622 }
1623 }
Andreas Huber57a339c2012-12-03 11:18:00 -08001624
1625 mStarted = false;
Andreas Hubera1f8ab02012-11-30 10:53:22 -08001626}
1627
1628void NuPlayer::performScanSources() {
1629 ALOGV("performScanSources");
1630
Andreas Huber57a339c2012-12-03 11:18:00 -08001631 if (!mStarted) {
1632 return;
1633 }
1634
Andreas Hubera1f8ab02012-11-30 10:53:22 -08001635 if (mAudioDecoder == NULL || mVideoDecoder == NULL) {
1636 postScanSources();
1637 }
1638}
1639
Andreas Huber57a339c2012-12-03 11:18:00 -08001640void NuPlayer::performSetSurface(const sp<NativeWindowWrapper> &wrapper) {
1641 ALOGV("performSetSurface");
1642
1643 mNativeWindow = wrapper;
1644
1645 // XXX - ignore error from setVideoScalingMode for now
1646 setVideoScalingMode(mVideoScalingMode);
1647
1648 if (mDriver != NULL) {
1649 sp<NuPlayerDriver> driver = mDriver.promote();
1650 if (driver != NULL) {
1651 driver->notifySetSurfaceComplete();
1652 }
1653 }
1654}
1655
Andreas Huber9575c962013-02-05 13:59:56 -08001656void NuPlayer::onSourceNotify(const sp<AMessage> &msg) {
1657 int32_t what;
1658 CHECK(msg->findInt32("what", &what));
1659
1660 switch (what) {
1661 case Source::kWhatPrepared:
1662 {
Andreas Huberb5f28d42013-04-25 15:11:19 -07001663 if (mSource == NULL) {
1664 // This is a stale notification from a source that was
1665 // asynchronously preparing when the client called reset().
1666 // We handled the reset, the source is gone.
1667 break;
1668 }
1669
Andreas Huberec0c5972013-02-05 14:47:13 -08001670 int32_t err;
1671 CHECK(msg->findInt32("err", &err));
1672
Andreas Huber9575c962013-02-05 13:59:56 -08001673 sp<NuPlayerDriver> driver = mDriver.promote();
1674 if (driver != NULL) {
Marco Nelissendd114d12014-05-28 15:23:14 -07001675 // notify duration first, so that it's definitely set when
1676 // the app received the "prepare complete" callback.
1677 int64_t durationUs;
1678 if (mSource->getDuration(&durationUs) == OK) {
1679 driver->notifyDuration(durationUs);
1680 }
Andreas Huberec0c5972013-02-05 14:47:13 -08001681 driver->notifyPrepareCompleted(err);
Andreas Huber9575c962013-02-05 13:59:56 -08001682 }
Andreas Huber99759402013-04-01 14:28:31 -07001683
Andreas Huber9575c962013-02-05 13:59:56 -08001684 break;
1685 }
1686
1687 case Source::kWhatFlagsChanged:
1688 {
1689 uint32_t flags;
1690 CHECK(msg->findInt32("flags", (int32_t *)&flags));
1691
Chong Zhang4b7069d2013-09-11 12:52:43 -07001692 sp<NuPlayerDriver> driver = mDriver.promote();
1693 if (driver != NULL) {
1694 driver->notifyFlagsChanged(flags);
1695 }
1696
Andreas Huber9575c962013-02-05 13:59:56 -08001697 if ((mSourceFlags & Source::FLAG_DYNAMIC_DURATION)
1698 && (!(flags & Source::FLAG_DYNAMIC_DURATION))) {
1699 cancelPollDuration();
1700 } else if (!(mSourceFlags & Source::FLAG_DYNAMIC_DURATION)
1701 && (flags & Source::FLAG_DYNAMIC_DURATION)
1702 && (mAudioDecoder != NULL || mVideoDecoder != NULL)) {
1703 schedulePollDuration();
1704 }
1705
1706 mSourceFlags = flags;
1707 break;
1708 }
1709
1710 case Source::kWhatVideoSizeChanged:
1711 {
1712 int32_t width, height;
1713 CHECK(msg->findInt32("width", &width));
1714 CHECK(msg->findInt32("height", &height));
1715
1716 notifyListener(MEDIA_SET_VIDEO_SIZE, width, height);
1717 break;
1718 }
1719
Roger Jönssonb50e83e2013-01-21 16:26:41 +01001720 case Source::kWhatBufferingStart:
1721 {
1722 notifyListener(MEDIA_INFO, MEDIA_INFO_BUFFERING_START, 0);
1723 break;
1724 }
1725
1726 case Source::kWhatBufferingEnd:
1727 {
1728 notifyListener(MEDIA_INFO, MEDIA_INFO_BUFFERING_END, 0);
1729 break;
1730 }
1731
Chong Zhangdcb89b32013-08-06 09:44:47 -07001732 case Source::kWhatSubtitleData:
1733 {
1734 sp<ABuffer> buffer;
1735 CHECK(msg->findBuffer("buffer", &buffer));
1736
Chong Zhang404fced2014-06-11 14:45:31 -07001737 sendSubtitleData(buffer, 0 /* baseIndex */);
Chong Zhangdcb89b32013-08-06 09:44:47 -07001738 break;
1739 }
1740
Robert Shihd3b0bbb2014-07-23 15:00:25 -07001741 case Source::kWhatTimedTextData:
1742 {
1743 int32_t generation;
1744 if (msg->findInt32("generation", &generation)
1745 && generation != mTimedTextGeneration) {
1746 break;
1747 }
1748
1749 sp<ABuffer> buffer;
1750 CHECK(msg->findBuffer("buffer", &buffer));
1751
1752 sp<NuPlayerDriver> driver = mDriver.promote();
1753 if (driver == NULL) {
1754 break;
1755 }
1756
1757 int posMs;
1758 int64_t timeUs, posUs;
1759 driver->getCurrentPosition(&posMs);
1760 posUs = posMs * 1000;
1761 CHECK(buffer->meta()->findInt64("timeUs", &timeUs));
1762
1763 if (posUs < timeUs) {
1764 if (!msg->findInt32("generation", &generation)) {
1765 msg->setInt32("generation", mTimedTextGeneration);
1766 }
1767 msg->post(timeUs - posUs);
1768 } else {
1769 sendTimedTextData(buffer);
1770 }
1771 break;
1772 }
1773
Andreas Huber14f76722013-01-15 09:04:18 -08001774 case Source::kWhatQueueDecoderShutdown:
1775 {
1776 int32_t audio, video;
1777 CHECK(msg->findInt32("audio", &audio));
1778 CHECK(msg->findInt32("video", &video));
1779
1780 sp<AMessage> reply;
1781 CHECK(msg->findMessage("reply", &reply));
1782
1783 queueDecoderShutdown(audio, video, reply);
1784 break;
1785 }
1786
Andreas Huber9575c962013-02-05 13:59:56 -08001787 default:
1788 TRESPASS();
1789 }
1790}
1791
Chong Zhanga7fa1d92014-06-11 14:49:23 -07001792void NuPlayer::onClosedCaptionNotify(const sp<AMessage> &msg) {
1793 int32_t what;
1794 CHECK(msg->findInt32("what", &what));
1795
1796 switch (what) {
1797 case NuPlayer::CCDecoder::kWhatClosedCaptionData:
1798 {
1799 sp<ABuffer> buffer;
1800 CHECK(msg->findBuffer("buffer", &buffer));
1801
1802 size_t inbandTracks = 0;
1803 if (mSource != NULL) {
1804 inbandTracks = mSource->getTrackCount();
1805 }
1806
1807 sendSubtitleData(buffer, inbandTracks);
1808 break;
1809 }
1810
1811 case NuPlayer::CCDecoder::kWhatTrackAdded:
1812 {
1813 notifyListener(MEDIA_INFO, MEDIA_INFO_METADATA_UPDATE, 0);
1814
1815 break;
1816 }
1817
1818 default:
1819 TRESPASS();
1820 }
1821
1822
1823}
1824
Chong Zhang404fced2014-06-11 14:45:31 -07001825void NuPlayer::sendSubtitleData(const sp<ABuffer> &buffer, int32_t baseIndex) {
1826 int32_t trackIndex;
1827 int64_t timeUs, durationUs;
1828 CHECK(buffer->meta()->findInt32("trackIndex", &trackIndex));
1829 CHECK(buffer->meta()->findInt64("timeUs", &timeUs));
1830 CHECK(buffer->meta()->findInt64("durationUs", &durationUs));
1831
1832 Parcel in;
1833 in.writeInt32(trackIndex + baseIndex);
1834 in.writeInt64(timeUs);
1835 in.writeInt64(durationUs);
1836 in.writeInt32(buffer->size());
1837 in.writeInt32(buffer->size());
1838 in.write(buffer->data(), buffer->size());
1839
1840 notifyListener(MEDIA_SUBTITLE_DATA, 0, 0, &in);
1841}
Robert Shihd3b0bbb2014-07-23 15:00:25 -07001842
1843void NuPlayer::sendTimedTextData(const sp<ABuffer> &buffer) {
1844 const void *data;
1845 size_t size = 0;
1846 int64_t timeUs;
1847 int32_t flag = TextDescriptions::LOCAL_DESCRIPTIONS;
1848
1849 AString mime;
1850 CHECK(buffer->meta()->findString("mime", &mime));
1851 CHECK(strcasecmp(mime.c_str(), MEDIA_MIMETYPE_TEXT_3GPP) == 0);
1852
1853 data = buffer->data();
1854 size = buffer->size();
1855
1856 Parcel parcel;
1857 if (size > 0) {
1858 CHECK(buffer->meta()->findInt64("timeUs", &timeUs));
1859 flag |= TextDescriptions::IN_BAND_TEXT_3GPP;
1860 TextDescriptions::getParcelOfDescriptions(
1861 (const uint8_t *)data, size, flag, timeUs / 1000, &parcel);
1862 }
1863
1864 if ((parcel.dataSize() > 0)) {
1865 notifyListener(MEDIA_TIMED_TEXT, 0, 0, &parcel);
1866 } else { // send an empty timed text
1867 notifyListener(MEDIA_TIMED_TEXT, 0, 0);
1868 }
1869}
Andreas Huberb5f25f02013-02-05 10:14:26 -08001870////////////////////////////////////////////////////////////////////////////////
1871
Andreas Huber9575c962013-02-05 13:59:56 -08001872void NuPlayer::Source::notifyFlagsChanged(uint32_t flags) {
1873 sp<AMessage> notify = dupNotify();
1874 notify->setInt32("what", kWhatFlagsChanged);
1875 notify->setInt32("flags", flags);
1876 notify->post();
1877}
1878
1879void NuPlayer::Source::notifyVideoSizeChanged(int32_t width, int32_t height) {
1880 sp<AMessage> notify = dupNotify();
1881 notify->setInt32("what", kWhatVideoSizeChanged);
1882 notify->setInt32("width", width);
1883 notify->setInt32("height", height);
1884 notify->post();
1885}
1886
Andreas Huberec0c5972013-02-05 14:47:13 -08001887void NuPlayer::Source::notifyPrepared(status_t err) {
Andreas Huber9575c962013-02-05 13:59:56 -08001888 sp<AMessage> notify = dupNotify();
1889 notify->setInt32("what", kWhatPrepared);
Andreas Huberec0c5972013-02-05 14:47:13 -08001890 notify->setInt32("err", err);
Andreas Huber9575c962013-02-05 13:59:56 -08001891 notify->post();
1892}
1893
Andreas Huber84333e02014-02-07 15:36:10 -08001894void NuPlayer::Source::onMessageReceived(const sp<AMessage> & /* msg */) {
Andreas Huberb5f25f02013-02-05 10:14:26 -08001895 TRESPASS();
1896}
1897
Andreas Huber14f76722013-01-15 09:04:18 -08001898void NuPlayer::queueDecoderShutdown(
1899 bool audio, bool video, const sp<AMessage> &reply) {
1900 ALOGI("queueDecoderShutdown audio=%d, video=%d", audio, video);
1901
1902 mDeferredActions.push_back(
1903 new ShutdownDecoderAction(audio, video));
1904
1905 mDeferredActions.push_back(
1906 new SimpleAction(&NuPlayer::performScanSources));
1907
1908 mDeferredActions.push_back(new PostMessageAction(reply));
1909
1910 processDeferredActions();
1911}
1912
Andreas Huberf9334412010-12-15 15:17:42 -08001913} // namespace android