blob: 6593fcd2842329bbb0e537cd1eaa5d2d3708f922 [file] [log] [blame]
Andreas Huberf9334412010-12-15 15:17:42 -08001/*
2 * Copyright (C) 2010 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17//#define LOG_NDEBUG 0
18#define LOG_TAG "NuPlayer"
Ray Essickd4d00612017-01-03 09:36:27 -080019
20#include <inttypes.h>
21
Andreas Huberf9334412010-12-15 15:17:42 -080022#include <utils/Log.h>
23
24#include "NuPlayer.h"
Andreas Huber5bc087c2010-12-23 10:27:40 -080025
26#include "HTTPLiveSource.h"
Chong Zhang7137ec72014-11-12 16:41:05 -080027#include "NuPlayerCCDecoder.h"
Andreas Huberf9334412010-12-15 15:17:42 -080028#include "NuPlayerDecoder.h"
Chong Zhang7137ec72014-11-12 16:41:05 -080029#include "NuPlayerDecoderBase.h"
Wei Jiabc2fb722014-07-08 16:37:57 -070030#include "NuPlayerDecoderPassThrough.h"
Andreas Huber43c3e6c2011-01-05 12:17:08 -080031#include "NuPlayerDriver.h"
Andreas Huberf9334412010-12-15 15:17:42 -080032#include "NuPlayerRenderer.h"
Andreas Huber5bc087c2010-12-23 10:27:40 -080033#include "NuPlayerSource.h"
Andreas Huber2bfdd422011-10-11 15:24:07 -070034#include "RTSPSource.h"
Andreas Huber5bc087c2010-12-23 10:27:40 -080035#include "StreamingSource.h"
Andreas Huberafed0e12011-09-20 15:39:58 -070036#include "GenericSource.h"
Robert Shihd3b0bbb2014-07-23 15:00:25 -070037#include "TextDescriptions.h"
Andreas Huber5bc087c2010-12-23 10:27:40 -080038
39#include "ATSParser.h"
Andreas Huberf9334412010-12-15 15:17:42 -080040
Lajos Molnard9fd6312014-11-06 11:00:00 -080041#include <cutils/properties.h>
42
Lajos Molnar3a474aa2015-04-24 17:10:07 -070043#include <media/AudioResamplerPublic.h>
44#include <media/AVSyncSettings.h>
Wonsik Kim7e34bf52016-08-23 00:09:18 +090045#include <media/MediaCodecBuffer.h>
Lajos Molnar3a474aa2015-04-24 17:10:07 -070046
Andreas Huber3831a062010-12-21 10:22:33 -080047#include <media/stagefright/foundation/hexdump.h>
Andreas Huberf9334412010-12-15 15:17:42 -080048#include <media/stagefright/foundation/ABuffer.h>
49#include <media/stagefright/foundation/ADebug.h>
50#include <media/stagefright/foundation/AMessage.h>
Lajos Molnar09524832014-07-17 14:29:51 -070051#include <media/stagefright/MediaBuffer.h>
Andreas Huber3fe62152011-09-16 15:09:22 -070052#include <media/stagefright/MediaDefs.h>
Andreas Huberf9334412010-12-15 15:17:42 -080053#include <media/stagefright/MediaErrors.h>
54#include <media/stagefright/MetaData.h>
Lajos Molnar1de1e252015-04-30 18:18:34 -070055
Andy McFadden8ba01022012-12-18 09:46:54 -080056#include <gui/IGraphicBufferProducer.h>
Lajos Molnar1de1e252015-04-30 18:18:34 -070057#include <gui/Surface.h>
Andreas Huberf9334412010-12-15 15:17:42 -080058
Andreas Huber3fe62152011-09-16 15:09:22 -070059#include "avc_utils.h"
60
Andreas Huber84066782011-08-16 09:34:26 -070061#include "ESDS.h"
62#include <media/stagefright/Utils.h>
63
Andreas Huberf9334412010-12-15 15:17:42 -080064namespace android {
65
Andreas Hubera1f8ab02012-11-30 10:53:22 -080066struct NuPlayer::Action : public RefBase {
67 Action() {}
68
69 virtual void execute(NuPlayer *player) = 0;
70
71private:
72 DISALLOW_EVIL_CONSTRUCTORS(Action);
73};
74
75struct NuPlayer::SeekAction : public Action {
Wei Jiac5de0912016-11-18 10:22:14 -080076 explicit SeekAction(int64_t seekTimeUs, MediaPlayerSeekMode mode)
Wei Jia14486822016-11-02 17:51:30 -070077 : mSeekTimeUs(seekTimeUs),
Wei Jiac5de0912016-11-18 10:22:14 -080078 mMode(mode) {
Andreas Hubera1f8ab02012-11-30 10:53:22 -080079 }
80
81 virtual void execute(NuPlayer *player) {
Wei Jiac5de0912016-11-18 10:22:14 -080082 player->performSeek(mSeekTimeUs, mMode);
Andreas Hubera1f8ab02012-11-30 10:53:22 -080083 }
84
85private:
86 int64_t mSeekTimeUs;
Wei Jiac5de0912016-11-18 10:22:14 -080087 MediaPlayerSeekMode mMode;
Andreas Hubera1f8ab02012-11-30 10:53:22 -080088
89 DISALLOW_EVIL_CONSTRUCTORS(SeekAction);
90};
91
Chong Zhangf8d71772014-11-26 15:08:34 -080092struct NuPlayer::ResumeDecoderAction : public Action {
Chih-Hung Hsieh090ef602016-04-27 10:39:54 -070093 explicit ResumeDecoderAction(bool needNotify)
Chong Zhangf8d71772014-11-26 15:08:34 -080094 : mNeedNotify(needNotify) {
95 }
96
97 virtual void execute(NuPlayer *player) {
98 player->performResumeDecoders(mNeedNotify);
99 }
100
101private:
102 bool mNeedNotify;
103
104 DISALLOW_EVIL_CONSTRUCTORS(ResumeDecoderAction);
105};
106
Andreas Huber57a339c2012-12-03 11:18:00 -0800107struct NuPlayer::SetSurfaceAction : public Action {
Chih-Hung Hsieh090ef602016-04-27 10:39:54 -0700108 explicit SetSurfaceAction(const sp<Surface> &surface)
Lajos Molnar1de1e252015-04-30 18:18:34 -0700109 : mSurface(surface) {
Andreas Huber57a339c2012-12-03 11:18:00 -0800110 }
111
112 virtual void execute(NuPlayer *player) {
Lajos Molnar1de1e252015-04-30 18:18:34 -0700113 player->performSetSurface(mSurface);
Andreas Huber57a339c2012-12-03 11:18:00 -0800114 }
115
116private:
Lajos Molnar1de1e252015-04-30 18:18:34 -0700117 sp<Surface> mSurface;
Andreas Huber57a339c2012-12-03 11:18:00 -0800118
119 DISALLOW_EVIL_CONSTRUCTORS(SetSurfaceAction);
120};
121
Wei Jiafef808d2014-10-31 17:57:05 -0700122struct NuPlayer::FlushDecoderAction : public Action {
123 FlushDecoderAction(FlushCommand audio, FlushCommand video)
Andreas Huber14f76722013-01-15 09:04:18 -0800124 : mAudio(audio),
125 mVideo(video) {
126 }
127
128 virtual void execute(NuPlayer *player) {
Wei Jiafef808d2014-10-31 17:57:05 -0700129 player->performDecoderFlush(mAudio, mVideo);
Andreas Huber14f76722013-01-15 09:04:18 -0800130 }
131
132private:
Wei Jiafef808d2014-10-31 17:57:05 -0700133 FlushCommand mAudio;
134 FlushCommand mVideo;
Andreas Huber14f76722013-01-15 09:04:18 -0800135
Wei Jiafef808d2014-10-31 17:57:05 -0700136 DISALLOW_EVIL_CONSTRUCTORS(FlushDecoderAction);
Andreas Huber14f76722013-01-15 09:04:18 -0800137};
138
139struct NuPlayer::PostMessageAction : public Action {
Chih-Hung Hsieh090ef602016-04-27 10:39:54 -0700140 explicit PostMessageAction(const sp<AMessage> &msg)
Andreas Huber14f76722013-01-15 09:04:18 -0800141 : mMessage(msg) {
142 }
143
144 virtual void execute(NuPlayer *) {
145 mMessage->post();
146 }
147
148private:
149 sp<AMessage> mMessage;
150
151 DISALLOW_EVIL_CONSTRUCTORS(PostMessageAction);
152};
153
Andreas Hubera1f8ab02012-11-30 10:53:22 -0800154// Use this if there's no state necessary to save in order to execute
155// the action.
156struct NuPlayer::SimpleAction : public Action {
157 typedef void (NuPlayer::*ActionFunc)();
158
Chih-Hung Hsieh090ef602016-04-27 10:39:54 -0700159 explicit SimpleAction(ActionFunc func)
Andreas Hubera1f8ab02012-11-30 10:53:22 -0800160 : mFunc(func) {
161 }
162
163 virtual void execute(NuPlayer *player) {
164 (player->*mFunc)();
165 }
166
167private:
168 ActionFunc mFunc;
169
170 DISALLOW_EVIL_CONSTRUCTORS(SimpleAction);
171};
172
Andreas Huberf9334412010-12-15 15:17:42 -0800173////////////////////////////////////////////////////////////////////////////////
174
Ronghua Wu68845c12015-07-21 09:50:48 -0700175NuPlayer::NuPlayer(pid_t pid)
Andreas Huber9b80c2b2011-06-30 15:47:02 -0700176 : mUIDValid(false),
Ronghua Wu68845c12015-07-21 09:50:48 -0700177 mPID(pid),
Andreas Huber9575c962013-02-05 13:59:56 -0800178 mSourceFlags(0),
Wei Jiabc2fb722014-07-08 16:37:57 -0700179 mOffloadAudio(false),
Wei Jia88703c32014-08-06 11:24:07 -0700180 mAudioDecoderGeneration(0),
181 mVideoDecoderGeneration(0),
Wei Jia57568df2014-09-22 10:16:29 -0700182 mRendererGeneration(0),
Robert Shih1a5c8592015-08-04 18:07:44 -0700183 mPreviousSeekTimeUs(0),
Andreas Huber9b80c2b2011-06-30 15:47:02 -0700184 mAudioEOS(false),
Andreas Huberf9334412010-12-15 15:17:42 -0800185 mVideoEOS(false),
Andreas Huber5bc087c2010-12-23 10:27:40 -0800186 mScanSourcesPending(false),
Andreas Huber1aef2112011-01-04 14:01:29 -0800187 mScanSourcesGeneration(0),
Andreas Huberb7c8e912012-11-27 15:02:53 -0800188 mPollDurationGeneration(0),
Robert Shihd3b0bbb2014-07-23 15:00:25 -0700189 mTimedTextGeneration(0),
Andreas Huberf9334412010-12-15 15:17:42 -0800190 mFlushingAudio(NONE),
Andreas Huber1aef2112011-01-04 14:01:29 -0800191 mFlushingVideo(NONE),
Chong Zhangf8d71772014-11-26 15:08:34 -0800192 mResumePending(false),
Andreas Huber57a339c2012-12-03 11:18:00 -0800193 mVideoScalingMode(NATIVE_WINDOW_SCALING_MODE_SCALE_TO_WINDOW),
Lajos Molnar3a474aa2015-04-24 17:10:07 -0700194 mPlaybackSettings(AUDIO_PLAYBACK_RATE_DEFAULT),
195 mVideoFpsHint(-1.f),
Chong Zhangefbb6192015-01-30 17:13:27 -0800196 mStarted(false),
Wei Jia8a092d32016-06-03 14:57:24 -0700197 mPrepared(false),
Ronghua Wu64c2d172015-10-07 16:52:19 -0700198 mResetting(false),
Robert Shih0c61a0d2015-07-06 15:09:10 -0700199 mSourceStarted(false),
Chong Zhangefbb6192015-01-30 17:13:27 -0800200 mPaused(false),
Wei Jia71c75e02016-02-04 09:40:47 -0800201 mPausedByClient(true),
Chong Zhang8a048332015-05-06 15:16:28 -0700202 mPausedForBuffering(false) {
Andy Hung8d121d42014-10-03 09:53:53 -0700203 clearFlushComplete();
Andreas Huberf9334412010-12-15 15:17:42 -0800204}
205
206NuPlayer::~NuPlayer() {
207}
208
Andreas Huber9b80c2b2011-06-30 15:47:02 -0700209void NuPlayer::setUID(uid_t uid) {
210 mUIDValid = true;
211 mUID = uid;
212}
213
Andreas Huber43c3e6c2011-01-05 12:17:08 -0800214void NuPlayer::setDriver(const wp<NuPlayerDriver> &driver) {
215 mDriver = driver;
Andreas Huberf9334412010-12-15 15:17:42 -0800216}
217
Andreas Huber9575c962013-02-05 13:59:56 -0800218void NuPlayer::setDataSourceAsync(const sp<IStreamSource> &source) {
Lajos Molnar1d15ab52015-03-04 16:46:34 -0800219 sp<AMessage> msg = new AMessage(kWhatSetDataSource, this);
Andreas Huberf9334412010-12-15 15:17:42 -0800220
Lajos Molnar1d15ab52015-03-04 16:46:34 -0800221 sp<AMessage> notify = new AMessage(kWhatSourceNotify, this);
Andreas Huberb5f25f02013-02-05 10:14:26 -0800222
Andreas Huber240abcc2014-02-13 13:32:37 -0800223 msg->setObject("source", new StreamingSource(notify, source));
Andreas Huber5bc087c2010-12-23 10:27:40 -0800224 msg->post();
225}
Andreas Huberf9334412010-12-15 15:17:42 -0800226
Andreas Huberafed0e12011-09-20 15:39:58 -0700227static bool IsHTTPLiveURL(const char *url) {
228 if (!strncasecmp("http://", url, 7)
Andreas Huber99759402013-04-01 14:28:31 -0700229 || !strncasecmp("https://", url, 8)
230 || !strncasecmp("file://", url, 7)) {
Andreas Huberafed0e12011-09-20 15:39:58 -0700231 size_t len = strlen(url);
232 if (len >= 5 && !strcasecmp(".m3u8", &url[len - 5])) {
233 return true;
234 }
235
236 if (strstr(url,"m3u8")) {
237 return true;
238 }
239 }
240
241 return false;
242}
243
Andreas Huber9575c962013-02-05 13:59:56 -0800244void NuPlayer::setDataSourceAsync(
Andreas Huber1b86fe02014-01-29 11:13:26 -0800245 const sp<IMediaHTTPService> &httpService,
246 const char *url,
247 const KeyedVector<String8, String8> *headers) {
Chong Zhang3de157d2014-08-05 20:54:44 -0700248
Lajos Molnar1d15ab52015-03-04 16:46:34 -0800249 sp<AMessage> msg = new AMessage(kWhatSetDataSource, this);
Oscar Rydhé7a33b772012-02-20 10:15:48 +0100250 size_t len = strlen(url);
Andreas Huber5bc087c2010-12-23 10:27:40 -0800251
Lajos Molnar1d15ab52015-03-04 16:46:34 -0800252 sp<AMessage> notify = new AMessage(kWhatSourceNotify, this);
Andreas Huberb5f25f02013-02-05 10:14:26 -0800253
Andreas Huberafed0e12011-09-20 15:39:58 -0700254 sp<Source> source;
255 if (IsHTTPLiveURL(url)) {
Andreas Huber81e68442014-02-05 11:52:33 -0800256 source = new HTTPLiveSource(notify, httpService, url, headers);
Andreas Huberafed0e12011-09-20 15:39:58 -0700257 } else if (!strncasecmp(url, "rtsp://", 7)) {
Andreas Huber1b86fe02014-01-29 11:13:26 -0800258 source = new RTSPSource(
259 notify, httpService, url, headers, mUIDValid, mUID);
Oscar Rydhé7a33b772012-02-20 10:15:48 +0100260 } else if ((!strncasecmp(url, "http://", 7)
261 || !strncasecmp(url, "https://", 8))
262 && ((len >= 4 && !strcasecmp(".sdp", &url[len - 4]))
263 || strstr(url, ".sdp?"))) {
Andreas Huber1b86fe02014-01-29 11:13:26 -0800264 source = new RTSPSource(
265 notify, httpService, url, headers, mUIDValid, mUID, true);
Andreas Huber2bfdd422011-10-11 15:24:07 -0700266 } else {
Chong Zhang3de157d2014-08-05 20:54:44 -0700267 sp<GenericSource> genericSource =
268 new GenericSource(notify, mUIDValid, mUID);
Andreas Huber2bfdd422011-10-11 15:24:07 -0700269
Chong Zhanga19f33e2014-08-07 15:35:07 -0700270 status_t err = genericSource->setDataSource(httpService, url, headers);
Chong Zhang3de157d2014-08-05 20:54:44 -0700271
272 if (err == OK) {
273 source = genericSource;
274 } else {
Chong Zhanga19f33e2014-08-07 15:35:07 -0700275 ALOGE("Failed to set data source!");
Chong Zhang3de157d2014-08-05 20:54:44 -0700276 }
277 }
Andreas Huberafed0e12011-09-20 15:39:58 -0700278 msg->setObject("source", source);
279 msg->post();
280}
281
Andreas Huber9575c962013-02-05 13:59:56 -0800282void NuPlayer::setDataSourceAsync(int fd, int64_t offset, int64_t length) {
Lajos Molnar1d15ab52015-03-04 16:46:34 -0800283 sp<AMessage> msg = new AMessage(kWhatSetDataSource, this);
Andreas Huberafed0e12011-09-20 15:39:58 -0700284
Lajos Molnar1d15ab52015-03-04 16:46:34 -0800285 sp<AMessage> notify = new AMessage(kWhatSourceNotify, this);
Andreas Huberb5f25f02013-02-05 10:14:26 -0800286
Chong Zhang3de157d2014-08-05 20:54:44 -0700287 sp<GenericSource> source =
288 new GenericSource(notify, mUIDValid, mUID);
289
Chong Zhanga19f33e2014-08-07 15:35:07 -0700290 status_t err = source->setDataSource(fd, offset, length);
Chong Zhang3de157d2014-08-05 20:54:44 -0700291
292 if (err != OK) {
Chong Zhanga19f33e2014-08-07 15:35:07 -0700293 ALOGE("Failed to set data source!");
Chong Zhang3de157d2014-08-05 20:54:44 -0700294 source = NULL;
295 }
296
Andreas Huberafed0e12011-09-20 15:39:58 -0700297 msg->setObject("source", source);
Andreas Huberf9334412010-12-15 15:17:42 -0800298 msg->post();
299}
300
Chris Watkins99f31602015-03-20 13:06:33 -0700301void NuPlayer::setDataSourceAsync(const sp<DataSource> &dataSource) {
302 sp<AMessage> msg = new AMessage(kWhatSetDataSource, this);
303 sp<AMessage> notify = new AMessage(kWhatSourceNotify, this);
304
305 sp<GenericSource> source = new GenericSource(notify, mUIDValid, mUID);
306 status_t err = source->setDataSource(dataSource);
307
308 if (err != OK) {
309 ALOGE("Failed to set data source!");
310 source = NULL;
311 }
312
313 msg->setObject("source", source);
314 msg->post();
315}
316
Wei Jia48fa06d2016-12-20 15:30:49 -0800317status_t NuPlayer::getDefaultBufferingSettings(
318 BufferingSettings *buffering /* nonnull */) {
319 sp<AMessage> msg = new AMessage(kWhatGetDefaultBufferingSettings, this);
320 sp<AMessage> response;
321 status_t err = msg->postAndAwaitResponse(&response);
322 if (err == OK && response != NULL) {
323 CHECK(response->findInt32("err", &err));
324 if (err == OK) {
325 readFromAMessage(response, buffering);
326 }
327 }
328 return err;
329}
330
331status_t NuPlayer::setBufferingSettings(const BufferingSettings& buffering) {
332 sp<AMessage> msg = new AMessage(kWhatSetBufferingSettings, this);
333 writeToAMessage(msg, buffering);
334 sp<AMessage> response;
335 status_t err = msg->postAndAwaitResponse(&response);
336 if (err == OK && response != NULL) {
337 CHECK(response->findInt32("err", &err));
338 }
339 return err;
340}
341
Andreas Huber9575c962013-02-05 13:59:56 -0800342void NuPlayer::prepareAsync() {
Lajos Molnar1d15ab52015-03-04 16:46:34 -0800343 (new AMessage(kWhatPrepare, this))->post();
Andreas Huber9575c962013-02-05 13:59:56 -0800344}
345
Andreas Huber57a339c2012-12-03 11:18:00 -0800346void NuPlayer::setVideoSurfaceTextureAsync(
Andy McFadden8ba01022012-12-18 09:46:54 -0800347 const sp<IGraphicBufferProducer> &bufferProducer) {
Lajos Molnar1de1e252015-04-30 18:18:34 -0700348 sp<AMessage> msg = new AMessage(kWhatSetVideoSurface, this);
Andreas Huber57a339c2012-12-03 11:18:00 -0800349
Andy McFadden8ba01022012-12-18 09:46:54 -0800350 if (bufferProducer == NULL) {
Lajos Molnar1de1e252015-04-30 18:18:34 -0700351 msg->setObject("surface", NULL);
Andreas Huber57a339c2012-12-03 11:18:00 -0800352 } else {
Lajos Molnar1de1e252015-04-30 18:18:34 -0700353 msg->setObject("surface", new Surface(bufferProducer, true /* controlledByApp */));
Andreas Huber57a339c2012-12-03 11:18:00 -0800354 }
355
Andreas Huberf9334412010-12-15 15:17:42 -0800356 msg->post();
357}
358
359void NuPlayer::setAudioSink(const sp<MediaPlayerBase::AudioSink> &sink) {
Lajos Molnar1d15ab52015-03-04 16:46:34 -0800360 sp<AMessage> msg = new AMessage(kWhatSetAudioSink, this);
Andreas Huberf9334412010-12-15 15:17:42 -0800361 msg->setObject("sink", sink);
362 msg->post();
363}
364
365void NuPlayer::start() {
Lajos Molnar1d15ab52015-03-04 16:46:34 -0800366 (new AMessage(kWhatStart, this))->post();
Andreas Huberf9334412010-12-15 15:17:42 -0800367}
368
Lajos Molnar3a474aa2015-04-24 17:10:07 -0700369status_t NuPlayer::setPlaybackSettings(const AudioPlaybackRate &rate) {
370 // do some cursory validation of the settings here. audio modes are
371 // only validated when set on the audiosink.
372 if ((rate.mSpeed != 0.f && rate.mSpeed < AUDIO_TIMESTRETCH_SPEED_MIN)
373 || rate.mSpeed > AUDIO_TIMESTRETCH_SPEED_MAX
374 || rate.mPitch < AUDIO_TIMESTRETCH_SPEED_MIN
375 || rate.mPitch > AUDIO_TIMESTRETCH_SPEED_MAX) {
376 return BAD_VALUE;
377 }
378 sp<AMessage> msg = new AMessage(kWhatConfigPlayback, this);
379 writeToAMessage(msg, rate);
380 sp<AMessage> response;
381 status_t err = msg->postAndAwaitResponse(&response);
382 if (err == OK && response != NULL) {
383 CHECK(response->findInt32("err", &err));
384 }
385 return err;
386}
387
388status_t NuPlayer::getPlaybackSettings(AudioPlaybackRate *rate /* nonnull */) {
389 sp<AMessage> msg = new AMessage(kWhatGetPlaybackSettings, this);
390 sp<AMessage> response;
391 status_t err = msg->postAndAwaitResponse(&response);
392 if (err == OK && response != NULL) {
393 CHECK(response->findInt32("err", &err));
394 if (err == OK) {
395 readFromAMessage(response, rate);
396 }
397 }
398 return err;
399}
400
401status_t NuPlayer::setSyncSettings(const AVSyncSettings &sync, float videoFpsHint) {
402 sp<AMessage> msg = new AMessage(kWhatConfigSync, this);
403 writeToAMessage(msg, sync, videoFpsHint);
404 sp<AMessage> response;
405 status_t err = msg->postAndAwaitResponse(&response);
406 if (err == OK && response != NULL) {
407 CHECK(response->findInt32("err", &err));
408 }
409 return err;
410}
411
412status_t NuPlayer::getSyncSettings(
413 AVSyncSettings *sync /* nonnull */, float *videoFps /* nonnull */) {
414 sp<AMessage> msg = new AMessage(kWhatGetSyncSettings, this);
415 sp<AMessage> response;
416 status_t err = msg->postAndAwaitResponse(&response);
417 if (err == OK && response != NULL) {
418 CHECK(response->findInt32("err", &err));
419 if (err == OK) {
420 readFromAMessage(response, sync, videoFps);
421 }
422 }
423 return err;
Wei Jia98160162015-02-04 17:01:11 -0800424}
425
Andreas Huber43c3e6c2011-01-05 12:17:08 -0800426void NuPlayer::pause() {
Lajos Molnar1d15ab52015-03-04 16:46:34 -0800427 (new AMessage(kWhatPause, this))->post();
Andreas Huber43c3e6c2011-01-05 12:17:08 -0800428}
429
Andreas Huber1aef2112011-01-04 14:01:29 -0800430void NuPlayer::resetAsync() {
Wei Jiac45a4b22016-04-15 15:30:23 -0700431 sp<Source> source;
432 {
433 Mutex::Autolock autoLock(mSourceLock);
434 source = mSource;
435 }
436
437 if (source != NULL) {
Chong Zhang48296b72014-09-14 14:28:45 -0700438 // During a reset, the data source might be unresponsive already, we need to
439 // disconnect explicitly so that reads exit promptly.
440 // We can't queue the disconnect request to the looper, as it might be
441 // queued behind a stuck read and never gets processed.
442 // Doing a disconnect outside the looper to allows the pending reads to exit
443 // (either successfully or with error).
Wei Jiac45a4b22016-04-15 15:30:23 -0700444 source->disconnect();
Chong Zhang48296b72014-09-14 14:28:45 -0700445 }
446
Lajos Molnar1d15ab52015-03-04 16:46:34 -0800447 (new AMessage(kWhatReset, this))->post();
Andreas Huber1aef2112011-01-04 14:01:29 -0800448}
449
Wei Jiac5de0912016-11-18 10:22:14 -0800450void NuPlayer::seekToAsync(int64_t seekTimeUs, MediaPlayerSeekMode mode, bool needNotify) {
Lajos Molnar1d15ab52015-03-04 16:46:34 -0800451 sp<AMessage> msg = new AMessage(kWhatSeek, this);
Andreas Huber43c3e6c2011-01-05 12:17:08 -0800452 msg->setInt64("seekTimeUs", seekTimeUs);
Wei Jiac5de0912016-11-18 10:22:14 -0800453 msg->setInt32("mode", mode);
Wei Jiae427abf2014-09-22 15:21:11 -0700454 msg->setInt32("needNotify", needNotify);
Andreas Huber43c3e6c2011-01-05 12:17:08 -0800455 msg->post();
456}
457
Andreas Huber53df1a42010-12-22 10:03:04 -0800458
Chong Zhang404fced2014-06-11 14:45:31 -0700459void NuPlayer::writeTrackInfo(
Chih-Hung Hsiehe964d4e2016-08-09 14:31:32 -0700460 Parcel* reply, const sp<AMessage>& format) const {
Marco Nelissenc367ca12015-09-15 09:51:59 -0700461 if (format == NULL) {
462 ALOGE("NULL format");
463 return;
464 }
Chong Zhang404fced2014-06-11 14:45:31 -0700465 int32_t trackType;
Marco Nelissen9436e482015-09-15 10:21:53 -0700466 if (!format->findInt32("type", &trackType)) {
Marco Nelissenc367ca12015-09-15 09:51:59 -0700467 ALOGE("no track type");
468 return;
469 }
Chong Zhang404fced2014-06-11 14:45:31 -0700470
Robert Shih755106e2015-04-30 14:36:45 -0700471 AString mime;
Robert Shih2e3a4252015-05-06 10:21:15 -0700472 if (!format->findString("mime", &mime)) {
473 // Java MediaPlayer only uses mimetype for subtitle and timedtext tracks.
474 // If we can't find the mimetype here it means that we wouldn't be needing
475 // the mimetype on the Java end. We still write a placeholder mime to keep the
476 // (de)serialization logic simple.
477 if (trackType == MEDIA_TRACK_TYPE_AUDIO) {
478 mime = "audio/";
479 } else if (trackType == MEDIA_TRACK_TYPE_VIDEO) {
480 mime = "video/";
481 } else {
Marco Nelissenc367ca12015-09-15 09:51:59 -0700482 ALOGE("unknown track type: %d", trackType);
483 return;
Robert Shih2e3a4252015-05-06 10:21:15 -0700484 }
485 }
Robert Shih755106e2015-04-30 14:36:45 -0700486
Chong Zhang404fced2014-06-11 14:45:31 -0700487 AString lang;
Marco Nelissen9436e482015-09-15 10:21:53 -0700488 if (!format->findString("language", &lang)) {
Marco Nelissenc367ca12015-09-15 09:51:59 -0700489 ALOGE("no language");
490 return;
491 }
Chong Zhang404fced2014-06-11 14:45:31 -0700492
493 reply->writeInt32(2); // write something non-zero
494 reply->writeInt32(trackType);
Robert Shih755106e2015-04-30 14:36:45 -0700495 reply->writeString16(String16(mime.c_str()));
Chong Zhang404fced2014-06-11 14:45:31 -0700496 reply->writeString16(String16(lang.c_str()));
497
498 if (trackType == MEDIA_TRACK_TYPE_SUBTITLE) {
Chong Zhang404fced2014-06-11 14:45:31 -0700499 int32_t isAuto, isDefault, isForced;
500 CHECK(format->findInt32("auto", &isAuto));
501 CHECK(format->findInt32("default", &isDefault));
502 CHECK(format->findInt32("forced", &isForced));
503
Chong Zhang404fced2014-06-11 14:45:31 -0700504 reply->writeInt32(isAuto);
505 reply->writeInt32(isDefault);
506 reply->writeInt32(isForced);
507 }
508}
509
Andreas Huberf9334412010-12-15 15:17:42 -0800510void NuPlayer::onMessageReceived(const sp<AMessage> &msg) {
511 switch (msg->what()) {
512 case kWhatSetDataSource:
513 {
Steve Block3856b092011-10-20 11:56:00 +0100514 ALOGV("kWhatSetDataSource");
Andreas Huberf9334412010-12-15 15:17:42 -0800515
516 CHECK(mSource == NULL);
517
Chong Zhang3de157d2014-08-05 20:54:44 -0700518 status_t err = OK;
Andreas Huber5bc087c2010-12-23 10:27:40 -0800519 sp<RefBase> obj;
520 CHECK(msg->findObject("source", &obj));
Chong Zhang3de157d2014-08-05 20:54:44 -0700521 if (obj != NULL) {
Wei Jiac45a4b22016-04-15 15:30:23 -0700522 Mutex::Autolock autoLock(mSourceLock);
Chong Zhang3de157d2014-08-05 20:54:44 -0700523 mSource = static_cast<Source *>(obj.get());
Chong Zhang3de157d2014-08-05 20:54:44 -0700524 } else {
525 err = UNKNOWN_ERROR;
526 }
Andreas Huber9575c962013-02-05 13:59:56 -0800527
528 CHECK(mDriver != NULL);
529 sp<NuPlayerDriver> driver = mDriver.promote();
530 if (driver != NULL) {
Chong Zhang3de157d2014-08-05 20:54:44 -0700531 driver->notifySetDataSourceCompleted(err);
Andreas Huber9575c962013-02-05 13:59:56 -0800532 }
533 break;
534 }
535
Wei Jia48fa06d2016-12-20 15:30:49 -0800536 case kWhatGetDefaultBufferingSettings:
537 {
538 sp<AReplyToken> replyID;
539 CHECK(msg->senderAwaitsResponse(&replyID));
540
541 ALOGV("kWhatGetDefaultBufferingSettings");
542 BufferingSettings buffering;
543 status_t err = OK;
544 if (mSource != NULL) {
545 err = mSource->getDefaultBufferingSettings(&buffering);
546 } else {
547 err = INVALID_OPERATION;
548 }
549 sp<AMessage> response = new AMessage;
550 if (err == OK) {
551 writeToAMessage(response, buffering);
552 }
553 response->setInt32("err", err);
554 response->postReply(replyID);
555 break;
556 }
557
558 case kWhatSetBufferingSettings:
559 {
560 sp<AReplyToken> replyID;
561 CHECK(msg->senderAwaitsResponse(&replyID));
562
563 ALOGV("kWhatSetBufferingSettings");
564 BufferingSettings buffering;
565 readFromAMessage(msg, &buffering);
566 status_t err = OK;
567 if (mSource != NULL) {
568 err = mSource->setBufferingSettings(buffering);
569 } else {
570 err = INVALID_OPERATION;
571 }
572 sp<AMessage> response = new AMessage;
573 response->setInt32("err", err);
574 response->postReply(replyID);
575 break;
576 }
577
Andreas Huber9575c962013-02-05 13:59:56 -0800578 case kWhatPrepare:
579 {
580 mSource->prepareAsync();
Andreas Huberf9334412010-12-15 15:17:42 -0800581 break;
582 }
583
Chong Zhangdcb89b32013-08-06 09:44:47 -0700584 case kWhatGetTrackInfo:
585 {
Lajos Molnar3f274362015-03-05 14:35:41 -0800586 sp<AReplyToken> replyID;
Chong Zhangdcb89b32013-08-06 09:44:47 -0700587 CHECK(msg->senderAwaitsResponse(&replyID));
588
Chong Zhang404fced2014-06-11 14:45:31 -0700589 Parcel* reply;
590 CHECK(msg->findPointer("reply", (void**)&reply));
591
592 size_t inbandTracks = 0;
Chong Zhangdcb89b32013-08-06 09:44:47 -0700593 if (mSource != NULL) {
Chong Zhang404fced2014-06-11 14:45:31 -0700594 inbandTracks = mSource->getTrackCount();
595 }
596
Chong Zhanga7fa1d92014-06-11 14:49:23 -0700597 size_t ccTracks = 0;
598 if (mCCDecoder != NULL) {
599 ccTracks = mCCDecoder->getTrackCount();
600 }
601
Chong Zhang404fced2014-06-11 14:45:31 -0700602 // total track count
Chong Zhanga7fa1d92014-06-11 14:49:23 -0700603 reply->writeInt32(inbandTracks + ccTracks);
Chong Zhang404fced2014-06-11 14:45:31 -0700604
605 // write inband tracks
606 for (size_t i = 0; i < inbandTracks; ++i) {
607 writeTrackInfo(reply, mSource->getTrackInfo(i));
Chong Zhangdcb89b32013-08-06 09:44:47 -0700608 }
609
Chong Zhanga7fa1d92014-06-11 14:49:23 -0700610 // write CC track
611 for (size_t i = 0; i < ccTracks; ++i) {
612 writeTrackInfo(reply, mCCDecoder->getTrackInfo(i));
613 }
614
Chong Zhangdcb89b32013-08-06 09:44:47 -0700615 sp<AMessage> response = new AMessage;
Chong Zhangdcb89b32013-08-06 09:44:47 -0700616 response->postReply(replyID);
617 break;
618 }
619
Robert Shih7c4f0d72014-07-09 18:53:31 -0700620 case kWhatGetSelectedTrack:
621 {
622 status_t err = INVALID_OPERATION;
623 if (mSource != NULL) {
624 err = OK;
625
626 int32_t type32;
627 CHECK(msg->findInt32("type", (int32_t*)&type32));
628 media_track_type type = (media_track_type)type32;
629 ssize_t selectedTrack = mSource->getSelectedTrack(type);
630
631 Parcel* reply;
632 CHECK(msg->findPointer("reply", (void**)&reply));
633 reply->writeInt32(selectedTrack);
634 }
635
636 sp<AMessage> response = new AMessage;
637 response->setInt32("err", err);
638
Lajos Molnar3f274362015-03-05 14:35:41 -0800639 sp<AReplyToken> replyID;
Robert Shih7c4f0d72014-07-09 18:53:31 -0700640 CHECK(msg->senderAwaitsResponse(&replyID));
641 response->postReply(replyID);
642 break;
643 }
644
Chong Zhangdcb89b32013-08-06 09:44:47 -0700645 case kWhatSelectTrack:
646 {
Lajos Molnar3f274362015-03-05 14:35:41 -0800647 sp<AReplyToken> replyID;
Chong Zhangdcb89b32013-08-06 09:44:47 -0700648 CHECK(msg->senderAwaitsResponse(&replyID));
649
Chong Zhang404fced2014-06-11 14:45:31 -0700650 size_t trackIndex;
651 int32_t select;
Robert Shih6ffb1fd2014-10-29 16:24:32 -0700652 int64_t timeUs;
Chong Zhang404fced2014-06-11 14:45:31 -0700653 CHECK(msg->findSize("trackIndex", &trackIndex));
654 CHECK(msg->findInt32("select", &select));
Robert Shih6ffb1fd2014-10-29 16:24:32 -0700655 CHECK(msg->findInt64("timeUs", &timeUs));
Chong Zhang404fced2014-06-11 14:45:31 -0700656
Chong Zhangdcb89b32013-08-06 09:44:47 -0700657 status_t err = INVALID_OPERATION;
Chong Zhang404fced2014-06-11 14:45:31 -0700658
659 size_t inbandTracks = 0;
Chong Zhangdcb89b32013-08-06 09:44:47 -0700660 if (mSource != NULL) {
Chong Zhang404fced2014-06-11 14:45:31 -0700661 inbandTracks = mSource->getTrackCount();
662 }
Chong Zhanga7fa1d92014-06-11 14:49:23 -0700663 size_t ccTracks = 0;
664 if (mCCDecoder != NULL) {
665 ccTracks = mCCDecoder->getTrackCount();
666 }
Chong Zhang404fced2014-06-11 14:45:31 -0700667
668 if (trackIndex < inbandTracks) {
Robert Shih6ffb1fd2014-10-29 16:24:32 -0700669 err = mSource->selectTrack(trackIndex, select, timeUs);
Robert Shihd3b0bbb2014-07-23 15:00:25 -0700670
671 if (!select && err == OK) {
672 int32_t type;
673 sp<AMessage> info = mSource->getTrackInfo(trackIndex);
674 if (info != NULL
675 && info->findInt32("type", &type)
676 && type == MEDIA_TRACK_TYPE_TIMEDTEXT) {
677 ++mTimedTextGeneration;
678 }
679 }
Chong Zhanga7fa1d92014-06-11 14:49:23 -0700680 } else {
681 trackIndex -= inbandTracks;
682
683 if (trackIndex < ccTracks) {
684 err = mCCDecoder->selectTrack(trackIndex, select);
685 }
Chong Zhangdcb89b32013-08-06 09:44:47 -0700686 }
687
688 sp<AMessage> response = new AMessage;
689 response->setInt32("err", err);
690
691 response->postReply(replyID);
692 break;
693 }
694
Andreas Huberb7c8e912012-11-27 15:02:53 -0800695 case kWhatPollDuration:
696 {
697 int32_t generation;
698 CHECK(msg->findInt32("generation", &generation));
699
700 if (generation != mPollDurationGeneration) {
701 // stale
702 break;
703 }
704
705 int64_t durationUs;
706 if (mDriver != NULL && mSource->getDuration(&durationUs) == OK) {
707 sp<NuPlayerDriver> driver = mDriver.promote();
708 if (driver != NULL) {
709 driver->notifyDuration(durationUs);
710 }
711 }
712
713 msg->post(1000000ll); // poll again in a second.
714 break;
715 }
716
Lajos Molnar1de1e252015-04-30 18:18:34 -0700717 case kWhatSetVideoSurface:
Andreas Huberf9334412010-12-15 15:17:42 -0800718 {
Andreas Huberf9334412010-12-15 15:17:42 -0800719
720 sp<RefBase> obj;
Lajos Molnar1de1e252015-04-30 18:18:34 -0700721 CHECK(msg->findObject("surface", &obj));
722 sp<Surface> surface = static_cast<Surface *>(obj.get());
Lajos Molnara81c6222015-07-10 19:17:45 -0700723
724 ALOGD("onSetVideoSurface(%p, %s video decoder)",
725 surface.get(),
Wei Jia6b7d2ed2015-08-10 15:11:54 -0700726 (mSource != NULL && mStarted && mSource->getFormat(false /* audio */) != NULL
Lajos Molnara81c6222015-07-10 19:17:45 -0700727 && mVideoDecoder != NULL) ? "have" : "no");
728
Wei Jia6b7d2ed2015-08-10 15:11:54 -0700729 // Need to check mStarted before calling mSource->getFormat because NuPlayer might
730 // be in preparing state and it could take long time.
731 // When mStarted is true, mSource must have been set.
732 if (mSource == NULL || !mStarted || mSource->getFormat(false /* audio */) == NULL
Lajos Molnara81c6222015-07-10 19:17:45 -0700733 // NOTE: mVideoDecoder's mSurface is always non-null
734 || (mVideoDecoder != NULL && mVideoDecoder->setVideoSurface(surface) == OK)) {
Lajos Molnar1de1e252015-04-30 18:18:34 -0700735 performSetSurface(surface);
Wei Jiafef808d2014-10-31 17:57:05 -0700736 break;
737 }
738
739 mDeferredActions.push_back(
740 new FlushDecoderAction(FLUSH_CMD_FLUSH /* audio */,
741 FLUSH_CMD_SHUTDOWN /* video */));
742
Lajos Molnar1de1e252015-04-30 18:18:34 -0700743 mDeferredActions.push_back(new SetSurfaceAction(surface));
James Dong0d268a32012-08-31 12:18:27 -0700744
Wei Jiac6e58412015-07-10 18:04:55 -0700745 if (obj != NULL || mAudioDecoder != NULL) {
Wei Jiafef808d2014-10-31 17:57:05 -0700746 if (mStarted) {
Andy Hung73535852014-09-05 11:42:58 -0700747 // Issue a seek to refresh the video screen only if started otherwise
748 // the extractor may not yet be started and will assert.
749 // If the video decoder is not set (perhaps audio only in this case)
750 // do not perform a seek as it is not needed.
Ronghua Wua73d9e02014-10-08 15:13:29 -0700751 int64_t currentPositionUs = 0;
752 if (getCurrentPosition(&currentPositionUs) == OK) {
753 mDeferredActions.push_back(
Wei Jiac5de0912016-11-18 10:22:14 -0800754 new SeekAction(currentPositionUs,
755 MediaPlayerSeekMode::SEEK_PREVIOUS_SYNC /* mode */));
Ronghua Wua73d9e02014-10-08 15:13:29 -0700756 }
Andy Hung73535852014-09-05 11:42:58 -0700757 }
Wei Jiaac428aa2014-09-02 19:01:34 -0700758
Andreas Huber57a339c2012-12-03 11:18:00 -0800759 // If there is a new surface texture, instantiate decoders
760 // again if possible.
761 mDeferredActions.push_back(
762 new SimpleAction(&NuPlayer::performScanSources));
763 }
764
Chong Zhangf8d71772014-11-26 15:08:34 -0800765 // After a flush without shutdown, decoder is paused.
766 // Don't resume it until source seek is done, otherwise it could
Chong Zhang7137ec72014-11-12 16:41:05 -0800767 // start pulling stale data too soon.
768 mDeferredActions.push_back(
Chong Zhangf8d71772014-11-26 15:08:34 -0800769 new ResumeDecoderAction(false /* needNotify */));
Chong Zhang7137ec72014-11-12 16:41:05 -0800770
Andreas Huber57a339c2012-12-03 11:18:00 -0800771 processDeferredActions();
Andreas Huberf9334412010-12-15 15:17:42 -0800772 break;
773 }
774
775 case kWhatSetAudioSink:
776 {
Steve Block3856b092011-10-20 11:56:00 +0100777 ALOGV("kWhatSetAudioSink");
Andreas Huberf9334412010-12-15 15:17:42 -0800778
779 sp<RefBase> obj;
780 CHECK(msg->findObject("sink", &obj));
781
782 mAudioSink = static_cast<MediaPlayerBase::AudioSink *>(obj.get());
783 break;
784 }
785
786 case kWhatStart:
787 {
Steve Block3856b092011-10-20 11:56:00 +0100788 ALOGV("kWhatStart");
Wei Jia94211742014-10-28 17:09:06 -0700789 if (mStarted) {
Chong Zhang8a048332015-05-06 15:16:28 -0700790 // do not resume yet if the source is still buffering
791 if (!mPausedForBuffering) {
792 onResume();
793 }
Wei Jia94211742014-10-28 17:09:06 -0700794 } else {
795 onStart();
Lajos Molnar09524832014-07-17 14:29:51 -0700796 }
Chong Zhangefbb6192015-01-30 17:13:27 -0800797 mPausedByClient = false;
Andreas Huberf9334412010-12-15 15:17:42 -0800798 break;
799 }
800
Lajos Molnar3a474aa2015-04-24 17:10:07 -0700801 case kWhatConfigPlayback:
Wei Jia98160162015-02-04 17:01:11 -0800802 {
Lajos Molnar3a474aa2015-04-24 17:10:07 -0700803 sp<AReplyToken> replyID;
804 CHECK(msg->senderAwaitsResponse(&replyID));
805 AudioPlaybackRate rate /* sanitized */;
806 readFromAMessage(msg, &rate);
807 status_t err = OK;
Wei Jia98160162015-02-04 17:01:11 -0800808 if (mRenderer != NULL) {
Wei Jiabf70feb2016-02-19 15:47:16 -0800809 // AudioSink allows only 1.f and 0.f for offload mode.
810 // For other speed, switch to non-offload mode.
811 if (mOffloadAudio && ((rate.mSpeed != 0.f && rate.mSpeed != 1.f)
812 || rate.mPitch != 1.f)) {
813 int64_t currentPositionUs;
814 if (getCurrentPosition(&currentPositionUs) != OK) {
815 currentPositionUs = mPreviousSeekTimeUs;
816 }
817
818 // Set mPlaybackSettings so that the new audio decoder can
819 // be created correctly.
820 mPlaybackSettings = rate;
821 if (!mPaused) {
822 mRenderer->pause();
823 }
Wei Jia5031b2f2016-02-25 11:19:31 -0800824 restartAudio(
Wei Jiabf70feb2016-02-19 15:47:16 -0800825 currentPositionUs, true /* forceNonOffload */,
826 true /* needsToCreateAudioDecoder */);
827 if (!mPaused) {
828 mRenderer->resume();
829 }
830 }
831
Lajos Molnar3a474aa2015-04-24 17:10:07 -0700832 err = mRenderer->setPlaybackSettings(rate);
833 }
834 if (err == OK) {
835 if (rate.mSpeed == 0.f) {
836 onPause();
Wei Jia351ce872016-02-10 13:21:59 -0800837 mPausedByClient = true;
Lajos Molnar3a474aa2015-04-24 17:10:07 -0700838 // save all other settings (using non-paused speed)
839 // so we can restore them on start
840 AudioPlaybackRate newRate = rate;
841 newRate.mSpeed = mPlaybackSettings.mSpeed;
842 mPlaybackSettings = newRate;
843 } else { /* rate.mSpeed != 0.f */
Lajos Molnar3a474aa2015-04-24 17:10:07 -0700844 mPlaybackSettings = rate;
Wei Jia8a092d32016-06-03 14:57:24 -0700845 if (mStarted) {
846 // do not resume yet if the source is still buffering
847 if (!mPausedForBuffering) {
848 onResume();
849 }
850 } else if (mPrepared) {
851 onStart();
852 }
853
854 mPausedByClient = false;
Lajos Molnar3a474aa2015-04-24 17:10:07 -0700855 }
Wei Jia98160162015-02-04 17:01:11 -0800856 }
Ronghua Wu8db88132015-04-22 13:51:35 -0700857
858 if (mVideoDecoder != NULL) {
Praveen Chavanbbaa1442016-04-08 13:33:49 -0700859 sp<AMessage> params = new AMessage();
860 params->setFloat("playback-speed", mPlaybackSettings.mSpeed);
861 mVideoDecoder->setParameters(params);
Ronghua Wu8db88132015-04-22 13:51:35 -0700862 }
863
Lajos Molnar3a474aa2015-04-24 17:10:07 -0700864 sp<AMessage> response = new AMessage;
865 response->setInt32("err", err);
866 response->postReply(replyID);
867 break;
868 }
869
870 case kWhatGetPlaybackSettings:
871 {
872 sp<AReplyToken> replyID;
873 CHECK(msg->senderAwaitsResponse(&replyID));
874 AudioPlaybackRate rate = mPlaybackSettings;
875 status_t err = OK;
876 if (mRenderer != NULL) {
877 err = mRenderer->getPlaybackSettings(&rate);
878 }
879 if (err == OK) {
880 // get playback settings used by renderer, as it may be
881 // slightly off due to audiosink not taking small changes.
882 mPlaybackSettings = rate;
883 if (mPaused) {
884 rate.mSpeed = 0.f;
885 }
886 }
887 sp<AMessage> response = new AMessage;
888 if (err == OK) {
889 writeToAMessage(response, rate);
890 }
891 response->setInt32("err", err);
892 response->postReply(replyID);
893 break;
894 }
895
896 case kWhatConfigSync:
897 {
898 sp<AReplyToken> replyID;
899 CHECK(msg->senderAwaitsResponse(&replyID));
900
901 ALOGV("kWhatConfigSync");
902 AVSyncSettings sync;
903 float videoFpsHint;
904 readFromAMessage(msg, &sync, &videoFpsHint);
905 status_t err = OK;
906 if (mRenderer != NULL) {
907 err = mRenderer->setSyncSettings(sync, videoFpsHint);
908 }
909 if (err == OK) {
910 mSyncSettings = sync;
911 mVideoFpsHint = videoFpsHint;
912 }
913 sp<AMessage> response = new AMessage;
914 response->setInt32("err", err);
915 response->postReply(replyID);
916 break;
917 }
918
919 case kWhatGetSyncSettings:
920 {
921 sp<AReplyToken> replyID;
922 CHECK(msg->senderAwaitsResponse(&replyID));
923 AVSyncSettings sync = mSyncSettings;
924 float videoFps = mVideoFpsHint;
925 status_t err = OK;
926 if (mRenderer != NULL) {
927 err = mRenderer->getSyncSettings(&sync, &videoFps);
928 if (err == OK) {
929 mSyncSettings = sync;
930 mVideoFpsHint = videoFps;
931 }
932 }
933 sp<AMessage> response = new AMessage;
934 if (err == OK) {
935 writeToAMessage(response, sync, videoFps);
936 }
937 response->setInt32("err", err);
938 response->postReply(replyID);
Wei Jia98160162015-02-04 17:01:11 -0800939 break;
940 }
941
Andreas Huberf9334412010-12-15 15:17:42 -0800942 case kWhatScanSources:
943 {
Andreas Huber1aef2112011-01-04 14:01:29 -0800944 int32_t generation;
945 CHECK(msg->findInt32("generation", &generation));
946 if (generation != mScanSourcesGeneration) {
947 // Drop obsolete msg.
948 break;
949 }
950
Andreas Huber5bc087c2010-12-23 10:27:40 -0800951 mScanSourcesPending = false;
952
Steve Block3856b092011-10-20 11:56:00 +0100953 ALOGV("scanning sources haveAudio=%d, haveVideo=%d",
Andreas Huber43c3e6c2011-01-05 12:17:08 -0800954 mAudioDecoder != NULL, mVideoDecoder != NULL);
955
Andreas Huberb7c8e912012-11-27 15:02:53 -0800956 bool mHadAnySourcesBefore =
957 (mAudioDecoder != NULL) || (mVideoDecoder != NULL);
Robert Shih7350b052015-10-01 15:50:14 -0700958 bool rescan = false;
Andreas Huberb7c8e912012-11-27 15:02:53 -0800959
Andy Hung282a7e32014-08-14 15:56:34 -0700960 // initialize video before audio because successful initialization of
961 // video may change deep buffer mode of audio.
Lajos Molnar1de1e252015-04-30 18:18:34 -0700962 if (mSurface != NULL) {
Robert Shih7350b052015-10-01 15:50:14 -0700963 if (instantiateDecoder(false, &mVideoDecoder) == -EWOULDBLOCK) {
964 rescan = true;
965 }
Haynes Mathew George5d246ef2012-07-09 10:36:57 -0700966 }
Andreas Huberf9334412010-12-15 15:17:42 -0800967
Ronghua Wua10fd232014-11-06 16:15:20 -0800968 // Don't try to re-open audio sink if there's an existing decoder.
969 if (mAudioSink != NULL && mAudioDecoder == NULL) {
Robert Shih7350b052015-10-01 15:50:14 -0700970 if (instantiateDecoder(true, &mAudioDecoder) == -EWOULDBLOCK) {
971 rescan = true;
972 }
Andreas Huberf9334412010-12-15 15:17:42 -0800973 }
974
Andreas Huberb7c8e912012-11-27 15:02:53 -0800975 if (!mHadAnySourcesBefore
976 && (mAudioDecoder != NULL || mVideoDecoder != NULL)) {
977 // This is the first time we've found anything playable.
978
Andreas Huber9575c962013-02-05 13:59:56 -0800979 if (mSourceFlags & Source::FLAG_DYNAMIC_DURATION) {
Andreas Huberb7c8e912012-11-27 15:02:53 -0800980 schedulePollDuration();
981 }
982 }
983
Andreas Hubereac68ba2011-09-27 12:12:25 -0700984 status_t err;
985 if ((err = mSource->feedMoreTSData()) != OK) {
Andreas Huber1aef2112011-01-04 14:01:29 -0800986 if (mAudioDecoder == NULL && mVideoDecoder == NULL) {
987 // We're not currently decoding anything (no audio or
988 // video tracks found) and we just ran out of input data.
Andreas Hubereac68ba2011-09-27 12:12:25 -0700989
990 if (err == ERROR_END_OF_STREAM) {
991 notifyListener(MEDIA_PLAYBACK_COMPLETE, 0, 0);
992 } else {
993 notifyListener(MEDIA_ERROR, MEDIA_ERROR_UNKNOWN, err);
994 }
Andreas Huber1aef2112011-01-04 14:01:29 -0800995 }
Andreas Huberf9334412010-12-15 15:17:42 -0800996 break;
997 }
998
Robert Shih7350b052015-10-01 15:50:14 -0700999 if (rescan) {
Andreas Huberf9334412010-12-15 15:17:42 -08001000 msg->post(100000ll);
Andreas Huber5bc087c2010-12-23 10:27:40 -08001001 mScanSourcesPending = true;
Andreas Huberf9334412010-12-15 15:17:42 -08001002 }
1003 break;
1004 }
1005
1006 case kWhatVideoNotify:
1007 case kWhatAudioNotify:
1008 {
1009 bool audio = msg->what() == kWhatAudioNotify;
1010
Wei Jia88703c32014-08-06 11:24:07 -07001011 int32_t currentDecoderGeneration =
1012 (audio? mAudioDecoderGeneration : mVideoDecoderGeneration);
1013 int32_t requesterGeneration = currentDecoderGeneration - 1;
1014 CHECK(msg->findInt32("generation", &requesterGeneration));
1015
1016 if (requesterGeneration != currentDecoderGeneration) {
1017 ALOGV("got message from old %s decoder, generation(%d:%d)",
1018 audio ? "audio" : "video", requesterGeneration,
1019 currentDecoderGeneration);
1020 sp<AMessage> reply;
1021 if (!(msg->findMessage("reply", &reply))) {
1022 return;
1023 }
1024
1025 reply->setInt32("err", INFO_DISCONTINUITY);
1026 reply->post();
1027 return;
1028 }
1029
Andreas Huberf9334412010-12-15 15:17:42 -08001030 int32_t what;
Lajos Molnar1cd13982014-01-17 15:12:51 -08001031 CHECK(msg->findInt32("what", &what));
Andreas Huberf9334412010-12-15 15:17:42 -08001032
Chong Zhang7137ec72014-11-12 16:41:05 -08001033 if (what == DecoderBase::kWhatInputDiscontinuity) {
1034 int32_t formatChange;
1035 CHECK(msg->findInt32("formatChange", &formatChange));
Andreas Huberf9334412010-12-15 15:17:42 -08001036
Chong Zhang7137ec72014-11-12 16:41:05 -08001037 ALOGV("%s discontinuity: formatChange %d",
1038 audio ? "audio" : "video", formatChange);
1039
1040 if (formatChange) {
1041 mDeferredActions.push_back(
1042 new FlushDecoderAction(
1043 audio ? FLUSH_CMD_SHUTDOWN : FLUSH_CMD_NONE,
1044 audio ? FLUSH_CMD_NONE : FLUSH_CMD_SHUTDOWN));
Andreas Huberf9334412010-12-15 15:17:42 -08001045 }
Chong Zhang7137ec72014-11-12 16:41:05 -08001046
1047 mDeferredActions.push_back(
1048 new SimpleAction(
1049 &NuPlayer::performScanSources));
1050
1051 processDeferredActions();
1052 } else if (what == DecoderBase::kWhatEOS) {
Andreas Huberdc9bacd2011-09-26 10:53:29 -07001053 int32_t err;
Lajos Molnar1cd13982014-01-17 15:12:51 -08001054 CHECK(msg->findInt32("err", &err));
Andreas Huberdc9bacd2011-09-26 10:53:29 -07001055
1056 if (err == ERROR_END_OF_STREAM) {
Steve Block3856b092011-10-20 11:56:00 +01001057 ALOGV("got %s decoder EOS", audio ? "audio" : "video");
Andreas Huberdc9bacd2011-09-26 10:53:29 -07001058 } else {
Steve Block3856b092011-10-20 11:56:00 +01001059 ALOGV("got %s decoder EOS w/ error %d",
Andreas Huberdc9bacd2011-09-26 10:53:29 -07001060 audio ? "audio" : "video",
1061 err);
1062 }
1063
1064 mRenderer->queueEOS(audio, err);
Chong Zhang7137ec72014-11-12 16:41:05 -08001065 } else if (what == DecoderBase::kWhatFlushCompleted) {
Steve Block3856b092011-10-20 11:56:00 +01001066 ALOGV("decoder %s flush completed", audio ? "audio" : "video");
Andreas Huberf9334412010-12-15 15:17:42 -08001067
Andy Hung8d121d42014-10-03 09:53:53 -07001068 handleFlushComplete(audio, true /* isDecoder */);
Andreas Huber3831a062010-12-21 10:22:33 -08001069 finishFlushIfPossible();
Chong Zhang7137ec72014-11-12 16:41:05 -08001070 } else if (what == DecoderBase::kWhatVideoSizeChanged) {
Lajos Molnar1cd13982014-01-17 15:12:51 -08001071 sp<AMessage> format;
1072 CHECK(msg->findMessage("format", &format));
1073
Wei Jiac6cfd702014-11-11 16:33:20 -08001074 sp<AMessage> inputFormat =
1075 mSource->getFormat(false /* audio */);
Andreas Huber3831a062010-12-21 10:22:33 -08001076
Bartosz Dolewski26936f72014-12-11 12:47:08 +01001077 setVideoScalingMode(mVideoScalingMode);
Wei Jiac6cfd702014-11-11 16:33:20 -08001078 updateVideoSize(inputFormat, format);
Chong Zhang7137ec72014-11-12 16:41:05 -08001079 } else if (what == DecoderBase::kWhatShutdownCompleted) {
Steve Block3856b092011-10-20 11:56:00 +01001080 ALOGV("%s shutdown completed", audio ? "audio" : "video");
Andreas Huber3831a062010-12-21 10:22:33 -08001081 if (audio) {
1082 mAudioDecoder.clear();
Wei Jia57568df2014-09-22 10:16:29 -07001083 ++mAudioDecoderGeneration;
Andreas Huber3831a062010-12-21 10:22:33 -08001084
1085 CHECK_EQ((int)mFlushingAudio, (int)SHUTTING_DOWN_DECODER);
1086 mFlushingAudio = SHUT_DOWN;
1087 } else {
1088 mVideoDecoder.clear();
Wei Jia57568df2014-09-22 10:16:29 -07001089 ++mVideoDecoderGeneration;
Andreas Huber3831a062010-12-21 10:22:33 -08001090
1091 CHECK_EQ((int)mFlushingVideo, (int)SHUTTING_DOWN_DECODER);
1092 mFlushingVideo = SHUT_DOWN;
1093 }
1094
1095 finishFlushIfPossible();
Chong Zhangf8d71772014-11-26 15:08:34 -08001096 } else if (what == DecoderBase::kWhatResumeCompleted) {
1097 finishResume();
Chong Zhang7137ec72014-11-12 16:41:05 -08001098 } else if (what == DecoderBase::kWhatError) {
Chong Zhangf4c0a942014-08-11 15:14:10 -07001099 status_t err;
Andy Hung2abde2c2014-09-30 14:40:32 -07001100 if (!msg->findInt32("err", &err) || err == OK) {
Chong Zhangf4c0a942014-08-11 15:14:10 -07001101 err = UNKNOWN_ERROR;
1102 }
Andy Hungcf31f1e2014-09-23 14:59:01 -07001103
Andy Hung2abde2c2014-09-30 14:40:32 -07001104 // Decoder errors can be due to Source (e.g. from streaming),
1105 // or from decoding corrupted bitstreams, or from other decoder
1106 // MediaCodec operations (e.g. from an ongoing reset or seek).
Andy Hung202bce12014-12-03 11:47:36 -08001107 // They may also be due to openAudioSink failure at
1108 // decoder start or after a format change.
Andy Hung2abde2c2014-09-30 14:40:32 -07001109 //
1110 // We try to gracefully shut down the affected decoder if possible,
1111 // rather than trying to force the shutdown with something
1112 // similar to performReset(). This method can lead to a hang
1113 // if MediaCodec functions block after an error, but they should
1114 // typically return INVALID_OPERATION instead of blocking.
1115
1116 FlushStatus *flushing = audio ? &mFlushingAudio : &mFlushingVideo;
1117 ALOGE("received error(%#x) from %s decoder, flushing(%d), now shutting down",
1118 err, audio ? "audio" : "video", *flushing);
1119
1120 switch (*flushing) {
1121 case NONE:
1122 mDeferredActions.push_back(
Wei Jiafef808d2014-10-31 17:57:05 -07001123 new FlushDecoderAction(
1124 audio ? FLUSH_CMD_SHUTDOWN : FLUSH_CMD_NONE,
1125 audio ? FLUSH_CMD_NONE : FLUSH_CMD_SHUTDOWN));
Andy Hung2abde2c2014-09-30 14:40:32 -07001126 processDeferredActions();
1127 break;
1128 case FLUSHING_DECODER:
1129 *flushing = FLUSHING_DECODER_SHUTDOWN; // initiate shutdown after flush.
1130 break; // Wait for flush to complete.
1131 case FLUSHING_DECODER_SHUTDOWN:
1132 break; // Wait for flush to complete.
1133 case SHUTTING_DOWN_DECODER:
1134 break; // Wait for shutdown to complete.
1135 case FLUSHED:
1136 // Widevine source reads must stop before releasing the video decoder.
1137 if (!audio && mSource != NULL && mSourceFlags & Source::FLAG_SECURE) {
1138 mSource->stop();
Robert Shih0c61a0d2015-07-06 15:09:10 -07001139 mSourceStarted = false;
Andy Hung2abde2c2014-09-30 14:40:32 -07001140 }
1141 getDecoder(audio)->initiateShutdown(); // In the middle of a seek.
1142 *flushing = SHUTTING_DOWN_DECODER; // Shut down.
1143 break;
1144 case SHUT_DOWN:
1145 finishFlushIfPossible(); // Should not occur.
1146 break; // Finish anyways.
Marco Nelissen9e2b7912014-08-18 16:13:03 -07001147 }
Andy Hung2abde2c2014-09-30 14:40:32 -07001148 notifyListener(MEDIA_ERROR, MEDIA_ERROR_UNKNOWN, err);
Lajos Molnar1cd13982014-01-17 15:12:51 -08001149 } else {
1150 ALOGV("Unhandled decoder notification %d '%c%c%c%c'.",
Andreas Hubera1f8ab02012-11-30 10:53:22 -08001151 what,
1152 what >> 24,
1153 (what >> 16) & 0xff,
1154 (what >> 8) & 0xff,
1155 what & 0xff);
Andreas Huberf9334412010-12-15 15:17:42 -08001156 }
1157
1158 break;
1159 }
1160
1161 case kWhatRendererNotify:
1162 {
Wei Jia57568df2014-09-22 10:16:29 -07001163 int32_t requesterGeneration = mRendererGeneration - 1;
1164 CHECK(msg->findInt32("generation", &requesterGeneration));
1165 if (requesterGeneration != mRendererGeneration) {
1166 ALOGV("got message from old renderer, generation(%d:%d)",
1167 requesterGeneration, mRendererGeneration);
1168 return;
1169 }
1170
Andreas Huberf9334412010-12-15 15:17:42 -08001171 int32_t what;
1172 CHECK(msg->findInt32("what", &what));
1173
1174 if (what == Renderer::kWhatEOS) {
1175 int32_t audio;
1176 CHECK(msg->findInt32("audio", &audio));
1177
Andreas Huberc92fd242011-08-16 13:48:44 -07001178 int32_t finalResult;
1179 CHECK(msg->findInt32("finalResult", &finalResult));
1180
Andreas Huberf9334412010-12-15 15:17:42 -08001181 if (audio) {
1182 mAudioEOS = true;
1183 } else {
1184 mVideoEOS = true;
1185 }
1186
Andreas Huberc92fd242011-08-16 13:48:44 -07001187 if (finalResult == ERROR_END_OF_STREAM) {
Steve Block3856b092011-10-20 11:56:00 +01001188 ALOGV("reached %s EOS", audio ? "audio" : "video");
Andreas Huberc92fd242011-08-16 13:48:44 -07001189 } else {
Steve Block29357bc2012-01-06 19:20:56 +00001190 ALOGE("%s track encountered an error (%d)",
Andreas Huberc92fd242011-08-16 13:48:44 -07001191 audio ? "audio" : "video", finalResult);
1192
1193 notifyListener(
1194 MEDIA_ERROR, MEDIA_ERROR_UNKNOWN, finalResult);
1195 }
Andreas Huberf9334412010-12-15 15:17:42 -08001196
1197 if ((mAudioEOS || mAudioDecoder == NULL)
1198 && (mVideoEOS || mVideoDecoder == NULL)) {
1199 notifyListener(MEDIA_PLAYBACK_COMPLETE, 0, 0);
1200 }
Andreas Huber3fe62152011-09-16 15:09:22 -07001201 } else if (what == Renderer::kWhatFlushComplete) {
Andreas Huberf9334412010-12-15 15:17:42 -08001202 int32_t audio;
1203 CHECK(msg->findInt32("audio", &audio));
1204
Wei Jia3261f0d2015-10-08 13:58:33 -07001205 if (audio) {
1206 mAudioEOS = false;
1207 } else {
1208 mVideoEOS = false;
1209 }
1210
Steve Block3856b092011-10-20 11:56:00 +01001211 ALOGV("renderer %s flush completed.", audio ? "audio" : "video");
Wei Jiada4252f2015-07-14 18:15:28 -07001212 if (audio && (mFlushingAudio == NONE || mFlushingAudio == FLUSHED
1213 || mFlushingAudio == SHUT_DOWN)) {
1214 // Flush has been handled by tear down.
1215 break;
1216 }
Andy Hung8d121d42014-10-03 09:53:53 -07001217 handleFlushComplete(audio, false /* isDecoder */);
1218 finishFlushIfPossible();
James Dongf57b4ea2012-07-20 13:38:36 -07001219 } else if (what == Renderer::kWhatVideoRenderingStart) {
1220 notifyListener(MEDIA_INFO, MEDIA_INFO_RENDERING_START, 0);
Lajos Molnarcbaffcf2013-08-14 18:30:38 -07001221 } else if (what == Renderer::kWhatMediaRenderingStart) {
1222 ALOGV("media rendering started");
1223 notifyListener(MEDIA_STARTED, 0, 0);
Ronghua Wufaeb0f22015-05-21 12:20:21 -07001224 } else if (what == Renderer::kWhatAudioTearDown) {
Ronghua Wu08529172014-10-02 16:55:52 -07001225 int32_t reason;
1226 CHECK(msg->findInt32("reason", &reason));
Ronghua Wufaeb0f22015-05-21 12:20:21 -07001227 ALOGV("Tear down audio with reason %d.", reason);
Wei Jia8456ddd2016-04-22 14:46:43 -07001228 if (reason == Renderer::kDueToTimeout && !(mPaused && mOffloadAudio)) {
1229 // TimeoutWhenPaused is only for offload mode.
1230 ALOGW("Receive a stale message for teardown.");
1231 break;
1232 }
Wei Jiada4252f2015-07-14 18:15:28 -07001233 int64_t positionUs;
Robert Shih1a5c8592015-08-04 18:07:44 -07001234 if (!msg->findInt64("positionUs", &positionUs)) {
1235 positionUs = mPreviousSeekTimeUs;
1236 }
Ronghua Wufaeb0f22015-05-21 12:20:21 -07001237
Wei Jia5031b2f2016-02-25 11:19:31 -08001238 restartAudio(
Wei Jiaa05f1e32016-03-25 16:31:22 -07001239 positionUs, reason == Renderer::kForceNonOffload /* forceNonOffload */,
1240 reason != Renderer::kDueToTimeout /* needsToCreateAudioDecoder */);
Andreas Huberf9334412010-12-15 15:17:42 -08001241 }
1242 break;
1243 }
1244
1245 case kWhatMoreDataQueued:
1246 {
1247 break;
1248 }
1249
Andreas Huber1aef2112011-01-04 14:01:29 -08001250 case kWhatReset:
1251 {
Steve Block3856b092011-10-20 11:56:00 +01001252 ALOGV("kWhatReset");
Andreas Huber1aef2112011-01-04 14:01:29 -08001253
Ronghua Wu64c2d172015-10-07 16:52:19 -07001254 mResetting = true;
1255
Andreas Hubera1f8ab02012-11-30 10:53:22 -08001256 mDeferredActions.push_back(
Wei Jiafef808d2014-10-31 17:57:05 -07001257 new FlushDecoderAction(
1258 FLUSH_CMD_SHUTDOWN /* audio */,
1259 FLUSH_CMD_SHUTDOWN /* video */));
Andreas Huberb7c8e912012-11-27 15:02:53 -08001260
Andreas Hubera1f8ab02012-11-30 10:53:22 -08001261 mDeferredActions.push_back(
1262 new SimpleAction(&NuPlayer::performReset));
Andreas Huberb58ce9f2011-11-28 16:27:35 -08001263
Andreas Hubera1f8ab02012-11-30 10:53:22 -08001264 processDeferredActions();
Andreas Huber1aef2112011-01-04 14:01:29 -08001265 break;
1266 }
1267
Andreas Huber43c3e6c2011-01-05 12:17:08 -08001268 case kWhatSeek:
1269 {
1270 int64_t seekTimeUs;
Wei Jiac5de0912016-11-18 10:22:14 -08001271 int32_t mode;
Wei Jiae427abf2014-09-22 15:21:11 -07001272 int32_t needNotify;
Andreas Huber43c3e6c2011-01-05 12:17:08 -08001273 CHECK(msg->findInt64("seekTimeUs", &seekTimeUs));
Wei Jiac5de0912016-11-18 10:22:14 -08001274 CHECK(msg->findInt32("mode", &mode));
Wei Jiae427abf2014-09-22 15:21:11 -07001275 CHECK(msg->findInt32("needNotify", &needNotify));
Andreas Huber43c3e6c2011-01-05 12:17:08 -08001276
Wei Jiac5de0912016-11-18 10:22:14 -08001277 ALOGV("kWhatSeek seekTimeUs=%lld us, mode=%d, needNotify=%d",
1278 (long long)seekTimeUs, mode, needNotify);
Andreas Huber43c3e6c2011-01-05 12:17:08 -08001279
Wei Jia1061c9c2015-05-19 16:02:17 -07001280 if (!mStarted) {
1281 // Seek before the player is started. In order to preview video,
1282 // need to start the player and pause it. This branch is called
1283 // only once if needed. After the player is started, any seek
1284 // operation will go through normal path.
Robert Shih0c61a0d2015-07-06 15:09:10 -07001285 // Audio-only cases are handled separately.
Wei Jiac5de0912016-11-18 10:22:14 -08001286 onStart(seekTimeUs, (MediaPlayerSeekMode)mode);
Robert Shih0c61a0d2015-07-06 15:09:10 -07001287 if (mStarted) {
1288 onPause();
1289 mPausedByClient = true;
1290 }
Wei Jia1061c9c2015-05-19 16:02:17 -07001291 if (needNotify) {
1292 notifyDriverSeekComplete();
1293 }
1294 break;
1295 }
1296
Andreas Hubera1f8ab02012-11-30 10:53:22 -08001297 mDeferredActions.push_back(
Wei Jiafef808d2014-10-31 17:57:05 -07001298 new FlushDecoderAction(FLUSH_CMD_FLUSH /* audio */,
1299 FLUSH_CMD_FLUSH /* video */));
Andreas Huber43c3e6c2011-01-05 12:17:08 -08001300
Wei Jiae427abf2014-09-22 15:21:11 -07001301 mDeferredActions.push_back(
Wei Jiac5de0912016-11-18 10:22:14 -08001302 new SeekAction(seekTimeUs, (MediaPlayerSeekMode)mode));
Andreas Huber43c3e6c2011-01-05 12:17:08 -08001303
Chong Zhangf8d71772014-11-26 15:08:34 -08001304 // After a flush without shutdown, decoder is paused.
1305 // Don't resume it until source seek is done, otherwise it could
Chong Zhang7137ec72014-11-12 16:41:05 -08001306 // start pulling stale data too soon.
1307 mDeferredActions.push_back(
Chong Zhangf8d71772014-11-26 15:08:34 -08001308 new ResumeDecoderAction(needNotify));
Chong Zhang7137ec72014-11-12 16:41:05 -08001309
Andreas Hubera1f8ab02012-11-30 10:53:22 -08001310 processDeferredActions();
Andreas Huber43c3e6c2011-01-05 12:17:08 -08001311 break;
1312 }
1313
Andreas Huberb4082222011-01-20 15:23:04 -08001314 case kWhatPause:
1315 {
Chong Zhangefbb6192015-01-30 17:13:27 -08001316 onPause();
1317 mPausedByClient = true;
Andreas Huberb4082222011-01-20 15:23:04 -08001318 break;
1319 }
1320
Andreas Huberb5f25f02013-02-05 10:14:26 -08001321 case kWhatSourceNotify:
1322 {
Andreas Huber9575c962013-02-05 13:59:56 -08001323 onSourceNotify(msg);
Andreas Huberb5f25f02013-02-05 10:14:26 -08001324 break;
1325 }
1326
Chong Zhanga7fa1d92014-06-11 14:49:23 -07001327 case kWhatClosedCaptionNotify:
1328 {
1329 onClosedCaptionNotify(msg);
1330 break;
1331 }
1332
Andreas Huberf9334412010-12-15 15:17:42 -08001333 default:
1334 TRESPASS();
1335 break;
1336 }
1337}
1338
Wei Jia94211742014-10-28 17:09:06 -07001339void NuPlayer::onResume() {
Ronghua Wu64c2d172015-10-07 16:52:19 -07001340 if (!mPaused || mResetting) {
1341 ALOGD_IF(mResetting, "resetting, onResume discarded");
Chong Zhangefbb6192015-01-30 17:13:27 -08001342 return;
1343 }
1344 mPaused = false;
Wei Jia94211742014-10-28 17:09:06 -07001345 if (mSource != NULL) {
1346 mSource->resume();
1347 } else {
1348 ALOGW("resume called when source is gone or not set");
1349 }
1350 // |mAudioDecoder| may have been released due to the pause timeout, so re-create it if
1351 // needed.
1352 if (audioDecoderStillNeeded() && mAudioDecoder == NULL) {
1353 instantiateDecoder(true /* audio */, &mAudioDecoder);
1354 }
1355 if (mRenderer != NULL) {
1356 mRenderer->resume();
1357 } else {
1358 ALOGW("resume called when renderer is gone or not set");
1359 }
Ray Essickd4d00612017-01-03 09:36:27 -08001360
1361 mLastStartedPlayingTimeNs = systemTime();
Wei Jia94211742014-10-28 17:09:06 -07001362}
1363
Lajos Molnarfcd3e942015-03-31 10:06:48 -07001364status_t NuPlayer::onInstantiateSecureDecoders() {
1365 status_t err;
1366 if (!(mSourceFlags & Source::FLAG_SECURE)) {
1367 return BAD_TYPE;
1368 }
1369
1370 if (mRenderer != NULL) {
1371 ALOGE("renderer should not be set when instantiating secure decoders");
1372 return UNKNOWN_ERROR;
1373 }
1374
1375 // TRICKY: We rely on mRenderer being null, so that decoder does not start requesting
1376 // data on instantiation.
Lajos Molnar1de1e252015-04-30 18:18:34 -07001377 if (mSurface != NULL) {
Lajos Molnarfcd3e942015-03-31 10:06:48 -07001378 err = instantiateDecoder(false, &mVideoDecoder);
1379 if (err != OK) {
1380 return err;
1381 }
1382 }
1383
1384 if (mAudioSink != NULL) {
1385 err = instantiateDecoder(true, &mAudioDecoder);
1386 if (err != OK) {
1387 return err;
1388 }
1389 }
1390 return OK;
1391}
1392
Wei Jiac5de0912016-11-18 10:22:14 -08001393void NuPlayer::onStart(int64_t startPositionUs, MediaPlayerSeekMode mode) {
Robert Shih0c61a0d2015-07-06 15:09:10 -07001394 if (!mSourceStarted) {
1395 mSourceStarted = true;
1396 mSource->start();
1397 }
1398 if (startPositionUs > 0) {
Wei Jiac5de0912016-11-18 10:22:14 -08001399 performSeek(startPositionUs, mode);
Robert Shih0c61a0d2015-07-06 15:09:10 -07001400 if (mSource->getFormat(false /* audio */) == NULL) {
1401 return;
1402 }
1403 }
1404
Wei Jia94211742014-10-28 17:09:06 -07001405 mOffloadAudio = false;
1406 mAudioEOS = false;
1407 mVideoEOS = false;
Wei Jia94211742014-10-28 17:09:06 -07001408 mStarted = true;
Wei Jiafe8fe7d2016-06-08 10:29:25 -07001409 mPaused = false;
Wei Jia94211742014-10-28 17:09:06 -07001410
Wei Jia94211742014-10-28 17:09:06 -07001411 uint32_t flags = 0;
1412
1413 if (mSource->isRealTime()) {
1414 flags |= Renderer::FLAG_REAL_TIME;
1415 }
1416
Wei Jia9737d342016-12-28 14:59:59 -08001417 bool hasAudio = (mSource->getFormat(true /* audio */) != NULL);
1418 bool hasVideo = (mSource->getFormat(false /* audio */) != NULL);
1419 if (!hasAudio && !hasVideo) {
Wei Jia1de83d52016-10-10 10:15:03 -07001420 ALOGE("no metadata for either audio or video source");
1421 mSource->stop();
1422 mSourceStarted = false;
1423 notifyListener(MEDIA_ERROR, MEDIA_ERROR_UNKNOWN, ERROR_MALFORMED);
1424 return;
1425 }
Wei Jia9737d342016-12-28 14:59:59 -08001426 ALOGV_IF(!hasAudio, "no metadata for audio source"); // video only stream
1427
1428 sp<MetaData> audioMeta = mSource->getFormatMeta(true /* audio */);
Wei Jia1de83d52016-10-10 10:15:03 -07001429
Wei Jia94211742014-10-28 17:09:06 -07001430 audio_stream_type_t streamType = AUDIO_STREAM_MUSIC;
1431 if (mAudioSink != NULL) {
1432 streamType = mAudioSink->getAudioStreamType();
1433 }
1434
Wei Jia94211742014-10-28 17:09:06 -07001435 mOffloadAudio =
Wei Jia9737d342016-12-28 14:59:59 -08001436 canOffloadStream(audioMeta, hasVideo, mSource->isStreaming(), streamType)
Wei Jiabf70feb2016-02-19 15:47:16 -08001437 && (mPlaybackSettings.mSpeed == 1.f && mPlaybackSettings.mPitch == 1.f);
Wei Jia94211742014-10-28 17:09:06 -07001438 if (mOffloadAudio) {
1439 flags |= Renderer::FLAG_OFFLOAD_AUDIO;
1440 }
1441
Lajos Molnar1d15ab52015-03-04 16:46:34 -08001442 sp<AMessage> notify = new AMessage(kWhatRendererNotify, this);
Wei Jia94211742014-10-28 17:09:06 -07001443 ++mRendererGeneration;
1444 notify->setInt32("generation", mRendererGeneration);
1445 mRenderer = new Renderer(mAudioSink, notify, flags);
Wei Jia94211742014-10-28 17:09:06 -07001446 mRendererLooper = new ALooper;
1447 mRendererLooper->setName("NuPlayerRenderer");
1448 mRendererLooper->start(false, false, ANDROID_PRIORITY_AUDIO);
1449 mRendererLooper->registerHandler(mRenderer);
Lajos Molnar3a474aa2015-04-24 17:10:07 -07001450
1451 status_t err = mRenderer->setPlaybackSettings(mPlaybackSettings);
1452 if (err != OK) {
1453 mSource->stop();
Robert Shih0c61a0d2015-07-06 15:09:10 -07001454 mSourceStarted = false;
Lajos Molnar3a474aa2015-04-24 17:10:07 -07001455 notifyListener(MEDIA_ERROR, MEDIA_ERROR_UNKNOWN, err);
1456 return;
Wei Jiac8206ff2015-03-04 13:59:37 -08001457 }
Wei Jia94211742014-10-28 17:09:06 -07001458
Ronghua Wuc8a70d32015-04-29 16:26:34 -07001459 float rate = getFrameRate();
1460 if (rate > 0) {
Wei Jia94211742014-10-28 17:09:06 -07001461 mRenderer->setVideoFrameRate(rate);
1462 }
1463
Wei Jiac6cfd702014-11-11 16:33:20 -08001464 if (mVideoDecoder != NULL) {
1465 mVideoDecoder->setRenderer(mRenderer);
1466 }
1467 if (mAudioDecoder != NULL) {
1468 mAudioDecoder->setRenderer(mRenderer);
1469 }
1470
Ray Essickd4d00612017-01-03 09:36:27 -08001471 mLastStartedPlayingTimeNs = systemTime();
1472
Wei Jia94211742014-10-28 17:09:06 -07001473 postScanSources();
1474}
1475
Chong Zhangefbb6192015-01-30 17:13:27 -08001476void NuPlayer::onPause() {
1477 if (mPaused) {
1478 return;
1479 }
1480 mPaused = true;
1481 if (mSource != NULL) {
1482 mSource->pause();
1483 } else {
1484 ALOGW("pause called when source is gone or not set");
1485 }
1486 if (mRenderer != NULL) {
1487 mRenderer->pause();
1488 } else {
1489 ALOGW("pause called when renderer is gone or not set");
1490 }
Ray Essickd4d00612017-01-03 09:36:27 -08001491
1492 sp<NuPlayerDriver> driver = mDriver.promote();
1493 if (driver != NULL) {
1494 int64_t now = systemTime();
1495 int64_t played = now - mLastStartedPlayingTimeNs;
1496 ALOGD("played from %" PRId64 " to %" PRId64 " = %" PRId64 ,
1497 mLastStartedPlayingTimeNs, now, played);
1498
1499 driver->notifyMorePlayingTimeUs((played+500)/1000);
1500 }
Chong Zhangefbb6192015-01-30 17:13:27 -08001501}
1502
Ronghua Wud7988b12014-10-03 15:19:10 -07001503bool NuPlayer::audioDecoderStillNeeded() {
1504 // Audio decoder is no longer needed if it's in shut/shutting down status.
1505 return ((mFlushingAudio != SHUT_DOWN) && (mFlushingAudio != SHUTTING_DOWN_DECODER));
1506}
1507
Andy Hung8d121d42014-10-03 09:53:53 -07001508void NuPlayer::handleFlushComplete(bool audio, bool isDecoder) {
1509 // We wait for both the decoder flush and the renderer flush to complete
1510 // before entering either the FLUSHED or the SHUTTING_DOWN_DECODER state.
1511
1512 mFlushComplete[audio][isDecoder] = true;
1513 if (!mFlushComplete[audio][!isDecoder]) {
1514 return;
1515 }
1516
1517 FlushStatus *state = audio ? &mFlushingAudio : &mFlushingVideo;
1518 switch (*state) {
1519 case FLUSHING_DECODER:
1520 {
1521 *state = FLUSHED;
Andy Hung8d121d42014-10-03 09:53:53 -07001522 break;
1523 }
1524
1525 case FLUSHING_DECODER_SHUTDOWN:
1526 {
1527 *state = SHUTTING_DOWN_DECODER;
1528
1529 ALOGV("initiating %s decoder shutdown", audio ? "audio" : "video");
1530 if (!audio) {
Andy Hung8d121d42014-10-03 09:53:53 -07001531 // Widevine source reads must stop before releasing the video decoder.
1532 if (mSource != NULL && mSourceFlags & Source::FLAG_SECURE) {
1533 mSource->stop();
Robert Shih0c61a0d2015-07-06 15:09:10 -07001534 mSourceStarted = false;
Andy Hung8d121d42014-10-03 09:53:53 -07001535 }
1536 }
1537 getDecoder(audio)->initiateShutdown();
1538 break;
1539 }
1540
1541 default:
1542 // decoder flush completes only occur in a flushing state.
1543 LOG_ALWAYS_FATAL_IF(isDecoder, "decoder flush in invalid state %d", *state);
1544 break;
1545 }
1546}
1547
Andreas Huber3831a062010-12-21 10:22:33 -08001548void NuPlayer::finishFlushIfPossible() {
Wei Jia53904f32014-07-29 10:22:53 -07001549 if (mFlushingAudio != NONE && mFlushingAudio != FLUSHED
1550 && mFlushingAudio != SHUT_DOWN) {
Andreas Huber3831a062010-12-21 10:22:33 -08001551 return;
1552 }
1553
Wei Jia53904f32014-07-29 10:22:53 -07001554 if (mFlushingVideo != NONE && mFlushingVideo != FLUSHED
1555 && mFlushingVideo != SHUT_DOWN) {
Andreas Huber3831a062010-12-21 10:22:33 -08001556 return;
1557 }
1558
Steve Block3856b092011-10-20 11:56:00 +01001559 ALOGV("both audio and video are flushed now.");
Andreas Huber3831a062010-12-21 10:22:33 -08001560
Andreas Huber3831a062010-12-21 10:22:33 -08001561 mFlushingAudio = NONE;
1562 mFlushingVideo = NONE;
Andreas Huber3831a062010-12-21 10:22:33 -08001563
Andy Hung8d121d42014-10-03 09:53:53 -07001564 clearFlushComplete();
1565
Andreas Hubera1f8ab02012-11-30 10:53:22 -08001566 processDeferredActions();
Andreas Huber1aef2112011-01-04 14:01:29 -08001567}
1568
1569void NuPlayer::postScanSources() {
1570 if (mScanSourcesPending) {
1571 return;
1572 }
1573
Lajos Molnar1d15ab52015-03-04 16:46:34 -08001574 sp<AMessage> msg = new AMessage(kWhatScanSources, this);
Andreas Huber1aef2112011-01-04 14:01:29 -08001575 msg->setInt32("generation", mScanSourcesGeneration);
1576 msg->post();
1577
1578 mScanSourcesPending = true;
1579}
1580
Wei Jia41cd4632016-05-13 10:53:30 -07001581void NuPlayer::tryOpenAudioSinkForOffload(
1582 const sp<AMessage> &format, const sp<MetaData> &audioMeta, bool hasVideo) {
Andy Hung202bce12014-12-03 11:47:36 -08001583 // Note: This is called early in NuPlayer to determine whether offloading
1584 // is possible; otherwise the decoders call the renderer openAudioSink directly.
Andy Hung282a7e32014-08-14 15:56:34 -07001585
Andy Hung202bce12014-12-03 11:47:36 -08001586 status_t err = mRenderer->openAudioSink(
1587 format, true /* offloadOnly */, hasVideo, AUDIO_OUTPUT_FLAG_NONE, &mOffloadAudio);
1588 if (err != OK) {
1589 // Any failure we turn off mOffloadAudio.
1590 mOffloadAudio = false;
1591 } else if (mOffloadAudio) {
Chong Zhang3b9eb1f2014-10-15 17:05:08 -07001592 sendMetaDataToHal(mAudioSink, audioMeta);
Andy Hung282a7e32014-08-14 15:56:34 -07001593 }
1594}
1595
1596void NuPlayer::closeAudioSink() {
Chong Zhang3b9eb1f2014-10-15 17:05:08 -07001597 mRenderer->closeAudioSink();
Andy Hung282a7e32014-08-14 15:56:34 -07001598}
1599
Wei Jia5031b2f2016-02-25 11:19:31 -08001600void NuPlayer::restartAudio(
Wei Jiabf70feb2016-02-19 15:47:16 -08001601 int64_t currentPositionUs, bool forceNonOffload, bool needsToCreateAudioDecoder) {
Wei Jiaa05f1e32016-03-25 16:31:22 -07001602 if (mAudioDecoder != NULL) {
1603 mAudioDecoder->pause();
1604 mAudioDecoder.clear();
1605 ++mAudioDecoderGeneration;
1606 }
Wei Jiabf70feb2016-02-19 15:47:16 -08001607 if (mFlushingAudio == FLUSHING_DECODER) {
1608 mFlushComplete[1 /* audio */][1 /* isDecoder */] = true;
1609 mFlushingAudio = FLUSHED;
1610 finishFlushIfPossible();
1611 } else if (mFlushingAudio == FLUSHING_DECODER_SHUTDOWN
1612 || mFlushingAudio == SHUTTING_DOWN_DECODER) {
1613 mFlushComplete[1 /* audio */][1 /* isDecoder */] = true;
1614 mFlushingAudio = SHUT_DOWN;
1615 finishFlushIfPossible();
1616 needsToCreateAudioDecoder = false;
1617 }
1618 if (mRenderer == NULL) {
1619 return;
1620 }
1621 closeAudioSink();
1622 mRenderer->flush(true /* audio */, false /* notifyComplete */);
1623 if (mVideoDecoder != NULL) {
1624 mRenderer->flush(false /* audio */, false /* notifyComplete */);
1625 }
1626
Wei Jiac5de0912016-11-18 10:22:14 -08001627 performSeek(currentPositionUs, MediaPlayerSeekMode::SEEK_PREVIOUS_SYNC /* mode */);
Wei Jiabf70feb2016-02-19 15:47:16 -08001628
1629 if (forceNonOffload) {
1630 mRenderer->signalDisableOffloadAudio();
1631 mOffloadAudio = false;
1632 }
1633 if (needsToCreateAudioDecoder) {
Wei Jiaa05f1e32016-03-25 16:31:22 -07001634 instantiateDecoder(true /* audio */, &mAudioDecoder, !forceNonOffload);
Wei Jiabf70feb2016-02-19 15:47:16 -08001635 }
1636}
1637
Wei Jia41cd4632016-05-13 10:53:30 -07001638void NuPlayer::determineAudioModeChange(const sp<AMessage> &audioFormat) {
Wei Jiae4d18c72015-07-13 17:58:11 -07001639 if (mSource == NULL || mAudioSink == NULL) {
1640 return;
1641 }
1642
1643 if (mRenderer == NULL) {
1644 ALOGW("No renderer can be used to determine audio mode. Use non-offload for safety.");
1645 mOffloadAudio = false;
1646 return;
1647 }
1648
1649 sp<MetaData> audioMeta = mSource->getFormatMeta(true /* audio */);
1650 sp<AMessage> videoFormat = mSource->getFormat(false /* audio */);
1651 audio_stream_type_t streamType = mAudioSink->getAudioStreamType();
1652 const bool hasVideo = (videoFormat != NULL);
1653 const bool canOffload = canOffloadStream(
Wei Jiabf70feb2016-02-19 15:47:16 -08001654 audioMeta, hasVideo, mSource->isStreaming(), streamType)
1655 && (mPlaybackSettings.mSpeed == 1.f && mPlaybackSettings.mPitch == 1.f);
Wei Jiae4d18c72015-07-13 17:58:11 -07001656 if (canOffload) {
1657 if (!mOffloadAudio) {
1658 mRenderer->signalEnableOffloadAudio();
1659 }
1660 // open audio sink early under offload mode.
Wei Jia41cd4632016-05-13 10:53:30 -07001661 tryOpenAudioSinkForOffload(audioFormat, audioMeta, hasVideo);
Wei Jiae4d18c72015-07-13 17:58:11 -07001662 } else {
Robert Shihe1d70192015-07-23 17:54:13 -07001663 if (mOffloadAudio) {
1664 mRenderer->signalDisableOffloadAudio();
1665 mOffloadAudio = false;
1666 }
Wei Jiae4d18c72015-07-13 17:58:11 -07001667 }
1668}
1669
Wei Jiaa05f1e32016-03-25 16:31:22 -07001670status_t NuPlayer::instantiateDecoder(
1671 bool audio, sp<DecoderBase> *decoder, bool checkAudioModeChange) {
Wei Jia566da802015-08-27 13:59:30 -07001672 // The audio decoder could be cleared by tear down. If still in shut down
1673 // process, no need to create a new audio decoder.
1674 if (*decoder != NULL || (audio && mFlushingAudio == SHUT_DOWN)) {
Andreas Huberf9334412010-12-15 15:17:42 -08001675 return OK;
1676 }
1677
Andreas Huber84066782011-08-16 09:34:26 -07001678 sp<AMessage> format = mSource->getFormat(audio);
Andreas Huberf9334412010-12-15 15:17:42 -08001679
Andreas Huber84066782011-08-16 09:34:26 -07001680 if (format == NULL) {
Robert Shih7350b052015-10-01 15:50:14 -07001681 return UNKNOWN_ERROR;
1682 } else {
1683 status_t err;
1684 if (format->findInt32("err", &err) && err) {
1685 return err;
1686 }
Andreas Huberf9334412010-12-15 15:17:42 -08001687 }
1688
Ronghua Wu8db88132015-04-22 13:51:35 -07001689 format->setInt32("priority", 0 /* realtime */);
1690
Andreas Huber3fe62152011-09-16 15:09:22 -07001691 if (!audio) {
Andreas Huber84066782011-08-16 09:34:26 -07001692 AString mime;
1693 CHECK(format->findString("mime", &mime));
Chong Zhanga7fa1d92014-06-11 14:49:23 -07001694
Lajos Molnar1d15ab52015-03-04 16:46:34 -08001695 sp<AMessage> ccNotify = new AMessage(kWhatClosedCaptionNotify, this);
Chong Zhang341ab6e2015-02-04 13:37:18 -08001696 if (mCCDecoder == NULL) {
1697 mCCDecoder = new CCDecoder(ccNotify);
1698 }
Lajos Molnar09524832014-07-17 14:29:51 -07001699
1700 if (mSourceFlags & Source::FLAG_SECURE) {
1701 format->setInt32("secure", true);
1702 }
Chong Zhang17134602015-01-07 16:14:34 -08001703
1704 if (mSourceFlags & Source::FLAG_PROTECTED) {
1705 format->setInt32("protected", true);
1706 }
Ronghua Wu8db88132015-04-22 13:51:35 -07001707
Ronghua Wuc8a70d32015-04-29 16:26:34 -07001708 float rate = getFrameRate();
1709 if (rate > 0) {
Lajos Molnar3a474aa2015-04-24 17:10:07 -07001710 format->setFloat("operating-rate", rate * mPlaybackSettings.mSpeed);
Ronghua Wu8db88132015-04-22 13:51:35 -07001711 }
Andreas Huber3fe62152011-09-16 15:09:22 -07001712 }
1713
Wei Jiabc2fb722014-07-08 16:37:57 -07001714 if (audio) {
Lajos Molnar1d15ab52015-03-04 16:46:34 -08001715 sp<AMessage> notify = new AMessage(kWhatAudioNotify, this);
Wei Jia88703c32014-08-06 11:24:07 -07001716 ++mAudioDecoderGeneration;
1717 notify->setInt32("generation", mAudioDecoderGeneration);
1718
Wei Jiaa05f1e32016-03-25 16:31:22 -07001719 if (checkAudioModeChange) {
Wei Jia41cd4632016-05-13 10:53:30 -07001720 determineAudioModeChange(format);
Wei Jiaa05f1e32016-03-25 16:31:22 -07001721 }
Wei Jiabc2fb722014-07-08 16:37:57 -07001722 if (mOffloadAudio) {
Wei Jia14532f22015-12-29 11:28:15 -08001723 mSource->setOffloadAudio(true /* offload */);
1724
Haynes Mathew George8b635332015-03-30 17:59:47 -07001725 const bool hasVideo = (mSource->getFormat(false /*audio */) != NULL);
1726 format->setInt32("has-video", hasVideo);
Wei Jiac6cfd702014-11-11 16:33:20 -08001727 *decoder = new DecoderPassThrough(notify, mSource, mRenderer);
Wei Jiabc2fb722014-07-08 16:37:57 -07001728 } else {
Wei Jia14532f22015-12-29 11:28:15 -08001729 mSource->setOffloadAudio(false /* offload */);
1730
Wei Jiaf2ae3e12016-10-27 17:10:59 -07001731 *decoder = new Decoder(notify, mSource, mPID, mUID, mRenderer);
Wei Jiabc2fb722014-07-08 16:37:57 -07001732 }
1733 } else {
Lajos Molnar1d15ab52015-03-04 16:46:34 -08001734 sp<AMessage> notify = new AMessage(kWhatVideoNotify, this);
Wei Jia88703c32014-08-06 11:24:07 -07001735 ++mVideoDecoderGeneration;
1736 notify->setInt32("generation", mVideoDecoderGeneration);
1737
Chong Zhang7137ec72014-11-12 16:41:05 -08001738 *decoder = new Decoder(
Wei Jiaf2ae3e12016-10-27 17:10:59 -07001739 notify, mSource, mPID, mUID, mRenderer, mSurface, mCCDecoder);
Lajos Molnard9fd6312014-11-06 11:00:00 -08001740
1741 // enable FRC if high-quality AV sync is requested, even if not
Lajos Molnar1de1e252015-04-30 18:18:34 -07001742 // directly queuing to display, as this will even improve textureview
Lajos Molnard9fd6312014-11-06 11:00:00 -08001743 // playback.
1744 {
Marco Nelissen96626b72016-12-01 13:58:59 -08001745 if (property_get_bool("persist.sys.media.avsync", false)) {
Lajos Molnard9fd6312014-11-06 11:00:00 -08001746 format->setInt32("auto-frc", 1);
1747 }
1748 }
Wei Jiabc2fb722014-07-08 16:37:57 -07001749 }
Lajos Molnar1cd13982014-01-17 15:12:51 -08001750 (*decoder)->init();
Andreas Huber84066782011-08-16 09:34:26 -07001751 (*decoder)->configure(format);
Andreas Huberf9334412010-12-15 15:17:42 -08001752
Praveen Chavanbbaa1442016-04-08 13:33:49 -07001753 if (!audio) {
1754 sp<AMessage> params = new AMessage();
1755 float rate = getFrameRate();
1756 if (rate > 0) {
1757 params->setFloat("frame-rate-total", rate);
1758 }
1759
1760 sp<MetaData> fileMeta = getFileMeta();
1761 if (fileMeta != NULL) {
1762 int32_t videoTemporalLayerCount;
1763 if (fileMeta->findInt32(kKeyTemporalLayerCount, &videoTemporalLayerCount)
1764 && videoTemporalLayerCount > 0) {
1765 params->setInt32("temporal-layer-count", videoTemporalLayerCount);
1766 }
1767 }
1768
1769 if (params->countEntries() > 0) {
1770 (*decoder)->setParameters(params);
1771 }
1772 }
Andreas Huberf9334412010-12-15 15:17:42 -08001773 return OK;
1774}
1775
Chong Zhangced1c2f2014-08-08 15:22:35 -07001776void NuPlayer::updateVideoSize(
1777 const sp<AMessage> &inputFormat,
1778 const sp<AMessage> &outputFormat) {
1779 if (inputFormat == NULL) {
1780 ALOGW("Unknown video size, reporting 0x0!");
1781 notifyListener(MEDIA_SET_VIDEO_SIZE, 0, 0);
1782 return;
1783 }
Wei Jia9737d342016-12-28 14:59:59 -08001784 int32_t err = OK;
1785 inputFormat->findInt32("err", &err);
1786 if (err == -EWOULDBLOCK) {
1787 ALOGW("Video meta is not available yet!");
1788 return;
1789 }
1790 if (err != OK) {
1791 ALOGW("Something is wrong with video meta!");
1792 return;
1793 }
Chong Zhangced1c2f2014-08-08 15:22:35 -07001794
1795 int32_t displayWidth, displayHeight;
Chong Zhangced1c2f2014-08-08 15:22:35 -07001796 if (outputFormat != NULL) {
1797 int32_t width, height;
1798 CHECK(outputFormat->findInt32("width", &width));
1799 CHECK(outputFormat->findInt32("height", &height));
1800
1801 int32_t cropLeft, cropTop, cropRight, cropBottom;
1802 CHECK(outputFormat->findRect(
1803 "crop",
1804 &cropLeft, &cropTop, &cropRight, &cropBottom));
1805
1806 displayWidth = cropRight - cropLeft + 1;
1807 displayHeight = cropBottom - cropTop + 1;
1808
1809 ALOGV("Video output format changed to %d x %d "
1810 "(crop: %d x %d @ (%d, %d))",
1811 width, height,
1812 displayWidth,
1813 displayHeight,
1814 cropLeft, cropTop);
1815 } else {
1816 CHECK(inputFormat->findInt32("width", &displayWidth));
1817 CHECK(inputFormat->findInt32("height", &displayHeight));
1818
1819 ALOGV("Video input format %d x %d", displayWidth, displayHeight);
1820 }
1821
1822 // Take into account sample aspect ratio if necessary:
1823 int32_t sarWidth, sarHeight;
1824 if (inputFormat->findInt32("sar-width", &sarWidth)
Wei Jia095bc252017-01-27 10:16:16 -08001825 && inputFormat->findInt32("sar-height", &sarHeight)
1826 && sarWidth > 0 && sarHeight > 0) {
Chong Zhangced1c2f2014-08-08 15:22:35 -07001827 ALOGV("Sample aspect ratio %d : %d", sarWidth, sarHeight);
1828
1829 displayWidth = (displayWidth * sarWidth) / sarHeight;
1830
1831 ALOGV("display dimensions %d x %d", displayWidth, displayHeight);
Wei Jiadf6c6af2016-09-29 11:27:15 -07001832 } else {
1833 int32_t width, height;
1834 if (inputFormat->findInt32("display-width", &width)
1835 && inputFormat->findInt32("display-height", &height)
1836 && width > 0 && height > 0
1837 && displayWidth > 0 && displayHeight > 0) {
1838 if (displayHeight * (int64_t)width / height > (int64_t)displayWidth) {
1839 displayHeight = (int32_t)(displayWidth * (int64_t)height / width);
1840 } else {
1841 displayWidth = (int32_t)(displayHeight * (int64_t)width / height);
1842 }
1843 ALOGV("Video display width and height are overridden to %d x %d",
1844 displayWidth, displayHeight);
1845 }
Chong Zhangced1c2f2014-08-08 15:22:35 -07001846 }
1847
1848 int32_t rotationDegrees;
1849 if (!inputFormat->findInt32("rotation-degrees", &rotationDegrees)) {
1850 rotationDegrees = 0;
1851 }
1852
1853 if (rotationDegrees == 90 || rotationDegrees == 270) {
1854 int32_t tmp = displayWidth;
1855 displayWidth = displayHeight;
1856 displayHeight = tmp;
1857 }
1858
1859 notifyListener(
1860 MEDIA_SET_VIDEO_SIZE,
1861 displayWidth,
1862 displayHeight);
1863}
1864
Chong Zhangdcb89b32013-08-06 09:44:47 -07001865void NuPlayer::notifyListener(int msg, int ext1, int ext2, const Parcel *in) {
Andreas Huber43c3e6c2011-01-05 12:17:08 -08001866 if (mDriver == NULL) {
Andreas Huberf9334412010-12-15 15:17:42 -08001867 return;
1868 }
1869
Andreas Huber43c3e6c2011-01-05 12:17:08 -08001870 sp<NuPlayerDriver> driver = mDriver.promote();
Andreas Huberf9334412010-12-15 15:17:42 -08001871
Andreas Huber43c3e6c2011-01-05 12:17:08 -08001872 if (driver == NULL) {
Andreas Huberf9334412010-12-15 15:17:42 -08001873 return;
1874 }
1875
Chong Zhangdcb89b32013-08-06 09:44:47 -07001876 driver->notifyListener(msg, ext1, ext2, in);
Andreas Huberf9334412010-12-15 15:17:42 -08001877}
1878
Chong Zhang7137ec72014-11-12 16:41:05 -08001879void NuPlayer::flushDecoder(bool audio, bool needShutdown) {
Andreas Huber14f76722013-01-15 09:04:18 -08001880 ALOGV("[%s] flushDecoder needShutdown=%d",
1881 audio ? "audio" : "video", needShutdown);
1882
Chong Zhang7137ec72014-11-12 16:41:05 -08001883 const sp<DecoderBase> &decoder = getDecoder(audio);
Lajos Molnar87603c02014-08-20 19:25:30 -07001884 if (decoder == NULL) {
Steve Blockdf64d152012-01-04 20:05:49 +00001885 ALOGI("flushDecoder %s without decoder present",
Andreas Huber6e3d3112011-11-28 12:36:11 -08001886 audio ? "audio" : "video");
Lajos Molnar87603c02014-08-20 19:25:30 -07001887 return;
Andreas Huber6e3d3112011-11-28 12:36:11 -08001888 }
1889
Andreas Huber1aef2112011-01-04 14:01:29 -08001890 // Make sure we don't continue to scan sources until we finish flushing.
1891 ++mScanSourcesGeneration;
Chong Zhang3b032b32015-04-17 15:49:06 -07001892 if (mScanSourcesPending) {
Toshikazu Saitocbcbb792015-09-15 14:53:01 +09001893 if (!needShutdown) {
1894 mDeferredActions.push_back(
1895 new SimpleAction(&NuPlayer::performScanSources));
1896 }
Chong Zhang3b032b32015-04-17 15:49:06 -07001897 mScanSourcesPending = false;
1898 }
Andreas Huber1aef2112011-01-04 14:01:29 -08001899
Chong Zhang7137ec72014-11-12 16:41:05 -08001900 decoder->signalFlush();
Andreas Huber1aef2112011-01-04 14:01:29 -08001901
1902 FlushStatus newStatus =
1903 needShutdown ? FLUSHING_DECODER_SHUTDOWN : FLUSHING_DECODER;
1904
Lajos Molnarfcd3e942015-03-31 10:06:48 -07001905 mFlushComplete[audio][false /* isDecoder */] = (mRenderer == NULL);
Andy Hung8d121d42014-10-03 09:53:53 -07001906 mFlushComplete[audio][true /* isDecoder */] = false;
Andreas Huber1aef2112011-01-04 14:01:29 -08001907 if (audio) {
Wei Jia53904f32014-07-29 10:22:53 -07001908 ALOGE_IF(mFlushingAudio != NONE,
1909 "audio flushDecoder() is called in state %d", mFlushingAudio);
Andreas Huber1aef2112011-01-04 14:01:29 -08001910 mFlushingAudio = newStatus;
Andreas Huber1aef2112011-01-04 14:01:29 -08001911 } else {
Wei Jia53904f32014-07-29 10:22:53 -07001912 ALOGE_IF(mFlushingVideo != NONE,
1913 "video flushDecoder() is called in state %d", mFlushingVideo);
Andreas Huber1aef2112011-01-04 14:01:29 -08001914 mFlushingVideo = newStatus;
Andreas Huber1aef2112011-01-04 14:01:29 -08001915 }
1916}
1917
Chong Zhangced1c2f2014-08-08 15:22:35 -07001918void NuPlayer::queueDecoderShutdown(
1919 bool audio, bool video, const sp<AMessage> &reply) {
1920 ALOGI("queueDecoderShutdown audio=%d, video=%d", audio, video);
Andreas Huber84066782011-08-16 09:34:26 -07001921
Chong Zhangced1c2f2014-08-08 15:22:35 -07001922 mDeferredActions.push_back(
Wei Jiafef808d2014-10-31 17:57:05 -07001923 new FlushDecoderAction(
1924 audio ? FLUSH_CMD_SHUTDOWN : FLUSH_CMD_NONE,
1925 video ? FLUSH_CMD_SHUTDOWN : FLUSH_CMD_NONE));
Andreas Huber84066782011-08-16 09:34:26 -07001926
Chong Zhangced1c2f2014-08-08 15:22:35 -07001927 mDeferredActions.push_back(
1928 new SimpleAction(&NuPlayer::performScanSources));
Andreas Huber84066782011-08-16 09:34:26 -07001929
Chong Zhangced1c2f2014-08-08 15:22:35 -07001930 mDeferredActions.push_back(new PostMessageAction(reply));
1931
1932 processDeferredActions();
Andreas Huber84066782011-08-16 09:34:26 -07001933}
1934
James Dong0d268a32012-08-31 12:18:27 -07001935status_t NuPlayer::setVideoScalingMode(int32_t mode) {
1936 mVideoScalingMode = mode;
Lajos Molnar1de1e252015-04-30 18:18:34 -07001937 if (mSurface != NULL) {
1938 status_t ret = native_window_set_scaling_mode(mSurface.get(), mVideoScalingMode);
James Dong0d268a32012-08-31 12:18:27 -07001939 if (ret != OK) {
1940 ALOGE("Failed to set scaling mode (%d): %s",
1941 -ret, strerror(-ret));
1942 return ret;
1943 }
1944 }
1945 return OK;
1946}
1947
Chong Zhangdcb89b32013-08-06 09:44:47 -07001948status_t NuPlayer::getTrackInfo(Parcel* reply) const {
Lajos Molnar1d15ab52015-03-04 16:46:34 -08001949 sp<AMessage> msg = new AMessage(kWhatGetTrackInfo, this);
Chong Zhangdcb89b32013-08-06 09:44:47 -07001950 msg->setPointer("reply", reply);
1951
1952 sp<AMessage> response;
1953 status_t err = msg->postAndAwaitResponse(&response);
1954 return err;
1955}
1956
Robert Shih7c4f0d72014-07-09 18:53:31 -07001957status_t NuPlayer::getSelectedTrack(int32_t type, Parcel* reply) const {
Lajos Molnar1d15ab52015-03-04 16:46:34 -08001958 sp<AMessage> msg = new AMessage(kWhatGetSelectedTrack, this);
Robert Shih7c4f0d72014-07-09 18:53:31 -07001959 msg->setPointer("reply", reply);
1960 msg->setInt32("type", type);
1961
1962 sp<AMessage> response;
1963 status_t err = msg->postAndAwaitResponse(&response);
1964 if (err == OK && response != NULL) {
1965 CHECK(response->findInt32("err", &err));
1966 }
1967 return err;
1968}
1969
Robert Shih6ffb1fd2014-10-29 16:24:32 -07001970status_t NuPlayer::selectTrack(size_t trackIndex, bool select, int64_t timeUs) {
Lajos Molnar1d15ab52015-03-04 16:46:34 -08001971 sp<AMessage> msg = new AMessage(kWhatSelectTrack, this);
Chong Zhangdcb89b32013-08-06 09:44:47 -07001972 msg->setSize("trackIndex", trackIndex);
1973 msg->setInt32("select", select);
Robert Shih6ffb1fd2014-10-29 16:24:32 -07001974 msg->setInt64("timeUs", timeUs);
Chong Zhangdcb89b32013-08-06 09:44:47 -07001975
1976 sp<AMessage> response;
1977 status_t err = msg->postAndAwaitResponse(&response);
1978
Chong Zhang404fced2014-06-11 14:45:31 -07001979 if (err != OK) {
1980 return err;
1981 }
1982
1983 if (!response->findInt32("err", &err)) {
1984 err = OK;
1985 }
1986
Chong Zhangdcb89b32013-08-06 09:44:47 -07001987 return err;
1988}
1989
Ronghua Wua73d9e02014-10-08 15:13:29 -07001990status_t NuPlayer::getCurrentPosition(int64_t *mediaUs) {
1991 sp<Renderer> renderer = mRenderer;
1992 if (renderer == NULL) {
1993 return NO_INIT;
1994 }
1995
1996 return renderer->getCurrentPosition(mediaUs);
1997}
1998
Praveen Chavane1e5d7a2015-05-19 19:09:48 -07001999void NuPlayer::getStats(Vector<sp<AMessage> > *mTrackStats) {
2000 CHECK(mTrackStats != NULL);
2001
2002 mTrackStats->clear();
2003 if (mVideoDecoder != NULL) {
2004 mTrackStats->push_back(mVideoDecoder->getStats());
2005 }
2006 if (mAudioDecoder != NULL) {
2007 mTrackStats->push_back(mAudioDecoder->getStats());
Chong Zhang7137ec72014-11-12 16:41:05 -08002008 }
Ronghua Wua73d9e02014-10-08 15:13:29 -07002009}
2010
Marco Nelissenf0b72b52014-09-16 15:43:44 -07002011sp<MetaData> NuPlayer::getFileMeta() {
2012 return mSource->getFileFormatMeta();
2013}
2014
Ronghua Wuc8a70d32015-04-29 16:26:34 -07002015float NuPlayer::getFrameRate() {
2016 sp<MetaData> meta = mSource->getFormatMeta(false /* audio */);
2017 if (meta == NULL) {
2018 return 0;
2019 }
2020 int32_t rate;
2021 if (!meta->findInt32(kKeyFrameRate, &rate)) {
2022 // fall back to try file meta
2023 sp<MetaData> fileMeta = getFileMeta();
2024 if (fileMeta == NULL) {
2025 ALOGW("source has video meta but not file meta");
2026 return -1;
2027 }
2028 int32_t fileMetaRate;
2029 if (!fileMeta->findInt32(kKeyFrameRate, &fileMetaRate)) {
2030 return -1;
2031 }
2032 return fileMetaRate;
2033 }
2034 return rate;
2035}
2036
Andreas Huberb7c8e912012-11-27 15:02:53 -08002037void NuPlayer::schedulePollDuration() {
Lajos Molnar1d15ab52015-03-04 16:46:34 -08002038 sp<AMessage> msg = new AMessage(kWhatPollDuration, this);
Andreas Huberb7c8e912012-11-27 15:02:53 -08002039 msg->setInt32("generation", mPollDurationGeneration);
2040 msg->post();
2041}
2042
2043void NuPlayer::cancelPollDuration() {
2044 ++mPollDurationGeneration;
2045}
2046
Andreas Hubera1f8ab02012-11-30 10:53:22 -08002047void NuPlayer::processDeferredActions() {
2048 while (!mDeferredActions.empty()) {
2049 // We won't execute any deferred actions until we're no longer in
2050 // an intermediate state, i.e. one more more decoders are currently
2051 // flushing or shutting down.
2052
Andreas Hubera1f8ab02012-11-30 10:53:22 -08002053 if (mFlushingAudio != NONE || mFlushingVideo != NONE) {
2054 // We're currently flushing, postpone the reset until that's
2055 // completed.
2056
2057 ALOGV("postponing action mFlushingAudio=%d, mFlushingVideo=%d",
2058 mFlushingAudio, mFlushingVideo);
2059
2060 break;
2061 }
2062
2063 sp<Action> action = *mDeferredActions.begin();
2064 mDeferredActions.erase(mDeferredActions.begin());
2065
2066 action->execute(this);
2067 }
2068}
2069
Wei Jiac5de0912016-11-18 10:22:14 -08002070void NuPlayer::performSeek(int64_t seekTimeUs, MediaPlayerSeekMode mode) {
2071 ALOGV("performSeek seekTimeUs=%lld us (%.2f secs), mode=%d",
2072 (long long)seekTimeUs, seekTimeUs / 1E6, mode);
Andreas Hubera1f8ab02012-11-30 10:53:22 -08002073
Andy Hungadf34bf2014-09-03 18:22:22 -07002074 if (mSource == NULL) {
2075 // This happens when reset occurs right before the loop mode
2076 // asynchronously seeks to the start of the stream.
2077 LOG_ALWAYS_FATAL_IF(mAudioDecoder != NULL || mVideoDecoder != NULL,
2078 "mSource is NULL and decoders not NULL audio(%p) video(%p)",
2079 mAudioDecoder.get(), mVideoDecoder.get());
2080 return;
2081 }
Robert Shih1a5c8592015-08-04 18:07:44 -07002082 mPreviousSeekTimeUs = seekTimeUs;
Wei Jiac5de0912016-11-18 10:22:14 -08002083 mSource->seekTo(seekTimeUs, mode);
Robert Shihd3b0bbb2014-07-23 15:00:25 -07002084 ++mTimedTextGeneration;
Andreas Hubera1f8ab02012-11-30 10:53:22 -08002085
Andreas Hubera1f8ab02012-11-30 10:53:22 -08002086 // everything's flushed, continue playback.
2087}
2088
Wei Jiafef808d2014-10-31 17:57:05 -07002089void NuPlayer::performDecoderFlush(FlushCommand audio, FlushCommand video) {
2090 ALOGV("performDecoderFlush audio=%d, video=%d", audio, video);
Andreas Hubera1f8ab02012-11-30 10:53:22 -08002091
Wei Jiafef808d2014-10-31 17:57:05 -07002092 if ((audio == FLUSH_CMD_NONE || mAudioDecoder == NULL)
2093 && (video == FLUSH_CMD_NONE || mVideoDecoder == NULL)) {
Andreas Hubera1f8ab02012-11-30 10:53:22 -08002094 return;
2095 }
2096
Wei Jiafef808d2014-10-31 17:57:05 -07002097 if (audio != FLUSH_CMD_NONE && mAudioDecoder != NULL) {
2098 flushDecoder(true /* audio */, (audio == FLUSH_CMD_SHUTDOWN));
Andreas Hubera1f8ab02012-11-30 10:53:22 -08002099 }
2100
Wei Jiafef808d2014-10-31 17:57:05 -07002101 if (video != FLUSH_CMD_NONE && mVideoDecoder != NULL) {
2102 flushDecoder(false /* audio */, (video == FLUSH_CMD_SHUTDOWN));
Andreas Hubera1f8ab02012-11-30 10:53:22 -08002103 }
2104}
2105
2106void NuPlayer::performReset() {
2107 ALOGV("performReset");
2108
2109 CHECK(mAudioDecoder == NULL);
2110 CHECK(mVideoDecoder == NULL);
2111
2112 cancelPollDuration();
2113
2114 ++mScanSourcesGeneration;
2115 mScanSourcesPending = false;
2116
Lajos Molnar09524832014-07-17 14:29:51 -07002117 if (mRendererLooper != NULL) {
2118 if (mRenderer != NULL) {
2119 mRendererLooper->unregisterHandler(mRenderer->id());
2120 }
2121 mRendererLooper->stop();
2122 mRendererLooper.clear();
2123 }
Andreas Hubera1f8ab02012-11-30 10:53:22 -08002124 mRenderer.clear();
Wei Jia57568df2014-09-22 10:16:29 -07002125 ++mRendererGeneration;
Andreas Hubera1f8ab02012-11-30 10:53:22 -08002126
2127 if (mSource != NULL) {
2128 mSource->stop();
Andreas Huberb5f25f02013-02-05 10:14:26 -08002129
Wei Jiac45a4b22016-04-15 15:30:23 -07002130 Mutex::Autolock autoLock(mSourceLock);
Andreas Hubera1f8ab02012-11-30 10:53:22 -08002131 mSource.clear();
2132 }
2133
2134 if (mDriver != NULL) {
2135 sp<NuPlayerDriver> driver = mDriver.promote();
2136 if (driver != NULL) {
2137 driver->notifyResetComplete();
2138 }
2139 }
Andreas Huber57a339c2012-12-03 11:18:00 -08002140
2141 mStarted = false;
Wei Jia8a092d32016-06-03 14:57:24 -07002142 mPrepared = false;
Ronghua Wu64c2d172015-10-07 16:52:19 -07002143 mResetting = false;
Robert Shih0c61a0d2015-07-06 15:09:10 -07002144 mSourceStarted = false;
Andreas Hubera1f8ab02012-11-30 10:53:22 -08002145}
2146
2147void NuPlayer::performScanSources() {
2148 ALOGV("performScanSources");
2149
Andreas Huber57a339c2012-12-03 11:18:00 -08002150 if (!mStarted) {
2151 return;
2152 }
2153
Andreas Hubera1f8ab02012-11-30 10:53:22 -08002154 if (mAudioDecoder == NULL || mVideoDecoder == NULL) {
2155 postScanSources();
2156 }
2157}
2158
Lajos Molnar1de1e252015-04-30 18:18:34 -07002159void NuPlayer::performSetSurface(const sp<Surface> &surface) {
Andreas Huber57a339c2012-12-03 11:18:00 -08002160 ALOGV("performSetSurface");
2161
Lajos Molnar1de1e252015-04-30 18:18:34 -07002162 mSurface = surface;
Andreas Huber57a339c2012-12-03 11:18:00 -08002163
2164 // XXX - ignore error from setVideoScalingMode for now
2165 setVideoScalingMode(mVideoScalingMode);
Chong Zhang13d6faa2014-08-22 15:35:28 -07002166
2167 if (mDriver != NULL) {
2168 sp<NuPlayerDriver> driver = mDriver.promote();
2169 if (driver != NULL) {
2170 driver->notifySetSurfaceComplete();
2171 }
2172 }
Andreas Huber57a339c2012-12-03 11:18:00 -08002173}
2174
Chong Zhangf8d71772014-11-26 15:08:34 -08002175void NuPlayer::performResumeDecoders(bool needNotify) {
2176 if (needNotify) {
2177 mResumePending = true;
2178 if (mVideoDecoder == NULL) {
2179 // if audio-only, we can notify seek complete now,
2180 // as the resume operation will be relatively fast.
2181 finishResume();
2182 }
2183 }
2184
Chong Zhang7137ec72014-11-12 16:41:05 -08002185 if (mVideoDecoder != NULL) {
Chong Zhangf8d71772014-11-26 15:08:34 -08002186 // When there is continuous seek, MediaPlayer will cache the seek
2187 // position, and send down new seek request when previous seek is
2188 // complete. Let's wait for at least one video output frame before
2189 // notifying seek complete, so that the video thumbnail gets updated
2190 // when seekbar is dragged.
2191 mVideoDecoder->signalResume(needNotify);
Chong Zhang7137ec72014-11-12 16:41:05 -08002192 }
2193
2194 if (mAudioDecoder != NULL) {
Chong Zhangf8d71772014-11-26 15:08:34 -08002195 mAudioDecoder->signalResume(false /* needNotify */);
2196 }
2197}
2198
2199void NuPlayer::finishResume() {
2200 if (mResumePending) {
2201 mResumePending = false;
Wei Jia1061c9c2015-05-19 16:02:17 -07002202 notifyDriverSeekComplete();
2203 }
2204}
2205
2206void NuPlayer::notifyDriverSeekComplete() {
2207 if (mDriver != NULL) {
2208 sp<NuPlayerDriver> driver = mDriver.promote();
2209 if (driver != NULL) {
2210 driver->notifySeekComplete();
Chong Zhangf8d71772014-11-26 15:08:34 -08002211 }
Chong Zhang7137ec72014-11-12 16:41:05 -08002212 }
2213}
2214
Andreas Huber9575c962013-02-05 13:59:56 -08002215void NuPlayer::onSourceNotify(const sp<AMessage> &msg) {
2216 int32_t what;
2217 CHECK(msg->findInt32("what", &what));
2218
2219 switch (what) {
Lajos Molnarfcd3e942015-03-31 10:06:48 -07002220 case Source::kWhatInstantiateSecureDecoders:
2221 {
2222 if (mSource == NULL) {
2223 // This is a stale notification from a source that was
2224 // asynchronously preparing when the client called reset().
2225 // We handled the reset, the source is gone.
2226 break;
2227 }
2228
2229 sp<AMessage> reply;
2230 CHECK(msg->findMessage("reply", &reply));
2231 status_t err = onInstantiateSecureDecoders();
2232 reply->setInt32("err", err);
2233 reply->post();
2234 break;
2235 }
2236
Andreas Huber9575c962013-02-05 13:59:56 -08002237 case Source::kWhatPrepared:
2238 {
Andreas Huberb5f28d42013-04-25 15:11:19 -07002239 if (mSource == NULL) {
2240 // This is a stale notification from a source that was
2241 // asynchronously preparing when the client called reset().
2242 // We handled the reset, the source is gone.
2243 break;
2244 }
2245
Andreas Huberec0c5972013-02-05 14:47:13 -08002246 int32_t err;
2247 CHECK(msg->findInt32("err", &err));
2248
Lajos Molnarfcd3e942015-03-31 10:06:48 -07002249 if (err != OK) {
2250 // shut down potential secure codecs in case client never calls reset
2251 mDeferredActions.push_back(
2252 new FlushDecoderAction(FLUSH_CMD_SHUTDOWN /* audio */,
2253 FLUSH_CMD_SHUTDOWN /* video */));
2254 processDeferredActions();
Wei Jia8a092d32016-06-03 14:57:24 -07002255 } else {
2256 mPrepared = true;
Lajos Molnarfcd3e942015-03-31 10:06:48 -07002257 }
2258
Andreas Huber9575c962013-02-05 13:59:56 -08002259 sp<NuPlayerDriver> driver = mDriver.promote();
2260 if (driver != NULL) {
Marco Nelissendd114d12014-05-28 15:23:14 -07002261 // notify duration first, so that it's definitely set when
2262 // the app received the "prepare complete" callback.
2263 int64_t durationUs;
2264 if (mSource->getDuration(&durationUs) == OK) {
2265 driver->notifyDuration(durationUs);
2266 }
Andreas Huberec0c5972013-02-05 14:47:13 -08002267 driver->notifyPrepareCompleted(err);
Andreas Huber9575c962013-02-05 13:59:56 -08002268 }
Andreas Huber99759402013-04-01 14:28:31 -07002269
Andreas Huber9575c962013-02-05 13:59:56 -08002270 break;
2271 }
2272
2273 case Source::kWhatFlagsChanged:
2274 {
2275 uint32_t flags;
2276 CHECK(msg->findInt32("flags", (int32_t *)&flags));
2277
Chong Zhang4b7069d2013-09-11 12:52:43 -07002278 sp<NuPlayerDriver> driver = mDriver.promote();
2279 if (driver != NULL) {
Wei Jia895651b2014-12-10 17:31:52 -08002280 if ((flags & NuPlayer::Source::FLAG_CAN_SEEK) == 0) {
2281 driver->notifyListener(
2282 MEDIA_INFO, MEDIA_INFO_NOT_SEEKABLE, 0);
2283 }
Chong Zhang4b7069d2013-09-11 12:52:43 -07002284 driver->notifyFlagsChanged(flags);
2285 }
2286
Andreas Huber9575c962013-02-05 13:59:56 -08002287 if ((mSourceFlags & Source::FLAG_DYNAMIC_DURATION)
2288 && (!(flags & Source::FLAG_DYNAMIC_DURATION))) {
2289 cancelPollDuration();
2290 } else if (!(mSourceFlags & Source::FLAG_DYNAMIC_DURATION)
2291 && (flags & Source::FLAG_DYNAMIC_DURATION)
2292 && (mAudioDecoder != NULL || mVideoDecoder != NULL)) {
2293 schedulePollDuration();
2294 }
2295
2296 mSourceFlags = flags;
2297 break;
2298 }
2299
2300 case Source::kWhatVideoSizeChanged:
2301 {
Chong Zhangced1c2f2014-08-08 15:22:35 -07002302 sp<AMessage> format;
2303 CHECK(msg->findMessage("format", &format));
Andreas Huber9575c962013-02-05 13:59:56 -08002304
Chong Zhangced1c2f2014-08-08 15:22:35 -07002305 updateVideoSize(format);
Andreas Huber9575c962013-02-05 13:59:56 -08002306 break;
2307 }
2308
Chong Zhang2a3cc9a2014-08-21 17:48:26 -07002309 case Source::kWhatBufferingUpdate:
2310 {
2311 int32_t percentage;
2312 CHECK(msg->findInt32("percentage", &percentage));
2313
2314 notifyListener(MEDIA_BUFFERING_UPDATE, percentage, 0);
2315 break;
2316 }
2317
Chong Zhangefbb6192015-01-30 17:13:27 -08002318 case Source::kWhatPauseOnBufferingStart:
2319 {
2320 // ignore if not playing
Chong Zhang8a048332015-05-06 15:16:28 -07002321 if (mStarted) {
Chong Zhangefbb6192015-01-30 17:13:27 -08002322 ALOGI("buffer low, pausing...");
2323
Chong Zhang8a048332015-05-06 15:16:28 -07002324 mPausedForBuffering = true;
Chong Zhangefbb6192015-01-30 17:13:27 -08002325 onPause();
2326 }
Wei Jiabfe82072016-05-20 09:21:50 -07002327 notifyListener(MEDIA_INFO, MEDIA_INFO_BUFFERING_START, 0);
Roger Jönssonb50e83e2013-01-21 16:26:41 +01002328 break;
2329 }
2330
Chong Zhangefbb6192015-01-30 17:13:27 -08002331 case Source::kWhatResumeOnBufferingEnd:
2332 {
2333 // ignore if not playing
Chong Zhang8a048332015-05-06 15:16:28 -07002334 if (mStarted) {
Chong Zhangefbb6192015-01-30 17:13:27 -08002335 ALOGI("buffer ready, resuming...");
2336
Chong Zhang8a048332015-05-06 15:16:28 -07002337 mPausedForBuffering = false;
2338
2339 // do not resume yet if client didn't unpause
2340 if (!mPausedByClient) {
2341 onResume();
2342 }
Chong Zhangefbb6192015-01-30 17:13:27 -08002343 }
Wei Jia3bed45a2016-02-17 11:06:47 -08002344 notifyListener(MEDIA_INFO, MEDIA_INFO_BUFFERING_END, 0);
Roger Jönssonb50e83e2013-01-21 16:26:41 +01002345 break;
2346 }
2347
Chong Zhangefbb6192015-01-30 17:13:27 -08002348 case Source::kWhatCacheStats:
2349 {
2350 int32_t kbps;
2351 CHECK(msg->findInt32("bandwidth", &kbps));
2352
2353 notifyListener(MEDIA_INFO, MEDIA_INFO_NETWORK_BANDWIDTH, kbps);
2354 break;
2355 }
2356
Chong Zhangdcb89b32013-08-06 09:44:47 -07002357 case Source::kWhatSubtitleData:
2358 {
2359 sp<ABuffer> buffer;
2360 CHECK(msg->findBuffer("buffer", &buffer));
2361
Chong Zhang404fced2014-06-11 14:45:31 -07002362 sendSubtitleData(buffer, 0 /* baseIndex */);
Chong Zhangdcb89b32013-08-06 09:44:47 -07002363 break;
2364 }
2365
Robert Shih08528432015-04-08 09:06:54 -07002366 case Source::kWhatTimedMetaData:
2367 {
2368 sp<ABuffer> buffer;
2369 if (!msg->findBuffer("buffer", &buffer)) {
2370 notifyListener(MEDIA_INFO, MEDIA_INFO_METADATA_UPDATE, 0);
2371 } else {
2372 sendTimedMetaData(buffer);
2373 }
2374 break;
2375 }
2376
Robert Shihd3b0bbb2014-07-23 15:00:25 -07002377 case Source::kWhatTimedTextData:
2378 {
2379 int32_t generation;
2380 if (msg->findInt32("generation", &generation)
2381 && generation != mTimedTextGeneration) {
2382 break;
2383 }
2384
2385 sp<ABuffer> buffer;
2386 CHECK(msg->findBuffer("buffer", &buffer));
2387
2388 sp<NuPlayerDriver> driver = mDriver.promote();
2389 if (driver == NULL) {
2390 break;
2391 }
2392
2393 int posMs;
2394 int64_t timeUs, posUs;
2395 driver->getCurrentPosition(&posMs);
Patrik2 Carlsson24d484b2015-01-27 16:49:45 +01002396 posUs = (int64_t) posMs * 1000ll;
Robert Shihd3b0bbb2014-07-23 15:00:25 -07002397 CHECK(buffer->meta()->findInt64("timeUs", &timeUs));
2398
2399 if (posUs < timeUs) {
2400 if (!msg->findInt32("generation", &generation)) {
2401 msg->setInt32("generation", mTimedTextGeneration);
2402 }
2403 msg->post(timeUs - posUs);
2404 } else {
2405 sendTimedTextData(buffer);
2406 }
2407 break;
2408 }
2409
Andreas Huber14f76722013-01-15 09:04:18 -08002410 case Source::kWhatQueueDecoderShutdown:
2411 {
2412 int32_t audio, video;
2413 CHECK(msg->findInt32("audio", &audio));
2414 CHECK(msg->findInt32("video", &video));
2415
2416 sp<AMessage> reply;
2417 CHECK(msg->findMessage("reply", &reply));
2418
2419 queueDecoderShutdown(audio, video, reply);
2420 break;
2421 }
2422
Ronghua Wu80276872014-08-28 15:50:29 -07002423 case Source::kWhatDrmNoLicense:
2424 {
2425 notifyListener(MEDIA_ERROR, MEDIA_ERROR_UNKNOWN, ERROR_DRM_NO_LICENSE);
2426 break;
2427 }
2428
Andreas Huber9575c962013-02-05 13:59:56 -08002429 default:
2430 TRESPASS();
2431 }
2432}
2433
Chong Zhanga7fa1d92014-06-11 14:49:23 -07002434void NuPlayer::onClosedCaptionNotify(const sp<AMessage> &msg) {
2435 int32_t what;
2436 CHECK(msg->findInt32("what", &what));
2437
2438 switch (what) {
2439 case NuPlayer::CCDecoder::kWhatClosedCaptionData:
2440 {
2441 sp<ABuffer> buffer;
2442 CHECK(msg->findBuffer("buffer", &buffer));
2443
2444 size_t inbandTracks = 0;
2445 if (mSource != NULL) {
2446 inbandTracks = mSource->getTrackCount();
2447 }
2448
2449 sendSubtitleData(buffer, inbandTracks);
2450 break;
2451 }
2452
2453 case NuPlayer::CCDecoder::kWhatTrackAdded:
2454 {
2455 notifyListener(MEDIA_INFO, MEDIA_INFO_METADATA_UPDATE, 0);
2456
2457 break;
2458 }
2459
2460 default:
2461 TRESPASS();
2462 }
2463
2464
2465}
2466
Chong Zhang404fced2014-06-11 14:45:31 -07002467void NuPlayer::sendSubtitleData(const sp<ABuffer> &buffer, int32_t baseIndex) {
2468 int32_t trackIndex;
2469 int64_t timeUs, durationUs;
2470 CHECK(buffer->meta()->findInt32("trackIndex", &trackIndex));
2471 CHECK(buffer->meta()->findInt64("timeUs", &timeUs));
2472 CHECK(buffer->meta()->findInt64("durationUs", &durationUs));
2473
2474 Parcel in;
2475 in.writeInt32(trackIndex + baseIndex);
2476 in.writeInt64(timeUs);
2477 in.writeInt64(durationUs);
2478 in.writeInt32(buffer->size());
2479 in.writeInt32(buffer->size());
2480 in.write(buffer->data(), buffer->size());
2481
2482 notifyListener(MEDIA_SUBTITLE_DATA, 0, 0, &in);
2483}
Robert Shihd3b0bbb2014-07-23 15:00:25 -07002484
Robert Shih08528432015-04-08 09:06:54 -07002485void NuPlayer::sendTimedMetaData(const sp<ABuffer> &buffer) {
2486 int64_t timeUs;
2487 CHECK(buffer->meta()->findInt64("timeUs", &timeUs));
2488
2489 Parcel in;
2490 in.writeInt64(timeUs);
2491 in.writeInt32(buffer->size());
2492 in.writeInt32(buffer->size());
2493 in.write(buffer->data(), buffer->size());
2494
2495 notifyListener(MEDIA_META_DATA, 0, 0, &in);
2496}
2497
Robert Shihd3b0bbb2014-07-23 15:00:25 -07002498void NuPlayer::sendTimedTextData(const sp<ABuffer> &buffer) {
2499 const void *data;
2500 size_t size = 0;
2501 int64_t timeUs;
Marco Nelissen55e2f4c2015-09-04 15:57:15 -07002502 int32_t flag = TextDescriptions::IN_BAND_TEXT_3GPP;
Robert Shihd3b0bbb2014-07-23 15:00:25 -07002503
2504 AString mime;
2505 CHECK(buffer->meta()->findString("mime", &mime));
2506 CHECK(strcasecmp(mime.c_str(), MEDIA_MIMETYPE_TEXT_3GPP) == 0);
2507
2508 data = buffer->data();
2509 size = buffer->size();
2510
2511 Parcel parcel;
2512 if (size > 0) {
2513 CHECK(buffer->meta()->findInt64("timeUs", &timeUs));
Marco Nelissen55e2f4c2015-09-04 15:57:15 -07002514 int32_t global = 0;
2515 if (buffer->meta()->findInt32("global", &global) && global) {
2516 flag |= TextDescriptions::GLOBAL_DESCRIPTIONS;
2517 } else {
2518 flag |= TextDescriptions::LOCAL_DESCRIPTIONS;
2519 }
Robert Shihd3b0bbb2014-07-23 15:00:25 -07002520 TextDescriptions::getParcelOfDescriptions(
2521 (const uint8_t *)data, size, flag, timeUs / 1000, &parcel);
2522 }
2523
2524 if ((parcel.dataSize() > 0)) {
2525 notifyListener(MEDIA_TIMED_TEXT, 0, 0, &parcel);
2526 } else { // send an empty timed text
2527 notifyListener(MEDIA_TIMED_TEXT, 0, 0);
2528 }
2529}
Andreas Huberb5f25f02013-02-05 10:14:26 -08002530////////////////////////////////////////////////////////////////////////////////
2531
Chong Zhangced1c2f2014-08-08 15:22:35 -07002532sp<AMessage> NuPlayer::Source::getFormat(bool audio) {
2533 sp<MetaData> meta = getFormatMeta(audio);
2534
2535 if (meta == NULL) {
2536 return NULL;
2537 }
2538
2539 sp<AMessage> msg = new AMessage;
2540
2541 if(convertMetaDataToMessage(meta, &msg) == OK) {
2542 return msg;
2543 }
2544 return NULL;
2545}
2546
Andreas Huber9575c962013-02-05 13:59:56 -08002547void NuPlayer::Source::notifyFlagsChanged(uint32_t flags) {
2548 sp<AMessage> notify = dupNotify();
2549 notify->setInt32("what", kWhatFlagsChanged);
2550 notify->setInt32("flags", flags);
2551 notify->post();
2552}
2553
Chong Zhangced1c2f2014-08-08 15:22:35 -07002554void NuPlayer::Source::notifyVideoSizeChanged(const sp<AMessage> &format) {
Andreas Huber9575c962013-02-05 13:59:56 -08002555 sp<AMessage> notify = dupNotify();
2556 notify->setInt32("what", kWhatVideoSizeChanged);
Chong Zhangced1c2f2014-08-08 15:22:35 -07002557 notify->setMessage("format", format);
Andreas Huber9575c962013-02-05 13:59:56 -08002558 notify->post();
2559}
2560
Andreas Huberec0c5972013-02-05 14:47:13 -08002561void NuPlayer::Source::notifyPrepared(status_t err) {
Andreas Huber9575c962013-02-05 13:59:56 -08002562 sp<AMessage> notify = dupNotify();
2563 notify->setInt32("what", kWhatPrepared);
Andreas Huberec0c5972013-02-05 14:47:13 -08002564 notify->setInt32("err", err);
Andreas Huber9575c962013-02-05 13:59:56 -08002565 notify->post();
2566}
2567
Lajos Molnarfcd3e942015-03-31 10:06:48 -07002568void NuPlayer::Source::notifyInstantiateSecureDecoders(const sp<AMessage> &reply) {
2569 sp<AMessage> notify = dupNotify();
2570 notify->setInt32("what", kWhatInstantiateSecureDecoders);
2571 notify->setMessage("reply", reply);
2572 notify->post();
2573}
2574
Andreas Huber84333e02014-02-07 15:36:10 -08002575void NuPlayer::Source::onMessageReceived(const sp<AMessage> & /* msg */) {
Andreas Huberb5f25f02013-02-05 10:14:26 -08002576 TRESPASS();
2577}
2578
Andreas Huberf9334412010-12-15 15:17:42 -08002579} // namespace android