blob: a5f5fc675be001864b0adf5db5b3efc2478e7540 [file] [log] [blame]
Andreas Huberf9334412010-12-15 15:17:42 -08001/*
2 * Copyright (C) 2010 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17//#define LOG_NDEBUG 0
18#define LOG_TAG "NuPlayer"
Ray Essickd4d00612017-01-03 09:36:27 -080019
20#include <inttypes.h>
21
Andreas Huberf9334412010-12-15 15:17:42 -080022#include <utils/Log.h>
23
24#include "NuPlayer.h"
Andreas Huber5bc087c2010-12-23 10:27:40 -080025
26#include "HTTPLiveSource.h"
Chong Zhang7137ec72014-11-12 16:41:05 -080027#include "NuPlayerCCDecoder.h"
Andreas Huberf9334412010-12-15 15:17:42 -080028#include "NuPlayerDecoder.h"
Chong Zhang7137ec72014-11-12 16:41:05 -080029#include "NuPlayerDecoderBase.h"
Wei Jiabc2fb722014-07-08 16:37:57 -070030#include "NuPlayerDecoderPassThrough.h"
Andreas Huber43c3e6c2011-01-05 12:17:08 -080031#include "NuPlayerDriver.h"
Andreas Huberf9334412010-12-15 15:17:42 -080032#include "NuPlayerRenderer.h"
Andreas Huber5bc087c2010-12-23 10:27:40 -080033#include "NuPlayerSource.h"
Andreas Huber2bfdd422011-10-11 15:24:07 -070034#include "RTSPSource.h"
Andreas Huber5bc087c2010-12-23 10:27:40 -080035#include "StreamingSource.h"
Andreas Huberafed0e12011-09-20 15:39:58 -070036#include "GenericSource.h"
Robert Shihd3b0bbb2014-07-23 15:00:25 -070037#include "TextDescriptions.h"
Andreas Huber5bc087c2010-12-23 10:27:40 -080038
39#include "ATSParser.h"
Andreas Huberf9334412010-12-15 15:17:42 -080040
Lajos Molnard9fd6312014-11-06 11:00:00 -080041#include <cutils/properties.h>
42
Lajos Molnar3a474aa2015-04-24 17:10:07 -070043#include <media/AudioResamplerPublic.h>
44#include <media/AVSyncSettings.h>
Wonsik Kim7e34bf52016-08-23 00:09:18 +090045#include <media/MediaCodecBuffer.h>
Lajos Molnar3a474aa2015-04-24 17:10:07 -070046
Andreas Huber3831a062010-12-21 10:22:33 -080047#include <media/stagefright/foundation/hexdump.h>
Andreas Huberf9334412010-12-15 15:17:42 -080048#include <media/stagefright/foundation/ABuffer.h>
49#include <media/stagefright/foundation/ADebug.h>
50#include <media/stagefright/foundation/AMessage.h>
Dongwon Kangd91dc5a2017-10-10 00:07:09 -070051#include <media/stagefright/foundation/avc_utils.h>
Lajos Molnar09524832014-07-17 14:29:51 -070052#include <media/stagefright/MediaBuffer.h>
Wei Jia0a68f662017-08-30 18:01:26 -070053#include <media/stagefright/MediaClock.h>
Andreas Huber3fe62152011-09-16 15:09:22 -070054#include <media/stagefright/MediaDefs.h>
Andreas Huberf9334412010-12-15 15:17:42 -080055#include <media/stagefright/MediaErrors.h>
56#include <media/stagefright/MetaData.h>
Lajos Molnar1de1e252015-04-30 18:18:34 -070057
Andy McFadden8ba01022012-12-18 09:46:54 -080058#include <gui/IGraphicBufferProducer.h>
Lajos Molnar1de1e252015-04-30 18:18:34 -070059#include <gui/Surface.h>
Andreas Huberf9334412010-12-15 15:17:42 -080060
Andreas Huber3fe62152011-09-16 15:09:22 -070061
Andreas Huber84066782011-08-16 09:34:26 -070062#include "ESDS.h"
63#include <media/stagefright/Utils.h>
64
Andreas Huberf9334412010-12-15 15:17:42 -080065namespace android {
66
Andreas Hubera1f8ab02012-11-30 10:53:22 -080067struct NuPlayer::Action : public RefBase {
68 Action() {}
69
70 virtual void execute(NuPlayer *player) = 0;
71
72private:
73 DISALLOW_EVIL_CONSTRUCTORS(Action);
74};
75
76struct NuPlayer::SeekAction : public Action {
Wei Jiac5de0912016-11-18 10:22:14 -080077 explicit SeekAction(int64_t seekTimeUs, MediaPlayerSeekMode mode)
Wei Jia14486822016-11-02 17:51:30 -070078 : mSeekTimeUs(seekTimeUs),
Wei Jiac5de0912016-11-18 10:22:14 -080079 mMode(mode) {
Andreas Hubera1f8ab02012-11-30 10:53:22 -080080 }
81
82 virtual void execute(NuPlayer *player) {
Wei Jiac5de0912016-11-18 10:22:14 -080083 player->performSeek(mSeekTimeUs, mMode);
Andreas Hubera1f8ab02012-11-30 10:53:22 -080084 }
85
86private:
87 int64_t mSeekTimeUs;
Wei Jiac5de0912016-11-18 10:22:14 -080088 MediaPlayerSeekMode mMode;
Andreas Hubera1f8ab02012-11-30 10:53:22 -080089
90 DISALLOW_EVIL_CONSTRUCTORS(SeekAction);
91};
92
Chong Zhangf8d71772014-11-26 15:08:34 -080093struct NuPlayer::ResumeDecoderAction : public Action {
Chih-Hung Hsieh090ef602016-04-27 10:39:54 -070094 explicit ResumeDecoderAction(bool needNotify)
Chong Zhangf8d71772014-11-26 15:08:34 -080095 : mNeedNotify(needNotify) {
96 }
97
98 virtual void execute(NuPlayer *player) {
99 player->performResumeDecoders(mNeedNotify);
100 }
101
102private:
103 bool mNeedNotify;
104
105 DISALLOW_EVIL_CONSTRUCTORS(ResumeDecoderAction);
106};
107
Andreas Huber57a339c2012-12-03 11:18:00 -0800108struct NuPlayer::SetSurfaceAction : public Action {
Chih-Hung Hsieh090ef602016-04-27 10:39:54 -0700109 explicit SetSurfaceAction(const sp<Surface> &surface)
Lajos Molnar1de1e252015-04-30 18:18:34 -0700110 : mSurface(surface) {
Andreas Huber57a339c2012-12-03 11:18:00 -0800111 }
112
113 virtual void execute(NuPlayer *player) {
Lajos Molnar1de1e252015-04-30 18:18:34 -0700114 player->performSetSurface(mSurface);
Andreas Huber57a339c2012-12-03 11:18:00 -0800115 }
116
117private:
Lajos Molnar1de1e252015-04-30 18:18:34 -0700118 sp<Surface> mSurface;
Andreas Huber57a339c2012-12-03 11:18:00 -0800119
120 DISALLOW_EVIL_CONSTRUCTORS(SetSurfaceAction);
121};
122
Wei Jiafef808d2014-10-31 17:57:05 -0700123struct NuPlayer::FlushDecoderAction : public Action {
124 FlushDecoderAction(FlushCommand audio, FlushCommand video)
Andreas Huber14f76722013-01-15 09:04:18 -0800125 : mAudio(audio),
126 mVideo(video) {
127 }
128
129 virtual void execute(NuPlayer *player) {
Wei Jiafef808d2014-10-31 17:57:05 -0700130 player->performDecoderFlush(mAudio, mVideo);
Andreas Huber14f76722013-01-15 09:04:18 -0800131 }
132
133private:
Wei Jiafef808d2014-10-31 17:57:05 -0700134 FlushCommand mAudio;
135 FlushCommand mVideo;
Andreas Huber14f76722013-01-15 09:04:18 -0800136
Wei Jiafef808d2014-10-31 17:57:05 -0700137 DISALLOW_EVIL_CONSTRUCTORS(FlushDecoderAction);
Andreas Huber14f76722013-01-15 09:04:18 -0800138};
139
140struct NuPlayer::PostMessageAction : public Action {
Chih-Hung Hsieh090ef602016-04-27 10:39:54 -0700141 explicit PostMessageAction(const sp<AMessage> &msg)
Andreas Huber14f76722013-01-15 09:04:18 -0800142 : mMessage(msg) {
143 }
144
145 virtual void execute(NuPlayer *) {
146 mMessage->post();
147 }
148
149private:
150 sp<AMessage> mMessage;
151
152 DISALLOW_EVIL_CONSTRUCTORS(PostMessageAction);
153};
154
Andreas Hubera1f8ab02012-11-30 10:53:22 -0800155// Use this if there's no state necessary to save in order to execute
156// the action.
157struct NuPlayer::SimpleAction : public Action {
158 typedef void (NuPlayer::*ActionFunc)();
159
Chih-Hung Hsieh090ef602016-04-27 10:39:54 -0700160 explicit SimpleAction(ActionFunc func)
Andreas Hubera1f8ab02012-11-30 10:53:22 -0800161 : mFunc(func) {
162 }
163
164 virtual void execute(NuPlayer *player) {
165 (player->*mFunc)();
166 }
167
168private:
169 ActionFunc mFunc;
170
171 DISALLOW_EVIL_CONSTRUCTORS(SimpleAction);
172};
173
Andreas Huberf9334412010-12-15 15:17:42 -0800174////////////////////////////////////////////////////////////////////////////////
175
Wei Jia0a68f662017-08-30 18:01:26 -0700176NuPlayer::NuPlayer(pid_t pid, const sp<MediaClock> &mediaClock)
Andreas Huber9b80c2b2011-06-30 15:47:02 -0700177 : mUIDValid(false),
Ronghua Wu68845c12015-07-21 09:50:48 -0700178 mPID(pid),
Wei Jia0a68f662017-08-30 18:01:26 -0700179 mMediaClock(mediaClock),
Andreas Huber9575c962013-02-05 13:59:56 -0800180 mSourceFlags(0),
Wei Jiabc2fb722014-07-08 16:37:57 -0700181 mOffloadAudio(false),
Wei Jia88703c32014-08-06 11:24:07 -0700182 mAudioDecoderGeneration(0),
183 mVideoDecoderGeneration(0),
Wei Jia57568df2014-09-22 10:16:29 -0700184 mRendererGeneration(0),
Ray Essick0d98c182017-11-09 13:42:42 -0800185 mLastStartedPlayingTimeNs(0),
Ray Essickeda32522018-02-28 12:08:28 -0800186 mLastStartedRebufferingTimeNs(0),
Robert Shih1a5c8592015-08-04 18:07:44 -0700187 mPreviousSeekTimeUs(0),
Andreas Huber9b80c2b2011-06-30 15:47:02 -0700188 mAudioEOS(false),
Andreas Huberf9334412010-12-15 15:17:42 -0800189 mVideoEOS(false),
Andreas Huber5bc087c2010-12-23 10:27:40 -0800190 mScanSourcesPending(false),
Andreas Huber1aef2112011-01-04 14:01:29 -0800191 mScanSourcesGeneration(0),
Andreas Huberb7c8e912012-11-27 15:02:53 -0800192 mPollDurationGeneration(0),
Robert Shihd3b0bbb2014-07-23 15:00:25 -0700193 mTimedTextGeneration(0),
Andreas Huberf9334412010-12-15 15:17:42 -0800194 mFlushingAudio(NONE),
Andreas Huber1aef2112011-01-04 14:01:29 -0800195 mFlushingVideo(NONE),
Chong Zhangf8d71772014-11-26 15:08:34 -0800196 mResumePending(false),
Andreas Huber57a339c2012-12-03 11:18:00 -0800197 mVideoScalingMode(NATIVE_WINDOW_SCALING_MODE_SCALE_TO_WINDOW),
Lajos Molnar3a474aa2015-04-24 17:10:07 -0700198 mPlaybackSettings(AUDIO_PLAYBACK_RATE_DEFAULT),
199 mVideoFpsHint(-1.f),
Chong Zhangefbb6192015-01-30 17:13:27 -0800200 mStarted(false),
Wei Jia8a092d32016-06-03 14:57:24 -0700201 mPrepared(false),
Ronghua Wu64c2d172015-10-07 16:52:19 -0700202 mResetting(false),
Robert Shih0c61a0d2015-07-06 15:09:10 -0700203 mSourceStarted(false),
Wei Jia686e8e52017-04-03 14:08:01 -0700204 mAudioDecoderError(false),
205 mVideoDecoderError(false),
Chong Zhangefbb6192015-01-30 17:13:27 -0800206 mPaused(false),
Wei Jia71c75e02016-02-04 09:40:47 -0800207 mPausedByClient(true),
Hassan Shojania50b20c92017-02-16 18:28:58 -0800208 mPausedForBuffering(false),
Hassan Shojaniaff63de72017-04-26 15:10:42 -0700209 mIsDrmProtected(false),
210 mDataSourceType(DATA_SOURCE_TYPE_NONE) {
Wei Jia0a68f662017-08-30 18:01:26 -0700211 CHECK(mediaClock != NULL);
Andy Hung8d121d42014-10-03 09:53:53 -0700212 clearFlushComplete();
Andreas Huberf9334412010-12-15 15:17:42 -0800213}
214
215NuPlayer::~NuPlayer() {
216}
217
Andreas Huber9b80c2b2011-06-30 15:47:02 -0700218void NuPlayer::setUID(uid_t uid) {
219 mUIDValid = true;
220 mUID = uid;
221}
222
Dongwon Kang47afe0a2018-03-27 15:15:30 -0700223void NuPlayer::init(const wp<NuPlayerDriver> &driver) {
Andreas Huber43c3e6c2011-01-05 12:17:08 -0800224 mDriver = driver;
Dongwon Kang47afe0a2018-03-27 15:15:30 -0700225
226 sp<AMessage> notify = new AMessage(kWhatMediaClockNotify, this);
227 mMediaClock->setNotificationMessage(notify);
Andreas Huberf9334412010-12-15 15:17:42 -0800228}
229
Andreas Huber9575c962013-02-05 13:59:56 -0800230void NuPlayer::setDataSourceAsync(const sp<IStreamSource> &source) {
Lajos Molnar1d15ab52015-03-04 16:46:34 -0800231 sp<AMessage> msg = new AMessage(kWhatSetDataSource, this);
Andreas Huberf9334412010-12-15 15:17:42 -0800232
Lajos Molnar1d15ab52015-03-04 16:46:34 -0800233 sp<AMessage> notify = new AMessage(kWhatSourceNotify, this);
Andreas Huberb5f25f02013-02-05 10:14:26 -0800234
Andreas Huber240abcc2014-02-13 13:32:37 -0800235 msg->setObject("source", new StreamingSource(notify, source));
Andreas Huber5bc087c2010-12-23 10:27:40 -0800236 msg->post();
Hassan Shojaniaff63de72017-04-26 15:10:42 -0700237 mDataSourceType = DATA_SOURCE_TYPE_STREAM;
Andreas Huber5bc087c2010-12-23 10:27:40 -0800238}
Andreas Huberf9334412010-12-15 15:17:42 -0800239
Andreas Huberafed0e12011-09-20 15:39:58 -0700240static bool IsHTTPLiveURL(const char *url) {
241 if (!strncasecmp("http://", url, 7)
Andreas Huber99759402013-04-01 14:28:31 -0700242 || !strncasecmp("https://", url, 8)
243 || !strncasecmp("file://", url, 7)) {
Andreas Huberafed0e12011-09-20 15:39:58 -0700244 size_t len = strlen(url);
245 if (len >= 5 && !strcasecmp(".m3u8", &url[len - 5])) {
246 return true;
247 }
248
249 if (strstr(url,"m3u8")) {
250 return true;
251 }
252 }
253
254 return false;
255}
256
Andreas Huber9575c962013-02-05 13:59:56 -0800257void NuPlayer::setDataSourceAsync(
Andreas Huber1b86fe02014-01-29 11:13:26 -0800258 const sp<IMediaHTTPService> &httpService,
259 const char *url,
260 const KeyedVector<String8, String8> *headers) {
Chong Zhang3de157d2014-08-05 20:54:44 -0700261
Lajos Molnar1d15ab52015-03-04 16:46:34 -0800262 sp<AMessage> msg = new AMessage(kWhatSetDataSource, this);
Oscar Rydhé7a33b772012-02-20 10:15:48 +0100263 size_t len = strlen(url);
Andreas Huber5bc087c2010-12-23 10:27:40 -0800264
Lajos Molnar1d15ab52015-03-04 16:46:34 -0800265 sp<AMessage> notify = new AMessage(kWhatSourceNotify, this);
Andreas Huberb5f25f02013-02-05 10:14:26 -0800266
Andreas Huberafed0e12011-09-20 15:39:58 -0700267 sp<Source> source;
268 if (IsHTTPLiveURL(url)) {
Andreas Huber81e68442014-02-05 11:52:33 -0800269 source = new HTTPLiveSource(notify, httpService, url, headers);
Hassan Shojaniacefac142017-02-06 21:02:02 -0800270 ALOGV("setDataSourceAsync HTTPLiveSource %s", url);
Hassan Shojaniaff63de72017-04-26 15:10:42 -0700271 mDataSourceType = DATA_SOURCE_TYPE_HTTP_LIVE;
Andreas Huberafed0e12011-09-20 15:39:58 -0700272 } else if (!strncasecmp(url, "rtsp://", 7)) {
Andreas Huber1b86fe02014-01-29 11:13:26 -0800273 source = new RTSPSource(
274 notify, httpService, url, headers, mUIDValid, mUID);
Hassan Shojaniacefac142017-02-06 21:02:02 -0800275 ALOGV("setDataSourceAsync RTSPSource %s", url);
Hassan Shojaniaff63de72017-04-26 15:10:42 -0700276 mDataSourceType = DATA_SOURCE_TYPE_RTSP;
Oscar Rydhé7a33b772012-02-20 10:15:48 +0100277 } else if ((!strncasecmp(url, "http://", 7)
278 || !strncasecmp(url, "https://", 8))
279 && ((len >= 4 && !strcasecmp(".sdp", &url[len - 4]))
280 || strstr(url, ".sdp?"))) {
Andreas Huber1b86fe02014-01-29 11:13:26 -0800281 source = new RTSPSource(
282 notify, httpService, url, headers, mUIDValid, mUID, true);
Hassan Shojaniacefac142017-02-06 21:02:02 -0800283 ALOGV("setDataSourceAsync RTSPSource http/https/.sdp %s", url);
Hassan Shojaniaff63de72017-04-26 15:10:42 -0700284 mDataSourceType = DATA_SOURCE_TYPE_RTSP;
Andreas Huber2bfdd422011-10-11 15:24:07 -0700285 } else {
Hassan Shojaniacefac142017-02-06 21:02:02 -0800286 ALOGV("setDataSourceAsync GenericSource %s", url);
287
Chong Zhang3de157d2014-08-05 20:54:44 -0700288 sp<GenericSource> genericSource =
Wei Jia992c5592017-09-01 14:20:23 -0700289 new GenericSource(notify, mUIDValid, mUID, mMediaClock);
Andreas Huber2bfdd422011-10-11 15:24:07 -0700290
Chong Zhanga19f33e2014-08-07 15:35:07 -0700291 status_t err = genericSource->setDataSource(httpService, url, headers);
Chong Zhang3de157d2014-08-05 20:54:44 -0700292
293 if (err == OK) {
294 source = genericSource;
295 } else {
Chong Zhanga19f33e2014-08-07 15:35:07 -0700296 ALOGE("Failed to set data source!");
Chong Zhang3de157d2014-08-05 20:54:44 -0700297 }
Hassan Shojaniaff63de72017-04-26 15:10:42 -0700298
299 // regardless of success/failure
300 mDataSourceType = DATA_SOURCE_TYPE_GENERIC_URL;
Chong Zhang3de157d2014-08-05 20:54:44 -0700301 }
Andreas Huberafed0e12011-09-20 15:39:58 -0700302 msg->setObject("source", source);
303 msg->post();
304}
305
Andreas Huber9575c962013-02-05 13:59:56 -0800306void NuPlayer::setDataSourceAsync(int fd, int64_t offset, int64_t length) {
Lajos Molnar1d15ab52015-03-04 16:46:34 -0800307 sp<AMessage> msg = new AMessage(kWhatSetDataSource, this);
Andreas Huberafed0e12011-09-20 15:39:58 -0700308
Lajos Molnar1d15ab52015-03-04 16:46:34 -0800309 sp<AMessage> notify = new AMessage(kWhatSourceNotify, this);
Andreas Huberb5f25f02013-02-05 10:14:26 -0800310
Chong Zhang3de157d2014-08-05 20:54:44 -0700311 sp<GenericSource> source =
Wei Jia992c5592017-09-01 14:20:23 -0700312 new GenericSource(notify, mUIDValid, mUID, mMediaClock);
Chong Zhang3de157d2014-08-05 20:54:44 -0700313
Hassan Shojaniacefac142017-02-06 21:02:02 -0800314 ALOGV("setDataSourceAsync fd %d/%lld/%lld source: %p",
315 fd, (long long)offset, (long long)length, source.get());
316
Chong Zhanga19f33e2014-08-07 15:35:07 -0700317 status_t err = source->setDataSource(fd, offset, length);
Chong Zhang3de157d2014-08-05 20:54:44 -0700318
319 if (err != OK) {
Chong Zhanga19f33e2014-08-07 15:35:07 -0700320 ALOGE("Failed to set data source!");
Chong Zhang3de157d2014-08-05 20:54:44 -0700321 source = NULL;
322 }
323
Andreas Huberafed0e12011-09-20 15:39:58 -0700324 msg->setObject("source", source);
Andreas Huberf9334412010-12-15 15:17:42 -0800325 msg->post();
Hassan Shojaniaff63de72017-04-26 15:10:42 -0700326 mDataSourceType = DATA_SOURCE_TYPE_GENERIC_FD;
Andreas Huberf9334412010-12-15 15:17:42 -0800327}
328
Chris Watkins99f31602015-03-20 13:06:33 -0700329void NuPlayer::setDataSourceAsync(const sp<DataSource> &dataSource) {
330 sp<AMessage> msg = new AMessage(kWhatSetDataSource, this);
331 sp<AMessage> notify = new AMessage(kWhatSourceNotify, this);
332
Wei Jia992c5592017-09-01 14:20:23 -0700333 sp<GenericSource> source = new GenericSource(notify, mUIDValid, mUID, mMediaClock);
Chris Watkins99f31602015-03-20 13:06:33 -0700334 status_t err = source->setDataSource(dataSource);
335
336 if (err != OK) {
337 ALOGE("Failed to set data source!");
338 source = NULL;
339 }
340
341 msg->setObject("source", source);
342 msg->post();
Hassan Shojaniaff63de72017-04-26 15:10:42 -0700343 mDataSourceType = DATA_SOURCE_TYPE_MEDIA;
Chris Watkins99f31602015-03-20 13:06:33 -0700344}
345
Wei Jia9bb38032017-03-23 18:00:38 -0700346status_t NuPlayer::getBufferingSettings(
Wei Jia48fa06d2016-12-20 15:30:49 -0800347 BufferingSettings *buffering /* nonnull */) {
Wei Jia9bb38032017-03-23 18:00:38 -0700348 sp<AMessage> msg = new AMessage(kWhatGetBufferingSettings, this);
Wei Jia48fa06d2016-12-20 15:30:49 -0800349 sp<AMessage> response;
350 status_t err = msg->postAndAwaitResponse(&response);
351 if (err == OK && response != NULL) {
352 CHECK(response->findInt32("err", &err));
353 if (err == OK) {
354 readFromAMessage(response, buffering);
355 }
356 }
357 return err;
358}
359
360status_t NuPlayer::setBufferingSettings(const BufferingSettings& buffering) {
361 sp<AMessage> msg = new AMessage(kWhatSetBufferingSettings, this);
362 writeToAMessage(msg, buffering);
363 sp<AMessage> response;
364 status_t err = msg->postAndAwaitResponse(&response);
365 if (err == OK && response != NULL) {
366 CHECK(response->findInt32("err", &err));
367 }
368 return err;
369}
370
Andreas Huber9575c962013-02-05 13:59:56 -0800371void NuPlayer::prepareAsync() {
Hassan Shojaniacefac142017-02-06 21:02:02 -0800372 ALOGV("prepareAsync");
373
Lajos Molnar1d15ab52015-03-04 16:46:34 -0800374 (new AMessage(kWhatPrepare, this))->post();
Andreas Huber9575c962013-02-05 13:59:56 -0800375}
376
Andreas Huber57a339c2012-12-03 11:18:00 -0800377void NuPlayer::setVideoSurfaceTextureAsync(
Andy McFadden8ba01022012-12-18 09:46:54 -0800378 const sp<IGraphicBufferProducer> &bufferProducer) {
Lajos Molnar1de1e252015-04-30 18:18:34 -0700379 sp<AMessage> msg = new AMessage(kWhatSetVideoSurface, this);
Andreas Huber57a339c2012-12-03 11:18:00 -0800380
Andy McFadden8ba01022012-12-18 09:46:54 -0800381 if (bufferProducer == NULL) {
Lajos Molnar1de1e252015-04-30 18:18:34 -0700382 msg->setObject("surface", NULL);
Andreas Huber57a339c2012-12-03 11:18:00 -0800383 } else {
Lajos Molnar1de1e252015-04-30 18:18:34 -0700384 msg->setObject("surface", new Surface(bufferProducer, true /* controlledByApp */));
Andreas Huber57a339c2012-12-03 11:18:00 -0800385 }
386
Andreas Huberf9334412010-12-15 15:17:42 -0800387 msg->post();
388}
389
390void NuPlayer::setAudioSink(const sp<MediaPlayerBase::AudioSink> &sink) {
Lajos Molnar1d15ab52015-03-04 16:46:34 -0800391 sp<AMessage> msg = new AMessage(kWhatSetAudioSink, this);
Andreas Huberf9334412010-12-15 15:17:42 -0800392 msg->setObject("sink", sink);
393 msg->post();
394}
395
396void NuPlayer::start() {
Lajos Molnar1d15ab52015-03-04 16:46:34 -0800397 (new AMessage(kWhatStart, this))->post();
Andreas Huberf9334412010-12-15 15:17:42 -0800398}
399
Lajos Molnar3a474aa2015-04-24 17:10:07 -0700400status_t NuPlayer::setPlaybackSettings(const AudioPlaybackRate &rate) {
401 // do some cursory validation of the settings here. audio modes are
402 // only validated when set on the audiosink.
403 if ((rate.mSpeed != 0.f && rate.mSpeed < AUDIO_TIMESTRETCH_SPEED_MIN)
404 || rate.mSpeed > AUDIO_TIMESTRETCH_SPEED_MAX
405 || rate.mPitch < AUDIO_TIMESTRETCH_SPEED_MIN
406 || rate.mPitch > AUDIO_TIMESTRETCH_SPEED_MAX) {
407 return BAD_VALUE;
408 }
409 sp<AMessage> msg = new AMessage(kWhatConfigPlayback, this);
410 writeToAMessage(msg, rate);
411 sp<AMessage> response;
412 status_t err = msg->postAndAwaitResponse(&response);
413 if (err == OK && response != NULL) {
414 CHECK(response->findInt32("err", &err));
415 }
416 return err;
417}
418
419status_t NuPlayer::getPlaybackSettings(AudioPlaybackRate *rate /* nonnull */) {
420 sp<AMessage> msg = new AMessage(kWhatGetPlaybackSettings, this);
421 sp<AMessage> response;
422 status_t err = msg->postAndAwaitResponse(&response);
423 if (err == OK && response != NULL) {
424 CHECK(response->findInt32("err", &err));
425 if (err == OK) {
426 readFromAMessage(response, rate);
427 }
428 }
429 return err;
430}
431
432status_t NuPlayer::setSyncSettings(const AVSyncSettings &sync, float videoFpsHint) {
433 sp<AMessage> msg = new AMessage(kWhatConfigSync, this);
434 writeToAMessage(msg, sync, videoFpsHint);
435 sp<AMessage> response;
436 status_t err = msg->postAndAwaitResponse(&response);
437 if (err == OK && response != NULL) {
438 CHECK(response->findInt32("err", &err));
439 }
440 return err;
441}
442
443status_t NuPlayer::getSyncSettings(
444 AVSyncSettings *sync /* nonnull */, float *videoFps /* nonnull */) {
445 sp<AMessage> msg = new AMessage(kWhatGetSyncSettings, this);
446 sp<AMessage> response;
447 status_t err = msg->postAndAwaitResponse(&response);
448 if (err == OK && response != NULL) {
449 CHECK(response->findInt32("err", &err));
450 if (err == OK) {
451 readFromAMessage(response, sync, videoFps);
452 }
453 }
454 return err;
Wei Jia98160162015-02-04 17:01:11 -0800455}
456
Andreas Huber43c3e6c2011-01-05 12:17:08 -0800457void NuPlayer::pause() {
Lajos Molnar1d15ab52015-03-04 16:46:34 -0800458 (new AMessage(kWhatPause, this))->post();
Andreas Huber43c3e6c2011-01-05 12:17:08 -0800459}
460
Andreas Huber1aef2112011-01-04 14:01:29 -0800461void NuPlayer::resetAsync() {
Wei Jiac45a4b22016-04-15 15:30:23 -0700462 sp<Source> source;
463 {
464 Mutex::Autolock autoLock(mSourceLock);
465 source = mSource;
466 }
467
468 if (source != NULL) {
Chong Zhang48296b72014-09-14 14:28:45 -0700469 // During a reset, the data source might be unresponsive already, we need to
470 // disconnect explicitly so that reads exit promptly.
471 // We can't queue the disconnect request to the looper, as it might be
472 // queued behind a stuck read and never gets processed.
473 // Doing a disconnect outside the looper to allows the pending reads to exit
474 // (either successfully or with error).
Wei Jiac45a4b22016-04-15 15:30:23 -0700475 source->disconnect();
Chong Zhang48296b72014-09-14 14:28:45 -0700476 }
477
Lajos Molnar1d15ab52015-03-04 16:46:34 -0800478 (new AMessage(kWhatReset, this))->post();
Andreas Huber1aef2112011-01-04 14:01:29 -0800479}
480
Wei Jia52c28512017-09-13 18:17:51 -0700481status_t NuPlayer::notifyAt(int64_t mediaTimeUs) {
482 sp<AMessage> notify = new AMessage(kWhatNotifyTime, this);
483 notify->setInt64("timerUs", mediaTimeUs);
484 mMediaClock->addTimer(notify, mediaTimeUs);
485 return OK;
486}
487
Wei Jiac5de0912016-11-18 10:22:14 -0800488void NuPlayer::seekToAsync(int64_t seekTimeUs, MediaPlayerSeekMode mode, bool needNotify) {
Lajos Molnar1d15ab52015-03-04 16:46:34 -0800489 sp<AMessage> msg = new AMessage(kWhatSeek, this);
Andreas Huber43c3e6c2011-01-05 12:17:08 -0800490 msg->setInt64("seekTimeUs", seekTimeUs);
Wei Jiac5de0912016-11-18 10:22:14 -0800491 msg->setInt32("mode", mode);
Wei Jiae427abf2014-09-22 15:21:11 -0700492 msg->setInt32("needNotify", needNotify);
Andreas Huber43c3e6c2011-01-05 12:17:08 -0800493 msg->post();
494}
495
Andreas Huber53df1a42010-12-22 10:03:04 -0800496
Chong Zhang404fced2014-06-11 14:45:31 -0700497void NuPlayer::writeTrackInfo(
Chih-Hung Hsiehe964d4e2016-08-09 14:31:32 -0700498 Parcel* reply, const sp<AMessage>& format) const {
Marco Nelissenc367ca12015-09-15 09:51:59 -0700499 if (format == NULL) {
500 ALOGE("NULL format");
501 return;
502 }
Chong Zhang404fced2014-06-11 14:45:31 -0700503 int32_t trackType;
Marco Nelissen9436e482015-09-15 10:21:53 -0700504 if (!format->findInt32("type", &trackType)) {
Marco Nelissenc367ca12015-09-15 09:51:59 -0700505 ALOGE("no track type");
506 return;
507 }
Chong Zhang404fced2014-06-11 14:45:31 -0700508
Robert Shih755106e2015-04-30 14:36:45 -0700509 AString mime;
Robert Shih2e3a4252015-05-06 10:21:15 -0700510 if (!format->findString("mime", &mime)) {
511 // Java MediaPlayer only uses mimetype for subtitle and timedtext tracks.
512 // If we can't find the mimetype here it means that we wouldn't be needing
513 // the mimetype on the Java end. We still write a placeholder mime to keep the
514 // (de)serialization logic simple.
515 if (trackType == MEDIA_TRACK_TYPE_AUDIO) {
516 mime = "audio/";
517 } else if (trackType == MEDIA_TRACK_TYPE_VIDEO) {
518 mime = "video/";
519 } else {
Marco Nelissenc367ca12015-09-15 09:51:59 -0700520 ALOGE("unknown track type: %d", trackType);
521 return;
Robert Shih2e3a4252015-05-06 10:21:15 -0700522 }
523 }
Robert Shih755106e2015-04-30 14:36:45 -0700524
Chong Zhang404fced2014-06-11 14:45:31 -0700525 AString lang;
Marco Nelissen9436e482015-09-15 10:21:53 -0700526 if (!format->findString("language", &lang)) {
Marco Nelissenc367ca12015-09-15 09:51:59 -0700527 ALOGE("no language");
528 return;
529 }
Chong Zhang404fced2014-06-11 14:45:31 -0700530
531 reply->writeInt32(2); // write something non-zero
532 reply->writeInt32(trackType);
Robert Shih755106e2015-04-30 14:36:45 -0700533 reply->writeString16(String16(mime.c_str()));
Chong Zhang404fced2014-06-11 14:45:31 -0700534 reply->writeString16(String16(lang.c_str()));
535
536 if (trackType == MEDIA_TRACK_TYPE_SUBTITLE) {
Chong Zhang404fced2014-06-11 14:45:31 -0700537 int32_t isAuto, isDefault, isForced;
538 CHECK(format->findInt32("auto", &isAuto));
539 CHECK(format->findInt32("default", &isDefault));
540 CHECK(format->findInt32("forced", &isForced));
541
Chong Zhang404fced2014-06-11 14:45:31 -0700542 reply->writeInt32(isAuto);
543 reply->writeInt32(isDefault);
544 reply->writeInt32(isForced);
545 }
546}
547
Andreas Huberf9334412010-12-15 15:17:42 -0800548void NuPlayer::onMessageReceived(const sp<AMessage> &msg) {
549 switch (msg->what()) {
550 case kWhatSetDataSource:
551 {
Steve Block3856b092011-10-20 11:56:00 +0100552 ALOGV("kWhatSetDataSource");
Andreas Huberf9334412010-12-15 15:17:42 -0800553
554 CHECK(mSource == NULL);
555
Chong Zhang3de157d2014-08-05 20:54:44 -0700556 status_t err = OK;
Andreas Huber5bc087c2010-12-23 10:27:40 -0800557 sp<RefBase> obj;
558 CHECK(msg->findObject("source", &obj));
Chong Zhang3de157d2014-08-05 20:54:44 -0700559 if (obj != NULL) {
Wei Jiac45a4b22016-04-15 15:30:23 -0700560 Mutex::Autolock autoLock(mSourceLock);
Chong Zhang3de157d2014-08-05 20:54:44 -0700561 mSource = static_cast<Source *>(obj.get());
Chong Zhang3de157d2014-08-05 20:54:44 -0700562 } else {
563 err = UNKNOWN_ERROR;
564 }
Andreas Huber9575c962013-02-05 13:59:56 -0800565
566 CHECK(mDriver != NULL);
567 sp<NuPlayerDriver> driver = mDriver.promote();
568 if (driver != NULL) {
Chong Zhang3de157d2014-08-05 20:54:44 -0700569 driver->notifySetDataSourceCompleted(err);
Andreas Huber9575c962013-02-05 13:59:56 -0800570 }
571 break;
572 }
573
Wei Jia9bb38032017-03-23 18:00:38 -0700574 case kWhatGetBufferingSettings:
Wei Jia48fa06d2016-12-20 15:30:49 -0800575 {
576 sp<AReplyToken> replyID;
577 CHECK(msg->senderAwaitsResponse(&replyID));
578
Wei Jia9bb38032017-03-23 18:00:38 -0700579 ALOGV("kWhatGetBufferingSettings");
Wei Jia48fa06d2016-12-20 15:30:49 -0800580 BufferingSettings buffering;
581 status_t err = OK;
582 if (mSource != NULL) {
Wei Jia9bb38032017-03-23 18:00:38 -0700583 err = mSource->getBufferingSettings(&buffering);
Wei Jia48fa06d2016-12-20 15:30:49 -0800584 } else {
585 err = INVALID_OPERATION;
586 }
587 sp<AMessage> response = new AMessage;
588 if (err == OK) {
589 writeToAMessage(response, buffering);
590 }
591 response->setInt32("err", err);
592 response->postReply(replyID);
593 break;
594 }
595
596 case kWhatSetBufferingSettings:
597 {
598 sp<AReplyToken> replyID;
599 CHECK(msg->senderAwaitsResponse(&replyID));
600
601 ALOGV("kWhatSetBufferingSettings");
602 BufferingSettings buffering;
603 readFromAMessage(msg, &buffering);
604 status_t err = OK;
605 if (mSource != NULL) {
606 err = mSource->setBufferingSettings(buffering);
607 } else {
608 err = INVALID_OPERATION;
609 }
610 sp<AMessage> response = new AMessage;
611 response->setInt32("err", err);
612 response->postReply(replyID);
613 break;
614 }
615
Andreas Huber9575c962013-02-05 13:59:56 -0800616 case kWhatPrepare:
617 {
Hassan Shojaniacefac142017-02-06 21:02:02 -0800618 ALOGV("onMessageReceived kWhatPrepare");
619
Andreas Huber9575c962013-02-05 13:59:56 -0800620 mSource->prepareAsync();
Andreas Huberf9334412010-12-15 15:17:42 -0800621 break;
622 }
623
Chong Zhangdcb89b32013-08-06 09:44:47 -0700624 case kWhatGetTrackInfo:
625 {
Lajos Molnar3f274362015-03-05 14:35:41 -0800626 sp<AReplyToken> replyID;
Chong Zhangdcb89b32013-08-06 09:44:47 -0700627 CHECK(msg->senderAwaitsResponse(&replyID));
628
Chong Zhang404fced2014-06-11 14:45:31 -0700629 Parcel* reply;
630 CHECK(msg->findPointer("reply", (void**)&reply));
631
632 size_t inbandTracks = 0;
Chong Zhangdcb89b32013-08-06 09:44:47 -0700633 if (mSource != NULL) {
Chong Zhang404fced2014-06-11 14:45:31 -0700634 inbandTracks = mSource->getTrackCount();
635 }
636
Chong Zhanga7fa1d92014-06-11 14:49:23 -0700637 size_t ccTracks = 0;
638 if (mCCDecoder != NULL) {
639 ccTracks = mCCDecoder->getTrackCount();
640 }
641
Chong Zhang404fced2014-06-11 14:45:31 -0700642 // total track count
Chong Zhanga7fa1d92014-06-11 14:49:23 -0700643 reply->writeInt32(inbandTracks + ccTracks);
Chong Zhang404fced2014-06-11 14:45:31 -0700644
645 // write inband tracks
646 for (size_t i = 0; i < inbandTracks; ++i) {
647 writeTrackInfo(reply, mSource->getTrackInfo(i));
Chong Zhangdcb89b32013-08-06 09:44:47 -0700648 }
649
Chong Zhanga7fa1d92014-06-11 14:49:23 -0700650 // write CC track
651 for (size_t i = 0; i < ccTracks; ++i) {
652 writeTrackInfo(reply, mCCDecoder->getTrackInfo(i));
653 }
654
Chong Zhangdcb89b32013-08-06 09:44:47 -0700655 sp<AMessage> response = new AMessage;
Chong Zhangdcb89b32013-08-06 09:44:47 -0700656 response->postReply(replyID);
657 break;
658 }
659
Robert Shih7c4f0d72014-07-09 18:53:31 -0700660 case kWhatGetSelectedTrack:
661 {
662 status_t err = INVALID_OPERATION;
663 if (mSource != NULL) {
664 err = OK;
665
666 int32_t type32;
667 CHECK(msg->findInt32("type", (int32_t*)&type32));
668 media_track_type type = (media_track_type)type32;
669 ssize_t selectedTrack = mSource->getSelectedTrack(type);
670
671 Parcel* reply;
672 CHECK(msg->findPointer("reply", (void**)&reply));
673 reply->writeInt32(selectedTrack);
674 }
675
676 sp<AMessage> response = new AMessage;
677 response->setInt32("err", err);
678
Lajos Molnar3f274362015-03-05 14:35:41 -0800679 sp<AReplyToken> replyID;
Robert Shih7c4f0d72014-07-09 18:53:31 -0700680 CHECK(msg->senderAwaitsResponse(&replyID));
681 response->postReply(replyID);
682 break;
683 }
684
Chong Zhangdcb89b32013-08-06 09:44:47 -0700685 case kWhatSelectTrack:
686 {
Lajos Molnar3f274362015-03-05 14:35:41 -0800687 sp<AReplyToken> replyID;
Chong Zhangdcb89b32013-08-06 09:44:47 -0700688 CHECK(msg->senderAwaitsResponse(&replyID));
689
Chong Zhang404fced2014-06-11 14:45:31 -0700690 size_t trackIndex;
691 int32_t select;
Robert Shih6ffb1fd2014-10-29 16:24:32 -0700692 int64_t timeUs;
Chong Zhang404fced2014-06-11 14:45:31 -0700693 CHECK(msg->findSize("trackIndex", &trackIndex));
694 CHECK(msg->findInt32("select", &select));
Robert Shih6ffb1fd2014-10-29 16:24:32 -0700695 CHECK(msg->findInt64("timeUs", &timeUs));
Chong Zhang404fced2014-06-11 14:45:31 -0700696
Chong Zhangdcb89b32013-08-06 09:44:47 -0700697 status_t err = INVALID_OPERATION;
Chong Zhang404fced2014-06-11 14:45:31 -0700698
699 size_t inbandTracks = 0;
Chong Zhangdcb89b32013-08-06 09:44:47 -0700700 if (mSource != NULL) {
Chong Zhang404fced2014-06-11 14:45:31 -0700701 inbandTracks = mSource->getTrackCount();
702 }
Chong Zhanga7fa1d92014-06-11 14:49:23 -0700703 size_t ccTracks = 0;
704 if (mCCDecoder != NULL) {
705 ccTracks = mCCDecoder->getTrackCount();
706 }
Chong Zhang404fced2014-06-11 14:45:31 -0700707
708 if (trackIndex < inbandTracks) {
Robert Shih6ffb1fd2014-10-29 16:24:32 -0700709 err = mSource->selectTrack(trackIndex, select, timeUs);
Robert Shihd3b0bbb2014-07-23 15:00:25 -0700710
711 if (!select && err == OK) {
712 int32_t type;
713 sp<AMessage> info = mSource->getTrackInfo(trackIndex);
714 if (info != NULL
715 && info->findInt32("type", &type)
716 && type == MEDIA_TRACK_TYPE_TIMEDTEXT) {
717 ++mTimedTextGeneration;
718 }
719 }
Chong Zhanga7fa1d92014-06-11 14:49:23 -0700720 } else {
721 trackIndex -= inbandTracks;
722
723 if (trackIndex < ccTracks) {
724 err = mCCDecoder->selectTrack(trackIndex, select);
725 }
Chong Zhangdcb89b32013-08-06 09:44:47 -0700726 }
727
728 sp<AMessage> response = new AMessage;
729 response->setInt32("err", err);
730
731 response->postReply(replyID);
732 break;
733 }
734
Andreas Huberb7c8e912012-11-27 15:02:53 -0800735 case kWhatPollDuration:
736 {
737 int32_t generation;
738 CHECK(msg->findInt32("generation", &generation));
739
740 if (generation != mPollDurationGeneration) {
741 // stale
742 break;
743 }
744
745 int64_t durationUs;
746 if (mDriver != NULL && mSource->getDuration(&durationUs) == OK) {
747 sp<NuPlayerDriver> driver = mDriver.promote();
748 if (driver != NULL) {
749 driver->notifyDuration(durationUs);
750 }
751 }
752
753 msg->post(1000000ll); // poll again in a second.
754 break;
755 }
756
Lajos Molnar1de1e252015-04-30 18:18:34 -0700757 case kWhatSetVideoSurface:
Andreas Huberf9334412010-12-15 15:17:42 -0800758 {
Andreas Huberf9334412010-12-15 15:17:42 -0800759
760 sp<RefBase> obj;
Lajos Molnar1de1e252015-04-30 18:18:34 -0700761 CHECK(msg->findObject("surface", &obj));
762 sp<Surface> surface = static_cast<Surface *>(obj.get());
Lajos Molnara81c6222015-07-10 19:17:45 -0700763
764 ALOGD("onSetVideoSurface(%p, %s video decoder)",
765 surface.get(),
Wei Jia6b7d2ed2015-08-10 15:11:54 -0700766 (mSource != NULL && mStarted && mSource->getFormat(false /* audio */) != NULL
Lajos Molnara81c6222015-07-10 19:17:45 -0700767 && mVideoDecoder != NULL) ? "have" : "no");
768
Wei Jia6b7d2ed2015-08-10 15:11:54 -0700769 // Need to check mStarted before calling mSource->getFormat because NuPlayer might
770 // be in preparing state and it could take long time.
771 // When mStarted is true, mSource must have been set.
772 if (mSource == NULL || !mStarted || mSource->getFormat(false /* audio */) == NULL
Lajos Molnara81c6222015-07-10 19:17:45 -0700773 // NOTE: mVideoDecoder's mSurface is always non-null
774 || (mVideoDecoder != NULL && mVideoDecoder->setVideoSurface(surface) == OK)) {
Lajos Molnar1de1e252015-04-30 18:18:34 -0700775 performSetSurface(surface);
Wei Jiafef808d2014-10-31 17:57:05 -0700776 break;
777 }
778
779 mDeferredActions.push_back(
Krystian Turczyn687a5892016-01-20 14:20:35 +0100780 new FlushDecoderAction(
781 (obj != NULL ? FLUSH_CMD_FLUSH : FLUSH_CMD_NONE) /* audio */,
Wei Jiafef808d2014-10-31 17:57:05 -0700782 FLUSH_CMD_SHUTDOWN /* video */));
783
Lajos Molnar1de1e252015-04-30 18:18:34 -0700784 mDeferredActions.push_back(new SetSurfaceAction(surface));
James Dong0d268a32012-08-31 12:18:27 -0700785
Krystian Turczyn687a5892016-01-20 14:20:35 +0100786 if (obj != NULL) {
Wei Jiafef808d2014-10-31 17:57:05 -0700787 if (mStarted) {
Andy Hung73535852014-09-05 11:42:58 -0700788 // Issue a seek to refresh the video screen only if started otherwise
789 // the extractor may not yet be started and will assert.
790 // If the video decoder is not set (perhaps audio only in this case)
791 // do not perform a seek as it is not needed.
Ronghua Wua73d9e02014-10-08 15:13:29 -0700792 int64_t currentPositionUs = 0;
793 if (getCurrentPosition(&currentPositionUs) == OK) {
794 mDeferredActions.push_back(
Wei Jiac5de0912016-11-18 10:22:14 -0800795 new SeekAction(currentPositionUs,
796 MediaPlayerSeekMode::SEEK_PREVIOUS_SYNC /* mode */));
Ronghua Wua73d9e02014-10-08 15:13:29 -0700797 }
Andy Hung73535852014-09-05 11:42:58 -0700798 }
Wei Jiaac428aa2014-09-02 19:01:34 -0700799
Andreas Huber57a339c2012-12-03 11:18:00 -0800800 // If there is a new surface texture, instantiate decoders
801 // again if possible.
802 mDeferredActions.push_back(
803 new SimpleAction(&NuPlayer::performScanSources));
Andreas Huber57a339c2012-12-03 11:18:00 -0800804
Krystian Turczyn687a5892016-01-20 14:20:35 +0100805 // After a flush without shutdown, decoder is paused.
806 // Don't resume it until source seek is done, otherwise it could
807 // start pulling stale data too soon.
808 mDeferredActions.push_back(
809 new ResumeDecoderAction(false /* needNotify */));
810 }
Chong Zhang7137ec72014-11-12 16:41:05 -0800811
Andreas Huber57a339c2012-12-03 11:18:00 -0800812 processDeferredActions();
Andreas Huberf9334412010-12-15 15:17:42 -0800813 break;
814 }
815
816 case kWhatSetAudioSink:
817 {
Steve Block3856b092011-10-20 11:56:00 +0100818 ALOGV("kWhatSetAudioSink");
Andreas Huberf9334412010-12-15 15:17:42 -0800819
820 sp<RefBase> obj;
821 CHECK(msg->findObject("sink", &obj));
822
823 mAudioSink = static_cast<MediaPlayerBase::AudioSink *>(obj.get());
824 break;
825 }
826
827 case kWhatStart:
828 {
Steve Block3856b092011-10-20 11:56:00 +0100829 ALOGV("kWhatStart");
Wei Jia94211742014-10-28 17:09:06 -0700830 if (mStarted) {
Chong Zhang8a048332015-05-06 15:16:28 -0700831 // do not resume yet if the source is still buffering
832 if (!mPausedForBuffering) {
833 onResume();
834 }
Wei Jia94211742014-10-28 17:09:06 -0700835 } else {
836 onStart();
Lajos Molnar09524832014-07-17 14:29:51 -0700837 }
Chong Zhangefbb6192015-01-30 17:13:27 -0800838 mPausedByClient = false;
Andreas Huberf9334412010-12-15 15:17:42 -0800839 break;
840 }
841
Lajos Molnar3a474aa2015-04-24 17:10:07 -0700842 case kWhatConfigPlayback:
Wei Jia98160162015-02-04 17:01:11 -0800843 {
Lajos Molnar3a474aa2015-04-24 17:10:07 -0700844 sp<AReplyToken> replyID;
845 CHECK(msg->senderAwaitsResponse(&replyID));
846 AudioPlaybackRate rate /* sanitized */;
847 readFromAMessage(msg, &rate);
848 status_t err = OK;
Wei Jia98160162015-02-04 17:01:11 -0800849 if (mRenderer != NULL) {
Wei Jiabf70feb2016-02-19 15:47:16 -0800850 // AudioSink allows only 1.f and 0.f for offload mode.
851 // For other speed, switch to non-offload mode.
852 if (mOffloadAudio && ((rate.mSpeed != 0.f && rate.mSpeed != 1.f)
853 || rate.mPitch != 1.f)) {
854 int64_t currentPositionUs;
855 if (getCurrentPosition(&currentPositionUs) != OK) {
856 currentPositionUs = mPreviousSeekTimeUs;
857 }
858
859 // Set mPlaybackSettings so that the new audio decoder can
860 // be created correctly.
861 mPlaybackSettings = rate;
862 if (!mPaused) {
863 mRenderer->pause();
864 }
Wei Jia5031b2f2016-02-25 11:19:31 -0800865 restartAudio(
Wei Jiabf70feb2016-02-19 15:47:16 -0800866 currentPositionUs, true /* forceNonOffload */,
867 true /* needsToCreateAudioDecoder */);
868 if (!mPaused) {
869 mRenderer->resume();
870 }
871 }
872
Lajos Molnar3a474aa2015-04-24 17:10:07 -0700873 err = mRenderer->setPlaybackSettings(rate);
874 }
875 if (err == OK) {
876 if (rate.mSpeed == 0.f) {
877 onPause();
Wei Jia351ce872016-02-10 13:21:59 -0800878 mPausedByClient = true;
Lajos Molnar3a474aa2015-04-24 17:10:07 -0700879 // save all other settings (using non-paused speed)
880 // so we can restore them on start
881 AudioPlaybackRate newRate = rate;
882 newRate.mSpeed = mPlaybackSettings.mSpeed;
883 mPlaybackSettings = newRate;
884 } else { /* rate.mSpeed != 0.f */
Lajos Molnar3a474aa2015-04-24 17:10:07 -0700885 mPlaybackSettings = rate;
Wei Jia8a092d32016-06-03 14:57:24 -0700886 if (mStarted) {
887 // do not resume yet if the source is still buffering
888 if (!mPausedForBuffering) {
889 onResume();
890 }
891 } else if (mPrepared) {
892 onStart();
893 }
894
895 mPausedByClient = false;
Lajos Molnar3a474aa2015-04-24 17:10:07 -0700896 }
Wei Jia98160162015-02-04 17:01:11 -0800897 }
Ronghua Wu8db88132015-04-22 13:51:35 -0700898
899 if (mVideoDecoder != NULL) {
Praveen Chavanbbaa1442016-04-08 13:33:49 -0700900 sp<AMessage> params = new AMessage();
901 params->setFloat("playback-speed", mPlaybackSettings.mSpeed);
902 mVideoDecoder->setParameters(params);
Ronghua Wu8db88132015-04-22 13:51:35 -0700903 }
904
Lajos Molnar3a474aa2015-04-24 17:10:07 -0700905 sp<AMessage> response = new AMessage;
906 response->setInt32("err", err);
907 response->postReply(replyID);
908 break;
909 }
910
911 case kWhatGetPlaybackSettings:
912 {
913 sp<AReplyToken> replyID;
914 CHECK(msg->senderAwaitsResponse(&replyID));
915 AudioPlaybackRate rate = mPlaybackSettings;
916 status_t err = OK;
917 if (mRenderer != NULL) {
918 err = mRenderer->getPlaybackSettings(&rate);
919 }
920 if (err == OK) {
921 // get playback settings used by renderer, as it may be
922 // slightly off due to audiosink not taking small changes.
923 mPlaybackSettings = rate;
924 if (mPaused) {
925 rate.mSpeed = 0.f;
926 }
927 }
928 sp<AMessage> response = new AMessage;
929 if (err == OK) {
930 writeToAMessage(response, rate);
931 }
932 response->setInt32("err", err);
933 response->postReply(replyID);
934 break;
935 }
936
937 case kWhatConfigSync:
938 {
939 sp<AReplyToken> replyID;
940 CHECK(msg->senderAwaitsResponse(&replyID));
941
942 ALOGV("kWhatConfigSync");
943 AVSyncSettings sync;
944 float videoFpsHint;
945 readFromAMessage(msg, &sync, &videoFpsHint);
946 status_t err = OK;
947 if (mRenderer != NULL) {
948 err = mRenderer->setSyncSettings(sync, videoFpsHint);
949 }
950 if (err == OK) {
951 mSyncSettings = sync;
952 mVideoFpsHint = videoFpsHint;
953 }
954 sp<AMessage> response = new AMessage;
955 response->setInt32("err", err);
956 response->postReply(replyID);
957 break;
958 }
959
960 case kWhatGetSyncSettings:
961 {
962 sp<AReplyToken> replyID;
963 CHECK(msg->senderAwaitsResponse(&replyID));
964 AVSyncSettings sync = mSyncSettings;
965 float videoFps = mVideoFpsHint;
966 status_t err = OK;
967 if (mRenderer != NULL) {
968 err = mRenderer->getSyncSettings(&sync, &videoFps);
969 if (err == OK) {
970 mSyncSettings = sync;
971 mVideoFpsHint = videoFps;
972 }
973 }
974 sp<AMessage> response = new AMessage;
975 if (err == OK) {
976 writeToAMessage(response, sync, videoFps);
977 }
978 response->setInt32("err", err);
979 response->postReply(replyID);
Wei Jia98160162015-02-04 17:01:11 -0800980 break;
981 }
982
Andreas Huberf9334412010-12-15 15:17:42 -0800983 case kWhatScanSources:
984 {
Andreas Huber1aef2112011-01-04 14:01:29 -0800985 int32_t generation;
986 CHECK(msg->findInt32("generation", &generation));
987 if (generation != mScanSourcesGeneration) {
988 // Drop obsolete msg.
989 break;
990 }
991
Andreas Huber5bc087c2010-12-23 10:27:40 -0800992 mScanSourcesPending = false;
993
Steve Block3856b092011-10-20 11:56:00 +0100994 ALOGV("scanning sources haveAudio=%d, haveVideo=%d",
Andreas Huber43c3e6c2011-01-05 12:17:08 -0800995 mAudioDecoder != NULL, mVideoDecoder != NULL);
996
Andreas Huberb7c8e912012-11-27 15:02:53 -0800997 bool mHadAnySourcesBefore =
998 (mAudioDecoder != NULL) || (mVideoDecoder != NULL);
Robert Shih7350b052015-10-01 15:50:14 -0700999 bool rescan = false;
Andreas Huberb7c8e912012-11-27 15:02:53 -08001000
Andy Hung282a7e32014-08-14 15:56:34 -07001001 // initialize video before audio because successful initialization of
1002 // video may change deep buffer mode of audio.
Lajos Molnar1de1e252015-04-30 18:18:34 -07001003 if (mSurface != NULL) {
Robert Shih7350b052015-10-01 15:50:14 -07001004 if (instantiateDecoder(false, &mVideoDecoder) == -EWOULDBLOCK) {
1005 rescan = true;
1006 }
Haynes Mathew George5d246ef2012-07-09 10:36:57 -07001007 }
Andreas Huberf9334412010-12-15 15:17:42 -08001008
Ronghua Wua10fd232014-11-06 16:15:20 -08001009 // Don't try to re-open audio sink if there's an existing decoder.
1010 if (mAudioSink != NULL && mAudioDecoder == NULL) {
Robert Shih7350b052015-10-01 15:50:14 -07001011 if (instantiateDecoder(true, &mAudioDecoder) == -EWOULDBLOCK) {
1012 rescan = true;
1013 }
Andreas Huberf9334412010-12-15 15:17:42 -08001014 }
1015
Andreas Huberb7c8e912012-11-27 15:02:53 -08001016 if (!mHadAnySourcesBefore
1017 && (mAudioDecoder != NULL || mVideoDecoder != NULL)) {
1018 // This is the first time we've found anything playable.
1019
Andreas Huber9575c962013-02-05 13:59:56 -08001020 if (mSourceFlags & Source::FLAG_DYNAMIC_DURATION) {
Andreas Huberb7c8e912012-11-27 15:02:53 -08001021 schedulePollDuration();
1022 }
1023 }
1024
Andreas Hubereac68ba2011-09-27 12:12:25 -07001025 status_t err;
1026 if ((err = mSource->feedMoreTSData()) != OK) {
Andreas Huber1aef2112011-01-04 14:01:29 -08001027 if (mAudioDecoder == NULL && mVideoDecoder == NULL) {
1028 // We're not currently decoding anything (no audio or
1029 // video tracks found) and we just ran out of input data.
Andreas Hubereac68ba2011-09-27 12:12:25 -07001030
1031 if (err == ERROR_END_OF_STREAM) {
1032 notifyListener(MEDIA_PLAYBACK_COMPLETE, 0, 0);
1033 } else {
1034 notifyListener(MEDIA_ERROR, MEDIA_ERROR_UNKNOWN, err);
1035 }
Andreas Huber1aef2112011-01-04 14:01:29 -08001036 }
Andreas Huberf9334412010-12-15 15:17:42 -08001037 break;
1038 }
1039
Robert Shih7350b052015-10-01 15:50:14 -07001040 if (rescan) {
Andreas Huberf9334412010-12-15 15:17:42 -08001041 msg->post(100000ll);
Andreas Huber5bc087c2010-12-23 10:27:40 -08001042 mScanSourcesPending = true;
Andreas Huberf9334412010-12-15 15:17:42 -08001043 }
1044 break;
1045 }
1046
1047 case kWhatVideoNotify:
1048 case kWhatAudioNotify:
1049 {
1050 bool audio = msg->what() == kWhatAudioNotify;
1051
Wei Jia88703c32014-08-06 11:24:07 -07001052 int32_t currentDecoderGeneration =
1053 (audio? mAudioDecoderGeneration : mVideoDecoderGeneration);
1054 int32_t requesterGeneration = currentDecoderGeneration - 1;
1055 CHECK(msg->findInt32("generation", &requesterGeneration));
1056
1057 if (requesterGeneration != currentDecoderGeneration) {
1058 ALOGV("got message from old %s decoder, generation(%d:%d)",
1059 audio ? "audio" : "video", requesterGeneration,
1060 currentDecoderGeneration);
1061 sp<AMessage> reply;
1062 if (!(msg->findMessage("reply", &reply))) {
1063 return;
1064 }
1065
1066 reply->setInt32("err", INFO_DISCONTINUITY);
1067 reply->post();
1068 return;
1069 }
1070
Andreas Huberf9334412010-12-15 15:17:42 -08001071 int32_t what;
Lajos Molnar1cd13982014-01-17 15:12:51 -08001072 CHECK(msg->findInt32("what", &what));
Andreas Huberf9334412010-12-15 15:17:42 -08001073
Chong Zhang7137ec72014-11-12 16:41:05 -08001074 if (what == DecoderBase::kWhatInputDiscontinuity) {
1075 int32_t formatChange;
1076 CHECK(msg->findInt32("formatChange", &formatChange));
Andreas Huberf9334412010-12-15 15:17:42 -08001077
Chong Zhang7137ec72014-11-12 16:41:05 -08001078 ALOGV("%s discontinuity: formatChange %d",
1079 audio ? "audio" : "video", formatChange);
1080
1081 if (formatChange) {
1082 mDeferredActions.push_back(
1083 new FlushDecoderAction(
1084 audio ? FLUSH_CMD_SHUTDOWN : FLUSH_CMD_NONE,
1085 audio ? FLUSH_CMD_NONE : FLUSH_CMD_SHUTDOWN));
Andreas Huberf9334412010-12-15 15:17:42 -08001086 }
Chong Zhang7137ec72014-11-12 16:41:05 -08001087
1088 mDeferredActions.push_back(
1089 new SimpleAction(
1090 &NuPlayer::performScanSources));
1091
1092 processDeferredActions();
1093 } else if (what == DecoderBase::kWhatEOS) {
Andreas Huberdc9bacd2011-09-26 10:53:29 -07001094 int32_t err;
Lajos Molnar1cd13982014-01-17 15:12:51 -08001095 CHECK(msg->findInt32("err", &err));
Andreas Huberdc9bacd2011-09-26 10:53:29 -07001096
1097 if (err == ERROR_END_OF_STREAM) {
Steve Block3856b092011-10-20 11:56:00 +01001098 ALOGV("got %s decoder EOS", audio ? "audio" : "video");
Andreas Huberdc9bacd2011-09-26 10:53:29 -07001099 } else {
Steve Block3856b092011-10-20 11:56:00 +01001100 ALOGV("got %s decoder EOS w/ error %d",
Andreas Huberdc9bacd2011-09-26 10:53:29 -07001101 audio ? "audio" : "video",
1102 err);
1103 }
1104
1105 mRenderer->queueEOS(audio, err);
Chong Zhang7137ec72014-11-12 16:41:05 -08001106 } else if (what == DecoderBase::kWhatFlushCompleted) {
Steve Block3856b092011-10-20 11:56:00 +01001107 ALOGV("decoder %s flush completed", audio ? "audio" : "video");
Andreas Huberf9334412010-12-15 15:17:42 -08001108
Andy Hung8d121d42014-10-03 09:53:53 -07001109 handleFlushComplete(audio, true /* isDecoder */);
Andreas Huber3831a062010-12-21 10:22:33 -08001110 finishFlushIfPossible();
Chong Zhang7137ec72014-11-12 16:41:05 -08001111 } else if (what == DecoderBase::kWhatVideoSizeChanged) {
Lajos Molnar1cd13982014-01-17 15:12:51 -08001112 sp<AMessage> format;
1113 CHECK(msg->findMessage("format", &format));
1114
Wei Jiac6cfd702014-11-11 16:33:20 -08001115 sp<AMessage> inputFormat =
1116 mSource->getFormat(false /* audio */);
Andreas Huber3831a062010-12-21 10:22:33 -08001117
Bartosz Dolewski26936f72014-12-11 12:47:08 +01001118 setVideoScalingMode(mVideoScalingMode);
Wei Jiac6cfd702014-11-11 16:33:20 -08001119 updateVideoSize(inputFormat, format);
Chong Zhang7137ec72014-11-12 16:41:05 -08001120 } else if (what == DecoderBase::kWhatShutdownCompleted) {
Steve Block3856b092011-10-20 11:56:00 +01001121 ALOGV("%s shutdown completed", audio ? "audio" : "video");
Andreas Huber3831a062010-12-21 10:22:33 -08001122 if (audio) {
1123 mAudioDecoder.clear();
Wei Jia686e8e52017-04-03 14:08:01 -07001124 mAudioDecoderError = false;
Wei Jia57568df2014-09-22 10:16:29 -07001125 ++mAudioDecoderGeneration;
Andreas Huber3831a062010-12-21 10:22:33 -08001126
1127 CHECK_EQ((int)mFlushingAudio, (int)SHUTTING_DOWN_DECODER);
1128 mFlushingAudio = SHUT_DOWN;
1129 } else {
1130 mVideoDecoder.clear();
Wei Jia686e8e52017-04-03 14:08:01 -07001131 mVideoDecoderError = false;
Wei Jia57568df2014-09-22 10:16:29 -07001132 ++mVideoDecoderGeneration;
Andreas Huber3831a062010-12-21 10:22:33 -08001133
1134 CHECK_EQ((int)mFlushingVideo, (int)SHUTTING_DOWN_DECODER);
1135 mFlushingVideo = SHUT_DOWN;
1136 }
1137
1138 finishFlushIfPossible();
Chong Zhangf8d71772014-11-26 15:08:34 -08001139 } else if (what == DecoderBase::kWhatResumeCompleted) {
1140 finishResume();
Chong Zhang7137ec72014-11-12 16:41:05 -08001141 } else if (what == DecoderBase::kWhatError) {
Chong Zhangf4c0a942014-08-11 15:14:10 -07001142 status_t err;
Andy Hung2abde2c2014-09-30 14:40:32 -07001143 if (!msg->findInt32("err", &err) || err == OK) {
Chong Zhangf4c0a942014-08-11 15:14:10 -07001144 err = UNKNOWN_ERROR;
1145 }
Andy Hungcf31f1e2014-09-23 14:59:01 -07001146
Andy Hung2abde2c2014-09-30 14:40:32 -07001147 // Decoder errors can be due to Source (e.g. from streaming),
1148 // or from decoding corrupted bitstreams, or from other decoder
1149 // MediaCodec operations (e.g. from an ongoing reset or seek).
Andy Hung202bce12014-12-03 11:47:36 -08001150 // They may also be due to openAudioSink failure at
1151 // decoder start or after a format change.
Andy Hung2abde2c2014-09-30 14:40:32 -07001152 //
1153 // We try to gracefully shut down the affected decoder if possible,
1154 // rather than trying to force the shutdown with something
1155 // similar to performReset(). This method can lead to a hang
1156 // if MediaCodec functions block after an error, but they should
1157 // typically return INVALID_OPERATION instead of blocking.
1158
1159 FlushStatus *flushing = audio ? &mFlushingAudio : &mFlushingVideo;
1160 ALOGE("received error(%#x) from %s decoder, flushing(%d), now shutting down",
1161 err, audio ? "audio" : "video", *flushing);
1162
1163 switch (*flushing) {
1164 case NONE:
1165 mDeferredActions.push_back(
Wei Jiafef808d2014-10-31 17:57:05 -07001166 new FlushDecoderAction(
1167 audio ? FLUSH_CMD_SHUTDOWN : FLUSH_CMD_NONE,
1168 audio ? FLUSH_CMD_NONE : FLUSH_CMD_SHUTDOWN));
Andy Hung2abde2c2014-09-30 14:40:32 -07001169 processDeferredActions();
1170 break;
1171 case FLUSHING_DECODER:
1172 *flushing = FLUSHING_DECODER_SHUTDOWN; // initiate shutdown after flush.
1173 break; // Wait for flush to complete.
1174 case FLUSHING_DECODER_SHUTDOWN:
1175 break; // Wait for flush to complete.
1176 case SHUTTING_DOWN_DECODER:
1177 break; // Wait for shutdown to complete.
1178 case FLUSHED:
Andy Hung2abde2c2014-09-30 14:40:32 -07001179 getDecoder(audio)->initiateShutdown(); // In the middle of a seek.
1180 *flushing = SHUTTING_DOWN_DECODER; // Shut down.
1181 break;
1182 case SHUT_DOWN:
1183 finishFlushIfPossible(); // Should not occur.
1184 break; // Finish anyways.
Marco Nelissen9e2b7912014-08-18 16:13:03 -07001185 }
Wei Jia686e8e52017-04-03 14:08:01 -07001186 if (mSource != nullptr) {
1187 if (audio) {
Wei Jia2f6d8c52017-04-11 09:50:31 -07001188 if (mVideoDecoderError || mSource->getFormat(false /* audio */) == NULL
Wei Jiaf86731d2017-07-11 17:24:34 -07001189 || mSurface == NULL || mVideoDecoder == NULL) {
Wei Jia686e8e52017-04-03 14:08:01 -07001190 // When both audio and video have error, or this stream has only audio
1191 // which has error, notify client of error.
1192 notifyListener(MEDIA_ERROR, MEDIA_ERROR_UNKNOWN, err);
1193 } else {
1194 // Only audio track has error. Video track could be still good to play.
1195 notifyListener(MEDIA_INFO, MEDIA_INFO_PLAY_AUDIO_ERROR, err);
1196 }
1197 mAudioDecoderError = true;
1198 } else {
Wei Jia2f6d8c52017-04-11 09:50:31 -07001199 if (mAudioDecoderError || mSource->getFormat(true /* audio */) == NULL
Wei Jiaf86731d2017-07-11 17:24:34 -07001200 || mAudioSink == NULL || mAudioDecoder == NULL) {
Wei Jia686e8e52017-04-03 14:08:01 -07001201 // When both audio and video have error, or this stream has only video
1202 // which has error, notify client of error.
1203 notifyListener(MEDIA_ERROR, MEDIA_ERROR_UNKNOWN, err);
1204 } else {
1205 // Only video track has error. Audio track could be still good to play.
1206 notifyListener(MEDIA_INFO, MEDIA_INFO_PLAY_VIDEO_ERROR, err);
1207 }
1208 mVideoDecoderError = true;
1209 }
1210 }
Lajos Molnar1cd13982014-01-17 15:12:51 -08001211 } else {
1212 ALOGV("Unhandled decoder notification %d '%c%c%c%c'.",
Andreas Hubera1f8ab02012-11-30 10:53:22 -08001213 what,
1214 what >> 24,
1215 (what >> 16) & 0xff,
1216 (what >> 8) & 0xff,
1217 what & 0xff);
Andreas Huberf9334412010-12-15 15:17:42 -08001218 }
1219
1220 break;
1221 }
1222
1223 case kWhatRendererNotify:
1224 {
Wei Jia57568df2014-09-22 10:16:29 -07001225 int32_t requesterGeneration = mRendererGeneration - 1;
1226 CHECK(msg->findInt32("generation", &requesterGeneration));
1227 if (requesterGeneration != mRendererGeneration) {
1228 ALOGV("got message from old renderer, generation(%d:%d)",
1229 requesterGeneration, mRendererGeneration);
1230 return;
1231 }
1232
Andreas Huberf9334412010-12-15 15:17:42 -08001233 int32_t what;
1234 CHECK(msg->findInt32("what", &what));
1235
1236 if (what == Renderer::kWhatEOS) {
1237 int32_t audio;
1238 CHECK(msg->findInt32("audio", &audio));
1239
Andreas Huberc92fd242011-08-16 13:48:44 -07001240 int32_t finalResult;
1241 CHECK(msg->findInt32("finalResult", &finalResult));
1242
Andreas Huberf9334412010-12-15 15:17:42 -08001243 if (audio) {
1244 mAudioEOS = true;
1245 } else {
1246 mVideoEOS = true;
1247 }
1248
Andreas Huberc92fd242011-08-16 13:48:44 -07001249 if (finalResult == ERROR_END_OF_STREAM) {
Steve Block3856b092011-10-20 11:56:00 +01001250 ALOGV("reached %s EOS", audio ? "audio" : "video");
Andreas Huberc92fd242011-08-16 13:48:44 -07001251 } else {
Steve Block29357bc2012-01-06 19:20:56 +00001252 ALOGE("%s track encountered an error (%d)",
Andreas Huberc92fd242011-08-16 13:48:44 -07001253 audio ? "audio" : "video", finalResult);
1254
1255 notifyListener(
1256 MEDIA_ERROR, MEDIA_ERROR_UNKNOWN, finalResult);
1257 }
Andreas Huberf9334412010-12-15 15:17:42 -08001258
1259 if ((mAudioEOS || mAudioDecoder == NULL)
1260 && (mVideoEOS || mVideoDecoder == NULL)) {
1261 notifyListener(MEDIA_PLAYBACK_COMPLETE, 0, 0);
1262 }
Andreas Huber3fe62152011-09-16 15:09:22 -07001263 } else if (what == Renderer::kWhatFlushComplete) {
Andreas Huberf9334412010-12-15 15:17:42 -08001264 int32_t audio;
1265 CHECK(msg->findInt32("audio", &audio));
1266
Wei Jia3261f0d2015-10-08 13:58:33 -07001267 if (audio) {
1268 mAudioEOS = false;
1269 } else {
1270 mVideoEOS = false;
1271 }
1272
Steve Block3856b092011-10-20 11:56:00 +01001273 ALOGV("renderer %s flush completed.", audio ? "audio" : "video");
Wei Jiada4252f2015-07-14 18:15:28 -07001274 if (audio && (mFlushingAudio == NONE || mFlushingAudio == FLUSHED
1275 || mFlushingAudio == SHUT_DOWN)) {
1276 // Flush has been handled by tear down.
1277 break;
1278 }
Andy Hung8d121d42014-10-03 09:53:53 -07001279 handleFlushComplete(audio, false /* isDecoder */);
1280 finishFlushIfPossible();
James Dongf57b4ea2012-07-20 13:38:36 -07001281 } else if (what == Renderer::kWhatVideoRenderingStart) {
1282 notifyListener(MEDIA_INFO, MEDIA_INFO_RENDERING_START, 0);
Lajos Molnarcbaffcf2013-08-14 18:30:38 -07001283 } else if (what == Renderer::kWhatMediaRenderingStart) {
1284 ALOGV("media rendering started");
1285 notifyListener(MEDIA_STARTED, 0, 0);
Ronghua Wufaeb0f22015-05-21 12:20:21 -07001286 } else if (what == Renderer::kWhatAudioTearDown) {
Ronghua Wu08529172014-10-02 16:55:52 -07001287 int32_t reason;
1288 CHECK(msg->findInt32("reason", &reason));
Ronghua Wufaeb0f22015-05-21 12:20:21 -07001289 ALOGV("Tear down audio with reason %d.", reason);
Wei Jia8456ddd2016-04-22 14:46:43 -07001290 if (reason == Renderer::kDueToTimeout && !(mPaused && mOffloadAudio)) {
1291 // TimeoutWhenPaused is only for offload mode.
Wei Jia355b2bb2018-04-09 16:36:23 -07001292 ALOGW("Received a stale message for teardown, mPaused(%d), mOffloadAudio(%d)",
1293 mPaused, mOffloadAudio);
Wei Jia8456ddd2016-04-22 14:46:43 -07001294 break;
1295 }
Wei Jiada4252f2015-07-14 18:15:28 -07001296 int64_t positionUs;
Robert Shih1a5c8592015-08-04 18:07:44 -07001297 if (!msg->findInt64("positionUs", &positionUs)) {
1298 positionUs = mPreviousSeekTimeUs;
1299 }
Ronghua Wufaeb0f22015-05-21 12:20:21 -07001300
Wei Jia5031b2f2016-02-25 11:19:31 -08001301 restartAudio(
Wei Jiaa05f1e32016-03-25 16:31:22 -07001302 positionUs, reason == Renderer::kForceNonOffload /* forceNonOffload */,
1303 reason != Renderer::kDueToTimeout /* needsToCreateAudioDecoder */);
Andreas Huberf9334412010-12-15 15:17:42 -08001304 }
1305 break;
1306 }
1307
1308 case kWhatMoreDataQueued:
1309 {
1310 break;
1311 }
1312
Andreas Huber1aef2112011-01-04 14:01:29 -08001313 case kWhatReset:
1314 {
Steve Block3856b092011-10-20 11:56:00 +01001315 ALOGV("kWhatReset");
Andreas Huber1aef2112011-01-04 14:01:29 -08001316
Ronghua Wu64c2d172015-10-07 16:52:19 -07001317 mResetting = true;
Ray Essickeda32522018-02-28 12:08:28 -08001318 updatePlaybackTimer(true /* stopping */, "kWhatReset");
1319 updateRebufferingTimer(true /* stopping */, true /* exiting */);
Ronghua Wu64c2d172015-10-07 16:52:19 -07001320
Andreas Hubera1f8ab02012-11-30 10:53:22 -08001321 mDeferredActions.push_back(
Wei Jiafef808d2014-10-31 17:57:05 -07001322 new FlushDecoderAction(
1323 FLUSH_CMD_SHUTDOWN /* audio */,
1324 FLUSH_CMD_SHUTDOWN /* video */));
Andreas Huberb7c8e912012-11-27 15:02:53 -08001325
Andreas Hubera1f8ab02012-11-30 10:53:22 -08001326 mDeferredActions.push_back(
1327 new SimpleAction(&NuPlayer::performReset));
Andreas Huberb58ce9f2011-11-28 16:27:35 -08001328
Andreas Hubera1f8ab02012-11-30 10:53:22 -08001329 processDeferredActions();
Andreas Huber1aef2112011-01-04 14:01:29 -08001330 break;
1331 }
1332
Wei Jia52c28512017-09-13 18:17:51 -07001333 case kWhatNotifyTime:
1334 {
1335 ALOGV("kWhatNotifyTime");
1336 int64_t timerUs;
1337 CHECK(msg->findInt64("timerUs", &timerUs));
1338
1339 notifyListener(MEDIA_NOTIFY_TIME, timerUs, 0);
1340 break;
1341 }
1342
Andreas Huber43c3e6c2011-01-05 12:17:08 -08001343 case kWhatSeek:
1344 {
1345 int64_t seekTimeUs;
Wei Jiac5de0912016-11-18 10:22:14 -08001346 int32_t mode;
Wei Jiae427abf2014-09-22 15:21:11 -07001347 int32_t needNotify;
Andreas Huber43c3e6c2011-01-05 12:17:08 -08001348 CHECK(msg->findInt64("seekTimeUs", &seekTimeUs));
Wei Jiac5de0912016-11-18 10:22:14 -08001349 CHECK(msg->findInt32("mode", &mode));
Wei Jiae427abf2014-09-22 15:21:11 -07001350 CHECK(msg->findInt32("needNotify", &needNotify));
Andreas Huber43c3e6c2011-01-05 12:17:08 -08001351
Wei Jiac5de0912016-11-18 10:22:14 -08001352 ALOGV("kWhatSeek seekTimeUs=%lld us, mode=%d, needNotify=%d",
1353 (long long)seekTimeUs, mode, needNotify);
Andreas Huber43c3e6c2011-01-05 12:17:08 -08001354
Wei Jia1061c9c2015-05-19 16:02:17 -07001355 if (!mStarted) {
1356 // Seek before the player is started. In order to preview video,
1357 // need to start the player and pause it. This branch is called
1358 // only once if needed. After the player is started, any seek
1359 // operation will go through normal path.
Robert Shih0c61a0d2015-07-06 15:09:10 -07001360 // Audio-only cases are handled separately.
Wei Jiac5de0912016-11-18 10:22:14 -08001361 onStart(seekTimeUs, (MediaPlayerSeekMode)mode);
Robert Shih0c61a0d2015-07-06 15:09:10 -07001362 if (mStarted) {
1363 onPause();
1364 mPausedByClient = true;
1365 }
Wei Jia1061c9c2015-05-19 16:02:17 -07001366 if (needNotify) {
1367 notifyDriverSeekComplete();
1368 }
1369 break;
1370 }
1371
Andreas Hubera1f8ab02012-11-30 10:53:22 -08001372 mDeferredActions.push_back(
Wei Jiafef808d2014-10-31 17:57:05 -07001373 new FlushDecoderAction(FLUSH_CMD_FLUSH /* audio */,
1374 FLUSH_CMD_FLUSH /* video */));
Andreas Huber43c3e6c2011-01-05 12:17:08 -08001375
Wei Jiae427abf2014-09-22 15:21:11 -07001376 mDeferredActions.push_back(
Wei Jiac5de0912016-11-18 10:22:14 -08001377 new SeekAction(seekTimeUs, (MediaPlayerSeekMode)mode));
Andreas Huber43c3e6c2011-01-05 12:17:08 -08001378
Chong Zhangf8d71772014-11-26 15:08:34 -08001379 // After a flush without shutdown, decoder is paused.
1380 // Don't resume it until source seek is done, otherwise it could
Chong Zhang7137ec72014-11-12 16:41:05 -08001381 // start pulling stale data too soon.
1382 mDeferredActions.push_back(
Chong Zhangf8d71772014-11-26 15:08:34 -08001383 new ResumeDecoderAction(needNotify));
Chong Zhang7137ec72014-11-12 16:41:05 -08001384
Andreas Hubera1f8ab02012-11-30 10:53:22 -08001385 processDeferredActions();
Andreas Huber43c3e6c2011-01-05 12:17:08 -08001386 break;
1387 }
1388
Andreas Huberb4082222011-01-20 15:23:04 -08001389 case kWhatPause:
1390 {
Chong Zhangefbb6192015-01-30 17:13:27 -08001391 onPause();
1392 mPausedByClient = true;
Andreas Huberb4082222011-01-20 15:23:04 -08001393 break;
1394 }
1395
Andreas Huberb5f25f02013-02-05 10:14:26 -08001396 case kWhatSourceNotify:
1397 {
Andreas Huber9575c962013-02-05 13:59:56 -08001398 onSourceNotify(msg);
Andreas Huberb5f25f02013-02-05 10:14:26 -08001399 break;
1400 }
1401
Chong Zhanga7fa1d92014-06-11 14:49:23 -07001402 case kWhatClosedCaptionNotify:
1403 {
1404 onClosedCaptionNotify(msg);
1405 break;
1406 }
1407
Hassan Shojaniacefac142017-02-06 21:02:02 -08001408 case kWhatPrepareDrm:
1409 {
1410 status_t status = onPrepareDrm(msg);
1411
1412 sp<AMessage> response = new AMessage;
1413 response->setInt32("status", status);
1414 sp<AReplyToken> replyID;
1415 CHECK(msg->senderAwaitsResponse(&replyID));
1416 response->postReply(replyID);
1417 break;
1418 }
1419
1420 case kWhatReleaseDrm:
1421 {
1422 status_t status = onReleaseDrm();
1423
1424 sp<AMessage> response = new AMessage;
1425 response->setInt32("status", status);
1426 sp<AReplyToken> replyID;
1427 CHECK(msg->senderAwaitsResponse(&replyID));
1428 response->postReply(replyID);
1429 break;
1430 }
1431
Dongwon Kang47afe0a2018-03-27 15:15:30 -07001432 case kWhatMediaClockNotify:
1433 {
1434 ALOGV("kWhatMediaClockNotify");
1435 int64_t anchorMediaUs, anchorRealUs;
1436 float playbackRate;
1437 CHECK(msg->findInt64("anchor-media-us", &anchorMediaUs));
1438 CHECK(msg->findInt64("anchor-real-us", &anchorRealUs));
1439 CHECK(msg->findFloat("playback-rate", &playbackRate));
1440
1441 Parcel in;
1442 in.writeInt64(anchorMediaUs);
1443 in.writeInt64(anchorRealUs);
1444 in.writeFloat(playbackRate);
1445
1446 notifyListener(MEDIA_TIME_DISCONTINUITY, 0, 0, &in);
1447 break;
1448 }
1449
Ray Essickffc941f2018-05-18 11:08:37 -07001450 case kWhatGetStats:
1451 {
1452 ALOGV("kWhatGetStats");
1453
1454 Vector<sp<AMessage>> *trackStats;
1455 CHECK(msg->findPointer("trackstats", (void**)&trackStats));
1456
1457 trackStats->clear();
1458 if (mVideoDecoder != NULL) {
1459 trackStats->push_back(mVideoDecoder->getStats());
1460 }
1461 if (mAudioDecoder != NULL) {
1462 trackStats->push_back(mAudioDecoder->getStats());
1463 }
1464
1465 // respond for synchronization
1466 sp<AMessage> response = new AMessage;
1467 sp<AReplyToken> replyID;
1468 CHECK(msg->senderAwaitsResponse(&replyID));
1469 response->postReply(replyID);
1470 break;
1471 }
1472
Andreas Huberf9334412010-12-15 15:17:42 -08001473 default:
1474 TRESPASS();
1475 break;
1476 }
1477}
1478
Wei Jia94211742014-10-28 17:09:06 -07001479void NuPlayer::onResume() {
Ronghua Wu64c2d172015-10-07 16:52:19 -07001480 if (!mPaused || mResetting) {
1481 ALOGD_IF(mResetting, "resetting, onResume discarded");
Chong Zhangefbb6192015-01-30 17:13:27 -08001482 return;
1483 }
1484 mPaused = false;
Wei Jia94211742014-10-28 17:09:06 -07001485 if (mSource != NULL) {
1486 mSource->resume();
1487 } else {
1488 ALOGW("resume called when source is gone or not set");
1489 }
1490 // |mAudioDecoder| may have been released due to the pause timeout, so re-create it if
1491 // needed.
1492 if (audioDecoderStillNeeded() && mAudioDecoder == NULL) {
1493 instantiateDecoder(true /* audio */, &mAudioDecoder);
1494 }
1495 if (mRenderer != NULL) {
1496 mRenderer->resume();
1497 } else {
1498 ALOGW("resume called when renderer is gone or not set");
1499 }
Ray Essickd4d00612017-01-03 09:36:27 -08001500
Ray Essick0d98c182017-11-09 13:42:42 -08001501 startPlaybackTimer("onresume");
Wei Jia94211742014-10-28 17:09:06 -07001502}
1503
Lajos Molnarfcd3e942015-03-31 10:06:48 -07001504status_t NuPlayer::onInstantiateSecureDecoders() {
1505 status_t err;
1506 if (!(mSourceFlags & Source::FLAG_SECURE)) {
1507 return BAD_TYPE;
1508 }
1509
1510 if (mRenderer != NULL) {
1511 ALOGE("renderer should not be set when instantiating secure decoders");
1512 return UNKNOWN_ERROR;
1513 }
1514
1515 // TRICKY: We rely on mRenderer being null, so that decoder does not start requesting
1516 // data on instantiation.
Lajos Molnar1de1e252015-04-30 18:18:34 -07001517 if (mSurface != NULL) {
Lajos Molnarfcd3e942015-03-31 10:06:48 -07001518 err = instantiateDecoder(false, &mVideoDecoder);
1519 if (err != OK) {
1520 return err;
1521 }
1522 }
1523
1524 if (mAudioSink != NULL) {
1525 err = instantiateDecoder(true, &mAudioDecoder);
1526 if (err != OK) {
1527 return err;
1528 }
1529 }
1530 return OK;
1531}
1532
Wei Jiac5de0912016-11-18 10:22:14 -08001533void NuPlayer::onStart(int64_t startPositionUs, MediaPlayerSeekMode mode) {
Hassan Shojaniacefac142017-02-06 21:02:02 -08001534 ALOGV("onStart: mCrypto: %p (%d)", mCrypto.get(),
1535 (mCrypto != NULL ? mCrypto->getStrongCount() : 0));
1536
Robert Shih0c61a0d2015-07-06 15:09:10 -07001537 if (!mSourceStarted) {
1538 mSourceStarted = true;
1539 mSource->start();
1540 }
1541 if (startPositionUs > 0) {
Wei Jiac5de0912016-11-18 10:22:14 -08001542 performSeek(startPositionUs, mode);
Robert Shih0c61a0d2015-07-06 15:09:10 -07001543 if (mSource->getFormat(false /* audio */) == NULL) {
1544 return;
1545 }
1546 }
1547
Wei Jia94211742014-10-28 17:09:06 -07001548 mOffloadAudio = false;
1549 mAudioEOS = false;
1550 mVideoEOS = false;
Wei Jia94211742014-10-28 17:09:06 -07001551 mStarted = true;
Wei Jiafe8fe7d2016-06-08 10:29:25 -07001552 mPaused = false;
Wei Jia94211742014-10-28 17:09:06 -07001553
Wei Jia94211742014-10-28 17:09:06 -07001554 uint32_t flags = 0;
1555
1556 if (mSource->isRealTime()) {
1557 flags |= Renderer::FLAG_REAL_TIME;
1558 }
1559
Wei Jia9737d342016-12-28 14:59:59 -08001560 bool hasAudio = (mSource->getFormat(true /* audio */) != NULL);
1561 bool hasVideo = (mSource->getFormat(false /* audio */) != NULL);
1562 if (!hasAudio && !hasVideo) {
Wei Jia1de83d52016-10-10 10:15:03 -07001563 ALOGE("no metadata for either audio or video source");
1564 mSource->stop();
1565 mSourceStarted = false;
1566 notifyListener(MEDIA_ERROR, MEDIA_ERROR_UNKNOWN, ERROR_MALFORMED);
1567 return;
1568 }
Wei Jia9737d342016-12-28 14:59:59 -08001569 ALOGV_IF(!hasAudio, "no metadata for audio source"); // video only stream
1570
1571 sp<MetaData> audioMeta = mSource->getFormatMeta(true /* audio */);
Wei Jia1de83d52016-10-10 10:15:03 -07001572
Wei Jia94211742014-10-28 17:09:06 -07001573 audio_stream_type_t streamType = AUDIO_STREAM_MUSIC;
1574 if (mAudioSink != NULL) {
1575 streamType = mAudioSink->getAudioStreamType();
1576 }
1577
Wei Jia94211742014-10-28 17:09:06 -07001578 mOffloadAudio =
Wei Jia9737d342016-12-28 14:59:59 -08001579 canOffloadStream(audioMeta, hasVideo, mSource->isStreaming(), streamType)
Wei Jiabf70feb2016-02-19 15:47:16 -08001580 && (mPlaybackSettings.mSpeed == 1.f && mPlaybackSettings.mPitch == 1.f);
Hassan Shojaniacefac142017-02-06 21:02:02 -08001581
1582 // Modular DRM: Disabling audio offload if the source is protected
1583 if (mOffloadAudio && mIsDrmProtected) {
1584 mOffloadAudio = false;
1585 ALOGV("onStart: Disabling mOffloadAudio now that the source is protected.");
1586 }
1587
Wei Jia94211742014-10-28 17:09:06 -07001588 if (mOffloadAudio) {
1589 flags |= Renderer::FLAG_OFFLOAD_AUDIO;
1590 }
1591
Lajos Molnar1d15ab52015-03-04 16:46:34 -08001592 sp<AMessage> notify = new AMessage(kWhatRendererNotify, this);
Wei Jia94211742014-10-28 17:09:06 -07001593 ++mRendererGeneration;
1594 notify->setInt32("generation", mRendererGeneration);
Wei Jia0a68f662017-08-30 18:01:26 -07001595 mRenderer = new Renderer(mAudioSink, mMediaClock, notify, flags);
Wei Jia94211742014-10-28 17:09:06 -07001596 mRendererLooper = new ALooper;
1597 mRendererLooper->setName("NuPlayerRenderer");
1598 mRendererLooper->start(false, false, ANDROID_PRIORITY_AUDIO);
1599 mRendererLooper->registerHandler(mRenderer);
Lajos Molnar3a474aa2015-04-24 17:10:07 -07001600
1601 status_t err = mRenderer->setPlaybackSettings(mPlaybackSettings);
1602 if (err != OK) {
1603 mSource->stop();
Robert Shih0c61a0d2015-07-06 15:09:10 -07001604 mSourceStarted = false;
Lajos Molnar3a474aa2015-04-24 17:10:07 -07001605 notifyListener(MEDIA_ERROR, MEDIA_ERROR_UNKNOWN, err);
1606 return;
Wei Jiac8206ff2015-03-04 13:59:37 -08001607 }
Wei Jia94211742014-10-28 17:09:06 -07001608
Ronghua Wuc8a70d32015-04-29 16:26:34 -07001609 float rate = getFrameRate();
1610 if (rate > 0) {
Wei Jia94211742014-10-28 17:09:06 -07001611 mRenderer->setVideoFrameRate(rate);
1612 }
1613
Wei Jiac6cfd702014-11-11 16:33:20 -08001614 if (mVideoDecoder != NULL) {
1615 mVideoDecoder->setRenderer(mRenderer);
1616 }
1617 if (mAudioDecoder != NULL) {
1618 mAudioDecoder->setRenderer(mRenderer);
1619 }
1620
Ray Essick0d98c182017-11-09 13:42:42 -08001621 startPlaybackTimer("onstart");
Ray Essickd4d00612017-01-03 09:36:27 -08001622
Wei Jia94211742014-10-28 17:09:06 -07001623 postScanSources();
1624}
1625
Ray Essick0d98c182017-11-09 13:42:42 -08001626void NuPlayer::startPlaybackTimer(const char *where) {
1627 Mutex::Autolock autoLock(mPlayingTimeLock);
1628 if (mLastStartedPlayingTimeNs == 0) {
1629 mLastStartedPlayingTimeNs = systemTime();
1630 ALOGV("startPlaybackTimer() time %20" PRId64 " (%s)", mLastStartedPlayingTimeNs, where);
1631 }
1632}
1633
Ray Essickeda32522018-02-28 12:08:28 -08001634void NuPlayer::updatePlaybackTimer(bool stopping, const char *where) {
Ray Essick0d98c182017-11-09 13:42:42 -08001635 Mutex::Autolock autoLock(mPlayingTimeLock);
1636
Ray Essickeda32522018-02-28 12:08:28 -08001637 ALOGV("updatePlaybackTimer(%s) time %20" PRId64 " (%s)",
1638 stopping ? "stop" : "snap", mLastStartedPlayingTimeNs, where);
Ray Essick0d98c182017-11-09 13:42:42 -08001639
1640 if (mLastStartedPlayingTimeNs != 0) {
1641 sp<NuPlayerDriver> driver = mDriver.promote();
Ray Essickeda32522018-02-28 12:08:28 -08001642 int64_t now = systemTime();
Ray Essick0d98c182017-11-09 13:42:42 -08001643 if (driver != NULL) {
Ray Essick0d98c182017-11-09 13:42:42 -08001644 int64_t played = now - mLastStartedPlayingTimeNs;
Ray Essickeda32522018-02-28 12:08:28 -08001645 ALOGV("updatePlaybackTimer() log %20" PRId64 "", played);
Ray Essick0d98c182017-11-09 13:42:42 -08001646
1647 if (played > 0) {
1648 driver->notifyMorePlayingTimeUs((played+500)/1000);
1649 }
1650 }
Ray Essickeda32522018-02-28 12:08:28 -08001651 if (stopping) {
1652 mLastStartedPlayingTimeNs = 0;
1653 } else {
1654 mLastStartedPlayingTimeNs = now;
1655 }
Ray Essick0d98c182017-11-09 13:42:42 -08001656 }
1657}
1658
Ray Essick58e0f7a2017-11-21 10:59:38 -08001659void NuPlayer::startRebufferingTimer() {
1660 Mutex::Autolock autoLock(mPlayingTimeLock);
1661 if (mLastStartedRebufferingTimeNs == 0) {
1662 mLastStartedRebufferingTimeNs = systemTime();
1663 ALOGV("startRebufferingTimer() time %20" PRId64 "", mLastStartedRebufferingTimeNs);
1664 }
1665}
1666
Ray Essickeda32522018-02-28 12:08:28 -08001667void NuPlayer::updateRebufferingTimer(bool stopping, bool exitingPlayback) {
Ray Essick58e0f7a2017-11-21 10:59:38 -08001668 Mutex::Autolock autoLock(mPlayingTimeLock);
1669
Ray Essickeda32522018-02-28 12:08:28 -08001670 ALOGV("updateRebufferingTimer(%s) time %20" PRId64 " (exiting %d)",
1671 stopping ? "stop" : "snap", mLastStartedRebufferingTimeNs, exitingPlayback);
Ray Essick58e0f7a2017-11-21 10:59:38 -08001672
1673 if (mLastStartedRebufferingTimeNs != 0) {
1674 sp<NuPlayerDriver> driver = mDriver.promote();
Ray Essickeda32522018-02-28 12:08:28 -08001675 int64_t now = systemTime();
Ray Essick58e0f7a2017-11-21 10:59:38 -08001676 if (driver != NULL) {
Ray Essick58e0f7a2017-11-21 10:59:38 -08001677 int64_t rebuffered = now - mLastStartedRebufferingTimeNs;
Ray Essickeda32522018-02-28 12:08:28 -08001678 ALOGV("updateRebufferingTimer() log %20" PRId64 "", rebuffered);
Ray Essick58e0f7a2017-11-21 10:59:38 -08001679
1680 if (rebuffered > 0) {
1681 driver->notifyMoreRebufferingTimeUs((rebuffered+500)/1000);
1682 if (exitingPlayback) {
1683 driver->notifyRebufferingWhenExit(true);
1684 }
1685 }
1686 }
Ray Essickeda32522018-02-28 12:08:28 -08001687 if (stopping) {
1688 mLastStartedRebufferingTimeNs = 0;
1689 } else {
1690 mLastStartedRebufferingTimeNs = now;
1691 }
Ray Essick58e0f7a2017-11-21 10:59:38 -08001692 }
1693}
1694
Ray Essickeda32522018-02-28 12:08:28 -08001695void NuPlayer::updateInternalTimers() {
1696 // update values, but ticking clocks keep ticking
1697 ALOGV("updateInternalTimers()");
1698 updatePlaybackTimer(false /* stopping */, "updateInternalTimers");
1699 updateRebufferingTimer(false /* stopping */, false /* exiting */);
1700}
1701
Chong Zhangefbb6192015-01-30 17:13:27 -08001702void NuPlayer::onPause() {
Ray Essick0d98c182017-11-09 13:42:42 -08001703
Ray Essickeda32522018-02-28 12:08:28 -08001704 updatePlaybackTimer(true /* stopping */, "onPause");
Ray Essick0d98c182017-11-09 13:42:42 -08001705
Chong Zhangefbb6192015-01-30 17:13:27 -08001706 if (mPaused) {
1707 return;
1708 }
1709 mPaused = true;
1710 if (mSource != NULL) {
1711 mSource->pause();
1712 } else {
1713 ALOGW("pause called when source is gone or not set");
1714 }
1715 if (mRenderer != NULL) {
1716 mRenderer->pause();
1717 } else {
1718 ALOGW("pause called when renderer is gone or not set");
1719 }
Ray Essickd4d00612017-01-03 09:36:27 -08001720
Chong Zhangefbb6192015-01-30 17:13:27 -08001721}
1722
Ronghua Wud7988b12014-10-03 15:19:10 -07001723bool NuPlayer::audioDecoderStillNeeded() {
1724 // Audio decoder is no longer needed if it's in shut/shutting down status.
1725 return ((mFlushingAudio != SHUT_DOWN) && (mFlushingAudio != SHUTTING_DOWN_DECODER));
1726}
1727
Andy Hung8d121d42014-10-03 09:53:53 -07001728void NuPlayer::handleFlushComplete(bool audio, bool isDecoder) {
1729 // We wait for both the decoder flush and the renderer flush to complete
1730 // before entering either the FLUSHED or the SHUTTING_DOWN_DECODER state.
1731
1732 mFlushComplete[audio][isDecoder] = true;
1733 if (!mFlushComplete[audio][!isDecoder]) {
1734 return;
1735 }
1736
1737 FlushStatus *state = audio ? &mFlushingAudio : &mFlushingVideo;
1738 switch (*state) {
1739 case FLUSHING_DECODER:
1740 {
1741 *state = FLUSHED;
Andy Hung8d121d42014-10-03 09:53:53 -07001742 break;
1743 }
1744
1745 case FLUSHING_DECODER_SHUTDOWN:
1746 {
1747 *state = SHUTTING_DOWN_DECODER;
1748
1749 ALOGV("initiating %s decoder shutdown", audio ? "audio" : "video");
Andy Hung8d121d42014-10-03 09:53:53 -07001750 getDecoder(audio)->initiateShutdown();
1751 break;
1752 }
1753
1754 default:
1755 // decoder flush completes only occur in a flushing state.
1756 LOG_ALWAYS_FATAL_IF(isDecoder, "decoder flush in invalid state %d", *state);
1757 break;
1758 }
1759}
1760
Andreas Huber3831a062010-12-21 10:22:33 -08001761void NuPlayer::finishFlushIfPossible() {
Wei Jia53904f32014-07-29 10:22:53 -07001762 if (mFlushingAudio != NONE && mFlushingAudio != FLUSHED
1763 && mFlushingAudio != SHUT_DOWN) {
Andreas Huber3831a062010-12-21 10:22:33 -08001764 return;
1765 }
1766
Wei Jia53904f32014-07-29 10:22:53 -07001767 if (mFlushingVideo != NONE && mFlushingVideo != FLUSHED
1768 && mFlushingVideo != SHUT_DOWN) {
Andreas Huber3831a062010-12-21 10:22:33 -08001769 return;
1770 }
1771
Steve Block3856b092011-10-20 11:56:00 +01001772 ALOGV("both audio and video are flushed now.");
Andreas Huber3831a062010-12-21 10:22:33 -08001773
Andreas Huber3831a062010-12-21 10:22:33 -08001774 mFlushingAudio = NONE;
1775 mFlushingVideo = NONE;
Andreas Huber3831a062010-12-21 10:22:33 -08001776
Andy Hung8d121d42014-10-03 09:53:53 -07001777 clearFlushComplete();
1778
Andreas Hubera1f8ab02012-11-30 10:53:22 -08001779 processDeferredActions();
Andreas Huber1aef2112011-01-04 14:01:29 -08001780}
1781
1782void NuPlayer::postScanSources() {
1783 if (mScanSourcesPending) {
1784 return;
1785 }
1786
Lajos Molnar1d15ab52015-03-04 16:46:34 -08001787 sp<AMessage> msg = new AMessage(kWhatScanSources, this);
Andreas Huber1aef2112011-01-04 14:01:29 -08001788 msg->setInt32("generation", mScanSourcesGeneration);
1789 msg->post();
1790
1791 mScanSourcesPending = true;
1792}
1793
Wei Jia41cd4632016-05-13 10:53:30 -07001794void NuPlayer::tryOpenAudioSinkForOffload(
1795 const sp<AMessage> &format, const sp<MetaData> &audioMeta, bool hasVideo) {
Andy Hung202bce12014-12-03 11:47:36 -08001796 // Note: This is called early in NuPlayer to determine whether offloading
1797 // is possible; otherwise the decoders call the renderer openAudioSink directly.
Andy Hung282a7e32014-08-14 15:56:34 -07001798
Andy Hung202bce12014-12-03 11:47:36 -08001799 status_t err = mRenderer->openAudioSink(
Dhananjay Kumarc387f2b2015-08-06 10:43:16 +05301800 format, true /* offloadOnly */, hasVideo,
1801 AUDIO_OUTPUT_FLAG_NONE, &mOffloadAudio, mSource->isStreaming());
Andy Hung202bce12014-12-03 11:47:36 -08001802 if (err != OK) {
1803 // Any failure we turn off mOffloadAudio.
1804 mOffloadAudio = false;
1805 } else if (mOffloadAudio) {
Chong Zhang3b9eb1f2014-10-15 17:05:08 -07001806 sendMetaDataToHal(mAudioSink, audioMeta);
Andy Hung282a7e32014-08-14 15:56:34 -07001807 }
1808}
1809
1810void NuPlayer::closeAudioSink() {
Chong Zhang3b9eb1f2014-10-15 17:05:08 -07001811 mRenderer->closeAudioSink();
Andy Hung282a7e32014-08-14 15:56:34 -07001812}
1813
Wei Jia5031b2f2016-02-25 11:19:31 -08001814void NuPlayer::restartAudio(
Wei Jiabf70feb2016-02-19 15:47:16 -08001815 int64_t currentPositionUs, bool forceNonOffload, bool needsToCreateAudioDecoder) {
Wei Jia355b2bb2018-04-09 16:36:23 -07001816 ALOGD("restartAudio timeUs(%lld), dontOffload(%d), createDecoder(%d)",
1817 (long long)currentPositionUs, forceNonOffload, needsToCreateAudioDecoder);
Wei Jiaa05f1e32016-03-25 16:31:22 -07001818 if (mAudioDecoder != NULL) {
1819 mAudioDecoder->pause();
1820 mAudioDecoder.clear();
Wei Jia686e8e52017-04-03 14:08:01 -07001821 mAudioDecoderError = false;
Wei Jiaa05f1e32016-03-25 16:31:22 -07001822 ++mAudioDecoderGeneration;
1823 }
Wei Jiabf70feb2016-02-19 15:47:16 -08001824 if (mFlushingAudio == FLUSHING_DECODER) {
1825 mFlushComplete[1 /* audio */][1 /* isDecoder */] = true;
1826 mFlushingAudio = FLUSHED;
1827 finishFlushIfPossible();
1828 } else if (mFlushingAudio == FLUSHING_DECODER_SHUTDOWN
1829 || mFlushingAudio == SHUTTING_DOWN_DECODER) {
1830 mFlushComplete[1 /* audio */][1 /* isDecoder */] = true;
1831 mFlushingAudio = SHUT_DOWN;
1832 finishFlushIfPossible();
1833 needsToCreateAudioDecoder = false;
1834 }
1835 if (mRenderer == NULL) {
1836 return;
1837 }
1838 closeAudioSink();
1839 mRenderer->flush(true /* audio */, false /* notifyComplete */);
1840 if (mVideoDecoder != NULL) {
1841 mRenderer->flush(false /* audio */, false /* notifyComplete */);
1842 }
1843
Wei Jiac5de0912016-11-18 10:22:14 -08001844 performSeek(currentPositionUs, MediaPlayerSeekMode::SEEK_PREVIOUS_SYNC /* mode */);
Wei Jiabf70feb2016-02-19 15:47:16 -08001845
1846 if (forceNonOffload) {
1847 mRenderer->signalDisableOffloadAudio();
1848 mOffloadAudio = false;
1849 }
1850 if (needsToCreateAudioDecoder) {
Wei Jiaa05f1e32016-03-25 16:31:22 -07001851 instantiateDecoder(true /* audio */, &mAudioDecoder, !forceNonOffload);
Wei Jiabf70feb2016-02-19 15:47:16 -08001852 }
1853}
1854
Wei Jia41cd4632016-05-13 10:53:30 -07001855void NuPlayer::determineAudioModeChange(const sp<AMessage> &audioFormat) {
Wei Jiae4d18c72015-07-13 17:58:11 -07001856 if (mSource == NULL || mAudioSink == NULL) {
1857 return;
1858 }
1859
1860 if (mRenderer == NULL) {
1861 ALOGW("No renderer can be used to determine audio mode. Use non-offload for safety.");
1862 mOffloadAudio = false;
1863 return;
1864 }
1865
1866 sp<MetaData> audioMeta = mSource->getFormatMeta(true /* audio */);
1867 sp<AMessage> videoFormat = mSource->getFormat(false /* audio */);
1868 audio_stream_type_t streamType = mAudioSink->getAudioStreamType();
1869 const bool hasVideo = (videoFormat != NULL);
Hassan Shojaniacefac142017-02-06 21:02:02 -08001870 bool canOffload = canOffloadStream(
Wei Jiabf70feb2016-02-19 15:47:16 -08001871 audioMeta, hasVideo, mSource->isStreaming(), streamType)
1872 && (mPlaybackSettings.mSpeed == 1.f && mPlaybackSettings.mPitch == 1.f);
Hassan Shojaniacefac142017-02-06 21:02:02 -08001873
1874 // Modular DRM: Disabling audio offload if the source is protected
1875 if (canOffload && mIsDrmProtected) {
1876 canOffload = false;
1877 ALOGV("determineAudioModeChange: Disabling mOffloadAudio b/c the source is protected.");
1878 }
1879
Wei Jiae4d18c72015-07-13 17:58:11 -07001880 if (canOffload) {
1881 if (!mOffloadAudio) {
1882 mRenderer->signalEnableOffloadAudio();
1883 }
1884 // open audio sink early under offload mode.
Wei Jia41cd4632016-05-13 10:53:30 -07001885 tryOpenAudioSinkForOffload(audioFormat, audioMeta, hasVideo);
Wei Jiae4d18c72015-07-13 17:58:11 -07001886 } else {
Robert Shihe1d70192015-07-23 17:54:13 -07001887 if (mOffloadAudio) {
1888 mRenderer->signalDisableOffloadAudio();
1889 mOffloadAudio = false;
1890 }
Wei Jiae4d18c72015-07-13 17:58:11 -07001891 }
1892}
1893
Wei Jiaa05f1e32016-03-25 16:31:22 -07001894status_t NuPlayer::instantiateDecoder(
1895 bool audio, sp<DecoderBase> *decoder, bool checkAudioModeChange) {
Wei Jia566da802015-08-27 13:59:30 -07001896 // The audio decoder could be cleared by tear down. If still in shut down
1897 // process, no need to create a new audio decoder.
1898 if (*decoder != NULL || (audio && mFlushingAudio == SHUT_DOWN)) {
Andreas Huberf9334412010-12-15 15:17:42 -08001899 return OK;
1900 }
1901
Andreas Huber84066782011-08-16 09:34:26 -07001902 sp<AMessage> format = mSource->getFormat(audio);
Andreas Huberf9334412010-12-15 15:17:42 -08001903
Andreas Huber84066782011-08-16 09:34:26 -07001904 if (format == NULL) {
Robert Shih7350b052015-10-01 15:50:14 -07001905 return UNKNOWN_ERROR;
1906 } else {
1907 status_t err;
1908 if (format->findInt32("err", &err) && err) {
1909 return err;
1910 }
Andreas Huberf9334412010-12-15 15:17:42 -08001911 }
1912
Ronghua Wu8db88132015-04-22 13:51:35 -07001913 format->setInt32("priority", 0 /* realtime */);
1914
Andreas Huber3fe62152011-09-16 15:09:22 -07001915 if (!audio) {
Andreas Huber84066782011-08-16 09:34:26 -07001916 AString mime;
1917 CHECK(format->findString("mime", &mime));
Chong Zhanga7fa1d92014-06-11 14:49:23 -07001918
Lajos Molnar1d15ab52015-03-04 16:46:34 -08001919 sp<AMessage> ccNotify = new AMessage(kWhatClosedCaptionNotify, this);
Chong Zhang341ab6e2015-02-04 13:37:18 -08001920 if (mCCDecoder == NULL) {
1921 mCCDecoder = new CCDecoder(ccNotify);
1922 }
Lajos Molnar09524832014-07-17 14:29:51 -07001923
1924 if (mSourceFlags & Source::FLAG_SECURE) {
1925 format->setInt32("secure", true);
1926 }
Chong Zhang17134602015-01-07 16:14:34 -08001927
1928 if (mSourceFlags & Source::FLAG_PROTECTED) {
1929 format->setInt32("protected", true);
1930 }
Ronghua Wu8db88132015-04-22 13:51:35 -07001931
Ronghua Wuc8a70d32015-04-29 16:26:34 -07001932 float rate = getFrameRate();
1933 if (rate > 0) {
Lajos Molnar3a474aa2015-04-24 17:10:07 -07001934 format->setFloat("operating-rate", rate * mPlaybackSettings.mSpeed);
Ronghua Wu8db88132015-04-22 13:51:35 -07001935 }
Andreas Huber3fe62152011-09-16 15:09:22 -07001936 }
1937
Wei Jiabc2fb722014-07-08 16:37:57 -07001938 if (audio) {
Lajos Molnar1d15ab52015-03-04 16:46:34 -08001939 sp<AMessage> notify = new AMessage(kWhatAudioNotify, this);
Wei Jia88703c32014-08-06 11:24:07 -07001940 ++mAudioDecoderGeneration;
1941 notify->setInt32("generation", mAudioDecoderGeneration);
1942
Wei Jiaa05f1e32016-03-25 16:31:22 -07001943 if (checkAudioModeChange) {
Wei Jia41cd4632016-05-13 10:53:30 -07001944 determineAudioModeChange(format);
Wei Jiaa05f1e32016-03-25 16:31:22 -07001945 }
Wei Jiabc2fb722014-07-08 16:37:57 -07001946 if (mOffloadAudio) {
Wei Jia14532f22015-12-29 11:28:15 -08001947 mSource->setOffloadAudio(true /* offload */);
1948
Haynes Mathew George8b635332015-03-30 17:59:47 -07001949 const bool hasVideo = (mSource->getFormat(false /*audio */) != NULL);
1950 format->setInt32("has-video", hasVideo);
Wei Jiac6cfd702014-11-11 16:33:20 -08001951 *decoder = new DecoderPassThrough(notify, mSource, mRenderer);
Hassan Shojaniacefac142017-02-06 21:02:02 -08001952 ALOGV("instantiateDecoder audio DecoderPassThrough hasVideo: %d", hasVideo);
Wei Jiabc2fb722014-07-08 16:37:57 -07001953 } else {
Wei Jia14532f22015-12-29 11:28:15 -08001954 mSource->setOffloadAudio(false /* offload */);
1955
Wei Jiaf2ae3e12016-10-27 17:10:59 -07001956 *decoder = new Decoder(notify, mSource, mPID, mUID, mRenderer);
Hassan Shojaniacefac142017-02-06 21:02:02 -08001957 ALOGV("instantiateDecoder audio Decoder");
Wei Jiabc2fb722014-07-08 16:37:57 -07001958 }
Wei Jia686e8e52017-04-03 14:08:01 -07001959 mAudioDecoderError = false;
Wei Jiabc2fb722014-07-08 16:37:57 -07001960 } else {
Lajos Molnar1d15ab52015-03-04 16:46:34 -08001961 sp<AMessage> notify = new AMessage(kWhatVideoNotify, this);
Wei Jia88703c32014-08-06 11:24:07 -07001962 ++mVideoDecoderGeneration;
1963 notify->setInt32("generation", mVideoDecoderGeneration);
1964
Chong Zhang7137ec72014-11-12 16:41:05 -08001965 *decoder = new Decoder(
Wei Jiaf2ae3e12016-10-27 17:10:59 -07001966 notify, mSource, mPID, mUID, mRenderer, mSurface, mCCDecoder);
Wei Jia686e8e52017-04-03 14:08:01 -07001967 mVideoDecoderError = false;
Lajos Molnard9fd6312014-11-06 11:00:00 -08001968
1969 // enable FRC if high-quality AV sync is requested, even if not
Lajos Molnar1de1e252015-04-30 18:18:34 -07001970 // directly queuing to display, as this will even improve textureview
Lajos Molnard9fd6312014-11-06 11:00:00 -08001971 // playback.
1972 {
Marco Nelissen96626b72016-12-01 13:58:59 -08001973 if (property_get_bool("persist.sys.media.avsync", false)) {
Lajos Molnard9fd6312014-11-06 11:00:00 -08001974 format->setInt32("auto-frc", 1);
1975 }
1976 }
Wei Jiabc2fb722014-07-08 16:37:57 -07001977 }
Lajos Molnar1cd13982014-01-17 15:12:51 -08001978 (*decoder)->init();
Hassan Shojaniacefac142017-02-06 21:02:02 -08001979
1980 // Modular DRM
1981 if (mIsDrmProtected) {
1982 format->setPointer("crypto", mCrypto.get());
1983 ALOGV("instantiateDecoder: mCrypto: %p (%d) isSecure: %d", mCrypto.get(),
1984 (mCrypto != NULL ? mCrypto->getStrongCount() : 0),
1985 (mSourceFlags & Source::FLAG_SECURE) != 0);
1986 }
1987
Andreas Huber84066782011-08-16 09:34:26 -07001988 (*decoder)->configure(format);
Andreas Huberf9334412010-12-15 15:17:42 -08001989
Praveen Chavanbbaa1442016-04-08 13:33:49 -07001990 if (!audio) {
1991 sp<AMessage> params = new AMessage();
1992 float rate = getFrameRate();
1993 if (rate > 0) {
1994 params->setFloat("frame-rate-total", rate);
1995 }
1996
1997 sp<MetaData> fileMeta = getFileMeta();
1998 if (fileMeta != NULL) {
1999 int32_t videoTemporalLayerCount;
2000 if (fileMeta->findInt32(kKeyTemporalLayerCount, &videoTemporalLayerCount)
2001 && videoTemporalLayerCount > 0) {
2002 params->setInt32("temporal-layer-count", videoTemporalLayerCount);
2003 }
2004 }
2005
2006 if (params->countEntries() > 0) {
2007 (*decoder)->setParameters(params);
2008 }
2009 }
Andreas Huberf9334412010-12-15 15:17:42 -08002010 return OK;
2011}
2012
Chong Zhangced1c2f2014-08-08 15:22:35 -07002013void NuPlayer::updateVideoSize(
2014 const sp<AMessage> &inputFormat,
2015 const sp<AMessage> &outputFormat) {
2016 if (inputFormat == NULL) {
2017 ALOGW("Unknown video size, reporting 0x0!");
2018 notifyListener(MEDIA_SET_VIDEO_SIZE, 0, 0);
2019 return;
2020 }
Wei Jia9737d342016-12-28 14:59:59 -08002021 int32_t err = OK;
2022 inputFormat->findInt32("err", &err);
2023 if (err == -EWOULDBLOCK) {
2024 ALOGW("Video meta is not available yet!");
2025 return;
2026 }
2027 if (err != OK) {
2028 ALOGW("Something is wrong with video meta!");
2029 return;
2030 }
Chong Zhangced1c2f2014-08-08 15:22:35 -07002031
2032 int32_t displayWidth, displayHeight;
Chong Zhangced1c2f2014-08-08 15:22:35 -07002033 if (outputFormat != NULL) {
2034 int32_t width, height;
2035 CHECK(outputFormat->findInt32("width", &width));
2036 CHECK(outputFormat->findInt32("height", &height));
2037
2038 int32_t cropLeft, cropTop, cropRight, cropBottom;
2039 CHECK(outputFormat->findRect(
2040 "crop",
2041 &cropLeft, &cropTop, &cropRight, &cropBottom));
2042
2043 displayWidth = cropRight - cropLeft + 1;
2044 displayHeight = cropBottom - cropTop + 1;
2045
2046 ALOGV("Video output format changed to %d x %d "
2047 "(crop: %d x %d @ (%d, %d))",
2048 width, height,
2049 displayWidth,
2050 displayHeight,
2051 cropLeft, cropTop);
2052 } else {
2053 CHECK(inputFormat->findInt32("width", &displayWidth));
2054 CHECK(inputFormat->findInt32("height", &displayHeight));
2055
2056 ALOGV("Video input format %d x %d", displayWidth, displayHeight);
2057 }
2058
2059 // Take into account sample aspect ratio if necessary:
2060 int32_t sarWidth, sarHeight;
2061 if (inputFormat->findInt32("sar-width", &sarWidth)
Wei Jia095bc252017-01-27 10:16:16 -08002062 && inputFormat->findInt32("sar-height", &sarHeight)
2063 && sarWidth > 0 && sarHeight > 0) {
Chong Zhangced1c2f2014-08-08 15:22:35 -07002064 ALOGV("Sample aspect ratio %d : %d", sarWidth, sarHeight);
2065
2066 displayWidth = (displayWidth * sarWidth) / sarHeight;
2067
2068 ALOGV("display dimensions %d x %d", displayWidth, displayHeight);
Wei Jiadf6c6af2016-09-29 11:27:15 -07002069 } else {
2070 int32_t width, height;
2071 if (inputFormat->findInt32("display-width", &width)
2072 && inputFormat->findInt32("display-height", &height)
2073 && width > 0 && height > 0
2074 && displayWidth > 0 && displayHeight > 0) {
2075 if (displayHeight * (int64_t)width / height > (int64_t)displayWidth) {
2076 displayHeight = (int32_t)(displayWidth * (int64_t)height / width);
2077 } else {
2078 displayWidth = (int32_t)(displayHeight * (int64_t)width / height);
2079 }
2080 ALOGV("Video display width and height are overridden to %d x %d",
2081 displayWidth, displayHeight);
2082 }
Chong Zhangced1c2f2014-08-08 15:22:35 -07002083 }
2084
2085 int32_t rotationDegrees;
2086 if (!inputFormat->findInt32("rotation-degrees", &rotationDegrees)) {
2087 rotationDegrees = 0;
2088 }
2089
2090 if (rotationDegrees == 90 || rotationDegrees == 270) {
2091 int32_t tmp = displayWidth;
2092 displayWidth = displayHeight;
2093 displayHeight = tmp;
2094 }
2095
2096 notifyListener(
2097 MEDIA_SET_VIDEO_SIZE,
2098 displayWidth,
2099 displayHeight);
2100}
2101
Chong Zhangdcb89b32013-08-06 09:44:47 -07002102void NuPlayer::notifyListener(int msg, int ext1, int ext2, const Parcel *in) {
Andreas Huber43c3e6c2011-01-05 12:17:08 -08002103 if (mDriver == NULL) {
Andreas Huberf9334412010-12-15 15:17:42 -08002104 return;
2105 }
2106
Andreas Huber43c3e6c2011-01-05 12:17:08 -08002107 sp<NuPlayerDriver> driver = mDriver.promote();
Andreas Huberf9334412010-12-15 15:17:42 -08002108
Andreas Huber43c3e6c2011-01-05 12:17:08 -08002109 if (driver == NULL) {
Andreas Huberf9334412010-12-15 15:17:42 -08002110 return;
2111 }
2112
Chong Zhangdcb89b32013-08-06 09:44:47 -07002113 driver->notifyListener(msg, ext1, ext2, in);
Andreas Huberf9334412010-12-15 15:17:42 -08002114}
2115
Chong Zhang7137ec72014-11-12 16:41:05 -08002116void NuPlayer::flushDecoder(bool audio, bool needShutdown) {
Andreas Huber14f76722013-01-15 09:04:18 -08002117 ALOGV("[%s] flushDecoder needShutdown=%d",
2118 audio ? "audio" : "video", needShutdown);
2119
Chong Zhang7137ec72014-11-12 16:41:05 -08002120 const sp<DecoderBase> &decoder = getDecoder(audio);
Lajos Molnar87603c02014-08-20 19:25:30 -07002121 if (decoder == NULL) {
Steve Blockdf64d152012-01-04 20:05:49 +00002122 ALOGI("flushDecoder %s without decoder present",
Andreas Huber6e3d3112011-11-28 12:36:11 -08002123 audio ? "audio" : "video");
Lajos Molnar87603c02014-08-20 19:25:30 -07002124 return;
Andreas Huber6e3d3112011-11-28 12:36:11 -08002125 }
2126
Andreas Huber1aef2112011-01-04 14:01:29 -08002127 // Make sure we don't continue to scan sources until we finish flushing.
2128 ++mScanSourcesGeneration;
Chong Zhang3b032b32015-04-17 15:49:06 -07002129 if (mScanSourcesPending) {
Toshikazu Saitocbcbb792015-09-15 14:53:01 +09002130 if (!needShutdown) {
2131 mDeferredActions.push_back(
2132 new SimpleAction(&NuPlayer::performScanSources));
2133 }
Chong Zhang3b032b32015-04-17 15:49:06 -07002134 mScanSourcesPending = false;
2135 }
Andreas Huber1aef2112011-01-04 14:01:29 -08002136
Chong Zhang7137ec72014-11-12 16:41:05 -08002137 decoder->signalFlush();
Andreas Huber1aef2112011-01-04 14:01:29 -08002138
2139 FlushStatus newStatus =
2140 needShutdown ? FLUSHING_DECODER_SHUTDOWN : FLUSHING_DECODER;
2141
Lajos Molnarfcd3e942015-03-31 10:06:48 -07002142 mFlushComplete[audio][false /* isDecoder */] = (mRenderer == NULL);
Andy Hung8d121d42014-10-03 09:53:53 -07002143 mFlushComplete[audio][true /* isDecoder */] = false;
Andreas Huber1aef2112011-01-04 14:01:29 -08002144 if (audio) {
Wei Jia53904f32014-07-29 10:22:53 -07002145 ALOGE_IF(mFlushingAudio != NONE,
2146 "audio flushDecoder() is called in state %d", mFlushingAudio);
Andreas Huber1aef2112011-01-04 14:01:29 -08002147 mFlushingAudio = newStatus;
Andreas Huber1aef2112011-01-04 14:01:29 -08002148 } else {
Wei Jia53904f32014-07-29 10:22:53 -07002149 ALOGE_IF(mFlushingVideo != NONE,
2150 "video flushDecoder() is called in state %d", mFlushingVideo);
Andreas Huber1aef2112011-01-04 14:01:29 -08002151 mFlushingVideo = newStatus;
Andreas Huber1aef2112011-01-04 14:01:29 -08002152 }
2153}
2154
Chong Zhangced1c2f2014-08-08 15:22:35 -07002155void NuPlayer::queueDecoderShutdown(
2156 bool audio, bool video, const sp<AMessage> &reply) {
2157 ALOGI("queueDecoderShutdown audio=%d, video=%d", audio, video);
Andreas Huber84066782011-08-16 09:34:26 -07002158
Chong Zhangced1c2f2014-08-08 15:22:35 -07002159 mDeferredActions.push_back(
Wei Jiafef808d2014-10-31 17:57:05 -07002160 new FlushDecoderAction(
2161 audio ? FLUSH_CMD_SHUTDOWN : FLUSH_CMD_NONE,
2162 video ? FLUSH_CMD_SHUTDOWN : FLUSH_CMD_NONE));
Andreas Huber84066782011-08-16 09:34:26 -07002163
Chong Zhangced1c2f2014-08-08 15:22:35 -07002164 mDeferredActions.push_back(
2165 new SimpleAction(&NuPlayer::performScanSources));
Andreas Huber84066782011-08-16 09:34:26 -07002166
Chong Zhangced1c2f2014-08-08 15:22:35 -07002167 mDeferredActions.push_back(new PostMessageAction(reply));
2168
2169 processDeferredActions();
Andreas Huber84066782011-08-16 09:34:26 -07002170}
2171
James Dong0d268a32012-08-31 12:18:27 -07002172status_t NuPlayer::setVideoScalingMode(int32_t mode) {
2173 mVideoScalingMode = mode;
Lajos Molnar1de1e252015-04-30 18:18:34 -07002174 if (mSurface != NULL) {
2175 status_t ret = native_window_set_scaling_mode(mSurface.get(), mVideoScalingMode);
James Dong0d268a32012-08-31 12:18:27 -07002176 if (ret != OK) {
2177 ALOGE("Failed to set scaling mode (%d): %s",
2178 -ret, strerror(-ret));
2179 return ret;
2180 }
2181 }
2182 return OK;
2183}
2184
Chong Zhangdcb89b32013-08-06 09:44:47 -07002185status_t NuPlayer::getTrackInfo(Parcel* reply) const {
Lajos Molnar1d15ab52015-03-04 16:46:34 -08002186 sp<AMessage> msg = new AMessage(kWhatGetTrackInfo, this);
Chong Zhangdcb89b32013-08-06 09:44:47 -07002187 msg->setPointer("reply", reply);
2188
2189 sp<AMessage> response;
2190 status_t err = msg->postAndAwaitResponse(&response);
2191 return err;
2192}
2193
Robert Shih7c4f0d72014-07-09 18:53:31 -07002194status_t NuPlayer::getSelectedTrack(int32_t type, Parcel* reply) const {
Lajos Molnar1d15ab52015-03-04 16:46:34 -08002195 sp<AMessage> msg = new AMessage(kWhatGetSelectedTrack, this);
Robert Shih7c4f0d72014-07-09 18:53:31 -07002196 msg->setPointer("reply", reply);
2197 msg->setInt32("type", type);
2198
2199 sp<AMessage> response;
2200 status_t err = msg->postAndAwaitResponse(&response);
2201 if (err == OK && response != NULL) {
2202 CHECK(response->findInt32("err", &err));
2203 }
2204 return err;
2205}
2206
Robert Shih6ffb1fd2014-10-29 16:24:32 -07002207status_t NuPlayer::selectTrack(size_t trackIndex, bool select, int64_t timeUs) {
Lajos Molnar1d15ab52015-03-04 16:46:34 -08002208 sp<AMessage> msg = new AMessage(kWhatSelectTrack, this);
Chong Zhangdcb89b32013-08-06 09:44:47 -07002209 msg->setSize("trackIndex", trackIndex);
2210 msg->setInt32("select", select);
Robert Shih6ffb1fd2014-10-29 16:24:32 -07002211 msg->setInt64("timeUs", timeUs);
Chong Zhangdcb89b32013-08-06 09:44:47 -07002212
2213 sp<AMessage> response;
2214 status_t err = msg->postAndAwaitResponse(&response);
2215
Chong Zhang404fced2014-06-11 14:45:31 -07002216 if (err != OK) {
2217 return err;
2218 }
2219
2220 if (!response->findInt32("err", &err)) {
2221 err = OK;
2222 }
2223
Chong Zhangdcb89b32013-08-06 09:44:47 -07002224 return err;
2225}
2226
Ronghua Wua73d9e02014-10-08 15:13:29 -07002227status_t NuPlayer::getCurrentPosition(int64_t *mediaUs) {
2228 sp<Renderer> renderer = mRenderer;
2229 if (renderer == NULL) {
2230 return NO_INIT;
2231 }
2232
2233 return renderer->getCurrentPosition(mediaUs);
2234}
2235
Ray Essickffc941f2018-05-18 11:08:37 -07002236void NuPlayer::getStats(Vector<sp<AMessage> > *trackStats) {
2237 CHECK(trackStats != NULL);
Praveen Chavane1e5d7a2015-05-19 19:09:48 -07002238
Ray Essickffc941f2018-05-18 11:08:37 -07002239 ALOGV("NuPlayer::getStats()");
2240 sp<AMessage> msg = new AMessage(kWhatGetStats, this);
2241 msg->setPointer("trackstats", trackStats);
2242
2243 sp<AMessage> response;
2244 (void) msg->postAndAwaitResponse(&response);
2245 // response is for synchronization, ignore contents
Ronghua Wua73d9e02014-10-08 15:13:29 -07002246}
2247
Marco Nelissenf0b72b52014-09-16 15:43:44 -07002248sp<MetaData> NuPlayer::getFileMeta() {
2249 return mSource->getFileFormatMeta();
2250}
2251
Ronghua Wuc8a70d32015-04-29 16:26:34 -07002252float NuPlayer::getFrameRate() {
2253 sp<MetaData> meta = mSource->getFormatMeta(false /* audio */);
2254 if (meta == NULL) {
2255 return 0;
2256 }
2257 int32_t rate;
2258 if (!meta->findInt32(kKeyFrameRate, &rate)) {
2259 // fall back to try file meta
2260 sp<MetaData> fileMeta = getFileMeta();
2261 if (fileMeta == NULL) {
2262 ALOGW("source has video meta but not file meta");
2263 return -1;
2264 }
2265 int32_t fileMetaRate;
2266 if (!fileMeta->findInt32(kKeyFrameRate, &fileMetaRate)) {
2267 return -1;
2268 }
2269 return fileMetaRate;
2270 }
2271 return rate;
2272}
2273
Andreas Huberb7c8e912012-11-27 15:02:53 -08002274void NuPlayer::schedulePollDuration() {
Lajos Molnar1d15ab52015-03-04 16:46:34 -08002275 sp<AMessage> msg = new AMessage(kWhatPollDuration, this);
Andreas Huberb7c8e912012-11-27 15:02:53 -08002276 msg->setInt32("generation", mPollDurationGeneration);
2277 msg->post();
2278}
2279
2280void NuPlayer::cancelPollDuration() {
2281 ++mPollDurationGeneration;
2282}
2283
Andreas Hubera1f8ab02012-11-30 10:53:22 -08002284void NuPlayer::processDeferredActions() {
2285 while (!mDeferredActions.empty()) {
2286 // We won't execute any deferred actions until we're no longer in
2287 // an intermediate state, i.e. one more more decoders are currently
2288 // flushing or shutting down.
2289
Andreas Hubera1f8ab02012-11-30 10:53:22 -08002290 if (mFlushingAudio != NONE || mFlushingVideo != NONE) {
2291 // We're currently flushing, postpone the reset until that's
2292 // completed.
2293
2294 ALOGV("postponing action mFlushingAudio=%d, mFlushingVideo=%d",
2295 mFlushingAudio, mFlushingVideo);
2296
2297 break;
2298 }
2299
2300 sp<Action> action = *mDeferredActions.begin();
2301 mDeferredActions.erase(mDeferredActions.begin());
2302
2303 action->execute(this);
2304 }
2305}
2306
Wei Jiac5de0912016-11-18 10:22:14 -08002307void NuPlayer::performSeek(int64_t seekTimeUs, MediaPlayerSeekMode mode) {
2308 ALOGV("performSeek seekTimeUs=%lld us (%.2f secs), mode=%d",
2309 (long long)seekTimeUs, seekTimeUs / 1E6, mode);
Andreas Hubera1f8ab02012-11-30 10:53:22 -08002310
Andy Hungadf34bf2014-09-03 18:22:22 -07002311 if (mSource == NULL) {
2312 // This happens when reset occurs right before the loop mode
2313 // asynchronously seeks to the start of the stream.
2314 LOG_ALWAYS_FATAL_IF(mAudioDecoder != NULL || mVideoDecoder != NULL,
2315 "mSource is NULL and decoders not NULL audio(%p) video(%p)",
2316 mAudioDecoder.get(), mVideoDecoder.get());
2317 return;
2318 }
Robert Shih1a5c8592015-08-04 18:07:44 -07002319 mPreviousSeekTimeUs = seekTimeUs;
Wei Jiac5de0912016-11-18 10:22:14 -08002320 mSource->seekTo(seekTimeUs, mode);
Robert Shihd3b0bbb2014-07-23 15:00:25 -07002321 ++mTimedTextGeneration;
Andreas Hubera1f8ab02012-11-30 10:53:22 -08002322
Andreas Hubera1f8ab02012-11-30 10:53:22 -08002323 // everything's flushed, continue playback.
2324}
2325
Wei Jiafef808d2014-10-31 17:57:05 -07002326void NuPlayer::performDecoderFlush(FlushCommand audio, FlushCommand video) {
2327 ALOGV("performDecoderFlush audio=%d, video=%d", audio, video);
Andreas Hubera1f8ab02012-11-30 10:53:22 -08002328
Wei Jiafef808d2014-10-31 17:57:05 -07002329 if ((audio == FLUSH_CMD_NONE || mAudioDecoder == NULL)
2330 && (video == FLUSH_CMD_NONE || mVideoDecoder == NULL)) {
Andreas Hubera1f8ab02012-11-30 10:53:22 -08002331 return;
2332 }
2333
Wei Jiafef808d2014-10-31 17:57:05 -07002334 if (audio != FLUSH_CMD_NONE && mAudioDecoder != NULL) {
2335 flushDecoder(true /* audio */, (audio == FLUSH_CMD_SHUTDOWN));
Andreas Hubera1f8ab02012-11-30 10:53:22 -08002336 }
2337
Wei Jiafef808d2014-10-31 17:57:05 -07002338 if (video != FLUSH_CMD_NONE && mVideoDecoder != NULL) {
2339 flushDecoder(false /* audio */, (video == FLUSH_CMD_SHUTDOWN));
Andreas Hubera1f8ab02012-11-30 10:53:22 -08002340 }
2341}
2342
2343void NuPlayer::performReset() {
2344 ALOGV("performReset");
2345
2346 CHECK(mAudioDecoder == NULL);
2347 CHECK(mVideoDecoder == NULL);
2348
Ray Essickeda32522018-02-28 12:08:28 -08002349 updatePlaybackTimer(true /* stopping */, "performReset");
2350 updateRebufferingTimer(true /* stopping */, true /* exiting */);
Ray Essick0d98c182017-11-09 13:42:42 -08002351
Andreas Hubera1f8ab02012-11-30 10:53:22 -08002352 cancelPollDuration();
2353
2354 ++mScanSourcesGeneration;
2355 mScanSourcesPending = false;
2356
Lajos Molnar09524832014-07-17 14:29:51 -07002357 if (mRendererLooper != NULL) {
2358 if (mRenderer != NULL) {
2359 mRendererLooper->unregisterHandler(mRenderer->id());
2360 }
2361 mRendererLooper->stop();
2362 mRendererLooper.clear();
2363 }
Andreas Hubera1f8ab02012-11-30 10:53:22 -08002364 mRenderer.clear();
Wei Jia57568df2014-09-22 10:16:29 -07002365 ++mRendererGeneration;
Andreas Hubera1f8ab02012-11-30 10:53:22 -08002366
2367 if (mSource != NULL) {
2368 mSource->stop();
Andreas Huberb5f25f02013-02-05 10:14:26 -08002369
Wei Jiac45a4b22016-04-15 15:30:23 -07002370 Mutex::Autolock autoLock(mSourceLock);
Andreas Hubera1f8ab02012-11-30 10:53:22 -08002371 mSource.clear();
2372 }
2373
2374 if (mDriver != NULL) {
2375 sp<NuPlayerDriver> driver = mDriver.promote();
2376 if (driver != NULL) {
2377 driver->notifyResetComplete();
2378 }
2379 }
Andreas Huber57a339c2012-12-03 11:18:00 -08002380
2381 mStarted = false;
Wei Jia8a092d32016-06-03 14:57:24 -07002382 mPrepared = false;
Ronghua Wu64c2d172015-10-07 16:52:19 -07002383 mResetting = false;
Robert Shih0c61a0d2015-07-06 15:09:10 -07002384 mSourceStarted = false;
Hassan Shojaniacefac142017-02-06 21:02:02 -08002385
2386 // Modular DRM
2387 if (mCrypto != NULL) {
2388 // decoders will be flushed before this so their mCrypto would go away on their own
2389 // TODO change to ALOGV
2390 ALOGD("performReset mCrypto: %p (%d)", mCrypto.get(),
2391 (mCrypto != NULL ? mCrypto->getStrongCount() : 0));
2392 mCrypto.clear();
2393 }
2394 mIsDrmProtected = false;
Andreas Hubera1f8ab02012-11-30 10:53:22 -08002395}
2396
2397void NuPlayer::performScanSources() {
2398 ALOGV("performScanSources");
2399
Andreas Huber57a339c2012-12-03 11:18:00 -08002400 if (!mStarted) {
2401 return;
2402 }
2403
Andreas Hubera1f8ab02012-11-30 10:53:22 -08002404 if (mAudioDecoder == NULL || mVideoDecoder == NULL) {
2405 postScanSources();
2406 }
2407}
2408
Lajos Molnar1de1e252015-04-30 18:18:34 -07002409void NuPlayer::performSetSurface(const sp<Surface> &surface) {
Andreas Huber57a339c2012-12-03 11:18:00 -08002410 ALOGV("performSetSurface");
2411
Lajos Molnar1de1e252015-04-30 18:18:34 -07002412 mSurface = surface;
Andreas Huber57a339c2012-12-03 11:18:00 -08002413
2414 // XXX - ignore error from setVideoScalingMode for now
2415 setVideoScalingMode(mVideoScalingMode);
Chong Zhang13d6faa2014-08-22 15:35:28 -07002416
2417 if (mDriver != NULL) {
2418 sp<NuPlayerDriver> driver = mDriver.promote();
2419 if (driver != NULL) {
2420 driver->notifySetSurfaceComplete();
2421 }
2422 }
Andreas Huber57a339c2012-12-03 11:18:00 -08002423}
2424
Chong Zhangf8d71772014-11-26 15:08:34 -08002425void NuPlayer::performResumeDecoders(bool needNotify) {
2426 if (needNotify) {
2427 mResumePending = true;
2428 if (mVideoDecoder == NULL) {
2429 // if audio-only, we can notify seek complete now,
2430 // as the resume operation will be relatively fast.
2431 finishResume();
2432 }
2433 }
2434
Chong Zhang7137ec72014-11-12 16:41:05 -08002435 if (mVideoDecoder != NULL) {
Chong Zhangf8d71772014-11-26 15:08:34 -08002436 // When there is continuous seek, MediaPlayer will cache the seek
2437 // position, and send down new seek request when previous seek is
2438 // complete. Let's wait for at least one video output frame before
2439 // notifying seek complete, so that the video thumbnail gets updated
2440 // when seekbar is dragged.
2441 mVideoDecoder->signalResume(needNotify);
Chong Zhang7137ec72014-11-12 16:41:05 -08002442 }
2443
2444 if (mAudioDecoder != NULL) {
Chong Zhangf8d71772014-11-26 15:08:34 -08002445 mAudioDecoder->signalResume(false /* needNotify */);
2446 }
2447}
2448
2449void NuPlayer::finishResume() {
2450 if (mResumePending) {
2451 mResumePending = false;
Wei Jia1061c9c2015-05-19 16:02:17 -07002452 notifyDriverSeekComplete();
2453 }
2454}
2455
2456void NuPlayer::notifyDriverSeekComplete() {
2457 if (mDriver != NULL) {
2458 sp<NuPlayerDriver> driver = mDriver.promote();
2459 if (driver != NULL) {
2460 driver->notifySeekComplete();
Chong Zhangf8d71772014-11-26 15:08:34 -08002461 }
Chong Zhang7137ec72014-11-12 16:41:05 -08002462 }
2463}
2464
Andreas Huber9575c962013-02-05 13:59:56 -08002465void NuPlayer::onSourceNotify(const sp<AMessage> &msg) {
2466 int32_t what;
2467 CHECK(msg->findInt32("what", &what));
2468
2469 switch (what) {
Lajos Molnarfcd3e942015-03-31 10:06:48 -07002470 case Source::kWhatInstantiateSecureDecoders:
2471 {
2472 if (mSource == NULL) {
2473 // This is a stale notification from a source that was
2474 // asynchronously preparing when the client called reset().
2475 // We handled the reset, the source is gone.
2476 break;
2477 }
2478
2479 sp<AMessage> reply;
2480 CHECK(msg->findMessage("reply", &reply));
2481 status_t err = onInstantiateSecureDecoders();
2482 reply->setInt32("err", err);
2483 reply->post();
2484 break;
2485 }
2486
Andreas Huber9575c962013-02-05 13:59:56 -08002487 case Source::kWhatPrepared:
2488 {
Hassan Shojaniacefac142017-02-06 21:02:02 -08002489 ALOGV("NuPlayer::onSourceNotify Source::kWhatPrepared source: %p", mSource.get());
Andreas Huberb5f28d42013-04-25 15:11:19 -07002490 if (mSource == NULL) {
2491 // This is a stale notification from a source that was
2492 // asynchronously preparing when the client called reset().
2493 // We handled the reset, the source is gone.
2494 break;
2495 }
2496
Andreas Huberec0c5972013-02-05 14:47:13 -08002497 int32_t err;
2498 CHECK(msg->findInt32("err", &err));
2499
Lajos Molnarfcd3e942015-03-31 10:06:48 -07002500 if (err != OK) {
2501 // shut down potential secure codecs in case client never calls reset
2502 mDeferredActions.push_back(
2503 new FlushDecoderAction(FLUSH_CMD_SHUTDOWN /* audio */,
2504 FLUSH_CMD_SHUTDOWN /* video */));
2505 processDeferredActions();
Wei Jia8a092d32016-06-03 14:57:24 -07002506 } else {
2507 mPrepared = true;
Lajos Molnarfcd3e942015-03-31 10:06:48 -07002508 }
2509
Andreas Huber9575c962013-02-05 13:59:56 -08002510 sp<NuPlayerDriver> driver = mDriver.promote();
2511 if (driver != NULL) {
Marco Nelissendd114d12014-05-28 15:23:14 -07002512 // notify duration first, so that it's definitely set when
2513 // the app received the "prepare complete" callback.
2514 int64_t durationUs;
2515 if (mSource->getDuration(&durationUs) == OK) {
2516 driver->notifyDuration(durationUs);
2517 }
Andreas Huberec0c5972013-02-05 14:47:13 -08002518 driver->notifyPrepareCompleted(err);
Andreas Huber9575c962013-02-05 13:59:56 -08002519 }
Andreas Huber99759402013-04-01 14:28:31 -07002520
Andreas Huber9575c962013-02-05 13:59:56 -08002521 break;
2522 }
2523
Hassan Shojaniacefac142017-02-06 21:02:02 -08002524 // Modular DRM
2525 case Source::kWhatDrmInfo:
2526 {
2527 Parcel parcel;
2528 sp<ABuffer> drmInfo;
2529 CHECK(msg->findBuffer("drmInfo", &drmInfo));
2530 parcel.setData(drmInfo->data(), drmInfo->size());
2531
2532 ALOGV("onSourceNotify() kWhatDrmInfo MEDIA_DRM_INFO drmInfo: %p parcel size: %zu",
2533 drmInfo.get(), parcel.dataSize());
2534
2535 notifyListener(MEDIA_DRM_INFO, 0 /* ext1 */, 0 /* ext2 */, &parcel);
2536
2537 break;
2538 }
2539
Andreas Huber9575c962013-02-05 13:59:56 -08002540 case Source::kWhatFlagsChanged:
2541 {
2542 uint32_t flags;
2543 CHECK(msg->findInt32("flags", (int32_t *)&flags));
2544
Chong Zhang4b7069d2013-09-11 12:52:43 -07002545 sp<NuPlayerDriver> driver = mDriver.promote();
2546 if (driver != NULL) {
Hassan Shojaniacefac142017-02-06 21:02:02 -08002547
2548 ALOGV("onSourceNotify() kWhatFlagsChanged FLAG_CAN_PAUSE: %d "
2549 "FLAG_CAN_SEEK_BACKWARD: %d \n\t\t\t\t FLAG_CAN_SEEK_FORWARD: %d "
2550 "FLAG_CAN_SEEK: %d FLAG_DYNAMIC_DURATION: %d \n"
2551 "\t\t\t\t FLAG_SECURE: %d FLAG_PROTECTED: %d",
2552 (flags & Source::FLAG_CAN_PAUSE) != 0,
2553 (flags & Source::FLAG_CAN_SEEK_BACKWARD) != 0,
2554 (flags & Source::FLAG_CAN_SEEK_FORWARD) != 0,
2555 (flags & Source::FLAG_CAN_SEEK) != 0,
2556 (flags & Source::FLAG_DYNAMIC_DURATION) != 0,
2557 (flags & Source::FLAG_SECURE) != 0,
2558 (flags & Source::FLAG_PROTECTED) != 0);
2559
Wei Jia895651b2014-12-10 17:31:52 -08002560 if ((flags & NuPlayer::Source::FLAG_CAN_SEEK) == 0) {
2561 driver->notifyListener(
2562 MEDIA_INFO, MEDIA_INFO_NOT_SEEKABLE, 0);
2563 }
Chong Zhang4b7069d2013-09-11 12:52:43 -07002564 driver->notifyFlagsChanged(flags);
2565 }
2566
Andreas Huber9575c962013-02-05 13:59:56 -08002567 if ((mSourceFlags & Source::FLAG_DYNAMIC_DURATION)
2568 && (!(flags & Source::FLAG_DYNAMIC_DURATION))) {
2569 cancelPollDuration();
2570 } else if (!(mSourceFlags & Source::FLAG_DYNAMIC_DURATION)
2571 && (flags & Source::FLAG_DYNAMIC_DURATION)
2572 && (mAudioDecoder != NULL || mVideoDecoder != NULL)) {
2573 schedulePollDuration();
2574 }
2575
2576 mSourceFlags = flags;
2577 break;
2578 }
2579
2580 case Source::kWhatVideoSizeChanged:
2581 {
Chong Zhangced1c2f2014-08-08 15:22:35 -07002582 sp<AMessage> format;
2583 CHECK(msg->findMessage("format", &format));
Andreas Huber9575c962013-02-05 13:59:56 -08002584
Chong Zhangced1c2f2014-08-08 15:22:35 -07002585 updateVideoSize(format);
Andreas Huber9575c962013-02-05 13:59:56 -08002586 break;
2587 }
2588
Chong Zhang2a3cc9a2014-08-21 17:48:26 -07002589 case Source::kWhatBufferingUpdate:
2590 {
2591 int32_t percentage;
2592 CHECK(msg->findInt32("percentage", &percentage));
2593
2594 notifyListener(MEDIA_BUFFERING_UPDATE, percentage, 0);
2595 break;
2596 }
2597
Chong Zhangefbb6192015-01-30 17:13:27 -08002598 case Source::kWhatPauseOnBufferingStart:
2599 {
2600 // ignore if not playing
Chong Zhang8a048332015-05-06 15:16:28 -07002601 if (mStarted) {
Chong Zhangefbb6192015-01-30 17:13:27 -08002602 ALOGI("buffer low, pausing...");
2603
Ray Essick58e0f7a2017-11-21 10:59:38 -08002604 startRebufferingTimer();
Chong Zhang8a048332015-05-06 15:16:28 -07002605 mPausedForBuffering = true;
Chong Zhangefbb6192015-01-30 17:13:27 -08002606 onPause();
2607 }
Wei Jiabfe82072016-05-20 09:21:50 -07002608 notifyListener(MEDIA_INFO, MEDIA_INFO_BUFFERING_START, 0);
Roger Jönssonb50e83e2013-01-21 16:26:41 +01002609 break;
2610 }
2611
Chong Zhangefbb6192015-01-30 17:13:27 -08002612 case Source::kWhatResumeOnBufferingEnd:
2613 {
2614 // ignore if not playing
Chong Zhang8a048332015-05-06 15:16:28 -07002615 if (mStarted) {
Chong Zhangefbb6192015-01-30 17:13:27 -08002616 ALOGI("buffer ready, resuming...");
2617
Ray Essickeda32522018-02-28 12:08:28 -08002618 updateRebufferingTimer(true /* stopping */, false /* exiting */);
Chong Zhang8a048332015-05-06 15:16:28 -07002619 mPausedForBuffering = false;
2620
2621 // do not resume yet if client didn't unpause
2622 if (!mPausedByClient) {
2623 onResume();
2624 }
Chong Zhangefbb6192015-01-30 17:13:27 -08002625 }
Wei Jia3bed45a2016-02-17 11:06:47 -08002626 notifyListener(MEDIA_INFO, MEDIA_INFO_BUFFERING_END, 0);
Roger Jönssonb50e83e2013-01-21 16:26:41 +01002627 break;
2628 }
2629
Chong Zhangefbb6192015-01-30 17:13:27 -08002630 case Source::kWhatCacheStats:
2631 {
2632 int32_t kbps;
2633 CHECK(msg->findInt32("bandwidth", &kbps));
2634
2635 notifyListener(MEDIA_INFO, MEDIA_INFO_NETWORK_BANDWIDTH, kbps);
2636 break;
2637 }
2638
Chong Zhangdcb89b32013-08-06 09:44:47 -07002639 case Source::kWhatSubtitleData:
2640 {
2641 sp<ABuffer> buffer;
2642 CHECK(msg->findBuffer("buffer", &buffer));
2643
Chong Zhang404fced2014-06-11 14:45:31 -07002644 sendSubtitleData(buffer, 0 /* baseIndex */);
Chong Zhangdcb89b32013-08-06 09:44:47 -07002645 break;
2646 }
2647
Robert Shih08528432015-04-08 09:06:54 -07002648 case Source::kWhatTimedMetaData:
2649 {
2650 sp<ABuffer> buffer;
2651 if (!msg->findBuffer("buffer", &buffer)) {
2652 notifyListener(MEDIA_INFO, MEDIA_INFO_METADATA_UPDATE, 0);
2653 } else {
2654 sendTimedMetaData(buffer);
2655 }
2656 break;
2657 }
2658
Robert Shihd3b0bbb2014-07-23 15:00:25 -07002659 case Source::kWhatTimedTextData:
2660 {
2661 int32_t generation;
2662 if (msg->findInt32("generation", &generation)
2663 && generation != mTimedTextGeneration) {
2664 break;
2665 }
2666
2667 sp<ABuffer> buffer;
2668 CHECK(msg->findBuffer("buffer", &buffer));
2669
2670 sp<NuPlayerDriver> driver = mDriver.promote();
2671 if (driver == NULL) {
2672 break;
2673 }
2674
2675 int posMs;
2676 int64_t timeUs, posUs;
2677 driver->getCurrentPosition(&posMs);
Patrik2 Carlsson24d484b2015-01-27 16:49:45 +01002678 posUs = (int64_t) posMs * 1000ll;
Robert Shihd3b0bbb2014-07-23 15:00:25 -07002679 CHECK(buffer->meta()->findInt64("timeUs", &timeUs));
2680
2681 if (posUs < timeUs) {
2682 if (!msg->findInt32("generation", &generation)) {
2683 msg->setInt32("generation", mTimedTextGeneration);
2684 }
2685 msg->post(timeUs - posUs);
2686 } else {
2687 sendTimedTextData(buffer);
2688 }
2689 break;
2690 }
2691
Andreas Huber14f76722013-01-15 09:04:18 -08002692 case Source::kWhatQueueDecoderShutdown:
2693 {
2694 int32_t audio, video;
2695 CHECK(msg->findInt32("audio", &audio));
2696 CHECK(msg->findInt32("video", &video));
2697
2698 sp<AMessage> reply;
2699 CHECK(msg->findMessage("reply", &reply));
2700
2701 queueDecoderShutdown(audio, video, reply);
2702 break;
2703 }
2704
Ronghua Wu80276872014-08-28 15:50:29 -07002705 case Source::kWhatDrmNoLicense:
2706 {
2707 notifyListener(MEDIA_ERROR, MEDIA_ERROR_UNKNOWN, ERROR_DRM_NO_LICENSE);
2708 break;
2709 }
2710
Andreas Huber9575c962013-02-05 13:59:56 -08002711 default:
2712 TRESPASS();
2713 }
2714}
2715
Chong Zhanga7fa1d92014-06-11 14:49:23 -07002716void NuPlayer::onClosedCaptionNotify(const sp<AMessage> &msg) {
2717 int32_t what;
2718 CHECK(msg->findInt32("what", &what));
2719
2720 switch (what) {
2721 case NuPlayer::CCDecoder::kWhatClosedCaptionData:
2722 {
2723 sp<ABuffer> buffer;
2724 CHECK(msg->findBuffer("buffer", &buffer));
2725
2726 size_t inbandTracks = 0;
2727 if (mSource != NULL) {
2728 inbandTracks = mSource->getTrackCount();
2729 }
2730
2731 sendSubtitleData(buffer, inbandTracks);
2732 break;
2733 }
2734
2735 case NuPlayer::CCDecoder::kWhatTrackAdded:
2736 {
2737 notifyListener(MEDIA_INFO, MEDIA_INFO_METADATA_UPDATE, 0);
2738
2739 break;
2740 }
2741
2742 default:
2743 TRESPASS();
2744 }
2745
2746
2747}
2748
Chong Zhang404fced2014-06-11 14:45:31 -07002749void NuPlayer::sendSubtitleData(const sp<ABuffer> &buffer, int32_t baseIndex) {
2750 int32_t trackIndex;
2751 int64_t timeUs, durationUs;
Robert Shihd83d4f42018-02-24 19:02:46 -08002752 CHECK(buffer->meta()->findInt32("track-index", &trackIndex));
Chong Zhang404fced2014-06-11 14:45:31 -07002753 CHECK(buffer->meta()->findInt64("timeUs", &timeUs));
2754 CHECK(buffer->meta()->findInt64("durationUs", &durationUs));
2755
2756 Parcel in;
2757 in.writeInt32(trackIndex + baseIndex);
2758 in.writeInt64(timeUs);
2759 in.writeInt64(durationUs);
2760 in.writeInt32(buffer->size());
2761 in.writeInt32(buffer->size());
2762 in.write(buffer->data(), buffer->size());
2763
2764 notifyListener(MEDIA_SUBTITLE_DATA, 0, 0, &in);
2765}
Robert Shihd3b0bbb2014-07-23 15:00:25 -07002766
Robert Shih08528432015-04-08 09:06:54 -07002767void NuPlayer::sendTimedMetaData(const sp<ABuffer> &buffer) {
2768 int64_t timeUs;
2769 CHECK(buffer->meta()->findInt64("timeUs", &timeUs));
2770
2771 Parcel in;
2772 in.writeInt64(timeUs);
2773 in.writeInt32(buffer->size());
2774 in.writeInt32(buffer->size());
2775 in.write(buffer->data(), buffer->size());
2776
2777 notifyListener(MEDIA_META_DATA, 0, 0, &in);
2778}
2779
Robert Shihd3b0bbb2014-07-23 15:00:25 -07002780void NuPlayer::sendTimedTextData(const sp<ABuffer> &buffer) {
2781 const void *data;
2782 size_t size = 0;
2783 int64_t timeUs;
Marco Nelissen55e2f4c2015-09-04 15:57:15 -07002784 int32_t flag = TextDescriptions::IN_BAND_TEXT_3GPP;
Robert Shihd3b0bbb2014-07-23 15:00:25 -07002785
2786 AString mime;
2787 CHECK(buffer->meta()->findString("mime", &mime));
2788 CHECK(strcasecmp(mime.c_str(), MEDIA_MIMETYPE_TEXT_3GPP) == 0);
2789
2790 data = buffer->data();
2791 size = buffer->size();
2792
2793 Parcel parcel;
2794 if (size > 0) {
2795 CHECK(buffer->meta()->findInt64("timeUs", &timeUs));
Marco Nelissen55e2f4c2015-09-04 15:57:15 -07002796 int32_t global = 0;
2797 if (buffer->meta()->findInt32("global", &global) && global) {
2798 flag |= TextDescriptions::GLOBAL_DESCRIPTIONS;
2799 } else {
2800 flag |= TextDescriptions::LOCAL_DESCRIPTIONS;
2801 }
Robert Shihd3b0bbb2014-07-23 15:00:25 -07002802 TextDescriptions::getParcelOfDescriptions(
2803 (const uint8_t *)data, size, flag, timeUs / 1000, &parcel);
2804 }
2805
2806 if ((parcel.dataSize() > 0)) {
2807 notifyListener(MEDIA_TIMED_TEXT, 0, 0, &parcel);
2808 } else { // send an empty timed text
2809 notifyListener(MEDIA_TIMED_TEXT, 0, 0);
2810 }
2811}
Hassan Shojaniacefac142017-02-06 21:02:02 -08002812
Hassan Shojaniaff63de72017-04-26 15:10:42 -07002813const char *NuPlayer::getDataSourceType() {
2814 switch (mDataSourceType) {
2815 case DATA_SOURCE_TYPE_HTTP_LIVE:
2816 return "HTTPLive";
2817
2818 case DATA_SOURCE_TYPE_RTSP:
2819 return "RTSP";
2820
2821 case DATA_SOURCE_TYPE_GENERIC_URL:
2822 return "GenURL";
2823
2824 case DATA_SOURCE_TYPE_GENERIC_FD:
2825 return "GenFD";
2826
2827 case DATA_SOURCE_TYPE_MEDIA:
2828 return "Media";
2829
2830 case DATA_SOURCE_TYPE_STREAM:
2831 return "Stream";
2832
2833 case DATA_SOURCE_TYPE_NONE:
2834 default:
2835 return "None";
2836 }
2837 }
2838
Hassan Shojaniacefac142017-02-06 21:02:02 -08002839// Modular DRM begin
2840status_t NuPlayer::prepareDrm(const uint8_t uuid[16], const Vector<uint8_t> &drmSessionId)
2841{
2842 ALOGV("prepareDrm ");
2843
2844 // Passing to the looper anyway; called in a pre-config prepared state so no race on mCrypto
2845 sp<AMessage> msg = new AMessage(kWhatPrepareDrm, this);
2846 // synchronous call so just passing the address but with local copies of "const" args
2847 uint8_t UUID[16];
2848 memcpy(UUID, uuid, sizeof(UUID));
2849 Vector<uint8_t> sessionId = drmSessionId;
2850 msg->setPointer("uuid", (void*)UUID);
2851 msg->setPointer("drmSessionId", (void*)&sessionId);
2852
2853 sp<AMessage> response;
2854 status_t status = msg->postAndAwaitResponse(&response);
2855
2856 if (status == OK && response != NULL) {
2857 CHECK(response->findInt32("status", &status));
2858 ALOGV("prepareDrm ret: %d ", status);
2859 } else {
2860 ALOGE("prepareDrm err: %d", status);
2861 }
2862
2863 return status;
2864}
2865
2866status_t NuPlayer::releaseDrm()
2867{
2868 ALOGV("releaseDrm ");
2869
2870 sp<AMessage> msg = new AMessage(kWhatReleaseDrm, this);
2871
2872 sp<AMessage> response;
2873 status_t status = msg->postAndAwaitResponse(&response);
2874
2875 if (status == OK && response != NULL) {
2876 CHECK(response->findInt32("status", &status));
2877 ALOGV("releaseDrm ret: %d ", status);
2878 } else {
2879 ALOGE("releaseDrm err: %d", status);
2880 }
2881
2882 return status;
2883}
2884
2885status_t NuPlayer::onPrepareDrm(const sp<AMessage> &msg)
2886{
2887 // TODO change to ALOGV
2888 ALOGD("onPrepareDrm ");
2889
2890 status_t status = INVALID_OPERATION;
2891 if (mSource == NULL) {
2892 ALOGE("onPrepareDrm: No source. onPrepareDrm failed with %d.", status);
2893 return status;
2894 }
2895
2896 uint8_t *uuid;
2897 Vector<uint8_t> *drmSessionId;
2898 CHECK(msg->findPointer("uuid", (void**)&uuid));
2899 CHECK(msg->findPointer("drmSessionId", (void**)&drmSessionId));
2900
2901 status = OK;
2902 sp<ICrypto> crypto = NULL;
2903
2904 status = mSource->prepareDrm(uuid, *drmSessionId, &crypto);
2905 if (crypto == NULL) {
2906 ALOGE("onPrepareDrm: mSource->prepareDrm failed. status: %d", status);
2907 return status;
2908 }
2909 ALOGV("onPrepareDrm: mSource->prepareDrm succeeded");
2910
2911 if (mCrypto != NULL) {
2912 ALOGE("onPrepareDrm: Unexpected. Already having mCrypto: %p (%d)",
2913 mCrypto.get(), mCrypto->getStrongCount());
2914 mCrypto.clear();
2915 }
2916
2917 mCrypto = crypto;
2918 mIsDrmProtected = true;
2919 // TODO change to ALOGV
2920 ALOGD("onPrepareDrm: mCrypto: %p (%d)", mCrypto.get(),
2921 (mCrypto != NULL ? mCrypto->getStrongCount() : 0));
2922
2923 return status;
2924}
2925
2926status_t NuPlayer::onReleaseDrm()
2927{
2928 // TODO change to ALOGV
2929 ALOGD("onReleaseDrm ");
2930
Hassan Shojania50b20c92017-02-16 18:28:58 -08002931 if (!mIsDrmProtected) {
2932 ALOGW("onReleaseDrm: Unexpected. mIsDrmProtected is already false.");
2933 }
2934
2935 mIsDrmProtected = false;
Hassan Shojaniacefac142017-02-06 21:02:02 -08002936
2937 status_t status;
2938 if (mCrypto != NULL) {
Hassan Shojania355e8472017-05-12 10:33:16 -07002939 // notifying the source first before removing crypto from codec
2940 if (mSource != NULL) {
2941 mSource->releaseDrm();
2942 }
2943
Hassan Shojaniacefac142017-02-06 21:02:02 -08002944 status=OK;
2945 // first making sure the codecs have released their crypto reference
2946 const sp<DecoderBase> &videoDecoder = getDecoder(false/*audio*/);
2947 if (videoDecoder != NULL) {
2948 status = videoDecoder->releaseCrypto();
2949 ALOGV("onReleaseDrm: video decoder ret: %d", status);
2950 }
2951
2952 const sp<DecoderBase> &audioDecoder = getDecoder(true/*audio*/);
2953 if (audioDecoder != NULL) {
2954 status_t status_audio = audioDecoder->releaseCrypto();
2955 if (status == OK) { // otherwise, returning the first error
2956 status = status_audio;
2957 }
2958 ALOGV("onReleaseDrm: audio decoder ret: %d", status_audio);
2959 }
2960
2961 // TODO change to ALOGV
2962 ALOGD("onReleaseDrm: mCrypto: %p (%d)", mCrypto.get(),
2963 (mCrypto != NULL ? mCrypto->getStrongCount() : 0));
2964 mCrypto.clear();
2965 } else { // mCrypto == NULL
2966 ALOGE("onReleaseDrm: Unexpected. There is no crypto.");
2967 status = INVALID_OPERATION;
2968 }
2969
2970 return status;
2971}
2972// Modular DRM end
Andreas Huberb5f25f02013-02-05 10:14:26 -08002973////////////////////////////////////////////////////////////////////////////////
2974
Chong Zhangced1c2f2014-08-08 15:22:35 -07002975sp<AMessage> NuPlayer::Source::getFormat(bool audio) {
2976 sp<MetaData> meta = getFormatMeta(audio);
2977
2978 if (meta == NULL) {
2979 return NULL;
2980 }
2981
2982 sp<AMessage> msg = new AMessage;
2983
2984 if(convertMetaDataToMessage(meta, &msg) == OK) {
2985 return msg;
2986 }
2987 return NULL;
2988}
2989
Andreas Huber9575c962013-02-05 13:59:56 -08002990void NuPlayer::Source::notifyFlagsChanged(uint32_t flags) {
2991 sp<AMessage> notify = dupNotify();
2992 notify->setInt32("what", kWhatFlagsChanged);
2993 notify->setInt32("flags", flags);
2994 notify->post();
2995}
2996
Chong Zhangced1c2f2014-08-08 15:22:35 -07002997void NuPlayer::Source::notifyVideoSizeChanged(const sp<AMessage> &format) {
Andreas Huber9575c962013-02-05 13:59:56 -08002998 sp<AMessage> notify = dupNotify();
2999 notify->setInt32("what", kWhatVideoSizeChanged);
Chong Zhangced1c2f2014-08-08 15:22:35 -07003000 notify->setMessage("format", format);
Andreas Huber9575c962013-02-05 13:59:56 -08003001 notify->post();
3002}
3003
Andreas Huberec0c5972013-02-05 14:47:13 -08003004void NuPlayer::Source::notifyPrepared(status_t err) {
Hassan Shojaniacefac142017-02-06 21:02:02 -08003005 ALOGV("Source::notifyPrepared %d", err);
Andreas Huber9575c962013-02-05 13:59:56 -08003006 sp<AMessage> notify = dupNotify();
3007 notify->setInt32("what", kWhatPrepared);
Andreas Huberec0c5972013-02-05 14:47:13 -08003008 notify->setInt32("err", err);
Andreas Huber9575c962013-02-05 13:59:56 -08003009 notify->post();
3010}
3011
Hassan Shojaniacefac142017-02-06 21:02:02 -08003012void NuPlayer::Source::notifyDrmInfo(const sp<ABuffer> &drmInfoBuffer)
3013{
3014 ALOGV("Source::notifyDrmInfo");
3015
3016 sp<AMessage> notify = dupNotify();
3017 notify->setInt32("what", kWhatDrmInfo);
3018 notify->setBuffer("drmInfo", drmInfoBuffer);
3019
3020 notify->post();
3021}
3022
Lajos Molnarfcd3e942015-03-31 10:06:48 -07003023void NuPlayer::Source::notifyInstantiateSecureDecoders(const sp<AMessage> &reply) {
3024 sp<AMessage> notify = dupNotify();
3025 notify->setInt32("what", kWhatInstantiateSecureDecoders);
3026 notify->setMessage("reply", reply);
3027 notify->post();
3028}
3029
Andreas Huber84333e02014-02-07 15:36:10 -08003030void NuPlayer::Source::onMessageReceived(const sp<AMessage> & /* msg */) {
Andreas Huberb5f25f02013-02-05 10:14:26 -08003031 TRESPASS();
3032}
3033
Andreas Huberf9334412010-12-15 15:17:42 -08003034} // namespace android