blob: f8827b33ad0970452c57f4bd502d6bac0347e108 [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
Andreas Huber9575c962013-02-05 13:59:56 -0800314void NuPlayer::prepareAsync() {
Lajos Molnar1d15ab52015-03-04 16:46:34 -0800315 (new AMessage(kWhatPrepare, this))->post();
Andreas Huber9575c962013-02-05 13:59:56 -0800316}
317
Andreas Huber57a339c2012-12-03 11:18:00 -0800318void NuPlayer::setVideoSurfaceTextureAsync(
Andy McFadden8ba01022012-12-18 09:46:54 -0800319 const sp<IGraphicBufferProducer> &bufferProducer) {
Lajos Molnar1de1e252015-04-30 18:18:34 -0700320 sp<AMessage> msg = new AMessage(kWhatSetVideoSurface, this);
Andreas Huber57a339c2012-12-03 11:18:00 -0800321
Andy McFadden8ba01022012-12-18 09:46:54 -0800322 if (bufferProducer == NULL) {
Lajos Molnar1de1e252015-04-30 18:18:34 -0700323 msg->setObject("surface", NULL);
Andreas Huber57a339c2012-12-03 11:18:00 -0800324 } else {
Lajos Molnar1de1e252015-04-30 18:18:34 -0700325 msg->setObject("surface", new Surface(bufferProducer, true /* controlledByApp */));
Andreas Huber57a339c2012-12-03 11:18:00 -0800326 }
327
Andreas Huberf9334412010-12-15 15:17:42 -0800328 msg->post();
329}
330
331void NuPlayer::setAudioSink(const sp<MediaPlayerBase::AudioSink> &sink) {
Lajos Molnar1d15ab52015-03-04 16:46:34 -0800332 sp<AMessage> msg = new AMessage(kWhatSetAudioSink, this);
Andreas Huberf9334412010-12-15 15:17:42 -0800333 msg->setObject("sink", sink);
334 msg->post();
335}
336
337void NuPlayer::start() {
Lajos Molnar1d15ab52015-03-04 16:46:34 -0800338 (new AMessage(kWhatStart, this))->post();
Andreas Huberf9334412010-12-15 15:17:42 -0800339}
340
Lajos Molnar3a474aa2015-04-24 17:10:07 -0700341status_t NuPlayer::setPlaybackSettings(const AudioPlaybackRate &rate) {
342 // do some cursory validation of the settings here. audio modes are
343 // only validated when set on the audiosink.
344 if ((rate.mSpeed != 0.f && rate.mSpeed < AUDIO_TIMESTRETCH_SPEED_MIN)
345 || rate.mSpeed > AUDIO_TIMESTRETCH_SPEED_MAX
346 || rate.mPitch < AUDIO_TIMESTRETCH_SPEED_MIN
347 || rate.mPitch > AUDIO_TIMESTRETCH_SPEED_MAX) {
348 return BAD_VALUE;
349 }
350 sp<AMessage> msg = new AMessage(kWhatConfigPlayback, this);
351 writeToAMessage(msg, rate);
352 sp<AMessage> response;
353 status_t err = msg->postAndAwaitResponse(&response);
354 if (err == OK && response != NULL) {
355 CHECK(response->findInt32("err", &err));
356 }
357 return err;
358}
359
360status_t NuPlayer::getPlaybackSettings(AudioPlaybackRate *rate /* nonnull */) {
361 sp<AMessage> msg = new AMessage(kWhatGetPlaybackSettings, this);
362 sp<AMessage> response;
363 status_t err = msg->postAndAwaitResponse(&response);
364 if (err == OK && response != NULL) {
365 CHECK(response->findInt32("err", &err));
366 if (err == OK) {
367 readFromAMessage(response, rate);
368 }
369 }
370 return err;
371}
372
373status_t NuPlayer::setSyncSettings(const AVSyncSettings &sync, float videoFpsHint) {
374 sp<AMessage> msg = new AMessage(kWhatConfigSync, this);
375 writeToAMessage(msg, sync, videoFpsHint);
376 sp<AMessage> response;
377 status_t err = msg->postAndAwaitResponse(&response);
378 if (err == OK && response != NULL) {
379 CHECK(response->findInt32("err", &err));
380 }
381 return err;
382}
383
384status_t NuPlayer::getSyncSettings(
385 AVSyncSettings *sync /* nonnull */, float *videoFps /* nonnull */) {
386 sp<AMessage> msg = new AMessage(kWhatGetSyncSettings, 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, sync, videoFps);
393 }
394 }
395 return err;
Wei Jia98160162015-02-04 17:01:11 -0800396}
397
Andreas Huber43c3e6c2011-01-05 12:17:08 -0800398void NuPlayer::pause() {
Lajos Molnar1d15ab52015-03-04 16:46:34 -0800399 (new AMessage(kWhatPause, this))->post();
Andreas Huber43c3e6c2011-01-05 12:17:08 -0800400}
401
Andreas Huber1aef2112011-01-04 14:01:29 -0800402void NuPlayer::resetAsync() {
Wei Jiac45a4b22016-04-15 15:30:23 -0700403 sp<Source> source;
404 {
405 Mutex::Autolock autoLock(mSourceLock);
406 source = mSource;
407 }
408
409 if (source != NULL) {
Chong Zhang48296b72014-09-14 14:28:45 -0700410 // During a reset, the data source might be unresponsive already, we need to
411 // disconnect explicitly so that reads exit promptly.
412 // We can't queue the disconnect request to the looper, as it might be
413 // queued behind a stuck read and never gets processed.
414 // Doing a disconnect outside the looper to allows the pending reads to exit
415 // (either successfully or with error).
Wei Jiac45a4b22016-04-15 15:30:23 -0700416 source->disconnect();
Chong Zhang48296b72014-09-14 14:28:45 -0700417 }
418
Lajos Molnar1d15ab52015-03-04 16:46:34 -0800419 (new AMessage(kWhatReset, this))->post();
Andreas Huber1aef2112011-01-04 14:01:29 -0800420}
421
Wei Jiac5de0912016-11-18 10:22:14 -0800422void NuPlayer::seekToAsync(int64_t seekTimeUs, MediaPlayerSeekMode mode, bool needNotify) {
Lajos Molnar1d15ab52015-03-04 16:46:34 -0800423 sp<AMessage> msg = new AMessage(kWhatSeek, this);
Andreas Huber43c3e6c2011-01-05 12:17:08 -0800424 msg->setInt64("seekTimeUs", seekTimeUs);
Wei Jiac5de0912016-11-18 10:22:14 -0800425 msg->setInt32("mode", mode);
Wei Jiae427abf2014-09-22 15:21:11 -0700426 msg->setInt32("needNotify", needNotify);
Andreas Huber43c3e6c2011-01-05 12:17:08 -0800427 msg->post();
428}
429
Andreas Huber53df1a42010-12-22 10:03:04 -0800430
Chong Zhang404fced2014-06-11 14:45:31 -0700431void NuPlayer::writeTrackInfo(
Chih-Hung Hsiehe964d4e2016-08-09 14:31:32 -0700432 Parcel* reply, const sp<AMessage>& format) const {
Marco Nelissenc367ca12015-09-15 09:51:59 -0700433 if (format == NULL) {
434 ALOGE("NULL format");
435 return;
436 }
Chong Zhang404fced2014-06-11 14:45:31 -0700437 int32_t trackType;
Marco Nelissen9436e482015-09-15 10:21:53 -0700438 if (!format->findInt32("type", &trackType)) {
Marco Nelissenc367ca12015-09-15 09:51:59 -0700439 ALOGE("no track type");
440 return;
441 }
Chong Zhang404fced2014-06-11 14:45:31 -0700442
Robert Shih755106e2015-04-30 14:36:45 -0700443 AString mime;
Robert Shih2e3a4252015-05-06 10:21:15 -0700444 if (!format->findString("mime", &mime)) {
445 // Java MediaPlayer only uses mimetype for subtitle and timedtext tracks.
446 // If we can't find the mimetype here it means that we wouldn't be needing
447 // the mimetype on the Java end. We still write a placeholder mime to keep the
448 // (de)serialization logic simple.
449 if (trackType == MEDIA_TRACK_TYPE_AUDIO) {
450 mime = "audio/";
451 } else if (trackType == MEDIA_TRACK_TYPE_VIDEO) {
452 mime = "video/";
453 } else {
Marco Nelissenc367ca12015-09-15 09:51:59 -0700454 ALOGE("unknown track type: %d", trackType);
455 return;
Robert Shih2e3a4252015-05-06 10:21:15 -0700456 }
457 }
Robert Shih755106e2015-04-30 14:36:45 -0700458
Chong Zhang404fced2014-06-11 14:45:31 -0700459 AString lang;
Marco Nelissen9436e482015-09-15 10:21:53 -0700460 if (!format->findString("language", &lang)) {
Marco Nelissenc367ca12015-09-15 09:51:59 -0700461 ALOGE("no language");
462 return;
463 }
Chong Zhang404fced2014-06-11 14:45:31 -0700464
465 reply->writeInt32(2); // write something non-zero
466 reply->writeInt32(trackType);
Robert Shih755106e2015-04-30 14:36:45 -0700467 reply->writeString16(String16(mime.c_str()));
Chong Zhang404fced2014-06-11 14:45:31 -0700468 reply->writeString16(String16(lang.c_str()));
469
470 if (trackType == MEDIA_TRACK_TYPE_SUBTITLE) {
Chong Zhang404fced2014-06-11 14:45:31 -0700471 int32_t isAuto, isDefault, isForced;
472 CHECK(format->findInt32("auto", &isAuto));
473 CHECK(format->findInt32("default", &isDefault));
474 CHECK(format->findInt32("forced", &isForced));
475
Chong Zhang404fced2014-06-11 14:45:31 -0700476 reply->writeInt32(isAuto);
477 reply->writeInt32(isDefault);
478 reply->writeInt32(isForced);
479 }
480}
481
Andreas Huberf9334412010-12-15 15:17:42 -0800482void NuPlayer::onMessageReceived(const sp<AMessage> &msg) {
483 switch (msg->what()) {
484 case kWhatSetDataSource:
485 {
Steve Block3856b092011-10-20 11:56:00 +0100486 ALOGV("kWhatSetDataSource");
Andreas Huberf9334412010-12-15 15:17:42 -0800487
488 CHECK(mSource == NULL);
489
Chong Zhang3de157d2014-08-05 20:54:44 -0700490 status_t err = OK;
Andreas Huber5bc087c2010-12-23 10:27:40 -0800491 sp<RefBase> obj;
492 CHECK(msg->findObject("source", &obj));
Chong Zhang3de157d2014-08-05 20:54:44 -0700493 if (obj != NULL) {
Wei Jiac45a4b22016-04-15 15:30:23 -0700494 Mutex::Autolock autoLock(mSourceLock);
Chong Zhang3de157d2014-08-05 20:54:44 -0700495 mSource = static_cast<Source *>(obj.get());
Chong Zhang3de157d2014-08-05 20:54:44 -0700496 } else {
497 err = UNKNOWN_ERROR;
498 }
Andreas Huber9575c962013-02-05 13:59:56 -0800499
500 CHECK(mDriver != NULL);
501 sp<NuPlayerDriver> driver = mDriver.promote();
502 if (driver != NULL) {
Chong Zhang3de157d2014-08-05 20:54:44 -0700503 driver->notifySetDataSourceCompleted(err);
Andreas Huber9575c962013-02-05 13:59:56 -0800504 }
505 break;
506 }
507
508 case kWhatPrepare:
509 {
510 mSource->prepareAsync();
Andreas Huberf9334412010-12-15 15:17:42 -0800511 break;
512 }
513
Chong Zhangdcb89b32013-08-06 09:44:47 -0700514 case kWhatGetTrackInfo:
515 {
Lajos Molnar3f274362015-03-05 14:35:41 -0800516 sp<AReplyToken> replyID;
Chong Zhangdcb89b32013-08-06 09:44:47 -0700517 CHECK(msg->senderAwaitsResponse(&replyID));
518
Chong Zhang404fced2014-06-11 14:45:31 -0700519 Parcel* reply;
520 CHECK(msg->findPointer("reply", (void**)&reply));
521
522 size_t inbandTracks = 0;
Chong Zhangdcb89b32013-08-06 09:44:47 -0700523 if (mSource != NULL) {
Chong Zhang404fced2014-06-11 14:45:31 -0700524 inbandTracks = mSource->getTrackCount();
525 }
526
Chong Zhanga7fa1d92014-06-11 14:49:23 -0700527 size_t ccTracks = 0;
528 if (mCCDecoder != NULL) {
529 ccTracks = mCCDecoder->getTrackCount();
530 }
531
Chong Zhang404fced2014-06-11 14:45:31 -0700532 // total track count
Chong Zhanga7fa1d92014-06-11 14:49:23 -0700533 reply->writeInt32(inbandTracks + ccTracks);
Chong Zhang404fced2014-06-11 14:45:31 -0700534
535 // write inband tracks
536 for (size_t i = 0; i < inbandTracks; ++i) {
537 writeTrackInfo(reply, mSource->getTrackInfo(i));
Chong Zhangdcb89b32013-08-06 09:44:47 -0700538 }
539
Chong Zhanga7fa1d92014-06-11 14:49:23 -0700540 // write CC track
541 for (size_t i = 0; i < ccTracks; ++i) {
542 writeTrackInfo(reply, mCCDecoder->getTrackInfo(i));
543 }
544
Chong Zhangdcb89b32013-08-06 09:44:47 -0700545 sp<AMessage> response = new AMessage;
Chong Zhangdcb89b32013-08-06 09:44:47 -0700546 response->postReply(replyID);
547 break;
548 }
549
Robert Shih7c4f0d72014-07-09 18:53:31 -0700550 case kWhatGetSelectedTrack:
551 {
552 status_t err = INVALID_OPERATION;
553 if (mSource != NULL) {
554 err = OK;
555
556 int32_t type32;
557 CHECK(msg->findInt32("type", (int32_t*)&type32));
558 media_track_type type = (media_track_type)type32;
559 ssize_t selectedTrack = mSource->getSelectedTrack(type);
560
561 Parcel* reply;
562 CHECK(msg->findPointer("reply", (void**)&reply));
563 reply->writeInt32(selectedTrack);
564 }
565
566 sp<AMessage> response = new AMessage;
567 response->setInt32("err", err);
568
Lajos Molnar3f274362015-03-05 14:35:41 -0800569 sp<AReplyToken> replyID;
Robert Shih7c4f0d72014-07-09 18:53:31 -0700570 CHECK(msg->senderAwaitsResponse(&replyID));
571 response->postReply(replyID);
572 break;
573 }
574
Chong Zhangdcb89b32013-08-06 09:44:47 -0700575 case kWhatSelectTrack:
576 {
Lajos Molnar3f274362015-03-05 14:35:41 -0800577 sp<AReplyToken> replyID;
Chong Zhangdcb89b32013-08-06 09:44:47 -0700578 CHECK(msg->senderAwaitsResponse(&replyID));
579
Chong Zhang404fced2014-06-11 14:45:31 -0700580 size_t trackIndex;
581 int32_t select;
Robert Shih6ffb1fd2014-10-29 16:24:32 -0700582 int64_t timeUs;
Chong Zhang404fced2014-06-11 14:45:31 -0700583 CHECK(msg->findSize("trackIndex", &trackIndex));
584 CHECK(msg->findInt32("select", &select));
Robert Shih6ffb1fd2014-10-29 16:24:32 -0700585 CHECK(msg->findInt64("timeUs", &timeUs));
Chong Zhang404fced2014-06-11 14:45:31 -0700586
Chong Zhangdcb89b32013-08-06 09:44:47 -0700587 status_t err = INVALID_OPERATION;
Chong Zhang404fced2014-06-11 14:45:31 -0700588
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 }
Chong Zhanga7fa1d92014-06-11 14:49:23 -0700593 size_t ccTracks = 0;
594 if (mCCDecoder != NULL) {
595 ccTracks = mCCDecoder->getTrackCount();
596 }
Chong Zhang404fced2014-06-11 14:45:31 -0700597
598 if (trackIndex < inbandTracks) {
Robert Shih6ffb1fd2014-10-29 16:24:32 -0700599 err = mSource->selectTrack(trackIndex, select, timeUs);
Robert Shihd3b0bbb2014-07-23 15:00:25 -0700600
601 if (!select && err == OK) {
602 int32_t type;
603 sp<AMessage> info = mSource->getTrackInfo(trackIndex);
604 if (info != NULL
605 && info->findInt32("type", &type)
606 && type == MEDIA_TRACK_TYPE_TIMEDTEXT) {
607 ++mTimedTextGeneration;
608 }
609 }
Chong Zhanga7fa1d92014-06-11 14:49:23 -0700610 } else {
611 trackIndex -= inbandTracks;
612
613 if (trackIndex < ccTracks) {
614 err = mCCDecoder->selectTrack(trackIndex, select);
615 }
Chong Zhangdcb89b32013-08-06 09:44:47 -0700616 }
617
618 sp<AMessage> response = new AMessage;
619 response->setInt32("err", err);
620
621 response->postReply(replyID);
622 break;
623 }
624
Andreas Huberb7c8e912012-11-27 15:02:53 -0800625 case kWhatPollDuration:
626 {
627 int32_t generation;
628 CHECK(msg->findInt32("generation", &generation));
629
630 if (generation != mPollDurationGeneration) {
631 // stale
632 break;
633 }
634
635 int64_t durationUs;
636 if (mDriver != NULL && mSource->getDuration(&durationUs) == OK) {
637 sp<NuPlayerDriver> driver = mDriver.promote();
638 if (driver != NULL) {
639 driver->notifyDuration(durationUs);
640 }
641 }
642
643 msg->post(1000000ll); // poll again in a second.
644 break;
645 }
646
Lajos Molnar1de1e252015-04-30 18:18:34 -0700647 case kWhatSetVideoSurface:
Andreas Huberf9334412010-12-15 15:17:42 -0800648 {
Andreas Huberf9334412010-12-15 15:17:42 -0800649
650 sp<RefBase> obj;
Lajos Molnar1de1e252015-04-30 18:18:34 -0700651 CHECK(msg->findObject("surface", &obj));
652 sp<Surface> surface = static_cast<Surface *>(obj.get());
Lajos Molnara81c6222015-07-10 19:17:45 -0700653
654 ALOGD("onSetVideoSurface(%p, %s video decoder)",
655 surface.get(),
Wei Jia6b7d2ed2015-08-10 15:11:54 -0700656 (mSource != NULL && mStarted && mSource->getFormat(false /* audio */) != NULL
Lajos Molnara81c6222015-07-10 19:17:45 -0700657 && mVideoDecoder != NULL) ? "have" : "no");
658
Wei Jia6b7d2ed2015-08-10 15:11:54 -0700659 // Need to check mStarted before calling mSource->getFormat because NuPlayer might
660 // be in preparing state and it could take long time.
661 // When mStarted is true, mSource must have been set.
662 if (mSource == NULL || !mStarted || mSource->getFormat(false /* audio */) == NULL
Lajos Molnara81c6222015-07-10 19:17:45 -0700663 // NOTE: mVideoDecoder's mSurface is always non-null
664 || (mVideoDecoder != NULL && mVideoDecoder->setVideoSurface(surface) == OK)) {
Lajos Molnar1de1e252015-04-30 18:18:34 -0700665 performSetSurface(surface);
Wei Jiafef808d2014-10-31 17:57:05 -0700666 break;
667 }
668
669 mDeferredActions.push_back(
670 new FlushDecoderAction(FLUSH_CMD_FLUSH /* audio */,
671 FLUSH_CMD_SHUTDOWN /* video */));
672
Lajos Molnar1de1e252015-04-30 18:18:34 -0700673 mDeferredActions.push_back(new SetSurfaceAction(surface));
James Dong0d268a32012-08-31 12:18:27 -0700674
Wei Jiac6e58412015-07-10 18:04:55 -0700675 if (obj != NULL || mAudioDecoder != NULL) {
Wei Jiafef808d2014-10-31 17:57:05 -0700676 if (mStarted) {
Andy Hung73535852014-09-05 11:42:58 -0700677 // Issue a seek to refresh the video screen only if started otherwise
678 // the extractor may not yet be started and will assert.
679 // If the video decoder is not set (perhaps audio only in this case)
680 // do not perform a seek as it is not needed.
Ronghua Wua73d9e02014-10-08 15:13:29 -0700681 int64_t currentPositionUs = 0;
682 if (getCurrentPosition(&currentPositionUs) == OK) {
683 mDeferredActions.push_back(
Wei Jiac5de0912016-11-18 10:22:14 -0800684 new SeekAction(currentPositionUs,
685 MediaPlayerSeekMode::SEEK_PREVIOUS_SYNC /* mode */));
Ronghua Wua73d9e02014-10-08 15:13:29 -0700686 }
Andy Hung73535852014-09-05 11:42:58 -0700687 }
Wei Jiaac428aa2014-09-02 19:01:34 -0700688
Andreas Huber57a339c2012-12-03 11:18:00 -0800689 // If there is a new surface texture, instantiate decoders
690 // again if possible.
691 mDeferredActions.push_back(
692 new SimpleAction(&NuPlayer::performScanSources));
693 }
694
Chong Zhangf8d71772014-11-26 15:08:34 -0800695 // After a flush without shutdown, decoder is paused.
696 // Don't resume it until source seek is done, otherwise it could
Chong Zhang7137ec72014-11-12 16:41:05 -0800697 // start pulling stale data too soon.
698 mDeferredActions.push_back(
Chong Zhangf8d71772014-11-26 15:08:34 -0800699 new ResumeDecoderAction(false /* needNotify */));
Chong Zhang7137ec72014-11-12 16:41:05 -0800700
Andreas Huber57a339c2012-12-03 11:18:00 -0800701 processDeferredActions();
Andreas Huberf9334412010-12-15 15:17:42 -0800702 break;
703 }
704
705 case kWhatSetAudioSink:
706 {
Steve Block3856b092011-10-20 11:56:00 +0100707 ALOGV("kWhatSetAudioSink");
Andreas Huberf9334412010-12-15 15:17:42 -0800708
709 sp<RefBase> obj;
710 CHECK(msg->findObject("sink", &obj));
711
712 mAudioSink = static_cast<MediaPlayerBase::AudioSink *>(obj.get());
713 break;
714 }
715
716 case kWhatStart:
717 {
Steve Block3856b092011-10-20 11:56:00 +0100718 ALOGV("kWhatStart");
Wei Jia94211742014-10-28 17:09:06 -0700719 if (mStarted) {
Chong Zhang8a048332015-05-06 15:16:28 -0700720 // do not resume yet if the source is still buffering
721 if (!mPausedForBuffering) {
722 onResume();
723 }
Wei Jia94211742014-10-28 17:09:06 -0700724 } else {
725 onStart();
Lajos Molnar09524832014-07-17 14:29:51 -0700726 }
Chong Zhangefbb6192015-01-30 17:13:27 -0800727 mPausedByClient = false;
Andreas Huberf9334412010-12-15 15:17:42 -0800728 break;
729 }
730
Lajos Molnar3a474aa2015-04-24 17:10:07 -0700731 case kWhatConfigPlayback:
Wei Jia98160162015-02-04 17:01:11 -0800732 {
Lajos Molnar3a474aa2015-04-24 17:10:07 -0700733 sp<AReplyToken> replyID;
734 CHECK(msg->senderAwaitsResponse(&replyID));
735 AudioPlaybackRate rate /* sanitized */;
736 readFromAMessage(msg, &rate);
737 status_t err = OK;
Wei Jia98160162015-02-04 17:01:11 -0800738 if (mRenderer != NULL) {
Wei Jiabf70feb2016-02-19 15:47:16 -0800739 // AudioSink allows only 1.f and 0.f for offload mode.
740 // For other speed, switch to non-offload mode.
741 if (mOffloadAudio && ((rate.mSpeed != 0.f && rate.mSpeed != 1.f)
742 || rate.mPitch != 1.f)) {
743 int64_t currentPositionUs;
744 if (getCurrentPosition(&currentPositionUs) != OK) {
745 currentPositionUs = mPreviousSeekTimeUs;
746 }
747
748 // Set mPlaybackSettings so that the new audio decoder can
749 // be created correctly.
750 mPlaybackSettings = rate;
751 if (!mPaused) {
752 mRenderer->pause();
753 }
Wei Jia5031b2f2016-02-25 11:19:31 -0800754 restartAudio(
Wei Jiabf70feb2016-02-19 15:47:16 -0800755 currentPositionUs, true /* forceNonOffload */,
756 true /* needsToCreateAudioDecoder */);
757 if (!mPaused) {
758 mRenderer->resume();
759 }
760 }
761
Lajos Molnar3a474aa2015-04-24 17:10:07 -0700762 err = mRenderer->setPlaybackSettings(rate);
763 }
764 if (err == OK) {
765 if (rate.mSpeed == 0.f) {
766 onPause();
Wei Jia351ce872016-02-10 13:21:59 -0800767 mPausedByClient = true;
Lajos Molnar3a474aa2015-04-24 17:10:07 -0700768 // save all other settings (using non-paused speed)
769 // so we can restore them on start
770 AudioPlaybackRate newRate = rate;
771 newRate.mSpeed = mPlaybackSettings.mSpeed;
772 mPlaybackSettings = newRate;
773 } else { /* rate.mSpeed != 0.f */
Lajos Molnar3a474aa2015-04-24 17:10:07 -0700774 mPlaybackSettings = rate;
Wei Jia8a092d32016-06-03 14:57:24 -0700775 if (mStarted) {
776 // do not resume yet if the source is still buffering
777 if (!mPausedForBuffering) {
778 onResume();
779 }
780 } else if (mPrepared) {
781 onStart();
782 }
783
784 mPausedByClient = false;
Lajos Molnar3a474aa2015-04-24 17:10:07 -0700785 }
Wei Jia98160162015-02-04 17:01:11 -0800786 }
Ronghua Wu8db88132015-04-22 13:51:35 -0700787
788 if (mVideoDecoder != NULL) {
Praveen Chavanbbaa1442016-04-08 13:33:49 -0700789 sp<AMessage> params = new AMessage();
790 params->setFloat("playback-speed", mPlaybackSettings.mSpeed);
791 mVideoDecoder->setParameters(params);
Ronghua Wu8db88132015-04-22 13:51:35 -0700792 }
793
Lajos Molnar3a474aa2015-04-24 17:10:07 -0700794 sp<AMessage> response = new AMessage;
795 response->setInt32("err", err);
796 response->postReply(replyID);
797 break;
798 }
799
800 case kWhatGetPlaybackSettings:
801 {
802 sp<AReplyToken> replyID;
803 CHECK(msg->senderAwaitsResponse(&replyID));
804 AudioPlaybackRate rate = mPlaybackSettings;
805 status_t err = OK;
806 if (mRenderer != NULL) {
807 err = mRenderer->getPlaybackSettings(&rate);
808 }
809 if (err == OK) {
810 // get playback settings used by renderer, as it may be
811 // slightly off due to audiosink not taking small changes.
812 mPlaybackSettings = rate;
813 if (mPaused) {
814 rate.mSpeed = 0.f;
815 }
816 }
817 sp<AMessage> response = new AMessage;
818 if (err == OK) {
819 writeToAMessage(response, rate);
820 }
821 response->setInt32("err", err);
822 response->postReply(replyID);
823 break;
824 }
825
826 case kWhatConfigSync:
827 {
828 sp<AReplyToken> replyID;
829 CHECK(msg->senderAwaitsResponse(&replyID));
830
831 ALOGV("kWhatConfigSync");
832 AVSyncSettings sync;
833 float videoFpsHint;
834 readFromAMessage(msg, &sync, &videoFpsHint);
835 status_t err = OK;
836 if (mRenderer != NULL) {
837 err = mRenderer->setSyncSettings(sync, videoFpsHint);
838 }
839 if (err == OK) {
840 mSyncSettings = sync;
841 mVideoFpsHint = videoFpsHint;
842 }
843 sp<AMessage> response = new AMessage;
844 response->setInt32("err", err);
845 response->postReply(replyID);
846 break;
847 }
848
849 case kWhatGetSyncSettings:
850 {
851 sp<AReplyToken> replyID;
852 CHECK(msg->senderAwaitsResponse(&replyID));
853 AVSyncSettings sync = mSyncSettings;
854 float videoFps = mVideoFpsHint;
855 status_t err = OK;
856 if (mRenderer != NULL) {
857 err = mRenderer->getSyncSettings(&sync, &videoFps);
858 if (err == OK) {
859 mSyncSettings = sync;
860 mVideoFpsHint = videoFps;
861 }
862 }
863 sp<AMessage> response = new AMessage;
864 if (err == OK) {
865 writeToAMessage(response, sync, videoFps);
866 }
867 response->setInt32("err", err);
868 response->postReply(replyID);
Wei Jia98160162015-02-04 17:01:11 -0800869 break;
870 }
871
Andreas Huberf9334412010-12-15 15:17:42 -0800872 case kWhatScanSources:
873 {
Andreas Huber1aef2112011-01-04 14:01:29 -0800874 int32_t generation;
875 CHECK(msg->findInt32("generation", &generation));
876 if (generation != mScanSourcesGeneration) {
877 // Drop obsolete msg.
878 break;
879 }
880
Andreas Huber5bc087c2010-12-23 10:27:40 -0800881 mScanSourcesPending = false;
882
Steve Block3856b092011-10-20 11:56:00 +0100883 ALOGV("scanning sources haveAudio=%d, haveVideo=%d",
Andreas Huber43c3e6c2011-01-05 12:17:08 -0800884 mAudioDecoder != NULL, mVideoDecoder != NULL);
885
Andreas Huberb7c8e912012-11-27 15:02:53 -0800886 bool mHadAnySourcesBefore =
887 (mAudioDecoder != NULL) || (mVideoDecoder != NULL);
Robert Shih7350b052015-10-01 15:50:14 -0700888 bool rescan = false;
Andreas Huberb7c8e912012-11-27 15:02:53 -0800889
Andy Hung282a7e32014-08-14 15:56:34 -0700890 // initialize video before audio because successful initialization of
891 // video may change deep buffer mode of audio.
Lajos Molnar1de1e252015-04-30 18:18:34 -0700892 if (mSurface != NULL) {
Robert Shih7350b052015-10-01 15:50:14 -0700893 if (instantiateDecoder(false, &mVideoDecoder) == -EWOULDBLOCK) {
894 rescan = true;
895 }
Haynes Mathew George5d246ef2012-07-09 10:36:57 -0700896 }
Andreas Huberf9334412010-12-15 15:17:42 -0800897
Ronghua Wua10fd232014-11-06 16:15:20 -0800898 // Don't try to re-open audio sink if there's an existing decoder.
899 if (mAudioSink != NULL && mAudioDecoder == NULL) {
Robert Shih7350b052015-10-01 15:50:14 -0700900 if (instantiateDecoder(true, &mAudioDecoder) == -EWOULDBLOCK) {
901 rescan = true;
902 }
Andreas Huberf9334412010-12-15 15:17:42 -0800903 }
904
Andreas Huberb7c8e912012-11-27 15:02:53 -0800905 if (!mHadAnySourcesBefore
906 && (mAudioDecoder != NULL || mVideoDecoder != NULL)) {
907 // This is the first time we've found anything playable.
908
Andreas Huber9575c962013-02-05 13:59:56 -0800909 if (mSourceFlags & Source::FLAG_DYNAMIC_DURATION) {
Andreas Huberb7c8e912012-11-27 15:02:53 -0800910 schedulePollDuration();
911 }
912 }
913
Andreas Hubereac68ba2011-09-27 12:12:25 -0700914 status_t err;
915 if ((err = mSource->feedMoreTSData()) != OK) {
Andreas Huber1aef2112011-01-04 14:01:29 -0800916 if (mAudioDecoder == NULL && mVideoDecoder == NULL) {
917 // We're not currently decoding anything (no audio or
918 // video tracks found) and we just ran out of input data.
Andreas Hubereac68ba2011-09-27 12:12:25 -0700919
920 if (err == ERROR_END_OF_STREAM) {
921 notifyListener(MEDIA_PLAYBACK_COMPLETE, 0, 0);
922 } else {
923 notifyListener(MEDIA_ERROR, MEDIA_ERROR_UNKNOWN, err);
924 }
Andreas Huber1aef2112011-01-04 14:01:29 -0800925 }
Andreas Huberf9334412010-12-15 15:17:42 -0800926 break;
927 }
928
Robert Shih7350b052015-10-01 15:50:14 -0700929 if (rescan) {
Andreas Huberf9334412010-12-15 15:17:42 -0800930 msg->post(100000ll);
Andreas Huber5bc087c2010-12-23 10:27:40 -0800931 mScanSourcesPending = true;
Andreas Huberf9334412010-12-15 15:17:42 -0800932 }
933 break;
934 }
935
936 case kWhatVideoNotify:
937 case kWhatAudioNotify:
938 {
939 bool audio = msg->what() == kWhatAudioNotify;
940
Wei Jia88703c32014-08-06 11:24:07 -0700941 int32_t currentDecoderGeneration =
942 (audio? mAudioDecoderGeneration : mVideoDecoderGeneration);
943 int32_t requesterGeneration = currentDecoderGeneration - 1;
944 CHECK(msg->findInt32("generation", &requesterGeneration));
945
946 if (requesterGeneration != currentDecoderGeneration) {
947 ALOGV("got message from old %s decoder, generation(%d:%d)",
948 audio ? "audio" : "video", requesterGeneration,
949 currentDecoderGeneration);
950 sp<AMessage> reply;
951 if (!(msg->findMessage("reply", &reply))) {
952 return;
953 }
954
955 reply->setInt32("err", INFO_DISCONTINUITY);
956 reply->post();
957 return;
958 }
959
Andreas Huberf9334412010-12-15 15:17:42 -0800960 int32_t what;
Lajos Molnar1cd13982014-01-17 15:12:51 -0800961 CHECK(msg->findInt32("what", &what));
Andreas Huberf9334412010-12-15 15:17:42 -0800962
Chong Zhang7137ec72014-11-12 16:41:05 -0800963 if (what == DecoderBase::kWhatInputDiscontinuity) {
964 int32_t formatChange;
965 CHECK(msg->findInt32("formatChange", &formatChange));
Andreas Huberf9334412010-12-15 15:17:42 -0800966
Chong Zhang7137ec72014-11-12 16:41:05 -0800967 ALOGV("%s discontinuity: formatChange %d",
968 audio ? "audio" : "video", formatChange);
969
970 if (formatChange) {
971 mDeferredActions.push_back(
972 new FlushDecoderAction(
973 audio ? FLUSH_CMD_SHUTDOWN : FLUSH_CMD_NONE,
974 audio ? FLUSH_CMD_NONE : FLUSH_CMD_SHUTDOWN));
Andreas Huberf9334412010-12-15 15:17:42 -0800975 }
Chong Zhang7137ec72014-11-12 16:41:05 -0800976
977 mDeferredActions.push_back(
978 new SimpleAction(
979 &NuPlayer::performScanSources));
980
981 processDeferredActions();
982 } else if (what == DecoderBase::kWhatEOS) {
Andreas Huberdc9bacd2011-09-26 10:53:29 -0700983 int32_t err;
Lajos Molnar1cd13982014-01-17 15:12:51 -0800984 CHECK(msg->findInt32("err", &err));
Andreas Huberdc9bacd2011-09-26 10:53:29 -0700985
986 if (err == ERROR_END_OF_STREAM) {
Steve Block3856b092011-10-20 11:56:00 +0100987 ALOGV("got %s decoder EOS", audio ? "audio" : "video");
Andreas Huberdc9bacd2011-09-26 10:53:29 -0700988 } else {
Steve Block3856b092011-10-20 11:56:00 +0100989 ALOGV("got %s decoder EOS w/ error %d",
Andreas Huberdc9bacd2011-09-26 10:53:29 -0700990 audio ? "audio" : "video",
991 err);
992 }
993
994 mRenderer->queueEOS(audio, err);
Chong Zhang7137ec72014-11-12 16:41:05 -0800995 } else if (what == DecoderBase::kWhatFlushCompleted) {
Steve Block3856b092011-10-20 11:56:00 +0100996 ALOGV("decoder %s flush completed", audio ? "audio" : "video");
Andreas Huberf9334412010-12-15 15:17:42 -0800997
Andy Hung8d121d42014-10-03 09:53:53 -0700998 handleFlushComplete(audio, true /* isDecoder */);
Andreas Huber3831a062010-12-21 10:22:33 -0800999 finishFlushIfPossible();
Chong Zhang7137ec72014-11-12 16:41:05 -08001000 } else if (what == DecoderBase::kWhatVideoSizeChanged) {
Lajos Molnar1cd13982014-01-17 15:12:51 -08001001 sp<AMessage> format;
1002 CHECK(msg->findMessage("format", &format));
1003
Wei Jiac6cfd702014-11-11 16:33:20 -08001004 sp<AMessage> inputFormat =
1005 mSource->getFormat(false /* audio */);
Andreas Huber3831a062010-12-21 10:22:33 -08001006
Bartosz Dolewski26936f72014-12-11 12:47:08 +01001007 setVideoScalingMode(mVideoScalingMode);
Wei Jiac6cfd702014-11-11 16:33:20 -08001008 updateVideoSize(inputFormat, format);
Chong Zhang7137ec72014-11-12 16:41:05 -08001009 } else if (what == DecoderBase::kWhatShutdownCompleted) {
Steve Block3856b092011-10-20 11:56:00 +01001010 ALOGV("%s shutdown completed", audio ? "audio" : "video");
Andreas Huber3831a062010-12-21 10:22:33 -08001011 if (audio) {
1012 mAudioDecoder.clear();
Wei Jia57568df2014-09-22 10:16:29 -07001013 ++mAudioDecoderGeneration;
Andreas Huber3831a062010-12-21 10:22:33 -08001014
1015 CHECK_EQ((int)mFlushingAudio, (int)SHUTTING_DOWN_DECODER);
1016 mFlushingAudio = SHUT_DOWN;
1017 } else {
1018 mVideoDecoder.clear();
Wei Jia57568df2014-09-22 10:16:29 -07001019 ++mVideoDecoderGeneration;
Andreas Huber3831a062010-12-21 10:22:33 -08001020
1021 CHECK_EQ((int)mFlushingVideo, (int)SHUTTING_DOWN_DECODER);
1022 mFlushingVideo = SHUT_DOWN;
1023 }
1024
1025 finishFlushIfPossible();
Chong Zhangf8d71772014-11-26 15:08:34 -08001026 } else if (what == DecoderBase::kWhatResumeCompleted) {
1027 finishResume();
Chong Zhang7137ec72014-11-12 16:41:05 -08001028 } else if (what == DecoderBase::kWhatError) {
Chong Zhangf4c0a942014-08-11 15:14:10 -07001029 status_t err;
Andy Hung2abde2c2014-09-30 14:40:32 -07001030 if (!msg->findInt32("err", &err) || err == OK) {
Chong Zhangf4c0a942014-08-11 15:14:10 -07001031 err = UNKNOWN_ERROR;
1032 }
Andy Hungcf31f1e2014-09-23 14:59:01 -07001033
Andy Hung2abde2c2014-09-30 14:40:32 -07001034 // Decoder errors can be due to Source (e.g. from streaming),
1035 // or from decoding corrupted bitstreams, or from other decoder
1036 // MediaCodec operations (e.g. from an ongoing reset or seek).
Andy Hung202bce12014-12-03 11:47:36 -08001037 // They may also be due to openAudioSink failure at
1038 // decoder start or after a format change.
Andy Hung2abde2c2014-09-30 14:40:32 -07001039 //
1040 // We try to gracefully shut down the affected decoder if possible,
1041 // rather than trying to force the shutdown with something
1042 // similar to performReset(). This method can lead to a hang
1043 // if MediaCodec functions block after an error, but they should
1044 // typically return INVALID_OPERATION instead of blocking.
1045
1046 FlushStatus *flushing = audio ? &mFlushingAudio : &mFlushingVideo;
1047 ALOGE("received error(%#x) from %s decoder, flushing(%d), now shutting down",
1048 err, audio ? "audio" : "video", *flushing);
1049
1050 switch (*flushing) {
1051 case NONE:
1052 mDeferredActions.push_back(
Wei Jiafef808d2014-10-31 17:57:05 -07001053 new FlushDecoderAction(
1054 audio ? FLUSH_CMD_SHUTDOWN : FLUSH_CMD_NONE,
1055 audio ? FLUSH_CMD_NONE : FLUSH_CMD_SHUTDOWN));
Andy Hung2abde2c2014-09-30 14:40:32 -07001056 processDeferredActions();
1057 break;
1058 case FLUSHING_DECODER:
1059 *flushing = FLUSHING_DECODER_SHUTDOWN; // initiate shutdown after flush.
1060 break; // Wait for flush to complete.
1061 case FLUSHING_DECODER_SHUTDOWN:
1062 break; // Wait for flush to complete.
1063 case SHUTTING_DOWN_DECODER:
1064 break; // Wait for shutdown to complete.
1065 case FLUSHED:
1066 // Widevine source reads must stop before releasing the video decoder.
1067 if (!audio && mSource != NULL && mSourceFlags & Source::FLAG_SECURE) {
1068 mSource->stop();
Robert Shih0c61a0d2015-07-06 15:09:10 -07001069 mSourceStarted = false;
Andy Hung2abde2c2014-09-30 14:40:32 -07001070 }
1071 getDecoder(audio)->initiateShutdown(); // In the middle of a seek.
1072 *flushing = SHUTTING_DOWN_DECODER; // Shut down.
1073 break;
1074 case SHUT_DOWN:
1075 finishFlushIfPossible(); // Should not occur.
1076 break; // Finish anyways.
Marco Nelissen9e2b7912014-08-18 16:13:03 -07001077 }
Andy Hung2abde2c2014-09-30 14:40:32 -07001078 notifyListener(MEDIA_ERROR, MEDIA_ERROR_UNKNOWN, err);
Lajos Molnar1cd13982014-01-17 15:12:51 -08001079 } else {
1080 ALOGV("Unhandled decoder notification %d '%c%c%c%c'.",
Andreas Hubera1f8ab02012-11-30 10:53:22 -08001081 what,
1082 what >> 24,
1083 (what >> 16) & 0xff,
1084 (what >> 8) & 0xff,
1085 what & 0xff);
Andreas Huberf9334412010-12-15 15:17:42 -08001086 }
1087
1088 break;
1089 }
1090
1091 case kWhatRendererNotify:
1092 {
Wei Jia57568df2014-09-22 10:16:29 -07001093 int32_t requesterGeneration = mRendererGeneration - 1;
1094 CHECK(msg->findInt32("generation", &requesterGeneration));
1095 if (requesterGeneration != mRendererGeneration) {
1096 ALOGV("got message from old renderer, generation(%d:%d)",
1097 requesterGeneration, mRendererGeneration);
1098 return;
1099 }
1100
Andreas Huberf9334412010-12-15 15:17:42 -08001101 int32_t what;
1102 CHECK(msg->findInt32("what", &what));
1103
1104 if (what == Renderer::kWhatEOS) {
1105 int32_t audio;
1106 CHECK(msg->findInt32("audio", &audio));
1107
Andreas Huberc92fd242011-08-16 13:48:44 -07001108 int32_t finalResult;
1109 CHECK(msg->findInt32("finalResult", &finalResult));
1110
Andreas Huberf9334412010-12-15 15:17:42 -08001111 if (audio) {
1112 mAudioEOS = true;
1113 } else {
1114 mVideoEOS = true;
1115 }
1116
Andreas Huberc92fd242011-08-16 13:48:44 -07001117 if (finalResult == ERROR_END_OF_STREAM) {
Steve Block3856b092011-10-20 11:56:00 +01001118 ALOGV("reached %s EOS", audio ? "audio" : "video");
Andreas Huberc92fd242011-08-16 13:48:44 -07001119 } else {
Steve Block29357bc2012-01-06 19:20:56 +00001120 ALOGE("%s track encountered an error (%d)",
Andreas Huberc92fd242011-08-16 13:48:44 -07001121 audio ? "audio" : "video", finalResult);
1122
1123 notifyListener(
1124 MEDIA_ERROR, MEDIA_ERROR_UNKNOWN, finalResult);
1125 }
Andreas Huberf9334412010-12-15 15:17:42 -08001126
1127 if ((mAudioEOS || mAudioDecoder == NULL)
1128 && (mVideoEOS || mVideoDecoder == NULL)) {
1129 notifyListener(MEDIA_PLAYBACK_COMPLETE, 0, 0);
1130 }
Andreas Huber3fe62152011-09-16 15:09:22 -07001131 } else if (what == Renderer::kWhatFlushComplete) {
Andreas Huberf9334412010-12-15 15:17:42 -08001132 int32_t audio;
1133 CHECK(msg->findInt32("audio", &audio));
1134
Wei Jia3261f0d2015-10-08 13:58:33 -07001135 if (audio) {
1136 mAudioEOS = false;
1137 } else {
1138 mVideoEOS = false;
1139 }
1140
Steve Block3856b092011-10-20 11:56:00 +01001141 ALOGV("renderer %s flush completed.", audio ? "audio" : "video");
Wei Jiada4252f2015-07-14 18:15:28 -07001142 if (audio && (mFlushingAudio == NONE || mFlushingAudio == FLUSHED
1143 || mFlushingAudio == SHUT_DOWN)) {
1144 // Flush has been handled by tear down.
1145 break;
1146 }
Andy Hung8d121d42014-10-03 09:53:53 -07001147 handleFlushComplete(audio, false /* isDecoder */);
1148 finishFlushIfPossible();
James Dongf57b4ea2012-07-20 13:38:36 -07001149 } else if (what == Renderer::kWhatVideoRenderingStart) {
1150 notifyListener(MEDIA_INFO, MEDIA_INFO_RENDERING_START, 0);
Lajos Molnarcbaffcf2013-08-14 18:30:38 -07001151 } else if (what == Renderer::kWhatMediaRenderingStart) {
1152 ALOGV("media rendering started");
1153 notifyListener(MEDIA_STARTED, 0, 0);
Ronghua Wufaeb0f22015-05-21 12:20:21 -07001154 } else if (what == Renderer::kWhatAudioTearDown) {
Ronghua Wu08529172014-10-02 16:55:52 -07001155 int32_t reason;
1156 CHECK(msg->findInt32("reason", &reason));
Ronghua Wufaeb0f22015-05-21 12:20:21 -07001157 ALOGV("Tear down audio with reason %d.", reason);
Wei Jia8456ddd2016-04-22 14:46:43 -07001158 if (reason == Renderer::kDueToTimeout && !(mPaused && mOffloadAudio)) {
1159 // TimeoutWhenPaused is only for offload mode.
1160 ALOGW("Receive a stale message for teardown.");
1161 break;
1162 }
Wei Jiada4252f2015-07-14 18:15:28 -07001163 int64_t positionUs;
Robert Shih1a5c8592015-08-04 18:07:44 -07001164 if (!msg->findInt64("positionUs", &positionUs)) {
1165 positionUs = mPreviousSeekTimeUs;
1166 }
Ronghua Wufaeb0f22015-05-21 12:20:21 -07001167
Wei Jia5031b2f2016-02-25 11:19:31 -08001168 restartAudio(
Wei Jiaa05f1e32016-03-25 16:31:22 -07001169 positionUs, reason == Renderer::kForceNonOffload /* forceNonOffload */,
1170 reason != Renderer::kDueToTimeout /* needsToCreateAudioDecoder */);
Andreas Huberf9334412010-12-15 15:17:42 -08001171 }
1172 break;
1173 }
1174
1175 case kWhatMoreDataQueued:
1176 {
1177 break;
1178 }
1179
Andreas Huber1aef2112011-01-04 14:01:29 -08001180 case kWhatReset:
1181 {
Steve Block3856b092011-10-20 11:56:00 +01001182 ALOGV("kWhatReset");
Andreas Huber1aef2112011-01-04 14:01:29 -08001183
Ronghua Wu64c2d172015-10-07 16:52:19 -07001184 mResetting = true;
1185
Andreas Hubera1f8ab02012-11-30 10:53:22 -08001186 mDeferredActions.push_back(
Wei Jiafef808d2014-10-31 17:57:05 -07001187 new FlushDecoderAction(
1188 FLUSH_CMD_SHUTDOWN /* audio */,
1189 FLUSH_CMD_SHUTDOWN /* video */));
Andreas Huberb7c8e912012-11-27 15:02:53 -08001190
Andreas Hubera1f8ab02012-11-30 10:53:22 -08001191 mDeferredActions.push_back(
1192 new SimpleAction(&NuPlayer::performReset));
Andreas Huberb58ce9f2011-11-28 16:27:35 -08001193
Andreas Hubera1f8ab02012-11-30 10:53:22 -08001194 processDeferredActions();
Andreas Huber1aef2112011-01-04 14:01:29 -08001195 break;
1196 }
1197
Andreas Huber43c3e6c2011-01-05 12:17:08 -08001198 case kWhatSeek:
1199 {
1200 int64_t seekTimeUs;
Wei Jiac5de0912016-11-18 10:22:14 -08001201 int32_t mode;
Wei Jiae427abf2014-09-22 15:21:11 -07001202 int32_t needNotify;
Andreas Huber43c3e6c2011-01-05 12:17:08 -08001203 CHECK(msg->findInt64("seekTimeUs", &seekTimeUs));
Wei Jiac5de0912016-11-18 10:22:14 -08001204 CHECK(msg->findInt32("mode", &mode));
Wei Jiae427abf2014-09-22 15:21:11 -07001205 CHECK(msg->findInt32("needNotify", &needNotify));
Andreas Huber43c3e6c2011-01-05 12:17:08 -08001206
Wei Jiac5de0912016-11-18 10:22:14 -08001207 ALOGV("kWhatSeek seekTimeUs=%lld us, mode=%d, needNotify=%d",
1208 (long long)seekTimeUs, mode, needNotify);
Andreas Huber43c3e6c2011-01-05 12:17:08 -08001209
Wei Jia1061c9c2015-05-19 16:02:17 -07001210 if (!mStarted) {
1211 // Seek before the player is started. In order to preview video,
1212 // need to start the player and pause it. This branch is called
1213 // only once if needed. After the player is started, any seek
1214 // operation will go through normal path.
Robert Shih0c61a0d2015-07-06 15:09:10 -07001215 // Audio-only cases are handled separately.
Wei Jiac5de0912016-11-18 10:22:14 -08001216 onStart(seekTimeUs, (MediaPlayerSeekMode)mode);
Robert Shih0c61a0d2015-07-06 15:09:10 -07001217 if (mStarted) {
1218 onPause();
1219 mPausedByClient = true;
1220 }
Wei Jia1061c9c2015-05-19 16:02:17 -07001221 if (needNotify) {
1222 notifyDriverSeekComplete();
1223 }
1224 break;
1225 }
1226
Andreas Hubera1f8ab02012-11-30 10:53:22 -08001227 mDeferredActions.push_back(
Wei Jiafef808d2014-10-31 17:57:05 -07001228 new FlushDecoderAction(FLUSH_CMD_FLUSH /* audio */,
1229 FLUSH_CMD_FLUSH /* video */));
Andreas Huber43c3e6c2011-01-05 12:17:08 -08001230
Wei Jiae427abf2014-09-22 15:21:11 -07001231 mDeferredActions.push_back(
Wei Jiac5de0912016-11-18 10:22:14 -08001232 new SeekAction(seekTimeUs, (MediaPlayerSeekMode)mode));
Andreas Huber43c3e6c2011-01-05 12:17:08 -08001233
Chong Zhangf8d71772014-11-26 15:08:34 -08001234 // After a flush without shutdown, decoder is paused.
1235 // Don't resume it until source seek is done, otherwise it could
Chong Zhang7137ec72014-11-12 16:41:05 -08001236 // start pulling stale data too soon.
1237 mDeferredActions.push_back(
Chong Zhangf8d71772014-11-26 15:08:34 -08001238 new ResumeDecoderAction(needNotify));
Chong Zhang7137ec72014-11-12 16:41:05 -08001239
Andreas Hubera1f8ab02012-11-30 10:53:22 -08001240 processDeferredActions();
Andreas Huber43c3e6c2011-01-05 12:17:08 -08001241 break;
1242 }
1243
Andreas Huberb4082222011-01-20 15:23:04 -08001244 case kWhatPause:
1245 {
Chong Zhangefbb6192015-01-30 17:13:27 -08001246 onPause();
1247 mPausedByClient = true;
Andreas Huberb4082222011-01-20 15:23:04 -08001248 break;
1249 }
1250
Andreas Huberb5f25f02013-02-05 10:14:26 -08001251 case kWhatSourceNotify:
1252 {
Andreas Huber9575c962013-02-05 13:59:56 -08001253 onSourceNotify(msg);
Andreas Huberb5f25f02013-02-05 10:14:26 -08001254 break;
1255 }
1256
Chong Zhanga7fa1d92014-06-11 14:49:23 -07001257 case kWhatClosedCaptionNotify:
1258 {
1259 onClosedCaptionNotify(msg);
1260 break;
1261 }
1262
Andreas Huberf9334412010-12-15 15:17:42 -08001263 default:
1264 TRESPASS();
1265 break;
1266 }
1267}
1268
Wei Jia94211742014-10-28 17:09:06 -07001269void NuPlayer::onResume() {
Ronghua Wu64c2d172015-10-07 16:52:19 -07001270 if (!mPaused || mResetting) {
1271 ALOGD_IF(mResetting, "resetting, onResume discarded");
Chong Zhangefbb6192015-01-30 17:13:27 -08001272 return;
1273 }
1274 mPaused = false;
Wei Jia94211742014-10-28 17:09:06 -07001275 if (mSource != NULL) {
1276 mSource->resume();
1277 } else {
1278 ALOGW("resume called when source is gone or not set");
1279 }
1280 // |mAudioDecoder| may have been released due to the pause timeout, so re-create it if
1281 // needed.
1282 if (audioDecoderStillNeeded() && mAudioDecoder == NULL) {
1283 instantiateDecoder(true /* audio */, &mAudioDecoder);
1284 }
1285 if (mRenderer != NULL) {
1286 mRenderer->resume();
1287 } else {
1288 ALOGW("resume called when renderer is gone or not set");
1289 }
1290}
1291
Lajos Molnarfcd3e942015-03-31 10:06:48 -07001292status_t NuPlayer::onInstantiateSecureDecoders() {
1293 status_t err;
1294 if (!(mSourceFlags & Source::FLAG_SECURE)) {
1295 return BAD_TYPE;
1296 }
1297
1298 if (mRenderer != NULL) {
1299 ALOGE("renderer should not be set when instantiating secure decoders");
1300 return UNKNOWN_ERROR;
1301 }
1302
1303 // TRICKY: We rely on mRenderer being null, so that decoder does not start requesting
1304 // data on instantiation.
Lajos Molnar1de1e252015-04-30 18:18:34 -07001305 if (mSurface != NULL) {
Lajos Molnarfcd3e942015-03-31 10:06:48 -07001306 err = instantiateDecoder(false, &mVideoDecoder);
1307 if (err != OK) {
1308 return err;
1309 }
1310 }
1311
1312 if (mAudioSink != NULL) {
1313 err = instantiateDecoder(true, &mAudioDecoder);
1314 if (err != OK) {
1315 return err;
1316 }
1317 }
1318 return OK;
1319}
1320
Wei Jiac5de0912016-11-18 10:22:14 -08001321void NuPlayer::onStart(int64_t startPositionUs, MediaPlayerSeekMode mode) {
Robert Shih0c61a0d2015-07-06 15:09:10 -07001322 if (!mSourceStarted) {
1323 mSourceStarted = true;
1324 mSource->start();
1325 }
1326 if (startPositionUs > 0) {
Wei Jiac5de0912016-11-18 10:22:14 -08001327 performSeek(startPositionUs, mode);
Robert Shih0c61a0d2015-07-06 15:09:10 -07001328 if (mSource->getFormat(false /* audio */) == NULL) {
1329 return;
1330 }
1331 }
1332
Wei Jia94211742014-10-28 17:09:06 -07001333 mOffloadAudio = false;
1334 mAudioEOS = false;
1335 mVideoEOS = false;
Wei Jia94211742014-10-28 17:09:06 -07001336 mStarted = true;
Wei Jiafe8fe7d2016-06-08 10:29:25 -07001337 mPaused = false;
Wei Jia94211742014-10-28 17:09:06 -07001338
Wei Jia94211742014-10-28 17:09:06 -07001339 uint32_t flags = 0;
1340
1341 if (mSource->isRealTime()) {
1342 flags |= Renderer::FLAG_REAL_TIME;
1343 }
1344
Wei Jia9737d342016-12-28 14:59:59 -08001345 bool hasAudio = (mSource->getFormat(true /* audio */) != NULL);
1346 bool hasVideo = (mSource->getFormat(false /* audio */) != NULL);
1347 if (!hasAudio && !hasVideo) {
Wei Jia1de83d52016-10-10 10:15:03 -07001348 ALOGE("no metadata for either audio or video source");
1349 mSource->stop();
1350 mSourceStarted = false;
1351 notifyListener(MEDIA_ERROR, MEDIA_ERROR_UNKNOWN, ERROR_MALFORMED);
1352 return;
1353 }
Wei Jia9737d342016-12-28 14:59:59 -08001354 ALOGV_IF(!hasAudio, "no metadata for audio source"); // video only stream
1355
1356 sp<MetaData> audioMeta = mSource->getFormatMeta(true /* audio */);
Wei Jia1de83d52016-10-10 10:15:03 -07001357
Wei Jia94211742014-10-28 17:09:06 -07001358 audio_stream_type_t streamType = AUDIO_STREAM_MUSIC;
1359 if (mAudioSink != NULL) {
1360 streamType = mAudioSink->getAudioStreamType();
1361 }
1362
Wei Jia94211742014-10-28 17:09:06 -07001363 mOffloadAudio =
Wei Jia9737d342016-12-28 14:59:59 -08001364 canOffloadStream(audioMeta, hasVideo, mSource->isStreaming(), streamType)
Wei Jiabf70feb2016-02-19 15:47:16 -08001365 && (mPlaybackSettings.mSpeed == 1.f && mPlaybackSettings.mPitch == 1.f);
Wei Jia94211742014-10-28 17:09:06 -07001366 if (mOffloadAudio) {
1367 flags |= Renderer::FLAG_OFFLOAD_AUDIO;
1368 }
1369
Lajos Molnar1d15ab52015-03-04 16:46:34 -08001370 sp<AMessage> notify = new AMessage(kWhatRendererNotify, this);
Wei Jia94211742014-10-28 17:09:06 -07001371 ++mRendererGeneration;
1372 notify->setInt32("generation", mRendererGeneration);
1373 mRenderer = new Renderer(mAudioSink, notify, flags);
Wei Jia94211742014-10-28 17:09:06 -07001374 mRendererLooper = new ALooper;
1375 mRendererLooper->setName("NuPlayerRenderer");
1376 mRendererLooper->start(false, false, ANDROID_PRIORITY_AUDIO);
1377 mRendererLooper->registerHandler(mRenderer);
Lajos Molnar3a474aa2015-04-24 17:10:07 -07001378
1379 status_t err = mRenderer->setPlaybackSettings(mPlaybackSettings);
1380 if (err != OK) {
1381 mSource->stop();
Robert Shih0c61a0d2015-07-06 15:09:10 -07001382 mSourceStarted = false;
Lajos Molnar3a474aa2015-04-24 17:10:07 -07001383 notifyListener(MEDIA_ERROR, MEDIA_ERROR_UNKNOWN, err);
1384 return;
Wei Jiac8206ff2015-03-04 13:59:37 -08001385 }
Wei Jia94211742014-10-28 17:09:06 -07001386
Ronghua Wuc8a70d32015-04-29 16:26:34 -07001387 float rate = getFrameRate();
1388 if (rate > 0) {
Wei Jia94211742014-10-28 17:09:06 -07001389 mRenderer->setVideoFrameRate(rate);
1390 }
1391
Wei Jiac6cfd702014-11-11 16:33:20 -08001392 if (mVideoDecoder != NULL) {
1393 mVideoDecoder->setRenderer(mRenderer);
1394 }
1395 if (mAudioDecoder != NULL) {
1396 mAudioDecoder->setRenderer(mRenderer);
1397 }
1398
Wei Jia94211742014-10-28 17:09:06 -07001399 postScanSources();
1400}
1401
Chong Zhangefbb6192015-01-30 17:13:27 -08001402void NuPlayer::onPause() {
1403 if (mPaused) {
1404 return;
1405 }
1406 mPaused = true;
1407 if (mSource != NULL) {
1408 mSource->pause();
1409 } else {
1410 ALOGW("pause called when source is gone or not set");
1411 }
1412 if (mRenderer != NULL) {
1413 mRenderer->pause();
1414 } else {
1415 ALOGW("pause called when renderer is gone or not set");
1416 }
1417}
1418
Ronghua Wud7988b12014-10-03 15:19:10 -07001419bool NuPlayer::audioDecoderStillNeeded() {
1420 // Audio decoder is no longer needed if it's in shut/shutting down status.
1421 return ((mFlushingAudio != SHUT_DOWN) && (mFlushingAudio != SHUTTING_DOWN_DECODER));
1422}
1423
Andy Hung8d121d42014-10-03 09:53:53 -07001424void NuPlayer::handleFlushComplete(bool audio, bool isDecoder) {
1425 // We wait for both the decoder flush and the renderer flush to complete
1426 // before entering either the FLUSHED or the SHUTTING_DOWN_DECODER state.
1427
1428 mFlushComplete[audio][isDecoder] = true;
1429 if (!mFlushComplete[audio][!isDecoder]) {
1430 return;
1431 }
1432
1433 FlushStatus *state = audio ? &mFlushingAudio : &mFlushingVideo;
1434 switch (*state) {
1435 case FLUSHING_DECODER:
1436 {
1437 *state = FLUSHED;
Andy Hung8d121d42014-10-03 09:53:53 -07001438 break;
1439 }
1440
1441 case FLUSHING_DECODER_SHUTDOWN:
1442 {
1443 *state = SHUTTING_DOWN_DECODER;
1444
1445 ALOGV("initiating %s decoder shutdown", audio ? "audio" : "video");
1446 if (!audio) {
Andy Hung8d121d42014-10-03 09:53:53 -07001447 // Widevine source reads must stop before releasing the video decoder.
1448 if (mSource != NULL && mSourceFlags & Source::FLAG_SECURE) {
1449 mSource->stop();
Robert Shih0c61a0d2015-07-06 15:09:10 -07001450 mSourceStarted = false;
Andy Hung8d121d42014-10-03 09:53:53 -07001451 }
1452 }
1453 getDecoder(audio)->initiateShutdown();
1454 break;
1455 }
1456
1457 default:
1458 // decoder flush completes only occur in a flushing state.
1459 LOG_ALWAYS_FATAL_IF(isDecoder, "decoder flush in invalid state %d", *state);
1460 break;
1461 }
1462}
1463
Andreas Huber3831a062010-12-21 10:22:33 -08001464void NuPlayer::finishFlushIfPossible() {
Wei Jia53904f32014-07-29 10:22:53 -07001465 if (mFlushingAudio != NONE && mFlushingAudio != FLUSHED
1466 && mFlushingAudio != SHUT_DOWN) {
Andreas Huber3831a062010-12-21 10:22:33 -08001467 return;
1468 }
1469
Wei Jia53904f32014-07-29 10:22:53 -07001470 if (mFlushingVideo != NONE && mFlushingVideo != FLUSHED
1471 && mFlushingVideo != SHUT_DOWN) {
Andreas Huber3831a062010-12-21 10:22:33 -08001472 return;
1473 }
1474
Steve Block3856b092011-10-20 11:56:00 +01001475 ALOGV("both audio and video are flushed now.");
Andreas Huber3831a062010-12-21 10:22:33 -08001476
Andreas Huber3831a062010-12-21 10:22:33 -08001477 mFlushingAudio = NONE;
1478 mFlushingVideo = NONE;
Andreas Huber3831a062010-12-21 10:22:33 -08001479
Andy Hung8d121d42014-10-03 09:53:53 -07001480 clearFlushComplete();
1481
Andreas Hubera1f8ab02012-11-30 10:53:22 -08001482 processDeferredActions();
Andreas Huber1aef2112011-01-04 14:01:29 -08001483}
1484
1485void NuPlayer::postScanSources() {
1486 if (mScanSourcesPending) {
1487 return;
1488 }
1489
Lajos Molnar1d15ab52015-03-04 16:46:34 -08001490 sp<AMessage> msg = new AMessage(kWhatScanSources, this);
Andreas Huber1aef2112011-01-04 14:01:29 -08001491 msg->setInt32("generation", mScanSourcesGeneration);
1492 msg->post();
1493
1494 mScanSourcesPending = true;
1495}
1496
Wei Jia41cd4632016-05-13 10:53:30 -07001497void NuPlayer::tryOpenAudioSinkForOffload(
1498 const sp<AMessage> &format, const sp<MetaData> &audioMeta, bool hasVideo) {
Andy Hung202bce12014-12-03 11:47:36 -08001499 // Note: This is called early in NuPlayer to determine whether offloading
1500 // is possible; otherwise the decoders call the renderer openAudioSink directly.
Andy Hung282a7e32014-08-14 15:56:34 -07001501
Andy Hung202bce12014-12-03 11:47:36 -08001502 status_t err = mRenderer->openAudioSink(
1503 format, true /* offloadOnly */, hasVideo, AUDIO_OUTPUT_FLAG_NONE, &mOffloadAudio);
1504 if (err != OK) {
1505 // Any failure we turn off mOffloadAudio.
1506 mOffloadAudio = false;
1507 } else if (mOffloadAudio) {
Chong Zhang3b9eb1f2014-10-15 17:05:08 -07001508 sendMetaDataToHal(mAudioSink, audioMeta);
Andy Hung282a7e32014-08-14 15:56:34 -07001509 }
1510}
1511
1512void NuPlayer::closeAudioSink() {
Chong Zhang3b9eb1f2014-10-15 17:05:08 -07001513 mRenderer->closeAudioSink();
Andy Hung282a7e32014-08-14 15:56:34 -07001514}
1515
Wei Jia5031b2f2016-02-25 11:19:31 -08001516void NuPlayer::restartAudio(
Wei Jiabf70feb2016-02-19 15:47:16 -08001517 int64_t currentPositionUs, bool forceNonOffload, bool needsToCreateAudioDecoder) {
Wei Jiaa05f1e32016-03-25 16:31:22 -07001518 if (mAudioDecoder != NULL) {
1519 mAudioDecoder->pause();
1520 mAudioDecoder.clear();
1521 ++mAudioDecoderGeneration;
1522 }
Wei Jiabf70feb2016-02-19 15:47:16 -08001523 if (mFlushingAudio == FLUSHING_DECODER) {
1524 mFlushComplete[1 /* audio */][1 /* isDecoder */] = true;
1525 mFlushingAudio = FLUSHED;
1526 finishFlushIfPossible();
1527 } else if (mFlushingAudio == FLUSHING_DECODER_SHUTDOWN
1528 || mFlushingAudio == SHUTTING_DOWN_DECODER) {
1529 mFlushComplete[1 /* audio */][1 /* isDecoder */] = true;
1530 mFlushingAudio = SHUT_DOWN;
1531 finishFlushIfPossible();
1532 needsToCreateAudioDecoder = false;
1533 }
1534 if (mRenderer == NULL) {
1535 return;
1536 }
1537 closeAudioSink();
1538 mRenderer->flush(true /* audio */, false /* notifyComplete */);
1539 if (mVideoDecoder != NULL) {
1540 mRenderer->flush(false /* audio */, false /* notifyComplete */);
1541 }
1542
Wei Jiac5de0912016-11-18 10:22:14 -08001543 performSeek(currentPositionUs, MediaPlayerSeekMode::SEEK_PREVIOUS_SYNC /* mode */);
Wei Jiabf70feb2016-02-19 15:47:16 -08001544
1545 if (forceNonOffload) {
1546 mRenderer->signalDisableOffloadAudio();
1547 mOffloadAudio = false;
1548 }
1549 if (needsToCreateAudioDecoder) {
Wei Jiaa05f1e32016-03-25 16:31:22 -07001550 instantiateDecoder(true /* audio */, &mAudioDecoder, !forceNonOffload);
Wei Jiabf70feb2016-02-19 15:47:16 -08001551 }
1552}
1553
Wei Jia41cd4632016-05-13 10:53:30 -07001554void NuPlayer::determineAudioModeChange(const sp<AMessage> &audioFormat) {
Wei Jiae4d18c72015-07-13 17:58:11 -07001555 if (mSource == NULL || mAudioSink == NULL) {
1556 return;
1557 }
1558
1559 if (mRenderer == NULL) {
1560 ALOGW("No renderer can be used to determine audio mode. Use non-offload for safety.");
1561 mOffloadAudio = false;
1562 return;
1563 }
1564
1565 sp<MetaData> audioMeta = mSource->getFormatMeta(true /* audio */);
1566 sp<AMessage> videoFormat = mSource->getFormat(false /* audio */);
1567 audio_stream_type_t streamType = mAudioSink->getAudioStreamType();
1568 const bool hasVideo = (videoFormat != NULL);
1569 const bool canOffload = canOffloadStream(
Wei Jiabf70feb2016-02-19 15:47:16 -08001570 audioMeta, hasVideo, mSource->isStreaming(), streamType)
1571 && (mPlaybackSettings.mSpeed == 1.f && mPlaybackSettings.mPitch == 1.f);
Wei Jiae4d18c72015-07-13 17:58:11 -07001572 if (canOffload) {
1573 if (!mOffloadAudio) {
1574 mRenderer->signalEnableOffloadAudio();
1575 }
1576 // open audio sink early under offload mode.
Wei Jia41cd4632016-05-13 10:53:30 -07001577 tryOpenAudioSinkForOffload(audioFormat, audioMeta, hasVideo);
Wei Jiae4d18c72015-07-13 17:58:11 -07001578 } else {
Robert Shihe1d70192015-07-23 17:54:13 -07001579 if (mOffloadAudio) {
1580 mRenderer->signalDisableOffloadAudio();
1581 mOffloadAudio = false;
1582 }
Wei Jiae4d18c72015-07-13 17:58:11 -07001583 }
1584}
1585
Wei Jiaa05f1e32016-03-25 16:31:22 -07001586status_t NuPlayer::instantiateDecoder(
1587 bool audio, sp<DecoderBase> *decoder, bool checkAudioModeChange) {
Wei Jia566da802015-08-27 13:59:30 -07001588 // The audio decoder could be cleared by tear down. If still in shut down
1589 // process, no need to create a new audio decoder.
1590 if (*decoder != NULL || (audio && mFlushingAudio == SHUT_DOWN)) {
Andreas Huberf9334412010-12-15 15:17:42 -08001591 return OK;
1592 }
1593
Andreas Huber84066782011-08-16 09:34:26 -07001594 sp<AMessage> format = mSource->getFormat(audio);
Andreas Huberf9334412010-12-15 15:17:42 -08001595
Andreas Huber84066782011-08-16 09:34:26 -07001596 if (format == NULL) {
Robert Shih7350b052015-10-01 15:50:14 -07001597 return UNKNOWN_ERROR;
1598 } else {
1599 status_t err;
1600 if (format->findInt32("err", &err) && err) {
1601 return err;
1602 }
Andreas Huberf9334412010-12-15 15:17:42 -08001603 }
1604
Ronghua Wu8db88132015-04-22 13:51:35 -07001605 format->setInt32("priority", 0 /* realtime */);
1606
Andreas Huber3fe62152011-09-16 15:09:22 -07001607 if (!audio) {
Andreas Huber84066782011-08-16 09:34:26 -07001608 AString mime;
1609 CHECK(format->findString("mime", &mime));
Chong Zhanga7fa1d92014-06-11 14:49:23 -07001610
Lajos Molnar1d15ab52015-03-04 16:46:34 -08001611 sp<AMessage> ccNotify = new AMessage(kWhatClosedCaptionNotify, this);
Chong Zhang341ab6e2015-02-04 13:37:18 -08001612 if (mCCDecoder == NULL) {
1613 mCCDecoder = new CCDecoder(ccNotify);
1614 }
Lajos Molnar09524832014-07-17 14:29:51 -07001615
1616 if (mSourceFlags & Source::FLAG_SECURE) {
1617 format->setInt32("secure", true);
1618 }
Chong Zhang17134602015-01-07 16:14:34 -08001619
1620 if (mSourceFlags & Source::FLAG_PROTECTED) {
1621 format->setInt32("protected", true);
1622 }
Ronghua Wu8db88132015-04-22 13:51:35 -07001623
Ronghua Wuc8a70d32015-04-29 16:26:34 -07001624 float rate = getFrameRate();
1625 if (rate > 0) {
Lajos Molnar3a474aa2015-04-24 17:10:07 -07001626 format->setFloat("operating-rate", rate * mPlaybackSettings.mSpeed);
Ronghua Wu8db88132015-04-22 13:51:35 -07001627 }
Andreas Huber3fe62152011-09-16 15:09:22 -07001628 }
1629
Wei Jiabc2fb722014-07-08 16:37:57 -07001630 if (audio) {
Lajos Molnar1d15ab52015-03-04 16:46:34 -08001631 sp<AMessage> notify = new AMessage(kWhatAudioNotify, this);
Wei Jia88703c32014-08-06 11:24:07 -07001632 ++mAudioDecoderGeneration;
1633 notify->setInt32("generation", mAudioDecoderGeneration);
1634
Wei Jiaa05f1e32016-03-25 16:31:22 -07001635 if (checkAudioModeChange) {
Wei Jia41cd4632016-05-13 10:53:30 -07001636 determineAudioModeChange(format);
Wei Jiaa05f1e32016-03-25 16:31:22 -07001637 }
Wei Jiabc2fb722014-07-08 16:37:57 -07001638 if (mOffloadAudio) {
Wei Jia14532f22015-12-29 11:28:15 -08001639 mSource->setOffloadAudio(true /* offload */);
1640
Haynes Mathew George8b635332015-03-30 17:59:47 -07001641 const bool hasVideo = (mSource->getFormat(false /*audio */) != NULL);
1642 format->setInt32("has-video", hasVideo);
Wei Jiac6cfd702014-11-11 16:33:20 -08001643 *decoder = new DecoderPassThrough(notify, mSource, mRenderer);
Wei Jiabc2fb722014-07-08 16:37:57 -07001644 } else {
Wei Jia14532f22015-12-29 11:28:15 -08001645 mSource->setOffloadAudio(false /* offload */);
1646
Wei Jiaf2ae3e12016-10-27 17:10:59 -07001647 *decoder = new Decoder(notify, mSource, mPID, mUID, mRenderer);
Wei Jiabc2fb722014-07-08 16:37:57 -07001648 }
1649 } else {
Lajos Molnar1d15ab52015-03-04 16:46:34 -08001650 sp<AMessage> notify = new AMessage(kWhatVideoNotify, this);
Wei Jia88703c32014-08-06 11:24:07 -07001651 ++mVideoDecoderGeneration;
1652 notify->setInt32("generation", mVideoDecoderGeneration);
1653
Chong Zhang7137ec72014-11-12 16:41:05 -08001654 *decoder = new Decoder(
Wei Jiaf2ae3e12016-10-27 17:10:59 -07001655 notify, mSource, mPID, mUID, mRenderer, mSurface, mCCDecoder);
Lajos Molnard9fd6312014-11-06 11:00:00 -08001656
1657 // enable FRC if high-quality AV sync is requested, even if not
Lajos Molnar1de1e252015-04-30 18:18:34 -07001658 // directly queuing to display, as this will even improve textureview
Lajos Molnard9fd6312014-11-06 11:00:00 -08001659 // playback.
1660 {
Marco Nelissen96626b72016-12-01 13:58:59 -08001661 if (property_get_bool("persist.sys.media.avsync", false)) {
Lajos Molnard9fd6312014-11-06 11:00:00 -08001662 format->setInt32("auto-frc", 1);
1663 }
1664 }
Wei Jiabc2fb722014-07-08 16:37:57 -07001665 }
Lajos Molnar1cd13982014-01-17 15:12:51 -08001666 (*decoder)->init();
Andreas Huber84066782011-08-16 09:34:26 -07001667 (*decoder)->configure(format);
Andreas Huberf9334412010-12-15 15:17:42 -08001668
Praveen Chavanbbaa1442016-04-08 13:33:49 -07001669 if (!audio) {
1670 sp<AMessage> params = new AMessage();
1671 float rate = getFrameRate();
1672 if (rate > 0) {
1673 params->setFloat("frame-rate-total", rate);
1674 }
1675
1676 sp<MetaData> fileMeta = getFileMeta();
1677 if (fileMeta != NULL) {
1678 int32_t videoTemporalLayerCount;
1679 if (fileMeta->findInt32(kKeyTemporalLayerCount, &videoTemporalLayerCount)
1680 && videoTemporalLayerCount > 0) {
1681 params->setInt32("temporal-layer-count", videoTemporalLayerCount);
1682 }
1683 }
1684
1685 if (params->countEntries() > 0) {
1686 (*decoder)->setParameters(params);
1687 }
1688 }
Andreas Huberf9334412010-12-15 15:17:42 -08001689 return OK;
1690}
1691
Chong Zhangced1c2f2014-08-08 15:22:35 -07001692void NuPlayer::updateVideoSize(
1693 const sp<AMessage> &inputFormat,
1694 const sp<AMessage> &outputFormat) {
1695 if (inputFormat == NULL) {
1696 ALOGW("Unknown video size, reporting 0x0!");
1697 notifyListener(MEDIA_SET_VIDEO_SIZE, 0, 0);
1698 return;
1699 }
Wei Jia9737d342016-12-28 14:59:59 -08001700 int32_t err = OK;
1701 inputFormat->findInt32("err", &err);
1702 if (err == -EWOULDBLOCK) {
1703 ALOGW("Video meta is not available yet!");
1704 return;
1705 }
1706 if (err != OK) {
1707 ALOGW("Something is wrong with video meta!");
1708 return;
1709 }
Chong Zhangced1c2f2014-08-08 15:22:35 -07001710
1711 int32_t displayWidth, displayHeight;
Chong Zhangced1c2f2014-08-08 15:22:35 -07001712 if (outputFormat != NULL) {
1713 int32_t width, height;
1714 CHECK(outputFormat->findInt32("width", &width));
1715 CHECK(outputFormat->findInt32("height", &height));
1716
1717 int32_t cropLeft, cropTop, cropRight, cropBottom;
1718 CHECK(outputFormat->findRect(
1719 "crop",
1720 &cropLeft, &cropTop, &cropRight, &cropBottom));
1721
1722 displayWidth = cropRight - cropLeft + 1;
1723 displayHeight = cropBottom - cropTop + 1;
1724
1725 ALOGV("Video output format changed to %d x %d "
1726 "(crop: %d x %d @ (%d, %d))",
1727 width, height,
1728 displayWidth,
1729 displayHeight,
1730 cropLeft, cropTop);
1731 } else {
1732 CHECK(inputFormat->findInt32("width", &displayWidth));
1733 CHECK(inputFormat->findInt32("height", &displayHeight));
1734
1735 ALOGV("Video input format %d x %d", displayWidth, displayHeight);
1736 }
1737
1738 // Take into account sample aspect ratio if necessary:
1739 int32_t sarWidth, sarHeight;
1740 if (inputFormat->findInt32("sar-width", &sarWidth)
1741 && inputFormat->findInt32("sar-height", &sarHeight)) {
1742 ALOGV("Sample aspect ratio %d : %d", sarWidth, sarHeight);
1743
1744 displayWidth = (displayWidth * sarWidth) / sarHeight;
1745
1746 ALOGV("display dimensions %d x %d", displayWidth, displayHeight);
Wei Jiadf6c6af2016-09-29 11:27:15 -07001747 } else {
1748 int32_t width, height;
1749 if (inputFormat->findInt32("display-width", &width)
1750 && inputFormat->findInt32("display-height", &height)
1751 && width > 0 && height > 0
1752 && displayWidth > 0 && displayHeight > 0) {
1753 if (displayHeight * (int64_t)width / height > (int64_t)displayWidth) {
1754 displayHeight = (int32_t)(displayWidth * (int64_t)height / width);
1755 } else {
1756 displayWidth = (int32_t)(displayHeight * (int64_t)width / height);
1757 }
1758 ALOGV("Video display width and height are overridden to %d x %d",
1759 displayWidth, displayHeight);
1760 }
Chong Zhangced1c2f2014-08-08 15:22:35 -07001761 }
1762
1763 int32_t rotationDegrees;
1764 if (!inputFormat->findInt32("rotation-degrees", &rotationDegrees)) {
1765 rotationDegrees = 0;
1766 }
1767
1768 if (rotationDegrees == 90 || rotationDegrees == 270) {
1769 int32_t tmp = displayWidth;
1770 displayWidth = displayHeight;
1771 displayHeight = tmp;
1772 }
1773
1774 notifyListener(
1775 MEDIA_SET_VIDEO_SIZE,
1776 displayWidth,
1777 displayHeight);
1778}
1779
Chong Zhangdcb89b32013-08-06 09:44:47 -07001780void NuPlayer::notifyListener(int msg, int ext1, int ext2, const Parcel *in) {
Andreas Huber43c3e6c2011-01-05 12:17:08 -08001781 if (mDriver == NULL) {
Andreas Huberf9334412010-12-15 15:17:42 -08001782 return;
1783 }
1784
Andreas Huber43c3e6c2011-01-05 12:17:08 -08001785 sp<NuPlayerDriver> driver = mDriver.promote();
Andreas Huberf9334412010-12-15 15:17:42 -08001786
Andreas Huber43c3e6c2011-01-05 12:17:08 -08001787 if (driver == NULL) {
Andreas Huberf9334412010-12-15 15:17:42 -08001788 return;
1789 }
1790
Chong Zhangdcb89b32013-08-06 09:44:47 -07001791 driver->notifyListener(msg, ext1, ext2, in);
Andreas Huberf9334412010-12-15 15:17:42 -08001792}
1793
Chong Zhang7137ec72014-11-12 16:41:05 -08001794void NuPlayer::flushDecoder(bool audio, bool needShutdown) {
Andreas Huber14f76722013-01-15 09:04:18 -08001795 ALOGV("[%s] flushDecoder needShutdown=%d",
1796 audio ? "audio" : "video", needShutdown);
1797
Chong Zhang7137ec72014-11-12 16:41:05 -08001798 const sp<DecoderBase> &decoder = getDecoder(audio);
Lajos Molnar87603c02014-08-20 19:25:30 -07001799 if (decoder == NULL) {
Steve Blockdf64d152012-01-04 20:05:49 +00001800 ALOGI("flushDecoder %s without decoder present",
Andreas Huber6e3d3112011-11-28 12:36:11 -08001801 audio ? "audio" : "video");
Lajos Molnar87603c02014-08-20 19:25:30 -07001802 return;
Andreas Huber6e3d3112011-11-28 12:36:11 -08001803 }
1804
Andreas Huber1aef2112011-01-04 14:01:29 -08001805 // Make sure we don't continue to scan sources until we finish flushing.
1806 ++mScanSourcesGeneration;
Chong Zhang3b032b32015-04-17 15:49:06 -07001807 if (mScanSourcesPending) {
Toshikazu Saitocbcbb792015-09-15 14:53:01 +09001808 if (!needShutdown) {
1809 mDeferredActions.push_back(
1810 new SimpleAction(&NuPlayer::performScanSources));
1811 }
Chong Zhang3b032b32015-04-17 15:49:06 -07001812 mScanSourcesPending = false;
1813 }
Andreas Huber1aef2112011-01-04 14:01:29 -08001814
Chong Zhang7137ec72014-11-12 16:41:05 -08001815 decoder->signalFlush();
Andreas Huber1aef2112011-01-04 14:01:29 -08001816
1817 FlushStatus newStatus =
1818 needShutdown ? FLUSHING_DECODER_SHUTDOWN : FLUSHING_DECODER;
1819
Lajos Molnarfcd3e942015-03-31 10:06:48 -07001820 mFlushComplete[audio][false /* isDecoder */] = (mRenderer == NULL);
Andy Hung8d121d42014-10-03 09:53:53 -07001821 mFlushComplete[audio][true /* isDecoder */] = false;
Andreas Huber1aef2112011-01-04 14:01:29 -08001822 if (audio) {
Wei Jia53904f32014-07-29 10:22:53 -07001823 ALOGE_IF(mFlushingAudio != NONE,
1824 "audio flushDecoder() is called in state %d", mFlushingAudio);
Andreas Huber1aef2112011-01-04 14:01:29 -08001825 mFlushingAudio = newStatus;
Andreas Huber1aef2112011-01-04 14:01:29 -08001826 } else {
Wei Jia53904f32014-07-29 10:22:53 -07001827 ALOGE_IF(mFlushingVideo != NONE,
1828 "video flushDecoder() is called in state %d", mFlushingVideo);
Andreas Huber1aef2112011-01-04 14:01:29 -08001829 mFlushingVideo = newStatus;
Andreas Huber1aef2112011-01-04 14:01:29 -08001830 }
1831}
1832
Chong Zhangced1c2f2014-08-08 15:22:35 -07001833void NuPlayer::queueDecoderShutdown(
1834 bool audio, bool video, const sp<AMessage> &reply) {
1835 ALOGI("queueDecoderShutdown audio=%d, video=%d", audio, video);
Andreas Huber84066782011-08-16 09:34:26 -07001836
Chong Zhangced1c2f2014-08-08 15:22:35 -07001837 mDeferredActions.push_back(
Wei Jiafef808d2014-10-31 17:57:05 -07001838 new FlushDecoderAction(
1839 audio ? FLUSH_CMD_SHUTDOWN : FLUSH_CMD_NONE,
1840 video ? FLUSH_CMD_SHUTDOWN : FLUSH_CMD_NONE));
Andreas Huber84066782011-08-16 09:34:26 -07001841
Chong Zhangced1c2f2014-08-08 15:22:35 -07001842 mDeferredActions.push_back(
1843 new SimpleAction(&NuPlayer::performScanSources));
Andreas Huber84066782011-08-16 09:34:26 -07001844
Chong Zhangced1c2f2014-08-08 15:22:35 -07001845 mDeferredActions.push_back(new PostMessageAction(reply));
1846
1847 processDeferredActions();
Andreas Huber84066782011-08-16 09:34:26 -07001848}
1849
James Dong0d268a32012-08-31 12:18:27 -07001850status_t NuPlayer::setVideoScalingMode(int32_t mode) {
1851 mVideoScalingMode = mode;
Lajos Molnar1de1e252015-04-30 18:18:34 -07001852 if (mSurface != NULL) {
1853 status_t ret = native_window_set_scaling_mode(mSurface.get(), mVideoScalingMode);
James Dong0d268a32012-08-31 12:18:27 -07001854 if (ret != OK) {
1855 ALOGE("Failed to set scaling mode (%d): %s",
1856 -ret, strerror(-ret));
1857 return ret;
1858 }
1859 }
1860 return OK;
1861}
1862
Chong Zhangdcb89b32013-08-06 09:44:47 -07001863status_t NuPlayer::getTrackInfo(Parcel* reply) const {
Lajos Molnar1d15ab52015-03-04 16:46:34 -08001864 sp<AMessage> msg = new AMessage(kWhatGetTrackInfo, this);
Chong Zhangdcb89b32013-08-06 09:44:47 -07001865 msg->setPointer("reply", reply);
1866
1867 sp<AMessage> response;
1868 status_t err = msg->postAndAwaitResponse(&response);
1869 return err;
1870}
1871
Robert Shih7c4f0d72014-07-09 18:53:31 -07001872status_t NuPlayer::getSelectedTrack(int32_t type, Parcel* reply) const {
Lajos Molnar1d15ab52015-03-04 16:46:34 -08001873 sp<AMessage> msg = new AMessage(kWhatGetSelectedTrack, this);
Robert Shih7c4f0d72014-07-09 18:53:31 -07001874 msg->setPointer("reply", reply);
1875 msg->setInt32("type", type);
1876
1877 sp<AMessage> response;
1878 status_t err = msg->postAndAwaitResponse(&response);
1879 if (err == OK && response != NULL) {
1880 CHECK(response->findInt32("err", &err));
1881 }
1882 return err;
1883}
1884
Robert Shih6ffb1fd2014-10-29 16:24:32 -07001885status_t NuPlayer::selectTrack(size_t trackIndex, bool select, int64_t timeUs) {
Lajos Molnar1d15ab52015-03-04 16:46:34 -08001886 sp<AMessage> msg = new AMessage(kWhatSelectTrack, this);
Chong Zhangdcb89b32013-08-06 09:44:47 -07001887 msg->setSize("trackIndex", trackIndex);
1888 msg->setInt32("select", select);
Robert Shih6ffb1fd2014-10-29 16:24:32 -07001889 msg->setInt64("timeUs", timeUs);
Chong Zhangdcb89b32013-08-06 09:44:47 -07001890
1891 sp<AMessage> response;
1892 status_t err = msg->postAndAwaitResponse(&response);
1893
Chong Zhang404fced2014-06-11 14:45:31 -07001894 if (err != OK) {
1895 return err;
1896 }
1897
1898 if (!response->findInt32("err", &err)) {
1899 err = OK;
1900 }
1901
Chong Zhangdcb89b32013-08-06 09:44:47 -07001902 return err;
1903}
1904
Ronghua Wua73d9e02014-10-08 15:13:29 -07001905status_t NuPlayer::getCurrentPosition(int64_t *mediaUs) {
1906 sp<Renderer> renderer = mRenderer;
1907 if (renderer == NULL) {
1908 return NO_INIT;
1909 }
1910
1911 return renderer->getCurrentPosition(mediaUs);
1912}
1913
Praveen Chavane1e5d7a2015-05-19 19:09:48 -07001914void NuPlayer::getStats(Vector<sp<AMessage> > *mTrackStats) {
1915 CHECK(mTrackStats != NULL);
1916
1917 mTrackStats->clear();
1918 if (mVideoDecoder != NULL) {
1919 mTrackStats->push_back(mVideoDecoder->getStats());
1920 }
1921 if (mAudioDecoder != NULL) {
1922 mTrackStats->push_back(mAudioDecoder->getStats());
Chong Zhang7137ec72014-11-12 16:41:05 -08001923 }
Ronghua Wua73d9e02014-10-08 15:13:29 -07001924}
1925
Marco Nelissenf0b72b52014-09-16 15:43:44 -07001926sp<MetaData> NuPlayer::getFileMeta() {
1927 return mSource->getFileFormatMeta();
1928}
1929
Ronghua Wuc8a70d32015-04-29 16:26:34 -07001930float NuPlayer::getFrameRate() {
1931 sp<MetaData> meta = mSource->getFormatMeta(false /* audio */);
1932 if (meta == NULL) {
1933 return 0;
1934 }
1935 int32_t rate;
1936 if (!meta->findInt32(kKeyFrameRate, &rate)) {
1937 // fall back to try file meta
1938 sp<MetaData> fileMeta = getFileMeta();
1939 if (fileMeta == NULL) {
1940 ALOGW("source has video meta but not file meta");
1941 return -1;
1942 }
1943 int32_t fileMetaRate;
1944 if (!fileMeta->findInt32(kKeyFrameRate, &fileMetaRate)) {
1945 return -1;
1946 }
1947 return fileMetaRate;
1948 }
1949 return rate;
1950}
1951
Andreas Huberb7c8e912012-11-27 15:02:53 -08001952void NuPlayer::schedulePollDuration() {
Lajos Molnar1d15ab52015-03-04 16:46:34 -08001953 sp<AMessage> msg = new AMessage(kWhatPollDuration, this);
Andreas Huberb7c8e912012-11-27 15:02:53 -08001954 msg->setInt32("generation", mPollDurationGeneration);
1955 msg->post();
1956}
1957
1958void NuPlayer::cancelPollDuration() {
1959 ++mPollDurationGeneration;
1960}
1961
Andreas Hubera1f8ab02012-11-30 10:53:22 -08001962void NuPlayer::processDeferredActions() {
1963 while (!mDeferredActions.empty()) {
1964 // We won't execute any deferred actions until we're no longer in
1965 // an intermediate state, i.e. one more more decoders are currently
1966 // flushing or shutting down.
1967
Andreas Hubera1f8ab02012-11-30 10:53:22 -08001968 if (mFlushingAudio != NONE || mFlushingVideo != NONE) {
1969 // We're currently flushing, postpone the reset until that's
1970 // completed.
1971
1972 ALOGV("postponing action mFlushingAudio=%d, mFlushingVideo=%d",
1973 mFlushingAudio, mFlushingVideo);
1974
1975 break;
1976 }
1977
1978 sp<Action> action = *mDeferredActions.begin();
1979 mDeferredActions.erase(mDeferredActions.begin());
1980
1981 action->execute(this);
1982 }
1983}
1984
Wei Jiac5de0912016-11-18 10:22:14 -08001985void NuPlayer::performSeek(int64_t seekTimeUs, MediaPlayerSeekMode mode) {
1986 ALOGV("performSeek seekTimeUs=%lld us (%.2f secs), mode=%d",
1987 (long long)seekTimeUs, seekTimeUs / 1E6, mode);
Andreas Hubera1f8ab02012-11-30 10:53:22 -08001988
Andy Hungadf34bf2014-09-03 18:22:22 -07001989 if (mSource == NULL) {
1990 // This happens when reset occurs right before the loop mode
1991 // asynchronously seeks to the start of the stream.
1992 LOG_ALWAYS_FATAL_IF(mAudioDecoder != NULL || mVideoDecoder != NULL,
1993 "mSource is NULL and decoders not NULL audio(%p) video(%p)",
1994 mAudioDecoder.get(), mVideoDecoder.get());
1995 return;
1996 }
Robert Shih1a5c8592015-08-04 18:07:44 -07001997 mPreviousSeekTimeUs = seekTimeUs;
Wei Jiac5de0912016-11-18 10:22:14 -08001998 mSource->seekTo(seekTimeUs, mode);
Robert Shihd3b0bbb2014-07-23 15:00:25 -07001999 ++mTimedTextGeneration;
Andreas Hubera1f8ab02012-11-30 10:53:22 -08002000
Andreas Hubera1f8ab02012-11-30 10:53:22 -08002001 // everything's flushed, continue playback.
2002}
2003
Wei Jiafef808d2014-10-31 17:57:05 -07002004void NuPlayer::performDecoderFlush(FlushCommand audio, FlushCommand video) {
2005 ALOGV("performDecoderFlush audio=%d, video=%d", audio, video);
Andreas Hubera1f8ab02012-11-30 10:53:22 -08002006
Wei Jiafef808d2014-10-31 17:57:05 -07002007 if ((audio == FLUSH_CMD_NONE || mAudioDecoder == NULL)
2008 && (video == FLUSH_CMD_NONE || mVideoDecoder == NULL)) {
Andreas Hubera1f8ab02012-11-30 10:53:22 -08002009 return;
2010 }
2011
Wei Jiafef808d2014-10-31 17:57:05 -07002012 if (audio != FLUSH_CMD_NONE && mAudioDecoder != NULL) {
2013 flushDecoder(true /* audio */, (audio == FLUSH_CMD_SHUTDOWN));
Andreas Hubera1f8ab02012-11-30 10:53:22 -08002014 }
2015
Wei Jiafef808d2014-10-31 17:57:05 -07002016 if (video != FLUSH_CMD_NONE && mVideoDecoder != NULL) {
2017 flushDecoder(false /* audio */, (video == FLUSH_CMD_SHUTDOWN));
Andreas Hubera1f8ab02012-11-30 10:53:22 -08002018 }
2019}
2020
2021void NuPlayer::performReset() {
2022 ALOGV("performReset");
2023
2024 CHECK(mAudioDecoder == NULL);
2025 CHECK(mVideoDecoder == NULL);
2026
2027 cancelPollDuration();
2028
2029 ++mScanSourcesGeneration;
2030 mScanSourcesPending = false;
2031
Lajos Molnar09524832014-07-17 14:29:51 -07002032 if (mRendererLooper != NULL) {
2033 if (mRenderer != NULL) {
2034 mRendererLooper->unregisterHandler(mRenderer->id());
2035 }
2036 mRendererLooper->stop();
2037 mRendererLooper.clear();
2038 }
Andreas Hubera1f8ab02012-11-30 10:53:22 -08002039 mRenderer.clear();
Wei Jia57568df2014-09-22 10:16:29 -07002040 ++mRendererGeneration;
Andreas Hubera1f8ab02012-11-30 10:53:22 -08002041
2042 if (mSource != NULL) {
2043 mSource->stop();
Andreas Huberb5f25f02013-02-05 10:14:26 -08002044
Wei Jiac45a4b22016-04-15 15:30:23 -07002045 Mutex::Autolock autoLock(mSourceLock);
Andreas Hubera1f8ab02012-11-30 10:53:22 -08002046 mSource.clear();
2047 }
2048
2049 if (mDriver != NULL) {
2050 sp<NuPlayerDriver> driver = mDriver.promote();
2051 if (driver != NULL) {
2052 driver->notifyResetComplete();
2053 }
2054 }
Andreas Huber57a339c2012-12-03 11:18:00 -08002055
2056 mStarted = false;
Wei Jia8a092d32016-06-03 14:57:24 -07002057 mPrepared = false;
Ronghua Wu64c2d172015-10-07 16:52:19 -07002058 mResetting = false;
Robert Shih0c61a0d2015-07-06 15:09:10 -07002059 mSourceStarted = false;
Andreas Hubera1f8ab02012-11-30 10:53:22 -08002060}
2061
2062void NuPlayer::performScanSources() {
2063 ALOGV("performScanSources");
2064
Andreas Huber57a339c2012-12-03 11:18:00 -08002065 if (!mStarted) {
2066 return;
2067 }
2068
Andreas Hubera1f8ab02012-11-30 10:53:22 -08002069 if (mAudioDecoder == NULL || mVideoDecoder == NULL) {
2070 postScanSources();
2071 }
2072}
2073
Lajos Molnar1de1e252015-04-30 18:18:34 -07002074void NuPlayer::performSetSurface(const sp<Surface> &surface) {
Andreas Huber57a339c2012-12-03 11:18:00 -08002075 ALOGV("performSetSurface");
2076
Lajos Molnar1de1e252015-04-30 18:18:34 -07002077 mSurface = surface;
Andreas Huber57a339c2012-12-03 11:18:00 -08002078
2079 // XXX - ignore error from setVideoScalingMode for now
2080 setVideoScalingMode(mVideoScalingMode);
Chong Zhang13d6faa2014-08-22 15:35:28 -07002081
2082 if (mDriver != NULL) {
2083 sp<NuPlayerDriver> driver = mDriver.promote();
2084 if (driver != NULL) {
2085 driver->notifySetSurfaceComplete();
2086 }
2087 }
Andreas Huber57a339c2012-12-03 11:18:00 -08002088}
2089
Chong Zhangf8d71772014-11-26 15:08:34 -08002090void NuPlayer::performResumeDecoders(bool needNotify) {
2091 if (needNotify) {
2092 mResumePending = true;
2093 if (mVideoDecoder == NULL) {
2094 // if audio-only, we can notify seek complete now,
2095 // as the resume operation will be relatively fast.
2096 finishResume();
2097 }
2098 }
2099
Chong Zhang7137ec72014-11-12 16:41:05 -08002100 if (mVideoDecoder != NULL) {
Chong Zhangf8d71772014-11-26 15:08:34 -08002101 // When there is continuous seek, MediaPlayer will cache the seek
2102 // position, and send down new seek request when previous seek is
2103 // complete. Let's wait for at least one video output frame before
2104 // notifying seek complete, so that the video thumbnail gets updated
2105 // when seekbar is dragged.
2106 mVideoDecoder->signalResume(needNotify);
Chong Zhang7137ec72014-11-12 16:41:05 -08002107 }
2108
2109 if (mAudioDecoder != NULL) {
Chong Zhangf8d71772014-11-26 15:08:34 -08002110 mAudioDecoder->signalResume(false /* needNotify */);
2111 }
2112}
2113
2114void NuPlayer::finishResume() {
2115 if (mResumePending) {
2116 mResumePending = false;
Wei Jia1061c9c2015-05-19 16:02:17 -07002117 notifyDriverSeekComplete();
2118 }
2119}
2120
2121void NuPlayer::notifyDriverSeekComplete() {
2122 if (mDriver != NULL) {
2123 sp<NuPlayerDriver> driver = mDriver.promote();
2124 if (driver != NULL) {
2125 driver->notifySeekComplete();
Chong Zhangf8d71772014-11-26 15:08:34 -08002126 }
Chong Zhang7137ec72014-11-12 16:41:05 -08002127 }
2128}
2129
Andreas Huber9575c962013-02-05 13:59:56 -08002130void NuPlayer::onSourceNotify(const sp<AMessage> &msg) {
2131 int32_t what;
2132 CHECK(msg->findInt32("what", &what));
2133
2134 switch (what) {
Lajos Molnarfcd3e942015-03-31 10:06:48 -07002135 case Source::kWhatInstantiateSecureDecoders:
2136 {
2137 if (mSource == NULL) {
2138 // This is a stale notification from a source that was
2139 // asynchronously preparing when the client called reset().
2140 // We handled the reset, the source is gone.
2141 break;
2142 }
2143
2144 sp<AMessage> reply;
2145 CHECK(msg->findMessage("reply", &reply));
2146 status_t err = onInstantiateSecureDecoders();
2147 reply->setInt32("err", err);
2148 reply->post();
2149 break;
2150 }
2151
Andreas Huber9575c962013-02-05 13:59:56 -08002152 case Source::kWhatPrepared:
2153 {
Andreas Huberb5f28d42013-04-25 15:11:19 -07002154 if (mSource == NULL) {
2155 // This is a stale notification from a source that was
2156 // asynchronously preparing when the client called reset().
2157 // We handled the reset, the source is gone.
2158 break;
2159 }
2160
Andreas Huberec0c5972013-02-05 14:47:13 -08002161 int32_t err;
2162 CHECK(msg->findInt32("err", &err));
2163
Lajos Molnarfcd3e942015-03-31 10:06:48 -07002164 if (err != OK) {
2165 // shut down potential secure codecs in case client never calls reset
2166 mDeferredActions.push_back(
2167 new FlushDecoderAction(FLUSH_CMD_SHUTDOWN /* audio */,
2168 FLUSH_CMD_SHUTDOWN /* video */));
2169 processDeferredActions();
Wei Jia8a092d32016-06-03 14:57:24 -07002170 } else {
2171 mPrepared = true;
Lajos Molnarfcd3e942015-03-31 10:06:48 -07002172 }
2173
Andreas Huber9575c962013-02-05 13:59:56 -08002174 sp<NuPlayerDriver> driver = mDriver.promote();
2175 if (driver != NULL) {
Marco Nelissendd114d12014-05-28 15:23:14 -07002176 // notify duration first, so that it's definitely set when
2177 // the app received the "prepare complete" callback.
2178 int64_t durationUs;
2179 if (mSource->getDuration(&durationUs) == OK) {
2180 driver->notifyDuration(durationUs);
2181 }
Andreas Huberec0c5972013-02-05 14:47:13 -08002182 driver->notifyPrepareCompleted(err);
Andreas Huber9575c962013-02-05 13:59:56 -08002183 }
Andreas Huber99759402013-04-01 14:28:31 -07002184
Andreas Huber9575c962013-02-05 13:59:56 -08002185 break;
2186 }
2187
2188 case Source::kWhatFlagsChanged:
2189 {
2190 uint32_t flags;
2191 CHECK(msg->findInt32("flags", (int32_t *)&flags));
2192
Chong Zhang4b7069d2013-09-11 12:52:43 -07002193 sp<NuPlayerDriver> driver = mDriver.promote();
2194 if (driver != NULL) {
Wei Jia895651b2014-12-10 17:31:52 -08002195 if ((flags & NuPlayer::Source::FLAG_CAN_SEEK) == 0) {
2196 driver->notifyListener(
2197 MEDIA_INFO, MEDIA_INFO_NOT_SEEKABLE, 0);
2198 }
Chong Zhang4b7069d2013-09-11 12:52:43 -07002199 driver->notifyFlagsChanged(flags);
2200 }
2201
Andreas Huber9575c962013-02-05 13:59:56 -08002202 if ((mSourceFlags & Source::FLAG_DYNAMIC_DURATION)
2203 && (!(flags & Source::FLAG_DYNAMIC_DURATION))) {
2204 cancelPollDuration();
2205 } else if (!(mSourceFlags & Source::FLAG_DYNAMIC_DURATION)
2206 && (flags & Source::FLAG_DYNAMIC_DURATION)
2207 && (mAudioDecoder != NULL || mVideoDecoder != NULL)) {
2208 schedulePollDuration();
2209 }
2210
2211 mSourceFlags = flags;
2212 break;
2213 }
2214
2215 case Source::kWhatVideoSizeChanged:
2216 {
Chong Zhangced1c2f2014-08-08 15:22:35 -07002217 sp<AMessage> format;
2218 CHECK(msg->findMessage("format", &format));
Andreas Huber9575c962013-02-05 13:59:56 -08002219
Chong Zhangced1c2f2014-08-08 15:22:35 -07002220 updateVideoSize(format);
Andreas Huber9575c962013-02-05 13:59:56 -08002221 break;
2222 }
2223
Chong Zhang2a3cc9a2014-08-21 17:48:26 -07002224 case Source::kWhatBufferingUpdate:
2225 {
2226 int32_t percentage;
2227 CHECK(msg->findInt32("percentage", &percentage));
2228
2229 notifyListener(MEDIA_BUFFERING_UPDATE, percentage, 0);
2230 break;
2231 }
2232
Chong Zhangefbb6192015-01-30 17:13:27 -08002233 case Source::kWhatPauseOnBufferingStart:
2234 {
2235 // ignore if not playing
Chong Zhang8a048332015-05-06 15:16:28 -07002236 if (mStarted) {
Chong Zhangefbb6192015-01-30 17:13:27 -08002237 ALOGI("buffer low, pausing...");
2238
Chong Zhang8a048332015-05-06 15:16:28 -07002239 mPausedForBuffering = true;
Chong Zhangefbb6192015-01-30 17:13:27 -08002240 onPause();
2241 }
Wei Jiabfe82072016-05-20 09:21:50 -07002242 notifyListener(MEDIA_INFO, MEDIA_INFO_BUFFERING_START, 0);
Roger Jönssonb50e83e2013-01-21 16:26:41 +01002243 break;
2244 }
2245
Chong Zhangefbb6192015-01-30 17:13:27 -08002246 case Source::kWhatResumeOnBufferingEnd:
2247 {
2248 // ignore if not playing
Chong Zhang8a048332015-05-06 15:16:28 -07002249 if (mStarted) {
Chong Zhangefbb6192015-01-30 17:13:27 -08002250 ALOGI("buffer ready, resuming...");
2251
Chong Zhang8a048332015-05-06 15:16:28 -07002252 mPausedForBuffering = false;
2253
2254 // do not resume yet if client didn't unpause
2255 if (!mPausedByClient) {
2256 onResume();
2257 }
Chong Zhangefbb6192015-01-30 17:13:27 -08002258 }
Wei Jia3bed45a2016-02-17 11:06:47 -08002259 notifyListener(MEDIA_INFO, MEDIA_INFO_BUFFERING_END, 0);
Roger Jönssonb50e83e2013-01-21 16:26:41 +01002260 break;
2261 }
2262
Chong Zhangefbb6192015-01-30 17:13:27 -08002263 case Source::kWhatCacheStats:
2264 {
2265 int32_t kbps;
2266 CHECK(msg->findInt32("bandwidth", &kbps));
2267
2268 notifyListener(MEDIA_INFO, MEDIA_INFO_NETWORK_BANDWIDTH, kbps);
2269 break;
2270 }
2271
Chong Zhangdcb89b32013-08-06 09:44:47 -07002272 case Source::kWhatSubtitleData:
2273 {
2274 sp<ABuffer> buffer;
2275 CHECK(msg->findBuffer("buffer", &buffer));
2276
Chong Zhang404fced2014-06-11 14:45:31 -07002277 sendSubtitleData(buffer, 0 /* baseIndex */);
Chong Zhangdcb89b32013-08-06 09:44:47 -07002278 break;
2279 }
2280
Robert Shih08528432015-04-08 09:06:54 -07002281 case Source::kWhatTimedMetaData:
2282 {
2283 sp<ABuffer> buffer;
2284 if (!msg->findBuffer("buffer", &buffer)) {
2285 notifyListener(MEDIA_INFO, MEDIA_INFO_METADATA_UPDATE, 0);
2286 } else {
2287 sendTimedMetaData(buffer);
2288 }
2289 break;
2290 }
2291
Robert Shihd3b0bbb2014-07-23 15:00:25 -07002292 case Source::kWhatTimedTextData:
2293 {
2294 int32_t generation;
2295 if (msg->findInt32("generation", &generation)
2296 && generation != mTimedTextGeneration) {
2297 break;
2298 }
2299
2300 sp<ABuffer> buffer;
2301 CHECK(msg->findBuffer("buffer", &buffer));
2302
2303 sp<NuPlayerDriver> driver = mDriver.promote();
2304 if (driver == NULL) {
2305 break;
2306 }
2307
2308 int posMs;
2309 int64_t timeUs, posUs;
2310 driver->getCurrentPosition(&posMs);
Patrik2 Carlsson24d484b2015-01-27 16:49:45 +01002311 posUs = (int64_t) posMs * 1000ll;
Robert Shihd3b0bbb2014-07-23 15:00:25 -07002312 CHECK(buffer->meta()->findInt64("timeUs", &timeUs));
2313
2314 if (posUs < timeUs) {
2315 if (!msg->findInt32("generation", &generation)) {
2316 msg->setInt32("generation", mTimedTextGeneration);
2317 }
2318 msg->post(timeUs - posUs);
2319 } else {
2320 sendTimedTextData(buffer);
2321 }
2322 break;
2323 }
2324
Andreas Huber14f76722013-01-15 09:04:18 -08002325 case Source::kWhatQueueDecoderShutdown:
2326 {
2327 int32_t audio, video;
2328 CHECK(msg->findInt32("audio", &audio));
2329 CHECK(msg->findInt32("video", &video));
2330
2331 sp<AMessage> reply;
2332 CHECK(msg->findMessage("reply", &reply));
2333
2334 queueDecoderShutdown(audio, video, reply);
2335 break;
2336 }
2337
Ronghua Wu80276872014-08-28 15:50:29 -07002338 case Source::kWhatDrmNoLicense:
2339 {
2340 notifyListener(MEDIA_ERROR, MEDIA_ERROR_UNKNOWN, ERROR_DRM_NO_LICENSE);
2341 break;
2342 }
2343
Andreas Huber9575c962013-02-05 13:59:56 -08002344 default:
2345 TRESPASS();
2346 }
2347}
2348
Chong Zhanga7fa1d92014-06-11 14:49:23 -07002349void NuPlayer::onClosedCaptionNotify(const sp<AMessage> &msg) {
2350 int32_t what;
2351 CHECK(msg->findInt32("what", &what));
2352
2353 switch (what) {
2354 case NuPlayer::CCDecoder::kWhatClosedCaptionData:
2355 {
2356 sp<ABuffer> buffer;
2357 CHECK(msg->findBuffer("buffer", &buffer));
2358
2359 size_t inbandTracks = 0;
2360 if (mSource != NULL) {
2361 inbandTracks = mSource->getTrackCount();
2362 }
2363
2364 sendSubtitleData(buffer, inbandTracks);
2365 break;
2366 }
2367
2368 case NuPlayer::CCDecoder::kWhatTrackAdded:
2369 {
2370 notifyListener(MEDIA_INFO, MEDIA_INFO_METADATA_UPDATE, 0);
2371
2372 break;
2373 }
2374
2375 default:
2376 TRESPASS();
2377 }
2378
2379
2380}
2381
Chong Zhang404fced2014-06-11 14:45:31 -07002382void NuPlayer::sendSubtitleData(const sp<ABuffer> &buffer, int32_t baseIndex) {
2383 int32_t trackIndex;
2384 int64_t timeUs, durationUs;
2385 CHECK(buffer->meta()->findInt32("trackIndex", &trackIndex));
2386 CHECK(buffer->meta()->findInt64("timeUs", &timeUs));
2387 CHECK(buffer->meta()->findInt64("durationUs", &durationUs));
2388
2389 Parcel in;
2390 in.writeInt32(trackIndex + baseIndex);
2391 in.writeInt64(timeUs);
2392 in.writeInt64(durationUs);
2393 in.writeInt32(buffer->size());
2394 in.writeInt32(buffer->size());
2395 in.write(buffer->data(), buffer->size());
2396
2397 notifyListener(MEDIA_SUBTITLE_DATA, 0, 0, &in);
2398}
Robert Shihd3b0bbb2014-07-23 15:00:25 -07002399
Robert Shih08528432015-04-08 09:06:54 -07002400void NuPlayer::sendTimedMetaData(const sp<ABuffer> &buffer) {
2401 int64_t timeUs;
2402 CHECK(buffer->meta()->findInt64("timeUs", &timeUs));
2403
2404 Parcel in;
2405 in.writeInt64(timeUs);
2406 in.writeInt32(buffer->size());
2407 in.writeInt32(buffer->size());
2408 in.write(buffer->data(), buffer->size());
2409
2410 notifyListener(MEDIA_META_DATA, 0, 0, &in);
2411}
2412
Robert Shihd3b0bbb2014-07-23 15:00:25 -07002413void NuPlayer::sendTimedTextData(const sp<ABuffer> &buffer) {
2414 const void *data;
2415 size_t size = 0;
2416 int64_t timeUs;
Marco Nelissen55e2f4c2015-09-04 15:57:15 -07002417 int32_t flag = TextDescriptions::IN_BAND_TEXT_3GPP;
Robert Shihd3b0bbb2014-07-23 15:00:25 -07002418
2419 AString mime;
2420 CHECK(buffer->meta()->findString("mime", &mime));
2421 CHECK(strcasecmp(mime.c_str(), MEDIA_MIMETYPE_TEXT_3GPP) == 0);
2422
2423 data = buffer->data();
2424 size = buffer->size();
2425
2426 Parcel parcel;
2427 if (size > 0) {
2428 CHECK(buffer->meta()->findInt64("timeUs", &timeUs));
Marco Nelissen55e2f4c2015-09-04 15:57:15 -07002429 int32_t global = 0;
2430 if (buffer->meta()->findInt32("global", &global) && global) {
2431 flag |= TextDescriptions::GLOBAL_DESCRIPTIONS;
2432 } else {
2433 flag |= TextDescriptions::LOCAL_DESCRIPTIONS;
2434 }
Robert Shihd3b0bbb2014-07-23 15:00:25 -07002435 TextDescriptions::getParcelOfDescriptions(
2436 (const uint8_t *)data, size, flag, timeUs / 1000, &parcel);
2437 }
2438
2439 if ((parcel.dataSize() > 0)) {
2440 notifyListener(MEDIA_TIMED_TEXT, 0, 0, &parcel);
2441 } else { // send an empty timed text
2442 notifyListener(MEDIA_TIMED_TEXT, 0, 0);
2443 }
2444}
Andreas Huberb5f25f02013-02-05 10:14:26 -08002445////////////////////////////////////////////////////////////////////////////////
2446
Chong Zhangced1c2f2014-08-08 15:22:35 -07002447sp<AMessage> NuPlayer::Source::getFormat(bool audio) {
2448 sp<MetaData> meta = getFormatMeta(audio);
2449
2450 if (meta == NULL) {
2451 return NULL;
2452 }
2453
2454 sp<AMessage> msg = new AMessage;
2455
2456 if(convertMetaDataToMessage(meta, &msg) == OK) {
2457 return msg;
2458 }
2459 return NULL;
2460}
2461
Andreas Huber9575c962013-02-05 13:59:56 -08002462void NuPlayer::Source::notifyFlagsChanged(uint32_t flags) {
2463 sp<AMessage> notify = dupNotify();
2464 notify->setInt32("what", kWhatFlagsChanged);
2465 notify->setInt32("flags", flags);
2466 notify->post();
2467}
2468
Chong Zhangced1c2f2014-08-08 15:22:35 -07002469void NuPlayer::Source::notifyVideoSizeChanged(const sp<AMessage> &format) {
Andreas Huber9575c962013-02-05 13:59:56 -08002470 sp<AMessage> notify = dupNotify();
2471 notify->setInt32("what", kWhatVideoSizeChanged);
Chong Zhangced1c2f2014-08-08 15:22:35 -07002472 notify->setMessage("format", format);
Andreas Huber9575c962013-02-05 13:59:56 -08002473 notify->post();
2474}
2475
Andreas Huberec0c5972013-02-05 14:47:13 -08002476void NuPlayer::Source::notifyPrepared(status_t err) {
Andreas Huber9575c962013-02-05 13:59:56 -08002477 sp<AMessage> notify = dupNotify();
2478 notify->setInt32("what", kWhatPrepared);
Andreas Huberec0c5972013-02-05 14:47:13 -08002479 notify->setInt32("err", err);
Andreas Huber9575c962013-02-05 13:59:56 -08002480 notify->post();
2481}
2482
Lajos Molnarfcd3e942015-03-31 10:06:48 -07002483void NuPlayer::Source::notifyInstantiateSecureDecoders(const sp<AMessage> &reply) {
2484 sp<AMessage> notify = dupNotify();
2485 notify->setInt32("what", kWhatInstantiateSecureDecoders);
2486 notify->setMessage("reply", reply);
2487 notify->post();
2488}
2489
Andreas Huber84333e02014-02-07 15:36:10 -08002490void NuPlayer::Source::onMessageReceived(const sp<AMessage> & /* msg */) {
Andreas Huberb5f25f02013-02-05 10:14:26 -08002491 TRESPASS();
2492}
2493
Andreas Huberf9334412010-12-15 15:17:42 -08002494} // namespace android