blob: d1e5d453400a790c439a8ac81711a87b49d37018 [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 Jia9bb38032017-03-23 18:00:38 -0700342status_t NuPlayer::getBufferingSettings(
Wei Jia48fa06d2016-12-20 15:30:49 -0800343 BufferingSettings *buffering /* nonnull */) {
Wei Jia9bb38032017-03-23 18:00:38 -0700344 sp<AMessage> msg = new AMessage(kWhatGetBufferingSettings, this);
Wei Jia48fa06d2016-12-20 15:30:49 -0800345 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 Jia9bb38032017-03-23 18:00:38 -0700570 case kWhatGetBufferingSettings:
Wei Jia48fa06d2016-12-20 15:30:49 -0800571 {
572 sp<AReplyToken> replyID;
573 CHECK(msg->senderAwaitsResponse(&replyID));
574
Wei Jia9bb38032017-03-23 18:00:38 -0700575 ALOGV("kWhatGetBufferingSettings");
Wei Jia48fa06d2016-12-20 15:30:49 -0800576 BufferingSettings buffering;
577 status_t err = OK;
578 if (mSource != NULL) {
Wei Jia9bb38032017-03-23 18:00:38 -0700579 err = mSource->getBufferingSettings(&buffering);
Wei Jia48fa06d2016-12-20 15:30:49 -0800580 } 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");
Ray Essick58e0f7a2017-11-21 10:59:38 -08001314 stopRebufferingTimer(true);
Ronghua Wu64c2d172015-10-07 16:52:19 -07001315
Andreas Hubera1f8ab02012-11-30 10:53:22 -08001316 mDeferredActions.push_back(
Wei Jiafef808d2014-10-31 17:57:05 -07001317 new FlushDecoderAction(
1318 FLUSH_CMD_SHUTDOWN /* audio */,
1319 FLUSH_CMD_SHUTDOWN /* video */));
Andreas Huberb7c8e912012-11-27 15:02:53 -08001320
Andreas Hubera1f8ab02012-11-30 10:53:22 -08001321 mDeferredActions.push_back(
1322 new SimpleAction(&NuPlayer::performReset));
Andreas Huberb58ce9f2011-11-28 16:27:35 -08001323
Andreas Hubera1f8ab02012-11-30 10:53:22 -08001324 processDeferredActions();
Andreas Huber1aef2112011-01-04 14:01:29 -08001325 break;
1326 }
1327
Wei Jia52c28512017-09-13 18:17:51 -07001328 case kWhatNotifyTime:
1329 {
1330 ALOGV("kWhatNotifyTime");
1331 int64_t timerUs;
1332 CHECK(msg->findInt64("timerUs", &timerUs));
1333
1334 notifyListener(MEDIA_NOTIFY_TIME, timerUs, 0);
1335 break;
1336 }
1337
Andreas Huber43c3e6c2011-01-05 12:17:08 -08001338 case kWhatSeek:
1339 {
1340 int64_t seekTimeUs;
Wei Jiac5de0912016-11-18 10:22:14 -08001341 int32_t mode;
Wei Jiae427abf2014-09-22 15:21:11 -07001342 int32_t needNotify;
Andreas Huber43c3e6c2011-01-05 12:17:08 -08001343 CHECK(msg->findInt64("seekTimeUs", &seekTimeUs));
Wei Jiac5de0912016-11-18 10:22:14 -08001344 CHECK(msg->findInt32("mode", &mode));
Wei Jiae427abf2014-09-22 15:21:11 -07001345 CHECK(msg->findInt32("needNotify", &needNotify));
Andreas Huber43c3e6c2011-01-05 12:17:08 -08001346
Wei Jiac5de0912016-11-18 10:22:14 -08001347 ALOGV("kWhatSeek seekTimeUs=%lld us, mode=%d, needNotify=%d",
1348 (long long)seekTimeUs, mode, needNotify);
Andreas Huber43c3e6c2011-01-05 12:17:08 -08001349
Wei Jia1061c9c2015-05-19 16:02:17 -07001350 if (!mStarted) {
1351 // Seek before the player is started. In order to preview video,
1352 // need to start the player and pause it. This branch is called
1353 // only once if needed. After the player is started, any seek
1354 // operation will go through normal path.
Robert Shih0c61a0d2015-07-06 15:09:10 -07001355 // Audio-only cases are handled separately.
Wei Jiac5de0912016-11-18 10:22:14 -08001356 onStart(seekTimeUs, (MediaPlayerSeekMode)mode);
Robert Shih0c61a0d2015-07-06 15:09:10 -07001357 if (mStarted) {
1358 onPause();
1359 mPausedByClient = true;
1360 }
Wei Jia1061c9c2015-05-19 16:02:17 -07001361 if (needNotify) {
1362 notifyDriverSeekComplete();
1363 }
1364 break;
1365 }
1366
Andreas Hubera1f8ab02012-11-30 10:53:22 -08001367 mDeferredActions.push_back(
Wei Jiafef808d2014-10-31 17:57:05 -07001368 new FlushDecoderAction(FLUSH_CMD_FLUSH /* audio */,
1369 FLUSH_CMD_FLUSH /* video */));
Andreas Huber43c3e6c2011-01-05 12:17:08 -08001370
Wei Jiae427abf2014-09-22 15:21:11 -07001371 mDeferredActions.push_back(
Wei Jiac5de0912016-11-18 10:22:14 -08001372 new SeekAction(seekTimeUs, (MediaPlayerSeekMode)mode));
Andreas Huber43c3e6c2011-01-05 12:17:08 -08001373
Chong Zhangf8d71772014-11-26 15:08:34 -08001374 // After a flush without shutdown, decoder is paused.
1375 // Don't resume it until source seek is done, otherwise it could
Chong Zhang7137ec72014-11-12 16:41:05 -08001376 // start pulling stale data too soon.
1377 mDeferredActions.push_back(
Chong Zhangf8d71772014-11-26 15:08:34 -08001378 new ResumeDecoderAction(needNotify));
Chong Zhang7137ec72014-11-12 16:41:05 -08001379
Andreas Hubera1f8ab02012-11-30 10:53:22 -08001380 processDeferredActions();
Andreas Huber43c3e6c2011-01-05 12:17:08 -08001381 break;
1382 }
1383
Andreas Huberb4082222011-01-20 15:23:04 -08001384 case kWhatPause:
1385 {
Chong Zhangefbb6192015-01-30 17:13:27 -08001386 onPause();
1387 mPausedByClient = true;
Andreas Huberb4082222011-01-20 15:23:04 -08001388 break;
1389 }
1390
Andreas Huberb5f25f02013-02-05 10:14:26 -08001391 case kWhatSourceNotify:
1392 {
Andreas Huber9575c962013-02-05 13:59:56 -08001393 onSourceNotify(msg);
Andreas Huberb5f25f02013-02-05 10:14:26 -08001394 break;
1395 }
1396
Chong Zhanga7fa1d92014-06-11 14:49:23 -07001397 case kWhatClosedCaptionNotify:
1398 {
1399 onClosedCaptionNotify(msg);
1400 break;
1401 }
1402
Hassan Shojaniacefac142017-02-06 21:02:02 -08001403 case kWhatPrepareDrm:
1404 {
1405 status_t status = onPrepareDrm(msg);
1406
1407 sp<AMessage> response = new AMessage;
1408 response->setInt32("status", status);
1409 sp<AReplyToken> replyID;
1410 CHECK(msg->senderAwaitsResponse(&replyID));
1411 response->postReply(replyID);
1412 break;
1413 }
1414
1415 case kWhatReleaseDrm:
1416 {
1417 status_t status = onReleaseDrm();
1418
1419 sp<AMessage> response = new AMessage;
1420 response->setInt32("status", status);
1421 sp<AReplyToken> replyID;
1422 CHECK(msg->senderAwaitsResponse(&replyID));
1423 response->postReply(replyID);
1424 break;
1425 }
1426
Andreas Huberf9334412010-12-15 15:17:42 -08001427 default:
1428 TRESPASS();
1429 break;
1430 }
1431}
1432
Wei Jia94211742014-10-28 17:09:06 -07001433void NuPlayer::onResume() {
Ronghua Wu64c2d172015-10-07 16:52:19 -07001434 if (!mPaused || mResetting) {
1435 ALOGD_IF(mResetting, "resetting, onResume discarded");
Chong Zhangefbb6192015-01-30 17:13:27 -08001436 return;
1437 }
1438 mPaused = false;
Wei Jia94211742014-10-28 17:09:06 -07001439 if (mSource != NULL) {
1440 mSource->resume();
1441 } else {
1442 ALOGW("resume called when source is gone or not set");
1443 }
1444 // |mAudioDecoder| may have been released due to the pause timeout, so re-create it if
1445 // needed.
1446 if (audioDecoderStillNeeded() && mAudioDecoder == NULL) {
1447 instantiateDecoder(true /* audio */, &mAudioDecoder);
1448 }
1449 if (mRenderer != NULL) {
1450 mRenderer->resume();
1451 } else {
1452 ALOGW("resume called when renderer is gone or not set");
1453 }
Ray Essickd4d00612017-01-03 09:36:27 -08001454
Ray Essick0d98c182017-11-09 13:42:42 -08001455 startPlaybackTimer("onresume");
Wei Jia94211742014-10-28 17:09:06 -07001456}
1457
Lajos Molnarfcd3e942015-03-31 10:06:48 -07001458status_t NuPlayer::onInstantiateSecureDecoders() {
1459 status_t err;
1460 if (!(mSourceFlags & Source::FLAG_SECURE)) {
1461 return BAD_TYPE;
1462 }
1463
1464 if (mRenderer != NULL) {
1465 ALOGE("renderer should not be set when instantiating secure decoders");
1466 return UNKNOWN_ERROR;
1467 }
1468
1469 // TRICKY: We rely on mRenderer being null, so that decoder does not start requesting
1470 // data on instantiation.
Lajos Molnar1de1e252015-04-30 18:18:34 -07001471 if (mSurface != NULL) {
Lajos Molnarfcd3e942015-03-31 10:06:48 -07001472 err = instantiateDecoder(false, &mVideoDecoder);
1473 if (err != OK) {
1474 return err;
1475 }
1476 }
1477
1478 if (mAudioSink != NULL) {
1479 err = instantiateDecoder(true, &mAudioDecoder);
1480 if (err != OK) {
1481 return err;
1482 }
1483 }
1484 return OK;
1485}
1486
Wei Jiac5de0912016-11-18 10:22:14 -08001487void NuPlayer::onStart(int64_t startPositionUs, MediaPlayerSeekMode mode) {
Hassan Shojaniacefac142017-02-06 21:02:02 -08001488 ALOGV("onStart: mCrypto: %p (%d)", mCrypto.get(),
1489 (mCrypto != NULL ? mCrypto->getStrongCount() : 0));
1490
Robert Shih0c61a0d2015-07-06 15:09:10 -07001491 if (!mSourceStarted) {
1492 mSourceStarted = true;
1493 mSource->start();
1494 }
1495 if (startPositionUs > 0) {
Wei Jiac5de0912016-11-18 10:22:14 -08001496 performSeek(startPositionUs, mode);
Robert Shih0c61a0d2015-07-06 15:09:10 -07001497 if (mSource->getFormat(false /* audio */) == NULL) {
1498 return;
1499 }
1500 }
1501
Wei Jia94211742014-10-28 17:09:06 -07001502 mOffloadAudio = false;
1503 mAudioEOS = false;
1504 mVideoEOS = false;
Wei Jia94211742014-10-28 17:09:06 -07001505 mStarted = true;
Wei Jiafe8fe7d2016-06-08 10:29:25 -07001506 mPaused = false;
Wei Jia94211742014-10-28 17:09:06 -07001507
Wei Jia94211742014-10-28 17:09:06 -07001508 uint32_t flags = 0;
1509
1510 if (mSource->isRealTime()) {
1511 flags |= Renderer::FLAG_REAL_TIME;
1512 }
1513
Wei Jia9737d342016-12-28 14:59:59 -08001514 bool hasAudio = (mSource->getFormat(true /* audio */) != NULL);
1515 bool hasVideo = (mSource->getFormat(false /* audio */) != NULL);
1516 if (!hasAudio && !hasVideo) {
Wei Jia1de83d52016-10-10 10:15:03 -07001517 ALOGE("no metadata for either audio or video source");
1518 mSource->stop();
1519 mSourceStarted = false;
1520 notifyListener(MEDIA_ERROR, MEDIA_ERROR_UNKNOWN, ERROR_MALFORMED);
1521 return;
1522 }
Wei Jia9737d342016-12-28 14:59:59 -08001523 ALOGV_IF(!hasAudio, "no metadata for audio source"); // video only stream
1524
1525 sp<MetaData> audioMeta = mSource->getFormatMeta(true /* audio */);
Wei Jia1de83d52016-10-10 10:15:03 -07001526
Wei Jia94211742014-10-28 17:09:06 -07001527 audio_stream_type_t streamType = AUDIO_STREAM_MUSIC;
1528 if (mAudioSink != NULL) {
1529 streamType = mAudioSink->getAudioStreamType();
1530 }
1531
Wei Jia94211742014-10-28 17:09:06 -07001532 mOffloadAudio =
Wei Jia9737d342016-12-28 14:59:59 -08001533 canOffloadStream(audioMeta, hasVideo, mSource->isStreaming(), streamType)
Wei Jiabf70feb2016-02-19 15:47:16 -08001534 && (mPlaybackSettings.mSpeed == 1.f && mPlaybackSettings.mPitch == 1.f);
Hassan Shojaniacefac142017-02-06 21:02:02 -08001535
1536 // Modular DRM: Disabling audio offload if the source is protected
1537 if (mOffloadAudio && mIsDrmProtected) {
1538 mOffloadAudio = false;
1539 ALOGV("onStart: Disabling mOffloadAudio now that the source is protected.");
1540 }
1541
Wei Jia94211742014-10-28 17:09:06 -07001542 if (mOffloadAudio) {
1543 flags |= Renderer::FLAG_OFFLOAD_AUDIO;
1544 }
1545
Lajos Molnar1d15ab52015-03-04 16:46:34 -08001546 sp<AMessage> notify = new AMessage(kWhatRendererNotify, this);
Wei Jia94211742014-10-28 17:09:06 -07001547 ++mRendererGeneration;
1548 notify->setInt32("generation", mRendererGeneration);
Wei Jia0a68f662017-08-30 18:01:26 -07001549 mRenderer = new Renderer(mAudioSink, mMediaClock, notify, flags);
Wei Jia94211742014-10-28 17:09:06 -07001550 mRendererLooper = new ALooper;
1551 mRendererLooper->setName("NuPlayerRenderer");
1552 mRendererLooper->start(false, false, ANDROID_PRIORITY_AUDIO);
1553 mRendererLooper->registerHandler(mRenderer);
Lajos Molnar3a474aa2015-04-24 17:10:07 -07001554
1555 status_t err = mRenderer->setPlaybackSettings(mPlaybackSettings);
1556 if (err != OK) {
1557 mSource->stop();
Robert Shih0c61a0d2015-07-06 15:09:10 -07001558 mSourceStarted = false;
Lajos Molnar3a474aa2015-04-24 17:10:07 -07001559 notifyListener(MEDIA_ERROR, MEDIA_ERROR_UNKNOWN, err);
1560 return;
Wei Jiac8206ff2015-03-04 13:59:37 -08001561 }
Wei Jia94211742014-10-28 17:09:06 -07001562
Ronghua Wuc8a70d32015-04-29 16:26:34 -07001563 float rate = getFrameRate();
1564 if (rate > 0) {
Wei Jia94211742014-10-28 17:09:06 -07001565 mRenderer->setVideoFrameRate(rate);
1566 }
1567
Wei Jiac6cfd702014-11-11 16:33:20 -08001568 if (mVideoDecoder != NULL) {
1569 mVideoDecoder->setRenderer(mRenderer);
1570 }
1571 if (mAudioDecoder != NULL) {
1572 mAudioDecoder->setRenderer(mRenderer);
1573 }
1574
Ray Essick0d98c182017-11-09 13:42:42 -08001575 startPlaybackTimer("onstart");
Ray Essickd4d00612017-01-03 09:36:27 -08001576
Wei Jia94211742014-10-28 17:09:06 -07001577 postScanSources();
1578}
1579
Ray Essick0d98c182017-11-09 13:42:42 -08001580void NuPlayer::startPlaybackTimer(const char *where) {
1581 Mutex::Autolock autoLock(mPlayingTimeLock);
1582 if (mLastStartedPlayingTimeNs == 0) {
1583 mLastStartedPlayingTimeNs = systemTime();
1584 ALOGV("startPlaybackTimer() time %20" PRId64 " (%s)", mLastStartedPlayingTimeNs, where);
1585 }
1586}
1587
1588void NuPlayer::stopPlaybackTimer(const char *where) {
1589 Mutex::Autolock autoLock(mPlayingTimeLock);
1590
1591 ALOGV("stopPlaybackTimer() time %20" PRId64 " (%s)", mLastStartedPlayingTimeNs, where);
1592
1593 if (mLastStartedPlayingTimeNs != 0) {
1594 sp<NuPlayerDriver> driver = mDriver.promote();
1595 if (driver != NULL) {
1596 int64_t now = systemTime();
1597 int64_t played = now - mLastStartedPlayingTimeNs;
1598 ALOGV("stopPlaybackTimer() log %20" PRId64 "", played);
1599
1600 if (played > 0) {
1601 driver->notifyMorePlayingTimeUs((played+500)/1000);
1602 }
1603 }
1604 mLastStartedPlayingTimeNs = 0;
1605 }
1606}
1607
Ray Essick58e0f7a2017-11-21 10:59:38 -08001608void NuPlayer::startRebufferingTimer() {
1609 Mutex::Autolock autoLock(mPlayingTimeLock);
1610 if (mLastStartedRebufferingTimeNs == 0) {
1611 mLastStartedRebufferingTimeNs = systemTime();
1612 ALOGV("startRebufferingTimer() time %20" PRId64 "", mLastStartedRebufferingTimeNs);
1613 }
1614}
1615
1616void NuPlayer::stopRebufferingTimer(bool exitingPlayback) {
1617 Mutex::Autolock autoLock(mPlayingTimeLock);
1618
1619 ALOGV("stopRebufferTimer() time %20" PRId64 " (exiting %d)", mLastStartedRebufferingTimeNs, exitingPlayback);
1620
1621 if (mLastStartedRebufferingTimeNs != 0) {
1622 sp<NuPlayerDriver> driver = mDriver.promote();
1623 if (driver != NULL) {
1624 int64_t now = systemTime();
1625 int64_t rebuffered = now - mLastStartedRebufferingTimeNs;
1626 ALOGV("stopRebufferingTimer() log %20" PRId64 "", rebuffered);
1627
1628 if (rebuffered > 0) {
1629 driver->notifyMoreRebufferingTimeUs((rebuffered+500)/1000);
1630 if (exitingPlayback) {
1631 driver->notifyRebufferingWhenExit(true);
1632 }
1633 }
1634 }
1635 mLastStartedRebufferingTimeNs = 0;
1636 }
1637}
1638
Chong Zhangefbb6192015-01-30 17:13:27 -08001639void NuPlayer::onPause() {
Ray Essick0d98c182017-11-09 13:42:42 -08001640
1641 stopPlaybackTimer("onPause");
1642
Chong Zhangefbb6192015-01-30 17:13:27 -08001643 if (mPaused) {
1644 return;
1645 }
1646 mPaused = true;
1647 if (mSource != NULL) {
1648 mSource->pause();
1649 } else {
1650 ALOGW("pause called when source is gone or not set");
1651 }
1652 if (mRenderer != NULL) {
1653 mRenderer->pause();
1654 } else {
1655 ALOGW("pause called when renderer is gone or not set");
1656 }
Ray Essickd4d00612017-01-03 09:36:27 -08001657
Chong Zhangefbb6192015-01-30 17:13:27 -08001658}
1659
Ronghua Wud7988b12014-10-03 15:19:10 -07001660bool NuPlayer::audioDecoderStillNeeded() {
1661 // Audio decoder is no longer needed if it's in shut/shutting down status.
1662 return ((mFlushingAudio != SHUT_DOWN) && (mFlushingAudio != SHUTTING_DOWN_DECODER));
1663}
1664
Andy Hung8d121d42014-10-03 09:53:53 -07001665void NuPlayer::handleFlushComplete(bool audio, bool isDecoder) {
1666 // We wait for both the decoder flush and the renderer flush to complete
1667 // before entering either the FLUSHED or the SHUTTING_DOWN_DECODER state.
1668
1669 mFlushComplete[audio][isDecoder] = true;
1670 if (!mFlushComplete[audio][!isDecoder]) {
1671 return;
1672 }
1673
1674 FlushStatus *state = audio ? &mFlushingAudio : &mFlushingVideo;
1675 switch (*state) {
1676 case FLUSHING_DECODER:
1677 {
1678 *state = FLUSHED;
Andy Hung8d121d42014-10-03 09:53:53 -07001679 break;
1680 }
1681
1682 case FLUSHING_DECODER_SHUTDOWN:
1683 {
1684 *state = SHUTTING_DOWN_DECODER;
1685
1686 ALOGV("initiating %s decoder shutdown", audio ? "audio" : "video");
Andy Hung8d121d42014-10-03 09:53:53 -07001687 getDecoder(audio)->initiateShutdown();
1688 break;
1689 }
1690
1691 default:
1692 // decoder flush completes only occur in a flushing state.
1693 LOG_ALWAYS_FATAL_IF(isDecoder, "decoder flush in invalid state %d", *state);
1694 break;
1695 }
1696}
1697
Andreas Huber3831a062010-12-21 10:22:33 -08001698void NuPlayer::finishFlushIfPossible() {
Wei Jia53904f32014-07-29 10:22:53 -07001699 if (mFlushingAudio != NONE && mFlushingAudio != FLUSHED
1700 && mFlushingAudio != SHUT_DOWN) {
Andreas Huber3831a062010-12-21 10:22:33 -08001701 return;
1702 }
1703
Wei Jia53904f32014-07-29 10:22:53 -07001704 if (mFlushingVideo != NONE && mFlushingVideo != FLUSHED
1705 && mFlushingVideo != SHUT_DOWN) {
Andreas Huber3831a062010-12-21 10:22:33 -08001706 return;
1707 }
1708
Steve Block3856b092011-10-20 11:56:00 +01001709 ALOGV("both audio and video are flushed now.");
Andreas Huber3831a062010-12-21 10:22:33 -08001710
Andreas Huber3831a062010-12-21 10:22:33 -08001711 mFlushingAudio = NONE;
1712 mFlushingVideo = NONE;
Andreas Huber3831a062010-12-21 10:22:33 -08001713
Andy Hung8d121d42014-10-03 09:53:53 -07001714 clearFlushComplete();
1715
Andreas Hubera1f8ab02012-11-30 10:53:22 -08001716 processDeferredActions();
Andreas Huber1aef2112011-01-04 14:01:29 -08001717}
1718
1719void NuPlayer::postScanSources() {
1720 if (mScanSourcesPending) {
1721 return;
1722 }
1723
Lajos Molnar1d15ab52015-03-04 16:46:34 -08001724 sp<AMessage> msg = new AMessage(kWhatScanSources, this);
Andreas Huber1aef2112011-01-04 14:01:29 -08001725 msg->setInt32("generation", mScanSourcesGeneration);
1726 msg->post();
1727
1728 mScanSourcesPending = true;
1729}
1730
Wei Jia41cd4632016-05-13 10:53:30 -07001731void NuPlayer::tryOpenAudioSinkForOffload(
1732 const sp<AMessage> &format, const sp<MetaData> &audioMeta, bool hasVideo) {
Andy Hung202bce12014-12-03 11:47:36 -08001733 // Note: This is called early in NuPlayer to determine whether offloading
1734 // is possible; otherwise the decoders call the renderer openAudioSink directly.
Andy Hung282a7e32014-08-14 15:56:34 -07001735
Andy Hung202bce12014-12-03 11:47:36 -08001736 status_t err = mRenderer->openAudioSink(
Dhananjay Kumarc387f2b2015-08-06 10:43:16 +05301737 format, true /* offloadOnly */, hasVideo,
1738 AUDIO_OUTPUT_FLAG_NONE, &mOffloadAudio, mSource->isStreaming());
Andy Hung202bce12014-12-03 11:47:36 -08001739 if (err != OK) {
1740 // Any failure we turn off mOffloadAudio.
1741 mOffloadAudio = false;
1742 } else if (mOffloadAudio) {
Chong Zhang3b9eb1f2014-10-15 17:05:08 -07001743 sendMetaDataToHal(mAudioSink, audioMeta);
Andy Hung282a7e32014-08-14 15:56:34 -07001744 }
1745}
1746
1747void NuPlayer::closeAudioSink() {
Chong Zhang3b9eb1f2014-10-15 17:05:08 -07001748 mRenderer->closeAudioSink();
Andy Hung282a7e32014-08-14 15:56:34 -07001749}
1750
Wei Jia5031b2f2016-02-25 11:19:31 -08001751void NuPlayer::restartAudio(
Wei Jiabf70feb2016-02-19 15:47:16 -08001752 int64_t currentPositionUs, bool forceNonOffload, bool needsToCreateAudioDecoder) {
Wei Jiaa05f1e32016-03-25 16:31:22 -07001753 if (mAudioDecoder != NULL) {
1754 mAudioDecoder->pause();
1755 mAudioDecoder.clear();
Wei Jia686e8e52017-04-03 14:08:01 -07001756 mAudioDecoderError = false;
Wei Jiaa05f1e32016-03-25 16:31:22 -07001757 ++mAudioDecoderGeneration;
1758 }
Wei Jiabf70feb2016-02-19 15:47:16 -08001759 if (mFlushingAudio == FLUSHING_DECODER) {
1760 mFlushComplete[1 /* audio */][1 /* isDecoder */] = true;
1761 mFlushingAudio = FLUSHED;
1762 finishFlushIfPossible();
1763 } else if (mFlushingAudio == FLUSHING_DECODER_SHUTDOWN
1764 || mFlushingAudio == SHUTTING_DOWN_DECODER) {
1765 mFlushComplete[1 /* audio */][1 /* isDecoder */] = true;
1766 mFlushingAudio = SHUT_DOWN;
1767 finishFlushIfPossible();
1768 needsToCreateAudioDecoder = false;
1769 }
1770 if (mRenderer == NULL) {
1771 return;
1772 }
1773 closeAudioSink();
1774 mRenderer->flush(true /* audio */, false /* notifyComplete */);
1775 if (mVideoDecoder != NULL) {
1776 mRenderer->flush(false /* audio */, false /* notifyComplete */);
1777 }
1778
Wei Jiac5de0912016-11-18 10:22:14 -08001779 performSeek(currentPositionUs, MediaPlayerSeekMode::SEEK_PREVIOUS_SYNC /* mode */);
Wei Jiabf70feb2016-02-19 15:47:16 -08001780
1781 if (forceNonOffload) {
1782 mRenderer->signalDisableOffloadAudio();
1783 mOffloadAudio = false;
1784 }
1785 if (needsToCreateAudioDecoder) {
Wei Jiaa05f1e32016-03-25 16:31:22 -07001786 instantiateDecoder(true /* audio */, &mAudioDecoder, !forceNonOffload);
Wei Jiabf70feb2016-02-19 15:47:16 -08001787 }
1788}
1789
Wei Jia41cd4632016-05-13 10:53:30 -07001790void NuPlayer::determineAudioModeChange(const sp<AMessage> &audioFormat) {
Wei Jiae4d18c72015-07-13 17:58:11 -07001791 if (mSource == NULL || mAudioSink == NULL) {
1792 return;
1793 }
1794
1795 if (mRenderer == NULL) {
1796 ALOGW("No renderer can be used to determine audio mode. Use non-offload for safety.");
1797 mOffloadAudio = false;
1798 return;
1799 }
1800
1801 sp<MetaData> audioMeta = mSource->getFormatMeta(true /* audio */);
1802 sp<AMessage> videoFormat = mSource->getFormat(false /* audio */);
1803 audio_stream_type_t streamType = mAudioSink->getAudioStreamType();
1804 const bool hasVideo = (videoFormat != NULL);
Hassan Shojaniacefac142017-02-06 21:02:02 -08001805 bool canOffload = canOffloadStream(
Wei Jiabf70feb2016-02-19 15:47:16 -08001806 audioMeta, hasVideo, mSource->isStreaming(), streamType)
1807 && (mPlaybackSettings.mSpeed == 1.f && mPlaybackSettings.mPitch == 1.f);
Hassan Shojaniacefac142017-02-06 21:02:02 -08001808
1809 // Modular DRM: Disabling audio offload if the source is protected
1810 if (canOffload && mIsDrmProtected) {
1811 canOffload = false;
1812 ALOGV("determineAudioModeChange: Disabling mOffloadAudio b/c the source is protected.");
1813 }
1814
Wei Jiae4d18c72015-07-13 17:58:11 -07001815 if (canOffload) {
1816 if (!mOffloadAudio) {
1817 mRenderer->signalEnableOffloadAudio();
1818 }
1819 // open audio sink early under offload mode.
Wei Jia41cd4632016-05-13 10:53:30 -07001820 tryOpenAudioSinkForOffload(audioFormat, audioMeta, hasVideo);
Wei Jiae4d18c72015-07-13 17:58:11 -07001821 } else {
Robert Shihe1d70192015-07-23 17:54:13 -07001822 if (mOffloadAudio) {
1823 mRenderer->signalDisableOffloadAudio();
1824 mOffloadAudio = false;
1825 }
Wei Jiae4d18c72015-07-13 17:58:11 -07001826 }
1827}
1828
Wei Jiaa05f1e32016-03-25 16:31:22 -07001829status_t NuPlayer::instantiateDecoder(
1830 bool audio, sp<DecoderBase> *decoder, bool checkAudioModeChange) {
Wei Jia566da802015-08-27 13:59:30 -07001831 // The audio decoder could be cleared by tear down. If still in shut down
1832 // process, no need to create a new audio decoder.
1833 if (*decoder != NULL || (audio && mFlushingAudio == SHUT_DOWN)) {
Andreas Huberf9334412010-12-15 15:17:42 -08001834 return OK;
1835 }
1836
Andreas Huber84066782011-08-16 09:34:26 -07001837 sp<AMessage> format = mSource->getFormat(audio);
Andreas Huberf9334412010-12-15 15:17:42 -08001838
Andreas Huber84066782011-08-16 09:34:26 -07001839 if (format == NULL) {
Robert Shih7350b052015-10-01 15:50:14 -07001840 return UNKNOWN_ERROR;
1841 } else {
1842 status_t err;
1843 if (format->findInt32("err", &err) && err) {
1844 return err;
1845 }
Andreas Huberf9334412010-12-15 15:17:42 -08001846 }
1847
Ronghua Wu8db88132015-04-22 13:51:35 -07001848 format->setInt32("priority", 0 /* realtime */);
1849
Andreas Huber3fe62152011-09-16 15:09:22 -07001850 if (!audio) {
Andreas Huber84066782011-08-16 09:34:26 -07001851 AString mime;
1852 CHECK(format->findString("mime", &mime));
Chong Zhanga7fa1d92014-06-11 14:49:23 -07001853
Lajos Molnar1d15ab52015-03-04 16:46:34 -08001854 sp<AMessage> ccNotify = new AMessage(kWhatClosedCaptionNotify, this);
Chong Zhang341ab6e2015-02-04 13:37:18 -08001855 if (mCCDecoder == NULL) {
1856 mCCDecoder = new CCDecoder(ccNotify);
1857 }
Lajos Molnar09524832014-07-17 14:29:51 -07001858
1859 if (mSourceFlags & Source::FLAG_SECURE) {
1860 format->setInt32("secure", true);
1861 }
Chong Zhang17134602015-01-07 16:14:34 -08001862
1863 if (mSourceFlags & Source::FLAG_PROTECTED) {
1864 format->setInt32("protected", true);
1865 }
Ronghua Wu8db88132015-04-22 13:51:35 -07001866
Ronghua Wuc8a70d32015-04-29 16:26:34 -07001867 float rate = getFrameRate();
1868 if (rate > 0) {
Lajos Molnar3a474aa2015-04-24 17:10:07 -07001869 format->setFloat("operating-rate", rate * mPlaybackSettings.mSpeed);
Ronghua Wu8db88132015-04-22 13:51:35 -07001870 }
Andreas Huber3fe62152011-09-16 15:09:22 -07001871 }
1872
Wei Jiabc2fb722014-07-08 16:37:57 -07001873 if (audio) {
Lajos Molnar1d15ab52015-03-04 16:46:34 -08001874 sp<AMessage> notify = new AMessage(kWhatAudioNotify, this);
Wei Jia88703c32014-08-06 11:24:07 -07001875 ++mAudioDecoderGeneration;
1876 notify->setInt32("generation", mAudioDecoderGeneration);
1877
Wei Jiaa05f1e32016-03-25 16:31:22 -07001878 if (checkAudioModeChange) {
Wei Jia41cd4632016-05-13 10:53:30 -07001879 determineAudioModeChange(format);
Wei Jiaa05f1e32016-03-25 16:31:22 -07001880 }
Wei Jiabc2fb722014-07-08 16:37:57 -07001881 if (mOffloadAudio) {
Wei Jia14532f22015-12-29 11:28:15 -08001882 mSource->setOffloadAudio(true /* offload */);
1883
Haynes Mathew George8b635332015-03-30 17:59:47 -07001884 const bool hasVideo = (mSource->getFormat(false /*audio */) != NULL);
1885 format->setInt32("has-video", hasVideo);
Wei Jiac6cfd702014-11-11 16:33:20 -08001886 *decoder = new DecoderPassThrough(notify, mSource, mRenderer);
Hassan Shojaniacefac142017-02-06 21:02:02 -08001887 ALOGV("instantiateDecoder audio DecoderPassThrough hasVideo: %d", hasVideo);
Wei Jiabc2fb722014-07-08 16:37:57 -07001888 } else {
Wei Jia14532f22015-12-29 11:28:15 -08001889 mSource->setOffloadAudio(false /* offload */);
1890
Wei Jiaf2ae3e12016-10-27 17:10:59 -07001891 *decoder = new Decoder(notify, mSource, mPID, mUID, mRenderer);
Hassan Shojaniacefac142017-02-06 21:02:02 -08001892 ALOGV("instantiateDecoder audio Decoder");
Wei Jiabc2fb722014-07-08 16:37:57 -07001893 }
Wei Jia686e8e52017-04-03 14:08:01 -07001894 mAudioDecoderError = false;
Wei Jiabc2fb722014-07-08 16:37:57 -07001895 } else {
Lajos Molnar1d15ab52015-03-04 16:46:34 -08001896 sp<AMessage> notify = new AMessage(kWhatVideoNotify, this);
Wei Jia88703c32014-08-06 11:24:07 -07001897 ++mVideoDecoderGeneration;
1898 notify->setInt32("generation", mVideoDecoderGeneration);
1899
Chong Zhang7137ec72014-11-12 16:41:05 -08001900 *decoder = new Decoder(
Wei Jiaf2ae3e12016-10-27 17:10:59 -07001901 notify, mSource, mPID, mUID, mRenderer, mSurface, mCCDecoder);
Wei Jia686e8e52017-04-03 14:08:01 -07001902 mVideoDecoderError = false;
Lajos Molnard9fd6312014-11-06 11:00:00 -08001903
1904 // enable FRC if high-quality AV sync is requested, even if not
Lajos Molnar1de1e252015-04-30 18:18:34 -07001905 // directly queuing to display, as this will even improve textureview
Lajos Molnard9fd6312014-11-06 11:00:00 -08001906 // playback.
1907 {
Marco Nelissen96626b72016-12-01 13:58:59 -08001908 if (property_get_bool("persist.sys.media.avsync", false)) {
Lajos Molnard9fd6312014-11-06 11:00:00 -08001909 format->setInt32("auto-frc", 1);
1910 }
1911 }
Wei Jiabc2fb722014-07-08 16:37:57 -07001912 }
Lajos Molnar1cd13982014-01-17 15:12:51 -08001913 (*decoder)->init();
Hassan Shojaniacefac142017-02-06 21:02:02 -08001914
1915 // Modular DRM
1916 if (mIsDrmProtected) {
1917 format->setPointer("crypto", mCrypto.get());
1918 ALOGV("instantiateDecoder: mCrypto: %p (%d) isSecure: %d", mCrypto.get(),
1919 (mCrypto != NULL ? mCrypto->getStrongCount() : 0),
1920 (mSourceFlags & Source::FLAG_SECURE) != 0);
1921 }
1922
Andreas Huber84066782011-08-16 09:34:26 -07001923 (*decoder)->configure(format);
Andreas Huberf9334412010-12-15 15:17:42 -08001924
Praveen Chavanbbaa1442016-04-08 13:33:49 -07001925 if (!audio) {
1926 sp<AMessage> params = new AMessage();
1927 float rate = getFrameRate();
1928 if (rate > 0) {
1929 params->setFloat("frame-rate-total", rate);
1930 }
1931
1932 sp<MetaData> fileMeta = getFileMeta();
1933 if (fileMeta != NULL) {
1934 int32_t videoTemporalLayerCount;
1935 if (fileMeta->findInt32(kKeyTemporalLayerCount, &videoTemporalLayerCount)
1936 && videoTemporalLayerCount > 0) {
1937 params->setInt32("temporal-layer-count", videoTemporalLayerCount);
1938 }
1939 }
1940
1941 if (params->countEntries() > 0) {
1942 (*decoder)->setParameters(params);
1943 }
1944 }
Andreas Huberf9334412010-12-15 15:17:42 -08001945 return OK;
1946}
1947
Chong Zhangced1c2f2014-08-08 15:22:35 -07001948void NuPlayer::updateVideoSize(
1949 const sp<AMessage> &inputFormat,
1950 const sp<AMessage> &outputFormat) {
1951 if (inputFormat == NULL) {
1952 ALOGW("Unknown video size, reporting 0x0!");
1953 notifyListener(MEDIA_SET_VIDEO_SIZE, 0, 0);
1954 return;
1955 }
Wei Jia9737d342016-12-28 14:59:59 -08001956 int32_t err = OK;
1957 inputFormat->findInt32("err", &err);
1958 if (err == -EWOULDBLOCK) {
1959 ALOGW("Video meta is not available yet!");
1960 return;
1961 }
1962 if (err != OK) {
1963 ALOGW("Something is wrong with video meta!");
1964 return;
1965 }
Chong Zhangced1c2f2014-08-08 15:22:35 -07001966
1967 int32_t displayWidth, displayHeight;
Chong Zhangced1c2f2014-08-08 15:22:35 -07001968 if (outputFormat != NULL) {
1969 int32_t width, height;
1970 CHECK(outputFormat->findInt32("width", &width));
1971 CHECK(outputFormat->findInt32("height", &height));
1972
1973 int32_t cropLeft, cropTop, cropRight, cropBottom;
1974 CHECK(outputFormat->findRect(
1975 "crop",
1976 &cropLeft, &cropTop, &cropRight, &cropBottom));
1977
1978 displayWidth = cropRight - cropLeft + 1;
1979 displayHeight = cropBottom - cropTop + 1;
1980
1981 ALOGV("Video output format changed to %d x %d "
1982 "(crop: %d x %d @ (%d, %d))",
1983 width, height,
1984 displayWidth,
1985 displayHeight,
1986 cropLeft, cropTop);
1987 } else {
1988 CHECK(inputFormat->findInt32("width", &displayWidth));
1989 CHECK(inputFormat->findInt32("height", &displayHeight));
1990
1991 ALOGV("Video input format %d x %d", displayWidth, displayHeight);
1992 }
1993
1994 // Take into account sample aspect ratio if necessary:
1995 int32_t sarWidth, sarHeight;
1996 if (inputFormat->findInt32("sar-width", &sarWidth)
Wei Jia095bc252017-01-27 10:16:16 -08001997 && inputFormat->findInt32("sar-height", &sarHeight)
1998 && sarWidth > 0 && sarHeight > 0) {
Chong Zhangced1c2f2014-08-08 15:22:35 -07001999 ALOGV("Sample aspect ratio %d : %d", sarWidth, sarHeight);
2000
2001 displayWidth = (displayWidth * sarWidth) / sarHeight;
2002
2003 ALOGV("display dimensions %d x %d", displayWidth, displayHeight);
Wei Jiadf6c6af2016-09-29 11:27:15 -07002004 } else {
2005 int32_t width, height;
2006 if (inputFormat->findInt32("display-width", &width)
2007 && inputFormat->findInt32("display-height", &height)
2008 && width > 0 && height > 0
2009 && displayWidth > 0 && displayHeight > 0) {
2010 if (displayHeight * (int64_t)width / height > (int64_t)displayWidth) {
2011 displayHeight = (int32_t)(displayWidth * (int64_t)height / width);
2012 } else {
2013 displayWidth = (int32_t)(displayHeight * (int64_t)width / height);
2014 }
2015 ALOGV("Video display width and height are overridden to %d x %d",
2016 displayWidth, displayHeight);
2017 }
Chong Zhangced1c2f2014-08-08 15:22:35 -07002018 }
2019
2020 int32_t rotationDegrees;
2021 if (!inputFormat->findInt32("rotation-degrees", &rotationDegrees)) {
2022 rotationDegrees = 0;
2023 }
2024
2025 if (rotationDegrees == 90 || rotationDegrees == 270) {
2026 int32_t tmp = displayWidth;
2027 displayWidth = displayHeight;
2028 displayHeight = tmp;
2029 }
2030
2031 notifyListener(
2032 MEDIA_SET_VIDEO_SIZE,
2033 displayWidth,
2034 displayHeight);
2035}
2036
Chong Zhangdcb89b32013-08-06 09:44:47 -07002037void NuPlayer::notifyListener(int msg, int ext1, int ext2, const Parcel *in) {
Andreas Huber43c3e6c2011-01-05 12:17:08 -08002038 if (mDriver == NULL) {
Andreas Huberf9334412010-12-15 15:17:42 -08002039 return;
2040 }
2041
Andreas Huber43c3e6c2011-01-05 12:17:08 -08002042 sp<NuPlayerDriver> driver = mDriver.promote();
Andreas Huberf9334412010-12-15 15:17:42 -08002043
Andreas Huber43c3e6c2011-01-05 12:17:08 -08002044 if (driver == NULL) {
Andreas Huberf9334412010-12-15 15:17:42 -08002045 return;
2046 }
2047
Chong Zhangdcb89b32013-08-06 09:44:47 -07002048 driver->notifyListener(msg, ext1, ext2, in);
Andreas Huberf9334412010-12-15 15:17:42 -08002049}
2050
Chong Zhang7137ec72014-11-12 16:41:05 -08002051void NuPlayer::flushDecoder(bool audio, bool needShutdown) {
Andreas Huber14f76722013-01-15 09:04:18 -08002052 ALOGV("[%s] flushDecoder needShutdown=%d",
2053 audio ? "audio" : "video", needShutdown);
2054
Chong Zhang7137ec72014-11-12 16:41:05 -08002055 const sp<DecoderBase> &decoder = getDecoder(audio);
Lajos Molnar87603c02014-08-20 19:25:30 -07002056 if (decoder == NULL) {
Steve Blockdf64d152012-01-04 20:05:49 +00002057 ALOGI("flushDecoder %s without decoder present",
Andreas Huber6e3d3112011-11-28 12:36:11 -08002058 audio ? "audio" : "video");
Lajos Molnar87603c02014-08-20 19:25:30 -07002059 return;
Andreas Huber6e3d3112011-11-28 12:36:11 -08002060 }
2061
Andreas Huber1aef2112011-01-04 14:01:29 -08002062 // Make sure we don't continue to scan sources until we finish flushing.
2063 ++mScanSourcesGeneration;
Chong Zhang3b032b32015-04-17 15:49:06 -07002064 if (mScanSourcesPending) {
Toshikazu Saitocbcbb792015-09-15 14:53:01 +09002065 if (!needShutdown) {
2066 mDeferredActions.push_back(
2067 new SimpleAction(&NuPlayer::performScanSources));
2068 }
Chong Zhang3b032b32015-04-17 15:49:06 -07002069 mScanSourcesPending = false;
2070 }
Andreas Huber1aef2112011-01-04 14:01:29 -08002071
Chong Zhang7137ec72014-11-12 16:41:05 -08002072 decoder->signalFlush();
Andreas Huber1aef2112011-01-04 14:01:29 -08002073
2074 FlushStatus newStatus =
2075 needShutdown ? FLUSHING_DECODER_SHUTDOWN : FLUSHING_DECODER;
2076
Lajos Molnarfcd3e942015-03-31 10:06:48 -07002077 mFlushComplete[audio][false /* isDecoder */] = (mRenderer == NULL);
Andy Hung8d121d42014-10-03 09:53:53 -07002078 mFlushComplete[audio][true /* isDecoder */] = false;
Andreas Huber1aef2112011-01-04 14:01:29 -08002079 if (audio) {
Wei Jia53904f32014-07-29 10:22:53 -07002080 ALOGE_IF(mFlushingAudio != NONE,
2081 "audio flushDecoder() is called in state %d", mFlushingAudio);
Andreas Huber1aef2112011-01-04 14:01:29 -08002082 mFlushingAudio = newStatus;
Andreas Huber1aef2112011-01-04 14:01:29 -08002083 } else {
Wei Jia53904f32014-07-29 10:22:53 -07002084 ALOGE_IF(mFlushingVideo != NONE,
2085 "video flushDecoder() is called in state %d", mFlushingVideo);
Andreas Huber1aef2112011-01-04 14:01:29 -08002086 mFlushingVideo = newStatus;
Andreas Huber1aef2112011-01-04 14:01:29 -08002087 }
2088}
2089
Chong Zhangced1c2f2014-08-08 15:22:35 -07002090void NuPlayer::queueDecoderShutdown(
2091 bool audio, bool video, const sp<AMessage> &reply) {
2092 ALOGI("queueDecoderShutdown audio=%d, video=%d", audio, video);
Andreas Huber84066782011-08-16 09:34:26 -07002093
Chong Zhangced1c2f2014-08-08 15:22:35 -07002094 mDeferredActions.push_back(
Wei Jiafef808d2014-10-31 17:57:05 -07002095 new FlushDecoderAction(
2096 audio ? FLUSH_CMD_SHUTDOWN : FLUSH_CMD_NONE,
2097 video ? FLUSH_CMD_SHUTDOWN : FLUSH_CMD_NONE));
Andreas Huber84066782011-08-16 09:34:26 -07002098
Chong Zhangced1c2f2014-08-08 15:22:35 -07002099 mDeferredActions.push_back(
2100 new SimpleAction(&NuPlayer::performScanSources));
Andreas Huber84066782011-08-16 09:34:26 -07002101
Chong Zhangced1c2f2014-08-08 15:22:35 -07002102 mDeferredActions.push_back(new PostMessageAction(reply));
2103
2104 processDeferredActions();
Andreas Huber84066782011-08-16 09:34:26 -07002105}
2106
James Dong0d268a32012-08-31 12:18:27 -07002107status_t NuPlayer::setVideoScalingMode(int32_t mode) {
2108 mVideoScalingMode = mode;
Lajos Molnar1de1e252015-04-30 18:18:34 -07002109 if (mSurface != NULL) {
2110 status_t ret = native_window_set_scaling_mode(mSurface.get(), mVideoScalingMode);
James Dong0d268a32012-08-31 12:18:27 -07002111 if (ret != OK) {
2112 ALOGE("Failed to set scaling mode (%d): %s",
2113 -ret, strerror(-ret));
2114 return ret;
2115 }
2116 }
2117 return OK;
2118}
2119
Chong Zhangdcb89b32013-08-06 09:44:47 -07002120status_t NuPlayer::getTrackInfo(Parcel* reply) const {
Lajos Molnar1d15ab52015-03-04 16:46:34 -08002121 sp<AMessage> msg = new AMessage(kWhatGetTrackInfo, this);
Chong Zhangdcb89b32013-08-06 09:44:47 -07002122 msg->setPointer("reply", reply);
2123
2124 sp<AMessage> response;
2125 status_t err = msg->postAndAwaitResponse(&response);
2126 return err;
2127}
2128
Robert Shih7c4f0d72014-07-09 18:53:31 -07002129status_t NuPlayer::getSelectedTrack(int32_t type, Parcel* reply) const {
Lajos Molnar1d15ab52015-03-04 16:46:34 -08002130 sp<AMessage> msg = new AMessage(kWhatGetSelectedTrack, this);
Robert Shih7c4f0d72014-07-09 18:53:31 -07002131 msg->setPointer("reply", reply);
2132 msg->setInt32("type", type);
2133
2134 sp<AMessage> response;
2135 status_t err = msg->postAndAwaitResponse(&response);
2136 if (err == OK && response != NULL) {
2137 CHECK(response->findInt32("err", &err));
2138 }
2139 return err;
2140}
2141
Robert Shih6ffb1fd2014-10-29 16:24:32 -07002142status_t NuPlayer::selectTrack(size_t trackIndex, bool select, int64_t timeUs) {
Lajos Molnar1d15ab52015-03-04 16:46:34 -08002143 sp<AMessage> msg = new AMessage(kWhatSelectTrack, this);
Chong Zhangdcb89b32013-08-06 09:44:47 -07002144 msg->setSize("trackIndex", trackIndex);
2145 msg->setInt32("select", select);
Robert Shih6ffb1fd2014-10-29 16:24:32 -07002146 msg->setInt64("timeUs", timeUs);
Chong Zhangdcb89b32013-08-06 09:44:47 -07002147
2148 sp<AMessage> response;
2149 status_t err = msg->postAndAwaitResponse(&response);
2150
Chong Zhang404fced2014-06-11 14:45:31 -07002151 if (err != OK) {
2152 return err;
2153 }
2154
2155 if (!response->findInt32("err", &err)) {
2156 err = OK;
2157 }
2158
Chong Zhangdcb89b32013-08-06 09:44:47 -07002159 return err;
2160}
2161
Ronghua Wua73d9e02014-10-08 15:13:29 -07002162status_t NuPlayer::getCurrentPosition(int64_t *mediaUs) {
2163 sp<Renderer> renderer = mRenderer;
2164 if (renderer == NULL) {
2165 return NO_INIT;
2166 }
2167
2168 return renderer->getCurrentPosition(mediaUs);
2169}
2170
Praveen Chavane1e5d7a2015-05-19 19:09:48 -07002171void NuPlayer::getStats(Vector<sp<AMessage> > *mTrackStats) {
2172 CHECK(mTrackStats != NULL);
2173
2174 mTrackStats->clear();
2175 if (mVideoDecoder != NULL) {
2176 mTrackStats->push_back(mVideoDecoder->getStats());
2177 }
2178 if (mAudioDecoder != NULL) {
2179 mTrackStats->push_back(mAudioDecoder->getStats());
Chong Zhang7137ec72014-11-12 16:41:05 -08002180 }
Ronghua Wua73d9e02014-10-08 15:13:29 -07002181}
2182
Marco Nelissenf0b72b52014-09-16 15:43:44 -07002183sp<MetaData> NuPlayer::getFileMeta() {
2184 return mSource->getFileFormatMeta();
2185}
2186
Ronghua Wuc8a70d32015-04-29 16:26:34 -07002187float NuPlayer::getFrameRate() {
2188 sp<MetaData> meta = mSource->getFormatMeta(false /* audio */);
2189 if (meta == NULL) {
2190 return 0;
2191 }
2192 int32_t rate;
2193 if (!meta->findInt32(kKeyFrameRate, &rate)) {
2194 // fall back to try file meta
2195 sp<MetaData> fileMeta = getFileMeta();
2196 if (fileMeta == NULL) {
2197 ALOGW("source has video meta but not file meta");
2198 return -1;
2199 }
2200 int32_t fileMetaRate;
2201 if (!fileMeta->findInt32(kKeyFrameRate, &fileMetaRate)) {
2202 return -1;
2203 }
2204 return fileMetaRate;
2205 }
2206 return rate;
2207}
2208
Andreas Huberb7c8e912012-11-27 15:02:53 -08002209void NuPlayer::schedulePollDuration() {
Lajos Molnar1d15ab52015-03-04 16:46:34 -08002210 sp<AMessage> msg = new AMessage(kWhatPollDuration, this);
Andreas Huberb7c8e912012-11-27 15:02:53 -08002211 msg->setInt32("generation", mPollDurationGeneration);
2212 msg->post();
2213}
2214
2215void NuPlayer::cancelPollDuration() {
2216 ++mPollDurationGeneration;
2217}
2218
Andreas Hubera1f8ab02012-11-30 10:53:22 -08002219void NuPlayer::processDeferredActions() {
2220 while (!mDeferredActions.empty()) {
2221 // We won't execute any deferred actions until we're no longer in
2222 // an intermediate state, i.e. one more more decoders are currently
2223 // flushing or shutting down.
2224
Andreas Hubera1f8ab02012-11-30 10:53:22 -08002225 if (mFlushingAudio != NONE || mFlushingVideo != NONE) {
2226 // We're currently flushing, postpone the reset until that's
2227 // completed.
2228
2229 ALOGV("postponing action mFlushingAudio=%d, mFlushingVideo=%d",
2230 mFlushingAudio, mFlushingVideo);
2231
2232 break;
2233 }
2234
2235 sp<Action> action = *mDeferredActions.begin();
2236 mDeferredActions.erase(mDeferredActions.begin());
2237
2238 action->execute(this);
2239 }
2240}
2241
Wei Jiac5de0912016-11-18 10:22:14 -08002242void NuPlayer::performSeek(int64_t seekTimeUs, MediaPlayerSeekMode mode) {
2243 ALOGV("performSeek seekTimeUs=%lld us (%.2f secs), mode=%d",
2244 (long long)seekTimeUs, seekTimeUs / 1E6, mode);
Andreas Hubera1f8ab02012-11-30 10:53:22 -08002245
Andy Hungadf34bf2014-09-03 18:22:22 -07002246 if (mSource == NULL) {
2247 // This happens when reset occurs right before the loop mode
2248 // asynchronously seeks to the start of the stream.
2249 LOG_ALWAYS_FATAL_IF(mAudioDecoder != NULL || mVideoDecoder != NULL,
2250 "mSource is NULL and decoders not NULL audio(%p) video(%p)",
2251 mAudioDecoder.get(), mVideoDecoder.get());
2252 return;
2253 }
Robert Shih1a5c8592015-08-04 18:07:44 -07002254 mPreviousSeekTimeUs = seekTimeUs;
Wei Jiac5de0912016-11-18 10:22:14 -08002255 mSource->seekTo(seekTimeUs, mode);
Robert Shihd3b0bbb2014-07-23 15:00:25 -07002256 ++mTimedTextGeneration;
Andreas Hubera1f8ab02012-11-30 10:53:22 -08002257
Andreas Hubera1f8ab02012-11-30 10:53:22 -08002258 // everything's flushed, continue playback.
2259}
2260
Wei Jiafef808d2014-10-31 17:57:05 -07002261void NuPlayer::performDecoderFlush(FlushCommand audio, FlushCommand video) {
2262 ALOGV("performDecoderFlush audio=%d, video=%d", audio, video);
Andreas Hubera1f8ab02012-11-30 10:53:22 -08002263
Wei Jiafef808d2014-10-31 17:57:05 -07002264 if ((audio == FLUSH_CMD_NONE || mAudioDecoder == NULL)
2265 && (video == FLUSH_CMD_NONE || mVideoDecoder == NULL)) {
Andreas Hubera1f8ab02012-11-30 10:53:22 -08002266 return;
2267 }
2268
Wei Jiafef808d2014-10-31 17:57:05 -07002269 if (audio != FLUSH_CMD_NONE && mAudioDecoder != NULL) {
2270 flushDecoder(true /* audio */, (audio == FLUSH_CMD_SHUTDOWN));
Andreas Hubera1f8ab02012-11-30 10:53:22 -08002271 }
2272
Wei Jiafef808d2014-10-31 17:57:05 -07002273 if (video != FLUSH_CMD_NONE && mVideoDecoder != NULL) {
2274 flushDecoder(false /* audio */, (video == FLUSH_CMD_SHUTDOWN));
Andreas Hubera1f8ab02012-11-30 10:53:22 -08002275 }
2276}
2277
2278void NuPlayer::performReset() {
2279 ALOGV("performReset");
2280
2281 CHECK(mAudioDecoder == NULL);
2282 CHECK(mVideoDecoder == NULL);
2283
Ray Essick0d98c182017-11-09 13:42:42 -08002284 stopPlaybackTimer("performReset");
Ray Essick58e0f7a2017-11-21 10:59:38 -08002285 stopRebufferingTimer(true);
Ray Essick0d98c182017-11-09 13:42:42 -08002286
Andreas Hubera1f8ab02012-11-30 10:53:22 -08002287 cancelPollDuration();
2288
2289 ++mScanSourcesGeneration;
2290 mScanSourcesPending = false;
2291
Lajos Molnar09524832014-07-17 14:29:51 -07002292 if (mRendererLooper != NULL) {
2293 if (mRenderer != NULL) {
2294 mRendererLooper->unregisterHandler(mRenderer->id());
2295 }
2296 mRendererLooper->stop();
2297 mRendererLooper.clear();
2298 }
Andreas Hubera1f8ab02012-11-30 10:53:22 -08002299 mRenderer.clear();
Wei Jia57568df2014-09-22 10:16:29 -07002300 ++mRendererGeneration;
Andreas Hubera1f8ab02012-11-30 10:53:22 -08002301
2302 if (mSource != NULL) {
2303 mSource->stop();
Andreas Huberb5f25f02013-02-05 10:14:26 -08002304
Wei Jiac45a4b22016-04-15 15:30:23 -07002305 Mutex::Autolock autoLock(mSourceLock);
Andreas Hubera1f8ab02012-11-30 10:53:22 -08002306 mSource.clear();
2307 }
2308
2309 if (mDriver != NULL) {
2310 sp<NuPlayerDriver> driver = mDriver.promote();
2311 if (driver != NULL) {
2312 driver->notifyResetComplete();
2313 }
2314 }
Andreas Huber57a339c2012-12-03 11:18:00 -08002315
2316 mStarted = false;
Wei Jia8a092d32016-06-03 14:57:24 -07002317 mPrepared = false;
Ronghua Wu64c2d172015-10-07 16:52:19 -07002318 mResetting = false;
Robert Shih0c61a0d2015-07-06 15:09:10 -07002319 mSourceStarted = false;
Hassan Shojaniacefac142017-02-06 21:02:02 -08002320
2321 // Modular DRM
2322 if (mCrypto != NULL) {
2323 // decoders will be flushed before this so their mCrypto would go away on their own
2324 // TODO change to ALOGV
2325 ALOGD("performReset mCrypto: %p (%d)", mCrypto.get(),
2326 (mCrypto != NULL ? mCrypto->getStrongCount() : 0));
2327 mCrypto.clear();
2328 }
2329 mIsDrmProtected = false;
Andreas Hubera1f8ab02012-11-30 10:53:22 -08002330}
2331
2332void NuPlayer::performScanSources() {
2333 ALOGV("performScanSources");
2334
Andreas Huber57a339c2012-12-03 11:18:00 -08002335 if (!mStarted) {
2336 return;
2337 }
2338
Andreas Hubera1f8ab02012-11-30 10:53:22 -08002339 if (mAudioDecoder == NULL || mVideoDecoder == NULL) {
2340 postScanSources();
2341 }
2342}
2343
Lajos Molnar1de1e252015-04-30 18:18:34 -07002344void NuPlayer::performSetSurface(const sp<Surface> &surface) {
Andreas Huber57a339c2012-12-03 11:18:00 -08002345 ALOGV("performSetSurface");
2346
Lajos Molnar1de1e252015-04-30 18:18:34 -07002347 mSurface = surface;
Andreas Huber57a339c2012-12-03 11:18:00 -08002348
2349 // XXX - ignore error from setVideoScalingMode for now
2350 setVideoScalingMode(mVideoScalingMode);
Chong Zhang13d6faa2014-08-22 15:35:28 -07002351
2352 if (mDriver != NULL) {
2353 sp<NuPlayerDriver> driver = mDriver.promote();
2354 if (driver != NULL) {
2355 driver->notifySetSurfaceComplete();
2356 }
2357 }
Andreas Huber57a339c2012-12-03 11:18:00 -08002358}
2359
Chong Zhangf8d71772014-11-26 15:08:34 -08002360void NuPlayer::performResumeDecoders(bool needNotify) {
2361 if (needNotify) {
2362 mResumePending = true;
2363 if (mVideoDecoder == NULL) {
2364 // if audio-only, we can notify seek complete now,
2365 // as the resume operation will be relatively fast.
2366 finishResume();
2367 }
2368 }
2369
Chong Zhang7137ec72014-11-12 16:41:05 -08002370 if (mVideoDecoder != NULL) {
Chong Zhangf8d71772014-11-26 15:08:34 -08002371 // When there is continuous seek, MediaPlayer will cache the seek
2372 // position, and send down new seek request when previous seek is
2373 // complete. Let's wait for at least one video output frame before
2374 // notifying seek complete, so that the video thumbnail gets updated
2375 // when seekbar is dragged.
2376 mVideoDecoder->signalResume(needNotify);
Chong Zhang7137ec72014-11-12 16:41:05 -08002377 }
2378
2379 if (mAudioDecoder != NULL) {
Chong Zhangf8d71772014-11-26 15:08:34 -08002380 mAudioDecoder->signalResume(false /* needNotify */);
2381 }
2382}
2383
2384void NuPlayer::finishResume() {
2385 if (mResumePending) {
2386 mResumePending = false;
Wei Jia1061c9c2015-05-19 16:02:17 -07002387 notifyDriverSeekComplete();
2388 }
2389}
2390
2391void NuPlayer::notifyDriverSeekComplete() {
2392 if (mDriver != NULL) {
2393 sp<NuPlayerDriver> driver = mDriver.promote();
2394 if (driver != NULL) {
2395 driver->notifySeekComplete();
Chong Zhangf8d71772014-11-26 15:08:34 -08002396 }
Chong Zhang7137ec72014-11-12 16:41:05 -08002397 }
2398}
2399
Andreas Huber9575c962013-02-05 13:59:56 -08002400void NuPlayer::onSourceNotify(const sp<AMessage> &msg) {
2401 int32_t what;
2402 CHECK(msg->findInt32("what", &what));
2403
2404 switch (what) {
Lajos Molnarfcd3e942015-03-31 10:06:48 -07002405 case Source::kWhatInstantiateSecureDecoders:
2406 {
2407 if (mSource == NULL) {
2408 // This is a stale notification from a source that was
2409 // asynchronously preparing when the client called reset().
2410 // We handled the reset, the source is gone.
2411 break;
2412 }
2413
2414 sp<AMessage> reply;
2415 CHECK(msg->findMessage("reply", &reply));
2416 status_t err = onInstantiateSecureDecoders();
2417 reply->setInt32("err", err);
2418 reply->post();
2419 break;
2420 }
2421
Andreas Huber9575c962013-02-05 13:59:56 -08002422 case Source::kWhatPrepared:
2423 {
Hassan Shojaniacefac142017-02-06 21:02:02 -08002424 ALOGV("NuPlayer::onSourceNotify Source::kWhatPrepared source: %p", mSource.get());
Andreas Huberb5f28d42013-04-25 15:11:19 -07002425 if (mSource == NULL) {
2426 // This is a stale notification from a source that was
2427 // asynchronously preparing when the client called reset().
2428 // We handled the reset, the source is gone.
2429 break;
2430 }
2431
Andreas Huberec0c5972013-02-05 14:47:13 -08002432 int32_t err;
2433 CHECK(msg->findInt32("err", &err));
2434
Lajos Molnarfcd3e942015-03-31 10:06:48 -07002435 if (err != OK) {
2436 // shut down potential secure codecs in case client never calls reset
2437 mDeferredActions.push_back(
2438 new FlushDecoderAction(FLUSH_CMD_SHUTDOWN /* audio */,
2439 FLUSH_CMD_SHUTDOWN /* video */));
2440 processDeferredActions();
Wei Jia8a092d32016-06-03 14:57:24 -07002441 } else {
2442 mPrepared = true;
Lajos Molnarfcd3e942015-03-31 10:06:48 -07002443 }
2444
Andreas Huber9575c962013-02-05 13:59:56 -08002445 sp<NuPlayerDriver> driver = mDriver.promote();
2446 if (driver != NULL) {
Marco Nelissendd114d12014-05-28 15:23:14 -07002447 // notify duration first, so that it's definitely set when
2448 // the app received the "prepare complete" callback.
2449 int64_t durationUs;
2450 if (mSource->getDuration(&durationUs) == OK) {
2451 driver->notifyDuration(durationUs);
2452 }
Andreas Huberec0c5972013-02-05 14:47:13 -08002453 driver->notifyPrepareCompleted(err);
Andreas Huber9575c962013-02-05 13:59:56 -08002454 }
Andreas Huber99759402013-04-01 14:28:31 -07002455
Andreas Huber9575c962013-02-05 13:59:56 -08002456 break;
2457 }
2458
Hassan Shojaniacefac142017-02-06 21:02:02 -08002459 // Modular DRM
2460 case Source::kWhatDrmInfo:
2461 {
2462 Parcel parcel;
2463 sp<ABuffer> drmInfo;
2464 CHECK(msg->findBuffer("drmInfo", &drmInfo));
2465 parcel.setData(drmInfo->data(), drmInfo->size());
2466
2467 ALOGV("onSourceNotify() kWhatDrmInfo MEDIA_DRM_INFO drmInfo: %p parcel size: %zu",
2468 drmInfo.get(), parcel.dataSize());
2469
2470 notifyListener(MEDIA_DRM_INFO, 0 /* ext1 */, 0 /* ext2 */, &parcel);
2471
2472 break;
2473 }
2474
Andreas Huber9575c962013-02-05 13:59:56 -08002475 case Source::kWhatFlagsChanged:
2476 {
2477 uint32_t flags;
2478 CHECK(msg->findInt32("flags", (int32_t *)&flags));
2479
Chong Zhang4b7069d2013-09-11 12:52:43 -07002480 sp<NuPlayerDriver> driver = mDriver.promote();
2481 if (driver != NULL) {
Hassan Shojaniacefac142017-02-06 21:02:02 -08002482
2483 ALOGV("onSourceNotify() kWhatFlagsChanged FLAG_CAN_PAUSE: %d "
2484 "FLAG_CAN_SEEK_BACKWARD: %d \n\t\t\t\t FLAG_CAN_SEEK_FORWARD: %d "
2485 "FLAG_CAN_SEEK: %d FLAG_DYNAMIC_DURATION: %d \n"
2486 "\t\t\t\t FLAG_SECURE: %d FLAG_PROTECTED: %d",
2487 (flags & Source::FLAG_CAN_PAUSE) != 0,
2488 (flags & Source::FLAG_CAN_SEEK_BACKWARD) != 0,
2489 (flags & Source::FLAG_CAN_SEEK_FORWARD) != 0,
2490 (flags & Source::FLAG_CAN_SEEK) != 0,
2491 (flags & Source::FLAG_DYNAMIC_DURATION) != 0,
2492 (flags & Source::FLAG_SECURE) != 0,
2493 (flags & Source::FLAG_PROTECTED) != 0);
2494
Wei Jia895651b2014-12-10 17:31:52 -08002495 if ((flags & NuPlayer::Source::FLAG_CAN_SEEK) == 0) {
2496 driver->notifyListener(
2497 MEDIA_INFO, MEDIA_INFO_NOT_SEEKABLE, 0);
2498 }
Chong Zhang4b7069d2013-09-11 12:52:43 -07002499 driver->notifyFlagsChanged(flags);
2500 }
2501
Andreas Huber9575c962013-02-05 13:59:56 -08002502 if ((mSourceFlags & Source::FLAG_DYNAMIC_DURATION)
2503 && (!(flags & Source::FLAG_DYNAMIC_DURATION))) {
2504 cancelPollDuration();
2505 } else if (!(mSourceFlags & Source::FLAG_DYNAMIC_DURATION)
2506 && (flags & Source::FLAG_DYNAMIC_DURATION)
2507 && (mAudioDecoder != NULL || mVideoDecoder != NULL)) {
2508 schedulePollDuration();
2509 }
2510
2511 mSourceFlags = flags;
2512 break;
2513 }
2514
2515 case Source::kWhatVideoSizeChanged:
2516 {
Chong Zhangced1c2f2014-08-08 15:22:35 -07002517 sp<AMessage> format;
2518 CHECK(msg->findMessage("format", &format));
Andreas Huber9575c962013-02-05 13:59:56 -08002519
Chong Zhangced1c2f2014-08-08 15:22:35 -07002520 updateVideoSize(format);
Andreas Huber9575c962013-02-05 13:59:56 -08002521 break;
2522 }
2523
Chong Zhang2a3cc9a2014-08-21 17:48:26 -07002524 case Source::kWhatBufferingUpdate:
2525 {
2526 int32_t percentage;
2527 CHECK(msg->findInt32("percentage", &percentage));
2528
2529 notifyListener(MEDIA_BUFFERING_UPDATE, percentage, 0);
2530 break;
2531 }
2532
Chong Zhangefbb6192015-01-30 17:13:27 -08002533 case Source::kWhatPauseOnBufferingStart:
2534 {
2535 // ignore if not playing
Chong Zhang8a048332015-05-06 15:16:28 -07002536 if (mStarted) {
Chong Zhangefbb6192015-01-30 17:13:27 -08002537 ALOGI("buffer low, pausing...");
2538
Ray Essick58e0f7a2017-11-21 10:59:38 -08002539 startRebufferingTimer();
Chong Zhang8a048332015-05-06 15:16:28 -07002540 mPausedForBuffering = true;
Chong Zhangefbb6192015-01-30 17:13:27 -08002541 onPause();
2542 }
Wei Jiabfe82072016-05-20 09:21:50 -07002543 notifyListener(MEDIA_INFO, MEDIA_INFO_BUFFERING_START, 0);
Roger Jönssonb50e83e2013-01-21 16:26:41 +01002544 break;
2545 }
2546
Chong Zhangefbb6192015-01-30 17:13:27 -08002547 case Source::kWhatResumeOnBufferingEnd:
2548 {
2549 // ignore if not playing
Chong Zhang8a048332015-05-06 15:16:28 -07002550 if (mStarted) {
Chong Zhangefbb6192015-01-30 17:13:27 -08002551 ALOGI("buffer ready, resuming...");
2552
Ray Essick58e0f7a2017-11-21 10:59:38 -08002553 stopRebufferingTimer(false);
Chong Zhang8a048332015-05-06 15:16:28 -07002554 mPausedForBuffering = false;
2555
2556 // do not resume yet if client didn't unpause
2557 if (!mPausedByClient) {
2558 onResume();
2559 }
Chong Zhangefbb6192015-01-30 17:13:27 -08002560 }
Wei Jia3bed45a2016-02-17 11:06:47 -08002561 notifyListener(MEDIA_INFO, MEDIA_INFO_BUFFERING_END, 0);
Roger Jönssonb50e83e2013-01-21 16:26:41 +01002562 break;
2563 }
2564
Chong Zhangefbb6192015-01-30 17:13:27 -08002565 case Source::kWhatCacheStats:
2566 {
2567 int32_t kbps;
2568 CHECK(msg->findInt32("bandwidth", &kbps));
2569
2570 notifyListener(MEDIA_INFO, MEDIA_INFO_NETWORK_BANDWIDTH, kbps);
2571 break;
2572 }
2573
Chong Zhangdcb89b32013-08-06 09:44:47 -07002574 case Source::kWhatSubtitleData:
2575 {
2576 sp<ABuffer> buffer;
2577 CHECK(msg->findBuffer("buffer", &buffer));
2578
Chong Zhang404fced2014-06-11 14:45:31 -07002579 sendSubtitleData(buffer, 0 /* baseIndex */);
Chong Zhangdcb89b32013-08-06 09:44:47 -07002580 break;
2581 }
2582
Robert Shih08528432015-04-08 09:06:54 -07002583 case Source::kWhatTimedMetaData:
2584 {
2585 sp<ABuffer> buffer;
2586 if (!msg->findBuffer("buffer", &buffer)) {
2587 notifyListener(MEDIA_INFO, MEDIA_INFO_METADATA_UPDATE, 0);
2588 } else {
2589 sendTimedMetaData(buffer);
2590 }
2591 break;
2592 }
2593
Robert Shihd3b0bbb2014-07-23 15:00:25 -07002594 case Source::kWhatTimedTextData:
2595 {
2596 int32_t generation;
2597 if (msg->findInt32("generation", &generation)
2598 && generation != mTimedTextGeneration) {
2599 break;
2600 }
2601
2602 sp<ABuffer> buffer;
2603 CHECK(msg->findBuffer("buffer", &buffer));
2604
2605 sp<NuPlayerDriver> driver = mDriver.promote();
2606 if (driver == NULL) {
2607 break;
2608 }
2609
2610 int posMs;
2611 int64_t timeUs, posUs;
2612 driver->getCurrentPosition(&posMs);
Patrik2 Carlsson24d484b2015-01-27 16:49:45 +01002613 posUs = (int64_t) posMs * 1000ll;
Robert Shihd3b0bbb2014-07-23 15:00:25 -07002614 CHECK(buffer->meta()->findInt64("timeUs", &timeUs));
2615
2616 if (posUs < timeUs) {
2617 if (!msg->findInt32("generation", &generation)) {
2618 msg->setInt32("generation", mTimedTextGeneration);
2619 }
2620 msg->post(timeUs - posUs);
2621 } else {
2622 sendTimedTextData(buffer);
2623 }
2624 break;
2625 }
2626
Andreas Huber14f76722013-01-15 09:04:18 -08002627 case Source::kWhatQueueDecoderShutdown:
2628 {
2629 int32_t audio, video;
2630 CHECK(msg->findInt32("audio", &audio));
2631 CHECK(msg->findInt32("video", &video));
2632
2633 sp<AMessage> reply;
2634 CHECK(msg->findMessage("reply", &reply));
2635
2636 queueDecoderShutdown(audio, video, reply);
2637 break;
2638 }
2639
Ronghua Wu80276872014-08-28 15:50:29 -07002640 case Source::kWhatDrmNoLicense:
2641 {
2642 notifyListener(MEDIA_ERROR, MEDIA_ERROR_UNKNOWN, ERROR_DRM_NO_LICENSE);
2643 break;
2644 }
2645
Andreas Huber9575c962013-02-05 13:59:56 -08002646 default:
2647 TRESPASS();
2648 }
2649}
2650
Chong Zhanga7fa1d92014-06-11 14:49:23 -07002651void NuPlayer::onClosedCaptionNotify(const sp<AMessage> &msg) {
2652 int32_t what;
2653 CHECK(msg->findInt32("what", &what));
2654
2655 switch (what) {
2656 case NuPlayer::CCDecoder::kWhatClosedCaptionData:
2657 {
2658 sp<ABuffer> buffer;
2659 CHECK(msg->findBuffer("buffer", &buffer));
2660
2661 size_t inbandTracks = 0;
2662 if (mSource != NULL) {
2663 inbandTracks = mSource->getTrackCount();
2664 }
2665
2666 sendSubtitleData(buffer, inbandTracks);
2667 break;
2668 }
2669
2670 case NuPlayer::CCDecoder::kWhatTrackAdded:
2671 {
2672 notifyListener(MEDIA_INFO, MEDIA_INFO_METADATA_UPDATE, 0);
2673
2674 break;
2675 }
2676
2677 default:
2678 TRESPASS();
2679 }
2680
2681
2682}
2683
Chong Zhang404fced2014-06-11 14:45:31 -07002684void NuPlayer::sendSubtitleData(const sp<ABuffer> &buffer, int32_t baseIndex) {
2685 int32_t trackIndex;
2686 int64_t timeUs, durationUs;
2687 CHECK(buffer->meta()->findInt32("trackIndex", &trackIndex));
2688 CHECK(buffer->meta()->findInt64("timeUs", &timeUs));
2689 CHECK(buffer->meta()->findInt64("durationUs", &durationUs));
2690
2691 Parcel in;
2692 in.writeInt32(trackIndex + baseIndex);
2693 in.writeInt64(timeUs);
2694 in.writeInt64(durationUs);
2695 in.writeInt32(buffer->size());
2696 in.writeInt32(buffer->size());
2697 in.write(buffer->data(), buffer->size());
2698
2699 notifyListener(MEDIA_SUBTITLE_DATA, 0, 0, &in);
2700}
Robert Shihd3b0bbb2014-07-23 15:00:25 -07002701
Robert Shih08528432015-04-08 09:06:54 -07002702void NuPlayer::sendTimedMetaData(const sp<ABuffer> &buffer) {
2703 int64_t timeUs;
2704 CHECK(buffer->meta()->findInt64("timeUs", &timeUs));
2705
2706 Parcel in;
2707 in.writeInt64(timeUs);
2708 in.writeInt32(buffer->size());
2709 in.writeInt32(buffer->size());
2710 in.write(buffer->data(), buffer->size());
2711
2712 notifyListener(MEDIA_META_DATA, 0, 0, &in);
2713}
2714
Robert Shihd3b0bbb2014-07-23 15:00:25 -07002715void NuPlayer::sendTimedTextData(const sp<ABuffer> &buffer) {
2716 const void *data;
2717 size_t size = 0;
2718 int64_t timeUs;
Marco Nelissen55e2f4c2015-09-04 15:57:15 -07002719 int32_t flag = TextDescriptions::IN_BAND_TEXT_3GPP;
Robert Shihd3b0bbb2014-07-23 15:00:25 -07002720
2721 AString mime;
2722 CHECK(buffer->meta()->findString("mime", &mime));
2723 CHECK(strcasecmp(mime.c_str(), MEDIA_MIMETYPE_TEXT_3GPP) == 0);
2724
2725 data = buffer->data();
2726 size = buffer->size();
2727
2728 Parcel parcel;
2729 if (size > 0) {
2730 CHECK(buffer->meta()->findInt64("timeUs", &timeUs));
Marco Nelissen55e2f4c2015-09-04 15:57:15 -07002731 int32_t global = 0;
2732 if (buffer->meta()->findInt32("global", &global) && global) {
2733 flag |= TextDescriptions::GLOBAL_DESCRIPTIONS;
2734 } else {
2735 flag |= TextDescriptions::LOCAL_DESCRIPTIONS;
2736 }
Robert Shihd3b0bbb2014-07-23 15:00:25 -07002737 TextDescriptions::getParcelOfDescriptions(
2738 (const uint8_t *)data, size, flag, timeUs / 1000, &parcel);
2739 }
2740
2741 if ((parcel.dataSize() > 0)) {
2742 notifyListener(MEDIA_TIMED_TEXT, 0, 0, &parcel);
2743 } else { // send an empty timed text
2744 notifyListener(MEDIA_TIMED_TEXT, 0, 0);
2745 }
2746}
Hassan Shojaniacefac142017-02-06 21:02:02 -08002747
Hassan Shojaniaff63de72017-04-26 15:10:42 -07002748const char *NuPlayer::getDataSourceType() {
2749 switch (mDataSourceType) {
2750 case DATA_SOURCE_TYPE_HTTP_LIVE:
2751 return "HTTPLive";
2752
2753 case DATA_SOURCE_TYPE_RTSP:
2754 return "RTSP";
2755
2756 case DATA_SOURCE_TYPE_GENERIC_URL:
2757 return "GenURL";
2758
2759 case DATA_SOURCE_TYPE_GENERIC_FD:
2760 return "GenFD";
2761
2762 case DATA_SOURCE_TYPE_MEDIA:
2763 return "Media";
2764
2765 case DATA_SOURCE_TYPE_STREAM:
2766 return "Stream";
2767
2768 case DATA_SOURCE_TYPE_NONE:
2769 default:
2770 return "None";
2771 }
2772 }
2773
Hassan Shojaniacefac142017-02-06 21:02:02 -08002774// Modular DRM begin
2775status_t NuPlayer::prepareDrm(const uint8_t uuid[16], const Vector<uint8_t> &drmSessionId)
2776{
2777 ALOGV("prepareDrm ");
2778
2779 // Passing to the looper anyway; called in a pre-config prepared state so no race on mCrypto
2780 sp<AMessage> msg = new AMessage(kWhatPrepareDrm, this);
2781 // synchronous call so just passing the address but with local copies of "const" args
2782 uint8_t UUID[16];
2783 memcpy(UUID, uuid, sizeof(UUID));
2784 Vector<uint8_t> sessionId = drmSessionId;
2785 msg->setPointer("uuid", (void*)UUID);
2786 msg->setPointer("drmSessionId", (void*)&sessionId);
2787
2788 sp<AMessage> response;
2789 status_t status = msg->postAndAwaitResponse(&response);
2790
2791 if (status == OK && response != NULL) {
2792 CHECK(response->findInt32("status", &status));
2793 ALOGV("prepareDrm ret: %d ", status);
2794 } else {
2795 ALOGE("prepareDrm err: %d", status);
2796 }
2797
2798 return status;
2799}
2800
2801status_t NuPlayer::releaseDrm()
2802{
2803 ALOGV("releaseDrm ");
2804
2805 sp<AMessage> msg = new AMessage(kWhatReleaseDrm, this);
2806
2807 sp<AMessage> response;
2808 status_t status = msg->postAndAwaitResponse(&response);
2809
2810 if (status == OK && response != NULL) {
2811 CHECK(response->findInt32("status", &status));
2812 ALOGV("releaseDrm ret: %d ", status);
2813 } else {
2814 ALOGE("releaseDrm err: %d", status);
2815 }
2816
2817 return status;
2818}
2819
2820status_t NuPlayer::onPrepareDrm(const sp<AMessage> &msg)
2821{
2822 // TODO change to ALOGV
2823 ALOGD("onPrepareDrm ");
2824
2825 status_t status = INVALID_OPERATION;
2826 if (mSource == NULL) {
2827 ALOGE("onPrepareDrm: No source. onPrepareDrm failed with %d.", status);
2828 return status;
2829 }
2830
2831 uint8_t *uuid;
2832 Vector<uint8_t> *drmSessionId;
2833 CHECK(msg->findPointer("uuid", (void**)&uuid));
2834 CHECK(msg->findPointer("drmSessionId", (void**)&drmSessionId));
2835
2836 status = OK;
2837 sp<ICrypto> crypto = NULL;
2838
2839 status = mSource->prepareDrm(uuid, *drmSessionId, &crypto);
2840 if (crypto == NULL) {
2841 ALOGE("onPrepareDrm: mSource->prepareDrm failed. status: %d", status);
2842 return status;
2843 }
2844 ALOGV("onPrepareDrm: mSource->prepareDrm succeeded");
2845
2846 if (mCrypto != NULL) {
2847 ALOGE("onPrepareDrm: Unexpected. Already having mCrypto: %p (%d)",
2848 mCrypto.get(), mCrypto->getStrongCount());
2849 mCrypto.clear();
2850 }
2851
2852 mCrypto = crypto;
2853 mIsDrmProtected = true;
2854 // TODO change to ALOGV
2855 ALOGD("onPrepareDrm: mCrypto: %p (%d)", mCrypto.get(),
2856 (mCrypto != NULL ? mCrypto->getStrongCount() : 0));
2857
2858 return status;
2859}
2860
2861status_t NuPlayer::onReleaseDrm()
2862{
2863 // TODO change to ALOGV
2864 ALOGD("onReleaseDrm ");
2865
Hassan Shojania50b20c92017-02-16 18:28:58 -08002866 if (!mIsDrmProtected) {
2867 ALOGW("onReleaseDrm: Unexpected. mIsDrmProtected is already false.");
2868 }
2869
2870 mIsDrmProtected = false;
Hassan Shojaniacefac142017-02-06 21:02:02 -08002871
2872 status_t status;
2873 if (mCrypto != NULL) {
Hassan Shojania355e8472017-05-12 10:33:16 -07002874 // notifying the source first before removing crypto from codec
2875 if (mSource != NULL) {
2876 mSource->releaseDrm();
2877 }
2878
Hassan Shojaniacefac142017-02-06 21:02:02 -08002879 status=OK;
2880 // first making sure the codecs have released their crypto reference
2881 const sp<DecoderBase> &videoDecoder = getDecoder(false/*audio*/);
2882 if (videoDecoder != NULL) {
2883 status = videoDecoder->releaseCrypto();
2884 ALOGV("onReleaseDrm: video decoder ret: %d", status);
2885 }
2886
2887 const sp<DecoderBase> &audioDecoder = getDecoder(true/*audio*/);
2888 if (audioDecoder != NULL) {
2889 status_t status_audio = audioDecoder->releaseCrypto();
2890 if (status == OK) { // otherwise, returning the first error
2891 status = status_audio;
2892 }
2893 ALOGV("onReleaseDrm: audio decoder ret: %d", status_audio);
2894 }
2895
2896 // TODO change to ALOGV
2897 ALOGD("onReleaseDrm: mCrypto: %p (%d)", mCrypto.get(),
2898 (mCrypto != NULL ? mCrypto->getStrongCount() : 0));
2899 mCrypto.clear();
2900 } else { // mCrypto == NULL
2901 ALOGE("onReleaseDrm: Unexpected. There is no crypto.");
2902 status = INVALID_OPERATION;
2903 }
2904
2905 return status;
2906}
2907// Modular DRM end
Andreas Huberb5f25f02013-02-05 10:14:26 -08002908////////////////////////////////////////////////////////////////////////////////
2909
Chong Zhangced1c2f2014-08-08 15:22:35 -07002910sp<AMessage> NuPlayer::Source::getFormat(bool audio) {
2911 sp<MetaData> meta = getFormatMeta(audio);
2912
2913 if (meta == NULL) {
2914 return NULL;
2915 }
2916
2917 sp<AMessage> msg = new AMessage;
2918
2919 if(convertMetaDataToMessage(meta, &msg) == OK) {
2920 return msg;
2921 }
2922 return NULL;
2923}
2924
Andreas Huber9575c962013-02-05 13:59:56 -08002925void NuPlayer::Source::notifyFlagsChanged(uint32_t flags) {
2926 sp<AMessage> notify = dupNotify();
2927 notify->setInt32("what", kWhatFlagsChanged);
2928 notify->setInt32("flags", flags);
2929 notify->post();
2930}
2931
Chong Zhangced1c2f2014-08-08 15:22:35 -07002932void NuPlayer::Source::notifyVideoSizeChanged(const sp<AMessage> &format) {
Andreas Huber9575c962013-02-05 13:59:56 -08002933 sp<AMessage> notify = dupNotify();
2934 notify->setInt32("what", kWhatVideoSizeChanged);
Chong Zhangced1c2f2014-08-08 15:22:35 -07002935 notify->setMessage("format", format);
Andreas Huber9575c962013-02-05 13:59:56 -08002936 notify->post();
2937}
2938
Andreas Huberec0c5972013-02-05 14:47:13 -08002939void NuPlayer::Source::notifyPrepared(status_t err) {
Hassan Shojaniacefac142017-02-06 21:02:02 -08002940 ALOGV("Source::notifyPrepared %d", err);
Andreas Huber9575c962013-02-05 13:59:56 -08002941 sp<AMessage> notify = dupNotify();
2942 notify->setInt32("what", kWhatPrepared);
Andreas Huberec0c5972013-02-05 14:47:13 -08002943 notify->setInt32("err", err);
Andreas Huber9575c962013-02-05 13:59:56 -08002944 notify->post();
2945}
2946
Hassan Shojaniacefac142017-02-06 21:02:02 -08002947void NuPlayer::Source::notifyDrmInfo(const sp<ABuffer> &drmInfoBuffer)
2948{
2949 ALOGV("Source::notifyDrmInfo");
2950
2951 sp<AMessage> notify = dupNotify();
2952 notify->setInt32("what", kWhatDrmInfo);
2953 notify->setBuffer("drmInfo", drmInfoBuffer);
2954
2955 notify->post();
2956}
2957
Lajos Molnarfcd3e942015-03-31 10:06:48 -07002958void NuPlayer::Source::notifyInstantiateSecureDecoders(const sp<AMessage> &reply) {
2959 sp<AMessage> notify = dupNotify();
2960 notify->setInt32("what", kWhatInstantiateSecureDecoders);
2961 notify->setMessage("reply", reply);
2962 notify->post();
2963}
2964
Andreas Huber84333e02014-02-07 15:36:10 -08002965void NuPlayer::Source::onMessageReceived(const sp<AMessage> & /* msg */) {
Andreas Huberb5f25f02013-02-05 10:14:26 -08002966 TRESPASS();
2967}
2968
Andreas Huberf9334412010-12-15 15:17:42 -08002969} // namespace android