blob: 90b7b7f1ed032707db2aa4da9d41f7f83f1d58e9 [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 Jia14486822016-11-02 17:51:30 -070073 explicit SeekAction(int64_t seekTimeUs, bool precise)
74 : mSeekTimeUs(seekTimeUs),
75 mPrecise(precise) {
Andreas Hubera1f8ab02012-11-30 10:53:22 -080076 }
77
78 virtual void execute(NuPlayer *player) {
Wei Jia14486822016-11-02 17:51:30 -070079 player->performSeek(mSeekTimeUs, mPrecise);
Andreas Hubera1f8ab02012-11-30 10:53:22 -080080 }
81
82private:
83 int64_t mSeekTimeUs;
Wei Jia14486822016-11-02 17:51:30 -070084 bool mPrecise;
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);
266 // Don't set FLAG_SECURE on mSourceFlags here for widevine.
267 // The correct flags will be updated in Source::kWhatFlagsChanged
268 // handler when GenericSource is prepared.
Andreas Huber2bfdd422011-10-11 15:24:07 -0700269
Chong Zhanga19f33e2014-08-07 15:35:07 -0700270 status_t err = genericSource->setDataSource(httpService, url, headers);
Chong Zhang3de157d2014-08-05 20:54:44 -0700271
272 if (err == OK) {
273 source = genericSource;
274 } else {
Chong Zhanga19f33e2014-08-07 15:35:07 -0700275 ALOGE("Failed to set data source!");
Chong Zhang3de157d2014-08-05 20:54:44 -0700276 }
277 }
Andreas Huberafed0e12011-09-20 15:39:58 -0700278 msg->setObject("source", source);
279 msg->post();
280}
281
Andreas Huber9575c962013-02-05 13:59:56 -0800282void NuPlayer::setDataSourceAsync(int fd, int64_t offset, int64_t length) {
Lajos Molnar1d15ab52015-03-04 16:46:34 -0800283 sp<AMessage> msg = new AMessage(kWhatSetDataSource, this);
Andreas Huberafed0e12011-09-20 15:39:58 -0700284
Lajos Molnar1d15ab52015-03-04 16:46:34 -0800285 sp<AMessage> notify = new AMessage(kWhatSourceNotify, this);
Andreas Huberb5f25f02013-02-05 10:14:26 -0800286
Chong Zhang3de157d2014-08-05 20:54:44 -0700287 sp<GenericSource> source =
288 new GenericSource(notify, mUIDValid, mUID);
289
Chong Zhanga19f33e2014-08-07 15:35:07 -0700290 status_t err = source->setDataSource(fd, offset, length);
Chong Zhang3de157d2014-08-05 20:54:44 -0700291
292 if (err != OK) {
Chong Zhanga19f33e2014-08-07 15:35:07 -0700293 ALOGE("Failed to set data source!");
Chong Zhang3de157d2014-08-05 20:54:44 -0700294 source = NULL;
295 }
296
Andreas Huberafed0e12011-09-20 15:39:58 -0700297 msg->setObject("source", source);
Andreas Huberf9334412010-12-15 15:17:42 -0800298 msg->post();
299}
300
Chris Watkins99f31602015-03-20 13:06:33 -0700301void NuPlayer::setDataSourceAsync(const sp<DataSource> &dataSource) {
302 sp<AMessage> msg = new AMessage(kWhatSetDataSource, this);
303 sp<AMessage> notify = new AMessage(kWhatSourceNotify, this);
304
305 sp<GenericSource> source = new GenericSource(notify, mUIDValid, mUID);
306 status_t err = source->setDataSource(dataSource);
307
308 if (err != OK) {
309 ALOGE("Failed to set data source!");
310 source = NULL;
311 }
312
313 msg->setObject("source", source);
314 msg->post();
315}
316
Andreas Huber9575c962013-02-05 13:59:56 -0800317void NuPlayer::prepareAsync() {
Lajos Molnar1d15ab52015-03-04 16:46:34 -0800318 (new AMessage(kWhatPrepare, this))->post();
Andreas Huber9575c962013-02-05 13:59:56 -0800319}
320
Andreas Huber57a339c2012-12-03 11:18:00 -0800321void NuPlayer::setVideoSurfaceTextureAsync(
Andy McFadden8ba01022012-12-18 09:46:54 -0800322 const sp<IGraphicBufferProducer> &bufferProducer) {
Lajos Molnar1de1e252015-04-30 18:18:34 -0700323 sp<AMessage> msg = new AMessage(kWhatSetVideoSurface, this);
Andreas Huber57a339c2012-12-03 11:18:00 -0800324
Andy McFadden8ba01022012-12-18 09:46:54 -0800325 if (bufferProducer == NULL) {
Lajos Molnar1de1e252015-04-30 18:18:34 -0700326 msg->setObject("surface", NULL);
Andreas Huber57a339c2012-12-03 11:18:00 -0800327 } else {
Lajos Molnar1de1e252015-04-30 18:18:34 -0700328 msg->setObject("surface", new Surface(bufferProducer, true /* controlledByApp */));
Andreas Huber57a339c2012-12-03 11:18:00 -0800329 }
330
Andreas Huberf9334412010-12-15 15:17:42 -0800331 msg->post();
332}
333
334void NuPlayer::setAudioSink(const sp<MediaPlayerBase::AudioSink> &sink) {
Lajos Molnar1d15ab52015-03-04 16:46:34 -0800335 sp<AMessage> msg = new AMessage(kWhatSetAudioSink, this);
Andreas Huberf9334412010-12-15 15:17:42 -0800336 msg->setObject("sink", sink);
337 msg->post();
338}
339
340void NuPlayer::start() {
Lajos Molnar1d15ab52015-03-04 16:46:34 -0800341 (new AMessage(kWhatStart, this))->post();
Andreas Huberf9334412010-12-15 15:17:42 -0800342}
343
Lajos Molnar3a474aa2015-04-24 17:10:07 -0700344status_t NuPlayer::setPlaybackSettings(const AudioPlaybackRate &rate) {
345 // do some cursory validation of the settings here. audio modes are
346 // only validated when set on the audiosink.
347 if ((rate.mSpeed != 0.f && rate.mSpeed < AUDIO_TIMESTRETCH_SPEED_MIN)
348 || rate.mSpeed > AUDIO_TIMESTRETCH_SPEED_MAX
349 || rate.mPitch < AUDIO_TIMESTRETCH_SPEED_MIN
350 || rate.mPitch > AUDIO_TIMESTRETCH_SPEED_MAX) {
351 return BAD_VALUE;
352 }
353 sp<AMessage> msg = new AMessage(kWhatConfigPlayback, this);
354 writeToAMessage(msg, rate);
355 sp<AMessage> response;
356 status_t err = msg->postAndAwaitResponse(&response);
357 if (err == OK && response != NULL) {
358 CHECK(response->findInt32("err", &err));
359 }
360 return err;
361}
362
363status_t NuPlayer::getPlaybackSettings(AudioPlaybackRate *rate /* nonnull */) {
364 sp<AMessage> msg = new AMessage(kWhatGetPlaybackSettings, this);
365 sp<AMessage> response;
366 status_t err = msg->postAndAwaitResponse(&response);
367 if (err == OK && response != NULL) {
368 CHECK(response->findInt32("err", &err));
369 if (err == OK) {
370 readFromAMessage(response, rate);
371 }
372 }
373 return err;
374}
375
376status_t NuPlayer::setSyncSettings(const AVSyncSettings &sync, float videoFpsHint) {
377 sp<AMessage> msg = new AMessage(kWhatConfigSync, this);
378 writeToAMessage(msg, sync, videoFpsHint);
379 sp<AMessage> response;
380 status_t err = msg->postAndAwaitResponse(&response);
381 if (err == OK && response != NULL) {
382 CHECK(response->findInt32("err", &err));
383 }
384 return err;
385}
386
387status_t NuPlayer::getSyncSettings(
388 AVSyncSettings *sync /* nonnull */, float *videoFps /* nonnull */) {
389 sp<AMessage> msg = new AMessage(kWhatGetSyncSettings, this);
390 sp<AMessage> response;
391 status_t err = msg->postAndAwaitResponse(&response);
392 if (err == OK && response != NULL) {
393 CHECK(response->findInt32("err", &err));
394 if (err == OK) {
395 readFromAMessage(response, sync, videoFps);
396 }
397 }
398 return err;
Wei Jia98160162015-02-04 17:01:11 -0800399}
400
Andreas Huber43c3e6c2011-01-05 12:17:08 -0800401void NuPlayer::pause() {
Lajos Molnar1d15ab52015-03-04 16:46:34 -0800402 (new AMessage(kWhatPause, this))->post();
Andreas Huber43c3e6c2011-01-05 12:17:08 -0800403}
404
Andreas Huber1aef2112011-01-04 14:01:29 -0800405void NuPlayer::resetAsync() {
Wei Jiac45a4b22016-04-15 15:30:23 -0700406 sp<Source> source;
407 {
408 Mutex::Autolock autoLock(mSourceLock);
409 source = mSource;
410 }
411
412 if (source != NULL) {
Chong Zhang48296b72014-09-14 14:28:45 -0700413 // During a reset, the data source might be unresponsive already, we need to
414 // disconnect explicitly so that reads exit promptly.
415 // We can't queue the disconnect request to the looper, as it might be
416 // queued behind a stuck read and never gets processed.
417 // Doing a disconnect outside the looper to allows the pending reads to exit
418 // (either successfully or with error).
Wei Jiac45a4b22016-04-15 15:30:23 -0700419 source->disconnect();
Chong Zhang48296b72014-09-14 14:28:45 -0700420 }
421
Lajos Molnar1d15ab52015-03-04 16:46:34 -0800422 (new AMessage(kWhatReset, this))->post();
Andreas Huber1aef2112011-01-04 14:01:29 -0800423}
424
Wei Jia67b6dcc2016-10-31 17:01:37 -0700425void NuPlayer::seekToAsync(int64_t seekTimeUs, bool precise, bool needNotify) {
Lajos Molnar1d15ab52015-03-04 16:46:34 -0800426 sp<AMessage> msg = new AMessage(kWhatSeek, this);
Andreas Huber43c3e6c2011-01-05 12:17:08 -0800427 msg->setInt64("seekTimeUs", seekTimeUs);
Wei Jia67b6dcc2016-10-31 17:01:37 -0700428 msg->setInt32("precise", precise);
Wei Jiae427abf2014-09-22 15:21:11 -0700429 msg->setInt32("needNotify", needNotify);
Andreas Huber43c3e6c2011-01-05 12:17:08 -0800430 msg->post();
431}
432
Andreas Huber53df1a42010-12-22 10:03:04 -0800433
Chong Zhang404fced2014-06-11 14:45:31 -0700434void NuPlayer::writeTrackInfo(
Chih-Hung Hsiehe964d4e2016-08-09 14:31:32 -0700435 Parcel* reply, const sp<AMessage>& format) const {
Marco Nelissenc367ca12015-09-15 09:51:59 -0700436 if (format == NULL) {
437 ALOGE("NULL format");
438 return;
439 }
Chong Zhang404fced2014-06-11 14:45:31 -0700440 int32_t trackType;
Marco Nelissen9436e482015-09-15 10:21:53 -0700441 if (!format->findInt32("type", &trackType)) {
Marco Nelissenc367ca12015-09-15 09:51:59 -0700442 ALOGE("no track type");
443 return;
444 }
Chong Zhang404fced2014-06-11 14:45:31 -0700445
Robert Shih755106e2015-04-30 14:36:45 -0700446 AString mime;
Robert Shih2e3a4252015-05-06 10:21:15 -0700447 if (!format->findString("mime", &mime)) {
448 // Java MediaPlayer only uses mimetype for subtitle and timedtext tracks.
449 // If we can't find the mimetype here it means that we wouldn't be needing
450 // the mimetype on the Java end. We still write a placeholder mime to keep the
451 // (de)serialization logic simple.
452 if (trackType == MEDIA_TRACK_TYPE_AUDIO) {
453 mime = "audio/";
454 } else if (trackType == MEDIA_TRACK_TYPE_VIDEO) {
455 mime = "video/";
456 } else {
Marco Nelissenc367ca12015-09-15 09:51:59 -0700457 ALOGE("unknown track type: %d", trackType);
458 return;
Robert Shih2e3a4252015-05-06 10:21:15 -0700459 }
460 }
Robert Shih755106e2015-04-30 14:36:45 -0700461
Chong Zhang404fced2014-06-11 14:45:31 -0700462 AString lang;
Marco Nelissen9436e482015-09-15 10:21:53 -0700463 if (!format->findString("language", &lang)) {
Marco Nelissenc367ca12015-09-15 09:51:59 -0700464 ALOGE("no language");
465 return;
466 }
Chong Zhang404fced2014-06-11 14:45:31 -0700467
468 reply->writeInt32(2); // write something non-zero
469 reply->writeInt32(trackType);
Robert Shih755106e2015-04-30 14:36:45 -0700470 reply->writeString16(String16(mime.c_str()));
Chong Zhang404fced2014-06-11 14:45:31 -0700471 reply->writeString16(String16(lang.c_str()));
472
473 if (trackType == MEDIA_TRACK_TYPE_SUBTITLE) {
Chong Zhang404fced2014-06-11 14:45:31 -0700474 int32_t isAuto, isDefault, isForced;
475 CHECK(format->findInt32("auto", &isAuto));
476 CHECK(format->findInt32("default", &isDefault));
477 CHECK(format->findInt32("forced", &isForced));
478
Chong Zhang404fced2014-06-11 14:45:31 -0700479 reply->writeInt32(isAuto);
480 reply->writeInt32(isDefault);
481 reply->writeInt32(isForced);
482 }
483}
484
Andreas Huberf9334412010-12-15 15:17:42 -0800485void NuPlayer::onMessageReceived(const sp<AMessage> &msg) {
486 switch (msg->what()) {
487 case kWhatSetDataSource:
488 {
Steve Block3856b092011-10-20 11:56:00 +0100489 ALOGV("kWhatSetDataSource");
Andreas Huberf9334412010-12-15 15:17:42 -0800490
491 CHECK(mSource == NULL);
492
Chong Zhang3de157d2014-08-05 20:54:44 -0700493 status_t err = OK;
Andreas Huber5bc087c2010-12-23 10:27:40 -0800494 sp<RefBase> obj;
495 CHECK(msg->findObject("source", &obj));
Chong Zhang3de157d2014-08-05 20:54:44 -0700496 if (obj != NULL) {
Wei Jiac45a4b22016-04-15 15:30:23 -0700497 Mutex::Autolock autoLock(mSourceLock);
Chong Zhang3de157d2014-08-05 20:54:44 -0700498 mSource = static_cast<Source *>(obj.get());
Chong Zhang3de157d2014-08-05 20:54:44 -0700499 } else {
500 err = UNKNOWN_ERROR;
501 }
Andreas Huber9575c962013-02-05 13:59:56 -0800502
503 CHECK(mDriver != NULL);
504 sp<NuPlayerDriver> driver = mDriver.promote();
505 if (driver != NULL) {
Chong Zhang3de157d2014-08-05 20:54:44 -0700506 driver->notifySetDataSourceCompleted(err);
Andreas Huber9575c962013-02-05 13:59:56 -0800507 }
508 break;
509 }
510
511 case kWhatPrepare:
512 {
513 mSource->prepareAsync();
Andreas Huberf9334412010-12-15 15:17:42 -0800514 break;
515 }
516
Chong Zhangdcb89b32013-08-06 09:44:47 -0700517 case kWhatGetTrackInfo:
518 {
Lajos Molnar3f274362015-03-05 14:35:41 -0800519 sp<AReplyToken> replyID;
Chong Zhangdcb89b32013-08-06 09:44:47 -0700520 CHECK(msg->senderAwaitsResponse(&replyID));
521
Chong Zhang404fced2014-06-11 14:45:31 -0700522 Parcel* reply;
523 CHECK(msg->findPointer("reply", (void**)&reply));
524
525 size_t inbandTracks = 0;
Chong Zhangdcb89b32013-08-06 09:44:47 -0700526 if (mSource != NULL) {
Chong Zhang404fced2014-06-11 14:45:31 -0700527 inbandTracks = mSource->getTrackCount();
528 }
529
Chong Zhanga7fa1d92014-06-11 14:49:23 -0700530 size_t ccTracks = 0;
531 if (mCCDecoder != NULL) {
532 ccTracks = mCCDecoder->getTrackCount();
533 }
534
Chong Zhang404fced2014-06-11 14:45:31 -0700535 // total track count
Chong Zhanga7fa1d92014-06-11 14:49:23 -0700536 reply->writeInt32(inbandTracks + ccTracks);
Chong Zhang404fced2014-06-11 14:45:31 -0700537
538 // write inband tracks
539 for (size_t i = 0; i < inbandTracks; ++i) {
540 writeTrackInfo(reply, mSource->getTrackInfo(i));
Chong Zhangdcb89b32013-08-06 09:44:47 -0700541 }
542
Chong Zhanga7fa1d92014-06-11 14:49:23 -0700543 // write CC track
544 for (size_t i = 0; i < ccTracks; ++i) {
545 writeTrackInfo(reply, mCCDecoder->getTrackInfo(i));
546 }
547
Chong Zhangdcb89b32013-08-06 09:44:47 -0700548 sp<AMessage> response = new AMessage;
Chong Zhangdcb89b32013-08-06 09:44:47 -0700549 response->postReply(replyID);
550 break;
551 }
552
Robert Shih7c4f0d72014-07-09 18:53:31 -0700553 case kWhatGetSelectedTrack:
554 {
555 status_t err = INVALID_OPERATION;
556 if (mSource != NULL) {
557 err = OK;
558
559 int32_t type32;
560 CHECK(msg->findInt32("type", (int32_t*)&type32));
561 media_track_type type = (media_track_type)type32;
562 ssize_t selectedTrack = mSource->getSelectedTrack(type);
563
564 Parcel* reply;
565 CHECK(msg->findPointer("reply", (void**)&reply));
566 reply->writeInt32(selectedTrack);
567 }
568
569 sp<AMessage> response = new AMessage;
570 response->setInt32("err", err);
571
Lajos Molnar3f274362015-03-05 14:35:41 -0800572 sp<AReplyToken> replyID;
Robert Shih7c4f0d72014-07-09 18:53:31 -0700573 CHECK(msg->senderAwaitsResponse(&replyID));
574 response->postReply(replyID);
575 break;
576 }
577
Chong Zhangdcb89b32013-08-06 09:44:47 -0700578 case kWhatSelectTrack:
579 {
Lajos Molnar3f274362015-03-05 14:35:41 -0800580 sp<AReplyToken> replyID;
Chong Zhangdcb89b32013-08-06 09:44:47 -0700581 CHECK(msg->senderAwaitsResponse(&replyID));
582
Chong Zhang404fced2014-06-11 14:45:31 -0700583 size_t trackIndex;
584 int32_t select;
Robert Shih6ffb1fd2014-10-29 16:24:32 -0700585 int64_t timeUs;
Chong Zhang404fced2014-06-11 14:45:31 -0700586 CHECK(msg->findSize("trackIndex", &trackIndex));
587 CHECK(msg->findInt32("select", &select));
Robert Shih6ffb1fd2014-10-29 16:24:32 -0700588 CHECK(msg->findInt64("timeUs", &timeUs));
Chong Zhang404fced2014-06-11 14:45:31 -0700589
Chong Zhangdcb89b32013-08-06 09:44:47 -0700590 status_t err = INVALID_OPERATION;
Chong Zhang404fced2014-06-11 14:45:31 -0700591
592 size_t inbandTracks = 0;
Chong Zhangdcb89b32013-08-06 09:44:47 -0700593 if (mSource != NULL) {
Chong Zhang404fced2014-06-11 14:45:31 -0700594 inbandTracks = mSource->getTrackCount();
595 }
Chong Zhanga7fa1d92014-06-11 14:49:23 -0700596 size_t ccTracks = 0;
597 if (mCCDecoder != NULL) {
598 ccTracks = mCCDecoder->getTrackCount();
599 }
Chong Zhang404fced2014-06-11 14:45:31 -0700600
601 if (trackIndex < inbandTracks) {
Robert Shih6ffb1fd2014-10-29 16:24:32 -0700602 err = mSource->selectTrack(trackIndex, select, timeUs);
Robert Shihd3b0bbb2014-07-23 15:00:25 -0700603
604 if (!select && err == OK) {
605 int32_t type;
606 sp<AMessage> info = mSource->getTrackInfo(trackIndex);
607 if (info != NULL
608 && info->findInt32("type", &type)
609 && type == MEDIA_TRACK_TYPE_TIMEDTEXT) {
610 ++mTimedTextGeneration;
611 }
612 }
Chong Zhanga7fa1d92014-06-11 14:49:23 -0700613 } else {
614 trackIndex -= inbandTracks;
615
616 if (trackIndex < ccTracks) {
617 err = mCCDecoder->selectTrack(trackIndex, select);
618 }
Chong Zhangdcb89b32013-08-06 09:44:47 -0700619 }
620
621 sp<AMessage> response = new AMessage;
622 response->setInt32("err", err);
623
624 response->postReply(replyID);
625 break;
626 }
627
Andreas Huberb7c8e912012-11-27 15:02:53 -0800628 case kWhatPollDuration:
629 {
630 int32_t generation;
631 CHECK(msg->findInt32("generation", &generation));
632
633 if (generation != mPollDurationGeneration) {
634 // stale
635 break;
636 }
637
638 int64_t durationUs;
639 if (mDriver != NULL && mSource->getDuration(&durationUs) == OK) {
640 sp<NuPlayerDriver> driver = mDriver.promote();
641 if (driver != NULL) {
642 driver->notifyDuration(durationUs);
643 }
644 }
645
646 msg->post(1000000ll); // poll again in a second.
647 break;
648 }
649
Lajos Molnar1de1e252015-04-30 18:18:34 -0700650 case kWhatSetVideoSurface:
Andreas Huberf9334412010-12-15 15:17:42 -0800651 {
Andreas Huberf9334412010-12-15 15:17:42 -0800652
653 sp<RefBase> obj;
Lajos Molnar1de1e252015-04-30 18:18:34 -0700654 CHECK(msg->findObject("surface", &obj));
655 sp<Surface> surface = static_cast<Surface *>(obj.get());
Lajos Molnara81c6222015-07-10 19:17:45 -0700656
657 ALOGD("onSetVideoSurface(%p, %s video decoder)",
658 surface.get(),
Wei Jia6b7d2ed2015-08-10 15:11:54 -0700659 (mSource != NULL && mStarted && mSource->getFormat(false /* audio */) != NULL
Lajos Molnara81c6222015-07-10 19:17:45 -0700660 && mVideoDecoder != NULL) ? "have" : "no");
661
Wei Jia6b7d2ed2015-08-10 15:11:54 -0700662 // Need to check mStarted before calling mSource->getFormat because NuPlayer might
663 // be in preparing state and it could take long time.
664 // When mStarted is true, mSource must have been set.
665 if (mSource == NULL || !mStarted || mSource->getFormat(false /* audio */) == NULL
Lajos Molnara81c6222015-07-10 19:17:45 -0700666 // NOTE: mVideoDecoder's mSurface is always non-null
667 || (mVideoDecoder != NULL && mVideoDecoder->setVideoSurface(surface) == OK)) {
Lajos Molnar1de1e252015-04-30 18:18:34 -0700668 performSetSurface(surface);
Wei Jiafef808d2014-10-31 17:57:05 -0700669 break;
670 }
671
672 mDeferredActions.push_back(
673 new FlushDecoderAction(FLUSH_CMD_FLUSH /* audio */,
674 FLUSH_CMD_SHUTDOWN /* video */));
675
Lajos Molnar1de1e252015-04-30 18:18:34 -0700676 mDeferredActions.push_back(new SetSurfaceAction(surface));
James Dong0d268a32012-08-31 12:18:27 -0700677
Wei Jiac6e58412015-07-10 18:04:55 -0700678 if (obj != NULL || mAudioDecoder != NULL) {
Wei Jiafef808d2014-10-31 17:57:05 -0700679 if (mStarted) {
Andy Hung73535852014-09-05 11:42:58 -0700680 // Issue a seek to refresh the video screen only if started otherwise
681 // the extractor may not yet be started and will assert.
682 // If the video decoder is not set (perhaps audio only in this case)
683 // do not perform a seek as it is not needed.
Ronghua Wua73d9e02014-10-08 15:13:29 -0700684 int64_t currentPositionUs = 0;
685 if (getCurrentPosition(&currentPositionUs) == OK) {
686 mDeferredActions.push_back(
Wei Jia14486822016-11-02 17:51:30 -0700687 new SeekAction(currentPositionUs, false /* precise */));
Ronghua Wua73d9e02014-10-08 15:13:29 -0700688 }
Andy Hung73535852014-09-05 11:42:58 -0700689 }
Wei Jiaac428aa2014-09-02 19:01:34 -0700690
Andreas Huber57a339c2012-12-03 11:18:00 -0800691 // If there is a new surface texture, instantiate decoders
692 // again if possible.
693 mDeferredActions.push_back(
694 new SimpleAction(&NuPlayer::performScanSources));
695 }
696
Chong Zhangf8d71772014-11-26 15:08:34 -0800697 // After a flush without shutdown, decoder is paused.
698 // Don't resume it until source seek is done, otherwise it could
Chong Zhang7137ec72014-11-12 16:41:05 -0800699 // start pulling stale data too soon.
700 mDeferredActions.push_back(
Chong Zhangf8d71772014-11-26 15:08:34 -0800701 new ResumeDecoderAction(false /* needNotify */));
Chong Zhang7137ec72014-11-12 16:41:05 -0800702
Andreas Huber57a339c2012-12-03 11:18:00 -0800703 processDeferredActions();
Andreas Huberf9334412010-12-15 15:17:42 -0800704 break;
705 }
706
707 case kWhatSetAudioSink:
708 {
Steve Block3856b092011-10-20 11:56:00 +0100709 ALOGV("kWhatSetAudioSink");
Andreas Huberf9334412010-12-15 15:17:42 -0800710
711 sp<RefBase> obj;
712 CHECK(msg->findObject("sink", &obj));
713
714 mAudioSink = static_cast<MediaPlayerBase::AudioSink *>(obj.get());
715 break;
716 }
717
718 case kWhatStart:
719 {
Steve Block3856b092011-10-20 11:56:00 +0100720 ALOGV("kWhatStart");
Wei Jia94211742014-10-28 17:09:06 -0700721 if (mStarted) {
Chong Zhang8a048332015-05-06 15:16:28 -0700722 // do not resume yet if the source is still buffering
723 if (!mPausedForBuffering) {
724 onResume();
725 }
Wei Jia94211742014-10-28 17:09:06 -0700726 } else {
727 onStart();
Lajos Molnar09524832014-07-17 14:29:51 -0700728 }
Chong Zhangefbb6192015-01-30 17:13:27 -0800729 mPausedByClient = false;
Andreas Huberf9334412010-12-15 15:17:42 -0800730 break;
731 }
732
Lajos Molnar3a474aa2015-04-24 17:10:07 -0700733 case kWhatConfigPlayback:
Wei Jia98160162015-02-04 17:01:11 -0800734 {
Lajos Molnar3a474aa2015-04-24 17:10:07 -0700735 sp<AReplyToken> replyID;
736 CHECK(msg->senderAwaitsResponse(&replyID));
737 AudioPlaybackRate rate /* sanitized */;
738 readFromAMessage(msg, &rate);
739 status_t err = OK;
Wei Jia98160162015-02-04 17:01:11 -0800740 if (mRenderer != NULL) {
Wei Jiabf70feb2016-02-19 15:47:16 -0800741 // AudioSink allows only 1.f and 0.f for offload mode.
742 // For other speed, switch to non-offload mode.
743 if (mOffloadAudio && ((rate.mSpeed != 0.f && rate.mSpeed != 1.f)
744 || rate.mPitch != 1.f)) {
745 int64_t currentPositionUs;
746 if (getCurrentPosition(&currentPositionUs) != OK) {
747 currentPositionUs = mPreviousSeekTimeUs;
748 }
749
750 // Set mPlaybackSettings so that the new audio decoder can
751 // be created correctly.
752 mPlaybackSettings = rate;
753 if (!mPaused) {
754 mRenderer->pause();
755 }
Wei Jia5031b2f2016-02-25 11:19:31 -0800756 restartAudio(
Wei Jiabf70feb2016-02-19 15:47:16 -0800757 currentPositionUs, true /* forceNonOffload */,
758 true /* needsToCreateAudioDecoder */);
759 if (!mPaused) {
760 mRenderer->resume();
761 }
762 }
763
Lajos Molnar3a474aa2015-04-24 17:10:07 -0700764 err = mRenderer->setPlaybackSettings(rate);
765 }
766 if (err == OK) {
767 if (rate.mSpeed == 0.f) {
768 onPause();
Wei Jia351ce872016-02-10 13:21:59 -0800769 mPausedByClient = true;
Lajos Molnar3a474aa2015-04-24 17:10:07 -0700770 // save all other settings (using non-paused speed)
771 // so we can restore them on start
772 AudioPlaybackRate newRate = rate;
773 newRate.mSpeed = mPlaybackSettings.mSpeed;
774 mPlaybackSettings = newRate;
775 } else { /* rate.mSpeed != 0.f */
Lajos Molnar3a474aa2015-04-24 17:10:07 -0700776 mPlaybackSettings = rate;
Wei Jia8a092d32016-06-03 14:57:24 -0700777 if (mStarted) {
778 // do not resume yet if the source is still buffering
779 if (!mPausedForBuffering) {
780 onResume();
781 }
782 } else if (mPrepared) {
783 onStart();
784 }
785
786 mPausedByClient = false;
Lajos Molnar3a474aa2015-04-24 17:10:07 -0700787 }
Wei Jia98160162015-02-04 17:01:11 -0800788 }
Ronghua Wu8db88132015-04-22 13:51:35 -0700789
790 if (mVideoDecoder != NULL) {
Praveen Chavanbbaa1442016-04-08 13:33:49 -0700791 sp<AMessage> params = new AMessage();
792 params->setFloat("playback-speed", mPlaybackSettings.mSpeed);
793 mVideoDecoder->setParameters(params);
Ronghua Wu8db88132015-04-22 13:51:35 -0700794 }
795
Lajos Molnar3a474aa2015-04-24 17:10:07 -0700796 sp<AMessage> response = new AMessage;
797 response->setInt32("err", err);
798 response->postReply(replyID);
799 break;
800 }
801
802 case kWhatGetPlaybackSettings:
803 {
804 sp<AReplyToken> replyID;
805 CHECK(msg->senderAwaitsResponse(&replyID));
806 AudioPlaybackRate rate = mPlaybackSettings;
807 status_t err = OK;
808 if (mRenderer != NULL) {
809 err = mRenderer->getPlaybackSettings(&rate);
810 }
811 if (err == OK) {
812 // get playback settings used by renderer, as it may be
813 // slightly off due to audiosink not taking small changes.
814 mPlaybackSettings = rate;
815 if (mPaused) {
816 rate.mSpeed = 0.f;
817 }
818 }
819 sp<AMessage> response = new AMessage;
820 if (err == OK) {
821 writeToAMessage(response, rate);
822 }
823 response->setInt32("err", err);
824 response->postReply(replyID);
825 break;
826 }
827
828 case kWhatConfigSync:
829 {
830 sp<AReplyToken> replyID;
831 CHECK(msg->senderAwaitsResponse(&replyID));
832
833 ALOGV("kWhatConfigSync");
834 AVSyncSettings sync;
835 float videoFpsHint;
836 readFromAMessage(msg, &sync, &videoFpsHint);
837 status_t err = OK;
838 if (mRenderer != NULL) {
839 err = mRenderer->setSyncSettings(sync, videoFpsHint);
840 }
841 if (err == OK) {
842 mSyncSettings = sync;
843 mVideoFpsHint = videoFpsHint;
844 }
845 sp<AMessage> response = new AMessage;
846 response->setInt32("err", err);
847 response->postReply(replyID);
848 break;
849 }
850
851 case kWhatGetSyncSettings:
852 {
853 sp<AReplyToken> replyID;
854 CHECK(msg->senderAwaitsResponse(&replyID));
855 AVSyncSettings sync = mSyncSettings;
856 float videoFps = mVideoFpsHint;
857 status_t err = OK;
858 if (mRenderer != NULL) {
859 err = mRenderer->getSyncSettings(&sync, &videoFps);
860 if (err == OK) {
861 mSyncSettings = sync;
862 mVideoFpsHint = videoFps;
863 }
864 }
865 sp<AMessage> response = new AMessage;
866 if (err == OK) {
867 writeToAMessage(response, sync, videoFps);
868 }
869 response->setInt32("err", err);
870 response->postReply(replyID);
Wei Jia98160162015-02-04 17:01:11 -0800871 break;
872 }
873
Andreas Huberf9334412010-12-15 15:17:42 -0800874 case kWhatScanSources:
875 {
Andreas Huber1aef2112011-01-04 14:01:29 -0800876 int32_t generation;
877 CHECK(msg->findInt32("generation", &generation));
878 if (generation != mScanSourcesGeneration) {
879 // Drop obsolete msg.
880 break;
881 }
882
Andreas Huber5bc087c2010-12-23 10:27:40 -0800883 mScanSourcesPending = false;
884
Steve Block3856b092011-10-20 11:56:00 +0100885 ALOGV("scanning sources haveAudio=%d, haveVideo=%d",
Andreas Huber43c3e6c2011-01-05 12:17:08 -0800886 mAudioDecoder != NULL, mVideoDecoder != NULL);
887
Andreas Huberb7c8e912012-11-27 15:02:53 -0800888 bool mHadAnySourcesBefore =
889 (mAudioDecoder != NULL) || (mVideoDecoder != NULL);
Robert Shih7350b052015-10-01 15:50:14 -0700890 bool rescan = false;
Andreas Huberb7c8e912012-11-27 15:02:53 -0800891
Andy Hung282a7e32014-08-14 15:56:34 -0700892 // initialize video before audio because successful initialization of
893 // video may change deep buffer mode of audio.
Lajos Molnar1de1e252015-04-30 18:18:34 -0700894 if (mSurface != NULL) {
Robert Shih7350b052015-10-01 15:50:14 -0700895 if (instantiateDecoder(false, &mVideoDecoder) == -EWOULDBLOCK) {
896 rescan = true;
897 }
Haynes Mathew George5d246ef2012-07-09 10:36:57 -0700898 }
Andreas Huberf9334412010-12-15 15:17:42 -0800899
Ronghua Wua10fd232014-11-06 16:15:20 -0800900 // Don't try to re-open audio sink if there's an existing decoder.
901 if (mAudioSink != NULL && mAudioDecoder == NULL) {
Robert Shih7350b052015-10-01 15:50:14 -0700902 if (instantiateDecoder(true, &mAudioDecoder) == -EWOULDBLOCK) {
903 rescan = true;
904 }
Andreas Huberf9334412010-12-15 15:17:42 -0800905 }
906
Andreas Huberb7c8e912012-11-27 15:02:53 -0800907 if (!mHadAnySourcesBefore
908 && (mAudioDecoder != NULL || mVideoDecoder != NULL)) {
909 // This is the first time we've found anything playable.
910
Andreas Huber9575c962013-02-05 13:59:56 -0800911 if (mSourceFlags & Source::FLAG_DYNAMIC_DURATION) {
Andreas Huberb7c8e912012-11-27 15:02:53 -0800912 schedulePollDuration();
913 }
914 }
915
Andreas Hubereac68ba2011-09-27 12:12:25 -0700916 status_t err;
917 if ((err = mSource->feedMoreTSData()) != OK) {
Andreas Huber1aef2112011-01-04 14:01:29 -0800918 if (mAudioDecoder == NULL && mVideoDecoder == NULL) {
919 // We're not currently decoding anything (no audio or
920 // video tracks found) and we just ran out of input data.
Andreas Hubereac68ba2011-09-27 12:12:25 -0700921
922 if (err == ERROR_END_OF_STREAM) {
923 notifyListener(MEDIA_PLAYBACK_COMPLETE, 0, 0);
924 } else {
925 notifyListener(MEDIA_ERROR, MEDIA_ERROR_UNKNOWN, err);
926 }
Andreas Huber1aef2112011-01-04 14:01:29 -0800927 }
Andreas Huberf9334412010-12-15 15:17:42 -0800928 break;
929 }
930
Robert Shih7350b052015-10-01 15:50:14 -0700931 if (rescan) {
Andreas Huberf9334412010-12-15 15:17:42 -0800932 msg->post(100000ll);
Andreas Huber5bc087c2010-12-23 10:27:40 -0800933 mScanSourcesPending = true;
Andreas Huberf9334412010-12-15 15:17:42 -0800934 }
935 break;
936 }
937
938 case kWhatVideoNotify:
939 case kWhatAudioNotify:
940 {
941 bool audio = msg->what() == kWhatAudioNotify;
942
Wei Jia88703c32014-08-06 11:24:07 -0700943 int32_t currentDecoderGeneration =
944 (audio? mAudioDecoderGeneration : mVideoDecoderGeneration);
945 int32_t requesterGeneration = currentDecoderGeneration - 1;
946 CHECK(msg->findInt32("generation", &requesterGeneration));
947
948 if (requesterGeneration != currentDecoderGeneration) {
949 ALOGV("got message from old %s decoder, generation(%d:%d)",
950 audio ? "audio" : "video", requesterGeneration,
951 currentDecoderGeneration);
952 sp<AMessage> reply;
953 if (!(msg->findMessage("reply", &reply))) {
954 return;
955 }
956
957 reply->setInt32("err", INFO_DISCONTINUITY);
958 reply->post();
959 return;
960 }
961
Andreas Huberf9334412010-12-15 15:17:42 -0800962 int32_t what;
Lajos Molnar1cd13982014-01-17 15:12:51 -0800963 CHECK(msg->findInt32("what", &what));
Andreas Huberf9334412010-12-15 15:17:42 -0800964
Chong Zhang7137ec72014-11-12 16:41:05 -0800965 if (what == DecoderBase::kWhatInputDiscontinuity) {
966 int32_t formatChange;
967 CHECK(msg->findInt32("formatChange", &formatChange));
Andreas Huberf9334412010-12-15 15:17:42 -0800968
Chong Zhang7137ec72014-11-12 16:41:05 -0800969 ALOGV("%s discontinuity: formatChange %d",
970 audio ? "audio" : "video", formatChange);
971
972 if (formatChange) {
973 mDeferredActions.push_back(
974 new FlushDecoderAction(
975 audio ? FLUSH_CMD_SHUTDOWN : FLUSH_CMD_NONE,
976 audio ? FLUSH_CMD_NONE : FLUSH_CMD_SHUTDOWN));
Andreas Huberf9334412010-12-15 15:17:42 -0800977 }
Chong Zhang7137ec72014-11-12 16:41:05 -0800978
979 mDeferredActions.push_back(
980 new SimpleAction(
981 &NuPlayer::performScanSources));
982
983 processDeferredActions();
984 } else if (what == DecoderBase::kWhatEOS) {
Andreas Huberdc9bacd2011-09-26 10:53:29 -0700985 int32_t err;
Lajos Molnar1cd13982014-01-17 15:12:51 -0800986 CHECK(msg->findInt32("err", &err));
Andreas Huberdc9bacd2011-09-26 10:53:29 -0700987
988 if (err == ERROR_END_OF_STREAM) {
Steve Block3856b092011-10-20 11:56:00 +0100989 ALOGV("got %s decoder EOS", audio ? "audio" : "video");
Andreas Huberdc9bacd2011-09-26 10:53:29 -0700990 } else {
Steve Block3856b092011-10-20 11:56:00 +0100991 ALOGV("got %s decoder EOS w/ error %d",
Andreas Huberdc9bacd2011-09-26 10:53:29 -0700992 audio ? "audio" : "video",
993 err);
994 }
995
996 mRenderer->queueEOS(audio, err);
Chong Zhang7137ec72014-11-12 16:41:05 -0800997 } else if (what == DecoderBase::kWhatFlushCompleted) {
Steve Block3856b092011-10-20 11:56:00 +0100998 ALOGV("decoder %s flush completed", audio ? "audio" : "video");
Andreas Huberf9334412010-12-15 15:17:42 -0800999
Andy Hung8d121d42014-10-03 09:53:53 -07001000 handleFlushComplete(audio, true /* isDecoder */);
Andreas Huber3831a062010-12-21 10:22:33 -08001001 finishFlushIfPossible();
Chong Zhang7137ec72014-11-12 16:41:05 -08001002 } else if (what == DecoderBase::kWhatVideoSizeChanged) {
Lajos Molnar1cd13982014-01-17 15:12:51 -08001003 sp<AMessage> format;
1004 CHECK(msg->findMessage("format", &format));
1005
Wei Jiac6cfd702014-11-11 16:33:20 -08001006 sp<AMessage> inputFormat =
1007 mSource->getFormat(false /* audio */);
Andreas Huber3831a062010-12-21 10:22:33 -08001008
Bartosz Dolewski26936f72014-12-11 12:47:08 +01001009 setVideoScalingMode(mVideoScalingMode);
Wei Jiac6cfd702014-11-11 16:33:20 -08001010 updateVideoSize(inputFormat, format);
Chong Zhang7137ec72014-11-12 16:41:05 -08001011 } else if (what == DecoderBase::kWhatShutdownCompleted) {
Steve Block3856b092011-10-20 11:56:00 +01001012 ALOGV("%s shutdown completed", audio ? "audio" : "video");
Andreas Huber3831a062010-12-21 10:22:33 -08001013 if (audio) {
1014 mAudioDecoder.clear();
Wei Jia57568df2014-09-22 10:16:29 -07001015 ++mAudioDecoderGeneration;
Andreas Huber3831a062010-12-21 10:22:33 -08001016
1017 CHECK_EQ((int)mFlushingAudio, (int)SHUTTING_DOWN_DECODER);
1018 mFlushingAudio = SHUT_DOWN;
1019 } else {
1020 mVideoDecoder.clear();
Wei Jia57568df2014-09-22 10:16:29 -07001021 ++mVideoDecoderGeneration;
Andreas Huber3831a062010-12-21 10:22:33 -08001022
1023 CHECK_EQ((int)mFlushingVideo, (int)SHUTTING_DOWN_DECODER);
1024 mFlushingVideo = SHUT_DOWN;
1025 }
1026
1027 finishFlushIfPossible();
Chong Zhangf8d71772014-11-26 15:08:34 -08001028 } else if (what == DecoderBase::kWhatResumeCompleted) {
1029 finishResume();
Chong Zhang7137ec72014-11-12 16:41:05 -08001030 } else if (what == DecoderBase::kWhatError) {
Chong Zhangf4c0a942014-08-11 15:14:10 -07001031 status_t err;
Andy Hung2abde2c2014-09-30 14:40:32 -07001032 if (!msg->findInt32("err", &err) || err == OK) {
Chong Zhangf4c0a942014-08-11 15:14:10 -07001033 err = UNKNOWN_ERROR;
1034 }
Andy Hungcf31f1e2014-09-23 14:59:01 -07001035
Andy Hung2abde2c2014-09-30 14:40:32 -07001036 // Decoder errors can be due to Source (e.g. from streaming),
1037 // or from decoding corrupted bitstreams, or from other decoder
1038 // MediaCodec operations (e.g. from an ongoing reset or seek).
Andy Hung202bce12014-12-03 11:47:36 -08001039 // They may also be due to openAudioSink failure at
1040 // decoder start or after a format change.
Andy Hung2abde2c2014-09-30 14:40:32 -07001041 //
1042 // We try to gracefully shut down the affected decoder if possible,
1043 // rather than trying to force the shutdown with something
1044 // similar to performReset(). This method can lead to a hang
1045 // if MediaCodec functions block after an error, but they should
1046 // typically return INVALID_OPERATION instead of blocking.
1047
1048 FlushStatus *flushing = audio ? &mFlushingAudio : &mFlushingVideo;
1049 ALOGE("received error(%#x) from %s decoder, flushing(%d), now shutting down",
1050 err, audio ? "audio" : "video", *flushing);
1051
1052 switch (*flushing) {
1053 case NONE:
1054 mDeferredActions.push_back(
Wei Jiafef808d2014-10-31 17:57:05 -07001055 new FlushDecoderAction(
1056 audio ? FLUSH_CMD_SHUTDOWN : FLUSH_CMD_NONE,
1057 audio ? FLUSH_CMD_NONE : FLUSH_CMD_SHUTDOWN));
Andy Hung2abde2c2014-09-30 14:40:32 -07001058 processDeferredActions();
1059 break;
1060 case FLUSHING_DECODER:
1061 *flushing = FLUSHING_DECODER_SHUTDOWN; // initiate shutdown after flush.
1062 break; // Wait for flush to complete.
1063 case FLUSHING_DECODER_SHUTDOWN:
1064 break; // Wait for flush to complete.
1065 case SHUTTING_DOWN_DECODER:
1066 break; // Wait for shutdown to complete.
1067 case FLUSHED:
1068 // Widevine source reads must stop before releasing the video decoder.
1069 if (!audio && mSource != NULL && mSourceFlags & Source::FLAG_SECURE) {
1070 mSource->stop();
Robert Shih0c61a0d2015-07-06 15:09:10 -07001071 mSourceStarted = false;
Andy Hung2abde2c2014-09-30 14:40:32 -07001072 }
1073 getDecoder(audio)->initiateShutdown(); // In the middle of a seek.
1074 *flushing = SHUTTING_DOWN_DECODER; // Shut down.
1075 break;
1076 case SHUT_DOWN:
1077 finishFlushIfPossible(); // Should not occur.
1078 break; // Finish anyways.
Marco Nelissen9e2b7912014-08-18 16:13:03 -07001079 }
Andy Hung2abde2c2014-09-30 14:40:32 -07001080 notifyListener(MEDIA_ERROR, MEDIA_ERROR_UNKNOWN, err);
Lajos Molnar1cd13982014-01-17 15:12:51 -08001081 } else {
1082 ALOGV("Unhandled decoder notification %d '%c%c%c%c'.",
Andreas Hubera1f8ab02012-11-30 10:53:22 -08001083 what,
1084 what >> 24,
1085 (what >> 16) & 0xff,
1086 (what >> 8) & 0xff,
1087 what & 0xff);
Andreas Huberf9334412010-12-15 15:17:42 -08001088 }
1089
1090 break;
1091 }
1092
1093 case kWhatRendererNotify:
1094 {
Wei Jia57568df2014-09-22 10:16:29 -07001095 int32_t requesterGeneration = mRendererGeneration - 1;
1096 CHECK(msg->findInt32("generation", &requesterGeneration));
1097 if (requesterGeneration != mRendererGeneration) {
1098 ALOGV("got message from old renderer, generation(%d:%d)",
1099 requesterGeneration, mRendererGeneration);
1100 return;
1101 }
1102
Andreas Huberf9334412010-12-15 15:17:42 -08001103 int32_t what;
1104 CHECK(msg->findInt32("what", &what));
1105
1106 if (what == Renderer::kWhatEOS) {
1107 int32_t audio;
1108 CHECK(msg->findInt32("audio", &audio));
1109
Andreas Huberc92fd242011-08-16 13:48:44 -07001110 int32_t finalResult;
1111 CHECK(msg->findInt32("finalResult", &finalResult));
1112
Andreas Huberf9334412010-12-15 15:17:42 -08001113 if (audio) {
1114 mAudioEOS = true;
1115 } else {
1116 mVideoEOS = true;
1117 }
1118
Andreas Huberc92fd242011-08-16 13:48:44 -07001119 if (finalResult == ERROR_END_OF_STREAM) {
Steve Block3856b092011-10-20 11:56:00 +01001120 ALOGV("reached %s EOS", audio ? "audio" : "video");
Andreas Huberc92fd242011-08-16 13:48:44 -07001121 } else {
Steve Block29357bc2012-01-06 19:20:56 +00001122 ALOGE("%s track encountered an error (%d)",
Andreas Huberc92fd242011-08-16 13:48:44 -07001123 audio ? "audio" : "video", finalResult);
1124
1125 notifyListener(
1126 MEDIA_ERROR, MEDIA_ERROR_UNKNOWN, finalResult);
1127 }
Andreas Huberf9334412010-12-15 15:17:42 -08001128
1129 if ((mAudioEOS || mAudioDecoder == NULL)
1130 && (mVideoEOS || mVideoDecoder == NULL)) {
1131 notifyListener(MEDIA_PLAYBACK_COMPLETE, 0, 0);
1132 }
Andreas Huber3fe62152011-09-16 15:09:22 -07001133 } else if (what == Renderer::kWhatFlushComplete) {
Andreas Huberf9334412010-12-15 15:17:42 -08001134 int32_t audio;
1135 CHECK(msg->findInt32("audio", &audio));
1136
Wei Jia3261f0d2015-10-08 13:58:33 -07001137 if (audio) {
1138 mAudioEOS = false;
1139 } else {
1140 mVideoEOS = false;
1141 }
1142
Steve Block3856b092011-10-20 11:56:00 +01001143 ALOGV("renderer %s flush completed.", audio ? "audio" : "video");
Wei Jiada4252f2015-07-14 18:15:28 -07001144 if (audio && (mFlushingAudio == NONE || mFlushingAudio == FLUSHED
1145 || mFlushingAudio == SHUT_DOWN)) {
1146 // Flush has been handled by tear down.
1147 break;
1148 }
Andy Hung8d121d42014-10-03 09:53:53 -07001149 handleFlushComplete(audio, false /* isDecoder */);
1150 finishFlushIfPossible();
James Dongf57b4ea2012-07-20 13:38:36 -07001151 } else if (what == Renderer::kWhatVideoRenderingStart) {
1152 notifyListener(MEDIA_INFO, MEDIA_INFO_RENDERING_START, 0);
Lajos Molnarcbaffcf2013-08-14 18:30:38 -07001153 } else if (what == Renderer::kWhatMediaRenderingStart) {
1154 ALOGV("media rendering started");
1155 notifyListener(MEDIA_STARTED, 0, 0);
Ronghua Wufaeb0f22015-05-21 12:20:21 -07001156 } else if (what == Renderer::kWhatAudioTearDown) {
Ronghua Wu08529172014-10-02 16:55:52 -07001157 int32_t reason;
1158 CHECK(msg->findInt32("reason", &reason));
Ronghua Wufaeb0f22015-05-21 12:20:21 -07001159 ALOGV("Tear down audio with reason %d.", reason);
Wei Jia8456ddd2016-04-22 14:46:43 -07001160 if (reason == Renderer::kDueToTimeout && !(mPaused && mOffloadAudio)) {
1161 // TimeoutWhenPaused is only for offload mode.
1162 ALOGW("Receive a stale message for teardown.");
1163 break;
1164 }
Wei Jiada4252f2015-07-14 18:15:28 -07001165 int64_t positionUs;
Robert Shih1a5c8592015-08-04 18:07:44 -07001166 if (!msg->findInt64("positionUs", &positionUs)) {
1167 positionUs = mPreviousSeekTimeUs;
1168 }
Ronghua Wufaeb0f22015-05-21 12:20:21 -07001169
Wei Jia5031b2f2016-02-25 11:19:31 -08001170 restartAudio(
Wei Jiaa05f1e32016-03-25 16:31:22 -07001171 positionUs, reason == Renderer::kForceNonOffload /* forceNonOffload */,
1172 reason != Renderer::kDueToTimeout /* needsToCreateAudioDecoder */);
Andreas Huberf9334412010-12-15 15:17:42 -08001173 }
1174 break;
1175 }
1176
1177 case kWhatMoreDataQueued:
1178 {
1179 break;
1180 }
1181
Andreas Huber1aef2112011-01-04 14:01:29 -08001182 case kWhatReset:
1183 {
Steve Block3856b092011-10-20 11:56:00 +01001184 ALOGV("kWhatReset");
Andreas Huber1aef2112011-01-04 14:01:29 -08001185
Ronghua Wu64c2d172015-10-07 16:52:19 -07001186 mResetting = true;
1187
Andreas Hubera1f8ab02012-11-30 10:53:22 -08001188 mDeferredActions.push_back(
Wei Jiafef808d2014-10-31 17:57:05 -07001189 new FlushDecoderAction(
1190 FLUSH_CMD_SHUTDOWN /* audio */,
1191 FLUSH_CMD_SHUTDOWN /* video */));
Andreas Huberb7c8e912012-11-27 15:02:53 -08001192
Andreas Hubera1f8ab02012-11-30 10:53:22 -08001193 mDeferredActions.push_back(
1194 new SimpleAction(&NuPlayer::performReset));
Andreas Huberb58ce9f2011-11-28 16:27:35 -08001195
Andreas Hubera1f8ab02012-11-30 10:53:22 -08001196 processDeferredActions();
Andreas Huber1aef2112011-01-04 14:01:29 -08001197 break;
1198 }
1199
Andreas Huber43c3e6c2011-01-05 12:17:08 -08001200 case kWhatSeek:
1201 {
1202 int64_t seekTimeUs;
Wei Jia67b6dcc2016-10-31 17:01:37 -07001203 int32_t precise;
Wei Jiae427abf2014-09-22 15:21:11 -07001204 int32_t needNotify;
Andreas Huber43c3e6c2011-01-05 12:17:08 -08001205 CHECK(msg->findInt64("seekTimeUs", &seekTimeUs));
Wei Jia67b6dcc2016-10-31 17:01:37 -07001206 CHECK(msg->findInt32("precise", &precise));
Wei Jiae427abf2014-09-22 15:21:11 -07001207 CHECK(msg->findInt32("needNotify", &needNotify));
Andreas Huber43c3e6c2011-01-05 12:17:08 -08001208
Wei Jia67b6dcc2016-10-31 17:01:37 -07001209 ALOGV("kWhatSeek seekTimeUs=%lld us, precise=%d, needNotify=%d",
1210 (long long)seekTimeUs, precise, needNotify);
Andreas Huber43c3e6c2011-01-05 12:17:08 -08001211
Wei Jia1061c9c2015-05-19 16:02:17 -07001212 if (!mStarted) {
1213 // Seek before the player is started. In order to preview video,
1214 // need to start the player and pause it. This branch is called
1215 // only once if needed. After the player is started, any seek
1216 // operation will go through normal path.
Robert Shih0c61a0d2015-07-06 15:09:10 -07001217 // Audio-only cases are handled separately.
Wei Jia14486822016-11-02 17:51:30 -07001218 onStart(seekTimeUs, precise);
Robert Shih0c61a0d2015-07-06 15:09:10 -07001219 if (mStarted) {
1220 onPause();
1221 mPausedByClient = true;
1222 }
Wei Jia1061c9c2015-05-19 16:02:17 -07001223 if (needNotify) {
1224 notifyDriverSeekComplete();
1225 }
1226 break;
1227 }
1228
Andreas Hubera1f8ab02012-11-30 10:53:22 -08001229 mDeferredActions.push_back(
Wei Jiafef808d2014-10-31 17:57:05 -07001230 new FlushDecoderAction(FLUSH_CMD_FLUSH /* audio */,
1231 FLUSH_CMD_FLUSH /* video */));
Andreas Huber43c3e6c2011-01-05 12:17:08 -08001232
Wei Jiae427abf2014-09-22 15:21:11 -07001233 mDeferredActions.push_back(
Wei Jia14486822016-11-02 17:51:30 -07001234 new SeekAction(seekTimeUs, precise));
Andreas Huber43c3e6c2011-01-05 12:17:08 -08001235
Chong Zhangf8d71772014-11-26 15:08:34 -08001236 // After a flush without shutdown, decoder is paused.
1237 // Don't resume it until source seek is done, otherwise it could
Chong Zhang7137ec72014-11-12 16:41:05 -08001238 // start pulling stale data too soon.
1239 mDeferredActions.push_back(
Chong Zhangf8d71772014-11-26 15:08:34 -08001240 new ResumeDecoderAction(needNotify));
Chong Zhang7137ec72014-11-12 16:41:05 -08001241
Andreas Hubera1f8ab02012-11-30 10:53:22 -08001242 processDeferredActions();
Andreas Huber43c3e6c2011-01-05 12:17:08 -08001243 break;
1244 }
1245
Andreas Huberb4082222011-01-20 15:23:04 -08001246 case kWhatPause:
1247 {
Chong Zhangefbb6192015-01-30 17:13:27 -08001248 onPause();
1249 mPausedByClient = true;
Andreas Huberb4082222011-01-20 15:23:04 -08001250 break;
1251 }
1252
Andreas Huberb5f25f02013-02-05 10:14:26 -08001253 case kWhatSourceNotify:
1254 {
Andreas Huber9575c962013-02-05 13:59:56 -08001255 onSourceNotify(msg);
Andreas Huberb5f25f02013-02-05 10:14:26 -08001256 break;
1257 }
1258
Chong Zhanga7fa1d92014-06-11 14:49:23 -07001259 case kWhatClosedCaptionNotify:
1260 {
1261 onClosedCaptionNotify(msg);
1262 break;
1263 }
1264
Andreas Huberf9334412010-12-15 15:17:42 -08001265 default:
1266 TRESPASS();
1267 break;
1268 }
1269}
1270
Wei Jia94211742014-10-28 17:09:06 -07001271void NuPlayer::onResume() {
Ronghua Wu64c2d172015-10-07 16:52:19 -07001272 if (!mPaused || mResetting) {
1273 ALOGD_IF(mResetting, "resetting, onResume discarded");
Chong Zhangefbb6192015-01-30 17:13:27 -08001274 return;
1275 }
1276 mPaused = false;
Wei Jia94211742014-10-28 17:09:06 -07001277 if (mSource != NULL) {
1278 mSource->resume();
1279 } else {
1280 ALOGW("resume called when source is gone or not set");
1281 }
1282 // |mAudioDecoder| may have been released due to the pause timeout, so re-create it if
1283 // needed.
1284 if (audioDecoderStillNeeded() && mAudioDecoder == NULL) {
1285 instantiateDecoder(true /* audio */, &mAudioDecoder);
1286 }
1287 if (mRenderer != NULL) {
1288 mRenderer->resume();
1289 } else {
1290 ALOGW("resume called when renderer is gone or not set");
1291 }
1292}
1293
Lajos Molnarfcd3e942015-03-31 10:06:48 -07001294status_t NuPlayer::onInstantiateSecureDecoders() {
1295 status_t err;
1296 if (!(mSourceFlags & Source::FLAG_SECURE)) {
1297 return BAD_TYPE;
1298 }
1299
1300 if (mRenderer != NULL) {
1301 ALOGE("renderer should not be set when instantiating secure decoders");
1302 return UNKNOWN_ERROR;
1303 }
1304
1305 // TRICKY: We rely on mRenderer being null, so that decoder does not start requesting
1306 // data on instantiation.
Lajos Molnar1de1e252015-04-30 18:18:34 -07001307 if (mSurface != NULL) {
Lajos Molnarfcd3e942015-03-31 10:06:48 -07001308 err = instantiateDecoder(false, &mVideoDecoder);
1309 if (err != OK) {
1310 return err;
1311 }
1312 }
1313
1314 if (mAudioSink != NULL) {
1315 err = instantiateDecoder(true, &mAudioDecoder);
1316 if (err != OK) {
1317 return err;
1318 }
1319 }
1320 return OK;
1321}
1322
Wei Jia14486822016-11-02 17:51:30 -07001323void NuPlayer::onStart(int64_t startPositionUs, bool precise) {
Robert Shih0c61a0d2015-07-06 15:09:10 -07001324 if (!mSourceStarted) {
1325 mSourceStarted = true;
1326 mSource->start();
1327 }
1328 if (startPositionUs > 0) {
Wei Jia14486822016-11-02 17:51:30 -07001329 performSeek(startPositionUs, precise);
Robert Shih0c61a0d2015-07-06 15:09:10 -07001330 if (mSource->getFormat(false /* audio */) == NULL) {
1331 return;
1332 }
1333 }
1334
Wei Jia94211742014-10-28 17:09:06 -07001335 mOffloadAudio = false;
1336 mAudioEOS = false;
1337 mVideoEOS = false;
Wei Jia94211742014-10-28 17:09:06 -07001338 mStarted = true;
Wei Jiafe8fe7d2016-06-08 10:29:25 -07001339 mPaused = false;
Wei Jia94211742014-10-28 17:09:06 -07001340
Wei Jia94211742014-10-28 17:09:06 -07001341 uint32_t flags = 0;
1342
1343 if (mSource->isRealTime()) {
1344 flags |= Renderer::FLAG_REAL_TIME;
1345 }
1346
1347 sp<MetaData> audioMeta = mSource->getFormatMeta(true /* audio */);
Wei Jia1de83d52016-10-10 10:15:03 -07001348 sp<MetaData> videoMeta = mSource->getFormatMeta(false /* audio */);
1349 if (audioMeta == NULL && videoMeta == NULL) {
1350 ALOGE("no metadata for either audio or video source");
1351 mSource->stop();
1352 mSourceStarted = false;
1353 notifyListener(MEDIA_ERROR, MEDIA_ERROR_UNKNOWN, ERROR_MALFORMED);
1354 return;
1355 }
Andy Hung12a84132015-10-20 16:09:21 -07001356 ALOGV_IF(audioMeta == NULL, "no metadata for audio source"); // video only stream
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
1363 sp<AMessage> videoFormat = mSource->getFormat(false /* audio */);
1364
1365 mOffloadAudio =
Wei Jiabf70feb2016-02-19 15:47:16 -08001366 canOffloadStream(audioMeta, (videoFormat != NULL), mSource->isStreaming(), streamType)
1367 && (mPlaybackSettings.mSpeed == 1.f && mPlaybackSettings.mPitch == 1.f);
Wei Jia94211742014-10-28 17:09:06 -07001368 if (mOffloadAudio) {
1369 flags |= Renderer::FLAG_OFFLOAD_AUDIO;
1370 }
1371
Lajos Molnar1d15ab52015-03-04 16:46:34 -08001372 sp<AMessage> notify = new AMessage(kWhatRendererNotify, this);
Wei Jia94211742014-10-28 17:09:06 -07001373 ++mRendererGeneration;
1374 notify->setInt32("generation", mRendererGeneration);
1375 mRenderer = new Renderer(mAudioSink, notify, flags);
Wei Jia94211742014-10-28 17:09:06 -07001376 mRendererLooper = new ALooper;
1377 mRendererLooper->setName("NuPlayerRenderer");
1378 mRendererLooper->start(false, false, ANDROID_PRIORITY_AUDIO);
1379 mRendererLooper->registerHandler(mRenderer);
Lajos Molnar3a474aa2015-04-24 17:10:07 -07001380
1381 status_t err = mRenderer->setPlaybackSettings(mPlaybackSettings);
1382 if (err != OK) {
1383 mSource->stop();
Robert Shih0c61a0d2015-07-06 15:09:10 -07001384 mSourceStarted = false;
Lajos Molnar3a474aa2015-04-24 17:10:07 -07001385 notifyListener(MEDIA_ERROR, MEDIA_ERROR_UNKNOWN, err);
1386 return;
Wei Jiac8206ff2015-03-04 13:59:37 -08001387 }
Wei Jia94211742014-10-28 17:09:06 -07001388
Ronghua Wuc8a70d32015-04-29 16:26:34 -07001389 float rate = getFrameRate();
1390 if (rate > 0) {
Wei Jia94211742014-10-28 17:09:06 -07001391 mRenderer->setVideoFrameRate(rate);
1392 }
1393
Wei Jiac6cfd702014-11-11 16:33:20 -08001394 if (mVideoDecoder != NULL) {
1395 mVideoDecoder->setRenderer(mRenderer);
1396 }
1397 if (mAudioDecoder != NULL) {
1398 mAudioDecoder->setRenderer(mRenderer);
1399 }
1400
Wei Jia94211742014-10-28 17:09:06 -07001401 postScanSources();
1402}
1403
Chong Zhangefbb6192015-01-30 17:13:27 -08001404void NuPlayer::onPause() {
1405 if (mPaused) {
1406 return;
1407 }
1408 mPaused = true;
1409 if (mSource != NULL) {
1410 mSource->pause();
1411 } else {
1412 ALOGW("pause called when source is gone or not set");
1413 }
1414 if (mRenderer != NULL) {
1415 mRenderer->pause();
1416 } else {
1417 ALOGW("pause called when renderer is gone or not set");
1418 }
1419}
1420
Ronghua Wud7988b12014-10-03 15:19:10 -07001421bool NuPlayer::audioDecoderStillNeeded() {
1422 // Audio decoder is no longer needed if it's in shut/shutting down status.
1423 return ((mFlushingAudio != SHUT_DOWN) && (mFlushingAudio != SHUTTING_DOWN_DECODER));
1424}
1425
Andy Hung8d121d42014-10-03 09:53:53 -07001426void NuPlayer::handleFlushComplete(bool audio, bool isDecoder) {
1427 // We wait for both the decoder flush and the renderer flush to complete
1428 // before entering either the FLUSHED or the SHUTTING_DOWN_DECODER state.
1429
1430 mFlushComplete[audio][isDecoder] = true;
1431 if (!mFlushComplete[audio][!isDecoder]) {
1432 return;
1433 }
1434
1435 FlushStatus *state = audio ? &mFlushingAudio : &mFlushingVideo;
1436 switch (*state) {
1437 case FLUSHING_DECODER:
1438 {
1439 *state = FLUSHED;
Andy Hung8d121d42014-10-03 09:53:53 -07001440 break;
1441 }
1442
1443 case FLUSHING_DECODER_SHUTDOWN:
1444 {
1445 *state = SHUTTING_DOWN_DECODER;
1446
1447 ALOGV("initiating %s decoder shutdown", audio ? "audio" : "video");
1448 if (!audio) {
Andy Hung8d121d42014-10-03 09:53:53 -07001449 // Widevine source reads must stop before releasing the video decoder.
1450 if (mSource != NULL && mSourceFlags & Source::FLAG_SECURE) {
1451 mSource->stop();
Robert Shih0c61a0d2015-07-06 15:09:10 -07001452 mSourceStarted = false;
Andy Hung8d121d42014-10-03 09:53:53 -07001453 }
1454 }
1455 getDecoder(audio)->initiateShutdown();
1456 break;
1457 }
1458
1459 default:
1460 // decoder flush completes only occur in a flushing state.
1461 LOG_ALWAYS_FATAL_IF(isDecoder, "decoder flush in invalid state %d", *state);
1462 break;
1463 }
1464}
1465
Andreas Huber3831a062010-12-21 10:22:33 -08001466void NuPlayer::finishFlushIfPossible() {
Wei Jia53904f32014-07-29 10:22:53 -07001467 if (mFlushingAudio != NONE && mFlushingAudio != FLUSHED
1468 && mFlushingAudio != SHUT_DOWN) {
Andreas Huber3831a062010-12-21 10:22:33 -08001469 return;
1470 }
1471
Wei Jia53904f32014-07-29 10:22:53 -07001472 if (mFlushingVideo != NONE && mFlushingVideo != FLUSHED
1473 && mFlushingVideo != SHUT_DOWN) {
Andreas Huber3831a062010-12-21 10:22:33 -08001474 return;
1475 }
1476
Steve Block3856b092011-10-20 11:56:00 +01001477 ALOGV("both audio and video are flushed now.");
Andreas Huber3831a062010-12-21 10:22:33 -08001478
Andreas Huber3831a062010-12-21 10:22:33 -08001479 mFlushingAudio = NONE;
1480 mFlushingVideo = NONE;
Andreas Huber3831a062010-12-21 10:22:33 -08001481
Andy Hung8d121d42014-10-03 09:53:53 -07001482 clearFlushComplete();
1483
Andreas Hubera1f8ab02012-11-30 10:53:22 -08001484 processDeferredActions();
Andreas Huber1aef2112011-01-04 14:01:29 -08001485}
1486
1487void NuPlayer::postScanSources() {
1488 if (mScanSourcesPending) {
1489 return;
1490 }
1491
Lajos Molnar1d15ab52015-03-04 16:46:34 -08001492 sp<AMessage> msg = new AMessage(kWhatScanSources, this);
Andreas Huber1aef2112011-01-04 14:01:29 -08001493 msg->setInt32("generation", mScanSourcesGeneration);
1494 msg->post();
1495
1496 mScanSourcesPending = true;
1497}
1498
Wei Jia41cd4632016-05-13 10:53:30 -07001499void NuPlayer::tryOpenAudioSinkForOffload(
1500 const sp<AMessage> &format, const sp<MetaData> &audioMeta, bool hasVideo) {
Andy Hung202bce12014-12-03 11:47:36 -08001501 // Note: This is called early in NuPlayer to determine whether offloading
1502 // is possible; otherwise the decoders call the renderer openAudioSink directly.
Andy Hung282a7e32014-08-14 15:56:34 -07001503
Andy Hung202bce12014-12-03 11:47:36 -08001504 status_t err = mRenderer->openAudioSink(
1505 format, true /* offloadOnly */, hasVideo, AUDIO_OUTPUT_FLAG_NONE, &mOffloadAudio);
1506 if (err != OK) {
1507 // Any failure we turn off mOffloadAudio.
1508 mOffloadAudio = false;
1509 } else if (mOffloadAudio) {
Chong Zhang3b9eb1f2014-10-15 17:05:08 -07001510 sendMetaDataToHal(mAudioSink, audioMeta);
Andy Hung282a7e32014-08-14 15:56:34 -07001511 }
1512}
1513
1514void NuPlayer::closeAudioSink() {
Chong Zhang3b9eb1f2014-10-15 17:05:08 -07001515 mRenderer->closeAudioSink();
Andy Hung282a7e32014-08-14 15:56:34 -07001516}
1517
Wei Jia5031b2f2016-02-25 11:19:31 -08001518void NuPlayer::restartAudio(
Wei Jiabf70feb2016-02-19 15:47:16 -08001519 int64_t currentPositionUs, bool forceNonOffload, bool needsToCreateAudioDecoder) {
Wei Jiaa05f1e32016-03-25 16:31:22 -07001520 if (mAudioDecoder != NULL) {
1521 mAudioDecoder->pause();
1522 mAudioDecoder.clear();
1523 ++mAudioDecoderGeneration;
1524 }
Wei Jiabf70feb2016-02-19 15:47:16 -08001525 if (mFlushingAudio == FLUSHING_DECODER) {
1526 mFlushComplete[1 /* audio */][1 /* isDecoder */] = true;
1527 mFlushingAudio = FLUSHED;
1528 finishFlushIfPossible();
1529 } else if (mFlushingAudio == FLUSHING_DECODER_SHUTDOWN
1530 || mFlushingAudio == SHUTTING_DOWN_DECODER) {
1531 mFlushComplete[1 /* audio */][1 /* isDecoder */] = true;
1532 mFlushingAudio = SHUT_DOWN;
1533 finishFlushIfPossible();
1534 needsToCreateAudioDecoder = false;
1535 }
1536 if (mRenderer == NULL) {
1537 return;
1538 }
1539 closeAudioSink();
1540 mRenderer->flush(true /* audio */, false /* notifyComplete */);
1541 if (mVideoDecoder != NULL) {
1542 mRenderer->flush(false /* audio */, false /* notifyComplete */);
1543 }
1544
Wei Jia14486822016-11-02 17:51:30 -07001545 performSeek(currentPositionUs, false /* precise */);
Wei Jiabf70feb2016-02-19 15:47:16 -08001546
1547 if (forceNonOffload) {
1548 mRenderer->signalDisableOffloadAudio();
1549 mOffloadAudio = false;
1550 }
1551 if (needsToCreateAudioDecoder) {
Wei Jiaa05f1e32016-03-25 16:31:22 -07001552 instantiateDecoder(true /* audio */, &mAudioDecoder, !forceNonOffload);
Wei Jiabf70feb2016-02-19 15:47:16 -08001553 }
1554}
1555
Wei Jia41cd4632016-05-13 10:53:30 -07001556void NuPlayer::determineAudioModeChange(const sp<AMessage> &audioFormat) {
Wei Jiae4d18c72015-07-13 17:58:11 -07001557 if (mSource == NULL || mAudioSink == NULL) {
1558 return;
1559 }
1560
1561 if (mRenderer == NULL) {
1562 ALOGW("No renderer can be used to determine audio mode. Use non-offload for safety.");
1563 mOffloadAudio = false;
1564 return;
1565 }
1566
1567 sp<MetaData> audioMeta = mSource->getFormatMeta(true /* audio */);
1568 sp<AMessage> videoFormat = mSource->getFormat(false /* audio */);
1569 audio_stream_type_t streamType = mAudioSink->getAudioStreamType();
1570 const bool hasVideo = (videoFormat != NULL);
1571 const bool canOffload = canOffloadStream(
Wei Jiabf70feb2016-02-19 15:47:16 -08001572 audioMeta, hasVideo, mSource->isStreaming(), streamType)
1573 && (mPlaybackSettings.mSpeed == 1.f && mPlaybackSettings.mPitch == 1.f);
Wei Jiae4d18c72015-07-13 17:58:11 -07001574 if (canOffload) {
1575 if (!mOffloadAudio) {
1576 mRenderer->signalEnableOffloadAudio();
1577 }
1578 // open audio sink early under offload mode.
Wei Jia41cd4632016-05-13 10:53:30 -07001579 tryOpenAudioSinkForOffload(audioFormat, audioMeta, hasVideo);
Wei Jiae4d18c72015-07-13 17:58:11 -07001580 } else {
Robert Shihe1d70192015-07-23 17:54:13 -07001581 if (mOffloadAudio) {
1582 mRenderer->signalDisableOffloadAudio();
1583 mOffloadAudio = false;
1584 }
Wei Jiae4d18c72015-07-13 17:58:11 -07001585 }
1586}
1587
Wei Jiaa05f1e32016-03-25 16:31:22 -07001588status_t NuPlayer::instantiateDecoder(
1589 bool audio, sp<DecoderBase> *decoder, bool checkAudioModeChange) {
Wei Jia566da802015-08-27 13:59:30 -07001590 // The audio decoder could be cleared by tear down. If still in shut down
1591 // process, no need to create a new audio decoder.
1592 if (*decoder != NULL || (audio && mFlushingAudio == SHUT_DOWN)) {
Andreas Huberf9334412010-12-15 15:17:42 -08001593 return OK;
1594 }
1595
Andreas Huber84066782011-08-16 09:34:26 -07001596 sp<AMessage> format = mSource->getFormat(audio);
Andreas Huberf9334412010-12-15 15:17:42 -08001597
Andreas Huber84066782011-08-16 09:34:26 -07001598 if (format == NULL) {
Robert Shih7350b052015-10-01 15:50:14 -07001599 return UNKNOWN_ERROR;
1600 } else {
1601 status_t err;
1602 if (format->findInt32("err", &err) && err) {
1603 return err;
1604 }
Andreas Huberf9334412010-12-15 15:17:42 -08001605 }
1606
Ronghua Wu8db88132015-04-22 13:51:35 -07001607 format->setInt32("priority", 0 /* realtime */);
1608
Andreas Huber3fe62152011-09-16 15:09:22 -07001609 if (!audio) {
Andreas Huber84066782011-08-16 09:34:26 -07001610 AString mime;
1611 CHECK(format->findString("mime", &mime));
Chong Zhanga7fa1d92014-06-11 14:49:23 -07001612
Lajos Molnar1d15ab52015-03-04 16:46:34 -08001613 sp<AMessage> ccNotify = new AMessage(kWhatClosedCaptionNotify, this);
Chong Zhang341ab6e2015-02-04 13:37:18 -08001614 if (mCCDecoder == NULL) {
1615 mCCDecoder = new CCDecoder(ccNotify);
1616 }
Lajos Molnar09524832014-07-17 14:29:51 -07001617
1618 if (mSourceFlags & Source::FLAG_SECURE) {
1619 format->setInt32("secure", true);
1620 }
Chong Zhang17134602015-01-07 16:14:34 -08001621
1622 if (mSourceFlags & Source::FLAG_PROTECTED) {
1623 format->setInt32("protected", true);
1624 }
Ronghua Wu8db88132015-04-22 13:51:35 -07001625
Ronghua Wuc8a70d32015-04-29 16:26:34 -07001626 float rate = getFrameRate();
1627 if (rate > 0) {
Lajos Molnar3a474aa2015-04-24 17:10:07 -07001628 format->setFloat("operating-rate", rate * mPlaybackSettings.mSpeed);
Ronghua Wu8db88132015-04-22 13:51:35 -07001629 }
Andreas Huber3fe62152011-09-16 15:09:22 -07001630 }
1631
Wei Jiabc2fb722014-07-08 16:37:57 -07001632 if (audio) {
Lajos Molnar1d15ab52015-03-04 16:46:34 -08001633 sp<AMessage> notify = new AMessage(kWhatAudioNotify, this);
Wei Jia88703c32014-08-06 11:24:07 -07001634 ++mAudioDecoderGeneration;
1635 notify->setInt32("generation", mAudioDecoderGeneration);
1636
Wei Jiaa05f1e32016-03-25 16:31:22 -07001637 if (checkAudioModeChange) {
Wei Jia41cd4632016-05-13 10:53:30 -07001638 determineAudioModeChange(format);
Wei Jiaa05f1e32016-03-25 16:31:22 -07001639 }
Wei Jiabc2fb722014-07-08 16:37:57 -07001640 if (mOffloadAudio) {
Wei Jia14532f22015-12-29 11:28:15 -08001641 mSource->setOffloadAudio(true /* offload */);
1642
Haynes Mathew George8b635332015-03-30 17:59:47 -07001643 const bool hasVideo = (mSource->getFormat(false /*audio */) != NULL);
1644 format->setInt32("has-video", hasVideo);
Wei Jiac6cfd702014-11-11 16:33:20 -08001645 *decoder = new DecoderPassThrough(notify, mSource, mRenderer);
Wei Jiabc2fb722014-07-08 16:37:57 -07001646 } else {
Wei Jia14532f22015-12-29 11:28:15 -08001647 mSource->setOffloadAudio(false /* offload */);
1648
Wei Jiaf2ae3e12016-10-27 17:10:59 -07001649 *decoder = new Decoder(notify, mSource, mPID, mUID, mRenderer);
Wei Jiabc2fb722014-07-08 16:37:57 -07001650 }
1651 } else {
Lajos Molnar1d15ab52015-03-04 16:46:34 -08001652 sp<AMessage> notify = new AMessage(kWhatVideoNotify, this);
Wei Jia88703c32014-08-06 11:24:07 -07001653 ++mVideoDecoderGeneration;
1654 notify->setInt32("generation", mVideoDecoderGeneration);
1655
Chong Zhang7137ec72014-11-12 16:41:05 -08001656 *decoder = new Decoder(
Wei Jiaf2ae3e12016-10-27 17:10:59 -07001657 notify, mSource, mPID, mUID, mRenderer, mSurface, mCCDecoder);
Lajos Molnard9fd6312014-11-06 11:00:00 -08001658
1659 // enable FRC if high-quality AV sync is requested, even if not
Lajos Molnar1de1e252015-04-30 18:18:34 -07001660 // directly queuing to display, as this will even improve textureview
Lajos Molnard9fd6312014-11-06 11:00:00 -08001661 // playback.
1662 {
1663 char value[PROPERTY_VALUE_MAX];
1664 if (property_get("persist.sys.media.avsync", value, NULL) &&
1665 (!strcmp("1", value) || !strcasecmp("true", value))) {
1666 format->setInt32("auto-frc", 1);
1667 }
1668 }
Wei Jiabc2fb722014-07-08 16:37:57 -07001669 }
Lajos Molnar1cd13982014-01-17 15:12:51 -08001670 (*decoder)->init();
Andreas Huber84066782011-08-16 09:34:26 -07001671 (*decoder)->configure(format);
Andreas Huberf9334412010-12-15 15:17:42 -08001672
Lajos Molnar09524832014-07-17 14:29:51 -07001673 // allocate buffers to decrypt widevine source buffers
1674 if (!audio && (mSourceFlags & Source::FLAG_SECURE)) {
Wonsik Kim7e34bf52016-08-23 00:09:18 +09001675 Vector<sp<MediaCodecBuffer> > inputBufs;
Lajos Molnar09524832014-07-17 14:29:51 -07001676 CHECK_EQ((*decoder)->getInputBuffers(&inputBufs), (status_t)OK);
1677
1678 Vector<MediaBuffer *> mediaBufs;
1679 for (size_t i = 0; i < inputBufs.size(); i++) {
Wonsik Kim7e34bf52016-08-23 00:09:18 +09001680 const sp<MediaCodecBuffer> &buffer = inputBufs[i];
Lajos Molnar09524832014-07-17 14:29:51 -07001681 MediaBuffer *mbuf = new MediaBuffer(buffer->data(), buffer->size());
1682 mediaBufs.push(mbuf);
1683 }
1684
1685 status_t err = mSource->setBuffers(audio, mediaBufs);
1686 if (err != OK) {
1687 for (size_t i = 0; i < mediaBufs.size(); ++i) {
1688 mediaBufs[i]->release();
1689 }
1690 mediaBufs.clear();
1691 ALOGE("Secure source didn't support secure mediaBufs.");
1692 return err;
1693 }
1694 }
Praveen Chavanbbaa1442016-04-08 13:33:49 -07001695
1696 if (!audio) {
1697 sp<AMessage> params = new AMessage();
1698 float rate = getFrameRate();
1699 if (rate > 0) {
1700 params->setFloat("frame-rate-total", rate);
1701 }
1702
1703 sp<MetaData> fileMeta = getFileMeta();
1704 if (fileMeta != NULL) {
1705 int32_t videoTemporalLayerCount;
1706 if (fileMeta->findInt32(kKeyTemporalLayerCount, &videoTemporalLayerCount)
1707 && videoTemporalLayerCount > 0) {
1708 params->setInt32("temporal-layer-count", videoTemporalLayerCount);
1709 }
1710 }
1711
1712 if (params->countEntries() > 0) {
1713 (*decoder)->setParameters(params);
1714 }
1715 }
Andreas Huberf9334412010-12-15 15:17:42 -08001716 return OK;
1717}
1718
Chong Zhangced1c2f2014-08-08 15:22:35 -07001719void NuPlayer::updateVideoSize(
1720 const sp<AMessage> &inputFormat,
1721 const sp<AMessage> &outputFormat) {
1722 if (inputFormat == NULL) {
1723 ALOGW("Unknown video size, reporting 0x0!");
1724 notifyListener(MEDIA_SET_VIDEO_SIZE, 0, 0);
1725 return;
1726 }
1727
1728 int32_t displayWidth, displayHeight;
Chong Zhangced1c2f2014-08-08 15:22:35 -07001729 if (outputFormat != NULL) {
1730 int32_t width, height;
1731 CHECK(outputFormat->findInt32("width", &width));
1732 CHECK(outputFormat->findInt32("height", &height));
1733
1734 int32_t cropLeft, cropTop, cropRight, cropBottom;
1735 CHECK(outputFormat->findRect(
1736 "crop",
1737 &cropLeft, &cropTop, &cropRight, &cropBottom));
1738
1739 displayWidth = cropRight - cropLeft + 1;
1740 displayHeight = cropBottom - cropTop + 1;
1741
1742 ALOGV("Video output format changed to %d x %d "
1743 "(crop: %d x %d @ (%d, %d))",
1744 width, height,
1745 displayWidth,
1746 displayHeight,
1747 cropLeft, cropTop);
1748 } else {
1749 CHECK(inputFormat->findInt32("width", &displayWidth));
1750 CHECK(inputFormat->findInt32("height", &displayHeight));
1751
1752 ALOGV("Video input format %d x %d", displayWidth, displayHeight);
1753 }
1754
1755 // Take into account sample aspect ratio if necessary:
1756 int32_t sarWidth, sarHeight;
1757 if (inputFormat->findInt32("sar-width", &sarWidth)
1758 && inputFormat->findInt32("sar-height", &sarHeight)) {
1759 ALOGV("Sample aspect ratio %d : %d", sarWidth, sarHeight);
1760
1761 displayWidth = (displayWidth * sarWidth) / sarHeight;
1762
1763 ALOGV("display dimensions %d x %d", displayWidth, displayHeight);
Wei Jiadf6c6af2016-09-29 11:27:15 -07001764 } else {
1765 int32_t width, height;
1766 if (inputFormat->findInt32("display-width", &width)
1767 && inputFormat->findInt32("display-height", &height)
1768 && width > 0 && height > 0
1769 && displayWidth > 0 && displayHeight > 0) {
1770 if (displayHeight * (int64_t)width / height > (int64_t)displayWidth) {
1771 displayHeight = (int32_t)(displayWidth * (int64_t)height / width);
1772 } else {
1773 displayWidth = (int32_t)(displayHeight * (int64_t)width / height);
1774 }
1775 ALOGV("Video display width and height are overridden to %d x %d",
1776 displayWidth, displayHeight);
1777 }
Chong Zhangced1c2f2014-08-08 15:22:35 -07001778 }
1779
1780 int32_t rotationDegrees;
1781 if (!inputFormat->findInt32("rotation-degrees", &rotationDegrees)) {
1782 rotationDegrees = 0;
1783 }
1784
1785 if (rotationDegrees == 90 || rotationDegrees == 270) {
1786 int32_t tmp = displayWidth;
1787 displayWidth = displayHeight;
1788 displayHeight = tmp;
1789 }
1790
1791 notifyListener(
1792 MEDIA_SET_VIDEO_SIZE,
1793 displayWidth,
1794 displayHeight);
1795}
1796
Chong Zhangdcb89b32013-08-06 09:44:47 -07001797void NuPlayer::notifyListener(int msg, int ext1, int ext2, const Parcel *in) {
Andreas Huber43c3e6c2011-01-05 12:17:08 -08001798 if (mDriver == NULL) {
Andreas Huberf9334412010-12-15 15:17:42 -08001799 return;
1800 }
1801
Andreas Huber43c3e6c2011-01-05 12:17:08 -08001802 sp<NuPlayerDriver> driver = mDriver.promote();
Andreas Huberf9334412010-12-15 15:17:42 -08001803
Andreas Huber43c3e6c2011-01-05 12:17:08 -08001804 if (driver == NULL) {
Andreas Huberf9334412010-12-15 15:17:42 -08001805 return;
1806 }
1807
Chong Zhangdcb89b32013-08-06 09:44:47 -07001808 driver->notifyListener(msg, ext1, ext2, in);
Andreas Huberf9334412010-12-15 15:17:42 -08001809}
1810
Chong Zhang7137ec72014-11-12 16:41:05 -08001811void NuPlayer::flushDecoder(bool audio, bool needShutdown) {
Andreas Huber14f76722013-01-15 09:04:18 -08001812 ALOGV("[%s] flushDecoder needShutdown=%d",
1813 audio ? "audio" : "video", needShutdown);
1814
Chong Zhang7137ec72014-11-12 16:41:05 -08001815 const sp<DecoderBase> &decoder = getDecoder(audio);
Lajos Molnar87603c02014-08-20 19:25:30 -07001816 if (decoder == NULL) {
Steve Blockdf64d152012-01-04 20:05:49 +00001817 ALOGI("flushDecoder %s without decoder present",
Andreas Huber6e3d3112011-11-28 12:36:11 -08001818 audio ? "audio" : "video");
Lajos Molnar87603c02014-08-20 19:25:30 -07001819 return;
Andreas Huber6e3d3112011-11-28 12:36:11 -08001820 }
1821
Andreas Huber1aef2112011-01-04 14:01:29 -08001822 // Make sure we don't continue to scan sources until we finish flushing.
1823 ++mScanSourcesGeneration;
Chong Zhang3b032b32015-04-17 15:49:06 -07001824 if (mScanSourcesPending) {
Toshikazu Saitocbcbb792015-09-15 14:53:01 +09001825 if (!needShutdown) {
1826 mDeferredActions.push_back(
1827 new SimpleAction(&NuPlayer::performScanSources));
1828 }
Chong Zhang3b032b32015-04-17 15:49:06 -07001829 mScanSourcesPending = false;
1830 }
Andreas Huber1aef2112011-01-04 14:01:29 -08001831
Chong Zhang7137ec72014-11-12 16:41:05 -08001832 decoder->signalFlush();
Andreas Huber1aef2112011-01-04 14:01:29 -08001833
1834 FlushStatus newStatus =
1835 needShutdown ? FLUSHING_DECODER_SHUTDOWN : FLUSHING_DECODER;
1836
Lajos Molnarfcd3e942015-03-31 10:06:48 -07001837 mFlushComplete[audio][false /* isDecoder */] = (mRenderer == NULL);
Andy Hung8d121d42014-10-03 09:53:53 -07001838 mFlushComplete[audio][true /* isDecoder */] = false;
Andreas Huber1aef2112011-01-04 14:01:29 -08001839 if (audio) {
Wei Jia53904f32014-07-29 10:22:53 -07001840 ALOGE_IF(mFlushingAudio != NONE,
1841 "audio flushDecoder() is called in state %d", mFlushingAudio);
Andreas Huber1aef2112011-01-04 14:01:29 -08001842 mFlushingAudio = newStatus;
Andreas Huber1aef2112011-01-04 14:01:29 -08001843 } else {
Wei Jia53904f32014-07-29 10:22:53 -07001844 ALOGE_IF(mFlushingVideo != NONE,
1845 "video flushDecoder() is called in state %d", mFlushingVideo);
Andreas Huber1aef2112011-01-04 14:01:29 -08001846 mFlushingVideo = newStatus;
Andreas Huber1aef2112011-01-04 14:01:29 -08001847 }
1848}
1849
Chong Zhangced1c2f2014-08-08 15:22:35 -07001850void NuPlayer::queueDecoderShutdown(
1851 bool audio, bool video, const sp<AMessage> &reply) {
1852 ALOGI("queueDecoderShutdown audio=%d, video=%d", audio, video);
Andreas Huber84066782011-08-16 09:34:26 -07001853
Chong Zhangced1c2f2014-08-08 15:22:35 -07001854 mDeferredActions.push_back(
Wei Jiafef808d2014-10-31 17:57:05 -07001855 new FlushDecoderAction(
1856 audio ? FLUSH_CMD_SHUTDOWN : FLUSH_CMD_NONE,
1857 video ? FLUSH_CMD_SHUTDOWN : FLUSH_CMD_NONE));
Andreas Huber84066782011-08-16 09:34:26 -07001858
Chong Zhangced1c2f2014-08-08 15:22:35 -07001859 mDeferredActions.push_back(
1860 new SimpleAction(&NuPlayer::performScanSources));
Andreas Huber84066782011-08-16 09:34:26 -07001861
Chong Zhangced1c2f2014-08-08 15:22:35 -07001862 mDeferredActions.push_back(new PostMessageAction(reply));
1863
1864 processDeferredActions();
Andreas Huber84066782011-08-16 09:34:26 -07001865}
1866
James Dong0d268a32012-08-31 12:18:27 -07001867status_t NuPlayer::setVideoScalingMode(int32_t mode) {
1868 mVideoScalingMode = mode;
Lajos Molnar1de1e252015-04-30 18:18:34 -07001869 if (mSurface != NULL) {
1870 status_t ret = native_window_set_scaling_mode(mSurface.get(), mVideoScalingMode);
James Dong0d268a32012-08-31 12:18:27 -07001871 if (ret != OK) {
1872 ALOGE("Failed to set scaling mode (%d): %s",
1873 -ret, strerror(-ret));
1874 return ret;
1875 }
1876 }
1877 return OK;
1878}
1879
Chong Zhangdcb89b32013-08-06 09:44:47 -07001880status_t NuPlayer::getTrackInfo(Parcel* reply) const {
Lajos Molnar1d15ab52015-03-04 16:46:34 -08001881 sp<AMessage> msg = new AMessage(kWhatGetTrackInfo, this);
Chong Zhangdcb89b32013-08-06 09:44:47 -07001882 msg->setPointer("reply", reply);
1883
1884 sp<AMessage> response;
1885 status_t err = msg->postAndAwaitResponse(&response);
1886 return err;
1887}
1888
Robert Shih7c4f0d72014-07-09 18:53:31 -07001889status_t NuPlayer::getSelectedTrack(int32_t type, Parcel* reply) const {
Lajos Molnar1d15ab52015-03-04 16:46:34 -08001890 sp<AMessage> msg = new AMessage(kWhatGetSelectedTrack, this);
Robert Shih7c4f0d72014-07-09 18:53:31 -07001891 msg->setPointer("reply", reply);
1892 msg->setInt32("type", type);
1893
1894 sp<AMessage> response;
1895 status_t err = msg->postAndAwaitResponse(&response);
1896 if (err == OK && response != NULL) {
1897 CHECK(response->findInt32("err", &err));
1898 }
1899 return err;
1900}
1901
Robert Shih6ffb1fd2014-10-29 16:24:32 -07001902status_t NuPlayer::selectTrack(size_t trackIndex, bool select, int64_t timeUs) {
Lajos Molnar1d15ab52015-03-04 16:46:34 -08001903 sp<AMessage> msg = new AMessage(kWhatSelectTrack, this);
Chong Zhangdcb89b32013-08-06 09:44:47 -07001904 msg->setSize("trackIndex", trackIndex);
1905 msg->setInt32("select", select);
Robert Shih6ffb1fd2014-10-29 16:24:32 -07001906 msg->setInt64("timeUs", timeUs);
Chong Zhangdcb89b32013-08-06 09:44:47 -07001907
1908 sp<AMessage> response;
1909 status_t err = msg->postAndAwaitResponse(&response);
1910
Chong Zhang404fced2014-06-11 14:45:31 -07001911 if (err != OK) {
1912 return err;
1913 }
1914
1915 if (!response->findInt32("err", &err)) {
1916 err = OK;
1917 }
1918
Chong Zhangdcb89b32013-08-06 09:44:47 -07001919 return err;
1920}
1921
Ronghua Wua73d9e02014-10-08 15:13:29 -07001922status_t NuPlayer::getCurrentPosition(int64_t *mediaUs) {
1923 sp<Renderer> renderer = mRenderer;
1924 if (renderer == NULL) {
1925 return NO_INIT;
1926 }
1927
1928 return renderer->getCurrentPosition(mediaUs);
1929}
1930
Praveen Chavane1e5d7a2015-05-19 19:09:48 -07001931void NuPlayer::getStats(Vector<sp<AMessage> > *mTrackStats) {
1932 CHECK(mTrackStats != NULL);
1933
1934 mTrackStats->clear();
1935 if (mVideoDecoder != NULL) {
1936 mTrackStats->push_back(mVideoDecoder->getStats());
1937 }
1938 if (mAudioDecoder != NULL) {
1939 mTrackStats->push_back(mAudioDecoder->getStats());
Chong Zhang7137ec72014-11-12 16:41:05 -08001940 }
Ronghua Wua73d9e02014-10-08 15:13:29 -07001941}
1942
Marco Nelissenf0b72b52014-09-16 15:43:44 -07001943sp<MetaData> NuPlayer::getFileMeta() {
1944 return mSource->getFileFormatMeta();
1945}
1946
Ronghua Wuc8a70d32015-04-29 16:26:34 -07001947float NuPlayer::getFrameRate() {
1948 sp<MetaData> meta = mSource->getFormatMeta(false /* audio */);
1949 if (meta == NULL) {
1950 return 0;
1951 }
1952 int32_t rate;
1953 if (!meta->findInt32(kKeyFrameRate, &rate)) {
1954 // fall back to try file meta
1955 sp<MetaData> fileMeta = getFileMeta();
1956 if (fileMeta == NULL) {
1957 ALOGW("source has video meta but not file meta");
1958 return -1;
1959 }
1960 int32_t fileMetaRate;
1961 if (!fileMeta->findInt32(kKeyFrameRate, &fileMetaRate)) {
1962 return -1;
1963 }
1964 return fileMetaRate;
1965 }
1966 return rate;
1967}
1968
Andreas Huberb7c8e912012-11-27 15:02:53 -08001969void NuPlayer::schedulePollDuration() {
Lajos Molnar1d15ab52015-03-04 16:46:34 -08001970 sp<AMessage> msg = new AMessage(kWhatPollDuration, this);
Andreas Huberb7c8e912012-11-27 15:02:53 -08001971 msg->setInt32("generation", mPollDurationGeneration);
1972 msg->post();
1973}
1974
1975void NuPlayer::cancelPollDuration() {
1976 ++mPollDurationGeneration;
1977}
1978
Andreas Hubera1f8ab02012-11-30 10:53:22 -08001979void NuPlayer::processDeferredActions() {
1980 while (!mDeferredActions.empty()) {
1981 // We won't execute any deferred actions until we're no longer in
1982 // an intermediate state, i.e. one more more decoders are currently
1983 // flushing or shutting down.
1984
Andreas Hubera1f8ab02012-11-30 10:53:22 -08001985 if (mFlushingAudio != NONE || mFlushingVideo != NONE) {
1986 // We're currently flushing, postpone the reset until that's
1987 // completed.
1988
1989 ALOGV("postponing action mFlushingAudio=%d, mFlushingVideo=%d",
1990 mFlushingAudio, mFlushingVideo);
1991
1992 break;
1993 }
1994
1995 sp<Action> action = *mDeferredActions.begin();
1996 mDeferredActions.erase(mDeferredActions.begin());
1997
1998 action->execute(this);
1999 }
2000}
2001
Wei Jia14486822016-11-02 17:51:30 -07002002void NuPlayer::performSeek(int64_t seekTimeUs, bool precise) {
2003 ALOGV("performSeek seekTimeUs=%lld us (%.2f secs), precise=%d",
2004 (long long)seekTimeUs, seekTimeUs / 1E6, precise);
Andreas Hubera1f8ab02012-11-30 10:53:22 -08002005
Andy Hungadf34bf2014-09-03 18:22:22 -07002006 if (mSource == NULL) {
2007 // This happens when reset occurs right before the loop mode
2008 // asynchronously seeks to the start of the stream.
2009 LOG_ALWAYS_FATAL_IF(mAudioDecoder != NULL || mVideoDecoder != NULL,
2010 "mSource is NULL and decoders not NULL audio(%p) video(%p)",
2011 mAudioDecoder.get(), mVideoDecoder.get());
2012 return;
2013 }
Robert Shih1a5c8592015-08-04 18:07:44 -07002014 mPreviousSeekTimeUs = seekTimeUs;
Wei Jia14486822016-11-02 17:51:30 -07002015 mSource->seekTo(seekTimeUs, precise);
Robert Shihd3b0bbb2014-07-23 15:00:25 -07002016 ++mTimedTextGeneration;
Andreas Hubera1f8ab02012-11-30 10:53:22 -08002017
Andreas Hubera1f8ab02012-11-30 10:53:22 -08002018 // everything's flushed, continue playback.
2019}
2020
Wei Jiafef808d2014-10-31 17:57:05 -07002021void NuPlayer::performDecoderFlush(FlushCommand audio, FlushCommand video) {
2022 ALOGV("performDecoderFlush audio=%d, video=%d", audio, video);
Andreas Hubera1f8ab02012-11-30 10:53:22 -08002023
Wei Jiafef808d2014-10-31 17:57:05 -07002024 if ((audio == FLUSH_CMD_NONE || mAudioDecoder == NULL)
2025 && (video == FLUSH_CMD_NONE || mVideoDecoder == NULL)) {
Andreas Hubera1f8ab02012-11-30 10:53:22 -08002026 return;
2027 }
2028
Wei Jiafef808d2014-10-31 17:57:05 -07002029 if (audio != FLUSH_CMD_NONE && mAudioDecoder != NULL) {
2030 flushDecoder(true /* audio */, (audio == FLUSH_CMD_SHUTDOWN));
Andreas Hubera1f8ab02012-11-30 10:53:22 -08002031 }
2032
Wei Jiafef808d2014-10-31 17:57:05 -07002033 if (video != FLUSH_CMD_NONE && mVideoDecoder != NULL) {
2034 flushDecoder(false /* audio */, (video == FLUSH_CMD_SHUTDOWN));
Andreas Hubera1f8ab02012-11-30 10:53:22 -08002035 }
2036}
2037
2038void NuPlayer::performReset() {
2039 ALOGV("performReset");
2040
2041 CHECK(mAudioDecoder == NULL);
2042 CHECK(mVideoDecoder == NULL);
2043
2044 cancelPollDuration();
2045
2046 ++mScanSourcesGeneration;
2047 mScanSourcesPending = false;
2048
Lajos Molnar09524832014-07-17 14:29:51 -07002049 if (mRendererLooper != NULL) {
2050 if (mRenderer != NULL) {
2051 mRendererLooper->unregisterHandler(mRenderer->id());
2052 }
2053 mRendererLooper->stop();
2054 mRendererLooper.clear();
2055 }
Andreas Hubera1f8ab02012-11-30 10:53:22 -08002056 mRenderer.clear();
Wei Jia57568df2014-09-22 10:16:29 -07002057 ++mRendererGeneration;
Andreas Hubera1f8ab02012-11-30 10:53:22 -08002058
2059 if (mSource != NULL) {
2060 mSource->stop();
Andreas Huberb5f25f02013-02-05 10:14:26 -08002061
Wei Jiac45a4b22016-04-15 15:30:23 -07002062 Mutex::Autolock autoLock(mSourceLock);
Andreas Hubera1f8ab02012-11-30 10:53:22 -08002063 mSource.clear();
2064 }
2065
2066 if (mDriver != NULL) {
2067 sp<NuPlayerDriver> driver = mDriver.promote();
2068 if (driver != NULL) {
2069 driver->notifyResetComplete();
2070 }
2071 }
Andreas Huber57a339c2012-12-03 11:18:00 -08002072
2073 mStarted = false;
Wei Jia8a092d32016-06-03 14:57:24 -07002074 mPrepared = false;
Ronghua Wu64c2d172015-10-07 16:52:19 -07002075 mResetting = false;
Robert Shih0c61a0d2015-07-06 15:09:10 -07002076 mSourceStarted = false;
Andreas Hubera1f8ab02012-11-30 10:53:22 -08002077}
2078
2079void NuPlayer::performScanSources() {
2080 ALOGV("performScanSources");
2081
Andreas Huber57a339c2012-12-03 11:18:00 -08002082 if (!mStarted) {
2083 return;
2084 }
2085
Andreas Hubera1f8ab02012-11-30 10:53:22 -08002086 if (mAudioDecoder == NULL || mVideoDecoder == NULL) {
2087 postScanSources();
2088 }
2089}
2090
Lajos Molnar1de1e252015-04-30 18:18:34 -07002091void NuPlayer::performSetSurface(const sp<Surface> &surface) {
Andreas Huber57a339c2012-12-03 11:18:00 -08002092 ALOGV("performSetSurface");
2093
Lajos Molnar1de1e252015-04-30 18:18:34 -07002094 mSurface = surface;
Andreas Huber57a339c2012-12-03 11:18:00 -08002095
2096 // XXX - ignore error from setVideoScalingMode for now
2097 setVideoScalingMode(mVideoScalingMode);
Chong Zhang13d6faa2014-08-22 15:35:28 -07002098
2099 if (mDriver != NULL) {
2100 sp<NuPlayerDriver> driver = mDriver.promote();
2101 if (driver != NULL) {
2102 driver->notifySetSurfaceComplete();
2103 }
2104 }
Andreas Huber57a339c2012-12-03 11:18:00 -08002105}
2106
Chong Zhangf8d71772014-11-26 15:08:34 -08002107void NuPlayer::performResumeDecoders(bool needNotify) {
2108 if (needNotify) {
2109 mResumePending = true;
2110 if (mVideoDecoder == NULL) {
2111 // if audio-only, we can notify seek complete now,
2112 // as the resume operation will be relatively fast.
2113 finishResume();
2114 }
2115 }
2116
Chong Zhang7137ec72014-11-12 16:41:05 -08002117 if (mVideoDecoder != NULL) {
Chong Zhangf8d71772014-11-26 15:08:34 -08002118 // When there is continuous seek, MediaPlayer will cache the seek
2119 // position, and send down new seek request when previous seek is
2120 // complete. Let's wait for at least one video output frame before
2121 // notifying seek complete, so that the video thumbnail gets updated
2122 // when seekbar is dragged.
2123 mVideoDecoder->signalResume(needNotify);
Chong Zhang7137ec72014-11-12 16:41:05 -08002124 }
2125
2126 if (mAudioDecoder != NULL) {
Chong Zhangf8d71772014-11-26 15:08:34 -08002127 mAudioDecoder->signalResume(false /* needNotify */);
2128 }
2129}
2130
2131void NuPlayer::finishResume() {
2132 if (mResumePending) {
2133 mResumePending = false;
Wei Jia1061c9c2015-05-19 16:02:17 -07002134 notifyDriverSeekComplete();
2135 }
2136}
2137
2138void NuPlayer::notifyDriverSeekComplete() {
2139 if (mDriver != NULL) {
2140 sp<NuPlayerDriver> driver = mDriver.promote();
2141 if (driver != NULL) {
2142 driver->notifySeekComplete();
Chong Zhangf8d71772014-11-26 15:08:34 -08002143 }
Chong Zhang7137ec72014-11-12 16:41:05 -08002144 }
2145}
2146
Andreas Huber9575c962013-02-05 13:59:56 -08002147void NuPlayer::onSourceNotify(const sp<AMessage> &msg) {
2148 int32_t what;
2149 CHECK(msg->findInt32("what", &what));
2150
2151 switch (what) {
Lajos Molnarfcd3e942015-03-31 10:06:48 -07002152 case Source::kWhatInstantiateSecureDecoders:
2153 {
2154 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
2161 sp<AMessage> reply;
2162 CHECK(msg->findMessage("reply", &reply));
2163 status_t err = onInstantiateSecureDecoders();
2164 reply->setInt32("err", err);
2165 reply->post();
2166 break;
2167 }
2168
Andreas Huber9575c962013-02-05 13:59:56 -08002169 case Source::kWhatPrepared:
2170 {
Andreas Huberb5f28d42013-04-25 15:11:19 -07002171 if (mSource == NULL) {
2172 // This is a stale notification from a source that was
2173 // asynchronously preparing when the client called reset().
2174 // We handled the reset, the source is gone.
2175 break;
2176 }
2177
Andreas Huberec0c5972013-02-05 14:47:13 -08002178 int32_t err;
2179 CHECK(msg->findInt32("err", &err));
2180
Lajos Molnarfcd3e942015-03-31 10:06:48 -07002181 if (err != OK) {
2182 // shut down potential secure codecs in case client never calls reset
2183 mDeferredActions.push_back(
2184 new FlushDecoderAction(FLUSH_CMD_SHUTDOWN /* audio */,
2185 FLUSH_CMD_SHUTDOWN /* video */));
2186 processDeferredActions();
Wei Jia8a092d32016-06-03 14:57:24 -07002187 } else {
2188 mPrepared = true;
Lajos Molnarfcd3e942015-03-31 10:06:48 -07002189 }
2190
Andreas Huber9575c962013-02-05 13:59:56 -08002191 sp<NuPlayerDriver> driver = mDriver.promote();
2192 if (driver != NULL) {
Marco Nelissendd114d12014-05-28 15:23:14 -07002193 // notify duration first, so that it's definitely set when
2194 // the app received the "prepare complete" callback.
2195 int64_t durationUs;
2196 if (mSource->getDuration(&durationUs) == OK) {
2197 driver->notifyDuration(durationUs);
2198 }
Andreas Huberec0c5972013-02-05 14:47:13 -08002199 driver->notifyPrepareCompleted(err);
Andreas Huber9575c962013-02-05 13:59:56 -08002200 }
Andreas Huber99759402013-04-01 14:28:31 -07002201
Andreas Huber9575c962013-02-05 13:59:56 -08002202 break;
2203 }
2204
2205 case Source::kWhatFlagsChanged:
2206 {
2207 uint32_t flags;
2208 CHECK(msg->findInt32("flags", (int32_t *)&flags));
2209
Chong Zhang4b7069d2013-09-11 12:52:43 -07002210 sp<NuPlayerDriver> driver = mDriver.promote();
2211 if (driver != NULL) {
Wei Jia895651b2014-12-10 17:31:52 -08002212 if ((flags & NuPlayer::Source::FLAG_CAN_SEEK) == 0) {
2213 driver->notifyListener(
2214 MEDIA_INFO, MEDIA_INFO_NOT_SEEKABLE, 0);
2215 }
Chong Zhang4b7069d2013-09-11 12:52:43 -07002216 driver->notifyFlagsChanged(flags);
2217 }
2218
Andreas Huber9575c962013-02-05 13:59:56 -08002219 if ((mSourceFlags & Source::FLAG_DYNAMIC_DURATION)
2220 && (!(flags & Source::FLAG_DYNAMIC_DURATION))) {
2221 cancelPollDuration();
2222 } else if (!(mSourceFlags & Source::FLAG_DYNAMIC_DURATION)
2223 && (flags & Source::FLAG_DYNAMIC_DURATION)
2224 && (mAudioDecoder != NULL || mVideoDecoder != NULL)) {
2225 schedulePollDuration();
2226 }
2227
2228 mSourceFlags = flags;
2229 break;
2230 }
2231
2232 case Source::kWhatVideoSizeChanged:
2233 {
Chong Zhangced1c2f2014-08-08 15:22:35 -07002234 sp<AMessage> format;
2235 CHECK(msg->findMessage("format", &format));
Andreas Huber9575c962013-02-05 13:59:56 -08002236
Chong Zhangced1c2f2014-08-08 15:22:35 -07002237 updateVideoSize(format);
Andreas Huber9575c962013-02-05 13:59:56 -08002238 break;
2239 }
2240
Chong Zhang2a3cc9a2014-08-21 17:48:26 -07002241 case Source::kWhatBufferingUpdate:
2242 {
2243 int32_t percentage;
2244 CHECK(msg->findInt32("percentage", &percentage));
2245
2246 notifyListener(MEDIA_BUFFERING_UPDATE, percentage, 0);
2247 break;
2248 }
2249
Chong Zhangefbb6192015-01-30 17:13:27 -08002250 case Source::kWhatPauseOnBufferingStart:
2251 {
2252 // ignore if not playing
Chong Zhang8a048332015-05-06 15:16:28 -07002253 if (mStarted) {
Chong Zhangefbb6192015-01-30 17:13:27 -08002254 ALOGI("buffer low, pausing...");
2255
Chong Zhang8a048332015-05-06 15:16:28 -07002256 mPausedForBuffering = true;
Chong Zhangefbb6192015-01-30 17:13:27 -08002257 onPause();
2258 }
Wei Jiabfe82072016-05-20 09:21:50 -07002259 notifyListener(MEDIA_INFO, MEDIA_INFO_BUFFERING_START, 0);
Roger Jönssonb50e83e2013-01-21 16:26:41 +01002260 break;
2261 }
2262
Chong Zhangefbb6192015-01-30 17:13:27 -08002263 case Source::kWhatResumeOnBufferingEnd:
2264 {
2265 // ignore if not playing
Chong Zhang8a048332015-05-06 15:16:28 -07002266 if (mStarted) {
Chong Zhangefbb6192015-01-30 17:13:27 -08002267 ALOGI("buffer ready, resuming...");
2268
Chong Zhang8a048332015-05-06 15:16:28 -07002269 mPausedForBuffering = false;
2270
2271 // do not resume yet if client didn't unpause
2272 if (!mPausedByClient) {
2273 onResume();
2274 }
Chong Zhangefbb6192015-01-30 17:13:27 -08002275 }
Wei Jia3bed45a2016-02-17 11:06:47 -08002276 notifyListener(MEDIA_INFO, MEDIA_INFO_BUFFERING_END, 0);
Roger Jönssonb50e83e2013-01-21 16:26:41 +01002277 break;
2278 }
2279
Chong Zhangefbb6192015-01-30 17:13:27 -08002280 case Source::kWhatCacheStats:
2281 {
2282 int32_t kbps;
2283 CHECK(msg->findInt32("bandwidth", &kbps));
2284
2285 notifyListener(MEDIA_INFO, MEDIA_INFO_NETWORK_BANDWIDTH, kbps);
2286 break;
2287 }
2288
Chong Zhangdcb89b32013-08-06 09:44:47 -07002289 case Source::kWhatSubtitleData:
2290 {
2291 sp<ABuffer> buffer;
2292 CHECK(msg->findBuffer("buffer", &buffer));
2293
Chong Zhang404fced2014-06-11 14:45:31 -07002294 sendSubtitleData(buffer, 0 /* baseIndex */);
Chong Zhangdcb89b32013-08-06 09:44:47 -07002295 break;
2296 }
2297
Robert Shih08528432015-04-08 09:06:54 -07002298 case Source::kWhatTimedMetaData:
2299 {
2300 sp<ABuffer> buffer;
2301 if (!msg->findBuffer("buffer", &buffer)) {
2302 notifyListener(MEDIA_INFO, MEDIA_INFO_METADATA_UPDATE, 0);
2303 } else {
2304 sendTimedMetaData(buffer);
2305 }
2306 break;
2307 }
2308
Robert Shihd3b0bbb2014-07-23 15:00:25 -07002309 case Source::kWhatTimedTextData:
2310 {
2311 int32_t generation;
2312 if (msg->findInt32("generation", &generation)
2313 && generation != mTimedTextGeneration) {
2314 break;
2315 }
2316
2317 sp<ABuffer> buffer;
2318 CHECK(msg->findBuffer("buffer", &buffer));
2319
2320 sp<NuPlayerDriver> driver = mDriver.promote();
2321 if (driver == NULL) {
2322 break;
2323 }
2324
2325 int posMs;
2326 int64_t timeUs, posUs;
2327 driver->getCurrentPosition(&posMs);
Patrik2 Carlsson24d484b2015-01-27 16:49:45 +01002328 posUs = (int64_t) posMs * 1000ll;
Robert Shihd3b0bbb2014-07-23 15:00:25 -07002329 CHECK(buffer->meta()->findInt64("timeUs", &timeUs));
2330
2331 if (posUs < timeUs) {
2332 if (!msg->findInt32("generation", &generation)) {
2333 msg->setInt32("generation", mTimedTextGeneration);
2334 }
2335 msg->post(timeUs - posUs);
2336 } else {
2337 sendTimedTextData(buffer);
2338 }
2339 break;
2340 }
2341
Andreas Huber14f76722013-01-15 09:04:18 -08002342 case Source::kWhatQueueDecoderShutdown:
2343 {
2344 int32_t audio, video;
2345 CHECK(msg->findInt32("audio", &audio));
2346 CHECK(msg->findInt32("video", &video));
2347
2348 sp<AMessage> reply;
2349 CHECK(msg->findMessage("reply", &reply));
2350
2351 queueDecoderShutdown(audio, video, reply);
2352 break;
2353 }
2354
Ronghua Wu80276872014-08-28 15:50:29 -07002355 case Source::kWhatDrmNoLicense:
2356 {
2357 notifyListener(MEDIA_ERROR, MEDIA_ERROR_UNKNOWN, ERROR_DRM_NO_LICENSE);
2358 break;
2359 }
2360
Andreas Huber9575c962013-02-05 13:59:56 -08002361 default:
2362 TRESPASS();
2363 }
2364}
2365
Chong Zhanga7fa1d92014-06-11 14:49:23 -07002366void NuPlayer::onClosedCaptionNotify(const sp<AMessage> &msg) {
2367 int32_t what;
2368 CHECK(msg->findInt32("what", &what));
2369
2370 switch (what) {
2371 case NuPlayer::CCDecoder::kWhatClosedCaptionData:
2372 {
2373 sp<ABuffer> buffer;
2374 CHECK(msg->findBuffer("buffer", &buffer));
2375
2376 size_t inbandTracks = 0;
2377 if (mSource != NULL) {
2378 inbandTracks = mSource->getTrackCount();
2379 }
2380
2381 sendSubtitleData(buffer, inbandTracks);
2382 break;
2383 }
2384
2385 case NuPlayer::CCDecoder::kWhatTrackAdded:
2386 {
2387 notifyListener(MEDIA_INFO, MEDIA_INFO_METADATA_UPDATE, 0);
2388
2389 break;
2390 }
2391
2392 default:
2393 TRESPASS();
2394 }
2395
2396
2397}
2398
Chong Zhang404fced2014-06-11 14:45:31 -07002399void NuPlayer::sendSubtitleData(const sp<ABuffer> &buffer, int32_t baseIndex) {
2400 int32_t trackIndex;
2401 int64_t timeUs, durationUs;
2402 CHECK(buffer->meta()->findInt32("trackIndex", &trackIndex));
2403 CHECK(buffer->meta()->findInt64("timeUs", &timeUs));
2404 CHECK(buffer->meta()->findInt64("durationUs", &durationUs));
2405
2406 Parcel in;
2407 in.writeInt32(trackIndex + baseIndex);
2408 in.writeInt64(timeUs);
2409 in.writeInt64(durationUs);
2410 in.writeInt32(buffer->size());
2411 in.writeInt32(buffer->size());
2412 in.write(buffer->data(), buffer->size());
2413
2414 notifyListener(MEDIA_SUBTITLE_DATA, 0, 0, &in);
2415}
Robert Shihd3b0bbb2014-07-23 15:00:25 -07002416
Robert Shih08528432015-04-08 09:06:54 -07002417void NuPlayer::sendTimedMetaData(const sp<ABuffer> &buffer) {
2418 int64_t timeUs;
2419 CHECK(buffer->meta()->findInt64("timeUs", &timeUs));
2420
2421 Parcel in;
2422 in.writeInt64(timeUs);
2423 in.writeInt32(buffer->size());
2424 in.writeInt32(buffer->size());
2425 in.write(buffer->data(), buffer->size());
2426
2427 notifyListener(MEDIA_META_DATA, 0, 0, &in);
2428}
2429
Robert Shihd3b0bbb2014-07-23 15:00:25 -07002430void NuPlayer::sendTimedTextData(const sp<ABuffer> &buffer) {
2431 const void *data;
2432 size_t size = 0;
2433 int64_t timeUs;
Marco Nelissen55e2f4c2015-09-04 15:57:15 -07002434 int32_t flag = TextDescriptions::IN_BAND_TEXT_3GPP;
Robert Shihd3b0bbb2014-07-23 15:00:25 -07002435
2436 AString mime;
2437 CHECK(buffer->meta()->findString("mime", &mime));
2438 CHECK(strcasecmp(mime.c_str(), MEDIA_MIMETYPE_TEXT_3GPP) == 0);
2439
2440 data = buffer->data();
2441 size = buffer->size();
2442
2443 Parcel parcel;
2444 if (size > 0) {
2445 CHECK(buffer->meta()->findInt64("timeUs", &timeUs));
Marco Nelissen55e2f4c2015-09-04 15:57:15 -07002446 int32_t global = 0;
2447 if (buffer->meta()->findInt32("global", &global) && global) {
2448 flag |= TextDescriptions::GLOBAL_DESCRIPTIONS;
2449 } else {
2450 flag |= TextDescriptions::LOCAL_DESCRIPTIONS;
2451 }
Robert Shihd3b0bbb2014-07-23 15:00:25 -07002452 TextDescriptions::getParcelOfDescriptions(
2453 (const uint8_t *)data, size, flag, timeUs / 1000, &parcel);
2454 }
2455
2456 if ((parcel.dataSize() > 0)) {
2457 notifyListener(MEDIA_TIMED_TEXT, 0, 0, &parcel);
2458 } else { // send an empty timed text
2459 notifyListener(MEDIA_TIMED_TEXT, 0, 0);
2460 }
2461}
Andreas Huberb5f25f02013-02-05 10:14:26 -08002462////////////////////////////////////////////////////////////////////////////////
2463
Chong Zhangced1c2f2014-08-08 15:22:35 -07002464sp<AMessage> NuPlayer::Source::getFormat(bool audio) {
2465 sp<MetaData> meta = getFormatMeta(audio);
2466
2467 if (meta == NULL) {
2468 return NULL;
2469 }
2470
2471 sp<AMessage> msg = new AMessage;
2472
2473 if(convertMetaDataToMessage(meta, &msg) == OK) {
2474 return msg;
2475 }
2476 return NULL;
2477}
2478
Andreas Huber9575c962013-02-05 13:59:56 -08002479void NuPlayer::Source::notifyFlagsChanged(uint32_t flags) {
2480 sp<AMessage> notify = dupNotify();
2481 notify->setInt32("what", kWhatFlagsChanged);
2482 notify->setInt32("flags", flags);
2483 notify->post();
2484}
2485
Chong Zhangced1c2f2014-08-08 15:22:35 -07002486void NuPlayer::Source::notifyVideoSizeChanged(const sp<AMessage> &format) {
Andreas Huber9575c962013-02-05 13:59:56 -08002487 sp<AMessage> notify = dupNotify();
2488 notify->setInt32("what", kWhatVideoSizeChanged);
Chong Zhangced1c2f2014-08-08 15:22:35 -07002489 notify->setMessage("format", format);
Andreas Huber9575c962013-02-05 13:59:56 -08002490 notify->post();
2491}
2492
Andreas Huberec0c5972013-02-05 14:47:13 -08002493void NuPlayer::Source::notifyPrepared(status_t err) {
Andreas Huber9575c962013-02-05 13:59:56 -08002494 sp<AMessage> notify = dupNotify();
2495 notify->setInt32("what", kWhatPrepared);
Andreas Huberec0c5972013-02-05 14:47:13 -08002496 notify->setInt32("err", err);
Andreas Huber9575c962013-02-05 13:59:56 -08002497 notify->post();
2498}
2499
Lajos Molnarfcd3e942015-03-31 10:06:48 -07002500void NuPlayer::Source::notifyInstantiateSecureDecoders(const sp<AMessage> &reply) {
2501 sp<AMessage> notify = dupNotify();
2502 notify->setInt32("what", kWhatInstantiateSecureDecoders);
2503 notify->setMessage("reply", reply);
2504 notify->post();
2505}
2506
Andreas Huber84333e02014-02-07 15:36:10 -08002507void NuPlayer::Source::onMessageReceived(const sp<AMessage> & /* msg */) {
Andreas Huberb5f25f02013-02-05 10:14:26 -08002508 TRESPASS();
2509}
2510
Andreas Huberf9334412010-12-15 15:17:42 -08002511} // namespace android