blob: 274f61321d49c385889c343c421735989478cef4 [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>
Lajos Molnar09524832014-07-17 14:29:51 -070051#include <media/stagefright/MediaBuffer.h>
Wei Jia0a68f662017-08-30 18:01:26 -070052#include <media/stagefright/MediaClock.h>
Andreas Huber3fe62152011-09-16 15:09:22 -070053#include <media/stagefright/MediaDefs.h>
Andreas Huberf9334412010-12-15 15:17:42 -080054#include <media/stagefright/MediaErrors.h>
55#include <media/stagefright/MetaData.h>
Lajos Molnar1de1e252015-04-30 18:18:34 -070056
Andy McFadden8ba01022012-12-18 09:46:54 -080057#include <gui/IGraphicBufferProducer.h>
Lajos Molnar1de1e252015-04-30 18:18:34 -070058#include <gui/Surface.h>
Andreas Huberf9334412010-12-15 15:17:42 -080059
Andreas Huber3fe62152011-09-16 15:09:22 -070060#include "avc_utils.h"
61
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),
Robert Shih1a5c8592015-08-04 18:07:44 -0700185 mPreviousSeekTimeUs(0),
Andreas Huber9b80c2b2011-06-30 15:47:02 -0700186 mAudioEOS(false),
Andreas Huberf9334412010-12-15 15:17:42 -0800187 mVideoEOS(false),
Andreas Huber5bc087c2010-12-23 10:27:40 -0800188 mScanSourcesPending(false),
Andreas Huber1aef2112011-01-04 14:01:29 -0800189 mScanSourcesGeneration(0),
Andreas Huberb7c8e912012-11-27 15:02:53 -0800190 mPollDurationGeneration(0),
Robert Shihd3b0bbb2014-07-23 15:00:25 -0700191 mTimedTextGeneration(0),
Andreas Huberf9334412010-12-15 15:17:42 -0800192 mFlushingAudio(NONE),
Andreas Huber1aef2112011-01-04 14:01:29 -0800193 mFlushingVideo(NONE),
Chong Zhangf8d71772014-11-26 15:08:34 -0800194 mResumePending(false),
Andreas Huber57a339c2012-12-03 11:18:00 -0800195 mVideoScalingMode(NATIVE_WINDOW_SCALING_MODE_SCALE_TO_WINDOW),
Lajos Molnar3a474aa2015-04-24 17:10:07 -0700196 mPlaybackSettings(AUDIO_PLAYBACK_RATE_DEFAULT),
197 mVideoFpsHint(-1.f),
Chong Zhangefbb6192015-01-30 17:13:27 -0800198 mStarted(false),
Wei Jia8a092d32016-06-03 14:57:24 -0700199 mPrepared(false),
Ronghua Wu64c2d172015-10-07 16:52:19 -0700200 mResetting(false),
Robert Shih0c61a0d2015-07-06 15:09:10 -0700201 mSourceStarted(false),
Wei Jia686e8e52017-04-03 14:08:01 -0700202 mAudioDecoderError(false),
203 mVideoDecoderError(false),
Chong Zhangefbb6192015-01-30 17:13:27 -0800204 mPaused(false),
Wei Jia71c75e02016-02-04 09:40:47 -0800205 mPausedByClient(true),
Hassan Shojania50b20c92017-02-16 18:28:58 -0800206 mPausedForBuffering(false),
Hassan Shojaniaff63de72017-04-26 15:10:42 -0700207 mIsDrmProtected(false),
208 mDataSourceType(DATA_SOURCE_TYPE_NONE) {
Wei Jia0a68f662017-08-30 18:01:26 -0700209 CHECK(mediaClock != NULL);
Andy Hung8d121d42014-10-03 09:53:53 -0700210 clearFlushComplete();
Andreas Huberf9334412010-12-15 15:17:42 -0800211}
212
213NuPlayer::~NuPlayer() {
214}
215
Andreas Huber9b80c2b2011-06-30 15:47:02 -0700216void NuPlayer::setUID(uid_t uid) {
217 mUIDValid = true;
218 mUID = uid;
219}
220
Andreas Huber43c3e6c2011-01-05 12:17:08 -0800221void NuPlayer::setDriver(const wp<NuPlayerDriver> &driver) {
222 mDriver = driver;
Andreas Huberf9334412010-12-15 15:17:42 -0800223}
224
Andreas Huber9575c962013-02-05 13:59:56 -0800225void NuPlayer::setDataSourceAsync(const sp<IStreamSource> &source) {
Lajos Molnar1d15ab52015-03-04 16:46:34 -0800226 sp<AMessage> msg = new AMessage(kWhatSetDataSource, this);
Andreas Huberf9334412010-12-15 15:17:42 -0800227
Lajos Molnar1d15ab52015-03-04 16:46:34 -0800228 sp<AMessage> notify = new AMessage(kWhatSourceNotify, this);
Andreas Huberb5f25f02013-02-05 10:14:26 -0800229
Andreas Huber240abcc2014-02-13 13:32:37 -0800230 msg->setObject("source", new StreamingSource(notify, source));
Andreas Huber5bc087c2010-12-23 10:27:40 -0800231 msg->post();
Hassan Shojaniaff63de72017-04-26 15:10:42 -0700232 mDataSourceType = DATA_SOURCE_TYPE_STREAM;
Andreas Huber5bc087c2010-12-23 10:27:40 -0800233}
Andreas Huberf9334412010-12-15 15:17:42 -0800234
Andreas Huberafed0e12011-09-20 15:39:58 -0700235static bool IsHTTPLiveURL(const char *url) {
236 if (!strncasecmp("http://", url, 7)
Andreas Huber99759402013-04-01 14:28:31 -0700237 || !strncasecmp("https://", url, 8)
238 || !strncasecmp("file://", url, 7)) {
Andreas Huberafed0e12011-09-20 15:39:58 -0700239 size_t len = strlen(url);
240 if (len >= 5 && !strcasecmp(".m3u8", &url[len - 5])) {
241 return true;
242 }
243
244 if (strstr(url,"m3u8")) {
245 return true;
246 }
247 }
248
249 return false;
250}
251
Andreas Huber9575c962013-02-05 13:59:56 -0800252void NuPlayer::setDataSourceAsync(
Andreas Huber1b86fe02014-01-29 11:13:26 -0800253 const sp<IMediaHTTPService> &httpService,
254 const char *url,
255 const KeyedVector<String8, String8> *headers) {
Chong Zhang3de157d2014-08-05 20:54:44 -0700256
Lajos Molnar1d15ab52015-03-04 16:46:34 -0800257 sp<AMessage> msg = new AMessage(kWhatSetDataSource, this);
Oscar Rydhé7a33b772012-02-20 10:15:48 +0100258 size_t len = strlen(url);
Andreas Huber5bc087c2010-12-23 10:27:40 -0800259
Lajos Molnar1d15ab52015-03-04 16:46:34 -0800260 sp<AMessage> notify = new AMessage(kWhatSourceNotify, this);
Andreas Huberb5f25f02013-02-05 10:14:26 -0800261
Andreas Huberafed0e12011-09-20 15:39:58 -0700262 sp<Source> source;
263 if (IsHTTPLiveURL(url)) {
Andreas Huber81e68442014-02-05 11:52:33 -0800264 source = new HTTPLiveSource(notify, httpService, url, headers);
Hassan Shojaniacefac142017-02-06 21:02:02 -0800265 ALOGV("setDataSourceAsync HTTPLiveSource %s", url);
Hassan Shojaniaff63de72017-04-26 15:10:42 -0700266 mDataSourceType = DATA_SOURCE_TYPE_HTTP_LIVE;
Andreas Huberafed0e12011-09-20 15:39:58 -0700267 } else if (!strncasecmp(url, "rtsp://", 7)) {
Andreas Huber1b86fe02014-01-29 11:13:26 -0800268 source = new RTSPSource(
269 notify, httpService, url, headers, mUIDValid, mUID);
Hassan Shojaniacefac142017-02-06 21:02:02 -0800270 ALOGV("setDataSourceAsync RTSPSource %s", url);
Hassan Shojaniaff63de72017-04-26 15:10:42 -0700271 mDataSourceType = DATA_SOURCE_TYPE_RTSP;
Oscar Rydhé7a33b772012-02-20 10:15:48 +0100272 } else if ((!strncasecmp(url, "http://", 7)
273 || !strncasecmp(url, "https://", 8))
274 && ((len >= 4 && !strcasecmp(".sdp", &url[len - 4]))
275 || strstr(url, ".sdp?"))) {
Andreas Huber1b86fe02014-01-29 11:13:26 -0800276 source = new RTSPSource(
277 notify, httpService, url, headers, mUIDValid, mUID, true);
Hassan Shojaniacefac142017-02-06 21:02:02 -0800278 ALOGV("setDataSourceAsync RTSPSource http/https/.sdp %s", url);
Hassan Shojaniaff63de72017-04-26 15:10:42 -0700279 mDataSourceType = DATA_SOURCE_TYPE_RTSP;
Andreas Huber2bfdd422011-10-11 15:24:07 -0700280 } else {
Hassan Shojaniacefac142017-02-06 21:02:02 -0800281 ALOGV("setDataSourceAsync GenericSource %s", url);
282
Chong Zhang3de157d2014-08-05 20:54:44 -0700283 sp<GenericSource> genericSource =
284 new GenericSource(notify, mUIDValid, mUID);
Andreas Huber2bfdd422011-10-11 15:24:07 -0700285
Chong Zhanga19f33e2014-08-07 15:35:07 -0700286 status_t err = genericSource->setDataSource(httpService, url, headers);
Chong Zhang3de157d2014-08-05 20:54:44 -0700287
288 if (err == OK) {
289 source = genericSource;
290 } else {
Chong Zhanga19f33e2014-08-07 15:35:07 -0700291 ALOGE("Failed to set data source!");
Chong Zhang3de157d2014-08-05 20:54:44 -0700292 }
Hassan Shojaniaff63de72017-04-26 15:10:42 -0700293
294 // regardless of success/failure
295 mDataSourceType = DATA_SOURCE_TYPE_GENERIC_URL;
Chong Zhang3de157d2014-08-05 20:54:44 -0700296 }
Andreas Huberafed0e12011-09-20 15:39:58 -0700297 msg->setObject("source", source);
298 msg->post();
299}
300
Andreas Huber9575c962013-02-05 13:59:56 -0800301void NuPlayer::setDataSourceAsync(int fd, int64_t offset, int64_t length) {
Lajos Molnar1d15ab52015-03-04 16:46:34 -0800302 sp<AMessage> msg = new AMessage(kWhatSetDataSource, this);
Andreas Huberafed0e12011-09-20 15:39:58 -0700303
Lajos Molnar1d15ab52015-03-04 16:46:34 -0800304 sp<AMessage> notify = new AMessage(kWhatSourceNotify, this);
Andreas Huberb5f25f02013-02-05 10:14:26 -0800305
Chong Zhang3de157d2014-08-05 20:54:44 -0700306 sp<GenericSource> source =
307 new GenericSource(notify, mUIDValid, mUID);
308
Hassan Shojaniacefac142017-02-06 21:02:02 -0800309 ALOGV("setDataSourceAsync fd %d/%lld/%lld source: %p",
310 fd, (long long)offset, (long long)length, source.get());
311
Chong Zhanga19f33e2014-08-07 15:35:07 -0700312 status_t err = source->setDataSource(fd, offset, length);
Chong Zhang3de157d2014-08-05 20:54:44 -0700313
314 if (err != OK) {
Chong Zhanga19f33e2014-08-07 15:35:07 -0700315 ALOGE("Failed to set data source!");
Chong Zhang3de157d2014-08-05 20:54:44 -0700316 source = NULL;
317 }
318
Andreas Huberafed0e12011-09-20 15:39:58 -0700319 msg->setObject("source", source);
Andreas Huberf9334412010-12-15 15:17:42 -0800320 msg->post();
Hassan Shojaniaff63de72017-04-26 15:10:42 -0700321 mDataSourceType = DATA_SOURCE_TYPE_GENERIC_FD;
Andreas Huberf9334412010-12-15 15:17:42 -0800322}
323
Chris Watkins99f31602015-03-20 13:06:33 -0700324void NuPlayer::setDataSourceAsync(const sp<DataSource> &dataSource) {
325 sp<AMessage> msg = new AMessage(kWhatSetDataSource, this);
326 sp<AMessage> notify = new AMessage(kWhatSourceNotify, this);
327
328 sp<GenericSource> source = new GenericSource(notify, mUIDValid, mUID);
329 status_t err = source->setDataSource(dataSource);
330
331 if (err != OK) {
332 ALOGE("Failed to set data source!");
333 source = NULL;
334 }
335
336 msg->setObject("source", source);
337 msg->post();
Hassan Shojaniaff63de72017-04-26 15:10:42 -0700338 mDataSourceType = DATA_SOURCE_TYPE_MEDIA;
Chris Watkins99f31602015-03-20 13:06:33 -0700339}
340
Wei Jia48fa06d2016-12-20 15:30:49 -0800341status_t NuPlayer::getDefaultBufferingSettings(
342 BufferingSettings *buffering /* nonnull */) {
343 sp<AMessage> msg = new AMessage(kWhatGetDefaultBufferingSettings, this);
344 sp<AMessage> response;
345 status_t err = msg->postAndAwaitResponse(&response);
346 if (err == OK && response != NULL) {
347 CHECK(response->findInt32("err", &err));
348 if (err == OK) {
349 readFromAMessage(response, buffering);
350 }
351 }
352 return err;
353}
354
355status_t NuPlayer::setBufferingSettings(const BufferingSettings& buffering) {
356 sp<AMessage> msg = new AMessage(kWhatSetBufferingSettings, this);
357 writeToAMessage(msg, buffering);
358 sp<AMessage> response;
359 status_t err = msg->postAndAwaitResponse(&response);
360 if (err == OK && response != NULL) {
361 CHECK(response->findInt32("err", &err));
362 }
363 return err;
364}
365
Andreas Huber9575c962013-02-05 13:59:56 -0800366void NuPlayer::prepareAsync() {
Hassan Shojaniacefac142017-02-06 21:02:02 -0800367 ALOGV("prepareAsync");
368
Lajos Molnar1d15ab52015-03-04 16:46:34 -0800369 (new AMessage(kWhatPrepare, this))->post();
Andreas Huber9575c962013-02-05 13:59:56 -0800370}
371
Andreas Huber57a339c2012-12-03 11:18:00 -0800372void NuPlayer::setVideoSurfaceTextureAsync(
Andy McFadden8ba01022012-12-18 09:46:54 -0800373 const sp<IGraphicBufferProducer> &bufferProducer) {
Lajos Molnar1de1e252015-04-30 18:18:34 -0700374 sp<AMessage> msg = new AMessage(kWhatSetVideoSurface, this);
Andreas Huber57a339c2012-12-03 11:18:00 -0800375
Andy McFadden8ba01022012-12-18 09:46:54 -0800376 if (bufferProducer == NULL) {
Lajos Molnar1de1e252015-04-30 18:18:34 -0700377 msg->setObject("surface", NULL);
Andreas Huber57a339c2012-12-03 11:18:00 -0800378 } else {
Lajos Molnar1de1e252015-04-30 18:18:34 -0700379 msg->setObject("surface", new Surface(bufferProducer, true /* controlledByApp */));
Andreas Huber57a339c2012-12-03 11:18:00 -0800380 }
381
Andreas Huberf9334412010-12-15 15:17:42 -0800382 msg->post();
383}
384
385void NuPlayer::setAudioSink(const sp<MediaPlayerBase::AudioSink> &sink) {
Lajos Molnar1d15ab52015-03-04 16:46:34 -0800386 sp<AMessage> msg = new AMessage(kWhatSetAudioSink, this);
Andreas Huberf9334412010-12-15 15:17:42 -0800387 msg->setObject("sink", sink);
388 msg->post();
389}
390
391void NuPlayer::start() {
Lajos Molnar1d15ab52015-03-04 16:46:34 -0800392 (new AMessage(kWhatStart, this))->post();
Andreas Huberf9334412010-12-15 15:17:42 -0800393}
394
Lajos Molnar3a474aa2015-04-24 17:10:07 -0700395status_t NuPlayer::setPlaybackSettings(const AudioPlaybackRate &rate) {
396 // do some cursory validation of the settings here. audio modes are
397 // only validated when set on the audiosink.
398 if ((rate.mSpeed != 0.f && rate.mSpeed < AUDIO_TIMESTRETCH_SPEED_MIN)
399 || rate.mSpeed > AUDIO_TIMESTRETCH_SPEED_MAX
400 || rate.mPitch < AUDIO_TIMESTRETCH_SPEED_MIN
401 || rate.mPitch > AUDIO_TIMESTRETCH_SPEED_MAX) {
402 return BAD_VALUE;
403 }
404 sp<AMessage> msg = new AMessage(kWhatConfigPlayback, this);
405 writeToAMessage(msg, rate);
406 sp<AMessage> response;
407 status_t err = msg->postAndAwaitResponse(&response);
408 if (err == OK && response != NULL) {
409 CHECK(response->findInt32("err", &err));
410 }
411 return err;
412}
413
414status_t NuPlayer::getPlaybackSettings(AudioPlaybackRate *rate /* nonnull */) {
415 sp<AMessage> msg = new AMessage(kWhatGetPlaybackSettings, this);
416 sp<AMessage> response;
417 status_t err = msg->postAndAwaitResponse(&response);
418 if (err == OK && response != NULL) {
419 CHECK(response->findInt32("err", &err));
420 if (err == OK) {
421 readFromAMessage(response, rate);
422 }
423 }
424 return err;
425}
426
427status_t NuPlayer::setSyncSettings(const AVSyncSettings &sync, float videoFpsHint) {
428 sp<AMessage> msg = new AMessage(kWhatConfigSync, this);
429 writeToAMessage(msg, sync, videoFpsHint);
430 sp<AMessage> response;
431 status_t err = msg->postAndAwaitResponse(&response);
432 if (err == OK && response != NULL) {
433 CHECK(response->findInt32("err", &err));
434 }
435 return err;
436}
437
438status_t NuPlayer::getSyncSettings(
439 AVSyncSettings *sync /* nonnull */, float *videoFps /* nonnull */) {
440 sp<AMessage> msg = new AMessage(kWhatGetSyncSettings, this);
441 sp<AMessage> response;
442 status_t err = msg->postAndAwaitResponse(&response);
443 if (err == OK && response != NULL) {
444 CHECK(response->findInt32("err", &err));
445 if (err == OK) {
446 readFromAMessage(response, sync, videoFps);
447 }
448 }
449 return err;
Wei Jia98160162015-02-04 17:01:11 -0800450}
451
Andreas Huber43c3e6c2011-01-05 12:17:08 -0800452void NuPlayer::pause() {
Lajos Molnar1d15ab52015-03-04 16:46:34 -0800453 (new AMessage(kWhatPause, this))->post();
Andreas Huber43c3e6c2011-01-05 12:17:08 -0800454}
455
Andreas Huber1aef2112011-01-04 14:01:29 -0800456void NuPlayer::resetAsync() {
Wei Jiac45a4b22016-04-15 15:30:23 -0700457 sp<Source> source;
458 {
459 Mutex::Autolock autoLock(mSourceLock);
460 source = mSource;
461 }
462
463 if (source != NULL) {
Chong Zhang48296b72014-09-14 14:28:45 -0700464 // During a reset, the data source might be unresponsive already, we need to
465 // disconnect explicitly so that reads exit promptly.
466 // We can't queue the disconnect request to the looper, as it might be
467 // queued behind a stuck read and never gets processed.
468 // Doing a disconnect outside the looper to allows the pending reads to exit
469 // (either successfully or with error).
Wei Jiac45a4b22016-04-15 15:30:23 -0700470 source->disconnect();
Chong Zhang48296b72014-09-14 14:28:45 -0700471 }
472
Lajos Molnar1d15ab52015-03-04 16:46:34 -0800473 (new AMessage(kWhatReset, this))->post();
Andreas Huber1aef2112011-01-04 14:01:29 -0800474}
475
Wei Jiac5de0912016-11-18 10:22:14 -0800476void NuPlayer::seekToAsync(int64_t seekTimeUs, MediaPlayerSeekMode mode, bool needNotify) {
Lajos Molnar1d15ab52015-03-04 16:46:34 -0800477 sp<AMessage> msg = new AMessage(kWhatSeek, this);
Andreas Huber43c3e6c2011-01-05 12:17:08 -0800478 msg->setInt64("seekTimeUs", seekTimeUs);
Wei Jiac5de0912016-11-18 10:22:14 -0800479 msg->setInt32("mode", mode);
Wei Jiae427abf2014-09-22 15:21:11 -0700480 msg->setInt32("needNotify", needNotify);
Andreas Huber43c3e6c2011-01-05 12:17:08 -0800481 msg->post();
482}
483
Andreas Huber53df1a42010-12-22 10:03:04 -0800484
Chong Zhang404fced2014-06-11 14:45:31 -0700485void NuPlayer::writeTrackInfo(
Chih-Hung Hsiehe964d4e2016-08-09 14:31:32 -0700486 Parcel* reply, const sp<AMessage>& format) const {
Marco Nelissenc367ca12015-09-15 09:51:59 -0700487 if (format == NULL) {
488 ALOGE("NULL format");
489 return;
490 }
Chong Zhang404fced2014-06-11 14:45:31 -0700491 int32_t trackType;
Marco Nelissen9436e482015-09-15 10:21:53 -0700492 if (!format->findInt32("type", &trackType)) {
Marco Nelissenc367ca12015-09-15 09:51:59 -0700493 ALOGE("no track type");
494 return;
495 }
Chong Zhang404fced2014-06-11 14:45:31 -0700496
Robert Shih755106e2015-04-30 14:36:45 -0700497 AString mime;
Robert Shih2e3a4252015-05-06 10:21:15 -0700498 if (!format->findString("mime", &mime)) {
499 // Java MediaPlayer only uses mimetype for subtitle and timedtext tracks.
500 // If we can't find the mimetype here it means that we wouldn't be needing
501 // the mimetype on the Java end. We still write a placeholder mime to keep the
502 // (de)serialization logic simple.
503 if (trackType == MEDIA_TRACK_TYPE_AUDIO) {
504 mime = "audio/";
505 } else if (trackType == MEDIA_TRACK_TYPE_VIDEO) {
506 mime = "video/";
507 } else {
Marco Nelissenc367ca12015-09-15 09:51:59 -0700508 ALOGE("unknown track type: %d", trackType);
509 return;
Robert Shih2e3a4252015-05-06 10:21:15 -0700510 }
511 }
Robert Shih755106e2015-04-30 14:36:45 -0700512
Chong Zhang404fced2014-06-11 14:45:31 -0700513 AString lang;
Marco Nelissen9436e482015-09-15 10:21:53 -0700514 if (!format->findString("language", &lang)) {
Marco Nelissenc367ca12015-09-15 09:51:59 -0700515 ALOGE("no language");
516 return;
517 }
Chong Zhang404fced2014-06-11 14:45:31 -0700518
519 reply->writeInt32(2); // write something non-zero
520 reply->writeInt32(trackType);
Robert Shih755106e2015-04-30 14:36:45 -0700521 reply->writeString16(String16(mime.c_str()));
Chong Zhang404fced2014-06-11 14:45:31 -0700522 reply->writeString16(String16(lang.c_str()));
523
524 if (trackType == MEDIA_TRACK_TYPE_SUBTITLE) {
Chong Zhang404fced2014-06-11 14:45:31 -0700525 int32_t isAuto, isDefault, isForced;
526 CHECK(format->findInt32("auto", &isAuto));
527 CHECK(format->findInt32("default", &isDefault));
528 CHECK(format->findInt32("forced", &isForced));
529
Chong Zhang404fced2014-06-11 14:45:31 -0700530 reply->writeInt32(isAuto);
531 reply->writeInt32(isDefault);
532 reply->writeInt32(isForced);
533 }
534}
535
Andreas Huberf9334412010-12-15 15:17:42 -0800536void NuPlayer::onMessageReceived(const sp<AMessage> &msg) {
537 switch (msg->what()) {
538 case kWhatSetDataSource:
539 {
Steve Block3856b092011-10-20 11:56:00 +0100540 ALOGV("kWhatSetDataSource");
Andreas Huberf9334412010-12-15 15:17:42 -0800541
542 CHECK(mSource == NULL);
543
Chong Zhang3de157d2014-08-05 20:54:44 -0700544 status_t err = OK;
Andreas Huber5bc087c2010-12-23 10:27:40 -0800545 sp<RefBase> obj;
546 CHECK(msg->findObject("source", &obj));
Chong Zhang3de157d2014-08-05 20:54:44 -0700547 if (obj != NULL) {
Wei Jiac45a4b22016-04-15 15:30:23 -0700548 Mutex::Autolock autoLock(mSourceLock);
Chong Zhang3de157d2014-08-05 20:54:44 -0700549 mSource = static_cast<Source *>(obj.get());
Chong Zhang3de157d2014-08-05 20:54:44 -0700550 } else {
551 err = UNKNOWN_ERROR;
552 }
Andreas Huber9575c962013-02-05 13:59:56 -0800553
554 CHECK(mDriver != NULL);
555 sp<NuPlayerDriver> driver = mDriver.promote();
556 if (driver != NULL) {
Chong Zhang3de157d2014-08-05 20:54:44 -0700557 driver->notifySetDataSourceCompleted(err);
Andreas Huber9575c962013-02-05 13:59:56 -0800558 }
559 break;
560 }
561
Wei Jia48fa06d2016-12-20 15:30:49 -0800562 case kWhatGetDefaultBufferingSettings:
563 {
564 sp<AReplyToken> replyID;
565 CHECK(msg->senderAwaitsResponse(&replyID));
566
567 ALOGV("kWhatGetDefaultBufferingSettings");
568 BufferingSettings buffering;
569 status_t err = OK;
570 if (mSource != NULL) {
571 err = mSource->getDefaultBufferingSettings(&buffering);
572 } else {
573 err = INVALID_OPERATION;
574 }
575 sp<AMessage> response = new AMessage;
576 if (err == OK) {
577 writeToAMessage(response, buffering);
578 }
579 response->setInt32("err", err);
580 response->postReply(replyID);
581 break;
582 }
583
584 case kWhatSetBufferingSettings:
585 {
586 sp<AReplyToken> replyID;
587 CHECK(msg->senderAwaitsResponse(&replyID));
588
589 ALOGV("kWhatSetBufferingSettings");
590 BufferingSettings buffering;
591 readFromAMessage(msg, &buffering);
592 status_t err = OK;
593 if (mSource != NULL) {
594 err = mSource->setBufferingSettings(buffering);
595 } else {
596 err = INVALID_OPERATION;
597 }
598 sp<AMessage> response = new AMessage;
599 response->setInt32("err", err);
600 response->postReply(replyID);
601 break;
602 }
603
Andreas Huber9575c962013-02-05 13:59:56 -0800604 case kWhatPrepare:
605 {
Hassan Shojaniacefac142017-02-06 21:02:02 -0800606 ALOGV("onMessageReceived kWhatPrepare");
607
Andreas Huber9575c962013-02-05 13:59:56 -0800608 mSource->prepareAsync();
Andreas Huberf9334412010-12-15 15:17:42 -0800609 break;
610 }
611
Chong Zhangdcb89b32013-08-06 09:44:47 -0700612 case kWhatGetTrackInfo:
613 {
Lajos Molnar3f274362015-03-05 14:35:41 -0800614 sp<AReplyToken> replyID;
Chong Zhangdcb89b32013-08-06 09:44:47 -0700615 CHECK(msg->senderAwaitsResponse(&replyID));
616
Chong Zhang404fced2014-06-11 14:45:31 -0700617 Parcel* reply;
618 CHECK(msg->findPointer("reply", (void**)&reply));
619
620 size_t inbandTracks = 0;
Chong Zhangdcb89b32013-08-06 09:44:47 -0700621 if (mSource != NULL) {
Chong Zhang404fced2014-06-11 14:45:31 -0700622 inbandTracks = mSource->getTrackCount();
623 }
624
Chong Zhanga7fa1d92014-06-11 14:49:23 -0700625 size_t ccTracks = 0;
626 if (mCCDecoder != NULL) {
627 ccTracks = mCCDecoder->getTrackCount();
628 }
629
Chong Zhang404fced2014-06-11 14:45:31 -0700630 // total track count
Chong Zhanga7fa1d92014-06-11 14:49:23 -0700631 reply->writeInt32(inbandTracks + ccTracks);
Chong Zhang404fced2014-06-11 14:45:31 -0700632
633 // write inband tracks
634 for (size_t i = 0; i < inbandTracks; ++i) {
635 writeTrackInfo(reply, mSource->getTrackInfo(i));
Chong Zhangdcb89b32013-08-06 09:44:47 -0700636 }
637
Chong Zhanga7fa1d92014-06-11 14:49:23 -0700638 // write CC track
639 for (size_t i = 0; i < ccTracks; ++i) {
640 writeTrackInfo(reply, mCCDecoder->getTrackInfo(i));
641 }
642
Chong Zhangdcb89b32013-08-06 09:44:47 -0700643 sp<AMessage> response = new AMessage;
Chong Zhangdcb89b32013-08-06 09:44:47 -0700644 response->postReply(replyID);
645 break;
646 }
647
Robert Shih7c4f0d72014-07-09 18:53:31 -0700648 case kWhatGetSelectedTrack:
649 {
650 status_t err = INVALID_OPERATION;
651 if (mSource != NULL) {
652 err = OK;
653
654 int32_t type32;
655 CHECK(msg->findInt32("type", (int32_t*)&type32));
656 media_track_type type = (media_track_type)type32;
657 ssize_t selectedTrack = mSource->getSelectedTrack(type);
658
659 Parcel* reply;
660 CHECK(msg->findPointer("reply", (void**)&reply));
661 reply->writeInt32(selectedTrack);
662 }
663
664 sp<AMessage> response = new AMessage;
665 response->setInt32("err", err);
666
Lajos Molnar3f274362015-03-05 14:35:41 -0800667 sp<AReplyToken> replyID;
Robert Shih7c4f0d72014-07-09 18:53:31 -0700668 CHECK(msg->senderAwaitsResponse(&replyID));
669 response->postReply(replyID);
670 break;
671 }
672
Chong Zhangdcb89b32013-08-06 09:44:47 -0700673 case kWhatSelectTrack:
674 {
Lajos Molnar3f274362015-03-05 14:35:41 -0800675 sp<AReplyToken> replyID;
Chong Zhangdcb89b32013-08-06 09:44:47 -0700676 CHECK(msg->senderAwaitsResponse(&replyID));
677
Chong Zhang404fced2014-06-11 14:45:31 -0700678 size_t trackIndex;
679 int32_t select;
Robert Shih6ffb1fd2014-10-29 16:24:32 -0700680 int64_t timeUs;
Chong Zhang404fced2014-06-11 14:45:31 -0700681 CHECK(msg->findSize("trackIndex", &trackIndex));
682 CHECK(msg->findInt32("select", &select));
Robert Shih6ffb1fd2014-10-29 16:24:32 -0700683 CHECK(msg->findInt64("timeUs", &timeUs));
Chong Zhang404fced2014-06-11 14:45:31 -0700684
Chong Zhangdcb89b32013-08-06 09:44:47 -0700685 status_t err = INVALID_OPERATION;
Chong Zhang404fced2014-06-11 14:45:31 -0700686
687 size_t inbandTracks = 0;
Chong Zhangdcb89b32013-08-06 09:44:47 -0700688 if (mSource != NULL) {
Chong Zhang404fced2014-06-11 14:45:31 -0700689 inbandTracks = mSource->getTrackCount();
690 }
Chong Zhanga7fa1d92014-06-11 14:49:23 -0700691 size_t ccTracks = 0;
692 if (mCCDecoder != NULL) {
693 ccTracks = mCCDecoder->getTrackCount();
694 }
Chong Zhang404fced2014-06-11 14:45:31 -0700695
696 if (trackIndex < inbandTracks) {
Robert Shih6ffb1fd2014-10-29 16:24:32 -0700697 err = mSource->selectTrack(trackIndex, select, timeUs);
Robert Shihd3b0bbb2014-07-23 15:00:25 -0700698
699 if (!select && err == OK) {
700 int32_t type;
701 sp<AMessage> info = mSource->getTrackInfo(trackIndex);
702 if (info != NULL
703 && info->findInt32("type", &type)
704 && type == MEDIA_TRACK_TYPE_TIMEDTEXT) {
705 ++mTimedTextGeneration;
706 }
707 }
Chong Zhanga7fa1d92014-06-11 14:49:23 -0700708 } else {
709 trackIndex -= inbandTracks;
710
711 if (trackIndex < ccTracks) {
712 err = mCCDecoder->selectTrack(trackIndex, select);
713 }
Chong Zhangdcb89b32013-08-06 09:44:47 -0700714 }
715
716 sp<AMessage> response = new AMessage;
717 response->setInt32("err", err);
718
719 response->postReply(replyID);
720 break;
721 }
722
Andreas Huberb7c8e912012-11-27 15:02:53 -0800723 case kWhatPollDuration:
724 {
725 int32_t generation;
726 CHECK(msg->findInt32("generation", &generation));
727
728 if (generation != mPollDurationGeneration) {
729 // stale
730 break;
731 }
732
733 int64_t durationUs;
734 if (mDriver != NULL && mSource->getDuration(&durationUs) == OK) {
735 sp<NuPlayerDriver> driver = mDriver.promote();
736 if (driver != NULL) {
737 driver->notifyDuration(durationUs);
738 }
739 }
740
741 msg->post(1000000ll); // poll again in a second.
742 break;
743 }
744
Lajos Molnar1de1e252015-04-30 18:18:34 -0700745 case kWhatSetVideoSurface:
Andreas Huberf9334412010-12-15 15:17:42 -0800746 {
Andreas Huberf9334412010-12-15 15:17:42 -0800747
748 sp<RefBase> obj;
Lajos Molnar1de1e252015-04-30 18:18:34 -0700749 CHECK(msg->findObject("surface", &obj));
750 sp<Surface> surface = static_cast<Surface *>(obj.get());
Lajos Molnara81c6222015-07-10 19:17:45 -0700751
752 ALOGD("onSetVideoSurface(%p, %s video decoder)",
753 surface.get(),
Wei Jia6b7d2ed2015-08-10 15:11:54 -0700754 (mSource != NULL && mStarted && mSource->getFormat(false /* audio */) != NULL
Lajos Molnara81c6222015-07-10 19:17:45 -0700755 && mVideoDecoder != NULL) ? "have" : "no");
756
Wei Jia6b7d2ed2015-08-10 15:11:54 -0700757 // Need to check mStarted before calling mSource->getFormat because NuPlayer might
758 // be in preparing state and it could take long time.
759 // When mStarted is true, mSource must have been set.
760 if (mSource == NULL || !mStarted || mSource->getFormat(false /* audio */) == NULL
Lajos Molnara81c6222015-07-10 19:17:45 -0700761 // NOTE: mVideoDecoder's mSurface is always non-null
762 || (mVideoDecoder != NULL && mVideoDecoder->setVideoSurface(surface) == OK)) {
Lajos Molnar1de1e252015-04-30 18:18:34 -0700763 performSetSurface(surface);
Wei Jiafef808d2014-10-31 17:57:05 -0700764 break;
765 }
766
767 mDeferredActions.push_back(
Krystian Turczyn687a5892016-01-20 14:20:35 +0100768 new FlushDecoderAction(
769 (obj != NULL ? FLUSH_CMD_FLUSH : FLUSH_CMD_NONE) /* audio */,
Wei Jiafef808d2014-10-31 17:57:05 -0700770 FLUSH_CMD_SHUTDOWN /* video */));
771
Lajos Molnar1de1e252015-04-30 18:18:34 -0700772 mDeferredActions.push_back(new SetSurfaceAction(surface));
James Dong0d268a32012-08-31 12:18:27 -0700773
Krystian Turczyn687a5892016-01-20 14:20:35 +0100774 if (obj != NULL) {
Wei Jiafef808d2014-10-31 17:57:05 -0700775 if (mStarted) {
Andy Hung73535852014-09-05 11:42:58 -0700776 // Issue a seek to refresh the video screen only if started otherwise
777 // the extractor may not yet be started and will assert.
778 // If the video decoder is not set (perhaps audio only in this case)
779 // do not perform a seek as it is not needed.
Ronghua Wua73d9e02014-10-08 15:13:29 -0700780 int64_t currentPositionUs = 0;
781 if (getCurrentPosition(&currentPositionUs) == OK) {
782 mDeferredActions.push_back(
Wei Jiac5de0912016-11-18 10:22:14 -0800783 new SeekAction(currentPositionUs,
784 MediaPlayerSeekMode::SEEK_PREVIOUS_SYNC /* mode */));
Ronghua Wua73d9e02014-10-08 15:13:29 -0700785 }
Andy Hung73535852014-09-05 11:42:58 -0700786 }
Wei Jiaac428aa2014-09-02 19:01:34 -0700787
Andreas Huber57a339c2012-12-03 11:18:00 -0800788 // If there is a new surface texture, instantiate decoders
789 // again if possible.
790 mDeferredActions.push_back(
791 new SimpleAction(&NuPlayer::performScanSources));
Andreas Huber57a339c2012-12-03 11:18:00 -0800792
Krystian Turczyn687a5892016-01-20 14:20:35 +0100793 // After a flush without shutdown, decoder is paused.
794 // Don't resume it until source seek is done, otherwise it could
795 // start pulling stale data too soon.
796 mDeferredActions.push_back(
797 new ResumeDecoderAction(false /* needNotify */));
798 }
Chong Zhang7137ec72014-11-12 16:41:05 -0800799
Andreas Huber57a339c2012-12-03 11:18:00 -0800800 processDeferredActions();
Andreas Huberf9334412010-12-15 15:17:42 -0800801 break;
802 }
803
804 case kWhatSetAudioSink:
805 {
Steve Block3856b092011-10-20 11:56:00 +0100806 ALOGV("kWhatSetAudioSink");
Andreas Huberf9334412010-12-15 15:17:42 -0800807
808 sp<RefBase> obj;
809 CHECK(msg->findObject("sink", &obj));
810
811 mAudioSink = static_cast<MediaPlayerBase::AudioSink *>(obj.get());
812 break;
813 }
814
815 case kWhatStart:
816 {
Steve Block3856b092011-10-20 11:56:00 +0100817 ALOGV("kWhatStart");
Wei Jia94211742014-10-28 17:09:06 -0700818 if (mStarted) {
Chong Zhang8a048332015-05-06 15:16:28 -0700819 // do not resume yet if the source is still buffering
820 if (!mPausedForBuffering) {
821 onResume();
822 }
Wei Jia94211742014-10-28 17:09:06 -0700823 } else {
824 onStart();
Lajos Molnar09524832014-07-17 14:29:51 -0700825 }
Chong Zhangefbb6192015-01-30 17:13:27 -0800826 mPausedByClient = false;
Andreas Huberf9334412010-12-15 15:17:42 -0800827 break;
828 }
829
Lajos Molnar3a474aa2015-04-24 17:10:07 -0700830 case kWhatConfigPlayback:
Wei Jia98160162015-02-04 17:01:11 -0800831 {
Lajos Molnar3a474aa2015-04-24 17:10:07 -0700832 sp<AReplyToken> replyID;
833 CHECK(msg->senderAwaitsResponse(&replyID));
834 AudioPlaybackRate rate /* sanitized */;
835 readFromAMessage(msg, &rate);
836 status_t err = OK;
Wei Jia98160162015-02-04 17:01:11 -0800837 if (mRenderer != NULL) {
Wei Jiabf70feb2016-02-19 15:47:16 -0800838 // AudioSink allows only 1.f and 0.f for offload mode.
839 // For other speed, switch to non-offload mode.
840 if (mOffloadAudio && ((rate.mSpeed != 0.f && rate.mSpeed != 1.f)
841 || rate.mPitch != 1.f)) {
842 int64_t currentPositionUs;
843 if (getCurrentPosition(&currentPositionUs) != OK) {
844 currentPositionUs = mPreviousSeekTimeUs;
845 }
846
847 // Set mPlaybackSettings so that the new audio decoder can
848 // be created correctly.
849 mPlaybackSettings = rate;
850 if (!mPaused) {
851 mRenderer->pause();
852 }
Wei Jia5031b2f2016-02-25 11:19:31 -0800853 restartAudio(
Wei Jiabf70feb2016-02-19 15:47:16 -0800854 currentPositionUs, true /* forceNonOffload */,
855 true /* needsToCreateAudioDecoder */);
856 if (!mPaused) {
857 mRenderer->resume();
858 }
859 }
860
Lajos Molnar3a474aa2015-04-24 17:10:07 -0700861 err = mRenderer->setPlaybackSettings(rate);
862 }
863 if (err == OK) {
864 if (rate.mSpeed == 0.f) {
865 onPause();
Wei Jia351ce872016-02-10 13:21:59 -0800866 mPausedByClient = true;
Lajos Molnar3a474aa2015-04-24 17:10:07 -0700867 // save all other settings (using non-paused speed)
868 // so we can restore them on start
869 AudioPlaybackRate newRate = rate;
870 newRate.mSpeed = mPlaybackSettings.mSpeed;
871 mPlaybackSettings = newRate;
872 } else { /* rate.mSpeed != 0.f */
Lajos Molnar3a474aa2015-04-24 17:10:07 -0700873 mPlaybackSettings = rate;
Wei Jia8a092d32016-06-03 14:57:24 -0700874 if (mStarted) {
875 // do not resume yet if the source is still buffering
876 if (!mPausedForBuffering) {
877 onResume();
878 }
879 } else if (mPrepared) {
880 onStart();
881 }
882
883 mPausedByClient = false;
Lajos Molnar3a474aa2015-04-24 17:10:07 -0700884 }
Wei Jia98160162015-02-04 17:01:11 -0800885 }
Ronghua Wu8db88132015-04-22 13:51:35 -0700886
887 if (mVideoDecoder != NULL) {
Praveen Chavanbbaa1442016-04-08 13:33:49 -0700888 sp<AMessage> params = new AMessage();
889 params->setFloat("playback-speed", mPlaybackSettings.mSpeed);
890 mVideoDecoder->setParameters(params);
Ronghua Wu8db88132015-04-22 13:51:35 -0700891 }
892
Lajos Molnar3a474aa2015-04-24 17:10:07 -0700893 sp<AMessage> response = new AMessage;
894 response->setInt32("err", err);
895 response->postReply(replyID);
896 break;
897 }
898
899 case kWhatGetPlaybackSettings:
900 {
901 sp<AReplyToken> replyID;
902 CHECK(msg->senderAwaitsResponse(&replyID));
903 AudioPlaybackRate rate = mPlaybackSettings;
904 status_t err = OK;
905 if (mRenderer != NULL) {
906 err = mRenderer->getPlaybackSettings(&rate);
907 }
908 if (err == OK) {
909 // get playback settings used by renderer, as it may be
910 // slightly off due to audiosink not taking small changes.
911 mPlaybackSettings = rate;
912 if (mPaused) {
913 rate.mSpeed = 0.f;
914 }
915 }
916 sp<AMessage> response = new AMessage;
917 if (err == OK) {
918 writeToAMessage(response, rate);
919 }
920 response->setInt32("err", err);
921 response->postReply(replyID);
922 break;
923 }
924
925 case kWhatConfigSync:
926 {
927 sp<AReplyToken> replyID;
928 CHECK(msg->senderAwaitsResponse(&replyID));
929
930 ALOGV("kWhatConfigSync");
931 AVSyncSettings sync;
932 float videoFpsHint;
933 readFromAMessage(msg, &sync, &videoFpsHint);
934 status_t err = OK;
935 if (mRenderer != NULL) {
936 err = mRenderer->setSyncSettings(sync, videoFpsHint);
937 }
938 if (err == OK) {
939 mSyncSettings = sync;
940 mVideoFpsHint = videoFpsHint;
941 }
942 sp<AMessage> response = new AMessage;
943 response->setInt32("err", err);
944 response->postReply(replyID);
945 break;
946 }
947
948 case kWhatGetSyncSettings:
949 {
950 sp<AReplyToken> replyID;
951 CHECK(msg->senderAwaitsResponse(&replyID));
952 AVSyncSettings sync = mSyncSettings;
953 float videoFps = mVideoFpsHint;
954 status_t err = OK;
955 if (mRenderer != NULL) {
956 err = mRenderer->getSyncSettings(&sync, &videoFps);
957 if (err == OK) {
958 mSyncSettings = sync;
959 mVideoFpsHint = videoFps;
960 }
961 }
962 sp<AMessage> response = new AMessage;
963 if (err == OK) {
964 writeToAMessage(response, sync, videoFps);
965 }
966 response->setInt32("err", err);
967 response->postReply(replyID);
Wei Jia98160162015-02-04 17:01:11 -0800968 break;
969 }
970
Andreas Huberf9334412010-12-15 15:17:42 -0800971 case kWhatScanSources:
972 {
Andreas Huber1aef2112011-01-04 14:01:29 -0800973 int32_t generation;
974 CHECK(msg->findInt32("generation", &generation));
975 if (generation != mScanSourcesGeneration) {
976 // Drop obsolete msg.
977 break;
978 }
979
Andreas Huber5bc087c2010-12-23 10:27:40 -0800980 mScanSourcesPending = false;
981
Steve Block3856b092011-10-20 11:56:00 +0100982 ALOGV("scanning sources haveAudio=%d, haveVideo=%d",
Andreas Huber43c3e6c2011-01-05 12:17:08 -0800983 mAudioDecoder != NULL, mVideoDecoder != NULL);
984
Andreas Huberb7c8e912012-11-27 15:02:53 -0800985 bool mHadAnySourcesBefore =
986 (mAudioDecoder != NULL) || (mVideoDecoder != NULL);
Robert Shih7350b052015-10-01 15:50:14 -0700987 bool rescan = false;
Andreas Huberb7c8e912012-11-27 15:02:53 -0800988
Andy Hung282a7e32014-08-14 15:56:34 -0700989 // initialize video before audio because successful initialization of
990 // video may change deep buffer mode of audio.
Lajos Molnar1de1e252015-04-30 18:18:34 -0700991 if (mSurface != NULL) {
Robert Shih7350b052015-10-01 15:50:14 -0700992 if (instantiateDecoder(false, &mVideoDecoder) == -EWOULDBLOCK) {
993 rescan = true;
994 }
Haynes Mathew George5d246ef2012-07-09 10:36:57 -0700995 }
Andreas Huberf9334412010-12-15 15:17:42 -0800996
Ronghua Wua10fd232014-11-06 16:15:20 -0800997 // Don't try to re-open audio sink if there's an existing decoder.
998 if (mAudioSink != NULL && mAudioDecoder == NULL) {
Robert Shih7350b052015-10-01 15:50:14 -0700999 if (instantiateDecoder(true, &mAudioDecoder) == -EWOULDBLOCK) {
1000 rescan = true;
1001 }
Andreas Huberf9334412010-12-15 15:17:42 -08001002 }
1003
Andreas Huberb7c8e912012-11-27 15:02:53 -08001004 if (!mHadAnySourcesBefore
1005 && (mAudioDecoder != NULL || mVideoDecoder != NULL)) {
1006 // This is the first time we've found anything playable.
1007
Andreas Huber9575c962013-02-05 13:59:56 -08001008 if (mSourceFlags & Source::FLAG_DYNAMIC_DURATION) {
Andreas Huberb7c8e912012-11-27 15:02:53 -08001009 schedulePollDuration();
1010 }
1011 }
1012
Andreas Hubereac68ba2011-09-27 12:12:25 -07001013 status_t err;
1014 if ((err = mSource->feedMoreTSData()) != OK) {
Andreas Huber1aef2112011-01-04 14:01:29 -08001015 if (mAudioDecoder == NULL && mVideoDecoder == NULL) {
1016 // We're not currently decoding anything (no audio or
1017 // video tracks found) and we just ran out of input data.
Andreas Hubereac68ba2011-09-27 12:12:25 -07001018
1019 if (err == ERROR_END_OF_STREAM) {
1020 notifyListener(MEDIA_PLAYBACK_COMPLETE, 0, 0);
1021 } else {
1022 notifyListener(MEDIA_ERROR, MEDIA_ERROR_UNKNOWN, err);
1023 }
Andreas Huber1aef2112011-01-04 14:01:29 -08001024 }
Andreas Huberf9334412010-12-15 15:17:42 -08001025 break;
1026 }
1027
Robert Shih7350b052015-10-01 15:50:14 -07001028 if (rescan) {
Andreas Huberf9334412010-12-15 15:17:42 -08001029 msg->post(100000ll);
Andreas Huber5bc087c2010-12-23 10:27:40 -08001030 mScanSourcesPending = true;
Andreas Huberf9334412010-12-15 15:17:42 -08001031 }
1032 break;
1033 }
1034
1035 case kWhatVideoNotify:
1036 case kWhatAudioNotify:
1037 {
1038 bool audio = msg->what() == kWhatAudioNotify;
1039
Wei Jia88703c32014-08-06 11:24:07 -07001040 int32_t currentDecoderGeneration =
1041 (audio? mAudioDecoderGeneration : mVideoDecoderGeneration);
1042 int32_t requesterGeneration = currentDecoderGeneration - 1;
1043 CHECK(msg->findInt32("generation", &requesterGeneration));
1044
1045 if (requesterGeneration != currentDecoderGeneration) {
1046 ALOGV("got message from old %s decoder, generation(%d:%d)",
1047 audio ? "audio" : "video", requesterGeneration,
1048 currentDecoderGeneration);
1049 sp<AMessage> reply;
1050 if (!(msg->findMessage("reply", &reply))) {
1051 return;
1052 }
1053
1054 reply->setInt32("err", INFO_DISCONTINUITY);
1055 reply->post();
1056 return;
1057 }
1058
Andreas Huberf9334412010-12-15 15:17:42 -08001059 int32_t what;
Lajos Molnar1cd13982014-01-17 15:12:51 -08001060 CHECK(msg->findInt32("what", &what));
Andreas Huberf9334412010-12-15 15:17:42 -08001061
Chong Zhang7137ec72014-11-12 16:41:05 -08001062 if (what == DecoderBase::kWhatInputDiscontinuity) {
1063 int32_t formatChange;
1064 CHECK(msg->findInt32("formatChange", &formatChange));
Andreas Huberf9334412010-12-15 15:17:42 -08001065
Chong Zhang7137ec72014-11-12 16:41:05 -08001066 ALOGV("%s discontinuity: formatChange %d",
1067 audio ? "audio" : "video", formatChange);
1068
1069 if (formatChange) {
1070 mDeferredActions.push_back(
1071 new FlushDecoderAction(
1072 audio ? FLUSH_CMD_SHUTDOWN : FLUSH_CMD_NONE,
1073 audio ? FLUSH_CMD_NONE : FLUSH_CMD_SHUTDOWN));
Andreas Huberf9334412010-12-15 15:17:42 -08001074 }
Chong Zhang7137ec72014-11-12 16:41:05 -08001075
1076 mDeferredActions.push_back(
1077 new SimpleAction(
1078 &NuPlayer::performScanSources));
1079
1080 processDeferredActions();
1081 } else if (what == DecoderBase::kWhatEOS) {
Andreas Huberdc9bacd2011-09-26 10:53:29 -07001082 int32_t err;
Lajos Molnar1cd13982014-01-17 15:12:51 -08001083 CHECK(msg->findInt32("err", &err));
Andreas Huberdc9bacd2011-09-26 10:53:29 -07001084
1085 if (err == ERROR_END_OF_STREAM) {
Steve Block3856b092011-10-20 11:56:00 +01001086 ALOGV("got %s decoder EOS", audio ? "audio" : "video");
Andreas Huberdc9bacd2011-09-26 10:53:29 -07001087 } else {
Steve Block3856b092011-10-20 11:56:00 +01001088 ALOGV("got %s decoder EOS w/ error %d",
Andreas Huberdc9bacd2011-09-26 10:53:29 -07001089 audio ? "audio" : "video",
1090 err);
1091 }
1092
1093 mRenderer->queueEOS(audio, err);
Chong Zhang7137ec72014-11-12 16:41:05 -08001094 } else if (what == DecoderBase::kWhatFlushCompleted) {
Steve Block3856b092011-10-20 11:56:00 +01001095 ALOGV("decoder %s flush completed", audio ? "audio" : "video");
Andreas Huberf9334412010-12-15 15:17:42 -08001096
Andy Hung8d121d42014-10-03 09:53:53 -07001097 handleFlushComplete(audio, true /* isDecoder */);
Andreas Huber3831a062010-12-21 10:22:33 -08001098 finishFlushIfPossible();
Chong Zhang7137ec72014-11-12 16:41:05 -08001099 } else if (what == DecoderBase::kWhatVideoSizeChanged) {
Lajos Molnar1cd13982014-01-17 15:12:51 -08001100 sp<AMessage> format;
1101 CHECK(msg->findMessage("format", &format));
1102
Wei Jiac6cfd702014-11-11 16:33:20 -08001103 sp<AMessage> inputFormat =
1104 mSource->getFormat(false /* audio */);
Andreas Huber3831a062010-12-21 10:22:33 -08001105
Bartosz Dolewski26936f72014-12-11 12:47:08 +01001106 setVideoScalingMode(mVideoScalingMode);
Wei Jiac6cfd702014-11-11 16:33:20 -08001107 updateVideoSize(inputFormat, format);
Chong Zhang7137ec72014-11-12 16:41:05 -08001108 } else if (what == DecoderBase::kWhatShutdownCompleted) {
Steve Block3856b092011-10-20 11:56:00 +01001109 ALOGV("%s shutdown completed", audio ? "audio" : "video");
Andreas Huber3831a062010-12-21 10:22:33 -08001110 if (audio) {
1111 mAudioDecoder.clear();
Wei Jia686e8e52017-04-03 14:08:01 -07001112 mAudioDecoderError = false;
Wei Jia57568df2014-09-22 10:16:29 -07001113 ++mAudioDecoderGeneration;
Andreas Huber3831a062010-12-21 10:22:33 -08001114
1115 CHECK_EQ((int)mFlushingAudio, (int)SHUTTING_DOWN_DECODER);
1116 mFlushingAudio = SHUT_DOWN;
1117 } else {
1118 mVideoDecoder.clear();
Wei Jia686e8e52017-04-03 14:08:01 -07001119 mVideoDecoderError = false;
Wei Jia57568df2014-09-22 10:16:29 -07001120 ++mVideoDecoderGeneration;
Andreas Huber3831a062010-12-21 10:22:33 -08001121
1122 CHECK_EQ((int)mFlushingVideo, (int)SHUTTING_DOWN_DECODER);
1123 mFlushingVideo = SHUT_DOWN;
1124 }
1125
1126 finishFlushIfPossible();
Chong Zhangf8d71772014-11-26 15:08:34 -08001127 } else if (what == DecoderBase::kWhatResumeCompleted) {
1128 finishResume();
Chong Zhang7137ec72014-11-12 16:41:05 -08001129 } else if (what == DecoderBase::kWhatError) {
Chong Zhangf4c0a942014-08-11 15:14:10 -07001130 status_t err;
Andy Hung2abde2c2014-09-30 14:40:32 -07001131 if (!msg->findInt32("err", &err) || err == OK) {
Chong Zhangf4c0a942014-08-11 15:14:10 -07001132 err = UNKNOWN_ERROR;
1133 }
Andy Hungcf31f1e2014-09-23 14:59:01 -07001134
Andy Hung2abde2c2014-09-30 14:40:32 -07001135 // Decoder errors can be due to Source (e.g. from streaming),
1136 // or from decoding corrupted bitstreams, or from other decoder
1137 // MediaCodec operations (e.g. from an ongoing reset or seek).
Andy Hung202bce12014-12-03 11:47:36 -08001138 // They may also be due to openAudioSink failure at
1139 // decoder start or after a format change.
Andy Hung2abde2c2014-09-30 14:40:32 -07001140 //
1141 // We try to gracefully shut down the affected decoder if possible,
1142 // rather than trying to force the shutdown with something
1143 // similar to performReset(). This method can lead to a hang
1144 // if MediaCodec functions block after an error, but they should
1145 // typically return INVALID_OPERATION instead of blocking.
1146
1147 FlushStatus *flushing = audio ? &mFlushingAudio : &mFlushingVideo;
1148 ALOGE("received error(%#x) from %s decoder, flushing(%d), now shutting down",
1149 err, audio ? "audio" : "video", *flushing);
1150
1151 switch (*flushing) {
1152 case NONE:
1153 mDeferredActions.push_back(
Wei Jiafef808d2014-10-31 17:57:05 -07001154 new FlushDecoderAction(
1155 audio ? FLUSH_CMD_SHUTDOWN : FLUSH_CMD_NONE,
1156 audio ? FLUSH_CMD_NONE : FLUSH_CMD_SHUTDOWN));
Andy Hung2abde2c2014-09-30 14:40:32 -07001157 processDeferredActions();
1158 break;
1159 case FLUSHING_DECODER:
1160 *flushing = FLUSHING_DECODER_SHUTDOWN; // initiate shutdown after flush.
1161 break; // Wait for flush to complete.
1162 case FLUSHING_DECODER_SHUTDOWN:
1163 break; // Wait for flush to complete.
1164 case SHUTTING_DOWN_DECODER:
1165 break; // Wait for shutdown to complete.
1166 case FLUSHED:
Andy Hung2abde2c2014-09-30 14:40:32 -07001167 getDecoder(audio)->initiateShutdown(); // In the middle of a seek.
1168 *flushing = SHUTTING_DOWN_DECODER; // Shut down.
1169 break;
1170 case SHUT_DOWN:
1171 finishFlushIfPossible(); // Should not occur.
1172 break; // Finish anyways.
Marco Nelissen9e2b7912014-08-18 16:13:03 -07001173 }
Wei Jia686e8e52017-04-03 14:08:01 -07001174 if (mSource != nullptr) {
1175 if (audio) {
Wei Jia2f6d8c52017-04-11 09:50:31 -07001176 if (mVideoDecoderError || mSource->getFormat(false /* audio */) == NULL
Wei Jiaf86731d2017-07-11 17:24:34 -07001177 || mSurface == NULL || mVideoDecoder == NULL) {
Wei Jia686e8e52017-04-03 14:08:01 -07001178 // When both audio and video have error, or this stream has only audio
1179 // which has error, notify client of error.
1180 notifyListener(MEDIA_ERROR, MEDIA_ERROR_UNKNOWN, err);
1181 } else {
1182 // Only audio track has error. Video track could be still good to play.
1183 notifyListener(MEDIA_INFO, MEDIA_INFO_PLAY_AUDIO_ERROR, err);
1184 }
1185 mAudioDecoderError = true;
1186 } else {
Wei Jia2f6d8c52017-04-11 09:50:31 -07001187 if (mAudioDecoderError || mSource->getFormat(true /* audio */) == NULL
Wei Jiaf86731d2017-07-11 17:24:34 -07001188 || mAudioSink == NULL || mAudioDecoder == NULL) {
Wei Jia686e8e52017-04-03 14:08:01 -07001189 // When both audio and video have error, or this stream has only video
1190 // which has error, notify client of error.
1191 notifyListener(MEDIA_ERROR, MEDIA_ERROR_UNKNOWN, err);
1192 } else {
1193 // Only video track has error. Audio track could be still good to play.
1194 notifyListener(MEDIA_INFO, MEDIA_INFO_PLAY_VIDEO_ERROR, err);
1195 }
1196 mVideoDecoderError = true;
1197 }
1198 }
Lajos Molnar1cd13982014-01-17 15:12:51 -08001199 } else {
1200 ALOGV("Unhandled decoder notification %d '%c%c%c%c'.",
Andreas Hubera1f8ab02012-11-30 10:53:22 -08001201 what,
1202 what >> 24,
1203 (what >> 16) & 0xff,
1204 (what >> 8) & 0xff,
1205 what & 0xff);
Andreas Huberf9334412010-12-15 15:17:42 -08001206 }
1207
1208 break;
1209 }
1210
1211 case kWhatRendererNotify:
1212 {
Wei Jia57568df2014-09-22 10:16:29 -07001213 int32_t requesterGeneration = mRendererGeneration - 1;
1214 CHECK(msg->findInt32("generation", &requesterGeneration));
1215 if (requesterGeneration != mRendererGeneration) {
1216 ALOGV("got message from old renderer, generation(%d:%d)",
1217 requesterGeneration, mRendererGeneration);
1218 return;
1219 }
1220
Andreas Huberf9334412010-12-15 15:17:42 -08001221 int32_t what;
1222 CHECK(msg->findInt32("what", &what));
1223
1224 if (what == Renderer::kWhatEOS) {
1225 int32_t audio;
1226 CHECK(msg->findInt32("audio", &audio));
1227
Andreas Huberc92fd242011-08-16 13:48:44 -07001228 int32_t finalResult;
1229 CHECK(msg->findInt32("finalResult", &finalResult));
1230
Andreas Huberf9334412010-12-15 15:17:42 -08001231 if (audio) {
1232 mAudioEOS = true;
1233 } else {
1234 mVideoEOS = true;
1235 }
1236
Andreas Huberc92fd242011-08-16 13:48:44 -07001237 if (finalResult == ERROR_END_OF_STREAM) {
Steve Block3856b092011-10-20 11:56:00 +01001238 ALOGV("reached %s EOS", audio ? "audio" : "video");
Andreas Huberc92fd242011-08-16 13:48:44 -07001239 } else {
Steve Block29357bc2012-01-06 19:20:56 +00001240 ALOGE("%s track encountered an error (%d)",
Andreas Huberc92fd242011-08-16 13:48:44 -07001241 audio ? "audio" : "video", finalResult);
1242
1243 notifyListener(
1244 MEDIA_ERROR, MEDIA_ERROR_UNKNOWN, finalResult);
1245 }
Andreas Huberf9334412010-12-15 15:17:42 -08001246
1247 if ((mAudioEOS || mAudioDecoder == NULL)
1248 && (mVideoEOS || mVideoDecoder == NULL)) {
1249 notifyListener(MEDIA_PLAYBACK_COMPLETE, 0, 0);
1250 }
Andreas Huber3fe62152011-09-16 15:09:22 -07001251 } else if (what == Renderer::kWhatFlushComplete) {
Andreas Huberf9334412010-12-15 15:17:42 -08001252 int32_t audio;
1253 CHECK(msg->findInt32("audio", &audio));
1254
Wei Jia3261f0d2015-10-08 13:58:33 -07001255 if (audio) {
1256 mAudioEOS = false;
1257 } else {
1258 mVideoEOS = false;
1259 }
1260
Steve Block3856b092011-10-20 11:56:00 +01001261 ALOGV("renderer %s flush completed.", audio ? "audio" : "video");
Wei Jiada4252f2015-07-14 18:15:28 -07001262 if (audio && (mFlushingAudio == NONE || mFlushingAudio == FLUSHED
1263 || mFlushingAudio == SHUT_DOWN)) {
1264 // Flush has been handled by tear down.
1265 break;
1266 }
Andy Hung8d121d42014-10-03 09:53:53 -07001267 handleFlushComplete(audio, false /* isDecoder */);
1268 finishFlushIfPossible();
James Dongf57b4ea2012-07-20 13:38:36 -07001269 } else if (what == Renderer::kWhatVideoRenderingStart) {
1270 notifyListener(MEDIA_INFO, MEDIA_INFO_RENDERING_START, 0);
Lajos Molnarcbaffcf2013-08-14 18:30:38 -07001271 } else if (what == Renderer::kWhatMediaRenderingStart) {
1272 ALOGV("media rendering started");
1273 notifyListener(MEDIA_STARTED, 0, 0);
Ronghua Wufaeb0f22015-05-21 12:20:21 -07001274 } else if (what == Renderer::kWhatAudioTearDown) {
Ronghua Wu08529172014-10-02 16:55:52 -07001275 int32_t reason;
1276 CHECK(msg->findInt32("reason", &reason));
Ronghua Wufaeb0f22015-05-21 12:20:21 -07001277 ALOGV("Tear down audio with reason %d.", reason);
Wei Jia8456ddd2016-04-22 14:46:43 -07001278 if (reason == Renderer::kDueToTimeout && !(mPaused && mOffloadAudio)) {
1279 // TimeoutWhenPaused is only for offload mode.
1280 ALOGW("Receive a stale message for teardown.");
1281 break;
1282 }
Wei Jiada4252f2015-07-14 18:15:28 -07001283 int64_t positionUs;
Robert Shih1a5c8592015-08-04 18:07:44 -07001284 if (!msg->findInt64("positionUs", &positionUs)) {
1285 positionUs = mPreviousSeekTimeUs;
1286 }
Ronghua Wufaeb0f22015-05-21 12:20:21 -07001287
Wei Jia5031b2f2016-02-25 11:19:31 -08001288 restartAudio(
Wei Jiaa05f1e32016-03-25 16:31:22 -07001289 positionUs, reason == Renderer::kForceNonOffload /* forceNonOffload */,
1290 reason != Renderer::kDueToTimeout /* needsToCreateAudioDecoder */);
Andreas Huberf9334412010-12-15 15:17:42 -08001291 }
1292 break;
1293 }
1294
1295 case kWhatMoreDataQueued:
1296 {
1297 break;
1298 }
1299
Andreas Huber1aef2112011-01-04 14:01:29 -08001300 case kWhatReset:
1301 {
Steve Block3856b092011-10-20 11:56:00 +01001302 ALOGV("kWhatReset");
Andreas Huber1aef2112011-01-04 14:01:29 -08001303
Ronghua Wu64c2d172015-10-07 16:52:19 -07001304 mResetting = true;
1305
Andreas Hubera1f8ab02012-11-30 10:53:22 -08001306 mDeferredActions.push_back(
Wei Jiafef808d2014-10-31 17:57:05 -07001307 new FlushDecoderAction(
1308 FLUSH_CMD_SHUTDOWN /* audio */,
1309 FLUSH_CMD_SHUTDOWN /* video */));
Andreas Huberb7c8e912012-11-27 15:02:53 -08001310
Andreas Hubera1f8ab02012-11-30 10:53:22 -08001311 mDeferredActions.push_back(
1312 new SimpleAction(&NuPlayer::performReset));
Andreas Huberb58ce9f2011-11-28 16:27:35 -08001313
Andreas Hubera1f8ab02012-11-30 10:53:22 -08001314 processDeferredActions();
Andreas Huber1aef2112011-01-04 14:01:29 -08001315 break;
1316 }
1317
Andreas Huber43c3e6c2011-01-05 12:17:08 -08001318 case kWhatSeek:
1319 {
1320 int64_t seekTimeUs;
Wei Jiac5de0912016-11-18 10:22:14 -08001321 int32_t mode;
Wei Jiae427abf2014-09-22 15:21:11 -07001322 int32_t needNotify;
Andreas Huber43c3e6c2011-01-05 12:17:08 -08001323 CHECK(msg->findInt64("seekTimeUs", &seekTimeUs));
Wei Jiac5de0912016-11-18 10:22:14 -08001324 CHECK(msg->findInt32("mode", &mode));
Wei Jiae427abf2014-09-22 15:21:11 -07001325 CHECK(msg->findInt32("needNotify", &needNotify));
Andreas Huber43c3e6c2011-01-05 12:17:08 -08001326
Wei Jiac5de0912016-11-18 10:22:14 -08001327 ALOGV("kWhatSeek seekTimeUs=%lld us, mode=%d, needNotify=%d",
1328 (long long)seekTimeUs, mode, needNotify);
Andreas Huber43c3e6c2011-01-05 12:17:08 -08001329
Wei Jia1061c9c2015-05-19 16:02:17 -07001330 if (!mStarted) {
1331 // Seek before the player is started. In order to preview video,
1332 // need to start the player and pause it. This branch is called
1333 // only once if needed. After the player is started, any seek
1334 // operation will go through normal path.
Robert Shih0c61a0d2015-07-06 15:09:10 -07001335 // Audio-only cases are handled separately.
Wei Jiac5de0912016-11-18 10:22:14 -08001336 onStart(seekTimeUs, (MediaPlayerSeekMode)mode);
Robert Shih0c61a0d2015-07-06 15:09:10 -07001337 if (mStarted) {
1338 onPause();
1339 mPausedByClient = true;
1340 }
Wei Jia1061c9c2015-05-19 16:02:17 -07001341 if (needNotify) {
1342 notifyDriverSeekComplete();
1343 }
1344 break;
1345 }
1346
Andreas Hubera1f8ab02012-11-30 10:53:22 -08001347 mDeferredActions.push_back(
Wei Jiafef808d2014-10-31 17:57:05 -07001348 new FlushDecoderAction(FLUSH_CMD_FLUSH /* audio */,
1349 FLUSH_CMD_FLUSH /* video */));
Andreas Huber43c3e6c2011-01-05 12:17:08 -08001350
Wei Jiae427abf2014-09-22 15:21:11 -07001351 mDeferredActions.push_back(
Wei Jiac5de0912016-11-18 10:22:14 -08001352 new SeekAction(seekTimeUs, (MediaPlayerSeekMode)mode));
Andreas Huber43c3e6c2011-01-05 12:17:08 -08001353
Chong Zhangf8d71772014-11-26 15:08:34 -08001354 // After a flush without shutdown, decoder is paused.
1355 // Don't resume it until source seek is done, otherwise it could
Chong Zhang7137ec72014-11-12 16:41:05 -08001356 // start pulling stale data too soon.
1357 mDeferredActions.push_back(
Chong Zhangf8d71772014-11-26 15:08:34 -08001358 new ResumeDecoderAction(needNotify));
Chong Zhang7137ec72014-11-12 16:41:05 -08001359
Andreas Hubera1f8ab02012-11-30 10:53:22 -08001360 processDeferredActions();
Andreas Huber43c3e6c2011-01-05 12:17:08 -08001361 break;
1362 }
1363
Andreas Huberb4082222011-01-20 15:23:04 -08001364 case kWhatPause:
1365 {
Chong Zhangefbb6192015-01-30 17:13:27 -08001366 onPause();
1367 mPausedByClient = true;
Andreas Huberb4082222011-01-20 15:23:04 -08001368 break;
1369 }
1370
Andreas Huberb5f25f02013-02-05 10:14:26 -08001371 case kWhatSourceNotify:
1372 {
Andreas Huber9575c962013-02-05 13:59:56 -08001373 onSourceNotify(msg);
Andreas Huberb5f25f02013-02-05 10:14:26 -08001374 break;
1375 }
1376
Chong Zhanga7fa1d92014-06-11 14:49:23 -07001377 case kWhatClosedCaptionNotify:
1378 {
1379 onClosedCaptionNotify(msg);
1380 break;
1381 }
1382
Hassan Shojaniacefac142017-02-06 21:02:02 -08001383 case kWhatPrepareDrm:
1384 {
1385 status_t status = onPrepareDrm(msg);
1386
1387 sp<AMessage> response = new AMessage;
1388 response->setInt32("status", status);
1389 sp<AReplyToken> replyID;
1390 CHECK(msg->senderAwaitsResponse(&replyID));
1391 response->postReply(replyID);
1392 break;
1393 }
1394
1395 case kWhatReleaseDrm:
1396 {
1397 status_t status = onReleaseDrm();
1398
1399 sp<AMessage> response = new AMessage;
1400 response->setInt32("status", status);
1401 sp<AReplyToken> replyID;
1402 CHECK(msg->senderAwaitsResponse(&replyID));
1403 response->postReply(replyID);
1404 break;
1405 }
1406
Andreas Huberf9334412010-12-15 15:17:42 -08001407 default:
1408 TRESPASS();
1409 break;
1410 }
1411}
1412
Wei Jia94211742014-10-28 17:09:06 -07001413void NuPlayer::onResume() {
Ronghua Wu64c2d172015-10-07 16:52:19 -07001414 if (!mPaused || mResetting) {
1415 ALOGD_IF(mResetting, "resetting, onResume discarded");
Chong Zhangefbb6192015-01-30 17:13:27 -08001416 return;
1417 }
1418 mPaused = false;
Wei Jia94211742014-10-28 17:09:06 -07001419 if (mSource != NULL) {
1420 mSource->resume();
1421 } else {
1422 ALOGW("resume called when source is gone or not set");
1423 }
1424 // |mAudioDecoder| may have been released due to the pause timeout, so re-create it if
1425 // needed.
1426 if (audioDecoderStillNeeded() && mAudioDecoder == NULL) {
1427 instantiateDecoder(true /* audio */, &mAudioDecoder);
1428 }
1429 if (mRenderer != NULL) {
1430 mRenderer->resume();
1431 } else {
1432 ALOGW("resume called when renderer is gone or not set");
1433 }
Ray Essickd4d00612017-01-03 09:36:27 -08001434
1435 mLastStartedPlayingTimeNs = systemTime();
Wei Jia94211742014-10-28 17:09:06 -07001436}
1437
Lajos Molnarfcd3e942015-03-31 10:06:48 -07001438status_t NuPlayer::onInstantiateSecureDecoders() {
1439 status_t err;
1440 if (!(mSourceFlags & Source::FLAG_SECURE)) {
1441 return BAD_TYPE;
1442 }
1443
1444 if (mRenderer != NULL) {
1445 ALOGE("renderer should not be set when instantiating secure decoders");
1446 return UNKNOWN_ERROR;
1447 }
1448
1449 // TRICKY: We rely on mRenderer being null, so that decoder does not start requesting
1450 // data on instantiation.
Lajos Molnar1de1e252015-04-30 18:18:34 -07001451 if (mSurface != NULL) {
Lajos Molnarfcd3e942015-03-31 10:06:48 -07001452 err = instantiateDecoder(false, &mVideoDecoder);
1453 if (err != OK) {
1454 return err;
1455 }
1456 }
1457
1458 if (mAudioSink != NULL) {
1459 err = instantiateDecoder(true, &mAudioDecoder);
1460 if (err != OK) {
1461 return err;
1462 }
1463 }
1464 return OK;
1465}
1466
Wei Jiac5de0912016-11-18 10:22:14 -08001467void NuPlayer::onStart(int64_t startPositionUs, MediaPlayerSeekMode mode) {
Hassan Shojaniacefac142017-02-06 21:02:02 -08001468 ALOGV("onStart: mCrypto: %p (%d)", mCrypto.get(),
1469 (mCrypto != NULL ? mCrypto->getStrongCount() : 0));
1470
Robert Shih0c61a0d2015-07-06 15:09:10 -07001471 if (!mSourceStarted) {
1472 mSourceStarted = true;
1473 mSource->start();
1474 }
1475 if (startPositionUs > 0) {
Wei Jiac5de0912016-11-18 10:22:14 -08001476 performSeek(startPositionUs, mode);
Robert Shih0c61a0d2015-07-06 15:09:10 -07001477 if (mSource->getFormat(false /* audio */) == NULL) {
1478 return;
1479 }
1480 }
1481
Wei Jia94211742014-10-28 17:09:06 -07001482 mOffloadAudio = false;
1483 mAudioEOS = false;
1484 mVideoEOS = false;
Wei Jia94211742014-10-28 17:09:06 -07001485 mStarted = true;
Wei Jiafe8fe7d2016-06-08 10:29:25 -07001486 mPaused = false;
Wei Jia94211742014-10-28 17:09:06 -07001487
Wei Jia94211742014-10-28 17:09:06 -07001488 uint32_t flags = 0;
1489
1490 if (mSource->isRealTime()) {
1491 flags |= Renderer::FLAG_REAL_TIME;
1492 }
1493
Wei Jia9737d342016-12-28 14:59:59 -08001494 bool hasAudio = (mSource->getFormat(true /* audio */) != NULL);
1495 bool hasVideo = (mSource->getFormat(false /* audio */) != NULL);
1496 if (!hasAudio && !hasVideo) {
Wei Jia1de83d52016-10-10 10:15:03 -07001497 ALOGE("no metadata for either audio or video source");
1498 mSource->stop();
1499 mSourceStarted = false;
1500 notifyListener(MEDIA_ERROR, MEDIA_ERROR_UNKNOWN, ERROR_MALFORMED);
1501 return;
1502 }
Wei Jia9737d342016-12-28 14:59:59 -08001503 ALOGV_IF(!hasAudio, "no metadata for audio source"); // video only stream
1504
1505 sp<MetaData> audioMeta = mSource->getFormatMeta(true /* audio */);
Wei Jia1de83d52016-10-10 10:15:03 -07001506
Wei Jia94211742014-10-28 17:09:06 -07001507 audio_stream_type_t streamType = AUDIO_STREAM_MUSIC;
1508 if (mAudioSink != NULL) {
1509 streamType = mAudioSink->getAudioStreamType();
1510 }
1511
Wei Jia94211742014-10-28 17:09:06 -07001512 mOffloadAudio =
Wei Jia9737d342016-12-28 14:59:59 -08001513 canOffloadStream(audioMeta, hasVideo, mSource->isStreaming(), streamType)
Wei Jiabf70feb2016-02-19 15:47:16 -08001514 && (mPlaybackSettings.mSpeed == 1.f && mPlaybackSettings.mPitch == 1.f);
Hassan Shojaniacefac142017-02-06 21:02:02 -08001515
1516 // Modular DRM: Disabling audio offload if the source is protected
1517 if (mOffloadAudio && mIsDrmProtected) {
1518 mOffloadAudio = false;
1519 ALOGV("onStart: Disabling mOffloadAudio now that the source is protected.");
1520 }
1521
Wei Jia94211742014-10-28 17:09:06 -07001522 if (mOffloadAudio) {
1523 flags |= Renderer::FLAG_OFFLOAD_AUDIO;
1524 }
1525
Lajos Molnar1d15ab52015-03-04 16:46:34 -08001526 sp<AMessage> notify = new AMessage(kWhatRendererNotify, this);
Wei Jia94211742014-10-28 17:09:06 -07001527 ++mRendererGeneration;
1528 notify->setInt32("generation", mRendererGeneration);
Wei Jia0a68f662017-08-30 18:01:26 -07001529 mRenderer = new Renderer(mAudioSink, mMediaClock, notify, flags);
Wei Jia94211742014-10-28 17:09:06 -07001530 mRendererLooper = new ALooper;
1531 mRendererLooper->setName("NuPlayerRenderer");
1532 mRendererLooper->start(false, false, ANDROID_PRIORITY_AUDIO);
1533 mRendererLooper->registerHandler(mRenderer);
Lajos Molnar3a474aa2015-04-24 17:10:07 -07001534
1535 status_t err = mRenderer->setPlaybackSettings(mPlaybackSettings);
1536 if (err != OK) {
1537 mSource->stop();
Robert Shih0c61a0d2015-07-06 15:09:10 -07001538 mSourceStarted = false;
Lajos Molnar3a474aa2015-04-24 17:10:07 -07001539 notifyListener(MEDIA_ERROR, MEDIA_ERROR_UNKNOWN, err);
1540 return;
Wei Jiac8206ff2015-03-04 13:59:37 -08001541 }
Wei Jia94211742014-10-28 17:09:06 -07001542
Ronghua Wuc8a70d32015-04-29 16:26:34 -07001543 float rate = getFrameRate();
1544 if (rate > 0) {
Wei Jia94211742014-10-28 17:09:06 -07001545 mRenderer->setVideoFrameRate(rate);
1546 }
1547
Wei Jiac6cfd702014-11-11 16:33:20 -08001548 if (mVideoDecoder != NULL) {
1549 mVideoDecoder->setRenderer(mRenderer);
1550 }
1551 if (mAudioDecoder != NULL) {
1552 mAudioDecoder->setRenderer(mRenderer);
1553 }
1554
Ray Essickd4d00612017-01-03 09:36:27 -08001555 mLastStartedPlayingTimeNs = systemTime();
1556
Wei Jia94211742014-10-28 17:09:06 -07001557 postScanSources();
1558}
1559
Chong Zhangefbb6192015-01-30 17:13:27 -08001560void NuPlayer::onPause() {
1561 if (mPaused) {
1562 return;
1563 }
1564 mPaused = true;
1565 if (mSource != NULL) {
1566 mSource->pause();
1567 } else {
1568 ALOGW("pause called when source is gone or not set");
1569 }
1570 if (mRenderer != NULL) {
1571 mRenderer->pause();
1572 } else {
1573 ALOGW("pause called when renderer is gone or not set");
1574 }
Ray Essickd4d00612017-01-03 09:36:27 -08001575
1576 sp<NuPlayerDriver> driver = mDriver.promote();
1577 if (driver != NULL) {
1578 int64_t now = systemTime();
1579 int64_t played = now - mLastStartedPlayingTimeNs;
Ray Essickd4d00612017-01-03 09:36:27 -08001580
1581 driver->notifyMorePlayingTimeUs((played+500)/1000);
1582 }
Chong Zhangefbb6192015-01-30 17:13:27 -08001583}
1584
Ronghua Wud7988b12014-10-03 15:19:10 -07001585bool NuPlayer::audioDecoderStillNeeded() {
1586 // Audio decoder is no longer needed if it's in shut/shutting down status.
1587 return ((mFlushingAudio != SHUT_DOWN) && (mFlushingAudio != SHUTTING_DOWN_DECODER));
1588}
1589
Andy Hung8d121d42014-10-03 09:53:53 -07001590void NuPlayer::handleFlushComplete(bool audio, bool isDecoder) {
1591 // We wait for both the decoder flush and the renderer flush to complete
1592 // before entering either the FLUSHED or the SHUTTING_DOWN_DECODER state.
1593
1594 mFlushComplete[audio][isDecoder] = true;
1595 if (!mFlushComplete[audio][!isDecoder]) {
1596 return;
1597 }
1598
1599 FlushStatus *state = audio ? &mFlushingAudio : &mFlushingVideo;
1600 switch (*state) {
1601 case FLUSHING_DECODER:
1602 {
1603 *state = FLUSHED;
Andy Hung8d121d42014-10-03 09:53:53 -07001604 break;
1605 }
1606
1607 case FLUSHING_DECODER_SHUTDOWN:
1608 {
1609 *state = SHUTTING_DOWN_DECODER;
1610
1611 ALOGV("initiating %s decoder shutdown", audio ? "audio" : "video");
Andy Hung8d121d42014-10-03 09:53:53 -07001612 getDecoder(audio)->initiateShutdown();
1613 break;
1614 }
1615
1616 default:
1617 // decoder flush completes only occur in a flushing state.
1618 LOG_ALWAYS_FATAL_IF(isDecoder, "decoder flush in invalid state %d", *state);
1619 break;
1620 }
1621}
1622
Andreas Huber3831a062010-12-21 10:22:33 -08001623void NuPlayer::finishFlushIfPossible() {
Wei Jia53904f32014-07-29 10:22:53 -07001624 if (mFlushingAudio != NONE && mFlushingAudio != FLUSHED
1625 && mFlushingAudio != SHUT_DOWN) {
Andreas Huber3831a062010-12-21 10:22:33 -08001626 return;
1627 }
1628
Wei Jia53904f32014-07-29 10:22:53 -07001629 if (mFlushingVideo != NONE && mFlushingVideo != FLUSHED
1630 && mFlushingVideo != SHUT_DOWN) {
Andreas Huber3831a062010-12-21 10:22:33 -08001631 return;
1632 }
1633
Steve Block3856b092011-10-20 11:56:00 +01001634 ALOGV("both audio and video are flushed now.");
Andreas Huber3831a062010-12-21 10:22:33 -08001635
Andreas Huber3831a062010-12-21 10:22:33 -08001636 mFlushingAudio = NONE;
1637 mFlushingVideo = NONE;
Andreas Huber3831a062010-12-21 10:22:33 -08001638
Andy Hung8d121d42014-10-03 09:53:53 -07001639 clearFlushComplete();
1640
Andreas Hubera1f8ab02012-11-30 10:53:22 -08001641 processDeferredActions();
Andreas Huber1aef2112011-01-04 14:01:29 -08001642}
1643
1644void NuPlayer::postScanSources() {
1645 if (mScanSourcesPending) {
1646 return;
1647 }
1648
Lajos Molnar1d15ab52015-03-04 16:46:34 -08001649 sp<AMessage> msg = new AMessage(kWhatScanSources, this);
Andreas Huber1aef2112011-01-04 14:01:29 -08001650 msg->setInt32("generation", mScanSourcesGeneration);
1651 msg->post();
1652
1653 mScanSourcesPending = true;
1654}
1655
Wei Jia41cd4632016-05-13 10:53:30 -07001656void NuPlayer::tryOpenAudioSinkForOffload(
1657 const sp<AMessage> &format, const sp<MetaData> &audioMeta, bool hasVideo) {
Andy Hung202bce12014-12-03 11:47:36 -08001658 // Note: This is called early in NuPlayer to determine whether offloading
1659 // is possible; otherwise the decoders call the renderer openAudioSink directly.
Andy Hung282a7e32014-08-14 15:56:34 -07001660
Andy Hung202bce12014-12-03 11:47:36 -08001661 status_t err = mRenderer->openAudioSink(
Dhananjay Kumarc387f2b2015-08-06 10:43:16 +05301662 format, true /* offloadOnly */, hasVideo,
1663 AUDIO_OUTPUT_FLAG_NONE, &mOffloadAudio, mSource->isStreaming());
Andy Hung202bce12014-12-03 11:47:36 -08001664 if (err != OK) {
1665 // Any failure we turn off mOffloadAudio.
1666 mOffloadAudio = false;
1667 } else if (mOffloadAudio) {
Chong Zhang3b9eb1f2014-10-15 17:05:08 -07001668 sendMetaDataToHal(mAudioSink, audioMeta);
Andy Hung282a7e32014-08-14 15:56:34 -07001669 }
1670}
1671
1672void NuPlayer::closeAudioSink() {
Chong Zhang3b9eb1f2014-10-15 17:05:08 -07001673 mRenderer->closeAudioSink();
Andy Hung282a7e32014-08-14 15:56:34 -07001674}
1675
Wei Jia5031b2f2016-02-25 11:19:31 -08001676void NuPlayer::restartAudio(
Wei Jiabf70feb2016-02-19 15:47:16 -08001677 int64_t currentPositionUs, bool forceNonOffload, bool needsToCreateAudioDecoder) {
Wei Jiaa05f1e32016-03-25 16:31:22 -07001678 if (mAudioDecoder != NULL) {
1679 mAudioDecoder->pause();
1680 mAudioDecoder.clear();
Wei Jia686e8e52017-04-03 14:08:01 -07001681 mAudioDecoderError = false;
Wei Jiaa05f1e32016-03-25 16:31:22 -07001682 ++mAudioDecoderGeneration;
1683 }
Wei Jiabf70feb2016-02-19 15:47:16 -08001684 if (mFlushingAudio == FLUSHING_DECODER) {
1685 mFlushComplete[1 /* audio */][1 /* isDecoder */] = true;
1686 mFlushingAudio = FLUSHED;
1687 finishFlushIfPossible();
1688 } else if (mFlushingAudio == FLUSHING_DECODER_SHUTDOWN
1689 || mFlushingAudio == SHUTTING_DOWN_DECODER) {
1690 mFlushComplete[1 /* audio */][1 /* isDecoder */] = true;
1691 mFlushingAudio = SHUT_DOWN;
1692 finishFlushIfPossible();
1693 needsToCreateAudioDecoder = false;
1694 }
1695 if (mRenderer == NULL) {
1696 return;
1697 }
1698 closeAudioSink();
1699 mRenderer->flush(true /* audio */, false /* notifyComplete */);
1700 if (mVideoDecoder != NULL) {
1701 mRenderer->flush(false /* audio */, false /* notifyComplete */);
1702 }
1703
Wei Jiac5de0912016-11-18 10:22:14 -08001704 performSeek(currentPositionUs, MediaPlayerSeekMode::SEEK_PREVIOUS_SYNC /* mode */);
Wei Jiabf70feb2016-02-19 15:47:16 -08001705
1706 if (forceNonOffload) {
1707 mRenderer->signalDisableOffloadAudio();
1708 mOffloadAudio = false;
1709 }
1710 if (needsToCreateAudioDecoder) {
Wei Jiaa05f1e32016-03-25 16:31:22 -07001711 instantiateDecoder(true /* audio */, &mAudioDecoder, !forceNonOffload);
Wei Jiabf70feb2016-02-19 15:47:16 -08001712 }
1713}
1714
Wei Jia41cd4632016-05-13 10:53:30 -07001715void NuPlayer::determineAudioModeChange(const sp<AMessage> &audioFormat) {
Wei Jiae4d18c72015-07-13 17:58:11 -07001716 if (mSource == NULL || mAudioSink == NULL) {
1717 return;
1718 }
1719
1720 if (mRenderer == NULL) {
1721 ALOGW("No renderer can be used to determine audio mode. Use non-offload for safety.");
1722 mOffloadAudio = false;
1723 return;
1724 }
1725
1726 sp<MetaData> audioMeta = mSource->getFormatMeta(true /* audio */);
1727 sp<AMessage> videoFormat = mSource->getFormat(false /* audio */);
1728 audio_stream_type_t streamType = mAudioSink->getAudioStreamType();
1729 const bool hasVideo = (videoFormat != NULL);
Hassan Shojaniacefac142017-02-06 21:02:02 -08001730 bool canOffload = canOffloadStream(
Wei Jiabf70feb2016-02-19 15:47:16 -08001731 audioMeta, hasVideo, mSource->isStreaming(), streamType)
1732 && (mPlaybackSettings.mSpeed == 1.f && mPlaybackSettings.mPitch == 1.f);
Hassan Shojaniacefac142017-02-06 21:02:02 -08001733
1734 // Modular DRM: Disabling audio offload if the source is protected
1735 if (canOffload && mIsDrmProtected) {
1736 canOffload = false;
1737 ALOGV("determineAudioModeChange: Disabling mOffloadAudio b/c the source is protected.");
1738 }
1739
Wei Jiae4d18c72015-07-13 17:58:11 -07001740 if (canOffload) {
1741 if (!mOffloadAudio) {
1742 mRenderer->signalEnableOffloadAudio();
1743 }
1744 // open audio sink early under offload mode.
Wei Jia41cd4632016-05-13 10:53:30 -07001745 tryOpenAudioSinkForOffload(audioFormat, audioMeta, hasVideo);
Wei Jiae4d18c72015-07-13 17:58:11 -07001746 } else {
Robert Shihe1d70192015-07-23 17:54:13 -07001747 if (mOffloadAudio) {
1748 mRenderer->signalDisableOffloadAudio();
1749 mOffloadAudio = false;
1750 }
Wei Jiae4d18c72015-07-13 17:58:11 -07001751 }
1752}
1753
Wei Jiaa05f1e32016-03-25 16:31:22 -07001754status_t NuPlayer::instantiateDecoder(
1755 bool audio, sp<DecoderBase> *decoder, bool checkAudioModeChange) {
Wei Jia566da802015-08-27 13:59:30 -07001756 // The audio decoder could be cleared by tear down. If still in shut down
1757 // process, no need to create a new audio decoder.
1758 if (*decoder != NULL || (audio && mFlushingAudio == SHUT_DOWN)) {
Andreas Huberf9334412010-12-15 15:17:42 -08001759 return OK;
1760 }
1761
Andreas Huber84066782011-08-16 09:34:26 -07001762 sp<AMessage> format = mSource->getFormat(audio);
Andreas Huberf9334412010-12-15 15:17:42 -08001763
Andreas Huber84066782011-08-16 09:34:26 -07001764 if (format == NULL) {
Robert Shih7350b052015-10-01 15:50:14 -07001765 return UNKNOWN_ERROR;
1766 } else {
1767 status_t err;
1768 if (format->findInt32("err", &err) && err) {
1769 return err;
1770 }
Andreas Huberf9334412010-12-15 15:17:42 -08001771 }
1772
Ronghua Wu8db88132015-04-22 13:51:35 -07001773 format->setInt32("priority", 0 /* realtime */);
1774
Andreas Huber3fe62152011-09-16 15:09:22 -07001775 if (!audio) {
Andreas Huber84066782011-08-16 09:34:26 -07001776 AString mime;
1777 CHECK(format->findString("mime", &mime));
Chong Zhanga7fa1d92014-06-11 14:49:23 -07001778
Lajos Molnar1d15ab52015-03-04 16:46:34 -08001779 sp<AMessage> ccNotify = new AMessage(kWhatClosedCaptionNotify, this);
Chong Zhang341ab6e2015-02-04 13:37:18 -08001780 if (mCCDecoder == NULL) {
1781 mCCDecoder = new CCDecoder(ccNotify);
1782 }
Lajos Molnar09524832014-07-17 14:29:51 -07001783
1784 if (mSourceFlags & Source::FLAG_SECURE) {
1785 format->setInt32("secure", true);
1786 }
Chong Zhang17134602015-01-07 16:14:34 -08001787
1788 if (mSourceFlags & Source::FLAG_PROTECTED) {
1789 format->setInt32("protected", true);
1790 }
Ronghua Wu8db88132015-04-22 13:51:35 -07001791
Ronghua Wuc8a70d32015-04-29 16:26:34 -07001792 float rate = getFrameRate();
1793 if (rate > 0) {
Lajos Molnar3a474aa2015-04-24 17:10:07 -07001794 format->setFloat("operating-rate", rate * mPlaybackSettings.mSpeed);
Ronghua Wu8db88132015-04-22 13:51:35 -07001795 }
Andreas Huber3fe62152011-09-16 15:09:22 -07001796 }
1797
Wei Jiabc2fb722014-07-08 16:37:57 -07001798 if (audio) {
Lajos Molnar1d15ab52015-03-04 16:46:34 -08001799 sp<AMessage> notify = new AMessage(kWhatAudioNotify, this);
Wei Jia88703c32014-08-06 11:24:07 -07001800 ++mAudioDecoderGeneration;
1801 notify->setInt32("generation", mAudioDecoderGeneration);
1802
Wei Jiaa05f1e32016-03-25 16:31:22 -07001803 if (checkAudioModeChange) {
Wei Jia41cd4632016-05-13 10:53:30 -07001804 determineAudioModeChange(format);
Wei Jiaa05f1e32016-03-25 16:31:22 -07001805 }
Wei Jiabc2fb722014-07-08 16:37:57 -07001806 if (mOffloadAudio) {
Wei Jia14532f22015-12-29 11:28:15 -08001807 mSource->setOffloadAudio(true /* offload */);
1808
Haynes Mathew George8b635332015-03-30 17:59:47 -07001809 const bool hasVideo = (mSource->getFormat(false /*audio */) != NULL);
1810 format->setInt32("has-video", hasVideo);
Wei Jiac6cfd702014-11-11 16:33:20 -08001811 *decoder = new DecoderPassThrough(notify, mSource, mRenderer);
Hassan Shojaniacefac142017-02-06 21:02:02 -08001812 ALOGV("instantiateDecoder audio DecoderPassThrough hasVideo: %d", hasVideo);
Wei Jiabc2fb722014-07-08 16:37:57 -07001813 } else {
Wei Jia14532f22015-12-29 11:28:15 -08001814 mSource->setOffloadAudio(false /* offload */);
1815
Wei Jiaf2ae3e12016-10-27 17:10:59 -07001816 *decoder = new Decoder(notify, mSource, mPID, mUID, mRenderer);
Hassan Shojaniacefac142017-02-06 21:02:02 -08001817 ALOGV("instantiateDecoder audio Decoder");
Wei Jiabc2fb722014-07-08 16:37:57 -07001818 }
Wei Jia686e8e52017-04-03 14:08:01 -07001819 mAudioDecoderError = false;
Wei Jiabc2fb722014-07-08 16:37:57 -07001820 } else {
Lajos Molnar1d15ab52015-03-04 16:46:34 -08001821 sp<AMessage> notify = new AMessage(kWhatVideoNotify, this);
Wei Jia88703c32014-08-06 11:24:07 -07001822 ++mVideoDecoderGeneration;
1823 notify->setInt32("generation", mVideoDecoderGeneration);
1824
Chong Zhang7137ec72014-11-12 16:41:05 -08001825 *decoder = new Decoder(
Wei Jiaf2ae3e12016-10-27 17:10:59 -07001826 notify, mSource, mPID, mUID, mRenderer, mSurface, mCCDecoder);
Wei Jia686e8e52017-04-03 14:08:01 -07001827 mVideoDecoderError = false;
Lajos Molnard9fd6312014-11-06 11:00:00 -08001828
1829 // enable FRC if high-quality AV sync is requested, even if not
Lajos Molnar1de1e252015-04-30 18:18:34 -07001830 // directly queuing to display, as this will even improve textureview
Lajos Molnard9fd6312014-11-06 11:00:00 -08001831 // playback.
1832 {
Marco Nelissen96626b72016-12-01 13:58:59 -08001833 if (property_get_bool("persist.sys.media.avsync", false)) {
Lajos Molnard9fd6312014-11-06 11:00:00 -08001834 format->setInt32("auto-frc", 1);
1835 }
1836 }
Wei Jiabc2fb722014-07-08 16:37:57 -07001837 }
Lajos Molnar1cd13982014-01-17 15:12:51 -08001838 (*decoder)->init();
Hassan Shojaniacefac142017-02-06 21:02:02 -08001839
1840 // Modular DRM
1841 if (mIsDrmProtected) {
1842 format->setPointer("crypto", mCrypto.get());
1843 ALOGV("instantiateDecoder: mCrypto: %p (%d) isSecure: %d", mCrypto.get(),
1844 (mCrypto != NULL ? mCrypto->getStrongCount() : 0),
1845 (mSourceFlags & Source::FLAG_SECURE) != 0);
1846 }
1847
Andreas Huber84066782011-08-16 09:34:26 -07001848 (*decoder)->configure(format);
Andreas Huberf9334412010-12-15 15:17:42 -08001849
Praveen Chavanbbaa1442016-04-08 13:33:49 -07001850 if (!audio) {
1851 sp<AMessage> params = new AMessage();
1852 float rate = getFrameRate();
1853 if (rate > 0) {
1854 params->setFloat("frame-rate-total", rate);
1855 }
1856
1857 sp<MetaData> fileMeta = getFileMeta();
1858 if (fileMeta != NULL) {
1859 int32_t videoTemporalLayerCount;
1860 if (fileMeta->findInt32(kKeyTemporalLayerCount, &videoTemporalLayerCount)
1861 && videoTemporalLayerCount > 0) {
1862 params->setInt32("temporal-layer-count", videoTemporalLayerCount);
1863 }
1864 }
1865
1866 if (params->countEntries() > 0) {
1867 (*decoder)->setParameters(params);
1868 }
1869 }
Andreas Huberf9334412010-12-15 15:17:42 -08001870 return OK;
1871}
1872
Chong Zhangced1c2f2014-08-08 15:22:35 -07001873void NuPlayer::updateVideoSize(
1874 const sp<AMessage> &inputFormat,
1875 const sp<AMessage> &outputFormat) {
1876 if (inputFormat == NULL) {
1877 ALOGW("Unknown video size, reporting 0x0!");
1878 notifyListener(MEDIA_SET_VIDEO_SIZE, 0, 0);
1879 return;
1880 }
Wei Jia9737d342016-12-28 14:59:59 -08001881 int32_t err = OK;
1882 inputFormat->findInt32("err", &err);
1883 if (err == -EWOULDBLOCK) {
1884 ALOGW("Video meta is not available yet!");
1885 return;
1886 }
1887 if (err != OK) {
1888 ALOGW("Something is wrong with video meta!");
1889 return;
1890 }
Chong Zhangced1c2f2014-08-08 15:22:35 -07001891
1892 int32_t displayWidth, displayHeight;
Chong Zhangced1c2f2014-08-08 15:22:35 -07001893 if (outputFormat != NULL) {
1894 int32_t width, height;
1895 CHECK(outputFormat->findInt32("width", &width));
1896 CHECK(outputFormat->findInt32("height", &height));
1897
1898 int32_t cropLeft, cropTop, cropRight, cropBottom;
1899 CHECK(outputFormat->findRect(
1900 "crop",
1901 &cropLeft, &cropTop, &cropRight, &cropBottom));
1902
1903 displayWidth = cropRight - cropLeft + 1;
1904 displayHeight = cropBottom - cropTop + 1;
1905
1906 ALOGV("Video output format changed to %d x %d "
1907 "(crop: %d x %d @ (%d, %d))",
1908 width, height,
1909 displayWidth,
1910 displayHeight,
1911 cropLeft, cropTop);
1912 } else {
1913 CHECK(inputFormat->findInt32("width", &displayWidth));
1914 CHECK(inputFormat->findInt32("height", &displayHeight));
1915
1916 ALOGV("Video input format %d x %d", displayWidth, displayHeight);
1917 }
1918
1919 // Take into account sample aspect ratio if necessary:
1920 int32_t sarWidth, sarHeight;
1921 if (inputFormat->findInt32("sar-width", &sarWidth)
Wei Jia095bc252017-01-27 10:16:16 -08001922 && inputFormat->findInt32("sar-height", &sarHeight)
1923 && sarWidth > 0 && sarHeight > 0) {
Chong Zhangced1c2f2014-08-08 15:22:35 -07001924 ALOGV("Sample aspect ratio %d : %d", sarWidth, sarHeight);
1925
1926 displayWidth = (displayWidth * sarWidth) / sarHeight;
1927
1928 ALOGV("display dimensions %d x %d", displayWidth, displayHeight);
Wei Jiadf6c6af2016-09-29 11:27:15 -07001929 } else {
1930 int32_t width, height;
1931 if (inputFormat->findInt32("display-width", &width)
1932 && inputFormat->findInt32("display-height", &height)
1933 && width > 0 && height > 0
1934 && displayWidth > 0 && displayHeight > 0) {
1935 if (displayHeight * (int64_t)width / height > (int64_t)displayWidth) {
1936 displayHeight = (int32_t)(displayWidth * (int64_t)height / width);
1937 } else {
1938 displayWidth = (int32_t)(displayHeight * (int64_t)width / height);
1939 }
1940 ALOGV("Video display width and height are overridden to %d x %d",
1941 displayWidth, displayHeight);
1942 }
Chong Zhangced1c2f2014-08-08 15:22:35 -07001943 }
1944
1945 int32_t rotationDegrees;
1946 if (!inputFormat->findInt32("rotation-degrees", &rotationDegrees)) {
1947 rotationDegrees = 0;
1948 }
1949
1950 if (rotationDegrees == 90 || rotationDegrees == 270) {
1951 int32_t tmp = displayWidth;
1952 displayWidth = displayHeight;
1953 displayHeight = tmp;
1954 }
1955
1956 notifyListener(
1957 MEDIA_SET_VIDEO_SIZE,
1958 displayWidth,
1959 displayHeight);
1960}
1961
Chong Zhangdcb89b32013-08-06 09:44:47 -07001962void NuPlayer::notifyListener(int msg, int ext1, int ext2, const Parcel *in) {
Andreas Huber43c3e6c2011-01-05 12:17:08 -08001963 if (mDriver == NULL) {
Andreas Huberf9334412010-12-15 15:17:42 -08001964 return;
1965 }
1966
Andreas Huber43c3e6c2011-01-05 12:17:08 -08001967 sp<NuPlayerDriver> driver = mDriver.promote();
Andreas Huberf9334412010-12-15 15:17:42 -08001968
Andreas Huber43c3e6c2011-01-05 12:17:08 -08001969 if (driver == NULL) {
Andreas Huberf9334412010-12-15 15:17:42 -08001970 return;
1971 }
1972
Chong Zhangdcb89b32013-08-06 09:44:47 -07001973 driver->notifyListener(msg, ext1, ext2, in);
Andreas Huberf9334412010-12-15 15:17:42 -08001974}
1975
Chong Zhang7137ec72014-11-12 16:41:05 -08001976void NuPlayer::flushDecoder(bool audio, bool needShutdown) {
Andreas Huber14f76722013-01-15 09:04:18 -08001977 ALOGV("[%s] flushDecoder needShutdown=%d",
1978 audio ? "audio" : "video", needShutdown);
1979
Chong Zhang7137ec72014-11-12 16:41:05 -08001980 const sp<DecoderBase> &decoder = getDecoder(audio);
Lajos Molnar87603c02014-08-20 19:25:30 -07001981 if (decoder == NULL) {
Steve Blockdf64d152012-01-04 20:05:49 +00001982 ALOGI("flushDecoder %s without decoder present",
Andreas Huber6e3d3112011-11-28 12:36:11 -08001983 audio ? "audio" : "video");
Lajos Molnar87603c02014-08-20 19:25:30 -07001984 return;
Andreas Huber6e3d3112011-11-28 12:36:11 -08001985 }
1986
Andreas Huber1aef2112011-01-04 14:01:29 -08001987 // Make sure we don't continue to scan sources until we finish flushing.
1988 ++mScanSourcesGeneration;
Chong Zhang3b032b32015-04-17 15:49:06 -07001989 if (mScanSourcesPending) {
Toshikazu Saitocbcbb792015-09-15 14:53:01 +09001990 if (!needShutdown) {
1991 mDeferredActions.push_back(
1992 new SimpleAction(&NuPlayer::performScanSources));
1993 }
Chong Zhang3b032b32015-04-17 15:49:06 -07001994 mScanSourcesPending = false;
1995 }
Andreas Huber1aef2112011-01-04 14:01:29 -08001996
Chong Zhang7137ec72014-11-12 16:41:05 -08001997 decoder->signalFlush();
Andreas Huber1aef2112011-01-04 14:01:29 -08001998
1999 FlushStatus newStatus =
2000 needShutdown ? FLUSHING_DECODER_SHUTDOWN : FLUSHING_DECODER;
2001
Lajos Molnarfcd3e942015-03-31 10:06:48 -07002002 mFlushComplete[audio][false /* isDecoder */] = (mRenderer == NULL);
Andy Hung8d121d42014-10-03 09:53:53 -07002003 mFlushComplete[audio][true /* isDecoder */] = false;
Andreas Huber1aef2112011-01-04 14:01:29 -08002004 if (audio) {
Wei Jia53904f32014-07-29 10:22:53 -07002005 ALOGE_IF(mFlushingAudio != NONE,
2006 "audio flushDecoder() is called in state %d", mFlushingAudio);
Andreas Huber1aef2112011-01-04 14:01:29 -08002007 mFlushingAudio = newStatus;
Andreas Huber1aef2112011-01-04 14:01:29 -08002008 } else {
Wei Jia53904f32014-07-29 10:22:53 -07002009 ALOGE_IF(mFlushingVideo != NONE,
2010 "video flushDecoder() is called in state %d", mFlushingVideo);
Andreas Huber1aef2112011-01-04 14:01:29 -08002011 mFlushingVideo = newStatus;
Andreas Huber1aef2112011-01-04 14:01:29 -08002012 }
2013}
2014
Chong Zhangced1c2f2014-08-08 15:22:35 -07002015void NuPlayer::queueDecoderShutdown(
2016 bool audio, bool video, const sp<AMessage> &reply) {
2017 ALOGI("queueDecoderShutdown audio=%d, video=%d", audio, video);
Andreas Huber84066782011-08-16 09:34:26 -07002018
Chong Zhangced1c2f2014-08-08 15:22:35 -07002019 mDeferredActions.push_back(
Wei Jiafef808d2014-10-31 17:57:05 -07002020 new FlushDecoderAction(
2021 audio ? FLUSH_CMD_SHUTDOWN : FLUSH_CMD_NONE,
2022 video ? FLUSH_CMD_SHUTDOWN : FLUSH_CMD_NONE));
Andreas Huber84066782011-08-16 09:34:26 -07002023
Chong Zhangced1c2f2014-08-08 15:22:35 -07002024 mDeferredActions.push_back(
2025 new SimpleAction(&NuPlayer::performScanSources));
Andreas Huber84066782011-08-16 09:34:26 -07002026
Chong Zhangced1c2f2014-08-08 15:22:35 -07002027 mDeferredActions.push_back(new PostMessageAction(reply));
2028
2029 processDeferredActions();
Andreas Huber84066782011-08-16 09:34:26 -07002030}
2031
James Dong0d268a32012-08-31 12:18:27 -07002032status_t NuPlayer::setVideoScalingMode(int32_t mode) {
2033 mVideoScalingMode = mode;
Lajos Molnar1de1e252015-04-30 18:18:34 -07002034 if (mSurface != NULL) {
2035 status_t ret = native_window_set_scaling_mode(mSurface.get(), mVideoScalingMode);
James Dong0d268a32012-08-31 12:18:27 -07002036 if (ret != OK) {
2037 ALOGE("Failed to set scaling mode (%d): %s",
2038 -ret, strerror(-ret));
2039 return ret;
2040 }
2041 }
2042 return OK;
2043}
2044
Chong Zhangdcb89b32013-08-06 09:44:47 -07002045status_t NuPlayer::getTrackInfo(Parcel* reply) const {
Lajos Molnar1d15ab52015-03-04 16:46:34 -08002046 sp<AMessage> msg = new AMessage(kWhatGetTrackInfo, this);
Chong Zhangdcb89b32013-08-06 09:44:47 -07002047 msg->setPointer("reply", reply);
2048
2049 sp<AMessage> response;
2050 status_t err = msg->postAndAwaitResponse(&response);
2051 return err;
2052}
2053
Robert Shih7c4f0d72014-07-09 18:53:31 -07002054status_t NuPlayer::getSelectedTrack(int32_t type, Parcel* reply) const {
Lajos Molnar1d15ab52015-03-04 16:46:34 -08002055 sp<AMessage> msg = new AMessage(kWhatGetSelectedTrack, this);
Robert Shih7c4f0d72014-07-09 18:53:31 -07002056 msg->setPointer("reply", reply);
2057 msg->setInt32("type", type);
2058
2059 sp<AMessage> response;
2060 status_t err = msg->postAndAwaitResponse(&response);
2061 if (err == OK && response != NULL) {
2062 CHECK(response->findInt32("err", &err));
2063 }
2064 return err;
2065}
2066
Robert Shih6ffb1fd2014-10-29 16:24:32 -07002067status_t NuPlayer::selectTrack(size_t trackIndex, bool select, int64_t timeUs) {
Lajos Molnar1d15ab52015-03-04 16:46:34 -08002068 sp<AMessage> msg = new AMessage(kWhatSelectTrack, this);
Chong Zhangdcb89b32013-08-06 09:44:47 -07002069 msg->setSize("trackIndex", trackIndex);
2070 msg->setInt32("select", select);
Robert Shih6ffb1fd2014-10-29 16:24:32 -07002071 msg->setInt64("timeUs", timeUs);
Chong Zhangdcb89b32013-08-06 09:44:47 -07002072
2073 sp<AMessage> response;
2074 status_t err = msg->postAndAwaitResponse(&response);
2075
Chong Zhang404fced2014-06-11 14:45:31 -07002076 if (err != OK) {
2077 return err;
2078 }
2079
2080 if (!response->findInt32("err", &err)) {
2081 err = OK;
2082 }
2083
Chong Zhangdcb89b32013-08-06 09:44:47 -07002084 return err;
2085}
2086
Ronghua Wua73d9e02014-10-08 15:13:29 -07002087status_t NuPlayer::getCurrentPosition(int64_t *mediaUs) {
2088 sp<Renderer> renderer = mRenderer;
2089 if (renderer == NULL) {
2090 return NO_INIT;
2091 }
2092
2093 return renderer->getCurrentPosition(mediaUs);
2094}
2095
Praveen Chavane1e5d7a2015-05-19 19:09:48 -07002096void NuPlayer::getStats(Vector<sp<AMessage> > *mTrackStats) {
2097 CHECK(mTrackStats != NULL);
2098
2099 mTrackStats->clear();
2100 if (mVideoDecoder != NULL) {
2101 mTrackStats->push_back(mVideoDecoder->getStats());
2102 }
2103 if (mAudioDecoder != NULL) {
2104 mTrackStats->push_back(mAudioDecoder->getStats());
Chong Zhang7137ec72014-11-12 16:41:05 -08002105 }
Ronghua Wua73d9e02014-10-08 15:13:29 -07002106}
2107
Marco Nelissenf0b72b52014-09-16 15:43:44 -07002108sp<MetaData> NuPlayer::getFileMeta() {
2109 return mSource->getFileFormatMeta();
2110}
2111
Ronghua Wuc8a70d32015-04-29 16:26:34 -07002112float NuPlayer::getFrameRate() {
2113 sp<MetaData> meta = mSource->getFormatMeta(false /* audio */);
2114 if (meta == NULL) {
2115 return 0;
2116 }
2117 int32_t rate;
2118 if (!meta->findInt32(kKeyFrameRate, &rate)) {
2119 // fall back to try file meta
2120 sp<MetaData> fileMeta = getFileMeta();
2121 if (fileMeta == NULL) {
2122 ALOGW("source has video meta but not file meta");
2123 return -1;
2124 }
2125 int32_t fileMetaRate;
2126 if (!fileMeta->findInt32(kKeyFrameRate, &fileMetaRate)) {
2127 return -1;
2128 }
2129 return fileMetaRate;
2130 }
2131 return rate;
2132}
2133
Andreas Huberb7c8e912012-11-27 15:02:53 -08002134void NuPlayer::schedulePollDuration() {
Lajos Molnar1d15ab52015-03-04 16:46:34 -08002135 sp<AMessage> msg = new AMessage(kWhatPollDuration, this);
Andreas Huberb7c8e912012-11-27 15:02:53 -08002136 msg->setInt32("generation", mPollDurationGeneration);
2137 msg->post();
2138}
2139
2140void NuPlayer::cancelPollDuration() {
2141 ++mPollDurationGeneration;
2142}
2143
Andreas Hubera1f8ab02012-11-30 10:53:22 -08002144void NuPlayer::processDeferredActions() {
2145 while (!mDeferredActions.empty()) {
2146 // We won't execute any deferred actions until we're no longer in
2147 // an intermediate state, i.e. one more more decoders are currently
2148 // flushing or shutting down.
2149
Andreas Hubera1f8ab02012-11-30 10:53:22 -08002150 if (mFlushingAudio != NONE || mFlushingVideo != NONE) {
2151 // We're currently flushing, postpone the reset until that's
2152 // completed.
2153
2154 ALOGV("postponing action mFlushingAudio=%d, mFlushingVideo=%d",
2155 mFlushingAudio, mFlushingVideo);
2156
2157 break;
2158 }
2159
2160 sp<Action> action = *mDeferredActions.begin();
2161 mDeferredActions.erase(mDeferredActions.begin());
2162
2163 action->execute(this);
2164 }
2165}
2166
Wei Jiac5de0912016-11-18 10:22:14 -08002167void NuPlayer::performSeek(int64_t seekTimeUs, MediaPlayerSeekMode mode) {
2168 ALOGV("performSeek seekTimeUs=%lld us (%.2f secs), mode=%d",
2169 (long long)seekTimeUs, seekTimeUs / 1E6, mode);
Andreas Hubera1f8ab02012-11-30 10:53:22 -08002170
Andy Hungadf34bf2014-09-03 18:22:22 -07002171 if (mSource == NULL) {
2172 // This happens when reset occurs right before the loop mode
2173 // asynchronously seeks to the start of the stream.
2174 LOG_ALWAYS_FATAL_IF(mAudioDecoder != NULL || mVideoDecoder != NULL,
2175 "mSource is NULL and decoders not NULL audio(%p) video(%p)",
2176 mAudioDecoder.get(), mVideoDecoder.get());
2177 return;
2178 }
Robert Shih1a5c8592015-08-04 18:07:44 -07002179 mPreviousSeekTimeUs = seekTimeUs;
Wei Jiac5de0912016-11-18 10:22:14 -08002180 mSource->seekTo(seekTimeUs, mode);
Robert Shihd3b0bbb2014-07-23 15:00:25 -07002181 ++mTimedTextGeneration;
Andreas Hubera1f8ab02012-11-30 10:53:22 -08002182
Andreas Hubera1f8ab02012-11-30 10:53:22 -08002183 // everything's flushed, continue playback.
2184}
2185
Wei Jiafef808d2014-10-31 17:57:05 -07002186void NuPlayer::performDecoderFlush(FlushCommand audio, FlushCommand video) {
2187 ALOGV("performDecoderFlush audio=%d, video=%d", audio, video);
Andreas Hubera1f8ab02012-11-30 10:53:22 -08002188
Wei Jiafef808d2014-10-31 17:57:05 -07002189 if ((audio == FLUSH_CMD_NONE || mAudioDecoder == NULL)
2190 && (video == FLUSH_CMD_NONE || mVideoDecoder == NULL)) {
Andreas Hubera1f8ab02012-11-30 10:53:22 -08002191 return;
2192 }
2193
Wei Jiafef808d2014-10-31 17:57:05 -07002194 if (audio != FLUSH_CMD_NONE && mAudioDecoder != NULL) {
2195 flushDecoder(true /* audio */, (audio == FLUSH_CMD_SHUTDOWN));
Andreas Hubera1f8ab02012-11-30 10:53:22 -08002196 }
2197
Wei Jiafef808d2014-10-31 17:57:05 -07002198 if (video != FLUSH_CMD_NONE && mVideoDecoder != NULL) {
2199 flushDecoder(false /* audio */, (video == FLUSH_CMD_SHUTDOWN));
Andreas Hubera1f8ab02012-11-30 10:53:22 -08002200 }
2201}
2202
2203void NuPlayer::performReset() {
2204 ALOGV("performReset");
2205
2206 CHECK(mAudioDecoder == NULL);
2207 CHECK(mVideoDecoder == NULL);
2208
2209 cancelPollDuration();
2210
2211 ++mScanSourcesGeneration;
2212 mScanSourcesPending = false;
2213
Lajos Molnar09524832014-07-17 14:29:51 -07002214 if (mRendererLooper != NULL) {
2215 if (mRenderer != NULL) {
2216 mRendererLooper->unregisterHandler(mRenderer->id());
2217 }
2218 mRendererLooper->stop();
2219 mRendererLooper.clear();
2220 }
Andreas Hubera1f8ab02012-11-30 10:53:22 -08002221 mRenderer.clear();
Wei Jia57568df2014-09-22 10:16:29 -07002222 ++mRendererGeneration;
Andreas Hubera1f8ab02012-11-30 10:53:22 -08002223
2224 if (mSource != NULL) {
2225 mSource->stop();
Andreas Huberb5f25f02013-02-05 10:14:26 -08002226
Wei Jiac45a4b22016-04-15 15:30:23 -07002227 Mutex::Autolock autoLock(mSourceLock);
Andreas Hubera1f8ab02012-11-30 10:53:22 -08002228 mSource.clear();
2229 }
2230
2231 if (mDriver != NULL) {
2232 sp<NuPlayerDriver> driver = mDriver.promote();
2233 if (driver != NULL) {
2234 driver->notifyResetComplete();
2235 }
2236 }
Andreas Huber57a339c2012-12-03 11:18:00 -08002237
2238 mStarted = false;
Wei Jia8a092d32016-06-03 14:57:24 -07002239 mPrepared = false;
Ronghua Wu64c2d172015-10-07 16:52:19 -07002240 mResetting = false;
Robert Shih0c61a0d2015-07-06 15:09:10 -07002241 mSourceStarted = false;
Hassan Shojaniacefac142017-02-06 21:02:02 -08002242
2243 // Modular DRM
2244 if (mCrypto != NULL) {
2245 // decoders will be flushed before this so their mCrypto would go away on their own
2246 // TODO change to ALOGV
2247 ALOGD("performReset mCrypto: %p (%d)", mCrypto.get(),
2248 (mCrypto != NULL ? mCrypto->getStrongCount() : 0));
2249 mCrypto.clear();
2250 }
2251 mIsDrmProtected = false;
Andreas Hubera1f8ab02012-11-30 10:53:22 -08002252}
2253
2254void NuPlayer::performScanSources() {
2255 ALOGV("performScanSources");
2256
Andreas Huber57a339c2012-12-03 11:18:00 -08002257 if (!mStarted) {
2258 return;
2259 }
2260
Andreas Hubera1f8ab02012-11-30 10:53:22 -08002261 if (mAudioDecoder == NULL || mVideoDecoder == NULL) {
2262 postScanSources();
2263 }
2264}
2265
Lajos Molnar1de1e252015-04-30 18:18:34 -07002266void NuPlayer::performSetSurface(const sp<Surface> &surface) {
Andreas Huber57a339c2012-12-03 11:18:00 -08002267 ALOGV("performSetSurface");
2268
Lajos Molnar1de1e252015-04-30 18:18:34 -07002269 mSurface = surface;
Andreas Huber57a339c2012-12-03 11:18:00 -08002270
2271 // XXX - ignore error from setVideoScalingMode for now
2272 setVideoScalingMode(mVideoScalingMode);
Chong Zhang13d6faa2014-08-22 15:35:28 -07002273
2274 if (mDriver != NULL) {
2275 sp<NuPlayerDriver> driver = mDriver.promote();
2276 if (driver != NULL) {
2277 driver->notifySetSurfaceComplete();
2278 }
2279 }
Andreas Huber57a339c2012-12-03 11:18:00 -08002280}
2281
Chong Zhangf8d71772014-11-26 15:08:34 -08002282void NuPlayer::performResumeDecoders(bool needNotify) {
2283 if (needNotify) {
2284 mResumePending = true;
2285 if (mVideoDecoder == NULL) {
2286 // if audio-only, we can notify seek complete now,
2287 // as the resume operation will be relatively fast.
2288 finishResume();
2289 }
2290 }
2291
Chong Zhang7137ec72014-11-12 16:41:05 -08002292 if (mVideoDecoder != NULL) {
Chong Zhangf8d71772014-11-26 15:08:34 -08002293 // When there is continuous seek, MediaPlayer will cache the seek
2294 // position, and send down new seek request when previous seek is
2295 // complete. Let's wait for at least one video output frame before
2296 // notifying seek complete, so that the video thumbnail gets updated
2297 // when seekbar is dragged.
2298 mVideoDecoder->signalResume(needNotify);
Chong Zhang7137ec72014-11-12 16:41:05 -08002299 }
2300
2301 if (mAudioDecoder != NULL) {
Chong Zhangf8d71772014-11-26 15:08:34 -08002302 mAudioDecoder->signalResume(false /* needNotify */);
2303 }
2304}
2305
2306void NuPlayer::finishResume() {
2307 if (mResumePending) {
2308 mResumePending = false;
Wei Jia1061c9c2015-05-19 16:02:17 -07002309 notifyDriverSeekComplete();
2310 }
2311}
2312
2313void NuPlayer::notifyDriverSeekComplete() {
2314 if (mDriver != NULL) {
2315 sp<NuPlayerDriver> driver = mDriver.promote();
2316 if (driver != NULL) {
2317 driver->notifySeekComplete();
Chong Zhangf8d71772014-11-26 15:08:34 -08002318 }
Chong Zhang7137ec72014-11-12 16:41:05 -08002319 }
2320}
2321
Andreas Huber9575c962013-02-05 13:59:56 -08002322void NuPlayer::onSourceNotify(const sp<AMessage> &msg) {
2323 int32_t what;
2324 CHECK(msg->findInt32("what", &what));
2325
2326 switch (what) {
Lajos Molnarfcd3e942015-03-31 10:06:48 -07002327 case Source::kWhatInstantiateSecureDecoders:
2328 {
2329 if (mSource == NULL) {
2330 // This is a stale notification from a source that was
2331 // asynchronously preparing when the client called reset().
2332 // We handled the reset, the source is gone.
2333 break;
2334 }
2335
2336 sp<AMessage> reply;
2337 CHECK(msg->findMessage("reply", &reply));
2338 status_t err = onInstantiateSecureDecoders();
2339 reply->setInt32("err", err);
2340 reply->post();
2341 break;
2342 }
2343
Andreas Huber9575c962013-02-05 13:59:56 -08002344 case Source::kWhatPrepared:
2345 {
Hassan Shojaniacefac142017-02-06 21:02:02 -08002346 ALOGV("NuPlayer::onSourceNotify Source::kWhatPrepared source: %p", mSource.get());
Andreas Huberb5f28d42013-04-25 15:11:19 -07002347 if (mSource == NULL) {
2348 // This is a stale notification from a source that was
2349 // asynchronously preparing when the client called reset().
2350 // We handled the reset, the source is gone.
2351 break;
2352 }
2353
Andreas Huberec0c5972013-02-05 14:47:13 -08002354 int32_t err;
2355 CHECK(msg->findInt32("err", &err));
2356
Lajos Molnarfcd3e942015-03-31 10:06:48 -07002357 if (err != OK) {
2358 // shut down potential secure codecs in case client never calls reset
2359 mDeferredActions.push_back(
2360 new FlushDecoderAction(FLUSH_CMD_SHUTDOWN /* audio */,
2361 FLUSH_CMD_SHUTDOWN /* video */));
2362 processDeferredActions();
Wei Jia8a092d32016-06-03 14:57:24 -07002363 } else {
2364 mPrepared = true;
Lajos Molnarfcd3e942015-03-31 10:06:48 -07002365 }
2366
Andreas Huber9575c962013-02-05 13:59:56 -08002367 sp<NuPlayerDriver> driver = mDriver.promote();
2368 if (driver != NULL) {
Marco Nelissendd114d12014-05-28 15:23:14 -07002369 // notify duration first, so that it's definitely set when
2370 // the app received the "prepare complete" callback.
2371 int64_t durationUs;
2372 if (mSource->getDuration(&durationUs) == OK) {
2373 driver->notifyDuration(durationUs);
2374 }
Andreas Huberec0c5972013-02-05 14:47:13 -08002375 driver->notifyPrepareCompleted(err);
Andreas Huber9575c962013-02-05 13:59:56 -08002376 }
Andreas Huber99759402013-04-01 14:28:31 -07002377
Andreas Huber9575c962013-02-05 13:59:56 -08002378 break;
2379 }
2380
Hassan Shojaniacefac142017-02-06 21:02:02 -08002381 // Modular DRM
2382 case Source::kWhatDrmInfo:
2383 {
2384 Parcel parcel;
2385 sp<ABuffer> drmInfo;
2386 CHECK(msg->findBuffer("drmInfo", &drmInfo));
2387 parcel.setData(drmInfo->data(), drmInfo->size());
2388
2389 ALOGV("onSourceNotify() kWhatDrmInfo MEDIA_DRM_INFO drmInfo: %p parcel size: %zu",
2390 drmInfo.get(), parcel.dataSize());
2391
2392 notifyListener(MEDIA_DRM_INFO, 0 /* ext1 */, 0 /* ext2 */, &parcel);
2393
2394 break;
2395 }
2396
Andreas Huber9575c962013-02-05 13:59:56 -08002397 case Source::kWhatFlagsChanged:
2398 {
2399 uint32_t flags;
2400 CHECK(msg->findInt32("flags", (int32_t *)&flags));
2401
Chong Zhang4b7069d2013-09-11 12:52:43 -07002402 sp<NuPlayerDriver> driver = mDriver.promote();
2403 if (driver != NULL) {
Hassan Shojaniacefac142017-02-06 21:02:02 -08002404
2405 ALOGV("onSourceNotify() kWhatFlagsChanged FLAG_CAN_PAUSE: %d "
2406 "FLAG_CAN_SEEK_BACKWARD: %d \n\t\t\t\t FLAG_CAN_SEEK_FORWARD: %d "
2407 "FLAG_CAN_SEEK: %d FLAG_DYNAMIC_DURATION: %d \n"
2408 "\t\t\t\t FLAG_SECURE: %d FLAG_PROTECTED: %d",
2409 (flags & Source::FLAG_CAN_PAUSE) != 0,
2410 (flags & Source::FLAG_CAN_SEEK_BACKWARD) != 0,
2411 (flags & Source::FLAG_CAN_SEEK_FORWARD) != 0,
2412 (flags & Source::FLAG_CAN_SEEK) != 0,
2413 (flags & Source::FLAG_DYNAMIC_DURATION) != 0,
2414 (flags & Source::FLAG_SECURE) != 0,
2415 (flags & Source::FLAG_PROTECTED) != 0);
2416
Wei Jia895651b2014-12-10 17:31:52 -08002417 if ((flags & NuPlayer::Source::FLAG_CAN_SEEK) == 0) {
2418 driver->notifyListener(
2419 MEDIA_INFO, MEDIA_INFO_NOT_SEEKABLE, 0);
2420 }
Chong Zhang4b7069d2013-09-11 12:52:43 -07002421 driver->notifyFlagsChanged(flags);
2422 }
2423
Andreas Huber9575c962013-02-05 13:59:56 -08002424 if ((mSourceFlags & Source::FLAG_DYNAMIC_DURATION)
2425 && (!(flags & Source::FLAG_DYNAMIC_DURATION))) {
2426 cancelPollDuration();
2427 } else if (!(mSourceFlags & Source::FLAG_DYNAMIC_DURATION)
2428 && (flags & Source::FLAG_DYNAMIC_DURATION)
2429 && (mAudioDecoder != NULL || mVideoDecoder != NULL)) {
2430 schedulePollDuration();
2431 }
2432
2433 mSourceFlags = flags;
2434 break;
2435 }
2436
2437 case Source::kWhatVideoSizeChanged:
2438 {
Chong Zhangced1c2f2014-08-08 15:22:35 -07002439 sp<AMessage> format;
2440 CHECK(msg->findMessage("format", &format));
Andreas Huber9575c962013-02-05 13:59:56 -08002441
Chong Zhangced1c2f2014-08-08 15:22:35 -07002442 updateVideoSize(format);
Andreas Huber9575c962013-02-05 13:59:56 -08002443 break;
2444 }
2445
Chong Zhang2a3cc9a2014-08-21 17:48:26 -07002446 case Source::kWhatBufferingUpdate:
2447 {
2448 int32_t percentage;
2449 CHECK(msg->findInt32("percentage", &percentage));
2450
2451 notifyListener(MEDIA_BUFFERING_UPDATE, percentage, 0);
2452 break;
2453 }
2454
Chong Zhangefbb6192015-01-30 17:13:27 -08002455 case Source::kWhatPauseOnBufferingStart:
2456 {
2457 // ignore if not playing
Chong Zhang8a048332015-05-06 15:16:28 -07002458 if (mStarted) {
Chong Zhangefbb6192015-01-30 17:13:27 -08002459 ALOGI("buffer low, pausing...");
2460
Chong Zhang8a048332015-05-06 15:16:28 -07002461 mPausedForBuffering = true;
Chong Zhangefbb6192015-01-30 17:13:27 -08002462 onPause();
2463 }
Wei Jiabfe82072016-05-20 09:21:50 -07002464 notifyListener(MEDIA_INFO, MEDIA_INFO_BUFFERING_START, 0);
Roger Jönssonb50e83e2013-01-21 16:26:41 +01002465 break;
2466 }
2467
Chong Zhangefbb6192015-01-30 17:13:27 -08002468 case Source::kWhatResumeOnBufferingEnd:
2469 {
2470 // ignore if not playing
Chong Zhang8a048332015-05-06 15:16:28 -07002471 if (mStarted) {
Chong Zhangefbb6192015-01-30 17:13:27 -08002472 ALOGI("buffer ready, resuming...");
2473
Chong Zhang8a048332015-05-06 15:16:28 -07002474 mPausedForBuffering = false;
2475
2476 // do not resume yet if client didn't unpause
2477 if (!mPausedByClient) {
2478 onResume();
2479 }
Chong Zhangefbb6192015-01-30 17:13:27 -08002480 }
Wei Jia3bed45a2016-02-17 11:06:47 -08002481 notifyListener(MEDIA_INFO, MEDIA_INFO_BUFFERING_END, 0);
Roger Jönssonb50e83e2013-01-21 16:26:41 +01002482 break;
2483 }
2484
Chong Zhangefbb6192015-01-30 17:13:27 -08002485 case Source::kWhatCacheStats:
2486 {
2487 int32_t kbps;
2488 CHECK(msg->findInt32("bandwidth", &kbps));
2489
2490 notifyListener(MEDIA_INFO, MEDIA_INFO_NETWORK_BANDWIDTH, kbps);
2491 break;
2492 }
2493
Chong Zhangdcb89b32013-08-06 09:44:47 -07002494 case Source::kWhatSubtitleData:
2495 {
2496 sp<ABuffer> buffer;
2497 CHECK(msg->findBuffer("buffer", &buffer));
2498
Chong Zhang404fced2014-06-11 14:45:31 -07002499 sendSubtitleData(buffer, 0 /* baseIndex */);
Chong Zhangdcb89b32013-08-06 09:44:47 -07002500 break;
2501 }
2502
Robert Shih08528432015-04-08 09:06:54 -07002503 case Source::kWhatTimedMetaData:
2504 {
2505 sp<ABuffer> buffer;
2506 if (!msg->findBuffer("buffer", &buffer)) {
2507 notifyListener(MEDIA_INFO, MEDIA_INFO_METADATA_UPDATE, 0);
2508 } else {
2509 sendTimedMetaData(buffer);
2510 }
2511 break;
2512 }
2513
Robert Shihd3b0bbb2014-07-23 15:00:25 -07002514 case Source::kWhatTimedTextData:
2515 {
2516 int32_t generation;
2517 if (msg->findInt32("generation", &generation)
2518 && generation != mTimedTextGeneration) {
2519 break;
2520 }
2521
2522 sp<ABuffer> buffer;
2523 CHECK(msg->findBuffer("buffer", &buffer));
2524
2525 sp<NuPlayerDriver> driver = mDriver.promote();
2526 if (driver == NULL) {
2527 break;
2528 }
2529
2530 int posMs;
2531 int64_t timeUs, posUs;
2532 driver->getCurrentPosition(&posMs);
Patrik2 Carlsson24d484b2015-01-27 16:49:45 +01002533 posUs = (int64_t) posMs * 1000ll;
Robert Shihd3b0bbb2014-07-23 15:00:25 -07002534 CHECK(buffer->meta()->findInt64("timeUs", &timeUs));
2535
2536 if (posUs < timeUs) {
2537 if (!msg->findInt32("generation", &generation)) {
2538 msg->setInt32("generation", mTimedTextGeneration);
2539 }
2540 msg->post(timeUs - posUs);
2541 } else {
2542 sendTimedTextData(buffer);
2543 }
2544 break;
2545 }
2546
Andreas Huber14f76722013-01-15 09:04:18 -08002547 case Source::kWhatQueueDecoderShutdown:
2548 {
2549 int32_t audio, video;
2550 CHECK(msg->findInt32("audio", &audio));
2551 CHECK(msg->findInt32("video", &video));
2552
2553 sp<AMessage> reply;
2554 CHECK(msg->findMessage("reply", &reply));
2555
2556 queueDecoderShutdown(audio, video, reply);
2557 break;
2558 }
2559
Ronghua Wu80276872014-08-28 15:50:29 -07002560 case Source::kWhatDrmNoLicense:
2561 {
2562 notifyListener(MEDIA_ERROR, MEDIA_ERROR_UNKNOWN, ERROR_DRM_NO_LICENSE);
2563 break;
2564 }
2565
Andreas Huber9575c962013-02-05 13:59:56 -08002566 default:
2567 TRESPASS();
2568 }
2569}
2570
Chong Zhanga7fa1d92014-06-11 14:49:23 -07002571void NuPlayer::onClosedCaptionNotify(const sp<AMessage> &msg) {
2572 int32_t what;
2573 CHECK(msg->findInt32("what", &what));
2574
2575 switch (what) {
2576 case NuPlayer::CCDecoder::kWhatClosedCaptionData:
2577 {
2578 sp<ABuffer> buffer;
2579 CHECK(msg->findBuffer("buffer", &buffer));
2580
2581 size_t inbandTracks = 0;
2582 if (mSource != NULL) {
2583 inbandTracks = mSource->getTrackCount();
2584 }
2585
2586 sendSubtitleData(buffer, inbandTracks);
2587 break;
2588 }
2589
2590 case NuPlayer::CCDecoder::kWhatTrackAdded:
2591 {
2592 notifyListener(MEDIA_INFO, MEDIA_INFO_METADATA_UPDATE, 0);
2593
2594 break;
2595 }
2596
2597 default:
2598 TRESPASS();
2599 }
2600
2601
2602}
2603
Chong Zhang404fced2014-06-11 14:45:31 -07002604void NuPlayer::sendSubtitleData(const sp<ABuffer> &buffer, int32_t baseIndex) {
2605 int32_t trackIndex;
2606 int64_t timeUs, durationUs;
2607 CHECK(buffer->meta()->findInt32("trackIndex", &trackIndex));
2608 CHECK(buffer->meta()->findInt64("timeUs", &timeUs));
2609 CHECK(buffer->meta()->findInt64("durationUs", &durationUs));
2610
2611 Parcel in;
2612 in.writeInt32(trackIndex + baseIndex);
2613 in.writeInt64(timeUs);
2614 in.writeInt64(durationUs);
2615 in.writeInt32(buffer->size());
2616 in.writeInt32(buffer->size());
2617 in.write(buffer->data(), buffer->size());
2618
2619 notifyListener(MEDIA_SUBTITLE_DATA, 0, 0, &in);
2620}
Robert Shihd3b0bbb2014-07-23 15:00:25 -07002621
Robert Shih08528432015-04-08 09:06:54 -07002622void NuPlayer::sendTimedMetaData(const sp<ABuffer> &buffer) {
2623 int64_t timeUs;
2624 CHECK(buffer->meta()->findInt64("timeUs", &timeUs));
2625
2626 Parcel in;
2627 in.writeInt64(timeUs);
2628 in.writeInt32(buffer->size());
2629 in.writeInt32(buffer->size());
2630 in.write(buffer->data(), buffer->size());
2631
2632 notifyListener(MEDIA_META_DATA, 0, 0, &in);
2633}
2634
Robert Shihd3b0bbb2014-07-23 15:00:25 -07002635void NuPlayer::sendTimedTextData(const sp<ABuffer> &buffer) {
2636 const void *data;
2637 size_t size = 0;
2638 int64_t timeUs;
Marco Nelissen55e2f4c2015-09-04 15:57:15 -07002639 int32_t flag = TextDescriptions::IN_BAND_TEXT_3GPP;
Robert Shihd3b0bbb2014-07-23 15:00:25 -07002640
2641 AString mime;
2642 CHECK(buffer->meta()->findString("mime", &mime));
2643 CHECK(strcasecmp(mime.c_str(), MEDIA_MIMETYPE_TEXT_3GPP) == 0);
2644
2645 data = buffer->data();
2646 size = buffer->size();
2647
2648 Parcel parcel;
2649 if (size > 0) {
2650 CHECK(buffer->meta()->findInt64("timeUs", &timeUs));
Marco Nelissen55e2f4c2015-09-04 15:57:15 -07002651 int32_t global = 0;
2652 if (buffer->meta()->findInt32("global", &global) && global) {
2653 flag |= TextDescriptions::GLOBAL_DESCRIPTIONS;
2654 } else {
2655 flag |= TextDescriptions::LOCAL_DESCRIPTIONS;
2656 }
Robert Shihd3b0bbb2014-07-23 15:00:25 -07002657 TextDescriptions::getParcelOfDescriptions(
2658 (const uint8_t *)data, size, flag, timeUs / 1000, &parcel);
2659 }
2660
2661 if ((parcel.dataSize() > 0)) {
2662 notifyListener(MEDIA_TIMED_TEXT, 0, 0, &parcel);
2663 } else { // send an empty timed text
2664 notifyListener(MEDIA_TIMED_TEXT, 0, 0);
2665 }
2666}
Hassan Shojaniacefac142017-02-06 21:02:02 -08002667
Hassan Shojaniaff63de72017-04-26 15:10:42 -07002668const char *NuPlayer::getDataSourceType() {
2669 switch (mDataSourceType) {
2670 case DATA_SOURCE_TYPE_HTTP_LIVE:
2671 return "HTTPLive";
2672
2673 case DATA_SOURCE_TYPE_RTSP:
2674 return "RTSP";
2675
2676 case DATA_SOURCE_TYPE_GENERIC_URL:
2677 return "GenURL";
2678
2679 case DATA_SOURCE_TYPE_GENERIC_FD:
2680 return "GenFD";
2681
2682 case DATA_SOURCE_TYPE_MEDIA:
2683 return "Media";
2684
2685 case DATA_SOURCE_TYPE_STREAM:
2686 return "Stream";
2687
2688 case DATA_SOURCE_TYPE_NONE:
2689 default:
2690 return "None";
2691 }
2692 }
2693
Hassan Shojaniacefac142017-02-06 21:02:02 -08002694// Modular DRM begin
2695status_t NuPlayer::prepareDrm(const uint8_t uuid[16], const Vector<uint8_t> &drmSessionId)
2696{
2697 ALOGV("prepareDrm ");
2698
2699 // Passing to the looper anyway; called in a pre-config prepared state so no race on mCrypto
2700 sp<AMessage> msg = new AMessage(kWhatPrepareDrm, this);
2701 // synchronous call so just passing the address but with local copies of "const" args
2702 uint8_t UUID[16];
2703 memcpy(UUID, uuid, sizeof(UUID));
2704 Vector<uint8_t> sessionId = drmSessionId;
2705 msg->setPointer("uuid", (void*)UUID);
2706 msg->setPointer("drmSessionId", (void*)&sessionId);
2707
2708 sp<AMessage> response;
2709 status_t status = msg->postAndAwaitResponse(&response);
2710
2711 if (status == OK && response != NULL) {
2712 CHECK(response->findInt32("status", &status));
2713 ALOGV("prepareDrm ret: %d ", status);
2714 } else {
2715 ALOGE("prepareDrm err: %d", status);
2716 }
2717
2718 return status;
2719}
2720
2721status_t NuPlayer::releaseDrm()
2722{
2723 ALOGV("releaseDrm ");
2724
2725 sp<AMessage> msg = new AMessage(kWhatReleaseDrm, this);
2726
2727 sp<AMessage> response;
2728 status_t status = msg->postAndAwaitResponse(&response);
2729
2730 if (status == OK && response != NULL) {
2731 CHECK(response->findInt32("status", &status));
2732 ALOGV("releaseDrm ret: %d ", status);
2733 } else {
2734 ALOGE("releaseDrm err: %d", status);
2735 }
2736
2737 return status;
2738}
2739
2740status_t NuPlayer::onPrepareDrm(const sp<AMessage> &msg)
2741{
2742 // TODO change to ALOGV
2743 ALOGD("onPrepareDrm ");
2744
2745 status_t status = INVALID_OPERATION;
2746 if (mSource == NULL) {
2747 ALOGE("onPrepareDrm: No source. onPrepareDrm failed with %d.", status);
2748 return status;
2749 }
2750
2751 uint8_t *uuid;
2752 Vector<uint8_t> *drmSessionId;
2753 CHECK(msg->findPointer("uuid", (void**)&uuid));
2754 CHECK(msg->findPointer("drmSessionId", (void**)&drmSessionId));
2755
2756 status = OK;
2757 sp<ICrypto> crypto = NULL;
2758
2759 status = mSource->prepareDrm(uuid, *drmSessionId, &crypto);
2760 if (crypto == NULL) {
2761 ALOGE("onPrepareDrm: mSource->prepareDrm failed. status: %d", status);
2762 return status;
2763 }
2764 ALOGV("onPrepareDrm: mSource->prepareDrm succeeded");
2765
2766 if (mCrypto != NULL) {
2767 ALOGE("onPrepareDrm: Unexpected. Already having mCrypto: %p (%d)",
2768 mCrypto.get(), mCrypto->getStrongCount());
2769 mCrypto.clear();
2770 }
2771
2772 mCrypto = crypto;
2773 mIsDrmProtected = true;
2774 // TODO change to ALOGV
2775 ALOGD("onPrepareDrm: mCrypto: %p (%d)", mCrypto.get(),
2776 (mCrypto != NULL ? mCrypto->getStrongCount() : 0));
2777
2778 return status;
2779}
2780
2781status_t NuPlayer::onReleaseDrm()
2782{
2783 // TODO change to ALOGV
2784 ALOGD("onReleaseDrm ");
2785
Hassan Shojania50b20c92017-02-16 18:28:58 -08002786 if (!mIsDrmProtected) {
2787 ALOGW("onReleaseDrm: Unexpected. mIsDrmProtected is already false.");
2788 }
2789
2790 mIsDrmProtected = false;
Hassan Shojaniacefac142017-02-06 21:02:02 -08002791
2792 status_t status;
2793 if (mCrypto != NULL) {
Hassan Shojania355e8472017-05-12 10:33:16 -07002794 // notifying the source first before removing crypto from codec
2795 if (mSource != NULL) {
2796 mSource->releaseDrm();
2797 }
2798
Hassan Shojaniacefac142017-02-06 21:02:02 -08002799 status=OK;
2800 // first making sure the codecs have released their crypto reference
2801 const sp<DecoderBase> &videoDecoder = getDecoder(false/*audio*/);
2802 if (videoDecoder != NULL) {
2803 status = videoDecoder->releaseCrypto();
2804 ALOGV("onReleaseDrm: video decoder ret: %d", status);
2805 }
2806
2807 const sp<DecoderBase> &audioDecoder = getDecoder(true/*audio*/);
2808 if (audioDecoder != NULL) {
2809 status_t status_audio = audioDecoder->releaseCrypto();
2810 if (status == OK) { // otherwise, returning the first error
2811 status = status_audio;
2812 }
2813 ALOGV("onReleaseDrm: audio decoder ret: %d", status_audio);
2814 }
2815
2816 // TODO change to ALOGV
2817 ALOGD("onReleaseDrm: mCrypto: %p (%d)", mCrypto.get(),
2818 (mCrypto != NULL ? mCrypto->getStrongCount() : 0));
2819 mCrypto.clear();
2820 } else { // mCrypto == NULL
2821 ALOGE("onReleaseDrm: Unexpected. There is no crypto.");
2822 status = INVALID_OPERATION;
2823 }
2824
2825 return status;
2826}
2827// Modular DRM end
Andreas Huberb5f25f02013-02-05 10:14:26 -08002828////////////////////////////////////////////////////////////////////////////////
2829
Chong Zhangced1c2f2014-08-08 15:22:35 -07002830sp<AMessage> NuPlayer::Source::getFormat(bool audio) {
2831 sp<MetaData> meta = getFormatMeta(audio);
2832
2833 if (meta == NULL) {
2834 return NULL;
2835 }
2836
2837 sp<AMessage> msg = new AMessage;
2838
2839 if(convertMetaDataToMessage(meta, &msg) == OK) {
2840 return msg;
2841 }
2842 return NULL;
2843}
2844
Andreas Huber9575c962013-02-05 13:59:56 -08002845void NuPlayer::Source::notifyFlagsChanged(uint32_t flags) {
2846 sp<AMessage> notify = dupNotify();
2847 notify->setInt32("what", kWhatFlagsChanged);
2848 notify->setInt32("flags", flags);
2849 notify->post();
2850}
2851
Chong Zhangced1c2f2014-08-08 15:22:35 -07002852void NuPlayer::Source::notifyVideoSizeChanged(const sp<AMessage> &format) {
Andreas Huber9575c962013-02-05 13:59:56 -08002853 sp<AMessage> notify = dupNotify();
2854 notify->setInt32("what", kWhatVideoSizeChanged);
Chong Zhangced1c2f2014-08-08 15:22:35 -07002855 notify->setMessage("format", format);
Andreas Huber9575c962013-02-05 13:59:56 -08002856 notify->post();
2857}
2858
Andreas Huberec0c5972013-02-05 14:47:13 -08002859void NuPlayer::Source::notifyPrepared(status_t err) {
Hassan Shojaniacefac142017-02-06 21:02:02 -08002860 ALOGV("Source::notifyPrepared %d", err);
Andreas Huber9575c962013-02-05 13:59:56 -08002861 sp<AMessage> notify = dupNotify();
2862 notify->setInt32("what", kWhatPrepared);
Andreas Huberec0c5972013-02-05 14:47:13 -08002863 notify->setInt32("err", err);
Andreas Huber9575c962013-02-05 13:59:56 -08002864 notify->post();
2865}
2866
Hassan Shojaniacefac142017-02-06 21:02:02 -08002867void NuPlayer::Source::notifyDrmInfo(const sp<ABuffer> &drmInfoBuffer)
2868{
2869 ALOGV("Source::notifyDrmInfo");
2870
2871 sp<AMessage> notify = dupNotify();
2872 notify->setInt32("what", kWhatDrmInfo);
2873 notify->setBuffer("drmInfo", drmInfoBuffer);
2874
2875 notify->post();
2876}
2877
Lajos Molnarfcd3e942015-03-31 10:06:48 -07002878void NuPlayer::Source::notifyInstantiateSecureDecoders(const sp<AMessage> &reply) {
2879 sp<AMessage> notify = dupNotify();
2880 notify->setInt32("what", kWhatInstantiateSecureDecoders);
2881 notify->setMessage("reply", reply);
2882 notify->post();
2883}
2884
Andreas Huber84333e02014-02-07 15:36:10 -08002885void NuPlayer::Source::onMessageReceived(const sp<AMessage> & /* msg */) {
Andreas Huberb5f25f02013-02-05 10:14:26 -08002886 TRESPASS();
2887}
2888
Andreas Huberf9334412010-12-15 15:17:42 -08002889} // namespace android