blob: 47362ef1c808e7a94377f456577ca2885737e2b1 [file] [log] [blame]
Andreas Huberf9334412010-12-15 15:17:42 -08001/*
2 * Copyright (C) 2010 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17//#define LOG_NDEBUG 0
18#define LOG_TAG "NuPlayer"
Ray Essickd4d00612017-01-03 09:36:27 -080019
20#include <inttypes.h>
21
Andreas Huberf9334412010-12-15 15:17:42 -080022#include <utils/Log.h>
23
24#include "NuPlayer.h"
Andreas Huber5bc087c2010-12-23 10:27:40 -080025
26#include "HTTPLiveSource.h"
Chong Zhang7137ec72014-11-12 16:41:05 -080027#include "NuPlayerCCDecoder.h"
Andreas Huberf9334412010-12-15 15:17:42 -080028#include "NuPlayerDecoder.h"
Chong Zhang7137ec72014-11-12 16:41:05 -080029#include "NuPlayerDecoderBase.h"
Wei Jiabc2fb722014-07-08 16:37:57 -070030#include "NuPlayerDecoderPassThrough.h"
Andreas Huber43c3e6c2011-01-05 12:17:08 -080031#include "NuPlayerDriver.h"
Andreas Huberf9334412010-12-15 15:17:42 -080032#include "NuPlayerRenderer.h"
Andreas Huber5bc087c2010-12-23 10:27:40 -080033#include "NuPlayerSource.h"
Byeongjo Park28225ab2019-01-24 20:31:19 +090034#include "RTPSource.h"
Andreas Huber2bfdd422011-10-11 15:24:07 -070035#include "RTSPSource.h"
Andreas Huber5bc087c2010-12-23 10:27:40 -080036#include "StreamingSource.h"
Andreas Huberafed0e12011-09-20 15:39:58 -070037#include "GenericSource.h"
Robert Shihd3b0bbb2014-07-23 15:00:25 -070038#include "TextDescriptions.h"
Andreas Huber5bc087c2010-12-23 10:27:40 -080039
40#include "ATSParser.h"
Andreas Huberf9334412010-12-15 15:17:42 -080041
Lajos Molnard9fd6312014-11-06 11:00:00 -080042#include <cutils/properties.h>
43
Lajos Molnar3a474aa2015-04-24 17:10:07 -070044#include <media/AudioResamplerPublic.h>
45#include <media/AVSyncSettings.h>
Wonsik Kim7e34bf52016-08-23 00:09:18 +090046#include <media/MediaCodecBuffer.h>
Lajos Molnar3a474aa2015-04-24 17:10:07 -070047
Andreas Huber3831a062010-12-21 10:22:33 -080048#include <media/stagefright/foundation/hexdump.h>
Andreas Huberf9334412010-12-15 15:17:42 -080049#include <media/stagefright/foundation/ABuffer.h>
50#include <media/stagefright/foundation/ADebug.h>
51#include <media/stagefright/foundation/AMessage.h>
Dongwon Kangd91dc5a2017-10-10 00:07:09 -070052#include <media/stagefright/foundation/avc_utils.h>
Lajos Molnar09524832014-07-17 14:29:51 -070053#include <media/stagefright/MediaBuffer.h>
Wei Jia0a68f662017-08-30 18:01:26 -070054#include <media/stagefright/MediaClock.h>
Andreas Huber3fe62152011-09-16 15:09:22 -070055#include <media/stagefright/MediaDefs.h>
Andreas Huberf9334412010-12-15 15:17:42 -080056#include <media/stagefright/MediaErrors.h>
57#include <media/stagefright/MetaData.h>
Lajos Molnar1de1e252015-04-30 18:18:34 -070058
Andy McFadden8ba01022012-12-18 09:46:54 -080059#include <gui/IGraphicBufferProducer.h>
Lajos Molnar1de1e252015-04-30 18:18:34 -070060#include <gui/Surface.h>
Andreas Huberf9334412010-12-15 15:17:42 -080061
Andreas Huber3fe62152011-09-16 15:09:22 -070062
Andreas Huber84066782011-08-16 09:34:26 -070063#include "ESDS.h"
64#include <media/stagefright/Utils.h>
65
Andreas Huberf9334412010-12-15 15:17:42 -080066namespace android {
67
Andreas Hubera1f8ab02012-11-30 10:53:22 -080068struct NuPlayer::Action : public RefBase {
69 Action() {}
70
71 virtual void execute(NuPlayer *player) = 0;
72
73private:
74 DISALLOW_EVIL_CONSTRUCTORS(Action);
75};
76
77struct NuPlayer::SeekAction : public Action {
Wei Jiac5de0912016-11-18 10:22:14 -080078 explicit SeekAction(int64_t seekTimeUs, MediaPlayerSeekMode mode)
Wei Jia14486822016-11-02 17:51:30 -070079 : mSeekTimeUs(seekTimeUs),
Wei Jiac5de0912016-11-18 10:22:14 -080080 mMode(mode) {
Andreas Hubera1f8ab02012-11-30 10:53:22 -080081 }
82
83 virtual void execute(NuPlayer *player) {
Wei Jiac5de0912016-11-18 10:22:14 -080084 player->performSeek(mSeekTimeUs, mMode);
Andreas Hubera1f8ab02012-11-30 10:53:22 -080085 }
86
87private:
88 int64_t mSeekTimeUs;
Wei Jiac5de0912016-11-18 10:22:14 -080089 MediaPlayerSeekMode mMode;
Andreas Hubera1f8ab02012-11-30 10:53:22 -080090
91 DISALLOW_EVIL_CONSTRUCTORS(SeekAction);
92};
93
Chong Zhangf8d71772014-11-26 15:08:34 -080094struct NuPlayer::ResumeDecoderAction : public Action {
Chih-Hung Hsieh090ef602016-04-27 10:39:54 -070095 explicit ResumeDecoderAction(bool needNotify)
Chong Zhangf8d71772014-11-26 15:08:34 -080096 : mNeedNotify(needNotify) {
97 }
98
99 virtual void execute(NuPlayer *player) {
100 player->performResumeDecoders(mNeedNotify);
101 }
102
103private:
104 bool mNeedNotify;
105
106 DISALLOW_EVIL_CONSTRUCTORS(ResumeDecoderAction);
107};
108
Andreas Huber57a339c2012-12-03 11:18:00 -0800109struct NuPlayer::SetSurfaceAction : public Action {
Chih-Hung Hsieh090ef602016-04-27 10:39:54 -0700110 explicit SetSurfaceAction(const sp<Surface> &surface)
Lajos Molnar1de1e252015-04-30 18:18:34 -0700111 : mSurface(surface) {
Andreas Huber57a339c2012-12-03 11:18:00 -0800112 }
113
114 virtual void execute(NuPlayer *player) {
Lajos Molnar1de1e252015-04-30 18:18:34 -0700115 player->performSetSurface(mSurface);
Andreas Huber57a339c2012-12-03 11:18:00 -0800116 }
117
118private:
Lajos Molnar1de1e252015-04-30 18:18:34 -0700119 sp<Surface> mSurface;
Andreas Huber57a339c2012-12-03 11:18:00 -0800120
121 DISALLOW_EVIL_CONSTRUCTORS(SetSurfaceAction);
122};
123
Wei Jiafef808d2014-10-31 17:57:05 -0700124struct NuPlayer::FlushDecoderAction : public Action {
125 FlushDecoderAction(FlushCommand audio, FlushCommand video)
Andreas Huber14f76722013-01-15 09:04:18 -0800126 : mAudio(audio),
127 mVideo(video) {
128 }
129
130 virtual void execute(NuPlayer *player) {
Wei Jiafef808d2014-10-31 17:57:05 -0700131 player->performDecoderFlush(mAudio, mVideo);
Andreas Huber14f76722013-01-15 09:04:18 -0800132 }
133
134private:
Wei Jiafef808d2014-10-31 17:57:05 -0700135 FlushCommand mAudio;
136 FlushCommand mVideo;
Andreas Huber14f76722013-01-15 09:04:18 -0800137
Wei Jiafef808d2014-10-31 17:57:05 -0700138 DISALLOW_EVIL_CONSTRUCTORS(FlushDecoderAction);
Andreas Huber14f76722013-01-15 09:04:18 -0800139};
140
141struct NuPlayer::PostMessageAction : public Action {
Chih-Hung Hsieh090ef602016-04-27 10:39:54 -0700142 explicit PostMessageAction(const sp<AMessage> &msg)
Andreas Huber14f76722013-01-15 09:04:18 -0800143 : mMessage(msg) {
144 }
145
146 virtual void execute(NuPlayer *) {
147 mMessage->post();
148 }
149
150private:
151 sp<AMessage> mMessage;
152
153 DISALLOW_EVIL_CONSTRUCTORS(PostMessageAction);
154};
155
Andreas Hubera1f8ab02012-11-30 10:53:22 -0800156// Use this if there's no state necessary to save in order to execute
157// the action.
158struct NuPlayer::SimpleAction : public Action {
159 typedef void (NuPlayer::*ActionFunc)();
160
Chih-Hung Hsieh090ef602016-04-27 10:39:54 -0700161 explicit SimpleAction(ActionFunc func)
Andreas Hubera1f8ab02012-11-30 10:53:22 -0800162 : mFunc(func) {
163 }
164
165 virtual void execute(NuPlayer *player) {
166 (player->*mFunc)();
167 }
168
169private:
170 ActionFunc mFunc;
171
172 DISALLOW_EVIL_CONSTRUCTORS(SimpleAction);
173};
174
Andreas Huberf9334412010-12-15 15:17:42 -0800175////////////////////////////////////////////////////////////////////////////////
176
Wei Jia0a68f662017-08-30 18:01:26 -0700177NuPlayer::NuPlayer(pid_t pid, const sp<MediaClock> &mediaClock)
Andreas Huber9b80c2b2011-06-30 15:47:02 -0700178 : mUIDValid(false),
Ronghua Wu68845c12015-07-21 09:50:48 -0700179 mPID(pid),
Wei Jia0a68f662017-08-30 18:01:26 -0700180 mMediaClock(mediaClock),
Andreas Huber9575c962013-02-05 13:59:56 -0800181 mSourceFlags(0),
Wei Jiabc2fb722014-07-08 16:37:57 -0700182 mOffloadAudio(false),
Wei Jia88703c32014-08-06 11:24:07 -0700183 mAudioDecoderGeneration(0),
184 mVideoDecoderGeneration(0),
Wei Jia57568df2014-09-22 10:16:29 -0700185 mRendererGeneration(0),
Ray Essick0d98c182017-11-09 13:42:42 -0800186 mLastStartedPlayingTimeNs(0),
Ray Essickeda32522018-02-28 12:08:28 -0800187 mLastStartedRebufferingTimeNs(0),
Robert Shih1a5c8592015-08-04 18:07:44 -0700188 mPreviousSeekTimeUs(0),
Andreas Huber9b80c2b2011-06-30 15:47:02 -0700189 mAudioEOS(false),
Andreas Huberf9334412010-12-15 15:17:42 -0800190 mVideoEOS(false),
Andreas Huber5bc087c2010-12-23 10:27:40 -0800191 mScanSourcesPending(false),
Andreas Huber1aef2112011-01-04 14:01:29 -0800192 mScanSourcesGeneration(0),
Andreas Huberb7c8e912012-11-27 15:02:53 -0800193 mPollDurationGeneration(0),
Robert Shihd3b0bbb2014-07-23 15:00:25 -0700194 mTimedTextGeneration(0),
Andreas Huberf9334412010-12-15 15:17:42 -0800195 mFlushingAudio(NONE),
Andreas Huber1aef2112011-01-04 14:01:29 -0800196 mFlushingVideo(NONE),
Chong Zhangf8d71772014-11-26 15:08:34 -0800197 mResumePending(false),
Andreas Huber57a339c2012-12-03 11:18:00 -0800198 mVideoScalingMode(NATIVE_WINDOW_SCALING_MODE_SCALE_TO_WINDOW),
Lajos Molnar3a474aa2015-04-24 17:10:07 -0700199 mPlaybackSettings(AUDIO_PLAYBACK_RATE_DEFAULT),
200 mVideoFpsHint(-1.f),
Chong Zhangefbb6192015-01-30 17:13:27 -0800201 mStarted(false),
Wei Jia8a092d32016-06-03 14:57:24 -0700202 mPrepared(false),
Ronghua Wu64c2d172015-10-07 16:52:19 -0700203 mResetting(false),
Robert Shih0c61a0d2015-07-06 15:09:10 -0700204 mSourceStarted(false),
Wei Jia686e8e52017-04-03 14:08:01 -0700205 mAudioDecoderError(false),
206 mVideoDecoderError(false),
Chong Zhangefbb6192015-01-30 17:13:27 -0800207 mPaused(false),
Wei Jia71c75e02016-02-04 09:40:47 -0800208 mPausedByClient(true),
Hassan Shojania50b20c92017-02-16 18:28:58 -0800209 mPausedForBuffering(false),
Hassan Shojaniaff63de72017-04-26 15:10:42 -0700210 mIsDrmProtected(false),
211 mDataSourceType(DATA_SOURCE_TYPE_NONE) {
Wei Jia0a68f662017-08-30 18:01:26 -0700212 CHECK(mediaClock != NULL);
Andy Hung8d121d42014-10-03 09:53:53 -0700213 clearFlushComplete();
Andreas Huberf9334412010-12-15 15:17:42 -0800214}
215
216NuPlayer::~NuPlayer() {
217}
218
Andreas Huber9b80c2b2011-06-30 15:47:02 -0700219void NuPlayer::setUID(uid_t uid) {
220 mUIDValid = true;
221 mUID = uid;
222}
223
Dongwon Kang47afe0a2018-03-27 15:15:30 -0700224void NuPlayer::init(const wp<NuPlayerDriver> &driver) {
Andreas Huber43c3e6c2011-01-05 12:17:08 -0800225 mDriver = driver;
Dongwon Kang47afe0a2018-03-27 15:15:30 -0700226
227 sp<AMessage> notify = new AMessage(kWhatMediaClockNotify, this);
228 mMediaClock->setNotificationMessage(notify);
Andreas Huberf9334412010-12-15 15:17:42 -0800229}
230
Andreas Huber9575c962013-02-05 13:59:56 -0800231void NuPlayer::setDataSourceAsync(const sp<IStreamSource> &source) {
Lajos Molnar1d15ab52015-03-04 16:46:34 -0800232 sp<AMessage> msg = new AMessage(kWhatSetDataSource, this);
Andreas Huberf9334412010-12-15 15:17:42 -0800233
Lajos Molnar1d15ab52015-03-04 16:46:34 -0800234 sp<AMessage> notify = new AMessage(kWhatSourceNotify, this);
Andreas Huberb5f25f02013-02-05 10:14:26 -0800235
Andreas Huber240abcc2014-02-13 13:32:37 -0800236 msg->setObject("source", new StreamingSource(notify, source));
Andreas Huber5bc087c2010-12-23 10:27:40 -0800237 msg->post();
Hassan Shojaniaff63de72017-04-26 15:10:42 -0700238 mDataSourceType = DATA_SOURCE_TYPE_STREAM;
Andreas Huber5bc087c2010-12-23 10:27:40 -0800239}
Andreas Huberf9334412010-12-15 15:17:42 -0800240
Andreas Huberafed0e12011-09-20 15:39:58 -0700241static bool IsHTTPLiveURL(const char *url) {
242 if (!strncasecmp("http://", url, 7)
Andreas Huber99759402013-04-01 14:28:31 -0700243 || !strncasecmp("https://", url, 8)
244 || !strncasecmp("file://", url, 7)) {
Andreas Huberafed0e12011-09-20 15:39:58 -0700245 size_t len = strlen(url);
246 if (len >= 5 && !strcasecmp(".m3u8", &url[len - 5])) {
247 return true;
248 }
249
250 if (strstr(url,"m3u8")) {
251 return true;
252 }
253 }
254
255 return false;
256}
257
Andreas Huber9575c962013-02-05 13:59:56 -0800258void NuPlayer::setDataSourceAsync(
Andreas Huber1b86fe02014-01-29 11:13:26 -0800259 const sp<IMediaHTTPService> &httpService,
260 const char *url,
261 const KeyedVector<String8, String8> *headers) {
Chong Zhang3de157d2014-08-05 20:54:44 -0700262
Lajos Molnar1d15ab52015-03-04 16:46:34 -0800263 sp<AMessage> msg = new AMessage(kWhatSetDataSource, this);
Oscar Rydhé7a33b772012-02-20 10:15:48 +0100264 size_t len = strlen(url);
Andreas Huber5bc087c2010-12-23 10:27:40 -0800265
Lajos Molnar1d15ab52015-03-04 16:46:34 -0800266 sp<AMessage> notify = new AMessage(kWhatSourceNotify, this);
Andreas Huberb5f25f02013-02-05 10:14:26 -0800267
Andreas Huberafed0e12011-09-20 15:39:58 -0700268 sp<Source> source;
269 if (IsHTTPLiveURL(url)) {
Andreas Huber81e68442014-02-05 11:52:33 -0800270 source = new HTTPLiveSource(notify, httpService, url, headers);
Hassan Shojaniacefac142017-02-06 21:02:02 -0800271 ALOGV("setDataSourceAsync HTTPLiveSource %s", url);
Hassan Shojaniaff63de72017-04-26 15:10:42 -0700272 mDataSourceType = DATA_SOURCE_TYPE_HTTP_LIVE;
Andreas Huberafed0e12011-09-20 15:39:58 -0700273 } else if (!strncasecmp(url, "rtsp://", 7)) {
Andreas Huber1b86fe02014-01-29 11:13:26 -0800274 source = new RTSPSource(
275 notify, httpService, url, headers, mUIDValid, mUID);
Hassan Shojaniacefac142017-02-06 21:02:02 -0800276 ALOGV("setDataSourceAsync RTSPSource %s", url);
Hassan Shojaniaff63de72017-04-26 15:10:42 -0700277 mDataSourceType = DATA_SOURCE_TYPE_RTSP;
Oscar Rydhé7a33b772012-02-20 10:15:48 +0100278 } else if ((!strncasecmp(url, "http://", 7)
279 || !strncasecmp(url, "https://", 8))
280 && ((len >= 4 && !strcasecmp(".sdp", &url[len - 4]))
281 || strstr(url, ".sdp?"))) {
Andreas Huber1b86fe02014-01-29 11:13:26 -0800282 source = new RTSPSource(
283 notify, httpService, url, headers, mUIDValid, mUID, true);
Hassan Shojaniacefac142017-02-06 21:02:02 -0800284 ALOGV("setDataSourceAsync RTSPSource http/https/.sdp %s", url);
Hassan Shojaniaff63de72017-04-26 15:10:42 -0700285 mDataSourceType = DATA_SOURCE_TYPE_RTSP;
Andreas Huber2bfdd422011-10-11 15:24:07 -0700286 } else {
Hassan Shojaniacefac142017-02-06 21:02:02 -0800287 ALOGV("setDataSourceAsync GenericSource %s", url);
288
Chong Zhang3de157d2014-08-05 20:54:44 -0700289 sp<GenericSource> genericSource =
Wei Jia992c5592017-09-01 14:20:23 -0700290 new GenericSource(notify, mUIDValid, mUID, mMediaClock);
Andreas Huber2bfdd422011-10-11 15:24:07 -0700291
Chong Zhanga19f33e2014-08-07 15:35:07 -0700292 status_t err = genericSource->setDataSource(httpService, url, headers);
Chong Zhang3de157d2014-08-05 20:54:44 -0700293
294 if (err == OK) {
295 source = genericSource;
296 } else {
Chong Zhanga19f33e2014-08-07 15:35:07 -0700297 ALOGE("Failed to set data source!");
Chong Zhang3de157d2014-08-05 20:54:44 -0700298 }
Hassan Shojaniaff63de72017-04-26 15:10:42 -0700299
300 // regardless of success/failure
301 mDataSourceType = DATA_SOURCE_TYPE_GENERIC_URL;
Chong Zhang3de157d2014-08-05 20:54:44 -0700302 }
Andreas Huberafed0e12011-09-20 15:39:58 -0700303 msg->setObject("source", source);
304 msg->post();
305}
306
Andreas Huber9575c962013-02-05 13:59:56 -0800307void NuPlayer::setDataSourceAsync(int fd, int64_t offset, int64_t length) {
Lajos Molnar1d15ab52015-03-04 16:46:34 -0800308 sp<AMessage> msg = new AMessage(kWhatSetDataSource, this);
Andreas Huberafed0e12011-09-20 15:39:58 -0700309
Lajos Molnar1d15ab52015-03-04 16:46:34 -0800310 sp<AMessage> notify = new AMessage(kWhatSourceNotify, this);
Andreas Huberb5f25f02013-02-05 10:14:26 -0800311
Chong Zhang3de157d2014-08-05 20:54:44 -0700312 sp<GenericSource> source =
Wei Jia992c5592017-09-01 14:20:23 -0700313 new GenericSource(notify, mUIDValid, mUID, mMediaClock);
Chong Zhang3de157d2014-08-05 20:54:44 -0700314
Hassan Shojaniacefac142017-02-06 21:02:02 -0800315 ALOGV("setDataSourceAsync fd %d/%lld/%lld source: %p",
316 fd, (long long)offset, (long long)length, source.get());
317
Chong Zhanga19f33e2014-08-07 15:35:07 -0700318 status_t err = source->setDataSource(fd, offset, length);
Chong Zhang3de157d2014-08-05 20:54:44 -0700319
320 if (err != OK) {
Chong Zhanga19f33e2014-08-07 15:35:07 -0700321 ALOGE("Failed to set data source!");
Chong Zhang3de157d2014-08-05 20:54:44 -0700322 source = NULL;
323 }
324
Andreas Huberafed0e12011-09-20 15:39:58 -0700325 msg->setObject("source", source);
Andreas Huberf9334412010-12-15 15:17:42 -0800326 msg->post();
Hassan Shojaniaff63de72017-04-26 15:10:42 -0700327 mDataSourceType = DATA_SOURCE_TYPE_GENERIC_FD;
Andreas Huberf9334412010-12-15 15:17:42 -0800328}
329
Chris Watkins99f31602015-03-20 13:06:33 -0700330void NuPlayer::setDataSourceAsync(const sp<DataSource> &dataSource) {
331 sp<AMessage> msg = new AMessage(kWhatSetDataSource, this);
332 sp<AMessage> notify = new AMessage(kWhatSourceNotify, this);
333
Wei Jia992c5592017-09-01 14:20:23 -0700334 sp<GenericSource> source = new GenericSource(notify, mUIDValid, mUID, mMediaClock);
Chris Watkins99f31602015-03-20 13:06:33 -0700335 status_t err = source->setDataSource(dataSource);
336
337 if (err != OK) {
338 ALOGE("Failed to set data source!");
339 source = NULL;
340 }
341
342 msg->setObject("source", source);
343 msg->post();
Hassan Shojaniaff63de72017-04-26 15:10:42 -0700344 mDataSourceType = DATA_SOURCE_TYPE_MEDIA;
Chris Watkins99f31602015-03-20 13:06:33 -0700345}
346
Wei Jia9bb38032017-03-23 18:00:38 -0700347status_t NuPlayer::getBufferingSettings(
Wei Jia48fa06d2016-12-20 15:30:49 -0800348 BufferingSettings *buffering /* nonnull */) {
Wei Jia9bb38032017-03-23 18:00:38 -0700349 sp<AMessage> msg = new AMessage(kWhatGetBufferingSettings, this);
Wei Jia48fa06d2016-12-20 15:30:49 -0800350 sp<AMessage> response;
351 status_t err = msg->postAndAwaitResponse(&response);
352 if (err == OK && response != NULL) {
353 CHECK(response->findInt32("err", &err));
354 if (err == OK) {
355 readFromAMessage(response, buffering);
356 }
357 }
358 return err;
359}
360
361status_t NuPlayer::setBufferingSettings(const BufferingSettings& buffering) {
362 sp<AMessage> msg = new AMessage(kWhatSetBufferingSettings, this);
363 writeToAMessage(msg, buffering);
364 sp<AMessage> response;
365 status_t err = msg->postAndAwaitResponse(&response);
366 if (err == OK && response != NULL) {
367 CHECK(response->findInt32("err", &err));
368 }
369 return err;
370}
371
Byeongjo Park28225ab2019-01-24 20:31:19 +0900372void NuPlayer::setDataSourceAsync(const String8& rtpParams) {
373 ALOGD("setDataSourceAsync for RTP = %s", rtpParams.string());
374 sp<AMessage> msg = new AMessage(kWhatSetDataSource, this);
375
376 sp<AMessage> notify = new AMessage(kWhatSourceNotify, this);
377 sp<Source> source = new RTPSource(notify, rtpParams);
378
379 msg->setObject("source", source);
380 msg->post();
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) {
Wei Jiabf70feb2016-02-19 15:47:16 -0800874 // AudioSink allows only 1.f and 0.f for offload mode.
875 // For other speed, switch to non-offload mode.
876 if (mOffloadAudio && ((rate.mSpeed != 0.f && rate.mSpeed != 1.f)
877 || rate.mPitch != 1.f)) {
878 int64_t currentPositionUs;
879 if (getCurrentPosition(&currentPositionUs) != OK) {
880 currentPositionUs = mPreviousSeekTimeUs;
881 }
882
883 // Set mPlaybackSettings so that the new audio decoder can
884 // be created correctly.
885 mPlaybackSettings = rate;
886 if (!mPaused) {
887 mRenderer->pause();
888 }
Wei Jia5031b2f2016-02-25 11:19:31 -0800889 restartAudio(
Wei Jiabf70feb2016-02-19 15:47:16 -0800890 currentPositionUs, true /* forceNonOffload */,
891 true /* needsToCreateAudioDecoder */);
892 if (!mPaused) {
893 mRenderer->resume();
894 }
895 }
896
Lajos Molnar3a474aa2015-04-24 17:10:07 -0700897 err = mRenderer->setPlaybackSettings(rate);
898 }
899 if (err == OK) {
900 if (rate.mSpeed == 0.f) {
901 onPause();
Wei Jia351ce872016-02-10 13:21:59 -0800902 mPausedByClient = true;
Lajos Molnar3a474aa2015-04-24 17:10:07 -0700903 // save all other settings (using non-paused speed)
904 // so we can restore them on start
905 AudioPlaybackRate newRate = rate;
906 newRate.mSpeed = mPlaybackSettings.mSpeed;
907 mPlaybackSettings = newRate;
908 } else { /* rate.mSpeed != 0.f */
Lajos Molnar3a474aa2015-04-24 17:10:07 -0700909 mPlaybackSettings = rate;
Wei Jia8a092d32016-06-03 14:57:24 -0700910 if (mStarted) {
911 // do not resume yet if the source is still buffering
912 if (!mPausedForBuffering) {
913 onResume();
914 }
915 } else if (mPrepared) {
916 onStart();
917 }
918
919 mPausedByClient = false;
Lajos Molnar3a474aa2015-04-24 17:10:07 -0700920 }
Wei Jia98160162015-02-04 17:01:11 -0800921 }
Ronghua Wu8db88132015-04-22 13:51:35 -0700922
923 if (mVideoDecoder != NULL) {
Praveen Chavanbbaa1442016-04-08 13:33:49 -0700924 sp<AMessage> params = new AMessage();
925 params->setFloat("playback-speed", mPlaybackSettings.mSpeed);
926 mVideoDecoder->setParameters(params);
Ronghua Wu8db88132015-04-22 13:51:35 -0700927 }
928
Lajos Molnar3a474aa2015-04-24 17:10:07 -0700929 sp<AMessage> response = new AMessage;
930 response->setInt32("err", err);
931 response->postReply(replyID);
932 break;
933 }
934
935 case kWhatGetPlaybackSettings:
936 {
937 sp<AReplyToken> replyID;
938 CHECK(msg->senderAwaitsResponse(&replyID));
939 AudioPlaybackRate rate = mPlaybackSettings;
940 status_t err = OK;
941 if (mRenderer != NULL) {
942 err = mRenderer->getPlaybackSettings(&rate);
943 }
944 if (err == OK) {
945 // get playback settings used by renderer, as it may be
946 // slightly off due to audiosink not taking small changes.
947 mPlaybackSettings = rate;
948 if (mPaused) {
949 rate.mSpeed = 0.f;
950 }
951 }
952 sp<AMessage> response = new AMessage;
953 if (err == OK) {
954 writeToAMessage(response, rate);
955 }
956 response->setInt32("err", err);
957 response->postReply(replyID);
958 break;
959 }
960
961 case kWhatConfigSync:
962 {
963 sp<AReplyToken> replyID;
964 CHECK(msg->senderAwaitsResponse(&replyID));
965
966 ALOGV("kWhatConfigSync");
967 AVSyncSettings sync;
968 float videoFpsHint;
969 readFromAMessage(msg, &sync, &videoFpsHint);
970 status_t err = OK;
971 if (mRenderer != NULL) {
972 err = mRenderer->setSyncSettings(sync, videoFpsHint);
973 }
974 if (err == OK) {
975 mSyncSettings = sync;
976 mVideoFpsHint = videoFpsHint;
977 }
978 sp<AMessage> response = new AMessage;
979 response->setInt32("err", err);
980 response->postReply(replyID);
981 break;
982 }
983
984 case kWhatGetSyncSettings:
985 {
986 sp<AReplyToken> replyID;
987 CHECK(msg->senderAwaitsResponse(&replyID));
988 AVSyncSettings sync = mSyncSettings;
989 float videoFps = mVideoFpsHint;
990 status_t err = OK;
991 if (mRenderer != NULL) {
992 err = mRenderer->getSyncSettings(&sync, &videoFps);
993 if (err == OK) {
994 mSyncSettings = sync;
995 mVideoFpsHint = videoFps;
996 }
997 }
998 sp<AMessage> response = new AMessage;
999 if (err == OK) {
1000 writeToAMessage(response, sync, videoFps);
1001 }
1002 response->setInt32("err", err);
1003 response->postReply(replyID);
Wei Jia98160162015-02-04 17:01:11 -08001004 break;
1005 }
1006
Andreas Huberf9334412010-12-15 15:17:42 -08001007 case kWhatScanSources:
1008 {
Andreas Huber1aef2112011-01-04 14:01:29 -08001009 int32_t generation;
1010 CHECK(msg->findInt32("generation", &generation));
1011 if (generation != mScanSourcesGeneration) {
1012 // Drop obsolete msg.
1013 break;
1014 }
1015
Andreas Huber5bc087c2010-12-23 10:27:40 -08001016 mScanSourcesPending = false;
1017
Steve Block3856b092011-10-20 11:56:00 +01001018 ALOGV("scanning sources haveAudio=%d, haveVideo=%d",
Andreas Huber43c3e6c2011-01-05 12:17:08 -08001019 mAudioDecoder != NULL, mVideoDecoder != NULL);
1020
Andreas Huberb7c8e912012-11-27 15:02:53 -08001021 bool mHadAnySourcesBefore =
1022 (mAudioDecoder != NULL) || (mVideoDecoder != NULL);
Robert Shih7350b052015-10-01 15:50:14 -07001023 bool rescan = false;
Andreas Huberb7c8e912012-11-27 15:02:53 -08001024
Andy Hung282a7e32014-08-14 15:56:34 -07001025 // initialize video before audio because successful initialization of
1026 // video may change deep buffer mode of audio.
Lajos Molnar1de1e252015-04-30 18:18:34 -07001027 if (mSurface != NULL) {
Robert Shih7350b052015-10-01 15:50:14 -07001028 if (instantiateDecoder(false, &mVideoDecoder) == -EWOULDBLOCK) {
1029 rescan = true;
1030 }
Haynes Mathew George5d246ef2012-07-09 10:36:57 -07001031 }
Andreas Huberf9334412010-12-15 15:17:42 -08001032
Ronghua Wua10fd232014-11-06 16:15:20 -08001033 // Don't try to re-open audio sink if there's an existing decoder.
1034 if (mAudioSink != NULL && mAudioDecoder == NULL) {
Robert Shih7350b052015-10-01 15:50:14 -07001035 if (instantiateDecoder(true, &mAudioDecoder) == -EWOULDBLOCK) {
1036 rescan = true;
1037 }
Andreas Huberf9334412010-12-15 15:17:42 -08001038 }
1039
Andreas Huberb7c8e912012-11-27 15:02:53 -08001040 if (!mHadAnySourcesBefore
1041 && (mAudioDecoder != NULL || mVideoDecoder != NULL)) {
1042 // This is the first time we've found anything playable.
1043
Andreas Huber9575c962013-02-05 13:59:56 -08001044 if (mSourceFlags & Source::FLAG_DYNAMIC_DURATION) {
Andreas Huberb7c8e912012-11-27 15:02:53 -08001045 schedulePollDuration();
1046 }
1047 }
1048
Andreas Hubereac68ba2011-09-27 12:12:25 -07001049 status_t err;
1050 if ((err = mSource->feedMoreTSData()) != OK) {
Andreas Huber1aef2112011-01-04 14:01:29 -08001051 if (mAudioDecoder == NULL && mVideoDecoder == NULL) {
1052 // We're not currently decoding anything (no audio or
1053 // video tracks found) and we just ran out of input data.
Andreas Hubereac68ba2011-09-27 12:12:25 -07001054
1055 if (err == ERROR_END_OF_STREAM) {
1056 notifyListener(MEDIA_PLAYBACK_COMPLETE, 0, 0);
1057 } else {
1058 notifyListener(MEDIA_ERROR, MEDIA_ERROR_UNKNOWN, err);
1059 }
Andreas Huber1aef2112011-01-04 14:01:29 -08001060 }
Andreas Huberf9334412010-12-15 15:17:42 -08001061 break;
1062 }
1063
Robert Shih7350b052015-10-01 15:50:14 -07001064 if (rescan) {
Chih-Hung Hsieh62309d52018-12-11 13:54:02 -08001065 msg->post(100000LL);
Andreas Huber5bc087c2010-12-23 10:27:40 -08001066 mScanSourcesPending = true;
Andreas Huberf9334412010-12-15 15:17:42 -08001067 }
1068 break;
1069 }
1070
1071 case kWhatVideoNotify:
1072 case kWhatAudioNotify:
1073 {
1074 bool audio = msg->what() == kWhatAudioNotify;
1075
Wei Jia88703c32014-08-06 11:24:07 -07001076 int32_t currentDecoderGeneration =
1077 (audio? mAudioDecoderGeneration : mVideoDecoderGeneration);
1078 int32_t requesterGeneration = currentDecoderGeneration - 1;
1079 CHECK(msg->findInt32("generation", &requesterGeneration));
1080
1081 if (requesterGeneration != currentDecoderGeneration) {
1082 ALOGV("got message from old %s decoder, generation(%d:%d)",
1083 audio ? "audio" : "video", requesterGeneration,
1084 currentDecoderGeneration);
1085 sp<AMessage> reply;
1086 if (!(msg->findMessage("reply", &reply))) {
1087 return;
1088 }
1089
1090 reply->setInt32("err", INFO_DISCONTINUITY);
1091 reply->post();
1092 return;
1093 }
1094
Andreas Huberf9334412010-12-15 15:17:42 -08001095 int32_t what;
Lajos Molnar1cd13982014-01-17 15:12:51 -08001096 CHECK(msg->findInt32("what", &what));
Andreas Huberf9334412010-12-15 15:17:42 -08001097
Chong Zhang7137ec72014-11-12 16:41:05 -08001098 if (what == DecoderBase::kWhatInputDiscontinuity) {
1099 int32_t formatChange;
1100 CHECK(msg->findInt32("formatChange", &formatChange));
Andreas Huberf9334412010-12-15 15:17:42 -08001101
Chong Zhang7137ec72014-11-12 16:41:05 -08001102 ALOGV("%s discontinuity: formatChange %d",
1103 audio ? "audio" : "video", formatChange);
1104
1105 if (formatChange) {
1106 mDeferredActions.push_back(
1107 new FlushDecoderAction(
1108 audio ? FLUSH_CMD_SHUTDOWN : FLUSH_CMD_NONE,
1109 audio ? FLUSH_CMD_NONE : FLUSH_CMD_SHUTDOWN));
Andreas Huberf9334412010-12-15 15:17:42 -08001110 }
Chong Zhang7137ec72014-11-12 16:41:05 -08001111
1112 mDeferredActions.push_back(
1113 new SimpleAction(
1114 &NuPlayer::performScanSources));
1115
1116 processDeferredActions();
1117 } else if (what == DecoderBase::kWhatEOS) {
Andreas Huberdc9bacd2011-09-26 10:53:29 -07001118 int32_t err;
Lajos Molnar1cd13982014-01-17 15:12:51 -08001119 CHECK(msg->findInt32("err", &err));
Andreas Huberdc9bacd2011-09-26 10:53:29 -07001120
1121 if (err == ERROR_END_OF_STREAM) {
Steve Block3856b092011-10-20 11:56:00 +01001122 ALOGV("got %s decoder EOS", audio ? "audio" : "video");
Andreas Huberdc9bacd2011-09-26 10:53:29 -07001123 } else {
Steve Block3856b092011-10-20 11:56:00 +01001124 ALOGV("got %s decoder EOS w/ error %d",
Andreas Huberdc9bacd2011-09-26 10:53:29 -07001125 audio ? "audio" : "video",
1126 err);
1127 }
1128
1129 mRenderer->queueEOS(audio, err);
Chong Zhang7137ec72014-11-12 16:41:05 -08001130 } else if (what == DecoderBase::kWhatFlushCompleted) {
Steve Block3856b092011-10-20 11:56:00 +01001131 ALOGV("decoder %s flush completed", audio ? "audio" : "video");
Andreas Huberf9334412010-12-15 15:17:42 -08001132
Andy Hung8d121d42014-10-03 09:53:53 -07001133 handleFlushComplete(audio, true /* isDecoder */);
Andreas Huber3831a062010-12-21 10:22:33 -08001134 finishFlushIfPossible();
Chong Zhang7137ec72014-11-12 16:41:05 -08001135 } else if (what == DecoderBase::kWhatVideoSizeChanged) {
Lajos Molnar1cd13982014-01-17 15:12:51 -08001136 sp<AMessage> format;
1137 CHECK(msg->findMessage("format", &format));
1138
Wei Jiac6cfd702014-11-11 16:33:20 -08001139 sp<AMessage> inputFormat =
1140 mSource->getFormat(false /* audio */);
Andreas Huber3831a062010-12-21 10:22:33 -08001141
Bartosz Dolewski26936f72014-12-11 12:47:08 +01001142 setVideoScalingMode(mVideoScalingMode);
Wei Jiac6cfd702014-11-11 16:33:20 -08001143 updateVideoSize(inputFormat, format);
Chong Zhang7137ec72014-11-12 16:41:05 -08001144 } else if (what == DecoderBase::kWhatShutdownCompleted) {
Steve Block3856b092011-10-20 11:56:00 +01001145 ALOGV("%s shutdown completed", audio ? "audio" : "video");
Andreas Huber3831a062010-12-21 10:22:33 -08001146 if (audio) {
Ray Essickfcb8fd32018-08-07 09:39:38 -07001147 Mutex::Autolock autoLock(mDecoderLock);
Andreas Huber3831a062010-12-21 10:22:33 -08001148 mAudioDecoder.clear();
Wei Jia686e8e52017-04-03 14:08:01 -07001149 mAudioDecoderError = false;
Wei Jia57568df2014-09-22 10:16:29 -07001150 ++mAudioDecoderGeneration;
Andreas Huber3831a062010-12-21 10:22:33 -08001151
1152 CHECK_EQ((int)mFlushingAudio, (int)SHUTTING_DOWN_DECODER);
1153 mFlushingAudio = SHUT_DOWN;
1154 } else {
Ray Essickfcb8fd32018-08-07 09:39:38 -07001155 Mutex::Autolock autoLock(mDecoderLock);
Andreas Huber3831a062010-12-21 10:22:33 -08001156 mVideoDecoder.clear();
Wei Jia686e8e52017-04-03 14:08:01 -07001157 mVideoDecoderError = false;
Wei Jia57568df2014-09-22 10:16:29 -07001158 ++mVideoDecoderGeneration;
Andreas Huber3831a062010-12-21 10:22:33 -08001159
1160 CHECK_EQ((int)mFlushingVideo, (int)SHUTTING_DOWN_DECODER);
1161 mFlushingVideo = SHUT_DOWN;
1162 }
1163
1164 finishFlushIfPossible();
Chong Zhangf8d71772014-11-26 15:08:34 -08001165 } else if (what == DecoderBase::kWhatResumeCompleted) {
1166 finishResume();
Chong Zhang7137ec72014-11-12 16:41:05 -08001167 } else if (what == DecoderBase::kWhatError) {
Chong Zhangf4c0a942014-08-11 15:14:10 -07001168 status_t err;
Andy Hung2abde2c2014-09-30 14:40:32 -07001169 if (!msg->findInt32("err", &err) || err == OK) {
Chong Zhangf4c0a942014-08-11 15:14:10 -07001170 err = UNKNOWN_ERROR;
1171 }
Andy Hungcf31f1e2014-09-23 14:59:01 -07001172
Andy Hung2abde2c2014-09-30 14:40:32 -07001173 // Decoder errors can be due to Source (e.g. from streaming),
1174 // or from decoding corrupted bitstreams, or from other decoder
1175 // MediaCodec operations (e.g. from an ongoing reset or seek).
Andy Hung202bce12014-12-03 11:47:36 -08001176 // They may also be due to openAudioSink failure at
1177 // decoder start or after a format change.
Andy Hung2abde2c2014-09-30 14:40:32 -07001178 //
1179 // We try to gracefully shut down the affected decoder if possible,
1180 // rather than trying to force the shutdown with something
1181 // similar to performReset(). This method can lead to a hang
1182 // if MediaCodec functions block after an error, but they should
1183 // typically return INVALID_OPERATION instead of blocking.
1184
1185 FlushStatus *flushing = audio ? &mFlushingAudio : &mFlushingVideo;
1186 ALOGE("received error(%#x) from %s decoder, flushing(%d), now shutting down",
1187 err, audio ? "audio" : "video", *flushing);
1188
1189 switch (*flushing) {
1190 case NONE:
1191 mDeferredActions.push_back(
Wei Jiafef808d2014-10-31 17:57:05 -07001192 new FlushDecoderAction(
1193 audio ? FLUSH_CMD_SHUTDOWN : FLUSH_CMD_NONE,
1194 audio ? FLUSH_CMD_NONE : FLUSH_CMD_SHUTDOWN));
Andy Hung2abde2c2014-09-30 14:40:32 -07001195 processDeferredActions();
1196 break;
1197 case FLUSHING_DECODER:
1198 *flushing = FLUSHING_DECODER_SHUTDOWN; // initiate shutdown after flush.
1199 break; // Wait for flush to complete.
1200 case FLUSHING_DECODER_SHUTDOWN:
1201 break; // Wait for flush to complete.
1202 case SHUTTING_DOWN_DECODER:
1203 break; // Wait for shutdown to complete.
1204 case FLUSHED:
Andy Hung2abde2c2014-09-30 14:40:32 -07001205 getDecoder(audio)->initiateShutdown(); // In the middle of a seek.
1206 *flushing = SHUTTING_DOWN_DECODER; // Shut down.
1207 break;
1208 case SHUT_DOWN:
1209 finishFlushIfPossible(); // Should not occur.
1210 break; // Finish anyways.
Marco Nelissen9e2b7912014-08-18 16:13:03 -07001211 }
Wei Jia686e8e52017-04-03 14:08:01 -07001212 if (mSource != nullptr) {
1213 if (audio) {
Wei Jia2f6d8c52017-04-11 09:50:31 -07001214 if (mVideoDecoderError || mSource->getFormat(false /* audio */) == NULL
Wei Jiaf86731d2017-07-11 17:24:34 -07001215 || mSurface == NULL || mVideoDecoder == NULL) {
Wei Jia686e8e52017-04-03 14:08:01 -07001216 // When both audio and video have error, or this stream has only audio
1217 // which has error, notify client of error.
1218 notifyListener(MEDIA_ERROR, MEDIA_ERROR_UNKNOWN, err);
1219 } else {
1220 // Only audio track has error. Video track could be still good to play.
1221 notifyListener(MEDIA_INFO, MEDIA_INFO_PLAY_AUDIO_ERROR, err);
1222 }
1223 mAudioDecoderError = true;
1224 } else {
Wei Jia2f6d8c52017-04-11 09:50:31 -07001225 if (mAudioDecoderError || mSource->getFormat(true /* audio */) == NULL
Wei Jiaf86731d2017-07-11 17:24:34 -07001226 || mAudioSink == NULL || mAudioDecoder == NULL) {
Wei Jia686e8e52017-04-03 14:08:01 -07001227 // When both audio and video have error, or this stream has only video
1228 // which has error, notify client of error.
1229 notifyListener(MEDIA_ERROR, MEDIA_ERROR_UNKNOWN, err);
1230 } else {
1231 // Only video track has error. Audio track could be still good to play.
1232 notifyListener(MEDIA_INFO, MEDIA_INFO_PLAY_VIDEO_ERROR, err);
1233 }
1234 mVideoDecoderError = true;
1235 }
1236 }
Lajos Molnar1cd13982014-01-17 15:12:51 -08001237 } else {
1238 ALOGV("Unhandled decoder notification %d '%c%c%c%c'.",
Andreas Hubera1f8ab02012-11-30 10:53:22 -08001239 what,
1240 what >> 24,
1241 (what >> 16) & 0xff,
1242 (what >> 8) & 0xff,
1243 what & 0xff);
Andreas Huberf9334412010-12-15 15:17:42 -08001244 }
1245
1246 break;
1247 }
1248
1249 case kWhatRendererNotify:
1250 {
Wei Jia57568df2014-09-22 10:16:29 -07001251 int32_t requesterGeneration = mRendererGeneration - 1;
1252 CHECK(msg->findInt32("generation", &requesterGeneration));
1253 if (requesterGeneration != mRendererGeneration) {
1254 ALOGV("got message from old renderer, generation(%d:%d)",
1255 requesterGeneration, mRendererGeneration);
1256 return;
1257 }
1258
Andreas Huberf9334412010-12-15 15:17:42 -08001259 int32_t what;
1260 CHECK(msg->findInt32("what", &what));
1261
1262 if (what == Renderer::kWhatEOS) {
1263 int32_t audio;
1264 CHECK(msg->findInt32("audio", &audio));
1265
Andreas Huberc92fd242011-08-16 13:48:44 -07001266 int32_t finalResult;
1267 CHECK(msg->findInt32("finalResult", &finalResult));
1268
Andreas Huberf9334412010-12-15 15:17:42 -08001269 if (audio) {
1270 mAudioEOS = true;
1271 } else {
1272 mVideoEOS = true;
1273 }
1274
Andreas Huberc92fd242011-08-16 13:48:44 -07001275 if (finalResult == ERROR_END_OF_STREAM) {
Steve Block3856b092011-10-20 11:56:00 +01001276 ALOGV("reached %s EOS", audio ? "audio" : "video");
Andreas Huberc92fd242011-08-16 13:48:44 -07001277 } else {
Steve Block29357bc2012-01-06 19:20:56 +00001278 ALOGE("%s track encountered an error (%d)",
Andreas Huberc92fd242011-08-16 13:48:44 -07001279 audio ? "audio" : "video", finalResult);
1280
1281 notifyListener(
1282 MEDIA_ERROR, MEDIA_ERROR_UNKNOWN, finalResult);
1283 }
Andreas Huberf9334412010-12-15 15:17:42 -08001284
1285 if ((mAudioEOS || mAudioDecoder == NULL)
1286 && (mVideoEOS || mVideoDecoder == NULL)) {
1287 notifyListener(MEDIA_PLAYBACK_COMPLETE, 0, 0);
1288 }
Andreas Huber3fe62152011-09-16 15:09:22 -07001289 } else if (what == Renderer::kWhatFlushComplete) {
Andreas Huberf9334412010-12-15 15:17:42 -08001290 int32_t audio;
1291 CHECK(msg->findInt32("audio", &audio));
1292
Wei Jia3261f0d2015-10-08 13:58:33 -07001293 if (audio) {
1294 mAudioEOS = false;
1295 } else {
1296 mVideoEOS = false;
1297 }
1298
Steve Block3856b092011-10-20 11:56:00 +01001299 ALOGV("renderer %s flush completed.", audio ? "audio" : "video");
Wei Jiada4252f2015-07-14 18:15:28 -07001300 if (audio && (mFlushingAudio == NONE || mFlushingAudio == FLUSHED
1301 || mFlushingAudio == SHUT_DOWN)) {
1302 // Flush has been handled by tear down.
1303 break;
1304 }
Andy Hung8d121d42014-10-03 09:53:53 -07001305 handleFlushComplete(audio, false /* isDecoder */);
1306 finishFlushIfPossible();
James Dongf57b4ea2012-07-20 13:38:36 -07001307 } else if (what == Renderer::kWhatVideoRenderingStart) {
1308 notifyListener(MEDIA_INFO, MEDIA_INFO_RENDERING_START, 0);
Lajos Molnarcbaffcf2013-08-14 18:30:38 -07001309 } else if (what == Renderer::kWhatMediaRenderingStart) {
1310 ALOGV("media rendering started");
1311 notifyListener(MEDIA_STARTED, 0, 0);
Ronghua Wufaeb0f22015-05-21 12:20:21 -07001312 } else if (what == Renderer::kWhatAudioTearDown) {
Ronghua Wu08529172014-10-02 16:55:52 -07001313 int32_t reason;
1314 CHECK(msg->findInt32("reason", &reason));
Ronghua Wufaeb0f22015-05-21 12:20:21 -07001315 ALOGV("Tear down audio with reason %d.", reason);
Wei Jia8456ddd2016-04-22 14:46:43 -07001316 if (reason == Renderer::kDueToTimeout && !(mPaused && mOffloadAudio)) {
1317 // TimeoutWhenPaused is only for offload mode.
Wei Jia355b2bb2018-04-09 16:36:23 -07001318 ALOGW("Received a stale message for teardown, mPaused(%d), mOffloadAudio(%d)",
1319 mPaused, mOffloadAudio);
Wei Jia8456ddd2016-04-22 14:46:43 -07001320 break;
1321 }
Wei Jiada4252f2015-07-14 18:15:28 -07001322 int64_t positionUs;
Robert Shih1a5c8592015-08-04 18:07:44 -07001323 if (!msg->findInt64("positionUs", &positionUs)) {
1324 positionUs = mPreviousSeekTimeUs;
1325 }
Ronghua Wufaeb0f22015-05-21 12:20:21 -07001326
Wei Jia5031b2f2016-02-25 11:19:31 -08001327 restartAudio(
Wei Jiaa05f1e32016-03-25 16:31:22 -07001328 positionUs, reason == Renderer::kForceNonOffload /* forceNonOffload */,
1329 reason != Renderer::kDueToTimeout /* needsToCreateAudioDecoder */);
Andreas Huberf9334412010-12-15 15:17:42 -08001330 }
1331 break;
1332 }
1333
1334 case kWhatMoreDataQueued:
1335 {
1336 break;
1337 }
1338
Andreas Huber1aef2112011-01-04 14:01:29 -08001339 case kWhatReset:
1340 {
Steve Block3856b092011-10-20 11:56:00 +01001341 ALOGV("kWhatReset");
Andreas Huber1aef2112011-01-04 14:01:29 -08001342
Ronghua Wu64c2d172015-10-07 16:52:19 -07001343 mResetting = true;
Ray Essickeda32522018-02-28 12:08:28 -08001344 updatePlaybackTimer(true /* stopping */, "kWhatReset");
1345 updateRebufferingTimer(true /* stopping */, true /* exiting */);
Ronghua Wu64c2d172015-10-07 16:52:19 -07001346
Andreas Hubera1f8ab02012-11-30 10:53:22 -08001347 mDeferredActions.push_back(
Wei Jiafef808d2014-10-31 17:57:05 -07001348 new FlushDecoderAction(
1349 FLUSH_CMD_SHUTDOWN /* audio */,
1350 FLUSH_CMD_SHUTDOWN /* video */));
Andreas Huberb7c8e912012-11-27 15:02:53 -08001351
Andreas Hubera1f8ab02012-11-30 10:53:22 -08001352 mDeferredActions.push_back(
1353 new SimpleAction(&NuPlayer::performReset));
Andreas Huberb58ce9f2011-11-28 16:27:35 -08001354
Andreas Hubera1f8ab02012-11-30 10:53:22 -08001355 processDeferredActions();
Andreas Huber1aef2112011-01-04 14:01:29 -08001356 break;
1357 }
1358
Wei Jia52c28512017-09-13 18:17:51 -07001359 case kWhatNotifyTime:
1360 {
1361 ALOGV("kWhatNotifyTime");
1362 int64_t timerUs;
1363 CHECK(msg->findInt64("timerUs", &timerUs));
1364
1365 notifyListener(MEDIA_NOTIFY_TIME, timerUs, 0);
1366 break;
1367 }
1368
Andreas Huber43c3e6c2011-01-05 12:17:08 -08001369 case kWhatSeek:
1370 {
1371 int64_t seekTimeUs;
Wei Jiac5de0912016-11-18 10:22:14 -08001372 int32_t mode;
Wei Jiae427abf2014-09-22 15:21:11 -07001373 int32_t needNotify;
Andreas Huber43c3e6c2011-01-05 12:17:08 -08001374 CHECK(msg->findInt64("seekTimeUs", &seekTimeUs));
Wei Jiac5de0912016-11-18 10:22:14 -08001375 CHECK(msg->findInt32("mode", &mode));
Wei Jiae427abf2014-09-22 15:21:11 -07001376 CHECK(msg->findInt32("needNotify", &needNotify));
Andreas Huber43c3e6c2011-01-05 12:17:08 -08001377
Wei Jiac5de0912016-11-18 10:22:14 -08001378 ALOGV("kWhatSeek seekTimeUs=%lld us, mode=%d, needNotify=%d",
1379 (long long)seekTimeUs, mode, needNotify);
Andreas Huber43c3e6c2011-01-05 12:17:08 -08001380
Wei Jia1061c9c2015-05-19 16:02:17 -07001381 if (!mStarted) {
1382 // Seek before the player is started. In order to preview video,
1383 // need to start the player and pause it. This branch is called
1384 // only once if needed. After the player is started, any seek
1385 // operation will go through normal path.
Robert Shih0c61a0d2015-07-06 15:09:10 -07001386 // Audio-only cases are handled separately.
Wei Jiac5de0912016-11-18 10:22:14 -08001387 onStart(seekTimeUs, (MediaPlayerSeekMode)mode);
Robert Shih0c61a0d2015-07-06 15:09:10 -07001388 if (mStarted) {
1389 onPause();
1390 mPausedByClient = true;
1391 }
Wei Jia1061c9c2015-05-19 16:02:17 -07001392 if (needNotify) {
1393 notifyDriverSeekComplete();
1394 }
1395 break;
1396 }
1397
Andreas Hubera1f8ab02012-11-30 10:53:22 -08001398 mDeferredActions.push_back(
Wei Jiafef808d2014-10-31 17:57:05 -07001399 new FlushDecoderAction(FLUSH_CMD_FLUSH /* audio */,
1400 FLUSH_CMD_FLUSH /* video */));
Andreas Huber43c3e6c2011-01-05 12:17:08 -08001401
Wei Jiae427abf2014-09-22 15:21:11 -07001402 mDeferredActions.push_back(
Wei Jiac5de0912016-11-18 10:22:14 -08001403 new SeekAction(seekTimeUs, (MediaPlayerSeekMode)mode));
Andreas Huber43c3e6c2011-01-05 12:17:08 -08001404
Chong Zhangf8d71772014-11-26 15:08:34 -08001405 // After a flush without shutdown, decoder is paused.
1406 // Don't resume it until source seek is done, otherwise it could
Chong Zhang7137ec72014-11-12 16:41:05 -08001407 // start pulling stale data too soon.
1408 mDeferredActions.push_back(
Chong Zhangf8d71772014-11-26 15:08:34 -08001409 new ResumeDecoderAction(needNotify));
Chong Zhang7137ec72014-11-12 16:41:05 -08001410
Andreas Hubera1f8ab02012-11-30 10:53:22 -08001411 processDeferredActions();
Andreas Huber43c3e6c2011-01-05 12:17:08 -08001412 break;
1413 }
1414
Andreas Huberb4082222011-01-20 15:23:04 -08001415 case kWhatPause:
1416 {
Chong Zhangefbb6192015-01-30 17:13:27 -08001417 onPause();
1418 mPausedByClient = true;
Andreas Huberb4082222011-01-20 15:23:04 -08001419 break;
1420 }
1421
Andreas Huberb5f25f02013-02-05 10:14:26 -08001422 case kWhatSourceNotify:
1423 {
Andreas Huber9575c962013-02-05 13:59:56 -08001424 onSourceNotify(msg);
Andreas Huberb5f25f02013-02-05 10:14:26 -08001425 break;
1426 }
1427
Chong Zhanga7fa1d92014-06-11 14:49:23 -07001428 case kWhatClosedCaptionNotify:
1429 {
1430 onClosedCaptionNotify(msg);
1431 break;
1432 }
1433
Hassan Shojaniacefac142017-02-06 21:02:02 -08001434 case kWhatPrepareDrm:
1435 {
1436 status_t status = onPrepareDrm(msg);
1437
1438 sp<AMessage> response = new AMessage;
1439 response->setInt32("status", status);
1440 sp<AReplyToken> replyID;
1441 CHECK(msg->senderAwaitsResponse(&replyID));
1442 response->postReply(replyID);
1443 break;
1444 }
1445
1446 case kWhatReleaseDrm:
1447 {
1448 status_t status = onReleaseDrm();
1449
1450 sp<AMessage> response = new AMessage;
1451 response->setInt32("status", status);
1452 sp<AReplyToken> replyID;
1453 CHECK(msg->senderAwaitsResponse(&replyID));
1454 response->postReply(replyID);
1455 break;
1456 }
1457
Dongwon Kang47afe0a2018-03-27 15:15:30 -07001458 case kWhatMediaClockNotify:
1459 {
1460 ALOGV("kWhatMediaClockNotify");
1461 int64_t anchorMediaUs, anchorRealUs;
1462 float playbackRate;
1463 CHECK(msg->findInt64("anchor-media-us", &anchorMediaUs));
1464 CHECK(msg->findInt64("anchor-real-us", &anchorRealUs));
1465 CHECK(msg->findFloat("playback-rate", &playbackRate));
1466
1467 Parcel in;
1468 in.writeInt64(anchorMediaUs);
1469 in.writeInt64(anchorRealUs);
1470 in.writeFloat(playbackRate);
1471
1472 notifyListener(MEDIA_TIME_DISCONTINUITY, 0, 0, &in);
1473 break;
1474 }
1475
Andreas Huberf9334412010-12-15 15:17:42 -08001476 default:
1477 TRESPASS();
1478 break;
1479 }
1480}
1481
Wei Jia94211742014-10-28 17:09:06 -07001482void NuPlayer::onResume() {
Ronghua Wu64c2d172015-10-07 16:52:19 -07001483 if (!mPaused || mResetting) {
1484 ALOGD_IF(mResetting, "resetting, onResume discarded");
Chong Zhangefbb6192015-01-30 17:13:27 -08001485 return;
1486 }
1487 mPaused = false;
Wei Jia94211742014-10-28 17:09:06 -07001488 if (mSource != NULL) {
1489 mSource->resume();
1490 } else {
1491 ALOGW("resume called when source is gone or not set");
1492 }
1493 // |mAudioDecoder| may have been released due to the pause timeout, so re-create it if
1494 // needed.
1495 if (audioDecoderStillNeeded() && mAudioDecoder == NULL) {
1496 instantiateDecoder(true /* audio */, &mAudioDecoder);
1497 }
1498 if (mRenderer != NULL) {
1499 mRenderer->resume();
1500 } else {
1501 ALOGW("resume called when renderer is gone or not set");
1502 }
Ray Essickd4d00612017-01-03 09:36:27 -08001503
Ray Essick0d98c182017-11-09 13:42:42 -08001504 startPlaybackTimer("onresume");
Wei Jia94211742014-10-28 17:09:06 -07001505}
1506
Lajos Molnarfcd3e942015-03-31 10:06:48 -07001507status_t NuPlayer::onInstantiateSecureDecoders() {
1508 status_t err;
1509 if (!(mSourceFlags & Source::FLAG_SECURE)) {
1510 return BAD_TYPE;
1511 }
1512
1513 if (mRenderer != NULL) {
1514 ALOGE("renderer should not be set when instantiating secure decoders");
1515 return UNKNOWN_ERROR;
1516 }
1517
1518 // TRICKY: We rely on mRenderer being null, so that decoder does not start requesting
1519 // data on instantiation.
Lajos Molnar1de1e252015-04-30 18:18:34 -07001520 if (mSurface != NULL) {
Lajos Molnarfcd3e942015-03-31 10:06:48 -07001521 err = instantiateDecoder(false, &mVideoDecoder);
1522 if (err != OK) {
1523 return err;
1524 }
1525 }
1526
1527 if (mAudioSink != NULL) {
1528 err = instantiateDecoder(true, &mAudioDecoder);
1529 if (err != OK) {
1530 return err;
1531 }
1532 }
1533 return OK;
1534}
1535
Wei Jiac5de0912016-11-18 10:22:14 -08001536void NuPlayer::onStart(int64_t startPositionUs, MediaPlayerSeekMode mode) {
Hassan Shojaniacefac142017-02-06 21:02:02 -08001537 ALOGV("onStart: mCrypto: %p (%d)", mCrypto.get(),
1538 (mCrypto != NULL ? mCrypto->getStrongCount() : 0));
1539
Robert Shih0c61a0d2015-07-06 15:09:10 -07001540 if (!mSourceStarted) {
1541 mSourceStarted = true;
1542 mSource->start();
1543 }
1544 if (startPositionUs > 0) {
Wei Jiac5de0912016-11-18 10:22:14 -08001545 performSeek(startPositionUs, mode);
Robert Shih0c61a0d2015-07-06 15:09:10 -07001546 if (mSource->getFormat(false /* audio */) == NULL) {
1547 return;
1548 }
1549 }
1550
Wei Jia94211742014-10-28 17:09:06 -07001551 mOffloadAudio = false;
1552 mAudioEOS = false;
1553 mVideoEOS = false;
Wei Jia94211742014-10-28 17:09:06 -07001554 mStarted = true;
Wei Jiafe8fe7d2016-06-08 10:29:25 -07001555 mPaused = false;
Wei Jia94211742014-10-28 17:09:06 -07001556
Wei Jia94211742014-10-28 17:09:06 -07001557 uint32_t flags = 0;
1558
1559 if (mSource->isRealTime()) {
1560 flags |= Renderer::FLAG_REAL_TIME;
1561 }
1562
Wei Jia9737d342016-12-28 14:59:59 -08001563 bool hasAudio = (mSource->getFormat(true /* audio */) != NULL);
1564 bool hasVideo = (mSource->getFormat(false /* audio */) != NULL);
1565 if (!hasAudio && !hasVideo) {
Wei Jia1de83d52016-10-10 10:15:03 -07001566 ALOGE("no metadata for either audio or video source");
1567 mSource->stop();
1568 mSourceStarted = false;
1569 notifyListener(MEDIA_ERROR, MEDIA_ERROR_UNKNOWN, ERROR_MALFORMED);
1570 return;
1571 }
Wei Jia9737d342016-12-28 14:59:59 -08001572 ALOGV_IF(!hasAudio, "no metadata for audio source"); // video only stream
1573
1574 sp<MetaData> audioMeta = mSource->getFormatMeta(true /* audio */);
Wei Jia1de83d52016-10-10 10:15:03 -07001575
Wei Jia94211742014-10-28 17:09:06 -07001576 audio_stream_type_t streamType = AUDIO_STREAM_MUSIC;
1577 if (mAudioSink != NULL) {
1578 streamType = mAudioSink->getAudioStreamType();
1579 }
1580
Wei Jia94211742014-10-28 17:09:06 -07001581 mOffloadAudio =
Wei Jia9737d342016-12-28 14:59:59 -08001582 canOffloadStream(audioMeta, hasVideo, mSource->isStreaming(), streamType)
Wei Jiabf70feb2016-02-19 15:47:16 -08001583 && (mPlaybackSettings.mSpeed == 1.f && mPlaybackSettings.mPitch == 1.f);
Hassan Shojaniacefac142017-02-06 21:02:02 -08001584
1585 // Modular DRM: Disabling audio offload if the source is protected
1586 if (mOffloadAudio && mIsDrmProtected) {
1587 mOffloadAudio = false;
1588 ALOGV("onStart: Disabling mOffloadAudio now that the source is protected.");
1589 }
1590
Wei Jia94211742014-10-28 17:09:06 -07001591 if (mOffloadAudio) {
1592 flags |= Renderer::FLAG_OFFLOAD_AUDIO;
1593 }
1594
Lajos Molnar1d15ab52015-03-04 16:46:34 -08001595 sp<AMessage> notify = new AMessage(kWhatRendererNotify, this);
Wei Jia94211742014-10-28 17:09:06 -07001596 ++mRendererGeneration;
1597 notify->setInt32("generation", mRendererGeneration);
Wei Jia0a68f662017-08-30 18:01:26 -07001598 mRenderer = new Renderer(mAudioSink, mMediaClock, notify, flags);
Wei Jia94211742014-10-28 17:09:06 -07001599 mRendererLooper = new ALooper;
1600 mRendererLooper->setName("NuPlayerRenderer");
1601 mRendererLooper->start(false, false, ANDROID_PRIORITY_AUDIO);
1602 mRendererLooper->registerHandler(mRenderer);
Lajos Molnar3a474aa2015-04-24 17:10:07 -07001603
1604 status_t err = mRenderer->setPlaybackSettings(mPlaybackSettings);
1605 if (err != OK) {
1606 mSource->stop();
Robert Shih0c61a0d2015-07-06 15:09:10 -07001607 mSourceStarted = false;
Lajos Molnar3a474aa2015-04-24 17:10:07 -07001608 notifyListener(MEDIA_ERROR, MEDIA_ERROR_UNKNOWN, err);
1609 return;
Wei Jiac8206ff2015-03-04 13:59:37 -08001610 }
Wei Jia94211742014-10-28 17:09:06 -07001611
Ronghua Wuc8a70d32015-04-29 16:26:34 -07001612 float rate = getFrameRate();
1613 if (rate > 0) {
Wei Jia94211742014-10-28 17:09:06 -07001614 mRenderer->setVideoFrameRate(rate);
1615 }
1616
Wei Jiac6cfd702014-11-11 16:33:20 -08001617 if (mVideoDecoder != NULL) {
1618 mVideoDecoder->setRenderer(mRenderer);
1619 }
1620 if (mAudioDecoder != NULL) {
1621 mAudioDecoder->setRenderer(mRenderer);
1622 }
1623
Ray Essick0d98c182017-11-09 13:42:42 -08001624 startPlaybackTimer("onstart");
Ray Essickd4d00612017-01-03 09:36:27 -08001625
Wei Jia94211742014-10-28 17:09:06 -07001626 postScanSources();
1627}
1628
Ray Essick0d98c182017-11-09 13:42:42 -08001629void NuPlayer::startPlaybackTimer(const char *where) {
1630 Mutex::Autolock autoLock(mPlayingTimeLock);
1631 if (mLastStartedPlayingTimeNs == 0) {
1632 mLastStartedPlayingTimeNs = systemTime();
1633 ALOGV("startPlaybackTimer() time %20" PRId64 " (%s)", mLastStartedPlayingTimeNs, where);
1634 }
1635}
1636
Ray Essickeda32522018-02-28 12:08:28 -08001637void NuPlayer::updatePlaybackTimer(bool stopping, const char *where) {
Ray Essick0d98c182017-11-09 13:42:42 -08001638 Mutex::Autolock autoLock(mPlayingTimeLock);
1639
Ray Essickeda32522018-02-28 12:08:28 -08001640 ALOGV("updatePlaybackTimer(%s) time %20" PRId64 " (%s)",
1641 stopping ? "stop" : "snap", mLastStartedPlayingTimeNs, where);
Ray Essick0d98c182017-11-09 13:42:42 -08001642
1643 if (mLastStartedPlayingTimeNs != 0) {
1644 sp<NuPlayerDriver> driver = mDriver.promote();
Ray Essickeda32522018-02-28 12:08:28 -08001645 int64_t now = systemTime();
Ray Essick0d98c182017-11-09 13:42:42 -08001646 if (driver != NULL) {
Ray Essick0d98c182017-11-09 13:42:42 -08001647 int64_t played = now - mLastStartedPlayingTimeNs;
Ray Essickeda32522018-02-28 12:08:28 -08001648 ALOGV("updatePlaybackTimer() log %20" PRId64 "", played);
Ray Essick0d98c182017-11-09 13:42:42 -08001649
1650 if (played > 0) {
1651 driver->notifyMorePlayingTimeUs((played+500)/1000);
1652 }
1653 }
Ray Essickeda32522018-02-28 12:08:28 -08001654 if (stopping) {
1655 mLastStartedPlayingTimeNs = 0;
1656 } else {
1657 mLastStartedPlayingTimeNs = now;
1658 }
Ray Essick0d98c182017-11-09 13:42:42 -08001659 }
1660}
1661
Ray Essick58e0f7a2017-11-21 10:59:38 -08001662void NuPlayer::startRebufferingTimer() {
1663 Mutex::Autolock autoLock(mPlayingTimeLock);
1664 if (mLastStartedRebufferingTimeNs == 0) {
1665 mLastStartedRebufferingTimeNs = systemTime();
1666 ALOGV("startRebufferingTimer() time %20" PRId64 "", mLastStartedRebufferingTimeNs);
1667 }
1668}
1669
Ray Essickeda32522018-02-28 12:08:28 -08001670void NuPlayer::updateRebufferingTimer(bool stopping, bool exitingPlayback) {
Ray Essick58e0f7a2017-11-21 10:59:38 -08001671 Mutex::Autolock autoLock(mPlayingTimeLock);
1672
Ray Essickeda32522018-02-28 12:08:28 -08001673 ALOGV("updateRebufferingTimer(%s) time %20" PRId64 " (exiting %d)",
1674 stopping ? "stop" : "snap", mLastStartedRebufferingTimeNs, exitingPlayback);
Ray Essick58e0f7a2017-11-21 10:59:38 -08001675
1676 if (mLastStartedRebufferingTimeNs != 0) {
1677 sp<NuPlayerDriver> driver = mDriver.promote();
Ray Essickeda32522018-02-28 12:08:28 -08001678 int64_t now = systemTime();
Ray Essick58e0f7a2017-11-21 10:59:38 -08001679 if (driver != NULL) {
Ray Essick58e0f7a2017-11-21 10:59:38 -08001680 int64_t rebuffered = now - mLastStartedRebufferingTimeNs;
Ray Essickeda32522018-02-28 12:08:28 -08001681 ALOGV("updateRebufferingTimer() log %20" PRId64 "", rebuffered);
Ray Essick58e0f7a2017-11-21 10:59:38 -08001682
1683 if (rebuffered > 0) {
1684 driver->notifyMoreRebufferingTimeUs((rebuffered+500)/1000);
1685 if (exitingPlayback) {
1686 driver->notifyRebufferingWhenExit(true);
1687 }
1688 }
1689 }
Ray Essickeda32522018-02-28 12:08:28 -08001690 if (stopping) {
1691 mLastStartedRebufferingTimeNs = 0;
1692 } else {
1693 mLastStartedRebufferingTimeNs = now;
1694 }
Ray Essick58e0f7a2017-11-21 10:59:38 -08001695 }
1696}
1697
Ray Essickeda32522018-02-28 12:08:28 -08001698void NuPlayer::updateInternalTimers() {
1699 // update values, but ticking clocks keep ticking
1700 ALOGV("updateInternalTimers()");
1701 updatePlaybackTimer(false /* stopping */, "updateInternalTimers");
1702 updateRebufferingTimer(false /* stopping */, false /* exiting */);
1703}
1704
Kim Sungyeon23b23932019-07-18 17:48:32 +09001705void NuPlayer::setTargetBitrate(int bitrate) {
Lajos Molnared809da2020-08-17 16:53:02 -07001706 if (mSource != NULL) {
Kim Sungyeon23b23932019-07-18 17:48:32 +09001707 mSource->setTargetBitrate(bitrate);
Lajos Molnared809da2020-08-17 16:53:02 -07001708 }
Kim Sungyeon23b23932019-07-18 17:48:32 +09001709}
1710
Chong Zhangefbb6192015-01-30 17:13:27 -08001711void NuPlayer::onPause() {
Ray Essick0d98c182017-11-09 13:42:42 -08001712
Ray Essickeda32522018-02-28 12:08:28 -08001713 updatePlaybackTimer(true /* stopping */, "onPause");
Ray Essick0d98c182017-11-09 13:42:42 -08001714
Chong Zhangefbb6192015-01-30 17:13:27 -08001715 if (mPaused) {
1716 return;
1717 }
1718 mPaused = true;
1719 if (mSource != NULL) {
1720 mSource->pause();
1721 } else {
1722 ALOGW("pause called when source is gone or not set");
1723 }
1724 if (mRenderer != NULL) {
1725 mRenderer->pause();
1726 } else {
1727 ALOGW("pause called when renderer is gone or not set");
1728 }
Ray Essickd4d00612017-01-03 09:36:27 -08001729
Chong Zhangefbb6192015-01-30 17:13:27 -08001730}
1731
Ronghua Wud7988b12014-10-03 15:19:10 -07001732bool NuPlayer::audioDecoderStillNeeded() {
1733 // Audio decoder is no longer needed if it's in shut/shutting down status.
1734 return ((mFlushingAudio != SHUT_DOWN) && (mFlushingAudio != SHUTTING_DOWN_DECODER));
1735}
1736
Andy Hung8d121d42014-10-03 09:53:53 -07001737void NuPlayer::handleFlushComplete(bool audio, bool isDecoder) {
1738 // We wait for both the decoder flush and the renderer flush to complete
1739 // before entering either the FLUSHED or the SHUTTING_DOWN_DECODER state.
1740
1741 mFlushComplete[audio][isDecoder] = true;
1742 if (!mFlushComplete[audio][!isDecoder]) {
1743 return;
1744 }
1745
1746 FlushStatus *state = audio ? &mFlushingAudio : &mFlushingVideo;
1747 switch (*state) {
1748 case FLUSHING_DECODER:
1749 {
1750 *state = FLUSHED;
Andy Hung8d121d42014-10-03 09:53:53 -07001751 break;
1752 }
1753
1754 case FLUSHING_DECODER_SHUTDOWN:
1755 {
1756 *state = SHUTTING_DOWN_DECODER;
1757
1758 ALOGV("initiating %s decoder shutdown", audio ? "audio" : "video");
Andy Hung8d121d42014-10-03 09:53:53 -07001759 getDecoder(audio)->initiateShutdown();
1760 break;
1761 }
1762
1763 default:
1764 // decoder flush completes only occur in a flushing state.
1765 LOG_ALWAYS_FATAL_IF(isDecoder, "decoder flush in invalid state %d", *state);
1766 break;
1767 }
1768}
1769
Andreas Huber3831a062010-12-21 10:22:33 -08001770void NuPlayer::finishFlushIfPossible() {
Wei Jia53904f32014-07-29 10:22:53 -07001771 if (mFlushingAudio != NONE && mFlushingAudio != FLUSHED
1772 && mFlushingAudio != SHUT_DOWN) {
Andreas Huber3831a062010-12-21 10:22:33 -08001773 return;
1774 }
1775
Wei Jia53904f32014-07-29 10:22:53 -07001776 if (mFlushingVideo != NONE && mFlushingVideo != FLUSHED
1777 && mFlushingVideo != SHUT_DOWN) {
Andreas Huber3831a062010-12-21 10:22:33 -08001778 return;
1779 }
1780
Steve Block3856b092011-10-20 11:56:00 +01001781 ALOGV("both audio and video are flushed now.");
Andreas Huber3831a062010-12-21 10:22:33 -08001782
Andreas Huber3831a062010-12-21 10:22:33 -08001783 mFlushingAudio = NONE;
1784 mFlushingVideo = NONE;
Andreas Huber3831a062010-12-21 10:22:33 -08001785
Andy Hung8d121d42014-10-03 09:53:53 -07001786 clearFlushComplete();
1787
Andreas Hubera1f8ab02012-11-30 10:53:22 -08001788 processDeferredActions();
Andreas Huber1aef2112011-01-04 14:01:29 -08001789}
1790
1791void NuPlayer::postScanSources() {
1792 if (mScanSourcesPending) {
1793 return;
1794 }
1795
Lajos Molnar1d15ab52015-03-04 16:46:34 -08001796 sp<AMessage> msg = new AMessage(kWhatScanSources, this);
Andreas Huber1aef2112011-01-04 14:01:29 -08001797 msg->setInt32("generation", mScanSourcesGeneration);
1798 msg->post();
1799
1800 mScanSourcesPending = true;
1801}
1802
Wei Jia41cd4632016-05-13 10:53:30 -07001803void NuPlayer::tryOpenAudioSinkForOffload(
1804 const sp<AMessage> &format, const sp<MetaData> &audioMeta, bool hasVideo) {
Andy Hung202bce12014-12-03 11:47:36 -08001805 // Note: This is called early in NuPlayer to determine whether offloading
1806 // is possible; otherwise the decoders call the renderer openAudioSink directly.
Andy Hung282a7e32014-08-14 15:56:34 -07001807
Andy Hung202bce12014-12-03 11:47:36 -08001808 status_t err = mRenderer->openAudioSink(
Dhananjay Kumarc387f2b2015-08-06 10:43:16 +05301809 format, true /* offloadOnly */, hasVideo,
1810 AUDIO_OUTPUT_FLAG_NONE, &mOffloadAudio, mSource->isStreaming());
Andy Hung202bce12014-12-03 11:47:36 -08001811 if (err != OK) {
1812 // Any failure we turn off mOffloadAudio.
1813 mOffloadAudio = false;
1814 } else if (mOffloadAudio) {
Chong Zhang3b9eb1f2014-10-15 17:05:08 -07001815 sendMetaDataToHal(mAudioSink, audioMeta);
Andy Hung282a7e32014-08-14 15:56:34 -07001816 }
1817}
1818
1819void NuPlayer::closeAudioSink() {
Daniel Norman1e0d88b2020-01-07 15:02:54 -08001820 if (mRenderer != NULL) {
1821 mRenderer->closeAudioSink();
1822 }
Andy Hung282a7e32014-08-14 15:56:34 -07001823}
1824
Wei Jia5031b2f2016-02-25 11:19:31 -08001825void NuPlayer::restartAudio(
Wei Jiabf70feb2016-02-19 15:47:16 -08001826 int64_t currentPositionUs, bool forceNonOffload, bool needsToCreateAudioDecoder) {
Wei Jia355b2bb2018-04-09 16:36:23 -07001827 ALOGD("restartAudio timeUs(%lld), dontOffload(%d), createDecoder(%d)",
1828 (long long)currentPositionUs, forceNonOffload, needsToCreateAudioDecoder);
Wei Jiaa05f1e32016-03-25 16:31:22 -07001829 if (mAudioDecoder != NULL) {
1830 mAudioDecoder->pause();
Ray Essickfcb8fd32018-08-07 09:39:38 -07001831 Mutex::Autolock autoLock(mDecoderLock);
Wei Jiaa05f1e32016-03-25 16:31:22 -07001832 mAudioDecoder.clear();
Wei Jia686e8e52017-04-03 14:08:01 -07001833 mAudioDecoderError = false;
Wei Jiaa05f1e32016-03-25 16:31:22 -07001834 ++mAudioDecoderGeneration;
1835 }
Wei Jiabf70feb2016-02-19 15:47:16 -08001836 if (mFlushingAudio == FLUSHING_DECODER) {
1837 mFlushComplete[1 /* audio */][1 /* isDecoder */] = true;
1838 mFlushingAudio = FLUSHED;
1839 finishFlushIfPossible();
1840 } else if (mFlushingAudio == FLUSHING_DECODER_SHUTDOWN
1841 || mFlushingAudio == SHUTTING_DOWN_DECODER) {
1842 mFlushComplete[1 /* audio */][1 /* isDecoder */] = true;
1843 mFlushingAudio = SHUT_DOWN;
1844 finishFlushIfPossible();
1845 needsToCreateAudioDecoder = false;
1846 }
1847 if (mRenderer == NULL) {
1848 return;
1849 }
1850 closeAudioSink();
1851 mRenderer->flush(true /* audio */, false /* notifyComplete */);
1852 if (mVideoDecoder != NULL) {
Praveen Chavaneca08fb2019-01-21 11:00:38 -08001853 mDeferredActions.push_back(
1854 new FlushDecoderAction(FLUSH_CMD_NONE /* audio */,
1855 FLUSH_CMD_FLUSH /* video */));
1856 mDeferredActions.push_back(
1857 new SeekAction(currentPositionUs,
1858 MediaPlayerSeekMode::SEEK_PREVIOUS_SYNC /* mode */));
1859 // After a flush without shutdown, decoder is paused.
1860 // Don't resume it until source seek is done, otherwise it could
1861 // start pulling stale data too soon.
1862 mDeferredActions.push_back(new ResumeDecoderAction(false));
1863 processDeferredActions();
1864 } else {
1865 performSeek(currentPositionUs, MediaPlayerSeekMode::SEEK_PREVIOUS_SYNC /* mode */);
Wei Jiabf70feb2016-02-19 15:47:16 -08001866 }
1867
Wei Jiabf70feb2016-02-19 15:47:16 -08001868 if (forceNonOffload) {
1869 mRenderer->signalDisableOffloadAudio();
1870 mOffloadAudio = false;
1871 }
1872 if (needsToCreateAudioDecoder) {
Wei Jiaa05f1e32016-03-25 16:31:22 -07001873 instantiateDecoder(true /* audio */, &mAudioDecoder, !forceNonOffload);
Wei Jiabf70feb2016-02-19 15:47:16 -08001874 }
1875}
1876
Wei Jia41cd4632016-05-13 10:53:30 -07001877void NuPlayer::determineAudioModeChange(const sp<AMessage> &audioFormat) {
Wei Jiae4d18c72015-07-13 17:58:11 -07001878 if (mSource == NULL || mAudioSink == NULL) {
1879 return;
1880 }
1881
1882 if (mRenderer == NULL) {
1883 ALOGW("No renderer can be used to determine audio mode. Use non-offload for safety.");
1884 mOffloadAudio = false;
1885 return;
1886 }
1887
1888 sp<MetaData> audioMeta = mSource->getFormatMeta(true /* audio */);
1889 sp<AMessage> videoFormat = mSource->getFormat(false /* audio */);
1890 audio_stream_type_t streamType = mAudioSink->getAudioStreamType();
1891 const bool hasVideo = (videoFormat != NULL);
Hassan Shojaniacefac142017-02-06 21:02:02 -08001892 bool canOffload = canOffloadStream(
Wei Jiabf70feb2016-02-19 15:47:16 -08001893 audioMeta, hasVideo, mSource->isStreaming(), streamType)
1894 && (mPlaybackSettings.mSpeed == 1.f && mPlaybackSettings.mPitch == 1.f);
Hassan Shojaniacefac142017-02-06 21:02:02 -08001895
1896 // Modular DRM: Disabling audio offload if the source is protected
1897 if (canOffload && mIsDrmProtected) {
1898 canOffload = false;
1899 ALOGV("determineAudioModeChange: Disabling mOffloadAudio b/c the source is protected.");
1900 }
1901
Wei Jiae4d18c72015-07-13 17:58:11 -07001902 if (canOffload) {
1903 if (!mOffloadAudio) {
1904 mRenderer->signalEnableOffloadAudio();
1905 }
1906 // open audio sink early under offload mode.
Wei Jia41cd4632016-05-13 10:53:30 -07001907 tryOpenAudioSinkForOffload(audioFormat, audioMeta, hasVideo);
Wei Jiae4d18c72015-07-13 17:58:11 -07001908 } else {
Robert Shihe1d70192015-07-23 17:54:13 -07001909 if (mOffloadAudio) {
1910 mRenderer->signalDisableOffloadAudio();
1911 mOffloadAudio = false;
1912 }
Wei Jiae4d18c72015-07-13 17:58:11 -07001913 }
1914}
1915
Wei Jiaa05f1e32016-03-25 16:31:22 -07001916status_t NuPlayer::instantiateDecoder(
1917 bool audio, sp<DecoderBase> *decoder, bool checkAudioModeChange) {
Wei Jia566da802015-08-27 13:59:30 -07001918 // The audio decoder could be cleared by tear down. If still in shut down
1919 // process, no need to create a new audio decoder.
1920 if (*decoder != NULL || (audio && mFlushingAudio == SHUT_DOWN)) {
Andreas Huberf9334412010-12-15 15:17:42 -08001921 return OK;
1922 }
1923
Andreas Huber84066782011-08-16 09:34:26 -07001924 sp<AMessage> format = mSource->getFormat(audio);
Andreas Huberf9334412010-12-15 15:17:42 -08001925
Andreas Huber84066782011-08-16 09:34:26 -07001926 if (format == NULL) {
Robert Shih7350b052015-10-01 15:50:14 -07001927 return UNKNOWN_ERROR;
1928 } else {
1929 status_t err;
1930 if (format->findInt32("err", &err) && err) {
1931 return err;
1932 }
Andreas Huberf9334412010-12-15 15:17:42 -08001933 }
1934
Ronghua Wu8db88132015-04-22 13:51:35 -07001935 format->setInt32("priority", 0 /* realtime */);
1936
Byeongjo Park5e27b1b2018-07-12 15:36:03 +09001937 if (mDataSourceType == DATA_SOURCE_TYPE_RTP) {
1938 ALOGV("instantiateDecoder: set decoder error free on stream corrupt.");
1939 format->setInt32("corrupt-free", true);
1940 }
1941
Andreas Huber3fe62152011-09-16 15:09:22 -07001942 if (!audio) {
Andreas Huber84066782011-08-16 09:34:26 -07001943 AString mime;
1944 CHECK(format->findString("mime", &mime));
Chong Zhanga7fa1d92014-06-11 14:49:23 -07001945
Lajos Molnar1d15ab52015-03-04 16:46:34 -08001946 sp<AMessage> ccNotify = new AMessage(kWhatClosedCaptionNotify, this);
Chong Zhang341ab6e2015-02-04 13:37:18 -08001947 if (mCCDecoder == NULL) {
1948 mCCDecoder = new CCDecoder(ccNotify);
1949 }
Lajos Molnar09524832014-07-17 14:29:51 -07001950
1951 if (mSourceFlags & Source::FLAG_SECURE) {
1952 format->setInt32("secure", true);
1953 }
Chong Zhang17134602015-01-07 16:14:34 -08001954
1955 if (mSourceFlags & Source::FLAG_PROTECTED) {
1956 format->setInt32("protected", true);
1957 }
Ronghua Wu8db88132015-04-22 13:51:35 -07001958
Ronghua Wuc8a70d32015-04-29 16:26:34 -07001959 float rate = getFrameRate();
1960 if (rate > 0) {
Lajos Molnar3a474aa2015-04-24 17:10:07 -07001961 format->setFloat("operating-rate", rate * mPlaybackSettings.mSpeed);
Ronghua Wu8db88132015-04-22 13:51:35 -07001962 }
Andreas Huber3fe62152011-09-16 15:09:22 -07001963 }
1964
Ray Essickfcb8fd32018-08-07 09:39:38 -07001965 Mutex::Autolock autoLock(mDecoderLock);
1966
Wei Jiabc2fb722014-07-08 16:37:57 -07001967 if (audio) {
Lajos Molnar1d15ab52015-03-04 16:46:34 -08001968 sp<AMessage> notify = new AMessage(kWhatAudioNotify, this);
Wei Jia88703c32014-08-06 11:24:07 -07001969 ++mAudioDecoderGeneration;
1970 notify->setInt32("generation", mAudioDecoderGeneration);
1971
Wei Jiaa05f1e32016-03-25 16:31:22 -07001972 if (checkAudioModeChange) {
Wei Jia41cd4632016-05-13 10:53:30 -07001973 determineAudioModeChange(format);
Wei Jiaa05f1e32016-03-25 16:31:22 -07001974 }
Wei Jiabc2fb722014-07-08 16:37:57 -07001975 if (mOffloadAudio) {
Wei Jia14532f22015-12-29 11:28:15 -08001976 mSource->setOffloadAudio(true /* offload */);
1977
Haynes Mathew George8b635332015-03-30 17:59:47 -07001978 const bool hasVideo = (mSource->getFormat(false /*audio */) != NULL);
1979 format->setInt32("has-video", hasVideo);
Wei Jiac6cfd702014-11-11 16:33:20 -08001980 *decoder = new DecoderPassThrough(notify, mSource, mRenderer);
Hassan Shojaniacefac142017-02-06 21:02:02 -08001981 ALOGV("instantiateDecoder audio DecoderPassThrough hasVideo: %d", hasVideo);
Wei Jiabc2fb722014-07-08 16:37:57 -07001982 } else {
Wei Jia14532f22015-12-29 11:28:15 -08001983 mSource->setOffloadAudio(false /* offload */);
1984
Wei Jiaf2ae3e12016-10-27 17:10:59 -07001985 *decoder = new Decoder(notify, mSource, mPID, mUID, mRenderer);
Hassan Shojaniacefac142017-02-06 21:02:02 -08001986 ALOGV("instantiateDecoder audio Decoder");
Wei Jiabc2fb722014-07-08 16:37:57 -07001987 }
Wei Jia686e8e52017-04-03 14:08:01 -07001988 mAudioDecoderError = false;
Wei Jiabc2fb722014-07-08 16:37:57 -07001989 } else {
Lajos Molnar1d15ab52015-03-04 16:46:34 -08001990 sp<AMessage> notify = new AMessage(kWhatVideoNotify, this);
Wei Jia88703c32014-08-06 11:24:07 -07001991 ++mVideoDecoderGeneration;
1992 notify->setInt32("generation", mVideoDecoderGeneration);
1993
Chong Zhang7137ec72014-11-12 16:41:05 -08001994 *decoder = new Decoder(
Wei Jiaf2ae3e12016-10-27 17:10:59 -07001995 notify, mSource, mPID, mUID, mRenderer, mSurface, mCCDecoder);
Wei Jia686e8e52017-04-03 14:08:01 -07001996 mVideoDecoderError = false;
Lajos Molnard9fd6312014-11-06 11:00:00 -08001997
1998 // enable FRC if high-quality AV sync is requested, even if not
Lajos Molnar1de1e252015-04-30 18:18:34 -07001999 // directly queuing to display, as this will even improve textureview
Lajos Molnard9fd6312014-11-06 11:00:00 -08002000 // playback.
2001 {
Marco Nelissen96626b72016-12-01 13:58:59 -08002002 if (property_get_bool("persist.sys.media.avsync", false)) {
Lajos Molnard9fd6312014-11-06 11:00:00 -08002003 format->setInt32("auto-frc", 1);
2004 }
2005 }
Wei Jiabc2fb722014-07-08 16:37:57 -07002006 }
Lajos Molnar1cd13982014-01-17 15:12:51 -08002007 (*decoder)->init();
Hassan Shojaniacefac142017-02-06 21:02:02 -08002008
2009 // Modular DRM
2010 if (mIsDrmProtected) {
2011 format->setPointer("crypto", mCrypto.get());
2012 ALOGV("instantiateDecoder: mCrypto: %p (%d) isSecure: %d", mCrypto.get(),
2013 (mCrypto != NULL ? mCrypto->getStrongCount() : 0),
2014 (mSourceFlags & Source::FLAG_SECURE) != 0);
2015 }
2016
Andreas Huber84066782011-08-16 09:34:26 -07002017 (*decoder)->configure(format);
Andreas Huberf9334412010-12-15 15:17:42 -08002018
Praveen Chavanbbaa1442016-04-08 13:33:49 -07002019 if (!audio) {
2020 sp<AMessage> params = new AMessage();
2021 float rate = getFrameRate();
2022 if (rate > 0) {
2023 params->setFloat("frame-rate-total", rate);
2024 }
2025
2026 sp<MetaData> fileMeta = getFileMeta();
2027 if (fileMeta != NULL) {
2028 int32_t videoTemporalLayerCount;
2029 if (fileMeta->findInt32(kKeyTemporalLayerCount, &videoTemporalLayerCount)
2030 && videoTemporalLayerCount > 0) {
2031 params->setInt32("temporal-layer-count", videoTemporalLayerCount);
2032 }
2033 }
2034
2035 if (params->countEntries() > 0) {
2036 (*decoder)->setParameters(params);
2037 }
2038 }
Andreas Huberf9334412010-12-15 15:17:42 -08002039 return OK;
2040}
2041
Chong Zhangced1c2f2014-08-08 15:22:35 -07002042void NuPlayer::updateVideoSize(
2043 const sp<AMessage> &inputFormat,
2044 const sp<AMessage> &outputFormat) {
2045 if (inputFormat == NULL) {
2046 ALOGW("Unknown video size, reporting 0x0!");
2047 notifyListener(MEDIA_SET_VIDEO_SIZE, 0, 0);
2048 return;
2049 }
Wei Jia9737d342016-12-28 14:59:59 -08002050 int32_t err = OK;
2051 inputFormat->findInt32("err", &err);
2052 if (err == -EWOULDBLOCK) {
2053 ALOGW("Video meta is not available yet!");
2054 return;
2055 }
2056 if (err != OK) {
2057 ALOGW("Something is wrong with video meta!");
2058 return;
2059 }
Chong Zhangced1c2f2014-08-08 15:22:35 -07002060
2061 int32_t displayWidth, displayHeight;
Chong Zhangced1c2f2014-08-08 15:22:35 -07002062 if (outputFormat != NULL) {
2063 int32_t width, height;
2064 CHECK(outputFormat->findInt32("width", &width));
2065 CHECK(outputFormat->findInt32("height", &height));
2066
2067 int32_t cropLeft, cropTop, cropRight, cropBottom;
2068 CHECK(outputFormat->findRect(
2069 "crop",
2070 &cropLeft, &cropTop, &cropRight, &cropBottom));
2071
2072 displayWidth = cropRight - cropLeft + 1;
2073 displayHeight = cropBottom - cropTop + 1;
2074
2075 ALOGV("Video output format changed to %d x %d "
2076 "(crop: %d x %d @ (%d, %d))",
2077 width, height,
2078 displayWidth,
2079 displayHeight,
2080 cropLeft, cropTop);
2081 } else {
2082 CHECK(inputFormat->findInt32("width", &displayWidth));
2083 CHECK(inputFormat->findInt32("height", &displayHeight));
2084
2085 ALOGV("Video input format %d x %d", displayWidth, displayHeight);
2086 }
2087
2088 // Take into account sample aspect ratio if necessary:
2089 int32_t sarWidth, sarHeight;
2090 if (inputFormat->findInt32("sar-width", &sarWidth)
Wei Jia095bc252017-01-27 10:16:16 -08002091 && inputFormat->findInt32("sar-height", &sarHeight)
2092 && sarWidth > 0 && sarHeight > 0) {
Chong Zhangced1c2f2014-08-08 15:22:35 -07002093 ALOGV("Sample aspect ratio %d : %d", sarWidth, sarHeight);
2094
2095 displayWidth = (displayWidth * sarWidth) / sarHeight;
2096
2097 ALOGV("display dimensions %d x %d", displayWidth, displayHeight);
Wei Jiadf6c6af2016-09-29 11:27:15 -07002098 } else {
2099 int32_t width, height;
2100 if (inputFormat->findInt32("display-width", &width)
2101 && inputFormat->findInt32("display-height", &height)
2102 && width > 0 && height > 0
2103 && displayWidth > 0 && displayHeight > 0) {
2104 if (displayHeight * (int64_t)width / height > (int64_t)displayWidth) {
2105 displayHeight = (int32_t)(displayWidth * (int64_t)height / width);
2106 } else {
2107 displayWidth = (int32_t)(displayHeight * (int64_t)width / height);
2108 }
2109 ALOGV("Video display width and height are overridden to %d x %d",
2110 displayWidth, displayHeight);
2111 }
Chong Zhangced1c2f2014-08-08 15:22:35 -07002112 }
2113
2114 int32_t rotationDegrees;
2115 if (!inputFormat->findInt32("rotation-degrees", &rotationDegrees)) {
2116 rotationDegrees = 0;
2117 }
2118
2119 if (rotationDegrees == 90 || rotationDegrees == 270) {
2120 int32_t tmp = displayWidth;
2121 displayWidth = displayHeight;
2122 displayHeight = tmp;
2123 }
2124
2125 notifyListener(
2126 MEDIA_SET_VIDEO_SIZE,
2127 displayWidth,
2128 displayHeight);
2129}
2130
Chong Zhangdcb89b32013-08-06 09:44:47 -07002131void NuPlayer::notifyListener(int msg, int ext1, int ext2, const Parcel *in) {
Andreas Huber43c3e6c2011-01-05 12:17:08 -08002132 if (mDriver == NULL) {
Andreas Huberf9334412010-12-15 15:17:42 -08002133 return;
2134 }
2135
Andreas Huber43c3e6c2011-01-05 12:17:08 -08002136 sp<NuPlayerDriver> driver = mDriver.promote();
Andreas Huberf9334412010-12-15 15:17:42 -08002137
Andreas Huber43c3e6c2011-01-05 12:17:08 -08002138 if (driver == NULL) {
Andreas Huberf9334412010-12-15 15:17:42 -08002139 return;
2140 }
2141
Chong Zhangdcb89b32013-08-06 09:44:47 -07002142 driver->notifyListener(msg, ext1, ext2, in);
Andreas Huberf9334412010-12-15 15:17:42 -08002143}
2144
Chong Zhang7137ec72014-11-12 16:41:05 -08002145void NuPlayer::flushDecoder(bool audio, bool needShutdown) {
Andreas Huber14f76722013-01-15 09:04:18 -08002146 ALOGV("[%s] flushDecoder needShutdown=%d",
2147 audio ? "audio" : "video", needShutdown);
2148
Chong Zhang7137ec72014-11-12 16:41:05 -08002149 const sp<DecoderBase> &decoder = getDecoder(audio);
Lajos Molnar87603c02014-08-20 19:25:30 -07002150 if (decoder == NULL) {
Steve Blockdf64d152012-01-04 20:05:49 +00002151 ALOGI("flushDecoder %s without decoder present",
Andreas Huber6e3d3112011-11-28 12:36:11 -08002152 audio ? "audio" : "video");
Lajos Molnar87603c02014-08-20 19:25:30 -07002153 return;
Andreas Huber6e3d3112011-11-28 12:36:11 -08002154 }
2155
Andreas Huber1aef2112011-01-04 14:01:29 -08002156 // Make sure we don't continue to scan sources until we finish flushing.
2157 ++mScanSourcesGeneration;
Chong Zhang3b032b32015-04-17 15:49:06 -07002158 if (mScanSourcesPending) {
Toshikazu Saitocbcbb792015-09-15 14:53:01 +09002159 if (!needShutdown) {
2160 mDeferredActions.push_back(
2161 new SimpleAction(&NuPlayer::performScanSources));
2162 }
Chong Zhang3b032b32015-04-17 15:49:06 -07002163 mScanSourcesPending = false;
2164 }
Andreas Huber1aef2112011-01-04 14:01:29 -08002165
Chong Zhang7137ec72014-11-12 16:41:05 -08002166 decoder->signalFlush();
Andreas Huber1aef2112011-01-04 14:01:29 -08002167
2168 FlushStatus newStatus =
2169 needShutdown ? FLUSHING_DECODER_SHUTDOWN : FLUSHING_DECODER;
2170
Lajos Molnarfcd3e942015-03-31 10:06:48 -07002171 mFlushComplete[audio][false /* isDecoder */] = (mRenderer == NULL);
Andy Hung8d121d42014-10-03 09:53:53 -07002172 mFlushComplete[audio][true /* isDecoder */] = false;
Andreas Huber1aef2112011-01-04 14:01:29 -08002173 if (audio) {
Wei Jia53904f32014-07-29 10:22:53 -07002174 ALOGE_IF(mFlushingAudio != NONE,
2175 "audio flushDecoder() is called in state %d", mFlushingAudio);
Andreas Huber1aef2112011-01-04 14:01:29 -08002176 mFlushingAudio = newStatus;
Andreas Huber1aef2112011-01-04 14:01:29 -08002177 } else {
Wei Jia53904f32014-07-29 10:22:53 -07002178 ALOGE_IF(mFlushingVideo != NONE,
2179 "video flushDecoder() is called in state %d", mFlushingVideo);
Andreas Huber1aef2112011-01-04 14:01:29 -08002180 mFlushingVideo = newStatus;
Andreas Huber1aef2112011-01-04 14:01:29 -08002181 }
2182}
2183
Chong Zhangced1c2f2014-08-08 15:22:35 -07002184void NuPlayer::queueDecoderShutdown(
2185 bool audio, bool video, const sp<AMessage> &reply) {
2186 ALOGI("queueDecoderShutdown audio=%d, video=%d", audio, video);
Andreas Huber84066782011-08-16 09:34:26 -07002187
Chong Zhangced1c2f2014-08-08 15:22:35 -07002188 mDeferredActions.push_back(
Wei Jiafef808d2014-10-31 17:57:05 -07002189 new FlushDecoderAction(
2190 audio ? FLUSH_CMD_SHUTDOWN : FLUSH_CMD_NONE,
2191 video ? FLUSH_CMD_SHUTDOWN : FLUSH_CMD_NONE));
Andreas Huber84066782011-08-16 09:34:26 -07002192
Chong Zhangced1c2f2014-08-08 15:22:35 -07002193 mDeferredActions.push_back(
2194 new SimpleAction(&NuPlayer::performScanSources));
Andreas Huber84066782011-08-16 09:34:26 -07002195
Chong Zhangced1c2f2014-08-08 15:22:35 -07002196 mDeferredActions.push_back(new PostMessageAction(reply));
2197
2198 processDeferredActions();
Andreas Huber84066782011-08-16 09:34:26 -07002199}
2200
James Dong0d268a32012-08-31 12:18:27 -07002201status_t NuPlayer::setVideoScalingMode(int32_t mode) {
2202 mVideoScalingMode = mode;
Lajos Molnar1de1e252015-04-30 18:18:34 -07002203 if (mSurface != NULL) {
2204 status_t ret = native_window_set_scaling_mode(mSurface.get(), mVideoScalingMode);
James Dong0d268a32012-08-31 12:18:27 -07002205 if (ret != OK) {
2206 ALOGE("Failed to set scaling mode (%d): %s",
2207 -ret, strerror(-ret));
2208 return ret;
2209 }
2210 }
2211 return OK;
2212}
2213
Chong Zhangdcb89b32013-08-06 09:44:47 -07002214status_t NuPlayer::getTrackInfo(Parcel* reply) const {
Lajos Molnar1d15ab52015-03-04 16:46:34 -08002215 sp<AMessage> msg = new AMessage(kWhatGetTrackInfo, this);
Chong Zhangdcb89b32013-08-06 09:44:47 -07002216 msg->setPointer("reply", reply);
2217
2218 sp<AMessage> response;
2219 status_t err = msg->postAndAwaitResponse(&response);
2220 return err;
2221}
2222
Robert Shih7c4f0d72014-07-09 18:53:31 -07002223status_t NuPlayer::getSelectedTrack(int32_t type, Parcel* reply) const {
Lajos Molnar1d15ab52015-03-04 16:46:34 -08002224 sp<AMessage> msg = new AMessage(kWhatGetSelectedTrack, this);
Robert Shih7c4f0d72014-07-09 18:53:31 -07002225 msg->setPointer("reply", reply);
2226 msg->setInt32("type", type);
2227
2228 sp<AMessage> response;
2229 status_t err = msg->postAndAwaitResponse(&response);
2230 if (err == OK && response != NULL) {
2231 CHECK(response->findInt32("err", &err));
2232 }
2233 return err;
2234}
2235
Robert Shih6ffb1fd2014-10-29 16:24:32 -07002236status_t NuPlayer::selectTrack(size_t trackIndex, bool select, int64_t timeUs) {
Lajos Molnar1d15ab52015-03-04 16:46:34 -08002237 sp<AMessage> msg = new AMessage(kWhatSelectTrack, this);
Chong Zhangdcb89b32013-08-06 09:44:47 -07002238 msg->setSize("trackIndex", trackIndex);
2239 msg->setInt32("select", select);
Robert Shih6ffb1fd2014-10-29 16:24:32 -07002240 msg->setInt64("timeUs", timeUs);
Chong Zhangdcb89b32013-08-06 09:44:47 -07002241
2242 sp<AMessage> response;
2243 status_t err = msg->postAndAwaitResponse(&response);
2244
Chong Zhang404fced2014-06-11 14:45:31 -07002245 if (err != OK) {
2246 return err;
2247 }
2248
2249 if (!response->findInt32("err", &err)) {
2250 err = OK;
2251 }
2252
Chong Zhangdcb89b32013-08-06 09:44:47 -07002253 return err;
2254}
2255
Ronghua Wua73d9e02014-10-08 15:13:29 -07002256status_t NuPlayer::getCurrentPosition(int64_t *mediaUs) {
2257 sp<Renderer> renderer = mRenderer;
2258 if (renderer == NULL) {
2259 return NO_INIT;
2260 }
2261
2262 return renderer->getCurrentPosition(mediaUs);
2263}
2264
Ray Essickffc941f2018-05-18 11:08:37 -07002265void NuPlayer::getStats(Vector<sp<AMessage> > *trackStats) {
2266 CHECK(trackStats != NULL);
Praveen Chavane1e5d7a2015-05-19 19:09:48 -07002267
Ray Essickfcb8fd32018-08-07 09:39:38 -07002268 trackStats->clear();
Ray Essickffc941f2018-05-18 11:08:37 -07002269
Ray Essickfcb8fd32018-08-07 09:39:38 -07002270 Mutex::Autolock autoLock(mDecoderLock);
2271 if (mVideoDecoder != NULL) {
2272 trackStats->push_back(mVideoDecoder->getStats());
2273 }
2274 if (mAudioDecoder != NULL) {
2275 trackStats->push_back(mAudioDecoder->getStats());
2276 }
Ronghua Wua73d9e02014-10-08 15:13:29 -07002277}
2278
Marco Nelissenf0b72b52014-09-16 15:43:44 -07002279sp<MetaData> NuPlayer::getFileMeta() {
2280 return mSource->getFileFormatMeta();
2281}
2282
Ronghua Wuc8a70d32015-04-29 16:26:34 -07002283float NuPlayer::getFrameRate() {
2284 sp<MetaData> meta = mSource->getFormatMeta(false /* audio */);
2285 if (meta == NULL) {
2286 return 0;
2287 }
2288 int32_t rate;
2289 if (!meta->findInt32(kKeyFrameRate, &rate)) {
2290 // fall back to try file meta
2291 sp<MetaData> fileMeta = getFileMeta();
2292 if (fileMeta == NULL) {
2293 ALOGW("source has video meta but not file meta");
2294 return -1;
2295 }
2296 int32_t fileMetaRate;
2297 if (!fileMeta->findInt32(kKeyFrameRate, &fileMetaRate)) {
2298 return -1;
2299 }
2300 return fileMetaRate;
2301 }
2302 return rate;
2303}
2304
Andreas Huberb7c8e912012-11-27 15:02:53 -08002305void NuPlayer::schedulePollDuration() {
Lajos Molnar1d15ab52015-03-04 16:46:34 -08002306 sp<AMessage> msg = new AMessage(kWhatPollDuration, this);
Andreas Huberb7c8e912012-11-27 15:02:53 -08002307 msg->setInt32("generation", mPollDurationGeneration);
2308 msg->post();
2309}
2310
2311void NuPlayer::cancelPollDuration() {
2312 ++mPollDurationGeneration;
2313}
2314
Andreas Hubera1f8ab02012-11-30 10:53:22 -08002315void NuPlayer::processDeferredActions() {
2316 while (!mDeferredActions.empty()) {
2317 // We won't execute any deferred actions until we're no longer in
2318 // an intermediate state, i.e. one more more decoders are currently
2319 // flushing or shutting down.
2320
Andreas Hubera1f8ab02012-11-30 10:53:22 -08002321 if (mFlushingAudio != NONE || mFlushingVideo != NONE) {
2322 // We're currently flushing, postpone the reset until that's
2323 // completed.
2324
2325 ALOGV("postponing action mFlushingAudio=%d, mFlushingVideo=%d",
2326 mFlushingAudio, mFlushingVideo);
2327
2328 break;
2329 }
2330
2331 sp<Action> action = *mDeferredActions.begin();
2332 mDeferredActions.erase(mDeferredActions.begin());
2333
2334 action->execute(this);
2335 }
2336}
2337
Wei Jiac5de0912016-11-18 10:22:14 -08002338void NuPlayer::performSeek(int64_t seekTimeUs, MediaPlayerSeekMode mode) {
2339 ALOGV("performSeek seekTimeUs=%lld us (%.2f secs), mode=%d",
2340 (long long)seekTimeUs, seekTimeUs / 1E6, mode);
Andreas Hubera1f8ab02012-11-30 10:53:22 -08002341
Andy Hungadf34bf2014-09-03 18:22:22 -07002342 if (mSource == NULL) {
2343 // This happens when reset occurs right before the loop mode
2344 // asynchronously seeks to the start of the stream.
2345 LOG_ALWAYS_FATAL_IF(mAudioDecoder != NULL || mVideoDecoder != NULL,
2346 "mSource is NULL and decoders not NULL audio(%p) video(%p)",
2347 mAudioDecoder.get(), mVideoDecoder.get());
2348 return;
2349 }
Robert Shih1a5c8592015-08-04 18:07:44 -07002350 mPreviousSeekTimeUs = seekTimeUs;
Wei Jiac5de0912016-11-18 10:22:14 -08002351 mSource->seekTo(seekTimeUs, mode);
Robert Shihd3b0bbb2014-07-23 15:00:25 -07002352 ++mTimedTextGeneration;
Andreas Hubera1f8ab02012-11-30 10:53:22 -08002353
Andreas Hubera1f8ab02012-11-30 10:53:22 -08002354 // everything's flushed, continue playback.
2355}
2356
Wei Jiafef808d2014-10-31 17:57:05 -07002357void NuPlayer::performDecoderFlush(FlushCommand audio, FlushCommand video) {
2358 ALOGV("performDecoderFlush audio=%d, video=%d", audio, video);
Andreas Hubera1f8ab02012-11-30 10:53:22 -08002359
Wei Jiafef808d2014-10-31 17:57:05 -07002360 if ((audio == FLUSH_CMD_NONE || mAudioDecoder == NULL)
2361 && (video == FLUSH_CMD_NONE || mVideoDecoder == NULL)) {
Andreas Hubera1f8ab02012-11-30 10:53:22 -08002362 return;
2363 }
2364
Wei Jiafef808d2014-10-31 17:57:05 -07002365 if (audio != FLUSH_CMD_NONE && mAudioDecoder != NULL) {
2366 flushDecoder(true /* audio */, (audio == FLUSH_CMD_SHUTDOWN));
Andreas Hubera1f8ab02012-11-30 10:53:22 -08002367 }
2368
Wei Jiafef808d2014-10-31 17:57:05 -07002369 if (video != FLUSH_CMD_NONE && mVideoDecoder != NULL) {
2370 flushDecoder(false /* audio */, (video == FLUSH_CMD_SHUTDOWN));
Andreas Hubera1f8ab02012-11-30 10:53:22 -08002371 }
2372}
2373
2374void NuPlayer::performReset() {
2375 ALOGV("performReset");
2376
2377 CHECK(mAudioDecoder == NULL);
2378 CHECK(mVideoDecoder == NULL);
2379
Ray Essickeda32522018-02-28 12:08:28 -08002380 updatePlaybackTimer(true /* stopping */, "performReset");
2381 updateRebufferingTimer(true /* stopping */, true /* exiting */);
Ray Essick0d98c182017-11-09 13:42:42 -08002382
Andreas Hubera1f8ab02012-11-30 10:53:22 -08002383 cancelPollDuration();
2384
2385 ++mScanSourcesGeneration;
2386 mScanSourcesPending = false;
2387
Lajos Molnar09524832014-07-17 14:29:51 -07002388 if (mRendererLooper != NULL) {
2389 if (mRenderer != NULL) {
2390 mRendererLooper->unregisterHandler(mRenderer->id());
2391 }
2392 mRendererLooper->stop();
2393 mRendererLooper.clear();
2394 }
Andreas Hubera1f8ab02012-11-30 10:53:22 -08002395 mRenderer.clear();
Wei Jia57568df2014-09-22 10:16:29 -07002396 ++mRendererGeneration;
Andreas Hubera1f8ab02012-11-30 10:53:22 -08002397
2398 if (mSource != NULL) {
2399 mSource->stop();
Andreas Huberb5f25f02013-02-05 10:14:26 -08002400
Wei Jiac45a4b22016-04-15 15:30:23 -07002401 Mutex::Autolock autoLock(mSourceLock);
Andreas Hubera1f8ab02012-11-30 10:53:22 -08002402 mSource.clear();
2403 }
2404
2405 if (mDriver != NULL) {
2406 sp<NuPlayerDriver> driver = mDriver.promote();
2407 if (driver != NULL) {
2408 driver->notifyResetComplete();
2409 }
2410 }
Andreas Huber57a339c2012-12-03 11:18:00 -08002411
2412 mStarted = false;
Wei Jia8a092d32016-06-03 14:57:24 -07002413 mPrepared = false;
Ronghua Wu64c2d172015-10-07 16:52:19 -07002414 mResetting = false;
Robert Shih0c61a0d2015-07-06 15:09:10 -07002415 mSourceStarted = false;
Hassan Shojaniacefac142017-02-06 21:02:02 -08002416
2417 // Modular DRM
2418 if (mCrypto != NULL) {
2419 // decoders will be flushed before this so their mCrypto would go away on their own
2420 // TODO change to ALOGV
2421 ALOGD("performReset mCrypto: %p (%d)", mCrypto.get(),
2422 (mCrypto != NULL ? mCrypto->getStrongCount() : 0));
2423 mCrypto.clear();
2424 }
2425 mIsDrmProtected = false;
Andreas Hubera1f8ab02012-11-30 10:53:22 -08002426}
2427
2428void NuPlayer::performScanSources() {
2429 ALOGV("performScanSources");
2430
Andreas Huber57a339c2012-12-03 11:18:00 -08002431 if (!mStarted) {
2432 return;
2433 }
2434
Andreas Hubera1f8ab02012-11-30 10:53:22 -08002435 if (mAudioDecoder == NULL || mVideoDecoder == NULL) {
2436 postScanSources();
2437 }
2438}
2439
Lajos Molnar1de1e252015-04-30 18:18:34 -07002440void NuPlayer::performSetSurface(const sp<Surface> &surface) {
Andreas Huber57a339c2012-12-03 11:18:00 -08002441 ALOGV("performSetSurface");
2442
Lajos Molnar1de1e252015-04-30 18:18:34 -07002443 mSurface = surface;
Andreas Huber57a339c2012-12-03 11:18:00 -08002444
2445 // XXX - ignore error from setVideoScalingMode for now
2446 setVideoScalingMode(mVideoScalingMode);
Chong Zhang13d6faa2014-08-22 15:35:28 -07002447
2448 if (mDriver != NULL) {
2449 sp<NuPlayerDriver> driver = mDriver.promote();
2450 if (driver != NULL) {
2451 driver->notifySetSurfaceComplete();
2452 }
2453 }
Andreas Huber57a339c2012-12-03 11:18:00 -08002454}
2455
Chong Zhangf8d71772014-11-26 15:08:34 -08002456void NuPlayer::performResumeDecoders(bool needNotify) {
2457 if (needNotify) {
2458 mResumePending = true;
2459 if (mVideoDecoder == NULL) {
2460 // if audio-only, we can notify seek complete now,
2461 // as the resume operation will be relatively fast.
2462 finishResume();
2463 }
2464 }
2465
Chong Zhang7137ec72014-11-12 16:41:05 -08002466 if (mVideoDecoder != NULL) {
Chong Zhangf8d71772014-11-26 15:08:34 -08002467 // When there is continuous seek, MediaPlayer will cache the seek
2468 // position, and send down new seek request when previous seek is
2469 // complete. Let's wait for at least one video output frame before
2470 // notifying seek complete, so that the video thumbnail gets updated
2471 // when seekbar is dragged.
2472 mVideoDecoder->signalResume(needNotify);
Chong Zhang7137ec72014-11-12 16:41:05 -08002473 }
2474
2475 if (mAudioDecoder != NULL) {
Chong Zhangf8d71772014-11-26 15:08:34 -08002476 mAudioDecoder->signalResume(false /* needNotify */);
2477 }
2478}
2479
2480void NuPlayer::finishResume() {
2481 if (mResumePending) {
2482 mResumePending = false;
Wei Jia1061c9c2015-05-19 16:02:17 -07002483 notifyDriverSeekComplete();
2484 }
2485}
2486
2487void NuPlayer::notifyDriverSeekComplete() {
2488 if (mDriver != NULL) {
2489 sp<NuPlayerDriver> driver = mDriver.promote();
2490 if (driver != NULL) {
2491 driver->notifySeekComplete();
Chong Zhangf8d71772014-11-26 15:08:34 -08002492 }
Chong Zhang7137ec72014-11-12 16:41:05 -08002493 }
2494}
2495
Andreas Huber9575c962013-02-05 13:59:56 -08002496void NuPlayer::onSourceNotify(const sp<AMessage> &msg) {
2497 int32_t what;
2498 CHECK(msg->findInt32("what", &what));
2499
2500 switch (what) {
Lajos Molnarfcd3e942015-03-31 10:06:48 -07002501 case Source::kWhatInstantiateSecureDecoders:
2502 {
2503 if (mSource == NULL) {
2504 // This is a stale notification from a source that was
2505 // asynchronously preparing when the client called reset().
2506 // We handled the reset, the source is gone.
2507 break;
2508 }
2509
2510 sp<AMessage> reply;
2511 CHECK(msg->findMessage("reply", &reply));
2512 status_t err = onInstantiateSecureDecoders();
2513 reply->setInt32("err", err);
2514 reply->post();
2515 break;
2516 }
2517
Andreas Huber9575c962013-02-05 13:59:56 -08002518 case Source::kWhatPrepared:
2519 {
Hassan Shojaniacefac142017-02-06 21:02:02 -08002520 ALOGV("NuPlayer::onSourceNotify Source::kWhatPrepared source: %p", mSource.get());
Andreas Huberb5f28d42013-04-25 15:11:19 -07002521 if (mSource == NULL) {
2522 // This is a stale notification from a source that was
2523 // asynchronously preparing when the client called reset().
2524 // We handled the reset, the source is gone.
2525 break;
2526 }
2527
Andreas Huberec0c5972013-02-05 14:47:13 -08002528 int32_t err;
2529 CHECK(msg->findInt32("err", &err));
2530
Lajos Molnarfcd3e942015-03-31 10:06:48 -07002531 if (err != OK) {
2532 // shut down potential secure codecs in case client never calls reset
2533 mDeferredActions.push_back(
2534 new FlushDecoderAction(FLUSH_CMD_SHUTDOWN /* audio */,
2535 FLUSH_CMD_SHUTDOWN /* video */));
2536 processDeferredActions();
Wei Jia8a092d32016-06-03 14:57:24 -07002537 } else {
2538 mPrepared = true;
Lajos Molnarfcd3e942015-03-31 10:06:48 -07002539 }
2540
Andreas Huber9575c962013-02-05 13:59:56 -08002541 sp<NuPlayerDriver> driver = mDriver.promote();
2542 if (driver != NULL) {
Marco Nelissendd114d12014-05-28 15:23:14 -07002543 // notify duration first, so that it's definitely set when
2544 // the app received the "prepare complete" callback.
2545 int64_t durationUs;
2546 if (mSource->getDuration(&durationUs) == OK) {
2547 driver->notifyDuration(durationUs);
2548 }
Andreas Huberec0c5972013-02-05 14:47:13 -08002549 driver->notifyPrepareCompleted(err);
Andreas Huber9575c962013-02-05 13:59:56 -08002550 }
Andreas Huber99759402013-04-01 14:28:31 -07002551
Andreas Huber9575c962013-02-05 13:59:56 -08002552 break;
2553 }
2554
Hassan Shojaniacefac142017-02-06 21:02:02 -08002555 // Modular DRM
2556 case Source::kWhatDrmInfo:
2557 {
2558 Parcel parcel;
2559 sp<ABuffer> drmInfo;
2560 CHECK(msg->findBuffer("drmInfo", &drmInfo));
2561 parcel.setData(drmInfo->data(), drmInfo->size());
2562
2563 ALOGV("onSourceNotify() kWhatDrmInfo MEDIA_DRM_INFO drmInfo: %p parcel size: %zu",
2564 drmInfo.get(), parcel.dataSize());
2565
2566 notifyListener(MEDIA_DRM_INFO, 0 /* ext1 */, 0 /* ext2 */, &parcel);
2567
2568 break;
2569 }
2570
Andreas Huber9575c962013-02-05 13:59:56 -08002571 case Source::kWhatFlagsChanged:
2572 {
2573 uint32_t flags;
2574 CHECK(msg->findInt32("flags", (int32_t *)&flags));
2575
Chong Zhang4b7069d2013-09-11 12:52:43 -07002576 sp<NuPlayerDriver> driver = mDriver.promote();
2577 if (driver != NULL) {
Hassan Shojaniacefac142017-02-06 21:02:02 -08002578
2579 ALOGV("onSourceNotify() kWhatFlagsChanged FLAG_CAN_PAUSE: %d "
2580 "FLAG_CAN_SEEK_BACKWARD: %d \n\t\t\t\t FLAG_CAN_SEEK_FORWARD: %d "
2581 "FLAG_CAN_SEEK: %d FLAG_DYNAMIC_DURATION: %d \n"
2582 "\t\t\t\t FLAG_SECURE: %d FLAG_PROTECTED: %d",
2583 (flags & Source::FLAG_CAN_PAUSE) != 0,
2584 (flags & Source::FLAG_CAN_SEEK_BACKWARD) != 0,
2585 (flags & Source::FLAG_CAN_SEEK_FORWARD) != 0,
2586 (flags & Source::FLAG_CAN_SEEK) != 0,
2587 (flags & Source::FLAG_DYNAMIC_DURATION) != 0,
2588 (flags & Source::FLAG_SECURE) != 0,
2589 (flags & Source::FLAG_PROTECTED) != 0);
2590
Wei Jia895651b2014-12-10 17:31:52 -08002591 if ((flags & NuPlayer::Source::FLAG_CAN_SEEK) == 0) {
2592 driver->notifyListener(
2593 MEDIA_INFO, MEDIA_INFO_NOT_SEEKABLE, 0);
2594 }
Chong Zhang4b7069d2013-09-11 12:52:43 -07002595 driver->notifyFlagsChanged(flags);
2596 }
2597
Andreas Huber9575c962013-02-05 13:59:56 -08002598 if ((mSourceFlags & Source::FLAG_DYNAMIC_DURATION)
2599 && (!(flags & Source::FLAG_DYNAMIC_DURATION))) {
2600 cancelPollDuration();
2601 } else if (!(mSourceFlags & Source::FLAG_DYNAMIC_DURATION)
2602 && (flags & Source::FLAG_DYNAMIC_DURATION)
2603 && (mAudioDecoder != NULL || mVideoDecoder != NULL)) {
2604 schedulePollDuration();
2605 }
2606
2607 mSourceFlags = flags;
2608 break;
2609 }
2610
2611 case Source::kWhatVideoSizeChanged:
2612 {
Chong Zhangced1c2f2014-08-08 15:22:35 -07002613 sp<AMessage> format;
2614 CHECK(msg->findMessage("format", &format));
Andreas Huber9575c962013-02-05 13:59:56 -08002615
Chong Zhangced1c2f2014-08-08 15:22:35 -07002616 updateVideoSize(format);
Andreas Huber9575c962013-02-05 13:59:56 -08002617 break;
2618 }
2619
Chong Zhang2a3cc9a2014-08-21 17:48:26 -07002620 case Source::kWhatBufferingUpdate:
2621 {
2622 int32_t percentage;
2623 CHECK(msg->findInt32("percentage", &percentage));
2624
2625 notifyListener(MEDIA_BUFFERING_UPDATE, percentage, 0);
2626 break;
2627 }
2628
Chong Zhangefbb6192015-01-30 17:13:27 -08002629 case Source::kWhatPauseOnBufferingStart:
2630 {
2631 // ignore if not playing
Chong Zhang8a048332015-05-06 15:16:28 -07002632 if (mStarted) {
Chong Zhangefbb6192015-01-30 17:13:27 -08002633 ALOGI("buffer low, pausing...");
2634
Ray Essick58e0f7a2017-11-21 10:59:38 -08002635 startRebufferingTimer();
Chong Zhang8a048332015-05-06 15:16:28 -07002636 mPausedForBuffering = true;
Chong Zhangefbb6192015-01-30 17:13:27 -08002637 onPause();
2638 }
Wei Jiabfe82072016-05-20 09:21:50 -07002639 notifyListener(MEDIA_INFO, MEDIA_INFO_BUFFERING_START, 0);
Roger Jönssonb50e83e2013-01-21 16:26:41 +01002640 break;
2641 }
2642
Chong Zhangefbb6192015-01-30 17:13:27 -08002643 case Source::kWhatResumeOnBufferingEnd:
2644 {
2645 // ignore if not playing
Chong Zhang8a048332015-05-06 15:16:28 -07002646 if (mStarted) {
Chong Zhangefbb6192015-01-30 17:13:27 -08002647 ALOGI("buffer ready, resuming...");
2648
Ray Essickeda32522018-02-28 12:08:28 -08002649 updateRebufferingTimer(true /* stopping */, false /* exiting */);
Chong Zhang8a048332015-05-06 15:16:28 -07002650 mPausedForBuffering = false;
2651
2652 // do not resume yet if client didn't unpause
2653 if (!mPausedByClient) {
2654 onResume();
2655 }
Chong Zhangefbb6192015-01-30 17:13:27 -08002656 }
Wei Jia3bed45a2016-02-17 11:06:47 -08002657 notifyListener(MEDIA_INFO, MEDIA_INFO_BUFFERING_END, 0);
Roger Jönssonb50e83e2013-01-21 16:26:41 +01002658 break;
2659 }
2660
Chong Zhangefbb6192015-01-30 17:13:27 -08002661 case Source::kWhatCacheStats:
2662 {
2663 int32_t kbps;
2664 CHECK(msg->findInt32("bandwidth", &kbps));
2665
2666 notifyListener(MEDIA_INFO, MEDIA_INFO_NETWORK_BANDWIDTH, kbps);
2667 break;
2668 }
2669
Chong Zhangdcb89b32013-08-06 09:44:47 -07002670 case Source::kWhatSubtitleData:
2671 {
2672 sp<ABuffer> buffer;
2673 CHECK(msg->findBuffer("buffer", &buffer));
2674
Chong Zhang404fced2014-06-11 14:45:31 -07002675 sendSubtitleData(buffer, 0 /* baseIndex */);
Chong Zhangdcb89b32013-08-06 09:44:47 -07002676 break;
2677 }
2678
Robert Shih08528432015-04-08 09:06:54 -07002679 case Source::kWhatTimedMetaData:
2680 {
2681 sp<ABuffer> buffer;
2682 if (!msg->findBuffer("buffer", &buffer)) {
2683 notifyListener(MEDIA_INFO, MEDIA_INFO_METADATA_UPDATE, 0);
2684 } else {
2685 sendTimedMetaData(buffer);
2686 }
2687 break;
2688 }
2689
Robert Shihd3b0bbb2014-07-23 15:00:25 -07002690 case Source::kWhatTimedTextData:
2691 {
2692 int32_t generation;
2693 if (msg->findInt32("generation", &generation)
2694 && generation != mTimedTextGeneration) {
2695 break;
2696 }
2697
2698 sp<ABuffer> buffer;
2699 CHECK(msg->findBuffer("buffer", &buffer));
2700
2701 sp<NuPlayerDriver> driver = mDriver.promote();
2702 if (driver == NULL) {
2703 break;
2704 }
2705
2706 int posMs;
2707 int64_t timeUs, posUs;
2708 driver->getCurrentPosition(&posMs);
Chih-Hung Hsieh62309d52018-12-11 13:54:02 -08002709 posUs = (int64_t) posMs * 1000LL;
Robert Shihd3b0bbb2014-07-23 15:00:25 -07002710 CHECK(buffer->meta()->findInt64("timeUs", &timeUs));
2711
2712 if (posUs < timeUs) {
2713 if (!msg->findInt32("generation", &generation)) {
2714 msg->setInt32("generation", mTimedTextGeneration);
2715 }
2716 msg->post(timeUs - posUs);
2717 } else {
2718 sendTimedTextData(buffer);
2719 }
2720 break;
2721 }
2722
Andreas Huber14f76722013-01-15 09:04:18 -08002723 case Source::kWhatQueueDecoderShutdown:
2724 {
2725 int32_t audio, video;
2726 CHECK(msg->findInt32("audio", &audio));
2727 CHECK(msg->findInt32("video", &video));
2728
2729 sp<AMessage> reply;
2730 CHECK(msg->findMessage("reply", &reply));
2731
2732 queueDecoderShutdown(audio, video, reply);
2733 break;
2734 }
2735
Ronghua Wu80276872014-08-28 15:50:29 -07002736 case Source::kWhatDrmNoLicense:
2737 {
2738 notifyListener(MEDIA_ERROR, MEDIA_ERROR_UNKNOWN, ERROR_DRM_NO_LICENSE);
2739 break;
2740 }
2741
Kim Sungyeond3c6b322018-03-02 14:41:19 +09002742 case Source::kWhatIMSRxNotice:
2743 {
2744 sp<AMessage> IMSRxNotice;
2745 CHECK(msg->findMessage("message", &IMSRxNotice));
2746 sendIMSRxNotice(IMSRxNotice);
2747 break;
2748 }
2749
Andreas Huber9575c962013-02-05 13:59:56 -08002750 default:
2751 TRESPASS();
2752 }
2753}
2754
Chong Zhanga7fa1d92014-06-11 14:49:23 -07002755void NuPlayer::onClosedCaptionNotify(const sp<AMessage> &msg) {
2756 int32_t what;
2757 CHECK(msg->findInt32("what", &what));
2758
2759 switch (what) {
2760 case NuPlayer::CCDecoder::kWhatClosedCaptionData:
2761 {
2762 sp<ABuffer> buffer;
2763 CHECK(msg->findBuffer("buffer", &buffer));
2764
2765 size_t inbandTracks = 0;
2766 if (mSource != NULL) {
2767 inbandTracks = mSource->getTrackCount();
2768 }
2769
2770 sendSubtitleData(buffer, inbandTracks);
2771 break;
2772 }
2773
2774 case NuPlayer::CCDecoder::kWhatTrackAdded:
2775 {
2776 notifyListener(MEDIA_INFO, MEDIA_INFO_METADATA_UPDATE, 0);
2777
2778 break;
2779 }
2780
2781 default:
2782 TRESPASS();
2783 }
2784
2785
2786}
2787
Chong Zhang404fced2014-06-11 14:45:31 -07002788void NuPlayer::sendSubtitleData(const sp<ABuffer> &buffer, int32_t baseIndex) {
2789 int32_t trackIndex;
2790 int64_t timeUs, durationUs;
Robert Shihd83d4f42018-02-24 19:02:46 -08002791 CHECK(buffer->meta()->findInt32("track-index", &trackIndex));
Chong Zhang404fced2014-06-11 14:45:31 -07002792 CHECK(buffer->meta()->findInt64("timeUs", &timeUs));
2793 CHECK(buffer->meta()->findInt64("durationUs", &durationUs));
2794
2795 Parcel in;
2796 in.writeInt32(trackIndex + baseIndex);
2797 in.writeInt64(timeUs);
2798 in.writeInt64(durationUs);
2799 in.writeInt32(buffer->size());
2800 in.writeInt32(buffer->size());
2801 in.write(buffer->data(), buffer->size());
2802
2803 notifyListener(MEDIA_SUBTITLE_DATA, 0, 0, &in);
2804}
Robert Shihd3b0bbb2014-07-23 15:00:25 -07002805
Robert Shih08528432015-04-08 09:06:54 -07002806void NuPlayer::sendTimedMetaData(const sp<ABuffer> &buffer) {
2807 int64_t timeUs;
2808 CHECK(buffer->meta()->findInt64("timeUs", &timeUs));
2809
2810 Parcel in;
2811 in.writeInt64(timeUs);
2812 in.writeInt32(buffer->size());
2813 in.writeInt32(buffer->size());
2814 in.write(buffer->data(), buffer->size());
2815
2816 notifyListener(MEDIA_META_DATA, 0, 0, &in);
2817}
2818
Robert Shihd3b0bbb2014-07-23 15:00:25 -07002819void NuPlayer::sendTimedTextData(const sp<ABuffer> &buffer) {
2820 const void *data;
2821 size_t size = 0;
2822 int64_t timeUs;
Marco Nelissen55e2f4c2015-09-04 15:57:15 -07002823 int32_t flag = TextDescriptions::IN_BAND_TEXT_3GPP;
Robert Shihd3b0bbb2014-07-23 15:00:25 -07002824
2825 AString mime;
2826 CHECK(buffer->meta()->findString("mime", &mime));
2827 CHECK(strcasecmp(mime.c_str(), MEDIA_MIMETYPE_TEXT_3GPP) == 0);
2828
2829 data = buffer->data();
2830 size = buffer->size();
2831
2832 Parcel parcel;
2833 if (size > 0) {
2834 CHECK(buffer->meta()->findInt64("timeUs", &timeUs));
Marco Nelissen55e2f4c2015-09-04 15:57:15 -07002835 int32_t global = 0;
2836 if (buffer->meta()->findInt32("global", &global) && global) {
2837 flag |= TextDescriptions::GLOBAL_DESCRIPTIONS;
2838 } else {
2839 flag |= TextDescriptions::LOCAL_DESCRIPTIONS;
2840 }
Robert Shihd3b0bbb2014-07-23 15:00:25 -07002841 TextDescriptions::getParcelOfDescriptions(
2842 (const uint8_t *)data, size, flag, timeUs / 1000, &parcel);
2843 }
2844
2845 if ((parcel.dataSize() > 0)) {
2846 notifyListener(MEDIA_TIMED_TEXT, 0, 0, &parcel);
2847 } else { // send an empty timed text
2848 notifyListener(MEDIA_TIMED_TEXT, 0, 0);
2849 }
2850}
Hassan Shojaniacefac142017-02-06 21:02:02 -08002851
Kim Sungyeond3c6b322018-03-02 14:41:19 +09002852void NuPlayer::sendIMSRxNotice(const sp<AMessage> &msg) {
Byeongjo Parkb0225aa2018-04-20 14:00:15 +09002853 int32_t payloadType;
Kim Sungyeond3c6b322018-03-02 14:41:19 +09002854
Kim Sungyeond3c6b322018-03-02 14:41:19 +09002855 CHECK(msg->findInt32("payload-type", &payloadType));
Kim Sungyeond3c6b322018-03-02 14:41:19 +09002856
2857 Parcel in;
2858 in.writeInt32(payloadType);
Kim Sungyeond3c6b322018-03-02 14:41:19 +09002859
2860 switch (payloadType) {
Byeongjo Parkb0225aa2018-04-20 14:00:15 +09002861 case NuPlayer::RTPSource::RTCP_TSFB: // RTCP TSFB
2862 case NuPlayer::RTPSource::RTCP_PSFB: // RTCP PSFB
Kim Sungyeond194b8c2019-03-17 22:04:14 +09002863 case NuPlayer::RTPSource::RTP_AUTODOWN:
Kim Sungyeond3c6b322018-03-02 14:41:19 +09002864 {
Byeongjo Parkb0225aa2018-04-20 14:00:15 +09002865 int32_t feedbackType, id;
2866 CHECK(msg->findInt32("feedback-type", &feedbackType));
2867 CHECK(msg->findInt32("sender", &id));
2868 in.writeInt32(feedbackType);
2869 in.writeInt32(id);
2870 if (payloadType == NuPlayer::RTPSource::RTCP_TSFB) {
2871 int32_t bitrate;
2872 CHECK(msg->findInt32("bit-rate", &bitrate));
2873 in.writeInt32(bitrate);
2874 }
Kim Sungyeond3c6b322018-03-02 14:41:19 +09002875 break;
2876 }
Kim Sungyeon23b23932019-07-18 17:48:32 +09002877 case NuPlayer::RTPSource::RTP_QUALITY:
2878 {
2879 int32_t feedbackType, bitrate;
2880 int32_t highestSeqNum, baseSeqNum, prevExpected;
2881 int32_t numBufRecv, prevNumBufRecv;
2882 CHECK(msg->findInt32("feedback-type", &feedbackType));
2883 CHECK(msg->findInt32("bit-rate", &bitrate));
2884 CHECK(msg->findInt32("highest-seq-num", &highestSeqNum));
2885 CHECK(msg->findInt32("base-seq-num", &baseSeqNum));
2886 CHECK(msg->findInt32("prev-expected", &prevExpected));
2887 CHECK(msg->findInt32("num-buf-recv", &numBufRecv));
2888 CHECK(msg->findInt32("prev-num-buf-recv", &prevNumBufRecv));
2889 in.writeInt32(feedbackType);
2890 in.writeInt32(bitrate);
2891 in.writeInt32(highestSeqNum);
2892 in.writeInt32(baseSeqNum);
2893 in.writeInt32(prevExpected);
2894 in.writeInt32(numBufRecv);
2895 in.writeInt32(prevNumBufRecv);
2896 break;
2897 }
Byeongjo Parkb0225aa2018-04-20 14:00:15 +09002898 case NuPlayer::RTPSource::RTP_CVO:
Kim Sungyeond3c6b322018-03-02 14:41:19 +09002899 {
Byeongjo Parkb0225aa2018-04-20 14:00:15 +09002900 int32_t cvo;
2901 CHECK(msg->findInt32("cvo", &cvo));
2902 in.writeInt32(cvo);
Kim Sungyeond3c6b322018-03-02 14:41:19 +09002903 break;
2904 }
2905 default:
2906 break;
2907 }
2908
2909 notifyListener(MEDIA_IMS_RX_NOTICE, 0, 0, &in);
2910}
2911
Hassan Shojaniaff63de72017-04-26 15:10:42 -07002912const char *NuPlayer::getDataSourceType() {
2913 switch (mDataSourceType) {
2914 case DATA_SOURCE_TYPE_HTTP_LIVE:
2915 return "HTTPLive";
2916
Byeongjo Park5e27b1b2018-07-12 15:36:03 +09002917 case DATA_SOURCE_TYPE_RTP:
2918 return "RTP";
2919
Hassan Shojaniaff63de72017-04-26 15:10:42 -07002920 case DATA_SOURCE_TYPE_RTSP:
2921 return "RTSP";
2922
2923 case DATA_SOURCE_TYPE_GENERIC_URL:
2924 return "GenURL";
2925
2926 case DATA_SOURCE_TYPE_GENERIC_FD:
2927 return "GenFD";
2928
2929 case DATA_SOURCE_TYPE_MEDIA:
2930 return "Media";
2931
2932 case DATA_SOURCE_TYPE_STREAM:
2933 return "Stream";
2934
2935 case DATA_SOURCE_TYPE_NONE:
2936 default:
2937 return "None";
2938 }
2939 }
2940
Hassan Shojaniacefac142017-02-06 21:02:02 -08002941// Modular DRM begin
2942status_t NuPlayer::prepareDrm(const uint8_t uuid[16], const Vector<uint8_t> &drmSessionId)
2943{
2944 ALOGV("prepareDrm ");
2945
2946 // Passing to the looper anyway; called in a pre-config prepared state so no race on mCrypto
2947 sp<AMessage> msg = new AMessage(kWhatPrepareDrm, this);
2948 // synchronous call so just passing the address but with local copies of "const" args
2949 uint8_t UUID[16];
2950 memcpy(UUID, uuid, sizeof(UUID));
2951 Vector<uint8_t> sessionId = drmSessionId;
2952 msg->setPointer("uuid", (void*)UUID);
2953 msg->setPointer("drmSessionId", (void*)&sessionId);
2954
2955 sp<AMessage> response;
2956 status_t status = msg->postAndAwaitResponse(&response);
2957
2958 if (status == OK && response != NULL) {
2959 CHECK(response->findInt32("status", &status));
2960 ALOGV("prepareDrm ret: %d ", status);
2961 } else {
2962 ALOGE("prepareDrm err: %d", status);
2963 }
2964
2965 return status;
2966}
2967
2968status_t NuPlayer::releaseDrm()
2969{
2970 ALOGV("releaseDrm ");
2971
2972 sp<AMessage> msg = new AMessage(kWhatReleaseDrm, this);
2973
2974 sp<AMessage> response;
2975 status_t status = msg->postAndAwaitResponse(&response);
2976
2977 if (status == OK && response != NULL) {
2978 CHECK(response->findInt32("status", &status));
2979 ALOGV("releaseDrm ret: %d ", status);
2980 } else {
2981 ALOGE("releaseDrm err: %d", status);
2982 }
2983
2984 return status;
2985}
2986
2987status_t NuPlayer::onPrepareDrm(const sp<AMessage> &msg)
2988{
2989 // TODO change to ALOGV
2990 ALOGD("onPrepareDrm ");
2991
2992 status_t status = INVALID_OPERATION;
2993 if (mSource == NULL) {
2994 ALOGE("onPrepareDrm: No source. onPrepareDrm failed with %d.", status);
2995 return status;
2996 }
2997
2998 uint8_t *uuid;
2999 Vector<uint8_t> *drmSessionId;
3000 CHECK(msg->findPointer("uuid", (void**)&uuid));
3001 CHECK(msg->findPointer("drmSessionId", (void**)&drmSessionId));
3002
3003 status = OK;
3004 sp<ICrypto> crypto = NULL;
3005
3006 status = mSource->prepareDrm(uuid, *drmSessionId, &crypto);
3007 if (crypto == NULL) {
3008 ALOGE("onPrepareDrm: mSource->prepareDrm failed. status: %d", status);
3009 return status;
3010 }
3011 ALOGV("onPrepareDrm: mSource->prepareDrm succeeded");
3012
3013 if (mCrypto != NULL) {
3014 ALOGE("onPrepareDrm: Unexpected. Already having mCrypto: %p (%d)",
3015 mCrypto.get(), mCrypto->getStrongCount());
3016 mCrypto.clear();
3017 }
3018
3019 mCrypto = crypto;
3020 mIsDrmProtected = true;
3021 // TODO change to ALOGV
3022 ALOGD("onPrepareDrm: mCrypto: %p (%d)", mCrypto.get(),
3023 (mCrypto != NULL ? mCrypto->getStrongCount() : 0));
3024
3025 return status;
3026}
3027
3028status_t NuPlayer::onReleaseDrm()
3029{
3030 // TODO change to ALOGV
3031 ALOGD("onReleaseDrm ");
3032
Hassan Shojania50b20c92017-02-16 18:28:58 -08003033 if (!mIsDrmProtected) {
3034 ALOGW("onReleaseDrm: Unexpected. mIsDrmProtected is already false.");
3035 }
3036
3037 mIsDrmProtected = false;
Hassan Shojaniacefac142017-02-06 21:02:02 -08003038
3039 status_t status;
3040 if (mCrypto != NULL) {
Hassan Shojania355e8472017-05-12 10:33:16 -07003041 // notifying the source first before removing crypto from codec
3042 if (mSource != NULL) {
3043 mSource->releaseDrm();
3044 }
3045
Hassan Shojaniacefac142017-02-06 21:02:02 -08003046 status=OK;
3047 // first making sure the codecs have released their crypto reference
3048 const sp<DecoderBase> &videoDecoder = getDecoder(false/*audio*/);
3049 if (videoDecoder != NULL) {
3050 status = videoDecoder->releaseCrypto();
3051 ALOGV("onReleaseDrm: video decoder ret: %d", status);
3052 }
3053
3054 const sp<DecoderBase> &audioDecoder = getDecoder(true/*audio*/);
3055 if (audioDecoder != NULL) {
3056 status_t status_audio = audioDecoder->releaseCrypto();
3057 if (status == OK) { // otherwise, returning the first error
3058 status = status_audio;
3059 }
3060 ALOGV("onReleaseDrm: audio decoder ret: %d", status_audio);
3061 }
3062
3063 // TODO change to ALOGV
3064 ALOGD("onReleaseDrm: mCrypto: %p (%d)", mCrypto.get(),
3065 (mCrypto != NULL ? mCrypto->getStrongCount() : 0));
3066 mCrypto.clear();
3067 } else { // mCrypto == NULL
3068 ALOGE("onReleaseDrm: Unexpected. There is no crypto.");
3069 status = INVALID_OPERATION;
3070 }
3071
3072 return status;
3073}
3074// Modular DRM end
Andreas Huberb5f25f02013-02-05 10:14:26 -08003075////////////////////////////////////////////////////////////////////////////////
3076
Chong Zhangced1c2f2014-08-08 15:22:35 -07003077sp<AMessage> NuPlayer::Source::getFormat(bool audio) {
3078 sp<MetaData> meta = getFormatMeta(audio);
3079
3080 if (meta == NULL) {
3081 return NULL;
3082 }
3083
3084 sp<AMessage> msg = new AMessage;
3085
3086 if(convertMetaDataToMessage(meta, &msg) == OK) {
3087 return msg;
3088 }
3089 return NULL;
3090}
3091
Andreas Huber9575c962013-02-05 13:59:56 -08003092void NuPlayer::Source::notifyFlagsChanged(uint32_t flags) {
3093 sp<AMessage> notify = dupNotify();
3094 notify->setInt32("what", kWhatFlagsChanged);
3095 notify->setInt32("flags", flags);
3096 notify->post();
3097}
3098
Chong Zhangced1c2f2014-08-08 15:22:35 -07003099void NuPlayer::Source::notifyVideoSizeChanged(const sp<AMessage> &format) {
Andreas Huber9575c962013-02-05 13:59:56 -08003100 sp<AMessage> notify = dupNotify();
3101 notify->setInt32("what", kWhatVideoSizeChanged);
Chong Zhangced1c2f2014-08-08 15:22:35 -07003102 notify->setMessage("format", format);
Andreas Huber9575c962013-02-05 13:59:56 -08003103 notify->post();
3104}
3105
Andreas Huberec0c5972013-02-05 14:47:13 -08003106void NuPlayer::Source::notifyPrepared(status_t err) {
Hassan Shojaniacefac142017-02-06 21:02:02 -08003107 ALOGV("Source::notifyPrepared %d", err);
Andreas Huber9575c962013-02-05 13:59:56 -08003108 sp<AMessage> notify = dupNotify();
3109 notify->setInt32("what", kWhatPrepared);
Andreas Huberec0c5972013-02-05 14:47:13 -08003110 notify->setInt32("err", err);
Andreas Huber9575c962013-02-05 13:59:56 -08003111 notify->post();
3112}
3113
Hassan Shojaniacefac142017-02-06 21:02:02 -08003114void NuPlayer::Source::notifyDrmInfo(const sp<ABuffer> &drmInfoBuffer)
3115{
3116 ALOGV("Source::notifyDrmInfo");
3117
3118 sp<AMessage> notify = dupNotify();
3119 notify->setInt32("what", kWhatDrmInfo);
3120 notify->setBuffer("drmInfo", drmInfoBuffer);
3121
3122 notify->post();
3123}
3124
Lajos Molnarfcd3e942015-03-31 10:06:48 -07003125void NuPlayer::Source::notifyInstantiateSecureDecoders(const sp<AMessage> &reply) {
3126 sp<AMessage> notify = dupNotify();
3127 notify->setInt32("what", kWhatInstantiateSecureDecoders);
3128 notify->setMessage("reply", reply);
3129 notify->post();
3130}
3131
Andreas Huber84333e02014-02-07 15:36:10 -08003132void NuPlayer::Source::onMessageReceived(const sp<AMessage> & /* msg */) {
Andreas Huberb5f25f02013-02-05 10:14:26 -08003133 TRESPASS();
3134}
3135
Andreas Huberf9334412010-12-15 15:17:42 -08003136} // namespace android