blob: d04877769746f433cd890b186931522551a70c52 [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) {
Wei Jia2f6d8c52017-04-11 09:50:31 -07001162 if (mVideoDecoderError || mSource->getFormat(false /* audio */) == NULL
1163 || mSurface == NULL) {
Wei Jia686e8e52017-04-03 14:08:01 -07001164 // When both audio and video have error, or this stream has only audio
1165 // which has error, notify client of error.
1166 notifyListener(MEDIA_ERROR, MEDIA_ERROR_UNKNOWN, err);
1167 } else {
1168 // Only audio track has error. Video track could be still good to play.
1169 notifyListener(MEDIA_INFO, MEDIA_INFO_PLAY_AUDIO_ERROR, err);
1170 }
1171 mAudioDecoderError = true;
1172 } else {
Wei Jia2f6d8c52017-04-11 09:50:31 -07001173 if (mAudioDecoderError || mSource->getFormat(true /* audio */) == NULL
1174 || mAudioSink == NULL) {
Wei Jia686e8e52017-04-03 14:08:01 -07001175 // When both audio and video have error, or this stream has only video
1176 // which has error, notify client of error.
1177 notifyListener(MEDIA_ERROR, MEDIA_ERROR_UNKNOWN, err);
1178 } else {
1179 // Only video track has error. Audio track could be still good to play.
1180 notifyListener(MEDIA_INFO, MEDIA_INFO_PLAY_VIDEO_ERROR, err);
1181 }
1182 mVideoDecoderError = true;
1183 }
1184 }
Lajos Molnar1cd13982014-01-17 15:12:51 -08001185 } else {
1186 ALOGV("Unhandled decoder notification %d '%c%c%c%c'.",
Andreas Hubera1f8ab02012-11-30 10:53:22 -08001187 what,
1188 what >> 24,
1189 (what >> 16) & 0xff,
1190 (what >> 8) & 0xff,
1191 what & 0xff);
Andreas Huberf9334412010-12-15 15:17:42 -08001192 }
1193
1194 break;
1195 }
1196
1197 case kWhatRendererNotify:
1198 {
Wei Jia57568df2014-09-22 10:16:29 -07001199 int32_t requesterGeneration = mRendererGeneration - 1;
1200 CHECK(msg->findInt32("generation", &requesterGeneration));
1201 if (requesterGeneration != mRendererGeneration) {
1202 ALOGV("got message from old renderer, generation(%d:%d)",
1203 requesterGeneration, mRendererGeneration);
1204 return;
1205 }
1206
Andreas Huberf9334412010-12-15 15:17:42 -08001207 int32_t what;
1208 CHECK(msg->findInt32("what", &what));
1209
1210 if (what == Renderer::kWhatEOS) {
1211 int32_t audio;
1212 CHECK(msg->findInt32("audio", &audio));
1213
Andreas Huberc92fd242011-08-16 13:48:44 -07001214 int32_t finalResult;
1215 CHECK(msg->findInt32("finalResult", &finalResult));
1216
Andreas Huberf9334412010-12-15 15:17:42 -08001217 if (audio) {
1218 mAudioEOS = true;
1219 } else {
1220 mVideoEOS = true;
1221 }
1222
Andreas Huberc92fd242011-08-16 13:48:44 -07001223 if (finalResult == ERROR_END_OF_STREAM) {
Steve Block3856b092011-10-20 11:56:00 +01001224 ALOGV("reached %s EOS", audio ? "audio" : "video");
Andreas Huberc92fd242011-08-16 13:48:44 -07001225 } else {
Steve Block29357bc2012-01-06 19:20:56 +00001226 ALOGE("%s track encountered an error (%d)",
Andreas Huberc92fd242011-08-16 13:48:44 -07001227 audio ? "audio" : "video", finalResult);
1228
1229 notifyListener(
1230 MEDIA_ERROR, MEDIA_ERROR_UNKNOWN, finalResult);
1231 }
Andreas Huberf9334412010-12-15 15:17:42 -08001232
1233 if ((mAudioEOS || mAudioDecoder == NULL)
1234 && (mVideoEOS || mVideoDecoder == NULL)) {
1235 notifyListener(MEDIA_PLAYBACK_COMPLETE, 0, 0);
1236 }
Andreas Huber3fe62152011-09-16 15:09:22 -07001237 } else if (what == Renderer::kWhatFlushComplete) {
Andreas Huberf9334412010-12-15 15:17:42 -08001238 int32_t audio;
1239 CHECK(msg->findInt32("audio", &audio));
1240
Wei Jia3261f0d2015-10-08 13:58:33 -07001241 if (audio) {
1242 mAudioEOS = false;
1243 } else {
1244 mVideoEOS = false;
1245 }
1246
Steve Block3856b092011-10-20 11:56:00 +01001247 ALOGV("renderer %s flush completed.", audio ? "audio" : "video");
Wei Jiada4252f2015-07-14 18:15:28 -07001248 if (audio && (mFlushingAudio == NONE || mFlushingAudio == FLUSHED
1249 || mFlushingAudio == SHUT_DOWN)) {
1250 // Flush has been handled by tear down.
1251 break;
1252 }
Andy Hung8d121d42014-10-03 09:53:53 -07001253 handleFlushComplete(audio, false /* isDecoder */);
1254 finishFlushIfPossible();
James Dongf57b4ea2012-07-20 13:38:36 -07001255 } else if (what == Renderer::kWhatVideoRenderingStart) {
1256 notifyListener(MEDIA_INFO, MEDIA_INFO_RENDERING_START, 0);
Lajos Molnarcbaffcf2013-08-14 18:30:38 -07001257 } else if (what == Renderer::kWhatMediaRenderingStart) {
1258 ALOGV("media rendering started");
1259 notifyListener(MEDIA_STARTED, 0, 0);
Ronghua Wufaeb0f22015-05-21 12:20:21 -07001260 } else if (what == Renderer::kWhatAudioTearDown) {
Ronghua Wu08529172014-10-02 16:55:52 -07001261 int32_t reason;
1262 CHECK(msg->findInt32("reason", &reason));
Ronghua Wufaeb0f22015-05-21 12:20:21 -07001263 ALOGV("Tear down audio with reason %d.", reason);
Wei Jia8456ddd2016-04-22 14:46:43 -07001264 if (reason == Renderer::kDueToTimeout && !(mPaused && mOffloadAudio)) {
1265 // TimeoutWhenPaused is only for offload mode.
1266 ALOGW("Receive a stale message for teardown.");
1267 break;
1268 }
Wei Jiada4252f2015-07-14 18:15:28 -07001269 int64_t positionUs;
Robert Shih1a5c8592015-08-04 18:07:44 -07001270 if (!msg->findInt64("positionUs", &positionUs)) {
1271 positionUs = mPreviousSeekTimeUs;
1272 }
Ronghua Wufaeb0f22015-05-21 12:20:21 -07001273
Wei Jia5031b2f2016-02-25 11:19:31 -08001274 restartAudio(
Wei Jiaa05f1e32016-03-25 16:31:22 -07001275 positionUs, reason == Renderer::kForceNonOffload /* forceNonOffload */,
1276 reason != Renderer::kDueToTimeout /* needsToCreateAudioDecoder */);
Andreas Huberf9334412010-12-15 15:17:42 -08001277 }
1278 break;
1279 }
1280
1281 case kWhatMoreDataQueued:
1282 {
1283 break;
1284 }
1285
Andreas Huber1aef2112011-01-04 14:01:29 -08001286 case kWhatReset:
1287 {
Steve Block3856b092011-10-20 11:56:00 +01001288 ALOGV("kWhatReset");
Andreas Huber1aef2112011-01-04 14:01:29 -08001289
Ronghua Wu64c2d172015-10-07 16:52:19 -07001290 mResetting = true;
1291
Andreas Hubera1f8ab02012-11-30 10:53:22 -08001292 mDeferredActions.push_back(
Wei Jiafef808d2014-10-31 17:57:05 -07001293 new FlushDecoderAction(
1294 FLUSH_CMD_SHUTDOWN /* audio */,
1295 FLUSH_CMD_SHUTDOWN /* video */));
Andreas Huberb7c8e912012-11-27 15:02:53 -08001296
Andreas Hubera1f8ab02012-11-30 10:53:22 -08001297 mDeferredActions.push_back(
1298 new SimpleAction(&NuPlayer::performReset));
Andreas Huberb58ce9f2011-11-28 16:27:35 -08001299
Andreas Hubera1f8ab02012-11-30 10:53:22 -08001300 processDeferredActions();
Andreas Huber1aef2112011-01-04 14:01:29 -08001301 break;
1302 }
1303
Andreas Huber43c3e6c2011-01-05 12:17:08 -08001304 case kWhatSeek:
1305 {
1306 int64_t seekTimeUs;
Wei Jiac5de0912016-11-18 10:22:14 -08001307 int32_t mode;
Wei Jiae427abf2014-09-22 15:21:11 -07001308 int32_t needNotify;
Andreas Huber43c3e6c2011-01-05 12:17:08 -08001309 CHECK(msg->findInt64("seekTimeUs", &seekTimeUs));
Wei Jiac5de0912016-11-18 10:22:14 -08001310 CHECK(msg->findInt32("mode", &mode));
Wei Jiae427abf2014-09-22 15:21:11 -07001311 CHECK(msg->findInt32("needNotify", &needNotify));
Andreas Huber43c3e6c2011-01-05 12:17:08 -08001312
Wei Jiac5de0912016-11-18 10:22:14 -08001313 ALOGV("kWhatSeek seekTimeUs=%lld us, mode=%d, needNotify=%d",
1314 (long long)seekTimeUs, mode, needNotify);
Andreas Huber43c3e6c2011-01-05 12:17:08 -08001315
Wei Jia1061c9c2015-05-19 16:02:17 -07001316 if (!mStarted) {
1317 // Seek before the player is started. In order to preview video,
1318 // need to start the player and pause it. This branch is called
1319 // only once if needed. After the player is started, any seek
1320 // operation will go through normal path.
Robert Shih0c61a0d2015-07-06 15:09:10 -07001321 // Audio-only cases are handled separately.
Wei Jiac5de0912016-11-18 10:22:14 -08001322 onStart(seekTimeUs, (MediaPlayerSeekMode)mode);
Robert Shih0c61a0d2015-07-06 15:09:10 -07001323 if (mStarted) {
1324 onPause();
1325 mPausedByClient = true;
1326 }
Wei Jia1061c9c2015-05-19 16:02:17 -07001327 if (needNotify) {
1328 notifyDriverSeekComplete();
1329 }
1330 break;
1331 }
1332
Andreas Hubera1f8ab02012-11-30 10:53:22 -08001333 mDeferredActions.push_back(
Wei Jiafef808d2014-10-31 17:57:05 -07001334 new FlushDecoderAction(FLUSH_CMD_FLUSH /* audio */,
1335 FLUSH_CMD_FLUSH /* video */));
Andreas Huber43c3e6c2011-01-05 12:17:08 -08001336
Wei Jiae427abf2014-09-22 15:21:11 -07001337 mDeferredActions.push_back(
Wei Jiac5de0912016-11-18 10:22:14 -08001338 new SeekAction(seekTimeUs, (MediaPlayerSeekMode)mode));
Andreas Huber43c3e6c2011-01-05 12:17:08 -08001339
Chong Zhangf8d71772014-11-26 15:08:34 -08001340 // After a flush without shutdown, decoder is paused.
1341 // Don't resume it until source seek is done, otherwise it could
Chong Zhang7137ec72014-11-12 16:41:05 -08001342 // start pulling stale data too soon.
1343 mDeferredActions.push_back(
Chong Zhangf8d71772014-11-26 15:08:34 -08001344 new ResumeDecoderAction(needNotify));
Chong Zhang7137ec72014-11-12 16:41:05 -08001345
Andreas Hubera1f8ab02012-11-30 10:53:22 -08001346 processDeferredActions();
Andreas Huber43c3e6c2011-01-05 12:17:08 -08001347 break;
1348 }
1349
Andreas Huberb4082222011-01-20 15:23:04 -08001350 case kWhatPause:
1351 {
Chong Zhangefbb6192015-01-30 17:13:27 -08001352 onPause();
1353 mPausedByClient = true;
Andreas Huberb4082222011-01-20 15:23:04 -08001354 break;
1355 }
1356
Andreas Huberb5f25f02013-02-05 10:14:26 -08001357 case kWhatSourceNotify:
1358 {
Andreas Huber9575c962013-02-05 13:59:56 -08001359 onSourceNotify(msg);
Andreas Huberb5f25f02013-02-05 10:14:26 -08001360 break;
1361 }
1362
Chong Zhanga7fa1d92014-06-11 14:49:23 -07001363 case kWhatClosedCaptionNotify:
1364 {
1365 onClosedCaptionNotify(msg);
1366 break;
1367 }
1368
Hassan Shojaniacefac142017-02-06 21:02:02 -08001369 case kWhatPrepareDrm:
1370 {
1371 status_t status = onPrepareDrm(msg);
1372
1373 sp<AMessage> response = new AMessage;
1374 response->setInt32("status", status);
1375 sp<AReplyToken> replyID;
1376 CHECK(msg->senderAwaitsResponse(&replyID));
1377 response->postReply(replyID);
1378 break;
1379 }
1380
1381 case kWhatReleaseDrm:
1382 {
1383 status_t status = onReleaseDrm();
1384
1385 sp<AMessage> response = new AMessage;
1386 response->setInt32("status", status);
1387 sp<AReplyToken> replyID;
1388 CHECK(msg->senderAwaitsResponse(&replyID));
1389 response->postReply(replyID);
1390 break;
1391 }
1392
Andreas Huberf9334412010-12-15 15:17:42 -08001393 default:
1394 TRESPASS();
1395 break;
1396 }
1397}
1398
Wei Jia94211742014-10-28 17:09:06 -07001399void NuPlayer::onResume() {
Ronghua Wu64c2d172015-10-07 16:52:19 -07001400 if (!mPaused || mResetting) {
1401 ALOGD_IF(mResetting, "resetting, onResume discarded");
Chong Zhangefbb6192015-01-30 17:13:27 -08001402 return;
1403 }
1404 mPaused = false;
Wei Jia94211742014-10-28 17:09:06 -07001405 if (mSource != NULL) {
1406 mSource->resume();
1407 } else {
1408 ALOGW("resume called when source is gone or not set");
1409 }
1410 // |mAudioDecoder| may have been released due to the pause timeout, so re-create it if
1411 // needed.
1412 if (audioDecoderStillNeeded() && mAudioDecoder == NULL) {
1413 instantiateDecoder(true /* audio */, &mAudioDecoder);
1414 }
1415 if (mRenderer != NULL) {
1416 mRenderer->resume();
1417 } else {
1418 ALOGW("resume called when renderer is gone or not set");
1419 }
Ray Essickd4d00612017-01-03 09:36:27 -08001420
1421 mLastStartedPlayingTimeNs = systemTime();
Wei Jia94211742014-10-28 17:09:06 -07001422}
1423
Lajos Molnarfcd3e942015-03-31 10:06:48 -07001424status_t NuPlayer::onInstantiateSecureDecoders() {
1425 status_t err;
1426 if (!(mSourceFlags & Source::FLAG_SECURE)) {
1427 return BAD_TYPE;
1428 }
1429
1430 if (mRenderer != NULL) {
1431 ALOGE("renderer should not be set when instantiating secure decoders");
1432 return UNKNOWN_ERROR;
1433 }
1434
1435 // TRICKY: We rely on mRenderer being null, so that decoder does not start requesting
1436 // data on instantiation.
Lajos Molnar1de1e252015-04-30 18:18:34 -07001437 if (mSurface != NULL) {
Lajos Molnarfcd3e942015-03-31 10:06:48 -07001438 err = instantiateDecoder(false, &mVideoDecoder);
1439 if (err != OK) {
1440 return err;
1441 }
1442 }
1443
1444 if (mAudioSink != NULL) {
1445 err = instantiateDecoder(true, &mAudioDecoder);
1446 if (err != OK) {
1447 return err;
1448 }
1449 }
1450 return OK;
1451}
1452
Wei Jiac5de0912016-11-18 10:22:14 -08001453void NuPlayer::onStart(int64_t startPositionUs, MediaPlayerSeekMode mode) {
Hassan Shojaniacefac142017-02-06 21:02:02 -08001454 ALOGV("onStart: mCrypto: %p (%d)", mCrypto.get(),
1455 (mCrypto != NULL ? mCrypto->getStrongCount() : 0));
1456
Robert Shih0c61a0d2015-07-06 15:09:10 -07001457 if (!mSourceStarted) {
1458 mSourceStarted = true;
1459 mSource->start();
1460 }
1461 if (startPositionUs > 0) {
Wei Jiac5de0912016-11-18 10:22:14 -08001462 performSeek(startPositionUs, mode);
Robert Shih0c61a0d2015-07-06 15:09:10 -07001463 if (mSource->getFormat(false /* audio */) == NULL) {
1464 return;
1465 }
1466 }
1467
Wei Jia94211742014-10-28 17:09:06 -07001468 mOffloadAudio = false;
1469 mAudioEOS = false;
1470 mVideoEOS = false;
Wei Jia94211742014-10-28 17:09:06 -07001471 mStarted = true;
Wei Jiafe8fe7d2016-06-08 10:29:25 -07001472 mPaused = false;
Wei Jia94211742014-10-28 17:09:06 -07001473
Wei Jia94211742014-10-28 17:09:06 -07001474 uint32_t flags = 0;
1475
1476 if (mSource->isRealTime()) {
1477 flags |= Renderer::FLAG_REAL_TIME;
1478 }
1479
Wei Jia9737d342016-12-28 14:59:59 -08001480 bool hasAudio = (mSource->getFormat(true /* audio */) != NULL);
1481 bool hasVideo = (mSource->getFormat(false /* audio */) != NULL);
1482 if (!hasAudio && !hasVideo) {
Wei Jia1de83d52016-10-10 10:15:03 -07001483 ALOGE("no metadata for either audio or video source");
1484 mSource->stop();
1485 mSourceStarted = false;
1486 notifyListener(MEDIA_ERROR, MEDIA_ERROR_UNKNOWN, ERROR_MALFORMED);
1487 return;
1488 }
Wei Jia9737d342016-12-28 14:59:59 -08001489 ALOGV_IF(!hasAudio, "no metadata for audio source"); // video only stream
1490
1491 sp<MetaData> audioMeta = mSource->getFormatMeta(true /* audio */);
Wei Jia1de83d52016-10-10 10:15:03 -07001492
Wei Jia94211742014-10-28 17:09:06 -07001493 audio_stream_type_t streamType = AUDIO_STREAM_MUSIC;
1494 if (mAudioSink != NULL) {
1495 streamType = mAudioSink->getAudioStreamType();
1496 }
1497
Wei Jia94211742014-10-28 17:09:06 -07001498 mOffloadAudio =
Wei Jia9737d342016-12-28 14:59:59 -08001499 canOffloadStream(audioMeta, hasVideo, mSource->isStreaming(), streamType)
Wei Jiabf70feb2016-02-19 15:47:16 -08001500 && (mPlaybackSettings.mSpeed == 1.f && mPlaybackSettings.mPitch == 1.f);
Hassan Shojaniacefac142017-02-06 21:02:02 -08001501
1502 // Modular DRM: Disabling audio offload if the source is protected
1503 if (mOffloadAudio && mIsDrmProtected) {
1504 mOffloadAudio = false;
1505 ALOGV("onStart: Disabling mOffloadAudio now that the source is protected.");
1506 }
1507
Wei Jia94211742014-10-28 17:09:06 -07001508 if (mOffloadAudio) {
1509 flags |= Renderer::FLAG_OFFLOAD_AUDIO;
1510 }
1511
Lajos Molnar1d15ab52015-03-04 16:46:34 -08001512 sp<AMessage> notify = new AMessage(kWhatRendererNotify, this);
Wei Jia94211742014-10-28 17:09:06 -07001513 ++mRendererGeneration;
1514 notify->setInt32("generation", mRendererGeneration);
1515 mRenderer = new Renderer(mAudioSink, notify, flags);
Wei Jia94211742014-10-28 17:09:06 -07001516 mRendererLooper = new ALooper;
1517 mRendererLooper->setName("NuPlayerRenderer");
1518 mRendererLooper->start(false, false, ANDROID_PRIORITY_AUDIO);
1519 mRendererLooper->registerHandler(mRenderer);
Lajos Molnar3a474aa2015-04-24 17:10:07 -07001520
1521 status_t err = mRenderer->setPlaybackSettings(mPlaybackSettings);
1522 if (err != OK) {
1523 mSource->stop();
Robert Shih0c61a0d2015-07-06 15:09:10 -07001524 mSourceStarted = false;
Lajos Molnar3a474aa2015-04-24 17:10:07 -07001525 notifyListener(MEDIA_ERROR, MEDIA_ERROR_UNKNOWN, err);
1526 return;
Wei Jiac8206ff2015-03-04 13:59:37 -08001527 }
Wei Jia94211742014-10-28 17:09:06 -07001528
Ronghua Wuc8a70d32015-04-29 16:26:34 -07001529 float rate = getFrameRate();
1530 if (rate > 0) {
Wei Jia94211742014-10-28 17:09:06 -07001531 mRenderer->setVideoFrameRate(rate);
1532 }
1533
Wei Jiac6cfd702014-11-11 16:33:20 -08001534 if (mVideoDecoder != NULL) {
1535 mVideoDecoder->setRenderer(mRenderer);
1536 }
1537 if (mAudioDecoder != NULL) {
1538 mAudioDecoder->setRenderer(mRenderer);
1539 }
1540
Ray Essickd4d00612017-01-03 09:36:27 -08001541 mLastStartedPlayingTimeNs = systemTime();
1542
Wei Jia94211742014-10-28 17:09:06 -07001543 postScanSources();
1544}
1545
Chong Zhangefbb6192015-01-30 17:13:27 -08001546void NuPlayer::onPause() {
1547 if (mPaused) {
1548 return;
1549 }
1550 mPaused = true;
1551 if (mSource != NULL) {
1552 mSource->pause();
1553 } else {
1554 ALOGW("pause called when source is gone or not set");
1555 }
1556 if (mRenderer != NULL) {
1557 mRenderer->pause();
1558 } else {
1559 ALOGW("pause called when renderer is gone or not set");
1560 }
Ray Essickd4d00612017-01-03 09:36:27 -08001561
1562 sp<NuPlayerDriver> driver = mDriver.promote();
1563 if (driver != NULL) {
1564 int64_t now = systemTime();
1565 int64_t played = now - mLastStartedPlayingTimeNs;
Ray Essickd4d00612017-01-03 09:36:27 -08001566
1567 driver->notifyMorePlayingTimeUs((played+500)/1000);
1568 }
Chong Zhangefbb6192015-01-30 17:13:27 -08001569}
1570
Ronghua Wud7988b12014-10-03 15:19:10 -07001571bool NuPlayer::audioDecoderStillNeeded() {
1572 // Audio decoder is no longer needed if it's in shut/shutting down status.
1573 return ((mFlushingAudio != SHUT_DOWN) && (mFlushingAudio != SHUTTING_DOWN_DECODER));
1574}
1575
Andy Hung8d121d42014-10-03 09:53:53 -07001576void NuPlayer::handleFlushComplete(bool audio, bool isDecoder) {
1577 // We wait for both the decoder flush and the renderer flush to complete
1578 // before entering either the FLUSHED or the SHUTTING_DOWN_DECODER state.
1579
1580 mFlushComplete[audio][isDecoder] = true;
1581 if (!mFlushComplete[audio][!isDecoder]) {
1582 return;
1583 }
1584
1585 FlushStatus *state = audio ? &mFlushingAudio : &mFlushingVideo;
1586 switch (*state) {
1587 case FLUSHING_DECODER:
1588 {
1589 *state = FLUSHED;
Andy Hung8d121d42014-10-03 09:53:53 -07001590 break;
1591 }
1592
1593 case FLUSHING_DECODER_SHUTDOWN:
1594 {
1595 *state = SHUTTING_DOWN_DECODER;
1596
1597 ALOGV("initiating %s decoder shutdown", audio ? "audio" : "video");
Andy Hung8d121d42014-10-03 09:53:53 -07001598 getDecoder(audio)->initiateShutdown();
1599 break;
1600 }
1601
1602 default:
1603 // decoder flush completes only occur in a flushing state.
1604 LOG_ALWAYS_FATAL_IF(isDecoder, "decoder flush in invalid state %d", *state);
1605 break;
1606 }
1607}
1608
Andreas Huber3831a062010-12-21 10:22:33 -08001609void NuPlayer::finishFlushIfPossible() {
Wei Jia53904f32014-07-29 10:22:53 -07001610 if (mFlushingAudio != NONE && mFlushingAudio != FLUSHED
1611 && mFlushingAudio != SHUT_DOWN) {
Andreas Huber3831a062010-12-21 10:22:33 -08001612 return;
1613 }
1614
Wei Jia53904f32014-07-29 10:22:53 -07001615 if (mFlushingVideo != NONE && mFlushingVideo != FLUSHED
1616 && mFlushingVideo != SHUT_DOWN) {
Andreas Huber3831a062010-12-21 10:22:33 -08001617 return;
1618 }
1619
Steve Block3856b092011-10-20 11:56:00 +01001620 ALOGV("both audio and video are flushed now.");
Andreas Huber3831a062010-12-21 10:22:33 -08001621
Andreas Huber3831a062010-12-21 10:22:33 -08001622 mFlushingAudio = NONE;
1623 mFlushingVideo = NONE;
Andreas Huber3831a062010-12-21 10:22:33 -08001624
Andy Hung8d121d42014-10-03 09:53:53 -07001625 clearFlushComplete();
1626
Andreas Hubera1f8ab02012-11-30 10:53:22 -08001627 processDeferredActions();
Andreas Huber1aef2112011-01-04 14:01:29 -08001628}
1629
1630void NuPlayer::postScanSources() {
1631 if (mScanSourcesPending) {
1632 return;
1633 }
1634
Lajos Molnar1d15ab52015-03-04 16:46:34 -08001635 sp<AMessage> msg = new AMessage(kWhatScanSources, this);
Andreas Huber1aef2112011-01-04 14:01:29 -08001636 msg->setInt32("generation", mScanSourcesGeneration);
1637 msg->post();
1638
1639 mScanSourcesPending = true;
1640}
1641
Wei Jia41cd4632016-05-13 10:53:30 -07001642void NuPlayer::tryOpenAudioSinkForOffload(
1643 const sp<AMessage> &format, const sp<MetaData> &audioMeta, bool hasVideo) {
Andy Hung202bce12014-12-03 11:47:36 -08001644 // Note: This is called early in NuPlayer to determine whether offloading
1645 // is possible; otherwise the decoders call the renderer openAudioSink directly.
Andy Hung282a7e32014-08-14 15:56:34 -07001646
Andy Hung202bce12014-12-03 11:47:36 -08001647 status_t err = mRenderer->openAudioSink(
Dhananjay Kumarc387f2b2015-08-06 10:43:16 +05301648 format, true /* offloadOnly */, hasVideo,
1649 AUDIO_OUTPUT_FLAG_NONE, &mOffloadAudio, mSource->isStreaming());
Andy Hung202bce12014-12-03 11:47:36 -08001650 if (err != OK) {
1651 // Any failure we turn off mOffloadAudio.
1652 mOffloadAudio = false;
1653 } else if (mOffloadAudio) {
Chong Zhang3b9eb1f2014-10-15 17:05:08 -07001654 sendMetaDataToHal(mAudioSink, audioMeta);
Andy Hung282a7e32014-08-14 15:56:34 -07001655 }
1656}
1657
1658void NuPlayer::closeAudioSink() {
Chong Zhang3b9eb1f2014-10-15 17:05:08 -07001659 mRenderer->closeAudioSink();
Andy Hung282a7e32014-08-14 15:56:34 -07001660}
1661
Wei Jia5031b2f2016-02-25 11:19:31 -08001662void NuPlayer::restartAudio(
Wei Jiabf70feb2016-02-19 15:47:16 -08001663 int64_t currentPositionUs, bool forceNonOffload, bool needsToCreateAudioDecoder) {
Wei Jiaa05f1e32016-03-25 16:31:22 -07001664 if (mAudioDecoder != NULL) {
1665 mAudioDecoder->pause();
1666 mAudioDecoder.clear();
Wei Jia686e8e52017-04-03 14:08:01 -07001667 mAudioDecoderError = false;
Wei Jiaa05f1e32016-03-25 16:31:22 -07001668 ++mAudioDecoderGeneration;
1669 }
Wei Jiabf70feb2016-02-19 15:47:16 -08001670 if (mFlushingAudio == FLUSHING_DECODER) {
1671 mFlushComplete[1 /* audio */][1 /* isDecoder */] = true;
1672 mFlushingAudio = FLUSHED;
1673 finishFlushIfPossible();
1674 } else if (mFlushingAudio == FLUSHING_DECODER_SHUTDOWN
1675 || mFlushingAudio == SHUTTING_DOWN_DECODER) {
1676 mFlushComplete[1 /* audio */][1 /* isDecoder */] = true;
1677 mFlushingAudio = SHUT_DOWN;
1678 finishFlushIfPossible();
1679 needsToCreateAudioDecoder = false;
1680 }
1681 if (mRenderer == NULL) {
1682 return;
1683 }
1684 closeAudioSink();
1685 mRenderer->flush(true /* audio */, false /* notifyComplete */);
1686 if (mVideoDecoder != NULL) {
1687 mRenderer->flush(false /* audio */, false /* notifyComplete */);
1688 }
1689
Wei Jiac5de0912016-11-18 10:22:14 -08001690 performSeek(currentPositionUs, MediaPlayerSeekMode::SEEK_PREVIOUS_SYNC /* mode */);
Wei Jiabf70feb2016-02-19 15:47:16 -08001691
1692 if (forceNonOffload) {
1693 mRenderer->signalDisableOffloadAudio();
1694 mOffloadAudio = false;
1695 }
1696 if (needsToCreateAudioDecoder) {
Wei Jiaa05f1e32016-03-25 16:31:22 -07001697 instantiateDecoder(true /* audio */, &mAudioDecoder, !forceNonOffload);
Wei Jiabf70feb2016-02-19 15:47:16 -08001698 }
1699}
1700
Wei Jia41cd4632016-05-13 10:53:30 -07001701void NuPlayer::determineAudioModeChange(const sp<AMessage> &audioFormat) {
Wei Jiae4d18c72015-07-13 17:58:11 -07001702 if (mSource == NULL || mAudioSink == NULL) {
1703 return;
1704 }
1705
1706 if (mRenderer == NULL) {
1707 ALOGW("No renderer can be used to determine audio mode. Use non-offload for safety.");
1708 mOffloadAudio = false;
1709 return;
1710 }
1711
1712 sp<MetaData> audioMeta = mSource->getFormatMeta(true /* audio */);
1713 sp<AMessage> videoFormat = mSource->getFormat(false /* audio */);
1714 audio_stream_type_t streamType = mAudioSink->getAudioStreamType();
1715 const bool hasVideo = (videoFormat != NULL);
Hassan Shojaniacefac142017-02-06 21:02:02 -08001716 bool canOffload = canOffloadStream(
Wei Jiabf70feb2016-02-19 15:47:16 -08001717 audioMeta, hasVideo, mSource->isStreaming(), streamType)
1718 && (mPlaybackSettings.mSpeed == 1.f && mPlaybackSettings.mPitch == 1.f);
Hassan Shojaniacefac142017-02-06 21:02:02 -08001719
1720 // Modular DRM: Disabling audio offload if the source is protected
1721 if (canOffload && mIsDrmProtected) {
1722 canOffload = false;
1723 ALOGV("determineAudioModeChange: Disabling mOffloadAudio b/c the source is protected.");
1724 }
1725
Wei Jiae4d18c72015-07-13 17:58:11 -07001726 if (canOffload) {
1727 if (!mOffloadAudio) {
1728 mRenderer->signalEnableOffloadAudio();
1729 }
1730 // open audio sink early under offload mode.
Wei Jia41cd4632016-05-13 10:53:30 -07001731 tryOpenAudioSinkForOffload(audioFormat, audioMeta, hasVideo);
Wei Jiae4d18c72015-07-13 17:58:11 -07001732 } else {
Robert Shihe1d70192015-07-23 17:54:13 -07001733 if (mOffloadAudio) {
1734 mRenderer->signalDisableOffloadAudio();
1735 mOffloadAudio = false;
1736 }
Wei Jiae4d18c72015-07-13 17:58:11 -07001737 }
1738}
1739
Wei Jiaa05f1e32016-03-25 16:31:22 -07001740status_t NuPlayer::instantiateDecoder(
1741 bool audio, sp<DecoderBase> *decoder, bool checkAudioModeChange) {
Wei Jia566da802015-08-27 13:59:30 -07001742 // The audio decoder could be cleared by tear down. If still in shut down
1743 // process, no need to create a new audio decoder.
1744 if (*decoder != NULL || (audio && mFlushingAudio == SHUT_DOWN)) {
Andreas Huberf9334412010-12-15 15:17:42 -08001745 return OK;
1746 }
1747
Andreas Huber84066782011-08-16 09:34:26 -07001748 sp<AMessage> format = mSource->getFormat(audio);
Andreas Huberf9334412010-12-15 15:17:42 -08001749
Andreas Huber84066782011-08-16 09:34:26 -07001750 if (format == NULL) {
Robert Shih7350b052015-10-01 15:50:14 -07001751 return UNKNOWN_ERROR;
1752 } else {
1753 status_t err;
1754 if (format->findInt32("err", &err) && err) {
1755 return err;
1756 }
Andreas Huberf9334412010-12-15 15:17:42 -08001757 }
1758
Ronghua Wu8db88132015-04-22 13:51:35 -07001759 format->setInt32("priority", 0 /* realtime */);
1760
Andreas Huber3fe62152011-09-16 15:09:22 -07001761 if (!audio) {
Andreas Huber84066782011-08-16 09:34:26 -07001762 AString mime;
1763 CHECK(format->findString("mime", &mime));
Chong Zhanga7fa1d92014-06-11 14:49:23 -07001764
Lajos Molnar1d15ab52015-03-04 16:46:34 -08001765 sp<AMessage> ccNotify = new AMessage(kWhatClosedCaptionNotify, this);
Chong Zhang341ab6e2015-02-04 13:37:18 -08001766 if (mCCDecoder == NULL) {
1767 mCCDecoder = new CCDecoder(ccNotify);
1768 }
Lajos Molnar09524832014-07-17 14:29:51 -07001769
1770 if (mSourceFlags & Source::FLAG_SECURE) {
1771 format->setInt32("secure", true);
1772 }
Chong Zhang17134602015-01-07 16:14:34 -08001773
1774 if (mSourceFlags & Source::FLAG_PROTECTED) {
1775 format->setInt32("protected", true);
1776 }
Ronghua Wu8db88132015-04-22 13:51:35 -07001777
Ronghua Wuc8a70d32015-04-29 16:26:34 -07001778 float rate = getFrameRate();
1779 if (rate > 0) {
Lajos Molnar3a474aa2015-04-24 17:10:07 -07001780 format->setFloat("operating-rate", rate * mPlaybackSettings.mSpeed);
Ronghua Wu8db88132015-04-22 13:51:35 -07001781 }
Andreas Huber3fe62152011-09-16 15:09:22 -07001782 }
1783
Wei Jiabc2fb722014-07-08 16:37:57 -07001784 if (audio) {
Lajos Molnar1d15ab52015-03-04 16:46:34 -08001785 sp<AMessage> notify = new AMessage(kWhatAudioNotify, this);
Wei Jia88703c32014-08-06 11:24:07 -07001786 ++mAudioDecoderGeneration;
1787 notify->setInt32("generation", mAudioDecoderGeneration);
1788
Wei Jiaa05f1e32016-03-25 16:31:22 -07001789 if (checkAudioModeChange) {
Wei Jia41cd4632016-05-13 10:53:30 -07001790 determineAudioModeChange(format);
Wei Jiaa05f1e32016-03-25 16:31:22 -07001791 }
Wei Jiabc2fb722014-07-08 16:37:57 -07001792 if (mOffloadAudio) {
Wei Jia14532f22015-12-29 11:28:15 -08001793 mSource->setOffloadAudio(true /* offload */);
1794
Haynes Mathew George8b635332015-03-30 17:59:47 -07001795 const bool hasVideo = (mSource->getFormat(false /*audio */) != NULL);
1796 format->setInt32("has-video", hasVideo);
Wei Jiac6cfd702014-11-11 16:33:20 -08001797 *decoder = new DecoderPassThrough(notify, mSource, mRenderer);
Hassan Shojaniacefac142017-02-06 21:02:02 -08001798 ALOGV("instantiateDecoder audio DecoderPassThrough hasVideo: %d", hasVideo);
Wei Jiabc2fb722014-07-08 16:37:57 -07001799 } else {
Wei Jia14532f22015-12-29 11:28:15 -08001800 mSource->setOffloadAudio(false /* offload */);
1801
Wei Jiaf2ae3e12016-10-27 17:10:59 -07001802 *decoder = new Decoder(notify, mSource, mPID, mUID, mRenderer);
Hassan Shojaniacefac142017-02-06 21:02:02 -08001803 ALOGV("instantiateDecoder audio Decoder");
Wei Jiabc2fb722014-07-08 16:37:57 -07001804 }
Wei Jia686e8e52017-04-03 14:08:01 -07001805 mAudioDecoderError = false;
Wei Jiabc2fb722014-07-08 16:37:57 -07001806 } else {
Lajos Molnar1d15ab52015-03-04 16:46:34 -08001807 sp<AMessage> notify = new AMessage(kWhatVideoNotify, this);
Wei Jia88703c32014-08-06 11:24:07 -07001808 ++mVideoDecoderGeneration;
1809 notify->setInt32("generation", mVideoDecoderGeneration);
1810
Chong Zhang7137ec72014-11-12 16:41:05 -08001811 *decoder = new Decoder(
Wei Jiaf2ae3e12016-10-27 17:10:59 -07001812 notify, mSource, mPID, mUID, mRenderer, mSurface, mCCDecoder);
Wei Jia686e8e52017-04-03 14:08:01 -07001813 mVideoDecoderError = false;
Lajos Molnard9fd6312014-11-06 11:00:00 -08001814
1815 // enable FRC if high-quality AV sync is requested, even if not
Lajos Molnar1de1e252015-04-30 18:18:34 -07001816 // directly queuing to display, as this will even improve textureview
Lajos Molnard9fd6312014-11-06 11:00:00 -08001817 // playback.
1818 {
Marco Nelissen96626b72016-12-01 13:58:59 -08001819 if (property_get_bool("persist.sys.media.avsync", false)) {
Lajos Molnard9fd6312014-11-06 11:00:00 -08001820 format->setInt32("auto-frc", 1);
1821 }
1822 }
Wei Jiabc2fb722014-07-08 16:37:57 -07001823 }
Lajos Molnar1cd13982014-01-17 15:12:51 -08001824 (*decoder)->init();
Hassan Shojaniacefac142017-02-06 21:02:02 -08001825
1826 // Modular DRM
1827 if (mIsDrmProtected) {
1828 format->setPointer("crypto", mCrypto.get());
1829 ALOGV("instantiateDecoder: mCrypto: %p (%d) isSecure: %d", mCrypto.get(),
1830 (mCrypto != NULL ? mCrypto->getStrongCount() : 0),
1831 (mSourceFlags & Source::FLAG_SECURE) != 0);
1832 }
1833
Andreas Huber84066782011-08-16 09:34:26 -07001834 (*decoder)->configure(format);
Andreas Huberf9334412010-12-15 15:17:42 -08001835
Praveen Chavanbbaa1442016-04-08 13:33:49 -07001836 if (!audio) {
1837 sp<AMessage> params = new AMessage();
1838 float rate = getFrameRate();
1839 if (rate > 0) {
1840 params->setFloat("frame-rate-total", rate);
1841 }
1842
1843 sp<MetaData> fileMeta = getFileMeta();
1844 if (fileMeta != NULL) {
1845 int32_t videoTemporalLayerCount;
1846 if (fileMeta->findInt32(kKeyTemporalLayerCount, &videoTemporalLayerCount)
1847 && videoTemporalLayerCount > 0) {
1848 params->setInt32("temporal-layer-count", videoTemporalLayerCount);
1849 }
1850 }
1851
1852 if (params->countEntries() > 0) {
1853 (*decoder)->setParameters(params);
1854 }
1855 }
Andreas Huberf9334412010-12-15 15:17:42 -08001856 return OK;
1857}
1858
Chong Zhangced1c2f2014-08-08 15:22:35 -07001859void NuPlayer::updateVideoSize(
1860 const sp<AMessage> &inputFormat,
1861 const sp<AMessage> &outputFormat) {
1862 if (inputFormat == NULL) {
1863 ALOGW("Unknown video size, reporting 0x0!");
1864 notifyListener(MEDIA_SET_VIDEO_SIZE, 0, 0);
1865 return;
1866 }
Wei Jia9737d342016-12-28 14:59:59 -08001867 int32_t err = OK;
1868 inputFormat->findInt32("err", &err);
1869 if (err == -EWOULDBLOCK) {
1870 ALOGW("Video meta is not available yet!");
1871 return;
1872 }
1873 if (err != OK) {
1874 ALOGW("Something is wrong with video meta!");
1875 return;
1876 }
Chong Zhangced1c2f2014-08-08 15:22:35 -07001877
1878 int32_t displayWidth, displayHeight;
Chong Zhangced1c2f2014-08-08 15:22:35 -07001879 if (outputFormat != NULL) {
1880 int32_t width, height;
1881 CHECK(outputFormat->findInt32("width", &width));
1882 CHECK(outputFormat->findInt32("height", &height));
1883
1884 int32_t cropLeft, cropTop, cropRight, cropBottom;
1885 CHECK(outputFormat->findRect(
1886 "crop",
1887 &cropLeft, &cropTop, &cropRight, &cropBottom));
1888
1889 displayWidth = cropRight - cropLeft + 1;
1890 displayHeight = cropBottom - cropTop + 1;
1891
1892 ALOGV("Video output format changed to %d x %d "
1893 "(crop: %d x %d @ (%d, %d))",
1894 width, height,
1895 displayWidth,
1896 displayHeight,
1897 cropLeft, cropTop);
1898 } else {
1899 CHECK(inputFormat->findInt32("width", &displayWidth));
1900 CHECK(inputFormat->findInt32("height", &displayHeight));
1901
1902 ALOGV("Video input format %d x %d", displayWidth, displayHeight);
1903 }
1904
1905 // Take into account sample aspect ratio if necessary:
1906 int32_t sarWidth, sarHeight;
1907 if (inputFormat->findInt32("sar-width", &sarWidth)
Wei Jia095bc252017-01-27 10:16:16 -08001908 && inputFormat->findInt32("sar-height", &sarHeight)
1909 && sarWidth > 0 && sarHeight > 0) {
Chong Zhangced1c2f2014-08-08 15:22:35 -07001910 ALOGV("Sample aspect ratio %d : %d", sarWidth, sarHeight);
1911
1912 displayWidth = (displayWidth * sarWidth) / sarHeight;
1913
1914 ALOGV("display dimensions %d x %d", displayWidth, displayHeight);
Wei Jiadf6c6af2016-09-29 11:27:15 -07001915 } else {
1916 int32_t width, height;
1917 if (inputFormat->findInt32("display-width", &width)
1918 && inputFormat->findInt32("display-height", &height)
1919 && width > 0 && height > 0
1920 && displayWidth > 0 && displayHeight > 0) {
1921 if (displayHeight * (int64_t)width / height > (int64_t)displayWidth) {
1922 displayHeight = (int32_t)(displayWidth * (int64_t)height / width);
1923 } else {
1924 displayWidth = (int32_t)(displayHeight * (int64_t)width / height);
1925 }
1926 ALOGV("Video display width and height are overridden to %d x %d",
1927 displayWidth, displayHeight);
1928 }
Chong Zhangced1c2f2014-08-08 15:22:35 -07001929 }
1930
1931 int32_t rotationDegrees;
1932 if (!inputFormat->findInt32("rotation-degrees", &rotationDegrees)) {
1933 rotationDegrees = 0;
1934 }
1935
1936 if (rotationDegrees == 90 || rotationDegrees == 270) {
1937 int32_t tmp = displayWidth;
1938 displayWidth = displayHeight;
1939 displayHeight = tmp;
1940 }
1941
1942 notifyListener(
1943 MEDIA_SET_VIDEO_SIZE,
1944 displayWidth,
1945 displayHeight);
1946}
1947
Chong Zhangdcb89b32013-08-06 09:44:47 -07001948void NuPlayer::notifyListener(int msg, int ext1, int ext2, const Parcel *in) {
Andreas Huber43c3e6c2011-01-05 12:17:08 -08001949 if (mDriver == NULL) {
Andreas Huberf9334412010-12-15 15:17:42 -08001950 return;
1951 }
1952
Andreas Huber43c3e6c2011-01-05 12:17:08 -08001953 sp<NuPlayerDriver> driver = mDriver.promote();
Andreas Huberf9334412010-12-15 15:17:42 -08001954
Andreas Huber43c3e6c2011-01-05 12:17:08 -08001955 if (driver == NULL) {
Andreas Huberf9334412010-12-15 15:17:42 -08001956 return;
1957 }
1958
Chong Zhangdcb89b32013-08-06 09:44:47 -07001959 driver->notifyListener(msg, ext1, ext2, in);
Andreas Huberf9334412010-12-15 15:17:42 -08001960}
1961
Chong Zhang7137ec72014-11-12 16:41:05 -08001962void NuPlayer::flushDecoder(bool audio, bool needShutdown) {
Andreas Huber14f76722013-01-15 09:04:18 -08001963 ALOGV("[%s] flushDecoder needShutdown=%d",
1964 audio ? "audio" : "video", needShutdown);
1965
Chong Zhang7137ec72014-11-12 16:41:05 -08001966 const sp<DecoderBase> &decoder = getDecoder(audio);
Lajos Molnar87603c02014-08-20 19:25:30 -07001967 if (decoder == NULL) {
Steve Blockdf64d152012-01-04 20:05:49 +00001968 ALOGI("flushDecoder %s without decoder present",
Andreas Huber6e3d3112011-11-28 12:36:11 -08001969 audio ? "audio" : "video");
Lajos Molnar87603c02014-08-20 19:25:30 -07001970 return;
Andreas Huber6e3d3112011-11-28 12:36:11 -08001971 }
1972
Andreas Huber1aef2112011-01-04 14:01:29 -08001973 // Make sure we don't continue to scan sources until we finish flushing.
1974 ++mScanSourcesGeneration;
Chong Zhang3b032b32015-04-17 15:49:06 -07001975 if (mScanSourcesPending) {
Toshikazu Saitocbcbb792015-09-15 14:53:01 +09001976 if (!needShutdown) {
1977 mDeferredActions.push_back(
1978 new SimpleAction(&NuPlayer::performScanSources));
1979 }
Chong Zhang3b032b32015-04-17 15:49:06 -07001980 mScanSourcesPending = false;
1981 }
Andreas Huber1aef2112011-01-04 14:01:29 -08001982
Chong Zhang7137ec72014-11-12 16:41:05 -08001983 decoder->signalFlush();
Andreas Huber1aef2112011-01-04 14:01:29 -08001984
1985 FlushStatus newStatus =
1986 needShutdown ? FLUSHING_DECODER_SHUTDOWN : FLUSHING_DECODER;
1987
Lajos Molnarfcd3e942015-03-31 10:06:48 -07001988 mFlushComplete[audio][false /* isDecoder */] = (mRenderer == NULL);
Andy Hung8d121d42014-10-03 09:53:53 -07001989 mFlushComplete[audio][true /* isDecoder */] = false;
Andreas Huber1aef2112011-01-04 14:01:29 -08001990 if (audio) {
Wei Jia53904f32014-07-29 10:22:53 -07001991 ALOGE_IF(mFlushingAudio != NONE,
1992 "audio flushDecoder() is called in state %d", mFlushingAudio);
Andreas Huber1aef2112011-01-04 14:01:29 -08001993 mFlushingAudio = newStatus;
Andreas Huber1aef2112011-01-04 14:01:29 -08001994 } else {
Wei Jia53904f32014-07-29 10:22:53 -07001995 ALOGE_IF(mFlushingVideo != NONE,
1996 "video flushDecoder() is called in state %d", mFlushingVideo);
Andreas Huber1aef2112011-01-04 14:01:29 -08001997 mFlushingVideo = newStatus;
Andreas Huber1aef2112011-01-04 14:01:29 -08001998 }
1999}
2000
Chong Zhangced1c2f2014-08-08 15:22:35 -07002001void NuPlayer::queueDecoderShutdown(
2002 bool audio, bool video, const sp<AMessage> &reply) {
2003 ALOGI("queueDecoderShutdown audio=%d, video=%d", audio, video);
Andreas Huber84066782011-08-16 09:34:26 -07002004
Chong Zhangced1c2f2014-08-08 15:22:35 -07002005 mDeferredActions.push_back(
Wei Jiafef808d2014-10-31 17:57:05 -07002006 new FlushDecoderAction(
2007 audio ? FLUSH_CMD_SHUTDOWN : FLUSH_CMD_NONE,
2008 video ? FLUSH_CMD_SHUTDOWN : FLUSH_CMD_NONE));
Andreas Huber84066782011-08-16 09:34:26 -07002009
Chong Zhangced1c2f2014-08-08 15:22:35 -07002010 mDeferredActions.push_back(
2011 new SimpleAction(&NuPlayer::performScanSources));
Andreas Huber84066782011-08-16 09:34:26 -07002012
Chong Zhangced1c2f2014-08-08 15:22:35 -07002013 mDeferredActions.push_back(new PostMessageAction(reply));
2014
2015 processDeferredActions();
Andreas Huber84066782011-08-16 09:34:26 -07002016}
2017
James Dong0d268a32012-08-31 12:18:27 -07002018status_t NuPlayer::setVideoScalingMode(int32_t mode) {
2019 mVideoScalingMode = mode;
Lajos Molnar1de1e252015-04-30 18:18:34 -07002020 if (mSurface != NULL) {
2021 status_t ret = native_window_set_scaling_mode(mSurface.get(), mVideoScalingMode);
James Dong0d268a32012-08-31 12:18:27 -07002022 if (ret != OK) {
2023 ALOGE("Failed to set scaling mode (%d): %s",
2024 -ret, strerror(-ret));
2025 return ret;
2026 }
2027 }
2028 return OK;
2029}
2030
Chong Zhangdcb89b32013-08-06 09:44:47 -07002031status_t NuPlayer::getTrackInfo(Parcel* reply) const {
Lajos Molnar1d15ab52015-03-04 16:46:34 -08002032 sp<AMessage> msg = new AMessage(kWhatGetTrackInfo, this);
Chong Zhangdcb89b32013-08-06 09:44:47 -07002033 msg->setPointer("reply", reply);
2034
2035 sp<AMessage> response;
2036 status_t err = msg->postAndAwaitResponse(&response);
2037 return err;
2038}
2039
Robert Shih7c4f0d72014-07-09 18:53:31 -07002040status_t NuPlayer::getSelectedTrack(int32_t type, Parcel* reply) const {
Lajos Molnar1d15ab52015-03-04 16:46:34 -08002041 sp<AMessage> msg = new AMessage(kWhatGetSelectedTrack, this);
Robert Shih7c4f0d72014-07-09 18:53:31 -07002042 msg->setPointer("reply", reply);
2043 msg->setInt32("type", type);
2044
2045 sp<AMessage> response;
2046 status_t err = msg->postAndAwaitResponse(&response);
2047 if (err == OK && response != NULL) {
2048 CHECK(response->findInt32("err", &err));
2049 }
2050 return err;
2051}
2052
Robert Shih6ffb1fd2014-10-29 16:24:32 -07002053status_t NuPlayer::selectTrack(size_t trackIndex, bool select, int64_t timeUs) {
Lajos Molnar1d15ab52015-03-04 16:46:34 -08002054 sp<AMessage> msg = new AMessage(kWhatSelectTrack, this);
Chong Zhangdcb89b32013-08-06 09:44:47 -07002055 msg->setSize("trackIndex", trackIndex);
2056 msg->setInt32("select", select);
Robert Shih6ffb1fd2014-10-29 16:24:32 -07002057 msg->setInt64("timeUs", timeUs);
Chong Zhangdcb89b32013-08-06 09:44:47 -07002058
2059 sp<AMessage> response;
2060 status_t err = msg->postAndAwaitResponse(&response);
2061
Chong Zhang404fced2014-06-11 14:45:31 -07002062 if (err != OK) {
2063 return err;
2064 }
2065
2066 if (!response->findInt32("err", &err)) {
2067 err = OK;
2068 }
2069
Chong Zhangdcb89b32013-08-06 09:44:47 -07002070 return err;
2071}
2072
Ronghua Wua73d9e02014-10-08 15:13:29 -07002073status_t NuPlayer::getCurrentPosition(int64_t *mediaUs) {
2074 sp<Renderer> renderer = mRenderer;
2075 if (renderer == NULL) {
2076 return NO_INIT;
2077 }
2078
2079 return renderer->getCurrentPosition(mediaUs);
2080}
2081
Praveen Chavane1e5d7a2015-05-19 19:09:48 -07002082void NuPlayer::getStats(Vector<sp<AMessage> > *mTrackStats) {
2083 CHECK(mTrackStats != NULL);
2084
2085 mTrackStats->clear();
2086 if (mVideoDecoder != NULL) {
2087 mTrackStats->push_back(mVideoDecoder->getStats());
2088 }
2089 if (mAudioDecoder != NULL) {
2090 mTrackStats->push_back(mAudioDecoder->getStats());
Chong Zhang7137ec72014-11-12 16:41:05 -08002091 }
Ronghua Wua73d9e02014-10-08 15:13:29 -07002092}
2093
Marco Nelissenf0b72b52014-09-16 15:43:44 -07002094sp<MetaData> NuPlayer::getFileMeta() {
2095 return mSource->getFileFormatMeta();
2096}
2097
Ronghua Wuc8a70d32015-04-29 16:26:34 -07002098float NuPlayer::getFrameRate() {
2099 sp<MetaData> meta = mSource->getFormatMeta(false /* audio */);
2100 if (meta == NULL) {
2101 return 0;
2102 }
2103 int32_t rate;
2104 if (!meta->findInt32(kKeyFrameRate, &rate)) {
2105 // fall back to try file meta
2106 sp<MetaData> fileMeta = getFileMeta();
2107 if (fileMeta == NULL) {
2108 ALOGW("source has video meta but not file meta");
2109 return -1;
2110 }
2111 int32_t fileMetaRate;
2112 if (!fileMeta->findInt32(kKeyFrameRate, &fileMetaRate)) {
2113 return -1;
2114 }
2115 return fileMetaRate;
2116 }
2117 return rate;
2118}
2119
Andreas Huberb7c8e912012-11-27 15:02:53 -08002120void NuPlayer::schedulePollDuration() {
Lajos Molnar1d15ab52015-03-04 16:46:34 -08002121 sp<AMessage> msg = new AMessage(kWhatPollDuration, this);
Andreas Huberb7c8e912012-11-27 15:02:53 -08002122 msg->setInt32("generation", mPollDurationGeneration);
2123 msg->post();
2124}
2125
2126void NuPlayer::cancelPollDuration() {
2127 ++mPollDurationGeneration;
2128}
2129
Andreas Hubera1f8ab02012-11-30 10:53:22 -08002130void NuPlayer::processDeferredActions() {
2131 while (!mDeferredActions.empty()) {
2132 // We won't execute any deferred actions until we're no longer in
2133 // an intermediate state, i.e. one more more decoders are currently
2134 // flushing or shutting down.
2135
Andreas Hubera1f8ab02012-11-30 10:53:22 -08002136 if (mFlushingAudio != NONE || mFlushingVideo != NONE) {
2137 // We're currently flushing, postpone the reset until that's
2138 // completed.
2139
2140 ALOGV("postponing action mFlushingAudio=%d, mFlushingVideo=%d",
2141 mFlushingAudio, mFlushingVideo);
2142
2143 break;
2144 }
2145
2146 sp<Action> action = *mDeferredActions.begin();
2147 mDeferredActions.erase(mDeferredActions.begin());
2148
2149 action->execute(this);
2150 }
2151}
2152
Wei Jiac5de0912016-11-18 10:22:14 -08002153void NuPlayer::performSeek(int64_t seekTimeUs, MediaPlayerSeekMode mode) {
2154 ALOGV("performSeek seekTimeUs=%lld us (%.2f secs), mode=%d",
2155 (long long)seekTimeUs, seekTimeUs / 1E6, mode);
Andreas Hubera1f8ab02012-11-30 10:53:22 -08002156
Andy Hungadf34bf2014-09-03 18:22:22 -07002157 if (mSource == NULL) {
2158 // This happens when reset occurs right before the loop mode
2159 // asynchronously seeks to the start of the stream.
2160 LOG_ALWAYS_FATAL_IF(mAudioDecoder != NULL || mVideoDecoder != NULL,
2161 "mSource is NULL and decoders not NULL audio(%p) video(%p)",
2162 mAudioDecoder.get(), mVideoDecoder.get());
2163 return;
2164 }
Robert Shih1a5c8592015-08-04 18:07:44 -07002165 mPreviousSeekTimeUs = seekTimeUs;
Wei Jiac5de0912016-11-18 10:22:14 -08002166 mSource->seekTo(seekTimeUs, mode);
Robert Shihd3b0bbb2014-07-23 15:00:25 -07002167 ++mTimedTextGeneration;
Andreas Hubera1f8ab02012-11-30 10:53:22 -08002168
Andreas Hubera1f8ab02012-11-30 10:53:22 -08002169 // everything's flushed, continue playback.
2170}
2171
Wei Jiafef808d2014-10-31 17:57:05 -07002172void NuPlayer::performDecoderFlush(FlushCommand audio, FlushCommand video) {
2173 ALOGV("performDecoderFlush audio=%d, video=%d", audio, video);
Andreas Hubera1f8ab02012-11-30 10:53:22 -08002174
Wei Jiafef808d2014-10-31 17:57:05 -07002175 if ((audio == FLUSH_CMD_NONE || mAudioDecoder == NULL)
2176 && (video == FLUSH_CMD_NONE || mVideoDecoder == NULL)) {
Andreas Hubera1f8ab02012-11-30 10:53:22 -08002177 return;
2178 }
2179
Wei Jiafef808d2014-10-31 17:57:05 -07002180 if (audio != FLUSH_CMD_NONE && mAudioDecoder != NULL) {
2181 flushDecoder(true /* audio */, (audio == FLUSH_CMD_SHUTDOWN));
Andreas Hubera1f8ab02012-11-30 10:53:22 -08002182 }
2183
Wei Jiafef808d2014-10-31 17:57:05 -07002184 if (video != FLUSH_CMD_NONE && mVideoDecoder != NULL) {
2185 flushDecoder(false /* audio */, (video == FLUSH_CMD_SHUTDOWN));
Andreas Hubera1f8ab02012-11-30 10:53:22 -08002186 }
2187}
2188
2189void NuPlayer::performReset() {
2190 ALOGV("performReset");
2191
2192 CHECK(mAudioDecoder == NULL);
2193 CHECK(mVideoDecoder == NULL);
2194
2195 cancelPollDuration();
2196
2197 ++mScanSourcesGeneration;
2198 mScanSourcesPending = false;
2199
Lajos Molnar09524832014-07-17 14:29:51 -07002200 if (mRendererLooper != NULL) {
2201 if (mRenderer != NULL) {
2202 mRendererLooper->unregisterHandler(mRenderer->id());
2203 }
2204 mRendererLooper->stop();
2205 mRendererLooper.clear();
2206 }
Andreas Hubera1f8ab02012-11-30 10:53:22 -08002207 mRenderer.clear();
Wei Jia57568df2014-09-22 10:16:29 -07002208 ++mRendererGeneration;
Andreas Hubera1f8ab02012-11-30 10:53:22 -08002209
2210 if (mSource != NULL) {
2211 mSource->stop();
Andreas Huberb5f25f02013-02-05 10:14:26 -08002212
Wei Jiac45a4b22016-04-15 15:30:23 -07002213 Mutex::Autolock autoLock(mSourceLock);
Andreas Hubera1f8ab02012-11-30 10:53:22 -08002214 mSource.clear();
2215 }
2216
2217 if (mDriver != NULL) {
2218 sp<NuPlayerDriver> driver = mDriver.promote();
2219 if (driver != NULL) {
2220 driver->notifyResetComplete();
2221 }
2222 }
Andreas Huber57a339c2012-12-03 11:18:00 -08002223
2224 mStarted = false;
Wei Jia8a092d32016-06-03 14:57:24 -07002225 mPrepared = false;
Ronghua Wu64c2d172015-10-07 16:52:19 -07002226 mResetting = false;
Robert Shih0c61a0d2015-07-06 15:09:10 -07002227 mSourceStarted = false;
Hassan Shojaniacefac142017-02-06 21:02:02 -08002228
2229 // Modular DRM
2230 if (mCrypto != NULL) {
2231 // decoders will be flushed before this so their mCrypto would go away on their own
2232 // TODO change to ALOGV
2233 ALOGD("performReset mCrypto: %p (%d)", mCrypto.get(),
2234 (mCrypto != NULL ? mCrypto->getStrongCount() : 0));
2235 mCrypto.clear();
2236 }
2237 mIsDrmProtected = false;
Andreas Hubera1f8ab02012-11-30 10:53:22 -08002238}
2239
2240void NuPlayer::performScanSources() {
2241 ALOGV("performScanSources");
2242
Andreas Huber57a339c2012-12-03 11:18:00 -08002243 if (!mStarted) {
2244 return;
2245 }
2246
Andreas Hubera1f8ab02012-11-30 10:53:22 -08002247 if (mAudioDecoder == NULL || mVideoDecoder == NULL) {
2248 postScanSources();
2249 }
2250}
2251
Lajos Molnar1de1e252015-04-30 18:18:34 -07002252void NuPlayer::performSetSurface(const sp<Surface> &surface) {
Andreas Huber57a339c2012-12-03 11:18:00 -08002253 ALOGV("performSetSurface");
2254
Lajos Molnar1de1e252015-04-30 18:18:34 -07002255 mSurface = surface;
Andreas Huber57a339c2012-12-03 11:18:00 -08002256
2257 // XXX - ignore error from setVideoScalingMode for now
2258 setVideoScalingMode(mVideoScalingMode);
Chong Zhang13d6faa2014-08-22 15:35:28 -07002259
2260 if (mDriver != NULL) {
2261 sp<NuPlayerDriver> driver = mDriver.promote();
2262 if (driver != NULL) {
2263 driver->notifySetSurfaceComplete();
2264 }
2265 }
Andreas Huber57a339c2012-12-03 11:18:00 -08002266}
2267
Chong Zhangf8d71772014-11-26 15:08:34 -08002268void NuPlayer::performResumeDecoders(bool needNotify) {
2269 if (needNotify) {
2270 mResumePending = true;
2271 if (mVideoDecoder == NULL) {
2272 // if audio-only, we can notify seek complete now,
2273 // as the resume operation will be relatively fast.
2274 finishResume();
2275 }
2276 }
2277
Chong Zhang7137ec72014-11-12 16:41:05 -08002278 if (mVideoDecoder != NULL) {
Chong Zhangf8d71772014-11-26 15:08:34 -08002279 // When there is continuous seek, MediaPlayer will cache the seek
2280 // position, and send down new seek request when previous seek is
2281 // complete. Let's wait for at least one video output frame before
2282 // notifying seek complete, so that the video thumbnail gets updated
2283 // when seekbar is dragged.
2284 mVideoDecoder->signalResume(needNotify);
Chong Zhang7137ec72014-11-12 16:41:05 -08002285 }
2286
2287 if (mAudioDecoder != NULL) {
Chong Zhangf8d71772014-11-26 15:08:34 -08002288 mAudioDecoder->signalResume(false /* needNotify */);
2289 }
2290}
2291
2292void NuPlayer::finishResume() {
2293 if (mResumePending) {
2294 mResumePending = false;
Wei Jia1061c9c2015-05-19 16:02:17 -07002295 notifyDriverSeekComplete();
2296 }
2297}
2298
2299void NuPlayer::notifyDriverSeekComplete() {
2300 if (mDriver != NULL) {
2301 sp<NuPlayerDriver> driver = mDriver.promote();
2302 if (driver != NULL) {
2303 driver->notifySeekComplete();
Chong Zhangf8d71772014-11-26 15:08:34 -08002304 }
Chong Zhang7137ec72014-11-12 16:41:05 -08002305 }
2306}
2307
Andreas Huber9575c962013-02-05 13:59:56 -08002308void NuPlayer::onSourceNotify(const sp<AMessage> &msg) {
2309 int32_t what;
2310 CHECK(msg->findInt32("what", &what));
2311
2312 switch (what) {
Lajos Molnarfcd3e942015-03-31 10:06:48 -07002313 case Source::kWhatInstantiateSecureDecoders:
2314 {
2315 if (mSource == NULL) {
2316 // This is a stale notification from a source that was
2317 // asynchronously preparing when the client called reset().
2318 // We handled the reset, the source is gone.
2319 break;
2320 }
2321
2322 sp<AMessage> reply;
2323 CHECK(msg->findMessage("reply", &reply));
2324 status_t err = onInstantiateSecureDecoders();
2325 reply->setInt32("err", err);
2326 reply->post();
2327 break;
2328 }
2329
Andreas Huber9575c962013-02-05 13:59:56 -08002330 case Source::kWhatPrepared:
2331 {
Hassan Shojaniacefac142017-02-06 21:02:02 -08002332 ALOGV("NuPlayer::onSourceNotify Source::kWhatPrepared source: %p", mSource.get());
Andreas Huberb5f28d42013-04-25 15:11:19 -07002333 if (mSource == NULL) {
2334 // This is a stale notification from a source that was
2335 // asynchronously preparing when the client called reset().
2336 // We handled the reset, the source is gone.
2337 break;
2338 }
2339
Andreas Huberec0c5972013-02-05 14:47:13 -08002340 int32_t err;
2341 CHECK(msg->findInt32("err", &err));
2342
Lajos Molnarfcd3e942015-03-31 10:06:48 -07002343 if (err != OK) {
2344 // shut down potential secure codecs in case client never calls reset
2345 mDeferredActions.push_back(
2346 new FlushDecoderAction(FLUSH_CMD_SHUTDOWN /* audio */,
2347 FLUSH_CMD_SHUTDOWN /* video */));
2348 processDeferredActions();
Wei Jia8a092d32016-06-03 14:57:24 -07002349 } else {
2350 mPrepared = true;
Lajos Molnarfcd3e942015-03-31 10:06:48 -07002351 }
2352
Andreas Huber9575c962013-02-05 13:59:56 -08002353 sp<NuPlayerDriver> driver = mDriver.promote();
2354 if (driver != NULL) {
Marco Nelissendd114d12014-05-28 15:23:14 -07002355 // notify duration first, so that it's definitely set when
2356 // the app received the "prepare complete" callback.
2357 int64_t durationUs;
2358 if (mSource->getDuration(&durationUs) == OK) {
2359 driver->notifyDuration(durationUs);
2360 }
Andreas Huberec0c5972013-02-05 14:47:13 -08002361 driver->notifyPrepareCompleted(err);
Andreas Huber9575c962013-02-05 13:59:56 -08002362 }
Andreas Huber99759402013-04-01 14:28:31 -07002363
Andreas Huber9575c962013-02-05 13:59:56 -08002364 break;
2365 }
2366
Hassan Shojaniacefac142017-02-06 21:02:02 -08002367 // Modular DRM
2368 case Source::kWhatDrmInfo:
2369 {
2370 Parcel parcel;
2371 sp<ABuffer> drmInfo;
2372 CHECK(msg->findBuffer("drmInfo", &drmInfo));
2373 parcel.setData(drmInfo->data(), drmInfo->size());
2374
2375 ALOGV("onSourceNotify() kWhatDrmInfo MEDIA_DRM_INFO drmInfo: %p parcel size: %zu",
2376 drmInfo.get(), parcel.dataSize());
2377
2378 notifyListener(MEDIA_DRM_INFO, 0 /* ext1 */, 0 /* ext2 */, &parcel);
2379
2380 break;
2381 }
2382
Andreas Huber9575c962013-02-05 13:59:56 -08002383 case Source::kWhatFlagsChanged:
2384 {
2385 uint32_t flags;
2386 CHECK(msg->findInt32("flags", (int32_t *)&flags));
2387
Chong Zhang4b7069d2013-09-11 12:52:43 -07002388 sp<NuPlayerDriver> driver = mDriver.promote();
2389 if (driver != NULL) {
Hassan Shojaniacefac142017-02-06 21:02:02 -08002390
2391 ALOGV("onSourceNotify() kWhatFlagsChanged FLAG_CAN_PAUSE: %d "
2392 "FLAG_CAN_SEEK_BACKWARD: %d \n\t\t\t\t FLAG_CAN_SEEK_FORWARD: %d "
2393 "FLAG_CAN_SEEK: %d FLAG_DYNAMIC_DURATION: %d \n"
2394 "\t\t\t\t FLAG_SECURE: %d FLAG_PROTECTED: %d",
2395 (flags & Source::FLAG_CAN_PAUSE) != 0,
2396 (flags & Source::FLAG_CAN_SEEK_BACKWARD) != 0,
2397 (flags & Source::FLAG_CAN_SEEK_FORWARD) != 0,
2398 (flags & Source::FLAG_CAN_SEEK) != 0,
2399 (flags & Source::FLAG_DYNAMIC_DURATION) != 0,
2400 (flags & Source::FLAG_SECURE) != 0,
2401 (flags & Source::FLAG_PROTECTED) != 0);
2402
Wei Jia895651b2014-12-10 17:31:52 -08002403 if ((flags & NuPlayer::Source::FLAG_CAN_SEEK) == 0) {
2404 driver->notifyListener(
2405 MEDIA_INFO, MEDIA_INFO_NOT_SEEKABLE, 0);
2406 }
Chong Zhang4b7069d2013-09-11 12:52:43 -07002407 driver->notifyFlagsChanged(flags);
2408 }
2409
Andreas Huber9575c962013-02-05 13:59:56 -08002410 if ((mSourceFlags & Source::FLAG_DYNAMIC_DURATION)
2411 && (!(flags & Source::FLAG_DYNAMIC_DURATION))) {
2412 cancelPollDuration();
2413 } else if (!(mSourceFlags & Source::FLAG_DYNAMIC_DURATION)
2414 && (flags & Source::FLAG_DYNAMIC_DURATION)
2415 && (mAudioDecoder != NULL || mVideoDecoder != NULL)) {
2416 schedulePollDuration();
2417 }
2418
2419 mSourceFlags = flags;
2420 break;
2421 }
2422
2423 case Source::kWhatVideoSizeChanged:
2424 {
Chong Zhangced1c2f2014-08-08 15:22:35 -07002425 sp<AMessage> format;
2426 CHECK(msg->findMessage("format", &format));
Andreas Huber9575c962013-02-05 13:59:56 -08002427
Chong Zhangced1c2f2014-08-08 15:22:35 -07002428 updateVideoSize(format);
Andreas Huber9575c962013-02-05 13:59:56 -08002429 break;
2430 }
2431
Chong Zhang2a3cc9a2014-08-21 17:48:26 -07002432 case Source::kWhatBufferingUpdate:
2433 {
2434 int32_t percentage;
2435 CHECK(msg->findInt32("percentage", &percentage));
2436
2437 notifyListener(MEDIA_BUFFERING_UPDATE, percentage, 0);
2438 break;
2439 }
2440
Chong Zhangefbb6192015-01-30 17:13:27 -08002441 case Source::kWhatPauseOnBufferingStart:
2442 {
2443 // ignore if not playing
Chong Zhang8a048332015-05-06 15:16:28 -07002444 if (mStarted) {
Chong Zhangefbb6192015-01-30 17:13:27 -08002445 ALOGI("buffer low, pausing...");
2446
Chong Zhang8a048332015-05-06 15:16:28 -07002447 mPausedForBuffering = true;
Chong Zhangefbb6192015-01-30 17:13:27 -08002448 onPause();
2449 }
Wei Jiabfe82072016-05-20 09:21:50 -07002450 notifyListener(MEDIA_INFO, MEDIA_INFO_BUFFERING_START, 0);
Roger Jönssonb50e83e2013-01-21 16:26:41 +01002451 break;
2452 }
2453
Chong Zhangefbb6192015-01-30 17:13:27 -08002454 case Source::kWhatResumeOnBufferingEnd:
2455 {
2456 // ignore if not playing
Chong Zhang8a048332015-05-06 15:16:28 -07002457 if (mStarted) {
Chong Zhangefbb6192015-01-30 17:13:27 -08002458 ALOGI("buffer ready, resuming...");
2459
Chong Zhang8a048332015-05-06 15:16:28 -07002460 mPausedForBuffering = false;
2461
2462 // do not resume yet if client didn't unpause
2463 if (!mPausedByClient) {
2464 onResume();
2465 }
Chong Zhangefbb6192015-01-30 17:13:27 -08002466 }
Wei Jia3bed45a2016-02-17 11:06:47 -08002467 notifyListener(MEDIA_INFO, MEDIA_INFO_BUFFERING_END, 0);
Roger Jönssonb50e83e2013-01-21 16:26:41 +01002468 break;
2469 }
2470
Chong Zhangefbb6192015-01-30 17:13:27 -08002471 case Source::kWhatCacheStats:
2472 {
2473 int32_t kbps;
2474 CHECK(msg->findInt32("bandwidth", &kbps));
2475
2476 notifyListener(MEDIA_INFO, MEDIA_INFO_NETWORK_BANDWIDTH, kbps);
2477 break;
2478 }
2479
Chong Zhangdcb89b32013-08-06 09:44:47 -07002480 case Source::kWhatSubtitleData:
2481 {
2482 sp<ABuffer> buffer;
2483 CHECK(msg->findBuffer("buffer", &buffer));
2484
Chong Zhang404fced2014-06-11 14:45:31 -07002485 sendSubtitleData(buffer, 0 /* baseIndex */);
Chong Zhangdcb89b32013-08-06 09:44:47 -07002486 break;
2487 }
2488
Robert Shih08528432015-04-08 09:06:54 -07002489 case Source::kWhatTimedMetaData:
2490 {
2491 sp<ABuffer> buffer;
2492 if (!msg->findBuffer("buffer", &buffer)) {
2493 notifyListener(MEDIA_INFO, MEDIA_INFO_METADATA_UPDATE, 0);
2494 } else {
2495 sendTimedMetaData(buffer);
2496 }
2497 break;
2498 }
2499
Robert Shihd3b0bbb2014-07-23 15:00:25 -07002500 case Source::kWhatTimedTextData:
2501 {
2502 int32_t generation;
2503 if (msg->findInt32("generation", &generation)
2504 && generation != mTimedTextGeneration) {
2505 break;
2506 }
2507
2508 sp<ABuffer> buffer;
2509 CHECK(msg->findBuffer("buffer", &buffer));
2510
2511 sp<NuPlayerDriver> driver = mDriver.promote();
2512 if (driver == NULL) {
2513 break;
2514 }
2515
2516 int posMs;
2517 int64_t timeUs, posUs;
2518 driver->getCurrentPosition(&posMs);
Patrik2 Carlsson24d484b2015-01-27 16:49:45 +01002519 posUs = (int64_t) posMs * 1000ll;
Robert Shihd3b0bbb2014-07-23 15:00:25 -07002520 CHECK(buffer->meta()->findInt64("timeUs", &timeUs));
2521
2522 if (posUs < timeUs) {
2523 if (!msg->findInt32("generation", &generation)) {
2524 msg->setInt32("generation", mTimedTextGeneration);
2525 }
2526 msg->post(timeUs - posUs);
2527 } else {
2528 sendTimedTextData(buffer);
2529 }
2530 break;
2531 }
2532
Andreas Huber14f76722013-01-15 09:04:18 -08002533 case Source::kWhatQueueDecoderShutdown:
2534 {
2535 int32_t audio, video;
2536 CHECK(msg->findInt32("audio", &audio));
2537 CHECK(msg->findInt32("video", &video));
2538
2539 sp<AMessage> reply;
2540 CHECK(msg->findMessage("reply", &reply));
2541
2542 queueDecoderShutdown(audio, video, reply);
2543 break;
2544 }
2545
Ronghua Wu80276872014-08-28 15:50:29 -07002546 case Source::kWhatDrmNoLicense:
2547 {
2548 notifyListener(MEDIA_ERROR, MEDIA_ERROR_UNKNOWN, ERROR_DRM_NO_LICENSE);
2549 break;
2550 }
2551
Andreas Huber9575c962013-02-05 13:59:56 -08002552 default:
2553 TRESPASS();
2554 }
2555}
2556
Chong Zhanga7fa1d92014-06-11 14:49:23 -07002557void NuPlayer::onClosedCaptionNotify(const sp<AMessage> &msg) {
2558 int32_t what;
2559 CHECK(msg->findInt32("what", &what));
2560
2561 switch (what) {
2562 case NuPlayer::CCDecoder::kWhatClosedCaptionData:
2563 {
2564 sp<ABuffer> buffer;
2565 CHECK(msg->findBuffer("buffer", &buffer));
2566
2567 size_t inbandTracks = 0;
2568 if (mSource != NULL) {
2569 inbandTracks = mSource->getTrackCount();
2570 }
2571
2572 sendSubtitleData(buffer, inbandTracks);
2573 break;
2574 }
2575
2576 case NuPlayer::CCDecoder::kWhatTrackAdded:
2577 {
2578 notifyListener(MEDIA_INFO, MEDIA_INFO_METADATA_UPDATE, 0);
2579
2580 break;
2581 }
2582
2583 default:
2584 TRESPASS();
2585 }
2586
2587
2588}
2589
Chong Zhang404fced2014-06-11 14:45:31 -07002590void NuPlayer::sendSubtitleData(const sp<ABuffer> &buffer, int32_t baseIndex) {
2591 int32_t trackIndex;
2592 int64_t timeUs, durationUs;
2593 CHECK(buffer->meta()->findInt32("trackIndex", &trackIndex));
2594 CHECK(buffer->meta()->findInt64("timeUs", &timeUs));
2595 CHECK(buffer->meta()->findInt64("durationUs", &durationUs));
2596
2597 Parcel in;
2598 in.writeInt32(trackIndex + baseIndex);
2599 in.writeInt64(timeUs);
2600 in.writeInt64(durationUs);
2601 in.writeInt32(buffer->size());
2602 in.writeInt32(buffer->size());
2603 in.write(buffer->data(), buffer->size());
2604
2605 notifyListener(MEDIA_SUBTITLE_DATA, 0, 0, &in);
2606}
Robert Shihd3b0bbb2014-07-23 15:00:25 -07002607
Robert Shih08528432015-04-08 09:06:54 -07002608void NuPlayer::sendTimedMetaData(const sp<ABuffer> &buffer) {
2609 int64_t timeUs;
2610 CHECK(buffer->meta()->findInt64("timeUs", &timeUs));
2611
2612 Parcel in;
2613 in.writeInt64(timeUs);
2614 in.writeInt32(buffer->size());
2615 in.writeInt32(buffer->size());
2616 in.write(buffer->data(), buffer->size());
2617
2618 notifyListener(MEDIA_META_DATA, 0, 0, &in);
2619}
2620
Robert Shihd3b0bbb2014-07-23 15:00:25 -07002621void NuPlayer::sendTimedTextData(const sp<ABuffer> &buffer) {
2622 const void *data;
2623 size_t size = 0;
2624 int64_t timeUs;
Marco Nelissen55e2f4c2015-09-04 15:57:15 -07002625 int32_t flag = TextDescriptions::IN_BAND_TEXT_3GPP;
Robert Shihd3b0bbb2014-07-23 15:00:25 -07002626
2627 AString mime;
2628 CHECK(buffer->meta()->findString("mime", &mime));
2629 CHECK(strcasecmp(mime.c_str(), MEDIA_MIMETYPE_TEXT_3GPP) == 0);
2630
2631 data = buffer->data();
2632 size = buffer->size();
2633
2634 Parcel parcel;
2635 if (size > 0) {
2636 CHECK(buffer->meta()->findInt64("timeUs", &timeUs));
Marco Nelissen55e2f4c2015-09-04 15:57:15 -07002637 int32_t global = 0;
2638 if (buffer->meta()->findInt32("global", &global) && global) {
2639 flag |= TextDescriptions::GLOBAL_DESCRIPTIONS;
2640 } else {
2641 flag |= TextDescriptions::LOCAL_DESCRIPTIONS;
2642 }
Robert Shihd3b0bbb2014-07-23 15:00:25 -07002643 TextDescriptions::getParcelOfDescriptions(
2644 (const uint8_t *)data, size, flag, timeUs / 1000, &parcel);
2645 }
2646
2647 if ((parcel.dataSize() > 0)) {
2648 notifyListener(MEDIA_TIMED_TEXT, 0, 0, &parcel);
2649 } else { // send an empty timed text
2650 notifyListener(MEDIA_TIMED_TEXT, 0, 0);
2651 }
2652}
Hassan Shojaniacefac142017-02-06 21:02:02 -08002653
2654// Modular DRM begin
2655status_t NuPlayer::prepareDrm(const uint8_t uuid[16], const Vector<uint8_t> &drmSessionId)
2656{
2657 ALOGV("prepareDrm ");
2658
2659 // Passing to the looper anyway; called in a pre-config prepared state so no race on mCrypto
2660 sp<AMessage> msg = new AMessage(kWhatPrepareDrm, this);
2661 // synchronous call so just passing the address but with local copies of "const" args
2662 uint8_t UUID[16];
2663 memcpy(UUID, uuid, sizeof(UUID));
2664 Vector<uint8_t> sessionId = drmSessionId;
2665 msg->setPointer("uuid", (void*)UUID);
2666 msg->setPointer("drmSessionId", (void*)&sessionId);
2667
2668 sp<AMessage> response;
2669 status_t status = msg->postAndAwaitResponse(&response);
2670
2671 if (status == OK && response != NULL) {
2672 CHECK(response->findInt32("status", &status));
2673 ALOGV("prepareDrm ret: %d ", status);
2674 } else {
2675 ALOGE("prepareDrm err: %d", status);
2676 }
2677
2678 return status;
2679}
2680
2681status_t NuPlayer::releaseDrm()
2682{
2683 ALOGV("releaseDrm ");
2684
2685 sp<AMessage> msg = new AMessage(kWhatReleaseDrm, this);
2686
2687 sp<AMessage> response;
2688 status_t status = msg->postAndAwaitResponse(&response);
2689
2690 if (status == OK && response != NULL) {
2691 CHECK(response->findInt32("status", &status));
2692 ALOGV("releaseDrm ret: %d ", status);
2693 } else {
2694 ALOGE("releaseDrm err: %d", status);
2695 }
2696
2697 return status;
2698}
2699
2700status_t NuPlayer::onPrepareDrm(const sp<AMessage> &msg)
2701{
2702 // TODO change to ALOGV
2703 ALOGD("onPrepareDrm ");
2704
2705 status_t status = INVALID_OPERATION;
2706 if (mSource == NULL) {
2707 ALOGE("onPrepareDrm: No source. onPrepareDrm failed with %d.", status);
2708 return status;
2709 }
2710
2711 uint8_t *uuid;
2712 Vector<uint8_t> *drmSessionId;
2713 CHECK(msg->findPointer("uuid", (void**)&uuid));
2714 CHECK(msg->findPointer("drmSessionId", (void**)&drmSessionId));
2715
2716 status = OK;
2717 sp<ICrypto> crypto = NULL;
2718
2719 status = mSource->prepareDrm(uuid, *drmSessionId, &crypto);
2720 if (crypto == NULL) {
2721 ALOGE("onPrepareDrm: mSource->prepareDrm failed. status: %d", status);
2722 return status;
2723 }
2724 ALOGV("onPrepareDrm: mSource->prepareDrm succeeded");
2725
2726 if (mCrypto != NULL) {
2727 ALOGE("onPrepareDrm: Unexpected. Already having mCrypto: %p (%d)",
2728 mCrypto.get(), mCrypto->getStrongCount());
2729 mCrypto.clear();
2730 }
2731
2732 mCrypto = crypto;
2733 mIsDrmProtected = true;
2734 // TODO change to ALOGV
2735 ALOGD("onPrepareDrm: mCrypto: %p (%d)", mCrypto.get(),
2736 (mCrypto != NULL ? mCrypto->getStrongCount() : 0));
2737
2738 return status;
2739}
2740
2741status_t NuPlayer::onReleaseDrm()
2742{
2743 // TODO change to ALOGV
2744 ALOGD("onReleaseDrm ");
2745
Hassan Shojania50b20c92017-02-16 18:28:58 -08002746 if (!mIsDrmProtected) {
2747 ALOGW("onReleaseDrm: Unexpected. mIsDrmProtected is already false.");
2748 }
2749
2750 mIsDrmProtected = false;
Hassan Shojaniacefac142017-02-06 21:02:02 -08002751
2752 status_t status;
2753 if (mCrypto != NULL) {
2754 status=OK;
2755 // first making sure the codecs have released their crypto reference
2756 const sp<DecoderBase> &videoDecoder = getDecoder(false/*audio*/);
2757 if (videoDecoder != NULL) {
2758 status = videoDecoder->releaseCrypto();
2759 ALOGV("onReleaseDrm: video decoder ret: %d", status);
2760 }
2761
2762 const sp<DecoderBase> &audioDecoder = getDecoder(true/*audio*/);
2763 if (audioDecoder != NULL) {
2764 status_t status_audio = audioDecoder->releaseCrypto();
2765 if (status == OK) { // otherwise, returning the first error
2766 status = status_audio;
2767 }
2768 ALOGV("onReleaseDrm: audio decoder ret: %d", status_audio);
2769 }
2770
2771 // TODO change to ALOGV
2772 ALOGD("onReleaseDrm: mCrypto: %p (%d)", mCrypto.get(),
2773 (mCrypto != NULL ? mCrypto->getStrongCount() : 0));
2774 mCrypto.clear();
2775 } else { // mCrypto == NULL
2776 ALOGE("onReleaseDrm: Unexpected. There is no crypto.");
2777 status = INVALID_OPERATION;
2778 }
2779
2780 return status;
2781}
2782// Modular DRM end
Andreas Huberb5f25f02013-02-05 10:14:26 -08002783////////////////////////////////////////////////////////////////////////////////
2784
Chong Zhangced1c2f2014-08-08 15:22:35 -07002785sp<AMessage> NuPlayer::Source::getFormat(bool audio) {
2786 sp<MetaData> meta = getFormatMeta(audio);
2787
2788 if (meta == NULL) {
2789 return NULL;
2790 }
2791
2792 sp<AMessage> msg = new AMessage;
2793
2794 if(convertMetaDataToMessage(meta, &msg) == OK) {
2795 return msg;
2796 }
2797 return NULL;
2798}
2799
Andreas Huber9575c962013-02-05 13:59:56 -08002800void NuPlayer::Source::notifyFlagsChanged(uint32_t flags) {
2801 sp<AMessage> notify = dupNotify();
2802 notify->setInt32("what", kWhatFlagsChanged);
2803 notify->setInt32("flags", flags);
2804 notify->post();
2805}
2806
Chong Zhangced1c2f2014-08-08 15:22:35 -07002807void NuPlayer::Source::notifyVideoSizeChanged(const sp<AMessage> &format) {
Andreas Huber9575c962013-02-05 13:59:56 -08002808 sp<AMessage> notify = dupNotify();
2809 notify->setInt32("what", kWhatVideoSizeChanged);
Chong Zhangced1c2f2014-08-08 15:22:35 -07002810 notify->setMessage("format", format);
Andreas Huber9575c962013-02-05 13:59:56 -08002811 notify->post();
2812}
2813
Andreas Huberec0c5972013-02-05 14:47:13 -08002814void NuPlayer::Source::notifyPrepared(status_t err) {
Hassan Shojaniacefac142017-02-06 21:02:02 -08002815 ALOGV("Source::notifyPrepared %d", err);
Andreas Huber9575c962013-02-05 13:59:56 -08002816 sp<AMessage> notify = dupNotify();
2817 notify->setInt32("what", kWhatPrepared);
Andreas Huberec0c5972013-02-05 14:47:13 -08002818 notify->setInt32("err", err);
Andreas Huber9575c962013-02-05 13:59:56 -08002819 notify->post();
2820}
2821
Hassan Shojaniacefac142017-02-06 21:02:02 -08002822void NuPlayer::Source::notifyDrmInfo(const sp<ABuffer> &drmInfoBuffer)
2823{
2824 ALOGV("Source::notifyDrmInfo");
2825
2826 sp<AMessage> notify = dupNotify();
2827 notify->setInt32("what", kWhatDrmInfo);
2828 notify->setBuffer("drmInfo", drmInfoBuffer);
2829
2830 notify->post();
2831}
2832
Lajos Molnarfcd3e942015-03-31 10:06:48 -07002833void NuPlayer::Source::notifyInstantiateSecureDecoders(const sp<AMessage> &reply) {
2834 sp<AMessage> notify = dupNotify();
2835 notify->setInt32("what", kWhatInstantiateSecureDecoders);
2836 notify->setMessage("reply", reply);
2837 notify->post();
2838}
2839
Andreas Huber84333e02014-02-07 15:36:10 -08002840void NuPlayer::Source::onMessageReceived(const sp<AMessage> & /* msg */) {
Andreas Huberb5f25f02013-02-05 10:14:26 -08002841 TRESPASS();
2842}
2843
Andreas Huberf9334412010-12-15 15:17:42 -08002844} // namespace android