blob: bd83c6d4c4105bc1f82294503450e53110f94125 [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),
Ray Essick0d98c182017-11-09 13:42:42 -0800185 mLastStartedPlayingTimeNs(0),
Ray Essickeda32522018-02-28 12:08:28 -0800186 mLastStartedRebufferingTimeNs(0),
Robert Shih1a5c8592015-08-04 18:07:44 -0700187 mPreviousSeekTimeUs(0),
Andreas Huber9b80c2b2011-06-30 15:47:02 -0700188 mAudioEOS(false),
Andreas Huberf9334412010-12-15 15:17:42 -0800189 mVideoEOS(false),
Andreas Huber5bc087c2010-12-23 10:27:40 -0800190 mScanSourcesPending(false),
Andreas Huber1aef2112011-01-04 14:01:29 -0800191 mScanSourcesGeneration(0),
Andreas Huberb7c8e912012-11-27 15:02:53 -0800192 mPollDurationGeneration(0),
Robert Shihd3b0bbb2014-07-23 15:00:25 -0700193 mTimedTextGeneration(0),
Andreas Huberf9334412010-12-15 15:17:42 -0800194 mFlushingAudio(NONE),
Andreas Huber1aef2112011-01-04 14:01:29 -0800195 mFlushingVideo(NONE),
Chong Zhangf8d71772014-11-26 15:08:34 -0800196 mResumePending(false),
Andreas Huber57a339c2012-12-03 11:18:00 -0800197 mVideoScalingMode(NATIVE_WINDOW_SCALING_MODE_SCALE_TO_WINDOW),
Lajos Molnar3a474aa2015-04-24 17:10:07 -0700198 mPlaybackSettings(AUDIO_PLAYBACK_RATE_DEFAULT),
199 mVideoFpsHint(-1.f),
Chong Zhangefbb6192015-01-30 17:13:27 -0800200 mStarted(false),
Wei Jia8a092d32016-06-03 14:57:24 -0700201 mPrepared(false),
Ronghua Wu64c2d172015-10-07 16:52:19 -0700202 mResetting(false),
Robert Shih0c61a0d2015-07-06 15:09:10 -0700203 mSourceStarted(false),
Wei Jia686e8e52017-04-03 14:08:01 -0700204 mAudioDecoderError(false),
205 mVideoDecoderError(false),
Chong Zhangefbb6192015-01-30 17:13:27 -0800206 mPaused(false),
Wei Jia71c75e02016-02-04 09:40:47 -0800207 mPausedByClient(true),
Hassan Shojania50b20c92017-02-16 18:28:58 -0800208 mPausedForBuffering(false),
Hassan Shojaniaff63de72017-04-26 15:10:42 -0700209 mIsDrmProtected(false),
210 mDataSourceType(DATA_SOURCE_TYPE_NONE) {
Wei Jia0a68f662017-08-30 18:01:26 -0700211 CHECK(mediaClock != NULL);
Andy Hung8d121d42014-10-03 09:53:53 -0700212 clearFlushComplete();
Andreas Huberf9334412010-12-15 15:17:42 -0800213}
214
215NuPlayer::~NuPlayer() {
216}
217
Andreas Huber9b80c2b2011-06-30 15:47:02 -0700218void NuPlayer::setUID(uid_t uid) {
219 mUIDValid = true;
220 mUID = uid;
221}
222
Andreas Huber43c3e6c2011-01-05 12:17:08 -0800223void NuPlayer::setDriver(const wp<NuPlayerDriver> &driver) {
224 mDriver = driver;
Andreas Huberf9334412010-12-15 15:17:42 -0800225}
226
Andreas Huber9575c962013-02-05 13:59:56 -0800227void NuPlayer::setDataSourceAsync(const sp<IStreamSource> &source) {
Lajos Molnar1d15ab52015-03-04 16:46:34 -0800228 sp<AMessage> msg = new AMessage(kWhatSetDataSource, this);
Andreas Huberf9334412010-12-15 15:17:42 -0800229
Lajos Molnar1d15ab52015-03-04 16:46:34 -0800230 sp<AMessage> notify = new AMessage(kWhatSourceNotify, this);
Andreas Huberb5f25f02013-02-05 10:14:26 -0800231
Andreas Huber240abcc2014-02-13 13:32:37 -0800232 msg->setObject("source", new StreamingSource(notify, source));
Andreas Huber5bc087c2010-12-23 10:27:40 -0800233 msg->post();
Hassan Shojaniaff63de72017-04-26 15:10:42 -0700234 mDataSourceType = DATA_SOURCE_TYPE_STREAM;
Andreas Huber5bc087c2010-12-23 10:27:40 -0800235}
Andreas Huberf9334412010-12-15 15:17:42 -0800236
Andreas Huberafed0e12011-09-20 15:39:58 -0700237static bool IsHTTPLiveURL(const char *url) {
238 if (!strncasecmp("http://", url, 7)
Andreas Huber99759402013-04-01 14:28:31 -0700239 || !strncasecmp("https://", url, 8)
240 || !strncasecmp("file://", url, 7)) {
Andreas Huberafed0e12011-09-20 15:39:58 -0700241 size_t len = strlen(url);
242 if (len >= 5 && !strcasecmp(".m3u8", &url[len - 5])) {
243 return true;
244 }
245
246 if (strstr(url,"m3u8")) {
247 return true;
248 }
249 }
250
251 return false;
252}
253
Andreas Huber9575c962013-02-05 13:59:56 -0800254void NuPlayer::setDataSourceAsync(
Andreas Huber1b86fe02014-01-29 11:13:26 -0800255 const sp<IMediaHTTPService> &httpService,
256 const char *url,
257 const KeyedVector<String8, String8> *headers) {
Chong Zhang3de157d2014-08-05 20:54:44 -0700258
Lajos Molnar1d15ab52015-03-04 16:46:34 -0800259 sp<AMessage> msg = new AMessage(kWhatSetDataSource, this);
Oscar Rydhé7a33b772012-02-20 10:15:48 +0100260 size_t len = strlen(url);
Andreas Huber5bc087c2010-12-23 10:27:40 -0800261
Lajos Molnar1d15ab52015-03-04 16:46:34 -0800262 sp<AMessage> notify = new AMessage(kWhatSourceNotify, this);
Andreas Huberb5f25f02013-02-05 10:14:26 -0800263
Andreas Huberafed0e12011-09-20 15:39:58 -0700264 sp<Source> source;
265 if (IsHTTPLiveURL(url)) {
Andreas Huber81e68442014-02-05 11:52:33 -0800266 source = new HTTPLiveSource(notify, httpService, url, headers);
Hassan Shojaniacefac142017-02-06 21:02:02 -0800267 ALOGV("setDataSourceAsync HTTPLiveSource %s", url);
Hassan Shojaniaff63de72017-04-26 15:10:42 -0700268 mDataSourceType = DATA_SOURCE_TYPE_HTTP_LIVE;
Andreas Huberafed0e12011-09-20 15:39:58 -0700269 } else if (!strncasecmp(url, "rtsp://", 7)) {
Andreas Huber1b86fe02014-01-29 11:13:26 -0800270 source = new RTSPSource(
271 notify, httpService, url, headers, mUIDValid, mUID);
Hassan Shojaniacefac142017-02-06 21:02:02 -0800272 ALOGV("setDataSourceAsync RTSPSource %s", url);
Hassan Shojaniaff63de72017-04-26 15:10:42 -0700273 mDataSourceType = DATA_SOURCE_TYPE_RTSP;
Oscar Rydhé7a33b772012-02-20 10:15:48 +0100274 } else if ((!strncasecmp(url, "http://", 7)
275 || !strncasecmp(url, "https://", 8))
276 && ((len >= 4 && !strcasecmp(".sdp", &url[len - 4]))
277 || strstr(url, ".sdp?"))) {
Andreas Huber1b86fe02014-01-29 11:13:26 -0800278 source = new RTSPSource(
279 notify, httpService, url, headers, mUIDValid, mUID, true);
Hassan Shojaniacefac142017-02-06 21:02:02 -0800280 ALOGV("setDataSourceAsync RTSPSource http/https/.sdp %s", url);
Hassan Shojaniaff63de72017-04-26 15:10:42 -0700281 mDataSourceType = DATA_SOURCE_TYPE_RTSP;
Andreas Huber2bfdd422011-10-11 15:24:07 -0700282 } else {
Hassan Shojaniacefac142017-02-06 21:02:02 -0800283 ALOGV("setDataSourceAsync GenericSource %s", url);
284
Chong Zhang3de157d2014-08-05 20:54:44 -0700285 sp<GenericSource> genericSource =
Wei Jia992c5592017-09-01 14:20:23 -0700286 new GenericSource(notify, mUIDValid, mUID, mMediaClock);
Andreas Huber2bfdd422011-10-11 15:24:07 -0700287
Chong Zhanga19f33e2014-08-07 15:35:07 -0700288 status_t err = genericSource->setDataSource(httpService, url, headers);
Chong Zhang3de157d2014-08-05 20:54:44 -0700289
290 if (err == OK) {
291 source = genericSource;
292 } else {
Chong Zhanga19f33e2014-08-07 15:35:07 -0700293 ALOGE("Failed to set data source!");
Chong Zhang3de157d2014-08-05 20:54:44 -0700294 }
Hassan Shojaniaff63de72017-04-26 15:10:42 -0700295
296 // regardless of success/failure
297 mDataSourceType = DATA_SOURCE_TYPE_GENERIC_URL;
Chong Zhang3de157d2014-08-05 20:54:44 -0700298 }
Andreas Huberafed0e12011-09-20 15:39:58 -0700299 msg->setObject("source", source);
300 msg->post();
301}
302
Andreas Huber9575c962013-02-05 13:59:56 -0800303void NuPlayer::setDataSourceAsync(int fd, int64_t offset, int64_t length) {
Lajos Molnar1d15ab52015-03-04 16:46:34 -0800304 sp<AMessage> msg = new AMessage(kWhatSetDataSource, this);
Andreas Huberafed0e12011-09-20 15:39:58 -0700305
Lajos Molnar1d15ab52015-03-04 16:46:34 -0800306 sp<AMessage> notify = new AMessage(kWhatSourceNotify, this);
Andreas Huberb5f25f02013-02-05 10:14:26 -0800307
Chong Zhang3de157d2014-08-05 20:54:44 -0700308 sp<GenericSource> source =
Wei Jia992c5592017-09-01 14:20:23 -0700309 new GenericSource(notify, mUIDValid, mUID, mMediaClock);
Chong Zhang3de157d2014-08-05 20:54:44 -0700310
Hassan Shojaniacefac142017-02-06 21:02:02 -0800311 ALOGV("setDataSourceAsync fd %d/%lld/%lld source: %p",
312 fd, (long long)offset, (long long)length, source.get());
313
Chong Zhanga19f33e2014-08-07 15:35:07 -0700314 status_t err = source->setDataSource(fd, offset, length);
Chong Zhang3de157d2014-08-05 20:54:44 -0700315
316 if (err != OK) {
Chong Zhanga19f33e2014-08-07 15:35:07 -0700317 ALOGE("Failed to set data source!");
Chong Zhang3de157d2014-08-05 20:54:44 -0700318 source = NULL;
319 }
320
Andreas Huberafed0e12011-09-20 15:39:58 -0700321 msg->setObject("source", source);
Andreas Huberf9334412010-12-15 15:17:42 -0800322 msg->post();
Hassan Shojaniaff63de72017-04-26 15:10:42 -0700323 mDataSourceType = DATA_SOURCE_TYPE_GENERIC_FD;
Andreas Huberf9334412010-12-15 15:17:42 -0800324}
325
Chris Watkins99f31602015-03-20 13:06:33 -0700326void NuPlayer::setDataSourceAsync(const sp<DataSource> &dataSource) {
327 sp<AMessage> msg = new AMessage(kWhatSetDataSource, this);
328 sp<AMessage> notify = new AMessage(kWhatSourceNotify, this);
329
Wei Jia992c5592017-09-01 14:20:23 -0700330 sp<GenericSource> source = new GenericSource(notify, mUIDValid, mUID, mMediaClock);
Chris Watkins99f31602015-03-20 13:06:33 -0700331 status_t err = source->setDataSource(dataSource);
332
333 if (err != OK) {
334 ALOGE("Failed to set data source!");
335 source = NULL;
336 }
337
338 msg->setObject("source", source);
339 msg->post();
Hassan Shojaniaff63de72017-04-26 15:10:42 -0700340 mDataSourceType = DATA_SOURCE_TYPE_MEDIA;
Chris Watkins99f31602015-03-20 13:06:33 -0700341}
342
Wei Jia9bb38032017-03-23 18:00:38 -0700343status_t NuPlayer::getBufferingSettings(
Wei Jia48fa06d2016-12-20 15:30:49 -0800344 BufferingSettings *buffering /* nonnull */) {
Wei Jia9bb38032017-03-23 18:00:38 -0700345 sp<AMessage> msg = new AMessage(kWhatGetBufferingSettings, this);
Wei Jia48fa06d2016-12-20 15:30:49 -0800346 sp<AMessage> response;
347 status_t err = msg->postAndAwaitResponse(&response);
348 if (err == OK && response != NULL) {
349 CHECK(response->findInt32("err", &err));
350 if (err == OK) {
351 readFromAMessage(response, buffering);
352 }
353 }
354 return err;
355}
356
357status_t NuPlayer::setBufferingSettings(const BufferingSettings& buffering) {
358 sp<AMessage> msg = new AMessage(kWhatSetBufferingSettings, this);
359 writeToAMessage(msg, buffering);
360 sp<AMessage> response;
361 status_t err = msg->postAndAwaitResponse(&response);
362 if (err == OK && response != NULL) {
363 CHECK(response->findInt32("err", &err));
364 }
365 return err;
366}
367
Andreas Huber9575c962013-02-05 13:59:56 -0800368void NuPlayer::prepareAsync() {
Hassan Shojaniacefac142017-02-06 21:02:02 -0800369 ALOGV("prepareAsync");
370
Lajos Molnar1d15ab52015-03-04 16:46:34 -0800371 (new AMessage(kWhatPrepare, this))->post();
Andreas Huber9575c962013-02-05 13:59:56 -0800372}
373
Andreas Huber57a339c2012-12-03 11:18:00 -0800374void NuPlayer::setVideoSurfaceTextureAsync(
Andy McFadden8ba01022012-12-18 09:46:54 -0800375 const sp<IGraphicBufferProducer> &bufferProducer) {
Lajos Molnar1de1e252015-04-30 18:18:34 -0700376 sp<AMessage> msg = new AMessage(kWhatSetVideoSurface, this);
Andreas Huber57a339c2012-12-03 11:18:00 -0800377
Andy McFadden8ba01022012-12-18 09:46:54 -0800378 if (bufferProducer == NULL) {
Lajos Molnar1de1e252015-04-30 18:18:34 -0700379 msg->setObject("surface", NULL);
Andreas Huber57a339c2012-12-03 11:18:00 -0800380 } else {
Lajos Molnar1de1e252015-04-30 18:18:34 -0700381 msg->setObject("surface", new Surface(bufferProducer, true /* controlledByApp */));
Andreas Huber57a339c2012-12-03 11:18:00 -0800382 }
383
Andreas Huberf9334412010-12-15 15:17:42 -0800384 msg->post();
385}
386
387void NuPlayer::setAudioSink(const sp<MediaPlayerBase::AudioSink> &sink) {
Lajos Molnar1d15ab52015-03-04 16:46:34 -0800388 sp<AMessage> msg = new AMessage(kWhatSetAudioSink, this);
Andreas Huberf9334412010-12-15 15:17:42 -0800389 msg->setObject("sink", sink);
390 msg->post();
391}
392
393void NuPlayer::start() {
Lajos Molnar1d15ab52015-03-04 16:46:34 -0800394 (new AMessage(kWhatStart, this))->post();
Andreas Huberf9334412010-12-15 15:17:42 -0800395}
396
Lajos Molnar3a474aa2015-04-24 17:10:07 -0700397status_t NuPlayer::setPlaybackSettings(const AudioPlaybackRate &rate) {
398 // do some cursory validation of the settings here. audio modes are
399 // only validated when set on the audiosink.
400 if ((rate.mSpeed != 0.f && rate.mSpeed < AUDIO_TIMESTRETCH_SPEED_MIN)
401 || rate.mSpeed > AUDIO_TIMESTRETCH_SPEED_MAX
402 || rate.mPitch < AUDIO_TIMESTRETCH_SPEED_MIN
403 || rate.mPitch > AUDIO_TIMESTRETCH_SPEED_MAX) {
404 return BAD_VALUE;
405 }
406 sp<AMessage> msg = new AMessage(kWhatConfigPlayback, this);
407 writeToAMessage(msg, rate);
408 sp<AMessage> response;
409 status_t err = msg->postAndAwaitResponse(&response);
410 if (err == OK && response != NULL) {
411 CHECK(response->findInt32("err", &err));
412 }
413 return err;
414}
415
416status_t NuPlayer::getPlaybackSettings(AudioPlaybackRate *rate /* nonnull */) {
417 sp<AMessage> msg = new AMessage(kWhatGetPlaybackSettings, this);
418 sp<AMessage> response;
419 status_t err = msg->postAndAwaitResponse(&response);
420 if (err == OK && response != NULL) {
421 CHECK(response->findInt32("err", &err));
422 if (err == OK) {
423 readFromAMessage(response, rate);
424 }
425 }
426 return err;
427}
428
429status_t NuPlayer::setSyncSettings(const AVSyncSettings &sync, float videoFpsHint) {
430 sp<AMessage> msg = new AMessage(kWhatConfigSync, this);
431 writeToAMessage(msg, sync, videoFpsHint);
432 sp<AMessage> response;
433 status_t err = msg->postAndAwaitResponse(&response);
434 if (err == OK && response != NULL) {
435 CHECK(response->findInt32("err", &err));
436 }
437 return err;
438}
439
440status_t NuPlayer::getSyncSettings(
441 AVSyncSettings *sync /* nonnull */, float *videoFps /* nonnull */) {
442 sp<AMessage> msg = new AMessage(kWhatGetSyncSettings, this);
443 sp<AMessage> response;
444 status_t err = msg->postAndAwaitResponse(&response);
445 if (err == OK && response != NULL) {
446 CHECK(response->findInt32("err", &err));
447 if (err == OK) {
448 readFromAMessage(response, sync, videoFps);
449 }
450 }
451 return err;
Wei Jia98160162015-02-04 17:01:11 -0800452}
453
Andreas Huber43c3e6c2011-01-05 12:17:08 -0800454void NuPlayer::pause() {
Lajos Molnar1d15ab52015-03-04 16:46:34 -0800455 (new AMessage(kWhatPause, this))->post();
Andreas Huber43c3e6c2011-01-05 12:17:08 -0800456}
457
Andreas Huber1aef2112011-01-04 14:01:29 -0800458void NuPlayer::resetAsync() {
Wei Jiac45a4b22016-04-15 15:30:23 -0700459 sp<Source> source;
460 {
461 Mutex::Autolock autoLock(mSourceLock);
462 source = mSource;
463 }
464
465 if (source != NULL) {
Chong Zhang48296b72014-09-14 14:28:45 -0700466 // During a reset, the data source might be unresponsive already, we need to
467 // disconnect explicitly so that reads exit promptly.
468 // We can't queue the disconnect request to the looper, as it might be
469 // queued behind a stuck read and never gets processed.
470 // Doing a disconnect outside the looper to allows the pending reads to exit
471 // (either successfully or with error).
Wei Jiac45a4b22016-04-15 15:30:23 -0700472 source->disconnect();
Chong Zhang48296b72014-09-14 14:28:45 -0700473 }
474
Lajos Molnar1d15ab52015-03-04 16:46:34 -0800475 (new AMessage(kWhatReset, this))->post();
Andreas Huber1aef2112011-01-04 14:01:29 -0800476}
477
Wei Jia52c28512017-09-13 18:17:51 -0700478status_t NuPlayer::notifyAt(int64_t mediaTimeUs) {
479 sp<AMessage> notify = new AMessage(kWhatNotifyTime, this);
480 notify->setInt64("timerUs", mediaTimeUs);
481 mMediaClock->addTimer(notify, mediaTimeUs);
482 return OK;
483}
484
Wei Jiac5de0912016-11-18 10:22:14 -0800485void NuPlayer::seekToAsync(int64_t seekTimeUs, MediaPlayerSeekMode mode, bool needNotify) {
Lajos Molnar1d15ab52015-03-04 16:46:34 -0800486 sp<AMessage> msg = new AMessage(kWhatSeek, this);
Andreas Huber43c3e6c2011-01-05 12:17:08 -0800487 msg->setInt64("seekTimeUs", seekTimeUs);
Wei Jiac5de0912016-11-18 10:22:14 -0800488 msg->setInt32("mode", mode);
Wei Jiae427abf2014-09-22 15:21:11 -0700489 msg->setInt32("needNotify", needNotify);
Andreas Huber43c3e6c2011-01-05 12:17:08 -0800490 msg->post();
491}
492
Andreas Huber53df1a42010-12-22 10:03:04 -0800493
Chong Zhang404fced2014-06-11 14:45:31 -0700494void NuPlayer::writeTrackInfo(
Chih-Hung Hsiehe964d4e2016-08-09 14:31:32 -0700495 Parcel* reply, const sp<AMessage>& format) const {
Marco Nelissenc367ca12015-09-15 09:51:59 -0700496 if (format == NULL) {
497 ALOGE("NULL format");
498 return;
499 }
Chong Zhang404fced2014-06-11 14:45:31 -0700500 int32_t trackType;
Marco Nelissen9436e482015-09-15 10:21:53 -0700501 if (!format->findInt32("type", &trackType)) {
Marco Nelissenc367ca12015-09-15 09:51:59 -0700502 ALOGE("no track type");
503 return;
504 }
Chong Zhang404fced2014-06-11 14:45:31 -0700505
Robert Shih755106e2015-04-30 14:36:45 -0700506 AString mime;
Robert Shih2e3a4252015-05-06 10:21:15 -0700507 if (!format->findString("mime", &mime)) {
508 // Java MediaPlayer only uses mimetype for subtitle and timedtext tracks.
509 // If we can't find the mimetype here it means that we wouldn't be needing
510 // the mimetype on the Java end. We still write a placeholder mime to keep the
511 // (de)serialization logic simple.
512 if (trackType == MEDIA_TRACK_TYPE_AUDIO) {
513 mime = "audio/";
514 } else if (trackType == MEDIA_TRACK_TYPE_VIDEO) {
515 mime = "video/";
516 } else {
Marco Nelissenc367ca12015-09-15 09:51:59 -0700517 ALOGE("unknown track type: %d", trackType);
518 return;
Robert Shih2e3a4252015-05-06 10:21:15 -0700519 }
520 }
Robert Shih755106e2015-04-30 14:36:45 -0700521
Chong Zhang404fced2014-06-11 14:45:31 -0700522 AString lang;
Marco Nelissen9436e482015-09-15 10:21:53 -0700523 if (!format->findString("language", &lang)) {
Marco Nelissenc367ca12015-09-15 09:51:59 -0700524 ALOGE("no language");
525 return;
526 }
Chong Zhang404fced2014-06-11 14:45:31 -0700527
528 reply->writeInt32(2); // write something non-zero
529 reply->writeInt32(trackType);
Robert Shih755106e2015-04-30 14:36:45 -0700530 reply->writeString16(String16(mime.c_str()));
Chong Zhang404fced2014-06-11 14:45:31 -0700531 reply->writeString16(String16(lang.c_str()));
532
533 if (trackType == MEDIA_TRACK_TYPE_SUBTITLE) {
Chong Zhang404fced2014-06-11 14:45:31 -0700534 int32_t isAuto, isDefault, isForced;
535 CHECK(format->findInt32("auto", &isAuto));
536 CHECK(format->findInt32("default", &isDefault));
537 CHECK(format->findInt32("forced", &isForced));
538
Chong Zhang404fced2014-06-11 14:45:31 -0700539 reply->writeInt32(isAuto);
540 reply->writeInt32(isDefault);
541 reply->writeInt32(isForced);
542 }
543}
544
Andreas Huberf9334412010-12-15 15:17:42 -0800545void NuPlayer::onMessageReceived(const sp<AMessage> &msg) {
546 switch (msg->what()) {
547 case kWhatSetDataSource:
548 {
Steve Block3856b092011-10-20 11:56:00 +0100549 ALOGV("kWhatSetDataSource");
Andreas Huberf9334412010-12-15 15:17:42 -0800550
551 CHECK(mSource == NULL);
552
Chong Zhang3de157d2014-08-05 20:54:44 -0700553 status_t err = OK;
Andreas Huber5bc087c2010-12-23 10:27:40 -0800554 sp<RefBase> obj;
555 CHECK(msg->findObject("source", &obj));
Chong Zhang3de157d2014-08-05 20:54:44 -0700556 if (obj != NULL) {
Wei Jiac45a4b22016-04-15 15:30:23 -0700557 Mutex::Autolock autoLock(mSourceLock);
Chong Zhang3de157d2014-08-05 20:54:44 -0700558 mSource = static_cast<Source *>(obj.get());
Chong Zhang3de157d2014-08-05 20:54:44 -0700559 } else {
560 err = UNKNOWN_ERROR;
561 }
Andreas Huber9575c962013-02-05 13:59:56 -0800562
563 CHECK(mDriver != NULL);
564 sp<NuPlayerDriver> driver = mDriver.promote();
565 if (driver != NULL) {
Chong Zhang3de157d2014-08-05 20:54:44 -0700566 driver->notifySetDataSourceCompleted(err);
Andreas Huber9575c962013-02-05 13:59:56 -0800567 }
568 break;
569 }
570
Wei Jia9bb38032017-03-23 18:00:38 -0700571 case kWhatGetBufferingSettings:
Wei Jia48fa06d2016-12-20 15:30:49 -0800572 {
573 sp<AReplyToken> replyID;
574 CHECK(msg->senderAwaitsResponse(&replyID));
575
Wei Jia9bb38032017-03-23 18:00:38 -0700576 ALOGV("kWhatGetBufferingSettings");
Wei Jia48fa06d2016-12-20 15:30:49 -0800577 BufferingSettings buffering;
578 status_t err = OK;
579 if (mSource != NULL) {
Wei Jia9bb38032017-03-23 18:00:38 -0700580 err = mSource->getBufferingSettings(&buffering);
Wei Jia48fa06d2016-12-20 15:30:49 -0800581 } else {
582 err = INVALID_OPERATION;
583 }
584 sp<AMessage> response = new AMessage;
585 if (err == OK) {
586 writeToAMessage(response, buffering);
587 }
588 response->setInt32("err", err);
589 response->postReply(replyID);
590 break;
591 }
592
593 case kWhatSetBufferingSettings:
594 {
595 sp<AReplyToken> replyID;
596 CHECK(msg->senderAwaitsResponse(&replyID));
597
598 ALOGV("kWhatSetBufferingSettings");
599 BufferingSettings buffering;
600 readFromAMessage(msg, &buffering);
601 status_t err = OK;
602 if (mSource != NULL) {
603 err = mSource->setBufferingSettings(buffering);
604 } else {
605 err = INVALID_OPERATION;
606 }
607 sp<AMessage> response = new AMessage;
608 response->setInt32("err", err);
609 response->postReply(replyID);
610 break;
611 }
612
Andreas Huber9575c962013-02-05 13:59:56 -0800613 case kWhatPrepare:
614 {
Hassan Shojaniacefac142017-02-06 21:02:02 -0800615 ALOGV("onMessageReceived kWhatPrepare");
616
Andreas Huber9575c962013-02-05 13:59:56 -0800617 mSource->prepareAsync();
Andreas Huberf9334412010-12-15 15:17:42 -0800618 break;
619 }
620
Chong Zhangdcb89b32013-08-06 09:44:47 -0700621 case kWhatGetTrackInfo:
622 {
Lajos Molnar3f274362015-03-05 14:35:41 -0800623 sp<AReplyToken> replyID;
Chong Zhangdcb89b32013-08-06 09:44:47 -0700624 CHECK(msg->senderAwaitsResponse(&replyID));
625
Chong Zhang404fced2014-06-11 14:45:31 -0700626 Parcel* reply;
627 CHECK(msg->findPointer("reply", (void**)&reply));
628
629 size_t inbandTracks = 0;
Chong Zhangdcb89b32013-08-06 09:44:47 -0700630 if (mSource != NULL) {
Chong Zhang404fced2014-06-11 14:45:31 -0700631 inbandTracks = mSource->getTrackCount();
632 }
633
Chong Zhanga7fa1d92014-06-11 14:49:23 -0700634 size_t ccTracks = 0;
635 if (mCCDecoder != NULL) {
636 ccTracks = mCCDecoder->getTrackCount();
637 }
638
Chong Zhang404fced2014-06-11 14:45:31 -0700639 // total track count
Chong Zhanga7fa1d92014-06-11 14:49:23 -0700640 reply->writeInt32(inbandTracks + ccTracks);
Chong Zhang404fced2014-06-11 14:45:31 -0700641
642 // write inband tracks
643 for (size_t i = 0; i < inbandTracks; ++i) {
644 writeTrackInfo(reply, mSource->getTrackInfo(i));
Chong Zhangdcb89b32013-08-06 09:44:47 -0700645 }
646
Chong Zhanga7fa1d92014-06-11 14:49:23 -0700647 // write CC track
648 for (size_t i = 0; i < ccTracks; ++i) {
649 writeTrackInfo(reply, mCCDecoder->getTrackInfo(i));
650 }
651
Chong Zhangdcb89b32013-08-06 09:44:47 -0700652 sp<AMessage> response = new AMessage;
Chong Zhangdcb89b32013-08-06 09:44:47 -0700653 response->postReply(replyID);
654 break;
655 }
656
Robert Shih7c4f0d72014-07-09 18:53:31 -0700657 case kWhatGetSelectedTrack:
658 {
659 status_t err = INVALID_OPERATION;
660 if (mSource != NULL) {
661 err = OK;
662
663 int32_t type32;
664 CHECK(msg->findInt32("type", (int32_t*)&type32));
665 media_track_type type = (media_track_type)type32;
666 ssize_t selectedTrack = mSource->getSelectedTrack(type);
667
668 Parcel* reply;
669 CHECK(msg->findPointer("reply", (void**)&reply));
670 reply->writeInt32(selectedTrack);
671 }
672
673 sp<AMessage> response = new AMessage;
674 response->setInt32("err", err);
675
Lajos Molnar3f274362015-03-05 14:35:41 -0800676 sp<AReplyToken> replyID;
Robert Shih7c4f0d72014-07-09 18:53:31 -0700677 CHECK(msg->senderAwaitsResponse(&replyID));
678 response->postReply(replyID);
679 break;
680 }
681
Chong Zhangdcb89b32013-08-06 09:44:47 -0700682 case kWhatSelectTrack:
683 {
Lajos Molnar3f274362015-03-05 14:35:41 -0800684 sp<AReplyToken> replyID;
Chong Zhangdcb89b32013-08-06 09:44:47 -0700685 CHECK(msg->senderAwaitsResponse(&replyID));
686
Chong Zhang404fced2014-06-11 14:45:31 -0700687 size_t trackIndex;
688 int32_t select;
Robert Shih6ffb1fd2014-10-29 16:24:32 -0700689 int64_t timeUs;
Chong Zhang404fced2014-06-11 14:45:31 -0700690 CHECK(msg->findSize("trackIndex", &trackIndex));
691 CHECK(msg->findInt32("select", &select));
Robert Shih6ffb1fd2014-10-29 16:24:32 -0700692 CHECK(msg->findInt64("timeUs", &timeUs));
Chong Zhang404fced2014-06-11 14:45:31 -0700693
Chong Zhangdcb89b32013-08-06 09:44:47 -0700694 status_t err = INVALID_OPERATION;
Chong Zhang404fced2014-06-11 14:45:31 -0700695
696 size_t inbandTracks = 0;
Chong Zhangdcb89b32013-08-06 09:44:47 -0700697 if (mSource != NULL) {
Chong Zhang404fced2014-06-11 14:45:31 -0700698 inbandTracks = mSource->getTrackCount();
699 }
Chong Zhanga7fa1d92014-06-11 14:49:23 -0700700 size_t ccTracks = 0;
701 if (mCCDecoder != NULL) {
702 ccTracks = mCCDecoder->getTrackCount();
703 }
Chong Zhang404fced2014-06-11 14:45:31 -0700704
705 if (trackIndex < inbandTracks) {
Robert Shih6ffb1fd2014-10-29 16:24:32 -0700706 err = mSource->selectTrack(trackIndex, select, timeUs);
Robert Shihd3b0bbb2014-07-23 15:00:25 -0700707
708 if (!select && err == OK) {
709 int32_t type;
710 sp<AMessage> info = mSource->getTrackInfo(trackIndex);
711 if (info != NULL
712 && info->findInt32("type", &type)
713 && type == MEDIA_TRACK_TYPE_TIMEDTEXT) {
714 ++mTimedTextGeneration;
715 }
716 }
Chong Zhanga7fa1d92014-06-11 14:49:23 -0700717 } else {
718 trackIndex -= inbandTracks;
719
720 if (trackIndex < ccTracks) {
721 err = mCCDecoder->selectTrack(trackIndex, select);
722 }
Chong Zhangdcb89b32013-08-06 09:44:47 -0700723 }
724
725 sp<AMessage> response = new AMessage;
726 response->setInt32("err", err);
727
728 response->postReply(replyID);
729 break;
730 }
731
Andreas Huberb7c8e912012-11-27 15:02:53 -0800732 case kWhatPollDuration:
733 {
734 int32_t generation;
735 CHECK(msg->findInt32("generation", &generation));
736
737 if (generation != mPollDurationGeneration) {
738 // stale
739 break;
740 }
741
742 int64_t durationUs;
743 if (mDriver != NULL && mSource->getDuration(&durationUs) == OK) {
744 sp<NuPlayerDriver> driver = mDriver.promote();
745 if (driver != NULL) {
746 driver->notifyDuration(durationUs);
747 }
748 }
749
750 msg->post(1000000ll); // poll again in a second.
751 break;
752 }
753
Lajos Molnar1de1e252015-04-30 18:18:34 -0700754 case kWhatSetVideoSurface:
Andreas Huberf9334412010-12-15 15:17:42 -0800755 {
Andreas Huberf9334412010-12-15 15:17:42 -0800756
757 sp<RefBase> obj;
Lajos Molnar1de1e252015-04-30 18:18:34 -0700758 CHECK(msg->findObject("surface", &obj));
759 sp<Surface> surface = static_cast<Surface *>(obj.get());
Lajos Molnara81c6222015-07-10 19:17:45 -0700760
761 ALOGD("onSetVideoSurface(%p, %s video decoder)",
762 surface.get(),
Wei Jia6b7d2ed2015-08-10 15:11:54 -0700763 (mSource != NULL && mStarted && mSource->getFormat(false /* audio */) != NULL
Lajos Molnara81c6222015-07-10 19:17:45 -0700764 && mVideoDecoder != NULL) ? "have" : "no");
765
Wei Jia6b7d2ed2015-08-10 15:11:54 -0700766 // Need to check mStarted before calling mSource->getFormat because NuPlayer might
767 // be in preparing state and it could take long time.
768 // When mStarted is true, mSource must have been set.
769 if (mSource == NULL || !mStarted || mSource->getFormat(false /* audio */) == NULL
Lajos Molnara81c6222015-07-10 19:17:45 -0700770 // NOTE: mVideoDecoder's mSurface is always non-null
771 || (mVideoDecoder != NULL && mVideoDecoder->setVideoSurface(surface) == OK)) {
Lajos Molnar1de1e252015-04-30 18:18:34 -0700772 performSetSurface(surface);
Wei Jiafef808d2014-10-31 17:57:05 -0700773 break;
774 }
775
776 mDeferredActions.push_back(
Krystian Turczyn687a5892016-01-20 14:20:35 +0100777 new FlushDecoderAction(
778 (obj != NULL ? FLUSH_CMD_FLUSH : FLUSH_CMD_NONE) /* audio */,
Wei Jiafef808d2014-10-31 17:57:05 -0700779 FLUSH_CMD_SHUTDOWN /* video */));
780
Lajos Molnar1de1e252015-04-30 18:18:34 -0700781 mDeferredActions.push_back(new SetSurfaceAction(surface));
James Dong0d268a32012-08-31 12:18:27 -0700782
Krystian Turczyn687a5892016-01-20 14:20:35 +0100783 if (obj != NULL) {
Wei Jiafef808d2014-10-31 17:57:05 -0700784 if (mStarted) {
Andy Hung73535852014-09-05 11:42:58 -0700785 // Issue a seek to refresh the video screen only if started otherwise
786 // the extractor may not yet be started and will assert.
787 // If the video decoder is not set (perhaps audio only in this case)
788 // do not perform a seek as it is not needed.
Ronghua Wua73d9e02014-10-08 15:13:29 -0700789 int64_t currentPositionUs = 0;
790 if (getCurrentPosition(&currentPositionUs) == OK) {
791 mDeferredActions.push_back(
Wei Jiac5de0912016-11-18 10:22:14 -0800792 new SeekAction(currentPositionUs,
793 MediaPlayerSeekMode::SEEK_PREVIOUS_SYNC /* mode */));
Ronghua Wua73d9e02014-10-08 15:13:29 -0700794 }
Andy Hung73535852014-09-05 11:42:58 -0700795 }
Wei Jiaac428aa2014-09-02 19:01:34 -0700796
Andreas Huber57a339c2012-12-03 11:18:00 -0800797 // If there is a new surface texture, instantiate decoders
798 // again if possible.
799 mDeferredActions.push_back(
800 new SimpleAction(&NuPlayer::performScanSources));
Andreas Huber57a339c2012-12-03 11:18:00 -0800801
Krystian Turczyn687a5892016-01-20 14:20:35 +0100802 // After a flush without shutdown, decoder is paused.
803 // Don't resume it until source seek is done, otherwise it could
804 // start pulling stale data too soon.
805 mDeferredActions.push_back(
806 new ResumeDecoderAction(false /* needNotify */));
807 }
Chong Zhang7137ec72014-11-12 16:41:05 -0800808
Andreas Huber57a339c2012-12-03 11:18:00 -0800809 processDeferredActions();
Andreas Huberf9334412010-12-15 15:17:42 -0800810 break;
811 }
812
813 case kWhatSetAudioSink:
814 {
Steve Block3856b092011-10-20 11:56:00 +0100815 ALOGV("kWhatSetAudioSink");
Andreas Huberf9334412010-12-15 15:17:42 -0800816
817 sp<RefBase> obj;
818 CHECK(msg->findObject("sink", &obj));
819
820 mAudioSink = static_cast<MediaPlayerBase::AudioSink *>(obj.get());
821 break;
822 }
823
824 case kWhatStart:
825 {
Steve Block3856b092011-10-20 11:56:00 +0100826 ALOGV("kWhatStart");
Wei Jia94211742014-10-28 17:09:06 -0700827 if (mStarted) {
Chong Zhang8a048332015-05-06 15:16:28 -0700828 // do not resume yet if the source is still buffering
829 if (!mPausedForBuffering) {
830 onResume();
831 }
Wei Jia94211742014-10-28 17:09:06 -0700832 } else {
833 onStart();
Lajos Molnar09524832014-07-17 14:29:51 -0700834 }
Chong Zhangefbb6192015-01-30 17:13:27 -0800835 mPausedByClient = false;
Andreas Huberf9334412010-12-15 15:17:42 -0800836 break;
837 }
838
Lajos Molnar3a474aa2015-04-24 17:10:07 -0700839 case kWhatConfigPlayback:
Wei Jia98160162015-02-04 17:01:11 -0800840 {
Lajos Molnar3a474aa2015-04-24 17:10:07 -0700841 sp<AReplyToken> replyID;
842 CHECK(msg->senderAwaitsResponse(&replyID));
843 AudioPlaybackRate rate /* sanitized */;
844 readFromAMessage(msg, &rate);
845 status_t err = OK;
Wei Jia98160162015-02-04 17:01:11 -0800846 if (mRenderer != NULL) {
Wei Jiabf70feb2016-02-19 15:47:16 -0800847 // AudioSink allows only 1.f and 0.f for offload mode.
848 // For other speed, switch to non-offload mode.
849 if (mOffloadAudio && ((rate.mSpeed != 0.f && rate.mSpeed != 1.f)
850 || rate.mPitch != 1.f)) {
851 int64_t currentPositionUs;
852 if (getCurrentPosition(&currentPositionUs) != OK) {
853 currentPositionUs = mPreviousSeekTimeUs;
854 }
855
856 // Set mPlaybackSettings so that the new audio decoder can
857 // be created correctly.
858 mPlaybackSettings = rate;
859 if (!mPaused) {
860 mRenderer->pause();
861 }
Wei Jia5031b2f2016-02-25 11:19:31 -0800862 restartAudio(
Wei Jiabf70feb2016-02-19 15:47:16 -0800863 currentPositionUs, true /* forceNonOffload */,
864 true /* needsToCreateAudioDecoder */);
865 if (!mPaused) {
866 mRenderer->resume();
867 }
868 }
869
Lajos Molnar3a474aa2015-04-24 17:10:07 -0700870 err = mRenderer->setPlaybackSettings(rate);
871 }
872 if (err == OK) {
873 if (rate.mSpeed == 0.f) {
874 onPause();
Wei Jia351ce872016-02-10 13:21:59 -0800875 mPausedByClient = true;
Lajos Molnar3a474aa2015-04-24 17:10:07 -0700876 // save all other settings (using non-paused speed)
877 // so we can restore them on start
878 AudioPlaybackRate newRate = rate;
879 newRate.mSpeed = mPlaybackSettings.mSpeed;
880 mPlaybackSettings = newRate;
881 } else { /* rate.mSpeed != 0.f */
Lajos Molnar3a474aa2015-04-24 17:10:07 -0700882 mPlaybackSettings = rate;
Wei Jia8a092d32016-06-03 14:57:24 -0700883 if (mStarted) {
884 // do not resume yet if the source is still buffering
885 if (!mPausedForBuffering) {
886 onResume();
887 }
888 } else if (mPrepared) {
889 onStart();
890 }
891
892 mPausedByClient = false;
Lajos Molnar3a474aa2015-04-24 17:10:07 -0700893 }
Wei Jia98160162015-02-04 17:01:11 -0800894 }
Ronghua Wu8db88132015-04-22 13:51:35 -0700895
896 if (mVideoDecoder != NULL) {
Praveen Chavanbbaa1442016-04-08 13:33:49 -0700897 sp<AMessage> params = new AMessage();
898 params->setFloat("playback-speed", mPlaybackSettings.mSpeed);
899 mVideoDecoder->setParameters(params);
Ronghua Wu8db88132015-04-22 13:51:35 -0700900 }
901
Lajos Molnar3a474aa2015-04-24 17:10:07 -0700902 sp<AMessage> response = new AMessage;
903 response->setInt32("err", err);
904 response->postReply(replyID);
905 break;
906 }
907
908 case kWhatGetPlaybackSettings:
909 {
910 sp<AReplyToken> replyID;
911 CHECK(msg->senderAwaitsResponse(&replyID));
912 AudioPlaybackRate rate = mPlaybackSettings;
913 status_t err = OK;
914 if (mRenderer != NULL) {
915 err = mRenderer->getPlaybackSettings(&rate);
916 }
917 if (err == OK) {
918 // get playback settings used by renderer, as it may be
919 // slightly off due to audiosink not taking small changes.
920 mPlaybackSettings = rate;
921 if (mPaused) {
922 rate.mSpeed = 0.f;
923 }
924 }
925 sp<AMessage> response = new AMessage;
926 if (err == OK) {
927 writeToAMessage(response, rate);
928 }
929 response->setInt32("err", err);
930 response->postReply(replyID);
931 break;
932 }
933
934 case kWhatConfigSync:
935 {
936 sp<AReplyToken> replyID;
937 CHECK(msg->senderAwaitsResponse(&replyID));
938
939 ALOGV("kWhatConfigSync");
940 AVSyncSettings sync;
941 float videoFpsHint;
942 readFromAMessage(msg, &sync, &videoFpsHint);
943 status_t err = OK;
944 if (mRenderer != NULL) {
945 err = mRenderer->setSyncSettings(sync, videoFpsHint);
946 }
947 if (err == OK) {
948 mSyncSettings = sync;
949 mVideoFpsHint = videoFpsHint;
950 }
951 sp<AMessage> response = new AMessage;
952 response->setInt32("err", err);
953 response->postReply(replyID);
954 break;
955 }
956
957 case kWhatGetSyncSettings:
958 {
959 sp<AReplyToken> replyID;
960 CHECK(msg->senderAwaitsResponse(&replyID));
961 AVSyncSettings sync = mSyncSettings;
962 float videoFps = mVideoFpsHint;
963 status_t err = OK;
964 if (mRenderer != NULL) {
965 err = mRenderer->getSyncSettings(&sync, &videoFps);
966 if (err == OK) {
967 mSyncSettings = sync;
968 mVideoFpsHint = videoFps;
969 }
970 }
971 sp<AMessage> response = new AMessage;
972 if (err == OK) {
973 writeToAMessage(response, sync, videoFps);
974 }
975 response->setInt32("err", err);
976 response->postReply(replyID);
Wei Jia98160162015-02-04 17:01:11 -0800977 break;
978 }
979
Andreas Huberf9334412010-12-15 15:17:42 -0800980 case kWhatScanSources:
981 {
Andreas Huber1aef2112011-01-04 14:01:29 -0800982 int32_t generation;
983 CHECK(msg->findInt32("generation", &generation));
984 if (generation != mScanSourcesGeneration) {
985 // Drop obsolete msg.
986 break;
987 }
988
Andreas Huber5bc087c2010-12-23 10:27:40 -0800989 mScanSourcesPending = false;
990
Steve Block3856b092011-10-20 11:56:00 +0100991 ALOGV("scanning sources haveAudio=%d, haveVideo=%d",
Andreas Huber43c3e6c2011-01-05 12:17:08 -0800992 mAudioDecoder != NULL, mVideoDecoder != NULL);
993
Andreas Huberb7c8e912012-11-27 15:02:53 -0800994 bool mHadAnySourcesBefore =
995 (mAudioDecoder != NULL) || (mVideoDecoder != NULL);
Robert Shih7350b052015-10-01 15:50:14 -0700996 bool rescan = false;
Andreas Huberb7c8e912012-11-27 15:02:53 -0800997
Andy Hung282a7e32014-08-14 15:56:34 -0700998 // initialize video before audio because successful initialization of
999 // video may change deep buffer mode of audio.
Lajos Molnar1de1e252015-04-30 18:18:34 -07001000 if (mSurface != NULL) {
Robert Shih7350b052015-10-01 15:50:14 -07001001 if (instantiateDecoder(false, &mVideoDecoder) == -EWOULDBLOCK) {
1002 rescan = true;
1003 }
Haynes Mathew George5d246ef2012-07-09 10:36:57 -07001004 }
Andreas Huberf9334412010-12-15 15:17:42 -08001005
Ronghua Wua10fd232014-11-06 16:15:20 -08001006 // Don't try to re-open audio sink if there's an existing decoder.
1007 if (mAudioSink != NULL && mAudioDecoder == NULL) {
Robert Shih7350b052015-10-01 15:50:14 -07001008 if (instantiateDecoder(true, &mAudioDecoder) == -EWOULDBLOCK) {
1009 rescan = true;
1010 }
Andreas Huberf9334412010-12-15 15:17:42 -08001011 }
1012
Andreas Huberb7c8e912012-11-27 15:02:53 -08001013 if (!mHadAnySourcesBefore
1014 && (mAudioDecoder != NULL || mVideoDecoder != NULL)) {
1015 // This is the first time we've found anything playable.
1016
Andreas Huber9575c962013-02-05 13:59:56 -08001017 if (mSourceFlags & Source::FLAG_DYNAMIC_DURATION) {
Andreas Huberb7c8e912012-11-27 15:02:53 -08001018 schedulePollDuration();
1019 }
1020 }
1021
Andreas Hubereac68ba2011-09-27 12:12:25 -07001022 status_t err;
1023 if ((err = mSource->feedMoreTSData()) != OK) {
Andreas Huber1aef2112011-01-04 14:01:29 -08001024 if (mAudioDecoder == NULL && mVideoDecoder == NULL) {
1025 // We're not currently decoding anything (no audio or
1026 // video tracks found) and we just ran out of input data.
Andreas Hubereac68ba2011-09-27 12:12:25 -07001027
1028 if (err == ERROR_END_OF_STREAM) {
1029 notifyListener(MEDIA_PLAYBACK_COMPLETE, 0, 0);
1030 } else {
1031 notifyListener(MEDIA_ERROR, MEDIA_ERROR_UNKNOWN, err);
1032 }
Andreas Huber1aef2112011-01-04 14:01:29 -08001033 }
Andreas Huberf9334412010-12-15 15:17:42 -08001034 break;
1035 }
1036
Robert Shih7350b052015-10-01 15:50:14 -07001037 if (rescan) {
Andreas Huberf9334412010-12-15 15:17:42 -08001038 msg->post(100000ll);
Andreas Huber5bc087c2010-12-23 10:27:40 -08001039 mScanSourcesPending = true;
Andreas Huberf9334412010-12-15 15:17:42 -08001040 }
1041 break;
1042 }
1043
1044 case kWhatVideoNotify:
1045 case kWhatAudioNotify:
1046 {
1047 bool audio = msg->what() == kWhatAudioNotify;
1048
Wei Jia88703c32014-08-06 11:24:07 -07001049 int32_t currentDecoderGeneration =
1050 (audio? mAudioDecoderGeneration : mVideoDecoderGeneration);
1051 int32_t requesterGeneration = currentDecoderGeneration - 1;
1052 CHECK(msg->findInt32("generation", &requesterGeneration));
1053
1054 if (requesterGeneration != currentDecoderGeneration) {
1055 ALOGV("got message from old %s decoder, generation(%d:%d)",
1056 audio ? "audio" : "video", requesterGeneration,
1057 currentDecoderGeneration);
1058 sp<AMessage> reply;
1059 if (!(msg->findMessage("reply", &reply))) {
1060 return;
1061 }
1062
1063 reply->setInt32("err", INFO_DISCONTINUITY);
1064 reply->post();
1065 return;
1066 }
1067
Andreas Huberf9334412010-12-15 15:17:42 -08001068 int32_t what;
Lajos Molnar1cd13982014-01-17 15:12:51 -08001069 CHECK(msg->findInt32("what", &what));
Andreas Huberf9334412010-12-15 15:17:42 -08001070
Chong Zhang7137ec72014-11-12 16:41:05 -08001071 if (what == DecoderBase::kWhatInputDiscontinuity) {
1072 int32_t formatChange;
1073 CHECK(msg->findInt32("formatChange", &formatChange));
Andreas Huberf9334412010-12-15 15:17:42 -08001074
Chong Zhang7137ec72014-11-12 16:41:05 -08001075 ALOGV("%s discontinuity: formatChange %d",
1076 audio ? "audio" : "video", formatChange);
1077
1078 if (formatChange) {
1079 mDeferredActions.push_back(
1080 new FlushDecoderAction(
1081 audio ? FLUSH_CMD_SHUTDOWN : FLUSH_CMD_NONE,
1082 audio ? FLUSH_CMD_NONE : FLUSH_CMD_SHUTDOWN));
Andreas Huberf9334412010-12-15 15:17:42 -08001083 }
Chong Zhang7137ec72014-11-12 16:41:05 -08001084
1085 mDeferredActions.push_back(
1086 new SimpleAction(
1087 &NuPlayer::performScanSources));
1088
1089 processDeferredActions();
1090 } else if (what == DecoderBase::kWhatEOS) {
Andreas Huberdc9bacd2011-09-26 10:53:29 -07001091 int32_t err;
Lajos Molnar1cd13982014-01-17 15:12:51 -08001092 CHECK(msg->findInt32("err", &err));
Andreas Huberdc9bacd2011-09-26 10:53:29 -07001093
1094 if (err == ERROR_END_OF_STREAM) {
Steve Block3856b092011-10-20 11:56:00 +01001095 ALOGV("got %s decoder EOS", audio ? "audio" : "video");
Andreas Huberdc9bacd2011-09-26 10:53:29 -07001096 } else {
Steve Block3856b092011-10-20 11:56:00 +01001097 ALOGV("got %s decoder EOS w/ error %d",
Andreas Huberdc9bacd2011-09-26 10:53:29 -07001098 audio ? "audio" : "video",
1099 err);
1100 }
1101
1102 mRenderer->queueEOS(audio, err);
Chong Zhang7137ec72014-11-12 16:41:05 -08001103 } else if (what == DecoderBase::kWhatFlushCompleted) {
Steve Block3856b092011-10-20 11:56:00 +01001104 ALOGV("decoder %s flush completed", audio ? "audio" : "video");
Andreas Huberf9334412010-12-15 15:17:42 -08001105
Andy Hung8d121d42014-10-03 09:53:53 -07001106 handleFlushComplete(audio, true /* isDecoder */);
Andreas Huber3831a062010-12-21 10:22:33 -08001107 finishFlushIfPossible();
Chong Zhang7137ec72014-11-12 16:41:05 -08001108 } else if (what == DecoderBase::kWhatVideoSizeChanged) {
Lajos Molnar1cd13982014-01-17 15:12:51 -08001109 sp<AMessage> format;
1110 CHECK(msg->findMessage("format", &format));
1111
Wei Jiac6cfd702014-11-11 16:33:20 -08001112 sp<AMessage> inputFormat =
1113 mSource->getFormat(false /* audio */);
Andreas Huber3831a062010-12-21 10:22:33 -08001114
Bartosz Dolewski26936f72014-12-11 12:47:08 +01001115 setVideoScalingMode(mVideoScalingMode);
Wei Jiac6cfd702014-11-11 16:33:20 -08001116 updateVideoSize(inputFormat, format);
Chong Zhang7137ec72014-11-12 16:41:05 -08001117 } else if (what == DecoderBase::kWhatShutdownCompleted) {
Steve Block3856b092011-10-20 11:56:00 +01001118 ALOGV("%s shutdown completed", audio ? "audio" : "video");
Andreas Huber3831a062010-12-21 10:22:33 -08001119 if (audio) {
1120 mAudioDecoder.clear();
Wei Jia686e8e52017-04-03 14:08:01 -07001121 mAudioDecoderError = false;
Wei Jia57568df2014-09-22 10:16:29 -07001122 ++mAudioDecoderGeneration;
Andreas Huber3831a062010-12-21 10:22:33 -08001123
1124 CHECK_EQ((int)mFlushingAudio, (int)SHUTTING_DOWN_DECODER);
1125 mFlushingAudio = SHUT_DOWN;
1126 } else {
1127 mVideoDecoder.clear();
Wei Jia686e8e52017-04-03 14:08:01 -07001128 mVideoDecoderError = false;
Wei Jia57568df2014-09-22 10:16:29 -07001129 ++mVideoDecoderGeneration;
Andreas Huber3831a062010-12-21 10:22:33 -08001130
1131 CHECK_EQ((int)mFlushingVideo, (int)SHUTTING_DOWN_DECODER);
1132 mFlushingVideo = SHUT_DOWN;
1133 }
1134
1135 finishFlushIfPossible();
Chong Zhangf8d71772014-11-26 15:08:34 -08001136 } else if (what == DecoderBase::kWhatResumeCompleted) {
1137 finishResume();
Chong Zhang7137ec72014-11-12 16:41:05 -08001138 } else if (what == DecoderBase::kWhatError) {
Chong Zhangf4c0a942014-08-11 15:14:10 -07001139 status_t err;
Andy Hung2abde2c2014-09-30 14:40:32 -07001140 if (!msg->findInt32("err", &err) || err == OK) {
Chong Zhangf4c0a942014-08-11 15:14:10 -07001141 err = UNKNOWN_ERROR;
1142 }
Andy Hungcf31f1e2014-09-23 14:59:01 -07001143
Andy Hung2abde2c2014-09-30 14:40:32 -07001144 // Decoder errors can be due to Source (e.g. from streaming),
1145 // or from decoding corrupted bitstreams, or from other decoder
1146 // MediaCodec operations (e.g. from an ongoing reset or seek).
Andy Hung202bce12014-12-03 11:47:36 -08001147 // They may also be due to openAudioSink failure at
1148 // decoder start or after a format change.
Andy Hung2abde2c2014-09-30 14:40:32 -07001149 //
1150 // We try to gracefully shut down the affected decoder if possible,
1151 // rather than trying to force the shutdown with something
1152 // similar to performReset(). This method can lead to a hang
1153 // if MediaCodec functions block after an error, but they should
1154 // typically return INVALID_OPERATION instead of blocking.
1155
1156 FlushStatus *flushing = audio ? &mFlushingAudio : &mFlushingVideo;
1157 ALOGE("received error(%#x) from %s decoder, flushing(%d), now shutting down",
1158 err, audio ? "audio" : "video", *flushing);
1159
1160 switch (*flushing) {
1161 case NONE:
1162 mDeferredActions.push_back(
Wei Jiafef808d2014-10-31 17:57:05 -07001163 new FlushDecoderAction(
1164 audio ? FLUSH_CMD_SHUTDOWN : FLUSH_CMD_NONE,
1165 audio ? FLUSH_CMD_NONE : FLUSH_CMD_SHUTDOWN));
Andy Hung2abde2c2014-09-30 14:40:32 -07001166 processDeferredActions();
1167 break;
1168 case FLUSHING_DECODER:
1169 *flushing = FLUSHING_DECODER_SHUTDOWN; // initiate shutdown after flush.
1170 break; // Wait for flush to complete.
1171 case FLUSHING_DECODER_SHUTDOWN:
1172 break; // Wait for flush to complete.
1173 case SHUTTING_DOWN_DECODER:
1174 break; // Wait for shutdown to complete.
1175 case FLUSHED:
Andy Hung2abde2c2014-09-30 14:40:32 -07001176 getDecoder(audio)->initiateShutdown(); // In the middle of a seek.
1177 *flushing = SHUTTING_DOWN_DECODER; // Shut down.
1178 break;
1179 case SHUT_DOWN:
1180 finishFlushIfPossible(); // Should not occur.
1181 break; // Finish anyways.
Marco Nelissen9e2b7912014-08-18 16:13:03 -07001182 }
Wei Jia686e8e52017-04-03 14:08:01 -07001183 if (mSource != nullptr) {
1184 if (audio) {
Wei Jia2f6d8c52017-04-11 09:50:31 -07001185 if (mVideoDecoderError || mSource->getFormat(false /* audio */) == NULL
Wei Jiaf86731d2017-07-11 17:24:34 -07001186 || mSurface == NULL || mVideoDecoder == NULL) {
Wei Jia686e8e52017-04-03 14:08:01 -07001187 // When both audio and video have error, or this stream has only audio
1188 // which has error, notify client of error.
1189 notifyListener(MEDIA_ERROR, MEDIA_ERROR_UNKNOWN, err);
1190 } else {
1191 // Only audio track has error. Video track could be still good to play.
1192 notifyListener(MEDIA_INFO, MEDIA_INFO_PLAY_AUDIO_ERROR, err);
1193 }
1194 mAudioDecoderError = true;
1195 } else {
Wei Jia2f6d8c52017-04-11 09:50:31 -07001196 if (mAudioDecoderError || mSource->getFormat(true /* audio */) == NULL
Wei Jiaf86731d2017-07-11 17:24:34 -07001197 || mAudioSink == NULL || mAudioDecoder == NULL) {
Wei Jia686e8e52017-04-03 14:08:01 -07001198 // When both audio and video have error, or this stream has only video
1199 // which has error, notify client of error.
1200 notifyListener(MEDIA_ERROR, MEDIA_ERROR_UNKNOWN, err);
1201 } else {
1202 // Only video track has error. Audio track could be still good to play.
1203 notifyListener(MEDIA_INFO, MEDIA_INFO_PLAY_VIDEO_ERROR, err);
1204 }
1205 mVideoDecoderError = true;
1206 }
1207 }
Lajos Molnar1cd13982014-01-17 15:12:51 -08001208 } else {
1209 ALOGV("Unhandled decoder notification %d '%c%c%c%c'.",
Andreas Hubera1f8ab02012-11-30 10:53:22 -08001210 what,
1211 what >> 24,
1212 (what >> 16) & 0xff,
1213 (what >> 8) & 0xff,
1214 what & 0xff);
Andreas Huberf9334412010-12-15 15:17:42 -08001215 }
1216
1217 break;
1218 }
1219
1220 case kWhatRendererNotify:
1221 {
Wei Jia57568df2014-09-22 10:16:29 -07001222 int32_t requesterGeneration = mRendererGeneration - 1;
1223 CHECK(msg->findInt32("generation", &requesterGeneration));
1224 if (requesterGeneration != mRendererGeneration) {
1225 ALOGV("got message from old renderer, generation(%d:%d)",
1226 requesterGeneration, mRendererGeneration);
1227 return;
1228 }
1229
Andreas Huberf9334412010-12-15 15:17:42 -08001230 int32_t what;
1231 CHECK(msg->findInt32("what", &what));
1232
1233 if (what == Renderer::kWhatEOS) {
1234 int32_t audio;
1235 CHECK(msg->findInt32("audio", &audio));
1236
Andreas Huberc92fd242011-08-16 13:48:44 -07001237 int32_t finalResult;
1238 CHECK(msg->findInt32("finalResult", &finalResult));
1239
Andreas Huberf9334412010-12-15 15:17:42 -08001240 if (audio) {
1241 mAudioEOS = true;
1242 } else {
1243 mVideoEOS = true;
1244 }
1245
Andreas Huberc92fd242011-08-16 13:48:44 -07001246 if (finalResult == ERROR_END_OF_STREAM) {
Steve Block3856b092011-10-20 11:56:00 +01001247 ALOGV("reached %s EOS", audio ? "audio" : "video");
Andreas Huberc92fd242011-08-16 13:48:44 -07001248 } else {
Steve Block29357bc2012-01-06 19:20:56 +00001249 ALOGE("%s track encountered an error (%d)",
Andreas Huberc92fd242011-08-16 13:48:44 -07001250 audio ? "audio" : "video", finalResult);
1251
1252 notifyListener(
1253 MEDIA_ERROR, MEDIA_ERROR_UNKNOWN, finalResult);
1254 }
Andreas Huberf9334412010-12-15 15:17:42 -08001255
1256 if ((mAudioEOS || mAudioDecoder == NULL)
1257 && (mVideoEOS || mVideoDecoder == NULL)) {
1258 notifyListener(MEDIA_PLAYBACK_COMPLETE, 0, 0);
1259 }
Andreas Huber3fe62152011-09-16 15:09:22 -07001260 } else if (what == Renderer::kWhatFlushComplete) {
Andreas Huberf9334412010-12-15 15:17:42 -08001261 int32_t audio;
1262 CHECK(msg->findInt32("audio", &audio));
1263
Wei Jia3261f0d2015-10-08 13:58:33 -07001264 if (audio) {
1265 mAudioEOS = false;
1266 } else {
1267 mVideoEOS = false;
1268 }
1269
Steve Block3856b092011-10-20 11:56:00 +01001270 ALOGV("renderer %s flush completed.", audio ? "audio" : "video");
Wei Jiada4252f2015-07-14 18:15:28 -07001271 if (audio && (mFlushingAudio == NONE || mFlushingAudio == FLUSHED
1272 || mFlushingAudio == SHUT_DOWN)) {
1273 // Flush has been handled by tear down.
1274 break;
1275 }
Andy Hung8d121d42014-10-03 09:53:53 -07001276 handleFlushComplete(audio, false /* isDecoder */);
1277 finishFlushIfPossible();
James Dongf57b4ea2012-07-20 13:38:36 -07001278 } else if (what == Renderer::kWhatVideoRenderingStart) {
1279 notifyListener(MEDIA_INFO, MEDIA_INFO_RENDERING_START, 0);
Lajos Molnarcbaffcf2013-08-14 18:30:38 -07001280 } else if (what == Renderer::kWhatMediaRenderingStart) {
1281 ALOGV("media rendering started");
1282 notifyListener(MEDIA_STARTED, 0, 0);
Ronghua Wufaeb0f22015-05-21 12:20:21 -07001283 } else if (what == Renderer::kWhatAudioTearDown) {
Ronghua Wu08529172014-10-02 16:55:52 -07001284 int32_t reason;
1285 CHECK(msg->findInt32("reason", &reason));
Ronghua Wufaeb0f22015-05-21 12:20:21 -07001286 ALOGV("Tear down audio with reason %d.", reason);
Wei Jia8456ddd2016-04-22 14:46:43 -07001287 if (reason == Renderer::kDueToTimeout && !(mPaused && mOffloadAudio)) {
1288 // TimeoutWhenPaused is only for offload mode.
1289 ALOGW("Receive a stale message for teardown.");
1290 break;
1291 }
Wei Jiada4252f2015-07-14 18:15:28 -07001292 int64_t positionUs;
Robert Shih1a5c8592015-08-04 18:07:44 -07001293 if (!msg->findInt64("positionUs", &positionUs)) {
1294 positionUs = mPreviousSeekTimeUs;
1295 }
Ronghua Wufaeb0f22015-05-21 12:20:21 -07001296
Wei Jia5031b2f2016-02-25 11:19:31 -08001297 restartAudio(
Wei Jiaa05f1e32016-03-25 16:31:22 -07001298 positionUs, reason == Renderer::kForceNonOffload /* forceNonOffload */,
1299 reason != Renderer::kDueToTimeout /* needsToCreateAudioDecoder */);
Andreas Huberf9334412010-12-15 15:17:42 -08001300 }
1301 break;
1302 }
1303
1304 case kWhatMoreDataQueued:
1305 {
1306 break;
1307 }
1308
Andreas Huber1aef2112011-01-04 14:01:29 -08001309 case kWhatReset:
1310 {
Steve Block3856b092011-10-20 11:56:00 +01001311 ALOGV("kWhatReset");
Andreas Huber1aef2112011-01-04 14:01:29 -08001312
Ronghua Wu64c2d172015-10-07 16:52:19 -07001313 mResetting = true;
Ray Essickeda32522018-02-28 12:08:28 -08001314 updatePlaybackTimer(true /* stopping */, "kWhatReset");
1315 updateRebufferingTimer(true /* stopping */, true /* exiting */);
Ronghua Wu64c2d172015-10-07 16:52:19 -07001316
Andreas Hubera1f8ab02012-11-30 10:53:22 -08001317 mDeferredActions.push_back(
Wei Jiafef808d2014-10-31 17:57:05 -07001318 new FlushDecoderAction(
1319 FLUSH_CMD_SHUTDOWN /* audio */,
1320 FLUSH_CMD_SHUTDOWN /* video */));
Andreas Huberb7c8e912012-11-27 15:02:53 -08001321
Andreas Hubera1f8ab02012-11-30 10:53:22 -08001322 mDeferredActions.push_back(
1323 new SimpleAction(&NuPlayer::performReset));
Andreas Huberb58ce9f2011-11-28 16:27:35 -08001324
Andreas Hubera1f8ab02012-11-30 10:53:22 -08001325 processDeferredActions();
Andreas Huber1aef2112011-01-04 14:01:29 -08001326 break;
1327 }
1328
Wei Jia52c28512017-09-13 18:17:51 -07001329 case kWhatNotifyTime:
1330 {
1331 ALOGV("kWhatNotifyTime");
1332 int64_t timerUs;
1333 CHECK(msg->findInt64("timerUs", &timerUs));
1334
1335 notifyListener(MEDIA_NOTIFY_TIME, timerUs, 0);
1336 break;
1337 }
1338
Andreas Huber43c3e6c2011-01-05 12:17:08 -08001339 case kWhatSeek:
1340 {
1341 int64_t seekTimeUs;
Wei Jiac5de0912016-11-18 10:22:14 -08001342 int32_t mode;
Wei Jiae427abf2014-09-22 15:21:11 -07001343 int32_t needNotify;
Andreas Huber43c3e6c2011-01-05 12:17:08 -08001344 CHECK(msg->findInt64("seekTimeUs", &seekTimeUs));
Wei Jiac5de0912016-11-18 10:22:14 -08001345 CHECK(msg->findInt32("mode", &mode));
Wei Jiae427abf2014-09-22 15:21:11 -07001346 CHECK(msg->findInt32("needNotify", &needNotify));
Andreas Huber43c3e6c2011-01-05 12:17:08 -08001347
Wei Jiac5de0912016-11-18 10:22:14 -08001348 ALOGV("kWhatSeek seekTimeUs=%lld us, mode=%d, needNotify=%d",
1349 (long long)seekTimeUs, mode, needNotify);
Andreas Huber43c3e6c2011-01-05 12:17:08 -08001350
Wei Jia1061c9c2015-05-19 16:02:17 -07001351 if (!mStarted) {
1352 // Seek before the player is started. In order to preview video,
1353 // need to start the player and pause it. This branch is called
1354 // only once if needed. After the player is started, any seek
1355 // operation will go through normal path.
Robert Shih0c61a0d2015-07-06 15:09:10 -07001356 // Audio-only cases are handled separately.
Wei Jiac5de0912016-11-18 10:22:14 -08001357 onStart(seekTimeUs, (MediaPlayerSeekMode)mode);
Robert Shih0c61a0d2015-07-06 15:09:10 -07001358 if (mStarted) {
1359 onPause();
1360 mPausedByClient = true;
1361 }
Wei Jia1061c9c2015-05-19 16:02:17 -07001362 if (needNotify) {
1363 notifyDriverSeekComplete();
1364 }
1365 break;
1366 }
1367
Andreas Hubera1f8ab02012-11-30 10:53:22 -08001368 mDeferredActions.push_back(
Wei Jiafef808d2014-10-31 17:57:05 -07001369 new FlushDecoderAction(FLUSH_CMD_FLUSH /* audio */,
1370 FLUSH_CMD_FLUSH /* video */));
Andreas Huber43c3e6c2011-01-05 12:17:08 -08001371
Wei Jiae427abf2014-09-22 15:21:11 -07001372 mDeferredActions.push_back(
Wei Jiac5de0912016-11-18 10:22:14 -08001373 new SeekAction(seekTimeUs, (MediaPlayerSeekMode)mode));
Andreas Huber43c3e6c2011-01-05 12:17:08 -08001374
Chong Zhangf8d71772014-11-26 15:08:34 -08001375 // After a flush without shutdown, decoder is paused.
1376 // Don't resume it until source seek is done, otherwise it could
Chong Zhang7137ec72014-11-12 16:41:05 -08001377 // start pulling stale data too soon.
1378 mDeferredActions.push_back(
Chong Zhangf8d71772014-11-26 15:08:34 -08001379 new ResumeDecoderAction(needNotify));
Chong Zhang7137ec72014-11-12 16:41:05 -08001380
Andreas Hubera1f8ab02012-11-30 10:53:22 -08001381 processDeferredActions();
Andreas Huber43c3e6c2011-01-05 12:17:08 -08001382 break;
1383 }
1384
Andreas Huberb4082222011-01-20 15:23:04 -08001385 case kWhatPause:
1386 {
Chong Zhangefbb6192015-01-30 17:13:27 -08001387 onPause();
1388 mPausedByClient = true;
Andreas Huberb4082222011-01-20 15:23:04 -08001389 break;
1390 }
1391
Andreas Huberb5f25f02013-02-05 10:14:26 -08001392 case kWhatSourceNotify:
1393 {
Andreas Huber9575c962013-02-05 13:59:56 -08001394 onSourceNotify(msg);
Andreas Huberb5f25f02013-02-05 10:14:26 -08001395 break;
1396 }
1397
Chong Zhanga7fa1d92014-06-11 14:49:23 -07001398 case kWhatClosedCaptionNotify:
1399 {
1400 onClosedCaptionNotify(msg);
1401 break;
1402 }
1403
Hassan Shojaniacefac142017-02-06 21:02:02 -08001404 case kWhatPrepareDrm:
1405 {
1406 status_t status = onPrepareDrm(msg);
1407
1408 sp<AMessage> response = new AMessage;
1409 response->setInt32("status", status);
1410 sp<AReplyToken> replyID;
1411 CHECK(msg->senderAwaitsResponse(&replyID));
1412 response->postReply(replyID);
1413 break;
1414 }
1415
1416 case kWhatReleaseDrm:
1417 {
1418 status_t status = onReleaseDrm();
1419
1420 sp<AMessage> response = new AMessage;
1421 response->setInt32("status", status);
1422 sp<AReplyToken> replyID;
1423 CHECK(msg->senderAwaitsResponse(&replyID));
1424 response->postReply(replyID);
1425 break;
1426 }
1427
Andreas Huberf9334412010-12-15 15:17:42 -08001428 default:
1429 TRESPASS();
1430 break;
1431 }
1432}
1433
Wei Jia94211742014-10-28 17:09:06 -07001434void NuPlayer::onResume() {
Ronghua Wu64c2d172015-10-07 16:52:19 -07001435 if (!mPaused || mResetting) {
1436 ALOGD_IF(mResetting, "resetting, onResume discarded");
Chong Zhangefbb6192015-01-30 17:13:27 -08001437 return;
1438 }
1439 mPaused = false;
Wei Jia94211742014-10-28 17:09:06 -07001440 if (mSource != NULL) {
1441 mSource->resume();
1442 } else {
1443 ALOGW("resume called when source is gone or not set");
1444 }
1445 // |mAudioDecoder| may have been released due to the pause timeout, so re-create it if
1446 // needed.
1447 if (audioDecoderStillNeeded() && mAudioDecoder == NULL) {
1448 instantiateDecoder(true /* audio */, &mAudioDecoder);
1449 }
1450 if (mRenderer != NULL) {
1451 mRenderer->resume();
1452 } else {
1453 ALOGW("resume called when renderer is gone or not set");
1454 }
Ray Essickd4d00612017-01-03 09:36:27 -08001455
Ray Essick0d98c182017-11-09 13:42:42 -08001456 startPlaybackTimer("onresume");
Wei Jia94211742014-10-28 17:09:06 -07001457}
1458
Lajos Molnarfcd3e942015-03-31 10:06:48 -07001459status_t NuPlayer::onInstantiateSecureDecoders() {
1460 status_t err;
1461 if (!(mSourceFlags & Source::FLAG_SECURE)) {
1462 return BAD_TYPE;
1463 }
1464
1465 if (mRenderer != NULL) {
1466 ALOGE("renderer should not be set when instantiating secure decoders");
1467 return UNKNOWN_ERROR;
1468 }
1469
1470 // TRICKY: We rely on mRenderer being null, so that decoder does not start requesting
1471 // data on instantiation.
Lajos Molnar1de1e252015-04-30 18:18:34 -07001472 if (mSurface != NULL) {
Lajos Molnarfcd3e942015-03-31 10:06:48 -07001473 err = instantiateDecoder(false, &mVideoDecoder);
1474 if (err != OK) {
1475 return err;
1476 }
1477 }
1478
1479 if (mAudioSink != NULL) {
1480 err = instantiateDecoder(true, &mAudioDecoder);
1481 if (err != OK) {
1482 return err;
1483 }
1484 }
1485 return OK;
1486}
1487
Wei Jiac5de0912016-11-18 10:22:14 -08001488void NuPlayer::onStart(int64_t startPositionUs, MediaPlayerSeekMode mode) {
Hassan Shojaniacefac142017-02-06 21:02:02 -08001489 ALOGV("onStart: mCrypto: %p (%d)", mCrypto.get(),
1490 (mCrypto != NULL ? mCrypto->getStrongCount() : 0));
1491
Robert Shih0c61a0d2015-07-06 15:09:10 -07001492 if (!mSourceStarted) {
1493 mSourceStarted = true;
1494 mSource->start();
1495 }
1496 if (startPositionUs > 0) {
Wei Jiac5de0912016-11-18 10:22:14 -08001497 performSeek(startPositionUs, mode);
Robert Shih0c61a0d2015-07-06 15:09:10 -07001498 if (mSource->getFormat(false /* audio */) == NULL) {
1499 return;
1500 }
1501 }
1502
Wei Jia94211742014-10-28 17:09:06 -07001503 mOffloadAudio = false;
1504 mAudioEOS = false;
1505 mVideoEOS = false;
Wei Jia94211742014-10-28 17:09:06 -07001506 mStarted = true;
Wei Jiafe8fe7d2016-06-08 10:29:25 -07001507 mPaused = false;
Wei Jia94211742014-10-28 17:09:06 -07001508
Wei Jia94211742014-10-28 17:09:06 -07001509 uint32_t flags = 0;
1510
1511 if (mSource->isRealTime()) {
1512 flags |= Renderer::FLAG_REAL_TIME;
1513 }
1514
Wei Jia9737d342016-12-28 14:59:59 -08001515 bool hasAudio = (mSource->getFormat(true /* audio */) != NULL);
1516 bool hasVideo = (mSource->getFormat(false /* audio */) != NULL);
1517 if (!hasAudio && !hasVideo) {
Wei Jia1de83d52016-10-10 10:15:03 -07001518 ALOGE("no metadata for either audio or video source");
1519 mSource->stop();
1520 mSourceStarted = false;
1521 notifyListener(MEDIA_ERROR, MEDIA_ERROR_UNKNOWN, ERROR_MALFORMED);
1522 return;
1523 }
Wei Jia9737d342016-12-28 14:59:59 -08001524 ALOGV_IF(!hasAudio, "no metadata for audio source"); // video only stream
1525
1526 sp<MetaData> audioMeta = mSource->getFormatMeta(true /* audio */);
Wei Jia1de83d52016-10-10 10:15:03 -07001527
Wei Jia94211742014-10-28 17:09:06 -07001528 audio_stream_type_t streamType = AUDIO_STREAM_MUSIC;
1529 if (mAudioSink != NULL) {
1530 streamType = mAudioSink->getAudioStreamType();
1531 }
1532
Wei Jia94211742014-10-28 17:09:06 -07001533 mOffloadAudio =
Wei Jia9737d342016-12-28 14:59:59 -08001534 canOffloadStream(audioMeta, hasVideo, mSource->isStreaming(), streamType)
Wei Jiabf70feb2016-02-19 15:47:16 -08001535 && (mPlaybackSettings.mSpeed == 1.f && mPlaybackSettings.mPitch == 1.f);
Hassan Shojaniacefac142017-02-06 21:02:02 -08001536
1537 // Modular DRM: Disabling audio offload if the source is protected
1538 if (mOffloadAudio && mIsDrmProtected) {
1539 mOffloadAudio = false;
1540 ALOGV("onStart: Disabling mOffloadAudio now that the source is protected.");
1541 }
1542
Wei Jia94211742014-10-28 17:09:06 -07001543 if (mOffloadAudio) {
1544 flags |= Renderer::FLAG_OFFLOAD_AUDIO;
1545 }
1546
Lajos Molnar1d15ab52015-03-04 16:46:34 -08001547 sp<AMessage> notify = new AMessage(kWhatRendererNotify, this);
Wei Jia94211742014-10-28 17:09:06 -07001548 ++mRendererGeneration;
1549 notify->setInt32("generation", mRendererGeneration);
Wei Jia0a68f662017-08-30 18:01:26 -07001550 mRenderer = new Renderer(mAudioSink, mMediaClock, notify, flags);
Wei Jia94211742014-10-28 17:09:06 -07001551 mRendererLooper = new ALooper;
1552 mRendererLooper->setName("NuPlayerRenderer");
1553 mRendererLooper->start(false, false, ANDROID_PRIORITY_AUDIO);
1554 mRendererLooper->registerHandler(mRenderer);
Lajos Molnar3a474aa2015-04-24 17:10:07 -07001555
1556 status_t err = mRenderer->setPlaybackSettings(mPlaybackSettings);
1557 if (err != OK) {
1558 mSource->stop();
Robert Shih0c61a0d2015-07-06 15:09:10 -07001559 mSourceStarted = false;
Lajos Molnar3a474aa2015-04-24 17:10:07 -07001560 notifyListener(MEDIA_ERROR, MEDIA_ERROR_UNKNOWN, err);
1561 return;
Wei Jiac8206ff2015-03-04 13:59:37 -08001562 }
Wei Jia94211742014-10-28 17:09:06 -07001563
Ronghua Wuc8a70d32015-04-29 16:26:34 -07001564 float rate = getFrameRate();
1565 if (rate > 0) {
Wei Jia94211742014-10-28 17:09:06 -07001566 mRenderer->setVideoFrameRate(rate);
1567 }
1568
Wei Jiac6cfd702014-11-11 16:33:20 -08001569 if (mVideoDecoder != NULL) {
1570 mVideoDecoder->setRenderer(mRenderer);
1571 }
1572 if (mAudioDecoder != NULL) {
1573 mAudioDecoder->setRenderer(mRenderer);
1574 }
1575
Ray Essick0d98c182017-11-09 13:42:42 -08001576 startPlaybackTimer("onstart");
Ray Essickd4d00612017-01-03 09:36:27 -08001577
Wei Jia94211742014-10-28 17:09:06 -07001578 postScanSources();
1579}
1580
Ray Essick0d98c182017-11-09 13:42:42 -08001581void NuPlayer::startPlaybackTimer(const char *where) {
1582 Mutex::Autolock autoLock(mPlayingTimeLock);
1583 if (mLastStartedPlayingTimeNs == 0) {
1584 mLastStartedPlayingTimeNs = systemTime();
1585 ALOGV("startPlaybackTimer() time %20" PRId64 " (%s)", mLastStartedPlayingTimeNs, where);
1586 }
1587}
1588
Ray Essickeda32522018-02-28 12:08:28 -08001589void NuPlayer::updatePlaybackTimer(bool stopping, const char *where) {
Ray Essick0d98c182017-11-09 13:42:42 -08001590 Mutex::Autolock autoLock(mPlayingTimeLock);
1591
Ray Essickeda32522018-02-28 12:08:28 -08001592 ALOGV("updatePlaybackTimer(%s) time %20" PRId64 " (%s)",
1593 stopping ? "stop" : "snap", mLastStartedPlayingTimeNs, where);
Ray Essick0d98c182017-11-09 13:42:42 -08001594
1595 if (mLastStartedPlayingTimeNs != 0) {
1596 sp<NuPlayerDriver> driver = mDriver.promote();
Ray Essickeda32522018-02-28 12:08:28 -08001597 int64_t now = systemTime();
Ray Essick0d98c182017-11-09 13:42:42 -08001598 if (driver != NULL) {
Ray Essick0d98c182017-11-09 13:42:42 -08001599 int64_t played = now - mLastStartedPlayingTimeNs;
Ray Essickeda32522018-02-28 12:08:28 -08001600 ALOGV("updatePlaybackTimer() log %20" PRId64 "", played);
Ray Essick0d98c182017-11-09 13:42:42 -08001601
1602 if (played > 0) {
1603 driver->notifyMorePlayingTimeUs((played+500)/1000);
1604 }
1605 }
Ray Essickeda32522018-02-28 12:08:28 -08001606 if (stopping) {
1607 mLastStartedPlayingTimeNs = 0;
1608 } else {
1609 mLastStartedPlayingTimeNs = now;
1610 }
Ray Essick0d98c182017-11-09 13:42:42 -08001611 }
1612}
1613
Ray Essick58e0f7a2017-11-21 10:59:38 -08001614void NuPlayer::startRebufferingTimer() {
1615 Mutex::Autolock autoLock(mPlayingTimeLock);
1616 if (mLastStartedRebufferingTimeNs == 0) {
1617 mLastStartedRebufferingTimeNs = systemTime();
1618 ALOGV("startRebufferingTimer() time %20" PRId64 "", mLastStartedRebufferingTimeNs);
1619 }
1620}
1621
Ray Essickeda32522018-02-28 12:08:28 -08001622void NuPlayer::updateRebufferingTimer(bool stopping, bool exitingPlayback) {
Ray Essick58e0f7a2017-11-21 10:59:38 -08001623 Mutex::Autolock autoLock(mPlayingTimeLock);
1624
Ray Essickeda32522018-02-28 12:08:28 -08001625 ALOGV("updateRebufferingTimer(%s) time %20" PRId64 " (exiting %d)",
1626 stopping ? "stop" : "snap", mLastStartedRebufferingTimeNs, exitingPlayback);
Ray Essick58e0f7a2017-11-21 10:59:38 -08001627
1628 if (mLastStartedRebufferingTimeNs != 0) {
1629 sp<NuPlayerDriver> driver = mDriver.promote();
Ray Essickeda32522018-02-28 12:08:28 -08001630 int64_t now = systemTime();
Ray Essick58e0f7a2017-11-21 10:59:38 -08001631 if (driver != NULL) {
Ray Essick58e0f7a2017-11-21 10:59:38 -08001632 int64_t rebuffered = now - mLastStartedRebufferingTimeNs;
Ray Essickeda32522018-02-28 12:08:28 -08001633 ALOGV("updateRebufferingTimer() log %20" PRId64 "", rebuffered);
Ray Essick58e0f7a2017-11-21 10:59:38 -08001634
1635 if (rebuffered > 0) {
1636 driver->notifyMoreRebufferingTimeUs((rebuffered+500)/1000);
1637 if (exitingPlayback) {
1638 driver->notifyRebufferingWhenExit(true);
1639 }
1640 }
1641 }
Ray Essickeda32522018-02-28 12:08:28 -08001642 if (stopping) {
1643 mLastStartedRebufferingTimeNs = 0;
1644 } else {
1645 mLastStartedRebufferingTimeNs = now;
1646 }
Ray Essick58e0f7a2017-11-21 10:59:38 -08001647 }
1648}
1649
Ray Essickeda32522018-02-28 12:08:28 -08001650void NuPlayer::updateInternalTimers() {
1651 // update values, but ticking clocks keep ticking
1652 ALOGV("updateInternalTimers()");
1653 updatePlaybackTimer(false /* stopping */, "updateInternalTimers");
1654 updateRebufferingTimer(false /* stopping */, false /* exiting */);
1655}
1656
Chong Zhangefbb6192015-01-30 17:13:27 -08001657void NuPlayer::onPause() {
Ray Essick0d98c182017-11-09 13:42:42 -08001658
Ray Essickeda32522018-02-28 12:08:28 -08001659 updatePlaybackTimer(true /* stopping */, "onPause");
Ray Essick0d98c182017-11-09 13:42:42 -08001660
Chong Zhangefbb6192015-01-30 17:13:27 -08001661 if (mPaused) {
1662 return;
1663 }
1664 mPaused = true;
1665 if (mSource != NULL) {
1666 mSource->pause();
1667 } else {
1668 ALOGW("pause called when source is gone or not set");
1669 }
1670 if (mRenderer != NULL) {
1671 mRenderer->pause();
1672 } else {
1673 ALOGW("pause called when renderer is gone or not set");
1674 }
Ray Essickd4d00612017-01-03 09:36:27 -08001675
Chong Zhangefbb6192015-01-30 17:13:27 -08001676}
1677
Ronghua Wud7988b12014-10-03 15:19:10 -07001678bool NuPlayer::audioDecoderStillNeeded() {
1679 // Audio decoder is no longer needed if it's in shut/shutting down status.
1680 return ((mFlushingAudio != SHUT_DOWN) && (mFlushingAudio != SHUTTING_DOWN_DECODER));
1681}
1682
Andy Hung8d121d42014-10-03 09:53:53 -07001683void NuPlayer::handleFlushComplete(bool audio, bool isDecoder) {
1684 // We wait for both the decoder flush and the renderer flush to complete
1685 // before entering either the FLUSHED or the SHUTTING_DOWN_DECODER state.
1686
1687 mFlushComplete[audio][isDecoder] = true;
1688 if (!mFlushComplete[audio][!isDecoder]) {
1689 return;
1690 }
1691
1692 FlushStatus *state = audio ? &mFlushingAudio : &mFlushingVideo;
1693 switch (*state) {
1694 case FLUSHING_DECODER:
1695 {
1696 *state = FLUSHED;
Andy Hung8d121d42014-10-03 09:53:53 -07001697 break;
1698 }
1699
1700 case FLUSHING_DECODER_SHUTDOWN:
1701 {
1702 *state = SHUTTING_DOWN_DECODER;
1703
1704 ALOGV("initiating %s decoder shutdown", audio ? "audio" : "video");
Andy Hung8d121d42014-10-03 09:53:53 -07001705 getDecoder(audio)->initiateShutdown();
1706 break;
1707 }
1708
1709 default:
1710 // decoder flush completes only occur in a flushing state.
1711 LOG_ALWAYS_FATAL_IF(isDecoder, "decoder flush in invalid state %d", *state);
1712 break;
1713 }
1714}
1715
Andreas Huber3831a062010-12-21 10:22:33 -08001716void NuPlayer::finishFlushIfPossible() {
Wei Jia53904f32014-07-29 10:22:53 -07001717 if (mFlushingAudio != NONE && mFlushingAudio != FLUSHED
1718 && mFlushingAudio != SHUT_DOWN) {
Andreas Huber3831a062010-12-21 10:22:33 -08001719 return;
1720 }
1721
Wei Jia53904f32014-07-29 10:22:53 -07001722 if (mFlushingVideo != NONE && mFlushingVideo != FLUSHED
1723 && mFlushingVideo != SHUT_DOWN) {
Andreas Huber3831a062010-12-21 10:22:33 -08001724 return;
1725 }
1726
Steve Block3856b092011-10-20 11:56:00 +01001727 ALOGV("both audio and video are flushed now.");
Andreas Huber3831a062010-12-21 10:22:33 -08001728
Andreas Huber3831a062010-12-21 10:22:33 -08001729 mFlushingAudio = NONE;
1730 mFlushingVideo = NONE;
Andreas Huber3831a062010-12-21 10:22:33 -08001731
Andy Hung8d121d42014-10-03 09:53:53 -07001732 clearFlushComplete();
1733
Andreas Hubera1f8ab02012-11-30 10:53:22 -08001734 processDeferredActions();
Andreas Huber1aef2112011-01-04 14:01:29 -08001735}
1736
1737void NuPlayer::postScanSources() {
1738 if (mScanSourcesPending) {
1739 return;
1740 }
1741
Lajos Molnar1d15ab52015-03-04 16:46:34 -08001742 sp<AMessage> msg = new AMessage(kWhatScanSources, this);
Andreas Huber1aef2112011-01-04 14:01:29 -08001743 msg->setInt32("generation", mScanSourcesGeneration);
1744 msg->post();
1745
1746 mScanSourcesPending = true;
1747}
1748
Wei Jia41cd4632016-05-13 10:53:30 -07001749void NuPlayer::tryOpenAudioSinkForOffload(
1750 const sp<AMessage> &format, const sp<MetaData> &audioMeta, bool hasVideo) {
Andy Hung202bce12014-12-03 11:47:36 -08001751 // Note: This is called early in NuPlayer to determine whether offloading
1752 // is possible; otherwise the decoders call the renderer openAudioSink directly.
Andy Hung282a7e32014-08-14 15:56:34 -07001753
Andy Hung202bce12014-12-03 11:47:36 -08001754 status_t err = mRenderer->openAudioSink(
Dhananjay Kumarc387f2b2015-08-06 10:43:16 +05301755 format, true /* offloadOnly */, hasVideo,
1756 AUDIO_OUTPUT_FLAG_NONE, &mOffloadAudio, mSource->isStreaming());
Andy Hung202bce12014-12-03 11:47:36 -08001757 if (err != OK) {
1758 // Any failure we turn off mOffloadAudio.
1759 mOffloadAudio = false;
1760 } else if (mOffloadAudio) {
Chong Zhang3b9eb1f2014-10-15 17:05:08 -07001761 sendMetaDataToHal(mAudioSink, audioMeta);
Andy Hung282a7e32014-08-14 15:56:34 -07001762 }
1763}
1764
1765void NuPlayer::closeAudioSink() {
Chong Zhang3b9eb1f2014-10-15 17:05:08 -07001766 mRenderer->closeAudioSink();
Andy Hung282a7e32014-08-14 15:56:34 -07001767}
1768
Wei Jia5031b2f2016-02-25 11:19:31 -08001769void NuPlayer::restartAudio(
Wei Jiabf70feb2016-02-19 15:47:16 -08001770 int64_t currentPositionUs, bool forceNonOffload, bool needsToCreateAudioDecoder) {
Wei Jiaa05f1e32016-03-25 16:31:22 -07001771 if (mAudioDecoder != NULL) {
1772 mAudioDecoder->pause();
1773 mAudioDecoder.clear();
Wei Jia686e8e52017-04-03 14:08:01 -07001774 mAudioDecoderError = false;
Wei Jiaa05f1e32016-03-25 16:31:22 -07001775 ++mAudioDecoderGeneration;
1776 }
Wei Jiabf70feb2016-02-19 15:47:16 -08001777 if (mFlushingAudio == FLUSHING_DECODER) {
1778 mFlushComplete[1 /* audio */][1 /* isDecoder */] = true;
1779 mFlushingAudio = FLUSHED;
1780 finishFlushIfPossible();
1781 } else if (mFlushingAudio == FLUSHING_DECODER_SHUTDOWN
1782 || mFlushingAudio == SHUTTING_DOWN_DECODER) {
1783 mFlushComplete[1 /* audio */][1 /* isDecoder */] = true;
1784 mFlushingAudio = SHUT_DOWN;
1785 finishFlushIfPossible();
1786 needsToCreateAudioDecoder = false;
1787 }
1788 if (mRenderer == NULL) {
1789 return;
1790 }
1791 closeAudioSink();
1792 mRenderer->flush(true /* audio */, false /* notifyComplete */);
1793 if (mVideoDecoder != NULL) {
1794 mRenderer->flush(false /* audio */, false /* notifyComplete */);
1795 }
1796
Wei Jiac5de0912016-11-18 10:22:14 -08001797 performSeek(currentPositionUs, MediaPlayerSeekMode::SEEK_PREVIOUS_SYNC /* mode */);
Wei Jiabf70feb2016-02-19 15:47:16 -08001798
1799 if (forceNonOffload) {
1800 mRenderer->signalDisableOffloadAudio();
1801 mOffloadAudio = false;
1802 }
1803 if (needsToCreateAudioDecoder) {
Wei Jiaa05f1e32016-03-25 16:31:22 -07001804 instantiateDecoder(true /* audio */, &mAudioDecoder, !forceNonOffload);
Wei Jiabf70feb2016-02-19 15:47:16 -08001805 }
1806}
1807
Wei Jia41cd4632016-05-13 10:53:30 -07001808void NuPlayer::determineAudioModeChange(const sp<AMessage> &audioFormat) {
Wei Jiae4d18c72015-07-13 17:58:11 -07001809 if (mSource == NULL || mAudioSink == NULL) {
1810 return;
1811 }
1812
1813 if (mRenderer == NULL) {
1814 ALOGW("No renderer can be used to determine audio mode. Use non-offload for safety.");
1815 mOffloadAudio = false;
1816 return;
1817 }
1818
1819 sp<MetaData> audioMeta = mSource->getFormatMeta(true /* audio */);
1820 sp<AMessage> videoFormat = mSource->getFormat(false /* audio */);
1821 audio_stream_type_t streamType = mAudioSink->getAudioStreamType();
1822 const bool hasVideo = (videoFormat != NULL);
Hassan Shojaniacefac142017-02-06 21:02:02 -08001823 bool canOffload = canOffloadStream(
Wei Jiabf70feb2016-02-19 15:47:16 -08001824 audioMeta, hasVideo, mSource->isStreaming(), streamType)
1825 && (mPlaybackSettings.mSpeed == 1.f && mPlaybackSettings.mPitch == 1.f);
Hassan Shojaniacefac142017-02-06 21:02:02 -08001826
1827 // Modular DRM: Disabling audio offload if the source is protected
1828 if (canOffload && mIsDrmProtected) {
1829 canOffload = false;
1830 ALOGV("determineAudioModeChange: Disabling mOffloadAudio b/c the source is protected.");
1831 }
1832
Wei Jiae4d18c72015-07-13 17:58:11 -07001833 if (canOffload) {
1834 if (!mOffloadAudio) {
1835 mRenderer->signalEnableOffloadAudio();
1836 }
1837 // open audio sink early under offload mode.
Wei Jia41cd4632016-05-13 10:53:30 -07001838 tryOpenAudioSinkForOffload(audioFormat, audioMeta, hasVideo);
Wei Jiae4d18c72015-07-13 17:58:11 -07001839 } else {
Robert Shihe1d70192015-07-23 17:54:13 -07001840 if (mOffloadAudio) {
1841 mRenderer->signalDisableOffloadAudio();
1842 mOffloadAudio = false;
1843 }
Wei Jiae4d18c72015-07-13 17:58:11 -07001844 }
1845}
1846
Wei Jiaa05f1e32016-03-25 16:31:22 -07001847status_t NuPlayer::instantiateDecoder(
1848 bool audio, sp<DecoderBase> *decoder, bool checkAudioModeChange) {
Wei Jia566da802015-08-27 13:59:30 -07001849 // The audio decoder could be cleared by tear down. If still in shut down
1850 // process, no need to create a new audio decoder.
1851 if (*decoder != NULL || (audio && mFlushingAudio == SHUT_DOWN)) {
Andreas Huberf9334412010-12-15 15:17:42 -08001852 return OK;
1853 }
1854
Andreas Huber84066782011-08-16 09:34:26 -07001855 sp<AMessage> format = mSource->getFormat(audio);
Andreas Huberf9334412010-12-15 15:17:42 -08001856
Andreas Huber84066782011-08-16 09:34:26 -07001857 if (format == NULL) {
Robert Shih7350b052015-10-01 15:50:14 -07001858 return UNKNOWN_ERROR;
1859 } else {
1860 status_t err;
1861 if (format->findInt32("err", &err) && err) {
1862 return err;
1863 }
Andreas Huberf9334412010-12-15 15:17:42 -08001864 }
1865
Ronghua Wu8db88132015-04-22 13:51:35 -07001866 format->setInt32("priority", 0 /* realtime */);
1867
Andreas Huber3fe62152011-09-16 15:09:22 -07001868 if (!audio) {
Andreas Huber84066782011-08-16 09:34:26 -07001869 AString mime;
1870 CHECK(format->findString("mime", &mime));
Chong Zhanga7fa1d92014-06-11 14:49:23 -07001871
Lajos Molnar1d15ab52015-03-04 16:46:34 -08001872 sp<AMessage> ccNotify = new AMessage(kWhatClosedCaptionNotify, this);
Chong Zhang341ab6e2015-02-04 13:37:18 -08001873 if (mCCDecoder == NULL) {
1874 mCCDecoder = new CCDecoder(ccNotify);
1875 }
Lajos Molnar09524832014-07-17 14:29:51 -07001876
1877 if (mSourceFlags & Source::FLAG_SECURE) {
1878 format->setInt32("secure", true);
1879 }
Chong Zhang17134602015-01-07 16:14:34 -08001880
1881 if (mSourceFlags & Source::FLAG_PROTECTED) {
1882 format->setInt32("protected", true);
1883 }
Ronghua Wu8db88132015-04-22 13:51:35 -07001884
Ronghua Wuc8a70d32015-04-29 16:26:34 -07001885 float rate = getFrameRate();
1886 if (rate > 0) {
Lajos Molnar3a474aa2015-04-24 17:10:07 -07001887 format->setFloat("operating-rate", rate * mPlaybackSettings.mSpeed);
Ronghua Wu8db88132015-04-22 13:51:35 -07001888 }
Andreas Huber3fe62152011-09-16 15:09:22 -07001889 }
1890
Wei Jiabc2fb722014-07-08 16:37:57 -07001891 if (audio) {
Lajos Molnar1d15ab52015-03-04 16:46:34 -08001892 sp<AMessage> notify = new AMessage(kWhatAudioNotify, this);
Wei Jia88703c32014-08-06 11:24:07 -07001893 ++mAudioDecoderGeneration;
1894 notify->setInt32("generation", mAudioDecoderGeneration);
1895
Wei Jiaa05f1e32016-03-25 16:31:22 -07001896 if (checkAudioModeChange) {
Wei Jia41cd4632016-05-13 10:53:30 -07001897 determineAudioModeChange(format);
Wei Jiaa05f1e32016-03-25 16:31:22 -07001898 }
Wei Jiabc2fb722014-07-08 16:37:57 -07001899 if (mOffloadAudio) {
Wei Jia14532f22015-12-29 11:28:15 -08001900 mSource->setOffloadAudio(true /* offload */);
1901
Haynes Mathew George8b635332015-03-30 17:59:47 -07001902 const bool hasVideo = (mSource->getFormat(false /*audio */) != NULL);
1903 format->setInt32("has-video", hasVideo);
Wei Jiac6cfd702014-11-11 16:33:20 -08001904 *decoder = new DecoderPassThrough(notify, mSource, mRenderer);
Hassan Shojaniacefac142017-02-06 21:02:02 -08001905 ALOGV("instantiateDecoder audio DecoderPassThrough hasVideo: %d", hasVideo);
Wei Jiabc2fb722014-07-08 16:37:57 -07001906 } else {
Wei Jia14532f22015-12-29 11:28:15 -08001907 mSource->setOffloadAudio(false /* offload */);
1908
Wei Jiaf2ae3e12016-10-27 17:10:59 -07001909 *decoder = new Decoder(notify, mSource, mPID, mUID, mRenderer);
Hassan Shojaniacefac142017-02-06 21:02:02 -08001910 ALOGV("instantiateDecoder audio Decoder");
Wei Jiabc2fb722014-07-08 16:37:57 -07001911 }
Wei Jia686e8e52017-04-03 14:08:01 -07001912 mAudioDecoderError = false;
Wei Jiabc2fb722014-07-08 16:37:57 -07001913 } else {
Lajos Molnar1d15ab52015-03-04 16:46:34 -08001914 sp<AMessage> notify = new AMessage(kWhatVideoNotify, this);
Wei Jia88703c32014-08-06 11:24:07 -07001915 ++mVideoDecoderGeneration;
1916 notify->setInt32("generation", mVideoDecoderGeneration);
1917
Chong Zhang7137ec72014-11-12 16:41:05 -08001918 *decoder = new Decoder(
Wei Jiaf2ae3e12016-10-27 17:10:59 -07001919 notify, mSource, mPID, mUID, mRenderer, mSurface, mCCDecoder);
Wei Jia686e8e52017-04-03 14:08:01 -07001920 mVideoDecoderError = false;
Lajos Molnard9fd6312014-11-06 11:00:00 -08001921
1922 // enable FRC if high-quality AV sync is requested, even if not
Lajos Molnar1de1e252015-04-30 18:18:34 -07001923 // directly queuing to display, as this will even improve textureview
Lajos Molnard9fd6312014-11-06 11:00:00 -08001924 // playback.
1925 {
Marco Nelissen96626b72016-12-01 13:58:59 -08001926 if (property_get_bool("persist.sys.media.avsync", false)) {
Lajos Molnard9fd6312014-11-06 11:00:00 -08001927 format->setInt32("auto-frc", 1);
1928 }
1929 }
Wei Jiabc2fb722014-07-08 16:37:57 -07001930 }
Lajos Molnar1cd13982014-01-17 15:12:51 -08001931 (*decoder)->init();
Hassan Shojaniacefac142017-02-06 21:02:02 -08001932
1933 // Modular DRM
1934 if (mIsDrmProtected) {
1935 format->setPointer("crypto", mCrypto.get());
1936 ALOGV("instantiateDecoder: mCrypto: %p (%d) isSecure: %d", mCrypto.get(),
1937 (mCrypto != NULL ? mCrypto->getStrongCount() : 0),
1938 (mSourceFlags & Source::FLAG_SECURE) != 0);
1939 }
1940
Andreas Huber84066782011-08-16 09:34:26 -07001941 (*decoder)->configure(format);
Andreas Huberf9334412010-12-15 15:17:42 -08001942
Praveen Chavanbbaa1442016-04-08 13:33:49 -07001943 if (!audio) {
1944 sp<AMessage> params = new AMessage();
1945 float rate = getFrameRate();
1946 if (rate > 0) {
1947 params->setFloat("frame-rate-total", rate);
1948 }
1949
1950 sp<MetaData> fileMeta = getFileMeta();
1951 if (fileMeta != NULL) {
1952 int32_t videoTemporalLayerCount;
1953 if (fileMeta->findInt32(kKeyTemporalLayerCount, &videoTemporalLayerCount)
1954 && videoTemporalLayerCount > 0) {
1955 params->setInt32("temporal-layer-count", videoTemporalLayerCount);
1956 }
1957 }
1958
1959 if (params->countEntries() > 0) {
1960 (*decoder)->setParameters(params);
1961 }
1962 }
Andreas Huberf9334412010-12-15 15:17:42 -08001963 return OK;
1964}
1965
Chong Zhangced1c2f2014-08-08 15:22:35 -07001966void NuPlayer::updateVideoSize(
1967 const sp<AMessage> &inputFormat,
1968 const sp<AMessage> &outputFormat) {
1969 if (inputFormat == NULL) {
1970 ALOGW("Unknown video size, reporting 0x0!");
1971 notifyListener(MEDIA_SET_VIDEO_SIZE, 0, 0);
1972 return;
1973 }
Wei Jia9737d342016-12-28 14:59:59 -08001974 int32_t err = OK;
1975 inputFormat->findInt32("err", &err);
1976 if (err == -EWOULDBLOCK) {
1977 ALOGW("Video meta is not available yet!");
1978 return;
1979 }
1980 if (err != OK) {
1981 ALOGW("Something is wrong with video meta!");
1982 return;
1983 }
Chong Zhangced1c2f2014-08-08 15:22:35 -07001984
1985 int32_t displayWidth, displayHeight;
Chong Zhangced1c2f2014-08-08 15:22:35 -07001986 if (outputFormat != NULL) {
1987 int32_t width, height;
1988 CHECK(outputFormat->findInt32("width", &width));
1989 CHECK(outputFormat->findInt32("height", &height));
1990
1991 int32_t cropLeft, cropTop, cropRight, cropBottom;
1992 CHECK(outputFormat->findRect(
1993 "crop",
1994 &cropLeft, &cropTop, &cropRight, &cropBottom));
1995
1996 displayWidth = cropRight - cropLeft + 1;
1997 displayHeight = cropBottom - cropTop + 1;
1998
1999 ALOGV("Video output format changed to %d x %d "
2000 "(crop: %d x %d @ (%d, %d))",
2001 width, height,
2002 displayWidth,
2003 displayHeight,
2004 cropLeft, cropTop);
2005 } else {
2006 CHECK(inputFormat->findInt32("width", &displayWidth));
2007 CHECK(inputFormat->findInt32("height", &displayHeight));
2008
2009 ALOGV("Video input format %d x %d", displayWidth, displayHeight);
2010 }
2011
2012 // Take into account sample aspect ratio if necessary:
2013 int32_t sarWidth, sarHeight;
2014 if (inputFormat->findInt32("sar-width", &sarWidth)
Wei Jia095bc252017-01-27 10:16:16 -08002015 && inputFormat->findInt32("sar-height", &sarHeight)
2016 && sarWidth > 0 && sarHeight > 0) {
Chong Zhangced1c2f2014-08-08 15:22:35 -07002017 ALOGV("Sample aspect ratio %d : %d", sarWidth, sarHeight);
2018
2019 displayWidth = (displayWidth * sarWidth) / sarHeight;
2020
2021 ALOGV("display dimensions %d x %d", displayWidth, displayHeight);
Wei Jiadf6c6af2016-09-29 11:27:15 -07002022 } else {
2023 int32_t width, height;
2024 if (inputFormat->findInt32("display-width", &width)
2025 && inputFormat->findInt32("display-height", &height)
2026 && width > 0 && height > 0
2027 && displayWidth > 0 && displayHeight > 0) {
2028 if (displayHeight * (int64_t)width / height > (int64_t)displayWidth) {
2029 displayHeight = (int32_t)(displayWidth * (int64_t)height / width);
2030 } else {
2031 displayWidth = (int32_t)(displayHeight * (int64_t)width / height);
2032 }
2033 ALOGV("Video display width and height are overridden to %d x %d",
2034 displayWidth, displayHeight);
2035 }
Chong Zhangced1c2f2014-08-08 15:22:35 -07002036 }
2037
2038 int32_t rotationDegrees;
2039 if (!inputFormat->findInt32("rotation-degrees", &rotationDegrees)) {
2040 rotationDegrees = 0;
2041 }
2042
2043 if (rotationDegrees == 90 || rotationDegrees == 270) {
2044 int32_t tmp = displayWidth;
2045 displayWidth = displayHeight;
2046 displayHeight = tmp;
2047 }
2048
2049 notifyListener(
2050 MEDIA_SET_VIDEO_SIZE,
2051 displayWidth,
2052 displayHeight);
2053}
2054
Chong Zhangdcb89b32013-08-06 09:44:47 -07002055void NuPlayer::notifyListener(int msg, int ext1, int ext2, const Parcel *in) {
Andreas Huber43c3e6c2011-01-05 12:17:08 -08002056 if (mDriver == NULL) {
Andreas Huberf9334412010-12-15 15:17:42 -08002057 return;
2058 }
2059
Andreas Huber43c3e6c2011-01-05 12:17:08 -08002060 sp<NuPlayerDriver> driver = mDriver.promote();
Andreas Huberf9334412010-12-15 15:17:42 -08002061
Andreas Huber43c3e6c2011-01-05 12:17:08 -08002062 if (driver == NULL) {
Andreas Huberf9334412010-12-15 15:17:42 -08002063 return;
2064 }
2065
Chong Zhangdcb89b32013-08-06 09:44:47 -07002066 driver->notifyListener(msg, ext1, ext2, in);
Andreas Huberf9334412010-12-15 15:17:42 -08002067}
2068
Chong Zhang7137ec72014-11-12 16:41:05 -08002069void NuPlayer::flushDecoder(bool audio, bool needShutdown) {
Andreas Huber14f76722013-01-15 09:04:18 -08002070 ALOGV("[%s] flushDecoder needShutdown=%d",
2071 audio ? "audio" : "video", needShutdown);
2072
Chong Zhang7137ec72014-11-12 16:41:05 -08002073 const sp<DecoderBase> &decoder = getDecoder(audio);
Lajos Molnar87603c02014-08-20 19:25:30 -07002074 if (decoder == NULL) {
Steve Blockdf64d152012-01-04 20:05:49 +00002075 ALOGI("flushDecoder %s without decoder present",
Andreas Huber6e3d3112011-11-28 12:36:11 -08002076 audio ? "audio" : "video");
Lajos Molnar87603c02014-08-20 19:25:30 -07002077 return;
Andreas Huber6e3d3112011-11-28 12:36:11 -08002078 }
2079
Andreas Huber1aef2112011-01-04 14:01:29 -08002080 // Make sure we don't continue to scan sources until we finish flushing.
2081 ++mScanSourcesGeneration;
Chong Zhang3b032b32015-04-17 15:49:06 -07002082 if (mScanSourcesPending) {
Toshikazu Saitocbcbb792015-09-15 14:53:01 +09002083 if (!needShutdown) {
2084 mDeferredActions.push_back(
2085 new SimpleAction(&NuPlayer::performScanSources));
2086 }
Chong Zhang3b032b32015-04-17 15:49:06 -07002087 mScanSourcesPending = false;
2088 }
Andreas Huber1aef2112011-01-04 14:01:29 -08002089
Chong Zhang7137ec72014-11-12 16:41:05 -08002090 decoder->signalFlush();
Andreas Huber1aef2112011-01-04 14:01:29 -08002091
2092 FlushStatus newStatus =
2093 needShutdown ? FLUSHING_DECODER_SHUTDOWN : FLUSHING_DECODER;
2094
Lajos Molnarfcd3e942015-03-31 10:06:48 -07002095 mFlushComplete[audio][false /* isDecoder */] = (mRenderer == NULL);
Andy Hung8d121d42014-10-03 09:53:53 -07002096 mFlushComplete[audio][true /* isDecoder */] = false;
Andreas Huber1aef2112011-01-04 14:01:29 -08002097 if (audio) {
Wei Jia53904f32014-07-29 10:22:53 -07002098 ALOGE_IF(mFlushingAudio != NONE,
2099 "audio flushDecoder() is called in state %d", mFlushingAudio);
Andreas Huber1aef2112011-01-04 14:01:29 -08002100 mFlushingAudio = newStatus;
Andreas Huber1aef2112011-01-04 14:01:29 -08002101 } else {
Wei Jia53904f32014-07-29 10:22:53 -07002102 ALOGE_IF(mFlushingVideo != NONE,
2103 "video flushDecoder() is called in state %d", mFlushingVideo);
Andreas Huber1aef2112011-01-04 14:01:29 -08002104 mFlushingVideo = newStatus;
Andreas Huber1aef2112011-01-04 14:01:29 -08002105 }
2106}
2107
Chong Zhangced1c2f2014-08-08 15:22:35 -07002108void NuPlayer::queueDecoderShutdown(
2109 bool audio, bool video, const sp<AMessage> &reply) {
2110 ALOGI("queueDecoderShutdown audio=%d, video=%d", audio, video);
Andreas Huber84066782011-08-16 09:34:26 -07002111
Chong Zhangced1c2f2014-08-08 15:22:35 -07002112 mDeferredActions.push_back(
Wei Jiafef808d2014-10-31 17:57:05 -07002113 new FlushDecoderAction(
2114 audio ? FLUSH_CMD_SHUTDOWN : FLUSH_CMD_NONE,
2115 video ? FLUSH_CMD_SHUTDOWN : FLUSH_CMD_NONE));
Andreas Huber84066782011-08-16 09:34:26 -07002116
Chong Zhangced1c2f2014-08-08 15:22:35 -07002117 mDeferredActions.push_back(
2118 new SimpleAction(&NuPlayer::performScanSources));
Andreas Huber84066782011-08-16 09:34:26 -07002119
Chong Zhangced1c2f2014-08-08 15:22:35 -07002120 mDeferredActions.push_back(new PostMessageAction(reply));
2121
2122 processDeferredActions();
Andreas Huber84066782011-08-16 09:34:26 -07002123}
2124
James Dong0d268a32012-08-31 12:18:27 -07002125status_t NuPlayer::setVideoScalingMode(int32_t mode) {
2126 mVideoScalingMode = mode;
Lajos Molnar1de1e252015-04-30 18:18:34 -07002127 if (mSurface != NULL) {
2128 status_t ret = native_window_set_scaling_mode(mSurface.get(), mVideoScalingMode);
James Dong0d268a32012-08-31 12:18:27 -07002129 if (ret != OK) {
2130 ALOGE("Failed to set scaling mode (%d): %s",
2131 -ret, strerror(-ret));
2132 return ret;
2133 }
2134 }
2135 return OK;
2136}
2137
Chong Zhangdcb89b32013-08-06 09:44:47 -07002138status_t NuPlayer::getTrackInfo(Parcel* reply) const {
Lajos Molnar1d15ab52015-03-04 16:46:34 -08002139 sp<AMessage> msg = new AMessage(kWhatGetTrackInfo, this);
Chong Zhangdcb89b32013-08-06 09:44:47 -07002140 msg->setPointer("reply", reply);
2141
2142 sp<AMessage> response;
2143 status_t err = msg->postAndAwaitResponse(&response);
2144 return err;
2145}
2146
Robert Shih7c4f0d72014-07-09 18:53:31 -07002147status_t NuPlayer::getSelectedTrack(int32_t type, Parcel* reply) const {
Lajos Molnar1d15ab52015-03-04 16:46:34 -08002148 sp<AMessage> msg = new AMessage(kWhatGetSelectedTrack, this);
Robert Shih7c4f0d72014-07-09 18:53:31 -07002149 msg->setPointer("reply", reply);
2150 msg->setInt32("type", type);
2151
2152 sp<AMessage> response;
2153 status_t err = msg->postAndAwaitResponse(&response);
2154 if (err == OK && response != NULL) {
2155 CHECK(response->findInt32("err", &err));
2156 }
2157 return err;
2158}
2159
Robert Shih6ffb1fd2014-10-29 16:24:32 -07002160status_t NuPlayer::selectTrack(size_t trackIndex, bool select, int64_t timeUs) {
Lajos Molnar1d15ab52015-03-04 16:46:34 -08002161 sp<AMessage> msg = new AMessage(kWhatSelectTrack, this);
Chong Zhangdcb89b32013-08-06 09:44:47 -07002162 msg->setSize("trackIndex", trackIndex);
2163 msg->setInt32("select", select);
Robert Shih6ffb1fd2014-10-29 16:24:32 -07002164 msg->setInt64("timeUs", timeUs);
Chong Zhangdcb89b32013-08-06 09:44:47 -07002165
2166 sp<AMessage> response;
2167 status_t err = msg->postAndAwaitResponse(&response);
2168
Chong Zhang404fced2014-06-11 14:45:31 -07002169 if (err != OK) {
2170 return err;
2171 }
2172
2173 if (!response->findInt32("err", &err)) {
2174 err = OK;
2175 }
2176
Chong Zhangdcb89b32013-08-06 09:44:47 -07002177 return err;
2178}
2179
Ronghua Wua73d9e02014-10-08 15:13:29 -07002180status_t NuPlayer::getCurrentPosition(int64_t *mediaUs) {
2181 sp<Renderer> renderer = mRenderer;
2182 if (renderer == NULL) {
2183 return NO_INIT;
2184 }
2185
2186 return renderer->getCurrentPosition(mediaUs);
2187}
2188
Praveen Chavane1e5d7a2015-05-19 19:09:48 -07002189void NuPlayer::getStats(Vector<sp<AMessage> > *mTrackStats) {
2190 CHECK(mTrackStats != NULL);
2191
2192 mTrackStats->clear();
2193 if (mVideoDecoder != NULL) {
2194 mTrackStats->push_back(mVideoDecoder->getStats());
2195 }
2196 if (mAudioDecoder != NULL) {
2197 mTrackStats->push_back(mAudioDecoder->getStats());
Chong Zhang7137ec72014-11-12 16:41:05 -08002198 }
Ronghua Wua73d9e02014-10-08 15:13:29 -07002199}
2200
Marco Nelissenf0b72b52014-09-16 15:43:44 -07002201sp<MetaData> NuPlayer::getFileMeta() {
2202 return mSource->getFileFormatMeta();
2203}
2204
Ronghua Wuc8a70d32015-04-29 16:26:34 -07002205float NuPlayer::getFrameRate() {
2206 sp<MetaData> meta = mSource->getFormatMeta(false /* audio */);
2207 if (meta == NULL) {
2208 return 0;
2209 }
2210 int32_t rate;
2211 if (!meta->findInt32(kKeyFrameRate, &rate)) {
2212 // fall back to try file meta
2213 sp<MetaData> fileMeta = getFileMeta();
2214 if (fileMeta == NULL) {
2215 ALOGW("source has video meta but not file meta");
2216 return -1;
2217 }
2218 int32_t fileMetaRate;
2219 if (!fileMeta->findInt32(kKeyFrameRate, &fileMetaRate)) {
2220 return -1;
2221 }
2222 return fileMetaRate;
2223 }
2224 return rate;
2225}
2226
Andreas Huberb7c8e912012-11-27 15:02:53 -08002227void NuPlayer::schedulePollDuration() {
Lajos Molnar1d15ab52015-03-04 16:46:34 -08002228 sp<AMessage> msg = new AMessage(kWhatPollDuration, this);
Andreas Huberb7c8e912012-11-27 15:02:53 -08002229 msg->setInt32("generation", mPollDurationGeneration);
2230 msg->post();
2231}
2232
2233void NuPlayer::cancelPollDuration() {
2234 ++mPollDurationGeneration;
2235}
2236
Andreas Hubera1f8ab02012-11-30 10:53:22 -08002237void NuPlayer::processDeferredActions() {
2238 while (!mDeferredActions.empty()) {
2239 // We won't execute any deferred actions until we're no longer in
2240 // an intermediate state, i.e. one more more decoders are currently
2241 // flushing or shutting down.
2242
Andreas Hubera1f8ab02012-11-30 10:53:22 -08002243 if (mFlushingAudio != NONE || mFlushingVideo != NONE) {
2244 // We're currently flushing, postpone the reset until that's
2245 // completed.
2246
2247 ALOGV("postponing action mFlushingAudio=%d, mFlushingVideo=%d",
2248 mFlushingAudio, mFlushingVideo);
2249
2250 break;
2251 }
2252
2253 sp<Action> action = *mDeferredActions.begin();
2254 mDeferredActions.erase(mDeferredActions.begin());
2255
2256 action->execute(this);
2257 }
2258}
2259
Wei Jiac5de0912016-11-18 10:22:14 -08002260void NuPlayer::performSeek(int64_t seekTimeUs, MediaPlayerSeekMode mode) {
2261 ALOGV("performSeek seekTimeUs=%lld us (%.2f secs), mode=%d",
2262 (long long)seekTimeUs, seekTimeUs / 1E6, mode);
Andreas Hubera1f8ab02012-11-30 10:53:22 -08002263
Andy Hungadf34bf2014-09-03 18:22:22 -07002264 if (mSource == NULL) {
2265 // This happens when reset occurs right before the loop mode
2266 // asynchronously seeks to the start of the stream.
2267 LOG_ALWAYS_FATAL_IF(mAudioDecoder != NULL || mVideoDecoder != NULL,
2268 "mSource is NULL and decoders not NULL audio(%p) video(%p)",
2269 mAudioDecoder.get(), mVideoDecoder.get());
2270 return;
2271 }
Robert Shih1a5c8592015-08-04 18:07:44 -07002272 mPreviousSeekTimeUs = seekTimeUs;
Wei Jiac5de0912016-11-18 10:22:14 -08002273 mSource->seekTo(seekTimeUs, mode);
Robert Shihd3b0bbb2014-07-23 15:00:25 -07002274 ++mTimedTextGeneration;
Andreas Hubera1f8ab02012-11-30 10:53:22 -08002275
Andreas Hubera1f8ab02012-11-30 10:53:22 -08002276 // everything's flushed, continue playback.
2277}
2278
Wei Jiafef808d2014-10-31 17:57:05 -07002279void NuPlayer::performDecoderFlush(FlushCommand audio, FlushCommand video) {
2280 ALOGV("performDecoderFlush audio=%d, video=%d", audio, video);
Andreas Hubera1f8ab02012-11-30 10:53:22 -08002281
Wei Jiafef808d2014-10-31 17:57:05 -07002282 if ((audio == FLUSH_CMD_NONE || mAudioDecoder == NULL)
2283 && (video == FLUSH_CMD_NONE || mVideoDecoder == NULL)) {
Andreas Hubera1f8ab02012-11-30 10:53:22 -08002284 return;
2285 }
2286
Wei Jiafef808d2014-10-31 17:57:05 -07002287 if (audio != FLUSH_CMD_NONE && mAudioDecoder != NULL) {
2288 flushDecoder(true /* audio */, (audio == FLUSH_CMD_SHUTDOWN));
Andreas Hubera1f8ab02012-11-30 10:53:22 -08002289 }
2290
Wei Jiafef808d2014-10-31 17:57:05 -07002291 if (video != FLUSH_CMD_NONE && mVideoDecoder != NULL) {
2292 flushDecoder(false /* audio */, (video == FLUSH_CMD_SHUTDOWN));
Andreas Hubera1f8ab02012-11-30 10:53:22 -08002293 }
2294}
2295
2296void NuPlayer::performReset() {
2297 ALOGV("performReset");
2298
2299 CHECK(mAudioDecoder == NULL);
2300 CHECK(mVideoDecoder == NULL);
2301
Ray Essickeda32522018-02-28 12:08:28 -08002302 updatePlaybackTimer(true /* stopping */, "performReset");
2303 updateRebufferingTimer(true /* stopping */, true /* exiting */);
Ray Essick0d98c182017-11-09 13:42:42 -08002304
Andreas Hubera1f8ab02012-11-30 10:53:22 -08002305 cancelPollDuration();
2306
2307 ++mScanSourcesGeneration;
2308 mScanSourcesPending = false;
2309
Lajos Molnar09524832014-07-17 14:29:51 -07002310 if (mRendererLooper != NULL) {
2311 if (mRenderer != NULL) {
2312 mRendererLooper->unregisterHandler(mRenderer->id());
2313 }
2314 mRendererLooper->stop();
2315 mRendererLooper.clear();
2316 }
Andreas Hubera1f8ab02012-11-30 10:53:22 -08002317 mRenderer.clear();
Wei Jia57568df2014-09-22 10:16:29 -07002318 ++mRendererGeneration;
Andreas Hubera1f8ab02012-11-30 10:53:22 -08002319
2320 if (mSource != NULL) {
2321 mSource->stop();
Andreas Huberb5f25f02013-02-05 10:14:26 -08002322
Wei Jiac45a4b22016-04-15 15:30:23 -07002323 Mutex::Autolock autoLock(mSourceLock);
Andreas Hubera1f8ab02012-11-30 10:53:22 -08002324 mSource.clear();
2325 }
2326
2327 if (mDriver != NULL) {
2328 sp<NuPlayerDriver> driver = mDriver.promote();
2329 if (driver != NULL) {
2330 driver->notifyResetComplete();
2331 }
2332 }
Andreas Huber57a339c2012-12-03 11:18:00 -08002333
2334 mStarted = false;
Wei Jia8a092d32016-06-03 14:57:24 -07002335 mPrepared = false;
Ronghua Wu64c2d172015-10-07 16:52:19 -07002336 mResetting = false;
Robert Shih0c61a0d2015-07-06 15:09:10 -07002337 mSourceStarted = false;
Hassan Shojaniacefac142017-02-06 21:02:02 -08002338
2339 // Modular DRM
2340 if (mCrypto != NULL) {
2341 // decoders will be flushed before this so their mCrypto would go away on their own
2342 // TODO change to ALOGV
2343 ALOGD("performReset mCrypto: %p (%d)", mCrypto.get(),
2344 (mCrypto != NULL ? mCrypto->getStrongCount() : 0));
2345 mCrypto.clear();
2346 }
2347 mIsDrmProtected = false;
Andreas Hubera1f8ab02012-11-30 10:53:22 -08002348}
2349
2350void NuPlayer::performScanSources() {
2351 ALOGV("performScanSources");
2352
Andreas Huber57a339c2012-12-03 11:18:00 -08002353 if (!mStarted) {
2354 return;
2355 }
2356
Andreas Hubera1f8ab02012-11-30 10:53:22 -08002357 if (mAudioDecoder == NULL || mVideoDecoder == NULL) {
2358 postScanSources();
2359 }
2360}
2361
Lajos Molnar1de1e252015-04-30 18:18:34 -07002362void NuPlayer::performSetSurface(const sp<Surface> &surface) {
Andreas Huber57a339c2012-12-03 11:18:00 -08002363 ALOGV("performSetSurface");
2364
Lajos Molnar1de1e252015-04-30 18:18:34 -07002365 mSurface = surface;
Andreas Huber57a339c2012-12-03 11:18:00 -08002366
2367 // XXX - ignore error from setVideoScalingMode for now
2368 setVideoScalingMode(mVideoScalingMode);
Chong Zhang13d6faa2014-08-22 15:35:28 -07002369
2370 if (mDriver != NULL) {
2371 sp<NuPlayerDriver> driver = mDriver.promote();
2372 if (driver != NULL) {
2373 driver->notifySetSurfaceComplete();
2374 }
2375 }
Andreas Huber57a339c2012-12-03 11:18:00 -08002376}
2377
Chong Zhangf8d71772014-11-26 15:08:34 -08002378void NuPlayer::performResumeDecoders(bool needNotify) {
2379 if (needNotify) {
2380 mResumePending = true;
2381 if (mVideoDecoder == NULL) {
2382 // if audio-only, we can notify seek complete now,
2383 // as the resume operation will be relatively fast.
2384 finishResume();
2385 }
2386 }
2387
Chong Zhang7137ec72014-11-12 16:41:05 -08002388 if (mVideoDecoder != NULL) {
Chong Zhangf8d71772014-11-26 15:08:34 -08002389 // When there is continuous seek, MediaPlayer will cache the seek
2390 // position, and send down new seek request when previous seek is
2391 // complete. Let's wait for at least one video output frame before
2392 // notifying seek complete, so that the video thumbnail gets updated
2393 // when seekbar is dragged.
2394 mVideoDecoder->signalResume(needNotify);
Chong Zhang7137ec72014-11-12 16:41:05 -08002395 }
2396
2397 if (mAudioDecoder != NULL) {
Chong Zhangf8d71772014-11-26 15:08:34 -08002398 mAudioDecoder->signalResume(false /* needNotify */);
2399 }
2400}
2401
2402void NuPlayer::finishResume() {
2403 if (mResumePending) {
2404 mResumePending = false;
Wei Jia1061c9c2015-05-19 16:02:17 -07002405 notifyDriverSeekComplete();
2406 }
2407}
2408
2409void NuPlayer::notifyDriverSeekComplete() {
2410 if (mDriver != NULL) {
2411 sp<NuPlayerDriver> driver = mDriver.promote();
2412 if (driver != NULL) {
2413 driver->notifySeekComplete();
Chong Zhangf8d71772014-11-26 15:08:34 -08002414 }
Chong Zhang7137ec72014-11-12 16:41:05 -08002415 }
2416}
2417
Andreas Huber9575c962013-02-05 13:59:56 -08002418void NuPlayer::onSourceNotify(const sp<AMessage> &msg) {
2419 int32_t what;
2420 CHECK(msg->findInt32("what", &what));
2421
2422 switch (what) {
Lajos Molnarfcd3e942015-03-31 10:06:48 -07002423 case Source::kWhatInstantiateSecureDecoders:
2424 {
2425 if (mSource == NULL) {
2426 // This is a stale notification from a source that was
2427 // asynchronously preparing when the client called reset().
2428 // We handled the reset, the source is gone.
2429 break;
2430 }
2431
2432 sp<AMessage> reply;
2433 CHECK(msg->findMessage("reply", &reply));
2434 status_t err = onInstantiateSecureDecoders();
2435 reply->setInt32("err", err);
2436 reply->post();
2437 break;
2438 }
2439
Andreas Huber9575c962013-02-05 13:59:56 -08002440 case Source::kWhatPrepared:
2441 {
Hassan Shojaniacefac142017-02-06 21:02:02 -08002442 ALOGV("NuPlayer::onSourceNotify Source::kWhatPrepared source: %p", mSource.get());
Andreas Huberb5f28d42013-04-25 15:11:19 -07002443 if (mSource == NULL) {
2444 // This is a stale notification from a source that was
2445 // asynchronously preparing when the client called reset().
2446 // We handled the reset, the source is gone.
2447 break;
2448 }
2449
Andreas Huberec0c5972013-02-05 14:47:13 -08002450 int32_t err;
2451 CHECK(msg->findInt32("err", &err));
2452
Lajos Molnarfcd3e942015-03-31 10:06:48 -07002453 if (err != OK) {
2454 // shut down potential secure codecs in case client never calls reset
2455 mDeferredActions.push_back(
2456 new FlushDecoderAction(FLUSH_CMD_SHUTDOWN /* audio */,
2457 FLUSH_CMD_SHUTDOWN /* video */));
2458 processDeferredActions();
Wei Jia8a092d32016-06-03 14:57:24 -07002459 } else {
2460 mPrepared = true;
Lajos Molnarfcd3e942015-03-31 10:06:48 -07002461 }
2462
Andreas Huber9575c962013-02-05 13:59:56 -08002463 sp<NuPlayerDriver> driver = mDriver.promote();
2464 if (driver != NULL) {
Marco Nelissendd114d12014-05-28 15:23:14 -07002465 // notify duration first, so that it's definitely set when
2466 // the app received the "prepare complete" callback.
2467 int64_t durationUs;
2468 if (mSource->getDuration(&durationUs) == OK) {
2469 driver->notifyDuration(durationUs);
2470 }
Andreas Huberec0c5972013-02-05 14:47:13 -08002471 driver->notifyPrepareCompleted(err);
Andreas Huber9575c962013-02-05 13:59:56 -08002472 }
Andreas Huber99759402013-04-01 14:28:31 -07002473
Andreas Huber9575c962013-02-05 13:59:56 -08002474 break;
2475 }
2476
Hassan Shojaniacefac142017-02-06 21:02:02 -08002477 // Modular DRM
2478 case Source::kWhatDrmInfo:
2479 {
2480 Parcel parcel;
2481 sp<ABuffer> drmInfo;
2482 CHECK(msg->findBuffer("drmInfo", &drmInfo));
2483 parcel.setData(drmInfo->data(), drmInfo->size());
2484
2485 ALOGV("onSourceNotify() kWhatDrmInfo MEDIA_DRM_INFO drmInfo: %p parcel size: %zu",
2486 drmInfo.get(), parcel.dataSize());
2487
2488 notifyListener(MEDIA_DRM_INFO, 0 /* ext1 */, 0 /* ext2 */, &parcel);
2489
2490 break;
2491 }
2492
Andreas Huber9575c962013-02-05 13:59:56 -08002493 case Source::kWhatFlagsChanged:
2494 {
2495 uint32_t flags;
2496 CHECK(msg->findInt32("flags", (int32_t *)&flags));
2497
Chong Zhang4b7069d2013-09-11 12:52:43 -07002498 sp<NuPlayerDriver> driver = mDriver.promote();
2499 if (driver != NULL) {
Hassan Shojaniacefac142017-02-06 21:02:02 -08002500
2501 ALOGV("onSourceNotify() kWhatFlagsChanged FLAG_CAN_PAUSE: %d "
2502 "FLAG_CAN_SEEK_BACKWARD: %d \n\t\t\t\t FLAG_CAN_SEEK_FORWARD: %d "
2503 "FLAG_CAN_SEEK: %d FLAG_DYNAMIC_DURATION: %d \n"
2504 "\t\t\t\t FLAG_SECURE: %d FLAG_PROTECTED: %d",
2505 (flags & Source::FLAG_CAN_PAUSE) != 0,
2506 (flags & Source::FLAG_CAN_SEEK_BACKWARD) != 0,
2507 (flags & Source::FLAG_CAN_SEEK_FORWARD) != 0,
2508 (flags & Source::FLAG_CAN_SEEK) != 0,
2509 (flags & Source::FLAG_DYNAMIC_DURATION) != 0,
2510 (flags & Source::FLAG_SECURE) != 0,
2511 (flags & Source::FLAG_PROTECTED) != 0);
2512
Wei Jia895651b2014-12-10 17:31:52 -08002513 if ((flags & NuPlayer::Source::FLAG_CAN_SEEK) == 0) {
2514 driver->notifyListener(
2515 MEDIA_INFO, MEDIA_INFO_NOT_SEEKABLE, 0);
2516 }
Chong Zhang4b7069d2013-09-11 12:52:43 -07002517 driver->notifyFlagsChanged(flags);
2518 }
2519
Andreas Huber9575c962013-02-05 13:59:56 -08002520 if ((mSourceFlags & Source::FLAG_DYNAMIC_DURATION)
2521 && (!(flags & Source::FLAG_DYNAMIC_DURATION))) {
2522 cancelPollDuration();
2523 } else if (!(mSourceFlags & Source::FLAG_DYNAMIC_DURATION)
2524 && (flags & Source::FLAG_DYNAMIC_DURATION)
2525 && (mAudioDecoder != NULL || mVideoDecoder != NULL)) {
2526 schedulePollDuration();
2527 }
2528
2529 mSourceFlags = flags;
2530 break;
2531 }
2532
2533 case Source::kWhatVideoSizeChanged:
2534 {
Chong Zhangced1c2f2014-08-08 15:22:35 -07002535 sp<AMessage> format;
2536 CHECK(msg->findMessage("format", &format));
Andreas Huber9575c962013-02-05 13:59:56 -08002537
Chong Zhangced1c2f2014-08-08 15:22:35 -07002538 updateVideoSize(format);
Andreas Huber9575c962013-02-05 13:59:56 -08002539 break;
2540 }
2541
Chong Zhang2a3cc9a2014-08-21 17:48:26 -07002542 case Source::kWhatBufferingUpdate:
2543 {
2544 int32_t percentage;
2545 CHECK(msg->findInt32("percentage", &percentage));
2546
2547 notifyListener(MEDIA_BUFFERING_UPDATE, percentage, 0);
2548 break;
2549 }
2550
Chong Zhangefbb6192015-01-30 17:13:27 -08002551 case Source::kWhatPauseOnBufferingStart:
2552 {
2553 // ignore if not playing
Chong Zhang8a048332015-05-06 15:16:28 -07002554 if (mStarted) {
Chong Zhangefbb6192015-01-30 17:13:27 -08002555 ALOGI("buffer low, pausing...");
2556
Ray Essick58e0f7a2017-11-21 10:59:38 -08002557 startRebufferingTimer();
Chong Zhang8a048332015-05-06 15:16:28 -07002558 mPausedForBuffering = true;
Chong Zhangefbb6192015-01-30 17:13:27 -08002559 onPause();
2560 }
Wei Jiabfe82072016-05-20 09:21:50 -07002561 notifyListener(MEDIA_INFO, MEDIA_INFO_BUFFERING_START, 0);
Roger Jönssonb50e83e2013-01-21 16:26:41 +01002562 break;
2563 }
2564
Chong Zhangefbb6192015-01-30 17:13:27 -08002565 case Source::kWhatResumeOnBufferingEnd:
2566 {
2567 // ignore if not playing
Chong Zhang8a048332015-05-06 15:16:28 -07002568 if (mStarted) {
Chong Zhangefbb6192015-01-30 17:13:27 -08002569 ALOGI("buffer ready, resuming...");
2570
Ray Essickeda32522018-02-28 12:08:28 -08002571 updateRebufferingTimer(true /* stopping */, false /* exiting */);
Chong Zhang8a048332015-05-06 15:16:28 -07002572 mPausedForBuffering = false;
2573
2574 // do not resume yet if client didn't unpause
2575 if (!mPausedByClient) {
2576 onResume();
2577 }
Chong Zhangefbb6192015-01-30 17:13:27 -08002578 }
Wei Jia3bed45a2016-02-17 11:06:47 -08002579 notifyListener(MEDIA_INFO, MEDIA_INFO_BUFFERING_END, 0);
Roger Jönssonb50e83e2013-01-21 16:26:41 +01002580 break;
2581 }
2582
Chong Zhangefbb6192015-01-30 17:13:27 -08002583 case Source::kWhatCacheStats:
2584 {
2585 int32_t kbps;
2586 CHECK(msg->findInt32("bandwidth", &kbps));
2587
2588 notifyListener(MEDIA_INFO, MEDIA_INFO_NETWORK_BANDWIDTH, kbps);
2589 break;
2590 }
2591
Chong Zhangdcb89b32013-08-06 09:44:47 -07002592 case Source::kWhatSubtitleData:
2593 {
2594 sp<ABuffer> buffer;
2595 CHECK(msg->findBuffer("buffer", &buffer));
2596
Chong Zhang404fced2014-06-11 14:45:31 -07002597 sendSubtitleData(buffer, 0 /* baseIndex */);
Chong Zhangdcb89b32013-08-06 09:44:47 -07002598 break;
2599 }
2600
Robert Shih08528432015-04-08 09:06:54 -07002601 case Source::kWhatTimedMetaData:
2602 {
2603 sp<ABuffer> buffer;
2604 if (!msg->findBuffer("buffer", &buffer)) {
2605 notifyListener(MEDIA_INFO, MEDIA_INFO_METADATA_UPDATE, 0);
2606 } else {
2607 sendTimedMetaData(buffer);
2608 }
2609 break;
2610 }
2611
Robert Shihd3b0bbb2014-07-23 15:00:25 -07002612 case Source::kWhatTimedTextData:
2613 {
2614 int32_t generation;
2615 if (msg->findInt32("generation", &generation)
2616 && generation != mTimedTextGeneration) {
2617 break;
2618 }
2619
2620 sp<ABuffer> buffer;
2621 CHECK(msg->findBuffer("buffer", &buffer));
2622
2623 sp<NuPlayerDriver> driver = mDriver.promote();
2624 if (driver == NULL) {
2625 break;
2626 }
2627
2628 int posMs;
2629 int64_t timeUs, posUs;
2630 driver->getCurrentPosition(&posMs);
Patrik2 Carlsson24d484b2015-01-27 16:49:45 +01002631 posUs = (int64_t) posMs * 1000ll;
Robert Shihd3b0bbb2014-07-23 15:00:25 -07002632 CHECK(buffer->meta()->findInt64("timeUs", &timeUs));
2633
2634 if (posUs < timeUs) {
2635 if (!msg->findInt32("generation", &generation)) {
2636 msg->setInt32("generation", mTimedTextGeneration);
2637 }
2638 msg->post(timeUs - posUs);
2639 } else {
2640 sendTimedTextData(buffer);
2641 }
2642 break;
2643 }
2644
Andreas Huber14f76722013-01-15 09:04:18 -08002645 case Source::kWhatQueueDecoderShutdown:
2646 {
2647 int32_t audio, video;
2648 CHECK(msg->findInt32("audio", &audio));
2649 CHECK(msg->findInt32("video", &video));
2650
2651 sp<AMessage> reply;
2652 CHECK(msg->findMessage("reply", &reply));
2653
2654 queueDecoderShutdown(audio, video, reply);
2655 break;
2656 }
2657
Ronghua Wu80276872014-08-28 15:50:29 -07002658 case Source::kWhatDrmNoLicense:
2659 {
2660 notifyListener(MEDIA_ERROR, MEDIA_ERROR_UNKNOWN, ERROR_DRM_NO_LICENSE);
2661 break;
2662 }
2663
Andreas Huber9575c962013-02-05 13:59:56 -08002664 default:
2665 TRESPASS();
2666 }
2667}
2668
Chong Zhanga7fa1d92014-06-11 14:49:23 -07002669void NuPlayer::onClosedCaptionNotify(const sp<AMessage> &msg) {
2670 int32_t what;
2671 CHECK(msg->findInt32("what", &what));
2672
2673 switch (what) {
2674 case NuPlayer::CCDecoder::kWhatClosedCaptionData:
2675 {
2676 sp<ABuffer> buffer;
2677 CHECK(msg->findBuffer("buffer", &buffer));
2678
2679 size_t inbandTracks = 0;
2680 if (mSource != NULL) {
2681 inbandTracks = mSource->getTrackCount();
2682 }
2683
2684 sendSubtitleData(buffer, inbandTracks);
2685 break;
2686 }
2687
2688 case NuPlayer::CCDecoder::kWhatTrackAdded:
2689 {
2690 notifyListener(MEDIA_INFO, MEDIA_INFO_METADATA_UPDATE, 0);
2691
2692 break;
2693 }
2694
2695 default:
2696 TRESPASS();
2697 }
2698
2699
2700}
2701
Chong Zhang404fced2014-06-11 14:45:31 -07002702void NuPlayer::sendSubtitleData(const sp<ABuffer> &buffer, int32_t baseIndex) {
2703 int32_t trackIndex;
2704 int64_t timeUs, durationUs;
Robert Shihd83d4f42018-02-24 19:02:46 -08002705 CHECK(buffer->meta()->findInt32("track-index", &trackIndex));
Chong Zhang404fced2014-06-11 14:45:31 -07002706 CHECK(buffer->meta()->findInt64("timeUs", &timeUs));
2707 CHECK(buffer->meta()->findInt64("durationUs", &durationUs));
2708
2709 Parcel in;
2710 in.writeInt32(trackIndex + baseIndex);
2711 in.writeInt64(timeUs);
2712 in.writeInt64(durationUs);
2713 in.writeInt32(buffer->size());
2714 in.writeInt32(buffer->size());
2715 in.write(buffer->data(), buffer->size());
2716
2717 notifyListener(MEDIA_SUBTITLE_DATA, 0, 0, &in);
2718}
Robert Shihd3b0bbb2014-07-23 15:00:25 -07002719
Robert Shih08528432015-04-08 09:06:54 -07002720void NuPlayer::sendTimedMetaData(const sp<ABuffer> &buffer) {
2721 int64_t timeUs;
2722 CHECK(buffer->meta()->findInt64("timeUs", &timeUs));
2723
2724 Parcel in;
2725 in.writeInt64(timeUs);
2726 in.writeInt32(buffer->size());
2727 in.writeInt32(buffer->size());
2728 in.write(buffer->data(), buffer->size());
2729
2730 notifyListener(MEDIA_META_DATA, 0, 0, &in);
2731}
2732
Robert Shihd3b0bbb2014-07-23 15:00:25 -07002733void NuPlayer::sendTimedTextData(const sp<ABuffer> &buffer) {
2734 const void *data;
2735 size_t size = 0;
2736 int64_t timeUs;
Marco Nelissen55e2f4c2015-09-04 15:57:15 -07002737 int32_t flag = TextDescriptions::IN_BAND_TEXT_3GPP;
Robert Shihd3b0bbb2014-07-23 15:00:25 -07002738
2739 AString mime;
2740 CHECK(buffer->meta()->findString("mime", &mime));
2741 CHECK(strcasecmp(mime.c_str(), MEDIA_MIMETYPE_TEXT_3GPP) == 0);
2742
2743 data = buffer->data();
2744 size = buffer->size();
2745
2746 Parcel parcel;
2747 if (size > 0) {
2748 CHECK(buffer->meta()->findInt64("timeUs", &timeUs));
Marco Nelissen55e2f4c2015-09-04 15:57:15 -07002749 int32_t global = 0;
2750 if (buffer->meta()->findInt32("global", &global) && global) {
2751 flag |= TextDescriptions::GLOBAL_DESCRIPTIONS;
2752 } else {
2753 flag |= TextDescriptions::LOCAL_DESCRIPTIONS;
2754 }
Robert Shihd3b0bbb2014-07-23 15:00:25 -07002755 TextDescriptions::getParcelOfDescriptions(
2756 (const uint8_t *)data, size, flag, timeUs / 1000, &parcel);
2757 }
2758
2759 if ((parcel.dataSize() > 0)) {
2760 notifyListener(MEDIA_TIMED_TEXT, 0, 0, &parcel);
2761 } else { // send an empty timed text
2762 notifyListener(MEDIA_TIMED_TEXT, 0, 0);
2763 }
2764}
Hassan Shojaniacefac142017-02-06 21:02:02 -08002765
Hassan Shojaniaff63de72017-04-26 15:10:42 -07002766const char *NuPlayer::getDataSourceType() {
2767 switch (mDataSourceType) {
2768 case DATA_SOURCE_TYPE_HTTP_LIVE:
2769 return "HTTPLive";
2770
2771 case DATA_SOURCE_TYPE_RTSP:
2772 return "RTSP";
2773
2774 case DATA_SOURCE_TYPE_GENERIC_URL:
2775 return "GenURL";
2776
2777 case DATA_SOURCE_TYPE_GENERIC_FD:
2778 return "GenFD";
2779
2780 case DATA_SOURCE_TYPE_MEDIA:
2781 return "Media";
2782
2783 case DATA_SOURCE_TYPE_STREAM:
2784 return "Stream";
2785
2786 case DATA_SOURCE_TYPE_NONE:
2787 default:
2788 return "None";
2789 }
2790 }
2791
Hassan Shojaniacefac142017-02-06 21:02:02 -08002792// Modular DRM begin
2793status_t NuPlayer::prepareDrm(const uint8_t uuid[16], const Vector<uint8_t> &drmSessionId)
2794{
2795 ALOGV("prepareDrm ");
2796
2797 // Passing to the looper anyway; called in a pre-config prepared state so no race on mCrypto
2798 sp<AMessage> msg = new AMessage(kWhatPrepareDrm, this);
2799 // synchronous call so just passing the address but with local copies of "const" args
2800 uint8_t UUID[16];
2801 memcpy(UUID, uuid, sizeof(UUID));
2802 Vector<uint8_t> sessionId = drmSessionId;
2803 msg->setPointer("uuid", (void*)UUID);
2804 msg->setPointer("drmSessionId", (void*)&sessionId);
2805
2806 sp<AMessage> response;
2807 status_t status = msg->postAndAwaitResponse(&response);
2808
2809 if (status == OK && response != NULL) {
2810 CHECK(response->findInt32("status", &status));
2811 ALOGV("prepareDrm ret: %d ", status);
2812 } else {
2813 ALOGE("prepareDrm err: %d", status);
2814 }
2815
2816 return status;
2817}
2818
2819status_t NuPlayer::releaseDrm()
2820{
2821 ALOGV("releaseDrm ");
2822
2823 sp<AMessage> msg = new AMessage(kWhatReleaseDrm, this);
2824
2825 sp<AMessage> response;
2826 status_t status = msg->postAndAwaitResponse(&response);
2827
2828 if (status == OK && response != NULL) {
2829 CHECK(response->findInt32("status", &status));
2830 ALOGV("releaseDrm ret: %d ", status);
2831 } else {
2832 ALOGE("releaseDrm err: %d", status);
2833 }
2834
2835 return status;
2836}
2837
2838status_t NuPlayer::onPrepareDrm(const sp<AMessage> &msg)
2839{
2840 // TODO change to ALOGV
2841 ALOGD("onPrepareDrm ");
2842
2843 status_t status = INVALID_OPERATION;
2844 if (mSource == NULL) {
2845 ALOGE("onPrepareDrm: No source. onPrepareDrm failed with %d.", status);
2846 return status;
2847 }
2848
2849 uint8_t *uuid;
2850 Vector<uint8_t> *drmSessionId;
2851 CHECK(msg->findPointer("uuid", (void**)&uuid));
2852 CHECK(msg->findPointer("drmSessionId", (void**)&drmSessionId));
2853
2854 status = OK;
2855 sp<ICrypto> crypto = NULL;
2856
2857 status = mSource->prepareDrm(uuid, *drmSessionId, &crypto);
2858 if (crypto == NULL) {
2859 ALOGE("onPrepareDrm: mSource->prepareDrm failed. status: %d", status);
2860 return status;
2861 }
2862 ALOGV("onPrepareDrm: mSource->prepareDrm succeeded");
2863
2864 if (mCrypto != NULL) {
2865 ALOGE("onPrepareDrm: Unexpected. Already having mCrypto: %p (%d)",
2866 mCrypto.get(), mCrypto->getStrongCount());
2867 mCrypto.clear();
2868 }
2869
2870 mCrypto = crypto;
2871 mIsDrmProtected = true;
2872 // TODO change to ALOGV
2873 ALOGD("onPrepareDrm: mCrypto: %p (%d)", mCrypto.get(),
2874 (mCrypto != NULL ? mCrypto->getStrongCount() : 0));
2875
2876 return status;
2877}
2878
2879status_t NuPlayer::onReleaseDrm()
2880{
2881 // TODO change to ALOGV
2882 ALOGD("onReleaseDrm ");
2883
Hassan Shojania50b20c92017-02-16 18:28:58 -08002884 if (!mIsDrmProtected) {
2885 ALOGW("onReleaseDrm: Unexpected. mIsDrmProtected is already false.");
2886 }
2887
2888 mIsDrmProtected = false;
Hassan Shojaniacefac142017-02-06 21:02:02 -08002889
2890 status_t status;
2891 if (mCrypto != NULL) {
Hassan Shojania355e8472017-05-12 10:33:16 -07002892 // notifying the source first before removing crypto from codec
2893 if (mSource != NULL) {
2894 mSource->releaseDrm();
2895 }
2896
Hassan Shojaniacefac142017-02-06 21:02:02 -08002897 status=OK;
2898 // first making sure the codecs have released their crypto reference
2899 const sp<DecoderBase> &videoDecoder = getDecoder(false/*audio*/);
2900 if (videoDecoder != NULL) {
2901 status = videoDecoder->releaseCrypto();
2902 ALOGV("onReleaseDrm: video decoder ret: %d", status);
2903 }
2904
2905 const sp<DecoderBase> &audioDecoder = getDecoder(true/*audio*/);
2906 if (audioDecoder != NULL) {
2907 status_t status_audio = audioDecoder->releaseCrypto();
2908 if (status == OK) { // otherwise, returning the first error
2909 status = status_audio;
2910 }
2911 ALOGV("onReleaseDrm: audio decoder ret: %d", status_audio);
2912 }
2913
2914 // TODO change to ALOGV
2915 ALOGD("onReleaseDrm: mCrypto: %p (%d)", mCrypto.get(),
2916 (mCrypto != NULL ? mCrypto->getStrongCount() : 0));
2917 mCrypto.clear();
2918 } else { // mCrypto == NULL
2919 ALOGE("onReleaseDrm: Unexpected. There is no crypto.");
2920 status = INVALID_OPERATION;
2921 }
2922
2923 return status;
2924}
2925// Modular DRM end
Andreas Huberb5f25f02013-02-05 10:14:26 -08002926////////////////////////////////////////////////////////////////////////////////
2927
Chong Zhangced1c2f2014-08-08 15:22:35 -07002928sp<AMessage> NuPlayer::Source::getFormat(bool audio) {
2929 sp<MetaData> meta = getFormatMeta(audio);
2930
2931 if (meta == NULL) {
2932 return NULL;
2933 }
2934
2935 sp<AMessage> msg = new AMessage;
2936
2937 if(convertMetaDataToMessage(meta, &msg) == OK) {
2938 return msg;
2939 }
2940 return NULL;
2941}
2942
Andreas Huber9575c962013-02-05 13:59:56 -08002943void NuPlayer::Source::notifyFlagsChanged(uint32_t flags) {
2944 sp<AMessage> notify = dupNotify();
2945 notify->setInt32("what", kWhatFlagsChanged);
2946 notify->setInt32("flags", flags);
2947 notify->post();
2948}
2949
Chong Zhangced1c2f2014-08-08 15:22:35 -07002950void NuPlayer::Source::notifyVideoSizeChanged(const sp<AMessage> &format) {
Andreas Huber9575c962013-02-05 13:59:56 -08002951 sp<AMessage> notify = dupNotify();
2952 notify->setInt32("what", kWhatVideoSizeChanged);
Chong Zhangced1c2f2014-08-08 15:22:35 -07002953 notify->setMessage("format", format);
Andreas Huber9575c962013-02-05 13:59:56 -08002954 notify->post();
2955}
2956
Andreas Huberec0c5972013-02-05 14:47:13 -08002957void NuPlayer::Source::notifyPrepared(status_t err) {
Hassan Shojaniacefac142017-02-06 21:02:02 -08002958 ALOGV("Source::notifyPrepared %d", err);
Andreas Huber9575c962013-02-05 13:59:56 -08002959 sp<AMessage> notify = dupNotify();
2960 notify->setInt32("what", kWhatPrepared);
Andreas Huberec0c5972013-02-05 14:47:13 -08002961 notify->setInt32("err", err);
Andreas Huber9575c962013-02-05 13:59:56 -08002962 notify->post();
2963}
2964
Hassan Shojaniacefac142017-02-06 21:02:02 -08002965void NuPlayer::Source::notifyDrmInfo(const sp<ABuffer> &drmInfoBuffer)
2966{
2967 ALOGV("Source::notifyDrmInfo");
2968
2969 sp<AMessage> notify = dupNotify();
2970 notify->setInt32("what", kWhatDrmInfo);
2971 notify->setBuffer("drmInfo", drmInfoBuffer);
2972
2973 notify->post();
2974}
2975
Lajos Molnarfcd3e942015-03-31 10:06:48 -07002976void NuPlayer::Source::notifyInstantiateSecureDecoders(const sp<AMessage> &reply) {
2977 sp<AMessage> notify = dupNotify();
2978 notify->setInt32("what", kWhatInstantiateSecureDecoders);
2979 notify->setMessage("reply", reply);
2980 notify->post();
2981}
2982
Andreas Huber84333e02014-02-07 15:36:10 -08002983void NuPlayer::Source::onMessageReceived(const sp<AMessage> & /* msg */) {
Andreas Huberb5f25f02013-02-05 10:14:26 -08002984 TRESPASS();
2985}
2986
Andreas Huberf9334412010-12-15 15:17:42 -08002987} // namespace android