blob: d2405216da2e6acfa8e6a29ff0119f2992e09401 [file] [log] [blame]
Andreas Huberf9334412010-12-15 15:17:42 -08001/*
2 * Copyright (C) 2010 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17//#define LOG_NDEBUG 0
18#define LOG_TAG "NuPlayer"
19#include <utils/Log.h>
20
21#include "NuPlayer.h"
Andreas Huber5bc087c2010-12-23 10:27:40 -080022
23#include "HTTPLiveSource.h"
Chong Zhang7137ec72014-11-12 16:41:05 -080024#include "NuPlayerCCDecoder.h"
Andreas Huberf9334412010-12-15 15:17:42 -080025#include "NuPlayerDecoder.h"
Chong Zhang7137ec72014-11-12 16:41:05 -080026#include "NuPlayerDecoderBase.h"
Wei Jiabc2fb722014-07-08 16:37:57 -070027#include "NuPlayerDecoderPassThrough.h"
Andreas Huber43c3e6c2011-01-05 12:17:08 -080028#include "NuPlayerDriver.h"
Andreas Huberf9334412010-12-15 15:17:42 -080029#include "NuPlayerRenderer.h"
Andreas Huber5bc087c2010-12-23 10:27:40 -080030#include "NuPlayerSource.h"
Andreas Huber2bfdd422011-10-11 15:24:07 -070031#include "RTSPSource.h"
Andreas Huber5bc087c2010-12-23 10:27:40 -080032#include "StreamingSource.h"
Andreas Huberafed0e12011-09-20 15:39:58 -070033#include "GenericSource.h"
Robert Shihd3b0bbb2014-07-23 15:00:25 -070034#include "TextDescriptions.h"
Andreas Huber5bc087c2010-12-23 10:27:40 -080035
36#include "ATSParser.h"
Andreas Huberf9334412010-12-15 15:17:42 -080037
Lajos Molnard9fd6312014-11-06 11:00:00 -080038#include <cutils/properties.h>
39
Lajos Molnar3a474aa2015-04-24 17:10:07 -070040#include <media/AudioResamplerPublic.h>
41#include <media/AVSyncSettings.h>
Wonsik Kim7e34bf52016-08-23 00:09:18 +090042#include <media/MediaCodecBuffer.h>
Lajos Molnar3a474aa2015-04-24 17:10:07 -070043
Andreas Huber3831a062010-12-21 10:22:33 -080044#include <media/stagefright/foundation/hexdump.h>
Andreas Huberf9334412010-12-15 15:17:42 -080045#include <media/stagefright/foundation/ABuffer.h>
46#include <media/stagefright/foundation/ADebug.h>
47#include <media/stagefright/foundation/AMessage.h>
Lajos Molnar09524832014-07-17 14:29:51 -070048#include <media/stagefright/MediaBuffer.h>
Andreas Huber3fe62152011-09-16 15:09:22 -070049#include <media/stagefright/MediaDefs.h>
Andreas Huberf9334412010-12-15 15:17:42 -080050#include <media/stagefright/MediaErrors.h>
51#include <media/stagefright/MetaData.h>
Lajos Molnar1de1e252015-04-30 18:18:34 -070052
Andy McFadden8ba01022012-12-18 09:46:54 -080053#include <gui/IGraphicBufferProducer.h>
Lajos Molnar1de1e252015-04-30 18:18:34 -070054#include <gui/Surface.h>
Andreas Huberf9334412010-12-15 15:17:42 -080055
Andreas Huber3fe62152011-09-16 15:09:22 -070056#include "avc_utils.h"
57
Andreas Huber84066782011-08-16 09:34:26 -070058#include "ESDS.h"
59#include <media/stagefright/Utils.h>
60
Andreas Huberf9334412010-12-15 15:17:42 -080061namespace android {
62
Andreas Hubera1f8ab02012-11-30 10:53:22 -080063struct NuPlayer::Action : public RefBase {
64 Action() {}
65
66 virtual void execute(NuPlayer *player) = 0;
67
68private:
69 DISALLOW_EVIL_CONSTRUCTORS(Action);
70};
71
72struct NuPlayer::SeekAction : public Action {
Wei Jiac5de0912016-11-18 10:22:14 -080073 explicit SeekAction(int64_t seekTimeUs, MediaPlayerSeekMode mode)
Wei Jia14486822016-11-02 17:51:30 -070074 : mSeekTimeUs(seekTimeUs),
Wei Jiac5de0912016-11-18 10:22:14 -080075 mMode(mode) {
Andreas Hubera1f8ab02012-11-30 10:53:22 -080076 }
77
78 virtual void execute(NuPlayer *player) {
Wei Jiac5de0912016-11-18 10:22:14 -080079 player->performSeek(mSeekTimeUs, mMode);
Andreas Hubera1f8ab02012-11-30 10:53:22 -080080 }
81
82private:
83 int64_t mSeekTimeUs;
Wei Jiac5de0912016-11-18 10:22:14 -080084 MediaPlayerSeekMode mMode;
Andreas Hubera1f8ab02012-11-30 10:53:22 -080085
86 DISALLOW_EVIL_CONSTRUCTORS(SeekAction);
87};
88
Chong Zhangf8d71772014-11-26 15:08:34 -080089struct NuPlayer::ResumeDecoderAction : public Action {
Chih-Hung Hsieh090ef602016-04-27 10:39:54 -070090 explicit ResumeDecoderAction(bool needNotify)
Chong Zhangf8d71772014-11-26 15:08:34 -080091 : mNeedNotify(needNotify) {
92 }
93
94 virtual void execute(NuPlayer *player) {
95 player->performResumeDecoders(mNeedNotify);
96 }
97
98private:
99 bool mNeedNotify;
100
101 DISALLOW_EVIL_CONSTRUCTORS(ResumeDecoderAction);
102};
103
Andreas Huber57a339c2012-12-03 11:18:00 -0800104struct NuPlayer::SetSurfaceAction : public Action {
Chih-Hung Hsieh090ef602016-04-27 10:39:54 -0700105 explicit SetSurfaceAction(const sp<Surface> &surface)
Lajos Molnar1de1e252015-04-30 18:18:34 -0700106 : mSurface(surface) {
Andreas Huber57a339c2012-12-03 11:18:00 -0800107 }
108
109 virtual void execute(NuPlayer *player) {
Lajos Molnar1de1e252015-04-30 18:18:34 -0700110 player->performSetSurface(mSurface);
Andreas Huber57a339c2012-12-03 11:18:00 -0800111 }
112
113private:
Lajos Molnar1de1e252015-04-30 18:18:34 -0700114 sp<Surface> mSurface;
Andreas Huber57a339c2012-12-03 11:18:00 -0800115
116 DISALLOW_EVIL_CONSTRUCTORS(SetSurfaceAction);
117};
118
Wei Jiafef808d2014-10-31 17:57:05 -0700119struct NuPlayer::FlushDecoderAction : public Action {
120 FlushDecoderAction(FlushCommand audio, FlushCommand video)
Andreas Huber14f76722013-01-15 09:04:18 -0800121 : mAudio(audio),
122 mVideo(video) {
123 }
124
125 virtual void execute(NuPlayer *player) {
Wei Jiafef808d2014-10-31 17:57:05 -0700126 player->performDecoderFlush(mAudio, mVideo);
Andreas Huber14f76722013-01-15 09:04:18 -0800127 }
128
129private:
Wei Jiafef808d2014-10-31 17:57:05 -0700130 FlushCommand mAudio;
131 FlushCommand mVideo;
Andreas Huber14f76722013-01-15 09:04:18 -0800132
Wei Jiafef808d2014-10-31 17:57:05 -0700133 DISALLOW_EVIL_CONSTRUCTORS(FlushDecoderAction);
Andreas Huber14f76722013-01-15 09:04:18 -0800134};
135
136struct NuPlayer::PostMessageAction : public Action {
Chih-Hung Hsieh090ef602016-04-27 10:39:54 -0700137 explicit PostMessageAction(const sp<AMessage> &msg)
Andreas Huber14f76722013-01-15 09:04:18 -0800138 : mMessage(msg) {
139 }
140
141 virtual void execute(NuPlayer *) {
142 mMessage->post();
143 }
144
145private:
146 sp<AMessage> mMessage;
147
148 DISALLOW_EVIL_CONSTRUCTORS(PostMessageAction);
149};
150
Andreas Hubera1f8ab02012-11-30 10:53:22 -0800151// Use this if there's no state necessary to save in order to execute
152// the action.
153struct NuPlayer::SimpleAction : public Action {
154 typedef void (NuPlayer::*ActionFunc)();
155
Chih-Hung Hsieh090ef602016-04-27 10:39:54 -0700156 explicit SimpleAction(ActionFunc func)
Andreas Hubera1f8ab02012-11-30 10:53:22 -0800157 : mFunc(func) {
158 }
159
160 virtual void execute(NuPlayer *player) {
161 (player->*mFunc)();
162 }
163
164private:
165 ActionFunc mFunc;
166
167 DISALLOW_EVIL_CONSTRUCTORS(SimpleAction);
168};
169
Andreas Huberf9334412010-12-15 15:17:42 -0800170////////////////////////////////////////////////////////////////////////////////
171
Ronghua Wu68845c12015-07-21 09:50:48 -0700172NuPlayer::NuPlayer(pid_t pid)
Andreas Huber9b80c2b2011-06-30 15:47:02 -0700173 : mUIDValid(false),
Ronghua Wu68845c12015-07-21 09:50:48 -0700174 mPID(pid),
Andreas Huber9575c962013-02-05 13:59:56 -0800175 mSourceFlags(0),
Wei Jiabc2fb722014-07-08 16:37:57 -0700176 mOffloadAudio(false),
Wei Jia88703c32014-08-06 11:24:07 -0700177 mAudioDecoderGeneration(0),
178 mVideoDecoderGeneration(0),
Wei Jia57568df2014-09-22 10:16:29 -0700179 mRendererGeneration(0),
Robert Shih1a5c8592015-08-04 18:07:44 -0700180 mPreviousSeekTimeUs(0),
Andreas Huber9b80c2b2011-06-30 15:47:02 -0700181 mAudioEOS(false),
Andreas Huberf9334412010-12-15 15:17:42 -0800182 mVideoEOS(false),
Andreas Huber5bc087c2010-12-23 10:27:40 -0800183 mScanSourcesPending(false),
Andreas Huber1aef2112011-01-04 14:01:29 -0800184 mScanSourcesGeneration(0),
Andreas Huberb7c8e912012-11-27 15:02:53 -0800185 mPollDurationGeneration(0),
Robert Shihd3b0bbb2014-07-23 15:00:25 -0700186 mTimedTextGeneration(0),
Andreas Huberf9334412010-12-15 15:17:42 -0800187 mFlushingAudio(NONE),
Andreas Huber1aef2112011-01-04 14:01:29 -0800188 mFlushingVideo(NONE),
Chong Zhangf8d71772014-11-26 15:08:34 -0800189 mResumePending(false),
Andreas Huber57a339c2012-12-03 11:18:00 -0800190 mVideoScalingMode(NATIVE_WINDOW_SCALING_MODE_SCALE_TO_WINDOW),
Lajos Molnar3a474aa2015-04-24 17:10:07 -0700191 mPlaybackSettings(AUDIO_PLAYBACK_RATE_DEFAULT),
192 mVideoFpsHint(-1.f),
Chong Zhangefbb6192015-01-30 17:13:27 -0800193 mStarted(false),
Wei Jia8a092d32016-06-03 14:57:24 -0700194 mPrepared(false),
Ronghua Wu64c2d172015-10-07 16:52:19 -0700195 mResetting(false),
Robert Shih0c61a0d2015-07-06 15:09:10 -0700196 mSourceStarted(false),
Chong Zhangefbb6192015-01-30 17:13:27 -0800197 mPaused(false),
Wei Jia71c75e02016-02-04 09:40:47 -0800198 mPausedByClient(true),
Chong Zhang8a048332015-05-06 15:16:28 -0700199 mPausedForBuffering(false) {
Andy Hung8d121d42014-10-03 09:53:53 -0700200 clearFlushComplete();
Andreas Huberf9334412010-12-15 15:17:42 -0800201}
202
203NuPlayer::~NuPlayer() {
204}
205
Andreas Huber9b80c2b2011-06-30 15:47:02 -0700206void NuPlayer::setUID(uid_t uid) {
207 mUIDValid = true;
208 mUID = uid;
209}
210
Andreas Huber43c3e6c2011-01-05 12:17:08 -0800211void NuPlayer::setDriver(const wp<NuPlayerDriver> &driver) {
212 mDriver = driver;
Andreas Huberf9334412010-12-15 15:17:42 -0800213}
214
Andreas Huber9575c962013-02-05 13:59:56 -0800215void NuPlayer::setDataSourceAsync(const sp<IStreamSource> &source) {
Lajos Molnar1d15ab52015-03-04 16:46:34 -0800216 sp<AMessage> msg = new AMessage(kWhatSetDataSource, this);
Andreas Huberf9334412010-12-15 15:17:42 -0800217
Lajos Molnar1d15ab52015-03-04 16:46:34 -0800218 sp<AMessage> notify = new AMessage(kWhatSourceNotify, this);
Andreas Huberb5f25f02013-02-05 10:14:26 -0800219
Andreas Huber240abcc2014-02-13 13:32:37 -0800220 msg->setObject("source", new StreamingSource(notify, source));
Andreas Huber5bc087c2010-12-23 10:27:40 -0800221 msg->post();
222}
Andreas Huberf9334412010-12-15 15:17:42 -0800223
Andreas Huberafed0e12011-09-20 15:39:58 -0700224static bool IsHTTPLiveURL(const char *url) {
225 if (!strncasecmp("http://", url, 7)
Andreas Huber99759402013-04-01 14:28:31 -0700226 || !strncasecmp("https://", url, 8)
227 || !strncasecmp("file://", url, 7)) {
Andreas Huberafed0e12011-09-20 15:39:58 -0700228 size_t len = strlen(url);
229 if (len >= 5 && !strcasecmp(".m3u8", &url[len - 5])) {
230 return true;
231 }
232
233 if (strstr(url,"m3u8")) {
234 return true;
235 }
236 }
237
238 return false;
239}
240
Andreas Huber9575c962013-02-05 13:59:56 -0800241void NuPlayer::setDataSourceAsync(
Andreas Huber1b86fe02014-01-29 11:13:26 -0800242 const sp<IMediaHTTPService> &httpService,
243 const char *url,
244 const KeyedVector<String8, String8> *headers) {
Chong Zhang3de157d2014-08-05 20:54:44 -0700245
Lajos Molnar1d15ab52015-03-04 16:46:34 -0800246 sp<AMessage> msg = new AMessage(kWhatSetDataSource, this);
Oscar Rydhé7a33b772012-02-20 10:15:48 +0100247 size_t len = strlen(url);
Andreas Huber5bc087c2010-12-23 10:27:40 -0800248
Lajos Molnar1d15ab52015-03-04 16:46:34 -0800249 sp<AMessage> notify = new AMessage(kWhatSourceNotify, this);
Andreas Huberb5f25f02013-02-05 10:14:26 -0800250
Andreas Huberafed0e12011-09-20 15:39:58 -0700251 sp<Source> source;
252 if (IsHTTPLiveURL(url)) {
Andreas Huber81e68442014-02-05 11:52:33 -0800253 source = new HTTPLiveSource(notify, httpService, url, headers);
Andreas Huberafed0e12011-09-20 15:39:58 -0700254 } else if (!strncasecmp(url, "rtsp://", 7)) {
Andreas Huber1b86fe02014-01-29 11:13:26 -0800255 source = new RTSPSource(
256 notify, httpService, url, headers, mUIDValid, mUID);
Oscar Rydhé7a33b772012-02-20 10:15:48 +0100257 } else if ((!strncasecmp(url, "http://", 7)
258 || !strncasecmp(url, "https://", 8))
259 && ((len >= 4 && !strcasecmp(".sdp", &url[len - 4]))
260 || strstr(url, ".sdp?"))) {
Andreas Huber1b86fe02014-01-29 11:13:26 -0800261 source = new RTSPSource(
262 notify, httpService, url, headers, mUIDValid, mUID, true);
Andreas Huber2bfdd422011-10-11 15:24:07 -0700263 } else {
Chong Zhang3de157d2014-08-05 20:54:44 -0700264 sp<GenericSource> genericSource =
265 new GenericSource(notify, mUIDValid, mUID);
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 Jiac5de0912016-11-18 10:22:14 -0800425void NuPlayer::seekToAsync(int64_t seekTimeUs, MediaPlayerSeekMode mode, 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 Jiac5de0912016-11-18 10:22:14 -0800428 msg->setInt32("mode", mode);
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 Jiac5de0912016-11-18 10:22:14 -0800687 new SeekAction(currentPositionUs,
688 MediaPlayerSeekMode::SEEK_PREVIOUS_SYNC /* mode */));
Ronghua Wua73d9e02014-10-08 15:13:29 -0700689 }
Andy Hung73535852014-09-05 11:42:58 -0700690 }
Wei Jiaac428aa2014-09-02 19:01:34 -0700691
Andreas Huber57a339c2012-12-03 11:18:00 -0800692 // If there is a new surface texture, instantiate decoders
693 // again if possible.
694 mDeferredActions.push_back(
695 new SimpleAction(&NuPlayer::performScanSources));
696 }
697
Chong Zhangf8d71772014-11-26 15:08:34 -0800698 // After a flush without shutdown, decoder is paused.
699 // Don't resume it until source seek is done, otherwise it could
Chong Zhang7137ec72014-11-12 16:41:05 -0800700 // start pulling stale data too soon.
701 mDeferredActions.push_back(
Chong Zhangf8d71772014-11-26 15:08:34 -0800702 new ResumeDecoderAction(false /* needNotify */));
Chong Zhang7137ec72014-11-12 16:41:05 -0800703
Andreas Huber57a339c2012-12-03 11:18:00 -0800704 processDeferredActions();
Andreas Huberf9334412010-12-15 15:17:42 -0800705 break;
706 }
707
708 case kWhatSetAudioSink:
709 {
Steve Block3856b092011-10-20 11:56:00 +0100710 ALOGV("kWhatSetAudioSink");
Andreas Huberf9334412010-12-15 15:17:42 -0800711
712 sp<RefBase> obj;
713 CHECK(msg->findObject("sink", &obj));
714
715 mAudioSink = static_cast<MediaPlayerBase::AudioSink *>(obj.get());
716 break;
717 }
718
719 case kWhatStart:
720 {
Steve Block3856b092011-10-20 11:56:00 +0100721 ALOGV("kWhatStart");
Wei Jia94211742014-10-28 17:09:06 -0700722 if (mStarted) {
Chong Zhang8a048332015-05-06 15:16:28 -0700723 // do not resume yet if the source is still buffering
724 if (!mPausedForBuffering) {
725 onResume();
726 }
Wei Jia94211742014-10-28 17:09:06 -0700727 } else {
728 onStart();
Lajos Molnar09524832014-07-17 14:29:51 -0700729 }
Chong Zhangefbb6192015-01-30 17:13:27 -0800730 mPausedByClient = false;
Andreas Huberf9334412010-12-15 15:17:42 -0800731 break;
732 }
733
Lajos Molnar3a474aa2015-04-24 17:10:07 -0700734 case kWhatConfigPlayback:
Wei Jia98160162015-02-04 17:01:11 -0800735 {
Lajos Molnar3a474aa2015-04-24 17:10:07 -0700736 sp<AReplyToken> replyID;
737 CHECK(msg->senderAwaitsResponse(&replyID));
738 AudioPlaybackRate rate /* sanitized */;
739 readFromAMessage(msg, &rate);
740 status_t err = OK;
Wei Jia98160162015-02-04 17:01:11 -0800741 if (mRenderer != NULL) {
Wei Jiabf70feb2016-02-19 15:47:16 -0800742 // AudioSink allows only 1.f and 0.f for offload mode.
743 // For other speed, switch to non-offload mode.
744 if (mOffloadAudio && ((rate.mSpeed != 0.f && rate.mSpeed != 1.f)
745 || rate.mPitch != 1.f)) {
746 int64_t currentPositionUs;
747 if (getCurrentPosition(&currentPositionUs) != OK) {
748 currentPositionUs = mPreviousSeekTimeUs;
749 }
750
751 // Set mPlaybackSettings so that the new audio decoder can
752 // be created correctly.
753 mPlaybackSettings = rate;
754 if (!mPaused) {
755 mRenderer->pause();
756 }
Wei Jia5031b2f2016-02-25 11:19:31 -0800757 restartAudio(
Wei Jiabf70feb2016-02-19 15:47:16 -0800758 currentPositionUs, true /* forceNonOffload */,
759 true /* needsToCreateAudioDecoder */);
760 if (!mPaused) {
761 mRenderer->resume();
762 }
763 }
764
Lajos Molnar3a474aa2015-04-24 17:10:07 -0700765 err = mRenderer->setPlaybackSettings(rate);
766 }
767 if (err == OK) {
768 if (rate.mSpeed == 0.f) {
769 onPause();
Wei Jia351ce872016-02-10 13:21:59 -0800770 mPausedByClient = true;
Lajos Molnar3a474aa2015-04-24 17:10:07 -0700771 // save all other settings (using non-paused speed)
772 // so we can restore them on start
773 AudioPlaybackRate newRate = rate;
774 newRate.mSpeed = mPlaybackSettings.mSpeed;
775 mPlaybackSettings = newRate;
776 } else { /* rate.mSpeed != 0.f */
Lajos Molnar3a474aa2015-04-24 17:10:07 -0700777 mPlaybackSettings = rate;
Wei Jia8a092d32016-06-03 14:57:24 -0700778 if (mStarted) {
779 // do not resume yet if the source is still buffering
780 if (!mPausedForBuffering) {
781 onResume();
782 }
783 } else if (mPrepared) {
784 onStart();
785 }
786
787 mPausedByClient = false;
Lajos Molnar3a474aa2015-04-24 17:10:07 -0700788 }
Wei Jia98160162015-02-04 17:01:11 -0800789 }
Ronghua Wu8db88132015-04-22 13:51:35 -0700790
791 if (mVideoDecoder != NULL) {
Praveen Chavanbbaa1442016-04-08 13:33:49 -0700792 sp<AMessage> params = new AMessage();
793 params->setFloat("playback-speed", mPlaybackSettings.mSpeed);
794 mVideoDecoder->setParameters(params);
Ronghua Wu8db88132015-04-22 13:51:35 -0700795 }
796
Lajos Molnar3a474aa2015-04-24 17:10:07 -0700797 sp<AMessage> response = new AMessage;
798 response->setInt32("err", err);
799 response->postReply(replyID);
800 break;
801 }
802
803 case kWhatGetPlaybackSettings:
804 {
805 sp<AReplyToken> replyID;
806 CHECK(msg->senderAwaitsResponse(&replyID));
807 AudioPlaybackRate rate = mPlaybackSettings;
808 status_t err = OK;
809 if (mRenderer != NULL) {
810 err = mRenderer->getPlaybackSettings(&rate);
811 }
812 if (err == OK) {
813 // get playback settings used by renderer, as it may be
814 // slightly off due to audiosink not taking small changes.
815 mPlaybackSettings = rate;
816 if (mPaused) {
817 rate.mSpeed = 0.f;
818 }
819 }
820 sp<AMessage> response = new AMessage;
821 if (err == OK) {
822 writeToAMessage(response, rate);
823 }
824 response->setInt32("err", err);
825 response->postReply(replyID);
826 break;
827 }
828
829 case kWhatConfigSync:
830 {
831 sp<AReplyToken> replyID;
832 CHECK(msg->senderAwaitsResponse(&replyID));
833
834 ALOGV("kWhatConfigSync");
835 AVSyncSettings sync;
836 float videoFpsHint;
837 readFromAMessage(msg, &sync, &videoFpsHint);
838 status_t err = OK;
839 if (mRenderer != NULL) {
840 err = mRenderer->setSyncSettings(sync, videoFpsHint);
841 }
842 if (err == OK) {
843 mSyncSettings = sync;
844 mVideoFpsHint = videoFpsHint;
845 }
846 sp<AMessage> response = new AMessage;
847 response->setInt32("err", err);
848 response->postReply(replyID);
849 break;
850 }
851
852 case kWhatGetSyncSettings:
853 {
854 sp<AReplyToken> replyID;
855 CHECK(msg->senderAwaitsResponse(&replyID));
856 AVSyncSettings sync = mSyncSettings;
857 float videoFps = mVideoFpsHint;
858 status_t err = OK;
859 if (mRenderer != NULL) {
860 err = mRenderer->getSyncSettings(&sync, &videoFps);
861 if (err == OK) {
862 mSyncSettings = sync;
863 mVideoFpsHint = videoFps;
864 }
865 }
866 sp<AMessage> response = new AMessage;
867 if (err == OK) {
868 writeToAMessage(response, sync, videoFps);
869 }
870 response->setInt32("err", err);
871 response->postReply(replyID);
Wei Jia98160162015-02-04 17:01:11 -0800872 break;
873 }
874
Andreas Huberf9334412010-12-15 15:17:42 -0800875 case kWhatScanSources:
876 {
Andreas Huber1aef2112011-01-04 14:01:29 -0800877 int32_t generation;
878 CHECK(msg->findInt32("generation", &generation));
879 if (generation != mScanSourcesGeneration) {
880 // Drop obsolete msg.
881 break;
882 }
883
Andreas Huber5bc087c2010-12-23 10:27:40 -0800884 mScanSourcesPending = false;
885
Steve Block3856b092011-10-20 11:56:00 +0100886 ALOGV("scanning sources haveAudio=%d, haveVideo=%d",
Andreas Huber43c3e6c2011-01-05 12:17:08 -0800887 mAudioDecoder != NULL, mVideoDecoder != NULL);
888
Andreas Huberb7c8e912012-11-27 15:02:53 -0800889 bool mHadAnySourcesBefore =
890 (mAudioDecoder != NULL) || (mVideoDecoder != NULL);
Robert Shih7350b052015-10-01 15:50:14 -0700891 bool rescan = false;
Andreas Huberb7c8e912012-11-27 15:02:53 -0800892
Andy Hung282a7e32014-08-14 15:56:34 -0700893 // initialize video before audio because successful initialization of
894 // video may change deep buffer mode of audio.
Lajos Molnar1de1e252015-04-30 18:18:34 -0700895 if (mSurface != NULL) {
Robert Shih7350b052015-10-01 15:50:14 -0700896 if (instantiateDecoder(false, &mVideoDecoder) == -EWOULDBLOCK) {
897 rescan = true;
898 }
Haynes Mathew George5d246ef2012-07-09 10:36:57 -0700899 }
Andreas Huberf9334412010-12-15 15:17:42 -0800900
Ronghua Wua10fd232014-11-06 16:15:20 -0800901 // Don't try to re-open audio sink if there's an existing decoder.
902 if (mAudioSink != NULL && mAudioDecoder == NULL) {
Robert Shih7350b052015-10-01 15:50:14 -0700903 if (instantiateDecoder(true, &mAudioDecoder) == -EWOULDBLOCK) {
904 rescan = true;
905 }
Andreas Huberf9334412010-12-15 15:17:42 -0800906 }
907
Andreas Huberb7c8e912012-11-27 15:02:53 -0800908 if (!mHadAnySourcesBefore
909 && (mAudioDecoder != NULL || mVideoDecoder != NULL)) {
910 // This is the first time we've found anything playable.
911
Andreas Huber9575c962013-02-05 13:59:56 -0800912 if (mSourceFlags & Source::FLAG_DYNAMIC_DURATION) {
Andreas Huberb7c8e912012-11-27 15:02:53 -0800913 schedulePollDuration();
914 }
915 }
916
Andreas Hubereac68ba2011-09-27 12:12:25 -0700917 status_t err;
918 if ((err = mSource->feedMoreTSData()) != OK) {
Andreas Huber1aef2112011-01-04 14:01:29 -0800919 if (mAudioDecoder == NULL && mVideoDecoder == NULL) {
920 // We're not currently decoding anything (no audio or
921 // video tracks found) and we just ran out of input data.
Andreas Hubereac68ba2011-09-27 12:12:25 -0700922
923 if (err == ERROR_END_OF_STREAM) {
924 notifyListener(MEDIA_PLAYBACK_COMPLETE, 0, 0);
925 } else {
926 notifyListener(MEDIA_ERROR, MEDIA_ERROR_UNKNOWN, err);
927 }
Andreas Huber1aef2112011-01-04 14:01:29 -0800928 }
Andreas Huberf9334412010-12-15 15:17:42 -0800929 break;
930 }
931
Robert Shih7350b052015-10-01 15:50:14 -0700932 if (rescan) {
Andreas Huberf9334412010-12-15 15:17:42 -0800933 msg->post(100000ll);
Andreas Huber5bc087c2010-12-23 10:27:40 -0800934 mScanSourcesPending = true;
Andreas Huberf9334412010-12-15 15:17:42 -0800935 }
936 break;
937 }
938
939 case kWhatVideoNotify:
940 case kWhatAudioNotify:
941 {
942 bool audio = msg->what() == kWhatAudioNotify;
943
Wei Jia88703c32014-08-06 11:24:07 -0700944 int32_t currentDecoderGeneration =
945 (audio? mAudioDecoderGeneration : mVideoDecoderGeneration);
946 int32_t requesterGeneration = currentDecoderGeneration - 1;
947 CHECK(msg->findInt32("generation", &requesterGeneration));
948
949 if (requesterGeneration != currentDecoderGeneration) {
950 ALOGV("got message from old %s decoder, generation(%d:%d)",
951 audio ? "audio" : "video", requesterGeneration,
952 currentDecoderGeneration);
953 sp<AMessage> reply;
954 if (!(msg->findMessage("reply", &reply))) {
955 return;
956 }
957
958 reply->setInt32("err", INFO_DISCONTINUITY);
959 reply->post();
960 return;
961 }
962
Andreas Huberf9334412010-12-15 15:17:42 -0800963 int32_t what;
Lajos Molnar1cd13982014-01-17 15:12:51 -0800964 CHECK(msg->findInt32("what", &what));
Andreas Huberf9334412010-12-15 15:17:42 -0800965
Chong Zhang7137ec72014-11-12 16:41:05 -0800966 if (what == DecoderBase::kWhatInputDiscontinuity) {
967 int32_t formatChange;
968 CHECK(msg->findInt32("formatChange", &formatChange));
Andreas Huberf9334412010-12-15 15:17:42 -0800969
Chong Zhang7137ec72014-11-12 16:41:05 -0800970 ALOGV("%s discontinuity: formatChange %d",
971 audio ? "audio" : "video", formatChange);
972
973 if (formatChange) {
974 mDeferredActions.push_back(
975 new FlushDecoderAction(
976 audio ? FLUSH_CMD_SHUTDOWN : FLUSH_CMD_NONE,
977 audio ? FLUSH_CMD_NONE : FLUSH_CMD_SHUTDOWN));
Andreas Huberf9334412010-12-15 15:17:42 -0800978 }
Chong Zhang7137ec72014-11-12 16:41:05 -0800979
980 mDeferredActions.push_back(
981 new SimpleAction(
982 &NuPlayer::performScanSources));
983
984 processDeferredActions();
985 } else if (what == DecoderBase::kWhatEOS) {
Andreas Huberdc9bacd2011-09-26 10:53:29 -0700986 int32_t err;
Lajos Molnar1cd13982014-01-17 15:12:51 -0800987 CHECK(msg->findInt32("err", &err));
Andreas Huberdc9bacd2011-09-26 10:53:29 -0700988
989 if (err == ERROR_END_OF_STREAM) {
Steve Block3856b092011-10-20 11:56:00 +0100990 ALOGV("got %s decoder EOS", audio ? "audio" : "video");
Andreas Huberdc9bacd2011-09-26 10:53:29 -0700991 } else {
Steve Block3856b092011-10-20 11:56:00 +0100992 ALOGV("got %s decoder EOS w/ error %d",
Andreas Huberdc9bacd2011-09-26 10:53:29 -0700993 audio ? "audio" : "video",
994 err);
995 }
996
997 mRenderer->queueEOS(audio, err);
Chong Zhang7137ec72014-11-12 16:41:05 -0800998 } else if (what == DecoderBase::kWhatFlushCompleted) {
Steve Block3856b092011-10-20 11:56:00 +0100999 ALOGV("decoder %s flush completed", audio ? "audio" : "video");
Andreas Huberf9334412010-12-15 15:17:42 -08001000
Andy Hung8d121d42014-10-03 09:53:53 -07001001 handleFlushComplete(audio, true /* isDecoder */);
Andreas Huber3831a062010-12-21 10:22:33 -08001002 finishFlushIfPossible();
Chong Zhang7137ec72014-11-12 16:41:05 -08001003 } else if (what == DecoderBase::kWhatVideoSizeChanged) {
Lajos Molnar1cd13982014-01-17 15:12:51 -08001004 sp<AMessage> format;
1005 CHECK(msg->findMessage("format", &format));
1006
Wei Jiac6cfd702014-11-11 16:33:20 -08001007 sp<AMessage> inputFormat =
1008 mSource->getFormat(false /* audio */);
Andreas Huber3831a062010-12-21 10:22:33 -08001009
Bartosz Dolewski26936f72014-12-11 12:47:08 +01001010 setVideoScalingMode(mVideoScalingMode);
Wei Jiac6cfd702014-11-11 16:33:20 -08001011 updateVideoSize(inputFormat, format);
Chong Zhang7137ec72014-11-12 16:41:05 -08001012 } else if (what == DecoderBase::kWhatShutdownCompleted) {
Steve Block3856b092011-10-20 11:56:00 +01001013 ALOGV("%s shutdown completed", audio ? "audio" : "video");
Andreas Huber3831a062010-12-21 10:22:33 -08001014 if (audio) {
1015 mAudioDecoder.clear();
Wei Jia57568df2014-09-22 10:16:29 -07001016 ++mAudioDecoderGeneration;
Andreas Huber3831a062010-12-21 10:22:33 -08001017
1018 CHECK_EQ((int)mFlushingAudio, (int)SHUTTING_DOWN_DECODER);
1019 mFlushingAudio = SHUT_DOWN;
1020 } else {
1021 mVideoDecoder.clear();
Wei Jia57568df2014-09-22 10:16:29 -07001022 ++mVideoDecoderGeneration;
Andreas Huber3831a062010-12-21 10:22:33 -08001023
1024 CHECK_EQ((int)mFlushingVideo, (int)SHUTTING_DOWN_DECODER);
1025 mFlushingVideo = SHUT_DOWN;
1026 }
1027
1028 finishFlushIfPossible();
Chong Zhangf8d71772014-11-26 15:08:34 -08001029 } else if (what == DecoderBase::kWhatResumeCompleted) {
1030 finishResume();
Chong Zhang7137ec72014-11-12 16:41:05 -08001031 } else if (what == DecoderBase::kWhatError) {
Chong Zhangf4c0a942014-08-11 15:14:10 -07001032 status_t err;
Andy Hung2abde2c2014-09-30 14:40:32 -07001033 if (!msg->findInt32("err", &err) || err == OK) {
Chong Zhangf4c0a942014-08-11 15:14:10 -07001034 err = UNKNOWN_ERROR;
1035 }
Andy Hungcf31f1e2014-09-23 14:59:01 -07001036
Andy Hung2abde2c2014-09-30 14:40:32 -07001037 // Decoder errors can be due to Source (e.g. from streaming),
1038 // or from decoding corrupted bitstreams, or from other decoder
1039 // MediaCodec operations (e.g. from an ongoing reset or seek).
Andy Hung202bce12014-12-03 11:47:36 -08001040 // They may also be due to openAudioSink failure at
1041 // decoder start or after a format change.
Andy Hung2abde2c2014-09-30 14:40:32 -07001042 //
1043 // We try to gracefully shut down the affected decoder if possible,
1044 // rather than trying to force the shutdown with something
1045 // similar to performReset(). This method can lead to a hang
1046 // if MediaCodec functions block after an error, but they should
1047 // typically return INVALID_OPERATION instead of blocking.
1048
1049 FlushStatus *flushing = audio ? &mFlushingAudio : &mFlushingVideo;
1050 ALOGE("received error(%#x) from %s decoder, flushing(%d), now shutting down",
1051 err, audio ? "audio" : "video", *flushing);
1052
1053 switch (*flushing) {
1054 case NONE:
1055 mDeferredActions.push_back(
Wei Jiafef808d2014-10-31 17:57:05 -07001056 new FlushDecoderAction(
1057 audio ? FLUSH_CMD_SHUTDOWN : FLUSH_CMD_NONE,
1058 audio ? FLUSH_CMD_NONE : FLUSH_CMD_SHUTDOWN));
Andy Hung2abde2c2014-09-30 14:40:32 -07001059 processDeferredActions();
1060 break;
1061 case FLUSHING_DECODER:
1062 *flushing = FLUSHING_DECODER_SHUTDOWN; // initiate shutdown after flush.
1063 break; // Wait for flush to complete.
1064 case FLUSHING_DECODER_SHUTDOWN:
1065 break; // Wait for flush to complete.
1066 case SHUTTING_DOWN_DECODER:
1067 break; // Wait for shutdown to complete.
1068 case FLUSHED:
1069 // Widevine source reads must stop before releasing the video decoder.
1070 if (!audio && mSource != NULL && mSourceFlags & Source::FLAG_SECURE) {
1071 mSource->stop();
Robert Shih0c61a0d2015-07-06 15:09:10 -07001072 mSourceStarted = false;
Andy Hung2abde2c2014-09-30 14:40:32 -07001073 }
1074 getDecoder(audio)->initiateShutdown(); // In the middle of a seek.
1075 *flushing = SHUTTING_DOWN_DECODER; // Shut down.
1076 break;
1077 case SHUT_DOWN:
1078 finishFlushIfPossible(); // Should not occur.
1079 break; // Finish anyways.
Marco Nelissen9e2b7912014-08-18 16:13:03 -07001080 }
Andy Hung2abde2c2014-09-30 14:40:32 -07001081 notifyListener(MEDIA_ERROR, MEDIA_ERROR_UNKNOWN, err);
Lajos Molnar1cd13982014-01-17 15:12:51 -08001082 } else {
1083 ALOGV("Unhandled decoder notification %d '%c%c%c%c'.",
Andreas Hubera1f8ab02012-11-30 10:53:22 -08001084 what,
1085 what >> 24,
1086 (what >> 16) & 0xff,
1087 (what >> 8) & 0xff,
1088 what & 0xff);
Andreas Huberf9334412010-12-15 15:17:42 -08001089 }
1090
1091 break;
1092 }
1093
1094 case kWhatRendererNotify:
1095 {
Wei Jia57568df2014-09-22 10:16:29 -07001096 int32_t requesterGeneration = mRendererGeneration - 1;
1097 CHECK(msg->findInt32("generation", &requesterGeneration));
1098 if (requesterGeneration != mRendererGeneration) {
1099 ALOGV("got message from old renderer, generation(%d:%d)",
1100 requesterGeneration, mRendererGeneration);
1101 return;
1102 }
1103
Andreas Huberf9334412010-12-15 15:17:42 -08001104 int32_t what;
1105 CHECK(msg->findInt32("what", &what));
1106
1107 if (what == Renderer::kWhatEOS) {
1108 int32_t audio;
1109 CHECK(msg->findInt32("audio", &audio));
1110
Andreas Huberc92fd242011-08-16 13:48:44 -07001111 int32_t finalResult;
1112 CHECK(msg->findInt32("finalResult", &finalResult));
1113
Andreas Huberf9334412010-12-15 15:17:42 -08001114 if (audio) {
1115 mAudioEOS = true;
1116 } else {
1117 mVideoEOS = true;
1118 }
1119
Andreas Huberc92fd242011-08-16 13:48:44 -07001120 if (finalResult == ERROR_END_OF_STREAM) {
Steve Block3856b092011-10-20 11:56:00 +01001121 ALOGV("reached %s EOS", audio ? "audio" : "video");
Andreas Huberc92fd242011-08-16 13:48:44 -07001122 } else {
Steve Block29357bc2012-01-06 19:20:56 +00001123 ALOGE("%s track encountered an error (%d)",
Andreas Huberc92fd242011-08-16 13:48:44 -07001124 audio ? "audio" : "video", finalResult);
1125
1126 notifyListener(
1127 MEDIA_ERROR, MEDIA_ERROR_UNKNOWN, finalResult);
1128 }
Andreas Huberf9334412010-12-15 15:17:42 -08001129
1130 if ((mAudioEOS || mAudioDecoder == NULL)
1131 && (mVideoEOS || mVideoDecoder == NULL)) {
1132 notifyListener(MEDIA_PLAYBACK_COMPLETE, 0, 0);
1133 }
Andreas Huber3fe62152011-09-16 15:09:22 -07001134 } else if (what == Renderer::kWhatFlushComplete) {
Andreas Huberf9334412010-12-15 15:17:42 -08001135 int32_t audio;
1136 CHECK(msg->findInt32("audio", &audio));
1137
Wei Jia3261f0d2015-10-08 13:58:33 -07001138 if (audio) {
1139 mAudioEOS = false;
1140 } else {
1141 mVideoEOS = false;
1142 }
1143
Steve Block3856b092011-10-20 11:56:00 +01001144 ALOGV("renderer %s flush completed.", audio ? "audio" : "video");
Wei Jiada4252f2015-07-14 18:15:28 -07001145 if (audio && (mFlushingAudio == NONE || mFlushingAudio == FLUSHED
1146 || mFlushingAudio == SHUT_DOWN)) {
1147 // Flush has been handled by tear down.
1148 break;
1149 }
Andy Hung8d121d42014-10-03 09:53:53 -07001150 handleFlushComplete(audio, false /* isDecoder */);
1151 finishFlushIfPossible();
James Dongf57b4ea2012-07-20 13:38:36 -07001152 } else if (what == Renderer::kWhatVideoRenderingStart) {
1153 notifyListener(MEDIA_INFO, MEDIA_INFO_RENDERING_START, 0);
Lajos Molnarcbaffcf2013-08-14 18:30:38 -07001154 } else if (what == Renderer::kWhatMediaRenderingStart) {
1155 ALOGV("media rendering started");
1156 notifyListener(MEDIA_STARTED, 0, 0);
Ronghua Wufaeb0f22015-05-21 12:20:21 -07001157 } else if (what == Renderer::kWhatAudioTearDown) {
Ronghua Wu08529172014-10-02 16:55:52 -07001158 int32_t reason;
1159 CHECK(msg->findInt32("reason", &reason));
Ronghua Wufaeb0f22015-05-21 12:20:21 -07001160 ALOGV("Tear down audio with reason %d.", reason);
Wei Jia8456ddd2016-04-22 14:46:43 -07001161 if (reason == Renderer::kDueToTimeout && !(mPaused && mOffloadAudio)) {
1162 // TimeoutWhenPaused is only for offload mode.
1163 ALOGW("Receive a stale message for teardown.");
1164 break;
1165 }
Wei Jiada4252f2015-07-14 18:15:28 -07001166 int64_t positionUs;
Robert Shih1a5c8592015-08-04 18:07:44 -07001167 if (!msg->findInt64("positionUs", &positionUs)) {
1168 positionUs = mPreviousSeekTimeUs;
1169 }
Ronghua Wufaeb0f22015-05-21 12:20:21 -07001170
Wei Jia5031b2f2016-02-25 11:19:31 -08001171 restartAudio(
Wei Jiaa05f1e32016-03-25 16:31:22 -07001172 positionUs, reason == Renderer::kForceNonOffload /* forceNonOffload */,
1173 reason != Renderer::kDueToTimeout /* needsToCreateAudioDecoder */);
Andreas Huberf9334412010-12-15 15:17:42 -08001174 }
1175 break;
1176 }
1177
1178 case kWhatMoreDataQueued:
1179 {
1180 break;
1181 }
1182
Andreas Huber1aef2112011-01-04 14:01:29 -08001183 case kWhatReset:
1184 {
Steve Block3856b092011-10-20 11:56:00 +01001185 ALOGV("kWhatReset");
Andreas Huber1aef2112011-01-04 14:01:29 -08001186
Ronghua Wu64c2d172015-10-07 16:52:19 -07001187 mResetting = true;
1188
Andreas Hubera1f8ab02012-11-30 10:53:22 -08001189 mDeferredActions.push_back(
Wei Jiafef808d2014-10-31 17:57:05 -07001190 new FlushDecoderAction(
1191 FLUSH_CMD_SHUTDOWN /* audio */,
1192 FLUSH_CMD_SHUTDOWN /* video */));
Andreas Huberb7c8e912012-11-27 15:02:53 -08001193
Andreas Hubera1f8ab02012-11-30 10:53:22 -08001194 mDeferredActions.push_back(
1195 new SimpleAction(&NuPlayer::performReset));
Andreas Huberb58ce9f2011-11-28 16:27:35 -08001196
Andreas Hubera1f8ab02012-11-30 10:53:22 -08001197 processDeferredActions();
Andreas Huber1aef2112011-01-04 14:01:29 -08001198 break;
1199 }
1200
Andreas Huber43c3e6c2011-01-05 12:17:08 -08001201 case kWhatSeek:
1202 {
1203 int64_t seekTimeUs;
Wei Jiac5de0912016-11-18 10:22:14 -08001204 int32_t mode;
Wei Jiae427abf2014-09-22 15:21:11 -07001205 int32_t needNotify;
Andreas Huber43c3e6c2011-01-05 12:17:08 -08001206 CHECK(msg->findInt64("seekTimeUs", &seekTimeUs));
Wei Jiac5de0912016-11-18 10:22:14 -08001207 CHECK(msg->findInt32("mode", &mode));
Wei Jiae427abf2014-09-22 15:21:11 -07001208 CHECK(msg->findInt32("needNotify", &needNotify));
Andreas Huber43c3e6c2011-01-05 12:17:08 -08001209
Wei Jiac5de0912016-11-18 10:22:14 -08001210 ALOGV("kWhatSeek seekTimeUs=%lld us, mode=%d, needNotify=%d",
1211 (long long)seekTimeUs, mode, needNotify);
Andreas Huber43c3e6c2011-01-05 12:17:08 -08001212
Wei Jia1061c9c2015-05-19 16:02:17 -07001213 if (!mStarted) {
1214 // Seek before the player is started. In order to preview video,
1215 // need to start the player and pause it. This branch is called
1216 // only once if needed. After the player is started, any seek
1217 // operation will go through normal path.
Robert Shih0c61a0d2015-07-06 15:09:10 -07001218 // Audio-only cases are handled separately.
Wei Jiac5de0912016-11-18 10:22:14 -08001219 onStart(seekTimeUs, (MediaPlayerSeekMode)mode);
Robert Shih0c61a0d2015-07-06 15:09:10 -07001220 if (mStarted) {
1221 onPause();
1222 mPausedByClient = true;
1223 }
Wei Jia1061c9c2015-05-19 16:02:17 -07001224 if (needNotify) {
1225 notifyDriverSeekComplete();
1226 }
1227 break;
1228 }
1229
Andreas Hubera1f8ab02012-11-30 10:53:22 -08001230 mDeferredActions.push_back(
Wei Jiafef808d2014-10-31 17:57:05 -07001231 new FlushDecoderAction(FLUSH_CMD_FLUSH /* audio */,
1232 FLUSH_CMD_FLUSH /* video */));
Andreas Huber43c3e6c2011-01-05 12:17:08 -08001233
Wei Jiae427abf2014-09-22 15:21:11 -07001234 mDeferredActions.push_back(
Wei Jiac5de0912016-11-18 10:22:14 -08001235 new SeekAction(seekTimeUs, (MediaPlayerSeekMode)mode));
Andreas Huber43c3e6c2011-01-05 12:17:08 -08001236
Chong Zhangf8d71772014-11-26 15:08:34 -08001237 // After a flush without shutdown, decoder is paused.
1238 // Don't resume it until source seek is done, otherwise it could
Chong Zhang7137ec72014-11-12 16:41:05 -08001239 // start pulling stale data too soon.
1240 mDeferredActions.push_back(
Chong Zhangf8d71772014-11-26 15:08:34 -08001241 new ResumeDecoderAction(needNotify));
Chong Zhang7137ec72014-11-12 16:41:05 -08001242
Andreas Hubera1f8ab02012-11-30 10:53:22 -08001243 processDeferredActions();
Andreas Huber43c3e6c2011-01-05 12:17:08 -08001244 break;
1245 }
1246
Andreas Huberb4082222011-01-20 15:23:04 -08001247 case kWhatPause:
1248 {
Chong Zhangefbb6192015-01-30 17:13:27 -08001249 onPause();
1250 mPausedByClient = true;
Andreas Huberb4082222011-01-20 15:23:04 -08001251 break;
1252 }
1253
Andreas Huberb5f25f02013-02-05 10:14:26 -08001254 case kWhatSourceNotify:
1255 {
Andreas Huber9575c962013-02-05 13:59:56 -08001256 onSourceNotify(msg);
Andreas Huberb5f25f02013-02-05 10:14:26 -08001257 break;
1258 }
1259
Chong Zhanga7fa1d92014-06-11 14:49:23 -07001260 case kWhatClosedCaptionNotify:
1261 {
1262 onClosedCaptionNotify(msg);
1263 break;
1264 }
1265
Andreas Huberf9334412010-12-15 15:17:42 -08001266 default:
1267 TRESPASS();
1268 break;
1269 }
1270}
1271
Wei Jia94211742014-10-28 17:09:06 -07001272void NuPlayer::onResume() {
Ronghua Wu64c2d172015-10-07 16:52:19 -07001273 if (!mPaused || mResetting) {
1274 ALOGD_IF(mResetting, "resetting, onResume discarded");
Chong Zhangefbb6192015-01-30 17:13:27 -08001275 return;
1276 }
1277 mPaused = false;
Wei Jia94211742014-10-28 17:09:06 -07001278 if (mSource != NULL) {
1279 mSource->resume();
1280 } else {
1281 ALOGW("resume called when source is gone or not set");
1282 }
1283 // |mAudioDecoder| may have been released due to the pause timeout, so re-create it if
1284 // needed.
1285 if (audioDecoderStillNeeded() && mAudioDecoder == NULL) {
1286 instantiateDecoder(true /* audio */, &mAudioDecoder);
1287 }
1288 if (mRenderer != NULL) {
1289 mRenderer->resume();
1290 } else {
1291 ALOGW("resume called when renderer is gone or not set");
1292 }
1293}
1294
Lajos Molnarfcd3e942015-03-31 10:06:48 -07001295status_t NuPlayer::onInstantiateSecureDecoders() {
1296 status_t err;
1297 if (!(mSourceFlags & Source::FLAG_SECURE)) {
1298 return BAD_TYPE;
1299 }
1300
1301 if (mRenderer != NULL) {
1302 ALOGE("renderer should not be set when instantiating secure decoders");
1303 return UNKNOWN_ERROR;
1304 }
1305
1306 // TRICKY: We rely on mRenderer being null, so that decoder does not start requesting
1307 // data on instantiation.
Lajos Molnar1de1e252015-04-30 18:18:34 -07001308 if (mSurface != NULL) {
Lajos Molnarfcd3e942015-03-31 10:06:48 -07001309 err = instantiateDecoder(false, &mVideoDecoder);
1310 if (err != OK) {
1311 return err;
1312 }
1313 }
1314
1315 if (mAudioSink != NULL) {
1316 err = instantiateDecoder(true, &mAudioDecoder);
1317 if (err != OK) {
1318 return err;
1319 }
1320 }
1321 return OK;
1322}
1323
Wei Jiac5de0912016-11-18 10:22:14 -08001324void NuPlayer::onStart(int64_t startPositionUs, MediaPlayerSeekMode mode) {
Robert Shih0c61a0d2015-07-06 15:09:10 -07001325 if (!mSourceStarted) {
1326 mSourceStarted = true;
1327 mSource->start();
1328 }
1329 if (startPositionUs > 0) {
Wei Jiac5de0912016-11-18 10:22:14 -08001330 performSeek(startPositionUs, mode);
Robert Shih0c61a0d2015-07-06 15:09:10 -07001331 if (mSource->getFormat(false /* audio */) == NULL) {
1332 return;
1333 }
1334 }
1335
Wei Jia94211742014-10-28 17:09:06 -07001336 mOffloadAudio = false;
1337 mAudioEOS = false;
1338 mVideoEOS = false;
Wei Jia94211742014-10-28 17:09:06 -07001339 mStarted = true;
Wei Jiafe8fe7d2016-06-08 10:29:25 -07001340 mPaused = false;
Wei Jia94211742014-10-28 17:09:06 -07001341
Wei Jia94211742014-10-28 17:09:06 -07001342 uint32_t flags = 0;
1343
1344 if (mSource->isRealTime()) {
1345 flags |= Renderer::FLAG_REAL_TIME;
1346 }
1347
1348 sp<MetaData> audioMeta = mSource->getFormatMeta(true /* audio */);
Wei Jia1de83d52016-10-10 10:15:03 -07001349 sp<MetaData> videoMeta = mSource->getFormatMeta(false /* audio */);
1350 if (audioMeta == NULL && videoMeta == NULL) {
1351 ALOGE("no metadata for either audio or video source");
1352 mSource->stop();
1353 mSourceStarted = false;
1354 notifyListener(MEDIA_ERROR, MEDIA_ERROR_UNKNOWN, ERROR_MALFORMED);
1355 return;
1356 }
Andy Hung12a84132015-10-20 16:09:21 -07001357 ALOGV_IF(audioMeta == NULL, "no metadata for audio source"); // video only stream
Wei Jia1de83d52016-10-10 10:15:03 -07001358
Wei Jia94211742014-10-28 17:09:06 -07001359 audio_stream_type_t streamType = AUDIO_STREAM_MUSIC;
1360 if (mAudioSink != NULL) {
1361 streamType = mAudioSink->getAudioStreamType();
1362 }
1363
1364 sp<AMessage> videoFormat = mSource->getFormat(false /* audio */);
1365
1366 mOffloadAudio =
Wei Jiabf70feb2016-02-19 15:47:16 -08001367 canOffloadStream(audioMeta, (videoFormat != NULL), mSource->isStreaming(), streamType)
1368 && (mPlaybackSettings.mSpeed == 1.f && mPlaybackSettings.mPitch == 1.f);
Wei Jia94211742014-10-28 17:09:06 -07001369 if (mOffloadAudio) {
1370 flags |= Renderer::FLAG_OFFLOAD_AUDIO;
1371 }
1372
Lajos Molnar1d15ab52015-03-04 16:46:34 -08001373 sp<AMessage> notify = new AMessage(kWhatRendererNotify, this);
Wei Jia94211742014-10-28 17:09:06 -07001374 ++mRendererGeneration;
1375 notify->setInt32("generation", mRendererGeneration);
1376 mRenderer = new Renderer(mAudioSink, notify, flags);
Wei Jia94211742014-10-28 17:09:06 -07001377 mRendererLooper = new ALooper;
1378 mRendererLooper->setName("NuPlayerRenderer");
1379 mRendererLooper->start(false, false, ANDROID_PRIORITY_AUDIO);
1380 mRendererLooper->registerHandler(mRenderer);
Lajos Molnar3a474aa2015-04-24 17:10:07 -07001381
1382 status_t err = mRenderer->setPlaybackSettings(mPlaybackSettings);
1383 if (err != OK) {
1384 mSource->stop();
Robert Shih0c61a0d2015-07-06 15:09:10 -07001385 mSourceStarted = false;
Lajos Molnar3a474aa2015-04-24 17:10:07 -07001386 notifyListener(MEDIA_ERROR, MEDIA_ERROR_UNKNOWN, err);
1387 return;
Wei Jiac8206ff2015-03-04 13:59:37 -08001388 }
Wei Jia94211742014-10-28 17:09:06 -07001389
Ronghua Wuc8a70d32015-04-29 16:26:34 -07001390 float rate = getFrameRate();
1391 if (rate > 0) {
Wei Jia94211742014-10-28 17:09:06 -07001392 mRenderer->setVideoFrameRate(rate);
1393 }
1394
Wei Jiac6cfd702014-11-11 16:33:20 -08001395 if (mVideoDecoder != NULL) {
1396 mVideoDecoder->setRenderer(mRenderer);
1397 }
1398 if (mAudioDecoder != NULL) {
1399 mAudioDecoder->setRenderer(mRenderer);
1400 }
1401
Wei Jia94211742014-10-28 17:09:06 -07001402 postScanSources();
1403}
1404
Chong Zhangefbb6192015-01-30 17:13:27 -08001405void NuPlayer::onPause() {
1406 if (mPaused) {
1407 return;
1408 }
1409 mPaused = true;
1410 if (mSource != NULL) {
1411 mSource->pause();
1412 } else {
1413 ALOGW("pause called when source is gone or not set");
1414 }
1415 if (mRenderer != NULL) {
1416 mRenderer->pause();
1417 } else {
1418 ALOGW("pause called when renderer is gone or not set");
1419 }
1420}
1421
Ronghua Wud7988b12014-10-03 15:19:10 -07001422bool NuPlayer::audioDecoderStillNeeded() {
1423 // Audio decoder is no longer needed if it's in shut/shutting down status.
1424 return ((mFlushingAudio != SHUT_DOWN) && (mFlushingAudio != SHUTTING_DOWN_DECODER));
1425}
1426
Andy Hung8d121d42014-10-03 09:53:53 -07001427void NuPlayer::handleFlushComplete(bool audio, bool isDecoder) {
1428 // We wait for both the decoder flush and the renderer flush to complete
1429 // before entering either the FLUSHED or the SHUTTING_DOWN_DECODER state.
1430
1431 mFlushComplete[audio][isDecoder] = true;
1432 if (!mFlushComplete[audio][!isDecoder]) {
1433 return;
1434 }
1435
1436 FlushStatus *state = audio ? &mFlushingAudio : &mFlushingVideo;
1437 switch (*state) {
1438 case FLUSHING_DECODER:
1439 {
1440 *state = FLUSHED;
Andy Hung8d121d42014-10-03 09:53:53 -07001441 break;
1442 }
1443
1444 case FLUSHING_DECODER_SHUTDOWN:
1445 {
1446 *state = SHUTTING_DOWN_DECODER;
1447
1448 ALOGV("initiating %s decoder shutdown", audio ? "audio" : "video");
1449 if (!audio) {
Andy Hung8d121d42014-10-03 09:53:53 -07001450 // Widevine source reads must stop before releasing the video decoder.
1451 if (mSource != NULL && mSourceFlags & Source::FLAG_SECURE) {
1452 mSource->stop();
Robert Shih0c61a0d2015-07-06 15:09:10 -07001453 mSourceStarted = false;
Andy Hung8d121d42014-10-03 09:53:53 -07001454 }
1455 }
1456 getDecoder(audio)->initiateShutdown();
1457 break;
1458 }
1459
1460 default:
1461 // decoder flush completes only occur in a flushing state.
1462 LOG_ALWAYS_FATAL_IF(isDecoder, "decoder flush in invalid state %d", *state);
1463 break;
1464 }
1465}
1466
Andreas Huber3831a062010-12-21 10:22:33 -08001467void NuPlayer::finishFlushIfPossible() {
Wei Jia53904f32014-07-29 10:22:53 -07001468 if (mFlushingAudio != NONE && mFlushingAudio != FLUSHED
1469 && mFlushingAudio != SHUT_DOWN) {
Andreas Huber3831a062010-12-21 10:22:33 -08001470 return;
1471 }
1472
Wei Jia53904f32014-07-29 10:22:53 -07001473 if (mFlushingVideo != NONE && mFlushingVideo != FLUSHED
1474 && mFlushingVideo != SHUT_DOWN) {
Andreas Huber3831a062010-12-21 10:22:33 -08001475 return;
1476 }
1477
Steve Block3856b092011-10-20 11:56:00 +01001478 ALOGV("both audio and video are flushed now.");
Andreas Huber3831a062010-12-21 10:22:33 -08001479
Andreas Huber3831a062010-12-21 10:22:33 -08001480 mFlushingAudio = NONE;
1481 mFlushingVideo = NONE;
Andreas Huber3831a062010-12-21 10:22:33 -08001482
Andy Hung8d121d42014-10-03 09:53:53 -07001483 clearFlushComplete();
1484
Andreas Hubera1f8ab02012-11-30 10:53:22 -08001485 processDeferredActions();
Andreas Huber1aef2112011-01-04 14:01:29 -08001486}
1487
1488void NuPlayer::postScanSources() {
1489 if (mScanSourcesPending) {
1490 return;
1491 }
1492
Lajos Molnar1d15ab52015-03-04 16:46:34 -08001493 sp<AMessage> msg = new AMessage(kWhatScanSources, this);
Andreas Huber1aef2112011-01-04 14:01:29 -08001494 msg->setInt32("generation", mScanSourcesGeneration);
1495 msg->post();
1496
1497 mScanSourcesPending = true;
1498}
1499
Wei Jia41cd4632016-05-13 10:53:30 -07001500void NuPlayer::tryOpenAudioSinkForOffload(
1501 const sp<AMessage> &format, const sp<MetaData> &audioMeta, bool hasVideo) {
Andy Hung202bce12014-12-03 11:47:36 -08001502 // Note: This is called early in NuPlayer to determine whether offloading
1503 // is possible; otherwise the decoders call the renderer openAudioSink directly.
Andy Hung282a7e32014-08-14 15:56:34 -07001504
Andy Hung202bce12014-12-03 11:47:36 -08001505 status_t err = mRenderer->openAudioSink(
1506 format, true /* offloadOnly */, hasVideo, AUDIO_OUTPUT_FLAG_NONE, &mOffloadAudio);
1507 if (err != OK) {
1508 // Any failure we turn off mOffloadAudio.
1509 mOffloadAudio = false;
1510 } else if (mOffloadAudio) {
Chong Zhang3b9eb1f2014-10-15 17:05:08 -07001511 sendMetaDataToHal(mAudioSink, audioMeta);
Andy Hung282a7e32014-08-14 15:56:34 -07001512 }
1513}
1514
1515void NuPlayer::closeAudioSink() {
Chong Zhang3b9eb1f2014-10-15 17:05:08 -07001516 mRenderer->closeAudioSink();
Andy Hung282a7e32014-08-14 15:56:34 -07001517}
1518
Wei Jia5031b2f2016-02-25 11:19:31 -08001519void NuPlayer::restartAudio(
Wei Jiabf70feb2016-02-19 15:47:16 -08001520 int64_t currentPositionUs, bool forceNonOffload, bool needsToCreateAudioDecoder) {
Wei Jiaa05f1e32016-03-25 16:31:22 -07001521 if (mAudioDecoder != NULL) {
1522 mAudioDecoder->pause();
1523 mAudioDecoder.clear();
1524 ++mAudioDecoderGeneration;
1525 }
Wei Jiabf70feb2016-02-19 15:47:16 -08001526 if (mFlushingAudio == FLUSHING_DECODER) {
1527 mFlushComplete[1 /* audio */][1 /* isDecoder */] = true;
1528 mFlushingAudio = FLUSHED;
1529 finishFlushIfPossible();
1530 } else if (mFlushingAudio == FLUSHING_DECODER_SHUTDOWN
1531 || mFlushingAudio == SHUTTING_DOWN_DECODER) {
1532 mFlushComplete[1 /* audio */][1 /* isDecoder */] = true;
1533 mFlushingAudio = SHUT_DOWN;
1534 finishFlushIfPossible();
1535 needsToCreateAudioDecoder = false;
1536 }
1537 if (mRenderer == NULL) {
1538 return;
1539 }
1540 closeAudioSink();
1541 mRenderer->flush(true /* audio */, false /* notifyComplete */);
1542 if (mVideoDecoder != NULL) {
1543 mRenderer->flush(false /* audio */, false /* notifyComplete */);
1544 }
1545
Wei Jiac5de0912016-11-18 10:22:14 -08001546 performSeek(currentPositionUs, MediaPlayerSeekMode::SEEK_PREVIOUS_SYNC /* mode */);
Wei Jiabf70feb2016-02-19 15:47:16 -08001547
1548 if (forceNonOffload) {
1549 mRenderer->signalDisableOffloadAudio();
1550 mOffloadAudio = false;
1551 }
1552 if (needsToCreateAudioDecoder) {
Wei Jiaa05f1e32016-03-25 16:31:22 -07001553 instantiateDecoder(true /* audio */, &mAudioDecoder, !forceNonOffload);
Wei Jiabf70feb2016-02-19 15:47:16 -08001554 }
1555}
1556
Wei Jia41cd4632016-05-13 10:53:30 -07001557void NuPlayer::determineAudioModeChange(const sp<AMessage> &audioFormat) {
Wei Jiae4d18c72015-07-13 17:58:11 -07001558 if (mSource == NULL || mAudioSink == NULL) {
1559 return;
1560 }
1561
1562 if (mRenderer == NULL) {
1563 ALOGW("No renderer can be used to determine audio mode. Use non-offload for safety.");
1564 mOffloadAudio = false;
1565 return;
1566 }
1567
1568 sp<MetaData> audioMeta = mSource->getFormatMeta(true /* audio */);
1569 sp<AMessage> videoFormat = mSource->getFormat(false /* audio */);
1570 audio_stream_type_t streamType = mAudioSink->getAudioStreamType();
1571 const bool hasVideo = (videoFormat != NULL);
1572 const bool canOffload = canOffloadStream(
Wei Jiabf70feb2016-02-19 15:47:16 -08001573 audioMeta, hasVideo, mSource->isStreaming(), streamType)
1574 && (mPlaybackSettings.mSpeed == 1.f && mPlaybackSettings.mPitch == 1.f);
Wei Jiae4d18c72015-07-13 17:58:11 -07001575 if (canOffload) {
1576 if (!mOffloadAudio) {
1577 mRenderer->signalEnableOffloadAudio();
1578 }
1579 // open audio sink early under offload mode.
Wei Jia41cd4632016-05-13 10:53:30 -07001580 tryOpenAudioSinkForOffload(audioFormat, audioMeta, hasVideo);
Wei Jiae4d18c72015-07-13 17:58:11 -07001581 } else {
Robert Shihe1d70192015-07-23 17:54:13 -07001582 if (mOffloadAudio) {
1583 mRenderer->signalDisableOffloadAudio();
1584 mOffloadAudio = false;
1585 }
Wei Jiae4d18c72015-07-13 17:58:11 -07001586 }
1587}
1588
Wei Jiaa05f1e32016-03-25 16:31:22 -07001589status_t NuPlayer::instantiateDecoder(
1590 bool audio, sp<DecoderBase> *decoder, bool checkAudioModeChange) {
Wei Jia566da802015-08-27 13:59:30 -07001591 // The audio decoder could be cleared by tear down. If still in shut down
1592 // process, no need to create a new audio decoder.
1593 if (*decoder != NULL || (audio && mFlushingAudio == SHUT_DOWN)) {
Andreas Huberf9334412010-12-15 15:17:42 -08001594 return OK;
1595 }
1596
Andreas Huber84066782011-08-16 09:34:26 -07001597 sp<AMessage> format = mSource->getFormat(audio);
Andreas Huberf9334412010-12-15 15:17:42 -08001598
Andreas Huber84066782011-08-16 09:34:26 -07001599 if (format == NULL) {
Robert Shih7350b052015-10-01 15:50:14 -07001600 return UNKNOWN_ERROR;
1601 } else {
1602 status_t err;
1603 if (format->findInt32("err", &err) && err) {
1604 return err;
1605 }
Andreas Huberf9334412010-12-15 15:17:42 -08001606 }
1607
Ronghua Wu8db88132015-04-22 13:51:35 -07001608 format->setInt32("priority", 0 /* realtime */);
1609
Andreas Huber3fe62152011-09-16 15:09:22 -07001610 if (!audio) {
Andreas Huber84066782011-08-16 09:34:26 -07001611 AString mime;
1612 CHECK(format->findString("mime", &mime));
Chong Zhanga7fa1d92014-06-11 14:49:23 -07001613
Lajos Molnar1d15ab52015-03-04 16:46:34 -08001614 sp<AMessage> ccNotify = new AMessage(kWhatClosedCaptionNotify, this);
Chong Zhang341ab6e2015-02-04 13:37:18 -08001615 if (mCCDecoder == NULL) {
1616 mCCDecoder = new CCDecoder(ccNotify);
1617 }
Lajos Molnar09524832014-07-17 14:29:51 -07001618
1619 if (mSourceFlags & Source::FLAG_SECURE) {
1620 format->setInt32("secure", true);
1621 }
Chong Zhang17134602015-01-07 16:14:34 -08001622
1623 if (mSourceFlags & Source::FLAG_PROTECTED) {
1624 format->setInt32("protected", true);
1625 }
Ronghua Wu8db88132015-04-22 13:51:35 -07001626
Ronghua Wuc8a70d32015-04-29 16:26:34 -07001627 float rate = getFrameRate();
1628 if (rate > 0) {
Lajos Molnar3a474aa2015-04-24 17:10:07 -07001629 format->setFloat("operating-rate", rate * mPlaybackSettings.mSpeed);
Ronghua Wu8db88132015-04-22 13:51:35 -07001630 }
Andreas Huber3fe62152011-09-16 15:09:22 -07001631 }
1632
Wei Jiabc2fb722014-07-08 16:37:57 -07001633 if (audio) {
Lajos Molnar1d15ab52015-03-04 16:46:34 -08001634 sp<AMessage> notify = new AMessage(kWhatAudioNotify, this);
Wei Jia88703c32014-08-06 11:24:07 -07001635 ++mAudioDecoderGeneration;
1636 notify->setInt32("generation", mAudioDecoderGeneration);
1637
Wei Jiaa05f1e32016-03-25 16:31:22 -07001638 if (checkAudioModeChange) {
Wei Jia41cd4632016-05-13 10:53:30 -07001639 determineAudioModeChange(format);
Wei Jiaa05f1e32016-03-25 16:31:22 -07001640 }
Wei Jiabc2fb722014-07-08 16:37:57 -07001641 if (mOffloadAudio) {
Wei Jia14532f22015-12-29 11:28:15 -08001642 mSource->setOffloadAudio(true /* offload */);
1643
Haynes Mathew George8b635332015-03-30 17:59:47 -07001644 const bool hasVideo = (mSource->getFormat(false /*audio */) != NULL);
1645 format->setInt32("has-video", hasVideo);
Wei Jiac6cfd702014-11-11 16:33:20 -08001646 *decoder = new DecoderPassThrough(notify, mSource, mRenderer);
Wei Jiabc2fb722014-07-08 16:37:57 -07001647 } else {
Wei Jia14532f22015-12-29 11:28:15 -08001648 mSource->setOffloadAudio(false /* offload */);
1649
Wei Jiaf2ae3e12016-10-27 17:10:59 -07001650 *decoder = new Decoder(notify, mSource, mPID, mUID, mRenderer);
Wei Jiabc2fb722014-07-08 16:37:57 -07001651 }
1652 } else {
Lajos Molnar1d15ab52015-03-04 16:46:34 -08001653 sp<AMessage> notify = new AMessage(kWhatVideoNotify, this);
Wei Jia88703c32014-08-06 11:24:07 -07001654 ++mVideoDecoderGeneration;
1655 notify->setInt32("generation", mVideoDecoderGeneration);
1656
Chong Zhang7137ec72014-11-12 16:41:05 -08001657 *decoder = new Decoder(
Wei Jiaf2ae3e12016-10-27 17:10:59 -07001658 notify, mSource, mPID, mUID, mRenderer, mSurface, mCCDecoder);
Lajos Molnard9fd6312014-11-06 11:00:00 -08001659
1660 // enable FRC if high-quality AV sync is requested, even if not
Lajos Molnar1de1e252015-04-30 18:18:34 -07001661 // directly queuing to display, as this will even improve textureview
Lajos Molnard9fd6312014-11-06 11:00:00 -08001662 // playback.
1663 {
1664 char value[PROPERTY_VALUE_MAX];
1665 if (property_get("persist.sys.media.avsync", value, NULL) &&
1666 (!strcmp("1", value) || !strcasecmp("true", value))) {
1667 format->setInt32("auto-frc", 1);
1668 }
1669 }
Wei Jiabc2fb722014-07-08 16:37:57 -07001670 }
Lajos Molnar1cd13982014-01-17 15:12:51 -08001671 (*decoder)->init();
Andreas Huber84066782011-08-16 09:34:26 -07001672 (*decoder)->configure(format);
Andreas Huberf9334412010-12-15 15:17:42 -08001673
Lajos Molnar09524832014-07-17 14:29:51 -07001674 // allocate buffers to decrypt widevine source buffers
1675 if (!audio && (mSourceFlags & Source::FLAG_SECURE)) {
Wonsik Kim7e34bf52016-08-23 00:09:18 +09001676 Vector<sp<MediaCodecBuffer> > inputBufs;
Lajos Molnar09524832014-07-17 14:29:51 -07001677 CHECK_EQ((*decoder)->getInputBuffers(&inputBufs), (status_t)OK);
1678
1679 Vector<MediaBuffer *> mediaBufs;
1680 for (size_t i = 0; i < inputBufs.size(); i++) {
Wonsik Kim7e34bf52016-08-23 00:09:18 +09001681 const sp<MediaCodecBuffer> &buffer = inputBufs[i];
Lajos Molnar09524832014-07-17 14:29:51 -07001682 MediaBuffer *mbuf = new MediaBuffer(buffer->data(), buffer->size());
1683 mediaBufs.push(mbuf);
1684 }
1685
1686 status_t err = mSource->setBuffers(audio, mediaBufs);
1687 if (err != OK) {
1688 for (size_t i = 0; i < mediaBufs.size(); ++i) {
1689 mediaBufs[i]->release();
1690 }
1691 mediaBufs.clear();
1692 ALOGE("Secure source didn't support secure mediaBufs.");
1693 return err;
1694 }
1695 }
Praveen Chavanbbaa1442016-04-08 13:33:49 -07001696
1697 if (!audio) {
1698 sp<AMessage> params = new AMessage();
1699 float rate = getFrameRate();
1700 if (rate > 0) {
1701 params->setFloat("frame-rate-total", rate);
1702 }
1703
1704 sp<MetaData> fileMeta = getFileMeta();
1705 if (fileMeta != NULL) {
1706 int32_t videoTemporalLayerCount;
1707 if (fileMeta->findInt32(kKeyTemporalLayerCount, &videoTemporalLayerCount)
1708 && videoTemporalLayerCount > 0) {
1709 params->setInt32("temporal-layer-count", videoTemporalLayerCount);
1710 }
1711 }
1712
1713 if (params->countEntries() > 0) {
1714 (*decoder)->setParameters(params);
1715 }
1716 }
Andreas Huberf9334412010-12-15 15:17:42 -08001717 return OK;
1718}
1719
Chong Zhangced1c2f2014-08-08 15:22:35 -07001720void NuPlayer::updateVideoSize(
1721 const sp<AMessage> &inputFormat,
1722 const sp<AMessage> &outputFormat) {
1723 if (inputFormat == NULL) {
1724 ALOGW("Unknown video size, reporting 0x0!");
1725 notifyListener(MEDIA_SET_VIDEO_SIZE, 0, 0);
1726 return;
1727 }
1728
1729 int32_t displayWidth, displayHeight;
Chong Zhangced1c2f2014-08-08 15:22:35 -07001730 if (outputFormat != NULL) {
1731 int32_t width, height;
1732 CHECK(outputFormat->findInt32("width", &width));
1733 CHECK(outputFormat->findInt32("height", &height));
1734
1735 int32_t cropLeft, cropTop, cropRight, cropBottom;
1736 CHECK(outputFormat->findRect(
1737 "crop",
1738 &cropLeft, &cropTop, &cropRight, &cropBottom));
1739
1740 displayWidth = cropRight - cropLeft + 1;
1741 displayHeight = cropBottom - cropTop + 1;
1742
1743 ALOGV("Video output format changed to %d x %d "
1744 "(crop: %d x %d @ (%d, %d))",
1745 width, height,
1746 displayWidth,
1747 displayHeight,
1748 cropLeft, cropTop);
1749 } else {
1750 CHECK(inputFormat->findInt32("width", &displayWidth));
1751 CHECK(inputFormat->findInt32("height", &displayHeight));
1752
1753 ALOGV("Video input format %d x %d", displayWidth, displayHeight);
1754 }
1755
1756 // Take into account sample aspect ratio if necessary:
1757 int32_t sarWidth, sarHeight;
1758 if (inputFormat->findInt32("sar-width", &sarWidth)
1759 && inputFormat->findInt32("sar-height", &sarHeight)) {
1760 ALOGV("Sample aspect ratio %d : %d", sarWidth, sarHeight);
1761
1762 displayWidth = (displayWidth * sarWidth) / sarHeight;
1763
1764 ALOGV("display dimensions %d x %d", displayWidth, displayHeight);
Wei Jiadf6c6af2016-09-29 11:27:15 -07001765 } else {
1766 int32_t width, height;
1767 if (inputFormat->findInt32("display-width", &width)
1768 && inputFormat->findInt32("display-height", &height)
1769 && width > 0 && height > 0
1770 && displayWidth > 0 && displayHeight > 0) {
1771 if (displayHeight * (int64_t)width / height > (int64_t)displayWidth) {
1772 displayHeight = (int32_t)(displayWidth * (int64_t)height / width);
1773 } else {
1774 displayWidth = (int32_t)(displayHeight * (int64_t)width / height);
1775 }
1776 ALOGV("Video display width and height are overridden to %d x %d",
1777 displayWidth, displayHeight);
1778 }
Chong Zhangced1c2f2014-08-08 15:22:35 -07001779 }
1780
1781 int32_t rotationDegrees;
1782 if (!inputFormat->findInt32("rotation-degrees", &rotationDegrees)) {
1783 rotationDegrees = 0;
1784 }
1785
1786 if (rotationDegrees == 90 || rotationDegrees == 270) {
1787 int32_t tmp = displayWidth;
1788 displayWidth = displayHeight;
1789 displayHeight = tmp;
1790 }
1791
1792 notifyListener(
1793 MEDIA_SET_VIDEO_SIZE,
1794 displayWidth,
1795 displayHeight);
1796}
1797
Chong Zhangdcb89b32013-08-06 09:44:47 -07001798void NuPlayer::notifyListener(int msg, int ext1, int ext2, const Parcel *in) {
Andreas Huber43c3e6c2011-01-05 12:17:08 -08001799 if (mDriver == NULL) {
Andreas Huberf9334412010-12-15 15:17:42 -08001800 return;
1801 }
1802
Andreas Huber43c3e6c2011-01-05 12:17:08 -08001803 sp<NuPlayerDriver> driver = mDriver.promote();
Andreas Huberf9334412010-12-15 15:17:42 -08001804
Andreas Huber43c3e6c2011-01-05 12:17:08 -08001805 if (driver == NULL) {
Andreas Huberf9334412010-12-15 15:17:42 -08001806 return;
1807 }
1808
Chong Zhangdcb89b32013-08-06 09:44:47 -07001809 driver->notifyListener(msg, ext1, ext2, in);
Andreas Huberf9334412010-12-15 15:17:42 -08001810}
1811
Chong Zhang7137ec72014-11-12 16:41:05 -08001812void NuPlayer::flushDecoder(bool audio, bool needShutdown) {
Andreas Huber14f76722013-01-15 09:04:18 -08001813 ALOGV("[%s] flushDecoder needShutdown=%d",
1814 audio ? "audio" : "video", needShutdown);
1815
Chong Zhang7137ec72014-11-12 16:41:05 -08001816 const sp<DecoderBase> &decoder = getDecoder(audio);
Lajos Molnar87603c02014-08-20 19:25:30 -07001817 if (decoder == NULL) {
Steve Blockdf64d152012-01-04 20:05:49 +00001818 ALOGI("flushDecoder %s without decoder present",
Andreas Huber6e3d3112011-11-28 12:36:11 -08001819 audio ? "audio" : "video");
Lajos Molnar87603c02014-08-20 19:25:30 -07001820 return;
Andreas Huber6e3d3112011-11-28 12:36:11 -08001821 }
1822
Andreas Huber1aef2112011-01-04 14:01:29 -08001823 // Make sure we don't continue to scan sources until we finish flushing.
1824 ++mScanSourcesGeneration;
Chong Zhang3b032b32015-04-17 15:49:06 -07001825 if (mScanSourcesPending) {
Toshikazu Saitocbcbb792015-09-15 14:53:01 +09001826 if (!needShutdown) {
1827 mDeferredActions.push_back(
1828 new SimpleAction(&NuPlayer::performScanSources));
1829 }
Chong Zhang3b032b32015-04-17 15:49:06 -07001830 mScanSourcesPending = false;
1831 }
Andreas Huber1aef2112011-01-04 14:01:29 -08001832
Chong Zhang7137ec72014-11-12 16:41:05 -08001833 decoder->signalFlush();
Andreas Huber1aef2112011-01-04 14:01:29 -08001834
1835 FlushStatus newStatus =
1836 needShutdown ? FLUSHING_DECODER_SHUTDOWN : FLUSHING_DECODER;
1837
Lajos Molnarfcd3e942015-03-31 10:06:48 -07001838 mFlushComplete[audio][false /* isDecoder */] = (mRenderer == NULL);
Andy Hung8d121d42014-10-03 09:53:53 -07001839 mFlushComplete[audio][true /* isDecoder */] = false;
Andreas Huber1aef2112011-01-04 14:01:29 -08001840 if (audio) {
Wei Jia53904f32014-07-29 10:22:53 -07001841 ALOGE_IF(mFlushingAudio != NONE,
1842 "audio flushDecoder() is called in state %d", mFlushingAudio);
Andreas Huber1aef2112011-01-04 14:01:29 -08001843 mFlushingAudio = newStatus;
Andreas Huber1aef2112011-01-04 14:01:29 -08001844 } else {
Wei Jia53904f32014-07-29 10:22:53 -07001845 ALOGE_IF(mFlushingVideo != NONE,
1846 "video flushDecoder() is called in state %d", mFlushingVideo);
Andreas Huber1aef2112011-01-04 14:01:29 -08001847 mFlushingVideo = newStatus;
Andreas Huber1aef2112011-01-04 14:01:29 -08001848 }
1849}
1850
Chong Zhangced1c2f2014-08-08 15:22:35 -07001851void NuPlayer::queueDecoderShutdown(
1852 bool audio, bool video, const sp<AMessage> &reply) {
1853 ALOGI("queueDecoderShutdown audio=%d, video=%d", audio, video);
Andreas Huber84066782011-08-16 09:34:26 -07001854
Chong Zhangced1c2f2014-08-08 15:22:35 -07001855 mDeferredActions.push_back(
Wei Jiafef808d2014-10-31 17:57:05 -07001856 new FlushDecoderAction(
1857 audio ? FLUSH_CMD_SHUTDOWN : FLUSH_CMD_NONE,
1858 video ? FLUSH_CMD_SHUTDOWN : FLUSH_CMD_NONE));
Andreas Huber84066782011-08-16 09:34:26 -07001859
Chong Zhangced1c2f2014-08-08 15:22:35 -07001860 mDeferredActions.push_back(
1861 new SimpleAction(&NuPlayer::performScanSources));
Andreas Huber84066782011-08-16 09:34:26 -07001862
Chong Zhangced1c2f2014-08-08 15:22:35 -07001863 mDeferredActions.push_back(new PostMessageAction(reply));
1864
1865 processDeferredActions();
Andreas Huber84066782011-08-16 09:34:26 -07001866}
1867
James Dong0d268a32012-08-31 12:18:27 -07001868status_t NuPlayer::setVideoScalingMode(int32_t mode) {
1869 mVideoScalingMode = mode;
Lajos Molnar1de1e252015-04-30 18:18:34 -07001870 if (mSurface != NULL) {
1871 status_t ret = native_window_set_scaling_mode(mSurface.get(), mVideoScalingMode);
James Dong0d268a32012-08-31 12:18:27 -07001872 if (ret != OK) {
1873 ALOGE("Failed to set scaling mode (%d): %s",
1874 -ret, strerror(-ret));
1875 return ret;
1876 }
1877 }
1878 return OK;
1879}
1880
Chong Zhangdcb89b32013-08-06 09:44:47 -07001881status_t NuPlayer::getTrackInfo(Parcel* reply) const {
Lajos Molnar1d15ab52015-03-04 16:46:34 -08001882 sp<AMessage> msg = new AMessage(kWhatGetTrackInfo, this);
Chong Zhangdcb89b32013-08-06 09:44:47 -07001883 msg->setPointer("reply", reply);
1884
1885 sp<AMessage> response;
1886 status_t err = msg->postAndAwaitResponse(&response);
1887 return err;
1888}
1889
Robert Shih7c4f0d72014-07-09 18:53:31 -07001890status_t NuPlayer::getSelectedTrack(int32_t type, Parcel* reply) const {
Lajos Molnar1d15ab52015-03-04 16:46:34 -08001891 sp<AMessage> msg = new AMessage(kWhatGetSelectedTrack, this);
Robert Shih7c4f0d72014-07-09 18:53:31 -07001892 msg->setPointer("reply", reply);
1893 msg->setInt32("type", type);
1894
1895 sp<AMessage> response;
1896 status_t err = msg->postAndAwaitResponse(&response);
1897 if (err == OK && response != NULL) {
1898 CHECK(response->findInt32("err", &err));
1899 }
1900 return err;
1901}
1902
Robert Shih6ffb1fd2014-10-29 16:24:32 -07001903status_t NuPlayer::selectTrack(size_t trackIndex, bool select, int64_t timeUs) {
Lajos Molnar1d15ab52015-03-04 16:46:34 -08001904 sp<AMessage> msg = new AMessage(kWhatSelectTrack, this);
Chong Zhangdcb89b32013-08-06 09:44:47 -07001905 msg->setSize("trackIndex", trackIndex);
1906 msg->setInt32("select", select);
Robert Shih6ffb1fd2014-10-29 16:24:32 -07001907 msg->setInt64("timeUs", timeUs);
Chong Zhangdcb89b32013-08-06 09:44:47 -07001908
1909 sp<AMessage> response;
1910 status_t err = msg->postAndAwaitResponse(&response);
1911
Chong Zhang404fced2014-06-11 14:45:31 -07001912 if (err != OK) {
1913 return err;
1914 }
1915
1916 if (!response->findInt32("err", &err)) {
1917 err = OK;
1918 }
1919
Chong Zhangdcb89b32013-08-06 09:44:47 -07001920 return err;
1921}
1922
Ronghua Wua73d9e02014-10-08 15:13:29 -07001923status_t NuPlayer::getCurrentPosition(int64_t *mediaUs) {
1924 sp<Renderer> renderer = mRenderer;
1925 if (renderer == NULL) {
1926 return NO_INIT;
1927 }
1928
1929 return renderer->getCurrentPosition(mediaUs);
1930}
1931
Praveen Chavane1e5d7a2015-05-19 19:09:48 -07001932void NuPlayer::getStats(Vector<sp<AMessage> > *mTrackStats) {
1933 CHECK(mTrackStats != NULL);
1934
1935 mTrackStats->clear();
1936 if (mVideoDecoder != NULL) {
1937 mTrackStats->push_back(mVideoDecoder->getStats());
1938 }
1939 if (mAudioDecoder != NULL) {
1940 mTrackStats->push_back(mAudioDecoder->getStats());
Chong Zhang7137ec72014-11-12 16:41:05 -08001941 }
Ronghua Wua73d9e02014-10-08 15:13:29 -07001942}
1943
Marco Nelissenf0b72b52014-09-16 15:43:44 -07001944sp<MetaData> NuPlayer::getFileMeta() {
1945 return mSource->getFileFormatMeta();
1946}
1947
Ronghua Wuc8a70d32015-04-29 16:26:34 -07001948float NuPlayer::getFrameRate() {
1949 sp<MetaData> meta = mSource->getFormatMeta(false /* audio */);
1950 if (meta == NULL) {
1951 return 0;
1952 }
1953 int32_t rate;
1954 if (!meta->findInt32(kKeyFrameRate, &rate)) {
1955 // fall back to try file meta
1956 sp<MetaData> fileMeta = getFileMeta();
1957 if (fileMeta == NULL) {
1958 ALOGW("source has video meta but not file meta");
1959 return -1;
1960 }
1961 int32_t fileMetaRate;
1962 if (!fileMeta->findInt32(kKeyFrameRate, &fileMetaRate)) {
1963 return -1;
1964 }
1965 return fileMetaRate;
1966 }
1967 return rate;
1968}
1969
Andreas Huberb7c8e912012-11-27 15:02:53 -08001970void NuPlayer::schedulePollDuration() {
Lajos Molnar1d15ab52015-03-04 16:46:34 -08001971 sp<AMessage> msg = new AMessage(kWhatPollDuration, this);
Andreas Huberb7c8e912012-11-27 15:02:53 -08001972 msg->setInt32("generation", mPollDurationGeneration);
1973 msg->post();
1974}
1975
1976void NuPlayer::cancelPollDuration() {
1977 ++mPollDurationGeneration;
1978}
1979
Andreas Hubera1f8ab02012-11-30 10:53:22 -08001980void NuPlayer::processDeferredActions() {
1981 while (!mDeferredActions.empty()) {
1982 // We won't execute any deferred actions until we're no longer in
1983 // an intermediate state, i.e. one more more decoders are currently
1984 // flushing or shutting down.
1985
Andreas Hubera1f8ab02012-11-30 10:53:22 -08001986 if (mFlushingAudio != NONE || mFlushingVideo != NONE) {
1987 // We're currently flushing, postpone the reset until that's
1988 // completed.
1989
1990 ALOGV("postponing action mFlushingAudio=%d, mFlushingVideo=%d",
1991 mFlushingAudio, mFlushingVideo);
1992
1993 break;
1994 }
1995
1996 sp<Action> action = *mDeferredActions.begin();
1997 mDeferredActions.erase(mDeferredActions.begin());
1998
1999 action->execute(this);
2000 }
2001}
2002
Wei Jiac5de0912016-11-18 10:22:14 -08002003void NuPlayer::performSeek(int64_t seekTimeUs, MediaPlayerSeekMode mode) {
2004 ALOGV("performSeek seekTimeUs=%lld us (%.2f secs), mode=%d",
2005 (long long)seekTimeUs, seekTimeUs / 1E6, mode);
Andreas Hubera1f8ab02012-11-30 10:53:22 -08002006
Andy Hungadf34bf2014-09-03 18:22:22 -07002007 if (mSource == NULL) {
2008 // This happens when reset occurs right before the loop mode
2009 // asynchronously seeks to the start of the stream.
2010 LOG_ALWAYS_FATAL_IF(mAudioDecoder != NULL || mVideoDecoder != NULL,
2011 "mSource is NULL and decoders not NULL audio(%p) video(%p)",
2012 mAudioDecoder.get(), mVideoDecoder.get());
2013 return;
2014 }
Robert Shih1a5c8592015-08-04 18:07:44 -07002015 mPreviousSeekTimeUs = seekTimeUs;
Wei Jiac5de0912016-11-18 10:22:14 -08002016 mSource->seekTo(seekTimeUs, mode);
Robert Shihd3b0bbb2014-07-23 15:00:25 -07002017 ++mTimedTextGeneration;
Andreas Hubera1f8ab02012-11-30 10:53:22 -08002018
Andreas Hubera1f8ab02012-11-30 10:53:22 -08002019 // everything's flushed, continue playback.
2020}
2021
Wei Jiafef808d2014-10-31 17:57:05 -07002022void NuPlayer::performDecoderFlush(FlushCommand audio, FlushCommand video) {
2023 ALOGV("performDecoderFlush audio=%d, video=%d", audio, video);
Andreas Hubera1f8ab02012-11-30 10:53:22 -08002024
Wei Jiafef808d2014-10-31 17:57:05 -07002025 if ((audio == FLUSH_CMD_NONE || mAudioDecoder == NULL)
2026 && (video == FLUSH_CMD_NONE || mVideoDecoder == NULL)) {
Andreas Hubera1f8ab02012-11-30 10:53:22 -08002027 return;
2028 }
2029
Wei Jiafef808d2014-10-31 17:57:05 -07002030 if (audio != FLUSH_CMD_NONE && mAudioDecoder != NULL) {
2031 flushDecoder(true /* audio */, (audio == FLUSH_CMD_SHUTDOWN));
Andreas Hubera1f8ab02012-11-30 10:53:22 -08002032 }
2033
Wei Jiafef808d2014-10-31 17:57:05 -07002034 if (video != FLUSH_CMD_NONE && mVideoDecoder != NULL) {
2035 flushDecoder(false /* audio */, (video == FLUSH_CMD_SHUTDOWN));
Andreas Hubera1f8ab02012-11-30 10:53:22 -08002036 }
2037}
2038
2039void NuPlayer::performReset() {
2040 ALOGV("performReset");
2041
2042 CHECK(mAudioDecoder == NULL);
2043 CHECK(mVideoDecoder == NULL);
2044
2045 cancelPollDuration();
2046
2047 ++mScanSourcesGeneration;
2048 mScanSourcesPending = false;
2049
Lajos Molnar09524832014-07-17 14:29:51 -07002050 if (mRendererLooper != NULL) {
2051 if (mRenderer != NULL) {
2052 mRendererLooper->unregisterHandler(mRenderer->id());
2053 }
2054 mRendererLooper->stop();
2055 mRendererLooper.clear();
2056 }
Andreas Hubera1f8ab02012-11-30 10:53:22 -08002057 mRenderer.clear();
Wei Jia57568df2014-09-22 10:16:29 -07002058 ++mRendererGeneration;
Andreas Hubera1f8ab02012-11-30 10:53:22 -08002059
2060 if (mSource != NULL) {
2061 mSource->stop();
Andreas Huberb5f25f02013-02-05 10:14:26 -08002062
Wei Jiac45a4b22016-04-15 15:30:23 -07002063 Mutex::Autolock autoLock(mSourceLock);
Andreas Hubera1f8ab02012-11-30 10:53:22 -08002064 mSource.clear();
2065 }
2066
2067 if (mDriver != NULL) {
2068 sp<NuPlayerDriver> driver = mDriver.promote();
2069 if (driver != NULL) {
2070 driver->notifyResetComplete();
2071 }
2072 }
Andreas Huber57a339c2012-12-03 11:18:00 -08002073
2074 mStarted = false;
Wei Jia8a092d32016-06-03 14:57:24 -07002075 mPrepared = false;
Ronghua Wu64c2d172015-10-07 16:52:19 -07002076 mResetting = false;
Robert Shih0c61a0d2015-07-06 15:09:10 -07002077 mSourceStarted = false;
Andreas Hubera1f8ab02012-11-30 10:53:22 -08002078}
2079
2080void NuPlayer::performScanSources() {
2081 ALOGV("performScanSources");
2082
Andreas Huber57a339c2012-12-03 11:18:00 -08002083 if (!mStarted) {
2084 return;
2085 }
2086
Andreas Hubera1f8ab02012-11-30 10:53:22 -08002087 if (mAudioDecoder == NULL || mVideoDecoder == NULL) {
2088 postScanSources();
2089 }
2090}
2091
Lajos Molnar1de1e252015-04-30 18:18:34 -07002092void NuPlayer::performSetSurface(const sp<Surface> &surface) {
Andreas Huber57a339c2012-12-03 11:18:00 -08002093 ALOGV("performSetSurface");
2094
Lajos Molnar1de1e252015-04-30 18:18:34 -07002095 mSurface = surface;
Andreas Huber57a339c2012-12-03 11:18:00 -08002096
2097 // XXX - ignore error from setVideoScalingMode for now
2098 setVideoScalingMode(mVideoScalingMode);
Chong Zhang13d6faa2014-08-22 15:35:28 -07002099
2100 if (mDriver != NULL) {
2101 sp<NuPlayerDriver> driver = mDriver.promote();
2102 if (driver != NULL) {
2103 driver->notifySetSurfaceComplete();
2104 }
2105 }
Andreas Huber57a339c2012-12-03 11:18:00 -08002106}
2107
Chong Zhangf8d71772014-11-26 15:08:34 -08002108void NuPlayer::performResumeDecoders(bool needNotify) {
2109 if (needNotify) {
2110 mResumePending = true;
2111 if (mVideoDecoder == NULL) {
2112 // if audio-only, we can notify seek complete now,
2113 // as the resume operation will be relatively fast.
2114 finishResume();
2115 }
2116 }
2117
Chong Zhang7137ec72014-11-12 16:41:05 -08002118 if (mVideoDecoder != NULL) {
Chong Zhangf8d71772014-11-26 15:08:34 -08002119 // When there is continuous seek, MediaPlayer will cache the seek
2120 // position, and send down new seek request when previous seek is
2121 // complete. Let's wait for at least one video output frame before
2122 // notifying seek complete, so that the video thumbnail gets updated
2123 // when seekbar is dragged.
2124 mVideoDecoder->signalResume(needNotify);
Chong Zhang7137ec72014-11-12 16:41:05 -08002125 }
2126
2127 if (mAudioDecoder != NULL) {
Chong Zhangf8d71772014-11-26 15:08:34 -08002128 mAudioDecoder->signalResume(false /* needNotify */);
2129 }
2130}
2131
2132void NuPlayer::finishResume() {
2133 if (mResumePending) {
2134 mResumePending = false;
Wei Jia1061c9c2015-05-19 16:02:17 -07002135 notifyDriverSeekComplete();
2136 }
2137}
2138
2139void NuPlayer::notifyDriverSeekComplete() {
2140 if (mDriver != NULL) {
2141 sp<NuPlayerDriver> driver = mDriver.promote();
2142 if (driver != NULL) {
2143 driver->notifySeekComplete();
Chong Zhangf8d71772014-11-26 15:08:34 -08002144 }
Chong Zhang7137ec72014-11-12 16:41:05 -08002145 }
2146}
2147
Andreas Huber9575c962013-02-05 13:59:56 -08002148void NuPlayer::onSourceNotify(const sp<AMessage> &msg) {
2149 int32_t what;
2150 CHECK(msg->findInt32("what", &what));
2151
2152 switch (what) {
Lajos Molnarfcd3e942015-03-31 10:06:48 -07002153 case Source::kWhatInstantiateSecureDecoders:
2154 {
2155 if (mSource == NULL) {
2156 // This is a stale notification from a source that was
2157 // asynchronously preparing when the client called reset().
2158 // We handled the reset, the source is gone.
2159 break;
2160 }
2161
2162 sp<AMessage> reply;
2163 CHECK(msg->findMessage("reply", &reply));
2164 status_t err = onInstantiateSecureDecoders();
2165 reply->setInt32("err", err);
2166 reply->post();
2167 break;
2168 }
2169
Andreas Huber9575c962013-02-05 13:59:56 -08002170 case Source::kWhatPrepared:
2171 {
Andreas Huberb5f28d42013-04-25 15:11:19 -07002172 if (mSource == NULL) {
2173 // This is a stale notification from a source that was
2174 // asynchronously preparing when the client called reset().
2175 // We handled the reset, the source is gone.
2176 break;
2177 }
2178
Andreas Huberec0c5972013-02-05 14:47:13 -08002179 int32_t err;
2180 CHECK(msg->findInt32("err", &err));
2181
Lajos Molnarfcd3e942015-03-31 10:06:48 -07002182 if (err != OK) {
2183 // shut down potential secure codecs in case client never calls reset
2184 mDeferredActions.push_back(
2185 new FlushDecoderAction(FLUSH_CMD_SHUTDOWN /* audio */,
2186 FLUSH_CMD_SHUTDOWN /* video */));
2187 processDeferredActions();
Wei Jia8a092d32016-06-03 14:57:24 -07002188 } else {
2189 mPrepared = true;
Lajos Molnarfcd3e942015-03-31 10:06:48 -07002190 }
2191
Andreas Huber9575c962013-02-05 13:59:56 -08002192 sp<NuPlayerDriver> driver = mDriver.promote();
2193 if (driver != NULL) {
Marco Nelissendd114d12014-05-28 15:23:14 -07002194 // notify duration first, so that it's definitely set when
2195 // the app received the "prepare complete" callback.
2196 int64_t durationUs;
2197 if (mSource->getDuration(&durationUs) == OK) {
2198 driver->notifyDuration(durationUs);
2199 }
Andreas Huberec0c5972013-02-05 14:47:13 -08002200 driver->notifyPrepareCompleted(err);
Andreas Huber9575c962013-02-05 13:59:56 -08002201 }
Andreas Huber99759402013-04-01 14:28:31 -07002202
Andreas Huber9575c962013-02-05 13:59:56 -08002203 break;
2204 }
2205
2206 case Source::kWhatFlagsChanged:
2207 {
2208 uint32_t flags;
2209 CHECK(msg->findInt32("flags", (int32_t *)&flags));
2210
Chong Zhang4b7069d2013-09-11 12:52:43 -07002211 sp<NuPlayerDriver> driver = mDriver.promote();
2212 if (driver != NULL) {
Wei Jia895651b2014-12-10 17:31:52 -08002213 if ((flags & NuPlayer::Source::FLAG_CAN_SEEK) == 0) {
2214 driver->notifyListener(
2215 MEDIA_INFO, MEDIA_INFO_NOT_SEEKABLE, 0);
2216 }
Chong Zhang4b7069d2013-09-11 12:52:43 -07002217 driver->notifyFlagsChanged(flags);
2218 }
2219
Andreas Huber9575c962013-02-05 13:59:56 -08002220 if ((mSourceFlags & Source::FLAG_DYNAMIC_DURATION)
2221 && (!(flags & Source::FLAG_DYNAMIC_DURATION))) {
2222 cancelPollDuration();
2223 } else if (!(mSourceFlags & Source::FLAG_DYNAMIC_DURATION)
2224 && (flags & Source::FLAG_DYNAMIC_DURATION)
2225 && (mAudioDecoder != NULL || mVideoDecoder != NULL)) {
2226 schedulePollDuration();
2227 }
2228
2229 mSourceFlags = flags;
2230 break;
2231 }
2232
2233 case Source::kWhatVideoSizeChanged:
2234 {
Chong Zhangced1c2f2014-08-08 15:22:35 -07002235 sp<AMessage> format;
2236 CHECK(msg->findMessage("format", &format));
Andreas Huber9575c962013-02-05 13:59:56 -08002237
Chong Zhangced1c2f2014-08-08 15:22:35 -07002238 updateVideoSize(format);
Andreas Huber9575c962013-02-05 13:59:56 -08002239 break;
2240 }
2241
Chong Zhang2a3cc9a2014-08-21 17:48:26 -07002242 case Source::kWhatBufferingUpdate:
2243 {
2244 int32_t percentage;
2245 CHECK(msg->findInt32("percentage", &percentage));
2246
2247 notifyListener(MEDIA_BUFFERING_UPDATE, percentage, 0);
2248 break;
2249 }
2250
Chong Zhangefbb6192015-01-30 17:13:27 -08002251 case Source::kWhatPauseOnBufferingStart:
2252 {
2253 // ignore if not playing
Chong Zhang8a048332015-05-06 15:16:28 -07002254 if (mStarted) {
Chong Zhangefbb6192015-01-30 17:13:27 -08002255 ALOGI("buffer low, pausing...");
2256
Chong Zhang8a048332015-05-06 15:16:28 -07002257 mPausedForBuffering = true;
Chong Zhangefbb6192015-01-30 17:13:27 -08002258 onPause();
2259 }
Wei Jiabfe82072016-05-20 09:21:50 -07002260 notifyListener(MEDIA_INFO, MEDIA_INFO_BUFFERING_START, 0);
Roger Jönssonb50e83e2013-01-21 16:26:41 +01002261 break;
2262 }
2263
Chong Zhangefbb6192015-01-30 17:13:27 -08002264 case Source::kWhatResumeOnBufferingEnd:
2265 {
2266 // ignore if not playing
Chong Zhang8a048332015-05-06 15:16:28 -07002267 if (mStarted) {
Chong Zhangefbb6192015-01-30 17:13:27 -08002268 ALOGI("buffer ready, resuming...");
2269
Chong Zhang8a048332015-05-06 15:16:28 -07002270 mPausedForBuffering = false;
2271
2272 // do not resume yet if client didn't unpause
2273 if (!mPausedByClient) {
2274 onResume();
2275 }
Chong Zhangefbb6192015-01-30 17:13:27 -08002276 }
Wei Jia3bed45a2016-02-17 11:06:47 -08002277 notifyListener(MEDIA_INFO, MEDIA_INFO_BUFFERING_END, 0);
Roger Jönssonb50e83e2013-01-21 16:26:41 +01002278 break;
2279 }
2280
Chong Zhangefbb6192015-01-30 17:13:27 -08002281 case Source::kWhatCacheStats:
2282 {
2283 int32_t kbps;
2284 CHECK(msg->findInt32("bandwidth", &kbps));
2285
2286 notifyListener(MEDIA_INFO, MEDIA_INFO_NETWORK_BANDWIDTH, kbps);
2287 break;
2288 }
2289
Chong Zhangdcb89b32013-08-06 09:44:47 -07002290 case Source::kWhatSubtitleData:
2291 {
2292 sp<ABuffer> buffer;
2293 CHECK(msg->findBuffer("buffer", &buffer));
2294
Chong Zhang404fced2014-06-11 14:45:31 -07002295 sendSubtitleData(buffer, 0 /* baseIndex */);
Chong Zhangdcb89b32013-08-06 09:44:47 -07002296 break;
2297 }
2298
Robert Shih08528432015-04-08 09:06:54 -07002299 case Source::kWhatTimedMetaData:
2300 {
2301 sp<ABuffer> buffer;
2302 if (!msg->findBuffer("buffer", &buffer)) {
2303 notifyListener(MEDIA_INFO, MEDIA_INFO_METADATA_UPDATE, 0);
2304 } else {
2305 sendTimedMetaData(buffer);
2306 }
2307 break;
2308 }
2309
Robert Shihd3b0bbb2014-07-23 15:00:25 -07002310 case Source::kWhatTimedTextData:
2311 {
2312 int32_t generation;
2313 if (msg->findInt32("generation", &generation)
2314 && generation != mTimedTextGeneration) {
2315 break;
2316 }
2317
2318 sp<ABuffer> buffer;
2319 CHECK(msg->findBuffer("buffer", &buffer));
2320
2321 sp<NuPlayerDriver> driver = mDriver.promote();
2322 if (driver == NULL) {
2323 break;
2324 }
2325
2326 int posMs;
2327 int64_t timeUs, posUs;
2328 driver->getCurrentPosition(&posMs);
Patrik2 Carlsson24d484b2015-01-27 16:49:45 +01002329 posUs = (int64_t) posMs * 1000ll;
Robert Shihd3b0bbb2014-07-23 15:00:25 -07002330 CHECK(buffer->meta()->findInt64("timeUs", &timeUs));
2331
2332 if (posUs < timeUs) {
2333 if (!msg->findInt32("generation", &generation)) {
2334 msg->setInt32("generation", mTimedTextGeneration);
2335 }
2336 msg->post(timeUs - posUs);
2337 } else {
2338 sendTimedTextData(buffer);
2339 }
2340 break;
2341 }
2342
Andreas Huber14f76722013-01-15 09:04:18 -08002343 case Source::kWhatQueueDecoderShutdown:
2344 {
2345 int32_t audio, video;
2346 CHECK(msg->findInt32("audio", &audio));
2347 CHECK(msg->findInt32("video", &video));
2348
2349 sp<AMessage> reply;
2350 CHECK(msg->findMessage("reply", &reply));
2351
2352 queueDecoderShutdown(audio, video, reply);
2353 break;
2354 }
2355
Ronghua Wu80276872014-08-28 15:50:29 -07002356 case Source::kWhatDrmNoLicense:
2357 {
2358 notifyListener(MEDIA_ERROR, MEDIA_ERROR_UNKNOWN, ERROR_DRM_NO_LICENSE);
2359 break;
2360 }
2361
Andreas Huber9575c962013-02-05 13:59:56 -08002362 default:
2363 TRESPASS();
2364 }
2365}
2366
Chong Zhanga7fa1d92014-06-11 14:49:23 -07002367void NuPlayer::onClosedCaptionNotify(const sp<AMessage> &msg) {
2368 int32_t what;
2369 CHECK(msg->findInt32("what", &what));
2370
2371 switch (what) {
2372 case NuPlayer::CCDecoder::kWhatClosedCaptionData:
2373 {
2374 sp<ABuffer> buffer;
2375 CHECK(msg->findBuffer("buffer", &buffer));
2376
2377 size_t inbandTracks = 0;
2378 if (mSource != NULL) {
2379 inbandTracks = mSource->getTrackCount();
2380 }
2381
2382 sendSubtitleData(buffer, inbandTracks);
2383 break;
2384 }
2385
2386 case NuPlayer::CCDecoder::kWhatTrackAdded:
2387 {
2388 notifyListener(MEDIA_INFO, MEDIA_INFO_METADATA_UPDATE, 0);
2389
2390 break;
2391 }
2392
2393 default:
2394 TRESPASS();
2395 }
2396
2397
2398}
2399
Chong Zhang404fced2014-06-11 14:45:31 -07002400void NuPlayer::sendSubtitleData(const sp<ABuffer> &buffer, int32_t baseIndex) {
2401 int32_t trackIndex;
2402 int64_t timeUs, durationUs;
2403 CHECK(buffer->meta()->findInt32("trackIndex", &trackIndex));
2404 CHECK(buffer->meta()->findInt64("timeUs", &timeUs));
2405 CHECK(buffer->meta()->findInt64("durationUs", &durationUs));
2406
2407 Parcel in;
2408 in.writeInt32(trackIndex + baseIndex);
2409 in.writeInt64(timeUs);
2410 in.writeInt64(durationUs);
2411 in.writeInt32(buffer->size());
2412 in.writeInt32(buffer->size());
2413 in.write(buffer->data(), buffer->size());
2414
2415 notifyListener(MEDIA_SUBTITLE_DATA, 0, 0, &in);
2416}
Robert Shihd3b0bbb2014-07-23 15:00:25 -07002417
Robert Shih08528432015-04-08 09:06:54 -07002418void NuPlayer::sendTimedMetaData(const sp<ABuffer> &buffer) {
2419 int64_t timeUs;
2420 CHECK(buffer->meta()->findInt64("timeUs", &timeUs));
2421
2422 Parcel in;
2423 in.writeInt64(timeUs);
2424 in.writeInt32(buffer->size());
2425 in.writeInt32(buffer->size());
2426 in.write(buffer->data(), buffer->size());
2427
2428 notifyListener(MEDIA_META_DATA, 0, 0, &in);
2429}
2430
Robert Shihd3b0bbb2014-07-23 15:00:25 -07002431void NuPlayer::sendTimedTextData(const sp<ABuffer> &buffer) {
2432 const void *data;
2433 size_t size = 0;
2434 int64_t timeUs;
Marco Nelissen55e2f4c2015-09-04 15:57:15 -07002435 int32_t flag = TextDescriptions::IN_BAND_TEXT_3GPP;
Robert Shihd3b0bbb2014-07-23 15:00:25 -07002436
2437 AString mime;
2438 CHECK(buffer->meta()->findString("mime", &mime));
2439 CHECK(strcasecmp(mime.c_str(), MEDIA_MIMETYPE_TEXT_3GPP) == 0);
2440
2441 data = buffer->data();
2442 size = buffer->size();
2443
2444 Parcel parcel;
2445 if (size > 0) {
2446 CHECK(buffer->meta()->findInt64("timeUs", &timeUs));
Marco Nelissen55e2f4c2015-09-04 15:57:15 -07002447 int32_t global = 0;
2448 if (buffer->meta()->findInt32("global", &global) && global) {
2449 flag |= TextDescriptions::GLOBAL_DESCRIPTIONS;
2450 } else {
2451 flag |= TextDescriptions::LOCAL_DESCRIPTIONS;
2452 }
Robert Shihd3b0bbb2014-07-23 15:00:25 -07002453 TextDescriptions::getParcelOfDescriptions(
2454 (const uint8_t *)data, size, flag, timeUs / 1000, &parcel);
2455 }
2456
2457 if ((parcel.dataSize() > 0)) {
2458 notifyListener(MEDIA_TIMED_TEXT, 0, 0, &parcel);
2459 } else { // send an empty timed text
2460 notifyListener(MEDIA_TIMED_TEXT, 0, 0);
2461 }
2462}
Andreas Huberb5f25f02013-02-05 10:14:26 -08002463////////////////////////////////////////////////////////////////////////////////
2464
Chong Zhangced1c2f2014-08-08 15:22:35 -07002465sp<AMessage> NuPlayer::Source::getFormat(bool audio) {
2466 sp<MetaData> meta = getFormatMeta(audio);
2467
2468 if (meta == NULL) {
2469 return NULL;
2470 }
2471
2472 sp<AMessage> msg = new AMessage;
2473
2474 if(convertMetaDataToMessage(meta, &msg) == OK) {
2475 return msg;
2476 }
2477 return NULL;
2478}
2479
Andreas Huber9575c962013-02-05 13:59:56 -08002480void NuPlayer::Source::notifyFlagsChanged(uint32_t flags) {
2481 sp<AMessage> notify = dupNotify();
2482 notify->setInt32("what", kWhatFlagsChanged);
2483 notify->setInt32("flags", flags);
2484 notify->post();
2485}
2486
Chong Zhangced1c2f2014-08-08 15:22:35 -07002487void NuPlayer::Source::notifyVideoSizeChanged(const sp<AMessage> &format) {
Andreas Huber9575c962013-02-05 13:59:56 -08002488 sp<AMessage> notify = dupNotify();
2489 notify->setInt32("what", kWhatVideoSizeChanged);
Chong Zhangced1c2f2014-08-08 15:22:35 -07002490 notify->setMessage("format", format);
Andreas Huber9575c962013-02-05 13:59:56 -08002491 notify->post();
2492}
2493
Andreas Huberec0c5972013-02-05 14:47:13 -08002494void NuPlayer::Source::notifyPrepared(status_t err) {
Andreas Huber9575c962013-02-05 13:59:56 -08002495 sp<AMessage> notify = dupNotify();
2496 notify->setInt32("what", kWhatPrepared);
Andreas Huberec0c5972013-02-05 14:47:13 -08002497 notify->setInt32("err", err);
Andreas Huber9575c962013-02-05 13:59:56 -08002498 notify->post();
2499}
2500
Lajos Molnarfcd3e942015-03-31 10:06:48 -07002501void NuPlayer::Source::notifyInstantiateSecureDecoders(const sp<AMessage> &reply) {
2502 sp<AMessage> notify = dupNotify();
2503 notify->setInt32("what", kWhatInstantiateSecureDecoders);
2504 notify->setMessage("reply", reply);
2505 notify->post();
2506}
2507
Andreas Huber84333e02014-02-07 15:36:10 -08002508void NuPlayer::Source::onMessageReceived(const sp<AMessage> & /* msg */) {
Andreas Huberb5f25f02013-02-05 10:14:26 -08002509 TRESPASS();
2510}
2511
Andreas Huberf9334412010-12-15 15:17:42 -08002512} // namespace android