blob: 316f8242cc8a459fd428e2b500ca2a215c1be055 [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),
Hassan Shojania50b20c92017-02-16 18:28:58 -0800202 mPausedForBuffering(false),
203 mIsDrmProtected(false) {
Andy Hung8d121d42014-10-03 09:53:53 -0700204 clearFlushComplete();
Andreas Huberf9334412010-12-15 15:17:42 -0800205}
206
207NuPlayer::~NuPlayer() {
208}
209
Andreas Huber9b80c2b2011-06-30 15:47:02 -0700210void NuPlayer::setUID(uid_t uid) {
211 mUIDValid = true;
212 mUID = uid;
213}
214
Andreas Huber43c3e6c2011-01-05 12:17:08 -0800215void NuPlayer::setDriver(const wp<NuPlayerDriver> &driver) {
216 mDriver = driver;
Andreas Huberf9334412010-12-15 15:17:42 -0800217}
218
Andreas Huber9575c962013-02-05 13:59:56 -0800219void NuPlayer::setDataSourceAsync(const sp<IStreamSource> &source) {
Lajos Molnar1d15ab52015-03-04 16:46:34 -0800220 sp<AMessage> msg = new AMessage(kWhatSetDataSource, this);
Andreas Huberf9334412010-12-15 15:17:42 -0800221
Lajos Molnar1d15ab52015-03-04 16:46:34 -0800222 sp<AMessage> notify = new AMessage(kWhatSourceNotify, this);
Andreas Huberb5f25f02013-02-05 10:14:26 -0800223
Andreas Huber240abcc2014-02-13 13:32:37 -0800224 msg->setObject("source", new StreamingSource(notify, source));
Andreas Huber5bc087c2010-12-23 10:27:40 -0800225 msg->post();
226}
Andreas Huberf9334412010-12-15 15:17:42 -0800227
Andreas Huberafed0e12011-09-20 15:39:58 -0700228static bool IsHTTPLiveURL(const char *url) {
229 if (!strncasecmp("http://", url, 7)
Andreas Huber99759402013-04-01 14:28:31 -0700230 || !strncasecmp("https://", url, 8)
231 || !strncasecmp("file://", url, 7)) {
Andreas Huberafed0e12011-09-20 15:39:58 -0700232 size_t len = strlen(url);
233 if (len >= 5 && !strcasecmp(".m3u8", &url[len - 5])) {
234 return true;
235 }
236
237 if (strstr(url,"m3u8")) {
238 return true;
239 }
240 }
241
242 return false;
243}
244
Andreas Huber9575c962013-02-05 13:59:56 -0800245void NuPlayer::setDataSourceAsync(
Andreas Huber1b86fe02014-01-29 11:13:26 -0800246 const sp<IMediaHTTPService> &httpService,
247 const char *url,
248 const KeyedVector<String8, String8> *headers) {
Chong Zhang3de157d2014-08-05 20:54:44 -0700249
Lajos Molnar1d15ab52015-03-04 16:46:34 -0800250 sp<AMessage> msg = new AMessage(kWhatSetDataSource, this);
Oscar Rydhé7a33b772012-02-20 10:15:48 +0100251 size_t len = strlen(url);
Andreas Huber5bc087c2010-12-23 10:27:40 -0800252
Lajos Molnar1d15ab52015-03-04 16:46:34 -0800253 sp<AMessage> notify = new AMessage(kWhatSourceNotify, this);
Andreas Huberb5f25f02013-02-05 10:14:26 -0800254
Andreas Huberafed0e12011-09-20 15:39:58 -0700255 sp<Source> source;
256 if (IsHTTPLiveURL(url)) {
Andreas Huber81e68442014-02-05 11:52:33 -0800257 source = new HTTPLiveSource(notify, httpService, url, headers);
Hassan Shojaniacefac142017-02-06 21:02:02 -0800258 ALOGV("setDataSourceAsync HTTPLiveSource %s", url);
Andreas Huberafed0e12011-09-20 15:39:58 -0700259 } else if (!strncasecmp(url, "rtsp://", 7)) {
Andreas Huber1b86fe02014-01-29 11:13:26 -0800260 source = new RTSPSource(
261 notify, httpService, url, headers, mUIDValid, mUID);
Hassan Shojaniacefac142017-02-06 21:02:02 -0800262 ALOGV("setDataSourceAsync RTSPSource %s", url);
Oscar Rydhé7a33b772012-02-20 10:15:48 +0100263 } else if ((!strncasecmp(url, "http://", 7)
264 || !strncasecmp(url, "https://", 8))
265 && ((len >= 4 && !strcasecmp(".sdp", &url[len - 4]))
266 || strstr(url, ".sdp?"))) {
Andreas Huber1b86fe02014-01-29 11:13:26 -0800267 source = new RTSPSource(
268 notify, httpService, url, headers, mUIDValid, mUID, true);
Hassan Shojaniacefac142017-02-06 21:02:02 -0800269 ALOGV("setDataSourceAsync RTSPSource http/https/.sdp %s", url);
Andreas Huber2bfdd422011-10-11 15:24:07 -0700270 } else {
Hassan Shojaniacefac142017-02-06 21:02:02 -0800271 ALOGV("setDataSourceAsync GenericSource %s", url);
272
Chong Zhang3de157d2014-08-05 20:54:44 -0700273 sp<GenericSource> genericSource =
274 new GenericSource(notify, mUIDValid, mUID);
Andreas Huber2bfdd422011-10-11 15:24:07 -0700275
Chong Zhanga19f33e2014-08-07 15:35:07 -0700276 status_t err = genericSource->setDataSource(httpService, url, headers);
Chong Zhang3de157d2014-08-05 20:54:44 -0700277
278 if (err == OK) {
279 source = genericSource;
280 } else {
Chong Zhanga19f33e2014-08-07 15:35:07 -0700281 ALOGE("Failed to set data source!");
Chong Zhang3de157d2014-08-05 20:54:44 -0700282 }
283 }
Andreas Huberafed0e12011-09-20 15:39:58 -0700284 msg->setObject("source", source);
285 msg->post();
286}
287
Andreas Huber9575c962013-02-05 13:59:56 -0800288void NuPlayer::setDataSourceAsync(int fd, int64_t offset, int64_t length) {
Lajos Molnar1d15ab52015-03-04 16:46:34 -0800289 sp<AMessage> msg = new AMessage(kWhatSetDataSource, this);
Andreas Huberafed0e12011-09-20 15:39:58 -0700290
Lajos Molnar1d15ab52015-03-04 16:46:34 -0800291 sp<AMessage> notify = new AMessage(kWhatSourceNotify, this);
Andreas Huberb5f25f02013-02-05 10:14:26 -0800292
Chong Zhang3de157d2014-08-05 20:54:44 -0700293 sp<GenericSource> source =
294 new GenericSource(notify, mUIDValid, mUID);
295
Hassan Shojaniacefac142017-02-06 21:02:02 -0800296 ALOGV("setDataSourceAsync fd %d/%lld/%lld source: %p",
297 fd, (long long)offset, (long long)length, source.get());
298
Chong Zhanga19f33e2014-08-07 15:35:07 -0700299 status_t err = source->setDataSource(fd, offset, length);
Chong Zhang3de157d2014-08-05 20:54:44 -0700300
301 if (err != OK) {
Chong Zhanga19f33e2014-08-07 15:35:07 -0700302 ALOGE("Failed to set data source!");
Chong Zhang3de157d2014-08-05 20:54:44 -0700303 source = NULL;
304 }
305
Andreas Huberafed0e12011-09-20 15:39:58 -0700306 msg->setObject("source", source);
Andreas Huberf9334412010-12-15 15:17:42 -0800307 msg->post();
308}
309
Chris Watkins99f31602015-03-20 13:06:33 -0700310void NuPlayer::setDataSourceAsync(const sp<DataSource> &dataSource) {
311 sp<AMessage> msg = new AMessage(kWhatSetDataSource, this);
312 sp<AMessage> notify = new AMessage(kWhatSourceNotify, this);
313
314 sp<GenericSource> source = new GenericSource(notify, mUIDValid, mUID);
315 status_t err = source->setDataSource(dataSource);
316
317 if (err != OK) {
318 ALOGE("Failed to set data source!");
319 source = NULL;
320 }
321
322 msg->setObject("source", source);
323 msg->post();
324}
325
Wei Jia48fa06d2016-12-20 15:30:49 -0800326status_t NuPlayer::getDefaultBufferingSettings(
327 BufferingSettings *buffering /* nonnull */) {
328 sp<AMessage> msg = new AMessage(kWhatGetDefaultBufferingSettings, this);
329 sp<AMessage> response;
330 status_t err = msg->postAndAwaitResponse(&response);
331 if (err == OK && response != NULL) {
332 CHECK(response->findInt32("err", &err));
333 if (err == OK) {
334 readFromAMessage(response, buffering);
335 }
336 }
337 return err;
338}
339
340status_t NuPlayer::setBufferingSettings(const BufferingSettings& buffering) {
341 sp<AMessage> msg = new AMessage(kWhatSetBufferingSettings, this);
342 writeToAMessage(msg, buffering);
343 sp<AMessage> response;
344 status_t err = msg->postAndAwaitResponse(&response);
345 if (err == OK && response != NULL) {
346 CHECK(response->findInt32("err", &err));
347 }
348 return err;
349}
350
Andreas Huber9575c962013-02-05 13:59:56 -0800351void NuPlayer::prepareAsync() {
Hassan Shojaniacefac142017-02-06 21:02:02 -0800352 ALOGV("prepareAsync");
353
Lajos Molnar1d15ab52015-03-04 16:46:34 -0800354 (new AMessage(kWhatPrepare, this))->post();
Andreas Huber9575c962013-02-05 13:59:56 -0800355}
356
Andreas Huber57a339c2012-12-03 11:18:00 -0800357void NuPlayer::setVideoSurfaceTextureAsync(
Andy McFadden8ba01022012-12-18 09:46:54 -0800358 const sp<IGraphicBufferProducer> &bufferProducer) {
Lajos Molnar1de1e252015-04-30 18:18:34 -0700359 sp<AMessage> msg = new AMessage(kWhatSetVideoSurface, this);
Andreas Huber57a339c2012-12-03 11:18:00 -0800360
Andy McFadden8ba01022012-12-18 09:46:54 -0800361 if (bufferProducer == NULL) {
Lajos Molnar1de1e252015-04-30 18:18:34 -0700362 msg->setObject("surface", NULL);
Andreas Huber57a339c2012-12-03 11:18:00 -0800363 } else {
Lajos Molnar1de1e252015-04-30 18:18:34 -0700364 msg->setObject("surface", new Surface(bufferProducer, true /* controlledByApp */));
Andreas Huber57a339c2012-12-03 11:18:00 -0800365 }
366
Andreas Huberf9334412010-12-15 15:17:42 -0800367 msg->post();
368}
369
370void NuPlayer::setAudioSink(const sp<MediaPlayerBase::AudioSink> &sink) {
Lajos Molnar1d15ab52015-03-04 16:46:34 -0800371 sp<AMessage> msg = new AMessage(kWhatSetAudioSink, this);
Andreas Huberf9334412010-12-15 15:17:42 -0800372 msg->setObject("sink", sink);
373 msg->post();
374}
375
376void NuPlayer::start() {
Lajos Molnar1d15ab52015-03-04 16:46:34 -0800377 (new AMessage(kWhatStart, this))->post();
Andreas Huberf9334412010-12-15 15:17:42 -0800378}
379
Lajos Molnar3a474aa2015-04-24 17:10:07 -0700380status_t NuPlayer::setPlaybackSettings(const AudioPlaybackRate &rate) {
381 // do some cursory validation of the settings here. audio modes are
382 // only validated when set on the audiosink.
383 if ((rate.mSpeed != 0.f && rate.mSpeed < AUDIO_TIMESTRETCH_SPEED_MIN)
384 || rate.mSpeed > AUDIO_TIMESTRETCH_SPEED_MAX
385 || rate.mPitch < AUDIO_TIMESTRETCH_SPEED_MIN
386 || rate.mPitch > AUDIO_TIMESTRETCH_SPEED_MAX) {
387 return BAD_VALUE;
388 }
389 sp<AMessage> msg = new AMessage(kWhatConfigPlayback, this);
390 writeToAMessage(msg, rate);
391 sp<AMessage> response;
392 status_t err = msg->postAndAwaitResponse(&response);
393 if (err == OK && response != NULL) {
394 CHECK(response->findInt32("err", &err));
395 }
396 return err;
397}
398
399status_t NuPlayer::getPlaybackSettings(AudioPlaybackRate *rate /* nonnull */) {
400 sp<AMessage> msg = new AMessage(kWhatGetPlaybackSettings, this);
401 sp<AMessage> response;
402 status_t err = msg->postAndAwaitResponse(&response);
403 if (err == OK && response != NULL) {
404 CHECK(response->findInt32("err", &err));
405 if (err == OK) {
406 readFromAMessage(response, rate);
407 }
408 }
409 return err;
410}
411
412status_t NuPlayer::setSyncSettings(const AVSyncSettings &sync, float videoFpsHint) {
413 sp<AMessage> msg = new AMessage(kWhatConfigSync, this);
414 writeToAMessage(msg, sync, videoFpsHint);
415 sp<AMessage> response;
416 status_t err = msg->postAndAwaitResponse(&response);
417 if (err == OK && response != NULL) {
418 CHECK(response->findInt32("err", &err));
419 }
420 return err;
421}
422
423status_t NuPlayer::getSyncSettings(
424 AVSyncSettings *sync /* nonnull */, float *videoFps /* nonnull */) {
425 sp<AMessage> msg = new AMessage(kWhatGetSyncSettings, this);
426 sp<AMessage> response;
427 status_t err = msg->postAndAwaitResponse(&response);
428 if (err == OK && response != NULL) {
429 CHECK(response->findInt32("err", &err));
430 if (err == OK) {
431 readFromAMessage(response, sync, videoFps);
432 }
433 }
434 return err;
Wei Jia98160162015-02-04 17:01:11 -0800435}
436
Andreas Huber43c3e6c2011-01-05 12:17:08 -0800437void NuPlayer::pause() {
Lajos Molnar1d15ab52015-03-04 16:46:34 -0800438 (new AMessage(kWhatPause, this))->post();
Andreas Huber43c3e6c2011-01-05 12:17:08 -0800439}
440
Andreas Huber1aef2112011-01-04 14:01:29 -0800441void NuPlayer::resetAsync() {
Wei Jiac45a4b22016-04-15 15:30:23 -0700442 sp<Source> source;
443 {
444 Mutex::Autolock autoLock(mSourceLock);
445 source = mSource;
446 }
447
448 if (source != NULL) {
Chong Zhang48296b72014-09-14 14:28:45 -0700449 // During a reset, the data source might be unresponsive already, we need to
450 // disconnect explicitly so that reads exit promptly.
451 // We can't queue the disconnect request to the looper, as it might be
452 // queued behind a stuck read and never gets processed.
453 // Doing a disconnect outside the looper to allows the pending reads to exit
454 // (either successfully or with error).
Wei Jiac45a4b22016-04-15 15:30:23 -0700455 source->disconnect();
Chong Zhang48296b72014-09-14 14:28:45 -0700456 }
457
Lajos Molnar1d15ab52015-03-04 16:46:34 -0800458 (new AMessage(kWhatReset, this))->post();
Andreas Huber1aef2112011-01-04 14:01:29 -0800459}
460
Wei Jiac5de0912016-11-18 10:22:14 -0800461void NuPlayer::seekToAsync(int64_t seekTimeUs, MediaPlayerSeekMode mode, bool needNotify) {
Lajos Molnar1d15ab52015-03-04 16:46:34 -0800462 sp<AMessage> msg = new AMessage(kWhatSeek, this);
Andreas Huber43c3e6c2011-01-05 12:17:08 -0800463 msg->setInt64("seekTimeUs", seekTimeUs);
Wei Jiac5de0912016-11-18 10:22:14 -0800464 msg->setInt32("mode", mode);
Wei Jiae427abf2014-09-22 15:21:11 -0700465 msg->setInt32("needNotify", needNotify);
Andreas Huber43c3e6c2011-01-05 12:17:08 -0800466 msg->post();
467}
468
Andreas Huber53df1a42010-12-22 10:03:04 -0800469
Chong Zhang404fced2014-06-11 14:45:31 -0700470void NuPlayer::writeTrackInfo(
Chih-Hung Hsiehe964d4e2016-08-09 14:31:32 -0700471 Parcel* reply, const sp<AMessage>& format) const {
Marco Nelissenc367ca12015-09-15 09:51:59 -0700472 if (format == NULL) {
473 ALOGE("NULL format");
474 return;
475 }
Chong Zhang404fced2014-06-11 14:45:31 -0700476 int32_t trackType;
Marco Nelissen9436e482015-09-15 10:21:53 -0700477 if (!format->findInt32("type", &trackType)) {
Marco Nelissenc367ca12015-09-15 09:51:59 -0700478 ALOGE("no track type");
479 return;
480 }
Chong Zhang404fced2014-06-11 14:45:31 -0700481
Robert Shih755106e2015-04-30 14:36:45 -0700482 AString mime;
Robert Shih2e3a4252015-05-06 10:21:15 -0700483 if (!format->findString("mime", &mime)) {
484 // Java MediaPlayer only uses mimetype for subtitle and timedtext tracks.
485 // If we can't find the mimetype here it means that we wouldn't be needing
486 // the mimetype on the Java end. We still write a placeholder mime to keep the
487 // (de)serialization logic simple.
488 if (trackType == MEDIA_TRACK_TYPE_AUDIO) {
489 mime = "audio/";
490 } else if (trackType == MEDIA_TRACK_TYPE_VIDEO) {
491 mime = "video/";
492 } else {
Marco Nelissenc367ca12015-09-15 09:51:59 -0700493 ALOGE("unknown track type: %d", trackType);
494 return;
Robert Shih2e3a4252015-05-06 10:21:15 -0700495 }
496 }
Robert Shih755106e2015-04-30 14:36:45 -0700497
Chong Zhang404fced2014-06-11 14:45:31 -0700498 AString lang;
Marco Nelissen9436e482015-09-15 10:21:53 -0700499 if (!format->findString("language", &lang)) {
Marco Nelissenc367ca12015-09-15 09:51:59 -0700500 ALOGE("no language");
501 return;
502 }
Chong Zhang404fced2014-06-11 14:45:31 -0700503
504 reply->writeInt32(2); // write something non-zero
505 reply->writeInt32(trackType);
Robert Shih755106e2015-04-30 14:36:45 -0700506 reply->writeString16(String16(mime.c_str()));
Chong Zhang404fced2014-06-11 14:45:31 -0700507 reply->writeString16(String16(lang.c_str()));
508
509 if (trackType == MEDIA_TRACK_TYPE_SUBTITLE) {
Chong Zhang404fced2014-06-11 14:45:31 -0700510 int32_t isAuto, isDefault, isForced;
511 CHECK(format->findInt32("auto", &isAuto));
512 CHECK(format->findInt32("default", &isDefault));
513 CHECK(format->findInt32("forced", &isForced));
514
Chong Zhang404fced2014-06-11 14:45:31 -0700515 reply->writeInt32(isAuto);
516 reply->writeInt32(isDefault);
517 reply->writeInt32(isForced);
518 }
519}
520
Andreas Huberf9334412010-12-15 15:17:42 -0800521void NuPlayer::onMessageReceived(const sp<AMessage> &msg) {
522 switch (msg->what()) {
523 case kWhatSetDataSource:
524 {
Steve Block3856b092011-10-20 11:56:00 +0100525 ALOGV("kWhatSetDataSource");
Andreas Huberf9334412010-12-15 15:17:42 -0800526
527 CHECK(mSource == NULL);
528
Chong Zhang3de157d2014-08-05 20:54:44 -0700529 status_t err = OK;
Andreas Huber5bc087c2010-12-23 10:27:40 -0800530 sp<RefBase> obj;
531 CHECK(msg->findObject("source", &obj));
Chong Zhang3de157d2014-08-05 20:54:44 -0700532 if (obj != NULL) {
Wei Jiac45a4b22016-04-15 15:30:23 -0700533 Mutex::Autolock autoLock(mSourceLock);
Chong Zhang3de157d2014-08-05 20:54:44 -0700534 mSource = static_cast<Source *>(obj.get());
Chong Zhang3de157d2014-08-05 20:54:44 -0700535 } else {
536 err = UNKNOWN_ERROR;
537 }
Andreas Huber9575c962013-02-05 13:59:56 -0800538
539 CHECK(mDriver != NULL);
540 sp<NuPlayerDriver> driver = mDriver.promote();
541 if (driver != NULL) {
Chong Zhang3de157d2014-08-05 20:54:44 -0700542 driver->notifySetDataSourceCompleted(err);
Andreas Huber9575c962013-02-05 13:59:56 -0800543 }
544 break;
545 }
546
Wei Jia48fa06d2016-12-20 15:30:49 -0800547 case kWhatGetDefaultBufferingSettings:
548 {
549 sp<AReplyToken> replyID;
550 CHECK(msg->senderAwaitsResponse(&replyID));
551
552 ALOGV("kWhatGetDefaultBufferingSettings");
553 BufferingSettings buffering;
554 status_t err = OK;
555 if (mSource != NULL) {
556 err = mSource->getDefaultBufferingSettings(&buffering);
557 } else {
558 err = INVALID_OPERATION;
559 }
560 sp<AMessage> response = new AMessage;
561 if (err == OK) {
562 writeToAMessage(response, buffering);
563 }
564 response->setInt32("err", err);
565 response->postReply(replyID);
566 break;
567 }
568
569 case kWhatSetBufferingSettings:
570 {
571 sp<AReplyToken> replyID;
572 CHECK(msg->senderAwaitsResponse(&replyID));
573
574 ALOGV("kWhatSetBufferingSettings");
575 BufferingSettings buffering;
576 readFromAMessage(msg, &buffering);
577 status_t err = OK;
578 if (mSource != NULL) {
579 err = mSource->setBufferingSettings(buffering);
580 } else {
581 err = INVALID_OPERATION;
582 }
583 sp<AMessage> response = new AMessage;
584 response->setInt32("err", err);
585 response->postReply(replyID);
586 break;
587 }
588
Andreas Huber9575c962013-02-05 13:59:56 -0800589 case kWhatPrepare:
590 {
Hassan Shojaniacefac142017-02-06 21:02:02 -0800591 ALOGV("onMessageReceived kWhatPrepare");
592
Andreas Huber9575c962013-02-05 13:59:56 -0800593 mSource->prepareAsync();
Andreas Huberf9334412010-12-15 15:17:42 -0800594 break;
595 }
596
Chong Zhangdcb89b32013-08-06 09:44:47 -0700597 case kWhatGetTrackInfo:
598 {
Lajos Molnar3f274362015-03-05 14:35:41 -0800599 sp<AReplyToken> replyID;
Chong Zhangdcb89b32013-08-06 09:44:47 -0700600 CHECK(msg->senderAwaitsResponse(&replyID));
601
Chong Zhang404fced2014-06-11 14:45:31 -0700602 Parcel* reply;
603 CHECK(msg->findPointer("reply", (void**)&reply));
604
605 size_t inbandTracks = 0;
Chong Zhangdcb89b32013-08-06 09:44:47 -0700606 if (mSource != NULL) {
Chong Zhang404fced2014-06-11 14:45:31 -0700607 inbandTracks = mSource->getTrackCount();
608 }
609
Chong Zhanga7fa1d92014-06-11 14:49:23 -0700610 size_t ccTracks = 0;
611 if (mCCDecoder != NULL) {
612 ccTracks = mCCDecoder->getTrackCount();
613 }
614
Chong Zhang404fced2014-06-11 14:45:31 -0700615 // total track count
Chong Zhanga7fa1d92014-06-11 14:49:23 -0700616 reply->writeInt32(inbandTracks + ccTracks);
Chong Zhang404fced2014-06-11 14:45:31 -0700617
618 // write inband tracks
619 for (size_t i = 0; i < inbandTracks; ++i) {
620 writeTrackInfo(reply, mSource->getTrackInfo(i));
Chong Zhangdcb89b32013-08-06 09:44:47 -0700621 }
622
Chong Zhanga7fa1d92014-06-11 14:49:23 -0700623 // write CC track
624 for (size_t i = 0; i < ccTracks; ++i) {
625 writeTrackInfo(reply, mCCDecoder->getTrackInfo(i));
626 }
627
Chong Zhangdcb89b32013-08-06 09:44:47 -0700628 sp<AMessage> response = new AMessage;
Chong Zhangdcb89b32013-08-06 09:44:47 -0700629 response->postReply(replyID);
630 break;
631 }
632
Robert Shih7c4f0d72014-07-09 18:53:31 -0700633 case kWhatGetSelectedTrack:
634 {
635 status_t err = INVALID_OPERATION;
636 if (mSource != NULL) {
637 err = OK;
638
639 int32_t type32;
640 CHECK(msg->findInt32("type", (int32_t*)&type32));
641 media_track_type type = (media_track_type)type32;
642 ssize_t selectedTrack = mSource->getSelectedTrack(type);
643
644 Parcel* reply;
645 CHECK(msg->findPointer("reply", (void**)&reply));
646 reply->writeInt32(selectedTrack);
647 }
648
649 sp<AMessage> response = new AMessage;
650 response->setInt32("err", err);
651
Lajos Molnar3f274362015-03-05 14:35:41 -0800652 sp<AReplyToken> replyID;
Robert Shih7c4f0d72014-07-09 18:53:31 -0700653 CHECK(msg->senderAwaitsResponse(&replyID));
654 response->postReply(replyID);
655 break;
656 }
657
Chong Zhangdcb89b32013-08-06 09:44:47 -0700658 case kWhatSelectTrack:
659 {
Lajos Molnar3f274362015-03-05 14:35:41 -0800660 sp<AReplyToken> replyID;
Chong Zhangdcb89b32013-08-06 09:44:47 -0700661 CHECK(msg->senderAwaitsResponse(&replyID));
662
Chong Zhang404fced2014-06-11 14:45:31 -0700663 size_t trackIndex;
664 int32_t select;
Robert Shih6ffb1fd2014-10-29 16:24:32 -0700665 int64_t timeUs;
Chong Zhang404fced2014-06-11 14:45:31 -0700666 CHECK(msg->findSize("trackIndex", &trackIndex));
667 CHECK(msg->findInt32("select", &select));
Robert Shih6ffb1fd2014-10-29 16:24:32 -0700668 CHECK(msg->findInt64("timeUs", &timeUs));
Chong Zhang404fced2014-06-11 14:45:31 -0700669
Chong Zhangdcb89b32013-08-06 09:44:47 -0700670 status_t err = INVALID_OPERATION;
Chong Zhang404fced2014-06-11 14:45:31 -0700671
672 size_t inbandTracks = 0;
Chong Zhangdcb89b32013-08-06 09:44:47 -0700673 if (mSource != NULL) {
Chong Zhang404fced2014-06-11 14:45:31 -0700674 inbandTracks = mSource->getTrackCount();
675 }
Chong Zhanga7fa1d92014-06-11 14:49:23 -0700676 size_t ccTracks = 0;
677 if (mCCDecoder != NULL) {
678 ccTracks = mCCDecoder->getTrackCount();
679 }
Chong Zhang404fced2014-06-11 14:45:31 -0700680
681 if (trackIndex < inbandTracks) {
Robert Shih6ffb1fd2014-10-29 16:24:32 -0700682 err = mSource->selectTrack(trackIndex, select, timeUs);
Robert Shihd3b0bbb2014-07-23 15:00:25 -0700683
684 if (!select && err == OK) {
685 int32_t type;
686 sp<AMessage> info = mSource->getTrackInfo(trackIndex);
687 if (info != NULL
688 && info->findInt32("type", &type)
689 && type == MEDIA_TRACK_TYPE_TIMEDTEXT) {
690 ++mTimedTextGeneration;
691 }
692 }
Chong Zhanga7fa1d92014-06-11 14:49:23 -0700693 } else {
694 trackIndex -= inbandTracks;
695
696 if (trackIndex < ccTracks) {
697 err = mCCDecoder->selectTrack(trackIndex, select);
698 }
Chong Zhangdcb89b32013-08-06 09:44:47 -0700699 }
700
701 sp<AMessage> response = new AMessage;
702 response->setInt32("err", err);
703
704 response->postReply(replyID);
705 break;
706 }
707
Andreas Huberb7c8e912012-11-27 15:02:53 -0800708 case kWhatPollDuration:
709 {
710 int32_t generation;
711 CHECK(msg->findInt32("generation", &generation));
712
713 if (generation != mPollDurationGeneration) {
714 // stale
715 break;
716 }
717
718 int64_t durationUs;
719 if (mDriver != NULL && mSource->getDuration(&durationUs) == OK) {
720 sp<NuPlayerDriver> driver = mDriver.promote();
721 if (driver != NULL) {
722 driver->notifyDuration(durationUs);
723 }
724 }
725
726 msg->post(1000000ll); // poll again in a second.
727 break;
728 }
729
Lajos Molnar1de1e252015-04-30 18:18:34 -0700730 case kWhatSetVideoSurface:
Andreas Huberf9334412010-12-15 15:17:42 -0800731 {
Andreas Huberf9334412010-12-15 15:17:42 -0800732
733 sp<RefBase> obj;
Lajos Molnar1de1e252015-04-30 18:18:34 -0700734 CHECK(msg->findObject("surface", &obj));
735 sp<Surface> surface = static_cast<Surface *>(obj.get());
Lajos Molnara81c6222015-07-10 19:17:45 -0700736
737 ALOGD("onSetVideoSurface(%p, %s video decoder)",
738 surface.get(),
Wei Jia6b7d2ed2015-08-10 15:11:54 -0700739 (mSource != NULL && mStarted && mSource->getFormat(false /* audio */) != NULL
Lajos Molnara81c6222015-07-10 19:17:45 -0700740 && mVideoDecoder != NULL) ? "have" : "no");
741
Wei Jia6b7d2ed2015-08-10 15:11:54 -0700742 // Need to check mStarted before calling mSource->getFormat because NuPlayer might
743 // be in preparing state and it could take long time.
744 // When mStarted is true, mSource must have been set.
745 if (mSource == NULL || !mStarted || mSource->getFormat(false /* audio */) == NULL
Lajos Molnara81c6222015-07-10 19:17:45 -0700746 // NOTE: mVideoDecoder's mSurface is always non-null
747 || (mVideoDecoder != NULL && mVideoDecoder->setVideoSurface(surface) == OK)) {
Lajos Molnar1de1e252015-04-30 18:18:34 -0700748 performSetSurface(surface);
Wei Jiafef808d2014-10-31 17:57:05 -0700749 break;
750 }
751
752 mDeferredActions.push_back(
Krystian Turczyn687a5892016-01-20 14:20:35 +0100753 new FlushDecoderAction(
754 (obj != NULL ? FLUSH_CMD_FLUSH : FLUSH_CMD_NONE) /* audio */,
Wei Jiafef808d2014-10-31 17:57:05 -0700755 FLUSH_CMD_SHUTDOWN /* video */));
756
Lajos Molnar1de1e252015-04-30 18:18:34 -0700757 mDeferredActions.push_back(new SetSurfaceAction(surface));
James Dong0d268a32012-08-31 12:18:27 -0700758
Krystian Turczyn687a5892016-01-20 14:20:35 +0100759 if (obj != NULL) {
Wei Jiafef808d2014-10-31 17:57:05 -0700760 if (mStarted) {
Andy Hung73535852014-09-05 11:42:58 -0700761 // Issue a seek to refresh the video screen only if started otherwise
762 // the extractor may not yet be started and will assert.
763 // If the video decoder is not set (perhaps audio only in this case)
764 // do not perform a seek as it is not needed.
Ronghua Wua73d9e02014-10-08 15:13:29 -0700765 int64_t currentPositionUs = 0;
766 if (getCurrentPosition(&currentPositionUs) == OK) {
767 mDeferredActions.push_back(
Wei Jiac5de0912016-11-18 10:22:14 -0800768 new SeekAction(currentPositionUs,
769 MediaPlayerSeekMode::SEEK_PREVIOUS_SYNC /* mode */));
Ronghua Wua73d9e02014-10-08 15:13:29 -0700770 }
Andy Hung73535852014-09-05 11:42:58 -0700771 }
Wei Jiaac428aa2014-09-02 19:01:34 -0700772
Andreas Huber57a339c2012-12-03 11:18:00 -0800773 // If there is a new surface texture, instantiate decoders
774 // again if possible.
775 mDeferredActions.push_back(
776 new SimpleAction(&NuPlayer::performScanSources));
Andreas Huber57a339c2012-12-03 11:18:00 -0800777
Krystian Turczyn687a5892016-01-20 14:20:35 +0100778 // After a flush without shutdown, decoder is paused.
779 // Don't resume it until source seek is done, otherwise it could
780 // start pulling stale data too soon.
781 mDeferredActions.push_back(
782 new ResumeDecoderAction(false /* needNotify */));
783 }
Chong Zhang7137ec72014-11-12 16:41:05 -0800784
Andreas Huber57a339c2012-12-03 11:18:00 -0800785 processDeferredActions();
Andreas Huberf9334412010-12-15 15:17:42 -0800786 break;
787 }
788
789 case kWhatSetAudioSink:
790 {
Steve Block3856b092011-10-20 11:56:00 +0100791 ALOGV("kWhatSetAudioSink");
Andreas Huberf9334412010-12-15 15:17:42 -0800792
793 sp<RefBase> obj;
794 CHECK(msg->findObject("sink", &obj));
795
796 mAudioSink = static_cast<MediaPlayerBase::AudioSink *>(obj.get());
797 break;
798 }
799
800 case kWhatStart:
801 {
Steve Block3856b092011-10-20 11:56:00 +0100802 ALOGV("kWhatStart");
Wei Jia94211742014-10-28 17:09:06 -0700803 if (mStarted) {
Chong Zhang8a048332015-05-06 15:16:28 -0700804 // do not resume yet if the source is still buffering
805 if (!mPausedForBuffering) {
806 onResume();
807 }
Wei Jia94211742014-10-28 17:09:06 -0700808 } else {
809 onStart();
Lajos Molnar09524832014-07-17 14:29:51 -0700810 }
Chong Zhangefbb6192015-01-30 17:13:27 -0800811 mPausedByClient = false;
Andreas Huberf9334412010-12-15 15:17:42 -0800812 break;
813 }
814
Lajos Molnar3a474aa2015-04-24 17:10:07 -0700815 case kWhatConfigPlayback:
Wei Jia98160162015-02-04 17:01:11 -0800816 {
Lajos Molnar3a474aa2015-04-24 17:10:07 -0700817 sp<AReplyToken> replyID;
818 CHECK(msg->senderAwaitsResponse(&replyID));
819 AudioPlaybackRate rate /* sanitized */;
820 readFromAMessage(msg, &rate);
821 status_t err = OK;
Wei Jia98160162015-02-04 17:01:11 -0800822 if (mRenderer != NULL) {
Wei Jiabf70feb2016-02-19 15:47:16 -0800823 // AudioSink allows only 1.f and 0.f for offload mode.
824 // For other speed, switch to non-offload mode.
825 if (mOffloadAudio && ((rate.mSpeed != 0.f && rate.mSpeed != 1.f)
826 || rate.mPitch != 1.f)) {
827 int64_t currentPositionUs;
828 if (getCurrentPosition(&currentPositionUs) != OK) {
829 currentPositionUs = mPreviousSeekTimeUs;
830 }
831
832 // Set mPlaybackSettings so that the new audio decoder can
833 // be created correctly.
834 mPlaybackSettings = rate;
835 if (!mPaused) {
836 mRenderer->pause();
837 }
Wei Jia5031b2f2016-02-25 11:19:31 -0800838 restartAudio(
Wei Jiabf70feb2016-02-19 15:47:16 -0800839 currentPositionUs, true /* forceNonOffload */,
840 true /* needsToCreateAudioDecoder */);
841 if (!mPaused) {
842 mRenderer->resume();
843 }
844 }
845
Lajos Molnar3a474aa2015-04-24 17:10:07 -0700846 err = mRenderer->setPlaybackSettings(rate);
847 }
848 if (err == OK) {
849 if (rate.mSpeed == 0.f) {
850 onPause();
Wei Jia351ce872016-02-10 13:21:59 -0800851 mPausedByClient = true;
Lajos Molnar3a474aa2015-04-24 17:10:07 -0700852 // save all other settings (using non-paused speed)
853 // so we can restore them on start
854 AudioPlaybackRate newRate = rate;
855 newRate.mSpeed = mPlaybackSettings.mSpeed;
856 mPlaybackSettings = newRate;
857 } else { /* rate.mSpeed != 0.f */
Lajos Molnar3a474aa2015-04-24 17:10:07 -0700858 mPlaybackSettings = rate;
Wei Jia8a092d32016-06-03 14:57:24 -0700859 if (mStarted) {
860 // do not resume yet if the source is still buffering
861 if (!mPausedForBuffering) {
862 onResume();
863 }
864 } else if (mPrepared) {
865 onStart();
866 }
867
868 mPausedByClient = false;
Lajos Molnar3a474aa2015-04-24 17:10:07 -0700869 }
Wei Jia98160162015-02-04 17:01:11 -0800870 }
Ronghua Wu8db88132015-04-22 13:51:35 -0700871
872 if (mVideoDecoder != NULL) {
Praveen Chavanbbaa1442016-04-08 13:33:49 -0700873 sp<AMessage> params = new AMessage();
874 params->setFloat("playback-speed", mPlaybackSettings.mSpeed);
875 mVideoDecoder->setParameters(params);
Ronghua Wu8db88132015-04-22 13:51:35 -0700876 }
877
Lajos Molnar3a474aa2015-04-24 17:10:07 -0700878 sp<AMessage> response = new AMessage;
879 response->setInt32("err", err);
880 response->postReply(replyID);
881 break;
882 }
883
884 case kWhatGetPlaybackSettings:
885 {
886 sp<AReplyToken> replyID;
887 CHECK(msg->senderAwaitsResponse(&replyID));
888 AudioPlaybackRate rate = mPlaybackSettings;
889 status_t err = OK;
890 if (mRenderer != NULL) {
891 err = mRenderer->getPlaybackSettings(&rate);
892 }
893 if (err == OK) {
894 // get playback settings used by renderer, as it may be
895 // slightly off due to audiosink not taking small changes.
896 mPlaybackSettings = rate;
897 if (mPaused) {
898 rate.mSpeed = 0.f;
899 }
900 }
901 sp<AMessage> response = new AMessage;
902 if (err == OK) {
903 writeToAMessage(response, rate);
904 }
905 response->setInt32("err", err);
906 response->postReply(replyID);
907 break;
908 }
909
910 case kWhatConfigSync:
911 {
912 sp<AReplyToken> replyID;
913 CHECK(msg->senderAwaitsResponse(&replyID));
914
915 ALOGV("kWhatConfigSync");
916 AVSyncSettings sync;
917 float videoFpsHint;
918 readFromAMessage(msg, &sync, &videoFpsHint);
919 status_t err = OK;
920 if (mRenderer != NULL) {
921 err = mRenderer->setSyncSettings(sync, videoFpsHint);
922 }
923 if (err == OK) {
924 mSyncSettings = sync;
925 mVideoFpsHint = videoFpsHint;
926 }
927 sp<AMessage> response = new AMessage;
928 response->setInt32("err", err);
929 response->postReply(replyID);
930 break;
931 }
932
933 case kWhatGetSyncSettings:
934 {
935 sp<AReplyToken> replyID;
936 CHECK(msg->senderAwaitsResponse(&replyID));
937 AVSyncSettings sync = mSyncSettings;
938 float videoFps = mVideoFpsHint;
939 status_t err = OK;
940 if (mRenderer != NULL) {
941 err = mRenderer->getSyncSettings(&sync, &videoFps);
942 if (err == OK) {
943 mSyncSettings = sync;
944 mVideoFpsHint = videoFps;
945 }
946 }
947 sp<AMessage> response = new AMessage;
948 if (err == OK) {
949 writeToAMessage(response, sync, videoFps);
950 }
951 response->setInt32("err", err);
952 response->postReply(replyID);
Wei Jia98160162015-02-04 17:01:11 -0800953 break;
954 }
955
Andreas Huberf9334412010-12-15 15:17:42 -0800956 case kWhatScanSources:
957 {
Andreas Huber1aef2112011-01-04 14:01:29 -0800958 int32_t generation;
959 CHECK(msg->findInt32("generation", &generation));
960 if (generation != mScanSourcesGeneration) {
961 // Drop obsolete msg.
962 break;
963 }
964
Andreas Huber5bc087c2010-12-23 10:27:40 -0800965 mScanSourcesPending = false;
966
Steve Block3856b092011-10-20 11:56:00 +0100967 ALOGV("scanning sources haveAudio=%d, haveVideo=%d",
Andreas Huber43c3e6c2011-01-05 12:17:08 -0800968 mAudioDecoder != NULL, mVideoDecoder != NULL);
969
Andreas Huberb7c8e912012-11-27 15:02:53 -0800970 bool mHadAnySourcesBefore =
971 (mAudioDecoder != NULL) || (mVideoDecoder != NULL);
Robert Shih7350b052015-10-01 15:50:14 -0700972 bool rescan = false;
Andreas Huberb7c8e912012-11-27 15:02:53 -0800973
Andy Hung282a7e32014-08-14 15:56:34 -0700974 // initialize video before audio because successful initialization of
975 // video may change deep buffer mode of audio.
Lajos Molnar1de1e252015-04-30 18:18:34 -0700976 if (mSurface != NULL) {
Robert Shih7350b052015-10-01 15:50:14 -0700977 if (instantiateDecoder(false, &mVideoDecoder) == -EWOULDBLOCK) {
978 rescan = true;
979 }
Haynes Mathew George5d246ef2012-07-09 10:36:57 -0700980 }
Andreas Huberf9334412010-12-15 15:17:42 -0800981
Ronghua Wua10fd232014-11-06 16:15:20 -0800982 // Don't try to re-open audio sink if there's an existing decoder.
983 if (mAudioSink != NULL && mAudioDecoder == NULL) {
Robert Shih7350b052015-10-01 15:50:14 -0700984 if (instantiateDecoder(true, &mAudioDecoder) == -EWOULDBLOCK) {
985 rescan = true;
986 }
Andreas Huberf9334412010-12-15 15:17:42 -0800987 }
988
Andreas Huberb7c8e912012-11-27 15:02:53 -0800989 if (!mHadAnySourcesBefore
990 && (mAudioDecoder != NULL || mVideoDecoder != NULL)) {
991 // This is the first time we've found anything playable.
992
Andreas Huber9575c962013-02-05 13:59:56 -0800993 if (mSourceFlags & Source::FLAG_DYNAMIC_DURATION) {
Andreas Huberb7c8e912012-11-27 15:02:53 -0800994 schedulePollDuration();
995 }
996 }
997
Andreas Hubereac68ba2011-09-27 12:12:25 -0700998 status_t err;
999 if ((err = mSource->feedMoreTSData()) != OK) {
Andreas Huber1aef2112011-01-04 14:01:29 -08001000 if (mAudioDecoder == NULL && mVideoDecoder == NULL) {
1001 // We're not currently decoding anything (no audio or
1002 // video tracks found) and we just ran out of input data.
Andreas Hubereac68ba2011-09-27 12:12:25 -07001003
1004 if (err == ERROR_END_OF_STREAM) {
1005 notifyListener(MEDIA_PLAYBACK_COMPLETE, 0, 0);
1006 } else {
1007 notifyListener(MEDIA_ERROR, MEDIA_ERROR_UNKNOWN, err);
1008 }
Andreas Huber1aef2112011-01-04 14:01:29 -08001009 }
Andreas Huberf9334412010-12-15 15:17:42 -08001010 break;
1011 }
1012
Robert Shih7350b052015-10-01 15:50:14 -07001013 if (rescan) {
Andreas Huberf9334412010-12-15 15:17:42 -08001014 msg->post(100000ll);
Andreas Huber5bc087c2010-12-23 10:27:40 -08001015 mScanSourcesPending = true;
Andreas Huberf9334412010-12-15 15:17:42 -08001016 }
1017 break;
1018 }
1019
1020 case kWhatVideoNotify:
1021 case kWhatAudioNotify:
1022 {
1023 bool audio = msg->what() == kWhatAudioNotify;
1024
Wei Jia88703c32014-08-06 11:24:07 -07001025 int32_t currentDecoderGeneration =
1026 (audio? mAudioDecoderGeneration : mVideoDecoderGeneration);
1027 int32_t requesterGeneration = currentDecoderGeneration - 1;
1028 CHECK(msg->findInt32("generation", &requesterGeneration));
1029
1030 if (requesterGeneration != currentDecoderGeneration) {
1031 ALOGV("got message from old %s decoder, generation(%d:%d)",
1032 audio ? "audio" : "video", requesterGeneration,
1033 currentDecoderGeneration);
1034 sp<AMessage> reply;
1035 if (!(msg->findMessage("reply", &reply))) {
1036 return;
1037 }
1038
1039 reply->setInt32("err", INFO_DISCONTINUITY);
1040 reply->post();
1041 return;
1042 }
1043
Andreas Huberf9334412010-12-15 15:17:42 -08001044 int32_t what;
Lajos Molnar1cd13982014-01-17 15:12:51 -08001045 CHECK(msg->findInt32("what", &what));
Andreas Huberf9334412010-12-15 15:17:42 -08001046
Chong Zhang7137ec72014-11-12 16:41:05 -08001047 if (what == DecoderBase::kWhatInputDiscontinuity) {
1048 int32_t formatChange;
1049 CHECK(msg->findInt32("formatChange", &formatChange));
Andreas Huberf9334412010-12-15 15:17:42 -08001050
Chong Zhang7137ec72014-11-12 16:41:05 -08001051 ALOGV("%s discontinuity: formatChange %d",
1052 audio ? "audio" : "video", formatChange);
1053
1054 if (formatChange) {
1055 mDeferredActions.push_back(
1056 new FlushDecoderAction(
1057 audio ? FLUSH_CMD_SHUTDOWN : FLUSH_CMD_NONE,
1058 audio ? FLUSH_CMD_NONE : FLUSH_CMD_SHUTDOWN));
Andreas Huberf9334412010-12-15 15:17:42 -08001059 }
Chong Zhang7137ec72014-11-12 16:41:05 -08001060
1061 mDeferredActions.push_back(
1062 new SimpleAction(
1063 &NuPlayer::performScanSources));
1064
1065 processDeferredActions();
1066 } else if (what == DecoderBase::kWhatEOS) {
Andreas Huberdc9bacd2011-09-26 10:53:29 -07001067 int32_t err;
Lajos Molnar1cd13982014-01-17 15:12:51 -08001068 CHECK(msg->findInt32("err", &err));
Andreas Huberdc9bacd2011-09-26 10:53:29 -07001069
1070 if (err == ERROR_END_OF_STREAM) {
Steve Block3856b092011-10-20 11:56:00 +01001071 ALOGV("got %s decoder EOS", audio ? "audio" : "video");
Andreas Huberdc9bacd2011-09-26 10:53:29 -07001072 } else {
Steve Block3856b092011-10-20 11:56:00 +01001073 ALOGV("got %s decoder EOS w/ error %d",
Andreas Huberdc9bacd2011-09-26 10:53:29 -07001074 audio ? "audio" : "video",
1075 err);
1076 }
1077
1078 mRenderer->queueEOS(audio, err);
Chong Zhang7137ec72014-11-12 16:41:05 -08001079 } else if (what == DecoderBase::kWhatFlushCompleted) {
Steve Block3856b092011-10-20 11:56:00 +01001080 ALOGV("decoder %s flush completed", audio ? "audio" : "video");
Andreas Huberf9334412010-12-15 15:17:42 -08001081
Andy Hung8d121d42014-10-03 09:53:53 -07001082 handleFlushComplete(audio, true /* isDecoder */);
Andreas Huber3831a062010-12-21 10:22:33 -08001083 finishFlushIfPossible();
Chong Zhang7137ec72014-11-12 16:41:05 -08001084 } else if (what == DecoderBase::kWhatVideoSizeChanged) {
Lajos Molnar1cd13982014-01-17 15:12:51 -08001085 sp<AMessage> format;
1086 CHECK(msg->findMessage("format", &format));
1087
Wei Jiac6cfd702014-11-11 16:33:20 -08001088 sp<AMessage> inputFormat =
1089 mSource->getFormat(false /* audio */);
Andreas Huber3831a062010-12-21 10:22:33 -08001090
Bartosz Dolewski26936f72014-12-11 12:47:08 +01001091 setVideoScalingMode(mVideoScalingMode);
Wei Jiac6cfd702014-11-11 16:33:20 -08001092 updateVideoSize(inputFormat, format);
Chong Zhang7137ec72014-11-12 16:41:05 -08001093 } else if (what == DecoderBase::kWhatShutdownCompleted) {
Steve Block3856b092011-10-20 11:56:00 +01001094 ALOGV("%s shutdown completed", audio ? "audio" : "video");
Andreas Huber3831a062010-12-21 10:22:33 -08001095 if (audio) {
1096 mAudioDecoder.clear();
Wei Jia57568df2014-09-22 10:16:29 -07001097 ++mAudioDecoderGeneration;
Andreas Huber3831a062010-12-21 10:22:33 -08001098
1099 CHECK_EQ((int)mFlushingAudio, (int)SHUTTING_DOWN_DECODER);
1100 mFlushingAudio = SHUT_DOWN;
1101 } else {
1102 mVideoDecoder.clear();
Wei Jia57568df2014-09-22 10:16:29 -07001103 ++mVideoDecoderGeneration;
Andreas Huber3831a062010-12-21 10:22:33 -08001104
1105 CHECK_EQ((int)mFlushingVideo, (int)SHUTTING_DOWN_DECODER);
1106 mFlushingVideo = SHUT_DOWN;
1107 }
1108
1109 finishFlushIfPossible();
Chong Zhangf8d71772014-11-26 15:08:34 -08001110 } else if (what == DecoderBase::kWhatResumeCompleted) {
1111 finishResume();
Chong Zhang7137ec72014-11-12 16:41:05 -08001112 } else if (what == DecoderBase::kWhatError) {
Chong Zhangf4c0a942014-08-11 15:14:10 -07001113 status_t err;
Andy Hung2abde2c2014-09-30 14:40:32 -07001114 if (!msg->findInt32("err", &err) || err == OK) {
Chong Zhangf4c0a942014-08-11 15:14:10 -07001115 err = UNKNOWN_ERROR;
1116 }
Andy Hungcf31f1e2014-09-23 14:59:01 -07001117
Andy Hung2abde2c2014-09-30 14:40:32 -07001118 // Decoder errors can be due to Source (e.g. from streaming),
1119 // or from decoding corrupted bitstreams, or from other decoder
1120 // MediaCodec operations (e.g. from an ongoing reset or seek).
Andy Hung202bce12014-12-03 11:47:36 -08001121 // They may also be due to openAudioSink failure at
1122 // decoder start or after a format change.
Andy Hung2abde2c2014-09-30 14:40:32 -07001123 //
1124 // We try to gracefully shut down the affected decoder if possible,
1125 // rather than trying to force the shutdown with something
1126 // similar to performReset(). This method can lead to a hang
1127 // if MediaCodec functions block after an error, but they should
1128 // typically return INVALID_OPERATION instead of blocking.
1129
1130 FlushStatus *flushing = audio ? &mFlushingAudio : &mFlushingVideo;
1131 ALOGE("received error(%#x) from %s decoder, flushing(%d), now shutting down",
1132 err, audio ? "audio" : "video", *flushing);
1133
1134 switch (*flushing) {
1135 case NONE:
1136 mDeferredActions.push_back(
Wei Jiafef808d2014-10-31 17:57:05 -07001137 new FlushDecoderAction(
1138 audio ? FLUSH_CMD_SHUTDOWN : FLUSH_CMD_NONE,
1139 audio ? FLUSH_CMD_NONE : FLUSH_CMD_SHUTDOWN));
Andy Hung2abde2c2014-09-30 14:40:32 -07001140 processDeferredActions();
1141 break;
1142 case FLUSHING_DECODER:
1143 *flushing = FLUSHING_DECODER_SHUTDOWN; // initiate shutdown after flush.
1144 break; // Wait for flush to complete.
1145 case FLUSHING_DECODER_SHUTDOWN:
1146 break; // Wait for flush to complete.
1147 case SHUTTING_DOWN_DECODER:
1148 break; // Wait for shutdown to complete.
1149 case FLUSHED:
Andy Hung2abde2c2014-09-30 14:40:32 -07001150 getDecoder(audio)->initiateShutdown(); // In the middle of a seek.
1151 *flushing = SHUTTING_DOWN_DECODER; // Shut down.
1152 break;
1153 case SHUT_DOWN:
1154 finishFlushIfPossible(); // Should not occur.
1155 break; // Finish anyways.
Marco Nelissen9e2b7912014-08-18 16:13:03 -07001156 }
Andy Hung2abde2c2014-09-30 14:40:32 -07001157 notifyListener(MEDIA_ERROR, MEDIA_ERROR_UNKNOWN, err);
Lajos Molnar1cd13982014-01-17 15:12:51 -08001158 } else {
1159 ALOGV("Unhandled decoder notification %d '%c%c%c%c'.",
Andreas Hubera1f8ab02012-11-30 10:53:22 -08001160 what,
1161 what >> 24,
1162 (what >> 16) & 0xff,
1163 (what >> 8) & 0xff,
1164 what & 0xff);
Andreas Huberf9334412010-12-15 15:17:42 -08001165 }
1166
1167 break;
1168 }
1169
1170 case kWhatRendererNotify:
1171 {
Wei Jia57568df2014-09-22 10:16:29 -07001172 int32_t requesterGeneration = mRendererGeneration - 1;
1173 CHECK(msg->findInt32("generation", &requesterGeneration));
1174 if (requesterGeneration != mRendererGeneration) {
1175 ALOGV("got message from old renderer, generation(%d:%d)",
1176 requesterGeneration, mRendererGeneration);
1177 return;
1178 }
1179
Andreas Huberf9334412010-12-15 15:17:42 -08001180 int32_t what;
1181 CHECK(msg->findInt32("what", &what));
1182
1183 if (what == Renderer::kWhatEOS) {
1184 int32_t audio;
1185 CHECK(msg->findInt32("audio", &audio));
1186
Andreas Huberc92fd242011-08-16 13:48:44 -07001187 int32_t finalResult;
1188 CHECK(msg->findInt32("finalResult", &finalResult));
1189
Andreas Huberf9334412010-12-15 15:17:42 -08001190 if (audio) {
1191 mAudioEOS = true;
1192 } else {
1193 mVideoEOS = true;
1194 }
1195
Andreas Huberc92fd242011-08-16 13:48:44 -07001196 if (finalResult == ERROR_END_OF_STREAM) {
Steve Block3856b092011-10-20 11:56:00 +01001197 ALOGV("reached %s EOS", audio ? "audio" : "video");
Andreas Huberc92fd242011-08-16 13:48:44 -07001198 } else {
Steve Block29357bc2012-01-06 19:20:56 +00001199 ALOGE("%s track encountered an error (%d)",
Andreas Huberc92fd242011-08-16 13:48:44 -07001200 audio ? "audio" : "video", finalResult);
1201
1202 notifyListener(
1203 MEDIA_ERROR, MEDIA_ERROR_UNKNOWN, finalResult);
1204 }
Andreas Huberf9334412010-12-15 15:17:42 -08001205
1206 if ((mAudioEOS || mAudioDecoder == NULL)
1207 && (mVideoEOS || mVideoDecoder == NULL)) {
1208 notifyListener(MEDIA_PLAYBACK_COMPLETE, 0, 0);
1209 }
Andreas Huber3fe62152011-09-16 15:09:22 -07001210 } else if (what == Renderer::kWhatFlushComplete) {
Andreas Huberf9334412010-12-15 15:17:42 -08001211 int32_t audio;
1212 CHECK(msg->findInt32("audio", &audio));
1213
Wei Jia3261f0d2015-10-08 13:58:33 -07001214 if (audio) {
1215 mAudioEOS = false;
1216 } else {
1217 mVideoEOS = false;
1218 }
1219
Steve Block3856b092011-10-20 11:56:00 +01001220 ALOGV("renderer %s flush completed.", audio ? "audio" : "video");
Wei Jiada4252f2015-07-14 18:15:28 -07001221 if (audio && (mFlushingAudio == NONE || mFlushingAudio == FLUSHED
1222 || mFlushingAudio == SHUT_DOWN)) {
1223 // Flush has been handled by tear down.
1224 break;
1225 }
Andy Hung8d121d42014-10-03 09:53:53 -07001226 handleFlushComplete(audio, false /* isDecoder */);
1227 finishFlushIfPossible();
James Dongf57b4ea2012-07-20 13:38:36 -07001228 } else if (what == Renderer::kWhatVideoRenderingStart) {
1229 notifyListener(MEDIA_INFO, MEDIA_INFO_RENDERING_START, 0);
Lajos Molnarcbaffcf2013-08-14 18:30:38 -07001230 } else if (what == Renderer::kWhatMediaRenderingStart) {
1231 ALOGV("media rendering started");
1232 notifyListener(MEDIA_STARTED, 0, 0);
Ronghua Wufaeb0f22015-05-21 12:20:21 -07001233 } else if (what == Renderer::kWhatAudioTearDown) {
Ronghua Wu08529172014-10-02 16:55:52 -07001234 int32_t reason;
1235 CHECK(msg->findInt32("reason", &reason));
Ronghua Wufaeb0f22015-05-21 12:20:21 -07001236 ALOGV("Tear down audio with reason %d.", reason);
Wei Jia8456ddd2016-04-22 14:46:43 -07001237 if (reason == Renderer::kDueToTimeout && !(mPaused && mOffloadAudio)) {
1238 // TimeoutWhenPaused is only for offload mode.
1239 ALOGW("Receive a stale message for teardown.");
1240 break;
1241 }
Wei Jiada4252f2015-07-14 18:15:28 -07001242 int64_t positionUs;
Robert Shih1a5c8592015-08-04 18:07:44 -07001243 if (!msg->findInt64("positionUs", &positionUs)) {
1244 positionUs = mPreviousSeekTimeUs;
1245 }
Ronghua Wufaeb0f22015-05-21 12:20:21 -07001246
Wei Jia5031b2f2016-02-25 11:19:31 -08001247 restartAudio(
Wei Jiaa05f1e32016-03-25 16:31:22 -07001248 positionUs, reason == Renderer::kForceNonOffload /* forceNonOffload */,
1249 reason != Renderer::kDueToTimeout /* needsToCreateAudioDecoder */);
Andreas Huberf9334412010-12-15 15:17:42 -08001250 }
1251 break;
1252 }
1253
1254 case kWhatMoreDataQueued:
1255 {
1256 break;
1257 }
1258
Andreas Huber1aef2112011-01-04 14:01:29 -08001259 case kWhatReset:
1260 {
Steve Block3856b092011-10-20 11:56:00 +01001261 ALOGV("kWhatReset");
Andreas Huber1aef2112011-01-04 14:01:29 -08001262
Ronghua Wu64c2d172015-10-07 16:52:19 -07001263 mResetting = true;
1264
Andreas Hubera1f8ab02012-11-30 10:53:22 -08001265 mDeferredActions.push_back(
Wei Jiafef808d2014-10-31 17:57:05 -07001266 new FlushDecoderAction(
1267 FLUSH_CMD_SHUTDOWN /* audio */,
1268 FLUSH_CMD_SHUTDOWN /* video */));
Andreas Huberb7c8e912012-11-27 15:02:53 -08001269
Andreas Hubera1f8ab02012-11-30 10:53:22 -08001270 mDeferredActions.push_back(
1271 new SimpleAction(&NuPlayer::performReset));
Andreas Huberb58ce9f2011-11-28 16:27:35 -08001272
Andreas Hubera1f8ab02012-11-30 10:53:22 -08001273 processDeferredActions();
Andreas Huber1aef2112011-01-04 14:01:29 -08001274 break;
1275 }
1276
Andreas Huber43c3e6c2011-01-05 12:17:08 -08001277 case kWhatSeek:
1278 {
1279 int64_t seekTimeUs;
Wei Jiac5de0912016-11-18 10:22:14 -08001280 int32_t mode;
Wei Jiae427abf2014-09-22 15:21:11 -07001281 int32_t needNotify;
Andreas Huber43c3e6c2011-01-05 12:17:08 -08001282 CHECK(msg->findInt64("seekTimeUs", &seekTimeUs));
Wei Jiac5de0912016-11-18 10:22:14 -08001283 CHECK(msg->findInt32("mode", &mode));
Wei Jiae427abf2014-09-22 15:21:11 -07001284 CHECK(msg->findInt32("needNotify", &needNotify));
Andreas Huber43c3e6c2011-01-05 12:17:08 -08001285
Wei Jiac5de0912016-11-18 10:22:14 -08001286 ALOGV("kWhatSeek seekTimeUs=%lld us, mode=%d, needNotify=%d",
1287 (long long)seekTimeUs, mode, needNotify);
Andreas Huber43c3e6c2011-01-05 12:17:08 -08001288
Wei Jia1061c9c2015-05-19 16:02:17 -07001289 if (!mStarted) {
1290 // Seek before the player is started. In order to preview video,
1291 // need to start the player and pause it. This branch is called
1292 // only once if needed. After the player is started, any seek
1293 // operation will go through normal path.
Robert Shih0c61a0d2015-07-06 15:09:10 -07001294 // Audio-only cases are handled separately.
Wei Jiac5de0912016-11-18 10:22:14 -08001295 onStart(seekTimeUs, (MediaPlayerSeekMode)mode);
Robert Shih0c61a0d2015-07-06 15:09:10 -07001296 if (mStarted) {
1297 onPause();
1298 mPausedByClient = true;
1299 }
Wei Jia1061c9c2015-05-19 16:02:17 -07001300 if (needNotify) {
1301 notifyDriverSeekComplete();
1302 }
1303 break;
1304 }
1305
Andreas Hubera1f8ab02012-11-30 10:53:22 -08001306 mDeferredActions.push_back(
Wei Jiafef808d2014-10-31 17:57:05 -07001307 new FlushDecoderAction(FLUSH_CMD_FLUSH /* audio */,
1308 FLUSH_CMD_FLUSH /* video */));
Andreas Huber43c3e6c2011-01-05 12:17:08 -08001309
Wei Jiae427abf2014-09-22 15:21:11 -07001310 mDeferredActions.push_back(
Wei Jiac5de0912016-11-18 10:22:14 -08001311 new SeekAction(seekTimeUs, (MediaPlayerSeekMode)mode));
Andreas Huber43c3e6c2011-01-05 12:17:08 -08001312
Chong Zhangf8d71772014-11-26 15:08:34 -08001313 // After a flush without shutdown, decoder is paused.
1314 // Don't resume it until source seek is done, otherwise it could
Chong Zhang7137ec72014-11-12 16:41:05 -08001315 // start pulling stale data too soon.
1316 mDeferredActions.push_back(
Chong Zhangf8d71772014-11-26 15:08:34 -08001317 new ResumeDecoderAction(needNotify));
Chong Zhang7137ec72014-11-12 16:41:05 -08001318
Andreas Hubera1f8ab02012-11-30 10:53:22 -08001319 processDeferredActions();
Andreas Huber43c3e6c2011-01-05 12:17:08 -08001320 break;
1321 }
1322
Andreas Huberb4082222011-01-20 15:23:04 -08001323 case kWhatPause:
1324 {
Chong Zhangefbb6192015-01-30 17:13:27 -08001325 onPause();
1326 mPausedByClient = true;
Andreas Huberb4082222011-01-20 15:23:04 -08001327 break;
1328 }
1329
Andreas Huberb5f25f02013-02-05 10:14:26 -08001330 case kWhatSourceNotify:
1331 {
Andreas Huber9575c962013-02-05 13:59:56 -08001332 onSourceNotify(msg);
Andreas Huberb5f25f02013-02-05 10:14:26 -08001333 break;
1334 }
1335
Chong Zhanga7fa1d92014-06-11 14:49:23 -07001336 case kWhatClosedCaptionNotify:
1337 {
1338 onClosedCaptionNotify(msg);
1339 break;
1340 }
1341
Hassan Shojaniacefac142017-02-06 21:02:02 -08001342 case kWhatPrepareDrm:
1343 {
1344 status_t status = onPrepareDrm(msg);
1345
1346 sp<AMessage> response = new AMessage;
1347 response->setInt32("status", status);
1348 sp<AReplyToken> replyID;
1349 CHECK(msg->senderAwaitsResponse(&replyID));
1350 response->postReply(replyID);
1351 break;
1352 }
1353
1354 case kWhatReleaseDrm:
1355 {
1356 status_t status = onReleaseDrm();
1357
1358 sp<AMessage> response = new AMessage;
1359 response->setInt32("status", status);
1360 sp<AReplyToken> replyID;
1361 CHECK(msg->senderAwaitsResponse(&replyID));
1362 response->postReply(replyID);
1363 break;
1364 }
1365
Andreas Huberf9334412010-12-15 15:17:42 -08001366 default:
1367 TRESPASS();
1368 break;
1369 }
1370}
1371
Wei Jia94211742014-10-28 17:09:06 -07001372void NuPlayer::onResume() {
Ronghua Wu64c2d172015-10-07 16:52:19 -07001373 if (!mPaused || mResetting) {
1374 ALOGD_IF(mResetting, "resetting, onResume discarded");
Chong Zhangefbb6192015-01-30 17:13:27 -08001375 return;
1376 }
1377 mPaused = false;
Wei Jia94211742014-10-28 17:09:06 -07001378 if (mSource != NULL) {
1379 mSource->resume();
1380 } else {
1381 ALOGW("resume called when source is gone or not set");
1382 }
1383 // |mAudioDecoder| may have been released due to the pause timeout, so re-create it if
1384 // needed.
1385 if (audioDecoderStillNeeded() && mAudioDecoder == NULL) {
1386 instantiateDecoder(true /* audio */, &mAudioDecoder);
1387 }
1388 if (mRenderer != NULL) {
1389 mRenderer->resume();
1390 } else {
1391 ALOGW("resume called when renderer is gone or not set");
1392 }
Ray Essickd4d00612017-01-03 09:36:27 -08001393
1394 mLastStartedPlayingTimeNs = systemTime();
Wei Jia94211742014-10-28 17:09:06 -07001395}
1396
Lajos Molnarfcd3e942015-03-31 10:06:48 -07001397status_t NuPlayer::onInstantiateSecureDecoders() {
1398 status_t err;
1399 if (!(mSourceFlags & Source::FLAG_SECURE)) {
1400 return BAD_TYPE;
1401 }
1402
1403 if (mRenderer != NULL) {
1404 ALOGE("renderer should not be set when instantiating secure decoders");
1405 return UNKNOWN_ERROR;
1406 }
1407
1408 // TRICKY: We rely on mRenderer being null, so that decoder does not start requesting
1409 // data on instantiation.
Lajos Molnar1de1e252015-04-30 18:18:34 -07001410 if (mSurface != NULL) {
Lajos Molnarfcd3e942015-03-31 10:06:48 -07001411 err = instantiateDecoder(false, &mVideoDecoder);
1412 if (err != OK) {
1413 return err;
1414 }
1415 }
1416
1417 if (mAudioSink != NULL) {
1418 err = instantiateDecoder(true, &mAudioDecoder);
1419 if (err != OK) {
1420 return err;
1421 }
1422 }
1423 return OK;
1424}
1425
Wei Jiac5de0912016-11-18 10:22:14 -08001426void NuPlayer::onStart(int64_t startPositionUs, MediaPlayerSeekMode mode) {
Hassan Shojaniacefac142017-02-06 21:02:02 -08001427 ALOGV("onStart: mCrypto: %p (%d)", mCrypto.get(),
1428 (mCrypto != NULL ? mCrypto->getStrongCount() : 0));
1429
Robert Shih0c61a0d2015-07-06 15:09:10 -07001430 if (!mSourceStarted) {
1431 mSourceStarted = true;
1432 mSource->start();
1433 }
1434 if (startPositionUs > 0) {
Wei Jiac5de0912016-11-18 10:22:14 -08001435 performSeek(startPositionUs, mode);
Robert Shih0c61a0d2015-07-06 15:09:10 -07001436 if (mSource->getFormat(false /* audio */) == NULL) {
1437 return;
1438 }
1439 }
1440
Wei Jia94211742014-10-28 17:09:06 -07001441 mOffloadAudio = false;
1442 mAudioEOS = false;
1443 mVideoEOS = false;
Wei Jia94211742014-10-28 17:09:06 -07001444 mStarted = true;
Wei Jiafe8fe7d2016-06-08 10:29:25 -07001445 mPaused = false;
Wei Jia94211742014-10-28 17:09:06 -07001446
Wei Jia94211742014-10-28 17:09:06 -07001447 uint32_t flags = 0;
1448
1449 if (mSource->isRealTime()) {
1450 flags |= Renderer::FLAG_REAL_TIME;
1451 }
1452
Wei Jia9737d342016-12-28 14:59:59 -08001453 bool hasAudio = (mSource->getFormat(true /* audio */) != NULL);
1454 bool hasVideo = (mSource->getFormat(false /* audio */) != NULL);
1455 if (!hasAudio && !hasVideo) {
Wei Jia1de83d52016-10-10 10:15:03 -07001456 ALOGE("no metadata for either audio or video source");
1457 mSource->stop();
1458 mSourceStarted = false;
1459 notifyListener(MEDIA_ERROR, MEDIA_ERROR_UNKNOWN, ERROR_MALFORMED);
1460 return;
1461 }
Wei Jia9737d342016-12-28 14:59:59 -08001462 ALOGV_IF(!hasAudio, "no metadata for audio source"); // video only stream
1463
1464 sp<MetaData> audioMeta = mSource->getFormatMeta(true /* audio */);
Wei Jia1de83d52016-10-10 10:15:03 -07001465
Wei Jia94211742014-10-28 17:09:06 -07001466 audio_stream_type_t streamType = AUDIO_STREAM_MUSIC;
1467 if (mAudioSink != NULL) {
1468 streamType = mAudioSink->getAudioStreamType();
1469 }
1470
Wei Jia94211742014-10-28 17:09:06 -07001471 mOffloadAudio =
Wei Jia9737d342016-12-28 14:59:59 -08001472 canOffloadStream(audioMeta, hasVideo, mSource->isStreaming(), streamType)
Wei Jiabf70feb2016-02-19 15:47:16 -08001473 && (mPlaybackSettings.mSpeed == 1.f && mPlaybackSettings.mPitch == 1.f);
Hassan Shojaniacefac142017-02-06 21:02:02 -08001474
1475 // Modular DRM: Disabling audio offload if the source is protected
1476 if (mOffloadAudio && mIsDrmProtected) {
1477 mOffloadAudio = false;
1478 ALOGV("onStart: Disabling mOffloadAudio now that the source is protected.");
1479 }
1480
Wei Jia94211742014-10-28 17:09:06 -07001481 if (mOffloadAudio) {
1482 flags |= Renderer::FLAG_OFFLOAD_AUDIO;
1483 }
1484
Lajos Molnar1d15ab52015-03-04 16:46:34 -08001485 sp<AMessage> notify = new AMessage(kWhatRendererNotify, this);
Wei Jia94211742014-10-28 17:09:06 -07001486 ++mRendererGeneration;
1487 notify->setInt32("generation", mRendererGeneration);
1488 mRenderer = new Renderer(mAudioSink, notify, flags);
Wei Jia94211742014-10-28 17:09:06 -07001489 mRendererLooper = new ALooper;
1490 mRendererLooper->setName("NuPlayerRenderer");
1491 mRendererLooper->start(false, false, ANDROID_PRIORITY_AUDIO);
1492 mRendererLooper->registerHandler(mRenderer);
Lajos Molnar3a474aa2015-04-24 17:10:07 -07001493
1494 status_t err = mRenderer->setPlaybackSettings(mPlaybackSettings);
1495 if (err != OK) {
1496 mSource->stop();
Robert Shih0c61a0d2015-07-06 15:09:10 -07001497 mSourceStarted = false;
Lajos Molnar3a474aa2015-04-24 17:10:07 -07001498 notifyListener(MEDIA_ERROR, MEDIA_ERROR_UNKNOWN, err);
1499 return;
Wei Jiac8206ff2015-03-04 13:59:37 -08001500 }
Wei Jia94211742014-10-28 17:09:06 -07001501
Ronghua Wuc8a70d32015-04-29 16:26:34 -07001502 float rate = getFrameRate();
1503 if (rate > 0) {
Wei Jia94211742014-10-28 17:09:06 -07001504 mRenderer->setVideoFrameRate(rate);
1505 }
1506
Wei Jiac6cfd702014-11-11 16:33:20 -08001507 if (mVideoDecoder != NULL) {
1508 mVideoDecoder->setRenderer(mRenderer);
1509 }
1510 if (mAudioDecoder != NULL) {
1511 mAudioDecoder->setRenderer(mRenderer);
1512 }
1513
Ray Essickd4d00612017-01-03 09:36:27 -08001514 mLastStartedPlayingTimeNs = systemTime();
1515
Wei Jia94211742014-10-28 17:09:06 -07001516 postScanSources();
1517}
1518
Chong Zhangefbb6192015-01-30 17:13:27 -08001519void NuPlayer::onPause() {
1520 if (mPaused) {
1521 return;
1522 }
1523 mPaused = true;
1524 if (mSource != NULL) {
1525 mSource->pause();
1526 } else {
1527 ALOGW("pause called when source is gone or not set");
1528 }
1529 if (mRenderer != NULL) {
1530 mRenderer->pause();
1531 } else {
1532 ALOGW("pause called when renderer is gone or not set");
1533 }
Ray Essickd4d00612017-01-03 09:36:27 -08001534
1535 sp<NuPlayerDriver> driver = mDriver.promote();
1536 if (driver != NULL) {
1537 int64_t now = systemTime();
1538 int64_t played = now - mLastStartedPlayingTimeNs;
Ray Essickd4d00612017-01-03 09:36:27 -08001539
1540 driver->notifyMorePlayingTimeUs((played+500)/1000);
1541 }
Chong Zhangefbb6192015-01-30 17:13:27 -08001542}
1543
Ronghua Wud7988b12014-10-03 15:19:10 -07001544bool NuPlayer::audioDecoderStillNeeded() {
1545 // Audio decoder is no longer needed if it's in shut/shutting down status.
1546 return ((mFlushingAudio != SHUT_DOWN) && (mFlushingAudio != SHUTTING_DOWN_DECODER));
1547}
1548
Andy Hung8d121d42014-10-03 09:53:53 -07001549void NuPlayer::handleFlushComplete(bool audio, bool isDecoder) {
1550 // We wait for both the decoder flush and the renderer flush to complete
1551 // before entering either the FLUSHED or the SHUTTING_DOWN_DECODER state.
1552
1553 mFlushComplete[audio][isDecoder] = true;
1554 if (!mFlushComplete[audio][!isDecoder]) {
1555 return;
1556 }
1557
1558 FlushStatus *state = audio ? &mFlushingAudio : &mFlushingVideo;
1559 switch (*state) {
1560 case FLUSHING_DECODER:
1561 {
1562 *state = FLUSHED;
Andy Hung8d121d42014-10-03 09:53:53 -07001563 break;
1564 }
1565
1566 case FLUSHING_DECODER_SHUTDOWN:
1567 {
1568 *state = SHUTTING_DOWN_DECODER;
1569
1570 ALOGV("initiating %s decoder shutdown", audio ? "audio" : "video");
Andy Hung8d121d42014-10-03 09:53:53 -07001571 getDecoder(audio)->initiateShutdown();
1572 break;
1573 }
1574
1575 default:
1576 // decoder flush completes only occur in a flushing state.
1577 LOG_ALWAYS_FATAL_IF(isDecoder, "decoder flush in invalid state %d", *state);
1578 break;
1579 }
1580}
1581
Andreas Huber3831a062010-12-21 10:22:33 -08001582void NuPlayer::finishFlushIfPossible() {
Wei Jia53904f32014-07-29 10:22:53 -07001583 if (mFlushingAudio != NONE && mFlushingAudio != FLUSHED
1584 && mFlushingAudio != SHUT_DOWN) {
Andreas Huber3831a062010-12-21 10:22:33 -08001585 return;
1586 }
1587
Wei Jia53904f32014-07-29 10:22:53 -07001588 if (mFlushingVideo != NONE && mFlushingVideo != FLUSHED
1589 && mFlushingVideo != SHUT_DOWN) {
Andreas Huber3831a062010-12-21 10:22:33 -08001590 return;
1591 }
1592
Steve Block3856b092011-10-20 11:56:00 +01001593 ALOGV("both audio and video are flushed now.");
Andreas Huber3831a062010-12-21 10:22:33 -08001594
Andreas Huber3831a062010-12-21 10:22:33 -08001595 mFlushingAudio = NONE;
1596 mFlushingVideo = NONE;
Andreas Huber3831a062010-12-21 10:22:33 -08001597
Andy Hung8d121d42014-10-03 09:53:53 -07001598 clearFlushComplete();
1599
Andreas Hubera1f8ab02012-11-30 10:53:22 -08001600 processDeferredActions();
Andreas Huber1aef2112011-01-04 14:01:29 -08001601}
1602
1603void NuPlayer::postScanSources() {
1604 if (mScanSourcesPending) {
1605 return;
1606 }
1607
Lajos Molnar1d15ab52015-03-04 16:46:34 -08001608 sp<AMessage> msg = new AMessage(kWhatScanSources, this);
Andreas Huber1aef2112011-01-04 14:01:29 -08001609 msg->setInt32("generation", mScanSourcesGeneration);
1610 msg->post();
1611
1612 mScanSourcesPending = true;
1613}
1614
Wei Jia41cd4632016-05-13 10:53:30 -07001615void NuPlayer::tryOpenAudioSinkForOffload(
1616 const sp<AMessage> &format, const sp<MetaData> &audioMeta, bool hasVideo) {
Andy Hung202bce12014-12-03 11:47:36 -08001617 // Note: This is called early in NuPlayer to determine whether offloading
1618 // is possible; otherwise the decoders call the renderer openAudioSink directly.
Andy Hung282a7e32014-08-14 15:56:34 -07001619
Andy Hung202bce12014-12-03 11:47:36 -08001620 status_t err = mRenderer->openAudioSink(
1621 format, true /* offloadOnly */, hasVideo, AUDIO_OUTPUT_FLAG_NONE, &mOffloadAudio);
1622 if (err != OK) {
1623 // Any failure we turn off mOffloadAudio.
1624 mOffloadAudio = false;
1625 } else if (mOffloadAudio) {
Chong Zhang3b9eb1f2014-10-15 17:05:08 -07001626 sendMetaDataToHal(mAudioSink, audioMeta);
Andy Hung282a7e32014-08-14 15:56:34 -07001627 }
1628}
1629
1630void NuPlayer::closeAudioSink() {
Chong Zhang3b9eb1f2014-10-15 17:05:08 -07001631 mRenderer->closeAudioSink();
Andy Hung282a7e32014-08-14 15:56:34 -07001632}
1633
Wei Jia5031b2f2016-02-25 11:19:31 -08001634void NuPlayer::restartAudio(
Wei Jiabf70feb2016-02-19 15:47:16 -08001635 int64_t currentPositionUs, bool forceNonOffload, bool needsToCreateAudioDecoder) {
Wei Jiaa05f1e32016-03-25 16:31:22 -07001636 if (mAudioDecoder != NULL) {
1637 mAudioDecoder->pause();
1638 mAudioDecoder.clear();
1639 ++mAudioDecoderGeneration;
1640 }
Wei Jiabf70feb2016-02-19 15:47:16 -08001641 if (mFlushingAudio == FLUSHING_DECODER) {
1642 mFlushComplete[1 /* audio */][1 /* isDecoder */] = true;
1643 mFlushingAudio = FLUSHED;
1644 finishFlushIfPossible();
1645 } else if (mFlushingAudio == FLUSHING_DECODER_SHUTDOWN
1646 || mFlushingAudio == SHUTTING_DOWN_DECODER) {
1647 mFlushComplete[1 /* audio */][1 /* isDecoder */] = true;
1648 mFlushingAudio = SHUT_DOWN;
1649 finishFlushIfPossible();
1650 needsToCreateAudioDecoder = false;
1651 }
1652 if (mRenderer == NULL) {
1653 return;
1654 }
1655 closeAudioSink();
1656 mRenderer->flush(true /* audio */, false /* notifyComplete */);
1657 if (mVideoDecoder != NULL) {
1658 mRenderer->flush(false /* audio */, false /* notifyComplete */);
1659 }
1660
Wei Jiac5de0912016-11-18 10:22:14 -08001661 performSeek(currentPositionUs, MediaPlayerSeekMode::SEEK_PREVIOUS_SYNC /* mode */);
Wei Jiabf70feb2016-02-19 15:47:16 -08001662
1663 if (forceNonOffload) {
1664 mRenderer->signalDisableOffloadAudio();
1665 mOffloadAudio = false;
1666 }
1667 if (needsToCreateAudioDecoder) {
Wei Jiaa05f1e32016-03-25 16:31:22 -07001668 instantiateDecoder(true /* audio */, &mAudioDecoder, !forceNonOffload);
Wei Jiabf70feb2016-02-19 15:47:16 -08001669 }
1670}
1671
Wei Jia41cd4632016-05-13 10:53:30 -07001672void NuPlayer::determineAudioModeChange(const sp<AMessage> &audioFormat) {
Wei Jiae4d18c72015-07-13 17:58:11 -07001673 if (mSource == NULL || mAudioSink == NULL) {
1674 return;
1675 }
1676
1677 if (mRenderer == NULL) {
1678 ALOGW("No renderer can be used to determine audio mode. Use non-offload for safety.");
1679 mOffloadAudio = false;
1680 return;
1681 }
1682
1683 sp<MetaData> audioMeta = mSource->getFormatMeta(true /* audio */);
1684 sp<AMessage> videoFormat = mSource->getFormat(false /* audio */);
1685 audio_stream_type_t streamType = mAudioSink->getAudioStreamType();
1686 const bool hasVideo = (videoFormat != NULL);
Hassan Shojaniacefac142017-02-06 21:02:02 -08001687 bool canOffload = canOffloadStream(
Wei Jiabf70feb2016-02-19 15:47:16 -08001688 audioMeta, hasVideo, mSource->isStreaming(), streamType)
1689 && (mPlaybackSettings.mSpeed == 1.f && mPlaybackSettings.mPitch == 1.f);
Hassan Shojaniacefac142017-02-06 21:02:02 -08001690
1691 // Modular DRM: Disabling audio offload if the source is protected
1692 if (canOffload && mIsDrmProtected) {
1693 canOffload = false;
1694 ALOGV("determineAudioModeChange: Disabling mOffloadAudio b/c the source is protected.");
1695 }
1696
Wei Jiae4d18c72015-07-13 17:58:11 -07001697 if (canOffload) {
1698 if (!mOffloadAudio) {
1699 mRenderer->signalEnableOffloadAudio();
1700 }
1701 // open audio sink early under offload mode.
Wei Jia41cd4632016-05-13 10:53:30 -07001702 tryOpenAudioSinkForOffload(audioFormat, audioMeta, hasVideo);
Wei Jiae4d18c72015-07-13 17:58:11 -07001703 } else {
Robert Shihe1d70192015-07-23 17:54:13 -07001704 if (mOffloadAudio) {
1705 mRenderer->signalDisableOffloadAudio();
1706 mOffloadAudio = false;
1707 }
Wei Jiae4d18c72015-07-13 17:58:11 -07001708 }
1709}
1710
Wei Jiaa05f1e32016-03-25 16:31:22 -07001711status_t NuPlayer::instantiateDecoder(
1712 bool audio, sp<DecoderBase> *decoder, bool checkAudioModeChange) {
Wei Jia566da802015-08-27 13:59:30 -07001713 // The audio decoder could be cleared by tear down. If still in shut down
1714 // process, no need to create a new audio decoder.
1715 if (*decoder != NULL || (audio && mFlushingAudio == SHUT_DOWN)) {
Andreas Huberf9334412010-12-15 15:17:42 -08001716 return OK;
1717 }
1718
Andreas Huber84066782011-08-16 09:34:26 -07001719 sp<AMessage> format = mSource->getFormat(audio);
Andreas Huberf9334412010-12-15 15:17:42 -08001720
Andreas Huber84066782011-08-16 09:34:26 -07001721 if (format == NULL) {
Robert Shih7350b052015-10-01 15:50:14 -07001722 return UNKNOWN_ERROR;
1723 } else {
1724 status_t err;
1725 if (format->findInt32("err", &err) && err) {
1726 return err;
1727 }
Andreas Huberf9334412010-12-15 15:17:42 -08001728 }
1729
Ronghua Wu8db88132015-04-22 13:51:35 -07001730 format->setInt32("priority", 0 /* realtime */);
1731
Andreas Huber3fe62152011-09-16 15:09:22 -07001732 if (!audio) {
Andreas Huber84066782011-08-16 09:34:26 -07001733 AString mime;
1734 CHECK(format->findString("mime", &mime));
Chong Zhanga7fa1d92014-06-11 14:49:23 -07001735
Lajos Molnar1d15ab52015-03-04 16:46:34 -08001736 sp<AMessage> ccNotify = new AMessage(kWhatClosedCaptionNotify, this);
Chong Zhang341ab6e2015-02-04 13:37:18 -08001737 if (mCCDecoder == NULL) {
1738 mCCDecoder = new CCDecoder(ccNotify);
1739 }
Lajos Molnar09524832014-07-17 14:29:51 -07001740
1741 if (mSourceFlags & Source::FLAG_SECURE) {
1742 format->setInt32("secure", true);
1743 }
Chong Zhang17134602015-01-07 16:14:34 -08001744
1745 if (mSourceFlags & Source::FLAG_PROTECTED) {
1746 format->setInt32("protected", true);
1747 }
Ronghua Wu8db88132015-04-22 13:51:35 -07001748
Ronghua Wuc8a70d32015-04-29 16:26:34 -07001749 float rate = getFrameRate();
1750 if (rate > 0) {
Lajos Molnar3a474aa2015-04-24 17:10:07 -07001751 format->setFloat("operating-rate", rate * mPlaybackSettings.mSpeed);
Ronghua Wu8db88132015-04-22 13:51:35 -07001752 }
Andreas Huber3fe62152011-09-16 15:09:22 -07001753 }
1754
Wei Jiabc2fb722014-07-08 16:37:57 -07001755 if (audio) {
Lajos Molnar1d15ab52015-03-04 16:46:34 -08001756 sp<AMessage> notify = new AMessage(kWhatAudioNotify, this);
Wei Jia88703c32014-08-06 11:24:07 -07001757 ++mAudioDecoderGeneration;
1758 notify->setInt32("generation", mAudioDecoderGeneration);
1759
Wei Jiaa05f1e32016-03-25 16:31:22 -07001760 if (checkAudioModeChange) {
Wei Jia41cd4632016-05-13 10:53:30 -07001761 determineAudioModeChange(format);
Wei Jiaa05f1e32016-03-25 16:31:22 -07001762 }
Wei Jiabc2fb722014-07-08 16:37:57 -07001763 if (mOffloadAudio) {
Wei Jia14532f22015-12-29 11:28:15 -08001764 mSource->setOffloadAudio(true /* offload */);
1765
Haynes Mathew George8b635332015-03-30 17:59:47 -07001766 const bool hasVideo = (mSource->getFormat(false /*audio */) != NULL);
1767 format->setInt32("has-video", hasVideo);
Wei Jiac6cfd702014-11-11 16:33:20 -08001768 *decoder = new DecoderPassThrough(notify, mSource, mRenderer);
Hassan Shojaniacefac142017-02-06 21:02:02 -08001769 ALOGV("instantiateDecoder audio DecoderPassThrough hasVideo: %d", hasVideo);
Wei Jiabc2fb722014-07-08 16:37:57 -07001770 } else {
Wei Jia14532f22015-12-29 11:28:15 -08001771 mSource->setOffloadAudio(false /* offload */);
1772
Wei Jiaf2ae3e12016-10-27 17:10:59 -07001773 *decoder = new Decoder(notify, mSource, mPID, mUID, mRenderer);
Hassan Shojaniacefac142017-02-06 21:02:02 -08001774 ALOGV("instantiateDecoder audio Decoder");
Wei Jiabc2fb722014-07-08 16:37:57 -07001775 }
1776 } else {
Lajos Molnar1d15ab52015-03-04 16:46:34 -08001777 sp<AMessage> notify = new AMessage(kWhatVideoNotify, this);
Wei Jia88703c32014-08-06 11:24:07 -07001778 ++mVideoDecoderGeneration;
1779 notify->setInt32("generation", mVideoDecoderGeneration);
1780
Chong Zhang7137ec72014-11-12 16:41:05 -08001781 *decoder = new Decoder(
Wei Jiaf2ae3e12016-10-27 17:10:59 -07001782 notify, mSource, mPID, mUID, mRenderer, mSurface, mCCDecoder);
Lajos Molnard9fd6312014-11-06 11:00:00 -08001783
1784 // enable FRC if high-quality AV sync is requested, even if not
Lajos Molnar1de1e252015-04-30 18:18:34 -07001785 // directly queuing to display, as this will even improve textureview
Lajos Molnard9fd6312014-11-06 11:00:00 -08001786 // playback.
1787 {
Marco Nelissen96626b72016-12-01 13:58:59 -08001788 if (property_get_bool("persist.sys.media.avsync", false)) {
Lajos Molnard9fd6312014-11-06 11:00:00 -08001789 format->setInt32("auto-frc", 1);
1790 }
1791 }
Wei Jiabc2fb722014-07-08 16:37:57 -07001792 }
Lajos Molnar1cd13982014-01-17 15:12:51 -08001793 (*decoder)->init();
Hassan Shojaniacefac142017-02-06 21:02:02 -08001794
1795 // Modular DRM
1796 if (mIsDrmProtected) {
1797 format->setPointer("crypto", mCrypto.get());
1798 ALOGV("instantiateDecoder: mCrypto: %p (%d) isSecure: %d", mCrypto.get(),
1799 (mCrypto != NULL ? mCrypto->getStrongCount() : 0),
1800 (mSourceFlags & Source::FLAG_SECURE) != 0);
1801 }
1802
Andreas Huber84066782011-08-16 09:34:26 -07001803 (*decoder)->configure(format);
Andreas Huberf9334412010-12-15 15:17:42 -08001804
Praveen Chavanbbaa1442016-04-08 13:33:49 -07001805 if (!audio) {
1806 sp<AMessage> params = new AMessage();
1807 float rate = getFrameRate();
1808 if (rate > 0) {
1809 params->setFloat("frame-rate-total", rate);
1810 }
1811
1812 sp<MetaData> fileMeta = getFileMeta();
1813 if (fileMeta != NULL) {
1814 int32_t videoTemporalLayerCount;
1815 if (fileMeta->findInt32(kKeyTemporalLayerCount, &videoTemporalLayerCount)
1816 && videoTemporalLayerCount > 0) {
1817 params->setInt32("temporal-layer-count", videoTemporalLayerCount);
1818 }
1819 }
1820
1821 if (params->countEntries() > 0) {
1822 (*decoder)->setParameters(params);
1823 }
1824 }
Andreas Huberf9334412010-12-15 15:17:42 -08001825 return OK;
1826}
1827
Chong Zhangced1c2f2014-08-08 15:22:35 -07001828void NuPlayer::updateVideoSize(
1829 const sp<AMessage> &inputFormat,
1830 const sp<AMessage> &outputFormat) {
1831 if (inputFormat == NULL) {
1832 ALOGW("Unknown video size, reporting 0x0!");
1833 notifyListener(MEDIA_SET_VIDEO_SIZE, 0, 0);
1834 return;
1835 }
Wei Jia9737d342016-12-28 14:59:59 -08001836 int32_t err = OK;
1837 inputFormat->findInt32("err", &err);
1838 if (err == -EWOULDBLOCK) {
1839 ALOGW("Video meta is not available yet!");
1840 return;
1841 }
1842 if (err != OK) {
1843 ALOGW("Something is wrong with video meta!");
1844 return;
1845 }
Chong Zhangced1c2f2014-08-08 15:22:35 -07001846
1847 int32_t displayWidth, displayHeight;
Chong Zhangced1c2f2014-08-08 15:22:35 -07001848 if (outputFormat != NULL) {
1849 int32_t width, height;
1850 CHECK(outputFormat->findInt32("width", &width));
1851 CHECK(outputFormat->findInt32("height", &height));
1852
1853 int32_t cropLeft, cropTop, cropRight, cropBottom;
1854 CHECK(outputFormat->findRect(
1855 "crop",
1856 &cropLeft, &cropTop, &cropRight, &cropBottom));
1857
1858 displayWidth = cropRight - cropLeft + 1;
1859 displayHeight = cropBottom - cropTop + 1;
1860
1861 ALOGV("Video output format changed to %d x %d "
1862 "(crop: %d x %d @ (%d, %d))",
1863 width, height,
1864 displayWidth,
1865 displayHeight,
1866 cropLeft, cropTop);
1867 } else {
1868 CHECK(inputFormat->findInt32("width", &displayWidth));
1869 CHECK(inputFormat->findInt32("height", &displayHeight));
1870
1871 ALOGV("Video input format %d x %d", displayWidth, displayHeight);
1872 }
1873
1874 // Take into account sample aspect ratio if necessary:
1875 int32_t sarWidth, sarHeight;
1876 if (inputFormat->findInt32("sar-width", &sarWidth)
Wei Jia095bc252017-01-27 10:16:16 -08001877 && inputFormat->findInt32("sar-height", &sarHeight)
1878 && sarWidth > 0 && sarHeight > 0) {
Chong Zhangced1c2f2014-08-08 15:22:35 -07001879 ALOGV("Sample aspect ratio %d : %d", sarWidth, sarHeight);
1880
1881 displayWidth = (displayWidth * sarWidth) / sarHeight;
1882
1883 ALOGV("display dimensions %d x %d", displayWidth, displayHeight);
Wei Jiadf6c6af2016-09-29 11:27:15 -07001884 } else {
1885 int32_t width, height;
1886 if (inputFormat->findInt32("display-width", &width)
1887 && inputFormat->findInt32("display-height", &height)
1888 && width > 0 && height > 0
1889 && displayWidth > 0 && displayHeight > 0) {
1890 if (displayHeight * (int64_t)width / height > (int64_t)displayWidth) {
1891 displayHeight = (int32_t)(displayWidth * (int64_t)height / width);
1892 } else {
1893 displayWidth = (int32_t)(displayHeight * (int64_t)width / height);
1894 }
1895 ALOGV("Video display width and height are overridden to %d x %d",
1896 displayWidth, displayHeight);
1897 }
Chong Zhangced1c2f2014-08-08 15:22:35 -07001898 }
1899
1900 int32_t rotationDegrees;
1901 if (!inputFormat->findInt32("rotation-degrees", &rotationDegrees)) {
1902 rotationDegrees = 0;
1903 }
1904
1905 if (rotationDegrees == 90 || rotationDegrees == 270) {
1906 int32_t tmp = displayWidth;
1907 displayWidth = displayHeight;
1908 displayHeight = tmp;
1909 }
1910
1911 notifyListener(
1912 MEDIA_SET_VIDEO_SIZE,
1913 displayWidth,
1914 displayHeight);
1915}
1916
Chong Zhangdcb89b32013-08-06 09:44:47 -07001917void NuPlayer::notifyListener(int msg, int ext1, int ext2, const Parcel *in) {
Andreas Huber43c3e6c2011-01-05 12:17:08 -08001918 if (mDriver == NULL) {
Andreas Huberf9334412010-12-15 15:17:42 -08001919 return;
1920 }
1921
Andreas Huber43c3e6c2011-01-05 12:17:08 -08001922 sp<NuPlayerDriver> driver = mDriver.promote();
Andreas Huberf9334412010-12-15 15:17:42 -08001923
Andreas Huber43c3e6c2011-01-05 12:17:08 -08001924 if (driver == NULL) {
Andreas Huberf9334412010-12-15 15:17:42 -08001925 return;
1926 }
1927
Chong Zhangdcb89b32013-08-06 09:44:47 -07001928 driver->notifyListener(msg, ext1, ext2, in);
Andreas Huberf9334412010-12-15 15:17:42 -08001929}
1930
Chong Zhang7137ec72014-11-12 16:41:05 -08001931void NuPlayer::flushDecoder(bool audio, bool needShutdown) {
Andreas Huber14f76722013-01-15 09:04:18 -08001932 ALOGV("[%s] flushDecoder needShutdown=%d",
1933 audio ? "audio" : "video", needShutdown);
1934
Chong Zhang7137ec72014-11-12 16:41:05 -08001935 const sp<DecoderBase> &decoder = getDecoder(audio);
Lajos Molnar87603c02014-08-20 19:25:30 -07001936 if (decoder == NULL) {
Steve Blockdf64d152012-01-04 20:05:49 +00001937 ALOGI("flushDecoder %s without decoder present",
Andreas Huber6e3d3112011-11-28 12:36:11 -08001938 audio ? "audio" : "video");
Lajos Molnar87603c02014-08-20 19:25:30 -07001939 return;
Andreas Huber6e3d3112011-11-28 12:36:11 -08001940 }
1941
Andreas Huber1aef2112011-01-04 14:01:29 -08001942 // Make sure we don't continue to scan sources until we finish flushing.
1943 ++mScanSourcesGeneration;
Chong Zhang3b032b32015-04-17 15:49:06 -07001944 if (mScanSourcesPending) {
Toshikazu Saitocbcbb792015-09-15 14:53:01 +09001945 if (!needShutdown) {
1946 mDeferredActions.push_back(
1947 new SimpleAction(&NuPlayer::performScanSources));
1948 }
Chong Zhang3b032b32015-04-17 15:49:06 -07001949 mScanSourcesPending = false;
1950 }
Andreas Huber1aef2112011-01-04 14:01:29 -08001951
Chong Zhang7137ec72014-11-12 16:41:05 -08001952 decoder->signalFlush();
Andreas Huber1aef2112011-01-04 14:01:29 -08001953
1954 FlushStatus newStatus =
1955 needShutdown ? FLUSHING_DECODER_SHUTDOWN : FLUSHING_DECODER;
1956
Lajos Molnarfcd3e942015-03-31 10:06:48 -07001957 mFlushComplete[audio][false /* isDecoder */] = (mRenderer == NULL);
Andy Hung8d121d42014-10-03 09:53:53 -07001958 mFlushComplete[audio][true /* isDecoder */] = false;
Andreas Huber1aef2112011-01-04 14:01:29 -08001959 if (audio) {
Wei Jia53904f32014-07-29 10:22:53 -07001960 ALOGE_IF(mFlushingAudio != NONE,
1961 "audio flushDecoder() is called in state %d", mFlushingAudio);
Andreas Huber1aef2112011-01-04 14:01:29 -08001962 mFlushingAudio = newStatus;
Andreas Huber1aef2112011-01-04 14:01:29 -08001963 } else {
Wei Jia53904f32014-07-29 10:22:53 -07001964 ALOGE_IF(mFlushingVideo != NONE,
1965 "video flushDecoder() is called in state %d", mFlushingVideo);
Andreas Huber1aef2112011-01-04 14:01:29 -08001966 mFlushingVideo = newStatus;
Andreas Huber1aef2112011-01-04 14:01:29 -08001967 }
1968}
1969
Chong Zhangced1c2f2014-08-08 15:22:35 -07001970void NuPlayer::queueDecoderShutdown(
1971 bool audio, bool video, const sp<AMessage> &reply) {
1972 ALOGI("queueDecoderShutdown audio=%d, video=%d", audio, video);
Andreas Huber84066782011-08-16 09:34:26 -07001973
Chong Zhangced1c2f2014-08-08 15:22:35 -07001974 mDeferredActions.push_back(
Wei Jiafef808d2014-10-31 17:57:05 -07001975 new FlushDecoderAction(
1976 audio ? FLUSH_CMD_SHUTDOWN : FLUSH_CMD_NONE,
1977 video ? FLUSH_CMD_SHUTDOWN : FLUSH_CMD_NONE));
Andreas Huber84066782011-08-16 09:34:26 -07001978
Chong Zhangced1c2f2014-08-08 15:22:35 -07001979 mDeferredActions.push_back(
1980 new SimpleAction(&NuPlayer::performScanSources));
Andreas Huber84066782011-08-16 09:34:26 -07001981
Chong Zhangced1c2f2014-08-08 15:22:35 -07001982 mDeferredActions.push_back(new PostMessageAction(reply));
1983
1984 processDeferredActions();
Andreas Huber84066782011-08-16 09:34:26 -07001985}
1986
James Dong0d268a32012-08-31 12:18:27 -07001987status_t NuPlayer::setVideoScalingMode(int32_t mode) {
1988 mVideoScalingMode = mode;
Lajos Molnar1de1e252015-04-30 18:18:34 -07001989 if (mSurface != NULL) {
1990 status_t ret = native_window_set_scaling_mode(mSurface.get(), mVideoScalingMode);
James Dong0d268a32012-08-31 12:18:27 -07001991 if (ret != OK) {
1992 ALOGE("Failed to set scaling mode (%d): %s",
1993 -ret, strerror(-ret));
1994 return ret;
1995 }
1996 }
1997 return OK;
1998}
1999
Chong Zhangdcb89b32013-08-06 09:44:47 -07002000status_t NuPlayer::getTrackInfo(Parcel* reply) const {
Lajos Molnar1d15ab52015-03-04 16:46:34 -08002001 sp<AMessage> msg = new AMessage(kWhatGetTrackInfo, this);
Chong Zhangdcb89b32013-08-06 09:44:47 -07002002 msg->setPointer("reply", reply);
2003
2004 sp<AMessage> response;
2005 status_t err = msg->postAndAwaitResponse(&response);
2006 return err;
2007}
2008
Robert Shih7c4f0d72014-07-09 18:53:31 -07002009status_t NuPlayer::getSelectedTrack(int32_t type, Parcel* reply) const {
Lajos Molnar1d15ab52015-03-04 16:46:34 -08002010 sp<AMessage> msg = new AMessage(kWhatGetSelectedTrack, this);
Robert Shih7c4f0d72014-07-09 18:53:31 -07002011 msg->setPointer("reply", reply);
2012 msg->setInt32("type", type);
2013
2014 sp<AMessage> response;
2015 status_t err = msg->postAndAwaitResponse(&response);
2016 if (err == OK && response != NULL) {
2017 CHECK(response->findInt32("err", &err));
2018 }
2019 return err;
2020}
2021
Robert Shih6ffb1fd2014-10-29 16:24:32 -07002022status_t NuPlayer::selectTrack(size_t trackIndex, bool select, int64_t timeUs) {
Lajos Molnar1d15ab52015-03-04 16:46:34 -08002023 sp<AMessage> msg = new AMessage(kWhatSelectTrack, this);
Chong Zhangdcb89b32013-08-06 09:44:47 -07002024 msg->setSize("trackIndex", trackIndex);
2025 msg->setInt32("select", select);
Robert Shih6ffb1fd2014-10-29 16:24:32 -07002026 msg->setInt64("timeUs", timeUs);
Chong Zhangdcb89b32013-08-06 09:44:47 -07002027
2028 sp<AMessage> response;
2029 status_t err = msg->postAndAwaitResponse(&response);
2030
Chong Zhang404fced2014-06-11 14:45:31 -07002031 if (err != OK) {
2032 return err;
2033 }
2034
2035 if (!response->findInt32("err", &err)) {
2036 err = OK;
2037 }
2038
Chong Zhangdcb89b32013-08-06 09:44:47 -07002039 return err;
2040}
2041
Ronghua Wua73d9e02014-10-08 15:13:29 -07002042status_t NuPlayer::getCurrentPosition(int64_t *mediaUs) {
2043 sp<Renderer> renderer = mRenderer;
2044 if (renderer == NULL) {
2045 return NO_INIT;
2046 }
2047
2048 return renderer->getCurrentPosition(mediaUs);
2049}
2050
Praveen Chavane1e5d7a2015-05-19 19:09:48 -07002051void NuPlayer::getStats(Vector<sp<AMessage> > *mTrackStats) {
2052 CHECK(mTrackStats != NULL);
2053
2054 mTrackStats->clear();
2055 if (mVideoDecoder != NULL) {
2056 mTrackStats->push_back(mVideoDecoder->getStats());
2057 }
2058 if (mAudioDecoder != NULL) {
2059 mTrackStats->push_back(mAudioDecoder->getStats());
Chong Zhang7137ec72014-11-12 16:41:05 -08002060 }
Ronghua Wua73d9e02014-10-08 15:13:29 -07002061}
2062
Marco Nelissenf0b72b52014-09-16 15:43:44 -07002063sp<MetaData> NuPlayer::getFileMeta() {
2064 return mSource->getFileFormatMeta();
2065}
2066
Ronghua Wuc8a70d32015-04-29 16:26:34 -07002067float NuPlayer::getFrameRate() {
2068 sp<MetaData> meta = mSource->getFormatMeta(false /* audio */);
2069 if (meta == NULL) {
2070 return 0;
2071 }
2072 int32_t rate;
2073 if (!meta->findInt32(kKeyFrameRate, &rate)) {
2074 // fall back to try file meta
2075 sp<MetaData> fileMeta = getFileMeta();
2076 if (fileMeta == NULL) {
2077 ALOGW("source has video meta but not file meta");
2078 return -1;
2079 }
2080 int32_t fileMetaRate;
2081 if (!fileMeta->findInt32(kKeyFrameRate, &fileMetaRate)) {
2082 return -1;
2083 }
2084 return fileMetaRate;
2085 }
2086 return rate;
2087}
2088
Andreas Huberb7c8e912012-11-27 15:02:53 -08002089void NuPlayer::schedulePollDuration() {
Lajos Molnar1d15ab52015-03-04 16:46:34 -08002090 sp<AMessage> msg = new AMessage(kWhatPollDuration, this);
Andreas Huberb7c8e912012-11-27 15:02:53 -08002091 msg->setInt32("generation", mPollDurationGeneration);
2092 msg->post();
2093}
2094
2095void NuPlayer::cancelPollDuration() {
2096 ++mPollDurationGeneration;
2097}
2098
Andreas Hubera1f8ab02012-11-30 10:53:22 -08002099void NuPlayer::processDeferredActions() {
2100 while (!mDeferredActions.empty()) {
2101 // We won't execute any deferred actions until we're no longer in
2102 // an intermediate state, i.e. one more more decoders are currently
2103 // flushing or shutting down.
2104
Andreas Hubera1f8ab02012-11-30 10:53:22 -08002105 if (mFlushingAudio != NONE || mFlushingVideo != NONE) {
2106 // We're currently flushing, postpone the reset until that's
2107 // completed.
2108
2109 ALOGV("postponing action mFlushingAudio=%d, mFlushingVideo=%d",
2110 mFlushingAudio, mFlushingVideo);
2111
2112 break;
2113 }
2114
2115 sp<Action> action = *mDeferredActions.begin();
2116 mDeferredActions.erase(mDeferredActions.begin());
2117
2118 action->execute(this);
2119 }
2120}
2121
Wei Jiac5de0912016-11-18 10:22:14 -08002122void NuPlayer::performSeek(int64_t seekTimeUs, MediaPlayerSeekMode mode) {
2123 ALOGV("performSeek seekTimeUs=%lld us (%.2f secs), mode=%d",
2124 (long long)seekTimeUs, seekTimeUs / 1E6, mode);
Andreas Hubera1f8ab02012-11-30 10:53:22 -08002125
Andy Hungadf34bf2014-09-03 18:22:22 -07002126 if (mSource == NULL) {
2127 // This happens when reset occurs right before the loop mode
2128 // asynchronously seeks to the start of the stream.
2129 LOG_ALWAYS_FATAL_IF(mAudioDecoder != NULL || mVideoDecoder != NULL,
2130 "mSource is NULL and decoders not NULL audio(%p) video(%p)",
2131 mAudioDecoder.get(), mVideoDecoder.get());
2132 return;
2133 }
Robert Shih1a5c8592015-08-04 18:07:44 -07002134 mPreviousSeekTimeUs = seekTimeUs;
Wei Jiac5de0912016-11-18 10:22:14 -08002135 mSource->seekTo(seekTimeUs, mode);
Robert Shihd3b0bbb2014-07-23 15:00:25 -07002136 ++mTimedTextGeneration;
Andreas Hubera1f8ab02012-11-30 10:53:22 -08002137
Andreas Hubera1f8ab02012-11-30 10:53:22 -08002138 // everything's flushed, continue playback.
2139}
2140
Wei Jiafef808d2014-10-31 17:57:05 -07002141void NuPlayer::performDecoderFlush(FlushCommand audio, FlushCommand video) {
2142 ALOGV("performDecoderFlush audio=%d, video=%d", audio, video);
Andreas Hubera1f8ab02012-11-30 10:53:22 -08002143
Wei Jiafef808d2014-10-31 17:57:05 -07002144 if ((audio == FLUSH_CMD_NONE || mAudioDecoder == NULL)
2145 && (video == FLUSH_CMD_NONE || mVideoDecoder == NULL)) {
Andreas Hubera1f8ab02012-11-30 10:53:22 -08002146 return;
2147 }
2148
Wei Jiafef808d2014-10-31 17:57:05 -07002149 if (audio != FLUSH_CMD_NONE && mAudioDecoder != NULL) {
2150 flushDecoder(true /* audio */, (audio == FLUSH_CMD_SHUTDOWN));
Andreas Hubera1f8ab02012-11-30 10:53:22 -08002151 }
2152
Wei Jiafef808d2014-10-31 17:57:05 -07002153 if (video != FLUSH_CMD_NONE && mVideoDecoder != NULL) {
2154 flushDecoder(false /* audio */, (video == FLUSH_CMD_SHUTDOWN));
Andreas Hubera1f8ab02012-11-30 10:53:22 -08002155 }
2156}
2157
2158void NuPlayer::performReset() {
2159 ALOGV("performReset");
2160
2161 CHECK(mAudioDecoder == NULL);
2162 CHECK(mVideoDecoder == NULL);
2163
2164 cancelPollDuration();
2165
2166 ++mScanSourcesGeneration;
2167 mScanSourcesPending = false;
2168
Lajos Molnar09524832014-07-17 14:29:51 -07002169 if (mRendererLooper != NULL) {
2170 if (mRenderer != NULL) {
2171 mRendererLooper->unregisterHandler(mRenderer->id());
2172 }
2173 mRendererLooper->stop();
2174 mRendererLooper.clear();
2175 }
Andreas Hubera1f8ab02012-11-30 10:53:22 -08002176 mRenderer.clear();
Wei Jia57568df2014-09-22 10:16:29 -07002177 ++mRendererGeneration;
Andreas Hubera1f8ab02012-11-30 10:53:22 -08002178
2179 if (mSource != NULL) {
2180 mSource->stop();
Andreas Huberb5f25f02013-02-05 10:14:26 -08002181
Wei Jiac45a4b22016-04-15 15:30:23 -07002182 Mutex::Autolock autoLock(mSourceLock);
Andreas Hubera1f8ab02012-11-30 10:53:22 -08002183 mSource.clear();
2184 }
2185
2186 if (mDriver != NULL) {
2187 sp<NuPlayerDriver> driver = mDriver.promote();
2188 if (driver != NULL) {
2189 driver->notifyResetComplete();
2190 }
2191 }
Andreas Huber57a339c2012-12-03 11:18:00 -08002192
2193 mStarted = false;
Wei Jia8a092d32016-06-03 14:57:24 -07002194 mPrepared = false;
Ronghua Wu64c2d172015-10-07 16:52:19 -07002195 mResetting = false;
Robert Shih0c61a0d2015-07-06 15:09:10 -07002196 mSourceStarted = false;
Hassan Shojaniacefac142017-02-06 21:02:02 -08002197
2198 // Modular DRM
2199 if (mCrypto != NULL) {
2200 // decoders will be flushed before this so their mCrypto would go away on their own
2201 // TODO change to ALOGV
2202 ALOGD("performReset mCrypto: %p (%d)", mCrypto.get(),
2203 (mCrypto != NULL ? mCrypto->getStrongCount() : 0));
2204 mCrypto.clear();
2205 }
2206 mIsDrmProtected = false;
Andreas Hubera1f8ab02012-11-30 10:53:22 -08002207}
2208
2209void NuPlayer::performScanSources() {
2210 ALOGV("performScanSources");
2211
Andreas Huber57a339c2012-12-03 11:18:00 -08002212 if (!mStarted) {
2213 return;
2214 }
2215
Andreas Hubera1f8ab02012-11-30 10:53:22 -08002216 if (mAudioDecoder == NULL || mVideoDecoder == NULL) {
2217 postScanSources();
2218 }
2219}
2220
Lajos Molnar1de1e252015-04-30 18:18:34 -07002221void NuPlayer::performSetSurface(const sp<Surface> &surface) {
Andreas Huber57a339c2012-12-03 11:18:00 -08002222 ALOGV("performSetSurface");
2223
Lajos Molnar1de1e252015-04-30 18:18:34 -07002224 mSurface = surface;
Andreas Huber57a339c2012-12-03 11:18:00 -08002225
2226 // XXX - ignore error from setVideoScalingMode for now
2227 setVideoScalingMode(mVideoScalingMode);
Chong Zhang13d6faa2014-08-22 15:35:28 -07002228
2229 if (mDriver != NULL) {
2230 sp<NuPlayerDriver> driver = mDriver.promote();
2231 if (driver != NULL) {
2232 driver->notifySetSurfaceComplete();
2233 }
2234 }
Andreas Huber57a339c2012-12-03 11:18:00 -08002235}
2236
Chong Zhangf8d71772014-11-26 15:08:34 -08002237void NuPlayer::performResumeDecoders(bool needNotify) {
2238 if (needNotify) {
2239 mResumePending = true;
2240 if (mVideoDecoder == NULL) {
2241 // if audio-only, we can notify seek complete now,
2242 // as the resume operation will be relatively fast.
2243 finishResume();
2244 }
2245 }
2246
Chong Zhang7137ec72014-11-12 16:41:05 -08002247 if (mVideoDecoder != NULL) {
Chong Zhangf8d71772014-11-26 15:08:34 -08002248 // When there is continuous seek, MediaPlayer will cache the seek
2249 // position, and send down new seek request when previous seek is
2250 // complete. Let's wait for at least one video output frame before
2251 // notifying seek complete, so that the video thumbnail gets updated
2252 // when seekbar is dragged.
2253 mVideoDecoder->signalResume(needNotify);
Chong Zhang7137ec72014-11-12 16:41:05 -08002254 }
2255
2256 if (mAudioDecoder != NULL) {
Chong Zhangf8d71772014-11-26 15:08:34 -08002257 mAudioDecoder->signalResume(false /* needNotify */);
2258 }
2259}
2260
2261void NuPlayer::finishResume() {
2262 if (mResumePending) {
2263 mResumePending = false;
Wei Jia1061c9c2015-05-19 16:02:17 -07002264 notifyDriverSeekComplete();
2265 }
2266}
2267
2268void NuPlayer::notifyDriverSeekComplete() {
2269 if (mDriver != NULL) {
2270 sp<NuPlayerDriver> driver = mDriver.promote();
2271 if (driver != NULL) {
2272 driver->notifySeekComplete();
Chong Zhangf8d71772014-11-26 15:08:34 -08002273 }
Chong Zhang7137ec72014-11-12 16:41:05 -08002274 }
2275}
2276
Andreas Huber9575c962013-02-05 13:59:56 -08002277void NuPlayer::onSourceNotify(const sp<AMessage> &msg) {
2278 int32_t what;
2279 CHECK(msg->findInt32("what", &what));
2280
2281 switch (what) {
Lajos Molnarfcd3e942015-03-31 10:06:48 -07002282 case Source::kWhatInstantiateSecureDecoders:
2283 {
2284 if (mSource == NULL) {
2285 // This is a stale notification from a source that was
2286 // asynchronously preparing when the client called reset().
2287 // We handled the reset, the source is gone.
2288 break;
2289 }
2290
2291 sp<AMessage> reply;
2292 CHECK(msg->findMessage("reply", &reply));
2293 status_t err = onInstantiateSecureDecoders();
2294 reply->setInt32("err", err);
2295 reply->post();
2296 break;
2297 }
2298
Andreas Huber9575c962013-02-05 13:59:56 -08002299 case Source::kWhatPrepared:
2300 {
Hassan Shojaniacefac142017-02-06 21:02:02 -08002301 ALOGV("NuPlayer::onSourceNotify Source::kWhatPrepared source: %p", mSource.get());
Andreas Huberb5f28d42013-04-25 15:11:19 -07002302 if (mSource == NULL) {
2303 // This is a stale notification from a source that was
2304 // asynchronously preparing when the client called reset().
2305 // We handled the reset, the source is gone.
2306 break;
2307 }
2308
Andreas Huberec0c5972013-02-05 14:47:13 -08002309 int32_t err;
2310 CHECK(msg->findInt32("err", &err));
2311
Lajos Molnarfcd3e942015-03-31 10:06:48 -07002312 if (err != OK) {
2313 // shut down potential secure codecs in case client never calls reset
2314 mDeferredActions.push_back(
2315 new FlushDecoderAction(FLUSH_CMD_SHUTDOWN /* audio */,
2316 FLUSH_CMD_SHUTDOWN /* video */));
2317 processDeferredActions();
Wei Jia8a092d32016-06-03 14:57:24 -07002318 } else {
2319 mPrepared = true;
Lajos Molnarfcd3e942015-03-31 10:06:48 -07002320 }
2321
Andreas Huber9575c962013-02-05 13:59:56 -08002322 sp<NuPlayerDriver> driver = mDriver.promote();
2323 if (driver != NULL) {
Marco Nelissendd114d12014-05-28 15:23:14 -07002324 // notify duration first, so that it's definitely set when
2325 // the app received the "prepare complete" callback.
2326 int64_t durationUs;
2327 if (mSource->getDuration(&durationUs) == OK) {
2328 driver->notifyDuration(durationUs);
2329 }
Andreas Huberec0c5972013-02-05 14:47:13 -08002330 driver->notifyPrepareCompleted(err);
Andreas Huber9575c962013-02-05 13:59:56 -08002331 }
Andreas Huber99759402013-04-01 14:28:31 -07002332
Andreas Huber9575c962013-02-05 13:59:56 -08002333 break;
2334 }
2335
Hassan Shojaniacefac142017-02-06 21:02:02 -08002336 // Modular DRM
2337 case Source::kWhatDrmInfo:
2338 {
2339 Parcel parcel;
2340 sp<ABuffer> drmInfo;
2341 CHECK(msg->findBuffer("drmInfo", &drmInfo));
2342 parcel.setData(drmInfo->data(), drmInfo->size());
2343
2344 ALOGV("onSourceNotify() kWhatDrmInfo MEDIA_DRM_INFO drmInfo: %p parcel size: %zu",
2345 drmInfo.get(), parcel.dataSize());
2346
2347 notifyListener(MEDIA_DRM_INFO, 0 /* ext1 */, 0 /* ext2 */, &parcel);
2348
2349 break;
2350 }
2351
Andreas Huber9575c962013-02-05 13:59:56 -08002352 case Source::kWhatFlagsChanged:
2353 {
2354 uint32_t flags;
2355 CHECK(msg->findInt32("flags", (int32_t *)&flags));
2356
Chong Zhang4b7069d2013-09-11 12:52:43 -07002357 sp<NuPlayerDriver> driver = mDriver.promote();
2358 if (driver != NULL) {
Hassan Shojaniacefac142017-02-06 21:02:02 -08002359
2360 ALOGV("onSourceNotify() kWhatFlagsChanged FLAG_CAN_PAUSE: %d "
2361 "FLAG_CAN_SEEK_BACKWARD: %d \n\t\t\t\t FLAG_CAN_SEEK_FORWARD: %d "
2362 "FLAG_CAN_SEEK: %d FLAG_DYNAMIC_DURATION: %d \n"
2363 "\t\t\t\t FLAG_SECURE: %d FLAG_PROTECTED: %d",
2364 (flags & Source::FLAG_CAN_PAUSE) != 0,
2365 (flags & Source::FLAG_CAN_SEEK_BACKWARD) != 0,
2366 (flags & Source::FLAG_CAN_SEEK_FORWARD) != 0,
2367 (flags & Source::FLAG_CAN_SEEK) != 0,
2368 (flags & Source::FLAG_DYNAMIC_DURATION) != 0,
2369 (flags & Source::FLAG_SECURE) != 0,
2370 (flags & Source::FLAG_PROTECTED) != 0);
2371
Wei Jia895651b2014-12-10 17:31:52 -08002372 if ((flags & NuPlayer::Source::FLAG_CAN_SEEK) == 0) {
2373 driver->notifyListener(
2374 MEDIA_INFO, MEDIA_INFO_NOT_SEEKABLE, 0);
2375 }
Chong Zhang4b7069d2013-09-11 12:52:43 -07002376 driver->notifyFlagsChanged(flags);
2377 }
2378
Andreas Huber9575c962013-02-05 13:59:56 -08002379 if ((mSourceFlags & Source::FLAG_DYNAMIC_DURATION)
2380 && (!(flags & Source::FLAG_DYNAMIC_DURATION))) {
2381 cancelPollDuration();
2382 } else if (!(mSourceFlags & Source::FLAG_DYNAMIC_DURATION)
2383 && (flags & Source::FLAG_DYNAMIC_DURATION)
2384 && (mAudioDecoder != NULL || mVideoDecoder != NULL)) {
2385 schedulePollDuration();
2386 }
2387
2388 mSourceFlags = flags;
2389 break;
2390 }
2391
2392 case Source::kWhatVideoSizeChanged:
2393 {
Chong Zhangced1c2f2014-08-08 15:22:35 -07002394 sp<AMessage> format;
2395 CHECK(msg->findMessage("format", &format));
Andreas Huber9575c962013-02-05 13:59:56 -08002396
Chong Zhangced1c2f2014-08-08 15:22:35 -07002397 updateVideoSize(format);
Andreas Huber9575c962013-02-05 13:59:56 -08002398 break;
2399 }
2400
Chong Zhang2a3cc9a2014-08-21 17:48:26 -07002401 case Source::kWhatBufferingUpdate:
2402 {
2403 int32_t percentage;
2404 CHECK(msg->findInt32("percentage", &percentage));
2405
2406 notifyListener(MEDIA_BUFFERING_UPDATE, percentage, 0);
2407 break;
2408 }
2409
Chong Zhangefbb6192015-01-30 17:13:27 -08002410 case Source::kWhatPauseOnBufferingStart:
2411 {
2412 // ignore if not playing
Chong Zhang8a048332015-05-06 15:16:28 -07002413 if (mStarted) {
Chong Zhangefbb6192015-01-30 17:13:27 -08002414 ALOGI("buffer low, pausing...");
2415
Chong Zhang8a048332015-05-06 15:16:28 -07002416 mPausedForBuffering = true;
Chong Zhangefbb6192015-01-30 17:13:27 -08002417 onPause();
2418 }
Wei Jiabfe82072016-05-20 09:21:50 -07002419 notifyListener(MEDIA_INFO, MEDIA_INFO_BUFFERING_START, 0);
Roger Jönssonb50e83e2013-01-21 16:26:41 +01002420 break;
2421 }
2422
Chong Zhangefbb6192015-01-30 17:13:27 -08002423 case Source::kWhatResumeOnBufferingEnd:
2424 {
2425 // ignore if not playing
Chong Zhang8a048332015-05-06 15:16:28 -07002426 if (mStarted) {
Chong Zhangefbb6192015-01-30 17:13:27 -08002427 ALOGI("buffer ready, resuming...");
2428
Chong Zhang8a048332015-05-06 15:16:28 -07002429 mPausedForBuffering = false;
2430
2431 // do not resume yet if client didn't unpause
2432 if (!mPausedByClient) {
2433 onResume();
2434 }
Chong Zhangefbb6192015-01-30 17:13:27 -08002435 }
Wei Jia3bed45a2016-02-17 11:06:47 -08002436 notifyListener(MEDIA_INFO, MEDIA_INFO_BUFFERING_END, 0);
Roger Jönssonb50e83e2013-01-21 16:26:41 +01002437 break;
2438 }
2439
Chong Zhangefbb6192015-01-30 17:13:27 -08002440 case Source::kWhatCacheStats:
2441 {
2442 int32_t kbps;
2443 CHECK(msg->findInt32("bandwidth", &kbps));
2444
2445 notifyListener(MEDIA_INFO, MEDIA_INFO_NETWORK_BANDWIDTH, kbps);
2446 break;
2447 }
2448
Chong Zhangdcb89b32013-08-06 09:44:47 -07002449 case Source::kWhatSubtitleData:
2450 {
2451 sp<ABuffer> buffer;
2452 CHECK(msg->findBuffer("buffer", &buffer));
2453
Chong Zhang404fced2014-06-11 14:45:31 -07002454 sendSubtitleData(buffer, 0 /* baseIndex */);
Chong Zhangdcb89b32013-08-06 09:44:47 -07002455 break;
2456 }
2457
Robert Shih08528432015-04-08 09:06:54 -07002458 case Source::kWhatTimedMetaData:
2459 {
2460 sp<ABuffer> buffer;
2461 if (!msg->findBuffer("buffer", &buffer)) {
2462 notifyListener(MEDIA_INFO, MEDIA_INFO_METADATA_UPDATE, 0);
2463 } else {
2464 sendTimedMetaData(buffer);
2465 }
2466 break;
2467 }
2468
Robert Shihd3b0bbb2014-07-23 15:00:25 -07002469 case Source::kWhatTimedTextData:
2470 {
2471 int32_t generation;
2472 if (msg->findInt32("generation", &generation)
2473 && generation != mTimedTextGeneration) {
2474 break;
2475 }
2476
2477 sp<ABuffer> buffer;
2478 CHECK(msg->findBuffer("buffer", &buffer));
2479
2480 sp<NuPlayerDriver> driver = mDriver.promote();
2481 if (driver == NULL) {
2482 break;
2483 }
2484
2485 int posMs;
2486 int64_t timeUs, posUs;
2487 driver->getCurrentPosition(&posMs);
Patrik2 Carlsson24d484b2015-01-27 16:49:45 +01002488 posUs = (int64_t) posMs * 1000ll;
Robert Shihd3b0bbb2014-07-23 15:00:25 -07002489 CHECK(buffer->meta()->findInt64("timeUs", &timeUs));
2490
2491 if (posUs < timeUs) {
2492 if (!msg->findInt32("generation", &generation)) {
2493 msg->setInt32("generation", mTimedTextGeneration);
2494 }
2495 msg->post(timeUs - posUs);
2496 } else {
2497 sendTimedTextData(buffer);
2498 }
2499 break;
2500 }
2501
Andreas Huber14f76722013-01-15 09:04:18 -08002502 case Source::kWhatQueueDecoderShutdown:
2503 {
2504 int32_t audio, video;
2505 CHECK(msg->findInt32("audio", &audio));
2506 CHECK(msg->findInt32("video", &video));
2507
2508 sp<AMessage> reply;
2509 CHECK(msg->findMessage("reply", &reply));
2510
2511 queueDecoderShutdown(audio, video, reply);
2512 break;
2513 }
2514
Ronghua Wu80276872014-08-28 15:50:29 -07002515 case Source::kWhatDrmNoLicense:
2516 {
2517 notifyListener(MEDIA_ERROR, MEDIA_ERROR_UNKNOWN, ERROR_DRM_NO_LICENSE);
2518 break;
2519 }
2520
Andreas Huber9575c962013-02-05 13:59:56 -08002521 default:
2522 TRESPASS();
2523 }
2524}
2525
Chong Zhanga7fa1d92014-06-11 14:49:23 -07002526void NuPlayer::onClosedCaptionNotify(const sp<AMessage> &msg) {
2527 int32_t what;
2528 CHECK(msg->findInt32("what", &what));
2529
2530 switch (what) {
2531 case NuPlayer::CCDecoder::kWhatClosedCaptionData:
2532 {
2533 sp<ABuffer> buffer;
2534 CHECK(msg->findBuffer("buffer", &buffer));
2535
2536 size_t inbandTracks = 0;
2537 if (mSource != NULL) {
2538 inbandTracks = mSource->getTrackCount();
2539 }
2540
2541 sendSubtitleData(buffer, inbandTracks);
2542 break;
2543 }
2544
2545 case NuPlayer::CCDecoder::kWhatTrackAdded:
2546 {
2547 notifyListener(MEDIA_INFO, MEDIA_INFO_METADATA_UPDATE, 0);
2548
2549 break;
2550 }
2551
2552 default:
2553 TRESPASS();
2554 }
2555
2556
2557}
2558
Chong Zhang404fced2014-06-11 14:45:31 -07002559void NuPlayer::sendSubtitleData(const sp<ABuffer> &buffer, int32_t baseIndex) {
2560 int32_t trackIndex;
2561 int64_t timeUs, durationUs;
2562 CHECK(buffer->meta()->findInt32("trackIndex", &trackIndex));
2563 CHECK(buffer->meta()->findInt64("timeUs", &timeUs));
2564 CHECK(buffer->meta()->findInt64("durationUs", &durationUs));
2565
2566 Parcel in;
2567 in.writeInt32(trackIndex + baseIndex);
2568 in.writeInt64(timeUs);
2569 in.writeInt64(durationUs);
2570 in.writeInt32(buffer->size());
2571 in.writeInt32(buffer->size());
2572 in.write(buffer->data(), buffer->size());
2573
2574 notifyListener(MEDIA_SUBTITLE_DATA, 0, 0, &in);
2575}
Robert Shihd3b0bbb2014-07-23 15:00:25 -07002576
Robert Shih08528432015-04-08 09:06:54 -07002577void NuPlayer::sendTimedMetaData(const sp<ABuffer> &buffer) {
2578 int64_t timeUs;
2579 CHECK(buffer->meta()->findInt64("timeUs", &timeUs));
2580
2581 Parcel in;
2582 in.writeInt64(timeUs);
2583 in.writeInt32(buffer->size());
2584 in.writeInt32(buffer->size());
2585 in.write(buffer->data(), buffer->size());
2586
2587 notifyListener(MEDIA_META_DATA, 0, 0, &in);
2588}
2589
Robert Shihd3b0bbb2014-07-23 15:00:25 -07002590void NuPlayer::sendTimedTextData(const sp<ABuffer> &buffer) {
2591 const void *data;
2592 size_t size = 0;
2593 int64_t timeUs;
Marco Nelissen55e2f4c2015-09-04 15:57:15 -07002594 int32_t flag = TextDescriptions::IN_BAND_TEXT_3GPP;
Robert Shihd3b0bbb2014-07-23 15:00:25 -07002595
2596 AString mime;
2597 CHECK(buffer->meta()->findString("mime", &mime));
2598 CHECK(strcasecmp(mime.c_str(), MEDIA_MIMETYPE_TEXT_3GPP) == 0);
2599
2600 data = buffer->data();
2601 size = buffer->size();
2602
2603 Parcel parcel;
2604 if (size > 0) {
2605 CHECK(buffer->meta()->findInt64("timeUs", &timeUs));
Marco Nelissen55e2f4c2015-09-04 15:57:15 -07002606 int32_t global = 0;
2607 if (buffer->meta()->findInt32("global", &global) && global) {
2608 flag |= TextDescriptions::GLOBAL_DESCRIPTIONS;
2609 } else {
2610 flag |= TextDescriptions::LOCAL_DESCRIPTIONS;
2611 }
Robert Shihd3b0bbb2014-07-23 15:00:25 -07002612 TextDescriptions::getParcelOfDescriptions(
2613 (const uint8_t *)data, size, flag, timeUs / 1000, &parcel);
2614 }
2615
2616 if ((parcel.dataSize() > 0)) {
2617 notifyListener(MEDIA_TIMED_TEXT, 0, 0, &parcel);
2618 } else { // send an empty timed text
2619 notifyListener(MEDIA_TIMED_TEXT, 0, 0);
2620 }
2621}
Hassan Shojaniacefac142017-02-06 21:02:02 -08002622
2623// Modular DRM begin
2624status_t NuPlayer::prepareDrm(const uint8_t uuid[16], const Vector<uint8_t> &drmSessionId)
2625{
2626 ALOGV("prepareDrm ");
2627
2628 // Passing to the looper anyway; called in a pre-config prepared state so no race on mCrypto
2629 sp<AMessage> msg = new AMessage(kWhatPrepareDrm, this);
2630 // synchronous call so just passing the address but with local copies of "const" args
2631 uint8_t UUID[16];
2632 memcpy(UUID, uuid, sizeof(UUID));
2633 Vector<uint8_t> sessionId = drmSessionId;
2634 msg->setPointer("uuid", (void*)UUID);
2635 msg->setPointer("drmSessionId", (void*)&sessionId);
2636
2637 sp<AMessage> response;
2638 status_t status = msg->postAndAwaitResponse(&response);
2639
2640 if (status == OK && response != NULL) {
2641 CHECK(response->findInt32("status", &status));
2642 ALOGV("prepareDrm ret: %d ", status);
2643 } else {
2644 ALOGE("prepareDrm err: %d", status);
2645 }
2646
2647 return status;
2648}
2649
2650status_t NuPlayer::releaseDrm()
2651{
2652 ALOGV("releaseDrm ");
2653
2654 sp<AMessage> msg = new AMessage(kWhatReleaseDrm, this);
2655
2656 sp<AMessage> response;
2657 status_t status = msg->postAndAwaitResponse(&response);
2658
2659 if (status == OK && response != NULL) {
2660 CHECK(response->findInt32("status", &status));
2661 ALOGV("releaseDrm ret: %d ", status);
2662 } else {
2663 ALOGE("releaseDrm err: %d", status);
2664 }
2665
2666 return status;
2667}
2668
2669status_t NuPlayer::onPrepareDrm(const sp<AMessage> &msg)
2670{
2671 // TODO change to ALOGV
2672 ALOGD("onPrepareDrm ");
2673
2674 status_t status = INVALID_OPERATION;
2675 if (mSource == NULL) {
2676 ALOGE("onPrepareDrm: No source. onPrepareDrm failed with %d.", status);
2677 return status;
2678 }
2679
2680 uint8_t *uuid;
2681 Vector<uint8_t> *drmSessionId;
2682 CHECK(msg->findPointer("uuid", (void**)&uuid));
2683 CHECK(msg->findPointer("drmSessionId", (void**)&drmSessionId));
2684
2685 status = OK;
2686 sp<ICrypto> crypto = NULL;
2687
2688 status = mSource->prepareDrm(uuid, *drmSessionId, &crypto);
2689 if (crypto == NULL) {
2690 ALOGE("onPrepareDrm: mSource->prepareDrm failed. status: %d", status);
2691 return status;
2692 }
2693 ALOGV("onPrepareDrm: mSource->prepareDrm succeeded");
2694
2695 if (mCrypto != NULL) {
2696 ALOGE("onPrepareDrm: Unexpected. Already having mCrypto: %p (%d)",
2697 mCrypto.get(), mCrypto->getStrongCount());
2698 mCrypto.clear();
2699 }
2700
2701 mCrypto = crypto;
2702 mIsDrmProtected = true;
2703 // TODO change to ALOGV
2704 ALOGD("onPrepareDrm: mCrypto: %p (%d)", mCrypto.get(),
2705 (mCrypto != NULL ? mCrypto->getStrongCount() : 0));
2706
2707 return status;
2708}
2709
2710status_t NuPlayer::onReleaseDrm()
2711{
2712 // TODO change to ALOGV
2713 ALOGD("onReleaseDrm ");
2714
Hassan Shojania50b20c92017-02-16 18:28:58 -08002715 if (!mIsDrmProtected) {
2716 ALOGW("onReleaseDrm: Unexpected. mIsDrmProtected is already false.");
2717 }
2718
2719 mIsDrmProtected = false;
Hassan Shojaniacefac142017-02-06 21:02:02 -08002720
2721 status_t status;
2722 if (mCrypto != NULL) {
2723 status=OK;
2724 // first making sure the codecs have released their crypto reference
2725 const sp<DecoderBase> &videoDecoder = getDecoder(false/*audio*/);
2726 if (videoDecoder != NULL) {
2727 status = videoDecoder->releaseCrypto();
2728 ALOGV("onReleaseDrm: video decoder ret: %d", status);
2729 }
2730
2731 const sp<DecoderBase> &audioDecoder = getDecoder(true/*audio*/);
2732 if (audioDecoder != NULL) {
2733 status_t status_audio = audioDecoder->releaseCrypto();
2734 if (status == OK) { // otherwise, returning the first error
2735 status = status_audio;
2736 }
2737 ALOGV("onReleaseDrm: audio decoder ret: %d", status_audio);
2738 }
2739
2740 // TODO change to ALOGV
2741 ALOGD("onReleaseDrm: mCrypto: %p (%d)", mCrypto.get(),
2742 (mCrypto != NULL ? mCrypto->getStrongCount() : 0));
2743 mCrypto.clear();
2744 } else { // mCrypto == NULL
2745 ALOGE("onReleaseDrm: Unexpected. There is no crypto.");
2746 status = INVALID_OPERATION;
2747 }
2748
2749 return status;
2750}
2751// Modular DRM end
Andreas Huberb5f25f02013-02-05 10:14:26 -08002752////////////////////////////////////////////////////////////////////////////////
2753
Chong Zhangced1c2f2014-08-08 15:22:35 -07002754sp<AMessage> NuPlayer::Source::getFormat(bool audio) {
2755 sp<MetaData> meta = getFormatMeta(audio);
2756
2757 if (meta == NULL) {
2758 return NULL;
2759 }
2760
2761 sp<AMessage> msg = new AMessage;
2762
2763 if(convertMetaDataToMessage(meta, &msg) == OK) {
2764 return msg;
2765 }
2766 return NULL;
2767}
2768
Andreas Huber9575c962013-02-05 13:59:56 -08002769void NuPlayer::Source::notifyFlagsChanged(uint32_t flags) {
2770 sp<AMessage> notify = dupNotify();
2771 notify->setInt32("what", kWhatFlagsChanged);
2772 notify->setInt32("flags", flags);
2773 notify->post();
2774}
2775
Chong Zhangced1c2f2014-08-08 15:22:35 -07002776void NuPlayer::Source::notifyVideoSizeChanged(const sp<AMessage> &format) {
Andreas Huber9575c962013-02-05 13:59:56 -08002777 sp<AMessage> notify = dupNotify();
2778 notify->setInt32("what", kWhatVideoSizeChanged);
Chong Zhangced1c2f2014-08-08 15:22:35 -07002779 notify->setMessage("format", format);
Andreas Huber9575c962013-02-05 13:59:56 -08002780 notify->post();
2781}
2782
Andreas Huberec0c5972013-02-05 14:47:13 -08002783void NuPlayer::Source::notifyPrepared(status_t err) {
Hassan Shojaniacefac142017-02-06 21:02:02 -08002784 ALOGV("Source::notifyPrepared %d", err);
Andreas Huber9575c962013-02-05 13:59:56 -08002785 sp<AMessage> notify = dupNotify();
2786 notify->setInt32("what", kWhatPrepared);
Andreas Huberec0c5972013-02-05 14:47:13 -08002787 notify->setInt32("err", err);
Andreas Huber9575c962013-02-05 13:59:56 -08002788 notify->post();
2789}
2790
Hassan Shojaniacefac142017-02-06 21:02:02 -08002791void NuPlayer::Source::notifyDrmInfo(const sp<ABuffer> &drmInfoBuffer)
2792{
2793 ALOGV("Source::notifyDrmInfo");
2794
2795 sp<AMessage> notify = dupNotify();
2796 notify->setInt32("what", kWhatDrmInfo);
2797 notify->setBuffer("drmInfo", drmInfoBuffer);
2798
2799 notify->post();
2800}
2801
Lajos Molnarfcd3e942015-03-31 10:06:48 -07002802void NuPlayer::Source::notifyInstantiateSecureDecoders(const sp<AMessage> &reply) {
2803 sp<AMessage> notify = dupNotify();
2804 notify->setInt32("what", kWhatInstantiateSecureDecoders);
2805 notify->setMessage("reply", reply);
2806 notify->post();
2807}
2808
Andreas Huber84333e02014-02-07 15:36:10 -08002809void NuPlayer::Source::onMessageReceived(const sp<AMessage> & /* msg */) {
Andreas Huberb5f25f02013-02-05 10:14:26 -08002810 TRESPASS();
2811}
2812
Andreas Huberf9334412010-12-15 15:17:42 -08002813} // namespace android