blob: 54682318db46ffef85595e5d06a12c1e0c048846 [file] [log] [blame]
Andreas Huberf9334412010-12-15 15:17:42 -08001/*
2 * Copyright (C) 2010 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17//#define LOG_NDEBUG 0
18#define LOG_TAG "NuPlayer"
Ray Essickd4d00612017-01-03 09:36:27 -080019
20#include <inttypes.h>
21
Andreas Huberf9334412010-12-15 15:17:42 -080022#include <utils/Log.h>
23
24#include "NuPlayer.h"
Andreas Huber5bc087c2010-12-23 10:27:40 -080025
26#include "HTTPLiveSource.h"
Chong Zhang7137ec72014-11-12 16:41:05 -080027#include "NuPlayerCCDecoder.h"
Andreas Huberf9334412010-12-15 15:17:42 -080028#include "NuPlayerDecoder.h"
Chong Zhang7137ec72014-11-12 16:41:05 -080029#include "NuPlayerDecoderBase.h"
Wei Jiabc2fb722014-07-08 16:37:57 -070030#include "NuPlayerDecoderPassThrough.h"
Andreas Huber43c3e6c2011-01-05 12:17:08 -080031#include "NuPlayerDriver.h"
Andreas Huberf9334412010-12-15 15:17:42 -080032#include "NuPlayerRenderer.h"
Andreas Huber5bc087c2010-12-23 10:27:40 -080033#include "NuPlayerSource.h"
Andreas Huber2bfdd422011-10-11 15:24:07 -070034#include "RTSPSource.h"
Andreas Huber5bc087c2010-12-23 10:27:40 -080035#include "StreamingSource.h"
Andreas Huberafed0e12011-09-20 15:39:58 -070036#include "GenericSource.h"
Robert Shihd3b0bbb2014-07-23 15:00:25 -070037#include "TextDescriptions.h"
Andreas Huber5bc087c2010-12-23 10:27:40 -080038
39#include "ATSParser.h"
Andreas Huberf9334412010-12-15 15:17:42 -080040
Lajos Molnard9fd6312014-11-06 11:00:00 -080041#include <cutils/properties.h>
42
Lajos Molnar3a474aa2015-04-24 17:10:07 -070043#include <media/AudioResamplerPublic.h>
44#include <media/AVSyncSettings.h>
Wonsik Kim7e34bf52016-08-23 00:09:18 +090045#include <media/MediaCodecBuffer.h>
Lajos Molnar3a474aa2015-04-24 17:10:07 -070046
Andreas Huber3831a062010-12-21 10:22:33 -080047#include <media/stagefright/foundation/hexdump.h>
Andreas Huberf9334412010-12-15 15:17:42 -080048#include <media/stagefright/foundation/ABuffer.h>
49#include <media/stagefright/foundation/ADebug.h>
50#include <media/stagefright/foundation/AMessage.h>
Lajos Molnar09524832014-07-17 14:29:51 -070051#include <media/stagefright/MediaBuffer.h>
Andreas Huber3fe62152011-09-16 15:09:22 -070052#include <media/stagefright/MediaDefs.h>
Andreas Huberf9334412010-12-15 15:17:42 -080053#include <media/stagefright/MediaErrors.h>
54#include <media/stagefright/MetaData.h>
Lajos Molnar1de1e252015-04-30 18:18:34 -070055
Andy McFadden8ba01022012-12-18 09:46:54 -080056#include <gui/IGraphicBufferProducer.h>
Lajos Molnar1de1e252015-04-30 18:18:34 -070057#include <gui/Surface.h>
Andreas Huberf9334412010-12-15 15:17:42 -080058
Andreas Huber3fe62152011-09-16 15:09:22 -070059#include "avc_utils.h"
60
Andreas Huber84066782011-08-16 09:34:26 -070061#include "ESDS.h"
62#include <media/stagefright/Utils.h>
63
Andreas Huberf9334412010-12-15 15:17:42 -080064namespace android {
65
Andreas Hubera1f8ab02012-11-30 10:53:22 -080066struct NuPlayer::Action : public RefBase {
67 Action() {}
68
69 virtual void execute(NuPlayer *player) = 0;
70
71private:
72 DISALLOW_EVIL_CONSTRUCTORS(Action);
73};
74
75struct NuPlayer::SeekAction : public Action {
Wei Jiac5de0912016-11-18 10:22:14 -080076 explicit SeekAction(int64_t seekTimeUs, MediaPlayerSeekMode mode)
Wei Jia14486822016-11-02 17:51:30 -070077 : mSeekTimeUs(seekTimeUs),
Wei Jiac5de0912016-11-18 10:22:14 -080078 mMode(mode) {
Andreas Hubera1f8ab02012-11-30 10:53:22 -080079 }
80
81 virtual void execute(NuPlayer *player) {
Wei Jiac5de0912016-11-18 10:22:14 -080082 player->performSeek(mSeekTimeUs, mMode);
Andreas Hubera1f8ab02012-11-30 10:53:22 -080083 }
84
85private:
86 int64_t mSeekTimeUs;
Wei Jiac5de0912016-11-18 10:22:14 -080087 MediaPlayerSeekMode mMode;
Andreas Hubera1f8ab02012-11-30 10:53:22 -080088
89 DISALLOW_EVIL_CONSTRUCTORS(SeekAction);
90};
91
Chong Zhangf8d71772014-11-26 15:08:34 -080092struct NuPlayer::ResumeDecoderAction : public Action {
Chih-Hung Hsieh090ef602016-04-27 10:39:54 -070093 explicit ResumeDecoderAction(bool needNotify)
Chong Zhangf8d71772014-11-26 15:08:34 -080094 : mNeedNotify(needNotify) {
95 }
96
97 virtual void execute(NuPlayer *player) {
98 player->performResumeDecoders(mNeedNotify);
99 }
100
101private:
102 bool mNeedNotify;
103
104 DISALLOW_EVIL_CONSTRUCTORS(ResumeDecoderAction);
105};
106
Andreas Huber57a339c2012-12-03 11:18:00 -0800107struct NuPlayer::SetSurfaceAction : public Action {
Chih-Hung Hsieh090ef602016-04-27 10:39:54 -0700108 explicit SetSurfaceAction(const sp<Surface> &surface)
Lajos Molnar1de1e252015-04-30 18:18:34 -0700109 : mSurface(surface) {
Andreas Huber57a339c2012-12-03 11:18:00 -0800110 }
111
112 virtual void execute(NuPlayer *player) {
Lajos Molnar1de1e252015-04-30 18:18:34 -0700113 player->performSetSurface(mSurface);
Andreas Huber57a339c2012-12-03 11:18:00 -0800114 }
115
116private:
Lajos Molnar1de1e252015-04-30 18:18:34 -0700117 sp<Surface> mSurface;
Andreas Huber57a339c2012-12-03 11:18:00 -0800118
119 DISALLOW_EVIL_CONSTRUCTORS(SetSurfaceAction);
120};
121
Wei Jiafef808d2014-10-31 17:57:05 -0700122struct NuPlayer::FlushDecoderAction : public Action {
123 FlushDecoderAction(FlushCommand audio, FlushCommand video)
Andreas Huber14f76722013-01-15 09:04:18 -0800124 : mAudio(audio),
125 mVideo(video) {
126 }
127
128 virtual void execute(NuPlayer *player) {
Wei Jiafef808d2014-10-31 17:57:05 -0700129 player->performDecoderFlush(mAudio, mVideo);
Andreas Huber14f76722013-01-15 09:04:18 -0800130 }
131
132private:
Wei Jiafef808d2014-10-31 17:57:05 -0700133 FlushCommand mAudio;
134 FlushCommand mVideo;
Andreas Huber14f76722013-01-15 09:04:18 -0800135
Wei Jiafef808d2014-10-31 17:57:05 -0700136 DISALLOW_EVIL_CONSTRUCTORS(FlushDecoderAction);
Andreas Huber14f76722013-01-15 09:04:18 -0800137};
138
139struct NuPlayer::PostMessageAction : public Action {
Chih-Hung Hsieh090ef602016-04-27 10:39:54 -0700140 explicit PostMessageAction(const sp<AMessage> &msg)
Andreas Huber14f76722013-01-15 09:04:18 -0800141 : mMessage(msg) {
142 }
143
144 virtual void execute(NuPlayer *) {
145 mMessage->post();
146 }
147
148private:
149 sp<AMessage> mMessage;
150
151 DISALLOW_EVIL_CONSTRUCTORS(PostMessageAction);
152};
153
Andreas Hubera1f8ab02012-11-30 10:53:22 -0800154// Use this if there's no state necessary to save in order to execute
155// the action.
156struct NuPlayer::SimpleAction : public Action {
157 typedef void (NuPlayer::*ActionFunc)();
158
Chih-Hung Hsieh090ef602016-04-27 10:39:54 -0700159 explicit SimpleAction(ActionFunc func)
Andreas Hubera1f8ab02012-11-30 10:53:22 -0800160 : mFunc(func) {
161 }
162
163 virtual void execute(NuPlayer *player) {
164 (player->*mFunc)();
165 }
166
167private:
168 ActionFunc mFunc;
169
170 DISALLOW_EVIL_CONSTRUCTORS(SimpleAction);
171};
172
Andreas Huberf9334412010-12-15 15:17:42 -0800173////////////////////////////////////////////////////////////////////////////////
174
Ronghua Wu68845c12015-07-21 09:50:48 -0700175NuPlayer::NuPlayer(pid_t pid)
Andreas Huber9b80c2b2011-06-30 15:47:02 -0700176 : mUIDValid(false),
Ronghua Wu68845c12015-07-21 09:50:48 -0700177 mPID(pid),
Andreas Huber9575c962013-02-05 13:59:56 -0800178 mSourceFlags(0),
Wei Jiabc2fb722014-07-08 16:37:57 -0700179 mOffloadAudio(false),
Wei Jia88703c32014-08-06 11:24:07 -0700180 mAudioDecoderGeneration(0),
181 mVideoDecoderGeneration(0),
Wei Jia57568df2014-09-22 10:16:29 -0700182 mRendererGeneration(0),
Robert Shih1a5c8592015-08-04 18:07:44 -0700183 mPreviousSeekTimeUs(0),
Andreas Huber9b80c2b2011-06-30 15:47:02 -0700184 mAudioEOS(false),
Andreas Huberf9334412010-12-15 15:17:42 -0800185 mVideoEOS(false),
Andreas Huber5bc087c2010-12-23 10:27:40 -0800186 mScanSourcesPending(false),
Andreas Huber1aef2112011-01-04 14:01:29 -0800187 mScanSourcesGeneration(0),
Andreas Huberb7c8e912012-11-27 15:02:53 -0800188 mPollDurationGeneration(0),
Robert Shihd3b0bbb2014-07-23 15:00:25 -0700189 mTimedTextGeneration(0),
Andreas Huberf9334412010-12-15 15:17:42 -0800190 mFlushingAudio(NONE),
Andreas Huber1aef2112011-01-04 14:01:29 -0800191 mFlushingVideo(NONE),
Chong Zhangf8d71772014-11-26 15:08:34 -0800192 mResumePending(false),
Andreas Huber57a339c2012-12-03 11:18:00 -0800193 mVideoScalingMode(NATIVE_WINDOW_SCALING_MODE_SCALE_TO_WINDOW),
Lajos Molnar3a474aa2015-04-24 17:10:07 -0700194 mPlaybackSettings(AUDIO_PLAYBACK_RATE_DEFAULT),
195 mVideoFpsHint(-1.f),
Chong Zhangefbb6192015-01-30 17:13:27 -0800196 mStarted(false),
Wei Jia8a092d32016-06-03 14:57:24 -0700197 mPrepared(false),
Ronghua Wu64c2d172015-10-07 16:52:19 -0700198 mResetting(false),
Robert Shih0c61a0d2015-07-06 15:09:10 -0700199 mSourceStarted(false),
Wei Jia686e8e52017-04-03 14:08:01 -0700200 mAudioDecoderError(false),
201 mVideoDecoderError(false),
Chong Zhangefbb6192015-01-30 17:13:27 -0800202 mPaused(false),
Wei Jia71c75e02016-02-04 09:40:47 -0800203 mPausedByClient(true),
Hassan Shojania50b20c92017-02-16 18:28:58 -0800204 mPausedForBuffering(false),
205 mIsDrmProtected(false) {
Andy Hung8d121d42014-10-03 09:53:53 -0700206 clearFlushComplete();
Andreas Huberf9334412010-12-15 15:17:42 -0800207}
208
209NuPlayer::~NuPlayer() {
210}
211
Andreas Huber9b80c2b2011-06-30 15:47:02 -0700212void NuPlayer::setUID(uid_t uid) {
213 mUIDValid = true;
214 mUID = uid;
215}
216
Andreas Huber43c3e6c2011-01-05 12:17:08 -0800217void NuPlayer::setDriver(const wp<NuPlayerDriver> &driver) {
218 mDriver = driver;
Andreas Huberf9334412010-12-15 15:17:42 -0800219}
220
Andreas Huber9575c962013-02-05 13:59:56 -0800221void NuPlayer::setDataSourceAsync(const sp<IStreamSource> &source) {
Lajos Molnar1d15ab52015-03-04 16:46:34 -0800222 sp<AMessage> msg = new AMessage(kWhatSetDataSource, this);
Andreas Huberf9334412010-12-15 15:17:42 -0800223
Lajos Molnar1d15ab52015-03-04 16:46:34 -0800224 sp<AMessage> notify = new AMessage(kWhatSourceNotify, this);
Andreas Huberb5f25f02013-02-05 10:14:26 -0800225
Andreas Huber240abcc2014-02-13 13:32:37 -0800226 msg->setObject("source", new StreamingSource(notify, source));
Andreas Huber5bc087c2010-12-23 10:27:40 -0800227 msg->post();
228}
Andreas Huberf9334412010-12-15 15:17:42 -0800229
Andreas Huberafed0e12011-09-20 15:39:58 -0700230static bool IsHTTPLiveURL(const char *url) {
231 if (!strncasecmp("http://", url, 7)
Andreas Huber99759402013-04-01 14:28:31 -0700232 || !strncasecmp("https://", url, 8)
233 || !strncasecmp("file://", url, 7)) {
Andreas Huberafed0e12011-09-20 15:39:58 -0700234 size_t len = strlen(url);
235 if (len >= 5 && !strcasecmp(".m3u8", &url[len - 5])) {
236 return true;
237 }
238
239 if (strstr(url,"m3u8")) {
240 return true;
241 }
242 }
243
244 return false;
245}
246
Andreas Huber9575c962013-02-05 13:59:56 -0800247void NuPlayer::setDataSourceAsync(
Andreas Huber1b86fe02014-01-29 11:13:26 -0800248 const sp<IMediaHTTPService> &httpService,
249 const char *url,
250 const KeyedVector<String8, String8> *headers) {
Chong Zhang3de157d2014-08-05 20:54:44 -0700251
Lajos Molnar1d15ab52015-03-04 16:46:34 -0800252 sp<AMessage> msg = new AMessage(kWhatSetDataSource, this);
Oscar Rydhé7a33b772012-02-20 10:15:48 +0100253 size_t len = strlen(url);
Andreas Huber5bc087c2010-12-23 10:27:40 -0800254
Lajos Molnar1d15ab52015-03-04 16:46:34 -0800255 sp<AMessage> notify = new AMessage(kWhatSourceNotify, this);
Andreas Huberb5f25f02013-02-05 10:14:26 -0800256
Andreas Huberafed0e12011-09-20 15:39:58 -0700257 sp<Source> source;
258 if (IsHTTPLiveURL(url)) {
Andreas Huber81e68442014-02-05 11:52:33 -0800259 source = new HTTPLiveSource(notify, httpService, url, headers);
Hassan Shojaniacefac142017-02-06 21:02:02 -0800260 ALOGV("setDataSourceAsync HTTPLiveSource %s", url);
Andreas Huberafed0e12011-09-20 15:39:58 -0700261 } else if (!strncasecmp(url, "rtsp://", 7)) {
Andreas Huber1b86fe02014-01-29 11:13:26 -0800262 source = new RTSPSource(
263 notify, httpService, url, headers, mUIDValid, mUID);
Hassan Shojaniacefac142017-02-06 21:02:02 -0800264 ALOGV("setDataSourceAsync RTSPSource %s", url);
Oscar Rydhé7a33b772012-02-20 10:15:48 +0100265 } else if ((!strncasecmp(url, "http://", 7)
266 || !strncasecmp(url, "https://", 8))
267 && ((len >= 4 && !strcasecmp(".sdp", &url[len - 4]))
268 || strstr(url, ".sdp?"))) {
Andreas Huber1b86fe02014-01-29 11:13:26 -0800269 source = new RTSPSource(
270 notify, httpService, url, headers, mUIDValid, mUID, true);
Hassan Shojaniacefac142017-02-06 21:02:02 -0800271 ALOGV("setDataSourceAsync RTSPSource http/https/.sdp %s", url);
Andreas Huber2bfdd422011-10-11 15:24:07 -0700272 } else {
Hassan Shojaniacefac142017-02-06 21:02:02 -0800273 ALOGV("setDataSourceAsync GenericSource %s", url);
274
Chong Zhang3de157d2014-08-05 20:54:44 -0700275 sp<GenericSource> genericSource =
276 new GenericSource(notify, mUIDValid, mUID);
Andreas Huber2bfdd422011-10-11 15:24:07 -0700277
Chong Zhanga19f33e2014-08-07 15:35:07 -0700278 status_t err = genericSource->setDataSource(httpService, url, headers);
Chong Zhang3de157d2014-08-05 20:54:44 -0700279
280 if (err == OK) {
281 source = genericSource;
282 } else {
Chong Zhanga19f33e2014-08-07 15:35:07 -0700283 ALOGE("Failed to set data source!");
Chong Zhang3de157d2014-08-05 20:54:44 -0700284 }
285 }
Andreas Huberafed0e12011-09-20 15:39:58 -0700286 msg->setObject("source", source);
287 msg->post();
288}
289
Andreas Huber9575c962013-02-05 13:59:56 -0800290void NuPlayer::setDataSourceAsync(int fd, int64_t offset, int64_t length) {
Lajos Molnar1d15ab52015-03-04 16:46:34 -0800291 sp<AMessage> msg = new AMessage(kWhatSetDataSource, this);
Andreas Huberafed0e12011-09-20 15:39:58 -0700292
Lajos Molnar1d15ab52015-03-04 16:46:34 -0800293 sp<AMessage> notify = new AMessage(kWhatSourceNotify, this);
Andreas Huberb5f25f02013-02-05 10:14:26 -0800294
Chong Zhang3de157d2014-08-05 20:54:44 -0700295 sp<GenericSource> source =
296 new GenericSource(notify, mUIDValid, mUID);
297
Hassan Shojaniacefac142017-02-06 21:02:02 -0800298 ALOGV("setDataSourceAsync fd %d/%lld/%lld source: %p",
299 fd, (long long)offset, (long long)length, source.get());
300
Chong Zhanga19f33e2014-08-07 15:35:07 -0700301 status_t err = source->setDataSource(fd, offset, length);
Chong Zhang3de157d2014-08-05 20:54:44 -0700302
303 if (err != OK) {
Chong Zhanga19f33e2014-08-07 15:35:07 -0700304 ALOGE("Failed to set data source!");
Chong Zhang3de157d2014-08-05 20:54:44 -0700305 source = NULL;
306 }
307
Andreas Huberafed0e12011-09-20 15:39:58 -0700308 msg->setObject("source", source);
Andreas Huberf9334412010-12-15 15:17:42 -0800309 msg->post();
310}
311
Chris Watkins99f31602015-03-20 13:06:33 -0700312void NuPlayer::setDataSourceAsync(const sp<DataSource> &dataSource) {
313 sp<AMessage> msg = new AMessage(kWhatSetDataSource, this);
314 sp<AMessage> notify = new AMessage(kWhatSourceNotify, this);
315
316 sp<GenericSource> source = new GenericSource(notify, mUIDValid, mUID);
317 status_t err = source->setDataSource(dataSource);
318
319 if (err != OK) {
320 ALOGE("Failed to set data source!");
321 source = NULL;
322 }
323
324 msg->setObject("source", source);
325 msg->post();
326}
327
Wei Jia48fa06d2016-12-20 15:30:49 -0800328status_t NuPlayer::getDefaultBufferingSettings(
329 BufferingSettings *buffering /* nonnull */) {
330 sp<AMessage> msg = new AMessage(kWhatGetDefaultBufferingSettings, this);
331 sp<AMessage> response;
332 status_t err = msg->postAndAwaitResponse(&response);
333 if (err == OK && response != NULL) {
334 CHECK(response->findInt32("err", &err));
335 if (err == OK) {
336 readFromAMessage(response, buffering);
337 }
338 }
339 return err;
340}
341
342status_t NuPlayer::setBufferingSettings(const BufferingSettings& buffering) {
343 sp<AMessage> msg = new AMessage(kWhatSetBufferingSettings, this);
344 writeToAMessage(msg, buffering);
345 sp<AMessage> response;
346 status_t err = msg->postAndAwaitResponse(&response);
347 if (err == OK && response != NULL) {
348 CHECK(response->findInt32("err", &err));
349 }
350 return err;
351}
352
Andreas Huber9575c962013-02-05 13:59:56 -0800353void NuPlayer::prepareAsync() {
Hassan Shojaniacefac142017-02-06 21:02:02 -0800354 ALOGV("prepareAsync");
355
Lajos Molnar1d15ab52015-03-04 16:46:34 -0800356 (new AMessage(kWhatPrepare, this))->post();
Andreas Huber9575c962013-02-05 13:59:56 -0800357}
358
Andreas Huber57a339c2012-12-03 11:18:00 -0800359void NuPlayer::setVideoSurfaceTextureAsync(
Andy McFadden8ba01022012-12-18 09:46:54 -0800360 const sp<IGraphicBufferProducer> &bufferProducer) {
Lajos Molnar1de1e252015-04-30 18:18:34 -0700361 sp<AMessage> msg = new AMessage(kWhatSetVideoSurface, this);
Andreas Huber57a339c2012-12-03 11:18:00 -0800362
Andy McFadden8ba01022012-12-18 09:46:54 -0800363 if (bufferProducer == NULL) {
Lajos Molnar1de1e252015-04-30 18:18:34 -0700364 msg->setObject("surface", NULL);
Andreas Huber57a339c2012-12-03 11:18:00 -0800365 } else {
Lajos Molnar1de1e252015-04-30 18:18:34 -0700366 msg->setObject("surface", new Surface(bufferProducer, true /* controlledByApp */));
Andreas Huber57a339c2012-12-03 11:18:00 -0800367 }
368
Andreas Huberf9334412010-12-15 15:17:42 -0800369 msg->post();
370}
371
372void NuPlayer::setAudioSink(const sp<MediaPlayerBase::AudioSink> &sink) {
Lajos Molnar1d15ab52015-03-04 16:46:34 -0800373 sp<AMessage> msg = new AMessage(kWhatSetAudioSink, this);
Andreas Huberf9334412010-12-15 15:17:42 -0800374 msg->setObject("sink", sink);
375 msg->post();
376}
377
378void NuPlayer::start() {
Lajos Molnar1d15ab52015-03-04 16:46:34 -0800379 (new AMessage(kWhatStart, this))->post();
Andreas Huberf9334412010-12-15 15:17:42 -0800380}
381
Lajos Molnar3a474aa2015-04-24 17:10:07 -0700382status_t NuPlayer::setPlaybackSettings(const AudioPlaybackRate &rate) {
383 // do some cursory validation of the settings here. audio modes are
384 // only validated when set on the audiosink.
385 if ((rate.mSpeed != 0.f && rate.mSpeed < AUDIO_TIMESTRETCH_SPEED_MIN)
386 || rate.mSpeed > AUDIO_TIMESTRETCH_SPEED_MAX
387 || rate.mPitch < AUDIO_TIMESTRETCH_SPEED_MIN
388 || rate.mPitch > AUDIO_TIMESTRETCH_SPEED_MAX) {
389 return BAD_VALUE;
390 }
391 sp<AMessage> msg = new AMessage(kWhatConfigPlayback, this);
392 writeToAMessage(msg, rate);
393 sp<AMessage> response;
394 status_t err = msg->postAndAwaitResponse(&response);
395 if (err == OK && response != NULL) {
396 CHECK(response->findInt32("err", &err));
397 }
398 return err;
399}
400
401status_t NuPlayer::getPlaybackSettings(AudioPlaybackRate *rate /* nonnull */) {
402 sp<AMessage> msg = new AMessage(kWhatGetPlaybackSettings, this);
403 sp<AMessage> response;
404 status_t err = msg->postAndAwaitResponse(&response);
405 if (err == OK && response != NULL) {
406 CHECK(response->findInt32("err", &err));
407 if (err == OK) {
408 readFromAMessage(response, rate);
409 }
410 }
411 return err;
412}
413
414status_t NuPlayer::setSyncSettings(const AVSyncSettings &sync, float videoFpsHint) {
415 sp<AMessage> msg = new AMessage(kWhatConfigSync, this);
416 writeToAMessage(msg, sync, videoFpsHint);
417 sp<AMessage> response;
418 status_t err = msg->postAndAwaitResponse(&response);
419 if (err == OK && response != NULL) {
420 CHECK(response->findInt32("err", &err));
421 }
422 return err;
423}
424
425status_t NuPlayer::getSyncSettings(
426 AVSyncSettings *sync /* nonnull */, float *videoFps /* nonnull */) {
427 sp<AMessage> msg = new AMessage(kWhatGetSyncSettings, this);
428 sp<AMessage> response;
429 status_t err = msg->postAndAwaitResponse(&response);
430 if (err == OK && response != NULL) {
431 CHECK(response->findInt32("err", &err));
432 if (err == OK) {
433 readFromAMessage(response, sync, videoFps);
434 }
435 }
436 return err;
Wei Jia98160162015-02-04 17:01:11 -0800437}
438
Andreas Huber43c3e6c2011-01-05 12:17:08 -0800439void NuPlayer::pause() {
Lajos Molnar1d15ab52015-03-04 16:46:34 -0800440 (new AMessage(kWhatPause, this))->post();
Andreas Huber43c3e6c2011-01-05 12:17:08 -0800441}
442
Andreas Huber1aef2112011-01-04 14:01:29 -0800443void NuPlayer::resetAsync() {
Wei Jiac45a4b22016-04-15 15:30:23 -0700444 sp<Source> source;
445 {
446 Mutex::Autolock autoLock(mSourceLock);
447 source = mSource;
448 }
449
450 if (source != NULL) {
Chong Zhang48296b72014-09-14 14:28:45 -0700451 // During a reset, the data source might be unresponsive already, we need to
452 // disconnect explicitly so that reads exit promptly.
453 // We can't queue the disconnect request to the looper, as it might be
454 // queued behind a stuck read and never gets processed.
455 // Doing a disconnect outside the looper to allows the pending reads to exit
456 // (either successfully or with error).
Wei Jiac45a4b22016-04-15 15:30:23 -0700457 source->disconnect();
Chong Zhang48296b72014-09-14 14:28:45 -0700458 }
459
Lajos Molnar1d15ab52015-03-04 16:46:34 -0800460 (new AMessage(kWhatReset, this))->post();
Andreas Huber1aef2112011-01-04 14:01:29 -0800461}
462
Wei Jiac5de0912016-11-18 10:22:14 -0800463void NuPlayer::seekToAsync(int64_t seekTimeUs, MediaPlayerSeekMode mode, bool needNotify) {
Lajos Molnar1d15ab52015-03-04 16:46:34 -0800464 sp<AMessage> msg = new AMessage(kWhatSeek, this);
Andreas Huber43c3e6c2011-01-05 12:17:08 -0800465 msg->setInt64("seekTimeUs", seekTimeUs);
Wei Jiac5de0912016-11-18 10:22:14 -0800466 msg->setInt32("mode", mode);
Wei Jiae427abf2014-09-22 15:21:11 -0700467 msg->setInt32("needNotify", needNotify);
Andreas Huber43c3e6c2011-01-05 12:17:08 -0800468 msg->post();
469}
470
Andreas Huber53df1a42010-12-22 10:03:04 -0800471
Chong Zhang404fced2014-06-11 14:45:31 -0700472void NuPlayer::writeTrackInfo(
Chih-Hung Hsiehe964d4e2016-08-09 14:31:32 -0700473 Parcel* reply, const sp<AMessage>& format) const {
Marco Nelissenc367ca12015-09-15 09:51:59 -0700474 if (format == NULL) {
475 ALOGE("NULL format");
476 return;
477 }
Chong Zhang404fced2014-06-11 14:45:31 -0700478 int32_t trackType;
Marco Nelissen9436e482015-09-15 10:21:53 -0700479 if (!format->findInt32("type", &trackType)) {
Marco Nelissenc367ca12015-09-15 09:51:59 -0700480 ALOGE("no track type");
481 return;
482 }
Chong Zhang404fced2014-06-11 14:45:31 -0700483
Robert Shih755106e2015-04-30 14:36:45 -0700484 AString mime;
Robert Shih2e3a4252015-05-06 10:21:15 -0700485 if (!format->findString("mime", &mime)) {
486 // Java MediaPlayer only uses mimetype for subtitle and timedtext tracks.
487 // If we can't find the mimetype here it means that we wouldn't be needing
488 // the mimetype on the Java end. We still write a placeholder mime to keep the
489 // (de)serialization logic simple.
490 if (trackType == MEDIA_TRACK_TYPE_AUDIO) {
491 mime = "audio/";
492 } else if (trackType == MEDIA_TRACK_TYPE_VIDEO) {
493 mime = "video/";
494 } else {
Marco Nelissenc367ca12015-09-15 09:51:59 -0700495 ALOGE("unknown track type: %d", trackType);
496 return;
Robert Shih2e3a4252015-05-06 10:21:15 -0700497 }
498 }
Robert Shih755106e2015-04-30 14:36:45 -0700499
Chong Zhang404fced2014-06-11 14:45:31 -0700500 AString lang;
Marco Nelissen9436e482015-09-15 10:21:53 -0700501 if (!format->findString("language", &lang)) {
Marco Nelissenc367ca12015-09-15 09:51:59 -0700502 ALOGE("no language");
503 return;
504 }
Chong Zhang404fced2014-06-11 14:45:31 -0700505
506 reply->writeInt32(2); // write something non-zero
507 reply->writeInt32(trackType);
Robert Shih755106e2015-04-30 14:36:45 -0700508 reply->writeString16(String16(mime.c_str()));
Chong Zhang404fced2014-06-11 14:45:31 -0700509 reply->writeString16(String16(lang.c_str()));
510
511 if (trackType == MEDIA_TRACK_TYPE_SUBTITLE) {
Chong Zhang404fced2014-06-11 14:45:31 -0700512 int32_t isAuto, isDefault, isForced;
513 CHECK(format->findInt32("auto", &isAuto));
514 CHECK(format->findInt32("default", &isDefault));
515 CHECK(format->findInt32("forced", &isForced));
516
Chong Zhang404fced2014-06-11 14:45:31 -0700517 reply->writeInt32(isAuto);
518 reply->writeInt32(isDefault);
519 reply->writeInt32(isForced);
520 }
521}
522
Andreas Huberf9334412010-12-15 15:17:42 -0800523void NuPlayer::onMessageReceived(const sp<AMessage> &msg) {
524 switch (msg->what()) {
525 case kWhatSetDataSource:
526 {
Steve Block3856b092011-10-20 11:56:00 +0100527 ALOGV("kWhatSetDataSource");
Andreas Huberf9334412010-12-15 15:17:42 -0800528
529 CHECK(mSource == NULL);
530
Chong Zhang3de157d2014-08-05 20:54:44 -0700531 status_t err = OK;
Andreas Huber5bc087c2010-12-23 10:27:40 -0800532 sp<RefBase> obj;
533 CHECK(msg->findObject("source", &obj));
Chong Zhang3de157d2014-08-05 20:54:44 -0700534 if (obj != NULL) {
Wei Jiac45a4b22016-04-15 15:30:23 -0700535 Mutex::Autolock autoLock(mSourceLock);
Chong Zhang3de157d2014-08-05 20:54:44 -0700536 mSource = static_cast<Source *>(obj.get());
Chong Zhang3de157d2014-08-05 20:54:44 -0700537 } else {
538 err = UNKNOWN_ERROR;
539 }
Andreas Huber9575c962013-02-05 13:59:56 -0800540
541 CHECK(mDriver != NULL);
542 sp<NuPlayerDriver> driver = mDriver.promote();
543 if (driver != NULL) {
Chong Zhang3de157d2014-08-05 20:54:44 -0700544 driver->notifySetDataSourceCompleted(err);
Andreas Huber9575c962013-02-05 13:59:56 -0800545 }
546 break;
547 }
548
Wei Jia48fa06d2016-12-20 15:30:49 -0800549 case kWhatGetDefaultBufferingSettings:
550 {
551 sp<AReplyToken> replyID;
552 CHECK(msg->senderAwaitsResponse(&replyID));
553
554 ALOGV("kWhatGetDefaultBufferingSettings");
555 BufferingSettings buffering;
556 status_t err = OK;
557 if (mSource != NULL) {
558 err = mSource->getDefaultBufferingSettings(&buffering);
559 } else {
560 err = INVALID_OPERATION;
561 }
562 sp<AMessage> response = new AMessage;
563 if (err == OK) {
564 writeToAMessage(response, buffering);
565 }
566 response->setInt32("err", err);
567 response->postReply(replyID);
568 break;
569 }
570
571 case kWhatSetBufferingSettings:
572 {
573 sp<AReplyToken> replyID;
574 CHECK(msg->senderAwaitsResponse(&replyID));
575
576 ALOGV("kWhatSetBufferingSettings");
577 BufferingSettings buffering;
578 readFromAMessage(msg, &buffering);
579 status_t err = OK;
580 if (mSource != NULL) {
581 err = mSource->setBufferingSettings(buffering);
582 } else {
583 err = INVALID_OPERATION;
584 }
585 sp<AMessage> response = new AMessage;
586 response->setInt32("err", err);
587 response->postReply(replyID);
588 break;
589 }
590
Andreas Huber9575c962013-02-05 13:59:56 -0800591 case kWhatPrepare:
592 {
Hassan Shojaniacefac142017-02-06 21:02:02 -0800593 ALOGV("onMessageReceived kWhatPrepare");
594
Andreas Huber9575c962013-02-05 13:59:56 -0800595 mSource->prepareAsync();
Andreas Huberf9334412010-12-15 15:17:42 -0800596 break;
597 }
598
Chong Zhangdcb89b32013-08-06 09:44:47 -0700599 case kWhatGetTrackInfo:
600 {
Lajos Molnar3f274362015-03-05 14:35:41 -0800601 sp<AReplyToken> replyID;
Chong Zhangdcb89b32013-08-06 09:44:47 -0700602 CHECK(msg->senderAwaitsResponse(&replyID));
603
Chong Zhang404fced2014-06-11 14:45:31 -0700604 Parcel* reply;
605 CHECK(msg->findPointer("reply", (void**)&reply));
606
607 size_t inbandTracks = 0;
Chong Zhangdcb89b32013-08-06 09:44:47 -0700608 if (mSource != NULL) {
Chong Zhang404fced2014-06-11 14:45:31 -0700609 inbandTracks = mSource->getTrackCount();
610 }
611
Chong Zhanga7fa1d92014-06-11 14:49:23 -0700612 size_t ccTracks = 0;
613 if (mCCDecoder != NULL) {
614 ccTracks = mCCDecoder->getTrackCount();
615 }
616
Chong Zhang404fced2014-06-11 14:45:31 -0700617 // total track count
Chong Zhanga7fa1d92014-06-11 14:49:23 -0700618 reply->writeInt32(inbandTracks + ccTracks);
Chong Zhang404fced2014-06-11 14:45:31 -0700619
620 // write inband tracks
621 for (size_t i = 0; i < inbandTracks; ++i) {
622 writeTrackInfo(reply, mSource->getTrackInfo(i));
Chong Zhangdcb89b32013-08-06 09:44:47 -0700623 }
624
Chong Zhanga7fa1d92014-06-11 14:49:23 -0700625 // write CC track
626 for (size_t i = 0; i < ccTracks; ++i) {
627 writeTrackInfo(reply, mCCDecoder->getTrackInfo(i));
628 }
629
Chong Zhangdcb89b32013-08-06 09:44:47 -0700630 sp<AMessage> response = new AMessage;
Chong Zhangdcb89b32013-08-06 09:44:47 -0700631 response->postReply(replyID);
632 break;
633 }
634
Robert Shih7c4f0d72014-07-09 18:53:31 -0700635 case kWhatGetSelectedTrack:
636 {
637 status_t err = INVALID_OPERATION;
638 if (mSource != NULL) {
639 err = OK;
640
641 int32_t type32;
642 CHECK(msg->findInt32("type", (int32_t*)&type32));
643 media_track_type type = (media_track_type)type32;
644 ssize_t selectedTrack = mSource->getSelectedTrack(type);
645
646 Parcel* reply;
647 CHECK(msg->findPointer("reply", (void**)&reply));
648 reply->writeInt32(selectedTrack);
649 }
650
651 sp<AMessage> response = new AMessage;
652 response->setInt32("err", err);
653
Lajos Molnar3f274362015-03-05 14:35:41 -0800654 sp<AReplyToken> replyID;
Robert Shih7c4f0d72014-07-09 18:53:31 -0700655 CHECK(msg->senderAwaitsResponse(&replyID));
656 response->postReply(replyID);
657 break;
658 }
659
Chong Zhangdcb89b32013-08-06 09:44:47 -0700660 case kWhatSelectTrack:
661 {
Lajos Molnar3f274362015-03-05 14:35:41 -0800662 sp<AReplyToken> replyID;
Chong Zhangdcb89b32013-08-06 09:44:47 -0700663 CHECK(msg->senderAwaitsResponse(&replyID));
664
Chong Zhang404fced2014-06-11 14:45:31 -0700665 size_t trackIndex;
666 int32_t select;
Robert Shih6ffb1fd2014-10-29 16:24:32 -0700667 int64_t timeUs;
Chong Zhang404fced2014-06-11 14:45:31 -0700668 CHECK(msg->findSize("trackIndex", &trackIndex));
669 CHECK(msg->findInt32("select", &select));
Robert Shih6ffb1fd2014-10-29 16:24:32 -0700670 CHECK(msg->findInt64("timeUs", &timeUs));
Chong Zhang404fced2014-06-11 14:45:31 -0700671
Chong Zhangdcb89b32013-08-06 09:44:47 -0700672 status_t err = INVALID_OPERATION;
Chong Zhang404fced2014-06-11 14:45:31 -0700673
674 size_t inbandTracks = 0;
Chong Zhangdcb89b32013-08-06 09:44:47 -0700675 if (mSource != NULL) {
Chong Zhang404fced2014-06-11 14:45:31 -0700676 inbandTracks = mSource->getTrackCount();
677 }
Chong Zhanga7fa1d92014-06-11 14:49:23 -0700678 size_t ccTracks = 0;
679 if (mCCDecoder != NULL) {
680 ccTracks = mCCDecoder->getTrackCount();
681 }
Chong Zhang404fced2014-06-11 14:45:31 -0700682
683 if (trackIndex < inbandTracks) {
Robert Shih6ffb1fd2014-10-29 16:24:32 -0700684 err = mSource->selectTrack(trackIndex, select, timeUs);
Robert Shihd3b0bbb2014-07-23 15:00:25 -0700685
686 if (!select && err == OK) {
687 int32_t type;
688 sp<AMessage> info = mSource->getTrackInfo(trackIndex);
689 if (info != NULL
690 && info->findInt32("type", &type)
691 && type == MEDIA_TRACK_TYPE_TIMEDTEXT) {
692 ++mTimedTextGeneration;
693 }
694 }
Chong Zhanga7fa1d92014-06-11 14:49:23 -0700695 } else {
696 trackIndex -= inbandTracks;
697
698 if (trackIndex < ccTracks) {
699 err = mCCDecoder->selectTrack(trackIndex, select);
700 }
Chong Zhangdcb89b32013-08-06 09:44:47 -0700701 }
702
703 sp<AMessage> response = new AMessage;
704 response->setInt32("err", err);
705
706 response->postReply(replyID);
707 break;
708 }
709
Andreas Huberb7c8e912012-11-27 15:02:53 -0800710 case kWhatPollDuration:
711 {
712 int32_t generation;
713 CHECK(msg->findInt32("generation", &generation));
714
715 if (generation != mPollDurationGeneration) {
716 // stale
717 break;
718 }
719
720 int64_t durationUs;
721 if (mDriver != NULL && mSource->getDuration(&durationUs) == OK) {
722 sp<NuPlayerDriver> driver = mDriver.promote();
723 if (driver != NULL) {
724 driver->notifyDuration(durationUs);
725 }
726 }
727
728 msg->post(1000000ll); // poll again in a second.
729 break;
730 }
731
Lajos Molnar1de1e252015-04-30 18:18:34 -0700732 case kWhatSetVideoSurface:
Andreas Huberf9334412010-12-15 15:17:42 -0800733 {
Andreas Huberf9334412010-12-15 15:17:42 -0800734
735 sp<RefBase> obj;
Lajos Molnar1de1e252015-04-30 18:18:34 -0700736 CHECK(msg->findObject("surface", &obj));
737 sp<Surface> surface = static_cast<Surface *>(obj.get());
Lajos Molnara81c6222015-07-10 19:17:45 -0700738
739 ALOGD("onSetVideoSurface(%p, %s video decoder)",
740 surface.get(),
Wei Jia6b7d2ed2015-08-10 15:11:54 -0700741 (mSource != NULL && mStarted && mSource->getFormat(false /* audio */) != NULL
Lajos Molnara81c6222015-07-10 19:17:45 -0700742 && mVideoDecoder != NULL) ? "have" : "no");
743
Wei Jia6b7d2ed2015-08-10 15:11:54 -0700744 // Need to check mStarted before calling mSource->getFormat because NuPlayer might
745 // be in preparing state and it could take long time.
746 // When mStarted is true, mSource must have been set.
747 if (mSource == NULL || !mStarted || mSource->getFormat(false /* audio */) == NULL
Lajos Molnara81c6222015-07-10 19:17:45 -0700748 // NOTE: mVideoDecoder's mSurface is always non-null
749 || (mVideoDecoder != NULL && mVideoDecoder->setVideoSurface(surface) == OK)) {
Lajos Molnar1de1e252015-04-30 18:18:34 -0700750 performSetSurface(surface);
Wei Jiafef808d2014-10-31 17:57:05 -0700751 break;
752 }
753
754 mDeferredActions.push_back(
755 new FlushDecoderAction(FLUSH_CMD_FLUSH /* audio */,
756 FLUSH_CMD_SHUTDOWN /* video */));
757
Lajos Molnar1de1e252015-04-30 18:18:34 -0700758 mDeferredActions.push_back(new SetSurfaceAction(surface));
James Dong0d268a32012-08-31 12:18:27 -0700759
Wei Jiac6e58412015-07-10 18:04:55 -0700760 if (obj != NULL || mAudioDecoder != NULL) {
Wei Jiafef808d2014-10-31 17:57:05 -0700761 if (mStarted) {
Andy Hung73535852014-09-05 11:42:58 -0700762 // Issue a seek to refresh the video screen only if started otherwise
763 // the extractor may not yet be started and will assert.
764 // If the video decoder is not set (perhaps audio only in this case)
765 // do not perform a seek as it is not needed.
Ronghua Wua73d9e02014-10-08 15:13:29 -0700766 int64_t currentPositionUs = 0;
767 if (getCurrentPosition(&currentPositionUs) == OK) {
768 mDeferredActions.push_back(
Wei Jiac5de0912016-11-18 10:22:14 -0800769 new SeekAction(currentPositionUs,
770 MediaPlayerSeekMode::SEEK_PREVIOUS_SYNC /* mode */));
Ronghua Wua73d9e02014-10-08 15:13:29 -0700771 }
Andy Hung73535852014-09-05 11:42:58 -0700772 }
Wei Jiaac428aa2014-09-02 19:01:34 -0700773
Andreas Huber57a339c2012-12-03 11:18:00 -0800774 // If there is a new surface texture, instantiate decoders
775 // again if possible.
776 mDeferredActions.push_back(
777 new SimpleAction(&NuPlayer::performScanSources));
778 }
779
Chong Zhangf8d71772014-11-26 15:08:34 -0800780 // After a flush without shutdown, decoder is paused.
781 // Don't resume it until source seek is done, otherwise it could
Chong Zhang7137ec72014-11-12 16:41:05 -0800782 // start pulling stale data too soon.
783 mDeferredActions.push_back(
Chong Zhangf8d71772014-11-26 15:08:34 -0800784 new ResumeDecoderAction(false /* needNotify */));
Chong Zhang7137ec72014-11-12 16:41:05 -0800785
Andreas Huber57a339c2012-12-03 11:18:00 -0800786 processDeferredActions();
Andreas Huberf9334412010-12-15 15:17:42 -0800787 break;
788 }
789
790 case kWhatSetAudioSink:
791 {
Steve Block3856b092011-10-20 11:56:00 +0100792 ALOGV("kWhatSetAudioSink");
Andreas Huberf9334412010-12-15 15:17:42 -0800793
794 sp<RefBase> obj;
795 CHECK(msg->findObject("sink", &obj));
796
797 mAudioSink = static_cast<MediaPlayerBase::AudioSink *>(obj.get());
798 break;
799 }
800
801 case kWhatStart:
802 {
Steve Block3856b092011-10-20 11:56:00 +0100803 ALOGV("kWhatStart");
Wei Jia94211742014-10-28 17:09:06 -0700804 if (mStarted) {
Chong Zhang8a048332015-05-06 15:16:28 -0700805 // do not resume yet if the source is still buffering
806 if (!mPausedForBuffering) {
807 onResume();
808 }
Wei Jia94211742014-10-28 17:09:06 -0700809 } else {
810 onStart();
Lajos Molnar09524832014-07-17 14:29:51 -0700811 }
Chong Zhangefbb6192015-01-30 17:13:27 -0800812 mPausedByClient = false;
Andreas Huberf9334412010-12-15 15:17:42 -0800813 break;
814 }
815
Lajos Molnar3a474aa2015-04-24 17:10:07 -0700816 case kWhatConfigPlayback:
Wei Jia98160162015-02-04 17:01:11 -0800817 {
Lajos Molnar3a474aa2015-04-24 17:10:07 -0700818 sp<AReplyToken> replyID;
819 CHECK(msg->senderAwaitsResponse(&replyID));
820 AudioPlaybackRate rate /* sanitized */;
821 readFromAMessage(msg, &rate);
822 status_t err = OK;
Wei Jia98160162015-02-04 17:01:11 -0800823 if (mRenderer != NULL) {
Wei Jiabf70feb2016-02-19 15:47:16 -0800824 // AudioSink allows only 1.f and 0.f for offload mode.
825 // For other speed, switch to non-offload mode.
826 if (mOffloadAudio && ((rate.mSpeed != 0.f && rate.mSpeed != 1.f)
827 || rate.mPitch != 1.f)) {
828 int64_t currentPositionUs;
829 if (getCurrentPosition(&currentPositionUs) != OK) {
830 currentPositionUs = mPreviousSeekTimeUs;
831 }
832
833 // Set mPlaybackSettings so that the new audio decoder can
834 // be created correctly.
835 mPlaybackSettings = rate;
836 if (!mPaused) {
837 mRenderer->pause();
838 }
Wei Jia5031b2f2016-02-25 11:19:31 -0800839 restartAudio(
Wei Jiabf70feb2016-02-19 15:47:16 -0800840 currentPositionUs, true /* forceNonOffload */,
841 true /* needsToCreateAudioDecoder */);
842 if (!mPaused) {
843 mRenderer->resume();
844 }
845 }
846
Lajos Molnar3a474aa2015-04-24 17:10:07 -0700847 err = mRenderer->setPlaybackSettings(rate);
848 }
849 if (err == OK) {
850 if (rate.mSpeed == 0.f) {
851 onPause();
Wei Jia351ce872016-02-10 13:21:59 -0800852 mPausedByClient = true;
Lajos Molnar3a474aa2015-04-24 17:10:07 -0700853 // save all other settings (using non-paused speed)
854 // so we can restore them on start
855 AudioPlaybackRate newRate = rate;
856 newRate.mSpeed = mPlaybackSettings.mSpeed;
857 mPlaybackSettings = newRate;
858 } else { /* rate.mSpeed != 0.f */
Lajos Molnar3a474aa2015-04-24 17:10:07 -0700859 mPlaybackSettings = rate;
Wei Jia8a092d32016-06-03 14:57:24 -0700860 if (mStarted) {
861 // do not resume yet if the source is still buffering
862 if (!mPausedForBuffering) {
863 onResume();
864 }
865 } else if (mPrepared) {
866 onStart();
867 }
868
869 mPausedByClient = false;
Lajos Molnar3a474aa2015-04-24 17:10:07 -0700870 }
Wei Jia98160162015-02-04 17:01:11 -0800871 }
Ronghua Wu8db88132015-04-22 13:51:35 -0700872
873 if (mVideoDecoder != NULL) {
Praveen Chavanbbaa1442016-04-08 13:33:49 -0700874 sp<AMessage> params = new AMessage();
875 params->setFloat("playback-speed", mPlaybackSettings.mSpeed);
876 mVideoDecoder->setParameters(params);
Ronghua Wu8db88132015-04-22 13:51:35 -0700877 }
878
Lajos Molnar3a474aa2015-04-24 17:10:07 -0700879 sp<AMessage> response = new AMessage;
880 response->setInt32("err", err);
881 response->postReply(replyID);
882 break;
883 }
884
885 case kWhatGetPlaybackSettings:
886 {
887 sp<AReplyToken> replyID;
888 CHECK(msg->senderAwaitsResponse(&replyID));
889 AudioPlaybackRate rate = mPlaybackSettings;
890 status_t err = OK;
891 if (mRenderer != NULL) {
892 err = mRenderer->getPlaybackSettings(&rate);
893 }
894 if (err == OK) {
895 // get playback settings used by renderer, as it may be
896 // slightly off due to audiosink not taking small changes.
897 mPlaybackSettings = rate;
898 if (mPaused) {
899 rate.mSpeed = 0.f;
900 }
901 }
902 sp<AMessage> response = new AMessage;
903 if (err == OK) {
904 writeToAMessage(response, rate);
905 }
906 response->setInt32("err", err);
907 response->postReply(replyID);
908 break;
909 }
910
911 case kWhatConfigSync:
912 {
913 sp<AReplyToken> replyID;
914 CHECK(msg->senderAwaitsResponse(&replyID));
915
916 ALOGV("kWhatConfigSync");
917 AVSyncSettings sync;
918 float videoFpsHint;
919 readFromAMessage(msg, &sync, &videoFpsHint);
920 status_t err = OK;
921 if (mRenderer != NULL) {
922 err = mRenderer->setSyncSettings(sync, videoFpsHint);
923 }
924 if (err == OK) {
925 mSyncSettings = sync;
926 mVideoFpsHint = videoFpsHint;
927 }
928 sp<AMessage> response = new AMessage;
929 response->setInt32("err", err);
930 response->postReply(replyID);
931 break;
932 }
933
934 case kWhatGetSyncSettings:
935 {
936 sp<AReplyToken> replyID;
937 CHECK(msg->senderAwaitsResponse(&replyID));
938 AVSyncSettings sync = mSyncSettings;
939 float videoFps = mVideoFpsHint;
940 status_t err = OK;
941 if (mRenderer != NULL) {
942 err = mRenderer->getSyncSettings(&sync, &videoFps);
943 if (err == OK) {
944 mSyncSettings = sync;
945 mVideoFpsHint = videoFps;
946 }
947 }
948 sp<AMessage> response = new AMessage;
949 if (err == OK) {
950 writeToAMessage(response, sync, videoFps);
951 }
952 response->setInt32("err", err);
953 response->postReply(replyID);
Wei Jia98160162015-02-04 17:01:11 -0800954 break;
955 }
956
Andreas Huberf9334412010-12-15 15:17:42 -0800957 case kWhatScanSources:
958 {
Andreas Huber1aef2112011-01-04 14:01:29 -0800959 int32_t generation;
960 CHECK(msg->findInt32("generation", &generation));
961 if (generation != mScanSourcesGeneration) {
962 // Drop obsolete msg.
963 break;
964 }
965
Andreas Huber5bc087c2010-12-23 10:27:40 -0800966 mScanSourcesPending = false;
967
Steve Block3856b092011-10-20 11:56:00 +0100968 ALOGV("scanning sources haveAudio=%d, haveVideo=%d",
Andreas Huber43c3e6c2011-01-05 12:17:08 -0800969 mAudioDecoder != NULL, mVideoDecoder != NULL);
970
Andreas Huberb7c8e912012-11-27 15:02:53 -0800971 bool mHadAnySourcesBefore =
972 (mAudioDecoder != NULL) || (mVideoDecoder != NULL);
Robert Shih7350b052015-10-01 15:50:14 -0700973 bool rescan = false;
Andreas Huberb7c8e912012-11-27 15:02:53 -0800974
Andy Hung282a7e32014-08-14 15:56:34 -0700975 // initialize video before audio because successful initialization of
976 // video may change deep buffer mode of audio.
Lajos Molnar1de1e252015-04-30 18:18:34 -0700977 if (mSurface != NULL) {
Robert Shih7350b052015-10-01 15:50:14 -0700978 if (instantiateDecoder(false, &mVideoDecoder) == -EWOULDBLOCK) {
979 rescan = true;
980 }
Haynes Mathew George5d246ef2012-07-09 10:36:57 -0700981 }
Andreas Huberf9334412010-12-15 15:17:42 -0800982
Ronghua Wua10fd232014-11-06 16:15:20 -0800983 // Don't try to re-open audio sink if there's an existing decoder.
984 if (mAudioSink != NULL && mAudioDecoder == NULL) {
Robert Shih7350b052015-10-01 15:50:14 -0700985 if (instantiateDecoder(true, &mAudioDecoder) == -EWOULDBLOCK) {
986 rescan = true;
987 }
Andreas Huberf9334412010-12-15 15:17:42 -0800988 }
989
Andreas Huberb7c8e912012-11-27 15:02:53 -0800990 if (!mHadAnySourcesBefore
991 && (mAudioDecoder != NULL || mVideoDecoder != NULL)) {
992 // This is the first time we've found anything playable.
993
Andreas Huber9575c962013-02-05 13:59:56 -0800994 if (mSourceFlags & Source::FLAG_DYNAMIC_DURATION) {
Andreas Huberb7c8e912012-11-27 15:02:53 -0800995 schedulePollDuration();
996 }
997 }
998
Andreas Hubereac68ba2011-09-27 12:12:25 -0700999 status_t err;
1000 if ((err = mSource->feedMoreTSData()) != OK) {
Andreas Huber1aef2112011-01-04 14:01:29 -08001001 if (mAudioDecoder == NULL && mVideoDecoder == NULL) {
1002 // We're not currently decoding anything (no audio or
1003 // video tracks found) and we just ran out of input data.
Andreas Hubereac68ba2011-09-27 12:12:25 -07001004
1005 if (err == ERROR_END_OF_STREAM) {
1006 notifyListener(MEDIA_PLAYBACK_COMPLETE, 0, 0);
1007 } else {
1008 notifyListener(MEDIA_ERROR, MEDIA_ERROR_UNKNOWN, err);
1009 }
Andreas Huber1aef2112011-01-04 14:01:29 -08001010 }
Andreas Huberf9334412010-12-15 15:17:42 -08001011 break;
1012 }
1013
Robert Shih7350b052015-10-01 15:50:14 -07001014 if (rescan) {
Andreas Huberf9334412010-12-15 15:17:42 -08001015 msg->post(100000ll);
Andreas Huber5bc087c2010-12-23 10:27:40 -08001016 mScanSourcesPending = true;
Andreas Huberf9334412010-12-15 15:17:42 -08001017 }
1018 break;
1019 }
1020
1021 case kWhatVideoNotify:
1022 case kWhatAudioNotify:
1023 {
1024 bool audio = msg->what() == kWhatAudioNotify;
1025
Wei Jia88703c32014-08-06 11:24:07 -07001026 int32_t currentDecoderGeneration =
1027 (audio? mAudioDecoderGeneration : mVideoDecoderGeneration);
1028 int32_t requesterGeneration = currentDecoderGeneration - 1;
1029 CHECK(msg->findInt32("generation", &requesterGeneration));
1030
1031 if (requesterGeneration != currentDecoderGeneration) {
1032 ALOGV("got message from old %s decoder, generation(%d:%d)",
1033 audio ? "audio" : "video", requesterGeneration,
1034 currentDecoderGeneration);
1035 sp<AMessage> reply;
1036 if (!(msg->findMessage("reply", &reply))) {
1037 return;
1038 }
1039
1040 reply->setInt32("err", INFO_DISCONTINUITY);
1041 reply->post();
1042 return;
1043 }
1044
Andreas Huberf9334412010-12-15 15:17:42 -08001045 int32_t what;
Lajos Molnar1cd13982014-01-17 15:12:51 -08001046 CHECK(msg->findInt32("what", &what));
Andreas Huberf9334412010-12-15 15:17:42 -08001047
Chong Zhang7137ec72014-11-12 16:41:05 -08001048 if (what == DecoderBase::kWhatInputDiscontinuity) {
1049 int32_t formatChange;
1050 CHECK(msg->findInt32("formatChange", &formatChange));
Andreas Huberf9334412010-12-15 15:17:42 -08001051
Chong Zhang7137ec72014-11-12 16:41:05 -08001052 ALOGV("%s discontinuity: formatChange %d",
1053 audio ? "audio" : "video", formatChange);
1054
1055 if (formatChange) {
1056 mDeferredActions.push_back(
1057 new FlushDecoderAction(
1058 audio ? FLUSH_CMD_SHUTDOWN : FLUSH_CMD_NONE,
1059 audio ? FLUSH_CMD_NONE : FLUSH_CMD_SHUTDOWN));
Andreas Huberf9334412010-12-15 15:17:42 -08001060 }
Chong Zhang7137ec72014-11-12 16:41:05 -08001061
1062 mDeferredActions.push_back(
1063 new SimpleAction(
1064 &NuPlayer::performScanSources));
1065
1066 processDeferredActions();
1067 } else if (what == DecoderBase::kWhatEOS) {
Andreas Huberdc9bacd2011-09-26 10:53:29 -07001068 int32_t err;
Lajos Molnar1cd13982014-01-17 15:12:51 -08001069 CHECK(msg->findInt32("err", &err));
Andreas Huberdc9bacd2011-09-26 10:53:29 -07001070
1071 if (err == ERROR_END_OF_STREAM) {
Steve Block3856b092011-10-20 11:56:00 +01001072 ALOGV("got %s decoder EOS", audio ? "audio" : "video");
Andreas Huberdc9bacd2011-09-26 10:53:29 -07001073 } else {
Steve Block3856b092011-10-20 11:56:00 +01001074 ALOGV("got %s decoder EOS w/ error %d",
Andreas Huberdc9bacd2011-09-26 10:53:29 -07001075 audio ? "audio" : "video",
1076 err);
1077 }
1078
1079 mRenderer->queueEOS(audio, err);
Chong Zhang7137ec72014-11-12 16:41:05 -08001080 } else if (what == DecoderBase::kWhatFlushCompleted) {
Steve Block3856b092011-10-20 11:56:00 +01001081 ALOGV("decoder %s flush completed", audio ? "audio" : "video");
Andreas Huberf9334412010-12-15 15:17:42 -08001082
Andy Hung8d121d42014-10-03 09:53:53 -07001083 handleFlushComplete(audio, true /* isDecoder */);
Andreas Huber3831a062010-12-21 10:22:33 -08001084 finishFlushIfPossible();
Chong Zhang7137ec72014-11-12 16:41:05 -08001085 } else if (what == DecoderBase::kWhatVideoSizeChanged) {
Lajos Molnar1cd13982014-01-17 15:12:51 -08001086 sp<AMessage> format;
1087 CHECK(msg->findMessage("format", &format));
1088
Wei Jiac6cfd702014-11-11 16:33:20 -08001089 sp<AMessage> inputFormat =
1090 mSource->getFormat(false /* audio */);
Andreas Huber3831a062010-12-21 10:22:33 -08001091
Bartosz Dolewski26936f72014-12-11 12:47:08 +01001092 setVideoScalingMode(mVideoScalingMode);
Wei Jiac6cfd702014-11-11 16:33:20 -08001093 updateVideoSize(inputFormat, format);
Chong Zhang7137ec72014-11-12 16:41:05 -08001094 } else if (what == DecoderBase::kWhatShutdownCompleted) {
Steve Block3856b092011-10-20 11:56:00 +01001095 ALOGV("%s shutdown completed", audio ? "audio" : "video");
Andreas Huber3831a062010-12-21 10:22:33 -08001096 if (audio) {
1097 mAudioDecoder.clear();
Wei Jia686e8e52017-04-03 14:08:01 -07001098 mAudioDecoderError = false;
Wei Jia57568df2014-09-22 10:16:29 -07001099 ++mAudioDecoderGeneration;
Andreas Huber3831a062010-12-21 10:22:33 -08001100
1101 CHECK_EQ((int)mFlushingAudio, (int)SHUTTING_DOWN_DECODER);
1102 mFlushingAudio = SHUT_DOWN;
1103 } else {
1104 mVideoDecoder.clear();
Wei Jia686e8e52017-04-03 14:08:01 -07001105 mVideoDecoderError = false;
Wei Jia57568df2014-09-22 10:16:29 -07001106 ++mVideoDecoderGeneration;
Andreas Huber3831a062010-12-21 10:22:33 -08001107
1108 CHECK_EQ((int)mFlushingVideo, (int)SHUTTING_DOWN_DECODER);
1109 mFlushingVideo = SHUT_DOWN;
1110 }
1111
1112 finishFlushIfPossible();
Chong Zhangf8d71772014-11-26 15:08:34 -08001113 } else if (what == DecoderBase::kWhatResumeCompleted) {
1114 finishResume();
Chong Zhang7137ec72014-11-12 16:41:05 -08001115 } else if (what == DecoderBase::kWhatError) {
Chong Zhangf4c0a942014-08-11 15:14:10 -07001116 status_t err;
Andy Hung2abde2c2014-09-30 14:40:32 -07001117 if (!msg->findInt32("err", &err) || err == OK) {
Chong Zhangf4c0a942014-08-11 15:14:10 -07001118 err = UNKNOWN_ERROR;
1119 }
Andy Hungcf31f1e2014-09-23 14:59:01 -07001120
Andy Hung2abde2c2014-09-30 14:40:32 -07001121 // Decoder errors can be due to Source (e.g. from streaming),
1122 // or from decoding corrupted bitstreams, or from other decoder
1123 // MediaCodec operations (e.g. from an ongoing reset or seek).
Andy Hung202bce12014-12-03 11:47:36 -08001124 // They may also be due to openAudioSink failure at
1125 // decoder start or after a format change.
Andy Hung2abde2c2014-09-30 14:40:32 -07001126 //
1127 // We try to gracefully shut down the affected decoder if possible,
1128 // rather than trying to force the shutdown with something
1129 // similar to performReset(). This method can lead to a hang
1130 // if MediaCodec functions block after an error, but they should
1131 // typically return INVALID_OPERATION instead of blocking.
1132
1133 FlushStatus *flushing = audio ? &mFlushingAudio : &mFlushingVideo;
1134 ALOGE("received error(%#x) from %s decoder, flushing(%d), now shutting down",
1135 err, audio ? "audio" : "video", *flushing);
1136
1137 switch (*flushing) {
1138 case NONE:
1139 mDeferredActions.push_back(
Wei Jiafef808d2014-10-31 17:57:05 -07001140 new FlushDecoderAction(
1141 audio ? FLUSH_CMD_SHUTDOWN : FLUSH_CMD_NONE,
1142 audio ? FLUSH_CMD_NONE : FLUSH_CMD_SHUTDOWN));
Andy Hung2abde2c2014-09-30 14:40:32 -07001143 processDeferredActions();
1144 break;
1145 case FLUSHING_DECODER:
1146 *flushing = FLUSHING_DECODER_SHUTDOWN; // initiate shutdown after flush.
1147 break; // Wait for flush to complete.
1148 case FLUSHING_DECODER_SHUTDOWN:
1149 break; // Wait for flush to complete.
1150 case SHUTTING_DOWN_DECODER:
1151 break; // Wait for shutdown to complete.
1152 case FLUSHED:
Andy Hung2abde2c2014-09-30 14:40:32 -07001153 getDecoder(audio)->initiateShutdown(); // In the middle of a seek.
1154 *flushing = SHUTTING_DOWN_DECODER; // Shut down.
1155 break;
1156 case SHUT_DOWN:
1157 finishFlushIfPossible(); // Should not occur.
1158 break; // Finish anyways.
Marco Nelissen9e2b7912014-08-18 16:13:03 -07001159 }
Wei Jia686e8e52017-04-03 14:08:01 -07001160 if (mSource != nullptr) {
1161 if (audio) {
1162 if (mVideoDecoderError || mSource->getFormat(false /* audio */) == NULL) {
1163 // When both audio and video have error, or this stream has only audio
1164 // which has error, notify client of error.
1165 notifyListener(MEDIA_ERROR, MEDIA_ERROR_UNKNOWN, err);
1166 } else {
1167 // Only audio track has error. Video track could be still good to play.
1168 notifyListener(MEDIA_INFO, MEDIA_INFO_PLAY_AUDIO_ERROR, err);
1169 }
1170 mAudioDecoderError = true;
1171 } else {
1172 if (mAudioDecoderError || mSource->getFormat(true /* audio */) == NULL) {
1173 // When both audio and video have error, or this stream has only video
1174 // which has error, notify client of error.
1175 notifyListener(MEDIA_ERROR, MEDIA_ERROR_UNKNOWN, err);
1176 } else {
1177 // Only video track has error. Audio track could be still good to play.
1178 notifyListener(MEDIA_INFO, MEDIA_INFO_PLAY_VIDEO_ERROR, err);
1179 }
1180 mVideoDecoderError = true;
1181 }
1182 }
Lajos Molnar1cd13982014-01-17 15:12:51 -08001183 } else {
1184 ALOGV("Unhandled decoder notification %d '%c%c%c%c'.",
Andreas Hubera1f8ab02012-11-30 10:53:22 -08001185 what,
1186 what >> 24,
1187 (what >> 16) & 0xff,
1188 (what >> 8) & 0xff,
1189 what & 0xff);
Andreas Huberf9334412010-12-15 15:17:42 -08001190 }
1191
1192 break;
1193 }
1194
1195 case kWhatRendererNotify:
1196 {
Wei Jia57568df2014-09-22 10:16:29 -07001197 int32_t requesterGeneration = mRendererGeneration - 1;
1198 CHECK(msg->findInt32("generation", &requesterGeneration));
1199 if (requesterGeneration != mRendererGeneration) {
1200 ALOGV("got message from old renderer, generation(%d:%d)",
1201 requesterGeneration, mRendererGeneration);
1202 return;
1203 }
1204
Andreas Huberf9334412010-12-15 15:17:42 -08001205 int32_t what;
1206 CHECK(msg->findInt32("what", &what));
1207
1208 if (what == Renderer::kWhatEOS) {
1209 int32_t audio;
1210 CHECK(msg->findInt32("audio", &audio));
1211
Andreas Huberc92fd242011-08-16 13:48:44 -07001212 int32_t finalResult;
1213 CHECK(msg->findInt32("finalResult", &finalResult));
1214
Andreas Huberf9334412010-12-15 15:17:42 -08001215 if (audio) {
1216 mAudioEOS = true;
1217 } else {
1218 mVideoEOS = true;
1219 }
1220
Andreas Huberc92fd242011-08-16 13:48:44 -07001221 if (finalResult == ERROR_END_OF_STREAM) {
Steve Block3856b092011-10-20 11:56:00 +01001222 ALOGV("reached %s EOS", audio ? "audio" : "video");
Andreas Huberc92fd242011-08-16 13:48:44 -07001223 } else {
Steve Block29357bc2012-01-06 19:20:56 +00001224 ALOGE("%s track encountered an error (%d)",
Andreas Huberc92fd242011-08-16 13:48:44 -07001225 audio ? "audio" : "video", finalResult);
1226
1227 notifyListener(
1228 MEDIA_ERROR, MEDIA_ERROR_UNKNOWN, finalResult);
1229 }
Andreas Huberf9334412010-12-15 15:17:42 -08001230
1231 if ((mAudioEOS || mAudioDecoder == NULL)
1232 && (mVideoEOS || mVideoDecoder == NULL)) {
1233 notifyListener(MEDIA_PLAYBACK_COMPLETE, 0, 0);
1234 }
Andreas Huber3fe62152011-09-16 15:09:22 -07001235 } else if (what == Renderer::kWhatFlushComplete) {
Andreas Huberf9334412010-12-15 15:17:42 -08001236 int32_t audio;
1237 CHECK(msg->findInt32("audio", &audio));
1238
Wei Jia3261f0d2015-10-08 13:58:33 -07001239 if (audio) {
1240 mAudioEOS = false;
1241 } else {
1242 mVideoEOS = false;
1243 }
1244
Steve Block3856b092011-10-20 11:56:00 +01001245 ALOGV("renderer %s flush completed.", audio ? "audio" : "video");
Wei Jiada4252f2015-07-14 18:15:28 -07001246 if (audio && (mFlushingAudio == NONE || mFlushingAudio == FLUSHED
1247 || mFlushingAudio == SHUT_DOWN)) {
1248 // Flush has been handled by tear down.
1249 break;
1250 }
Andy Hung8d121d42014-10-03 09:53:53 -07001251 handleFlushComplete(audio, false /* isDecoder */);
1252 finishFlushIfPossible();
James Dongf57b4ea2012-07-20 13:38:36 -07001253 } else if (what == Renderer::kWhatVideoRenderingStart) {
1254 notifyListener(MEDIA_INFO, MEDIA_INFO_RENDERING_START, 0);
Lajos Molnarcbaffcf2013-08-14 18:30:38 -07001255 } else if (what == Renderer::kWhatMediaRenderingStart) {
1256 ALOGV("media rendering started");
1257 notifyListener(MEDIA_STARTED, 0, 0);
Ronghua Wufaeb0f22015-05-21 12:20:21 -07001258 } else if (what == Renderer::kWhatAudioTearDown) {
Ronghua Wu08529172014-10-02 16:55:52 -07001259 int32_t reason;
1260 CHECK(msg->findInt32("reason", &reason));
Ronghua Wufaeb0f22015-05-21 12:20:21 -07001261 ALOGV("Tear down audio with reason %d.", reason);
Wei Jia8456ddd2016-04-22 14:46:43 -07001262 if (reason == Renderer::kDueToTimeout && !(mPaused && mOffloadAudio)) {
1263 // TimeoutWhenPaused is only for offload mode.
1264 ALOGW("Receive a stale message for teardown.");
1265 break;
1266 }
Wei Jiada4252f2015-07-14 18:15:28 -07001267 int64_t positionUs;
Robert Shih1a5c8592015-08-04 18:07:44 -07001268 if (!msg->findInt64("positionUs", &positionUs)) {
1269 positionUs = mPreviousSeekTimeUs;
1270 }
Ronghua Wufaeb0f22015-05-21 12:20:21 -07001271
Wei Jia5031b2f2016-02-25 11:19:31 -08001272 restartAudio(
Wei Jiaa05f1e32016-03-25 16:31:22 -07001273 positionUs, reason == Renderer::kForceNonOffload /* forceNonOffload */,
1274 reason != Renderer::kDueToTimeout /* needsToCreateAudioDecoder */);
Andreas Huberf9334412010-12-15 15:17:42 -08001275 }
1276 break;
1277 }
1278
1279 case kWhatMoreDataQueued:
1280 {
1281 break;
1282 }
1283
Andreas Huber1aef2112011-01-04 14:01:29 -08001284 case kWhatReset:
1285 {
Steve Block3856b092011-10-20 11:56:00 +01001286 ALOGV("kWhatReset");
Andreas Huber1aef2112011-01-04 14:01:29 -08001287
Ronghua Wu64c2d172015-10-07 16:52:19 -07001288 mResetting = true;
1289
Andreas Hubera1f8ab02012-11-30 10:53:22 -08001290 mDeferredActions.push_back(
Wei Jiafef808d2014-10-31 17:57:05 -07001291 new FlushDecoderAction(
1292 FLUSH_CMD_SHUTDOWN /* audio */,
1293 FLUSH_CMD_SHUTDOWN /* video */));
Andreas Huberb7c8e912012-11-27 15:02:53 -08001294
Andreas Hubera1f8ab02012-11-30 10:53:22 -08001295 mDeferredActions.push_back(
1296 new SimpleAction(&NuPlayer::performReset));
Andreas Huberb58ce9f2011-11-28 16:27:35 -08001297
Andreas Hubera1f8ab02012-11-30 10:53:22 -08001298 processDeferredActions();
Andreas Huber1aef2112011-01-04 14:01:29 -08001299 break;
1300 }
1301
Andreas Huber43c3e6c2011-01-05 12:17:08 -08001302 case kWhatSeek:
1303 {
1304 int64_t seekTimeUs;
Wei Jiac5de0912016-11-18 10:22:14 -08001305 int32_t mode;
Wei Jiae427abf2014-09-22 15:21:11 -07001306 int32_t needNotify;
Andreas Huber43c3e6c2011-01-05 12:17:08 -08001307 CHECK(msg->findInt64("seekTimeUs", &seekTimeUs));
Wei Jiac5de0912016-11-18 10:22:14 -08001308 CHECK(msg->findInt32("mode", &mode));
Wei Jiae427abf2014-09-22 15:21:11 -07001309 CHECK(msg->findInt32("needNotify", &needNotify));
Andreas Huber43c3e6c2011-01-05 12:17:08 -08001310
Wei Jiac5de0912016-11-18 10:22:14 -08001311 ALOGV("kWhatSeek seekTimeUs=%lld us, mode=%d, needNotify=%d",
1312 (long long)seekTimeUs, mode, needNotify);
Andreas Huber43c3e6c2011-01-05 12:17:08 -08001313
Wei Jia1061c9c2015-05-19 16:02:17 -07001314 if (!mStarted) {
1315 // Seek before the player is started. In order to preview video,
1316 // need to start the player and pause it. This branch is called
1317 // only once if needed. After the player is started, any seek
1318 // operation will go through normal path.
Robert Shih0c61a0d2015-07-06 15:09:10 -07001319 // Audio-only cases are handled separately.
Wei Jiac5de0912016-11-18 10:22:14 -08001320 onStart(seekTimeUs, (MediaPlayerSeekMode)mode);
Robert Shih0c61a0d2015-07-06 15:09:10 -07001321 if (mStarted) {
1322 onPause();
1323 mPausedByClient = true;
1324 }
Wei Jia1061c9c2015-05-19 16:02:17 -07001325 if (needNotify) {
1326 notifyDriverSeekComplete();
1327 }
1328 break;
1329 }
1330
Andreas Hubera1f8ab02012-11-30 10:53:22 -08001331 mDeferredActions.push_back(
Wei Jiafef808d2014-10-31 17:57:05 -07001332 new FlushDecoderAction(FLUSH_CMD_FLUSH /* audio */,
1333 FLUSH_CMD_FLUSH /* video */));
Andreas Huber43c3e6c2011-01-05 12:17:08 -08001334
Wei Jiae427abf2014-09-22 15:21:11 -07001335 mDeferredActions.push_back(
Wei Jiac5de0912016-11-18 10:22:14 -08001336 new SeekAction(seekTimeUs, (MediaPlayerSeekMode)mode));
Andreas Huber43c3e6c2011-01-05 12:17:08 -08001337
Chong Zhangf8d71772014-11-26 15:08:34 -08001338 // After a flush without shutdown, decoder is paused.
1339 // Don't resume it until source seek is done, otherwise it could
Chong Zhang7137ec72014-11-12 16:41:05 -08001340 // start pulling stale data too soon.
1341 mDeferredActions.push_back(
Chong Zhangf8d71772014-11-26 15:08:34 -08001342 new ResumeDecoderAction(needNotify));
Chong Zhang7137ec72014-11-12 16:41:05 -08001343
Andreas Hubera1f8ab02012-11-30 10:53:22 -08001344 processDeferredActions();
Andreas Huber43c3e6c2011-01-05 12:17:08 -08001345 break;
1346 }
1347
Andreas Huberb4082222011-01-20 15:23:04 -08001348 case kWhatPause:
1349 {
Chong Zhangefbb6192015-01-30 17:13:27 -08001350 onPause();
1351 mPausedByClient = true;
Andreas Huberb4082222011-01-20 15:23:04 -08001352 break;
1353 }
1354
Andreas Huberb5f25f02013-02-05 10:14:26 -08001355 case kWhatSourceNotify:
1356 {
Andreas Huber9575c962013-02-05 13:59:56 -08001357 onSourceNotify(msg);
Andreas Huberb5f25f02013-02-05 10:14:26 -08001358 break;
1359 }
1360
Chong Zhanga7fa1d92014-06-11 14:49:23 -07001361 case kWhatClosedCaptionNotify:
1362 {
1363 onClosedCaptionNotify(msg);
1364 break;
1365 }
1366
Hassan Shojaniacefac142017-02-06 21:02:02 -08001367 case kWhatPrepareDrm:
1368 {
1369 status_t status = onPrepareDrm(msg);
1370
1371 sp<AMessage> response = new AMessage;
1372 response->setInt32("status", status);
1373 sp<AReplyToken> replyID;
1374 CHECK(msg->senderAwaitsResponse(&replyID));
1375 response->postReply(replyID);
1376 break;
1377 }
1378
1379 case kWhatReleaseDrm:
1380 {
1381 status_t status = onReleaseDrm();
1382
1383 sp<AMessage> response = new AMessage;
1384 response->setInt32("status", status);
1385 sp<AReplyToken> replyID;
1386 CHECK(msg->senderAwaitsResponse(&replyID));
1387 response->postReply(replyID);
1388 break;
1389 }
1390
Andreas Huberf9334412010-12-15 15:17:42 -08001391 default:
1392 TRESPASS();
1393 break;
1394 }
1395}
1396
Wei Jia94211742014-10-28 17:09:06 -07001397void NuPlayer::onResume() {
Ronghua Wu64c2d172015-10-07 16:52:19 -07001398 if (!mPaused || mResetting) {
1399 ALOGD_IF(mResetting, "resetting, onResume discarded");
Chong Zhangefbb6192015-01-30 17:13:27 -08001400 return;
1401 }
1402 mPaused = false;
Wei Jia94211742014-10-28 17:09:06 -07001403 if (mSource != NULL) {
1404 mSource->resume();
1405 } else {
1406 ALOGW("resume called when source is gone or not set");
1407 }
1408 // |mAudioDecoder| may have been released due to the pause timeout, so re-create it if
1409 // needed.
1410 if (audioDecoderStillNeeded() && mAudioDecoder == NULL) {
1411 instantiateDecoder(true /* audio */, &mAudioDecoder);
1412 }
1413 if (mRenderer != NULL) {
1414 mRenderer->resume();
1415 } else {
1416 ALOGW("resume called when renderer is gone or not set");
1417 }
Ray Essickd4d00612017-01-03 09:36:27 -08001418
1419 mLastStartedPlayingTimeNs = systemTime();
Wei Jia94211742014-10-28 17:09:06 -07001420}
1421
Lajos Molnarfcd3e942015-03-31 10:06:48 -07001422status_t NuPlayer::onInstantiateSecureDecoders() {
1423 status_t err;
1424 if (!(mSourceFlags & Source::FLAG_SECURE)) {
1425 return BAD_TYPE;
1426 }
1427
1428 if (mRenderer != NULL) {
1429 ALOGE("renderer should not be set when instantiating secure decoders");
1430 return UNKNOWN_ERROR;
1431 }
1432
1433 // TRICKY: We rely on mRenderer being null, so that decoder does not start requesting
1434 // data on instantiation.
Lajos Molnar1de1e252015-04-30 18:18:34 -07001435 if (mSurface != NULL) {
Lajos Molnarfcd3e942015-03-31 10:06:48 -07001436 err = instantiateDecoder(false, &mVideoDecoder);
1437 if (err != OK) {
1438 return err;
1439 }
1440 }
1441
1442 if (mAudioSink != NULL) {
1443 err = instantiateDecoder(true, &mAudioDecoder);
1444 if (err != OK) {
1445 return err;
1446 }
1447 }
1448 return OK;
1449}
1450
Wei Jiac5de0912016-11-18 10:22:14 -08001451void NuPlayer::onStart(int64_t startPositionUs, MediaPlayerSeekMode mode) {
Hassan Shojaniacefac142017-02-06 21:02:02 -08001452 ALOGV("onStart: mCrypto: %p (%d)", mCrypto.get(),
1453 (mCrypto != NULL ? mCrypto->getStrongCount() : 0));
1454
Robert Shih0c61a0d2015-07-06 15:09:10 -07001455 if (!mSourceStarted) {
1456 mSourceStarted = true;
1457 mSource->start();
1458 }
1459 if (startPositionUs > 0) {
Wei Jiac5de0912016-11-18 10:22:14 -08001460 performSeek(startPositionUs, mode);
Robert Shih0c61a0d2015-07-06 15:09:10 -07001461 if (mSource->getFormat(false /* audio */) == NULL) {
1462 return;
1463 }
1464 }
1465
Wei Jia94211742014-10-28 17:09:06 -07001466 mOffloadAudio = false;
1467 mAudioEOS = false;
1468 mVideoEOS = false;
Wei Jia94211742014-10-28 17:09:06 -07001469 mStarted = true;
Wei Jiafe8fe7d2016-06-08 10:29:25 -07001470 mPaused = false;
Wei Jia94211742014-10-28 17:09:06 -07001471
Wei Jia94211742014-10-28 17:09:06 -07001472 uint32_t flags = 0;
1473
1474 if (mSource->isRealTime()) {
1475 flags |= Renderer::FLAG_REAL_TIME;
1476 }
1477
Wei Jia9737d342016-12-28 14:59:59 -08001478 bool hasAudio = (mSource->getFormat(true /* audio */) != NULL);
1479 bool hasVideo = (mSource->getFormat(false /* audio */) != NULL);
1480 if (!hasAudio && !hasVideo) {
Wei Jia1de83d52016-10-10 10:15:03 -07001481 ALOGE("no metadata for either audio or video source");
1482 mSource->stop();
1483 mSourceStarted = false;
1484 notifyListener(MEDIA_ERROR, MEDIA_ERROR_UNKNOWN, ERROR_MALFORMED);
1485 return;
1486 }
Wei Jia9737d342016-12-28 14:59:59 -08001487 ALOGV_IF(!hasAudio, "no metadata for audio source"); // video only stream
1488
1489 sp<MetaData> audioMeta = mSource->getFormatMeta(true /* audio */);
Wei Jia1de83d52016-10-10 10:15:03 -07001490
Wei Jia94211742014-10-28 17:09:06 -07001491 audio_stream_type_t streamType = AUDIO_STREAM_MUSIC;
1492 if (mAudioSink != NULL) {
1493 streamType = mAudioSink->getAudioStreamType();
1494 }
1495
Wei Jia94211742014-10-28 17:09:06 -07001496 mOffloadAudio =
Wei Jia9737d342016-12-28 14:59:59 -08001497 canOffloadStream(audioMeta, hasVideo, mSource->isStreaming(), streamType)
Wei Jiabf70feb2016-02-19 15:47:16 -08001498 && (mPlaybackSettings.mSpeed == 1.f && mPlaybackSettings.mPitch == 1.f);
Hassan Shojaniacefac142017-02-06 21:02:02 -08001499
1500 // Modular DRM: Disabling audio offload if the source is protected
1501 if (mOffloadAudio && mIsDrmProtected) {
1502 mOffloadAudio = false;
1503 ALOGV("onStart: Disabling mOffloadAudio now that the source is protected.");
1504 }
1505
Wei Jia94211742014-10-28 17:09:06 -07001506 if (mOffloadAudio) {
1507 flags |= Renderer::FLAG_OFFLOAD_AUDIO;
1508 }
1509
Lajos Molnar1d15ab52015-03-04 16:46:34 -08001510 sp<AMessage> notify = new AMessage(kWhatRendererNotify, this);
Wei Jia94211742014-10-28 17:09:06 -07001511 ++mRendererGeneration;
1512 notify->setInt32("generation", mRendererGeneration);
1513 mRenderer = new Renderer(mAudioSink, notify, flags);
Wei Jia94211742014-10-28 17:09:06 -07001514 mRendererLooper = new ALooper;
1515 mRendererLooper->setName("NuPlayerRenderer");
1516 mRendererLooper->start(false, false, ANDROID_PRIORITY_AUDIO);
1517 mRendererLooper->registerHandler(mRenderer);
Lajos Molnar3a474aa2015-04-24 17:10:07 -07001518
1519 status_t err = mRenderer->setPlaybackSettings(mPlaybackSettings);
1520 if (err != OK) {
1521 mSource->stop();
Robert Shih0c61a0d2015-07-06 15:09:10 -07001522 mSourceStarted = false;
Lajos Molnar3a474aa2015-04-24 17:10:07 -07001523 notifyListener(MEDIA_ERROR, MEDIA_ERROR_UNKNOWN, err);
1524 return;
Wei Jiac8206ff2015-03-04 13:59:37 -08001525 }
Wei Jia94211742014-10-28 17:09:06 -07001526
Ronghua Wuc8a70d32015-04-29 16:26:34 -07001527 float rate = getFrameRate();
1528 if (rate > 0) {
Wei Jia94211742014-10-28 17:09:06 -07001529 mRenderer->setVideoFrameRate(rate);
1530 }
1531
Wei Jiac6cfd702014-11-11 16:33:20 -08001532 if (mVideoDecoder != NULL) {
1533 mVideoDecoder->setRenderer(mRenderer);
1534 }
1535 if (mAudioDecoder != NULL) {
1536 mAudioDecoder->setRenderer(mRenderer);
1537 }
1538
Ray Essickd4d00612017-01-03 09:36:27 -08001539 mLastStartedPlayingTimeNs = systemTime();
1540
Wei Jia94211742014-10-28 17:09:06 -07001541 postScanSources();
1542}
1543
Chong Zhangefbb6192015-01-30 17:13:27 -08001544void NuPlayer::onPause() {
1545 if (mPaused) {
1546 return;
1547 }
1548 mPaused = true;
1549 if (mSource != NULL) {
1550 mSource->pause();
1551 } else {
1552 ALOGW("pause called when source is gone or not set");
1553 }
1554 if (mRenderer != NULL) {
1555 mRenderer->pause();
1556 } else {
1557 ALOGW("pause called when renderer is gone or not set");
1558 }
Ray Essickd4d00612017-01-03 09:36:27 -08001559
1560 sp<NuPlayerDriver> driver = mDriver.promote();
1561 if (driver != NULL) {
1562 int64_t now = systemTime();
1563 int64_t played = now - mLastStartedPlayingTimeNs;
1564 ALOGD("played from %" PRId64 " to %" PRId64 " = %" PRId64 ,
1565 mLastStartedPlayingTimeNs, now, played);
1566
1567 driver->notifyMorePlayingTimeUs((played+500)/1000);
1568 }
Chong Zhangefbb6192015-01-30 17:13:27 -08001569}
1570
Ronghua Wud7988b12014-10-03 15:19:10 -07001571bool NuPlayer::audioDecoderStillNeeded() {
1572 // Audio decoder is no longer needed if it's in shut/shutting down status.
1573 return ((mFlushingAudio != SHUT_DOWN) && (mFlushingAudio != SHUTTING_DOWN_DECODER));
1574}
1575
Andy Hung8d121d42014-10-03 09:53:53 -07001576void NuPlayer::handleFlushComplete(bool audio, bool isDecoder) {
1577 // We wait for both the decoder flush and the renderer flush to complete
1578 // before entering either the FLUSHED or the SHUTTING_DOWN_DECODER state.
1579
1580 mFlushComplete[audio][isDecoder] = true;
1581 if (!mFlushComplete[audio][!isDecoder]) {
1582 return;
1583 }
1584
1585 FlushStatus *state = audio ? &mFlushingAudio : &mFlushingVideo;
1586 switch (*state) {
1587 case FLUSHING_DECODER:
1588 {
1589 *state = FLUSHED;
Andy Hung8d121d42014-10-03 09:53:53 -07001590 break;
1591 }
1592
1593 case FLUSHING_DECODER_SHUTDOWN:
1594 {
1595 *state = SHUTTING_DOWN_DECODER;
1596
1597 ALOGV("initiating %s decoder shutdown", audio ? "audio" : "video");
Andy Hung8d121d42014-10-03 09:53:53 -07001598 getDecoder(audio)->initiateShutdown();
1599 break;
1600 }
1601
1602 default:
1603 // decoder flush completes only occur in a flushing state.
1604 LOG_ALWAYS_FATAL_IF(isDecoder, "decoder flush in invalid state %d", *state);
1605 break;
1606 }
1607}
1608
Andreas Huber3831a062010-12-21 10:22:33 -08001609void NuPlayer::finishFlushIfPossible() {
Wei Jia53904f32014-07-29 10:22:53 -07001610 if (mFlushingAudio != NONE && mFlushingAudio != FLUSHED
1611 && mFlushingAudio != SHUT_DOWN) {
Andreas Huber3831a062010-12-21 10:22:33 -08001612 return;
1613 }
1614
Wei Jia53904f32014-07-29 10:22:53 -07001615 if (mFlushingVideo != NONE && mFlushingVideo != FLUSHED
1616 && mFlushingVideo != SHUT_DOWN) {
Andreas Huber3831a062010-12-21 10:22:33 -08001617 return;
1618 }
1619
Steve Block3856b092011-10-20 11:56:00 +01001620 ALOGV("both audio and video are flushed now.");
Andreas Huber3831a062010-12-21 10:22:33 -08001621
Andreas Huber3831a062010-12-21 10:22:33 -08001622 mFlushingAudio = NONE;
1623 mFlushingVideo = NONE;
Andreas Huber3831a062010-12-21 10:22:33 -08001624
Andy Hung8d121d42014-10-03 09:53:53 -07001625 clearFlushComplete();
1626
Andreas Hubera1f8ab02012-11-30 10:53:22 -08001627 processDeferredActions();
Andreas Huber1aef2112011-01-04 14:01:29 -08001628}
1629
1630void NuPlayer::postScanSources() {
1631 if (mScanSourcesPending) {
1632 return;
1633 }
1634
Lajos Molnar1d15ab52015-03-04 16:46:34 -08001635 sp<AMessage> msg = new AMessage(kWhatScanSources, this);
Andreas Huber1aef2112011-01-04 14:01:29 -08001636 msg->setInt32("generation", mScanSourcesGeneration);
1637 msg->post();
1638
1639 mScanSourcesPending = true;
1640}
1641
Wei Jia41cd4632016-05-13 10:53:30 -07001642void NuPlayer::tryOpenAudioSinkForOffload(
1643 const sp<AMessage> &format, const sp<MetaData> &audioMeta, bool hasVideo) {
Andy Hung202bce12014-12-03 11:47:36 -08001644 // Note: This is called early in NuPlayer to determine whether offloading
1645 // is possible; otherwise the decoders call the renderer openAudioSink directly.
Andy Hung282a7e32014-08-14 15:56:34 -07001646
Andy Hung202bce12014-12-03 11:47:36 -08001647 status_t err = mRenderer->openAudioSink(
1648 format, true /* offloadOnly */, hasVideo, AUDIO_OUTPUT_FLAG_NONE, &mOffloadAudio);
1649 if (err != OK) {
1650 // Any failure we turn off mOffloadAudio.
1651 mOffloadAudio = false;
1652 } else if (mOffloadAudio) {
Chong Zhang3b9eb1f2014-10-15 17:05:08 -07001653 sendMetaDataToHal(mAudioSink, audioMeta);
Andy Hung282a7e32014-08-14 15:56:34 -07001654 }
1655}
1656
1657void NuPlayer::closeAudioSink() {
Chong Zhang3b9eb1f2014-10-15 17:05:08 -07001658 mRenderer->closeAudioSink();
Andy Hung282a7e32014-08-14 15:56:34 -07001659}
1660
Wei Jia5031b2f2016-02-25 11:19:31 -08001661void NuPlayer::restartAudio(
Wei Jiabf70feb2016-02-19 15:47:16 -08001662 int64_t currentPositionUs, bool forceNonOffload, bool needsToCreateAudioDecoder) {
Wei Jiaa05f1e32016-03-25 16:31:22 -07001663 if (mAudioDecoder != NULL) {
1664 mAudioDecoder->pause();
1665 mAudioDecoder.clear();
Wei Jia686e8e52017-04-03 14:08:01 -07001666 mAudioDecoderError = false;
Wei Jiaa05f1e32016-03-25 16:31:22 -07001667 ++mAudioDecoderGeneration;
1668 }
Wei Jiabf70feb2016-02-19 15:47:16 -08001669 if (mFlushingAudio == FLUSHING_DECODER) {
1670 mFlushComplete[1 /* audio */][1 /* isDecoder */] = true;
1671 mFlushingAudio = FLUSHED;
1672 finishFlushIfPossible();
1673 } else if (mFlushingAudio == FLUSHING_DECODER_SHUTDOWN
1674 || mFlushingAudio == SHUTTING_DOWN_DECODER) {
1675 mFlushComplete[1 /* audio */][1 /* isDecoder */] = true;
1676 mFlushingAudio = SHUT_DOWN;
1677 finishFlushIfPossible();
1678 needsToCreateAudioDecoder = false;
1679 }
1680 if (mRenderer == NULL) {
1681 return;
1682 }
1683 closeAudioSink();
1684 mRenderer->flush(true /* audio */, false /* notifyComplete */);
1685 if (mVideoDecoder != NULL) {
1686 mRenderer->flush(false /* audio */, false /* notifyComplete */);
1687 }
1688
Wei Jiac5de0912016-11-18 10:22:14 -08001689 performSeek(currentPositionUs, MediaPlayerSeekMode::SEEK_PREVIOUS_SYNC /* mode */);
Wei Jiabf70feb2016-02-19 15:47:16 -08001690
1691 if (forceNonOffload) {
1692 mRenderer->signalDisableOffloadAudio();
1693 mOffloadAudio = false;
1694 }
1695 if (needsToCreateAudioDecoder) {
Wei Jiaa05f1e32016-03-25 16:31:22 -07001696 instantiateDecoder(true /* audio */, &mAudioDecoder, !forceNonOffload);
Wei Jiabf70feb2016-02-19 15:47:16 -08001697 }
1698}
1699
Wei Jia41cd4632016-05-13 10:53:30 -07001700void NuPlayer::determineAudioModeChange(const sp<AMessage> &audioFormat) {
Wei Jiae4d18c72015-07-13 17:58:11 -07001701 if (mSource == NULL || mAudioSink == NULL) {
1702 return;
1703 }
1704
1705 if (mRenderer == NULL) {
1706 ALOGW("No renderer can be used to determine audio mode. Use non-offload for safety.");
1707 mOffloadAudio = false;
1708 return;
1709 }
1710
1711 sp<MetaData> audioMeta = mSource->getFormatMeta(true /* audio */);
1712 sp<AMessage> videoFormat = mSource->getFormat(false /* audio */);
1713 audio_stream_type_t streamType = mAudioSink->getAudioStreamType();
1714 const bool hasVideo = (videoFormat != NULL);
Hassan Shojaniacefac142017-02-06 21:02:02 -08001715 bool canOffload = canOffloadStream(
Wei Jiabf70feb2016-02-19 15:47:16 -08001716 audioMeta, hasVideo, mSource->isStreaming(), streamType)
1717 && (mPlaybackSettings.mSpeed == 1.f && mPlaybackSettings.mPitch == 1.f);
Hassan Shojaniacefac142017-02-06 21:02:02 -08001718
1719 // Modular DRM: Disabling audio offload if the source is protected
1720 if (canOffload && mIsDrmProtected) {
1721 canOffload = false;
1722 ALOGV("determineAudioModeChange: Disabling mOffloadAudio b/c the source is protected.");
1723 }
1724
Wei Jiae4d18c72015-07-13 17:58:11 -07001725 if (canOffload) {
1726 if (!mOffloadAudio) {
1727 mRenderer->signalEnableOffloadAudio();
1728 }
1729 // open audio sink early under offload mode.
Wei Jia41cd4632016-05-13 10:53:30 -07001730 tryOpenAudioSinkForOffload(audioFormat, audioMeta, hasVideo);
Wei Jiae4d18c72015-07-13 17:58:11 -07001731 } else {
Robert Shihe1d70192015-07-23 17:54:13 -07001732 if (mOffloadAudio) {
1733 mRenderer->signalDisableOffloadAudio();
1734 mOffloadAudio = false;
1735 }
Wei Jiae4d18c72015-07-13 17:58:11 -07001736 }
1737}
1738
Wei Jiaa05f1e32016-03-25 16:31:22 -07001739status_t NuPlayer::instantiateDecoder(
1740 bool audio, sp<DecoderBase> *decoder, bool checkAudioModeChange) {
Wei Jia566da802015-08-27 13:59:30 -07001741 // The audio decoder could be cleared by tear down. If still in shut down
1742 // process, no need to create a new audio decoder.
1743 if (*decoder != NULL || (audio && mFlushingAudio == SHUT_DOWN)) {
Andreas Huberf9334412010-12-15 15:17:42 -08001744 return OK;
1745 }
1746
Andreas Huber84066782011-08-16 09:34:26 -07001747 sp<AMessage> format = mSource->getFormat(audio);
Andreas Huberf9334412010-12-15 15:17:42 -08001748
Andreas Huber84066782011-08-16 09:34:26 -07001749 if (format == NULL) {
Robert Shih7350b052015-10-01 15:50:14 -07001750 return UNKNOWN_ERROR;
1751 } else {
1752 status_t err;
1753 if (format->findInt32("err", &err) && err) {
1754 return err;
1755 }
Andreas Huberf9334412010-12-15 15:17:42 -08001756 }
1757
Ronghua Wu8db88132015-04-22 13:51:35 -07001758 format->setInt32("priority", 0 /* realtime */);
1759
Andreas Huber3fe62152011-09-16 15:09:22 -07001760 if (!audio) {
Andreas Huber84066782011-08-16 09:34:26 -07001761 AString mime;
1762 CHECK(format->findString("mime", &mime));
Chong Zhanga7fa1d92014-06-11 14:49:23 -07001763
Lajos Molnar1d15ab52015-03-04 16:46:34 -08001764 sp<AMessage> ccNotify = new AMessage(kWhatClosedCaptionNotify, this);
Chong Zhang341ab6e2015-02-04 13:37:18 -08001765 if (mCCDecoder == NULL) {
1766 mCCDecoder = new CCDecoder(ccNotify);
1767 }
Lajos Molnar09524832014-07-17 14:29:51 -07001768
1769 if (mSourceFlags & Source::FLAG_SECURE) {
1770 format->setInt32("secure", true);
1771 }
Chong Zhang17134602015-01-07 16:14:34 -08001772
1773 if (mSourceFlags & Source::FLAG_PROTECTED) {
1774 format->setInt32("protected", true);
1775 }
Ronghua Wu8db88132015-04-22 13:51:35 -07001776
Ronghua Wuc8a70d32015-04-29 16:26:34 -07001777 float rate = getFrameRate();
1778 if (rate > 0) {
Lajos Molnar3a474aa2015-04-24 17:10:07 -07001779 format->setFloat("operating-rate", rate * mPlaybackSettings.mSpeed);
Ronghua Wu8db88132015-04-22 13:51:35 -07001780 }
Andreas Huber3fe62152011-09-16 15:09:22 -07001781 }
1782
Wei Jiabc2fb722014-07-08 16:37:57 -07001783 if (audio) {
Lajos Molnar1d15ab52015-03-04 16:46:34 -08001784 sp<AMessage> notify = new AMessage(kWhatAudioNotify, this);
Wei Jia88703c32014-08-06 11:24:07 -07001785 ++mAudioDecoderGeneration;
1786 notify->setInt32("generation", mAudioDecoderGeneration);
1787
Wei Jiaa05f1e32016-03-25 16:31:22 -07001788 if (checkAudioModeChange) {
Wei Jia41cd4632016-05-13 10:53:30 -07001789 determineAudioModeChange(format);
Wei Jiaa05f1e32016-03-25 16:31:22 -07001790 }
Wei Jiabc2fb722014-07-08 16:37:57 -07001791 if (mOffloadAudio) {
Wei Jia14532f22015-12-29 11:28:15 -08001792 mSource->setOffloadAudio(true /* offload */);
1793
Haynes Mathew George8b635332015-03-30 17:59:47 -07001794 const bool hasVideo = (mSource->getFormat(false /*audio */) != NULL);
1795 format->setInt32("has-video", hasVideo);
Wei Jiac6cfd702014-11-11 16:33:20 -08001796 *decoder = new DecoderPassThrough(notify, mSource, mRenderer);
Hassan Shojaniacefac142017-02-06 21:02:02 -08001797 ALOGV("instantiateDecoder audio DecoderPassThrough hasVideo: %d", hasVideo);
Wei Jiabc2fb722014-07-08 16:37:57 -07001798 } else {
Wei Jia14532f22015-12-29 11:28:15 -08001799 mSource->setOffloadAudio(false /* offload */);
1800
Wei Jiaf2ae3e12016-10-27 17:10:59 -07001801 *decoder = new Decoder(notify, mSource, mPID, mUID, mRenderer);
Hassan Shojaniacefac142017-02-06 21:02:02 -08001802 ALOGV("instantiateDecoder audio Decoder");
Wei Jiabc2fb722014-07-08 16:37:57 -07001803 }
Wei Jia686e8e52017-04-03 14:08:01 -07001804 mAudioDecoderError = false;
Wei Jiabc2fb722014-07-08 16:37:57 -07001805 } else {
Lajos Molnar1d15ab52015-03-04 16:46:34 -08001806 sp<AMessage> notify = new AMessage(kWhatVideoNotify, this);
Wei Jia88703c32014-08-06 11:24:07 -07001807 ++mVideoDecoderGeneration;
1808 notify->setInt32("generation", mVideoDecoderGeneration);
1809
Chong Zhang7137ec72014-11-12 16:41:05 -08001810 *decoder = new Decoder(
Wei Jiaf2ae3e12016-10-27 17:10:59 -07001811 notify, mSource, mPID, mUID, mRenderer, mSurface, mCCDecoder);
Wei Jia686e8e52017-04-03 14:08:01 -07001812 mVideoDecoderError = false;
Lajos Molnard9fd6312014-11-06 11:00:00 -08001813
1814 // enable FRC if high-quality AV sync is requested, even if not
Lajos Molnar1de1e252015-04-30 18:18:34 -07001815 // directly queuing to display, as this will even improve textureview
Lajos Molnard9fd6312014-11-06 11:00:00 -08001816 // playback.
1817 {
Marco Nelissen96626b72016-12-01 13:58:59 -08001818 if (property_get_bool("persist.sys.media.avsync", false)) {
Lajos Molnard9fd6312014-11-06 11:00:00 -08001819 format->setInt32("auto-frc", 1);
1820 }
1821 }
Wei Jiabc2fb722014-07-08 16:37:57 -07001822 }
Lajos Molnar1cd13982014-01-17 15:12:51 -08001823 (*decoder)->init();
Hassan Shojaniacefac142017-02-06 21:02:02 -08001824
1825 // Modular DRM
1826 if (mIsDrmProtected) {
1827 format->setPointer("crypto", mCrypto.get());
1828 ALOGV("instantiateDecoder: mCrypto: %p (%d) isSecure: %d", mCrypto.get(),
1829 (mCrypto != NULL ? mCrypto->getStrongCount() : 0),
1830 (mSourceFlags & Source::FLAG_SECURE) != 0);
1831 }
1832
Andreas Huber84066782011-08-16 09:34:26 -07001833 (*decoder)->configure(format);
Andreas Huberf9334412010-12-15 15:17:42 -08001834
Praveen Chavanbbaa1442016-04-08 13:33:49 -07001835 if (!audio) {
1836 sp<AMessage> params = new AMessage();
1837 float rate = getFrameRate();
1838 if (rate > 0) {
1839 params->setFloat("frame-rate-total", rate);
1840 }
1841
1842 sp<MetaData> fileMeta = getFileMeta();
1843 if (fileMeta != NULL) {
1844 int32_t videoTemporalLayerCount;
1845 if (fileMeta->findInt32(kKeyTemporalLayerCount, &videoTemporalLayerCount)
1846 && videoTemporalLayerCount > 0) {
1847 params->setInt32("temporal-layer-count", videoTemporalLayerCount);
1848 }
1849 }
1850
1851 if (params->countEntries() > 0) {
1852 (*decoder)->setParameters(params);
1853 }
1854 }
Andreas Huberf9334412010-12-15 15:17:42 -08001855 return OK;
1856}
1857
Chong Zhangced1c2f2014-08-08 15:22:35 -07001858void NuPlayer::updateVideoSize(
1859 const sp<AMessage> &inputFormat,
1860 const sp<AMessage> &outputFormat) {
1861 if (inputFormat == NULL) {
1862 ALOGW("Unknown video size, reporting 0x0!");
1863 notifyListener(MEDIA_SET_VIDEO_SIZE, 0, 0);
1864 return;
1865 }
Wei Jia9737d342016-12-28 14:59:59 -08001866 int32_t err = OK;
1867 inputFormat->findInt32("err", &err);
1868 if (err == -EWOULDBLOCK) {
1869 ALOGW("Video meta is not available yet!");
1870 return;
1871 }
1872 if (err != OK) {
1873 ALOGW("Something is wrong with video meta!");
1874 return;
1875 }
Chong Zhangced1c2f2014-08-08 15:22:35 -07001876
1877 int32_t displayWidth, displayHeight;
Chong Zhangced1c2f2014-08-08 15:22:35 -07001878 if (outputFormat != NULL) {
1879 int32_t width, height;
1880 CHECK(outputFormat->findInt32("width", &width));
1881 CHECK(outputFormat->findInt32("height", &height));
1882
1883 int32_t cropLeft, cropTop, cropRight, cropBottom;
1884 CHECK(outputFormat->findRect(
1885 "crop",
1886 &cropLeft, &cropTop, &cropRight, &cropBottom));
1887
1888 displayWidth = cropRight - cropLeft + 1;
1889 displayHeight = cropBottom - cropTop + 1;
1890
1891 ALOGV("Video output format changed to %d x %d "
1892 "(crop: %d x %d @ (%d, %d))",
1893 width, height,
1894 displayWidth,
1895 displayHeight,
1896 cropLeft, cropTop);
1897 } else {
1898 CHECK(inputFormat->findInt32("width", &displayWidth));
1899 CHECK(inputFormat->findInt32("height", &displayHeight));
1900
1901 ALOGV("Video input format %d x %d", displayWidth, displayHeight);
1902 }
1903
1904 // Take into account sample aspect ratio if necessary:
1905 int32_t sarWidth, sarHeight;
1906 if (inputFormat->findInt32("sar-width", &sarWidth)
Wei Jia095bc252017-01-27 10:16:16 -08001907 && inputFormat->findInt32("sar-height", &sarHeight)
1908 && sarWidth > 0 && sarHeight > 0) {
Chong Zhangced1c2f2014-08-08 15:22:35 -07001909 ALOGV("Sample aspect ratio %d : %d", sarWidth, sarHeight);
1910
1911 displayWidth = (displayWidth * sarWidth) / sarHeight;
1912
1913 ALOGV("display dimensions %d x %d", displayWidth, displayHeight);
Wei Jiadf6c6af2016-09-29 11:27:15 -07001914 } else {
1915 int32_t width, height;
1916 if (inputFormat->findInt32("display-width", &width)
1917 && inputFormat->findInt32("display-height", &height)
1918 && width > 0 && height > 0
1919 && displayWidth > 0 && displayHeight > 0) {
1920 if (displayHeight * (int64_t)width / height > (int64_t)displayWidth) {
1921 displayHeight = (int32_t)(displayWidth * (int64_t)height / width);
1922 } else {
1923 displayWidth = (int32_t)(displayHeight * (int64_t)width / height);
1924 }
1925 ALOGV("Video display width and height are overridden to %d x %d",
1926 displayWidth, displayHeight);
1927 }
Chong Zhangced1c2f2014-08-08 15:22:35 -07001928 }
1929
1930 int32_t rotationDegrees;
1931 if (!inputFormat->findInt32("rotation-degrees", &rotationDegrees)) {
1932 rotationDegrees = 0;
1933 }
1934
1935 if (rotationDegrees == 90 || rotationDegrees == 270) {
1936 int32_t tmp = displayWidth;
1937 displayWidth = displayHeight;
1938 displayHeight = tmp;
1939 }
1940
1941 notifyListener(
1942 MEDIA_SET_VIDEO_SIZE,
1943 displayWidth,
1944 displayHeight);
1945}
1946
Chong Zhangdcb89b32013-08-06 09:44:47 -07001947void NuPlayer::notifyListener(int msg, int ext1, int ext2, const Parcel *in) {
Andreas Huber43c3e6c2011-01-05 12:17:08 -08001948 if (mDriver == NULL) {
Andreas Huberf9334412010-12-15 15:17:42 -08001949 return;
1950 }
1951
Andreas Huber43c3e6c2011-01-05 12:17:08 -08001952 sp<NuPlayerDriver> driver = mDriver.promote();
Andreas Huberf9334412010-12-15 15:17:42 -08001953
Andreas Huber43c3e6c2011-01-05 12:17:08 -08001954 if (driver == NULL) {
Andreas Huberf9334412010-12-15 15:17:42 -08001955 return;
1956 }
1957
Chong Zhangdcb89b32013-08-06 09:44:47 -07001958 driver->notifyListener(msg, ext1, ext2, in);
Andreas Huberf9334412010-12-15 15:17:42 -08001959}
1960
Chong Zhang7137ec72014-11-12 16:41:05 -08001961void NuPlayer::flushDecoder(bool audio, bool needShutdown) {
Andreas Huber14f76722013-01-15 09:04:18 -08001962 ALOGV("[%s] flushDecoder needShutdown=%d",
1963 audio ? "audio" : "video", needShutdown);
1964
Chong Zhang7137ec72014-11-12 16:41:05 -08001965 const sp<DecoderBase> &decoder = getDecoder(audio);
Lajos Molnar87603c02014-08-20 19:25:30 -07001966 if (decoder == NULL) {
Steve Blockdf64d152012-01-04 20:05:49 +00001967 ALOGI("flushDecoder %s without decoder present",
Andreas Huber6e3d3112011-11-28 12:36:11 -08001968 audio ? "audio" : "video");
Lajos Molnar87603c02014-08-20 19:25:30 -07001969 return;
Andreas Huber6e3d3112011-11-28 12:36:11 -08001970 }
1971
Andreas Huber1aef2112011-01-04 14:01:29 -08001972 // Make sure we don't continue to scan sources until we finish flushing.
1973 ++mScanSourcesGeneration;
Chong Zhang3b032b32015-04-17 15:49:06 -07001974 if (mScanSourcesPending) {
Toshikazu Saitocbcbb792015-09-15 14:53:01 +09001975 if (!needShutdown) {
1976 mDeferredActions.push_back(
1977 new SimpleAction(&NuPlayer::performScanSources));
1978 }
Chong Zhang3b032b32015-04-17 15:49:06 -07001979 mScanSourcesPending = false;
1980 }
Andreas Huber1aef2112011-01-04 14:01:29 -08001981
Chong Zhang7137ec72014-11-12 16:41:05 -08001982 decoder->signalFlush();
Andreas Huber1aef2112011-01-04 14:01:29 -08001983
1984 FlushStatus newStatus =
1985 needShutdown ? FLUSHING_DECODER_SHUTDOWN : FLUSHING_DECODER;
1986
Lajos Molnarfcd3e942015-03-31 10:06:48 -07001987 mFlushComplete[audio][false /* isDecoder */] = (mRenderer == NULL);
Andy Hung8d121d42014-10-03 09:53:53 -07001988 mFlushComplete[audio][true /* isDecoder */] = false;
Andreas Huber1aef2112011-01-04 14:01:29 -08001989 if (audio) {
Wei Jia53904f32014-07-29 10:22:53 -07001990 ALOGE_IF(mFlushingAudio != NONE,
1991 "audio flushDecoder() is called in state %d", mFlushingAudio);
Andreas Huber1aef2112011-01-04 14:01:29 -08001992 mFlushingAudio = newStatus;
Andreas Huber1aef2112011-01-04 14:01:29 -08001993 } else {
Wei Jia53904f32014-07-29 10:22:53 -07001994 ALOGE_IF(mFlushingVideo != NONE,
1995 "video flushDecoder() is called in state %d", mFlushingVideo);
Andreas Huber1aef2112011-01-04 14:01:29 -08001996 mFlushingVideo = newStatus;
Andreas Huber1aef2112011-01-04 14:01:29 -08001997 }
1998}
1999
Chong Zhangced1c2f2014-08-08 15:22:35 -07002000void NuPlayer::queueDecoderShutdown(
2001 bool audio, bool video, const sp<AMessage> &reply) {
2002 ALOGI("queueDecoderShutdown audio=%d, video=%d", audio, video);
Andreas Huber84066782011-08-16 09:34:26 -07002003
Chong Zhangced1c2f2014-08-08 15:22:35 -07002004 mDeferredActions.push_back(
Wei Jiafef808d2014-10-31 17:57:05 -07002005 new FlushDecoderAction(
2006 audio ? FLUSH_CMD_SHUTDOWN : FLUSH_CMD_NONE,
2007 video ? FLUSH_CMD_SHUTDOWN : FLUSH_CMD_NONE));
Andreas Huber84066782011-08-16 09:34:26 -07002008
Chong Zhangced1c2f2014-08-08 15:22:35 -07002009 mDeferredActions.push_back(
2010 new SimpleAction(&NuPlayer::performScanSources));
Andreas Huber84066782011-08-16 09:34:26 -07002011
Chong Zhangced1c2f2014-08-08 15:22:35 -07002012 mDeferredActions.push_back(new PostMessageAction(reply));
2013
2014 processDeferredActions();
Andreas Huber84066782011-08-16 09:34:26 -07002015}
2016
James Dong0d268a32012-08-31 12:18:27 -07002017status_t NuPlayer::setVideoScalingMode(int32_t mode) {
2018 mVideoScalingMode = mode;
Lajos Molnar1de1e252015-04-30 18:18:34 -07002019 if (mSurface != NULL) {
2020 status_t ret = native_window_set_scaling_mode(mSurface.get(), mVideoScalingMode);
James Dong0d268a32012-08-31 12:18:27 -07002021 if (ret != OK) {
2022 ALOGE("Failed to set scaling mode (%d): %s",
2023 -ret, strerror(-ret));
2024 return ret;
2025 }
2026 }
2027 return OK;
2028}
2029
Chong Zhangdcb89b32013-08-06 09:44:47 -07002030status_t NuPlayer::getTrackInfo(Parcel* reply) const {
Lajos Molnar1d15ab52015-03-04 16:46:34 -08002031 sp<AMessage> msg = new AMessage(kWhatGetTrackInfo, this);
Chong Zhangdcb89b32013-08-06 09:44:47 -07002032 msg->setPointer("reply", reply);
2033
2034 sp<AMessage> response;
2035 status_t err = msg->postAndAwaitResponse(&response);
2036 return err;
2037}
2038
Robert Shih7c4f0d72014-07-09 18:53:31 -07002039status_t NuPlayer::getSelectedTrack(int32_t type, Parcel* reply) const {
Lajos Molnar1d15ab52015-03-04 16:46:34 -08002040 sp<AMessage> msg = new AMessage(kWhatGetSelectedTrack, this);
Robert Shih7c4f0d72014-07-09 18:53:31 -07002041 msg->setPointer("reply", reply);
2042 msg->setInt32("type", type);
2043
2044 sp<AMessage> response;
2045 status_t err = msg->postAndAwaitResponse(&response);
2046 if (err == OK && response != NULL) {
2047 CHECK(response->findInt32("err", &err));
2048 }
2049 return err;
2050}
2051
Robert Shih6ffb1fd2014-10-29 16:24:32 -07002052status_t NuPlayer::selectTrack(size_t trackIndex, bool select, int64_t timeUs) {
Lajos Molnar1d15ab52015-03-04 16:46:34 -08002053 sp<AMessage> msg = new AMessage(kWhatSelectTrack, this);
Chong Zhangdcb89b32013-08-06 09:44:47 -07002054 msg->setSize("trackIndex", trackIndex);
2055 msg->setInt32("select", select);
Robert Shih6ffb1fd2014-10-29 16:24:32 -07002056 msg->setInt64("timeUs", timeUs);
Chong Zhangdcb89b32013-08-06 09:44:47 -07002057
2058 sp<AMessage> response;
2059 status_t err = msg->postAndAwaitResponse(&response);
2060
Chong Zhang404fced2014-06-11 14:45:31 -07002061 if (err != OK) {
2062 return err;
2063 }
2064
2065 if (!response->findInt32("err", &err)) {
2066 err = OK;
2067 }
2068
Chong Zhangdcb89b32013-08-06 09:44:47 -07002069 return err;
2070}
2071
Ronghua Wua73d9e02014-10-08 15:13:29 -07002072status_t NuPlayer::getCurrentPosition(int64_t *mediaUs) {
2073 sp<Renderer> renderer = mRenderer;
2074 if (renderer == NULL) {
2075 return NO_INIT;
2076 }
2077
2078 return renderer->getCurrentPosition(mediaUs);
2079}
2080
Praveen Chavane1e5d7a2015-05-19 19:09:48 -07002081void NuPlayer::getStats(Vector<sp<AMessage> > *mTrackStats) {
2082 CHECK(mTrackStats != NULL);
2083
2084 mTrackStats->clear();
2085 if (mVideoDecoder != NULL) {
2086 mTrackStats->push_back(mVideoDecoder->getStats());
2087 }
2088 if (mAudioDecoder != NULL) {
2089 mTrackStats->push_back(mAudioDecoder->getStats());
Chong Zhang7137ec72014-11-12 16:41:05 -08002090 }
Ronghua Wua73d9e02014-10-08 15:13:29 -07002091}
2092
Marco Nelissenf0b72b52014-09-16 15:43:44 -07002093sp<MetaData> NuPlayer::getFileMeta() {
2094 return mSource->getFileFormatMeta();
2095}
2096
Ronghua Wuc8a70d32015-04-29 16:26:34 -07002097float NuPlayer::getFrameRate() {
2098 sp<MetaData> meta = mSource->getFormatMeta(false /* audio */);
2099 if (meta == NULL) {
2100 return 0;
2101 }
2102 int32_t rate;
2103 if (!meta->findInt32(kKeyFrameRate, &rate)) {
2104 // fall back to try file meta
2105 sp<MetaData> fileMeta = getFileMeta();
2106 if (fileMeta == NULL) {
2107 ALOGW("source has video meta but not file meta");
2108 return -1;
2109 }
2110 int32_t fileMetaRate;
2111 if (!fileMeta->findInt32(kKeyFrameRate, &fileMetaRate)) {
2112 return -1;
2113 }
2114 return fileMetaRate;
2115 }
2116 return rate;
2117}
2118
Andreas Huberb7c8e912012-11-27 15:02:53 -08002119void NuPlayer::schedulePollDuration() {
Lajos Molnar1d15ab52015-03-04 16:46:34 -08002120 sp<AMessage> msg = new AMessage(kWhatPollDuration, this);
Andreas Huberb7c8e912012-11-27 15:02:53 -08002121 msg->setInt32("generation", mPollDurationGeneration);
2122 msg->post();
2123}
2124
2125void NuPlayer::cancelPollDuration() {
2126 ++mPollDurationGeneration;
2127}
2128
Andreas Hubera1f8ab02012-11-30 10:53:22 -08002129void NuPlayer::processDeferredActions() {
2130 while (!mDeferredActions.empty()) {
2131 // We won't execute any deferred actions until we're no longer in
2132 // an intermediate state, i.e. one more more decoders are currently
2133 // flushing or shutting down.
2134
Andreas Hubera1f8ab02012-11-30 10:53:22 -08002135 if (mFlushingAudio != NONE || mFlushingVideo != NONE) {
2136 // We're currently flushing, postpone the reset until that's
2137 // completed.
2138
2139 ALOGV("postponing action mFlushingAudio=%d, mFlushingVideo=%d",
2140 mFlushingAudio, mFlushingVideo);
2141
2142 break;
2143 }
2144
2145 sp<Action> action = *mDeferredActions.begin();
2146 mDeferredActions.erase(mDeferredActions.begin());
2147
2148 action->execute(this);
2149 }
2150}
2151
Wei Jiac5de0912016-11-18 10:22:14 -08002152void NuPlayer::performSeek(int64_t seekTimeUs, MediaPlayerSeekMode mode) {
2153 ALOGV("performSeek seekTimeUs=%lld us (%.2f secs), mode=%d",
2154 (long long)seekTimeUs, seekTimeUs / 1E6, mode);
Andreas Hubera1f8ab02012-11-30 10:53:22 -08002155
Andy Hungadf34bf2014-09-03 18:22:22 -07002156 if (mSource == NULL) {
2157 // This happens when reset occurs right before the loop mode
2158 // asynchronously seeks to the start of the stream.
2159 LOG_ALWAYS_FATAL_IF(mAudioDecoder != NULL || mVideoDecoder != NULL,
2160 "mSource is NULL and decoders not NULL audio(%p) video(%p)",
2161 mAudioDecoder.get(), mVideoDecoder.get());
2162 return;
2163 }
Robert Shih1a5c8592015-08-04 18:07:44 -07002164 mPreviousSeekTimeUs = seekTimeUs;
Wei Jiac5de0912016-11-18 10:22:14 -08002165 mSource->seekTo(seekTimeUs, mode);
Robert Shihd3b0bbb2014-07-23 15:00:25 -07002166 ++mTimedTextGeneration;
Andreas Hubera1f8ab02012-11-30 10:53:22 -08002167
Andreas Hubera1f8ab02012-11-30 10:53:22 -08002168 // everything's flushed, continue playback.
2169}
2170
Wei Jiafef808d2014-10-31 17:57:05 -07002171void NuPlayer::performDecoderFlush(FlushCommand audio, FlushCommand video) {
2172 ALOGV("performDecoderFlush audio=%d, video=%d", audio, video);
Andreas Hubera1f8ab02012-11-30 10:53:22 -08002173
Wei Jiafef808d2014-10-31 17:57:05 -07002174 if ((audio == FLUSH_CMD_NONE || mAudioDecoder == NULL)
2175 && (video == FLUSH_CMD_NONE || mVideoDecoder == NULL)) {
Andreas Hubera1f8ab02012-11-30 10:53:22 -08002176 return;
2177 }
2178
Wei Jiafef808d2014-10-31 17:57:05 -07002179 if (audio != FLUSH_CMD_NONE && mAudioDecoder != NULL) {
2180 flushDecoder(true /* audio */, (audio == FLUSH_CMD_SHUTDOWN));
Andreas Hubera1f8ab02012-11-30 10:53:22 -08002181 }
2182
Wei Jiafef808d2014-10-31 17:57:05 -07002183 if (video != FLUSH_CMD_NONE && mVideoDecoder != NULL) {
2184 flushDecoder(false /* audio */, (video == FLUSH_CMD_SHUTDOWN));
Andreas Hubera1f8ab02012-11-30 10:53:22 -08002185 }
2186}
2187
2188void NuPlayer::performReset() {
2189 ALOGV("performReset");
2190
2191 CHECK(mAudioDecoder == NULL);
2192 CHECK(mVideoDecoder == NULL);
2193
2194 cancelPollDuration();
2195
2196 ++mScanSourcesGeneration;
2197 mScanSourcesPending = false;
2198
Lajos Molnar09524832014-07-17 14:29:51 -07002199 if (mRendererLooper != NULL) {
2200 if (mRenderer != NULL) {
2201 mRendererLooper->unregisterHandler(mRenderer->id());
2202 }
2203 mRendererLooper->stop();
2204 mRendererLooper.clear();
2205 }
Andreas Hubera1f8ab02012-11-30 10:53:22 -08002206 mRenderer.clear();
Wei Jia57568df2014-09-22 10:16:29 -07002207 ++mRendererGeneration;
Andreas Hubera1f8ab02012-11-30 10:53:22 -08002208
2209 if (mSource != NULL) {
2210 mSource->stop();
Andreas Huberb5f25f02013-02-05 10:14:26 -08002211
Wei Jiac45a4b22016-04-15 15:30:23 -07002212 Mutex::Autolock autoLock(mSourceLock);
Andreas Hubera1f8ab02012-11-30 10:53:22 -08002213 mSource.clear();
2214 }
2215
2216 if (mDriver != NULL) {
2217 sp<NuPlayerDriver> driver = mDriver.promote();
2218 if (driver != NULL) {
2219 driver->notifyResetComplete();
2220 }
2221 }
Andreas Huber57a339c2012-12-03 11:18:00 -08002222
2223 mStarted = false;
Wei Jia8a092d32016-06-03 14:57:24 -07002224 mPrepared = false;
Ronghua Wu64c2d172015-10-07 16:52:19 -07002225 mResetting = false;
Robert Shih0c61a0d2015-07-06 15:09:10 -07002226 mSourceStarted = false;
Hassan Shojaniacefac142017-02-06 21:02:02 -08002227
2228 // Modular DRM
2229 if (mCrypto != NULL) {
2230 // decoders will be flushed before this so their mCrypto would go away on their own
2231 // TODO change to ALOGV
2232 ALOGD("performReset mCrypto: %p (%d)", mCrypto.get(),
2233 (mCrypto != NULL ? mCrypto->getStrongCount() : 0));
2234 mCrypto.clear();
2235 }
2236 mIsDrmProtected = false;
Andreas Hubera1f8ab02012-11-30 10:53:22 -08002237}
2238
2239void NuPlayer::performScanSources() {
2240 ALOGV("performScanSources");
2241
Andreas Huber57a339c2012-12-03 11:18:00 -08002242 if (!mStarted) {
2243 return;
2244 }
2245
Andreas Hubera1f8ab02012-11-30 10:53:22 -08002246 if (mAudioDecoder == NULL || mVideoDecoder == NULL) {
2247 postScanSources();
2248 }
2249}
2250
Lajos Molnar1de1e252015-04-30 18:18:34 -07002251void NuPlayer::performSetSurface(const sp<Surface> &surface) {
Andreas Huber57a339c2012-12-03 11:18:00 -08002252 ALOGV("performSetSurface");
2253
Lajos Molnar1de1e252015-04-30 18:18:34 -07002254 mSurface = surface;
Andreas Huber57a339c2012-12-03 11:18:00 -08002255
2256 // XXX - ignore error from setVideoScalingMode for now
2257 setVideoScalingMode(mVideoScalingMode);
Chong Zhang13d6faa2014-08-22 15:35:28 -07002258
2259 if (mDriver != NULL) {
2260 sp<NuPlayerDriver> driver = mDriver.promote();
2261 if (driver != NULL) {
2262 driver->notifySetSurfaceComplete();
2263 }
2264 }
Andreas Huber57a339c2012-12-03 11:18:00 -08002265}
2266
Chong Zhangf8d71772014-11-26 15:08:34 -08002267void NuPlayer::performResumeDecoders(bool needNotify) {
2268 if (needNotify) {
2269 mResumePending = true;
2270 if (mVideoDecoder == NULL) {
2271 // if audio-only, we can notify seek complete now,
2272 // as the resume operation will be relatively fast.
2273 finishResume();
2274 }
2275 }
2276
Chong Zhang7137ec72014-11-12 16:41:05 -08002277 if (mVideoDecoder != NULL) {
Chong Zhangf8d71772014-11-26 15:08:34 -08002278 // When there is continuous seek, MediaPlayer will cache the seek
2279 // position, and send down new seek request when previous seek is
2280 // complete. Let's wait for at least one video output frame before
2281 // notifying seek complete, so that the video thumbnail gets updated
2282 // when seekbar is dragged.
2283 mVideoDecoder->signalResume(needNotify);
Chong Zhang7137ec72014-11-12 16:41:05 -08002284 }
2285
2286 if (mAudioDecoder != NULL) {
Chong Zhangf8d71772014-11-26 15:08:34 -08002287 mAudioDecoder->signalResume(false /* needNotify */);
2288 }
2289}
2290
2291void NuPlayer::finishResume() {
2292 if (mResumePending) {
2293 mResumePending = false;
Wei Jia1061c9c2015-05-19 16:02:17 -07002294 notifyDriverSeekComplete();
2295 }
2296}
2297
2298void NuPlayer::notifyDriverSeekComplete() {
2299 if (mDriver != NULL) {
2300 sp<NuPlayerDriver> driver = mDriver.promote();
2301 if (driver != NULL) {
2302 driver->notifySeekComplete();
Chong Zhangf8d71772014-11-26 15:08:34 -08002303 }
Chong Zhang7137ec72014-11-12 16:41:05 -08002304 }
2305}
2306
Andreas Huber9575c962013-02-05 13:59:56 -08002307void NuPlayer::onSourceNotify(const sp<AMessage> &msg) {
2308 int32_t what;
2309 CHECK(msg->findInt32("what", &what));
2310
2311 switch (what) {
Lajos Molnarfcd3e942015-03-31 10:06:48 -07002312 case Source::kWhatInstantiateSecureDecoders:
2313 {
2314 if (mSource == NULL) {
2315 // This is a stale notification from a source that was
2316 // asynchronously preparing when the client called reset().
2317 // We handled the reset, the source is gone.
2318 break;
2319 }
2320
2321 sp<AMessage> reply;
2322 CHECK(msg->findMessage("reply", &reply));
2323 status_t err = onInstantiateSecureDecoders();
2324 reply->setInt32("err", err);
2325 reply->post();
2326 break;
2327 }
2328
Andreas Huber9575c962013-02-05 13:59:56 -08002329 case Source::kWhatPrepared:
2330 {
Hassan Shojaniacefac142017-02-06 21:02:02 -08002331 ALOGV("NuPlayer::onSourceNotify Source::kWhatPrepared source: %p", mSource.get());
Andreas Huberb5f28d42013-04-25 15:11:19 -07002332 if (mSource == NULL) {
2333 // This is a stale notification from a source that was
2334 // asynchronously preparing when the client called reset().
2335 // We handled the reset, the source is gone.
2336 break;
2337 }
2338
Andreas Huberec0c5972013-02-05 14:47:13 -08002339 int32_t err;
2340 CHECK(msg->findInt32("err", &err));
2341
Lajos Molnarfcd3e942015-03-31 10:06:48 -07002342 if (err != OK) {
2343 // shut down potential secure codecs in case client never calls reset
2344 mDeferredActions.push_back(
2345 new FlushDecoderAction(FLUSH_CMD_SHUTDOWN /* audio */,
2346 FLUSH_CMD_SHUTDOWN /* video */));
2347 processDeferredActions();
Wei Jia8a092d32016-06-03 14:57:24 -07002348 } else {
2349 mPrepared = true;
Lajos Molnarfcd3e942015-03-31 10:06:48 -07002350 }
2351
Andreas Huber9575c962013-02-05 13:59:56 -08002352 sp<NuPlayerDriver> driver = mDriver.promote();
2353 if (driver != NULL) {
Marco Nelissendd114d12014-05-28 15:23:14 -07002354 // notify duration first, so that it's definitely set when
2355 // the app received the "prepare complete" callback.
2356 int64_t durationUs;
2357 if (mSource->getDuration(&durationUs) == OK) {
2358 driver->notifyDuration(durationUs);
2359 }
Andreas Huberec0c5972013-02-05 14:47:13 -08002360 driver->notifyPrepareCompleted(err);
Andreas Huber9575c962013-02-05 13:59:56 -08002361 }
Andreas Huber99759402013-04-01 14:28:31 -07002362
Andreas Huber9575c962013-02-05 13:59:56 -08002363 break;
2364 }
2365
Hassan Shojaniacefac142017-02-06 21:02:02 -08002366 // Modular DRM
2367 case Source::kWhatDrmInfo:
2368 {
2369 Parcel parcel;
2370 sp<ABuffer> drmInfo;
2371 CHECK(msg->findBuffer("drmInfo", &drmInfo));
2372 parcel.setData(drmInfo->data(), drmInfo->size());
2373
2374 ALOGV("onSourceNotify() kWhatDrmInfo MEDIA_DRM_INFO drmInfo: %p parcel size: %zu",
2375 drmInfo.get(), parcel.dataSize());
2376
2377 notifyListener(MEDIA_DRM_INFO, 0 /* ext1 */, 0 /* ext2 */, &parcel);
2378
2379 break;
2380 }
2381
Andreas Huber9575c962013-02-05 13:59:56 -08002382 case Source::kWhatFlagsChanged:
2383 {
2384 uint32_t flags;
2385 CHECK(msg->findInt32("flags", (int32_t *)&flags));
2386
Chong Zhang4b7069d2013-09-11 12:52:43 -07002387 sp<NuPlayerDriver> driver = mDriver.promote();
2388 if (driver != NULL) {
Hassan Shojaniacefac142017-02-06 21:02:02 -08002389
2390 ALOGV("onSourceNotify() kWhatFlagsChanged FLAG_CAN_PAUSE: %d "
2391 "FLAG_CAN_SEEK_BACKWARD: %d \n\t\t\t\t FLAG_CAN_SEEK_FORWARD: %d "
2392 "FLAG_CAN_SEEK: %d FLAG_DYNAMIC_DURATION: %d \n"
2393 "\t\t\t\t FLAG_SECURE: %d FLAG_PROTECTED: %d",
2394 (flags & Source::FLAG_CAN_PAUSE) != 0,
2395 (flags & Source::FLAG_CAN_SEEK_BACKWARD) != 0,
2396 (flags & Source::FLAG_CAN_SEEK_FORWARD) != 0,
2397 (flags & Source::FLAG_CAN_SEEK) != 0,
2398 (flags & Source::FLAG_DYNAMIC_DURATION) != 0,
2399 (flags & Source::FLAG_SECURE) != 0,
2400 (flags & Source::FLAG_PROTECTED) != 0);
2401
Wei Jia895651b2014-12-10 17:31:52 -08002402 if ((flags & NuPlayer::Source::FLAG_CAN_SEEK) == 0) {
2403 driver->notifyListener(
2404 MEDIA_INFO, MEDIA_INFO_NOT_SEEKABLE, 0);
2405 }
Chong Zhang4b7069d2013-09-11 12:52:43 -07002406 driver->notifyFlagsChanged(flags);
2407 }
2408
Andreas Huber9575c962013-02-05 13:59:56 -08002409 if ((mSourceFlags & Source::FLAG_DYNAMIC_DURATION)
2410 && (!(flags & Source::FLAG_DYNAMIC_DURATION))) {
2411 cancelPollDuration();
2412 } else if (!(mSourceFlags & Source::FLAG_DYNAMIC_DURATION)
2413 && (flags & Source::FLAG_DYNAMIC_DURATION)
2414 && (mAudioDecoder != NULL || mVideoDecoder != NULL)) {
2415 schedulePollDuration();
2416 }
2417
2418 mSourceFlags = flags;
2419 break;
2420 }
2421
2422 case Source::kWhatVideoSizeChanged:
2423 {
Chong Zhangced1c2f2014-08-08 15:22:35 -07002424 sp<AMessage> format;
2425 CHECK(msg->findMessage("format", &format));
Andreas Huber9575c962013-02-05 13:59:56 -08002426
Chong Zhangced1c2f2014-08-08 15:22:35 -07002427 updateVideoSize(format);
Andreas Huber9575c962013-02-05 13:59:56 -08002428 break;
2429 }
2430
Chong Zhang2a3cc9a2014-08-21 17:48:26 -07002431 case Source::kWhatBufferingUpdate:
2432 {
2433 int32_t percentage;
2434 CHECK(msg->findInt32("percentage", &percentage));
2435
2436 notifyListener(MEDIA_BUFFERING_UPDATE, percentage, 0);
2437 break;
2438 }
2439
Chong Zhangefbb6192015-01-30 17:13:27 -08002440 case Source::kWhatPauseOnBufferingStart:
2441 {
2442 // ignore if not playing
Chong Zhang8a048332015-05-06 15:16:28 -07002443 if (mStarted) {
Chong Zhangefbb6192015-01-30 17:13:27 -08002444 ALOGI("buffer low, pausing...");
2445
Chong Zhang8a048332015-05-06 15:16:28 -07002446 mPausedForBuffering = true;
Chong Zhangefbb6192015-01-30 17:13:27 -08002447 onPause();
2448 }
Wei Jiabfe82072016-05-20 09:21:50 -07002449 notifyListener(MEDIA_INFO, MEDIA_INFO_BUFFERING_START, 0);
Roger Jönssonb50e83e2013-01-21 16:26:41 +01002450 break;
2451 }
2452
Chong Zhangefbb6192015-01-30 17:13:27 -08002453 case Source::kWhatResumeOnBufferingEnd:
2454 {
2455 // ignore if not playing
Chong Zhang8a048332015-05-06 15:16:28 -07002456 if (mStarted) {
Chong Zhangefbb6192015-01-30 17:13:27 -08002457 ALOGI("buffer ready, resuming...");
2458
Chong Zhang8a048332015-05-06 15:16:28 -07002459 mPausedForBuffering = false;
2460
2461 // do not resume yet if client didn't unpause
2462 if (!mPausedByClient) {
2463 onResume();
2464 }
Chong Zhangefbb6192015-01-30 17:13:27 -08002465 }
Wei Jia3bed45a2016-02-17 11:06:47 -08002466 notifyListener(MEDIA_INFO, MEDIA_INFO_BUFFERING_END, 0);
Roger Jönssonb50e83e2013-01-21 16:26:41 +01002467 break;
2468 }
2469
Chong Zhangefbb6192015-01-30 17:13:27 -08002470 case Source::kWhatCacheStats:
2471 {
2472 int32_t kbps;
2473 CHECK(msg->findInt32("bandwidth", &kbps));
2474
2475 notifyListener(MEDIA_INFO, MEDIA_INFO_NETWORK_BANDWIDTH, kbps);
2476 break;
2477 }
2478
Chong Zhangdcb89b32013-08-06 09:44:47 -07002479 case Source::kWhatSubtitleData:
2480 {
2481 sp<ABuffer> buffer;
2482 CHECK(msg->findBuffer("buffer", &buffer));
2483
Chong Zhang404fced2014-06-11 14:45:31 -07002484 sendSubtitleData(buffer, 0 /* baseIndex */);
Chong Zhangdcb89b32013-08-06 09:44:47 -07002485 break;
2486 }
2487
Robert Shih08528432015-04-08 09:06:54 -07002488 case Source::kWhatTimedMetaData:
2489 {
2490 sp<ABuffer> buffer;
2491 if (!msg->findBuffer("buffer", &buffer)) {
2492 notifyListener(MEDIA_INFO, MEDIA_INFO_METADATA_UPDATE, 0);
2493 } else {
2494 sendTimedMetaData(buffer);
2495 }
2496 break;
2497 }
2498
Robert Shihd3b0bbb2014-07-23 15:00:25 -07002499 case Source::kWhatTimedTextData:
2500 {
2501 int32_t generation;
2502 if (msg->findInt32("generation", &generation)
2503 && generation != mTimedTextGeneration) {
2504 break;
2505 }
2506
2507 sp<ABuffer> buffer;
2508 CHECK(msg->findBuffer("buffer", &buffer));
2509
2510 sp<NuPlayerDriver> driver = mDriver.promote();
2511 if (driver == NULL) {
2512 break;
2513 }
2514
2515 int posMs;
2516 int64_t timeUs, posUs;
2517 driver->getCurrentPosition(&posMs);
Patrik2 Carlsson24d484b2015-01-27 16:49:45 +01002518 posUs = (int64_t) posMs * 1000ll;
Robert Shihd3b0bbb2014-07-23 15:00:25 -07002519 CHECK(buffer->meta()->findInt64("timeUs", &timeUs));
2520
2521 if (posUs < timeUs) {
2522 if (!msg->findInt32("generation", &generation)) {
2523 msg->setInt32("generation", mTimedTextGeneration);
2524 }
2525 msg->post(timeUs - posUs);
2526 } else {
2527 sendTimedTextData(buffer);
2528 }
2529 break;
2530 }
2531
Andreas Huber14f76722013-01-15 09:04:18 -08002532 case Source::kWhatQueueDecoderShutdown:
2533 {
2534 int32_t audio, video;
2535 CHECK(msg->findInt32("audio", &audio));
2536 CHECK(msg->findInt32("video", &video));
2537
2538 sp<AMessage> reply;
2539 CHECK(msg->findMessage("reply", &reply));
2540
2541 queueDecoderShutdown(audio, video, reply);
2542 break;
2543 }
2544
Ronghua Wu80276872014-08-28 15:50:29 -07002545 case Source::kWhatDrmNoLicense:
2546 {
2547 notifyListener(MEDIA_ERROR, MEDIA_ERROR_UNKNOWN, ERROR_DRM_NO_LICENSE);
2548 break;
2549 }
2550
Andreas Huber9575c962013-02-05 13:59:56 -08002551 default:
2552 TRESPASS();
2553 }
2554}
2555
Chong Zhanga7fa1d92014-06-11 14:49:23 -07002556void NuPlayer::onClosedCaptionNotify(const sp<AMessage> &msg) {
2557 int32_t what;
2558 CHECK(msg->findInt32("what", &what));
2559
2560 switch (what) {
2561 case NuPlayer::CCDecoder::kWhatClosedCaptionData:
2562 {
2563 sp<ABuffer> buffer;
2564 CHECK(msg->findBuffer("buffer", &buffer));
2565
2566 size_t inbandTracks = 0;
2567 if (mSource != NULL) {
2568 inbandTracks = mSource->getTrackCount();
2569 }
2570
2571 sendSubtitleData(buffer, inbandTracks);
2572 break;
2573 }
2574
2575 case NuPlayer::CCDecoder::kWhatTrackAdded:
2576 {
2577 notifyListener(MEDIA_INFO, MEDIA_INFO_METADATA_UPDATE, 0);
2578
2579 break;
2580 }
2581
2582 default:
2583 TRESPASS();
2584 }
2585
2586
2587}
2588
Chong Zhang404fced2014-06-11 14:45:31 -07002589void NuPlayer::sendSubtitleData(const sp<ABuffer> &buffer, int32_t baseIndex) {
2590 int32_t trackIndex;
2591 int64_t timeUs, durationUs;
2592 CHECK(buffer->meta()->findInt32("trackIndex", &trackIndex));
2593 CHECK(buffer->meta()->findInt64("timeUs", &timeUs));
2594 CHECK(buffer->meta()->findInt64("durationUs", &durationUs));
2595
2596 Parcel in;
2597 in.writeInt32(trackIndex + baseIndex);
2598 in.writeInt64(timeUs);
2599 in.writeInt64(durationUs);
2600 in.writeInt32(buffer->size());
2601 in.writeInt32(buffer->size());
2602 in.write(buffer->data(), buffer->size());
2603
2604 notifyListener(MEDIA_SUBTITLE_DATA, 0, 0, &in);
2605}
Robert Shihd3b0bbb2014-07-23 15:00:25 -07002606
Robert Shih08528432015-04-08 09:06:54 -07002607void NuPlayer::sendTimedMetaData(const sp<ABuffer> &buffer) {
2608 int64_t timeUs;
2609 CHECK(buffer->meta()->findInt64("timeUs", &timeUs));
2610
2611 Parcel in;
2612 in.writeInt64(timeUs);
2613 in.writeInt32(buffer->size());
2614 in.writeInt32(buffer->size());
2615 in.write(buffer->data(), buffer->size());
2616
2617 notifyListener(MEDIA_META_DATA, 0, 0, &in);
2618}
2619
Robert Shihd3b0bbb2014-07-23 15:00:25 -07002620void NuPlayer::sendTimedTextData(const sp<ABuffer> &buffer) {
2621 const void *data;
2622 size_t size = 0;
2623 int64_t timeUs;
Marco Nelissen55e2f4c2015-09-04 15:57:15 -07002624 int32_t flag = TextDescriptions::IN_BAND_TEXT_3GPP;
Robert Shihd3b0bbb2014-07-23 15:00:25 -07002625
2626 AString mime;
2627 CHECK(buffer->meta()->findString("mime", &mime));
2628 CHECK(strcasecmp(mime.c_str(), MEDIA_MIMETYPE_TEXT_3GPP) == 0);
2629
2630 data = buffer->data();
2631 size = buffer->size();
2632
2633 Parcel parcel;
2634 if (size > 0) {
2635 CHECK(buffer->meta()->findInt64("timeUs", &timeUs));
Marco Nelissen55e2f4c2015-09-04 15:57:15 -07002636 int32_t global = 0;
2637 if (buffer->meta()->findInt32("global", &global) && global) {
2638 flag |= TextDescriptions::GLOBAL_DESCRIPTIONS;
2639 } else {
2640 flag |= TextDescriptions::LOCAL_DESCRIPTIONS;
2641 }
Robert Shihd3b0bbb2014-07-23 15:00:25 -07002642 TextDescriptions::getParcelOfDescriptions(
2643 (const uint8_t *)data, size, flag, timeUs / 1000, &parcel);
2644 }
2645
2646 if ((parcel.dataSize() > 0)) {
2647 notifyListener(MEDIA_TIMED_TEXT, 0, 0, &parcel);
2648 } else { // send an empty timed text
2649 notifyListener(MEDIA_TIMED_TEXT, 0, 0);
2650 }
2651}
Hassan Shojaniacefac142017-02-06 21:02:02 -08002652
2653// Modular DRM begin
2654status_t NuPlayer::prepareDrm(const uint8_t uuid[16], const Vector<uint8_t> &drmSessionId)
2655{
2656 ALOGV("prepareDrm ");
2657
2658 // Passing to the looper anyway; called in a pre-config prepared state so no race on mCrypto
2659 sp<AMessage> msg = new AMessage(kWhatPrepareDrm, this);
2660 // synchronous call so just passing the address but with local copies of "const" args
2661 uint8_t UUID[16];
2662 memcpy(UUID, uuid, sizeof(UUID));
2663 Vector<uint8_t> sessionId = drmSessionId;
2664 msg->setPointer("uuid", (void*)UUID);
2665 msg->setPointer("drmSessionId", (void*)&sessionId);
2666
2667 sp<AMessage> response;
2668 status_t status = msg->postAndAwaitResponse(&response);
2669
2670 if (status == OK && response != NULL) {
2671 CHECK(response->findInt32("status", &status));
2672 ALOGV("prepareDrm ret: %d ", status);
2673 } else {
2674 ALOGE("prepareDrm err: %d", status);
2675 }
2676
2677 return status;
2678}
2679
2680status_t NuPlayer::releaseDrm()
2681{
2682 ALOGV("releaseDrm ");
2683
2684 sp<AMessage> msg = new AMessage(kWhatReleaseDrm, this);
2685
2686 sp<AMessage> response;
2687 status_t status = msg->postAndAwaitResponse(&response);
2688
2689 if (status == OK && response != NULL) {
2690 CHECK(response->findInt32("status", &status));
2691 ALOGV("releaseDrm ret: %d ", status);
2692 } else {
2693 ALOGE("releaseDrm err: %d", status);
2694 }
2695
2696 return status;
2697}
2698
2699status_t NuPlayer::onPrepareDrm(const sp<AMessage> &msg)
2700{
2701 // TODO change to ALOGV
2702 ALOGD("onPrepareDrm ");
2703
2704 status_t status = INVALID_OPERATION;
2705 if (mSource == NULL) {
2706 ALOGE("onPrepareDrm: No source. onPrepareDrm failed with %d.", status);
2707 return status;
2708 }
2709
2710 uint8_t *uuid;
2711 Vector<uint8_t> *drmSessionId;
2712 CHECK(msg->findPointer("uuid", (void**)&uuid));
2713 CHECK(msg->findPointer("drmSessionId", (void**)&drmSessionId));
2714
2715 status = OK;
2716 sp<ICrypto> crypto = NULL;
2717
2718 status = mSource->prepareDrm(uuid, *drmSessionId, &crypto);
2719 if (crypto == NULL) {
2720 ALOGE("onPrepareDrm: mSource->prepareDrm failed. status: %d", status);
2721 return status;
2722 }
2723 ALOGV("onPrepareDrm: mSource->prepareDrm succeeded");
2724
2725 if (mCrypto != NULL) {
2726 ALOGE("onPrepareDrm: Unexpected. Already having mCrypto: %p (%d)",
2727 mCrypto.get(), mCrypto->getStrongCount());
2728 mCrypto.clear();
2729 }
2730
2731 mCrypto = crypto;
2732 mIsDrmProtected = true;
2733 // TODO change to ALOGV
2734 ALOGD("onPrepareDrm: mCrypto: %p (%d)", mCrypto.get(),
2735 (mCrypto != NULL ? mCrypto->getStrongCount() : 0));
2736
2737 return status;
2738}
2739
2740status_t NuPlayer::onReleaseDrm()
2741{
2742 // TODO change to ALOGV
2743 ALOGD("onReleaseDrm ");
2744
Hassan Shojania50b20c92017-02-16 18:28:58 -08002745 if (!mIsDrmProtected) {
2746 ALOGW("onReleaseDrm: Unexpected. mIsDrmProtected is already false.");
2747 }
2748
2749 mIsDrmProtected = false;
Hassan Shojaniacefac142017-02-06 21:02:02 -08002750
2751 status_t status;
2752 if (mCrypto != NULL) {
2753 status=OK;
2754 // first making sure the codecs have released their crypto reference
2755 const sp<DecoderBase> &videoDecoder = getDecoder(false/*audio*/);
2756 if (videoDecoder != NULL) {
2757 status = videoDecoder->releaseCrypto();
2758 ALOGV("onReleaseDrm: video decoder ret: %d", status);
2759 }
2760
2761 const sp<DecoderBase> &audioDecoder = getDecoder(true/*audio*/);
2762 if (audioDecoder != NULL) {
2763 status_t status_audio = audioDecoder->releaseCrypto();
2764 if (status == OK) { // otherwise, returning the first error
2765 status = status_audio;
2766 }
2767 ALOGV("onReleaseDrm: audio decoder ret: %d", status_audio);
2768 }
2769
2770 // TODO change to ALOGV
2771 ALOGD("onReleaseDrm: mCrypto: %p (%d)", mCrypto.get(),
2772 (mCrypto != NULL ? mCrypto->getStrongCount() : 0));
2773 mCrypto.clear();
2774 } else { // mCrypto == NULL
2775 ALOGE("onReleaseDrm: Unexpected. There is no crypto.");
2776 status = INVALID_OPERATION;
2777 }
2778
2779 return status;
2780}
2781// Modular DRM end
Andreas Huberb5f25f02013-02-05 10:14:26 -08002782////////////////////////////////////////////////////////////////////////////////
2783
Chong Zhangced1c2f2014-08-08 15:22:35 -07002784sp<AMessage> NuPlayer::Source::getFormat(bool audio) {
2785 sp<MetaData> meta = getFormatMeta(audio);
2786
2787 if (meta == NULL) {
2788 return NULL;
2789 }
2790
2791 sp<AMessage> msg = new AMessage;
2792
2793 if(convertMetaDataToMessage(meta, &msg) == OK) {
2794 return msg;
2795 }
2796 return NULL;
2797}
2798
Andreas Huber9575c962013-02-05 13:59:56 -08002799void NuPlayer::Source::notifyFlagsChanged(uint32_t flags) {
2800 sp<AMessage> notify = dupNotify();
2801 notify->setInt32("what", kWhatFlagsChanged);
2802 notify->setInt32("flags", flags);
2803 notify->post();
2804}
2805
Chong Zhangced1c2f2014-08-08 15:22:35 -07002806void NuPlayer::Source::notifyVideoSizeChanged(const sp<AMessage> &format) {
Andreas Huber9575c962013-02-05 13:59:56 -08002807 sp<AMessage> notify = dupNotify();
2808 notify->setInt32("what", kWhatVideoSizeChanged);
Chong Zhangced1c2f2014-08-08 15:22:35 -07002809 notify->setMessage("format", format);
Andreas Huber9575c962013-02-05 13:59:56 -08002810 notify->post();
2811}
2812
Andreas Huberec0c5972013-02-05 14:47:13 -08002813void NuPlayer::Source::notifyPrepared(status_t err) {
Hassan Shojaniacefac142017-02-06 21:02:02 -08002814 ALOGV("Source::notifyPrepared %d", err);
Andreas Huber9575c962013-02-05 13:59:56 -08002815 sp<AMessage> notify = dupNotify();
2816 notify->setInt32("what", kWhatPrepared);
Andreas Huberec0c5972013-02-05 14:47:13 -08002817 notify->setInt32("err", err);
Andreas Huber9575c962013-02-05 13:59:56 -08002818 notify->post();
2819}
2820
Hassan Shojaniacefac142017-02-06 21:02:02 -08002821void NuPlayer::Source::notifyDrmInfo(const sp<ABuffer> &drmInfoBuffer)
2822{
2823 ALOGV("Source::notifyDrmInfo");
2824
2825 sp<AMessage> notify = dupNotify();
2826 notify->setInt32("what", kWhatDrmInfo);
2827 notify->setBuffer("drmInfo", drmInfoBuffer);
2828
2829 notify->post();
2830}
2831
Lajos Molnarfcd3e942015-03-31 10:06:48 -07002832void NuPlayer::Source::notifyInstantiateSecureDecoders(const sp<AMessage> &reply) {
2833 sp<AMessage> notify = dupNotify();
2834 notify->setInt32("what", kWhatInstantiateSecureDecoders);
2835 notify->setMessage("reply", reply);
2836 notify->post();
2837}
2838
Andreas Huber84333e02014-02-07 15:36:10 -08002839void NuPlayer::Source::onMessageReceived(const sp<AMessage> & /* msg */) {
Andreas Huberb5f25f02013-02-05 10:14:26 -08002840 TRESPASS();
2841}
2842
Andreas Huberf9334412010-12-15 15:17:42 -08002843} // namespace android