blob: 4c576a5c0f24eb70890fdb360fd1e40f93507d73 [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)
1825 && inputFormat->findInt32("sar-height", &sarHeight)) {
1826 ALOGV("Sample aspect ratio %d : %d", sarWidth, sarHeight);
1827
1828 displayWidth = (displayWidth * sarWidth) / sarHeight;
1829
1830 ALOGV("display dimensions %d x %d", displayWidth, displayHeight);
Wei Jiadf6c6af2016-09-29 11:27:15 -07001831 } else {
1832 int32_t width, height;
1833 if (inputFormat->findInt32("display-width", &width)
1834 && inputFormat->findInt32("display-height", &height)
1835 && width > 0 && height > 0
1836 && displayWidth > 0 && displayHeight > 0) {
1837 if (displayHeight * (int64_t)width / height > (int64_t)displayWidth) {
1838 displayHeight = (int32_t)(displayWidth * (int64_t)height / width);
1839 } else {
1840 displayWidth = (int32_t)(displayHeight * (int64_t)width / height);
1841 }
1842 ALOGV("Video display width and height are overridden to %d x %d",
1843 displayWidth, displayHeight);
1844 }
Chong Zhangced1c2f2014-08-08 15:22:35 -07001845 }
1846
1847 int32_t rotationDegrees;
1848 if (!inputFormat->findInt32("rotation-degrees", &rotationDegrees)) {
1849 rotationDegrees = 0;
1850 }
1851
1852 if (rotationDegrees == 90 || rotationDegrees == 270) {
1853 int32_t tmp = displayWidth;
1854 displayWidth = displayHeight;
1855 displayHeight = tmp;
1856 }
1857
1858 notifyListener(
1859 MEDIA_SET_VIDEO_SIZE,
1860 displayWidth,
1861 displayHeight);
1862}
1863
Chong Zhangdcb89b32013-08-06 09:44:47 -07001864void NuPlayer::notifyListener(int msg, int ext1, int ext2, const Parcel *in) {
Andreas Huber43c3e6c2011-01-05 12:17:08 -08001865 if (mDriver == NULL) {
Andreas Huberf9334412010-12-15 15:17:42 -08001866 return;
1867 }
1868
Andreas Huber43c3e6c2011-01-05 12:17:08 -08001869 sp<NuPlayerDriver> driver = mDriver.promote();
Andreas Huberf9334412010-12-15 15:17:42 -08001870
Andreas Huber43c3e6c2011-01-05 12:17:08 -08001871 if (driver == NULL) {
Andreas Huberf9334412010-12-15 15:17:42 -08001872 return;
1873 }
1874
Chong Zhangdcb89b32013-08-06 09:44:47 -07001875 driver->notifyListener(msg, ext1, ext2, in);
Andreas Huberf9334412010-12-15 15:17:42 -08001876}
1877
Chong Zhang7137ec72014-11-12 16:41:05 -08001878void NuPlayer::flushDecoder(bool audio, bool needShutdown) {
Andreas Huber14f76722013-01-15 09:04:18 -08001879 ALOGV("[%s] flushDecoder needShutdown=%d",
1880 audio ? "audio" : "video", needShutdown);
1881
Chong Zhang7137ec72014-11-12 16:41:05 -08001882 const sp<DecoderBase> &decoder = getDecoder(audio);
Lajos Molnar87603c02014-08-20 19:25:30 -07001883 if (decoder == NULL) {
Steve Blockdf64d152012-01-04 20:05:49 +00001884 ALOGI("flushDecoder %s without decoder present",
Andreas Huber6e3d3112011-11-28 12:36:11 -08001885 audio ? "audio" : "video");
Lajos Molnar87603c02014-08-20 19:25:30 -07001886 return;
Andreas Huber6e3d3112011-11-28 12:36:11 -08001887 }
1888
Andreas Huber1aef2112011-01-04 14:01:29 -08001889 // Make sure we don't continue to scan sources until we finish flushing.
1890 ++mScanSourcesGeneration;
Chong Zhang3b032b32015-04-17 15:49:06 -07001891 if (mScanSourcesPending) {
Toshikazu Saitocbcbb792015-09-15 14:53:01 +09001892 if (!needShutdown) {
1893 mDeferredActions.push_back(
1894 new SimpleAction(&NuPlayer::performScanSources));
1895 }
Chong Zhang3b032b32015-04-17 15:49:06 -07001896 mScanSourcesPending = false;
1897 }
Andreas Huber1aef2112011-01-04 14:01:29 -08001898
Chong Zhang7137ec72014-11-12 16:41:05 -08001899 decoder->signalFlush();
Andreas Huber1aef2112011-01-04 14:01:29 -08001900
1901 FlushStatus newStatus =
1902 needShutdown ? FLUSHING_DECODER_SHUTDOWN : FLUSHING_DECODER;
1903
Lajos Molnarfcd3e942015-03-31 10:06:48 -07001904 mFlushComplete[audio][false /* isDecoder */] = (mRenderer == NULL);
Andy Hung8d121d42014-10-03 09:53:53 -07001905 mFlushComplete[audio][true /* isDecoder */] = false;
Andreas Huber1aef2112011-01-04 14:01:29 -08001906 if (audio) {
Wei Jia53904f32014-07-29 10:22:53 -07001907 ALOGE_IF(mFlushingAudio != NONE,
1908 "audio flushDecoder() is called in state %d", mFlushingAudio);
Andreas Huber1aef2112011-01-04 14:01:29 -08001909 mFlushingAudio = newStatus;
Andreas Huber1aef2112011-01-04 14:01:29 -08001910 } else {
Wei Jia53904f32014-07-29 10:22:53 -07001911 ALOGE_IF(mFlushingVideo != NONE,
1912 "video flushDecoder() is called in state %d", mFlushingVideo);
Andreas Huber1aef2112011-01-04 14:01:29 -08001913 mFlushingVideo = newStatus;
Andreas Huber1aef2112011-01-04 14:01:29 -08001914 }
1915}
1916
Chong Zhangced1c2f2014-08-08 15:22:35 -07001917void NuPlayer::queueDecoderShutdown(
1918 bool audio, bool video, const sp<AMessage> &reply) {
1919 ALOGI("queueDecoderShutdown audio=%d, video=%d", audio, video);
Andreas Huber84066782011-08-16 09:34:26 -07001920
Chong Zhangced1c2f2014-08-08 15:22:35 -07001921 mDeferredActions.push_back(
Wei Jiafef808d2014-10-31 17:57:05 -07001922 new FlushDecoderAction(
1923 audio ? FLUSH_CMD_SHUTDOWN : FLUSH_CMD_NONE,
1924 video ? FLUSH_CMD_SHUTDOWN : FLUSH_CMD_NONE));
Andreas Huber84066782011-08-16 09:34:26 -07001925
Chong Zhangced1c2f2014-08-08 15:22:35 -07001926 mDeferredActions.push_back(
1927 new SimpleAction(&NuPlayer::performScanSources));
Andreas Huber84066782011-08-16 09:34:26 -07001928
Chong Zhangced1c2f2014-08-08 15:22:35 -07001929 mDeferredActions.push_back(new PostMessageAction(reply));
1930
1931 processDeferredActions();
Andreas Huber84066782011-08-16 09:34:26 -07001932}
1933
James Dong0d268a32012-08-31 12:18:27 -07001934status_t NuPlayer::setVideoScalingMode(int32_t mode) {
1935 mVideoScalingMode = mode;
Lajos Molnar1de1e252015-04-30 18:18:34 -07001936 if (mSurface != NULL) {
1937 status_t ret = native_window_set_scaling_mode(mSurface.get(), mVideoScalingMode);
James Dong0d268a32012-08-31 12:18:27 -07001938 if (ret != OK) {
1939 ALOGE("Failed to set scaling mode (%d): %s",
1940 -ret, strerror(-ret));
1941 return ret;
1942 }
1943 }
1944 return OK;
1945}
1946
Chong Zhangdcb89b32013-08-06 09:44:47 -07001947status_t NuPlayer::getTrackInfo(Parcel* reply) const {
Lajos Molnar1d15ab52015-03-04 16:46:34 -08001948 sp<AMessage> msg = new AMessage(kWhatGetTrackInfo, this);
Chong Zhangdcb89b32013-08-06 09:44:47 -07001949 msg->setPointer("reply", reply);
1950
1951 sp<AMessage> response;
1952 status_t err = msg->postAndAwaitResponse(&response);
1953 return err;
1954}
1955
Robert Shih7c4f0d72014-07-09 18:53:31 -07001956status_t NuPlayer::getSelectedTrack(int32_t type, Parcel* reply) const {
Lajos Molnar1d15ab52015-03-04 16:46:34 -08001957 sp<AMessage> msg = new AMessage(kWhatGetSelectedTrack, this);
Robert Shih7c4f0d72014-07-09 18:53:31 -07001958 msg->setPointer("reply", reply);
1959 msg->setInt32("type", type);
1960
1961 sp<AMessage> response;
1962 status_t err = msg->postAndAwaitResponse(&response);
1963 if (err == OK && response != NULL) {
1964 CHECK(response->findInt32("err", &err));
1965 }
1966 return err;
1967}
1968
Robert Shih6ffb1fd2014-10-29 16:24:32 -07001969status_t NuPlayer::selectTrack(size_t trackIndex, bool select, int64_t timeUs) {
Lajos Molnar1d15ab52015-03-04 16:46:34 -08001970 sp<AMessage> msg = new AMessage(kWhatSelectTrack, this);
Chong Zhangdcb89b32013-08-06 09:44:47 -07001971 msg->setSize("trackIndex", trackIndex);
1972 msg->setInt32("select", select);
Robert Shih6ffb1fd2014-10-29 16:24:32 -07001973 msg->setInt64("timeUs", timeUs);
Chong Zhangdcb89b32013-08-06 09:44:47 -07001974
1975 sp<AMessage> response;
1976 status_t err = msg->postAndAwaitResponse(&response);
1977
Chong Zhang404fced2014-06-11 14:45:31 -07001978 if (err != OK) {
1979 return err;
1980 }
1981
1982 if (!response->findInt32("err", &err)) {
1983 err = OK;
1984 }
1985
Chong Zhangdcb89b32013-08-06 09:44:47 -07001986 return err;
1987}
1988
Ronghua Wua73d9e02014-10-08 15:13:29 -07001989status_t NuPlayer::getCurrentPosition(int64_t *mediaUs) {
1990 sp<Renderer> renderer = mRenderer;
1991 if (renderer == NULL) {
1992 return NO_INIT;
1993 }
1994
1995 return renderer->getCurrentPosition(mediaUs);
1996}
1997
Praveen Chavane1e5d7a2015-05-19 19:09:48 -07001998void NuPlayer::getStats(Vector<sp<AMessage> > *mTrackStats) {
1999 CHECK(mTrackStats != NULL);
2000
2001 mTrackStats->clear();
2002 if (mVideoDecoder != NULL) {
2003 mTrackStats->push_back(mVideoDecoder->getStats());
2004 }
2005 if (mAudioDecoder != NULL) {
2006 mTrackStats->push_back(mAudioDecoder->getStats());
Chong Zhang7137ec72014-11-12 16:41:05 -08002007 }
Ronghua Wua73d9e02014-10-08 15:13:29 -07002008}
2009
Marco Nelissenf0b72b52014-09-16 15:43:44 -07002010sp<MetaData> NuPlayer::getFileMeta() {
2011 return mSource->getFileFormatMeta();
2012}
2013
Ronghua Wuc8a70d32015-04-29 16:26:34 -07002014float NuPlayer::getFrameRate() {
2015 sp<MetaData> meta = mSource->getFormatMeta(false /* audio */);
2016 if (meta == NULL) {
2017 return 0;
2018 }
2019 int32_t rate;
2020 if (!meta->findInt32(kKeyFrameRate, &rate)) {
2021 // fall back to try file meta
2022 sp<MetaData> fileMeta = getFileMeta();
2023 if (fileMeta == NULL) {
2024 ALOGW("source has video meta but not file meta");
2025 return -1;
2026 }
2027 int32_t fileMetaRate;
2028 if (!fileMeta->findInt32(kKeyFrameRate, &fileMetaRate)) {
2029 return -1;
2030 }
2031 return fileMetaRate;
2032 }
2033 return rate;
2034}
2035
Andreas Huberb7c8e912012-11-27 15:02:53 -08002036void NuPlayer::schedulePollDuration() {
Lajos Molnar1d15ab52015-03-04 16:46:34 -08002037 sp<AMessage> msg = new AMessage(kWhatPollDuration, this);
Andreas Huberb7c8e912012-11-27 15:02:53 -08002038 msg->setInt32("generation", mPollDurationGeneration);
2039 msg->post();
2040}
2041
2042void NuPlayer::cancelPollDuration() {
2043 ++mPollDurationGeneration;
2044}
2045
Andreas Hubera1f8ab02012-11-30 10:53:22 -08002046void NuPlayer::processDeferredActions() {
2047 while (!mDeferredActions.empty()) {
2048 // We won't execute any deferred actions until we're no longer in
2049 // an intermediate state, i.e. one more more decoders are currently
2050 // flushing or shutting down.
2051
Andreas Hubera1f8ab02012-11-30 10:53:22 -08002052 if (mFlushingAudio != NONE || mFlushingVideo != NONE) {
2053 // We're currently flushing, postpone the reset until that's
2054 // completed.
2055
2056 ALOGV("postponing action mFlushingAudio=%d, mFlushingVideo=%d",
2057 mFlushingAudio, mFlushingVideo);
2058
2059 break;
2060 }
2061
2062 sp<Action> action = *mDeferredActions.begin();
2063 mDeferredActions.erase(mDeferredActions.begin());
2064
2065 action->execute(this);
2066 }
2067}
2068
Wei Jiac5de0912016-11-18 10:22:14 -08002069void NuPlayer::performSeek(int64_t seekTimeUs, MediaPlayerSeekMode mode) {
2070 ALOGV("performSeek seekTimeUs=%lld us (%.2f secs), mode=%d",
2071 (long long)seekTimeUs, seekTimeUs / 1E6, mode);
Andreas Hubera1f8ab02012-11-30 10:53:22 -08002072
Andy Hungadf34bf2014-09-03 18:22:22 -07002073 if (mSource == NULL) {
2074 // This happens when reset occurs right before the loop mode
2075 // asynchronously seeks to the start of the stream.
2076 LOG_ALWAYS_FATAL_IF(mAudioDecoder != NULL || mVideoDecoder != NULL,
2077 "mSource is NULL and decoders not NULL audio(%p) video(%p)",
2078 mAudioDecoder.get(), mVideoDecoder.get());
2079 return;
2080 }
Robert Shih1a5c8592015-08-04 18:07:44 -07002081 mPreviousSeekTimeUs = seekTimeUs;
Wei Jiac5de0912016-11-18 10:22:14 -08002082 mSource->seekTo(seekTimeUs, mode);
Robert Shihd3b0bbb2014-07-23 15:00:25 -07002083 ++mTimedTextGeneration;
Andreas Hubera1f8ab02012-11-30 10:53:22 -08002084
Andreas Hubera1f8ab02012-11-30 10:53:22 -08002085 // everything's flushed, continue playback.
2086}
2087
Wei Jiafef808d2014-10-31 17:57:05 -07002088void NuPlayer::performDecoderFlush(FlushCommand audio, FlushCommand video) {
2089 ALOGV("performDecoderFlush audio=%d, video=%d", audio, video);
Andreas Hubera1f8ab02012-11-30 10:53:22 -08002090
Wei Jiafef808d2014-10-31 17:57:05 -07002091 if ((audio == FLUSH_CMD_NONE || mAudioDecoder == NULL)
2092 && (video == FLUSH_CMD_NONE || mVideoDecoder == NULL)) {
Andreas Hubera1f8ab02012-11-30 10:53:22 -08002093 return;
2094 }
2095
Wei Jiafef808d2014-10-31 17:57:05 -07002096 if (audio != FLUSH_CMD_NONE && mAudioDecoder != NULL) {
2097 flushDecoder(true /* audio */, (audio == FLUSH_CMD_SHUTDOWN));
Andreas Hubera1f8ab02012-11-30 10:53:22 -08002098 }
2099
Wei Jiafef808d2014-10-31 17:57:05 -07002100 if (video != FLUSH_CMD_NONE && mVideoDecoder != NULL) {
2101 flushDecoder(false /* audio */, (video == FLUSH_CMD_SHUTDOWN));
Andreas Hubera1f8ab02012-11-30 10:53:22 -08002102 }
2103}
2104
2105void NuPlayer::performReset() {
2106 ALOGV("performReset");
2107
2108 CHECK(mAudioDecoder == NULL);
2109 CHECK(mVideoDecoder == NULL);
2110
2111 cancelPollDuration();
2112
2113 ++mScanSourcesGeneration;
2114 mScanSourcesPending = false;
2115
Lajos Molnar09524832014-07-17 14:29:51 -07002116 if (mRendererLooper != NULL) {
2117 if (mRenderer != NULL) {
2118 mRendererLooper->unregisterHandler(mRenderer->id());
2119 }
2120 mRendererLooper->stop();
2121 mRendererLooper.clear();
2122 }
Andreas Hubera1f8ab02012-11-30 10:53:22 -08002123 mRenderer.clear();
Wei Jia57568df2014-09-22 10:16:29 -07002124 ++mRendererGeneration;
Andreas Hubera1f8ab02012-11-30 10:53:22 -08002125
2126 if (mSource != NULL) {
2127 mSource->stop();
Andreas Huberb5f25f02013-02-05 10:14:26 -08002128
Wei Jiac45a4b22016-04-15 15:30:23 -07002129 Mutex::Autolock autoLock(mSourceLock);
Andreas Hubera1f8ab02012-11-30 10:53:22 -08002130 mSource.clear();
2131 }
2132
2133 if (mDriver != NULL) {
2134 sp<NuPlayerDriver> driver = mDriver.promote();
2135 if (driver != NULL) {
2136 driver->notifyResetComplete();
2137 }
2138 }
Andreas Huber57a339c2012-12-03 11:18:00 -08002139
2140 mStarted = false;
Wei Jia8a092d32016-06-03 14:57:24 -07002141 mPrepared = false;
Ronghua Wu64c2d172015-10-07 16:52:19 -07002142 mResetting = false;
Robert Shih0c61a0d2015-07-06 15:09:10 -07002143 mSourceStarted = false;
Andreas Hubera1f8ab02012-11-30 10:53:22 -08002144}
2145
2146void NuPlayer::performScanSources() {
2147 ALOGV("performScanSources");
2148
Andreas Huber57a339c2012-12-03 11:18:00 -08002149 if (!mStarted) {
2150 return;
2151 }
2152
Andreas Hubera1f8ab02012-11-30 10:53:22 -08002153 if (mAudioDecoder == NULL || mVideoDecoder == NULL) {
2154 postScanSources();
2155 }
2156}
2157
Lajos Molnar1de1e252015-04-30 18:18:34 -07002158void NuPlayer::performSetSurface(const sp<Surface> &surface) {
Andreas Huber57a339c2012-12-03 11:18:00 -08002159 ALOGV("performSetSurface");
2160
Lajos Molnar1de1e252015-04-30 18:18:34 -07002161 mSurface = surface;
Andreas Huber57a339c2012-12-03 11:18:00 -08002162
2163 // XXX - ignore error from setVideoScalingMode for now
2164 setVideoScalingMode(mVideoScalingMode);
Chong Zhang13d6faa2014-08-22 15:35:28 -07002165
2166 if (mDriver != NULL) {
2167 sp<NuPlayerDriver> driver = mDriver.promote();
2168 if (driver != NULL) {
2169 driver->notifySetSurfaceComplete();
2170 }
2171 }
Andreas Huber57a339c2012-12-03 11:18:00 -08002172}
2173
Chong Zhangf8d71772014-11-26 15:08:34 -08002174void NuPlayer::performResumeDecoders(bool needNotify) {
2175 if (needNotify) {
2176 mResumePending = true;
2177 if (mVideoDecoder == NULL) {
2178 // if audio-only, we can notify seek complete now,
2179 // as the resume operation will be relatively fast.
2180 finishResume();
2181 }
2182 }
2183
Chong Zhang7137ec72014-11-12 16:41:05 -08002184 if (mVideoDecoder != NULL) {
Chong Zhangf8d71772014-11-26 15:08:34 -08002185 // When there is continuous seek, MediaPlayer will cache the seek
2186 // position, and send down new seek request when previous seek is
2187 // complete. Let's wait for at least one video output frame before
2188 // notifying seek complete, so that the video thumbnail gets updated
2189 // when seekbar is dragged.
2190 mVideoDecoder->signalResume(needNotify);
Chong Zhang7137ec72014-11-12 16:41:05 -08002191 }
2192
2193 if (mAudioDecoder != NULL) {
Chong Zhangf8d71772014-11-26 15:08:34 -08002194 mAudioDecoder->signalResume(false /* needNotify */);
2195 }
2196}
2197
2198void NuPlayer::finishResume() {
2199 if (mResumePending) {
2200 mResumePending = false;
Wei Jia1061c9c2015-05-19 16:02:17 -07002201 notifyDriverSeekComplete();
2202 }
2203}
2204
2205void NuPlayer::notifyDriverSeekComplete() {
2206 if (mDriver != NULL) {
2207 sp<NuPlayerDriver> driver = mDriver.promote();
2208 if (driver != NULL) {
2209 driver->notifySeekComplete();
Chong Zhangf8d71772014-11-26 15:08:34 -08002210 }
Chong Zhang7137ec72014-11-12 16:41:05 -08002211 }
2212}
2213
Andreas Huber9575c962013-02-05 13:59:56 -08002214void NuPlayer::onSourceNotify(const sp<AMessage> &msg) {
2215 int32_t what;
2216 CHECK(msg->findInt32("what", &what));
2217
2218 switch (what) {
Lajos Molnarfcd3e942015-03-31 10:06:48 -07002219 case Source::kWhatInstantiateSecureDecoders:
2220 {
2221 if (mSource == NULL) {
2222 // This is a stale notification from a source that was
2223 // asynchronously preparing when the client called reset().
2224 // We handled the reset, the source is gone.
2225 break;
2226 }
2227
2228 sp<AMessage> reply;
2229 CHECK(msg->findMessage("reply", &reply));
2230 status_t err = onInstantiateSecureDecoders();
2231 reply->setInt32("err", err);
2232 reply->post();
2233 break;
2234 }
2235
Andreas Huber9575c962013-02-05 13:59:56 -08002236 case Source::kWhatPrepared:
2237 {
Andreas Huberb5f28d42013-04-25 15:11:19 -07002238 if (mSource == NULL) {
2239 // This is a stale notification from a source that was
2240 // asynchronously preparing when the client called reset().
2241 // We handled the reset, the source is gone.
2242 break;
2243 }
2244
Andreas Huberec0c5972013-02-05 14:47:13 -08002245 int32_t err;
2246 CHECK(msg->findInt32("err", &err));
2247
Lajos Molnarfcd3e942015-03-31 10:06:48 -07002248 if (err != OK) {
2249 // shut down potential secure codecs in case client never calls reset
2250 mDeferredActions.push_back(
2251 new FlushDecoderAction(FLUSH_CMD_SHUTDOWN /* audio */,
2252 FLUSH_CMD_SHUTDOWN /* video */));
2253 processDeferredActions();
Wei Jia8a092d32016-06-03 14:57:24 -07002254 } else {
2255 mPrepared = true;
Lajos Molnarfcd3e942015-03-31 10:06:48 -07002256 }
2257
Andreas Huber9575c962013-02-05 13:59:56 -08002258 sp<NuPlayerDriver> driver = mDriver.promote();
2259 if (driver != NULL) {
Marco Nelissendd114d12014-05-28 15:23:14 -07002260 // notify duration first, so that it's definitely set when
2261 // the app received the "prepare complete" callback.
2262 int64_t durationUs;
2263 if (mSource->getDuration(&durationUs) == OK) {
2264 driver->notifyDuration(durationUs);
2265 }
Andreas Huberec0c5972013-02-05 14:47:13 -08002266 driver->notifyPrepareCompleted(err);
Andreas Huber9575c962013-02-05 13:59:56 -08002267 }
Andreas Huber99759402013-04-01 14:28:31 -07002268
Andreas Huber9575c962013-02-05 13:59:56 -08002269 break;
2270 }
2271
2272 case Source::kWhatFlagsChanged:
2273 {
2274 uint32_t flags;
2275 CHECK(msg->findInt32("flags", (int32_t *)&flags));
2276
Chong Zhang4b7069d2013-09-11 12:52:43 -07002277 sp<NuPlayerDriver> driver = mDriver.promote();
2278 if (driver != NULL) {
Wei Jia895651b2014-12-10 17:31:52 -08002279 if ((flags & NuPlayer::Source::FLAG_CAN_SEEK) == 0) {
2280 driver->notifyListener(
2281 MEDIA_INFO, MEDIA_INFO_NOT_SEEKABLE, 0);
2282 }
Chong Zhang4b7069d2013-09-11 12:52:43 -07002283 driver->notifyFlagsChanged(flags);
2284 }
2285
Andreas Huber9575c962013-02-05 13:59:56 -08002286 if ((mSourceFlags & Source::FLAG_DYNAMIC_DURATION)
2287 && (!(flags & Source::FLAG_DYNAMIC_DURATION))) {
2288 cancelPollDuration();
2289 } else if (!(mSourceFlags & Source::FLAG_DYNAMIC_DURATION)
2290 && (flags & Source::FLAG_DYNAMIC_DURATION)
2291 && (mAudioDecoder != NULL || mVideoDecoder != NULL)) {
2292 schedulePollDuration();
2293 }
2294
2295 mSourceFlags = flags;
2296 break;
2297 }
2298
2299 case Source::kWhatVideoSizeChanged:
2300 {
Chong Zhangced1c2f2014-08-08 15:22:35 -07002301 sp<AMessage> format;
2302 CHECK(msg->findMessage("format", &format));
Andreas Huber9575c962013-02-05 13:59:56 -08002303
Chong Zhangced1c2f2014-08-08 15:22:35 -07002304 updateVideoSize(format);
Andreas Huber9575c962013-02-05 13:59:56 -08002305 break;
2306 }
2307
Chong Zhang2a3cc9a2014-08-21 17:48:26 -07002308 case Source::kWhatBufferingUpdate:
2309 {
2310 int32_t percentage;
2311 CHECK(msg->findInt32("percentage", &percentage));
2312
2313 notifyListener(MEDIA_BUFFERING_UPDATE, percentage, 0);
2314 break;
2315 }
2316
Chong Zhangefbb6192015-01-30 17:13:27 -08002317 case Source::kWhatPauseOnBufferingStart:
2318 {
2319 // ignore if not playing
Chong Zhang8a048332015-05-06 15:16:28 -07002320 if (mStarted) {
Chong Zhangefbb6192015-01-30 17:13:27 -08002321 ALOGI("buffer low, pausing...");
2322
Chong Zhang8a048332015-05-06 15:16:28 -07002323 mPausedForBuffering = true;
Chong Zhangefbb6192015-01-30 17:13:27 -08002324 onPause();
2325 }
Wei Jiabfe82072016-05-20 09:21:50 -07002326 notifyListener(MEDIA_INFO, MEDIA_INFO_BUFFERING_START, 0);
Roger Jönssonb50e83e2013-01-21 16:26:41 +01002327 break;
2328 }
2329
Chong Zhangefbb6192015-01-30 17:13:27 -08002330 case Source::kWhatResumeOnBufferingEnd:
2331 {
2332 // ignore if not playing
Chong Zhang8a048332015-05-06 15:16:28 -07002333 if (mStarted) {
Chong Zhangefbb6192015-01-30 17:13:27 -08002334 ALOGI("buffer ready, resuming...");
2335
Chong Zhang8a048332015-05-06 15:16:28 -07002336 mPausedForBuffering = false;
2337
2338 // do not resume yet if client didn't unpause
2339 if (!mPausedByClient) {
2340 onResume();
2341 }
Chong Zhangefbb6192015-01-30 17:13:27 -08002342 }
Wei Jia3bed45a2016-02-17 11:06:47 -08002343 notifyListener(MEDIA_INFO, MEDIA_INFO_BUFFERING_END, 0);
Roger Jönssonb50e83e2013-01-21 16:26:41 +01002344 break;
2345 }
2346
Chong Zhangefbb6192015-01-30 17:13:27 -08002347 case Source::kWhatCacheStats:
2348 {
2349 int32_t kbps;
2350 CHECK(msg->findInt32("bandwidth", &kbps));
2351
2352 notifyListener(MEDIA_INFO, MEDIA_INFO_NETWORK_BANDWIDTH, kbps);
2353 break;
2354 }
2355
Chong Zhangdcb89b32013-08-06 09:44:47 -07002356 case Source::kWhatSubtitleData:
2357 {
2358 sp<ABuffer> buffer;
2359 CHECK(msg->findBuffer("buffer", &buffer));
2360
Chong Zhang404fced2014-06-11 14:45:31 -07002361 sendSubtitleData(buffer, 0 /* baseIndex */);
Chong Zhangdcb89b32013-08-06 09:44:47 -07002362 break;
2363 }
2364
Robert Shih08528432015-04-08 09:06:54 -07002365 case Source::kWhatTimedMetaData:
2366 {
2367 sp<ABuffer> buffer;
2368 if (!msg->findBuffer("buffer", &buffer)) {
2369 notifyListener(MEDIA_INFO, MEDIA_INFO_METADATA_UPDATE, 0);
2370 } else {
2371 sendTimedMetaData(buffer);
2372 }
2373 break;
2374 }
2375
Robert Shihd3b0bbb2014-07-23 15:00:25 -07002376 case Source::kWhatTimedTextData:
2377 {
2378 int32_t generation;
2379 if (msg->findInt32("generation", &generation)
2380 && generation != mTimedTextGeneration) {
2381 break;
2382 }
2383
2384 sp<ABuffer> buffer;
2385 CHECK(msg->findBuffer("buffer", &buffer));
2386
2387 sp<NuPlayerDriver> driver = mDriver.promote();
2388 if (driver == NULL) {
2389 break;
2390 }
2391
2392 int posMs;
2393 int64_t timeUs, posUs;
2394 driver->getCurrentPosition(&posMs);
Patrik2 Carlsson24d484b2015-01-27 16:49:45 +01002395 posUs = (int64_t) posMs * 1000ll;
Robert Shihd3b0bbb2014-07-23 15:00:25 -07002396 CHECK(buffer->meta()->findInt64("timeUs", &timeUs));
2397
2398 if (posUs < timeUs) {
2399 if (!msg->findInt32("generation", &generation)) {
2400 msg->setInt32("generation", mTimedTextGeneration);
2401 }
2402 msg->post(timeUs - posUs);
2403 } else {
2404 sendTimedTextData(buffer);
2405 }
2406 break;
2407 }
2408
Andreas Huber14f76722013-01-15 09:04:18 -08002409 case Source::kWhatQueueDecoderShutdown:
2410 {
2411 int32_t audio, video;
2412 CHECK(msg->findInt32("audio", &audio));
2413 CHECK(msg->findInt32("video", &video));
2414
2415 sp<AMessage> reply;
2416 CHECK(msg->findMessage("reply", &reply));
2417
2418 queueDecoderShutdown(audio, video, reply);
2419 break;
2420 }
2421
Ronghua Wu80276872014-08-28 15:50:29 -07002422 case Source::kWhatDrmNoLicense:
2423 {
2424 notifyListener(MEDIA_ERROR, MEDIA_ERROR_UNKNOWN, ERROR_DRM_NO_LICENSE);
2425 break;
2426 }
2427
Andreas Huber9575c962013-02-05 13:59:56 -08002428 default:
2429 TRESPASS();
2430 }
2431}
2432
Chong Zhanga7fa1d92014-06-11 14:49:23 -07002433void NuPlayer::onClosedCaptionNotify(const sp<AMessage> &msg) {
2434 int32_t what;
2435 CHECK(msg->findInt32("what", &what));
2436
2437 switch (what) {
2438 case NuPlayer::CCDecoder::kWhatClosedCaptionData:
2439 {
2440 sp<ABuffer> buffer;
2441 CHECK(msg->findBuffer("buffer", &buffer));
2442
2443 size_t inbandTracks = 0;
2444 if (mSource != NULL) {
2445 inbandTracks = mSource->getTrackCount();
2446 }
2447
2448 sendSubtitleData(buffer, inbandTracks);
2449 break;
2450 }
2451
2452 case NuPlayer::CCDecoder::kWhatTrackAdded:
2453 {
2454 notifyListener(MEDIA_INFO, MEDIA_INFO_METADATA_UPDATE, 0);
2455
2456 break;
2457 }
2458
2459 default:
2460 TRESPASS();
2461 }
2462
2463
2464}
2465
Chong Zhang404fced2014-06-11 14:45:31 -07002466void NuPlayer::sendSubtitleData(const sp<ABuffer> &buffer, int32_t baseIndex) {
2467 int32_t trackIndex;
2468 int64_t timeUs, durationUs;
2469 CHECK(buffer->meta()->findInt32("trackIndex", &trackIndex));
2470 CHECK(buffer->meta()->findInt64("timeUs", &timeUs));
2471 CHECK(buffer->meta()->findInt64("durationUs", &durationUs));
2472
2473 Parcel in;
2474 in.writeInt32(trackIndex + baseIndex);
2475 in.writeInt64(timeUs);
2476 in.writeInt64(durationUs);
2477 in.writeInt32(buffer->size());
2478 in.writeInt32(buffer->size());
2479 in.write(buffer->data(), buffer->size());
2480
2481 notifyListener(MEDIA_SUBTITLE_DATA, 0, 0, &in);
2482}
Robert Shihd3b0bbb2014-07-23 15:00:25 -07002483
Robert Shih08528432015-04-08 09:06:54 -07002484void NuPlayer::sendTimedMetaData(const sp<ABuffer> &buffer) {
2485 int64_t timeUs;
2486 CHECK(buffer->meta()->findInt64("timeUs", &timeUs));
2487
2488 Parcel in;
2489 in.writeInt64(timeUs);
2490 in.writeInt32(buffer->size());
2491 in.writeInt32(buffer->size());
2492 in.write(buffer->data(), buffer->size());
2493
2494 notifyListener(MEDIA_META_DATA, 0, 0, &in);
2495}
2496
Robert Shihd3b0bbb2014-07-23 15:00:25 -07002497void NuPlayer::sendTimedTextData(const sp<ABuffer> &buffer) {
2498 const void *data;
2499 size_t size = 0;
2500 int64_t timeUs;
Marco Nelissen55e2f4c2015-09-04 15:57:15 -07002501 int32_t flag = TextDescriptions::IN_BAND_TEXT_3GPP;
Robert Shihd3b0bbb2014-07-23 15:00:25 -07002502
2503 AString mime;
2504 CHECK(buffer->meta()->findString("mime", &mime));
2505 CHECK(strcasecmp(mime.c_str(), MEDIA_MIMETYPE_TEXT_3GPP) == 0);
2506
2507 data = buffer->data();
2508 size = buffer->size();
2509
2510 Parcel parcel;
2511 if (size > 0) {
2512 CHECK(buffer->meta()->findInt64("timeUs", &timeUs));
Marco Nelissen55e2f4c2015-09-04 15:57:15 -07002513 int32_t global = 0;
2514 if (buffer->meta()->findInt32("global", &global) && global) {
2515 flag |= TextDescriptions::GLOBAL_DESCRIPTIONS;
2516 } else {
2517 flag |= TextDescriptions::LOCAL_DESCRIPTIONS;
2518 }
Robert Shihd3b0bbb2014-07-23 15:00:25 -07002519 TextDescriptions::getParcelOfDescriptions(
2520 (const uint8_t *)data, size, flag, timeUs / 1000, &parcel);
2521 }
2522
2523 if ((parcel.dataSize() > 0)) {
2524 notifyListener(MEDIA_TIMED_TEXT, 0, 0, &parcel);
2525 } else { // send an empty timed text
2526 notifyListener(MEDIA_TIMED_TEXT, 0, 0);
2527 }
2528}
Andreas Huberb5f25f02013-02-05 10:14:26 -08002529////////////////////////////////////////////////////////////////////////////////
2530
Chong Zhangced1c2f2014-08-08 15:22:35 -07002531sp<AMessage> NuPlayer::Source::getFormat(bool audio) {
2532 sp<MetaData> meta = getFormatMeta(audio);
2533
2534 if (meta == NULL) {
2535 return NULL;
2536 }
2537
2538 sp<AMessage> msg = new AMessage;
2539
2540 if(convertMetaDataToMessage(meta, &msg) == OK) {
2541 return msg;
2542 }
2543 return NULL;
2544}
2545
Andreas Huber9575c962013-02-05 13:59:56 -08002546void NuPlayer::Source::notifyFlagsChanged(uint32_t flags) {
2547 sp<AMessage> notify = dupNotify();
2548 notify->setInt32("what", kWhatFlagsChanged);
2549 notify->setInt32("flags", flags);
2550 notify->post();
2551}
2552
Chong Zhangced1c2f2014-08-08 15:22:35 -07002553void NuPlayer::Source::notifyVideoSizeChanged(const sp<AMessage> &format) {
Andreas Huber9575c962013-02-05 13:59:56 -08002554 sp<AMessage> notify = dupNotify();
2555 notify->setInt32("what", kWhatVideoSizeChanged);
Chong Zhangced1c2f2014-08-08 15:22:35 -07002556 notify->setMessage("format", format);
Andreas Huber9575c962013-02-05 13:59:56 -08002557 notify->post();
2558}
2559
Andreas Huberec0c5972013-02-05 14:47:13 -08002560void NuPlayer::Source::notifyPrepared(status_t err) {
Andreas Huber9575c962013-02-05 13:59:56 -08002561 sp<AMessage> notify = dupNotify();
2562 notify->setInt32("what", kWhatPrepared);
Andreas Huberec0c5972013-02-05 14:47:13 -08002563 notify->setInt32("err", err);
Andreas Huber9575c962013-02-05 13:59:56 -08002564 notify->post();
2565}
2566
Lajos Molnarfcd3e942015-03-31 10:06:48 -07002567void NuPlayer::Source::notifyInstantiateSecureDecoders(const sp<AMessage> &reply) {
2568 sp<AMessage> notify = dupNotify();
2569 notify->setInt32("what", kWhatInstantiateSecureDecoders);
2570 notify->setMessage("reply", reply);
2571 notify->post();
2572}
2573
Andreas Huber84333e02014-02-07 15:36:10 -08002574void NuPlayer::Source::onMessageReceived(const sp<AMessage> & /* msg */) {
Andreas Huberb5f25f02013-02-05 10:14:26 -08002575 TRESPASS();
2576}
2577
Andreas Huberf9334412010-12-15 15:17:42 -08002578} // namespace android