blob: 33880975bc033392d39393598af0bda3e951ebaa [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"
Ray Essickd4d00612017-01-03 09:36:27 -080019
20#include <inttypes.h>
21
Andreas Huberf9334412010-12-15 15:17:42 -080022#include <utils/Log.h>
23
24#include "NuPlayer.h"
Andreas Huber5bc087c2010-12-23 10:27:40 -080025
26#include "HTTPLiveSource.h"
Chong Zhang7137ec72014-11-12 16:41:05 -080027#include "NuPlayerCCDecoder.h"
Andreas Huberf9334412010-12-15 15:17:42 -080028#include "NuPlayerDecoder.h"
Chong Zhang7137ec72014-11-12 16:41:05 -080029#include "NuPlayerDecoderBase.h"
Wei Jiabc2fb722014-07-08 16:37:57 -070030#include "NuPlayerDecoderPassThrough.h"
Andreas Huber43c3e6c2011-01-05 12:17:08 -080031#include "NuPlayerDriver.h"
Andreas Huberf9334412010-12-15 15:17:42 -080032#include "NuPlayerRenderer.h"
Andreas Huber5bc087c2010-12-23 10:27:40 -080033#include "NuPlayerSource.h"
Andreas Huber2bfdd422011-10-11 15:24:07 -070034#include "RTSPSource.h"
Andreas Huber5bc087c2010-12-23 10:27:40 -080035#include "StreamingSource.h"
Andreas Huberafed0e12011-09-20 15:39:58 -070036#include "GenericSource.h"
Robert Shihd3b0bbb2014-07-23 15:00:25 -070037#include "TextDescriptions.h"
Andreas Huber5bc087c2010-12-23 10:27:40 -080038
39#include "ATSParser.h"
Andreas Huberf9334412010-12-15 15:17:42 -080040
Lajos Molnard9fd6312014-11-06 11:00:00 -080041#include <cutils/properties.h>
42
Lajos Molnar3a474aa2015-04-24 17:10:07 -070043#include <media/AudioResamplerPublic.h>
44#include <media/AVSyncSettings.h>
Wonsik Kim7e34bf52016-08-23 00:09:18 +090045#include <media/MediaCodecBuffer.h>
Lajos Molnar3a474aa2015-04-24 17:10:07 -070046
Andreas Huber3831a062010-12-21 10:22:33 -080047#include <media/stagefright/foundation/hexdump.h>
Andreas Huberf9334412010-12-15 15:17:42 -080048#include <media/stagefright/foundation/ABuffer.h>
49#include <media/stagefright/foundation/ADebug.h>
50#include <media/stagefright/foundation/AMessage.h>
Dongwon Kangd91dc5a2017-10-10 00:07:09 -070051#include <media/stagefright/foundation/avc_utils.h>
Lajos Molnar09524832014-07-17 14:29:51 -070052#include <media/stagefright/MediaBuffer.h>
Wei Jia0a68f662017-08-30 18:01:26 -070053#include <media/stagefright/MediaClock.h>
Andreas Huber3fe62152011-09-16 15:09:22 -070054#include <media/stagefright/MediaDefs.h>
Andreas Huberf9334412010-12-15 15:17:42 -080055#include <media/stagefright/MediaErrors.h>
56#include <media/stagefright/MetaData.h>
Lajos Molnar1de1e252015-04-30 18:18:34 -070057
Andy McFadden8ba01022012-12-18 09:46:54 -080058#include <gui/IGraphicBufferProducer.h>
Lajos Molnar1de1e252015-04-30 18:18:34 -070059#include <gui/Surface.h>
Andreas Huberf9334412010-12-15 15:17:42 -080060
Andreas Huber3fe62152011-09-16 15:09:22 -070061
Andreas Huber84066782011-08-16 09:34:26 -070062#include "ESDS.h"
63#include <media/stagefright/Utils.h>
64
Andreas Huberf9334412010-12-15 15:17:42 -080065namespace android {
66
Andreas Hubera1f8ab02012-11-30 10:53:22 -080067struct NuPlayer::Action : public RefBase {
68 Action() {}
69
70 virtual void execute(NuPlayer *player) = 0;
71
72private:
73 DISALLOW_EVIL_CONSTRUCTORS(Action);
74};
75
76struct NuPlayer::SeekAction : public Action {
Wei Jiac5de0912016-11-18 10:22:14 -080077 explicit SeekAction(int64_t seekTimeUs, MediaPlayerSeekMode mode)
Wei Jia14486822016-11-02 17:51:30 -070078 : mSeekTimeUs(seekTimeUs),
Wei Jiac5de0912016-11-18 10:22:14 -080079 mMode(mode) {
Andreas Hubera1f8ab02012-11-30 10:53:22 -080080 }
81
82 virtual void execute(NuPlayer *player) {
Wei Jiac5de0912016-11-18 10:22:14 -080083 player->performSeek(mSeekTimeUs, mMode);
Andreas Hubera1f8ab02012-11-30 10:53:22 -080084 }
85
86private:
87 int64_t mSeekTimeUs;
Wei Jiac5de0912016-11-18 10:22:14 -080088 MediaPlayerSeekMode mMode;
Andreas Hubera1f8ab02012-11-30 10:53:22 -080089
90 DISALLOW_EVIL_CONSTRUCTORS(SeekAction);
91};
92
Chong Zhangf8d71772014-11-26 15:08:34 -080093struct NuPlayer::ResumeDecoderAction : public Action {
Chih-Hung Hsieh090ef602016-04-27 10:39:54 -070094 explicit ResumeDecoderAction(bool needNotify)
Chong Zhangf8d71772014-11-26 15:08:34 -080095 : mNeedNotify(needNotify) {
96 }
97
98 virtual void execute(NuPlayer *player) {
99 player->performResumeDecoders(mNeedNotify);
100 }
101
102private:
103 bool mNeedNotify;
104
105 DISALLOW_EVIL_CONSTRUCTORS(ResumeDecoderAction);
106};
107
Andreas Huber57a339c2012-12-03 11:18:00 -0800108struct NuPlayer::SetSurfaceAction : public Action {
Chih-Hung Hsieh090ef602016-04-27 10:39:54 -0700109 explicit SetSurfaceAction(const sp<Surface> &surface)
Lajos Molnar1de1e252015-04-30 18:18:34 -0700110 : mSurface(surface) {
Andreas Huber57a339c2012-12-03 11:18:00 -0800111 }
112
113 virtual void execute(NuPlayer *player) {
Lajos Molnar1de1e252015-04-30 18:18:34 -0700114 player->performSetSurface(mSurface);
Andreas Huber57a339c2012-12-03 11:18:00 -0800115 }
116
117private:
Lajos Molnar1de1e252015-04-30 18:18:34 -0700118 sp<Surface> mSurface;
Andreas Huber57a339c2012-12-03 11:18:00 -0800119
120 DISALLOW_EVIL_CONSTRUCTORS(SetSurfaceAction);
121};
122
Wei Jiafef808d2014-10-31 17:57:05 -0700123struct NuPlayer::FlushDecoderAction : public Action {
124 FlushDecoderAction(FlushCommand audio, FlushCommand video)
Andreas Huber14f76722013-01-15 09:04:18 -0800125 : mAudio(audio),
126 mVideo(video) {
127 }
128
129 virtual void execute(NuPlayer *player) {
Wei Jiafef808d2014-10-31 17:57:05 -0700130 player->performDecoderFlush(mAudio, mVideo);
Andreas Huber14f76722013-01-15 09:04:18 -0800131 }
132
133private:
Wei Jiafef808d2014-10-31 17:57:05 -0700134 FlushCommand mAudio;
135 FlushCommand mVideo;
Andreas Huber14f76722013-01-15 09:04:18 -0800136
Wei Jiafef808d2014-10-31 17:57:05 -0700137 DISALLOW_EVIL_CONSTRUCTORS(FlushDecoderAction);
Andreas Huber14f76722013-01-15 09:04:18 -0800138};
139
140struct NuPlayer::PostMessageAction : public Action {
Chih-Hung Hsieh090ef602016-04-27 10:39:54 -0700141 explicit PostMessageAction(const sp<AMessage> &msg)
Andreas Huber14f76722013-01-15 09:04:18 -0800142 : mMessage(msg) {
143 }
144
145 virtual void execute(NuPlayer *) {
146 mMessage->post();
147 }
148
149private:
150 sp<AMessage> mMessage;
151
152 DISALLOW_EVIL_CONSTRUCTORS(PostMessageAction);
153};
154
Andreas Hubera1f8ab02012-11-30 10:53:22 -0800155// Use this if there's no state necessary to save in order to execute
156// the action.
157struct NuPlayer::SimpleAction : public Action {
158 typedef void (NuPlayer::*ActionFunc)();
159
Chih-Hung Hsieh090ef602016-04-27 10:39:54 -0700160 explicit SimpleAction(ActionFunc func)
Andreas Hubera1f8ab02012-11-30 10:53:22 -0800161 : mFunc(func) {
162 }
163
164 virtual void execute(NuPlayer *player) {
165 (player->*mFunc)();
166 }
167
168private:
169 ActionFunc mFunc;
170
171 DISALLOW_EVIL_CONSTRUCTORS(SimpleAction);
172};
173
Andreas Huberf9334412010-12-15 15:17:42 -0800174////////////////////////////////////////////////////////////////////////////////
175
Wei Jia0a68f662017-08-30 18:01:26 -0700176NuPlayer::NuPlayer(pid_t pid, const sp<MediaClock> &mediaClock)
Andreas Huber9b80c2b2011-06-30 15:47:02 -0700177 : mUIDValid(false),
Ronghua Wu68845c12015-07-21 09:50:48 -0700178 mPID(pid),
Wei Jia0a68f662017-08-30 18:01:26 -0700179 mMediaClock(mediaClock),
Andreas Huber9575c962013-02-05 13:59:56 -0800180 mSourceFlags(0),
Wei Jiabc2fb722014-07-08 16:37:57 -0700181 mOffloadAudio(false),
Wei Jia88703c32014-08-06 11:24:07 -0700182 mAudioDecoderGeneration(0),
183 mVideoDecoderGeneration(0),
Wei Jia57568df2014-09-22 10:16:29 -0700184 mRendererGeneration(0),
Ray Essick0d98c182017-11-09 13:42:42 -0800185 mLastStartedPlayingTimeNs(0),
Ray Essickeda32522018-02-28 12:08:28 -0800186 mLastStartedRebufferingTimeNs(0),
Robert Shih1a5c8592015-08-04 18:07:44 -0700187 mPreviousSeekTimeUs(0),
Andreas Huber9b80c2b2011-06-30 15:47:02 -0700188 mAudioEOS(false),
Andreas Huberf9334412010-12-15 15:17:42 -0800189 mVideoEOS(false),
Andreas Huber5bc087c2010-12-23 10:27:40 -0800190 mScanSourcesPending(false),
Andreas Huber1aef2112011-01-04 14:01:29 -0800191 mScanSourcesGeneration(0),
Andreas Huberb7c8e912012-11-27 15:02:53 -0800192 mPollDurationGeneration(0),
Robert Shihd3b0bbb2014-07-23 15:00:25 -0700193 mTimedTextGeneration(0),
Andreas Huberf9334412010-12-15 15:17:42 -0800194 mFlushingAudio(NONE),
Andreas Huber1aef2112011-01-04 14:01:29 -0800195 mFlushingVideo(NONE),
Chong Zhangf8d71772014-11-26 15:08:34 -0800196 mResumePending(false),
Andreas Huber57a339c2012-12-03 11:18:00 -0800197 mVideoScalingMode(NATIVE_WINDOW_SCALING_MODE_SCALE_TO_WINDOW),
Lajos Molnar3a474aa2015-04-24 17:10:07 -0700198 mPlaybackSettings(AUDIO_PLAYBACK_RATE_DEFAULT),
199 mVideoFpsHint(-1.f),
Chong Zhangefbb6192015-01-30 17:13:27 -0800200 mStarted(false),
Wei Jia8a092d32016-06-03 14:57:24 -0700201 mPrepared(false),
Ronghua Wu64c2d172015-10-07 16:52:19 -0700202 mResetting(false),
Robert Shih0c61a0d2015-07-06 15:09:10 -0700203 mSourceStarted(false),
Wei Jia686e8e52017-04-03 14:08:01 -0700204 mAudioDecoderError(false),
205 mVideoDecoderError(false),
Chong Zhangefbb6192015-01-30 17:13:27 -0800206 mPaused(false),
Wei Jia71c75e02016-02-04 09:40:47 -0800207 mPausedByClient(true),
Hassan Shojania50b20c92017-02-16 18:28:58 -0800208 mPausedForBuffering(false),
Hassan Shojaniaff63de72017-04-26 15:10:42 -0700209 mIsDrmProtected(false),
210 mDataSourceType(DATA_SOURCE_TYPE_NONE) {
Wei Jia0a68f662017-08-30 18:01:26 -0700211 CHECK(mediaClock != NULL);
Andy Hung8d121d42014-10-03 09:53:53 -0700212 clearFlushComplete();
Andreas Huberf9334412010-12-15 15:17:42 -0800213}
214
215NuPlayer::~NuPlayer() {
216}
217
Andreas Huber9b80c2b2011-06-30 15:47:02 -0700218void NuPlayer::setUID(uid_t uid) {
219 mUIDValid = true;
220 mUID = uid;
221}
222
Dongwon Kang47afe0a2018-03-27 15:15:30 -0700223void NuPlayer::init(const wp<NuPlayerDriver> &driver) {
Andreas Huber43c3e6c2011-01-05 12:17:08 -0800224 mDriver = driver;
Dongwon Kang47afe0a2018-03-27 15:15:30 -0700225
226 sp<AMessage> notify = new AMessage(kWhatMediaClockNotify, this);
227 mMediaClock->setNotificationMessage(notify);
Andreas Huberf9334412010-12-15 15:17:42 -0800228}
229
Andreas Huber9575c962013-02-05 13:59:56 -0800230void NuPlayer::setDataSourceAsync(const sp<IStreamSource> &source) {
Lajos Molnar1d15ab52015-03-04 16:46:34 -0800231 sp<AMessage> msg = new AMessage(kWhatSetDataSource, this);
Andreas Huberf9334412010-12-15 15:17:42 -0800232
Lajos Molnar1d15ab52015-03-04 16:46:34 -0800233 sp<AMessage> notify = new AMessage(kWhatSourceNotify, this);
Andreas Huberb5f25f02013-02-05 10:14:26 -0800234
Andreas Huber240abcc2014-02-13 13:32:37 -0800235 msg->setObject("source", new StreamingSource(notify, source));
Andreas Huber5bc087c2010-12-23 10:27:40 -0800236 msg->post();
Hassan Shojaniaff63de72017-04-26 15:10:42 -0700237 mDataSourceType = DATA_SOURCE_TYPE_STREAM;
Andreas Huber5bc087c2010-12-23 10:27:40 -0800238}
Andreas Huberf9334412010-12-15 15:17:42 -0800239
Andreas Huberafed0e12011-09-20 15:39:58 -0700240static bool IsHTTPLiveURL(const char *url) {
241 if (!strncasecmp("http://", url, 7)
Andreas Huber99759402013-04-01 14:28:31 -0700242 || !strncasecmp("https://", url, 8)
243 || !strncasecmp("file://", url, 7)) {
Andreas Huberafed0e12011-09-20 15:39:58 -0700244 size_t len = strlen(url);
245 if (len >= 5 && !strcasecmp(".m3u8", &url[len - 5])) {
246 return true;
247 }
248
249 if (strstr(url,"m3u8")) {
250 return true;
251 }
252 }
253
254 return false;
255}
256
Andreas Huber9575c962013-02-05 13:59:56 -0800257void NuPlayer::setDataSourceAsync(
Andreas Huber1b86fe02014-01-29 11:13:26 -0800258 const sp<IMediaHTTPService> &httpService,
259 const char *url,
260 const KeyedVector<String8, String8> *headers) {
Chong Zhang3de157d2014-08-05 20:54:44 -0700261
Lajos Molnar1d15ab52015-03-04 16:46:34 -0800262 sp<AMessage> msg = new AMessage(kWhatSetDataSource, this);
Oscar Rydhé7a33b772012-02-20 10:15:48 +0100263 size_t len = strlen(url);
Andreas Huber5bc087c2010-12-23 10:27:40 -0800264
Lajos Molnar1d15ab52015-03-04 16:46:34 -0800265 sp<AMessage> notify = new AMessage(kWhatSourceNotify, this);
Andreas Huberb5f25f02013-02-05 10:14:26 -0800266
Andreas Huberafed0e12011-09-20 15:39:58 -0700267 sp<Source> source;
268 if (IsHTTPLiveURL(url)) {
Andreas Huber81e68442014-02-05 11:52:33 -0800269 source = new HTTPLiveSource(notify, httpService, url, headers);
Hassan Shojaniacefac142017-02-06 21:02:02 -0800270 ALOGV("setDataSourceAsync HTTPLiveSource %s", url);
Hassan Shojaniaff63de72017-04-26 15:10:42 -0700271 mDataSourceType = DATA_SOURCE_TYPE_HTTP_LIVE;
Andreas Huberafed0e12011-09-20 15:39:58 -0700272 } else if (!strncasecmp(url, "rtsp://", 7)) {
Andreas Huber1b86fe02014-01-29 11:13:26 -0800273 source = new RTSPSource(
274 notify, httpService, url, headers, mUIDValid, mUID);
Hassan Shojaniacefac142017-02-06 21:02:02 -0800275 ALOGV("setDataSourceAsync RTSPSource %s", url);
Hassan Shojaniaff63de72017-04-26 15:10:42 -0700276 mDataSourceType = DATA_SOURCE_TYPE_RTSP;
Oscar Rydhé7a33b772012-02-20 10:15:48 +0100277 } else if ((!strncasecmp(url, "http://", 7)
278 || !strncasecmp(url, "https://", 8))
279 && ((len >= 4 && !strcasecmp(".sdp", &url[len - 4]))
280 || strstr(url, ".sdp?"))) {
Andreas Huber1b86fe02014-01-29 11:13:26 -0800281 source = new RTSPSource(
282 notify, httpService, url, headers, mUIDValid, mUID, true);
Hassan Shojaniacefac142017-02-06 21:02:02 -0800283 ALOGV("setDataSourceAsync RTSPSource http/https/.sdp %s", url);
Hassan Shojaniaff63de72017-04-26 15:10:42 -0700284 mDataSourceType = DATA_SOURCE_TYPE_RTSP;
Andreas Huber2bfdd422011-10-11 15:24:07 -0700285 } else {
Hassan Shojaniacefac142017-02-06 21:02:02 -0800286 ALOGV("setDataSourceAsync GenericSource %s", url);
287
Chong Zhang3de157d2014-08-05 20:54:44 -0700288 sp<GenericSource> genericSource =
Wei Jia992c5592017-09-01 14:20:23 -0700289 new GenericSource(notify, mUIDValid, mUID, mMediaClock);
Andreas Huber2bfdd422011-10-11 15:24:07 -0700290
Chong Zhanga19f33e2014-08-07 15:35:07 -0700291 status_t err = genericSource->setDataSource(httpService, url, headers);
Chong Zhang3de157d2014-08-05 20:54:44 -0700292
293 if (err == OK) {
294 source = genericSource;
295 } else {
Chong Zhanga19f33e2014-08-07 15:35:07 -0700296 ALOGE("Failed to set data source!");
Chong Zhang3de157d2014-08-05 20:54:44 -0700297 }
Hassan Shojaniaff63de72017-04-26 15:10:42 -0700298
299 // regardless of success/failure
300 mDataSourceType = DATA_SOURCE_TYPE_GENERIC_URL;
Chong Zhang3de157d2014-08-05 20:54:44 -0700301 }
Andreas Huberafed0e12011-09-20 15:39:58 -0700302 msg->setObject("source", source);
303 msg->post();
304}
305
Andreas Huber9575c962013-02-05 13:59:56 -0800306void NuPlayer::setDataSourceAsync(int fd, int64_t offset, int64_t length) {
Lajos Molnar1d15ab52015-03-04 16:46:34 -0800307 sp<AMessage> msg = new AMessage(kWhatSetDataSource, this);
Andreas Huberafed0e12011-09-20 15:39:58 -0700308
Lajos Molnar1d15ab52015-03-04 16:46:34 -0800309 sp<AMessage> notify = new AMessage(kWhatSourceNotify, this);
Andreas Huberb5f25f02013-02-05 10:14:26 -0800310
Chong Zhang3de157d2014-08-05 20:54:44 -0700311 sp<GenericSource> source =
Wei Jia992c5592017-09-01 14:20:23 -0700312 new GenericSource(notify, mUIDValid, mUID, mMediaClock);
Chong Zhang3de157d2014-08-05 20:54:44 -0700313
Hassan Shojaniacefac142017-02-06 21:02:02 -0800314 ALOGV("setDataSourceAsync fd %d/%lld/%lld source: %p",
315 fd, (long long)offset, (long long)length, source.get());
316
Chong Zhanga19f33e2014-08-07 15:35:07 -0700317 status_t err = source->setDataSource(fd, offset, length);
Chong Zhang3de157d2014-08-05 20:54:44 -0700318
319 if (err != OK) {
Chong Zhanga19f33e2014-08-07 15:35:07 -0700320 ALOGE("Failed to set data source!");
Chong Zhang3de157d2014-08-05 20:54:44 -0700321 source = NULL;
322 }
323
Andreas Huberafed0e12011-09-20 15:39:58 -0700324 msg->setObject("source", source);
Andreas Huberf9334412010-12-15 15:17:42 -0800325 msg->post();
Hassan Shojaniaff63de72017-04-26 15:10:42 -0700326 mDataSourceType = DATA_SOURCE_TYPE_GENERIC_FD;
Andreas Huberf9334412010-12-15 15:17:42 -0800327}
328
Chris Watkins99f31602015-03-20 13:06:33 -0700329void NuPlayer::setDataSourceAsync(const sp<DataSource> &dataSource) {
330 sp<AMessage> msg = new AMessage(kWhatSetDataSource, this);
331 sp<AMessage> notify = new AMessage(kWhatSourceNotify, this);
332
Wei Jia992c5592017-09-01 14:20:23 -0700333 sp<GenericSource> source = new GenericSource(notify, mUIDValid, mUID, mMediaClock);
Chris Watkins99f31602015-03-20 13:06:33 -0700334 status_t err = source->setDataSource(dataSource);
335
336 if (err != OK) {
337 ALOGE("Failed to set data source!");
338 source = NULL;
339 }
340
341 msg->setObject("source", source);
342 msg->post();
Hassan Shojaniaff63de72017-04-26 15:10:42 -0700343 mDataSourceType = DATA_SOURCE_TYPE_MEDIA;
Chris Watkins99f31602015-03-20 13:06:33 -0700344}
345
Wei Jia9bb38032017-03-23 18:00:38 -0700346status_t NuPlayer::getBufferingSettings(
Wei Jia48fa06d2016-12-20 15:30:49 -0800347 BufferingSettings *buffering /* nonnull */) {
Wei Jia9bb38032017-03-23 18:00:38 -0700348 sp<AMessage> msg = new AMessage(kWhatGetBufferingSettings, this);
Wei Jia48fa06d2016-12-20 15:30:49 -0800349 sp<AMessage> response;
350 status_t err = msg->postAndAwaitResponse(&response);
351 if (err == OK && response != NULL) {
352 CHECK(response->findInt32("err", &err));
353 if (err == OK) {
354 readFromAMessage(response, buffering);
355 }
356 }
357 return err;
358}
359
360status_t NuPlayer::setBufferingSettings(const BufferingSettings& buffering) {
361 sp<AMessage> msg = new AMessage(kWhatSetBufferingSettings, this);
362 writeToAMessage(msg, buffering);
363 sp<AMessage> response;
364 status_t err = msg->postAndAwaitResponse(&response);
365 if (err == OK && response != NULL) {
366 CHECK(response->findInt32("err", &err));
367 }
368 return err;
369}
370
Andreas Huber9575c962013-02-05 13:59:56 -0800371void NuPlayer::prepareAsync() {
Hassan Shojaniacefac142017-02-06 21:02:02 -0800372 ALOGV("prepareAsync");
373
Lajos Molnar1d15ab52015-03-04 16:46:34 -0800374 (new AMessage(kWhatPrepare, this))->post();
Andreas Huber9575c962013-02-05 13:59:56 -0800375}
376
Andreas Huber57a339c2012-12-03 11:18:00 -0800377void NuPlayer::setVideoSurfaceTextureAsync(
Andy McFadden8ba01022012-12-18 09:46:54 -0800378 const sp<IGraphicBufferProducer> &bufferProducer) {
Lajos Molnar1de1e252015-04-30 18:18:34 -0700379 sp<AMessage> msg = new AMessage(kWhatSetVideoSurface, this);
Andreas Huber57a339c2012-12-03 11:18:00 -0800380
Andy McFadden8ba01022012-12-18 09:46:54 -0800381 if (bufferProducer == NULL) {
Lajos Molnar1de1e252015-04-30 18:18:34 -0700382 msg->setObject("surface", NULL);
Andreas Huber57a339c2012-12-03 11:18:00 -0800383 } else {
Lajos Molnar1de1e252015-04-30 18:18:34 -0700384 msg->setObject("surface", new Surface(bufferProducer, true /* controlledByApp */));
Andreas Huber57a339c2012-12-03 11:18:00 -0800385 }
386
Andreas Huberf9334412010-12-15 15:17:42 -0800387 msg->post();
388}
389
390void NuPlayer::setAudioSink(const sp<MediaPlayerBase::AudioSink> &sink) {
Lajos Molnar1d15ab52015-03-04 16:46:34 -0800391 sp<AMessage> msg = new AMessage(kWhatSetAudioSink, this);
Andreas Huberf9334412010-12-15 15:17:42 -0800392 msg->setObject("sink", sink);
393 msg->post();
394}
395
396void NuPlayer::start() {
Lajos Molnar1d15ab52015-03-04 16:46:34 -0800397 (new AMessage(kWhatStart, this))->post();
Andreas Huberf9334412010-12-15 15:17:42 -0800398}
399
Lajos Molnar3a474aa2015-04-24 17:10:07 -0700400status_t NuPlayer::setPlaybackSettings(const AudioPlaybackRate &rate) {
401 // do some cursory validation of the settings here. audio modes are
402 // only validated when set on the audiosink.
403 if ((rate.mSpeed != 0.f && rate.mSpeed < AUDIO_TIMESTRETCH_SPEED_MIN)
404 || rate.mSpeed > AUDIO_TIMESTRETCH_SPEED_MAX
405 || rate.mPitch < AUDIO_TIMESTRETCH_SPEED_MIN
406 || rate.mPitch > AUDIO_TIMESTRETCH_SPEED_MAX) {
407 return BAD_VALUE;
408 }
409 sp<AMessage> msg = new AMessage(kWhatConfigPlayback, this);
410 writeToAMessage(msg, rate);
411 sp<AMessage> response;
412 status_t err = msg->postAndAwaitResponse(&response);
413 if (err == OK && response != NULL) {
414 CHECK(response->findInt32("err", &err));
415 }
416 return err;
417}
418
419status_t NuPlayer::getPlaybackSettings(AudioPlaybackRate *rate /* nonnull */) {
420 sp<AMessage> msg = new AMessage(kWhatGetPlaybackSettings, this);
421 sp<AMessage> response;
422 status_t err = msg->postAndAwaitResponse(&response);
423 if (err == OK && response != NULL) {
424 CHECK(response->findInt32("err", &err));
425 if (err == OK) {
426 readFromAMessage(response, rate);
427 }
428 }
429 return err;
430}
431
432status_t NuPlayer::setSyncSettings(const AVSyncSettings &sync, float videoFpsHint) {
433 sp<AMessage> msg = new AMessage(kWhatConfigSync, this);
434 writeToAMessage(msg, sync, videoFpsHint);
435 sp<AMessage> response;
436 status_t err = msg->postAndAwaitResponse(&response);
437 if (err == OK && response != NULL) {
438 CHECK(response->findInt32("err", &err));
439 }
440 return err;
441}
442
443status_t NuPlayer::getSyncSettings(
444 AVSyncSettings *sync /* nonnull */, float *videoFps /* nonnull */) {
445 sp<AMessage> msg = new AMessage(kWhatGetSyncSettings, this);
446 sp<AMessage> response;
447 status_t err = msg->postAndAwaitResponse(&response);
448 if (err == OK && response != NULL) {
449 CHECK(response->findInt32("err", &err));
450 if (err == OK) {
451 readFromAMessage(response, sync, videoFps);
452 }
453 }
454 return err;
Wei Jia98160162015-02-04 17:01:11 -0800455}
456
Andreas Huber43c3e6c2011-01-05 12:17:08 -0800457void NuPlayer::pause() {
Lajos Molnar1d15ab52015-03-04 16:46:34 -0800458 (new AMessage(kWhatPause, this))->post();
Andreas Huber43c3e6c2011-01-05 12:17:08 -0800459}
460
Andreas Huber1aef2112011-01-04 14:01:29 -0800461void NuPlayer::resetAsync() {
Wei Jiac45a4b22016-04-15 15:30:23 -0700462 sp<Source> source;
463 {
464 Mutex::Autolock autoLock(mSourceLock);
465 source = mSource;
466 }
467
468 if (source != NULL) {
Chong Zhang48296b72014-09-14 14:28:45 -0700469 // During a reset, the data source might be unresponsive already, we need to
470 // disconnect explicitly so that reads exit promptly.
471 // We can't queue the disconnect request to the looper, as it might be
472 // queued behind a stuck read and never gets processed.
473 // Doing a disconnect outside the looper to allows the pending reads to exit
474 // (either successfully or with error).
Wei Jiac45a4b22016-04-15 15:30:23 -0700475 source->disconnect();
Chong Zhang48296b72014-09-14 14:28:45 -0700476 }
477
Lajos Molnar1d15ab52015-03-04 16:46:34 -0800478 (new AMessage(kWhatReset, this))->post();
Andreas Huber1aef2112011-01-04 14:01:29 -0800479}
480
Wei Jia52c28512017-09-13 18:17:51 -0700481status_t NuPlayer::notifyAt(int64_t mediaTimeUs) {
482 sp<AMessage> notify = new AMessage(kWhatNotifyTime, this);
483 notify->setInt64("timerUs", mediaTimeUs);
484 mMediaClock->addTimer(notify, mediaTimeUs);
485 return OK;
486}
487
Wei Jiac5de0912016-11-18 10:22:14 -0800488void NuPlayer::seekToAsync(int64_t seekTimeUs, MediaPlayerSeekMode mode, bool needNotify) {
Lajos Molnar1d15ab52015-03-04 16:46:34 -0800489 sp<AMessage> msg = new AMessage(kWhatSeek, this);
Andreas Huber43c3e6c2011-01-05 12:17:08 -0800490 msg->setInt64("seekTimeUs", seekTimeUs);
Wei Jiac5de0912016-11-18 10:22:14 -0800491 msg->setInt32("mode", mode);
Wei Jiae427abf2014-09-22 15:21:11 -0700492 msg->setInt32("needNotify", needNotify);
Andreas Huber43c3e6c2011-01-05 12:17:08 -0800493 msg->post();
494}
495
Andreas Huber53df1a42010-12-22 10:03:04 -0800496
Chong Zhang404fced2014-06-11 14:45:31 -0700497void NuPlayer::writeTrackInfo(
Chih-Hung Hsiehe964d4e2016-08-09 14:31:32 -0700498 Parcel* reply, const sp<AMessage>& format) const {
Marco Nelissenc367ca12015-09-15 09:51:59 -0700499 if (format == NULL) {
500 ALOGE("NULL format");
501 return;
502 }
Chong Zhang404fced2014-06-11 14:45:31 -0700503 int32_t trackType;
Marco Nelissen9436e482015-09-15 10:21:53 -0700504 if (!format->findInt32("type", &trackType)) {
Marco Nelissenc367ca12015-09-15 09:51:59 -0700505 ALOGE("no track type");
506 return;
507 }
Chong Zhang404fced2014-06-11 14:45:31 -0700508
Robert Shih755106e2015-04-30 14:36:45 -0700509 AString mime;
Robert Shih2e3a4252015-05-06 10:21:15 -0700510 if (!format->findString("mime", &mime)) {
511 // Java MediaPlayer only uses mimetype for subtitle and timedtext tracks.
512 // If we can't find the mimetype here it means that we wouldn't be needing
513 // the mimetype on the Java end. We still write a placeholder mime to keep the
514 // (de)serialization logic simple.
515 if (trackType == MEDIA_TRACK_TYPE_AUDIO) {
516 mime = "audio/";
517 } else if (trackType == MEDIA_TRACK_TYPE_VIDEO) {
518 mime = "video/";
519 } else {
Marco Nelissenc367ca12015-09-15 09:51:59 -0700520 ALOGE("unknown track type: %d", trackType);
521 return;
Robert Shih2e3a4252015-05-06 10:21:15 -0700522 }
523 }
Robert Shih755106e2015-04-30 14:36:45 -0700524
Chong Zhang404fced2014-06-11 14:45:31 -0700525 AString lang;
Marco Nelissen9436e482015-09-15 10:21:53 -0700526 if (!format->findString("language", &lang)) {
Marco Nelissenc367ca12015-09-15 09:51:59 -0700527 ALOGE("no language");
528 return;
529 }
Chong Zhang404fced2014-06-11 14:45:31 -0700530
531 reply->writeInt32(2); // write something non-zero
532 reply->writeInt32(trackType);
Robert Shih755106e2015-04-30 14:36:45 -0700533 reply->writeString16(String16(mime.c_str()));
Chong Zhang404fced2014-06-11 14:45:31 -0700534 reply->writeString16(String16(lang.c_str()));
535
536 if (trackType == MEDIA_TRACK_TYPE_SUBTITLE) {
Chong Zhang404fced2014-06-11 14:45:31 -0700537 int32_t isAuto, isDefault, isForced;
538 CHECK(format->findInt32("auto", &isAuto));
539 CHECK(format->findInt32("default", &isDefault));
540 CHECK(format->findInt32("forced", &isForced));
541
Chong Zhang404fced2014-06-11 14:45:31 -0700542 reply->writeInt32(isAuto);
543 reply->writeInt32(isDefault);
544 reply->writeInt32(isForced);
545 }
546}
547
Andreas Huberf9334412010-12-15 15:17:42 -0800548void NuPlayer::onMessageReceived(const sp<AMessage> &msg) {
549 switch (msg->what()) {
550 case kWhatSetDataSource:
551 {
Steve Block3856b092011-10-20 11:56:00 +0100552 ALOGV("kWhatSetDataSource");
Andreas Huberf9334412010-12-15 15:17:42 -0800553
554 CHECK(mSource == NULL);
555
Chong Zhang3de157d2014-08-05 20:54:44 -0700556 status_t err = OK;
Andreas Huber5bc087c2010-12-23 10:27:40 -0800557 sp<RefBase> obj;
558 CHECK(msg->findObject("source", &obj));
Chong Zhang3de157d2014-08-05 20:54:44 -0700559 if (obj != NULL) {
Wei Jiac45a4b22016-04-15 15:30:23 -0700560 Mutex::Autolock autoLock(mSourceLock);
Chong Zhang3de157d2014-08-05 20:54:44 -0700561 mSource = static_cast<Source *>(obj.get());
Chong Zhang3de157d2014-08-05 20:54:44 -0700562 } else {
563 err = UNKNOWN_ERROR;
564 }
Andreas Huber9575c962013-02-05 13:59:56 -0800565
566 CHECK(mDriver != NULL);
567 sp<NuPlayerDriver> driver = mDriver.promote();
568 if (driver != NULL) {
Chong Zhang3de157d2014-08-05 20:54:44 -0700569 driver->notifySetDataSourceCompleted(err);
Andreas Huber9575c962013-02-05 13:59:56 -0800570 }
571 break;
572 }
573
Wei Jia9bb38032017-03-23 18:00:38 -0700574 case kWhatGetBufferingSettings:
Wei Jia48fa06d2016-12-20 15:30:49 -0800575 {
576 sp<AReplyToken> replyID;
577 CHECK(msg->senderAwaitsResponse(&replyID));
578
Wei Jia9bb38032017-03-23 18:00:38 -0700579 ALOGV("kWhatGetBufferingSettings");
Wei Jia48fa06d2016-12-20 15:30:49 -0800580 BufferingSettings buffering;
581 status_t err = OK;
582 if (mSource != NULL) {
Wei Jia9bb38032017-03-23 18:00:38 -0700583 err = mSource->getBufferingSettings(&buffering);
Wei Jia48fa06d2016-12-20 15:30:49 -0800584 } else {
585 err = INVALID_OPERATION;
586 }
587 sp<AMessage> response = new AMessage;
588 if (err == OK) {
589 writeToAMessage(response, buffering);
590 }
591 response->setInt32("err", err);
592 response->postReply(replyID);
593 break;
594 }
595
596 case kWhatSetBufferingSettings:
597 {
598 sp<AReplyToken> replyID;
599 CHECK(msg->senderAwaitsResponse(&replyID));
600
601 ALOGV("kWhatSetBufferingSettings");
602 BufferingSettings buffering;
603 readFromAMessage(msg, &buffering);
604 status_t err = OK;
605 if (mSource != NULL) {
606 err = mSource->setBufferingSettings(buffering);
607 } else {
608 err = INVALID_OPERATION;
609 }
610 sp<AMessage> response = new AMessage;
611 response->setInt32("err", err);
612 response->postReply(replyID);
613 break;
614 }
615
Andreas Huber9575c962013-02-05 13:59:56 -0800616 case kWhatPrepare:
617 {
Hassan Shojaniacefac142017-02-06 21:02:02 -0800618 ALOGV("onMessageReceived kWhatPrepare");
619
Andreas Huber9575c962013-02-05 13:59:56 -0800620 mSource->prepareAsync();
Andreas Huberf9334412010-12-15 15:17:42 -0800621 break;
622 }
623
Chong Zhangdcb89b32013-08-06 09:44:47 -0700624 case kWhatGetTrackInfo:
625 {
Lajos Molnar3f274362015-03-05 14:35:41 -0800626 sp<AReplyToken> replyID;
Chong Zhangdcb89b32013-08-06 09:44:47 -0700627 CHECK(msg->senderAwaitsResponse(&replyID));
628
Chong Zhang404fced2014-06-11 14:45:31 -0700629 Parcel* reply;
630 CHECK(msg->findPointer("reply", (void**)&reply));
631
632 size_t inbandTracks = 0;
Chong Zhangdcb89b32013-08-06 09:44:47 -0700633 if (mSource != NULL) {
Chong Zhang404fced2014-06-11 14:45:31 -0700634 inbandTracks = mSource->getTrackCount();
635 }
636
Chong Zhanga7fa1d92014-06-11 14:49:23 -0700637 size_t ccTracks = 0;
638 if (mCCDecoder != NULL) {
639 ccTracks = mCCDecoder->getTrackCount();
640 }
641
Chong Zhang404fced2014-06-11 14:45:31 -0700642 // total track count
Chong Zhanga7fa1d92014-06-11 14:49:23 -0700643 reply->writeInt32(inbandTracks + ccTracks);
Chong Zhang404fced2014-06-11 14:45:31 -0700644
645 // write inband tracks
646 for (size_t i = 0; i < inbandTracks; ++i) {
647 writeTrackInfo(reply, mSource->getTrackInfo(i));
Chong Zhangdcb89b32013-08-06 09:44:47 -0700648 }
649
Chong Zhanga7fa1d92014-06-11 14:49:23 -0700650 // write CC track
651 for (size_t i = 0; i < ccTracks; ++i) {
652 writeTrackInfo(reply, mCCDecoder->getTrackInfo(i));
653 }
654
Chong Zhangdcb89b32013-08-06 09:44:47 -0700655 sp<AMessage> response = new AMessage;
Chong Zhangdcb89b32013-08-06 09:44:47 -0700656 response->postReply(replyID);
657 break;
658 }
659
Robert Shih7c4f0d72014-07-09 18:53:31 -0700660 case kWhatGetSelectedTrack:
661 {
Wei Jia039224f2018-11-29 17:25:17 -0800662 int32_t type32;
663 CHECK(msg->findInt32("type", (int32_t*)&type32));
664 media_track_type type = (media_track_type)type32;
665
666 size_t inbandTracks = 0;
Robert Shih7c4f0d72014-07-09 18:53:31 -0700667 status_t err = INVALID_OPERATION;
Wei Jia039224f2018-11-29 17:25:17 -0800668 ssize_t selectedTrack = -1;
Robert Shih7c4f0d72014-07-09 18:53:31 -0700669 if (mSource != NULL) {
670 err = OK;
Wei Jia039224f2018-11-29 17:25:17 -0800671 inbandTracks = mSource->getTrackCount();
672 selectedTrack = mSource->getSelectedTrack(type);
Robert Shih7c4f0d72014-07-09 18:53:31 -0700673 }
674
Wei Jia039224f2018-11-29 17:25:17 -0800675 if (selectedTrack == -1 && mCCDecoder != NULL) {
676 err = OK;
677 selectedTrack = mCCDecoder->getSelectedTrack(type);
678 if (selectedTrack != -1) {
679 selectedTrack += inbandTracks;
680 }
681 }
682
683 Parcel* reply;
684 CHECK(msg->findPointer("reply", (void**)&reply));
685 reply->writeInt32(selectedTrack);
686
Robert Shih7c4f0d72014-07-09 18:53:31 -0700687 sp<AMessage> response = new AMessage;
688 response->setInt32("err", err);
689
Lajos Molnar3f274362015-03-05 14:35:41 -0800690 sp<AReplyToken> replyID;
Robert Shih7c4f0d72014-07-09 18:53:31 -0700691 CHECK(msg->senderAwaitsResponse(&replyID));
692 response->postReply(replyID);
693 break;
694 }
695
Chong Zhangdcb89b32013-08-06 09:44:47 -0700696 case kWhatSelectTrack:
697 {
Lajos Molnar3f274362015-03-05 14:35:41 -0800698 sp<AReplyToken> replyID;
Chong Zhangdcb89b32013-08-06 09:44:47 -0700699 CHECK(msg->senderAwaitsResponse(&replyID));
700
Chong Zhang404fced2014-06-11 14:45:31 -0700701 size_t trackIndex;
702 int32_t select;
Robert Shih6ffb1fd2014-10-29 16:24:32 -0700703 int64_t timeUs;
Chong Zhang404fced2014-06-11 14:45:31 -0700704 CHECK(msg->findSize("trackIndex", &trackIndex));
705 CHECK(msg->findInt32("select", &select));
Robert Shih6ffb1fd2014-10-29 16:24:32 -0700706 CHECK(msg->findInt64("timeUs", &timeUs));
Chong Zhang404fced2014-06-11 14:45:31 -0700707
Chong Zhangdcb89b32013-08-06 09:44:47 -0700708 status_t err = INVALID_OPERATION;
Chong Zhang404fced2014-06-11 14:45:31 -0700709
710 size_t inbandTracks = 0;
Chong Zhangdcb89b32013-08-06 09:44:47 -0700711 if (mSource != NULL) {
Chong Zhang404fced2014-06-11 14:45:31 -0700712 inbandTracks = mSource->getTrackCount();
713 }
Chong Zhanga7fa1d92014-06-11 14:49:23 -0700714 size_t ccTracks = 0;
715 if (mCCDecoder != NULL) {
716 ccTracks = mCCDecoder->getTrackCount();
717 }
Chong Zhang404fced2014-06-11 14:45:31 -0700718
719 if (trackIndex < inbandTracks) {
Robert Shih6ffb1fd2014-10-29 16:24:32 -0700720 err = mSource->selectTrack(trackIndex, select, timeUs);
Robert Shihd3b0bbb2014-07-23 15:00:25 -0700721
722 if (!select && err == OK) {
723 int32_t type;
724 sp<AMessage> info = mSource->getTrackInfo(trackIndex);
725 if (info != NULL
726 && info->findInt32("type", &type)
727 && type == MEDIA_TRACK_TYPE_TIMEDTEXT) {
728 ++mTimedTextGeneration;
729 }
730 }
Chong Zhanga7fa1d92014-06-11 14:49:23 -0700731 } else {
732 trackIndex -= inbandTracks;
733
734 if (trackIndex < ccTracks) {
735 err = mCCDecoder->selectTrack(trackIndex, select);
736 }
Chong Zhangdcb89b32013-08-06 09:44:47 -0700737 }
738
739 sp<AMessage> response = new AMessage;
740 response->setInt32("err", err);
741
742 response->postReply(replyID);
743 break;
744 }
745
Andreas Huberb7c8e912012-11-27 15:02:53 -0800746 case kWhatPollDuration:
747 {
748 int32_t generation;
749 CHECK(msg->findInt32("generation", &generation));
750
751 if (generation != mPollDurationGeneration) {
752 // stale
753 break;
754 }
755
756 int64_t durationUs;
757 if (mDriver != NULL && mSource->getDuration(&durationUs) == OK) {
758 sp<NuPlayerDriver> driver = mDriver.promote();
759 if (driver != NULL) {
760 driver->notifyDuration(durationUs);
761 }
762 }
763
Chih-Hung Hsieh62309d52018-12-11 13:54:02 -0800764 msg->post(1000000LL); // poll again in a second.
Andreas Huberb7c8e912012-11-27 15:02:53 -0800765 break;
766 }
767
Lajos Molnar1de1e252015-04-30 18:18:34 -0700768 case kWhatSetVideoSurface:
Andreas Huberf9334412010-12-15 15:17:42 -0800769 {
Andreas Huberf9334412010-12-15 15:17:42 -0800770
771 sp<RefBase> obj;
Lajos Molnar1de1e252015-04-30 18:18:34 -0700772 CHECK(msg->findObject("surface", &obj));
773 sp<Surface> surface = static_cast<Surface *>(obj.get());
Lajos Molnara81c6222015-07-10 19:17:45 -0700774
775 ALOGD("onSetVideoSurface(%p, %s video decoder)",
776 surface.get(),
Wei Jia6b7d2ed2015-08-10 15:11:54 -0700777 (mSource != NULL && mStarted && mSource->getFormat(false /* audio */) != NULL
Lajos Molnara81c6222015-07-10 19:17:45 -0700778 && mVideoDecoder != NULL) ? "have" : "no");
779
Wei Jia6b7d2ed2015-08-10 15:11:54 -0700780 // Need to check mStarted before calling mSource->getFormat because NuPlayer might
781 // be in preparing state and it could take long time.
782 // When mStarted is true, mSource must have been set.
783 if (mSource == NULL || !mStarted || mSource->getFormat(false /* audio */) == NULL
Lajos Molnara81c6222015-07-10 19:17:45 -0700784 // NOTE: mVideoDecoder's mSurface is always non-null
785 || (mVideoDecoder != NULL && mVideoDecoder->setVideoSurface(surface) == OK)) {
Lajos Molnar1de1e252015-04-30 18:18:34 -0700786 performSetSurface(surface);
Wei Jiafef808d2014-10-31 17:57:05 -0700787 break;
788 }
789
790 mDeferredActions.push_back(
Krystian Turczyn687a5892016-01-20 14:20:35 +0100791 new FlushDecoderAction(
792 (obj != NULL ? FLUSH_CMD_FLUSH : FLUSH_CMD_NONE) /* audio */,
Wei Jiafef808d2014-10-31 17:57:05 -0700793 FLUSH_CMD_SHUTDOWN /* video */));
794
Lajos Molnar1de1e252015-04-30 18:18:34 -0700795 mDeferredActions.push_back(new SetSurfaceAction(surface));
James Dong0d268a32012-08-31 12:18:27 -0700796
Krystian Turczyn687a5892016-01-20 14:20:35 +0100797 if (obj != NULL) {
Wei Jiafef808d2014-10-31 17:57:05 -0700798 if (mStarted) {
Andy Hung73535852014-09-05 11:42:58 -0700799 // Issue a seek to refresh the video screen only if started otherwise
800 // the extractor may not yet be started and will assert.
801 // If the video decoder is not set (perhaps audio only in this case)
802 // do not perform a seek as it is not needed.
Ronghua Wua73d9e02014-10-08 15:13:29 -0700803 int64_t currentPositionUs = 0;
804 if (getCurrentPosition(&currentPositionUs) == OK) {
805 mDeferredActions.push_back(
Wei Jiac5de0912016-11-18 10:22:14 -0800806 new SeekAction(currentPositionUs,
807 MediaPlayerSeekMode::SEEK_PREVIOUS_SYNC /* mode */));
Ronghua Wua73d9e02014-10-08 15:13:29 -0700808 }
Andy Hung73535852014-09-05 11:42:58 -0700809 }
Wei Jiaac428aa2014-09-02 19:01:34 -0700810
Andreas Huber57a339c2012-12-03 11:18:00 -0800811 // If there is a new surface texture, instantiate decoders
812 // again if possible.
813 mDeferredActions.push_back(
814 new SimpleAction(&NuPlayer::performScanSources));
Andreas Huber57a339c2012-12-03 11:18:00 -0800815
Krystian Turczyn687a5892016-01-20 14:20:35 +0100816 // After a flush without shutdown, decoder is paused.
817 // Don't resume it until source seek is done, otherwise it could
818 // start pulling stale data too soon.
819 mDeferredActions.push_back(
820 new ResumeDecoderAction(false /* needNotify */));
821 }
Chong Zhang7137ec72014-11-12 16:41:05 -0800822
Andreas Huber57a339c2012-12-03 11:18:00 -0800823 processDeferredActions();
Andreas Huberf9334412010-12-15 15:17:42 -0800824 break;
825 }
826
827 case kWhatSetAudioSink:
828 {
Steve Block3856b092011-10-20 11:56:00 +0100829 ALOGV("kWhatSetAudioSink");
Andreas Huberf9334412010-12-15 15:17:42 -0800830
831 sp<RefBase> obj;
832 CHECK(msg->findObject("sink", &obj));
833
834 mAudioSink = static_cast<MediaPlayerBase::AudioSink *>(obj.get());
835 break;
836 }
837
838 case kWhatStart:
839 {
Steve Block3856b092011-10-20 11:56:00 +0100840 ALOGV("kWhatStart");
Wei Jia94211742014-10-28 17:09:06 -0700841 if (mStarted) {
Chong Zhang8a048332015-05-06 15:16:28 -0700842 // do not resume yet if the source is still buffering
843 if (!mPausedForBuffering) {
844 onResume();
845 }
Wei Jia94211742014-10-28 17:09:06 -0700846 } else {
847 onStart();
Lajos Molnar09524832014-07-17 14:29:51 -0700848 }
Chong Zhangefbb6192015-01-30 17:13:27 -0800849 mPausedByClient = false;
Andreas Huberf9334412010-12-15 15:17:42 -0800850 break;
851 }
852
Lajos Molnar3a474aa2015-04-24 17:10:07 -0700853 case kWhatConfigPlayback:
Wei Jia98160162015-02-04 17:01:11 -0800854 {
Lajos Molnar3a474aa2015-04-24 17:10:07 -0700855 sp<AReplyToken> replyID;
856 CHECK(msg->senderAwaitsResponse(&replyID));
857 AudioPlaybackRate rate /* sanitized */;
858 readFromAMessage(msg, &rate);
859 status_t err = OK;
Wei Jia98160162015-02-04 17:01:11 -0800860 if (mRenderer != NULL) {
Wei Jiabf70feb2016-02-19 15:47:16 -0800861 // AudioSink allows only 1.f and 0.f for offload mode.
862 // For other speed, switch to non-offload mode.
863 if (mOffloadAudio && ((rate.mSpeed != 0.f && rate.mSpeed != 1.f)
864 || rate.mPitch != 1.f)) {
865 int64_t currentPositionUs;
866 if (getCurrentPosition(&currentPositionUs) != OK) {
867 currentPositionUs = mPreviousSeekTimeUs;
868 }
869
870 // Set mPlaybackSettings so that the new audio decoder can
871 // be created correctly.
872 mPlaybackSettings = rate;
873 if (!mPaused) {
874 mRenderer->pause();
875 }
Wei Jia5031b2f2016-02-25 11:19:31 -0800876 restartAudio(
Wei Jiabf70feb2016-02-19 15:47:16 -0800877 currentPositionUs, true /* forceNonOffload */,
878 true /* needsToCreateAudioDecoder */);
879 if (!mPaused) {
880 mRenderer->resume();
881 }
882 }
883
Lajos Molnar3a474aa2015-04-24 17:10:07 -0700884 err = mRenderer->setPlaybackSettings(rate);
885 }
886 if (err == OK) {
887 if (rate.mSpeed == 0.f) {
888 onPause();
Wei Jia351ce872016-02-10 13:21:59 -0800889 mPausedByClient = true;
Lajos Molnar3a474aa2015-04-24 17:10:07 -0700890 // save all other settings (using non-paused speed)
891 // so we can restore them on start
892 AudioPlaybackRate newRate = rate;
893 newRate.mSpeed = mPlaybackSettings.mSpeed;
894 mPlaybackSettings = newRate;
895 } else { /* rate.mSpeed != 0.f */
Lajos Molnar3a474aa2015-04-24 17:10:07 -0700896 mPlaybackSettings = rate;
Wei Jia8a092d32016-06-03 14:57:24 -0700897 if (mStarted) {
898 // do not resume yet if the source is still buffering
899 if (!mPausedForBuffering) {
900 onResume();
901 }
902 } else if (mPrepared) {
903 onStart();
904 }
905
906 mPausedByClient = false;
Lajos Molnar3a474aa2015-04-24 17:10:07 -0700907 }
Wei Jia98160162015-02-04 17:01:11 -0800908 }
Ronghua Wu8db88132015-04-22 13:51:35 -0700909
910 if (mVideoDecoder != NULL) {
Praveen Chavanbbaa1442016-04-08 13:33:49 -0700911 sp<AMessage> params = new AMessage();
912 params->setFloat("playback-speed", mPlaybackSettings.mSpeed);
913 mVideoDecoder->setParameters(params);
Ronghua Wu8db88132015-04-22 13:51:35 -0700914 }
915
Lajos Molnar3a474aa2015-04-24 17:10:07 -0700916 sp<AMessage> response = new AMessage;
917 response->setInt32("err", err);
918 response->postReply(replyID);
919 break;
920 }
921
922 case kWhatGetPlaybackSettings:
923 {
924 sp<AReplyToken> replyID;
925 CHECK(msg->senderAwaitsResponse(&replyID));
926 AudioPlaybackRate rate = mPlaybackSettings;
927 status_t err = OK;
928 if (mRenderer != NULL) {
929 err = mRenderer->getPlaybackSettings(&rate);
930 }
931 if (err == OK) {
932 // get playback settings used by renderer, as it may be
933 // slightly off due to audiosink not taking small changes.
934 mPlaybackSettings = rate;
935 if (mPaused) {
936 rate.mSpeed = 0.f;
937 }
938 }
939 sp<AMessage> response = new AMessage;
940 if (err == OK) {
941 writeToAMessage(response, rate);
942 }
943 response->setInt32("err", err);
944 response->postReply(replyID);
945 break;
946 }
947
948 case kWhatConfigSync:
949 {
950 sp<AReplyToken> replyID;
951 CHECK(msg->senderAwaitsResponse(&replyID));
952
953 ALOGV("kWhatConfigSync");
954 AVSyncSettings sync;
955 float videoFpsHint;
956 readFromAMessage(msg, &sync, &videoFpsHint);
957 status_t err = OK;
958 if (mRenderer != NULL) {
959 err = mRenderer->setSyncSettings(sync, videoFpsHint);
960 }
961 if (err == OK) {
962 mSyncSettings = sync;
963 mVideoFpsHint = videoFpsHint;
964 }
965 sp<AMessage> response = new AMessage;
966 response->setInt32("err", err);
967 response->postReply(replyID);
968 break;
969 }
970
971 case kWhatGetSyncSettings:
972 {
973 sp<AReplyToken> replyID;
974 CHECK(msg->senderAwaitsResponse(&replyID));
975 AVSyncSettings sync = mSyncSettings;
976 float videoFps = mVideoFpsHint;
977 status_t err = OK;
978 if (mRenderer != NULL) {
979 err = mRenderer->getSyncSettings(&sync, &videoFps);
980 if (err == OK) {
981 mSyncSettings = sync;
982 mVideoFpsHint = videoFps;
983 }
984 }
985 sp<AMessage> response = new AMessage;
986 if (err == OK) {
987 writeToAMessage(response, sync, videoFps);
988 }
989 response->setInt32("err", err);
990 response->postReply(replyID);
Wei Jia98160162015-02-04 17:01:11 -0800991 break;
992 }
993
Andreas Huberf9334412010-12-15 15:17:42 -0800994 case kWhatScanSources:
995 {
Andreas Huber1aef2112011-01-04 14:01:29 -0800996 int32_t generation;
997 CHECK(msg->findInt32("generation", &generation));
998 if (generation != mScanSourcesGeneration) {
999 // Drop obsolete msg.
1000 break;
1001 }
1002
Andreas Huber5bc087c2010-12-23 10:27:40 -08001003 mScanSourcesPending = false;
1004
Steve Block3856b092011-10-20 11:56:00 +01001005 ALOGV("scanning sources haveAudio=%d, haveVideo=%d",
Andreas Huber43c3e6c2011-01-05 12:17:08 -08001006 mAudioDecoder != NULL, mVideoDecoder != NULL);
1007
Andreas Huberb7c8e912012-11-27 15:02:53 -08001008 bool mHadAnySourcesBefore =
1009 (mAudioDecoder != NULL) || (mVideoDecoder != NULL);
Robert Shih7350b052015-10-01 15:50:14 -07001010 bool rescan = false;
Andreas Huberb7c8e912012-11-27 15:02:53 -08001011
Andy Hung282a7e32014-08-14 15:56:34 -07001012 // initialize video before audio because successful initialization of
1013 // video may change deep buffer mode of audio.
Lajos Molnar1de1e252015-04-30 18:18:34 -07001014 if (mSurface != NULL) {
Robert Shih7350b052015-10-01 15:50:14 -07001015 if (instantiateDecoder(false, &mVideoDecoder) == -EWOULDBLOCK) {
1016 rescan = true;
1017 }
Haynes Mathew George5d246ef2012-07-09 10:36:57 -07001018 }
Andreas Huberf9334412010-12-15 15:17:42 -08001019
Ronghua Wua10fd232014-11-06 16:15:20 -08001020 // Don't try to re-open audio sink if there's an existing decoder.
1021 if (mAudioSink != NULL && mAudioDecoder == NULL) {
Robert Shih7350b052015-10-01 15:50:14 -07001022 if (instantiateDecoder(true, &mAudioDecoder) == -EWOULDBLOCK) {
1023 rescan = true;
1024 }
Andreas Huberf9334412010-12-15 15:17:42 -08001025 }
1026
Andreas Huberb7c8e912012-11-27 15:02:53 -08001027 if (!mHadAnySourcesBefore
1028 && (mAudioDecoder != NULL || mVideoDecoder != NULL)) {
1029 // This is the first time we've found anything playable.
1030
Andreas Huber9575c962013-02-05 13:59:56 -08001031 if (mSourceFlags & Source::FLAG_DYNAMIC_DURATION) {
Andreas Huberb7c8e912012-11-27 15:02:53 -08001032 schedulePollDuration();
1033 }
1034 }
1035
Andreas Hubereac68ba2011-09-27 12:12:25 -07001036 status_t err;
1037 if ((err = mSource->feedMoreTSData()) != OK) {
Andreas Huber1aef2112011-01-04 14:01:29 -08001038 if (mAudioDecoder == NULL && mVideoDecoder == NULL) {
1039 // We're not currently decoding anything (no audio or
1040 // video tracks found) and we just ran out of input data.
Andreas Hubereac68ba2011-09-27 12:12:25 -07001041
1042 if (err == ERROR_END_OF_STREAM) {
1043 notifyListener(MEDIA_PLAYBACK_COMPLETE, 0, 0);
1044 } else {
1045 notifyListener(MEDIA_ERROR, MEDIA_ERROR_UNKNOWN, err);
1046 }
Andreas Huber1aef2112011-01-04 14:01:29 -08001047 }
Andreas Huberf9334412010-12-15 15:17:42 -08001048 break;
1049 }
1050
Robert Shih7350b052015-10-01 15:50:14 -07001051 if (rescan) {
Chih-Hung Hsieh62309d52018-12-11 13:54:02 -08001052 msg->post(100000LL);
Andreas Huber5bc087c2010-12-23 10:27:40 -08001053 mScanSourcesPending = true;
Andreas Huberf9334412010-12-15 15:17:42 -08001054 }
1055 break;
1056 }
1057
1058 case kWhatVideoNotify:
1059 case kWhatAudioNotify:
1060 {
1061 bool audio = msg->what() == kWhatAudioNotify;
1062
Wei Jia88703c32014-08-06 11:24:07 -07001063 int32_t currentDecoderGeneration =
1064 (audio? mAudioDecoderGeneration : mVideoDecoderGeneration);
1065 int32_t requesterGeneration = currentDecoderGeneration - 1;
1066 CHECK(msg->findInt32("generation", &requesterGeneration));
1067
1068 if (requesterGeneration != currentDecoderGeneration) {
1069 ALOGV("got message from old %s decoder, generation(%d:%d)",
1070 audio ? "audio" : "video", requesterGeneration,
1071 currentDecoderGeneration);
1072 sp<AMessage> reply;
1073 if (!(msg->findMessage("reply", &reply))) {
1074 return;
1075 }
1076
1077 reply->setInt32("err", INFO_DISCONTINUITY);
1078 reply->post();
1079 return;
1080 }
1081
Andreas Huberf9334412010-12-15 15:17:42 -08001082 int32_t what;
Lajos Molnar1cd13982014-01-17 15:12:51 -08001083 CHECK(msg->findInt32("what", &what));
Andreas Huberf9334412010-12-15 15:17:42 -08001084
Chong Zhang7137ec72014-11-12 16:41:05 -08001085 if (what == DecoderBase::kWhatInputDiscontinuity) {
1086 int32_t formatChange;
1087 CHECK(msg->findInt32("formatChange", &formatChange));
Andreas Huberf9334412010-12-15 15:17:42 -08001088
Chong Zhang7137ec72014-11-12 16:41:05 -08001089 ALOGV("%s discontinuity: formatChange %d",
1090 audio ? "audio" : "video", formatChange);
1091
1092 if (formatChange) {
1093 mDeferredActions.push_back(
1094 new FlushDecoderAction(
1095 audio ? FLUSH_CMD_SHUTDOWN : FLUSH_CMD_NONE,
1096 audio ? FLUSH_CMD_NONE : FLUSH_CMD_SHUTDOWN));
Andreas Huberf9334412010-12-15 15:17:42 -08001097 }
Chong Zhang7137ec72014-11-12 16:41:05 -08001098
1099 mDeferredActions.push_back(
1100 new SimpleAction(
1101 &NuPlayer::performScanSources));
1102
1103 processDeferredActions();
1104 } else if (what == DecoderBase::kWhatEOS) {
Andreas Huberdc9bacd2011-09-26 10:53:29 -07001105 int32_t err;
Lajos Molnar1cd13982014-01-17 15:12:51 -08001106 CHECK(msg->findInt32("err", &err));
Andreas Huberdc9bacd2011-09-26 10:53:29 -07001107
1108 if (err == ERROR_END_OF_STREAM) {
Steve Block3856b092011-10-20 11:56:00 +01001109 ALOGV("got %s decoder EOS", audio ? "audio" : "video");
Andreas Huberdc9bacd2011-09-26 10:53:29 -07001110 } else {
Steve Block3856b092011-10-20 11:56:00 +01001111 ALOGV("got %s decoder EOS w/ error %d",
Andreas Huberdc9bacd2011-09-26 10:53:29 -07001112 audio ? "audio" : "video",
1113 err);
1114 }
1115
1116 mRenderer->queueEOS(audio, err);
Chong Zhang7137ec72014-11-12 16:41:05 -08001117 } else if (what == DecoderBase::kWhatFlushCompleted) {
Steve Block3856b092011-10-20 11:56:00 +01001118 ALOGV("decoder %s flush completed", audio ? "audio" : "video");
Andreas Huberf9334412010-12-15 15:17:42 -08001119
Andy Hung8d121d42014-10-03 09:53:53 -07001120 handleFlushComplete(audio, true /* isDecoder */);
Andreas Huber3831a062010-12-21 10:22:33 -08001121 finishFlushIfPossible();
Chong Zhang7137ec72014-11-12 16:41:05 -08001122 } else if (what == DecoderBase::kWhatVideoSizeChanged) {
Lajos Molnar1cd13982014-01-17 15:12:51 -08001123 sp<AMessage> format;
1124 CHECK(msg->findMessage("format", &format));
1125
Wei Jiac6cfd702014-11-11 16:33:20 -08001126 sp<AMessage> inputFormat =
1127 mSource->getFormat(false /* audio */);
Andreas Huber3831a062010-12-21 10:22:33 -08001128
Bartosz Dolewski26936f72014-12-11 12:47:08 +01001129 setVideoScalingMode(mVideoScalingMode);
Wei Jiac6cfd702014-11-11 16:33:20 -08001130 updateVideoSize(inputFormat, format);
Chong Zhang7137ec72014-11-12 16:41:05 -08001131 } else if (what == DecoderBase::kWhatShutdownCompleted) {
Steve Block3856b092011-10-20 11:56:00 +01001132 ALOGV("%s shutdown completed", audio ? "audio" : "video");
Andreas Huber3831a062010-12-21 10:22:33 -08001133 if (audio) {
Ray Essickfcb8fd32018-08-07 09:39:38 -07001134 Mutex::Autolock autoLock(mDecoderLock);
Andreas Huber3831a062010-12-21 10:22:33 -08001135 mAudioDecoder.clear();
Wei Jia686e8e52017-04-03 14:08:01 -07001136 mAudioDecoderError = false;
Wei Jia57568df2014-09-22 10:16:29 -07001137 ++mAudioDecoderGeneration;
Andreas Huber3831a062010-12-21 10:22:33 -08001138
1139 CHECK_EQ((int)mFlushingAudio, (int)SHUTTING_DOWN_DECODER);
1140 mFlushingAudio = SHUT_DOWN;
1141 } else {
Ray Essickfcb8fd32018-08-07 09:39:38 -07001142 Mutex::Autolock autoLock(mDecoderLock);
Andreas Huber3831a062010-12-21 10:22:33 -08001143 mVideoDecoder.clear();
Wei Jia686e8e52017-04-03 14:08:01 -07001144 mVideoDecoderError = false;
Wei Jia57568df2014-09-22 10:16:29 -07001145 ++mVideoDecoderGeneration;
Andreas Huber3831a062010-12-21 10:22:33 -08001146
1147 CHECK_EQ((int)mFlushingVideo, (int)SHUTTING_DOWN_DECODER);
1148 mFlushingVideo = SHUT_DOWN;
1149 }
1150
1151 finishFlushIfPossible();
Chong Zhangf8d71772014-11-26 15:08:34 -08001152 } else if (what == DecoderBase::kWhatResumeCompleted) {
1153 finishResume();
Chong Zhang7137ec72014-11-12 16:41:05 -08001154 } else if (what == DecoderBase::kWhatError) {
Chong Zhangf4c0a942014-08-11 15:14:10 -07001155 status_t err;
Andy Hung2abde2c2014-09-30 14:40:32 -07001156 if (!msg->findInt32("err", &err) || err == OK) {
Chong Zhangf4c0a942014-08-11 15:14:10 -07001157 err = UNKNOWN_ERROR;
1158 }
Andy Hungcf31f1e2014-09-23 14:59:01 -07001159
Andy Hung2abde2c2014-09-30 14:40:32 -07001160 // Decoder errors can be due to Source (e.g. from streaming),
1161 // or from decoding corrupted bitstreams, or from other decoder
1162 // MediaCodec operations (e.g. from an ongoing reset or seek).
Andy Hung202bce12014-12-03 11:47:36 -08001163 // They may also be due to openAudioSink failure at
1164 // decoder start or after a format change.
Andy Hung2abde2c2014-09-30 14:40:32 -07001165 //
1166 // We try to gracefully shut down the affected decoder if possible,
1167 // rather than trying to force the shutdown with something
1168 // similar to performReset(). This method can lead to a hang
1169 // if MediaCodec functions block after an error, but they should
1170 // typically return INVALID_OPERATION instead of blocking.
1171
1172 FlushStatus *flushing = audio ? &mFlushingAudio : &mFlushingVideo;
1173 ALOGE("received error(%#x) from %s decoder, flushing(%d), now shutting down",
1174 err, audio ? "audio" : "video", *flushing);
1175
1176 switch (*flushing) {
1177 case NONE:
1178 mDeferredActions.push_back(
Wei Jiafef808d2014-10-31 17:57:05 -07001179 new FlushDecoderAction(
1180 audio ? FLUSH_CMD_SHUTDOWN : FLUSH_CMD_NONE,
1181 audio ? FLUSH_CMD_NONE : FLUSH_CMD_SHUTDOWN));
Andy Hung2abde2c2014-09-30 14:40:32 -07001182 processDeferredActions();
1183 break;
1184 case FLUSHING_DECODER:
1185 *flushing = FLUSHING_DECODER_SHUTDOWN; // initiate shutdown after flush.
1186 break; // Wait for flush to complete.
1187 case FLUSHING_DECODER_SHUTDOWN:
1188 break; // Wait for flush to complete.
1189 case SHUTTING_DOWN_DECODER:
1190 break; // Wait for shutdown to complete.
1191 case FLUSHED:
Andy Hung2abde2c2014-09-30 14:40:32 -07001192 getDecoder(audio)->initiateShutdown(); // In the middle of a seek.
1193 *flushing = SHUTTING_DOWN_DECODER; // Shut down.
1194 break;
1195 case SHUT_DOWN:
1196 finishFlushIfPossible(); // Should not occur.
1197 break; // Finish anyways.
Marco Nelissen9e2b7912014-08-18 16:13:03 -07001198 }
Wei Jia686e8e52017-04-03 14:08:01 -07001199 if (mSource != nullptr) {
1200 if (audio) {
Wei Jia2f6d8c52017-04-11 09:50:31 -07001201 if (mVideoDecoderError || mSource->getFormat(false /* audio */) == NULL
Wei Jiaf86731d2017-07-11 17:24:34 -07001202 || mSurface == NULL || mVideoDecoder == NULL) {
Wei Jia686e8e52017-04-03 14:08:01 -07001203 // When both audio and video have error, or this stream has only audio
1204 // which has error, notify client of error.
1205 notifyListener(MEDIA_ERROR, MEDIA_ERROR_UNKNOWN, err);
1206 } else {
1207 // Only audio track has error. Video track could be still good to play.
1208 notifyListener(MEDIA_INFO, MEDIA_INFO_PLAY_AUDIO_ERROR, err);
1209 }
1210 mAudioDecoderError = true;
1211 } else {
Wei Jia2f6d8c52017-04-11 09:50:31 -07001212 if (mAudioDecoderError || mSource->getFormat(true /* audio */) == NULL
Wei Jiaf86731d2017-07-11 17:24:34 -07001213 || mAudioSink == NULL || mAudioDecoder == NULL) {
Wei Jia686e8e52017-04-03 14:08:01 -07001214 // When both audio and video have error, or this stream has only video
1215 // which has error, notify client of error.
1216 notifyListener(MEDIA_ERROR, MEDIA_ERROR_UNKNOWN, err);
1217 } else {
1218 // Only video track has error. Audio track could be still good to play.
1219 notifyListener(MEDIA_INFO, MEDIA_INFO_PLAY_VIDEO_ERROR, err);
1220 }
1221 mVideoDecoderError = true;
1222 }
1223 }
Lajos Molnar1cd13982014-01-17 15:12:51 -08001224 } else {
1225 ALOGV("Unhandled decoder notification %d '%c%c%c%c'.",
Andreas Hubera1f8ab02012-11-30 10:53:22 -08001226 what,
1227 what >> 24,
1228 (what >> 16) & 0xff,
1229 (what >> 8) & 0xff,
1230 what & 0xff);
Andreas Huberf9334412010-12-15 15:17:42 -08001231 }
1232
1233 break;
1234 }
1235
1236 case kWhatRendererNotify:
1237 {
Wei Jia57568df2014-09-22 10:16:29 -07001238 int32_t requesterGeneration = mRendererGeneration - 1;
1239 CHECK(msg->findInt32("generation", &requesterGeneration));
1240 if (requesterGeneration != mRendererGeneration) {
1241 ALOGV("got message from old renderer, generation(%d:%d)",
1242 requesterGeneration, mRendererGeneration);
1243 return;
1244 }
1245
Andreas Huberf9334412010-12-15 15:17:42 -08001246 int32_t what;
1247 CHECK(msg->findInt32("what", &what));
1248
1249 if (what == Renderer::kWhatEOS) {
1250 int32_t audio;
1251 CHECK(msg->findInt32("audio", &audio));
1252
Andreas Huberc92fd242011-08-16 13:48:44 -07001253 int32_t finalResult;
1254 CHECK(msg->findInt32("finalResult", &finalResult));
1255
Andreas Huberf9334412010-12-15 15:17:42 -08001256 if (audio) {
1257 mAudioEOS = true;
1258 } else {
1259 mVideoEOS = true;
1260 }
1261
Andreas Huberc92fd242011-08-16 13:48:44 -07001262 if (finalResult == ERROR_END_OF_STREAM) {
Steve Block3856b092011-10-20 11:56:00 +01001263 ALOGV("reached %s EOS", audio ? "audio" : "video");
Andreas Huberc92fd242011-08-16 13:48:44 -07001264 } else {
Steve Block29357bc2012-01-06 19:20:56 +00001265 ALOGE("%s track encountered an error (%d)",
Andreas Huberc92fd242011-08-16 13:48:44 -07001266 audio ? "audio" : "video", finalResult);
1267
1268 notifyListener(
1269 MEDIA_ERROR, MEDIA_ERROR_UNKNOWN, finalResult);
1270 }
Andreas Huberf9334412010-12-15 15:17:42 -08001271
1272 if ((mAudioEOS || mAudioDecoder == NULL)
1273 && (mVideoEOS || mVideoDecoder == NULL)) {
1274 notifyListener(MEDIA_PLAYBACK_COMPLETE, 0, 0);
1275 }
Andreas Huber3fe62152011-09-16 15:09:22 -07001276 } else if (what == Renderer::kWhatFlushComplete) {
Andreas Huberf9334412010-12-15 15:17:42 -08001277 int32_t audio;
1278 CHECK(msg->findInt32("audio", &audio));
1279
Wei Jia3261f0d2015-10-08 13:58:33 -07001280 if (audio) {
1281 mAudioEOS = false;
1282 } else {
1283 mVideoEOS = false;
1284 }
1285
Steve Block3856b092011-10-20 11:56:00 +01001286 ALOGV("renderer %s flush completed.", audio ? "audio" : "video");
Wei Jiada4252f2015-07-14 18:15:28 -07001287 if (audio && (mFlushingAudio == NONE || mFlushingAudio == FLUSHED
1288 || mFlushingAudio == SHUT_DOWN)) {
1289 // Flush has been handled by tear down.
1290 break;
1291 }
Andy Hung8d121d42014-10-03 09:53:53 -07001292 handleFlushComplete(audio, false /* isDecoder */);
1293 finishFlushIfPossible();
James Dongf57b4ea2012-07-20 13:38:36 -07001294 } else if (what == Renderer::kWhatVideoRenderingStart) {
1295 notifyListener(MEDIA_INFO, MEDIA_INFO_RENDERING_START, 0);
Lajos Molnarcbaffcf2013-08-14 18:30:38 -07001296 } else if (what == Renderer::kWhatMediaRenderingStart) {
1297 ALOGV("media rendering started");
1298 notifyListener(MEDIA_STARTED, 0, 0);
Ronghua Wufaeb0f22015-05-21 12:20:21 -07001299 } else if (what == Renderer::kWhatAudioTearDown) {
Ronghua Wu08529172014-10-02 16:55:52 -07001300 int32_t reason;
1301 CHECK(msg->findInt32("reason", &reason));
Ronghua Wufaeb0f22015-05-21 12:20:21 -07001302 ALOGV("Tear down audio with reason %d.", reason);
Wei Jia8456ddd2016-04-22 14:46:43 -07001303 if (reason == Renderer::kDueToTimeout && !(mPaused && mOffloadAudio)) {
1304 // TimeoutWhenPaused is only for offload mode.
Wei Jia355b2bb2018-04-09 16:36:23 -07001305 ALOGW("Received a stale message for teardown, mPaused(%d), mOffloadAudio(%d)",
1306 mPaused, mOffloadAudio);
Wei Jia8456ddd2016-04-22 14:46:43 -07001307 break;
1308 }
Wei Jiada4252f2015-07-14 18:15:28 -07001309 int64_t positionUs;
Robert Shih1a5c8592015-08-04 18:07:44 -07001310 if (!msg->findInt64("positionUs", &positionUs)) {
1311 positionUs = mPreviousSeekTimeUs;
1312 }
Ronghua Wufaeb0f22015-05-21 12:20:21 -07001313
Wei Jia5031b2f2016-02-25 11:19:31 -08001314 restartAudio(
Wei Jiaa05f1e32016-03-25 16:31:22 -07001315 positionUs, reason == Renderer::kForceNonOffload /* forceNonOffload */,
1316 reason != Renderer::kDueToTimeout /* needsToCreateAudioDecoder */);
Andreas Huberf9334412010-12-15 15:17:42 -08001317 }
1318 break;
1319 }
1320
1321 case kWhatMoreDataQueued:
1322 {
1323 break;
1324 }
1325
Andreas Huber1aef2112011-01-04 14:01:29 -08001326 case kWhatReset:
1327 {
Steve Block3856b092011-10-20 11:56:00 +01001328 ALOGV("kWhatReset");
Andreas Huber1aef2112011-01-04 14:01:29 -08001329
Ronghua Wu64c2d172015-10-07 16:52:19 -07001330 mResetting = true;
Ray Essickeda32522018-02-28 12:08:28 -08001331 updatePlaybackTimer(true /* stopping */, "kWhatReset");
1332 updateRebufferingTimer(true /* stopping */, true /* exiting */);
Ronghua Wu64c2d172015-10-07 16:52:19 -07001333
Andreas Hubera1f8ab02012-11-30 10:53:22 -08001334 mDeferredActions.push_back(
Wei Jiafef808d2014-10-31 17:57:05 -07001335 new FlushDecoderAction(
1336 FLUSH_CMD_SHUTDOWN /* audio */,
1337 FLUSH_CMD_SHUTDOWN /* video */));
Andreas Huberb7c8e912012-11-27 15:02:53 -08001338
Andreas Hubera1f8ab02012-11-30 10:53:22 -08001339 mDeferredActions.push_back(
1340 new SimpleAction(&NuPlayer::performReset));
Andreas Huberb58ce9f2011-11-28 16:27:35 -08001341
Andreas Hubera1f8ab02012-11-30 10:53:22 -08001342 processDeferredActions();
Andreas Huber1aef2112011-01-04 14:01:29 -08001343 break;
1344 }
1345
Wei Jia52c28512017-09-13 18:17:51 -07001346 case kWhatNotifyTime:
1347 {
1348 ALOGV("kWhatNotifyTime");
1349 int64_t timerUs;
1350 CHECK(msg->findInt64("timerUs", &timerUs));
1351
1352 notifyListener(MEDIA_NOTIFY_TIME, timerUs, 0);
1353 break;
1354 }
1355
Andreas Huber43c3e6c2011-01-05 12:17:08 -08001356 case kWhatSeek:
1357 {
1358 int64_t seekTimeUs;
Wei Jiac5de0912016-11-18 10:22:14 -08001359 int32_t mode;
Wei Jiae427abf2014-09-22 15:21:11 -07001360 int32_t needNotify;
Andreas Huber43c3e6c2011-01-05 12:17:08 -08001361 CHECK(msg->findInt64("seekTimeUs", &seekTimeUs));
Wei Jiac5de0912016-11-18 10:22:14 -08001362 CHECK(msg->findInt32("mode", &mode));
Wei Jiae427abf2014-09-22 15:21:11 -07001363 CHECK(msg->findInt32("needNotify", &needNotify));
Andreas Huber43c3e6c2011-01-05 12:17:08 -08001364
Wei Jiac5de0912016-11-18 10:22:14 -08001365 ALOGV("kWhatSeek seekTimeUs=%lld us, mode=%d, needNotify=%d",
1366 (long long)seekTimeUs, mode, needNotify);
Andreas Huber43c3e6c2011-01-05 12:17:08 -08001367
Wei Jia1061c9c2015-05-19 16:02:17 -07001368 if (!mStarted) {
1369 // Seek before the player is started. In order to preview video,
1370 // need to start the player and pause it. This branch is called
1371 // only once if needed. After the player is started, any seek
1372 // operation will go through normal path.
Robert Shih0c61a0d2015-07-06 15:09:10 -07001373 // Audio-only cases are handled separately.
Wei Jiac5de0912016-11-18 10:22:14 -08001374 onStart(seekTimeUs, (MediaPlayerSeekMode)mode);
Robert Shih0c61a0d2015-07-06 15:09:10 -07001375 if (mStarted) {
1376 onPause();
1377 mPausedByClient = true;
1378 }
Wei Jia1061c9c2015-05-19 16:02:17 -07001379 if (needNotify) {
1380 notifyDriverSeekComplete();
1381 }
1382 break;
1383 }
1384
Andreas Hubera1f8ab02012-11-30 10:53:22 -08001385 mDeferredActions.push_back(
Wei Jiafef808d2014-10-31 17:57:05 -07001386 new FlushDecoderAction(FLUSH_CMD_FLUSH /* audio */,
1387 FLUSH_CMD_FLUSH /* video */));
Andreas Huber43c3e6c2011-01-05 12:17:08 -08001388
Wei Jiae427abf2014-09-22 15:21:11 -07001389 mDeferredActions.push_back(
Wei Jiac5de0912016-11-18 10:22:14 -08001390 new SeekAction(seekTimeUs, (MediaPlayerSeekMode)mode));
Andreas Huber43c3e6c2011-01-05 12:17:08 -08001391
Chong Zhangf8d71772014-11-26 15:08:34 -08001392 // After a flush without shutdown, decoder is paused.
1393 // Don't resume it until source seek is done, otherwise it could
Chong Zhang7137ec72014-11-12 16:41:05 -08001394 // start pulling stale data too soon.
1395 mDeferredActions.push_back(
Chong Zhangf8d71772014-11-26 15:08:34 -08001396 new ResumeDecoderAction(needNotify));
Chong Zhang7137ec72014-11-12 16:41:05 -08001397
Andreas Hubera1f8ab02012-11-30 10:53:22 -08001398 processDeferredActions();
Andreas Huber43c3e6c2011-01-05 12:17:08 -08001399 break;
1400 }
1401
Andreas Huberb4082222011-01-20 15:23:04 -08001402 case kWhatPause:
1403 {
Chong Zhangefbb6192015-01-30 17:13:27 -08001404 onPause();
1405 mPausedByClient = true;
Andreas Huberb4082222011-01-20 15:23:04 -08001406 break;
1407 }
1408
Andreas Huberb5f25f02013-02-05 10:14:26 -08001409 case kWhatSourceNotify:
1410 {
Andreas Huber9575c962013-02-05 13:59:56 -08001411 onSourceNotify(msg);
Andreas Huberb5f25f02013-02-05 10:14:26 -08001412 break;
1413 }
1414
Chong Zhanga7fa1d92014-06-11 14:49:23 -07001415 case kWhatClosedCaptionNotify:
1416 {
1417 onClosedCaptionNotify(msg);
1418 break;
1419 }
1420
Hassan Shojaniacefac142017-02-06 21:02:02 -08001421 case kWhatPrepareDrm:
1422 {
1423 status_t status = onPrepareDrm(msg);
1424
1425 sp<AMessage> response = new AMessage;
1426 response->setInt32("status", status);
1427 sp<AReplyToken> replyID;
1428 CHECK(msg->senderAwaitsResponse(&replyID));
1429 response->postReply(replyID);
1430 break;
1431 }
1432
1433 case kWhatReleaseDrm:
1434 {
1435 status_t status = onReleaseDrm();
1436
1437 sp<AMessage> response = new AMessage;
1438 response->setInt32("status", status);
1439 sp<AReplyToken> replyID;
1440 CHECK(msg->senderAwaitsResponse(&replyID));
1441 response->postReply(replyID);
1442 break;
1443 }
1444
Dongwon Kang47afe0a2018-03-27 15:15:30 -07001445 case kWhatMediaClockNotify:
1446 {
1447 ALOGV("kWhatMediaClockNotify");
1448 int64_t anchorMediaUs, anchorRealUs;
1449 float playbackRate;
1450 CHECK(msg->findInt64("anchor-media-us", &anchorMediaUs));
1451 CHECK(msg->findInt64("anchor-real-us", &anchorRealUs));
1452 CHECK(msg->findFloat("playback-rate", &playbackRate));
1453
1454 Parcel in;
1455 in.writeInt64(anchorMediaUs);
1456 in.writeInt64(anchorRealUs);
1457 in.writeFloat(playbackRate);
1458
1459 notifyListener(MEDIA_TIME_DISCONTINUITY, 0, 0, &in);
1460 break;
1461 }
1462
Andreas Huberf9334412010-12-15 15:17:42 -08001463 default:
1464 TRESPASS();
1465 break;
1466 }
1467}
1468
Wei Jia94211742014-10-28 17:09:06 -07001469void NuPlayer::onResume() {
Ronghua Wu64c2d172015-10-07 16:52:19 -07001470 if (!mPaused || mResetting) {
1471 ALOGD_IF(mResetting, "resetting, onResume discarded");
Chong Zhangefbb6192015-01-30 17:13:27 -08001472 return;
1473 }
1474 mPaused = false;
Wei Jia94211742014-10-28 17:09:06 -07001475 if (mSource != NULL) {
1476 mSource->resume();
1477 } else {
1478 ALOGW("resume called when source is gone or not set");
1479 }
1480 // |mAudioDecoder| may have been released due to the pause timeout, so re-create it if
1481 // needed.
1482 if (audioDecoderStillNeeded() && mAudioDecoder == NULL) {
1483 instantiateDecoder(true /* audio */, &mAudioDecoder);
1484 }
1485 if (mRenderer != NULL) {
1486 mRenderer->resume();
1487 } else {
1488 ALOGW("resume called when renderer is gone or not set");
1489 }
Ray Essickd4d00612017-01-03 09:36:27 -08001490
Ray Essick0d98c182017-11-09 13:42:42 -08001491 startPlaybackTimer("onresume");
Wei Jia94211742014-10-28 17:09:06 -07001492}
1493
Lajos Molnarfcd3e942015-03-31 10:06:48 -07001494status_t NuPlayer::onInstantiateSecureDecoders() {
1495 status_t err;
1496 if (!(mSourceFlags & Source::FLAG_SECURE)) {
1497 return BAD_TYPE;
1498 }
1499
1500 if (mRenderer != NULL) {
1501 ALOGE("renderer should not be set when instantiating secure decoders");
1502 return UNKNOWN_ERROR;
1503 }
1504
1505 // TRICKY: We rely on mRenderer being null, so that decoder does not start requesting
1506 // data on instantiation.
Lajos Molnar1de1e252015-04-30 18:18:34 -07001507 if (mSurface != NULL) {
Lajos Molnarfcd3e942015-03-31 10:06:48 -07001508 err = instantiateDecoder(false, &mVideoDecoder);
1509 if (err != OK) {
1510 return err;
1511 }
1512 }
1513
1514 if (mAudioSink != NULL) {
1515 err = instantiateDecoder(true, &mAudioDecoder);
1516 if (err != OK) {
1517 return err;
1518 }
1519 }
1520 return OK;
1521}
1522
Wei Jiac5de0912016-11-18 10:22:14 -08001523void NuPlayer::onStart(int64_t startPositionUs, MediaPlayerSeekMode mode) {
Hassan Shojaniacefac142017-02-06 21:02:02 -08001524 ALOGV("onStart: mCrypto: %p (%d)", mCrypto.get(),
1525 (mCrypto != NULL ? mCrypto->getStrongCount() : 0));
1526
Robert Shih0c61a0d2015-07-06 15:09:10 -07001527 if (!mSourceStarted) {
1528 mSourceStarted = true;
1529 mSource->start();
1530 }
1531 if (startPositionUs > 0) {
Wei Jiac5de0912016-11-18 10:22:14 -08001532 performSeek(startPositionUs, mode);
Robert Shih0c61a0d2015-07-06 15:09:10 -07001533 if (mSource->getFormat(false /* audio */) == NULL) {
1534 return;
1535 }
1536 }
1537
Wei Jia94211742014-10-28 17:09:06 -07001538 mOffloadAudio = false;
1539 mAudioEOS = false;
1540 mVideoEOS = false;
Wei Jia94211742014-10-28 17:09:06 -07001541 mStarted = true;
Wei Jiafe8fe7d2016-06-08 10:29:25 -07001542 mPaused = false;
Wei Jia94211742014-10-28 17:09:06 -07001543
Wei Jia94211742014-10-28 17:09:06 -07001544 uint32_t flags = 0;
1545
1546 if (mSource->isRealTime()) {
1547 flags |= Renderer::FLAG_REAL_TIME;
1548 }
1549
Wei Jia9737d342016-12-28 14:59:59 -08001550 bool hasAudio = (mSource->getFormat(true /* audio */) != NULL);
1551 bool hasVideo = (mSource->getFormat(false /* audio */) != NULL);
1552 if (!hasAudio && !hasVideo) {
Wei Jia1de83d52016-10-10 10:15:03 -07001553 ALOGE("no metadata for either audio or video source");
1554 mSource->stop();
1555 mSourceStarted = false;
1556 notifyListener(MEDIA_ERROR, MEDIA_ERROR_UNKNOWN, ERROR_MALFORMED);
1557 return;
1558 }
Wei Jia9737d342016-12-28 14:59:59 -08001559 ALOGV_IF(!hasAudio, "no metadata for audio source"); // video only stream
1560
1561 sp<MetaData> audioMeta = mSource->getFormatMeta(true /* audio */);
Wei Jia1de83d52016-10-10 10:15:03 -07001562
Wei Jia94211742014-10-28 17:09:06 -07001563 audio_stream_type_t streamType = AUDIO_STREAM_MUSIC;
1564 if (mAudioSink != NULL) {
1565 streamType = mAudioSink->getAudioStreamType();
1566 }
1567
Wei Jia94211742014-10-28 17:09:06 -07001568 mOffloadAudio =
Wei Jia9737d342016-12-28 14:59:59 -08001569 canOffloadStream(audioMeta, hasVideo, mSource->isStreaming(), streamType)
Wei Jiabf70feb2016-02-19 15:47:16 -08001570 && (mPlaybackSettings.mSpeed == 1.f && mPlaybackSettings.mPitch == 1.f);
Hassan Shojaniacefac142017-02-06 21:02:02 -08001571
1572 // Modular DRM: Disabling audio offload if the source is protected
1573 if (mOffloadAudio && mIsDrmProtected) {
1574 mOffloadAudio = false;
1575 ALOGV("onStart: Disabling mOffloadAudio now that the source is protected.");
1576 }
1577
Wei Jia94211742014-10-28 17:09:06 -07001578 if (mOffloadAudio) {
1579 flags |= Renderer::FLAG_OFFLOAD_AUDIO;
1580 }
1581
Lajos Molnar1d15ab52015-03-04 16:46:34 -08001582 sp<AMessage> notify = new AMessage(kWhatRendererNotify, this);
Wei Jia94211742014-10-28 17:09:06 -07001583 ++mRendererGeneration;
1584 notify->setInt32("generation", mRendererGeneration);
Wei Jia0a68f662017-08-30 18:01:26 -07001585 mRenderer = new Renderer(mAudioSink, mMediaClock, notify, flags);
Wei Jia94211742014-10-28 17:09:06 -07001586 mRendererLooper = new ALooper;
1587 mRendererLooper->setName("NuPlayerRenderer");
1588 mRendererLooper->start(false, false, ANDROID_PRIORITY_AUDIO);
1589 mRendererLooper->registerHandler(mRenderer);
Lajos Molnar3a474aa2015-04-24 17:10:07 -07001590
1591 status_t err = mRenderer->setPlaybackSettings(mPlaybackSettings);
1592 if (err != OK) {
1593 mSource->stop();
Robert Shih0c61a0d2015-07-06 15:09:10 -07001594 mSourceStarted = false;
Lajos Molnar3a474aa2015-04-24 17:10:07 -07001595 notifyListener(MEDIA_ERROR, MEDIA_ERROR_UNKNOWN, err);
1596 return;
Wei Jiac8206ff2015-03-04 13:59:37 -08001597 }
Wei Jia94211742014-10-28 17:09:06 -07001598
Ronghua Wuc8a70d32015-04-29 16:26:34 -07001599 float rate = getFrameRate();
1600 if (rate > 0) {
Wei Jia94211742014-10-28 17:09:06 -07001601 mRenderer->setVideoFrameRate(rate);
1602 }
1603
Wei Jiac6cfd702014-11-11 16:33:20 -08001604 if (mVideoDecoder != NULL) {
1605 mVideoDecoder->setRenderer(mRenderer);
1606 }
1607 if (mAudioDecoder != NULL) {
1608 mAudioDecoder->setRenderer(mRenderer);
1609 }
1610
Ray Essick0d98c182017-11-09 13:42:42 -08001611 startPlaybackTimer("onstart");
Ray Essickd4d00612017-01-03 09:36:27 -08001612
Wei Jia94211742014-10-28 17:09:06 -07001613 postScanSources();
1614}
1615
Ray Essick0d98c182017-11-09 13:42:42 -08001616void NuPlayer::startPlaybackTimer(const char *where) {
1617 Mutex::Autolock autoLock(mPlayingTimeLock);
1618 if (mLastStartedPlayingTimeNs == 0) {
1619 mLastStartedPlayingTimeNs = systemTime();
1620 ALOGV("startPlaybackTimer() time %20" PRId64 " (%s)", mLastStartedPlayingTimeNs, where);
1621 }
1622}
1623
Ray Essickeda32522018-02-28 12:08:28 -08001624void NuPlayer::updatePlaybackTimer(bool stopping, const char *where) {
Ray Essick0d98c182017-11-09 13:42:42 -08001625 Mutex::Autolock autoLock(mPlayingTimeLock);
1626
Ray Essickeda32522018-02-28 12:08:28 -08001627 ALOGV("updatePlaybackTimer(%s) time %20" PRId64 " (%s)",
1628 stopping ? "stop" : "snap", mLastStartedPlayingTimeNs, where);
Ray Essick0d98c182017-11-09 13:42:42 -08001629
1630 if (mLastStartedPlayingTimeNs != 0) {
1631 sp<NuPlayerDriver> driver = mDriver.promote();
Ray Essickeda32522018-02-28 12:08:28 -08001632 int64_t now = systemTime();
Ray Essick0d98c182017-11-09 13:42:42 -08001633 if (driver != NULL) {
Ray Essick0d98c182017-11-09 13:42:42 -08001634 int64_t played = now - mLastStartedPlayingTimeNs;
Ray Essickeda32522018-02-28 12:08:28 -08001635 ALOGV("updatePlaybackTimer() log %20" PRId64 "", played);
Ray Essick0d98c182017-11-09 13:42:42 -08001636
1637 if (played > 0) {
1638 driver->notifyMorePlayingTimeUs((played+500)/1000);
1639 }
1640 }
Ray Essickeda32522018-02-28 12:08:28 -08001641 if (stopping) {
1642 mLastStartedPlayingTimeNs = 0;
1643 } else {
1644 mLastStartedPlayingTimeNs = now;
1645 }
Ray Essick0d98c182017-11-09 13:42:42 -08001646 }
1647}
1648
Ray Essick58e0f7a2017-11-21 10:59:38 -08001649void NuPlayer::startRebufferingTimer() {
1650 Mutex::Autolock autoLock(mPlayingTimeLock);
1651 if (mLastStartedRebufferingTimeNs == 0) {
1652 mLastStartedRebufferingTimeNs = systemTime();
1653 ALOGV("startRebufferingTimer() time %20" PRId64 "", mLastStartedRebufferingTimeNs);
1654 }
1655}
1656
Ray Essickeda32522018-02-28 12:08:28 -08001657void NuPlayer::updateRebufferingTimer(bool stopping, bool exitingPlayback) {
Ray Essick58e0f7a2017-11-21 10:59:38 -08001658 Mutex::Autolock autoLock(mPlayingTimeLock);
1659
Ray Essickeda32522018-02-28 12:08:28 -08001660 ALOGV("updateRebufferingTimer(%s) time %20" PRId64 " (exiting %d)",
1661 stopping ? "stop" : "snap", mLastStartedRebufferingTimeNs, exitingPlayback);
Ray Essick58e0f7a2017-11-21 10:59:38 -08001662
1663 if (mLastStartedRebufferingTimeNs != 0) {
1664 sp<NuPlayerDriver> driver = mDriver.promote();
Ray Essickeda32522018-02-28 12:08:28 -08001665 int64_t now = systemTime();
Ray Essick58e0f7a2017-11-21 10:59:38 -08001666 if (driver != NULL) {
Ray Essick58e0f7a2017-11-21 10:59:38 -08001667 int64_t rebuffered = now - mLastStartedRebufferingTimeNs;
Ray Essickeda32522018-02-28 12:08:28 -08001668 ALOGV("updateRebufferingTimer() log %20" PRId64 "", rebuffered);
Ray Essick58e0f7a2017-11-21 10:59:38 -08001669
1670 if (rebuffered > 0) {
1671 driver->notifyMoreRebufferingTimeUs((rebuffered+500)/1000);
1672 if (exitingPlayback) {
1673 driver->notifyRebufferingWhenExit(true);
1674 }
1675 }
1676 }
Ray Essickeda32522018-02-28 12:08:28 -08001677 if (stopping) {
1678 mLastStartedRebufferingTimeNs = 0;
1679 } else {
1680 mLastStartedRebufferingTimeNs = now;
1681 }
Ray Essick58e0f7a2017-11-21 10:59:38 -08001682 }
1683}
1684
Ray Essickeda32522018-02-28 12:08:28 -08001685void NuPlayer::updateInternalTimers() {
1686 // update values, but ticking clocks keep ticking
1687 ALOGV("updateInternalTimers()");
1688 updatePlaybackTimer(false /* stopping */, "updateInternalTimers");
1689 updateRebufferingTimer(false /* stopping */, false /* exiting */);
1690}
1691
Chong Zhangefbb6192015-01-30 17:13:27 -08001692void NuPlayer::onPause() {
Ray Essick0d98c182017-11-09 13:42:42 -08001693
Ray Essickeda32522018-02-28 12:08:28 -08001694 updatePlaybackTimer(true /* stopping */, "onPause");
Ray Essick0d98c182017-11-09 13:42:42 -08001695
Chong Zhangefbb6192015-01-30 17:13:27 -08001696 if (mPaused) {
1697 return;
1698 }
1699 mPaused = true;
1700 if (mSource != NULL) {
1701 mSource->pause();
1702 } else {
1703 ALOGW("pause called when source is gone or not set");
1704 }
1705 if (mRenderer != NULL) {
1706 mRenderer->pause();
1707 } else {
1708 ALOGW("pause called when renderer is gone or not set");
1709 }
Ray Essickd4d00612017-01-03 09:36:27 -08001710
Chong Zhangefbb6192015-01-30 17:13:27 -08001711}
1712
Ronghua Wud7988b12014-10-03 15:19:10 -07001713bool NuPlayer::audioDecoderStillNeeded() {
1714 // Audio decoder is no longer needed if it's in shut/shutting down status.
1715 return ((mFlushingAudio != SHUT_DOWN) && (mFlushingAudio != SHUTTING_DOWN_DECODER));
1716}
1717
Andy Hung8d121d42014-10-03 09:53:53 -07001718void NuPlayer::handleFlushComplete(bool audio, bool isDecoder) {
1719 // We wait for both the decoder flush and the renderer flush to complete
1720 // before entering either the FLUSHED or the SHUTTING_DOWN_DECODER state.
1721
1722 mFlushComplete[audio][isDecoder] = true;
1723 if (!mFlushComplete[audio][!isDecoder]) {
1724 return;
1725 }
1726
1727 FlushStatus *state = audio ? &mFlushingAudio : &mFlushingVideo;
1728 switch (*state) {
1729 case FLUSHING_DECODER:
1730 {
1731 *state = FLUSHED;
Andy Hung8d121d42014-10-03 09:53:53 -07001732 break;
1733 }
1734
1735 case FLUSHING_DECODER_SHUTDOWN:
1736 {
1737 *state = SHUTTING_DOWN_DECODER;
1738
1739 ALOGV("initiating %s decoder shutdown", audio ? "audio" : "video");
Andy Hung8d121d42014-10-03 09:53:53 -07001740 getDecoder(audio)->initiateShutdown();
1741 break;
1742 }
1743
1744 default:
1745 // decoder flush completes only occur in a flushing state.
1746 LOG_ALWAYS_FATAL_IF(isDecoder, "decoder flush in invalid state %d", *state);
1747 break;
1748 }
1749}
1750
Andreas Huber3831a062010-12-21 10:22:33 -08001751void NuPlayer::finishFlushIfPossible() {
Wei Jia53904f32014-07-29 10:22:53 -07001752 if (mFlushingAudio != NONE && mFlushingAudio != FLUSHED
1753 && mFlushingAudio != SHUT_DOWN) {
Andreas Huber3831a062010-12-21 10:22:33 -08001754 return;
1755 }
1756
Wei Jia53904f32014-07-29 10:22:53 -07001757 if (mFlushingVideo != NONE && mFlushingVideo != FLUSHED
1758 && mFlushingVideo != SHUT_DOWN) {
Andreas Huber3831a062010-12-21 10:22:33 -08001759 return;
1760 }
1761
Steve Block3856b092011-10-20 11:56:00 +01001762 ALOGV("both audio and video are flushed now.");
Andreas Huber3831a062010-12-21 10:22:33 -08001763
Andreas Huber3831a062010-12-21 10:22:33 -08001764 mFlushingAudio = NONE;
1765 mFlushingVideo = NONE;
Andreas Huber3831a062010-12-21 10:22:33 -08001766
Andy Hung8d121d42014-10-03 09:53:53 -07001767 clearFlushComplete();
1768
Andreas Hubera1f8ab02012-11-30 10:53:22 -08001769 processDeferredActions();
Andreas Huber1aef2112011-01-04 14:01:29 -08001770}
1771
1772void NuPlayer::postScanSources() {
1773 if (mScanSourcesPending) {
1774 return;
1775 }
1776
Lajos Molnar1d15ab52015-03-04 16:46:34 -08001777 sp<AMessage> msg = new AMessage(kWhatScanSources, this);
Andreas Huber1aef2112011-01-04 14:01:29 -08001778 msg->setInt32("generation", mScanSourcesGeneration);
1779 msg->post();
1780
1781 mScanSourcesPending = true;
1782}
1783
Wei Jia41cd4632016-05-13 10:53:30 -07001784void NuPlayer::tryOpenAudioSinkForOffload(
1785 const sp<AMessage> &format, const sp<MetaData> &audioMeta, bool hasVideo) {
Andy Hung202bce12014-12-03 11:47:36 -08001786 // Note: This is called early in NuPlayer to determine whether offloading
1787 // is possible; otherwise the decoders call the renderer openAudioSink directly.
Andy Hung282a7e32014-08-14 15:56:34 -07001788
Andy Hung202bce12014-12-03 11:47:36 -08001789 status_t err = mRenderer->openAudioSink(
Dhananjay Kumarc387f2b2015-08-06 10:43:16 +05301790 format, true /* offloadOnly */, hasVideo,
1791 AUDIO_OUTPUT_FLAG_NONE, &mOffloadAudio, mSource->isStreaming());
Andy Hung202bce12014-12-03 11:47:36 -08001792 if (err != OK) {
1793 // Any failure we turn off mOffloadAudio.
1794 mOffloadAudio = false;
1795 } else if (mOffloadAudio) {
Chong Zhang3b9eb1f2014-10-15 17:05:08 -07001796 sendMetaDataToHal(mAudioSink, audioMeta);
Andy Hung282a7e32014-08-14 15:56:34 -07001797 }
1798}
1799
1800void NuPlayer::closeAudioSink() {
Chong Zhang3b9eb1f2014-10-15 17:05:08 -07001801 mRenderer->closeAudioSink();
Andy Hung282a7e32014-08-14 15:56:34 -07001802}
1803
Wei Jia5031b2f2016-02-25 11:19:31 -08001804void NuPlayer::restartAudio(
Wei Jiabf70feb2016-02-19 15:47:16 -08001805 int64_t currentPositionUs, bool forceNonOffload, bool needsToCreateAudioDecoder) {
Wei Jia355b2bb2018-04-09 16:36:23 -07001806 ALOGD("restartAudio timeUs(%lld), dontOffload(%d), createDecoder(%d)",
1807 (long long)currentPositionUs, forceNonOffload, needsToCreateAudioDecoder);
Wei Jiaa05f1e32016-03-25 16:31:22 -07001808 if (mAudioDecoder != NULL) {
1809 mAudioDecoder->pause();
Ray Essickfcb8fd32018-08-07 09:39:38 -07001810 Mutex::Autolock autoLock(mDecoderLock);
Wei Jiaa05f1e32016-03-25 16:31:22 -07001811 mAudioDecoder.clear();
Wei Jia686e8e52017-04-03 14:08:01 -07001812 mAudioDecoderError = false;
Wei Jiaa05f1e32016-03-25 16:31:22 -07001813 ++mAudioDecoderGeneration;
1814 }
Wei Jiabf70feb2016-02-19 15:47:16 -08001815 if (mFlushingAudio == FLUSHING_DECODER) {
1816 mFlushComplete[1 /* audio */][1 /* isDecoder */] = true;
1817 mFlushingAudio = FLUSHED;
1818 finishFlushIfPossible();
1819 } else if (mFlushingAudio == FLUSHING_DECODER_SHUTDOWN
1820 || mFlushingAudio == SHUTTING_DOWN_DECODER) {
1821 mFlushComplete[1 /* audio */][1 /* isDecoder */] = true;
1822 mFlushingAudio = SHUT_DOWN;
1823 finishFlushIfPossible();
1824 needsToCreateAudioDecoder = false;
1825 }
1826 if (mRenderer == NULL) {
1827 return;
1828 }
1829 closeAudioSink();
1830 mRenderer->flush(true /* audio */, false /* notifyComplete */);
1831 if (mVideoDecoder != NULL) {
Praveen Chavaneca08fb2019-01-21 11:00:38 -08001832 mDeferredActions.push_back(
1833 new FlushDecoderAction(FLUSH_CMD_NONE /* audio */,
1834 FLUSH_CMD_FLUSH /* video */));
1835 mDeferredActions.push_back(
1836 new SeekAction(currentPositionUs,
1837 MediaPlayerSeekMode::SEEK_PREVIOUS_SYNC /* mode */));
1838 // After a flush without shutdown, decoder is paused.
1839 // Don't resume it until source seek is done, otherwise it could
1840 // start pulling stale data too soon.
1841 mDeferredActions.push_back(new ResumeDecoderAction(false));
1842 processDeferredActions();
1843 } else {
1844 performSeek(currentPositionUs, MediaPlayerSeekMode::SEEK_PREVIOUS_SYNC /* mode */);
Wei Jiabf70feb2016-02-19 15:47:16 -08001845 }
1846
Wei Jiabf70feb2016-02-19 15:47:16 -08001847 if (forceNonOffload) {
1848 mRenderer->signalDisableOffloadAudio();
1849 mOffloadAudio = false;
1850 }
1851 if (needsToCreateAudioDecoder) {
Wei Jiaa05f1e32016-03-25 16:31:22 -07001852 instantiateDecoder(true /* audio */, &mAudioDecoder, !forceNonOffload);
Wei Jiabf70feb2016-02-19 15:47:16 -08001853 }
1854}
1855
Wei Jia41cd4632016-05-13 10:53:30 -07001856void NuPlayer::determineAudioModeChange(const sp<AMessage> &audioFormat) {
Wei Jiae4d18c72015-07-13 17:58:11 -07001857 if (mSource == NULL || mAudioSink == NULL) {
1858 return;
1859 }
1860
1861 if (mRenderer == NULL) {
1862 ALOGW("No renderer can be used to determine audio mode. Use non-offload for safety.");
1863 mOffloadAudio = false;
1864 return;
1865 }
1866
1867 sp<MetaData> audioMeta = mSource->getFormatMeta(true /* audio */);
1868 sp<AMessage> videoFormat = mSource->getFormat(false /* audio */);
1869 audio_stream_type_t streamType = mAudioSink->getAudioStreamType();
1870 const bool hasVideo = (videoFormat != NULL);
Hassan Shojaniacefac142017-02-06 21:02:02 -08001871 bool canOffload = canOffloadStream(
Wei Jiabf70feb2016-02-19 15:47:16 -08001872 audioMeta, hasVideo, mSource->isStreaming(), streamType)
1873 && (mPlaybackSettings.mSpeed == 1.f && mPlaybackSettings.mPitch == 1.f);
Hassan Shojaniacefac142017-02-06 21:02:02 -08001874
1875 // Modular DRM: Disabling audio offload if the source is protected
1876 if (canOffload && mIsDrmProtected) {
1877 canOffload = false;
1878 ALOGV("determineAudioModeChange: Disabling mOffloadAudio b/c the source is protected.");
1879 }
1880
Wei Jiae4d18c72015-07-13 17:58:11 -07001881 if (canOffload) {
1882 if (!mOffloadAudio) {
1883 mRenderer->signalEnableOffloadAudio();
1884 }
1885 // open audio sink early under offload mode.
Wei Jia41cd4632016-05-13 10:53:30 -07001886 tryOpenAudioSinkForOffload(audioFormat, audioMeta, hasVideo);
Wei Jiae4d18c72015-07-13 17:58:11 -07001887 } else {
Robert Shihe1d70192015-07-23 17:54:13 -07001888 if (mOffloadAudio) {
1889 mRenderer->signalDisableOffloadAudio();
1890 mOffloadAudio = false;
1891 }
Wei Jiae4d18c72015-07-13 17:58:11 -07001892 }
1893}
1894
Wei Jiaa05f1e32016-03-25 16:31:22 -07001895status_t NuPlayer::instantiateDecoder(
1896 bool audio, sp<DecoderBase> *decoder, bool checkAudioModeChange) {
Wei Jia566da802015-08-27 13:59:30 -07001897 // The audio decoder could be cleared by tear down. If still in shut down
1898 // process, no need to create a new audio decoder.
1899 if (*decoder != NULL || (audio && mFlushingAudio == SHUT_DOWN)) {
Andreas Huberf9334412010-12-15 15:17:42 -08001900 return OK;
1901 }
1902
Andreas Huber84066782011-08-16 09:34:26 -07001903 sp<AMessage> format = mSource->getFormat(audio);
Andreas Huberf9334412010-12-15 15:17:42 -08001904
Andreas Huber84066782011-08-16 09:34:26 -07001905 if (format == NULL) {
Robert Shih7350b052015-10-01 15:50:14 -07001906 return UNKNOWN_ERROR;
1907 } else {
1908 status_t err;
1909 if (format->findInt32("err", &err) && err) {
1910 return err;
1911 }
Andreas Huberf9334412010-12-15 15:17:42 -08001912 }
1913
Ronghua Wu8db88132015-04-22 13:51:35 -07001914 format->setInt32("priority", 0 /* realtime */);
1915
Andreas Huber3fe62152011-09-16 15:09:22 -07001916 if (!audio) {
Andreas Huber84066782011-08-16 09:34:26 -07001917 AString mime;
1918 CHECK(format->findString("mime", &mime));
Chong Zhanga7fa1d92014-06-11 14:49:23 -07001919
Lajos Molnar1d15ab52015-03-04 16:46:34 -08001920 sp<AMessage> ccNotify = new AMessage(kWhatClosedCaptionNotify, this);
Chong Zhang341ab6e2015-02-04 13:37:18 -08001921 if (mCCDecoder == NULL) {
1922 mCCDecoder = new CCDecoder(ccNotify);
1923 }
Lajos Molnar09524832014-07-17 14:29:51 -07001924
1925 if (mSourceFlags & Source::FLAG_SECURE) {
1926 format->setInt32("secure", true);
1927 }
Chong Zhang17134602015-01-07 16:14:34 -08001928
1929 if (mSourceFlags & Source::FLAG_PROTECTED) {
1930 format->setInt32("protected", true);
1931 }
Ronghua Wu8db88132015-04-22 13:51:35 -07001932
Ronghua Wuc8a70d32015-04-29 16:26:34 -07001933 float rate = getFrameRate();
1934 if (rate > 0) {
Lajos Molnar3a474aa2015-04-24 17:10:07 -07001935 format->setFloat("operating-rate", rate * mPlaybackSettings.mSpeed);
Ronghua Wu8db88132015-04-22 13:51:35 -07001936 }
Andreas Huber3fe62152011-09-16 15:09:22 -07001937 }
1938
Ray Essickfcb8fd32018-08-07 09:39:38 -07001939 Mutex::Autolock autoLock(mDecoderLock);
1940
Wei Jiabc2fb722014-07-08 16:37:57 -07001941 if (audio) {
Lajos Molnar1d15ab52015-03-04 16:46:34 -08001942 sp<AMessage> notify = new AMessage(kWhatAudioNotify, this);
Wei Jia88703c32014-08-06 11:24:07 -07001943 ++mAudioDecoderGeneration;
1944 notify->setInt32("generation", mAudioDecoderGeneration);
1945
Wei Jiaa05f1e32016-03-25 16:31:22 -07001946 if (checkAudioModeChange) {
Wei Jia41cd4632016-05-13 10:53:30 -07001947 determineAudioModeChange(format);
Wei Jiaa05f1e32016-03-25 16:31:22 -07001948 }
Wei Jiabc2fb722014-07-08 16:37:57 -07001949 if (mOffloadAudio) {
Wei Jia14532f22015-12-29 11:28:15 -08001950 mSource->setOffloadAudio(true /* offload */);
1951
Haynes Mathew George8b635332015-03-30 17:59:47 -07001952 const bool hasVideo = (mSource->getFormat(false /*audio */) != NULL);
1953 format->setInt32("has-video", hasVideo);
Wei Jiac6cfd702014-11-11 16:33:20 -08001954 *decoder = new DecoderPassThrough(notify, mSource, mRenderer);
Hassan Shojaniacefac142017-02-06 21:02:02 -08001955 ALOGV("instantiateDecoder audio DecoderPassThrough hasVideo: %d", hasVideo);
Wei Jiabc2fb722014-07-08 16:37:57 -07001956 } else {
Wei Jia14532f22015-12-29 11:28:15 -08001957 mSource->setOffloadAudio(false /* offload */);
1958
Wei Jiaf2ae3e12016-10-27 17:10:59 -07001959 *decoder = new Decoder(notify, mSource, mPID, mUID, mRenderer);
Hassan Shojaniacefac142017-02-06 21:02:02 -08001960 ALOGV("instantiateDecoder audio Decoder");
Wei Jiabc2fb722014-07-08 16:37:57 -07001961 }
Wei Jia686e8e52017-04-03 14:08:01 -07001962 mAudioDecoderError = false;
Wei Jiabc2fb722014-07-08 16:37:57 -07001963 } else {
Lajos Molnar1d15ab52015-03-04 16:46:34 -08001964 sp<AMessage> notify = new AMessage(kWhatVideoNotify, this);
Wei Jia88703c32014-08-06 11:24:07 -07001965 ++mVideoDecoderGeneration;
1966 notify->setInt32("generation", mVideoDecoderGeneration);
1967
Chong Zhang7137ec72014-11-12 16:41:05 -08001968 *decoder = new Decoder(
Wei Jiaf2ae3e12016-10-27 17:10:59 -07001969 notify, mSource, mPID, mUID, mRenderer, mSurface, mCCDecoder);
Wei Jia686e8e52017-04-03 14:08:01 -07001970 mVideoDecoderError = false;
Lajos Molnard9fd6312014-11-06 11:00:00 -08001971
1972 // enable FRC if high-quality AV sync is requested, even if not
Lajos Molnar1de1e252015-04-30 18:18:34 -07001973 // directly queuing to display, as this will even improve textureview
Lajos Molnard9fd6312014-11-06 11:00:00 -08001974 // playback.
1975 {
Marco Nelissen96626b72016-12-01 13:58:59 -08001976 if (property_get_bool("persist.sys.media.avsync", false)) {
Lajos Molnard9fd6312014-11-06 11:00:00 -08001977 format->setInt32("auto-frc", 1);
1978 }
1979 }
Wei Jiabc2fb722014-07-08 16:37:57 -07001980 }
Lajos Molnar1cd13982014-01-17 15:12:51 -08001981 (*decoder)->init();
Hassan Shojaniacefac142017-02-06 21:02:02 -08001982
1983 // Modular DRM
1984 if (mIsDrmProtected) {
1985 format->setPointer("crypto", mCrypto.get());
1986 ALOGV("instantiateDecoder: mCrypto: %p (%d) isSecure: %d", mCrypto.get(),
1987 (mCrypto != NULL ? mCrypto->getStrongCount() : 0),
1988 (mSourceFlags & Source::FLAG_SECURE) != 0);
1989 }
1990
Andreas Huber84066782011-08-16 09:34:26 -07001991 (*decoder)->configure(format);
Andreas Huberf9334412010-12-15 15:17:42 -08001992
Praveen Chavanbbaa1442016-04-08 13:33:49 -07001993 if (!audio) {
1994 sp<AMessage> params = new AMessage();
1995 float rate = getFrameRate();
1996 if (rate > 0) {
1997 params->setFloat("frame-rate-total", rate);
1998 }
1999
2000 sp<MetaData> fileMeta = getFileMeta();
2001 if (fileMeta != NULL) {
2002 int32_t videoTemporalLayerCount;
2003 if (fileMeta->findInt32(kKeyTemporalLayerCount, &videoTemporalLayerCount)
2004 && videoTemporalLayerCount > 0) {
2005 params->setInt32("temporal-layer-count", videoTemporalLayerCount);
2006 }
2007 }
2008
2009 if (params->countEntries() > 0) {
2010 (*decoder)->setParameters(params);
2011 }
2012 }
Andreas Huberf9334412010-12-15 15:17:42 -08002013 return OK;
2014}
2015
Chong Zhangced1c2f2014-08-08 15:22:35 -07002016void NuPlayer::updateVideoSize(
2017 const sp<AMessage> &inputFormat,
2018 const sp<AMessage> &outputFormat) {
2019 if (inputFormat == NULL) {
2020 ALOGW("Unknown video size, reporting 0x0!");
2021 notifyListener(MEDIA_SET_VIDEO_SIZE, 0, 0);
2022 return;
2023 }
Wei Jia9737d342016-12-28 14:59:59 -08002024 int32_t err = OK;
2025 inputFormat->findInt32("err", &err);
2026 if (err == -EWOULDBLOCK) {
2027 ALOGW("Video meta is not available yet!");
2028 return;
2029 }
2030 if (err != OK) {
2031 ALOGW("Something is wrong with video meta!");
2032 return;
2033 }
Chong Zhangced1c2f2014-08-08 15:22:35 -07002034
2035 int32_t displayWidth, displayHeight;
Chong Zhangced1c2f2014-08-08 15:22:35 -07002036 if (outputFormat != NULL) {
2037 int32_t width, height;
2038 CHECK(outputFormat->findInt32("width", &width));
2039 CHECK(outputFormat->findInt32("height", &height));
2040
2041 int32_t cropLeft, cropTop, cropRight, cropBottom;
2042 CHECK(outputFormat->findRect(
2043 "crop",
2044 &cropLeft, &cropTop, &cropRight, &cropBottom));
2045
2046 displayWidth = cropRight - cropLeft + 1;
2047 displayHeight = cropBottom - cropTop + 1;
2048
2049 ALOGV("Video output format changed to %d x %d "
2050 "(crop: %d x %d @ (%d, %d))",
2051 width, height,
2052 displayWidth,
2053 displayHeight,
2054 cropLeft, cropTop);
2055 } else {
2056 CHECK(inputFormat->findInt32("width", &displayWidth));
2057 CHECK(inputFormat->findInt32("height", &displayHeight));
2058
2059 ALOGV("Video input format %d x %d", displayWidth, displayHeight);
2060 }
2061
2062 // Take into account sample aspect ratio if necessary:
2063 int32_t sarWidth, sarHeight;
2064 if (inputFormat->findInt32("sar-width", &sarWidth)
Wei Jia095bc252017-01-27 10:16:16 -08002065 && inputFormat->findInt32("sar-height", &sarHeight)
2066 && sarWidth > 0 && sarHeight > 0) {
Chong Zhangced1c2f2014-08-08 15:22:35 -07002067 ALOGV("Sample aspect ratio %d : %d", sarWidth, sarHeight);
2068
2069 displayWidth = (displayWidth * sarWidth) / sarHeight;
2070
2071 ALOGV("display dimensions %d x %d", displayWidth, displayHeight);
Wei Jiadf6c6af2016-09-29 11:27:15 -07002072 } else {
2073 int32_t width, height;
2074 if (inputFormat->findInt32("display-width", &width)
2075 && inputFormat->findInt32("display-height", &height)
2076 && width > 0 && height > 0
2077 && displayWidth > 0 && displayHeight > 0) {
2078 if (displayHeight * (int64_t)width / height > (int64_t)displayWidth) {
2079 displayHeight = (int32_t)(displayWidth * (int64_t)height / width);
2080 } else {
2081 displayWidth = (int32_t)(displayHeight * (int64_t)width / height);
2082 }
2083 ALOGV("Video display width and height are overridden to %d x %d",
2084 displayWidth, displayHeight);
2085 }
Chong Zhangced1c2f2014-08-08 15:22:35 -07002086 }
2087
2088 int32_t rotationDegrees;
2089 if (!inputFormat->findInt32("rotation-degrees", &rotationDegrees)) {
2090 rotationDegrees = 0;
2091 }
2092
2093 if (rotationDegrees == 90 || rotationDegrees == 270) {
2094 int32_t tmp = displayWidth;
2095 displayWidth = displayHeight;
2096 displayHeight = tmp;
2097 }
2098
2099 notifyListener(
2100 MEDIA_SET_VIDEO_SIZE,
2101 displayWidth,
2102 displayHeight);
2103}
2104
Chong Zhangdcb89b32013-08-06 09:44:47 -07002105void NuPlayer::notifyListener(int msg, int ext1, int ext2, const Parcel *in) {
Andreas Huber43c3e6c2011-01-05 12:17:08 -08002106 if (mDriver == NULL) {
Andreas Huberf9334412010-12-15 15:17:42 -08002107 return;
2108 }
2109
Andreas Huber43c3e6c2011-01-05 12:17:08 -08002110 sp<NuPlayerDriver> driver = mDriver.promote();
Andreas Huberf9334412010-12-15 15:17:42 -08002111
Andreas Huber43c3e6c2011-01-05 12:17:08 -08002112 if (driver == NULL) {
Andreas Huberf9334412010-12-15 15:17:42 -08002113 return;
2114 }
2115
Chong Zhangdcb89b32013-08-06 09:44:47 -07002116 driver->notifyListener(msg, ext1, ext2, in);
Andreas Huberf9334412010-12-15 15:17:42 -08002117}
2118
Chong Zhang7137ec72014-11-12 16:41:05 -08002119void NuPlayer::flushDecoder(bool audio, bool needShutdown) {
Andreas Huber14f76722013-01-15 09:04:18 -08002120 ALOGV("[%s] flushDecoder needShutdown=%d",
2121 audio ? "audio" : "video", needShutdown);
2122
Chong Zhang7137ec72014-11-12 16:41:05 -08002123 const sp<DecoderBase> &decoder = getDecoder(audio);
Lajos Molnar87603c02014-08-20 19:25:30 -07002124 if (decoder == NULL) {
Steve Blockdf64d152012-01-04 20:05:49 +00002125 ALOGI("flushDecoder %s without decoder present",
Andreas Huber6e3d3112011-11-28 12:36:11 -08002126 audio ? "audio" : "video");
Lajos Molnar87603c02014-08-20 19:25:30 -07002127 return;
Andreas Huber6e3d3112011-11-28 12:36:11 -08002128 }
2129
Andreas Huber1aef2112011-01-04 14:01:29 -08002130 // Make sure we don't continue to scan sources until we finish flushing.
2131 ++mScanSourcesGeneration;
Chong Zhang3b032b32015-04-17 15:49:06 -07002132 if (mScanSourcesPending) {
Toshikazu Saitocbcbb792015-09-15 14:53:01 +09002133 if (!needShutdown) {
2134 mDeferredActions.push_back(
2135 new SimpleAction(&NuPlayer::performScanSources));
2136 }
Chong Zhang3b032b32015-04-17 15:49:06 -07002137 mScanSourcesPending = false;
2138 }
Andreas Huber1aef2112011-01-04 14:01:29 -08002139
Chong Zhang7137ec72014-11-12 16:41:05 -08002140 decoder->signalFlush();
Andreas Huber1aef2112011-01-04 14:01:29 -08002141
2142 FlushStatus newStatus =
2143 needShutdown ? FLUSHING_DECODER_SHUTDOWN : FLUSHING_DECODER;
2144
Lajos Molnarfcd3e942015-03-31 10:06:48 -07002145 mFlushComplete[audio][false /* isDecoder */] = (mRenderer == NULL);
Andy Hung8d121d42014-10-03 09:53:53 -07002146 mFlushComplete[audio][true /* isDecoder */] = false;
Andreas Huber1aef2112011-01-04 14:01:29 -08002147 if (audio) {
Wei Jia53904f32014-07-29 10:22:53 -07002148 ALOGE_IF(mFlushingAudio != NONE,
2149 "audio flushDecoder() is called in state %d", mFlushingAudio);
Andreas Huber1aef2112011-01-04 14:01:29 -08002150 mFlushingAudio = newStatus;
Andreas Huber1aef2112011-01-04 14:01:29 -08002151 } else {
Wei Jia53904f32014-07-29 10:22:53 -07002152 ALOGE_IF(mFlushingVideo != NONE,
2153 "video flushDecoder() is called in state %d", mFlushingVideo);
Andreas Huber1aef2112011-01-04 14:01:29 -08002154 mFlushingVideo = newStatus;
Andreas Huber1aef2112011-01-04 14:01:29 -08002155 }
2156}
2157
Chong Zhangced1c2f2014-08-08 15:22:35 -07002158void NuPlayer::queueDecoderShutdown(
2159 bool audio, bool video, const sp<AMessage> &reply) {
2160 ALOGI("queueDecoderShutdown audio=%d, video=%d", audio, video);
Andreas Huber84066782011-08-16 09:34:26 -07002161
Chong Zhangced1c2f2014-08-08 15:22:35 -07002162 mDeferredActions.push_back(
Wei Jiafef808d2014-10-31 17:57:05 -07002163 new FlushDecoderAction(
2164 audio ? FLUSH_CMD_SHUTDOWN : FLUSH_CMD_NONE,
2165 video ? FLUSH_CMD_SHUTDOWN : FLUSH_CMD_NONE));
Andreas Huber84066782011-08-16 09:34:26 -07002166
Chong Zhangced1c2f2014-08-08 15:22:35 -07002167 mDeferredActions.push_back(
2168 new SimpleAction(&NuPlayer::performScanSources));
Andreas Huber84066782011-08-16 09:34:26 -07002169
Chong Zhangced1c2f2014-08-08 15:22:35 -07002170 mDeferredActions.push_back(new PostMessageAction(reply));
2171
2172 processDeferredActions();
Andreas Huber84066782011-08-16 09:34:26 -07002173}
2174
James Dong0d268a32012-08-31 12:18:27 -07002175status_t NuPlayer::setVideoScalingMode(int32_t mode) {
2176 mVideoScalingMode = mode;
Lajos Molnar1de1e252015-04-30 18:18:34 -07002177 if (mSurface != NULL) {
2178 status_t ret = native_window_set_scaling_mode(mSurface.get(), mVideoScalingMode);
James Dong0d268a32012-08-31 12:18:27 -07002179 if (ret != OK) {
2180 ALOGE("Failed to set scaling mode (%d): %s",
2181 -ret, strerror(-ret));
2182 return ret;
2183 }
2184 }
2185 return OK;
2186}
2187
Chong Zhangdcb89b32013-08-06 09:44:47 -07002188status_t NuPlayer::getTrackInfo(Parcel* reply) const {
Lajos Molnar1d15ab52015-03-04 16:46:34 -08002189 sp<AMessage> msg = new AMessage(kWhatGetTrackInfo, this);
Chong Zhangdcb89b32013-08-06 09:44:47 -07002190 msg->setPointer("reply", reply);
2191
2192 sp<AMessage> response;
2193 status_t err = msg->postAndAwaitResponse(&response);
2194 return err;
2195}
2196
Robert Shih7c4f0d72014-07-09 18:53:31 -07002197status_t NuPlayer::getSelectedTrack(int32_t type, Parcel* reply) const {
Lajos Molnar1d15ab52015-03-04 16:46:34 -08002198 sp<AMessage> msg = new AMessage(kWhatGetSelectedTrack, this);
Robert Shih7c4f0d72014-07-09 18:53:31 -07002199 msg->setPointer("reply", reply);
2200 msg->setInt32("type", type);
2201
2202 sp<AMessage> response;
2203 status_t err = msg->postAndAwaitResponse(&response);
2204 if (err == OK && response != NULL) {
2205 CHECK(response->findInt32("err", &err));
2206 }
2207 return err;
2208}
2209
Robert Shih6ffb1fd2014-10-29 16:24:32 -07002210status_t NuPlayer::selectTrack(size_t trackIndex, bool select, int64_t timeUs) {
Lajos Molnar1d15ab52015-03-04 16:46:34 -08002211 sp<AMessage> msg = new AMessage(kWhatSelectTrack, this);
Chong Zhangdcb89b32013-08-06 09:44:47 -07002212 msg->setSize("trackIndex", trackIndex);
2213 msg->setInt32("select", select);
Robert Shih6ffb1fd2014-10-29 16:24:32 -07002214 msg->setInt64("timeUs", timeUs);
Chong Zhangdcb89b32013-08-06 09:44:47 -07002215
2216 sp<AMessage> response;
2217 status_t err = msg->postAndAwaitResponse(&response);
2218
Chong Zhang404fced2014-06-11 14:45:31 -07002219 if (err != OK) {
2220 return err;
2221 }
2222
2223 if (!response->findInt32("err", &err)) {
2224 err = OK;
2225 }
2226
Chong Zhangdcb89b32013-08-06 09:44:47 -07002227 return err;
2228}
2229
Ronghua Wua73d9e02014-10-08 15:13:29 -07002230status_t NuPlayer::getCurrentPosition(int64_t *mediaUs) {
2231 sp<Renderer> renderer = mRenderer;
2232 if (renderer == NULL) {
2233 return NO_INIT;
2234 }
2235
2236 return renderer->getCurrentPosition(mediaUs);
2237}
2238
Ray Essickffc941f2018-05-18 11:08:37 -07002239void NuPlayer::getStats(Vector<sp<AMessage> > *trackStats) {
2240 CHECK(trackStats != NULL);
Praveen Chavane1e5d7a2015-05-19 19:09:48 -07002241
Ray Essickfcb8fd32018-08-07 09:39:38 -07002242 trackStats->clear();
Ray Essickffc941f2018-05-18 11:08:37 -07002243
Ray Essickfcb8fd32018-08-07 09:39:38 -07002244 Mutex::Autolock autoLock(mDecoderLock);
2245 if (mVideoDecoder != NULL) {
2246 trackStats->push_back(mVideoDecoder->getStats());
2247 }
2248 if (mAudioDecoder != NULL) {
2249 trackStats->push_back(mAudioDecoder->getStats());
2250 }
Ronghua Wua73d9e02014-10-08 15:13:29 -07002251}
2252
Marco Nelissenf0b72b52014-09-16 15:43:44 -07002253sp<MetaData> NuPlayer::getFileMeta() {
2254 return mSource->getFileFormatMeta();
2255}
2256
Ronghua Wuc8a70d32015-04-29 16:26:34 -07002257float NuPlayer::getFrameRate() {
2258 sp<MetaData> meta = mSource->getFormatMeta(false /* audio */);
2259 if (meta == NULL) {
2260 return 0;
2261 }
2262 int32_t rate;
2263 if (!meta->findInt32(kKeyFrameRate, &rate)) {
2264 // fall back to try file meta
2265 sp<MetaData> fileMeta = getFileMeta();
2266 if (fileMeta == NULL) {
2267 ALOGW("source has video meta but not file meta");
2268 return -1;
2269 }
2270 int32_t fileMetaRate;
2271 if (!fileMeta->findInt32(kKeyFrameRate, &fileMetaRate)) {
2272 return -1;
2273 }
2274 return fileMetaRate;
2275 }
2276 return rate;
2277}
2278
Andreas Huberb7c8e912012-11-27 15:02:53 -08002279void NuPlayer::schedulePollDuration() {
Lajos Molnar1d15ab52015-03-04 16:46:34 -08002280 sp<AMessage> msg = new AMessage(kWhatPollDuration, this);
Andreas Huberb7c8e912012-11-27 15:02:53 -08002281 msg->setInt32("generation", mPollDurationGeneration);
2282 msg->post();
2283}
2284
2285void NuPlayer::cancelPollDuration() {
2286 ++mPollDurationGeneration;
2287}
2288
Andreas Hubera1f8ab02012-11-30 10:53:22 -08002289void NuPlayer::processDeferredActions() {
2290 while (!mDeferredActions.empty()) {
2291 // We won't execute any deferred actions until we're no longer in
2292 // an intermediate state, i.e. one more more decoders are currently
2293 // flushing or shutting down.
2294
Andreas Hubera1f8ab02012-11-30 10:53:22 -08002295 if (mFlushingAudio != NONE || mFlushingVideo != NONE) {
2296 // We're currently flushing, postpone the reset until that's
2297 // completed.
2298
2299 ALOGV("postponing action mFlushingAudio=%d, mFlushingVideo=%d",
2300 mFlushingAudio, mFlushingVideo);
2301
2302 break;
2303 }
2304
2305 sp<Action> action = *mDeferredActions.begin();
2306 mDeferredActions.erase(mDeferredActions.begin());
2307
2308 action->execute(this);
2309 }
2310}
2311
Wei Jiac5de0912016-11-18 10:22:14 -08002312void NuPlayer::performSeek(int64_t seekTimeUs, MediaPlayerSeekMode mode) {
2313 ALOGV("performSeek seekTimeUs=%lld us (%.2f secs), mode=%d",
2314 (long long)seekTimeUs, seekTimeUs / 1E6, mode);
Andreas Hubera1f8ab02012-11-30 10:53:22 -08002315
Andy Hungadf34bf2014-09-03 18:22:22 -07002316 if (mSource == NULL) {
2317 // This happens when reset occurs right before the loop mode
2318 // asynchronously seeks to the start of the stream.
2319 LOG_ALWAYS_FATAL_IF(mAudioDecoder != NULL || mVideoDecoder != NULL,
2320 "mSource is NULL and decoders not NULL audio(%p) video(%p)",
2321 mAudioDecoder.get(), mVideoDecoder.get());
2322 return;
2323 }
Robert Shih1a5c8592015-08-04 18:07:44 -07002324 mPreviousSeekTimeUs = seekTimeUs;
Wei Jiac5de0912016-11-18 10:22:14 -08002325 mSource->seekTo(seekTimeUs, mode);
Robert Shihd3b0bbb2014-07-23 15:00:25 -07002326 ++mTimedTextGeneration;
Andreas Hubera1f8ab02012-11-30 10:53:22 -08002327
Andreas Hubera1f8ab02012-11-30 10:53:22 -08002328 // everything's flushed, continue playback.
2329}
2330
Wei Jiafef808d2014-10-31 17:57:05 -07002331void NuPlayer::performDecoderFlush(FlushCommand audio, FlushCommand video) {
2332 ALOGV("performDecoderFlush audio=%d, video=%d", audio, video);
Andreas Hubera1f8ab02012-11-30 10:53:22 -08002333
Wei Jiafef808d2014-10-31 17:57:05 -07002334 if ((audio == FLUSH_CMD_NONE || mAudioDecoder == NULL)
2335 && (video == FLUSH_CMD_NONE || mVideoDecoder == NULL)) {
Andreas Hubera1f8ab02012-11-30 10:53:22 -08002336 return;
2337 }
2338
Wei Jiafef808d2014-10-31 17:57:05 -07002339 if (audio != FLUSH_CMD_NONE && mAudioDecoder != NULL) {
2340 flushDecoder(true /* audio */, (audio == FLUSH_CMD_SHUTDOWN));
Andreas Hubera1f8ab02012-11-30 10:53:22 -08002341 }
2342
Wei Jiafef808d2014-10-31 17:57:05 -07002343 if (video != FLUSH_CMD_NONE && mVideoDecoder != NULL) {
2344 flushDecoder(false /* audio */, (video == FLUSH_CMD_SHUTDOWN));
Andreas Hubera1f8ab02012-11-30 10:53:22 -08002345 }
2346}
2347
2348void NuPlayer::performReset() {
2349 ALOGV("performReset");
2350
2351 CHECK(mAudioDecoder == NULL);
2352 CHECK(mVideoDecoder == NULL);
2353
Ray Essickeda32522018-02-28 12:08:28 -08002354 updatePlaybackTimer(true /* stopping */, "performReset");
2355 updateRebufferingTimer(true /* stopping */, true /* exiting */);
Ray Essick0d98c182017-11-09 13:42:42 -08002356
Andreas Hubera1f8ab02012-11-30 10:53:22 -08002357 cancelPollDuration();
2358
2359 ++mScanSourcesGeneration;
2360 mScanSourcesPending = false;
2361
Lajos Molnar09524832014-07-17 14:29:51 -07002362 if (mRendererLooper != NULL) {
2363 if (mRenderer != NULL) {
2364 mRendererLooper->unregisterHandler(mRenderer->id());
2365 }
2366 mRendererLooper->stop();
2367 mRendererLooper.clear();
2368 }
Andreas Hubera1f8ab02012-11-30 10:53:22 -08002369 mRenderer.clear();
Wei Jia57568df2014-09-22 10:16:29 -07002370 ++mRendererGeneration;
Andreas Hubera1f8ab02012-11-30 10:53:22 -08002371
2372 if (mSource != NULL) {
2373 mSource->stop();
Andreas Huberb5f25f02013-02-05 10:14:26 -08002374
Wei Jiac45a4b22016-04-15 15:30:23 -07002375 Mutex::Autolock autoLock(mSourceLock);
Andreas Hubera1f8ab02012-11-30 10:53:22 -08002376 mSource.clear();
2377 }
2378
2379 if (mDriver != NULL) {
2380 sp<NuPlayerDriver> driver = mDriver.promote();
2381 if (driver != NULL) {
2382 driver->notifyResetComplete();
2383 }
2384 }
Andreas Huber57a339c2012-12-03 11:18:00 -08002385
2386 mStarted = false;
Wei Jia8a092d32016-06-03 14:57:24 -07002387 mPrepared = false;
Ronghua Wu64c2d172015-10-07 16:52:19 -07002388 mResetting = false;
Robert Shih0c61a0d2015-07-06 15:09:10 -07002389 mSourceStarted = false;
Hassan Shojaniacefac142017-02-06 21:02:02 -08002390
2391 // Modular DRM
2392 if (mCrypto != NULL) {
2393 // decoders will be flushed before this so their mCrypto would go away on their own
2394 // TODO change to ALOGV
2395 ALOGD("performReset mCrypto: %p (%d)", mCrypto.get(),
2396 (mCrypto != NULL ? mCrypto->getStrongCount() : 0));
2397 mCrypto.clear();
2398 }
2399 mIsDrmProtected = false;
Andreas Hubera1f8ab02012-11-30 10:53:22 -08002400}
2401
2402void NuPlayer::performScanSources() {
2403 ALOGV("performScanSources");
2404
Andreas Huber57a339c2012-12-03 11:18:00 -08002405 if (!mStarted) {
2406 return;
2407 }
2408
Andreas Hubera1f8ab02012-11-30 10:53:22 -08002409 if (mAudioDecoder == NULL || mVideoDecoder == NULL) {
2410 postScanSources();
2411 }
2412}
2413
Lajos Molnar1de1e252015-04-30 18:18:34 -07002414void NuPlayer::performSetSurface(const sp<Surface> &surface) {
Andreas Huber57a339c2012-12-03 11:18:00 -08002415 ALOGV("performSetSurface");
2416
Lajos Molnar1de1e252015-04-30 18:18:34 -07002417 mSurface = surface;
Andreas Huber57a339c2012-12-03 11:18:00 -08002418
2419 // XXX - ignore error from setVideoScalingMode for now
2420 setVideoScalingMode(mVideoScalingMode);
Chong Zhang13d6faa2014-08-22 15:35:28 -07002421
2422 if (mDriver != NULL) {
2423 sp<NuPlayerDriver> driver = mDriver.promote();
2424 if (driver != NULL) {
2425 driver->notifySetSurfaceComplete();
2426 }
2427 }
Andreas Huber57a339c2012-12-03 11:18:00 -08002428}
2429
Chong Zhangf8d71772014-11-26 15:08:34 -08002430void NuPlayer::performResumeDecoders(bool needNotify) {
2431 if (needNotify) {
2432 mResumePending = true;
2433 if (mVideoDecoder == NULL) {
2434 // if audio-only, we can notify seek complete now,
2435 // as the resume operation will be relatively fast.
2436 finishResume();
2437 }
2438 }
2439
Chong Zhang7137ec72014-11-12 16:41:05 -08002440 if (mVideoDecoder != NULL) {
Chong Zhangf8d71772014-11-26 15:08:34 -08002441 // When there is continuous seek, MediaPlayer will cache the seek
2442 // position, and send down new seek request when previous seek is
2443 // complete. Let's wait for at least one video output frame before
2444 // notifying seek complete, so that the video thumbnail gets updated
2445 // when seekbar is dragged.
2446 mVideoDecoder->signalResume(needNotify);
Chong Zhang7137ec72014-11-12 16:41:05 -08002447 }
2448
2449 if (mAudioDecoder != NULL) {
Chong Zhangf8d71772014-11-26 15:08:34 -08002450 mAudioDecoder->signalResume(false /* needNotify */);
2451 }
2452}
2453
2454void NuPlayer::finishResume() {
2455 if (mResumePending) {
2456 mResumePending = false;
Wei Jia1061c9c2015-05-19 16:02:17 -07002457 notifyDriverSeekComplete();
2458 }
2459}
2460
2461void NuPlayer::notifyDriverSeekComplete() {
2462 if (mDriver != NULL) {
2463 sp<NuPlayerDriver> driver = mDriver.promote();
2464 if (driver != NULL) {
2465 driver->notifySeekComplete();
Chong Zhangf8d71772014-11-26 15:08:34 -08002466 }
Chong Zhang7137ec72014-11-12 16:41:05 -08002467 }
2468}
2469
Andreas Huber9575c962013-02-05 13:59:56 -08002470void NuPlayer::onSourceNotify(const sp<AMessage> &msg) {
2471 int32_t what;
2472 CHECK(msg->findInt32("what", &what));
2473
2474 switch (what) {
Lajos Molnarfcd3e942015-03-31 10:06:48 -07002475 case Source::kWhatInstantiateSecureDecoders:
2476 {
2477 if (mSource == NULL) {
2478 // This is a stale notification from a source that was
2479 // asynchronously preparing when the client called reset().
2480 // We handled the reset, the source is gone.
2481 break;
2482 }
2483
2484 sp<AMessage> reply;
2485 CHECK(msg->findMessage("reply", &reply));
2486 status_t err = onInstantiateSecureDecoders();
2487 reply->setInt32("err", err);
2488 reply->post();
2489 break;
2490 }
2491
Andreas Huber9575c962013-02-05 13:59:56 -08002492 case Source::kWhatPrepared:
2493 {
Hassan Shojaniacefac142017-02-06 21:02:02 -08002494 ALOGV("NuPlayer::onSourceNotify Source::kWhatPrepared source: %p", mSource.get());
Andreas Huberb5f28d42013-04-25 15:11:19 -07002495 if (mSource == NULL) {
2496 // This is a stale notification from a source that was
2497 // asynchronously preparing when the client called reset().
2498 // We handled the reset, the source is gone.
2499 break;
2500 }
2501
Andreas Huberec0c5972013-02-05 14:47:13 -08002502 int32_t err;
2503 CHECK(msg->findInt32("err", &err));
2504
Lajos Molnarfcd3e942015-03-31 10:06:48 -07002505 if (err != OK) {
2506 // shut down potential secure codecs in case client never calls reset
2507 mDeferredActions.push_back(
2508 new FlushDecoderAction(FLUSH_CMD_SHUTDOWN /* audio */,
2509 FLUSH_CMD_SHUTDOWN /* video */));
2510 processDeferredActions();
Wei Jia8a092d32016-06-03 14:57:24 -07002511 } else {
2512 mPrepared = true;
Lajos Molnarfcd3e942015-03-31 10:06:48 -07002513 }
2514
Andreas Huber9575c962013-02-05 13:59:56 -08002515 sp<NuPlayerDriver> driver = mDriver.promote();
2516 if (driver != NULL) {
Marco Nelissendd114d12014-05-28 15:23:14 -07002517 // notify duration first, so that it's definitely set when
2518 // the app received the "prepare complete" callback.
2519 int64_t durationUs;
2520 if (mSource->getDuration(&durationUs) == OK) {
2521 driver->notifyDuration(durationUs);
2522 }
Andreas Huberec0c5972013-02-05 14:47:13 -08002523 driver->notifyPrepareCompleted(err);
Andreas Huber9575c962013-02-05 13:59:56 -08002524 }
Andreas Huber99759402013-04-01 14:28:31 -07002525
Andreas Huber9575c962013-02-05 13:59:56 -08002526 break;
2527 }
2528
Hassan Shojaniacefac142017-02-06 21:02:02 -08002529 // Modular DRM
2530 case Source::kWhatDrmInfo:
2531 {
2532 Parcel parcel;
2533 sp<ABuffer> drmInfo;
2534 CHECK(msg->findBuffer("drmInfo", &drmInfo));
2535 parcel.setData(drmInfo->data(), drmInfo->size());
2536
2537 ALOGV("onSourceNotify() kWhatDrmInfo MEDIA_DRM_INFO drmInfo: %p parcel size: %zu",
2538 drmInfo.get(), parcel.dataSize());
2539
2540 notifyListener(MEDIA_DRM_INFO, 0 /* ext1 */, 0 /* ext2 */, &parcel);
2541
2542 break;
2543 }
2544
Andreas Huber9575c962013-02-05 13:59:56 -08002545 case Source::kWhatFlagsChanged:
2546 {
2547 uint32_t flags;
2548 CHECK(msg->findInt32("flags", (int32_t *)&flags));
2549
Chong Zhang4b7069d2013-09-11 12:52:43 -07002550 sp<NuPlayerDriver> driver = mDriver.promote();
2551 if (driver != NULL) {
Hassan Shojaniacefac142017-02-06 21:02:02 -08002552
2553 ALOGV("onSourceNotify() kWhatFlagsChanged FLAG_CAN_PAUSE: %d "
2554 "FLAG_CAN_SEEK_BACKWARD: %d \n\t\t\t\t FLAG_CAN_SEEK_FORWARD: %d "
2555 "FLAG_CAN_SEEK: %d FLAG_DYNAMIC_DURATION: %d \n"
2556 "\t\t\t\t FLAG_SECURE: %d FLAG_PROTECTED: %d",
2557 (flags & Source::FLAG_CAN_PAUSE) != 0,
2558 (flags & Source::FLAG_CAN_SEEK_BACKWARD) != 0,
2559 (flags & Source::FLAG_CAN_SEEK_FORWARD) != 0,
2560 (flags & Source::FLAG_CAN_SEEK) != 0,
2561 (flags & Source::FLAG_DYNAMIC_DURATION) != 0,
2562 (flags & Source::FLAG_SECURE) != 0,
2563 (flags & Source::FLAG_PROTECTED) != 0);
2564
Wei Jia895651b2014-12-10 17:31:52 -08002565 if ((flags & NuPlayer::Source::FLAG_CAN_SEEK) == 0) {
2566 driver->notifyListener(
2567 MEDIA_INFO, MEDIA_INFO_NOT_SEEKABLE, 0);
2568 }
Chong Zhang4b7069d2013-09-11 12:52:43 -07002569 driver->notifyFlagsChanged(flags);
2570 }
2571
Andreas Huber9575c962013-02-05 13:59:56 -08002572 if ((mSourceFlags & Source::FLAG_DYNAMIC_DURATION)
2573 && (!(flags & Source::FLAG_DYNAMIC_DURATION))) {
2574 cancelPollDuration();
2575 } else if (!(mSourceFlags & Source::FLAG_DYNAMIC_DURATION)
2576 && (flags & Source::FLAG_DYNAMIC_DURATION)
2577 && (mAudioDecoder != NULL || mVideoDecoder != NULL)) {
2578 schedulePollDuration();
2579 }
2580
2581 mSourceFlags = flags;
2582 break;
2583 }
2584
2585 case Source::kWhatVideoSizeChanged:
2586 {
Chong Zhangced1c2f2014-08-08 15:22:35 -07002587 sp<AMessage> format;
2588 CHECK(msg->findMessage("format", &format));
Andreas Huber9575c962013-02-05 13:59:56 -08002589
Chong Zhangced1c2f2014-08-08 15:22:35 -07002590 updateVideoSize(format);
Andreas Huber9575c962013-02-05 13:59:56 -08002591 break;
2592 }
2593
Chong Zhang2a3cc9a2014-08-21 17:48:26 -07002594 case Source::kWhatBufferingUpdate:
2595 {
2596 int32_t percentage;
2597 CHECK(msg->findInt32("percentage", &percentage));
2598
2599 notifyListener(MEDIA_BUFFERING_UPDATE, percentage, 0);
2600 break;
2601 }
2602
Chong Zhangefbb6192015-01-30 17:13:27 -08002603 case Source::kWhatPauseOnBufferingStart:
2604 {
2605 // ignore if not playing
Chong Zhang8a048332015-05-06 15:16:28 -07002606 if (mStarted) {
Chong Zhangefbb6192015-01-30 17:13:27 -08002607 ALOGI("buffer low, pausing...");
2608
Ray Essick58e0f7a2017-11-21 10:59:38 -08002609 startRebufferingTimer();
Chong Zhang8a048332015-05-06 15:16:28 -07002610 mPausedForBuffering = true;
Chong Zhangefbb6192015-01-30 17:13:27 -08002611 onPause();
2612 }
Wei Jiabfe82072016-05-20 09:21:50 -07002613 notifyListener(MEDIA_INFO, MEDIA_INFO_BUFFERING_START, 0);
Roger Jönssonb50e83e2013-01-21 16:26:41 +01002614 break;
2615 }
2616
Chong Zhangefbb6192015-01-30 17:13:27 -08002617 case Source::kWhatResumeOnBufferingEnd:
2618 {
2619 // ignore if not playing
Chong Zhang8a048332015-05-06 15:16:28 -07002620 if (mStarted) {
Chong Zhangefbb6192015-01-30 17:13:27 -08002621 ALOGI("buffer ready, resuming...");
2622
Ray Essickeda32522018-02-28 12:08:28 -08002623 updateRebufferingTimer(true /* stopping */, false /* exiting */);
Chong Zhang8a048332015-05-06 15:16:28 -07002624 mPausedForBuffering = false;
2625
2626 // do not resume yet if client didn't unpause
2627 if (!mPausedByClient) {
2628 onResume();
2629 }
Chong Zhangefbb6192015-01-30 17:13:27 -08002630 }
Wei Jia3bed45a2016-02-17 11:06:47 -08002631 notifyListener(MEDIA_INFO, MEDIA_INFO_BUFFERING_END, 0);
Roger Jönssonb50e83e2013-01-21 16:26:41 +01002632 break;
2633 }
2634
Chong Zhangefbb6192015-01-30 17:13:27 -08002635 case Source::kWhatCacheStats:
2636 {
2637 int32_t kbps;
2638 CHECK(msg->findInt32("bandwidth", &kbps));
2639
2640 notifyListener(MEDIA_INFO, MEDIA_INFO_NETWORK_BANDWIDTH, kbps);
2641 break;
2642 }
2643
Chong Zhangdcb89b32013-08-06 09:44:47 -07002644 case Source::kWhatSubtitleData:
2645 {
2646 sp<ABuffer> buffer;
2647 CHECK(msg->findBuffer("buffer", &buffer));
2648
Chong Zhang404fced2014-06-11 14:45:31 -07002649 sendSubtitleData(buffer, 0 /* baseIndex */);
Chong Zhangdcb89b32013-08-06 09:44:47 -07002650 break;
2651 }
2652
Robert Shih08528432015-04-08 09:06:54 -07002653 case Source::kWhatTimedMetaData:
2654 {
2655 sp<ABuffer> buffer;
2656 if (!msg->findBuffer("buffer", &buffer)) {
2657 notifyListener(MEDIA_INFO, MEDIA_INFO_METADATA_UPDATE, 0);
2658 } else {
2659 sendTimedMetaData(buffer);
2660 }
2661 break;
2662 }
2663
Robert Shihd3b0bbb2014-07-23 15:00:25 -07002664 case Source::kWhatTimedTextData:
2665 {
2666 int32_t generation;
2667 if (msg->findInt32("generation", &generation)
2668 && generation != mTimedTextGeneration) {
2669 break;
2670 }
2671
2672 sp<ABuffer> buffer;
2673 CHECK(msg->findBuffer("buffer", &buffer));
2674
2675 sp<NuPlayerDriver> driver = mDriver.promote();
2676 if (driver == NULL) {
2677 break;
2678 }
2679
2680 int posMs;
2681 int64_t timeUs, posUs;
2682 driver->getCurrentPosition(&posMs);
Chih-Hung Hsieh62309d52018-12-11 13:54:02 -08002683 posUs = (int64_t) posMs * 1000LL;
Robert Shihd3b0bbb2014-07-23 15:00:25 -07002684 CHECK(buffer->meta()->findInt64("timeUs", &timeUs));
2685
2686 if (posUs < timeUs) {
2687 if (!msg->findInt32("generation", &generation)) {
2688 msg->setInt32("generation", mTimedTextGeneration);
2689 }
2690 msg->post(timeUs - posUs);
2691 } else {
2692 sendTimedTextData(buffer);
2693 }
2694 break;
2695 }
2696
Andreas Huber14f76722013-01-15 09:04:18 -08002697 case Source::kWhatQueueDecoderShutdown:
2698 {
2699 int32_t audio, video;
2700 CHECK(msg->findInt32("audio", &audio));
2701 CHECK(msg->findInt32("video", &video));
2702
2703 sp<AMessage> reply;
2704 CHECK(msg->findMessage("reply", &reply));
2705
2706 queueDecoderShutdown(audio, video, reply);
2707 break;
2708 }
2709
Ronghua Wu80276872014-08-28 15:50:29 -07002710 case Source::kWhatDrmNoLicense:
2711 {
2712 notifyListener(MEDIA_ERROR, MEDIA_ERROR_UNKNOWN, ERROR_DRM_NO_LICENSE);
2713 break;
2714 }
2715
Andreas Huber9575c962013-02-05 13:59:56 -08002716 default:
2717 TRESPASS();
2718 }
2719}
2720
Chong Zhanga7fa1d92014-06-11 14:49:23 -07002721void NuPlayer::onClosedCaptionNotify(const sp<AMessage> &msg) {
2722 int32_t what;
2723 CHECK(msg->findInt32("what", &what));
2724
2725 switch (what) {
2726 case NuPlayer::CCDecoder::kWhatClosedCaptionData:
2727 {
2728 sp<ABuffer> buffer;
2729 CHECK(msg->findBuffer("buffer", &buffer));
2730
2731 size_t inbandTracks = 0;
2732 if (mSource != NULL) {
2733 inbandTracks = mSource->getTrackCount();
2734 }
2735
2736 sendSubtitleData(buffer, inbandTracks);
2737 break;
2738 }
2739
2740 case NuPlayer::CCDecoder::kWhatTrackAdded:
2741 {
2742 notifyListener(MEDIA_INFO, MEDIA_INFO_METADATA_UPDATE, 0);
2743
2744 break;
2745 }
2746
2747 default:
2748 TRESPASS();
2749 }
2750
2751
2752}
2753
Chong Zhang404fced2014-06-11 14:45:31 -07002754void NuPlayer::sendSubtitleData(const sp<ABuffer> &buffer, int32_t baseIndex) {
2755 int32_t trackIndex;
2756 int64_t timeUs, durationUs;
Robert Shihd83d4f42018-02-24 19:02:46 -08002757 CHECK(buffer->meta()->findInt32("track-index", &trackIndex));
Chong Zhang404fced2014-06-11 14:45:31 -07002758 CHECK(buffer->meta()->findInt64("timeUs", &timeUs));
2759 CHECK(buffer->meta()->findInt64("durationUs", &durationUs));
2760
2761 Parcel in;
2762 in.writeInt32(trackIndex + baseIndex);
2763 in.writeInt64(timeUs);
2764 in.writeInt64(durationUs);
2765 in.writeInt32(buffer->size());
2766 in.writeInt32(buffer->size());
2767 in.write(buffer->data(), buffer->size());
2768
2769 notifyListener(MEDIA_SUBTITLE_DATA, 0, 0, &in);
2770}
Robert Shihd3b0bbb2014-07-23 15:00:25 -07002771
Robert Shih08528432015-04-08 09:06:54 -07002772void NuPlayer::sendTimedMetaData(const sp<ABuffer> &buffer) {
2773 int64_t timeUs;
2774 CHECK(buffer->meta()->findInt64("timeUs", &timeUs));
2775
2776 Parcel in;
2777 in.writeInt64(timeUs);
2778 in.writeInt32(buffer->size());
2779 in.writeInt32(buffer->size());
2780 in.write(buffer->data(), buffer->size());
2781
2782 notifyListener(MEDIA_META_DATA, 0, 0, &in);
2783}
2784
Robert Shihd3b0bbb2014-07-23 15:00:25 -07002785void NuPlayer::sendTimedTextData(const sp<ABuffer> &buffer) {
2786 const void *data;
2787 size_t size = 0;
2788 int64_t timeUs;
Marco Nelissen55e2f4c2015-09-04 15:57:15 -07002789 int32_t flag = TextDescriptions::IN_BAND_TEXT_3GPP;
Robert Shihd3b0bbb2014-07-23 15:00:25 -07002790
2791 AString mime;
2792 CHECK(buffer->meta()->findString("mime", &mime));
2793 CHECK(strcasecmp(mime.c_str(), MEDIA_MIMETYPE_TEXT_3GPP) == 0);
2794
2795 data = buffer->data();
2796 size = buffer->size();
2797
2798 Parcel parcel;
2799 if (size > 0) {
2800 CHECK(buffer->meta()->findInt64("timeUs", &timeUs));
Marco Nelissen55e2f4c2015-09-04 15:57:15 -07002801 int32_t global = 0;
2802 if (buffer->meta()->findInt32("global", &global) && global) {
2803 flag |= TextDescriptions::GLOBAL_DESCRIPTIONS;
2804 } else {
2805 flag |= TextDescriptions::LOCAL_DESCRIPTIONS;
2806 }
Robert Shihd3b0bbb2014-07-23 15:00:25 -07002807 TextDescriptions::getParcelOfDescriptions(
2808 (const uint8_t *)data, size, flag, timeUs / 1000, &parcel);
2809 }
2810
2811 if ((parcel.dataSize() > 0)) {
2812 notifyListener(MEDIA_TIMED_TEXT, 0, 0, &parcel);
2813 } else { // send an empty timed text
2814 notifyListener(MEDIA_TIMED_TEXT, 0, 0);
2815 }
2816}
Hassan Shojaniacefac142017-02-06 21:02:02 -08002817
Hassan Shojaniaff63de72017-04-26 15:10:42 -07002818const char *NuPlayer::getDataSourceType() {
2819 switch (mDataSourceType) {
2820 case DATA_SOURCE_TYPE_HTTP_LIVE:
2821 return "HTTPLive";
2822
2823 case DATA_SOURCE_TYPE_RTSP:
2824 return "RTSP";
2825
2826 case DATA_SOURCE_TYPE_GENERIC_URL:
2827 return "GenURL";
2828
2829 case DATA_SOURCE_TYPE_GENERIC_FD:
2830 return "GenFD";
2831
2832 case DATA_SOURCE_TYPE_MEDIA:
2833 return "Media";
2834
2835 case DATA_SOURCE_TYPE_STREAM:
2836 return "Stream";
2837
2838 case DATA_SOURCE_TYPE_NONE:
2839 default:
2840 return "None";
2841 }
2842 }
2843
Hassan Shojaniacefac142017-02-06 21:02:02 -08002844// Modular DRM begin
2845status_t NuPlayer::prepareDrm(const uint8_t uuid[16], const Vector<uint8_t> &drmSessionId)
2846{
2847 ALOGV("prepareDrm ");
2848
2849 // Passing to the looper anyway; called in a pre-config prepared state so no race on mCrypto
2850 sp<AMessage> msg = new AMessage(kWhatPrepareDrm, this);
2851 // synchronous call so just passing the address but with local copies of "const" args
2852 uint8_t UUID[16];
2853 memcpy(UUID, uuid, sizeof(UUID));
2854 Vector<uint8_t> sessionId = drmSessionId;
2855 msg->setPointer("uuid", (void*)UUID);
2856 msg->setPointer("drmSessionId", (void*)&sessionId);
2857
2858 sp<AMessage> response;
2859 status_t status = msg->postAndAwaitResponse(&response);
2860
2861 if (status == OK && response != NULL) {
2862 CHECK(response->findInt32("status", &status));
2863 ALOGV("prepareDrm ret: %d ", status);
2864 } else {
2865 ALOGE("prepareDrm err: %d", status);
2866 }
2867
2868 return status;
2869}
2870
2871status_t NuPlayer::releaseDrm()
2872{
2873 ALOGV("releaseDrm ");
2874
2875 sp<AMessage> msg = new AMessage(kWhatReleaseDrm, this);
2876
2877 sp<AMessage> response;
2878 status_t status = msg->postAndAwaitResponse(&response);
2879
2880 if (status == OK && response != NULL) {
2881 CHECK(response->findInt32("status", &status));
2882 ALOGV("releaseDrm ret: %d ", status);
2883 } else {
2884 ALOGE("releaseDrm err: %d", status);
2885 }
2886
2887 return status;
2888}
2889
2890status_t NuPlayer::onPrepareDrm(const sp<AMessage> &msg)
2891{
2892 // TODO change to ALOGV
2893 ALOGD("onPrepareDrm ");
2894
2895 status_t status = INVALID_OPERATION;
2896 if (mSource == NULL) {
2897 ALOGE("onPrepareDrm: No source. onPrepareDrm failed with %d.", status);
2898 return status;
2899 }
2900
2901 uint8_t *uuid;
2902 Vector<uint8_t> *drmSessionId;
2903 CHECK(msg->findPointer("uuid", (void**)&uuid));
2904 CHECK(msg->findPointer("drmSessionId", (void**)&drmSessionId));
2905
2906 status = OK;
2907 sp<ICrypto> crypto = NULL;
2908
2909 status = mSource->prepareDrm(uuid, *drmSessionId, &crypto);
2910 if (crypto == NULL) {
2911 ALOGE("onPrepareDrm: mSource->prepareDrm failed. status: %d", status);
2912 return status;
2913 }
2914 ALOGV("onPrepareDrm: mSource->prepareDrm succeeded");
2915
2916 if (mCrypto != NULL) {
2917 ALOGE("onPrepareDrm: Unexpected. Already having mCrypto: %p (%d)",
2918 mCrypto.get(), mCrypto->getStrongCount());
2919 mCrypto.clear();
2920 }
2921
2922 mCrypto = crypto;
2923 mIsDrmProtected = true;
2924 // TODO change to ALOGV
2925 ALOGD("onPrepareDrm: mCrypto: %p (%d)", mCrypto.get(),
2926 (mCrypto != NULL ? mCrypto->getStrongCount() : 0));
2927
2928 return status;
2929}
2930
2931status_t NuPlayer::onReleaseDrm()
2932{
2933 // TODO change to ALOGV
2934 ALOGD("onReleaseDrm ");
2935
Hassan Shojania50b20c92017-02-16 18:28:58 -08002936 if (!mIsDrmProtected) {
2937 ALOGW("onReleaseDrm: Unexpected. mIsDrmProtected is already false.");
2938 }
2939
2940 mIsDrmProtected = false;
Hassan Shojaniacefac142017-02-06 21:02:02 -08002941
2942 status_t status;
2943 if (mCrypto != NULL) {
Hassan Shojania355e8472017-05-12 10:33:16 -07002944 // notifying the source first before removing crypto from codec
2945 if (mSource != NULL) {
2946 mSource->releaseDrm();
2947 }
2948
Hassan Shojaniacefac142017-02-06 21:02:02 -08002949 status=OK;
2950 // first making sure the codecs have released their crypto reference
2951 const sp<DecoderBase> &videoDecoder = getDecoder(false/*audio*/);
2952 if (videoDecoder != NULL) {
2953 status = videoDecoder->releaseCrypto();
2954 ALOGV("onReleaseDrm: video decoder ret: %d", status);
2955 }
2956
2957 const sp<DecoderBase> &audioDecoder = getDecoder(true/*audio*/);
2958 if (audioDecoder != NULL) {
2959 status_t status_audio = audioDecoder->releaseCrypto();
2960 if (status == OK) { // otherwise, returning the first error
2961 status = status_audio;
2962 }
2963 ALOGV("onReleaseDrm: audio decoder ret: %d", status_audio);
2964 }
2965
2966 // TODO change to ALOGV
2967 ALOGD("onReleaseDrm: mCrypto: %p (%d)", mCrypto.get(),
2968 (mCrypto != NULL ? mCrypto->getStrongCount() : 0));
2969 mCrypto.clear();
2970 } else { // mCrypto == NULL
2971 ALOGE("onReleaseDrm: Unexpected. There is no crypto.");
2972 status = INVALID_OPERATION;
2973 }
2974
2975 return status;
2976}
2977// Modular DRM end
Andreas Huberb5f25f02013-02-05 10:14:26 -08002978////////////////////////////////////////////////////////////////////////////////
2979
Chong Zhangced1c2f2014-08-08 15:22:35 -07002980sp<AMessage> NuPlayer::Source::getFormat(bool audio) {
2981 sp<MetaData> meta = getFormatMeta(audio);
2982
2983 if (meta == NULL) {
2984 return NULL;
2985 }
2986
2987 sp<AMessage> msg = new AMessage;
2988
2989 if(convertMetaDataToMessage(meta, &msg) == OK) {
2990 return msg;
2991 }
2992 return NULL;
2993}
2994
Andreas Huber9575c962013-02-05 13:59:56 -08002995void NuPlayer::Source::notifyFlagsChanged(uint32_t flags) {
2996 sp<AMessage> notify = dupNotify();
2997 notify->setInt32("what", kWhatFlagsChanged);
2998 notify->setInt32("flags", flags);
2999 notify->post();
3000}
3001
Chong Zhangced1c2f2014-08-08 15:22:35 -07003002void NuPlayer::Source::notifyVideoSizeChanged(const sp<AMessage> &format) {
Andreas Huber9575c962013-02-05 13:59:56 -08003003 sp<AMessage> notify = dupNotify();
3004 notify->setInt32("what", kWhatVideoSizeChanged);
Chong Zhangced1c2f2014-08-08 15:22:35 -07003005 notify->setMessage("format", format);
Andreas Huber9575c962013-02-05 13:59:56 -08003006 notify->post();
3007}
3008
Andreas Huberec0c5972013-02-05 14:47:13 -08003009void NuPlayer::Source::notifyPrepared(status_t err) {
Hassan Shojaniacefac142017-02-06 21:02:02 -08003010 ALOGV("Source::notifyPrepared %d", err);
Andreas Huber9575c962013-02-05 13:59:56 -08003011 sp<AMessage> notify = dupNotify();
3012 notify->setInt32("what", kWhatPrepared);
Andreas Huberec0c5972013-02-05 14:47:13 -08003013 notify->setInt32("err", err);
Andreas Huber9575c962013-02-05 13:59:56 -08003014 notify->post();
3015}
3016
Hassan Shojaniacefac142017-02-06 21:02:02 -08003017void NuPlayer::Source::notifyDrmInfo(const sp<ABuffer> &drmInfoBuffer)
3018{
3019 ALOGV("Source::notifyDrmInfo");
3020
3021 sp<AMessage> notify = dupNotify();
3022 notify->setInt32("what", kWhatDrmInfo);
3023 notify->setBuffer("drmInfo", drmInfoBuffer);
3024
3025 notify->post();
3026}
3027
Lajos Molnarfcd3e942015-03-31 10:06:48 -07003028void NuPlayer::Source::notifyInstantiateSecureDecoders(const sp<AMessage> &reply) {
3029 sp<AMessage> notify = dupNotify();
3030 notify->setInt32("what", kWhatInstantiateSecureDecoders);
3031 notify->setMessage("reply", reply);
3032 notify->post();
3033}
3034
Andreas Huber84333e02014-02-07 15:36:10 -08003035void NuPlayer::Source::onMessageReceived(const sp<AMessage> & /* msg */) {
Andreas Huberb5f25f02013-02-05 10:14:26 -08003036 TRESPASS();
3037}
3038
Andreas Huberf9334412010-12-15 15:17:42 -08003039} // namespace android