blob: 3c4a96b9f4823b7de4adf090baafbf4125b32433 [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"
Byeongjo Park28225ab2019-01-24 20:31:19 +090034#include "RTPSource.h"
Andreas Huber2bfdd422011-10-11 15:24:07 -070035#include "RTSPSource.h"
Andreas Huber5bc087c2010-12-23 10:27:40 -080036#include "StreamingSource.h"
Andreas Huberafed0e12011-09-20 15:39:58 -070037#include "GenericSource.h"
Robert Shihd3b0bbb2014-07-23 15:00:25 -070038#include "TextDescriptions.h"
Andreas Huber5bc087c2010-12-23 10:27:40 -080039
40#include "ATSParser.h"
Andreas Huberf9334412010-12-15 15:17:42 -080041
Lajos Molnard9fd6312014-11-06 11:00:00 -080042#include <cutils/properties.h>
43
Lajos Molnar3a474aa2015-04-24 17:10:07 -070044#include <media/AudioResamplerPublic.h>
45#include <media/AVSyncSettings.h>
Wonsik Kim7e34bf52016-08-23 00:09:18 +090046#include <media/MediaCodecBuffer.h>
Lajos Molnar3a474aa2015-04-24 17:10:07 -070047
Andreas Huber3831a062010-12-21 10:22:33 -080048#include <media/stagefright/foundation/hexdump.h>
Andreas Huberf9334412010-12-15 15:17:42 -080049#include <media/stagefright/foundation/ABuffer.h>
50#include <media/stagefright/foundation/ADebug.h>
51#include <media/stagefright/foundation/AMessage.h>
Dongwon Kangd91dc5a2017-10-10 00:07:09 -070052#include <media/stagefright/foundation/avc_utils.h>
Lajos Molnar09524832014-07-17 14:29:51 -070053#include <media/stagefright/MediaBuffer.h>
Wei Jia0a68f662017-08-30 18:01:26 -070054#include <media/stagefright/MediaClock.h>
Andreas Huber3fe62152011-09-16 15:09:22 -070055#include <media/stagefright/MediaDefs.h>
Andreas Huberf9334412010-12-15 15:17:42 -080056#include <media/stagefright/MediaErrors.h>
57#include <media/stagefright/MetaData.h>
Lajos Molnar1de1e252015-04-30 18:18:34 -070058
Andy McFadden8ba01022012-12-18 09:46:54 -080059#include <gui/IGraphicBufferProducer.h>
Lajos Molnar1de1e252015-04-30 18:18:34 -070060#include <gui/Surface.h>
Andreas Huberf9334412010-12-15 15:17:42 -080061
Andreas Huber3fe62152011-09-16 15:09:22 -070062
Andreas Huber84066782011-08-16 09:34:26 -070063#include "ESDS.h"
64#include <media/stagefright/Utils.h>
65
Andreas Huberf9334412010-12-15 15:17:42 -080066namespace android {
67
Andreas Hubera1f8ab02012-11-30 10:53:22 -080068struct NuPlayer::Action : public RefBase {
69 Action() {}
70
71 virtual void execute(NuPlayer *player) = 0;
72
73private:
74 DISALLOW_EVIL_CONSTRUCTORS(Action);
75};
76
77struct NuPlayer::SeekAction : public Action {
Wei Jiac5de0912016-11-18 10:22:14 -080078 explicit SeekAction(int64_t seekTimeUs, MediaPlayerSeekMode mode)
Wei Jia14486822016-11-02 17:51:30 -070079 : mSeekTimeUs(seekTimeUs),
Wei Jiac5de0912016-11-18 10:22:14 -080080 mMode(mode) {
Andreas Hubera1f8ab02012-11-30 10:53:22 -080081 }
82
83 virtual void execute(NuPlayer *player) {
Wei Jiac5de0912016-11-18 10:22:14 -080084 player->performSeek(mSeekTimeUs, mMode);
Andreas Hubera1f8ab02012-11-30 10:53:22 -080085 }
86
87private:
88 int64_t mSeekTimeUs;
Wei Jiac5de0912016-11-18 10:22:14 -080089 MediaPlayerSeekMode mMode;
Andreas Hubera1f8ab02012-11-30 10:53:22 -080090
91 DISALLOW_EVIL_CONSTRUCTORS(SeekAction);
92};
93
Chong Zhangf8d71772014-11-26 15:08:34 -080094struct NuPlayer::ResumeDecoderAction : public Action {
Chih-Hung Hsieh090ef602016-04-27 10:39:54 -070095 explicit ResumeDecoderAction(bool needNotify)
Chong Zhangf8d71772014-11-26 15:08:34 -080096 : mNeedNotify(needNotify) {
97 }
98
99 virtual void execute(NuPlayer *player) {
100 player->performResumeDecoders(mNeedNotify);
101 }
102
103private:
104 bool mNeedNotify;
105
106 DISALLOW_EVIL_CONSTRUCTORS(ResumeDecoderAction);
107};
108
Andreas Huber57a339c2012-12-03 11:18:00 -0800109struct NuPlayer::SetSurfaceAction : public Action {
Chih-Hung Hsieh090ef602016-04-27 10:39:54 -0700110 explicit SetSurfaceAction(const sp<Surface> &surface)
Lajos Molnar1de1e252015-04-30 18:18:34 -0700111 : mSurface(surface) {
Andreas Huber57a339c2012-12-03 11:18:00 -0800112 }
113
114 virtual void execute(NuPlayer *player) {
Lajos Molnar1de1e252015-04-30 18:18:34 -0700115 player->performSetSurface(mSurface);
Andreas Huber57a339c2012-12-03 11:18:00 -0800116 }
117
118private:
Lajos Molnar1de1e252015-04-30 18:18:34 -0700119 sp<Surface> mSurface;
Andreas Huber57a339c2012-12-03 11:18:00 -0800120
121 DISALLOW_EVIL_CONSTRUCTORS(SetSurfaceAction);
122};
123
Wei Jiafef808d2014-10-31 17:57:05 -0700124struct NuPlayer::FlushDecoderAction : public Action {
125 FlushDecoderAction(FlushCommand audio, FlushCommand video)
Andreas Huber14f76722013-01-15 09:04:18 -0800126 : mAudio(audio),
127 mVideo(video) {
128 }
129
130 virtual void execute(NuPlayer *player) {
Wei Jiafef808d2014-10-31 17:57:05 -0700131 player->performDecoderFlush(mAudio, mVideo);
Andreas Huber14f76722013-01-15 09:04:18 -0800132 }
133
134private:
Wei Jiafef808d2014-10-31 17:57:05 -0700135 FlushCommand mAudio;
136 FlushCommand mVideo;
Andreas Huber14f76722013-01-15 09:04:18 -0800137
Wei Jiafef808d2014-10-31 17:57:05 -0700138 DISALLOW_EVIL_CONSTRUCTORS(FlushDecoderAction);
Andreas Huber14f76722013-01-15 09:04:18 -0800139};
140
141struct NuPlayer::PostMessageAction : public Action {
Chih-Hung Hsieh090ef602016-04-27 10:39:54 -0700142 explicit PostMessageAction(const sp<AMessage> &msg)
Andreas Huber14f76722013-01-15 09:04:18 -0800143 : mMessage(msg) {
144 }
145
146 virtual void execute(NuPlayer *) {
147 mMessage->post();
148 }
149
150private:
151 sp<AMessage> mMessage;
152
153 DISALLOW_EVIL_CONSTRUCTORS(PostMessageAction);
154};
155
Andreas Hubera1f8ab02012-11-30 10:53:22 -0800156// Use this if there's no state necessary to save in order to execute
157// the action.
158struct NuPlayer::SimpleAction : public Action {
159 typedef void (NuPlayer::*ActionFunc)();
160
Chih-Hung Hsieh090ef602016-04-27 10:39:54 -0700161 explicit SimpleAction(ActionFunc func)
Andreas Hubera1f8ab02012-11-30 10:53:22 -0800162 : mFunc(func) {
163 }
164
165 virtual void execute(NuPlayer *player) {
166 (player->*mFunc)();
167 }
168
169private:
170 ActionFunc mFunc;
171
172 DISALLOW_EVIL_CONSTRUCTORS(SimpleAction);
173};
174
Andreas Huberf9334412010-12-15 15:17:42 -0800175////////////////////////////////////////////////////////////////////////////////
176
Wei Jia0a68f662017-08-30 18:01:26 -0700177NuPlayer::NuPlayer(pid_t pid, const sp<MediaClock> &mediaClock)
Andreas Huber9b80c2b2011-06-30 15:47:02 -0700178 : mUIDValid(false),
Ronghua Wu68845c12015-07-21 09:50:48 -0700179 mPID(pid),
Wei Jia0a68f662017-08-30 18:01:26 -0700180 mMediaClock(mediaClock),
Andreas Huber9575c962013-02-05 13:59:56 -0800181 mSourceFlags(0),
Wei Jiabc2fb722014-07-08 16:37:57 -0700182 mOffloadAudio(false),
Wei Jia88703c32014-08-06 11:24:07 -0700183 mAudioDecoderGeneration(0),
184 mVideoDecoderGeneration(0),
Wei Jia57568df2014-09-22 10:16:29 -0700185 mRendererGeneration(0),
Ray Essick0d98c182017-11-09 13:42:42 -0800186 mLastStartedPlayingTimeNs(0),
Ray Essickeda32522018-02-28 12:08:28 -0800187 mLastStartedRebufferingTimeNs(0),
Robert Shih1a5c8592015-08-04 18:07:44 -0700188 mPreviousSeekTimeUs(0),
Andreas Huber9b80c2b2011-06-30 15:47:02 -0700189 mAudioEOS(false),
Andreas Huberf9334412010-12-15 15:17:42 -0800190 mVideoEOS(false),
Andreas Huber5bc087c2010-12-23 10:27:40 -0800191 mScanSourcesPending(false),
Andreas Huber1aef2112011-01-04 14:01:29 -0800192 mScanSourcesGeneration(0),
Andreas Huberb7c8e912012-11-27 15:02:53 -0800193 mPollDurationGeneration(0),
Robert Shihd3b0bbb2014-07-23 15:00:25 -0700194 mTimedTextGeneration(0),
Andreas Huberf9334412010-12-15 15:17:42 -0800195 mFlushingAudio(NONE),
Andreas Huber1aef2112011-01-04 14:01:29 -0800196 mFlushingVideo(NONE),
Chong Zhangf8d71772014-11-26 15:08:34 -0800197 mResumePending(false),
Andreas Huber57a339c2012-12-03 11:18:00 -0800198 mVideoScalingMode(NATIVE_WINDOW_SCALING_MODE_SCALE_TO_WINDOW),
Lajos Molnar3a474aa2015-04-24 17:10:07 -0700199 mPlaybackSettings(AUDIO_PLAYBACK_RATE_DEFAULT),
200 mVideoFpsHint(-1.f),
Chong Zhangefbb6192015-01-30 17:13:27 -0800201 mStarted(false),
Wei Jia8a092d32016-06-03 14:57:24 -0700202 mPrepared(false),
Ronghua Wu64c2d172015-10-07 16:52:19 -0700203 mResetting(false),
Robert Shih0c61a0d2015-07-06 15:09:10 -0700204 mSourceStarted(false),
Wei Jia686e8e52017-04-03 14:08:01 -0700205 mAudioDecoderError(false),
206 mVideoDecoderError(false),
Chong Zhangefbb6192015-01-30 17:13:27 -0800207 mPaused(false),
Wei Jia71c75e02016-02-04 09:40:47 -0800208 mPausedByClient(true),
Hassan Shojania50b20c92017-02-16 18:28:58 -0800209 mPausedForBuffering(false),
Hassan Shojaniaff63de72017-04-26 15:10:42 -0700210 mIsDrmProtected(false),
211 mDataSourceType(DATA_SOURCE_TYPE_NONE) {
Wei Jia0a68f662017-08-30 18:01:26 -0700212 CHECK(mediaClock != NULL);
Andy Hung8d121d42014-10-03 09:53:53 -0700213 clearFlushComplete();
Andreas Huberf9334412010-12-15 15:17:42 -0800214}
215
216NuPlayer::~NuPlayer() {
217}
218
Andreas Huber9b80c2b2011-06-30 15:47:02 -0700219void NuPlayer::setUID(uid_t uid) {
220 mUIDValid = true;
221 mUID = uid;
222}
223
Dongwon Kang47afe0a2018-03-27 15:15:30 -0700224void NuPlayer::init(const wp<NuPlayerDriver> &driver) {
Andreas Huber43c3e6c2011-01-05 12:17:08 -0800225 mDriver = driver;
Dongwon Kang47afe0a2018-03-27 15:15:30 -0700226
227 sp<AMessage> notify = new AMessage(kWhatMediaClockNotify, this);
228 mMediaClock->setNotificationMessage(notify);
Andreas Huberf9334412010-12-15 15:17:42 -0800229}
230
Andreas Huber9575c962013-02-05 13:59:56 -0800231void NuPlayer::setDataSourceAsync(const sp<IStreamSource> &source) {
Lajos Molnar1d15ab52015-03-04 16:46:34 -0800232 sp<AMessage> msg = new AMessage(kWhatSetDataSource, this);
Andreas Huberf9334412010-12-15 15:17:42 -0800233
Lajos Molnar1d15ab52015-03-04 16:46:34 -0800234 sp<AMessage> notify = new AMessage(kWhatSourceNotify, this);
Andreas Huberb5f25f02013-02-05 10:14:26 -0800235
Andreas Huber240abcc2014-02-13 13:32:37 -0800236 msg->setObject("source", new StreamingSource(notify, source));
Andreas Huber5bc087c2010-12-23 10:27:40 -0800237 msg->post();
Hassan Shojaniaff63de72017-04-26 15:10:42 -0700238 mDataSourceType = DATA_SOURCE_TYPE_STREAM;
Andreas Huber5bc087c2010-12-23 10:27:40 -0800239}
Andreas Huberf9334412010-12-15 15:17:42 -0800240
Andreas Huberafed0e12011-09-20 15:39:58 -0700241static bool IsHTTPLiveURL(const char *url) {
242 if (!strncasecmp("http://", url, 7)
Andreas Huber99759402013-04-01 14:28:31 -0700243 || !strncasecmp("https://", url, 8)
244 || !strncasecmp("file://", url, 7)) {
Andreas Huberafed0e12011-09-20 15:39:58 -0700245 size_t len = strlen(url);
246 if (len >= 5 && !strcasecmp(".m3u8", &url[len - 5])) {
247 return true;
248 }
249
250 if (strstr(url,"m3u8")) {
251 return true;
252 }
253 }
254
255 return false;
256}
257
Andreas Huber9575c962013-02-05 13:59:56 -0800258void NuPlayer::setDataSourceAsync(
Andreas Huber1b86fe02014-01-29 11:13:26 -0800259 const sp<IMediaHTTPService> &httpService,
260 const char *url,
261 const KeyedVector<String8, String8> *headers) {
Chong Zhang3de157d2014-08-05 20:54:44 -0700262
Lajos Molnar1d15ab52015-03-04 16:46:34 -0800263 sp<AMessage> msg = new AMessage(kWhatSetDataSource, this);
Oscar Rydhé7a33b772012-02-20 10:15:48 +0100264 size_t len = strlen(url);
Andreas Huber5bc087c2010-12-23 10:27:40 -0800265
Lajos Molnar1d15ab52015-03-04 16:46:34 -0800266 sp<AMessage> notify = new AMessage(kWhatSourceNotify, this);
Andreas Huberb5f25f02013-02-05 10:14:26 -0800267
Andreas Huberafed0e12011-09-20 15:39:58 -0700268 sp<Source> source;
269 if (IsHTTPLiveURL(url)) {
Andreas Huber81e68442014-02-05 11:52:33 -0800270 source = new HTTPLiveSource(notify, httpService, url, headers);
Hassan Shojaniacefac142017-02-06 21:02:02 -0800271 ALOGV("setDataSourceAsync HTTPLiveSource %s", url);
Hassan Shojaniaff63de72017-04-26 15:10:42 -0700272 mDataSourceType = DATA_SOURCE_TYPE_HTTP_LIVE;
Andreas Huberafed0e12011-09-20 15:39:58 -0700273 } else if (!strncasecmp(url, "rtsp://", 7)) {
Andreas Huber1b86fe02014-01-29 11:13:26 -0800274 source = new RTSPSource(
275 notify, httpService, url, headers, mUIDValid, mUID);
Hassan Shojaniacefac142017-02-06 21:02:02 -0800276 ALOGV("setDataSourceAsync RTSPSource %s", url);
Hassan Shojaniaff63de72017-04-26 15:10:42 -0700277 mDataSourceType = DATA_SOURCE_TYPE_RTSP;
Oscar Rydhé7a33b772012-02-20 10:15:48 +0100278 } else if ((!strncasecmp(url, "http://", 7)
279 || !strncasecmp(url, "https://", 8))
280 && ((len >= 4 && !strcasecmp(".sdp", &url[len - 4]))
281 || strstr(url, ".sdp?"))) {
Andreas Huber1b86fe02014-01-29 11:13:26 -0800282 source = new RTSPSource(
283 notify, httpService, url, headers, mUIDValid, mUID, true);
Hassan Shojaniacefac142017-02-06 21:02:02 -0800284 ALOGV("setDataSourceAsync RTSPSource http/https/.sdp %s", url);
Hassan Shojaniaff63de72017-04-26 15:10:42 -0700285 mDataSourceType = DATA_SOURCE_TYPE_RTSP;
Andreas Huber2bfdd422011-10-11 15:24:07 -0700286 } else {
Hassan Shojaniacefac142017-02-06 21:02:02 -0800287 ALOGV("setDataSourceAsync GenericSource %s", url);
288
Chong Zhang3de157d2014-08-05 20:54:44 -0700289 sp<GenericSource> genericSource =
Wei Jia992c5592017-09-01 14:20:23 -0700290 new GenericSource(notify, mUIDValid, mUID, mMediaClock);
Andreas Huber2bfdd422011-10-11 15:24:07 -0700291
Chong Zhanga19f33e2014-08-07 15:35:07 -0700292 status_t err = genericSource->setDataSource(httpService, url, headers);
Chong Zhang3de157d2014-08-05 20:54:44 -0700293
294 if (err == OK) {
295 source = genericSource;
296 } else {
Chong Zhanga19f33e2014-08-07 15:35:07 -0700297 ALOGE("Failed to set data source!");
Chong Zhang3de157d2014-08-05 20:54:44 -0700298 }
Hassan Shojaniaff63de72017-04-26 15:10:42 -0700299
300 // regardless of success/failure
301 mDataSourceType = DATA_SOURCE_TYPE_GENERIC_URL;
Chong Zhang3de157d2014-08-05 20:54:44 -0700302 }
Andreas Huberafed0e12011-09-20 15:39:58 -0700303 msg->setObject("source", source);
304 msg->post();
305}
306
Andreas Huber9575c962013-02-05 13:59:56 -0800307void NuPlayer::setDataSourceAsync(int fd, int64_t offset, int64_t length) {
Lajos Molnar1d15ab52015-03-04 16:46:34 -0800308 sp<AMessage> msg = new AMessage(kWhatSetDataSource, this);
Andreas Huberafed0e12011-09-20 15:39:58 -0700309
Lajos Molnar1d15ab52015-03-04 16:46:34 -0800310 sp<AMessage> notify = new AMessage(kWhatSourceNotify, this);
Andreas Huberb5f25f02013-02-05 10:14:26 -0800311
Chong Zhang3de157d2014-08-05 20:54:44 -0700312 sp<GenericSource> source =
Wei Jia992c5592017-09-01 14:20:23 -0700313 new GenericSource(notify, mUIDValid, mUID, mMediaClock);
Chong Zhang3de157d2014-08-05 20:54:44 -0700314
Hassan Shojaniacefac142017-02-06 21:02:02 -0800315 ALOGV("setDataSourceAsync fd %d/%lld/%lld source: %p",
316 fd, (long long)offset, (long long)length, source.get());
317
Chong Zhanga19f33e2014-08-07 15:35:07 -0700318 status_t err = source->setDataSource(fd, offset, length);
Chong Zhang3de157d2014-08-05 20:54:44 -0700319
320 if (err != OK) {
Chong Zhanga19f33e2014-08-07 15:35:07 -0700321 ALOGE("Failed to set data source!");
Chong Zhang3de157d2014-08-05 20:54:44 -0700322 source = NULL;
323 }
324
Andreas Huberafed0e12011-09-20 15:39:58 -0700325 msg->setObject("source", source);
Andreas Huberf9334412010-12-15 15:17:42 -0800326 msg->post();
Hassan Shojaniaff63de72017-04-26 15:10:42 -0700327 mDataSourceType = DATA_SOURCE_TYPE_GENERIC_FD;
Andreas Huberf9334412010-12-15 15:17:42 -0800328}
329
Chris Watkins99f31602015-03-20 13:06:33 -0700330void NuPlayer::setDataSourceAsync(const sp<DataSource> &dataSource) {
331 sp<AMessage> msg = new AMessage(kWhatSetDataSource, this);
332 sp<AMessage> notify = new AMessage(kWhatSourceNotify, this);
333
Wei Jia992c5592017-09-01 14:20:23 -0700334 sp<GenericSource> source = new GenericSource(notify, mUIDValid, mUID, mMediaClock);
Chris Watkins99f31602015-03-20 13:06:33 -0700335 status_t err = source->setDataSource(dataSource);
336
337 if (err != OK) {
338 ALOGE("Failed to set data source!");
339 source = NULL;
340 }
341
342 msg->setObject("source", source);
343 msg->post();
Hassan Shojaniaff63de72017-04-26 15:10:42 -0700344 mDataSourceType = DATA_SOURCE_TYPE_MEDIA;
Chris Watkins99f31602015-03-20 13:06:33 -0700345}
346
Wei Jia9bb38032017-03-23 18:00:38 -0700347status_t NuPlayer::getBufferingSettings(
Wei Jia48fa06d2016-12-20 15:30:49 -0800348 BufferingSettings *buffering /* nonnull */) {
Wei Jia9bb38032017-03-23 18:00:38 -0700349 sp<AMessage> msg = new AMessage(kWhatGetBufferingSettings, this);
Wei Jia48fa06d2016-12-20 15:30:49 -0800350 sp<AMessage> response;
351 status_t err = msg->postAndAwaitResponse(&response);
352 if (err == OK && response != NULL) {
353 CHECK(response->findInt32("err", &err));
354 if (err == OK) {
355 readFromAMessage(response, buffering);
356 }
357 }
358 return err;
359}
360
361status_t NuPlayer::setBufferingSettings(const BufferingSettings& buffering) {
362 sp<AMessage> msg = new AMessage(kWhatSetBufferingSettings, this);
363 writeToAMessage(msg, buffering);
364 sp<AMessage> response;
365 status_t err = msg->postAndAwaitResponse(&response);
366 if (err == OK && response != NULL) {
367 CHECK(response->findInt32("err", &err));
368 }
369 return err;
370}
371
Byeongjo Park28225ab2019-01-24 20:31:19 +0900372void NuPlayer::setDataSourceAsync(const String8& rtpParams) {
373 ALOGD("setDataSourceAsync for RTP = %s", rtpParams.string());
374 sp<AMessage> msg = new AMessage(kWhatSetDataSource, this);
375
376 sp<AMessage> notify = new AMessage(kWhatSourceNotify, this);
377 sp<Source> source = new RTPSource(notify, rtpParams);
378
379 msg->setObject("source", source);
380 msg->post();
381}
382
Andreas Huber9575c962013-02-05 13:59:56 -0800383void NuPlayer::prepareAsync() {
Hassan Shojaniacefac142017-02-06 21:02:02 -0800384 ALOGV("prepareAsync");
385
Lajos Molnar1d15ab52015-03-04 16:46:34 -0800386 (new AMessage(kWhatPrepare, this))->post();
Andreas Huber9575c962013-02-05 13:59:56 -0800387}
388
Andreas Huber57a339c2012-12-03 11:18:00 -0800389void NuPlayer::setVideoSurfaceTextureAsync(
Andy McFadden8ba01022012-12-18 09:46:54 -0800390 const sp<IGraphicBufferProducer> &bufferProducer) {
Lajos Molnar1de1e252015-04-30 18:18:34 -0700391 sp<AMessage> msg = new AMessage(kWhatSetVideoSurface, this);
Andreas Huber57a339c2012-12-03 11:18:00 -0800392
Andy McFadden8ba01022012-12-18 09:46:54 -0800393 if (bufferProducer == NULL) {
Lajos Molnar1de1e252015-04-30 18:18:34 -0700394 msg->setObject("surface", NULL);
Andreas Huber57a339c2012-12-03 11:18:00 -0800395 } else {
Lajos Molnar1de1e252015-04-30 18:18:34 -0700396 msg->setObject("surface", new Surface(bufferProducer, true /* controlledByApp */));
Andreas Huber57a339c2012-12-03 11:18:00 -0800397 }
398
Andreas Huberf9334412010-12-15 15:17:42 -0800399 msg->post();
400}
401
402void NuPlayer::setAudioSink(const sp<MediaPlayerBase::AudioSink> &sink) {
Lajos Molnar1d15ab52015-03-04 16:46:34 -0800403 sp<AMessage> msg = new AMessage(kWhatSetAudioSink, this);
Andreas Huberf9334412010-12-15 15:17:42 -0800404 msg->setObject("sink", sink);
405 msg->post();
406}
407
408void NuPlayer::start() {
Lajos Molnar1d15ab52015-03-04 16:46:34 -0800409 (new AMessage(kWhatStart, this))->post();
Andreas Huberf9334412010-12-15 15:17:42 -0800410}
411
Lajos Molnar3a474aa2015-04-24 17:10:07 -0700412status_t NuPlayer::setPlaybackSettings(const AudioPlaybackRate &rate) {
413 // do some cursory validation of the settings here. audio modes are
414 // only validated when set on the audiosink.
415 if ((rate.mSpeed != 0.f && rate.mSpeed < AUDIO_TIMESTRETCH_SPEED_MIN)
416 || rate.mSpeed > AUDIO_TIMESTRETCH_SPEED_MAX
417 || rate.mPitch < AUDIO_TIMESTRETCH_SPEED_MIN
418 || rate.mPitch > AUDIO_TIMESTRETCH_SPEED_MAX) {
419 return BAD_VALUE;
420 }
421 sp<AMessage> msg = new AMessage(kWhatConfigPlayback, this);
422 writeToAMessage(msg, rate);
423 sp<AMessage> response;
424 status_t err = msg->postAndAwaitResponse(&response);
425 if (err == OK && response != NULL) {
426 CHECK(response->findInt32("err", &err));
427 }
428 return err;
429}
430
431status_t NuPlayer::getPlaybackSettings(AudioPlaybackRate *rate /* nonnull */) {
432 sp<AMessage> msg = new AMessage(kWhatGetPlaybackSettings, this);
433 sp<AMessage> response;
434 status_t err = msg->postAndAwaitResponse(&response);
435 if (err == OK && response != NULL) {
436 CHECK(response->findInt32("err", &err));
437 if (err == OK) {
438 readFromAMessage(response, rate);
439 }
440 }
441 return err;
442}
443
444status_t NuPlayer::setSyncSettings(const AVSyncSettings &sync, float videoFpsHint) {
445 sp<AMessage> msg = new AMessage(kWhatConfigSync, this);
446 writeToAMessage(msg, sync, videoFpsHint);
447 sp<AMessage> response;
448 status_t err = msg->postAndAwaitResponse(&response);
449 if (err == OK && response != NULL) {
450 CHECK(response->findInt32("err", &err));
451 }
452 return err;
453}
454
455status_t NuPlayer::getSyncSettings(
456 AVSyncSettings *sync /* nonnull */, float *videoFps /* nonnull */) {
457 sp<AMessage> msg = new AMessage(kWhatGetSyncSettings, this);
458 sp<AMessage> response;
459 status_t err = msg->postAndAwaitResponse(&response);
460 if (err == OK && response != NULL) {
461 CHECK(response->findInt32("err", &err));
462 if (err == OK) {
463 readFromAMessage(response, sync, videoFps);
464 }
465 }
466 return err;
Wei Jia98160162015-02-04 17:01:11 -0800467}
468
Andreas Huber43c3e6c2011-01-05 12:17:08 -0800469void NuPlayer::pause() {
Lajos Molnar1d15ab52015-03-04 16:46:34 -0800470 (new AMessage(kWhatPause, this))->post();
Andreas Huber43c3e6c2011-01-05 12:17:08 -0800471}
472
Andreas Huber1aef2112011-01-04 14:01:29 -0800473void NuPlayer::resetAsync() {
Wei Jiac45a4b22016-04-15 15:30:23 -0700474 sp<Source> source;
475 {
476 Mutex::Autolock autoLock(mSourceLock);
477 source = mSource;
478 }
479
480 if (source != NULL) {
Chong Zhang48296b72014-09-14 14:28:45 -0700481 // During a reset, the data source might be unresponsive already, we need to
482 // disconnect explicitly so that reads exit promptly.
483 // We can't queue the disconnect request to the looper, as it might be
484 // queued behind a stuck read and never gets processed.
485 // Doing a disconnect outside the looper to allows the pending reads to exit
486 // (either successfully or with error).
Wei Jiac45a4b22016-04-15 15:30:23 -0700487 source->disconnect();
Chong Zhang48296b72014-09-14 14:28:45 -0700488 }
489
Lajos Molnar1d15ab52015-03-04 16:46:34 -0800490 (new AMessage(kWhatReset, this))->post();
Andreas Huber1aef2112011-01-04 14:01:29 -0800491}
492
Wei Jia52c28512017-09-13 18:17:51 -0700493status_t NuPlayer::notifyAt(int64_t mediaTimeUs) {
494 sp<AMessage> notify = new AMessage(kWhatNotifyTime, this);
495 notify->setInt64("timerUs", mediaTimeUs);
496 mMediaClock->addTimer(notify, mediaTimeUs);
497 return OK;
498}
499
Wei Jiac5de0912016-11-18 10:22:14 -0800500void NuPlayer::seekToAsync(int64_t seekTimeUs, MediaPlayerSeekMode mode, bool needNotify) {
Lajos Molnar1d15ab52015-03-04 16:46:34 -0800501 sp<AMessage> msg = new AMessage(kWhatSeek, this);
Andreas Huber43c3e6c2011-01-05 12:17:08 -0800502 msg->setInt64("seekTimeUs", seekTimeUs);
Wei Jiac5de0912016-11-18 10:22:14 -0800503 msg->setInt32("mode", mode);
Wei Jiae427abf2014-09-22 15:21:11 -0700504 msg->setInt32("needNotify", needNotify);
Andreas Huber43c3e6c2011-01-05 12:17:08 -0800505 msg->post();
506}
507
Andreas Huber53df1a42010-12-22 10:03:04 -0800508
Chong Zhang404fced2014-06-11 14:45:31 -0700509void NuPlayer::writeTrackInfo(
Chih-Hung Hsiehe964d4e2016-08-09 14:31:32 -0700510 Parcel* reply, const sp<AMessage>& format) const {
Marco Nelissenc367ca12015-09-15 09:51:59 -0700511 if (format == NULL) {
512 ALOGE("NULL format");
513 return;
514 }
Chong Zhang404fced2014-06-11 14:45:31 -0700515 int32_t trackType;
Marco Nelissen9436e482015-09-15 10:21:53 -0700516 if (!format->findInt32("type", &trackType)) {
Marco Nelissenc367ca12015-09-15 09:51:59 -0700517 ALOGE("no track type");
518 return;
519 }
Chong Zhang404fced2014-06-11 14:45:31 -0700520
Robert Shih755106e2015-04-30 14:36:45 -0700521 AString mime;
Robert Shih2e3a4252015-05-06 10:21:15 -0700522 if (!format->findString("mime", &mime)) {
523 // Java MediaPlayer only uses mimetype for subtitle and timedtext tracks.
524 // If we can't find the mimetype here it means that we wouldn't be needing
525 // the mimetype on the Java end. We still write a placeholder mime to keep the
526 // (de)serialization logic simple.
527 if (trackType == MEDIA_TRACK_TYPE_AUDIO) {
528 mime = "audio/";
529 } else if (trackType == MEDIA_TRACK_TYPE_VIDEO) {
530 mime = "video/";
531 } else {
Marco Nelissenc367ca12015-09-15 09:51:59 -0700532 ALOGE("unknown track type: %d", trackType);
533 return;
Robert Shih2e3a4252015-05-06 10:21:15 -0700534 }
535 }
Robert Shih755106e2015-04-30 14:36:45 -0700536
Chong Zhang404fced2014-06-11 14:45:31 -0700537 AString lang;
Marco Nelissen9436e482015-09-15 10:21:53 -0700538 if (!format->findString("language", &lang)) {
Marco Nelissenc367ca12015-09-15 09:51:59 -0700539 ALOGE("no language");
540 return;
541 }
Chong Zhang404fced2014-06-11 14:45:31 -0700542
543 reply->writeInt32(2); // write something non-zero
544 reply->writeInt32(trackType);
Robert Shih755106e2015-04-30 14:36:45 -0700545 reply->writeString16(String16(mime.c_str()));
Chong Zhang404fced2014-06-11 14:45:31 -0700546 reply->writeString16(String16(lang.c_str()));
547
548 if (trackType == MEDIA_TRACK_TYPE_SUBTITLE) {
Chong Zhang404fced2014-06-11 14:45:31 -0700549 int32_t isAuto, isDefault, isForced;
550 CHECK(format->findInt32("auto", &isAuto));
551 CHECK(format->findInt32("default", &isDefault));
552 CHECK(format->findInt32("forced", &isForced));
553
Chong Zhang404fced2014-06-11 14:45:31 -0700554 reply->writeInt32(isAuto);
555 reply->writeInt32(isDefault);
556 reply->writeInt32(isForced);
557 }
558}
559
Andreas Huberf9334412010-12-15 15:17:42 -0800560void NuPlayer::onMessageReceived(const sp<AMessage> &msg) {
561 switch (msg->what()) {
562 case kWhatSetDataSource:
563 {
Steve Block3856b092011-10-20 11:56:00 +0100564 ALOGV("kWhatSetDataSource");
Andreas Huberf9334412010-12-15 15:17:42 -0800565
566 CHECK(mSource == NULL);
567
Chong Zhang3de157d2014-08-05 20:54:44 -0700568 status_t err = OK;
Andreas Huber5bc087c2010-12-23 10:27:40 -0800569 sp<RefBase> obj;
570 CHECK(msg->findObject("source", &obj));
Chong Zhang3de157d2014-08-05 20:54:44 -0700571 if (obj != NULL) {
Wei Jiac45a4b22016-04-15 15:30:23 -0700572 Mutex::Autolock autoLock(mSourceLock);
Chong Zhang3de157d2014-08-05 20:54:44 -0700573 mSource = static_cast<Source *>(obj.get());
Chong Zhang3de157d2014-08-05 20:54:44 -0700574 } else {
575 err = UNKNOWN_ERROR;
576 }
Andreas Huber9575c962013-02-05 13:59:56 -0800577
578 CHECK(mDriver != NULL);
579 sp<NuPlayerDriver> driver = mDriver.promote();
580 if (driver != NULL) {
Chong Zhang3de157d2014-08-05 20:54:44 -0700581 driver->notifySetDataSourceCompleted(err);
Andreas Huber9575c962013-02-05 13:59:56 -0800582 }
583 break;
584 }
585
Wei Jia9bb38032017-03-23 18:00:38 -0700586 case kWhatGetBufferingSettings:
Wei Jia48fa06d2016-12-20 15:30:49 -0800587 {
588 sp<AReplyToken> replyID;
589 CHECK(msg->senderAwaitsResponse(&replyID));
590
Wei Jia9bb38032017-03-23 18:00:38 -0700591 ALOGV("kWhatGetBufferingSettings");
Wei Jia48fa06d2016-12-20 15:30:49 -0800592 BufferingSettings buffering;
593 status_t err = OK;
594 if (mSource != NULL) {
Wei Jia9bb38032017-03-23 18:00:38 -0700595 err = mSource->getBufferingSettings(&buffering);
Wei Jia48fa06d2016-12-20 15:30:49 -0800596 } else {
597 err = INVALID_OPERATION;
598 }
599 sp<AMessage> response = new AMessage;
600 if (err == OK) {
601 writeToAMessage(response, buffering);
602 }
603 response->setInt32("err", err);
604 response->postReply(replyID);
605 break;
606 }
607
608 case kWhatSetBufferingSettings:
609 {
610 sp<AReplyToken> replyID;
611 CHECK(msg->senderAwaitsResponse(&replyID));
612
613 ALOGV("kWhatSetBufferingSettings");
614 BufferingSettings buffering;
615 readFromAMessage(msg, &buffering);
616 status_t err = OK;
617 if (mSource != NULL) {
618 err = mSource->setBufferingSettings(buffering);
619 } else {
620 err = INVALID_OPERATION;
621 }
622 sp<AMessage> response = new AMessage;
623 response->setInt32("err", err);
624 response->postReply(replyID);
625 break;
626 }
627
Andreas Huber9575c962013-02-05 13:59:56 -0800628 case kWhatPrepare:
629 {
Hassan Shojaniacefac142017-02-06 21:02:02 -0800630 ALOGV("onMessageReceived kWhatPrepare");
631
Andreas Huber9575c962013-02-05 13:59:56 -0800632 mSource->prepareAsync();
Andreas Huberf9334412010-12-15 15:17:42 -0800633 break;
634 }
635
Chong Zhangdcb89b32013-08-06 09:44:47 -0700636 case kWhatGetTrackInfo:
637 {
Lajos Molnar3f274362015-03-05 14:35:41 -0800638 sp<AReplyToken> replyID;
Chong Zhangdcb89b32013-08-06 09:44:47 -0700639 CHECK(msg->senderAwaitsResponse(&replyID));
640
Chong Zhang404fced2014-06-11 14:45:31 -0700641 Parcel* reply;
642 CHECK(msg->findPointer("reply", (void**)&reply));
643
644 size_t inbandTracks = 0;
Chong Zhangdcb89b32013-08-06 09:44:47 -0700645 if (mSource != NULL) {
Chong Zhang404fced2014-06-11 14:45:31 -0700646 inbandTracks = mSource->getTrackCount();
647 }
648
Chong Zhanga7fa1d92014-06-11 14:49:23 -0700649 size_t ccTracks = 0;
650 if (mCCDecoder != NULL) {
651 ccTracks = mCCDecoder->getTrackCount();
652 }
653
Chong Zhang404fced2014-06-11 14:45:31 -0700654 // total track count
Chong Zhanga7fa1d92014-06-11 14:49:23 -0700655 reply->writeInt32(inbandTracks + ccTracks);
Chong Zhang404fced2014-06-11 14:45:31 -0700656
657 // write inband tracks
658 for (size_t i = 0; i < inbandTracks; ++i) {
659 writeTrackInfo(reply, mSource->getTrackInfo(i));
Chong Zhangdcb89b32013-08-06 09:44:47 -0700660 }
661
Chong Zhanga7fa1d92014-06-11 14:49:23 -0700662 // write CC track
663 for (size_t i = 0; i < ccTracks; ++i) {
664 writeTrackInfo(reply, mCCDecoder->getTrackInfo(i));
665 }
666
Chong Zhangdcb89b32013-08-06 09:44:47 -0700667 sp<AMessage> response = new AMessage;
Chong Zhangdcb89b32013-08-06 09:44:47 -0700668 response->postReply(replyID);
669 break;
670 }
671
Robert Shih7c4f0d72014-07-09 18:53:31 -0700672 case kWhatGetSelectedTrack:
673 {
Wei Jia039224f2018-11-29 17:25:17 -0800674 int32_t type32;
675 CHECK(msg->findInt32("type", (int32_t*)&type32));
676 media_track_type type = (media_track_type)type32;
677
678 size_t inbandTracks = 0;
Robert Shih7c4f0d72014-07-09 18:53:31 -0700679 status_t err = INVALID_OPERATION;
Wei Jia039224f2018-11-29 17:25:17 -0800680 ssize_t selectedTrack = -1;
Robert Shih7c4f0d72014-07-09 18:53:31 -0700681 if (mSource != NULL) {
682 err = OK;
Wei Jia039224f2018-11-29 17:25:17 -0800683 inbandTracks = mSource->getTrackCount();
684 selectedTrack = mSource->getSelectedTrack(type);
Robert Shih7c4f0d72014-07-09 18:53:31 -0700685 }
686
Wei Jia039224f2018-11-29 17:25:17 -0800687 if (selectedTrack == -1 && mCCDecoder != NULL) {
688 err = OK;
689 selectedTrack = mCCDecoder->getSelectedTrack(type);
690 if (selectedTrack != -1) {
691 selectedTrack += inbandTracks;
692 }
693 }
694
695 Parcel* reply;
696 CHECK(msg->findPointer("reply", (void**)&reply));
697 reply->writeInt32(selectedTrack);
698
Robert Shih7c4f0d72014-07-09 18:53:31 -0700699 sp<AMessage> response = new AMessage;
700 response->setInt32("err", err);
701
Lajos Molnar3f274362015-03-05 14:35:41 -0800702 sp<AReplyToken> replyID;
Robert Shih7c4f0d72014-07-09 18:53:31 -0700703 CHECK(msg->senderAwaitsResponse(&replyID));
704 response->postReply(replyID);
705 break;
706 }
707
Chong Zhangdcb89b32013-08-06 09:44:47 -0700708 case kWhatSelectTrack:
709 {
Lajos Molnar3f274362015-03-05 14:35:41 -0800710 sp<AReplyToken> replyID;
Chong Zhangdcb89b32013-08-06 09:44:47 -0700711 CHECK(msg->senderAwaitsResponse(&replyID));
712
Chong Zhang404fced2014-06-11 14:45:31 -0700713 size_t trackIndex;
714 int32_t select;
Robert Shih6ffb1fd2014-10-29 16:24:32 -0700715 int64_t timeUs;
Chong Zhang404fced2014-06-11 14:45:31 -0700716 CHECK(msg->findSize("trackIndex", &trackIndex));
717 CHECK(msg->findInt32("select", &select));
Robert Shih6ffb1fd2014-10-29 16:24:32 -0700718 CHECK(msg->findInt64("timeUs", &timeUs));
Chong Zhang404fced2014-06-11 14:45:31 -0700719
Chong Zhangdcb89b32013-08-06 09:44:47 -0700720 status_t err = INVALID_OPERATION;
Chong Zhang404fced2014-06-11 14:45:31 -0700721
722 size_t inbandTracks = 0;
Chong Zhangdcb89b32013-08-06 09:44:47 -0700723 if (mSource != NULL) {
Chong Zhang404fced2014-06-11 14:45:31 -0700724 inbandTracks = mSource->getTrackCount();
725 }
Chong Zhanga7fa1d92014-06-11 14:49:23 -0700726 size_t ccTracks = 0;
727 if (mCCDecoder != NULL) {
728 ccTracks = mCCDecoder->getTrackCount();
729 }
Chong Zhang404fced2014-06-11 14:45:31 -0700730
731 if (trackIndex < inbandTracks) {
Robert Shih6ffb1fd2014-10-29 16:24:32 -0700732 err = mSource->selectTrack(trackIndex, select, timeUs);
Robert Shihd3b0bbb2014-07-23 15:00:25 -0700733
734 if (!select && err == OK) {
735 int32_t type;
736 sp<AMessage> info = mSource->getTrackInfo(trackIndex);
737 if (info != NULL
738 && info->findInt32("type", &type)
739 && type == MEDIA_TRACK_TYPE_TIMEDTEXT) {
740 ++mTimedTextGeneration;
741 }
742 }
Chong Zhanga7fa1d92014-06-11 14:49:23 -0700743 } else {
744 trackIndex -= inbandTracks;
745
746 if (trackIndex < ccTracks) {
747 err = mCCDecoder->selectTrack(trackIndex, select);
748 }
Chong Zhangdcb89b32013-08-06 09:44:47 -0700749 }
750
751 sp<AMessage> response = new AMessage;
752 response->setInt32("err", err);
753
754 response->postReply(replyID);
755 break;
756 }
757
Andreas Huberb7c8e912012-11-27 15:02:53 -0800758 case kWhatPollDuration:
759 {
760 int32_t generation;
761 CHECK(msg->findInt32("generation", &generation));
762
763 if (generation != mPollDurationGeneration) {
764 // stale
765 break;
766 }
767
768 int64_t durationUs;
769 if (mDriver != NULL && mSource->getDuration(&durationUs) == OK) {
770 sp<NuPlayerDriver> driver = mDriver.promote();
771 if (driver != NULL) {
772 driver->notifyDuration(durationUs);
773 }
774 }
775
Chih-Hung Hsieh62309d52018-12-11 13:54:02 -0800776 msg->post(1000000LL); // poll again in a second.
Andreas Huberb7c8e912012-11-27 15:02:53 -0800777 break;
778 }
779
Lajos Molnar1de1e252015-04-30 18:18:34 -0700780 case kWhatSetVideoSurface:
Andreas Huberf9334412010-12-15 15:17:42 -0800781 {
Andreas Huberf9334412010-12-15 15:17:42 -0800782
783 sp<RefBase> obj;
Lajos Molnar1de1e252015-04-30 18:18:34 -0700784 CHECK(msg->findObject("surface", &obj));
785 sp<Surface> surface = static_cast<Surface *>(obj.get());
Lajos Molnara81c6222015-07-10 19:17:45 -0700786
787 ALOGD("onSetVideoSurface(%p, %s video decoder)",
788 surface.get(),
Wei Jia6b7d2ed2015-08-10 15:11:54 -0700789 (mSource != NULL && mStarted && mSource->getFormat(false /* audio */) != NULL
Lajos Molnara81c6222015-07-10 19:17:45 -0700790 && mVideoDecoder != NULL) ? "have" : "no");
791
Wei Jia6b7d2ed2015-08-10 15:11:54 -0700792 // Need to check mStarted before calling mSource->getFormat because NuPlayer might
793 // be in preparing state and it could take long time.
794 // When mStarted is true, mSource must have been set.
795 if (mSource == NULL || !mStarted || mSource->getFormat(false /* audio */) == NULL
Lajos Molnara81c6222015-07-10 19:17:45 -0700796 // NOTE: mVideoDecoder's mSurface is always non-null
797 || (mVideoDecoder != NULL && mVideoDecoder->setVideoSurface(surface) == OK)) {
Lajos Molnar1de1e252015-04-30 18:18:34 -0700798 performSetSurface(surface);
Wei Jiafef808d2014-10-31 17:57:05 -0700799 break;
800 }
801
802 mDeferredActions.push_back(
Krystian Turczyn687a5892016-01-20 14:20:35 +0100803 new FlushDecoderAction(
804 (obj != NULL ? FLUSH_CMD_FLUSH : FLUSH_CMD_NONE) /* audio */,
Wei Jiafef808d2014-10-31 17:57:05 -0700805 FLUSH_CMD_SHUTDOWN /* video */));
806
Lajos Molnar1de1e252015-04-30 18:18:34 -0700807 mDeferredActions.push_back(new SetSurfaceAction(surface));
James Dong0d268a32012-08-31 12:18:27 -0700808
Krystian Turczyn687a5892016-01-20 14:20:35 +0100809 if (obj != NULL) {
Wei Jiafef808d2014-10-31 17:57:05 -0700810 if (mStarted) {
Andy Hung73535852014-09-05 11:42:58 -0700811 // Issue a seek to refresh the video screen only if started otherwise
812 // the extractor may not yet be started and will assert.
813 // If the video decoder is not set (perhaps audio only in this case)
814 // do not perform a seek as it is not needed.
Ronghua Wua73d9e02014-10-08 15:13:29 -0700815 int64_t currentPositionUs = 0;
816 if (getCurrentPosition(&currentPositionUs) == OK) {
817 mDeferredActions.push_back(
Wei Jiac5de0912016-11-18 10:22:14 -0800818 new SeekAction(currentPositionUs,
819 MediaPlayerSeekMode::SEEK_PREVIOUS_SYNC /* mode */));
Ronghua Wua73d9e02014-10-08 15:13:29 -0700820 }
Andy Hung73535852014-09-05 11:42:58 -0700821 }
Wei Jiaac428aa2014-09-02 19:01:34 -0700822
Andreas Huber57a339c2012-12-03 11:18:00 -0800823 // If there is a new surface texture, instantiate decoders
824 // again if possible.
825 mDeferredActions.push_back(
826 new SimpleAction(&NuPlayer::performScanSources));
Andreas Huber57a339c2012-12-03 11:18:00 -0800827
Krystian Turczyn687a5892016-01-20 14:20:35 +0100828 // After a flush without shutdown, decoder is paused.
829 // Don't resume it until source seek is done, otherwise it could
830 // start pulling stale data too soon.
831 mDeferredActions.push_back(
832 new ResumeDecoderAction(false /* needNotify */));
833 }
Chong Zhang7137ec72014-11-12 16:41:05 -0800834
Andreas Huber57a339c2012-12-03 11:18:00 -0800835 processDeferredActions();
Andreas Huberf9334412010-12-15 15:17:42 -0800836 break;
837 }
838
839 case kWhatSetAudioSink:
840 {
Steve Block3856b092011-10-20 11:56:00 +0100841 ALOGV("kWhatSetAudioSink");
Andreas Huberf9334412010-12-15 15:17:42 -0800842
843 sp<RefBase> obj;
844 CHECK(msg->findObject("sink", &obj));
845
846 mAudioSink = static_cast<MediaPlayerBase::AudioSink *>(obj.get());
847 break;
848 }
849
850 case kWhatStart:
851 {
Steve Block3856b092011-10-20 11:56:00 +0100852 ALOGV("kWhatStart");
Wei Jia94211742014-10-28 17:09:06 -0700853 if (mStarted) {
Chong Zhang8a048332015-05-06 15:16:28 -0700854 // do not resume yet if the source is still buffering
855 if (!mPausedForBuffering) {
856 onResume();
857 }
Wei Jia94211742014-10-28 17:09:06 -0700858 } else {
859 onStart();
Lajos Molnar09524832014-07-17 14:29:51 -0700860 }
Chong Zhangefbb6192015-01-30 17:13:27 -0800861 mPausedByClient = false;
Andreas Huberf9334412010-12-15 15:17:42 -0800862 break;
863 }
864
Lajos Molnar3a474aa2015-04-24 17:10:07 -0700865 case kWhatConfigPlayback:
Wei Jia98160162015-02-04 17:01:11 -0800866 {
Lajos Molnar3a474aa2015-04-24 17:10:07 -0700867 sp<AReplyToken> replyID;
868 CHECK(msg->senderAwaitsResponse(&replyID));
869 AudioPlaybackRate rate /* sanitized */;
870 readFromAMessage(msg, &rate);
871 status_t err = OK;
Wei Jia98160162015-02-04 17:01:11 -0800872 if (mRenderer != NULL) {
Wei Jiabf70feb2016-02-19 15:47:16 -0800873 // AudioSink allows only 1.f and 0.f for offload mode.
874 // For other speed, switch to non-offload mode.
875 if (mOffloadAudio && ((rate.mSpeed != 0.f && rate.mSpeed != 1.f)
876 || rate.mPitch != 1.f)) {
877 int64_t currentPositionUs;
878 if (getCurrentPosition(&currentPositionUs) != OK) {
879 currentPositionUs = mPreviousSeekTimeUs;
880 }
881
882 // Set mPlaybackSettings so that the new audio decoder can
883 // be created correctly.
884 mPlaybackSettings = rate;
885 if (!mPaused) {
886 mRenderer->pause();
887 }
Wei Jia5031b2f2016-02-25 11:19:31 -0800888 restartAudio(
Wei Jiabf70feb2016-02-19 15:47:16 -0800889 currentPositionUs, true /* forceNonOffload */,
890 true /* needsToCreateAudioDecoder */);
891 if (!mPaused) {
892 mRenderer->resume();
893 }
894 }
895
Lajos Molnar3a474aa2015-04-24 17:10:07 -0700896 err = mRenderer->setPlaybackSettings(rate);
897 }
898 if (err == OK) {
899 if (rate.mSpeed == 0.f) {
900 onPause();
Wei Jia351ce872016-02-10 13:21:59 -0800901 mPausedByClient = true;
Lajos Molnar3a474aa2015-04-24 17:10:07 -0700902 // save all other settings (using non-paused speed)
903 // so we can restore them on start
904 AudioPlaybackRate newRate = rate;
905 newRate.mSpeed = mPlaybackSettings.mSpeed;
906 mPlaybackSettings = newRate;
907 } else { /* rate.mSpeed != 0.f */
Lajos Molnar3a474aa2015-04-24 17:10:07 -0700908 mPlaybackSettings = rate;
Wei Jia8a092d32016-06-03 14:57:24 -0700909 if (mStarted) {
910 // do not resume yet if the source is still buffering
911 if (!mPausedForBuffering) {
912 onResume();
913 }
914 } else if (mPrepared) {
915 onStart();
916 }
917
918 mPausedByClient = false;
Lajos Molnar3a474aa2015-04-24 17:10:07 -0700919 }
Wei Jia98160162015-02-04 17:01:11 -0800920 }
Ronghua Wu8db88132015-04-22 13:51:35 -0700921
922 if (mVideoDecoder != NULL) {
Praveen Chavanbbaa1442016-04-08 13:33:49 -0700923 sp<AMessage> params = new AMessage();
924 params->setFloat("playback-speed", mPlaybackSettings.mSpeed);
925 mVideoDecoder->setParameters(params);
Ronghua Wu8db88132015-04-22 13:51:35 -0700926 }
927
Lajos Molnar3a474aa2015-04-24 17:10:07 -0700928 sp<AMessage> response = new AMessage;
929 response->setInt32("err", err);
930 response->postReply(replyID);
931 break;
932 }
933
934 case kWhatGetPlaybackSettings:
935 {
936 sp<AReplyToken> replyID;
937 CHECK(msg->senderAwaitsResponse(&replyID));
938 AudioPlaybackRate rate = mPlaybackSettings;
939 status_t err = OK;
940 if (mRenderer != NULL) {
941 err = mRenderer->getPlaybackSettings(&rate);
942 }
943 if (err == OK) {
944 // get playback settings used by renderer, as it may be
945 // slightly off due to audiosink not taking small changes.
946 mPlaybackSettings = rate;
947 if (mPaused) {
948 rate.mSpeed = 0.f;
949 }
950 }
951 sp<AMessage> response = new AMessage;
952 if (err == OK) {
953 writeToAMessage(response, rate);
954 }
955 response->setInt32("err", err);
956 response->postReply(replyID);
957 break;
958 }
959
960 case kWhatConfigSync:
961 {
962 sp<AReplyToken> replyID;
963 CHECK(msg->senderAwaitsResponse(&replyID));
964
965 ALOGV("kWhatConfigSync");
966 AVSyncSettings sync;
967 float videoFpsHint;
968 readFromAMessage(msg, &sync, &videoFpsHint);
969 status_t err = OK;
970 if (mRenderer != NULL) {
971 err = mRenderer->setSyncSettings(sync, videoFpsHint);
972 }
973 if (err == OK) {
974 mSyncSettings = sync;
975 mVideoFpsHint = videoFpsHint;
976 }
977 sp<AMessage> response = new AMessage;
978 response->setInt32("err", err);
979 response->postReply(replyID);
980 break;
981 }
982
983 case kWhatGetSyncSettings:
984 {
985 sp<AReplyToken> replyID;
986 CHECK(msg->senderAwaitsResponse(&replyID));
987 AVSyncSettings sync = mSyncSettings;
988 float videoFps = mVideoFpsHint;
989 status_t err = OK;
990 if (mRenderer != NULL) {
991 err = mRenderer->getSyncSettings(&sync, &videoFps);
992 if (err == OK) {
993 mSyncSettings = sync;
994 mVideoFpsHint = videoFps;
995 }
996 }
997 sp<AMessage> response = new AMessage;
998 if (err == OK) {
999 writeToAMessage(response, sync, videoFps);
1000 }
1001 response->setInt32("err", err);
1002 response->postReply(replyID);
Wei Jia98160162015-02-04 17:01:11 -08001003 break;
1004 }
1005
Andreas Huberf9334412010-12-15 15:17:42 -08001006 case kWhatScanSources:
1007 {
Andreas Huber1aef2112011-01-04 14:01:29 -08001008 int32_t generation;
1009 CHECK(msg->findInt32("generation", &generation));
1010 if (generation != mScanSourcesGeneration) {
1011 // Drop obsolete msg.
1012 break;
1013 }
1014
Andreas Huber5bc087c2010-12-23 10:27:40 -08001015 mScanSourcesPending = false;
1016
Steve Block3856b092011-10-20 11:56:00 +01001017 ALOGV("scanning sources haveAudio=%d, haveVideo=%d",
Andreas Huber43c3e6c2011-01-05 12:17:08 -08001018 mAudioDecoder != NULL, mVideoDecoder != NULL);
1019
Andreas Huberb7c8e912012-11-27 15:02:53 -08001020 bool mHadAnySourcesBefore =
1021 (mAudioDecoder != NULL) || (mVideoDecoder != NULL);
Robert Shih7350b052015-10-01 15:50:14 -07001022 bool rescan = false;
Andreas Huberb7c8e912012-11-27 15:02:53 -08001023
Andy Hung282a7e32014-08-14 15:56:34 -07001024 // initialize video before audio because successful initialization of
1025 // video may change deep buffer mode of audio.
Lajos Molnar1de1e252015-04-30 18:18:34 -07001026 if (mSurface != NULL) {
Robert Shih7350b052015-10-01 15:50:14 -07001027 if (instantiateDecoder(false, &mVideoDecoder) == -EWOULDBLOCK) {
1028 rescan = true;
1029 }
Haynes Mathew George5d246ef2012-07-09 10:36:57 -07001030 }
Andreas Huberf9334412010-12-15 15:17:42 -08001031
Ronghua Wua10fd232014-11-06 16:15:20 -08001032 // Don't try to re-open audio sink if there's an existing decoder.
1033 if (mAudioSink != NULL && mAudioDecoder == NULL) {
Robert Shih7350b052015-10-01 15:50:14 -07001034 if (instantiateDecoder(true, &mAudioDecoder) == -EWOULDBLOCK) {
1035 rescan = true;
1036 }
Andreas Huberf9334412010-12-15 15:17:42 -08001037 }
1038
Andreas Huberb7c8e912012-11-27 15:02:53 -08001039 if (!mHadAnySourcesBefore
1040 && (mAudioDecoder != NULL || mVideoDecoder != NULL)) {
1041 // This is the first time we've found anything playable.
1042
Andreas Huber9575c962013-02-05 13:59:56 -08001043 if (mSourceFlags & Source::FLAG_DYNAMIC_DURATION) {
Andreas Huberb7c8e912012-11-27 15:02:53 -08001044 schedulePollDuration();
1045 }
1046 }
1047
Andreas Hubereac68ba2011-09-27 12:12:25 -07001048 status_t err;
1049 if ((err = mSource->feedMoreTSData()) != OK) {
Andreas Huber1aef2112011-01-04 14:01:29 -08001050 if (mAudioDecoder == NULL && mVideoDecoder == NULL) {
1051 // We're not currently decoding anything (no audio or
1052 // video tracks found) and we just ran out of input data.
Andreas Hubereac68ba2011-09-27 12:12:25 -07001053
1054 if (err == ERROR_END_OF_STREAM) {
1055 notifyListener(MEDIA_PLAYBACK_COMPLETE, 0, 0);
1056 } else {
1057 notifyListener(MEDIA_ERROR, MEDIA_ERROR_UNKNOWN, err);
1058 }
Andreas Huber1aef2112011-01-04 14:01:29 -08001059 }
Andreas Huberf9334412010-12-15 15:17:42 -08001060 break;
1061 }
1062
Robert Shih7350b052015-10-01 15:50:14 -07001063 if (rescan) {
Chih-Hung Hsieh62309d52018-12-11 13:54:02 -08001064 msg->post(100000LL);
Andreas Huber5bc087c2010-12-23 10:27:40 -08001065 mScanSourcesPending = true;
Andreas Huberf9334412010-12-15 15:17:42 -08001066 }
1067 break;
1068 }
1069
1070 case kWhatVideoNotify:
1071 case kWhatAudioNotify:
1072 {
1073 bool audio = msg->what() == kWhatAudioNotify;
1074
Wei Jia88703c32014-08-06 11:24:07 -07001075 int32_t currentDecoderGeneration =
1076 (audio? mAudioDecoderGeneration : mVideoDecoderGeneration);
1077 int32_t requesterGeneration = currentDecoderGeneration - 1;
1078 CHECK(msg->findInt32("generation", &requesterGeneration));
1079
1080 if (requesterGeneration != currentDecoderGeneration) {
1081 ALOGV("got message from old %s decoder, generation(%d:%d)",
1082 audio ? "audio" : "video", requesterGeneration,
1083 currentDecoderGeneration);
1084 sp<AMessage> reply;
1085 if (!(msg->findMessage("reply", &reply))) {
1086 return;
1087 }
1088
1089 reply->setInt32("err", INFO_DISCONTINUITY);
1090 reply->post();
1091 return;
1092 }
1093
Andreas Huberf9334412010-12-15 15:17:42 -08001094 int32_t what;
Lajos Molnar1cd13982014-01-17 15:12:51 -08001095 CHECK(msg->findInt32("what", &what));
Andreas Huberf9334412010-12-15 15:17:42 -08001096
Chong Zhang7137ec72014-11-12 16:41:05 -08001097 if (what == DecoderBase::kWhatInputDiscontinuity) {
1098 int32_t formatChange;
1099 CHECK(msg->findInt32("formatChange", &formatChange));
Andreas Huberf9334412010-12-15 15:17:42 -08001100
Chong Zhang7137ec72014-11-12 16:41:05 -08001101 ALOGV("%s discontinuity: formatChange %d",
1102 audio ? "audio" : "video", formatChange);
1103
1104 if (formatChange) {
1105 mDeferredActions.push_back(
1106 new FlushDecoderAction(
1107 audio ? FLUSH_CMD_SHUTDOWN : FLUSH_CMD_NONE,
1108 audio ? FLUSH_CMD_NONE : FLUSH_CMD_SHUTDOWN));
Andreas Huberf9334412010-12-15 15:17:42 -08001109 }
Chong Zhang7137ec72014-11-12 16:41:05 -08001110
1111 mDeferredActions.push_back(
1112 new SimpleAction(
1113 &NuPlayer::performScanSources));
1114
1115 processDeferredActions();
1116 } else if (what == DecoderBase::kWhatEOS) {
Andreas Huberdc9bacd2011-09-26 10:53:29 -07001117 int32_t err;
Lajos Molnar1cd13982014-01-17 15:12:51 -08001118 CHECK(msg->findInt32("err", &err));
Andreas Huberdc9bacd2011-09-26 10:53:29 -07001119
1120 if (err == ERROR_END_OF_STREAM) {
Steve Block3856b092011-10-20 11:56:00 +01001121 ALOGV("got %s decoder EOS", audio ? "audio" : "video");
Andreas Huberdc9bacd2011-09-26 10:53:29 -07001122 } else {
Steve Block3856b092011-10-20 11:56:00 +01001123 ALOGV("got %s decoder EOS w/ error %d",
Andreas Huberdc9bacd2011-09-26 10:53:29 -07001124 audio ? "audio" : "video",
1125 err);
1126 }
1127
1128 mRenderer->queueEOS(audio, err);
Chong Zhang7137ec72014-11-12 16:41:05 -08001129 } else if (what == DecoderBase::kWhatFlushCompleted) {
Steve Block3856b092011-10-20 11:56:00 +01001130 ALOGV("decoder %s flush completed", audio ? "audio" : "video");
Andreas Huberf9334412010-12-15 15:17:42 -08001131
Andy Hung8d121d42014-10-03 09:53:53 -07001132 handleFlushComplete(audio, true /* isDecoder */);
Andreas Huber3831a062010-12-21 10:22:33 -08001133 finishFlushIfPossible();
Chong Zhang7137ec72014-11-12 16:41:05 -08001134 } else if (what == DecoderBase::kWhatVideoSizeChanged) {
Lajos Molnar1cd13982014-01-17 15:12:51 -08001135 sp<AMessage> format;
1136 CHECK(msg->findMessage("format", &format));
1137
Wei Jiac6cfd702014-11-11 16:33:20 -08001138 sp<AMessage> inputFormat =
1139 mSource->getFormat(false /* audio */);
Andreas Huber3831a062010-12-21 10:22:33 -08001140
Bartosz Dolewski26936f72014-12-11 12:47:08 +01001141 setVideoScalingMode(mVideoScalingMode);
Wei Jiac6cfd702014-11-11 16:33:20 -08001142 updateVideoSize(inputFormat, format);
Chong Zhang7137ec72014-11-12 16:41:05 -08001143 } else if (what == DecoderBase::kWhatShutdownCompleted) {
Steve Block3856b092011-10-20 11:56:00 +01001144 ALOGV("%s shutdown completed", audio ? "audio" : "video");
Andreas Huber3831a062010-12-21 10:22:33 -08001145 if (audio) {
Ray Essickfcb8fd32018-08-07 09:39:38 -07001146 Mutex::Autolock autoLock(mDecoderLock);
Andreas Huber3831a062010-12-21 10:22:33 -08001147 mAudioDecoder.clear();
Wei Jia686e8e52017-04-03 14:08:01 -07001148 mAudioDecoderError = false;
Wei Jia57568df2014-09-22 10:16:29 -07001149 ++mAudioDecoderGeneration;
Andreas Huber3831a062010-12-21 10:22:33 -08001150
1151 CHECK_EQ((int)mFlushingAudio, (int)SHUTTING_DOWN_DECODER);
1152 mFlushingAudio = SHUT_DOWN;
1153 } else {
Ray Essickfcb8fd32018-08-07 09:39:38 -07001154 Mutex::Autolock autoLock(mDecoderLock);
Andreas Huber3831a062010-12-21 10:22:33 -08001155 mVideoDecoder.clear();
Wei Jia686e8e52017-04-03 14:08:01 -07001156 mVideoDecoderError = false;
Wei Jia57568df2014-09-22 10:16:29 -07001157 ++mVideoDecoderGeneration;
Andreas Huber3831a062010-12-21 10:22:33 -08001158
1159 CHECK_EQ((int)mFlushingVideo, (int)SHUTTING_DOWN_DECODER);
1160 mFlushingVideo = SHUT_DOWN;
1161 }
1162
1163 finishFlushIfPossible();
Chong Zhangf8d71772014-11-26 15:08:34 -08001164 } else if (what == DecoderBase::kWhatResumeCompleted) {
1165 finishResume();
Chong Zhang7137ec72014-11-12 16:41:05 -08001166 } else if (what == DecoderBase::kWhatError) {
Chong Zhangf4c0a942014-08-11 15:14:10 -07001167 status_t err;
Andy Hung2abde2c2014-09-30 14:40:32 -07001168 if (!msg->findInt32("err", &err) || err == OK) {
Chong Zhangf4c0a942014-08-11 15:14:10 -07001169 err = UNKNOWN_ERROR;
1170 }
Andy Hungcf31f1e2014-09-23 14:59:01 -07001171
Andy Hung2abde2c2014-09-30 14:40:32 -07001172 // Decoder errors can be due to Source (e.g. from streaming),
1173 // or from decoding corrupted bitstreams, or from other decoder
1174 // MediaCodec operations (e.g. from an ongoing reset or seek).
Andy Hung202bce12014-12-03 11:47:36 -08001175 // They may also be due to openAudioSink failure at
1176 // decoder start or after a format change.
Andy Hung2abde2c2014-09-30 14:40:32 -07001177 //
1178 // We try to gracefully shut down the affected decoder if possible,
1179 // rather than trying to force the shutdown with something
1180 // similar to performReset(). This method can lead to a hang
1181 // if MediaCodec functions block after an error, but they should
1182 // typically return INVALID_OPERATION instead of blocking.
1183
1184 FlushStatus *flushing = audio ? &mFlushingAudio : &mFlushingVideo;
1185 ALOGE("received error(%#x) from %s decoder, flushing(%d), now shutting down",
1186 err, audio ? "audio" : "video", *flushing);
1187
1188 switch (*flushing) {
1189 case NONE:
1190 mDeferredActions.push_back(
Wei Jiafef808d2014-10-31 17:57:05 -07001191 new FlushDecoderAction(
1192 audio ? FLUSH_CMD_SHUTDOWN : FLUSH_CMD_NONE,
1193 audio ? FLUSH_CMD_NONE : FLUSH_CMD_SHUTDOWN));
Andy Hung2abde2c2014-09-30 14:40:32 -07001194 processDeferredActions();
1195 break;
1196 case FLUSHING_DECODER:
1197 *flushing = FLUSHING_DECODER_SHUTDOWN; // initiate shutdown after flush.
1198 break; // Wait for flush to complete.
1199 case FLUSHING_DECODER_SHUTDOWN:
1200 break; // Wait for flush to complete.
1201 case SHUTTING_DOWN_DECODER:
1202 break; // Wait for shutdown to complete.
1203 case FLUSHED:
Andy Hung2abde2c2014-09-30 14:40:32 -07001204 getDecoder(audio)->initiateShutdown(); // In the middle of a seek.
1205 *flushing = SHUTTING_DOWN_DECODER; // Shut down.
1206 break;
1207 case SHUT_DOWN:
1208 finishFlushIfPossible(); // Should not occur.
1209 break; // Finish anyways.
Marco Nelissen9e2b7912014-08-18 16:13:03 -07001210 }
Wei Jia686e8e52017-04-03 14:08:01 -07001211 if (mSource != nullptr) {
1212 if (audio) {
Wei Jia2f6d8c52017-04-11 09:50:31 -07001213 if (mVideoDecoderError || mSource->getFormat(false /* audio */) == NULL
Wei Jiaf86731d2017-07-11 17:24:34 -07001214 || mSurface == NULL || mVideoDecoder == NULL) {
Wei Jia686e8e52017-04-03 14:08:01 -07001215 // When both audio and video have error, or this stream has only audio
1216 // which has error, notify client of error.
1217 notifyListener(MEDIA_ERROR, MEDIA_ERROR_UNKNOWN, err);
1218 } else {
1219 // Only audio track has error. Video track could be still good to play.
1220 notifyListener(MEDIA_INFO, MEDIA_INFO_PLAY_AUDIO_ERROR, err);
1221 }
1222 mAudioDecoderError = true;
1223 } else {
Wei Jia2f6d8c52017-04-11 09:50:31 -07001224 if (mAudioDecoderError || mSource->getFormat(true /* audio */) == NULL
Wei Jiaf86731d2017-07-11 17:24:34 -07001225 || mAudioSink == NULL || mAudioDecoder == NULL) {
Wei Jia686e8e52017-04-03 14:08:01 -07001226 // When both audio and video have error, or this stream has only video
1227 // which has error, notify client of error.
1228 notifyListener(MEDIA_ERROR, MEDIA_ERROR_UNKNOWN, err);
1229 } else {
1230 // Only video track has error. Audio track could be still good to play.
1231 notifyListener(MEDIA_INFO, MEDIA_INFO_PLAY_VIDEO_ERROR, err);
1232 }
1233 mVideoDecoderError = true;
1234 }
1235 }
Lajos Molnar1cd13982014-01-17 15:12:51 -08001236 } else {
1237 ALOGV("Unhandled decoder notification %d '%c%c%c%c'.",
Andreas Hubera1f8ab02012-11-30 10:53:22 -08001238 what,
1239 what >> 24,
1240 (what >> 16) & 0xff,
1241 (what >> 8) & 0xff,
1242 what & 0xff);
Andreas Huberf9334412010-12-15 15:17:42 -08001243 }
1244
1245 break;
1246 }
1247
1248 case kWhatRendererNotify:
1249 {
Wei Jia57568df2014-09-22 10:16:29 -07001250 int32_t requesterGeneration = mRendererGeneration - 1;
1251 CHECK(msg->findInt32("generation", &requesterGeneration));
1252 if (requesterGeneration != mRendererGeneration) {
1253 ALOGV("got message from old renderer, generation(%d:%d)",
1254 requesterGeneration, mRendererGeneration);
1255 return;
1256 }
1257
Andreas Huberf9334412010-12-15 15:17:42 -08001258 int32_t what;
1259 CHECK(msg->findInt32("what", &what));
1260
1261 if (what == Renderer::kWhatEOS) {
1262 int32_t audio;
1263 CHECK(msg->findInt32("audio", &audio));
1264
Andreas Huberc92fd242011-08-16 13:48:44 -07001265 int32_t finalResult;
1266 CHECK(msg->findInt32("finalResult", &finalResult));
1267
Andreas Huberf9334412010-12-15 15:17:42 -08001268 if (audio) {
1269 mAudioEOS = true;
1270 } else {
1271 mVideoEOS = true;
1272 }
1273
Andreas Huberc92fd242011-08-16 13:48:44 -07001274 if (finalResult == ERROR_END_OF_STREAM) {
Steve Block3856b092011-10-20 11:56:00 +01001275 ALOGV("reached %s EOS", audio ? "audio" : "video");
Andreas Huberc92fd242011-08-16 13:48:44 -07001276 } else {
Steve Block29357bc2012-01-06 19:20:56 +00001277 ALOGE("%s track encountered an error (%d)",
Andreas Huberc92fd242011-08-16 13:48:44 -07001278 audio ? "audio" : "video", finalResult);
1279
1280 notifyListener(
1281 MEDIA_ERROR, MEDIA_ERROR_UNKNOWN, finalResult);
1282 }
Andreas Huberf9334412010-12-15 15:17:42 -08001283
1284 if ((mAudioEOS || mAudioDecoder == NULL)
1285 && (mVideoEOS || mVideoDecoder == NULL)) {
1286 notifyListener(MEDIA_PLAYBACK_COMPLETE, 0, 0);
1287 }
Andreas Huber3fe62152011-09-16 15:09:22 -07001288 } else if (what == Renderer::kWhatFlushComplete) {
Andreas Huberf9334412010-12-15 15:17:42 -08001289 int32_t audio;
1290 CHECK(msg->findInt32("audio", &audio));
1291
Wei Jia3261f0d2015-10-08 13:58:33 -07001292 if (audio) {
1293 mAudioEOS = false;
1294 } else {
1295 mVideoEOS = false;
1296 }
1297
Steve Block3856b092011-10-20 11:56:00 +01001298 ALOGV("renderer %s flush completed.", audio ? "audio" : "video");
Wei Jiada4252f2015-07-14 18:15:28 -07001299 if (audio && (mFlushingAudio == NONE || mFlushingAudio == FLUSHED
1300 || mFlushingAudio == SHUT_DOWN)) {
1301 // Flush has been handled by tear down.
1302 break;
1303 }
Andy Hung8d121d42014-10-03 09:53:53 -07001304 handleFlushComplete(audio, false /* isDecoder */);
1305 finishFlushIfPossible();
James Dongf57b4ea2012-07-20 13:38:36 -07001306 } else if (what == Renderer::kWhatVideoRenderingStart) {
1307 notifyListener(MEDIA_INFO, MEDIA_INFO_RENDERING_START, 0);
Lajos Molnarcbaffcf2013-08-14 18:30:38 -07001308 } else if (what == Renderer::kWhatMediaRenderingStart) {
1309 ALOGV("media rendering started");
1310 notifyListener(MEDIA_STARTED, 0, 0);
Ronghua Wufaeb0f22015-05-21 12:20:21 -07001311 } else if (what == Renderer::kWhatAudioTearDown) {
Ronghua Wu08529172014-10-02 16:55:52 -07001312 int32_t reason;
1313 CHECK(msg->findInt32("reason", &reason));
Ronghua Wufaeb0f22015-05-21 12:20:21 -07001314 ALOGV("Tear down audio with reason %d.", reason);
Wei Jia8456ddd2016-04-22 14:46:43 -07001315 if (reason == Renderer::kDueToTimeout && !(mPaused && mOffloadAudio)) {
1316 // TimeoutWhenPaused is only for offload mode.
Wei Jia355b2bb2018-04-09 16:36:23 -07001317 ALOGW("Received a stale message for teardown, mPaused(%d), mOffloadAudio(%d)",
1318 mPaused, mOffloadAudio);
Wei Jia8456ddd2016-04-22 14:46:43 -07001319 break;
1320 }
Wei Jiada4252f2015-07-14 18:15:28 -07001321 int64_t positionUs;
Robert Shih1a5c8592015-08-04 18:07:44 -07001322 if (!msg->findInt64("positionUs", &positionUs)) {
1323 positionUs = mPreviousSeekTimeUs;
1324 }
Ronghua Wufaeb0f22015-05-21 12:20:21 -07001325
Wei Jia5031b2f2016-02-25 11:19:31 -08001326 restartAudio(
Wei Jiaa05f1e32016-03-25 16:31:22 -07001327 positionUs, reason == Renderer::kForceNonOffload /* forceNonOffload */,
1328 reason != Renderer::kDueToTimeout /* needsToCreateAudioDecoder */);
Andreas Huberf9334412010-12-15 15:17:42 -08001329 }
1330 break;
1331 }
1332
1333 case kWhatMoreDataQueued:
1334 {
1335 break;
1336 }
1337
Andreas Huber1aef2112011-01-04 14:01:29 -08001338 case kWhatReset:
1339 {
Steve Block3856b092011-10-20 11:56:00 +01001340 ALOGV("kWhatReset");
Andreas Huber1aef2112011-01-04 14:01:29 -08001341
Ronghua Wu64c2d172015-10-07 16:52:19 -07001342 mResetting = true;
Ray Essickeda32522018-02-28 12:08:28 -08001343 updatePlaybackTimer(true /* stopping */, "kWhatReset");
1344 updateRebufferingTimer(true /* stopping */, true /* exiting */);
Ronghua Wu64c2d172015-10-07 16:52:19 -07001345
Andreas Hubera1f8ab02012-11-30 10:53:22 -08001346 mDeferredActions.push_back(
Wei Jiafef808d2014-10-31 17:57:05 -07001347 new FlushDecoderAction(
1348 FLUSH_CMD_SHUTDOWN /* audio */,
1349 FLUSH_CMD_SHUTDOWN /* video */));
Andreas Huberb7c8e912012-11-27 15:02:53 -08001350
Andreas Hubera1f8ab02012-11-30 10:53:22 -08001351 mDeferredActions.push_back(
1352 new SimpleAction(&NuPlayer::performReset));
Andreas Huberb58ce9f2011-11-28 16:27:35 -08001353
Andreas Hubera1f8ab02012-11-30 10:53:22 -08001354 processDeferredActions();
Andreas Huber1aef2112011-01-04 14:01:29 -08001355 break;
1356 }
1357
Wei Jia52c28512017-09-13 18:17:51 -07001358 case kWhatNotifyTime:
1359 {
1360 ALOGV("kWhatNotifyTime");
1361 int64_t timerUs;
1362 CHECK(msg->findInt64("timerUs", &timerUs));
1363
1364 notifyListener(MEDIA_NOTIFY_TIME, timerUs, 0);
1365 break;
1366 }
1367
Andreas Huber43c3e6c2011-01-05 12:17:08 -08001368 case kWhatSeek:
1369 {
1370 int64_t seekTimeUs;
Wei Jiac5de0912016-11-18 10:22:14 -08001371 int32_t mode;
Wei Jiae427abf2014-09-22 15:21:11 -07001372 int32_t needNotify;
Andreas Huber43c3e6c2011-01-05 12:17:08 -08001373 CHECK(msg->findInt64("seekTimeUs", &seekTimeUs));
Wei Jiac5de0912016-11-18 10:22:14 -08001374 CHECK(msg->findInt32("mode", &mode));
Wei Jiae427abf2014-09-22 15:21:11 -07001375 CHECK(msg->findInt32("needNotify", &needNotify));
Andreas Huber43c3e6c2011-01-05 12:17:08 -08001376
Wei Jiac5de0912016-11-18 10:22:14 -08001377 ALOGV("kWhatSeek seekTimeUs=%lld us, mode=%d, needNotify=%d",
1378 (long long)seekTimeUs, mode, needNotify);
Andreas Huber43c3e6c2011-01-05 12:17:08 -08001379
Wei Jia1061c9c2015-05-19 16:02:17 -07001380 if (!mStarted) {
1381 // Seek before the player is started. In order to preview video,
1382 // need to start the player and pause it. This branch is called
1383 // only once if needed. After the player is started, any seek
1384 // operation will go through normal path.
Robert Shih0c61a0d2015-07-06 15:09:10 -07001385 // Audio-only cases are handled separately.
Wei Jiac5de0912016-11-18 10:22:14 -08001386 onStart(seekTimeUs, (MediaPlayerSeekMode)mode);
Robert Shih0c61a0d2015-07-06 15:09:10 -07001387 if (mStarted) {
1388 onPause();
1389 mPausedByClient = true;
1390 }
Wei Jia1061c9c2015-05-19 16:02:17 -07001391 if (needNotify) {
1392 notifyDriverSeekComplete();
1393 }
1394 break;
1395 }
1396
Andreas Hubera1f8ab02012-11-30 10:53:22 -08001397 mDeferredActions.push_back(
Wei Jiafef808d2014-10-31 17:57:05 -07001398 new FlushDecoderAction(FLUSH_CMD_FLUSH /* audio */,
1399 FLUSH_CMD_FLUSH /* video */));
Andreas Huber43c3e6c2011-01-05 12:17:08 -08001400
Wei Jiae427abf2014-09-22 15:21:11 -07001401 mDeferredActions.push_back(
Wei Jiac5de0912016-11-18 10:22:14 -08001402 new SeekAction(seekTimeUs, (MediaPlayerSeekMode)mode));
Andreas Huber43c3e6c2011-01-05 12:17:08 -08001403
Chong Zhangf8d71772014-11-26 15:08:34 -08001404 // After a flush without shutdown, decoder is paused.
1405 // Don't resume it until source seek is done, otherwise it could
Chong Zhang7137ec72014-11-12 16:41:05 -08001406 // start pulling stale data too soon.
1407 mDeferredActions.push_back(
Chong Zhangf8d71772014-11-26 15:08:34 -08001408 new ResumeDecoderAction(needNotify));
Chong Zhang7137ec72014-11-12 16:41:05 -08001409
Andreas Hubera1f8ab02012-11-30 10:53:22 -08001410 processDeferredActions();
Andreas Huber43c3e6c2011-01-05 12:17:08 -08001411 break;
1412 }
1413
Andreas Huberb4082222011-01-20 15:23:04 -08001414 case kWhatPause:
1415 {
Chong Zhangefbb6192015-01-30 17:13:27 -08001416 onPause();
1417 mPausedByClient = true;
Andreas Huberb4082222011-01-20 15:23:04 -08001418 break;
1419 }
1420
Andreas Huberb5f25f02013-02-05 10:14:26 -08001421 case kWhatSourceNotify:
1422 {
Andreas Huber9575c962013-02-05 13:59:56 -08001423 onSourceNotify(msg);
Andreas Huberb5f25f02013-02-05 10:14:26 -08001424 break;
1425 }
1426
Chong Zhanga7fa1d92014-06-11 14:49:23 -07001427 case kWhatClosedCaptionNotify:
1428 {
1429 onClosedCaptionNotify(msg);
1430 break;
1431 }
1432
Hassan Shojaniacefac142017-02-06 21:02:02 -08001433 case kWhatPrepareDrm:
1434 {
1435 status_t status = onPrepareDrm(msg);
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
1445 case kWhatReleaseDrm:
1446 {
1447 status_t status = onReleaseDrm();
1448
1449 sp<AMessage> response = new AMessage;
1450 response->setInt32("status", status);
1451 sp<AReplyToken> replyID;
1452 CHECK(msg->senderAwaitsResponse(&replyID));
1453 response->postReply(replyID);
1454 break;
1455 }
1456
Dongwon Kang47afe0a2018-03-27 15:15:30 -07001457 case kWhatMediaClockNotify:
1458 {
1459 ALOGV("kWhatMediaClockNotify");
1460 int64_t anchorMediaUs, anchorRealUs;
1461 float playbackRate;
1462 CHECK(msg->findInt64("anchor-media-us", &anchorMediaUs));
1463 CHECK(msg->findInt64("anchor-real-us", &anchorRealUs));
1464 CHECK(msg->findFloat("playback-rate", &playbackRate));
1465
1466 Parcel in;
1467 in.writeInt64(anchorMediaUs);
1468 in.writeInt64(anchorRealUs);
1469 in.writeFloat(playbackRate);
1470
1471 notifyListener(MEDIA_TIME_DISCONTINUITY, 0, 0, &in);
1472 break;
1473 }
1474
Andreas Huberf9334412010-12-15 15:17:42 -08001475 default:
1476 TRESPASS();
1477 break;
1478 }
1479}
1480
Wei Jia94211742014-10-28 17:09:06 -07001481void NuPlayer::onResume() {
Ronghua Wu64c2d172015-10-07 16:52:19 -07001482 if (!mPaused || mResetting) {
1483 ALOGD_IF(mResetting, "resetting, onResume discarded");
Chong Zhangefbb6192015-01-30 17:13:27 -08001484 return;
1485 }
1486 mPaused = false;
Wei Jia94211742014-10-28 17:09:06 -07001487 if (mSource != NULL) {
1488 mSource->resume();
1489 } else {
1490 ALOGW("resume called when source is gone or not set");
1491 }
1492 // |mAudioDecoder| may have been released due to the pause timeout, so re-create it if
1493 // needed.
1494 if (audioDecoderStillNeeded() && mAudioDecoder == NULL) {
1495 instantiateDecoder(true /* audio */, &mAudioDecoder);
1496 }
1497 if (mRenderer != NULL) {
1498 mRenderer->resume();
1499 } else {
1500 ALOGW("resume called when renderer is gone or not set");
1501 }
Ray Essickd4d00612017-01-03 09:36:27 -08001502
Ray Essick0d98c182017-11-09 13:42:42 -08001503 startPlaybackTimer("onresume");
Wei Jia94211742014-10-28 17:09:06 -07001504}
1505
Lajos Molnarfcd3e942015-03-31 10:06:48 -07001506status_t NuPlayer::onInstantiateSecureDecoders() {
1507 status_t err;
1508 if (!(mSourceFlags & Source::FLAG_SECURE)) {
1509 return BAD_TYPE;
1510 }
1511
1512 if (mRenderer != NULL) {
1513 ALOGE("renderer should not be set when instantiating secure decoders");
1514 return UNKNOWN_ERROR;
1515 }
1516
1517 // TRICKY: We rely on mRenderer being null, so that decoder does not start requesting
1518 // data on instantiation.
Lajos Molnar1de1e252015-04-30 18:18:34 -07001519 if (mSurface != NULL) {
Lajos Molnarfcd3e942015-03-31 10:06:48 -07001520 err = instantiateDecoder(false, &mVideoDecoder);
1521 if (err != OK) {
1522 return err;
1523 }
1524 }
1525
1526 if (mAudioSink != NULL) {
1527 err = instantiateDecoder(true, &mAudioDecoder);
1528 if (err != OK) {
1529 return err;
1530 }
1531 }
1532 return OK;
1533}
1534
Wei Jiac5de0912016-11-18 10:22:14 -08001535void NuPlayer::onStart(int64_t startPositionUs, MediaPlayerSeekMode mode) {
Hassan Shojaniacefac142017-02-06 21:02:02 -08001536 ALOGV("onStart: mCrypto: %p (%d)", mCrypto.get(),
1537 (mCrypto != NULL ? mCrypto->getStrongCount() : 0));
1538
Robert Shih0c61a0d2015-07-06 15:09:10 -07001539 if (!mSourceStarted) {
1540 mSourceStarted = true;
1541 mSource->start();
1542 }
1543 if (startPositionUs > 0) {
Wei Jiac5de0912016-11-18 10:22:14 -08001544 performSeek(startPositionUs, mode);
Robert Shih0c61a0d2015-07-06 15:09:10 -07001545 if (mSource->getFormat(false /* audio */) == NULL) {
1546 return;
1547 }
1548 }
1549
Wei Jia94211742014-10-28 17:09:06 -07001550 mOffloadAudio = false;
1551 mAudioEOS = false;
1552 mVideoEOS = false;
Wei Jia94211742014-10-28 17:09:06 -07001553 mStarted = true;
Wei Jiafe8fe7d2016-06-08 10:29:25 -07001554 mPaused = false;
Wei Jia94211742014-10-28 17:09:06 -07001555
Wei Jia94211742014-10-28 17:09:06 -07001556 uint32_t flags = 0;
1557
1558 if (mSource->isRealTime()) {
1559 flags |= Renderer::FLAG_REAL_TIME;
1560 }
1561
Wei Jia9737d342016-12-28 14:59:59 -08001562 bool hasAudio = (mSource->getFormat(true /* audio */) != NULL);
1563 bool hasVideo = (mSource->getFormat(false /* audio */) != NULL);
1564 if (!hasAudio && !hasVideo) {
Wei Jia1de83d52016-10-10 10:15:03 -07001565 ALOGE("no metadata for either audio or video source");
1566 mSource->stop();
1567 mSourceStarted = false;
1568 notifyListener(MEDIA_ERROR, MEDIA_ERROR_UNKNOWN, ERROR_MALFORMED);
1569 return;
1570 }
Wei Jia9737d342016-12-28 14:59:59 -08001571 ALOGV_IF(!hasAudio, "no metadata for audio source"); // video only stream
1572
1573 sp<MetaData> audioMeta = mSource->getFormatMeta(true /* audio */);
Wei Jia1de83d52016-10-10 10:15:03 -07001574
Wei Jia94211742014-10-28 17:09:06 -07001575 audio_stream_type_t streamType = AUDIO_STREAM_MUSIC;
1576 if (mAudioSink != NULL) {
1577 streamType = mAudioSink->getAudioStreamType();
1578 }
1579
Wei Jia94211742014-10-28 17:09:06 -07001580 mOffloadAudio =
Wei Jia9737d342016-12-28 14:59:59 -08001581 canOffloadStream(audioMeta, hasVideo, mSource->isStreaming(), streamType)
Wei Jiabf70feb2016-02-19 15:47:16 -08001582 && (mPlaybackSettings.mSpeed == 1.f && mPlaybackSettings.mPitch == 1.f);
Hassan Shojaniacefac142017-02-06 21:02:02 -08001583
1584 // Modular DRM: Disabling audio offload if the source is protected
1585 if (mOffloadAudio && mIsDrmProtected) {
1586 mOffloadAudio = false;
1587 ALOGV("onStart: Disabling mOffloadAudio now that the source is protected.");
1588 }
1589
Wei Jia94211742014-10-28 17:09:06 -07001590 if (mOffloadAudio) {
1591 flags |= Renderer::FLAG_OFFLOAD_AUDIO;
1592 }
1593
Lajos Molnar1d15ab52015-03-04 16:46:34 -08001594 sp<AMessage> notify = new AMessage(kWhatRendererNotify, this);
Wei Jia94211742014-10-28 17:09:06 -07001595 ++mRendererGeneration;
1596 notify->setInt32("generation", mRendererGeneration);
Wei Jia0a68f662017-08-30 18:01:26 -07001597 mRenderer = new Renderer(mAudioSink, mMediaClock, notify, flags);
Wei Jia94211742014-10-28 17:09:06 -07001598 mRendererLooper = new ALooper;
1599 mRendererLooper->setName("NuPlayerRenderer");
1600 mRendererLooper->start(false, false, ANDROID_PRIORITY_AUDIO);
1601 mRendererLooper->registerHandler(mRenderer);
Lajos Molnar3a474aa2015-04-24 17:10:07 -07001602
1603 status_t err = mRenderer->setPlaybackSettings(mPlaybackSettings);
1604 if (err != OK) {
1605 mSource->stop();
Robert Shih0c61a0d2015-07-06 15:09:10 -07001606 mSourceStarted = false;
Lajos Molnar3a474aa2015-04-24 17:10:07 -07001607 notifyListener(MEDIA_ERROR, MEDIA_ERROR_UNKNOWN, err);
1608 return;
Wei Jiac8206ff2015-03-04 13:59:37 -08001609 }
Wei Jia94211742014-10-28 17:09:06 -07001610
Ronghua Wuc8a70d32015-04-29 16:26:34 -07001611 float rate = getFrameRate();
1612 if (rate > 0) {
Wei Jia94211742014-10-28 17:09:06 -07001613 mRenderer->setVideoFrameRate(rate);
1614 }
1615
Wei Jiac6cfd702014-11-11 16:33:20 -08001616 if (mVideoDecoder != NULL) {
1617 mVideoDecoder->setRenderer(mRenderer);
1618 }
1619 if (mAudioDecoder != NULL) {
1620 mAudioDecoder->setRenderer(mRenderer);
1621 }
1622
Ray Essick0d98c182017-11-09 13:42:42 -08001623 startPlaybackTimer("onstart");
Ray Essickd4d00612017-01-03 09:36:27 -08001624
Wei Jia94211742014-10-28 17:09:06 -07001625 postScanSources();
1626}
1627
Ray Essick0d98c182017-11-09 13:42:42 -08001628void NuPlayer::startPlaybackTimer(const char *where) {
1629 Mutex::Autolock autoLock(mPlayingTimeLock);
1630 if (mLastStartedPlayingTimeNs == 0) {
1631 mLastStartedPlayingTimeNs = systemTime();
1632 ALOGV("startPlaybackTimer() time %20" PRId64 " (%s)", mLastStartedPlayingTimeNs, where);
1633 }
1634}
1635
Ray Essickeda32522018-02-28 12:08:28 -08001636void NuPlayer::updatePlaybackTimer(bool stopping, const char *where) {
Ray Essick0d98c182017-11-09 13:42:42 -08001637 Mutex::Autolock autoLock(mPlayingTimeLock);
1638
Ray Essickeda32522018-02-28 12:08:28 -08001639 ALOGV("updatePlaybackTimer(%s) time %20" PRId64 " (%s)",
1640 stopping ? "stop" : "snap", mLastStartedPlayingTimeNs, where);
Ray Essick0d98c182017-11-09 13:42:42 -08001641
1642 if (mLastStartedPlayingTimeNs != 0) {
1643 sp<NuPlayerDriver> driver = mDriver.promote();
Ray Essickeda32522018-02-28 12:08:28 -08001644 int64_t now = systemTime();
Ray Essick0d98c182017-11-09 13:42:42 -08001645 if (driver != NULL) {
Ray Essick0d98c182017-11-09 13:42:42 -08001646 int64_t played = now - mLastStartedPlayingTimeNs;
Ray Essickeda32522018-02-28 12:08:28 -08001647 ALOGV("updatePlaybackTimer() log %20" PRId64 "", played);
Ray Essick0d98c182017-11-09 13:42:42 -08001648
1649 if (played > 0) {
1650 driver->notifyMorePlayingTimeUs((played+500)/1000);
1651 }
1652 }
Ray Essickeda32522018-02-28 12:08:28 -08001653 if (stopping) {
1654 mLastStartedPlayingTimeNs = 0;
1655 } else {
1656 mLastStartedPlayingTimeNs = now;
1657 }
Ray Essick0d98c182017-11-09 13:42:42 -08001658 }
1659}
1660
Ray Essick58e0f7a2017-11-21 10:59:38 -08001661void NuPlayer::startRebufferingTimer() {
1662 Mutex::Autolock autoLock(mPlayingTimeLock);
1663 if (mLastStartedRebufferingTimeNs == 0) {
1664 mLastStartedRebufferingTimeNs = systemTime();
1665 ALOGV("startRebufferingTimer() time %20" PRId64 "", mLastStartedRebufferingTimeNs);
1666 }
1667}
1668
Ray Essickeda32522018-02-28 12:08:28 -08001669void NuPlayer::updateRebufferingTimer(bool stopping, bool exitingPlayback) {
Ray Essick58e0f7a2017-11-21 10:59:38 -08001670 Mutex::Autolock autoLock(mPlayingTimeLock);
1671
Ray Essickeda32522018-02-28 12:08:28 -08001672 ALOGV("updateRebufferingTimer(%s) time %20" PRId64 " (exiting %d)",
1673 stopping ? "stop" : "snap", mLastStartedRebufferingTimeNs, exitingPlayback);
Ray Essick58e0f7a2017-11-21 10:59:38 -08001674
1675 if (mLastStartedRebufferingTimeNs != 0) {
1676 sp<NuPlayerDriver> driver = mDriver.promote();
Ray Essickeda32522018-02-28 12:08:28 -08001677 int64_t now = systemTime();
Ray Essick58e0f7a2017-11-21 10:59:38 -08001678 if (driver != NULL) {
Ray Essick58e0f7a2017-11-21 10:59:38 -08001679 int64_t rebuffered = now - mLastStartedRebufferingTimeNs;
Ray Essickeda32522018-02-28 12:08:28 -08001680 ALOGV("updateRebufferingTimer() log %20" PRId64 "", rebuffered);
Ray Essick58e0f7a2017-11-21 10:59:38 -08001681
1682 if (rebuffered > 0) {
1683 driver->notifyMoreRebufferingTimeUs((rebuffered+500)/1000);
1684 if (exitingPlayback) {
1685 driver->notifyRebufferingWhenExit(true);
1686 }
1687 }
1688 }
Ray Essickeda32522018-02-28 12:08:28 -08001689 if (stopping) {
1690 mLastStartedRebufferingTimeNs = 0;
1691 } else {
1692 mLastStartedRebufferingTimeNs = now;
1693 }
Ray Essick58e0f7a2017-11-21 10:59:38 -08001694 }
1695}
1696
Ray Essickeda32522018-02-28 12:08:28 -08001697void NuPlayer::updateInternalTimers() {
1698 // update values, but ticking clocks keep ticking
1699 ALOGV("updateInternalTimers()");
1700 updatePlaybackTimer(false /* stopping */, "updateInternalTimers");
1701 updateRebufferingTimer(false /* stopping */, false /* exiting */);
1702}
1703
Chong Zhangefbb6192015-01-30 17:13:27 -08001704void NuPlayer::onPause() {
Ray Essick0d98c182017-11-09 13:42:42 -08001705
Ray Essickeda32522018-02-28 12:08:28 -08001706 updatePlaybackTimer(true /* stopping */, "onPause");
Ray Essick0d98c182017-11-09 13:42:42 -08001707
Chong Zhangefbb6192015-01-30 17:13:27 -08001708 if (mPaused) {
1709 return;
1710 }
1711 mPaused = true;
1712 if (mSource != NULL) {
1713 mSource->pause();
1714 } else {
1715 ALOGW("pause called when source is gone or not set");
1716 }
1717 if (mRenderer != NULL) {
1718 mRenderer->pause();
1719 } else {
1720 ALOGW("pause called when renderer is gone or not set");
1721 }
Ray Essickd4d00612017-01-03 09:36:27 -08001722
Chong Zhangefbb6192015-01-30 17:13:27 -08001723}
1724
Ronghua Wud7988b12014-10-03 15:19:10 -07001725bool NuPlayer::audioDecoderStillNeeded() {
1726 // Audio decoder is no longer needed if it's in shut/shutting down status.
1727 return ((mFlushingAudio != SHUT_DOWN) && (mFlushingAudio != SHUTTING_DOWN_DECODER));
1728}
1729
Andy Hung8d121d42014-10-03 09:53:53 -07001730void NuPlayer::handleFlushComplete(bool audio, bool isDecoder) {
1731 // We wait for both the decoder flush and the renderer flush to complete
1732 // before entering either the FLUSHED or the SHUTTING_DOWN_DECODER state.
1733
1734 mFlushComplete[audio][isDecoder] = true;
1735 if (!mFlushComplete[audio][!isDecoder]) {
1736 return;
1737 }
1738
1739 FlushStatus *state = audio ? &mFlushingAudio : &mFlushingVideo;
1740 switch (*state) {
1741 case FLUSHING_DECODER:
1742 {
1743 *state = FLUSHED;
Andy Hung8d121d42014-10-03 09:53:53 -07001744 break;
1745 }
1746
1747 case FLUSHING_DECODER_SHUTDOWN:
1748 {
1749 *state = SHUTTING_DOWN_DECODER;
1750
1751 ALOGV("initiating %s decoder shutdown", audio ? "audio" : "video");
Andy Hung8d121d42014-10-03 09:53:53 -07001752 getDecoder(audio)->initiateShutdown();
1753 break;
1754 }
1755
1756 default:
1757 // decoder flush completes only occur in a flushing state.
1758 LOG_ALWAYS_FATAL_IF(isDecoder, "decoder flush in invalid state %d", *state);
1759 break;
1760 }
1761}
1762
Andreas Huber3831a062010-12-21 10:22:33 -08001763void NuPlayer::finishFlushIfPossible() {
Wei Jia53904f32014-07-29 10:22:53 -07001764 if (mFlushingAudio != NONE && mFlushingAudio != FLUSHED
1765 && mFlushingAudio != SHUT_DOWN) {
Andreas Huber3831a062010-12-21 10:22:33 -08001766 return;
1767 }
1768
Wei Jia53904f32014-07-29 10:22:53 -07001769 if (mFlushingVideo != NONE && mFlushingVideo != FLUSHED
1770 && mFlushingVideo != SHUT_DOWN) {
Andreas Huber3831a062010-12-21 10:22:33 -08001771 return;
1772 }
1773
Steve Block3856b092011-10-20 11:56:00 +01001774 ALOGV("both audio and video are flushed now.");
Andreas Huber3831a062010-12-21 10:22:33 -08001775
Andreas Huber3831a062010-12-21 10:22:33 -08001776 mFlushingAudio = NONE;
1777 mFlushingVideo = NONE;
Andreas Huber3831a062010-12-21 10:22:33 -08001778
Andy Hung8d121d42014-10-03 09:53:53 -07001779 clearFlushComplete();
1780
Andreas Hubera1f8ab02012-11-30 10:53:22 -08001781 processDeferredActions();
Andreas Huber1aef2112011-01-04 14:01:29 -08001782}
1783
1784void NuPlayer::postScanSources() {
1785 if (mScanSourcesPending) {
1786 return;
1787 }
1788
Lajos Molnar1d15ab52015-03-04 16:46:34 -08001789 sp<AMessage> msg = new AMessage(kWhatScanSources, this);
Andreas Huber1aef2112011-01-04 14:01:29 -08001790 msg->setInt32("generation", mScanSourcesGeneration);
1791 msg->post();
1792
1793 mScanSourcesPending = true;
1794}
1795
Wei Jia41cd4632016-05-13 10:53:30 -07001796void NuPlayer::tryOpenAudioSinkForOffload(
1797 const sp<AMessage> &format, const sp<MetaData> &audioMeta, bool hasVideo) {
Andy Hung202bce12014-12-03 11:47:36 -08001798 // Note: This is called early in NuPlayer to determine whether offloading
1799 // is possible; otherwise the decoders call the renderer openAudioSink directly.
Andy Hung282a7e32014-08-14 15:56:34 -07001800
Andy Hung202bce12014-12-03 11:47:36 -08001801 status_t err = mRenderer->openAudioSink(
Dhananjay Kumarc387f2b2015-08-06 10:43:16 +05301802 format, true /* offloadOnly */, hasVideo,
1803 AUDIO_OUTPUT_FLAG_NONE, &mOffloadAudio, mSource->isStreaming());
Andy Hung202bce12014-12-03 11:47:36 -08001804 if (err != OK) {
1805 // Any failure we turn off mOffloadAudio.
1806 mOffloadAudio = false;
1807 } else if (mOffloadAudio) {
Chong Zhang3b9eb1f2014-10-15 17:05:08 -07001808 sendMetaDataToHal(mAudioSink, audioMeta);
Andy Hung282a7e32014-08-14 15:56:34 -07001809 }
1810}
1811
1812void NuPlayer::closeAudioSink() {
Daniel Norman1e0d88b2020-01-07 15:02:54 -08001813 if (mRenderer != NULL) {
1814 mRenderer->closeAudioSink();
1815 }
Andy Hung282a7e32014-08-14 15:56:34 -07001816}
1817
Wei Jia5031b2f2016-02-25 11:19:31 -08001818void NuPlayer::restartAudio(
Wei Jiabf70feb2016-02-19 15:47:16 -08001819 int64_t currentPositionUs, bool forceNonOffload, bool needsToCreateAudioDecoder) {
Wei Jia355b2bb2018-04-09 16:36:23 -07001820 ALOGD("restartAudio timeUs(%lld), dontOffload(%d), createDecoder(%d)",
1821 (long long)currentPositionUs, forceNonOffload, needsToCreateAudioDecoder);
Wei Jiaa05f1e32016-03-25 16:31:22 -07001822 if (mAudioDecoder != NULL) {
1823 mAudioDecoder->pause();
Ray Essickfcb8fd32018-08-07 09:39:38 -07001824 Mutex::Autolock autoLock(mDecoderLock);
Wei Jiaa05f1e32016-03-25 16:31:22 -07001825 mAudioDecoder.clear();
Wei Jia686e8e52017-04-03 14:08:01 -07001826 mAudioDecoderError = false;
Wei Jiaa05f1e32016-03-25 16:31:22 -07001827 ++mAudioDecoderGeneration;
1828 }
Wei Jiabf70feb2016-02-19 15:47:16 -08001829 if (mFlushingAudio == FLUSHING_DECODER) {
1830 mFlushComplete[1 /* audio */][1 /* isDecoder */] = true;
1831 mFlushingAudio = FLUSHED;
1832 finishFlushIfPossible();
1833 } else if (mFlushingAudio == FLUSHING_DECODER_SHUTDOWN
1834 || mFlushingAudio == SHUTTING_DOWN_DECODER) {
1835 mFlushComplete[1 /* audio */][1 /* isDecoder */] = true;
1836 mFlushingAudio = SHUT_DOWN;
1837 finishFlushIfPossible();
1838 needsToCreateAudioDecoder = false;
1839 }
1840 if (mRenderer == NULL) {
1841 return;
1842 }
1843 closeAudioSink();
1844 mRenderer->flush(true /* audio */, false /* notifyComplete */);
1845 if (mVideoDecoder != NULL) {
Praveen Chavaneca08fb2019-01-21 11:00:38 -08001846 mDeferredActions.push_back(
1847 new FlushDecoderAction(FLUSH_CMD_NONE /* audio */,
1848 FLUSH_CMD_FLUSH /* video */));
1849 mDeferredActions.push_back(
1850 new SeekAction(currentPositionUs,
1851 MediaPlayerSeekMode::SEEK_PREVIOUS_SYNC /* mode */));
1852 // After a flush without shutdown, decoder is paused.
1853 // Don't resume it until source seek is done, otherwise it could
1854 // start pulling stale data too soon.
1855 mDeferredActions.push_back(new ResumeDecoderAction(false));
1856 processDeferredActions();
1857 } else {
1858 performSeek(currentPositionUs, MediaPlayerSeekMode::SEEK_PREVIOUS_SYNC /* mode */);
Wei Jiabf70feb2016-02-19 15:47:16 -08001859 }
1860
Wei Jiabf70feb2016-02-19 15:47:16 -08001861 if (forceNonOffload) {
1862 mRenderer->signalDisableOffloadAudio();
1863 mOffloadAudio = false;
1864 }
1865 if (needsToCreateAudioDecoder) {
Wei Jiaa05f1e32016-03-25 16:31:22 -07001866 instantiateDecoder(true /* audio */, &mAudioDecoder, !forceNonOffload);
Wei Jiabf70feb2016-02-19 15:47:16 -08001867 }
1868}
1869
Wei Jia41cd4632016-05-13 10:53:30 -07001870void NuPlayer::determineAudioModeChange(const sp<AMessage> &audioFormat) {
Wei Jiae4d18c72015-07-13 17:58:11 -07001871 if (mSource == NULL || mAudioSink == NULL) {
1872 return;
1873 }
1874
1875 if (mRenderer == NULL) {
1876 ALOGW("No renderer can be used to determine audio mode. Use non-offload for safety.");
1877 mOffloadAudio = false;
1878 return;
1879 }
1880
1881 sp<MetaData> audioMeta = mSource->getFormatMeta(true /* audio */);
1882 sp<AMessage> videoFormat = mSource->getFormat(false /* audio */);
1883 audio_stream_type_t streamType = mAudioSink->getAudioStreamType();
1884 const bool hasVideo = (videoFormat != NULL);
Hassan Shojaniacefac142017-02-06 21:02:02 -08001885 bool canOffload = canOffloadStream(
Wei Jiabf70feb2016-02-19 15:47:16 -08001886 audioMeta, hasVideo, mSource->isStreaming(), streamType)
1887 && (mPlaybackSettings.mSpeed == 1.f && mPlaybackSettings.mPitch == 1.f);
Hassan Shojaniacefac142017-02-06 21:02:02 -08001888
1889 // Modular DRM: Disabling audio offload if the source is protected
1890 if (canOffload && mIsDrmProtected) {
1891 canOffload = false;
1892 ALOGV("determineAudioModeChange: Disabling mOffloadAudio b/c the source is protected.");
1893 }
1894
Wei Jiae4d18c72015-07-13 17:58:11 -07001895 if (canOffload) {
1896 if (!mOffloadAudio) {
1897 mRenderer->signalEnableOffloadAudio();
1898 }
1899 // open audio sink early under offload mode.
Wei Jia41cd4632016-05-13 10:53:30 -07001900 tryOpenAudioSinkForOffload(audioFormat, audioMeta, hasVideo);
Wei Jiae4d18c72015-07-13 17:58:11 -07001901 } else {
Robert Shihe1d70192015-07-23 17:54:13 -07001902 if (mOffloadAudio) {
1903 mRenderer->signalDisableOffloadAudio();
1904 mOffloadAudio = false;
1905 }
Wei Jiae4d18c72015-07-13 17:58:11 -07001906 }
1907}
1908
Wei Jiaa05f1e32016-03-25 16:31:22 -07001909status_t NuPlayer::instantiateDecoder(
1910 bool audio, sp<DecoderBase> *decoder, bool checkAudioModeChange) {
Wei Jia566da802015-08-27 13:59:30 -07001911 // The audio decoder could be cleared by tear down. If still in shut down
1912 // process, no need to create a new audio decoder.
1913 if (*decoder != NULL || (audio && mFlushingAudio == SHUT_DOWN)) {
Andreas Huberf9334412010-12-15 15:17:42 -08001914 return OK;
1915 }
1916
Andreas Huber84066782011-08-16 09:34:26 -07001917 sp<AMessage> format = mSource->getFormat(audio);
Andreas Huberf9334412010-12-15 15:17:42 -08001918
Andreas Huber84066782011-08-16 09:34:26 -07001919 if (format == NULL) {
Robert Shih7350b052015-10-01 15:50:14 -07001920 return UNKNOWN_ERROR;
1921 } else {
1922 status_t err;
1923 if (format->findInt32("err", &err) && err) {
1924 return err;
1925 }
Andreas Huberf9334412010-12-15 15:17:42 -08001926 }
1927
Ronghua Wu8db88132015-04-22 13:51:35 -07001928 format->setInt32("priority", 0 /* realtime */);
1929
Andreas Huber3fe62152011-09-16 15:09:22 -07001930 if (!audio) {
Andreas Huber84066782011-08-16 09:34:26 -07001931 AString mime;
1932 CHECK(format->findString("mime", &mime));
Chong Zhanga7fa1d92014-06-11 14:49:23 -07001933
Lajos Molnar1d15ab52015-03-04 16:46:34 -08001934 sp<AMessage> ccNotify = new AMessage(kWhatClosedCaptionNotify, this);
Chong Zhang341ab6e2015-02-04 13:37:18 -08001935 if (mCCDecoder == NULL) {
1936 mCCDecoder = new CCDecoder(ccNotify);
1937 }
Lajos Molnar09524832014-07-17 14:29:51 -07001938
1939 if (mSourceFlags & Source::FLAG_SECURE) {
1940 format->setInt32("secure", true);
1941 }
Chong Zhang17134602015-01-07 16:14:34 -08001942
1943 if (mSourceFlags & Source::FLAG_PROTECTED) {
1944 format->setInt32("protected", true);
1945 }
Ronghua Wu8db88132015-04-22 13:51:35 -07001946
Ronghua Wuc8a70d32015-04-29 16:26:34 -07001947 float rate = getFrameRate();
1948 if (rate > 0) {
Lajos Molnar3a474aa2015-04-24 17:10:07 -07001949 format->setFloat("operating-rate", rate * mPlaybackSettings.mSpeed);
Ronghua Wu8db88132015-04-22 13:51:35 -07001950 }
Andreas Huber3fe62152011-09-16 15:09:22 -07001951 }
1952
Ray Essickfcb8fd32018-08-07 09:39:38 -07001953 Mutex::Autolock autoLock(mDecoderLock);
1954
Wei Jiabc2fb722014-07-08 16:37:57 -07001955 if (audio) {
Lajos Molnar1d15ab52015-03-04 16:46:34 -08001956 sp<AMessage> notify = new AMessage(kWhatAudioNotify, this);
Wei Jia88703c32014-08-06 11:24:07 -07001957 ++mAudioDecoderGeneration;
1958 notify->setInt32("generation", mAudioDecoderGeneration);
1959
Wei Jiaa05f1e32016-03-25 16:31:22 -07001960 if (checkAudioModeChange) {
Wei Jia41cd4632016-05-13 10:53:30 -07001961 determineAudioModeChange(format);
Wei Jiaa05f1e32016-03-25 16:31:22 -07001962 }
Wei Jiabc2fb722014-07-08 16:37:57 -07001963 if (mOffloadAudio) {
Wei Jia14532f22015-12-29 11:28:15 -08001964 mSource->setOffloadAudio(true /* offload */);
1965
Haynes Mathew George8b635332015-03-30 17:59:47 -07001966 const bool hasVideo = (mSource->getFormat(false /*audio */) != NULL);
1967 format->setInt32("has-video", hasVideo);
Wei Jiac6cfd702014-11-11 16:33:20 -08001968 *decoder = new DecoderPassThrough(notify, mSource, mRenderer);
Hassan Shojaniacefac142017-02-06 21:02:02 -08001969 ALOGV("instantiateDecoder audio DecoderPassThrough hasVideo: %d", hasVideo);
Wei Jiabc2fb722014-07-08 16:37:57 -07001970 } else {
Wei Jia14532f22015-12-29 11:28:15 -08001971 mSource->setOffloadAudio(false /* offload */);
1972
Wei Jiaf2ae3e12016-10-27 17:10:59 -07001973 *decoder = new Decoder(notify, mSource, mPID, mUID, mRenderer);
Hassan Shojaniacefac142017-02-06 21:02:02 -08001974 ALOGV("instantiateDecoder audio Decoder");
Wei Jiabc2fb722014-07-08 16:37:57 -07001975 }
Wei Jia686e8e52017-04-03 14:08:01 -07001976 mAudioDecoderError = false;
Wei Jiabc2fb722014-07-08 16:37:57 -07001977 } else {
Lajos Molnar1d15ab52015-03-04 16:46:34 -08001978 sp<AMessage> notify = new AMessage(kWhatVideoNotify, this);
Wei Jia88703c32014-08-06 11:24:07 -07001979 ++mVideoDecoderGeneration;
1980 notify->setInt32("generation", mVideoDecoderGeneration);
1981
Chong Zhang7137ec72014-11-12 16:41:05 -08001982 *decoder = new Decoder(
Wei Jiaf2ae3e12016-10-27 17:10:59 -07001983 notify, mSource, mPID, mUID, mRenderer, mSurface, mCCDecoder);
Wei Jia686e8e52017-04-03 14:08:01 -07001984 mVideoDecoderError = false;
Lajos Molnard9fd6312014-11-06 11:00:00 -08001985
1986 // enable FRC if high-quality AV sync is requested, even if not
Lajos Molnar1de1e252015-04-30 18:18:34 -07001987 // directly queuing to display, as this will even improve textureview
Lajos Molnard9fd6312014-11-06 11:00:00 -08001988 // playback.
1989 {
Marco Nelissen96626b72016-12-01 13:58:59 -08001990 if (property_get_bool("persist.sys.media.avsync", false)) {
Lajos Molnard9fd6312014-11-06 11:00:00 -08001991 format->setInt32("auto-frc", 1);
1992 }
1993 }
Wei Jiabc2fb722014-07-08 16:37:57 -07001994 }
Lajos Molnar1cd13982014-01-17 15:12:51 -08001995 (*decoder)->init();
Hassan Shojaniacefac142017-02-06 21:02:02 -08001996
1997 // Modular DRM
1998 if (mIsDrmProtected) {
1999 format->setPointer("crypto", mCrypto.get());
2000 ALOGV("instantiateDecoder: mCrypto: %p (%d) isSecure: %d", mCrypto.get(),
2001 (mCrypto != NULL ? mCrypto->getStrongCount() : 0),
2002 (mSourceFlags & Source::FLAG_SECURE) != 0);
2003 }
2004
Andreas Huber84066782011-08-16 09:34:26 -07002005 (*decoder)->configure(format);
Andreas Huberf9334412010-12-15 15:17:42 -08002006
Praveen Chavanbbaa1442016-04-08 13:33:49 -07002007 if (!audio) {
2008 sp<AMessage> params = new AMessage();
2009 float rate = getFrameRate();
2010 if (rate > 0) {
2011 params->setFloat("frame-rate-total", rate);
2012 }
2013
2014 sp<MetaData> fileMeta = getFileMeta();
2015 if (fileMeta != NULL) {
2016 int32_t videoTemporalLayerCount;
2017 if (fileMeta->findInt32(kKeyTemporalLayerCount, &videoTemporalLayerCount)
2018 && videoTemporalLayerCount > 0) {
2019 params->setInt32("temporal-layer-count", videoTemporalLayerCount);
2020 }
2021 }
2022
2023 if (params->countEntries() > 0) {
2024 (*decoder)->setParameters(params);
2025 }
2026 }
Andreas Huberf9334412010-12-15 15:17:42 -08002027 return OK;
2028}
2029
Chong Zhangced1c2f2014-08-08 15:22:35 -07002030void NuPlayer::updateVideoSize(
2031 const sp<AMessage> &inputFormat,
2032 const sp<AMessage> &outputFormat) {
2033 if (inputFormat == NULL) {
2034 ALOGW("Unknown video size, reporting 0x0!");
2035 notifyListener(MEDIA_SET_VIDEO_SIZE, 0, 0);
2036 return;
2037 }
Wei Jia9737d342016-12-28 14:59:59 -08002038 int32_t err = OK;
2039 inputFormat->findInt32("err", &err);
2040 if (err == -EWOULDBLOCK) {
2041 ALOGW("Video meta is not available yet!");
2042 return;
2043 }
2044 if (err != OK) {
2045 ALOGW("Something is wrong with video meta!");
2046 return;
2047 }
Chong Zhangced1c2f2014-08-08 15:22:35 -07002048
2049 int32_t displayWidth, displayHeight;
Chong Zhangced1c2f2014-08-08 15:22:35 -07002050 if (outputFormat != NULL) {
2051 int32_t width, height;
2052 CHECK(outputFormat->findInt32("width", &width));
2053 CHECK(outputFormat->findInt32("height", &height));
2054
2055 int32_t cropLeft, cropTop, cropRight, cropBottom;
2056 CHECK(outputFormat->findRect(
2057 "crop",
2058 &cropLeft, &cropTop, &cropRight, &cropBottom));
2059
2060 displayWidth = cropRight - cropLeft + 1;
2061 displayHeight = cropBottom - cropTop + 1;
2062
2063 ALOGV("Video output format changed to %d x %d "
2064 "(crop: %d x %d @ (%d, %d))",
2065 width, height,
2066 displayWidth,
2067 displayHeight,
2068 cropLeft, cropTop);
2069 } else {
2070 CHECK(inputFormat->findInt32("width", &displayWidth));
2071 CHECK(inputFormat->findInt32("height", &displayHeight));
2072
2073 ALOGV("Video input format %d x %d", displayWidth, displayHeight);
2074 }
2075
2076 // Take into account sample aspect ratio if necessary:
2077 int32_t sarWidth, sarHeight;
2078 if (inputFormat->findInt32("sar-width", &sarWidth)
Wei Jia095bc252017-01-27 10:16:16 -08002079 && inputFormat->findInt32("sar-height", &sarHeight)
2080 && sarWidth > 0 && sarHeight > 0) {
Chong Zhangced1c2f2014-08-08 15:22:35 -07002081 ALOGV("Sample aspect ratio %d : %d", sarWidth, sarHeight);
2082
2083 displayWidth = (displayWidth * sarWidth) / sarHeight;
2084
2085 ALOGV("display dimensions %d x %d", displayWidth, displayHeight);
Wei Jiadf6c6af2016-09-29 11:27:15 -07002086 } else {
2087 int32_t width, height;
2088 if (inputFormat->findInt32("display-width", &width)
2089 && inputFormat->findInt32("display-height", &height)
2090 && width > 0 && height > 0
2091 && displayWidth > 0 && displayHeight > 0) {
2092 if (displayHeight * (int64_t)width / height > (int64_t)displayWidth) {
2093 displayHeight = (int32_t)(displayWidth * (int64_t)height / width);
2094 } else {
2095 displayWidth = (int32_t)(displayHeight * (int64_t)width / height);
2096 }
2097 ALOGV("Video display width and height are overridden to %d x %d",
2098 displayWidth, displayHeight);
2099 }
Chong Zhangced1c2f2014-08-08 15:22:35 -07002100 }
2101
2102 int32_t rotationDegrees;
2103 if (!inputFormat->findInt32("rotation-degrees", &rotationDegrees)) {
2104 rotationDegrees = 0;
2105 }
2106
2107 if (rotationDegrees == 90 || rotationDegrees == 270) {
2108 int32_t tmp = displayWidth;
2109 displayWidth = displayHeight;
2110 displayHeight = tmp;
2111 }
2112
2113 notifyListener(
2114 MEDIA_SET_VIDEO_SIZE,
2115 displayWidth,
2116 displayHeight);
2117}
2118
Chong Zhangdcb89b32013-08-06 09:44:47 -07002119void NuPlayer::notifyListener(int msg, int ext1, int ext2, const Parcel *in) {
Andreas Huber43c3e6c2011-01-05 12:17:08 -08002120 if (mDriver == NULL) {
Andreas Huberf9334412010-12-15 15:17:42 -08002121 return;
2122 }
2123
Andreas Huber43c3e6c2011-01-05 12:17:08 -08002124 sp<NuPlayerDriver> driver = mDriver.promote();
Andreas Huberf9334412010-12-15 15:17:42 -08002125
Andreas Huber43c3e6c2011-01-05 12:17:08 -08002126 if (driver == NULL) {
Andreas Huberf9334412010-12-15 15:17:42 -08002127 return;
2128 }
2129
Chong Zhangdcb89b32013-08-06 09:44:47 -07002130 driver->notifyListener(msg, ext1, ext2, in);
Andreas Huberf9334412010-12-15 15:17:42 -08002131}
2132
Chong Zhang7137ec72014-11-12 16:41:05 -08002133void NuPlayer::flushDecoder(bool audio, bool needShutdown) {
Andreas Huber14f76722013-01-15 09:04:18 -08002134 ALOGV("[%s] flushDecoder needShutdown=%d",
2135 audio ? "audio" : "video", needShutdown);
2136
Chong Zhang7137ec72014-11-12 16:41:05 -08002137 const sp<DecoderBase> &decoder = getDecoder(audio);
Lajos Molnar87603c02014-08-20 19:25:30 -07002138 if (decoder == NULL) {
Steve Blockdf64d152012-01-04 20:05:49 +00002139 ALOGI("flushDecoder %s without decoder present",
Andreas Huber6e3d3112011-11-28 12:36:11 -08002140 audio ? "audio" : "video");
Lajos Molnar87603c02014-08-20 19:25:30 -07002141 return;
Andreas Huber6e3d3112011-11-28 12:36:11 -08002142 }
2143
Andreas Huber1aef2112011-01-04 14:01:29 -08002144 // Make sure we don't continue to scan sources until we finish flushing.
2145 ++mScanSourcesGeneration;
Chong Zhang3b032b32015-04-17 15:49:06 -07002146 if (mScanSourcesPending) {
Toshikazu Saitocbcbb792015-09-15 14:53:01 +09002147 if (!needShutdown) {
2148 mDeferredActions.push_back(
2149 new SimpleAction(&NuPlayer::performScanSources));
2150 }
Chong Zhang3b032b32015-04-17 15:49:06 -07002151 mScanSourcesPending = false;
2152 }
Andreas Huber1aef2112011-01-04 14:01:29 -08002153
Chong Zhang7137ec72014-11-12 16:41:05 -08002154 decoder->signalFlush();
Andreas Huber1aef2112011-01-04 14:01:29 -08002155
2156 FlushStatus newStatus =
2157 needShutdown ? FLUSHING_DECODER_SHUTDOWN : FLUSHING_DECODER;
2158
Lajos Molnarfcd3e942015-03-31 10:06:48 -07002159 mFlushComplete[audio][false /* isDecoder */] = (mRenderer == NULL);
Andy Hung8d121d42014-10-03 09:53:53 -07002160 mFlushComplete[audio][true /* isDecoder */] = false;
Andreas Huber1aef2112011-01-04 14:01:29 -08002161 if (audio) {
Wei Jia53904f32014-07-29 10:22:53 -07002162 ALOGE_IF(mFlushingAudio != NONE,
2163 "audio flushDecoder() is called in state %d", mFlushingAudio);
Andreas Huber1aef2112011-01-04 14:01:29 -08002164 mFlushingAudio = newStatus;
Andreas Huber1aef2112011-01-04 14:01:29 -08002165 } else {
Wei Jia53904f32014-07-29 10:22:53 -07002166 ALOGE_IF(mFlushingVideo != NONE,
2167 "video flushDecoder() is called in state %d", mFlushingVideo);
Andreas Huber1aef2112011-01-04 14:01:29 -08002168 mFlushingVideo = newStatus;
Andreas Huber1aef2112011-01-04 14:01:29 -08002169 }
2170}
2171
Chong Zhangced1c2f2014-08-08 15:22:35 -07002172void NuPlayer::queueDecoderShutdown(
2173 bool audio, bool video, const sp<AMessage> &reply) {
2174 ALOGI("queueDecoderShutdown audio=%d, video=%d", audio, video);
Andreas Huber84066782011-08-16 09:34:26 -07002175
Chong Zhangced1c2f2014-08-08 15:22:35 -07002176 mDeferredActions.push_back(
Wei Jiafef808d2014-10-31 17:57:05 -07002177 new FlushDecoderAction(
2178 audio ? FLUSH_CMD_SHUTDOWN : FLUSH_CMD_NONE,
2179 video ? FLUSH_CMD_SHUTDOWN : FLUSH_CMD_NONE));
Andreas Huber84066782011-08-16 09:34:26 -07002180
Chong Zhangced1c2f2014-08-08 15:22:35 -07002181 mDeferredActions.push_back(
2182 new SimpleAction(&NuPlayer::performScanSources));
Andreas Huber84066782011-08-16 09:34:26 -07002183
Chong Zhangced1c2f2014-08-08 15:22:35 -07002184 mDeferredActions.push_back(new PostMessageAction(reply));
2185
2186 processDeferredActions();
Andreas Huber84066782011-08-16 09:34:26 -07002187}
2188
James Dong0d268a32012-08-31 12:18:27 -07002189status_t NuPlayer::setVideoScalingMode(int32_t mode) {
2190 mVideoScalingMode = mode;
Lajos Molnar1de1e252015-04-30 18:18:34 -07002191 if (mSurface != NULL) {
2192 status_t ret = native_window_set_scaling_mode(mSurface.get(), mVideoScalingMode);
James Dong0d268a32012-08-31 12:18:27 -07002193 if (ret != OK) {
2194 ALOGE("Failed to set scaling mode (%d): %s",
2195 -ret, strerror(-ret));
2196 return ret;
2197 }
2198 }
2199 return OK;
2200}
2201
Chong Zhangdcb89b32013-08-06 09:44:47 -07002202status_t NuPlayer::getTrackInfo(Parcel* reply) const {
Lajos Molnar1d15ab52015-03-04 16:46:34 -08002203 sp<AMessage> msg = new AMessage(kWhatGetTrackInfo, this);
Chong Zhangdcb89b32013-08-06 09:44:47 -07002204 msg->setPointer("reply", reply);
2205
2206 sp<AMessage> response;
2207 status_t err = msg->postAndAwaitResponse(&response);
2208 return err;
2209}
2210
Robert Shih7c4f0d72014-07-09 18:53:31 -07002211status_t NuPlayer::getSelectedTrack(int32_t type, Parcel* reply) const {
Lajos Molnar1d15ab52015-03-04 16:46:34 -08002212 sp<AMessage> msg = new AMessage(kWhatGetSelectedTrack, this);
Robert Shih7c4f0d72014-07-09 18:53:31 -07002213 msg->setPointer("reply", reply);
2214 msg->setInt32("type", type);
2215
2216 sp<AMessage> response;
2217 status_t err = msg->postAndAwaitResponse(&response);
2218 if (err == OK && response != NULL) {
2219 CHECK(response->findInt32("err", &err));
2220 }
2221 return err;
2222}
2223
Robert Shih6ffb1fd2014-10-29 16:24:32 -07002224status_t NuPlayer::selectTrack(size_t trackIndex, bool select, int64_t timeUs) {
Lajos Molnar1d15ab52015-03-04 16:46:34 -08002225 sp<AMessage> msg = new AMessage(kWhatSelectTrack, this);
Chong Zhangdcb89b32013-08-06 09:44:47 -07002226 msg->setSize("trackIndex", trackIndex);
2227 msg->setInt32("select", select);
Robert Shih6ffb1fd2014-10-29 16:24:32 -07002228 msg->setInt64("timeUs", timeUs);
Chong Zhangdcb89b32013-08-06 09:44:47 -07002229
2230 sp<AMessage> response;
2231 status_t err = msg->postAndAwaitResponse(&response);
2232
Chong Zhang404fced2014-06-11 14:45:31 -07002233 if (err != OK) {
2234 return err;
2235 }
2236
2237 if (!response->findInt32("err", &err)) {
2238 err = OK;
2239 }
2240
Chong Zhangdcb89b32013-08-06 09:44:47 -07002241 return err;
2242}
2243
Ronghua Wua73d9e02014-10-08 15:13:29 -07002244status_t NuPlayer::getCurrentPosition(int64_t *mediaUs) {
2245 sp<Renderer> renderer = mRenderer;
2246 if (renderer == NULL) {
2247 return NO_INIT;
2248 }
2249
2250 return renderer->getCurrentPosition(mediaUs);
2251}
2252
Ray Essickffc941f2018-05-18 11:08:37 -07002253void NuPlayer::getStats(Vector<sp<AMessage> > *trackStats) {
2254 CHECK(trackStats != NULL);
Praveen Chavane1e5d7a2015-05-19 19:09:48 -07002255
Ray Essickfcb8fd32018-08-07 09:39:38 -07002256 trackStats->clear();
Ray Essickffc941f2018-05-18 11:08:37 -07002257
Ray Essickfcb8fd32018-08-07 09:39:38 -07002258 Mutex::Autolock autoLock(mDecoderLock);
2259 if (mVideoDecoder != NULL) {
2260 trackStats->push_back(mVideoDecoder->getStats());
2261 }
2262 if (mAudioDecoder != NULL) {
2263 trackStats->push_back(mAudioDecoder->getStats());
2264 }
Ronghua Wua73d9e02014-10-08 15:13:29 -07002265}
2266
Marco Nelissenf0b72b52014-09-16 15:43:44 -07002267sp<MetaData> NuPlayer::getFileMeta() {
2268 return mSource->getFileFormatMeta();
2269}
2270
Ronghua Wuc8a70d32015-04-29 16:26:34 -07002271float NuPlayer::getFrameRate() {
2272 sp<MetaData> meta = mSource->getFormatMeta(false /* audio */);
2273 if (meta == NULL) {
2274 return 0;
2275 }
2276 int32_t rate;
2277 if (!meta->findInt32(kKeyFrameRate, &rate)) {
2278 // fall back to try file meta
2279 sp<MetaData> fileMeta = getFileMeta();
2280 if (fileMeta == NULL) {
2281 ALOGW("source has video meta but not file meta");
2282 return -1;
2283 }
2284 int32_t fileMetaRate;
2285 if (!fileMeta->findInt32(kKeyFrameRate, &fileMetaRate)) {
2286 return -1;
2287 }
2288 return fileMetaRate;
2289 }
2290 return rate;
2291}
2292
Andreas Huberb7c8e912012-11-27 15:02:53 -08002293void NuPlayer::schedulePollDuration() {
Lajos Molnar1d15ab52015-03-04 16:46:34 -08002294 sp<AMessage> msg = new AMessage(kWhatPollDuration, this);
Andreas Huberb7c8e912012-11-27 15:02:53 -08002295 msg->setInt32("generation", mPollDurationGeneration);
2296 msg->post();
2297}
2298
2299void NuPlayer::cancelPollDuration() {
2300 ++mPollDurationGeneration;
2301}
2302
Andreas Hubera1f8ab02012-11-30 10:53:22 -08002303void NuPlayer::processDeferredActions() {
2304 while (!mDeferredActions.empty()) {
2305 // We won't execute any deferred actions until we're no longer in
2306 // an intermediate state, i.e. one more more decoders are currently
2307 // flushing or shutting down.
2308
Andreas Hubera1f8ab02012-11-30 10:53:22 -08002309 if (mFlushingAudio != NONE || mFlushingVideo != NONE) {
2310 // We're currently flushing, postpone the reset until that's
2311 // completed.
2312
2313 ALOGV("postponing action mFlushingAudio=%d, mFlushingVideo=%d",
2314 mFlushingAudio, mFlushingVideo);
2315
2316 break;
2317 }
2318
2319 sp<Action> action = *mDeferredActions.begin();
2320 mDeferredActions.erase(mDeferredActions.begin());
2321
2322 action->execute(this);
2323 }
2324}
2325
Wei Jiac5de0912016-11-18 10:22:14 -08002326void NuPlayer::performSeek(int64_t seekTimeUs, MediaPlayerSeekMode mode) {
2327 ALOGV("performSeek seekTimeUs=%lld us (%.2f secs), mode=%d",
2328 (long long)seekTimeUs, seekTimeUs / 1E6, mode);
Andreas Hubera1f8ab02012-11-30 10:53:22 -08002329
Andy Hungadf34bf2014-09-03 18:22:22 -07002330 if (mSource == NULL) {
2331 // This happens when reset occurs right before the loop mode
2332 // asynchronously seeks to the start of the stream.
2333 LOG_ALWAYS_FATAL_IF(mAudioDecoder != NULL || mVideoDecoder != NULL,
2334 "mSource is NULL and decoders not NULL audio(%p) video(%p)",
2335 mAudioDecoder.get(), mVideoDecoder.get());
2336 return;
2337 }
Robert Shih1a5c8592015-08-04 18:07:44 -07002338 mPreviousSeekTimeUs = seekTimeUs;
Wei Jiac5de0912016-11-18 10:22:14 -08002339 mSource->seekTo(seekTimeUs, mode);
Robert Shihd3b0bbb2014-07-23 15:00:25 -07002340 ++mTimedTextGeneration;
Andreas Hubera1f8ab02012-11-30 10:53:22 -08002341
Andreas Hubera1f8ab02012-11-30 10:53:22 -08002342 // everything's flushed, continue playback.
2343}
2344
Wei Jiafef808d2014-10-31 17:57:05 -07002345void NuPlayer::performDecoderFlush(FlushCommand audio, FlushCommand video) {
2346 ALOGV("performDecoderFlush audio=%d, video=%d", audio, video);
Andreas Hubera1f8ab02012-11-30 10:53:22 -08002347
Wei Jiafef808d2014-10-31 17:57:05 -07002348 if ((audio == FLUSH_CMD_NONE || mAudioDecoder == NULL)
2349 && (video == FLUSH_CMD_NONE || mVideoDecoder == NULL)) {
Andreas Hubera1f8ab02012-11-30 10:53:22 -08002350 return;
2351 }
2352
Wei Jiafef808d2014-10-31 17:57:05 -07002353 if (audio != FLUSH_CMD_NONE && mAudioDecoder != NULL) {
2354 flushDecoder(true /* audio */, (audio == FLUSH_CMD_SHUTDOWN));
Andreas Hubera1f8ab02012-11-30 10:53:22 -08002355 }
2356
Wei Jiafef808d2014-10-31 17:57:05 -07002357 if (video != FLUSH_CMD_NONE && mVideoDecoder != NULL) {
2358 flushDecoder(false /* audio */, (video == FLUSH_CMD_SHUTDOWN));
Andreas Hubera1f8ab02012-11-30 10:53:22 -08002359 }
2360}
2361
2362void NuPlayer::performReset() {
2363 ALOGV("performReset");
2364
2365 CHECK(mAudioDecoder == NULL);
2366 CHECK(mVideoDecoder == NULL);
2367
Ray Essickeda32522018-02-28 12:08:28 -08002368 updatePlaybackTimer(true /* stopping */, "performReset");
2369 updateRebufferingTimer(true /* stopping */, true /* exiting */);
Ray Essick0d98c182017-11-09 13:42:42 -08002370
Andreas Hubera1f8ab02012-11-30 10:53:22 -08002371 cancelPollDuration();
2372
2373 ++mScanSourcesGeneration;
2374 mScanSourcesPending = false;
2375
Lajos Molnar09524832014-07-17 14:29:51 -07002376 if (mRendererLooper != NULL) {
2377 if (mRenderer != NULL) {
2378 mRendererLooper->unregisterHandler(mRenderer->id());
2379 }
2380 mRendererLooper->stop();
2381 mRendererLooper.clear();
2382 }
Andreas Hubera1f8ab02012-11-30 10:53:22 -08002383 mRenderer.clear();
Wei Jia57568df2014-09-22 10:16:29 -07002384 ++mRendererGeneration;
Andreas Hubera1f8ab02012-11-30 10:53:22 -08002385
2386 if (mSource != NULL) {
2387 mSource->stop();
Andreas Huberb5f25f02013-02-05 10:14:26 -08002388
Wei Jiac45a4b22016-04-15 15:30:23 -07002389 Mutex::Autolock autoLock(mSourceLock);
Andreas Hubera1f8ab02012-11-30 10:53:22 -08002390 mSource.clear();
2391 }
2392
2393 if (mDriver != NULL) {
2394 sp<NuPlayerDriver> driver = mDriver.promote();
2395 if (driver != NULL) {
2396 driver->notifyResetComplete();
2397 }
2398 }
Andreas Huber57a339c2012-12-03 11:18:00 -08002399
2400 mStarted = false;
Wei Jia8a092d32016-06-03 14:57:24 -07002401 mPrepared = false;
Ronghua Wu64c2d172015-10-07 16:52:19 -07002402 mResetting = false;
Robert Shih0c61a0d2015-07-06 15:09:10 -07002403 mSourceStarted = false;
Hassan Shojaniacefac142017-02-06 21:02:02 -08002404
2405 // Modular DRM
2406 if (mCrypto != NULL) {
2407 // decoders will be flushed before this so their mCrypto would go away on their own
2408 // TODO change to ALOGV
2409 ALOGD("performReset mCrypto: %p (%d)", mCrypto.get(),
2410 (mCrypto != NULL ? mCrypto->getStrongCount() : 0));
2411 mCrypto.clear();
2412 }
2413 mIsDrmProtected = false;
Andreas Hubera1f8ab02012-11-30 10:53:22 -08002414}
2415
2416void NuPlayer::performScanSources() {
2417 ALOGV("performScanSources");
2418
Andreas Huber57a339c2012-12-03 11:18:00 -08002419 if (!mStarted) {
2420 return;
2421 }
2422
Andreas Hubera1f8ab02012-11-30 10:53:22 -08002423 if (mAudioDecoder == NULL || mVideoDecoder == NULL) {
2424 postScanSources();
2425 }
2426}
2427
Lajos Molnar1de1e252015-04-30 18:18:34 -07002428void NuPlayer::performSetSurface(const sp<Surface> &surface) {
Andreas Huber57a339c2012-12-03 11:18:00 -08002429 ALOGV("performSetSurface");
2430
Lajos Molnar1de1e252015-04-30 18:18:34 -07002431 mSurface = surface;
Andreas Huber57a339c2012-12-03 11:18:00 -08002432
2433 // XXX - ignore error from setVideoScalingMode for now
2434 setVideoScalingMode(mVideoScalingMode);
Chong Zhang13d6faa2014-08-22 15:35:28 -07002435
2436 if (mDriver != NULL) {
2437 sp<NuPlayerDriver> driver = mDriver.promote();
2438 if (driver != NULL) {
2439 driver->notifySetSurfaceComplete();
2440 }
2441 }
Andreas Huber57a339c2012-12-03 11:18:00 -08002442}
2443
Chong Zhangf8d71772014-11-26 15:08:34 -08002444void NuPlayer::performResumeDecoders(bool needNotify) {
2445 if (needNotify) {
2446 mResumePending = true;
2447 if (mVideoDecoder == NULL) {
2448 // if audio-only, we can notify seek complete now,
2449 // as the resume operation will be relatively fast.
2450 finishResume();
2451 }
2452 }
2453
Chong Zhang7137ec72014-11-12 16:41:05 -08002454 if (mVideoDecoder != NULL) {
Chong Zhangf8d71772014-11-26 15:08:34 -08002455 // When there is continuous seek, MediaPlayer will cache the seek
2456 // position, and send down new seek request when previous seek is
2457 // complete. Let's wait for at least one video output frame before
2458 // notifying seek complete, so that the video thumbnail gets updated
2459 // when seekbar is dragged.
2460 mVideoDecoder->signalResume(needNotify);
Chong Zhang7137ec72014-11-12 16:41:05 -08002461 }
2462
2463 if (mAudioDecoder != NULL) {
Chong Zhangf8d71772014-11-26 15:08:34 -08002464 mAudioDecoder->signalResume(false /* needNotify */);
2465 }
2466}
2467
2468void NuPlayer::finishResume() {
2469 if (mResumePending) {
2470 mResumePending = false;
Wei Jia1061c9c2015-05-19 16:02:17 -07002471 notifyDriverSeekComplete();
2472 }
2473}
2474
2475void NuPlayer::notifyDriverSeekComplete() {
2476 if (mDriver != NULL) {
2477 sp<NuPlayerDriver> driver = mDriver.promote();
2478 if (driver != NULL) {
2479 driver->notifySeekComplete();
Chong Zhangf8d71772014-11-26 15:08:34 -08002480 }
Chong Zhang7137ec72014-11-12 16:41:05 -08002481 }
2482}
2483
Andreas Huber9575c962013-02-05 13:59:56 -08002484void NuPlayer::onSourceNotify(const sp<AMessage> &msg) {
2485 int32_t what;
2486 CHECK(msg->findInt32("what", &what));
2487
2488 switch (what) {
Lajos Molnarfcd3e942015-03-31 10:06:48 -07002489 case Source::kWhatInstantiateSecureDecoders:
2490 {
2491 if (mSource == NULL) {
2492 // This is a stale notification from a source that was
2493 // asynchronously preparing when the client called reset().
2494 // We handled the reset, the source is gone.
2495 break;
2496 }
2497
2498 sp<AMessage> reply;
2499 CHECK(msg->findMessage("reply", &reply));
2500 status_t err = onInstantiateSecureDecoders();
2501 reply->setInt32("err", err);
2502 reply->post();
2503 break;
2504 }
2505
Andreas Huber9575c962013-02-05 13:59:56 -08002506 case Source::kWhatPrepared:
2507 {
Hassan Shojaniacefac142017-02-06 21:02:02 -08002508 ALOGV("NuPlayer::onSourceNotify Source::kWhatPrepared source: %p", mSource.get());
Andreas Huberb5f28d42013-04-25 15:11:19 -07002509 if (mSource == NULL) {
2510 // This is a stale notification from a source that was
2511 // asynchronously preparing when the client called reset().
2512 // We handled the reset, the source is gone.
2513 break;
2514 }
2515
Andreas Huberec0c5972013-02-05 14:47:13 -08002516 int32_t err;
2517 CHECK(msg->findInt32("err", &err));
2518
Lajos Molnarfcd3e942015-03-31 10:06:48 -07002519 if (err != OK) {
2520 // shut down potential secure codecs in case client never calls reset
2521 mDeferredActions.push_back(
2522 new FlushDecoderAction(FLUSH_CMD_SHUTDOWN /* audio */,
2523 FLUSH_CMD_SHUTDOWN /* video */));
2524 processDeferredActions();
Wei Jia8a092d32016-06-03 14:57:24 -07002525 } else {
2526 mPrepared = true;
Lajos Molnarfcd3e942015-03-31 10:06:48 -07002527 }
2528
Andreas Huber9575c962013-02-05 13:59:56 -08002529 sp<NuPlayerDriver> driver = mDriver.promote();
2530 if (driver != NULL) {
Marco Nelissendd114d12014-05-28 15:23:14 -07002531 // notify duration first, so that it's definitely set when
2532 // the app received the "prepare complete" callback.
2533 int64_t durationUs;
2534 if (mSource->getDuration(&durationUs) == OK) {
2535 driver->notifyDuration(durationUs);
2536 }
Andreas Huberec0c5972013-02-05 14:47:13 -08002537 driver->notifyPrepareCompleted(err);
Andreas Huber9575c962013-02-05 13:59:56 -08002538 }
Andreas Huber99759402013-04-01 14:28:31 -07002539
Andreas Huber9575c962013-02-05 13:59:56 -08002540 break;
2541 }
2542
Hassan Shojaniacefac142017-02-06 21:02:02 -08002543 // Modular DRM
2544 case Source::kWhatDrmInfo:
2545 {
2546 Parcel parcel;
2547 sp<ABuffer> drmInfo;
2548 CHECK(msg->findBuffer("drmInfo", &drmInfo));
2549 parcel.setData(drmInfo->data(), drmInfo->size());
2550
2551 ALOGV("onSourceNotify() kWhatDrmInfo MEDIA_DRM_INFO drmInfo: %p parcel size: %zu",
2552 drmInfo.get(), parcel.dataSize());
2553
2554 notifyListener(MEDIA_DRM_INFO, 0 /* ext1 */, 0 /* ext2 */, &parcel);
2555
2556 break;
2557 }
2558
Andreas Huber9575c962013-02-05 13:59:56 -08002559 case Source::kWhatFlagsChanged:
2560 {
2561 uint32_t flags;
2562 CHECK(msg->findInt32("flags", (int32_t *)&flags));
2563
Chong Zhang4b7069d2013-09-11 12:52:43 -07002564 sp<NuPlayerDriver> driver = mDriver.promote();
2565 if (driver != NULL) {
Hassan Shojaniacefac142017-02-06 21:02:02 -08002566
2567 ALOGV("onSourceNotify() kWhatFlagsChanged FLAG_CAN_PAUSE: %d "
2568 "FLAG_CAN_SEEK_BACKWARD: %d \n\t\t\t\t FLAG_CAN_SEEK_FORWARD: %d "
2569 "FLAG_CAN_SEEK: %d FLAG_DYNAMIC_DURATION: %d \n"
2570 "\t\t\t\t FLAG_SECURE: %d FLAG_PROTECTED: %d",
2571 (flags & Source::FLAG_CAN_PAUSE) != 0,
2572 (flags & Source::FLAG_CAN_SEEK_BACKWARD) != 0,
2573 (flags & Source::FLAG_CAN_SEEK_FORWARD) != 0,
2574 (flags & Source::FLAG_CAN_SEEK) != 0,
2575 (flags & Source::FLAG_DYNAMIC_DURATION) != 0,
2576 (flags & Source::FLAG_SECURE) != 0,
2577 (flags & Source::FLAG_PROTECTED) != 0);
2578
Wei Jia895651b2014-12-10 17:31:52 -08002579 if ((flags & NuPlayer::Source::FLAG_CAN_SEEK) == 0) {
2580 driver->notifyListener(
2581 MEDIA_INFO, MEDIA_INFO_NOT_SEEKABLE, 0);
2582 }
Chong Zhang4b7069d2013-09-11 12:52:43 -07002583 driver->notifyFlagsChanged(flags);
2584 }
2585
Andreas Huber9575c962013-02-05 13:59:56 -08002586 if ((mSourceFlags & Source::FLAG_DYNAMIC_DURATION)
2587 && (!(flags & Source::FLAG_DYNAMIC_DURATION))) {
2588 cancelPollDuration();
2589 } else if (!(mSourceFlags & Source::FLAG_DYNAMIC_DURATION)
2590 && (flags & Source::FLAG_DYNAMIC_DURATION)
2591 && (mAudioDecoder != NULL || mVideoDecoder != NULL)) {
2592 schedulePollDuration();
2593 }
2594
2595 mSourceFlags = flags;
2596 break;
2597 }
2598
2599 case Source::kWhatVideoSizeChanged:
2600 {
Chong Zhangced1c2f2014-08-08 15:22:35 -07002601 sp<AMessage> format;
2602 CHECK(msg->findMessage("format", &format));
Andreas Huber9575c962013-02-05 13:59:56 -08002603
Chong Zhangced1c2f2014-08-08 15:22:35 -07002604 updateVideoSize(format);
Andreas Huber9575c962013-02-05 13:59:56 -08002605 break;
2606 }
2607
Chong Zhang2a3cc9a2014-08-21 17:48:26 -07002608 case Source::kWhatBufferingUpdate:
2609 {
2610 int32_t percentage;
2611 CHECK(msg->findInt32("percentage", &percentage));
2612
2613 notifyListener(MEDIA_BUFFERING_UPDATE, percentage, 0);
2614 break;
2615 }
2616
Chong Zhangefbb6192015-01-30 17:13:27 -08002617 case Source::kWhatPauseOnBufferingStart:
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 low, pausing...");
2622
Ray Essick58e0f7a2017-11-21 10:59:38 -08002623 startRebufferingTimer();
Chong Zhang8a048332015-05-06 15:16:28 -07002624 mPausedForBuffering = true;
Chong Zhangefbb6192015-01-30 17:13:27 -08002625 onPause();
2626 }
Wei Jiabfe82072016-05-20 09:21:50 -07002627 notifyListener(MEDIA_INFO, MEDIA_INFO_BUFFERING_START, 0);
Roger Jönssonb50e83e2013-01-21 16:26:41 +01002628 break;
2629 }
2630
Chong Zhangefbb6192015-01-30 17:13:27 -08002631 case Source::kWhatResumeOnBufferingEnd:
2632 {
2633 // ignore if not playing
Chong Zhang8a048332015-05-06 15:16:28 -07002634 if (mStarted) {
Chong Zhangefbb6192015-01-30 17:13:27 -08002635 ALOGI("buffer ready, resuming...");
2636
Ray Essickeda32522018-02-28 12:08:28 -08002637 updateRebufferingTimer(true /* stopping */, false /* exiting */);
Chong Zhang8a048332015-05-06 15:16:28 -07002638 mPausedForBuffering = false;
2639
2640 // do not resume yet if client didn't unpause
2641 if (!mPausedByClient) {
2642 onResume();
2643 }
Chong Zhangefbb6192015-01-30 17:13:27 -08002644 }
Wei Jia3bed45a2016-02-17 11:06:47 -08002645 notifyListener(MEDIA_INFO, MEDIA_INFO_BUFFERING_END, 0);
Roger Jönssonb50e83e2013-01-21 16:26:41 +01002646 break;
2647 }
2648
Chong Zhangefbb6192015-01-30 17:13:27 -08002649 case Source::kWhatCacheStats:
2650 {
2651 int32_t kbps;
2652 CHECK(msg->findInt32("bandwidth", &kbps));
2653
2654 notifyListener(MEDIA_INFO, MEDIA_INFO_NETWORK_BANDWIDTH, kbps);
2655 break;
2656 }
2657
Chong Zhangdcb89b32013-08-06 09:44:47 -07002658 case Source::kWhatSubtitleData:
2659 {
2660 sp<ABuffer> buffer;
2661 CHECK(msg->findBuffer("buffer", &buffer));
2662
Chong Zhang404fced2014-06-11 14:45:31 -07002663 sendSubtitleData(buffer, 0 /* baseIndex */);
Chong Zhangdcb89b32013-08-06 09:44:47 -07002664 break;
2665 }
2666
Robert Shih08528432015-04-08 09:06:54 -07002667 case Source::kWhatTimedMetaData:
2668 {
2669 sp<ABuffer> buffer;
2670 if (!msg->findBuffer("buffer", &buffer)) {
2671 notifyListener(MEDIA_INFO, MEDIA_INFO_METADATA_UPDATE, 0);
2672 } else {
2673 sendTimedMetaData(buffer);
2674 }
2675 break;
2676 }
2677
Robert Shihd3b0bbb2014-07-23 15:00:25 -07002678 case Source::kWhatTimedTextData:
2679 {
2680 int32_t generation;
2681 if (msg->findInt32("generation", &generation)
2682 && generation != mTimedTextGeneration) {
2683 break;
2684 }
2685
2686 sp<ABuffer> buffer;
2687 CHECK(msg->findBuffer("buffer", &buffer));
2688
2689 sp<NuPlayerDriver> driver = mDriver.promote();
2690 if (driver == NULL) {
2691 break;
2692 }
2693
2694 int posMs;
2695 int64_t timeUs, posUs;
2696 driver->getCurrentPosition(&posMs);
Chih-Hung Hsieh62309d52018-12-11 13:54:02 -08002697 posUs = (int64_t) posMs * 1000LL;
Robert Shihd3b0bbb2014-07-23 15:00:25 -07002698 CHECK(buffer->meta()->findInt64("timeUs", &timeUs));
2699
2700 if (posUs < timeUs) {
2701 if (!msg->findInt32("generation", &generation)) {
2702 msg->setInt32("generation", mTimedTextGeneration);
2703 }
2704 msg->post(timeUs - posUs);
2705 } else {
2706 sendTimedTextData(buffer);
2707 }
2708 break;
2709 }
2710
Andreas Huber14f76722013-01-15 09:04:18 -08002711 case Source::kWhatQueueDecoderShutdown:
2712 {
2713 int32_t audio, video;
2714 CHECK(msg->findInt32("audio", &audio));
2715 CHECK(msg->findInt32("video", &video));
2716
2717 sp<AMessage> reply;
2718 CHECK(msg->findMessage("reply", &reply));
2719
2720 queueDecoderShutdown(audio, video, reply);
2721 break;
2722 }
2723
Ronghua Wu80276872014-08-28 15:50:29 -07002724 case Source::kWhatDrmNoLicense:
2725 {
2726 notifyListener(MEDIA_ERROR, MEDIA_ERROR_UNKNOWN, ERROR_DRM_NO_LICENSE);
2727 break;
2728 }
2729
Kim Sungyeond3c6b322018-03-02 14:41:19 +09002730 case Source::kWhatIMSRxNotice:
2731 {
2732 sp<AMessage> IMSRxNotice;
2733 CHECK(msg->findMessage("message", &IMSRxNotice));
2734 sendIMSRxNotice(IMSRxNotice);
2735 break;
2736 }
2737
Andreas Huber9575c962013-02-05 13:59:56 -08002738 default:
2739 TRESPASS();
2740 }
2741}
2742
Chong Zhanga7fa1d92014-06-11 14:49:23 -07002743void NuPlayer::onClosedCaptionNotify(const sp<AMessage> &msg) {
2744 int32_t what;
2745 CHECK(msg->findInt32("what", &what));
2746
2747 switch (what) {
2748 case NuPlayer::CCDecoder::kWhatClosedCaptionData:
2749 {
2750 sp<ABuffer> buffer;
2751 CHECK(msg->findBuffer("buffer", &buffer));
2752
2753 size_t inbandTracks = 0;
2754 if (mSource != NULL) {
2755 inbandTracks = mSource->getTrackCount();
2756 }
2757
2758 sendSubtitleData(buffer, inbandTracks);
2759 break;
2760 }
2761
2762 case NuPlayer::CCDecoder::kWhatTrackAdded:
2763 {
2764 notifyListener(MEDIA_INFO, MEDIA_INFO_METADATA_UPDATE, 0);
2765
2766 break;
2767 }
2768
2769 default:
2770 TRESPASS();
2771 }
2772
2773
2774}
2775
Chong Zhang404fced2014-06-11 14:45:31 -07002776void NuPlayer::sendSubtitleData(const sp<ABuffer> &buffer, int32_t baseIndex) {
2777 int32_t trackIndex;
2778 int64_t timeUs, durationUs;
Robert Shihd83d4f42018-02-24 19:02:46 -08002779 CHECK(buffer->meta()->findInt32("track-index", &trackIndex));
Chong Zhang404fced2014-06-11 14:45:31 -07002780 CHECK(buffer->meta()->findInt64("timeUs", &timeUs));
2781 CHECK(buffer->meta()->findInt64("durationUs", &durationUs));
2782
2783 Parcel in;
2784 in.writeInt32(trackIndex + baseIndex);
2785 in.writeInt64(timeUs);
2786 in.writeInt64(durationUs);
2787 in.writeInt32(buffer->size());
2788 in.writeInt32(buffer->size());
2789 in.write(buffer->data(), buffer->size());
2790
2791 notifyListener(MEDIA_SUBTITLE_DATA, 0, 0, &in);
2792}
Robert Shihd3b0bbb2014-07-23 15:00:25 -07002793
Robert Shih08528432015-04-08 09:06:54 -07002794void NuPlayer::sendTimedMetaData(const sp<ABuffer> &buffer) {
2795 int64_t timeUs;
2796 CHECK(buffer->meta()->findInt64("timeUs", &timeUs));
2797
2798 Parcel in;
2799 in.writeInt64(timeUs);
2800 in.writeInt32(buffer->size());
2801 in.writeInt32(buffer->size());
2802 in.write(buffer->data(), buffer->size());
2803
2804 notifyListener(MEDIA_META_DATA, 0, 0, &in);
2805}
2806
Robert Shihd3b0bbb2014-07-23 15:00:25 -07002807void NuPlayer::sendTimedTextData(const sp<ABuffer> &buffer) {
2808 const void *data;
2809 size_t size = 0;
2810 int64_t timeUs;
Marco Nelissen55e2f4c2015-09-04 15:57:15 -07002811 int32_t flag = TextDescriptions::IN_BAND_TEXT_3GPP;
Robert Shihd3b0bbb2014-07-23 15:00:25 -07002812
2813 AString mime;
2814 CHECK(buffer->meta()->findString("mime", &mime));
2815 CHECK(strcasecmp(mime.c_str(), MEDIA_MIMETYPE_TEXT_3GPP) == 0);
2816
2817 data = buffer->data();
2818 size = buffer->size();
2819
2820 Parcel parcel;
2821 if (size > 0) {
2822 CHECK(buffer->meta()->findInt64("timeUs", &timeUs));
Marco Nelissen55e2f4c2015-09-04 15:57:15 -07002823 int32_t global = 0;
2824 if (buffer->meta()->findInt32("global", &global) && global) {
2825 flag |= TextDescriptions::GLOBAL_DESCRIPTIONS;
2826 } else {
2827 flag |= TextDescriptions::LOCAL_DESCRIPTIONS;
2828 }
Robert Shihd3b0bbb2014-07-23 15:00:25 -07002829 TextDescriptions::getParcelOfDescriptions(
2830 (const uint8_t *)data, size, flag, timeUs / 1000, &parcel);
2831 }
2832
2833 if ((parcel.dataSize() > 0)) {
2834 notifyListener(MEDIA_TIMED_TEXT, 0, 0, &parcel);
2835 } else { // send an empty timed text
2836 notifyListener(MEDIA_TIMED_TEXT, 0, 0);
2837 }
2838}
Hassan Shojaniacefac142017-02-06 21:02:02 -08002839
Kim Sungyeond3c6b322018-03-02 14:41:19 +09002840void NuPlayer::sendIMSRxNotice(const sp<AMessage> &msg) {
2841 int32_t notice, payloadType, feedbackType, id, bitrate;
2842
2843 CHECK(msg->findInt32("IMS-Rx-notice", &notice));
2844 CHECK(msg->findInt32("payload-type", &payloadType));
2845 CHECK(msg->findInt32("feedback-type", &feedbackType));
2846 CHECK(msg->findInt32("sender", &id));
2847
2848 Parcel in;
2849 in.writeInt32(payloadType);
2850 in.writeInt32(feedbackType);
2851 in.writeInt32(id);
2852
2853 switch (payloadType) {
2854 case 205: // TSFB
2855 {
2856 CHECK(msg->findInt32("bit-rate", &bitrate));
2857 in.writeInt32(bitrate);
2858 break;
2859 }
2860 case 206: // PSFB
2861 {
2862 // nothing to do yet
2863 break;
2864 }
2865 default:
2866 break;
2867 }
2868
2869 notifyListener(MEDIA_IMS_RX_NOTICE, 0, 0, &in);
2870}
2871
Hassan Shojaniaff63de72017-04-26 15:10:42 -07002872const char *NuPlayer::getDataSourceType() {
2873 switch (mDataSourceType) {
2874 case DATA_SOURCE_TYPE_HTTP_LIVE:
2875 return "HTTPLive";
2876
2877 case DATA_SOURCE_TYPE_RTSP:
2878 return "RTSP";
2879
2880 case DATA_SOURCE_TYPE_GENERIC_URL:
2881 return "GenURL";
2882
2883 case DATA_SOURCE_TYPE_GENERIC_FD:
2884 return "GenFD";
2885
2886 case DATA_SOURCE_TYPE_MEDIA:
2887 return "Media";
2888
2889 case DATA_SOURCE_TYPE_STREAM:
2890 return "Stream";
2891
2892 case DATA_SOURCE_TYPE_NONE:
2893 default:
2894 return "None";
2895 }
2896 }
2897
Hassan Shojaniacefac142017-02-06 21:02:02 -08002898// Modular DRM begin
2899status_t NuPlayer::prepareDrm(const uint8_t uuid[16], const Vector<uint8_t> &drmSessionId)
2900{
2901 ALOGV("prepareDrm ");
2902
2903 // Passing to the looper anyway; called in a pre-config prepared state so no race on mCrypto
2904 sp<AMessage> msg = new AMessage(kWhatPrepareDrm, this);
2905 // synchronous call so just passing the address but with local copies of "const" args
2906 uint8_t UUID[16];
2907 memcpy(UUID, uuid, sizeof(UUID));
2908 Vector<uint8_t> sessionId = drmSessionId;
2909 msg->setPointer("uuid", (void*)UUID);
2910 msg->setPointer("drmSessionId", (void*)&sessionId);
2911
2912 sp<AMessage> response;
2913 status_t status = msg->postAndAwaitResponse(&response);
2914
2915 if (status == OK && response != NULL) {
2916 CHECK(response->findInt32("status", &status));
2917 ALOGV("prepareDrm ret: %d ", status);
2918 } else {
2919 ALOGE("prepareDrm err: %d", status);
2920 }
2921
2922 return status;
2923}
2924
2925status_t NuPlayer::releaseDrm()
2926{
2927 ALOGV("releaseDrm ");
2928
2929 sp<AMessage> msg = new AMessage(kWhatReleaseDrm, this);
2930
2931 sp<AMessage> response;
2932 status_t status = msg->postAndAwaitResponse(&response);
2933
2934 if (status == OK && response != NULL) {
2935 CHECK(response->findInt32("status", &status));
2936 ALOGV("releaseDrm ret: %d ", status);
2937 } else {
2938 ALOGE("releaseDrm err: %d", status);
2939 }
2940
2941 return status;
2942}
2943
2944status_t NuPlayer::onPrepareDrm(const sp<AMessage> &msg)
2945{
2946 // TODO change to ALOGV
2947 ALOGD("onPrepareDrm ");
2948
2949 status_t status = INVALID_OPERATION;
2950 if (mSource == NULL) {
2951 ALOGE("onPrepareDrm: No source. onPrepareDrm failed with %d.", status);
2952 return status;
2953 }
2954
2955 uint8_t *uuid;
2956 Vector<uint8_t> *drmSessionId;
2957 CHECK(msg->findPointer("uuid", (void**)&uuid));
2958 CHECK(msg->findPointer("drmSessionId", (void**)&drmSessionId));
2959
2960 status = OK;
2961 sp<ICrypto> crypto = NULL;
2962
2963 status = mSource->prepareDrm(uuid, *drmSessionId, &crypto);
2964 if (crypto == NULL) {
2965 ALOGE("onPrepareDrm: mSource->prepareDrm failed. status: %d", status);
2966 return status;
2967 }
2968 ALOGV("onPrepareDrm: mSource->prepareDrm succeeded");
2969
2970 if (mCrypto != NULL) {
2971 ALOGE("onPrepareDrm: Unexpected. Already having mCrypto: %p (%d)",
2972 mCrypto.get(), mCrypto->getStrongCount());
2973 mCrypto.clear();
2974 }
2975
2976 mCrypto = crypto;
2977 mIsDrmProtected = true;
2978 // TODO change to ALOGV
2979 ALOGD("onPrepareDrm: mCrypto: %p (%d)", mCrypto.get(),
2980 (mCrypto != NULL ? mCrypto->getStrongCount() : 0));
2981
2982 return status;
2983}
2984
2985status_t NuPlayer::onReleaseDrm()
2986{
2987 // TODO change to ALOGV
2988 ALOGD("onReleaseDrm ");
2989
Hassan Shojania50b20c92017-02-16 18:28:58 -08002990 if (!mIsDrmProtected) {
2991 ALOGW("onReleaseDrm: Unexpected. mIsDrmProtected is already false.");
2992 }
2993
2994 mIsDrmProtected = false;
Hassan Shojaniacefac142017-02-06 21:02:02 -08002995
2996 status_t status;
2997 if (mCrypto != NULL) {
Hassan Shojania355e8472017-05-12 10:33:16 -07002998 // notifying the source first before removing crypto from codec
2999 if (mSource != NULL) {
3000 mSource->releaseDrm();
3001 }
3002
Hassan Shojaniacefac142017-02-06 21:02:02 -08003003 status=OK;
3004 // first making sure the codecs have released their crypto reference
3005 const sp<DecoderBase> &videoDecoder = getDecoder(false/*audio*/);
3006 if (videoDecoder != NULL) {
3007 status = videoDecoder->releaseCrypto();
3008 ALOGV("onReleaseDrm: video decoder ret: %d", status);
3009 }
3010
3011 const sp<DecoderBase> &audioDecoder = getDecoder(true/*audio*/);
3012 if (audioDecoder != NULL) {
3013 status_t status_audio = audioDecoder->releaseCrypto();
3014 if (status == OK) { // otherwise, returning the first error
3015 status = status_audio;
3016 }
3017 ALOGV("onReleaseDrm: audio decoder ret: %d", status_audio);
3018 }
3019
3020 // TODO change to ALOGV
3021 ALOGD("onReleaseDrm: mCrypto: %p (%d)", mCrypto.get(),
3022 (mCrypto != NULL ? mCrypto->getStrongCount() : 0));
3023 mCrypto.clear();
3024 } else { // mCrypto == NULL
3025 ALOGE("onReleaseDrm: Unexpected. There is no crypto.");
3026 status = INVALID_OPERATION;
3027 }
3028
3029 return status;
3030}
3031// Modular DRM end
Andreas Huberb5f25f02013-02-05 10:14:26 -08003032////////////////////////////////////////////////////////////////////////////////
3033
Chong Zhangced1c2f2014-08-08 15:22:35 -07003034sp<AMessage> NuPlayer::Source::getFormat(bool audio) {
3035 sp<MetaData> meta = getFormatMeta(audio);
3036
3037 if (meta == NULL) {
3038 return NULL;
3039 }
3040
3041 sp<AMessage> msg = new AMessage;
3042
3043 if(convertMetaDataToMessage(meta, &msg) == OK) {
3044 return msg;
3045 }
3046 return NULL;
3047}
3048
Andreas Huber9575c962013-02-05 13:59:56 -08003049void NuPlayer::Source::notifyFlagsChanged(uint32_t flags) {
3050 sp<AMessage> notify = dupNotify();
3051 notify->setInt32("what", kWhatFlagsChanged);
3052 notify->setInt32("flags", flags);
3053 notify->post();
3054}
3055
Chong Zhangced1c2f2014-08-08 15:22:35 -07003056void NuPlayer::Source::notifyVideoSizeChanged(const sp<AMessage> &format) {
Andreas Huber9575c962013-02-05 13:59:56 -08003057 sp<AMessage> notify = dupNotify();
3058 notify->setInt32("what", kWhatVideoSizeChanged);
Chong Zhangced1c2f2014-08-08 15:22:35 -07003059 notify->setMessage("format", format);
Andreas Huber9575c962013-02-05 13:59:56 -08003060 notify->post();
3061}
3062
Andreas Huberec0c5972013-02-05 14:47:13 -08003063void NuPlayer::Source::notifyPrepared(status_t err) {
Hassan Shojaniacefac142017-02-06 21:02:02 -08003064 ALOGV("Source::notifyPrepared %d", err);
Andreas Huber9575c962013-02-05 13:59:56 -08003065 sp<AMessage> notify = dupNotify();
3066 notify->setInt32("what", kWhatPrepared);
Andreas Huberec0c5972013-02-05 14:47:13 -08003067 notify->setInt32("err", err);
Andreas Huber9575c962013-02-05 13:59:56 -08003068 notify->post();
3069}
3070
Hassan Shojaniacefac142017-02-06 21:02:02 -08003071void NuPlayer::Source::notifyDrmInfo(const sp<ABuffer> &drmInfoBuffer)
3072{
3073 ALOGV("Source::notifyDrmInfo");
3074
3075 sp<AMessage> notify = dupNotify();
3076 notify->setInt32("what", kWhatDrmInfo);
3077 notify->setBuffer("drmInfo", drmInfoBuffer);
3078
3079 notify->post();
3080}
3081
Lajos Molnarfcd3e942015-03-31 10:06:48 -07003082void NuPlayer::Source::notifyInstantiateSecureDecoders(const sp<AMessage> &reply) {
3083 sp<AMessage> notify = dupNotify();
3084 notify->setInt32("what", kWhatInstantiateSecureDecoders);
3085 notify->setMessage("reply", reply);
3086 notify->post();
3087}
3088
Andreas Huber84333e02014-02-07 15:36:10 -08003089void NuPlayer::Source::onMessageReceived(const sp<AMessage> & /* msg */) {
Andreas Huberb5f25f02013-02-05 10:14:26 -08003090 TRESPASS();
3091}
3092
Andreas Huberf9334412010-12-15 15:17:42 -08003093} // namespace android