blob: fb599389835552eb77124598fe87f4052677c001 [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"
19#include <utils/Log.h>
20
21#include "NuPlayer.h"
Andreas Huber5bc087c2010-12-23 10:27:40 -080022
23#include "HTTPLiveSource.h"
Chong Zhang7137ec72014-11-12 16:41:05 -080024#include "NuPlayerCCDecoder.h"
Andreas Huberf9334412010-12-15 15:17:42 -080025#include "NuPlayerDecoder.h"
Chong Zhang7137ec72014-11-12 16:41:05 -080026#include "NuPlayerDecoderBase.h"
Wei Jiabc2fb722014-07-08 16:37:57 -070027#include "NuPlayerDecoderPassThrough.h"
Andreas Huber43c3e6c2011-01-05 12:17:08 -080028#include "NuPlayerDriver.h"
Andreas Huberf9334412010-12-15 15:17:42 -080029#include "NuPlayerRenderer.h"
Andreas Huber5bc087c2010-12-23 10:27:40 -080030#include "NuPlayerSource.h"
Andreas Huber2bfdd422011-10-11 15:24:07 -070031#include "RTSPSource.h"
Andreas Huber5bc087c2010-12-23 10:27:40 -080032#include "StreamingSource.h"
Andreas Huberafed0e12011-09-20 15:39:58 -070033#include "GenericSource.h"
Robert Shihd3b0bbb2014-07-23 15:00:25 -070034#include "TextDescriptions.h"
Andreas Huber5bc087c2010-12-23 10:27:40 -080035
36#include "ATSParser.h"
Andreas Huberf9334412010-12-15 15:17:42 -080037
Lajos Molnard9fd6312014-11-06 11:00:00 -080038#include <cutils/properties.h>
39
Lajos Molnar3a474aa2015-04-24 17:10:07 -070040#include <media/AudioResamplerPublic.h>
41#include <media/AVSyncSettings.h>
Wonsik Kim7e34bf52016-08-23 00:09:18 +090042#include <media/MediaCodecBuffer.h>
Lajos Molnar3a474aa2015-04-24 17:10:07 -070043
Andreas Huber3831a062010-12-21 10:22:33 -080044#include <media/stagefright/foundation/hexdump.h>
Andreas Huberf9334412010-12-15 15:17:42 -080045#include <media/stagefright/foundation/ABuffer.h>
46#include <media/stagefright/foundation/ADebug.h>
47#include <media/stagefright/foundation/AMessage.h>
Lajos Molnar09524832014-07-17 14:29:51 -070048#include <media/stagefright/MediaBuffer.h>
Andreas Huber3fe62152011-09-16 15:09:22 -070049#include <media/stagefright/MediaDefs.h>
Andreas Huberf9334412010-12-15 15:17:42 -080050#include <media/stagefright/MediaErrors.h>
51#include <media/stagefright/MetaData.h>
Lajos Molnar1de1e252015-04-30 18:18:34 -070052
Andy McFadden8ba01022012-12-18 09:46:54 -080053#include <gui/IGraphicBufferProducer.h>
Lajos Molnar1de1e252015-04-30 18:18:34 -070054#include <gui/Surface.h>
Andreas Huberf9334412010-12-15 15:17:42 -080055
Andreas Huber3fe62152011-09-16 15:09:22 -070056#include "avc_utils.h"
57
Andreas Huber84066782011-08-16 09:34:26 -070058#include "ESDS.h"
59#include <media/stagefright/Utils.h>
60
Andreas Huberf9334412010-12-15 15:17:42 -080061namespace android {
62
Andreas Hubera1f8ab02012-11-30 10:53:22 -080063struct NuPlayer::Action : public RefBase {
64 Action() {}
65
66 virtual void execute(NuPlayer *player) = 0;
67
68private:
69 DISALLOW_EVIL_CONSTRUCTORS(Action);
70};
71
72struct NuPlayer::SeekAction : public Action {
Wei Jiac5de0912016-11-18 10:22:14 -080073 explicit SeekAction(int64_t seekTimeUs, MediaPlayerSeekMode mode)
Wei Jia14486822016-11-02 17:51:30 -070074 : mSeekTimeUs(seekTimeUs),
Wei Jiac5de0912016-11-18 10:22:14 -080075 mMode(mode) {
Andreas Hubera1f8ab02012-11-30 10:53:22 -080076 }
77
78 virtual void execute(NuPlayer *player) {
Wei Jiac5de0912016-11-18 10:22:14 -080079 player->performSeek(mSeekTimeUs, mMode);
Andreas Hubera1f8ab02012-11-30 10:53:22 -080080 }
81
82private:
83 int64_t mSeekTimeUs;
Wei Jiac5de0912016-11-18 10:22:14 -080084 MediaPlayerSeekMode mMode;
Andreas Hubera1f8ab02012-11-30 10:53:22 -080085
86 DISALLOW_EVIL_CONSTRUCTORS(SeekAction);
87};
88
Chong Zhangf8d71772014-11-26 15:08:34 -080089struct NuPlayer::ResumeDecoderAction : public Action {
Chih-Hung Hsieh090ef602016-04-27 10:39:54 -070090 explicit ResumeDecoderAction(bool needNotify)
Chong Zhangf8d71772014-11-26 15:08:34 -080091 : mNeedNotify(needNotify) {
92 }
93
94 virtual void execute(NuPlayer *player) {
95 player->performResumeDecoders(mNeedNotify);
96 }
97
98private:
99 bool mNeedNotify;
100
101 DISALLOW_EVIL_CONSTRUCTORS(ResumeDecoderAction);
102};
103
Andreas Huber57a339c2012-12-03 11:18:00 -0800104struct NuPlayer::SetSurfaceAction : public Action {
Chih-Hung Hsieh090ef602016-04-27 10:39:54 -0700105 explicit SetSurfaceAction(const sp<Surface> &surface)
Lajos Molnar1de1e252015-04-30 18:18:34 -0700106 : mSurface(surface) {
Andreas Huber57a339c2012-12-03 11:18:00 -0800107 }
108
109 virtual void execute(NuPlayer *player) {
Lajos Molnar1de1e252015-04-30 18:18:34 -0700110 player->performSetSurface(mSurface);
Andreas Huber57a339c2012-12-03 11:18:00 -0800111 }
112
113private:
Lajos Molnar1de1e252015-04-30 18:18:34 -0700114 sp<Surface> mSurface;
Andreas Huber57a339c2012-12-03 11:18:00 -0800115
116 DISALLOW_EVIL_CONSTRUCTORS(SetSurfaceAction);
117};
118
Wei Jiafef808d2014-10-31 17:57:05 -0700119struct NuPlayer::FlushDecoderAction : public Action {
120 FlushDecoderAction(FlushCommand audio, FlushCommand video)
Andreas Huber14f76722013-01-15 09:04:18 -0800121 : mAudio(audio),
122 mVideo(video) {
123 }
124
125 virtual void execute(NuPlayer *player) {
Wei Jiafef808d2014-10-31 17:57:05 -0700126 player->performDecoderFlush(mAudio, mVideo);
Andreas Huber14f76722013-01-15 09:04:18 -0800127 }
128
129private:
Wei Jiafef808d2014-10-31 17:57:05 -0700130 FlushCommand mAudio;
131 FlushCommand mVideo;
Andreas Huber14f76722013-01-15 09:04:18 -0800132
Wei Jiafef808d2014-10-31 17:57:05 -0700133 DISALLOW_EVIL_CONSTRUCTORS(FlushDecoderAction);
Andreas Huber14f76722013-01-15 09:04:18 -0800134};
135
136struct NuPlayer::PostMessageAction : public Action {
Chih-Hung Hsieh090ef602016-04-27 10:39:54 -0700137 explicit PostMessageAction(const sp<AMessage> &msg)
Andreas Huber14f76722013-01-15 09:04:18 -0800138 : mMessage(msg) {
139 }
140
141 virtual void execute(NuPlayer *) {
142 mMessage->post();
143 }
144
145private:
146 sp<AMessage> mMessage;
147
148 DISALLOW_EVIL_CONSTRUCTORS(PostMessageAction);
149};
150
Andreas Hubera1f8ab02012-11-30 10:53:22 -0800151// Use this if there's no state necessary to save in order to execute
152// the action.
153struct NuPlayer::SimpleAction : public Action {
154 typedef void (NuPlayer::*ActionFunc)();
155
Chih-Hung Hsieh090ef602016-04-27 10:39:54 -0700156 explicit SimpleAction(ActionFunc func)
Andreas Hubera1f8ab02012-11-30 10:53:22 -0800157 : mFunc(func) {
158 }
159
160 virtual void execute(NuPlayer *player) {
161 (player->*mFunc)();
162 }
163
164private:
165 ActionFunc mFunc;
166
167 DISALLOW_EVIL_CONSTRUCTORS(SimpleAction);
168};
169
Andreas Huberf9334412010-12-15 15:17:42 -0800170////////////////////////////////////////////////////////////////////////////////
171
Ronghua Wu68845c12015-07-21 09:50:48 -0700172NuPlayer::NuPlayer(pid_t pid)
Andreas Huber9b80c2b2011-06-30 15:47:02 -0700173 : mUIDValid(false),
Ronghua Wu68845c12015-07-21 09:50:48 -0700174 mPID(pid),
Andreas Huber9575c962013-02-05 13:59:56 -0800175 mSourceFlags(0),
Wei Jiabc2fb722014-07-08 16:37:57 -0700176 mOffloadAudio(false),
Wei Jia88703c32014-08-06 11:24:07 -0700177 mAudioDecoderGeneration(0),
178 mVideoDecoderGeneration(0),
Wei Jia57568df2014-09-22 10:16:29 -0700179 mRendererGeneration(0),
Robert Shih1a5c8592015-08-04 18:07:44 -0700180 mPreviousSeekTimeUs(0),
Andreas Huber9b80c2b2011-06-30 15:47:02 -0700181 mAudioEOS(false),
Andreas Huberf9334412010-12-15 15:17:42 -0800182 mVideoEOS(false),
Andreas Huber5bc087c2010-12-23 10:27:40 -0800183 mScanSourcesPending(false),
Andreas Huber1aef2112011-01-04 14:01:29 -0800184 mScanSourcesGeneration(0),
Andreas Huberb7c8e912012-11-27 15:02:53 -0800185 mPollDurationGeneration(0),
Robert Shihd3b0bbb2014-07-23 15:00:25 -0700186 mTimedTextGeneration(0),
Andreas Huberf9334412010-12-15 15:17:42 -0800187 mFlushingAudio(NONE),
Andreas Huber1aef2112011-01-04 14:01:29 -0800188 mFlushingVideo(NONE),
Chong Zhangf8d71772014-11-26 15:08:34 -0800189 mResumePending(false),
Andreas Huber57a339c2012-12-03 11:18:00 -0800190 mVideoScalingMode(NATIVE_WINDOW_SCALING_MODE_SCALE_TO_WINDOW),
Lajos Molnar3a474aa2015-04-24 17:10:07 -0700191 mPlaybackSettings(AUDIO_PLAYBACK_RATE_DEFAULT),
192 mVideoFpsHint(-1.f),
Chong Zhangefbb6192015-01-30 17:13:27 -0800193 mStarted(false),
Wei Jia8a092d32016-06-03 14:57:24 -0700194 mPrepared(false),
Ronghua Wu64c2d172015-10-07 16:52:19 -0700195 mResetting(false),
Robert Shih0c61a0d2015-07-06 15:09:10 -0700196 mSourceStarted(false),
Chong Zhangefbb6192015-01-30 17:13:27 -0800197 mPaused(false),
Wei Jia71c75e02016-02-04 09:40:47 -0800198 mPausedByClient(true),
Chong Zhang8a048332015-05-06 15:16:28 -0700199 mPausedForBuffering(false) {
Andy Hung8d121d42014-10-03 09:53:53 -0700200 clearFlushComplete();
Andreas Huberf9334412010-12-15 15:17:42 -0800201}
202
203NuPlayer::~NuPlayer() {
204}
205
Andreas Huber9b80c2b2011-06-30 15:47:02 -0700206void NuPlayer::setUID(uid_t uid) {
207 mUIDValid = true;
208 mUID = uid;
209}
210
Andreas Huber43c3e6c2011-01-05 12:17:08 -0800211void NuPlayer::setDriver(const wp<NuPlayerDriver> &driver) {
212 mDriver = driver;
Andreas Huberf9334412010-12-15 15:17:42 -0800213}
214
Andreas Huber9575c962013-02-05 13:59:56 -0800215void NuPlayer::setDataSourceAsync(const sp<IStreamSource> &source) {
Lajos Molnar1d15ab52015-03-04 16:46:34 -0800216 sp<AMessage> msg = new AMessage(kWhatSetDataSource, this);
Andreas Huberf9334412010-12-15 15:17:42 -0800217
Lajos Molnar1d15ab52015-03-04 16:46:34 -0800218 sp<AMessage> notify = new AMessage(kWhatSourceNotify, this);
Andreas Huberb5f25f02013-02-05 10:14:26 -0800219
Andreas Huber240abcc2014-02-13 13:32:37 -0800220 msg->setObject("source", new StreamingSource(notify, source));
Andreas Huber5bc087c2010-12-23 10:27:40 -0800221 msg->post();
222}
Andreas Huberf9334412010-12-15 15:17:42 -0800223
Andreas Huberafed0e12011-09-20 15:39:58 -0700224static bool IsHTTPLiveURL(const char *url) {
225 if (!strncasecmp("http://", url, 7)
Andreas Huber99759402013-04-01 14:28:31 -0700226 || !strncasecmp("https://", url, 8)
227 || !strncasecmp("file://", url, 7)) {
Andreas Huberafed0e12011-09-20 15:39:58 -0700228 size_t len = strlen(url);
229 if (len >= 5 && !strcasecmp(".m3u8", &url[len - 5])) {
230 return true;
231 }
232
233 if (strstr(url,"m3u8")) {
234 return true;
235 }
236 }
237
238 return false;
239}
240
Andreas Huber9575c962013-02-05 13:59:56 -0800241void NuPlayer::setDataSourceAsync(
Andreas Huber1b86fe02014-01-29 11:13:26 -0800242 const sp<IMediaHTTPService> &httpService,
243 const char *url,
244 const KeyedVector<String8, String8> *headers) {
Chong Zhang3de157d2014-08-05 20:54:44 -0700245
Lajos Molnar1d15ab52015-03-04 16:46:34 -0800246 sp<AMessage> msg = new AMessage(kWhatSetDataSource, this);
Oscar Rydhé7a33b772012-02-20 10:15:48 +0100247 size_t len = strlen(url);
Andreas Huber5bc087c2010-12-23 10:27:40 -0800248
Lajos Molnar1d15ab52015-03-04 16:46:34 -0800249 sp<AMessage> notify = new AMessage(kWhatSourceNotify, this);
Andreas Huberb5f25f02013-02-05 10:14:26 -0800250
Andreas Huberafed0e12011-09-20 15:39:58 -0700251 sp<Source> source;
252 if (IsHTTPLiveURL(url)) {
Andreas Huber81e68442014-02-05 11:52:33 -0800253 source = new HTTPLiveSource(notify, httpService, url, headers);
Andreas Huberafed0e12011-09-20 15:39:58 -0700254 } else if (!strncasecmp(url, "rtsp://", 7)) {
Andreas Huber1b86fe02014-01-29 11:13:26 -0800255 source = new RTSPSource(
256 notify, httpService, url, headers, mUIDValid, mUID);
Oscar Rydhé7a33b772012-02-20 10:15:48 +0100257 } else if ((!strncasecmp(url, "http://", 7)
258 || !strncasecmp(url, "https://", 8))
259 && ((len >= 4 && !strcasecmp(".sdp", &url[len - 4]))
260 || strstr(url, ".sdp?"))) {
Andreas Huber1b86fe02014-01-29 11:13:26 -0800261 source = new RTSPSource(
262 notify, httpService, url, headers, mUIDValid, mUID, true);
Andreas Huber2bfdd422011-10-11 15:24:07 -0700263 } else {
Chong Zhang3de157d2014-08-05 20:54:44 -0700264 sp<GenericSource> genericSource =
265 new GenericSource(notify, mUIDValid, mUID);
Andreas Huber2bfdd422011-10-11 15:24:07 -0700266
Chong Zhanga19f33e2014-08-07 15:35:07 -0700267 status_t err = genericSource->setDataSource(httpService, url, headers);
Chong Zhang3de157d2014-08-05 20:54:44 -0700268
269 if (err == OK) {
270 source = genericSource;
271 } else {
Chong Zhanga19f33e2014-08-07 15:35:07 -0700272 ALOGE("Failed to set data source!");
Chong Zhang3de157d2014-08-05 20:54:44 -0700273 }
274 }
Andreas Huberafed0e12011-09-20 15:39:58 -0700275 msg->setObject("source", source);
276 msg->post();
277}
278
Andreas Huber9575c962013-02-05 13:59:56 -0800279void NuPlayer::setDataSourceAsync(int fd, int64_t offset, int64_t length) {
Lajos Molnar1d15ab52015-03-04 16:46:34 -0800280 sp<AMessage> msg = new AMessage(kWhatSetDataSource, this);
Andreas Huberafed0e12011-09-20 15:39:58 -0700281
Lajos Molnar1d15ab52015-03-04 16:46:34 -0800282 sp<AMessage> notify = new AMessage(kWhatSourceNotify, this);
Andreas Huberb5f25f02013-02-05 10:14:26 -0800283
Chong Zhang3de157d2014-08-05 20:54:44 -0700284 sp<GenericSource> source =
285 new GenericSource(notify, mUIDValid, mUID);
286
Chong Zhanga19f33e2014-08-07 15:35:07 -0700287 status_t err = source->setDataSource(fd, offset, length);
Chong Zhang3de157d2014-08-05 20:54:44 -0700288
289 if (err != OK) {
Chong Zhanga19f33e2014-08-07 15:35:07 -0700290 ALOGE("Failed to set data source!");
Chong Zhang3de157d2014-08-05 20:54:44 -0700291 source = NULL;
292 }
293
Andreas Huberafed0e12011-09-20 15:39:58 -0700294 msg->setObject("source", source);
Andreas Huberf9334412010-12-15 15:17:42 -0800295 msg->post();
296}
297
Chris Watkins99f31602015-03-20 13:06:33 -0700298void NuPlayer::setDataSourceAsync(const sp<DataSource> &dataSource) {
299 sp<AMessage> msg = new AMessage(kWhatSetDataSource, this);
300 sp<AMessage> notify = new AMessage(kWhatSourceNotify, this);
301
302 sp<GenericSource> source = new GenericSource(notify, mUIDValid, mUID);
303 status_t err = source->setDataSource(dataSource);
304
305 if (err != OK) {
306 ALOGE("Failed to set data source!");
307 source = NULL;
308 }
309
310 msg->setObject("source", source);
311 msg->post();
312}
313
Wei Jia48fa06d2016-12-20 15:30:49 -0800314status_t NuPlayer::getDefaultBufferingSettings(
315 BufferingSettings *buffering /* nonnull */) {
316 sp<AMessage> msg = new AMessage(kWhatGetDefaultBufferingSettings, this);
317 sp<AMessage> response;
318 status_t err = msg->postAndAwaitResponse(&response);
319 if (err == OK && response != NULL) {
320 CHECK(response->findInt32("err", &err));
321 if (err == OK) {
322 readFromAMessage(response, buffering);
323 }
324 }
325 return err;
326}
327
328status_t NuPlayer::setBufferingSettings(const BufferingSettings& buffering) {
329 sp<AMessage> msg = new AMessage(kWhatSetBufferingSettings, this);
330 writeToAMessage(msg, buffering);
331 sp<AMessage> response;
332 status_t err = msg->postAndAwaitResponse(&response);
333 if (err == OK && response != NULL) {
334 CHECK(response->findInt32("err", &err));
335 }
336 return err;
337}
338
Andreas Huber9575c962013-02-05 13:59:56 -0800339void NuPlayer::prepareAsync() {
Lajos Molnar1d15ab52015-03-04 16:46:34 -0800340 (new AMessage(kWhatPrepare, this))->post();
Andreas Huber9575c962013-02-05 13:59:56 -0800341}
342
Andreas Huber57a339c2012-12-03 11:18:00 -0800343void NuPlayer::setVideoSurfaceTextureAsync(
Andy McFadden8ba01022012-12-18 09:46:54 -0800344 const sp<IGraphicBufferProducer> &bufferProducer) {
Lajos Molnar1de1e252015-04-30 18:18:34 -0700345 sp<AMessage> msg = new AMessage(kWhatSetVideoSurface, this);
Andreas Huber57a339c2012-12-03 11:18:00 -0800346
Andy McFadden8ba01022012-12-18 09:46:54 -0800347 if (bufferProducer == NULL) {
Lajos Molnar1de1e252015-04-30 18:18:34 -0700348 msg->setObject("surface", NULL);
Andreas Huber57a339c2012-12-03 11:18:00 -0800349 } else {
Lajos Molnar1de1e252015-04-30 18:18:34 -0700350 msg->setObject("surface", new Surface(bufferProducer, true /* controlledByApp */));
Andreas Huber57a339c2012-12-03 11:18:00 -0800351 }
352
Andreas Huberf9334412010-12-15 15:17:42 -0800353 msg->post();
354}
355
356void NuPlayer::setAudioSink(const sp<MediaPlayerBase::AudioSink> &sink) {
Lajos Molnar1d15ab52015-03-04 16:46:34 -0800357 sp<AMessage> msg = new AMessage(kWhatSetAudioSink, this);
Andreas Huberf9334412010-12-15 15:17:42 -0800358 msg->setObject("sink", sink);
359 msg->post();
360}
361
362void NuPlayer::start() {
Lajos Molnar1d15ab52015-03-04 16:46:34 -0800363 (new AMessage(kWhatStart, this))->post();
Andreas Huberf9334412010-12-15 15:17:42 -0800364}
365
Lajos Molnar3a474aa2015-04-24 17:10:07 -0700366status_t NuPlayer::setPlaybackSettings(const AudioPlaybackRate &rate) {
367 // do some cursory validation of the settings here. audio modes are
368 // only validated when set on the audiosink.
369 if ((rate.mSpeed != 0.f && rate.mSpeed < AUDIO_TIMESTRETCH_SPEED_MIN)
370 || rate.mSpeed > AUDIO_TIMESTRETCH_SPEED_MAX
371 || rate.mPitch < AUDIO_TIMESTRETCH_SPEED_MIN
372 || rate.mPitch > AUDIO_TIMESTRETCH_SPEED_MAX) {
373 return BAD_VALUE;
374 }
375 sp<AMessage> msg = new AMessage(kWhatConfigPlayback, this);
376 writeToAMessage(msg, rate);
377 sp<AMessage> response;
378 status_t err = msg->postAndAwaitResponse(&response);
379 if (err == OK && response != NULL) {
380 CHECK(response->findInt32("err", &err));
381 }
382 return err;
383}
384
385status_t NuPlayer::getPlaybackSettings(AudioPlaybackRate *rate /* nonnull */) {
386 sp<AMessage> msg = new AMessage(kWhatGetPlaybackSettings, this);
387 sp<AMessage> response;
388 status_t err = msg->postAndAwaitResponse(&response);
389 if (err == OK && response != NULL) {
390 CHECK(response->findInt32("err", &err));
391 if (err == OK) {
392 readFromAMessage(response, rate);
393 }
394 }
395 return err;
396}
397
398status_t NuPlayer::setSyncSettings(const AVSyncSettings &sync, float videoFpsHint) {
399 sp<AMessage> msg = new AMessage(kWhatConfigSync, this);
400 writeToAMessage(msg, sync, videoFpsHint);
401 sp<AMessage> response;
402 status_t err = msg->postAndAwaitResponse(&response);
403 if (err == OK && response != NULL) {
404 CHECK(response->findInt32("err", &err));
405 }
406 return err;
407}
408
409status_t NuPlayer::getSyncSettings(
410 AVSyncSettings *sync /* nonnull */, float *videoFps /* nonnull */) {
411 sp<AMessage> msg = new AMessage(kWhatGetSyncSettings, this);
412 sp<AMessage> response;
413 status_t err = msg->postAndAwaitResponse(&response);
414 if (err == OK && response != NULL) {
415 CHECK(response->findInt32("err", &err));
416 if (err == OK) {
417 readFromAMessage(response, sync, videoFps);
418 }
419 }
420 return err;
Wei Jia98160162015-02-04 17:01:11 -0800421}
422
Andreas Huber43c3e6c2011-01-05 12:17:08 -0800423void NuPlayer::pause() {
Lajos Molnar1d15ab52015-03-04 16:46:34 -0800424 (new AMessage(kWhatPause, this))->post();
Andreas Huber43c3e6c2011-01-05 12:17:08 -0800425}
426
Andreas Huber1aef2112011-01-04 14:01:29 -0800427void NuPlayer::resetAsync() {
Wei Jiac45a4b22016-04-15 15:30:23 -0700428 sp<Source> source;
429 {
430 Mutex::Autolock autoLock(mSourceLock);
431 source = mSource;
432 }
433
434 if (source != NULL) {
Chong Zhang48296b72014-09-14 14:28:45 -0700435 // During a reset, the data source might be unresponsive already, we need to
436 // disconnect explicitly so that reads exit promptly.
437 // We can't queue the disconnect request to the looper, as it might be
438 // queued behind a stuck read and never gets processed.
439 // Doing a disconnect outside the looper to allows the pending reads to exit
440 // (either successfully or with error).
Wei Jiac45a4b22016-04-15 15:30:23 -0700441 source->disconnect();
Chong Zhang48296b72014-09-14 14:28:45 -0700442 }
443
Lajos Molnar1d15ab52015-03-04 16:46:34 -0800444 (new AMessage(kWhatReset, this))->post();
Andreas Huber1aef2112011-01-04 14:01:29 -0800445}
446
Wei Jiac5de0912016-11-18 10:22:14 -0800447void NuPlayer::seekToAsync(int64_t seekTimeUs, MediaPlayerSeekMode mode, bool needNotify) {
Lajos Molnar1d15ab52015-03-04 16:46:34 -0800448 sp<AMessage> msg = new AMessage(kWhatSeek, this);
Andreas Huber43c3e6c2011-01-05 12:17:08 -0800449 msg->setInt64("seekTimeUs", seekTimeUs);
Wei Jiac5de0912016-11-18 10:22:14 -0800450 msg->setInt32("mode", mode);
Wei Jiae427abf2014-09-22 15:21:11 -0700451 msg->setInt32("needNotify", needNotify);
Andreas Huber43c3e6c2011-01-05 12:17:08 -0800452 msg->post();
453}
454
Andreas Huber53df1a42010-12-22 10:03:04 -0800455
Chong Zhang404fced2014-06-11 14:45:31 -0700456void NuPlayer::writeTrackInfo(
Chih-Hung Hsiehe964d4e2016-08-09 14:31:32 -0700457 Parcel* reply, const sp<AMessage>& format) const {
Marco Nelissenc367ca12015-09-15 09:51:59 -0700458 if (format == NULL) {
459 ALOGE("NULL format");
460 return;
461 }
Chong Zhang404fced2014-06-11 14:45:31 -0700462 int32_t trackType;
Marco Nelissen9436e482015-09-15 10:21:53 -0700463 if (!format->findInt32("type", &trackType)) {
Marco Nelissenc367ca12015-09-15 09:51:59 -0700464 ALOGE("no track type");
465 return;
466 }
Chong Zhang404fced2014-06-11 14:45:31 -0700467
Robert Shih755106e2015-04-30 14:36:45 -0700468 AString mime;
Robert Shih2e3a4252015-05-06 10:21:15 -0700469 if (!format->findString("mime", &mime)) {
470 // Java MediaPlayer only uses mimetype for subtitle and timedtext tracks.
471 // If we can't find the mimetype here it means that we wouldn't be needing
472 // the mimetype on the Java end. We still write a placeholder mime to keep the
473 // (de)serialization logic simple.
474 if (trackType == MEDIA_TRACK_TYPE_AUDIO) {
475 mime = "audio/";
476 } else if (trackType == MEDIA_TRACK_TYPE_VIDEO) {
477 mime = "video/";
478 } else {
Marco Nelissenc367ca12015-09-15 09:51:59 -0700479 ALOGE("unknown track type: %d", trackType);
480 return;
Robert Shih2e3a4252015-05-06 10:21:15 -0700481 }
482 }
Robert Shih755106e2015-04-30 14:36:45 -0700483
Chong Zhang404fced2014-06-11 14:45:31 -0700484 AString lang;
Marco Nelissen9436e482015-09-15 10:21:53 -0700485 if (!format->findString("language", &lang)) {
Marco Nelissenc367ca12015-09-15 09:51:59 -0700486 ALOGE("no language");
487 return;
488 }
Chong Zhang404fced2014-06-11 14:45:31 -0700489
490 reply->writeInt32(2); // write something non-zero
491 reply->writeInt32(trackType);
Robert Shih755106e2015-04-30 14:36:45 -0700492 reply->writeString16(String16(mime.c_str()));
Chong Zhang404fced2014-06-11 14:45:31 -0700493 reply->writeString16(String16(lang.c_str()));
494
495 if (trackType == MEDIA_TRACK_TYPE_SUBTITLE) {
Chong Zhang404fced2014-06-11 14:45:31 -0700496 int32_t isAuto, isDefault, isForced;
497 CHECK(format->findInt32("auto", &isAuto));
498 CHECK(format->findInt32("default", &isDefault));
499 CHECK(format->findInt32("forced", &isForced));
500
Chong Zhang404fced2014-06-11 14:45:31 -0700501 reply->writeInt32(isAuto);
502 reply->writeInt32(isDefault);
503 reply->writeInt32(isForced);
504 }
505}
506
Andreas Huberf9334412010-12-15 15:17:42 -0800507void NuPlayer::onMessageReceived(const sp<AMessage> &msg) {
508 switch (msg->what()) {
509 case kWhatSetDataSource:
510 {
Steve Block3856b092011-10-20 11:56:00 +0100511 ALOGV("kWhatSetDataSource");
Andreas Huberf9334412010-12-15 15:17:42 -0800512
513 CHECK(mSource == NULL);
514
Chong Zhang3de157d2014-08-05 20:54:44 -0700515 status_t err = OK;
Andreas Huber5bc087c2010-12-23 10:27:40 -0800516 sp<RefBase> obj;
517 CHECK(msg->findObject("source", &obj));
Chong Zhang3de157d2014-08-05 20:54:44 -0700518 if (obj != NULL) {
Wei Jiac45a4b22016-04-15 15:30:23 -0700519 Mutex::Autolock autoLock(mSourceLock);
Chong Zhang3de157d2014-08-05 20:54:44 -0700520 mSource = static_cast<Source *>(obj.get());
Chong Zhang3de157d2014-08-05 20:54:44 -0700521 } else {
522 err = UNKNOWN_ERROR;
523 }
Andreas Huber9575c962013-02-05 13:59:56 -0800524
525 CHECK(mDriver != NULL);
526 sp<NuPlayerDriver> driver = mDriver.promote();
527 if (driver != NULL) {
Chong Zhang3de157d2014-08-05 20:54:44 -0700528 driver->notifySetDataSourceCompleted(err);
Andreas Huber9575c962013-02-05 13:59:56 -0800529 }
530 break;
531 }
532
Wei Jia48fa06d2016-12-20 15:30:49 -0800533 case kWhatGetDefaultBufferingSettings:
534 {
535 sp<AReplyToken> replyID;
536 CHECK(msg->senderAwaitsResponse(&replyID));
537
538 ALOGV("kWhatGetDefaultBufferingSettings");
539 BufferingSettings buffering;
540 status_t err = OK;
541 if (mSource != NULL) {
542 err = mSource->getDefaultBufferingSettings(&buffering);
543 } else {
544 err = INVALID_OPERATION;
545 }
546 sp<AMessage> response = new AMessage;
547 if (err == OK) {
548 writeToAMessage(response, buffering);
549 }
550 response->setInt32("err", err);
551 response->postReply(replyID);
552 break;
553 }
554
555 case kWhatSetBufferingSettings:
556 {
557 sp<AReplyToken> replyID;
558 CHECK(msg->senderAwaitsResponse(&replyID));
559
560 ALOGV("kWhatSetBufferingSettings");
561 BufferingSettings buffering;
562 readFromAMessage(msg, &buffering);
563 status_t err = OK;
564 if (mSource != NULL) {
565 err = mSource->setBufferingSettings(buffering);
566 } else {
567 err = INVALID_OPERATION;
568 }
569 sp<AMessage> response = new AMessage;
570 response->setInt32("err", err);
571 response->postReply(replyID);
572 break;
573 }
574
Andreas Huber9575c962013-02-05 13:59:56 -0800575 case kWhatPrepare:
576 {
577 mSource->prepareAsync();
Andreas Huberf9334412010-12-15 15:17:42 -0800578 break;
579 }
580
Chong Zhangdcb89b32013-08-06 09:44:47 -0700581 case kWhatGetTrackInfo:
582 {
Lajos Molnar3f274362015-03-05 14:35:41 -0800583 sp<AReplyToken> replyID;
Chong Zhangdcb89b32013-08-06 09:44:47 -0700584 CHECK(msg->senderAwaitsResponse(&replyID));
585
Chong Zhang404fced2014-06-11 14:45:31 -0700586 Parcel* reply;
587 CHECK(msg->findPointer("reply", (void**)&reply));
588
589 size_t inbandTracks = 0;
Chong Zhangdcb89b32013-08-06 09:44:47 -0700590 if (mSource != NULL) {
Chong Zhang404fced2014-06-11 14:45:31 -0700591 inbandTracks = mSource->getTrackCount();
592 }
593
Chong Zhanga7fa1d92014-06-11 14:49:23 -0700594 size_t ccTracks = 0;
595 if (mCCDecoder != NULL) {
596 ccTracks = mCCDecoder->getTrackCount();
597 }
598
Chong Zhang404fced2014-06-11 14:45:31 -0700599 // total track count
Chong Zhanga7fa1d92014-06-11 14:49:23 -0700600 reply->writeInt32(inbandTracks + ccTracks);
Chong Zhang404fced2014-06-11 14:45:31 -0700601
602 // write inband tracks
603 for (size_t i = 0; i < inbandTracks; ++i) {
604 writeTrackInfo(reply, mSource->getTrackInfo(i));
Chong Zhangdcb89b32013-08-06 09:44:47 -0700605 }
606
Chong Zhanga7fa1d92014-06-11 14:49:23 -0700607 // write CC track
608 for (size_t i = 0; i < ccTracks; ++i) {
609 writeTrackInfo(reply, mCCDecoder->getTrackInfo(i));
610 }
611
Chong Zhangdcb89b32013-08-06 09:44:47 -0700612 sp<AMessage> response = new AMessage;
Chong Zhangdcb89b32013-08-06 09:44:47 -0700613 response->postReply(replyID);
614 break;
615 }
616
Robert Shih7c4f0d72014-07-09 18:53:31 -0700617 case kWhatGetSelectedTrack:
618 {
619 status_t err = INVALID_OPERATION;
620 if (mSource != NULL) {
621 err = OK;
622
623 int32_t type32;
624 CHECK(msg->findInt32("type", (int32_t*)&type32));
625 media_track_type type = (media_track_type)type32;
626 ssize_t selectedTrack = mSource->getSelectedTrack(type);
627
628 Parcel* reply;
629 CHECK(msg->findPointer("reply", (void**)&reply));
630 reply->writeInt32(selectedTrack);
631 }
632
633 sp<AMessage> response = new AMessage;
634 response->setInt32("err", err);
635
Lajos Molnar3f274362015-03-05 14:35:41 -0800636 sp<AReplyToken> replyID;
Robert Shih7c4f0d72014-07-09 18:53:31 -0700637 CHECK(msg->senderAwaitsResponse(&replyID));
638 response->postReply(replyID);
639 break;
640 }
641
Chong Zhangdcb89b32013-08-06 09:44:47 -0700642 case kWhatSelectTrack:
643 {
Lajos Molnar3f274362015-03-05 14:35:41 -0800644 sp<AReplyToken> replyID;
Chong Zhangdcb89b32013-08-06 09:44:47 -0700645 CHECK(msg->senderAwaitsResponse(&replyID));
646
Chong Zhang404fced2014-06-11 14:45:31 -0700647 size_t trackIndex;
648 int32_t select;
Robert Shih6ffb1fd2014-10-29 16:24:32 -0700649 int64_t timeUs;
Chong Zhang404fced2014-06-11 14:45:31 -0700650 CHECK(msg->findSize("trackIndex", &trackIndex));
651 CHECK(msg->findInt32("select", &select));
Robert Shih6ffb1fd2014-10-29 16:24:32 -0700652 CHECK(msg->findInt64("timeUs", &timeUs));
Chong Zhang404fced2014-06-11 14:45:31 -0700653
Chong Zhangdcb89b32013-08-06 09:44:47 -0700654 status_t err = INVALID_OPERATION;
Chong Zhang404fced2014-06-11 14:45:31 -0700655
656 size_t inbandTracks = 0;
Chong Zhangdcb89b32013-08-06 09:44:47 -0700657 if (mSource != NULL) {
Chong Zhang404fced2014-06-11 14:45:31 -0700658 inbandTracks = mSource->getTrackCount();
659 }
Chong Zhanga7fa1d92014-06-11 14:49:23 -0700660 size_t ccTracks = 0;
661 if (mCCDecoder != NULL) {
662 ccTracks = mCCDecoder->getTrackCount();
663 }
Chong Zhang404fced2014-06-11 14:45:31 -0700664
665 if (trackIndex < inbandTracks) {
Robert Shih6ffb1fd2014-10-29 16:24:32 -0700666 err = mSource->selectTrack(trackIndex, select, timeUs);
Robert Shihd3b0bbb2014-07-23 15:00:25 -0700667
668 if (!select && err == OK) {
669 int32_t type;
670 sp<AMessage> info = mSource->getTrackInfo(trackIndex);
671 if (info != NULL
672 && info->findInt32("type", &type)
673 && type == MEDIA_TRACK_TYPE_TIMEDTEXT) {
674 ++mTimedTextGeneration;
675 }
676 }
Chong Zhanga7fa1d92014-06-11 14:49:23 -0700677 } else {
678 trackIndex -= inbandTracks;
679
680 if (trackIndex < ccTracks) {
681 err = mCCDecoder->selectTrack(trackIndex, select);
682 }
Chong Zhangdcb89b32013-08-06 09:44:47 -0700683 }
684
685 sp<AMessage> response = new AMessage;
686 response->setInt32("err", err);
687
688 response->postReply(replyID);
689 break;
690 }
691
Andreas Huberb7c8e912012-11-27 15:02:53 -0800692 case kWhatPollDuration:
693 {
694 int32_t generation;
695 CHECK(msg->findInt32("generation", &generation));
696
697 if (generation != mPollDurationGeneration) {
698 // stale
699 break;
700 }
701
702 int64_t durationUs;
703 if (mDriver != NULL && mSource->getDuration(&durationUs) == OK) {
704 sp<NuPlayerDriver> driver = mDriver.promote();
705 if (driver != NULL) {
706 driver->notifyDuration(durationUs);
707 }
708 }
709
710 msg->post(1000000ll); // poll again in a second.
711 break;
712 }
713
Lajos Molnar1de1e252015-04-30 18:18:34 -0700714 case kWhatSetVideoSurface:
Andreas Huberf9334412010-12-15 15:17:42 -0800715 {
Andreas Huberf9334412010-12-15 15:17:42 -0800716
717 sp<RefBase> obj;
Lajos Molnar1de1e252015-04-30 18:18:34 -0700718 CHECK(msg->findObject("surface", &obj));
719 sp<Surface> surface = static_cast<Surface *>(obj.get());
Lajos Molnara81c6222015-07-10 19:17:45 -0700720
721 ALOGD("onSetVideoSurface(%p, %s video decoder)",
722 surface.get(),
Wei Jia6b7d2ed2015-08-10 15:11:54 -0700723 (mSource != NULL && mStarted && mSource->getFormat(false /* audio */) != NULL
Lajos Molnara81c6222015-07-10 19:17:45 -0700724 && mVideoDecoder != NULL) ? "have" : "no");
725
Wei Jia6b7d2ed2015-08-10 15:11:54 -0700726 // Need to check mStarted before calling mSource->getFormat because NuPlayer might
727 // be in preparing state and it could take long time.
728 // When mStarted is true, mSource must have been set.
729 if (mSource == NULL || !mStarted || mSource->getFormat(false /* audio */) == NULL
Lajos Molnara81c6222015-07-10 19:17:45 -0700730 // NOTE: mVideoDecoder's mSurface is always non-null
731 || (mVideoDecoder != NULL && mVideoDecoder->setVideoSurface(surface) == OK)) {
Lajos Molnar1de1e252015-04-30 18:18:34 -0700732 performSetSurface(surface);
Wei Jiafef808d2014-10-31 17:57:05 -0700733 break;
734 }
735
736 mDeferredActions.push_back(
737 new FlushDecoderAction(FLUSH_CMD_FLUSH /* audio */,
738 FLUSH_CMD_SHUTDOWN /* video */));
739
Lajos Molnar1de1e252015-04-30 18:18:34 -0700740 mDeferredActions.push_back(new SetSurfaceAction(surface));
James Dong0d268a32012-08-31 12:18:27 -0700741
Wei Jiac6e58412015-07-10 18:04:55 -0700742 if (obj != NULL || mAudioDecoder != NULL) {
Wei Jiafef808d2014-10-31 17:57:05 -0700743 if (mStarted) {
Andy Hung73535852014-09-05 11:42:58 -0700744 // Issue a seek to refresh the video screen only if started otherwise
745 // the extractor may not yet be started and will assert.
746 // If the video decoder is not set (perhaps audio only in this case)
747 // do not perform a seek as it is not needed.
Ronghua Wua73d9e02014-10-08 15:13:29 -0700748 int64_t currentPositionUs = 0;
749 if (getCurrentPosition(&currentPositionUs) == OK) {
750 mDeferredActions.push_back(
Wei Jiac5de0912016-11-18 10:22:14 -0800751 new SeekAction(currentPositionUs,
752 MediaPlayerSeekMode::SEEK_PREVIOUS_SYNC /* mode */));
Ronghua Wua73d9e02014-10-08 15:13:29 -0700753 }
Andy Hung73535852014-09-05 11:42:58 -0700754 }
Wei Jiaac428aa2014-09-02 19:01:34 -0700755
Andreas Huber57a339c2012-12-03 11:18:00 -0800756 // If there is a new surface texture, instantiate decoders
757 // again if possible.
758 mDeferredActions.push_back(
759 new SimpleAction(&NuPlayer::performScanSources));
760 }
761
Chong Zhangf8d71772014-11-26 15:08:34 -0800762 // After a flush without shutdown, decoder is paused.
763 // Don't resume it until source seek is done, otherwise it could
Chong Zhang7137ec72014-11-12 16:41:05 -0800764 // start pulling stale data too soon.
765 mDeferredActions.push_back(
Chong Zhangf8d71772014-11-26 15:08:34 -0800766 new ResumeDecoderAction(false /* needNotify */));
Chong Zhang7137ec72014-11-12 16:41:05 -0800767
Andreas Huber57a339c2012-12-03 11:18:00 -0800768 processDeferredActions();
Andreas Huberf9334412010-12-15 15:17:42 -0800769 break;
770 }
771
772 case kWhatSetAudioSink:
773 {
Steve Block3856b092011-10-20 11:56:00 +0100774 ALOGV("kWhatSetAudioSink");
Andreas Huberf9334412010-12-15 15:17:42 -0800775
776 sp<RefBase> obj;
777 CHECK(msg->findObject("sink", &obj));
778
779 mAudioSink = static_cast<MediaPlayerBase::AudioSink *>(obj.get());
780 break;
781 }
782
783 case kWhatStart:
784 {
Steve Block3856b092011-10-20 11:56:00 +0100785 ALOGV("kWhatStart");
Wei Jia94211742014-10-28 17:09:06 -0700786 if (mStarted) {
Chong Zhang8a048332015-05-06 15:16:28 -0700787 // do not resume yet if the source is still buffering
788 if (!mPausedForBuffering) {
789 onResume();
790 }
Wei Jia94211742014-10-28 17:09:06 -0700791 } else {
792 onStart();
Lajos Molnar09524832014-07-17 14:29:51 -0700793 }
Chong Zhangefbb6192015-01-30 17:13:27 -0800794 mPausedByClient = false;
Andreas Huberf9334412010-12-15 15:17:42 -0800795 break;
796 }
797
Lajos Molnar3a474aa2015-04-24 17:10:07 -0700798 case kWhatConfigPlayback:
Wei Jia98160162015-02-04 17:01:11 -0800799 {
Lajos Molnar3a474aa2015-04-24 17:10:07 -0700800 sp<AReplyToken> replyID;
801 CHECK(msg->senderAwaitsResponse(&replyID));
802 AudioPlaybackRate rate /* sanitized */;
803 readFromAMessage(msg, &rate);
804 status_t err = OK;
Wei Jia98160162015-02-04 17:01:11 -0800805 if (mRenderer != NULL) {
Wei Jiabf70feb2016-02-19 15:47:16 -0800806 // AudioSink allows only 1.f and 0.f for offload mode.
807 // For other speed, switch to non-offload mode.
808 if (mOffloadAudio && ((rate.mSpeed != 0.f && rate.mSpeed != 1.f)
809 || rate.mPitch != 1.f)) {
810 int64_t currentPositionUs;
811 if (getCurrentPosition(&currentPositionUs) != OK) {
812 currentPositionUs = mPreviousSeekTimeUs;
813 }
814
815 // Set mPlaybackSettings so that the new audio decoder can
816 // be created correctly.
817 mPlaybackSettings = rate;
818 if (!mPaused) {
819 mRenderer->pause();
820 }
Wei Jia5031b2f2016-02-25 11:19:31 -0800821 restartAudio(
Wei Jiabf70feb2016-02-19 15:47:16 -0800822 currentPositionUs, true /* forceNonOffload */,
823 true /* needsToCreateAudioDecoder */);
824 if (!mPaused) {
825 mRenderer->resume();
826 }
827 }
828
Lajos Molnar3a474aa2015-04-24 17:10:07 -0700829 err = mRenderer->setPlaybackSettings(rate);
830 }
831 if (err == OK) {
832 if (rate.mSpeed == 0.f) {
833 onPause();
Wei Jia351ce872016-02-10 13:21:59 -0800834 mPausedByClient = true;
Lajos Molnar3a474aa2015-04-24 17:10:07 -0700835 // save all other settings (using non-paused speed)
836 // so we can restore them on start
837 AudioPlaybackRate newRate = rate;
838 newRate.mSpeed = mPlaybackSettings.mSpeed;
839 mPlaybackSettings = newRate;
840 } else { /* rate.mSpeed != 0.f */
Lajos Molnar3a474aa2015-04-24 17:10:07 -0700841 mPlaybackSettings = rate;
Wei Jia8a092d32016-06-03 14:57:24 -0700842 if (mStarted) {
843 // do not resume yet if the source is still buffering
844 if (!mPausedForBuffering) {
845 onResume();
846 }
847 } else if (mPrepared) {
848 onStart();
849 }
850
851 mPausedByClient = false;
Lajos Molnar3a474aa2015-04-24 17:10:07 -0700852 }
Wei Jia98160162015-02-04 17:01:11 -0800853 }
Ronghua Wu8db88132015-04-22 13:51:35 -0700854
855 if (mVideoDecoder != NULL) {
Praveen Chavanbbaa1442016-04-08 13:33:49 -0700856 sp<AMessage> params = new AMessage();
857 params->setFloat("playback-speed", mPlaybackSettings.mSpeed);
858 mVideoDecoder->setParameters(params);
Ronghua Wu8db88132015-04-22 13:51:35 -0700859 }
860
Lajos Molnar3a474aa2015-04-24 17:10:07 -0700861 sp<AMessage> response = new AMessage;
862 response->setInt32("err", err);
863 response->postReply(replyID);
864 break;
865 }
866
867 case kWhatGetPlaybackSettings:
868 {
869 sp<AReplyToken> replyID;
870 CHECK(msg->senderAwaitsResponse(&replyID));
871 AudioPlaybackRate rate = mPlaybackSettings;
872 status_t err = OK;
873 if (mRenderer != NULL) {
874 err = mRenderer->getPlaybackSettings(&rate);
875 }
876 if (err == OK) {
877 // get playback settings used by renderer, as it may be
878 // slightly off due to audiosink not taking small changes.
879 mPlaybackSettings = rate;
880 if (mPaused) {
881 rate.mSpeed = 0.f;
882 }
883 }
884 sp<AMessage> response = new AMessage;
885 if (err == OK) {
886 writeToAMessage(response, rate);
887 }
888 response->setInt32("err", err);
889 response->postReply(replyID);
890 break;
891 }
892
893 case kWhatConfigSync:
894 {
895 sp<AReplyToken> replyID;
896 CHECK(msg->senderAwaitsResponse(&replyID));
897
898 ALOGV("kWhatConfigSync");
899 AVSyncSettings sync;
900 float videoFpsHint;
901 readFromAMessage(msg, &sync, &videoFpsHint);
902 status_t err = OK;
903 if (mRenderer != NULL) {
904 err = mRenderer->setSyncSettings(sync, videoFpsHint);
905 }
906 if (err == OK) {
907 mSyncSettings = sync;
908 mVideoFpsHint = videoFpsHint;
909 }
910 sp<AMessage> response = new AMessage;
911 response->setInt32("err", err);
912 response->postReply(replyID);
913 break;
914 }
915
916 case kWhatGetSyncSettings:
917 {
918 sp<AReplyToken> replyID;
919 CHECK(msg->senderAwaitsResponse(&replyID));
920 AVSyncSettings sync = mSyncSettings;
921 float videoFps = mVideoFpsHint;
922 status_t err = OK;
923 if (mRenderer != NULL) {
924 err = mRenderer->getSyncSettings(&sync, &videoFps);
925 if (err == OK) {
926 mSyncSettings = sync;
927 mVideoFpsHint = videoFps;
928 }
929 }
930 sp<AMessage> response = new AMessage;
931 if (err == OK) {
932 writeToAMessage(response, sync, videoFps);
933 }
934 response->setInt32("err", err);
935 response->postReply(replyID);
Wei Jia98160162015-02-04 17:01:11 -0800936 break;
937 }
938
Andreas Huberf9334412010-12-15 15:17:42 -0800939 case kWhatScanSources:
940 {
Andreas Huber1aef2112011-01-04 14:01:29 -0800941 int32_t generation;
942 CHECK(msg->findInt32("generation", &generation));
943 if (generation != mScanSourcesGeneration) {
944 // Drop obsolete msg.
945 break;
946 }
947
Andreas Huber5bc087c2010-12-23 10:27:40 -0800948 mScanSourcesPending = false;
949
Steve Block3856b092011-10-20 11:56:00 +0100950 ALOGV("scanning sources haveAudio=%d, haveVideo=%d",
Andreas Huber43c3e6c2011-01-05 12:17:08 -0800951 mAudioDecoder != NULL, mVideoDecoder != NULL);
952
Andreas Huberb7c8e912012-11-27 15:02:53 -0800953 bool mHadAnySourcesBefore =
954 (mAudioDecoder != NULL) || (mVideoDecoder != NULL);
Robert Shih7350b052015-10-01 15:50:14 -0700955 bool rescan = false;
Andreas Huberb7c8e912012-11-27 15:02:53 -0800956
Andy Hung282a7e32014-08-14 15:56:34 -0700957 // initialize video before audio because successful initialization of
958 // video may change deep buffer mode of audio.
Lajos Molnar1de1e252015-04-30 18:18:34 -0700959 if (mSurface != NULL) {
Robert Shih7350b052015-10-01 15:50:14 -0700960 if (instantiateDecoder(false, &mVideoDecoder) == -EWOULDBLOCK) {
961 rescan = true;
962 }
Haynes Mathew George5d246ef2012-07-09 10:36:57 -0700963 }
Andreas Huberf9334412010-12-15 15:17:42 -0800964
Ronghua Wua10fd232014-11-06 16:15:20 -0800965 // Don't try to re-open audio sink if there's an existing decoder.
966 if (mAudioSink != NULL && mAudioDecoder == NULL) {
Robert Shih7350b052015-10-01 15:50:14 -0700967 if (instantiateDecoder(true, &mAudioDecoder) == -EWOULDBLOCK) {
968 rescan = true;
969 }
Andreas Huberf9334412010-12-15 15:17:42 -0800970 }
971
Andreas Huberb7c8e912012-11-27 15:02:53 -0800972 if (!mHadAnySourcesBefore
973 && (mAudioDecoder != NULL || mVideoDecoder != NULL)) {
974 // This is the first time we've found anything playable.
975
Andreas Huber9575c962013-02-05 13:59:56 -0800976 if (mSourceFlags & Source::FLAG_DYNAMIC_DURATION) {
Andreas Huberb7c8e912012-11-27 15:02:53 -0800977 schedulePollDuration();
978 }
979 }
980
Andreas Hubereac68ba2011-09-27 12:12:25 -0700981 status_t err;
982 if ((err = mSource->feedMoreTSData()) != OK) {
Andreas Huber1aef2112011-01-04 14:01:29 -0800983 if (mAudioDecoder == NULL && mVideoDecoder == NULL) {
984 // We're not currently decoding anything (no audio or
985 // video tracks found) and we just ran out of input data.
Andreas Hubereac68ba2011-09-27 12:12:25 -0700986
987 if (err == ERROR_END_OF_STREAM) {
988 notifyListener(MEDIA_PLAYBACK_COMPLETE, 0, 0);
989 } else {
990 notifyListener(MEDIA_ERROR, MEDIA_ERROR_UNKNOWN, err);
991 }
Andreas Huber1aef2112011-01-04 14:01:29 -0800992 }
Andreas Huberf9334412010-12-15 15:17:42 -0800993 break;
994 }
995
Robert Shih7350b052015-10-01 15:50:14 -0700996 if (rescan) {
Andreas Huberf9334412010-12-15 15:17:42 -0800997 msg->post(100000ll);
Andreas Huber5bc087c2010-12-23 10:27:40 -0800998 mScanSourcesPending = true;
Andreas Huberf9334412010-12-15 15:17:42 -0800999 }
1000 break;
1001 }
1002
1003 case kWhatVideoNotify:
1004 case kWhatAudioNotify:
1005 {
1006 bool audio = msg->what() == kWhatAudioNotify;
1007
Wei Jia88703c32014-08-06 11:24:07 -07001008 int32_t currentDecoderGeneration =
1009 (audio? mAudioDecoderGeneration : mVideoDecoderGeneration);
1010 int32_t requesterGeneration = currentDecoderGeneration - 1;
1011 CHECK(msg->findInt32("generation", &requesterGeneration));
1012
1013 if (requesterGeneration != currentDecoderGeneration) {
1014 ALOGV("got message from old %s decoder, generation(%d:%d)",
1015 audio ? "audio" : "video", requesterGeneration,
1016 currentDecoderGeneration);
1017 sp<AMessage> reply;
1018 if (!(msg->findMessage("reply", &reply))) {
1019 return;
1020 }
1021
1022 reply->setInt32("err", INFO_DISCONTINUITY);
1023 reply->post();
1024 return;
1025 }
1026
Andreas Huberf9334412010-12-15 15:17:42 -08001027 int32_t what;
Lajos Molnar1cd13982014-01-17 15:12:51 -08001028 CHECK(msg->findInt32("what", &what));
Andreas Huberf9334412010-12-15 15:17:42 -08001029
Chong Zhang7137ec72014-11-12 16:41:05 -08001030 if (what == DecoderBase::kWhatInputDiscontinuity) {
1031 int32_t formatChange;
1032 CHECK(msg->findInt32("formatChange", &formatChange));
Andreas Huberf9334412010-12-15 15:17:42 -08001033
Chong Zhang7137ec72014-11-12 16:41:05 -08001034 ALOGV("%s discontinuity: formatChange %d",
1035 audio ? "audio" : "video", formatChange);
1036
1037 if (formatChange) {
1038 mDeferredActions.push_back(
1039 new FlushDecoderAction(
1040 audio ? FLUSH_CMD_SHUTDOWN : FLUSH_CMD_NONE,
1041 audio ? FLUSH_CMD_NONE : FLUSH_CMD_SHUTDOWN));
Andreas Huberf9334412010-12-15 15:17:42 -08001042 }
Chong Zhang7137ec72014-11-12 16:41:05 -08001043
1044 mDeferredActions.push_back(
1045 new SimpleAction(
1046 &NuPlayer::performScanSources));
1047
1048 processDeferredActions();
1049 } else if (what == DecoderBase::kWhatEOS) {
Andreas Huberdc9bacd2011-09-26 10:53:29 -07001050 int32_t err;
Lajos Molnar1cd13982014-01-17 15:12:51 -08001051 CHECK(msg->findInt32("err", &err));
Andreas Huberdc9bacd2011-09-26 10:53:29 -07001052
1053 if (err == ERROR_END_OF_STREAM) {
Steve Block3856b092011-10-20 11:56:00 +01001054 ALOGV("got %s decoder EOS", audio ? "audio" : "video");
Andreas Huberdc9bacd2011-09-26 10:53:29 -07001055 } else {
Steve Block3856b092011-10-20 11:56:00 +01001056 ALOGV("got %s decoder EOS w/ error %d",
Andreas Huberdc9bacd2011-09-26 10:53:29 -07001057 audio ? "audio" : "video",
1058 err);
1059 }
1060
1061 mRenderer->queueEOS(audio, err);
Chong Zhang7137ec72014-11-12 16:41:05 -08001062 } else if (what == DecoderBase::kWhatFlushCompleted) {
Steve Block3856b092011-10-20 11:56:00 +01001063 ALOGV("decoder %s flush completed", audio ? "audio" : "video");
Andreas Huberf9334412010-12-15 15:17:42 -08001064
Andy Hung8d121d42014-10-03 09:53:53 -07001065 handleFlushComplete(audio, true /* isDecoder */);
Andreas Huber3831a062010-12-21 10:22:33 -08001066 finishFlushIfPossible();
Chong Zhang7137ec72014-11-12 16:41:05 -08001067 } else if (what == DecoderBase::kWhatVideoSizeChanged) {
Lajos Molnar1cd13982014-01-17 15:12:51 -08001068 sp<AMessage> format;
1069 CHECK(msg->findMessage("format", &format));
1070
Wei Jiac6cfd702014-11-11 16:33:20 -08001071 sp<AMessage> inputFormat =
1072 mSource->getFormat(false /* audio */);
Andreas Huber3831a062010-12-21 10:22:33 -08001073
Bartosz Dolewski26936f72014-12-11 12:47:08 +01001074 setVideoScalingMode(mVideoScalingMode);
Wei Jiac6cfd702014-11-11 16:33:20 -08001075 updateVideoSize(inputFormat, format);
Chong Zhang7137ec72014-11-12 16:41:05 -08001076 } else if (what == DecoderBase::kWhatShutdownCompleted) {
Steve Block3856b092011-10-20 11:56:00 +01001077 ALOGV("%s shutdown completed", audio ? "audio" : "video");
Andreas Huber3831a062010-12-21 10:22:33 -08001078 if (audio) {
1079 mAudioDecoder.clear();
Wei Jia57568df2014-09-22 10:16:29 -07001080 ++mAudioDecoderGeneration;
Andreas Huber3831a062010-12-21 10:22:33 -08001081
1082 CHECK_EQ((int)mFlushingAudio, (int)SHUTTING_DOWN_DECODER);
1083 mFlushingAudio = SHUT_DOWN;
1084 } else {
1085 mVideoDecoder.clear();
Wei Jia57568df2014-09-22 10:16:29 -07001086 ++mVideoDecoderGeneration;
Andreas Huber3831a062010-12-21 10:22:33 -08001087
1088 CHECK_EQ((int)mFlushingVideo, (int)SHUTTING_DOWN_DECODER);
1089 mFlushingVideo = SHUT_DOWN;
1090 }
1091
1092 finishFlushIfPossible();
Chong Zhangf8d71772014-11-26 15:08:34 -08001093 } else if (what == DecoderBase::kWhatResumeCompleted) {
1094 finishResume();
Chong Zhang7137ec72014-11-12 16:41:05 -08001095 } else if (what == DecoderBase::kWhatError) {
Chong Zhangf4c0a942014-08-11 15:14:10 -07001096 status_t err;
Andy Hung2abde2c2014-09-30 14:40:32 -07001097 if (!msg->findInt32("err", &err) || err == OK) {
Chong Zhangf4c0a942014-08-11 15:14:10 -07001098 err = UNKNOWN_ERROR;
1099 }
Andy Hungcf31f1e2014-09-23 14:59:01 -07001100
Andy Hung2abde2c2014-09-30 14:40:32 -07001101 // Decoder errors can be due to Source (e.g. from streaming),
1102 // or from decoding corrupted bitstreams, or from other decoder
1103 // MediaCodec operations (e.g. from an ongoing reset or seek).
Andy Hung202bce12014-12-03 11:47:36 -08001104 // They may also be due to openAudioSink failure at
1105 // decoder start or after a format change.
Andy Hung2abde2c2014-09-30 14:40:32 -07001106 //
1107 // We try to gracefully shut down the affected decoder if possible,
1108 // rather than trying to force the shutdown with something
1109 // similar to performReset(). This method can lead to a hang
1110 // if MediaCodec functions block after an error, but they should
1111 // typically return INVALID_OPERATION instead of blocking.
1112
1113 FlushStatus *flushing = audio ? &mFlushingAudio : &mFlushingVideo;
1114 ALOGE("received error(%#x) from %s decoder, flushing(%d), now shutting down",
1115 err, audio ? "audio" : "video", *flushing);
1116
1117 switch (*flushing) {
1118 case NONE:
1119 mDeferredActions.push_back(
Wei Jiafef808d2014-10-31 17:57:05 -07001120 new FlushDecoderAction(
1121 audio ? FLUSH_CMD_SHUTDOWN : FLUSH_CMD_NONE,
1122 audio ? FLUSH_CMD_NONE : FLUSH_CMD_SHUTDOWN));
Andy Hung2abde2c2014-09-30 14:40:32 -07001123 processDeferredActions();
1124 break;
1125 case FLUSHING_DECODER:
1126 *flushing = FLUSHING_DECODER_SHUTDOWN; // initiate shutdown after flush.
1127 break; // Wait for flush to complete.
1128 case FLUSHING_DECODER_SHUTDOWN:
1129 break; // Wait for flush to complete.
1130 case SHUTTING_DOWN_DECODER:
1131 break; // Wait for shutdown to complete.
1132 case FLUSHED:
1133 // Widevine source reads must stop before releasing the video decoder.
1134 if (!audio && mSource != NULL && mSourceFlags & Source::FLAG_SECURE) {
1135 mSource->stop();
Robert Shih0c61a0d2015-07-06 15:09:10 -07001136 mSourceStarted = false;
Andy Hung2abde2c2014-09-30 14:40:32 -07001137 }
1138 getDecoder(audio)->initiateShutdown(); // In the middle of a seek.
1139 *flushing = SHUTTING_DOWN_DECODER; // Shut down.
1140 break;
1141 case SHUT_DOWN:
1142 finishFlushIfPossible(); // Should not occur.
1143 break; // Finish anyways.
Marco Nelissen9e2b7912014-08-18 16:13:03 -07001144 }
Andy Hung2abde2c2014-09-30 14:40:32 -07001145 notifyListener(MEDIA_ERROR, MEDIA_ERROR_UNKNOWN, err);
Lajos Molnar1cd13982014-01-17 15:12:51 -08001146 } else {
1147 ALOGV("Unhandled decoder notification %d '%c%c%c%c'.",
Andreas Hubera1f8ab02012-11-30 10:53:22 -08001148 what,
1149 what >> 24,
1150 (what >> 16) & 0xff,
1151 (what >> 8) & 0xff,
1152 what & 0xff);
Andreas Huberf9334412010-12-15 15:17:42 -08001153 }
1154
1155 break;
1156 }
1157
1158 case kWhatRendererNotify:
1159 {
Wei Jia57568df2014-09-22 10:16:29 -07001160 int32_t requesterGeneration = mRendererGeneration - 1;
1161 CHECK(msg->findInt32("generation", &requesterGeneration));
1162 if (requesterGeneration != mRendererGeneration) {
1163 ALOGV("got message from old renderer, generation(%d:%d)",
1164 requesterGeneration, mRendererGeneration);
1165 return;
1166 }
1167
Andreas Huberf9334412010-12-15 15:17:42 -08001168 int32_t what;
1169 CHECK(msg->findInt32("what", &what));
1170
1171 if (what == Renderer::kWhatEOS) {
1172 int32_t audio;
1173 CHECK(msg->findInt32("audio", &audio));
1174
Andreas Huberc92fd242011-08-16 13:48:44 -07001175 int32_t finalResult;
1176 CHECK(msg->findInt32("finalResult", &finalResult));
1177
Andreas Huberf9334412010-12-15 15:17:42 -08001178 if (audio) {
1179 mAudioEOS = true;
1180 } else {
1181 mVideoEOS = true;
1182 }
1183
Andreas Huberc92fd242011-08-16 13:48:44 -07001184 if (finalResult == ERROR_END_OF_STREAM) {
Steve Block3856b092011-10-20 11:56:00 +01001185 ALOGV("reached %s EOS", audio ? "audio" : "video");
Andreas Huberc92fd242011-08-16 13:48:44 -07001186 } else {
Steve Block29357bc2012-01-06 19:20:56 +00001187 ALOGE("%s track encountered an error (%d)",
Andreas Huberc92fd242011-08-16 13:48:44 -07001188 audio ? "audio" : "video", finalResult);
1189
1190 notifyListener(
1191 MEDIA_ERROR, MEDIA_ERROR_UNKNOWN, finalResult);
1192 }
Andreas Huberf9334412010-12-15 15:17:42 -08001193
1194 if ((mAudioEOS || mAudioDecoder == NULL)
1195 && (mVideoEOS || mVideoDecoder == NULL)) {
1196 notifyListener(MEDIA_PLAYBACK_COMPLETE, 0, 0);
1197 }
Andreas Huber3fe62152011-09-16 15:09:22 -07001198 } else if (what == Renderer::kWhatFlushComplete) {
Andreas Huberf9334412010-12-15 15:17:42 -08001199 int32_t audio;
1200 CHECK(msg->findInt32("audio", &audio));
1201
Wei Jia3261f0d2015-10-08 13:58:33 -07001202 if (audio) {
1203 mAudioEOS = false;
1204 } else {
1205 mVideoEOS = false;
1206 }
1207
Steve Block3856b092011-10-20 11:56:00 +01001208 ALOGV("renderer %s flush completed.", audio ? "audio" : "video");
Wei Jiada4252f2015-07-14 18:15:28 -07001209 if (audio && (mFlushingAudio == NONE || mFlushingAudio == FLUSHED
1210 || mFlushingAudio == SHUT_DOWN)) {
1211 // Flush has been handled by tear down.
1212 break;
1213 }
Andy Hung8d121d42014-10-03 09:53:53 -07001214 handleFlushComplete(audio, false /* isDecoder */);
1215 finishFlushIfPossible();
James Dongf57b4ea2012-07-20 13:38:36 -07001216 } else if (what == Renderer::kWhatVideoRenderingStart) {
1217 notifyListener(MEDIA_INFO, MEDIA_INFO_RENDERING_START, 0);
Lajos Molnarcbaffcf2013-08-14 18:30:38 -07001218 } else if (what == Renderer::kWhatMediaRenderingStart) {
1219 ALOGV("media rendering started");
1220 notifyListener(MEDIA_STARTED, 0, 0);
Ronghua Wufaeb0f22015-05-21 12:20:21 -07001221 } else if (what == Renderer::kWhatAudioTearDown) {
Ronghua Wu08529172014-10-02 16:55:52 -07001222 int32_t reason;
1223 CHECK(msg->findInt32("reason", &reason));
Ronghua Wufaeb0f22015-05-21 12:20:21 -07001224 ALOGV("Tear down audio with reason %d.", reason);
Wei Jia8456ddd2016-04-22 14:46:43 -07001225 if (reason == Renderer::kDueToTimeout && !(mPaused && mOffloadAudio)) {
1226 // TimeoutWhenPaused is only for offload mode.
1227 ALOGW("Receive a stale message for teardown.");
1228 break;
1229 }
Wei Jiada4252f2015-07-14 18:15:28 -07001230 int64_t positionUs;
Robert Shih1a5c8592015-08-04 18:07:44 -07001231 if (!msg->findInt64("positionUs", &positionUs)) {
1232 positionUs = mPreviousSeekTimeUs;
1233 }
Ronghua Wufaeb0f22015-05-21 12:20:21 -07001234
Wei Jia5031b2f2016-02-25 11:19:31 -08001235 restartAudio(
Wei Jiaa05f1e32016-03-25 16:31:22 -07001236 positionUs, reason == Renderer::kForceNonOffload /* forceNonOffload */,
1237 reason != Renderer::kDueToTimeout /* needsToCreateAudioDecoder */);
Andreas Huberf9334412010-12-15 15:17:42 -08001238 }
1239 break;
1240 }
1241
1242 case kWhatMoreDataQueued:
1243 {
1244 break;
1245 }
1246
Andreas Huber1aef2112011-01-04 14:01:29 -08001247 case kWhatReset:
1248 {
Steve Block3856b092011-10-20 11:56:00 +01001249 ALOGV("kWhatReset");
Andreas Huber1aef2112011-01-04 14:01:29 -08001250
Ronghua Wu64c2d172015-10-07 16:52:19 -07001251 mResetting = true;
1252
Andreas Hubera1f8ab02012-11-30 10:53:22 -08001253 mDeferredActions.push_back(
Wei Jiafef808d2014-10-31 17:57:05 -07001254 new FlushDecoderAction(
1255 FLUSH_CMD_SHUTDOWN /* audio */,
1256 FLUSH_CMD_SHUTDOWN /* video */));
Andreas Huberb7c8e912012-11-27 15:02:53 -08001257
Andreas Hubera1f8ab02012-11-30 10:53:22 -08001258 mDeferredActions.push_back(
1259 new SimpleAction(&NuPlayer::performReset));
Andreas Huberb58ce9f2011-11-28 16:27:35 -08001260
Andreas Hubera1f8ab02012-11-30 10:53:22 -08001261 processDeferredActions();
Andreas Huber1aef2112011-01-04 14:01:29 -08001262 break;
1263 }
1264
Andreas Huber43c3e6c2011-01-05 12:17:08 -08001265 case kWhatSeek:
1266 {
1267 int64_t seekTimeUs;
Wei Jiac5de0912016-11-18 10:22:14 -08001268 int32_t mode;
Wei Jiae427abf2014-09-22 15:21:11 -07001269 int32_t needNotify;
Andreas Huber43c3e6c2011-01-05 12:17:08 -08001270 CHECK(msg->findInt64("seekTimeUs", &seekTimeUs));
Wei Jiac5de0912016-11-18 10:22:14 -08001271 CHECK(msg->findInt32("mode", &mode));
Wei Jiae427abf2014-09-22 15:21:11 -07001272 CHECK(msg->findInt32("needNotify", &needNotify));
Andreas Huber43c3e6c2011-01-05 12:17:08 -08001273
Wei Jiac5de0912016-11-18 10:22:14 -08001274 ALOGV("kWhatSeek seekTimeUs=%lld us, mode=%d, needNotify=%d",
1275 (long long)seekTimeUs, mode, needNotify);
Andreas Huber43c3e6c2011-01-05 12:17:08 -08001276
Wei Jia1061c9c2015-05-19 16:02:17 -07001277 if (!mStarted) {
1278 // Seek before the player is started. In order to preview video,
1279 // need to start the player and pause it. This branch is called
1280 // only once if needed. After the player is started, any seek
1281 // operation will go through normal path.
Robert Shih0c61a0d2015-07-06 15:09:10 -07001282 // Audio-only cases are handled separately.
Wei Jiac5de0912016-11-18 10:22:14 -08001283 onStart(seekTimeUs, (MediaPlayerSeekMode)mode);
Robert Shih0c61a0d2015-07-06 15:09:10 -07001284 if (mStarted) {
1285 onPause();
1286 mPausedByClient = true;
1287 }
Wei Jia1061c9c2015-05-19 16:02:17 -07001288 if (needNotify) {
1289 notifyDriverSeekComplete();
1290 }
1291 break;
1292 }
1293
Andreas Hubera1f8ab02012-11-30 10:53:22 -08001294 mDeferredActions.push_back(
Wei Jiafef808d2014-10-31 17:57:05 -07001295 new FlushDecoderAction(FLUSH_CMD_FLUSH /* audio */,
1296 FLUSH_CMD_FLUSH /* video */));
Andreas Huber43c3e6c2011-01-05 12:17:08 -08001297
Wei Jiae427abf2014-09-22 15:21:11 -07001298 mDeferredActions.push_back(
Wei Jiac5de0912016-11-18 10:22:14 -08001299 new SeekAction(seekTimeUs, (MediaPlayerSeekMode)mode));
Andreas Huber43c3e6c2011-01-05 12:17:08 -08001300
Chong Zhangf8d71772014-11-26 15:08:34 -08001301 // After a flush without shutdown, decoder is paused.
1302 // Don't resume it until source seek is done, otherwise it could
Chong Zhang7137ec72014-11-12 16:41:05 -08001303 // start pulling stale data too soon.
1304 mDeferredActions.push_back(
Chong Zhangf8d71772014-11-26 15:08:34 -08001305 new ResumeDecoderAction(needNotify));
Chong Zhang7137ec72014-11-12 16:41:05 -08001306
Andreas Hubera1f8ab02012-11-30 10:53:22 -08001307 processDeferredActions();
Andreas Huber43c3e6c2011-01-05 12:17:08 -08001308 break;
1309 }
1310
Andreas Huberb4082222011-01-20 15:23:04 -08001311 case kWhatPause:
1312 {
Chong Zhangefbb6192015-01-30 17:13:27 -08001313 onPause();
1314 mPausedByClient = true;
Andreas Huberb4082222011-01-20 15:23:04 -08001315 break;
1316 }
1317
Andreas Huberb5f25f02013-02-05 10:14:26 -08001318 case kWhatSourceNotify:
1319 {
Andreas Huber9575c962013-02-05 13:59:56 -08001320 onSourceNotify(msg);
Andreas Huberb5f25f02013-02-05 10:14:26 -08001321 break;
1322 }
1323
Chong Zhanga7fa1d92014-06-11 14:49:23 -07001324 case kWhatClosedCaptionNotify:
1325 {
1326 onClosedCaptionNotify(msg);
1327 break;
1328 }
1329
Andreas Huberf9334412010-12-15 15:17:42 -08001330 default:
1331 TRESPASS();
1332 break;
1333 }
1334}
1335
Wei Jia94211742014-10-28 17:09:06 -07001336void NuPlayer::onResume() {
Ronghua Wu64c2d172015-10-07 16:52:19 -07001337 if (!mPaused || mResetting) {
1338 ALOGD_IF(mResetting, "resetting, onResume discarded");
Chong Zhangefbb6192015-01-30 17:13:27 -08001339 return;
1340 }
1341 mPaused = false;
Wei Jia94211742014-10-28 17:09:06 -07001342 if (mSource != NULL) {
1343 mSource->resume();
1344 } else {
1345 ALOGW("resume called when source is gone or not set");
1346 }
1347 // |mAudioDecoder| may have been released due to the pause timeout, so re-create it if
1348 // needed.
1349 if (audioDecoderStillNeeded() && mAudioDecoder == NULL) {
1350 instantiateDecoder(true /* audio */, &mAudioDecoder);
1351 }
1352 if (mRenderer != NULL) {
1353 mRenderer->resume();
1354 } else {
1355 ALOGW("resume called when renderer is gone or not set");
1356 }
1357}
1358
Lajos Molnarfcd3e942015-03-31 10:06:48 -07001359status_t NuPlayer::onInstantiateSecureDecoders() {
1360 status_t err;
1361 if (!(mSourceFlags & Source::FLAG_SECURE)) {
1362 return BAD_TYPE;
1363 }
1364
1365 if (mRenderer != NULL) {
1366 ALOGE("renderer should not be set when instantiating secure decoders");
1367 return UNKNOWN_ERROR;
1368 }
1369
1370 // TRICKY: We rely on mRenderer being null, so that decoder does not start requesting
1371 // data on instantiation.
Lajos Molnar1de1e252015-04-30 18:18:34 -07001372 if (mSurface != NULL) {
Lajos Molnarfcd3e942015-03-31 10:06:48 -07001373 err = instantiateDecoder(false, &mVideoDecoder);
1374 if (err != OK) {
1375 return err;
1376 }
1377 }
1378
1379 if (mAudioSink != NULL) {
1380 err = instantiateDecoder(true, &mAudioDecoder);
1381 if (err != OK) {
1382 return err;
1383 }
1384 }
1385 return OK;
1386}
1387
Wei Jiac5de0912016-11-18 10:22:14 -08001388void NuPlayer::onStart(int64_t startPositionUs, MediaPlayerSeekMode mode) {
Robert Shih0c61a0d2015-07-06 15:09:10 -07001389 if (!mSourceStarted) {
1390 mSourceStarted = true;
1391 mSource->start();
1392 }
1393 if (startPositionUs > 0) {
Wei Jiac5de0912016-11-18 10:22:14 -08001394 performSeek(startPositionUs, mode);
Robert Shih0c61a0d2015-07-06 15:09:10 -07001395 if (mSource->getFormat(false /* audio */) == NULL) {
1396 return;
1397 }
1398 }
1399
Wei Jia94211742014-10-28 17:09:06 -07001400 mOffloadAudio = false;
1401 mAudioEOS = false;
1402 mVideoEOS = false;
Wei Jia94211742014-10-28 17:09:06 -07001403 mStarted = true;
Wei Jiafe8fe7d2016-06-08 10:29:25 -07001404 mPaused = false;
Wei Jia94211742014-10-28 17:09:06 -07001405
Wei Jia94211742014-10-28 17:09:06 -07001406 uint32_t flags = 0;
1407
1408 if (mSource->isRealTime()) {
1409 flags |= Renderer::FLAG_REAL_TIME;
1410 }
1411
1412 sp<MetaData> audioMeta = mSource->getFormatMeta(true /* audio */);
Wei Jia1de83d52016-10-10 10:15:03 -07001413 sp<MetaData> videoMeta = mSource->getFormatMeta(false /* audio */);
1414 if (audioMeta == NULL && videoMeta == NULL) {
1415 ALOGE("no metadata for either audio or video source");
1416 mSource->stop();
1417 mSourceStarted = false;
1418 notifyListener(MEDIA_ERROR, MEDIA_ERROR_UNKNOWN, ERROR_MALFORMED);
1419 return;
1420 }
Andy Hung12a84132015-10-20 16:09:21 -07001421 ALOGV_IF(audioMeta == NULL, "no metadata for audio source"); // video only stream
Wei Jia1de83d52016-10-10 10:15:03 -07001422
Wei Jia94211742014-10-28 17:09:06 -07001423 audio_stream_type_t streamType = AUDIO_STREAM_MUSIC;
1424 if (mAudioSink != NULL) {
1425 streamType = mAudioSink->getAudioStreamType();
1426 }
1427
1428 sp<AMessage> videoFormat = mSource->getFormat(false /* audio */);
1429
1430 mOffloadAudio =
Wei Jiabf70feb2016-02-19 15:47:16 -08001431 canOffloadStream(audioMeta, (videoFormat != NULL), mSource->isStreaming(), streamType)
1432 && (mPlaybackSettings.mSpeed == 1.f && mPlaybackSettings.mPitch == 1.f);
Wei Jia94211742014-10-28 17:09:06 -07001433 if (mOffloadAudio) {
1434 flags |= Renderer::FLAG_OFFLOAD_AUDIO;
1435 }
1436
Lajos Molnar1d15ab52015-03-04 16:46:34 -08001437 sp<AMessage> notify = new AMessage(kWhatRendererNotify, this);
Wei Jia94211742014-10-28 17:09:06 -07001438 ++mRendererGeneration;
1439 notify->setInt32("generation", mRendererGeneration);
1440 mRenderer = new Renderer(mAudioSink, notify, flags);
Wei Jia94211742014-10-28 17:09:06 -07001441 mRendererLooper = new ALooper;
1442 mRendererLooper->setName("NuPlayerRenderer");
1443 mRendererLooper->start(false, false, ANDROID_PRIORITY_AUDIO);
1444 mRendererLooper->registerHandler(mRenderer);
Lajos Molnar3a474aa2015-04-24 17:10:07 -07001445
1446 status_t err = mRenderer->setPlaybackSettings(mPlaybackSettings);
1447 if (err != OK) {
1448 mSource->stop();
Robert Shih0c61a0d2015-07-06 15:09:10 -07001449 mSourceStarted = false;
Lajos Molnar3a474aa2015-04-24 17:10:07 -07001450 notifyListener(MEDIA_ERROR, MEDIA_ERROR_UNKNOWN, err);
1451 return;
Wei Jiac8206ff2015-03-04 13:59:37 -08001452 }
Wei Jia94211742014-10-28 17:09:06 -07001453
Ronghua Wuc8a70d32015-04-29 16:26:34 -07001454 float rate = getFrameRate();
1455 if (rate > 0) {
Wei Jia94211742014-10-28 17:09:06 -07001456 mRenderer->setVideoFrameRate(rate);
1457 }
1458
Wei Jiac6cfd702014-11-11 16:33:20 -08001459 if (mVideoDecoder != NULL) {
1460 mVideoDecoder->setRenderer(mRenderer);
1461 }
1462 if (mAudioDecoder != NULL) {
1463 mAudioDecoder->setRenderer(mRenderer);
1464 }
1465
Wei Jia94211742014-10-28 17:09:06 -07001466 postScanSources();
1467}
1468
Chong Zhangefbb6192015-01-30 17:13:27 -08001469void NuPlayer::onPause() {
1470 if (mPaused) {
1471 return;
1472 }
1473 mPaused = true;
1474 if (mSource != NULL) {
1475 mSource->pause();
1476 } else {
1477 ALOGW("pause called when source is gone or not set");
1478 }
1479 if (mRenderer != NULL) {
1480 mRenderer->pause();
1481 } else {
1482 ALOGW("pause called when renderer is gone or not set");
1483 }
1484}
1485
Ronghua Wud7988b12014-10-03 15:19:10 -07001486bool NuPlayer::audioDecoderStillNeeded() {
1487 // Audio decoder is no longer needed if it's in shut/shutting down status.
1488 return ((mFlushingAudio != SHUT_DOWN) && (mFlushingAudio != SHUTTING_DOWN_DECODER));
1489}
1490
Andy Hung8d121d42014-10-03 09:53:53 -07001491void NuPlayer::handleFlushComplete(bool audio, bool isDecoder) {
1492 // We wait for both the decoder flush and the renderer flush to complete
1493 // before entering either the FLUSHED or the SHUTTING_DOWN_DECODER state.
1494
1495 mFlushComplete[audio][isDecoder] = true;
1496 if (!mFlushComplete[audio][!isDecoder]) {
1497 return;
1498 }
1499
1500 FlushStatus *state = audio ? &mFlushingAudio : &mFlushingVideo;
1501 switch (*state) {
1502 case FLUSHING_DECODER:
1503 {
1504 *state = FLUSHED;
Andy Hung8d121d42014-10-03 09:53:53 -07001505 break;
1506 }
1507
1508 case FLUSHING_DECODER_SHUTDOWN:
1509 {
1510 *state = SHUTTING_DOWN_DECODER;
1511
1512 ALOGV("initiating %s decoder shutdown", audio ? "audio" : "video");
1513 if (!audio) {
Andy Hung8d121d42014-10-03 09:53:53 -07001514 // Widevine source reads must stop before releasing the video decoder.
1515 if (mSource != NULL && mSourceFlags & Source::FLAG_SECURE) {
1516 mSource->stop();
Robert Shih0c61a0d2015-07-06 15:09:10 -07001517 mSourceStarted = false;
Andy Hung8d121d42014-10-03 09:53:53 -07001518 }
1519 }
1520 getDecoder(audio)->initiateShutdown();
1521 break;
1522 }
1523
1524 default:
1525 // decoder flush completes only occur in a flushing state.
1526 LOG_ALWAYS_FATAL_IF(isDecoder, "decoder flush in invalid state %d", *state);
1527 break;
1528 }
1529}
1530
Andreas Huber3831a062010-12-21 10:22:33 -08001531void NuPlayer::finishFlushIfPossible() {
Wei Jia53904f32014-07-29 10:22:53 -07001532 if (mFlushingAudio != NONE && mFlushingAudio != FLUSHED
1533 && mFlushingAudio != SHUT_DOWN) {
Andreas Huber3831a062010-12-21 10:22:33 -08001534 return;
1535 }
1536
Wei Jia53904f32014-07-29 10:22:53 -07001537 if (mFlushingVideo != NONE && mFlushingVideo != FLUSHED
1538 && mFlushingVideo != SHUT_DOWN) {
Andreas Huber3831a062010-12-21 10:22:33 -08001539 return;
1540 }
1541
Steve Block3856b092011-10-20 11:56:00 +01001542 ALOGV("both audio and video are flushed now.");
Andreas Huber3831a062010-12-21 10:22:33 -08001543
Andreas Huber3831a062010-12-21 10:22:33 -08001544 mFlushingAudio = NONE;
1545 mFlushingVideo = NONE;
Andreas Huber3831a062010-12-21 10:22:33 -08001546
Andy Hung8d121d42014-10-03 09:53:53 -07001547 clearFlushComplete();
1548
Andreas Hubera1f8ab02012-11-30 10:53:22 -08001549 processDeferredActions();
Andreas Huber1aef2112011-01-04 14:01:29 -08001550}
1551
1552void NuPlayer::postScanSources() {
1553 if (mScanSourcesPending) {
1554 return;
1555 }
1556
Lajos Molnar1d15ab52015-03-04 16:46:34 -08001557 sp<AMessage> msg = new AMessage(kWhatScanSources, this);
Andreas Huber1aef2112011-01-04 14:01:29 -08001558 msg->setInt32("generation", mScanSourcesGeneration);
1559 msg->post();
1560
1561 mScanSourcesPending = true;
1562}
1563
Wei Jia41cd4632016-05-13 10:53:30 -07001564void NuPlayer::tryOpenAudioSinkForOffload(
1565 const sp<AMessage> &format, const sp<MetaData> &audioMeta, bool hasVideo) {
Andy Hung202bce12014-12-03 11:47:36 -08001566 // Note: This is called early in NuPlayer to determine whether offloading
1567 // is possible; otherwise the decoders call the renderer openAudioSink directly.
Andy Hung282a7e32014-08-14 15:56:34 -07001568
Andy Hung202bce12014-12-03 11:47:36 -08001569 status_t err = mRenderer->openAudioSink(
1570 format, true /* offloadOnly */, hasVideo, AUDIO_OUTPUT_FLAG_NONE, &mOffloadAudio);
1571 if (err != OK) {
1572 // Any failure we turn off mOffloadAudio.
1573 mOffloadAudio = false;
1574 } else if (mOffloadAudio) {
Chong Zhang3b9eb1f2014-10-15 17:05:08 -07001575 sendMetaDataToHal(mAudioSink, audioMeta);
Andy Hung282a7e32014-08-14 15:56:34 -07001576 }
1577}
1578
1579void NuPlayer::closeAudioSink() {
Chong Zhang3b9eb1f2014-10-15 17:05:08 -07001580 mRenderer->closeAudioSink();
Andy Hung282a7e32014-08-14 15:56:34 -07001581}
1582
Wei Jia5031b2f2016-02-25 11:19:31 -08001583void NuPlayer::restartAudio(
Wei Jiabf70feb2016-02-19 15:47:16 -08001584 int64_t currentPositionUs, bool forceNonOffload, bool needsToCreateAudioDecoder) {
Wei Jiaa05f1e32016-03-25 16:31:22 -07001585 if (mAudioDecoder != NULL) {
1586 mAudioDecoder->pause();
1587 mAudioDecoder.clear();
1588 ++mAudioDecoderGeneration;
1589 }
Wei Jiabf70feb2016-02-19 15:47:16 -08001590 if (mFlushingAudio == FLUSHING_DECODER) {
1591 mFlushComplete[1 /* audio */][1 /* isDecoder */] = true;
1592 mFlushingAudio = FLUSHED;
1593 finishFlushIfPossible();
1594 } else if (mFlushingAudio == FLUSHING_DECODER_SHUTDOWN
1595 || mFlushingAudio == SHUTTING_DOWN_DECODER) {
1596 mFlushComplete[1 /* audio */][1 /* isDecoder */] = true;
1597 mFlushingAudio = SHUT_DOWN;
1598 finishFlushIfPossible();
1599 needsToCreateAudioDecoder = false;
1600 }
1601 if (mRenderer == NULL) {
1602 return;
1603 }
1604 closeAudioSink();
1605 mRenderer->flush(true /* audio */, false /* notifyComplete */);
1606 if (mVideoDecoder != NULL) {
1607 mRenderer->flush(false /* audio */, false /* notifyComplete */);
1608 }
1609
Wei Jiac5de0912016-11-18 10:22:14 -08001610 performSeek(currentPositionUs, MediaPlayerSeekMode::SEEK_PREVIOUS_SYNC /* mode */);
Wei Jiabf70feb2016-02-19 15:47:16 -08001611
1612 if (forceNonOffload) {
1613 mRenderer->signalDisableOffloadAudio();
1614 mOffloadAudio = false;
1615 }
1616 if (needsToCreateAudioDecoder) {
Wei Jiaa05f1e32016-03-25 16:31:22 -07001617 instantiateDecoder(true /* audio */, &mAudioDecoder, !forceNonOffload);
Wei Jiabf70feb2016-02-19 15:47:16 -08001618 }
1619}
1620
Wei Jia41cd4632016-05-13 10:53:30 -07001621void NuPlayer::determineAudioModeChange(const sp<AMessage> &audioFormat) {
Wei Jiae4d18c72015-07-13 17:58:11 -07001622 if (mSource == NULL || mAudioSink == NULL) {
1623 return;
1624 }
1625
1626 if (mRenderer == NULL) {
1627 ALOGW("No renderer can be used to determine audio mode. Use non-offload for safety.");
1628 mOffloadAudio = false;
1629 return;
1630 }
1631
1632 sp<MetaData> audioMeta = mSource->getFormatMeta(true /* audio */);
1633 sp<AMessage> videoFormat = mSource->getFormat(false /* audio */);
1634 audio_stream_type_t streamType = mAudioSink->getAudioStreamType();
1635 const bool hasVideo = (videoFormat != NULL);
1636 const bool canOffload = canOffloadStream(
Wei Jiabf70feb2016-02-19 15:47:16 -08001637 audioMeta, hasVideo, mSource->isStreaming(), streamType)
1638 && (mPlaybackSettings.mSpeed == 1.f && mPlaybackSettings.mPitch == 1.f);
Wei Jiae4d18c72015-07-13 17:58:11 -07001639 if (canOffload) {
1640 if (!mOffloadAudio) {
1641 mRenderer->signalEnableOffloadAudio();
1642 }
1643 // open audio sink early under offload mode.
Wei Jia41cd4632016-05-13 10:53:30 -07001644 tryOpenAudioSinkForOffload(audioFormat, audioMeta, hasVideo);
Wei Jiae4d18c72015-07-13 17:58:11 -07001645 } else {
Robert Shihe1d70192015-07-23 17:54:13 -07001646 if (mOffloadAudio) {
1647 mRenderer->signalDisableOffloadAudio();
1648 mOffloadAudio = false;
1649 }
Wei Jiae4d18c72015-07-13 17:58:11 -07001650 }
1651}
1652
Wei Jiaa05f1e32016-03-25 16:31:22 -07001653status_t NuPlayer::instantiateDecoder(
1654 bool audio, sp<DecoderBase> *decoder, bool checkAudioModeChange) {
Wei Jia566da802015-08-27 13:59:30 -07001655 // The audio decoder could be cleared by tear down. If still in shut down
1656 // process, no need to create a new audio decoder.
1657 if (*decoder != NULL || (audio && mFlushingAudio == SHUT_DOWN)) {
Andreas Huberf9334412010-12-15 15:17:42 -08001658 return OK;
1659 }
1660
Andreas Huber84066782011-08-16 09:34:26 -07001661 sp<AMessage> format = mSource->getFormat(audio);
Andreas Huberf9334412010-12-15 15:17:42 -08001662
Andreas Huber84066782011-08-16 09:34:26 -07001663 if (format == NULL) {
Robert Shih7350b052015-10-01 15:50:14 -07001664 return UNKNOWN_ERROR;
1665 } else {
1666 status_t err;
1667 if (format->findInt32("err", &err) && err) {
1668 return err;
1669 }
Andreas Huberf9334412010-12-15 15:17:42 -08001670 }
1671
Ronghua Wu8db88132015-04-22 13:51:35 -07001672 format->setInt32("priority", 0 /* realtime */);
1673
Andreas Huber3fe62152011-09-16 15:09:22 -07001674 if (!audio) {
Andreas Huber84066782011-08-16 09:34:26 -07001675 AString mime;
1676 CHECK(format->findString("mime", &mime));
Chong Zhanga7fa1d92014-06-11 14:49:23 -07001677
Lajos Molnar1d15ab52015-03-04 16:46:34 -08001678 sp<AMessage> ccNotify = new AMessage(kWhatClosedCaptionNotify, this);
Chong Zhang341ab6e2015-02-04 13:37:18 -08001679 if (mCCDecoder == NULL) {
1680 mCCDecoder = new CCDecoder(ccNotify);
1681 }
Lajos Molnar09524832014-07-17 14:29:51 -07001682
1683 if (mSourceFlags & Source::FLAG_SECURE) {
1684 format->setInt32("secure", true);
1685 }
Chong Zhang17134602015-01-07 16:14:34 -08001686
1687 if (mSourceFlags & Source::FLAG_PROTECTED) {
1688 format->setInt32("protected", true);
1689 }
Ronghua Wu8db88132015-04-22 13:51:35 -07001690
Ronghua Wuc8a70d32015-04-29 16:26:34 -07001691 float rate = getFrameRate();
1692 if (rate > 0) {
Lajos Molnar3a474aa2015-04-24 17:10:07 -07001693 format->setFloat("operating-rate", rate * mPlaybackSettings.mSpeed);
Ronghua Wu8db88132015-04-22 13:51:35 -07001694 }
Andreas Huber3fe62152011-09-16 15:09:22 -07001695 }
1696
Wei Jiabc2fb722014-07-08 16:37:57 -07001697 if (audio) {
Lajos Molnar1d15ab52015-03-04 16:46:34 -08001698 sp<AMessage> notify = new AMessage(kWhatAudioNotify, this);
Wei Jia88703c32014-08-06 11:24:07 -07001699 ++mAudioDecoderGeneration;
1700 notify->setInt32("generation", mAudioDecoderGeneration);
1701
Wei Jiaa05f1e32016-03-25 16:31:22 -07001702 if (checkAudioModeChange) {
Wei Jia41cd4632016-05-13 10:53:30 -07001703 determineAudioModeChange(format);
Wei Jiaa05f1e32016-03-25 16:31:22 -07001704 }
Wei Jiabc2fb722014-07-08 16:37:57 -07001705 if (mOffloadAudio) {
Wei Jia14532f22015-12-29 11:28:15 -08001706 mSource->setOffloadAudio(true /* offload */);
1707
Haynes Mathew George8b635332015-03-30 17:59:47 -07001708 const bool hasVideo = (mSource->getFormat(false /*audio */) != NULL);
1709 format->setInt32("has-video", hasVideo);
Wei Jiac6cfd702014-11-11 16:33:20 -08001710 *decoder = new DecoderPassThrough(notify, mSource, mRenderer);
Wei Jiabc2fb722014-07-08 16:37:57 -07001711 } else {
Wei Jia14532f22015-12-29 11:28:15 -08001712 mSource->setOffloadAudio(false /* offload */);
1713
Wei Jiaf2ae3e12016-10-27 17:10:59 -07001714 *decoder = new Decoder(notify, mSource, mPID, mUID, mRenderer);
Wei Jiabc2fb722014-07-08 16:37:57 -07001715 }
1716 } else {
Lajos Molnar1d15ab52015-03-04 16:46:34 -08001717 sp<AMessage> notify = new AMessage(kWhatVideoNotify, this);
Wei Jia88703c32014-08-06 11:24:07 -07001718 ++mVideoDecoderGeneration;
1719 notify->setInt32("generation", mVideoDecoderGeneration);
1720
Chong Zhang7137ec72014-11-12 16:41:05 -08001721 *decoder = new Decoder(
Wei Jiaf2ae3e12016-10-27 17:10:59 -07001722 notify, mSource, mPID, mUID, mRenderer, mSurface, mCCDecoder);
Lajos Molnard9fd6312014-11-06 11:00:00 -08001723
1724 // enable FRC if high-quality AV sync is requested, even if not
Lajos Molnar1de1e252015-04-30 18:18:34 -07001725 // directly queuing to display, as this will even improve textureview
Lajos Molnard9fd6312014-11-06 11:00:00 -08001726 // playback.
1727 {
Marco Nelissen96626b72016-12-01 13:58:59 -08001728 if (property_get_bool("persist.sys.media.avsync", false)) {
Lajos Molnard9fd6312014-11-06 11:00:00 -08001729 format->setInt32("auto-frc", 1);
1730 }
1731 }
Wei Jiabc2fb722014-07-08 16:37:57 -07001732 }
Lajos Molnar1cd13982014-01-17 15:12:51 -08001733 (*decoder)->init();
Andreas Huber84066782011-08-16 09:34:26 -07001734 (*decoder)->configure(format);
Andreas Huberf9334412010-12-15 15:17:42 -08001735
Praveen Chavanbbaa1442016-04-08 13:33:49 -07001736 if (!audio) {
1737 sp<AMessage> params = new AMessage();
1738 float rate = getFrameRate();
1739 if (rate > 0) {
1740 params->setFloat("frame-rate-total", rate);
1741 }
1742
1743 sp<MetaData> fileMeta = getFileMeta();
1744 if (fileMeta != NULL) {
1745 int32_t videoTemporalLayerCount;
1746 if (fileMeta->findInt32(kKeyTemporalLayerCount, &videoTemporalLayerCount)
1747 && videoTemporalLayerCount > 0) {
1748 params->setInt32("temporal-layer-count", videoTemporalLayerCount);
1749 }
1750 }
1751
1752 if (params->countEntries() > 0) {
1753 (*decoder)->setParameters(params);
1754 }
1755 }
Andreas Huberf9334412010-12-15 15:17:42 -08001756 return OK;
1757}
1758
Chong Zhangced1c2f2014-08-08 15:22:35 -07001759void NuPlayer::updateVideoSize(
1760 const sp<AMessage> &inputFormat,
1761 const sp<AMessage> &outputFormat) {
1762 if (inputFormat == NULL) {
1763 ALOGW("Unknown video size, reporting 0x0!");
1764 notifyListener(MEDIA_SET_VIDEO_SIZE, 0, 0);
1765 return;
1766 }
1767
1768 int32_t displayWidth, displayHeight;
Chong Zhangced1c2f2014-08-08 15:22:35 -07001769 if (outputFormat != NULL) {
1770 int32_t width, height;
1771 CHECK(outputFormat->findInt32("width", &width));
1772 CHECK(outputFormat->findInt32("height", &height));
1773
1774 int32_t cropLeft, cropTop, cropRight, cropBottom;
1775 CHECK(outputFormat->findRect(
1776 "crop",
1777 &cropLeft, &cropTop, &cropRight, &cropBottom));
1778
1779 displayWidth = cropRight - cropLeft + 1;
1780 displayHeight = cropBottom - cropTop + 1;
1781
1782 ALOGV("Video output format changed to %d x %d "
1783 "(crop: %d x %d @ (%d, %d))",
1784 width, height,
1785 displayWidth,
1786 displayHeight,
1787 cropLeft, cropTop);
1788 } else {
1789 CHECK(inputFormat->findInt32("width", &displayWidth));
1790 CHECK(inputFormat->findInt32("height", &displayHeight));
1791
1792 ALOGV("Video input format %d x %d", displayWidth, displayHeight);
1793 }
1794
1795 // Take into account sample aspect ratio if necessary:
1796 int32_t sarWidth, sarHeight;
1797 if (inputFormat->findInt32("sar-width", &sarWidth)
1798 && inputFormat->findInt32("sar-height", &sarHeight)) {
1799 ALOGV("Sample aspect ratio %d : %d", sarWidth, sarHeight);
1800
1801 displayWidth = (displayWidth * sarWidth) / sarHeight;
1802
1803 ALOGV("display dimensions %d x %d", displayWidth, displayHeight);
Wei Jiadf6c6af2016-09-29 11:27:15 -07001804 } else {
1805 int32_t width, height;
1806 if (inputFormat->findInt32("display-width", &width)
1807 && inputFormat->findInt32("display-height", &height)
1808 && width > 0 && height > 0
1809 && displayWidth > 0 && displayHeight > 0) {
1810 if (displayHeight * (int64_t)width / height > (int64_t)displayWidth) {
1811 displayHeight = (int32_t)(displayWidth * (int64_t)height / width);
1812 } else {
1813 displayWidth = (int32_t)(displayHeight * (int64_t)width / height);
1814 }
1815 ALOGV("Video display width and height are overridden to %d x %d",
1816 displayWidth, displayHeight);
1817 }
Chong Zhangced1c2f2014-08-08 15:22:35 -07001818 }
1819
1820 int32_t rotationDegrees;
1821 if (!inputFormat->findInt32("rotation-degrees", &rotationDegrees)) {
1822 rotationDegrees = 0;
1823 }
1824
1825 if (rotationDegrees == 90 || rotationDegrees == 270) {
1826 int32_t tmp = displayWidth;
1827 displayWidth = displayHeight;
1828 displayHeight = tmp;
1829 }
1830
1831 notifyListener(
1832 MEDIA_SET_VIDEO_SIZE,
1833 displayWidth,
1834 displayHeight);
1835}
1836
Chong Zhangdcb89b32013-08-06 09:44:47 -07001837void NuPlayer::notifyListener(int msg, int ext1, int ext2, const Parcel *in) {
Andreas Huber43c3e6c2011-01-05 12:17:08 -08001838 if (mDriver == NULL) {
Andreas Huberf9334412010-12-15 15:17:42 -08001839 return;
1840 }
1841
Andreas Huber43c3e6c2011-01-05 12:17:08 -08001842 sp<NuPlayerDriver> driver = mDriver.promote();
Andreas Huberf9334412010-12-15 15:17:42 -08001843
Andreas Huber43c3e6c2011-01-05 12:17:08 -08001844 if (driver == NULL) {
Andreas Huberf9334412010-12-15 15:17:42 -08001845 return;
1846 }
1847
Chong Zhangdcb89b32013-08-06 09:44:47 -07001848 driver->notifyListener(msg, ext1, ext2, in);
Andreas Huberf9334412010-12-15 15:17:42 -08001849}
1850
Chong Zhang7137ec72014-11-12 16:41:05 -08001851void NuPlayer::flushDecoder(bool audio, bool needShutdown) {
Andreas Huber14f76722013-01-15 09:04:18 -08001852 ALOGV("[%s] flushDecoder needShutdown=%d",
1853 audio ? "audio" : "video", needShutdown);
1854
Chong Zhang7137ec72014-11-12 16:41:05 -08001855 const sp<DecoderBase> &decoder = getDecoder(audio);
Lajos Molnar87603c02014-08-20 19:25:30 -07001856 if (decoder == NULL) {
Steve Blockdf64d152012-01-04 20:05:49 +00001857 ALOGI("flushDecoder %s without decoder present",
Andreas Huber6e3d3112011-11-28 12:36:11 -08001858 audio ? "audio" : "video");
Lajos Molnar87603c02014-08-20 19:25:30 -07001859 return;
Andreas Huber6e3d3112011-11-28 12:36:11 -08001860 }
1861
Andreas Huber1aef2112011-01-04 14:01:29 -08001862 // Make sure we don't continue to scan sources until we finish flushing.
1863 ++mScanSourcesGeneration;
Chong Zhang3b032b32015-04-17 15:49:06 -07001864 if (mScanSourcesPending) {
Toshikazu Saitocbcbb792015-09-15 14:53:01 +09001865 if (!needShutdown) {
1866 mDeferredActions.push_back(
1867 new SimpleAction(&NuPlayer::performScanSources));
1868 }
Chong Zhang3b032b32015-04-17 15:49:06 -07001869 mScanSourcesPending = false;
1870 }
Andreas Huber1aef2112011-01-04 14:01:29 -08001871
Chong Zhang7137ec72014-11-12 16:41:05 -08001872 decoder->signalFlush();
Andreas Huber1aef2112011-01-04 14:01:29 -08001873
1874 FlushStatus newStatus =
1875 needShutdown ? FLUSHING_DECODER_SHUTDOWN : FLUSHING_DECODER;
1876
Lajos Molnarfcd3e942015-03-31 10:06:48 -07001877 mFlushComplete[audio][false /* isDecoder */] = (mRenderer == NULL);
Andy Hung8d121d42014-10-03 09:53:53 -07001878 mFlushComplete[audio][true /* isDecoder */] = false;
Andreas Huber1aef2112011-01-04 14:01:29 -08001879 if (audio) {
Wei Jia53904f32014-07-29 10:22:53 -07001880 ALOGE_IF(mFlushingAudio != NONE,
1881 "audio flushDecoder() is called in state %d", mFlushingAudio);
Andreas Huber1aef2112011-01-04 14:01:29 -08001882 mFlushingAudio = newStatus;
Andreas Huber1aef2112011-01-04 14:01:29 -08001883 } else {
Wei Jia53904f32014-07-29 10:22:53 -07001884 ALOGE_IF(mFlushingVideo != NONE,
1885 "video flushDecoder() is called in state %d", mFlushingVideo);
Andreas Huber1aef2112011-01-04 14:01:29 -08001886 mFlushingVideo = newStatus;
Andreas Huber1aef2112011-01-04 14:01:29 -08001887 }
1888}
1889
Chong Zhangced1c2f2014-08-08 15:22:35 -07001890void NuPlayer::queueDecoderShutdown(
1891 bool audio, bool video, const sp<AMessage> &reply) {
1892 ALOGI("queueDecoderShutdown audio=%d, video=%d", audio, video);
Andreas Huber84066782011-08-16 09:34:26 -07001893
Chong Zhangced1c2f2014-08-08 15:22:35 -07001894 mDeferredActions.push_back(
Wei Jiafef808d2014-10-31 17:57:05 -07001895 new FlushDecoderAction(
1896 audio ? FLUSH_CMD_SHUTDOWN : FLUSH_CMD_NONE,
1897 video ? FLUSH_CMD_SHUTDOWN : FLUSH_CMD_NONE));
Andreas Huber84066782011-08-16 09:34:26 -07001898
Chong Zhangced1c2f2014-08-08 15:22:35 -07001899 mDeferredActions.push_back(
1900 new SimpleAction(&NuPlayer::performScanSources));
Andreas Huber84066782011-08-16 09:34:26 -07001901
Chong Zhangced1c2f2014-08-08 15:22:35 -07001902 mDeferredActions.push_back(new PostMessageAction(reply));
1903
1904 processDeferredActions();
Andreas Huber84066782011-08-16 09:34:26 -07001905}
1906
James Dong0d268a32012-08-31 12:18:27 -07001907status_t NuPlayer::setVideoScalingMode(int32_t mode) {
1908 mVideoScalingMode = mode;
Lajos Molnar1de1e252015-04-30 18:18:34 -07001909 if (mSurface != NULL) {
1910 status_t ret = native_window_set_scaling_mode(mSurface.get(), mVideoScalingMode);
James Dong0d268a32012-08-31 12:18:27 -07001911 if (ret != OK) {
1912 ALOGE("Failed to set scaling mode (%d): %s",
1913 -ret, strerror(-ret));
1914 return ret;
1915 }
1916 }
1917 return OK;
1918}
1919
Chong Zhangdcb89b32013-08-06 09:44:47 -07001920status_t NuPlayer::getTrackInfo(Parcel* reply) const {
Lajos Molnar1d15ab52015-03-04 16:46:34 -08001921 sp<AMessage> msg = new AMessage(kWhatGetTrackInfo, this);
Chong Zhangdcb89b32013-08-06 09:44:47 -07001922 msg->setPointer("reply", reply);
1923
1924 sp<AMessage> response;
1925 status_t err = msg->postAndAwaitResponse(&response);
1926 return err;
1927}
1928
Robert Shih7c4f0d72014-07-09 18:53:31 -07001929status_t NuPlayer::getSelectedTrack(int32_t type, Parcel* reply) const {
Lajos Molnar1d15ab52015-03-04 16:46:34 -08001930 sp<AMessage> msg = new AMessage(kWhatGetSelectedTrack, this);
Robert Shih7c4f0d72014-07-09 18:53:31 -07001931 msg->setPointer("reply", reply);
1932 msg->setInt32("type", type);
1933
1934 sp<AMessage> response;
1935 status_t err = msg->postAndAwaitResponse(&response);
1936 if (err == OK && response != NULL) {
1937 CHECK(response->findInt32("err", &err));
1938 }
1939 return err;
1940}
1941
Robert Shih6ffb1fd2014-10-29 16:24:32 -07001942status_t NuPlayer::selectTrack(size_t trackIndex, bool select, int64_t timeUs) {
Lajos Molnar1d15ab52015-03-04 16:46:34 -08001943 sp<AMessage> msg = new AMessage(kWhatSelectTrack, this);
Chong Zhangdcb89b32013-08-06 09:44:47 -07001944 msg->setSize("trackIndex", trackIndex);
1945 msg->setInt32("select", select);
Robert Shih6ffb1fd2014-10-29 16:24:32 -07001946 msg->setInt64("timeUs", timeUs);
Chong Zhangdcb89b32013-08-06 09:44:47 -07001947
1948 sp<AMessage> response;
1949 status_t err = msg->postAndAwaitResponse(&response);
1950
Chong Zhang404fced2014-06-11 14:45:31 -07001951 if (err != OK) {
1952 return err;
1953 }
1954
1955 if (!response->findInt32("err", &err)) {
1956 err = OK;
1957 }
1958
Chong Zhangdcb89b32013-08-06 09:44:47 -07001959 return err;
1960}
1961
Ronghua Wua73d9e02014-10-08 15:13:29 -07001962status_t NuPlayer::getCurrentPosition(int64_t *mediaUs) {
1963 sp<Renderer> renderer = mRenderer;
1964 if (renderer == NULL) {
1965 return NO_INIT;
1966 }
1967
1968 return renderer->getCurrentPosition(mediaUs);
1969}
1970
Praveen Chavane1e5d7a2015-05-19 19:09:48 -07001971void NuPlayer::getStats(Vector<sp<AMessage> > *mTrackStats) {
1972 CHECK(mTrackStats != NULL);
1973
1974 mTrackStats->clear();
1975 if (mVideoDecoder != NULL) {
1976 mTrackStats->push_back(mVideoDecoder->getStats());
1977 }
1978 if (mAudioDecoder != NULL) {
1979 mTrackStats->push_back(mAudioDecoder->getStats());
Chong Zhang7137ec72014-11-12 16:41:05 -08001980 }
Ronghua Wua73d9e02014-10-08 15:13:29 -07001981}
1982
Marco Nelissenf0b72b52014-09-16 15:43:44 -07001983sp<MetaData> NuPlayer::getFileMeta() {
1984 return mSource->getFileFormatMeta();
1985}
1986
Ronghua Wuc8a70d32015-04-29 16:26:34 -07001987float NuPlayer::getFrameRate() {
1988 sp<MetaData> meta = mSource->getFormatMeta(false /* audio */);
1989 if (meta == NULL) {
1990 return 0;
1991 }
1992 int32_t rate;
1993 if (!meta->findInt32(kKeyFrameRate, &rate)) {
1994 // fall back to try file meta
1995 sp<MetaData> fileMeta = getFileMeta();
1996 if (fileMeta == NULL) {
1997 ALOGW("source has video meta but not file meta");
1998 return -1;
1999 }
2000 int32_t fileMetaRate;
2001 if (!fileMeta->findInt32(kKeyFrameRate, &fileMetaRate)) {
2002 return -1;
2003 }
2004 return fileMetaRate;
2005 }
2006 return rate;
2007}
2008
Andreas Huberb7c8e912012-11-27 15:02:53 -08002009void NuPlayer::schedulePollDuration() {
Lajos Molnar1d15ab52015-03-04 16:46:34 -08002010 sp<AMessage> msg = new AMessage(kWhatPollDuration, this);
Andreas Huberb7c8e912012-11-27 15:02:53 -08002011 msg->setInt32("generation", mPollDurationGeneration);
2012 msg->post();
2013}
2014
2015void NuPlayer::cancelPollDuration() {
2016 ++mPollDurationGeneration;
2017}
2018
Andreas Hubera1f8ab02012-11-30 10:53:22 -08002019void NuPlayer::processDeferredActions() {
2020 while (!mDeferredActions.empty()) {
2021 // We won't execute any deferred actions until we're no longer in
2022 // an intermediate state, i.e. one more more decoders are currently
2023 // flushing or shutting down.
2024
Andreas Hubera1f8ab02012-11-30 10:53:22 -08002025 if (mFlushingAudio != NONE || mFlushingVideo != NONE) {
2026 // We're currently flushing, postpone the reset until that's
2027 // completed.
2028
2029 ALOGV("postponing action mFlushingAudio=%d, mFlushingVideo=%d",
2030 mFlushingAudio, mFlushingVideo);
2031
2032 break;
2033 }
2034
2035 sp<Action> action = *mDeferredActions.begin();
2036 mDeferredActions.erase(mDeferredActions.begin());
2037
2038 action->execute(this);
2039 }
2040}
2041
Wei Jiac5de0912016-11-18 10:22:14 -08002042void NuPlayer::performSeek(int64_t seekTimeUs, MediaPlayerSeekMode mode) {
2043 ALOGV("performSeek seekTimeUs=%lld us (%.2f secs), mode=%d",
2044 (long long)seekTimeUs, seekTimeUs / 1E6, mode);
Andreas Hubera1f8ab02012-11-30 10:53:22 -08002045
Andy Hungadf34bf2014-09-03 18:22:22 -07002046 if (mSource == NULL) {
2047 // This happens when reset occurs right before the loop mode
2048 // asynchronously seeks to the start of the stream.
2049 LOG_ALWAYS_FATAL_IF(mAudioDecoder != NULL || mVideoDecoder != NULL,
2050 "mSource is NULL and decoders not NULL audio(%p) video(%p)",
2051 mAudioDecoder.get(), mVideoDecoder.get());
2052 return;
2053 }
Robert Shih1a5c8592015-08-04 18:07:44 -07002054 mPreviousSeekTimeUs = seekTimeUs;
Wei Jiac5de0912016-11-18 10:22:14 -08002055 mSource->seekTo(seekTimeUs, mode);
Robert Shihd3b0bbb2014-07-23 15:00:25 -07002056 ++mTimedTextGeneration;
Andreas Hubera1f8ab02012-11-30 10:53:22 -08002057
Andreas Hubera1f8ab02012-11-30 10:53:22 -08002058 // everything's flushed, continue playback.
2059}
2060
Wei Jiafef808d2014-10-31 17:57:05 -07002061void NuPlayer::performDecoderFlush(FlushCommand audio, FlushCommand video) {
2062 ALOGV("performDecoderFlush audio=%d, video=%d", audio, video);
Andreas Hubera1f8ab02012-11-30 10:53:22 -08002063
Wei Jiafef808d2014-10-31 17:57:05 -07002064 if ((audio == FLUSH_CMD_NONE || mAudioDecoder == NULL)
2065 && (video == FLUSH_CMD_NONE || mVideoDecoder == NULL)) {
Andreas Hubera1f8ab02012-11-30 10:53:22 -08002066 return;
2067 }
2068
Wei Jiafef808d2014-10-31 17:57:05 -07002069 if (audio != FLUSH_CMD_NONE && mAudioDecoder != NULL) {
2070 flushDecoder(true /* audio */, (audio == FLUSH_CMD_SHUTDOWN));
Andreas Hubera1f8ab02012-11-30 10:53:22 -08002071 }
2072
Wei Jiafef808d2014-10-31 17:57:05 -07002073 if (video != FLUSH_CMD_NONE && mVideoDecoder != NULL) {
2074 flushDecoder(false /* audio */, (video == FLUSH_CMD_SHUTDOWN));
Andreas Hubera1f8ab02012-11-30 10:53:22 -08002075 }
2076}
2077
2078void NuPlayer::performReset() {
2079 ALOGV("performReset");
2080
2081 CHECK(mAudioDecoder == NULL);
2082 CHECK(mVideoDecoder == NULL);
2083
2084 cancelPollDuration();
2085
2086 ++mScanSourcesGeneration;
2087 mScanSourcesPending = false;
2088
Lajos Molnar09524832014-07-17 14:29:51 -07002089 if (mRendererLooper != NULL) {
2090 if (mRenderer != NULL) {
2091 mRendererLooper->unregisterHandler(mRenderer->id());
2092 }
2093 mRendererLooper->stop();
2094 mRendererLooper.clear();
2095 }
Andreas Hubera1f8ab02012-11-30 10:53:22 -08002096 mRenderer.clear();
Wei Jia57568df2014-09-22 10:16:29 -07002097 ++mRendererGeneration;
Andreas Hubera1f8ab02012-11-30 10:53:22 -08002098
2099 if (mSource != NULL) {
2100 mSource->stop();
Andreas Huberb5f25f02013-02-05 10:14:26 -08002101
Wei Jiac45a4b22016-04-15 15:30:23 -07002102 Mutex::Autolock autoLock(mSourceLock);
Andreas Hubera1f8ab02012-11-30 10:53:22 -08002103 mSource.clear();
2104 }
2105
2106 if (mDriver != NULL) {
2107 sp<NuPlayerDriver> driver = mDriver.promote();
2108 if (driver != NULL) {
2109 driver->notifyResetComplete();
2110 }
2111 }
Andreas Huber57a339c2012-12-03 11:18:00 -08002112
2113 mStarted = false;
Wei Jia8a092d32016-06-03 14:57:24 -07002114 mPrepared = false;
Ronghua Wu64c2d172015-10-07 16:52:19 -07002115 mResetting = false;
Robert Shih0c61a0d2015-07-06 15:09:10 -07002116 mSourceStarted = false;
Andreas Hubera1f8ab02012-11-30 10:53:22 -08002117}
2118
2119void NuPlayer::performScanSources() {
2120 ALOGV("performScanSources");
2121
Andreas Huber57a339c2012-12-03 11:18:00 -08002122 if (!mStarted) {
2123 return;
2124 }
2125
Andreas Hubera1f8ab02012-11-30 10:53:22 -08002126 if (mAudioDecoder == NULL || mVideoDecoder == NULL) {
2127 postScanSources();
2128 }
2129}
2130
Lajos Molnar1de1e252015-04-30 18:18:34 -07002131void NuPlayer::performSetSurface(const sp<Surface> &surface) {
Andreas Huber57a339c2012-12-03 11:18:00 -08002132 ALOGV("performSetSurface");
2133
Lajos Molnar1de1e252015-04-30 18:18:34 -07002134 mSurface = surface;
Andreas Huber57a339c2012-12-03 11:18:00 -08002135
2136 // XXX - ignore error from setVideoScalingMode for now
2137 setVideoScalingMode(mVideoScalingMode);
Chong Zhang13d6faa2014-08-22 15:35:28 -07002138
2139 if (mDriver != NULL) {
2140 sp<NuPlayerDriver> driver = mDriver.promote();
2141 if (driver != NULL) {
2142 driver->notifySetSurfaceComplete();
2143 }
2144 }
Andreas Huber57a339c2012-12-03 11:18:00 -08002145}
2146
Chong Zhangf8d71772014-11-26 15:08:34 -08002147void NuPlayer::performResumeDecoders(bool needNotify) {
2148 if (needNotify) {
2149 mResumePending = true;
2150 if (mVideoDecoder == NULL) {
2151 // if audio-only, we can notify seek complete now,
2152 // as the resume operation will be relatively fast.
2153 finishResume();
2154 }
2155 }
2156
Chong Zhang7137ec72014-11-12 16:41:05 -08002157 if (mVideoDecoder != NULL) {
Chong Zhangf8d71772014-11-26 15:08:34 -08002158 // When there is continuous seek, MediaPlayer will cache the seek
2159 // position, and send down new seek request when previous seek is
2160 // complete. Let's wait for at least one video output frame before
2161 // notifying seek complete, so that the video thumbnail gets updated
2162 // when seekbar is dragged.
2163 mVideoDecoder->signalResume(needNotify);
Chong Zhang7137ec72014-11-12 16:41:05 -08002164 }
2165
2166 if (mAudioDecoder != NULL) {
Chong Zhangf8d71772014-11-26 15:08:34 -08002167 mAudioDecoder->signalResume(false /* needNotify */);
2168 }
2169}
2170
2171void NuPlayer::finishResume() {
2172 if (mResumePending) {
2173 mResumePending = false;
Wei Jia1061c9c2015-05-19 16:02:17 -07002174 notifyDriverSeekComplete();
2175 }
2176}
2177
2178void NuPlayer::notifyDriverSeekComplete() {
2179 if (mDriver != NULL) {
2180 sp<NuPlayerDriver> driver = mDriver.promote();
2181 if (driver != NULL) {
2182 driver->notifySeekComplete();
Chong Zhangf8d71772014-11-26 15:08:34 -08002183 }
Chong Zhang7137ec72014-11-12 16:41:05 -08002184 }
2185}
2186
Andreas Huber9575c962013-02-05 13:59:56 -08002187void NuPlayer::onSourceNotify(const sp<AMessage> &msg) {
2188 int32_t what;
2189 CHECK(msg->findInt32("what", &what));
2190
2191 switch (what) {
Lajos Molnarfcd3e942015-03-31 10:06:48 -07002192 case Source::kWhatInstantiateSecureDecoders:
2193 {
2194 if (mSource == NULL) {
2195 // This is a stale notification from a source that was
2196 // asynchronously preparing when the client called reset().
2197 // We handled the reset, the source is gone.
2198 break;
2199 }
2200
2201 sp<AMessage> reply;
2202 CHECK(msg->findMessage("reply", &reply));
2203 status_t err = onInstantiateSecureDecoders();
2204 reply->setInt32("err", err);
2205 reply->post();
2206 break;
2207 }
2208
Andreas Huber9575c962013-02-05 13:59:56 -08002209 case Source::kWhatPrepared:
2210 {
Andreas Huberb5f28d42013-04-25 15:11:19 -07002211 if (mSource == NULL) {
2212 // This is a stale notification from a source that was
2213 // asynchronously preparing when the client called reset().
2214 // We handled the reset, the source is gone.
2215 break;
2216 }
2217
Andreas Huberec0c5972013-02-05 14:47:13 -08002218 int32_t err;
2219 CHECK(msg->findInt32("err", &err));
2220
Lajos Molnarfcd3e942015-03-31 10:06:48 -07002221 if (err != OK) {
2222 // shut down potential secure codecs in case client never calls reset
2223 mDeferredActions.push_back(
2224 new FlushDecoderAction(FLUSH_CMD_SHUTDOWN /* audio */,
2225 FLUSH_CMD_SHUTDOWN /* video */));
2226 processDeferredActions();
Wei Jia8a092d32016-06-03 14:57:24 -07002227 } else {
2228 mPrepared = true;
Lajos Molnarfcd3e942015-03-31 10:06:48 -07002229 }
2230
Andreas Huber9575c962013-02-05 13:59:56 -08002231 sp<NuPlayerDriver> driver = mDriver.promote();
2232 if (driver != NULL) {
Marco Nelissendd114d12014-05-28 15:23:14 -07002233 // notify duration first, so that it's definitely set when
2234 // the app received the "prepare complete" callback.
2235 int64_t durationUs;
2236 if (mSource->getDuration(&durationUs) == OK) {
2237 driver->notifyDuration(durationUs);
2238 }
Andreas Huberec0c5972013-02-05 14:47:13 -08002239 driver->notifyPrepareCompleted(err);
Andreas Huber9575c962013-02-05 13:59:56 -08002240 }
Andreas Huber99759402013-04-01 14:28:31 -07002241
Andreas Huber9575c962013-02-05 13:59:56 -08002242 break;
2243 }
2244
2245 case Source::kWhatFlagsChanged:
2246 {
2247 uint32_t flags;
2248 CHECK(msg->findInt32("flags", (int32_t *)&flags));
2249
Chong Zhang4b7069d2013-09-11 12:52:43 -07002250 sp<NuPlayerDriver> driver = mDriver.promote();
2251 if (driver != NULL) {
Wei Jia895651b2014-12-10 17:31:52 -08002252 if ((flags & NuPlayer::Source::FLAG_CAN_SEEK) == 0) {
2253 driver->notifyListener(
2254 MEDIA_INFO, MEDIA_INFO_NOT_SEEKABLE, 0);
2255 }
Chong Zhang4b7069d2013-09-11 12:52:43 -07002256 driver->notifyFlagsChanged(flags);
2257 }
2258
Andreas Huber9575c962013-02-05 13:59:56 -08002259 if ((mSourceFlags & Source::FLAG_DYNAMIC_DURATION)
2260 && (!(flags & Source::FLAG_DYNAMIC_DURATION))) {
2261 cancelPollDuration();
2262 } else if (!(mSourceFlags & Source::FLAG_DYNAMIC_DURATION)
2263 && (flags & Source::FLAG_DYNAMIC_DURATION)
2264 && (mAudioDecoder != NULL || mVideoDecoder != NULL)) {
2265 schedulePollDuration();
2266 }
2267
2268 mSourceFlags = flags;
2269 break;
2270 }
2271
2272 case Source::kWhatVideoSizeChanged:
2273 {
Chong Zhangced1c2f2014-08-08 15:22:35 -07002274 sp<AMessage> format;
2275 CHECK(msg->findMessage("format", &format));
Andreas Huber9575c962013-02-05 13:59:56 -08002276
Chong Zhangced1c2f2014-08-08 15:22:35 -07002277 updateVideoSize(format);
Andreas Huber9575c962013-02-05 13:59:56 -08002278 break;
2279 }
2280
Chong Zhang2a3cc9a2014-08-21 17:48:26 -07002281 case Source::kWhatBufferingUpdate:
2282 {
2283 int32_t percentage;
2284 CHECK(msg->findInt32("percentage", &percentage));
2285
2286 notifyListener(MEDIA_BUFFERING_UPDATE, percentage, 0);
2287 break;
2288 }
2289
Chong Zhangefbb6192015-01-30 17:13:27 -08002290 case Source::kWhatPauseOnBufferingStart:
2291 {
2292 // ignore if not playing
Chong Zhang8a048332015-05-06 15:16:28 -07002293 if (mStarted) {
Chong Zhangefbb6192015-01-30 17:13:27 -08002294 ALOGI("buffer low, pausing...");
2295
Chong Zhang8a048332015-05-06 15:16:28 -07002296 mPausedForBuffering = true;
Chong Zhangefbb6192015-01-30 17:13:27 -08002297 onPause();
2298 }
Wei Jiabfe82072016-05-20 09:21:50 -07002299 notifyListener(MEDIA_INFO, MEDIA_INFO_BUFFERING_START, 0);
Roger Jönssonb50e83e2013-01-21 16:26:41 +01002300 break;
2301 }
2302
Chong Zhangefbb6192015-01-30 17:13:27 -08002303 case Source::kWhatResumeOnBufferingEnd:
2304 {
2305 // ignore if not playing
Chong Zhang8a048332015-05-06 15:16:28 -07002306 if (mStarted) {
Chong Zhangefbb6192015-01-30 17:13:27 -08002307 ALOGI("buffer ready, resuming...");
2308
Chong Zhang8a048332015-05-06 15:16:28 -07002309 mPausedForBuffering = false;
2310
2311 // do not resume yet if client didn't unpause
2312 if (!mPausedByClient) {
2313 onResume();
2314 }
Chong Zhangefbb6192015-01-30 17:13:27 -08002315 }
Wei Jia3bed45a2016-02-17 11:06:47 -08002316 notifyListener(MEDIA_INFO, MEDIA_INFO_BUFFERING_END, 0);
Roger Jönssonb50e83e2013-01-21 16:26:41 +01002317 break;
2318 }
2319
Chong Zhangefbb6192015-01-30 17:13:27 -08002320 case Source::kWhatCacheStats:
2321 {
2322 int32_t kbps;
2323 CHECK(msg->findInt32("bandwidth", &kbps));
2324
2325 notifyListener(MEDIA_INFO, MEDIA_INFO_NETWORK_BANDWIDTH, kbps);
2326 break;
2327 }
2328
Chong Zhangdcb89b32013-08-06 09:44:47 -07002329 case Source::kWhatSubtitleData:
2330 {
2331 sp<ABuffer> buffer;
2332 CHECK(msg->findBuffer("buffer", &buffer));
2333
Chong Zhang404fced2014-06-11 14:45:31 -07002334 sendSubtitleData(buffer, 0 /* baseIndex */);
Chong Zhangdcb89b32013-08-06 09:44:47 -07002335 break;
2336 }
2337
Robert Shih08528432015-04-08 09:06:54 -07002338 case Source::kWhatTimedMetaData:
2339 {
2340 sp<ABuffer> buffer;
2341 if (!msg->findBuffer("buffer", &buffer)) {
2342 notifyListener(MEDIA_INFO, MEDIA_INFO_METADATA_UPDATE, 0);
2343 } else {
2344 sendTimedMetaData(buffer);
2345 }
2346 break;
2347 }
2348
Robert Shihd3b0bbb2014-07-23 15:00:25 -07002349 case Source::kWhatTimedTextData:
2350 {
2351 int32_t generation;
2352 if (msg->findInt32("generation", &generation)
2353 && generation != mTimedTextGeneration) {
2354 break;
2355 }
2356
2357 sp<ABuffer> buffer;
2358 CHECK(msg->findBuffer("buffer", &buffer));
2359
2360 sp<NuPlayerDriver> driver = mDriver.promote();
2361 if (driver == NULL) {
2362 break;
2363 }
2364
2365 int posMs;
2366 int64_t timeUs, posUs;
2367 driver->getCurrentPosition(&posMs);
Patrik2 Carlsson24d484b2015-01-27 16:49:45 +01002368 posUs = (int64_t) posMs * 1000ll;
Robert Shihd3b0bbb2014-07-23 15:00:25 -07002369 CHECK(buffer->meta()->findInt64("timeUs", &timeUs));
2370
2371 if (posUs < timeUs) {
2372 if (!msg->findInt32("generation", &generation)) {
2373 msg->setInt32("generation", mTimedTextGeneration);
2374 }
2375 msg->post(timeUs - posUs);
2376 } else {
2377 sendTimedTextData(buffer);
2378 }
2379 break;
2380 }
2381
Andreas Huber14f76722013-01-15 09:04:18 -08002382 case Source::kWhatQueueDecoderShutdown:
2383 {
2384 int32_t audio, video;
2385 CHECK(msg->findInt32("audio", &audio));
2386 CHECK(msg->findInt32("video", &video));
2387
2388 sp<AMessage> reply;
2389 CHECK(msg->findMessage("reply", &reply));
2390
2391 queueDecoderShutdown(audio, video, reply);
2392 break;
2393 }
2394
Ronghua Wu80276872014-08-28 15:50:29 -07002395 case Source::kWhatDrmNoLicense:
2396 {
2397 notifyListener(MEDIA_ERROR, MEDIA_ERROR_UNKNOWN, ERROR_DRM_NO_LICENSE);
2398 break;
2399 }
2400
Andreas Huber9575c962013-02-05 13:59:56 -08002401 default:
2402 TRESPASS();
2403 }
2404}
2405
Chong Zhanga7fa1d92014-06-11 14:49:23 -07002406void NuPlayer::onClosedCaptionNotify(const sp<AMessage> &msg) {
2407 int32_t what;
2408 CHECK(msg->findInt32("what", &what));
2409
2410 switch (what) {
2411 case NuPlayer::CCDecoder::kWhatClosedCaptionData:
2412 {
2413 sp<ABuffer> buffer;
2414 CHECK(msg->findBuffer("buffer", &buffer));
2415
2416 size_t inbandTracks = 0;
2417 if (mSource != NULL) {
2418 inbandTracks = mSource->getTrackCount();
2419 }
2420
2421 sendSubtitleData(buffer, inbandTracks);
2422 break;
2423 }
2424
2425 case NuPlayer::CCDecoder::kWhatTrackAdded:
2426 {
2427 notifyListener(MEDIA_INFO, MEDIA_INFO_METADATA_UPDATE, 0);
2428
2429 break;
2430 }
2431
2432 default:
2433 TRESPASS();
2434 }
2435
2436
2437}
2438
Chong Zhang404fced2014-06-11 14:45:31 -07002439void NuPlayer::sendSubtitleData(const sp<ABuffer> &buffer, int32_t baseIndex) {
2440 int32_t trackIndex;
2441 int64_t timeUs, durationUs;
2442 CHECK(buffer->meta()->findInt32("trackIndex", &trackIndex));
2443 CHECK(buffer->meta()->findInt64("timeUs", &timeUs));
2444 CHECK(buffer->meta()->findInt64("durationUs", &durationUs));
2445
2446 Parcel in;
2447 in.writeInt32(trackIndex + baseIndex);
2448 in.writeInt64(timeUs);
2449 in.writeInt64(durationUs);
2450 in.writeInt32(buffer->size());
2451 in.writeInt32(buffer->size());
2452 in.write(buffer->data(), buffer->size());
2453
2454 notifyListener(MEDIA_SUBTITLE_DATA, 0, 0, &in);
2455}
Robert Shihd3b0bbb2014-07-23 15:00:25 -07002456
Robert Shih08528432015-04-08 09:06:54 -07002457void NuPlayer::sendTimedMetaData(const sp<ABuffer> &buffer) {
2458 int64_t timeUs;
2459 CHECK(buffer->meta()->findInt64("timeUs", &timeUs));
2460
2461 Parcel in;
2462 in.writeInt64(timeUs);
2463 in.writeInt32(buffer->size());
2464 in.writeInt32(buffer->size());
2465 in.write(buffer->data(), buffer->size());
2466
2467 notifyListener(MEDIA_META_DATA, 0, 0, &in);
2468}
2469
Robert Shihd3b0bbb2014-07-23 15:00:25 -07002470void NuPlayer::sendTimedTextData(const sp<ABuffer> &buffer) {
2471 const void *data;
2472 size_t size = 0;
2473 int64_t timeUs;
Marco Nelissen55e2f4c2015-09-04 15:57:15 -07002474 int32_t flag = TextDescriptions::IN_BAND_TEXT_3GPP;
Robert Shihd3b0bbb2014-07-23 15:00:25 -07002475
2476 AString mime;
2477 CHECK(buffer->meta()->findString("mime", &mime));
2478 CHECK(strcasecmp(mime.c_str(), MEDIA_MIMETYPE_TEXT_3GPP) == 0);
2479
2480 data = buffer->data();
2481 size = buffer->size();
2482
2483 Parcel parcel;
2484 if (size > 0) {
2485 CHECK(buffer->meta()->findInt64("timeUs", &timeUs));
Marco Nelissen55e2f4c2015-09-04 15:57:15 -07002486 int32_t global = 0;
2487 if (buffer->meta()->findInt32("global", &global) && global) {
2488 flag |= TextDescriptions::GLOBAL_DESCRIPTIONS;
2489 } else {
2490 flag |= TextDescriptions::LOCAL_DESCRIPTIONS;
2491 }
Robert Shihd3b0bbb2014-07-23 15:00:25 -07002492 TextDescriptions::getParcelOfDescriptions(
2493 (const uint8_t *)data, size, flag, timeUs / 1000, &parcel);
2494 }
2495
2496 if ((parcel.dataSize() > 0)) {
2497 notifyListener(MEDIA_TIMED_TEXT, 0, 0, &parcel);
2498 } else { // send an empty timed text
2499 notifyListener(MEDIA_TIMED_TEXT, 0, 0);
2500 }
2501}
Andreas Huberb5f25f02013-02-05 10:14:26 -08002502////////////////////////////////////////////////////////////////////////////////
2503
Chong Zhangced1c2f2014-08-08 15:22:35 -07002504sp<AMessage> NuPlayer::Source::getFormat(bool audio) {
2505 sp<MetaData> meta = getFormatMeta(audio);
2506
2507 if (meta == NULL) {
2508 return NULL;
2509 }
2510
2511 sp<AMessage> msg = new AMessage;
2512
2513 if(convertMetaDataToMessage(meta, &msg) == OK) {
2514 return msg;
2515 }
2516 return NULL;
2517}
2518
Andreas Huber9575c962013-02-05 13:59:56 -08002519void NuPlayer::Source::notifyFlagsChanged(uint32_t flags) {
2520 sp<AMessage> notify = dupNotify();
2521 notify->setInt32("what", kWhatFlagsChanged);
2522 notify->setInt32("flags", flags);
2523 notify->post();
2524}
2525
Chong Zhangced1c2f2014-08-08 15:22:35 -07002526void NuPlayer::Source::notifyVideoSizeChanged(const sp<AMessage> &format) {
Andreas Huber9575c962013-02-05 13:59:56 -08002527 sp<AMessage> notify = dupNotify();
2528 notify->setInt32("what", kWhatVideoSizeChanged);
Chong Zhangced1c2f2014-08-08 15:22:35 -07002529 notify->setMessage("format", format);
Andreas Huber9575c962013-02-05 13:59:56 -08002530 notify->post();
2531}
2532
Andreas Huberec0c5972013-02-05 14:47:13 -08002533void NuPlayer::Source::notifyPrepared(status_t err) {
Andreas Huber9575c962013-02-05 13:59:56 -08002534 sp<AMessage> notify = dupNotify();
2535 notify->setInt32("what", kWhatPrepared);
Andreas Huberec0c5972013-02-05 14:47:13 -08002536 notify->setInt32("err", err);
Andreas Huber9575c962013-02-05 13:59:56 -08002537 notify->post();
2538}
2539
Lajos Molnarfcd3e942015-03-31 10:06:48 -07002540void NuPlayer::Source::notifyInstantiateSecureDecoders(const sp<AMessage> &reply) {
2541 sp<AMessage> notify = dupNotify();
2542 notify->setInt32("what", kWhatInstantiateSecureDecoders);
2543 notify->setMessage("reply", reply);
2544 notify->post();
2545}
2546
Andreas Huber84333e02014-02-07 15:36:10 -08002547void NuPlayer::Source::onMessageReceived(const sp<AMessage> & /* msg */) {
Andreas Huberb5f25f02013-02-05 10:14:26 -08002548 TRESPASS();
2549}
2550
Andreas Huberf9334412010-12-15 15:17:42 -08002551} // namespace android