blob: 87d64cd015630968ca33cb62cab51ba6aeeb4e13 [file] [log] [blame]
Andreas Huberf9334412010-12-15 15:17:42 -08001/*
2 * Copyright (C) 2010 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17//#define LOG_NDEBUG 0
18#define LOG_TAG "NuPlayer"
19#include <utils/Log.h>
20
21#include "NuPlayer.h"
Andreas Huber5bc087c2010-12-23 10:27:40 -080022
23#include "HTTPLiveSource.h"
Chong Zhang7137ec72014-11-12 16:41:05 -080024#include "NuPlayerCCDecoder.h"
Andreas Huberf9334412010-12-15 15:17:42 -080025#include "NuPlayerDecoder.h"
Chong Zhang7137ec72014-11-12 16:41:05 -080026#include "NuPlayerDecoderBase.h"
Wei Jiabc2fb722014-07-08 16:37:57 -070027#include "NuPlayerDecoderPassThrough.h"
Andreas Huber43c3e6c2011-01-05 12:17:08 -080028#include "NuPlayerDriver.h"
Andreas Huberf9334412010-12-15 15:17:42 -080029#include "NuPlayerRenderer.h"
Andreas Huber5bc087c2010-12-23 10:27:40 -080030#include "NuPlayerSource.h"
Andreas Huber2bfdd422011-10-11 15:24:07 -070031#include "RTSPSource.h"
Andreas Huber5bc087c2010-12-23 10:27:40 -080032#include "StreamingSource.h"
Andreas Huberafed0e12011-09-20 15:39:58 -070033#include "GenericSource.h"
Robert Shihd3b0bbb2014-07-23 15:00:25 -070034#include "TextDescriptions.h"
Andreas Huber5bc087c2010-12-23 10:27:40 -080035
36#include "ATSParser.h"
Andreas Huberf9334412010-12-15 15:17:42 -080037
Lajos Molnard9fd6312014-11-06 11:00:00 -080038#include <cutils/properties.h>
39
Lajos Molnar3a474aa2015-04-24 17:10:07 -070040#include <media/AudioResamplerPublic.h>
41#include <media/AVSyncSettings.h>
42
Andreas Huber3831a062010-12-21 10:22:33 -080043#include <media/stagefright/foundation/hexdump.h>
Andreas Huberf9334412010-12-15 15:17:42 -080044#include <media/stagefright/foundation/ABuffer.h>
45#include <media/stagefright/foundation/ADebug.h>
46#include <media/stagefright/foundation/AMessage.h>
Lajos Molnar09524832014-07-17 14:29:51 -070047#include <media/stagefright/MediaBuffer.h>
Andreas Huber3fe62152011-09-16 15:09:22 -070048#include <media/stagefright/MediaDefs.h>
Andreas Huberf9334412010-12-15 15:17:42 -080049#include <media/stagefright/MediaErrors.h>
50#include <media/stagefright/MetaData.h>
Lajos Molnar1de1e252015-04-30 18:18:34 -070051
Andy McFadden8ba01022012-12-18 09:46:54 -080052#include <gui/IGraphicBufferProducer.h>
Lajos Molnar1de1e252015-04-30 18:18:34 -070053#include <gui/Surface.h>
Andreas Huberf9334412010-12-15 15:17:42 -080054
Andreas Huber3fe62152011-09-16 15:09:22 -070055#include "avc_utils.h"
56
Andreas Huber84066782011-08-16 09:34:26 -070057#include "ESDS.h"
58#include <media/stagefright/Utils.h>
59
Andreas Huberf9334412010-12-15 15:17:42 -080060namespace android {
61
Andreas Hubera1f8ab02012-11-30 10:53:22 -080062struct NuPlayer::Action : public RefBase {
63 Action() {}
64
65 virtual void execute(NuPlayer *player) = 0;
66
67private:
68 DISALLOW_EVIL_CONSTRUCTORS(Action);
69};
70
71struct NuPlayer::SeekAction : public Action {
Wei Jia29840802015-05-15 17:11:38 -070072 SeekAction(int64_t seekTimeUs)
73 : mSeekTimeUs(seekTimeUs) {
Andreas Hubera1f8ab02012-11-30 10:53:22 -080074 }
75
76 virtual void execute(NuPlayer *player) {
Wei Jia29840802015-05-15 17:11:38 -070077 player->performSeek(mSeekTimeUs);
Andreas Hubera1f8ab02012-11-30 10:53:22 -080078 }
79
80private:
81 int64_t mSeekTimeUs;
82
83 DISALLOW_EVIL_CONSTRUCTORS(SeekAction);
84};
85
Chong Zhangf8d71772014-11-26 15:08:34 -080086struct NuPlayer::ResumeDecoderAction : public Action {
87 ResumeDecoderAction(bool needNotify)
88 : mNeedNotify(needNotify) {
89 }
90
91 virtual void execute(NuPlayer *player) {
92 player->performResumeDecoders(mNeedNotify);
93 }
94
95private:
96 bool mNeedNotify;
97
98 DISALLOW_EVIL_CONSTRUCTORS(ResumeDecoderAction);
99};
100
Andreas Huber57a339c2012-12-03 11:18:00 -0800101struct NuPlayer::SetSurfaceAction : public Action {
Lajos Molnar1de1e252015-04-30 18:18:34 -0700102 SetSurfaceAction(const sp<Surface> &surface)
103 : mSurface(surface) {
Andreas Huber57a339c2012-12-03 11:18:00 -0800104 }
105
106 virtual void execute(NuPlayer *player) {
Lajos Molnar1de1e252015-04-30 18:18:34 -0700107 player->performSetSurface(mSurface);
Andreas Huber57a339c2012-12-03 11:18:00 -0800108 }
109
110private:
Lajos Molnar1de1e252015-04-30 18:18:34 -0700111 sp<Surface> mSurface;
Andreas Huber57a339c2012-12-03 11:18:00 -0800112
113 DISALLOW_EVIL_CONSTRUCTORS(SetSurfaceAction);
114};
115
Wei Jiafef808d2014-10-31 17:57:05 -0700116struct NuPlayer::FlushDecoderAction : public Action {
117 FlushDecoderAction(FlushCommand audio, FlushCommand video)
Andreas Huber14f76722013-01-15 09:04:18 -0800118 : mAudio(audio),
119 mVideo(video) {
120 }
121
122 virtual void execute(NuPlayer *player) {
Wei Jiafef808d2014-10-31 17:57:05 -0700123 player->performDecoderFlush(mAudio, mVideo);
Andreas Huber14f76722013-01-15 09:04:18 -0800124 }
125
126private:
Wei Jiafef808d2014-10-31 17:57:05 -0700127 FlushCommand mAudio;
128 FlushCommand mVideo;
Andreas Huber14f76722013-01-15 09:04:18 -0800129
Wei Jiafef808d2014-10-31 17:57:05 -0700130 DISALLOW_EVIL_CONSTRUCTORS(FlushDecoderAction);
Andreas Huber14f76722013-01-15 09:04:18 -0800131};
132
133struct NuPlayer::PostMessageAction : public Action {
134 PostMessageAction(const sp<AMessage> &msg)
135 : mMessage(msg) {
136 }
137
138 virtual void execute(NuPlayer *) {
139 mMessage->post();
140 }
141
142private:
143 sp<AMessage> mMessage;
144
145 DISALLOW_EVIL_CONSTRUCTORS(PostMessageAction);
146};
147
Andreas Hubera1f8ab02012-11-30 10:53:22 -0800148// Use this if there's no state necessary to save in order to execute
149// the action.
150struct NuPlayer::SimpleAction : public Action {
151 typedef void (NuPlayer::*ActionFunc)();
152
153 SimpleAction(ActionFunc func)
154 : mFunc(func) {
155 }
156
157 virtual void execute(NuPlayer *player) {
158 (player->*mFunc)();
159 }
160
161private:
162 ActionFunc mFunc;
163
164 DISALLOW_EVIL_CONSTRUCTORS(SimpleAction);
165};
166
Andreas Huberf9334412010-12-15 15:17:42 -0800167////////////////////////////////////////////////////////////////////////////////
168
Ronghua Wu68845c12015-07-21 09:50:48 -0700169NuPlayer::NuPlayer(pid_t pid)
Andreas Huber9b80c2b2011-06-30 15:47:02 -0700170 : mUIDValid(false),
Ronghua Wu68845c12015-07-21 09:50:48 -0700171 mPID(pid),
Andreas Huber9575c962013-02-05 13:59:56 -0800172 mSourceFlags(0),
Wei Jiabc2fb722014-07-08 16:37:57 -0700173 mOffloadAudio(false),
Wei Jia88703c32014-08-06 11:24:07 -0700174 mAudioDecoderGeneration(0),
175 mVideoDecoderGeneration(0),
Wei Jia57568df2014-09-22 10:16:29 -0700176 mRendererGeneration(0),
Robert Shih1a5c8592015-08-04 18:07:44 -0700177 mPreviousSeekTimeUs(0),
Andreas Huber9b80c2b2011-06-30 15:47:02 -0700178 mAudioEOS(false),
Andreas Huberf9334412010-12-15 15:17:42 -0800179 mVideoEOS(false),
Andreas Huber5bc087c2010-12-23 10:27:40 -0800180 mScanSourcesPending(false),
Andreas Huber1aef2112011-01-04 14:01:29 -0800181 mScanSourcesGeneration(0),
Andreas Huberb7c8e912012-11-27 15:02:53 -0800182 mPollDurationGeneration(0),
Robert Shihd3b0bbb2014-07-23 15:00:25 -0700183 mTimedTextGeneration(0),
Andreas Huberf9334412010-12-15 15:17:42 -0800184 mFlushingAudio(NONE),
Andreas Huber1aef2112011-01-04 14:01:29 -0800185 mFlushingVideo(NONE),
Chong Zhangf8d71772014-11-26 15:08:34 -0800186 mResumePending(false),
Andreas Huber57a339c2012-12-03 11:18:00 -0800187 mVideoScalingMode(NATIVE_WINDOW_SCALING_MODE_SCALE_TO_WINDOW),
Lajos Molnar3a474aa2015-04-24 17:10:07 -0700188 mPlaybackSettings(AUDIO_PLAYBACK_RATE_DEFAULT),
189 mVideoFpsHint(-1.f),
Chong Zhangefbb6192015-01-30 17:13:27 -0800190 mStarted(false),
Ronghua Wu64c2d172015-10-07 16:52:19 -0700191 mResetting(false),
Robert Shih0c61a0d2015-07-06 15:09:10 -0700192 mSourceStarted(false),
Chong Zhangefbb6192015-01-30 17:13:27 -0800193 mPaused(false),
Wei Jia71c75e02016-02-04 09:40:47 -0800194 mPausedByClient(true),
Chong Zhang8a048332015-05-06 15:16:28 -0700195 mPausedForBuffering(false) {
Andy Hung8d121d42014-10-03 09:53:53 -0700196 clearFlushComplete();
Andreas Huberf9334412010-12-15 15:17:42 -0800197}
198
199NuPlayer::~NuPlayer() {
200}
201
Andreas Huber9b80c2b2011-06-30 15:47:02 -0700202void NuPlayer::setUID(uid_t uid) {
203 mUIDValid = true;
204 mUID = uid;
205}
206
Andreas Huber43c3e6c2011-01-05 12:17:08 -0800207void NuPlayer::setDriver(const wp<NuPlayerDriver> &driver) {
208 mDriver = driver;
Andreas Huberf9334412010-12-15 15:17:42 -0800209}
210
Andreas Huber9575c962013-02-05 13:59:56 -0800211void NuPlayer::setDataSourceAsync(const sp<IStreamSource> &source) {
Lajos Molnar1d15ab52015-03-04 16:46:34 -0800212 sp<AMessage> msg = new AMessage(kWhatSetDataSource, this);
Andreas Huberf9334412010-12-15 15:17:42 -0800213
Lajos Molnar1d15ab52015-03-04 16:46:34 -0800214 sp<AMessage> notify = new AMessage(kWhatSourceNotify, this);
Andreas Huberb5f25f02013-02-05 10:14:26 -0800215
Andreas Huber240abcc2014-02-13 13:32:37 -0800216 msg->setObject("source", new StreamingSource(notify, source));
Andreas Huber5bc087c2010-12-23 10:27:40 -0800217 msg->post();
218}
Andreas Huberf9334412010-12-15 15:17:42 -0800219
Andreas Huberafed0e12011-09-20 15:39:58 -0700220static bool IsHTTPLiveURL(const char *url) {
221 if (!strncasecmp("http://", url, 7)
Andreas Huber99759402013-04-01 14:28:31 -0700222 || !strncasecmp("https://", url, 8)
223 || !strncasecmp("file://", url, 7)) {
Andreas Huberafed0e12011-09-20 15:39:58 -0700224 size_t len = strlen(url);
225 if (len >= 5 && !strcasecmp(".m3u8", &url[len - 5])) {
226 return true;
227 }
228
229 if (strstr(url,"m3u8")) {
230 return true;
231 }
232 }
233
234 return false;
235}
236
Andreas Huber9575c962013-02-05 13:59:56 -0800237void NuPlayer::setDataSourceAsync(
Andreas Huber1b86fe02014-01-29 11:13:26 -0800238 const sp<IMediaHTTPService> &httpService,
239 const char *url,
240 const KeyedVector<String8, String8> *headers) {
Chong Zhang3de157d2014-08-05 20:54:44 -0700241
Lajos Molnar1d15ab52015-03-04 16:46:34 -0800242 sp<AMessage> msg = new AMessage(kWhatSetDataSource, this);
Oscar Rydhé7a33b772012-02-20 10:15:48 +0100243 size_t len = strlen(url);
Andreas Huber5bc087c2010-12-23 10:27:40 -0800244
Lajos Molnar1d15ab52015-03-04 16:46:34 -0800245 sp<AMessage> notify = new AMessage(kWhatSourceNotify, this);
Andreas Huberb5f25f02013-02-05 10:14:26 -0800246
Andreas Huberafed0e12011-09-20 15:39:58 -0700247 sp<Source> source;
248 if (IsHTTPLiveURL(url)) {
Andreas Huber81e68442014-02-05 11:52:33 -0800249 source = new HTTPLiveSource(notify, httpService, url, headers);
Andreas Huberafed0e12011-09-20 15:39:58 -0700250 } else if (!strncasecmp(url, "rtsp://", 7)) {
Andreas Huber1b86fe02014-01-29 11:13:26 -0800251 source = new RTSPSource(
252 notify, httpService, url, headers, mUIDValid, mUID);
Oscar Rydhé7a33b772012-02-20 10:15:48 +0100253 } else if ((!strncasecmp(url, "http://", 7)
254 || !strncasecmp(url, "https://", 8))
255 && ((len >= 4 && !strcasecmp(".sdp", &url[len - 4]))
256 || strstr(url, ".sdp?"))) {
Andreas Huber1b86fe02014-01-29 11:13:26 -0800257 source = new RTSPSource(
258 notify, httpService, url, headers, mUIDValid, mUID, true);
Andreas Huber2bfdd422011-10-11 15:24:07 -0700259 } else {
Chong Zhang3de157d2014-08-05 20:54:44 -0700260 sp<GenericSource> genericSource =
261 new GenericSource(notify, mUIDValid, mUID);
262 // Don't set FLAG_SECURE on mSourceFlags here for widevine.
263 // The correct flags will be updated in Source::kWhatFlagsChanged
264 // handler when GenericSource is prepared.
Andreas Huber2bfdd422011-10-11 15:24:07 -0700265
Chong Zhanga19f33e2014-08-07 15:35:07 -0700266 status_t err = genericSource->setDataSource(httpService, url, headers);
Chong Zhang3de157d2014-08-05 20:54:44 -0700267
268 if (err == OK) {
269 source = genericSource;
270 } else {
Chong Zhanga19f33e2014-08-07 15:35:07 -0700271 ALOGE("Failed to set data source!");
Chong Zhang3de157d2014-08-05 20:54:44 -0700272 }
273 }
Andreas Huberafed0e12011-09-20 15:39:58 -0700274 msg->setObject("source", source);
275 msg->post();
276}
277
Andreas Huber9575c962013-02-05 13:59:56 -0800278void NuPlayer::setDataSourceAsync(int fd, int64_t offset, int64_t length) {
Lajos Molnar1d15ab52015-03-04 16:46:34 -0800279 sp<AMessage> msg = new AMessage(kWhatSetDataSource, this);
Andreas Huberafed0e12011-09-20 15:39:58 -0700280
Lajos Molnar1d15ab52015-03-04 16:46:34 -0800281 sp<AMessage> notify = new AMessage(kWhatSourceNotify, this);
Andreas Huberb5f25f02013-02-05 10:14:26 -0800282
Chong Zhang3de157d2014-08-05 20:54:44 -0700283 sp<GenericSource> source =
284 new GenericSource(notify, mUIDValid, mUID);
285
Chong Zhanga19f33e2014-08-07 15:35:07 -0700286 status_t err = source->setDataSource(fd, offset, length);
Chong Zhang3de157d2014-08-05 20:54:44 -0700287
288 if (err != OK) {
Chong Zhanga19f33e2014-08-07 15:35:07 -0700289 ALOGE("Failed to set data source!");
Chong Zhang3de157d2014-08-05 20:54:44 -0700290 source = NULL;
291 }
292
Andreas Huberafed0e12011-09-20 15:39:58 -0700293 msg->setObject("source", source);
Andreas Huberf9334412010-12-15 15:17:42 -0800294 msg->post();
295}
296
Chris Watkins99f31602015-03-20 13:06:33 -0700297void NuPlayer::setDataSourceAsync(const sp<DataSource> &dataSource) {
298 sp<AMessage> msg = new AMessage(kWhatSetDataSource, this);
299 sp<AMessage> notify = new AMessage(kWhatSourceNotify, this);
300
301 sp<GenericSource> source = new GenericSource(notify, mUIDValid, mUID);
302 status_t err = source->setDataSource(dataSource);
303
304 if (err != OK) {
305 ALOGE("Failed to set data source!");
306 source = NULL;
307 }
308
309 msg->setObject("source", source);
310 msg->post();
311}
312
Andreas Huber9575c962013-02-05 13:59:56 -0800313void NuPlayer::prepareAsync() {
Lajos Molnar1d15ab52015-03-04 16:46:34 -0800314 (new AMessage(kWhatPrepare, this))->post();
Andreas Huber9575c962013-02-05 13:59:56 -0800315}
316
Andreas Huber57a339c2012-12-03 11:18:00 -0800317void NuPlayer::setVideoSurfaceTextureAsync(
Andy McFadden8ba01022012-12-18 09:46:54 -0800318 const sp<IGraphicBufferProducer> &bufferProducer) {
Lajos Molnar1de1e252015-04-30 18:18:34 -0700319 sp<AMessage> msg = new AMessage(kWhatSetVideoSurface, this);
Andreas Huber57a339c2012-12-03 11:18:00 -0800320
Andy McFadden8ba01022012-12-18 09:46:54 -0800321 if (bufferProducer == NULL) {
Lajos Molnar1de1e252015-04-30 18:18:34 -0700322 msg->setObject("surface", NULL);
Andreas Huber57a339c2012-12-03 11:18:00 -0800323 } else {
Lajos Molnar1de1e252015-04-30 18:18:34 -0700324 msg->setObject("surface", new Surface(bufferProducer, true /* controlledByApp */));
Andreas Huber57a339c2012-12-03 11:18:00 -0800325 }
326
Andreas Huberf9334412010-12-15 15:17:42 -0800327 msg->post();
328}
329
330void NuPlayer::setAudioSink(const sp<MediaPlayerBase::AudioSink> &sink) {
Lajos Molnar1d15ab52015-03-04 16:46:34 -0800331 sp<AMessage> msg = new AMessage(kWhatSetAudioSink, this);
Andreas Huberf9334412010-12-15 15:17:42 -0800332 msg->setObject("sink", sink);
333 msg->post();
334}
335
336void NuPlayer::start() {
Lajos Molnar1d15ab52015-03-04 16:46:34 -0800337 (new AMessage(kWhatStart, this))->post();
Andreas Huberf9334412010-12-15 15:17:42 -0800338}
339
Lajos Molnar3a474aa2015-04-24 17:10:07 -0700340status_t NuPlayer::setPlaybackSettings(const AudioPlaybackRate &rate) {
341 // do some cursory validation of the settings here. audio modes are
342 // only validated when set on the audiosink.
343 if ((rate.mSpeed != 0.f && rate.mSpeed < AUDIO_TIMESTRETCH_SPEED_MIN)
344 || rate.mSpeed > AUDIO_TIMESTRETCH_SPEED_MAX
345 || rate.mPitch < AUDIO_TIMESTRETCH_SPEED_MIN
346 || rate.mPitch > AUDIO_TIMESTRETCH_SPEED_MAX) {
347 return BAD_VALUE;
348 }
349 sp<AMessage> msg = new AMessage(kWhatConfigPlayback, this);
350 writeToAMessage(msg, rate);
351 sp<AMessage> response;
352 status_t err = msg->postAndAwaitResponse(&response);
353 if (err == OK && response != NULL) {
354 CHECK(response->findInt32("err", &err));
355 }
356 return err;
357}
358
359status_t NuPlayer::getPlaybackSettings(AudioPlaybackRate *rate /* nonnull */) {
360 sp<AMessage> msg = new AMessage(kWhatGetPlaybackSettings, this);
361 sp<AMessage> response;
362 status_t err = msg->postAndAwaitResponse(&response);
363 if (err == OK && response != NULL) {
364 CHECK(response->findInt32("err", &err));
365 if (err == OK) {
366 readFromAMessage(response, rate);
367 }
368 }
369 return err;
370}
371
372status_t NuPlayer::setSyncSettings(const AVSyncSettings &sync, float videoFpsHint) {
373 sp<AMessage> msg = new AMessage(kWhatConfigSync, this);
374 writeToAMessage(msg, sync, videoFpsHint);
375 sp<AMessage> response;
376 status_t err = msg->postAndAwaitResponse(&response);
377 if (err == OK && response != NULL) {
378 CHECK(response->findInt32("err", &err));
379 }
380 return err;
381}
382
383status_t NuPlayer::getSyncSettings(
384 AVSyncSettings *sync /* nonnull */, float *videoFps /* nonnull */) {
385 sp<AMessage> msg = new AMessage(kWhatGetSyncSettings, this);
386 sp<AMessage> response;
387 status_t err = msg->postAndAwaitResponse(&response);
388 if (err == OK && response != NULL) {
389 CHECK(response->findInt32("err", &err));
390 if (err == OK) {
391 readFromAMessage(response, sync, videoFps);
392 }
393 }
394 return err;
Wei Jia98160162015-02-04 17:01:11 -0800395}
396
Andreas Huber43c3e6c2011-01-05 12:17:08 -0800397void NuPlayer::pause() {
Lajos Molnar1d15ab52015-03-04 16:46:34 -0800398 (new AMessage(kWhatPause, this))->post();
Andreas Huber43c3e6c2011-01-05 12:17:08 -0800399}
400
Andreas Huber1aef2112011-01-04 14:01:29 -0800401void NuPlayer::resetAsync() {
Wei Jiac45a4b22016-04-15 15:30:23 -0700402 sp<Source> source;
403 {
404 Mutex::Autolock autoLock(mSourceLock);
405 source = mSource;
406 }
407
408 if (source != NULL) {
Chong Zhang48296b72014-09-14 14:28:45 -0700409 // During a reset, the data source might be unresponsive already, we need to
410 // disconnect explicitly so that reads exit promptly.
411 // We can't queue the disconnect request to the looper, as it might be
412 // queued behind a stuck read and never gets processed.
413 // Doing a disconnect outside the looper to allows the pending reads to exit
414 // (either successfully or with error).
Wei Jiac45a4b22016-04-15 15:30:23 -0700415 source->disconnect();
Chong Zhang48296b72014-09-14 14:28:45 -0700416 }
417
Lajos Molnar1d15ab52015-03-04 16:46:34 -0800418 (new AMessage(kWhatReset, this))->post();
Andreas Huber1aef2112011-01-04 14:01:29 -0800419}
420
Wei Jiae427abf2014-09-22 15:21:11 -0700421void NuPlayer::seekToAsync(int64_t seekTimeUs, bool needNotify) {
Lajos Molnar1d15ab52015-03-04 16:46:34 -0800422 sp<AMessage> msg = new AMessage(kWhatSeek, this);
Andreas Huber43c3e6c2011-01-05 12:17:08 -0800423 msg->setInt64("seekTimeUs", seekTimeUs);
Wei Jiae427abf2014-09-22 15:21:11 -0700424 msg->setInt32("needNotify", needNotify);
Andreas Huber43c3e6c2011-01-05 12:17:08 -0800425 msg->post();
426}
427
Andreas Huber53df1a42010-12-22 10:03:04 -0800428
Chong Zhang404fced2014-06-11 14:45:31 -0700429void NuPlayer::writeTrackInfo(
430 Parcel* reply, const sp<AMessage> format) const {
Marco Nelissenc367ca12015-09-15 09:51:59 -0700431 if (format == NULL) {
432 ALOGE("NULL format");
433 return;
434 }
Chong Zhang404fced2014-06-11 14:45:31 -0700435 int32_t trackType;
Marco Nelissen9436e482015-09-15 10:21:53 -0700436 if (!format->findInt32("type", &trackType)) {
Marco Nelissenc367ca12015-09-15 09:51:59 -0700437 ALOGE("no track type");
438 return;
439 }
Chong Zhang404fced2014-06-11 14:45:31 -0700440
Robert Shih755106e2015-04-30 14:36:45 -0700441 AString mime;
Robert Shih2e3a4252015-05-06 10:21:15 -0700442 if (!format->findString("mime", &mime)) {
443 // Java MediaPlayer only uses mimetype for subtitle and timedtext tracks.
444 // If we can't find the mimetype here it means that we wouldn't be needing
445 // the mimetype on the Java end. We still write a placeholder mime to keep the
446 // (de)serialization logic simple.
447 if (trackType == MEDIA_TRACK_TYPE_AUDIO) {
448 mime = "audio/";
449 } else if (trackType == MEDIA_TRACK_TYPE_VIDEO) {
450 mime = "video/";
451 } else {
Marco Nelissenc367ca12015-09-15 09:51:59 -0700452 ALOGE("unknown track type: %d", trackType);
453 return;
Robert Shih2e3a4252015-05-06 10:21:15 -0700454 }
455 }
Robert Shih755106e2015-04-30 14:36:45 -0700456
Chong Zhang404fced2014-06-11 14:45:31 -0700457 AString lang;
Marco Nelissen9436e482015-09-15 10:21:53 -0700458 if (!format->findString("language", &lang)) {
Marco Nelissenc367ca12015-09-15 09:51:59 -0700459 ALOGE("no language");
460 return;
461 }
Chong Zhang404fced2014-06-11 14:45:31 -0700462
463 reply->writeInt32(2); // write something non-zero
464 reply->writeInt32(trackType);
Robert Shih755106e2015-04-30 14:36:45 -0700465 reply->writeString16(String16(mime.c_str()));
Chong Zhang404fced2014-06-11 14:45:31 -0700466 reply->writeString16(String16(lang.c_str()));
467
468 if (trackType == MEDIA_TRACK_TYPE_SUBTITLE) {
Chong Zhang404fced2014-06-11 14:45:31 -0700469 int32_t isAuto, isDefault, isForced;
470 CHECK(format->findInt32("auto", &isAuto));
471 CHECK(format->findInt32("default", &isDefault));
472 CHECK(format->findInt32("forced", &isForced));
473
Chong Zhang404fced2014-06-11 14:45:31 -0700474 reply->writeInt32(isAuto);
475 reply->writeInt32(isDefault);
476 reply->writeInt32(isForced);
477 }
478}
479
Andreas Huberf9334412010-12-15 15:17:42 -0800480void NuPlayer::onMessageReceived(const sp<AMessage> &msg) {
481 switch (msg->what()) {
482 case kWhatSetDataSource:
483 {
Steve Block3856b092011-10-20 11:56:00 +0100484 ALOGV("kWhatSetDataSource");
Andreas Huberf9334412010-12-15 15:17:42 -0800485
486 CHECK(mSource == NULL);
487
Chong Zhang3de157d2014-08-05 20:54:44 -0700488 status_t err = OK;
Andreas Huber5bc087c2010-12-23 10:27:40 -0800489 sp<RefBase> obj;
490 CHECK(msg->findObject("source", &obj));
Chong Zhang3de157d2014-08-05 20:54:44 -0700491 if (obj != NULL) {
Wei Jiac45a4b22016-04-15 15:30:23 -0700492 Mutex::Autolock autoLock(mSourceLock);
Chong Zhang3de157d2014-08-05 20:54:44 -0700493 mSource = static_cast<Source *>(obj.get());
Chong Zhang3de157d2014-08-05 20:54:44 -0700494 } else {
495 err = UNKNOWN_ERROR;
496 }
Andreas Huber9575c962013-02-05 13:59:56 -0800497
498 CHECK(mDriver != NULL);
499 sp<NuPlayerDriver> driver = mDriver.promote();
500 if (driver != NULL) {
Chong Zhang3de157d2014-08-05 20:54:44 -0700501 driver->notifySetDataSourceCompleted(err);
Andreas Huber9575c962013-02-05 13:59:56 -0800502 }
503 break;
504 }
505
506 case kWhatPrepare:
507 {
508 mSource->prepareAsync();
Andreas Huberf9334412010-12-15 15:17:42 -0800509 break;
510 }
511
Chong Zhangdcb89b32013-08-06 09:44:47 -0700512 case kWhatGetTrackInfo:
513 {
Lajos Molnar3f274362015-03-05 14:35:41 -0800514 sp<AReplyToken> replyID;
Chong Zhangdcb89b32013-08-06 09:44:47 -0700515 CHECK(msg->senderAwaitsResponse(&replyID));
516
Chong Zhang404fced2014-06-11 14:45:31 -0700517 Parcel* reply;
518 CHECK(msg->findPointer("reply", (void**)&reply));
519
520 size_t inbandTracks = 0;
Chong Zhangdcb89b32013-08-06 09:44:47 -0700521 if (mSource != NULL) {
Chong Zhang404fced2014-06-11 14:45:31 -0700522 inbandTracks = mSource->getTrackCount();
523 }
524
Chong Zhanga7fa1d92014-06-11 14:49:23 -0700525 size_t ccTracks = 0;
526 if (mCCDecoder != NULL) {
527 ccTracks = mCCDecoder->getTrackCount();
528 }
529
Chong Zhang404fced2014-06-11 14:45:31 -0700530 // total track count
Chong Zhanga7fa1d92014-06-11 14:49:23 -0700531 reply->writeInt32(inbandTracks + ccTracks);
Chong Zhang404fced2014-06-11 14:45:31 -0700532
533 // write inband tracks
534 for (size_t i = 0; i < inbandTracks; ++i) {
535 writeTrackInfo(reply, mSource->getTrackInfo(i));
Chong Zhangdcb89b32013-08-06 09:44:47 -0700536 }
537
Chong Zhanga7fa1d92014-06-11 14:49:23 -0700538 // write CC track
539 for (size_t i = 0; i < ccTracks; ++i) {
540 writeTrackInfo(reply, mCCDecoder->getTrackInfo(i));
541 }
542
Chong Zhangdcb89b32013-08-06 09:44:47 -0700543 sp<AMessage> response = new AMessage;
Chong Zhangdcb89b32013-08-06 09:44:47 -0700544 response->postReply(replyID);
545 break;
546 }
547
Robert Shih7c4f0d72014-07-09 18:53:31 -0700548 case kWhatGetSelectedTrack:
549 {
550 status_t err = INVALID_OPERATION;
551 if (mSource != NULL) {
552 err = OK;
553
554 int32_t type32;
555 CHECK(msg->findInt32("type", (int32_t*)&type32));
556 media_track_type type = (media_track_type)type32;
557 ssize_t selectedTrack = mSource->getSelectedTrack(type);
558
559 Parcel* reply;
560 CHECK(msg->findPointer("reply", (void**)&reply));
561 reply->writeInt32(selectedTrack);
562 }
563
564 sp<AMessage> response = new AMessage;
565 response->setInt32("err", err);
566
Lajos Molnar3f274362015-03-05 14:35:41 -0800567 sp<AReplyToken> replyID;
Robert Shih7c4f0d72014-07-09 18:53:31 -0700568 CHECK(msg->senderAwaitsResponse(&replyID));
569 response->postReply(replyID);
570 break;
571 }
572
Chong Zhangdcb89b32013-08-06 09:44:47 -0700573 case kWhatSelectTrack:
574 {
Lajos Molnar3f274362015-03-05 14:35:41 -0800575 sp<AReplyToken> replyID;
Chong Zhangdcb89b32013-08-06 09:44:47 -0700576 CHECK(msg->senderAwaitsResponse(&replyID));
577
Chong Zhang404fced2014-06-11 14:45:31 -0700578 size_t trackIndex;
579 int32_t select;
Robert Shih6ffb1fd2014-10-29 16:24:32 -0700580 int64_t timeUs;
Chong Zhang404fced2014-06-11 14:45:31 -0700581 CHECK(msg->findSize("trackIndex", &trackIndex));
582 CHECK(msg->findInt32("select", &select));
Robert Shih6ffb1fd2014-10-29 16:24:32 -0700583 CHECK(msg->findInt64("timeUs", &timeUs));
Chong Zhang404fced2014-06-11 14:45:31 -0700584
Chong Zhangdcb89b32013-08-06 09:44:47 -0700585 status_t err = INVALID_OPERATION;
Chong Zhang404fced2014-06-11 14:45:31 -0700586
587 size_t inbandTracks = 0;
Chong Zhangdcb89b32013-08-06 09:44:47 -0700588 if (mSource != NULL) {
Chong Zhang404fced2014-06-11 14:45:31 -0700589 inbandTracks = mSource->getTrackCount();
590 }
Chong Zhanga7fa1d92014-06-11 14:49:23 -0700591 size_t ccTracks = 0;
592 if (mCCDecoder != NULL) {
593 ccTracks = mCCDecoder->getTrackCount();
594 }
Chong Zhang404fced2014-06-11 14:45:31 -0700595
596 if (trackIndex < inbandTracks) {
Robert Shih6ffb1fd2014-10-29 16:24:32 -0700597 err = mSource->selectTrack(trackIndex, select, timeUs);
Robert Shihd3b0bbb2014-07-23 15:00:25 -0700598
599 if (!select && err == OK) {
600 int32_t type;
601 sp<AMessage> info = mSource->getTrackInfo(trackIndex);
602 if (info != NULL
603 && info->findInt32("type", &type)
604 && type == MEDIA_TRACK_TYPE_TIMEDTEXT) {
605 ++mTimedTextGeneration;
606 }
607 }
Chong Zhanga7fa1d92014-06-11 14:49:23 -0700608 } else {
609 trackIndex -= inbandTracks;
610
611 if (trackIndex < ccTracks) {
612 err = mCCDecoder->selectTrack(trackIndex, select);
613 }
Chong Zhangdcb89b32013-08-06 09:44:47 -0700614 }
615
616 sp<AMessage> response = new AMessage;
617 response->setInt32("err", err);
618
619 response->postReply(replyID);
620 break;
621 }
622
Andreas Huberb7c8e912012-11-27 15:02:53 -0800623 case kWhatPollDuration:
624 {
625 int32_t generation;
626 CHECK(msg->findInt32("generation", &generation));
627
628 if (generation != mPollDurationGeneration) {
629 // stale
630 break;
631 }
632
633 int64_t durationUs;
634 if (mDriver != NULL && mSource->getDuration(&durationUs) == OK) {
635 sp<NuPlayerDriver> driver = mDriver.promote();
636 if (driver != NULL) {
637 driver->notifyDuration(durationUs);
638 }
639 }
640
641 msg->post(1000000ll); // poll again in a second.
642 break;
643 }
644
Lajos Molnar1de1e252015-04-30 18:18:34 -0700645 case kWhatSetVideoSurface:
Andreas Huberf9334412010-12-15 15:17:42 -0800646 {
Andreas Huberf9334412010-12-15 15:17:42 -0800647
648 sp<RefBase> obj;
Lajos Molnar1de1e252015-04-30 18:18:34 -0700649 CHECK(msg->findObject("surface", &obj));
650 sp<Surface> surface = static_cast<Surface *>(obj.get());
Lajos Molnara81c6222015-07-10 19:17:45 -0700651
652 ALOGD("onSetVideoSurface(%p, %s video decoder)",
653 surface.get(),
Wei Jia6b7d2ed2015-08-10 15:11:54 -0700654 (mSource != NULL && mStarted && mSource->getFormat(false /* audio */) != NULL
Lajos Molnara81c6222015-07-10 19:17:45 -0700655 && mVideoDecoder != NULL) ? "have" : "no");
656
Wei Jia6b7d2ed2015-08-10 15:11:54 -0700657 // Need to check mStarted before calling mSource->getFormat because NuPlayer might
658 // be in preparing state and it could take long time.
659 // When mStarted is true, mSource must have been set.
660 if (mSource == NULL || !mStarted || mSource->getFormat(false /* audio */) == NULL
Lajos Molnara81c6222015-07-10 19:17:45 -0700661 // NOTE: mVideoDecoder's mSurface is always non-null
662 || (mVideoDecoder != NULL && mVideoDecoder->setVideoSurface(surface) == OK)) {
Lajos Molnar1de1e252015-04-30 18:18:34 -0700663 performSetSurface(surface);
Wei Jiafef808d2014-10-31 17:57:05 -0700664 break;
665 }
666
667 mDeferredActions.push_back(
668 new FlushDecoderAction(FLUSH_CMD_FLUSH /* audio */,
669 FLUSH_CMD_SHUTDOWN /* video */));
670
Lajos Molnar1de1e252015-04-30 18:18:34 -0700671 mDeferredActions.push_back(new SetSurfaceAction(surface));
James Dong0d268a32012-08-31 12:18:27 -0700672
Wei Jiac6e58412015-07-10 18:04:55 -0700673 if (obj != NULL || mAudioDecoder != NULL) {
Wei Jiafef808d2014-10-31 17:57:05 -0700674 if (mStarted) {
Andy Hung73535852014-09-05 11:42:58 -0700675 // Issue a seek to refresh the video screen only if started otherwise
676 // the extractor may not yet be started and will assert.
677 // If the video decoder is not set (perhaps audio only in this case)
678 // do not perform a seek as it is not needed.
Ronghua Wua73d9e02014-10-08 15:13:29 -0700679 int64_t currentPositionUs = 0;
680 if (getCurrentPosition(&currentPositionUs) == OK) {
681 mDeferredActions.push_back(
Wei Jia29840802015-05-15 17:11:38 -0700682 new SeekAction(currentPositionUs));
Ronghua Wua73d9e02014-10-08 15:13:29 -0700683 }
Andy Hung73535852014-09-05 11:42:58 -0700684 }
Wei Jiaac428aa2014-09-02 19:01:34 -0700685
Andreas Huber57a339c2012-12-03 11:18:00 -0800686 // If there is a new surface texture, instantiate decoders
687 // again if possible.
688 mDeferredActions.push_back(
689 new SimpleAction(&NuPlayer::performScanSources));
690 }
691
Chong Zhangf8d71772014-11-26 15:08:34 -0800692 // After a flush without shutdown, decoder is paused.
693 // Don't resume it until source seek is done, otherwise it could
Chong Zhang7137ec72014-11-12 16:41:05 -0800694 // start pulling stale data too soon.
695 mDeferredActions.push_back(
Chong Zhangf8d71772014-11-26 15:08:34 -0800696 new ResumeDecoderAction(false /* needNotify */));
Chong Zhang7137ec72014-11-12 16:41:05 -0800697
Andreas Huber57a339c2012-12-03 11:18:00 -0800698 processDeferredActions();
Andreas Huberf9334412010-12-15 15:17:42 -0800699 break;
700 }
701
702 case kWhatSetAudioSink:
703 {
Steve Block3856b092011-10-20 11:56:00 +0100704 ALOGV("kWhatSetAudioSink");
Andreas Huberf9334412010-12-15 15:17:42 -0800705
706 sp<RefBase> obj;
707 CHECK(msg->findObject("sink", &obj));
708
709 mAudioSink = static_cast<MediaPlayerBase::AudioSink *>(obj.get());
710 break;
711 }
712
713 case kWhatStart:
714 {
Steve Block3856b092011-10-20 11:56:00 +0100715 ALOGV("kWhatStart");
Wei Jia94211742014-10-28 17:09:06 -0700716 if (mStarted) {
Chong Zhang8a048332015-05-06 15:16:28 -0700717 // do not resume yet if the source is still buffering
718 if (!mPausedForBuffering) {
719 onResume();
720 }
Wei Jia94211742014-10-28 17:09:06 -0700721 } else {
722 onStart();
Lajos Molnar09524832014-07-17 14:29:51 -0700723 }
Chong Zhangefbb6192015-01-30 17:13:27 -0800724 mPausedByClient = false;
Andreas Huberf9334412010-12-15 15:17:42 -0800725 break;
726 }
727
Lajos Molnar3a474aa2015-04-24 17:10:07 -0700728 case kWhatConfigPlayback:
Wei Jia98160162015-02-04 17:01:11 -0800729 {
Lajos Molnar3a474aa2015-04-24 17:10:07 -0700730 sp<AReplyToken> replyID;
731 CHECK(msg->senderAwaitsResponse(&replyID));
732 AudioPlaybackRate rate /* sanitized */;
733 readFromAMessage(msg, &rate);
734 status_t err = OK;
Wei Jia98160162015-02-04 17:01:11 -0800735 if (mRenderer != NULL) {
Wei Jiabf70feb2016-02-19 15:47:16 -0800736 // AudioSink allows only 1.f and 0.f for offload mode.
737 // For other speed, switch to non-offload mode.
738 if (mOffloadAudio && ((rate.mSpeed != 0.f && rate.mSpeed != 1.f)
739 || rate.mPitch != 1.f)) {
740 int64_t currentPositionUs;
741 if (getCurrentPosition(&currentPositionUs) != OK) {
742 currentPositionUs = mPreviousSeekTimeUs;
743 }
744
745 // Set mPlaybackSettings so that the new audio decoder can
746 // be created correctly.
747 mPlaybackSettings = rate;
748 if (!mPaused) {
749 mRenderer->pause();
750 }
Wei Jia5031b2f2016-02-25 11:19:31 -0800751 restartAudio(
Wei Jiabf70feb2016-02-19 15:47:16 -0800752 currentPositionUs, true /* forceNonOffload */,
753 true /* needsToCreateAudioDecoder */);
754 if (!mPaused) {
755 mRenderer->resume();
756 }
757 }
758
Lajos Molnar3a474aa2015-04-24 17:10:07 -0700759 err = mRenderer->setPlaybackSettings(rate);
760 }
761 if (err == OK) {
762 if (rate.mSpeed == 0.f) {
763 onPause();
Wei Jia351ce872016-02-10 13:21:59 -0800764 mPausedByClient = true;
Lajos Molnar3a474aa2015-04-24 17:10:07 -0700765 // save all other settings (using non-paused speed)
766 // so we can restore them on start
767 AudioPlaybackRate newRate = rate;
768 newRate.mSpeed = mPlaybackSettings.mSpeed;
769 mPlaybackSettings = newRate;
770 } else { /* rate.mSpeed != 0.f */
771 onResume();
Wei Jia351ce872016-02-10 13:21:59 -0800772 mPausedByClient = false;
Lajos Molnar3a474aa2015-04-24 17:10:07 -0700773 mPlaybackSettings = rate;
774 }
Wei Jia98160162015-02-04 17:01:11 -0800775 }
Ronghua Wu8db88132015-04-22 13:51:35 -0700776
777 if (mVideoDecoder != NULL) {
Ronghua Wuc8a70d32015-04-29 16:26:34 -0700778 float rate = getFrameRate();
779 if (rate > 0) {
Ronghua Wu8db88132015-04-22 13:51:35 -0700780 sp<AMessage> params = new AMessage();
Lajos Molnar3a474aa2015-04-24 17:10:07 -0700781 params->setFloat("operating-rate", rate * mPlaybackSettings.mSpeed);
Ronghua Wu8db88132015-04-22 13:51:35 -0700782 mVideoDecoder->setParameters(params);
783 }
784 }
785
Lajos Molnar3a474aa2015-04-24 17:10:07 -0700786 sp<AMessage> response = new AMessage;
787 response->setInt32("err", err);
788 response->postReply(replyID);
789 break;
790 }
791
792 case kWhatGetPlaybackSettings:
793 {
794 sp<AReplyToken> replyID;
795 CHECK(msg->senderAwaitsResponse(&replyID));
796 AudioPlaybackRate rate = mPlaybackSettings;
797 status_t err = OK;
798 if (mRenderer != NULL) {
799 err = mRenderer->getPlaybackSettings(&rate);
800 }
801 if (err == OK) {
802 // get playback settings used by renderer, as it may be
803 // slightly off due to audiosink not taking small changes.
804 mPlaybackSettings = rate;
805 if (mPaused) {
806 rate.mSpeed = 0.f;
807 }
808 }
809 sp<AMessage> response = new AMessage;
810 if (err == OK) {
811 writeToAMessage(response, rate);
812 }
813 response->setInt32("err", err);
814 response->postReply(replyID);
815 break;
816 }
817
818 case kWhatConfigSync:
819 {
820 sp<AReplyToken> replyID;
821 CHECK(msg->senderAwaitsResponse(&replyID));
822
823 ALOGV("kWhatConfigSync");
824 AVSyncSettings sync;
825 float videoFpsHint;
826 readFromAMessage(msg, &sync, &videoFpsHint);
827 status_t err = OK;
828 if (mRenderer != NULL) {
829 err = mRenderer->setSyncSettings(sync, videoFpsHint);
830 }
831 if (err == OK) {
832 mSyncSettings = sync;
833 mVideoFpsHint = videoFpsHint;
834 }
835 sp<AMessage> response = new AMessage;
836 response->setInt32("err", err);
837 response->postReply(replyID);
838 break;
839 }
840
841 case kWhatGetSyncSettings:
842 {
843 sp<AReplyToken> replyID;
844 CHECK(msg->senderAwaitsResponse(&replyID));
845 AVSyncSettings sync = mSyncSettings;
846 float videoFps = mVideoFpsHint;
847 status_t err = OK;
848 if (mRenderer != NULL) {
849 err = mRenderer->getSyncSettings(&sync, &videoFps);
850 if (err == OK) {
851 mSyncSettings = sync;
852 mVideoFpsHint = videoFps;
853 }
854 }
855 sp<AMessage> response = new AMessage;
856 if (err == OK) {
857 writeToAMessage(response, sync, videoFps);
858 }
859 response->setInt32("err", err);
860 response->postReply(replyID);
Wei Jia98160162015-02-04 17:01:11 -0800861 break;
862 }
863
Andreas Huberf9334412010-12-15 15:17:42 -0800864 case kWhatScanSources:
865 {
Andreas Huber1aef2112011-01-04 14:01:29 -0800866 int32_t generation;
867 CHECK(msg->findInt32("generation", &generation));
868 if (generation != mScanSourcesGeneration) {
869 // Drop obsolete msg.
870 break;
871 }
872
Andreas Huber5bc087c2010-12-23 10:27:40 -0800873 mScanSourcesPending = false;
874
Steve Block3856b092011-10-20 11:56:00 +0100875 ALOGV("scanning sources haveAudio=%d, haveVideo=%d",
Andreas Huber43c3e6c2011-01-05 12:17:08 -0800876 mAudioDecoder != NULL, mVideoDecoder != NULL);
877
Andreas Huberb7c8e912012-11-27 15:02:53 -0800878 bool mHadAnySourcesBefore =
879 (mAudioDecoder != NULL) || (mVideoDecoder != NULL);
Robert Shih7350b052015-10-01 15:50:14 -0700880 bool rescan = false;
Andreas Huberb7c8e912012-11-27 15:02:53 -0800881
Andy Hung282a7e32014-08-14 15:56:34 -0700882 // initialize video before audio because successful initialization of
883 // video may change deep buffer mode of audio.
Lajos Molnar1de1e252015-04-30 18:18:34 -0700884 if (mSurface != NULL) {
Robert Shih7350b052015-10-01 15:50:14 -0700885 if (instantiateDecoder(false, &mVideoDecoder) == -EWOULDBLOCK) {
886 rescan = true;
887 }
Haynes Mathew George5d246ef2012-07-09 10:36:57 -0700888 }
Andreas Huberf9334412010-12-15 15:17:42 -0800889
Ronghua Wua10fd232014-11-06 16:15:20 -0800890 // Don't try to re-open audio sink if there's an existing decoder.
891 if (mAudioSink != NULL && mAudioDecoder == NULL) {
Robert Shih7350b052015-10-01 15:50:14 -0700892 if (instantiateDecoder(true, &mAudioDecoder) == -EWOULDBLOCK) {
893 rescan = true;
894 }
Andreas Huberf9334412010-12-15 15:17:42 -0800895 }
896
Andreas Huberb7c8e912012-11-27 15:02:53 -0800897 if (!mHadAnySourcesBefore
898 && (mAudioDecoder != NULL || mVideoDecoder != NULL)) {
899 // This is the first time we've found anything playable.
900
Andreas Huber9575c962013-02-05 13:59:56 -0800901 if (mSourceFlags & Source::FLAG_DYNAMIC_DURATION) {
Andreas Huberb7c8e912012-11-27 15:02:53 -0800902 schedulePollDuration();
903 }
904 }
905
Andreas Hubereac68ba2011-09-27 12:12:25 -0700906 status_t err;
907 if ((err = mSource->feedMoreTSData()) != OK) {
Andreas Huber1aef2112011-01-04 14:01:29 -0800908 if (mAudioDecoder == NULL && mVideoDecoder == NULL) {
909 // We're not currently decoding anything (no audio or
910 // video tracks found) and we just ran out of input data.
Andreas Hubereac68ba2011-09-27 12:12:25 -0700911
912 if (err == ERROR_END_OF_STREAM) {
913 notifyListener(MEDIA_PLAYBACK_COMPLETE, 0, 0);
914 } else {
915 notifyListener(MEDIA_ERROR, MEDIA_ERROR_UNKNOWN, err);
916 }
Andreas Huber1aef2112011-01-04 14:01:29 -0800917 }
Andreas Huberf9334412010-12-15 15:17:42 -0800918 break;
919 }
920
Robert Shih7350b052015-10-01 15:50:14 -0700921 if (rescan) {
Andreas Huberf9334412010-12-15 15:17:42 -0800922 msg->post(100000ll);
Andreas Huber5bc087c2010-12-23 10:27:40 -0800923 mScanSourcesPending = true;
Andreas Huberf9334412010-12-15 15:17:42 -0800924 }
925 break;
926 }
927
928 case kWhatVideoNotify:
929 case kWhatAudioNotify:
930 {
931 bool audio = msg->what() == kWhatAudioNotify;
932
Wei Jia88703c32014-08-06 11:24:07 -0700933 int32_t currentDecoderGeneration =
934 (audio? mAudioDecoderGeneration : mVideoDecoderGeneration);
935 int32_t requesterGeneration = currentDecoderGeneration - 1;
936 CHECK(msg->findInt32("generation", &requesterGeneration));
937
938 if (requesterGeneration != currentDecoderGeneration) {
939 ALOGV("got message from old %s decoder, generation(%d:%d)",
940 audio ? "audio" : "video", requesterGeneration,
941 currentDecoderGeneration);
942 sp<AMessage> reply;
943 if (!(msg->findMessage("reply", &reply))) {
944 return;
945 }
946
947 reply->setInt32("err", INFO_DISCONTINUITY);
948 reply->post();
949 return;
950 }
951
Andreas Huberf9334412010-12-15 15:17:42 -0800952 int32_t what;
Lajos Molnar1cd13982014-01-17 15:12:51 -0800953 CHECK(msg->findInt32("what", &what));
Andreas Huberf9334412010-12-15 15:17:42 -0800954
Chong Zhang7137ec72014-11-12 16:41:05 -0800955 if (what == DecoderBase::kWhatInputDiscontinuity) {
956 int32_t formatChange;
957 CHECK(msg->findInt32("formatChange", &formatChange));
Andreas Huberf9334412010-12-15 15:17:42 -0800958
Chong Zhang7137ec72014-11-12 16:41:05 -0800959 ALOGV("%s discontinuity: formatChange %d",
960 audio ? "audio" : "video", formatChange);
961
962 if (formatChange) {
963 mDeferredActions.push_back(
964 new FlushDecoderAction(
965 audio ? FLUSH_CMD_SHUTDOWN : FLUSH_CMD_NONE,
966 audio ? FLUSH_CMD_NONE : FLUSH_CMD_SHUTDOWN));
Andreas Huberf9334412010-12-15 15:17:42 -0800967 }
Chong Zhang7137ec72014-11-12 16:41:05 -0800968
969 mDeferredActions.push_back(
970 new SimpleAction(
971 &NuPlayer::performScanSources));
972
973 processDeferredActions();
974 } else if (what == DecoderBase::kWhatEOS) {
Andreas Huberdc9bacd2011-09-26 10:53:29 -0700975 int32_t err;
Lajos Molnar1cd13982014-01-17 15:12:51 -0800976 CHECK(msg->findInt32("err", &err));
Andreas Huberdc9bacd2011-09-26 10:53:29 -0700977
978 if (err == ERROR_END_OF_STREAM) {
Steve Block3856b092011-10-20 11:56:00 +0100979 ALOGV("got %s decoder EOS", audio ? "audio" : "video");
Andreas Huberdc9bacd2011-09-26 10:53:29 -0700980 } else {
Steve Block3856b092011-10-20 11:56:00 +0100981 ALOGV("got %s decoder EOS w/ error %d",
Andreas Huberdc9bacd2011-09-26 10:53:29 -0700982 audio ? "audio" : "video",
983 err);
984 }
985
986 mRenderer->queueEOS(audio, err);
Chong Zhang7137ec72014-11-12 16:41:05 -0800987 } else if (what == DecoderBase::kWhatFlushCompleted) {
Steve Block3856b092011-10-20 11:56:00 +0100988 ALOGV("decoder %s flush completed", audio ? "audio" : "video");
Andreas Huberf9334412010-12-15 15:17:42 -0800989
Andy Hung8d121d42014-10-03 09:53:53 -0700990 handleFlushComplete(audio, true /* isDecoder */);
Andreas Huber3831a062010-12-21 10:22:33 -0800991 finishFlushIfPossible();
Chong Zhang7137ec72014-11-12 16:41:05 -0800992 } else if (what == DecoderBase::kWhatVideoSizeChanged) {
Lajos Molnar1cd13982014-01-17 15:12:51 -0800993 sp<AMessage> format;
994 CHECK(msg->findMessage("format", &format));
995
Wei Jiac6cfd702014-11-11 16:33:20 -0800996 sp<AMessage> inputFormat =
997 mSource->getFormat(false /* audio */);
Andreas Huber3831a062010-12-21 10:22:33 -0800998
Wei Jiac6cfd702014-11-11 16:33:20 -0800999 updateVideoSize(inputFormat, format);
Chong Zhang7137ec72014-11-12 16:41:05 -08001000 } else if (what == DecoderBase::kWhatShutdownCompleted) {
Steve Block3856b092011-10-20 11:56:00 +01001001 ALOGV("%s shutdown completed", audio ? "audio" : "video");
Andreas Huber3831a062010-12-21 10:22:33 -08001002 if (audio) {
1003 mAudioDecoder.clear();
Wei Jia57568df2014-09-22 10:16:29 -07001004 ++mAudioDecoderGeneration;
Andreas Huber3831a062010-12-21 10:22:33 -08001005
1006 CHECK_EQ((int)mFlushingAudio, (int)SHUTTING_DOWN_DECODER);
1007 mFlushingAudio = SHUT_DOWN;
1008 } else {
1009 mVideoDecoder.clear();
Wei Jia57568df2014-09-22 10:16:29 -07001010 ++mVideoDecoderGeneration;
Andreas Huber3831a062010-12-21 10:22:33 -08001011
1012 CHECK_EQ((int)mFlushingVideo, (int)SHUTTING_DOWN_DECODER);
1013 mFlushingVideo = SHUT_DOWN;
1014 }
1015
1016 finishFlushIfPossible();
Chong Zhangf8d71772014-11-26 15:08:34 -08001017 } else if (what == DecoderBase::kWhatResumeCompleted) {
1018 finishResume();
Chong Zhang7137ec72014-11-12 16:41:05 -08001019 } else if (what == DecoderBase::kWhatError) {
Chong Zhangf4c0a942014-08-11 15:14:10 -07001020 status_t err;
Andy Hung2abde2c2014-09-30 14:40:32 -07001021 if (!msg->findInt32("err", &err) || err == OK) {
Chong Zhangf4c0a942014-08-11 15:14:10 -07001022 err = UNKNOWN_ERROR;
1023 }
Andy Hungcf31f1e2014-09-23 14:59:01 -07001024
Andy Hung2abde2c2014-09-30 14:40:32 -07001025 // Decoder errors can be due to Source (e.g. from streaming),
1026 // or from decoding corrupted bitstreams, or from other decoder
1027 // MediaCodec operations (e.g. from an ongoing reset or seek).
Andy Hung202bce12014-12-03 11:47:36 -08001028 // They may also be due to openAudioSink failure at
1029 // decoder start or after a format change.
Andy Hung2abde2c2014-09-30 14:40:32 -07001030 //
1031 // We try to gracefully shut down the affected decoder if possible,
1032 // rather than trying to force the shutdown with something
1033 // similar to performReset(). This method can lead to a hang
1034 // if MediaCodec functions block after an error, but they should
1035 // typically return INVALID_OPERATION instead of blocking.
1036
1037 FlushStatus *flushing = audio ? &mFlushingAudio : &mFlushingVideo;
1038 ALOGE("received error(%#x) from %s decoder, flushing(%d), now shutting down",
1039 err, audio ? "audio" : "video", *flushing);
1040
1041 switch (*flushing) {
1042 case NONE:
1043 mDeferredActions.push_back(
Wei Jiafef808d2014-10-31 17:57:05 -07001044 new FlushDecoderAction(
1045 audio ? FLUSH_CMD_SHUTDOWN : FLUSH_CMD_NONE,
1046 audio ? FLUSH_CMD_NONE : FLUSH_CMD_SHUTDOWN));
Andy Hung2abde2c2014-09-30 14:40:32 -07001047 processDeferredActions();
1048 break;
1049 case FLUSHING_DECODER:
1050 *flushing = FLUSHING_DECODER_SHUTDOWN; // initiate shutdown after flush.
1051 break; // Wait for flush to complete.
1052 case FLUSHING_DECODER_SHUTDOWN:
1053 break; // Wait for flush to complete.
1054 case SHUTTING_DOWN_DECODER:
1055 break; // Wait for shutdown to complete.
1056 case FLUSHED:
1057 // Widevine source reads must stop before releasing the video decoder.
1058 if (!audio && mSource != NULL && mSourceFlags & Source::FLAG_SECURE) {
1059 mSource->stop();
Robert Shih0c61a0d2015-07-06 15:09:10 -07001060 mSourceStarted = false;
Andy Hung2abde2c2014-09-30 14:40:32 -07001061 }
1062 getDecoder(audio)->initiateShutdown(); // In the middle of a seek.
1063 *flushing = SHUTTING_DOWN_DECODER; // Shut down.
1064 break;
1065 case SHUT_DOWN:
1066 finishFlushIfPossible(); // Should not occur.
1067 break; // Finish anyways.
Marco Nelissen9e2b7912014-08-18 16:13:03 -07001068 }
Andy Hung2abde2c2014-09-30 14:40:32 -07001069 notifyListener(MEDIA_ERROR, MEDIA_ERROR_UNKNOWN, err);
Lajos Molnar1cd13982014-01-17 15:12:51 -08001070 } else {
1071 ALOGV("Unhandled decoder notification %d '%c%c%c%c'.",
Andreas Hubera1f8ab02012-11-30 10:53:22 -08001072 what,
1073 what >> 24,
1074 (what >> 16) & 0xff,
1075 (what >> 8) & 0xff,
1076 what & 0xff);
Andreas Huberf9334412010-12-15 15:17:42 -08001077 }
1078
1079 break;
1080 }
1081
1082 case kWhatRendererNotify:
1083 {
Wei Jia57568df2014-09-22 10:16:29 -07001084 int32_t requesterGeneration = mRendererGeneration - 1;
1085 CHECK(msg->findInt32("generation", &requesterGeneration));
1086 if (requesterGeneration != mRendererGeneration) {
1087 ALOGV("got message from old renderer, generation(%d:%d)",
1088 requesterGeneration, mRendererGeneration);
1089 return;
1090 }
1091
Andreas Huberf9334412010-12-15 15:17:42 -08001092 int32_t what;
1093 CHECK(msg->findInt32("what", &what));
1094
1095 if (what == Renderer::kWhatEOS) {
1096 int32_t audio;
1097 CHECK(msg->findInt32("audio", &audio));
1098
Andreas Huberc92fd242011-08-16 13:48:44 -07001099 int32_t finalResult;
1100 CHECK(msg->findInt32("finalResult", &finalResult));
1101
Andreas Huberf9334412010-12-15 15:17:42 -08001102 if (audio) {
1103 mAudioEOS = true;
1104 } else {
1105 mVideoEOS = true;
1106 }
1107
Andreas Huberc92fd242011-08-16 13:48:44 -07001108 if (finalResult == ERROR_END_OF_STREAM) {
Steve Block3856b092011-10-20 11:56:00 +01001109 ALOGV("reached %s EOS", audio ? "audio" : "video");
Andreas Huberc92fd242011-08-16 13:48:44 -07001110 } else {
Steve Block29357bc2012-01-06 19:20:56 +00001111 ALOGE("%s track encountered an error (%d)",
Andreas Huberc92fd242011-08-16 13:48:44 -07001112 audio ? "audio" : "video", finalResult);
1113
1114 notifyListener(
1115 MEDIA_ERROR, MEDIA_ERROR_UNKNOWN, finalResult);
1116 }
Andreas Huberf9334412010-12-15 15:17:42 -08001117
1118 if ((mAudioEOS || mAudioDecoder == NULL)
1119 && (mVideoEOS || mVideoDecoder == NULL)) {
1120 notifyListener(MEDIA_PLAYBACK_COMPLETE, 0, 0);
1121 }
Andreas Huber3fe62152011-09-16 15:09:22 -07001122 } else if (what == Renderer::kWhatFlushComplete) {
Andreas Huberf9334412010-12-15 15:17:42 -08001123 int32_t audio;
1124 CHECK(msg->findInt32("audio", &audio));
1125
Wei Jia3261f0d2015-10-08 13:58:33 -07001126 if (audio) {
1127 mAudioEOS = false;
1128 } else {
1129 mVideoEOS = false;
1130 }
1131
Steve Block3856b092011-10-20 11:56:00 +01001132 ALOGV("renderer %s flush completed.", audio ? "audio" : "video");
Wei Jiada4252f2015-07-14 18:15:28 -07001133 if (audio && (mFlushingAudio == NONE || mFlushingAudio == FLUSHED
1134 || mFlushingAudio == SHUT_DOWN)) {
1135 // Flush has been handled by tear down.
1136 break;
1137 }
Andy Hung8d121d42014-10-03 09:53:53 -07001138 handleFlushComplete(audio, false /* isDecoder */);
1139 finishFlushIfPossible();
James Dongf57b4ea2012-07-20 13:38:36 -07001140 } else if (what == Renderer::kWhatVideoRenderingStart) {
1141 notifyListener(MEDIA_INFO, MEDIA_INFO_RENDERING_START, 0);
Lajos Molnarcbaffcf2013-08-14 18:30:38 -07001142 } else if (what == Renderer::kWhatMediaRenderingStart) {
1143 ALOGV("media rendering started");
1144 notifyListener(MEDIA_STARTED, 0, 0);
Ronghua Wufaeb0f22015-05-21 12:20:21 -07001145 } else if (what == Renderer::kWhatAudioTearDown) {
Ronghua Wu08529172014-10-02 16:55:52 -07001146 int32_t reason;
1147 CHECK(msg->findInt32("reason", &reason));
Ronghua Wufaeb0f22015-05-21 12:20:21 -07001148 ALOGV("Tear down audio with reason %d.", reason);
Wei Jia8456ddd2016-04-22 14:46:43 -07001149 if (reason == Renderer::kDueToTimeout && !(mPaused && mOffloadAudio)) {
1150 // TimeoutWhenPaused is only for offload mode.
1151 ALOGW("Receive a stale message for teardown.");
1152 break;
1153 }
Wei Jiada4252f2015-07-14 18:15:28 -07001154 int64_t positionUs;
Robert Shih1a5c8592015-08-04 18:07:44 -07001155 if (!msg->findInt64("positionUs", &positionUs)) {
1156 positionUs = mPreviousSeekTimeUs;
1157 }
Ronghua Wufaeb0f22015-05-21 12:20:21 -07001158
Wei Jia5031b2f2016-02-25 11:19:31 -08001159 restartAudio(
Wei Jiaa05f1e32016-03-25 16:31:22 -07001160 positionUs, reason == Renderer::kForceNonOffload /* forceNonOffload */,
1161 reason != Renderer::kDueToTimeout /* needsToCreateAudioDecoder */);
Andreas Huberf9334412010-12-15 15:17:42 -08001162 }
1163 break;
1164 }
1165
1166 case kWhatMoreDataQueued:
1167 {
1168 break;
1169 }
1170
Andreas Huber1aef2112011-01-04 14:01:29 -08001171 case kWhatReset:
1172 {
Steve Block3856b092011-10-20 11:56:00 +01001173 ALOGV("kWhatReset");
Andreas Huber1aef2112011-01-04 14:01:29 -08001174
Ronghua Wu64c2d172015-10-07 16:52:19 -07001175 mResetting = true;
1176
Andreas Hubera1f8ab02012-11-30 10:53:22 -08001177 mDeferredActions.push_back(
Wei Jiafef808d2014-10-31 17:57:05 -07001178 new FlushDecoderAction(
1179 FLUSH_CMD_SHUTDOWN /* audio */,
1180 FLUSH_CMD_SHUTDOWN /* video */));
Andreas Huberb7c8e912012-11-27 15:02:53 -08001181
Andreas Hubera1f8ab02012-11-30 10:53:22 -08001182 mDeferredActions.push_back(
1183 new SimpleAction(&NuPlayer::performReset));
Andreas Huberb58ce9f2011-11-28 16:27:35 -08001184
Andreas Hubera1f8ab02012-11-30 10:53:22 -08001185 processDeferredActions();
Andreas Huber1aef2112011-01-04 14:01:29 -08001186 break;
1187 }
1188
Andreas Huber43c3e6c2011-01-05 12:17:08 -08001189 case kWhatSeek:
1190 {
1191 int64_t seekTimeUs;
Wei Jiae427abf2014-09-22 15:21:11 -07001192 int32_t needNotify;
Andreas Huber43c3e6c2011-01-05 12:17:08 -08001193 CHECK(msg->findInt64("seekTimeUs", &seekTimeUs));
Wei Jiae427abf2014-09-22 15:21:11 -07001194 CHECK(msg->findInt32("needNotify", &needNotify));
Andreas Huber43c3e6c2011-01-05 12:17:08 -08001195
Wei Jiae427abf2014-09-22 15:21:11 -07001196 ALOGV("kWhatSeek seekTimeUs=%lld us, needNotify=%d",
Lajos Molnar6d339f12015-04-17 16:15:53 -07001197 (long long)seekTimeUs, needNotify);
Andreas Huber43c3e6c2011-01-05 12:17:08 -08001198
Wei Jia1061c9c2015-05-19 16:02:17 -07001199 if (!mStarted) {
1200 // Seek before the player is started. In order to preview video,
1201 // need to start the player and pause it. This branch is called
1202 // only once if needed. After the player is started, any seek
1203 // operation will go through normal path.
Robert Shih0c61a0d2015-07-06 15:09:10 -07001204 // Audio-only cases are handled separately.
Wei Jia1061c9c2015-05-19 16:02:17 -07001205 onStart(seekTimeUs);
Robert Shih0c61a0d2015-07-06 15:09:10 -07001206 if (mStarted) {
1207 onPause();
1208 mPausedByClient = true;
1209 }
Wei Jia1061c9c2015-05-19 16:02:17 -07001210 if (needNotify) {
1211 notifyDriverSeekComplete();
1212 }
1213 break;
1214 }
1215
Andreas Hubera1f8ab02012-11-30 10:53:22 -08001216 mDeferredActions.push_back(
Wei Jiafef808d2014-10-31 17:57:05 -07001217 new FlushDecoderAction(FLUSH_CMD_FLUSH /* audio */,
1218 FLUSH_CMD_FLUSH /* video */));
Andreas Huber43c3e6c2011-01-05 12:17:08 -08001219
Wei Jiae427abf2014-09-22 15:21:11 -07001220 mDeferredActions.push_back(
Wei Jia29840802015-05-15 17:11:38 -07001221 new SeekAction(seekTimeUs));
Andreas Huber43c3e6c2011-01-05 12:17:08 -08001222
Chong Zhangf8d71772014-11-26 15:08:34 -08001223 // After a flush without shutdown, decoder is paused.
1224 // Don't resume it until source seek is done, otherwise it could
Chong Zhang7137ec72014-11-12 16:41:05 -08001225 // start pulling stale data too soon.
1226 mDeferredActions.push_back(
Chong Zhangf8d71772014-11-26 15:08:34 -08001227 new ResumeDecoderAction(needNotify));
Chong Zhang7137ec72014-11-12 16:41:05 -08001228
Andreas Hubera1f8ab02012-11-30 10:53:22 -08001229 processDeferredActions();
Andreas Huber43c3e6c2011-01-05 12:17:08 -08001230 break;
1231 }
1232
Andreas Huberb4082222011-01-20 15:23:04 -08001233 case kWhatPause:
1234 {
Chong Zhangefbb6192015-01-30 17:13:27 -08001235 onPause();
1236 mPausedByClient = true;
Andreas Huberb4082222011-01-20 15:23:04 -08001237 break;
1238 }
1239
Andreas Huberb5f25f02013-02-05 10:14:26 -08001240 case kWhatSourceNotify:
1241 {
Andreas Huber9575c962013-02-05 13:59:56 -08001242 onSourceNotify(msg);
Andreas Huberb5f25f02013-02-05 10:14:26 -08001243 break;
1244 }
1245
Chong Zhanga7fa1d92014-06-11 14:49:23 -07001246 case kWhatClosedCaptionNotify:
1247 {
1248 onClosedCaptionNotify(msg);
1249 break;
1250 }
1251
Andreas Huberf9334412010-12-15 15:17:42 -08001252 default:
1253 TRESPASS();
1254 break;
1255 }
1256}
1257
Wei Jia94211742014-10-28 17:09:06 -07001258void NuPlayer::onResume() {
Ronghua Wu64c2d172015-10-07 16:52:19 -07001259 if (!mPaused || mResetting) {
1260 ALOGD_IF(mResetting, "resetting, onResume discarded");
Chong Zhangefbb6192015-01-30 17:13:27 -08001261 return;
1262 }
1263 mPaused = false;
Wei Jia94211742014-10-28 17:09:06 -07001264 if (mSource != NULL) {
1265 mSource->resume();
1266 } else {
1267 ALOGW("resume called when source is gone or not set");
1268 }
1269 // |mAudioDecoder| may have been released due to the pause timeout, so re-create it if
1270 // needed.
1271 if (audioDecoderStillNeeded() && mAudioDecoder == NULL) {
1272 instantiateDecoder(true /* audio */, &mAudioDecoder);
1273 }
1274 if (mRenderer != NULL) {
1275 mRenderer->resume();
1276 } else {
1277 ALOGW("resume called when renderer is gone or not set");
1278 }
1279}
1280
Lajos Molnarfcd3e942015-03-31 10:06:48 -07001281status_t NuPlayer::onInstantiateSecureDecoders() {
1282 status_t err;
1283 if (!(mSourceFlags & Source::FLAG_SECURE)) {
1284 return BAD_TYPE;
1285 }
1286
1287 if (mRenderer != NULL) {
1288 ALOGE("renderer should not be set when instantiating secure decoders");
1289 return UNKNOWN_ERROR;
1290 }
1291
1292 // TRICKY: We rely on mRenderer being null, so that decoder does not start requesting
1293 // data on instantiation.
Lajos Molnar1de1e252015-04-30 18:18:34 -07001294 if (mSurface != NULL) {
Lajos Molnarfcd3e942015-03-31 10:06:48 -07001295 err = instantiateDecoder(false, &mVideoDecoder);
1296 if (err != OK) {
1297 return err;
1298 }
1299 }
1300
1301 if (mAudioSink != NULL) {
1302 err = instantiateDecoder(true, &mAudioDecoder);
1303 if (err != OK) {
1304 return err;
1305 }
1306 }
1307 return OK;
1308}
1309
Wei Jia1061c9c2015-05-19 16:02:17 -07001310void NuPlayer::onStart(int64_t startPositionUs) {
Robert Shih0c61a0d2015-07-06 15:09:10 -07001311 if (!mSourceStarted) {
1312 mSourceStarted = true;
1313 mSource->start();
1314 }
1315 if (startPositionUs > 0) {
1316 performSeek(startPositionUs);
1317 if (mSource->getFormat(false /* audio */) == NULL) {
1318 return;
1319 }
1320 }
1321
Wei Jia94211742014-10-28 17:09:06 -07001322 mOffloadAudio = false;
1323 mAudioEOS = false;
1324 mVideoEOS = false;
Wei Jia94211742014-10-28 17:09:06 -07001325 mStarted = true;
1326
Wei Jia94211742014-10-28 17:09:06 -07001327 uint32_t flags = 0;
1328
1329 if (mSource->isRealTime()) {
1330 flags |= Renderer::FLAG_REAL_TIME;
1331 }
1332
1333 sp<MetaData> audioMeta = mSource->getFormatMeta(true /* audio */);
Andy Hung12a84132015-10-20 16:09:21 -07001334 ALOGV_IF(audioMeta == NULL, "no metadata for audio source"); // video only stream
Wei Jia94211742014-10-28 17:09:06 -07001335 audio_stream_type_t streamType = AUDIO_STREAM_MUSIC;
1336 if (mAudioSink != NULL) {
1337 streamType = mAudioSink->getAudioStreamType();
1338 }
1339
1340 sp<AMessage> videoFormat = mSource->getFormat(false /* audio */);
1341
1342 mOffloadAudio =
Wei Jiabf70feb2016-02-19 15:47:16 -08001343 canOffloadStream(audioMeta, (videoFormat != NULL), mSource->isStreaming(), streamType)
1344 && (mPlaybackSettings.mSpeed == 1.f && mPlaybackSettings.mPitch == 1.f);
Wei Jia94211742014-10-28 17:09:06 -07001345 if (mOffloadAudio) {
1346 flags |= Renderer::FLAG_OFFLOAD_AUDIO;
1347 }
1348
Lajos Molnar1d15ab52015-03-04 16:46:34 -08001349 sp<AMessage> notify = new AMessage(kWhatRendererNotify, this);
Wei Jia94211742014-10-28 17:09:06 -07001350 ++mRendererGeneration;
1351 notify->setInt32("generation", mRendererGeneration);
1352 mRenderer = new Renderer(mAudioSink, notify, flags);
Wei Jia94211742014-10-28 17:09:06 -07001353 mRendererLooper = new ALooper;
1354 mRendererLooper->setName("NuPlayerRenderer");
1355 mRendererLooper->start(false, false, ANDROID_PRIORITY_AUDIO);
1356 mRendererLooper->registerHandler(mRenderer);
Lajos Molnar3a474aa2015-04-24 17:10:07 -07001357
1358 status_t err = mRenderer->setPlaybackSettings(mPlaybackSettings);
1359 if (err != OK) {
1360 mSource->stop();
Robert Shih0c61a0d2015-07-06 15:09:10 -07001361 mSourceStarted = false;
Lajos Molnar3a474aa2015-04-24 17:10:07 -07001362 notifyListener(MEDIA_ERROR, MEDIA_ERROR_UNKNOWN, err);
1363 return;
Wei Jiac8206ff2015-03-04 13:59:37 -08001364 }
Wei Jia94211742014-10-28 17:09:06 -07001365
Ronghua Wuc8a70d32015-04-29 16:26:34 -07001366 float rate = getFrameRate();
1367 if (rate > 0) {
Wei Jia94211742014-10-28 17:09:06 -07001368 mRenderer->setVideoFrameRate(rate);
1369 }
1370
Wei Jiac6cfd702014-11-11 16:33:20 -08001371 if (mVideoDecoder != NULL) {
1372 mVideoDecoder->setRenderer(mRenderer);
1373 }
1374 if (mAudioDecoder != NULL) {
1375 mAudioDecoder->setRenderer(mRenderer);
1376 }
1377
Wei Jia94211742014-10-28 17:09:06 -07001378 postScanSources();
1379}
1380
Chong Zhangefbb6192015-01-30 17:13:27 -08001381void NuPlayer::onPause() {
1382 if (mPaused) {
1383 return;
1384 }
1385 mPaused = true;
1386 if (mSource != NULL) {
1387 mSource->pause();
1388 } else {
1389 ALOGW("pause called when source is gone or not set");
1390 }
1391 if (mRenderer != NULL) {
1392 mRenderer->pause();
1393 } else {
1394 ALOGW("pause called when renderer is gone or not set");
1395 }
1396}
1397
Ronghua Wud7988b12014-10-03 15:19:10 -07001398bool NuPlayer::audioDecoderStillNeeded() {
1399 // Audio decoder is no longer needed if it's in shut/shutting down status.
1400 return ((mFlushingAudio != SHUT_DOWN) && (mFlushingAudio != SHUTTING_DOWN_DECODER));
1401}
1402
Andy Hung8d121d42014-10-03 09:53:53 -07001403void NuPlayer::handleFlushComplete(bool audio, bool isDecoder) {
1404 // We wait for both the decoder flush and the renderer flush to complete
1405 // before entering either the FLUSHED or the SHUTTING_DOWN_DECODER state.
1406
1407 mFlushComplete[audio][isDecoder] = true;
1408 if (!mFlushComplete[audio][!isDecoder]) {
1409 return;
1410 }
1411
1412 FlushStatus *state = audio ? &mFlushingAudio : &mFlushingVideo;
1413 switch (*state) {
1414 case FLUSHING_DECODER:
1415 {
1416 *state = FLUSHED;
Andy Hung8d121d42014-10-03 09:53:53 -07001417 break;
1418 }
1419
1420 case FLUSHING_DECODER_SHUTDOWN:
1421 {
1422 *state = SHUTTING_DOWN_DECODER;
1423
1424 ALOGV("initiating %s decoder shutdown", audio ? "audio" : "video");
1425 if (!audio) {
Andy Hung8d121d42014-10-03 09:53:53 -07001426 // Widevine source reads must stop before releasing the video decoder.
1427 if (mSource != NULL && mSourceFlags & Source::FLAG_SECURE) {
1428 mSource->stop();
Robert Shih0c61a0d2015-07-06 15:09:10 -07001429 mSourceStarted = false;
Andy Hung8d121d42014-10-03 09:53:53 -07001430 }
1431 }
1432 getDecoder(audio)->initiateShutdown();
1433 break;
1434 }
1435
1436 default:
1437 // decoder flush completes only occur in a flushing state.
1438 LOG_ALWAYS_FATAL_IF(isDecoder, "decoder flush in invalid state %d", *state);
1439 break;
1440 }
1441}
1442
Andreas Huber3831a062010-12-21 10:22:33 -08001443void NuPlayer::finishFlushIfPossible() {
Wei Jia53904f32014-07-29 10:22:53 -07001444 if (mFlushingAudio != NONE && mFlushingAudio != FLUSHED
1445 && mFlushingAudio != SHUT_DOWN) {
Andreas Huber3831a062010-12-21 10:22:33 -08001446 return;
1447 }
1448
Wei Jia53904f32014-07-29 10:22:53 -07001449 if (mFlushingVideo != NONE && mFlushingVideo != FLUSHED
1450 && mFlushingVideo != SHUT_DOWN) {
Andreas Huber3831a062010-12-21 10:22:33 -08001451 return;
1452 }
1453
Steve Block3856b092011-10-20 11:56:00 +01001454 ALOGV("both audio and video are flushed now.");
Andreas Huber3831a062010-12-21 10:22:33 -08001455
Andreas Huber3831a062010-12-21 10:22:33 -08001456 mFlushingAudio = NONE;
1457 mFlushingVideo = NONE;
Andreas Huber3831a062010-12-21 10:22:33 -08001458
Andy Hung8d121d42014-10-03 09:53:53 -07001459 clearFlushComplete();
1460
Andreas Hubera1f8ab02012-11-30 10:53:22 -08001461 processDeferredActions();
Andreas Huber1aef2112011-01-04 14:01:29 -08001462}
1463
1464void NuPlayer::postScanSources() {
1465 if (mScanSourcesPending) {
1466 return;
1467 }
1468
Lajos Molnar1d15ab52015-03-04 16:46:34 -08001469 sp<AMessage> msg = new AMessage(kWhatScanSources, this);
Andreas Huber1aef2112011-01-04 14:01:29 -08001470 msg->setInt32("generation", mScanSourcesGeneration);
1471 msg->post();
1472
1473 mScanSourcesPending = true;
1474}
1475
Wei Jia41cd4632016-05-13 10:53:30 -07001476void NuPlayer::tryOpenAudioSinkForOffload(
1477 const sp<AMessage> &format, const sp<MetaData> &audioMeta, bool hasVideo) {
Andy Hung202bce12014-12-03 11:47:36 -08001478 // Note: This is called early in NuPlayer to determine whether offloading
1479 // is possible; otherwise the decoders call the renderer openAudioSink directly.
Andy Hung282a7e32014-08-14 15:56:34 -07001480
Andy Hung202bce12014-12-03 11:47:36 -08001481 status_t err = mRenderer->openAudioSink(
1482 format, true /* offloadOnly */, hasVideo, AUDIO_OUTPUT_FLAG_NONE, &mOffloadAudio);
1483 if (err != OK) {
1484 // Any failure we turn off mOffloadAudio.
1485 mOffloadAudio = false;
1486 } else if (mOffloadAudio) {
Chong Zhang3b9eb1f2014-10-15 17:05:08 -07001487 sendMetaDataToHal(mAudioSink, audioMeta);
Andy Hung282a7e32014-08-14 15:56:34 -07001488 }
1489}
1490
1491void NuPlayer::closeAudioSink() {
Chong Zhang3b9eb1f2014-10-15 17:05:08 -07001492 mRenderer->closeAudioSink();
Andy Hung282a7e32014-08-14 15:56:34 -07001493}
1494
Wei Jia5031b2f2016-02-25 11:19:31 -08001495void NuPlayer::restartAudio(
Wei Jiabf70feb2016-02-19 15:47:16 -08001496 int64_t currentPositionUs, bool forceNonOffload, bool needsToCreateAudioDecoder) {
Wei Jiaa05f1e32016-03-25 16:31:22 -07001497 if (mAudioDecoder != NULL) {
1498 mAudioDecoder->pause();
1499 mAudioDecoder.clear();
1500 ++mAudioDecoderGeneration;
1501 }
Wei Jiabf70feb2016-02-19 15:47:16 -08001502 if (mFlushingAudio == FLUSHING_DECODER) {
1503 mFlushComplete[1 /* audio */][1 /* isDecoder */] = true;
1504 mFlushingAudio = FLUSHED;
1505 finishFlushIfPossible();
1506 } else if (mFlushingAudio == FLUSHING_DECODER_SHUTDOWN
1507 || mFlushingAudio == SHUTTING_DOWN_DECODER) {
1508 mFlushComplete[1 /* audio */][1 /* isDecoder */] = true;
1509 mFlushingAudio = SHUT_DOWN;
1510 finishFlushIfPossible();
1511 needsToCreateAudioDecoder = false;
1512 }
1513 if (mRenderer == NULL) {
1514 return;
1515 }
1516 closeAudioSink();
1517 mRenderer->flush(true /* audio */, false /* notifyComplete */);
1518 if (mVideoDecoder != NULL) {
1519 mRenderer->flush(false /* audio */, false /* notifyComplete */);
1520 }
1521
1522 performSeek(currentPositionUs);
1523
1524 if (forceNonOffload) {
1525 mRenderer->signalDisableOffloadAudio();
1526 mOffloadAudio = false;
1527 }
1528 if (needsToCreateAudioDecoder) {
Wei Jiaa05f1e32016-03-25 16:31:22 -07001529 instantiateDecoder(true /* audio */, &mAudioDecoder, !forceNonOffload);
Wei Jiabf70feb2016-02-19 15:47:16 -08001530 }
1531}
1532
Wei Jia41cd4632016-05-13 10:53:30 -07001533void NuPlayer::determineAudioModeChange(const sp<AMessage> &audioFormat) {
Wei Jiae4d18c72015-07-13 17:58:11 -07001534 if (mSource == NULL || mAudioSink == NULL) {
1535 return;
1536 }
1537
1538 if (mRenderer == NULL) {
1539 ALOGW("No renderer can be used to determine audio mode. Use non-offload for safety.");
1540 mOffloadAudio = false;
1541 return;
1542 }
1543
1544 sp<MetaData> audioMeta = mSource->getFormatMeta(true /* audio */);
1545 sp<AMessage> videoFormat = mSource->getFormat(false /* audio */);
1546 audio_stream_type_t streamType = mAudioSink->getAudioStreamType();
1547 const bool hasVideo = (videoFormat != NULL);
1548 const bool canOffload = canOffloadStream(
Wei Jiabf70feb2016-02-19 15:47:16 -08001549 audioMeta, hasVideo, mSource->isStreaming(), streamType)
1550 && (mPlaybackSettings.mSpeed == 1.f && mPlaybackSettings.mPitch == 1.f);
Wei Jiae4d18c72015-07-13 17:58:11 -07001551 if (canOffload) {
1552 if (!mOffloadAudio) {
1553 mRenderer->signalEnableOffloadAudio();
1554 }
1555 // open audio sink early under offload mode.
Wei Jia41cd4632016-05-13 10:53:30 -07001556 tryOpenAudioSinkForOffload(audioFormat, audioMeta, hasVideo);
Wei Jiae4d18c72015-07-13 17:58:11 -07001557 } else {
Robert Shihe1d70192015-07-23 17:54:13 -07001558 if (mOffloadAudio) {
1559 mRenderer->signalDisableOffloadAudio();
1560 mOffloadAudio = false;
1561 }
Wei Jiae4d18c72015-07-13 17:58:11 -07001562 }
1563}
1564
Wei Jiaa05f1e32016-03-25 16:31:22 -07001565status_t NuPlayer::instantiateDecoder(
1566 bool audio, sp<DecoderBase> *decoder, bool checkAudioModeChange) {
Wei Jia566da802015-08-27 13:59:30 -07001567 // The audio decoder could be cleared by tear down. If still in shut down
1568 // process, no need to create a new audio decoder.
1569 if (*decoder != NULL || (audio && mFlushingAudio == SHUT_DOWN)) {
Andreas Huberf9334412010-12-15 15:17:42 -08001570 return OK;
1571 }
1572
Andreas Huber84066782011-08-16 09:34:26 -07001573 sp<AMessage> format = mSource->getFormat(audio);
Andreas Huberf9334412010-12-15 15:17:42 -08001574
Andreas Huber84066782011-08-16 09:34:26 -07001575 if (format == NULL) {
Robert Shih7350b052015-10-01 15:50:14 -07001576 return UNKNOWN_ERROR;
1577 } else {
1578 status_t err;
1579 if (format->findInt32("err", &err) && err) {
1580 return err;
1581 }
Andreas Huberf9334412010-12-15 15:17:42 -08001582 }
1583
Ronghua Wu8db88132015-04-22 13:51:35 -07001584 format->setInt32("priority", 0 /* realtime */);
1585
Andreas Huber3fe62152011-09-16 15:09:22 -07001586 if (!audio) {
Andreas Huber84066782011-08-16 09:34:26 -07001587 AString mime;
1588 CHECK(format->findString("mime", &mime));
Chong Zhanga7fa1d92014-06-11 14:49:23 -07001589
Lajos Molnar1d15ab52015-03-04 16:46:34 -08001590 sp<AMessage> ccNotify = new AMessage(kWhatClosedCaptionNotify, this);
Chong Zhang341ab6e2015-02-04 13:37:18 -08001591 if (mCCDecoder == NULL) {
1592 mCCDecoder = new CCDecoder(ccNotify);
1593 }
Lajos Molnar09524832014-07-17 14:29:51 -07001594
1595 if (mSourceFlags & Source::FLAG_SECURE) {
1596 format->setInt32("secure", true);
1597 }
Chong Zhang17134602015-01-07 16:14:34 -08001598
1599 if (mSourceFlags & Source::FLAG_PROTECTED) {
1600 format->setInt32("protected", true);
1601 }
Ronghua Wu8db88132015-04-22 13:51:35 -07001602
Ronghua Wuc8a70d32015-04-29 16:26:34 -07001603 float rate = getFrameRate();
1604 if (rate > 0) {
Lajos Molnar3a474aa2015-04-24 17:10:07 -07001605 format->setFloat("operating-rate", rate * mPlaybackSettings.mSpeed);
Ronghua Wu8db88132015-04-22 13:51:35 -07001606 }
Andreas Huber3fe62152011-09-16 15:09:22 -07001607 }
1608
Wei Jiabc2fb722014-07-08 16:37:57 -07001609 if (audio) {
Lajos Molnar1d15ab52015-03-04 16:46:34 -08001610 sp<AMessage> notify = new AMessage(kWhatAudioNotify, this);
Wei Jia88703c32014-08-06 11:24:07 -07001611 ++mAudioDecoderGeneration;
1612 notify->setInt32("generation", mAudioDecoderGeneration);
1613
Wei Jiaa05f1e32016-03-25 16:31:22 -07001614 if (checkAudioModeChange) {
Wei Jia41cd4632016-05-13 10:53:30 -07001615 determineAudioModeChange(format);
Wei Jiaa05f1e32016-03-25 16:31:22 -07001616 }
Wei Jiabc2fb722014-07-08 16:37:57 -07001617 if (mOffloadAudio) {
Wei Jia14532f22015-12-29 11:28:15 -08001618 mSource->setOffloadAudio(true /* offload */);
1619
Haynes Mathew George8b635332015-03-30 17:59:47 -07001620 const bool hasVideo = (mSource->getFormat(false /*audio */) != NULL);
1621 format->setInt32("has-video", hasVideo);
Wei Jiac6cfd702014-11-11 16:33:20 -08001622 *decoder = new DecoderPassThrough(notify, mSource, mRenderer);
Wei Jiabc2fb722014-07-08 16:37:57 -07001623 } else {
Wei Jia14532f22015-12-29 11:28:15 -08001624 mSource->setOffloadAudio(false /* offload */);
1625
Ronghua Wu68845c12015-07-21 09:50:48 -07001626 *decoder = new Decoder(notify, mSource, mPID, mRenderer);
Wei Jiabc2fb722014-07-08 16:37:57 -07001627 }
1628 } else {
Lajos Molnar1d15ab52015-03-04 16:46:34 -08001629 sp<AMessage> notify = new AMessage(kWhatVideoNotify, this);
Wei Jia88703c32014-08-06 11:24:07 -07001630 ++mVideoDecoderGeneration;
1631 notify->setInt32("generation", mVideoDecoderGeneration);
1632
Chong Zhang7137ec72014-11-12 16:41:05 -08001633 *decoder = new Decoder(
Ronghua Wu68845c12015-07-21 09:50:48 -07001634 notify, mSource, mPID, mRenderer, mSurface, mCCDecoder);
Lajos Molnard9fd6312014-11-06 11:00:00 -08001635
1636 // enable FRC if high-quality AV sync is requested, even if not
Lajos Molnar1de1e252015-04-30 18:18:34 -07001637 // directly queuing to display, as this will even improve textureview
Lajos Molnard9fd6312014-11-06 11:00:00 -08001638 // playback.
1639 {
1640 char value[PROPERTY_VALUE_MAX];
1641 if (property_get("persist.sys.media.avsync", value, NULL) &&
1642 (!strcmp("1", value) || !strcasecmp("true", value))) {
1643 format->setInt32("auto-frc", 1);
1644 }
1645 }
Wei Jiabc2fb722014-07-08 16:37:57 -07001646 }
Lajos Molnar1cd13982014-01-17 15:12:51 -08001647 (*decoder)->init();
Andreas Huber84066782011-08-16 09:34:26 -07001648 (*decoder)->configure(format);
Andreas Huberf9334412010-12-15 15:17:42 -08001649
Lajos Molnar09524832014-07-17 14:29:51 -07001650 // allocate buffers to decrypt widevine source buffers
1651 if (!audio && (mSourceFlags & Source::FLAG_SECURE)) {
1652 Vector<sp<ABuffer> > inputBufs;
1653 CHECK_EQ((*decoder)->getInputBuffers(&inputBufs), (status_t)OK);
1654
1655 Vector<MediaBuffer *> mediaBufs;
1656 for (size_t i = 0; i < inputBufs.size(); i++) {
1657 const sp<ABuffer> &buffer = inputBufs[i];
1658 MediaBuffer *mbuf = new MediaBuffer(buffer->data(), buffer->size());
1659 mediaBufs.push(mbuf);
1660 }
1661
1662 status_t err = mSource->setBuffers(audio, mediaBufs);
1663 if (err != OK) {
1664 for (size_t i = 0; i < mediaBufs.size(); ++i) {
1665 mediaBufs[i]->release();
1666 }
1667 mediaBufs.clear();
1668 ALOGE("Secure source didn't support secure mediaBufs.");
1669 return err;
1670 }
1671 }
Andreas Huberf9334412010-12-15 15:17:42 -08001672 return OK;
1673}
1674
Chong Zhangced1c2f2014-08-08 15:22:35 -07001675void NuPlayer::updateVideoSize(
1676 const sp<AMessage> &inputFormat,
1677 const sp<AMessage> &outputFormat) {
1678 if (inputFormat == NULL) {
1679 ALOGW("Unknown video size, reporting 0x0!");
1680 notifyListener(MEDIA_SET_VIDEO_SIZE, 0, 0);
1681 return;
1682 }
1683
1684 int32_t displayWidth, displayHeight;
Chong Zhangced1c2f2014-08-08 15:22:35 -07001685 if (outputFormat != NULL) {
1686 int32_t width, height;
1687 CHECK(outputFormat->findInt32("width", &width));
1688 CHECK(outputFormat->findInt32("height", &height));
1689
1690 int32_t cropLeft, cropTop, cropRight, cropBottom;
1691 CHECK(outputFormat->findRect(
1692 "crop",
1693 &cropLeft, &cropTop, &cropRight, &cropBottom));
1694
1695 displayWidth = cropRight - cropLeft + 1;
1696 displayHeight = cropBottom - cropTop + 1;
1697
1698 ALOGV("Video output format changed to %d x %d "
1699 "(crop: %d x %d @ (%d, %d))",
1700 width, height,
1701 displayWidth,
1702 displayHeight,
1703 cropLeft, cropTop);
1704 } else {
1705 CHECK(inputFormat->findInt32("width", &displayWidth));
1706 CHECK(inputFormat->findInt32("height", &displayHeight));
1707
1708 ALOGV("Video input format %d x %d", displayWidth, displayHeight);
1709 }
1710
1711 // Take into account sample aspect ratio if necessary:
1712 int32_t sarWidth, sarHeight;
1713 if (inputFormat->findInt32("sar-width", &sarWidth)
1714 && inputFormat->findInt32("sar-height", &sarHeight)) {
1715 ALOGV("Sample aspect ratio %d : %d", sarWidth, sarHeight);
1716
1717 displayWidth = (displayWidth * sarWidth) / sarHeight;
1718
1719 ALOGV("display dimensions %d x %d", displayWidth, displayHeight);
1720 }
1721
1722 int32_t rotationDegrees;
1723 if (!inputFormat->findInt32("rotation-degrees", &rotationDegrees)) {
1724 rotationDegrees = 0;
1725 }
1726
1727 if (rotationDegrees == 90 || rotationDegrees == 270) {
1728 int32_t tmp = displayWidth;
1729 displayWidth = displayHeight;
1730 displayHeight = tmp;
1731 }
1732
1733 notifyListener(
1734 MEDIA_SET_VIDEO_SIZE,
1735 displayWidth,
1736 displayHeight);
1737}
1738
Chong Zhangdcb89b32013-08-06 09:44:47 -07001739void NuPlayer::notifyListener(int msg, int ext1, int ext2, const Parcel *in) {
Andreas Huber43c3e6c2011-01-05 12:17:08 -08001740 if (mDriver == NULL) {
Andreas Huberf9334412010-12-15 15:17:42 -08001741 return;
1742 }
1743
Andreas Huber43c3e6c2011-01-05 12:17:08 -08001744 sp<NuPlayerDriver> driver = mDriver.promote();
Andreas Huberf9334412010-12-15 15:17:42 -08001745
Andreas Huber43c3e6c2011-01-05 12:17:08 -08001746 if (driver == NULL) {
Andreas Huberf9334412010-12-15 15:17:42 -08001747 return;
1748 }
1749
Chong Zhangdcb89b32013-08-06 09:44:47 -07001750 driver->notifyListener(msg, ext1, ext2, in);
Andreas Huberf9334412010-12-15 15:17:42 -08001751}
1752
Chong Zhang7137ec72014-11-12 16:41:05 -08001753void NuPlayer::flushDecoder(bool audio, bool needShutdown) {
Andreas Huber14f76722013-01-15 09:04:18 -08001754 ALOGV("[%s] flushDecoder needShutdown=%d",
1755 audio ? "audio" : "video", needShutdown);
1756
Chong Zhang7137ec72014-11-12 16:41:05 -08001757 const sp<DecoderBase> &decoder = getDecoder(audio);
Lajos Molnar87603c02014-08-20 19:25:30 -07001758 if (decoder == NULL) {
Steve Blockdf64d152012-01-04 20:05:49 +00001759 ALOGI("flushDecoder %s without decoder present",
Andreas Huber6e3d3112011-11-28 12:36:11 -08001760 audio ? "audio" : "video");
Lajos Molnar87603c02014-08-20 19:25:30 -07001761 return;
Andreas Huber6e3d3112011-11-28 12:36:11 -08001762 }
1763
Andreas Huber1aef2112011-01-04 14:01:29 -08001764 // Make sure we don't continue to scan sources until we finish flushing.
1765 ++mScanSourcesGeneration;
Chong Zhang3b032b32015-04-17 15:49:06 -07001766 if (mScanSourcesPending) {
1767 mDeferredActions.push_back(
1768 new SimpleAction(&NuPlayer::performScanSources));
1769 mScanSourcesPending = false;
1770 }
Andreas Huber1aef2112011-01-04 14:01:29 -08001771
Chong Zhang7137ec72014-11-12 16:41:05 -08001772 decoder->signalFlush();
Andreas Huber1aef2112011-01-04 14:01:29 -08001773
1774 FlushStatus newStatus =
1775 needShutdown ? FLUSHING_DECODER_SHUTDOWN : FLUSHING_DECODER;
1776
Lajos Molnarfcd3e942015-03-31 10:06:48 -07001777 mFlushComplete[audio][false /* isDecoder */] = (mRenderer == NULL);
Andy Hung8d121d42014-10-03 09:53:53 -07001778 mFlushComplete[audio][true /* isDecoder */] = false;
Andreas Huber1aef2112011-01-04 14:01:29 -08001779 if (audio) {
Wei Jia53904f32014-07-29 10:22:53 -07001780 ALOGE_IF(mFlushingAudio != NONE,
1781 "audio flushDecoder() is called in state %d", mFlushingAudio);
Andreas Huber1aef2112011-01-04 14:01:29 -08001782 mFlushingAudio = newStatus;
Andreas Huber1aef2112011-01-04 14:01:29 -08001783 } else {
Wei Jia53904f32014-07-29 10:22:53 -07001784 ALOGE_IF(mFlushingVideo != NONE,
1785 "video flushDecoder() is called in state %d", mFlushingVideo);
Andreas Huber1aef2112011-01-04 14:01:29 -08001786 mFlushingVideo = newStatus;
Andreas Huber1aef2112011-01-04 14:01:29 -08001787 }
1788}
1789
Chong Zhangced1c2f2014-08-08 15:22:35 -07001790void NuPlayer::queueDecoderShutdown(
1791 bool audio, bool video, const sp<AMessage> &reply) {
1792 ALOGI("queueDecoderShutdown audio=%d, video=%d", audio, video);
Andreas Huber84066782011-08-16 09:34:26 -07001793
Chong Zhangced1c2f2014-08-08 15:22:35 -07001794 mDeferredActions.push_back(
Wei Jiafef808d2014-10-31 17:57:05 -07001795 new FlushDecoderAction(
1796 audio ? FLUSH_CMD_SHUTDOWN : FLUSH_CMD_NONE,
1797 video ? FLUSH_CMD_SHUTDOWN : FLUSH_CMD_NONE));
Andreas Huber84066782011-08-16 09:34:26 -07001798
Chong Zhangced1c2f2014-08-08 15:22:35 -07001799 mDeferredActions.push_back(
1800 new SimpleAction(&NuPlayer::performScanSources));
Andreas Huber84066782011-08-16 09:34:26 -07001801
Chong Zhangced1c2f2014-08-08 15:22:35 -07001802 mDeferredActions.push_back(new PostMessageAction(reply));
1803
1804 processDeferredActions();
Andreas Huber84066782011-08-16 09:34:26 -07001805}
1806
James Dong0d268a32012-08-31 12:18:27 -07001807status_t NuPlayer::setVideoScalingMode(int32_t mode) {
1808 mVideoScalingMode = mode;
Lajos Molnar1de1e252015-04-30 18:18:34 -07001809 if (mSurface != NULL) {
1810 status_t ret = native_window_set_scaling_mode(mSurface.get(), mVideoScalingMode);
James Dong0d268a32012-08-31 12:18:27 -07001811 if (ret != OK) {
1812 ALOGE("Failed to set scaling mode (%d): %s",
1813 -ret, strerror(-ret));
1814 return ret;
1815 }
1816 }
1817 return OK;
1818}
1819
Chong Zhangdcb89b32013-08-06 09:44:47 -07001820status_t NuPlayer::getTrackInfo(Parcel* reply) const {
Lajos Molnar1d15ab52015-03-04 16:46:34 -08001821 sp<AMessage> msg = new AMessage(kWhatGetTrackInfo, this);
Chong Zhangdcb89b32013-08-06 09:44:47 -07001822 msg->setPointer("reply", reply);
1823
1824 sp<AMessage> response;
1825 status_t err = msg->postAndAwaitResponse(&response);
1826 return err;
1827}
1828
Robert Shih7c4f0d72014-07-09 18:53:31 -07001829status_t NuPlayer::getSelectedTrack(int32_t type, Parcel* reply) const {
Lajos Molnar1d15ab52015-03-04 16:46:34 -08001830 sp<AMessage> msg = new AMessage(kWhatGetSelectedTrack, this);
Robert Shih7c4f0d72014-07-09 18:53:31 -07001831 msg->setPointer("reply", reply);
1832 msg->setInt32("type", type);
1833
1834 sp<AMessage> response;
1835 status_t err = msg->postAndAwaitResponse(&response);
1836 if (err == OK && response != NULL) {
1837 CHECK(response->findInt32("err", &err));
1838 }
1839 return err;
1840}
1841
Robert Shih6ffb1fd2014-10-29 16:24:32 -07001842status_t NuPlayer::selectTrack(size_t trackIndex, bool select, int64_t timeUs) {
Lajos Molnar1d15ab52015-03-04 16:46:34 -08001843 sp<AMessage> msg = new AMessage(kWhatSelectTrack, this);
Chong Zhangdcb89b32013-08-06 09:44:47 -07001844 msg->setSize("trackIndex", trackIndex);
1845 msg->setInt32("select", select);
Robert Shih6ffb1fd2014-10-29 16:24:32 -07001846 msg->setInt64("timeUs", timeUs);
Chong Zhangdcb89b32013-08-06 09:44:47 -07001847
1848 sp<AMessage> response;
1849 status_t err = msg->postAndAwaitResponse(&response);
1850
Chong Zhang404fced2014-06-11 14:45:31 -07001851 if (err != OK) {
1852 return err;
1853 }
1854
1855 if (!response->findInt32("err", &err)) {
1856 err = OK;
1857 }
1858
Chong Zhangdcb89b32013-08-06 09:44:47 -07001859 return err;
1860}
1861
Ronghua Wua73d9e02014-10-08 15:13:29 -07001862status_t NuPlayer::getCurrentPosition(int64_t *mediaUs) {
1863 sp<Renderer> renderer = mRenderer;
1864 if (renderer == NULL) {
1865 return NO_INIT;
1866 }
1867
1868 return renderer->getCurrentPosition(mediaUs);
1869}
1870
Praveen Chavane1e5d7a2015-05-19 19:09:48 -07001871void NuPlayer::getStats(Vector<sp<AMessage> > *mTrackStats) {
1872 CHECK(mTrackStats != NULL);
1873
1874 mTrackStats->clear();
1875 if (mVideoDecoder != NULL) {
1876 mTrackStats->push_back(mVideoDecoder->getStats());
1877 }
1878 if (mAudioDecoder != NULL) {
1879 mTrackStats->push_back(mAudioDecoder->getStats());
Chong Zhang7137ec72014-11-12 16:41:05 -08001880 }
Ronghua Wua73d9e02014-10-08 15:13:29 -07001881}
1882
Marco Nelissenf0b72b52014-09-16 15:43:44 -07001883sp<MetaData> NuPlayer::getFileMeta() {
1884 return mSource->getFileFormatMeta();
1885}
1886
Ronghua Wuc8a70d32015-04-29 16:26:34 -07001887float NuPlayer::getFrameRate() {
1888 sp<MetaData> meta = mSource->getFormatMeta(false /* audio */);
1889 if (meta == NULL) {
1890 return 0;
1891 }
1892 int32_t rate;
1893 if (!meta->findInt32(kKeyFrameRate, &rate)) {
1894 // fall back to try file meta
1895 sp<MetaData> fileMeta = getFileMeta();
1896 if (fileMeta == NULL) {
1897 ALOGW("source has video meta but not file meta");
1898 return -1;
1899 }
1900 int32_t fileMetaRate;
1901 if (!fileMeta->findInt32(kKeyFrameRate, &fileMetaRate)) {
1902 return -1;
1903 }
1904 return fileMetaRate;
1905 }
1906 return rate;
1907}
1908
Andreas Huberb7c8e912012-11-27 15:02:53 -08001909void NuPlayer::schedulePollDuration() {
Lajos Molnar1d15ab52015-03-04 16:46:34 -08001910 sp<AMessage> msg = new AMessage(kWhatPollDuration, this);
Andreas Huberb7c8e912012-11-27 15:02:53 -08001911 msg->setInt32("generation", mPollDurationGeneration);
1912 msg->post();
1913}
1914
1915void NuPlayer::cancelPollDuration() {
1916 ++mPollDurationGeneration;
1917}
1918
Andreas Hubera1f8ab02012-11-30 10:53:22 -08001919void NuPlayer::processDeferredActions() {
1920 while (!mDeferredActions.empty()) {
1921 // We won't execute any deferred actions until we're no longer in
1922 // an intermediate state, i.e. one more more decoders are currently
1923 // flushing or shutting down.
1924
Andreas Hubera1f8ab02012-11-30 10:53:22 -08001925 if (mFlushingAudio != NONE || mFlushingVideo != NONE) {
1926 // We're currently flushing, postpone the reset until that's
1927 // completed.
1928
1929 ALOGV("postponing action mFlushingAudio=%d, mFlushingVideo=%d",
1930 mFlushingAudio, mFlushingVideo);
1931
1932 break;
1933 }
1934
1935 sp<Action> action = *mDeferredActions.begin();
1936 mDeferredActions.erase(mDeferredActions.begin());
1937
1938 action->execute(this);
1939 }
1940}
1941
Wei Jia29840802015-05-15 17:11:38 -07001942void NuPlayer::performSeek(int64_t seekTimeUs) {
1943 ALOGV("performSeek seekTimeUs=%lld us (%.2f secs)",
Lajos Molnar6d339f12015-04-17 16:15:53 -07001944 (long long)seekTimeUs,
Wei Jia29840802015-05-15 17:11:38 -07001945 seekTimeUs / 1E6);
Andreas Hubera1f8ab02012-11-30 10:53:22 -08001946
Andy Hungadf34bf2014-09-03 18:22:22 -07001947 if (mSource == NULL) {
1948 // This happens when reset occurs right before the loop mode
1949 // asynchronously seeks to the start of the stream.
1950 LOG_ALWAYS_FATAL_IF(mAudioDecoder != NULL || mVideoDecoder != NULL,
1951 "mSource is NULL and decoders not NULL audio(%p) video(%p)",
1952 mAudioDecoder.get(), mVideoDecoder.get());
1953 return;
1954 }
Robert Shih1a5c8592015-08-04 18:07:44 -07001955 mPreviousSeekTimeUs = seekTimeUs;
Andreas Hubera1f8ab02012-11-30 10:53:22 -08001956 mSource->seekTo(seekTimeUs);
Robert Shihd3b0bbb2014-07-23 15:00:25 -07001957 ++mTimedTextGeneration;
Andreas Hubera1f8ab02012-11-30 10:53:22 -08001958
Andreas Hubera1f8ab02012-11-30 10:53:22 -08001959 // everything's flushed, continue playback.
1960}
1961
Wei Jiafef808d2014-10-31 17:57:05 -07001962void NuPlayer::performDecoderFlush(FlushCommand audio, FlushCommand video) {
1963 ALOGV("performDecoderFlush audio=%d, video=%d", audio, video);
Andreas Hubera1f8ab02012-11-30 10:53:22 -08001964
Wei Jiafef808d2014-10-31 17:57:05 -07001965 if ((audio == FLUSH_CMD_NONE || mAudioDecoder == NULL)
1966 && (video == FLUSH_CMD_NONE || mVideoDecoder == NULL)) {
Andreas Hubera1f8ab02012-11-30 10:53:22 -08001967 return;
1968 }
1969
Wei Jiafef808d2014-10-31 17:57:05 -07001970 if (audio != FLUSH_CMD_NONE && mAudioDecoder != NULL) {
1971 flushDecoder(true /* audio */, (audio == FLUSH_CMD_SHUTDOWN));
Andreas Hubera1f8ab02012-11-30 10:53:22 -08001972 }
1973
Wei Jiafef808d2014-10-31 17:57:05 -07001974 if (video != FLUSH_CMD_NONE && mVideoDecoder != NULL) {
1975 flushDecoder(false /* audio */, (video == FLUSH_CMD_SHUTDOWN));
Andreas Hubera1f8ab02012-11-30 10:53:22 -08001976 }
1977}
1978
1979void NuPlayer::performReset() {
1980 ALOGV("performReset");
1981
1982 CHECK(mAudioDecoder == NULL);
1983 CHECK(mVideoDecoder == NULL);
1984
1985 cancelPollDuration();
1986
1987 ++mScanSourcesGeneration;
1988 mScanSourcesPending = false;
1989
Lajos Molnar09524832014-07-17 14:29:51 -07001990 if (mRendererLooper != NULL) {
1991 if (mRenderer != NULL) {
1992 mRendererLooper->unregisterHandler(mRenderer->id());
1993 }
1994 mRendererLooper->stop();
1995 mRendererLooper.clear();
1996 }
Andreas Hubera1f8ab02012-11-30 10:53:22 -08001997 mRenderer.clear();
Wei Jia57568df2014-09-22 10:16:29 -07001998 ++mRendererGeneration;
Andreas Hubera1f8ab02012-11-30 10:53:22 -08001999
2000 if (mSource != NULL) {
2001 mSource->stop();
Andreas Huberb5f25f02013-02-05 10:14:26 -08002002
Wei Jiac45a4b22016-04-15 15:30:23 -07002003 Mutex::Autolock autoLock(mSourceLock);
Andreas Hubera1f8ab02012-11-30 10:53:22 -08002004 mSource.clear();
2005 }
2006
2007 if (mDriver != NULL) {
2008 sp<NuPlayerDriver> driver = mDriver.promote();
2009 if (driver != NULL) {
2010 driver->notifyResetComplete();
2011 }
2012 }
Andreas Huber57a339c2012-12-03 11:18:00 -08002013
2014 mStarted = false;
Ronghua Wu64c2d172015-10-07 16:52:19 -07002015 mResetting = false;
Robert Shih0c61a0d2015-07-06 15:09:10 -07002016 mSourceStarted = false;
Andreas Hubera1f8ab02012-11-30 10:53:22 -08002017}
2018
2019void NuPlayer::performScanSources() {
2020 ALOGV("performScanSources");
2021
Andreas Huber57a339c2012-12-03 11:18:00 -08002022 if (!mStarted) {
2023 return;
2024 }
2025
Andreas Hubera1f8ab02012-11-30 10:53:22 -08002026 if (mAudioDecoder == NULL || mVideoDecoder == NULL) {
2027 postScanSources();
2028 }
2029}
2030
Lajos Molnar1de1e252015-04-30 18:18:34 -07002031void NuPlayer::performSetSurface(const sp<Surface> &surface) {
Andreas Huber57a339c2012-12-03 11:18:00 -08002032 ALOGV("performSetSurface");
2033
Lajos Molnar1de1e252015-04-30 18:18:34 -07002034 mSurface = surface;
Andreas Huber57a339c2012-12-03 11:18:00 -08002035
2036 // XXX - ignore error from setVideoScalingMode for now
2037 setVideoScalingMode(mVideoScalingMode);
Chong Zhang13d6faa2014-08-22 15:35:28 -07002038
2039 if (mDriver != NULL) {
2040 sp<NuPlayerDriver> driver = mDriver.promote();
2041 if (driver != NULL) {
2042 driver->notifySetSurfaceComplete();
2043 }
2044 }
Andreas Huber57a339c2012-12-03 11:18:00 -08002045}
2046
Chong Zhangf8d71772014-11-26 15:08:34 -08002047void NuPlayer::performResumeDecoders(bool needNotify) {
2048 if (needNotify) {
2049 mResumePending = true;
2050 if (mVideoDecoder == NULL) {
2051 // if audio-only, we can notify seek complete now,
2052 // as the resume operation will be relatively fast.
2053 finishResume();
2054 }
2055 }
2056
Chong Zhang7137ec72014-11-12 16:41:05 -08002057 if (mVideoDecoder != NULL) {
Chong Zhangf8d71772014-11-26 15:08:34 -08002058 // When there is continuous seek, MediaPlayer will cache the seek
2059 // position, and send down new seek request when previous seek is
2060 // complete. Let's wait for at least one video output frame before
2061 // notifying seek complete, so that the video thumbnail gets updated
2062 // when seekbar is dragged.
2063 mVideoDecoder->signalResume(needNotify);
Chong Zhang7137ec72014-11-12 16:41:05 -08002064 }
2065
2066 if (mAudioDecoder != NULL) {
Chong Zhangf8d71772014-11-26 15:08:34 -08002067 mAudioDecoder->signalResume(false /* needNotify */);
2068 }
2069}
2070
2071void NuPlayer::finishResume() {
2072 if (mResumePending) {
2073 mResumePending = false;
Wei Jia1061c9c2015-05-19 16:02:17 -07002074 notifyDriverSeekComplete();
2075 }
2076}
2077
2078void NuPlayer::notifyDriverSeekComplete() {
2079 if (mDriver != NULL) {
2080 sp<NuPlayerDriver> driver = mDriver.promote();
2081 if (driver != NULL) {
2082 driver->notifySeekComplete();
Chong Zhangf8d71772014-11-26 15:08:34 -08002083 }
Chong Zhang7137ec72014-11-12 16:41:05 -08002084 }
2085}
2086
Andreas Huber9575c962013-02-05 13:59:56 -08002087void NuPlayer::onSourceNotify(const sp<AMessage> &msg) {
2088 int32_t what;
2089 CHECK(msg->findInt32("what", &what));
2090
2091 switch (what) {
Lajos Molnarfcd3e942015-03-31 10:06:48 -07002092 case Source::kWhatInstantiateSecureDecoders:
2093 {
2094 if (mSource == NULL) {
2095 // This is a stale notification from a source that was
2096 // asynchronously preparing when the client called reset().
2097 // We handled the reset, the source is gone.
2098 break;
2099 }
2100
2101 sp<AMessage> reply;
2102 CHECK(msg->findMessage("reply", &reply));
2103 status_t err = onInstantiateSecureDecoders();
2104 reply->setInt32("err", err);
2105 reply->post();
2106 break;
2107 }
2108
Andreas Huber9575c962013-02-05 13:59:56 -08002109 case Source::kWhatPrepared:
2110 {
Andreas Huberb5f28d42013-04-25 15:11:19 -07002111 if (mSource == NULL) {
2112 // This is a stale notification from a source that was
2113 // asynchronously preparing when the client called reset().
2114 // We handled the reset, the source is gone.
2115 break;
2116 }
2117
Andreas Huberec0c5972013-02-05 14:47:13 -08002118 int32_t err;
2119 CHECK(msg->findInt32("err", &err));
2120
Lajos Molnarfcd3e942015-03-31 10:06:48 -07002121 if (err != OK) {
2122 // shut down potential secure codecs in case client never calls reset
2123 mDeferredActions.push_back(
2124 new FlushDecoderAction(FLUSH_CMD_SHUTDOWN /* audio */,
2125 FLUSH_CMD_SHUTDOWN /* video */));
2126 processDeferredActions();
2127 }
2128
Andreas Huber9575c962013-02-05 13:59:56 -08002129 sp<NuPlayerDriver> driver = mDriver.promote();
2130 if (driver != NULL) {
Marco Nelissendd114d12014-05-28 15:23:14 -07002131 // notify duration first, so that it's definitely set when
2132 // the app received the "prepare complete" callback.
2133 int64_t durationUs;
2134 if (mSource->getDuration(&durationUs) == OK) {
2135 driver->notifyDuration(durationUs);
2136 }
Andreas Huberec0c5972013-02-05 14:47:13 -08002137 driver->notifyPrepareCompleted(err);
Andreas Huber9575c962013-02-05 13:59:56 -08002138 }
Andreas Huber99759402013-04-01 14:28:31 -07002139
Andreas Huber9575c962013-02-05 13:59:56 -08002140 break;
2141 }
2142
2143 case Source::kWhatFlagsChanged:
2144 {
2145 uint32_t flags;
2146 CHECK(msg->findInt32("flags", (int32_t *)&flags));
2147
Chong Zhang4b7069d2013-09-11 12:52:43 -07002148 sp<NuPlayerDriver> driver = mDriver.promote();
2149 if (driver != NULL) {
Wei Jia895651b2014-12-10 17:31:52 -08002150 if ((flags & NuPlayer::Source::FLAG_CAN_SEEK) == 0) {
2151 driver->notifyListener(
2152 MEDIA_INFO, MEDIA_INFO_NOT_SEEKABLE, 0);
2153 }
Chong Zhang4b7069d2013-09-11 12:52:43 -07002154 driver->notifyFlagsChanged(flags);
2155 }
2156
Andreas Huber9575c962013-02-05 13:59:56 -08002157 if ((mSourceFlags & Source::FLAG_DYNAMIC_DURATION)
2158 && (!(flags & Source::FLAG_DYNAMIC_DURATION))) {
2159 cancelPollDuration();
2160 } else if (!(mSourceFlags & Source::FLAG_DYNAMIC_DURATION)
2161 && (flags & Source::FLAG_DYNAMIC_DURATION)
2162 && (mAudioDecoder != NULL || mVideoDecoder != NULL)) {
2163 schedulePollDuration();
2164 }
2165
2166 mSourceFlags = flags;
2167 break;
2168 }
2169
2170 case Source::kWhatVideoSizeChanged:
2171 {
Chong Zhangced1c2f2014-08-08 15:22:35 -07002172 sp<AMessage> format;
2173 CHECK(msg->findMessage("format", &format));
Andreas Huber9575c962013-02-05 13:59:56 -08002174
Chong Zhangced1c2f2014-08-08 15:22:35 -07002175 updateVideoSize(format);
Andreas Huber9575c962013-02-05 13:59:56 -08002176 break;
2177 }
2178
Chong Zhang2a3cc9a2014-08-21 17:48:26 -07002179 case Source::kWhatBufferingUpdate:
2180 {
2181 int32_t percentage;
2182 CHECK(msg->findInt32("percentage", &percentage));
2183
2184 notifyListener(MEDIA_BUFFERING_UPDATE, percentage, 0);
2185 break;
2186 }
2187
Chong Zhangefbb6192015-01-30 17:13:27 -08002188 case Source::kWhatPauseOnBufferingStart:
2189 {
2190 // ignore if not playing
Chong Zhang8a048332015-05-06 15:16:28 -07002191 if (mStarted) {
Chong Zhangefbb6192015-01-30 17:13:27 -08002192 ALOGI("buffer low, pausing...");
2193
Chong Zhang8a048332015-05-06 15:16:28 -07002194 mPausedForBuffering = true;
Chong Zhangefbb6192015-01-30 17:13:27 -08002195 onPause();
2196 }
Wei Jiae67ba382016-02-03 01:41:49 +00002197 // fall-thru
2198 }
Chong Zhangefbb6192015-01-30 17:13:27 -08002199
Wei Jiae67ba382016-02-03 01:41:49 +00002200 case Source::kWhatBufferingStart:
2201 {
Wei Jiabfe82072016-05-20 09:21:50 -07002202 notifyListener(MEDIA_INFO, MEDIA_INFO_BUFFERING_START, 0);
Roger Jönssonb50e83e2013-01-21 16:26:41 +01002203 break;
2204 }
2205
Chong Zhangefbb6192015-01-30 17:13:27 -08002206 case Source::kWhatResumeOnBufferingEnd:
2207 {
2208 // ignore if not playing
Chong Zhang8a048332015-05-06 15:16:28 -07002209 if (mStarted) {
Chong Zhangefbb6192015-01-30 17:13:27 -08002210 ALOGI("buffer ready, resuming...");
2211
Chong Zhang8a048332015-05-06 15:16:28 -07002212 mPausedForBuffering = false;
2213
2214 // do not resume yet if client didn't unpause
2215 if (!mPausedByClient) {
2216 onResume();
2217 }
Chong Zhangefbb6192015-01-30 17:13:27 -08002218 }
Wei Jiae67ba382016-02-03 01:41:49 +00002219 // fall-thru
2220 }
Chong Zhangefbb6192015-01-30 17:13:27 -08002221
Wei Jiae67ba382016-02-03 01:41:49 +00002222 case Source::kWhatBufferingEnd:
2223 {
Wei Jia3bed45a2016-02-17 11:06:47 -08002224 notifyListener(MEDIA_INFO, MEDIA_INFO_BUFFERING_END, 0);
Roger Jönssonb50e83e2013-01-21 16:26:41 +01002225 break;
2226 }
2227
Chong Zhangefbb6192015-01-30 17:13:27 -08002228 case Source::kWhatCacheStats:
2229 {
2230 int32_t kbps;
2231 CHECK(msg->findInt32("bandwidth", &kbps));
2232
2233 notifyListener(MEDIA_INFO, MEDIA_INFO_NETWORK_BANDWIDTH, kbps);
2234 break;
2235 }
2236
Chong Zhangdcb89b32013-08-06 09:44:47 -07002237 case Source::kWhatSubtitleData:
2238 {
2239 sp<ABuffer> buffer;
2240 CHECK(msg->findBuffer("buffer", &buffer));
2241
Chong Zhang404fced2014-06-11 14:45:31 -07002242 sendSubtitleData(buffer, 0 /* baseIndex */);
Chong Zhangdcb89b32013-08-06 09:44:47 -07002243 break;
2244 }
2245
Robert Shih08528432015-04-08 09:06:54 -07002246 case Source::kWhatTimedMetaData:
2247 {
2248 sp<ABuffer> buffer;
2249 if (!msg->findBuffer("buffer", &buffer)) {
2250 notifyListener(MEDIA_INFO, MEDIA_INFO_METADATA_UPDATE, 0);
2251 } else {
2252 sendTimedMetaData(buffer);
2253 }
2254 break;
2255 }
2256
Robert Shihd3b0bbb2014-07-23 15:00:25 -07002257 case Source::kWhatTimedTextData:
2258 {
2259 int32_t generation;
2260 if (msg->findInt32("generation", &generation)
2261 && generation != mTimedTextGeneration) {
2262 break;
2263 }
2264
2265 sp<ABuffer> buffer;
2266 CHECK(msg->findBuffer("buffer", &buffer));
2267
2268 sp<NuPlayerDriver> driver = mDriver.promote();
2269 if (driver == NULL) {
2270 break;
2271 }
2272
2273 int posMs;
2274 int64_t timeUs, posUs;
2275 driver->getCurrentPosition(&posMs);
Patrik2 Carlsson24d484b2015-01-27 16:49:45 +01002276 posUs = (int64_t) posMs * 1000ll;
Robert Shihd3b0bbb2014-07-23 15:00:25 -07002277 CHECK(buffer->meta()->findInt64("timeUs", &timeUs));
2278
2279 if (posUs < timeUs) {
2280 if (!msg->findInt32("generation", &generation)) {
2281 msg->setInt32("generation", mTimedTextGeneration);
2282 }
2283 msg->post(timeUs - posUs);
2284 } else {
2285 sendTimedTextData(buffer);
2286 }
2287 break;
2288 }
2289
Andreas Huber14f76722013-01-15 09:04:18 -08002290 case Source::kWhatQueueDecoderShutdown:
2291 {
2292 int32_t audio, video;
2293 CHECK(msg->findInt32("audio", &audio));
2294 CHECK(msg->findInt32("video", &video));
2295
2296 sp<AMessage> reply;
2297 CHECK(msg->findMessage("reply", &reply));
2298
2299 queueDecoderShutdown(audio, video, reply);
2300 break;
2301 }
2302
Ronghua Wu80276872014-08-28 15:50:29 -07002303 case Source::kWhatDrmNoLicense:
2304 {
2305 notifyListener(MEDIA_ERROR, MEDIA_ERROR_UNKNOWN, ERROR_DRM_NO_LICENSE);
2306 break;
2307 }
2308
Andreas Huber9575c962013-02-05 13:59:56 -08002309 default:
2310 TRESPASS();
2311 }
2312}
2313
Chong Zhanga7fa1d92014-06-11 14:49:23 -07002314void NuPlayer::onClosedCaptionNotify(const sp<AMessage> &msg) {
2315 int32_t what;
2316 CHECK(msg->findInt32("what", &what));
2317
2318 switch (what) {
2319 case NuPlayer::CCDecoder::kWhatClosedCaptionData:
2320 {
2321 sp<ABuffer> buffer;
2322 CHECK(msg->findBuffer("buffer", &buffer));
2323
2324 size_t inbandTracks = 0;
2325 if (mSource != NULL) {
2326 inbandTracks = mSource->getTrackCount();
2327 }
2328
2329 sendSubtitleData(buffer, inbandTracks);
2330 break;
2331 }
2332
2333 case NuPlayer::CCDecoder::kWhatTrackAdded:
2334 {
2335 notifyListener(MEDIA_INFO, MEDIA_INFO_METADATA_UPDATE, 0);
2336
2337 break;
2338 }
2339
2340 default:
2341 TRESPASS();
2342 }
2343
2344
2345}
2346
Chong Zhang404fced2014-06-11 14:45:31 -07002347void NuPlayer::sendSubtitleData(const sp<ABuffer> &buffer, int32_t baseIndex) {
2348 int32_t trackIndex;
2349 int64_t timeUs, durationUs;
2350 CHECK(buffer->meta()->findInt32("trackIndex", &trackIndex));
2351 CHECK(buffer->meta()->findInt64("timeUs", &timeUs));
2352 CHECK(buffer->meta()->findInt64("durationUs", &durationUs));
2353
2354 Parcel in;
2355 in.writeInt32(trackIndex + baseIndex);
2356 in.writeInt64(timeUs);
2357 in.writeInt64(durationUs);
2358 in.writeInt32(buffer->size());
2359 in.writeInt32(buffer->size());
2360 in.write(buffer->data(), buffer->size());
2361
2362 notifyListener(MEDIA_SUBTITLE_DATA, 0, 0, &in);
2363}
Robert Shihd3b0bbb2014-07-23 15:00:25 -07002364
Robert Shih08528432015-04-08 09:06:54 -07002365void NuPlayer::sendTimedMetaData(const sp<ABuffer> &buffer) {
2366 int64_t timeUs;
2367 CHECK(buffer->meta()->findInt64("timeUs", &timeUs));
2368
2369 Parcel in;
2370 in.writeInt64(timeUs);
2371 in.writeInt32(buffer->size());
2372 in.writeInt32(buffer->size());
2373 in.write(buffer->data(), buffer->size());
2374
2375 notifyListener(MEDIA_META_DATA, 0, 0, &in);
2376}
2377
Robert Shihd3b0bbb2014-07-23 15:00:25 -07002378void NuPlayer::sendTimedTextData(const sp<ABuffer> &buffer) {
2379 const void *data;
2380 size_t size = 0;
2381 int64_t timeUs;
Marco Nelissen55e2f4c2015-09-04 15:57:15 -07002382 int32_t flag = TextDescriptions::IN_BAND_TEXT_3GPP;
Robert Shihd3b0bbb2014-07-23 15:00:25 -07002383
2384 AString mime;
2385 CHECK(buffer->meta()->findString("mime", &mime));
2386 CHECK(strcasecmp(mime.c_str(), MEDIA_MIMETYPE_TEXT_3GPP) == 0);
2387
2388 data = buffer->data();
2389 size = buffer->size();
2390
2391 Parcel parcel;
2392 if (size > 0) {
2393 CHECK(buffer->meta()->findInt64("timeUs", &timeUs));
Marco Nelissen55e2f4c2015-09-04 15:57:15 -07002394 int32_t global = 0;
2395 if (buffer->meta()->findInt32("global", &global) && global) {
2396 flag |= TextDescriptions::GLOBAL_DESCRIPTIONS;
2397 } else {
2398 flag |= TextDescriptions::LOCAL_DESCRIPTIONS;
2399 }
Robert Shihd3b0bbb2014-07-23 15:00:25 -07002400 TextDescriptions::getParcelOfDescriptions(
2401 (const uint8_t *)data, size, flag, timeUs / 1000, &parcel);
2402 }
2403
2404 if ((parcel.dataSize() > 0)) {
2405 notifyListener(MEDIA_TIMED_TEXT, 0, 0, &parcel);
2406 } else { // send an empty timed text
2407 notifyListener(MEDIA_TIMED_TEXT, 0, 0);
2408 }
2409}
Andreas Huberb5f25f02013-02-05 10:14:26 -08002410////////////////////////////////////////////////////////////////////////////////
2411
Chong Zhangced1c2f2014-08-08 15:22:35 -07002412sp<AMessage> NuPlayer::Source::getFormat(bool audio) {
2413 sp<MetaData> meta = getFormatMeta(audio);
2414
2415 if (meta == NULL) {
2416 return NULL;
2417 }
2418
2419 sp<AMessage> msg = new AMessage;
2420
2421 if(convertMetaDataToMessage(meta, &msg) == OK) {
2422 return msg;
2423 }
2424 return NULL;
2425}
2426
Andreas Huber9575c962013-02-05 13:59:56 -08002427void NuPlayer::Source::notifyFlagsChanged(uint32_t flags) {
2428 sp<AMessage> notify = dupNotify();
2429 notify->setInt32("what", kWhatFlagsChanged);
2430 notify->setInt32("flags", flags);
2431 notify->post();
2432}
2433
Chong Zhangced1c2f2014-08-08 15:22:35 -07002434void NuPlayer::Source::notifyVideoSizeChanged(const sp<AMessage> &format) {
Andreas Huber9575c962013-02-05 13:59:56 -08002435 sp<AMessage> notify = dupNotify();
2436 notify->setInt32("what", kWhatVideoSizeChanged);
Chong Zhangced1c2f2014-08-08 15:22:35 -07002437 notify->setMessage("format", format);
Andreas Huber9575c962013-02-05 13:59:56 -08002438 notify->post();
2439}
2440
Andreas Huberec0c5972013-02-05 14:47:13 -08002441void NuPlayer::Source::notifyPrepared(status_t err) {
Andreas Huber9575c962013-02-05 13:59:56 -08002442 sp<AMessage> notify = dupNotify();
2443 notify->setInt32("what", kWhatPrepared);
Andreas Huberec0c5972013-02-05 14:47:13 -08002444 notify->setInt32("err", err);
Andreas Huber9575c962013-02-05 13:59:56 -08002445 notify->post();
2446}
2447
Lajos Molnarfcd3e942015-03-31 10:06:48 -07002448void NuPlayer::Source::notifyInstantiateSecureDecoders(const sp<AMessage> &reply) {
2449 sp<AMessage> notify = dupNotify();
2450 notify->setInt32("what", kWhatInstantiateSecureDecoders);
2451 notify->setMessage("reply", reply);
2452 notify->post();
2453}
2454
Andreas Huber84333e02014-02-07 15:36:10 -08002455void NuPlayer::Source::onMessageReceived(const sp<AMessage> & /* msg */) {
Andreas Huberb5f25f02013-02-05 10:14:26 -08002456 TRESPASS();
2457}
2458
Andreas Huberf9334412010-12-15 15:17:42 -08002459} // namespace android