blob: f2e3e7ce86371571b63a799ac9401360957c7fb3 [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>
Lajos Molnar09524832014-07-17 14:29:51 -070051#include <media/stagefright/MediaBuffer.h>
Andreas Huber3fe62152011-09-16 15:09:22 -070052#include <media/stagefright/MediaDefs.h>
Andreas Huberf9334412010-12-15 15:17:42 -080053#include <media/stagefright/MediaErrors.h>
54#include <media/stagefright/MetaData.h>
Lajos Molnar1de1e252015-04-30 18:18:34 -070055
Andy McFadden8ba01022012-12-18 09:46:54 -080056#include <gui/IGraphicBufferProducer.h>
Lajos Molnar1de1e252015-04-30 18:18:34 -070057#include <gui/Surface.h>
Andreas Huberf9334412010-12-15 15:17:42 -080058
Andreas Huber3fe62152011-09-16 15:09:22 -070059#include "avc_utils.h"
60
Andreas Huber84066782011-08-16 09:34:26 -070061#include "ESDS.h"
62#include <media/stagefright/Utils.h>
63
Andreas Huberf9334412010-12-15 15:17:42 -080064namespace android {
65
Andreas Hubera1f8ab02012-11-30 10:53:22 -080066struct NuPlayer::Action : public RefBase {
67 Action() {}
68
69 virtual void execute(NuPlayer *player) = 0;
70
71private:
72 DISALLOW_EVIL_CONSTRUCTORS(Action);
73};
74
75struct NuPlayer::SeekAction : public Action {
Wei Jiac5de0912016-11-18 10:22:14 -080076 explicit SeekAction(int64_t seekTimeUs, MediaPlayerSeekMode mode)
Wei Jia14486822016-11-02 17:51:30 -070077 : mSeekTimeUs(seekTimeUs),
Wei Jiac5de0912016-11-18 10:22:14 -080078 mMode(mode) {
Andreas Hubera1f8ab02012-11-30 10:53:22 -080079 }
80
81 virtual void execute(NuPlayer *player) {
Wei Jiac5de0912016-11-18 10:22:14 -080082 player->performSeek(mSeekTimeUs, mMode);
Andreas Hubera1f8ab02012-11-30 10:53:22 -080083 }
84
85private:
86 int64_t mSeekTimeUs;
Wei Jiac5de0912016-11-18 10:22:14 -080087 MediaPlayerSeekMode mMode;
Andreas Hubera1f8ab02012-11-30 10:53:22 -080088
89 DISALLOW_EVIL_CONSTRUCTORS(SeekAction);
90};
91
Chong Zhangf8d71772014-11-26 15:08:34 -080092struct NuPlayer::ResumeDecoderAction : public Action {
Chih-Hung Hsieh090ef602016-04-27 10:39:54 -070093 explicit ResumeDecoderAction(bool needNotify)
Chong Zhangf8d71772014-11-26 15:08:34 -080094 : mNeedNotify(needNotify) {
95 }
96
97 virtual void execute(NuPlayer *player) {
98 player->performResumeDecoders(mNeedNotify);
99 }
100
101private:
102 bool mNeedNotify;
103
104 DISALLOW_EVIL_CONSTRUCTORS(ResumeDecoderAction);
105};
106
Andreas Huber57a339c2012-12-03 11:18:00 -0800107struct NuPlayer::SetSurfaceAction : public Action {
Chih-Hung Hsieh090ef602016-04-27 10:39:54 -0700108 explicit SetSurfaceAction(const sp<Surface> &surface)
Lajos Molnar1de1e252015-04-30 18:18:34 -0700109 : mSurface(surface) {
Andreas Huber57a339c2012-12-03 11:18:00 -0800110 }
111
112 virtual void execute(NuPlayer *player) {
Lajos Molnar1de1e252015-04-30 18:18:34 -0700113 player->performSetSurface(mSurface);
Andreas Huber57a339c2012-12-03 11:18:00 -0800114 }
115
116private:
Lajos Molnar1de1e252015-04-30 18:18:34 -0700117 sp<Surface> mSurface;
Andreas Huber57a339c2012-12-03 11:18:00 -0800118
119 DISALLOW_EVIL_CONSTRUCTORS(SetSurfaceAction);
120};
121
Wei Jiafef808d2014-10-31 17:57:05 -0700122struct NuPlayer::FlushDecoderAction : public Action {
123 FlushDecoderAction(FlushCommand audio, FlushCommand video)
Andreas Huber14f76722013-01-15 09:04:18 -0800124 : mAudio(audio),
125 mVideo(video) {
126 }
127
128 virtual void execute(NuPlayer *player) {
Wei Jiafef808d2014-10-31 17:57:05 -0700129 player->performDecoderFlush(mAudio, mVideo);
Andreas Huber14f76722013-01-15 09:04:18 -0800130 }
131
132private:
Wei Jiafef808d2014-10-31 17:57:05 -0700133 FlushCommand mAudio;
134 FlushCommand mVideo;
Andreas Huber14f76722013-01-15 09:04:18 -0800135
Wei Jiafef808d2014-10-31 17:57:05 -0700136 DISALLOW_EVIL_CONSTRUCTORS(FlushDecoderAction);
Andreas Huber14f76722013-01-15 09:04:18 -0800137};
138
139struct NuPlayer::PostMessageAction : public Action {
Chih-Hung Hsieh090ef602016-04-27 10:39:54 -0700140 explicit PostMessageAction(const sp<AMessage> &msg)
Andreas Huber14f76722013-01-15 09:04:18 -0800141 : mMessage(msg) {
142 }
143
144 virtual void execute(NuPlayer *) {
145 mMessage->post();
146 }
147
148private:
149 sp<AMessage> mMessage;
150
151 DISALLOW_EVIL_CONSTRUCTORS(PostMessageAction);
152};
153
Andreas Hubera1f8ab02012-11-30 10:53:22 -0800154// Use this if there's no state necessary to save in order to execute
155// the action.
156struct NuPlayer::SimpleAction : public Action {
157 typedef void (NuPlayer::*ActionFunc)();
158
Chih-Hung Hsieh090ef602016-04-27 10:39:54 -0700159 explicit SimpleAction(ActionFunc func)
Andreas Hubera1f8ab02012-11-30 10:53:22 -0800160 : mFunc(func) {
161 }
162
163 virtual void execute(NuPlayer *player) {
164 (player->*mFunc)();
165 }
166
167private:
168 ActionFunc mFunc;
169
170 DISALLOW_EVIL_CONSTRUCTORS(SimpleAction);
171};
172
Andreas Huberf9334412010-12-15 15:17:42 -0800173////////////////////////////////////////////////////////////////////////////////
174
Ronghua Wu68845c12015-07-21 09:50:48 -0700175NuPlayer::NuPlayer(pid_t pid)
Andreas Huber9b80c2b2011-06-30 15:47:02 -0700176 : mUIDValid(false),
Ronghua Wu68845c12015-07-21 09:50:48 -0700177 mPID(pid),
Andreas Huber9575c962013-02-05 13:59:56 -0800178 mSourceFlags(0),
Wei Jiabc2fb722014-07-08 16:37:57 -0700179 mOffloadAudio(false),
Wei Jia88703c32014-08-06 11:24:07 -0700180 mAudioDecoderGeneration(0),
181 mVideoDecoderGeneration(0),
Wei Jia57568df2014-09-22 10:16:29 -0700182 mRendererGeneration(0),
Robert Shih1a5c8592015-08-04 18:07:44 -0700183 mPreviousSeekTimeUs(0),
Andreas Huber9b80c2b2011-06-30 15:47:02 -0700184 mAudioEOS(false),
Andreas Huberf9334412010-12-15 15:17:42 -0800185 mVideoEOS(false),
Andreas Huber5bc087c2010-12-23 10:27:40 -0800186 mScanSourcesPending(false),
Andreas Huber1aef2112011-01-04 14:01:29 -0800187 mScanSourcesGeneration(0),
Andreas Huberb7c8e912012-11-27 15:02:53 -0800188 mPollDurationGeneration(0),
Robert Shihd3b0bbb2014-07-23 15:00:25 -0700189 mTimedTextGeneration(0),
Andreas Huberf9334412010-12-15 15:17:42 -0800190 mFlushingAudio(NONE),
Andreas Huber1aef2112011-01-04 14:01:29 -0800191 mFlushingVideo(NONE),
Chong Zhangf8d71772014-11-26 15:08:34 -0800192 mResumePending(false),
Andreas Huber57a339c2012-12-03 11:18:00 -0800193 mVideoScalingMode(NATIVE_WINDOW_SCALING_MODE_SCALE_TO_WINDOW),
Lajos Molnar3a474aa2015-04-24 17:10:07 -0700194 mPlaybackSettings(AUDIO_PLAYBACK_RATE_DEFAULT),
195 mVideoFpsHint(-1.f),
Chong Zhangefbb6192015-01-30 17:13:27 -0800196 mStarted(false),
Wei Jia8a092d32016-06-03 14:57:24 -0700197 mPrepared(false),
Ronghua Wu64c2d172015-10-07 16:52:19 -0700198 mResetting(false),
Robert Shih0c61a0d2015-07-06 15:09:10 -0700199 mSourceStarted(false),
Chong Zhangefbb6192015-01-30 17:13:27 -0800200 mPaused(false),
Wei Jia71c75e02016-02-04 09:40:47 -0800201 mPausedByClient(true),
Chong Zhang8a048332015-05-06 15:16:28 -0700202 mPausedForBuffering(false) {
Andy Hung8d121d42014-10-03 09:53:53 -0700203 clearFlushComplete();
Andreas Huberf9334412010-12-15 15:17:42 -0800204}
205
206NuPlayer::~NuPlayer() {
207}
208
Andreas Huber9b80c2b2011-06-30 15:47:02 -0700209void NuPlayer::setUID(uid_t uid) {
210 mUIDValid = true;
211 mUID = uid;
212}
213
Andreas Huber43c3e6c2011-01-05 12:17:08 -0800214void NuPlayer::setDriver(const wp<NuPlayerDriver> &driver) {
215 mDriver = driver;
Andreas Huberf9334412010-12-15 15:17:42 -0800216}
217
Andreas Huber9575c962013-02-05 13:59:56 -0800218void NuPlayer::setDataSourceAsync(const sp<IStreamSource> &source) {
Lajos Molnar1d15ab52015-03-04 16:46:34 -0800219 sp<AMessage> msg = new AMessage(kWhatSetDataSource, this);
Andreas Huberf9334412010-12-15 15:17:42 -0800220
Lajos Molnar1d15ab52015-03-04 16:46:34 -0800221 sp<AMessage> notify = new AMessage(kWhatSourceNotify, this);
Andreas Huberb5f25f02013-02-05 10:14:26 -0800222
Andreas Huber240abcc2014-02-13 13:32:37 -0800223 msg->setObject("source", new StreamingSource(notify, source));
Andreas Huber5bc087c2010-12-23 10:27:40 -0800224 msg->post();
225}
Andreas Huberf9334412010-12-15 15:17:42 -0800226
Andreas Huberafed0e12011-09-20 15:39:58 -0700227static bool IsHTTPLiveURL(const char *url) {
228 if (!strncasecmp("http://", url, 7)
Andreas Huber99759402013-04-01 14:28:31 -0700229 || !strncasecmp("https://", url, 8)
230 || !strncasecmp("file://", url, 7)) {
Andreas Huberafed0e12011-09-20 15:39:58 -0700231 size_t len = strlen(url);
232 if (len >= 5 && !strcasecmp(".m3u8", &url[len - 5])) {
233 return true;
234 }
235
236 if (strstr(url,"m3u8")) {
237 return true;
238 }
239 }
240
241 return false;
242}
243
Andreas Huber9575c962013-02-05 13:59:56 -0800244void NuPlayer::setDataSourceAsync(
Andreas Huber1b86fe02014-01-29 11:13:26 -0800245 const sp<IMediaHTTPService> &httpService,
246 const char *url,
247 const KeyedVector<String8, String8> *headers) {
Chong Zhang3de157d2014-08-05 20:54:44 -0700248
Lajos Molnar1d15ab52015-03-04 16:46:34 -0800249 sp<AMessage> msg = new AMessage(kWhatSetDataSource, this);
Oscar Rydhé7a33b772012-02-20 10:15:48 +0100250 size_t len = strlen(url);
Andreas Huber5bc087c2010-12-23 10:27:40 -0800251
Lajos Molnar1d15ab52015-03-04 16:46:34 -0800252 sp<AMessage> notify = new AMessage(kWhatSourceNotify, this);
Andreas Huberb5f25f02013-02-05 10:14:26 -0800253
Andreas Huberafed0e12011-09-20 15:39:58 -0700254 sp<Source> source;
255 if (IsHTTPLiveURL(url)) {
Andreas Huber81e68442014-02-05 11:52:33 -0800256 source = new HTTPLiveSource(notify, httpService, url, headers);
Andreas Huberafed0e12011-09-20 15:39:58 -0700257 } else if (!strncasecmp(url, "rtsp://", 7)) {
Andreas Huber1b86fe02014-01-29 11:13:26 -0800258 source = new RTSPSource(
259 notify, httpService, url, headers, mUIDValid, mUID);
Oscar Rydhé7a33b772012-02-20 10:15:48 +0100260 } else if ((!strncasecmp(url, "http://", 7)
261 || !strncasecmp(url, "https://", 8))
262 && ((len >= 4 && !strcasecmp(".sdp", &url[len - 4]))
263 || strstr(url, ".sdp?"))) {
Andreas Huber1b86fe02014-01-29 11:13:26 -0800264 source = new RTSPSource(
265 notify, httpService, url, headers, mUIDValid, mUID, true);
Andreas Huber2bfdd422011-10-11 15:24:07 -0700266 } else {
Chong Zhang3de157d2014-08-05 20:54:44 -0700267 sp<GenericSource> genericSource =
268 new GenericSource(notify, mUIDValid, mUID);
Andreas Huber2bfdd422011-10-11 15:24:07 -0700269
Chong Zhanga19f33e2014-08-07 15:35:07 -0700270 status_t err = genericSource->setDataSource(httpService, url, headers);
Chong Zhang3de157d2014-08-05 20:54:44 -0700271
272 if (err == OK) {
273 source = genericSource;
274 } else {
Chong Zhanga19f33e2014-08-07 15:35:07 -0700275 ALOGE("Failed to set data source!");
Chong Zhang3de157d2014-08-05 20:54:44 -0700276 }
277 }
Andreas Huberafed0e12011-09-20 15:39:58 -0700278 msg->setObject("source", source);
279 msg->post();
280}
281
Andreas Huber9575c962013-02-05 13:59:56 -0800282void NuPlayer::setDataSourceAsync(int fd, int64_t offset, int64_t length) {
Lajos Molnar1d15ab52015-03-04 16:46:34 -0800283 sp<AMessage> msg = new AMessage(kWhatSetDataSource, this);
Andreas Huberafed0e12011-09-20 15:39:58 -0700284
Lajos Molnar1d15ab52015-03-04 16:46:34 -0800285 sp<AMessage> notify = new AMessage(kWhatSourceNotify, this);
Andreas Huberb5f25f02013-02-05 10:14:26 -0800286
Chong Zhang3de157d2014-08-05 20:54:44 -0700287 sp<GenericSource> source =
288 new GenericSource(notify, mUIDValid, mUID);
289
Chong Zhanga19f33e2014-08-07 15:35:07 -0700290 status_t err = source->setDataSource(fd, offset, length);
Chong Zhang3de157d2014-08-05 20:54:44 -0700291
292 if (err != OK) {
Chong Zhanga19f33e2014-08-07 15:35:07 -0700293 ALOGE("Failed to set data source!");
Chong Zhang3de157d2014-08-05 20:54:44 -0700294 source = NULL;
295 }
296
Andreas Huberafed0e12011-09-20 15:39:58 -0700297 msg->setObject("source", source);
Andreas Huberf9334412010-12-15 15:17:42 -0800298 msg->post();
299}
300
Chris Watkins99f31602015-03-20 13:06:33 -0700301void NuPlayer::setDataSourceAsync(const sp<DataSource> &dataSource) {
302 sp<AMessage> msg = new AMessage(kWhatSetDataSource, this);
303 sp<AMessage> notify = new AMessage(kWhatSourceNotify, this);
304
305 sp<GenericSource> source = new GenericSource(notify, mUIDValid, mUID);
306 status_t err = source->setDataSource(dataSource);
307
308 if (err != OK) {
309 ALOGE("Failed to set data source!");
310 source = NULL;
311 }
312
313 msg->setObject("source", source);
314 msg->post();
315}
316
Andreas Huber9575c962013-02-05 13:59:56 -0800317void NuPlayer::prepareAsync() {
Lajos Molnar1d15ab52015-03-04 16:46:34 -0800318 (new AMessage(kWhatPrepare, this))->post();
Andreas Huber9575c962013-02-05 13:59:56 -0800319}
320
Andreas Huber57a339c2012-12-03 11:18:00 -0800321void NuPlayer::setVideoSurfaceTextureAsync(
Andy McFadden8ba01022012-12-18 09:46:54 -0800322 const sp<IGraphicBufferProducer> &bufferProducer) {
Lajos Molnar1de1e252015-04-30 18:18:34 -0700323 sp<AMessage> msg = new AMessage(kWhatSetVideoSurface, this);
Andreas Huber57a339c2012-12-03 11:18:00 -0800324
Andy McFadden8ba01022012-12-18 09:46:54 -0800325 if (bufferProducer == NULL) {
Lajos Molnar1de1e252015-04-30 18:18:34 -0700326 msg->setObject("surface", NULL);
Andreas Huber57a339c2012-12-03 11:18:00 -0800327 } else {
Lajos Molnar1de1e252015-04-30 18:18:34 -0700328 msg->setObject("surface", new Surface(bufferProducer, true /* controlledByApp */));
Andreas Huber57a339c2012-12-03 11:18:00 -0800329 }
330
Andreas Huberf9334412010-12-15 15:17:42 -0800331 msg->post();
332}
333
334void NuPlayer::setAudioSink(const sp<MediaPlayerBase::AudioSink> &sink) {
Lajos Molnar1d15ab52015-03-04 16:46:34 -0800335 sp<AMessage> msg = new AMessage(kWhatSetAudioSink, this);
Andreas Huberf9334412010-12-15 15:17:42 -0800336 msg->setObject("sink", sink);
337 msg->post();
338}
339
340void NuPlayer::start() {
Lajos Molnar1d15ab52015-03-04 16:46:34 -0800341 (new AMessage(kWhatStart, this))->post();
Andreas Huberf9334412010-12-15 15:17:42 -0800342}
343
Lajos Molnar3a474aa2015-04-24 17:10:07 -0700344status_t NuPlayer::setPlaybackSettings(const AudioPlaybackRate &rate) {
345 // do some cursory validation of the settings here. audio modes are
346 // only validated when set on the audiosink.
347 if ((rate.mSpeed != 0.f && rate.mSpeed < AUDIO_TIMESTRETCH_SPEED_MIN)
348 || rate.mSpeed > AUDIO_TIMESTRETCH_SPEED_MAX
349 || rate.mPitch < AUDIO_TIMESTRETCH_SPEED_MIN
350 || rate.mPitch > AUDIO_TIMESTRETCH_SPEED_MAX) {
351 return BAD_VALUE;
352 }
353 sp<AMessage> msg = new AMessage(kWhatConfigPlayback, this);
354 writeToAMessage(msg, rate);
355 sp<AMessage> response;
356 status_t err = msg->postAndAwaitResponse(&response);
357 if (err == OK && response != NULL) {
358 CHECK(response->findInt32("err", &err));
359 }
360 return err;
361}
362
363status_t NuPlayer::getPlaybackSettings(AudioPlaybackRate *rate /* nonnull */) {
364 sp<AMessage> msg = new AMessage(kWhatGetPlaybackSettings, this);
365 sp<AMessage> response;
366 status_t err = msg->postAndAwaitResponse(&response);
367 if (err == OK && response != NULL) {
368 CHECK(response->findInt32("err", &err));
369 if (err == OK) {
370 readFromAMessage(response, rate);
371 }
372 }
373 return err;
374}
375
376status_t NuPlayer::setSyncSettings(const AVSyncSettings &sync, float videoFpsHint) {
377 sp<AMessage> msg = new AMessage(kWhatConfigSync, this);
378 writeToAMessage(msg, sync, videoFpsHint);
379 sp<AMessage> response;
380 status_t err = msg->postAndAwaitResponse(&response);
381 if (err == OK && response != NULL) {
382 CHECK(response->findInt32("err", &err));
383 }
384 return err;
385}
386
387status_t NuPlayer::getSyncSettings(
388 AVSyncSettings *sync /* nonnull */, float *videoFps /* nonnull */) {
389 sp<AMessage> msg = new AMessage(kWhatGetSyncSettings, this);
390 sp<AMessage> response;
391 status_t err = msg->postAndAwaitResponse(&response);
392 if (err == OK && response != NULL) {
393 CHECK(response->findInt32("err", &err));
394 if (err == OK) {
395 readFromAMessage(response, sync, videoFps);
396 }
397 }
398 return err;
Wei Jia98160162015-02-04 17:01:11 -0800399}
400
Andreas Huber43c3e6c2011-01-05 12:17:08 -0800401void NuPlayer::pause() {
Lajos Molnar1d15ab52015-03-04 16:46:34 -0800402 (new AMessage(kWhatPause, this))->post();
Andreas Huber43c3e6c2011-01-05 12:17:08 -0800403}
404
Andreas Huber1aef2112011-01-04 14:01:29 -0800405void NuPlayer::resetAsync() {
Wei Jiac45a4b22016-04-15 15:30:23 -0700406 sp<Source> source;
407 {
408 Mutex::Autolock autoLock(mSourceLock);
409 source = mSource;
410 }
411
412 if (source != NULL) {
Chong Zhang48296b72014-09-14 14:28:45 -0700413 // During a reset, the data source might be unresponsive already, we need to
414 // disconnect explicitly so that reads exit promptly.
415 // We can't queue the disconnect request to the looper, as it might be
416 // queued behind a stuck read and never gets processed.
417 // Doing a disconnect outside the looper to allows the pending reads to exit
418 // (either successfully or with error).
Wei Jiac45a4b22016-04-15 15:30:23 -0700419 source->disconnect();
Chong Zhang48296b72014-09-14 14:28:45 -0700420 }
421
Lajos Molnar1d15ab52015-03-04 16:46:34 -0800422 (new AMessage(kWhatReset, this))->post();
Andreas Huber1aef2112011-01-04 14:01:29 -0800423}
424
Wei Jiac5de0912016-11-18 10:22:14 -0800425void NuPlayer::seekToAsync(int64_t seekTimeUs, MediaPlayerSeekMode mode, bool needNotify) {
Lajos Molnar1d15ab52015-03-04 16:46:34 -0800426 sp<AMessage> msg = new AMessage(kWhatSeek, this);
Andreas Huber43c3e6c2011-01-05 12:17:08 -0800427 msg->setInt64("seekTimeUs", seekTimeUs);
Wei Jiac5de0912016-11-18 10:22:14 -0800428 msg->setInt32("mode", mode);
Wei Jiae427abf2014-09-22 15:21:11 -0700429 msg->setInt32("needNotify", needNotify);
Andreas Huber43c3e6c2011-01-05 12:17:08 -0800430 msg->post();
431}
432
Andreas Huber53df1a42010-12-22 10:03:04 -0800433
Chong Zhang404fced2014-06-11 14:45:31 -0700434void NuPlayer::writeTrackInfo(
Chih-Hung Hsiehe964d4e2016-08-09 14:31:32 -0700435 Parcel* reply, const sp<AMessage>& format) const {
Marco Nelissenc367ca12015-09-15 09:51:59 -0700436 if (format == NULL) {
437 ALOGE("NULL format");
438 return;
439 }
Chong Zhang404fced2014-06-11 14:45:31 -0700440 int32_t trackType;
Marco Nelissen9436e482015-09-15 10:21:53 -0700441 if (!format->findInt32("type", &trackType)) {
Marco Nelissenc367ca12015-09-15 09:51:59 -0700442 ALOGE("no track type");
443 return;
444 }
Chong Zhang404fced2014-06-11 14:45:31 -0700445
Robert Shih755106e2015-04-30 14:36:45 -0700446 AString mime;
Robert Shih2e3a4252015-05-06 10:21:15 -0700447 if (!format->findString("mime", &mime)) {
448 // Java MediaPlayer only uses mimetype for subtitle and timedtext tracks.
449 // If we can't find the mimetype here it means that we wouldn't be needing
450 // the mimetype on the Java end. We still write a placeholder mime to keep the
451 // (de)serialization logic simple.
452 if (trackType == MEDIA_TRACK_TYPE_AUDIO) {
453 mime = "audio/";
454 } else if (trackType == MEDIA_TRACK_TYPE_VIDEO) {
455 mime = "video/";
456 } else {
Marco Nelissenc367ca12015-09-15 09:51:59 -0700457 ALOGE("unknown track type: %d", trackType);
458 return;
Robert Shih2e3a4252015-05-06 10:21:15 -0700459 }
460 }
Robert Shih755106e2015-04-30 14:36:45 -0700461
Chong Zhang404fced2014-06-11 14:45:31 -0700462 AString lang;
Marco Nelissen9436e482015-09-15 10:21:53 -0700463 if (!format->findString("language", &lang)) {
Marco Nelissenc367ca12015-09-15 09:51:59 -0700464 ALOGE("no language");
465 return;
466 }
Chong Zhang404fced2014-06-11 14:45:31 -0700467
468 reply->writeInt32(2); // write something non-zero
469 reply->writeInt32(trackType);
Robert Shih755106e2015-04-30 14:36:45 -0700470 reply->writeString16(String16(mime.c_str()));
Chong Zhang404fced2014-06-11 14:45:31 -0700471 reply->writeString16(String16(lang.c_str()));
472
473 if (trackType == MEDIA_TRACK_TYPE_SUBTITLE) {
Chong Zhang404fced2014-06-11 14:45:31 -0700474 int32_t isAuto, isDefault, isForced;
475 CHECK(format->findInt32("auto", &isAuto));
476 CHECK(format->findInt32("default", &isDefault));
477 CHECK(format->findInt32("forced", &isForced));
478
Chong Zhang404fced2014-06-11 14:45:31 -0700479 reply->writeInt32(isAuto);
480 reply->writeInt32(isDefault);
481 reply->writeInt32(isForced);
482 }
483}
484
Andreas Huberf9334412010-12-15 15:17:42 -0800485void NuPlayer::onMessageReceived(const sp<AMessage> &msg) {
486 switch (msg->what()) {
487 case kWhatSetDataSource:
488 {
Steve Block3856b092011-10-20 11:56:00 +0100489 ALOGV("kWhatSetDataSource");
Andreas Huberf9334412010-12-15 15:17:42 -0800490
491 CHECK(mSource == NULL);
492
Chong Zhang3de157d2014-08-05 20:54:44 -0700493 status_t err = OK;
Andreas Huber5bc087c2010-12-23 10:27:40 -0800494 sp<RefBase> obj;
495 CHECK(msg->findObject("source", &obj));
Chong Zhang3de157d2014-08-05 20:54:44 -0700496 if (obj != NULL) {
Wei Jiac45a4b22016-04-15 15:30:23 -0700497 Mutex::Autolock autoLock(mSourceLock);
Chong Zhang3de157d2014-08-05 20:54:44 -0700498 mSource = static_cast<Source *>(obj.get());
Chong Zhang3de157d2014-08-05 20:54:44 -0700499 } else {
500 err = UNKNOWN_ERROR;
501 }
Andreas Huber9575c962013-02-05 13:59:56 -0800502
503 CHECK(mDriver != NULL);
504 sp<NuPlayerDriver> driver = mDriver.promote();
505 if (driver != NULL) {
Chong Zhang3de157d2014-08-05 20:54:44 -0700506 driver->notifySetDataSourceCompleted(err);
Andreas Huber9575c962013-02-05 13:59:56 -0800507 }
508 break;
509 }
510
511 case kWhatPrepare:
512 {
513 mSource->prepareAsync();
Andreas Huberf9334412010-12-15 15:17:42 -0800514 break;
515 }
516
Chong Zhangdcb89b32013-08-06 09:44:47 -0700517 case kWhatGetTrackInfo:
518 {
Lajos Molnar3f274362015-03-05 14:35:41 -0800519 sp<AReplyToken> replyID;
Chong Zhangdcb89b32013-08-06 09:44:47 -0700520 CHECK(msg->senderAwaitsResponse(&replyID));
521
Chong Zhang404fced2014-06-11 14:45:31 -0700522 Parcel* reply;
523 CHECK(msg->findPointer("reply", (void**)&reply));
524
525 size_t inbandTracks = 0;
Chong Zhangdcb89b32013-08-06 09:44:47 -0700526 if (mSource != NULL) {
Chong Zhang404fced2014-06-11 14:45:31 -0700527 inbandTracks = mSource->getTrackCount();
528 }
529
Chong Zhanga7fa1d92014-06-11 14:49:23 -0700530 size_t ccTracks = 0;
531 if (mCCDecoder != NULL) {
532 ccTracks = mCCDecoder->getTrackCount();
533 }
534
Chong Zhang404fced2014-06-11 14:45:31 -0700535 // total track count
Chong Zhanga7fa1d92014-06-11 14:49:23 -0700536 reply->writeInt32(inbandTracks + ccTracks);
Chong Zhang404fced2014-06-11 14:45:31 -0700537
538 // write inband tracks
539 for (size_t i = 0; i < inbandTracks; ++i) {
540 writeTrackInfo(reply, mSource->getTrackInfo(i));
Chong Zhangdcb89b32013-08-06 09:44:47 -0700541 }
542
Chong Zhanga7fa1d92014-06-11 14:49:23 -0700543 // write CC track
544 for (size_t i = 0; i < ccTracks; ++i) {
545 writeTrackInfo(reply, mCCDecoder->getTrackInfo(i));
546 }
547
Chong Zhangdcb89b32013-08-06 09:44:47 -0700548 sp<AMessage> response = new AMessage;
Chong Zhangdcb89b32013-08-06 09:44:47 -0700549 response->postReply(replyID);
550 break;
551 }
552
Robert Shih7c4f0d72014-07-09 18:53:31 -0700553 case kWhatGetSelectedTrack:
554 {
555 status_t err = INVALID_OPERATION;
556 if (mSource != NULL) {
557 err = OK;
558
559 int32_t type32;
560 CHECK(msg->findInt32("type", (int32_t*)&type32));
561 media_track_type type = (media_track_type)type32;
562 ssize_t selectedTrack = mSource->getSelectedTrack(type);
563
564 Parcel* reply;
565 CHECK(msg->findPointer("reply", (void**)&reply));
566 reply->writeInt32(selectedTrack);
567 }
568
569 sp<AMessage> response = new AMessage;
570 response->setInt32("err", err);
571
Lajos Molnar3f274362015-03-05 14:35:41 -0800572 sp<AReplyToken> replyID;
Robert Shih7c4f0d72014-07-09 18:53:31 -0700573 CHECK(msg->senderAwaitsResponse(&replyID));
574 response->postReply(replyID);
575 break;
576 }
577
Chong Zhangdcb89b32013-08-06 09:44:47 -0700578 case kWhatSelectTrack:
579 {
Lajos Molnar3f274362015-03-05 14:35:41 -0800580 sp<AReplyToken> replyID;
Chong Zhangdcb89b32013-08-06 09:44:47 -0700581 CHECK(msg->senderAwaitsResponse(&replyID));
582
Chong Zhang404fced2014-06-11 14:45:31 -0700583 size_t trackIndex;
584 int32_t select;
Robert Shih6ffb1fd2014-10-29 16:24:32 -0700585 int64_t timeUs;
Chong Zhang404fced2014-06-11 14:45:31 -0700586 CHECK(msg->findSize("trackIndex", &trackIndex));
587 CHECK(msg->findInt32("select", &select));
Robert Shih6ffb1fd2014-10-29 16:24:32 -0700588 CHECK(msg->findInt64("timeUs", &timeUs));
Chong Zhang404fced2014-06-11 14:45:31 -0700589
Chong Zhangdcb89b32013-08-06 09:44:47 -0700590 status_t err = INVALID_OPERATION;
Chong Zhang404fced2014-06-11 14:45:31 -0700591
592 size_t inbandTracks = 0;
Chong Zhangdcb89b32013-08-06 09:44:47 -0700593 if (mSource != NULL) {
Chong Zhang404fced2014-06-11 14:45:31 -0700594 inbandTracks = mSource->getTrackCount();
595 }
Chong Zhanga7fa1d92014-06-11 14:49:23 -0700596 size_t ccTracks = 0;
597 if (mCCDecoder != NULL) {
598 ccTracks = mCCDecoder->getTrackCount();
599 }
Chong Zhang404fced2014-06-11 14:45:31 -0700600
601 if (trackIndex < inbandTracks) {
Robert Shih6ffb1fd2014-10-29 16:24:32 -0700602 err = mSource->selectTrack(trackIndex, select, timeUs);
Robert Shihd3b0bbb2014-07-23 15:00:25 -0700603
604 if (!select && err == OK) {
605 int32_t type;
606 sp<AMessage> info = mSource->getTrackInfo(trackIndex);
607 if (info != NULL
608 && info->findInt32("type", &type)
609 && type == MEDIA_TRACK_TYPE_TIMEDTEXT) {
610 ++mTimedTextGeneration;
611 }
612 }
Chong Zhanga7fa1d92014-06-11 14:49:23 -0700613 } else {
614 trackIndex -= inbandTracks;
615
616 if (trackIndex < ccTracks) {
617 err = mCCDecoder->selectTrack(trackIndex, select);
618 }
Chong Zhangdcb89b32013-08-06 09:44:47 -0700619 }
620
621 sp<AMessage> response = new AMessage;
622 response->setInt32("err", err);
623
624 response->postReply(replyID);
625 break;
626 }
627
Andreas Huberb7c8e912012-11-27 15:02:53 -0800628 case kWhatPollDuration:
629 {
630 int32_t generation;
631 CHECK(msg->findInt32("generation", &generation));
632
633 if (generation != mPollDurationGeneration) {
634 // stale
635 break;
636 }
637
638 int64_t durationUs;
639 if (mDriver != NULL && mSource->getDuration(&durationUs) == OK) {
640 sp<NuPlayerDriver> driver = mDriver.promote();
641 if (driver != NULL) {
642 driver->notifyDuration(durationUs);
643 }
644 }
645
646 msg->post(1000000ll); // poll again in a second.
647 break;
648 }
649
Lajos Molnar1de1e252015-04-30 18:18:34 -0700650 case kWhatSetVideoSurface:
Andreas Huberf9334412010-12-15 15:17:42 -0800651 {
Andreas Huberf9334412010-12-15 15:17:42 -0800652
653 sp<RefBase> obj;
Lajos Molnar1de1e252015-04-30 18:18:34 -0700654 CHECK(msg->findObject("surface", &obj));
655 sp<Surface> surface = static_cast<Surface *>(obj.get());
Lajos Molnara81c6222015-07-10 19:17:45 -0700656
657 ALOGD("onSetVideoSurface(%p, %s video decoder)",
658 surface.get(),
Wei Jia6b7d2ed2015-08-10 15:11:54 -0700659 (mSource != NULL && mStarted && mSource->getFormat(false /* audio */) != NULL
Lajos Molnara81c6222015-07-10 19:17:45 -0700660 && mVideoDecoder != NULL) ? "have" : "no");
661
Wei Jia6b7d2ed2015-08-10 15:11:54 -0700662 // Need to check mStarted before calling mSource->getFormat because NuPlayer might
663 // be in preparing state and it could take long time.
664 // When mStarted is true, mSource must have been set.
665 if (mSource == NULL || !mStarted || mSource->getFormat(false /* audio */) == NULL
Lajos Molnara81c6222015-07-10 19:17:45 -0700666 // NOTE: mVideoDecoder's mSurface is always non-null
667 || (mVideoDecoder != NULL && mVideoDecoder->setVideoSurface(surface) == OK)) {
Lajos Molnar1de1e252015-04-30 18:18:34 -0700668 performSetSurface(surface);
Wei Jiafef808d2014-10-31 17:57:05 -0700669 break;
670 }
671
672 mDeferredActions.push_back(
673 new FlushDecoderAction(FLUSH_CMD_FLUSH /* audio */,
674 FLUSH_CMD_SHUTDOWN /* video */));
675
Lajos Molnar1de1e252015-04-30 18:18:34 -0700676 mDeferredActions.push_back(new SetSurfaceAction(surface));
James Dong0d268a32012-08-31 12:18:27 -0700677
Wei Jiac6e58412015-07-10 18:04:55 -0700678 if (obj != NULL || mAudioDecoder != NULL) {
Wei Jiafef808d2014-10-31 17:57:05 -0700679 if (mStarted) {
Andy Hung73535852014-09-05 11:42:58 -0700680 // Issue a seek to refresh the video screen only if started otherwise
681 // the extractor may not yet be started and will assert.
682 // If the video decoder is not set (perhaps audio only in this case)
683 // do not perform a seek as it is not needed.
Ronghua Wua73d9e02014-10-08 15:13:29 -0700684 int64_t currentPositionUs = 0;
685 if (getCurrentPosition(&currentPositionUs) == OK) {
686 mDeferredActions.push_back(
Wei Jiac5de0912016-11-18 10:22:14 -0800687 new SeekAction(currentPositionUs,
688 MediaPlayerSeekMode::SEEK_PREVIOUS_SYNC /* mode */));
Ronghua Wua73d9e02014-10-08 15:13:29 -0700689 }
Andy Hung73535852014-09-05 11:42:58 -0700690 }
Wei Jiaac428aa2014-09-02 19:01:34 -0700691
Andreas Huber57a339c2012-12-03 11:18:00 -0800692 // If there is a new surface texture, instantiate decoders
693 // again if possible.
694 mDeferredActions.push_back(
695 new SimpleAction(&NuPlayer::performScanSources));
696 }
697
Chong Zhangf8d71772014-11-26 15:08:34 -0800698 // After a flush without shutdown, decoder is paused.
699 // Don't resume it until source seek is done, otherwise it could
Chong Zhang7137ec72014-11-12 16:41:05 -0800700 // start pulling stale data too soon.
701 mDeferredActions.push_back(
Chong Zhangf8d71772014-11-26 15:08:34 -0800702 new ResumeDecoderAction(false /* needNotify */));
Chong Zhang7137ec72014-11-12 16:41:05 -0800703
Andreas Huber57a339c2012-12-03 11:18:00 -0800704 processDeferredActions();
Andreas Huberf9334412010-12-15 15:17:42 -0800705 break;
706 }
707
708 case kWhatSetAudioSink:
709 {
Steve Block3856b092011-10-20 11:56:00 +0100710 ALOGV("kWhatSetAudioSink");
Andreas Huberf9334412010-12-15 15:17:42 -0800711
712 sp<RefBase> obj;
713 CHECK(msg->findObject("sink", &obj));
714
715 mAudioSink = static_cast<MediaPlayerBase::AudioSink *>(obj.get());
716 break;
717 }
718
719 case kWhatStart:
720 {
Steve Block3856b092011-10-20 11:56:00 +0100721 ALOGV("kWhatStart");
Wei Jia94211742014-10-28 17:09:06 -0700722 if (mStarted) {
Chong Zhang8a048332015-05-06 15:16:28 -0700723 // do not resume yet if the source is still buffering
724 if (!mPausedForBuffering) {
725 onResume();
726 }
Wei Jia94211742014-10-28 17:09:06 -0700727 } else {
728 onStart();
Lajos Molnar09524832014-07-17 14:29:51 -0700729 }
Chong Zhangefbb6192015-01-30 17:13:27 -0800730 mPausedByClient = false;
Andreas Huberf9334412010-12-15 15:17:42 -0800731 break;
732 }
733
Lajos Molnar3a474aa2015-04-24 17:10:07 -0700734 case kWhatConfigPlayback:
Wei Jia98160162015-02-04 17:01:11 -0800735 {
Lajos Molnar3a474aa2015-04-24 17:10:07 -0700736 sp<AReplyToken> replyID;
737 CHECK(msg->senderAwaitsResponse(&replyID));
738 AudioPlaybackRate rate /* sanitized */;
739 readFromAMessage(msg, &rate);
740 status_t err = OK;
Wei Jia98160162015-02-04 17:01:11 -0800741 if (mRenderer != NULL) {
Wei Jiabf70feb2016-02-19 15:47:16 -0800742 // AudioSink allows only 1.f and 0.f for offload mode.
743 // For other speed, switch to non-offload mode.
744 if (mOffloadAudio && ((rate.mSpeed != 0.f && rate.mSpeed != 1.f)
745 || rate.mPitch != 1.f)) {
746 int64_t currentPositionUs;
747 if (getCurrentPosition(&currentPositionUs) != OK) {
748 currentPositionUs = mPreviousSeekTimeUs;
749 }
750
751 // Set mPlaybackSettings so that the new audio decoder can
752 // be created correctly.
753 mPlaybackSettings = rate;
754 if (!mPaused) {
755 mRenderer->pause();
756 }
Wei Jia5031b2f2016-02-25 11:19:31 -0800757 restartAudio(
Wei Jiabf70feb2016-02-19 15:47:16 -0800758 currentPositionUs, true /* forceNonOffload */,
759 true /* needsToCreateAudioDecoder */);
760 if (!mPaused) {
761 mRenderer->resume();
762 }
763 }
764
Lajos Molnar3a474aa2015-04-24 17:10:07 -0700765 err = mRenderer->setPlaybackSettings(rate);
766 }
767 if (err == OK) {
768 if (rate.mSpeed == 0.f) {
769 onPause();
Wei Jia351ce872016-02-10 13:21:59 -0800770 mPausedByClient = true;
Lajos Molnar3a474aa2015-04-24 17:10:07 -0700771 // save all other settings (using non-paused speed)
772 // so we can restore them on start
773 AudioPlaybackRate newRate = rate;
774 newRate.mSpeed = mPlaybackSettings.mSpeed;
775 mPlaybackSettings = newRate;
776 } else { /* rate.mSpeed != 0.f */
Lajos Molnar3a474aa2015-04-24 17:10:07 -0700777 mPlaybackSettings = rate;
Wei Jia8a092d32016-06-03 14:57:24 -0700778 if (mStarted) {
779 // do not resume yet if the source is still buffering
780 if (!mPausedForBuffering) {
781 onResume();
782 }
783 } else if (mPrepared) {
784 onStart();
785 }
786
787 mPausedByClient = false;
Lajos Molnar3a474aa2015-04-24 17:10:07 -0700788 }
Wei Jia98160162015-02-04 17:01:11 -0800789 }
Ronghua Wu8db88132015-04-22 13:51:35 -0700790
791 if (mVideoDecoder != NULL) {
Praveen Chavanbbaa1442016-04-08 13:33:49 -0700792 sp<AMessage> params = new AMessage();
793 params->setFloat("playback-speed", mPlaybackSettings.mSpeed);
794 mVideoDecoder->setParameters(params);
Ronghua Wu8db88132015-04-22 13:51:35 -0700795 }
796
Lajos Molnar3a474aa2015-04-24 17:10:07 -0700797 sp<AMessage> response = new AMessage;
798 response->setInt32("err", err);
799 response->postReply(replyID);
800 break;
801 }
802
803 case kWhatGetPlaybackSettings:
804 {
805 sp<AReplyToken> replyID;
806 CHECK(msg->senderAwaitsResponse(&replyID));
807 AudioPlaybackRate rate = mPlaybackSettings;
808 status_t err = OK;
809 if (mRenderer != NULL) {
810 err = mRenderer->getPlaybackSettings(&rate);
811 }
812 if (err == OK) {
813 // get playback settings used by renderer, as it may be
814 // slightly off due to audiosink not taking small changes.
815 mPlaybackSettings = rate;
816 if (mPaused) {
817 rate.mSpeed = 0.f;
818 }
819 }
820 sp<AMessage> response = new AMessage;
821 if (err == OK) {
822 writeToAMessage(response, rate);
823 }
824 response->setInt32("err", err);
825 response->postReply(replyID);
826 break;
827 }
828
829 case kWhatConfigSync:
830 {
831 sp<AReplyToken> replyID;
832 CHECK(msg->senderAwaitsResponse(&replyID));
833
834 ALOGV("kWhatConfigSync");
835 AVSyncSettings sync;
836 float videoFpsHint;
837 readFromAMessage(msg, &sync, &videoFpsHint);
838 status_t err = OK;
839 if (mRenderer != NULL) {
840 err = mRenderer->setSyncSettings(sync, videoFpsHint);
841 }
842 if (err == OK) {
843 mSyncSettings = sync;
844 mVideoFpsHint = videoFpsHint;
845 }
846 sp<AMessage> response = new AMessage;
847 response->setInt32("err", err);
848 response->postReply(replyID);
849 break;
850 }
851
852 case kWhatGetSyncSettings:
853 {
854 sp<AReplyToken> replyID;
855 CHECK(msg->senderAwaitsResponse(&replyID));
856 AVSyncSettings sync = mSyncSettings;
857 float videoFps = mVideoFpsHint;
858 status_t err = OK;
859 if (mRenderer != NULL) {
860 err = mRenderer->getSyncSettings(&sync, &videoFps);
861 if (err == OK) {
862 mSyncSettings = sync;
863 mVideoFpsHint = videoFps;
864 }
865 }
866 sp<AMessage> response = new AMessage;
867 if (err == OK) {
868 writeToAMessage(response, sync, videoFps);
869 }
870 response->setInt32("err", err);
871 response->postReply(replyID);
Wei Jia98160162015-02-04 17:01:11 -0800872 break;
873 }
874
Andreas Huberf9334412010-12-15 15:17:42 -0800875 case kWhatScanSources:
876 {
Andreas Huber1aef2112011-01-04 14:01:29 -0800877 int32_t generation;
878 CHECK(msg->findInt32("generation", &generation));
879 if (generation != mScanSourcesGeneration) {
880 // Drop obsolete msg.
881 break;
882 }
883
Andreas Huber5bc087c2010-12-23 10:27:40 -0800884 mScanSourcesPending = false;
885
Steve Block3856b092011-10-20 11:56:00 +0100886 ALOGV("scanning sources haveAudio=%d, haveVideo=%d",
Andreas Huber43c3e6c2011-01-05 12:17:08 -0800887 mAudioDecoder != NULL, mVideoDecoder != NULL);
888
Andreas Huberb7c8e912012-11-27 15:02:53 -0800889 bool mHadAnySourcesBefore =
890 (mAudioDecoder != NULL) || (mVideoDecoder != NULL);
Robert Shih7350b052015-10-01 15:50:14 -0700891 bool rescan = false;
Andreas Huberb7c8e912012-11-27 15:02:53 -0800892
Andy Hung282a7e32014-08-14 15:56:34 -0700893 // initialize video before audio because successful initialization of
894 // video may change deep buffer mode of audio.
Lajos Molnar1de1e252015-04-30 18:18:34 -0700895 if (mSurface != NULL) {
Robert Shih7350b052015-10-01 15:50:14 -0700896 if (instantiateDecoder(false, &mVideoDecoder) == -EWOULDBLOCK) {
897 rescan = true;
898 }
Haynes Mathew George5d246ef2012-07-09 10:36:57 -0700899 }
Andreas Huberf9334412010-12-15 15:17:42 -0800900
Ronghua Wua10fd232014-11-06 16:15:20 -0800901 // Don't try to re-open audio sink if there's an existing decoder.
902 if (mAudioSink != NULL && mAudioDecoder == NULL) {
Robert Shih7350b052015-10-01 15:50:14 -0700903 if (instantiateDecoder(true, &mAudioDecoder) == -EWOULDBLOCK) {
904 rescan = true;
905 }
Andreas Huberf9334412010-12-15 15:17:42 -0800906 }
907
Andreas Huberb7c8e912012-11-27 15:02:53 -0800908 if (!mHadAnySourcesBefore
909 && (mAudioDecoder != NULL || mVideoDecoder != NULL)) {
910 // This is the first time we've found anything playable.
911
Andreas Huber9575c962013-02-05 13:59:56 -0800912 if (mSourceFlags & Source::FLAG_DYNAMIC_DURATION) {
Andreas Huberb7c8e912012-11-27 15:02:53 -0800913 schedulePollDuration();
914 }
915 }
916
Andreas Hubereac68ba2011-09-27 12:12:25 -0700917 status_t err;
918 if ((err = mSource->feedMoreTSData()) != OK) {
Andreas Huber1aef2112011-01-04 14:01:29 -0800919 if (mAudioDecoder == NULL && mVideoDecoder == NULL) {
920 // We're not currently decoding anything (no audio or
921 // video tracks found) and we just ran out of input data.
Andreas Hubereac68ba2011-09-27 12:12:25 -0700922
923 if (err == ERROR_END_OF_STREAM) {
924 notifyListener(MEDIA_PLAYBACK_COMPLETE, 0, 0);
925 } else {
926 notifyListener(MEDIA_ERROR, MEDIA_ERROR_UNKNOWN, err);
927 }
Andreas Huber1aef2112011-01-04 14:01:29 -0800928 }
Andreas Huberf9334412010-12-15 15:17:42 -0800929 break;
930 }
931
Robert Shih7350b052015-10-01 15:50:14 -0700932 if (rescan) {
Andreas Huberf9334412010-12-15 15:17:42 -0800933 msg->post(100000ll);
Andreas Huber5bc087c2010-12-23 10:27:40 -0800934 mScanSourcesPending = true;
Andreas Huberf9334412010-12-15 15:17:42 -0800935 }
936 break;
937 }
938
939 case kWhatVideoNotify:
940 case kWhatAudioNotify:
941 {
942 bool audio = msg->what() == kWhatAudioNotify;
943
Wei Jia88703c32014-08-06 11:24:07 -0700944 int32_t currentDecoderGeneration =
945 (audio? mAudioDecoderGeneration : mVideoDecoderGeneration);
946 int32_t requesterGeneration = currentDecoderGeneration - 1;
947 CHECK(msg->findInt32("generation", &requesterGeneration));
948
949 if (requesterGeneration != currentDecoderGeneration) {
950 ALOGV("got message from old %s decoder, generation(%d:%d)",
951 audio ? "audio" : "video", requesterGeneration,
952 currentDecoderGeneration);
953 sp<AMessage> reply;
954 if (!(msg->findMessage("reply", &reply))) {
955 return;
956 }
957
958 reply->setInt32("err", INFO_DISCONTINUITY);
959 reply->post();
960 return;
961 }
962
Andreas Huberf9334412010-12-15 15:17:42 -0800963 int32_t what;
Lajos Molnar1cd13982014-01-17 15:12:51 -0800964 CHECK(msg->findInt32("what", &what));
Andreas Huberf9334412010-12-15 15:17:42 -0800965
Chong Zhang7137ec72014-11-12 16:41:05 -0800966 if (what == DecoderBase::kWhatInputDiscontinuity) {
967 int32_t formatChange;
968 CHECK(msg->findInt32("formatChange", &formatChange));
Andreas Huberf9334412010-12-15 15:17:42 -0800969
Chong Zhang7137ec72014-11-12 16:41:05 -0800970 ALOGV("%s discontinuity: formatChange %d",
971 audio ? "audio" : "video", formatChange);
972
973 if (formatChange) {
974 mDeferredActions.push_back(
975 new FlushDecoderAction(
976 audio ? FLUSH_CMD_SHUTDOWN : FLUSH_CMD_NONE,
977 audio ? FLUSH_CMD_NONE : FLUSH_CMD_SHUTDOWN));
Andreas Huberf9334412010-12-15 15:17:42 -0800978 }
Chong Zhang7137ec72014-11-12 16:41:05 -0800979
980 mDeferredActions.push_back(
981 new SimpleAction(
982 &NuPlayer::performScanSources));
983
984 processDeferredActions();
985 } else if (what == DecoderBase::kWhatEOS) {
Andreas Huberdc9bacd2011-09-26 10:53:29 -0700986 int32_t err;
Lajos Molnar1cd13982014-01-17 15:12:51 -0800987 CHECK(msg->findInt32("err", &err));
Andreas Huberdc9bacd2011-09-26 10:53:29 -0700988
989 if (err == ERROR_END_OF_STREAM) {
Steve Block3856b092011-10-20 11:56:00 +0100990 ALOGV("got %s decoder EOS", audio ? "audio" : "video");
Andreas Huberdc9bacd2011-09-26 10:53:29 -0700991 } else {
Steve Block3856b092011-10-20 11:56:00 +0100992 ALOGV("got %s decoder EOS w/ error %d",
Andreas Huberdc9bacd2011-09-26 10:53:29 -0700993 audio ? "audio" : "video",
994 err);
995 }
996
997 mRenderer->queueEOS(audio, err);
Chong Zhang7137ec72014-11-12 16:41:05 -0800998 } else if (what == DecoderBase::kWhatFlushCompleted) {
Steve Block3856b092011-10-20 11:56:00 +0100999 ALOGV("decoder %s flush completed", audio ? "audio" : "video");
Andreas Huberf9334412010-12-15 15:17:42 -08001000
Andy Hung8d121d42014-10-03 09:53:53 -07001001 handleFlushComplete(audio, true /* isDecoder */);
Andreas Huber3831a062010-12-21 10:22:33 -08001002 finishFlushIfPossible();
Chong Zhang7137ec72014-11-12 16:41:05 -08001003 } else if (what == DecoderBase::kWhatVideoSizeChanged) {
Lajos Molnar1cd13982014-01-17 15:12:51 -08001004 sp<AMessage> format;
1005 CHECK(msg->findMessage("format", &format));
1006
Wei Jiac6cfd702014-11-11 16:33:20 -08001007 sp<AMessage> inputFormat =
1008 mSource->getFormat(false /* audio */);
Andreas Huber3831a062010-12-21 10:22:33 -08001009
Bartosz Dolewski26936f72014-12-11 12:47:08 +01001010 setVideoScalingMode(mVideoScalingMode);
Wei Jiac6cfd702014-11-11 16:33:20 -08001011 updateVideoSize(inputFormat, format);
Chong Zhang7137ec72014-11-12 16:41:05 -08001012 } else if (what == DecoderBase::kWhatShutdownCompleted) {
Steve Block3856b092011-10-20 11:56:00 +01001013 ALOGV("%s shutdown completed", audio ? "audio" : "video");
Andreas Huber3831a062010-12-21 10:22:33 -08001014 if (audio) {
1015 mAudioDecoder.clear();
Wei Jia57568df2014-09-22 10:16:29 -07001016 ++mAudioDecoderGeneration;
Andreas Huber3831a062010-12-21 10:22:33 -08001017
1018 CHECK_EQ((int)mFlushingAudio, (int)SHUTTING_DOWN_DECODER);
1019 mFlushingAudio = SHUT_DOWN;
1020 } else {
1021 mVideoDecoder.clear();
Wei Jia57568df2014-09-22 10:16:29 -07001022 ++mVideoDecoderGeneration;
Andreas Huber3831a062010-12-21 10:22:33 -08001023
1024 CHECK_EQ((int)mFlushingVideo, (int)SHUTTING_DOWN_DECODER);
1025 mFlushingVideo = SHUT_DOWN;
1026 }
1027
1028 finishFlushIfPossible();
Chong Zhangf8d71772014-11-26 15:08:34 -08001029 } else if (what == DecoderBase::kWhatResumeCompleted) {
1030 finishResume();
Chong Zhang7137ec72014-11-12 16:41:05 -08001031 } else if (what == DecoderBase::kWhatError) {
Chong Zhangf4c0a942014-08-11 15:14:10 -07001032 status_t err;
Andy Hung2abde2c2014-09-30 14:40:32 -07001033 if (!msg->findInt32("err", &err) || err == OK) {
Chong Zhangf4c0a942014-08-11 15:14:10 -07001034 err = UNKNOWN_ERROR;
1035 }
Andy Hungcf31f1e2014-09-23 14:59:01 -07001036
Andy Hung2abde2c2014-09-30 14:40:32 -07001037 // Decoder errors can be due to Source (e.g. from streaming),
1038 // or from decoding corrupted bitstreams, or from other decoder
1039 // MediaCodec operations (e.g. from an ongoing reset or seek).
Andy Hung202bce12014-12-03 11:47:36 -08001040 // They may also be due to openAudioSink failure at
1041 // decoder start or after a format change.
Andy Hung2abde2c2014-09-30 14:40:32 -07001042 //
1043 // We try to gracefully shut down the affected decoder if possible,
1044 // rather than trying to force the shutdown with something
1045 // similar to performReset(). This method can lead to a hang
1046 // if MediaCodec functions block after an error, but they should
1047 // typically return INVALID_OPERATION instead of blocking.
1048
1049 FlushStatus *flushing = audio ? &mFlushingAudio : &mFlushingVideo;
1050 ALOGE("received error(%#x) from %s decoder, flushing(%d), now shutting down",
1051 err, audio ? "audio" : "video", *flushing);
1052
1053 switch (*flushing) {
1054 case NONE:
1055 mDeferredActions.push_back(
Wei Jiafef808d2014-10-31 17:57:05 -07001056 new FlushDecoderAction(
1057 audio ? FLUSH_CMD_SHUTDOWN : FLUSH_CMD_NONE,
1058 audio ? FLUSH_CMD_NONE : FLUSH_CMD_SHUTDOWN));
Andy Hung2abde2c2014-09-30 14:40:32 -07001059 processDeferredActions();
1060 break;
1061 case FLUSHING_DECODER:
1062 *flushing = FLUSHING_DECODER_SHUTDOWN; // initiate shutdown after flush.
1063 break; // Wait for flush to complete.
1064 case FLUSHING_DECODER_SHUTDOWN:
1065 break; // Wait for flush to complete.
1066 case SHUTTING_DOWN_DECODER:
1067 break; // Wait for shutdown to complete.
1068 case FLUSHED:
1069 // Widevine source reads must stop before releasing the video decoder.
1070 if (!audio && mSource != NULL && mSourceFlags & Source::FLAG_SECURE) {
1071 mSource->stop();
Robert Shih0c61a0d2015-07-06 15:09:10 -07001072 mSourceStarted = false;
Andy Hung2abde2c2014-09-30 14:40:32 -07001073 }
1074 getDecoder(audio)->initiateShutdown(); // In the middle of a seek.
1075 *flushing = SHUTTING_DOWN_DECODER; // Shut down.
1076 break;
1077 case SHUT_DOWN:
1078 finishFlushIfPossible(); // Should not occur.
1079 break; // Finish anyways.
Marco Nelissen9e2b7912014-08-18 16:13:03 -07001080 }
Andy Hung2abde2c2014-09-30 14:40:32 -07001081 notifyListener(MEDIA_ERROR, MEDIA_ERROR_UNKNOWN, err);
Lajos Molnar1cd13982014-01-17 15:12:51 -08001082 } else {
1083 ALOGV("Unhandled decoder notification %d '%c%c%c%c'.",
Andreas Hubera1f8ab02012-11-30 10:53:22 -08001084 what,
1085 what >> 24,
1086 (what >> 16) & 0xff,
1087 (what >> 8) & 0xff,
1088 what & 0xff);
Andreas Huberf9334412010-12-15 15:17:42 -08001089 }
1090
1091 break;
1092 }
1093
1094 case kWhatRendererNotify:
1095 {
Wei Jia57568df2014-09-22 10:16:29 -07001096 int32_t requesterGeneration = mRendererGeneration - 1;
1097 CHECK(msg->findInt32("generation", &requesterGeneration));
1098 if (requesterGeneration != mRendererGeneration) {
1099 ALOGV("got message from old renderer, generation(%d:%d)",
1100 requesterGeneration, mRendererGeneration);
1101 return;
1102 }
1103
Andreas Huberf9334412010-12-15 15:17:42 -08001104 int32_t what;
1105 CHECK(msg->findInt32("what", &what));
1106
1107 if (what == Renderer::kWhatEOS) {
1108 int32_t audio;
1109 CHECK(msg->findInt32("audio", &audio));
1110
Andreas Huberc92fd242011-08-16 13:48:44 -07001111 int32_t finalResult;
1112 CHECK(msg->findInt32("finalResult", &finalResult));
1113
Andreas Huberf9334412010-12-15 15:17:42 -08001114 if (audio) {
1115 mAudioEOS = true;
1116 } else {
1117 mVideoEOS = true;
1118 }
1119
Andreas Huberc92fd242011-08-16 13:48:44 -07001120 if (finalResult == ERROR_END_OF_STREAM) {
Steve Block3856b092011-10-20 11:56:00 +01001121 ALOGV("reached %s EOS", audio ? "audio" : "video");
Andreas Huberc92fd242011-08-16 13:48:44 -07001122 } else {
Steve Block29357bc2012-01-06 19:20:56 +00001123 ALOGE("%s track encountered an error (%d)",
Andreas Huberc92fd242011-08-16 13:48:44 -07001124 audio ? "audio" : "video", finalResult);
1125
1126 notifyListener(
1127 MEDIA_ERROR, MEDIA_ERROR_UNKNOWN, finalResult);
1128 }
Andreas Huberf9334412010-12-15 15:17:42 -08001129
1130 if ((mAudioEOS || mAudioDecoder == NULL)
1131 && (mVideoEOS || mVideoDecoder == NULL)) {
1132 notifyListener(MEDIA_PLAYBACK_COMPLETE, 0, 0);
1133 }
Andreas Huber3fe62152011-09-16 15:09:22 -07001134 } else if (what == Renderer::kWhatFlushComplete) {
Andreas Huberf9334412010-12-15 15:17:42 -08001135 int32_t audio;
1136 CHECK(msg->findInt32("audio", &audio));
1137
Wei Jia3261f0d2015-10-08 13:58:33 -07001138 if (audio) {
1139 mAudioEOS = false;
1140 } else {
1141 mVideoEOS = false;
1142 }
1143
Steve Block3856b092011-10-20 11:56:00 +01001144 ALOGV("renderer %s flush completed.", audio ? "audio" : "video");
Wei Jiada4252f2015-07-14 18:15:28 -07001145 if (audio && (mFlushingAudio == NONE || mFlushingAudio == FLUSHED
1146 || mFlushingAudio == SHUT_DOWN)) {
1147 // Flush has been handled by tear down.
1148 break;
1149 }
Andy Hung8d121d42014-10-03 09:53:53 -07001150 handleFlushComplete(audio, false /* isDecoder */);
1151 finishFlushIfPossible();
James Dongf57b4ea2012-07-20 13:38:36 -07001152 } else if (what == Renderer::kWhatVideoRenderingStart) {
1153 notifyListener(MEDIA_INFO, MEDIA_INFO_RENDERING_START, 0);
Lajos Molnarcbaffcf2013-08-14 18:30:38 -07001154 } else if (what == Renderer::kWhatMediaRenderingStart) {
1155 ALOGV("media rendering started");
1156 notifyListener(MEDIA_STARTED, 0, 0);
Ronghua Wufaeb0f22015-05-21 12:20:21 -07001157 } else if (what == Renderer::kWhatAudioTearDown) {
Ronghua Wu08529172014-10-02 16:55:52 -07001158 int32_t reason;
1159 CHECK(msg->findInt32("reason", &reason));
Ronghua Wufaeb0f22015-05-21 12:20:21 -07001160 ALOGV("Tear down audio with reason %d.", reason);
Wei Jia8456ddd2016-04-22 14:46:43 -07001161 if (reason == Renderer::kDueToTimeout && !(mPaused && mOffloadAudio)) {
1162 // TimeoutWhenPaused is only for offload mode.
1163 ALOGW("Receive a stale message for teardown.");
1164 break;
1165 }
Wei Jiada4252f2015-07-14 18:15:28 -07001166 int64_t positionUs;
Robert Shih1a5c8592015-08-04 18:07:44 -07001167 if (!msg->findInt64("positionUs", &positionUs)) {
1168 positionUs = mPreviousSeekTimeUs;
1169 }
Ronghua Wufaeb0f22015-05-21 12:20:21 -07001170
Wei Jia5031b2f2016-02-25 11:19:31 -08001171 restartAudio(
Wei Jiaa05f1e32016-03-25 16:31:22 -07001172 positionUs, reason == Renderer::kForceNonOffload /* forceNonOffload */,
1173 reason != Renderer::kDueToTimeout /* needsToCreateAudioDecoder */);
Andreas Huberf9334412010-12-15 15:17:42 -08001174 }
1175 break;
1176 }
1177
1178 case kWhatMoreDataQueued:
1179 {
1180 break;
1181 }
1182
Andreas Huber1aef2112011-01-04 14:01:29 -08001183 case kWhatReset:
1184 {
Steve Block3856b092011-10-20 11:56:00 +01001185 ALOGV("kWhatReset");
Andreas Huber1aef2112011-01-04 14:01:29 -08001186
Ronghua Wu64c2d172015-10-07 16:52:19 -07001187 mResetting = true;
1188
Andreas Hubera1f8ab02012-11-30 10:53:22 -08001189 mDeferredActions.push_back(
Wei Jiafef808d2014-10-31 17:57:05 -07001190 new FlushDecoderAction(
1191 FLUSH_CMD_SHUTDOWN /* audio */,
1192 FLUSH_CMD_SHUTDOWN /* video */));
Andreas Huberb7c8e912012-11-27 15:02:53 -08001193
Andreas Hubera1f8ab02012-11-30 10:53:22 -08001194 mDeferredActions.push_back(
1195 new SimpleAction(&NuPlayer::performReset));
Andreas Huberb58ce9f2011-11-28 16:27:35 -08001196
Andreas Hubera1f8ab02012-11-30 10:53:22 -08001197 processDeferredActions();
Andreas Huber1aef2112011-01-04 14:01:29 -08001198 break;
1199 }
1200
Andreas Huber43c3e6c2011-01-05 12:17:08 -08001201 case kWhatSeek:
1202 {
1203 int64_t seekTimeUs;
Wei Jiac5de0912016-11-18 10:22:14 -08001204 int32_t mode;
Wei Jiae427abf2014-09-22 15:21:11 -07001205 int32_t needNotify;
Andreas Huber43c3e6c2011-01-05 12:17:08 -08001206 CHECK(msg->findInt64("seekTimeUs", &seekTimeUs));
Wei Jiac5de0912016-11-18 10:22:14 -08001207 CHECK(msg->findInt32("mode", &mode));
Wei Jiae427abf2014-09-22 15:21:11 -07001208 CHECK(msg->findInt32("needNotify", &needNotify));
Andreas Huber43c3e6c2011-01-05 12:17:08 -08001209
Wei Jiac5de0912016-11-18 10:22:14 -08001210 ALOGV("kWhatSeek seekTimeUs=%lld us, mode=%d, needNotify=%d",
1211 (long long)seekTimeUs, mode, needNotify);
Andreas Huber43c3e6c2011-01-05 12:17:08 -08001212
Wei Jia1061c9c2015-05-19 16:02:17 -07001213 if (!mStarted) {
1214 // Seek before the player is started. In order to preview video,
1215 // need to start the player and pause it. This branch is called
1216 // only once if needed. After the player is started, any seek
1217 // operation will go through normal path.
Robert Shih0c61a0d2015-07-06 15:09:10 -07001218 // Audio-only cases are handled separately.
Wei Jiac5de0912016-11-18 10:22:14 -08001219 onStart(seekTimeUs, (MediaPlayerSeekMode)mode);
Robert Shih0c61a0d2015-07-06 15:09:10 -07001220 if (mStarted) {
1221 onPause();
1222 mPausedByClient = true;
1223 }
Wei Jia1061c9c2015-05-19 16:02:17 -07001224 if (needNotify) {
1225 notifyDriverSeekComplete();
1226 }
1227 break;
1228 }
1229
Andreas Hubera1f8ab02012-11-30 10:53:22 -08001230 mDeferredActions.push_back(
Wei Jiafef808d2014-10-31 17:57:05 -07001231 new FlushDecoderAction(FLUSH_CMD_FLUSH /* audio */,
1232 FLUSH_CMD_FLUSH /* video */));
Andreas Huber43c3e6c2011-01-05 12:17:08 -08001233
Wei Jiae427abf2014-09-22 15:21:11 -07001234 mDeferredActions.push_back(
Wei Jiac5de0912016-11-18 10:22:14 -08001235 new SeekAction(seekTimeUs, (MediaPlayerSeekMode)mode));
Andreas Huber43c3e6c2011-01-05 12:17:08 -08001236
Chong Zhangf8d71772014-11-26 15:08:34 -08001237 // After a flush without shutdown, decoder is paused.
1238 // Don't resume it until source seek is done, otherwise it could
Chong Zhang7137ec72014-11-12 16:41:05 -08001239 // start pulling stale data too soon.
1240 mDeferredActions.push_back(
Chong Zhangf8d71772014-11-26 15:08:34 -08001241 new ResumeDecoderAction(needNotify));
Chong Zhang7137ec72014-11-12 16:41:05 -08001242
Andreas Hubera1f8ab02012-11-30 10:53:22 -08001243 processDeferredActions();
Andreas Huber43c3e6c2011-01-05 12:17:08 -08001244 break;
1245 }
1246
Andreas Huberb4082222011-01-20 15:23:04 -08001247 case kWhatPause:
1248 {
Chong Zhangefbb6192015-01-30 17:13:27 -08001249 onPause();
1250 mPausedByClient = true;
Andreas Huberb4082222011-01-20 15:23:04 -08001251 break;
1252 }
1253
Andreas Huberb5f25f02013-02-05 10:14:26 -08001254 case kWhatSourceNotify:
1255 {
Andreas Huber9575c962013-02-05 13:59:56 -08001256 onSourceNotify(msg);
Andreas Huberb5f25f02013-02-05 10:14:26 -08001257 break;
1258 }
1259
Chong Zhanga7fa1d92014-06-11 14:49:23 -07001260 case kWhatClosedCaptionNotify:
1261 {
1262 onClosedCaptionNotify(msg);
1263 break;
1264 }
1265
Andreas Huberf9334412010-12-15 15:17:42 -08001266 default:
1267 TRESPASS();
1268 break;
1269 }
1270}
1271
Wei Jia94211742014-10-28 17:09:06 -07001272void NuPlayer::onResume() {
Ronghua Wu64c2d172015-10-07 16:52:19 -07001273 if (!mPaused || mResetting) {
1274 ALOGD_IF(mResetting, "resetting, onResume discarded");
Chong Zhangefbb6192015-01-30 17:13:27 -08001275 return;
1276 }
1277 mPaused = false;
Wei Jia94211742014-10-28 17:09:06 -07001278 if (mSource != NULL) {
1279 mSource->resume();
1280 } else {
1281 ALOGW("resume called when source is gone or not set");
1282 }
1283 // |mAudioDecoder| may have been released due to the pause timeout, so re-create it if
1284 // needed.
1285 if (audioDecoderStillNeeded() && mAudioDecoder == NULL) {
1286 instantiateDecoder(true /* audio */, &mAudioDecoder);
1287 }
1288 if (mRenderer != NULL) {
1289 mRenderer->resume();
1290 } else {
1291 ALOGW("resume called when renderer is gone or not set");
1292 }
Ray Essickd4d00612017-01-03 09:36:27 -08001293
1294 mLastStartedPlayingTimeNs = systemTime();
Wei Jia94211742014-10-28 17:09:06 -07001295}
1296
Lajos Molnarfcd3e942015-03-31 10:06:48 -07001297status_t NuPlayer::onInstantiateSecureDecoders() {
1298 status_t err;
1299 if (!(mSourceFlags & Source::FLAG_SECURE)) {
1300 return BAD_TYPE;
1301 }
1302
1303 if (mRenderer != NULL) {
1304 ALOGE("renderer should not be set when instantiating secure decoders");
1305 return UNKNOWN_ERROR;
1306 }
1307
1308 // TRICKY: We rely on mRenderer being null, so that decoder does not start requesting
1309 // data on instantiation.
Lajos Molnar1de1e252015-04-30 18:18:34 -07001310 if (mSurface != NULL) {
Lajos Molnarfcd3e942015-03-31 10:06:48 -07001311 err = instantiateDecoder(false, &mVideoDecoder);
1312 if (err != OK) {
1313 return err;
1314 }
1315 }
1316
1317 if (mAudioSink != NULL) {
1318 err = instantiateDecoder(true, &mAudioDecoder);
1319 if (err != OK) {
1320 return err;
1321 }
1322 }
1323 return OK;
1324}
1325
Wei Jiac5de0912016-11-18 10:22:14 -08001326void NuPlayer::onStart(int64_t startPositionUs, MediaPlayerSeekMode mode) {
Robert Shih0c61a0d2015-07-06 15:09:10 -07001327 if (!mSourceStarted) {
1328 mSourceStarted = true;
1329 mSource->start();
1330 }
1331 if (startPositionUs > 0) {
Wei Jiac5de0912016-11-18 10:22:14 -08001332 performSeek(startPositionUs, mode);
Robert Shih0c61a0d2015-07-06 15:09:10 -07001333 if (mSource->getFormat(false /* audio */) == NULL) {
1334 return;
1335 }
1336 }
1337
Wei Jia94211742014-10-28 17:09:06 -07001338 mOffloadAudio = false;
1339 mAudioEOS = false;
1340 mVideoEOS = false;
Wei Jia94211742014-10-28 17:09:06 -07001341 mStarted = true;
Wei Jiafe8fe7d2016-06-08 10:29:25 -07001342 mPaused = false;
Wei Jia94211742014-10-28 17:09:06 -07001343
Wei Jia94211742014-10-28 17:09:06 -07001344 uint32_t flags = 0;
1345
1346 if (mSource->isRealTime()) {
1347 flags |= Renderer::FLAG_REAL_TIME;
1348 }
1349
1350 sp<MetaData> audioMeta = mSource->getFormatMeta(true /* audio */);
Wei Jia1de83d52016-10-10 10:15:03 -07001351 sp<MetaData> videoMeta = mSource->getFormatMeta(false /* audio */);
1352 if (audioMeta == NULL && videoMeta == NULL) {
1353 ALOGE("no metadata for either audio or video source");
1354 mSource->stop();
1355 mSourceStarted = false;
1356 notifyListener(MEDIA_ERROR, MEDIA_ERROR_UNKNOWN, ERROR_MALFORMED);
1357 return;
1358 }
Andy Hung12a84132015-10-20 16:09:21 -07001359 ALOGV_IF(audioMeta == NULL, "no metadata for audio source"); // video only stream
Wei Jia1de83d52016-10-10 10:15:03 -07001360
Wei Jia94211742014-10-28 17:09:06 -07001361 audio_stream_type_t streamType = AUDIO_STREAM_MUSIC;
1362 if (mAudioSink != NULL) {
1363 streamType = mAudioSink->getAudioStreamType();
1364 }
1365
1366 sp<AMessage> videoFormat = mSource->getFormat(false /* audio */);
1367
1368 mOffloadAudio =
Wei Jiabf70feb2016-02-19 15:47:16 -08001369 canOffloadStream(audioMeta, (videoFormat != NULL), mSource->isStreaming(), streamType)
1370 && (mPlaybackSettings.mSpeed == 1.f && mPlaybackSettings.mPitch == 1.f);
Wei Jia94211742014-10-28 17:09:06 -07001371 if (mOffloadAudio) {
1372 flags |= Renderer::FLAG_OFFLOAD_AUDIO;
1373 }
1374
Lajos Molnar1d15ab52015-03-04 16:46:34 -08001375 sp<AMessage> notify = new AMessage(kWhatRendererNotify, this);
Wei Jia94211742014-10-28 17:09:06 -07001376 ++mRendererGeneration;
1377 notify->setInt32("generation", mRendererGeneration);
1378 mRenderer = new Renderer(mAudioSink, notify, flags);
Wei Jia94211742014-10-28 17:09:06 -07001379 mRendererLooper = new ALooper;
1380 mRendererLooper->setName("NuPlayerRenderer");
1381 mRendererLooper->start(false, false, ANDROID_PRIORITY_AUDIO);
1382 mRendererLooper->registerHandler(mRenderer);
Lajos Molnar3a474aa2015-04-24 17:10:07 -07001383
1384 status_t err = mRenderer->setPlaybackSettings(mPlaybackSettings);
1385 if (err != OK) {
1386 mSource->stop();
Robert Shih0c61a0d2015-07-06 15:09:10 -07001387 mSourceStarted = false;
Lajos Molnar3a474aa2015-04-24 17:10:07 -07001388 notifyListener(MEDIA_ERROR, MEDIA_ERROR_UNKNOWN, err);
1389 return;
Wei Jiac8206ff2015-03-04 13:59:37 -08001390 }
Wei Jia94211742014-10-28 17:09:06 -07001391
Ronghua Wuc8a70d32015-04-29 16:26:34 -07001392 float rate = getFrameRate();
1393 if (rate > 0) {
Wei Jia94211742014-10-28 17:09:06 -07001394 mRenderer->setVideoFrameRate(rate);
1395 }
1396
Wei Jiac6cfd702014-11-11 16:33:20 -08001397 if (mVideoDecoder != NULL) {
1398 mVideoDecoder->setRenderer(mRenderer);
1399 }
1400 if (mAudioDecoder != NULL) {
1401 mAudioDecoder->setRenderer(mRenderer);
1402 }
1403
Ray Essickd4d00612017-01-03 09:36:27 -08001404 mLastStartedPlayingTimeNs = systemTime();
1405
Wei Jia94211742014-10-28 17:09:06 -07001406 postScanSources();
1407}
1408
Chong Zhangefbb6192015-01-30 17:13:27 -08001409void NuPlayer::onPause() {
1410 if (mPaused) {
1411 return;
1412 }
1413 mPaused = true;
1414 if (mSource != NULL) {
1415 mSource->pause();
1416 } else {
1417 ALOGW("pause called when source is gone or not set");
1418 }
1419 if (mRenderer != NULL) {
1420 mRenderer->pause();
1421 } else {
1422 ALOGW("pause called when renderer is gone or not set");
1423 }
Ray Essickd4d00612017-01-03 09:36:27 -08001424
1425 sp<NuPlayerDriver> driver = mDriver.promote();
1426 if (driver != NULL) {
1427 int64_t now = systemTime();
1428 int64_t played = now - mLastStartedPlayingTimeNs;
1429 ALOGD("played from %" PRId64 " to %" PRId64 " = %" PRId64 ,
1430 mLastStartedPlayingTimeNs, now, played);
1431
1432 driver->notifyMorePlayingTimeUs((played+500)/1000);
1433 }
Chong Zhangefbb6192015-01-30 17:13:27 -08001434}
1435
Ronghua Wud7988b12014-10-03 15:19:10 -07001436bool NuPlayer::audioDecoderStillNeeded() {
1437 // Audio decoder is no longer needed if it's in shut/shutting down status.
1438 return ((mFlushingAudio != SHUT_DOWN) && (mFlushingAudio != SHUTTING_DOWN_DECODER));
1439}
1440
Andy Hung8d121d42014-10-03 09:53:53 -07001441void NuPlayer::handleFlushComplete(bool audio, bool isDecoder) {
1442 // We wait for both the decoder flush and the renderer flush to complete
1443 // before entering either the FLUSHED or the SHUTTING_DOWN_DECODER state.
1444
1445 mFlushComplete[audio][isDecoder] = true;
1446 if (!mFlushComplete[audio][!isDecoder]) {
1447 return;
1448 }
1449
1450 FlushStatus *state = audio ? &mFlushingAudio : &mFlushingVideo;
1451 switch (*state) {
1452 case FLUSHING_DECODER:
1453 {
1454 *state = FLUSHED;
Andy Hung8d121d42014-10-03 09:53:53 -07001455 break;
1456 }
1457
1458 case FLUSHING_DECODER_SHUTDOWN:
1459 {
1460 *state = SHUTTING_DOWN_DECODER;
1461
1462 ALOGV("initiating %s decoder shutdown", audio ? "audio" : "video");
1463 if (!audio) {
Andy Hung8d121d42014-10-03 09:53:53 -07001464 // Widevine source reads must stop before releasing the video decoder.
1465 if (mSource != NULL && mSourceFlags & Source::FLAG_SECURE) {
1466 mSource->stop();
Robert Shih0c61a0d2015-07-06 15:09:10 -07001467 mSourceStarted = false;
Andy Hung8d121d42014-10-03 09:53:53 -07001468 }
1469 }
1470 getDecoder(audio)->initiateShutdown();
1471 break;
1472 }
1473
1474 default:
1475 // decoder flush completes only occur in a flushing state.
1476 LOG_ALWAYS_FATAL_IF(isDecoder, "decoder flush in invalid state %d", *state);
1477 break;
1478 }
1479}
1480
Andreas Huber3831a062010-12-21 10:22:33 -08001481void NuPlayer::finishFlushIfPossible() {
Wei Jia53904f32014-07-29 10:22:53 -07001482 if (mFlushingAudio != NONE && mFlushingAudio != FLUSHED
1483 && mFlushingAudio != SHUT_DOWN) {
Andreas Huber3831a062010-12-21 10:22:33 -08001484 return;
1485 }
1486
Wei Jia53904f32014-07-29 10:22:53 -07001487 if (mFlushingVideo != NONE && mFlushingVideo != FLUSHED
1488 && mFlushingVideo != SHUT_DOWN) {
Andreas Huber3831a062010-12-21 10:22:33 -08001489 return;
1490 }
1491
Steve Block3856b092011-10-20 11:56:00 +01001492 ALOGV("both audio and video are flushed now.");
Andreas Huber3831a062010-12-21 10:22:33 -08001493
Andreas Huber3831a062010-12-21 10:22:33 -08001494 mFlushingAudio = NONE;
1495 mFlushingVideo = NONE;
Andreas Huber3831a062010-12-21 10:22:33 -08001496
Andy Hung8d121d42014-10-03 09:53:53 -07001497 clearFlushComplete();
1498
Andreas Hubera1f8ab02012-11-30 10:53:22 -08001499 processDeferredActions();
Andreas Huber1aef2112011-01-04 14:01:29 -08001500}
1501
1502void NuPlayer::postScanSources() {
1503 if (mScanSourcesPending) {
1504 return;
1505 }
1506
Lajos Molnar1d15ab52015-03-04 16:46:34 -08001507 sp<AMessage> msg = new AMessage(kWhatScanSources, this);
Andreas Huber1aef2112011-01-04 14:01:29 -08001508 msg->setInt32("generation", mScanSourcesGeneration);
1509 msg->post();
1510
1511 mScanSourcesPending = true;
1512}
1513
Wei Jia41cd4632016-05-13 10:53:30 -07001514void NuPlayer::tryOpenAudioSinkForOffload(
1515 const sp<AMessage> &format, const sp<MetaData> &audioMeta, bool hasVideo) {
Andy Hung202bce12014-12-03 11:47:36 -08001516 // Note: This is called early in NuPlayer to determine whether offloading
1517 // is possible; otherwise the decoders call the renderer openAudioSink directly.
Andy Hung282a7e32014-08-14 15:56:34 -07001518
Andy Hung202bce12014-12-03 11:47:36 -08001519 status_t err = mRenderer->openAudioSink(
1520 format, true /* offloadOnly */, hasVideo, AUDIO_OUTPUT_FLAG_NONE, &mOffloadAudio);
1521 if (err != OK) {
1522 // Any failure we turn off mOffloadAudio.
1523 mOffloadAudio = false;
1524 } else if (mOffloadAudio) {
Chong Zhang3b9eb1f2014-10-15 17:05:08 -07001525 sendMetaDataToHal(mAudioSink, audioMeta);
Andy Hung282a7e32014-08-14 15:56:34 -07001526 }
1527}
1528
1529void NuPlayer::closeAudioSink() {
Chong Zhang3b9eb1f2014-10-15 17:05:08 -07001530 mRenderer->closeAudioSink();
Andy Hung282a7e32014-08-14 15:56:34 -07001531}
1532
Wei Jia5031b2f2016-02-25 11:19:31 -08001533void NuPlayer::restartAudio(
Wei Jiabf70feb2016-02-19 15:47:16 -08001534 int64_t currentPositionUs, bool forceNonOffload, bool needsToCreateAudioDecoder) {
Wei Jiaa05f1e32016-03-25 16:31:22 -07001535 if (mAudioDecoder != NULL) {
1536 mAudioDecoder->pause();
1537 mAudioDecoder.clear();
1538 ++mAudioDecoderGeneration;
1539 }
Wei Jiabf70feb2016-02-19 15:47:16 -08001540 if (mFlushingAudio == FLUSHING_DECODER) {
1541 mFlushComplete[1 /* audio */][1 /* isDecoder */] = true;
1542 mFlushingAudio = FLUSHED;
1543 finishFlushIfPossible();
1544 } else if (mFlushingAudio == FLUSHING_DECODER_SHUTDOWN
1545 || mFlushingAudio == SHUTTING_DOWN_DECODER) {
1546 mFlushComplete[1 /* audio */][1 /* isDecoder */] = true;
1547 mFlushingAudio = SHUT_DOWN;
1548 finishFlushIfPossible();
1549 needsToCreateAudioDecoder = false;
1550 }
1551 if (mRenderer == NULL) {
1552 return;
1553 }
1554 closeAudioSink();
1555 mRenderer->flush(true /* audio */, false /* notifyComplete */);
1556 if (mVideoDecoder != NULL) {
1557 mRenderer->flush(false /* audio */, false /* notifyComplete */);
1558 }
1559
Wei Jiac5de0912016-11-18 10:22:14 -08001560 performSeek(currentPositionUs, MediaPlayerSeekMode::SEEK_PREVIOUS_SYNC /* mode */);
Wei Jiabf70feb2016-02-19 15:47:16 -08001561
1562 if (forceNonOffload) {
1563 mRenderer->signalDisableOffloadAudio();
1564 mOffloadAudio = false;
1565 }
1566 if (needsToCreateAudioDecoder) {
Wei Jiaa05f1e32016-03-25 16:31:22 -07001567 instantiateDecoder(true /* audio */, &mAudioDecoder, !forceNonOffload);
Wei Jiabf70feb2016-02-19 15:47:16 -08001568 }
1569}
1570
Wei Jia41cd4632016-05-13 10:53:30 -07001571void NuPlayer::determineAudioModeChange(const sp<AMessage> &audioFormat) {
Wei Jiae4d18c72015-07-13 17:58:11 -07001572 if (mSource == NULL || mAudioSink == NULL) {
1573 return;
1574 }
1575
1576 if (mRenderer == NULL) {
1577 ALOGW("No renderer can be used to determine audio mode. Use non-offload for safety.");
1578 mOffloadAudio = false;
1579 return;
1580 }
1581
1582 sp<MetaData> audioMeta = mSource->getFormatMeta(true /* audio */);
1583 sp<AMessage> videoFormat = mSource->getFormat(false /* audio */);
1584 audio_stream_type_t streamType = mAudioSink->getAudioStreamType();
1585 const bool hasVideo = (videoFormat != NULL);
1586 const bool canOffload = canOffloadStream(
Wei Jiabf70feb2016-02-19 15:47:16 -08001587 audioMeta, hasVideo, mSource->isStreaming(), streamType)
1588 && (mPlaybackSettings.mSpeed == 1.f && mPlaybackSettings.mPitch == 1.f);
Wei Jiae4d18c72015-07-13 17:58:11 -07001589 if (canOffload) {
1590 if (!mOffloadAudio) {
1591 mRenderer->signalEnableOffloadAudio();
1592 }
1593 // open audio sink early under offload mode.
Wei Jia41cd4632016-05-13 10:53:30 -07001594 tryOpenAudioSinkForOffload(audioFormat, audioMeta, hasVideo);
Wei Jiae4d18c72015-07-13 17:58:11 -07001595 } else {
Robert Shihe1d70192015-07-23 17:54:13 -07001596 if (mOffloadAudio) {
1597 mRenderer->signalDisableOffloadAudio();
1598 mOffloadAudio = false;
1599 }
Wei Jiae4d18c72015-07-13 17:58:11 -07001600 }
1601}
1602
Wei Jiaa05f1e32016-03-25 16:31:22 -07001603status_t NuPlayer::instantiateDecoder(
1604 bool audio, sp<DecoderBase> *decoder, bool checkAudioModeChange) {
Wei Jia566da802015-08-27 13:59:30 -07001605 // The audio decoder could be cleared by tear down. If still in shut down
1606 // process, no need to create a new audio decoder.
1607 if (*decoder != NULL || (audio && mFlushingAudio == SHUT_DOWN)) {
Andreas Huberf9334412010-12-15 15:17:42 -08001608 return OK;
1609 }
1610
Andreas Huber84066782011-08-16 09:34:26 -07001611 sp<AMessage> format = mSource->getFormat(audio);
Andreas Huberf9334412010-12-15 15:17:42 -08001612
Andreas Huber84066782011-08-16 09:34:26 -07001613 if (format == NULL) {
Robert Shih7350b052015-10-01 15:50:14 -07001614 return UNKNOWN_ERROR;
1615 } else {
1616 status_t err;
1617 if (format->findInt32("err", &err) && err) {
1618 return err;
1619 }
Andreas Huberf9334412010-12-15 15:17:42 -08001620 }
1621
Ronghua Wu8db88132015-04-22 13:51:35 -07001622 format->setInt32("priority", 0 /* realtime */);
1623
Andreas Huber3fe62152011-09-16 15:09:22 -07001624 if (!audio) {
Andreas Huber84066782011-08-16 09:34:26 -07001625 AString mime;
1626 CHECK(format->findString("mime", &mime));
Chong Zhanga7fa1d92014-06-11 14:49:23 -07001627
Lajos Molnar1d15ab52015-03-04 16:46:34 -08001628 sp<AMessage> ccNotify = new AMessage(kWhatClosedCaptionNotify, this);
Chong Zhang341ab6e2015-02-04 13:37:18 -08001629 if (mCCDecoder == NULL) {
1630 mCCDecoder = new CCDecoder(ccNotify);
1631 }
Lajos Molnar09524832014-07-17 14:29:51 -07001632
1633 if (mSourceFlags & Source::FLAG_SECURE) {
1634 format->setInt32("secure", true);
1635 }
Chong Zhang17134602015-01-07 16:14:34 -08001636
1637 if (mSourceFlags & Source::FLAG_PROTECTED) {
1638 format->setInt32("protected", true);
1639 }
Ronghua Wu8db88132015-04-22 13:51:35 -07001640
Ronghua Wuc8a70d32015-04-29 16:26:34 -07001641 float rate = getFrameRate();
1642 if (rate > 0) {
Lajos Molnar3a474aa2015-04-24 17:10:07 -07001643 format->setFloat("operating-rate", rate * mPlaybackSettings.mSpeed);
Ronghua Wu8db88132015-04-22 13:51:35 -07001644 }
Andreas Huber3fe62152011-09-16 15:09:22 -07001645 }
1646
Wei Jiabc2fb722014-07-08 16:37:57 -07001647 if (audio) {
Lajos Molnar1d15ab52015-03-04 16:46:34 -08001648 sp<AMessage> notify = new AMessage(kWhatAudioNotify, this);
Wei Jia88703c32014-08-06 11:24:07 -07001649 ++mAudioDecoderGeneration;
1650 notify->setInt32("generation", mAudioDecoderGeneration);
1651
Wei Jiaa05f1e32016-03-25 16:31:22 -07001652 if (checkAudioModeChange) {
Wei Jia41cd4632016-05-13 10:53:30 -07001653 determineAudioModeChange(format);
Wei Jiaa05f1e32016-03-25 16:31:22 -07001654 }
Wei Jiabc2fb722014-07-08 16:37:57 -07001655 if (mOffloadAudio) {
Wei Jia14532f22015-12-29 11:28:15 -08001656 mSource->setOffloadAudio(true /* offload */);
1657
Haynes Mathew George8b635332015-03-30 17:59:47 -07001658 const bool hasVideo = (mSource->getFormat(false /*audio */) != NULL);
1659 format->setInt32("has-video", hasVideo);
Wei Jiac6cfd702014-11-11 16:33:20 -08001660 *decoder = new DecoderPassThrough(notify, mSource, mRenderer);
Wei Jiabc2fb722014-07-08 16:37:57 -07001661 } else {
Wei Jia14532f22015-12-29 11:28:15 -08001662 mSource->setOffloadAudio(false /* offload */);
1663
Wei Jiaf2ae3e12016-10-27 17:10:59 -07001664 *decoder = new Decoder(notify, mSource, mPID, mUID, mRenderer);
Wei Jiabc2fb722014-07-08 16:37:57 -07001665 }
1666 } else {
Lajos Molnar1d15ab52015-03-04 16:46:34 -08001667 sp<AMessage> notify = new AMessage(kWhatVideoNotify, this);
Wei Jia88703c32014-08-06 11:24:07 -07001668 ++mVideoDecoderGeneration;
1669 notify->setInt32("generation", mVideoDecoderGeneration);
1670
Chong Zhang7137ec72014-11-12 16:41:05 -08001671 *decoder = new Decoder(
Wei Jiaf2ae3e12016-10-27 17:10:59 -07001672 notify, mSource, mPID, mUID, mRenderer, mSurface, mCCDecoder);
Lajos Molnard9fd6312014-11-06 11:00:00 -08001673
1674 // enable FRC if high-quality AV sync is requested, even if not
Lajos Molnar1de1e252015-04-30 18:18:34 -07001675 // directly queuing to display, as this will even improve textureview
Lajos Molnard9fd6312014-11-06 11:00:00 -08001676 // playback.
1677 {
Marco Nelissen96626b72016-12-01 13:58:59 -08001678 if (property_get_bool("persist.sys.media.avsync", false)) {
Lajos Molnard9fd6312014-11-06 11:00:00 -08001679 format->setInt32("auto-frc", 1);
1680 }
1681 }
Wei Jiabc2fb722014-07-08 16:37:57 -07001682 }
Lajos Molnar1cd13982014-01-17 15:12:51 -08001683 (*decoder)->init();
Andreas Huber84066782011-08-16 09:34:26 -07001684 (*decoder)->configure(format);
Andreas Huberf9334412010-12-15 15:17:42 -08001685
Praveen Chavanbbaa1442016-04-08 13:33:49 -07001686 if (!audio) {
1687 sp<AMessage> params = new AMessage();
1688 float rate = getFrameRate();
1689 if (rate > 0) {
1690 params->setFloat("frame-rate-total", rate);
1691 }
1692
1693 sp<MetaData> fileMeta = getFileMeta();
1694 if (fileMeta != NULL) {
1695 int32_t videoTemporalLayerCount;
1696 if (fileMeta->findInt32(kKeyTemporalLayerCount, &videoTemporalLayerCount)
1697 && videoTemporalLayerCount > 0) {
1698 params->setInt32("temporal-layer-count", videoTemporalLayerCount);
1699 }
1700 }
1701
1702 if (params->countEntries() > 0) {
1703 (*decoder)->setParameters(params);
1704 }
1705 }
Andreas Huberf9334412010-12-15 15:17:42 -08001706 return OK;
1707}
1708
Chong Zhangced1c2f2014-08-08 15:22:35 -07001709void NuPlayer::updateVideoSize(
1710 const sp<AMessage> &inputFormat,
1711 const sp<AMessage> &outputFormat) {
1712 if (inputFormat == NULL) {
1713 ALOGW("Unknown video size, reporting 0x0!");
1714 notifyListener(MEDIA_SET_VIDEO_SIZE, 0, 0);
1715 return;
1716 }
1717
1718 int32_t displayWidth, displayHeight;
Chong Zhangced1c2f2014-08-08 15:22:35 -07001719 if (outputFormat != NULL) {
1720 int32_t width, height;
1721 CHECK(outputFormat->findInt32("width", &width));
1722 CHECK(outputFormat->findInt32("height", &height));
1723
1724 int32_t cropLeft, cropTop, cropRight, cropBottom;
1725 CHECK(outputFormat->findRect(
1726 "crop",
1727 &cropLeft, &cropTop, &cropRight, &cropBottom));
1728
1729 displayWidth = cropRight - cropLeft + 1;
1730 displayHeight = cropBottom - cropTop + 1;
1731
1732 ALOGV("Video output format changed to %d x %d "
1733 "(crop: %d x %d @ (%d, %d))",
1734 width, height,
1735 displayWidth,
1736 displayHeight,
1737 cropLeft, cropTop);
1738 } else {
1739 CHECK(inputFormat->findInt32("width", &displayWidth));
1740 CHECK(inputFormat->findInt32("height", &displayHeight));
1741
1742 ALOGV("Video input format %d x %d", displayWidth, displayHeight);
1743 }
1744
1745 // Take into account sample aspect ratio if necessary:
1746 int32_t sarWidth, sarHeight;
1747 if (inputFormat->findInt32("sar-width", &sarWidth)
1748 && inputFormat->findInt32("sar-height", &sarHeight)) {
1749 ALOGV("Sample aspect ratio %d : %d", sarWidth, sarHeight);
1750
1751 displayWidth = (displayWidth * sarWidth) / sarHeight;
1752
1753 ALOGV("display dimensions %d x %d", displayWidth, displayHeight);
Wei Jiadf6c6af2016-09-29 11:27:15 -07001754 } else {
1755 int32_t width, height;
1756 if (inputFormat->findInt32("display-width", &width)
1757 && inputFormat->findInt32("display-height", &height)
1758 && width > 0 && height > 0
1759 && displayWidth > 0 && displayHeight > 0) {
1760 if (displayHeight * (int64_t)width / height > (int64_t)displayWidth) {
1761 displayHeight = (int32_t)(displayWidth * (int64_t)height / width);
1762 } else {
1763 displayWidth = (int32_t)(displayHeight * (int64_t)width / height);
1764 }
1765 ALOGV("Video display width and height are overridden to %d x %d",
1766 displayWidth, displayHeight);
1767 }
Chong Zhangced1c2f2014-08-08 15:22:35 -07001768 }
1769
1770 int32_t rotationDegrees;
1771 if (!inputFormat->findInt32("rotation-degrees", &rotationDegrees)) {
1772 rotationDegrees = 0;
1773 }
1774
1775 if (rotationDegrees == 90 || rotationDegrees == 270) {
1776 int32_t tmp = displayWidth;
1777 displayWidth = displayHeight;
1778 displayHeight = tmp;
1779 }
1780
1781 notifyListener(
1782 MEDIA_SET_VIDEO_SIZE,
1783 displayWidth,
1784 displayHeight);
1785}
1786
Chong Zhangdcb89b32013-08-06 09:44:47 -07001787void NuPlayer::notifyListener(int msg, int ext1, int ext2, const Parcel *in) {
Andreas Huber43c3e6c2011-01-05 12:17:08 -08001788 if (mDriver == NULL) {
Andreas Huberf9334412010-12-15 15:17:42 -08001789 return;
1790 }
1791
Andreas Huber43c3e6c2011-01-05 12:17:08 -08001792 sp<NuPlayerDriver> driver = mDriver.promote();
Andreas Huberf9334412010-12-15 15:17:42 -08001793
Andreas Huber43c3e6c2011-01-05 12:17:08 -08001794 if (driver == NULL) {
Andreas Huberf9334412010-12-15 15:17:42 -08001795 return;
1796 }
1797
Chong Zhangdcb89b32013-08-06 09:44:47 -07001798 driver->notifyListener(msg, ext1, ext2, in);
Andreas Huberf9334412010-12-15 15:17:42 -08001799}
1800
Chong Zhang7137ec72014-11-12 16:41:05 -08001801void NuPlayer::flushDecoder(bool audio, bool needShutdown) {
Andreas Huber14f76722013-01-15 09:04:18 -08001802 ALOGV("[%s] flushDecoder needShutdown=%d",
1803 audio ? "audio" : "video", needShutdown);
1804
Chong Zhang7137ec72014-11-12 16:41:05 -08001805 const sp<DecoderBase> &decoder = getDecoder(audio);
Lajos Molnar87603c02014-08-20 19:25:30 -07001806 if (decoder == NULL) {
Steve Blockdf64d152012-01-04 20:05:49 +00001807 ALOGI("flushDecoder %s without decoder present",
Andreas Huber6e3d3112011-11-28 12:36:11 -08001808 audio ? "audio" : "video");
Lajos Molnar87603c02014-08-20 19:25:30 -07001809 return;
Andreas Huber6e3d3112011-11-28 12:36:11 -08001810 }
1811
Andreas Huber1aef2112011-01-04 14:01:29 -08001812 // Make sure we don't continue to scan sources until we finish flushing.
1813 ++mScanSourcesGeneration;
Chong Zhang3b032b32015-04-17 15:49:06 -07001814 if (mScanSourcesPending) {
Toshikazu Saitocbcbb792015-09-15 14:53:01 +09001815 if (!needShutdown) {
1816 mDeferredActions.push_back(
1817 new SimpleAction(&NuPlayer::performScanSources));
1818 }
Chong Zhang3b032b32015-04-17 15:49:06 -07001819 mScanSourcesPending = false;
1820 }
Andreas Huber1aef2112011-01-04 14:01:29 -08001821
Chong Zhang7137ec72014-11-12 16:41:05 -08001822 decoder->signalFlush();
Andreas Huber1aef2112011-01-04 14:01:29 -08001823
1824 FlushStatus newStatus =
1825 needShutdown ? FLUSHING_DECODER_SHUTDOWN : FLUSHING_DECODER;
1826
Lajos Molnarfcd3e942015-03-31 10:06:48 -07001827 mFlushComplete[audio][false /* isDecoder */] = (mRenderer == NULL);
Andy Hung8d121d42014-10-03 09:53:53 -07001828 mFlushComplete[audio][true /* isDecoder */] = false;
Andreas Huber1aef2112011-01-04 14:01:29 -08001829 if (audio) {
Wei Jia53904f32014-07-29 10:22:53 -07001830 ALOGE_IF(mFlushingAudio != NONE,
1831 "audio flushDecoder() is called in state %d", mFlushingAudio);
Andreas Huber1aef2112011-01-04 14:01:29 -08001832 mFlushingAudio = newStatus;
Andreas Huber1aef2112011-01-04 14:01:29 -08001833 } else {
Wei Jia53904f32014-07-29 10:22:53 -07001834 ALOGE_IF(mFlushingVideo != NONE,
1835 "video flushDecoder() is called in state %d", mFlushingVideo);
Andreas Huber1aef2112011-01-04 14:01:29 -08001836 mFlushingVideo = newStatus;
Andreas Huber1aef2112011-01-04 14:01:29 -08001837 }
1838}
1839
Chong Zhangced1c2f2014-08-08 15:22:35 -07001840void NuPlayer::queueDecoderShutdown(
1841 bool audio, bool video, const sp<AMessage> &reply) {
1842 ALOGI("queueDecoderShutdown audio=%d, video=%d", audio, video);
Andreas Huber84066782011-08-16 09:34:26 -07001843
Chong Zhangced1c2f2014-08-08 15:22:35 -07001844 mDeferredActions.push_back(
Wei Jiafef808d2014-10-31 17:57:05 -07001845 new FlushDecoderAction(
1846 audio ? FLUSH_CMD_SHUTDOWN : FLUSH_CMD_NONE,
1847 video ? FLUSH_CMD_SHUTDOWN : FLUSH_CMD_NONE));
Andreas Huber84066782011-08-16 09:34:26 -07001848
Chong Zhangced1c2f2014-08-08 15:22:35 -07001849 mDeferredActions.push_back(
1850 new SimpleAction(&NuPlayer::performScanSources));
Andreas Huber84066782011-08-16 09:34:26 -07001851
Chong Zhangced1c2f2014-08-08 15:22:35 -07001852 mDeferredActions.push_back(new PostMessageAction(reply));
1853
1854 processDeferredActions();
Andreas Huber84066782011-08-16 09:34:26 -07001855}
1856
James Dong0d268a32012-08-31 12:18:27 -07001857status_t NuPlayer::setVideoScalingMode(int32_t mode) {
1858 mVideoScalingMode = mode;
Lajos Molnar1de1e252015-04-30 18:18:34 -07001859 if (mSurface != NULL) {
1860 status_t ret = native_window_set_scaling_mode(mSurface.get(), mVideoScalingMode);
James Dong0d268a32012-08-31 12:18:27 -07001861 if (ret != OK) {
1862 ALOGE("Failed to set scaling mode (%d): %s",
1863 -ret, strerror(-ret));
1864 return ret;
1865 }
1866 }
1867 return OK;
1868}
1869
Chong Zhangdcb89b32013-08-06 09:44:47 -07001870status_t NuPlayer::getTrackInfo(Parcel* reply) const {
Lajos Molnar1d15ab52015-03-04 16:46:34 -08001871 sp<AMessage> msg = new AMessage(kWhatGetTrackInfo, this);
Chong Zhangdcb89b32013-08-06 09:44:47 -07001872 msg->setPointer("reply", reply);
1873
1874 sp<AMessage> response;
1875 status_t err = msg->postAndAwaitResponse(&response);
1876 return err;
1877}
1878
Robert Shih7c4f0d72014-07-09 18:53:31 -07001879status_t NuPlayer::getSelectedTrack(int32_t type, Parcel* reply) const {
Lajos Molnar1d15ab52015-03-04 16:46:34 -08001880 sp<AMessage> msg = new AMessage(kWhatGetSelectedTrack, this);
Robert Shih7c4f0d72014-07-09 18:53:31 -07001881 msg->setPointer("reply", reply);
1882 msg->setInt32("type", type);
1883
1884 sp<AMessage> response;
1885 status_t err = msg->postAndAwaitResponse(&response);
1886 if (err == OK && response != NULL) {
1887 CHECK(response->findInt32("err", &err));
1888 }
1889 return err;
1890}
1891
Robert Shih6ffb1fd2014-10-29 16:24:32 -07001892status_t NuPlayer::selectTrack(size_t trackIndex, bool select, int64_t timeUs) {
Lajos Molnar1d15ab52015-03-04 16:46:34 -08001893 sp<AMessage> msg = new AMessage(kWhatSelectTrack, this);
Chong Zhangdcb89b32013-08-06 09:44:47 -07001894 msg->setSize("trackIndex", trackIndex);
1895 msg->setInt32("select", select);
Robert Shih6ffb1fd2014-10-29 16:24:32 -07001896 msg->setInt64("timeUs", timeUs);
Chong Zhangdcb89b32013-08-06 09:44:47 -07001897
1898 sp<AMessage> response;
1899 status_t err = msg->postAndAwaitResponse(&response);
1900
Chong Zhang404fced2014-06-11 14:45:31 -07001901 if (err != OK) {
1902 return err;
1903 }
1904
1905 if (!response->findInt32("err", &err)) {
1906 err = OK;
1907 }
1908
Chong Zhangdcb89b32013-08-06 09:44:47 -07001909 return err;
1910}
1911
Ronghua Wua73d9e02014-10-08 15:13:29 -07001912status_t NuPlayer::getCurrentPosition(int64_t *mediaUs) {
1913 sp<Renderer> renderer = mRenderer;
1914 if (renderer == NULL) {
1915 return NO_INIT;
1916 }
1917
1918 return renderer->getCurrentPosition(mediaUs);
1919}
1920
Praveen Chavane1e5d7a2015-05-19 19:09:48 -07001921void NuPlayer::getStats(Vector<sp<AMessage> > *mTrackStats) {
1922 CHECK(mTrackStats != NULL);
1923
1924 mTrackStats->clear();
1925 if (mVideoDecoder != NULL) {
1926 mTrackStats->push_back(mVideoDecoder->getStats());
1927 }
1928 if (mAudioDecoder != NULL) {
1929 mTrackStats->push_back(mAudioDecoder->getStats());
Chong Zhang7137ec72014-11-12 16:41:05 -08001930 }
Ronghua Wua73d9e02014-10-08 15:13:29 -07001931}
1932
Marco Nelissenf0b72b52014-09-16 15:43:44 -07001933sp<MetaData> NuPlayer::getFileMeta() {
1934 return mSource->getFileFormatMeta();
1935}
1936
Ronghua Wuc8a70d32015-04-29 16:26:34 -07001937float NuPlayer::getFrameRate() {
1938 sp<MetaData> meta = mSource->getFormatMeta(false /* audio */);
1939 if (meta == NULL) {
1940 return 0;
1941 }
1942 int32_t rate;
1943 if (!meta->findInt32(kKeyFrameRate, &rate)) {
1944 // fall back to try file meta
1945 sp<MetaData> fileMeta = getFileMeta();
1946 if (fileMeta == NULL) {
1947 ALOGW("source has video meta but not file meta");
1948 return -1;
1949 }
1950 int32_t fileMetaRate;
1951 if (!fileMeta->findInt32(kKeyFrameRate, &fileMetaRate)) {
1952 return -1;
1953 }
1954 return fileMetaRate;
1955 }
1956 return rate;
1957}
1958
Andreas Huberb7c8e912012-11-27 15:02:53 -08001959void NuPlayer::schedulePollDuration() {
Lajos Molnar1d15ab52015-03-04 16:46:34 -08001960 sp<AMessage> msg = new AMessage(kWhatPollDuration, this);
Andreas Huberb7c8e912012-11-27 15:02:53 -08001961 msg->setInt32("generation", mPollDurationGeneration);
1962 msg->post();
1963}
1964
1965void NuPlayer::cancelPollDuration() {
1966 ++mPollDurationGeneration;
1967}
1968
Andreas Hubera1f8ab02012-11-30 10:53:22 -08001969void NuPlayer::processDeferredActions() {
1970 while (!mDeferredActions.empty()) {
1971 // We won't execute any deferred actions until we're no longer in
1972 // an intermediate state, i.e. one more more decoders are currently
1973 // flushing or shutting down.
1974
Andreas Hubera1f8ab02012-11-30 10:53:22 -08001975 if (mFlushingAudio != NONE || mFlushingVideo != NONE) {
1976 // We're currently flushing, postpone the reset until that's
1977 // completed.
1978
1979 ALOGV("postponing action mFlushingAudio=%d, mFlushingVideo=%d",
1980 mFlushingAudio, mFlushingVideo);
1981
1982 break;
1983 }
1984
1985 sp<Action> action = *mDeferredActions.begin();
1986 mDeferredActions.erase(mDeferredActions.begin());
1987
1988 action->execute(this);
1989 }
1990}
1991
Wei Jiac5de0912016-11-18 10:22:14 -08001992void NuPlayer::performSeek(int64_t seekTimeUs, MediaPlayerSeekMode mode) {
1993 ALOGV("performSeek seekTimeUs=%lld us (%.2f secs), mode=%d",
1994 (long long)seekTimeUs, seekTimeUs / 1E6, mode);
Andreas Hubera1f8ab02012-11-30 10:53:22 -08001995
Andy Hungadf34bf2014-09-03 18:22:22 -07001996 if (mSource == NULL) {
1997 // This happens when reset occurs right before the loop mode
1998 // asynchronously seeks to the start of the stream.
1999 LOG_ALWAYS_FATAL_IF(mAudioDecoder != NULL || mVideoDecoder != NULL,
2000 "mSource is NULL and decoders not NULL audio(%p) video(%p)",
2001 mAudioDecoder.get(), mVideoDecoder.get());
2002 return;
2003 }
Robert Shih1a5c8592015-08-04 18:07:44 -07002004 mPreviousSeekTimeUs = seekTimeUs;
Wei Jiac5de0912016-11-18 10:22:14 -08002005 mSource->seekTo(seekTimeUs, mode);
Robert Shihd3b0bbb2014-07-23 15:00:25 -07002006 ++mTimedTextGeneration;
Andreas Hubera1f8ab02012-11-30 10:53:22 -08002007
Andreas Hubera1f8ab02012-11-30 10:53:22 -08002008 // everything's flushed, continue playback.
2009}
2010
Wei Jiafef808d2014-10-31 17:57:05 -07002011void NuPlayer::performDecoderFlush(FlushCommand audio, FlushCommand video) {
2012 ALOGV("performDecoderFlush audio=%d, video=%d", audio, video);
Andreas Hubera1f8ab02012-11-30 10:53:22 -08002013
Wei Jiafef808d2014-10-31 17:57:05 -07002014 if ((audio == FLUSH_CMD_NONE || mAudioDecoder == NULL)
2015 && (video == FLUSH_CMD_NONE || mVideoDecoder == NULL)) {
Andreas Hubera1f8ab02012-11-30 10:53:22 -08002016 return;
2017 }
2018
Wei Jiafef808d2014-10-31 17:57:05 -07002019 if (audio != FLUSH_CMD_NONE && mAudioDecoder != NULL) {
2020 flushDecoder(true /* audio */, (audio == FLUSH_CMD_SHUTDOWN));
Andreas Hubera1f8ab02012-11-30 10:53:22 -08002021 }
2022
Wei Jiafef808d2014-10-31 17:57:05 -07002023 if (video != FLUSH_CMD_NONE && mVideoDecoder != NULL) {
2024 flushDecoder(false /* audio */, (video == FLUSH_CMD_SHUTDOWN));
Andreas Hubera1f8ab02012-11-30 10:53:22 -08002025 }
2026}
2027
2028void NuPlayer::performReset() {
2029 ALOGV("performReset");
2030
2031 CHECK(mAudioDecoder == NULL);
2032 CHECK(mVideoDecoder == NULL);
2033
2034 cancelPollDuration();
2035
2036 ++mScanSourcesGeneration;
2037 mScanSourcesPending = false;
2038
Lajos Molnar09524832014-07-17 14:29:51 -07002039 if (mRendererLooper != NULL) {
2040 if (mRenderer != NULL) {
2041 mRendererLooper->unregisterHandler(mRenderer->id());
2042 }
2043 mRendererLooper->stop();
2044 mRendererLooper.clear();
2045 }
Andreas Hubera1f8ab02012-11-30 10:53:22 -08002046 mRenderer.clear();
Wei Jia57568df2014-09-22 10:16:29 -07002047 ++mRendererGeneration;
Andreas Hubera1f8ab02012-11-30 10:53:22 -08002048
2049 if (mSource != NULL) {
2050 mSource->stop();
Andreas Huberb5f25f02013-02-05 10:14:26 -08002051
Wei Jiac45a4b22016-04-15 15:30:23 -07002052 Mutex::Autolock autoLock(mSourceLock);
Andreas Hubera1f8ab02012-11-30 10:53:22 -08002053 mSource.clear();
2054 }
2055
2056 if (mDriver != NULL) {
2057 sp<NuPlayerDriver> driver = mDriver.promote();
2058 if (driver != NULL) {
2059 driver->notifyResetComplete();
2060 }
2061 }
Andreas Huber57a339c2012-12-03 11:18:00 -08002062
2063 mStarted = false;
Wei Jia8a092d32016-06-03 14:57:24 -07002064 mPrepared = false;
Ronghua Wu64c2d172015-10-07 16:52:19 -07002065 mResetting = false;
Robert Shih0c61a0d2015-07-06 15:09:10 -07002066 mSourceStarted = false;
Andreas Hubera1f8ab02012-11-30 10:53:22 -08002067}
2068
2069void NuPlayer::performScanSources() {
2070 ALOGV("performScanSources");
2071
Andreas Huber57a339c2012-12-03 11:18:00 -08002072 if (!mStarted) {
2073 return;
2074 }
2075
Andreas Hubera1f8ab02012-11-30 10:53:22 -08002076 if (mAudioDecoder == NULL || mVideoDecoder == NULL) {
2077 postScanSources();
2078 }
2079}
2080
Lajos Molnar1de1e252015-04-30 18:18:34 -07002081void NuPlayer::performSetSurface(const sp<Surface> &surface) {
Andreas Huber57a339c2012-12-03 11:18:00 -08002082 ALOGV("performSetSurface");
2083
Lajos Molnar1de1e252015-04-30 18:18:34 -07002084 mSurface = surface;
Andreas Huber57a339c2012-12-03 11:18:00 -08002085
2086 // XXX - ignore error from setVideoScalingMode for now
2087 setVideoScalingMode(mVideoScalingMode);
Chong Zhang13d6faa2014-08-22 15:35:28 -07002088
2089 if (mDriver != NULL) {
2090 sp<NuPlayerDriver> driver = mDriver.promote();
2091 if (driver != NULL) {
2092 driver->notifySetSurfaceComplete();
2093 }
2094 }
Andreas Huber57a339c2012-12-03 11:18:00 -08002095}
2096
Chong Zhangf8d71772014-11-26 15:08:34 -08002097void NuPlayer::performResumeDecoders(bool needNotify) {
2098 if (needNotify) {
2099 mResumePending = true;
2100 if (mVideoDecoder == NULL) {
2101 // if audio-only, we can notify seek complete now,
2102 // as the resume operation will be relatively fast.
2103 finishResume();
2104 }
2105 }
2106
Chong Zhang7137ec72014-11-12 16:41:05 -08002107 if (mVideoDecoder != NULL) {
Chong Zhangf8d71772014-11-26 15:08:34 -08002108 // When there is continuous seek, MediaPlayer will cache the seek
2109 // position, and send down new seek request when previous seek is
2110 // complete. Let's wait for at least one video output frame before
2111 // notifying seek complete, so that the video thumbnail gets updated
2112 // when seekbar is dragged.
2113 mVideoDecoder->signalResume(needNotify);
Chong Zhang7137ec72014-11-12 16:41:05 -08002114 }
2115
2116 if (mAudioDecoder != NULL) {
Chong Zhangf8d71772014-11-26 15:08:34 -08002117 mAudioDecoder->signalResume(false /* needNotify */);
2118 }
2119}
2120
2121void NuPlayer::finishResume() {
2122 if (mResumePending) {
2123 mResumePending = false;
Wei Jia1061c9c2015-05-19 16:02:17 -07002124 notifyDriverSeekComplete();
2125 }
2126}
2127
2128void NuPlayer::notifyDriverSeekComplete() {
2129 if (mDriver != NULL) {
2130 sp<NuPlayerDriver> driver = mDriver.promote();
2131 if (driver != NULL) {
2132 driver->notifySeekComplete();
Chong Zhangf8d71772014-11-26 15:08:34 -08002133 }
Chong Zhang7137ec72014-11-12 16:41:05 -08002134 }
2135}
2136
Andreas Huber9575c962013-02-05 13:59:56 -08002137void NuPlayer::onSourceNotify(const sp<AMessage> &msg) {
2138 int32_t what;
2139 CHECK(msg->findInt32("what", &what));
2140
2141 switch (what) {
Lajos Molnarfcd3e942015-03-31 10:06:48 -07002142 case Source::kWhatInstantiateSecureDecoders:
2143 {
2144 if (mSource == NULL) {
2145 // This is a stale notification from a source that was
2146 // asynchronously preparing when the client called reset().
2147 // We handled the reset, the source is gone.
2148 break;
2149 }
2150
2151 sp<AMessage> reply;
2152 CHECK(msg->findMessage("reply", &reply));
2153 status_t err = onInstantiateSecureDecoders();
2154 reply->setInt32("err", err);
2155 reply->post();
2156 break;
2157 }
2158
Andreas Huber9575c962013-02-05 13:59:56 -08002159 case Source::kWhatPrepared:
2160 {
Andreas Huberb5f28d42013-04-25 15:11:19 -07002161 if (mSource == NULL) {
2162 // This is a stale notification from a source that was
2163 // asynchronously preparing when the client called reset().
2164 // We handled the reset, the source is gone.
2165 break;
2166 }
2167
Andreas Huberec0c5972013-02-05 14:47:13 -08002168 int32_t err;
2169 CHECK(msg->findInt32("err", &err));
2170
Lajos Molnarfcd3e942015-03-31 10:06:48 -07002171 if (err != OK) {
2172 // shut down potential secure codecs in case client never calls reset
2173 mDeferredActions.push_back(
2174 new FlushDecoderAction(FLUSH_CMD_SHUTDOWN /* audio */,
2175 FLUSH_CMD_SHUTDOWN /* video */));
2176 processDeferredActions();
Wei Jia8a092d32016-06-03 14:57:24 -07002177 } else {
2178 mPrepared = true;
Lajos Molnarfcd3e942015-03-31 10:06:48 -07002179 }
2180
Andreas Huber9575c962013-02-05 13:59:56 -08002181 sp<NuPlayerDriver> driver = mDriver.promote();
2182 if (driver != NULL) {
Marco Nelissendd114d12014-05-28 15:23:14 -07002183 // notify duration first, so that it's definitely set when
2184 // the app received the "prepare complete" callback.
2185 int64_t durationUs;
2186 if (mSource->getDuration(&durationUs) == OK) {
2187 driver->notifyDuration(durationUs);
2188 }
Andreas Huberec0c5972013-02-05 14:47:13 -08002189 driver->notifyPrepareCompleted(err);
Andreas Huber9575c962013-02-05 13:59:56 -08002190 }
Andreas Huber99759402013-04-01 14:28:31 -07002191
Andreas Huber9575c962013-02-05 13:59:56 -08002192 break;
2193 }
2194
2195 case Source::kWhatFlagsChanged:
2196 {
2197 uint32_t flags;
2198 CHECK(msg->findInt32("flags", (int32_t *)&flags));
2199
Chong Zhang4b7069d2013-09-11 12:52:43 -07002200 sp<NuPlayerDriver> driver = mDriver.promote();
2201 if (driver != NULL) {
Wei Jia895651b2014-12-10 17:31:52 -08002202 if ((flags & NuPlayer::Source::FLAG_CAN_SEEK) == 0) {
2203 driver->notifyListener(
2204 MEDIA_INFO, MEDIA_INFO_NOT_SEEKABLE, 0);
2205 }
Chong Zhang4b7069d2013-09-11 12:52:43 -07002206 driver->notifyFlagsChanged(flags);
2207 }
2208
Andreas Huber9575c962013-02-05 13:59:56 -08002209 if ((mSourceFlags & Source::FLAG_DYNAMIC_DURATION)
2210 && (!(flags & Source::FLAG_DYNAMIC_DURATION))) {
2211 cancelPollDuration();
2212 } else if (!(mSourceFlags & Source::FLAG_DYNAMIC_DURATION)
2213 && (flags & Source::FLAG_DYNAMIC_DURATION)
2214 && (mAudioDecoder != NULL || mVideoDecoder != NULL)) {
2215 schedulePollDuration();
2216 }
2217
2218 mSourceFlags = flags;
2219 break;
2220 }
2221
2222 case Source::kWhatVideoSizeChanged:
2223 {
Chong Zhangced1c2f2014-08-08 15:22:35 -07002224 sp<AMessage> format;
2225 CHECK(msg->findMessage("format", &format));
Andreas Huber9575c962013-02-05 13:59:56 -08002226
Chong Zhangced1c2f2014-08-08 15:22:35 -07002227 updateVideoSize(format);
Andreas Huber9575c962013-02-05 13:59:56 -08002228 break;
2229 }
2230
Chong Zhang2a3cc9a2014-08-21 17:48:26 -07002231 case Source::kWhatBufferingUpdate:
2232 {
2233 int32_t percentage;
2234 CHECK(msg->findInt32("percentage", &percentage));
2235
2236 notifyListener(MEDIA_BUFFERING_UPDATE, percentage, 0);
2237 break;
2238 }
2239
Chong Zhangefbb6192015-01-30 17:13:27 -08002240 case Source::kWhatPauseOnBufferingStart:
2241 {
2242 // ignore if not playing
Chong Zhang8a048332015-05-06 15:16:28 -07002243 if (mStarted) {
Chong Zhangefbb6192015-01-30 17:13:27 -08002244 ALOGI("buffer low, pausing...");
2245
Chong Zhang8a048332015-05-06 15:16:28 -07002246 mPausedForBuffering = true;
Chong Zhangefbb6192015-01-30 17:13:27 -08002247 onPause();
2248 }
Wei Jiabfe82072016-05-20 09:21:50 -07002249 notifyListener(MEDIA_INFO, MEDIA_INFO_BUFFERING_START, 0);
Roger Jönssonb50e83e2013-01-21 16:26:41 +01002250 break;
2251 }
2252
Chong Zhangefbb6192015-01-30 17:13:27 -08002253 case Source::kWhatResumeOnBufferingEnd:
2254 {
2255 // ignore if not playing
Chong Zhang8a048332015-05-06 15:16:28 -07002256 if (mStarted) {
Chong Zhangefbb6192015-01-30 17:13:27 -08002257 ALOGI("buffer ready, resuming...");
2258
Chong Zhang8a048332015-05-06 15:16:28 -07002259 mPausedForBuffering = false;
2260
2261 // do not resume yet if client didn't unpause
2262 if (!mPausedByClient) {
2263 onResume();
2264 }
Chong Zhangefbb6192015-01-30 17:13:27 -08002265 }
Wei Jia3bed45a2016-02-17 11:06:47 -08002266 notifyListener(MEDIA_INFO, MEDIA_INFO_BUFFERING_END, 0);
Roger Jönssonb50e83e2013-01-21 16:26:41 +01002267 break;
2268 }
2269
Chong Zhangefbb6192015-01-30 17:13:27 -08002270 case Source::kWhatCacheStats:
2271 {
2272 int32_t kbps;
2273 CHECK(msg->findInt32("bandwidth", &kbps));
2274
2275 notifyListener(MEDIA_INFO, MEDIA_INFO_NETWORK_BANDWIDTH, kbps);
2276 break;
2277 }
2278
Chong Zhangdcb89b32013-08-06 09:44:47 -07002279 case Source::kWhatSubtitleData:
2280 {
2281 sp<ABuffer> buffer;
2282 CHECK(msg->findBuffer("buffer", &buffer));
2283
Chong Zhang404fced2014-06-11 14:45:31 -07002284 sendSubtitleData(buffer, 0 /* baseIndex */);
Chong Zhangdcb89b32013-08-06 09:44:47 -07002285 break;
2286 }
2287
Robert Shih08528432015-04-08 09:06:54 -07002288 case Source::kWhatTimedMetaData:
2289 {
2290 sp<ABuffer> buffer;
2291 if (!msg->findBuffer("buffer", &buffer)) {
2292 notifyListener(MEDIA_INFO, MEDIA_INFO_METADATA_UPDATE, 0);
2293 } else {
2294 sendTimedMetaData(buffer);
2295 }
2296 break;
2297 }
2298
Robert Shihd3b0bbb2014-07-23 15:00:25 -07002299 case Source::kWhatTimedTextData:
2300 {
2301 int32_t generation;
2302 if (msg->findInt32("generation", &generation)
2303 && generation != mTimedTextGeneration) {
2304 break;
2305 }
2306
2307 sp<ABuffer> buffer;
2308 CHECK(msg->findBuffer("buffer", &buffer));
2309
2310 sp<NuPlayerDriver> driver = mDriver.promote();
2311 if (driver == NULL) {
2312 break;
2313 }
2314
2315 int posMs;
2316 int64_t timeUs, posUs;
2317 driver->getCurrentPosition(&posMs);
Patrik2 Carlsson24d484b2015-01-27 16:49:45 +01002318 posUs = (int64_t) posMs * 1000ll;
Robert Shihd3b0bbb2014-07-23 15:00:25 -07002319 CHECK(buffer->meta()->findInt64("timeUs", &timeUs));
2320
2321 if (posUs < timeUs) {
2322 if (!msg->findInt32("generation", &generation)) {
2323 msg->setInt32("generation", mTimedTextGeneration);
2324 }
2325 msg->post(timeUs - posUs);
2326 } else {
2327 sendTimedTextData(buffer);
2328 }
2329 break;
2330 }
2331
Andreas Huber14f76722013-01-15 09:04:18 -08002332 case Source::kWhatQueueDecoderShutdown:
2333 {
2334 int32_t audio, video;
2335 CHECK(msg->findInt32("audio", &audio));
2336 CHECK(msg->findInt32("video", &video));
2337
2338 sp<AMessage> reply;
2339 CHECK(msg->findMessage("reply", &reply));
2340
2341 queueDecoderShutdown(audio, video, reply);
2342 break;
2343 }
2344
Ronghua Wu80276872014-08-28 15:50:29 -07002345 case Source::kWhatDrmNoLicense:
2346 {
2347 notifyListener(MEDIA_ERROR, MEDIA_ERROR_UNKNOWN, ERROR_DRM_NO_LICENSE);
2348 break;
2349 }
2350
Andreas Huber9575c962013-02-05 13:59:56 -08002351 default:
2352 TRESPASS();
2353 }
2354}
2355
Chong Zhanga7fa1d92014-06-11 14:49:23 -07002356void NuPlayer::onClosedCaptionNotify(const sp<AMessage> &msg) {
2357 int32_t what;
2358 CHECK(msg->findInt32("what", &what));
2359
2360 switch (what) {
2361 case NuPlayer::CCDecoder::kWhatClosedCaptionData:
2362 {
2363 sp<ABuffer> buffer;
2364 CHECK(msg->findBuffer("buffer", &buffer));
2365
2366 size_t inbandTracks = 0;
2367 if (mSource != NULL) {
2368 inbandTracks = mSource->getTrackCount();
2369 }
2370
2371 sendSubtitleData(buffer, inbandTracks);
2372 break;
2373 }
2374
2375 case NuPlayer::CCDecoder::kWhatTrackAdded:
2376 {
2377 notifyListener(MEDIA_INFO, MEDIA_INFO_METADATA_UPDATE, 0);
2378
2379 break;
2380 }
2381
2382 default:
2383 TRESPASS();
2384 }
2385
2386
2387}
2388
Chong Zhang404fced2014-06-11 14:45:31 -07002389void NuPlayer::sendSubtitleData(const sp<ABuffer> &buffer, int32_t baseIndex) {
2390 int32_t trackIndex;
2391 int64_t timeUs, durationUs;
2392 CHECK(buffer->meta()->findInt32("trackIndex", &trackIndex));
2393 CHECK(buffer->meta()->findInt64("timeUs", &timeUs));
2394 CHECK(buffer->meta()->findInt64("durationUs", &durationUs));
2395
2396 Parcel in;
2397 in.writeInt32(trackIndex + baseIndex);
2398 in.writeInt64(timeUs);
2399 in.writeInt64(durationUs);
2400 in.writeInt32(buffer->size());
2401 in.writeInt32(buffer->size());
2402 in.write(buffer->data(), buffer->size());
2403
2404 notifyListener(MEDIA_SUBTITLE_DATA, 0, 0, &in);
2405}
Robert Shihd3b0bbb2014-07-23 15:00:25 -07002406
Robert Shih08528432015-04-08 09:06:54 -07002407void NuPlayer::sendTimedMetaData(const sp<ABuffer> &buffer) {
2408 int64_t timeUs;
2409 CHECK(buffer->meta()->findInt64("timeUs", &timeUs));
2410
2411 Parcel in;
2412 in.writeInt64(timeUs);
2413 in.writeInt32(buffer->size());
2414 in.writeInt32(buffer->size());
2415 in.write(buffer->data(), buffer->size());
2416
2417 notifyListener(MEDIA_META_DATA, 0, 0, &in);
2418}
2419
Robert Shihd3b0bbb2014-07-23 15:00:25 -07002420void NuPlayer::sendTimedTextData(const sp<ABuffer> &buffer) {
2421 const void *data;
2422 size_t size = 0;
2423 int64_t timeUs;
Marco Nelissen55e2f4c2015-09-04 15:57:15 -07002424 int32_t flag = TextDescriptions::IN_BAND_TEXT_3GPP;
Robert Shihd3b0bbb2014-07-23 15:00:25 -07002425
2426 AString mime;
2427 CHECK(buffer->meta()->findString("mime", &mime));
2428 CHECK(strcasecmp(mime.c_str(), MEDIA_MIMETYPE_TEXT_3GPP) == 0);
2429
2430 data = buffer->data();
2431 size = buffer->size();
2432
2433 Parcel parcel;
2434 if (size > 0) {
2435 CHECK(buffer->meta()->findInt64("timeUs", &timeUs));
Marco Nelissen55e2f4c2015-09-04 15:57:15 -07002436 int32_t global = 0;
2437 if (buffer->meta()->findInt32("global", &global) && global) {
2438 flag |= TextDescriptions::GLOBAL_DESCRIPTIONS;
2439 } else {
2440 flag |= TextDescriptions::LOCAL_DESCRIPTIONS;
2441 }
Robert Shihd3b0bbb2014-07-23 15:00:25 -07002442 TextDescriptions::getParcelOfDescriptions(
2443 (const uint8_t *)data, size, flag, timeUs / 1000, &parcel);
2444 }
2445
2446 if ((parcel.dataSize() > 0)) {
2447 notifyListener(MEDIA_TIMED_TEXT, 0, 0, &parcel);
2448 } else { // send an empty timed text
2449 notifyListener(MEDIA_TIMED_TEXT, 0, 0);
2450 }
2451}
Andreas Huberb5f25f02013-02-05 10:14:26 -08002452////////////////////////////////////////////////////////////////////////////////
2453
Chong Zhangced1c2f2014-08-08 15:22:35 -07002454sp<AMessage> NuPlayer::Source::getFormat(bool audio) {
2455 sp<MetaData> meta = getFormatMeta(audio);
2456
2457 if (meta == NULL) {
2458 return NULL;
2459 }
2460
2461 sp<AMessage> msg = new AMessage;
2462
2463 if(convertMetaDataToMessage(meta, &msg) == OK) {
2464 return msg;
2465 }
2466 return NULL;
2467}
2468
Andreas Huber9575c962013-02-05 13:59:56 -08002469void NuPlayer::Source::notifyFlagsChanged(uint32_t flags) {
2470 sp<AMessage> notify = dupNotify();
2471 notify->setInt32("what", kWhatFlagsChanged);
2472 notify->setInt32("flags", flags);
2473 notify->post();
2474}
2475
Chong Zhangced1c2f2014-08-08 15:22:35 -07002476void NuPlayer::Source::notifyVideoSizeChanged(const sp<AMessage> &format) {
Andreas Huber9575c962013-02-05 13:59:56 -08002477 sp<AMessage> notify = dupNotify();
2478 notify->setInt32("what", kWhatVideoSizeChanged);
Chong Zhangced1c2f2014-08-08 15:22:35 -07002479 notify->setMessage("format", format);
Andreas Huber9575c962013-02-05 13:59:56 -08002480 notify->post();
2481}
2482
Andreas Huberec0c5972013-02-05 14:47:13 -08002483void NuPlayer::Source::notifyPrepared(status_t err) {
Andreas Huber9575c962013-02-05 13:59:56 -08002484 sp<AMessage> notify = dupNotify();
2485 notify->setInt32("what", kWhatPrepared);
Andreas Huberec0c5972013-02-05 14:47:13 -08002486 notify->setInt32("err", err);
Andreas Huber9575c962013-02-05 13:59:56 -08002487 notify->post();
2488}
2489
Lajos Molnarfcd3e942015-03-31 10:06:48 -07002490void NuPlayer::Source::notifyInstantiateSecureDecoders(const sp<AMessage> &reply) {
2491 sp<AMessage> notify = dupNotify();
2492 notify->setInt32("what", kWhatInstantiateSecureDecoders);
2493 notify->setMessage("reply", reply);
2494 notify->post();
2495}
2496
Andreas Huber84333e02014-02-07 15:36:10 -08002497void NuPlayer::Source::onMessageReceived(const sp<AMessage> & /* msg */) {
Andreas Huberb5f25f02013-02-05 10:14:26 -08002498 TRESPASS();
2499}
2500
Andreas Huberf9334412010-12-15 15:17:42 -08002501} // namespace android