blob: df360462d6d0882d65cb7ee43220f2f645e87859 [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>
Andreas Huber3fe62152011-09-16 15:09:22 -070052#include <media/stagefright/MediaDefs.h>
Andreas Huberf9334412010-12-15 15:17:42 -080053#include <media/stagefright/MediaErrors.h>
54#include <media/stagefright/MetaData.h>
Lajos Molnar1de1e252015-04-30 18:18:34 -070055
Andy McFadden8ba01022012-12-18 09:46:54 -080056#include <gui/IGraphicBufferProducer.h>
Lajos Molnar1de1e252015-04-30 18:18:34 -070057#include <gui/Surface.h>
Andreas Huberf9334412010-12-15 15:17:42 -080058
Andreas Huber3fe62152011-09-16 15:09:22 -070059#include "avc_utils.h"
60
Andreas Huber84066782011-08-16 09:34:26 -070061#include "ESDS.h"
62#include <media/stagefright/Utils.h>
63
Andreas Huberf9334412010-12-15 15:17:42 -080064namespace android {
65
Andreas Hubera1f8ab02012-11-30 10:53:22 -080066struct NuPlayer::Action : public RefBase {
67 Action() {}
68
69 virtual void execute(NuPlayer *player) = 0;
70
71private:
72 DISALLOW_EVIL_CONSTRUCTORS(Action);
73};
74
75struct NuPlayer::SeekAction : public Action {
Wei Jiac5de0912016-11-18 10:22:14 -080076 explicit SeekAction(int64_t seekTimeUs, MediaPlayerSeekMode mode)
Wei Jia14486822016-11-02 17:51:30 -070077 : mSeekTimeUs(seekTimeUs),
Wei Jiac5de0912016-11-18 10:22:14 -080078 mMode(mode) {
Andreas Hubera1f8ab02012-11-30 10:53:22 -080079 }
80
81 virtual void execute(NuPlayer *player) {
Wei Jiac5de0912016-11-18 10:22:14 -080082 player->performSeek(mSeekTimeUs, mMode);
Andreas Hubera1f8ab02012-11-30 10:53:22 -080083 }
84
85private:
86 int64_t mSeekTimeUs;
Wei Jiac5de0912016-11-18 10:22:14 -080087 MediaPlayerSeekMode mMode;
Andreas Hubera1f8ab02012-11-30 10:53:22 -080088
89 DISALLOW_EVIL_CONSTRUCTORS(SeekAction);
90};
91
Chong Zhangf8d71772014-11-26 15:08:34 -080092struct NuPlayer::ResumeDecoderAction : public Action {
Chih-Hung Hsieh090ef602016-04-27 10:39:54 -070093 explicit ResumeDecoderAction(bool needNotify)
Chong Zhangf8d71772014-11-26 15:08:34 -080094 : mNeedNotify(needNotify) {
95 }
96
97 virtual void execute(NuPlayer *player) {
98 player->performResumeDecoders(mNeedNotify);
99 }
100
101private:
102 bool mNeedNotify;
103
104 DISALLOW_EVIL_CONSTRUCTORS(ResumeDecoderAction);
105};
106
Andreas Huber57a339c2012-12-03 11:18:00 -0800107struct NuPlayer::SetSurfaceAction : public Action {
Chih-Hung Hsieh090ef602016-04-27 10:39:54 -0700108 explicit SetSurfaceAction(const sp<Surface> &surface)
Lajos Molnar1de1e252015-04-30 18:18:34 -0700109 : mSurface(surface) {
Andreas Huber57a339c2012-12-03 11:18:00 -0800110 }
111
112 virtual void execute(NuPlayer *player) {
Lajos Molnar1de1e252015-04-30 18:18:34 -0700113 player->performSetSurface(mSurface);
Andreas Huber57a339c2012-12-03 11:18:00 -0800114 }
115
116private:
Lajos Molnar1de1e252015-04-30 18:18:34 -0700117 sp<Surface> mSurface;
Andreas Huber57a339c2012-12-03 11:18:00 -0800118
119 DISALLOW_EVIL_CONSTRUCTORS(SetSurfaceAction);
120};
121
Wei Jiafef808d2014-10-31 17:57:05 -0700122struct NuPlayer::FlushDecoderAction : public Action {
123 FlushDecoderAction(FlushCommand audio, FlushCommand video)
Andreas Huber14f76722013-01-15 09:04:18 -0800124 : mAudio(audio),
125 mVideo(video) {
126 }
127
128 virtual void execute(NuPlayer *player) {
Wei Jiafef808d2014-10-31 17:57:05 -0700129 player->performDecoderFlush(mAudio, mVideo);
Andreas Huber14f76722013-01-15 09:04:18 -0800130 }
131
132private:
Wei Jiafef808d2014-10-31 17:57:05 -0700133 FlushCommand mAudio;
134 FlushCommand mVideo;
Andreas Huber14f76722013-01-15 09:04:18 -0800135
Wei Jiafef808d2014-10-31 17:57:05 -0700136 DISALLOW_EVIL_CONSTRUCTORS(FlushDecoderAction);
Andreas Huber14f76722013-01-15 09:04:18 -0800137};
138
139struct NuPlayer::PostMessageAction : public Action {
Chih-Hung Hsieh090ef602016-04-27 10:39:54 -0700140 explicit PostMessageAction(const sp<AMessage> &msg)
Andreas Huber14f76722013-01-15 09:04:18 -0800141 : mMessage(msg) {
142 }
143
144 virtual void execute(NuPlayer *) {
145 mMessage->post();
146 }
147
148private:
149 sp<AMessage> mMessage;
150
151 DISALLOW_EVIL_CONSTRUCTORS(PostMessageAction);
152};
153
Andreas Hubera1f8ab02012-11-30 10:53:22 -0800154// Use this if there's no state necessary to save in order to execute
155// the action.
156struct NuPlayer::SimpleAction : public Action {
157 typedef void (NuPlayer::*ActionFunc)();
158
Chih-Hung Hsieh090ef602016-04-27 10:39:54 -0700159 explicit SimpleAction(ActionFunc func)
Andreas Hubera1f8ab02012-11-30 10:53:22 -0800160 : mFunc(func) {
161 }
162
163 virtual void execute(NuPlayer *player) {
164 (player->*mFunc)();
165 }
166
167private:
168 ActionFunc mFunc;
169
170 DISALLOW_EVIL_CONSTRUCTORS(SimpleAction);
171};
172
Andreas Huberf9334412010-12-15 15:17:42 -0800173////////////////////////////////////////////////////////////////////////////////
174
Ronghua Wu68845c12015-07-21 09:50:48 -0700175NuPlayer::NuPlayer(pid_t pid)
Andreas Huber9b80c2b2011-06-30 15:47:02 -0700176 : mUIDValid(false),
Ronghua Wu68845c12015-07-21 09:50:48 -0700177 mPID(pid),
Andreas Huber9575c962013-02-05 13:59:56 -0800178 mSourceFlags(0),
Wei Jiabc2fb722014-07-08 16:37:57 -0700179 mOffloadAudio(false),
Wei Jia88703c32014-08-06 11:24:07 -0700180 mAudioDecoderGeneration(0),
181 mVideoDecoderGeneration(0),
Wei Jia57568df2014-09-22 10:16:29 -0700182 mRendererGeneration(0),
Robert Shih1a5c8592015-08-04 18:07:44 -0700183 mPreviousSeekTimeUs(0),
Andreas Huber9b80c2b2011-06-30 15:47:02 -0700184 mAudioEOS(false),
Andreas Huberf9334412010-12-15 15:17:42 -0800185 mVideoEOS(false),
Andreas Huber5bc087c2010-12-23 10:27:40 -0800186 mScanSourcesPending(false),
Andreas Huber1aef2112011-01-04 14:01:29 -0800187 mScanSourcesGeneration(0),
Andreas Huberb7c8e912012-11-27 15:02:53 -0800188 mPollDurationGeneration(0),
Robert Shihd3b0bbb2014-07-23 15:00:25 -0700189 mTimedTextGeneration(0),
Andreas Huberf9334412010-12-15 15:17:42 -0800190 mFlushingAudio(NONE),
Andreas Huber1aef2112011-01-04 14:01:29 -0800191 mFlushingVideo(NONE),
Chong Zhangf8d71772014-11-26 15:08:34 -0800192 mResumePending(false),
Andreas Huber57a339c2012-12-03 11:18:00 -0800193 mVideoScalingMode(NATIVE_WINDOW_SCALING_MODE_SCALE_TO_WINDOW),
Lajos Molnar3a474aa2015-04-24 17:10:07 -0700194 mPlaybackSettings(AUDIO_PLAYBACK_RATE_DEFAULT),
195 mVideoFpsHint(-1.f),
Chong Zhangefbb6192015-01-30 17:13:27 -0800196 mStarted(false),
Wei Jia8a092d32016-06-03 14:57:24 -0700197 mPrepared(false),
Ronghua Wu64c2d172015-10-07 16:52:19 -0700198 mResetting(false),
Robert Shih0c61a0d2015-07-06 15:09:10 -0700199 mSourceStarted(false),
Wei Jia686e8e52017-04-03 14:08:01 -0700200 mAudioDecoderError(false),
201 mVideoDecoderError(false),
Chong Zhangefbb6192015-01-30 17:13:27 -0800202 mPaused(false),
Wei Jia71c75e02016-02-04 09:40:47 -0800203 mPausedByClient(true),
Hassan Shojania50b20c92017-02-16 18:28:58 -0800204 mPausedForBuffering(false),
Hassan Shojaniaff63de72017-04-26 15:10:42 -0700205 mIsDrmProtected(false),
206 mDataSourceType(DATA_SOURCE_TYPE_NONE) {
Andy Hung8d121d42014-10-03 09:53:53 -0700207 clearFlushComplete();
Andreas Huberf9334412010-12-15 15:17:42 -0800208}
209
210NuPlayer::~NuPlayer() {
211}
212
Andreas Huber9b80c2b2011-06-30 15:47:02 -0700213void NuPlayer::setUID(uid_t uid) {
214 mUIDValid = true;
215 mUID = uid;
216}
217
Andreas Huber43c3e6c2011-01-05 12:17:08 -0800218void NuPlayer::setDriver(const wp<NuPlayerDriver> &driver) {
219 mDriver = driver;
Andreas Huberf9334412010-12-15 15:17:42 -0800220}
221
Andreas Huber9575c962013-02-05 13:59:56 -0800222void NuPlayer::setDataSourceAsync(const sp<IStreamSource> &source) {
Lajos Molnar1d15ab52015-03-04 16:46:34 -0800223 sp<AMessage> msg = new AMessage(kWhatSetDataSource, this);
Andreas Huberf9334412010-12-15 15:17:42 -0800224
Lajos Molnar1d15ab52015-03-04 16:46:34 -0800225 sp<AMessage> notify = new AMessage(kWhatSourceNotify, this);
Andreas Huberb5f25f02013-02-05 10:14:26 -0800226
Andreas Huber240abcc2014-02-13 13:32:37 -0800227 msg->setObject("source", new StreamingSource(notify, source));
Andreas Huber5bc087c2010-12-23 10:27:40 -0800228 msg->post();
Hassan Shojaniaff63de72017-04-26 15:10:42 -0700229 mDataSourceType = DATA_SOURCE_TYPE_STREAM;
Andreas Huber5bc087c2010-12-23 10:27:40 -0800230}
Andreas Huberf9334412010-12-15 15:17:42 -0800231
Andreas Huberafed0e12011-09-20 15:39:58 -0700232static bool IsHTTPLiveURL(const char *url) {
233 if (!strncasecmp("http://", url, 7)
Andreas Huber99759402013-04-01 14:28:31 -0700234 || !strncasecmp("https://", url, 8)
235 || !strncasecmp("file://", url, 7)) {
Andreas Huberafed0e12011-09-20 15:39:58 -0700236 size_t len = strlen(url);
237 if (len >= 5 && !strcasecmp(".m3u8", &url[len - 5])) {
238 return true;
239 }
240
241 if (strstr(url,"m3u8")) {
242 return true;
243 }
244 }
245
246 return false;
247}
248
Andreas Huber9575c962013-02-05 13:59:56 -0800249void NuPlayer::setDataSourceAsync(
Andreas Huber1b86fe02014-01-29 11:13:26 -0800250 const sp<IMediaHTTPService> &httpService,
251 const char *url,
252 const KeyedVector<String8, String8> *headers) {
Chong Zhang3de157d2014-08-05 20:54:44 -0700253
Lajos Molnar1d15ab52015-03-04 16:46:34 -0800254 sp<AMessage> msg = new AMessage(kWhatSetDataSource, this);
Oscar Rydhé7a33b772012-02-20 10:15:48 +0100255 size_t len = strlen(url);
Andreas Huber5bc087c2010-12-23 10:27:40 -0800256
Lajos Molnar1d15ab52015-03-04 16:46:34 -0800257 sp<AMessage> notify = new AMessage(kWhatSourceNotify, this);
Andreas Huberb5f25f02013-02-05 10:14:26 -0800258
Andreas Huberafed0e12011-09-20 15:39:58 -0700259 sp<Source> source;
260 if (IsHTTPLiveURL(url)) {
Andreas Huber81e68442014-02-05 11:52:33 -0800261 source = new HTTPLiveSource(notify, httpService, url, headers);
Hassan Shojaniacefac142017-02-06 21:02:02 -0800262 ALOGV("setDataSourceAsync HTTPLiveSource %s", url);
Hassan Shojaniaff63de72017-04-26 15:10:42 -0700263 mDataSourceType = DATA_SOURCE_TYPE_HTTP_LIVE;
Andreas Huberafed0e12011-09-20 15:39:58 -0700264 } else if (!strncasecmp(url, "rtsp://", 7)) {
Andreas Huber1b86fe02014-01-29 11:13:26 -0800265 source = new RTSPSource(
266 notify, httpService, url, headers, mUIDValid, mUID);
Hassan Shojaniacefac142017-02-06 21:02:02 -0800267 ALOGV("setDataSourceAsync RTSPSource %s", url);
Hassan Shojaniaff63de72017-04-26 15:10:42 -0700268 mDataSourceType = DATA_SOURCE_TYPE_RTSP;
Oscar Rydhé7a33b772012-02-20 10:15:48 +0100269 } else if ((!strncasecmp(url, "http://", 7)
270 || !strncasecmp(url, "https://", 8))
271 && ((len >= 4 && !strcasecmp(".sdp", &url[len - 4]))
272 || strstr(url, ".sdp?"))) {
Andreas Huber1b86fe02014-01-29 11:13:26 -0800273 source = new RTSPSource(
274 notify, httpService, url, headers, mUIDValid, mUID, true);
Hassan Shojaniacefac142017-02-06 21:02:02 -0800275 ALOGV("setDataSourceAsync RTSPSource http/https/.sdp %s", url);
Hassan Shojaniaff63de72017-04-26 15:10:42 -0700276 mDataSourceType = DATA_SOURCE_TYPE_RTSP;
Andreas Huber2bfdd422011-10-11 15:24:07 -0700277 } else {
Hassan Shojaniacefac142017-02-06 21:02:02 -0800278 ALOGV("setDataSourceAsync GenericSource %s", url);
279
Chong Zhang3de157d2014-08-05 20:54:44 -0700280 sp<GenericSource> genericSource =
281 new GenericSource(notify, mUIDValid, mUID);
Andreas Huber2bfdd422011-10-11 15:24:07 -0700282
Chong Zhanga19f33e2014-08-07 15:35:07 -0700283 status_t err = genericSource->setDataSource(httpService, url, headers);
Chong Zhang3de157d2014-08-05 20:54:44 -0700284
285 if (err == OK) {
286 source = genericSource;
287 } else {
Chong Zhanga19f33e2014-08-07 15:35:07 -0700288 ALOGE("Failed to set data source!");
Chong Zhang3de157d2014-08-05 20:54:44 -0700289 }
Hassan Shojaniaff63de72017-04-26 15:10:42 -0700290
291 // regardless of success/failure
292 mDataSourceType = DATA_SOURCE_TYPE_GENERIC_URL;
Chong Zhang3de157d2014-08-05 20:54:44 -0700293 }
Andreas Huberafed0e12011-09-20 15:39:58 -0700294 msg->setObject("source", source);
295 msg->post();
296}
297
Andreas Huber9575c962013-02-05 13:59:56 -0800298void NuPlayer::setDataSourceAsync(int fd, int64_t offset, int64_t length) {
Lajos Molnar1d15ab52015-03-04 16:46:34 -0800299 sp<AMessage> msg = new AMessage(kWhatSetDataSource, this);
Andreas Huberafed0e12011-09-20 15:39:58 -0700300
Lajos Molnar1d15ab52015-03-04 16:46:34 -0800301 sp<AMessage> notify = new AMessage(kWhatSourceNotify, this);
Andreas Huberb5f25f02013-02-05 10:14:26 -0800302
Chong Zhang3de157d2014-08-05 20:54:44 -0700303 sp<GenericSource> source =
304 new GenericSource(notify, mUIDValid, mUID);
305
Hassan Shojaniacefac142017-02-06 21:02:02 -0800306 ALOGV("setDataSourceAsync fd %d/%lld/%lld source: %p",
307 fd, (long long)offset, (long long)length, source.get());
308
Chong Zhanga19f33e2014-08-07 15:35:07 -0700309 status_t err = source->setDataSource(fd, offset, length);
Chong Zhang3de157d2014-08-05 20:54:44 -0700310
311 if (err != OK) {
Chong Zhanga19f33e2014-08-07 15:35:07 -0700312 ALOGE("Failed to set data source!");
Chong Zhang3de157d2014-08-05 20:54:44 -0700313 source = NULL;
314 }
315
Andreas Huberafed0e12011-09-20 15:39:58 -0700316 msg->setObject("source", source);
Andreas Huberf9334412010-12-15 15:17:42 -0800317 msg->post();
Hassan Shojaniaff63de72017-04-26 15:10:42 -0700318 mDataSourceType = DATA_SOURCE_TYPE_GENERIC_FD;
Andreas Huberf9334412010-12-15 15:17:42 -0800319}
320
Chris Watkins99f31602015-03-20 13:06:33 -0700321void NuPlayer::setDataSourceAsync(const sp<DataSource> &dataSource) {
322 sp<AMessage> msg = new AMessage(kWhatSetDataSource, this);
323 sp<AMessage> notify = new AMessage(kWhatSourceNotify, this);
324
325 sp<GenericSource> source = new GenericSource(notify, mUIDValid, mUID);
326 status_t err = source->setDataSource(dataSource);
327
328 if (err != OK) {
329 ALOGE("Failed to set data source!");
330 source = NULL;
331 }
332
333 msg->setObject("source", source);
334 msg->post();
Hassan Shojaniaff63de72017-04-26 15:10:42 -0700335 mDataSourceType = DATA_SOURCE_TYPE_MEDIA;
Chris Watkins99f31602015-03-20 13:06:33 -0700336}
337
Wei Jia48fa06d2016-12-20 15:30:49 -0800338status_t NuPlayer::getDefaultBufferingSettings(
339 BufferingSettings *buffering /* nonnull */) {
340 sp<AMessage> msg = new AMessage(kWhatGetDefaultBufferingSettings, this);
341 sp<AMessage> response;
342 status_t err = msg->postAndAwaitResponse(&response);
343 if (err == OK && response != NULL) {
344 CHECK(response->findInt32("err", &err));
345 if (err == OK) {
346 readFromAMessage(response, buffering);
347 }
348 }
349 return err;
350}
351
352status_t NuPlayer::setBufferingSettings(const BufferingSettings& buffering) {
353 sp<AMessage> msg = new AMessage(kWhatSetBufferingSettings, this);
354 writeToAMessage(msg, buffering);
355 sp<AMessage> response;
356 status_t err = msg->postAndAwaitResponse(&response);
357 if (err == OK && response != NULL) {
358 CHECK(response->findInt32("err", &err));
359 }
360 return err;
361}
362
Andreas Huber9575c962013-02-05 13:59:56 -0800363void NuPlayer::prepareAsync() {
Hassan Shojaniacefac142017-02-06 21:02:02 -0800364 ALOGV("prepareAsync");
365
Lajos Molnar1d15ab52015-03-04 16:46:34 -0800366 (new AMessage(kWhatPrepare, this))->post();
Andreas Huber9575c962013-02-05 13:59:56 -0800367}
368
Andreas Huber57a339c2012-12-03 11:18:00 -0800369void NuPlayer::setVideoSurfaceTextureAsync(
Andy McFadden8ba01022012-12-18 09:46:54 -0800370 const sp<IGraphicBufferProducer> &bufferProducer) {
Lajos Molnar1de1e252015-04-30 18:18:34 -0700371 sp<AMessage> msg = new AMessage(kWhatSetVideoSurface, this);
Andreas Huber57a339c2012-12-03 11:18:00 -0800372
Andy McFadden8ba01022012-12-18 09:46:54 -0800373 if (bufferProducer == NULL) {
Lajos Molnar1de1e252015-04-30 18:18:34 -0700374 msg->setObject("surface", NULL);
Andreas Huber57a339c2012-12-03 11:18:00 -0800375 } else {
Lajos Molnar1de1e252015-04-30 18:18:34 -0700376 msg->setObject("surface", new Surface(bufferProducer, true /* controlledByApp */));
Andreas Huber57a339c2012-12-03 11:18:00 -0800377 }
378
Andreas Huberf9334412010-12-15 15:17:42 -0800379 msg->post();
380}
381
382void NuPlayer::setAudioSink(const sp<MediaPlayerBase::AudioSink> &sink) {
Lajos Molnar1d15ab52015-03-04 16:46:34 -0800383 sp<AMessage> msg = new AMessage(kWhatSetAudioSink, this);
Andreas Huberf9334412010-12-15 15:17:42 -0800384 msg->setObject("sink", sink);
385 msg->post();
386}
387
388void NuPlayer::start() {
Lajos Molnar1d15ab52015-03-04 16:46:34 -0800389 (new AMessage(kWhatStart, this))->post();
Andreas Huberf9334412010-12-15 15:17:42 -0800390}
391
Lajos Molnar3a474aa2015-04-24 17:10:07 -0700392status_t NuPlayer::setPlaybackSettings(const AudioPlaybackRate &rate) {
393 // do some cursory validation of the settings here. audio modes are
394 // only validated when set on the audiosink.
395 if ((rate.mSpeed != 0.f && rate.mSpeed < AUDIO_TIMESTRETCH_SPEED_MIN)
396 || rate.mSpeed > AUDIO_TIMESTRETCH_SPEED_MAX
397 || rate.mPitch < AUDIO_TIMESTRETCH_SPEED_MIN
398 || rate.mPitch > AUDIO_TIMESTRETCH_SPEED_MAX) {
399 return BAD_VALUE;
400 }
401 sp<AMessage> msg = new AMessage(kWhatConfigPlayback, this);
402 writeToAMessage(msg, rate);
403 sp<AMessage> response;
404 status_t err = msg->postAndAwaitResponse(&response);
405 if (err == OK && response != NULL) {
406 CHECK(response->findInt32("err", &err));
407 }
408 return err;
409}
410
411status_t NuPlayer::getPlaybackSettings(AudioPlaybackRate *rate /* nonnull */) {
412 sp<AMessage> msg = new AMessage(kWhatGetPlaybackSettings, this);
413 sp<AMessage> response;
414 status_t err = msg->postAndAwaitResponse(&response);
415 if (err == OK && response != NULL) {
416 CHECK(response->findInt32("err", &err));
417 if (err == OK) {
418 readFromAMessage(response, rate);
419 }
420 }
421 return err;
422}
423
424status_t NuPlayer::setSyncSettings(const AVSyncSettings &sync, float videoFpsHint) {
425 sp<AMessage> msg = new AMessage(kWhatConfigSync, this);
426 writeToAMessage(msg, sync, videoFpsHint);
427 sp<AMessage> response;
428 status_t err = msg->postAndAwaitResponse(&response);
429 if (err == OK && response != NULL) {
430 CHECK(response->findInt32("err", &err));
431 }
432 return err;
433}
434
435status_t NuPlayer::getSyncSettings(
436 AVSyncSettings *sync /* nonnull */, float *videoFps /* nonnull */) {
437 sp<AMessage> msg = new AMessage(kWhatGetSyncSettings, this);
438 sp<AMessage> response;
439 status_t err = msg->postAndAwaitResponse(&response);
440 if (err == OK && response != NULL) {
441 CHECK(response->findInt32("err", &err));
442 if (err == OK) {
443 readFromAMessage(response, sync, videoFps);
444 }
445 }
446 return err;
Wei Jia98160162015-02-04 17:01:11 -0800447}
448
Andreas Huber43c3e6c2011-01-05 12:17:08 -0800449void NuPlayer::pause() {
Lajos Molnar1d15ab52015-03-04 16:46:34 -0800450 (new AMessage(kWhatPause, this))->post();
Andreas Huber43c3e6c2011-01-05 12:17:08 -0800451}
452
Andreas Huber1aef2112011-01-04 14:01:29 -0800453void NuPlayer::resetAsync() {
Wei Jiac45a4b22016-04-15 15:30:23 -0700454 sp<Source> source;
455 {
456 Mutex::Autolock autoLock(mSourceLock);
457 source = mSource;
458 }
459
460 if (source != NULL) {
Chong Zhang48296b72014-09-14 14:28:45 -0700461 // During a reset, the data source might be unresponsive already, we need to
462 // disconnect explicitly so that reads exit promptly.
463 // We can't queue the disconnect request to the looper, as it might be
464 // queued behind a stuck read and never gets processed.
465 // Doing a disconnect outside the looper to allows the pending reads to exit
466 // (either successfully or with error).
Wei Jiac45a4b22016-04-15 15:30:23 -0700467 source->disconnect();
Chong Zhang48296b72014-09-14 14:28:45 -0700468 }
469
Lajos Molnar1d15ab52015-03-04 16:46:34 -0800470 (new AMessage(kWhatReset, this))->post();
Andreas Huber1aef2112011-01-04 14:01:29 -0800471}
472
Wei Jiac5de0912016-11-18 10:22:14 -0800473void NuPlayer::seekToAsync(int64_t seekTimeUs, MediaPlayerSeekMode mode, bool needNotify) {
Lajos Molnar1d15ab52015-03-04 16:46:34 -0800474 sp<AMessage> msg = new AMessage(kWhatSeek, this);
Andreas Huber43c3e6c2011-01-05 12:17:08 -0800475 msg->setInt64("seekTimeUs", seekTimeUs);
Wei Jiac5de0912016-11-18 10:22:14 -0800476 msg->setInt32("mode", mode);
Wei Jiae427abf2014-09-22 15:21:11 -0700477 msg->setInt32("needNotify", needNotify);
Andreas Huber43c3e6c2011-01-05 12:17:08 -0800478 msg->post();
479}
480
Andreas Huber53df1a42010-12-22 10:03:04 -0800481
Chong Zhang404fced2014-06-11 14:45:31 -0700482void NuPlayer::writeTrackInfo(
Chih-Hung Hsiehe964d4e2016-08-09 14:31:32 -0700483 Parcel* reply, const sp<AMessage>& format) const {
Marco Nelissenc367ca12015-09-15 09:51:59 -0700484 if (format == NULL) {
485 ALOGE("NULL format");
486 return;
487 }
Chong Zhang404fced2014-06-11 14:45:31 -0700488 int32_t trackType;
Marco Nelissen9436e482015-09-15 10:21:53 -0700489 if (!format->findInt32("type", &trackType)) {
Marco Nelissenc367ca12015-09-15 09:51:59 -0700490 ALOGE("no track type");
491 return;
492 }
Chong Zhang404fced2014-06-11 14:45:31 -0700493
Robert Shih755106e2015-04-30 14:36:45 -0700494 AString mime;
Robert Shih2e3a4252015-05-06 10:21:15 -0700495 if (!format->findString("mime", &mime)) {
496 // Java MediaPlayer only uses mimetype for subtitle and timedtext tracks.
497 // If we can't find the mimetype here it means that we wouldn't be needing
498 // the mimetype on the Java end. We still write a placeholder mime to keep the
499 // (de)serialization logic simple.
500 if (trackType == MEDIA_TRACK_TYPE_AUDIO) {
501 mime = "audio/";
502 } else if (trackType == MEDIA_TRACK_TYPE_VIDEO) {
503 mime = "video/";
504 } else {
Marco Nelissenc367ca12015-09-15 09:51:59 -0700505 ALOGE("unknown track type: %d", trackType);
506 return;
Robert Shih2e3a4252015-05-06 10:21:15 -0700507 }
508 }
Robert Shih755106e2015-04-30 14:36:45 -0700509
Chong Zhang404fced2014-06-11 14:45:31 -0700510 AString lang;
Marco Nelissen9436e482015-09-15 10:21:53 -0700511 if (!format->findString("language", &lang)) {
Marco Nelissenc367ca12015-09-15 09:51:59 -0700512 ALOGE("no language");
513 return;
514 }
Chong Zhang404fced2014-06-11 14:45:31 -0700515
516 reply->writeInt32(2); // write something non-zero
517 reply->writeInt32(trackType);
Robert Shih755106e2015-04-30 14:36:45 -0700518 reply->writeString16(String16(mime.c_str()));
Chong Zhang404fced2014-06-11 14:45:31 -0700519 reply->writeString16(String16(lang.c_str()));
520
521 if (trackType == MEDIA_TRACK_TYPE_SUBTITLE) {
Chong Zhang404fced2014-06-11 14:45:31 -0700522 int32_t isAuto, isDefault, isForced;
523 CHECK(format->findInt32("auto", &isAuto));
524 CHECK(format->findInt32("default", &isDefault));
525 CHECK(format->findInt32("forced", &isForced));
526
Chong Zhang404fced2014-06-11 14:45:31 -0700527 reply->writeInt32(isAuto);
528 reply->writeInt32(isDefault);
529 reply->writeInt32(isForced);
530 }
531}
532
Andreas Huberf9334412010-12-15 15:17:42 -0800533void NuPlayer::onMessageReceived(const sp<AMessage> &msg) {
534 switch (msg->what()) {
535 case kWhatSetDataSource:
536 {
Steve Block3856b092011-10-20 11:56:00 +0100537 ALOGV("kWhatSetDataSource");
Andreas Huberf9334412010-12-15 15:17:42 -0800538
539 CHECK(mSource == NULL);
540
Chong Zhang3de157d2014-08-05 20:54:44 -0700541 status_t err = OK;
Andreas Huber5bc087c2010-12-23 10:27:40 -0800542 sp<RefBase> obj;
543 CHECK(msg->findObject("source", &obj));
Chong Zhang3de157d2014-08-05 20:54:44 -0700544 if (obj != NULL) {
Wei Jiac45a4b22016-04-15 15:30:23 -0700545 Mutex::Autolock autoLock(mSourceLock);
Chong Zhang3de157d2014-08-05 20:54:44 -0700546 mSource = static_cast<Source *>(obj.get());
Chong Zhang3de157d2014-08-05 20:54:44 -0700547 } else {
548 err = UNKNOWN_ERROR;
549 }
Andreas Huber9575c962013-02-05 13:59:56 -0800550
551 CHECK(mDriver != NULL);
552 sp<NuPlayerDriver> driver = mDriver.promote();
553 if (driver != NULL) {
Chong Zhang3de157d2014-08-05 20:54:44 -0700554 driver->notifySetDataSourceCompleted(err);
Andreas Huber9575c962013-02-05 13:59:56 -0800555 }
556 break;
557 }
558
Wei Jia48fa06d2016-12-20 15:30:49 -0800559 case kWhatGetDefaultBufferingSettings:
560 {
561 sp<AReplyToken> replyID;
562 CHECK(msg->senderAwaitsResponse(&replyID));
563
564 ALOGV("kWhatGetDefaultBufferingSettings");
565 BufferingSettings buffering;
566 status_t err = OK;
567 if (mSource != NULL) {
568 err = mSource->getDefaultBufferingSettings(&buffering);
569 } else {
570 err = INVALID_OPERATION;
571 }
572 sp<AMessage> response = new AMessage;
573 if (err == OK) {
574 writeToAMessage(response, buffering);
575 }
576 response->setInt32("err", err);
577 response->postReply(replyID);
578 break;
579 }
580
581 case kWhatSetBufferingSettings:
582 {
583 sp<AReplyToken> replyID;
584 CHECK(msg->senderAwaitsResponse(&replyID));
585
586 ALOGV("kWhatSetBufferingSettings");
587 BufferingSettings buffering;
588 readFromAMessage(msg, &buffering);
589 status_t err = OK;
590 if (mSource != NULL) {
591 err = mSource->setBufferingSettings(buffering);
592 } else {
593 err = INVALID_OPERATION;
594 }
595 sp<AMessage> response = new AMessage;
596 response->setInt32("err", err);
597 response->postReply(replyID);
598 break;
599 }
600
Andreas Huber9575c962013-02-05 13:59:56 -0800601 case kWhatPrepare:
602 {
Hassan Shojaniacefac142017-02-06 21:02:02 -0800603 ALOGV("onMessageReceived kWhatPrepare");
604
Andreas Huber9575c962013-02-05 13:59:56 -0800605 mSource->prepareAsync();
Andreas Huberf9334412010-12-15 15:17:42 -0800606 break;
607 }
608
Chong Zhangdcb89b32013-08-06 09:44:47 -0700609 case kWhatGetTrackInfo:
610 {
Lajos Molnar3f274362015-03-05 14:35:41 -0800611 sp<AReplyToken> replyID;
Chong Zhangdcb89b32013-08-06 09:44:47 -0700612 CHECK(msg->senderAwaitsResponse(&replyID));
613
Chong Zhang404fced2014-06-11 14:45:31 -0700614 Parcel* reply;
615 CHECK(msg->findPointer("reply", (void**)&reply));
616
617 size_t inbandTracks = 0;
Chong Zhangdcb89b32013-08-06 09:44:47 -0700618 if (mSource != NULL) {
Chong Zhang404fced2014-06-11 14:45:31 -0700619 inbandTracks = mSource->getTrackCount();
620 }
621
Chong Zhanga7fa1d92014-06-11 14:49:23 -0700622 size_t ccTracks = 0;
623 if (mCCDecoder != NULL) {
624 ccTracks = mCCDecoder->getTrackCount();
625 }
626
Chong Zhang404fced2014-06-11 14:45:31 -0700627 // total track count
Chong Zhanga7fa1d92014-06-11 14:49:23 -0700628 reply->writeInt32(inbandTracks + ccTracks);
Chong Zhang404fced2014-06-11 14:45:31 -0700629
630 // write inband tracks
631 for (size_t i = 0; i < inbandTracks; ++i) {
632 writeTrackInfo(reply, mSource->getTrackInfo(i));
Chong Zhangdcb89b32013-08-06 09:44:47 -0700633 }
634
Chong Zhanga7fa1d92014-06-11 14:49:23 -0700635 // write CC track
636 for (size_t i = 0; i < ccTracks; ++i) {
637 writeTrackInfo(reply, mCCDecoder->getTrackInfo(i));
638 }
639
Chong Zhangdcb89b32013-08-06 09:44:47 -0700640 sp<AMessage> response = new AMessage;
Chong Zhangdcb89b32013-08-06 09:44:47 -0700641 response->postReply(replyID);
642 break;
643 }
644
Robert Shih7c4f0d72014-07-09 18:53:31 -0700645 case kWhatGetSelectedTrack:
646 {
647 status_t err = INVALID_OPERATION;
648 if (mSource != NULL) {
649 err = OK;
650
651 int32_t type32;
652 CHECK(msg->findInt32("type", (int32_t*)&type32));
653 media_track_type type = (media_track_type)type32;
654 ssize_t selectedTrack = mSource->getSelectedTrack(type);
655
656 Parcel* reply;
657 CHECK(msg->findPointer("reply", (void**)&reply));
658 reply->writeInt32(selectedTrack);
659 }
660
661 sp<AMessage> response = new AMessage;
662 response->setInt32("err", err);
663
Lajos Molnar3f274362015-03-05 14:35:41 -0800664 sp<AReplyToken> replyID;
Robert Shih7c4f0d72014-07-09 18:53:31 -0700665 CHECK(msg->senderAwaitsResponse(&replyID));
666 response->postReply(replyID);
667 break;
668 }
669
Chong Zhangdcb89b32013-08-06 09:44:47 -0700670 case kWhatSelectTrack:
671 {
Lajos Molnar3f274362015-03-05 14:35:41 -0800672 sp<AReplyToken> replyID;
Chong Zhangdcb89b32013-08-06 09:44:47 -0700673 CHECK(msg->senderAwaitsResponse(&replyID));
674
Chong Zhang404fced2014-06-11 14:45:31 -0700675 size_t trackIndex;
676 int32_t select;
Robert Shih6ffb1fd2014-10-29 16:24:32 -0700677 int64_t timeUs;
Chong Zhang404fced2014-06-11 14:45:31 -0700678 CHECK(msg->findSize("trackIndex", &trackIndex));
679 CHECK(msg->findInt32("select", &select));
Robert Shih6ffb1fd2014-10-29 16:24:32 -0700680 CHECK(msg->findInt64("timeUs", &timeUs));
Chong Zhang404fced2014-06-11 14:45:31 -0700681
Chong Zhangdcb89b32013-08-06 09:44:47 -0700682 status_t err = INVALID_OPERATION;
Chong Zhang404fced2014-06-11 14:45:31 -0700683
684 size_t inbandTracks = 0;
Chong Zhangdcb89b32013-08-06 09:44:47 -0700685 if (mSource != NULL) {
Chong Zhang404fced2014-06-11 14:45:31 -0700686 inbandTracks = mSource->getTrackCount();
687 }
Chong Zhanga7fa1d92014-06-11 14:49:23 -0700688 size_t ccTracks = 0;
689 if (mCCDecoder != NULL) {
690 ccTracks = mCCDecoder->getTrackCount();
691 }
Chong Zhang404fced2014-06-11 14:45:31 -0700692
693 if (trackIndex < inbandTracks) {
Robert Shih6ffb1fd2014-10-29 16:24:32 -0700694 err = mSource->selectTrack(trackIndex, select, timeUs);
Robert Shihd3b0bbb2014-07-23 15:00:25 -0700695
696 if (!select && err == OK) {
697 int32_t type;
698 sp<AMessage> info = mSource->getTrackInfo(trackIndex);
699 if (info != NULL
700 && info->findInt32("type", &type)
701 && type == MEDIA_TRACK_TYPE_TIMEDTEXT) {
702 ++mTimedTextGeneration;
703 }
704 }
Chong Zhanga7fa1d92014-06-11 14:49:23 -0700705 } else {
706 trackIndex -= inbandTracks;
707
708 if (trackIndex < ccTracks) {
709 err = mCCDecoder->selectTrack(trackIndex, select);
710 }
Chong Zhangdcb89b32013-08-06 09:44:47 -0700711 }
712
713 sp<AMessage> response = new AMessage;
714 response->setInt32("err", err);
715
716 response->postReply(replyID);
717 break;
718 }
719
Andreas Huberb7c8e912012-11-27 15:02:53 -0800720 case kWhatPollDuration:
721 {
722 int32_t generation;
723 CHECK(msg->findInt32("generation", &generation));
724
725 if (generation != mPollDurationGeneration) {
726 // stale
727 break;
728 }
729
730 int64_t durationUs;
731 if (mDriver != NULL && mSource->getDuration(&durationUs) == OK) {
732 sp<NuPlayerDriver> driver = mDriver.promote();
733 if (driver != NULL) {
734 driver->notifyDuration(durationUs);
735 }
736 }
737
738 msg->post(1000000ll); // poll again in a second.
739 break;
740 }
741
Lajos Molnar1de1e252015-04-30 18:18:34 -0700742 case kWhatSetVideoSurface:
Andreas Huberf9334412010-12-15 15:17:42 -0800743 {
Andreas Huberf9334412010-12-15 15:17:42 -0800744
745 sp<RefBase> obj;
Lajos Molnar1de1e252015-04-30 18:18:34 -0700746 CHECK(msg->findObject("surface", &obj));
747 sp<Surface> surface = static_cast<Surface *>(obj.get());
Lajos Molnara81c6222015-07-10 19:17:45 -0700748
749 ALOGD("onSetVideoSurface(%p, %s video decoder)",
750 surface.get(),
Wei Jia6b7d2ed2015-08-10 15:11:54 -0700751 (mSource != NULL && mStarted && mSource->getFormat(false /* audio */) != NULL
Lajos Molnara81c6222015-07-10 19:17:45 -0700752 && mVideoDecoder != NULL) ? "have" : "no");
753
Wei Jia6b7d2ed2015-08-10 15:11:54 -0700754 // Need to check mStarted before calling mSource->getFormat because NuPlayer might
755 // be in preparing state and it could take long time.
756 // When mStarted is true, mSource must have been set.
757 if (mSource == NULL || !mStarted || mSource->getFormat(false /* audio */) == NULL
Lajos Molnara81c6222015-07-10 19:17:45 -0700758 // NOTE: mVideoDecoder's mSurface is always non-null
759 || (mVideoDecoder != NULL && mVideoDecoder->setVideoSurface(surface) == OK)) {
Lajos Molnar1de1e252015-04-30 18:18:34 -0700760 performSetSurface(surface);
Wei Jiafef808d2014-10-31 17:57:05 -0700761 break;
762 }
763
764 mDeferredActions.push_back(
Krystian Turczyn687a5892016-01-20 14:20:35 +0100765 new FlushDecoderAction(
766 (obj != NULL ? FLUSH_CMD_FLUSH : FLUSH_CMD_NONE) /* audio */,
Wei Jiafef808d2014-10-31 17:57:05 -0700767 FLUSH_CMD_SHUTDOWN /* video */));
768
Lajos Molnar1de1e252015-04-30 18:18:34 -0700769 mDeferredActions.push_back(new SetSurfaceAction(surface));
James Dong0d268a32012-08-31 12:18:27 -0700770
Krystian Turczyn687a5892016-01-20 14:20:35 +0100771 if (obj != NULL) {
Wei Jiafef808d2014-10-31 17:57:05 -0700772 if (mStarted) {
Andy Hung73535852014-09-05 11:42:58 -0700773 // Issue a seek to refresh the video screen only if started otherwise
774 // the extractor may not yet be started and will assert.
775 // If the video decoder is not set (perhaps audio only in this case)
776 // do not perform a seek as it is not needed.
Ronghua Wua73d9e02014-10-08 15:13:29 -0700777 int64_t currentPositionUs = 0;
778 if (getCurrentPosition(&currentPositionUs) == OK) {
779 mDeferredActions.push_back(
Wei Jiac5de0912016-11-18 10:22:14 -0800780 new SeekAction(currentPositionUs,
781 MediaPlayerSeekMode::SEEK_PREVIOUS_SYNC /* mode */));
Ronghua Wua73d9e02014-10-08 15:13:29 -0700782 }
Andy Hung73535852014-09-05 11:42:58 -0700783 }
Wei Jiaac428aa2014-09-02 19:01:34 -0700784
Andreas Huber57a339c2012-12-03 11:18:00 -0800785 // If there is a new surface texture, instantiate decoders
786 // again if possible.
787 mDeferredActions.push_back(
788 new SimpleAction(&NuPlayer::performScanSources));
Andreas Huber57a339c2012-12-03 11:18:00 -0800789
Krystian Turczyn687a5892016-01-20 14:20:35 +0100790 // After a flush without shutdown, decoder is paused.
791 // Don't resume it until source seek is done, otherwise it could
792 // start pulling stale data too soon.
793 mDeferredActions.push_back(
794 new ResumeDecoderAction(false /* needNotify */));
795 }
Chong Zhang7137ec72014-11-12 16:41:05 -0800796
Andreas Huber57a339c2012-12-03 11:18:00 -0800797 processDeferredActions();
Andreas Huberf9334412010-12-15 15:17:42 -0800798 break;
799 }
800
801 case kWhatSetAudioSink:
802 {
Steve Block3856b092011-10-20 11:56:00 +0100803 ALOGV("kWhatSetAudioSink");
Andreas Huberf9334412010-12-15 15:17:42 -0800804
805 sp<RefBase> obj;
806 CHECK(msg->findObject("sink", &obj));
807
808 mAudioSink = static_cast<MediaPlayerBase::AudioSink *>(obj.get());
809 break;
810 }
811
812 case kWhatStart:
813 {
Steve Block3856b092011-10-20 11:56:00 +0100814 ALOGV("kWhatStart");
Wei Jia94211742014-10-28 17:09:06 -0700815 if (mStarted) {
Chong Zhang8a048332015-05-06 15:16:28 -0700816 // do not resume yet if the source is still buffering
817 if (!mPausedForBuffering) {
818 onResume();
819 }
Wei Jia94211742014-10-28 17:09:06 -0700820 } else {
821 onStart();
Lajos Molnar09524832014-07-17 14:29:51 -0700822 }
Chong Zhangefbb6192015-01-30 17:13:27 -0800823 mPausedByClient = false;
Andreas Huberf9334412010-12-15 15:17:42 -0800824 break;
825 }
826
Lajos Molnar3a474aa2015-04-24 17:10:07 -0700827 case kWhatConfigPlayback:
Wei Jia98160162015-02-04 17:01:11 -0800828 {
Lajos Molnar3a474aa2015-04-24 17:10:07 -0700829 sp<AReplyToken> replyID;
830 CHECK(msg->senderAwaitsResponse(&replyID));
831 AudioPlaybackRate rate /* sanitized */;
832 readFromAMessage(msg, &rate);
833 status_t err = OK;
Wei Jia98160162015-02-04 17:01:11 -0800834 if (mRenderer != NULL) {
Wei Jiabf70feb2016-02-19 15:47:16 -0800835 // AudioSink allows only 1.f and 0.f for offload mode.
836 // For other speed, switch to non-offload mode.
837 if (mOffloadAudio && ((rate.mSpeed != 0.f && rate.mSpeed != 1.f)
838 || rate.mPitch != 1.f)) {
839 int64_t currentPositionUs;
840 if (getCurrentPosition(&currentPositionUs) != OK) {
841 currentPositionUs = mPreviousSeekTimeUs;
842 }
843
844 // Set mPlaybackSettings so that the new audio decoder can
845 // be created correctly.
846 mPlaybackSettings = rate;
847 if (!mPaused) {
848 mRenderer->pause();
849 }
Wei Jia5031b2f2016-02-25 11:19:31 -0800850 restartAudio(
Wei Jiabf70feb2016-02-19 15:47:16 -0800851 currentPositionUs, true /* forceNonOffload */,
852 true /* needsToCreateAudioDecoder */);
853 if (!mPaused) {
854 mRenderer->resume();
855 }
856 }
857
Lajos Molnar3a474aa2015-04-24 17:10:07 -0700858 err = mRenderer->setPlaybackSettings(rate);
859 }
860 if (err == OK) {
861 if (rate.mSpeed == 0.f) {
862 onPause();
Wei Jia351ce872016-02-10 13:21:59 -0800863 mPausedByClient = true;
Lajos Molnar3a474aa2015-04-24 17:10:07 -0700864 // save all other settings (using non-paused speed)
865 // so we can restore them on start
866 AudioPlaybackRate newRate = rate;
867 newRate.mSpeed = mPlaybackSettings.mSpeed;
868 mPlaybackSettings = newRate;
869 } else { /* rate.mSpeed != 0.f */
Lajos Molnar3a474aa2015-04-24 17:10:07 -0700870 mPlaybackSettings = rate;
Wei Jia8a092d32016-06-03 14:57:24 -0700871 if (mStarted) {
872 // do not resume yet if the source is still buffering
873 if (!mPausedForBuffering) {
874 onResume();
875 }
876 } else if (mPrepared) {
877 onStart();
878 }
879
880 mPausedByClient = false;
Lajos Molnar3a474aa2015-04-24 17:10:07 -0700881 }
Wei Jia98160162015-02-04 17:01:11 -0800882 }
Ronghua Wu8db88132015-04-22 13:51:35 -0700883
884 if (mVideoDecoder != NULL) {
Praveen Chavanbbaa1442016-04-08 13:33:49 -0700885 sp<AMessage> params = new AMessage();
886 params->setFloat("playback-speed", mPlaybackSettings.mSpeed);
887 mVideoDecoder->setParameters(params);
Ronghua Wu8db88132015-04-22 13:51:35 -0700888 }
889
Lajos Molnar3a474aa2015-04-24 17:10:07 -0700890 sp<AMessage> response = new AMessage;
891 response->setInt32("err", err);
892 response->postReply(replyID);
893 break;
894 }
895
896 case kWhatGetPlaybackSettings:
897 {
898 sp<AReplyToken> replyID;
899 CHECK(msg->senderAwaitsResponse(&replyID));
900 AudioPlaybackRate rate = mPlaybackSettings;
901 status_t err = OK;
902 if (mRenderer != NULL) {
903 err = mRenderer->getPlaybackSettings(&rate);
904 }
905 if (err == OK) {
906 // get playback settings used by renderer, as it may be
907 // slightly off due to audiosink not taking small changes.
908 mPlaybackSettings = rate;
909 if (mPaused) {
910 rate.mSpeed = 0.f;
911 }
912 }
913 sp<AMessage> response = new AMessage;
914 if (err == OK) {
915 writeToAMessage(response, rate);
916 }
917 response->setInt32("err", err);
918 response->postReply(replyID);
919 break;
920 }
921
922 case kWhatConfigSync:
923 {
924 sp<AReplyToken> replyID;
925 CHECK(msg->senderAwaitsResponse(&replyID));
926
927 ALOGV("kWhatConfigSync");
928 AVSyncSettings sync;
929 float videoFpsHint;
930 readFromAMessage(msg, &sync, &videoFpsHint);
931 status_t err = OK;
932 if (mRenderer != NULL) {
933 err = mRenderer->setSyncSettings(sync, videoFpsHint);
934 }
935 if (err == OK) {
936 mSyncSettings = sync;
937 mVideoFpsHint = videoFpsHint;
938 }
939 sp<AMessage> response = new AMessage;
940 response->setInt32("err", err);
941 response->postReply(replyID);
942 break;
943 }
944
945 case kWhatGetSyncSettings:
946 {
947 sp<AReplyToken> replyID;
948 CHECK(msg->senderAwaitsResponse(&replyID));
949 AVSyncSettings sync = mSyncSettings;
950 float videoFps = mVideoFpsHint;
951 status_t err = OK;
952 if (mRenderer != NULL) {
953 err = mRenderer->getSyncSettings(&sync, &videoFps);
954 if (err == OK) {
955 mSyncSettings = sync;
956 mVideoFpsHint = videoFps;
957 }
958 }
959 sp<AMessage> response = new AMessage;
960 if (err == OK) {
961 writeToAMessage(response, sync, videoFps);
962 }
963 response->setInt32("err", err);
964 response->postReply(replyID);
Wei Jia98160162015-02-04 17:01:11 -0800965 break;
966 }
967
Andreas Huberf9334412010-12-15 15:17:42 -0800968 case kWhatScanSources:
969 {
Andreas Huber1aef2112011-01-04 14:01:29 -0800970 int32_t generation;
971 CHECK(msg->findInt32("generation", &generation));
972 if (generation != mScanSourcesGeneration) {
973 // Drop obsolete msg.
974 break;
975 }
976
Andreas Huber5bc087c2010-12-23 10:27:40 -0800977 mScanSourcesPending = false;
978
Steve Block3856b092011-10-20 11:56:00 +0100979 ALOGV("scanning sources haveAudio=%d, haveVideo=%d",
Andreas Huber43c3e6c2011-01-05 12:17:08 -0800980 mAudioDecoder != NULL, mVideoDecoder != NULL);
981
Andreas Huberb7c8e912012-11-27 15:02:53 -0800982 bool mHadAnySourcesBefore =
983 (mAudioDecoder != NULL) || (mVideoDecoder != NULL);
Robert Shih7350b052015-10-01 15:50:14 -0700984 bool rescan = false;
Andreas Huberb7c8e912012-11-27 15:02:53 -0800985
Andy Hung282a7e32014-08-14 15:56:34 -0700986 // initialize video before audio because successful initialization of
987 // video may change deep buffer mode of audio.
Lajos Molnar1de1e252015-04-30 18:18:34 -0700988 if (mSurface != NULL) {
Robert Shih7350b052015-10-01 15:50:14 -0700989 if (instantiateDecoder(false, &mVideoDecoder) == -EWOULDBLOCK) {
990 rescan = true;
991 }
Haynes Mathew George5d246ef2012-07-09 10:36:57 -0700992 }
Andreas Huberf9334412010-12-15 15:17:42 -0800993
Ronghua Wua10fd232014-11-06 16:15:20 -0800994 // Don't try to re-open audio sink if there's an existing decoder.
995 if (mAudioSink != NULL && mAudioDecoder == NULL) {
Robert Shih7350b052015-10-01 15:50:14 -0700996 if (instantiateDecoder(true, &mAudioDecoder) == -EWOULDBLOCK) {
997 rescan = true;
998 }
Andreas Huberf9334412010-12-15 15:17:42 -0800999 }
1000
Andreas Huberb7c8e912012-11-27 15:02:53 -08001001 if (!mHadAnySourcesBefore
1002 && (mAudioDecoder != NULL || mVideoDecoder != NULL)) {
1003 // This is the first time we've found anything playable.
1004
Andreas Huber9575c962013-02-05 13:59:56 -08001005 if (mSourceFlags & Source::FLAG_DYNAMIC_DURATION) {
Andreas Huberb7c8e912012-11-27 15:02:53 -08001006 schedulePollDuration();
1007 }
1008 }
1009
Andreas Hubereac68ba2011-09-27 12:12:25 -07001010 status_t err;
1011 if ((err = mSource->feedMoreTSData()) != OK) {
Andreas Huber1aef2112011-01-04 14:01:29 -08001012 if (mAudioDecoder == NULL && mVideoDecoder == NULL) {
1013 // We're not currently decoding anything (no audio or
1014 // video tracks found) and we just ran out of input data.
Andreas Hubereac68ba2011-09-27 12:12:25 -07001015
1016 if (err == ERROR_END_OF_STREAM) {
1017 notifyListener(MEDIA_PLAYBACK_COMPLETE, 0, 0);
1018 } else {
1019 notifyListener(MEDIA_ERROR, MEDIA_ERROR_UNKNOWN, err);
1020 }
Andreas Huber1aef2112011-01-04 14:01:29 -08001021 }
Andreas Huberf9334412010-12-15 15:17:42 -08001022 break;
1023 }
1024
Robert Shih7350b052015-10-01 15:50:14 -07001025 if (rescan) {
Andreas Huberf9334412010-12-15 15:17:42 -08001026 msg->post(100000ll);
Andreas Huber5bc087c2010-12-23 10:27:40 -08001027 mScanSourcesPending = true;
Andreas Huberf9334412010-12-15 15:17:42 -08001028 }
1029 break;
1030 }
1031
1032 case kWhatVideoNotify:
1033 case kWhatAudioNotify:
1034 {
1035 bool audio = msg->what() == kWhatAudioNotify;
1036
Wei Jia88703c32014-08-06 11:24:07 -07001037 int32_t currentDecoderGeneration =
1038 (audio? mAudioDecoderGeneration : mVideoDecoderGeneration);
1039 int32_t requesterGeneration = currentDecoderGeneration - 1;
1040 CHECK(msg->findInt32("generation", &requesterGeneration));
1041
1042 if (requesterGeneration != currentDecoderGeneration) {
1043 ALOGV("got message from old %s decoder, generation(%d:%d)",
1044 audio ? "audio" : "video", requesterGeneration,
1045 currentDecoderGeneration);
1046 sp<AMessage> reply;
1047 if (!(msg->findMessage("reply", &reply))) {
1048 return;
1049 }
1050
1051 reply->setInt32("err", INFO_DISCONTINUITY);
1052 reply->post();
1053 return;
1054 }
1055
Andreas Huberf9334412010-12-15 15:17:42 -08001056 int32_t what;
Lajos Molnar1cd13982014-01-17 15:12:51 -08001057 CHECK(msg->findInt32("what", &what));
Andreas Huberf9334412010-12-15 15:17:42 -08001058
Chong Zhang7137ec72014-11-12 16:41:05 -08001059 if (what == DecoderBase::kWhatInputDiscontinuity) {
1060 int32_t formatChange;
1061 CHECK(msg->findInt32("formatChange", &formatChange));
Andreas Huberf9334412010-12-15 15:17:42 -08001062
Chong Zhang7137ec72014-11-12 16:41:05 -08001063 ALOGV("%s discontinuity: formatChange %d",
1064 audio ? "audio" : "video", formatChange);
1065
1066 if (formatChange) {
1067 mDeferredActions.push_back(
1068 new FlushDecoderAction(
1069 audio ? FLUSH_CMD_SHUTDOWN : FLUSH_CMD_NONE,
1070 audio ? FLUSH_CMD_NONE : FLUSH_CMD_SHUTDOWN));
Andreas Huberf9334412010-12-15 15:17:42 -08001071 }
Chong Zhang7137ec72014-11-12 16:41:05 -08001072
1073 mDeferredActions.push_back(
1074 new SimpleAction(
1075 &NuPlayer::performScanSources));
1076
1077 processDeferredActions();
1078 } else if (what == DecoderBase::kWhatEOS) {
Andreas Huberdc9bacd2011-09-26 10:53:29 -07001079 int32_t err;
Lajos Molnar1cd13982014-01-17 15:12:51 -08001080 CHECK(msg->findInt32("err", &err));
Andreas Huberdc9bacd2011-09-26 10:53:29 -07001081
1082 if (err == ERROR_END_OF_STREAM) {
Steve Block3856b092011-10-20 11:56:00 +01001083 ALOGV("got %s decoder EOS", audio ? "audio" : "video");
Andreas Huberdc9bacd2011-09-26 10:53:29 -07001084 } else {
Steve Block3856b092011-10-20 11:56:00 +01001085 ALOGV("got %s decoder EOS w/ error %d",
Andreas Huberdc9bacd2011-09-26 10:53:29 -07001086 audio ? "audio" : "video",
1087 err);
1088 }
1089
1090 mRenderer->queueEOS(audio, err);
Chong Zhang7137ec72014-11-12 16:41:05 -08001091 } else if (what == DecoderBase::kWhatFlushCompleted) {
Steve Block3856b092011-10-20 11:56:00 +01001092 ALOGV("decoder %s flush completed", audio ? "audio" : "video");
Andreas Huberf9334412010-12-15 15:17:42 -08001093
Andy Hung8d121d42014-10-03 09:53:53 -07001094 handleFlushComplete(audio, true /* isDecoder */);
Andreas Huber3831a062010-12-21 10:22:33 -08001095 finishFlushIfPossible();
Chong Zhang7137ec72014-11-12 16:41:05 -08001096 } else if (what == DecoderBase::kWhatVideoSizeChanged) {
Lajos Molnar1cd13982014-01-17 15:12:51 -08001097 sp<AMessage> format;
1098 CHECK(msg->findMessage("format", &format));
1099
Wei Jiac6cfd702014-11-11 16:33:20 -08001100 sp<AMessage> inputFormat =
1101 mSource->getFormat(false /* audio */);
Andreas Huber3831a062010-12-21 10:22:33 -08001102
Bartosz Dolewski26936f72014-12-11 12:47:08 +01001103 setVideoScalingMode(mVideoScalingMode);
Wei Jiac6cfd702014-11-11 16:33:20 -08001104 updateVideoSize(inputFormat, format);
Chong Zhang7137ec72014-11-12 16:41:05 -08001105 } else if (what == DecoderBase::kWhatShutdownCompleted) {
Steve Block3856b092011-10-20 11:56:00 +01001106 ALOGV("%s shutdown completed", audio ? "audio" : "video");
Andreas Huber3831a062010-12-21 10:22:33 -08001107 if (audio) {
1108 mAudioDecoder.clear();
Wei Jia686e8e52017-04-03 14:08:01 -07001109 mAudioDecoderError = false;
Wei Jia57568df2014-09-22 10:16:29 -07001110 ++mAudioDecoderGeneration;
Andreas Huber3831a062010-12-21 10:22:33 -08001111
1112 CHECK_EQ((int)mFlushingAudio, (int)SHUTTING_DOWN_DECODER);
1113 mFlushingAudio = SHUT_DOWN;
1114 } else {
1115 mVideoDecoder.clear();
Wei Jia686e8e52017-04-03 14:08:01 -07001116 mVideoDecoderError = false;
Wei Jia57568df2014-09-22 10:16:29 -07001117 ++mVideoDecoderGeneration;
Andreas Huber3831a062010-12-21 10:22:33 -08001118
1119 CHECK_EQ((int)mFlushingVideo, (int)SHUTTING_DOWN_DECODER);
1120 mFlushingVideo = SHUT_DOWN;
1121 }
1122
1123 finishFlushIfPossible();
Chong Zhangf8d71772014-11-26 15:08:34 -08001124 } else if (what == DecoderBase::kWhatResumeCompleted) {
1125 finishResume();
Chong Zhang7137ec72014-11-12 16:41:05 -08001126 } else if (what == DecoderBase::kWhatError) {
Chong Zhangf4c0a942014-08-11 15:14:10 -07001127 status_t err;
Andy Hung2abde2c2014-09-30 14:40:32 -07001128 if (!msg->findInt32("err", &err) || err == OK) {
Chong Zhangf4c0a942014-08-11 15:14:10 -07001129 err = UNKNOWN_ERROR;
1130 }
Andy Hungcf31f1e2014-09-23 14:59:01 -07001131
Andy Hung2abde2c2014-09-30 14:40:32 -07001132 // Decoder errors can be due to Source (e.g. from streaming),
1133 // or from decoding corrupted bitstreams, or from other decoder
1134 // MediaCodec operations (e.g. from an ongoing reset or seek).
Andy Hung202bce12014-12-03 11:47:36 -08001135 // They may also be due to openAudioSink failure at
1136 // decoder start or after a format change.
Andy Hung2abde2c2014-09-30 14:40:32 -07001137 //
1138 // We try to gracefully shut down the affected decoder if possible,
1139 // rather than trying to force the shutdown with something
1140 // similar to performReset(). This method can lead to a hang
1141 // if MediaCodec functions block after an error, but they should
1142 // typically return INVALID_OPERATION instead of blocking.
1143
1144 FlushStatus *flushing = audio ? &mFlushingAudio : &mFlushingVideo;
1145 ALOGE("received error(%#x) from %s decoder, flushing(%d), now shutting down",
1146 err, audio ? "audio" : "video", *flushing);
1147
1148 switch (*flushing) {
1149 case NONE:
1150 mDeferredActions.push_back(
Wei Jiafef808d2014-10-31 17:57:05 -07001151 new FlushDecoderAction(
1152 audio ? FLUSH_CMD_SHUTDOWN : FLUSH_CMD_NONE,
1153 audio ? FLUSH_CMD_NONE : FLUSH_CMD_SHUTDOWN));
Andy Hung2abde2c2014-09-30 14:40:32 -07001154 processDeferredActions();
1155 break;
1156 case FLUSHING_DECODER:
1157 *flushing = FLUSHING_DECODER_SHUTDOWN; // initiate shutdown after flush.
1158 break; // Wait for flush to complete.
1159 case FLUSHING_DECODER_SHUTDOWN:
1160 break; // Wait for flush to complete.
1161 case SHUTTING_DOWN_DECODER:
1162 break; // Wait for shutdown to complete.
1163 case FLUSHED:
Andy Hung2abde2c2014-09-30 14:40:32 -07001164 getDecoder(audio)->initiateShutdown(); // In the middle of a seek.
1165 *flushing = SHUTTING_DOWN_DECODER; // Shut down.
1166 break;
1167 case SHUT_DOWN:
1168 finishFlushIfPossible(); // Should not occur.
1169 break; // Finish anyways.
Marco Nelissen9e2b7912014-08-18 16:13:03 -07001170 }
Wei Jia686e8e52017-04-03 14:08:01 -07001171 if (mSource != nullptr) {
1172 if (audio) {
Wei Jia2f6d8c52017-04-11 09:50:31 -07001173 if (mVideoDecoderError || mSource->getFormat(false /* audio */) == NULL
Wei Jiaf86731d2017-07-11 17:24:34 -07001174 || mSurface == NULL || mVideoDecoder == NULL) {
Wei Jia686e8e52017-04-03 14:08:01 -07001175 // When both audio and video have error, or this stream has only audio
1176 // which has error, notify client of error.
1177 notifyListener(MEDIA_ERROR, MEDIA_ERROR_UNKNOWN, err);
1178 } else {
1179 // Only audio track has error. Video track could be still good to play.
1180 notifyListener(MEDIA_INFO, MEDIA_INFO_PLAY_AUDIO_ERROR, err);
1181 }
1182 mAudioDecoderError = true;
1183 } else {
Wei Jia2f6d8c52017-04-11 09:50:31 -07001184 if (mAudioDecoderError || mSource->getFormat(true /* audio */) == NULL
Wei Jiaf86731d2017-07-11 17:24:34 -07001185 || mAudioSink == NULL || mAudioDecoder == NULL) {
Wei Jia686e8e52017-04-03 14:08:01 -07001186 // When both audio and video have error, or this stream has only video
1187 // which has error, notify client of error.
1188 notifyListener(MEDIA_ERROR, MEDIA_ERROR_UNKNOWN, err);
1189 } else {
1190 // Only video track has error. Audio track could be still good to play.
1191 notifyListener(MEDIA_INFO, MEDIA_INFO_PLAY_VIDEO_ERROR, err);
1192 }
1193 mVideoDecoderError = true;
1194 }
1195 }
Lajos Molnar1cd13982014-01-17 15:12:51 -08001196 } else {
1197 ALOGV("Unhandled decoder notification %d '%c%c%c%c'.",
Andreas Hubera1f8ab02012-11-30 10:53:22 -08001198 what,
1199 what >> 24,
1200 (what >> 16) & 0xff,
1201 (what >> 8) & 0xff,
1202 what & 0xff);
Andreas Huberf9334412010-12-15 15:17:42 -08001203 }
1204
1205 break;
1206 }
1207
1208 case kWhatRendererNotify:
1209 {
Wei Jia57568df2014-09-22 10:16:29 -07001210 int32_t requesterGeneration = mRendererGeneration - 1;
1211 CHECK(msg->findInt32("generation", &requesterGeneration));
1212 if (requesterGeneration != mRendererGeneration) {
1213 ALOGV("got message from old renderer, generation(%d:%d)",
1214 requesterGeneration, mRendererGeneration);
1215 return;
1216 }
1217
Andreas Huberf9334412010-12-15 15:17:42 -08001218 int32_t what;
1219 CHECK(msg->findInt32("what", &what));
1220
1221 if (what == Renderer::kWhatEOS) {
1222 int32_t audio;
1223 CHECK(msg->findInt32("audio", &audio));
1224
Andreas Huberc92fd242011-08-16 13:48:44 -07001225 int32_t finalResult;
1226 CHECK(msg->findInt32("finalResult", &finalResult));
1227
Andreas Huberf9334412010-12-15 15:17:42 -08001228 if (audio) {
1229 mAudioEOS = true;
1230 } else {
1231 mVideoEOS = true;
1232 }
1233
Andreas Huberc92fd242011-08-16 13:48:44 -07001234 if (finalResult == ERROR_END_OF_STREAM) {
Steve Block3856b092011-10-20 11:56:00 +01001235 ALOGV("reached %s EOS", audio ? "audio" : "video");
Andreas Huberc92fd242011-08-16 13:48:44 -07001236 } else {
Steve Block29357bc2012-01-06 19:20:56 +00001237 ALOGE("%s track encountered an error (%d)",
Andreas Huberc92fd242011-08-16 13:48:44 -07001238 audio ? "audio" : "video", finalResult);
1239
1240 notifyListener(
1241 MEDIA_ERROR, MEDIA_ERROR_UNKNOWN, finalResult);
1242 }
Andreas Huberf9334412010-12-15 15:17:42 -08001243
1244 if ((mAudioEOS || mAudioDecoder == NULL)
1245 && (mVideoEOS || mVideoDecoder == NULL)) {
1246 notifyListener(MEDIA_PLAYBACK_COMPLETE, 0, 0);
1247 }
Andreas Huber3fe62152011-09-16 15:09:22 -07001248 } else if (what == Renderer::kWhatFlushComplete) {
Andreas Huberf9334412010-12-15 15:17:42 -08001249 int32_t audio;
1250 CHECK(msg->findInt32("audio", &audio));
1251
Wei Jia3261f0d2015-10-08 13:58:33 -07001252 if (audio) {
1253 mAudioEOS = false;
1254 } else {
1255 mVideoEOS = false;
1256 }
1257
Steve Block3856b092011-10-20 11:56:00 +01001258 ALOGV("renderer %s flush completed.", audio ? "audio" : "video");
Wei Jiada4252f2015-07-14 18:15:28 -07001259 if (audio && (mFlushingAudio == NONE || mFlushingAudio == FLUSHED
1260 || mFlushingAudio == SHUT_DOWN)) {
1261 // Flush has been handled by tear down.
1262 break;
1263 }
Andy Hung8d121d42014-10-03 09:53:53 -07001264 handleFlushComplete(audio, false /* isDecoder */);
1265 finishFlushIfPossible();
James Dongf57b4ea2012-07-20 13:38:36 -07001266 } else if (what == Renderer::kWhatVideoRenderingStart) {
1267 notifyListener(MEDIA_INFO, MEDIA_INFO_RENDERING_START, 0);
Lajos Molnarcbaffcf2013-08-14 18:30:38 -07001268 } else if (what == Renderer::kWhatMediaRenderingStart) {
1269 ALOGV("media rendering started");
1270 notifyListener(MEDIA_STARTED, 0, 0);
Ronghua Wufaeb0f22015-05-21 12:20:21 -07001271 } else if (what == Renderer::kWhatAudioTearDown) {
Ronghua Wu08529172014-10-02 16:55:52 -07001272 int32_t reason;
1273 CHECK(msg->findInt32("reason", &reason));
Ronghua Wufaeb0f22015-05-21 12:20:21 -07001274 ALOGV("Tear down audio with reason %d.", reason);
Wei Jia8456ddd2016-04-22 14:46:43 -07001275 if (reason == Renderer::kDueToTimeout && !(mPaused && mOffloadAudio)) {
1276 // TimeoutWhenPaused is only for offload mode.
1277 ALOGW("Receive a stale message for teardown.");
1278 break;
1279 }
Wei Jiada4252f2015-07-14 18:15:28 -07001280 int64_t positionUs;
Robert Shih1a5c8592015-08-04 18:07:44 -07001281 if (!msg->findInt64("positionUs", &positionUs)) {
1282 positionUs = mPreviousSeekTimeUs;
1283 }
Ronghua Wufaeb0f22015-05-21 12:20:21 -07001284
Wei Jia5031b2f2016-02-25 11:19:31 -08001285 restartAudio(
Wei Jiaa05f1e32016-03-25 16:31:22 -07001286 positionUs, reason == Renderer::kForceNonOffload /* forceNonOffload */,
1287 reason != Renderer::kDueToTimeout /* needsToCreateAudioDecoder */);
Andreas Huberf9334412010-12-15 15:17:42 -08001288 }
1289 break;
1290 }
1291
1292 case kWhatMoreDataQueued:
1293 {
1294 break;
1295 }
1296
Andreas Huber1aef2112011-01-04 14:01:29 -08001297 case kWhatReset:
1298 {
Steve Block3856b092011-10-20 11:56:00 +01001299 ALOGV("kWhatReset");
Andreas Huber1aef2112011-01-04 14:01:29 -08001300
Ronghua Wu64c2d172015-10-07 16:52:19 -07001301 mResetting = true;
1302
Andreas Hubera1f8ab02012-11-30 10:53:22 -08001303 mDeferredActions.push_back(
Wei Jiafef808d2014-10-31 17:57:05 -07001304 new FlushDecoderAction(
1305 FLUSH_CMD_SHUTDOWN /* audio */,
1306 FLUSH_CMD_SHUTDOWN /* video */));
Andreas Huberb7c8e912012-11-27 15:02:53 -08001307
Andreas Hubera1f8ab02012-11-30 10:53:22 -08001308 mDeferredActions.push_back(
1309 new SimpleAction(&NuPlayer::performReset));
Andreas Huberb58ce9f2011-11-28 16:27:35 -08001310
Andreas Hubera1f8ab02012-11-30 10:53:22 -08001311 processDeferredActions();
Andreas Huber1aef2112011-01-04 14:01:29 -08001312 break;
1313 }
1314
Andreas Huber43c3e6c2011-01-05 12:17:08 -08001315 case kWhatSeek:
1316 {
1317 int64_t seekTimeUs;
Wei Jiac5de0912016-11-18 10:22:14 -08001318 int32_t mode;
Wei Jiae427abf2014-09-22 15:21:11 -07001319 int32_t needNotify;
Andreas Huber43c3e6c2011-01-05 12:17:08 -08001320 CHECK(msg->findInt64("seekTimeUs", &seekTimeUs));
Wei Jiac5de0912016-11-18 10:22:14 -08001321 CHECK(msg->findInt32("mode", &mode));
Wei Jiae427abf2014-09-22 15:21:11 -07001322 CHECK(msg->findInt32("needNotify", &needNotify));
Andreas Huber43c3e6c2011-01-05 12:17:08 -08001323
Wei Jiac5de0912016-11-18 10:22:14 -08001324 ALOGV("kWhatSeek seekTimeUs=%lld us, mode=%d, needNotify=%d",
1325 (long long)seekTimeUs, mode, needNotify);
Andreas Huber43c3e6c2011-01-05 12:17:08 -08001326
Wei Jia1061c9c2015-05-19 16:02:17 -07001327 if (!mStarted) {
1328 // Seek before the player is started. In order to preview video,
1329 // need to start the player and pause it. This branch is called
1330 // only once if needed. After the player is started, any seek
1331 // operation will go through normal path.
Robert Shih0c61a0d2015-07-06 15:09:10 -07001332 // Audio-only cases are handled separately.
Wei Jiac5de0912016-11-18 10:22:14 -08001333 onStart(seekTimeUs, (MediaPlayerSeekMode)mode);
Robert Shih0c61a0d2015-07-06 15:09:10 -07001334 if (mStarted) {
1335 onPause();
1336 mPausedByClient = true;
1337 }
Wei Jia1061c9c2015-05-19 16:02:17 -07001338 if (needNotify) {
1339 notifyDriverSeekComplete();
1340 }
1341 break;
1342 }
1343
Andreas Hubera1f8ab02012-11-30 10:53:22 -08001344 mDeferredActions.push_back(
Wei Jiafef808d2014-10-31 17:57:05 -07001345 new FlushDecoderAction(FLUSH_CMD_FLUSH /* audio */,
1346 FLUSH_CMD_FLUSH /* video */));
Andreas Huber43c3e6c2011-01-05 12:17:08 -08001347
Wei Jiae427abf2014-09-22 15:21:11 -07001348 mDeferredActions.push_back(
Wei Jiac5de0912016-11-18 10:22:14 -08001349 new SeekAction(seekTimeUs, (MediaPlayerSeekMode)mode));
Andreas Huber43c3e6c2011-01-05 12:17:08 -08001350
Chong Zhangf8d71772014-11-26 15:08:34 -08001351 // After a flush without shutdown, decoder is paused.
1352 // Don't resume it until source seek is done, otherwise it could
Chong Zhang7137ec72014-11-12 16:41:05 -08001353 // start pulling stale data too soon.
1354 mDeferredActions.push_back(
Chong Zhangf8d71772014-11-26 15:08:34 -08001355 new ResumeDecoderAction(needNotify));
Chong Zhang7137ec72014-11-12 16:41:05 -08001356
Andreas Hubera1f8ab02012-11-30 10:53:22 -08001357 processDeferredActions();
Andreas Huber43c3e6c2011-01-05 12:17:08 -08001358 break;
1359 }
1360
Andreas Huberb4082222011-01-20 15:23:04 -08001361 case kWhatPause:
1362 {
Chong Zhangefbb6192015-01-30 17:13:27 -08001363 onPause();
1364 mPausedByClient = true;
Andreas Huberb4082222011-01-20 15:23:04 -08001365 break;
1366 }
1367
Andreas Huberb5f25f02013-02-05 10:14:26 -08001368 case kWhatSourceNotify:
1369 {
Andreas Huber9575c962013-02-05 13:59:56 -08001370 onSourceNotify(msg);
Andreas Huberb5f25f02013-02-05 10:14:26 -08001371 break;
1372 }
1373
Chong Zhanga7fa1d92014-06-11 14:49:23 -07001374 case kWhatClosedCaptionNotify:
1375 {
1376 onClosedCaptionNotify(msg);
1377 break;
1378 }
1379
Hassan Shojaniacefac142017-02-06 21:02:02 -08001380 case kWhatPrepareDrm:
1381 {
1382 status_t status = onPrepareDrm(msg);
1383
1384 sp<AMessage> response = new AMessage;
1385 response->setInt32("status", status);
1386 sp<AReplyToken> replyID;
1387 CHECK(msg->senderAwaitsResponse(&replyID));
1388 response->postReply(replyID);
1389 break;
1390 }
1391
1392 case kWhatReleaseDrm:
1393 {
1394 status_t status = onReleaseDrm();
1395
1396 sp<AMessage> response = new AMessage;
1397 response->setInt32("status", status);
1398 sp<AReplyToken> replyID;
1399 CHECK(msg->senderAwaitsResponse(&replyID));
1400 response->postReply(replyID);
1401 break;
1402 }
1403
Andreas Huberf9334412010-12-15 15:17:42 -08001404 default:
1405 TRESPASS();
1406 break;
1407 }
1408}
1409
Wei Jia94211742014-10-28 17:09:06 -07001410void NuPlayer::onResume() {
Ronghua Wu64c2d172015-10-07 16:52:19 -07001411 if (!mPaused || mResetting) {
1412 ALOGD_IF(mResetting, "resetting, onResume discarded");
Chong Zhangefbb6192015-01-30 17:13:27 -08001413 return;
1414 }
1415 mPaused = false;
Wei Jia94211742014-10-28 17:09:06 -07001416 if (mSource != NULL) {
1417 mSource->resume();
1418 } else {
1419 ALOGW("resume called when source is gone or not set");
1420 }
1421 // |mAudioDecoder| may have been released due to the pause timeout, so re-create it if
1422 // needed.
1423 if (audioDecoderStillNeeded() && mAudioDecoder == NULL) {
1424 instantiateDecoder(true /* audio */, &mAudioDecoder);
1425 }
1426 if (mRenderer != NULL) {
1427 mRenderer->resume();
1428 } else {
1429 ALOGW("resume called when renderer is gone or not set");
1430 }
Ray Essickd4d00612017-01-03 09:36:27 -08001431
1432 mLastStartedPlayingTimeNs = systemTime();
Wei Jia94211742014-10-28 17:09:06 -07001433}
1434
Lajos Molnarfcd3e942015-03-31 10:06:48 -07001435status_t NuPlayer::onInstantiateSecureDecoders() {
1436 status_t err;
1437 if (!(mSourceFlags & Source::FLAG_SECURE)) {
1438 return BAD_TYPE;
1439 }
1440
1441 if (mRenderer != NULL) {
1442 ALOGE("renderer should not be set when instantiating secure decoders");
1443 return UNKNOWN_ERROR;
1444 }
1445
1446 // TRICKY: We rely on mRenderer being null, so that decoder does not start requesting
1447 // data on instantiation.
Lajos Molnar1de1e252015-04-30 18:18:34 -07001448 if (mSurface != NULL) {
Lajos Molnarfcd3e942015-03-31 10:06:48 -07001449 err = instantiateDecoder(false, &mVideoDecoder);
1450 if (err != OK) {
1451 return err;
1452 }
1453 }
1454
1455 if (mAudioSink != NULL) {
1456 err = instantiateDecoder(true, &mAudioDecoder);
1457 if (err != OK) {
1458 return err;
1459 }
1460 }
1461 return OK;
1462}
1463
Wei Jiac5de0912016-11-18 10:22:14 -08001464void NuPlayer::onStart(int64_t startPositionUs, MediaPlayerSeekMode mode) {
Hassan Shojaniacefac142017-02-06 21:02:02 -08001465 ALOGV("onStart: mCrypto: %p (%d)", mCrypto.get(),
1466 (mCrypto != NULL ? mCrypto->getStrongCount() : 0));
1467
Robert Shih0c61a0d2015-07-06 15:09:10 -07001468 if (!mSourceStarted) {
1469 mSourceStarted = true;
1470 mSource->start();
1471 }
1472 if (startPositionUs > 0) {
Wei Jiac5de0912016-11-18 10:22:14 -08001473 performSeek(startPositionUs, mode);
Robert Shih0c61a0d2015-07-06 15:09:10 -07001474 if (mSource->getFormat(false /* audio */) == NULL) {
1475 return;
1476 }
1477 }
1478
Wei Jia94211742014-10-28 17:09:06 -07001479 mOffloadAudio = false;
1480 mAudioEOS = false;
1481 mVideoEOS = false;
Wei Jia94211742014-10-28 17:09:06 -07001482 mStarted = true;
Wei Jiafe8fe7d2016-06-08 10:29:25 -07001483 mPaused = false;
Wei Jia94211742014-10-28 17:09:06 -07001484
Wei Jia94211742014-10-28 17:09:06 -07001485 uint32_t flags = 0;
1486
1487 if (mSource->isRealTime()) {
1488 flags |= Renderer::FLAG_REAL_TIME;
1489 }
1490
Wei Jia9737d342016-12-28 14:59:59 -08001491 bool hasAudio = (mSource->getFormat(true /* audio */) != NULL);
1492 bool hasVideo = (mSource->getFormat(false /* audio */) != NULL);
1493 if (!hasAudio && !hasVideo) {
Wei Jia1de83d52016-10-10 10:15:03 -07001494 ALOGE("no metadata for either audio or video source");
1495 mSource->stop();
1496 mSourceStarted = false;
1497 notifyListener(MEDIA_ERROR, MEDIA_ERROR_UNKNOWN, ERROR_MALFORMED);
1498 return;
1499 }
Wei Jia9737d342016-12-28 14:59:59 -08001500 ALOGV_IF(!hasAudio, "no metadata for audio source"); // video only stream
1501
1502 sp<MetaData> audioMeta = mSource->getFormatMeta(true /* audio */);
Wei Jia1de83d52016-10-10 10:15:03 -07001503
Wei Jia94211742014-10-28 17:09:06 -07001504 audio_stream_type_t streamType = AUDIO_STREAM_MUSIC;
1505 if (mAudioSink != NULL) {
1506 streamType = mAudioSink->getAudioStreamType();
1507 }
1508
Wei Jia94211742014-10-28 17:09:06 -07001509 mOffloadAudio =
Wei Jia9737d342016-12-28 14:59:59 -08001510 canOffloadStream(audioMeta, hasVideo, mSource->isStreaming(), streamType)
Wei Jiabf70feb2016-02-19 15:47:16 -08001511 && (mPlaybackSettings.mSpeed == 1.f && mPlaybackSettings.mPitch == 1.f);
Hassan Shojaniacefac142017-02-06 21:02:02 -08001512
1513 // Modular DRM: Disabling audio offload if the source is protected
1514 if (mOffloadAudio && mIsDrmProtected) {
1515 mOffloadAudio = false;
1516 ALOGV("onStart: Disabling mOffloadAudio now that the source is protected.");
1517 }
1518
Wei Jia94211742014-10-28 17:09:06 -07001519 if (mOffloadAudio) {
1520 flags |= Renderer::FLAG_OFFLOAD_AUDIO;
1521 }
1522
Lajos Molnar1d15ab52015-03-04 16:46:34 -08001523 sp<AMessage> notify = new AMessage(kWhatRendererNotify, this);
Wei Jia94211742014-10-28 17:09:06 -07001524 ++mRendererGeneration;
1525 notify->setInt32("generation", mRendererGeneration);
1526 mRenderer = new Renderer(mAudioSink, notify, flags);
Wei Jia94211742014-10-28 17:09:06 -07001527 mRendererLooper = new ALooper;
1528 mRendererLooper->setName("NuPlayerRenderer");
1529 mRendererLooper->start(false, false, ANDROID_PRIORITY_AUDIO);
1530 mRendererLooper->registerHandler(mRenderer);
Lajos Molnar3a474aa2015-04-24 17:10:07 -07001531
1532 status_t err = mRenderer->setPlaybackSettings(mPlaybackSettings);
1533 if (err != OK) {
1534 mSource->stop();
Robert Shih0c61a0d2015-07-06 15:09:10 -07001535 mSourceStarted = false;
Lajos Molnar3a474aa2015-04-24 17:10:07 -07001536 notifyListener(MEDIA_ERROR, MEDIA_ERROR_UNKNOWN, err);
1537 return;
Wei Jiac8206ff2015-03-04 13:59:37 -08001538 }
Wei Jia94211742014-10-28 17:09:06 -07001539
Ronghua Wuc8a70d32015-04-29 16:26:34 -07001540 float rate = getFrameRate();
1541 if (rate > 0) {
Wei Jia94211742014-10-28 17:09:06 -07001542 mRenderer->setVideoFrameRate(rate);
1543 }
1544
Wei Jiac6cfd702014-11-11 16:33:20 -08001545 if (mVideoDecoder != NULL) {
1546 mVideoDecoder->setRenderer(mRenderer);
1547 }
1548 if (mAudioDecoder != NULL) {
1549 mAudioDecoder->setRenderer(mRenderer);
1550 }
1551
Ray Essickd4d00612017-01-03 09:36:27 -08001552 mLastStartedPlayingTimeNs = systemTime();
1553
Wei Jia94211742014-10-28 17:09:06 -07001554 postScanSources();
1555}
1556
Chong Zhangefbb6192015-01-30 17:13:27 -08001557void NuPlayer::onPause() {
1558 if (mPaused) {
1559 return;
1560 }
1561 mPaused = true;
1562 if (mSource != NULL) {
1563 mSource->pause();
1564 } else {
1565 ALOGW("pause called when source is gone or not set");
1566 }
1567 if (mRenderer != NULL) {
1568 mRenderer->pause();
1569 } else {
1570 ALOGW("pause called when renderer is gone or not set");
1571 }
Ray Essickd4d00612017-01-03 09:36:27 -08001572
1573 sp<NuPlayerDriver> driver = mDriver.promote();
1574 if (driver != NULL) {
1575 int64_t now = systemTime();
1576 int64_t played = now - mLastStartedPlayingTimeNs;
Ray Essickd4d00612017-01-03 09:36:27 -08001577
1578 driver->notifyMorePlayingTimeUs((played+500)/1000);
1579 }
Chong Zhangefbb6192015-01-30 17:13:27 -08001580}
1581
Ronghua Wud7988b12014-10-03 15:19:10 -07001582bool NuPlayer::audioDecoderStillNeeded() {
1583 // Audio decoder is no longer needed if it's in shut/shutting down status.
1584 return ((mFlushingAudio != SHUT_DOWN) && (mFlushingAudio != SHUTTING_DOWN_DECODER));
1585}
1586
Andy Hung8d121d42014-10-03 09:53:53 -07001587void NuPlayer::handleFlushComplete(bool audio, bool isDecoder) {
1588 // We wait for both the decoder flush and the renderer flush to complete
1589 // before entering either the FLUSHED or the SHUTTING_DOWN_DECODER state.
1590
1591 mFlushComplete[audio][isDecoder] = true;
1592 if (!mFlushComplete[audio][!isDecoder]) {
1593 return;
1594 }
1595
1596 FlushStatus *state = audio ? &mFlushingAudio : &mFlushingVideo;
1597 switch (*state) {
1598 case FLUSHING_DECODER:
1599 {
1600 *state = FLUSHED;
Andy Hung8d121d42014-10-03 09:53:53 -07001601 break;
1602 }
1603
1604 case FLUSHING_DECODER_SHUTDOWN:
1605 {
1606 *state = SHUTTING_DOWN_DECODER;
1607
1608 ALOGV("initiating %s decoder shutdown", audio ? "audio" : "video");
Andy Hung8d121d42014-10-03 09:53:53 -07001609 getDecoder(audio)->initiateShutdown();
1610 break;
1611 }
1612
1613 default:
1614 // decoder flush completes only occur in a flushing state.
1615 LOG_ALWAYS_FATAL_IF(isDecoder, "decoder flush in invalid state %d", *state);
1616 break;
1617 }
1618}
1619
Andreas Huber3831a062010-12-21 10:22:33 -08001620void NuPlayer::finishFlushIfPossible() {
Wei Jia53904f32014-07-29 10:22:53 -07001621 if (mFlushingAudio != NONE && mFlushingAudio != FLUSHED
1622 && mFlushingAudio != SHUT_DOWN) {
Andreas Huber3831a062010-12-21 10:22:33 -08001623 return;
1624 }
1625
Wei Jia53904f32014-07-29 10:22:53 -07001626 if (mFlushingVideo != NONE && mFlushingVideo != FLUSHED
1627 && mFlushingVideo != SHUT_DOWN) {
Andreas Huber3831a062010-12-21 10:22:33 -08001628 return;
1629 }
1630
Steve Block3856b092011-10-20 11:56:00 +01001631 ALOGV("both audio and video are flushed now.");
Andreas Huber3831a062010-12-21 10:22:33 -08001632
Andreas Huber3831a062010-12-21 10:22:33 -08001633 mFlushingAudio = NONE;
1634 mFlushingVideo = NONE;
Andreas Huber3831a062010-12-21 10:22:33 -08001635
Andy Hung8d121d42014-10-03 09:53:53 -07001636 clearFlushComplete();
1637
Andreas Hubera1f8ab02012-11-30 10:53:22 -08001638 processDeferredActions();
Andreas Huber1aef2112011-01-04 14:01:29 -08001639}
1640
1641void NuPlayer::postScanSources() {
1642 if (mScanSourcesPending) {
1643 return;
1644 }
1645
Lajos Molnar1d15ab52015-03-04 16:46:34 -08001646 sp<AMessage> msg = new AMessage(kWhatScanSources, this);
Andreas Huber1aef2112011-01-04 14:01:29 -08001647 msg->setInt32("generation", mScanSourcesGeneration);
1648 msg->post();
1649
1650 mScanSourcesPending = true;
1651}
1652
Wei Jia41cd4632016-05-13 10:53:30 -07001653void NuPlayer::tryOpenAudioSinkForOffload(
1654 const sp<AMessage> &format, const sp<MetaData> &audioMeta, bool hasVideo) {
Andy Hung202bce12014-12-03 11:47:36 -08001655 // Note: This is called early in NuPlayer to determine whether offloading
1656 // is possible; otherwise the decoders call the renderer openAudioSink directly.
Andy Hung282a7e32014-08-14 15:56:34 -07001657
Andy Hung202bce12014-12-03 11:47:36 -08001658 status_t err = mRenderer->openAudioSink(
Dhananjay Kumarc387f2b2015-08-06 10:43:16 +05301659 format, true /* offloadOnly */, hasVideo,
1660 AUDIO_OUTPUT_FLAG_NONE, &mOffloadAudio, mSource->isStreaming());
Andy Hung202bce12014-12-03 11:47:36 -08001661 if (err != OK) {
1662 // Any failure we turn off mOffloadAudio.
1663 mOffloadAudio = false;
1664 } else if (mOffloadAudio) {
Chong Zhang3b9eb1f2014-10-15 17:05:08 -07001665 sendMetaDataToHal(mAudioSink, audioMeta);
Andy Hung282a7e32014-08-14 15:56:34 -07001666 }
1667}
1668
1669void NuPlayer::closeAudioSink() {
Chong Zhang3b9eb1f2014-10-15 17:05:08 -07001670 mRenderer->closeAudioSink();
Andy Hung282a7e32014-08-14 15:56:34 -07001671}
1672
Wei Jia5031b2f2016-02-25 11:19:31 -08001673void NuPlayer::restartAudio(
Wei Jiabf70feb2016-02-19 15:47:16 -08001674 int64_t currentPositionUs, bool forceNonOffload, bool needsToCreateAudioDecoder) {
Wei Jiaa05f1e32016-03-25 16:31:22 -07001675 if (mAudioDecoder != NULL) {
1676 mAudioDecoder->pause();
1677 mAudioDecoder.clear();
Wei Jia686e8e52017-04-03 14:08:01 -07001678 mAudioDecoderError = false;
Wei Jiaa05f1e32016-03-25 16:31:22 -07001679 ++mAudioDecoderGeneration;
1680 }
Wei Jiabf70feb2016-02-19 15:47:16 -08001681 if (mFlushingAudio == FLUSHING_DECODER) {
1682 mFlushComplete[1 /* audio */][1 /* isDecoder */] = true;
1683 mFlushingAudio = FLUSHED;
1684 finishFlushIfPossible();
1685 } else if (mFlushingAudio == FLUSHING_DECODER_SHUTDOWN
1686 || mFlushingAudio == SHUTTING_DOWN_DECODER) {
1687 mFlushComplete[1 /* audio */][1 /* isDecoder */] = true;
1688 mFlushingAudio = SHUT_DOWN;
1689 finishFlushIfPossible();
1690 needsToCreateAudioDecoder = false;
1691 }
1692 if (mRenderer == NULL) {
1693 return;
1694 }
1695 closeAudioSink();
1696 mRenderer->flush(true /* audio */, false /* notifyComplete */);
1697 if (mVideoDecoder != NULL) {
1698 mRenderer->flush(false /* audio */, false /* notifyComplete */);
1699 }
1700
Wei Jiac5de0912016-11-18 10:22:14 -08001701 performSeek(currentPositionUs, MediaPlayerSeekMode::SEEK_PREVIOUS_SYNC /* mode */);
Wei Jiabf70feb2016-02-19 15:47:16 -08001702
1703 if (forceNonOffload) {
1704 mRenderer->signalDisableOffloadAudio();
1705 mOffloadAudio = false;
1706 }
1707 if (needsToCreateAudioDecoder) {
Wei Jiaa05f1e32016-03-25 16:31:22 -07001708 instantiateDecoder(true /* audio */, &mAudioDecoder, !forceNonOffload);
Wei Jiabf70feb2016-02-19 15:47:16 -08001709 }
1710}
1711
Wei Jia41cd4632016-05-13 10:53:30 -07001712void NuPlayer::determineAudioModeChange(const sp<AMessage> &audioFormat) {
Wei Jiae4d18c72015-07-13 17:58:11 -07001713 if (mSource == NULL || mAudioSink == NULL) {
1714 return;
1715 }
1716
1717 if (mRenderer == NULL) {
1718 ALOGW("No renderer can be used to determine audio mode. Use non-offload for safety.");
1719 mOffloadAudio = false;
1720 return;
1721 }
1722
1723 sp<MetaData> audioMeta = mSource->getFormatMeta(true /* audio */);
1724 sp<AMessage> videoFormat = mSource->getFormat(false /* audio */);
1725 audio_stream_type_t streamType = mAudioSink->getAudioStreamType();
1726 const bool hasVideo = (videoFormat != NULL);
Hassan Shojaniacefac142017-02-06 21:02:02 -08001727 bool canOffload = canOffloadStream(
Wei Jiabf70feb2016-02-19 15:47:16 -08001728 audioMeta, hasVideo, mSource->isStreaming(), streamType)
1729 && (mPlaybackSettings.mSpeed == 1.f && mPlaybackSettings.mPitch == 1.f);
Hassan Shojaniacefac142017-02-06 21:02:02 -08001730
1731 // Modular DRM: Disabling audio offload if the source is protected
1732 if (canOffload && mIsDrmProtected) {
1733 canOffload = false;
1734 ALOGV("determineAudioModeChange: Disabling mOffloadAudio b/c the source is protected.");
1735 }
1736
Wei Jiae4d18c72015-07-13 17:58:11 -07001737 if (canOffload) {
1738 if (!mOffloadAudio) {
1739 mRenderer->signalEnableOffloadAudio();
1740 }
1741 // open audio sink early under offload mode.
Wei Jia41cd4632016-05-13 10:53:30 -07001742 tryOpenAudioSinkForOffload(audioFormat, audioMeta, hasVideo);
Wei Jiae4d18c72015-07-13 17:58:11 -07001743 } else {
Robert Shihe1d70192015-07-23 17:54:13 -07001744 if (mOffloadAudio) {
1745 mRenderer->signalDisableOffloadAudio();
1746 mOffloadAudio = false;
1747 }
Wei Jiae4d18c72015-07-13 17:58:11 -07001748 }
1749}
1750
Wei Jiaa05f1e32016-03-25 16:31:22 -07001751status_t NuPlayer::instantiateDecoder(
1752 bool audio, sp<DecoderBase> *decoder, bool checkAudioModeChange) {
Wei Jia566da802015-08-27 13:59:30 -07001753 // The audio decoder could be cleared by tear down. If still in shut down
1754 // process, no need to create a new audio decoder.
1755 if (*decoder != NULL || (audio && mFlushingAudio == SHUT_DOWN)) {
Andreas Huberf9334412010-12-15 15:17:42 -08001756 return OK;
1757 }
1758
Andreas Huber84066782011-08-16 09:34:26 -07001759 sp<AMessage> format = mSource->getFormat(audio);
Andreas Huberf9334412010-12-15 15:17:42 -08001760
Andreas Huber84066782011-08-16 09:34:26 -07001761 if (format == NULL) {
Robert Shih7350b052015-10-01 15:50:14 -07001762 return UNKNOWN_ERROR;
1763 } else {
1764 status_t err;
1765 if (format->findInt32("err", &err) && err) {
1766 return err;
1767 }
Andreas Huberf9334412010-12-15 15:17:42 -08001768 }
1769
Ronghua Wu8db88132015-04-22 13:51:35 -07001770 format->setInt32("priority", 0 /* realtime */);
1771
Andreas Huber3fe62152011-09-16 15:09:22 -07001772 if (!audio) {
Andreas Huber84066782011-08-16 09:34:26 -07001773 AString mime;
1774 CHECK(format->findString("mime", &mime));
Chong Zhanga7fa1d92014-06-11 14:49:23 -07001775
Lajos Molnar1d15ab52015-03-04 16:46:34 -08001776 sp<AMessage> ccNotify = new AMessage(kWhatClosedCaptionNotify, this);
Chong Zhang341ab6e2015-02-04 13:37:18 -08001777 if (mCCDecoder == NULL) {
1778 mCCDecoder = new CCDecoder(ccNotify);
1779 }
Lajos Molnar09524832014-07-17 14:29:51 -07001780
1781 if (mSourceFlags & Source::FLAG_SECURE) {
1782 format->setInt32("secure", true);
1783 }
Chong Zhang17134602015-01-07 16:14:34 -08001784
1785 if (mSourceFlags & Source::FLAG_PROTECTED) {
1786 format->setInt32("protected", true);
1787 }
Ronghua Wu8db88132015-04-22 13:51:35 -07001788
Ronghua Wuc8a70d32015-04-29 16:26:34 -07001789 float rate = getFrameRate();
1790 if (rate > 0) {
Lajos Molnar3a474aa2015-04-24 17:10:07 -07001791 format->setFloat("operating-rate", rate * mPlaybackSettings.mSpeed);
Ronghua Wu8db88132015-04-22 13:51:35 -07001792 }
Andreas Huber3fe62152011-09-16 15:09:22 -07001793 }
1794
Wei Jiabc2fb722014-07-08 16:37:57 -07001795 if (audio) {
Lajos Molnar1d15ab52015-03-04 16:46:34 -08001796 sp<AMessage> notify = new AMessage(kWhatAudioNotify, this);
Wei Jia88703c32014-08-06 11:24:07 -07001797 ++mAudioDecoderGeneration;
1798 notify->setInt32("generation", mAudioDecoderGeneration);
1799
Wei Jiaa05f1e32016-03-25 16:31:22 -07001800 if (checkAudioModeChange) {
Wei Jia41cd4632016-05-13 10:53:30 -07001801 determineAudioModeChange(format);
Wei Jiaa05f1e32016-03-25 16:31:22 -07001802 }
Wei Jiabc2fb722014-07-08 16:37:57 -07001803 if (mOffloadAudio) {
Wei Jia14532f22015-12-29 11:28:15 -08001804 mSource->setOffloadAudio(true /* offload */);
1805
Haynes Mathew George8b635332015-03-30 17:59:47 -07001806 const bool hasVideo = (mSource->getFormat(false /*audio */) != NULL);
1807 format->setInt32("has-video", hasVideo);
Wei Jiac6cfd702014-11-11 16:33:20 -08001808 *decoder = new DecoderPassThrough(notify, mSource, mRenderer);
Hassan Shojaniacefac142017-02-06 21:02:02 -08001809 ALOGV("instantiateDecoder audio DecoderPassThrough hasVideo: %d", hasVideo);
Wei Jiabc2fb722014-07-08 16:37:57 -07001810 } else {
Wei Jia14532f22015-12-29 11:28:15 -08001811 mSource->setOffloadAudio(false /* offload */);
1812
Wei Jiaf2ae3e12016-10-27 17:10:59 -07001813 *decoder = new Decoder(notify, mSource, mPID, mUID, mRenderer);
Hassan Shojaniacefac142017-02-06 21:02:02 -08001814 ALOGV("instantiateDecoder audio Decoder");
Wei Jiabc2fb722014-07-08 16:37:57 -07001815 }
Wei Jia686e8e52017-04-03 14:08:01 -07001816 mAudioDecoderError = false;
Wei Jiabc2fb722014-07-08 16:37:57 -07001817 } else {
Lajos Molnar1d15ab52015-03-04 16:46:34 -08001818 sp<AMessage> notify = new AMessage(kWhatVideoNotify, this);
Wei Jia88703c32014-08-06 11:24:07 -07001819 ++mVideoDecoderGeneration;
1820 notify->setInt32("generation", mVideoDecoderGeneration);
1821
Chong Zhang7137ec72014-11-12 16:41:05 -08001822 *decoder = new Decoder(
Wei Jiaf2ae3e12016-10-27 17:10:59 -07001823 notify, mSource, mPID, mUID, mRenderer, mSurface, mCCDecoder);
Wei Jia686e8e52017-04-03 14:08:01 -07001824 mVideoDecoderError = false;
Lajos Molnard9fd6312014-11-06 11:00:00 -08001825
1826 // enable FRC if high-quality AV sync is requested, even if not
Lajos Molnar1de1e252015-04-30 18:18:34 -07001827 // directly queuing to display, as this will even improve textureview
Lajos Molnard9fd6312014-11-06 11:00:00 -08001828 // playback.
1829 {
Marco Nelissen96626b72016-12-01 13:58:59 -08001830 if (property_get_bool("persist.sys.media.avsync", false)) {
Lajos Molnard9fd6312014-11-06 11:00:00 -08001831 format->setInt32("auto-frc", 1);
1832 }
1833 }
Wei Jiabc2fb722014-07-08 16:37:57 -07001834 }
Lajos Molnar1cd13982014-01-17 15:12:51 -08001835 (*decoder)->init();
Hassan Shojaniacefac142017-02-06 21:02:02 -08001836
1837 // Modular DRM
1838 if (mIsDrmProtected) {
1839 format->setPointer("crypto", mCrypto.get());
1840 ALOGV("instantiateDecoder: mCrypto: %p (%d) isSecure: %d", mCrypto.get(),
1841 (mCrypto != NULL ? mCrypto->getStrongCount() : 0),
1842 (mSourceFlags & Source::FLAG_SECURE) != 0);
1843 }
1844
Andreas Huber84066782011-08-16 09:34:26 -07001845 (*decoder)->configure(format);
Andreas Huberf9334412010-12-15 15:17:42 -08001846
Praveen Chavanbbaa1442016-04-08 13:33:49 -07001847 if (!audio) {
1848 sp<AMessage> params = new AMessage();
1849 float rate = getFrameRate();
1850 if (rate > 0) {
1851 params->setFloat("frame-rate-total", rate);
1852 }
1853
1854 sp<MetaData> fileMeta = getFileMeta();
1855 if (fileMeta != NULL) {
1856 int32_t videoTemporalLayerCount;
1857 if (fileMeta->findInt32(kKeyTemporalLayerCount, &videoTemporalLayerCount)
1858 && videoTemporalLayerCount > 0) {
1859 params->setInt32("temporal-layer-count", videoTemporalLayerCount);
1860 }
1861 }
1862
1863 if (params->countEntries() > 0) {
1864 (*decoder)->setParameters(params);
1865 }
1866 }
Andreas Huberf9334412010-12-15 15:17:42 -08001867 return OK;
1868}
1869
Chong Zhangced1c2f2014-08-08 15:22:35 -07001870void NuPlayer::updateVideoSize(
1871 const sp<AMessage> &inputFormat,
1872 const sp<AMessage> &outputFormat) {
1873 if (inputFormat == NULL) {
1874 ALOGW("Unknown video size, reporting 0x0!");
1875 notifyListener(MEDIA_SET_VIDEO_SIZE, 0, 0);
1876 return;
1877 }
Wei Jia9737d342016-12-28 14:59:59 -08001878 int32_t err = OK;
1879 inputFormat->findInt32("err", &err);
1880 if (err == -EWOULDBLOCK) {
1881 ALOGW("Video meta is not available yet!");
1882 return;
1883 }
1884 if (err != OK) {
1885 ALOGW("Something is wrong with video meta!");
1886 return;
1887 }
Chong Zhangced1c2f2014-08-08 15:22:35 -07001888
1889 int32_t displayWidth, displayHeight;
Chong Zhangced1c2f2014-08-08 15:22:35 -07001890 if (outputFormat != NULL) {
1891 int32_t width, height;
1892 CHECK(outputFormat->findInt32("width", &width));
1893 CHECK(outputFormat->findInt32("height", &height));
1894
1895 int32_t cropLeft, cropTop, cropRight, cropBottom;
1896 CHECK(outputFormat->findRect(
1897 "crop",
1898 &cropLeft, &cropTop, &cropRight, &cropBottom));
1899
1900 displayWidth = cropRight - cropLeft + 1;
1901 displayHeight = cropBottom - cropTop + 1;
1902
1903 ALOGV("Video output format changed to %d x %d "
1904 "(crop: %d x %d @ (%d, %d))",
1905 width, height,
1906 displayWidth,
1907 displayHeight,
1908 cropLeft, cropTop);
1909 } else {
1910 CHECK(inputFormat->findInt32("width", &displayWidth));
1911 CHECK(inputFormat->findInt32("height", &displayHeight));
1912
1913 ALOGV("Video input format %d x %d", displayWidth, displayHeight);
1914 }
1915
1916 // Take into account sample aspect ratio if necessary:
1917 int32_t sarWidth, sarHeight;
1918 if (inputFormat->findInt32("sar-width", &sarWidth)
Wei Jia095bc252017-01-27 10:16:16 -08001919 && inputFormat->findInt32("sar-height", &sarHeight)
1920 && sarWidth > 0 && sarHeight > 0) {
Chong Zhangced1c2f2014-08-08 15:22:35 -07001921 ALOGV("Sample aspect ratio %d : %d", sarWidth, sarHeight);
1922
1923 displayWidth = (displayWidth * sarWidth) / sarHeight;
1924
1925 ALOGV("display dimensions %d x %d", displayWidth, displayHeight);
Wei Jiadf6c6af2016-09-29 11:27:15 -07001926 } else {
1927 int32_t width, height;
1928 if (inputFormat->findInt32("display-width", &width)
1929 && inputFormat->findInt32("display-height", &height)
1930 && width > 0 && height > 0
1931 && displayWidth > 0 && displayHeight > 0) {
1932 if (displayHeight * (int64_t)width / height > (int64_t)displayWidth) {
1933 displayHeight = (int32_t)(displayWidth * (int64_t)height / width);
1934 } else {
1935 displayWidth = (int32_t)(displayHeight * (int64_t)width / height);
1936 }
1937 ALOGV("Video display width and height are overridden to %d x %d",
1938 displayWidth, displayHeight);
1939 }
Chong Zhangced1c2f2014-08-08 15:22:35 -07001940 }
1941
1942 int32_t rotationDegrees;
1943 if (!inputFormat->findInt32("rotation-degrees", &rotationDegrees)) {
1944 rotationDegrees = 0;
1945 }
1946
1947 if (rotationDegrees == 90 || rotationDegrees == 270) {
1948 int32_t tmp = displayWidth;
1949 displayWidth = displayHeight;
1950 displayHeight = tmp;
1951 }
1952
1953 notifyListener(
1954 MEDIA_SET_VIDEO_SIZE,
1955 displayWidth,
1956 displayHeight);
1957}
1958
Chong Zhangdcb89b32013-08-06 09:44:47 -07001959void NuPlayer::notifyListener(int msg, int ext1, int ext2, const Parcel *in) {
Andreas Huber43c3e6c2011-01-05 12:17:08 -08001960 if (mDriver == NULL) {
Andreas Huberf9334412010-12-15 15:17:42 -08001961 return;
1962 }
1963
Andreas Huber43c3e6c2011-01-05 12:17:08 -08001964 sp<NuPlayerDriver> driver = mDriver.promote();
Andreas Huberf9334412010-12-15 15:17:42 -08001965
Andreas Huber43c3e6c2011-01-05 12:17:08 -08001966 if (driver == NULL) {
Andreas Huberf9334412010-12-15 15:17:42 -08001967 return;
1968 }
1969
Chong Zhangdcb89b32013-08-06 09:44:47 -07001970 driver->notifyListener(msg, ext1, ext2, in);
Andreas Huberf9334412010-12-15 15:17:42 -08001971}
1972
Chong Zhang7137ec72014-11-12 16:41:05 -08001973void NuPlayer::flushDecoder(bool audio, bool needShutdown) {
Andreas Huber14f76722013-01-15 09:04:18 -08001974 ALOGV("[%s] flushDecoder needShutdown=%d",
1975 audio ? "audio" : "video", needShutdown);
1976
Chong Zhang7137ec72014-11-12 16:41:05 -08001977 const sp<DecoderBase> &decoder = getDecoder(audio);
Lajos Molnar87603c02014-08-20 19:25:30 -07001978 if (decoder == NULL) {
Steve Blockdf64d152012-01-04 20:05:49 +00001979 ALOGI("flushDecoder %s without decoder present",
Andreas Huber6e3d3112011-11-28 12:36:11 -08001980 audio ? "audio" : "video");
Lajos Molnar87603c02014-08-20 19:25:30 -07001981 return;
Andreas Huber6e3d3112011-11-28 12:36:11 -08001982 }
1983
Andreas Huber1aef2112011-01-04 14:01:29 -08001984 // Make sure we don't continue to scan sources until we finish flushing.
1985 ++mScanSourcesGeneration;
Chong Zhang3b032b32015-04-17 15:49:06 -07001986 if (mScanSourcesPending) {
Toshikazu Saitocbcbb792015-09-15 14:53:01 +09001987 if (!needShutdown) {
1988 mDeferredActions.push_back(
1989 new SimpleAction(&NuPlayer::performScanSources));
1990 }
Chong Zhang3b032b32015-04-17 15:49:06 -07001991 mScanSourcesPending = false;
1992 }
Andreas Huber1aef2112011-01-04 14:01:29 -08001993
Chong Zhang7137ec72014-11-12 16:41:05 -08001994 decoder->signalFlush();
Andreas Huber1aef2112011-01-04 14:01:29 -08001995
1996 FlushStatus newStatus =
1997 needShutdown ? FLUSHING_DECODER_SHUTDOWN : FLUSHING_DECODER;
1998
Lajos Molnarfcd3e942015-03-31 10:06:48 -07001999 mFlushComplete[audio][false /* isDecoder */] = (mRenderer == NULL);
Andy Hung8d121d42014-10-03 09:53:53 -07002000 mFlushComplete[audio][true /* isDecoder */] = false;
Andreas Huber1aef2112011-01-04 14:01:29 -08002001 if (audio) {
Wei Jia53904f32014-07-29 10:22:53 -07002002 ALOGE_IF(mFlushingAudio != NONE,
2003 "audio flushDecoder() is called in state %d", mFlushingAudio);
Andreas Huber1aef2112011-01-04 14:01:29 -08002004 mFlushingAudio = newStatus;
Andreas Huber1aef2112011-01-04 14:01:29 -08002005 } else {
Wei Jia53904f32014-07-29 10:22:53 -07002006 ALOGE_IF(mFlushingVideo != NONE,
2007 "video flushDecoder() is called in state %d", mFlushingVideo);
Andreas Huber1aef2112011-01-04 14:01:29 -08002008 mFlushingVideo = newStatus;
Andreas Huber1aef2112011-01-04 14:01:29 -08002009 }
2010}
2011
Chong Zhangced1c2f2014-08-08 15:22:35 -07002012void NuPlayer::queueDecoderShutdown(
2013 bool audio, bool video, const sp<AMessage> &reply) {
2014 ALOGI("queueDecoderShutdown audio=%d, video=%d", audio, video);
Andreas Huber84066782011-08-16 09:34:26 -07002015
Chong Zhangced1c2f2014-08-08 15:22:35 -07002016 mDeferredActions.push_back(
Wei Jiafef808d2014-10-31 17:57:05 -07002017 new FlushDecoderAction(
2018 audio ? FLUSH_CMD_SHUTDOWN : FLUSH_CMD_NONE,
2019 video ? FLUSH_CMD_SHUTDOWN : FLUSH_CMD_NONE));
Andreas Huber84066782011-08-16 09:34:26 -07002020
Chong Zhangced1c2f2014-08-08 15:22:35 -07002021 mDeferredActions.push_back(
2022 new SimpleAction(&NuPlayer::performScanSources));
Andreas Huber84066782011-08-16 09:34:26 -07002023
Chong Zhangced1c2f2014-08-08 15:22:35 -07002024 mDeferredActions.push_back(new PostMessageAction(reply));
2025
2026 processDeferredActions();
Andreas Huber84066782011-08-16 09:34:26 -07002027}
2028
James Dong0d268a32012-08-31 12:18:27 -07002029status_t NuPlayer::setVideoScalingMode(int32_t mode) {
2030 mVideoScalingMode = mode;
Lajos Molnar1de1e252015-04-30 18:18:34 -07002031 if (mSurface != NULL) {
2032 status_t ret = native_window_set_scaling_mode(mSurface.get(), mVideoScalingMode);
James Dong0d268a32012-08-31 12:18:27 -07002033 if (ret != OK) {
2034 ALOGE("Failed to set scaling mode (%d): %s",
2035 -ret, strerror(-ret));
2036 return ret;
2037 }
2038 }
2039 return OK;
2040}
2041
Chong Zhangdcb89b32013-08-06 09:44:47 -07002042status_t NuPlayer::getTrackInfo(Parcel* reply) const {
Lajos Molnar1d15ab52015-03-04 16:46:34 -08002043 sp<AMessage> msg = new AMessage(kWhatGetTrackInfo, this);
Chong Zhangdcb89b32013-08-06 09:44:47 -07002044 msg->setPointer("reply", reply);
2045
2046 sp<AMessage> response;
2047 status_t err = msg->postAndAwaitResponse(&response);
2048 return err;
2049}
2050
Robert Shih7c4f0d72014-07-09 18:53:31 -07002051status_t NuPlayer::getSelectedTrack(int32_t type, Parcel* reply) const {
Lajos Molnar1d15ab52015-03-04 16:46:34 -08002052 sp<AMessage> msg = new AMessage(kWhatGetSelectedTrack, this);
Robert Shih7c4f0d72014-07-09 18:53:31 -07002053 msg->setPointer("reply", reply);
2054 msg->setInt32("type", type);
2055
2056 sp<AMessage> response;
2057 status_t err = msg->postAndAwaitResponse(&response);
2058 if (err == OK && response != NULL) {
2059 CHECK(response->findInt32("err", &err));
2060 }
2061 return err;
2062}
2063
Robert Shih6ffb1fd2014-10-29 16:24:32 -07002064status_t NuPlayer::selectTrack(size_t trackIndex, bool select, int64_t timeUs) {
Lajos Molnar1d15ab52015-03-04 16:46:34 -08002065 sp<AMessage> msg = new AMessage(kWhatSelectTrack, this);
Chong Zhangdcb89b32013-08-06 09:44:47 -07002066 msg->setSize("trackIndex", trackIndex);
2067 msg->setInt32("select", select);
Robert Shih6ffb1fd2014-10-29 16:24:32 -07002068 msg->setInt64("timeUs", timeUs);
Chong Zhangdcb89b32013-08-06 09:44:47 -07002069
2070 sp<AMessage> response;
2071 status_t err = msg->postAndAwaitResponse(&response);
2072
Chong Zhang404fced2014-06-11 14:45:31 -07002073 if (err != OK) {
2074 return err;
2075 }
2076
2077 if (!response->findInt32("err", &err)) {
2078 err = OK;
2079 }
2080
Chong Zhangdcb89b32013-08-06 09:44:47 -07002081 return err;
2082}
2083
Ronghua Wua73d9e02014-10-08 15:13:29 -07002084status_t NuPlayer::getCurrentPosition(int64_t *mediaUs) {
2085 sp<Renderer> renderer = mRenderer;
2086 if (renderer == NULL) {
2087 return NO_INIT;
2088 }
2089
2090 return renderer->getCurrentPosition(mediaUs);
2091}
2092
Praveen Chavane1e5d7a2015-05-19 19:09:48 -07002093void NuPlayer::getStats(Vector<sp<AMessage> > *mTrackStats) {
2094 CHECK(mTrackStats != NULL);
2095
2096 mTrackStats->clear();
2097 if (mVideoDecoder != NULL) {
2098 mTrackStats->push_back(mVideoDecoder->getStats());
2099 }
2100 if (mAudioDecoder != NULL) {
2101 mTrackStats->push_back(mAudioDecoder->getStats());
Chong Zhang7137ec72014-11-12 16:41:05 -08002102 }
Ronghua Wua73d9e02014-10-08 15:13:29 -07002103}
2104
Marco Nelissenf0b72b52014-09-16 15:43:44 -07002105sp<MetaData> NuPlayer::getFileMeta() {
2106 return mSource->getFileFormatMeta();
2107}
2108
Ronghua Wuc8a70d32015-04-29 16:26:34 -07002109float NuPlayer::getFrameRate() {
2110 sp<MetaData> meta = mSource->getFormatMeta(false /* audio */);
2111 if (meta == NULL) {
2112 return 0;
2113 }
2114 int32_t rate;
2115 if (!meta->findInt32(kKeyFrameRate, &rate)) {
2116 // fall back to try file meta
2117 sp<MetaData> fileMeta = getFileMeta();
2118 if (fileMeta == NULL) {
2119 ALOGW("source has video meta but not file meta");
2120 return -1;
2121 }
2122 int32_t fileMetaRate;
2123 if (!fileMeta->findInt32(kKeyFrameRate, &fileMetaRate)) {
2124 return -1;
2125 }
2126 return fileMetaRate;
2127 }
2128 return rate;
2129}
2130
Andreas Huberb7c8e912012-11-27 15:02:53 -08002131void NuPlayer::schedulePollDuration() {
Lajos Molnar1d15ab52015-03-04 16:46:34 -08002132 sp<AMessage> msg = new AMessage(kWhatPollDuration, this);
Andreas Huberb7c8e912012-11-27 15:02:53 -08002133 msg->setInt32("generation", mPollDurationGeneration);
2134 msg->post();
2135}
2136
2137void NuPlayer::cancelPollDuration() {
2138 ++mPollDurationGeneration;
2139}
2140
Andreas Hubera1f8ab02012-11-30 10:53:22 -08002141void NuPlayer::processDeferredActions() {
2142 while (!mDeferredActions.empty()) {
2143 // We won't execute any deferred actions until we're no longer in
2144 // an intermediate state, i.e. one more more decoders are currently
2145 // flushing or shutting down.
2146
Andreas Hubera1f8ab02012-11-30 10:53:22 -08002147 if (mFlushingAudio != NONE || mFlushingVideo != NONE) {
2148 // We're currently flushing, postpone the reset until that's
2149 // completed.
2150
2151 ALOGV("postponing action mFlushingAudio=%d, mFlushingVideo=%d",
2152 mFlushingAudio, mFlushingVideo);
2153
2154 break;
2155 }
2156
2157 sp<Action> action = *mDeferredActions.begin();
2158 mDeferredActions.erase(mDeferredActions.begin());
2159
2160 action->execute(this);
2161 }
2162}
2163
Wei Jiac5de0912016-11-18 10:22:14 -08002164void NuPlayer::performSeek(int64_t seekTimeUs, MediaPlayerSeekMode mode) {
2165 ALOGV("performSeek seekTimeUs=%lld us (%.2f secs), mode=%d",
2166 (long long)seekTimeUs, seekTimeUs / 1E6, mode);
Andreas Hubera1f8ab02012-11-30 10:53:22 -08002167
Andy Hungadf34bf2014-09-03 18:22:22 -07002168 if (mSource == NULL) {
2169 // This happens when reset occurs right before the loop mode
2170 // asynchronously seeks to the start of the stream.
2171 LOG_ALWAYS_FATAL_IF(mAudioDecoder != NULL || mVideoDecoder != NULL,
2172 "mSource is NULL and decoders not NULL audio(%p) video(%p)",
2173 mAudioDecoder.get(), mVideoDecoder.get());
2174 return;
2175 }
Robert Shih1a5c8592015-08-04 18:07:44 -07002176 mPreviousSeekTimeUs = seekTimeUs;
Wei Jiac5de0912016-11-18 10:22:14 -08002177 mSource->seekTo(seekTimeUs, mode);
Robert Shihd3b0bbb2014-07-23 15:00:25 -07002178 ++mTimedTextGeneration;
Andreas Hubera1f8ab02012-11-30 10:53:22 -08002179
Andreas Hubera1f8ab02012-11-30 10:53:22 -08002180 // everything's flushed, continue playback.
2181}
2182
Wei Jiafef808d2014-10-31 17:57:05 -07002183void NuPlayer::performDecoderFlush(FlushCommand audio, FlushCommand video) {
2184 ALOGV("performDecoderFlush audio=%d, video=%d", audio, video);
Andreas Hubera1f8ab02012-11-30 10:53:22 -08002185
Wei Jiafef808d2014-10-31 17:57:05 -07002186 if ((audio == FLUSH_CMD_NONE || mAudioDecoder == NULL)
2187 && (video == FLUSH_CMD_NONE || mVideoDecoder == NULL)) {
Andreas Hubera1f8ab02012-11-30 10:53:22 -08002188 return;
2189 }
2190
Wei Jiafef808d2014-10-31 17:57:05 -07002191 if (audio != FLUSH_CMD_NONE && mAudioDecoder != NULL) {
2192 flushDecoder(true /* audio */, (audio == FLUSH_CMD_SHUTDOWN));
Andreas Hubera1f8ab02012-11-30 10:53:22 -08002193 }
2194
Wei Jiafef808d2014-10-31 17:57:05 -07002195 if (video != FLUSH_CMD_NONE && mVideoDecoder != NULL) {
2196 flushDecoder(false /* audio */, (video == FLUSH_CMD_SHUTDOWN));
Andreas Hubera1f8ab02012-11-30 10:53:22 -08002197 }
2198}
2199
2200void NuPlayer::performReset() {
2201 ALOGV("performReset");
2202
2203 CHECK(mAudioDecoder == NULL);
2204 CHECK(mVideoDecoder == NULL);
2205
2206 cancelPollDuration();
2207
2208 ++mScanSourcesGeneration;
2209 mScanSourcesPending = false;
2210
Lajos Molnar09524832014-07-17 14:29:51 -07002211 if (mRendererLooper != NULL) {
2212 if (mRenderer != NULL) {
2213 mRendererLooper->unregisterHandler(mRenderer->id());
2214 }
2215 mRendererLooper->stop();
2216 mRendererLooper.clear();
2217 }
Andreas Hubera1f8ab02012-11-30 10:53:22 -08002218 mRenderer.clear();
Wei Jia57568df2014-09-22 10:16:29 -07002219 ++mRendererGeneration;
Andreas Hubera1f8ab02012-11-30 10:53:22 -08002220
2221 if (mSource != NULL) {
2222 mSource->stop();
Andreas Huberb5f25f02013-02-05 10:14:26 -08002223
Wei Jiac45a4b22016-04-15 15:30:23 -07002224 Mutex::Autolock autoLock(mSourceLock);
Andreas Hubera1f8ab02012-11-30 10:53:22 -08002225 mSource.clear();
2226 }
2227
2228 if (mDriver != NULL) {
2229 sp<NuPlayerDriver> driver = mDriver.promote();
2230 if (driver != NULL) {
2231 driver->notifyResetComplete();
2232 }
2233 }
Andreas Huber57a339c2012-12-03 11:18:00 -08002234
2235 mStarted = false;
Wei Jia8a092d32016-06-03 14:57:24 -07002236 mPrepared = false;
Ronghua Wu64c2d172015-10-07 16:52:19 -07002237 mResetting = false;
Robert Shih0c61a0d2015-07-06 15:09:10 -07002238 mSourceStarted = false;
Hassan Shojaniacefac142017-02-06 21:02:02 -08002239
2240 // Modular DRM
2241 if (mCrypto != NULL) {
2242 // decoders will be flushed before this so their mCrypto would go away on their own
2243 // TODO change to ALOGV
2244 ALOGD("performReset mCrypto: %p (%d)", mCrypto.get(),
2245 (mCrypto != NULL ? mCrypto->getStrongCount() : 0));
2246 mCrypto.clear();
2247 }
2248 mIsDrmProtected = false;
Andreas Hubera1f8ab02012-11-30 10:53:22 -08002249}
2250
2251void NuPlayer::performScanSources() {
2252 ALOGV("performScanSources");
2253
Andreas Huber57a339c2012-12-03 11:18:00 -08002254 if (!mStarted) {
2255 return;
2256 }
2257
Andreas Hubera1f8ab02012-11-30 10:53:22 -08002258 if (mAudioDecoder == NULL || mVideoDecoder == NULL) {
2259 postScanSources();
2260 }
2261}
2262
Lajos Molnar1de1e252015-04-30 18:18:34 -07002263void NuPlayer::performSetSurface(const sp<Surface> &surface) {
Andreas Huber57a339c2012-12-03 11:18:00 -08002264 ALOGV("performSetSurface");
2265
Lajos Molnar1de1e252015-04-30 18:18:34 -07002266 mSurface = surface;
Andreas Huber57a339c2012-12-03 11:18:00 -08002267
2268 // XXX - ignore error from setVideoScalingMode for now
2269 setVideoScalingMode(mVideoScalingMode);
Chong Zhang13d6faa2014-08-22 15:35:28 -07002270
2271 if (mDriver != NULL) {
2272 sp<NuPlayerDriver> driver = mDriver.promote();
2273 if (driver != NULL) {
2274 driver->notifySetSurfaceComplete();
2275 }
2276 }
Andreas Huber57a339c2012-12-03 11:18:00 -08002277}
2278
Chong Zhangf8d71772014-11-26 15:08:34 -08002279void NuPlayer::performResumeDecoders(bool needNotify) {
2280 if (needNotify) {
2281 mResumePending = true;
2282 if (mVideoDecoder == NULL) {
2283 // if audio-only, we can notify seek complete now,
2284 // as the resume operation will be relatively fast.
2285 finishResume();
2286 }
2287 }
2288
Chong Zhang7137ec72014-11-12 16:41:05 -08002289 if (mVideoDecoder != NULL) {
Chong Zhangf8d71772014-11-26 15:08:34 -08002290 // When there is continuous seek, MediaPlayer will cache the seek
2291 // position, and send down new seek request when previous seek is
2292 // complete. Let's wait for at least one video output frame before
2293 // notifying seek complete, so that the video thumbnail gets updated
2294 // when seekbar is dragged.
2295 mVideoDecoder->signalResume(needNotify);
Chong Zhang7137ec72014-11-12 16:41:05 -08002296 }
2297
2298 if (mAudioDecoder != NULL) {
Chong Zhangf8d71772014-11-26 15:08:34 -08002299 mAudioDecoder->signalResume(false /* needNotify */);
2300 }
2301}
2302
2303void NuPlayer::finishResume() {
2304 if (mResumePending) {
2305 mResumePending = false;
Wei Jia1061c9c2015-05-19 16:02:17 -07002306 notifyDriverSeekComplete();
2307 }
2308}
2309
2310void NuPlayer::notifyDriverSeekComplete() {
2311 if (mDriver != NULL) {
2312 sp<NuPlayerDriver> driver = mDriver.promote();
2313 if (driver != NULL) {
2314 driver->notifySeekComplete();
Chong Zhangf8d71772014-11-26 15:08:34 -08002315 }
Chong Zhang7137ec72014-11-12 16:41:05 -08002316 }
2317}
2318
Andreas Huber9575c962013-02-05 13:59:56 -08002319void NuPlayer::onSourceNotify(const sp<AMessage> &msg) {
2320 int32_t what;
2321 CHECK(msg->findInt32("what", &what));
2322
2323 switch (what) {
Lajos Molnarfcd3e942015-03-31 10:06:48 -07002324 case Source::kWhatInstantiateSecureDecoders:
2325 {
2326 if (mSource == NULL) {
2327 // This is a stale notification from a source that was
2328 // asynchronously preparing when the client called reset().
2329 // We handled the reset, the source is gone.
2330 break;
2331 }
2332
2333 sp<AMessage> reply;
2334 CHECK(msg->findMessage("reply", &reply));
2335 status_t err = onInstantiateSecureDecoders();
2336 reply->setInt32("err", err);
2337 reply->post();
2338 break;
2339 }
2340
Andreas Huber9575c962013-02-05 13:59:56 -08002341 case Source::kWhatPrepared:
2342 {
Hassan Shojaniacefac142017-02-06 21:02:02 -08002343 ALOGV("NuPlayer::onSourceNotify Source::kWhatPrepared source: %p", mSource.get());
Andreas Huberb5f28d42013-04-25 15:11:19 -07002344 if (mSource == NULL) {
2345 // This is a stale notification from a source that was
2346 // asynchronously preparing when the client called reset().
2347 // We handled the reset, the source is gone.
2348 break;
2349 }
2350
Andreas Huberec0c5972013-02-05 14:47:13 -08002351 int32_t err;
2352 CHECK(msg->findInt32("err", &err));
2353
Lajos Molnarfcd3e942015-03-31 10:06:48 -07002354 if (err != OK) {
2355 // shut down potential secure codecs in case client never calls reset
2356 mDeferredActions.push_back(
2357 new FlushDecoderAction(FLUSH_CMD_SHUTDOWN /* audio */,
2358 FLUSH_CMD_SHUTDOWN /* video */));
2359 processDeferredActions();
Wei Jia8a092d32016-06-03 14:57:24 -07002360 } else {
2361 mPrepared = true;
Lajos Molnarfcd3e942015-03-31 10:06:48 -07002362 }
2363
Andreas Huber9575c962013-02-05 13:59:56 -08002364 sp<NuPlayerDriver> driver = mDriver.promote();
2365 if (driver != NULL) {
Marco Nelissendd114d12014-05-28 15:23:14 -07002366 // notify duration first, so that it's definitely set when
2367 // the app received the "prepare complete" callback.
2368 int64_t durationUs;
2369 if (mSource->getDuration(&durationUs) == OK) {
2370 driver->notifyDuration(durationUs);
2371 }
Andreas Huberec0c5972013-02-05 14:47:13 -08002372 driver->notifyPrepareCompleted(err);
Andreas Huber9575c962013-02-05 13:59:56 -08002373 }
Andreas Huber99759402013-04-01 14:28:31 -07002374
Andreas Huber9575c962013-02-05 13:59:56 -08002375 break;
2376 }
2377
Hassan Shojaniacefac142017-02-06 21:02:02 -08002378 // Modular DRM
2379 case Source::kWhatDrmInfo:
2380 {
2381 Parcel parcel;
2382 sp<ABuffer> drmInfo;
2383 CHECK(msg->findBuffer("drmInfo", &drmInfo));
2384 parcel.setData(drmInfo->data(), drmInfo->size());
2385
2386 ALOGV("onSourceNotify() kWhatDrmInfo MEDIA_DRM_INFO drmInfo: %p parcel size: %zu",
2387 drmInfo.get(), parcel.dataSize());
2388
2389 notifyListener(MEDIA_DRM_INFO, 0 /* ext1 */, 0 /* ext2 */, &parcel);
2390
2391 break;
2392 }
2393
Andreas Huber9575c962013-02-05 13:59:56 -08002394 case Source::kWhatFlagsChanged:
2395 {
2396 uint32_t flags;
2397 CHECK(msg->findInt32("flags", (int32_t *)&flags));
2398
Chong Zhang4b7069d2013-09-11 12:52:43 -07002399 sp<NuPlayerDriver> driver = mDriver.promote();
2400 if (driver != NULL) {
Hassan Shojaniacefac142017-02-06 21:02:02 -08002401
2402 ALOGV("onSourceNotify() kWhatFlagsChanged FLAG_CAN_PAUSE: %d "
2403 "FLAG_CAN_SEEK_BACKWARD: %d \n\t\t\t\t FLAG_CAN_SEEK_FORWARD: %d "
2404 "FLAG_CAN_SEEK: %d FLAG_DYNAMIC_DURATION: %d \n"
2405 "\t\t\t\t FLAG_SECURE: %d FLAG_PROTECTED: %d",
2406 (flags & Source::FLAG_CAN_PAUSE) != 0,
2407 (flags & Source::FLAG_CAN_SEEK_BACKWARD) != 0,
2408 (flags & Source::FLAG_CAN_SEEK_FORWARD) != 0,
2409 (flags & Source::FLAG_CAN_SEEK) != 0,
2410 (flags & Source::FLAG_DYNAMIC_DURATION) != 0,
2411 (flags & Source::FLAG_SECURE) != 0,
2412 (flags & Source::FLAG_PROTECTED) != 0);
2413
Wei Jia895651b2014-12-10 17:31:52 -08002414 if ((flags & NuPlayer::Source::FLAG_CAN_SEEK) == 0) {
2415 driver->notifyListener(
2416 MEDIA_INFO, MEDIA_INFO_NOT_SEEKABLE, 0);
2417 }
Chong Zhang4b7069d2013-09-11 12:52:43 -07002418 driver->notifyFlagsChanged(flags);
2419 }
2420
Andreas Huber9575c962013-02-05 13:59:56 -08002421 if ((mSourceFlags & Source::FLAG_DYNAMIC_DURATION)
2422 && (!(flags & Source::FLAG_DYNAMIC_DURATION))) {
2423 cancelPollDuration();
2424 } else if (!(mSourceFlags & Source::FLAG_DYNAMIC_DURATION)
2425 && (flags & Source::FLAG_DYNAMIC_DURATION)
2426 && (mAudioDecoder != NULL || mVideoDecoder != NULL)) {
2427 schedulePollDuration();
2428 }
2429
2430 mSourceFlags = flags;
2431 break;
2432 }
2433
2434 case Source::kWhatVideoSizeChanged:
2435 {
Chong Zhangced1c2f2014-08-08 15:22:35 -07002436 sp<AMessage> format;
2437 CHECK(msg->findMessage("format", &format));
Andreas Huber9575c962013-02-05 13:59:56 -08002438
Chong Zhangced1c2f2014-08-08 15:22:35 -07002439 updateVideoSize(format);
Andreas Huber9575c962013-02-05 13:59:56 -08002440 break;
2441 }
2442
Chong Zhang2a3cc9a2014-08-21 17:48:26 -07002443 case Source::kWhatBufferingUpdate:
2444 {
2445 int32_t percentage;
2446 CHECK(msg->findInt32("percentage", &percentage));
2447
2448 notifyListener(MEDIA_BUFFERING_UPDATE, percentage, 0);
2449 break;
2450 }
2451
Chong Zhangefbb6192015-01-30 17:13:27 -08002452 case Source::kWhatPauseOnBufferingStart:
2453 {
2454 // ignore if not playing
Chong Zhang8a048332015-05-06 15:16:28 -07002455 if (mStarted) {
Chong Zhangefbb6192015-01-30 17:13:27 -08002456 ALOGI("buffer low, pausing...");
2457
Chong Zhang8a048332015-05-06 15:16:28 -07002458 mPausedForBuffering = true;
Chong Zhangefbb6192015-01-30 17:13:27 -08002459 onPause();
2460 }
Wei Jiabfe82072016-05-20 09:21:50 -07002461 notifyListener(MEDIA_INFO, MEDIA_INFO_BUFFERING_START, 0);
Roger Jönssonb50e83e2013-01-21 16:26:41 +01002462 break;
2463 }
2464
Chong Zhangefbb6192015-01-30 17:13:27 -08002465 case Source::kWhatResumeOnBufferingEnd:
2466 {
2467 // ignore if not playing
Chong Zhang8a048332015-05-06 15:16:28 -07002468 if (mStarted) {
Chong Zhangefbb6192015-01-30 17:13:27 -08002469 ALOGI("buffer ready, resuming...");
2470
Chong Zhang8a048332015-05-06 15:16:28 -07002471 mPausedForBuffering = false;
2472
2473 // do not resume yet if client didn't unpause
2474 if (!mPausedByClient) {
2475 onResume();
2476 }
Chong Zhangefbb6192015-01-30 17:13:27 -08002477 }
Wei Jia3bed45a2016-02-17 11:06:47 -08002478 notifyListener(MEDIA_INFO, MEDIA_INFO_BUFFERING_END, 0);
Roger Jönssonb50e83e2013-01-21 16:26:41 +01002479 break;
2480 }
2481
Chong Zhangefbb6192015-01-30 17:13:27 -08002482 case Source::kWhatCacheStats:
2483 {
2484 int32_t kbps;
2485 CHECK(msg->findInt32("bandwidth", &kbps));
2486
2487 notifyListener(MEDIA_INFO, MEDIA_INFO_NETWORK_BANDWIDTH, kbps);
2488 break;
2489 }
2490
Chong Zhangdcb89b32013-08-06 09:44:47 -07002491 case Source::kWhatSubtitleData:
2492 {
2493 sp<ABuffer> buffer;
2494 CHECK(msg->findBuffer("buffer", &buffer));
2495
Chong Zhang404fced2014-06-11 14:45:31 -07002496 sendSubtitleData(buffer, 0 /* baseIndex */);
Chong Zhangdcb89b32013-08-06 09:44:47 -07002497 break;
2498 }
2499
Robert Shih08528432015-04-08 09:06:54 -07002500 case Source::kWhatTimedMetaData:
2501 {
2502 sp<ABuffer> buffer;
2503 if (!msg->findBuffer("buffer", &buffer)) {
2504 notifyListener(MEDIA_INFO, MEDIA_INFO_METADATA_UPDATE, 0);
2505 } else {
2506 sendTimedMetaData(buffer);
2507 }
2508 break;
2509 }
2510
Robert Shihd3b0bbb2014-07-23 15:00:25 -07002511 case Source::kWhatTimedTextData:
2512 {
2513 int32_t generation;
2514 if (msg->findInt32("generation", &generation)
2515 && generation != mTimedTextGeneration) {
2516 break;
2517 }
2518
2519 sp<ABuffer> buffer;
2520 CHECK(msg->findBuffer("buffer", &buffer));
2521
2522 sp<NuPlayerDriver> driver = mDriver.promote();
2523 if (driver == NULL) {
2524 break;
2525 }
2526
2527 int posMs;
2528 int64_t timeUs, posUs;
2529 driver->getCurrentPosition(&posMs);
Patrik2 Carlsson24d484b2015-01-27 16:49:45 +01002530 posUs = (int64_t) posMs * 1000ll;
Robert Shihd3b0bbb2014-07-23 15:00:25 -07002531 CHECK(buffer->meta()->findInt64("timeUs", &timeUs));
2532
2533 if (posUs < timeUs) {
2534 if (!msg->findInt32("generation", &generation)) {
2535 msg->setInt32("generation", mTimedTextGeneration);
2536 }
2537 msg->post(timeUs - posUs);
2538 } else {
2539 sendTimedTextData(buffer);
2540 }
2541 break;
2542 }
2543
Andreas Huber14f76722013-01-15 09:04:18 -08002544 case Source::kWhatQueueDecoderShutdown:
2545 {
2546 int32_t audio, video;
2547 CHECK(msg->findInt32("audio", &audio));
2548 CHECK(msg->findInt32("video", &video));
2549
2550 sp<AMessage> reply;
2551 CHECK(msg->findMessage("reply", &reply));
2552
2553 queueDecoderShutdown(audio, video, reply);
2554 break;
2555 }
2556
Ronghua Wu80276872014-08-28 15:50:29 -07002557 case Source::kWhatDrmNoLicense:
2558 {
2559 notifyListener(MEDIA_ERROR, MEDIA_ERROR_UNKNOWN, ERROR_DRM_NO_LICENSE);
2560 break;
2561 }
2562
Andreas Huber9575c962013-02-05 13:59:56 -08002563 default:
2564 TRESPASS();
2565 }
2566}
2567
Chong Zhanga7fa1d92014-06-11 14:49:23 -07002568void NuPlayer::onClosedCaptionNotify(const sp<AMessage> &msg) {
2569 int32_t what;
2570 CHECK(msg->findInt32("what", &what));
2571
2572 switch (what) {
2573 case NuPlayer::CCDecoder::kWhatClosedCaptionData:
2574 {
2575 sp<ABuffer> buffer;
2576 CHECK(msg->findBuffer("buffer", &buffer));
2577
2578 size_t inbandTracks = 0;
2579 if (mSource != NULL) {
2580 inbandTracks = mSource->getTrackCount();
2581 }
2582
2583 sendSubtitleData(buffer, inbandTracks);
2584 break;
2585 }
2586
2587 case NuPlayer::CCDecoder::kWhatTrackAdded:
2588 {
2589 notifyListener(MEDIA_INFO, MEDIA_INFO_METADATA_UPDATE, 0);
2590
2591 break;
2592 }
2593
2594 default:
2595 TRESPASS();
2596 }
2597
2598
2599}
2600
Chong Zhang404fced2014-06-11 14:45:31 -07002601void NuPlayer::sendSubtitleData(const sp<ABuffer> &buffer, int32_t baseIndex) {
2602 int32_t trackIndex;
2603 int64_t timeUs, durationUs;
2604 CHECK(buffer->meta()->findInt32("trackIndex", &trackIndex));
2605 CHECK(buffer->meta()->findInt64("timeUs", &timeUs));
2606 CHECK(buffer->meta()->findInt64("durationUs", &durationUs));
2607
2608 Parcel in;
2609 in.writeInt32(trackIndex + baseIndex);
2610 in.writeInt64(timeUs);
2611 in.writeInt64(durationUs);
2612 in.writeInt32(buffer->size());
2613 in.writeInt32(buffer->size());
2614 in.write(buffer->data(), buffer->size());
2615
2616 notifyListener(MEDIA_SUBTITLE_DATA, 0, 0, &in);
2617}
Robert Shihd3b0bbb2014-07-23 15:00:25 -07002618
Robert Shih08528432015-04-08 09:06:54 -07002619void NuPlayer::sendTimedMetaData(const sp<ABuffer> &buffer) {
2620 int64_t timeUs;
2621 CHECK(buffer->meta()->findInt64("timeUs", &timeUs));
2622
2623 Parcel in;
2624 in.writeInt64(timeUs);
2625 in.writeInt32(buffer->size());
2626 in.writeInt32(buffer->size());
2627 in.write(buffer->data(), buffer->size());
2628
2629 notifyListener(MEDIA_META_DATA, 0, 0, &in);
2630}
2631
Robert Shihd3b0bbb2014-07-23 15:00:25 -07002632void NuPlayer::sendTimedTextData(const sp<ABuffer> &buffer) {
2633 const void *data;
2634 size_t size = 0;
2635 int64_t timeUs;
Marco Nelissen55e2f4c2015-09-04 15:57:15 -07002636 int32_t flag = TextDescriptions::IN_BAND_TEXT_3GPP;
Robert Shihd3b0bbb2014-07-23 15:00:25 -07002637
2638 AString mime;
2639 CHECK(buffer->meta()->findString("mime", &mime));
2640 CHECK(strcasecmp(mime.c_str(), MEDIA_MIMETYPE_TEXT_3GPP) == 0);
2641
2642 data = buffer->data();
2643 size = buffer->size();
2644
2645 Parcel parcel;
2646 if (size > 0) {
2647 CHECK(buffer->meta()->findInt64("timeUs", &timeUs));
Marco Nelissen55e2f4c2015-09-04 15:57:15 -07002648 int32_t global = 0;
2649 if (buffer->meta()->findInt32("global", &global) && global) {
2650 flag |= TextDescriptions::GLOBAL_DESCRIPTIONS;
2651 } else {
2652 flag |= TextDescriptions::LOCAL_DESCRIPTIONS;
2653 }
Robert Shihd3b0bbb2014-07-23 15:00:25 -07002654 TextDescriptions::getParcelOfDescriptions(
2655 (const uint8_t *)data, size, flag, timeUs / 1000, &parcel);
2656 }
2657
2658 if ((parcel.dataSize() > 0)) {
2659 notifyListener(MEDIA_TIMED_TEXT, 0, 0, &parcel);
2660 } else { // send an empty timed text
2661 notifyListener(MEDIA_TIMED_TEXT, 0, 0);
2662 }
2663}
Hassan Shojaniacefac142017-02-06 21:02:02 -08002664
Hassan Shojaniaff63de72017-04-26 15:10:42 -07002665const char *NuPlayer::getDataSourceType() {
2666 switch (mDataSourceType) {
2667 case DATA_SOURCE_TYPE_HTTP_LIVE:
2668 return "HTTPLive";
2669
2670 case DATA_SOURCE_TYPE_RTSP:
2671 return "RTSP";
2672
2673 case DATA_SOURCE_TYPE_GENERIC_URL:
2674 return "GenURL";
2675
2676 case DATA_SOURCE_TYPE_GENERIC_FD:
2677 return "GenFD";
2678
2679 case DATA_SOURCE_TYPE_MEDIA:
2680 return "Media";
2681
2682 case DATA_SOURCE_TYPE_STREAM:
2683 return "Stream";
2684
2685 case DATA_SOURCE_TYPE_NONE:
2686 default:
2687 return "None";
2688 }
2689 }
2690
Hassan Shojaniacefac142017-02-06 21:02:02 -08002691// Modular DRM begin
2692status_t NuPlayer::prepareDrm(const uint8_t uuid[16], const Vector<uint8_t> &drmSessionId)
2693{
2694 ALOGV("prepareDrm ");
2695
2696 // Passing to the looper anyway; called in a pre-config prepared state so no race on mCrypto
2697 sp<AMessage> msg = new AMessage(kWhatPrepareDrm, this);
2698 // synchronous call so just passing the address but with local copies of "const" args
2699 uint8_t UUID[16];
2700 memcpy(UUID, uuid, sizeof(UUID));
2701 Vector<uint8_t> sessionId = drmSessionId;
2702 msg->setPointer("uuid", (void*)UUID);
2703 msg->setPointer("drmSessionId", (void*)&sessionId);
2704
2705 sp<AMessage> response;
2706 status_t status = msg->postAndAwaitResponse(&response);
2707
2708 if (status == OK && response != NULL) {
2709 CHECK(response->findInt32("status", &status));
2710 ALOGV("prepareDrm ret: %d ", status);
2711 } else {
2712 ALOGE("prepareDrm err: %d", status);
2713 }
2714
2715 return status;
2716}
2717
2718status_t NuPlayer::releaseDrm()
2719{
2720 ALOGV("releaseDrm ");
2721
2722 sp<AMessage> msg = new AMessage(kWhatReleaseDrm, this);
2723
2724 sp<AMessage> response;
2725 status_t status = msg->postAndAwaitResponse(&response);
2726
2727 if (status == OK && response != NULL) {
2728 CHECK(response->findInt32("status", &status));
2729 ALOGV("releaseDrm ret: %d ", status);
2730 } else {
2731 ALOGE("releaseDrm err: %d", status);
2732 }
2733
2734 return status;
2735}
2736
2737status_t NuPlayer::onPrepareDrm(const sp<AMessage> &msg)
2738{
2739 // TODO change to ALOGV
2740 ALOGD("onPrepareDrm ");
2741
2742 status_t status = INVALID_OPERATION;
2743 if (mSource == NULL) {
2744 ALOGE("onPrepareDrm: No source. onPrepareDrm failed with %d.", status);
2745 return status;
2746 }
2747
2748 uint8_t *uuid;
2749 Vector<uint8_t> *drmSessionId;
2750 CHECK(msg->findPointer("uuid", (void**)&uuid));
2751 CHECK(msg->findPointer("drmSessionId", (void**)&drmSessionId));
2752
2753 status = OK;
2754 sp<ICrypto> crypto = NULL;
2755
2756 status = mSource->prepareDrm(uuid, *drmSessionId, &crypto);
2757 if (crypto == NULL) {
2758 ALOGE("onPrepareDrm: mSource->prepareDrm failed. status: %d", status);
2759 return status;
2760 }
2761 ALOGV("onPrepareDrm: mSource->prepareDrm succeeded");
2762
2763 if (mCrypto != NULL) {
2764 ALOGE("onPrepareDrm: Unexpected. Already having mCrypto: %p (%d)",
2765 mCrypto.get(), mCrypto->getStrongCount());
2766 mCrypto.clear();
2767 }
2768
2769 mCrypto = crypto;
2770 mIsDrmProtected = true;
2771 // TODO change to ALOGV
2772 ALOGD("onPrepareDrm: mCrypto: %p (%d)", mCrypto.get(),
2773 (mCrypto != NULL ? mCrypto->getStrongCount() : 0));
2774
2775 return status;
2776}
2777
2778status_t NuPlayer::onReleaseDrm()
2779{
2780 // TODO change to ALOGV
2781 ALOGD("onReleaseDrm ");
2782
Hassan Shojania50b20c92017-02-16 18:28:58 -08002783 if (!mIsDrmProtected) {
2784 ALOGW("onReleaseDrm: Unexpected. mIsDrmProtected is already false.");
2785 }
2786
2787 mIsDrmProtected = false;
Hassan Shojaniacefac142017-02-06 21:02:02 -08002788
2789 status_t status;
2790 if (mCrypto != NULL) {
Hassan Shojania355e8472017-05-12 10:33:16 -07002791 // notifying the source first before removing crypto from codec
2792 if (mSource != NULL) {
2793 mSource->releaseDrm();
2794 }
2795
Hassan Shojaniacefac142017-02-06 21:02:02 -08002796 status=OK;
2797 // first making sure the codecs have released their crypto reference
2798 const sp<DecoderBase> &videoDecoder = getDecoder(false/*audio*/);
2799 if (videoDecoder != NULL) {
2800 status = videoDecoder->releaseCrypto();
2801 ALOGV("onReleaseDrm: video decoder ret: %d", status);
2802 }
2803
2804 const sp<DecoderBase> &audioDecoder = getDecoder(true/*audio*/);
2805 if (audioDecoder != NULL) {
2806 status_t status_audio = audioDecoder->releaseCrypto();
2807 if (status == OK) { // otherwise, returning the first error
2808 status = status_audio;
2809 }
2810 ALOGV("onReleaseDrm: audio decoder ret: %d", status_audio);
2811 }
2812
2813 // TODO change to ALOGV
2814 ALOGD("onReleaseDrm: mCrypto: %p (%d)", mCrypto.get(),
2815 (mCrypto != NULL ? mCrypto->getStrongCount() : 0));
2816 mCrypto.clear();
2817 } else { // mCrypto == NULL
2818 ALOGE("onReleaseDrm: Unexpected. There is no crypto.");
2819 status = INVALID_OPERATION;
2820 }
2821
2822 return status;
2823}
2824// Modular DRM end
Andreas Huberb5f25f02013-02-05 10:14:26 -08002825////////////////////////////////////////////////////////////////////////////////
2826
Chong Zhangced1c2f2014-08-08 15:22:35 -07002827sp<AMessage> NuPlayer::Source::getFormat(bool audio) {
2828 sp<MetaData> meta = getFormatMeta(audio);
2829
2830 if (meta == NULL) {
2831 return NULL;
2832 }
2833
2834 sp<AMessage> msg = new AMessage;
2835
2836 if(convertMetaDataToMessage(meta, &msg) == OK) {
2837 return msg;
2838 }
2839 return NULL;
2840}
2841
Andreas Huber9575c962013-02-05 13:59:56 -08002842void NuPlayer::Source::notifyFlagsChanged(uint32_t flags) {
2843 sp<AMessage> notify = dupNotify();
2844 notify->setInt32("what", kWhatFlagsChanged);
2845 notify->setInt32("flags", flags);
2846 notify->post();
2847}
2848
Chong Zhangced1c2f2014-08-08 15:22:35 -07002849void NuPlayer::Source::notifyVideoSizeChanged(const sp<AMessage> &format) {
Andreas Huber9575c962013-02-05 13:59:56 -08002850 sp<AMessage> notify = dupNotify();
2851 notify->setInt32("what", kWhatVideoSizeChanged);
Chong Zhangced1c2f2014-08-08 15:22:35 -07002852 notify->setMessage("format", format);
Andreas Huber9575c962013-02-05 13:59:56 -08002853 notify->post();
2854}
2855
Andreas Huberec0c5972013-02-05 14:47:13 -08002856void NuPlayer::Source::notifyPrepared(status_t err) {
Hassan Shojaniacefac142017-02-06 21:02:02 -08002857 ALOGV("Source::notifyPrepared %d", err);
Andreas Huber9575c962013-02-05 13:59:56 -08002858 sp<AMessage> notify = dupNotify();
2859 notify->setInt32("what", kWhatPrepared);
Andreas Huberec0c5972013-02-05 14:47:13 -08002860 notify->setInt32("err", err);
Andreas Huber9575c962013-02-05 13:59:56 -08002861 notify->post();
2862}
2863
Hassan Shojaniacefac142017-02-06 21:02:02 -08002864void NuPlayer::Source::notifyDrmInfo(const sp<ABuffer> &drmInfoBuffer)
2865{
2866 ALOGV("Source::notifyDrmInfo");
2867
2868 sp<AMessage> notify = dupNotify();
2869 notify->setInt32("what", kWhatDrmInfo);
2870 notify->setBuffer("drmInfo", drmInfoBuffer);
2871
2872 notify->post();
2873}
2874
Lajos Molnarfcd3e942015-03-31 10:06:48 -07002875void NuPlayer::Source::notifyInstantiateSecureDecoders(const sp<AMessage> &reply) {
2876 sp<AMessage> notify = dupNotify();
2877 notify->setInt32("what", kWhatInstantiateSecureDecoders);
2878 notify->setMessage("reply", reply);
2879 notify->post();
2880}
2881
Andreas Huber84333e02014-02-07 15:36:10 -08002882void NuPlayer::Source::onMessageReceived(const sp<AMessage> & /* msg */) {
Andreas Huberb5f25f02013-02-05 10:14:26 -08002883 TRESPASS();
2884}
2885
Andreas Huberf9334412010-12-15 15:17:42 -08002886} // namespace android