blob: d20599016434b0a95ecf8ca870df688c2d8684e1 [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"
Ray Essick64050722022-01-14 13:46:33 -080038#include <timedtext/TextDescriptions.h>
Andreas Huberf9334412010-12-15 15:17:42 -080039
Lajos Molnard9fd6312014-11-06 11:00:00 -080040#include <cutils/properties.h>
41
Lajos Molnar3a474aa2015-04-24 17:10:07 -070042#include <media/AudioResamplerPublic.h>
43#include <media/AVSyncSettings.h>
Wonsik Kim7e34bf52016-08-23 00:09:18 +090044#include <media/MediaCodecBuffer.h>
Lajos Molnar3a474aa2015-04-24 17:10:07 -070045
Andreas Huber3831a062010-12-21 10:22:33 -080046#include <media/stagefright/foundation/hexdump.h>
Andreas Huberf9334412010-12-15 15:17:42 -080047#include <media/stagefright/foundation/ABuffer.h>
48#include <media/stagefright/foundation/ADebug.h>
49#include <media/stagefright/foundation/AMessage.h>
Dongwon Kangd91dc5a2017-10-10 00:07:09 -070050#include <media/stagefright/foundation/avc_utils.h>
Lajos Molnar09524832014-07-17 14:29:51 -070051#include <media/stagefright/MediaBuffer.h>
Wei Jia0a68f662017-08-30 18:01:26 -070052#include <media/stagefright/MediaClock.h>
Andreas Huber3fe62152011-09-16 15:09:22 -070053#include <media/stagefright/MediaDefs.h>
Andreas Huberf9334412010-12-15 15:17:42 -080054#include <media/stagefright/MediaErrors.h>
55#include <media/stagefright/MetaData.h>
Lajos Molnar1de1e252015-04-30 18:18:34 -070056
Ray Essick64050722022-01-14 13:46:33 -080057#include <mpeg2ts/ATSParser.h>
58
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
Ray Essickb5ccf1b2022-09-08 13:19:39 -050063#include <media/esds/ESDS.h>
Andreas Huber84066782011-08-16 09:34:26 -070064#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();
Byeongjo Park5e27b1b2018-07-12 15:36:03 +0900381 mDataSourceType = DATA_SOURCE_TYPE_RTP;
Byeongjo Park28225ab2019-01-24 20:31:19 +0900382}
383
Andreas Huber9575c962013-02-05 13:59:56 -0800384void NuPlayer::prepareAsync() {
Hassan Shojaniacefac142017-02-06 21:02:02 -0800385 ALOGV("prepareAsync");
386
Lajos Molnar1d15ab52015-03-04 16:46:34 -0800387 (new AMessage(kWhatPrepare, this))->post();
Andreas Huber9575c962013-02-05 13:59:56 -0800388}
389
Andreas Huber57a339c2012-12-03 11:18:00 -0800390void NuPlayer::setVideoSurfaceTextureAsync(
Andy McFadden8ba01022012-12-18 09:46:54 -0800391 const sp<IGraphicBufferProducer> &bufferProducer) {
Lajos Molnar1de1e252015-04-30 18:18:34 -0700392 sp<AMessage> msg = new AMessage(kWhatSetVideoSurface, this);
Andreas Huber57a339c2012-12-03 11:18:00 -0800393
Andy McFadden8ba01022012-12-18 09:46:54 -0800394 if (bufferProducer == NULL) {
Lajos Molnar1de1e252015-04-30 18:18:34 -0700395 msg->setObject("surface", NULL);
Andreas Huber57a339c2012-12-03 11:18:00 -0800396 } else {
Lajos Molnar1de1e252015-04-30 18:18:34 -0700397 msg->setObject("surface", new Surface(bufferProducer, true /* controlledByApp */));
Andreas Huber57a339c2012-12-03 11:18:00 -0800398 }
399
Andreas Huberf9334412010-12-15 15:17:42 -0800400 msg->post();
401}
402
403void NuPlayer::setAudioSink(const sp<MediaPlayerBase::AudioSink> &sink) {
Lajos Molnar1d15ab52015-03-04 16:46:34 -0800404 sp<AMessage> msg = new AMessage(kWhatSetAudioSink, this);
Andreas Huberf9334412010-12-15 15:17:42 -0800405 msg->setObject("sink", sink);
406 msg->post();
407}
408
409void NuPlayer::start() {
Lajos Molnar1d15ab52015-03-04 16:46:34 -0800410 (new AMessage(kWhatStart, this))->post();
Andreas Huberf9334412010-12-15 15:17:42 -0800411}
412
Lajos Molnar3a474aa2015-04-24 17:10:07 -0700413status_t NuPlayer::setPlaybackSettings(const AudioPlaybackRate &rate) {
414 // do some cursory validation of the settings here. audio modes are
415 // only validated when set on the audiosink.
416 if ((rate.mSpeed != 0.f && rate.mSpeed < AUDIO_TIMESTRETCH_SPEED_MIN)
417 || rate.mSpeed > AUDIO_TIMESTRETCH_SPEED_MAX
418 || rate.mPitch < AUDIO_TIMESTRETCH_SPEED_MIN
419 || rate.mPitch > AUDIO_TIMESTRETCH_SPEED_MAX) {
420 return BAD_VALUE;
421 }
422 sp<AMessage> msg = new AMessage(kWhatConfigPlayback, this);
423 writeToAMessage(msg, rate);
424 sp<AMessage> response;
425 status_t err = msg->postAndAwaitResponse(&response);
426 if (err == OK && response != NULL) {
427 CHECK(response->findInt32("err", &err));
428 }
429 return err;
430}
431
432status_t NuPlayer::getPlaybackSettings(AudioPlaybackRate *rate /* nonnull */) {
433 sp<AMessage> msg = new AMessage(kWhatGetPlaybackSettings, this);
434 sp<AMessage> response;
435 status_t err = msg->postAndAwaitResponse(&response);
436 if (err == OK && response != NULL) {
437 CHECK(response->findInt32("err", &err));
438 if (err == OK) {
439 readFromAMessage(response, rate);
440 }
441 }
442 return err;
443}
444
445status_t NuPlayer::setSyncSettings(const AVSyncSettings &sync, float videoFpsHint) {
446 sp<AMessage> msg = new AMessage(kWhatConfigSync, this);
447 writeToAMessage(msg, sync, videoFpsHint);
448 sp<AMessage> response;
449 status_t err = msg->postAndAwaitResponse(&response);
450 if (err == OK && response != NULL) {
451 CHECK(response->findInt32("err", &err));
452 }
453 return err;
454}
455
456status_t NuPlayer::getSyncSettings(
457 AVSyncSettings *sync /* nonnull */, float *videoFps /* nonnull */) {
458 sp<AMessage> msg = new AMessage(kWhatGetSyncSettings, this);
459 sp<AMessage> response;
460 status_t err = msg->postAndAwaitResponse(&response);
461 if (err == OK && response != NULL) {
462 CHECK(response->findInt32("err", &err));
463 if (err == OK) {
464 readFromAMessage(response, sync, videoFps);
465 }
466 }
467 return err;
Wei Jia98160162015-02-04 17:01:11 -0800468}
469
Andreas Huber43c3e6c2011-01-05 12:17:08 -0800470void NuPlayer::pause() {
Lajos Molnar1d15ab52015-03-04 16:46:34 -0800471 (new AMessage(kWhatPause, this))->post();
Andreas Huber43c3e6c2011-01-05 12:17:08 -0800472}
473
Andreas Huber1aef2112011-01-04 14:01:29 -0800474void NuPlayer::resetAsync() {
Wei Jiac45a4b22016-04-15 15:30:23 -0700475 sp<Source> source;
476 {
477 Mutex::Autolock autoLock(mSourceLock);
478 source = mSource;
479 }
480
481 if (source != NULL) {
Chong Zhang48296b72014-09-14 14:28:45 -0700482 // During a reset, the data source might be unresponsive already, we need to
483 // disconnect explicitly so that reads exit promptly.
484 // We can't queue the disconnect request to the looper, as it might be
485 // queued behind a stuck read and never gets processed.
486 // Doing a disconnect outside the looper to allows the pending reads to exit
487 // (either successfully or with error).
Wei Jiac45a4b22016-04-15 15:30:23 -0700488 source->disconnect();
Chong Zhang48296b72014-09-14 14:28:45 -0700489 }
490
Lajos Molnar1d15ab52015-03-04 16:46:34 -0800491 (new AMessage(kWhatReset, this))->post();
Andreas Huber1aef2112011-01-04 14:01:29 -0800492}
493
Wei Jia52c28512017-09-13 18:17:51 -0700494status_t NuPlayer::notifyAt(int64_t mediaTimeUs) {
495 sp<AMessage> notify = new AMessage(kWhatNotifyTime, this);
496 notify->setInt64("timerUs", mediaTimeUs);
497 mMediaClock->addTimer(notify, mediaTimeUs);
498 return OK;
499}
500
Wei Jiac5de0912016-11-18 10:22:14 -0800501void NuPlayer::seekToAsync(int64_t seekTimeUs, MediaPlayerSeekMode mode, bool needNotify) {
Lajos Molnar1d15ab52015-03-04 16:46:34 -0800502 sp<AMessage> msg = new AMessage(kWhatSeek, this);
Andreas Huber43c3e6c2011-01-05 12:17:08 -0800503 msg->setInt64("seekTimeUs", seekTimeUs);
Wei Jiac5de0912016-11-18 10:22:14 -0800504 msg->setInt32("mode", mode);
Wei Jiae427abf2014-09-22 15:21:11 -0700505 msg->setInt32("needNotify", needNotify);
Andreas Huber43c3e6c2011-01-05 12:17:08 -0800506 msg->post();
507}
508
Andreas Huber53df1a42010-12-22 10:03:04 -0800509
Chong Zhang404fced2014-06-11 14:45:31 -0700510void NuPlayer::writeTrackInfo(
Chih-Hung Hsiehe964d4e2016-08-09 14:31:32 -0700511 Parcel* reply, const sp<AMessage>& format) const {
Marco Nelissenc367ca12015-09-15 09:51:59 -0700512 if (format == NULL) {
513 ALOGE("NULL format");
514 return;
515 }
Chong Zhang404fced2014-06-11 14:45:31 -0700516 int32_t trackType;
Marco Nelissen9436e482015-09-15 10:21:53 -0700517 if (!format->findInt32("type", &trackType)) {
Marco Nelissenc367ca12015-09-15 09:51:59 -0700518 ALOGE("no track type");
519 return;
520 }
Chong Zhang404fced2014-06-11 14:45:31 -0700521
Robert Shih755106e2015-04-30 14:36:45 -0700522 AString mime;
Robert Shih2e3a4252015-05-06 10:21:15 -0700523 if (!format->findString("mime", &mime)) {
524 // Java MediaPlayer only uses mimetype for subtitle and timedtext tracks.
525 // If we can't find the mimetype here it means that we wouldn't be needing
526 // the mimetype on the Java end. We still write a placeholder mime to keep the
527 // (de)serialization logic simple.
528 if (trackType == MEDIA_TRACK_TYPE_AUDIO) {
529 mime = "audio/";
530 } else if (trackType == MEDIA_TRACK_TYPE_VIDEO) {
531 mime = "video/";
532 } else {
Marco Nelissenc367ca12015-09-15 09:51:59 -0700533 ALOGE("unknown track type: %d", trackType);
534 return;
Robert Shih2e3a4252015-05-06 10:21:15 -0700535 }
536 }
Robert Shih755106e2015-04-30 14:36:45 -0700537
Chong Zhang404fced2014-06-11 14:45:31 -0700538 AString lang;
Marco Nelissen9436e482015-09-15 10:21:53 -0700539 if (!format->findString("language", &lang)) {
Marco Nelissenc367ca12015-09-15 09:51:59 -0700540 ALOGE("no language");
541 return;
542 }
Chong Zhang404fced2014-06-11 14:45:31 -0700543
544 reply->writeInt32(2); // write something non-zero
545 reply->writeInt32(trackType);
Robert Shih755106e2015-04-30 14:36:45 -0700546 reply->writeString16(String16(mime.c_str()));
Chong Zhang404fced2014-06-11 14:45:31 -0700547 reply->writeString16(String16(lang.c_str()));
548
549 if (trackType == MEDIA_TRACK_TYPE_SUBTITLE) {
Chong Zhang404fced2014-06-11 14:45:31 -0700550 int32_t isAuto, isDefault, isForced;
551 CHECK(format->findInt32("auto", &isAuto));
552 CHECK(format->findInt32("default", &isDefault));
553 CHECK(format->findInt32("forced", &isForced));
554
Chong Zhang404fced2014-06-11 14:45:31 -0700555 reply->writeInt32(isAuto);
556 reply->writeInt32(isDefault);
557 reply->writeInt32(isForced);
558 }
559}
560
Andreas Huberf9334412010-12-15 15:17:42 -0800561void NuPlayer::onMessageReceived(const sp<AMessage> &msg) {
562 switch (msg->what()) {
563 case kWhatSetDataSource:
564 {
Steve Block3856b092011-10-20 11:56:00 +0100565 ALOGV("kWhatSetDataSource");
Andreas Huberf9334412010-12-15 15:17:42 -0800566
567 CHECK(mSource == NULL);
568
Chong Zhang3de157d2014-08-05 20:54:44 -0700569 status_t err = OK;
Andreas Huber5bc087c2010-12-23 10:27:40 -0800570 sp<RefBase> obj;
571 CHECK(msg->findObject("source", &obj));
Chong Zhang3de157d2014-08-05 20:54:44 -0700572 if (obj != NULL) {
Wei Jiac45a4b22016-04-15 15:30:23 -0700573 Mutex::Autolock autoLock(mSourceLock);
Chong Zhang3de157d2014-08-05 20:54:44 -0700574 mSource = static_cast<Source *>(obj.get());
Chong Zhang3de157d2014-08-05 20:54:44 -0700575 } else {
576 err = UNKNOWN_ERROR;
577 }
Andreas Huber9575c962013-02-05 13:59:56 -0800578
579 CHECK(mDriver != NULL);
580 sp<NuPlayerDriver> driver = mDriver.promote();
581 if (driver != NULL) {
Chong Zhang3de157d2014-08-05 20:54:44 -0700582 driver->notifySetDataSourceCompleted(err);
Andreas Huber9575c962013-02-05 13:59:56 -0800583 }
584 break;
585 }
586
Wei Jia9bb38032017-03-23 18:00:38 -0700587 case kWhatGetBufferingSettings:
Wei Jia48fa06d2016-12-20 15:30:49 -0800588 {
589 sp<AReplyToken> replyID;
590 CHECK(msg->senderAwaitsResponse(&replyID));
591
Wei Jia9bb38032017-03-23 18:00:38 -0700592 ALOGV("kWhatGetBufferingSettings");
Wei Jia48fa06d2016-12-20 15:30:49 -0800593 BufferingSettings buffering;
594 status_t err = OK;
595 if (mSource != NULL) {
Wei Jia9bb38032017-03-23 18:00:38 -0700596 err = mSource->getBufferingSettings(&buffering);
Wei Jia48fa06d2016-12-20 15:30:49 -0800597 } else {
598 err = INVALID_OPERATION;
599 }
600 sp<AMessage> response = new AMessage;
601 if (err == OK) {
602 writeToAMessage(response, buffering);
603 }
604 response->setInt32("err", err);
605 response->postReply(replyID);
606 break;
607 }
608
609 case kWhatSetBufferingSettings:
610 {
611 sp<AReplyToken> replyID;
612 CHECK(msg->senderAwaitsResponse(&replyID));
613
614 ALOGV("kWhatSetBufferingSettings");
615 BufferingSettings buffering;
616 readFromAMessage(msg, &buffering);
617 status_t err = OK;
618 if (mSource != NULL) {
619 err = mSource->setBufferingSettings(buffering);
620 } else {
621 err = INVALID_OPERATION;
622 }
623 sp<AMessage> response = new AMessage;
624 response->setInt32("err", err);
625 response->postReply(replyID);
626 break;
627 }
628
Andreas Huber9575c962013-02-05 13:59:56 -0800629 case kWhatPrepare:
630 {
Hassan Shojaniacefac142017-02-06 21:02:02 -0800631 ALOGV("onMessageReceived kWhatPrepare");
632
Andreas Huber9575c962013-02-05 13:59:56 -0800633 mSource->prepareAsync();
Andreas Huberf9334412010-12-15 15:17:42 -0800634 break;
635 }
636
Chong Zhangdcb89b32013-08-06 09:44:47 -0700637 case kWhatGetTrackInfo:
638 {
Lajos Molnar3f274362015-03-05 14:35:41 -0800639 sp<AReplyToken> replyID;
Chong Zhangdcb89b32013-08-06 09:44:47 -0700640 CHECK(msg->senderAwaitsResponse(&replyID));
641
Chong Zhang404fced2014-06-11 14:45:31 -0700642 Parcel* reply;
643 CHECK(msg->findPointer("reply", (void**)&reply));
644
645 size_t inbandTracks = 0;
Chong Zhangdcb89b32013-08-06 09:44:47 -0700646 if (mSource != NULL) {
Chong Zhang404fced2014-06-11 14:45:31 -0700647 inbandTracks = mSource->getTrackCount();
648 }
649
Chong Zhanga7fa1d92014-06-11 14:49:23 -0700650 size_t ccTracks = 0;
651 if (mCCDecoder != NULL) {
652 ccTracks = mCCDecoder->getTrackCount();
653 }
654
Chong Zhang404fced2014-06-11 14:45:31 -0700655 // total track count
Chong Zhanga7fa1d92014-06-11 14:49:23 -0700656 reply->writeInt32(inbandTracks + ccTracks);
Chong Zhang404fced2014-06-11 14:45:31 -0700657
658 // write inband tracks
659 for (size_t i = 0; i < inbandTracks; ++i) {
660 writeTrackInfo(reply, mSource->getTrackInfo(i));
Chong Zhangdcb89b32013-08-06 09:44:47 -0700661 }
662
Chong Zhanga7fa1d92014-06-11 14:49:23 -0700663 // write CC track
664 for (size_t i = 0; i < ccTracks; ++i) {
665 writeTrackInfo(reply, mCCDecoder->getTrackInfo(i));
666 }
667
Chong Zhangdcb89b32013-08-06 09:44:47 -0700668 sp<AMessage> response = new AMessage;
Chong Zhangdcb89b32013-08-06 09:44:47 -0700669 response->postReply(replyID);
670 break;
671 }
672
Robert Shih7c4f0d72014-07-09 18:53:31 -0700673 case kWhatGetSelectedTrack:
674 {
Wei Jia039224f2018-11-29 17:25:17 -0800675 int32_t type32;
676 CHECK(msg->findInt32("type", (int32_t*)&type32));
677 media_track_type type = (media_track_type)type32;
678
679 size_t inbandTracks = 0;
Robert Shih7c4f0d72014-07-09 18:53:31 -0700680 status_t err = INVALID_OPERATION;
Wei Jia039224f2018-11-29 17:25:17 -0800681 ssize_t selectedTrack = -1;
Robert Shih7c4f0d72014-07-09 18:53:31 -0700682 if (mSource != NULL) {
683 err = OK;
Wei Jia039224f2018-11-29 17:25:17 -0800684 inbandTracks = mSource->getTrackCount();
685 selectedTrack = mSource->getSelectedTrack(type);
Robert Shih7c4f0d72014-07-09 18:53:31 -0700686 }
687
Wei Jia039224f2018-11-29 17:25:17 -0800688 if (selectedTrack == -1 && mCCDecoder != NULL) {
689 err = OK;
690 selectedTrack = mCCDecoder->getSelectedTrack(type);
691 if (selectedTrack != -1) {
692 selectedTrack += inbandTracks;
693 }
694 }
695
696 Parcel* reply;
697 CHECK(msg->findPointer("reply", (void**)&reply));
698 reply->writeInt32(selectedTrack);
699
Robert Shih7c4f0d72014-07-09 18:53:31 -0700700 sp<AMessage> response = new AMessage;
701 response->setInt32("err", err);
702
Lajos Molnar3f274362015-03-05 14:35:41 -0800703 sp<AReplyToken> replyID;
Robert Shih7c4f0d72014-07-09 18:53:31 -0700704 CHECK(msg->senderAwaitsResponse(&replyID));
705 response->postReply(replyID);
706 break;
707 }
708
Chong Zhangdcb89b32013-08-06 09:44:47 -0700709 case kWhatSelectTrack:
710 {
Lajos Molnar3f274362015-03-05 14:35:41 -0800711 sp<AReplyToken> replyID;
Chong Zhangdcb89b32013-08-06 09:44:47 -0700712 CHECK(msg->senderAwaitsResponse(&replyID));
713
Chong Zhang404fced2014-06-11 14:45:31 -0700714 size_t trackIndex;
715 int32_t select;
Robert Shih6ffb1fd2014-10-29 16:24:32 -0700716 int64_t timeUs;
Chong Zhang404fced2014-06-11 14:45:31 -0700717 CHECK(msg->findSize("trackIndex", &trackIndex));
718 CHECK(msg->findInt32("select", &select));
Robert Shih6ffb1fd2014-10-29 16:24:32 -0700719 CHECK(msg->findInt64("timeUs", &timeUs));
Chong Zhang404fced2014-06-11 14:45:31 -0700720
Chong Zhangdcb89b32013-08-06 09:44:47 -0700721 status_t err = INVALID_OPERATION;
Chong Zhang404fced2014-06-11 14:45:31 -0700722
723 size_t inbandTracks = 0;
Chong Zhangdcb89b32013-08-06 09:44:47 -0700724 if (mSource != NULL) {
Chong Zhang404fced2014-06-11 14:45:31 -0700725 inbandTracks = mSource->getTrackCount();
726 }
Chong Zhanga7fa1d92014-06-11 14:49:23 -0700727 size_t ccTracks = 0;
728 if (mCCDecoder != NULL) {
729 ccTracks = mCCDecoder->getTrackCount();
730 }
Chong Zhang404fced2014-06-11 14:45:31 -0700731
732 if (trackIndex < inbandTracks) {
Robert Shih6ffb1fd2014-10-29 16:24:32 -0700733 err = mSource->selectTrack(trackIndex, select, timeUs);
Robert Shihd3b0bbb2014-07-23 15:00:25 -0700734
735 if (!select && err == OK) {
736 int32_t type;
737 sp<AMessage> info = mSource->getTrackInfo(trackIndex);
738 if (info != NULL
739 && info->findInt32("type", &type)
740 && type == MEDIA_TRACK_TYPE_TIMEDTEXT) {
741 ++mTimedTextGeneration;
742 }
743 }
Chong Zhanga7fa1d92014-06-11 14:49:23 -0700744 } else {
745 trackIndex -= inbandTracks;
746
747 if (trackIndex < ccTracks) {
748 err = mCCDecoder->selectTrack(trackIndex, select);
749 }
Chong Zhangdcb89b32013-08-06 09:44:47 -0700750 }
751
752 sp<AMessage> response = new AMessage;
753 response->setInt32("err", err);
754
755 response->postReply(replyID);
756 break;
757 }
758
Andreas Huberb7c8e912012-11-27 15:02:53 -0800759 case kWhatPollDuration:
760 {
761 int32_t generation;
762 CHECK(msg->findInt32("generation", &generation));
763
764 if (generation != mPollDurationGeneration) {
765 // stale
766 break;
767 }
768
769 int64_t durationUs;
770 if (mDriver != NULL && mSource->getDuration(&durationUs) == OK) {
771 sp<NuPlayerDriver> driver = mDriver.promote();
772 if (driver != NULL) {
773 driver->notifyDuration(durationUs);
774 }
775 }
776
Chih-Hung Hsieh62309d52018-12-11 13:54:02 -0800777 msg->post(1000000LL); // poll again in a second.
Andreas Huberb7c8e912012-11-27 15:02:53 -0800778 break;
779 }
780
Lajos Molnar1de1e252015-04-30 18:18:34 -0700781 case kWhatSetVideoSurface:
Andreas Huberf9334412010-12-15 15:17:42 -0800782 {
Andreas Huberf9334412010-12-15 15:17:42 -0800783
784 sp<RefBase> obj;
Lajos Molnar1de1e252015-04-30 18:18:34 -0700785 CHECK(msg->findObject("surface", &obj));
786 sp<Surface> surface = static_cast<Surface *>(obj.get());
Lajos Molnara81c6222015-07-10 19:17:45 -0700787
788 ALOGD("onSetVideoSurface(%p, %s video decoder)",
789 surface.get(),
Wei Jia6b7d2ed2015-08-10 15:11:54 -0700790 (mSource != NULL && mStarted && mSource->getFormat(false /* audio */) != NULL
Lajos Molnara81c6222015-07-10 19:17:45 -0700791 && mVideoDecoder != NULL) ? "have" : "no");
792
Wei Jia6b7d2ed2015-08-10 15:11:54 -0700793 // Need to check mStarted before calling mSource->getFormat because NuPlayer might
794 // be in preparing state and it could take long time.
795 // When mStarted is true, mSource must have been set.
796 if (mSource == NULL || !mStarted || mSource->getFormat(false /* audio */) == NULL
Lajos Molnara81c6222015-07-10 19:17:45 -0700797 // NOTE: mVideoDecoder's mSurface is always non-null
798 || (mVideoDecoder != NULL && mVideoDecoder->setVideoSurface(surface) == OK)) {
Lajos Molnar1de1e252015-04-30 18:18:34 -0700799 performSetSurface(surface);
Wei Jiafef808d2014-10-31 17:57:05 -0700800 break;
801 }
802
803 mDeferredActions.push_back(
Krystian Turczyn687a5892016-01-20 14:20:35 +0100804 new FlushDecoderAction(
805 (obj != NULL ? FLUSH_CMD_FLUSH : FLUSH_CMD_NONE) /* audio */,
Wei Jiafef808d2014-10-31 17:57:05 -0700806 FLUSH_CMD_SHUTDOWN /* video */));
807
Lajos Molnar1de1e252015-04-30 18:18:34 -0700808 mDeferredActions.push_back(new SetSurfaceAction(surface));
James Dong0d268a32012-08-31 12:18:27 -0700809
Krystian Turczyn687a5892016-01-20 14:20:35 +0100810 if (obj != NULL) {
Wei Jiafef808d2014-10-31 17:57:05 -0700811 if (mStarted) {
Andy Hung73535852014-09-05 11:42:58 -0700812 // Issue a seek to refresh the video screen only if started otherwise
813 // the extractor may not yet be started and will assert.
814 // If the video decoder is not set (perhaps audio only in this case)
815 // do not perform a seek as it is not needed.
Ronghua Wua73d9e02014-10-08 15:13:29 -0700816 int64_t currentPositionUs = 0;
817 if (getCurrentPosition(&currentPositionUs) == OK) {
818 mDeferredActions.push_back(
Wei Jiac5de0912016-11-18 10:22:14 -0800819 new SeekAction(currentPositionUs,
820 MediaPlayerSeekMode::SEEK_PREVIOUS_SYNC /* mode */));
Ronghua Wua73d9e02014-10-08 15:13:29 -0700821 }
Andy Hung73535852014-09-05 11:42:58 -0700822 }
Wei Jiaac428aa2014-09-02 19:01:34 -0700823
Andreas Huber57a339c2012-12-03 11:18:00 -0800824 // If there is a new surface texture, instantiate decoders
825 // again if possible.
826 mDeferredActions.push_back(
827 new SimpleAction(&NuPlayer::performScanSources));
Andreas Huber57a339c2012-12-03 11:18:00 -0800828
Krystian Turczyn687a5892016-01-20 14:20:35 +0100829 // After a flush without shutdown, decoder is paused.
830 // Don't resume it until source seek is done, otherwise it could
831 // start pulling stale data too soon.
832 mDeferredActions.push_back(
833 new ResumeDecoderAction(false /* needNotify */));
834 }
Chong Zhang7137ec72014-11-12 16:41:05 -0800835
Andreas Huber57a339c2012-12-03 11:18:00 -0800836 processDeferredActions();
Andreas Huberf9334412010-12-15 15:17:42 -0800837 break;
838 }
839
840 case kWhatSetAudioSink:
841 {
Steve Block3856b092011-10-20 11:56:00 +0100842 ALOGV("kWhatSetAudioSink");
Andreas Huberf9334412010-12-15 15:17:42 -0800843
844 sp<RefBase> obj;
845 CHECK(msg->findObject("sink", &obj));
846
847 mAudioSink = static_cast<MediaPlayerBase::AudioSink *>(obj.get());
848 break;
849 }
850
851 case kWhatStart:
852 {
Steve Block3856b092011-10-20 11:56:00 +0100853 ALOGV("kWhatStart");
Wei Jia94211742014-10-28 17:09:06 -0700854 if (mStarted) {
Chong Zhang8a048332015-05-06 15:16:28 -0700855 // do not resume yet if the source is still buffering
856 if (!mPausedForBuffering) {
857 onResume();
858 }
Wei Jia94211742014-10-28 17:09:06 -0700859 } else {
860 onStart();
Lajos Molnar09524832014-07-17 14:29:51 -0700861 }
Chong Zhangefbb6192015-01-30 17:13:27 -0800862 mPausedByClient = false;
Andreas Huberf9334412010-12-15 15:17:42 -0800863 break;
864 }
865
Lajos Molnar3a474aa2015-04-24 17:10:07 -0700866 case kWhatConfigPlayback:
Wei Jia98160162015-02-04 17:01:11 -0800867 {
Lajos Molnar3a474aa2015-04-24 17:10:07 -0700868 sp<AReplyToken> replyID;
869 CHECK(msg->senderAwaitsResponse(&replyID));
870 AudioPlaybackRate rate /* sanitized */;
871 readFromAMessage(msg, &rate);
872 status_t err = OK;
Wei Jia98160162015-02-04 17:01:11 -0800873 if (mRenderer != NULL) {
Jaideep Sharma18a79122020-11-04 16:30:05 +0530874 // AudioSink allows only 1.f and 0.f for offload and direct modes.
875 // For other speeds, restart audio to fallback to supported paths
876 bool audioDirectOutput = (mAudioSink->getFlags() & AUDIO_OUTPUT_FLAG_DIRECT) != 0;
877 if ((mOffloadAudio || audioDirectOutput) &&
878 ((rate.mSpeed != 0.f && rate.mSpeed != 1.f) || rate.mPitch != 1.f)) {
879
Wei Jiabf70feb2016-02-19 15:47:16 -0800880 int64_t currentPositionUs;
881 if (getCurrentPosition(&currentPositionUs) != OK) {
882 currentPositionUs = mPreviousSeekTimeUs;
883 }
884
885 // Set mPlaybackSettings so that the new audio decoder can
886 // be created correctly.
887 mPlaybackSettings = rate;
888 if (!mPaused) {
889 mRenderer->pause();
890 }
Wei Jia5031b2f2016-02-25 11:19:31 -0800891 restartAudio(
Wei Jiabf70feb2016-02-19 15:47:16 -0800892 currentPositionUs, true /* forceNonOffload */,
893 true /* needsToCreateAudioDecoder */);
894 if (!mPaused) {
895 mRenderer->resume();
896 }
897 }
898
Lajos Molnar3a474aa2015-04-24 17:10:07 -0700899 err = mRenderer->setPlaybackSettings(rate);
900 }
901 if (err == OK) {
902 if (rate.mSpeed == 0.f) {
903 onPause();
Wei Jia351ce872016-02-10 13:21:59 -0800904 mPausedByClient = true;
Lajos Molnar3a474aa2015-04-24 17:10:07 -0700905 // save all other settings (using non-paused speed)
906 // so we can restore them on start
907 AudioPlaybackRate newRate = rate;
908 newRate.mSpeed = mPlaybackSettings.mSpeed;
909 mPlaybackSettings = newRate;
910 } else { /* rate.mSpeed != 0.f */
Lajos Molnar3a474aa2015-04-24 17:10:07 -0700911 mPlaybackSettings = rate;
Wei Jia8a092d32016-06-03 14:57:24 -0700912 if (mStarted) {
913 // do not resume yet if the source is still buffering
914 if (!mPausedForBuffering) {
915 onResume();
916 }
917 } else if (mPrepared) {
918 onStart();
919 }
920
921 mPausedByClient = false;
Lajos Molnar3a474aa2015-04-24 17:10:07 -0700922 }
Wei Jia98160162015-02-04 17:01:11 -0800923 }
Ronghua Wu8db88132015-04-22 13:51:35 -0700924
925 if (mVideoDecoder != NULL) {
Praveen Chavanbbaa1442016-04-08 13:33:49 -0700926 sp<AMessage> params = new AMessage();
927 params->setFloat("playback-speed", mPlaybackSettings.mSpeed);
928 mVideoDecoder->setParameters(params);
Ronghua Wu8db88132015-04-22 13:51:35 -0700929 }
930
Lajos Molnar3a474aa2015-04-24 17:10:07 -0700931 sp<AMessage> response = new AMessage;
932 response->setInt32("err", err);
933 response->postReply(replyID);
934 break;
935 }
936
937 case kWhatGetPlaybackSettings:
938 {
939 sp<AReplyToken> replyID;
940 CHECK(msg->senderAwaitsResponse(&replyID));
941 AudioPlaybackRate rate = mPlaybackSettings;
942 status_t err = OK;
943 if (mRenderer != NULL) {
944 err = mRenderer->getPlaybackSettings(&rate);
945 }
946 if (err == OK) {
947 // get playback settings used by renderer, as it may be
948 // slightly off due to audiosink not taking small changes.
949 mPlaybackSettings = rate;
950 if (mPaused) {
951 rate.mSpeed = 0.f;
952 }
953 }
954 sp<AMessage> response = new AMessage;
955 if (err == OK) {
956 writeToAMessage(response, rate);
957 }
958 response->setInt32("err", err);
959 response->postReply(replyID);
960 break;
961 }
962
963 case kWhatConfigSync:
964 {
965 sp<AReplyToken> replyID;
966 CHECK(msg->senderAwaitsResponse(&replyID));
967
968 ALOGV("kWhatConfigSync");
969 AVSyncSettings sync;
970 float videoFpsHint;
971 readFromAMessage(msg, &sync, &videoFpsHint);
972 status_t err = OK;
973 if (mRenderer != NULL) {
974 err = mRenderer->setSyncSettings(sync, videoFpsHint);
975 }
976 if (err == OK) {
977 mSyncSettings = sync;
978 mVideoFpsHint = videoFpsHint;
979 }
980 sp<AMessage> response = new AMessage;
981 response->setInt32("err", err);
982 response->postReply(replyID);
983 break;
984 }
985
986 case kWhatGetSyncSettings:
987 {
988 sp<AReplyToken> replyID;
989 CHECK(msg->senderAwaitsResponse(&replyID));
990 AVSyncSettings sync = mSyncSettings;
991 float videoFps = mVideoFpsHint;
992 status_t err = OK;
993 if (mRenderer != NULL) {
994 err = mRenderer->getSyncSettings(&sync, &videoFps);
995 if (err == OK) {
996 mSyncSettings = sync;
997 mVideoFpsHint = videoFps;
998 }
999 }
1000 sp<AMessage> response = new AMessage;
1001 if (err == OK) {
1002 writeToAMessage(response, sync, videoFps);
1003 }
1004 response->setInt32("err", err);
1005 response->postReply(replyID);
Wei Jia98160162015-02-04 17:01:11 -08001006 break;
1007 }
1008
Andreas Huberf9334412010-12-15 15:17:42 -08001009 case kWhatScanSources:
1010 {
Andreas Huber1aef2112011-01-04 14:01:29 -08001011 int32_t generation;
1012 CHECK(msg->findInt32("generation", &generation));
1013 if (generation != mScanSourcesGeneration) {
1014 // Drop obsolete msg.
1015 break;
1016 }
1017
Andreas Huber5bc087c2010-12-23 10:27:40 -08001018 mScanSourcesPending = false;
1019
Steve Block3856b092011-10-20 11:56:00 +01001020 ALOGV("scanning sources haveAudio=%d, haveVideo=%d",
Andreas Huber43c3e6c2011-01-05 12:17:08 -08001021 mAudioDecoder != NULL, mVideoDecoder != NULL);
1022
Andreas Huberb7c8e912012-11-27 15:02:53 -08001023 bool mHadAnySourcesBefore =
1024 (mAudioDecoder != NULL) || (mVideoDecoder != NULL);
Robert Shih7350b052015-10-01 15:50:14 -07001025 bool rescan = false;
Andreas Huberb7c8e912012-11-27 15:02:53 -08001026
Andy Hung282a7e32014-08-14 15:56:34 -07001027 // initialize video before audio because successful initialization of
1028 // video may change deep buffer mode of audio.
Lajos Molnar1de1e252015-04-30 18:18:34 -07001029 if (mSurface != NULL) {
Robert Shih7350b052015-10-01 15:50:14 -07001030 if (instantiateDecoder(false, &mVideoDecoder) == -EWOULDBLOCK) {
1031 rescan = true;
1032 }
Haynes Mathew George5d246ef2012-07-09 10:36:57 -07001033 }
Andreas Huberf9334412010-12-15 15:17:42 -08001034
Ronghua Wua10fd232014-11-06 16:15:20 -08001035 // Don't try to re-open audio sink if there's an existing decoder.
1036 if (mAudioSink != NULL && mAudioDecoder == NULL) {
Robert Shih7350b052015-10-01 15:50:14 -07001037 if (instantiateDecoder(true, &mAudioDecoder) == -EWOULDBLOCK) {
1038 rescan = true;
1039 }
Andreas Huberf9334412010-12-15 15:17:42 -08001040 }
1041
Andreas Huberb7c8e912012-11-27 15:02:53 -08001042 if (!mHadAnySourcesBefore
1043 && (mAudioDecoder != NULL || mVideoDecoder != NULL)) {
1044 // This is the first time we've found anything playable.
1045
Andreas Huber9575c962013-02-05 13:59:56 -08001046 if (mSourceFlags & Source::FLAG_DYNAMIC_DURATION) {
Andreas Huberb7c8e912012-11-27 15:02:53 -08001047 schedulePollDuration();
1048 }
1049 }
1050
Andreas Hubereac68ba2011-09-27 12:12:25 -07001051 status_t err;
1052 if ((err = mSource->feedMoreTSData()) != OK) {
Andreas Huber1aef2112011-01-04 14:01:29 -08001053 if (mAudioDecoder == NULL && mVideoDecoder == NULL) {
1054 // We're not currently decoding anything (no audio or
1055 // video tracks found) and we just ran out of input data.
Andreas Hubereac68ba2011-09-27 12:12:25 -07001056
1057 if (err == ERROR_END_OF_STREAM) {
1058 notifyListener(MEDIA_PLAYBACK_COMPLETE, 0, 0);
1059 } else {
1060 notifyListener(MEDIA_ERROR, MEDIA_ERROR_UNKNOWN, err);
1061 }
Andreas Huber1aef2112011-01-04 14:01:29 -08001062 }
Andreas Huberf9334412010-12-15 15:17:42 -08001063 break;
1064 }
1065
Robert Shih7350b052015-10-01 15:50:14 -07001066 if (rescan) {
Chih-Hung Hsieh62309d52018-12-11 13:54:02 -08001067 msg->post(100000LL);
Andreas Huber5bc087c2010-12-23 10:27:40 -08001068 mScanSourcesPending = true;
Andreas Huberf9334412010-12-15 15:17:42 -08001069 }
1070 break;
1071 }
1072
1073 case kWhatVideoNotify:
1074 case kWhatAudioNotify:
1075 {
1076 bool audio = msg->what() == kWhatAudioNotify;
1077
Wei Jia88703c32014-08-06 11:24:07 -07001078 int32_t currentDecoderGeneration =
1079 (audio? mAudioDecoderGeneration : mVideoDecoderGeneration);
1080 int32_t requesterGeneration = currentDecoderGeneration - 1;
1081 CHECK(msg->findInt32("generation", &requesterGeneration));
1082
1083 if (requesterGeneration != currentDecoderGeneration) {
1084 ALOGV("got message from old %s decoder, generation(%d:%d)",
1085 audio ? "audio" : "video", requesterGeneration,
1086 currentDecoderGeneration);
1087 sp<AMessage> reply;
1088 if (!(msg->findMessage("reply", &reply))) {
1089 return;
1090 }
1091
1092 reply->setInt32("err", INFO_DISCONTINUITY);
1093 reply->post();
1094 return;
1095 }
1096
Andreas Huberf9334412010-12-15 15:17:42 -08001097 int32_t what;
Lajos Molnar1cd13982014-01-17 15:12:51 -08001098 CHECK(msg->findInt32("what", &what));
Andreas Huberf9334412010-12-15 15:17:42 -08001099
Chong Zhang7137ec72014-11-12 16:41:05 -08001100 if (what == DecoderBase::kWhatInputDiscontinuity) {
1101 int32_t formatChange;
1102 CHECK(msg->findInt32("formatChange", &formatChange));
Andreas Huberf9334412010-12-15 15:17:42 -08001103
Chong Zhang7137ec72014-11-12 16:41:05 -08001104 ALOGV("%s discontinuity: formatChange %d",
1105 audio ? "audio" : "video", formatChange);
1106
1107 if (formatChange) {
1108 mDeferredActions.push_back(
1109 new FlushDecoderAction(
1110 audio ? FLUSH_CMD_SHUTDOWN : FLUSH_CMD_NONE,
1111 audio ? FLUSH_CMD_NONE : FLUSH_CMD_SHUTDOWN));
Andreas Huberf9334412010-12-15 15:17:42 -08001112 }
Chong Zhang7137ec72014-11-12 16:41:05 -08001113
1114 mDeferredActions.push_back(
1115 new SimpleAction(
1116 &NuPlayer::performScanSources));
1117
1118 processDeferredActions();
1119 } else if (what == DecoderBase::kWhatEOS) {
Andreas Huberdc9bacd2011-09-26 10:53:29 -07001120 int32_t err;
Lajos Molnar1cd13982014-01-17 15:12:51 -08001121 CHECK(msg->findInt32("err", &err));
Andreas Huberdc9bacd2011-09-26 10:53:29 -07001122
1123 if (err == ERROR_END_OF_STREAM) {
Steve Block3856b092011-10-20 11:56:00 +01001124 ALOGV("got %s decoder EOS", audio ? "audio" : "video");
Andreas Huberdc9bacd2011-09-26 10:53:29 -07001125 } else {
Steve Block3856b092011-10-20 11:56:00 +01001126 ALOGV("got %s decoder EOS w/ error %d",
Andreas Huberdc9bacd2011-09-26 10:53:29 -07001127 audio ? "audio" : "video",
1128 err);
1129 }
1130
1131 mRenderer->queueEOS(audio, err);
Chong Zhang7137ec72014-11-12 16:41:05 -08001132 } else if (what == DecoderBase::kWhatFlushCompleted) {
Steve Block3856b092011-10-20 11:56:00 +01001133 ALOGV("decoder %s flush completed", audio ? "audio" : "video");
Andreas Huberf9334412010-12-15 15:17:42 -08001134
Andy Hung8d121d42014-10-03 09:53:53 -07001135 handleFlushComplete(audio, true /* isDecoder */);
Andreas Huber3831a062010-12-21 10:22:33 -08001136 finishFlushIfPossible();
Chong Zhang7137ec72014-11-12 16:41:05 -08001137 } else if (what == DecoderBase::kWhatVideoSizeChanged) {
Lajos Molnar1cd13982014-01-17 15:12:51 -08001138 sp<AMessage> format;
1139 CHECK(msg->findMessage("format", &format));
1140
Wei Jiac6cfd702014-11-11 16:33:20 -08001141 sp<AMessage> inputFormat =
1142 mSource->getFormat(false /* audio */);
Andreas Huber3831a062010-12-21 10:22:33 -08001143
Bartosz Dolewski26936f72014-12-11 12:47:08 +01001144 setVideoScalingMode(mVideoScalingMode);
Wei Jiac6cfd702014-11-11 16:33:20 -08001145 updateVideoSize(inputFormat, format);
Chong Zhang7137ec72014-11-12 16:41:05 -08001146 } else if (what == DecoderBase::kWhatShutdownCompleted) {
Steve Block3856b092011-10-20 11:56:00 +01001147 ALOGV("%s shutdown completed", audio ? "audio" : "video");
Andreas Huber3831a062010-12-21 10:22:33 -08001148 if (audio) {
Ray Essickfcb8fd32018-08-07 09:39:38 -07001149 Mutex::Autolock autoLock(mDecoderLock);
Andreas Huber3831a062010-12-21 10:22:33 -08001150 mAudioDecoder.clear();
Wei Jia686e8e52017-04-03 14:08:01 -07001151 mAudioDecoderError = false;
Wei Jia57568df2014-09-22 10:16:29 -07001152 ++mAudioDecoderGeneration;
Andreas Huber3831a062010-12-21 10:22:33 -08001153
1154 CHECK_EQ((int)mFlushingAudio, (int)SHUTTING_DOWN_DECODER);
1155 mFlushingAudio = SHUT_DOWN;
1156 } else {
Ray Essickfcb8fd32018-08-07 09:39:38 -07001157 Mutex::Autolock autoLock(mDecoderLock);
Andreas Huber3831a062010-12-21 10:22:33 -08001158 mVideoDecoder.clear();
Wei Jia686e8e52017-04-03 14:08:01 -07001159 mVideoDecoderError = false;
Wei Jia57568df2014-09-22 10:16:29 -07001160 ++mVideoDecoderGeneration;
Andreas Huber3831a062010-12-21 10:22:33 -08001161
1162 CHECK_EQ((int)mFlushingVideo, (int)SHUTTING_DOWN_DECODER);
1163 mFlushingVideo = SHUT_DOWN;
1164 }
1165
1166 finishFlushIfPossible();
Chong Zhangf8d71772014-11-26 15:08:34 -08001167 } else if (what == DecoderBase::kWhatResumeCompleted) {
1168 finishResume();
Chong Zhang7137ec72014-11-12 16:41:05 -08001169 } else if (what == DecoderBase::kWhatError) {
Chong Zhangf4c0a942014-08-11 15:14:10 -07001170 status_t err;
Andy Hung2abde2c2014-09-30 14:40:32 -07001171 if (!msg->findInt32("err", &err) || err == OK) {
Chong Zhangf4c0a942014-08-11 15:14:10 -07001172 err = UNKNOWN_ERROR;
1173 }
Andy Hungcf31f1e2014-09-23 14:59:01 -07001174
Andy Hung2abde2c2014-09-30 14:40:32 -07001175 // Decoder errors can be due to Source (e.g. from streaming),
1176 // or from decoding corrupted bitstreams, or from other decoder
1177 // MediaCodec operations (e.g. from an ongoing reset or seek).
Andy Hung202bce12014-12-03 11:47:36 -08001178 // They may also be due to openAudioSink failure at
1179 // decoder start or after a format change.
Andy Hung2abde2c2014-09-30 14:40:32 -07001180 //
1181 // We try to gracefully shut down the affected decoder if possible,
1182 // rather than trying to force the shutdown with something
1183 // similar to performReset(). This method can lead to a hang
1184 // if MediaCodec functions block after an error, but they should
1185 // typically return INVALID_OPERATION instead of blocking.
1186
1187 FlushStatus *flushing = audio ? &mFlushingAudio : &mFlushingVideo;
1188 ALOGE("received error(%#x) from %s decoder, flushing(%d), now shutting down",
1189 err, audio ? "audio" : "video", *flushing);
1190
1191 switch (*flushing) {
1192 case NONE:
1193 mDeferredActions.push_back(
Wei Jiafef808d2014-10-31 17:57:05 -07001194 new FlushDecoderAction(
1195 audio ? FLUSH_CMD_SHUTDOWN : FLUSH_CMD_NONE,
1196 audio ? FLUSH_CMD_NONE : FLUSH_CMD_SHUTDOWN));
Andy Hung2abde2c2014-09-30 14:40:32 -07001197 processDeferredActions();
1198 break;
1199 case FLUSHING_DECODER:
1200 *flushing = FLUSHING_DECODER_SHUTDOWN; // initiate shutdown after flush.
1201 break; // Wait for flush to complete.
1202 case FLUSHING_DECODER_SHUTDOWN:
1203 break; // Wait for flush to complete.
1204 case SHUTTING_DOWN_DECODER:
1205 break; // Wait for shutdown to complete.
1206 case FLUSHED:
Andy Hung2abde2c2014-09-30 14:40:32 -07001207 getDecoder(audio)->initiateShutdown(); // In the middle of a seek.
1208 *flushing = SHUTTING_DOWN_DECODER; // Shut down.
1209 break;
1210 case SHUT_DOWN:
1211 finishFlushIfPossible(); // Should not occur.
1212 break; // Finish anyways.
Marco Nelissen9e2b7912014-08-18 16:13:03 -07001213 }
Wei Jia686e8e52017-04-03 14:08:01 -07001214 if (mSource != nullptr) {
1215 if (audio) {
Wei Jia2f6d8c52017-04-11 09:50:31 -07001216 if (mVideoDecoderError || mSource->getFormat(false /* audio */) == NULL
Wei Jiaf86731d2017-07-11 17:24:34 -07001217 || mSurface == NULL || mVideoDecoder == NULL) {
Wei Jia686e8e52017-04-03 14:08:01 -07001218 // When both audio and video have error, or this stream has only audio
1219 // which has error, notify client of error.
1220 notifyListener(MEDIA_ERROR, MEDIA_ERROR_UNKNOWN, err);
1221 } else {
1222 // Only audio track has error. Video track could be still good to play.
Jonathan Kim2a012642023-01-04 11:14:05 +09001223 if (mVideoEOS) {
1224 notifyListener(MEDIA_PLAYBACK_COMPLETE, 0, 0);
1225 } else {
1226 notifyListener(MEDIA_INFO, MEDIA_INFO_PLAY_AUDIO_ERROR, err);
1227 }
Wei Jia686e8e52017-04-03 14:08:01 -07001228 }
1229 mAudioDecoderError = true;
1230 } else {
Wei Jia2f6d8c52017-04-11 09:50:31 -07001231 if (mAudioDecoderError || mSource->getFormat(true /* audio */) == NULL
Wei Jiaf86731d2017-07-11 17:24:34 -07001232 || mAudioSink == NULL || mAudioDecoder == NULL) {
Wei Jia686e8e52017-04-03 14:08:01 -07001233 // When both audio and video have error, or this stream has only video
1234 // which has error, notify client of error.
1235 notifyListener(MEDIA_ERROR, MEDIA_ERROR_UNKNOWN, err);
1236 } else {
1237 // Only video track has error. Audio track could be still good to play.
Jonathan Kim2a012642023-01-04 11:14:05 +09001238 if (mAudioEOS) {
1239 notifyListener(MEDIA_PLAYBACK_COMPLETE, 0, 0);
1240 } else {
1241 notifyListener(MEDIA_INFO, MEDIA_INFO_PLAY_VIDEO_ERROR, err);
1242 }
Wei Jia686e8e52017-04-03 14:08:01 -07001243 }
1244 mVideoDecoderError = true;
1245 }
1246 }
Lajos Molnar1cd13982014-01-17 15:12:51 -08001247 } else {
1248 ALOGV("Unhandled decoder notification %d '%c%c%c%c'.",
Andreas Hubera1f8ab02012-11-30 10:53:22 -08001249 what,
1250 what >> 24,
1251 (what >> 16) & 0xff,
1252 (what >> 8) & 0xff,
1253 what & 0xff);
Andreas Huberf9334412010-12-15 15:17:42 -08001254 }
1255
1256 break;
1257 }
1258
1259 case kWhatRendererNotify:
1260 {
Wei Jia57568df2014-09-22 10:16:29 -07001261 int32_t requesterGeneration = mRendererGeneration - 1;
1262 CHECK(msg->findInt32("generation", &requesterGeneration));
1263 if (requesterGeneration != mRendererGeneration) {
1264 ALOGV("got message from old renderer, generation(%d:%d)",
1265 requesterGeneration, mRendererGeneration);
1266 return;
1267 }
1268
Andreas Huberf9334412010-12-15 15:17:42 -08001269 int32_t what;
1270 CHECK(msg->findInt32("what", &what));
1271
1272 if (what == Renderer::kWhatEOS) {
1273 int32_t audio;
1274 CHECK(msg->findInt32("audio", &audio));
1275
Andreas Huberc92fd242011-08-16 13:48:44 -07001276 int32_t finalResult;
1277 CHECK(msg->findInt32("finalResult", &finalResult));
1278
Andreas Huberf9334412010-12-15 15:17:42 -08001279 if (audio) {
1280 mAudioEOS = true;
1281 } else {
1282 mVideoEOS = true;
1283 }
1284
Andreas Huberc92fd242011-08-16 13:48:44 -07001285 if (finalResult == ERROR_END_OF_STREAM) {
Steve Block3856b092011-10-20 11:56:00 +01001286 ALOGV("reached %s EOS", audio ? "audio" : "video");
Andreas Huberc92fd242011-08-16 13:48:44 -07001287 } else {
Steve Block29357bc2012-01-06 19:20:56 +00001288 ALOGE("%s track encountered an error (%d)",
Andreas Huberc92fd242011-08-16 13:48:44 -07001289 audio ? "audio" : "video", finalResult);
1290
1291 notifyListener(
1292 MEDIA_ERROR, MEDIA_ERROR_UNKNOWN, finalResult);
1293 }
Andreas Huberf9334412010-12-15 15:17:42 -08001294
1295 if ((mAudioEOS || mAudioDecoder == NULL)
1296 && (mVideoEOS || mVideoDecoder == NULL)) {
1297 notifyListener(MEDIA_PLAYBACK_COMPLETE, 0, 0);
1298 }
Andreas Huber3fe62152011-09-16 15:09:22 -07001299 } else if (what == Renderer::kWhatFlushComplete) {
Andreas Huberf9334412010-12-15 15:17:42 -08001300 int32_t audio;
1301 CHECK(msg->findInt32("audio", &audio));
1302
Wei Jia3261f0d2015-10-08 13:58:33 -07001303 if (audio) {
1304 mAudioEOS = false;
1305 } else {
1306 mVideoEOS = false;
1307 }
1308
Steve Block3856b092011-10-20 11:56:00 +01001309 ALOGV("renderer %s flush completed.", audio ? "audio" : "video");
Wei Jiada4252f2015-07-14 18:15:28 -07001310 if (audio && (mFlushingAudio == NONE || mFlushingAudio == FLUSHED
1311 || mFlushingAudio == SHUT_DOWN)) {
1312 // Flush has been handled by tear down.
1313 break;
1314 }
Andy Hung8d121d42014-10-03 09:53:53 -07001315 handleFlushComplete(audio, false /* isDecoder */);
1316 finishFlushIfPossible();
James Dongf57b4ea2012-07-20 13:38:36 -07001317 } else if (what == Renderer::kWhatVideoRenderingStart) {
1318 notifyListener(MEDIA_INFO, MEDIA_INFO_RENDERING_START, 0);
Lajos Molnarcbaffcf2013-08-14 18:30:38 -07001319 } else if (what == Renderer::kWhatMediaRenderingStart) {
1320 ALOGV("media rendering started");
1321 notifyListener(MEDIA_STARTED, 0, 0);
Ronghua Wufaeb0f22015-05-21 12:20:21 -07001322 } else if (what == Renderer::kWhatAudioTearDown) {
Ronghua Wu08529172014-10-02 16:55:52 -07001323 int32_t reason;
1324 CHECK(msg->findInt32("reason", &reason));
Ronghua Wufaeb0f22015-05-21 12:20:21 -07001325 ALOGV("Tear down audio with reason %d.", reason);
Wei Jia8456ddd2016-04-22 14:46:43 -07001326 if (reason == Renderer::kDueToTimeout && !(mPaused && mOffloadAudio)) {
1327 // TimeoutWhenPaused is only for offload mode.
Wei Jia355b2bb2018-04-09 16:36:23 -07001328 ALOGW("Received a stale message for teardown, mPaused(%d), mOffloadAudio(%d)",
1329 mPaused, mOffloadAudio);
Wei Jia8456ddd2016-04-22 14:46:43 -07001330 break;
1331 }
Wei Jiada4252f2015-07-14 18:15:28 -07001332 int64_t positionUs;
Robert Shih1a5c8592015-08-04 18:07:44 -07001333 if (!msg->findInt64("positionUs", &positionUs)) {
1334 positionUs = mPreviousSeekTimeUs;
1335 }
Ronghua Wufaeb0f22015-05-21 12:20:21 -07001336
Wei Jia5031b2f2016-02-25 11:19:31 -08001337 restartAudio(
Wei Jiaa05f1e32016-03-25 16:31:22 -07001338 positionUs, reason == Renderer::kForceNonOffload /* forceNonOffload */,
1339 reason != Renderer::kDueToTimeout /* needsToCreateAudioDecoder */);
Andreas Huberf9334412010-12-15 15:17:42 -08001340 }
1341 break;
1342 }
1343
1344 case kWhatMoreDataQueued:
1345 {
1346 break;
1347 }
1348
Andreas Huber1aef2112011-01-04 14:01:29 -08001349 case kWhatReset:
1350 {
Steve Block3856b092011-10-20 11:56:00 +01001351 ALOGV("kWhatReset");
Andreas Huber1aef2112011-01-04 14:01:29 -08001352
Ronghua Wu64c2d172015-10-07 16:52:19 -07001353 mResetting = true;
Ray Essickeda32522018-02-28 12:08:28 -08001354 updatePlaybackTimer(true /* stopping */, "kWhatReset");
1355 updateRebufferingTimer(true /* stopping */, true /* exiting */);
Ronghua Wu64c2d172015-10-07 16:52:19 -07001356
Andreas Hubera1f8ab02012-11-30 10:53:22 -08001357 mDeferredActions.push_back(
Wei Jiafef808d2014-10-31 17:57:05 -07001358 new FlushDecoderAction(
1359 FLUSH_CMD_SHUTDOWN /* audio */,
1360 FLUSH_CMD_SHUTDOWN /* video */));
Andreas Huberb7c8e912012-11-27 15:02:53 -08001361
Andreas Hubera1f8ab02012-11-30 10:53:22 -08001362 mDeferredActions.push_back(
1363 new SimpleAction(&NuPlayer::performReset));
Andreas Huberb58ce9f2011-11-28 16:27:35 -08001364
Andreas Hubera1f8ab02012-11-30 10:53:22 -08001365 processDeferredActions();
Andreas Huber1aef2112011-01-04 14:01:29 -08001366 break;
1367 }
1368
Wei Jia52c28512017-09-13 18:17:51 -07001369 case kWhatNotifyTime:
1370 {
1371 ALOGV("kWhatNotifyTime");
1372 int64_t timerUs;
1373 CHECK(msg->findInt64("timerUs", &timerUs));
1374
1375 notifyListener(MEDIA_NOTIFY_TIME, timerUs, 0);
1376 break;
1377 }
1378
Andreas Huber43c3e6c2011-01-05 12:17:08 -08001379 case kWhatSeek:
1380 {
1381 int64_t seekTimeUs;
Wei Jiac5de0912016-11-18 10:22:14 -08001382 int32_t mode;
Wei Jiae427abf2014-09-22 15:21:11 -07001383 int32_t needNotify;
Andreas Huber43c3e6c2011-01-05 12:17:08 -08001384 CHECK(msg->findInt64("seekTimeUs", &seekTimeUs));
Wei Jiac5de0912016-11-18 10:22:14 -08001385 CHECK(msg->findInt32("mode", &mode));
Wei Jiae427abf2014-09-22 15:21:11 -07001386 CHECK(msg->findInt32("needNotify", &needNotify));
Andreas Huber43c3e6c2011-01-05 12:17:08 -08001387
Wei Jiac5de0912016-11-18 10:22:14 -08001388 ALOGV("kWhatSeek seekTimeUs=%lld us, mode=%d, needNotify=%d",
1389 (long long)seekTimeUs, mode, needNotify);
Andreas Huber43c3e6c2011-01-05 12:17:08 -08001390
Wei Jia1061c9c2015-05-19 16:02:17 -07001391 if (!mStarted) {
1392 // Seek before the player is started. In order to preview video,
1393 // need to start the player and pause it. This branch is called
1394 // only once if needed. After the player is started, any seek
1395 // operation will go through normal path.
Robert Shih0c61a0d2015-07-06 15:09:10 -07001396 // Audio-only cases are handled separately.
Wei Jiac5de0912016-11-18 10:22:14 -08001397 onStart(seekTimeUs, (MediaPlayerSeekMode)mode);
Robert Shih0c61a0d2015-07-06 15:09:10 -07001398 if (mStarted) {
1399 onPause();
1400 mPausedByClient = true;
1401 }
Wei Jia1061c9c2015-05-19 16:02:17 -07001402 if (needNotify) {
1403 notifyDriverSeekComplete();
1404 }
1405 break;
1406 }
1407
Andreas Hubera1f8ab02012-11-30 10:53:22 -08001408 mDeferredActions.push_back(
Wei Jiafef808d2014-10-31 17:57:05 -07001409 new FlushDecoderAction(FLUSH_CMD_FLUSH /* audio */,
1410 FLUSH_CMD_FLUSH /* video */));
Andreas Huber43c3e6c2011-01-05 12:17:08 -08001411
Wei Jiae427abf2014-09-22 15:21:11 -07001412 mDeferredActions.push_back(
Wei Jiac5de0912016-11-18 10:22:14 -08001413 new SeekAction(seekTimeUs, (MediaPlayerSeekMode)mode));
Andreas Huber43c3e6c2011-01-05 12:17:08 -08001414
Chong Zhangf8d71772014-11-26 15:08:34 -08001415 // After a flush without shutdown, decoder is paused.
1416 // Don't resume it until source seek is done, otherwise it could
Chong Zhang7137ec72014-11-12 16:41:05 -08001417 // start pulling stale data too soon.
1418 mDeferredActions.push_back(
Chong Zhangf8d71772014-11-26 15:08:34 -08001419 new ResumeDecoderAction(needNotify));
Chong Zhang7137ec72014-11-12 16:41:05 -08001420
Andreas Hubera1f8ab02012-11-30 10:53:22 -08001421 processDeferredActions();
Andreas Huber43c3e6c2011-01-05 12:17:08 -08001422 break;
1423 }
1424
Andreas Huberb4082222011-01-20 15:23:04 -08001425 case kWhatPause:
1426 {
Chong Zhangefbb6192015-01-30 17:13:27 -08001427 onPause();
1428 mPausedByClient = true;
Andreas Huberb4082222011-01-20 15:23:04 -08001429 break;
1430 }
1431
Andreas Huberb5f25f02013-02-05 10:14:26 -08001432 case kWhatSourceNotify:
1433 {
Andreas Huber9575c962013-02-05 13:59:56 -08001434 onSourceNotify(msg);
Andreas Huberb5f25f02013-02-05 10:14:26 -08001435 break;
1436 }
1437
Chong Zhanga7fa1d92014-06-11 14:49:23 -07001438 case kWhatClosedCaptionNotify:
1439 {
1440 onClosedCaptionNotify(msg);
1441 break;
1442 }
1443
Hassan Shojaniacefac142017-02-06 21:02:02 -08001444 case kWhatPrepareDrm:
1445 {
1446 status_t status = onPrepareDrm(msg);
1447
1448 sp<AMessage> response = new AMessage;
1449 response->setInt32("status", status);
1450 sp<AReplyToken> replyID;
1451 CHECK(msg->senderAwaitsResponse(&replyID));
1452 response->postReply(replyID);
1453 break;
1454 }
1455
1456 case kWhatReleaseDrm:
1457 {
1458 status_t status = onReleaseDrm();
1459
1460 sp<AMessage> response = new AMessage;
1461 response->setInt32("status", status);
1462 sp<AReplyToken> replyID;
1463 CHECK(msg->senderAwaitsResponse(&replyID));
1464 response->postReply(replyID);
1465 break;
1466 }
1467
Dongwon Kang47afe0a2018-03-27 15:15:30 -07001468 case kWhatMediaClockNotify:
1469 {
1470 ALOGV("kWhatMediaClockNotify");
1471 int64_t anchorMediaUs, anchorRealUs;
1472 float playbackRate;
1473 CHECK(msg->findInt64("anchor-media-us", &anchorMediaUs));
1474 CHECK(msg->findInt64("anchor-real-us", &anchorRealUs));
1475 CHECK(msg->findFloat("playback-rate", &playbackRate));
1476
1477 Parcel in;
1478 in.writeInt64(anchorMediaUs);
1479 in.writeInt64(anchorRealUs);
1480 in.writeFloat(playbackRate);
1481
1482 notifyListener(MEDIA_TIME_DISCONTINUITY, 0, 0, &in);
1483 break;
1484 }
1485
Andreas Huberf9334412010-12-15 15:17:42 -08001486 default:
1487 TRESPASS();
1488 break;
1489 }
1490}
1491
Wei Jia94211742014-10-28 17:09:06 -07001492void NuPlayer::onResume() {
Ronghua Wu64c2d172015-10-07 16:52:19 -07001493 if (!mPaused || mResetting) {
1494 ALOGD_IF(mResetting, "resetting, onResume discarded");
Chong Zhangefbb6192015-01-30 17:13:27 -08001495 return;
1496 }
1497 mPaused = false;
Wei Jia94211742014-10-28 17:09:06 -07001498 if (mSource != NULL) {
1499 mSource->resume();
1500 } else {
1501 ALOGW("resume called when source is gone or not set");
1502 }
1503 // |mAudioDecoder| may have been released due to the pause timeout, so re-create it if
1504 // needed.
1505 if (audioDecoderStillNeeded() && mAudioDecoder == NULL) {
1506 instantiateDecoder(true /* audio */, &mAudioDecoder);
1507 }
1508 if (mRenderer != NULL) {
1509 mRenderer->resume();
1510 } else {
1511 ALOGW("resume called when renderer is gone or not set");
1512 }
Ray Essickd4d00612017-01-03 09:36:27 -08001513
Ray Essick0d98c182017-11-09 13:42:42 -08001514 startPlaybackTimer("onresume");
Wei Jia94211742014-10-28 17:09:06 -07001515}
1516
Lajos Molnarfcd3e942015-03-31 10:06:48 -07001517status_t NuPlayer::onInstantiateSecureDecoders() {
1518 status_t err;
1519 if (!(mSourceFlags & Source::FLAG_SECURE)) {
1520 return BAD_TYPE;
1521 }
1522
1523 if (mRenderer != NULL) {
1524 ALOGE("renderer should not be set when instantiating secure decoders");
1525 return UNKNOWN_ERROR;
1526 }
1527
1528 // TRICKY: We rely on mRenderer being null, so that decoder does not start requesting
1529 // data on instantiation.
Lajos Molnar1de1e252015-04-30 18:18:34 -07001530 if (mSurface != NULL) {
Lajos Molnarfcd3e942015-03-31 10:06:48 -07001531 err = instantiateDecoder(false, &mVideoDecoder);
1532 if (err != OK) {
1533 return err;
1534 }
1535 }
1536
1537 if (mAudioSink != NULL) {
1538 err = instantiateDecoder(true, &mAudioDecoder);
1539 if (err != OK) {
1540 return err;
1541 }
1542 }
1543 return OK;
1544}
1545
Wei Jiac5de0912016-11-18 10:22:14 -08001546void NuPlayer::onStart(int64_t startPositionUs, MediaPlayerSeekMode mode) {
Hassan Shojaniacefac142017-02-06 21:02:02 -08001547 ALOGV("onStart: mCrypto: %p (%d)", mCrypto.get(),
1548 (mCrypto != NULL ? mCrypto->getStrongCount() : 0));
1549
Robert Shih0c61a0d2015-07-06 15:09:10 -07001550 if (!mSourceStarted) {
1551 mSourceStarted = true;
1552 mSource->start();
1553 }
1554 if (startPositionUs > 0) {
Wei Jiac5de0912016-11-18 10:22:14 -08001555 performSeek(startPositionUs, mode);
Robert Shih0c61a0d2015-07-06 15:09:10 -07001556 if (mSource->getFormat(false /* audio */) == NULL) {
1557 return;
1558 }
1559 }
1560
Wei Jia94211742014-10-28 17:09:06 -07001561 mOffloadAudio = false;
1562 mAudioEOS = false;
1563 mVideoEOS = false;
Wei Jia94211742014-10-28 17:09:06 -07001564 mStarted = true;
Wei Jiafe8fe7d2016-06-08 10:29:25 -07001565 mPaused = false;
Wei Jia94211742014-10-28 17:09:06 -07001566
Wei Jia94211742014-10-28 17:09:06 -07001567 uint32_t flags = 0;
1568
1569 if (mSource->isRealTime()) {
1570 flags |= Renderer::FLAG_REAL_TIME;
1571 }
1572
Wei Jia9737d342016-12-28 14:59:59 -08001573 bool hasAudio = (mSource->getFormat(true /* audio */) != NULL);
1574 bool hasVideo = (mSource->getFormat(false /* audio */) != NULL);
1575 if (!hasAudio && !hasVideo) {
Wei Jia1de83d52016-10-10 10:15:03 -07001576 ALOGE("no metadata for either audio or video source");
1577 mSource->stop();
1578 mSourceStarted = false;
1579 notifyListener(MEDIA_ERROR, MEDIA_ERROR_UNKNOWN, ERROR_MALFORMED);
1580 return;
1581 }
Wei Jia9737d342016-12-28 14:59:59 -08001582 ALOGV_IF(!hasAudio, "no metadata for audio source"); // video only stream
1583
1584 sp<MetaData> audioMeta = mSource->getFormatMeta(true /* audio */);
Wei Jia1de83d52016-10-10 10:15:03 -07001585
Wei Jia94211742014-10-28 17:09:06 -07001586 audio_stream_type_t streamType = AUDIO_STREAM_MUSIC;
1587 if (mAudioSink != NULL) {
1588 streamType = mAudioSink->getAudioStreamType();
1589 }
1590
Wei Jia94211742014-10-28 17:09:06 -07001591 mOffloadAudio =
Wei Jia9737d342016-12-28 14:59:59 -08001592 canOffloadStream(audioMeta, hasVideo, mSource->isStreaming(), streamType)
Wei Jiabf70feb2016-02-19 15:47:16 -08001593 && (mPlaybackSettings.mSpeed == 1.f && mPlaybackSettings.mPitch == 1.f);
Hassan Shojaniacefac142017-02-06 21:02:02 -08001594
1595 // Modular DRM: Disabling audio offload if the source is protected
1596 if (mOffloadAudio && mIsDrmProtected) {
1597 mOffloadAudio = false;
1598 ALOGV("onStart: Disabling mOffloadAudio now that the source is protected.");
1599 }
1600
Wei Jia94211742014-10-28 17:09:06 -07001601 if (mOffloadAudio) {
1602 flags |= Renderer::FLAG_OFFLOAD_AUDIO;
1603 }
1604
Lajos Molnar1d15ab52015-03-04 16:46:34 -08001605 sp<AMessage> notify = new AMessage(kWhatRendererNotify, this);
Wei Jia94211742014-10-28 17:09:06 -07001606 ++mRendererGeneration;
1607 notify->setInt32("generation", mRendererGeneration);
Wei Jia0a68f662017-08-30 18:01:26 -07001608 mRenderer = new Renderer(mAudioSink, mMediaClock, notify, flags);
Wei Jia94211742014-10-28 17:09:06 -07001609 mRendererLooper = new ALooper;
1610 mRendererLooper->setName("NuPlayerRenderer");
1611 mRendererLooper->start(false, false, ANDROID_PRIORITY_AUDIO);
1612 mRendererLooper->registerHandler(mRenderer);
Lajos Molnar3a474aa2015-04-24 17:10:07 -07001613
1614 status_t err = mRenderer->setPlaybackSettings(mPlaybackSettings);
1615 if (err != OK) {
1616 mSource->stop();
Robert Shih0c61a0d2015-07-06 15:09:10 -07001617 mSourceStarted = false;
Lajos Molnar3a474aa2015-04-24 17:10:07 -07001618 notifyListener(MEDIA_ERROR, MEDIA_ERROR_UNKNOWN, err);
1619 return;
Wei Jiac8206ff2015-03-04 13:59:37 -08001620 }
Wei Jia94211742014-10-28 17:09:06 -07001621
Ronghua Wuc8a70d32015-04-29 16:26:34 -07001622 float rate = getFrameRate();
1623 if (rate > 0) {
Wei Jia94211742014-10-28 17:09:06 -07001624 mRenderer->setVideoFrameRate(rate);
1625 }
1626
Wei Jiac6cfd702014-11-11 16:33:20 -08001627 if (mVideoDecoder != NULL) {
1628 mVideoDecoder->setRenderer(mRenderer);
1629 }
1630 if (mAudioDecoder != NULL) {
1631 mAudioDecoder->setRenderer(mRenderer);
1632 }
1633
Ray Essick0d98c182017-11-09 13:42:42 -08001634 startPlaybackTimer("onstart");
Ray Essickd4d00612017-01-03 09:36:27 -08001635
Wei Jia94211742014-10-28 17:09:06 -07001636 postScanSources();
1637}
1638
Ray Essick0d98c182017-11-09 13:42:42 -08001639void NuPlayer::startPlaybackTimer(const char *where) {
1640 Mutex::Autolock autoLock(mPlayingTimeLock);
1641 if (mLastStartedPlayingTimeNs == 0) {
1642 mLastStartedPlayingTimeNs = systemTime();
1643 ALOGV("startPlaybackTimer() time %20" PRId64 " (%s)", mLastStartedPlayingTimeNs, where);
1644 }
1645}
1646
Ray Essickeda32522018-02-28 12:08:28 -08001647void NuPlayer::updatePlaybackTimer(bool stopping, const char *where) {
Ray Essick0d98c182017-11-09 13:42:42 -08001648 Mutex::Autolock autoLock(mPlayingTimeLock);
1649
Ray Essickeda32522018-02-28 12:08:28 -08001650 ALOGV("updatePlaybackTimer(%s) time %20" PRId64 " (%s)",
1651 stopping ? "stop" : "snap", mLastStartedPlayingTimeNs, where);
Ray Essick0d98c182017-11-09 13:42:42 -08001652
1653 if (mLastStartedPlayingTimeNs != 0) {
1654 sp<NuPlayerDriver> driver = mDriver.promote();
Ray Essickeda32522018-02-28 12:08:28 -08001655 int64_t now = systemTime();
Ray Essick0d98c182017-11-09 13:42:42 -08001656 if (driver != NULL) {
Ray Essick0d98c182017-11-09 13:42:42 -08001657 int64_t played = now - mLastStartedPlayingTimeNs;
Ray Essickeda32522018-02-28 12:08:28 -08001658 ALOGV("updatePlaybackTimer() log %20" PRId64 "", played);
Ray Essick0d98c182017-11-09 13:42:42 -08001659
1660 if (played > 0) {
1661 driver->notifyMorePlayingTimeUs((played+500)/1000);
1662 }
1663 }
Ray Essickeda32522018-02-28 12:08:28 -08001664 if (stopping) {
1665 mLastStartedPlayingTimeNs = 0;
1666 } else {
1667 mLastStartedPlayingTimeNs = now;
1668 }
Ray Essick0d98c182017-11-09 13:42:42 -08001669 }
1670}
1671
Ray Essick58e0f7a2017-11-21 10:59:38 -08001672void NuPlayer::startRebufferingTimer() {
1673 Mutex::Autolock autoLock(mPlayingTimeLock);
1674 if (mLastStartedRebufferingTimeNs == 0) {
1675 mLastStartedRebufferingTimeNs = systemTime();
1676 ALOGV("startRebufferingTimer() time %20" PRId64 "", mLastStartedRebufferingTimeNs);
1677 }
1678}
1679
Ray Essickeda32522018-02-28 12:08:28 -08001680void NuPlayer::updateRebufferingTimer(bool stopping, bool exitingPlayback) {
Ray Essick58e0f7a2017-11-21 10:59:38 -08001681 Mutex::Autolock autoLock(mPlayingTimeLock);
1682
Ray Essickeda32522018-02-28 12:08:28 -08001683 ALOGV("updateRebufferingTimer(%s) time %20" PRId64 " (exiting %d)",
1684 stopping ? "stop" : "snap", mLastStartedRebufferingTimeNs, exitingPlayback);
Ray Essick58e0f7a2017-11-21 10:59:38 -08001685
1686 if (mLastStartedRebufferingTimeNs != 0) {
1687 sp<NuPlayerDriver> driver = mDriver.promote();
Ray Essickeda32522018-02-28 12:08:28 -08001688 int64_t now = systemTime();
Ray Essick58e0f7a2017-11-21 10:59:38 -08001689 if (driver != NULL) {
Ray Essick58e0f7a2017-11-21 10:59:38 -08001690 int64_t rebuffered = now - mLastStartedRebufferingTimeNs;
Ray Essickeda32522018-02-28 12:08:28 -08001691 ALOGV("updateRebufferingTimer() log %20" PRId64 "", rebuffered);
Ray Essick58e0f7a2017-11-21 10:59:38 -08001692
1693 if (rebuffered > 0) {
1694 driver->notifyMoreRebufferingTimeUs((rebuffered+500)/1000);
1695 if (exitingPlayback) {
1696 driver->notifyRebufferingWhenExit(true);
1697 }
1698 }
1699 }
Ray Essickeda32522018-02-28 12:08:28 -08001700 if (stopping) {
1701 mLastStartedRebufferingTimeNs = 0;
1702 } else {
1703 mLastStartedRebufferingTimeNs = now;
1704 }
Ray Essick58e0f7a2017-11-21 10:59:38 -08001705 }
1706}
1707
Ray Essickeda32522018-02-28 12:08:28 -08001708void NuPlayer::updateInternalTimers() {
1709 // update values, but ticking clocks keep ticking
1710 ALOGV("updateInternalTimers()");
1711 updatePlaybackTimer(false /* stopping */, "updateInternalTimers");
1712 updateRebufferingTimer(false /* stopping */, false /* exiting */);
1713}
1714
Kim Sungyeon23b23932019-07-18 17:48:32 +09001715void NuPlayer::setTargetBitrate(int bitrate) {
Lajos Molnared809da2020-08-17 16:53:02 -07001716 if (mSource != NULL) {
Kim Sungyeon23b23932019-07-18 17:48:32 +09001717 mSource->setTargetBitrate(bitrate);
Lajos Molnared809da2020-08-17 16:53:02 -07001718 }
Kim Sungyeon23b23932019-07-18 17:48:32 +09001719}
1720
Chong Zhangefbb6192015-01-30 17:13:27 -08001721void NuPlayer::onPause() {
Ray Essick0d98c182017-11-09 13:42:42 -08001722
Ray Essickeda32522018-02-28 12:08:28 -08001723 updatePlaybackTimer(true /* stopping */, "onPause");
Ray Essick0d98c182017-11-09 13:42:42 -08001724
Chong Zhangefbb6192015-01-30 17:13:27 -08001725 if (mPaused) {
1726 return;
1727 }
1728 mPaused = true;
1729 if (mSource != NULL) {
1730 mSource->pause();
1731 } else {
1732 ALOGW("pause called when source is gone or not set");
1733 }
1734 if (mRenderer != NULL) {
1735 mRenderer->pause();
1736 } else {
1737 ALOGW("pause called when renderer is gone or not set");
1738 }
Ray Essickd4d00612017-01-03 09:36:27 -08001739
Chong Zhangefbb6192015-01-30 17:13:27 -08001740}
1741
Ronghua Wud7988b12014-10-03 15:19:10 -07001742bool NuPlayer::audioDecoderStillNeeded() {
1743 // Audio decoder is no longer needed if it's in shut/shutting down status.
1744 return ((mFlushingAudio != SHUT_DOWN) && (mFlushingAudio != SHUTTING_DOWN_DECODER));
1745}
1746
Andy Hung8d121d42014-10-03 09:53:53 -07001747void NuPlayer::handleFlushComplete(bool audio, bool isDecoder) {
1748 // We wait for both the decoder flush and the renderer flush to complete
1749 // before entering either the FLUSHED or the SHUTTING_DOWN_DECODER state.
1750
1751 mFlushComplete[audio][isDecoder] = true;
1752 if (!mFlushComplete[audio][!isDecoder]) {
1753 return;
1754 }
1755
1756 FlushStatus *state = audio ? &mFlushingAudio : &mFlushingVideo;
1757 switch (*state) {
1758 case FLUSHING_DECODER:
1759 {
1760 *state = FLUSHED;
Andy Hung8d121d42014-10-03 09:53:53 -07001761 break;
1762 }
1763
1764 case FLUSHING_DECODER_SHUTDOWN:
1765 {
1766 *state = SHUTTING_DOWN_DECODER;
1767
1768 ALOGV("initiating %s decoder shutdown", audio ? "audio" : "video");
Andy Hung8d121d42014-10-03 09:53:53 -07001769 getDecoder(audio)->initiateShutdown();
1770 break;
1771 }
1772
1773 default:
1774 // decoder flush completes only occur in a flushing state.
1775 LOG_ALWAYS_FATAL_IF(isDecoder, "decoder flush in invalid state %d", *state);
1776 break;
1777 }
1778}
1779
Andreas Huber3831a062010-12-21 10:22:33 -08001780void NuPlayer::finishFlushIfPossible() {
Wei Jia53904f32014-07-29 10:22:53 -07001781 if (mFlushingAudio != NONE && mFlushingAudio != FLUSHED
1782 && mFlushingAudio != SHUT_DOWN) {
Andreas Huber3831a062010-12-21 10:22:33 -08001783 return;
1784 }
1785
Wei Jia53904f32014-07-29 10:22:53 -07001786 if (mFlushingVideo != NONE && mFlushingVideo != FLUSHED
1787 && mFlushingVideo != SHUT_DOWN) {
Andreas Huber3831a062010-12-21 10:22:33 -08001788 return;
1789 }
1790
Steve Block3856b092011-10-20 11:56:00 +01001791 ALOGV("both audio and video are flushed now.");
Andreas Huber3831a062010-12-21 10:22:33 -08001792
Andreas Huber3831a062010-12-21 10:22:33 -08001793 mFlushingAudio = NONE;
1794 mFlushingVideo = NONE;
Andreas Huber3831a062010-12-21 10:22:33 -08001795
Andy Hung8d121d42014-10-03 09:53:53 -07001796 clearFlushComplete();
1797
Andreas Hubera1f8ab02012-11-30 10:53:22 -08001798 processDeferredActions();
Andreas Huber1aef2112011-01-04 14:01:29 -08001799}
1800
1801void NuPlayer::postScanSources() {
1802 if (mScanSourcesPending) {
1803 return;
1804 }
1805
Lajos Molnar1d15ab52015-03-04 16:46:34 -08001806 sp<AMessage> msg = new AMessage(kWhatScanSources, this);
Andreas Huber1aef2112011-01-04 14:01:29 -08001807 msg->setInt32("generation", mScanSourcesGeneration);
1808 msg->post();
1809
1810 mScanSourcesPending = true;
1811}
1812
Wei Jia41cd4632016-05-13 10:53:30 -07001813void NuPlayer::tryOpenAudioSinkForOffload(
1814 const sp<AMessage> &format, const sp<MetaData> &audioMeta, bool hasVideo) {
Andy Hung202bce12014-12-03 11:47:36 -08001815 // Note: This is called early in NuPlayer to determine whether offloading
1816 // is possible; otherwise the decoders call the renderer openAudioSink directly.
Andy Hung282a7e32014-08-14 15:56:34 -07001817
Andy Hung202bce12014-12-03 11:47:36 -08001818 status_t err = mRenderer->openAudioSink(
Dhananjay Kumarc387f2b2015-08-06 10:43:16 +05301819 format, true /* offloadOnly */, hasVideo,
1820 AUDIO_OUTPUT_FLAG_NONE, &mOffloadAudio, mSource->isStreaming());
Andy Hung202bce12014-12-03 11:47:36 -08001821 if (err != OK) {
1822 // Any failure we turn off mOffloadAudio.
1823 mOffloadAudio = false;
1824 } else if (mOffloadAudio) {
Chong Zhang3b9eb1f2014-10-15 17:05:08 -07001825 sendMetaDataToHal(mAudioSink, audioMeta);
Andy Hung282a7e32014-08-14 15:56:34 -07001826 }
1827}
1828
1829void NuPlayer::closeAudioSink() {
Daniel Norman1e0d88b2020-01-07 15:02:54 -08001830 if (mRenderer != NULL) {
1831 mRenderer->closeAudioSink();
1832 }
Andy Hung282a7e32014-08-14 15:56:34 -07001833}
1834
Wei Jia5031b2f2016-02-25 11:19:31 -08001835void NuPlayer::restartAudio(
Wei Jiabf70feb2016-02-19 15:47:16 -08001836 int64_t currentPositionUs, bool forceNonOffload, bool needsToCreateAudioDecoder) {
Wei Jia355b2bb2018-04-09 16:36:23 -07001837 ALOGD("restartAudio timeUs(%lld), dontOffload(%d), createDecoder(%d)",
1838 (long long)currentPositionUs, forceNonOffload, needsToCreateAudioDecoder);
Wei Jiaa05f1e32016-03-25 16:31:22 -07001839 if (mAudioDecoder != NULL) {
1840 mAudioDecoder->pause();
Ray Essickfcb8fd32018-08-07 09:39:38 -07001841 Mutex::Autolock autoLock(mDecoderLock);
Wei Jiaa05f1e32016-03-25 16:31:22 -07001842 mAudioDecoder.clear();
Wei Jia686e8e52017-04-03 14:08:01 -07001843 mAudioDecoderError = false;
Wei Jiaa05f1e32016-03-25 16:31:22 -07001844 ++mAudioDecoderGeneration;
1845 }
Wei Jiabf70feb2016-02-19 15:47:16 -08001846 if (mFlushingAudio == FLUSHING_DECODER) {
1847 mFlushComplete[1 /* audio */][1 /* isDecoder */] = true;
1848 mFlushingAudio = FLUSHED;
1849 finishFlushIfPossible();
1850 } else if (mFlushingAudio == FLUSHING_DECODER_SHUTDOWN
1851 || mFlushingAudio == SHUTTING_DOWN_DECODER) {
1852 mFlushComplete[1 /* audio */][1 /* isDecoder */] = true;
1853 mFlushingAudio = SHUT_DOWN;
1854 finishFlushIfPossible();
1855 needsToCreateAudioDecoder = false;
1856 }
1857 if (mRenderer == NULL) {
1858 return;
1859 }
1860 closeAudioSink();
1861 mRenderer->flush(true /* audio */, false /* notifyComplete */);
1862 if (mVideoDecoder != NULL) {
Praveen Chavaneca08fb2019-01-21 11:00:38 -08001863 mDeferredActions.push_back(
1864 new FlushDecoderAction(FLUSH_CMD_NONE /* audio */,
1865 FLUSH_CMD_FLUSH /* video */));
1866 mDeferredActions.push_back(
1867 new SeekAction(currentPositionUs,
1868 MediaPlayerSeekMode::SEEK_PREVIOUS_SYNC /* mode */));
1869 // After a flush without shutdown, decoder is paused.
1870 // Don't resume it until source seek is done, otherwise it could
1871 // start pulling stale data too soon.
1872 mDeferredActions.push_back(new ResumeDecoderAction(false));
1873 processDeferredActions();
1874 } else {
1875 performSeek(currentPositionUs, MediaPlayerSeekMode::SEEK_PREVIOUS_SYNC /* mode */);
Wei Jiabf70feb2016-02-19 15:47:16 -08001876 }
1877
Wei Jiabf70feb2016-02-19 15:47:16 -08001878 if (forceNonOffload) {
1879 mRenderer->signalDisableOffloadAudio();
1880 mOffloadAudio = false;
1881 }
1882 if (needsToCreateAudioDecoder) {
Wei Jiaa05f1e32016-03-25 16:31:22 -07001883 instantiateDecoder(true /* audio */, &mAudioDecoder, !forceNonOffload);
Wei Jiabf70feb2016-02-19 15:47:16 -08001884 }
1885}
1886
Wei Jia41cd4632016-05-13 10:53:30 -07001887void NuPlayer::determineAudioModeChange(const sp<AMessage> &audioFormat) {
Wei Jiae4d18c72015-07-13 17:58:11 -07001888 if (mSource == NULL || mAudioSink == NULL) {
1889 return;
1890 }
1891
1892 if (mRenderer == NULL) {
1893 ALOGW("No renderer can be used to determine audio mode. Use non-offload for safety.");
1894 mOffloadAudio = false;
1895 return;
1896 }
1897
1898 sp<MetaData> audioMeta = mSource->getFormatMeta(true /* audio */);
1899 sp<AMessage> videoFormat = mSource->getFormat(false /* audio */);
1900 audio_stream_type_t streamType = mAudioSink->getAudioStreamType();
1901 const bool hasVideo = (videoFormat != NULL);
Hassan Shojaniacefac142017-02-06 21:02:02 -08001902 bool canOffload = canOffloadStream(
Wei Jiabf70feb2016-02-19 15:47:16 -08001903 audioMeta, hasVideo, mSource->isStreaming(), streamType)
1904 && (mPlaybackSettings.mSpeed == 1.f && mPlaybackSettings.mPitch == 1.f);
Hassan Shojaniacefac142017-02-06 21:02:02 -08001905
1906 // Modular DRM: Disabling audio offload if the source is protected
1907 if (canOffload && mIsDrmProtected) {
1908 canOffload = false;
1909 ALOGV("determineAudioModeChange: Disabling mOffloadAudio b/c the source is protected.");
1910 }
1911
Wei Jiae4d18c72015-07-13 17:58:11 -07001912 if (canOffload) {
1913 if (!mOffloadAudio) {
1914 mRenderer->signalEnableOffloadAudio();
1915 }
1916 // open audio sink early under offload mode.
Wei Jia41cd4632016-05-13 10:53:30 -07001917 tryOpenAudioSinkForOffload(audioFormat, audioMeta, hasVideo);
Wei Jiae4d18c72015-07-13 17:58:11 -07001918 } else {
Robert Shihe1d70192015-07-23 17:54:13 -07001919 if (mOffloadAudio) {
1920 mRenderer->signalDisableOffloadAudio();
1921 mOffloadAudio = false;
1922 }
Wei Jiae4d18c72015-07-13 17:58:11 -07001923 }
1924}
1925
Wei Jiaa05f1e32016-03-25 16:31:22 -07001926status_t NuPlayer::instantiateDecoder(
1927 bool audio, sp<DecoderBase> *decoder, bool checkAudioModeChange) {
Wei Jia566da802015-08-27 13:59:30 -07001928 // The audio decoder could be cleared by tear down. If still in shut down
1929 // process, no need to create a new audio decoder.
1930 if (*decoder != NULL || (audio && mFlushingAudio == SHUT_DOWN)) {
Andreas Huberf9334412010-12-15 15:17:42 -08001931 return OK;
1932 }
1933
Andreas Huber84066782011-08-16 09:34:26 -07001934 sp<AMessage> format = mSource->getFormat(audio);
Andreas Huberf9334412010-12-15 15:17:42 -08001935
Andreas Huber84066782011-08-16 09:34:26 -07001936 if (format == NULL) {
Robert Shih7350b052015-10-01 15:50:14 -07001937 return UNKNOWN_ERROR;
1938 } else {
1939 status_t err;
1940 if (format->findInt32("err", &err) && err) {
1941 return err;
1942 }
Andreas Huberf9334412010-12-15 15:17:42 -08001943 }
1944
Ronghua Wu8db88132015-04-22 13:51:35 -07001945 format->setInt32("priority", 0 /* realtime */);
1946
Byeongjo Park5e27b1b2018-07-12 15:36:03 +09001947 if (mDataSourceType == DATA_SOURCE_TYPE_RTP) {
1948 ALOGV("instantiateDecoder: set decoder error free on stream corrupt.");
1949 format->setInt32("corrupt-free", true);
1950 }
1951
Andreas Huber3fe62152011-09-16 15:09:22 -07001952 if (!audio) {
Andreas Huber84066782011-08-16 09:34:26 -07001953 AString mime;
1954 CHECK(format->findString("mime", &mime));
Chong Zhanga7fa1d92014-06-11 14:49:23 -07001955
Lajos Molnar1d15ab52015-03-04 16:46:34 -08001956 sp<AMessage> ccNotify = new AMessage(kWhatClosedCaptionNotify, this);
Chong Zhang341ab6e2015-02-04 13:37:18 -08001957 if (mCCDecoder == NULL) {
1958 mCCDecoder = new CCDecoder(ccNotify);
1959 }
Lajos Molnar09524832014-07-17 14:29:51 -07001960
1961 if (mSourceFlags & Source::FLAG_SECURE) {
1962 format->setInt32("secure", true);
1963 }
Chong Zhang17134602015-01-07 16:14:34 -08001964
1965 if (mSourceFlags & Source::FLAG_PROTECTED) {
1966 format->setInt32("protected", true);
1967 }
Ronghua Wu8db88132015-04-22 13:51:35 -07001968
Ronghua Wuc8a70d32015-04-29 16:26:34 -07001969 float rate = getFrameRate();
1970 if (rate > 0) {
Lajos Molnar3a474aa2015-04-24 17:10:07 -07001971 format->setFloat("operating-rate", rate * mPlaybackSettings.mSpeed);
Ronghua Wu8db88132015-04-22 13:51:35 -07001972 }
Andreas Huber3fe62152011-09-16 15:09:22 -07001973 }
1974
Ray Essickfcb8fd32018-08-07 09:39:38 -07001975 Mutex::Autolock autoLock(mDecoderLock);
1976
Wei Jiabc2fb722014-07-08 16:37:57 -07001977 if (audio) {
Lajos Molnar1d15ab52015-03-04 16:46:34 -08001978 sp<AMessage> notify = new AMessage(kWhatAudioNotify, this);
Wei Jia88703c32014-08-06 11:24:07 -07001979 ++mAudioDecoderGeneration;
1980 notify->setInt32("generation", mAudioDecoderGeneration);
1981
Wei Jiaa05f1e32016-03-25 16:31:22 -07001982 if (checkAudioModeChange) {
Wei Jia41cd4632016-05-13 10:53:30 -07001983 determineAudioModeChange(format);
Wei Jiaa05f1e32016-03-25 16:31:22 -07001984 }
Wei Jiabc2fb722014-07-08 16:37:57 -07001985 if (mOffloadAudio) {
Wei Jia14532f22015-12-29 11:28:15 -08001986 mSource->setOffloadAudio(true /* offload */);
1987
Haynes Mathew George8b635332015-03-30 17:59:47 -07001988 const bool hasVideo = (mSource->getFormat(false /*audio */) != NULL);
1989 format->setInt32("has-video", hasVideo);
Wei Jiac6cfd702014-11-11 16:33:20 -08001990 *decoder = new DecoderPassThrough(notify, mSource, mRenderer);
Hassan Shojaniacefac142017-02-06 21:02:02 -08001991 ALOGV("instantiateDecoder audio DecoderPassThrough hasVideo: %d", hasVideo);
Wei Jiabc2fb722014-07-08 16:37:57 -07001992 } else {
Wei Jia14532f22015-12-29 11:28:15 -08001993 mSource->setOffloadAudio(false /* offload */);
1994
Wei Jiaf2ae3e12016-10-27 17:10:59 -07001995 *decoder = new Decoder(notify, mSource, mPID, mUID, mRenderer);
Hassan Shojaniacefac142017-02-06 21:02:02 -08001996 ALOGV("instantiateDecoder audio Decoder");
Wei Jiabc2fb722014-07-08 16:37:57 -07001997 }
Wei Jia686e8e52017-04-03 14:08:01 -07001998 mAudioDecoderError = false;
Wei Jiabc2fb722014-07-08 16:37:57 -07001999 } else {
Lajos Molnar1d15ab52015-03-04 16:46:34 -08002000 sp<AMessage> notify = new AMessage(kWhatVideoNotify, this);
Wei Jia88703c32014-08-06 11:24:07 -07002001 ++mVideoDecoderGeneration;
2002 notify->setInt32("generation", mVideoDecoderGeneration);
2003
Chong Zhang7137ec72014-11-12 16:41:05 -08002004 *decoder = new Decoder(
Wei Jiaf2ae3e12016-10-27 17:10:59 -07002005 notify, mSource, mPID, mUID, mRenderer, mSurface, mCCDecoder);
Wei Jia686e8e52017-04-03 14:08:01 -07002006 mVideoDecoderError = false;
Lajos Molnard9fd6312014-11-06 11:00:00 -08002007
2008 // enable FRC if high-quality AV sync is requested, even if not
Lajos Molnar1de1e252015-04-30 18:18:34 -07002009 // directly queuing to display, as this will even improve textureview
Lajos Molnard9fd6312014-11-06 11:00:00 -08002010 // playback.
2011 {
Marco Nelissen96626b72016-12-01 13:58:59 -08002012 if (property_get_bool("persist.sys.media.avsync", false)) {
Lajos Molnard9fd6312014-11-06 11:00:00 -08002013 format->setInt32("auto-frc", 1);
2014 }
2015 }
Wei Jiabc2fb722014-07-08 16:37:57 -07002016 }
Lajos Molnar1cd13982014-01-17 15:12:51 -08002017 (*decoder)->init();
Hassan Shojaniacefac142017-02-06 21:02:02 -08002018
2019 // Modular DRM
2020 if (mIsDrmProtected) {
2021 format->setPointer("crypto", mCrypto.get());
2022 ALOGV("instantiateDecoder: mCrypto: %p (%d) isSecure: %d", mCrypto.get(),
2023 (mCrypto != NULL ? mCrypto->getStrongCount() : 0),
2024 (mSourceFlags & Source::FLAG_SECURE) != 0);
2025 }
2026
Andreas Huber84066782011-08-16 09:34:26 -07002027 (*decoder)->configure(format);
Andreas Huberf9334412010-12-15 15:17:42 -08002028
Praveen Chavanbbaa1442016-04-08 13:33:49 -07002029 if (!audio) {
2030 sp<AMessage> params = new AMessage();
2031 float rate = getFrameRate();
2032 if (rate > 0) {
2033 params->setFloat("frame-rate-total", rate);
2034 }
2035
2036 sp<MetaData> fileMeta = getFileMeta();
2037 if (fileMeta != NULL) {
2038 int32_t videoTemporalLayerCount;
2039 if (fileMeta->findInt32(kKeyTemporalLayerCount, &videoTemporalLayerCount)
2040 && videoTemporalLayerCount > 0) {
2041 params->setInt32("temporal-layer-count", videoTemporalLayerCount);
2042 }
2043 }
2044
2045 if (params->countEntries() > 0) {
2046 (*decoder)->setParameters(params);
2047 }
2048 }
Andreas Huberf9334412010-12-15 15:17:42 -08002049 return OK;
2050}
2051
Chong Zhangced1c2f2014-08-08 15:22:35 -07002052void NuPlayer::updateVideoSize(
2053 const sp<AMessage> &inputFormat,
2054 const sp<AMessage> &outputFormat) {
2055 if (inputFormat == NULL) {
2056 ALOGW("Unknown video size, reporting 0x0!");
2057 notifyListener(MEDIA_SET_VIDEO_SIZE, 0, 0);
2058 return;
2059 }
Wei Jia9737d342016-12-28 14:59:59 -08002060 int32_t err = OK;
2061 inputFormat->findInt32("err", &err);
2062 if (err == -EWOULDBLOCK) {
2063 ALOGW("Video meta is not available yet!");
2064 return;
2065 }
2066 if (err != OK) {
2067 ALOGW("Something is wrong with video meta!");
2068 return;
2069 }
Chong Zhangced1c2f2014-08-08 15:22:35 -07002070
2071 int32_t displayWidth, displayHeight;
Chong Zhangced1c2f2014-08-08 15:22:35 -07002072 if (outputFormat != NULL) {
2073 int32_t width, height;
2074 CHECK(outputFormat->findInt32("width", &width));
2075 CHECK(outputFormat->findInt32("height", &height));
2076
2077 int32_t cropLeft, cropTop, cropRight, cropBottom;
2078 CHECK(outputFormat->findRect(
2079 "crop",
2080 &cropLeft, &cropTop, &cropRight, &cropBottom));
2081
2082 displayWidth = cropRight - cropLeft + 1;
2083 displayHeight = cropBottom - cropTop + 1;
2084
2085 ALOGV("Video output format changed to %d x %d "
2086 "(crop: %d x %d @ (%d, %d))",
2087 width, height,
2088 displayWidth,
2089 displayHeight,
2090 cropLeft, cropTop);
2091 } else {
2092 CHECK(inputFormat->findInt32("width", &displayWidth));
2093 CHECK(inputFormat->findInt32("height", &displayHeight));
2094
2095 ALOGV("Video input format %d x %d", displayWidth, displayHeight);
2096 }
2097
2098 // Take into account sample aspect ratio if necessary:
2099 int32_t sarWidth, sarHeight;
2100 if (inputFormat->findInt32("sar-width", &sarWidth)
Wei Jia095bc252017-01-27 10:16:16 -08002101 && inputFormat->findInt32("sar-height", &sarHeight)
2102 && sarWidth > 0 && sarHeight > 0) {
Chong Zhangced1c2f2014-08-08 15:22:35 -07002103 ALOGV("Sample aspect ratio %d : %d", sarWidth, sarHeight);
2104
2105 displayWidth = (displayWidth * sarWidth) / sarHeight;
2106
2107 ALOGV("display dimensions %d x %d", displayWidth, displayHeight);
Wei Jiadf6c6af2016-09-29 11:27:15 -07002108 } else {
2109 int32_t width, height;
2110 if (inputFormat->findInt32("display-width", &width)
2111 && inputFormat->findInt32("display-height", &height)
2112 && width > 0 && height > 0
2113 && displayWidth > 0 && displayHeight > 0) {
2114 if (displayHeight * (int64_t)width / height > (int64_t)displayWidth) {
2115 displayHeight = (int32_t)(displayWidth * (int64_t)height / width);
2116 } else {
2117 displayWidth = (int32_t)(displayHeight * (int64_t)width / height);
2118 }
2119 ALOGV("Video display width and height are overridden to %d x %d",
2120 displayWidth, displayHeight);
2121 }
Chong Zhangced1c2f2014-08-08 15:22:35 -07002122 }
2123
2124 int32_t rotationDegrees;
2125 if (!inputFormat->findInt32("rotation-degrees", &rotationDegrees)) {
2126 rotationDegrees = 0;
2127 }
2128
2129 if (rotationDegrees == 90 || rotationDegrees == 270) {
2130 int32_t tmp = displayWidth;
2131 displayWidth = displayHeight;
2132 displayHeight = tmp;
2133 }
2134
2135 notifyListener(
2136 MEDIA_SET_VIDEO_SIZE,
2137 displayWidth,
2138 displayHeight);
2139}
2140
Chong Zhangdcb89b32013-08-06 09:44:47 -07002141void NuPlayer::notifyListener(int msg, int ext1, int ext2, const Parcel *in) {
Andreas Huber43c3e6c2011-01-05 12:17:08 -08002142 if (mDriver == NULL) {
Andreas Huberf9334412010-12-15 15:17:42 -08002143 return;
2144 }
2145
Andreas Huber43c3e6c2011-01-05 12:17:08 -08002146 sp<NuPlayerDriver> driver = mDriver.promote();
Andreas Huberf9334412010-12-15 15:17:42 -08002147
Andreas Huber43c3e6c2011-01-05 12:17:08 -08002148 if (driver == NULL) {
Andreas Huberf9334412010-12-15 15:17:42 -08002149 return;
2150 }
2151
Chong Zhangdcb89b32013-08-06 09:44:47 -07002152 driver->notifyListener(msg, ext1, ext2, in);
Andreas Huberf9334412010-12-15 15:17:42 -08002153}
2154
Chong Zhang7137ec72014-11-12 16:41:05 -08002155void NuPlayer::flushDecoder(bool audio, bool needShutdown) {
Andreas Huber14f76722013-01-15 09:04:18 -08002156 ALOGV("[%s] flushDecoder needShutdown=%d",
2157 audio ? "audio" : "video", needShutdown);
2158
Chong Zhang7137ec72014-11-12 16:41:05 -08002159 const sp<DecoderBase> &decoder = getDecoder(audio);
Lajos Molnar87603c02014-08-20 19:25:30 -07002160 if (decoder == NULL) {
Steve Blockdf64d152012-01-04 20:05:49 +00002161 ALOGI("flushDecoder %s without decoder present",
Andreas Huber6e3d3112011-11-28 12:36:11 -08002162 audio ? "audio" : "video");
Lajos Molnar87603c02014-08-20 19:25:30 -07002163 return;
Andreas Huber6e3d3112011-11-28 12:36:11 -08002164 }
2165
Andreas Huber1aef2112011-01-04 14:01:29 -08002166 // Make sure we don't continue to scan sources until we finish flushing.
2167 ++mScanSourcesGeneration;
Chong Zhang3b032b32015-04-17 15:49:06 -07002168 if (mScanSourcesPending) {
Toshikazu Saitocbcbb792015-09-15 14:53:01 +09002169 if (!needShutdown) {
2170 mDeferredActions.push_back(
2171 new SimpleAction(&NuPlayer::performScanSources));
2172 }
Chong Zhang3b032b32015-04-17 15:49:06 -07002173 mScanSourcesPending = false;
2174 }
Andreas Huber1aef2112011-01-04 14:01:29 -08002175
Chong Zhang7137ec72014-11-12 16:41:05 -08002176 decoder->signalFlush();
Andreas Huber1aef2112011-01-04 14:01:29 -08002177
2178 FlushStatus newStatus =
2179 needShutdown ? FLUSHING_DECODER_SHUTDOWN : FLUSHING_DECODER;
2180
Lajos Molnarfcd3e942015-03-31 10:06:48 -07002181 mFlushComplete[audio][false /* isDecoder */] = (mRenderer == NULL);
Andy Hung8d121d42014-10-03 09:53:53 -07002182 mFlushComplete[audio][true /* isDecoder */] = false;
Andreas Huber1aef2112011-01-04 14:01:29 -08002183 if (audio) {
Wei Jia53904f32014-07-29 10:22:53 -07002184 ALOGE_IF(mFlushingAudio != NONE,
2185 "audio flushDecoder() is called in state %d", mFlushingAudio);
Andreas Huber1aef2112011-01-04 14:01:29 -08002186 mFlushingAudio = newStatus;
Andreas Huber1aef2112011-01-04 14:01:29 -08002187 } else {
Wei Jia53904f32014-07-29 10:22:53 -07002188 ALOGE_IF(mFlushingVideo != NONE,
2189 "video flushDecoder() is called in state %d", mFlushingVideo);
Andreas Huber1aef2112011-01-04 14:01:29 -08002190 mFlushingVideo = newStatus;
Andreas Huber1aef2112011-01-04 14:01:29 -08002191 }
2192}
2193
Chong Zhangced1c2f2014-08-08 15:22:35 -07002194void NuPlayer::queueDecoderShutdown(
2195 bool audio, bool video, const sp<AMessage> &reply) {
2196 ALOGI("queueDecoderShutdown audio=%d, video=%d", audio, video);
Andreas Huber84066782011-08-16 09:34:26 -07002197
Chong Zhangced1c2f2014-08-08 15:22:35 -07002198 mDeferredActions.push_back(
Wei Jiafef808d2014-10-31 17:57:05 -07002199 new FlushDecoderAction(
2200 audio ? FLUSH_CMD_SHUTDOWN : FLUSH_CMD_NONE,
2201 video ? FLUSH_CMD_SHUTDOWN : FLUSH_CMD_NONE));
Andreas Huber84066782011-08-16 09:34:26 -07002202
Chong Zhangced1c2f2014-08-08 15:22:35 -07002203 mDeferredActions.push_back(
2204 new SimpleAction(&NuPlayer::performScanSources));
Andreas Huber84066782011-08-16 09:34:26 -07002205
Chong Zhangced1c2f2014-08-08 15:22:35 -07002206 mDeferredActions.push_back(new PostMessageAction(reply));
2207
2208 processDeferredActions();
Andreas Huber84066782011-08-16 09:34:26 -07002209}
2210
James Dong0d268a32012-08-31 12:18:27 -07002211status_t NuPlayer::setVideoScalingMode(int32_t mode) {
2212 mVideoScalingMode = mode;
Lajos Molnar1de1e252015-04-30 18:18:34 -07002213 if (mSurface != NULL) {
2214 status_t ret = native_window_set_scaling_mode(mSurface.get(), mVideoScalingMode);
James Dong0d268a32012-08-31 12:18:27 -07002215 if (ret != OK) {
2216 ALOGE("Failed to set scaling mode (%d): %s",
2217 -ret, strerror(-ret));
2218 return ret;
2219 }
2220 }
2221 return OK;
2222}
2223
Chong Zhangdcb89b32013-08-06 09:44:47 -07002224status_t NuPlayer::getTrackInfo(Parcel* reply) const {
Lajos Molnar1d15ab52015-03-04 16:46:34 -08002225 sp<AMessage> msg = new AMessage(kWhatGetTrackInfo, this);
Chong Zhangdcb89b32013-08-06 09:44:47 -07002226 msg->setPointer("reply", reply);
2227
2228 sp<AMessage> response;
2229 status_t err = msg->postAndAwaitResponse(&response);
2230 return err;
2231}
2232
Robert Shih7c4f0d72014-07-09 18:53:31 -07002233status_t NuPlayer::getSelectedTrack(int32_t type, Parcel* reply) const {
Lajos Molnar1d15ab52015-03-04 16:46:34 -08002234 sp<AMessage> msg = new AMessage(kWhatGetSelectedTrack, this);
Robert Shih7c4f0d72014-07-09 18:53:31 -07002235 msg->setPointer("reply", reply);
2236 msg->setInt32("type", type);
2237
2238 sp<AMessage> response;
2239 status_t err = msg->postAndAwaitResponse(&response);
2240 if (err == OK && response != NULL) {
2241 CHECK(response->findInt32("err", &err));
2242 }
2243 return err;
2244}
2245
Robert Shih6ffb1fd2014-10-29 16:24:32 -07002246status_t NuPlayer::selectTrack(size_t trackIndex, bool select, int64_t timeUs) {
Lajos Molnar1d15ab52015-03-04 16:46:34 -08002247 sp<AMessage> msg = new AMessage(kWhatSelectTrack, this);
Chong Zhangdcb89b32013-08-06 09:44:47 -07002248 msg->setSize("trackIndex", trackIndex);
2249 msg->setInt32("select", select);
Robert Shih6ffb1fd2014-10-29 16:24:32 -07002250 msg->setInt64("timeUs", timeUs);
Chong Zhangdcb89b32013-08-06 09:44:47 -07002251
2252 sp<AMessage> response;
2253 status_t err = msg->postAndAwaitResponse(&response);
2254
Chong Zhang404fced2014-06-11 14:45:31 -07002255 if (err != OK) {
2256 return err;
2257 }
2258
2259 if (!response->findInt32("err", &err)) {
2260 err = OK;
2261 }
2262
Chong Zhangdcb89b32013-08-06 09:44:47 -07002263 return err;
2264}
2265
Ronghua Wua73d9e02014-10-08 15:13:29 -07002266status_t NuPlayer::getCurrentPosition(int64_t *mediaUs) {
2267 sp<Renderer> renderer = mRenderer;
2268 if (renderer == NULL) {
2269 return NO_INIT;
2270 }
2271
2272 return renderer->getCurrentPosition(mediaUs);
2273}
2274
Ray Essickffc941f2018-05-18 11:08:37 -07002275void NuPlayer::getStats(Vector<sp<AMessage> > *trackStats) {
2276 CHECK(trackStats != NULL);
Praveen Chavane1e5d7a2015-05-19 19:09:48 -07002277
Ray Essickfcb8fd32018-08-07 09:39:38 -07002278 trackStats->clear();
Ray Essickffc941f2018-05-18 11:08:37 -07002279
Ray Essickfcb8fd32018-08-07 09:39:38 -07002280 Mutex::Autolock autoLock(mDecoderLock);
2281 if (mVideoDecoder != NULL) {
2282 trackStats->push_back(mVideoDecoder->getStats());
2283 }
2284 if (mAudioDecoder != NULL) {
2285 trackStats->push_back(mAudioDecoder->getStats());
2286 }
Ronghua Wua73d9e02014-10-08 15:13:29 -07002287}
2288
Marco Nelissenf0b72b52014-09-16 15:43:44 -07002289sp<MetaData> NuPlayer::getFileMeta() {
2290 return mSource->getFileFormatMeta();
2291}
2292
Ronghua Wuc8a70d32015-04-29 16:26:34 -07002293float NuPlayer::getFrameRate() {
2294 sp<MetaData> meta = mSource->getFormatMeta(false /* audio */);
2295 if (meta == NULL) {
2296 return 0;
2297 }
2298 int32_t rate;
2299 if (!meta->findInt32(kKeyFrameRate, &rate)) {
2300 // fall back to try file meta
2301 sp<MetaData> fileMeta = getFileMeta();
2302 if (fileMeta == NULL) {
2303 ALOGW("source has video meta but not file meta");
2304 return -1;
2305 }
2306 int32_t fileMetaRate;
2307 if (!fileMeta->findInt32(kKeyFrameRate, &fileMetaRate)) {
2308 return -1;
2309 }
2310 return fileMetaRate;
2311 }
2312 return rate;
2313}
2314
Andreas Huberb7c8e912012-11-27 15:02:53 -08002315void NuPlayer::schedulePollDuration() {
Lajos Molnar1d15ab52015-03-04 16:46:34 -08002316 sp<AMessage> msg = new AMessage(kWhatPollDuration, this);
Andreas Huberb7c8e912012-11-27 15:02:53 -08002317 msg->setInt32("generation", mPollDurationGeneration);
2318 msg->post();
2319}
2320
2321void NuPlayer::cancelPollDuration() {
2322 ++mPollDurationGeneration;
2323}
2324
Andreas Hubera1f8ab02012-11-30 10:53:22 -08002325void NuPlayer::processDeferredActions() {
2326 while (!mDeferredActions.empty()) {
2327 // We won't execute any deferred actions until we're no longer in
2328 // an intermediate state, i.e. one more more decoders are currently
2329 // flushing or shutting down.
2330
Andreas Hubera1f8ab02012-11-30 10:53:22 -08002331 if (mFlushingAudio != NONE || mFlushingVideo != NONE) {
2332 // We're currently flushing, postpone the reset until that's
2333 // completed.
2334
2335 ALOGV("postponing action mFlushingAudio=%d, mFlushingVideo=%d",
2336 mFlushingAudio, mFlushingVideo);
2337
2338 break;
2339 }
2340
2341 sp<Action> action = *mDeferredActions.begin();
2342 mDeferredActions.erase(mDeferredActions.begin());
2343
2344 action->execute(this);
2345 }
2346}
2347
Wei Jiac5de0912016-11-18 10:22:14 -08002348void NuPlayer::performSeek(int64_t seekTimeUs, MediaPlayerSeekMode mode) {
2349 ALOGV("performSeek seekTimeUs=%lld us (%.2f secs), mode=%d",
2350 (long long)seekTimeUs, seekTimeUs / 1E6, mode);
Andreas Hubera1f8ab02012-11-30 10:53:22 -08002351
Andy Hungadf34bf2014-09-03 18:22:22 -07002352 if (mSource == NULL) {
2353 // This happens when reset occurs right before the loop mode
2354 // asynchronously seeks to the start of the stream.
2355 LOG_ALWAYS_FATAL_IF(mAudioDecoder != NULL || mVideoDecoder != NULL,
2356 "mSource is NULL and decoders not NULL audio(%p) video(%p)",
2357 mAudioDecoder.get(), mVideoDecoder.get());
2358 return;
2359 }
Robert Shih1a5c8592015-08-04 18:07:44 -07002360 mPreviousSeekTimeUs = seekTimeUs;
Wei Jiac5de0912016-11-18 10:22:14 -08002361 mSource->seekTo(seekTimeUs, mode);
Robert Shihd3b0bbb2014-07-23 15:00:25 -07002362 ++mTimedTextGeneration;
Andreas Hubera1f8ab02012-11-30 10:53:22 -08002363
Andreas Hubera1f8ab02012-11-30 10:53:22 -08002364 // everything's flushed, continue playback.
2365}
2366
Wei Jiafef808d2014-10-31 17:57:05 -07002367void NuPlayer::performDecoderFlush(FlushCommand audio, FlushCommand video) {
2368 ALOGV("performDecoderFlush audio=%d, video=%d", audio, video);
Andreas Hubera1f8ab02012-11-30 10:53:22 -08002369
Wei Jiafef808d2014-10-31 17:57:05 -07002370 if ((audio == FLUSH_CMD_NONE || mAudioDecoder == NULL)
2371 && (video == FLUSH_CMD_NONE || mVideoDecoder == NULL)) {
Andreas Hubera1f8ab02012-11-30 10:53:22 -08002372 return;
2373 }
2374
Wei Jiafef808d2014-10-31 17:57:05 -07002375 if (audio != FLUSH_CMD_NONE && mAudioDecoder != NULL) {
2376 flushDecoder(true /* audio */, (audio == FLUSH_CMD_SHUTDOWN));
Andreas Hubera1f8ab02012-11-30 10:53:22 -08002377 }
2378
Wei Jiafef808d2014-10-31 17:57:05 -07002379 if (video != FLUSH_CMD_NONE && mVideoDecoder != NULL) {
2380 flushDecoder(false /* audio */, (video == FLUSH_CMD_SHUTDOWN));
Andreas Hubera1f8ab02012-11-30 10:53:22 -08002381 }
2382}
2383
2384void NuPlayer::performReset() {
2385 ALOGV("performReset");
2386
2387 CHECK(mAudioDecoder == NULL);
2388 CHECK(mVideoDecoder == NULL);
2389
Ray Essickeda32522018-02-28 12:08:28 -08002390 updatePlaybackTimer(true /* stopping */, "performReset");
2391 updateRebufferingTimer(true /* stopping */, true /* exiting */);
Ray Essick0d98c182017-11-09 13:42:42 -08002392
Andreas Hubera1f8ab02012-11-30 10:53:22 -08002393 cancelPollDuration();
2394
2395 ++mScanSourcesGeneration;
2396 mScanSourcesPending = false;
2397
Lajos Molnar09524832014-07-17 14:29:51 -07002398 if (mRendererLooper != NULL) {
2399 if (mRenderer != NULL) {
2400 mRendererLooper->unregisterHandler(mRenderer->id());
2401 }
2402 mRendererLooper->stop();
2403 mRendererLooper.clear();
2404 }
Andreas Hubera1f8ab02012-11-30 10:53:22 -08002405 mRenderer.clear();
Wei Jia57568df2014-09-22 10:16:29 -07002406 ++mRendererGeneration;
Andreas Hubera1f8ab02012-11-30 10:53:22 -08002407
2408 if (mSource != NULL) {
2409 mSource->stop();
Andreas Huberb5f25f02013-02-05 10:14:26 -08002410
Wei Jiac45a4b22016-04-15 15:30:23 -07002411 Mutex::Autolock autoLock(mSourceLock);
Andreas Hubera1f8ab02012-11-30 10:53:22 -08002412 mSource.clear();
2413 }
2414
2415 if (mDriver != NULL) {
2416 sp<NuPlayerDriver> driver = mDriver.promote();
2417 if (driver != NULL) {
2418 driver->notifyResetComplete();
2419 }
2420 }
Andreas Huber57a339c2012-12-03 11:18:00 -08002421
2422 mStarted = false;
Wei Jia8a092d32016-06-03 14:57:24 -07002423 mPrepared = false;
Ronghua Wu64c2d172015-10-07 16:52:19 -07002424 mResetting = false;
Robert Shih0c61a0d2015-07-06 15:09:10 -07002425 mSourceStarted = false;
Hassan Shojaniacefac142017-02-06 21:02:02 -08002426
2427 // Modular DRM
2428 if (mCrypto != NULL) {
2429 // decoders will be flushed before this so their mCrypto would go away on their own
2430 // TODO change to ALOGV
2431 ALOGD("performReset mCrypto: %p (%d)", mCrypto.get(),
2432 (mCrypto != NULL ? mCrypto->getStrongCount() : 0));
2433 mCrypto.clear();
2434 }
2435 mIsDrmProtected = false;
Andreas Hubera1f8ab02012-11-30 10:53:22 -08002436}
2437
2438void NuPlayer::performScanSources() {
2439 ALOGV("performScanSources");
2440
Andreas Huber57a339c2012-12-03 11:18:00 -08002441 if (!mStarted) {
2442 return;
2443 }
2444
Andreas Hubera1f8ab02012-11-30 10:53:22 -08002445 if (mAudioDecoder == NULL || mVideoDecoder == NULL) {
2446 postScanSources();
2447 }
2448}
2449
Lajos Molnar1de1e252015-04-30 18:18:34 -07002450void NuPlayer::performSetSurface(const sp<Surface> &surface) {
Andreas Huber57a339c2012-12-03 11:18:00 -08002451 ALOGV("performSetSurface");
2452
Lajos Molnar1de1e252015-04-30 18:18:34 -07002453 mSurface = surface;
Andreas Huber57a339c2012-12-03 11:18:00 -08002454
2455 // XXX - ignore error from setVideoScalingMode for now
2456 setVideoScalingMode(mVideoScalingMode);
Chong Zhang13d6faa2014-08-22 15:35:28 -07002457
2458 if (mDriver != NULL) {
2459 sp<NuPlayerDriver> driver = mDriver.promote();
2460 if (driver != NULL) {
2461 driver->notifySetSurfaceComplete();
2462 }
2463 }
Andreas Huber57a339c2012-12-03 11:18:00 -08002464}
2465
Chong Zhangf8d71772014-11-26 15:08:34 -08002466void NuPlayer::performResumeDecoders(bool needNotify) {
2467 if (needNotify) {
2468 mResumePending = true;
2469 if (mVideoDecoder == NULL) {
2470 // if audio-only, we can notify seek complete now,
2471 // as the resume operation will be relatively fast.
2472 finishResume();
2473 }
2474 }
2475
Chong Zhang7137ec72014-11-12 16:41:05 -08002476 if (mVideoDecoder != NULL) {
Chong Zhangf8d71772014-11-26 15:08:34 -08002477 // When there is continuous seek, MediaPlayer will cache the seek
2478 // position, and send down new seek request when previous seek is
2479 // complete. Let's wait for at least one video output frame before
2480 // notifying seek complete, so that the video thumbnail gets updated
2481 // when seekbar is dragged.
2482 mVideoDecoder->signalResume(needNotify);
Chong Zhang7137ec72014-11-12 16:41:05 -08002483 }
2484
2485 if (mAudioDecoder != NULL) {
Chong Zhangf8d71772014-11-26 15:08:34 -08002486 mAudioDecoder->signalResume(false /* needNotify */);
2487 }
2488}
2489
2490void NuPlayer::finishResume() {
2491 if (mResumePending) {
2492 mResumePending = false;
Wei Jia1061c9c2015-05-19 16:02:17 -07002493 notifyDriverSeekComplete();
2494 }
2495}
2496
2497void NuPlayer::notifyDriverSeekComplete() {
2498 if (mDriver != NULL) {
2499 sp<NuPlayerDriver> driver = mDriver.promote();
2500 if (driver != NULL) {
2501 driver->notifySeekComplete();
Chong Zhangf8d71772014-11-26 15:08:34 -08002502 }
Chong Zhang7137ec72014-11-12 16:41:05 -08002503 }
2504}
2505
Andreas Huber9575c962013-02-05 13:59:56 -08002506void NuPlayer::onSourceNotify(const sp<AMessage> &msg) {
2507 int32_t what;
2508 CHECK(msg->findInt32("what", &what));
2509
2510 switch (what) {
Lajos Molnarfcd3e942015-03-31 10:06:48 -07002511 case Source::kWhatInstantiateSecureDecoders:
2512 {
2513 if (mSource == NULL) {
2514 // This is a stale notification from a source that was
2515 // asynchronously preparing when the client called reset().
2516 // We handled the reset, the source is gone.
2517 break;
2518 }
2519
2520 sp<AMessage> reply;
2521 CHECK(msg->findMessage("reply", &reply));
2522 status_t err = onInstantiateSecureDecoders();
2523 reply->setInt32("err", err);
2524 reply->post();
2525 break;
2526 }
2527
Andreas Huber9575c962013-02-05 13:59:56 -08002528 case Source::kWhatPrepared:
2529 {
Hassan Shojaniacefac142017-02-06 21:02:02 -08002530 ALOGV("NuPlayer::onSourceNotify Source::kWhatPrepared source: %p", mSource.get());
Andreas Huberb5f28d42013-04-25 15:11:19 -07002531 if (mSource == NULL) {
2532 // This is a stale notification from a source that was
2533 // asynchronously preparing when the client called reset().
2534 // We handled the reset, the source is gone.
2535 break;
2536 }
2537
Andreas Huberec0c5972013-02-05 14:47:13 -08002538 int32_t err;
2539 CHECK(msg->findInt32("err", &err));
2540
Lajos Molnarfcd3e942015-03-31 10:06:48 -07002541 if (err != OK) {
2542 // shut down potential secure codecs in case client never calls reset
2543 mDeferredActions.push_back(
2544 new FlushDecoderAction(FLUSH_CMD_SHUTDOWN /* audio */,
2545 FLUSH_CMD_SHUTDOWN /* video */));
2546 processDeferredActions();
Wei Jia8a092d32016-06-03 14:57:24 -07002547 } else {
2548 mPrepared = true;
Lajos Molnarfcd3e942015-03-31 10:06:48 -07002549 }
2550
Andreas Huber9575c962013-02-05 13:59:56 -08002551 sp<NuPlayerDriver> driver = mDriver.promote();
2552 if (driver != NULL) {
Marco Nelissendd114d12014-05-28 15:23:14 -07002553 // notify duration first, so that it's definitely set when
2554 // the app received the "prepare complete" callback.
2555 int64_t durationUs;
2556 if (mSource->getDuration(&durationUs) == OK) {
2557 driver->notifyDuration(durationUs);
2558 }
Andreas Huberec0c5972013-02-05 14:47:13 -08002559 driver->notifyPrepareCompleted(err);
Andreas Huber9575c962013-02-05 13:59:56 -08002560 }
Andreas Huber99759402013-04-01 14:28:31 -07002561
Andreas Huber9575c962013-02-05 13:59:56 -08002562 break;
2563 }
2564
Hassan Shojaniacefac142017-02-06 21:02:02 -08002565 // Modular DRM
2566 case Source::kWhatDrmInfo:
2567 {
2568 Parcel parcel;
2569 sp<ABuffer> drmInfo;
2570 CHECK(msg->findBuffer("drmInfo", &drmInfo));
2571 parcel.setData(drmInfo->data(), drmInfo->size());
2572
2573 ALOGV("onSourceNotify() kWhatDrmInfo MEDIA_DRM_INFO drmInfo: %p parcel size: %zu",
2574 drmInfo.get(), parcel.dataSize());
2575
2576 notifyListener(MEDIA_DRM_INFO, 0 /* ext1 */, 0 /* ext2 */, &parcel);
2577
2578 break;
2579 }
2580
Andreas Huber9575c962013-02-05 13:59:56 -08002581 case Source::kWhatFlagsChanged:
2582 {
2583 uint32_t flags;
2584 CHECK(msg->findInt32("flags", (int32_t *)&flags));
2585
Chong Zhang4b7069d2013-09-11 12:52:43 -07002586 sp<NuPlayerDriver> driver = mDriver.promote();
2587 if (driver != NULL) {
Hassan Shojaniacefac142017-02-06 21:02:02 -08002588
2589 ALOGV("onSourceNotify() kWhatFlagsChanged FLAG_CAN_PAUSE: %d "
2590 "FLAG_CAN_SEEK_BACKWARD: %d \n\t\t\t\t FLAG_CAN_SEEK_FORWARD: %d "
2591 "FLAG_CAN_SEEK: %d FLAG_DYNAMIC_DURATION: %d \n"
2592 "\t\t\t\t FLAG_SECURE: %d FLAG_PROTECTED: %d",
2593 (flags & Source::FLAG_CAN_PAUSE) != 0,
2594 (flags & Source::FLAG_CAN_SEEK_BACKWARD) != 0,
2595 (flags & Source::FLAG_CAN_SEEK_FORWARD) != 0,
2596 (flags & Source::FLAG_CAN_SEEK) != 0,
2597 (flags & Source::FLAG_DYNAMIC_DURATION) != 0,
2598 (flags & Source::FLAG_SECURE) != 0,
2599 (flags & Source::FLAG_PROTECTED) != 0);
2600
Wei Jia895651b2014-12-10 17:31:52 -08002601 if ((flags & NuPlayer::Source::FLAG_CAN_SEEK) == 0) {
2602 driver->notifyListener(
2603 MEDIA_INFO, MEDIA_INFO_NOT_SEEKABLE, 0);
2604 }
Chong Zhang4b7069d2013-09-11 12:52:43 -07002605 driver->notifyFlagsChanged(flags);
2606 }
2607
Andreas Huber9575c962013-02-05 13:59:56 -08002608 if ((mSourceFlags & Source::FLAG_DYNAMIC_DURATION)
2609 && (!(flags & Source::FLAG_DYNAMIC_DURATION))) {
2610 cancelPollDuration();
2611 } else if (!(mSourceFlags & Source::FLAG_DYNAMIC_DURATION)
2612 && (flags & Source::FLAG_DYNAMIC_DURATION)
2613 && (mAudioDecoder != NULL || mVideoDecoder != NULL)) {
2614 schedulePollDuration();
2615 }
2616
2617 mSourceFlags = flags;
2618 break;
2619 }
2620
2621 case Source::kWhatVideoSizeChanged:
2622 {
Chong Zhangced1c2f2014-08-08 15:22:35 -07002623 sp<AMessage> format;
2624 CHECK(msg->findMessage("format", &format));
Andreas Huber9575c962013-02-05 13:59:56 -08002625
Chong Zhangced1c2f2014-08-08 15:22:35 -07002626 updateVideoSize(format);
Andreas Huber9575c962013-02-05 13:59:56 -08002627 break;
2628 }
2629
Chong Zhang2a3cc9a2014-08-21 17:48:26 -07002630 case Source::kWhatBufferingUpdate:
2631 {
2632 int32_t percentage;
2633 CHECK(msg->findInt32("percentage", &percentage));
2634
2635 notifyListener(MEDIA_BUFFERING_UPDATE, percentage, 0);
2636 break;
2637 }
2638
Chong Zhangefbb6192015-01-30 17:13:27 -08002639 case Source::kWhatPauseOnBufferingStart:
2640 {
2641 // ignore if not playing
Chong Zhang8a048332015-05-06 15:16:28 -07002642 if (mStarted) {
Chong Zhangefbb6192015-01-30 17:13:27 -08002643 ALOGI("buffer low, pausing...");
2644
Ray Essick58e0f7a2017-11-21 10:59:38 -08002645 startRebufferingTimer();
Chong Zhang8a048332015-05-06 15:16:28 -07002646 mPausedForBuffering = true;
Chong Zhangefbb6192015-01-30 17:13:27 -08002647 onPause();
2648 }
Wei Jiabfe82072016-05-20 09:21:50 -07002649 notifyListener(MEDIA_INFO, MEDIA_INFO_BUFFERING_START, 0);
Roger Jönssonb50e83e2013-01-21 16:26:41 +01002650 break;
2651 }
2652
Chong Zhangefbb6192015-01-30 17:13:27 -08002653 case Source::kWhatResumeOnBufferingEnd:
2654 {
2655 // ignore if not playing
Chong Zhang8a048332015-05-06 15:16:28 -07002656 if (mStarted) {
Chong Zhangefbb6192015-01-30 17:13:27 -08002657 ALOGI("buffer ready, resuming...");
2658
Ray Essickeda32522018-02-28 12:08:28 -08002659 updateRebufferingTimer(true /* stopping */, false /* exiting */);
Chong Zhang8a048332015-05-06 15:16:28 -07002660 mPausedForBuffering = false;
2661
2662 // do not resume yet if client didn't unpause
2663 if (!mPausedByClient) {
2664 onResume();
2665 }
Chong Zhangefbb6192015-01-30 17:13:27 -08002666 }
Wei Jia3bed45a2016-02-17 11:06:47 -08002667 notifyListener(MEDIA_INFO, MEDIA_INFO_BUFFERING_END, 0);
Roger Jönssonb50e83e2013-01-21 16:26:41 +01002668 break;
2669 }
2670
Chong Zhangefbb6192015-01-30 17:13:27 -08002671 case Source::kWhatCacheStats:
2672 {
2673 int32_t kbps;
2674 CHECK(msg->findInt32("bandwidth", &kbps));
2675
2676 notifyListener(MEDIA_INFO, MEDIA_INFO_NETWORK_BANDWIDTH, kbps);
2677 break;
2678 }
2679
Chong Zhangdcb89b32013-08-06 09:44:47 -07002680 case Source::kWhatSubtitleData:
2681 {
2682 sp<ABuffer> buffer;
2683 CHECK(msg->findBuffer("buffer", &buffer));
2684
Chong Zhang404fced2014-06-11 14:45:31 -07002685 sendSubtitleData(buffer, 0 /* baseIndex */);
Chong Zhangdcb89b32013-08-06 09:44:47 -07002686 break;
2687 }
2688
Robert Shih08528432015-04-08 09:06:54 -07002689 case Source::kWhatTimedMetaData:
2690 {
2691 sp<ABuffer> buffer;
2692 if (!msg->findBuffer("buffer", &buffer)) {
2693 notifyListener(MEDIA_INFO, MEDIA_INFO_METADATA_UPDATE, 0);
2694 } else {
2695 sendTimedMetaData(buffer);
2696 }
2697 break;
2698 }
2699
Robert Shihd3b0bbb2014-07-23 15:00:25 -07002700 case Source::kWhatTimedTextData:
2701 {
2702 int32_t generation;
2703 if (msg->findInt32("generation", &generation)
2704 && generation != mTimedTextGeneration) {
2705 break;
2706 }
2707
2708 sp<ABuffer> buffer;
2709 CHECK(msg->findBuffer("buffer", &buffer));
2710
2711 sp<NuPlayerDriver> driver = mDriver.promote();
2712 if (driver == NULL) {
2713 break;
2714 }
2715
2716 int posMs;
2717 int64_t timeUs, posUs;
2718 driver->getCurrentPosition(&posMs);
Chih-Hung Hsieh62309d52018-12-11 13:54:02 -08002719 posUs = (int64_t) posMs * 1000LL;
Robert Shihd3b0bbb2014-07-23 15:00:25 -07002720 CHECK(buffer->meta()->findInt64("timeUs", &timeUs));
2721
2722 if (posUs < timeUs) {
2723 if (!msg->findInt32("generation", &generation)) {
2724 msg->setInt32("generation", mTimedTextGeneration);
2725 }
2726 msg->post(timeUs - posUs);
2727 } else {
2728 sendTimedTextData(buffer);
2729 }
2730 break;
2731 }
2732
Andreas Huber14f76722013-01-15 09:04:18 -08002733 case Source::kWhatQueueDecoderShutdown:
2734 {
2735 int32_t audio, video;
2736 CHECK(msg->findInt32("audio", &audio));
2737 CHECK(msg->findInt32("video", &video));
2738
2739 sp<AMessage> reply;
2740 CHECK(msg->findMessage("reply", &reply));
2741
2742 queueDecoderShutdown(audio, video, reply);
2743 break;
2744 }
2745
Ronghua Wu80276872014-08-28 15:50:29 -07002746 case Source::kWhatDrmNoLicense:
2747 {
2748 notifyListener(MEDIA_ERROR, MEDIA_ERROR_UNKNOWN, ERROR_DRM_NO_LICENSE);
2749 break;
2750 }
2751
Kim Sungyeond3c6b322018-03-02 14:41:19 +09002752 case Source::kWhatIMSRxNotice:
2753 {
2754 sp<AMessage> IMSRxNotice;
2755 CHECK(msg->findMessage("message", &IMSRxNotice));
2756 sendIMSRxNotice(IMSRxNotice);
2757 break;
2758 }
2759
Andreas Huber9575c962013-02-05 13:59:56 -08002760 default:
2761 TRESPASS();
2762 }
2763}
2764
Chong Zhanga7fa1d92014-06-11 14:49:23 -07002765void NuPlayer::onClosedCaptionNotify(const sp<AMessage> &msg) {
2766 int32_t what;
2767 CHECK(msg->findInt32("what", &what));
2768
2769 switch (what) {
2770 case NuPlayer::CCDecoder::kWhatClosedCaptionData:
2771 {
2772 sp<ABuffer> buffer;
2773 CHECK(msg->findBuffer("buffer", &buffer));
2774
2775 size_t inbandTracks = 0;
2776 if (mSource != NULL) {
2777 inbandTracks = mSource->getTrackCount();
2778 }
2779
2780 sendSubtitleData(buffer, inbandTracks);
2781 break;
2782 }
2783
2784 case NuPlayer::CCDecoder::kWhatTrackAdded:
2785 {
2786 notifyListener(MEDIA_INFO, MEDIA_INFO_METADATA_UPDATE, 0);
2787
2788 break;
2789 }
2790
2791 default:
2792 TRESPASS();
2793 }
2794
2795
2796}
2797
Chong Zhang404fced2014-06-11 14:45:31 -07002798void NuPlayer::sendSubtitleData(const sp<ABuffer> &buffer, int32_t baseIndex) {
2799 int32_t trackIndex;
2800 int64_t timeUs, durationUs;
Robert Shihd83d4f42018-02-24 19:02:46 -08002801 CHECK(buffer->meta()->findInt32("track-index", &trackIndex));
Chong Zhang404fced2014-06-11 14:45:31 -07002802 CHECK(buffer->meta()->findInt64("timeUs", &timeUs));
2803 CHECK(buffer->meta()->findInt64("durationUs", &durationUs));
2804
2805 Parcel in;
2806 in.writeInt32(trackIndex + baseIndex);
2807 in.writeInt64(timeUs);
2808 in.writeInt64(durationUs);
2809 in.writeInt32(buffer->size());
2810 in.writeInt32(buffer->size());
2811 in.write(buffer->data(), buffer->size());
2812
2813 notifyListener(MEDIA_SUBTITLE_DATA, 0, 0, &in);
2814}
Robert Shihd3b0bbb2014-07-23 15:00:25 -07002815
Robert Shih08528432015-04-08 09:06:54 -07002816void NuPlayer::sendTimedMetaData(const sp<ABuffer> &buffer) {
2817 int64_t timeUs;
2818 CHECK(buffer->meta()->findInt64("timeUs", &timeUs));
2819
2820 Parcel in;
2821 in.writeInt64(timeUs);
2822 in.writeInt32(buffer->size());
2823 in.writeInt32(buffer->size());
2824 in.write(buffer->data(), buffer->size());
2825
2826 notifyListener(MEDIA_META_DATA, 0, 0, &in);
2827}
2828
Robert Shihd3b0bbb2014-07-23 15:00:25 -07002829void NuPlayer::sendTimedTextData(const sp<ABuffer> &buffer) {
2830 const void *data;
2831 size_t size = 0;
2832 int64_t timeUs;
Marco Nelissen55e2f4c2015-09-04 15:57:15 -07002833 int32_t flag = TextDescriptions::IN_BAND_TEXT_3GPP;
Robert Shihd3b0bbb2014-07-23 15:00:25 -07002834
2835 AString mime;
2836 CHECK(buffer->meta()->findString("mime", &mime));
2837 CHECK(strcasecmp(mime.c_str(), MEDIA_MIMETYPE_TEXT_3GPP) == 0);
2838
2839 data = buffer->data();
2840 size = buffer->size();
2841
2842 Parcel parcel;
2843 if (size > 0) {
2844 CHECK(buffer->meta()->findInt64("timeUs", &timeUs));
Marco Nelissen55e2f4c2015-09-04 15:57:15 -07002845 int32_t global = 0;
2846 if (buffer->meta()->findInt32("global", &global) && global) {
2847 flag |= TextDescriptions::GLOBAL_DESCRIPTIONS;
2848 } else {
2849 flag |= TextDescriptions::LOCAL_DESCRIPTIONS;
2850 }
Robert Shihd3b0bbb2014-07-23 15:00:25 -07002851 TextDescriptions::getParcelOfDescriptions(
2852 (const uint8_t *)data, size, flag, timeUs / 1000, &parcel);
2853 }
2854
2855 if ((parcel.dataSize() > 0)) {
2856 notifyListener(MEDIA_TIMED_TEXT, 0, 0, &parcel);
2857 } else { // send an empty timed text
2858 notifyListener(MEDIA_TIMED_TEXT, 0, 0);
2859 }
2860}
Hassan Shojaniacefac142017-02-06 21:02:02 -08002861
Kim Sungyeond3c6b322018-03-02 14:41:19 +09002862void NuPlayer::sendIMSRxNotice(const sp<AMessage> &msg) {
Byeongjo Parkb0225aa2018-04-20 14:00:15 +09002863 int32_t payloadType;
Kim Sungyeond3c6b322018-03-02 14:41:19 +09002864
Kim Sungyeond3c6b322018-03-02 14:41:19 +09002865 CHECK(msg->findInt32("payload-type", &payloadType));
Kim Sungyeond3c6b322018-03-02 14:41:19 +09002866
Kim Sungyeonbf5d9322021-07-16 16:36:37 +09002867 int32_t rtpSeq = 0, rtpTime = 0;
2868 int64_t ntpTime = 0, recvTimeUs = 0;
2869
Kim Sungyeond3c6b322018-03-02 14:41:19 +09002870 Parcel in;
2871 in.writeInt32(payloadType);
Kim Sungyeond3c6b322018-03-02 14:41:19 +09002872
2873 switch (payloadType) {
Kim Sungyeonbf5d9322021-07-16 16:36:37 +09002874 case ARTPSource::RTP_FIRST_PACKET:
2875 {
2876 CHECK(msg->findInt32("rtp-time", &rtpTime));
2877 CHECK(msg->findInt32("rtp-seq-num", &rtpSeq));
2878 CHECK(msg->findInt64("recv-time-us", &recvTimeUs));
2879 in.writeInt32(rtpTime);
2880 in.writeInt32(rtpSeq);
2881 in.writeInt32(recvTimeUs >> 32);
2882 in.writeInt32(recvTimeUs & 0xFFFFFFFF);
2883 break;
2884 }
2885 case ARTPSource::RTCP_FIRST_PACKET:
2886 {
2887 CHECK(msg->findInt64("recv-time-us", &recvTimeUs));
2888 in.writeInt32(recvTimeUs >> 32);
2889 in.writeInt32(recvTimeUs & 0xFFFFFFFF);
2890 break;
2891 }
2892 case ARTPSource::RTCP_SR:
2893 {
2894 CHECK(msg->findInt32("rtp-time", &rtpTime));
2895 CHECK(msg->findInt64("ntp-time", &ntpTime));
2896 CHECK(msg->findInt64("recv-time-us", &recvTimeUs));
2897 in.writeInt32(rtpTime);
2898 in.writeInt32(ntpTime >> 32);
2899 in.writeInt32(ntpTime & 0xFFFFFFFF);
2900 in.writeInt32(recvTimeUs >> 32);
2901 in.writeInt32(recvTimeUs & 0xFFFFFFFF);
2902 break;
2903 }
Byeongjo Park195d6142021-11-12 19:27:09 +09002904 case ARTPSource::RTCP_RR:
2905 {
2906 int64_t recvTimeUs;
2907 int32_t senderId;
2908 int32_t ssrc;
2909 int32_t fraction;
2910 int32_t lost;
2911 int32_t lastSeq;
2912 int32_t jitter;
2913 int32_t lsr;
2914 int32_t dlsr;
2915 CHECK(msg->findInt64("recv-time-us", &recvTimeUs));
2916 CHECK(msg->findInt32("rtcp-rr-ssrc", &senderId));
2917 CHECK(msg->findInt32("rtcp-rrb-ssrc", &ssrc));
2918 CHECK(msg->findInt32("rtcp-rrb-fraction", &fraction));
2919 CHECK(msg->findInt32("rtcp-rrb-lost", &lost));
2920 CHECK(msg->findInt32("rtcp-rrb-lastSeq", &lastSeq));
2921 CHECK(msg->findInt32("rtcp-rrb-jitter", &jitter));
2922 CHECK(msg->findInt32("rtcp-rrb-lsr", &lsr));
2923 CHECK(msg->findInt32("rtcp-rrb-dlsr", &dlsr));
2924 in.writeInt32(recvTimeUs >> 32);
2925 in.writeInt32(recvTimeUs & 0xFFFFFFFF);
2926 in.writeInt32(senderId);
2927 in.writeInt32(ssrc);
2928 in.writeInt32(fraction);
2929 in.writeInt32(lost);
2930 in.writeInt32(lastSeq);
2931 in.writeInt32(jitter);
2932 in.writeInt32(lsr);
2933 in.writeInt32(dlsr);
2934 break;
2935 }
Kim Sungyeon89d5bc62020-03-17 21:39:33 +09002936 case ARTPSource::RTCP_TSFB: // RTCP TSFB
2937 case ARTPSource::RTCP_PSFB: // RTCP PSFB
2938 case ARTPSource::RTP_AUTODOWN:
Kim Sungyeond3c6b322018-03-02 14:41:19 +09002939 {
Byeongjo Parkb0225aa2018-04-20 14:00:15 +09002940 int32_t feedbackType, id;
2941 CHECK(msg->findInt32("feedback-type", &feedbackType));
2942 CHECK(msg->findInt32("sender", &id));
2943 in.writeInt32(feedbackType);
2944 in.writeInt32(id);
Kim Sungyeon89d5bc62020-03-17 21:39:33 +09002945 if (payloadType == ARTPSource::RTCP_TSFB) {
Byeongjo Parkb0225aa2018-04-20 14:00:15 +09002946 int32_t bitrate;
2947 CHECK(msg->findInt32("bit-rate", &bitrate));
2948 in.writeInt32(bitrate);
2949 }
Kim Sungyeond3c6b322018-03-02 14:41:19 +09002950 break;
2951 }
Kim Sungyeon89d5bc62020-03-17 21:39:33 +09002952 case ARTPSource::RTP_QUALITY:
2953 case ARTPSource::RTP_QUALITY_EMC:
Kim Sungyeon23b23932019-07-18 17:48:32 +09002954 {
2955 int32_t feedbackType, bitrate;
2956 int32_t highestSeqNum, baseSeqNum, prevExpected;
2957 int32_t numBufRecv, prevNumBufRecv;
Kim Sungyeonbf5d9322021-07-16 16:36:37 +09002958 int32_t latestRtpTime, jbTimeMs, rtpRtcpSrTimeGapMs;
2959 int64_t recvTimeUs;
Kim Sungyeon23b23932019-07-18 17:48:32 +09002960 CHECK(msg->findInt32("feedback-type", &feedbackType));
2961 CHECK(msg->findInt32("bit-rate", &bitrate));
2962 CHECK(msg->findInt32("highest-seq-num", &highestSeqNum));
2963 CHECK(msg->findInt32("base-seq-num", &baseSeqNum));
2964 CHECK(msg->findInt32("prev-expected", &prevExpected));
2965 CHECK(msg->findInt32("num-buf-recv", &numBufRecv));
2966 CHECK(msg->findInt32("prev-num-buf-recv", &prevNumBufRecv));
Kim Sungyeonbf5d9322021-07-16 16:36:37 +09002967 CHECK(msg->findInt32("latest-rtp-time", &latestRtpTime));
2968 CHECK(msg->findInt64("recv-time-us", &recvTimeUs));
2969 CHECK(msg->findInt32("rtp-jitter-time-ms", &jbTimeMs));
2970 CHECK(msg->findInt32("rtp-rtcpsr-time-gap-ms", &rtpRtcpSrTimeGapMs));
Kim Sungyeon23b23932019-07-18 17:48:32 +09002971 in.writeInt32(feedbackType);
2972 in.writeInt32(bitrate);
2973 in.writeInt32(highestSeqNum);
2974 in.writeInt32(baseSeqNum);
2975 in.writeInt32(prevExpected);
2976 in.writeInt32(numBufRecv);
2977 in.writeInt32(prevNumBufRecv);
Kim Sungyeonbf5d9322021-07-16 16:36:37 +09002978 in.writeInt32(latestRtpTime);
2979 in.writeInt32(recvTimeUs >> 32);
2980 in.writeInt32(recvTimeUs & 0xFFFFFFFF);
2981 in.writeInt32(jbTimeMs);
2982 in.writeInt32(rtpRtcpSrTimeGapMs);
Kim Sungyeon23b23932019-07-18 17:48:32 +09002983 break;
2984 }
Kim Sungyeon89d5bc62020-03-17 21:39:33 +09002985 case ARTPSource::RTP_CVO:
Kim Sungyeond3c6b322018-03-02 14:41:19 +09002986 {
Byeongjo Parkb0225aa2018-04-20 14:00:15 +09002987 int32_t cvo;
2988 CHECK(msg->findInt32("cvo", &cvo));
2989 in.writeInt32(cvo);
Kim Sungyeond3c6b322018-03-02 14:41:19 +09002990 break;
2991 }
2992 default:
2993 break;
2994 }
2995
2996 notifyListener(MEDIA_IMS_RX_NOTICE, 0, 0, &in);
2997}
2998
Hassan Shojaniaff63de72017-04-26 15:10:42 -07002999const char *NuPlayer::getDataSourceType() {
3000 switch (mDataSourceType) {
3001 case DATA_SOURCE_TYPE_HTTP_LIVE:
3002 return "HTTPLive";
3003
Byeongjo Park5e27b1b2018-07-12 15:36:03 +09003004 case DATA_SOURCE_TYPE_RTP:
3005 return "RTP";
3006
Hassan Shojaniaff63de72017-04-26 15:10:42 -07003007 case DATA_SOURCE_TYPE_RTSP:
3008 return "RTSP";
3009
3010 case DATA_SOURCE_TYPE_GENERIC_URL:
3011 return "GenURL";
3012
3013 case DATA_SOURCE_TYPE_GENERIC_FD:
3014 return "GenFD";
3015
3016 case DATA_SOURCE_TYPE_MEDIA:
3017 return "Media";
3018
3019 case DATA_SOURCE_TYPE_STREAM:
3020 return "Stream";
3021
3022 case DATA_SOURCE_TYPE_NONE:
3023 default:
3024 return "None";
3025 }
3026 }
3027
Hassan Shojaniacefac142017-02-06 21:02:02 -08003028// Modular DRM begin
3029status_t NuPlayer::prepareDrm(const uint8_t uuid[16], const Vector<uint8_t> &drmSessionId)
3030{
3031 ALOGV("prepareDrm ");
3032
3033 // Passing to the looper anyway; called in a pre-config prepared state so no race on mCrypto
3034 sp<AMessage> msg = new AMessage(kWhatPrepareDrm, this);
3035 // synchronous call so just passing the address but with local copies of "const" args
3036 uint8_t UUID[16];
3037 memcpy(UUID, uuid, sizeof(UUID));
3038 Vector<uint8_t> sessionId = drmSessionId;
3039 msg->setPointer("uuid", (void*)UUID);
3040 msg->setPointer("drmSessionId", (void*)&sessionId);
3041
3042 sp<AMessage> response;
3043 status_t status = msg->postAndAwaitResponse(&response);
3044
3045 if (status == OK && response != NULL) {
3046 CHECK(response->findInt32("status", &status));
3047 ALOGV("prepareDrm ret: %d ", status);
3048 } else {
3049 ALOGE("prepareDrm err: %d", status);
3050 }
3051
3052 return status;
3053}
3054
3055status_t NuPlayer::releaseDrm()
3056{
3057 ALOGV("releaseDrm ");
3058
3059 sp<AMessage> msg = new AMessage(kWhatReleaseDrm, this);
3060
3061 sp<AMessage> response;
3062 status_t status = msg->postAndAwaitResponse(&response);
3063
3064 if (status == OK && response != NULL) {
3065 CHECK(response->findInt32("status", &status));
3066 ALOGV("releaseDrm ret: %d ", status);
3067 } else {
3068 ALOGE("releaseDrm err: %d", status);
3069 }
3070
3071 return status;
3072}
3073
3074status_t NuPlayer::onPrepareDrm(const sp<AMessage> &msg)
3075{
3076 // TODO change to ALOGV
3077 ALOGD("onPrepareDrm ");
3078
3079 status_t status = INVALID_OPERATION;
3080 if (mSource == NULL) {
3081 ALOGE("onPrepareDrm: No source. onPrepareDrm failed with %d.", status);
3082 return status;
3083 }
3084
3085 uint8_t *uuid;
3086 Vector<uint8_t> *drmSessionId;
3087 CHECK(msg->findPointer("uuid", (void**)&uuid));
3088 CHECK(msg->findPointer("drmSessionId", (void**)&drmSessionId));
3089
3090 status = OK;
3091 sp<ICrypto> crypto = NULL;
3092
3093 status = mSource->prepareDrm(uuid, *drmSessionId, &crypto);
3094 if (crypto == NULL) {
3095 ALOGE("onPrepareDrm: mSource->prepareDrm failed. status: %d", status);
3096 return status;
3097 }
3098 ALOGV("onPrepareDrm: mSource->prepareDrm succeeded");
3099
3100 if (mCrypto != NULL) {
3101 ALOGE("onPrepareDrm: Unexpected. Already having mCrypto: %p (%d)",
3102 mCrypto.get(), mCrypto->getStrongCount());
3103 mCrypto.clear();
3104 }
3105
3106 mCrypto = crypto;
3107 mIsDrmProtected = true;
3108 // TODO change to ALOGV
3109 ALOGD("onPrepareDrm: mCrypto: %p (%d)", mCrypto.get(),
3110 (mCrypto != NULL ? mCrypto->getStrongCount() : 0));
3111
3112 return status;
3113}
3114
3115status_t NuPlayer::onReleaseDrm()
3116{
3117 // TODO change to ALOGV
3118 ALOGD("onReleaseDrm ");
3119
Hassan Shojania50b20c92017-02-16 18:28:58 -08003120 if (!mIsDrmProtected) {
3121 ALOGW("onReleaseDrm: Unexpected. mIsDrmProtected is already false.");
3122 }
3123
3124 mIsDrmProtected = false;
Hassan Shojaniacefac142017-02-06 21:02:02 -08003125
3126 status_t status;
3127 if (mCrypto != NULL) {
Hassan Shojania355e8472017-05-12 10:33:16 -07003128 // notifying the source first before removing crypto from codec
3129 if (mSource != NULL) {
3130 mSource->releaseDrm();
3131 }
3132
Hassan Shojaniacefac142017-02-06 21:02:02 -08003133 status=OK;
3134 // first making sure the codecs have released their crypto reference
3135 const sp<DecoderBase> &videoDecoder = getDecoder(false/*audio*/);
3136 if (videoDecoder != NULL) {
3137 status = videoDecoder->releaseCrypto();
3138 ALOGV("onReleaseDrm: video decoder ret: %d", status);
3139 }
3140
3141 const sp<DecoderBase> &audioDecoder = getDecoder(true/*audio*/);
3142 if (audioDecoder != NULL) {
3143 status_t status_audio = audioDecoder->releaseCrypto();
3144 if (status == OK) { // otherwise, returning the first error
3145 status = status_audio;
3146 }
3147 ALOGV("onReleaseDrm: audio decoder ret: %d", status_audio);
3148 }
3149
3150 // TODO change to ALOGV
3151 ALOGD("onReleaseDrm: mCrypto: %p (%d)", mCrypto.get(),
3152 (mCrypto != NULL ? mCrypto->getStrongCount() : 0));
3153 mCrypto.clear();
3154 } else { // mCrypto == NULL
3155 ALOGE("onReleaseDrm: Unexpected. There is no crypto.");
3156 status = INVALID_OPERATION;
3157 }
3158
3159 return status;
3160}
3161// Modular DRM end
Andreas Huberb5f25f02013-02-05 10:14:26 -08003162////////////////////////////////////////////////////////////////////////////////
3163
Chong Zhangced1c2f2014-08-08 15:22:35 -07003164sp<AMessage> NuPlayer::Source::getFormat(bool audio) {
3165 sp<MetaData> meta = getFormatMeta(audio);
3166
3167 if (meta == NULL) {
3168 return NULL;
3169 }
3170
3171 sp<AMessage> msg = new AMessage;
3172
3173 if(convertMetaDataToMessage(meta, &msg) == OK) {
3174 return msg;
3175 }
3176 return NULL;
3177}
3178
Andreas Huber9575c962013-02-05 13:59:56 -08003179void NuPlayer::Source::notifyFlagsChanged(uint32_t flags) {
3180 sp<AMessage> notify = dupNotify();
3181 notify->setInt32("what", kWhatFlagsChanged);
3182 notify->setInt32("flags", flags);
3183 notify->post();
3184}
3185
Chong Zhangced1c2f2014-08-08 15:22:35 -07003186void NuPlayer::Source::notifyVideoSizeChanged(const sp<AMessage> &format) {
Andreas Huber9575c962013-02-05 13:59:56 -08003187 sp<AMessage> notify = dupNotify();
3188 notify->setInt32("what", kWhatVideoSizeChanged);
Chong Zhangced1c2f2014-08-08 15:22:35 -07003189 notify->setMessage("format", format);
Andreas Huber9575c962013-02-05 13:59:56 -08003190 notify->post();
3191}
3192
Andreas Huberec0c5972013-02-05 14:47:13 -08003193void NuPlayer::Source::notifyPrepared(status_t err) {
Hassan Shojaniacefac142017-02-06 21:02:02 -08003194 ALOGV("Source::notifyPrepared %d", err);
Andreas Huber9575c962013-02-05 13:59:56 -08003195 sp<AMessage> notify = dupNotify();
3196 notify->setInt32("what", kWhatPrepared);
Andreas Huberec0c5972013-02-05 14:47:13 -08003197 notify->setInt32("err", err);
Andreas Huber9575c962013-02-05 13:59:56 -08003198 notify->post();
3199}
3200
Hassan Shojaniacefac142017-02-06 21:02:02 -08003201void NuPlayer::Source::notifyDrmInfo(const sp<ABuffer> &drmInfoBuffer)
3202{
3203 ALOGV("Source::notifyDrmInfo");
3204
3205 sp<AMessage> notify = dupNotify();
3206 notify->setInt32("what", kWhatDrmInfo);
3207 notify->setBuffer("drmInfo", drmInfoBuffer);
3208
3209 notify->post();
3210}
3211
Lajos Molnarfcd3e942015-03-31 10:06:48 -07003212void NuPlayer::Source::notifyInstantiateSecureDecoders(const sp<AMessage> &reply) {
3213 sp<AMessage> notify = dupNotify();
3214 notify->setInt32("what", kWhatInstantiateSecureDecoders);
3215 notify->setMessage("reply", reply);
3216 notify->post();
3217}
3218
Andreas Huber84333e02014-02-07 15:36:10 -08003219void NuPlayer::Source::onMessageReceived(const sp<AMessage> & /* msg */) {
Andreas Huberb5f25f02013-02-05 10:14:26 -08003220 TRESPASS();
3221}
3222
Andreas Huberf9334412010-12-15 15:17:42 -08003223} // namespace android