blob: a26d5253314d864c03fedfaf517ec5634acd2c4f [file] [log] [blame]
Andreas Huberf9334412010-12-15 15:17:42 -08001/*
2 * Copyright (C) 2010 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17//#define LOG_NDEBUG 0
18#define LOG_TAG "NuPlayer"
Ray Essickd4d00612017-01-03 09:36:27 -080019
20#include <inttypes.h>
21
Andreas Huberf9334412010-12-15 15:17:42 -080022#include <utils/Log.h>
23
24#include "NuPlayer.h"
Andreas Huber5bc087c2010-12-23 10:27:40 -080025
26#include "HTTPLiveSource.h"
Chong Zhang7137ec72014-11-12 16:41:05 -080027#include "NuPlayerCCDecoder.h"
Andreas Huberf9334412010-12-15 15:17:42 -080028#include "NuPlayerDecoder.h"
Chong Zhang7137ec72014-11-12 16:41:05 -080029#include "NuPlayerDecoderBase.h"
Wei Jiabc2fb722014-07-08 16:37:57 -070030#include "NuPlayerDecoderPassThrough.h"
Andreas Huber43c3e6c2011-01-05 12:17:08 -080031#include "NuPlayerDriver.h"
Andreas Huberf9334412010-12-15 15:17:42 -080032#include "NuPlayerRenderer.h"
Andreas Huber5bc087c2010-12-23 10:27:40 -080033#include "NuPlayerSource.h"
Andreas Huber2bfdd422011-10-11 15:24:07 -070034#include "RTSPSource.h"
Andreas Huber5bc087c2010-12-23 10:27:40 -080035#include "StreamingSource.h"
Andreas Huberafed0e12011-09-20 15:39:58 -070036#include "GenericSource.h"
Robert Shihd3b0bbb2014-07-23 15:00:25 -070037#include "TextDescriptions.h"
Andreas Huber5bc087c2010-12-23 10:27:40 -080038
39#include "ATSParser.h"
Andreas Huberf9334412010-12-15 15:17:42 -080040
Lajos Molnard9fd6312014-11-06 11:00:00 -080041#include <cutils/properties.h>
42
Lajos Molnar3a474aa2015-04-24 17:10:07 -070043#include <media/AudioResamplerPublic.h>
44#include <media/AVSyncSettings.h>
Wonsik Kim7e34bf52016-08-23 00:09:18 +090045#include <media/MediaCodecBuffer.h>
Lajos Molnar3a474aa2015-04-24 17:10:07 -070046
Andreas Huber3831a062010-12-21 10:22:33 -080047#include <media/stagefright/foundation/hexdump.h>
Andreas Huberf9334412010-12-15 15:17:42 -080048#include <media/stagefright/foundation/ABuffer.h>
49#include <media/stagefright/foundation/ADebug.h>
50#include <media/stagefright/foundation/AMessage.h>
Dongwon Kangd91dc5a2017-10-10 00:07:09 -070051#include <media/stagefright/foundation/avc_utils.h>
Lajos Molnar09524832014-07-17 14:29:51 -070052#include <media/stagefright/MediaBuffer.h>
Wei Jia0a68f662017-08-30 18:01:26 -070053#include <media/stagefright/MediaClock.h>
Andreas Huber3fe62152011-09-16 15:09:22 -070054#include <media/stagefright/MediaDefs.h>
Andreas Huberf9334412010-12-15 15:17:42 -080055#include <media/stagefright/MediaErrors.h>
56#include <media/stagefright/MetaData.h>
Lajos Molnar1de1e252015-04-30 18:18:34 -070057
Andy McFadden8ba01022012-12-18 09:46:54 -080058#include <gui/IGraphicBufferProducer.h>
Lajos Molnar1de1e252015-04-30 18:18:34 -070059#include <gui/Surface.h>
Andreas Huberf9334412010-12-15 15:17:42 -080060
Andreas Huber3fe62152011-09-16 15:09:22 -070061
Andreas Huber84066782011-08-16 09:34:26 -070062#include "ESDS.h"
63#include <media/stagefright/Utils.h>
64
Andreas Huberf9334412010-12-15 15:17:42 -080065namespace android {
66
Andreas Hubera1f8ab02012-11-30 10:53:22 -080067struct NuPlayer::Action : public RefBase {
68 Action() {}
69
70 virtual void execute(NuPlayer *player) = 0;
71
72private:
73 DISALLOW_EVIL_CONSTRUCTORS(Action);
74};
75
76struct NuPlayer::SeekAction : public Action {
Wei Jiac5de0912016-11-18 10:22:14 -080077 explicit SeekAction(int64_t seekTimeUs, MediaPlayerSeekMode mode)
Wei Jia14486822016-11-02 17:51:30 -070078 : mSeekTimeUs(seekTimeUs),
Wei Jiac5de0912016-11-18 10:22:14 -080079 mMode(mode) {
Andreas Hubera1f8ab02012-11-30 10:53:22 -080080 }
81
82 virtual void execute(NuPlayer *player) {
Wei Jiac5de0912016-11-18 10:22:14 -080083 player->performSeek(mSeekTimeUs, mMode);
Andreas Hubera1f8ab02012-11-30 10:53:22 -080084 }
85
86private:
87 int64_t mSeekTimeUs;
Wei Jiac5de0912016-11-18 10:22:14 -080088 MediaPlayerSeekMode mMode;
Andreas Hubera1f8ab02012-11-30 10:53:22 -080089
90 DISALLOW_EVIL_CONSTRUCTORS(SeekAction);
91};
92
Chong Zhangf8d71772014-11-26 15:08:34 -080093struct NuPlayer::ResumeDecoderAction : public Action {
Chih-Hung Hsieh090ef602016-04-27 10:39:54 -070094 explicit ResumeDecoderAction(bool needNotify)
Chong Zhangf8d71772014-11-26 15:08:34 -080095 : mNeedNotify(needNotify) {
96 }
97
98 virtual void execute(NuPlayer *player) {
99 player->performResumeDecoders(mNeedNotify);
100 }
101
102private:
103 bool mNeedNotify;
104
105 DISALLOW_EVIL_CONSTRUCTORS(ResumeDecoderAction);
106};
107
Andreas Huber57a339c2012-12-03 11:18:00 -0800108struct NuPlayer::SetSurfaceAction : public Action {
Chih-Hung Hsieh090ef602016-04-27 10:39:54 -0700109 explicit SetSurfaceAction(const sp<Surface> &surface)
Lajos Molnar1de1e252015-04-30 18:18:34 -0700110 : mSurface(surface) {
Andreas Huber57a339c2012-12-03 11:18:00 -0800111 }
112
113 virtual void execute(NuPlayer *player) {
Lajos Molnar1de1e252015-04-30 18:18:34 -0700114 player->performSetSurface(mSurface);
Andreas Huber57a339c2012-12-03 11:18:00 -0800115 }
116
117private:
Lajos Molnar1de1e252015-04-30 18:18:34 -0700118 sp<Surface> mSurface;
Andreas Huber57a339c2012-12-03 11:18:00 -0800119
120 DISALLOW_EVIL_CONSTRUCTORS(SetSurfaceAction);
121};
122
Wei Jiafef808d2014-10-31 17:57:05 -0700123struct NuPlayer::FlushDecoderAction : public Action {
124 FlushDecoderAction(FlushCommand audio, FlushCommand video)
Andreas Huber14f76722013-01-15 09:04:18 -0800125 : mAudio(audio),
126 mVideo(video) {
127 }
128
129 virtual void execute(NuPlayer *player) {
Wei Jiafef808d2014-10-31 17:57:05 -0700130 player->performDecoderFlush(mAudio, mVideo);
Andreas Huber14f76722013-01-15 09:04:18 -0800131 }
132
133private:
Wei Jiafef808d2014-10-31 17:57:05 -0700134 FlushCommand mAudio;
135 FlushCommand mVideo;
Andreas Huber14f76722013-01-15 09:04:18 -0800136
Wei Jiafef808d2014-10-31 17:57:05 -0700137 DISALLOW_EVIL_CONSTRUCTORS(FlushDecoderAction);
Andreas Huber14f76722013-01-15 09:04:18 -0800138};
139
140struct NuPlayer::PostMessageAction : public Action {
Chih-Hung Hsieh090ef602016-04-27 10:39:54 -0700141 explicit PostMessageAction(const sp<AMessage> &msg)
Andreas Huber14f76722013-01-15 09:04:18 -0800142 : mMessage(msg) {
143 }
144
145 virtual void execute(NuPlayer *) {
146 mMessage->post();
147 }
148
149private:
150 sp<AMessage> mMessage;
151
152 DISALLOW_EVIL_CONSTRUCTORS(PostMessageAction);
153};
154
Andreas Hubera1f8ab02012-11-30 10:53:22 -0800155// Use this if there's no state necessary to save in order to execute
156// the action.
157struct NuPlayer::SimpleAction : public Action {
158 typedef void (NuPlayer::*ActionFunc)();
159
Chih-Hung Hsieh090ef602016-04-27 10:39:54 -0700160 explicit SimpleAction(ActionFunc func)
Andreas Hubera1f8ab02012-11-30 10:53:22 -0800161 : mFunc(func) {
162 }
163
164 virtual void execute(NuPlayer *player) {
165 (player->*mFunc)();
166 }
167
168private:
169 ActionFunc mFunc;
170
171 DISALLOW_EVIL_CONSTRUCTORS(SimpleAction);
172};
173
Andreas Huberf9334412010-12-15 15:17:42 -0800174////////////////////////////////////////////////////////////////////////////////
175
Wei Jia0a68f662017-08-30 18:01:26 -0700176NuPlayer::NuPlayer(pid_t pid, const sp<MediaClock> &mediaClock)
Andreas Huber9b80c2b2011-06-30 15:47:02 -0700177 : mUIDValid(false),
Ronghua Wu68845c12015-07-21 09:50:48 -0700178 mPID(pid),
Wei Jia0a68f662017-08-30 18:01:26 -0700179 mMediaClock(mediaClock),
Andreas Huber9575c962013-02-05 13:59:56 -0800180 mSourceFlags(0),
Wei Jiabc2fb722014-07-08 16:37:57 -0700181 mOffloadAudio(false),
Wei Jia88703c32014-08-06 11:24:07 -0700182 mAudioDecoderGeneration(0),
183 mVideoDecoderGeneration(0),
Wei Jia57568df2014-09-22 10:16:29 -0700184 mRendererGeneration(0),
Robert Shih1a5c8592015-08-04 18:07:44 -0700185 mPreviousSeekTimeUs(0),
Andreas Huber9b80c2b2011-06-30 15:47:02 -0700186 mAudioEOS(false),
Andreas Huberf9334412010-12-15 15:17:42 -0800187 mVideoEOS(false),
Andreas Huber5bc087c2010-12-23 10:27:40 -0800188 mScanSourcesPending(false),
Andreas Huber1aef2112011-01-04 14:01:29 -0800189 mScanSourcesGeneration(0),
Andreas Huberb7c8e912012-11-27 15:02:53 -0800190 mPollDurationGeneration(0),
Robert Shihd3b0bbb2014-07-23 15:00:25 -0700191 mTimedTextGeneration(0),
Andreas Huberf9334412010-12-15 15:17:42 -0800192 mFlushingAudio(NONE),
Andreas Huber1aef2112011-01-04 14:01:29 -0800193 mFlushingVideo(NONE),
Chong Zhangf8d71772014-11-26 15:08:34 -0800194 mResumePending(false),
Andreas Huber57a339c2012-12-03 11:18:00 -0800195 mVideoScalingMode(NATIVE_WINDOW_SCALING_MODE_SCALE_TO_WINDOW),
Lajos Molnar3a474aa2015-04-24 17:10:07 -0700196 mPlaybackSettings(AUDIO_PLAYBACK_RATE_DEFAULT),
197 mVideoFpsHint(-1.f),
Chong Zhangefbb6192015-01-30 17:13:27 -0800198 mStarted(false),
Wei Jia8a092d32016-06-03 14:57:24 -0700199 mPrepared(false),
Ronghua Wu64c2d172015-10-07 16:52:19 -0700200 mResetting(false),
Robert Shih0c61a0d2015-07-06 15:09:10 -0700201 mSourceStarted(false),
Wei Jia686e8e52017-04-03 14:08:01 -0700202 mAudioDecoderError(false),
203 mVideoDecoderError(false),
Chong Zhangefbb6192015-01-30 17:13:27 -0800204 mPaused(false),
Wei Jia71c75e02016-02-04 09:40:47 -0800205 mPausedByClient(true),
Hassan Shojania50b20c92017-02-16 18:28:58 -0800206 mPausedForBuffering(false),
Hassan Shojaniaff63de72017-04-26 15:10:42 -0700207 mIsDrmProtected(false),
208 mDataSourceType(DATA_SOURCE_TYPE_NONE) {
Wei Jia0a68f662017-08-30 18:01:26 -0700209 CHECK(mediaClock != NULL);
Andy Hung8d121d42014-10-03 09:53:53 -0700210 clearFlushComplete();
Andreas Huberf9334412010-12-15 15:17:42 -0800211}
212
213NuPlayer::~NuPlayer() {
214}
215
Andreas Huber9b80c2b2011-06-30 15:47:02 -0700216void NuPlayer::setUID(uid_t uid) {
217 mUIDValid = true;
218 mUID = uid;
219}
220
Andreas Huber43c3e6c2011-01-05 12:17:08 -0800221void NuPlayer::setDriver(const wp<NuPlayerDriver> &driver) {
222 mDriver = driver;
Andreas Huberf9334412010-12-15 15:17:42 -0800223}
224
Andreas Huber9575c962013-02-05 13:59:56 -0800225void NuPlayer::setDataSourceAsync(const sp<IStreamSource> &source) {
Lajos Molnar1d15ab52015-03-04 16:46:34 -0800226 sp<AMessage> msg = new AMessage(kWhatSetDataSource, this);
Andreas Huberf9334412010-12-15 15:17:42 -0800227
Lajos Molnar1d15ab52015-03-04 16:46:34 -0800228 sp<AMessage> notify = new AMessage(kWhatSourceNotify, this);
Andreas Huberb5f25f02013-02-05 10:14:26 -0800229
Andreas Huber240abcc2014-02-13 13:32:37 -0800230 msg->setObject("source", new StreamingSource(notify, source));
Andreas Huber5bc087c2010-12-23 10:27:40 -0800231 msg->post();
Hassan Shojaniaff63de72017-04-26 15:10:42 -0700232 mDataSourceType = DATA_SOURCE_TYPE_STREAM;
Andreas Huber5bc087c2010-12-23 10:27:40 -0800233}
Andreas Huberf9334412010-12-15 15:17:42 -0800234
Andreas Huberafed0e12011-09-20 15:39:58 -0700235static bool IsHTTPLiveURL(const char *url) {
236 if (!strncasecmp("http://", url, 7)
Andreas Huber99759402013-04-01 14:28:31 -0700237 || !strncasecmp("https://", url, 8)
238 || !strncasecmp("file://", url, 7)) {
Andreas Huberafed0e12011-09-20 15:39:58 -0700239 size_t len = strlen(url);
240 if (len >= 5 && !strcasecmp(".m3u8", &url[len - 5])) {
241 return true;
242 }
243
244 if (strstr(url,"m3u8")) {
245 return true;
246 }
247 }
248
249 return false;
250}
251
Andreas Huber9575c962013-02-05 13:59:56 -0800252void NuPlayer::setDataSourceAsync(
Andreas Huber1b86fe02014-01-29 11:13:26 -0800253 const sp<IMediaHTTPService> &httpService,
254 const char *url,
255 const KeyedVector<String8, String8> *headers) {
Chong Zhang3de157d2014-08-05 20:54:44 -0700256
Lajos Molnar1d15ab52015-03-04 16:46:34 -0800257 sp<AMessage> msg = new AMessage(kWhatSetDataSource, this);
Oscar Rydhé7a33b772012-02-20 10:15:48 +0100258 size_t len = strlen(url);
Andreas Huber5bc087c2010-12-23 10:27:40 -0800259
Lajos Molnar1d15ab52015-03-04 16:46:34 -0800260 sp<AMessage> notify = new AMessage(kWhatSourceNotify, this);
Andreas Huberb5f25f02013-02-05 10:14:26 -0800261
Andreas Huberafed0e12011-09-20 15:39:58 -0700262 sp<Source> source;
263 if (IsHTTPLiveURL(url)) {
Andreas Huber81e68442014-02-05 11:52:33 -0800264 source = new HTTPLiveSource(notify, httpService, url, headers);
Hassan Shojaniacefac142017-02-06 21:02:02 -0800265 ALOGV("setDataSourceAsync HTTPLiveSource %s", url);
Hassan Shojaniaff63de72017-04-26 15:10:42 -0700266 mDataSourceType = DATA_SOURCE_TYPE_HTTP_LIVE;
Andreas Huberafed0e12011-09-20 15:39:58 -0700267 } else if (!strncasecmp(url, "rtsp://", 7)) {
Andreas Huber1b86fe02014-01-29 11:13:26 -0800268 source = new RTSPSource(
269 notify, httpService, url, headers, mUIDValid, mUID);
Hassan Shojaniacefac142017-02-06 21:02:02 -0800270 ALOGV("setDataSourceAsync RTSPSource %s", url);
Hassan Shojaniaff63de72017-04-26 15:10:42 -0700271 mDataSourceType = DATA_SOURCE_TYPE_RTSP;
Oscar Rydhé7a33b772012-02-20 10:15:48 +0100272 } else if ((!strncasecmp(url, "http://", 7)
273 || !strncasecmp(url, "https://", 8))
274 && ((len >= 4 && !strcasecmp(".sdp", &url[len - 4]))
275 || strstr(url, ".sdp?"))) {
Andreas Huber1b86fe02014-01-29 11:13:26 -0800276 source = new RTSPSource(
277 notify, httpService, url, headers, mUIDValid, mUID, true);
Hassan Shojaniacefac142017-02-06 21:02:02 -0800278 ALOGV("setDataSourceAsync RTSPSource http/https/.sdp %s", url);
Hassan Shojaniaff63de72017-04-26 15:10:42 -0700279 mDataSourceType = DATA_SOURCE_TYPE_RTSP;
Andreas Huber2bfdd422011-10-11 15:24:07 -0700280 } else {
Hassan Shojaniacefac142017-02-06 21:02:02 -0800281 ALOGV("setDataSourceAsync GenericSource %s", url);
282
Chong Zhang3de157d2014-08-05 20:54:44 -0700283 sp<GenericSource> genericSource =
Wei Jia992c5592017-09-01 14:20:23 -0700284 new GenericSource(notify, mUIDValid, mUID, mMediaClock);
Andreas Huber2bfdd422011-10-11 15:24:07 -0700285
Chong Zhanga19f33e2014-08-07 15:35:07 -0700286 status_t err = genericSource->setDataSource(httpService, url, headers);
Chong Zhang3de157d2014-08-05 20:54:44 -0700287
288 if (err == OK) {
289 source = genericSource;
290 } else {
Chong Zhanga19f33e2014-08-07 15:35:07 -0700291 ALOGE("Failed to set data source!");
Chong Zhang3de157d2014-08-05 20:54:44 -0700292 }
Hassan Shojaniaff63de72017-04-26 15:10:42 -0700293
294 // regardless of success/failure
295 mDataSourceType = DATA_SOURCE_TYPE_GENERIC_URL;
Chong Zhang3de157d2014-08-05 20:54:44 -0700296 }
Andreas Huberafed0e12011-09-20 15:39:58 -0700297 msg->setObject("source", source);
298 msg->post();
299}
300
Andreas Huber9575c962013-02-05 13:59:56 -0800301void NuPlayer::setDataSourceAsync(int fd, int64_t offset, int64_t length) {
Lajos Molnar1d15ab52015-03-04 16:46:34 -0800302 sp<AMessage> msg = new AMessage(kWhatSetDataSource, this);
Andreas Huberafed0e12011-09-20 15:39:58 -0700303
Lajos Molnar1d15ab52015-03-04 16:46:34 -0800304 sp<AMessage> notify = new AMessage(kWhatSourceNotify, this);
Andreas Huberb5f25f02013-02-05 10:14:26 -0800305
Chong Zhang3de157d2014-08-05 20:54:44 -0700306 sp<GenericSource> source =
Wei Jia992c5592017-09-01 14:20:23 -0700307 new GenericSource(notify, mUIDValid, mUID, mMediaClock);
Chong Zhang3de157d2014-08-05 20:54:44 -0700308
Hassan Shojaniacefac142017-02-06 21:02:02 -0800309 ALOGV("setDataSourceAsync fd %d/%lld/%lld source: %p",
310 fd, (long long)offset, (long long)length, source.get());
311
Chong Zhanga19f33e2014-08-07 15:35:07 -0700312 status_t err = source->setDataSource(fd, offset, length);
Chong Zhang3de157d2014-08-05 20:54:44 -0700313
314 if (err != OK) {
Chong Zhanga19f33e2014-08-07 15:35:07 -0700315 ALOGE("Failed to set data source!");
Chong Zhang3de157d2014-08-05 20:54:44 -0700316 source = NULL;
317 }
318
Andreas Huberafed0e12011-09-20 15:39:58 -0700319 msg->setObject("source", source);
Andreas Huberf9334412010-12-15 15:17:42 -0800320 msg->post();
Hassan Shojaniaff63de72017-04-26 15:10:42 -0700321 mDataSourceType = DATA_SOURCE_TYPE_GENERIC_FD;
Andreas Huberf9334412010-12-15 15:17:42 -0800322}
323
Chris Watkins99f31602015-03-20 13:06:33 -0700324void NuPlayer::setDataSourceAsync(const sp<DataSource> &dataSource) {
325 sp<AMessage> msg = new AMessage(kWhatSetDataSource, this);
326 sp<AMessage> notify = new AMessage(kWhatSourceNotify, this);
327
Wei Jia992c5592017-09-01 14:20:23 -0700328 sp<GenericSource> source = new GenericSource(notify, mUIDValid, mUID, mMediaClock);
Chris Watkins99f31602015-03-20 13:06:33 -0700329 status_t err = source->setDataSource(dataSource);
330
331 if (err != OK) {
332 ALOGE("Failed to set data source!");
333 source = NULL;
334 }
335
336 msg->setObject("source", source);
337 msg->post();
Hassan Shojaniaff63de72017-04-26 15:10:42 -0700338 mDataSourceType = DATA_SOURCE_TYPE_MEDIA;
Chris Watkins99f31602015-03-20 13:06:33 -0700339}
340
Wei Jia9bb38032017-03-23 18:00:38 -0700341status_t NuPlayer::getBufferingSettings(
Wei Jia48fa06d2016-12-20 15:30:49 -0800342 BufferingSettings *buffering /* nonnull */) {
Wei Jia9bb38032017-03-23 18:00:38 -0700343 sp<AMessage> msg = new AMessage(kWhatGetBufferingSettings, this);
Wei Jia48fa06d2016-12-20 15:30:49 -0800344 sp<AMessage> response;
345 status_t err = msg->postAndAwaitResponse(&response);
346 if (err == OK && response != NULL) {
347 CHECK(response->findInt32("err", &err));
348 if (err == OK) {
349 readFromAMessage(response, buffering);
350 }
351 }
352 return err;
353}
354
355status_t NuPlayer::setBufferingSettings(const BufferingSettings& buffering) {
356 sp<AMessage> msg = new AMessage(kWhatSetBufferingSettings, this);
357 writeToAMessage(msg, buffering);
358 sp<AMessage> response;
359 status_t err = msg->postAndAwaitResponse(&response);
360 if (err == OK && response != NULL) {
361 CHECK(response->findInt32("err", &err));
362 }
363 return err;
364}
365
Andreas Huber9575c962013-02-05 13:59:56 -0800366void NuPlayer::prepareAsync() {
Hassan Shojaniacefac142017-02-06 21:02:02 -0800367 ALOGV("prepareAsync");
368
Lajos Molnar1d15ab52015-03-04 16:46:34 -0800369 (new AMessage(kWhatPrepare, this))->post();
Andreas Huber9575c962013-02-05 13:59:56 -0800370}
371
Andreas Huber57a339c2012-12-03 11:18:00 -0800372void NuPlayer::setVideoSurfaceTextureAsync(
Andy McFadden8ba01022012-12-18 09:46:54 -0800373 const sp<IGraphicBufferProducer> &bufferProducer) {
Lajos Molnar1de1e252015-04-30 18:18:34 -0700374 sp<AMessage> msg = new AMessage(kWhatSetVideoSurface, this);
Andreas Huber57a339c2012-12-03 11:18:00 -0800375
Andy McFadden8ba01022012-12-18 09:46:54 -0800376 if (bufferProducer == NULL) {
Lajos Molnar1de1e252015-04-30 18:18:34 -0700377 msg->setObject("surface", NULL);
Andreas Huber57a339c2012-12-03 11:18:00 -0800378 } else {
Lajos Molnar1de1e252015-04-30 18:18:34 -0700379 msg->setObject("surface", new Surface(bufferProducer, true /* controlledByApp */));
Andreas Huber57a339c2012-12-03 11:18:00 -0800380 }
381
Andreas Huberf9334412010-12-15 15:17:42 -0800382 msg->post();
383}
384
385void NuPlayer::setAudioSink(const sp<MediaPlayerBase::AudioSink> &sink) {
Lajos Molnar1d15ab52015-03-04 16:46:34 -0800386 sp<AMessage> msg = new AMessage(kWhatSetAudioSink, this);
Andreas Huberf9334412010-12-15 15:17:42 -0800387 msg->setObject("sink", sink);
388 msg->post();
389}
390
391void NuPlayer::start() {
Lajos Molnar1d15ab52015-03-04 16:46:34 -0800392 (new AMessage(kWhatStart, this))->post();
Andreas Huberf9334412010-12-15 15:17:42 -0800393}
394
Lajos Molnar3a474aa2015-04-24 17:10:07 -0700395status_t NuPlayer::setPlaybackSettings(const AudioPlaybackRate &rate) {
396 // do some cursory validation of the settings here. audio modes are
397 // only validated when set on the audiosink.
398 if ((rate.mSpeed != 0.f && rate.mSpeed < AUDIO_TIMESTRETCH_SPEED_MIN)
399 || rate.mSpeed > AUDIO_TIMESTRETCH_SPEED_MAX
400 || rate.mPitch < AUDIO_TIMESTRETCH_SPEED_MIN
401 || rate.mPitch > AUDIO_TIMESTRETCH_SPEED_MAX) {
402 return BAD_VALUE;
403 }
404 sp<AMessage> msg = new AMessage(kWhatConfigPlayback, this);
405 writeToAMessage(msg, rate);
406 sp<AMessage> response;
407 status_t err = msg->postAndAwaitResponse(&response);
408 if (err == OK && response != NULL) {
409 CHECK(response->findInt32("err", &err));
410 }
411 return err;
412}
413
414status_t NuPlayer::getPlaybackSettings(AudioPlaybackRate *rate /* nonnull */) {
415 sp<AMessage> msg = new AMessage(kWhatGetPlaybackSettings, this);
416 sp<AMessage> response;
417 status_t err = msg->postAndAwaitResponse(&response);
418 if (err == OK && response != NULL) {
419 CHECK(response->findInt32("err", &err));
420 if (err == OK) {
421 readFromAMessage(response, rate);
422 }
423 }
424 return err;
425}
426
427status_t NuPlayer::setSyncSettings(const AVSyncSettings &sync, float videoFpsHint) {
428 sp<AMessage> msg = new AMessage(kWhatConfigSync, this);
429 writeToAMessage(msg, sync, videoFpsHint);
430 sp<AMessage> response;
431 status_t err = msg->postAndAwaitResponse(&response);
432 if (err == OK && response != NULL) {
433 CHECK(response->findInt32("err", &err));
434 }
435 return err;
436}
437
438status_t NuPlayer::getSyncSettings(
439 AVSyncSettings *sync /* nonnull */, float *videoFps /* nonnull */) {
440 sp<AMessage> msg = new AMessage(kWhatGetSyncSettings, this);
441 sp<AMessage> response;
442 status_t err = msg->postAndAwaitResponse(&response);
443 if (err == OK && response != NULL) {
444 CHECK(response->findInt32("err", &err));
445 if (err == OK) {
446 readFromAMessage(response, sync, videoFps);
447 }
448 }
449 return err;
Wei Jia98160162015-02-04 17:01:11 -0800450}
451
Andreas Huber43c3e6c2011-01-05 12:17:08 -0800452void NuPlayer::pause() {
Lajos Molnar1d15ab52015-03-04 16:46:34 -0800453 (new AMessage(kWhatPause, this))->post();
Andreas Huber43c3e6c2011-01-05 12:17:08 -0800454}
455
Andreas Huber1aef2112011-01-04 14:01:29 -0800456void NuPlayer::resetAsync() {
Wei Jiac45a4b22016-04-15 15:30:23 -0700457 sp<Source> source;
458 {
459 Mutex::Autolock autoLock(mSourceLock);
460 source = mSource;
461 }
462
463 if (source != NULL) {
Chong Zhang48296b72014-09-14 14:28:45 -0700464 // During a reset, the data source might be unresponsive already, we need to
465 // disconnect explicitly so that reads exit promptly.
466 // We can't queue the disconnect request to the looper, as it might be
467 // queued behind a stuck read and never gets processed.
468 // Doing a disconnect outside the looper to allows the pending reads to exit
469 // (either successfully or with error).
Wei Jiac45a4b22016-04-15 15:30:23 -0700470 source->disconnect();
Chong Zhang48296b72014-09-14 14:28:45 -0700471 }
472
Lajos Molnar1d15ab52015-03-04 16:46:34 -0800473 (new AMessage(kWhatReset, this))->post();
Andreas Huber1aef2112011-01-04 14:01:29 -0800474}
475
Wei Jia52c28512017-09-13 18:17:51 -0700476status_t NuPlayer::notifyAt(int64_t mediaTimeUs) {
477 sp<AMessage> notify = new AMessage(kWhatNotifyTime, this);
478 notify->setInt64("timerUs", mediaTimeUs);
479 mMediaClock->addTimer(notify, mediaTimeUs);
480 return OK;
481}
482
Wei Jiac5de0912016-11-18 10:22:14 -0800483void NuPlayer::seekToAsync(int64_t seekTimeUs, MediaPlayerSeekMode mode, bool needNotify) {
Lajos Molnar1d15ab52015-03-04 16:46:34 -0800484 sp<AMessage> msg = new AMessage(kWhatSeek, this);
Andreas Huber43c3e6c2011-01-05 12:17:08 -0800485 msg->setInt64("seekTimeUs", seekTimeUs);
Wei Jiac5de0912016-11-18 10:22:14 -0800486 msg->setInt32("mode", mode);
Wei Jiae427abf2014-09-22 15:21:11 -0700487 msg->setInt32("needNotify", needNotify);
Andreas Huber43c3e6c2011-01-05 12:17:08 -0800488 msg->post();
489}
490
Andreas Huber53df1a42010-12-22 10:03:04 -0800491
Chong Zhang404fced2014-06-11 14:45:31 -0700492void NuPlayer::writeTrackInfo(
Chih-Hung Hsiehe964d4e2016-08-09 14:31:32 -0700493 Parcel* reply, const sp<AMessage>& format) const {
Marco Nelissenc367ca12015-09-15 09:51:59 -0700494 if (format == NULL) {
495 ALOGE("NULL format");
496 return;
497 }
Chong Zhang404fced2014-06-11 14:45:31 -0700498 int32_t trackType;
Marco Nelissen9436e482015-09-15 10:21:53 -0700499 if (!format->findInt32("type", &trackType)) {
Marco Nelissenc367ca12015-09-15 09:51:59 -0700500 ALOGE("no track type");
501 return;
502 }
Chong Zhang404fced2014-06-11 14:45:31 -0700503
Robert Shih755106e2015-04-30 14:36:45 -0700504 AString mime;
Robert Shih2e3a4252015-05-06 10:21:15 -0700505 if (!format->findString("mime", &mime)) {
506 // Java MediaPlayer only uses mimetype for subtitle and timedtext tracks.
507 // If we can't find the mimetype here it means that we wouldn't be needing
508 // the mimetype on the Java end. We still write a placeholder mime to keep the
509 // (de)serialization logic simple.
510 if (trackType == MEDIA_TRACK_TYPE_AUDIO) {
511 mime = "audio/";
512 } else if (trackType == MEDIA_TRACK_TYPE_VIDEO) {
513 mime = "video/";
514 } else {
Marco Nelissenc367ca12015-09-15 09:51:59 -0700515 ALOGE("unknown track type: %d", trackType);
516 return;
Robert Shih2e3a4252015-05-06 10:21:15 -0700517 }
518 }
Robert Shih755106e2015-04-30 14:36:45 -0700519
Chong Zhang404fced2014-06-11 14:45:31 -0700520 AString lang;
Marco Nelissen9436e482015-09-15 10:21:53 -0700521 if (!format->findString("language", &lang)) {
Marco Nelissenc367ca12015-09-15 09:51:59 -0700522 ALOGE("no language");
523 return;
524 }
Chong Zhang404fced2014-06-11 14:45:31 -0700525
526 reply->writeInt32(2); // write something non-zero
527 reply->writeInt32(trackType);
Robert Shih755106e2015-04-30 14:36:45 -0700528 reply->writeString16(String16(mime.c_str()));
Chong Zhang404fced2014-06-11 14:45:31 -0700529 reply->writeString16(String16(lang.c_str()));
530
531 if (trackType == MEDIA_TRACK_TYPE_SUBTITLE) {
Chong Zhang404fced2014-06-11 14:45:31 -0700532 int32_t isAuto, isDefault, isForced;
533 CHECK(format->findInt32("auto", &isAuto));
534 CHECK(format->findInt32("default", &isDefault));
535 CHECK(format->findInt32("forced", &isForced));
536
Chong Zhang404fced2014-06-11 14:45:31 -0700537 reply->writeInt32(isAuto);
538 reply->writeInt32(isDefault);
539 reply->writeInt32(isForced);
540 }
541}
542
Andreas Huberf9334412010-12-15 15:17:42 -0800543void NuPlayer::onMessageReceived(const sp<AMessage> &msg) {
544 switch (msg->what()) {
545 case kWhatSetDataSource:
546 {
Steve Block3856b092011-10-20 11:56:00 +0100547 ALOGV("kWhatSetDataSource");
Andreas Huberf9334412010-12-15 15:17:42 -0800548
549 CHECK(mSource == NULL);
550
Chong Zhang3de157d2014-08-05 20:54:44 -0700551 status_t err = OK;
Andreas Huber5bc087c2010-12-23 10:27:40 -0800552 sp<RefBase> obj;
553 CHECK(msg->findObject("source", &obj));
Chong Zhang3de157d2014-08-05 20:54:44 -0700554 if (obj != NULL) {
Wei Jiac45a4b22016-04-15 15:30:23 -0700555 Mutex::Autolock autoLock(mSourceLock);
Chong Zhang3de157d2014-08-05 20:54:44 -0700556 mSource = static_cast<Source *>(obj.get());
Chong Zhang3de157d2014-08-05 20:54:44 -0700557 } else {
558 err = UNKNOWN_ERROR;
559 }
Andreas Huber9575c962013-02-05 13:59:56 -0800560
561 CHECK(mDriver != NULL);
562 sp<NuPlayerDriver> driver = mDriver.promote();
563 if (driver != NULL) {
Chong Zhang3de157d2014-08-05 20:54:44 -0700564 driver->notifySetDataSourceCompleted(err);
Andreas Huber9575c962013-02-05 13:59:56 -0800565 }
566 break;
567 }
568
Wei Jia9bb38032017-03-23 18:00:38 -0700569 case kWhatGetBufferingSettings:
Wei Jia48fa06d2016-12-20 15:30:49 -0800570 {
571 sp<AReplyToken> replyID;
572 CHECK(msg->senderAwaitsResponse(&replyID));
573
Wei Jia9bb38032017-03-23 18:00:38 -0700574 ALOGV("kWhatGetBufferingSettings");
Wei Jia48fa06d2016-12-20 15:30:49 -0800575 BufferingSettings buffering;
576 status_t err = OK;
577 if (mSource != NULL) {
Wei Jia9bb38032017-03-23 18:00:38 -0700578 err = mSource->getBufferingSettings(&buffering);
Wei Jia48fa06d2016-12-20 15:30:49 -0800579 } else {
580 err = INVALID_OPERATION;
581 }
582 sp<AMessage> response = new AMessage;
583 if (err == OK) {
584 writeToAMessage(response, buffering);
585 }
586 response->setInt32("err", err);
587 response->postReply(replyID);
588 break;
589 }
590
591 case kWhatSetBufferingSettings:
592 {
593 sp<AReplyToken> replyID;
594 CHECK(msg->senderAwaitsResponse(&replyID));
595
596 ALOGV("kWhatSetBufferingSettings");
597 BufferingSettings buffering;
598 readFromAMessage(msg, &buffering);
599 status_t err = OK;
600 if (mSource != NULL) {
601 err = mSource->setBufferingSettings(buffering);
602 } else {
603 err = INVALID_OPERATION;
604 }
605 sp<AMessage> response = new AMessage;
606 response->setInt32("err", err);
607 response->postReply(replyID);
608 break;
609 }
610
Andreas Huber9575c962013-02-05 13:59:56 -0800611 case kWhatPrepare:
612 {
Hassan Shojaniacefac142017-02-06 21:02:02 -0800613 ALOGV("onMessageReceived kWhatPrepare");
614
Andreas Huber9575c962013-02-05 13:59:56 -0800615 mSource->prepareAsync();
Andreas Huberf9334412010-12-15 15:17:42 -0800616 break;
617 }
618
Chong Zhangdcb89b32013-08-06 09:44:47 -0700619 case kWhatGetTrackInfo:
620 {
Lajos Molnar3f274362015-03-05 14:35:41 -0800621 sp<AReplyToken> replyID;
Chong Zhangdcb89b32013-08-06 09:44:47 -0700622 CHECK(msg->senderAwaitsResponse(&replyID));
623
Chong Zhang404fced2014-06-11 14:45:31 -0700624 Parcel* reply;
625 CHECK(msg->findPointer("reply", (void**)&reply));
626
627 size_t inbandTracks = 0;
Chong Zhangdcb89b32013-08-06 09:44:47 -0700628 if (mSource != NULL) {
Chong Zhang404fced2014-06-11 14:45:31 -0700629 inbandTracks = mSource->getTrackCount();
630 }
631
Chong Zhanga7fa1d92014-06-11 14:49:23 -0700632 size_t ccTracks = 0;
633 if (mCCDecoder != NULL) {
634 ccTracks = mCCDecoder->getTrackCount();
635 }
636
Chong Zhang404fced2014-06-11 14:45:31 -0700637 // total track count
Chong Zhanga7fa1d92014-06-11 14:49:23 -0700638 reply->writeInt32(inbandTracks + ccTracks);
Chong Zhang404fced2014-06-11 14:45:31 -0700639
640 // write inband tracks
641 for (size_t i = 0; i < inbandTracks; ++i) {
642 writeTrackInfo(reply, mSource->getTrackInfo(i));
Chong Zhangdcb89b32013-08-06 09:44:47 -0700643 }
644
Chong Zhanga7fa1d92014-06-11 14:49:23 -0700645 // write CC track
646 for (size_t i = 0; i < ccTracks; ++i) {
647 writeTrackInfo(reply, mCCDecoder->getTrackInfo(i));
648 }
649
Chong Zhangdcb89b32013-08-06 09:44:47 -0700650 sp<AMessage> response = new AMessage;
Chong Zhangdcb89b32013-08-06 09:44:47 -0700651 response->postReply(replyID);
652 break;
653 }
654
Robert Shih7c4f0d72014-07-09 18:53:31 -0700655 case kWhatGetSelectedTrack:
656 {
657 status_t err = INVALID_OPERATION;
658 if (mSource != NULL) {
659 err = OK;
660
661 int32_t type32;
662 CHECK(msg->findInt32("type", (int32_t*)&type32));
663 media_track_type type = (media_track_type)type32;
664 ssize_t selectedTrack = mSource->getSelectedTrack(type);
665
666 Parcel* reply;
667 CHECK(msg->findPointer("reply", (void**)&reply));
668 reply->writeInt32(selectedTrack);
669 }
670
671 sp<AMessage> response = new AMessage;
672 response->setInt32("err", err);
673
Lajos Molnar3f274362015-03-05 14:35:41 -0800674 sp<AReplyToken> replyID;
Robert Shih7c4f0d72014-07-09 18:53:31 -0700675 CHECK(msg->senderAwaitsResponse(&replyID));
676 response->postReply(replyID);
677 break;
678 }
679
Chong Zhangdcb89b32013-08-06 09:44:47 -0700680 case kWhatSelectTrack:
681 {
Lajos Molnar3f274362015-03-05 14:35:41 -0800682 sp<AReplyToken> replyID;
Chong Zhangdcb89b32013-08-06 09:44:47 -0700683 CHECK(msg->senderAwaitsResponse(&replyID));
684
Chong Zhang404fced2014-06-11 14:45:31 -0700685 size_t trackIndex;
686 int32_t select;
Robert Shih6ffb1fd2014-10-29 16:24:32 -0700687 int64_t timeUs;
Chong Zhang404fced2014-06-11 14:45:31 -0700688 CHECK(msg->findSize("trackIndex", &trackIndex));
689 CHECK(msg->findInt32("select", &select));
Robert Shih6ffb1fd2014-10-29 16:24:32 -0700690 CHECK(msg->findInt64("timeUs", &timeUs));
Chong Zhang404fced2014-06-11 14:45:31 -0700691
Chong Zhangdcb89b32013-08-06 09:44:47 -0700692 status_t err = INVALID_OPERATION;
Chong Zhang404fced2014-06-11 14:45:31 -0700693
694 size_t inbandTracks = 0;
Chong Zhangdcb89b32013-08-06 09:44:47 -0700695 if (mSource != NULL) {
Chong Zhang404fced2014-06-11 14:45:31 -0700696 inbandTracks = mSource->getTrackCount();
697 }
Chong Zhanga7fa1d92014-06-11 14:49:23 -0700698 size_t ccTracks = 0;
699 if (mCCDecoder != NULL) {
700 ccTracks = mCCDecoder->getTrackCount();
701 }
Chong Zhang404fced2014-06-11 14:45:31 -0700702
703 if (trackIndex < inbandTracks) {
Robert Shih6ffb1fd2014-10-29 16:24:32 -0700704 err = mSource->selectTrack(trackIndex, select, timeUs);
Robert Shihd3b0bbb2014-07-23 15:00:25 -0700705
706 if (!select && err == OK) {
707 int32_t type;
708 sp<AMessage> info = mSource->getTrackInfo(trackIndex);
709 if (info != NULL
710 && info->findInt32("type", &type)
711 && type == MEDIA_TRACK_TYPE_TIMEDTEXT) {
712 ++mTimedTextGeneration;
713 }
714 }
Chong Zhanga7fa1d92014-06-11 14:49:23 -0700715 } else {
716 trackIndex -= inbandTracks;
717
718 if (trackIndex < ccTracks) {
719 err = mCCDecoder->selectTrack(trackIndex, select);
720 }
Chong Zhangdcb89b32013-08-06 09:44:47 -0700721 }
722
723 sp<AMessage> response = new AMessage;
724 response->setInt32("err", err);
725
726 response->postReply(replyID);
727 break;
728 }
729
Andreas Huberb7c8e912012-11-27 15:02:53 -0800730 case kWhatPollDuration:
731 {
732 int32_t generation;
733 CHECK(msg->findInt32("generation", &generation));
734
735 if (generation != mPollDurationGeneration) {
736 // stale
737 break;
738 }
739
740 int64_t durationUs;
741 if (mDriver != NULL && mSource->getDuration(&durationUs) == OK) {
742 sp<NuPlayerDriver> driver = mDriver.promote();
743 if (driver != NULL) {
744 driver->notifyDuration(durationUs);
745 }
746 }
747
748 msg->post(1000000ll); // poll again in a second.
749 break;
750 }
751
Lajos Molnar1de1e252015-04-30 18:18:34 -0700752 case kWhatSetVideoSurface:
Andreas Huberf9334412010-12-15 15:17:42 -0800753 {
Andreas Huberf9334412010-12-15 15:17:42 -0800754
755 sp<RefBase> obj;
Lajos Molnar1de1e252015-04-30 18:18:34 -0700756 CHECK(msg->findObject("surface", &obj));
757 sp<Surface> surface = static_cast<Surface *>(obj.get());
Lajos Molnara81c6222015-07-10 19:17:45 -0700758
759 ALOGD("onSetVideoSurface(%p, %s video decoder)",
760 surface.get(),
Wei Jia6b7d2ed2015-08-10 15:11:54 -0700761 (mSource != NULL && mStarted && mSource->getFormat(false /* audio */) != NULL
Lajos Molnara81c6222015-07-10 19:17:45 -0700762 && mVideoDecoder != NULL) ? "have" : "no");
763
Wei Jia6b7d2ed2015-08-10 15:11:54 -0700764 // Need to check mStarted before calling mSource->getFormat because NuPlayer might
765 // be in preparing state and it could take long time.
766 // When mStarted is true, mSource must have been set.
767 if (mSource == NULL || !mStarted || mSource->getFormat(false /* audio */) == NULL
Lajos Molnara81c6222015-07-10 19:17:45 -0700768 // NOTE: mVideoDecoder's mSurface is always non-null
769 || (mVideoDecoder != NULL && mVideoDecoder->setVideoSurface(surface) == OK)) {
Lajos Molnar1de1e252015-04-30 18:18:34 -0700770 performSetSurface(surface);
Wei Jiafef808d2014-10-31 17:57:05 -0700771 break;
772 }
773
774 mDeferredActions.push_back(
Krystian Turczyn687a5892016-01-20 14:20:35 +0100775 new FlushDecoderAction(
776 (obj != NULL ? FLUSH_CMD_FLUSH : FLUSH_CMD_NONE) /* audio */,
Wei Jiafef808d2014-10-31 17:57:05 -0700777 FLUSH_CMD_SHUTDOWN /* video */));
778
Lajos Molnar1de1e252015-04-30 18:18:34 -0700779 mDeferredActions.push_back(new SetSurfaceAction(surface));
James Dong0d268a32012-08-31 12:18:27 -0700780
Krystian Turczyn687a5892016-01-20 14:20:35 +0100781 if (obj != NULL) {
Wei Jiafef808d2014-10-31 17:57:05 -0700782 if (mStarted) {
Andy Hung73535852014-09-05 11:42:58 -0700783 // Issue a seek to refresh the video screen only if started otherwise
784 // the extractor may not yet be started and will assert.
785 // If the video decoder is not set (perhaps audio only in this case)
786 // do not perform a seek as it is not needed.
Ronghua Wua73d9e02014-10-08 15:13:29 -0700787 int64_t currentPositionUs = 0;
788 if (getCurrentPosition(&currentPositionUs) == OK) {
789 mDeferredActions.push_back(
Wei Jiac5de0912016-11-18 10:22:14 -0800790 new SeekAction(currentPositionUs,
791 MediaPlayerSeekMode::SEEK_PREVIOUS_SYNC /* mode */));
Ronghua Wua73d9e02014-10-08 15:13:29 -0700792 }
Andy Hung73535852014-09-05 11:42:58 -0700793 }
Wei Jiaac428aa2014-09-02 19:01:34 -0700794
Andreas Huber57a339c2012-12-03 11:18:00 -0800795 // If there is a new surface texture, instantiate decoders
796 // again if possible.
797 mDeferredActions.push_back(
798 new SimpleAction(&NuPlayer::performScanSources));
Andreas Huber57a339c2012-12-03 11:18:00 -0800799
Krystian Turczyn687a5892016-01-20 14:20:35 +0100800 // After a flush without shutdown, decoder is paused.
801 // Don't resume it until source seek is done, otherwise it could
802 // start pulling stale data too soon.
803 mDeferredActions.push_back(
804 new ResumeDecoderAction(false /* needNotify */));
805 }
Chong Zhang7137ec72014-11-12 16:41:05 -0800806
Andreas Huber57a339c2012-12-03 11:18:00 -0800807 processDeferredActions();
Andreas Huberf9334412010-12-15 15:17:42 -0800808 break;
809 }
810
811 case kWhatSetAudioSink:
812 {
Steve Block3856b092011-10-20 11:56:00 +0100813 ALOGV("kWhatSetAudioSink");
Andreas Huberf9334412010-12-15 15:17:42 -0800814
815 sp<RefBase> obj;
816 CHECK(msg->findObject("sink", &obj));
817
818 mAudioSink = static_cast<MediaPlayerBase::AudioSink *>(obj.get());
819 break;
820 }
821
822 case kWhatStart:
823 {
Steve Block3856b092011-10-20 11:56:00 +0100824 ALOGV("kWhatStart");
Wei Jia94211742014-10-28 17:09:06 -0700825 if (mStarted) {
Chong Zhang8a048332015-05-06 15:16:28 -0700826 // do not resume yet if the source is still buffering
827 if (!mPausedForBuffering) {
828 onResume();
829 }
Wei Jia94211742014-10-28 17:09:06 -0700830 } else {
831 onStart();
Lajos Molnar09524832014-07-17 14:29:51 -0700832 }
Chong Zhangefbb6192015-01-30 17:13:27 -0800833 mPausedByClient = false;
Andreas Huberf9334412010-12-15 15:17:42 -0800834 break;
835 }
836
Lajos Molnar3a474aa2015-04-24 17:10:07 -0700837 case kWhatConfigPlayback:
Wei Jia98160162015-02-04 17:01:11 -0800838 {
Lajos Molnar3a474aa2015-04-24 17:10:07 -0700839 sp<AReplyToken> replyID;
840 CHECK(msg->senderAwaitsResponse(&replyID));
841 AudioPlaybackRate rate /* sanitized */;
842 readFromAMessage(msg, &rate);
843 status_t err = OK;
Wei Jia98160162015-02-04 17:01:11 -0800844 if (mRenderer != NULL) {
Wei Jiabf70feb2016-02-19 15:47:16 -0800845 // AudioSink allows only 1.f and 0.f for offload mode.
846 // For other speed, switch to non-offload mode.
847 if (mOffloadAudio && ((rate.mSpeed != 0.f && rate.mSpeed != 1.f)
848 || rate.mPitch != 1.f)) {
849 int64_t currentPositionUs;
850 if (getCurrentPosition(&currentPositionUs) != OK) {
851 currentPositionUs = mPreviousSeekTimeUs;
852 }
853
854 // Set mPlaybackSettings so that the new audio decoder can
855 // be created correctly.
856 mPlaybackSettings = rate;
857 if (!mPaused) {
858 mRenderer->pause();
859 }
Wei Jia5031b2f2016-02-25 11:19:31 -0800860 restartAudio(
Wei Jiabf70feb2016-02-19 15:47:16 -0800861 currentPositionUs, true /* forceNonOffload */,
862 true /* needsToCreateAudioDecoder */);
863 if (!mPaused) {
864 mRenderer->resume();
865 }
866 }
867
Lajos Molnar3a474aa2015-04-24 17:10:07 -0700868 err = mRenderer->setPlaybackSettings(rate);
869 }
870 if (err == OK) {
871 if (rate.mSpeed == 0.f) {
872 onPause();
Wei Jia351ce872016-02-10 13:21:59 -0800873 mPausedByClient = true;
Lajos Molnar3a474aa2015-04-24 17:10:07 -0700874 // save all other settings (using non-paused speed)
875 // so we can restore them on start
876 AudioPlaybackRate newRate = rate;
877 newRate.mSpeed = mPlaybackSettings.mSpeed;
878 mPlaybackSettings = newRate;
879 } else { /* rate.mSpeed != 0.f */
Lajos Molnar3a474aa2015-04-24 17:10:07 -0700880 mPlaybackSettings = rate;
Wei Jia8a092d32016-06-03 14:57:24 -0700881 if (mStarted) {
882 // do not resume yet if the source is still buffering
883 if (!mPausedForBuffering) {
884 onResume();
885 }
886 } else if (mPrepared) {
887 onStart();
888 }
889
890 mPausedByClient = false;
Lajos Molnar3a474aa2015-04-24 17:10:07 -0700891 }
Wei Jia98160162015-02-04 17:01:11 -0800892 }
Ronghua Wu8db88132015-04-22 13:51:35 -0700893
894 if (mVideoDecoder != NULL) {
Praveen Chavanbbaa1442016-04-08 13:33:49 -0700895 sp<AMessage> params = new AMessage();
896 params->setFloat("playback-speed", mPlaybackSettings.mSpeed);
897 mVideoDecoder->setParameters(params);
Ronghua Wu8db88132015-04-22 13:51:35 -0700898 }
899
Lajos Molnar3a474aa2015-04-24 17:10:07 -0700900 sp<AMessage> response = new AMessage;
901 response->setInt32("err", err);
902 response->postReply(replyID);
903 break;
904 }
905
906 case kWhatGetPlaybackSettings:
907 {
908 sp<AReplyToken> replyID;
909 CHECK(msg->senderAwaitsResponse(&replyID));
910 AudioPlaybackRate rate = mPlaybackSettings;
911 status_t err = OK;
912 if (mRenderer != NULL) {
913 err = mRenderer->getPlaybackSettings(&rate);
914 }
915 if (err == OK) {
916 // get playback settings used by renderer, as it may be
917 // slightly off due to audiosink not taking small changes.
918 mPlaybackSettings = rate;
919 if (mPaused) {
920 rate.mSpeed = 0.f;
921 }
922 }
923 sp<AMessage> response = new AMessage;
924 if (err == OK) {
925 writeToAMessage(response, rate);
926 }
927 response->setInt32("err", err);
928 response->postReply(replyID);
929 break;
930 }
931
932 case kWhatConfigSync:
933 {
934 sp<AReplyToken> replyID;
935 CHECK(msg->senderAwaitsResponse(&replyID));
936
937 ALOGV("kWhatConfigSync");
938 AVSyncSettings sync;
939 float videoFpsHint;
940 readFromAMessage(msg, &sync, &videoFpsHint);
941 status_t err = OK;
942 if (mRenderer != NULL) {
943 err = mRenderer->setSyncSettings(sync, videoFpsHint);
944 }
945 if (err == OK) {
946 mSyncSettings = sync;
947 mVideoFpsHint = videoFpsHint;
948 }
949 sp<AMessage> response = new AMessage;
950 response->setInt32("err", err);
951 response->postReply(replyID);
952 break;
953 }
954
955 case kWhatGetSyncSettings:
956 {
957 sp<AReplyToken> replyID;
958 CHECK(msg->senderAwaitsResponse(&replyID));
959 AVSyncSettings sync = mSyncSettings;
960 float videoFps = mVideoFpsHint;
961 status_t err = OK;
962 if (mRenderer != NULL) {
963 err = mRenderer->getSyncSettings(&sync, &videoFps);
964 if (err == OK) {
965 mSyncSettings = sync;
966 mVideoFpsHint = videoFps;
967 }
968 }
969 sp<AMessage> response = new AMessage;
970 if (err == OK) {
971 writeToAMessage(response, sync, videoFps);
972 }
973 response->setInt32("err", err);
974 response->postReply(replyID);
Wei Jia98160162015-02-04 17:01:11 -0800975 break;
976 }
977
Andreas Huberf9334412010-12-15 15:17:42 -0800978 case kWhatScanSources:
979 {
Andreas Huber1aef2112011-01-04 14:01:29 -0800980 int32_t generation;
981 CHECK(msg->findInt32("generation", &generation));
982 if (generation != mScanSourcesGeneration) {
983 // Drop obsolete msg.
984 break;
985 }
986
Andreas Huber5bc087c2010-12-23 10:27:40 -0800987 mScanSourcesPending = false;
988
Steve Block3856b092011-10-20 11:56:00 +0100989 ALOGV("scanning sources haveAudio=%d, haveVideo=%d",
Andreas Huber43c3e6c2011-01-05 12:17:08 -0800990 mAudioDecoder != NULL, mVideoDecoder != NULL);
991
Andreas Huberb7c8e912012-11-27 15:02:53 -0800992 bool mHadAnySourcesBefore =
993 (mAudioDecoder != NULL) || (mVideoDecoder != NULL);
Robert Shih7350b052015-10-01 15:50:14 -0700994 bool rescan = false;
Andreas Huberb7c8e912012-11-27 15:02:53 -0800995
Andy Hung282a7e32014-08-14 15:56:34 -0700996 // initialize video before audio because successful initialization of
997 // video may change deep buffer mode of audio.
Lajos Molnar1de1e252015-04-30 18:18:34 -0700998 if (mSurface != NULL) {
Robert Shih7350b052015-10-01 15:50:14 -0700999 if (instantiateDecoder(false, &mVideoDecoder) == -EWOULDBLOCK) {
1000 rescan = true;
1001 }
Haynes Mathew George5d246ef2012-07-09 10:36:57 -07001002 }
Andreas Huberf9334412010-12-15 15:17:42 -08001003
Ronghua Wua10fd232014-11-06 16:15:20 -08001004 // Don't try to re-open audio sink if there's an existing decoder.
1005 if (mAudioSink != NULL && mAudioDecoder == NULL) {
Robert Shih7350b052015-10-01 15:50:14 -07001006 if (instantiateDecoder(true, &mAudioDecoder) == -EWOULDBLOCK) {
1007 rescan = true;
1008 }
Andreas Huberf9334412010-12-15 15:17:42 -08001009 }
1010
Andreas Huberb7c8e912012-11-27 15:02:53 -08001011 if (!mHadAnySourcesBefore
1012 && (mAudioDecoder != NULL || mVideoDecoder != NULL)) {
1013 // This is the first time we've found anything playable.
1014
Andreas Huber9575c962013-02-05 13:59:56 -08001015 if (mSourceFlags & Source::FLAG_DYNAMIC_DURATION) {
Andreas Huberb7c8e912012-11-27 15:02:53 -08001016 schedulePollDuration();
1017 }
1018 }
1019
Andreas Hubereac68ba2011-09-27 12:12:25 -07001020 status_t err;
1021 if ((err = mSource->feedMoreTSData()) != OK) {
Andreas Huber1aef2112011-01-04 14:01:29 -08001022 if (mAudioDecoder == NULL && mVideoDecoder == NULL) {
1023 // We're not currently decoding anything (no audio or
1024 // video tracks found) and we just ran out of input data.
Andreas Hubereac68ba2011-09-27 12:12:25 -07001025
1026 if (err == ERROR_END_OF_STREAM) {
1027 notifyListener(MEDIA_PLAYBACK_COMPLETE, 0, 0);
1028 } else {
1029 notifyListener(MEDIA_ERROR, MEDIA_ERROR_UNKNOWN, err);
1030 }
Andreas Huber1aef2112011-01-04 14:01:29 -08001031 }
Andreas Huberf9334412010-12-15 15:17:42 -08001032 break;
1033 }
1034
Robert Shih7350b052015-10-01 15:50:14 -07001035 if (rescan) {
Andreas Huberf9334412010-12-15 15:17:42 -08001036 msg->post(100000ll);
Andreas Huber5bc087c2010-12-23 10:27:40 -08001037 mScanSourcesPending = true;
Andreas Huberf9334412010-12-15 15:17:42 -08001038 }
1039 break;
1040 }
1041
1042 case kWhatVideoNotify:
1043 case kWhatAudioNotify:
1044 {
1045 bool audio = msg->what() == kWhatAudioNotify;
1046
Wei Jia88703c32014-08-06 11:24:07 -07001047 int32_t currentDecoderGeneration =
1048 (audio? mAudioDecoderGeneration : mVideoDecoderGeneration);
1049 int32_t requesterGeneration = currentDecoderGeneration - 1;
1050 CHECK(msg->findInt32("generation", &requesterGeneration));
1051
1052 if (requesterGeneration != currentDecoderGeneration) {
1053 ALOGV("got message from old %s decoder, generation(%d:%d)",
1054 audio ? "audio" : "video", requesterGeneration,
1055 currentDecoderGeneration);
1056 sp<AMessage> reply;
1057 if (!(msg->findMessage("reply", &reply))) {
1058 return;
1059 }
1060
1061 reply->setInt32("err", INFO_DISCONTINUITY);
1062 reply->post();
1063 return;
1064 }
1065
Andreas Huberf9334412010-12-15 15:17:42 -08001066 int32_t what;
Lajos Molnar1cd13982014-01-17 15:12:51 -08001067 CHECK(msg->findInt32("what", &what));
Andreas Huberf9334412010-12-15 15:17:42 -08001068
Chong Zhang7137ec72014-11-12 16:41:05 -08001069 if (what == DecoderBase::kWhatInputDiscontinuity) {
1070 int32_t formatChange;
1071 CHECK(msg->findInt32("formatChange", &formatChange));
Andreas Huberf9334412010-12-15 15:17:42 -08001072
Chong Zhang7137ec72014-11-12 16:41:05 -08001073 ALOGV("%s discontinuity: formatChange %d",
1074 audio ? "audio" : "video", formatChange);
1075
1076 if (formatChange) {
1077 mDeferredActions.push_back(
1078 new FlushDecoderAction(
1079 audio ? FLUSH_CMD_SHUTDOWN : FLUSH_CMD_NONE,
1080 audio ? FLUSH_CMD_NONE : FLUSH_CMD_SHUTDOWN));
Andreas Huberf9334412010-12-15 15:17:42 -08001081 }
Chong Zhang7137ec72014-11-12 16:41:05 -08001082
1083 mDeferredActions.push_back(
1084 new SimpleAction(
1085 &NuPlayer::performScanSources));
1086
1087 processDeferredActions();
1088 } else if (what == DecoderBase::kWhatEOS) {
Andreas Huberdc9bacd2011-09-26 10:53:29 -07001089 int32_t err;
Lajos Molnar1cd13982014-01-17 15:12:51 -08001090 CHECK(msg->findInt32("err", &err));
Andreas Huberdc9bacd2011-09-26 10:53:29 -07001091
1092 if (err == ERROR_END_OF_STREAM) {
Steve Block3856b092011-10-20 11:56:00 +01001093 ALOGV("got %s decoder EOS", audio ? "audio" : "video");
Andreas Huberdc9bacd2011-09-26 10:53:29 -07001094 } else {
Steve Block3856b092011-10-20 11:56:00 +01001095 ALOGV("got %s decoder EOS w/ error %d",
Andreas Huberdc9bacd2011-09-26 10:53:29 -07001096 audio ? "audio" : "video",
1097 err);
1098 }
1099
1100 mRenderer->queueEOS(audio, err);
Chong Zhang7137ec72014-11-12 16:41:05 -08001101 } else if (what == DecoderBase::kWhatFlushCompleted) {
Steve Block3856b092011-10-20 11:56:00 +01001102 ALOGV("decoder %s flush completed", audio ? "audio" : "video");
Andreas Huberf9334412010-12-15 15:17:42 -08001103
Andy Hung8d121d42014-10-03 09:53:53 -07001104 handleFlushComplete(audio, true /* isDecoder */);
Andreas Huber3831a062010-12-21 10:22:33 -08001105 finishFlushIfPossible();
Chong Zhang7137ec72014-11-12 16:41:05 -08001106 } else if (what == DecoderBase::kWhatVideoSizeChanged) {
Lajos Molnar1cd13982014-01-17 15:12:51 -08001107 sp<AMessage> format;
1108 CHECK(msg->findMessage("format", &format));
1109
Wei Jiac6cfd702014-11-11 16:33:20 -08001110 sp<AMessage> inputFormat =
1111 mSource->getFormat(false /* audio */);
Andreas Huber3831a062010-12-21 10:22:33 -08001112
Bartosz Dolewski26936f72014-12-11 12:47:08 +01001113 setVideoScalingMode(mVideoScalingMode);
Wei Jiac6cfd702014-11-11 16:33:20 -08001114 updateVideoSize(inputFormat, format);
Chong Zhang7137ec72014-11-12 16:41:05 -08001115 } else if (what == DecoderBase::kWhatShutdownCompleted) {
Steve Block3856b092011-10-20 11:56:00 +01001116 ALOGV("%s shutdown completed", audio ? "audio" : "video");
Andreas Huber3831a062010-12-21 10:22:33 -08001117 if (audio) {
1118 mAudioDecoder.clear();
Wei Jia686e8e52017-04-03 14:08:01 -07001119 mAudioDecoderError = false;
Wei Jia57568df2014-09-22 10:16:29 -07001120 ++mAudioDecoderGeneration;
Andreas Huber3831a062010-12-21 10:22:33 -08001121
1122 CHECK_EQ((int)mFlushingAudio, (int)SHUTTING_DOWN_DECODER);
1123 mFlushingAudio = SHUT_DOWN;
1124 } else {
1125 mVideoDecoder.clear();
Wei Jia686e8e52017-04-03 14:08:01 -07001126 mVideoDecoderError = false;
Wei Jia57568df2014-09-22 10:16:29 -07001127 ++mVideoDecoderGeneration;
Andreas Huber3831a062010-12-21 10:22:33 -08001128
1129 CHECK_EQ((int)mFlushingVideo, (int)SHUTTING_DOWN_DECODER);
1130 mFlushingVideo = SHUT_DOWN;
1131 }
1132
1133 finishFlushIfPossible();
Chong Zhangf8d71772014-11-26 15:08:34 -08001134 } else if (what == DecoderBase::kWhatResumeCompleted) {
1135 finishResume();
Chong Zhang7137ec72014-11-12 16:41:05 -08001136 } else if (what == DecoderBase::kWhatError) {
Chong Zhangf4c0a942014-08-11 15:14:10 -07001137 status_t err;
Andy Hung2abde2c2014-09-30 14:40:32 -07001138 if (!msg->findInt32("err", &err) || err == OK) {
Chong Zhangf4c0a942014-08-11 15:14:10 -07001139 err = UNKNOWN_ERROR;
1140 }
Andy Hungcf31f1e2014-09-23 14:59:01 -07001141
Andy Hung2abde2c2014-09-30 14:40:32 -07001142 // Decoder errors can be due to Source (e.g. from streaming),
1143 // or from decoding corrupted bitstreams, or from other decoder
1144 // MediaCodec operations (e.g. from an ongoing reset or seek).
Andy Hung202bce12014-12-03 11:47:36 -08001145 // They may also be due to openAudioSink failure at
1146 // decoder start or after a format change.
Andy Hung2abde2c2014-09-30 14:40:32 -07001147 //
1148 // We try to gracefully shut down the affected decoder if possible,
1149 // rather than trying to force the shutdown with something
1150 // similar to performReset(). This method can lead to a hang
1151 // if MediaCodec functions block after an error, but they should
1152 // typically return INVALID_OPERATION instead of blocking.
1153
1154 FlushStatus *flushing = audio ? &mFlushingAudio : &mFlushingVideo;
1155 ALOGE("received error(%#x) from %s decoder, flushing(%d), now shutting down",
1156 err, audio ? "audio" : "video", *flushing);
1157
1158 switch (*flushing) {
1159 case NONE:
1160 mDeferredActions.push_back(
Wei Jiafef808d2014-10-31 17:57:05 -07001161 new FlushDecoderAction(
1162 audio ? FLUSH_CMD_SHUTDOWN : FLUSH_CMD_NONE,
1163 audio ? FLUSH_CMD_NONE : FLUSH_CMD_SHUTDOWN));
Andy Hung2abde2c2014-09-30 14:40:32 -07001164 processDeferredActions();
1165 break;
1166 case FLUSHING_DECODER:
1167 *flushing = FLUSHING_DECODER_SHUTDOWN; // initiate shutdown after flush.
1168 break; // Wait for flush to complete.
1169 case FLUSHING_DECODER_SHUTDOWN:
1170 break; // Wait for flush to complete.
1171 case SHUTTING_DOWN_DECODER:
1172 break; // Wait for shutdown to complete.
1173 case FLUSHED:
Andy Hung2abde2c2014-09-30 14:40:32 -07001174 getDecoder(audio)->initiateShutdown(); // In the middle of a seek.
1175 *flushing = SHUTTING_DOWN_DECODER; // Shut down.
1176 break;
1177 case SHUT_DOWN:
1178 finishFlushIfPossible(); // Should not occur.
1179 break; // Finish anyways.
Marco Nelissen9e2b7912014-08-18 16:13:03 -07001180 }
Wei Jia686e8e52017-04-03 14:08:01 -07001181 if (mSource != nullptr) {
1182 if (audio) {
Wei Jia2f6d8c52017-04-11 09:50:31 -07001183 if (mVideoDecoderError || mSource->getFormat(false /* audio */) == NULL
Wei Jiaf86731d2017-07-11 17:24:34 -07001184 || mSurface == NULL || mVideoDecoder == NULL) {
Wei Jia686e8e52017-04-03 14:08:01 -07001185 // When both audio and video have error, or this stream has only audio
1186 // which has error, notify client of error.
1187 notifyListener(MEDIA_ERROR, MEDIA_ERROR_UNKNOWN, err);
1188 } else {
1189 // Only audio track has error. Video track could be still good to play.
1190 notifyListener(MEDIA_INFO, MEDIA_INFO_PLAY_AUDIO_ERROR, err);
1191 }
1192 mAudioDecoderError = true;
1193 } else {
Wei Jia2f6d8c52017-04-11 09:50:31 -07001194 if (mAudioDecoderError || mSource->getFormat(true /* audio */) == NULL
Wei Jiaf86731d2017-07-11 17:24:34 -07001195 || mAudioSink == NULL || mAudioDecoder == NULL) {
Wei Jia686e8e52017-04-03 14:08:01 -07001196 // When both audio and video have error, or this stream has only video
1197 // which has error, notify client of error.
1198 notifyListener(MEDIA_ERROR, MEDIA_ERROR_UNKNOWN, err);
1199 } else {
1200 // Only video track has error. Audio track could be still good to play.
1201 notifyListener(MEDIA_INFO, MEDIA_INFO_PLAY_VIDEO_ERROR, err);
1202 }
1203 mVideoDecoderError = true;
1204 }
1205 }
Lajos Molnar1cd13982014-01-17 15:12:51 -08001206 } else {
1207 ALOGV("Unhandled decoder notification %d '%c%c%c%c'.",
Andreas Hubera1f8ab02012-11-30 10:53:22 -08001208 what,
1209 what >> 24,
1210 (what >> 16) & 0xff,
1211 (what >> 8) & 0xff,
1212 what & 0xff);
Andreas Huberf9334412010-12-15 15:17:42 -08001213 }
1214
1215 break;
1216 }
1217
1218 case kWhatRendererNotify:
1219 {
Wei Jia57568df2014-09-22 10:16:29 -07001220 int32_t requesterGeneration = mRendererGeneration - 1;
1221 CHECK(msg->findInt32("generation", &requesterGeneration));
1222 if (requesterGeneration != mRendererGeneration) {
1223 ALOGV("got message from old renderer, generation(%d:%d)",
1224 requesterGeneration, mRendererGeneration);
1225 return;
1226 }
1227
Andreas Huberf9334412010-12-15 15:17:42 -08001228 int32_t what;
1229 CHECK(msg->findInt32("what", &what));
1230
1231 if (what == Renderer::kWhatEOS) {
1232 int32_t audio;
1233 CHECK(msg->findInt32("audio", &audio));
1234
Andreas Huberc92fd242011-08-16 13:48:44 -07001235 int32_t finalResult;
1236 CHECK(msg->findInt32("finalResult", &finalResult));
1237
Andreas Huberf9334412010-12-15 15:17:42 -08001238 if (audio) {
1239 mAudioEOS = true;
1240 } else {
1241 mVideoEOS = true;
1242 }
1243
Andreas Huberc92fd242011-08-16 13:48:44 -07001244 if (finalResult == ERROR_END_OF_STREAM) {
Steve Block3856b092011-10-20 11:56:00 +01001245 ALOGV("reached %s EOS", audio ? "audio" : "video");
Andreas Huberc92fd242011-08-16 13:48:44 -07001246 } else {
Steve Block29357bc2012-01-06 19:20:56 +00001247 ALOGE("%s track encountered an error (%d)",
Andreas Huberc92fd242011-08-16 13:48:44 -07001248 audio ? "audio" : "video", finalResult);
1249
1250 notifyListener(
1251 MEDIA_ERROR, MEDIA_ERROR_UNKNOWN, finalResult);
1252 }
Andreas Huberf9334412010-12-15 15:17:42 -08001253
1254 if ((mAudioEOS || mAudioDecoder == NULL)
1255 && (mVideoEOS || mVideoDecoder == NULL)) {
1256 notifyListener(MEDIA_PLAYBACK_COMPLETE, 0, 0);
1257 }
Andreas Huber3fe62152011-09-16 15:09:22 -07001258 } else if (what == Renderer::kWhatFlushComplete) {
Andreas Huberf9334412010-12-15 15:17:42 -08001259 int32_t audio;
1260 CHECK(msg->findInt32("audio", &audio));
1261
Wei Jia3261f0d2015-10-08 13:58:33 -07001262 if (audio) {
1263 mAudioEOS = false;
1264 } else {
1265 mVideoEOS = false;
1266 }
1267
Steve Block3856b092011-10-20 11:56:00 +01001268 ALOGV("renderer %s flush completed.", audio ? "audio" : "video");
Wei Jiada4252f2015-07-14 18:15:28 -07001269 if (audio && (mFlushingAudio == NONE || mFlushingAudio == FLUSHED
1270 || mFlushingAudio == SHUT_DOWN)) {
1271 // Flush has been handled by tear down.
1272 break;
1273 }
Andy Hung8d121d42014-10-03 09:53:53 -07001274 handleFlushComplete(audio, false /* isDecoder */);
1275 finishFlushIfPossible();
James Dongf57b4ea2012-07-20 13:38:36 -07001276 } else if (what == Renderer::kWhatVideoRenderingStart) {
1277 notifyListener(MEDIA_INFO, MEDIA_INFO_RENDERING_START, 0);
Lajos Molnarcbaffcf2013-08-14 18:30:38 -07001278 } else if (what == Renderer::kWhatMediaRenderingStart) {
1279 ALOGV("media rendering started");
1280 notifyListener(MEDIA_STARTED, 0, 0);
Ronghua Wufaeb0f22015-05-21 12:20:21 -07001281 } else if (what == Renderer::kWhatAudioTearDown) {
Ronghua Wu08529172014-10-02 16:55:52 -07001282 int32_t reason;
1283 CHECK(msg->findInt32("reason", &reason));
Ronghua Wufaeb0f22015-05-21 12:20:21 -07001284 ALOGV("Tear down audio with reason %d.", reason);
Wei Jia8456ddd2016-04-22 14:46:43 -07001285 if (reason == Renderer::kDueToTimeout && !(mPaused && mOffloadAudio)) {
1286 // TimeoutWhenPaused is only for offload mode.
1287 ALOGW("Receive a stale message for teardown.");
1288 break;
1289 }
Wei Jiada4252f2015-07-14 18:15:28 -07001290 int64_t positionUs;
Robert Shih1a5c8592015-08-04 18:07:44 -07001291 if (!msg->findInt64("positionUs", &positionUs)) {
1292 positionUs = mPreviousSeekTimeUs;
1293 }
Ronghua Wufaeb0f22015-05-21 12:20:21 -07001294
Wei Jia5031b2f2016-02-25 11:19:31 -08001295 restartAudio(
Wei Jiaa05f1e32016-03-25 16:31:22 -07001296 positionUs, reason == Renderer::kForceNonOffload /* forceNonOffload */,
1297 reason != Renderer::kDueToTimeout /* needsToCreateAudioDecoder */);
Andreas Huberf9334412010-12-15 15:17:42 -08001298 }
1299 break;
1300 }
1301
1302 case kWhatMoreDataQueued:
1303 {
1304 break;
1305 }
1306
Andreas Huber1aef2112011-01-04 14:01:29 -08001307 case kWhatReset:
1308 {
Steve Block3856b092011-10-20 11:56:00 +01001309 ALOGV("kWhatReset");
Andreas Huber1aef2112011-01-04 14:01:29 -08001310
Ronghua Wu64c2d172015-10-07 16:52:19 -07001311 mResetting = true;
1312
Andreas Hubera1f8ab02012-11-30 10:53:22 -08001313 mDeferredActions.push_back(
Wei Jiafef808d2014-10-31 17:57:05 -07001314 new FlushDecoderAction(
1315 FLUSH_CMD_SHUTDOWN /* audio */,
1316 FLUSH_CMD_SHUTDOWN /* video */));
Andreas Huberb7c8e912012-11-27 15:02:53 -08001317
Andreas Hubera1f8ab02012-11-30 10:53:22 -08001318 mDeferredActions.push_back(
1319 new SimpleAction(&NuPlayer::performReset));
Andreas Huberb58ce9f2011-11-28 16:27:35 -08001320
Andreas Hubera1f8ab02012-11-30 10:53:22 -08001321 processDeferredActions();
Andreas Huber1aef2112011-01-04 14:01:29 -08001322 break;
1323 }
1324
Wei Jia52c28512017-09-13 18:17:51 -07001325 case kWhatNotifyTime:
1326 {
1327 ALOGV("kWhatNotifyTime");
1328 int64_t timerUs;
1329 CHECK(msg->findInt64("timerUs", &timerUs));
1330
1331 notifyListener(MEDIA_NOTIFY_TIME, timerUs, 0);
1332 break;
1333 }
1334
Andreas Huber43c3e6c2011-01-05 12:17:08 -08001335 case kWhatSeek:
1336 {
1337 int64_t seekTimeUs;
Wei Jiac5de0912016-11-18 10:22:14 -08001338 int32_t mode;
Wei Jiae427abf2014-09-22 15:21:11 -07001339 int32_t needNotify;
Andreas Huber43c3e6c2011-01-05 12:17:08 -08001340 CHECK(msg->findInt64("seekTimeUs", &seekTimeUs));
Wei Jiac5de0912016-11-18 10:22:14 -08001341 CHECK(msg->findInt32("mode", &mode));
Wei Jiae427abf2014-09-22 15:21:11 -07001342 CHECK(msg->findInt32("needNotify", &needNotify));
Andreas Huber43c3e6c2011-01-05 12:17:08 -08001343
Wei Jiac5de0912016-11-18 10:22:14 -08001344 ALOGV("kWhatSeek seekTimeUs=%lld us, mode=%d, needNotify=%d",
1345 (long long)seekTimeUs, mode, needNotify);
Andreas Huber43c3e6c2011-01-05 12:17:08 -08001346
Wei Jia1061c9c2015-05-19 16:02:17 -07001347 if (!mStarted) {
1348 // Seek before the player is started. In order to preview video,
1349 // need to start the player and pause it. This branch is called
1350 // only once if needed. After the player is started, any seek
1351 // operation will go through normal path.
Robert Shih0c61a0d2015-07-06 15:09:10 -07001352 // Audio-only cases are handled separately.
Wei Jiac5de0912016-11-18 10:22:14 -08001353 onStart(seekTimeUs, (MediaPlayerSeekMode)mode);
Robert Shih0c61a0d2015-07-06 15:09:10 -07001354 if (mStarted) {
1355 onPause();
1356 mPausedByClient = true;
1357 }
Wei Jia1061c9c2015-05-19 16:02:17 -07001358 if (needNotify) {
1359 notifyDriverSeekComplete();
1360 }
1361 break;
1362 }
1363
Andreas Hubera1f8ab02012-11-30 10:53:22 -08001364 mDeferredActions.push_back(
Wei Jiafef808d2014-10-31 17:57:05 -07001365 new FlushDecoderAction(FLUSH_CMD_FLUSH /* audio */,
1366 FLUSH_CMD_FLUSH /* video */));
Andreas Huber43c3e6c2011-01-05 12:17:08 -08001367
Wei Jiae427abf2014-09-22 15:21:11 -07001368 mDeferredActions.push_back(
Wei Jiac5de0912016-11-18 10:22:14 -08001369 new SeekAction(seekTimeUs, (MediaPlayerSeekMode)mode));
Andreas Huber43c3e6c2011-01-05 12:17:08 -08001370
Chong Zhangf8d71772014-11-26 15:08:34 -08001371 // After a flush without shutdown, decoder is paused.
1372 // Don't resume it until source seek is done, otherwise it could
Chong Zhang7137ec72014-11-12 16:41:05 -08001373 // start pulling stale data too soon.
1374 mDeferredActions.push_back(
Chong Zhangf8d71772014-11-26 15:08:34 -08001375 new ResumeDecoderAction(needNotify));
Chong Zhang7137ec72014-11-12 16:41:05 -08001376
Andreas Hubera1f8ab02012-11-30 10:53:22 -08001377 processDeferredActions();
Andreas Huber43c3e6c2011-01-05 12:17:08 -08001378 break;
1379 }
1380
Andreas Huberb4082222011-01-20 15:23:04 -08001381 case kWhatPause:
1382 {
Chong Zhangefbb6192015-01-30 17:13:27 -08001383 onPause();
1384 mPausedByClient = true;
Andreas Huberb4082222011-01-20 15:23:04 -08001385 break;
1386 }
1387
Andreas Huberb5f25f02013-02-05 10:14:26 -08001388 case kWhatSourceNotify:
1389 {
Andreas Huber9575c962013-02-05 13:59:56 -08001390 onSourceNotify(msg);
Andreas Huberb5f25f02013-02-05 10:14:26 -08001391 break;
1392 }
1393
Chong Zhanga7fa1d92014-06-11 14:49:23 -07001394 case kWhatClosedCaptionNotify:
1395 {
1396 onClosedCaptionNotify(msg);
1397 break;
1398 }
1399
Hassan Shojaniacefac142017-02-06 21:02:02 -08001400 case kWhatPrepareDrm:
1401 {
1402 status_t status = onPrepareDrm(msg);
1403
1404 sp<AMessage> response = new AMessage;
1405 response->setInt32("status", status);
1406 sp<AReplyToken> replyID;
1407 CHECK(msg->senderAwaitsResponse(&replyID));
1408 response->postReply(replyID);
1409 break;
1410 }
1411
1412 case kWhatReleaseDrm:
1413 {
1414 status_t status = onReleaseDrm();
1415
1416 sp<AMessage> response = new AMessage;
1417 response->setInt32("status", status);
1418 sp<AReplyToken> replyID;
1419 CHECK(msg->senderAwaitsResponse(&replyID));
1420 response->postReply(replyID);
1421 break;
1422 }
1423
Andreas Huberf9334412010-12-15 15:17:42 -08001424 default:
1425 TRESPASS();
1426 break;
1427 }
1428}
1429
Wei Jia94211742014-10-28 17:09:06 -07001430void NuPlayer::onResume() {
Ronghua Wu64c2d172015-10-07 16:52:19 -07001431 if (!mPaused || mResetting) {
1432 ALOGD_IF(mResetting, "resetting, onResume discarded");
Chong Zhangefbb6192015-01-30 17:13:27 -08001433 return;
1434 }
1435 mPaused = false;
Wei Jia94211742014-10-28 17:09:06 -07001436 if (mSource != NULL) {
1437 mSource->resume();
1438 } else {
1439 ALOGW("resume called when source is gone or not set");
1440 }
1441 // |mAudioDecoder| may have been released due to the pause timeout, so re-create it if
1442 // needed.
1443 if (audioDecoderStillNeeded() && mAudioDecoder == NULL) {
1444 instantiateDecoder(true /* audio */, &mAudioDecoder);
1445 }
1446 if (mRenderer != NULL) {
1447 mRenderer->resume();
1448 } else {
1449 ALOGW("resume called when renderer is gone or not set");
1450 }
Ray Essickd4d00612017-01-03 09:36:27 -08001451
1452 mLastStartedPlayingTimeNs = systemTime();
Wei Jia94211742014-10-28 17:09:06 -07001453}
1454
Lajos Molnarfcd3e942015-03-31 10:06:48 -07001455status_t NuPlayer::onInstantiateSecureDecoders() {
1456 status_t err;
1457 if (!(mSourceFlags & Source::FLAG_SECURE)) {
1458 return BAD_TYPE;
1459 }
1460
1461 if (mRenderer != NULL) {
1462 ALOGE("renderer should not be set when instantiating secure decoders");
1463 return UNKNOWN_ERROR;
1464 }
1465
1466 // TRICKY: We rely on mRenderer being null, so that decoder does not start requesting
1467 // data on instantiation.
Lajos Molnar1de1e252015-04-30 18:18:34 -07001468 if (mSurface != NULL) {
Lajos Molnarfcd3e942015-03-31 10:06:48 -07001469 err = instantiateDecoder(false, &mVideoDecoder);
1470 if (err != OK) {
1471 return err;
1472 }
1473 }
1474
1475 if (mAudioSink != NULL) {
1476 err = instantiateDecoder(true, &mAudioDecoder);
1477 if (err != OK) {
1478 return err;
1479 }
1480 }
1481 return OK;
1482}
1483
Wei Jiac5de0912016-11-18 10:22:14 -08001484void NuPlayer::onStart(int64_t startPositionUs, MediaPlayerSeekMode mode) {
Hassan Shojaniacefac142017-02-06 21:02:02 -08001485 ALOGV("onStart: mCrypto: %p (%d)", mCrypto.get(),
1486 (mCrypto != NULL ? mCrypto->getStrongCount() : 0));
1487
Robert Shih0c61a0d2015-07-06 15:09:10 -07001488 if (!mSourceStarted) {
1489 mSourceStarted = true;
1490 mSource->start();
1491 }
1492 if (startPositionUs > 0) {
Wei Jiac5de0912016-11-18 10:22:14 -08001493 performSeek(startPositionUs, mode);
Robert Shih0c61a0d2015-07-06 15:09:10 -07001494 if (mSource->getFormat(false /* audio */) == NULL) {
1495 return;
1496 }
1497 }
1498
Wei Jia94211742014-10-28 17:09:06 -07001499 mOffloadAudio = false;
1500 mAudioEOS = false;
1501 mVideoEOS = false;
Wei Jia94211742014-10-28 17:09:06 -07001502 mStarted = true;
Wei Jiafe8fe7d2016-06-08 10:29:25 -07001503 mPaused = false;
Wei Jia94211742014-10-28 17:09:06 -07001504
Wei Jia94211742014-10-28 17:09:06 -07001505 uint32_t flags = 0;
1506
1507 if (mSource->isRealTime()) {
1508 flags |= Renderer::FLAG_REAL_TIME;
1509 }
1510
Wei Jia9737d342016-12-28 14:59:59 -08001511 bool hasAudio = (mSource->getFormat(true /* audio */) != NULL);
1512 bool hasVideo = (mSource->getFormat(false /* audio */) != NULL);
1513 if (!hasAudio && !hasVideo) {
Wei Jia1de83d52016-10-10 10:15:03 -07001514 ALOGE("no metadata for either audio or video source");
1515 mSource->stop();
1516 mSourceStarted = false;
1517 notifyListener(MEDIA_ERROR, MEDIA_ERROR_UNKNOWN, ERROR_MALFORMED);
1518 return;
1519 }
Wei Jia9737d342016-12-28 14:59:59 -08001520 ALOGV_IF(!hasAudio, "no metadata for audio source"); // video only stream
1521
1522 sp<MetaData> audioMeta = mSource->getFormatMeta(true /* audio */);
Wei Jia1de83d52016-10-10 10:15:03 -07001523
Wei Jia94211742014-10-28 17:09:06 -07001524 audio_stream_type_t streamType = AUDIO_STREAM_MUSIC;
1525 if (mAudioSink != NULL) {
1526 streamType = mAudioSink->getAudioStreamType();
1527 }
1528
Wei Jia94211742014-10-28 17:09:06 -07001529 mOffloadAudio =
Wei Jia9737d342016-12-28 14:59:59 -08001530 canOffloadStream(audioMeta, hasVideo, mSource->isStreaming(), streamType)
Wei Jiabf70feb2016-02-19 15:47:16 -08001531 && (mPlaybackSettings.mSpeed == 1.f && mPlaybackSettings.mPitch == 1.f);
Hassan Shojaniacefac142017-02-06 21:02:02 -08001532
1533 // Modular DRM: Disabling audio offload if the source is protected
1534 if (mOffloadAudio && mIsDrmProtected) {
1535 mOffloadAudio = false;
1536 ALOGV("onStart: Disabling mOffloadAudio now that the source is protected.");
1537 }
1538
Wei Jia94211742014-10-28 17:09:06 -07001539 if (mOffloadAudio) {
1540 flags |= Renderer::FLAG_OFFLOAD_AUDIO;
1541 }
1542
Lajos Molnar1d15ab52015-03-04 16:46:34 -08001543 sp<AMessage> notify = new AMessage(kWhatRendererNotify, this);
Wei Jia94211742014-10-28 17:09:06 -07001544 ++mRendererGeneration;
1545 notify->setInt32("generation", mRendererGeneration);
Wei Jia0a68f662017-08-30 18:01:26 -07001546 mRenderer = new Renderer(mAudioSink, mMediaClock, notify, flags);
Wei Jia94211742014-10-28 17:09:06 -07001547 mRendererLooper = new ALooper;
1548 mRendererLooper->setName("NuPlayerRenderer");
1549 mRendererLooper->start(false, false, ANDROID_PRIORITY_AUDIO);
1550 mRendererLooper->registerHandler(mRenderer);
Lajos Molnar3a474aa2015-04-24 17:10:07 -07001551
1552 status_t err = mRenderer->setPlaybackSettings(mPlaybackSettings);
1553 if (err != OK) {
1554 mSource->stop();
Robert Shih0c61a0d2015-07-06 15:09:10 -07001555 mSourceStarted = false;
Lajos Molnar3a474aa2015-04-24 17:10:07 -07001556 notifyListener(MEDIA_ERROR, MEDIA_ERROR_UNKNOWN, err);
1557 return;
Wei Jiac8206ff2015-03-04 13:59:37 -08001558 }
Wei Jia94211742014-10-28 17:09:06 -07001559
Ronghua Wuc8a70d32015-04-29 16:26:34 -07001560 float rate = getFrameRate();
1561 if (rate > 0) {
Wei Jia94211742014-10-28 17:09:06 -07001562 mRenderer->setVideoFrameRate(rate);
1563 }
1564
Wei Jiac6cfd702014-11-11 16:33:20 -08001565 if (mVideoDecoder != NULL) {
1566 mVideoDecoder->setRenderer(mRenderer);
1567 }
1568 if (mAudioDecoder != NULL) {
1569 mAudioDecoder->setRenderer(mRenderer);
1570 }
1571
Ray Essickd4d00612017-01-03 09:36:27 -08001572 mLastStartedPlayingTimeNs = systemTime();
1573
Wei Jia94211742014-10-28 17:09:06 -07001574 postScanSources();
1575}
1576
Chong Zhangefbb6192015-01-30 17:13:27 -08001577void NuPlayer::onPause() {
1578 if (mPaused) {
1579 return;
1580 }
1581 mPaused = true;
1582 if (mSource != NULL) {
1583 mSource->pause();
1584 } else {
1585 ALOGW("pause called when source is gone or not set");
1586 }
1587 if (mRenderer != NULL) {
1588 mRenderer->pause();
1589 } else {
1590 ALOGW("pause called when renderer is gone or not set");
1591 }
Ray Essickd4d00612017-01-03 09:36:27 -08001592
1593 sp<NuPlayerDriver> driver = mDriver.promote();
1594 if (driver != NULL) {
1595 int64_t now = systemTime();
1596 int64_t played = now - mLastStartedPlayingTimeNs;
Ray Essickd4d00612017-01-03 09:36:27 -08001597
1598 driver->notifyMorePlayingTimeUs((played+500)/1000);
1599 }
Chong Zhangefbb6192015-01-30 17:13:27 -08001600}
1601
Ronghua Wud7988b12014-10-03 15:19:10 -07001602bool NuPlayer::audioDecoderStillNeeded() {
1603 // Audio decoder is no longer needed if it's in shut/shutting down status.
1604 return ((mFlushingAudio != SHUT_DOWN) && (mFlushingAudio != SHUTTING_DOWN_DECODER));
1605}
1606
Andy Hung8d121d42014-10-03 09:53:53 -07001607void NuPlayer::handleFlushComplete(bool audio, bool isDecoder) {
1608 // We wait for both the decoder flush and the renderer flush to complete
1609 // before entering either the FLUSHED or the SHUTTING_DOWN_DECODER state.
1610
1611 mFlushComplete[audio][isDecoder] = true;
1612 if (!mFlushComplete[audio][!isDecoder]) {
1613 return;
1614 }
1615
1616 FlushStatus *state = audio ? &mFlushingAudio : &mFlushingVideo;
1617 switch (*state) {
1618 case FLUSHING_DECODER:
1619 {
1620 *state = FLUSHED;
Andy Hung8d121d42014-10-03 09:53:53 -07001621 break;
1622 }
1623
1624 case FLUSHING_DECODER_SHUTDOWN:
1625 {
1626 *state = SHUTTING_DOWN_DECODER;
1627
1628 ALOGV("initiating %s decoder shutdown", audio ? "audio" : "video");
Andy Hung8d121d42014-10-03 09:53:53 -07001629 getDecoder(audio)->initiateShutdown();
1630 break;
1631 }
1632
1633 default:
1634 // decoder flush completes only occur in a flushing state.
1635 LOG_ALWAYS_FATAL_IF(isDecoder, "decoder flush in invalid state %d", *state);
1636 break;
1637 }
1638}
1639
Andreas Huber3831a062010-12-21 10:22:33 -08001640void NuPlayer::finishFlushIfPossible() {
Wei Jia53904f32014-07-29 10:22:53 -07001641 if (mFlushingAudio != NONE && mFlushingAudio != FLUSHED
1642 && mFlushingAudio != SHUT_DOWN) {
Andreas Huber3831a062010-12-21 10:22:33 -08001643 return;
1644 }
1645
Wei Jia53904f32014-07-29 10:22:53 -07001646 if (mFlushingVideo != NONE && mFlushingVideo != FLUSHED
1647 && mFlushingVideo != SHUT_DOWN) {
Andreas Huber3831a062010-12-21 10:22:33 -08001648 return;
1649 }
1650
Steve Block3856b092011-10-20 11:56:00 +01001651 ALOGV("both audio and video are flushed now.");
Andreas Huber3831a062010-12-21 10:22:33 -08001652
Andreas Huber3831a062010-12-21 10:22:33 -08001653 mFlushingAudio = NONE;
1654 mFlushingVideo = NONE;
Andreas Huber3831a062010-12-21 10:22:33 -08001655
Andy Hung8d121d42014-10-03 09:53:53 -07001656 clearFlushComplete();
1657
Andreas Hubera1f8ab02012-11-30 10:53:22 -08001658 processDeferredActions();
Andreas Huber1aef2112011-01-04 14:01:29 -08001659}
1660
1661void NuPlayer::postScanSources() {
1662 if (mScanSourcesPending) {
1663 return;
1664 }
1665
Lajos Molnar1d15ab52015-03-04 16:46:34 -08001666 sp<AMessage> msg = new AMessage(kWhatScanSources, this);
Andreas Huber1aef2112011-01-04 14:01:29 -08001667 msg->setInt32("generation", mScanSourcesGeneration);
1668 msg->post();
1669
1670 mScanSourcesPending = true;
1671}
1672
Wei Jia41cd4632016-05-13 10:53:30 -07001673void NuPlayer::tryOpenAudioSinkForOffload(
1674 const sp<AMessage> &format, const sp<MetaData> &audioMeta, bool hasVideo) {
Andy Hung202bce12014-12-03 11:47:36 -08001675 // Note: This is called early in NuPlayer to determine whether offloading
1676 // is possible; otherwise the decoders call the renderer openAudioSink directly.
Andy Hung282a7e32014-08-14 15:56:34 -07001677
Andy Hung202bce12014-12-03 11:47:36 -08001678 status_t err = mRenderer->openAudioSink(
Dhananjay Kumarc387f2b2015-08-06 10:43:16 +05301679 format, true /* offloadOnly */, hasVideo,
1680 AUDIO_OUTPUT_FLAG_NONE, &mOffloadAudio, mSource->isStreaming());
Andy Hung202bce12014-12-03 11:47:36 -08001681 if (err != OK) {
1682 // Any failure we turn off mOffloadAudio.
1683 mOffloadAudio = false;
1684 } else if (mOffloadAudio) {
Chong Zhang3b9eb1f2014-10-15 17:05:08 -07001685 sendMetaDataToHal(mAudioSink, audioMeta);
Andy Hung282a7e32014-08-14 15:56:34 -07001686 }
1687}
1688
1689void NuPlayer::closeAudioSink() {
Chong Zhang3b9eb1f2014-10-15 17:05:08 -07001690 mRenderer->closeAudioSink();
Andy Hung282a7e32014-08-14 15:56:34 -07001691}
1692
Wei Jia5031b2f2016-02-25 11:19:31 -08001693void NuPlayer::restartAudio(
Wei Jiabf70feb2016-02-19 15:47:16 -08001694 int64_t currentPositionUs, bool forceNonOffload, bool needsToCreateAudioDecoder) {
Wei Jiaa05f1e32016-03-25 16:31:22 -07001695 if (mAudioDecoder != NULL) {
1696 mAudioDecoder->pause();
1697 mAudioDecoder.clear();
Wei Jia686e8e52017-04-03 14:08:01 -07001698 mAudioDecoderError = false;
Wei Jiaa05f1e32016-03-25 16:31:22 -07001699 ++mAudioDecoderGeneration;
1700 }
Wei Jiabf70feb2016-02-19 15:47:16 -08001701 if (mFlushingAudio == FLUSHING_DECODER) {
1702 mFlushComplete[1 /* audio */][1 /* isDecoder */] = true;
1703 mFlushingAudio = FLUSHED;
1704 finishFlushIfPossible();
1705 } else if (mFlushingAudio == FLUSHING_DECODER_SHUTDOWN
1706 || mFlushingAudio == SHUTTING_DOWN_DECODER) {
1707 mFlushComplete[1 /* audio */][1 /* isDecoder */] = true;
1708 mFlushingAudio = SHUT_DOWN;
1709 finishFlushIfPossible();
1710 needsToCreateAudioDecoder = false;
1711 }
1712 if (mRenderer == NULL) {
1713 return;
1714 }
1715 closeAudioSink();
1716 mRenderer->flush(true /* audio */, false /* notifyComplete */);
1717 if (mVideoDecoder != NULL) {
1718 mRenderer->flush(false /* audio */, false /* notifyComplete */);
1719 }
1720
Wei Jiac5de0912016-11-18 10:22:14 -08001721 performSeek(currentPositionUs, MediaPlayerSeekMode::SEEK_PREVIOUS_SYNC /* mode */);
Wei Jiabf70feb2016-02-19 15:47:16 -08001722
1723 if (forceNonOffload) {
1724 mRenderer->signalDisableOffloadAudio();
1725 mOffloadAudio = false;
1726 }
1727 if (needsToCreateAudioDecoder) {
Wei Jiaa05f1e32016-03-25 16:31:22 -07001728 instantiateDecoder(true /* audio */, &mAudioDecoder, !forceNonOffload);
Wei Jiabf70feb2016-02-19 15:47:16 -08001729 }
1730}
1731
Wei Jia41cd4632016-05-13 10:53:30 -07001732void NuPlayer::determineAudioModeChange(const sp<AMessage> &audioFormat) {
Wei Jiae4d18c72015-07-13 17:58:11 -07001733 if (mSource == NULL || mAudioSink == NULL) {
1734 return;
1735 }
1736
1737 if (mRenderer == NULL) {
1738 ALOGW("No renderer can be used to determine audio mode. Use non-offload for safety.");
1739 mOffloadAudio = false;
1740 return;
1741 }
1742
1743 sp<MetaData> audioMeta = mSource->getFormatMeta(true /* audio */);
1744 sp<AMessage> videoFormat = mSource->getFormat(false /* audio */);
1745 audio_stream_type_t streamType = mAudioSink->getAudioStreamType();
1746 const bool hasVideo = (videoFormat != NULL);
Hassan Shojaniacefac142017-02-06 21:02:02 -08001747 bool canOffload = canOffloadStream(
Wei Jiabf70feb2016-02-19 15:47:16 -08001748 audioMeta, hasVideo, mSource->isStreaming(), streamType)
1749 && (mPlaybackSettings.mSpeed == 1.f && mPlaybackSettings.mPitch == 1.f);
Hassan Shojaniacefac142017-02-06 21:02:02 -08001750
1751 // Modular DRM: Disabling audio offload if the source is protected
1752 if (canOffload && mIsDrmProtected) {
1753 canOffload = false;
1754 ALOGV("determineAudioModeChange: Disabling mOffloadAudio b/c the source is protected.");
1755 }
1756
Wei Jiae4d18c72015-07-13 17:58:11 -07001757 if (canOffload) {
1758 if (!mOffloadAudio) {
1759 mRenderer->signalEnableOffloadAudio();
1760 }
1761 // open audio sink early under offload mode.
Wei Jia41cd4632016-05-13 10:53:30 -07001762 tryOpenAudioSinkForOffload(audioFormat, audioMeta, hasVideo);
Wei Jiae4d18c72015-07-13 17:58:11 -07001763 } else {
Robert Shihe1d70192015-07-23 17:54:13 -07001764 if (mOffloadAudio) {
1765 mRenderer->signalDisableOffloadAudio();
1766 mOffloadAudio = false;
1767 }
Wei Jiae4d18c72015-07-13 17:58:11 -07001768 }
1769}
1770
Wei Jiaa05f1e32016-03-25 16:31:22 -07001771status_t NuPlayer::instantiateDecoder(
1772 bool audio, sp<DecoderBase> *decoder, bool checkAudioModeChange) {
Wei Jia566da802015-08-27 13:59:30 -07001773 // The audio decoder could be cleared by tear down. If still in shut down
1774 // process, no need to create a new audio decoder.
1775 if (*decoder != NULL || (audio && mFlushingAudio == SHUT_DOWN)) {
Andreas Huberf9334412010-12-15 15:17:42 -08001776 return OK;
1777 }
1778
Andreas Huber84066782011-08-16 09:34:26 -07001779 sp<AMessage> format = mSource->getFormat(audio);
Andreas Huberf9334412010-12-15 15:17:42 -08001780
Andreas Huber84066782011-08-16 09:34:26 -07001781 if (format == NULL) {
Robert Shih7350b052015-10-01 15:50:14 -07001782 return UNKNOWN_ERROR;
1783 } else {
1784 status_t err;
1785 if (format->findInt32("err", &err) && err) {
1786 return err;
1787 }
Andreas Huberf9334412010-12-15 15:17:42 -08001788 }
1789
Ronghua Wu8db88132015-04-22 13:51:35 -07001790 format->setInt32("priority", 0 /* realtime */);
1791
Andreas Huber3fe62152011-09-16 15:09:22 -07001792 if (!audio) {
Andreas Huber84066782011-08-16 09:34:26 -07001793 AString mime;
1794 CHECK(format->findString("mime", &mime));
Chong Zhanga7fa1d92014-06-11 14:49:23 -07001795
Lajos Molnar1d15ab52015-03-04 16:46:34 -08001796 sp<AMessage> ccNotify = new AMessage(kWhatClosedCaptionNotify, this);
Chong Zhang341ab6e2015-02-04 13:37:18 -08001797 if (mCCDecoder == NULL) {
1798 mCCDecoder = new CCDecoder(ccNotify);
1799 }
Lajos Molnar09524832014-07-17 14:29:51 -07001800
1801 if (mSourceFlags & Source::FLAG_SECURE) {
1802 format->setInt32("secure", true);
1803 }
Chong Zhang17134602015-01-07 16:14:34 -08001804
1805 if (mSourceFlags & Source::FLAG_PROTECTED) {
1806 format->setInt32("protected", true);
1807 }
Ronghua Wu8db88132015-04-22 13:51:35 -07001808
Ronghua Wuc8a70d32015-04-29 16:26:34 -07001809 float rate = getFrameRate();
1810 if (rate > 0) {
Lajos Molnar3a474aa2015-04-24 17:10:07 -07001811 format->setFloat("operating-rate", rate * mPlaybackSettings.mSpeed);
Ronghua Wu8db88132015-04-22 13:51:35 -07001812 }
Andreas Huber3fe62152011-09-16 15:09:22 -07001813 }
1814
Wei Jiabc2fb722014-07-08 16:37:57 -07001815 if (audio) {
Lajos Molnar1d15ab52015-03-04 16:46:34 -08001816 sp<AMessage> notify = new AMessage(kWhatAudioNotify, this);
Wei Jia88703c32014-08-06 11:24:07 -07001817 ++mAudioDecoderGeneration;
1818 notify->setInt32("generation", mAudioDecoderGeneration);
1819
Wei Jiaa05f1e32016-03-25 16:31:22 -07001820 if (checkAudioModeChange) {
Wei Jia41cd4632016-05-13 10:53:30 -07001821 determineAudioModeChange(format);
Wei Jiaa05f1e32016-03-25 16:31:22 -07001822 }
Wei Jiabc2fb722014-07-08 16:37:57 -07001823 if (mOffloadAudio) {
Wei Jia14532f22015-12-29 11:28:15 -08001824 mSource->setOffloadAudio(true /* offload */);
1825
Haynes Mathew George8b635332015-03-30 17:59:47 -07001826 const bool hasVideo = (mSource->getFormat(false /*audio */) != NULL);
1827 format->setInt32("has-video", hasVideo);
Wei Jiac6cfd702014-11-11 16:33:20 -08001828 *decoder = new DecoderPassThrough(notify, mSource, mRenderer);
Hassan Shojaniacefac142017-02-06 21:02:02 -08001829 ALOGV("instantiateDecoder audio DecoderPassThrough hasVideo: %d", hasVideo);
Wei Jiabc2fb722014-07-08 16:37:57 -07001830 } else {
Wei Jia14532f22015-12-29 11:28:15 -08001831 mSource->setOffloadAudio(false /* offload */);
1832
Wei Jiaf2ae3e12016-10-27 17:10:59 -07001833 *decoder = new Decoder(notify, mSource, mPID, mUID, mRenderer);
Hassan Shojaniacefac142017-02-06 21:02:02 -08001834 ALOGV("instantiateDecoder audio Decoder");
Wei Jiabc2fb722014-07-08 16:37:57 -07001835 }
Wei Jia686e8e52017-04-03 14:08:01 -07001836 mAudioDecoderError = false;
Wei Jiabc2fb722014-07-08 16:37:57 -07001837 } else {
Lajos Molnar1d15ab52015-03-04 16:46:34 -08001838 sp<AMessage> notify = new AMessage(kWhatVideoNotify, this);
Wei Jia88703c32014-08-06 11:24:07 -07001839 ++mVideoDecoderGeneration;
1840 notify->setInt32("generation", mVideoDecoderGeneration);
1841
Chong Zhang7137ec72014-11-12 16:41:05 -08001842 *decoder = new Decoder(
Wei Jiaf2ae3e12016-10-27 17:10:59 -07001843 notify, mSource, mPID, mUID, mRenderer, mSurface, mCCDecoder);
Wei Jia686e8e52017-04-03 14:08:01 -07001844 mVideoDecoderError = false;
Lajos Molnard9fd6312014-11-06 11:00:00 -08001845
1846 // enable FRC if high-quality AV sync is requested, even if not
Lajos Molnar1de1e252015-04-30 18:18:34 -07001847 // directly queuing to display, as this will even improve textureview
Lajos Molnard9fd6312014-11-06 11:00:00 -08001848 // playback.
1849 {
Marco Nelissen96626b72016-12-01 13:58:59 -08001850 if (property_get_bool("persist.sys.media.avsync", false)) {
Lajos Molnard9fd6312014-11-06 11:00:00 -08001851 format->setInt32("auto-frc", 1);
1852 }
1853 }
Wei Jiabc2fb722014-07-08 16:37:57 -07001854 }
Lajos Molnar1cd13982014-01-17 15:12:51 -08001855 (*decoder)->init();
Hassan Shojaniacefac142017-02-06 21:02:02 -08001856
1857 // Modular DRM
1858 if (mIsDrmProtected) {
1859 format->setPointer("crypto", mCrypto.get());
1860 ALOGV("instantiateDecoder: mCrypto: %p (%d) isSecure: %d", mCrypto.get(),
1861 (mCrypto != NULL ? mCrypto->getStrongCount() : 0),
1862 (mSourceFlags & Source::FLAG_SECURE) != 0);
1863 }
1864
Andreas Huber84066782011-08-16 09:34:26 -07001865 (*decoder)->configure(format);
Andreas Huberf9334412010-12-15 15:17:42 -08001866
Praveen Chavanbbaa1442016-04-08 13:33:49 -07001867 if (!audio) {
1868 sp<AMessage> params = new AMessage();
1869 float rate = getFrameRate();
1870 if (rate > 0) {
1871 params->setFloat("frame-rate-total", rate);
1872 }
1873
1874 sp<MetaData> fileMeta = getFileMeta();
1875 if (fileMeta != NULL) {
1876 int32_t videoTemporalLayerCount;
1877 if (fileMeta->findInt32(kKeyTemporalLayerCount, &videoTemporalLayerCount)
1878 && videoTemporalLayerCount > 0) {
1879 params->setInt32("temporal-layer-count", videoTemporalLayerCount);
1880 }
1881 }
1882
1883 if (params->countEntries() > 0) {
1884 (*decoder)->setParameters(params);
1885 }
1886 }
Andreas Huberf9334412010-12-15 15:17:42 -08001887 return OK;
1888}
1889
Chong Zhangced1c2f2014-08-08 15:22:35 -07001890void NuPlayer::updateVideoSize(
1891 const sp<AMessage> &inputFormat,
1892 const sp<AMessage> &outputFormat) {
1893 if (inputFormat == NULL) {
1894 ALOGW("Unknown video size, reporting 0x0!");
1895 notifyListener(MEDIA_SET_VIDEO_SIZE, 0, 0);
1896 return;
1897 }
Wei Jia9737d342016-12-28 14:59:59 -08001898 int32_t err = OK;
1899 inputFormat->findInt32("err", &err);
1900 if (err == -EWOULDBLOCK) {
1901 ALOGW("Video meta is not available yet!");
1902 return;
1903 }
1904 if (err != OK) {
1905 ALOGW("Something is wrong with video meta!");
1906 return;
1907 }
Chong Zhangced1c2f2014-08-08 15:22:35 -07001908
1909 int32_t displayWidth, displayHeight;
Chong Zhangced1c2f2014-08-08 15:22:35 -07001910 if (outputFormat != NULL) {
1911 int32_t width, height;
1912 CHECK(outputFormat->findInt32("width", &width));
1913 CHECK(outputFormat->findInt32("height", &height));
1914
1915 int32_t cropLeft, cropTop, cropRight, cropBottom;
1916 CHECK(outputFormat->findRect(
1917 "crop",
1918 &cropLeft, &cropTop, &cropRight, &cropBottom));
1919
1920 displayWidth = cropRight - cropLeft + 1;
1921 displayHeight = cropBottom - cropTop + 1;
1922
1923 ALOGV("Video output format changed to %d x %d "
1924 "(crop: %d x %d @ (%d, %d))",
1925 width, height,
1926 displayWidth,
1927 displayHeight,
1928 cropLeft, cropTop);
1929 } else {
1930 CHECK(inputFormat->findInt32("width", &displayWidth));
1931 CHECK(inputFormat->findInt32("height", &displayHeight));
1932
1933 ALOGV("Video input format %d x %d", displayWidth, displayHeight);
1934 }
1935
1936 // Take into account sample aspect ratio if necessary:
1937 int32_t sarWidth, sarHeight;
1938 if (inputFormat->findInt32("sar-width", &sarWidth)
Wei Jia095bc252017-01-27 10:16:16 -08001939 && inputFormat->findInt32("sar-height", &sarHeight)
1940 && sarWidth > 0 && sarHeight > 0) {
Chong Zhangced1c2f2014-08-08 15:22:35 -07001941 ALOGV("Sample aspect ratio %d : %d", sarWidth, sarHeight);
1942
1943 displayWidth = (displayWidth * sarWidth) / sarHeight;
1944
1945 ALOGV("display dimensions %d x %d", displayWidth, displayHeight);
Wei Jiadf6c6af2016-09-29 11:27:15 -07001946 } else {
1947 int32_t width, height;
1948 if (inputFormat->findInt32("display-width", &width)
1949 && inputFormat->findInt32("display-height", &height)
1950 && width > 0 && height > 0
1951 && displayWidth > 0 && displayHeight > 0) {
1952 if (displayHeight * (int64_t)width / height > (int64_t)displayWidth) {
1953 displayHeight = (int32_t)(displayWidth * (int64_t)height / width);
1954 } else {
1955 displayWidth = (int32_t)(displayHeight * (int64_t)width / height);
1956 }
1957 ALOGV("Video display width and height are overridden to %d x %d",
1958 displayWidth, displayHeight);
1959 }
Chong Zhangced1c2f2014-08-08 15:22:35 -07001960 }
1961
1962 int32_t rotationDegrees;
1963 if (!inputFormat->findInt32("rotation-degrees", &rotationDegrees)) {
1964 rotationDegrees = 0;
1965 }
1966
1967 if (rotationDegrees == 90 || rotationDegrees == 270) {
1968 int32_t tmp = displayWidth;
1969 displayWidth = displayHeight;
1970 displayHeight = tmp;
1971 }
1972
1973 notifyListener(
1974 MEDIA_SET_VIDEO_SIZE,
1975 displayWidth,
1976 displayHeight);
1977}
1978
Chong Zhangdcb89b32013-08-06 09:44:47 -07001979void NuPlayer::notifyListener(int msg, int ext1, int ext2, const Parcel *in) {
Andreas Huber43c3e6c2011-01-05 12:17:08 -08001980 if (mDriver == NULL) {
Andreas Huberf9334412010-12-15 15:17:42 -08001981 return;
1982 }
1983
Andreas Huber43c3e6c2011-01-05 12:17:08 -08001984 sp<NuPlayerDriver> driver = mDriver.promote();
Andreas Huberf9334412010-12-15 15:17:42 -08001985
Andreas Huber43c3e6c2011-01-05 12:17:08 -08001986 if (driver == NULL) {
Andreas Huberf9334412010-12-15 15:17:42 -08001987 return;
1988 }
1989
Chong Zhangdcb89b32013-08-06 09:44:47 -07001990 driver->notifyListener(msg, ext1, ext2, in);
Andreas Huberf9334412010-12-15 15:17:42 -08001991}
1992
Chong Zhang7137ec72014-11-12 16:41:05 -08001993void NuPlayer::flushDecoder(bool audio, bool needShutdown) {
Andreas Huber14f76722013-01-15 09:04:18 -08001994 ALOGV("[%s] flushDecoder needShutdown=%d",
1995 audio ? "audio" : "video", needShutdown);
1996
Chong Zhang7137ec72014-11-12 16:41:05 -08001997 const sp<DecoderBase> &decoder = getDecoder(audio);
Lajos Molnar87603c02014-08-20 19:25:30 -07001998 if (decoder == NULL) {
Steve Blockdf64d152012-01-04 20:05:49 +00001999 ALOGI("flushDecoder %s without decoder present",
Andreas Huber6e3d3112011-11-28 12:36:11 -08002000 audio ? "audio" : "video");
Lajos Molnar87603c02014-08-20 19:25:30 -07002001 return;
Andreas Huber6e3d3112011-11-28 12:36:11 -08002002 }
2003
Andreas Huber1aef2112011-01-04 14:01:29 -08002004 // Make sure we don't continue to scan sources until we finish flushing.
2005 ++mScanSourcesGeneration;
Chong Zhang3b032b32015-04-17 15:49:06 -07002006 if (mScanSourcesPending) {
Toshikazu Saitocbcbb792015-09-15 14:53:01 +09002007 if (!needShutdown) {
2008 mDeferredActions.push_back(
2009 new SimpleAction(&NuPlayer::performScanSources));
2010 }
Chong Zhang3b032b32015-04-17 15:49:06 -07002011 mScanSourcesPending = false;
2012 }
Andreas Huber1aef2112011-01-04 14:01:29 -08002013
Chong Zhang7137ec72014-11-12 16:41:05 -08002014 decoder->signalFlush();
Andreas Huber1aef2112011-01-04 14:01:29 -08002015
2016 FlushStatus newStatus =
2017 needShutdown ? FLUSHING_DECODER_SHUTDOWN : FLUSHING_DECODER;
2018
Lajos Molnarfcd3e942015-03-31 10:06:48 -07002019 mFlushComplete[audio][false /* isDecoder */] = (mRenderer == NULL);
Andy Hung8d121d42014-10-03 09:53:53 -07002020 mFlushComplete[audio][true /* isDecoder */] = false;
Andreas Huber1aef2112011-01-04 14:01:29 -08002021 if (audio) {
Wei Jia53904f32014-07-29 10:22:53 -07002022 ALOGE_IF(mFlushingAudio != NONE,
2023 "audio flushDecoder() is called in state %d", mFlushingAudio);
Andreas Huber1aef2112011-01-04 14:01:29 -08002024 mFlushingAudio = newStatus;
Andreas Huber1aef2112011-01-04 14:01:29 -08002025 } else {
Wei Jia53904f32014-07-29 10:22:53 -07002026 ALOGE_IF(mFlushingVideo != NONE,
2027 "video flushDecoder() is called in state %d", mFlushingVideo);
Andreas Huber1aef2112011-01-04 14:01:29 -08002028 mFlushingVideo = newStatus;
Andreas Huber1aef2112011-01-04 14:01:29 -08002029 }
2030}
2031
Chong Zhangced1c2f2014-08-08 15:22:35 -07002032void NuPlayer::queueDecoderShutdown(
2033 bool audio, bool video, const sp<AMessage> &reply) {
2034 ALOGI("queueDecoderShutdown audio=%d, video=%d", audio, video);
Andreas Huber84066782011-08-16 09:34:26 -07002035
Chong Zhangced1c2f2014-08-08 15:22:35 -07002036 mDeferredActions.push_back(
Wei Jiafef808d2014-10-31 17:57:05 -07002037 new FlushDecoderAction(
2038 audio ? FLUSH_CMD_SHUTDOWN : FLUSH_CMD_NONE,
2039 video ? FLUSH_CMD_SHUTDOWN : FLUSH_CMD_NONE));
Andreas Huber84066782011-08-16 09:34:26 -07002040
Chong Zhangced1c2f2014-08-08 15:22:35 -07002041 mDeferredActions.push_back(
2042 new SimpleAction(&NuPlayer::performScanSources));
Andreas Huber84066782011-08-16 09:34:26 -07002043
Chong Zhangced1c2f2014-08-08 15:22:35 -07002044 mDeferredActions.push_back(new PostMessageAction(reply));
2045
2046 processDeferredActions();
Andreas Huber84066782011-08-16 09:34:26 -07002047}
2048
James Dong0d268a32012-08-31 12:18:27 -07002049status_t NuPlayer::setVideoScalingMode(int32_t mode) {
2050 mVideoScalingMode = mode;
Lajos Molnar1de1e252015-04-30 18:18:34 -07002051 if (mSurface != NULL) {
2052 status_t ret = native_window_set_scaling_mode(mSurface.get(), mVideoScalingMode);
James Dong0d268a32012-08-31 12:18:27 -07002053 if (ret != OK) {
2054 ALOGE("Failed to set scaling mode (%d): %s",
2055 -ret, strerror(-ret));
2056 return ret;
2057 }
2058 }
2059 return OK;
2060}
2061
Chong Zhangdcb89b32013-08-06 09:44:47 -07002062status_t NuPlayer::getTrackInfo(Parcel* reply) const {
Lajos Molnar1d15ab52015-03-04 16:46:34 -08002063 sp<AMessage> msg = new AMessage(kWhatGetTrackInfo, this);
Chong Zhangdcb89b32013-08-06 09:44:47 -07002064 msg->setPointer("reply", reply);
2065
2066 sp<AMessage> response;
2067 status_t err = msg->postAndAwaitResponse(&response);
2068 return err;
2069}
2070
Robert Shih7c4f0d72014-07-09 18:53:31 -07002071status_t NuPlayer::getSelectedTrack(int32_t type, Parcel* reply) const {
Lajos Molnar1d15ab52015-03-04 16:46:34 -08002072 sp<AMessage> msg = new AMessage(kWhatGetSelectedTrack, this);
Robert Shih7c4f0d72014-07-09 18:53:31 -07002073 msg->setPointer("reply", reply);
2074 msg->setInt32("type", type);
2075
2076 sp<AMessage> response;
2077 status_t err = msg->postAndAwaitResponse(&response);
2078 if (err == OK && response != NULL) {
2079 CHECK(response->findInt32("err", &err));
2080 }
2081 return err;
2082}
2083
Robert Shih6ffb1fd2014-10-29 16:24:32 -07002084status_t NuPlayer::selectTrack(size_t trackIndex, bool select, int64_t timeUs) {
Lajos Molnar1d15ab52015-03-04 16:46:34 -08002085 sp<AMessage> msg = new AMessage(kWhatSelectTrack, this);
Chong Zhangdcb89b32013-08-06 09:44:47 -07002086 msg->setSize("trackIndex", trackIndex);
2087 msg->setInt32("select", select);
Robert Shih6ffb1fd2014-10-29 16:24:32 -07002088 msg->setInt64("timeUs", timeUs);
Chong Zhangdcb89b32013-08-06 09:44:47 -07002089
2090 sp<AMessage> response;
2091 status_t err = msg->postAndAwaitResponse(&response);
2092
Chong Zhang404fced2014-06-11 14:45:31 -07002093 if (err != OK) {
2094 return err;
2095 }
2096
2097 if (!response->findInt32("err", &err)) {
2098 err = OK;
2099 }
2100
Chong Zhangdcb89b32013-08-06 09:44:47 -07002101 return err;
2102}
2103
Ronghua Wua73d9e02014-10-08 15:13:29 -07002104status_t NuPlayer::getCurrentPosition(int64_t *mediaUs) {
2105 sp<Renderer> renderer = mRenderer;
2106 if (renderer == NULL) {
2107 return NO_INIT;
2108 }
2109
2110 return renderer->getCurrentPosition(mediaUs);
2111}
2112
Praveen Chavane1e5d7a2015-05-19 19:09:48 -07002113void NuPlayer::getStats(Vector<sp<AMessage> > *mTrackStats) {
2114 CHECK(mTrackStats != NULL);
2115
2116 mTrackStats->clear();
2117 if (mVideoDecoder != NULL) {
2118 mTrackStats->push_back(mVideoDecoder->getStats());
2119 }
2120 if (mAudioDecoder != NULL) {
2121 mTrackStats->push_back(mAudioDecoder->getStats());
Chong Zhang7137ec72014-11-12 16:41:05 -08002122 }
Ronghua Wua73d9e02014-10-08 15:13:29 -07002123}
2124
Marco Nelissenf0b72b52014-09-16 15:43:44 -07002125sp<MetaData> NuPlayer::getFileMeta() {
2126 return mSource->getFileFormatMeta();
2127}
2128
Ronghua Wuc8a70d32015-04-29 16:26:34 -07002129float NuPlayer::getFrameRate() {
2130 sp<MetaData> meta = mSource->getFormatMeta(false /* audio */);
2131 if (meta == NULL) {
2132 return 0;
2133 }
2134 int32_t rate;
2135 if (!meta->findInt32(kKeyFrameRate, &rate)) {
2136 // fall back to try file meta
2137 sp<MetaData> fileMeta = getFileMeta();
2138 if (fileMeta == NULL) {
2139 ALOGW("source has video meta but not file meta");
2140 return -1;
2141 }
2142 int32_t fileMetaRate;
2143 if (!fileMeta->findInt32(kKeyFrameRate, &fileMetaRate)) {
2144 return -1;
2145 }
2146 return fileMetaRate;
2147 }
2148 return rate;
2149}
2150
Andreas Huberb7c8e912012-11-27 15:02:53 -08002151void NuPlayer::schedulePollDuration() {
Lajos Molnar1d15ab52015-03-04 16:46:34 -08002152 sp<AMessage> msg = new AMessage(kWhatPollDuration, this);
Andreas Huberb7c8e912012-11-27 15:02:53 -08002153 msg->setInt32("generation", mPollDurationGeneration);
2154 msg->post();
2155}
2156
2157void NuPlayer::cancelPollDuration() {
2158 ++mPollDurationGeneration;
2159}
2160
Andreas Hubera1f8ab02012-11-30 10:53:22 -08002161void NuPlayer::processDeferredActions() {
2162 while (!mDeferredActions.empty()) {
2163 // We won't execute any deferred actions until we're no longer in
2164 // an intermediate state, i.e. one more more decoders are currently
2165 // flushing or shutting down.
2166
Andreas Hubera1f8ab02012-11-30 10:53:22 -08002167 if (mFlushingAudio != NONE || mFlushingVideo != NONE) {
2168 // We're currently flushing, postpone the reset until that's
2169 // completed.
2170
2171 ALOGV("postponing action mFlushingAudio=%d, mFlushingVideo=%d",
2172 mFlushingAudio, mFlushingVideo);
2173
2174 break;
2175 }
2176
2177 sp<Action> action = *mDeferredActions.begin();
2178 mDeferredActions.erase(mDeferredActions.begin());
2179
2180 action->execute(this);
2181 }
2182}
2183
Wei Jiac5de0912016-11-18 10:22:14 -08002184void NuPlayer::performSeek(int64_t seekTimeUs, MediaPlayerSeekMode mode) {
2185 ALOGV("performSeek seekTimeUs=%lld us (%.2f secs), mode=%d",
2186 (long long)seekTimeUs, seekTimeUs / 1E6, mode);
Andreas Hubera1f8ab02012-11-30 10:53:22 -08002187
Andy Hungadf34bf2014-09-03 18:22:22 -07002188 if (mSource == NULL) {
2189 // This happens when reset occurs right before the loop mode
2190 // asynchronously seeks to the start of the stream.
2191 LOG_ALWAYS_FATAL_IF(mAudioDecoder != NULL || mVideoDecoder != NULL,
2192 "mSource is NULL and decoders not NULL audio(%p) video(%p)",
2193 mAudioDecoder.get(), mVideoDecoder.get());
2194 return;
2195 }
Robert Shih1a5c8592015-08-04 18:07:44 -07002196 mPreviousSeekTimeUs = seekTimeUs;
Wei Jiac5de0912016-11-18 10:22:14 -08002197 mSource->seekTo(seekTimeUs, mode);
Robert Shihd3b0bbb2014-07-23 15:00:25 -07002198 ++mTimedTextGeneration;
Andreas Hubera1f8ab02012-11-30 10:53:22 -08002199
Andreas Hubera1f8ab02012-11-30 10:53:22 -08002200 // everything's flushed, continue playback.
2201}
2202
Wei Jiafef808d2014-10-31 17:57:05 -07002203void NuPlayer::performDecoderFlush(FlushCommand audio, FlushCommand video) {
2204 ALOGV("performDecoderFlush audio=%d, video=%d", audio, video);
Andreas Hubera1f8ab02012-11-30 10:53:22 -08002205
Wei Jiafef808d2014-10-31 17:57:05 -07002206 if ((audio == FLUSH_CMD_NONE || mAudioDecoder == NULL)
2207 && (video == FLUSH_CMD_NONE || mVideoDecoder == NULL)) {
Andreas Hubera1f8ab02012-11-30 10:53:22 -08002208 return;
2209 }
2210
Wei Jiafef808d2014-10-31 17:57:05 -07002211 if (audio != FLUSH_CMD_NONE && mAudioDecoder != NULL) {
2212 flushDecoder(true /* audio */, (audio == FLUSH_CMD_SHUTDOWN));
Andreas Hubera1f8ab02012-11-30 10:53:22 -08002213 }
2214
Wei Jiafef808d2014-10-31 17:57:05 -07002215 if (video != FLUSH_CMD_NONE && mVideoDecoder != NULL) {
2216 flushDecoder(false /* audio */, (video == FLUSH_CMD_SHUTDOWN));
Andreas Hubera1f8ab02012-11-30 10:53:22 -08002217 }
2218}
2219
2220void NuPlayer::performReset() {
2221 ALOGV("performReset");
2222
2223 CHECK(mAudioDecoder == NULL);
2224 CHECK(mVideoDecoder == NULL);
2225
2226 cancelPollDuration();
2227
2228 ++mScanSourcesGeneration;
2229 mScanSourcesPending = false;
2230
Lajos Molnar09524832014-07-17 14:29:51 -07002231 if (mRendererLooper != NULL) {
2232 if (mRenderer != NULL) {
2233 mRendererLooper->unregisterHandler(mRenderer->id());
2234 }
2235 mRendererLooper->stop();
2236 mRendererLooper.clear();
2237 }
Andreas Hubera1f8ab02012-11-30 10:53:22 -08002238 mRenderer.clear();
Wei Jia57568df2014-09-22 10:16:29 -07002239 ++mRendererGeneration;
Andreas Hubera1f8ab02012-11-30 10:53:22 -08002240
2241 if (mSource != NULL) {
2242 mSource->stop();
Andreas Huberb5f25f02013-02-05 10:14:26 -08002243
Wei Jiac45a4b22016-04-15 15:30:23 -07002244 Mutex::Autolock autoLock(mSourceLock);
Andreas Hubera1f8ab02012-11-30 10:53:22 -08002245 mSource.clear();
2246 }
2247
2248 if (mDriver != NULL) {
2249 sp<NuPlayerDriver> driver = mDriver.promote();
2250 if (driver != NULL) {
2251 driver->notifyResetComplete();
2252 }
2253 }
Andreas Huber57a339c2012-12-03 11:18:00 -08002254
2255 mStarted = false;
Wei Jia8a092d32016-06-03 14:57:24 -07002256 mPrepared = false;
Ronghua Wu64c2d172015-10-07 16:52:19 -07002257 mResetting = false;
Robert Shih0c61a0d2015-07-06 15:09:10 -07002258 mSourceStarted = false;
Hassan Shojaniacefac142017-02-06 21:02:02 -08002259
2260 // Modular DRM
2261 if (mCrypto != NULL) {
2262 // decoders will be flushed before this so their mCrypto would go away on their own
2263 // TODO change to ALOGV
2264 ALOGD("performReset mCrypto: %p (%d)", mCrypto.get(),
2265 (mCrypto != NULL ? mCrypto->getStrongCount() : 0));
2266 mCrypto.clear();
2267 }
2268 mIsDrmProtected = false;
Andreas Hubera1f8ab02012-11-30 10:53:22 -08002269}
2270
2271void NuPlayer::performScanSources() {
2272 ALOGV("performScanSources");
2273
Andreas Huber57a339c2012-12-03 11:18:00 -08002274 if (!mStarted) {
2275 return;
2276 }
2277
Andreas Hubera1f8ab02012-11-30 10:53:22 -08002278 if (mAudioDecoder == NULL || mVideoDecoder == NULL) {
2279 postScanSources();
2280 }
2281}
2282
Lajos Molnar1de1e252015-04-30 18:18:34 -07002283void NuPlayer::performSetSurface(const sp<Surface> &surface) {
Andreas Huber57a339c2012-12-03 11:18:00 -08002284 ALOGV("performSetSurface");
2285
Lajos Molnar1de1e252015-04-30 18:18:34 -07002286 mSurface = surface;
Andreas Huber57a339c2012-12-03 11:18:00 -08002287
2288 // XXX - ignore error from setVideoScalingMode for now
2289 setVideoScalingMode(mVideoScalingMode);
Chong Zhang13d6faa2014-08-22 15:35:28 -07002290
2291 if (mDriver != NULL) {
2292 sp<NuPlayerDriver> driver = mDriver.promote();
2293 if (driver != NULL) {
2294 driver->notifySetSurfaceComplete();
2295 }
2296 }
Andreas Huber57a339c2012-12-03 11:18:00 -08002297}
2298
Chong Zhangf8d71772014-11-26 15:08:34 -08002299void NuPlayer::performResumeDecoders(bool needNotify) {
2300 if (needNotify) {
2301 mResumePending = true;
2302 if (mVideoDecoder == NULL) {
2303 // if audio-only, we can notify seek complete now,
2304 // as the resume operation will be relatively fast.
2305 finishResume();
2306 }
2307 }
2308
Chong Zhang7137ec72014-11-12 16:41:05 -08002309 if (mVideoDecoder != NULL) {
Chong Zhangf8d71772014-11-26 15:08:34 -08002310 // When there is continuous seek, MediaPlayer will cache the seek
2311 // position, and send down new seek request when previous seek is
2312 // complete. Let's wait for at least one video output frame before
2313 // notifying seek complete, so that the video thumbnail gets updated
2314 // when seekbar is dragged.
2315 mVideoDecoder->signalResume(needNotify);
Chong Zhang7137ec72014-11-12 16:41:05 -08002316 }
2317
2318 if (mAudioDecoder != NULL) {
Chong Zhangf8d71772014-11-26 15:08:34 -08002319 mAudioDecoder->signalResume(false /* needNotify */);
2320 }
2321}
2322
2323void NuPlayer::finishResume() {
2324 if (mResumePending) {
2325 mResumePending = false;
Wei Jia1061c9c2015-05-19 16:02:17 -07002326 notifyDriverSeekComplete();
2327 }
2328}
2329
2330void NuPlayer::notifyDriverSeekComplete() {
2331 if (mDriver != NULL) {
2332 sp<NuPlayerDriver> driver = mDriver.promote();
2333 if (driver != NULL) {
2334 driver->notifySeekComplete();
Chong Zhangf8d71772014-11-26 15:08:34 -08002335 }
Chong Zhang7137ec72014-11-12 16:41:05 -08002336 }
2337}
2338
Andreas Huber9575c962013-02-05 13:59:56 -08002339void NuPlayer::onSourceNotify(const sp<AMessage> &msg) {
2340 int32_t what;
2341 CHECK(msg->findInt32("what", &what));
2342
2343 switch (what) {
Lajos Molnarfcd3e942015-03-31 10:06:48 -07002344 case Source::kWhatInstantiateSecureDecoders:
2345 {
2346 if (mSource == NULL) {
2347 // This is a stale notification from a source that was
2348 // asynchronously preparing when the client called reset().
2349 // We handled the reset, the source is gone.
2350 break;
2351 }
2352
2353 sp<AMessage> reply;
2354 CHECK(msg->findMessage("reply", &reply));
2355 status_t err = onInstantiateSecureDecoders();
2356 reply->setInt32("err", err);
2357 reply->post();
2358 break;
2359 }
2360
Andreas Huber9575c962013-02-05 13:59:56 -08002361 case Source::kWhatPrepared:
2362 {
Hassan Shojaniacefac142017-02-06 21:02:02 -08002363 ALOGV("NuPlayer::onSourceNotify Source::kWhatPrepared source: %p", mSource.get());
Andreas Huberb5f28d42013-04-25 15:11:19 -07002364 if (mSource == NULL) {
2365 // This is a stale notification from a source that was
2366 // asynchronously preparing when the client called reset().
2367 // We handled the reset, the source is gone.
2368 break;
2369 }
2370
Andreas Huberec0c5972013-02-05 14:47:13 -08002371 int32_t err;
2372 CHECK(msg->findInt32("err", &err));
2373
Lajos Molnarfcd3e942015-03-31 10:06:48 -07002374 if (err != OK) {
2375 // shut down potential secure codecs in case client never calls reset
2376 mDeferredActions.push_back(
2377 new FlushDecoderAction(FLUSH_CMD_SHUTDOWN /* audio */,
2378 FLUSH_CMD_SHUTDOWN /* video */));
2379 processDeferredActions();
Wei Jia8a092d32016-06-03 14:57:24 -07002380 } else {
2381 mPrepared = true;
Lajos Molnarfcd3e942015-03-31 10:06:48 -07002382 }
2383
Andreas Huber9575c962013-02-05 13:59:56 -08002384 sp<NuPlayerDriver> driver = mDriver.promote();
2385 if (driver != NULL) {
Marco Nelissendd114d12014-05-28 15:23:14 -07002386 // notify duration first, so that it's definitely set when
2387 // the app received the "prepare complete" callback.
2388 int64_t durationUs;
2389 if (mSource->getDuration(&durationUs) == OK) {
2390 driver->notifyDuration(durationUs);
2391 }
Andreas Huberec0c5972013-02-05 14:47:13 -08002392 driver->notifyPrepareCompleted(err);
Andreas Huber9575c962013-02-05 13:59:56 -08002393 }
Andreas Huber99759402013-04-01 14:28:31 -07002394
Andreas Huber9575c962013-02-05 13:59:56 -08002395 break;
2396 }
2397
Hassan Shojaniacefac142017-02-06 21:02:02 -08002398 // Modular DRM
2399 case Source::kWhatDrmInfo:
2400 {
2401 Parcel parcel;
2402 sp<ABuffer> drmInfo;
2403 CHECK(msg->findBuffer("drmInfo", &drmInfo));
2404 parcel.setData(drmInfo->data(), drmInfo->size());
2405
2406 ALOGV("onSourceNotify() kWhatDrmInfo MEDIA_DRM_INFO drmInfo: %p parcel size: %zu",
2407 drmInfo.get(), parcel.dataSize());
2408
2409 notifyListener(MEDIA_DRM_INFO, 0 /* ext1 */, 0 /* ext2 */, &parcel);
2410
2411 break;
2412 }
2413
Andreas Huber9575c962013-02-05 13:59:56 -08002414 case Source::kWhatFlagsChanged:
2415 {
2416 uint32_t flags;
2417 CHECK(msg->findInt32("flags", (int32_t *)&flags));
2418
Chong Zhang4b7069d2013-09-11 12:52:43 -07002419 sp<NuPlayerDriver> driver = mDriver.promote();
2420 if (driver != NULL) {
Hassan Shojaniacefac142017-02-06 21:02:02 -08002421
2422 ALOGV("onSourceNotify() kWhatFlagsChanged FLAG_CAN_PAUSE: %d "
2423 "FLAG_CAN_SEEK_BACKWARD: %d \n\t\t\t\t FLAG_CAN_SEEK_FORWARD: %d "
2424 "FLAG_CAN_SEEK: %d FLAG_DYNAMIC_DURATION: %d \n"
2425 "\t\t\t\t FLAG_SECURE: %d FLAG_PROTECTED: %d",
2426 (flags & Source::FLAG_CAN_PAUSE) != 0,
2427 (flags & Source::FLAG_CAN_SEEK_BACKWARD) != 0,
2428 (flags & Source::FLAG_CAN_SEEK_FORWARD) != 0,
2429 (flags & Source::FLAG_CAN_SEEK) != 0,
2430 (flags & Source::FLAG_DYNAMIC_DURATION) != 0,
2431 (flags & Source::FLAG_SECURE) != 0,
2432 (flags & Source::FLAG_PROTECTED) != 0);
2433
Wei Jia895651b2014-12-10 17:31:52 -08002434 if ((flags & NuPlayer::Source::FLAG_CAN_SEEK) == 0) {
2435 driver->notifyListener(
2436 MEDIA_INFO, MEDIA_INFO_NOT_SEEKABLE, 0);
2437 }
Chong Zhang4b7069d2013-09-11 12:52:43 -07002438 driver->notifyFlagsChanged(flags);
2439 }
2440
Andreas Huber9575c962013-02-05 13:59:56 -08002441 if ((mSourceFlags & Source::FLAG_DYNAMIC_DURATION)
2442 && (!(flags & Source::FLAG_DYNAMIC_DURATION))) {
2443 cancelPollDuration();
2444 } else if (!(mSourceFlags & Source::FLAG_DYNAMIC_DURATION)
2445 && (flags & Source::FLAG_DYNAMIC_DURATION)
2446 && (mAudioDecoder != NULL || mVideoDecoder != NULL)) {
2447 schedulePollDuration();
2448 }
2449
2450 mSourceFlags = flags;
2451 break;
2452 }
2453
2454 case Source::kWhatVideoSizeChanged:
2455 {
Chong Zhangced1c2f2014-08-08 15:22:35 -07002456 sp<AMessage> format;
2457 CHECK(msg->findMessage("format", &format));
Andreas Huber9575c962013-02-05 13:59:56 -08002458
Chong Zhangced1c2f2014-08-08 15:22:35 -07002459 updateVideoSize(format);
Andreas Huber9575c962013-02-05 13:59:56 -08002460 break;
2461 }
2462
Chong Zhang2a3cc9a2014-08-21 17:48:26 -07002463 case Source::kWhatBufferingUpdate:
2464 {
2465 int32_t percentage;
2466 CHECK(msg->findInt32("percentage", &percentage));
2467
2468 notifyListener(MEDIA_BUFFERING_UPDATE, percentage, 0);
2469 break;
2470 }
2471
Chong Zhangefbb6192015-01-30 17:13:27 -08002472 case Source::kWhatPauseOnBufferingStart:
2473 {
2474 // ignore if not playing
Chong Zhang8a048332015-05-06 15:16:28 -07002475 if (mStarted) {
Chong Zhangefbb6192015-01-30 17:13:27 -08002476 ALOGI("buffer low, pausing...");
2477
Chong Zhang8a048332015-05-06 15:16:28 -07002478 mPausedForBuffering = true;
Chong Zhangefbb6192015-01-30 17:13:27 -08002479 onPause();
2480 }
Wei Jiabfe82072016-05-20 09:21:50 -07002481 notifyListener(MEDIA_INFO, MEDIA_INFO_BUFFERING_START, 0);
Roger Jönssonb50e83e2013-01-21 16:26:41 +01002482 break;
2483 }
2484
Chong Zhangefbb6192015-01-30 17:13:27 -08002485 case Source::kWhatResumeOnBufferingEnd:
2486 {
2487 // ignore if not playing
Chong Zhang8a048332015-05-06 15:16:28 -07002488 if (mStarted) {
Chong Zhangefbb6192015-01-30 17:13:27 -08002489 ALOGI("buffer ready, resuming...");
2490
Chong Zhang8a048332015-05-06 15:16:28 -07002491 mPausedForBuffering = false;
2492
2493 // do not resume yet if client didn't unpause
2494 if (!mPausedByClient) {
2495 onResume();
2496 }
Chong Zhangefbb6192015-01-30 17:13:27 -08002497 }
Wei Jia3bed45a2016-02-17 11:06:47 -08002498 notifyListener(MEDIA_INFO, MEDIA_INFO_BUFFERING_END, 0);
Roger Jönssonb50e83e2013-01-21 16:26:41 +01002499 break;
2500 }
2501
Chong Zhangefbb6192015-01-30 17:13:27 -08002502 case Source::kWhatCacheStats:
2503 {
2504 int32_t kbps;
2505 CHECK(msg->findInt32("bandwidth", &kbps));
2506
2507 notifyListener(MEDIA_INFO, MEDIA_INFO_NETWORK_BANDWIDTH, kbps);
2508 break;
2509 }
2510
Chong Zhangdcb89b32013-08-06 09:44:47 -07002511 case Source::kWhatSubtitleData:
2512 {
2513 sp<ABuffer> buffer;
2514 CHECK(msg->findBuffer("buffer", &buffer));
2515
Chong Zhang404fced2014-06-11 14:45:31 -07002516 sendSubtitleData(buffer, 0 /* baseIndex */);
Chong Zhangdcb89b32013-08-06 09:44:47 -07002517 break;
2518 }
2519
Robert Shih08528432015-04-08 09:06:54 -07002520 case Source::kWhatTimedMetaData:
2521 {
2522 sp<ABuffer> buffer;
2523 if (!msg->findBuffer("buffer", &buffer)) {
2524 notifyListener(MEDIA_INFO, MEDIA_INFO_METADATA_UPDATE, 0);
2525 } else {
2526 sendTimedMetaData(buffer);
2527 }
2528 break;
2529 }
2530
Robert Shihd3b0bbb2014-07-23 15:00:25 -07002531 case Source::kWhatTimedTextData:
2532 {
2533 int32_t generation;
2534 if (msg->findInt32("generation", &generation)
2535 && generation != mTimedTextGeneration) {
2536 break;
2537 }
2538
2539 sp<ABuffer> buffer;
2540 CHECK(msg->findBuffer("buffer", &buffer));
2541
2542 sp<NuPlayerDriver> driver = mDriver.promote();
2543 if (driver == NULL) {
2544 break;
2545 }
2546
2547 int posMs;
2548 int64_t timeUs, posUs;
2549 driver->getCurrentPosition(&posMs);
Patrik2 Carlsson24d484b2015-01-27 16:49:45 +01002550 posUs = (int64_t) posMs * 1000ll;
Robert Shihd3b0bbb2014-07-23 15:00:25 -07002551 CHECK(buffer->meta()->findInt64("timeUs", &timeUs));
2552
2553 if (posUs < timeUs) {
2554 if (!msg->findInt32("generation", &generation)) {
2555 msg->setInt32("generation", mTimedTextGeneration);
2556 }
2557 msg->post(timeUs - posUs);
2558 } else {
2559 sendTimedTextData(buffer);
2560 }
2561 break;
2562 }
2563
Andreas Huber14f76722013-01-15 09:04:18 -08002564 case Source::kWhatQueueDecoderShutdown:
2565 {
2566 int32_t audio, video;
2567 CHECK(msg->findInt32("audio", &audio));
2568 CHECK(msg->findInt32("video", &video));
2569
2570 sp<AMessage> reply;
2571 CHECK(msg->findMessage("reply", &reply));
2572
2573 queueDecoderShutdown(audio, video, reply);
2574 break;
2575 }
2576
Ronghua Wu80276872014-08-28 15:50:29 -07002577 case Source::kWhatDrmNoLicense:
2578 {
2579 notifyListener(MEDIA_ERROR, MEDIA_ERROR_UNKNOWN, ERROR_DRM_NO_LICENSE);
2580 break;
2581 }
2582
Andreas Huber9575c962013-02-05 13:59:56 -08002583 default:
2584 TRESPASS();
2585 }
2586}
2587
Chong Zhanga7fa1d92014-06-11 14:49:23 -07002588void NuPlayer::onClosedCaptionNotify(const sp<AMessage> &msg) {
2589 int32_t what;
2590 CHECK(msg->findInt32("what", &what));
2591
2592 switch (what) {
2593 case NuPlayer::CCDecoder::kWhatClosedCaptionData:
2594 {
2595 sp<ABuffer> buffer;
2596 CHECK(msg->findBuffer("buffer", &buffer));
2597
2598 size_t inbandTracks = 0;
2599 if (mSource != NULL) {
2600 inbandTracks = mSource->getTrackCount();
2601 }
2602
2603 sendSubtitleData(buffer, inbandTracks);
2604 break;
2605 }
2606
2607 case NuPlayer::CCDecoder::kWhatTrackAdded:
2608 {
2609 notifyListener(MEDIA_INFO, MEDIA_INFO_METADATA_UPDATE, 0);
2610
2611 break;
2612 }
2613
2614 default:
2615 TRESPASS();
2616 }
2617
2618
2619}
2620
Chong Zhang404fced2014-06-11 14:45:31 -07002621void NuPlayer::sendSubtitleData(const sp<ABuffer> &buffer, int32_t baseIndex) {
2622 int32_t trackIndex;
2623 int64_t timeUs, durationUs;
2624 CHECK(buffer->meta()->findInt32("trackIndex", &trackIndex));
2625 CHECK(buffer->meta()->findInt64("timeUs", &timeUs));
2626 CHECK(buffer->meta()->findInt64("durationUs", &durationUs));
2627
2628 Parcel in;
2629 in.writeInt32(trackIndex + baseIndex);
2630 in.writeInt64(timeUs);
2631 in.writeInt64(durationUs);
2632 in.writeInt32(buffer->size());
2633 in.writeInt32(buffer->size());
2634 in.write(buffer->data(), buffer->size());
2635
2636 notifyListener(MEDIA_SUBTITLE_DATA, 0, 0, &in);
2637}
Robert Shihd3b0bbb2014-07-23 15:00:25 -07002638
Robert Shih08528432015-04-08 09:06:54 -07002639void NuPlayer::sendTimedMetaData(const sp<ABuffer> &buffer) {
2640 int64_t timeUs;
2641 CHECK(buffer->meta()->findInt64("timeUs", &timeUs));
2642
2643 Parcel in;
2644 in.writeInt64(timeUs);
2645 in.writeInt32(buffer->size());
2646 in.writeInt32(buffer->size());
2647 in.write(buffer->data(), buffer->size());
2648
2649 notifyListener(MEDIA_META_DATA, 0, 0, &in);
2650}
2651
Robert Shihd3b0bbb2014-07-23 15:00:25 -07002652void NuPlayer::sendTimedTextData(const sp<ABuffer> &buffer) {
2653 const void *data;
2654 size_t size = 0;
2655 int64_t timeUs;
Marco Nelissen55e2f4c2015-09-04 15:57:15 -07002656 int32_t flag = TextDescriptions::IN_BAND_TEXT_3GPP;
Robert Shihd3b0bbb2014-07-23 15:00:25 -07002657
2658 AString mime;
2659 CHECK(buffer->meta()->findString("mime", &mime));
2660 CHECK(strcasecmp(mime.c_str(), MEDIA_MIMETYPE_TEXT_3GPP) == 0);
2661
2662 data = buffer->data();
2663 size = buffer->size();
2664
2665 Parcel parcel;
2666 if (size > 0) {
2667 CHECK(buffer->meta()->findInt64("timeUs", &timeUs));
Marco Nelissen55e2f4c2015-09-04 15:57:15 -07002668 int32_t global = 0;
2669 if (buffer->meta()->findInt32("global", &global) && global) {
2670 flag |= TextDescriptions::GLOBAL_DESCRIPTIONS;
2671 } else {
2672 flag |= TextDescriptions::LOCAL_DESCRIPTIONS;
2673 }
Robert Shihd3b0bbb2014-07-23 15:00:25 -07002674 TextDescriptions::getParcelOfDescriptions(
2675 (const uint8_t *)data, size, flag, timeUs / 1000, &parcel);
2676 }
2677
2678 if ((parcel.dataSize() > 0)) {
2679 notifyListener(MEDIA_TIMED_TEXT, 0, 0, &parcel);
2680 } else { // send an empty timed text
2681 notifyListener(MEDIA_TIMED_TEXT, 0, 0);
2682 }
2683}
Hassan Shojaniacefac142017-02-06 21:02:02 -08002684
Hassan Shojaniaff63de72017-04-26 15:10:42 -07002685const char *NuPlayer::getDataSourceType() {
2686 switch (mDataSourceType) {
2687 case DATA_SOURCE_TYPE_HTTP_LIVE:
2688 return "HTTPLive";
2689
2690 case DATA_SOURCE_TYPE_RTSP:
2691 return "RTSP";
2692
2693 case DATA_SOURCE_TYPE_GENERIC_URL:
2694 return "GenURL";
2695
2696 case DATA_SOURCE_TYPE_GENERIC_FD:
2697 return "GenFD";
2698
2699 case DATA_SOURCE_TYPE_MEDIA:
2700 return "Media";
2701
2702 case DATA_SOURCE_TYPE_STREAM:
2703 return "Stream";
2704
2705 case DATA_SOURCE_TYPE_NONE:
2706 default:
2707 return "None";
2708 }
2709 }
2710
Hassan Shojaniacefac142017-02-06 21:02:02 -08002711// Modular DRM begin
2712status_t NuPlayer::prepareDrm(const uint8_t uuid[16], const Vector<uint8_t> &drmSessionId)
2713{
2714 ALOGV("prepareDrm ");
2715
2716 // Passing to the looper anyway; called in a pre-config prepared state so no race on mCrypto
2717 sp<AMessage> msg = new AMessage(kWhatPrepareDrm, this);
2718 // synchronous call so just passing the address but with local copies of "const" args
2719 uint8_t UUID[16];
2720 memcpy(UUID, uuid, sizeof(UUID));
2721 Vector<uint8_t> sessionId = drmSessionId;
2722 msg->setPointer("uuid", (void*)UUID);
2723 msg->setPointer("drmSessionId", (void*)&sessionId);
2724
2725 sp<AMessage> response;
2726 status_t status = msg->postAndAwaitResponse(&response);
2727
2728 if (status == OK && response != NULL) {
2729 CHECK(response->findInt32("status", &status));
2730 ALOGV("prepareDrm ret: %d ", status);
2731 } else {
2732 ALOGE("prepareDrm err: %d", status);
2733 }
2734
2735 return status;
2736}
2737
2738status_t NuPlayer::releaseDrm()
2739{
2740 ALOGV("releaseDrm ");
2741
2742 sp<AMessage> msg = new AMessage(kWhatReleaseDrm, this);
2743
2744 sp<AMessage> response;
2745 status_t status = msg->postAndAwaitResponse(&response);
2746
2747 if (status == OK && response != NULL) {
2748 CHECK(response->findInt32("status", &status));
2749 ALOGV("releaseDrm ret: %d ", status);
2750 } else {
2751 ALOGE("releaseDrm err: %d", status);
2752 }
2753
2754 return status;
2755}
2756
2757status_t NuPlayer::onPrepareDrm(const sp<AMessage> &msg)
2758{
2759 // TODO change to ALOGV
2760 ALOGD("onPrepareDrm ");
2761
2762 status_t status = INVALID_OPERATION;
2763 if (mSource == NULL) {
2764 ALOGE("onPrepareDrm: No source. onPrepareDrm failed with %d.", status);
2765 return status;
2766 }
2767
2768 uint8_t *uuid;
2769 Vector<uint8_t> *drmSessionId;
2770 CHECK(msg->findPointer("uuid", (void**)&uuid));
2771 CHECK(msg->findPointer("drmSessionId", (void**)&drmSessionId));
2772
2773 status = OK;
2774 sp<ICrypto> crypto = NULL;
2775
2776 status = mSource->prepareDrm(uuid, *drmSessionId, &crypto);
2777 if (crypto == NULL) {
2778 ALOGE("onPrepareDrm: mSource->prepareDrm failed. status: %d", status);
2779 return status;
2780 }
2781 ALOGV("onPrepareDrm: mSource->prepareDrm succeeded");
2782
2783 if (mCrypto != NULL) {
2784 ALOGE("onPrepareDrm: Unexpected. Already having mCrypto: %p (%d)",
2785 mCrypto.get(), mCrypto->getStrongCount());
2786 mCrypto.clear();
2787 }
2788
2789 mCrypto = crypto;
2790 mIsDrmProtected = true;
2791 // TODO change to ALOGV
2792 ALOGD("onPrepareDrm: mCrypto: %p (%d)", mCrypto.get(),
2793 (mCrypto != NULL ? mCrypto->getStrongCount() : 0));
2794
2795 return status;
2796}
2797
2798status_t NuPlayer::onReleaseDrm()
2799{
2800 // TODO change to ALOGV
2801 ALOGD("onReleaseDrm ");
2802
Hassan Shojania50b20c92017-02-16 18:28:58 -08002803 if (!mIsDrmProtected) {
2804 ALOGW("onReleaseDrm: Unexpected. mIsDrmProtected is already false.");
2805 }
2806
2807 mIsDrmProtected = false;
Hassan Shojaniacefac142017-02-06 21:02:02 -08002808
2809 status_t status;
2810 if (mCrypto != NULL) {
Hassan Shojania355e8472017-05-12 10:33:16 -07002811 // notifying the source first before removing crypto from codec
2812 if (mSource != NULL) {
2813 mSource->releaseDrm();
2814 }
2815
Hassan Shojaniacefac142017-02-06 21:02:02 -08002816 status=OK;
2817 // first making sure the codecs have released their crypto reference
2818 const sp<DecoderBase> &videoDecoder = getDecoder(false/*audio*/);
2819 if (videoDecoder != NULL) {
2820 status = videoDecoder->releaseCrypto();
2821 ALOGV("onReleaseDrm: video decoder ret: %d", status);
2822 }
2823
2824 const sp<DecoderBase> &audioDecoder = getDecoder(true/*audio*/);
2825 if (audioDecoder != NULL) {
2826 status_t status_audio = audioDecoder->releaseCrypto();
2827 if (status == OK) { // otherwise, returning the first error
2828 status = status_audio;
2829 }
2830 ALOGV("onReleaseDrm: audio decoder ret: %d", status_audio);
2831 }
2832
2833 // TODO change to ALOGV
2834 ALOGD("onReleaseDrm: mCrypto: %p (%d)", mCrypto.get(),
2835 (mCrypto != NULL ? mCrypto->getStrongCount() : 0));
2836 mCrypto.clear();
2837 } else { // mCrypto == NULL
2838 ALOGE("onReleaseDrm: Unexpected. There is no crypto.");
2839 status = INVALID_OPERATION;
2840 }
2841
2842 return status;
2843}
2844// Modular DRM end
Andreas Huberb5f25f02013-02-05 10:14:26 -08002845////////////////////////////////////////////////////////////////////////////////
2846
Chong Zhangced1c2f2014-08-08 15:22:35 -07002847sp<AMessage> NuPlayer::Source::getFormat(bool audio) {
2848 sp<MetaData> meta = getFormatMeta(audio);
2849
2850 if (meta == NULL) {
2851 return NULL;
2852 }
2853
2854 sp<AMessage> msg = new AMessage;
2855
2856 if(convertMetaDataToMessage(meta, &msg) == OK) {
2857 return msg;
2858 }
2859 return NULL;
2860}
2861
Andreas Huber9575c962013-02-05 13:59:56 -08002862void NuPlayer::Source::notifyFlagsChanged(uint32_t flags) {
2863 sp<AMessage> notify = dupNotify();
2864 notify->setInt32("what", kWhatFlagsChanged);
2865 notify->setInt32("flags", flags);
2866 notify->post();
2867}
2868
Chong Zhangced1c2f2014-08-08 15:22:35 -07002869void NuPlayer::Source::notifyVideoSizeChanged(const sp<AMessage> &format) {
Andreas Huber9575c962013-02-05 13:59:56 -08002870 sp<AMessage> notify = dupNotify();
2871 notify->setInt32("what", kWhatVideoSizeChanged);
Chong Zhangced1c2f2014-08-08 15:22:35 -07002872 notify->setMessage("format", format);
Andreas Huber9575c962013-02-05 13:59:56 -08002873 notify->post();
2874}
2875
Andreas Huberec0c5972013-02-05 14:47:13 -08002876void NuPlayer::Source::notifyPrepared(status_t err) {
Hassan Shojaniacefac142017-02-06 21:02:02 -08002877 ALOGV("Source::notifyPrepared %d", err);
Andreas Huber9575c962013-02-05 13:59:56 -08002878 sp<AMessage> notify = dupNotify();
2879 notify->setInt32("what", kWhatPrepared);
Andreas Huberec0c5972013-02-05 14:47:13 -08002880 notify->setInt32("err", err);
Andreas Huber9575c962013-02-05 13:59:56 -08002881 notify->post();
2882}
2883
Hassan Shojaniacefac142017-02-06 21:02:02 -08002884void NuPlayer::Source::notifyDrmInfo(const sp<ABuffer> &drmInfoBuffer)
2885{
2886 ALOGV("Source::notifyDrmInfo");
2887
2888 sp<AMessage> notify = dupNotify();
2889 notify->setInt32("what", kWhatDrmInfo);
2890 notify->setBuffer("drmInfo", drmInfoBuffer);
2891
2892 notify->post();
2893}
2894
Lajos Molnarfcd3e942015-03-31 10:06:48 -07002895void NuPlayer::Source::notifyInstantiateSecureDecoders(const sp<AMessage> &reply) {
2896 sp<AMessage> notify = dupNotify();
2897 notify->setInt32("what", kWhatInstantiateSecureDecoders);
2898 notify->setMessage("reply", reply);
2899 notify->post();
2900}
2901
Andreas Huber84333e02014-02-07 15:36:10 -08002902void NuPlayer::Source::onMessageReceived(const sp<AMessage> & /* msg */) {
Andreas Huberb5f25f02013-02-05 10:14:26 -08002903 TRESPASS();
2904}
2905
Andreas Huberf9334412010-12-15 15:17:42 -08002906} // namespace android