blob: e800d136bcb122262b5df633982a84bfd4c8bb2b [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),
Chong Zhangefbb6192015-01-30 17:13:27 -0800200 mPaused(false),
Wei Jia71c75e02016-02-04 09:40:47 -0800201 mPausedByClient(true),
Chong Zhang8a048332015-05-06 15:16:28 -0700202 mPausedForBuffering(false) {
Andy Hung8d121d42014-10-03 09:53:53 -0700203 clearFlushComplete();
Andreas Huberf9334412010-12-15 15:17:42 -0800204}
205
206NuPlayer::~NuPlayer() {
207}
208
Andreas Huber9b80c2b2011-06-30 15:47:02 -0700209void NuPlayer::setUID(uid_t uid) {
210 mUIDValid = true;
211 mUID = uid;
212}
213
Andreas Huber43c3e6c2011-01-05 12:17:08 -0800214void NuPlayer::setDriver(const wp<NuPlayerDriver> &driver) {
215 mDriver = driver;
Andreas Huberf9334412010-12-15 15:17:42 -0800216}
217
Andreas Huber9575c962013-02-05 13:59:56 -0800218void NuPlayer::setDataSourceAsync(const sp<IStreamSource> &source) {
Lajos Molnar1d15ab52015-03-04 16:46:34 -0800219 sp<AMessage> msg = new AMessage(kWhatSetDataSource, this);
Andreas Huberf9334412010-12-15 15:17:42 -0800220
Lajos Molnar1d15ab52015-03-04 16:46:34 -0800221 sp<AMessage> notify = new AMessage(kWhatSourceNotify, this);
Andreas Huberb5f25f02013-02-05 10:14:26 -0800222
Andreas Huber240abcc2014-02-13 13:32:37 -0800223 msg->setObject("source", new StreamingSource(notify, source));
Andreas Huber5bc087c2010-12-23 10:27:40 -0800224 msg->post();
225}
Andreas Huberf9334412010-12-15 15:17:42 -0800226
Andreas Huberafed0e12011-09-20 15:39:58 -0700227static bool IsHTTPLiveURL(const char *url) {
228 if (!strncasecmp("http://", url, 7)
Andreas Huber99759402013-04-01 14:28:31 -0700229 || !strncasecmp("https://", url, 8)
230 || !strncasecmp("file://", url, 7)) {
Andreas Huberafed0e12011-09-20 15:39:58 -0700231 size_t len = strlen(url);
232 if (len >= 5 && !strcasecmp(".m3u8", &url[len - 5])) {
233 return true;
234 }
235
236 if (strstr(url,"m3u8")) {
237 return true;
238 }
239 }
240
241 return false;
242}
243
Andreas Huber9575c962013-02-05 13:59:56 -0800244void NuPlayer::setDataSourceAsync(
Andreas Huber1b86fe02014-01-29 11:13:26 -0800245 const sp<IMediaHTTPService> &httpService,
246 const char *url,
247 const KeyedVector<String8, String8> *headers) {
Chong Zhang3de157d2014-08-05 20:54:44 -0700248
Lajos Molnar1d15ab52015-03-04 16:46:34 -0800249 sp<AMessage> msg = new AMessage(kWhatSetDataSource, this);
Oscar Rydhé7a33b772012-02-20 10:15:48 +0100250 size_t len = strlen(url);
Andreas Huber5bc087c2010-12-23 10:27:40 -0800251
Lajos Molnar1d15ab52015-03-04 16:46:34 -0800252 sp<AMessage> notify = new AMessage(kWhatSourceNotify, this);
Andreas Huberb5f25f02013-02-05 10:14:26 -0800253
Andreas Huberafed0e12011-09-20 15:39:58 -0700254 sp<Source> source;
255 if (IsHTTPLiveURL(url)) {
Andreas Huber81e68442014-02-05 11:52:33 -0800256 source = new HTTPLiveSource(notify, httpService, url, headers);
Hassan Shojaniacefac142017-02-06 21:02:02 -0800257 ALOGV("setDataSourceAsync HTTPLiveSource %s", url);
Andreas Huberafed0e12011-09-20 15:39:58 -0700258 } else if (!strncasecmp(url, "rtsp://", 7)) {
Andreas Huber1b86fe02014-01-29 11:13:26 -0800259 source = new RTSPSource(
260 notify, httpService, url, headers, mUIDValid, mUID);
Hassan Shojaniacefac142017-02-06 21:02:02 -0800261 ALOGV("setDataSourceAsync RTSPSource %s", url);
Oscar Rydhé7a33b772012-02-20 10:15:48 +0100262 } else if ((!strncasecmp(url, "http://", 7)
263 || !strncasecmp(url, "https://", 8))
264 && ((len >= 4 && !strcasecmp(".sdp", &url[len - 4]))
265 || strstr(url, ".sdp?"))) {
Andreas Huber1b86fe02014-01-29 11:13:26 -0800266 source = new RTSPSource(
267 notify, httpService, url, headers, mUIDValid, mUID, true);
Hassan Shojaniacefac142017-02-06 21:02:02 -0800268 ALOGV("setDataSourceAsync RTSPSource http/https/.sdp %s", url);
Andreas Huber2bfdd422011-10-11 15:24:07 -0700269 } else {
Hassan Shojaniacefac142017-02-06 21:02:02 -0800270 ALOGV("setDataSourceAsync GenericSource %s", url);
271
Chong Zhang3de157d2014-08-05 20:54:44 -0700272 sp<GenericSource> genericSource =
273 new GenericSource(notify, mUIDValid, mUID);
Andreas Huber2bfdd422011-10-11 15:24:07 -0700274
Chong Zhanga19f33e2014-08-07 15:35:07 -0700275 status_t err = genericSource->setDataSource(httpService, url, headers);
Chong Zhang3de157d2014-08-05 20:54:44 -0700276
277 if (err == OK) {
278 source = genericSource;
279 } else {
Chong Zhanga19f33e2014-08-07 15:35:07 -0700280 ALOGE("Failed to set data source!");
Chong Zhang3de157d2014-08-05 20:54:44 -0700281 }
282 }
Andreas Huberafed0e12011-09-20 15:39:58 -0700283 msg->setObject("source", source);
284 msg->post();
285}
286
Andreas Huber9575c962013-02-05 13:59:56 -0800287void NuPlayer::setDataSourceAsync(int fd, int64_t offset, int64_t length) {
Lajos Molnar1d15ab52015-03-04 16:46:34 -0800288 sp<AMessage> msg = new AMessage(kWhatSetDataSource, this);
Andreas Huberafed0e12011-09-20 15:39:58 -0700289
Lajos Molnar1d15ab52015-03-04 16:46:34 -0800290 sp<AMessage> notify = new AMessage(kWhatSourceNotify, this);
Andreas Huberb5f25f02013-02-05 10:14:26 -0800291
Chong Zhang3de157d2014-08-05 20:54:44 -0700292 sp<GenericSource> source =
293 new GenericSource(notify, mUIDValid, mUID);
294
Hassan Shojaniacefac142017-02-06 21:02:02 -0800295 ALOGV("setDataSourceAsync fd %d/%lld/%lld source: %p",
296 fd, (long long)offset, (long long)length, source.get());
297
Chong Zhanga19f33e2014-08-07 15:35:07 -0700298 status_t err = source->setDataSource(fd, offset, length);
Chong Zhang3de157d2014-08-05 20:54:44 -0700299
300 if (err != OK) {
Chong Zhanga19f33e2014-08-07 15:35:07 -0700301 ALOGE("Failed to set data source!");
Chong Zhang3de157d2014-08-05 20:54:44 -0700302 source = NULL;
303 }
304
Andreas Huberafed0e12011-09-20 15:39:58 -0700305 msg->setObject("source", source);
Andreas Huberf9334412010-12-15 15:17:42 -0800306 msg->post();
307}
308
Chris Watkins99f31602015-03-20 13:06:33 -0700309void NuPlayer::setDataSourceAsync(const sp<DataSource> &dataSource) {
310 sp<AMessage> msg = new AMessage(kWhatSetDataSource, this);
311 sp<AMessage> notify = new AMessage(kWhatSourceNotify, this);
312
313 sp<GenericSource> source = new GenericSource(notify, mUIDValid, mUID);
314 status_t err = source->setDataSource(dataSource);
315
316 if (err != OK) {
317 ALOGE("Failed to set data source!");
318 source = NULL;
319 }
320
321 msg->setObject("source", source);
322 msg->post();
323}
324
Wei Jia48fa06d2016-12-20 15:30:49 -0800325status_t NuPlayer::getDefaultBufferingSettings(
326 BufferingSettings *buffering /* nonnull */) {
327 sp<AMessage> msg = new AMessage(kWhatGetDefaultBufferingSettings, this);
328 sp<AMessage> response;
329 status_t err = msg->postAndAwaitResponse(&response);
330 if (err == OK && response != NULL) {
331 CHECK(response->findInt32("err", &err));
332 if (err == OK) {
333 readFromAMessage(response, buffering);
334 }
335 }
336 return err;
337}
338
339status_t NuPlayer::setBufferingSettings(const BufferingSettings& buffering) {
340 sp<AMessage> msg = new AMessage(kWhatSetBufferingSettings, this);
341 writeToAMessage(msg, buffering);
342 sp<AMessage> response;
343 status_t err = msg->postAndAwaitResponse(&response);
344 if (err == OK && response != NULL) {
345 CHECK(response->findInt32("err", &err));
346 }
347 return err;
348}
349
Andreas Huber9575c962013-02-05 13:59:56 -0800350void NuPlayer::prepareAsync() {
Hassan Shojaniacefac142017-02-06 21:02:02 -0800351 ALOGV("prepareAsync");
352
Lajos Molnar1d15ab52015-03-04 16:46:34 -0800353 (new AMessage(kWhatPrepare, this))->post();
Andreas Huber9575c962013-02-05 13:59:56 -0800354}
355
Andreas Huber57a339c2012-12-03 11:18:00 -0800356void NuPlayer::setVideoSurfaceTextureAsync(
Andy McFadden8ba01022012-12-18 09:46:54 -0800357 const sp<IGraphicBufferProducer> &bufferProducer) {
Lajos Molnar1de1e252015-04-30 18:18:34 -0700358 sp<AMessage> msg = new AMessage(kWhatSetVideoSurface, this);
Andreas Huber57a339c2012-12-03 11:18:00 -0800359
Andy McFadden8ba01022012-12-18 09:46:54 -0800360 if (bufferProducer == NULL) {
Lajos Molnar1de1e252015-04-30 18:18:34 -0700361 msg->setObject("surface", NULL);
Andreas Huber57a339c2012-12-03 11:18:00 -0800362 } else {
Lajos Molnar1de1e252015-04-30 18:18:34 -0700363 msg->setObject("surface", new Surface(bufferProducer, true /* controlledByApp */));
Andreas Huber57a339c2012-12-03 11:18:00 -0800364 }
365
Andreas Huberf9334412010-12-15 15:17:42 -0800366 msg->post();
367}
368
369void NuPlayer::setAudioSink(const sp<MediaPlayerBase::AudioSink> &sink) {
Lajos Molnar1d15ab52015-03-04 16:46:34 -0800370 sp<AMessage> msg = new AMessage(kWhatSetAudioSink, this);
Andreas Huberf9334412010-12-15 15:17:42 -0800371 msg->setObject("sink", sink);
372 msg->post();
373}
374
375void NuPlayer::start() {
Lajos Molnar1d15ab52015-03-04 16:46:34 -0800376 (new AMessage(kWhatStart, this))->post();
Andreas Huberf9334412010-12-15 15:17:42 -0800377}
378
Lajos Molnar3a474aa2015-04-24 17:10:07 -0700379status_t NuPlayer::setPlaybackSettings(const AudioPlaybackRate &rate) {
380 // do some cursory validation of the settings here. audio modes are
381 // only validated when set on the audiosink.
382 if ((rate.mSpeed != 0.f && rate.mSpeed < AUDIO_TIMESTRETCH_SPEED_MIN)
383 || rate.mSpeed > AUDIO_TIMESTRETCH_SPEED_MAX
384 || rate.mPitch < AUDIO_TIMESTRETCH_SPEED_MIN
385 || rate.mPitch > AUDIO_TIMESTRETCH_SPEED_MAX) {
386 return BAD_VALUE;
387 }
388 sp<AMessage> msg = new AMessage(kWhatConfigPlayback, this);
389 writeToAMessage(msg, rate);
390 sp<AMessage> response;
391 status_t err = msg->postAndAwaitResponse(&response);
392 if (err == OK && response != NULL) {
393 CHECK(response->findInt32("err", &err));
394 }
395 return err;
396}
397
398status_t NuPlayer::getPlaybackSettings(AudioPlaybackRate *rate /* nonnull */) {
399 sp<AMessage> msg = new AMessage(kWhatGetPlaybackSettings, this);
400 sp<AMessage> response;
401 status_t err = msg->postAndAwaitResponse(&response);
402 if (err == OK && response != NULL) {
403 CHECK(response->findInt32("err", &err));
404 if (err == OK) {
405 readFromAMessage(response, rate);
406 }
407 }
408 return err;
409}
410
411status_t NuPlayer::setSyncSettings(const AVSyncSettings &sync, float videoFpsHint) {
412 sp<AMessage> msg = new AMessage(kWhatConfigSync, this);
413 writeToAMessage(msg, sync, videoFpsHint);
414 sp<AMessage> response;
415 status_t err = msg->postAndAwaitResponse(&response);
416 if (err == OK && response != NULL) {
417 CHECK(response->findInt32("err", &err));
418 }
419 return err;
420}
421
422status_t NuPlayer::getSyncSettings(
423 AVSyncSettings *sync /* nonnull */, float *videoFps /* nonnull */) {
424 sp<AMessage> msg = new AMessage(kWhatGetSyncSettings, this);
425 sp<AMessage> response;
426 status_t err = msg->postAndAwaitResponse(&response);
427 if (err == OK && response != NULL) {
428 CHECK(response->findInt32("err", &err));
429 if (err == OK) {
430 readFromAMessage(response, sync, videoFps);
431 }
432 }
433 return err;
Wei Jia98160162015-02-04 17:01:11 -0800434}
435
Andreas Huber43c3e6c2011-01-05 12:17:08 -0800436void NuPlayer::pause() {
Lajos Molnar1d15ab52015-03-04 16:46:34 -0800437 (new AMessage(kWhatPause, this))->post();
Andreas Huber43c3e6c2011-01-05 12:17:08 -0800438}
439
Andreas Huber1aef2112011-01-04 14:01:29 -0800440void NuPlayer::resetAsync() {
Wei Jiac45a4b22016-04-15 15:30:23 -0700441 sp<Source> source;
442 {
443 Mutex::Autolock autoLock(mSourceLock);
444 source = mSource;
445 }
446
447 if (source != NULL) {
Chong Zhang48296b72014-09-14 14:28:45 -0700448 // During a reset, the data source might be unresponsive already, we need to
449 // disconnect explicitly so that reads exit promptly.
450 // We can't queue the disconnect request to the looper, as it might be
451 // queued behind a stuck read and never gets processed.
452 // Doing a disconnect outside the looper to allows the pending reads to exit
453 // (either successfully or with error).
Wei Jiac45a4b22016-04-15 15:30:23 -0700454 source->disconnect();
Chong Zhang48296b72014-09-14 14:28:45 -0700455 }
456
Lajos Molnar1d15ab52015-03-04 16:46:34 -0800457 (new AMessage(kWhatReset, this))->post();
Andreas Huber1aef2112011-01-04 14:01:29 -0800458}
459
Wei Jiac5de0912016-11-18 10:22:14 -0800460void NuPlayer::seekToAsync(int64_t seekTimeUs, MediaPlayerSeekMode mode, bool needNotify) {
Lajos Molnar1d15ab52015-03-04 16:46:34 -0800461 sp<AMessage> msg = new AMessage(kWhatSeek, this);
Andreas Huber43c3e6c2011-01-05 12:17:08 -0800462 msg->setInt64("seekTimeUs", seekTimeUs);
Wei Jiac5de0912016-11-18 10:22:14 -0800463 msg->setInt32("mode", mode);
Wei Jiae427abf2014-09-22 15:21:11 -0700464 msg->setInt32("needNotify", needNotify);
Andreas Huber43c3e6c2011-01-05 12:17:08 -0800465 msg->post();
466}
467
Andreas Huber53df1a42010-12-22 10:03:04 -0800468
Chong Zhang404fced2014-06-11 14:45:31 -0700469void NuPlayer::writeTrackInfo(
Chih-Hung Hsiehe964d4e2016-08-09 14:31:32 -0700470 Parcel* reply, const sp<AMessage>& format) const {
Marco Nelissenc367ca12015-09-15 09:51:59 -0700471 if (format == NULL) {
472 ALOGE("NULL format");
473 return;
474 }
Chong Zhang404fced2014-06-11 14:45:31 -0700475 int32_t trackType;
Marco Nelissen9436e482015-09-15 10:21:53 -0700476 if (!format->findInt32("type", &trackType)) {
Marco Nelissenc367ca12015-09-15 09:51:59 -0700477 ALOGE("no track type");
478 return;
479 }
Chong Zhang404fced2014-06-11 14:45:31 -0700480
Robert Shih755106e2015-04-30 14:36:45 -0700481 AString mime;
Robert Shih2e3a4252015-05-06 10:21:15 -0700482 if (!format->findString("mime", &mime)) {
483 // Java MediaPlayer only uses mimetype for subtitle and timedtext tracks.
484 // If we can't find the mimetype here it means that we wouldn't be needing
485 // the mimetype on the Java end. We still write a placeholder mime to keep the
486 // (de)serialization logic simple.
487 if (trackType == MEDIA_TRACK_TYPE_AUDIO) {
488 mime = "audio/";
489 } else if (trackType == MEDIA_TRACK_TYPE_VIDEO) {
490 mime = "video/";
491 } else {
Marco Nelissenc367ca12015-09-15 09:51:59 -0700492 ALOGE("unknown track type: %d", trackType);
493 return;
Robert Shih2e3a4252015-05-06 10:21:15 -0700494 }
495 }
Robert Shih755106e2015-04-30 14:36:45 -0700496
Chong Zhang404fced2014-06-11 14:45:31 -0700497 AString lang;
Marco Nelissen9436e482015-09-15 10:21:53 -0700498 if (!format->findString("language", &lang)) {
Marco Nelissenc367ca12015-09-15 09:51:59 -0700499 ALOGE("no language");
500 return;
501 }
Chong Zhang404fced2014-06-11 14:45:31 -0700502
503 reply->writeInt32(2); // write something non-zero
504 reply->writeInt32(trackType);
Robert Shih755106e2015-04-30 14:36:45 -0700505 reply->writeString16(String16(mime.c_str()));
Chong Zhang404fced2014-06-11 14:45:31 -0700506 reply->writeString16(String16(lang.c_str()));
507
508 if (trackType == MEDIA_TRACK_TYPE_SUBTITLE) {
Chong Zhang404fced2014-06-11 14:45:31 -0700509 int32_t isAuto, isDefault, isForced;
510 CHECK(format->findInt32("auto", &isAuto));
511 CHECK(format->findInt32("default", &isDefault));
512 CHECK(format->findInt32("forced", &isForced));
513
Chong Zhang404fced2014-06-11 14:45:31 -0700514 reply->writeInt32(isAuto);
515 reply->writeInt32(isDefault);
516 reply->writeInt32(isForced);
517 }
518}
519
Andreas Huberf9334412010-12-15 15:17:42 -0800520void NuPlayer::onMessageReceived(const sp<AMessage> &msg) {
521 switch (msg->what()) {
522 case kWhatSetDataSource:
523 {
Steve Block3856b092011-10-20 11:56:00 +0100524 ALOGV("kWhatSetDataSource");
Andreas Huberf9334412010-12-15 15:17:42 -0800525
526 CHECK(mSource == NULL);
527
Chong Zhang3de157d2014-08-05 20:54:44 -0700528 status_t err = OK;
Andreas Huber5bc087c2010-12-23 10:27:40 -0800529 sp<RefBase> obj;
530 CHECK(msg->findObject("source", &obj));
Chong Zhang3de157d2014-08-05 20:54:44 -0700531 if (obj != NULL) {
Wei Jiac45a4b22016-04-15 15:30:23 -0700532 Mutex::Autolock autoLock(mSourceLock);
Chong Zhang3de157d2014-08-05 20:54:44 -0700533 mSource = static_cast<Source *>(obj.get());
Chong Zhang3de157d2014-08-05 20:54:44 -0700534 } else {
535 err = UNKNOWN_ERROR;
536 }
Andreas Huber9575c962013-02-05 13:59:56 -0800537
538 CHECK(mDriver != NULL);
539 sp<NuPlayerDriver> driver = mDriver.promote();
540 if (driver != NULL) {
Chong Zhang3de157d2014-08-05 20:54:44 -0700541 driver->notifySetDataSourceCompleted(err);
Andreas Huber9575c962013-02-05 13:59:56 -0800542 }
543 break;
544 }
545
Wei Jia48fa06d2016-12-20 15:30:49 -0800546 case kWhatGetDefaultBufferingSettings:
547 {
548 sp<AReplyToken> replyID;
549 CHECK(msg->senderAwaitsResponse(&replyID));
550
551 ALOGV("kWhatGetDefaultBufferingSettings");
552 BufferingSettings buffering;
553 status_t err = OK;
554 if (mSource != NULL) {
555 err = mSource->getDefaultBufferingSettings(&buffering);
556 } else {
557 err = INVALID_OPERATION;
558 }
559 sp<AMessage> response = new AMessage;
560 if (err == OK) {
561 writeToAMessage(response, buffering);
562 }
563 response->setInt32("err", err);
564 response->postReply(replyID);
565 break;
566 }
567
568 case kWhatSetBufferingSettings:
569 {
570 sp<AReplyToken> replyID;
571 CHECK(msg->senderAwaitsResponse(&replyID));
572
573 ALOGV("kWhatSetBufferingSettings");
574 BufferingSettings buffering;
575 readFromAMessage(msg, &buffering);
576 status_t err = OK;
577 if (mSource != NULL) {
578 err = mSource->setBufferingSettings(buffering);
579 } else {
580 err = INVALID_OPERATION;
581 }
582 sp<AMessage> response = new AMessage;
583 response->setInt32("err", err);
584 response->postReply(replyID);
585 break;
586 }
587
Andreas Huber9575c962013-02-05 13:59:56 -0800588 case kWhatPrepare:
589 {
Hassan Shojaniacefac142017-02-06 21:02:02 -0800590 ALOGV("onMessageReceived kWhatPrepare");
591
Andreas Huber9575c962013-02-05 13:59:56 -0800592 mSource->prepareAsync();
Andreas Huberf9334412010-12-15 15:17:42 -0800593 break;
594 }
595
Chong Zhangdcb89b32013-08-06 09:44:47 -0700596 case kWhatGetTrackInfo:
597 {
Lajos Molnar3f274362015-03-05 14:35:41 -0800598 sp<AReplyToken> replyID;
Chong Zhangdcb89b32013-08-06 09:44:47 -0700599 CHECK(msg->senderAwaitsResponse(&replyID));
600
Chong Zhang404fced2014-06-11 14:45:31 -0700601 Parcel* reply;
602 CHECK(msg->findPointer("reply", (void**)&reply));
603
604 size_t inbandTracks = 0;
Chong Zhangdcb89b32013-08-06 09:44:47 -0700605 if (mSource != NULL) {
Chong Zhang404fced2014-06-11 14:45:31 -0700606 inbandTracks = mSource->getTrackCount();
607 }
608
Chong Zhanga7fa1d92014-06-11 14:49:23 -0700609 size_t ccTracks = 0;
610 if (mCCDecoder != NULL) {
611 ccTracks = mCCDecoder->getTrackCount();
612 }
613
Chong Zhang404fced2014-06-11 14:45:31 -0700614 // total track count
Chong Zhanga7fa1d92014-06-11 14:49:23 -0700615 reply->writeInt32(inbandTracks + ccTracks);
Chong Zhang404fced2014-06-11 14:45:31 -0700616
617 // write inband tracks
618 for (size_t i = 0; i < inbandTracks; ++i) {
619 writeTrackInfo(reply, mSource->getTrackInfo(i));
Chong Zhangdcb89b32013-08-06 09:44:47 -0700620 }
621
Chong Zhanga7fa1d92014-06-11 14:49:23 -0700622 // write CC track
623 for (size_t i = 0; i < ccTracks; ++i) {
624 writeTrackInfo(reply, mCCDecoder->getTrackInfo(i));
625 }
626
Chong Zhangdcb89b32013-08-06 09:44:47 -0700627 sp<AMessage> response = new AMessage;
Chong Zhangdcb89b32013-08-06 09:44:47 -0700628 response->postReply(replyID);
629 break;
630 }
631
Robert Shih7c4f0d72014-07-09 18:53:31 -0700632 case kWhatGetSelectedTrack:
633 {
634 status_t err = INVALID_OPERATION;
635 if (mSource != NULL) {
636 err = OK;
637
638 int32_t type32;
639 CHECK(msg->findInt32("type", (int32_t*)&type32));
640 media_track_type type = (media_track_type)type32;
641 ssize_t selectedTrack = mSource->getSelectedTrack(type);
642
643 Parcel* reply;
644 CHECK(msg->findPointer("reply", (void**)&reply));
645 reply->writeInt32(selectedTrack);
646 }
647
648 sp<AMessage> response = new AMessage;
649 response->setInt32("err", err);
650
Lajos Molnar3f274362015-03-05 14:35:41 -0800651 sp<AReplyToken> replyID;
Robert Shih7c4f0d72014-07-09 18:53:31 -0700652 CHECK(msg->senderAwaitsResponse(&replyID));
653 response->postReply(replyID);
654 break;
655 }
656
Chong Zhangdcb89b32013-08-06 09:44:47 -0700657 case kWhatSelectTrack:
658 {
Lajos Molnar3f274362015-03-05 14:35:41 -0800659 sp<AReplyToken> replyID;
Chong Zhangdcb89b32013-08-06 09:44:47 -0700660 CHECK(msg->senderAwaitsResponse(&replyID));
661
Chong Zhang404fced2014-06-11 14:45:31 -0700662 size_t trackIndex;
663 int32_t select;
Robert Shih6ffb1fd2014-10-29 16:24:32 -0700664 int64_t timeUs;
Chong Zhang404fced2014-06-11 14:45:31 -0700665 CHECK(msg->findSize("trackIndex", &trackIndex));
666 CHECK(msg->findInt32("select", &select));
Robert Shih6ffb1fd2014-10-29 16:24:32 -0700667 CHECK(msg->findInt64("timeUs", &timeUs));
Chong Zhang404fced2014-06-11 14:45:31 -0700668
Chong Zhangdcb89b32013-08-06 09:44:47 -0700669 status_t err = INVALID_OPERATION;
Chong Zhang404fced2014-06-11 14:45:31 -0700670
671 size_t inbandTracks = 0;
Chong Zhangdcb89b32013-08-06 09:44:47 -0700672 if (mSource != NULL) {
Chong Zhang404fced2014-06-11 14:45:31 -0700673 inbandTracks = mSource->getTrackCount();
674 }
Chong Zhanga7fa1d92014-06-11 14:49:23 -0700675 size_t ccTracks = 0;
676 if (mCCDecoder != NULL) {
677 ccTracks = mCCDecoder->getTrackCount();
678 }
Chong Zhang404fced2014-06-11 14:45:31 -0700679
680 if (trackIndex < inbandTracks) {
Robert Shih6ffb1fd2014-10-29 16:24:32 -0700681 err = mSource->selectTrack(trackIndex, select, timeUs);
Robert Shihd3b0bbb2014-07-23 15:00:25 -0700682
683 if (!select && err == OK) {
684 int32_t type;
685 sp<AMessage> info = mSource->getTrackInfo(trackIndex);
686 if (info != NULL
687 && info->findInt32("type", &type)
688 && type == MEDIA_TRACK_TYPE_TIMEDTEXT) {
689 ++mTimedTextGeneration;
690 }
691 }
Chong Zhanga7fa1d92014-06-11 14:49:23 -0700692 } else {
693 trackIndex -= inbandTracks;
694
695 if (trackIndex < ccTracks) {
696 err = mCCDecoder->selectTrack(trackIndex, select);
697 }
Chong Zhangdcb89b32013-08-06 09:44:47 -0700698 }
699
700 sp<AMessage> response = new AMessage;
701 response->setInt32("err", err);
702
703 response->postReply(replyID);
704 break;
705 }
706
Andreas Huberb7c8e912012-11-27 15:02:53 -0800707 case kWhatPollDuration:
708 {
709 int32_t generation;
710 CHECK(msg->findInt32("generation", &generation));
711
712 if (generation != mPollDurationGeneration) {
713 // stale
714 break;
715 }
716
717 int64_t durationUs;
718 if (mDriver != NULL && mSource->getDuration(&durationUs) == OK) {
719 sp<NuPlayerDriver> driver = mDriver.promote();
720 if (driver != NULL) {
721 driver->notifyDuration(durationUs);
722 }
723 }
724
725 msg->post(1000000ll); // poll again in a second.
726 break;
727 }
728
Lajos Molnar1de1e252015-04-30 18:18:34 -0700729 case kWhatSetVideoSurface:
Andreas Huberf9334412010-12-15 15:17:42 -0800730 {
Andreas Huberf9334412010-12-15 15:17:42 -0800731
732 sp<RefBase> obj;
Lajos Molnar1de1e252015-04-30 18:18:34 -0700733 CHECK(msg->findObject("surface", &obj));
734 sp<Surface> surface = static_cast<Surface *>(obj.get());
Lajos Molnara81c6222015-07-10 19:17:45 -0700735
736 ALOGD("onSetVideoSurface(%p, %s video decoder)",
737 surface.get(),
Wei Jia6b7d2ed2015-08-10 15:11:54 -0700738 (mSource != NULL && mStarted && mSource->getFormat(false /* audio */) != NULL
Lajos Molnara81c6222015-07-10 19:17:45 -0700739 && mVideoDecoder != NULL) ? "have" : "no");
740
Wei Jia6b7d2ed2015-08-10 15:11:54 -0700741 // Need to check mStarted before calling mSource->getFormat because NuPlayer might
742 // be in preparing state and it could take long time.
743 // When mStarted is true, mSource must have been set.
744 if (mSource == NULL || !mStarted || mSource->getFormat(false /* audio */) == NULL
Lajos Molnara81c6222015-07-10 19:17:45 -0700745 // NOTE: mVideoDecoder's mSurface is always non-null
746 || (mVideoDecoder != NULL && mVideoDecoder->setVideoSurface(surface) == OK)) {
Lajos Molnar1de1e252015-04-30 18:18:34 -0700747 performSetSurface(surface);
Wei Jiafef808d2014-10-31 17:57:05 -0700748 break;
749 }
750
751 mDeferredActions.push_back(
752 new FlushDecoderAction(FLUSH_CMD_FLUSH /* audio */,
753 FLUSH_CMD_SHUTDOWN /* video */));
754
Lajos Molnar1de1e252015-04-30 18:18:34 -0700755 mDeferredActions.push_back(new SetSurfaceAction(surface));
James Dong0d268a32012-08-31 12:18:27 -0700756
Wei Jiac6e58412015-07-10 18:04:55 -0700757 if (obj != NULL || mAudioDecoder != NULL) {
Wei Jiafef808d2014-10-31 17:57:05 -0700758 if (mStarted) {
Andy Hung73535852014-09-05 11:42:58 -0700759 // Issue a seek to refresh the video screen only if started otherwise
760 // the extractor may not yet be started and will assert.
761 // If the video decoder is not set (perhaps audio only in this case)
762 // do not perform a seek as it is not needed.
Ronghua Wua73d9e02014-10-08 15:13:29 -0700763 int64_t currentPositionUs = 0;
764 if (getCurrentPosition(&currentPositionUs) == OK) {
765 mDeferredActions.push_back(
Wei Jiac5de0912016-11-18 10:22:14 -0800766 new SeekAction(currentPositionUs,
767 MediaPlayerSeekMode::SEEK_PREVIOUS_SYNC /* mode */));
Ronghua Wua73d9e02014-10-08 15:13:29 -0700768 }
Andy Hung73535852014-09-05 11:42:58 -0700769 }
Wei Jiaac428aa2014-09-02 19:01:34 -0700770
Andreas Huber57a339c2012-12-03 11:18:00 -0800771 // If there is a new surface texture, instantiate decoders
772 // again if possible.
773 mDeferredActions.push_back(
774 new SimpleAction(&NuPlayer::performScanSources));
775 }
776
Chong Zhangf8d71772014-11-26 15:08:34 -0800777 // After a flush without shutdown, decoder is paused.
778 // Don't resume it until source seek is done, otherwise it could
Chong Zhang7137ec72014-11-12 16:41:05 -0800779 // start pulling stale data too soon.
780 mDeferredActions.push_back(
Chong Zhangf8d71772014-11-26 15:08:34 -0800781 new ResumeDecoderAction(false /* needNotify */));
Chong Zhang7137ec72014-11-12 16:41:05 -0800782
Andreas Huber57a339c2012-12-03 11:18:00 -0800783 processDeferredActions();
Andreas Huberf9334412010-12-15 15:17:42 -0800784 break;
785 }
786
787 case kWhatSetAudioSink:
788 {
Steve Block3856b092011-10-20 11:56:00 +0100789 ALOGV("kWhatSetAudioSink");
Andreas Huberf9334412010-12-15 15:17:42 -0800790
791 sp<RefBase> obj;
792 CHECK(msg->findObject("sink", &obj));
793
794 mAudioSink = static_cast<MediaPlayerBase::AudioSink *>(obj.get());
795 break;
796 }
797
798 case kWhatStart:
799 {
Steve Block3856b092011-10-20 11:56:00 +0100800 ALOGV("kWhatStart");
Wei Jia94211742014-10-28 17:09:06 -0700801 if (mStarted) {
Chong Zhang8a048332015-05-06 15:16:28 -0700802 // do not resume yet if the source is still buffering
803 if (!mPausedForBuffering) {
804 onResume();
805 }
Wei Jia94211742014-10-28 17:09:06 -0700806 } else {
807 onStart();
Lajos Molnar09524832014-07-17 14:29:51 -0700808 }
Chong Zhangefbb6192015-01-30 17:13:27 -0800809 mPausedByClient = false;
Andreas Huberf9334412010-12-15 15:17:42 -0800810 break;
811 }
812
Lajos Molnar3a474aa2015-04-24 17:10:07 -0700813 case kWhatConfigPlayback:
Wei Jia98160162015-02-04 17:01:11 -0800814 {
Lajos Molnar3a474aa2015-04-24 17:10:07 -0700815 sp<AReplyToken> replyID;
816 CHECK(msg->senderAwaitsResponse(&replyID));
817 AudioPlaybackRate rate /* sanitized */;
818 readFromAMessage(msg, &rate);
819 status_t err = OK;
Wei Jia98160162015-02-04 17:01:11 -0800820 if (mRenderer != NULL) {
Wei Jiabf70feb2016-02-19 15:47:16 -0800821 // AudioSink allows only 1.f and 0.f for offload mode.
822 // For other speed, switch to non-offload mode.
823 if (mOffloadAudio && ((rate.mSpeed != 0.f && rate.mSpeed != 1.f)
824 || rate.mPitch != 1.f)) {
825 int64_t currentPositionUs;
826 if (getCurrentPosition(&currentPositionUs) != OK) {
827 currentPositionUs = mPreviousSeekTimeUs;
828 }
829
830 // Set mPlaybackSettings so that the new audio decoder can
831 // be created correctly.
832 mPlaybackSettings = rate;
833 if (!mPaused) {
834 mRenderer->pause();
835 }
Wei Jia5031b2f2016-02-25 11:19:31 -0800836 restartAudio(
Wei Jiabf70feb2016-02-19 15:47:16 -0800837 currentPositionUs, true /* forceNonOffload */,
838 true /* needsToCreateAudioDecoder */);
839 if (!mPaused) {
840 mRenderer->resume();
841 }
842 }
843
Lajos Molnar3a474aa2015-04-24 17:10:07 -0700844 err = mRenderer->setPlaybackSettings(rate);
845 }
846 if (err == OK) {
847 if (rate.mSpeed == 0.f) {
848 onPause();
Wei Jia351ce872016-02-10 13:21:59 -0800849 mPausedByClient = true;
Lajos Molnar3a474aa2015-04-24 17:10:07 -0700850 // save all other settings (using non-paused speed)
851 // so we can restore them on start
852 AudioPlaybackRate newRate = rate;
853 newRate.mSpeed = mPlaybackSettings.mSpeed;
854 mPlaybackSettings = newRate;
855 } else { /* rate.mSpeed != 0.f */
Lajos Molnar3a474aa2015-04-24 17:10:07 -0700856 mPlaybackSettings = rate;
Wei Jia8a092d32016-06-03 14:57:24 -0700857 if (mStarted) {
858 // do not resume yet if the source is still buffering
859 if (!mPausedForBuffering) {
860 onResume();
861 }
862 } else if (mPrepared) {
863 onStart();
864 }
865
866 mPausedByClient = false;
Lajos Molnar3a474aa2015-04-24 17:10:07 -0700867 }
Wei Jia98160162015-02-04 17:01:11 -0800868 }
Ronghua Wu8db88132015-04-22 13:51:35 -0700869
870 if (mVideoDecoder != NULL) {
Praveen Chavanbbaa1442016-04-08 13:33:49 -0700871 sp<AMessage> params = new AMessage();
872 params->setFloat("playback-speed", mPlaybackSettings.mSpeed);
873 mVideoDecoder->setParameters(params);
Ronghua Wu8db88132015-04-22 13:51:35 -0700874 }
875
Lajos Molnar3a474aa2015-04-24 17:10:07 -0700876 sp<AMessage> response = new AMessage;
877 response->setInt32("err", err);
878 response->postReply(replyID);
879 break;
880 }
881
882 case kWhatGetPlaybackSettings:
883 {
884 sp<AReplyToken> replyID;
885 CHECK(msg->senderAwaitsResponse(&replyID));
886 AudioPlaybackRate rate = mPlaybackSettings;
887 status_t err = OK;
888 if (mRenderer != NULL) {
889 err = mRenderer->getPlaybackSettings(&rate);
890 }
891 if (err == OK) {
892 // get playback settings used by renderer, as it may be
893 // slightly off due to audiosink not taking small changes.
894 mPlaybackSettings = rate;
895 if (mPaused) {
896 rate.mSpeed = 0.f;
897 }
898 }
899 sp<AMessage> response = new AMessage;
900 if (err == OK) {
901 writeToAMessage(response, rate);
902 }
903 response->setInt32("err", err);
904 response->postReply(replyID);
905 break;
906 }
907
908 case kWhatConfigSync:
909 {
910 sp<AReplyToken> replyID;
911 CHECK(msg->senderAwaitsResponse(&replyID));
912
913 ALOGV("kWhatConfigSync");
914 AVSyncSettings sync;
915 float videoFpsHint;
916 readFromAMessage(msg, &sync, &videoFpsHint);
917 status_t err = OK;
918 if (mRenderer != NULL) {
919 err = mRenderer->setSyncSettings(sync, videoFpsHint);
920 }
921 if (err == OK) {
922 mSyncSettings = sync;
923 mVideoFpsHint = videoFpsHint;
924 }
925 sp<AMessage> response = new AMessage;
926 response->setInt32("err", err);
927 response->postReply(replyID);
928 break;
929 }
930
931 case kWhatGetSyncSettings:
932 {
933 sp<AReplyToken> replyID;
934 CHECK(msg->senderAwaitsResponse(&replyID));
935 AVSyncSettings sync = mSyncSettings;
936 float videoFps = mVideoFpsHint;
937 status_t err = OK;
938 if (mRenderer != NULL) {
939 err = mRenderer->getSyncSettings(&sync, &videoFps);
940 if (err == OK) {
941 mSyncSettings = sync;
942 mVideoFpsHint = videoFps;
943 }
944 }
945 sp<AMessage> response = new AMessage;
946 if (err == OK) {
947 writeToAMessage(response, sync, videoFps);
948 }
949 response->setInt32("err", err);
950 response->postReply(replyID);
Wei Jia98160162015-02-04 17:01:11 -0800951 break;
952 }
953
Andreas Huberf9334412010-12-15 15:17:42 -0800954 case kWhatScanSources:
955 {
Andreas Huber1aef2112011-01-04 14:01:29 -0800956 int32_t generation;
957 CHECK(msg->findInt32("generation", &generation));
958 if (generation != mScanSourcesGeneration) {
959 // Drop obsolete msg.
960 break;
961 }
962
Andreas Huber5bc087c2010-12-23 10:27:40 -0800963 mScanSourcesPending = false;
964
Steve Block3856b092011-10-20 11:56:00 +0100965 ALOGV("scanning sources haveAudio=%d, haveVideo=%d",
Andreas Huber43c3e6c2011-01-05 12:17:08 -0800966 mAudioDecoder != NULL, mVideoDecoder != NULL);
967
Andreas Huberb7c8e912012-11-27 15:02:53 -0800968 bool mHadAnySourcesBefore =
969 (mAudioDecoder != NULL) || (mVideoDecoder != NULL);
Robert Shih7350b052015-10-01 15:50:14 -0700970 bool rescan = false;
Andreas Huberb7c8e912012-11-27 15:02:53 -0800971
Andy Hung282a7e32014-08-14 15:56:34 -0700972 // initialize video before audio because successful initialization of
973 // video may change deep buffer mode of audio.
Lajos Molnar1de1e252015-04-30 18:18:34 -0700974 if (mSurface != NULL) {
Robert Shih7350b052015-10-01 15:50:14 -0700975 if (instantiateDecoder(false, &mVideoDecoder) == -EWOULDBLOCK) {
976 rescan = true;
977 }
Haynes Mathew George5d246ef2012-07-09 10:36:57 -0700978 }
Andreas Huberf9334412010-12-15 15:17:42 -0800979
Ronghua Wua10fd232014-11-06 16:15:20 -0800980 // Don't try to re-open audio sink if there's an existing decoder.
981 if (mAudioSink != NULL && mAudioDecoder == NULL) {
Robert Shih7350b052015-10-01 15:50:14 -0700982 if (instantiateDecoder(true, &mAudioDecoder) == -EWOULDBLOCK) {
983 rescan = true;
984 }
Andreas Huberf9334412010-12-15 15:17:42 -0800985 }
986
Andreas Huberb7c8e912012-11-27 15:02:53 -0800987 if (!mHadAnySourcesBefore
988 && (mAudioDecoder != NULL || mVideoDecoder != NULL)) {
989 // This is the first time we've found anything playable.
990
Andreas Huber9575c962013-02-05 13:59:56 -0800991 if (mSourceFlags & Source::FLAG_DYNAMIC_DURATION) {
Andreas Huberb7c8e912012-11-27 15:02:53 -0800992 schedulePollDuration();
993 }
994 }
995
Andreas Hubereac68ba2011-09-27 12:12:25 -0700996 status_t err;
997 if ((err = mSource->feedMoreTSData()) != OK) {
Andreas Huber1aef2112011-01-04 14:01:29 -0800998 if (mAudioDecoder == NULL && mVideoDecoder == NULL) {
999 // We're not currently decoding anything (no audio or
1000 // video tracks found) and we just ran out of input data.
Andreas Hubereac68ba2011-09-27 12:12:25 -07001001
1002 if (err == ERROR_END_OF_STREAM) {
1003 notifyListener(MEDIA_PLAYBACK_COMPLETE, 0, 0);
1004 } else {
1005 notifyListener(MEDIA_ERROR, MEDIA_ERROR_UNKNOWN, err);
1006 }
Andreas Huber1aef2112011-01-04 14:01:29 -08001007 }
Andreas Huberf9334412010-12-15 15:17:42 -08001008 break;
1009 }
1010
Robert Shih7350b052015-10-01 15:50:14 -07001011 if (rescan) {
Andreas Huberf9334412010-12-15 15:17:42 -08001012 msg->post(100000ll);
Andreas Huber5bc087c2010-12-23 10:27:40 -08001013 mScanSourcesPending = true;
Andreas Huberf9334412010-12-15 15:17:42 -08001014 }
1015 break;
1016 }
1017
1018 case kWhatVideoNotify:
1019 case kWhatAudioNotify:
1020 {
1021 bool audio = msg->what() == kWhatAudioNotify;
1022
Wei Jia88703c32014-08-06 11:24:07 -07001023 int32_t currentDecoderGeneration =
1024 (audio? mAudioDecoderGeneration : mVideoDecoderGeneration);
1025 int32_t requesterGeneration = currentDecoderGeneration - 1;
1026 CHECK(msg->findInt32("generation", &requesterGeneration));
1027
1028 if (requesterGeneration != currentDecoderGeneration) {
1029 ALOGV("got message from old %s decoder, generation(%d:%d)",
1030 audio ? "audio" : "video", requesterGeneration,
1031 currentDecoderGeneration);
1032 sp<AMessage> reply;
1033 if (!(msg->findMessage("reply", &reply))) {
1034 return;
1035 }
1036
1037 reply->setInt32("err", INFO_DISCONTINUITY);
1038 reply->post();
1039 return;
1040 }
1041
Andreas Huberf9334412010-12-15 15:17:42 -08001042 int32_t what;
Lajos Molnar1cd13982014-01-17 15:12:51 -08001043 CHECK(msg->findInt32("what", &what));
Andreas Huberf9334412010-12-15 15:17:42 -08001044
Chong Zhang7137ec72014-11-12 16:41:05 -08001045 if (what == DecoderBase::kWhatInputDiscontinuity) {
1046 int32_t formatChange;
1047 CHECK(msg->findInt32("formatChange", &formatChange));
Andreas Huberf9334412010-12-15 15:17:42 -08001048
Chong Zhang7137ec72014-11-12 16:41:05 -08001049 ALOGV("%s discontinuity: formatChange %d",
1050 audio ? "audio" : "video", formatChange);
1051
1052 if (formatChange) {
1053 mDeferredActions.push_back(
1054 new FlushDecoderAction(
1055 audio ? FLUSH_CMD_SHUTDOWN : FLUSH_CMD_NONE,
1056 audio ? FLUSH_CMD_NONE : FLUSH_CMD_SHUTDOWN));
Andreas Huberf9334412010-12-15 15:17:42 -08001057 }
Chong Zhang7137ec72014-11-12 16:41:05 -08001058
1059 mDeferredActions.push_back(
1060 new SimpleAction(
1061 &NuPlayer::performScanSources));
1062
1063 processDeferredActions();
1064 } else if (what == DecoderBase::kWhatEOS) {
Andreas Huberdc9bacd2011-09-26 10:53:29 -07001065 int32_t err;
Lajos Molnar1cd13982014-01-17 15:12:51 -08001066 CHECK(msg->findInt32("err", &err));
Andreas Huberdc9bacd2011-09-26 10:53:29 -07001067
1068 if (err == ERROR_END_OF_STREAM) {
Steve Block3856b092011-10-20 11:56:00 +01001069 ALOGV("got %s decoder EOS", audio ? "audio" : "video");
Andreas Huberdc9bacd2011-09-26 10:53:29 -07001070 } else {
Steve Block3856b092011-10-20 11:56:00 +01001071 ALOGV("got %s decoder EOS w/ error %d",
Andreas Huberdc9bacd2011-09-26 10:53:29 -07001072 audio ? "audio" : "video",
1073 err);
1074 }
1075
1076 mRenderer->queueEOS(audio, err);
Chong Zhang7137ec72014-11-12 16:41:05 -08001077 } else if (what == DecoderBase::kWhatFlushCompleted) {
Steve Block3856b092011-10-20 11:56:00 +01001078 ALOGV("decoder %s flush completed", audio ? "audio" : "video");
Andreas Huberf9334412010-12-15 15:17:42 -08001079
Andy Hung8d121d42014-10-03 09:53:53 -07001080 handleFlushComplete(audio, true /* isDecoder */);
Andreas Huber3831a062010-12-21 10:22:33 -08001081 finishFlushIfPossible();
Chong Zhang7137ec72014-11-12 16:41:05 -08001082 } else if (what == DecoderBase::kWhatVideoSizeChanged) {
Lajos Molnar1cd13982014-01-17 15:12:51 -08001083 sp<AMessage> format;
1084 CHECK(msg->findMessage("format", &format));
1085
Wei Jiac6cfd702014-11-11 16:33:20 -08001086 sp<AMessage> inputFormat =
1087 mSource->getFormat(false /* audio */);
Andreas Huber3831a062010-12-21 10:22:33 -08001088
Bartosz Dolewski26936f72014-12-11 12:47:08 +01001089 setVideoScalingMode(mVideoScalingMode);
Wei Jiac6cfd702014-11-11 16:33:20 -08001090 updateVideoSize(inputFormat, format);
Chong Zhang7137ec72014-11-12 16:41:05 -08001091 } else if (what == DecoderBase::kWhatShutdownCompleted) {
Steve Block3856b092011-10-20 11:56:00 +01001092 ALOGV("%s shutdown completed", audio ? "audio" : "video");
Andreas Huber3831a062010-12-21 10:22:33 -08001093 if (audio) {
1094 mAudioDecoder.clear();
Wei Jia57568df2014-09-22 10:16:29 -07001095 ++mAudioDecoderGeneration;
Andreas Huber3831a062010-12-21 10:22:33 -08001096
1097 CHECK_EQ((int)mFlushingAudio, (int)SHUTTING_DOWN_DECODER);
1098 mFlushingAudio = SHUT_DOWN;
1099 } else {
1100 mVideoDecoder.clear();
Wei Jia57568df2014-09-22 10:16:29 -07001101 ++mVideoDecoderGeneration;
Andreas Huber3831a062010-12-21 10:22:33 -08001102
1103 CHECK_EQ((int)mFlushingVideo, (int)SHUTTING_DOWN_DECODER);
1104 mFlushingVideo = SHUT_DOWN;
1105 }
1106
1107 finishFlushIfPossible();
Chong Zhangf8d71772014-11-26 15:08:34 -08001108 } else if (what == DecoderBase::kWhatResumeCompleted) {
1109 finishResume();
Chong Zhang7137ec72014-11-12 16:41:05 -08001110 } else if (what == DecoderBase::kWhatError) {
Chong Zhangf4c0a942014-08-11 15:14:10 -07001111 status_t err;
Andy Hung2abde2c2014-09-30 14:40:32 -07001112 if (!msg->findInt32("err", &err) || err == OK) {
Chong Zhangf4c0a942014-08-11 15:14:10 -07001113 err = UNKNOWN_ERROR;
1114 }
Andy Hungcf31f1e2014-09-23 14:59:01 -07001115
Andy Hung2abde2c2014-09-30 14:40:32 -07001116 // Decoder errors can be due to Source (e.g. from streaming),
1117 // or from decoding corrupted bitstreams, or from other decoder
1118 // MediaCodec operations (e.g. from an ongoing reset or seek).
Andy Hung202bce12014-12-03 11:47:36 -08001119 // They may also be due to openAudioSink failure at
1120 // decoder start or after a format change.
Andy Hung2abde2c2014-09-30 14:40:32 -07001121 //
1122 // We try to gracefully shut down the affected decoder if possible,
1123 // rather than trying to force the shutdown with something
1124 // similar to performReset(). This method can lead to a hang
1125 // if MediaCodec functions block after an error, but they should
1126 // typically return INVALID_OPERATION instead of blocking.
1127
1128 FlushStatus *flushing = audio ? &mFlushingAudio : &mFlushingVideo;
1129 ALOGE("received error(%#x) from %s decoder, flushing(%d), now shutting down",
1130 err, audio ? "audio" : "video", *flushing);
1131
1132 switch (*flushing) {
1133 case NONE:
1134 mDeferredActions.push_back(
Wei Jiafef808d2014-10-31 17:57:05 -07001135 new FlushDecoderAction(
1136 audio ? FLUSH_CMD_SHUTDOWN : FLUSH_CMD_NONE,
1137 audio ? FLUSH_CMD_NONE : FLUSH_CMD_SHUTDOWN));
Andy Hung2abde2c2014-09-30 14:40:32 -07001138 processDeferredActions();
1139 break;
1140 case FLUSHING_DECODER:
1141 *flushing = FLUSHING_DECODER_SHUTDOWN; // initiate shutdown after flush.
1142 break; // Wait for flush to complete.
1143 case FLUSHING_DECODER_SHUTDOWN:
1144 break; // Wait for flush to complete.
1145 case SHUTTING_DOWN_DECODER:
1146 break; // Wait for shutdown to complete.
1147 case FLUSHED:
Hassan Shojaniacefac142017-02-06 21:02:02 -08001148 // Both secure audio/video now. Legacy Widevine did it for secure video.
Andy Hung2abde2c2014-09-30 14:40:32 -07001149 // Widevine source reads must stop before releasing the video decoder.
Hassan Shojaniacefac142017-02-06 21:02:02 -08001150 if (mSource != NULL && mIsDrmProtected) {
Andy Hung2abde2c2014-09-30 14:40:32 -07001151 mSource->stop();
Robert Shih0c61a0d2015-07-06 15:09:10 -07001152 mSourceStarted = false;
Andy Hung2abde2c2014-09-30 14:40:32 -07001153 }
1154 getDecoder(audio)->initiateShutdown(); // In the middle of a seek.
1155 *flushing = SHUTTING_DOWN_DECODER; // Shut down.
1156 break;
1157 case SHUT_DOWN:
1158 finishFlushIfPossible(); // Should not occur.
1159 break; // Finish anyways.
Marco Nelissen9e2b7912014-08-18 16:13:03 -07001160 }
Andy Hung2abde2c2014-09-30 14:40:32 -07001161 notifyListener(MEDIA_ERROR, MEDIA_ERROR_UNKNOWN, err);
Lajos Molnar1cd13982014-01-17 15:12:51 -08001162 } else {
1163 ALOGV("Unhandled decoder notification %d '%c%c%c%c'.",
Andreas Hubera1f8ab02012-11-30 10:53:22 -08001164 what,
1165 what >> 24,
1166 (what >> 16) & 0xff,
1167 (what >> 8) & 0xff,
1168 what & 0xff);
Andreas Huberf9334412010-12-15 15:17:42 -08001169 }
1170
1171 break;
1172 }
1173
1174 case kWhatRendererNotify:
1175 {
Wei Jia57568df2014-09-22 10:16:29 -07001176 int32_t requesterGeneration = mRendererGeneration - 1;
1177 CHECK(msg->findInt32("generation", &requesterGeneration));
1178 if (requesterGeneration != mRendererGeneration) {
1179 ALOGV("got message from old renderer, generation(%d:%d)",
1180 requesterGeneration, mRendererGeneration);
1181 return;
1182 }
1183
Andreas Huberf9334412010-12-15 15:17:42 -08001184 int32_t what;
1185 CHECK(msg->findInt32("what", &what));
1186
1187 if (what == Renderer::kWhatEOS) {
1188 int32_t audio;
1189 CHECK(msg->findInt32("audio", &audio));
1190
Andreas Huberc92fd242011-08-16 13:48:44 -07001191 int32_t finalResult;
1192 CHECK(msg->findInt32("finalResult", &finalResult));
1193
Andreas Huberf9334412010-12-15 15:17:42 -08001194 if (audio) {
1195 mAudioEOS = true;
1196 } else {
1197 mVideoEOS = true;
1198 }
1199
Andreas Huberc92fd242011-08-16 13:48:44 -07001200 if (finalResult == ERROR_END_OF_STREAM) {
Steve Block3856b092011-10-20 11:56:00 +01001201 ALOGV("reached %s EOS", audio ? "audio" : "video");
Andreas Huberc92fd242011-08-16 13:48:44 -07001202 } else {
Steve Block29357bc2012-01-06 19:20:56 +00001203 ALOGE("%s track encountered an error (%d)",
Andreas Huberc92fd242011-08-16 13:48:44 -07001204 audio ? "audio" : "video", finalResult);
1205
1206 notifyListener(
1207 MEDIA_ERROR, MEDIA_ERROR_UNKNOWN, finalResult);
1208 }
Andreas Huberf9334412010-12-15 15:17:42 -08001209
1210 if ((mAudioEOS || mAudioDecoder == NULL)
1211 && (mVideoEOS || mVideoDecoder == NULL)) {
1212 notifyListener(MEDIA_PLAYBACK_COMPLETE, 0, 0);
1213 }
Andreas Huber3fe62152011-09-16 15:09:22 -07001214 } else if (what == Renderer::kWhatFlushComplete) {
Andreas Huberf9334412010-12-15 15:17:42 -08001215 int32_t audio;
1216 CHECK(msg->findInt32("audio", &audio));
1217
Wei Jia3261f0d2015-10-08 13:58:33 -07001218 if (audio) {
1219 mAudioEOS = false;
1220 } else {
1221 mVideoEOS = false;
1222 }
1223
Steve Block3856b092011-10-20 11:56:00 +01001224 ALOGV("renderer %s flush completed.", audio ? "audio" : "video");
Wei Jiada4252f2015-07-14 18:15:28 -07001225 if (audio && (mFlushingAudio == NONE || mFlushingAudio == FLUSHED
1226 || mFlushingAudio == SHUT_DOWN)) {
1227 // Flush has been handled by tear down.
1228 break;
1229 }
Andy Hung8d121d42014-10-03 09:53:53 -07001230 handleFlushComplete(audio, false /* isDecoder */);
1231 finishFlushIfPossible();
James Dongf57b4ea2012-07-20 13:38:36 -07001232 } else if (what == Renderer::kWhatVideoRenderingStart) {
1233 notifyListener(MEDIA_INFO, MEDIA_INFO_RENDERING_START, 0);
Lajos Molnarcbaffcf2013-08-14 18:30:38 -07001234 } else if (what == Renderer::kWhatMediaRenderingStart) {
1235 ALOGV("media rendering started");
1236 notifyListener(MEDIA_STARTED, 0, 0);
Ronghua Wufaeb0f22015-05-21 12:20:21 -07001237 } else if (what == Renderer::kWhatAudioTearDown) {
Ronghua Wu08529172014-10-02 16:55:52 -07001238 int32_t reason;
1239 CHECK(msg->findInt32("reason", &reason));
Ronghua Wufaeb0f22015-05-21 12:20:21 -07001240 ALOGV("Tear down audio with reason %d.", reason);
Wei Jia8456ddd2016-04-22 14:46:43 -07001241 if (reason == Renderer::kDueToTimeout && !(mPaused && mOffloadAudio)) {
1242 // TimeoutWhenPaused is only for offload mode.
1243 ALOGW("Receive a stale message for teardown.");
1244 break;
1245 }
Wei Jiada4252f2015-07-14 18:15:28 -07001246 int64_t positionUs;
Robert Shih1a5c8592015-08-04 18:07:44 -07001247 if (!msg->findInt64("positionUs", &positionUs)) {
1248 positionUs = mPreviousSeekTimeUs;
1249 }
Ronghua Wufaeb0f22015-05-21 12:20:21 -07001250
Wei Jia5031b2f2016-02-25 11:19:31 -08001251 restartAudio(
Wei Jiaa05f1e32016-03-25 16:31:22 -07001252 positionUs, reason == Renderer::kForceNonOffload /* forceNonOffload */,
1253 reason != Renderer::kDueToTimeout /* needsToCreateAudioDecoder */);
Andreas Huberf9334412010-12-15 15:17:42 -08001254 }
1255 break;
1256 }
1257
1258 case kWhatMoreDataQueued:
1259 {
1260 break;
1261 }
1262
Andreas Huber1aef2112011-01-04 14:01:29 -08001263 case kWhatReset:
1264 {
Steve Block3856b092011-10-20 11:56:00 +01001265 ALOGV("kWhatReset");
Andreas Huber1aef2112011-01-04 14:01:29 -08001266
Ronghua Wu64c2d172015-10-07 16:52:19 -07001267 mResetting = true;
1268
Andreas Hubera1f8ab02012-11-30 10:53:22 -08001269 mDeferredActions.push_back(
Wei Jiafef808d2014-10-31 17:57:05 -07001270 new FlushDecoderAction(
1271 FLUSH_CMD_SHUTDOWN /* audio */,
1272 FLUSH_CMD_SHUTDOWN /* video */));
Andreas Huberb7c8e912012-11-27 15:02:53 -08001273
Andreas Hubera1f8ab02012-11-30 10:53:22 -08001274 mDeferredActions.push_back(
1275 new SimpleAction(&NuPlayer::performReset));
Andreas Huberb58ce9f2011-11-28 16:27:35 -08001276
Andreas Hubera1f8ab02012-11-30 10:53:22 -08001277 processDeferredActions();
Andreas Huber1aef2112011-01-04 14:01:29 -08001278 break;
1279 }
1280
Andreas Huber43c3e6c2011-01-05 12:17:08 -08001281 case kWhatSeek:
1282 {
1283 int64_t seekTimeUs;
Wei Jiac5de0912016-11-18 10:22:14 -08001284 int32_t mode;
Wei Jiae427abf2014-09-22 15:21:11 -07001285 int32_t needNotify;
Andreas Huber43c3e6c2011-01-05 12:17:08 -08001286 CHECK(msg->findInt64("seekTimeUs", &seekTimeUs));
Wei Jiac5de0912016-11-18 10:22:14 -08001287 CHECK(msg->findInt32("mode", &mode));
Wei Jiae427abf2014-09-22 15:21:11 -07001288 CHECK(msg->findInt32("needNotify", &needNotify));
Andreas Huber43c3e6c2011-01-05 12:17:08 -08001289
Wei Jiac5de0912016-11-18 10:22:14 -08001290 ALOGV("kWhatSeek seekTimeUs=%lld us, mode=%d, needNotify=%d",
1291 (long long)seekTimeUs, mode, needNotify);
Andreas Huber43c3e6c2011-01-05 12:17:08 -08001292
Wei Jia1061c9c2015-05-19 16:02:17 -07001293 if (!mStarted) {
1294 // Seek before the player is started. In order to preview video,
1295 // need to start the player and pause it. This branch is called
1296 // only once if needed. After the player is started, any seek
1297 // operation will go through normal path.
Robert Shih0c61a0d2015-07-06 15:09:10 -07001298 // Audio-only cases are handled separately.
Wei Jiac5de0912016-11-18 10:22:14 -08001299 onStart(seekTimeUs, (MediaPlayerSeekMode)mode);
Robert Shih0c61a0d2015-07-06 15:09:10 -07001300 if (mStarted) {
1301 onPause();
1302 mPausedByClient = true;
1303 }
Wei Jia1061c9c2015-05-19 16:02:17 -07001304 if (needNotify) {
1305 notifyDriverSeekComplete();
1306 }
1307 break;
1308 }
1309
Andreas Hubera1f8ab02012-11-30 10:53:22 -08001310 mDeferredActions.push_back(
Wei Jiafef808d2014-10-31 17:57:05 -07001311 new FlushDecoderAction(FLUSH_CMD_FLUSH /* audio */,
1312 FLUSH_CMD_FLUSH /* video */));
Andreas Huber43c3e6c2011-01-05 12:17:08 -08001313
Wei Jiae427abf2014-09-22 15:21:11 -07001314 mDeferredActions.push_back(
Wei Jiac5de0912016-11-18 10:22:14 -08001315 new SeekAction(seekTimeUs, (MediaPlayerSeekMode)mode));
Andreas Huber43c3e6c2011-01-05 12:17:08 -08001316
Chong Zhangf8d71772014-11-26 15:08:34 -08001317 // After a flush without shutdown, decoder is paused.
1318 // Don't resume it until source seek is done, otherwise it could
Chong Zhang7137ec72014-11-12 16:41:05 -08001319 // start pulling stale data too soon.
1320 mDeferredActions.push_back(
Chong Zhangf8d71772014-11-26 15:08:34 -08001321 new ResumeDecoderAction(needNotify));
Chong Zhang7137ec72014-11-12 16:41:05 -08001322
Andreas Hubera1f8ab02012-11-30 10:53:22 -08001323 processDeferredActions();
Andreas Huber43c3e6c2011-01-05 12:17:08 -08001324 break;
1325 }
1326
Andreas Huberb4082222011-01-20 15:23:04 -08001327 case kWhatPause:
1328 {
Chong Zhangefbb6192015-01-30 17:13:27 -08001329 onPause();
1330 mPausedByClient = true;
Andreas Huberb4082222011-01-20 15:23:04 -08001331 break;
1332 }
1333
Andreas Huberb5f25f02013-02-05 10:14:26 -08001334 case kWhatSourceNotify:
1335 {
Andreas Huber9575c962013-02-05 13:59:56 -08001336 onSourceNotify(msg);
Andreas Huberb5f25f02013-02-05 10:14:26 -08001337 break;
1338 }
1339
Chong Zhanga7fa1d92014-06-11 14:49:23 -07001340 case kWhatClosedCaptionNotify:
1341 {
1342 onClosedCaptionNotify(msg);
1343 break;
1344 }
1345
Hassan Shojaniacefac142017-02-06 21:02:02 -08001346 case kWhatPrepareDrm:
1347 {
1348 status_t status = onPrepareDrm(msg);
1349
1350 sp<AMessage> response = new AMessage;
1351 response->setInt32("status", status);
1352 sp<AReplyToken> replyID;
1353 CHECK(msg->senderAwaitsResponse(&replyID));
1354 response->postReply(replyID);
1355 break;
1356 }
1357
1358 case kWhatReleaseDrm:
1359 {
1360 status_t status = onReleaseDrm();
1361
1362 sp<AMessage> response = new AMessage;
1363 response->setInt32("status", status);
1364 sp<AReplyToken> replyID;
1365 CHECK(msg->senderAwaitsResponse(&replyID));
1366 response->postReply(replyID);
1367 break;
1368 }
1369
Andreas Huberf9334412010-12-15 15:17:42 -08001370 default:
1371 TRESPASS();
1372 break;
1373 }
1374}
1375
Wei Jia94211742014-10-28 17:09:06 -07001376void NuPlayer::onResume() {
Ronghua Wu64c2d172015-10-07 16:52:19 -07001377 if (!mPaused || mResetting) {
1378 ALOGD_IF(mResetting, "resetting, onResume discarded");
Chong Zhangefbb6192015-01-30 17:13:27 -08001379 return;
1380 }
1381 mPaused = false;
Wei Jia94211742014-10-28 17:09:06 -07001382 if (mSource != NULL) {
1383 mSource->resume();
1384 } else {
1385 ALOGW("resume called when source is gone or not set");
1386 }
1387 // |mAudioDecoder| may have been released due to the pause timeout, so re-create it if
1388 // needed.
1389 if (audioDecoderStillNeeded() && mAudioDecoder == NULL) {
1390 instantiateDecoder(true /* audio */, &mAudioDecoder);
1391 }
1392 if (mRenderer != NULL) {
1393 mRenderer->resume();
1394 } else {
1395 ALOGW("resume called when renderer is gone or not set");
1396 }
Ray Essickd4d00612017-01-03 09:36:27 -08001397
1398 mLastStartedPlayingTimeNs = systemTime();
Wei Jia94211742014-10-28 17:09:06 -07001399}
1400
Lajos Molnarfcd3e942015-03-31 10:06:48 -07001401status_t NuPlayer::onInstantiateSecureDecoders() {
1402 status_t err;
1403 if (!(mSourceFlags & Source::FLAG_SECURE)) {
1404 return BAD_TYPE;
1405 }
1406
1407 if (mRenderer != NULL) {
1408 ALOGE("renderer should not be set when instantiating secure decoders");
1409 return UNKNOWN_ERROR;
1410 }
1411
1412 // TRICKY: We rely on mRenderer being null, so that decoder does not start requesting
1413 // data on instantiation.
Lajos Molnar1de1e252015-04-30 18:18:34 -07001414 if (mSurface != NULL) {
Lajos Molnarfcd3e942015-03-31 10:06:48 -07001415 err = instantiateDecoder(false, &mVideoDecoder);
1416 if (err != OK) {
1417 return err;
1418 }
1419 }
1420
1421 if (mAudioSink != NULL) {
1422 err = instantiateDecoder(true, &mAudioDecoder);
1423 if (err != OK) {
1424 return err;
1425 }
1426 }
1427 return OK;
1428}
1429
Wei Jiac5de0912016-11-18 10:22:14 -08001430void NuPlayer::onStart(int64_t startPositionUs, MediaPlayerSeekMode mode) {
Hassan Shojaniacefac142017-02-06 21:02:02 -08001431 ALOGV("onStart: mCrypto: %p (%d)", mCrypto.get(),
1432 (mCrypto != NULL ? mCrypto->getStrongCount() : 0));
1433
Robert Shih0c61a0d2015-07-06 15:09:10 -07001434 if (!mSourceStarted) {
1435 mSourceStarted = true;
1436 mSource->start();
1437 }
1438 if (startPositionUs > 0) {
Wei Jiac5de0912016-11-18 10:22:14 -08001439 performSeek(startPositionUs, mode);
Robert Shih0c61a0d2015-07-06 15:09:10 -07001440 if (mSource->getFormat(false /* audio */) == NULL) {
1441 return;
1442 }
1443 }
1444
Wei Jia94211742014-10-28 17:09:06 -07001445 mOffloadAudio = false;
1446 mAudioEOS = false;
1447 mVideoEOS = false;
Wei Jia94211742014-10-28 17:09:06 -07001448 mStarted = true;
Wei Jiafe8fe7d2016-06-08 10:29:25 -07001449 mPaused = false;
Wei Jia94211742014-10-28 17:09:06 -07001450
Wei Jia94211742014-10-28 17:09:06 -07001451 uint32_t flags = 0;
1452
1453 if (mSource->isRealTime()) {
1454 flags |= Renderer::FLAG_REAL_TIME;
1455 }
1456
Wei Jia9737d342016-12-28 14:59:59 -08001457 bool hasAudio = (mSource->getFormat(true /* audio */) != NULL);
1458 bool hasVideo = (mSource->getFormat(false /* audio */) != NULL);
1459 if (!hasAudio && !hasVideo) {
Wei Jia1de83d52016-10-10 10:15:03 -07001460 ALOGE("no metadata for either audio or video source");
1461 mSource->stop();
1462 mSourceStarted = false;
1463 notifyListener(MEDIA_ERROR, MEDIA_ERROR_UNKNOWN, ERROR_MALFORMED);
1464 return;
1465 }
Wei Jia9737d342016-12-28 14:59:59 -08001466 ALOGV_IF(!hasAudio, "no metadata for audio source"); // video only stream
1467
1468 sp<MetaData> audioMeta = mSource->getFormatMeta(true /* audio */);
Wei Jia1de83d52016-10-10 10:15:03 -07001469
Wei Jia94211742014-10-28 17:09:06 -07001470 audio_stream_type_t streamType = AUDIO_STREAM_MUSIC;
1471 if (mAudioSink != NULL) {
1472 streamType = mAudioSink->getAudioStreamType();
1473 }
1474
Wei Jia94211742014-10-28 17:09:06 -07001475 mOffloadAudio =
Wei Jia9737d342016-12-28 14:59:59 -08001476 canOffloadStream(audioMeta, hasVideo, mSource->isStreaming(), streamType)
Wei Jiabf70feb2016-02-19 15:47:16 -08001477 && (mPlaybackSettings.mSpeed == 1.f && mPlaybackSettings.mPitch == 1.f);
Hassan Shojaniacefac142017-02-06 21:02:02 -08001478
1479 // Modular DRM: Disabling audio offload if the source is protected
1480 if (mOffloadAudio && mIsDrmProtected) {
1481 mOffloadAudio = false;
1482 ALOGV("onStart: Disabling mOffloadAudio now that the source is protected.");
1483 }
1484
Wei Jia94211742014-10-28 17:09:06 -07001485 if (mOffloadAudio) {
1486 flags |= Renderer::FLAG_OFFLOAD_AUDIO;
1487 }
1488
Lajos Molnar1d15ab52015-03-04 16:46:34 -08001489 sp<AMessage> notify = new AMessage(kWhatRendererNotify, this);
Wei Jia94211742014-10-28 17:09:06 -07001490 ++mRendererGeneration;
1491 notify->setInt32("generation", mRendererGeneration);
1492 mRenderer = new Renderer(mAudioSink, notify, flags);
Wei Jia94211742014-10-28 17:09:06 -07001493 mRendererLooper = new ALooper;
1494 mRendererLooper->setName("NuPlayerRenderer");
1495 mRendererLooper->start(false, false, ANDROID_PRIORITY_AUDIO);
1496 mRendererLooper->registerHandler(mRenderer);
Lajos Molnar3a474aa2015-04-24 17:10:07 -07001497
1498 status_t err = mRenderer->setPlaybackSettings(mPlaybackSettings);
1499 if (err != OK) {
1500 mSource->stop();
Robert Shih0c61a0d2015-07-06 15:09:10 -07001501 mSourceStarted = false;
Lajos Molnar3a474aa2015-04-24 17:10:07 -07001502 notifyListener(MEDIA_ERROR, MEDIA_ERROR_UNKNOWN, err);
1503 return;
Wei Jiac8206ff2015-03-04 13:59:37 -08001504 }
Wei Jia94211742014-10-28 17:09:06 -07001505
Ronghua Wuc8a70d32015-04-29 16:26:34 -07001506 float rate = getFrameRate();
1507 if (rate > 0) {
Wei Jia94211742014-10-28 17:09:06 -07001508 mRenderer->setVideoFrameRate(rate);
1509 }
1510
Wei Jiac6cfd702014-11-11 16:33:20 -08001511 if (mVideoDecoder != NULL) {
1512 mVideoDecoder->setRenderer(mRenderer);
1513 }
1514 if (mAudioDecoder != NULL) {
1515 mAudioDecoder->setRenderer(mRenderer);
1516 }
1517
Ray Essickd4d00612017-01-03 09:36:27 -08001518 mLastStartedPlayingTimeNs = systemTime();
1519
Wei Jia94211742014-10-28 17:09:06 -07001520 postScanSources();
1521}
1522
Chong Zhangefbb6192015-01-30 17:13:27 -08001523void NuPlayer::onPause() {
1524 if (mPaused) {
1525 return;
1526 }
1527 mPaused = true;
1528 if (mSource != NULL) {
1529 mSource->pause();
1530 } else {
1531 ALOGW("pause called when source is gone or not set");
1532 }
1533 if (mRenderer != NULL) {
1534 mRenderer->pause();
1535 } else {
1536 ALOGW("pause called when renderer is gone or not set");
1537 }
Ray Essickd4d00612017-01-03 09:36:27 -08001538
1539 sp<NuPlayerDriver> driver = mDriver.promote();
1540 if (driver != NULL) {
1541 int64_t now = systemTime();
1542 int64_t played = now - mLastStartedPlayingTimeNs;
1543 ALOGD("played from %" PRId64 " to %" PRId64 " = %" PRId64 ,
1544 mLastStartedPlayingTimeNs, now, played);
1545
1546 driver->notifyMorePlayingTimeUs((played+500)/1000);
1547 }
Chong Zhangefbb6192015-01-30 17:13:27 -08001548}
1549
Ronghua Wud7988b12014-10-03 15:19:10 -07001550bool NuPlayer::audioDecoderStillNeeded() {
1551 // Audio decoder is no longer needed if it's in shut/shutting down status.
1552 return ((mFlushingAudio != SHUT_DOWN) && (mFlushingAudio != SHUTTING_DOWN_DECODER));
1553}
1554
Andy Hung8d121d42014-10-03 09:53:53 -07001555void NuPlayer::handleFlushComplete(bool audio, bool isDecoder) {
1556 // We wait for both the decoder flush and the renderer flush to complete
1557 // before entering either the FLUSHED or the SHUTTING_DOWN_DECODER state.
1558
1559 mFlushComplete[audio][isDecoder] = true;
1560 if (!mFlushComplete[audio][!isDecoder]) {
1561 return;
1562 }
1563
1564 FlushStatus *state = audio ? &mFlushingAudio : &mFlushingVideo;
1565 switch (*state) {
1566 case FLUSHING_DECODER:
1567 {
1568 *state = FLUSHED;
Andy Hung8d121d42014-10-03 09:53:53 -07001569 break;
1570 }
1571
1572 case FLUSHING_DECODER_SHUTDOWN:
1573 {
1574 *state = SHUTTING_DOWN_DECODER;
1575
1576 ALOGV("initiating %s decoder shutdown", audio ? "audio" : "video");
Hassan Shojaniacefac142017-02-06 21:02:02 -08001577 // Both secure audio/video now. Legacy Widevine did it for secure video only.
1578 // Widevine source reads must stop before releasing the video decoder.
1579 if (mSource != NULL && mIsDrmProtected) {
1580 mSource->stop();
1581 mSourceStarted = false;
Andy Hung8d121d42014-10-03 09:53:53 -07001582 }
1583 getDecoder(audio)->initiateShutdown();
1584 break;
1585 }
1586
1587 default:
1588 // decoder flush completes only occur in a flushing state.
1589 LOG_ALWAYS_FATAL_IF(isDecoder, "decoder flush in invalid state %d", *state);
1590 break;
1591 }
1592}
1593
Andreas Huber3831a062010-12-21 10:22:33 -08001594void NuPlayer::finishFlushIfPossible() {
Wei Jia53904f32014-07-29 10:22:53 -07001595 if (mFlushingAudio != NONE && mFlushingAudio != FLUSHED
1596 && mFlushingAudio != SHUT_DOWN) {
Andreas Huber3831a062010-12-21 10:22:33 -08001597 return;
1598 }
1599
Wei Jia53904f32014-07-29 10:22:53 -07001600 if (mFlushingVideo != NONE && mFlushingVideo != FLUSHED
1601 && mFlushingVideo != SHUT_DOWN) {
Andreas Huber3831a062010-12-21 10:22:33 -08001602 return;
1603 }
1604
Steve Block3856b092011-10-20 11:56:00 +01001605 ALOGV("both audio and video are flushed now.");
Andreas Huber3831a062010-12-21 10:22:33 -08001606
Andreas Huber3831a062010-12-21 10:22:33 -08001607 mFlushingAudio = NONE;
1608 mFlushingVideo = NONE;
Andreas Huber3831a062010-12-21 10:22:33 -08001609
Andy Hung8d121d42014-10-03 09:53:53 -07001610 clearFlushComplete();
1611
Andreas Hubera1f8ab02012-11-30 10:53:22 -08001612 processDeferredActions();
Andreas Huber1aef2112011-01-04 14:01:29 -08001613}
1614
1615void NuPlayer::postScanSources() {
1616 if (mScanSourcesPending) {
1617 return;
1618 }
1619
Lajos Molnar1d15ab52015-03-04 16:46:34 -08001620 sp<AMessage> msg = new AMessage(kWhatScanSources, this);
Andreas Huber1aef2112011-01-04 14:01:29 -08001621 msg->setInt32("generation", mScanSourcesGeneration);
1622 msg->post();
1623
1624 mScanSourcesPending = true;
1625}
1626
Wei Jia41cd4632016-05-13 10:53:30 -07001627void NuPlayer::tryOpenAudioSinkForOffload(
1628 const sp<AMessage> &format, const sp<MetaData> &audioMeta, bool hasVideo) {
Andy Hung202bce12014-12-03 11:47:36 -08001629 // Note: This is called early in NuPlayer to determine whether offloading
1630 // is possible; otherwise the decoders call the renderer openAudioSink directly.
Andy Hung282a7e32014-08-14 15:56:34 -07001631
Andy Hung202bce12014-12-03 11:47:36 -08001632 status_t err = mRenderer->openAudioSink(
1633 format, true /* offloadOnly */, hasVideo, AUDIO_OUTPUT_FLAG_NONE, &mOffloadAudio);
1634 if (err != OK) {
1635 // Any failure we turn off mOffloadAudio.
1636 mOffloadAudio = false;
1637 } else if (mOffloadAudio) {
Chong Zhang3b9eb1f2014-10-15 17:05:08 -07001638 sendMetaDataToHal(mAudioSink, audioMeta);
Andy Hung282a7e32014-08-14 15:56:34 -07001639 }
1640}
1641
1642void NuPlayer::closeAudioSink() {
Chong Zhang3b9eb1f2014-10-15 17:05:08 -07001643 mRenderer->closeAudioSink();
Andy Hung282a7e32014-08-14 15:56:34 -07001644}
1645
Wei Jia5031b2f2016-02-25 11:19:31 -08001646void NuPlayer::restartAudio(
Wei Jiabf70feb2016-02-19 15:47:16 -08001647 int64_t currentPositionUs, bool forceNonOffload, bool needsToCreateAudioDecoder) {
Wei Jiaa05f1e32016-03-25 16:31:22 -07001648 if (mAudioDecoder != NULL) {
1649 mAudioDecoder->pause();
1650 mAudioDecoder.clear();
1651 ++mAudioDecoderGeneration;
1652 }
Wei Jiabf70feb2016-02-19 15:47:16 -08001653 if (mFlushingAudio == FLUSHING_DECODER) {
1654 mFlushComplete[1 /* audio */][1 /* isDecoder */] = true;
1655 mFlushingAudio = FLUSHED;
1656 finishFlushIfPossible();
1657 } else if (mFlushingAudio == FLUSHING_DECODER_SHUTDOWN
1658 || mFlushingAudio == SHUTTING_DOWN_DECODER) {
1659 mFlushComplete[1 /* audio */][1 /* isDecoder */] = true;
1660 mFlushingAudio = SHUT_DOWN;
1661 finishFlushIfPossible();
1662 needsToCreateAudioDecoder = false;
1663 }
1664 if (mRenderer == NULL) {
1665 return;
1666 }
1667 closeAudioSink();
1668 mRenderer->flush(true /* audio */, false /* notifyComplete */);
1669 if (mVideoDecoder != NULL) {
1670 mRenderer->flush(false /* audio */, false /* notifyComplete */);
1671 }
1672
Wei Jiac5de0912016-11-18 10:22:14 -08001673 performSeek(currentPositionUs, MediaPlayerSeekMode::SEEK_PREVIOUS_SYNC /* mode */);
Wei Jiabf70feb2016-02-19 15:47:16 -08001674
1675 if (forceNonOffload) {
1676 mRenderer->signalDisableOffloadAudio();
1677 mOffloadAudio = false;
1678 }
1679 if (needsToCreateAudioDecoder) {
Wei Jiaa05f1e32016-03-25 16:31:22 -07001680 instantiateDecoder(true /* audio */, &mAudioDecoder, !forceNonOffload);
Wei Jiabf70feb2016-02-19 15:47:16 -08001681 }
1682}
1683
Wei Jia41cd4632016-05-13 10:53:30 -07001684void NuPlayer::determineAudioModeChange(const sp<AMessage> &audioFormat) {
Wei Jiae4d18c72015-07-13 17:58:11 -07001685 if (mSource == NULL || mAudioSink == NULL) {
1686 return;
1687 }
1688
1689 if (mRenderer == NULL) {
1690 ALOGW("No renderer can be used to determine audio mode. Use non-offload for safety.");
1691 mOffloadAudio = false;
1692 return;
1693 }
1694
1695 sp<MetaData> audioMeta = mSource->getFormatMeta(true /* audio */);
1696 sp<AMessage> videoFormat = mSource->getFormat(false /* audio */);
1697 audio_stream_type_t streamType = mAudioSink->getAudioStreamType();
1698 const bool hasVideo = (videoFormat != NULL);
Hassan Shojaniacefac142017-02-06 21:02:02 -08001699 bool canOffload = canOffloadStream(
Wei Jiabf70feb2016-02-19 15:47:16 -08001700 audioMeta, hasVideo, mSource->isStreaming(), streamType)
1701 && (mPlaybackSettings.mSpeed == 1.f && mPlaybackSettings.mPitch == 1.f);
Hassan Shojaniacefac142017-02-06 21:02:02 -08001702
1703 // Modular DRM: Disabling audio offload if the source is protected
1704 if (canOffload && mIsDrmProtected) {
1705 canOffload = false;
1706 ALOGV("determineAudioModeChange: Disabling mOffloadAudio b/c the source is protected.");
1707 }
1708
Wei Jiae4d18c72015-07-13 17:58:11 -07001709 if (canOffload) {
1710 if (!mOffloadAudio) {
1711 mRenderer->signalEnableOffloadAudio();
1712 }
1713 // open audio sink early under offload mode.
Wei Jia41cd4632016-05-13 10:53:30 -07001714 tryOpenAudioSinkForOffload(audioFormat, audioMeta, hasVideo);
Wei Jiae4d18c72015-07-13 17:58:11 -07001715 } else {
Robert Shihe1d70192015-07-23 17:54:13 -07001716 if (mOffloadAudio) {
1717 mRenderer->signalDisableOffloadAudio();
1718 mOffloadAudio = false;
1719 }
Wei Jiae4d18c72015-07-13 17:58:11 -07001720 }
1721}
1722
Wei Jiaa05f1e32016-03-25 16:31:22 -07001723status_t NuPlayer::instantiateDecoder(
1724 bool audio, sp<DecoderBase> *decoder, bool checkAudioModeChange) {
Wei Jia566da802015-08-27 13:59:30 -07001725 // The audio decoder could be cleared by tear down. If still in shut down
1726 // process, no need to create a new audio decoder.
1727 if (*decoder != NULL || (audio && mFlushingAudio == SHUT_DOWN)) {
Andreas Huberf9334412010-12-15 15:17:42 -08001728 return OK;
1729 }
1730
Andreas Huber84066782011-08-16 09:34:26 -07001731 sp<AMessage> format = mSource->getFormat(audio);
Andreas Huberf9334412010-12-15 15:17:42 -08001732
Andreas Huber84066782011-08-16 09:34:26 -07001733 if (format == NULL) {
Robert Shih7350b052015-10-01 15:50:14 -07001734 return UNKNOWN_ERROR;
1735 } else {
1736 status_t err;
1737 if (format->findInt32("err", &err) && err) {
1738 return err;
1739 }
Andreas Huberf9334412010-12-15 15:17:42 -08001740 }
1741
Ronghua Wu8db88132015-04-22 13:51:35 -07001742 format->setInt32("priority", 0 /* realtime */);
1743
Andreas Huber3fe62152011-09-16 15:09:22 -07001744 if (!audio) {
Andreas Huber84066782011-08-16 09:34:26 -07001745 AString mime;
1746 CHECK(format->findString("mime", &mime));
Chong Zhanga7fa1d92014-06-11 14:49:23 -07001747
Lajos Molnar1d15ab52015-03-04 16:46:34 -08001748 sp<AMessage> ccNotify = new AMessage(kWhatClosedCaptionNotify, this);
Chong Zhang341ab6e2015-02-04 13:37:18 -08001749 if (mCCDecoder == NULL) {
1750 mCCDecoder = new CCDecoder(ccNotify);
1751 }
Lajos Molnar09524832014-07-17 14:29:51 -07001752
1753 if (mSourceFlags & Source::FLAG_SECURE) {
1754 format->setInt32("secure", true);
1755 }
Chong Zhang17134602015-01-07 16:14:34 -08001756
1757 if (mSourceFlags & Source::FLAG_PROTECTED) {
1758 format->setInt32("protected", true);
1759 }
Ronghua Wu8db88132015-04-22 13:51:35 -07001760
Ronghua Wuc8a70d32015-04-29 16:26:34 -07001761 float rate = getFrameRate();
1762 if (rate > 0) {
Lajos Molnar3a474aa2015-04-24 17:10:07 -07001763 format->setFloat("operating-rate", rate * mPlaybackSettings.mSpeed);
Ronghua Wu8db88132015-04-22 13:51:35 -07001764 }
Andreas Huber3fe62152011-09-16 15:09:22 -07001765 }
1766
Wei Jiabc2fb722014-07-08 16:37:57 -07001767 if (audio) {
Lajos Molnar1d15ab52015-03-04 16:46:34 -08001768 sp<AMessage> notify = new AMessage(kWhatAudioNotify, this);
Wei Jia88703c32014-08-06 11:24:07 -07001769 ++mAudioDecoderGeneration;
1770 notify->setInt32("generation", mAudioDecoderGeneration);
1771
Wei Jiaa05f1e32016-03-25 16:31:22 -07001772 if (checkAudioModeChange) {
Wei Jia41cd4632016-05-13 10:53:30 -07001773 determineAudioModeChange(format);
Wei Jiaa05f1e32016-03-25 16:31:22 -07001774 }
Wei Jiabc2fb722014-07-08 16:37:57 -07001775 if (mOffloadAudio) {
Wei Jia14532f22015-12-29 11:28:15 -08001776 mSource->setOffloadAudio(true /* offload */);
1777
Haynes Mathew George8b635332015-03-30 17:59:47 -07001778 const bool hasVideo = (mSource->getFormat(false /*audio */) != NULL);
1779 format->setInt32("has-video", hasVideo);
Wei Jiac6cfd702014-11-11 16:33:20 -08001780 *decoder = new DecoderPassThrough(notify, mSource, mRenderer);
Hassan Shojaniacefac142017-02-06 21:02:02 -08001781 ALOGV("instantiateDecoder audio DecoderPassThrough hasVideo: %d", hasVideo);
Wei Jiabc2fb722014-07-08 16:37:57 -07001782 } else {
Wei Jia14532f22015-12-29 11:28:15 -08001783 mSource->setOffloadAudio(false /* offload */);
1784
Wei Jiaf2ae3e12016-10-27 17:10:59 -07001785 *decoder = new Decoder(notify, mSource, mPID, mUID, mRenderer);
Hassan Shojaniacefac142017-02-06 21:02:02 -08001786 ALOGV("instantiateDecoder audio Decoder");
Wei Jiabc2fb722014-07-08 16:37:57 -07001787 }
1788 } else {
Lajos Molnar1d15ab52015-03-04 16:46:34 -08001789 sp<AMessage> notify = new AMessage(kWhatVideoNotify, this);
Wei Jia88703c32014-08-06 11:24:07 -07001790 ++mVideoDecoderGeneration;
1791 notify->setInt32("generation", mVideoDecoderGeneration);
1792
Chong Zhang7137ec72014-11-12 16:41:05 -08001793 *decoder = new Decoder(
Wei Jiaf2ae3e12016-10-27 17:10:59 -07001794 notify, mSource, mPID, mUID, mRenderer, mSurface, mCCDecoder);
Lajos Molnard9fd6312014-11-06 11:00:00 -08001795
1796 // enable FRC if high-quality AV sync is requested, even if not
Lajos Molnar1de1e252015-04-30 18:18:34 -07001797 // directly queuing to display, as this will even improve textureview
Lajos Molnard9fd6312014-11-06 11:00:00 -08001798 // playback.
1799 {
Marco Nelissen96626b72016-12-01 13:58:59 -08001800 if (property_get_bool("persist.sys.media.avsync", false)) {
Lajos Molnard9fd6312014-11-06 11:00:00 -08001801 format->setInt32("auto-frc", 1);
1802 }
1803 }
Wei Jiabc2fb722014-07-08 16:37:57 -07001804 }
Lajos Molnar1cd13982014-01-17 15:12:51 -08001805 (*decoder)->init();
Hassan Shojaniacefac142017-02-06 21:02:02 -08001806
1807 // Modular DRM
1808 if (mIsDrmProtected) {
1809 format->setPointer("crypto", mCrypto.get());
1810 ALOGV("instantiateDecoder: mCrypto: %p (%d) isSecure: %d", mCrypto.get(),
1811 (mCrypto != NULL ? mCrypto->getStrongCount() : 0),
1812 (mSourceFlags & Source::FLAG_SECURE) != 0);
1813 }
1814
Andreas Huber84066782011-08-16 09:34:26 -07001815 (*decoder)->configure(format);
Andreas Huberf9334412010-12-15 15:17:42 -08001816
Praveen Chavanbbaa1442016-04-08 13:33:49 -07001817 if (!audio) {
1818 sp<AMessage> params = new AMessage();
1819 float rate = getFrameRate();
1820 if (rate > 0) {
1821 params->setFloat("frame-rate-total", rate);
1822 }
1823
1824 sp<MetaData> fileMeta = getFileMeta();
1825 if (fileMeta != NULL) {
1826 int32_t videoTemporalLayerCount;
1827 if (fileMeta->findInt32(kKeyTemporalLayerCount, &videoTemporalLayerCount)
1828 && videoTemporalLayerCount > 0) {
1829 params->setInt32("temporal-layer-count", videoTemporalLayerCount);
1830 }
1831 }
1832
1833 if (params->countEntries() > 0) {
1834 (*decoder)->setParameters(params);
1835 }
1836 }
Andreas Huberf9334412010-12-15 15:17:42 -08001837 return OK;
1838}
1839
Chong Zhangced1c2f2014-08-08 15:22:35 -07001840void NuPlayer::updateVideoSize(
1841 const sp<AMessage> &inputFormat,
1842 const sp<AMessage> &outputFormat) {
1843 if (inputFormat == NULL) {
1844 ALOGW("Unknown video size, reporting 0x0!");
1845 notifyListener(MEDIA_SET_VIDEO_SIZE, 0, 0);
1846 return;
1847 }
Wei Jia9737d342016-12-28 14:59:59 -08001848 int32_t err = OK;
1849 inputFormat->findInt32("err", &err);
1850 if (err == -EWOULDBLOCK) {
1851 ALOGW("Video meta is not available yet!");
1852 return;
1853 }
1854 if (err != OK) {
1855 ALOGW("Something is wrong with video meta!");
1856 return;
1857 }
Chong Zhangced1c2f2014-08-08 15:22:35 -07001858
1859 int32_t displayWidth, displayHeight;
Chong Zhangced1c2f2014-08-08 15:22:35 -07001860 if (outputFormat != NULL) {
1861 int32_t width, height;
1862 CHECK(outputFormat->findInt32("width", &width));
1863 CHECK(outputFormat->findInt32("height", &height));
1864
1865 int32_t cropLeft, cropTop, cropRight, cropBottom;
1866 CHECK(outputFormat->findRect(
1867 "crop",
1868 &cropLeft, &cropTop, &cropRight, &cropBottom));
1869
1870 displayWidth = cropRight - cropLeft + 1;
1871 displayHeight = cropBottom - cropTop + 1;
1872
1873 ALOGV("Video output format changed to %d x %d "
1874 "(crop: %d x %d @ (%d, %d))",
1875 width, height,
1876 displayWidth,
1877 displayHeight,
1878 cropLeft, cropTop);
1879 } else {
1880 CHECK(inputFormat->findInt32("width", &displayWidth));
1881 CHECK(inputFormat->findInt32("height", &displayHeight));
1882
1883 ALOGV("Video input format %d x %d", displayWidth, displayHeight);
1884 }
1885
1886 // Take into account sample aspect ratio if necessary:
1887 int32_t sarWidth, sarHeight;
1888 if (inputFormat->findInt32("sar-width", &sarWidth)
Wei Jia095bc252017-01-27 10:16:16 -08001889 && inputFormat->findInt32("sar-height", &sarHeight)
1890 && sarWidth > 0 && sarHeight > 0) {
Chong Zhangced1c2f2014-08-08 15:22:35 -07001891 ALOGV("Sample aspect ratio %d : %d", sarWidth, sarHeight);
1892
1893 displayWidth = (displayWidth * sarWidth) / sarHeight;
1894
1895 ALOGV("display dimensions %d x %d", displayWidth, displayHeight);
Wei Jiadf6c6af2016-09-29 11:27:15 -07001896 } else {
1897 int32_t width, height;
1898 if (inputFormat->findInt32("display-width", &width)
1899 && inputFormat->findInt32("display-height", &height)
1900 && width > 0 && height > 0
1901 && displayWidth > 0 && displayHeight > 0) {
1902 if (displayHeight * (int64_t)width / height > (int64_t)displayWidth) {
1903 displayHeight = (int32_t)(displayWidth * (int64_t)height / width);
1904 } else {
1905 displayWidth = (int32_t)(displayHeight * (int64_t)width / height);
1906 }
1907 ALOGV("Video display width and height are overridden to %d x %d",
1908 displayWidth, displayHeight);
1909 }
Chong Zhangced1c2f2014-08-08 15:22:35 -07001910 }
1911
1912 int32_t rotationDegrees;
1913 if (!inputFormat->findInt32("rotation-degrees", &rotationDegrees)) {
1914 rotationDegrees = 0;
1915 }
1916
1917 if (rotationDegrees == 90 || rotationDegrees == 270) {
1918 int32_t tmp = displayWidth;
1919 displayWidth = displayHeight;
1920 displayHeight = tmp;
1921 }
1922
1923 notifyListener(
1924 MEDIA_SET_VIDEO_SIZE,
1925 displayWidth,
1926 displayHeight);
1927}
1928
Chong Zhangdcb89b32013-08-06 09:44:47 -07001929void NuPlayer::notifyListener(int msg, int ext1, int ext2, const Parcel *in) {
Andreas Huber43c3e6c2011-01-05 12:17:08 -08001930 if (mDriver == NULL) {
Andreas Huberf9334412010-12-15 15:17:42 -08001931 return;
1932 }
1933
Andreas Huber43c3e6c2011-01-05 12:17:08 -08001934 sp<NuPlayerDriver> driver = mDriver.promote();
Andreas Huberf9334412010-12-15 15:17:42 -08001935
Andreas Huber43c3e6c2011-01-05 12:17:08 -08001936 if (driver == NULL) {
Andreas Huberf9334412010-12-15 15:17:42 -08001937 return;
1938 }
1939
Chong Zhangdcb89b32013-08-06 09:44:47 -07001940 driver->notifyListener(msg, ext1, ext2, in);
Andreas Huberf9334412010-12-15 15:17:42 -08001941}
1942
Chong Zhang7137ec72014-11-12 16:41:05 -08001943void NuPlayer::flushDecoder(bool audio, bool needShutdown) {
Andreas Huber14f76722013-01-15 09:04:18 -08001944 ALOGV("[%s] flushDecoder needShutdown=%d",
1945 audio ? "audio" : "video", needShutdown);
1946
Chong Zhang7137ec72014-11-12 16:41:05 -08001947 const sp<DecoderBase> &decoder = getDecoder(audio);
Lajos Molnar87603c02014-08-20 19:25:30 -07001948 if (decoder == NULL) {
Steve Blockdf64d152012-01-04 20:05:49 +00001949 ALOGI("flushDecoder %s without decoder present",
Andreas Huber6e3d3112011-11-28 12:36:11 -08001950 audio ? "audio" : "video");
Lajos Molnar87603c02014-08-20 19:25:30 -07001951 return;
Andreas Huber6e3d3112011-11-28 12:36:11 -08001952 }
1953
Andreas Huber1aef2112011-01-04 14:01:29 -08001954 // Make sure we don't continue to scan sources until we finish flushing.
1955 ++mScanSourcesGeneration;
Chong Zhang3b032b32015-04-17 15:49:06 -07001956 if (mScanSourcesPending) {
Toshikazu Saitocbcbb792015-09-15 14:53:01 +09001957 if (!needShutdown) {
1958 mDeferredActions.push_back(
1959 new SimpleAction(&NuPlayer::performScanSources));
1960 }
Chong Zhang3b032b32015-04-17 15:49:06 -07001961 mScanSourcesPending = false;
1962 }
Andreas Huber1aef2112011-01-04 14:01:29 -08001963
Chong Zhang7137ec72014-11-12 16:41:05 -08001964 decoder->signalFlush();
Andreas Huber1aef2112011-01-04 14:01:29 -08001965
1966 FlushStatus newStatus =
1967 needShutdown ? FLUSHING_DECODER_SHUTDOWN : FLUSHING_DECODER;
1968
Lajos Molnarfcd3e942015-03-31 10:06:48 -07001969 mFlushComplete[audio][false /* isDecoder */] = (mRenderer == NULL);
Andy Hung8d121d42014-10-03 09:53:53 -07001970 mFlushComplete[audio][true /* isDecoder */] = false;
Andreas Huber1aef2112011-01-04 14:01:29 -08001971 if (audio) {
Wei Jia53904f32014-07-29 10:22:53 -07001972 ALOGE_IF(mFlushingAudio != NONE,
1973 "audio flushDecoder() is called in state %d", mFlushingAudio);
Andreas Huber1aef2112011-01-04 14:01:29 -08001974 mFlushingAudio = newStatus;
Andreas Huber1aef2112011-01-04 14:01:29 -08001975 } else {
Wei Jia53904f32014-07-29 10:22:53 -07001976 ALOGE_IF(mFlushingVideo != NONE,
1977 "video flushDecoder() is called in state %d", mFlushingVideo);
Andreas Huber1aef2112011-01-04 14:01:29 -08001978 mFlushingVideo = newStatus;
Andreas Huber1aef2112011-01-04 14:01:29 -08001979 }
1980}
1981
Chong Zhangced1c2f2014-08-08 15:22:35 -07001982void NuPlayer::queueDecoderShutdown(
1983 bool audio, bool video, const sp<AMessage> &reply) {
1984 ALOGI("queueDecoderShutdown audio=%d, video=%d", audio, video);
Andreas Huber84066782011-08-16 09:34:26 -07001985
Chong Zhangced1c2f2014-08-08 15:22:35 -07001986 mDeferredActions.push_back(
Wei Jiafef808d2014-10-31 17:57:05 -07001987 new FlushDecoderAction(
1988 audio ? FLUSH_CMD_SHUTDOWN : FLUSH_CMD_NONE,
1989 video ? FLUSH_CMD_SHUTDOWN : FLUSH_CMD_NONE));
Andreas Huber84066782011-08-16 09:34:26 -07001990
Chong Zhangced1c2f2014-08-08 15:22:35 -07001991 mDeferredActions.push_back(
1992 new SimpleAction(&NuPlayer::performScanSources));
Andreas Huber84066782011-08-16 09:34:26 -07001993
Chong Zhangced1c2f2014-08-08 15:22:35 -07001994 mDeferredActions.push_back(new PostMessageAction(reply));
1995
1996 processDeferredActions();
Andreas Huber84066782011-08-16 09:34:26 -07001997}
1998
James Dong0d268a32012-08-31 12:18:27 -07001999status_t NuPlayer::setVideoScalingMode(int32_t mode) {
2000 mVideoScalingMode = mode;
Lajos Molnar1de1e252015-04-30 18:18:34 -07002001 if (mSurface != NULL) {
2002 status_t ret = native_window_set_scaling_mode(mSurface.get(), mVideoScalingMode);
James Dong0d268a32012-08-31 12:18:27 -07002003 if (ret != OK) {
2004 ALOGE("Failed to set scaling mode (%d): %s",
2005 -ret, strerror(-ret));
2006 return ret;
2007 }
2008 }
2009 return OK;
2010}
2011
Chong Zhangdcb89b32013-08-06 09:44:47 -07002012status_t NuPlayer::getTrackInfo(Parcel* reply) const {
Lajos Molnar1d15ab52015-03-04 16:46:34 -08002013 sp<AMessage> msg = new AMessage(kWhatGetTrackInfo, this);
Chong Zhangdcb89b32013-08-06 09:44:47 -07002014 msg->setPointer("reply", reply);
2015
2016 sp<AMessage> response;
2017 status_t err = msg->postAndAwaitResponse(&response);
2018 return err;
2019}
2020
Robert Shih7c4f0d72014-07-09 18:53:31 -07002021status_t NuPlayer::getSelectedTrack(int32_t type, Parcel* reply) const {
Lajos Molnar1d15ab52015-03-04 16:46:34 -08002022 sp<AMessage> msg = new AMessage(kWhatGetSelectedTrack, this);
Robert Shih7c4f0d72014-07-09 18:53:31 -07002023 msg->setPointer("reply", reply);
2024 msg->setInt32("type", type);
2025
2026 sp<AMessage> response;
2027 status_t err = msg->postAndAwaitResponse(&response);
2028 if (err == OK && response != NULL) {
2029 CHECK(response->findInt32("err", &err));
2030 }
2031 return err;
2032}
2033
Robert Shih6ffb1fd2014-10-29 16:24:32 -07002034status_t NuPlayer::selectTrack(size_t trackIndex, bool select, int64_t timeUs) {
Lajos Molnar1d15ab52015-03-04 16:46:34 -08002035 sp<AMessage> msg = new AMessage(kWhatSelectTrack, this);
Chong Zhangdcb89b32013-08-06 09:44:47 -07002036 msg->setSize("trackIndex", trackIndex);
2037 msg->setInt32("select", select);
Robert Shih6ffb1fd2014-10-29 16:24:32 -07002038 msg->setInt64("timeUs", timeUs);
Chong Zhangdcb89b32013-08-06 09:44:47 -07002039
2040 sp<AMessage> response;
2041 status_t err = msg->postAndAwaitResponse(&response);
2042
Chong Zhang404fced2014-06-11 14:45:31 -07002043 if (err != OK) {
2044 return err;
2045 }
2046
2047 if (!response->findInt32("err", &err)) {
2048 err = OK;
2049 }
2050
Chong Zhangdcb89b32013-08-06 09:44:47 -07002051 return err;
2052}
2053
Ronghua Wua73d9e02014-10-08 15:13:29 -07002054status_t NuPlayer::getCurrentPosition(int64_t *mediaUs) {
2055 sp<Renderer> renderer = mRenderer;
2056 if (renderer == NULL) {
2057 return NO_INIT;
2058 }
2059
2060 return renderer->getCurrentPosition(mediaUs);
2061}
2062
Praveen Chavane1e5d7a2015-05-19 19:09:48 -07002063void NuPlayer::getStats(Vector<sp<AMessage> > *mTrackStats) {
2064 CHECK(mTrackStats != NULL);
2065
2066 mTrackStats->clear();
2067 if (mVideoDecoder != NULL) {
2068 mTrackStats->push_back(mVideoDecoder->getStats());
2069 }
2070 if (mAudioDecoder != NULL) {
2071 mTrackStats->push_back(mAudioDecoder->getStats());
Chong Zhang7137ec72014-11-12 16:41:05 -08002072 }
Ronghua Wua73d9e02014-10-08 15:13:29 -07002073}
2074
Marco Nelissenf0b72b52014-09-16 15:43:44 -07002075sp<MetaData> NuPlayer::getFileMeta() {
2076 return mSource->getFileFormatMeta();
2077}
2078
Ronghua Wuc8a70d32015-04-29 16:26:34 -07002079float NuPlayer::getFrameRate() {
2080 sp<MetaData> meta = mSource->getFormatMeta(false /* audio */);
2081 if (meta == NULL) {
2082 return 0;
2083 }
2084 int32_t rate;
2085 if (!meta->findInt32(kKeyFrameRate, &rate)) {
2086 // fall back to try file meta
2087 sp<MetaData> fileMeta = getFileMeta();
2088 if (fileMeta == NULL) {
2089 ALOGW("source has video meta but not file meta");
2090 return -1;
2091 }
2092 int32_t fileMetaRate;
2093 if (!fileMeta->findInt32(kKeyFrameRate, &fileMetaRate)) {
2094 return -1;
2095 }
2096 return fileMetaRate;
2097 }
2098 return rate;
2099}
2100
Andreas Huberb7c8e912012-11-27 15:02:53 -08002101void NuPlayer::schedulePollDuration() {
Lajos Molnar1d15ab52015-03-04 16:46:34 -08002102 sp<AMessage> msg = new AMessage(kWhatPollDuration, this);
Andreas Huberb7c8e912012-11-27 15:02:53 -08002103 msg->setInt32("generation", mPollDurationGeneration);
2104 msg->post();
2105}
2106
2107void NuPlayer::cancelPollDuration() {
2108 ++mPollDurationGeneration;
2109}
2110
Andreas Hubera1f8ab02012-11-30 10:53:22 -08002111void NuPlayer::processDeferredActions() {
2112 while (!mDeferredActions.empty()) {
2113 // We won't execute any deferred actions until we're no longer in
2114 // an intermediate state, i.e. one more more decoders are currently
2115 // flushing or shutting down.
2116
Andreas Hubera1f8ab02012-11-30 10:53:22 -08002117 if (mFlushingAudio != NONE || mFlushingVideo != NONE) {
2118 // We're currently flushing, postpone the reset until that's
2119 // completed.
2120
2121 ALOGV("postponing action mFlushingAudio=%d, mFlushingVideo=%d",
2122 mFlushingAudio, mFlushingVideo);
2123
2124 break;
2125 }
2126
2127 sp<Action> action = *mDeferredActions.begin();
2128 mDeferredActions.erase(mDeferredActions.begin());
2129
2130 action->execute(this);
2131 }
2132}
2133
Wei Jiac5de0912016-11-18 10:22:14 -08002134void NuPlayer::performSeek(int64_t seekTimeUs, MediaPlayerSeekMode mode) {
2135 ALOGV("performSeek seekTimeUs=%lld us (%.2f secs), mode=%d",
2136 (long long)seekTimeUs, seekTimeUs / 1E6, mode);
Andreas Hubera1f8ab02012-11-30 10:53:22 -08002137
Andy Hungadf34bf2014-09-03 18:22:22 -07002138 if (mSource == NULL) {
2139 // This happens when reset occurs right before the loop mode
2140 // asynchronously seeks to the start of the stream.
2141 LOG_ALWAYS_FATAL_IF(mAudioDecoder != NULL || mVideoDecoder != NULL,
2142 "mSource is NULL and decoders not NULL audio(%p) video(%p)",
2143 mAudioDecoder.get(), mVideoDecoder.get());
2144 return;
2145 }
Robert Shih1a5c8592015-08-04 18:07:44 -07002146 mPreviousSeekTimeUs = seekTimeUs;
Wei Jiac5de0912016-11-18 10:22:14 -08002147 mSource->seekTo(seekTimeUs, mode);
Robert Shihd3b0bbb2014-07-23 15:00:25 -07002148 ++mTimedTextGeneration;
Andreas Hubera1f8ab02012-11-30 10:53:22 -08002149
Andreas Hubera1f8ab02012-11-30 10:53:22 -08002150 // everything's flushed, continue playback.
2151}
2152
Wei Jiafef808d2014-10-31 17:57:05 -07002153void NuPlayer::performDecoderFlush(FlushCommand audio, FlushCommand video) {
2154 ALOGV("performDecoderFlush audio=%d, video=%d", audio, video);
Andreas Hubera1f8ab02012-11-30 10:53:22 -08002155
Wei Jiafef808d2014-10-31 17:57:05 -07002156 if ((audio == FLUSH_CMD_NONE || mAudioDecoder == NULL)
2157 && (video == FLUSH_CMD_NONE || mVideoDecoder == NULL)) {
Andreas Hubera1f8ab02012-11-30 10:53:22 -08002158 return;
2159 }
2160
Wei Jiafef808d2014-10-31 17:57:05 -07002161 if (audio != FLUSH_CMD_NONE && mAudioDecoder != NULL) {
2162 flushDecoder(true /* audio */, (audio == FLUSH_CMD_SHUTDOWN));
Andreas Hubera1f8ab02012-11-30 10:53:22 -08002163 }
2164
Wei Jiafef808d2014-10-31 17:57:05 -07002165 if (video != FLUSH_CMD_NONE && mVideoDecoder != NULL) {
2166 flushDecoder(false /* audio */, (video == FLUSH_CMD_SHUTDOWN));
Andreas Hubera1f8ab02012-11-30 10:53:22 -08002167 }
2168}
2169
2170void NuPlayer::performReset() {
2171 ALOGV("performReset");
2172
2173 CHECK(mAudioDecoder == NULL);
2174 CHECK(mVideoDecoder == NULL);
2175
2176 cancelPollDuration();
2177
2178 ++mScanSourcesGeneration;
2179 mScanSourcesPending = false;
2180
Lajos Molnar09524832014-07-17 14:29:51 -07002181 if (mRendererLooper != NULL) {
2182 if (mRenderer != NULL) {
2183 mRendererLooper->unregisterHandler(mRenderer->id());
2184 }
2185 mRendererLooper->stop();
2186 mRendererLooper.clear();
2187 }
Andreas Hubera1f8ab02012-11-30 10:53:22 -08002188 mRenderer.clear();
Wei Jia57568df2014-09-22 10:16:29 -07002189 ++mRendererGeneration;
Andreas Hubera1f8ab02012-11-30 10:53:22 -08002190
2191 if (mSource != NULL) {
2192 mSource->stop();
Andreas Huberb5f25f02013-02-05 10:14:26 -08002193
Wei Jiac45a4b22016-04-15 15:30:23 -07002194 Mutex::Autolock autoLock(mSourceLock);
Andreas Hubera1f8ab02012-11-30 10:53:22 -08002195 mSource.clear();
2196 }
2197
2198 if (mDriver != NULL) {
2199 sp<NuPlayerDriver> driver = mDriver.promote();
2200 if (driver != NULL) {
2201 driver->notifyResetComplete();
2202 }
2203 }
Andreas Huber57a339c2012-12-03 11:18:00 -08002204
2205 mStarted = false;
Wei Jia8a092d32016-06-03 14:57:24 -07002206 mPrepared = false;
Ronghua Wu64c2d172015-10-07 16:52:19 -07002207 mResetting = false;
Robert Shih0c61a0d2015-07-06 15:09:10 -07002208 mSourceStarted = false;
Hassan Shojaniacefac142017-02-06 21:02:02 -08002209
2210 // Modular DRM
2211 if (mCrypto != NULL) {
2212 // decoders will be flushed before this so their mCrypto would go away on their own
2213 // TODO change to ALOGV
2214 ALOGD("performReset mCrypto: %p (%d)", mCrypto.get(),
2215 (mCrypto != NULL ? mCrypto->getStrongCount() : 0));
2216 mCrypto.clear();
2217 }
2218 mIsDrmProtected = false;
Andreas Hubera1f8ab02012-11-30 10:53:22 -08002219}
2220
2221void NuPlayer::performScanSources() {
2222 ALOGV("performScanSources");
2223
Andreas Huber57a339c2012-12-03 11:18:00 -08002224 if (!mStarted) {
2225 return;
2226 }
2227
Andreas Hubera1f8ab02012-11-30 10:53:22 -08002228 if (mAudioDecoder == NULL || mVideoDecoder == NULL) {
2229 postScanSources();
2230 }
2231}
2232
Lajos Molnar1de1e252015-04-30 18:18:34 -07002233void NuPlayer::performSetSurface(const sp<Surface> &surface) {
Andreas Huber57a339c2012-12-03 11:18:00 -08002234 ALOGV("performSetSurface");
2235
Lajos Molnar1de1e252015-04-30 18:18:34 -07002236 mSurface = surface;
Andreas Huber57a339c2012-12-03 11:18:00 -08002237
2238 // XXX - ignore error from setVideoScalingMode for now
2239 setVideoScalingMode(mVideoScalingMode);
Chong Zhang13d6faa2014-08-22 15:35:28 -07002240
2241 if (mDriver != NULL) {
2242 sp<NuPlayerDriver> driver = mDriver.promote();
2243 if (driver != NULL) {
2244 driver->notifySetSurfaceComplete();
2245 }
2246 }
Andreas Huber57a339c2012-12-03 11:18:00 -08002247}
2248
Chong Zhangf8d71772014-11-26 15:08:34 -08002249void NuPlayer::performResumeDecoders(bool needNotify) {
2250 if (needNotify) {
2251 mResumePending = true;
2252 if (mVideoDecoder == NULL) {
2253 // if audio-only, we can notify seek complete now,
2254 // as the resume operation will be relatively fast.
2255 finishResume();
2256 }
2257 }
2258
Chong Zhang7137ec72014-11-12 16:41:05 -08002259 if (mVideoDecoder != NULL) {
Chong Zhangf8d71772014-11-26 15:08:34 -08002260 // When there is continuous seek, MediaPlayer will cache the seek
2261 // position, and send down new seek request when previous seek is
2262 // complete. Let's wait for at least one video output frame before
2263 // notifying seek complete, so that the video thumbnail gets updated
2264 // when seekbar is dragged.
2265 mVideoDecoder->signalResume(needNotify);
Chong Zhang7137ec72014-11-12 16:41:05 -08002266 }
2267
2268 if (mAudioDecoder != NULL) {
Chong Zhangf8d71772014-11-26 15:08:34 -08002269 mAudioDecoder->signalResume(false /* needNotify */);
2270 }
2271}
2272
2273void NuPlayer::finishResume() {
2274 if (mResumePending) {
2275 mResumePending = false;
Wei Jia1061c9c2015-05-19 16:02:17 -07002276 notifyDriverSeekComplete();
2277 }
2278}
2279
2280void NuPlayer::notifyDriverSeekComplete() {
2281 if (mDriver != NULL) {
2282 sp<NuPlayerDriver> driver = mDriver.promote();
2283 if (driver != NULL) {
2284 driver->notifySeekComplete();
Chong Zhangf8d71772014-11-26 15:08:34 -08002285 }
Chong Zhang7137ec72014-11-12 16:41:05 -08002286 }
2287}
2288
Andreas Huber9575c962013-02-05 13:59:56 -08002289void NuPlayer::onSourceNotify(const sp<AMessage> &msg) {
2290 int32_t what;
2291 CHECK(msg->findInt32("what", &what));
2292
2293 switch (what) {
Lajos Molnarfcd3e942015-03-31 10:06:48 -07002294 case Source::kWhatInstantiateSecureDecoders:
2295 {
2296 if (mSource == NULL) {
2297 // This is a stale notification from a source that was
2298 // asynchronously preparing when the client called reset().
2299 // We handled the reset, the source is gone.
2300 break;
2301 }
2302
2303 sp<AMessage> reply;
2304 CHECK(msg->findMessage("reply", &reply));
2305 status_t err = onInstantiateSecureDecoders();
2306 reply->setInt32("err", err);
2307 reply->post();
2308 break;
2309 }
2310
Andreas Huber9575c962013-02-05 13:59:56 -08002311 case Source::kWhatPrepared:
2312 {
Hassan Shojaniacefac142017-02-06 21:02:02 -08002313 ALOGV("NuPlayer::onSourceNotify Source::kWhatPrepared source: %p", mSource.get());
Andreas Huberb5f28d42013-04-25 15:11:19 -07002314 if (mSource == NULL) {
2315 // This is a stale notification from a source that was
2316 // asynchronously preparing when the client called reset().
2317 // We handled the reset, the source is gone.
2318 break;
2319 }
2320
Andreas Huberec0c5972013-02-05 14:47:13 -08002321 int32_t err;
2322 CHECK(msg->findInt32("err", &err));
2323
Lajos Molnarfcd3e942015-03-31 10:06:48 -07002324 if (err != OK) {
2325 // shut down potential secure codecs in case client never calls reset
2326 mDeferredActions.push_back(
2327 new FlushDecoderAction(FLUSH_CMD_SHUTDOWN /* audio */,
2328 FLUSH_CMD_SHUTDOWN /* video */));
2329 processDeferredActions();
Wei Jia8a092d32016-06-03 14:57:24 -07002330 } else {
2331 mPrepared = true;
Lajos Molnarfcd3e942015-03-31 10:06:48 -07002332 }
2333
Andreas Huber9575c962013-02-05 13:59:56 -08002334 sp<NuPlayerDriver> driver = mDriver.promote();
2335 if (driver != NULL) {
Marco Nelissendd114d12014-05-28 15:23:14 -07002336 // notify duration first, so that it's definitely set when
2337 // the app received the "prepare complete" callback.
2338 int64_t durationUs;
2339 if (mSource->getDuration(&durationUs) == OK) {
2340 driver->notifyDuration(durationUs);
2341 }
Andreas Huberec0c5972013-02-05 14:47:13 -08002342 driver->notifyPrepareCompleted(err);
Andreas Huber9575c962013-02-05 13:59:56 -08002343 }
Andreas Huber99759402013-04-01 14:28:31 -07002344
Andreas Huber9575c962013-02-05 13:59:56 -08002345 break;
2346 }
2347
Hassan Shojaniacefac142017-02-06 21:02:02 -08002348 // Modular DRM
2349 case Source::kWhatDrmInfo:
2350 {
2351 Parcel parcel;
2352 sp<ABuffer> drmInfo;
2353 CHECK(msg->findBuffer("drmInfo", &drmInfo));
2354 parcel.setData(drmInfo->data(), drmInfo->size());
2355
2356 ALOGV("onSourceNotify() kWhatDrmInfo MEDIA_DRM_INFO drmInfo: %p parcel size: %zu",
2357 drmInfo.get(), parcel.dataSize());
2358
2359 notifyListener(MEDIA_DRM_INFO, 0 /* ext1 */, 0 /* ext2 */, &parcel);
2360
2361 break;
2362 }
2363
Andreas Huber9575c962013-02-05 13:59:56 -08002364 case Source::kWhatFlagsChanged:
2365 {
2366 uint32_t flags;
2367 CHECK(msg->findInt32("flags", (int32_t *)&flags));
2368
Chong Zhang4b7069d2013-09-11 12:52:43 -07002369 sp<NuPlayerDriver> driver = mDriver.promote();
2370 if (driver != NULL) {
Hassan Shojaniacefac142017-02-06 21:02:02 -08002371
2372 ALOGV("onSourceNotify() kWhatFlagsChanged FLAG_CAN_PAUSE: %d "
2373 "FLAG_CAN_SEEK_BACKWARD: %d \n\t\t\t\t FLAG_CAN_SEEK_FORWARD: %d "
2374 "FLAG_CAN_SEEK: %d FLAG_DYNAMIC_DURATION: %d \n"
2375 "\t\t\t\t FLAG_SECURE: %d FLAG_PROTECTED: %d",
2376 (flags & Source::FLAG_CAN_PAUSE) != 0,
2377 (flags & Source::FLAG_CAN_SEEK_BACKWARD) != 0,
2378 (flags & Source::FLAG_CAN_SEEK_FORWARD) != 0,
2379 (flags & Source::FLAG_CAN_SEEK) != 0,
2380 (flags & Source::FLAG_DYNAMIC_DURATION) != 0,
2381 (flags & Source::FLAG_SECURE) != 0,
2382 (flags & Source::FLAG_PROTECTED) != 0);
2383
Wei Jia895651b2014-12-10 17:31:52 -08002384 if ((flags & NuPlayer::Source::FLAG_CAN_SEEK) == 0) {
2385 driver->notifyListener(
2386 MEDIA_INFO, MEDIA_INFO_NOT_SEEKABLE, 0);
2387 }
Chong Zhang4b7069d2013-09-11 12:52:43 -07002388 driver->notifyFlagsChanged(flags);
2389 }
2390
Andreas Huber9575c962013-02-05 13:59:56 -08002391 if ((mSourceFlags & Source::FLAG_DYNAMIC_DURATION)
2392 && (!(flags & Source::FLAG_DYNAMIC_DURATION))) {
2393 cancelPollDuration();
2394 } else if (!(mSourceFlags & Source::FLAG_DYNAMIC_DURATION)
2395 && (flags & Source::FLAG_DYNAMIC_DURATION)
2396 && (mAudioDecoder != NULL || mVideoDecoder != NULL)) {
2397 schedulePollDuration();
2398 }
2399
2400 mSourceFlags = flags;
2401 break;
2402 }
2403
2404 case Source::kWhatVideoSizeChanged:
2405 {
Chong Zhangced1c2f2014-08-08 15:22:35 -07002406 sp<AMessage> format;
2407 CHECK(msg->findMessage("format", &format));
Andreas Huber9575c962013-02-05 13:59:56 -08002408
Chong Zhangced1c2f2014-08-08 15:22:35 -07002409 updateVideoSize(format);
Andreas Huber9575c962013-02-05 13:59:56 -08002410 break;
2411 }
2412
Chong Zhang2a3cc9a2014-08-21 17:48:26 -07002413 case Source::kWhatBufferingUpdate:
2414 {
2415 int32_t percentage;
2416 CHECK(msg->findInt32("percentage", &percentage));
2417
2418 notifyListener(MEDIA_BUFFERING_UPDATE, percentage, 0);
2419 break;
2420 }
2421
Chong Zhangefbb6192015-01-30 17:13:27 -08002422 case Source::kWhatPauseOnBufferingStart:
2423 {
2424 // ignore if not playing
Chong Zhang8a048332015-05-06 15:16:28 -07002425 if (mStarted) {
Chong Zhangefbb6192015-01-30 17:13:27 -08002426 ALOGI("buffer low, pausing...");
2427
Chong Zhang8a048332015-05-06 15:16:28 -07002428 mPausedForBuffering = true;
Chong Zhangefbb6192015-01-30 17:13:27 -08002429 onPause();
2430 }
Wei Jiabfe82072016-05-20 09:21:50 -07002431 notifyListener(MEDIA_INFO, MEDIA_INFO_BUFFERING_START, 0);
Roger Jönssonb50e83e2013-01-21 16:26:41 +01002432 break;
2433 }
2434
Chong Zhangefbb6192015-01-30 17:13:27 -08002435 case Source::kWhatResumeOnBufferingEnd:
2436 {
2437 // ignore if not playing
Chong Zhang8a048332015-05-06 15:16:28 -07002438 if (mStarted) {
Chong Zhangefbb6192015-01-30 17:13:27 -08002439 ALOGI("buffer ready, resuming...");
2440
Chong Zhang8a048332015-05-06 15:16:28 -07002441 mPausedForBuffering = false;
2442
2443 // do not resume yet if client didn't unpause
2444 if (!mPausedByClient) {
2445 onResume();
2446 }
Chong Zhangefbb6192015-01-30 17:13:27 -08002447 }
Wei Jia3bed45a2016-02-17 11:06:47 -08002448 notifyListener(MEDIA_INFO, MEDIA_INFO_BUFFERING_END, 0);
Roger Jönssonb50e83e2013-01-21 16:26:41 +01002449 break;
2450 }
2451
Chong Zhangefbb6192015-01-30 17:13:27 -08002452 case Source::kWhatCacheStats:
2453 {
2454 int32_t kbps;
2455 CHECK(msg->findInt32("bandwidth", &kbps));
2456
2457 notifyListener(MEDIA_INFO, MEDIA_INFO_NETWORK_BANDWIDTH, kbps);
2458 break;
2459 }
2460
Chong Zhangdcb89b32013-08-06 09:44:47 -07002461 case Source::kWhatSubtitleData:
2462 {
2463 sp<ABuffer> buffer;
2464 CHECK(msg->findBuffer("buffer", &buffer));
2465
Chong Zhang404fced2014-06-11 14:45:31 -07002466 sendSubtitleData(buffer, 0 /* baseIndex */);
Chong Zhangdcb89b32013-08-06 09:44:47 -07002467 break;
2468 }
2469
Robert Shih08528432015-04-08 09:06:54 -07002470 case Source::kWhatTimedMetaData:
2471 {
2472 sp<ABuffer> buffer;
2473 if (!msg->findBuffer("buffer", &buffer)) {
2474 notifyListener(MEDIA_INFO, MEDIA_INFO_METADATA_UPDATE, 0);
2475 } else {
2476 sendTimedMetaData(buffer);
2477 }
2478 break;
2479 }
2480
Robert Shihd3b0bbb2014-07-23 15:00:25 -07002481 case Source::kWhatTimedTextData:
2482 {
2483 int32_t generation;
2484 if (msg->findInt32("generation", &generation)
2485 && generation != mTimedTextGeneration) {
2486 break;
2487 }
2488
2489 sp<ABuffer> buffer;
2490 CHECK(msg->findBuffer("buffer", &buffer));
2491
2492 sp<NuPlayerDriver> driver = mDriver.promote();
2493 if (driver == NULL) {
2494 break;
2495 }
2496
2497 int posMs;
2498 int64_t timeUs, posUs;
2499 driver->getCurrentPosition(&posMs);
Patrik2 Carlsson24d484b2015-01-27 16:49:45 +01002500 posUs = (int64_t) posMs * 1000ll;
Robert Shihd3b0bbb2014-07-23 15:00:25 -07002501 CHECK(buffer->meta()->findInt64("timeUs", &timeUs));
2502
2503 if (posUs < timeUs) {
2504 if (!msg->findInt32("generation", &generation)) {
2505 msg->setInt32("generation", mTimedTextGeneration);
2506 }
2507 msg->post(timeUs - posUs);
2508 } else {
2509 sendTimedTextData(buffer);
2510 }
2511 break;
2512 }
2513
Andreas Huber14f76722013-01-15 09:04:18 -08002514 case Source::kWhatQueueDecoderShutdown:
2515 {
2516 int32_t audio, video;
2517 CHECK(msg->findInt32("audio", &audio));
2518 CHECK(msg->findInt32("video", &video));
2519
2520 sp<AMessage> reply;
2521 CHECK(msg->findMessage("reply", &reply));
2522
2523 queueDecoderShutdown(audio, video, reply);
2524 break;
2525 }
2526
Ronghua Wu80276872014-08-28 15:50:29 -07002527 case Source::kWhatDrmNoLicense:
2528 {
2529 notifyListener(MEDIA_ERROR, MEDIA_ERROR_UNKNOWN, ERROR_DRM_NO_LICENSE);
2530 break;
2531 }
2532
Andreas Huber9575c962013-02-05 13:59:56 -08002533 default:
2534 TRESPASS();
2535 }
2536}
2537
Chong Zhanga7fa1d92014-06-11 14:49:23 -07002538void NuPlayer::onClosedCaptionNotify(const sp<AMessage> &msg) {
2539 int32_t what;
2540 CHECK(msg->findInt32("what", &what));
2541
2542 switch (what) {
2543 case NuPlayer::CCDecoder::kWhatClosedCaptionData:
2544 {
2545 sp<ABuffer> buffer;
2546 CHECK(msg->findBuffer("buffer", &buffer));
2547
2548 size_t inbandTracks = 0;
2549 if (mSource != NULL) {
2550 inbandTracks = mSource->getTrackCount();
2551 }
2552
2553 sendSubtitleData(buffer, inbandTracks);
2554 break;
2555 }
2556
2557 case NuPlayer::CCDecoder::kWhatTrackAdded:
2558 {
2559 notifyListener(MEDIA_INFO, MEDIA_INFO_METADATA_UPDATE, 0);
2560
2561 break;
2562 }
2563
2564 default:
2565 TRESPASS();
2566 }
2567
2568
2569}
2570
Chong Zhang404fced2014-06-11 14:45:31 -07002571void NuPlayer::sendSubtitleData(const sp<ABuffer> &buffer, int32_t baseIndex) {
2572 int32_t trackIndex;
2573 int64_t timeUs, durationUs;
2574 CHECK(buffer->meta()->findInt32("trackIndex", &trackIndex));
2575 CHECK(buffer->meta()->findInt64("timeUs", &timeUs));
2576 CHECK(buffer->meta()->findInt64("durationUs", &durationUs));
2577
2578 Parcel in;
2579 in.writeInt32(trackIndex + baseIndex);
2580 in.writeInt64(timeUs);
2581 in.writeInt64(durationUs);
2582 in.writeInt32(buffer->size());
2583 in.writeInt32(buffer->size());
2584 in.write(buffer->data(), buffer->size());
2585
2586 notifyListener(MEDIA_SUBTITLE_DATA, 0, 0, &in);
2587}
Robert Shihd3b0bbb2014-07-23 15:00:25 -07002588
Robert Shih08528432015-04-08 09:06:54 -07002589void NuPlayer::sendTimedMetaData(const sp<ABuffer> &buffer) {
2590 int64_t timeUs;
2591 CHECK(buffer->meta()->findInt64("timeUs", &timeUs));
2592
2593 Parcel in;
2594 in.writeInt64(timeUs);
2595 in.writeInt32(buffer->size());
2596 in.writeInt32(buffer->size());
2597 in.write(buffer->data(), buffer->size());
2598
2599 notifyListener(MEDIA_META_DATA, 0, 0, &in);
2600}
2601
Robert Shihd3b0bbb2014-07-23 15:00:25 -07002602void NuPlayer::sendTimedTextData(const sp<ABuffer> &buffer) {
2603 const void *data;
2604 size_t size = 0;
2605 int64_t timeUs;
Marco Nelissen55e2f4c2015-09-04 15:57:15 -07002606 int32_t flag = TextDescriptions::IN_BAND_TEXT_3GPP;
Robert Shihd3b0bbb2014-07-23 15:00:25 -07002607
2608 AString mime;
2609 CHECK(buffer->meta()->findString("mime", &mime));
2610 CHECK(strcasecmp(mime.c_str(), MEDIA_MIMETYPE_TEXT_3GPP) == 0);
2611
2612 data = buffer->data();
2613 size = buffer->size();
2614
2615 Parcel parcel;
2616 if (size > 0) {
2617 CHECK(buffer->meta()->findInt64("timeUs", &timeUs));
Marco Nelissen55e2f4c2015-09-04 15:57:15 -07002618 int32_t global = 0;
2619 if (buffer->meta()->findInt32("global", &global) && global) {
2620 flag |= TextDescriptions::GLOBAL_DESCRIPTIONS;
2621 } else {
2622 flag |= TextDescriptions::LOCAL_DESCRIPTIONS;
2623 }
Robert Shihd3b0bbb2014-07-23 15:00:25 -07002624 TextDescriptions::getParcelOfDescriptions(
2625 (const uint8_t *)data, size, flag, timeUs / 1000, &parcel);
2626 }
2627
2628 if ((parcel.dataSize() > 0)) {
2629 notifyListener(MEDIA_TIMED_TEXT, 0, 0, &parcel);
2630 } else { // send an empty timed text
2631 notifyListener(MEDIA_TIMED_TEXT, 0, 0);
2632 }
2633}
Hassan Shojaniacefac142017-02-06 21:02:02 -08002634
2635// Modular DRM begin
2636status_t NuPlayer::prepareDrm(const uint8_t uuid[16], const Vector<uint8_t> &drmSessionId)
2637{
2638 ALOGV("prepareDrm ");
2639
2640 // Passing to the looper anyway; called in a pre-config prepared state so no race on mCrypto
2641 sp<AMessage> msg = new AMessage(kWhatPrepareDrm, this);
2642 // synchronous call so just passing the address but with local copies of "const" args
2643 uint8_t UUID[16];
2644 memcpy(UUID, uuid, sizeof(UUID));
2645 Vector<uint8_t> sessionId = drmSessionId;
2646 msg->setPointer("uuid", (void*)UUID);
2647 msg->setPointer("drmSessionId", (void*)&sessionId);
2648
2649 sp<AMessage> response;
2650 status_t status = msg->postAndAwaitResponse(&response);
2651
2652 if (status == OK && response != NULL) {
2653 CHECK(response->findInt32("status", &status));
2654 ALOGV("prepareDrm ret: %d ", status);
2655 } else {
2656 ALOGE("prepareDrm err: %d", status);
2657 }
2658
2659 return status;
2660}
2661
2662status_t NuPlayer::releaseDrm()
2663{
2664 ALOGV("releaseDrm ");
2665
2666 sp<AMessage> msg = new AMessage(kWhatReleaseDrm, this);
2667
2668 sp<AMessage> response;
2669 status_t status = msg->postAndAwaitResponse(&response);
2670
2671 if (status == OK && response != NULL) {
2672 CHECK(response->findInt32("status", &status));
2673 ALOGV("releaseDrm ret: %d ", status);
2674 } else {
2675 ALOGE("releaseDrm err: %d", status);
2676 }
2677
2678 return status;
2679}
2680
2681status_t NuPlayer::onPrepareDrm(const sp<AMessage> &msg)
2682{
2683 // TODO change to ALOGV
2684 ALOGD("onPrepareDrm ");
2685
2686 status_t status = INVALID_OPERATION;
2687 if (mSource == NULL) {
2688 ALOGE("onPrepareDrm: No source. onPrepareDrm failed with %d.", status);
2689 return status;
2690 }
2691
2692 uint8_t *uuid;
2693 Vector<uint8_t> *drmSessionId;
2694 CHECK(msg->findPointer("uuid", (void**)&uuid));
2695 CHECK(msg->findPointer("drmSessionId", (void**)&drmSessionId));
2696
2697 status = OK;
2698 sp<ICrypto> crypto = NULL;
2699
2700 status = mSource->prepareDrm(uuid, *drmSessionId, &crypto);
2701 if (crypto == NULL) {
2702 ALOGE("onPrepareDrm: mSource->prepareDrm failed. status: %d", status);
2703 return status;
2704 }
2705 ALOGV("onPrepareDrm: mSource->prepareDrm succeeded");
2706
2707 if (mCrypto != NULL) {
2708 ALOGE("onPrepareDrm: Unexpected. Already having mCrypto: %p (%d)",
2709 mCrypto.get(), mCrypto->getStrongCount());
2710 mCrypto.clear();
2711 }
2712
2713 mCrypto = crypto;
2714 mIsDrmProtected = true;
2715 // TODO change to ALOGV
2716 ALOGD("onPrepareDrm: mCrypto: %p (%d)", mCrypto.get(),
2717 (mCrypto != NULL ? mCrypto->getStrongCount() : 0));
2718
2719 return status;
2720}
2721
2722status_t NuPlayer::onReleaseDrm()
2723{
2724 // TODO change to ALOGV
2725 ALOGD("onReleaseDrm ");
2726
2727 mIsDrmProtected = true;
2728
2729 status_t status;
2730 if (mCrypto != NULL) {
2731 status=OK;
2732 // first making sure the codecs have released their crypto reference
2733 const sp<DecoderBase> &videoDecoder = getDecoder(false/*audio*/);
2734 if (videoDecoder != NULL) {
2735 status = videoDecoder->releaseCrypto();
2736 ALOGV("onReleaseDrm: video decoder ret: %d", status);
2737 }
2738
2739 const sp<DecoderBase> &audioDecoder = getDecoder(true/*audio*/);
2740 if (audioDecoder != NULL) {
2741 status_t status_audio = audioDecoder->releaseCrypto();
2742 if (status == OK) { // otherwise, returning the first error
2743 status = status_audio;
2744 }
2745 ALOGV("onReleaseDrm: audio decoder ret: %d", status_audio);
2746 }
2747
2748 // TODO change to ALOGV
2749 ALOGD("onReleaseDrm: mCrypto: %p (%d)", mCrypto.get(),
2750 (mCrypto != NULL ? mCrypto->getStrongCount() : 0));
2751 mCrypto.clear();
2752 } else { // mCrypto == NULL
2753 ALOGE("onReleaseDrm: Unexpected. There is no crypto.");
2754 status = INVALID_OPERATION;
2755 }
2756
2757 return status;
2758}
2759// Modular DRM end
Andreas Huberb5f25f02013-02-05 10:14:26 -08002760////////////////////////////////////////////////////////////////////////////////
2761
Chong Zhangced1c2f2014-08-08 15:22:35 -07002762sp<AMessage> NuPlayer::Source::getFormat(bool audio) {
2763 sp<MetaData> meta = getFormatMeta(audio);
2764
2765 if (meta == NULL) {
2766 return NULL;
2767 }
2768
2769 sp<AMessage> msg = new AMessage;
2770
2771 if(convertMetaDataToMessage(meta, &msg) == OK) {
2772 return msg;
2773 }
2774 return NULL;
2775}
2776
Andreas Huber9575c962013-02-05 13:59:56 -08002777void NuPlayer::Source::notifyFlagsChanged(uint32_t flags) {
2778 sp<AMessage> notify = dupNotify();
2779 notify->setInt32("what", kWhatFlagsChanged);
2780 notify->setInt32("flags", flags);
2781 notify->post();
2782}
2783
Chong Zhangced1c2f2014-08-08 15:22:35 -07002784void NuPlayer::Source::notifyVideoSizeChanged(const sp<AMessage> &format) {
Andreas Huber9575c962013-02-05 13:59:56 -08002785 sp<AMessage> notify = dupNotify();
2786 notify->setInt32("what", kWhatVideoSizeChanged);
Chong Zhangced1c2f2014-08-08 15:22:35 -07002787 notify->setMessage("format", format);
Andreas Huber9575c962013-02-05 13:59:56 -08002788 notify->post();
2789}
2790
Andreas Huberec0c5972013-02-05 14:47:13 -08002791void NuPlayer::Source::notifyPrepared(status_t err) {
Hassan Shojaniacefac142017-02-06 21:02:02 -08002792 ALOGV("Source::notifyPrepared %d", err);
Andreas Huber9575c962013-02-05 13:59:56 -08002793 sp<AMessage> notify = dupNotify();
2794 notify->setInt32("what", kWhatPrepared);
Andreas Huberec0c5972013-02-05 14:47:13 -08002795 notify->setInt32("err", err);
Andreas Huber9575c962013-02-05 13:59:56 -08002796 notify->post();
2797}
2798
Hassan Shojaniacefac142017-02-06 21:02:02 -08002799void NuPlayer::Source::notifyDrmInfo(const sp<ABuffer> &drmInfoBuffer)
2800{
2801 ALOGV("Source::notifyDrmInfo");
2802
2803 sp<AMessage> notify = dupNotify();
2804 notify->setInt32("what", kWhatDrmInfo);
2805 notify->setBuffer("drmInfo", drmInfoBuffer);
2806
2807 notify->post();
2808}
2809
Lajos Molnarfcd3e942015-03-31 10:06:48 -07002810void NuPlayer::Source::notifyInstantiateSecureDecoders(const sp<AMessage> &reply) {
2811 sp<AMessage> notify = dupNotify();
2812 notify->setInt32("what", kWhatInstantiateSecureDecoders);
2813 notify->setMessage("reply", reply);
2814 notify->post();
2815}
2816
Andreas Huber84333e02014-02-07 15:36:10 -08002817void NuPlayer::Source::onMessageReceived(const sp<AMessage> & /* msg */) {
Andreas Huberb5f25f02013-02-05 10:14:26 -08002818 TRESPASS();
2819}
2820
Andreas Huberf9334412010-12-15 15:17:42 -08002821} // namespace android