blob: 0f0f8687d55e9872c66cc112502d631dd21cdd62 [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),
Robert Shih1a5c8592015-08-04 18:07:44 -0700186 mPreviousSeekTimeUs(0),
Andreas Huber9b80c2b2011-06-30 15:47:02 -0700187 mAudioEOS(false),
Andreas Huberf9334412010-12-15 15:17:42 -0800188 mVideoEOS(false),
Andreas Huber5bc087c2010-12-23 10:27:40 -0800189 mScanSourcesPending(false),
Andreas Huber1aef2112011-01-04 14:01:29 -0800190 mScanSourcesGeneration(0),
Andreas Huberb7c8e912012-11-27 15:02:53 -0800191 mPollDurationGeneration(0),
Robert Shihd3b0bbb2014-07-23 15:00:25 -0700192 mTimedTextGeneration(0),
Andreas Huberf9334412010-12-15 15:17:42 -0800193 mFlushingAudio(NONE),
Andreas Huber1aef2112011-01-04 14:01:29 -0800194 mFlushingVideo(NONE),
Chong Zhangf8d71772014-11-26 15:08:34 -0800195 mResumePending(false),
Andreas Huber57a339c2012-12-03 11:18:00 -0800196 mVideoScalingMode(NATIVE_WINDOW_SCALING_MODE_SCALE_TO_WINDOW),
Lajos Molnar3a474aa2015-04-24 17:10:07 -0700197 mPlaybackSettings(AUDIO_PLAYBACK_RATE_DEFAULT),
198 mVideoFpsHint(-1.f),
Chong Zhangefbb6192015-01-30 17:13:27 -0800199 mStarted(false),
Wei Jia8a092d32016-06-03 14:57:24 -0700200 mPrepared(false),
Ronghua Wu64c2d172015-10-07 16:52:19 -0700201 mResetting(false),
Robert Shih0c61a0d2015-07-06 15:09:10 -0700202 mSourceStarted(false),
Wei Jia686e8e52017-04-03 14:08:01 -0700203 mAudioDecoderError(false),
204 mVideoDecoderError(false),
Chong Zhangefbb6192015-01-30 17:13:27 -0800205 mPaused(false),
Wei Jia71c75e02016-02-04 09:40:47 -0800206 mPausedByClient(true),
Hassan Shojania50b20c92017-02-16 18:28:58 -0800207 mPausedForBuffering(false),
Hassan Shojaniaff63de72017-04-26 15:10:42 -0700208 mIsDrmProtected(false),
209 mDataSourceType(DATA_SOURCE_TYPE_NONE) {
Wei Jia0a68f662017-08-30 18:01:26 -0700210 CHECK(mediaClock != NULL);
Andy Hung8d121d42014-10-03 09:53:53 -0700211 clearFlushComplete();
Andreas Huberf9334412010-12-15 15:17:42 -0800212}
213
214NuPlayer::~NuPlayer() {
215}
216
Andreas Huber9b80c2b2011-06-30 15:47:02 -0700217void NuPlayer::setUID(uid_t uid) {
218 mUIDValid = true;
219 mUID = uid;
220}
221
Andreas Huber43c3e6c2011-01-05 12:17:08 -0800222void NuPlayer::setDriver(const wp<NuPlayerDriver> &driver) {
223 mDriver = driver;
Andreas Huberf9334412010-12-15 15:17:42 -0800224}
225
Andreas Huber9575c962013-02-05 13:59:56 -0800226void NuPlayer::setDataSourceAsync(const sp<IStreamSource> &source) {
Lajos Molnar1d15ab52015-03-04 16:46:34 -0800227 sp<AMessage> msg = new AMessage(kWhatSetDataSource, this);
Andreas Huberf9334412010-12-15 15:17:42 -0800228
Lajos Molnar1d15ab52015-03-04 16:46:34 -0800229 sp<AMessage> notify = new AMessage(kWhatSourceNotify, this);
Andreas Huberb5f25f02013-02-05 10:14:26 -0800230
Andreas Huber240abcc2014-02-13 13:32:37 -0800231 msg->setObject("source", new StreamingSource(notify, source));
Andreas Huber5bc087c2010-12-23 10:27:40 -0800232 msg->post();
Hassan Shojaniaff63de72017-04-26 15:10:42 -0700233 mDataSourceType = DATA_SOURCE_TYPE_STREAM;
Andreas Huber5bc087c2010-12-23 10:27:40 -0800234}
Andreas Huberf9334412010-12-15 15:17:42 -0800235
Andreas Huberafed0e12011-09-20 15:39:58 -0700236static bool IsHTTPLiveURL(const char *url) {
237 if (!strncasecmp("http://", url, 7)
Andreas Huber99759402013-04-01 14:28:31 -0700238 || !strncasecmp("https://", url, 8)
239 || !strncasecmp("file://", url, 7)) {
Andreas Huberafed0e12011-09-20 15:39:58 -0700240 size_t len = strlen(url);
241 if (len >= 5 && !strcasecmp(".m3u8", &url[len - 5])) {
242 return true;
243 }
244
245 if (strstr(url,"m3u8")) {
246 return true;
247 }
248 }
249
250 return false;
251}
252
Andreas Huber9575c962013-02-05 13:59:56 -0800253void NuPlayer::setDataSourceAsync(
Andreas Huber1b86fe02014-01-29 11:13:26 -0800254 const sp<IMediaHTTPService> &httpService,
255 const char *url,
256 const KeyedVector<String8, String8> *headers) {
Chong Zhang3de157d2014-08-05 20:54:44 -0700257
Lajos Molnar1d15ab52015-03-04 16:46:34 -0800258 sp<AMessage> msg = new AMessage(kWhatSetDataSource, this);
Oscar Rydhé7a33b772012-02-20 10:15:48 +0100259 size_t len = strlen(url);
Andreas Huber5bc087c2010-12-23 10:27:40 -0800260
Lajos Molnar1d15ab52015-03-04 16:46:34 -0800261 sp<AMessage> notify = new AMessage(kWhatSourceNotify, this);
Andreas Huberb5f25f02013-02-05 10:14:26 -0800262
Andreas Huberafed0e12011-09-20 15:39:58 -0700263 sp<Source> source;
264 if (IsHTTPLiveURL(url)) {
Andreas Huber81e68442014-02-05 11:52:33 -0800265 source = new HTTPLiveSource(notify, httpService, url, headers);
Hassan Shojaniacefac142017-02-06 21:02:02 -0800266 ALOGV("setDataSourceAsync HTTPLiveSource %s", url);
Hassan Shojaniaff63de72017-04-26 15:10:42 -0700267 mDataSourceType = DATA_SOURCE_TYPE_HTTP_LIVE;
Andreas Huberafed0e12011-09-20 15:39:58 -0700268 } else if (!strncasecmp(url, "rtsp://", 7)) {
Andreas Huber1b86fe02014-01-29 11:13:26 -0800269 source = new RTSPSource(
270 notify, httpService, url, headers, mUIDValid, mUID);
Hassan Shojaniacefac142017-02-06 21:02:02 -0800271 ALOGV("setDataSourceAsync RTSPSource %s", url);
Hassan Shojaniaff63de72017-04-26 15:10:42 -0700272 mDataSourceType = DATA_SOURCE_TYPE_RTSP;
Oscar Rydhé7a33b772012-02-20 10:15:48 +0100273 } else if ((!strncasecmp(url, "http://", 7)
274 || !strncasecmp(url, "https://", 8))
275 && ((len >= 4 && !strcasecmp(".sdp", &url[len - 4]))
276 || strstr(url, ".sdp?"))) {
Andreas Huber1b86fe02014-01-29 11:13:26 -0800277 source = new RTSPSource(
278 notify, httpService, url, headers, mUIDValid, mUID, true);
Hassan Shojaniacefac142017-02-06 21:02:02 -0800279 ALOGV("setDataSourceAsync RTSPSource http/https/.sdp %s", url);
Hassan Shojaniaff63de72017-04-26 15:10:42 -0700280 mDataSourceType = DATA_SOURCE_TYPE_RTSP;
Andreas Huber2bfdd422011-10-11 15:24:07 -0700281 } else {
Hassan Shojaniacefac142017-02-06 21:02:02 -0800282 ALOGV("setDataSourceAsync GenericSource %s", url);
283
Chong Zhang3de157d2014-08-05 20:54:44 -0700284 sp<GenericSource> genericSource =
Wei Jia992c5592017-09-01 14:20:23 -0700285 new GenericSource(notify, mUIDValid, mUID, mMediaClock);
Andreas Huber2bfdd422011-10-11 15:24:07 -0700286
Chong Zhanga19f33e2014-08-07 15:35:07 -0700287 status_t err = genericSource->setDataSource(httpService, url, headers);
Chong Zhang3de157d2014-08-05 20:54:44 -0700288
289 if (err == OK) {
290 source = genericSource;
291 } else {
Chong Zhanga19f33e2014-08-07 15:35:07 -0700292 ALOGE("Failed to set data source!");
Chong Zhang3de157d2014-08-05 20:54:44 -0700293 }
Hassan Shojaniaff63de72017-04-26 15:10:42 -0700294
295 // regardless of success/failure
296 mDataSourceType = DATA_SOURCE_TYPE_GENERIC_URL;
Chong Zhang3de157d2014-08-05 20:54:44 -0700297 }
Andreas Huberafed0e12011-09-20 15:39:58 -0700298 msg->setObject("source", source);
299 msg->post();
300}
301
Andreas Huber9575c962013-02-05 13:59:56 -0800302void NuPlayer::setDataSourceAsync(int fd, int64_t offset, int64_t length) {
Lajos Molnar1d15ab52015-03-04 16:46:34 -0800303 sp<AMessage> msg = new AMessage(kWhatSetDataSource, this);
Andreas Huberafed0e12011-09-20 15:39:58 -0700304
Lajos Molnar1d15ab52015-03-04 16:46:34 -0800305 sp<AMessage> notify = new AMessage(kWhatSourceNotify, this);
Andreas Huberb5f25f02013-02-05 10:14:26 -0800306
Chong Zhang3de157d2014-08-05 20:54:44 -0700307 sp<GenericSource> source =
Wei Jia992c5592017-09-01 14:20:23 -0700308 new GenericSource(notify, mUIDValid, mUID, mMediaClock);
Chong Zhang3de157d2014-08-05 20:54:44 -0700309
Hassan Shojaniacefac142017-02-06 21:02:02 -0800310 ALOGV("setDataSourceAsync fd %d/%lld/%lld source: %p",
311 fd, (long long)offset, (long long)length, source.get());
312
Chong Zhanga19f33e2014-08-07 15:35:07 -0700313 status_t err = source->setDataSource(fd, offset, length);
Chong Zhang3de157d2014-08-05 20:54:44 -0700314
315 if (err != OK) {
Chong Zhanga19f33e2014-08-07 15:35:07 -0700316 ALOGE("Failed to set data source!");
Chong Zhang3de157d2014-08-05 20:54:44 -0700317 source = NULL;
318 }
319
Andreas Huberafed0e12011-09-20 15:39:58 -0700320 msg->setObject("source", source);
Andreas Huberf9334412010-12-15 15:17:42 -0800321 msg->post();
Hassan Shojaniaff63de72017-04-26 15:10:42 -0700322 mDataSourceType = DATA_SOURCE_TYPE_GENERIC_FD;
Andreas Huberf9334412010-12-15 15:17:42 -0800323}
324
Chris Watkins99f31602015-03-20 13:06:33 -0700325void NuPlayer::setDataSourceAsync(const sp<DataSource> &dataSource) {
326 sp<AMessage> msg = new AMessage(kWhatSetDataSource, this);
327 sp<AMessage> notify = new AMessage(kWhatSourceNotify, this);
328
Wei Jia992c5592017-09-01 14:20:23 -0700329 sp<GenericSource> source = new GenericSource(notify, mUIDValid, mUID, mMediaClock);
Chris Watkins99f31602015-03-20 13:06:33 -0700330 status_t err = source->setDataSource(dataSource);
331
332 if (err != OK) {
333 ALOGE("Failed to set data source!");
334 source = NULL;
335 }
336
337 msg->setObject("source", source);
338 msg->post();
Hassan Shojaniaff63de72017-04-26 15:10:42 -0700339 mDataSourceType = DATA_SOURCE_TYPE_MEDIA;
Chris Watkins99f31602015-03-20 13:06:33 -0700340}
341
Wei Jia48fa06d2016-12-20 15:30:49 -0800342status_t NuPlayer::getDefaultBufferingSettings(
343 BufferingSettings *buffering /* nonnull */) {
344 sp<AMessage> msg = new AMessage(kWhatGetDefaultBufferingSettings, this);
345 sp<AMessage> response;
346 status_t err = msg->postAndAwaitResponse(&response);
347 if (err == OK && response != NULL) {
348 CHECK(response->findInt32("err", &err));
349 if (err == OK) {
350 readFromAMessage(response, buffering);
351 }
352 }
353 return err;
354}
355
356status_t NuPlayer::setBufferingSettings(const BufferingSettings& buffering) {
357 sp<AMessage> msg = new AMessage(kWhatSetBufferingSettings, this);
358 writeToAMessage(msg, buffering);
359 sp<AMessage> response;
360 status_t err = msg->postAndAwaitResponse(&response);
361 if (err == OK && response != NULL) {
362 CHECK(response->findInt32("err", &err));
363 }
364 return err;
365}
366
Andreas Huber9575c962013-02-05 13:59:56 -0800367void NuPlayer::prepareAsync() {
Hassan Shojaniacefac142017-02-06 21:02:02 -0800368 ALOGV("prepareAsync");
369
Lajos Molnar1d15ab52015-03-04 16:46:34 -0800370 (new AMessage(kWhatPrepare, this))->post();
Andreas Huber9575c962013-02-05 13:59:56 -0800371}
372
Andreas Huber57a339c2012-12-03 11:18:00 -0800373void NuPlayer::setVideoSurfaceTextureAsync(
Andy McFadden8ba01022012-12-18 09:46:54 -0800374 const sp<IGraphicBufferProducer> &bufferProducer) {
Lajos Molnar1de1e252015-04-30 18:18:34 -0700375 sp<AMessage> msg = new AMessage(kWhatSetVideoSurface, this);
Andreas Huber57a339c2012-12-03 11:18:00 -0800376
Andy McFadden8ba01022012-12-18 09:46:54 -0800377 if (bufferProducer == NULL) {
Lajos Molnar1de1e252015-04-30 18:18:34 -0700378 msg->setObject("surface", NULL);
Andreas Huber57a339c2012-12-03 11:18:00 -0800379 } else {
Lajos Molnar1de1e252015-04-30 18:18:34 -0700380 msg->setObject("surface", new Surface(bufferProducer, true /* controlledByApp */));
Andreas Huber57a339c2012-12-03 11:18:00 -0800381 }
382
Andreas Huberf9334412010-12-15 15:17:42 -0800383 msg->post();
384}
385
386void NuPlayer::setAudioSink(const sp<MediaPlayerBase::AudioSink> &sink) {
Lajos Molnar1d15ab52015-03-04 16:46:34 -0800387 sp<AMessage> msg = new AMessage(kWhatSetAudioSink, this);
Andreas Huberf9334412010-12-15 15:17:42 -0800388 msg->setObject("sink", sink);
389 msg->post();
390}
391
392void NuPlayer::start() {
Lajos Molnar1d15ab52015-03-04 16:46:34 -0800393 (new AMessage(kWhatStart, this))->post();
Andreas Huberf9334412010-12-15 15:17:42 -0800394}
395
Lajos Molnar3a474aa2015-04-24 17:10:07 -0700396status_t NuPlayer::setPlaybackSettings(const AudioPlaybackRate &rate) {
397 // do some cursory validation of the settings here. audio modes are
398 // only validated when set on the audiosink.
399 if ((rate.mSpeed != 0.f && rate.mSpeed < AUDIO_TIMESTRETCH_SPEED_MIN)
400 || rate.mSpeed > AUDIO_TIMESTRETCH_SPEED_MAX
401 || rate.mPitch < AUDIO_TIMESTRETCH_SPEED_MIN
402 || rate.mPitch > AUDIO_TIMESTRETCH_SPEED_MAX) {
403 return BAD_VALUE;
404 }
405 sp<AMessage> msg = new AMessage(kWhatConfigPlayback, this);
406 writeToAMessage(msg, rate);
407 sp<AMessage> response;
408 status_t err = msg->postAndAwaitResponse(&response);
409 if (err == OK && response != NULL) {
410 CHECK(response->findInt32("err", &err));
411 }
412 return err;
413}
414
415status_t NuPlayer::getPlaybackSettings(AudioPlaybackRate *rate /* nonnull */) {
416 sp<AMessage> msg = new AMessage(kWhatGetPlaybackSettings, this);
417 sp<AMessage> response;
418 status_t err = msg->postAndAwaitResponse(&response);
419 if (err == OK && response != NULL) {
420 CHECK(response->findInt32("err", &err));
421 if (err == OK) {
422 readFromAMessage(response, rate);
423 }
424 }
425 return err;
426}
427
428status_t NuPlayer::setSyncSettings(const AVSyncSettings &sync, float videoFpsHint) {
429 sp<AMessage> msg = new AMessage(kWhatConfigSync, this);
430 writeToAMessage(msg, sync, videoFpsHint);
431 sp<AMessage> response;
432 status_t err = msg->postAndAwaitResponse(&response);
433 if (err == OK && response != NULL) {
434 CHECK(response->findInt32("err", &err));
435 }
436 return err;
437}
438
439status_t NuPlayer::getSyncSettings(
440 AVSyncSettings *sync /* nonnull */, float *videoFps /* nonnull */) {
441 sp<AMessage> msg = new AMessage(kWhatGetSyncSettings, this);
442 sp<AMessage> response;
443 status_t err = msg->postAndAwaitResponse(&response);
444 if (err == OK && response != NULL) {
445 CHECK(response->findInt32("err", &err));
446 if (err == OK) {
447 readFromAMessage(response, sync, videoFps);
448 }
449 }
450 return err;
Wei Jia98160162015-02-04 17:01:11 -0800451}
452
Andreas Huber43c3e6c2011-01-05 12:17:08 -0800453void NuPlayer::pause() {
Lajos Molnar1d15ab52015-03-04 16:46:34 -0800454 (new AMessage(kWhatPause, this))->post();
Andreas Huber43c3e6c2011-01-05 12:17:08 -0800455}
456
Andreas Huber1aef2112011-01-04 14:01:29 -0800457void NuPlayer::resetAsync() {
Wei Jiac45a4b22016-04-15 15:30:23 -0700458 sp<Source> source;
459 {
460 Mutex::Autolock autoLock(mSourceLock);
461 source = mSource;
462 }
463
464 if (source != NULL) {
Chong Zhang48296b72014-09-14 14:28:45 -0700465 // During a reset, the data source might be unresponsive already, we need to
466 // disconnect explicitly so that reads exit promptly.
467 // We can't queue the disconnect request to the looper, as it might be
468 // queued behind a stuck read and never gets processed.
469 // Doing a disconnect outside the looper to allows the pending reads to exit
470 // (either successfully or with error).
Wei Jiac45a4b22016-04-15 15:30:23 -0700471 source->disconnect();
Chong Zhang48296b72014-09-14 14:28:45 -0700472 }
473
Lajos Molnar1d15ab52015-03-04 16:46:34 -0800474 (new AMessage(kWhatReset, this))->post();
Andreas Huber1aef2112011-01-04 14:01:29 -0800475}
476
Wei Jia52c28512017-09-13 18:17:51 -0700477status_t NuPlayer::notifyAt(int64_t mediaTimeUs) {
478 sp<AMessage> notify = new AMessage(kWhatNotifyTime, this);
479 notify->setInt64("timerUs", mediaTimeUs);
480 mMediaClock->addTimer(notify, mediaTimeUs);
481 return OK;
482}
483
Wei Jiac5de0912016-11-18 10:22:14 -0800484void NuPlayer::seekToAsync(int64_t seekTimeUs, MediaPlayerSeekMode mode, bool needNotify) {
Lajos Molnar1d15ab52015-03-04 16:46:34 -0800485 sp<AMessage> msg = new AMessage(kWhatSeek, this);
Andreas Huber43c3e6c2011-01-05 12:17:08 -0800486 msg->setInt64("seekTimeUs", seekTimeUs);
Wei Jiac5de0912016-11-18 10:22:14 -0800487 msg->setInt32("mode", mode);
Wei Jiae427abf2014-09-22 15:21:11 -0700488 msg->setInt32("needNotify", needNotify);
Andreas Huber43c3e6c2011-01-05 12:17:08 -0800489 msg->post();
490}
491
Andreas Huber53df1a42010-12-22 10:03:04 -0800492
Chong Zhang404fced2014-06-11 14:45:31 -0700493void NuPlayer::writeTrackInfo(
Chih-Hung Hsiehe964d4e2016-08-09 14:31:32 -0700494 Parcel* reply, const sp<AMessage>& format) const {
Marco Nelissenc367ca12015-09-15 09:51:59 -0700495 if (format == NULL) {
496 ALOGE("NULL format");
497 return;
498 }
Chong Zhang404fced2014-06-11 14:45:31 -0700499 int32_t trackType;
Marco Nelissen9436e482015-09-15 10:21:53 -0700500 if (!format->findInt32("type", &trackType)) {
Marco Nelissenc367ca12015-09-15 09:51:59 -0700501 ALOGE("no track type");
502 return;
503 }
Chong Zhang404fced2014-06-11 14:45:31 -0700504
Robert Shih755106e2015-04-30 14:36:45 -0700505 AString mime;
Robert Shih2e3a4252015-05-06 10:21:15 -0700506 if (!format->findString("mime", &mime)) {
507 // Java MediaPlayer only uses mimetype for subtitle and timedtext tracks.
508 // If we can't find the mimetype here it means that we wouldn't be needing
509 // the mimetype on the Java end. We still write a placeholder mime to keep the
510 // (de)serialization logic simple.
511 if (trackType == MEDIA_TRACK_TYPE_AUDIO) {
512 mime = "audio/";
513 } else if (trackType == MEDIA_TRACK_TYPE_VIDEO) {
514 mime = "video/";
515 } else {
Marco Nelissenc367ca12015-09-15 09:51:59 -0700516 ALOGE("unknown track type: %d", trackType);
517 return;
Robert Shih2e3a4252015-05-06 10:21:15 -0700518 }
519 }
Robert Shih755106e2015-04-30 14:36:45 -0700520
Chong Zhang404fced2014-06-11 14:45:31 -0700521 AString lang;
Marco Nelissen9436e482015-09-15 10:21:53 -0700522 if (!format->findString("language", &lang)) {
Marco Nelissenc367ca12015-09-15 09:51:59 -0700523 ALOGE("no language");
524 return;
525 }
Chong Zhang404fced2014-06-11 14:45:31 -0700526
527 reply->writeInt32(2); // write something non-zero
528 reply->writeInt32(trackType);
Robert Shih755106e2015-04-30 14:36:45 -0700529 reply->writeString16(String16(mime.c_str()));
Chong Zhang404fced2014-06-11 14:45:31 -0700530 reply->writeString16(String16(lang.c_str()));
531
532 if (trackType == MEDIA_TRACK_TYPE_SUBTITLE) {
Chong Zhang404fced2014-06-11 14:45:31 -0700533 int32_t isAuto, isDefault, isForced;
534 CHECK(format->findInt32("auto", &isAuto));
535 CHECK(format->findInt32("default", &isDefault));
536 CHECK(format->findInt32("forced", &isForced));
537
Chong Zhang404fced2014-06-11 14:45:31 -0700538 reply->writeInt32(isAuto);
539 reply->writeInt32(isDefault);
540 reply->writeInt32(isForced);
541 }
542}
543
Andreas Huberf9334412010-12-15 15:17:42 -0800544void NuPlayer::onMessageReceived(const sp<AMessage> &msg) {
545 switch (msg->what()) {
546 case kWhatSetDataSource:
547 {
Steve Block3856b092011-10-20 11:56:00 +0100548 ALOGV("kWhatSetDataSource");
Andreas Huberf9334412010-12-15 15:17:42 -0800549
550 CHECK(mSource == NULL);
551
Chong Zhang3de157d2014-08-05 20:54:44 -0700552 status_t err = OK;
Andreas Huber5bc087c2010-12-23 10:27:40 -0800553 sp<RefBase> obj;
554 CHECK(msg->findObject("source", &obj));
Chong Zhang3de157d2014-08-05 20:54:44 -0700555 if (obj != NULL) {
Wei Jiac45a4b22016-04-15 15:30:23 -0700556 Mutex::Autolock autoLock(mSourceLock);
Chong Zhang3de157d2014-08-05 20:54:44 -0700557 mSource = static_cast<Source *>(obj.get());
Chong Zhang3de157d2014-08-05 20:54:44 -0700558 } else {
559 err = UNKNOWN_ERROR;
560 }
Andreas Huber9575c962013-02-05 13:59:56 -0800561
562 CHECK(mDriver != NULL);
563 sp<NuPlayerDriver> driver = mDriver.promote();
564 if (driver != NULL) {
Chong Zhang3de157d2014-08-05 20:54:44 -0700565 driver->notifySetDataSourceCompleted(err);
Andreas Huber9575c962013-02-05 13:59:56 -0800566 }
567 break;
568 }
569
Wei Jia48fa06d2016-12-20 15:30:49 -0800570 case kWhatGetDefaultBufferingSettings:
571 {
572 sp<AReplyToken> replyID;
573 CHECK(msg->senderAwaitsResponse(&replyID));
574
575 ALOGV("kWhatGetDefaultBufferingSettings");
576 BufferingSettings buffering;
577 status_t err = OK;
578 if (mSource != NULL) {
579 err = mSource->getDefaultBufferingSettings(&buffering);
580 } else {
581 err = INVALID_OPERATION;
582 }
583 sp<AMessage> response = new AMessage;
584 if (err == OK) {
585 writeToAMessage(response, buffering);
586 }
587 response->setInt32("err", err);
588 response->postReply(replyID);
589 break;
590 }
591
592 case kWhatSetBufferingSettings:
593 {
594 sp<AReplyToken> replyID;
595 CHECK(msg->senderAwaitsResponse(&replyID));
596
597 ALOGV("kWhatSetBufferingSettings");
598 BufferingSettings buffering;
599 readFromAMessage(msg, &buffering);
600 status_t err = OK;
601 if (mSource != NULL) {
602 err = mSource->setBufferingSettings(buffering);
603 } else {
604 err = INVALID_OPERATION;
605 }
606 sp<AMessage> response = new AMessage;
607 response->setInt32("err", err);
608 response->postReply(replyID);
609 break;
610 }
611
Andreas Huber9575c962013-02-05 13:59:56 -0800612 case kWhatPrepare:
613 {
Hassan Shojaniacefac142017-02-06 21:02:02 -0800614 ALOGV("onMessageReceived kWhatPrepare");
615
Andreas Huber9575c962013-02-05 13:59:56 -0800616 mSource->prepareAsync();
Andreas Huberf9334412010-12-15 15:17:42 -0800617 break;
618 }
619
Chong Zhangdcb89b32013-08-06 09:44:47 -0700620 case kWhatGetTrackInfo:
621 {
Lajos Molnar3f274362015-03-05 14:35:41 -0800622 sp<AReplyToken> replyID;
Chong Zhangdcb89b32013-08-06 09:44:47 -0700623 CHECK(msg->senderAwaitsResponse(&replyID));
624
Chong Zhang404fced2014-06-11 14:45:31 -0700625 Parcel* reply;
626 CHECK(msg->findPointer("reply", (void**)&reply));
627
628 size_t inbandTracks = 0;
Chong Zhangdcb89b32013-08-06 09:44:47 -0700629 if (mSource != NULL) {
Chong Zhang404fced2014-06-11 14:45:31 -0700630 inbandTracks = mSource->getTrackCount();
631 }
632
Chong Zhanga7fa1d92014-06-11 14:49:23 -0700633 size_t ccTracks = 0;
634 if (mCCDecoder != NULL) {
635 ccTracks = mCCDecoder->getTrackCount();
636 }
637
Chong Zhang404fced2014-06-11 14:45:31 -0700638 // total track count
Chong Zhanga7fa1d92014-06-11 14:49:23 -0700639 reply->writeInt32(inbandTracks + ccTracks);
Chong Zhang404fced2014-06-11 14:45:31 -0700640
641 // write inband tracks
642 for (size_t i = 0; i < inbandTracks; ++i) {
643 writeTrackInfo(reply, mSource->getTrackInfo(i));
Chong Zhangdcb89b32013-08-06 09:44:47 -0700644 }
645
Chong Zhanga7fa1d92014-06-11 14:49:23 -0700646 // write CC track
647 for (size_t i = 0; i < ccTracks; ++i) {
648 writeTrackInfo(reply, mCCDecoder->getTrackInfo(i));
649 }
650
Chong Zhangdcb89b32013-08-06 09:44:47 -0700651 sp<AMessage> response = new AMessage;
Chong Zhangdcb89b32013-08-06 09:44:47 -0700652 response->postReply(replyID);
653 break;
654 }
655
Robert Shih7c4f0d72014-07-09 18:53:31 -0700656 case kWhatGetSelectedTrack:
657 {
658 status_t err = INVALID_OPERATION;
659 if (mSource != NULL) {
660 err = OK;
661
662 int32_t type32;
663 CHECK(msg->findInt32("type", (int32_t*)&type32));
664 media_track_type type = (media_track_type)type32;
665 ssize_t selectedTrack = mSource->getSelectedTrack(type);
666
667 Parcel* reply;
668 CHECK(msg->findPointer("reply", (void**)&reply));
669 reply->writeInt32(selectedTrack);
670 }
671
672 sp<AMessage> response = new AMessage;
673 response->setInt32("err", err);
674
Lajos Molnar3f274362015-03-05 14:35:41 -0800675 sp<AReplyToken> replyID;
Robert Shih7c4f0d72014-07-09 18:53:31 -0700676 CHECK(msg->senderAwaitsResponse(&replyID));
677 response->postReply(replyID);
678 break;
679 }
680
Chong Zhangdcb89b32013-08-06 09:44:47 -0700681 case kWhatSelectTrack:
682 {
Lajos Molnar3f274362015-03-05 14:35:41 -0800683 sp<AReplyToken> replyID;
Chong Zhangdcb89b32013-08-06 09:44:47 -0700684 CHECK(msg->senderAwaitsResponse(&replyID));
685
Chong Zhang404fced2014-06-11 14:45:31 -0700686 size_t trackIndex;
687 int32_t select;
Robert Shih6ffb1fd2014-10-29 16:24:32 -0700688 int64_t timeUs;
Chong Zhang404fced2014-06-11 14:45:31 -0700689 CHECK(msg->findSize("trackIndex", &trackIndex));
690 CHECK(msg->findInt32("select", &select));
Robert Shih6ffb1fd2014-10-29 16:24:32 -0700691 CHECK(msg->findInt64("timeUs", &timeUs));
Chong Zhang404fced2014-06-11 14:45:31 -0700692
Chong Zhangdcb89b32013-08-06 09:44:47 -0700693 status_t err = INVALID_OPERATION;
Chong Zhang404fced2014-06-11 14:45:31 -0700694
695 size_t inbandTracks = 0;
Chong Zhangdcb89b32013-08-06 09:44:47 -0700696 if (mSource != NULL) {
Chong Zhang404fced2014-06-11 14:45:31 -0700697 inbandTracks = mSource->getTrackCount();
698 }
Chong Zhanga7fa1d92014-06-11 14:49:23 -0700699 size_t ccTracks = 0;
700 if (mCCDecoder != NULL) {
701 ccTracks = mCCDecoder->getTrackCount();
702 }
Chong Zhang404fced2014-06-11 14:45:31 -0700703
704 if (trackIndex < inbandTracks) {
Robert Shih6ffb1fd2014-10-29 16:24:32 -0700705 err = mSource->selectTrack(trackIndex, select, timeUs);
Robert Shihd3b0bbb2014-07-23 15:00:25 -0700706
707 if (!select && err == OK) {
708 int32_t type;
709 sp<AMessage> info = mSource->getTrackInfo(trackIndex);
710 if (info != NULL
711 && info->findInt32("type", &type)
712 && type == MEDIA_TRACK_TYPE_TIMEDTEXT) {
713 ++mTimedTextGeneration;
714 }
715 }
Chong Zhanga7fa1d92014-06-11 14:49:23 -0700716 } else {
717 trackIndex -= inbandTracks;
718
719 if (trackIndex < ccTracks) {
720 err = mCCDecoder->selectTrack(trackIndex, select);
721 }
Chong Zhangdcb89b32013-08-06 09:44:47 -0700722 }
723
724 sp<AMessage> response = new AMessage;
725 response->setInt32("err", err);
726
727 response->postReply(replyID);
728 break;
729 }
730
Andreas Huberb7c8e912012-11-27 15:02:53 -0800731 case kWhatPollDuration:
732 {
733 int32_t generation;
734 CHECK(msg->findInt32("generation", &generation));
735
736 if (generation != mPollDurationGeneration) {
737 // stale
738 break;
739 }
740
741 int64_t durationUs;
742 if (mDriver != NULL && mSource->getDuration(&durationUs) == OK) {
743 sp<NuPlayerDriver> driver = mDriver.promote();
744 if (driver != NULL) {
745 driver->notifyDuration(durationUs);
746 }
747 }
748
749 msg->post(1000000ll); // poll again in a second.
750 break;
751 }
752
Lajos Molnar1de1e252015-04-30 18:18:34 -0700753 case kWhatSetVideoSurface:
Andreas Huberf9334412010-12-15 15:17:42 -0800754 {
Andreas Huberf9334412010-12-15 15:17:42 -0800755
756 sp<RefBase> obj;
Lajos Molnar1de1e252015-04-30 18:18:34 -0700757 CHECK(msg->findObject("surface", &obj));
758 sp<Surface> surface = static_cast<Surface *>(obj.get());
Lajos Molnara81c6222015-07-10 19:17:45 -0700759
760 ALOGD("onSetVideoSurface(%p, %s video decoder)",
761 surface.get(),
Wei Jia6b7d2ed2015-08-10 15:11:54 -0700762 (mSource != NULL && mStarted && mSource->getFormat(false /* audio */) != NULL
Lajos Molnara81c6222015-07-10 19:17:45 -0700763 && mVideoDecoder != NULL) ? "have" : "no");
764
Wei Jia6b7d2ed2015-08-10 15:11:54 -0700765 // Need to check mStarted before calling mSource->getFormat because NuPlayer might
766 // be in preparing state and it could take long time.
767 // When mStarted is true, mSource must have been set.
768 if (mSource == NULL || !mStarted || mSource->getFormat(false /* audio */) == NULL
Lajos Molnara81c6222015-07-10 19:17:45 -0700769 // NOTE: mVideoDecoder's mSurface is always non-null
770 || (mVideoDecoder != NULL && mVideoDecoder->setVideoSurface(surface) == OK)) {
Lajos Molnar1de1e252015-04-30 18:18:34 -0700771 performSetSurface(surface);
Wei Jiafef808d2014-10-31 17:57:05 -0700772 break;
773 }
774
775 mDeferredActions.push_back(
Krystian Turczyn687a5892016-01-20 14:20:35 +0100776 new FlushDecoderAction(
777 (obj != NULL ? FLUSH_CMD_FLUSH : FLUSH_CMD_NONE) /* audio */,
Wei Jiafef808d2014-10-31 17:57:05 -0700778 FLUSH_CMD_SHUTDOWN /* video */));
779
Lajos Molnar1de1e252015-04-30 18:18:34 -0700780 mDeferredActions.push_back(new SetSurfaceAction(surface));
James Dong0d268a32012-08-31 12:18:27 -0700781
Krystian Turczyn687a5892016-01-20 14:20:35 +0100782 if (obj != NULL) {
Wei Jiafef808d2014-10-31 17:57:05 -0700783 if (mStarted) {
Andy Hung73535852014-09-05 11:42:58 -0700784 // Issue a seek to refresh the video screen only if started otherwise
785 // the extractor may not yet be started and will assert.
786 // If the video decoder is not set (perhaps audio only in this case)
787 // do not perform a seek as it is not needed.
Ronghua Wua73d9e02014-10-08 15:13:29 -0700788 int64_t currentPositionUs = 0;
789 if (getCurrentPosition(&currentPositionUs) == OK) {
790 mDeferredActions.push_back(
Wei Jiac5de0912016-11-18 10:22:14 -0800791 new SeekAction(currentPositionUs,
792 MediaPlayerSeekMode::SEEK_PREVIOUS_SYNC /* mode */));
Ronghua Wua73d9e02014-10-08 15:13:29 -0700793 }
Andy Hung73535852014-09-05 11:42:58 -0700794 }
Wei Jiaac428aa2014-09-02 19:01:34 -0700795
Andreas Huber57a339c2012-12-03 11:18:00 -0800796 // If there is a new surface texture, instantiate decoders
797 // again if possible.
798 mDeferredActions.push_back(
799 new SimpleAction(&NuPlayer::performScanSources));
Andreas Huber57a339c2012-12-03 11:18:00 -0800800
Krystian Turczyn687a5892016-01-20 14:20:35 +0100801 // After a flush without shutdown, decoder is paused.
802 // Don't resume it until source seek is done, otherwise it could
803 // start pulling stale data too soon.
804 mDeferredActions.push_back(
805 new ResumeDecoderAction(false /* needNotify */));
806 }
Chong Zhang7137ec72014-11-12 16:41:05 -0800807
Andreas Huber57a339c2012-12-03 11:18:00 -0800808 processDeferredActions();
Andreas Huberf9334412010-12-15 15:17:42 -0800809 break;
810 }
811
812 case kWhatSetAudioSink:
813 {
Steve Block3856b092011-10-20 11:56:00 +0100814 ALOGV("kWhatSetAudioSink");
Andreas Huberf9334412010-12-15 15:17:42 -0800815
816 sp<RefBase> obj;
817 CHECK(msg->findObject("sink", &obj));
818
819 mAudioSink = static_cast<MediaPlayerBase::AudioSink *>(obj.get());
820 break;
821 }
822
823 case kWhatStart:
824 {
Steve Block3856b092011-10-20 11:56:00 +0100825 ALOGV("kWhatStart");
Wei Jia94211742014-10-28 17:09:06 -0700826 if (mStarted) {
Chong Zhang8a048332015-05-06 15:16:28 -0700827 // do not resume yet if the source is still buffering
828 if (!mPausedForBuffering) {
829 onResume();
830 }
Wei Jia94211742014-10-28 17:09:06 -0700831 } else {
832 onStart();
Lajos Molnar09524832014-07-17 14:29:51 -0700833 }
Chong Zhangefbb6192015-01-30 17:13:27 -0800834 mPausedByClient = false;
Andreas Huberf9334412010-12-15 15:17:42 -0800835 break;
836 }
837
Lajos Molnar3a474aa2015-04-24 17:10:07 -0700838 case kWhatConfigPlayback:
Wei Jia98160162015-02-04 17:01:11 -0800839 {
Lajos Molnar3a474aa2015-04-24 17:10:07 -0700840 sp<AReplyToken> replyID;
841 CHECK(msg->senderAwaitsResponse(&replyID));
842 AudioPlaybackRate rate /* sanitized */;
843 readFromAMessage(msg, &rate);
844 status_t err = OK;
Wei Jia98160162015-02-04 17:01:11 -0800845 if (mRenderer != NULL) {
Wei Jiabf70feb2016-02-19 15:47:16 -0800846 // AudioSink allows only 1.f and 0.f for offload mode.
847 // For other speed, switch to non-offload mode.
848 if (mOffloadAudio && ((rate.mSpeed != 0.f && rate.mSpeed != 1.f)
849 || rate.mPitch != 1.f)) {
850 int64_t currentPositionUs;
851 if (getCurrentPosition(&currentPositionUs) != OK) {
852 currentPositionUs = mPreviousSeekTimeUs;
853 }
854
855 // Set mPlaybackSettings so that the new audio decoder can
856 // be created correctly.
857 mPlaybackSettings = rate;
858 if (!mPaused) {
859 mRenderer->pause();
860 }
Wei Jia5031b2f2016-02-25 11:19:31 -0800861 restartAudio(
Wei Jiabf70feb2016-02-19 15:47:16 -0800862 currentPositionUs, true /* forceNonOffload */,
863 true /* needsToCreateAudioDecoder */);
864 if (!mPaused) {
865 mRenderer->resume();
866 }
867 }
868
Lajos Molnar3a474aa2015-04-24 17:10:07 -0700869 err = mRenderer->setPlaybackSettings(rate);
870 }
871 if (err == OK) {
872 if (rate.mSpeed == 0.f) {
873 onPause();
Wei Jia351ce872016-02-10 13:21:59 -0800874 mPausedByClient = true;
Lajos Molnar3a474aa2015-04-24 17:10:07 -0700875 // save all other settings (using non-paused speed)
876 // so we can restore them on start
877 AudioPlaybackRate newRate = rate;
878 newRate.mSpeed = mPlaybackSettings.mSpeed;
879 mPlaybackSettings = newRate;
880 } else { /* rate.mSpeed != 0.f */
Lajos Molnar3a474aa2015-04-24 17:10:07 -0700881 mPlaybackSettings = rate;
Wei Jia8a092d32016-06-03 14:57:24 -0700882 if (mStarted) {
883 // do not resume yet if the source is still buffering
884 if (!mPausedForBuffering) {
885 onResume();
886 }
887 } else if (mPrepared) {
888 onStart();
889 }
890
891 mPausedByClient = false;
Lajos Molnar3a474aa2015-04-24 17:10:07 -0700892 }
Wei Jia98160162015-02-04 17:01:11 -0800893 }
Ronghua Wu8db88132015-04-22 13:51:35 -0700894
895 if (mVideoDecoder != NULL) {
Praveen Chavanbbaa1442016-04-08 13:33:49 -0700896 sp<AMessage> params = new AMessage();
897 params->setFloat("playback-speed", mPlaybackSettings.mSpeed);
898 mVideoDecoder->setParameters(params);
Ronghua Wu8db88132015-04-22 13:51:35 -0700899 }
900
Lajos Molnar3a474aa2015-04-24 17:10:07 -0700901 sp<AMessage> response = new AMessage;
902 response->setInt32("err", err);
903 response->postReply(replyID);
904 break;
905 }
906
907 case kWhatGetPlaybackSettings:
908 {
909 sp<AReplyToken> replyID;
910 CHECK(msg->senderAwaitsResponse(&replyID));
911 AudioPlaybackRate rate = mPlaybackSettings;
912 status_t err = OK;
913 if (mRenderer != NULL) {
914 err = mRenderer->getPlaybackSettings(&rate);
915 }
916 if (err == OK) {
917 // get playback settings used by renderer, as it may be
918 // slightly off due to audiosink not taking small changes.
919 mPlaybackSettings = rate;
920 if (mPaused) {
921 rate.mSpeed = 0.f;
922 }
923 }
924 sp<AMessage> response = new AMessage;
925 if (err == OK) {
926 writeToAMessage(response, rate);
927 }
928 response->setInt32("err", err);
929 response->postReply(replyID);
930 break;
931 }
932
933 case kWhatConfigSync:
934 {
935 sp<AReplyToken> replyID;
936 CHECK(msg->senderAwaitsResponse(&replyID));
937
938 ALOGV("kWhatConfigSync");
939 AVSyncSettings sync;
940 float videoFpsHint;
941 readFromAMessage(msg, &sync, &videoFpsHint);
942 status_t err = OK;
943 if (mRenderer != NULL) {
944 err = mRenderer->setSyncSettings(sync, videoFpsHint);
945 }
946 if (err == OK) {
947 mSyncSettings = sync;
948 mVideoFpsHint = videoFpsHint;
949 }
950 sp<AMessage> response = new AMessage;
951 response->setInt32("err", err);
952 response->postReply(replyID);
953 break;
954 }
955
956 case kWhatGetSyncSettings:
957 {
958 sp<AReplyToken> replyID;
959 CHECK(msg->senderAwaitsResponse(&replyID));
960 AVSyncSettings sync = mSyncSettings;
961 float videoFps = mVideoFpsHint;
962 status_t err = OK;
963 if (mRenderer != NULL) {
964 err = mRenderer->getSyncSettings(&sync, &videoFps);
965 if (err == OK) {
966 mSyncSettings = sync;
967 mVideoFpsHint = videoFps;
968 }
969 }
970 sp<AMessage> response = new AMessage;
971 if (err == OK) {
972 writeToAMessage(response, sync, videoFps);
973 }
974 response->setInt32("err", err);
975 response->postReply(replyID);
Wei Jia98160162015-02-04 17:01:11 -0800976 break;
977 }
978
Andreas Huberf9334412010-12-15 15:17:42 -0800979 case kWhatScanSources:
980 {
Andreas Huber1aef2112011-01-04 14:01:29 -0800981 int32_t generation;
982 CHECK(msg->findInt32("generation", &generation));
983 if (generation != mScanSourcesGeneration) {
984 // Drop obsolete msg.
985 break;
986 }
987
Andreas Huber5bc087c2010-12-23 10:27:40 -0800988 mScanSourcesPending = false;
989
Steve Block3856b092011-10-20 11:56:00 +0100990 ALOGV("scanning sources haveAudio=%d, haveVideo=%d",
Andreas Huber43c3e6c2011-01-05 12:17:08 -0800991 mAudioDecoder != NULL, mVideoDecoder != NULL);
992
Andreas Huberb7c8e912012-11-27 15:02:53 -0800993 bool mHadAnySourcesBefore =
994 (mAudioDecoder != NULL) || (mVideoDecoder != NULL);
Robert Shih7350b052015-10-01 15:50:14 -0700995 bool rescan = false;
Andreas Huberb7c8e912012-11-27 15:02:53 -0800996
Andy Hung282a7e32014-08-14 15:56:34 -0700997 // initialize video before audio because successful initialization of
998 // video may change deep buffer mode of audio.
Lajos Molnar1de1e252015-04-30 18:18:34 -0700999 if (mSurface != NULL) {
Robert Shih7350b052015-10-01 15:50:14 -07001000 if (instantiateDecoder(false, &mVideoDecoder) == -EWOULDBLOCK) {
1001 rescan = true;
1002 }
Haynes Mathew George5d246ef2012-07-09 10:36:57 -07001003 }
Andreas Huberf9334412010-12-15 15:17:42 -08001004
Ronghua Wua10fd232014-11-06 16:15:20 -08001005 // Don't try to re-open audio sink if there's an existing decoder.
1006 if (mAudioSink != NULL && mAudioDecoder == NULL) {
Robert Shih7350b052015-10-01 15:50:14 -07001007 if (instantiateDecoder(true, &mAudioDecoder) == -EWOULDBLOCK) {
1008 rescan = true;
1009 }
Andreas Huberf9334412010-12-15 15:17:42 -08001010 }
1011
Andreas Huberb7c8e912012-11-27 15:02:53 -08001012 if (!mHadAnySourcesBefore
1013 && (mAudioDecoder != NULL || mVideoDecoder != NULL)) {
1014 // This is the first time we've found anything playable.
1015
Andreas Huber9575c962013-02-05 13:59:56 -08001016 if (mSourceFlags & Source::FLAG_DYNAMIC_DURATION) {
Andreas Huberb7c8e912012-11-27 15:02:53 -08001017 schedulePollDuration();
1018 }
1019 }
1020
Andreas Hubereac68ba2011-09-27 12:12:25 -07001021 status_t err;
1022 if ((err = mSource->feedMoreTSData()) != OK) {
Andreas Huber1aef2112011-01-04 14:01:29 -08001023 if (mAudioDecoder == NULL && mVideoDecoder == NULL) {
1024 // We're not currently decoding anything (no audio or
1025 // video tracks found) and we just ran out of input data.
Andreas Hubereac68ba2011-09-27 12:12:25 -07001026
1027 if (err == ERROR_END_OF_STREAM) {
1028 notifyListener(MEDIA_PLAYBACK_COMPLETE, 0, 0);
1029 } else {
1030 notifyListener(MEDIA_ERROR, MEDIA_ERROR_UNKNOWN, err);
1031 }
Andreas Huber1aef2112011-01-04 14:01:29 -08001032 }
Andreas Huberf9334412010-12-15 15:17:42 -08001033 break;
1034 }
1035
Robert Shih7350b052015-10-01 15:50:14 -07001036 if (rescan) {
Andreas Huberf9334412010-12-15 15:17:42 -08001037 msg->post(100000ll);
Andreas Huber5bc087c2010-12-23 10:27:40 -08001038 mScanSourcesPending = true;
Andreas Huberf9334412010-12-15 15:17:42 -08001039 }
1040 break;
1041 }
1042
1043 case kWhatVideoNotify:
1044 case kWhatAudioNotify:
1045 {
1046 bool audio = msg->what() == kWhatAudioNotify;
1047
Wei Jia88703c32014-08-06 11:24:07 -07001048 int32_t currentDecoderGeneration =
1049 (audio? mAudioDecoderGeneration : mVideoDecoderGeneration);
1050 int32_t requesterGeneration = currentDecoderGeneration - 1;
1051 CHECK(msg->findInt32("generation", &requesterGeneration));
1052
1053 if (requesterGeneration != currentDecoderGeneration) {
1054 ALOGV("got message from old %s decoder, generation(%d:%d)",
1055 audio ? "audio" : "video", requesterGeneration,
1056 currentDecoderGeneration);
1057 sp<AMessage> reply;
1058 if (!(msg->findMessage("reply", &reply))) {
1059 return;
1060 }
1061
1062 reply->setInt32("err", INFO_DISCONTINUITY);
1063 reply->post();
1064 return;
1065 }
1066
Andreas Huberf9334412010-12-15 15:17:42 -08001067 int32_t what;
Lajos Molnar1cd13982014-01-17 15:12:51 -08001068 CHECK(msg->findInt32("what", &what));
Andreas Huberf9334412010-12-15 15:17:42 -08001069
Chong Zhang7137ec72014-11-12 16:41:05 -08001070 if (what == DecoderBase::kWhatInputDiscontinuity) {
1071 int32_t formatChange;
1072 CHECK(msg->findInt32("formatChange", &formatChange));
Andreas Huberf9334412010-12-15 15:17:42 -08001073
Chong Zhang7137ec72014-11-12 16:41:05 -08001074 ALOGV("%s discontinuity: formatChange %d",
1075 audio ? "audio" : "video", formatChange);
1076
1077 if (formatChange) {
1078 mDeferredActions.push_back(
1079 new FlushDecoderAction(
1080 audio ? FLUSH_CMD_SHUTDOWN : FLUSH_CMD_NONE,
1081 audio ? FLUSH_CMD_NONE : FLUSH_CMD_SHUTDOWN));
Andreas Huberf9334412010-12-15 15:17:42 -08001082 }
Chong Zhang7137ec72014-11-12 16:41:05 -08001083
1084 mDeferredActions.push_back(
1085 new SimpleAction(
1086 &NuPlayer::performScanSources));
1087
1088 processDeferredActions();
1089 } else if (what == DecoderBase::kWhatEOS) {
Andreas Huberdc9bacd2011-09-26 10:53:29 -07001090 int32_t err;
Lajos Molnar1cd13982014-01-17 15:12:51 -08001091 CHECK(msg->findInt32("err", &err));
Andreas Huberdc9bacd2011-09-26 10:53:29 -07001092
1093 if (err == ERROR_END_OF_STREAM) {
Steve Block3856b092011-10-20 11:56:00 +01001094 ALOGV("got %s decoder EOS", audio ? "audio" : "video");
Andreas Huberdc9bacd2011-09-26 10:53:29 -07001095 } else {
Steve Block3856b092011-10-20 11:56:00 +01001096 ALOGV("got %s decoder EOS w/ error %d",
Andreas Huberdc9bacd2011-09-26 10:53:29 -07001097 audio ? "audio" : "video",
1098 err);
1099 }
1100
1101 mRenderer->queueEOS(audio, err);
Chong Zhang7137ec72014-11-12 16:41:05 -08001102 } else if (what == DecoderBase::kWhatFlushCompleted) {
Steve Block3856b092011-10-20 11:56:00 +01001103 ALOGV("decoder %s flush completed", audio ? "audio" : "video");
Andreas Huberf9334412010-12-15 15:17:42 -08001104
Andy Hung8d121d42014-10-03 09:53:53 -07001105 handleFlushComplete(audio, true /* isDecoder */);
Andreas Huber3831a062010-12-21 10:22:33 -08001106 finishFlushIfPossible();
Chong Zhang7137ec72014-11-12 16:41:05 -08001107 } else if (what == DecoderBase::kWhatVideoSizeChanged) {
Lajos Molnar1cd13982014-01-17 15:12:51 -08001108 sp<AMessage> format;
1109 CHECK(msg->findMessage("format", &format));
1110
Wei Jiac6cfd702014-11-11 16:33:20 -08001111 sp<AMessage> inputFormat =
1112 mSource->getFormat(false /* audio */);
Andreas Huber3831a062010-12-21 10:22:33 -08001113
Bartosz Dolewski26936f72014-12-11 12:47:08 +01001114 setVideoScalingMode(mVideoScalingMode);
Wei Jiac6cfd702014-11-11 16:33:20 -08001115 updateVideoSize(inputFormat, format);
Chong Zhang7137ec72014-11-12 16:41:05 -08001116 } else if (what == DecoderBase::kWhatShutdownCompleted) {
Steve Block3856b092011-10-20 11:56:00 +01001117 ALOGV("%s shutdown completed", audio ? "audio" : "video");
Andreas Huber3831a062010-12-21 10:22:33 -08001118 if (audio) {
1119 mAudioDecoder.clear();
Wei Jia686e8e52017-04-03 14:08:01 -07001120 mAudioDecoderError = false;
Wei Jia57568df2014-09-22 10:16:29 -07001121 ++mAudioDecoderGeneration;
Andreas Huber3831a062010-12-21 10:22:33 -08001122
1123 CHECK_EQ((int)mFlushingAudio, (int)SHUTTING_DOWN_DECODER);
1124 mFlushingAudio = SHUT_DOWN;
1125 } else {
1126 mVideoDecoder.clear();
Wei Jia686e8e52017-04-03 14:08:01 -07001127 mVideoDecoderError = false;
Wei Jia57568df2014-09-22 10:16:29 -07001128 ++mVideoDecoderGeneration;
Andreas Huber3831a062010-12-21 10:22:33 -08001129
1130 CHECK_EQ((int)mFlushingVideo, (int)SHUTTING_DOWN_DECODER);
1131 mFlushingVideo = SHUT_DOWN;
1132 }
1133
1134 finishFlushIfPossible();
Chong Zhangf8d71772014-11-26 15:08:34 -08001135 } else if (what == DecoderBase::kWhatResumeCompleted) {
1136 finishResume();
Chong Zhang7137ec72014-11-12 16:41:05 -08001137 } else if (what == DecoderBase::kWhatError) {
Chong Zhangf4c0a942014-08-11 15:14:10 -07001138 status_t err;
Andy Hung2abde2c2014-09-30 14:40:32 -07001139 if (!msg->findInt32("err", &err) || err == OK) {
Chong Zhangf4c0a942014-08-11 15:14:10 -07001140 err = UNKNOWN_ERROR;
1141 }
Andy Hungcf31f1e2014-09-23 14:59:01 -07001142
Andy Hung2abde2c2014-09-30 14:40:32 -07001143 // Decoder errors can be due to Source (e.g. from streaming),
1144 // or from decoding corrupted bitstreams, or from other decoder
1145 // MediaCodec operations (e.g. from an ongoing reset or seek).
Andy Hung202bce12014-12-03 11:47:36 -08001146 // They may also be due to openAudioSink failure at
1147 // decoder start or after a format change.
Andy Hung2abde2c2014-09-30 14:40:32 -07001148 //
1149 // We try to gracefully shut down the affected decoder if possible,
1150 // rather than trying to force the shutdown with something
1151 // similar to performReset(). This method can lead to a hang
1152 // if MediaCodec functions block after an error, but they should
1153 // typically return INVALID_OPERATION instead of blocking.
1154
1155 FlushStatus *flushing = audio ? &mFlushingAudio : &mFlushingVideo;
1156 ALOGE("received error(%#x) from %s decoder, flushing(%d), now shutting down",
1157 err, audio ? "audio" : "video", *flushing);
1158
1159 switch (*flushing) {
1160 case NONE:
1161 mDeferredActions.push_back(
Wei Jiafef808d2014-10-31 17:57:05 -07001162 new FlushDecoderAction(
1163 audio ? FLUSH_CMD_SHUTDOWN : FLUSH_CMD_NONE,
1164 audio ? FLUSH_CMD_NONE : FLUSH_CMD_SHUTDOWN));
Andy Hung2abde2c2014-09-30 14:40:32 -07001165 processDeferredActions();
1166 break;
1167 case FLUSHING_DECODER:
1168 *flushing = FLUSHING_DECODER_SHUTDOWN; // initiate shutdown after flush.
1169 break; // Wait for flush to complete.
1170 case FLUSHING_DECODER_SHUTDOWN:
1171 break; // Wait for flush to complete.
1172 case SHUTTING_DOWN_DECODER:
1173 break; // Wait for shutdown to complete.
1174 case FLUSHED:
Andy Hung2abde2c2014-09-30 14:40:32 -07001175 getDecoder(audio)->initiateShutdown(); // In the middle of a seek.
1176 *flushing = SHUTTING_DOWN_DECODER; // Shut down.
1177 break;
1178 case SHUT_DOWN:
1179 finishFlushIfPossible(); // Should not occur.
1180 break; // Finish anyways.
Marco Nelissen9e2b7912014-08-18 16:13:03 -07001181 }
Wei Jia686e8e52017-04-03 14:08:01 -07001182 if (mSource != nullptr) {
1183 if (audio) {
Wei Jia2f6d8c52017-04-11 09:50:31 -07001184 if (mVideoDecoderError || mSource->getFormat(false /* audio */) == NULL
Wei Jiaf86731d2017-07-11 17:24:34 -07001185 || mSurface == NULL || mVideoDecoder == NULL) {
Wei Jia686e8e52017-04-03 14:08:01 -07001186 // When both audio and video have error, or this stream has only audio
1187 // which has error, notify client of error.
1188 notifyListener(MEDIA_ERROR, MEDIA_ERROR_UNKNOWN, err);
1189 } else {
1190 // Only audio track has error. Video track could be still good to play.
1191 notifyListener(MEDIA_INFO, MEDIA_INFO_PLAY_AUDIO_ERROR, err);
1192 }
1193 mAudioDecoderError = true;
1194 } else {
Wei Jia2f6d8c52017-04-11 09:50:31 -07001195 if (mAudioDecoderError || mSource->getFormat(true /* audio */) == NULL
Wei Jiaf86731d2017-07-11 17:24:34 -07001196 || mAudioSink == NULL || mAudioDecoder == NULL) {
Wei Jia686e8e52017-04-03 14:08:01 -07001197 // When both audio and video have error, or this stream has only video
1198 // which has error, notify client of error.
1199 notifyListener(MEDIA_ERROR, MEDIA_ERROR_UNKNOWN, err);
1200 } else {
1201 // Only video track has error. Audio track could be still good to play.
1202 notifyListener(MEDIA_INFO, MEDIA_INFO_PLAY_VIDEO_ERROR, err);
1203 }
1204 mVideoDecoderError = true;
1205 }
1206 }
Lajos Molnar1cd13982014-01-17 15:12:51 -08001207 } else {
1208 ALOGV("Unhandled decoder notification %d '%c%c%c%c'.",
Andreas Hubera1f8ab02012-11-30 10:53:22 -08001209 what,
1210 what >> 24,
1211 (what >> 16) & 0xff,
1212 (what >> 8) & 0xff,
1213 what & 0xff);
Andreas Huberf9334412010-12-15 15:17:42 -08001214 }
1215
1216 break;
1217 }
1218
1219 case kWhatRendererNotify:
1220 {
Wei Jia57568df2014-09-22 10:16:29 -07001221 int32_t requesterGeneration = mRendererGeneration - 1;
1222 CHECK(msg->findInt32("generation", &requesterGeneration));
1223 if (requesterGeneration != mRendererGeneration) {
1224 ALOGV("got message from old renderer, generation(%d:%d)",
1225 requesterGeneration, mRendererGeneration);
1226 return;
1227 }
1228
Andreas Huberf9334412010-12-15 15:17:42 -08001229 int32_t what;
1230 CHECK(msg->findInt32("what", &what));
1231
1232 if (what == Renderer::kWhatEOS) {
1233 int32_t audio;
1234 CHECK(msg->findInt32("audio", &audio));
1235
Andreas Huberc92fd242011-08-16 13:48:44 -07001236 int32_t finalResult;
1237 CHECK(msg->findInt32("finalResult", &finalResult));
1238
Andreas Huberf9334412010-12-15 15:17:42 -08001239 if (audio) {
1240 mAudioEOS = true;
1241 } else {
1242 mVideoEOS = true;
1243 }
1244
Andreas Huberc92fd242011-08-16 13:48:44 -07001245 if (finalResult == ERROR_END_OF_STREAM) {
Steve Block3856b092011-10-20 11:56:00 +01001246 ALOGV("reached %s EOS", audio ? "audio" : "video");
Andreas Huberc92fd242011-08-16 13:48:44 -07001247 } else {
Steve Block29357bc2012-01-06 19:20:56 +00001248 ALOGE("%s track encountered an error (%d)",
Andreas Huberc92fd242011-08-16 13:48:44 -07001249 audio ? "audio" : "video", finalResult);
1250
1251 notifyListener(
1252 MEDIA_ERROR, MEDIA_ERROR_UNKNOWN, finalResult);
1253 }
Andreas Huberf9334412010-12-15 15:17:42 -08001254
1255 if ((mAudioEOS || mAudioDecoder == NULL)
1256 && (mVideoEOS || mVideoDecoder == NULL)) {
1257 notifyListener(MEDIA_PLAYBACK_COMPLETE, 0, 0);
1258 }
Andreas Huber3fe62152011-09-16 15:09:22 -07001259 } else if (what == Renderer::kWhatFlushComplete) {
Andreas Huberf9334412010-12-15 15:17:42 -08001260 int32_t audio;
1261 CHECK(msg->findInt32("audio", &audio));
1262
Wei Jia3261f0d2015-10-08 13:58:33 -07001263 if (audio) {
1264 mAudioEOS = false;
1265 } else {
1266 mVideoEOS = false;
1267 }
1268
Steve Block3856b092011-10-20 11:56:00 +01001269 ALOGV("renderer %s flush completed.", audio ? "audio" : "video");
Wei Jiada4252f2015-07-14 18:15:28 -07001270 if (audio && (mFlushingAudio == NONE || mFlushingAudio == FLUSHED
1271 || mFlushingAudio == SHUT_DOWN)) {
1272 // Flush has been handled by tear down.
1273 break;
1274 }
Andy Hung8d121d42014-10-03 09:53:53 -07001275 handleFlushComplete(audio, false /* isDecoder */);
1276 finishFlushIfPossible();
James Dongf57b4ea2012-07-20 13:38:36 -07001277 } else if (what == Renderer::kWhatVideoRenderingStart) {
1278 notifyListener(MEDIA_INFO, MEDIA_INFO_RENDERING_START, 0);
Lajos Molnarcbaffcf2013-08-14 18:30:38 -07001279 } else if (what == Renderer::kWhatMediaRenderingStart) {
1280 ALOGV("media rendering started");
1281 notifyListener(MEDIA_STARTED, 0, 0);
Ronghua Wufaeb0f22015-05-21 12:20:21 -07001282 } else if (what == Renderer::kWhatAudioTearDown) {
Ronghua Wu08529172014-10-02 16:55:52 -07001283 int32_t reason;
1284 CHECK(msg->findInt32("reason", &reason));
Ronghua Wufaeb0f22015-05-21 12:20:21 -07001285 ALOGV("Tear down audio with reason %d.", reason);
Wei Jia8456ddd2016-04-22 14:46:43 -07001286 if (reason == Renderer::kDueToTimeout && !(mPaused && mOffloadAudio)) {
1287 // TimeoutWhenPaused is only for offload mode.
1288 ALOGW("Receive a stale message for teardown.");
1289 break;
1290 }
Wei Jiada4252f2015-07-14 18:15:28 -07001291 int64_t positionUs;
Robert Shih1a5c8592015-08-04 18:07:44 -07001292 if (!msg->findInt64("positionUs", &positionUs)) {
1293 positionUs = mPreviousSeekTimeUs;
1294 }
Ronghua Wufaeb0f22015-05-21 12:20:21 -07001295
Wei Jia5031b2f2016-02-25 11:19:31 -08001296 restartAudio(
Wei Jiaa05f1e32016-03-25 16:31:22 -07001297 positionUs, reason == Renderer::kForceNonOffload /* forceNonOffload */,
1298 reason != Renderer::kDueToTimeout /* needsToCreateAudioDecoder */);
Andreas Huberf9334412010-12-15 15:17:42 -08001299 }
1300 break;
1301 }
1302
1303 case kWhatMoreDataQueued:
1304 {
1305 break;
1306 }
1307
Andreas Huber1aef2112011-01-04 14:01:29 -08001308 case kWhatReset:
1309 {
Steve Block3856b092011-10-20 11:56:00 +01001310 ALOGV("kWhatReset");
Andreas Huber1aef2112011-01-04 14:01:29 -08001311
Ronghua Wu64c2d172015-10-07 16:52:19 -07001312 mResetting = true;
Ray Essick0d98c182017-11-09 13:42:42 -08001313 stopPlaybackTimer("kWhatReset");
Ronghua Wu64c2d172015-10-07 16:52:19 -07001314
Andreas Hubera1f8ab02012-11-30 10:53:22 -08001315 mDeferredActions.push_back(
Wei Jiafef808d2014-10-31 17:57:05 -07001316 new FlushDecoderAction(
1317 FLUSH_CMD_SHUTDOWN /* audio */,
1318 FLUSH_CMD_SHUTDOWN /* video */));
Andreas Huberb7c8e912012-11-27 15:02:53 -08001319
Andreas Hubera1f8ab02012-11-30 10:53:22 -08001320 mDeferredActions.push_back(
1321 new SimpleAction(&NuPlayer::performReset));
Andreas Huberb58ce9f2011-11-28 16:27:35 -08001322
Andreas Hubera1f8ab02012-11-30 10:53:22 -08001323 processDeferredActions();
Andreas Huber1aef2112011-01-04 14:01:29 -08001324 break;
1325 }
1326
Wei Jia52c28512017-09-13 18:17:51 -07001327 case kWhatNotifyTime:
1328 {
1329 ALOGV("kWhatNotifyTime");
1330 int64_t timerUs;
1331 CHECK(msg->findInt64("timerUs", &timerUs));
1332
1333 notifyListener(MEDIA_NOTIFY_TIME, timerUs, 0);
1334 break;
1335 }
1336
Andreas Huber43c3e6c2011-01-05 12:17:08 -08001337 case kWhatSeek:
1338 {
1339 int64_t seekTimeUs;
Wei Jiac5de0912016-11-18 10:22:14 -08001340 int32_t mode;
Wei Jiae427abf2014-09-22 15:21:11 -07001341 int32_t needNotify;
Andreas Huber43c3e6c2011-01-05 12:17:08 -08001342 CHECK(msg->findInt64("seekTimeUs", &seekTimeUs));
Wei Jiac5de0912016-11-18 10:22:14 -08001343 CHECK(msg->findInt32("mode", &mode));
Wei Jiae427abf2014-09-22 15:21:11 -07001344 CHECK(msg->findInt32("needNotify", &needNotify));
Andreas Huber43c3e6c2011-01-05 12:17:08 -08001345
Wei Jiac5de0912016-11-18 10:22:14 -08001346 ALOGV("kWhatSeek seekTimeUs=%lld us, mode=%d, needNotify=%d",
1347 (long long)seekTimeUs, mode, needNotify);
Andreas Huber43c3e6c2011-01-05 12:17:08 -08001348
Wei Jia1061c9c2015-05-19 16:02:17 -07001349 if (!mStarted) {
1350 // Seek before the player is started. In order to preview video,
1351 // need to start the player and pause it. This branch is called
1352 // only once if needed. After the player is started, any seek
1353 // operation will go through normal path.
Robert Shih0c61a0d2015-07-06 15:09:10 -07001354 // Audio-only cases are handled separately.
Wei Jiac5de0912016-11-18 10:22:14 -08001355 onStart(seekTimeUs, (MediaPlayerSeekMode)mode);
Robert Shih0c61a0d2015-07-06 15:09:10 -07001356 if (mStarted) {
1357 onPause();
1358 mPausedByClient = true;
1359 }
Wei Jia1061c9c2015-05-19 16:02:17 -07001360 if (needNotify) {
1361 notifyDriverSeekComplete();
1362 }
1363 break;
1364 }
1365
Andreas Hubera1f8ab02012-11-30 10:53:22 -08001366 mDeferredActions.push_back(
Wei Jiafef808d2014-10-31 17:57:05 -07001367 new FlushDecoderAction(FLUSH_CMD_FLUSH /* audio */,
1368 FLUSH_CMD_FLUSH /* video */));
Andreas Huber43c3e6c2011-01-05 12:17:08 -08001369
Wei Jiae427abf2014-09-22 15:21:11 -07001370 mDeferredActions.push_back(
Wei Jiac5de0912016-11-18 10:22:14 -08001371 new SeekAction(seekTimeUs, (MediaPlayerSeekMode)mode));
Andreas Huber43c3e6c2011-01-05 12:17:08 -08001372
Chong Zhangf8d71772014-11-26 15:08:34 -08001373 // After a flush without shutdown, decoder is paused.
1374 // Don't resume it until source seek is done, otherwise it could
Chong Zhang7137ec72014-11-12 16:41:05 -08001375 // start pulling stale data too soon.
1376 mDeferredActions.push_back(
Chong Zhangf8d71772014-11-26 15:08:34 -08001377 new ResumeDecoderAction(needNotify));
Chong Zhang7137ec72014-11-12 16:41:05 -08001378
Andreas Hubera1f8ab02012-11-30 10:53:22 -08001379 processDeferredActions();
Andreas Huber43c3e6c2011-01-05 12:17:08 -08001380 break;
1381 }
1382
Andreas Huberb4082222011-01-20 15:23:04 -08001383 case kWhatPause:
1384 {
Chong Zhangefbb6192015-01-30 17:13:27 -08001385 onPause();
1386 mPausedByClient = true;
Andreas Huberb4082222011-01-20 15:23:04 -08001387 break;
1388 }
1389
Andreas Huberb5f25f02013-02-05 10:14:26 -08001390 case kWhatSourceNotify:
1391 {
Andreas Huber9575c962013-02-05 13:59:56 -08001392 onSourceNotify(msg);
Andreas Huberb5f25f02013-02-05 10:14:26 -08001393 break;
1394 }
1395
Chong Zhanga7fa1d92014-06-11 14:49:23 -07001396 case kWhatClosedCaptionNotify:
1397 {
1398 onClosedCaptionNotify(msg);
1399 break;
1400 }
1401
Hassan Shojaniacefac142017-02-06 21:02:02 -08001402 case kWhatPrepareDrm:
1403 {
1404 status_t status = onPrepareDrm(msg);
1405
1406 sp<AMessage> response = new AMessage;
1407 response->setInt32("status", status);
1408 sp<AReplyToken> replyID;
1409 CHECK(msg->senderAwaitsResponse(&replyID));
1410 response->postReply(replyID);
1411 break;
1412 }
1413
1414 case kWhatReleaseDrm:
1415 {
1416 status_t status = onReleaseDrm();
1417
1418 sp<AMessage> response = new AMessage;
1419 response->setInt32("status", status);
1420 sp<AReplyToken> replyID;
1421 CHECK(msg->senderAwaitsResponse(&replyID));
1422 response->postReply(replyID);
1423 break;
1424 }
1425
Andreas Huberf9334412010-12-15 15:17:42 -08001426 default:
1427 TRESPASS();
1428 break;
1429 }
1430}
1431
Wei Jia94211742014-10-28 17:09:06 -07001432void NuPlayer::onResume() {
Ronghua Wu64c2d172015-10-07 16:52:19 -07001433 if (!mPaused || mResetting) {
1434 ALOGD_IF(mResetting, "resetting, onResume discarded");
Chong Zhangefbb6192015-01-30 17:13:27 -08001435 return;
1436 }
1437 mPaused = false;
Wei Jia94211742014-10-28 17:09:06 -07001438 if (mSource != NULL) {
1439 mSource->resume();
1440 } else {
1441 ALOGW("resume called when source is gone or not set");
1442 }
1443 // |mAudioDecoder| may have been released due to the pause timeout, so re-create it if
1444 // needed.
1445 if (audioDecoderStillNeeded() && mAudioDecoder == NULL) {
1446 instantiateDecoder(true /* audio */, &mAudioDecoder);
1447 }
1448 if (mRenderer != NULL) {
1449 mRenderer->resume();
1450 } else {
1451 ALOGW("resume called when renderer is gone or not set");
1452 }
Ray Essickd4d00612017-01-03 09:36:27 -08001453
Ray Essick0d98c182017-11-09 13:42:42 -08001454 startPlaybackTimer("onresume");
Wei Jia94211742014-10-28 17:09:06 -07001455}
1456
Lajos Molnarfcd3e942015-03-31 10:06:48 -07001457status_t NuPlayer::onInstantiateSecureDecoders() {
1458 status_t err;
1459 if (!(mSourceFlags & Source::FLAG_SECURE)) {
1460 return BAD_TYPE;
1461 }
1462
1463 if (mRenderer != NULL) {
1464 ALOGE("renderer should not be set when instantiating secure decoders");
1465 return UNKNOWN_ERROR;
1466 }
1467
1468 // TRICKY: We rely on mRenderer being null, so that decoder does not start requesting
1469 // data on instantiation.
Lajos Molnar1de1e252015-04-30 18:18:34 -07001470 if (mSurface != NULL) {
Lajos Molnarfcd3e942015-03-31 10:06:48 -07001471 err = instantiateDecoder(false, &mVideoDecoder);
1472 if (err != OK) {
1473 return err;
1474 }
1475 }
1476
1477 if (mAudioSink != NULL) {
1478 err = instantiateDecoder(true, &mAudioDecoder);
1479 if (err != OK) {
1480 return err;
1481 }
1482 }
1483 return OK;
1484}
1485
Wei Jiac5de0912016-11-18 10:22:14 -08001486void NuPlayer::onStart(int64_t startPositionUs, MediaPlayerSeekMode mode) {
Hassan Shojaniacefac142017-02-06 21:02:02 -08001487 ALOGV("onStart: mCrypto: %p (%d)", mCrypto.get(),
1488 (mCrypto != NULL ? mCrypto->getStrongCount() : 0));
1489
Robert Shih0c61a0d2015-07-06 15:09:10 -07001490 if (!mSourceStarted) {
1491 mSourceStarted = true;
1492 mSource->start();
1493 }
1494 if (startPositionUs > 0) {
Wei Jiac5de0912016-11-18 10:22:14 -08001495 performSeek(startPositionUs, mode);
Robert Shih0c61a0d2015-07-06 15:09:10 -07001496 if (mSource->getFormat(false /* audio */) == NULL) {
1497 return;
1498 }
1499 }
1500
Wei Jia94211742014-10-28 17:09:06 -07001501 mOffloadAudio = false;
1502 mAudioEOS = false;
1503 mVideoEOS = false;
Wei Jia94211742014-10-28 17:09:06 -07001504 mStarted = true;
Wei Jiafe8fe7d2016-06-08 10:29:25 -07001505 mPaused = false;
Wei Jia94211742014-10-28 17:09:06 -07001506
Wei Jia94211742014-10-28 17:09:06 -07001507 uint32_t flags = 0;
1508
1509 if (mSource->isRealTime()) {
1510 flags |= Renderer::FLAG_REAL_TIME;
1511 }
1512
Wei Jia9737d342016-12-28 14:59:59 -08001513 bool hasAudio = (mSource->getFormat(true /* audio */) != NULL);
1514 bool hasVideo = (mSource->getFormat(false /* audio */) != NULL);
1515 if (!hasAudio && !hasVideo) {
Wei Jia1de83d52016-10-10 10:15:03 -07001516 ALOGE("no metadata for either audio or video source");
1517 mSource->stop();
1518 mSourceStarted = false;
1519 notifyListener(MEDIA_ERROR, MEDIA_ERROR_UNKNOWN, ERROR_MALFORMED);
1520 return;
1521 }
Wei Jia9737d342016-12-28 14:59:59 -08001522 ALOGV_IF(!hasAudio, "no metadata for audio source"); // video only stream
1523
1524 sp<MetaData> audioMeta = mSource->getFormatMeta(true /* audio */);
Wei Jia1de83d52016-10-10 10:15:03 -07001525
Wei Jia94211742014-10-28 17:09:06 -07001526 audio_stream_type_t streamType = AUDIO_STREAM_MUSIC;
1527 if (mAudioSink != NULL) {
1528 streamType = mAudioSink->getAudioStreamType();
1529 }
1530
Wei Jia94211742014-10-28 17:09:06 -07001531 mOffloadAudio =
Wei Jia9737d342016-12-28 14:59:59 -08001532 canOffloadStream(audioMeta, hasVideo, mSource->isStreaming(), streamType)
Wei Jiabf70feb2016-02-19 15:47:16 -08001533 && (mPlaybackSettings.mSpeed == 1.f && mPlaybackSettings.mPitch == 1.f);
Hassan Shojaniacefac142017-02-06 21:02:02 -08001534
1535 // Modular DRM: Disabling audio offload if the source is protected
1536 if (mOffloadAudio && mIsDrmProtected) {
1537 mOffloadAudio = false;
1538 ALOGV("onStart: Disabling mOffloadAudio now that the source is protected.");
1539 }
1540
Wei Jia94211742014-10-28 17:09:06 -07001541 if (mOffloadAudio) {
1542 flags |= Renderer::FLAG_OFFLOAD_AUDIO;
1543 }
1544
Lajos Molnar1d15ab52015-03-04 16:46:34 -08001545 sp<AMessage> notify = new AMessage(kWhatRendererNotify, this);
Wei Jia94211742014-10-28 17:09:06 -07001546 ++mRendererGeneration;
1547 notify->setInt32("generation", mRendererGeneration);
Wei Jia0a68f662017-08-30 18:01:26 -07001548 mRenderer = new Renderer(mAudioSink, mMediaClock, notify, flags);
Wei Jia94211742014-10-28 17:09:06 -07001549 mRendererLooper = new ALooper;
1550 mRendererLooper->setName("NuPlayerRenderer");
1551 mRendererLooper->start(false, false, ANDROID_PRIORITY_AUDIO);
1552 mRendererLooper->registerHandler(mRenderer);
Lajos Molnar3a474aa2015-04-24 17:10:07 -07001553
1554 status_t err = mRenderer->setPlaybackSettings(mPlaybackSettings);
1555 if (err != OK) {
1556 mSource->stop();
Robert Shih0c61a0d2015-07-06 15:09:10 -07001557 mSourceStarted = false;
Lajos Molnar3a474aa2015-04-24 17:10:07 -07001558 notifyListener(MEDIA_ERROR, MEDIA_ERROR_UNKNOWN, err);
1559 return;
Wei Jiac8206ff2015-03-04 13:59:37 -08001560 }
Wei Jia94211742014-10-28 17:09:06 -07001561
Ronghua Wuc8a70d32015-04-29 16:26:34 -07001562 float rate = getFrameRate();
1563 if (rate > 0) {
Wei Jia94211742014-10-28 17:09:06 -07001564 mRenderer->setVideoFrameRate(rate);
1565 }
1566
Wei Jiac6cfd702014-11-11 16:33:20 -08001567 if (mVideoDecoder != NULL) {
1568 mVideoDecoder->setRenderer(mRenderer);
1569 }
1570 if (mAudioDecoder != NULL) {
1571 mAudioDecoder->setRenderer(mRenderer);
1572 }
1573
Ray Essick0d98c182017-11-09 13:42:42 -08001574 startPlaybackTimer("onstart");
Ray Essickd4d00612017-01-03 09:36:27 -08001575
Wei Jia94211742014-10-28 17:09:06 -07001576 postScanSources();
1577}
1578
Ray Essick0d98c182017-11-09 13:42:42 -08001579void NuPlayer::startPlaybackTimer(const char *where) {
1580 Mutex::Autolock autoLock(mPlayingTimeLock);
1581 if (mLastStartedPlayingTimeNs == 0) {
1582 mLastStartedPlayingTimeNs = systemTime();
1583 ALOGV("startPlaybackTimer() time %20" PRId64 " (%s)", mLastStartedPlayingTimeNs, where);
1584 }
1585}
1586
1587void NuPlayer::stopPlaybackTimer(const char *where) {
1588 Mutex::Autolock autoLock(mPlayingTimeLock);
1589
1590 ALOGV("stopPlaybackTimer() time %20" PRId64 " (%s)", mLastStartedPlayingTimeNs, where);
1591
1592 if (mLastStartedPlayingTimeNs != 0) {
1593 sp<NuPlayerDriver> driver = mDriver.promote();
1594 if (driver != NULL) {
1595 int64_t now = systemTime();
1596 int64_t played = now - mLastStartedPlayingTimeNs;
1597 ALOGV("stopPlaybackTimer() log %20" PRId64 "", played);
1598
1599 if (played > 0) {
1600 driver->notifyMorePlayingTimeUs((played+500)/1000);
1601 }
1602 }
1603 mLastStartedPlayingTimeNs = 0;
1604 }
1605}
1606
Chong Zhangefbb6192015-01-30 17:13:27 -08001607void NuPlayer::onPause() {
Ray Essick0d98c182017-11-09 13:42:42 -08001608
1609 stopPlaybackTimer("onPause");
1610
Chong Zhangefbb6192015-01-30 17:13:27 -08001611 if (mPaused) {
1612 return;
1613 }
1614 mPaused = true;
1615 if (mSource != NULL) {
1616 mSource->pause();
1617 } else {
1618 ALOGW("pause called when source is gone or not set");
1619 }
1620 if (mRenderer != NULL) {
1621 mRenderer->pause();
1622 } else {
1623 ALOGW("pause called when renderer is gone or not set");
1624 }
Ray Essickd4d00612017-01-03 09:36:27 -08001625
Chong Zhangefbb6192015-01-30 17:13:27 -08001626}
1627
Ronghua Wud7988b12014-10-03 15:19:10 -07001628bool NuPlayer::audioDecoderStillNeeded() {
1629 // Audio decoder is no longer needed if it's in shut/shutting down status.
1630 return ((mFlushingAudio != SHUT_DOWN) && (mFlushingAudio != SHUTTING_DOWN_DECODER));
1631}
1632
Andy Hung8d121d42014-10-03 09:53:53 -07001633void NuPlayer::handleFlushComplete(bool audio, bool isDecoder) {
1634 // We wait for both the decoder flush and the renderer flush to complete
1635 // before entering either the FLUSHED or the SHUTTING_DOWN_DECODER state.
1636
1637 mFlushComplete[audio][isDecoder] = true;
1638 if (!mFlushComplete[audio][!isDecoder]) {
1639 return;
1640 }
1641
1642 FlushStatus *state = audio ? &mFlushingAudio : &mFlushingVideo;
1643 switch (*state) {
1644 case FLUSHING_DECODER:
1645 {
1646 *state = FLUSHED;
Andy Hung8d121d42014-10-03 09:53:53 -07001647 break;
1648 }
1649
1650 case FLUSHING_DECODER_SHUTDOWN:
1651 {
1652 *state = SHUTTING_DOWN_DECODER;
1653
1654 ALOGV("initiating %s decoder shutdown", audio ? "audio" : "video");
Andy Hung8d121d42014-10-03 09:53:53 -07001655 getDecoder(audio)->initiateShutdown();
1656 break;
1657 }
1658
1659 default:
1660 // decoder flush completes only occur in a flushing state.
1661 LOG_ALWAYS_FATAL_IF(isDecoder, "decoder flush in invalid state %d", *state);
1662 break;
1663 }
1664}
1665
Andreas Huber3831a062010-12-21 10:22:33 -08001666void NuPlayer::finishFlushIfPossible() {
Wei Jia53904f32014-07-29 10:22:53 -07001667 if (mFlushingAudio != NONE && mFlushingAudio != FLUSHED
1668 && mFlushingAudio != SHUT_DOWN) {
Andreas Huber3831a062010-12-21 10:22:33 -08001669 return;
1670 }
1671
Wei Jia53904f32014-07-29 10:22:53 -07001672 if (mFlushingVideo != NONE && mFlushingVideo != FLUSHED
1673 && mFlushingVideo != SHUT_DOWN) {
Andreas Huber3831a062010-12-21 10:22:33 -08001674 return;
1675 }
1676
Steve Block3856b092011-10-20 11:56:00 +01001677 ALOGV("both audio and video are flushed now.");
Andreas Huber3831a062010-12-21 10:22:33 -08001678
Andreas Huber3831a062010-12-21 10:22:33 -08001679 mFlushingAudio = NONE;
1680 mFlushingVideo = NONE;
Andreas Huber3831a062010-12-21 10:22:33 -08001681
Andy Hung8d121d42014-10-03 09:53:53 -07001682 clearFlushComplete();
1683
Andreas Hubera1f8ab02012-11-30 10:53:22 -08001684 processDeferredActions();
Andreas Huber1aef2112011-01-04 14:01:29 -08001685}
1686
1687void NuPlayer::postScanSources() {
1688 if (mScanSourcesPending) {
1689 return;
1690 }
1691
Lajos Molnar1d15ab52015-03-04 16:46:34 -08001692 sp<AMessage> msg = new AMessage(kWhatScanSources, this);
Andreas Huber1aef2112011-01-04 14:01:29 -08001693 msg->setInt32("generation", mScanSourcesGeneration);
1694 msg->post();
1695
1696 mScanSourcesPending = true;
1697}
1698
Wei Jia41cd4632016-05-13 10:53:30 -07001699void NuPlayer::tryOpenAudioSinkForOffload(
1700 const sp<AMessage> &format, const sp<MetaData> &audioMeta, bool hasVideo) {
Andy Hung202bce12014-12-03 11:47:36 -08001701 // Note: This is called early in NuPlayer to determine whether offloading
1702 // is possible; otherwise the decoders call the renderer openAudioSink directly.
Andy Hung282a7e32014-08-14 15:56:34 -07001703
Andy Hung202bce12014-12-03 11:47:36 -08001704 status_t err = mRenderer->openAudioSink(
Dhananjay Kumarc387f2b2015-08-06 10:43:16 +05301705 format, true /* offloadOnly */, hasVideo,
1706 AUDIO_OUTPUT_FLAG_NONE, &mOffloadAudio, mSource->isStreaming());
Andy Hung202bce12014-12-03 11:47:36 -08001707 if (err != OK) {
1708 // Any failure we turn off mOffloadAudio.
1709 mOffloadAudio = false;
1710 } else if (mOffloadAudio) {
Chong Zhang3b9eb1f2014-10-15 17:05:08 -07001711 sendMetaDataToHal(mAudioSink, audioMeta);
Andy Hung282a7e32014-08-14 15:56:34 -07001712 }
1713}
1714
1715void NuPlayer::closeAudioSink() {
Chong Zhang3b9eb1f2014-10-15 17:05:08 -07001716 mRenderer->closeAudioSink();
Andy Hung282a7e32014-08-14 15:56:34 -07001717}
1718
Wei Jia5031b2f2016-02-25 11:19:31 -08001719void NuPlayer::restartAudio(
Wei Jiabf70feb2016-02-19 15:47:16 -08001720 int64_t currentPositionUs, bool forceNonOffload, bool needsToCreateAudioDecoder) {
Wei Jiaa05f1e32016-03-25 16:31:22 -07001721 if (mAudioDecoder != NULL) {
1722 mAudioDecoder->pause();
1723 mAudioDecoder.clear();
Wei Jia686e8e52017-04-03 14:08:01 -07001724 mAudioDecoderError = false;
Wei Jiaa05f1e32016-03-25 16:31:22 -07001725 ++mAudioDecoderGeneration;
1726 }
Wei Jiabf70feb2016-02-19 15:47:16 -08001727 if (mFlushingAudio == FLUSHING_DECODER) {
1728 mFlushComplete[1 /* audio */][1 /* isDecoder */] = true;
1729 mFlushingAudio = FLUSHED;
1730 finishFlushIfPossible();
1731 } else if (mFlushingAudio == FLUSHING_DECODER_SHUTDOWN
1732 || mFlushingAudio == SHUTTING_DOWN_DECODER) {
1733 mFlushComplete[1 /* audio */][1 /* isDecoder */] = true;
1734 mFlushingAudio = SHUT_DOWN;
1735 finishFlushIfPossible();
1736 needsToCreateAudioDecoder = false;
1737 }
1738 if (mRenderer == NULL) {
1739 return;
1740 }
1741 closeAudioSink();
1742 mRenderer->flush(true /* audio */, false /* notifyComplete */);
1743 if (mVideoDecoder != NULL) {
1744 mRenderer->flush(false /* audio */, false /* notifyComplete */);
1745 }
1746
Wei Jiac5de0912016-11-18 10:22:14 -08001747 performSeek(currentPositionUs, MediaPlayerSeekMode::SEEK_PREVIOUS_SYNC /* mode */);
Wei Jiabf70feb2016-02-19 15:47:16 -08001748
1749 if (forceNonOffload) {
1750 mRenderer->signalDisableOffloadAudio();
1751 mOffloadAudio = false;
1752 }
1753 if (needsToCreateAudioDecoder) {
Wei Jiaa05f1e32016-03-25 16:31:22 -07001754 instantiateDecoder(true /* audio */, &mAudioDecoder, !forceNonOffload);
Wei Jiabf70feb2016-02-19 15:47:16 -08001755 }
1756}
1757
Wei Jia41cd4632016-05-13 10:53:30 -07001758void NuPlayer::determineAudioModeChange(const sp<AMessage> &audioFormat) {
Wei Jiae4d18c72015-07-13 17:58:11 -07001759 if (mSource == NULL || mAudioSink == NULL) {
1760 return;
1761 }
1762
1763 if (mRenderer == NULL) {
1764 ALOGW("No renderer can be used to determine audio mode. Use non-offload for safety.");
1765 mOffloadAudio = false;
1766 return;
1767 }
1768
1769 sp<MetaData> audioMeta = mSource->getFormatMeta(true /* audio */);
1770 sp<AMessage> videoFormat = mSource->getFormat(false /* audio */);
1771 audio_stream_type_t streamType = mAudioSink->getAudioStreamType();
1772 const bool hasVideo = (videoFormat != NULL);
Hassan Shojaniacefac142017-02-06 21:02:02 -08001773 bool canOffload = canOffloadStream(
Wei Jiabf70feb2016-02-19 15:47:16 -08001774 audioMeta, hasVideo, mSource->isStreaming(), streamType)
1775 && (mPlaybackSettings.mSpeed == 1.f && mPlaybackSettings.mPitch == 1.f);
Hassan Shojaniacefac142017-02-06 21:02:02 -08001776
1777 // Modular DRM: Disabling audio offload if the source is protected
1778 if (canOffload && mIsDrmProtected) {
1779 canOffload = false;
1780 ALOGV("determineAudioModeChange: Disabling mOffloadAudio b/c the source is protected.");
1781 }
1782
Wei Jiae4d18c72015-07-13 17:58:11 -07001783 if (canOffload) {
1784 if (!mOffloadAudio) {
1785 mRenderer->signalEnableOffloadAudio();
1786 }
1787 // open audio sink early under offload mode.
Wei Jia41cd4632016-05-13 10:53:30 -07001788 tryOpenAudioSinkForOffload(audioFormat, audioMeta, hasVideo);
Wei Jiae4d18c72015-07-13 17:58:11 -07001789 } else {
Robert Shihe1d70192015-07-23 17:54:13 -07001790 if (mOffloadAudio) {
1791 mRenderer->signalDisableOffloadAudio();
1792 mOffloadAudio = false;
1793 }
Wei Jiae4d18c72015-07-13 17:58:11 -07001794 }
1795}
1796
Wei Jiaa05f1e32016-03-25 16:31:22 -07001797status_t NuPlayer::instantiateDecoder(
1798 bool audio, sp<DecoderBase> *decoder, bool checkAudioModeChange) {
Wei Jia566da802015-08-27 13:59:30 -07001799 // The audio decoder could be cleared by tear down. If still in shut down
1800 // process, no need to create a new audio decoder.
1801 if (*decoder != NULL || (audio && mFlushingAudio == SHUT_DOWN)) {
Andreas Huberf9334412010-12-15 15:17:42 -08001802 return OK;
1803 }
1804
Andreas Huber84066782011-08-16 09:34:26 -07001805 sp<AMessage> format = mSource->getFormat(audio);
Andreas Huberf9334412010-12-15 15:17:42 -08001806
Andreas Huber84066782011-08-16 09:34:26 -07001807 if (format == NULL) {
Robert Shih7350b052015-10-01 15:50:14 -07001808 return UNKNOWN_ERROR;
1809 } else {
1810 status_t err;
1811 if (format->findInt32("err", &err) && err) {
1812 return err;
1813 }
Andreas Huberf9334412010-12-15 15:17:42 -08001814 }
1815
Ronghua Wu8db88132015-04-22 13:51:35 -07001816 format->setInt32("priority", 0 /* realtime */);
1817
Andreas Huber3fe62152011-09-16 15:09:22 -07001818 if (!audio) {
Andreas Huber84066782011-08-16 09:34:26 -07001819 AString mime;
1820 CHECK(format->findString("mime", &mime));
Chong Zhanga7fa1d92014-06-11 14:49:23 -07001821
Lajos Molnar1d15ab52015-03-04 16:46:34 -08001822 sp<AMessage> ccNotify = new AMessage(kWhatClosedCaptionNotify, this);
Chong Zhang341ab6e2015-02-04 13:37:18 -08001823 if (mCCDecoder == NULL) {
1824 mCCDecoder = new CCDecoder(ccNotify);
1825 }
Lajos Molnar09524832014-07-17 14:29:51 -07001826
1827 if (mSourceFlags & Source::FLAG_SECURE) {
1828 format->setInt32("secure", true);
1829 }
Chong Zhang17134602015-01-07 16:14:34 -08001830
1831 if (mSourceFlags & Source::FLAG_PROTECTED) {
1832 format->setInt32("protected", true);
1833 }
Ronghua Wu8db88132015-04-22 13:51:35 -07001834
Ronghua Wuc8a70d32015-04-29 16:26:34 -07001835 float rate = getFrameRate();
1836 if (rate > 0) {
Lajos Molnar3a474aa2015-04-24 17:10:07 -07001837 format->setFloat("operating-rate", rate * mPlaybackSettings.mSpeed);
Ronghua Wu8db88132015-04-22 13:51:35 -07001838 }
Andreas Huber3fe62152011-09-16 15:09:22 -07001839 }
1840
Wei Jiabc2fb722014-07-08 16:37:57 -07001841 if (audio) {
Lajos Molnar1d15ab52015-03-04 16:46:34 -08001842 sp<AMessage> notify = new AMessage(kWhatAudioNotify, this);
Wei Jia88703c32014-08-06 11:24:07 -07001843 ++mAudioDecoderGeneration;
1844 notify->setInt32("generation", mAudioDecoderGeneration);
1845
Wei Jiaa05f1e32016-03-25 16:31:22 -07001846 if (checkAudioModeChange) {
Wei Jia41cd4632016-05-13 10:53:30 -07001847 determineAudioModeChange(format);
Wei Jiaa05f1e32016-03-25 16:31:22 -07001848 }
Wei Jiabc2fb722014-07-08 16:37:57 -07001849 if (mOffloadAudio) {
Wei Jia14532f22015-12-29 11:28:15 -08001850 mSource->setOffloadAudio(true /* offload */);
1851
Haynes Mathew George8b635332015-03-30 17:59:47 -07001852 const bool hasVideo = (mSource->getFormat(false /*audio */) != NULL);
1853 format->setInt32("has-video", hasVideo);
Wei Jiac6cfd702014-11-11 16:33:20 -08001854 *decoder = new DecoderPassThrough(notify, mSource, mRenderer);
Hassan Shojaniacefac142017-02-06 21:02:02 -08001855 ALOGV("instantiateDecoder audio DecoderPassThrough hasVideo: %d", hasVideo);
Wei Jiabc2fb722014-07-08 16:37:57 -07001856 } else {
Wei Jia14532f22015-12-29 11:28:15 -08001857 mSource->setOffloadAudio(false /* offload */);
1858
Wei Jiaf2ae3e12016-10-27 17:10:59 -07001859 *decoder = new Decoder(notify, mSource, mPID, mUID, mRenderer);
Hassan Shojaniacefac142017-02-06 21:02:02 -08001860 ALOGV("instantiateDecoder audio Decoder");
Wei Jiabc2fb722014-07-08 16:37:57 -07001861 }
Wei Jia686e8e52017-04-03 14:08:01 -07001862 mAudioDecoderError = false;
Wei Jiabc2fb722014-07-08 16:37:57 -07001863 } else {
Lajos Molnar1d15ab52015-03-04 16:46:34 -08001864 sp<AMessage> notify = new AMessage(kWhatVideoNotify, this);
Wei Jia88703c32014-08-06 11:24:07 -07001865 ++mVideoDecoderGeneration;
1866 notify->setInt32("generation", mVideoDecoderGeneration);
1867
Chong Zhang7137ec72014-11-12 16:41:05 -08001868 *decoder = new Decoder(
Wei Jiaf2ae3e12016-10-27 17:10:59 -07001869 notify, mSource, mPID, mUID, mRenderer, mSurface, mCCDecoder);
Wei Jia686e8e52017-04-03 14:08:01 -07001870 mVideoDecoderError = false;
Lajos Molnard9fd6312014-11-06 11:00:00 -08001871
1872 // enable FRC if high-quality AV sync is requested, even if not
Lajos Molnar1de1e252015-04-30 18:18:34 -07001873 // directly queuing to display, as this will even improve textureview
Lajos Molnard9fd6312014-11-06 11:00:00 -08001874 // playback.
1875 {
Marco Nelissen96626b72016-12-01 13:58:59 -08001876 if (property_get_bool("persist.sys.media.avsync", false)) {
Lajos Molnard9fd6312014-11-06 11:00:00 -08001877 format->setInt32("auto-frc", 1);
1878 }
1879 }
Wei Jiabc2fb722014-07-08 16:37:57 -07001880 }
Lajos Molnar1cd13982014-01-17 15:12:51 -08001881 (*decoder)->init();
Hassan Shojaniacefac142017-02-06 21:02:02 -08001882
1883 // Modular DRM
1884 if (mIsDrmProtected) {
1885 format->setPointer("crypto", mCrypto.get());
1886 ALOGV("instantiateDecoder: mCrypto: %p (%d) isSecure: %d", mCrypto.get(),
1887 (mCrypto != NULL ? mCrypto->getStrongCount() : 0),
1888 (mSourceFlags & Source::FLAG_SECURE) != 0);
1889 }
1890
Andreas Huber84066782011-08-16 09:34:26 -07001891 (*decoder)->configure(format);
Andreas Huberf9334412010-12-15 15:17:42 -08001892
Praveen Chavanbbaa1442016-04-08 13:33:49 -07001893 if (!audio) {
1894 sp<AMessage> params = new AMessage();
1895 float rate = getFrameRate();
1896 if (rate > 0) {
1897 params->setFloat("frame-rate-total", rate);
1898 }
1899
1900 sp<MetaData> fileMeta = getFileMeta();
1901 if (fileMeta != NULL) {
1902 int32_t videoTemporalLayerCount;
1903 if (fileMeta->findInt32(kKeyTemporalLayerCount, &videoTemporalLayerCount)
1904 && videoTemporalLayerCount > 0) {
1905 params->setInt32("temporal-layer-count", videoTemporalLayerCount);
1906 }
1907 }
1908
1909 if (params->countEntries() > 0) {
1910 (*decoder)->setParameters(params);
1911 }
1912 }
Andreas Huberf9334412010-12-15 15:17:42 -08001913 return OK;
1914}
1915
Chong Zhangced1c2f2014-08-08 15:22:35 -07001916void NuPlayer::updateVideoSize(
1917 const sp<AMessage> &inputFormat,
1918 const sp<AMessage> &outputFormat) {
1919 if (inputFormat == NULL) {
1920 ALOGW("Unknown video size, reporting 0x0!");
1921 notifyListener(MEDIA_SET_VIDEO_SIZE, 0, 0);
1922 return;
1923 }
Wei Jia9737d342016-12-28 14:59:59 -08001924 int32_t err = OK;
1925 inputFormat->findInt32("err", &err);
1926 if (err == -EWOULDBLOCK) {
1927 ALOGW("Video meta is not available yet!");
1928 return;
1929 }
1930 if (err != OK) {
1931 ALOGW("Something is wrong with video meta!");
1932 return;
1933 }
Chong Zhangced1c2f2014-08-08 15:22:35 -07001934
1935 int32_t displayWidth, displayHeight;
Chong Zhangced1c2f2014-08-08 15:22:35 -07001936 if (outputFormat != NULL) {
1937 int32_t width, height;
1938 CHECK(outputFormat->findInt32("width", &width));
1939 CHECK(outputFormat->findInt32("height", &height));
1940
1941 int32_t cropLeft, cropTop, cropRight, cropBottom;
1942 CHECK(outputFormat->findRect(
1943 "crop",
1944 &cropLeft, &cropTop, &cropRight, &cropBottom));
1945
1946 displayWidth = cropRight - cropLeft + 1;
1947 displayHeight = cropBottom - cropTop + 1;
1948
1949 ALOGV("Video output format changed to %d x %d "
1950 "(crop: %d x %d @ (%d, %d))",
1951 width, height,
1952 displayWidth,
1953 displayHeight,
1954 cropLeft, cropTop);
1955 } else {
1956 CHECK(inputFormat->findInt32("width", &displayWidth));
1957 CHECK(inputFormat->findInt32("height", &displayHeight));
1958
1959 ALOGV("Video input format %d x %d", displayWidth, displayHeight);
1960 }
1961
1962 // Take into account sample aspect ratio if necessary:
1963 int32_t sarWidth, sarHeight;
1964 if (inputFormat->findInt32("sar-width", &sarWidth)
Wei Jia095bc252017-01-27 10:16:16 -08001965 && inputFormat->findInt32("sar-height", &sarHeight)
1966 && sarWidth > 0 && sarHeight > 0) {
Chong Zhangced1c2f2014-08-08 15:22:35 -07001967 ALOGV("Sample aspect ratio %d : %d", sarWidth, sarHeight);
1968
1969 displayWidth = (displayWidth * sarWidth) / sarHeight;
1970
1971 ALOGV("display dimensions %d x %d", displayWidth, displayHeight);
Wei Jiadf6c6af2016-09-29 11:27:15 -07001972 } else {
1973 int32_t width, height;
1974 if (inputFormat->findInt32("display-width", &width)
1975 && inputFormat->findInt32("display-height", &height)
1976 && width > 0 && height > 0
1977 && displayWidth > 0 && displayHeight > 0) {
1978 if (displayHeight * (int64_t)width / height > (int64_t)displayWidth) {
1979 displayHeight = (int32_t)(displayWidth * (int64_t)height / width);
1980 } else {
1981 displayWidth = (int32_t)(displayHeight * (int64_t)width / height);
1982 }
1983 ALOGV("Video display width and height are overridden to %d x %d",
1984 displayWidth, displayHeight);
1985 }
Chong Zhangced1c2f2014-08-08 15:22:35 -07001986 }
1987
1988 int32_t rotationDegrees;
1989 if (!inputFormat->findInt32("rotation-degrees", &rotationDegrees)) {
1990 rotationDegrees = 0;
1991 }
1992
1993 if (rotationDegrees == 90 || rotationDegrees == 270) {
1994 int32_t tmp = displayWidth;
1995 displayWidth = displayHeight;
1996 displayHeight = tmp;
1997 }
1998
1999 notifyListener(
2000 MEDIA_SET_VIDEO_SIZE,
2001 displayWidth,
2002 displayHeight);
2003}
2004
Chong Zhangdcb89b32013-08-06 09:44:47 -07002005void NuPlayer::notifyListener(int msg, int ext1, int ext2, const Parcel *in) {
Andreas Huber43c3e6c2011-01-05 12:17:08 -08002006 if (mDriver == NULL) {
Andreas Huberf9334412010-12-15 15:17:42 -08002007 return;
2008 }
2009
Andreas Huber43c3e6c2011-01-05 12:17:08 -08002010 sp<NuPlayerDriver> driver = mDriver.promote();
Andreas Huberf9334412010-12-15 15:17:42 -08002011
Andreas Huber43c3e6c2011-01-05 12:17:08 -08002012 if (driver == NULL) {
Andreas Huberf9334412010-12-15 15:17:42 -08002013 return;
2014 }
2015
Chong Zhangdcb89b32013-08-06 09:44:47 -07002016 driver->notifyListener(msg, ext1, ext2, in);
Andreas Huberf9334412010-12-15 15:17:42 -08002017}
2018
Chong Zhang7137ec72014-11-12 16:41:05 -08002019void NuPlayer::flushDecoder(bool audio, bool needShutdown) {
Andreas Huber14f76722013-01-15 09:04:18 -08002020 ALOGV("[%s] flushDecoder needShutdown=%d",
2021 audio ? "audio" : "video", needShutdown);
2022
Chong Zhang7137ec72014-11-12 16:41:05 -08002023 const sp<DecoderBase> &decoder = getDecoder(audio);
Lajos Molnar87603c02014-08-20 19:25:30 -07002024 if (decoder == NULL) {
Steve Blockdf64d152012-01-04 20:05:49 +00002025 ALOGI("flushDecoder %s without decoder present",
Andreas Huber6e3d3112011-11-28 12:36:11 -08002026 audio ? "audio" : "video");
Lajos Molnar87603c02014-08-20 19:25:30 -07002027 return;
Andreas Huber6e3d3112011-11-28 12:36:11 -08002028 }
2029
Andreas Huber1aef2112011-01-04 14:01:29 -08002030 // Make sure we don't continue to scan sources until we finish flushing.
2031 ++mScanSourcesGeneration;
Chong Zhang3b032b32015-04-17 15:49:06 -07002032 if (mScanSourcesPending) {
Toshikazu Saitocbcbb792015-09-15 14:53:01 +09002033 if (!needShutdown) {
2034 mDeferredActions.push_back(
2035 new SimpleAction(&NuPlayer::performScanSources));
2036 }
Chong Zhang3b032b32015-04-17 15:49:06 -07002037 mScanSourcesPending = false;
2038 }
Andreas Huber1aef2112011-01-04 14:01:29 -08002039
Chong Zhang7137ec72014-11-12 16:41:05 -08002040 decoder->signalFlush();
Andreas Huber1aef2112011-01-04 14:01:29 -08002041
2042 FlushStatus newStatus =
2043 needShutdown ? FLUSHING_DECODER_SHUTDOWN : FLUSHING_DECODER;
2044
Lajos Molnarfcd3e942015-03-31 10:06:48 -07002045 mFlushComplete[audio][false /* isDecoder */] = (mRenderer == NULL);
Andy Hung8d121d42014-10-03 09:53:53 -07002046 mFlushComplete[audio][true /* isDecoder */] = false;
Andreas Huber1aef2112011-01-04 14:01:29 -08002047 if (audio) {
Wei Jia53904f32014-07-29 10:22:53 -07002048 ALOGE_IF(mFlushingAudio != NONE,
2049 "audio flushDecoder() is called in state %d", mFlushingAudio);
Andreas Huber1aef2112011-01-04 14:01:29 -08002050 mFlushingAudio = newStatus;
Andreas Huber1aef2112011-01-04 14:01:29 -08002051 } else {
Wei Jia53904f32014-07-29 10:22:53 -07002052 ALOGE_IF(mFlushingVideo != NONE,
2053 "video flushDecoder() is called in state %d", mFlushingVideo);
Andreas Huber1aef2112011-01-04 14:01:29 -08002054 mFlushingVideo = newStatus;
Andreas Huber1aef2112011-01-04 14:01:29 -08002055 }
2056}
2057
Chong Zhangced1c2f2014-08-08 15:22:35 -07002058void NuPlayer::queueDecoderShutdown(
2059 bool audio, bool video, const sp<AMessage> &reply) {
2060 ALOGI("queueDecoderShutdown audio=%d, video=%d", audio, video);
Andreas Huber84066782011-08-16 09:34:26 -07002061
Chong Zhangced1c2f2014-08-08 15:22:35 -07002062 mDeferredActions.push_back(
Wei Jiafef808d2014-10-31 17:57:05 -07002063 new FlushDecoderAction(
2064 audio ? FLUSH_CMD_SHUTDOWN : FLUSH_CMD_NONE,
2065 video ? FLUSH_CMD_SHUTDOWN : FLUSH_CMD_NONE));
Andreas Huber84066782011-08-16 09:34:26 -07002066
Chong Zhangced1c2f2014-08-08 15:22:35 -07002067 mDeferredActions.push_back(
2068 new SimpleAction(&NuPlayer::performScanSources));
Andreas Huber84066782011-08-16 09:34:26 -07002069
Chong Zhangced1c2f2014-08-08 15:22:35 -07002070 mDeferredActions.push_back(new PostMessageAction(reply));
2071
2072 processDeferredActions();
Andreas Huber84066782011-08-16 09:34:26 -07002073}
2074
James Dong0d268a32012-08-31 12:18:27 -07002075status_t NuPlayer::setVideoScalingMode(int32_t mode) {
2076 mVideoScalingMode = mode;
Lajos Molnar1de1e252015-04-30 18:18:34 -07002077 if (mSurface != NULL) {
2078 status_t ret = native_window_set_scaling_mode(mSurface.get(), mVideoScalingMode);
James Dong0d268a32012-08-31 12:18:27 -07002079 if (ret != OK) {
2080 ALOGE("Failed to set scaling mode (%d): %s",
2081 -ret, strerror(-ret));
2082 return ret;
2083 }
2084 }
2085 return OK;
2086}
2087
Chong Zhangdcb89b32013-08-06 09:44:47 -07002088status_t NuPlayer::getTrackInfo(Parcel* reply) const {
Lajos Molnar1d15ab52015-03-04 16:46:34 -08002089 sp<AMessage> msg = new AMessage(kWhatGetTrackInfo, this);
Chong Zhangdcb89b32013-08-06 09:44:47 -07002090 msg->setPointer("reply", reply);
2091
2092 sp<AMessage> response;
2093 status_t err = msg->postAndAwaitResponse(&response);
2094 return err;
2095}
2096
Robert Shih7c4f0d72014-07-09 18:53:31 -07002097status_t NuPlayer::getSelectedTrack(int32_t type, Parcel* reply) const {
Lajos Molnar1d15ab52015-03-04 16:46:34 -08002098 sp<AMessage> msg = new AMessage(kWhatGetSelectedTrack, this);
Robert Shih7c4f0d72014-07-09 18:53:31 -07002099 msg->setPointer("reply", reply);
2100 msg->setInt32("type", type);
2101
2102 sp<AMessage> response;
2103 status_t err = msg->postAndAwaitResponse(&response);
2104 if (err == OK && response != NULL) {
2105 CHECK(response->findInt32("err", &err));
2106 }
2107 return err;
2108}
2109
Robert Shih6ffb1fd2014-10-29 16:24:32 -07002110status_t NuPlayer::selectTrack(size_t trackIndex, bool select, int64_t timeUs) {
Lajos Molnar1d15ab52015-03-04 16:46:34 -08002111 sp<AMessage> msg = new AMessage(kWhatSelectTrack, this);
Chong Zhangdcb89b32013-08-06 09:44:47 -07002112 msg->setSize("trackIndex", trackIndex);
2113 msg->setInt32("select", select);
Robert Shih6ffb1fd2014-10-29 16:24:32 -07002114 msg->setInt64("timeUs", timeUs);
Chong Zhangdcb89b32013-08-06 09:44:47 -07002115
2116 sp<AMessage> response;
2117 status_t err = msg->postAndAwaitResponse(&response);
2118
Chong Zhang404fced2014-06-11 14:45:31 -07002119 if (err != OK) {
2120 return err;
2121 }
2122
2123 if (!response->findInt32("err", &err)) {
2124 err = OK;
2125 }
2126
Chong Zhangdcb89b32013-08-06 09:44:47 -07002127 return err;
2128}
2129
Ronghua Wua73d9e02014-10-08 15:13:29 -07002130status_t NuPlayer::getCurrentPosition(int64_t *mediaUs) {
2131 sp<Renderer> renderer = mRenderer;
2132 if (renderer == NULL) {
2133 return NO_INIT;
2134 }
2135
2136 return renderer->getCurrentPosition(mediaUs);
2137}
2138
Praveen Chavane1e5d7a2015-05-19 19:09:48 -07002139void NuPlayer::getStats(Vector<sp<AMessage> > *mTrackStats) {
2140 CHECK(mTrackStats != NULL);
2141
2142 mTrackStats->clear();
2143 if (mVideoDecoder != NULL) {
2144 mTrackStats->push_back(mVideoDecoder->getStats());
2145 }
2146 if (mAudioDecoder != NULL) {
2147 mTrackStats->push_back(mAudioDecoder->getStats());
Chong Zhang7137ec72014-11-12 16:41:05 -08002148 }
Ronghua Wua73d9e02014-10-08 15:13:29 -07002149}
2150
Marco Nelissenf0b72b52014-09-16 15:43:44 -07002151sp<MetaData> NuPlayer::getFileMeta() {
2152 return mSource->getFileFormatMeta();
2153}
2154
Ronghua Wuc8a70d32015-04-29 16:26:34 -07002155float NuPlayer::getFrameRate() {
2156 sp<MetaData> meta = mSource->getFormatMeta(false /* audio */);
2157 if (meta == NULL) {
2158 return 0;
2159 }
2160 int32_t rate;
2161 if (!meta->findInt32(kKeyFrameRate, &rate)) {
2162 // fall back to try file meta
2163 sp<MetaData> fileMeta = getFileMeta();
2164 if (fileMeta == NULL) {
2165 ALOGW("source has video meta but not file meta");
2166 return -1;
2167 }
2168 int32_t fileMetaRate;
2169 if (!fileMeta->findInt32(kKeyFrameRate, &fileMetaRate)) {
2170 return -1;
2171 }
2172 return fileMetaRate;
2173 }
2174 return rate;
2175}
2176
Andreas Huberb7c8e912012-11-27 15:02:53 -08002177void NuPlayer::schedulePollDuration() {
Lajos Molnar1d15ab52015-03-04 16:46:34 -08002178 sp<AMessage> msg = new AMessage(kWhatPollDuration, this);
Andreas Huberb7c8e912012-11-27 15:02:53 -08002179 msg->setInt32("generation", mPollDurationGeneration);
2180 msg->post();
2181}
2182
2183void NuPlayer::cancelPollDuration() {
2184 ++mPollDurationGeneration;
2185}
2186
Andreas Hubera1f8ab02012-11-30 10:53:22 -08002187void NuPlayer::processDeferredActions() {
2188 while (!mDeferredActions.empty()) {
2189 // We won't execute any deferred actions until we're no longer in
2190 // an intermediate state, i.e. one more more decoders are currently
2191 // flushing or shutting down.
2192
Andreas Hubera1f8ab02012-11-30 10:53:22 -08002193 if (mFlushingAudio != NONE || mFlushingVideo != NONE) {
2194 // We're currently flushing, postpone the reset until that's
2195 // completed.
2196
2197 ALOGV("postponing action mFlushingAudio=%d, mFlushingVideo=%d",
2198 mFlushingAudio, mFlushingVideo);
2199
2200 break;
2201 }
2202
2203 sp<Action> action = *mDeferredActions.begin();
2204 mDeferredActions.erase(mDeferredActions.begin());
2205
2206 action->execute(this);
2207 }
2208}
2209
Wei Jiac5de0912016-11-18 10:22:14 -08002210void NuPlayer::performSeek(int64_t seekTimeUs, MediaPlayerSeekMode mode) {
2211 ALOGV("performSeek seekTimeUs=%lld us (%.2f secs), mode=%d",
2212 (long long)seekTimeUs, seekTimeUs / 1E6, mode);
Andreas Hubera1f8ab02012-11-30 10:53:22 -08002213
Andy Hungadf34bf2014-09-03 18:22:22 -07002214 if (mSource == NULL) {
2215 // This happens when reset occurs right before the loop mode
2216 // asynchronously seeks to the start of the stream.
2217 LOG_ALWAYS_FATAL_IF(mAudioDecoder != NULL || mVideoDecoder != NULL,
2218 "mSource is NULL and decoders not NULL audio(%p) video(%p)",
2219 mAudioDecoder.get(), mVideoDecoder.get());
2220 return;
2221 }
Robert Shih1a5c8592015-08-04 18:07:44 -07002222 mPreviousSeekTimeUs = seekTimeUs;
Wei Jiac5de0912016-11-18 10:22:14 -08002223 mSource->seekTo(seekTimeUs, mode);
Robert Shihd3b0bbb2014-07-23 15:00:25 -07002224 ++mTimedTextGeneration;
Andreas Hubera1f8ab02012-11-30 10:53:22 -08002225
Andreas Hubera1f8ab02012-11-30 10:53:22 -08002226 // everything's flushed, continue playback.
2227}
2228
Wei Jiafef808d2014-10-31 17:57:05 -07002229void NuPlayer::performDecoderFlush(FlushCommand audio, FlushCommand video) {
2230 ALOGV("performDecoderFlush audio=%d, video=%d", audio, video);
Andreas Hubera1f8ab02012-11-30 10:53:22 -08002231
Wei Jiafef808d2014-10-31 17:57:05 -07002232 if ((audio == FLUSH_CMD_NONE || mAudioDecoder == NULL)
2233 && (video == FLUSH_CMD_NONE || mVideoDecoder == NULL)) {
Andreas Hubera1f8ab02012-11-30 10:53:22 -08002234 return;
2235 }
2236
Wei Jiafef808d2014-10-31 17:57:05 -07002237 if (audio != FLUSH_CMD_NONE && mAudioDecoder != NULL) {
2238 flushDecoder(true /* audio */, (audio == FLUSH_CMD_SHUTDOWN));
Andreas Hubera1f8ab02012-11-30 10:53:22 -08002239 }
2240
Wei Jiafef808d2014-10-31 17:57:05 -07002241 if (video != FLUSH_CMD_NONE && mVideoDecoder != NULL) {
2242 flushDecoder(false /* audio */, (video == FLUSH_CMD_SHUTDOWN));
Andreas Hubera1f8ab02012-11-30 10:53:22 -08002243 }
2244}
2245
2246void NuPlayer::performReset() {
2247 ALOGV("performReset");
2248
2249 CHECK(mAudioDecoder == NULL);
2250 CHECK(mVideoDecoder == NULL);
2251
Ray Essick0d98c182017-11-09 13:42:42 -08002252 stopPlaybackTimer("performReset");
2253
Andreas Hubera1f8ab02012-11-30 10:53:22 -08002254 cancelPollDuration();
2255
2256 ++mScanSourcesGeneration;
2257 mScanSourcesPending = false;
2258
Lajos Molnar09524832014-07-17 14:29:51 -07002259 if (mRendererLooper != NULL) {
2260 if (mRenderer != NULL) {
2261 mRendererLooper->unregisterHandler(mRenderer->id());
2262 }
2263 mRendererLooper->stop();
2264 mRendererLooper.clear();
2265 }
Andreas Hubera1f8ab02012-11-30 10:53:22 -08002266 mRenderer.clear();
Wei Jia57568df2014-09-22 10:16:29 -07002267 ++mRendererGeneration;
Andreas Hubera1f8ab02012-11-30 10:53:22 -08002268
2269 if (mSource != NULL) {
2270 mSource->stop();
Andreas Huberb5f25f02013-02-05 10:14:26 -08002271
Wei Jiac45a4b22016-04-15 15:30:23 -07002272 Mutex::Autolock autoLock(mSourceLock);
Andreas Hubera1f8ab02012-11-30 10:53:22 -08002273 mSource.clear();
2274 }
2275
2276 if (mDriver != NULL) {
2277 sp<NuPlayerDriver> driver = mDriver.promote();
2278 if (driver != NULL) {
2279 driver->notifyResetComplete();
2280 }
2281 }
Andreas Huber57a339c2012-12-03 11:18:00 -08002282
2283 mStarted = false;
Wei Jia8a092d32016-06-03 14:57:24 -07002284 mPrepared = false;
Ronghua Wu64c2d172015-10-07 16:52:19 -07002285 mResetting = false;
Robert Shih0c61a0d2015-07-06 15:09:10 -07002286 mSourceStarted = false;
Hassan Shojaniacefac142017-02-06 21:02:02 -08002287
2288 // Modular DRM
2289 if (mCrypto != NULL) {
2290 // decoders will be flushed before this so their mCrypto would go away on their own
2291 // TODO change to ALOGV
2292 ALOGD("performReset mCrypto: %p (%d)", mCrypto.get(),
2293 (mCrypto != NULL ? mCrypto->getStrongCount() : 0));
2294 mCrypto.clear();
2295 }
2296 mIsDrmProtected = false;
Andreas Hubera1f8ab02012-11-30 10:53:22 -08002297}
2298
2299void NuPlayer::performScanSources() {
2300 ALOGV("performScanSources");
2301
Andreas Huber57a339c2012-12-03 11:18:00 -08002302 if (!mStarted) {
2303 return;
2304 }
2305
Andreas Hubera1f8ab02012-11-30 10:53:22 -08002306 if (mAudioDecoder == NULL || mVideoDecoder == NULL) {
2307 postScanSources();
2308 }
2309}
2310
Lajos Molnar1de1e252015-04-30 18:18:34 -07002311void NuPlayer::performSetSurface(const sp<Surface> &surface) {
Andreas Huber57a339c2012-12-03 11:18:00 -08002312 ALOGV("performSetSurface");
2313
Lajos Molnar1de1e252015-04-30 18:18:34 -07002314 mSurface = surface;
Andreas Huber57a339c2012-12-03 11:18:00 -08002315
2316 // XXX - ignore error from setVideoScalingMode for now
2317 setVideoScalingMode(mVideoScalingMode);
Chong Zhang13d6faa2014-08-22 15:35:28 -07002318
2319 if (mDriver != NULL) {
2320 sp<NuPlayerDriver> driver = mDriver.promote();
2321 if (driver != NULL) {
2322 driver->notifySetSurfaceComplete();
2323 }
2324 }
Andreas Huber57a339c2012-12-03 11:18:00 -08002325}
2326
Chong Zhangf8d71772014-11-26 15:08:34 -08002327void NuPlayer::performResumeDecoders(bool needNotify) {
2328 if (needNotify) {
2329 mResumePending = true;
2330 if (mVideoDecoder == NULL) {
2331 // if audio-only, we can notify seek complete now,
2332 // as the resume operation will be relatively fast.
2333 finishResume();
2334 }
2335 }
2336
Chong Zhang7137ec72014-11-12 16:41:05 -08002337 if (mVideoDecoder != NULL) {
Chong Zhangf8d71772014-11-26 15:08:34 -08002338 // When there is continuous seek, MediaPlayer will cache the seek
2339 // position, and send down new seek request when previous seek is
2340 // complete. Let's wait for at least one video output frame before
2341 // notifying seek complete, so that the video thumbnail gets updated
2342 // when seekbar is dragged.
2343 mVideoDecoder->signalResume(needNotify);
Chong Zhang7137ec72014-11-12 16:41:05 -08002344 }
2345
2346 if (mAudioDecoder != NULL) {
Chong Zhangf8d71772014-11-26 15:08:34 -08002347 mAudioDecoder->signalResume(false /* needNotify */);
2348 }
2349}
2350
2351void NuPlayer::finishResume() {
2352 if (mResumePending) {
2353 mResumePending = false;
Wei Jia1061c9c2015-05-19 16:02:17 -07002354 notifyDriverSeekComplete();
2355 }
2356}
2357
2358void NuPlayer::notifyDriverSeekComplete() {
2359 if (mDriver != NULL) {
2360 sp<NuPlayerDriver> driver = mDriver.promote();
2361 if (driver != NULL) {
2362 driver->notifySeekComplete();
Chong Zhangf8d71772014-11-26 15:08:34 -08002363 }
Chong Zhang7137ec72014-11-12 16:41:05 -08002364 }
2365}
2366
Andreas Huber9575c962013-02-05 13:59:56 -08002367void NuPlayer::onSourceNotify(const sp<AMessage> &msg) {
2368 int32_t what;
2369 CHECK(msg->findInt32("what", &what));
2370
2371 switch (what) {
Lajos Molnarfcd3e942015-03-31 10:06:48 -07002372 case Source::kWhatInstantiateSecureDecoders:
2373 {
2374 if (mSource == NULL) {
2375 // This is a stale notification from a source that was
2376 // asynchronously preparing when the client called reset().
2377 // We handled the reset, the source is gone.
2378 break;
2379 }
2380
2381 sp<AMessage> reply;
2382 CHECK(msg->findMessage("reply", &reply));
2383 status_t err = onInstantiateSecureDecoders();
2384 reply->setInt32("err", err);
2385 reply->post();
2386 break;
2387 }
2388
Andreas Huber9575c962013-02-05 13:59:56 -08002389 case Source::kWhatPrepared:
2390 {
Hassan Shojaniacefac142017-02-06 21:02:02 -08002391 ALOGV("NuPlayer::onSourceNotify Source::kWhatPrepared source: %p", mSource.get());
Andreas Huberb5f28d42013-04-25 15:11:19 -07002392 if (mSource == NULL) {
2393 // This is a stale notification from a source that was
2394 // asynchronously preparing when the client called reset().
2395 // We handled the reset, the source is gone.
2396 break;
2397 }
2398
Andreas Huberec0c5972013-02-05 14:47:13 -08002399 int32_t err;
2400 CHECK(msg->findInt32("err", &err));
2401
Lajos Molnarfcd3e942015-03-31 10:06:48 -07002402 if (err != OK) {
2403 // shut down potential secure codecs in case client never calls reset
2404 mDeferredActions.push_back(
2405 new FlushDecoderAction(FLUSH_CMD_SHUTDOWN /* audio */,
2406 FLUSH_CMD_SHUTDOWN /* video */));
2407 processDeferredActions();
Wei Jia8a092d32016-06-03 14:57:24 -07002408 } else {
2409 mPrepared = true;
Lajos Molnarfcd3e942015-03-31 10:06:48 -07002410 }
2411
Andreas Huber9575c962013-02-05 13:59:56 -08002412 sp<NuPlayerDriver> driver = mDriver.promote();
2413 if (driver != NULL) {
Marco Nelissendd114d12014-05-28 15:23:14 -07002414 // notify duration first, so that it's definitely set when
2415 // the app received the "prepare complete" callback.
2416 int64_t durationUs;
2417 if (mSource->getDuration(&durationUs) == OK) {
2418 driver->notifyDuration(durationUs);
2419 }
Andreas Huberec0c5972013-02-05 14:47:13 -08002420 driver->notifyPrepareCompleted(err);
Andreas Huber9575c962013-02-05 13:59:56 -08002421 }
Andreas Huber99759402013-04-01 14:28:31 -07002422
Andreas Huber9575c962013-02-05 13:59:56 -08002423 break;
2424 }
2425
Hassan Shojaniacefac142017-02-06 21:02:02 -08002426 // Modular DRM
2427 case Source::kWhatDrmInfo:
2428 {
2429 Parcel parcel;
2430 sp<ABuffer> drmInfo;
2431 CHECK(msg->findBuffer("drmInfo", &drmInfo));
2432 parcel.setData(drmInfo->data(), drmInfo->size());
2433
2434 ALOGV("onSourceNotify() kWhatDrmInfo MEDIA_DRM_INFO drmInfo: %p parcel size: %zu",
2435 drmInfo.get(), parcel.dataSize());
2436
2437 notifyListener(MEDIA_DRM_INFO, 0 /* ext1 */, 0 /* ext2 */, &parcel);
2438
2439 break;
2440 }
2441
Andreas Huber9575c962013-02-05 13:59:56 -08002442 case Source::kWhatFlagsChanged:
2443 {
2444 uint32_t flags;
2445 CHECK(msg->findInt32("flags", (int32_t *)&flags));
2446
Chong Zhang4b7069d2013-09-11 12:52:43 -07002447 sp<NuPlayerDriver> driver = mDriver.promote();
2448 if (driver != NULL) {
Hassan Shojaniacefac142017-02-06 21:02:02 -08002449
2450 ALOGV("onSourceNotify() kWhatFlagsChanged FLAG_CAN_PAUSE: %d "
2451 "FLAG_CAN_SEEK_BACKWARD: %d \n\t\t\t\t FLAG_CAN_SEEK_FORWARD: %d "
2452 "FLAG_CAN_SEEK: %d FLAG_DYNAMIC_DURATION: %d \n"
2453 "\t\t\t\t FLAG_SECURE: %d FLAG_PROTECTED: %d",
2454 (flags & Source::FLAG_CAN_PAUSE) != 0,
2455 (flags & Source::FLAG_CAN_SEEK_BACKWARD) != 0,
2456 (flags & Source::FLAG_CAN_SEEK_FORWARD) != 0,
2457 (flags & Source::FLAG_CAN_SEEK) != 0,
2458 (flags & Source::FLAG_DYNAMIC_DURATION) != 0,
2459 (flags & Source::FLAG_SECURE) != 0,
2460 (flags & Source::FLAG_PROTECTED) != 0);
2461
Wei Jia895651b2014-12-10 17:31:52 -08002462 if ((flags & NuPlayer::Source::FLAG_CAN_SEEK) == 0) {
2463 driver->notifyListener(
2464 MEDIA_INFO, MEDIA_INFO_NOT_SEEKABLE, 0);
2465 }
Chong Zhang4b7069d2013-09-11 12:52:43 -07002466 driver->notifyFlagsChanged(flags);
2467 }
2468
Andreas Huber9575c962013-02-05 13:59:56 -08002469 if ((mSourceFlags & Source::FLAG_DYNAMIC_DURATION)
2470 && (!(flags & Source::FLAG_DYNAMIC_DURATION))) {
2471 cancelPollDuration();
2472 } else if (!(mSourceFlags & Source::FLAG_DYNAMIC_DURATION)
2473 && (flags & Source::FLAG_DYNAMIC_DURATION)
2474 && (mAudioDecoder != NULL || mVideoDecoder != NULL)) {
2475 schedulePollDuration();
2476 }
2477
2478 mSourceFlags = flags;
2479 break;
2480 }
2481
2482 case Source::kWhatVideoSizeChanged:
2483 {
Chong Zhangced1c2f2014-08-08 15:22:35 -07002484 sp<AMessage> format;
2485 CHECK(msg->findMessage("format", &format));
Andreas Huber9575c962013-02-05 13:59:56 -08002486
Chong Zhangced1c2f2014-08-08 15:22:35 -07002487 updateVideoSize(format);
Andreas Huber9575c962013-02-05 13:59:56 -08002488 break;
2489 }
2490
Chong Zhang2a3cc9a2014-08-21 17:48:26 -07002491 case Source::kWhatBufferingUpdate:
2492 {
2493 int32_t percentage;
2494 CHECK(msg->findInt32("percentage", &percentage));
2495
2496 notifyListener(MEDIA_BUFFERING_UPDATE, percentage, 0);
2497 break;
2498 }
2499
Chong Zhangefbb6192015-01-30 17:13:27 -08002500 case Source::kWhatPauseOnBufferingStart:
2501 {
2502 // ignore if not playing
Chong Zhang8a048332015-05-06 15:16:28 -07002503 if (mStarted) {
Chong Zhangefbb6192015-01-30 17:13:27 -08002504 ALOGI("buffer low, pausing...");
2505
Chong Zhang8a048332015-05-06 15:16:28 -07002506 mPausedForBuffering = true;
Chong Zhangefbb6192015-01-30 17:13:27 -08002507 onPause();
2508 }
Wei Jiabfe82072016-05-20 09:21:50 -07002509 notifyListener(MEDIA_INFO, MEDIA_INFO_BUFFERING_START, 0);
Roger Jönssonb50e83e2013-01-21 16:26:41 +01002510 break;
2511 }
2512
Chong Zhangefbb6192015-01-30 17:13:27 -08002513 case Source::kWhatResumeOnBufferingEnd:
2514 {
2515 // ignore if not playing
Chong Zhang8a048332015-05-06 15:16:28 -07002516 if (mStarted) {
Chong Zhangefbb6192015-01-30 17:13:27 -08002517 ALOGI("buffer ready, resuming...");
2518
Chong Zhang8a048332015-05-06 15:16:28 -07002519 mPausedForBuffering = false;
2520
2521 // do not resume yet if client didn't unpause
2522 if (!mPausedByClient) {
2523 onResume();
2524 }
Chong Zhangefbb6192015-01-30 17:13:27 -08002525 }
Wei Jia3bed45a2016-02-17 11:06:47 -08002526 notifyListener(MEDIA_INFO, MEDIA_INFO_BUFFERING_END, 0);
Roger Jönssonb50e83e2013-01-21 16:26:41 +01002527 break;
2528 }
2529
Chong Zhangefbb6192015-01-30 17:13:27 -08002530 case Source::kWhatCacheStats:
2531 {
2532 int32_t kbps;
2533 CHECK(msg->findInt32("bandwidth", &kbps));
2534
2535 notifyListener(MEDIA_INFO, MEDIA_INFO_NETWORK_BANDWIDTH, kbps);
2536 break;
2537 }
2538
Chong Zhangdcb89b32013-08-06 09:44:47 -07002539 case Source::kWhatSubtitleData:
2540 {
2541 sp<ABuffer> buffer;
2542 CHECK(msg->findBuffer("buffer", &buffer));
2543
Chong Zhang404fced2014-06-11 14:45:31 -07002544 sendSubtitleData(buffer, 0 /* baseIndex */);
Chong Zhangdcb89b32013-08-06 09:44:47 -07002545 break;
2546 }
2547
Robert Shih08528432015-04-08 09:06:54 -07002548 case Source::kWhatTimedMetaData:
2549 {
2550 sp<ABuffer> buffer;
2551 if (!msg->findBuffer("buffer", &buffer)) {
2552 notifyListener(MEDIA_INFO, MEDIA_INFO_METADATA_UPDATE, 0);
2553 } else {
2554 sendTimedMetaData(buffer);
2555 }
2556 break;
2557 }
2558
Robert Shihd3b0bbb2014-07-23 15:00:25 -07002559 case Source::kWhatTimedTextData:
2560 {
2561 int32_t generation;
2562 if (msg->findInt32("generation", &generation)
2563 && generation != mTimedTextGeneration) {
2564 break;
2565 }
2566
2567 sp<ABuffer> buffer;
2568 CHECK(msg->findBuffer("buffer", &buffer));
2569
2570 sp<NuPlayerDriver> driver = mDriver.promote();
2571 if (driver == NULL) {
2572 break;
2573 }
2574
2575 int posMs;
2576 int64_t timeUs, posUs;
2577 driver->getCurrentPosition(&posMs);
Patrik2 Carlsson24d484b2015-01-27 16:49:45 +01002578 posUs = (int64_t) posMs * 1000ll;
Robert Shihd3b0bbb2014-07-23 15:00:25 -07002579 CHECK(buffer->meta()->findInt64("timeUs", &timeUs));
2580
2581 if (posUs < timeUs) {
2582 if (!msg->findInt32("generation", &generation)) {
2583 msg->setInt32("generation", mTimedTextGeneration);
2584 }
2585 msg->post(timeUs - posUs);
2586 } else {
2587 sendTimedTextData(buffer);
2588 }
2589 break;
2590 }
2591
Andreas Huber14f76722013-01-15 09:04:18 -08002592 case Source::kWhatQueueDecoderShutdown:
2593 {
2594 int32_t audio, video;
2595 CHECK(msg->findInt32("audio", &audio));
2596 CHECK(msg->findInt32("video", &video));
2597
2598 sp<AMessage> reply;
2599 CHECK(msg->findMessage("reply", &reply));
2600
2601 queueDecoderShutdown(audio, video, reply);
2602 break;
2603 }
2604
Ronghua Wu80276872014-08-28 15:50:29 -07002605 case Source::kWhatDrmNoLicense:
2606 {
2607 notifyListener(MEDIA_ERROR, MEDIA_ERROR_UNKNOWN, ERROR_DRM_NO_LICENSE);
2608 break;
2609 }
2610
Andreas Huber9575c962013-02-05 13:59:56 -08002611 default:
2612 TRESPASS();
2613 }
2614}
2615
Chong Zhanga7fa1d92014-06-11 14:49:23 -07002616void NuPlayer::onClosedCaptionNotify(const sp<AMessage> &msg) {
2617 int32_t what;
2618 CHECK(msg->findInt32("what", &what));
2619
2620 switch (what) {
2621 case NuPlayer::CCDecoder::kWhatClosedCaptionData:
2622 {
2623 sp<ABuffer> buffer;
2624 CHECK(msg->findBuffer("buffer", &buffer));
2625
2626 size_t inbandTracks = 0;
2627 if (mSource != NULL) {
2628 inbandTracks = mSource->getTrackCount();
2629 }
2630
2631 sendSubtitleData(buffer, inbandTracks);
2632 break;
2633 }
2634
2635 case NuPlayer::CCDecoder::kWhatTrackAdded:
2636 {
2637 notifyListener(MEDIA_INFO, MEDIA_INFO_METADATA_UPDATE, 0);
2638
2639 break;
2640 }
2641
2642 default:
2643 TRESPASS();
2644 }
2645
2646
2647}
2648
Chong Zhang404fced2014-06-11 14:45:31 -07002649void NuPlayer::sendSubtitleData(const sp<ABuffer> &buffer, int32_t baseIndex) {
2650 int32_t trackIndex;
2651 int64_t timeUs, durationUs;
2652 CHECK(buffer->meta()->findInt32("trackIndex", &trackIndex));
2653 CHECK(buffer->meta()->findInt64("timeUs", &timeUs));
2654 CHECK(buffer->meta()->findInt64("durationUs", &durationUs));
2655
2656 Parcel in;
2657 in.writeInt32(trackIndex + baseIndex);
2658 in.writeInt64(timeUs);
2659 in.writeInt64(durationUs);
2660 in.writeInt32(buffer->size());
2661 in.writeInt32(buffer->size());
2662 in.write(buffer->data(), buffer->size());
2663
2664 notifyListener(MEDIA_SUBTITLE_DATA, 0, 0, &in);
2665}
Robert Shihd3b0bbb2014-07-23 15:00:25 -07002666
Robert Shih08528432015-04-08 09:06:54 -07002667void NuPlayer::sendTimedMetaData(const sp<ABuffer> &buffer) {
2668 int64_t timeUs;
2669 CHECK(buffer->meta()->findInt64("timeUs", &timeUs));
2670
2671 Parcel in;
2672 in.writeInt64(timeUs);
2673 in.writeInt32(buffer->size());
2674 in.writeInt32(buffer->size());
2675 in.write(buffer->data(), buffer->size());
2676
2677 notifyListener(MEDIA_META_DATA, 0, 0, &in);
2678}
2679
Robert Shihd3b0bbb2014-07-23 15:00:25 -07002680void NuPlayer::sendTimedTextData(const sp<ABuffer> &buffer) {
2681 const void *data;
2682 size_t size = 0;
2683 int64_t timeUs;
Marco Nelissen55e2f4c2015-09-04 15:57:15 -07002684 int32_t flag = TextDescriptions::IN_BAND_TEXT_3GPP;
Robert Shihd3b0bbb2014-07-23 15:00:25 -07002685
2686 AString mime;
2687 CHECK(buffer->meta()->findString("mime", &mime));
2688 CHECK(strcasecmp(mime.c_str(), MEDIA_MIMETYPE_TEXT_3GPP) == 0);
2689
2690 data = buffer->data();
2691 size = buffer->size();
2692
2693 Parcel parcel;
2694 if (size > 0) {
2695 CHECK(buffer->meta()->findInt64("timeUs", &timeUs));
Marco Nelissen55e2f4c2015-09-04 15:57:15 -07002696 int32_t global = 0;
2697 if (buffer->meta()->findInt32("global", &global) && global) {
2698 flag |= TextDescriptions::GLOBAL_DESCRIPTIONS;
2699 } else {
2700 flag |= TextDescriptions::LOCAL_DESCRIPTIONS;
2701 }
Robert Shihd3b0bbb2014-07-23 15:00:25 -07002702 TextDescriptions::getParcelOfDescriptions(
2703 (const uint8_t *)data, size, flag, timeUs / 1000, &parcel);
2704 }
2705
2706 if ((parcel.dataSize() > 0)) {
2707 notifyListener(MEDIA_TIMED_TEXT, 0, 0, &parcel);
2708 } else { // send an empty timed text
2709 notifyListener(MEDIA_TIMED_TEXT, 0, 0);
2710 }
2711}
Hassan Shojaniacefac142017-02-06 21:02:02 -08002712
Hassan Shojaniaff63de72017-04-26 15:10:42 -07002713const char *NuPlayer::getDataSourceType() {
2714 switch (mDataSourceType) {
2715 case DATA_SOURCE_TYPE_HTTP_LIVE:
2716 return "HTTPLive";
2717
2718 case DATA_SOURCE_TYPE_RTSP:
2719 return "RTSP";
2720
2721 case DATA_SOURCE_TYPE_GENERIC_URL:
2722 return "GenURL";
2723
2724 case DATA_SOURCE_TYPE_GENERIC_FD:
2725 return "GenFD";
2726
2727 case DATA_SOURCE_TYPE_MEDIA:
2728 return "Media";
2729
2730 case DATA_SOURCE_TYPE_STREAM:
2731 return "Stream";
2732
2733 case DATA_SOURCE_TYPE_NONE:
2734 default:
2735 return "None";
2736 }
2737 }
2738
Hassan Shojaniacefac142017-02-06 21:02:02 -08002739// Modular DRM begin
2740status_t NuPlayer::prepareDrm(const uint8_t uuid[16], const Vector<uint8_t> &drmSessionId)
2741{
2742 ALOGV("prepareDrm ");
2743
2744 // Passing to the looper anyway; called in a pre-config prepared state so no race on mCrypto
2745 sp<AMessage> msg = new AMessage(kWhatPrepareDrm, this);
2746 // synchronous call so just passing the address but with local copies of "const" args
2747 uint8_t UUID[16];
2748 memcpy(UUID, uuid, sizeof(UUID));
2749 Vector<uint8_t> sessionId = drmSessionId;
2750 msg->setPointer("uuid", (void*)UUID);
2751 msg->setPointer("drmSessionId", (void*)&sessionId);
2752
2753 sp<AMessage> response;
2754 status_t status = msg->postAndAwaitResponse(&response);
2755
2756 if (status == OK && response != NULL) {
2757 CHECK(response->findInt32("status", &status));
2758 ALOGV("prepareDrm ret: %d ", status);
2759 } else {
2760 ALOGE("prepareDrm err: %d", status);
2761 }
2762
2763 return status;
2764}
2765
2766status_t NuPlayer::releaseDrm()
2767{
2768 ALOGV("releaseDrm ");
2769
2770 sp<AMessage> msg = new AMessage(kWhatReleaseDrm, this);
2771
2772 sp<AMessage> response;
2773 status_t status = msg->postAndAwaitResponse(&response);
2774
2775 if (status == OK && response != NULL) {
2776 CHECK(response->findInt32("status", &status));
2777 ALOGV("releaseDrm ret: %d ", status);
2778 } else {
2779 ALOGE("releaseDrm err: %d", status);
2780 }
2781
2782 return status;
2783}
2784
2785status_t NuPlayer::onPrepareDrm(const sp<AMessage> &msg)
2786{
2787 // TODO change to ALOGV
2788 ALOGD("onPrepareDrm ");
2789
2790 status_t status = INVALID_OPERATION;
2791 if (mSource == NULL) {
2792 ALOGE("onPrepareDrm: No source. onPrepareDrm failed with %d.", status);
2793 return status;
2794 }
2795
2796 uint8_t *uuid;
2797 Vector<uint8_t> *drmSessionId;
2798 CHECK(msg->findPointer("uuid", (void**)&uuid));
2799 CHECK(msg->findPointer("drmSessionId", (void**)&drmSessionId));
2800
2801 status = OK;
2802 sp<ICrypto> crypto = NULL;
2803
2804 status = mSource->prepareDrm(uuid, *drmSessionId, &crypto);
2805 if (crypto == NULL) {
2806 ALOGE("onPrepareDrm: mSource->prepareDrm failed. status: %d", status);
2807 return status;
2808 }
2809 ALOGV("onPrepareDrm: mSource->prepareDrm succeeded");
2810
2811 if (mCrypto != NULL) {
2812 ALOGE("onPrepareDrm: Unexpected. Already having mCrypto: %p (%d)",
2813 mCrypto.get(), mCrypto->getStrongCount());
2814 mCrypto.clear();
2815 }
2816
2817 mCrypto = crypto;
2818 mIsDrmProtected = true;
2819 // TODO change to ALOGV
2820 ALOGD("onPrepareDrm: mCrypto: %p (%d)", mCrypto.get(),
2821 (mCrypto != NULL ? mCrypto->getStrongCount() : 0));
2822
2823 return status;
2824}
2825
2826status_t NuPlayer::onReleaseDrm()
2827{
2828 // TODO change to ALOGV
2829 ALOGD("onReleaseDrm ");
2830
Hassan Shojania50b20c92017-02-16 18:28:58 -08002831 if (!mIsDrmProtected) {
2832 ALOGW("onReleaseDrm: Unexpected. mIsDrmProtected is already false.");
2833 }
2834
2835 mIsDrmProtected = false;
Hassan Shojaniacefac142017-02-06 21:02:02 -08002836
2837 status_t status;
2838 if (mCrypto != NULL) {
Hassan Shojania355e8472017-05-12 10:33:16 -07002839 // notifying the source first before removing crypto from codec
2840 if (mSource != NULL) {
2841 mSource->releaseDrm();
2842 }
2843
Hassan Shojaniacefac142017-02-06 21:02:02 -08002844 status=OK;
2845 // first making sure the codecs have released their crypto reference
2846 const sp<DecoderBase> &videoDecoder = getDecoder(false/*audio*/);
2847 if (videoDecoder != NULL) {
2848 status = videoDecoder->releaseCrypto();
2849 ALOGV("onReleaseDrm: video decoder ret: %d", status);
2850 }
2851
2852 const sp<DecoderBase> &audioDecoder = getDecoder(true/*audio*/);
2853 if (audioDecoder != NULL) {
2854 status_t status_audio = audioDecoder->releaseCrypto();
2855 if (status == OK) { // otherwise, returning the first error
2856 status = status_audio;
2857 }
2858 ALOGV("onReleaseDrm: audio decoder ret: %d", status_audio);
2859 }
2860
2861 // TODO change to ALOGV
2862 ALOGD("onReleaseDrm: mCrypto: %p (%d)", mCrypto.get(),
2863 (mCrypto != NULL ? mCrypto->getStrongCount() : 0));
2864 mCrypto.clear();
2865 } else { // mCrypto == NULL
2866 ALOGE("onReleaseDrm: Unexpected. There is no crypto.");
2867 status = INVALID_OPERATION;
2868 }
2869
2870 return status;
2871}
2872// Modular DRM end
Andreas Huberb5f25f02013-02-05 10:14:26 -08002873////////////////////////////////////////////////////////////////////////////////
2874
Chong Zhangced1c2f2014-08-08 15:22:35 -07002875sp<AMessage> NuPlayer::Source::getFormat(bool audio) {
2876 sp<MetaData> meta = getFormatMeta(audio);
2877
2878 if (meta == NULL) {
2879 return NULL;
2880 }
2881
2882 sp<AMessage> msg = new AMessage;
2883
2884 if(convertMetaDataToMessage(meta, &msg) == OK) {
2885 return msg;
2886 }
2887 return NULL;
2888}
2889
Andreas Huber9575c962013-02-05 13:59:56 -08002890void NuPlayer::Source::notifyFlagsChanged(uint32_t flags) {
2891 sp<AMessage> notify = dupNotify();
2892 notify->setInt32("what", kWhatFlagsChanged);
2893 notify->setInt32("flags", flags);
2894 notify->post();
2895}
2896
Chong Zhangced1c2f2014-08-08 15:22:35 -07002897void NuPlayer::Source::notifyVideoSizeChanged(const sp<AMessage> &format) {
Andreas Huber9575c962013-02-05 13:59:56 -08002898 sp<AMessage> notify = dupNotify();
2899 notify->setInt32("what", kWhatVideoSizeChanged);
Chong Zhangced1c2f2014-08-08 15:22:35 -07002900 notify->setMessage("format", format);
Andreas Huber9575c962013-02-05 13:59:56 -08002901 notify->post();
2902}
2903
Andreas Huberec0c5972013-02-05 14:47:13 -08002904void NuPlayer::Source::notifyPrepared(status_t err) {
Hassan Shojaniacefac142017-02-06 21:02:02 -08002905 ALOGV("Source::notifyPrepared %d", err);
Andreas Huber9575c962013-02-05 13:59:56 -08002906 sp<AMessage> notify = dupNotify();
2907 notify->setInt32("what", kWhatPrepared);
Andreas Huberec0c5972013-02-05 14:47:13 -08002908 notify->setInt32("err", err);
Andreas Huber9575c962013-02-05 13:59:56 -08002909 notify->post();
2910}
2911
Hassan Shojaniacefac142017-02-06 21:02:02 -08002912void NuPlayer::Source::notifyDrmInfo(const sp<ABuffer> &drmInfoBuffer)
2913{
2914 ALOGV("Source::notifyDrmInfo");
2915
2916 sp<AMessage> notify = dupNotify();
2917 notify->setInt32("what", kWhatDrmInfo);
2918 notify->setBuffer("drmInfo", drmInfoBuffer);
2919
2920 notify->post();
2921}
2922
Lajos Molnarfcd3e942015-03-31 10:06:48 -07002923void NuPlayer::Source::notifyInstantiateSecureDecoders(const sp<AMessage> &reply) {
2924 sp<AMessage> notify = dupNotify();
2925 notify->setInt32("what", kWhatInstantiateSecureDecoders);
2926 notify->setMessage("reply", reply);
2927 notify->post();
2928}
2929
Andreas Huber84333e02014-02-07 15:36:10 -08002930void NuPlayer::Source::onMessageReceived(const sp<AMessage> & /* msg */) {
Andreas Huberb5f25f02013-02-05 10:14:26 -08002931 TRESPASS();
2932}
2933
Andreas Huberf9334412010-12-15 15:17:42 -08002934} // namespace android