blob: d9a5c261626428b4f8c3653ca2a4e20befa193a5 [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;
Ray Essickd4d00612017-01-03 09:36:27 -08001564
1565 driver->notifyMorePlayingTimeUs((played+500)/1000);
1566 }
Chong Zhangefbb6192015-01-30 17:13:27 -08001567}
1568
Ronghua Wud7988b12014-10-03 15:19:10 -07001569bool NuPlayer::audioDecoderStillNeeded() {
1570 // Audio decoder is no longer needed if it's in shut/shutting down status.
1571 return ((mFlushingAudio != SHUT_DOWN) && (mFlushingAudio != SHUTTING_DOWN_DECODER));
1572}
1573
Andy Hung8d121d42014-10-03 09:53:53 -07001574void NuPlayer::handleFlushComplete(bool audio, bool isDecoder) {
1575 // We wait for both the decoder flush and the renderer flush to complete
1576 // before entering either the FLUSHED or the SHUTTING_DOWN_DECODER state.
1577
1578 mFlushComplete[audio][isDecoder] = true;
1579 if (!mFlushComplete[audio][!isDecoder]) {
1580 return;
1581 }
1582
1583 FlushStatus *state = audio ? &mFlushingAudio : &mFlushingVideo;
1584 switch (*state) {
1585 case FLUSHING_DECODER:
1586 {
1587 *state = FLUSHED;
Andy Hung8d121d42014-10-03 09:53:53 -07001588 break;
1589 }
1590
1591 case FLUSHING_DECODER_SHUTDOWN:
1592 {
1593 *state = SHUTTING_DOWN_DECODER;
1594
1595 ALOGV("initiating %s decoder shutdown", audio ? "audio" : "video");
Andy Hung8d121d42014-10-03 09:53:53 -07001596 getDecoder(audio)->initiateShutdown();
1597 break;
1598 }
1599
1600 default:
1601 // decoder flush completes only occur in a flushing state.
1602 LOG_ALWAYS_FATAL_IF(isDecoder, "decoder flush in invalid state %d", *state);
1603 break;
1604 }
1605}
1606
Andreas Huber3831a062010-12-21 10:22:33 -08001607void NuPlayer::finishFlushIfPossible() {
Wei Jia53904f32014-07-29 10:22:53 -07001608 if (mFlushingAudio != NONE && mFlushingAudio != FLUSHED
1609 && mFlushingAudio != SHUT_DOWN) {
Andreas Huber3831a062010-12-21 10:22:33 -08001610 return;
1611 }
1612
Wei Jia53904f32014-07-29 10:22:53 -07001613 if (mFlushingVideo != NONE && mFlushingVideo != FLUSHED
1614 && mFlushingVideo != SHUT_DOWN) {
Andreas Huber3831a062010-12-21 10:22:33 -08001615 return;
1616 }
1617
Steve Block3856b092011-10-20 11:56:00 +01001618 ALOGV("both audio and video are flushed now.");
Andreas Huber3831a062010-12-21 10:22:33 -08001619
Andreas Huber3831a062010-12-21 10:22:33 -08001620 mFlushingAudio = NONE;
1621 mFlushingVideo = NONE;
Andreas Huber3831a062010-12-21 10:22:33 -08001622
Andy Hung8d121d42014-10-03 09:53:53 -07001623 clearFlushComplete();
1624
Andreas Hubera1f8ab02012-11-30 10:53:22 -08001625 processDeferredActions();
Andreas Huber1aef2112011-01-04 14:01:29 -08001626}
1627
1628void NuPlayer::postScanSources() {
1629 if (mScanSourcesPending) {
1630 return;
1631 }
1632
Lajos Molnar1d15ab52015-03-04 16:46:34 -08001633 sp<AMessage> msg = new AMessage(kWhatScanSources, this);
Andreas Huber1aef2112011-01-04 14:01:29 -08001634 msg->setInt32("generation", mScanSourcesGeneration);
1635 msg->post();
1636
1637 mScanSourcesPending = true;
1638}
1639
Wei Jia41cd4632016-05-13 10:53:30 -07001640void NuPlayer::tryOpenAudioSinkForOffload(
1641 const sp<AMessage> &format, const sp<MetaData> &audioMeta, bool hasVideo) {
Andy Hung202bce12014-12-03 11:47:36 -08001642 // Note: This is called early in NuPlayer to determine whether offloading
1643 // is possible; otherwise the decoders call the renderer openAudioSink directly.
Andy Hung282a7e32014-08-14 15:56:34 -07001644
Andy Hung202bce12014-12-03 11:47:36 -08001645 status_t err = mRenderer->openAudioSink(
Dhananjay Kumarc387f2b2015-08-06 10:43:16 +05301646 format, true /* offloadOnly */, hasVideo,
1647 AUDIO_OUTPUT_FLAG_NONE, &mOffloadAudio, mSource->isStreaming());
Andy Hung202bce12014-12-03 11:47:36 -08001648 if (err != OK) {
1649 // Any failure we turn off mOffloadAudio.
1650 mOffloadAudio = false;
1651 } else if (mOffloadAudio) {
Chong Zhang3b9eb1f2014-10-15 17:05:08 -07001652 sendMetaDataToHal(mAudioSink, audioMeta);
Andy Hung282a7e32014-08-14 15:56:34 -07001653 }
1654}
1655
1656void NuPlayer::closeAudioSink() {
Chong Zhang3b9eb1f2014-10-15 17:05:08 -07001657 mRenderer->closeAudioSink();
Andy Hung282a7e32014-08-14 15:56:34 -07001658}
1659
Wei Jia5031b2f2016-02-25 11:19:31 -08001660void NuPlayer::restartAudio(
Wei Jiabf70feb2016-02-19 15:47:16 -08001661 int64_t currentPositionUs, bool forceNonOffload, bool needsToCreateAudioDecoder) {
Wei Jiaa05f1e32016-03-25 16:31:22 -07001662 if (mAudioDecoder != NULL) {
1663 mAudioDecoder->pause();
1664 mAudioDecoder.clear();
Wei Jia686e8e52017-04-03 14:08:01 -07001665 mAudioDecoderError = false;
Wei Jiaa05f1e32016-03-25 16:31:22 -07001666 ++mAudioDecoderGeneration;
1667 }
Wei Jiabf70feb2016-02-19 15:47:16 -08001668 if (mFlushingAudio == FLUSHING_DECODER) {
1669 mFlushComplete[1 /* audio */][1 /* isDecoder */] = true;
1670 mFlushingAudio = FLUSHED;
1671 finishFlushIfPossible();
1672 } else if (mFlushingAudio == FLUSHING_DECODER_SHUTDOWN
1673 || mFlushingAudio == SHUTTING_DOWN_DECODER) {
1674 mFlushComplete[1 /* audio */][1 /* isDecoder */] = true;
1675 mFlushingAudio = SHUT_DOWN;
1676 finishFlushIfPossible();
1677 needsToCreateAudioDecoder = false;
1678 }
1679 if (mRenderer == NULL) {
1680 return;
1681 }
1682 closeAudioSink();
1683 mRenderer->flush(true /* audio */, false /* notifyComplete */);
1684 if (mVideoDecoder != NULL) {
1685 mRenderer->flush(false /* audio */, false /* notifyComplete */);
1686 }
1687
Wei Jiac5de0912016-11-18 10:22:14 -08001688 performSeek(currentPositionUs, MediaPlayerSeekMode::SEEK_PREVIOUS_SYNC /* mode */);
Wei Jiabf70feb2016-02-19 15:47:16 -08001689
1690 if (forceNonOffload) {
1691 mRenderer->signalDisableOffloadAudio();
1692 mOffloadAudio = false;
1693 }
1694 if (needsToCreateAudioDecoder) {
Wei Jiaa05f1e32016-03-25 16:31:22 -07001695 instantiateDecoder(true /* audio */, &mAudioDecoder, !forceNonOffload);
Wei Jiabf70feb2016-02-19 15:47:16 -08001696 }
1697}
1698
Wei Jia41cd4632016-05-13 10:53:30 -07001699void NuPlayer::determineAudioModeChange(const sp<AMessage> &audioFormat) {
Wei Jiae4d18c72015-07-13 17:58:11 -07001700 if (mSource == NULL || mAudioSink == NULL) {
1701 return;
1702 }
1703
1704 if (mRenderer == NULL) {
1705 ALOGW("No renderer can be used to determine audio mode. Use non-offload for safety.");
1706 mOffloadAudio = false;
1707 return;
1708 }
1709
1710 sp<MetaData> audioMeta = mSource->getFormatMeta(true /* audio */);
1711 sp<AMessage> videoFormat = mSource->getFormat(false /* audio */);
1712 audio_stream_type_t streamType = mAudioSink->getAudioStreamType();
1713 const bool hasVideo = (videoFormat != NULL);
Hassan Shojaniacefac142017-02-06 21:02:02 -08001714 bool canOffload = canOffloadStream(
Wei Jiabf70feb2016-02-19 15:47:16 -08001715 audioMeta, hasVideo, mSource->isStreaming(), streamType)
1716 && (mPlaybackSettings.mSpeed == 1.f && mPlaybackSettings.mPitch == 1.f);
Hassan Shojaniacefac142017-02-06 21:02:02 -08001717
1718 // Modular DRM: Disabling audio offload if the source is protected
1719 if (canOffload && mIsDrmProtected) {
1720 canOffload = false;
1721 ALOGV("determineAudioModeChange: Disabling mOffloadAudio b/c the source is protected.");
1722 }
1723
Wei Jiae4d18c72015-07-13 17:58:11 -07001724 if (canOffload) {
1725 if (!mOffloadAudio) {
1726 mRenderer->signalEnableOffloadAudio();
1727 }
1728 // open audio sink early under offload mode.
Wei Jia41cd4632016-05-13 10:53:30 -07001729 tryOpenAudioSinkForOffload(audioFormat, audioMeta, hasVideo);
Wei Jiae4d18c72015-07-13 17:58:11 -07001730 } else {
Robert Shihe1d70192015-07-23 17:54:13 -07001731 if (mOffloadAudio) {
1732 mRenderer->signalDisableOffloadAudio();
1733 mOffloadAudio = false;
1734 }
Wei Jiae4d18c72015-07-13 17:58:11 -07001735 }
1736}
1737
Wei Jiaa05f1e32016-03-25 16:31:22 -07001738status_t NuPlayer::instantiateDecoder(
1739 bool audio, sp<DecoderBase> *decoder, bool checkAudioModeChange) {
Wei Jia566da802015-08-27 13:59:30 -07001740 // The audio decoder could be cleared by tear down. If still in shut down
1741 // process, no need to create a new audio decoder.
1742 if (*decoder != NULL || (audio && mFlushingAudio == SHUT_DOWN)) {
Andreas Huberf9334412010-12-15 15:17:42 -08001743 return OK;
1744 }
1745
Andreas Huber84066782011-08-16 09:34:26 -07001746 sp<AMessage> format = mSource->getFormat(audio);
Andreas Huberf9334412010-12-15 15:17:42 -08001747
Andreas Huber84066782011-08-16 09:34:26 -07001748 if (format == NULL) {
Robert Shih7350b052015-10-01 15:50:14 -07001749 return UNKNOWN_ERROR;
1750 } else {
1751 status_t err;
1752 if (format->findInt32("err", &err) && err) {
1753 return err;
1754 }
Andreas Huberf9334412010-12-15 15:17:42 -08001755 }
1756
Ronghua Wu8db88132015-04-22 13:51:35 -07001757 format->setInt32("priority", 0 /* realtime */);
1758
Andreas Huber3fe62152011-09-16 15:09:22 -07001759 if (!audio) {
Andreas Huber84066782011-08-16 09:34:26 -07001760 AString mime;
1761 CHECK(format->findString("mime", &mime));
Chong Zhanga7fa1d92014-06-11 14:49:23 -07001762
Lajos Molnar1d15ab52015-03-04 16:46:34 -08001763 sp<AMessage> ccNotify = new AMessage(kWhatClosedCaptionNotify, this);
Chong Zhang341ab6e2015-02-04 13:37:18 -08001764 if (mCCDecoder == NULL) {
1765 mCCDecoder = new CCDecoder(ccNotify);
1766 }
Lajos Molnar09524832014-07-17 14:29:51 -07001767
1768 if (mSourceFlags & Source::FLAG_SECURE) {
1769 format->setInt32("secure", true);
1770 }
Chong Zhang17134602015-01-07 16:14:34 -08001771
1772 if (mSourceFlags & Source::FLAG_PROTECTED) {
1773 format->setInt32("protected", true);
1774 }
Ronghua Wu8db88132015-04-22 13:51:35 -07001775
Ronghua Wuc8a70d32015-04-29 16:26:34 -07001776 float rate = getFrameRate();
1777 if (rate > 0) {
Lajos Molnar3a474aa2015-04-24 17:10:07 -07001778 format->setFloat("operating-rate", rate * mPlaybackSettings.mSpeed);
Ronghua Wu8db88132015-04-22 13:51:35 -07001779 }
Andreas Huber3fe62152011-09-16 15:09:22 -07001780 }
1781
Wei Jiabc2fb722014-07-08 16:37:57 -07001782 if (audio) {
Lajos Molnar1d15ab52015-03-04 16:46:34 -08001783 sp<AMessage> notify = new AMessage(kWhatAudioNotify, this);
Wei Jia88703c32014-08-06 11:24:07 -07001784 ++mAudioDecoderGeneration;
1785 notify->setInt32("generation", mAudioDecoderGeneration);
1786
Wei Jiaa05f1e32016-03-25 16:31:22 -07001787 if (checkAudioModeChange) {
Wei Jia41cd4632016-05-13 10:53:30 -07001788 determineAudioModeChange(format);
Wei Jiaa05f1e32016-03-25 16:31:22 -07001789 }
Wei Jiabc2fb722014-07-08 16:37:57 -07001790 if (mOffloadAudio) {
Wei Jia14532f22015-12-29 11:28:15 -08001791 mSource->setOffloadAudio(true /* offload */);
1792
Haynes Mathew George8b635332015-03-30 17:59:47 -07001793 const bool hasVideo = (mSource->getFormat(false /*audio */) != NULL);
1794 format->setInt32("has-video", hasVideo);
Wei Jiac6cfd702014-11-11 16:33:20 -08001795 *decoder = new DecoderPassThrough(notify, mSource, mRenderer);
Hassan Shojaniacefac142017-02-06 21:02:02 -08001796 ALOGV("instantiateDecoder audio DecoderPassThrough hasVideo: %d", hasVideo);
Wei Jiabc2fb722014-07-08 16:37:57 -07001797 } else {
Wei Jia14532f22015-12-29 11:28:15 -08001798 mSource->setOffloadAudio(false /* offload */);
1799
Wei Jiaf2ae3e12016-10-27 17:10:59 -07001800 *decoder = new Decoder(notify, mSource, mPID, mUID, mRenderer);
Hassan Shojaniacefac142017-02-06 21:02:02 -08001801 ALOGV("instantiateDecoder audio Decoder");
Wei Jiabc2fb722014-07-08 16:37:57 -07001802 }
Wei Jia686e8e52017-04-03 14:08:01 -07001803 mAudioDecoderError = false;
Wei Jiabc2fb722014-07-08 16:37:57 -07001804 } else {
Lajos Molnar1d15ab52015-03-04 16:46:34 -08001805 sp<AMessage> notify = new AMessage(kWhatVideoNotify, this);
Wei Jia88703c32014-08-06 11:24:07 -07001806 ++mVideoDecoderGeneration;
1807 notify->setInt32("generation", mVideoDecoderGeneration);
1808
Chong Zhang7137ec72014-11-12 16:41:05 -08001809 *decoder = new Decoder(
Wei Jiaf2ae3e12016-10-27 17:10:59 -07001810 notify, mSource, mPID, mUID, mRenderer, mSurface, mCCDecoder);
Wei Jia686e8e52017-04-03 14:08:01 -07001811 mVideoDecoderError = false;
Lajos Molnard9fd6312014-11-06 11:00:00 -08001812
1813 // enable FRC if high-quality AV sync is requested, even if not
Lajos Molnar1de1e252015-04-30 18:18:34 -07001814 // directly queuing to display, as this will even improve textureview
Lajos Molnard9fd6312014-11-06 11:00:00 -08001815 // playback.
1816 {
Marco Nelissen96626b72016-12-01 13:58:59 -08001817 if (property_get_bool("persist.sys.media.avsync", false)) {
Lajos Molnard9fd6312014-11-06 11:00:00 -08001818 format->setInt32("auto-frc", 1);
1819 }
1820 }
Wei Jiabc2fb722014-07-08 16:37:57 -07001821 }
Lajos Molnar1cd13982014-01-17 15:12:51 -08001822 (*decoder)->init();
Hassan Shojaniacefac142017-02-06 21:02:02 -08001823
1824 // Modular DRM
1825 if (mIsDrmProtected) {
1826 format->setPointer("crypto", mCrypto.get());
1827 ALOGV("instantiateDecoder: mCrypto: %p (%d) isSecure: %d", mCrypto.get(),
1828 (mCrypto != NULL ? mCrypto->getStrongCount() : 0),
1829 (mSourceFlags & Source::FLAG_SECURE) != 0);
1830 }
1831
Andreas Huber84066782011-08-16 09:34:26 -07001832 (*decoder)->configure(format);
Andreas Huberf9334412010-12-15 15:17:42 -08001833
Praveen Chavanbbaa1442016-04-08 13:33:49 -07001834 if (!audio) {
1835 sp<AMessage> params = new AMessage();
1836 float rate = getFrameRate();
1837 if (rate > 0) {
1838 params->setFloat("frame-rate-total", rate);
1839 }
1840
1841 sp<MetaData> fileMeta = getFileMeta();
1842 if (fileMeta != NULL) {
1843 int32_t videoTemporalLayerCount;
1844 if (fileMeta->findInt32(kKeyTemporalLayerCount, &videoTemporalLayerCount)
1845 && videoTemporalLayerCount > 0) {
1846 params->setInt32("temporal-layer-count", videoTemporalLayerCount);
1847 }
1848 }
1849
1850 if (params->countEntries() > 0) {
1851 (*decoder)->setParameters(params);
1852 }
1853 }
Andreas Huberf9334412010-12-15 15:17:42 -08001854 return OK;
1855}
1856
Chong Zhangced1c2f2014-08-08 15:22:35 -07001857void NuPlayer::updateVideoSize(
1858 const sp<AMessage> &inputFormat,
1859 const sp<AMessage> &outputFormat) {
1860 if (inputFormat == NULL) {
1861 ALOGW("Unknown video size, reporting 0x0!");
1862 notifyListener(MEDIA_SET_VIDEO_SIZE, 0, 0);
1863 return;
1864 }
Wei Jia9737d342016-12-28 14:59:59 -08001865 int32_t err = OK;
1866 inputFormat->findInt32("err", &err);
1867 if (err == -EWOULDBLOCK) {
1868 ALOGW("Video meta is not available yet!");
1869 return;
1870 }
1871 if (err != OK) {
1872 ALOGW("Something is wrong with video meta!");
1873 return;
1874 }
Chong Zhangced1c2f2014-08-08 15:22:35 -07001875
1876 int32_t displayWidth, displayHeight;
Chong Zhangced1c2f2014-08-08 15:22:35 -07001877 if (outputFormat != NULL) {
1878 int32_t width, height;
1879 CHECK(outputFormat->findInt32("width", &width));
1880 CHECK(outputFormat->findInt32("height", &height));
1881
1882 int32_t cropLeft, cropTop, cropRight, cropBottom;
1883 CHECK(outputFormat->findRect(
1884 "crop",
1885 &cropLeft, &cropTop, &cropRight, &cropBottom));
1886
1887 displayWidth = cropRight - cropLeft + 1;
1888 displayHeight = cropBottom - cropTop + 1;
1889
1890 ALOGV("Video output format changed to %d x %d "
1891 "(crop: %d x %d @ (%d, %d))",
1892 width, height,
1893 displayWidth,
1894 displayHeight,
1895 cropLeft, cropTop);
1896 } else {
1897 CHECK(inputFormat->findInt32("width", &displayWidth));
1898 CHECK(inputFormat->findInt32("height", &displayHeight));
1899
1900 ALOGV("Video input format %d x %d", displayWidth, displayHeight);
1901 }
1902
1903 // Take into account sample aspect ratio if necessary:
1904 int32_t sarWidth, sarHeight;
1905 if (inputFormat->findInt32("sar-width", &sarWidth)
Wei Jia095bc252017-01-27 10:16:16 -08001906 && inputFormat->findInt32("sar-height", &sarHeight)
1907 && sarWidth > 0 && sarHeight > 0) {
Chong Zhangced1c2f2014-08-08 15:22:35 -07001908 ALOGV("Sample aspect ratio %d : %d", sarWidth, sarHeight);
1909
1910 displayWidth = (displayWidth * sarWidth) / sarHeight;
1911
1912 ALOGV("display dimensions %d x %d", displayWidth, displayHeight);
Wei Jiadf6c6af2016-09-29 11:27:15 -07001913 } else {
1914 int32_t width, height;
1915 if (inputFormat->findInt32("display-width", &width)
1916 && inputFormat->findInt32("display-height", &height)
1917 && width > 0 && height > 0
1918 && displayWidth > 0 && displayHeight > 0) {
1919 if (displayHeight * (int64_t)width / height > (int64_t)displayWidth) {
1920 displayHeight = (int32_t)(displayWidth * (int64_t)height / width);
1921 } else {
1922 displayWidth = (int32_t)(displayHeight * (int64_t)width / height);
1923 }
1924 ALOGV("Video display width and height are overridden to %d x %d",
1925 displayWidth, displayHeight);
1926 }
Chong Zhangced1c2f2014-08-08 15:22:35 -07001927 }
1928
1929 int32_t rotationDegrees;
1930 if (!inputFormat->findInt32("rotation-degrees", &rotationDegrees)) {
1931 rotationDegrees = 0;
1932 }
1933
1934 if (rotationDegrees == 90 || rotationDegrees == 270) {
1935 int32_t tmp = displayWidth;
1936 displayWidth = displayHeight;
1937 displayHeight = tmp;
1938 }
1939
1940 notifyListener(
1941 MEDIA_SET_VIDEO_SIZE,
1942 displayWidth,
1943 displayHeight);
1944}
1945
Chong Zhangdcb89b32013-08-06 09:44:47 -07001946void NuPlayer::notifyListener(int msg, int ext1, int ext2, const Parcel *in) {
Andreas Huber43c3e6c2011-01-05 12:17:08 -08001947 if (mDriver == NULL) {
Andreas Huberf9334412010-12-15 15:17:42 -08001948 return;
1949 }
1950
Andreas Huber43c3e6c2011-01-05 12:17:08 -08001951 sp<NuPlayerDriver> driver = mDriver.promote();
Andreas Huberf9334412010-12-15 15:17:42 -08001952
Andreas Huber43c3e6c2011-01-05 12:17:08 -08001953 if (driver == NULL) {
Andreas Huberf9334412010-12-15 15:17:42 -08001954 return;
1955 }
1956
Chong Zhangdcb89b32013-08-06 09:44:47 -07001957 driver->notifyListener(msg, ext1, ext2, in);
Andreas Huberf9334412010-12-15 15:17:42 -08001958}
1959
Chong Zhang7137ec72014-11-12 16:41:05 -08001960void NuPlayer::flushDecoder(bool audio, bool needShutdown) {
Andreas Huber14f76722013-01-15 09:04:18 -08001961 ALOGV("[%s] flushDecoder needShutdown=%d",
1962 audio ? "audio" : "video", needShutdown);
1963
Chong Zhang7137ec72014-11-12 16:41:05 -08001964 const sp<DecoderBase> &decoder = getDecoder(audio);
Lajos Molnar87603c02014-08-20 19:25:30 -07001965 if (decoder == NULL) {
Steve Blockdf64d152012-01-04 20:05:49 +00001966 ALOGI("flushDecoder %s without decoder present",
Andreas Huber6e3d3112011-11-28 12:36:11 -08001967 audio ? "audio" : "video");
Lajos Molnar87603c02014-08-20 19:25:30 -07001968 return;
Andreas Huber6e3d3112011-11-28 12:36:11 -08001969 }
1970
Andreas Huber1aef2112011-01-04 14:01:29 -08001971 // Make sure we don't continue to scan sources until we finish flushing.
1972 ++mScanSourcesGeneration;
Chong Zhang3b032b32015-04-17 15:49:06 -07001973 if (mScanSourcesPending) {
Toshikazu Saitocbcbb792015-09-15 14:53:01 +09001974 if (!needShutdown) {
1975 mDeferredActions.push_back(
1976 new SimpleAction(&NuPlayer::performScanSources));
1977 }
Chong Zhang3b032b32015-04-17 15:49:06 -07001978 mScanSourcesPending = false;
1979 }
Andreas Huber1aef2112011-01-04 14:01:29 -08001980
Chong Zhang7137ec72014-11-12 16:41:05 -08001981 decoder->signalFlush();
Andreas Huber1aef2112011-01-04 14:01:29 -08001982
1983 FlushStatus newStatus =
1984 needShutdown ? FLUSHING_DECODER_SHUTDOWN : FLUSHING_DECODER;
1985
Lajos Molnarfcd3e942015-03-31 10:06:48 -07001986 mFlushComplete[audio][false /* isDecoder */] = (mRenderer == NULL);
Andy Hung8d121d42014-10-03 09:53:53 -07001987 mFlushComplete[audio][true /* isDecoder */] = false;
Andreas Huber1aef2112011-01-04 14:01:29 -08001988 if (audio) {
Wei Jia53904f32014-07-29 10:22:53 -07001989 ALOGE_IF(mFlushingAudio != NONE,
1990 "audio flushDecoder() is called in state %d", mFlushingAudio);
Andreas Huber1aef2112011-01-04 14:01:29 -08001991 mFlushingAudio = newStatus;
Andreas Huber1aef2112011-01-04 14:01:29 -08001992 } else {
Wei Jia53904f32014-07-29 10:22:53 -07001993 ALOGE_IF(mFlushingVideo != NONE,
1994 "video flushDecoder() is called in state %d", mFlushingVideo);
Andreas Huber1aef2112011-01-04 14:01:29 -08001995 mFlushingVideo = newStatus;
Andreas Huber1aef2112011-01-04 14:01:29 -08001996 }
1997}
1998
Chong Zhangced1c2f2014-08-08 15:22:35 -07001999void NuPlayer::queueDecoderShutdown(
2000 bool audio, bool video, const sp<AMessage> &reply) {
2001 ALOGI("queueDecoderShutdown audio=%d, video=%d", audio, video);
Andreas Huber84066782011-08-16 09:34:26 -07002002
Chong Zhangced1c2f2014-08-08 15:22:35 -07002003 mDeferredActions.push_back(
Wei Jiafef808d2014-10-31 17:57:05 -07002004 new FlushDecoderAction(
2005 audio ? FLUSH_CMD_SHUTDOWN : FLUSH_CMD_NONE,
2006 video ? FLUSH_CMD_SHUTDOWN : FLUSH_CMD_NONE));
Andreas Huber84066782011-08-16 09:34:26 -07002007
Chong Zhangced1c2f2014-08-08 15:22:35 -07002008 mDeferredActions.push_back(
2009 new SimpleAction(&NuPlayer::performScanSources));
Andreas Huber84066782011-08-16 09:34:26 -07002010
Chong Zhangced1c2f2014-08-08 15:22:35 -07002011 mDeferredActions.push_back(new PostMessageAction(reply));
2012
2013 processDeferredActions();
Andreas Huber84066782011-08-16 09:34:26 -07002014}
2015
James Dong0d268a32012-08-31 12:18:27 -07002016status_t NuPlayer::setVideoScalingMode(int32_t mode) {
2017 mVideoScalingMode = mode;
Lajos Molnar1de1e252015-04-30 18:18:34 -07002018 if (mSurface != NULL) {
2019 status_t ret = native_window_set_scaling_mode(mSurface.get(), mVideoScalingMode);
James Dong0d268a32012-08-31 12:18:27 -07002020 if (ret != OK) {
2021 ALOGE("Failed to set scaling mode (%d): %s",
2022 -ret, strerror(-ret));
2023 return ret;
2024 }
2025 }
2026 return OK;
2027}
2028
Chong Zhangdcb89b32013-08-06 09:44:47 -07002029status_t NuPlayer::getTrackInfo(Parcel* reply) const {
Lajos Molnar1d15ab52015-03-04 16:46:34 -08002030 sp<AMessage> msg = new AMessage(kWhatGetTrackInfo, this);
Chong Zhangdcb89b32013-08-06 09:44:47 -07002031 msg->setPointer("reply", reply);
2032
2033 sp<AMessage> response;
2034 status_t err = msg->postAndAwaitResponse(&response);
2035 return err;
2036}
2037
Robert Shih7c4f0d72014-07-09 18:53:31 -07002038status_t NuPlayer::getSelectedTrack(int32_t type, Parcel* reply) const {
Lajos Molnar1d15ab52015-03-04 16:46:34 -08002039 sp<AMessage> msg = new AMessage(kWhatGetSelectedTrack, this);
Robert Shih7c4f0d72014-07-09 18:53:31 -07002040 msg->setPointer("reply", reply);
2041 msg->setInt32("type", type);
2042
2043 sp<AMessage> response;
2044 status_t err = msg->postAndAwaitResponse(&response);
2045 if (err == OK && response != NULL) {
2046 CHECK(response->findInt32("err", &err));
2047 }
2048 return err;
2049}
2050
Robert Shih6ffb1fd2014-10-29 16:24:32 -07002051status_t NuPlayer::selectTrack(size_t trackIndex, bool select, int64_t timeUs) {
Lajos Molnar1d15ab52015-03-04 16:46:34 -08002052 sp<AMessage> msg = new AMessage(kWhatSelectTrack, this);
Chong Zhangdcb89b32013-08-06 09:44:47 -07002053 msg->setSize("trackIndex", trackIndex);
2054 msg->setInt32("select", select);
Robert Shih6ffb1fd2014-10-29 16:24:32 -07002055 msg->setInt64("timeUs", timeUs);
Chong Zhangdcb89b32013-08-06 09:44:47 -07002056
2057 sp<AMessage> response;
2058 status_t err = msg->postAndAwaitResponse(&response);
2059
Chong Zhang404fced2014-06-11 14:45:31 -07002060 if (err != OK) {
2061 return err;
2062 }
2063
2064 if (!response->findInt32("err", &err)) {
2065 err = OK;
2066 }
2067
Chong Zhangdcb89b32013-08-06 09:44:47 -07002068 return err;
2069}
2070
Ronghua Wua73d9e02014-10-08 15:13:29 -07002071status_t NuPlayer::getCurrentPosition(int64_t *mediaUs) {
2072 sp<Renderer> renderer = mRenderer;
2073 if (renderer == NULL) {
2074 return NO_INIT;
2075 }
2076
2077 return renderer->getCurrentPosition(mediaUs);
2078}
2079
Praveen Chavane1e5d7a2015-05-19 19:09:48 -07002080void NuPlayer::getStats(Vector<sp<AMessage> > *mTrackStats) {
2081 CHECK(mTrackStats != NULL);
2082
2083 mTrackStats->clear();
2084 if (mVideoDecoder != NULL) {
2085 mTrackStats->push_back(mVideoDecoder->getStats());
2086 }
2087 if (mAudioDecoder != NULL) {
2088 mTrackStats->push_back(mAudioDecoder->getStats());
Chong Zhang7137ec72014-11-12 16:41:05 -08002089 }
Ronghua Wua73d9e02014-10-08 15:13:29 -07002090}
2091
Marco Nelissenf0b72b52014-09-16 15:43:44 -07002092sp<MetaData> NuPlayer::getFileMeta() {
2093 return mSource->getFileFormatMeta();
2094}
2095
Ronghua Wuc8a70d32015-04-29 16:26:34 -07002096float NuPlayer::getFrameRate() {
2097 sp<MetaData> meta = mSource->getFormatMeta(false /* audio */);
2098 if (meta == NULL) {
2099 return 0;
2100 }
2101 int32_t rate;
2102 if (!meta->findInt32(kKeyFrameRate, &rate)) {
2103 // fall back to try file meta
2104 sp<MetaData> fileMeta = getFileMeta();
2105 if (fileMeta == NULL) {
2106 ALOGW("source has video meta but not file meta");
2107 return -1;
2108 }
2109 int32_t fileMetaRate;
2110 if (!fileMeta->findInt32(kKeyFrameRate, &fileMetaRate)) {
2111 return -1;
2112 }
2113 return fileMetaRate;
2114 }
2115 return rate;
2116}
2117
Andreas Huberb7c8e912012-11-27 15:02:53 -08002118void NuPlayer::schedulePollDuration() {
Lajos Molnar1d15ab52015-03-04 16:46:34 -08002119 sp<AMessage> msg = new AMessage(kWhatPollDuration, this);
Andreas Huberb7c8e912012-11-27 15:02:53 -08002120 msg->setInt32("generation", mPollDurationGeneration);
2121 msg->post();
2122}
2123
2124void NuPlayer::cancelPollDuration() {
2125 ++mPollDurationGeneration;
2126}
2127
Andreas Hubera1f8ab02012-11-30 10:53:22 -08002128void NuPlayer::processDeferredActions() {
2129 while (!mDeferredActions.empty()) {
2130 // We won't execute any deferred actions until we're no longer in
2131 // an intermediate state, i.e. one more more decoders are currently
2132 // flushing or shutting down.
2133
Andreas Hubera1f8ab02012-11-30 10:53:22 -08002134 if (mFlushingAudio != NONE || mFlushingVideo != NONE) {
2135 // We're currently flushing, postpone the reset until that's
2136 // completed.
2137
2138 ALOGV("postponing action mFlushingAudio=%d, mFlushingVideo=%d",
2139 mFlushingAudio, mFlushingVideo);
2140
2141 break;
2142 }
2143
2144 sp<Action> action = *mDeferredActions.begin();
2145 mDeferredActions.erase(mDeferredActions.begin());
2146
2147 action->execute(this);
2148 }
2149}
2150
Wei Jiac5de0912016-11-18 10:22:14 -08002151void NuPlayer::performSeek(int64_t seekTimeUs, MediaPlayerSeekMode mode) {
2152 ALOGV("performSeek seekTimeUs=%lld us (%.2f secs), mode=%d",
2153 (long long)seekTimeUs, seekTimeUs / 1E6, mode);
Andreas Hubera1f8ab02012-11-30 10:53:22 -08002154
Andy Hungadf34bf2014-09-03 18:22:22 -07002155 if (mSource == NULL) {
2156 // This happens when reset occurs right before the loop mode
2157 // asynchronously seeks to the start of the stream.
2158 LOG_ALWAYS_FATAL_IF(mAudioDecoder != NULL || mVideoDecoder != NULL,
2159 "mSource is NULL and decoders not NULL audio(%p) video(%p)",
2160 mAudioDecoder.get(), mVideoDecoder.get());
2161 return;
2162 }
Robert Shih1a5c8592015-08-04 18:07:44 -07002163 mPreviousSeekTimeUs = seekTimeUs;
Wei Jiac5de0912016-11-18 10:22:14 -08002164 mSource->seekTo(seekTimeUs, mode);
Robert Shihd3b0bbb2014-07-23 15:00:25 -07002165 ++mTimedTextGeneration;
Andreas Hubera1f8ab02012-11-30 10:53:22 -08002166
Andreas Hubera1f8ab02012-11-30 10:53:22 -08002167 // everything's flushed, continue playback.
2168}
2169
Wei Jiafef808d2014-10-31 17:57:05 -07002170void NuPlayer::performDecoderFlush(FlushCommand audio, FlushCommand video) {
2171 ALOGV("performDecoderFlush audio=%d, video=%d", audio, video);
Andreas Hubera1f8ab02012-11-30 10:53:22 -08002172
Wei Jiafef808d2014-10-31 17:57:05 -07002173 if ((audio == FLUSH_CMD_NONE || mAudioDecoder == NULL)
2174 && (video == FLUSH_CMD_NONE || mVideoDecoder == NULL)) {
Andreas Hubera1f8ab02012-11-30 10:53:22 -08002175 return;
2176 }
2177
Wei Jiafef808d2014-10-31 17:57:05 -07002178 if (audio != FLUSH_CMD_NONE && mAudioDecoder != NULL) {
2179 flushDecoder(true /* audio */, (audio == FLUSH_CMD_SHUTDOWN));
Andreas Hubera1f8ab02012-11-30 10:53:22 -08002180 }
2181
Wei Jiafef808d2014-10-31 17:57:05 -07002182 if (video != FLUSH_CMD_NONE && mVideoDecoder != NULL) {
2183 flushDecoder(false /* audio */, (video == FLUSH_CMD_SHUTDOWN));
Andreas Hubera1f8ab02012-11-30 10:53:22 -08002184 }
2185}
2186
2187void NuPlayer::performReset() {
2188 ALOGV("performReset");
2189
2190 CHECK(mAudioDecoder == NULL);
2191 CHECK(mVideoDecoder == NULL);
2192
2193 cancelPollDuration();
2194
2195 ++mScanSourcesGeneration;
2196 mScanSourcesPending = false;
2197
Lajos Molnar09524832014-07-17 14:29:51 -07002198 if (mRendererLooper != NULL) {
2199 if (mRenderer != NULL) {
2200 mRendererLooper->unregisterHandler(mRenderer->id());
2201 }
2202 mRendererLooper->stop();
2203 mRendererLooper.clear();
2204 }
Andreas Hubera1f8ab02012-11-30 10:53:22 -08002205 mRenderer.clear();
Wei Jia57568df2014-09-22 10:16:29 -07002206 ++mRendererGeneration;
Andreas Hubera1f8ab02012-11-30 10:53:22 -08002207
2208 if (mSource != NULL) {
2209 mSource->stop();
Andreas Huberb5f25f02013-02-05 10:14:26 -08002210
Wei Jiac45a4b22016-04-15 15:30:23 -07002211 Mutex::Autolock autoLock(mSourceLock);
Andreas Hubera1f8ab02012-11-30 10:53:22 -08002212 mSource.clear();
2213 }
2214
2215 if (mDriver != NULL) {
2216 sp<NuPlayerDriver> driver = mDriver.promote();
2217 if (driver != NULL) {
2218 driver->notifyResetComplete();
2219 }
2220 }
Andreas Huber57a339c2012-12-03 11:18:00 -08002221
2222 mStarted = false;
Wei Jia8a092d32016-06-03 14:57:24 -07002223 mPrepared = false;
Ronghua Wu64c2d172015-10-07 16:52:19 -07002224 mResetting = false;
Robert Shih0c61a0d2015-07-06 15:09:10 -07002225 mSourceStarted = false;
Hassan Shojaniacefac142017-02-06 21:02:02 -08002226
2227 // Modular DRM
2228 if (mCrypto != NULL) {
2229 // decoders will be flushed before this so their mCrypto would go away on their own
2230 // TODO change to ALOGV
2231 ALOGD("performReset mCrypto: %p (%d)", mCrypto.get(),
2232 (mCrypto != NULL ? mCrypto->getStrongCount() : 0));
2233 mCrypto.clear();
2234 }
2235 mIsDrmProtected = false;
Andreas Hubera1f8ab02012-11-30 10:53:22 -08002236}
2237
2238void NuPlayer::performScanSources() {
2239 ALOGV("performScanSources");
2240
Andreas Huber57a339c2012-12-03 11:18:00 -08002241 if (!mStarted) {
2242 return;
2243 }
2244
Andreas Hubera1f8ab02012-11-30 10:53:22 -08002245 if (mAudioDecoder == NULL || mVideoDecoder == NULL) {
2246 postScanSources();
2247 }
2248}
2249
Lajos Molnar1de1e252015-04-30 18:18:34 -07002250void NuPlayer::performSetSurface(const sp<Surface> &surface) {
Andreas Huber57a339c2012-12-03 11:18:00 -08002251 ALOGV("performSetSurface");
2252
Lajos Molnar1de1e252015-04-30 18:18:34 -07002253 mSurface = surface;
Andreas Huber57a339c2012-12-03 11:18:00 -08002254
2255 // XXX - ignore error from setVideoScalingMode for now
2256 setVideoScalingMode(mVideoScalingMode);
Chong Zhang13d6faa2014-08-22 15:35:28 -07002257
2258 if (mDriver != NULL) {
2259 sp<NuPlayerDriver> driver = mDriver.promote();
2260 if (driver != NULL) {
2261 driver->notifySetSurfaceComplete();
2262 }
2263 }
Andreas Huber57a339c2012-12-03 11:18:00 -08002264}
2265
Chong Zhangf8d71772014-11-26 15:08:34 -08002266void NuPlayer::performResumeDecoders(bool needNotify) {
2267 if (needNotify) {
2268 mResumePending = true;
2269 if (mVideoDecoder == NULL) {
2270 // if audio-only, we can notify seek complete now,
2271 // as the resume operation will be relatively fast.
2272 finishResume();
2273 }
2274 }
2275
Chong Zhang7137ec72014-11-12 16:41:05 -08002276 if (mVideoDecoder != NULL) {
Chong Zhangf8d71772014-11-26 15:08:34 -08002277 // When there is continuous seek, MediaPlayer will cache the seek
2278 // position, and send down new seek request when previous seek is
2279 // complete. Let's wait for at least one video output frame before
2280 // notifying seek complete, so that the video thumbnail gets updated
2281 // when seekbar is dragged.
2282 mVideoDecoder->signalResume(needNotify);
Chong Zhang7137ec72014-11-12 16:41:05 -08002283 }
2284
2285 if (mAudioDecoder != NULL) {
Chong Zhangf8d71772014-11-26 15:08:34 -08002286 mAudioDecoder->signalResume(false /* needNotify */);
2287 }
2288}
2289
2290void NuPlayer::finishResume() {
2291 if (mResumePending) {
2292 mResumePending = false;
Wei Jia1061c9c2015-05-19 16:02:17 -07002293 notifyDriverSeekComplete();
2294 }
2295}
2296
2297void NuPlayer::notifyDriverSeekComplete() {
2298 if (mDriver != NULL) {
2299 sp<NuPlayerDriver> driver = mDriver.promote();
2300 if (driver != NULL) {
2301 driver->notifySeekComplete();
Chong Zhangf8d71772014-11-26 15:08:34 -08002302 }
Chong Zhang7137ec72014-11-12 16:41:05 -08002303 }
2304}
2305
Andreas Huber9575c962013-02-05 13:59:56 -08002306void NuPlayer::onSourceNotify(const sp<AMessage> &msg) {
2307 int32_t what;
2308 CHECK(msg->findInt32("what", &what));
2309
2310 switch (what) {
Lajos Molnarfcd3e942015-03-31 10:06:48 -07002311 case Source::kWhatInstantiateSecureDecoders:
2312 {
2313 if (mSource == NULL) {
2314 // This is a stale notification from a source that was
2315 // asynchronously preparing when the client called reset().
2316 // We handled the reset, the source is gone.
2317 break;
2318 }
2319
2320 sp<AMessage> reply;
2321 CHECK(msg->findMessage("reply", &reply));
2322 status_t err = onInstantiateSecureDecoders();
2323 reply->setInt32("err", err);
2324 reply->post();
2325 break;
2326 }
2327
Andreas Huber9575c962013-02-05 13:59:56 -08002328 case Source::kWhatPrepared:
2329 {
Hassan Shojaniacefac142017-02-06 21:02:02 -08002330 ALOGV("NuPlayer::onSourceNotify Source::kWhatPrepared source: %p", mSource.get());
Andreas Huberb5f28d42013-04-25 15:11:19 -07002331 if (mSource == NULL) {
2332 // This is a stale notification from a source that was
2333 // asynchronously preparing when the client called reset().
2334 // We handled the reset, the source is gone.
2335 break;
2336 }
2337
Andreas Huberec0c5972013-02-05 14:47:13 -08002338 int32_t err;
2339 CHECK(msg->findInt32("err", &err));
2340
Lajos Molnarfcd3e942015-03-31 10:06:48 -07002341 if (err != OK) {
2342 // shut down potential secure codecs in case client never calls reset
2343 mDeferredActions.push_back(
2344 new FlushDecoderAction(FLUSH_CMD_SHUTDOWN /* audio */,
2345 FLUSH_CMD_SHUTDOWN /* video */));
2346 processDeferredActions();
Wei Jia8a092d32016-06-03 14:57:24 -07002347 } else {
2348 mPrepared = true;
Lajos Molnarfcd3e942015-03-31 10:06:48 -07002349 }
2350
Andreas Huber9575c962013-02-05 13:59:56 -08002351 sp<NuPlayerDriver> driver = mDriver.promote();
2352 if (driver != NULL) {
Marco Nelissendd114d12014-05-28 15:23:14 -07002353 // notify duration first, so that it's definitely set when
2354 // the app received the "prepare complete" callback.
2355 int64_t durationUs;
2356 if (mSource->getDuration(&durationUs) == OK) {
2357 driver->notifyDuration(durationUs);
2358 }
Andreas Huberec0c5972013-02-05 14:47:13 -08002359 driver->notifyPrepareCompleted(err);
Andreas Huber9575c962013-02-05 13:59:56 -08002360 }
Andreas Huber99759402013-04-01 14:28:31 -07002361
Andreas Huber9575c962013-02-05 13:59:56 -08002362 break;
2363 }
2364
Hassan Shojaniacefac142017-02-06 21:02:02 -08002365 // Modular DRM
2366 case Source::kWhatDrmInfo:
2367 {
2368 Parcel parcel;
2369 sp<ABuffer> drmInfo;
2370 CHECK(msg->findBuffer("drmInfo", &drmInfo));
2371 parcel.setData(drmInfo->data(), drmInfo->size());
2372
2373 ALOGV("onSourceNotify() kWhatDrmInfo MEDIA_DRM_INFO drmInfo: %p parcel size: %zu",
2374 drmInfo.get(), parcel.dataSize());
2375
2376 notifyListener(MEDIA_DRM_INFO, 0 /* ext1 */, 0 /* ext2 */, &parcel);
2377
2378 break;
2379 }
2380
Andreas Huber9575c962013-02-05 13:59:56 -08002381 case Source::kWhatFlagsChanged:
2382 {
2383 uint32_t flags;
2384 CHECK(msg->findInt32("flags", (int32_t *)&flags));
2385
Chong Zhang4b7069d2013-09-11 12:52:43 -07002386 sp<NuPlayerDriver> driver = mDriver.promote();
2387 if (driver != NULL) {
Hassan Shojaniacefac142017-02-06 21:02:02 -08002388
2389 ALOGV("onSourceNotify() kWhatFlagsChanged FLAG_CAN_PAUSE: %d "
2390 "FLAG_CAN_SEEK_BACKWARD: %d \n\t\t\t\t FLAG_CAN_SEEK_FORWARD: %d "
2391 "FLAG_CAN_SEEK: %d FLAG_DYNAMIC_DURATION: %d \n"
2392 "\t\t\t\t FLAG_SECURE: %d FLAG_PROTECTED: %d",
2393 (flags & Source::FLAG_CAN_PAUSE) != 0,
2394 (flags & Source::FLAG_CAN_SEEK_BACKWARD) != 0,
2395 (flags & Source::FLAG_CAN_SEEK_FORWARD) != 0,
2396 (flags & Source::FLAG_CAN_SEEK) != 0,
2397 (flags & Source::FLAG_DYNAMIC_DURATION) != 0,
2398 (flags & Source::FLAG_SECURE) != 0,
2399 (flags & Source::FLAG_PROTECTED) != 0);
2400
Wei Jia895651b2014-12-10 17:31:52 -08002401 if ((flags & NuPlayer::Source::FLAG_CAN_SEEK) == 0) {
2402 driver->notifyListener(
2403 MEDIA_INFO, MEDIA_INFO_NOT_SEEKABLE, 0);
2404 }
Chong Zhang4b7069d2013-09-11 12:52:43 -07002405 driver->notifyFlagsChanged(flags);
2406 }
2407
Andreas Huber9575c962013-02-05 13:59:56 -08002408 if ((mSourceFlags & Source::FLAG_DYNAMIC_DURATION)
2409 && (!(flags & Source::FLAG_DYNAMIC_DURATION))) {
2410 cancelPollDuration();
2411 } else if (!(mSourceFlags & Source::FLAG_DYNAMIC_DURATION)
2412 && (flags & Source::FLAG_DYNAMIC_DURATION)
2413 && (mAudioDecoder != NULL || mVideoDecoder != NULL)) {
2414 schedulePollDuration();
2415 }
2416
2417 mSourceFlags = flags;
2418 break;
2419 }
2420
2421 case Source::kWhatVideoSizeChanged:
2422 {
Chong Zhangced1c2f2014-08-08 15:22:35 -07002423 sp<AMessage> format;
2424 CHECK(msg->findMessage("format", &format));
Andreas Huber9575c962013-02-05 13:59:56 -08002425
Chong Zhangced1c2f2014-08-08 15:22:35 -07002426 updateVideoSize(format);
Andreas Huber9575c962013-02-05 13:59:56 -08002427 break;
2428 }
2429
Chong Zhang2a3cc9a2014-08-21 17:48:26 -07002430 case Source::kWhatBufferingUpdate:
2431 {
2432 int32_t percentage;
2433 CHECK(msg->findInt32("percentage", &percentage));
2434
2435 notifyListener(MEDIA_BUFFERING_UPDATE, percentage, 0);
2436 break;
2437 }
2438
Chong Zhangefbb6192015-01-30 17:13:27 -08002439 case Source::kWhatPauseOnBufferingStart:
2440 {
2441 // ignore if not playing
Chong Zhang8a048332015-05-06 15:16:28 -07002442 if (mStarted) {
Chong Zhangefbb6192015-01-30 17:13:27 -08002443 ALOGI("buffer low, pausing...");
2444
Chong Zhang8a048332015-05-06 15:16:28 -07002445 mPausedForBuffering = true;
Chong Zhangefbb6192015-01-30 17:13:27 -08002446 onPause();
2447 }
Wei Jiabfe82072016-05-20 09:21:50 -07002448 notifyListener(MEDIA_INFO, MEDIA_INFO_BUFFERING_START, 0);
Roger Jönssonb50e83e2013-01-21 16:26:41 +01002449 break;
2450 }
2451
Chong Zhangefbb6192015-01-30 17:13:27 -08002452 case Source::kWhatResumeOnBufferingEnd:
2453 {
2454 // ignore if not playing
Chong Zhang8a048332015-05-06 15:16:28 -07002455 if (mStarted) {
Chong Zhangefbb6192015-01-30 17:13:27 -08002456 ALOGI("buffer ready, resuming...");
2457
Chong Zhang8a048332015-05-06 15:16:28 -07002458 mPausedForBuffering = false;
2459
2460 // do not resume yet if client didn't unpause
2461 if (!mPausedByClient) {
2462 onResume();
2463 }
Chong Zhangefbb6192015-01-30 17:13:27 -08002464 }
Wei Jia3bed45a2016-02-17 11:06:47 -08002465 notifyListener(MEDIA_INFO, MEDIA_INFO_BUFFERING_END, 0);
Roger Jönssonb50e83e2013-01-21 16:26:41 +01002466 break;
2467 }
2468
Chong Zhangefbb6192015-01-30 17:13:27 -08002469 case Source::kWhatCacheStats:
2470 {
2471 int32_t kbps;
2472 CHECK(msg->findInt32("bandwidth", &kbps));
2473
2474 notifyListener(MEDIA_INFO, MEDIA_INFO_NETWORK_BANDWIDTH, kbps);
2475 break;
2476 }
2477
Chong Zhangdcb89b32013-08-06 09:44:47 -07002478 case Source::kWhatSubtitleData:
2479 {
2480 sp<ABuffer> buffer;
2481 CHECK(msg->findBuffer("buffer", &buffer));
2482
Chong Zhang404fced2014-06-11 14:45:31 -07002483 sendSubtitleData(buffer, 0 /* baseIndex */);
Chong Zhangdcb89b32013-08-06 09:44:47 -07002484 break;
2485 }
2486
Robert Shih08528432015-04-08 09:06:54 -07002487 case Source::kWhatTimedMetaData:
2488 {
2489 sp<ABuffer> buffer;
2490 if (!msg->findBuffer("buffer", &buffer)) {
2491 notifyListener(MEDIA_INFO, MEDIA_INFO_METADATA_UPDATE, 0);
2492 } else {
2493 sendTimedMetaData(buffer);
2494 }
2495 break;
2496 }
2497
Robert Shihd3b0bbb2014-07-23 15:00:25 -07002498 case Source::kWhatTimedTextData:
2499 {
2500 int32_t generation;
2501 if (msg->findInt32("generation", &generation)
2502 && generation != mTimedTextGeneration) {
2503 break;
2504 }
2505
2506 sp<ABuffer> buffer;
2507 CHECK(msg->findBuffer("buffer", &buffer));
2508
2509 sp<NuPlayerDriver> driver = mDriver.promote();
2510 if (driver == NULL) {
2511 break;
2512 }
2513
2514 int posMs;
2515 int64_t timeUs, posUs;
2516 driver->getCurrentPosition(&posMs);
Patrik2 Carlsson24d484b2015-01-27 16:49:45 +01002517 posUs = (int64_t) posMs * 1000ll;
Robert Shihd3b0bbb2014-07-23 15:00:25 -07002518 CHECK(buffer->meta()->findInt64("timeUs", &timeUs));
2519
2520 if (posUs < timeUs) {
2521 if (!msg->findInt32("generation", &generation)) {
2522 msg->setInt32("generation", mTimedTextGeneration);
2523 }
2524 msg->post(timeUs - posUs);
2525 } else {
2526 sendTimedTextData(buffer);
2527 }
2528 break;
2529 }
2530
Andreas Huber14f76722013-01-15 09:04:18 -08002531 case Source::kWhatQueueDecoderShutdown:
2532 {
2533 int32_t audio, video;
2534 CHECK(msg->findInt32("audio", &audio));
2535 CHECK(msg->findInt32("video", &video));
2536
2537 sp<AMessage> reply;
2538 CHECK(msg->findMessage("reply", &reply));
2539
2540 queueDecoderShutdown(audio, video, reply);
2541 break;
2542 }
2543
Ronghua Wu80276872014-08-28 15:50:29 -07002544 case Source::kWhatDrmNoLicense:
2545 {
2546 notifyListener(MEDIA_ERROR, MEDIA_ERROR_UNKNOWN, ERROR_DRM_NO_LICENSE);
2547 break;
2548 }
2549
Andreas Huber9575c962013-02-05 13:59:56 -08002550 default:
2551 TRESPASS();
2552 }
2553}
2554
Chong Zhanga7fa1d92014-06-11 14:49:23 -07002555void NuPlayer::onClosedCaptionNotify(const sp<AMessage> &msg) {
2556 int32_t what;
2557 CHECK(msg->findInt32("what", &what));
2558
2559 switch (what) {
2560 case NuPlayer::CCDecoder::kWhatClosedCaptionData:
2561 {
2562 sp<ABuffer> buffer;
2563 CHECK(msg->findBuffer("buffer", &buffer));
2564
2565 size_t inbandTracks = 0;
2566 if (mSource != NULL) {
2567 inbandTracks = mSource->getTrackCount();
2568 }
2569
2570 sendSubtitleData(buffer, inbandTracks);
2571 break;
2572 }
2573
2574 case NuPlayer::CCDecoder::kWhatTrackAdded:
2575 {
2576 notifyListener(MEDIA_INFO, MEDIA_INFO_METADATA_UPDATE, 0);
2577
2578 break;
2579 }
2580
2581 default:
2582 TRESPASS();
2583 }
2584
2585
2586}
2587
Chong Zhang404fced2014-06-11 14:45:31 -07002588void NuPlayer::sendSubtitleData(const sp<ABuffer> &buffer, int32_t baseIndex) {
2589 int32_t trackIndex;
2590 int64_t timeUs, durationUs;
2591 CHECK(buffer->meta()->findInt32("trackIndex", &trackIndex));
2592 CHECK(buffer->meta()->findInt64("timeUs", &timeUs));
2593 CHECK(buffer->meta()->findInt64("durationUs", &durationUs));
2594
2595 Parcel in;
2596 in.writeInt32(trackIndex + baseIndex);
2597 in.writeInt64(timeUs);
2598 in.writeInt64(durationUs);
2599 in.writeInt32(buffer->size());
2600 in.writeInt32(buffer->size());
2601 in.write(buffer->data(), buffer->size());
2602
2603 notifyListener(MEDIA_SUBTITLE_DATA, 0, 0, &in);
2604}
Robert Shihd3b0bbb2014-07-23 15:00:25 -07002605
Robert Shih08528432015-04-08 09:06:54 -07002606void NuPlayer::sendTimedMetaData(const sp<ABuffer> &buffer) {
2607 int64_t timeUs;
2608 CHECK(buffer->meta()->findInt64("timeUs", &timeUs));
2609
2610 Parcel in;
2611 in.writeInt64(timeUs);
2612 in.writeInt32(buffer->size());
2613 in.writeInt32(buffer->size());
2614 in.write(buffer->data(), buffer->size());
2615
2616 notifyListener(MEDIA_META_DATA, 0, 0, &in);
2617}
2618
Robert Shihd3b0bbb2014-07-23 15:00:25 -07002619void NuPlayer::sendTimedTextData(const sp<ABuffer> &buffer) {
2620 const void *data;
2621 size_t size = 0;
2622 int64_t timeUs;
Marco Nelissen55e2f4c2015-09-04 15:57:15 -07002623 int32_t flag = TextDescriptions::IN_BAND_TEXT_3GPP;
Robert Shihd3b0bbb2014-07-23 15:00:25 -07002624
2625 AString mime;
2626 CHECK(buffer->meta()->findString("mime", &mime));
2627 CHECK(strcasecmp(mime.c_str(), MEDIA_MIMETYPE_TEXT_3GPP) == 0);
2628
2629 data = buffer->data();
2630 size = buffer->size();
2631
2632 Parcel parcel;
2633 if (size > 0) {
2634 CHECK(buffer->meta()->findInt64("timeUs", &timeUs));
Marco Nelissen55e2f4c2015-09-04 15:57:15 -07002635 int32_t global = 0;
2636 if (buffer->meta()->findInt32("global", &global) && global) {
2637 flag |= TextDescriptions::GLOBAL_DESCRIPTIONS;
2638 } else {
2639 flag |= TextDescriptions::LOCAL_DESCRIPTIONS;
2640 }
Robert Shihd3b0bbb2014-07-23 15:00:25 -07002641 TextDescriptions::getParcelOfDescriptions(
2642 (const uint8_t *)data, size, flag, timeUs / 1000, &parcel);
2643 }
2644
2645 if ((parcel.dataSize() > 0)) {
2646 notifyListener(MEDIA_TIMED_TEXT, 0, 0, &parcel);
2647 } else { // send an empty timed text
2648 notifyListener(MEDIA_TIMED_TEXT, 0, 0);
2649 }
2650}
Hassan Shojaniacefac142017-02-06 21:02:02 -08002651
2652// Modular DRM begin
2653status_t NuPlayer::prepareDrm(const uint8_t uuid[16], const Vector<uint8_t> &drmSessionId)
2654{
2655 ALOGV("prepareDrm ");
2656
2657 // Passing to the looper anyway; called in a pre-config prepared state so no race on mCrypto
2658 sp<AMessage> msg = new AMessage(kWhatPrepareDrm, this);
2659 // synchronous call so just passing the address but with local copies of "const" args
2660 uint8_t UUID[16];
2661 memcpy(UUID, uuid, sizeof(UUID));
2662 Vector<uint8_t> sessionId = drmSessionId;
2663 msg->setPointer("uuid", (void*)UUID);
2664 msg->setPointer("drmSessionId", (void*)&sessionId);
2665
2666 sp<AMessage> response;
2667 status_t status = msg->postAndAwaitResponse(&response);
2668
2669 if (status == OK && response != NULL) {
2670 CHECK(response->findInt32("status", &status));
2671 ALOGV("prepareDrm ret: %d ", status);
2672 } else {
2673 ALOGE("prepareDrm err: %d", status);
2674 }
2675
2676 return status;
2677}
2678
2679status_t NuPlayer::releaseDrm()
2680{
2681 ALOGV("releaseDrm ");
2682
2683 sp<AMessage> msg = new AMessage(kWhatReleaseDrm, this);
2684
2685 sp<AMessage> response;
2686 status_t status = msg->postAndAwaitResponse(&response);
2687
2688 if (status == OK && response != NULL) {
2689 CHECK(response->findInt32("status", &status));
2690 ALOGV("releaseDrm ret: %d ", status);
2691 } else {
2692 ALOGE("releaseDrm err: %d", status);
2693 }
2694
2695 return status;
2696}
2697
2698status_t NuPlayer::onPrepareDrm(const sp<AMessage> &msg)
2699{
2700 // TODO change to ALOGV
2701 ALOGD("onPrepareDrm ");
2702
2703 status_t status = INVALID_OPERATION;
2704 if (mSource == NULL) {
2705 ALOGE("onPrepareDrm: No source. onPrepareDrm failed with %d.", status);
2706 return status;
2707 }
2708
2709 uint8_t *uuid;
2710 Vector<uint8_t> *drmSessionId;
2711 CHECK(msg->findPointer("uuid", (void**)&uuid));
2712 CHECK(msg->findPointer("drmSessionId", (void**)&drmSessionId));
2713
2714 status = OK;
2715 sp<ICrypto> crypto = NULL;
2716
2717 status = mSource->prepareDrm(uuid, *drmSessionId, &crypto);
2718 if (crypto == NULL) {
2719 ALOGE("onPrepareDrm: mSource->prepareDrm failed. status: %d", status);
2720 return status;
2721 }
2722 ALOGV("onPrepareDrm: mSource->prepareDrm succeeded");
2723
2724 if (mCrypto != NULL) {
2725 ALOGE("onPrepareDrm: Unexpected. Already having mCrypto: %p (%d)",
2726 mCrypto.get(), mCrypto->getStrongCount());
2727 mCrypto.clear();
2728 }
2729
2730 mCrypto = crypto;
2731 mIsDrmProtected = true;
2732 // TODO change to ALOGV
2733 ALOGD("onPrepareDrm: mCrypto: %p (%d)", mCrypto.get(),
2734 (mCrypto != NULL ? mCrypto->getStrongCount() : 0));
2735
2736 return status;
2737}
2738
2739status_t NuPlayer::onReleaseDrm()
2740{
2741 // TODO change to ALOGV
2742 ALOGD("onReleaseDrm ");
2743
Hassan Shojania50b20c92017-02-16 18:28:58 -08002744 if (!mIsDrmProtected) {
2745 ALOGW("onReleaseDrm: Unexpected. mIsDrmProtected is already false.");
2746 }
2747
2748 mIsDrmProtected = false;
Hassan Shojaniacefac142017-02-06 21:02:02 -08002749
2750 status_t status;
2751 if (mCrypto != NULL) {
2752 status=OK;
2753 // first making sure the codecs have released their crypto reference
2754 const sp<DecoderBase> &videoDecoder = getDecoder(false/*audio*/);
2755 if (videoDecoder != NULL) {
2756 status = videoDecoder->releaseCrypto();
2757 ALOGV("onReleaseDrm: video decoder ret: %d", status);
2758 }
2759
2760 const sp<DecoderBase> &audioDecoder = getDecoder(true/*audio*/);
2761 if (audioDecoder != NULL) {
2762 status_t status_audio = audioDecoder->releaseCrypto();
2763 if (status == OK) { // otherwise, returning the first error
2764 status = status_audio;
2765 }
2766 ALOGV("onReleaseDrm: audio decoder ret: %d", status_audio);
2767 }
2768
2769 // TODO change to ALOGV
2770 ALOGD("onReleaseDrm: mCrypto: %p (%d)", mCrypto.get(),
2771 (mCrypto != NULL ? mCrypto->getStrongCount() : 0));
2772 mCrypto.clear();
2773 } else { // mCrypto == NULL
2774 ALOGE("onReleaseDrm: Unexpected. There is no crypto.");
2775 status = INVALID_OPERATION;
2776 }
2777
2778 return status;
2779}
2780// Modular DRM end
Andreas Huberb5f25f02013-02-05 10:14:26 -08002781////////////////////////////////////////////////////////////////////////////////
2782
Chong Zhangced1c2f2014-08-08 15:22:35 -07002783sp<AMessage> NuPlayer::Source::getFormat(bool audio) {
2784 sp<MetaData> meta = getFormatMeta(audio);
2785
2786 if (meta == NULL) {
2787 return NULL;
2788 }
2789
2790 sp<AMessage> msg = new AMessage;
2791
2792 if(convertMetaDataToMessage(meta, &msg) == OK) {
2793 return msg;
2794 }
2795 return NULL;
2796}
2797
Andreas Huber9575c962013-02-05 13:59:56 -08002798void NuPlayer::Source::notifyFlagsChanged(uint32_t flags) {
2799 sp<AMessage> notify = dupNotify();
2800 notify->setInt32("what", kWhatFlagsChanged);
2801 notify->setInt32("flags", flags);
2802 notify->post();
2803}
2804
Chong Zhangced1c2f2014-08-08 15:22:35 -07002805void NuPlayer::Source::notifyVideoSizeChanged(const sp<AMessage> &format) {
Andreas Huber9575c962013-02-05 13:59:56 -08002806 sp<AMessage> notify = dupNotify();
2807 notify->setInt32("what", kWhatVideoSizeChanged);
Chong Zhangced1c2f2014-08-08 15:22:35 -07002808 notify->setMessage("format", format);
Andreas Huber9575c962013-02-05 13:59:56 -08002809 notify->post();
2810}
2811
Andreas Huberec0c5972013-02-05 14:47:13 -08002812void NuPlayer::Source::notifyPrepared(status_t err) {
Hassan Shojaniacefac142017-02-06 21:02:02 -08002813 ALOGV("Source::notifyPrepared %d", err);
Andreas Huber9575c962013-02-05 13:59:56 -08002814 sp<AMessage> notify = dupNotify();
2815 notify->setInt32("what", kWhatPrepared);
Andreas Huberec0c5972013-02-05 14:47:13 -08002816 notify->setInt32("err", err);
Andreas Huber9575c962013-02-05 13:59:56 -08002817 notify->post();
2818}
2819
Hassan Shojaniacefac142017-02-06 21:02:02 -08002820void NuPlayer::Source::notifyDrmInfo(const sp<ABuffer> &drmInfoBuffer)
2821{
2822 ALOGV("Source::notifyDrmInfo");
2823
2824 sp<AMessage> notify = dupNotify();
2825 notify->setInt32("what", kWhatDrmInfo);
2826 notify->setBuffer("drmInfo", drmInfoBuffer);
2827
2828 notify->post();
2829}
2830
Lajos Molnarfcd3e942015-03-31 10:06:48 -07002831void NuPlayer::Source::notifyInstantiateSecureDecoders(const sp<AMessage> &reply) {
2832 sp<AMessage> notify = dupNotify();
2833 notify->setInt32("what", kWhatInstantiateSecureDecoders);
2834 notify->setMessage("reply", reply);
2835 notify->post();
2836}
2837
Andreas Huber84333e02014-02-07 15:36:10 -08002838void NuPlayer::Source::onMessageReceived(const sp<AMessage> & /* msg */) {
Andreas Huberb5f25f02013-02-05 10:14:26 -08002839 TRESPASS();
2840}
2841
Andreas Huberf9334412010-12-15 15:17:42 -08002842} // namespace android