blob: 5c13633823e583caa64b4570b55dbc5e76239793 [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),
Chong Zhang8a048332015-05-06 15:16:28 -0700194 mPausedByClient(false),
195 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() {
Chong Zhang48296b72014-09-14 14:28:45 -0700402 if (mSource != NULL) {
403 // During a reset, the data source might be unresponsive already, we need to
404 // disconnect explicitly so that reads exit promptly.
405 // We can't queue the disconnect request to the looper, as it might be
406 // queued behind a stuck read and never gets processed.
407 // Doing a disconnect outside the looper to allows the pending reads to exit
408 // (either successfully or with error).
409 mSource->disconnect();
410 }
411
Lajos Molnar1d15ab52015-03-04 16:46:34 -0800412 (new AMessage(kWhatReset, this))->post();
Andreas Huber1aef2112011-01-04 14:01:29 -0800413}
414
Wei Jiae427abf2014-09-22 15:21:11 -0700415void NuPlayer::seekToAsync(int64_t seekTimeUs, bool needNotify) {
Lajos Molnar1d15ab52015-03-04 16:46:34 -0800416 sp<AMessage> msg = new AMessage(kWhatSeek, this);
Andreas Huber43c3e6c2011-01-05 12:17:08 -0800417 msg->setInt64("seekTimeUs", seekTimeUs);
Wei Jiae427abf2014-09-22 15:21:11 -0700418 msg->setInt32("needNotify", needNotify);
Andreas Huber43c3e6c2011-01-05 12:17:08 -0800419 msg->post();
420}
421
Andreas Huber53df1a42010-12-22 10:03:04 -0800422
Chong Zhang404fced2014-06-11 14:45:31 -0700423void NuPlayer::writeTrackInfo(
424 Parcel* reply, const sp<AMessage> format) const {
Marco Nelissenc367ca12015-09-15 09:51:59 -0700425 if (format == NULL) {
426 ALOGE("NULL format");
427 return;
428 }
Chong Zhang404fced2014-06-11 14:45:31 -0700429 int32_t trackType;
Marco Nelissen9436e482015-09-15 10:21:53 -0700430 if (!format->findInt32("type", &trackType)) {
Marco Nelissenc367ca12015-09-15 09:51:59 -0700431 ALOGE("no track type");
432 return;
433 }
Chong Zhang404fced2014-06-11 14:45:31 -0700434
Robert Shih755106e2015-04-30 14:36:45 -0700435 AString mime;
Robert Shih2e3a4252015-05-06 10:21:15 -0700436 if (!format->findString("mime", &mime)) {
437 // Java MediaPlayer only uses mimetype for subtitle and timedtext tracks.
438 // If we can't find the mimetype here it means that we wouldn't be needing
439 // the mimetype on the Java end. We still write a placeholder mime to keep the
440 // (de)serialization logic simple.
441 if (trackType == MEDIA_TRACK_TYPE_AUDIO) {
442 mime = "audio/";
443 } else if (trackType == MEDIA_TRACK_TYPE_VIDEO) {
444 mime = "video/";
445 } else {
Marco Nelissenc367ca12015-09-15 09:51:59 -0700446 ALOGE("unknown track type: %d", trackType);
447 return;
Robert Shih2e3a4252015-05-06 10:21:15 -0700448 }
449 }
Robert Shih755106e2015-04-30 14:36:45 -0700450
Chong Zhang404fced2014-06-11 14:45:31 -0700451 AString lang;
Marco Nelissen9436e482015-09-15 10:21:53 -0700452 if (!format->findString("language", &lang)) {
Marco Nelissenc367ca12015-09-15 09:51:59 -0700453 ALOGE("no language");
454 return;
455 }
Chong Zhang404fced2014-06-11 14:45:31 -0700456
457 reply->writeInt32(2); // write something non-zero
458 reply->writeInt32(trackType);
Robert Shih755106e2015-04-30 14:36:45 -0700459 reply->writeString16(String16(mime.c_str()));
Chong Zhang404fced2014-06-11 14:45:31 -0700460 reply->writeString16(String16(lang.c_str()));
461
462 if (trackType == MEDIA_TRACK_TYPE_SUBTITLE) {
Chong Zhang404fced2014-06-11 14:45:31 -0700463 int32_t isAuto, isDefault, isForced;
464 CHECK(format->findInt32("auto", &isAuto));
465 CHECK(format->findInt32("default", &isDefault));
466 CHECK(format->findInt32("forced", &isForced));
467
Chong Zhang404fced2014-06-11 14:45:31 -0700468 reply->writeInt32(isAuto);
469 reply->writeInt32(isDefault);
470 reply->writeInt32(isForced);
471 }
472}
473
Andreas Huberf9334412010-12-15 15:17:42 -0800474void NuPlayer::onMessageReceived(const sp<AMessage> &msg) {
475 switch (msg->what()) {
476 case kWhatSetDataSource:
477 {
Steve Block3856b092011-10-20 11:56:00 +0100478 ALOGV("kWhatSetDataSource");
Andreas Huberf9334412010-12-15 15:17:42 -0800479
480 CHECK(mSource == NULL);
481
Chong Zhang3de157d2014-08-05 20:54:44 -0700482 status_t err = OK;
Andreas Huber5bc087c2010-12-23 10:27:40 -0800483 sp<RefBase> obj;
484 CHECK(msg->findObject("source", &obj));
Chong Zhang3de157d2014-08-05 20:54:44 -0700485 if (obj != NULL) {
486 mSource = static_cast<Source *>(obj.get());
Chong Zhang3de157d2014-08-05 20:54:44 -0700487 } else {
488 err = UNKNOWN_ERROR;
489 }
Andreas Huber9575c962013-02-05 13:59:56 -0800490
491 CHECK(mDriver != NULL);
492 sp<NuPlayerDriver> driver = mDriver.promote();
493 if (driver != NULL) {
Chong Zhang3de157d2014-08-05 20:54:44 -0700494 driver->notifySetDataSourceCompleted(err);
Andreas Huber9575c962013-02-05 13:59:56 -0800495 }
496 break;
497 }
498
499 case kWhatPrepare:
500 {
501 mSource->prepareAsync();
Andreas Huberf9334412010-12-15 15:17:42 -0800502 break;
503 }
504
Chong Zhangdcb89b32013-08-06 09:44:47 -0700505 case kWhatGetTrackInfo:
506 {
Lajos Molnar3f274362015-03-05 14:35:41 -0800507 sp<AReplyToken> replyID;
Chong Zhangdcb89b32013-08-06 09:44:47 -0700508 CHECK(msg->senderAwaitsResponse(&replyID));
509
Chong Zhang404fced2014-06-11 14:45:31 -0700510 Parcel* reply;
511 CHECK(msg->findPointer("reply", (void**)&reply));
512
513 size_t inbandTracks = 0;
Chong Zhangdcb89b32013-08-06 09:44:47 -0700514 if (mSource != NULL) {
Chong Zhang404fced2014-06-11 14:45:31 -0700515 inbandTracks = mSource->getTrackCount();
516 }
517
Chong Zhanga7fa1d92014-06-11 14:49:23 -0700518 size_t ccTracks = 0;
519 if (mCCDecoder != NULL) {
520 ccTracks = mCCDecoder->getTrackCount();
521 }
522
Chong Zhang404fced2014-06-11 14:45:31 -0700523 // total track count
Chong Zhanga7fa1d92014-06-11 14:49:23 -0700524 reply->writeInt32(inbandTracks + ccTracks);
Chong Zhang404fced2014-06-11 14:45:31 -0700525
526 // write inband tracks
527 for (size_t i = 0; i < inbandTracks; ++i) {
528 writeTrackInfo(reply, mSource->getTrackInfo(i));
Chong Zhangdcb89b32013-08-06 09:44:47 -0700529 }
530
Chong Zhanga7fa1d92014-06-11 14:49:23 -0700531 // write CC track
532 for (size_t i = 0; i < ccTracks; ++i) {
533 writeTrackInfo(reply, mCCDecoder->getTrackInfo(i));
534 }
535
Chong Zhangdcb89b32013-08-06 09:44:47 -0700536 sp<AMessage> response = new AMessage;
Chong Zhangdcb89b32013-08-06 09:44:47 -0700537 response->postReply(replyID);
538 break;
539 }
540
Robert Shih7c4f0d72014-07-09 18:53:31 -0700541 case kWhatGetSelectedTrack:
542 {
543 status_t err = INVALID_OPERATION;
544 if (mSource != NULL) {
545 err = OK;
546
547 int32_t type32;
548 CHECK(msg->findInt32("type", (int32_t*)&type32));
549 media_track_type type = (media_track_type)type32;
550 ssize_t selectedTrack = mSource->getSelectedTrack(type);
551
552 Parcel* reply;
553 CHECK(msg->findPointer("reply", (void**)&reply));
554 reply->writeInt32(selectedTrack);
555 }
556
557 sp<AMessage> response = new AMessage;
558 response->setInt32("err", err);
559
Lajos Molnar3f274362015-03-05 14:35:41 -0800560 sp<AReplyToken> replyID;
Robert Shih7c4f0d72014-07-09 18:53:31 -0700561 CHECK(msg->senderAwaitsResponse(&replyID));
562 response->postReply(replyID);
563 break;
564 }
565
Chong Zhangdcb89b32013-08-06 09:44:47 -0700566 case kWhatSelectTrack:
567 {
Lajos Molnar3f274362015-03-05 14:35:41 -0800568 sp<AReplyToken> replyID;
Chong Zhangdcb89b32013-08-06 09:44:47 -0700569 CHECK(msg->senderAwaitsResponse(&replyID));
570
Chong Zhang404fced2014-06-11 14:45:31 -0700571 size_t trackIndex;
572 int32_t select;
Robert Shih6ffb1fd2014-10-29 16:24:32 -0700573 int64_t timeUs;
Chong Zhang404fced2014-06-11 14:45:31 -0700574 CHECK(msg->findSize("trackIndex", &trackIndex));
575 CHECK(msg->findInt32("select", &select));
Robert Shih6ffb1fd2014-10-29 16:24:32 -0700576 CHECK(msg->findInt64("timeUs", &timeUs));
Chong Zhang404fced2014-06-11 14:45:31 -0700577
Chong Zhangdcb89b32013-08-06 09:44:47 -0700578 status_t err = INVALID_OPERATION;
Chong Zhang404fced2014-06-11 14:45:31 -0700579
580 size_t inbandTracks = 0;
Chong Zhangdcb89b32013-08-06 09:44:47 -0700581 if (mSource != NULL) {
Chong Zhang404fced2014-06-11 14:45:31 -0700582 inbandTracks = mSource->getTrackCount();
583 }
Chong Zhanga7fa1d92014-06-11 14:49:23 -0700584 size_t ccTracks = 0;
585 if (mCCDecoder != NULL) {
586 ccTracks = mCCDecoder->getTrackCount();
587 }
Chong Zhang404fced2014-06-11 14:45:31 -0700588
589 if (trackIndex < inbandTracks) {
Robert Shih6ffb1fd2014-10-29 16:24:32 -0700590 err = mSource->selectTrack(trackIndex, select, timeUs);
Robert Shihd3b0bbb2014-07-23 15:00:25 -0700591
592 if (!select && err == OK) {
593 int32_t type;
594 sp<AMessage> info = mSource->getTrackInfo(trackIndex);
595 if (info != NULL
596 && info->findInt32("type", &type)
597 && type == MEDIA_TRACK_TYPE_TIMEDTEXT) {
598 ++mTimedTextGeneration;
599 }
600 }
Chong Zhanga7fa1d92014-06-11 14:49:23 -0700601 } else {
602 trackIndex -= inbandTracks;
603
604 if (trackIndex < ccTracks) {
605 err = mCCDecoder->selectTrack(trackIndex, select);
606 }
Chong Zhangdcb89b32013-08-06 09:44:47 -0700607 }
608
609 sp<AMessage> response = new AMessage;
610 response->setInt32("err", err);
611
612 response->postReply(replyID);
613 break;
614 }
615
Andreas Huberb7c8e912012-11-27 15:02:53 -0800616 case kWhatPollDuration:
617 {
618 int32_t generation;
619 CHECK(msg->findInt32("generation", &generation));
620
621 if (generation != mPollDurationGeneration) {
622 // stale
623 break;
624 }
625
626 int64_t durationUs;
627 if (mDriver != NULL && mSource->getDuration(&durationUs) == OK) {
628 sp<NuPlayerDriver> driver = mDriver.promote();
629 if (driver != NULL) {
630 driver->notifyDuration(durationUs);
631 }
632 }
633
634 msg->post(1000000ll); // poll again in a second.
635 break;
636 }
637
Lajos Molnar1de1e252015-04-30 18:18:34 -0700638 case kWhatSetVideoSurface:
Andreas Huberf9334412010-12-15 15:17:42 -0800639 {
Andreas Huberf9334412010-12-15 15:17:42 -0800640
641 sp<RefBase> obj;
Lajos Molnar1de1e252015-04-30 18:18:34 -0700642 CHECK(msg->findObject("surface", &obj));
643 sp<Surface> surface = static_cast<Surface *>(obj.get());
Lajos Molnara81c6222015-07-10 19:17:45 -0700644
645 ALOGD("onSetVideoSurface(%p, %s video decoder)",
646 surface.get(),
Wei Jia6b7d2ed2015-08-10 15:11:54 -0700647 (mSource != NULL && mStarted && mSource->getFormat(false /* audio */) != NULL
Lajos Molnara81c6222015-07-10 19:17:45 -0700648 && mVideoDecoder != NULL) ? "have" : "no");
649
Wei Jia6b7d2ed2015-08-10 15:11:54 -0700650 // Need to check mStarted before calling mSource->getFormat because NuPlayer might
651 // be in preparing state and it could take long time.
652 // When mStarted is true, mSource must have been set.
653 if (mSource == NULL || !mStarted || mSource->getFormat(false /* audio */) == NULL
Lajos Molnara81c6222015-07-10 19:17:45 -0700654 // NOTE: mVideoDecoder's mSurface is always non-null
655 || (mVideoDecoder != NULL && mVideoDecoder->setVideoSurface(surface) == OK)) {
Lajos Molnar1de1e252015-04-30 18:18:34 -0700656 performSetSurface(surface);
Wei Jiafef808d2014-10-31 17:57:05 -0700657 break;
658 }
659
660 mDeferredActions.push_back(
661 new FlushDecoderAction(FLUSH_CMD_FLUSH /* audio */,
662 FLUSH_CMD_SHUTDOWN /* video */));
663
Lajos Molnar1de1e252015-04-30 18:18:34 -0700664 mDeferredActions.push_back(new SetSurfaceAction(surface));
James Dong0d268a32012-08-31 12:18:27 -0700665
Wei Jiac6e58412015-07-10 18:04:55 -0700666 if (obj != NULL || mAudioDecoder != NULL) {
Wei Jiafef808d2014-10-31 17:57:05 -0700667 if (mStarted) {
Andy Hung73535852014-09-05 11:42:58 -0700668 // Issue a seek to refresh the video screen only if started otherwise
669 // the extractor may not yet be started and will assert.
670 // If the video decoder is not set (perhaps audio only in this case)
671 // do not perform a seek as it is not needed.
Ronghua Wua73d9e02014-10-08 15:13:29 -0700672 int64_t currentPositionUs = 0;
673 if (getCurrentPosition(&currentPositionUs) == OK) {
674 mDeferredActions.push_back(
Wei Jia29840802015-05-15 17:11:38 -0700675 new SeekAction(currentPositionUs));
Ronghua Wua73d9e02014-10-08 15:13:29 -0700676 }
Andy Hung73535852014-09-05 11:42:58 -0700677 }
Wei Jiaac428aa2014-09-02 19:01:34 -0700678
Andreas Huber57a339c2012-12-03 11:18:00 -0800679 // If there is a new surface texture, instantiate decoders
680 // again if possible.
681 mDeferredActions.push_back(
682 new SimpleAction(&NuPlayer::performScanSources));
683 }
684
Chong Zhangf8d71772014-11-26 15:08:34 -0800685 // After a flush without shutdown, decoder is paused.
686 // Don't resume it until source seek is done, otherwise it could
Chong Zhang7137ec72014-11-12 16:41:05 -0800687 // start pulling stale data too soon.
688 mDeferredActions.push_back(
Chong Zhangf8d71772014-11-26 15:08:34 -0800689 new ResumeDecoderAction(false /* needNotify */));
Chong Zhang7137ec72014-11-12 16:41:05 -0800690
Andreas Huber57a339c2012-12-03 11:18:00 -0800691 processDeferredActions();
Andreas Huberf9334412010-12-15 15:17:42 -0800692 break;
693 }
694
695 case kWhatSetAudioSink:
696 {
Steve Block3856b092011-10-20 11:56:00 +0100697 ALOGV("kWhatSetAudioSink");
Andreas Huberf9334412010-12-15 15:17:42 -0800698
699 sp<RefBase> obj;
700 CHECK(msg->findObject("sink", &obj));
701
702 mAudioSink = static_cast<MediaPlayerBase::AudioSink *>(obj.get());
703 break;
704 }
705
706 case kWhatStart:
707 {
Steve Block3856b092011-10-20 11:56:00 +0100708 ALOGV("kWhatStart");
Wei Jia94211742014-10-28 17:09:06 -0700709 if (mStarted) {
Chong Zhang8a048332015-05-06 15:16:28 -0700710 // do not resume yet if the source is still buffering
711 if (!mPausedForBuffering) {
712 onResume();
713 }
Wei Jia94211742014-10-28 17:09:06 -0700714 } else {
715 onStart();
Lajos Molnar09524832014-07-17 14:29:51 -0700716 }
Chong Zhangefbb6192015-01-30 17:13:27 -0800717 mPausedByClient = false;
Andreas Huberf9334412010-12-15 15:17:42 -0800718 break;
719 }
720
Lajos Molnar3a474aa2015-04-24 17:10:07 -0700721 case kWhatConfigPlayback:
Wei Jia98160162015-02-04 17:01:11 -0800722 {
Lajos Molnar3a474aa2015-04-24 17:10:07 -0700723 sp<AReplyToken> replyID;
724 CHECK(msg->senderAwaitsResponse(&replyID));
725 AudioPlaybackRate rate /* sanitized */;
726 readFromAMessage(msg, &rate);
727 status_t err = OK;
Wei Jia98160162015-02-04 17:01:11 -0800728 if (mRenderer != NULL) {
Lajos Molnar3a474aa2015-04-24 17:10:07 -0700729 err = mRenderer->setPlaybackSettings(rate);
730 }
731 if (err == OK) {
732 if (rate.mSpeed == 0.f) {
733 onPause();
734 // save all other settings (using non-paused speed)
735 // so we can restore them on start
736 AudioPlaybackRate newRate = rate;
737 newRate.mSpeed = mPlaybackSettings.mSpeed;
738 mPlaybackSettings = newRate;
739 } else { /* rate.mSpeed != 0.f */
740 onResume();
741 mPlaybackSettings = rate;
742 }
Wei Jia98160162015-02-04 17:01:11 -0800743 }
Ronghua Wu8db88132015-04-22 13:51:35 -0700744
745 if (mVideoDecoder != NULL) {
Ronghua Wuc8a70d32015-04-29 16:26:34 -0700746 float rate = getFrameRate();
747 if (rate > 0) {
Ronghua Wu8db88132015-04-22 13:51:35 -0700748 sp<AMessage> params = new AMessage();
Lajos Molnar3a474aa2015-04-24 17:10:07 -0700749 params->setFloat("operating-rate", rate * mPlaybackSettings.mSpeed);
Ronghua Wu8db88132015-04-22 13:51:35 -0700750 mVideoDecoder->setParameters(params);
751 }
752 }
753
Lajos Molnar3a474aa2015-04-24 17:10:07 -0700754 sp<AMessage> response = new AMessage;
755 response->setInt32("err", err);
756 response->postReply(replyID);
757 break;
758 }
759
760 case kWhatGetPlaybackSettings:
761 {
762 sp<AReplyToken> replyID;
763 CHECK(msg->senderAwaitsResponse(&replyID));
764 AudioPlaybackRate rate = mPlaybackSettings;
765 status_t err = OK;
766 if (mRenderer != NULL) {
767 err = mRenderer->getPlaybackSettings(&rate);
768 }
769 if (err == OK) {
770 // get playback settings used by renderer, as it may be
771 // slightly off due to audiosink not taking small changes.
772 mPlaybackSettings = rate;
773 if (mPaused) {
774 rate.mSpeed = 0.f;
775 }
776 }
777 sp<AMessage> response = new AMessage;
778 if (err == OK) {
779 writeToAMessage(response, rate);
780 }
781 response->setInt32("err", err);
782 response->postReply(replyID);
783 break;
784 }
785
786 case kWhatConfigSync:
787 {
788 sp<AReplyToken> replyID;
789 CHECK(msg->senderAwaitsResponse(&replyID));
790
791 ALOGV("kWhatConfigSync");
792 AVSyncSettings sync;
793 float videoFpsHint;
794 readFromAMessage(msg, &sync, &videoFpsHint);
795 status_t err = OK;
796 if (mRenderer != NULL) {
797 err = mRenderer->setSyncSettings(sync, videoFpsHint);
798 }
799 if (err == OK) {
800 mSyncSettings = sync;
801 mVideoFpsHint = videoFpsHint;
802 }
803 sp<AMessage> response = new AMessage;
804 response->setInt32("err", err);
805 response->postReply(replyID);
806 break;
807 }
808
809 case kWhatGetSyncSettings:
810 {
811 sp<AReplyToken> replyID;
812 CHECK(msg->senderAwaitsResponse(&replyID));
813 AVSyncSettings sync = mSyncSettings;
814 float videoFps = mVideoFpsHint;
815 status_t err = OK;
816 if (mRenderer != NULL) {
817 err = mRenderer->getSyncSettings(&sync, &videoFps);
818 if (err == OK) {
819 mSyncSettings = sync;
820 mVideoFpsHint = videoFps;
821 }
822 }
823 sp<AMessage> response = new AMessage;
824 if (err == OK) {
825 writeToAMessage(response, sync, videoFps);
826 }
827 response->setInt32("err", err);
828 response->postReply(replyID);
Wei Jia98160162015-02-04 17:01:11 -0800829 break;
830 }
831
Andreas Huberf9334412010-12-15 15:17:42 -0800832 case kWhatScanSources:
833 {
Andreas Huber1aef2112011-01-04 14:01:29 -0800834 int32_t generation;
835 CHECK(msg->findInt32("generation", &generation));
836 if (generation != mScanSourcesGeneration) {
837 // Drop obsolete msg.
838 break;
839 }
840
Andreas Huber5bc087c2010-12-23 10:27:40 -0800841 mScanSourcesPending = false;
842
Steve Block3856b092011-10-20 11:56:00 +0100843 ALOGV("scanning sources haveAudio=%d, haveVideo=%d",
Andreas Huber43c3e6c2011-01-05 12:17:08 -0800844 mAudioDecoder != NULL, mVideoDecoder != NULL);
845
Andreas Huberb7c8e912012-11-27 15:02:53 -0800846 bool mHadAnySourcesBefore =
847 (mAudioDecoder != NULL) || (mVideoDecoder != NULL);
Robert Shih7350b052015-10-01 15:50:14 -0700848 bool rescan = false;
Andreas Huberb7c8e912012-11-27 15:02:53 -0800849
Andy Hung282a7e32014-08-14 15:56:34 -0700850 // initialize video before audio because successful initialization of
851 // video may change deep buffer mode of audio.
Lajos Molnar1de1e252015-04-30 18:18:34 -0700852 if (mSurface != NULL) {
Robert Shih7350b052015-10-01 15:50:14 -0700853 if (instantiateDecoder(false, &mVideoDecoder) == -EWOULDBLOCK) {
854 rescan = true;
855 }
Haynes Mathew George5d246ef2012-07-09 10:36:57 -0700856 }
Andreas Huberf9334412010-12-15 15:17:42 -0800857
Ronghua Wua10fd232014-11-06 16:15:20 -0800858 // Don't try to re-open audio sink if there's an existing decoder.
859 if (mAudioSink != NULL && mAudioDecoder == NULL) {
Robert Shih7350b052015-10-01 15:50:14 -0700860 if (instantiateDecoder(true, &mAudioDecoder) == -EWOULDBLOCK) {
861 rescan = true;
862 }
Andreas Huberf9334412010-12-15 15:17:42 -0800863 }
864
Andreas Huberb7c8e912012-11-27 15:02:53 -0800865 if (!mHadAnySourcesBefore
866 && (mAudioDecoder != NULL || mVideoDecoder != NULL)) {
867 // This is the first time we've found anything playable.
868
Andreas Huber9575c962013-02-05 13:59:56 -0800869 if (mSourceFlags & Source::FLAG_DYNAMIC_DURATION) {
Andreas Huberb7c8e912012-11-27 15:02:53 -0800870 schedulePollDuration();
871 }
872 }
873
Andreas Hubereac68ba2011-09-27 12:12:25 -0700874 status_t err;
875 if ((err = mSource->feedMoreTSData()) != OK) {
Andreas Huber1aef2112011-01-04 14:01:29 -0800876 if (mAudioDecoder == NULL && mVideoDecoder == NULL) {
877 // We're not currently decoding anything (no audio or
878 // video tracks found) and we just ran out of input data.
Andreas Hubereac68ba2011-09-27 12:12:25 -0700879
880 if (err == ERROR_END_OF_STREAM) {
881 notifyListener(MEDIA_PLAYBACK_COMPLETE, 0, 0);
882 } else {
883 notifyListener(MEDIA_ERROR, MEDIA_ERROR_UNKNOWN, err);
884 }
Andreas Huber1aef2112011-01-04 14:01:29 -0800885 }
Andreas Huberf9334412010-12-15 15:17:42 -0800886 break;
887 }
888
Robert Shih7350b052015-10-01 15:50:14 -0700889 if (rescan) {
Andreas Huberf9334412010-12-15 15:17:42 -0800890 msg->post(100000ll);
Andreas Huber5bc087c2010-12-23 10:27:40 -0800891 mScanSourcesPending = true;
Andreas Huberf9334412010-12-15 15:17:42 -0800892 }
893 break;
894 }
895
896 case kWhatVideoNotify:
897 case kWhatAudioNotify:
898 {
899 bool audio = msg->what() == kWhatAudioNotify;
900
Wei Jia88703c32014-08-06 11:24:07 -0700901 int32_t currentDecoderGeneration =
902 (audio? mAudioDecoderGeneration : mVideoDecoderGeneration);
903 int32_t requesterGeneration = currentDecoderGeneration - 1;
904 CHECK(msg->findInt32("generation", &requesterGeneration));
905
906 if (requesterGeneration != currentDecoderGeneration) {
907 ALOGV("got message from old %s decoder, generation(%d:%d)",
908 audio ? "audio" : "video", requesterGeneration,
909 currentDecoderGeneration);
910 sp<AMessage> reply;
911 if (!(msg->findMessage("reply", &reply))) {
912 return;
913 }
914
915 reply->setInt32("err", INFO_DISCONTINUITY);
916 reply->post();
917 return;
918 }
919
Andreas Huberf9334412010-12-15 15:17:42 -0800920 int32_t what;
Lajos Molnar1cd13982014-01-17 15:12:51 -0800921 CHECK(msg->findInt32("what", &what));
Andreas Huberf9334412010-12-15 15:17:42 -0800922
Chong Zhang7137ec72014-11-12 16:41:05 -0800923 if (what == DecoderBase::kWhatInputDiscontinuity) {
924 int32_t formatChange;
925 CHECK(msg->findInt32("formatChange", &formatChange));
Andreas Huberf9334412010-12-15 15:17:42 -0800926
Chong Zhang7137ec72014-11-12 16:41:05 -0800927 ALOGV("%s discontinuity: formatChange %d",
928 audio ? "audio" : "video", formatChange);
929
930 if (formatChange) {
931 mDeferredActions.push_back(
932 new FlushDecoderAction(
933 audio ? FLUSH_CMD_SHUTDOWN : FLUSH_CMD_NONE,
934 audio ? FLUSH_CMD_NONE : FLUSH_CMD_SHUTDOWN));
Andreas Huberf9334412010-12-15 15:17:42 -0800935 }
Chong Zhang7137ec72014-11-12 16:41:05 -0800936
937 mDeferredActions.push_back(
938 new SimpleAction(
939 &NuPlayer::performScanSources));
940
941 processDeferredActions();
942 } else if (what == DecoderBase::kWhatEOS) {
Andreas Huberdc9bacd2011-09-26 10:53:29 -0700943 int32_t err;
Lajos Molnar1cd13982014-01-17 15:12:51 -0800944 CHECK(msg->findInt32("err", &err));
Andreas Huberdc9bacd2011-09-26 10:53:29 -0700945
946 if (err == ERROR_END_OF_STREAM) {
Steve Block3856b092011-10-20 11:56:00 +0100947 ALOGV("got %s decoder EOS", audio ? "audio" : "video");
Andreas Huberdc9bacd2011-09-26 10:53:29 -0700948 } else {
Steve Block3856b092011-10-20 11:56:00 +0100949 ALOGV("got %s decoder EOS w/ error %d",
Andreas Huberdc9bacd2011-09-26 10:53:29 -0700950 audio ? "audio" : "video",
951 err);
952 }
953
954 mRenderer->queueEOS(audio, err);
Chong Zhang7137ec72014-11-12 16:41:05 -0800955 } else if (what == DecoderBase::kWhatFlushCompleted) {
Steve Block3856b092011-10-20 11:56:00 +0100956 ALOGV("decoder %s flush completed", audio ? "audio" : "video");
Andreas Huberf9334412010-12-15 15:17:42 -0800957
Andy Hung8d121d42014-10-03 09:53:53 -0700958 handleFlushComplete(audio, true /* isDecoder */);
Andreas Huber3831a062010-12-21 10:22:33 -0800959 finishFlushIfPossible();
Chong Zhang7137ec72014-11-12 16:41:05 -0800960 } else if (what == DecoderBase::kWhatVideoSizeChanged) {
Lajos Molnar1cd13982014-01-17 15:12:51 -0800961 sp<AMessage> format;
962 CHECK(msg->findMessage("format", &format));
963
Wei Jiac6cfd702014-11-11 16:33:20 -0800964 sp<AMessage> inputFormat =
965 mSource->getFormat(false /* audio */);
Andreas Huber3831a062010-12-21 10:22:33 -0800966
Wei Jiac6cfd702014-11-11 16:33:20 -0800967 updateVideoSize(inputFormat, format);
Chong Zhang7137ec72014-11-12 16:41:05 -0800968 } else if (what == DecoderBase::kWhatShutdownCompleted) {
Steve Block3856b092011-10-20 11:56:00 +0100969 ALOGV("%s shutdown completed", audio ? "audio" : "video");
Andreas Huber3831a062010-12-21 10:22:33 -0800970 if (audio) {
971 mAudioDecoder.clear();
Wei Jia57568df2014-09-22 10:16:29 -0700972 ++mAudioDecoderGeneration;
Andreas Huber3831a062010-12-21 10:22:33 -0800973
974 CHECK_EQ((int)mFlushingAudio, (int)SHUTTING_DOWN_DECODER);
975 mFlushingAudio = SHUT_DOWN;
976 } else {
977 mVideoDecoder.clear();
Wei Jia57568df2014-09-22 10:16:29 -0700978 ++mVideoDecoderGeneration;
Andreas Huber3831a062010-12-21 10:22:33 -0800979
980 CHECK_EQ((int)mFlushingVideo, (int)SHUTTING_DOWN_DECODER);
981 mFlushingVideo = SHUT_DOWN;
982 }
983
984 finishFlushIfPossible();
Chong Zhangf8d71772014-11-26 15:08:34 -0800985 } else if (what == DecoderBase::kWhatResumeCompleted) {
986 finishResume();
Chong Zhang7137ec72014-11-12 16:41:05 -0800987 } else if (what == DecoderBase::kWhatError) {
Chong Zhangf4c0a942014-08-11 15:14:10 -0700988 status_t err;
Andy Hung2abde2c2014-09-30 14:40:32 -0700989 if (!msg->findInt32("err", &err) || err == OK) {
Chong Zhangf4c0a942014-08-11 15:14:10 -0700990 err = UNKNOWN_ERROR;
991 }
Andy Hungcf31f1e2014-09-23 14:59:01 -0700992
Andy Hung2abde2c2014-09-30 14:40:32 -0700993 // Decoder errors can be due to Source (e.g. from streaming),
994 // or from decoding corrupted bitstreams, or from other decoder
995 // MediaCodec operations (e.g. from an ongoing reset or seek).
Andy Hung202bce12014-12-03 11:47:36 -0800996 // They may also be due to openAudioSink failure at
997 // decoder start or after a format change.
Andy Hung2abde2c2014-09-30 14:40:32 -0700998 //
999 // We try to gracefully shut down the affected decoder if possible,
1000 // rather than trying to force the shutdown with something
1001 // similar to performReset(). This method can lead to a hang
1002 // if MediaCodec functions block after an error, but they should
1003 // typically return INVALID_OPERATION instead of blocking.
1004
1005 FlushStatus *flushing = audio ? &mFlushingAudio : &mFlushingVideo;
1006 ALOGE("received error(%#x) from %s decoder, flushing(%d), now shutting down",
1007 err, audio ? "audio" : "video", *flushing);
1008
1009 switch (*flushing) {
1010 case NONE:
1011 mDeferredActions.push_back(
Wei Jiafef808d2014-10-31 17:57:05 -07001012 new FlushDecoderAction(
1013 audio ? FLUSH_CMD_SHUTDOWN : FLUSH_CMD_NONE,
1014 audio ? FLUSH_CMD_NONE : FLUSH_CMD_SHUTDOWN));
Andy Hung2abde2c2014-09-30 14:40:32 -07001015 processDeferredActions();
1016 break;
1017 case FLUSHING_DECODER:
1018 *flushing = FLUSHING_DECODER_SHUTDOWN; // initiate shutdown after flush.
1019 break; // Wait for flush to complete.
1020 case FLUSHING_DECODER_SHUTDOWN:
1021 break; // Wait for flush to complete.
1022 case SHUTTING_DOWN_DECODER:
1023 break; // Wait for shutdown to complete.
1024 case FLUSHED:
1025 // Widevine source reads must stop before releasing the video decoder.
1026 if (!audio && mSource != NULL && mSourceFlags & Source::FLAG_SECURE) {
1027 mSource->stop();
Robert Shih0c61a0d2015-07-06 15:09:10 -07001028 mSourceStarted = false;
Andy Hung2abde2c2014-09-30 14:40:32 -07001029 }
1030 getDecoder(audio)->initiateShutdown(); // In the middle of a seek.
1031 *flushing = SHUTTING_DOWN_DECODER; // Shut down.
1032 break;
1033 case SHUT_DOWN:
1034 finishFlushIfPossible(); // Should not occur.
1035 break; // Finish anyways.
Marco Nelissen9e2b7912014-08-18 16:13:03 -07001036 }
Andy Hung2abde2c2014-09-30 14:40:32 -07001037 notifyListener(MEDIA_ERROR, MEDIA_ERROR_UNKNOWN, err);
Lajos Molnar1cd13982014-01-17 15:12:51 -08001038 } else {
1039 ALOGV("Unhandled decoder notification %d '%c%c%c%c'.",
Andreas Hubera1f8ab02012-11-30 10:53:22 -08001040 what,
1041 what >> 24,
1042 (what >> 16) & 0xff,
1043 (what >> 8) & 0xff,
1044 what & 0xff);
Andreas Huberf9334412010-12-15 15:17:42 -08001045 }
1046
1047 break;
1048 }
1049
1050 case kWhatRendererNotify:
1051 {
Wei Jia57568df2014-09-22 10:16:29 -07001052 int32_t requesterGeneration = mRendererGeneration - 1;
1053 CHECK(msg->findInt32("generation", &requesterGeneration));
1054 if (requesterGeneration != mRendererGeneration) {
1055 ALOGV("got message from old renderer, generation(%d:%d)",
1056 requesterGeneration, mRendererGeneration);
1057 return;
1058 }
1059
Andreas Huberf9334412010-12-15 15:17:42 -08001060 int32_t what;
1061 CHECK(msg->findInt32("what", &what));
1062
1063 if (what == Renderer::kWhatEOS) {
1064 int32_t audio;
1065 CHECK(msg->findInt32("audio", &audio));
1066
Andreas Huberc92fd242011-08-16 13:48:44 -07001067 int32_t finalResult;
1068 CHECK(msg->findInt32("finalResult", &finalResult));
1069
Andreas Huberf9334412010-12-15 15:17:42 -08001070 if (audio) {
1071 mAudioEOS = true;
1072 } else {
1073 mVideoEOS = true;
1074 }
1075
Andreas Huberc92fd242011-08-16 13:48:44 -07001076 if (finalResult == ERROR_END_OF_STREAM) {
Steve Block3856b092011-10-20 11:56:00 +01001077 ALOGV("reached %s EOS", audio ? "audio" : "video");
Andreas Huberc92fd242011-08-16 13:48:44 -07001078 } else {
Steve Block29357bc2012-01-06 19:20:56 +00001079 ALOGE("%s track encountered an error (%d)",
Andreas Huberc92fd242011-08-16 13:48:44 -07001080 audio ? "audio" : "video", finalResult);
1081
1082 notifyListener(
1083 MEDIA_ERROR, MEDIA_ERROR_UNKNOWN, finalResult);
1084 }
Andreas Huberf9334412010-12-15 15:17:42 -08001085
1086 if ((mAudioEOS || mAudioDecoder == NULL)
1087 && (mVideoEOS || mVideoDecoder == NULL)) {
1088 notifyListener(MEDIA_PLAYBACK_COMPLETE, 0, 0);
1089 }
Andreas Huber3fe62152011-09-16 15:09:22 -07001090 } else if (what == Renderer::kWhatFlushComplete) {
Andreas Huberf9334412010-12-15 15:17:42 -08001091 int32_t audio;
1092 CHECK(msg->findInt32("audio", &audio));
1093
Wei Jia3261f0d2015-10-08 13:58:33 -07001094 if (audio) {
1095 mAudioEOS = false;
1096 } else {
1097 mVideoEOS = false;
1098 }
1099
Steve Block3856b092011-10-20 11:56:00 +01001100 ALOGV("renderer %s flush completed.", audio ? "audio" : "video");
Wei Jiada4252f2015-07-14 18:15:28 -07001101 if (audio && (mFlushingAudio == NONE || mFlushingAudio == FLUSHED
1102 || mFlushingAudio == SHUT_DOWN)) {
1103 // Flush has been handled by tear down.
1104 break;
1105 }
Andy Hung8d121d42014-10-03 09:53:53 -07001106 handleFlushComplete(audio, false /* isDecoder */);
1107 finishFlushIfPossible();
James Dongf57b4ea2012-07-20 13:38:36 -07001108 } else if (what == Renderer::kWhatVideoRenderingStart) {
1109 notifyListener(MEDIA_INFO, MEDIA_INFO_RENDERING_START, 0);
Lajos Molnarcbaffcf2013-08-14 18:30:38 -07001110 } else if (what == Renderer::kWhatMediaRenderingStart) {
1111 ALOGV("media rendering started");
1112 notifyListener(MEDIA_STARTED, 0, 0);
Ronghua Wufaeb0f22015-05-21 12:20:21 -07001113 } else if (what == Renderer::kWhatAudioTearDown) {
Ronghua Wu08529172014-10-02 16:55:52 -07001114 int32_t reason;
1115 CHECK(msg->findInt32("reason", &reason));
Ronghua Wufaeb0f22015-05-21 12:20:21 -07001116 ALOGV("Tear down audio with reason %d.", reason);
Wei Jia3bc66702015-11-18 15:45:06 -08001117 mAudioDecoder->pause();
Wei Jia3a2956d2014-07-22 16:01:33 -07001118 mAudioDecoder.clear();
Wei Jia57568df2014-09-22 10:16:29 -07001119 ++mAudioDecoderGeneration;
Wei Jiada4252f2015-07-14 18:15:28 -07001120 bool needsToCreateAudioDecoder = true;
1121 if (mFlushingAudio == FLUSHING_DECODER) {
1122 mFlushComplete[1 /* audio */][1 /* isDecoder */] = true;
1123 mFlushingAudio = FLUSHED;
1124 finishFlushIfPossible();
1125 } else if (mFlushingAudio == FLUSHING_DECODER_SHUTDOWN
1126 || mFlushingAudio == SHUTTING_DOWN_DECODER) {
1127 mFlushComplete[1 /* audio */][1 /* isDecoder */] = true;
1128 mFlushingAudio = SHUT_DOWN;
1129 finishFlushIfPossible();
1130 needsToCreateAudioDecoder = false;
1131 }
1132 if (mRenderer == NULL) {
1133 break;
1134 }
1135 closeAudioSink();
Chong Zhang7137ec72014-11-12 16:41:05 -08001136 mRenderer->flush(
1137 true /* audio */, false /* notifyComplete */);
Wei Jia3a2956d2014-07-22 16:01:33 -07001138 if (mVideoDecoder != NULL) {
Chong Zhang7137ec72014-11-12 16:41:05 -08001139 mRenderer->flush(
1140 false /* audio */, false /* notifyComplete */);
Wei Jia3a2956d2014-07-22 16:01:33 -07001141 }
Wei Jia3a2956d2014-07-22 16:01:33 -07001142
Wei Jiada4252f2015-07-14 18:15:28 -07001143 int64_t positionUs;
Robert Shih1a5c8592015-08-04 18:07:44 -07001144 if (!msg->findInt64("positionUs", &positionUs)) {
1145 positionUs = mPreviousSeekTimeUs;
1146 }
Wei Jia29840802015-05-15 17:11:38 -07001147 performSeek(positionUs);
Ronghua Wufaeb0f22015-05-21 12:20:21 -07001148
Wei Jiada4252f2015-07-14 18:15:28 -07001149 if (reason == Renderer::kDueToError && needsToCreateAudioDecoder) {
Ronghua Wu08529172014-10-02 16:55:52 -07001150 instantiateDecoder(true /* audio */, &mAudioDecoder);
1151 }
Andreas Huberf9334412010-12-15 15:17:42 -08001152 }
1153 break;
1154 }
1155
1156 case kWhatMoreDataQueued:
1157 {
1158 break;
1159 }
1160
Andreas Huber1aef2112011-01-04 14:01:29 -08001161 case kWhatReset:
1162 {
Steve Block3856b092011-10-20 11:56:00 +01001163 ALOGV("kWhatReset");
Andreas Huber1aef2112011-01-04 14:01:29 -08001164
Ronghua Wu64c2d172015-10-07 16:52:19 -07001165 mResetting = true;
1166
Andreas Hubera1f8ab02012-11-30 10:53:22 -08001167 mDeferredActions.push_back(
Wei Jiafef808d2014-10-31 17:57:05 -07001168 new FlushDecoderAction(
1169 FLUSH_CMD_SHUTDOWN /* audio */,
1170 FLUSH_CMD_SHUTDOWN /* video */));
Andreas Huberb7c8e912012-11-27 15:02:53 -08001171
Andreas Hubera1f8ab02012-11-30 10:53:22 -08001172 mDeferredActions.push_back(
1173 new SimpleAction(&NuPlayer::performReset));
Andreas Huberb58ce9f2011-11-28 16:27:35 -08001174
Andreas Hubera1f8ab02012-11-30 10:53:22 -08001175 processDeferredActions();
Andreas Huber1aef2112011-01-04 14:01:29 -08001176 break;
1177 }
1178
Andreas Huber43c3e6c2011-01-05 12:17:08 -08001179 case kWhatSeek:
1180 {
1181 int64_t seekTimeUs;
Wei Jiae427abf2014-09-22 15:21:11 -07001182 int32_t needNotify;
Andreas Huber43c3e6c2011-01-05 12:17:08 -08001183 CHECK(msg->findInt64("seekTimeUs", &seekTimeUs));
Wei Jiae427abf2014-09-22 15:21:11 -07001184 CHECK(msg->findInt32("needNotify", &needNotify));
Andreas Huber43c3e6c2011-01-05 12:17:08 -08001185
Wei Jiae427abf2014-09-22 15:21:11 -07001186 ALOGV("kWhatSeek seekTimeUs=%lld us, needNotify=%d",
Lajos Molnar6d339f12015-04-17 16:15:53 -07001187 (long long)seekTimeUs, needNotify);
Andreas Huber43c3e6c2011-01-05 12:17:08 -08001188
Wei Jia1061c9c2015-05-19 16:02:17 -07001189 if (!mStarted) {
1190 // Seek before the player is started. In order to preview video,
1191 // need to start the player and pause it. This branch is called
1192 // only once if needed. After the player is started, any seek
1193 // operation will go through normal path.
Robert Shih0c61a0d2015-07-06 15:09:10 -07001194 // Audio-only cases are handled separately.
Wei Jia1061c9c2015-05-19 16:02:17 -07001195 onStart(seekTimeUs);
Robert Shih0c61a0d2015-07-06 15:09:10 -07001196 if (mStarted) {
1197 onPause();
1198 mPausedByClient = true;
1199 }
Wei Jia1061c9c2015-05-19 16:02:17 -07001200 if (needNotify) {
1201 notifyDriverSeekComplete();
1202 }
1203 break;
1204 }
1205
Andreas Hubera1f8ab02012-11-30 10:53:22 -08001206 mDeferredActions.push_back(
Wei Jiafef808d2014-10-31 17:57:05 -07001207 new FlushDecoderAction(FLUSH_CMD_FLUSH /* audio */,
1208 FLUSH_CMD_FLUSH /* video */));
Andreas Huber43c3e6c2011-01-05 12:17:08 -08001209
Wei Jiae427abf2014-09-22 15:21:11 -07001210 mDeferredActions.push_back(
Wei Jia29840802015-05-15 17:11:38 -07001211 new SeekAction(seekTimeUs));
Andreas Huber43c3e6c2011-01-05 12:17:08 -08001212
Chong Zhangf8d71772014-11-26 15:08:34 -08001213 // After a flush without shutdown, decoder is paused.
1214 // Don't resume it until source seek is done, otherwise it could
Chong Zhang7137ec72014-11-12 16:41:05 -08001215 // start pulling stale data too soon.
1216 mDeferredActions.push_back(
Chong Zhangf8d71772014-11-26 15:08:34 -08001217 new ResumeDecoderAction(needNotify));
Chong Zhang7137ec72014-11-12 16:41:05 -08001218
Andreas Hubera1f8ab02012-11-30 10:53:22 -08001219 processDeferredActions();
Andreas Huber43c3e6c2011-01-05 12:17:08 -08001220 break;
1221 }
1222
Andreas Huberb4082222011-01-20 15:23:04 -08001223 case kWhatPause:
1224 {
Chong Zhangefbb6192015-01-30 17:13:27 -08001225 onPause();
1226 mPausedByClient = true;
Andreas Huberb4082222011-01-20 15:23:04 -08001227 break;
1228 }
1229
Andreas Huberb5f25f02013-02-05 10:14:26 -08001230 case kWhatSourceNotify:
1231 {
Andreas Huber9575c962013-02-05 13:59:56 -08001232 onSourceNotify(msg);
Andreas Huberb5f25f02013-02-05 10:14:26 -08001233 break;
1234 }
1235
Chong Zhanga7fa1d92014-06-11 14:49:23 -07001236 case kWhatClosedCaptionNotify:
1237 {
1238 onClosedCaptionNotify(msg);
1239 break;
1240 }
1241
Andreas Huberf9334412010-12-15 15:17:42 -08001242 default:
1243 TRESPASS();
1244 break;
1245 }
1246}
1247
Wei Jia94211742014-10-28 17:09:06 -07001248void NuPlayer::onResume() {
Ronghua Wu64c2d172015-10-07 16:52:19 -07001249 if (!mPaused || mResetting) {
1250 ALOGD_IF(mResetting, "resetting, onResume discarded");
Chong Zhangefbb6192015-01-30 17:13:27 -08001251 return;
1252 }
1253 mPaused = false;
Wei Jia94211742014-10-28 17:09:06 -07001254 if (mSource != NULL) {
1255 mSource->resume();
1256 } else {
1257 ALOGW("resume called when source is gone or not set");
1258 }
1259 // |mAudioDecoder| may have been released due to the pause timeout, so re-create it if
1260 // needed.
1261 if (audioDecoderStillNeeded() && mAudioDecoder == NULL) {
1262 instantiateDecoder(true /* audio */, &mAudioDecoder);
1263 }
1264 if (mRenderer != NULL) {
1265 mRenderer->resume();
1266 } else {
1267 ALOGW("resume called when renderer is gone or not set");
1268 }
1269}
1270
Lajos Molnarfcd3e942015-03-31 10:06:48 -07001271status_t NuPlayer::onInstantiateSecureDecoders() {
1272 status_t err;
1273 if (!(mSourceFlags & Source::FLAG_SECURE)) {
1274 return BAD_TYPE;
1275 }
1276
1277 if (mRenderer != NULL) {
1278 ALOGE("renderer should not be set when instantiating secure decoders");
1279 return UNKNOWN_ERROR;
1280 }
1281
1282 // TRICKY: We rely on mRenderer being null, so that decoder does not start requesting
1283 // data on instantiation.
Lajos Molnar1de1e252015-04-30 18:18:34 -07001284 if (mSurface != NULL) {
Lajos Molnarfcd3e942015-03-31 10:06:48 -07001285 err = instantiateDecoder(false, &mVideoDecoder);
1286 if (err != OK) {
1287 return err;
1288 }
1289 }
1290
1291 if (mAudioSink != NULL) {
1292 err = instantiateDecoder(true, &mAudioDecoder);
1293 if (err != OK) {
1294 return err;
1295 }
1296 }
1297 return OK;
1298}
1299
Wei Jia1061c9c2015-05-19 16:02:17 -07001300void NuPlayer::onStart(int64_t startPositionUs) {
Robert Shih0c61a0d2015-07-06 15:09:10 -07001301 if (!mSourceStarted) {
1302 mSourceStarted = true;
1303 mSource->start();
1304 }
1305 if (startPositionUs > 0) {
1306 performSeek(startPositionUs);
1307 if (mSource->getFormat(false /* audio */) == NULL) {
1308 return;
1309 }
1310 }
1311
Wei Jia94211742014-10-28 17:09:06 -07001312 mOffloadAudio = false;
1313 mAudioEOS = false;
1314 mVideoEOS = false;
Wei Jia94211742014-10-28 17:09:06 -07001315 mStarted = true;
1316
Wei Jia94211742014-10-28 17:09:06 -07001317 uint32_t flags = 0;
1318
1319 if (mSource->isRealTime()) {
1320 flags |= Renderer::FLAG_REAL_TIME;
1321 }
1322
1323 sp<MetaData> audioMeta = mSource->getFormatMeta(true /* audio */);
Andy Hung12a84132015-10-20 16:09:21 -07001324 ALOGV_IF(audioMeta == NULL, "no metadata for audio source"); // video only stream
Wei Jia94211742014-10-28 17:09:06 -07001325 audio_stream_type_t streamType = AUDIO_STREAM_MUSIC;
1326 if (mAudioSink != NULL) {
1327 streamType = mAudioSink->getAudioStreamType();
1328 }
1329
1330 sp<AMessage> videoFormat = mSource->getFormat(false /* audio */);
1331
1332 mOffloadAudio =
Ronghua Wu02cb98d2015-05-27 11:02:54 -07001333 canOffloadStream(audioMeta, (videoFormat != NULL), mSource->isStreaming(), streamType);
Wei Jia94211742014-10-28 17:09:06 -07001334 if (mOffloadAudio) {
1335 flags |= Renderer::FLAG_OFFLOAD_AUDIO;
1336 }
1337
Lajos Molnar1d15ab52015-03-04 16:46:34 -08001338 sp<AMessage> notify = new AMessage(kWhatRendererNotify, this);
Wei Jia94211742014-10-28 17:09:06 -07001339 ++mRendererGeneration;
1340 notify->setInt32("generation", mRendererGeneration);
1341 mRenderer = new Renderer(mAudioSink, notify, flags);
Wei Jia94211742014-10-28 17:09:06 -07001342 mRendererLooper = new ALooper;
1343 mRendererLooper->setName("NuPlayerRenderer");
1344 mRendererLooper->start(false, false, ANDROID_PRIORITY_AUDIO);
1345 mRendererLooper->registerHandler(mRenderer);
Lajos Molnar3a474aa2015-04-24 17:10:07 -07001346
1347 status_t err = mRenderer->setPlaybackSettings(mPlaybackSettings);
1348 if (err != OK) {
1349 mSource->stop();
Robert Shih0c61a0d2015-07-06 15:09:10 -07001350 mSourceStarted = false;
Lajos Molnar3a474aa2015-04-24 17:10:07 -07001351 notifyListener(MEDIA_ERROR, MEDIA_ERROR_UNKNOWN, err);
1352 return;
Wei Jiac8206ff2015-03-04 13:59:37 -08001353 }
Wei Jia94211742014-10-28 17:09:06 -07001354
Ronghua Wuc8a70d32015-04-29 16:26:34 -07001355 float rate = getFrameRate();
1356 if (rate > 0) {
Wei Jia94211742014-10-28 17:09:06 -07001357 mRenderer->setVideoFrameRate(rate);
1358 }
1359
Wei Jiac6cfd702014-11-11 16:33:20 -08001360 if (mVideoDecoder != NULL) {
1361 mVideoDecoder->setRenderer(mRenderer);
1362 }
1363 if (mAudioDecoder != NULL) {
1364 mAudioDecoder->setRenderer(mRenderer);
1365 }
1366
Wei Jia94211742014-10-28 17:09:06 -07001367 postScanSources();
1368}
1369
Chong Zhangefbb6192015-01-30 17:13:27 -08001370void NuPlayer::onPause() {
1371 if (mPaused) {
1372 return;
1373 }
1374 mPaused = true;
1375 if (mSource != NULL) {
1376 mSource->pause();
1377 } else {
1378 ALOGW("pause called when source is gone or not set");
1379 }
1380 if (mRenderer != NULL) {
1381 mRenderer->pause();
1382 } else {
1383 ALOGW("pause called when renderer is gone or not set");
1384 }
1385}
1386
Ronghua Wud7988b12014-10-03 15:19:10 -07001387bool NuPlayer::audioDecoderStillNeeded() {
1388 // Audio decoder is no longer needed if it's in shut/shutting down status.
1389 return ((mFlushingAudio != SHUT_DOWN) && (mFlushingAudio != SHUTTING_DOWN_DECODER));
1390}
1391
Andy Hung8d121d42014-10-03 09:53:53 -07001392void NuPlayer::handleFlushComplete(bool audio, bool isDecoder) {
1393 // We wait for both the decoder flush and the renderer flush to complete
1394 // before entering either the FLUSHED or the SHUTTING_DOWN_DECODER state.
1395
1396 mFlushComplete[audio][isDecoder] = true;
1397 if (!mFlushComplete[audio][!isDecoder]) {
1398 return;
1399 }
1400
1401 FlushStatus *state = audio ? &mFlushingAudio : &mFlushingVideo;
1402 switch (*state) {
1403 case FLUSHING_DECODER:
1404 {
1405 *state = FLUSHED;
Andy Hung8d121d42014-10-03 09:53:53 -07001406 break;
1407 }
1408
1409 case FLUSHING_DECODER_SHUTDOWN:
1410 {
1411 *state = SHUTTING_DOWN_DECODER;
1412
1413 ALOGV("initiating %s decoder shutdown", audio ? "audio" : "video");
1414 if (!audio) {
Andy Hung8d121d42014-10-03 09:53:53 -07001415 // Widevine source reads must stop before releasing the video decoder.
1416 if (mSource != NULL && mSourceFlags & Source::FLAG_SECURE) {
1417 mSource->stop();
Robert Shih0c61a0d2015-07-06 15:09:10 -07001418 mSourceStarted = false;
Andy Hung8d121d42014-10-03 09:53:53 -07001419 }
1420 }
1421 getDecoder(audio)->initiateShutdown();
1422 break;
1423 }
1424
1425 default:
1426 // decoder flush completes only occur in a flushing state.
1427 LOG_ALWAYS_FATAL_IF(isDecoder, "decoder flush in invalid state %d", *state);
1428 break;
1429 }
1430}
1431
Andreas Huber3831a062010-12-21 10:22:33 -08001432void NuPlayer::finishFlushIfPossible() {
Wei Jia53904f32014-07-29 10:22:53 -07001433 if (mFlushingAudio != NONE && mFlushingAudio != FLUSHED
1434 && mFlushingAudio != SHUT_DOWN) {
Andreas Huber3831a062010-12-21 10:22:33 -08001435 return;
1436 }
1437
Wei Jia53904f32014-07-29 10:22:53 -07001438 if (mFlushingVideo != NONE && mFlushingVideo != FLUSHED
1439 && mFlushingVideo != SHUT_DOWN) {
Andreas Huber3831a062010-12-21 10:22:33 -08001440 return;
1441 }
1442
Steve Block3856b092011-10-20 11:56:00 +01001443 ALOGV("both audio and video are flushed now.");
Andreas Huber3831a062010-12-21 10:22:33 -08001444
Andreas Huber3831a062010-12-21 10:22:33 -08001445 mFlushingAudio = NONE;
1446 mFlushingVideo = NONE;
Andreas Huber3831a062010-12-21 10:22:33 -08001447
Andy Hung8d121d42014-10-03 09:53:53 -07001448 clearFlushComplete();
1449
Andreas Hubera1f8ab02012-11-30 10:53:22 -08001450 processDeferredActions();
Andreas Huber1aef2112011-01-04 14:01:29 -08001451}
1452
1453void NuPlayer::postScanSources() {
1454 if (mScanSourcesPending) {
1455 return;
1456 }
1457
Lajos Molnar1d15ab52015-03-04 16:46:34 -08001458 sp<AMessage> msg = new AMessage(kWhatScanSources, this);
Andreas Huber1aef2112011-01-04 14:01:29 -08001459 msg->setInt32("generation", mScanSourcesGeneration);
1460 msg->post();
1461
1462 mScanSourcesPending = true;
1463}
1464
Andy Hung202bce12014-12-03 11:47:36 -08001465void NuPlayer::tryOpenAudioSinkForOffload(const sp<AMessage> &format, bool hasVideo) {
1466 // Note: This is called early in NuPlayer to determine whether offloading
1467 // is possible; otherwise the decoders call the renderer openAudioSink directly.
Andy Hung282a7e32014-08-14 15:56:34 -07001468
Andy Hung202bce12014-12-03 11:47:36 -08001469 status_t err = mRenderer->openAudioSink(
1470 format, true /* offloadOnly */, hasVideo, AUDIO_OUTPUT_FLAG_NONE, &mOffloadAudio);
1471 if (err != OK) {
1472 // Any failure we turn off mOffloadAudio.
1473 mOffloadAudio = false;
1474 } else if (mOffloadAudio) {
Chong Zhang3b9eb1f2014-10-15 17:05:08 -07001475 sp<MetaData> audioMeta =
1476 mSource->getFormatMeta(true /* audio */);
1477 sendMetaDataToHal(mAudioSink, audioMeta);
Andy Hung282a7e32014-08-14 15:56:34 -07001478 }
1479}
1480
1481void NuPlayer::closeAudioSink() {
Chong Zhang3b9eb1f2014-10-15 17:05:08 -07001482 mRenderer->closeAudioSink();
Andy Hung282a7e32014-08-14 15:56:34 -07001483}
1484
Wei Jiae4d18c72015-07-13 17:58:11 -07001485void NuPlayer::determineAudioModeChange() {
1486 if (mSource == NULL || mAudioSink == NULL) {
1487 return;
1488 }
1489
1490 if (mRenderer == NULL) {
1491 ALOGW("No renderer can be used to determine audio mode. Use non-offload for safety.");
1492 mOffloadAudio = false;
1493 return;
1494 }
1495
1496 sp<MetaData> audioMeta = mSource->getFormatMeta(true /* audio */);
1497 sp<AMessage> videoFormat = mSource->getFormat(false /* audio */);
1498 audio_stream_type_t streamType = mAudioSink->getAudioStreamType();
1499 const bool hasVideo = (videoFormat != NULL);
1500 const bool canOffload = canOffloadStream(
1501 audioMeta, hasVideo, mSource->isStreaming(), streamType);
1502 if (canOffload) {
1503 if (!mOffloadAudio) {
1504 mRenderer->signalEnableOffloadAudio();
1505 }
1506 // open audio sink early under offload mode.
1507 sp<AMessage> format = mSource->getFormat(true /*audio*/);
1508 tryOpenAudioSinkForOffload(format, hasVideo);
1509 } else {
Robert Shihe1d70192015-07-23 17:54:13 -07001510 if (mOffloadAudio) {
1511 mRenderer->signalDisableOffloadAudio();
1512 mOffloadAudio = false;
1513 }
Wei Jiae4d18c72015-07-13 17:58:11 -07001514 }
1515}
1516
Chong Zhang7137ec72014-11-12 16:41:05 -08001517status_t NuPlayer::instantiateDecoder(bool audio, sp<DecoderBase> *decoder) {
Wei Jia566da802015-08-27 13:59:30 -07001518 // The audio decoder could be cleared by tear down. If still in shut down
1519 // process, no need to create a new audio decoder.
1520 if (*decoder != NULL || (audio && mFlushingAudio == SHUT_DOWN)) {
Andreas Huberf9334412010-12-15 15:17:42 -08001521 return OK;
1522 }
1523
Andreas Huber84066782011-08-16 09:34:26 -07001524 sp<AMessage> format = mSource->getFormat(audio);
Andreas Huberf9334412010-12-15 15:17:42 -08001525
Andreas Huber84066782011-08-16 09:34:26 -07001526 if (format == NULL) {
Robert Shih7350b052015-10-01 15:50:14 -07001527 return UNKNOWN_ERROR;
1528 } else {
1529 status_t err;
1530 if (format->findInt32("err", &err) && err) {
1531 return err;
1532 }
Andreas Huberf9334412010-12-15 15:17:42 -08001533 }
1534
Ronghua Wu8db88132015-04-22 13:51:35 -07001535 format->setInt32("priority", 0 /* realtime */);
1536
Andreas Huber3fe62152011-09-16 15:09:22 -07001537 if (!audio) {
Andreas Huber84066782011-08-16 09:34:26 -07001538 AString mime;
1539 CHECK(format->findString("mime", &mime));
Chong Zhanga7fa1d92014-06-11 14:49:23 -07001540
Lajos Molnar1d15ab52015-03-04 16:46:34 -08001541 sp<AMessage> ccNotify = new AMessage(kWhatClosedCaptionNotify, this);
Chong Zhang341ab6e2015-02-04 13:37:18 -08001542 if (mCCDecoder == NULL) {
1543 mCCDecoder = new CCDecoder(ccNotify);
1544 }
Lajos Molnar09524832014-07-17 14:29:51 -07001545
1546 if (mSourceFlags & Source::FLAG_SECURE) {
1547 format->setInt32("secure", true);
1548 }
Chong Zhang17134602015-01-07 16:14:34 -08001549
1550 if (mSourceFlags & Source::FLAG_PROTECTED) {
1551 format->setInt32("protected", true);
1552 }
Ronghua Wu8db88132015-04-22 13:51:35 -07001553
Ronghua Wuc8a70d32015-04-29 16:26:34 -07001554 float rate = getFrameRate();
1555 if (rate > 0) {
Lajos Molnar3a474aa2015-04-24 17:10:07 -07001556 format->setFloat("operating-rate", rate * mPlaybackSettings.mSpeed);
Ronghua Wu8db88132015-04-22 13:51:35 -07001557 }
Andreas Huber3fe62152011-09-16 15:09:22 -07001558 }
1559
Wei Jiabc2fb722014-07-08 16:37:57 -07001560 if (audio) {
Lajos Molnar1d15ab52015-03-04 16:46:34 -08001561 sp<AMessage> notify = new AMessage(kWhatAudioNotify, this);
Wei Jia88703c32014-08-06 11:24:07 -07001562 ++mAudioDecoderGeneration;
1563 notify->setInt32("generation", mAudioDecoderGeneration);
1564
Wei Jiae4d18c72015-07-13 17:58:11 -07001565 determineAudioModeChange();
Wei Jiabc2fb722014-07-08 16:37:57 -07001566 if (mOffloadAudio) {
Wei Jia14532f22015-12-29 11:28:15 -08001567 mSource->setOffloadAudio(true /* offload */);
1568
Haynes Mathew George8b635332015-03-30 17:59:47 -07001569 const bool hasVideo = (mSource->getFormat(false /*audio */) != NULL);
1570 format->setInt32("has-video", hasVideo);
Wei Jiac6cfd702014-11-11 16:33:20 -08001571 *decoder = new DecoderPassThrough(notify, mSource, mRenderer);
Wei Jiabc2fb722014-07-08 16:37:57 -07001572 } else {
Wei Jia14532f22015-12-29 11:28:15 -08001573 mSource->setOffloadAudio(false /* offload */);
1574
Ronghua Wu68845c12015-07-21 09:50:48 -07001575 *decoder = new Decoder(notify, mSource, mPID, mRenderer);
Wei Jiabc2fb722014-07-08 16:37:57 -07001576 }
1577 } else {
Lajos Molnar1d15ab52015-03-04 16:46:34 -08001578 sp<AMessage> notify = new AMessage(kWhatVideoNotify, this);
Wei Jia88703c32014-08-06 11:24:07 -07001579 ++mVideoDecoderGeneration;
1580 notify->setInt32("generation", mVideoDecoderGeneration);
1581
Chong Zhang7137ec72014-11-12 16:41:05 -08001582 *decoder = new Decoder(
Ronghua Wu68845c12015-07-21 09:50:48 -07001583 notify, mSource, mPID, mRenderer, mSurface, mCCDecoder);
Lajos Molnard9fd6312014-11-06 11:00:00 -08001584
1585 // enable FRC if high-quality AV sync is requested, even if not
Lajos Molnar1de1e252015-04-30 18:18:34 -07001586 // directly queuing to display, as this will even improve textureview
Lajos Molnard9fd6312014-11-06 11:00:00 -08001587 // playback.
1588 {
1589 char value[PROPERTY_VALUE_MAX];
1590 if (property_get("persist.sys.media.avsync", value, NULL) &&
1591 (!strcmp("1", value) || !strcasecmp("true", value))) {
1592 format->setInt32("auto-frc", 1);
1593 }
1594 }
Wei Jiabc2fb722014-07-08 16:37:57 -07001595 }
Lajos Molnar1cd13982014-01-17 15:12:51 -08001596 (*decoder)->init();
Andreas Huber84066782011-08-16 09:34:26 -07001597 (*decoder)->configure(format);
Andreas Huberf9334412010-12-15 15:17:42 -08001598
Lajos Molnar09524832014-07-17 14:29:51 -07001599 // allocate buffers to decrypt widevine source buffers
1600 if (!audio && (mSourceFlags & Source::FLAG_SECURE)) {
1601 Vector<sp<ABuffer> > inputBufs;
1602 CHECK_EQ((*decoder)->getInputBuffers(&inputBufs), (status_t)OK);
1603
1604 Vector<MediaBuffer *> mediaBufs;
1605 for (size_t i = 0; i < inputBufs.size(); i++) {
1606 const sp<ABuffer> &buffer = inputBufs[i];
1607 MediaBuffer *mbuf = new MediaBuffer(buffer->data(), buffer->size());
1608 mediaBufs.push(mbuf);
1609 }
1610
1611 status_t err = mSource->setBuffers(audio, mediaBufs);
1612 if (err != OK) {
1613 for (size_t i = 0; i < mediaBufs.size(); ++i) {
1614 mediaBufs[i]->release();
1615 }
1616 mediaBufs.clear();
1617 ALOGE("Secure source didn't support secure mediaBufs.");
1618 return err;
1619 }
1620 }
Andreas Huberf9334412010-12-15 15:17:42 -08001621 return OK;
1622}
1623
Chong Zhangced1c2f2014-08-08 15:22:35 -07001624void NuPlayer::updateVideoSize(
1625 const sp<AMessage> &inputFormat,
1626 const sp<AMessage> &outputFormat) {
1627 if (inputFormat == NULL) {
1628 ALOGW("Unknown video size, reporting 0x0!");
1629 notifyListener(MEDIA_SET_VIDEO_SIZE, 0, 0);
1630 return;
1631 }
1632
1633 int32_t displayWidth, displayHeight;
Chong Zhangced1c2f2014-08-08 15:22:35 -07001634 if (outputFormat != NULL) {
1635 int32_t width, height;
1636 CHECK(outputFormat->findInt32("width", &width));
1637 CHECK(outputFormat->findInt32("height", &height));
1638
1639 int32_t cropLeft, cropTop, cropRight, cropBottom;
1640 CHECK(outputFormat->findRect(
1641 "crop",
1642 &cropLeft, &cropTop, &cropRight, &cropBottom));
1643
1644 displayWidth = cropRight - cropLeft + 1;
1645 displayHeight = cropBottom - cropTop + 1;
1646
1647 ALOGV("Video output format changed to %d x %d "
1648 "(crop: %d x %d @ (%d, %d))",
1649 width, height,
1650 displayWidth,
1651 displayHeight,
1652 cropLeft, cropTop);
1653 } else {
1654 CHECK(inputFormat->findInt32("width", &displayWidth));
1655 CHECK(inputFormat->findInt32("height", &displayHeight));
1656
1657 ALOGV("Video input format %d x %d", displayWidth, displayHeight);
1658 }
1659
1660 // Take into account sample aspect ratio if necessary:
1661 int32_t sarWidth, sarHeight;
1662 if (inputFormat->findInt32("sar-width", &sarWidth)
1663 && inputFormat->findInt32("sar-height", &sarHeight)) {
1664 ALOGV("Sample aspect ratio %d : %d", sarWidth, sarHeight);
1665
1666 displayWidth = (displayWidth * sarWidth) / sarHeight;
1667
1668 ALOGV("display dimensions %d x %d", displayWidth, displayHeight);
1669 }
1670
1671 int32_t rotationDegrees;
1672 if (!inputFormat->findInt32("rotation-degrees", &rotationDegrees)) {
1673 rotationDegrees = 0;
1674 }
1675
1676 if (rotationDegrees == 90 || rotationDegrees == 270) {
1677 int32_t tmp = displayWidth;
1678 displayWidth = displayHeight;
1679 displayHeight = tmp;
1680 }
1681
1682 notifyListener(
1683 MEDIA_SET_VIDEO_SIZE,
1684 displayWidth,
1685 displayHeight);
1686}
1687
Chong Zhangdcb89b32013-08-06 09:44:47 -07001688void NuPlayer::notifyListener(int msg, int ext1, int ext2, const Parcel *in) {
Andreas Huber43c3e6c2011-01-05 12:17:08 -08001689 if (mDriver == NULL) {
Andreas Huberf9334412010-12-15 15:17:42 -08001690 return;
1691 }
1692
Andreas Huber43c3e6c2011-01-05 12:17:08 -08001693 sp<NuPlayerDriver> driver = mDriver.promote();
Andreas Huberf9334412010-12-15 15:17:42 -08001694
Andreas Huber43c3e6c2011-01-05 12:17:08 -08001695 if (driver == NULL) {
Andreas Huberf9334412010-12-15 15:17:42 -08001696 return;
1697 }
1698
Chong Zhangdcb89b32013-08-06 09:44:47 -07001699 driver->notifyListener(msg, ext1, ext2, in);
Andreas Huberf9334412010-12-15 15:17:42 -08001700}
1701
Chong Zhang7137ec72014-11-12 16:41:05 -08001702void NuPlayer::flushDecoder(bool audio, bool needShutdown) {
Andreas Huber14f76722013-01-15 09:04:18 -08001703 ALOGV("[%s] flushDecoder needShutdown=%d",
1704 audio ? "audio" : "video", needShutdown);
1705
Chong Zhang7137ec72014-11-12 16:41:05 -08001706 const sp<DecoderBase> &decoder = getDecoder(audio);
Lajos Molnar87603c02014-08-20 19:25:30 -07001707 if (decoder == NULL) {
Steve Blockdf64d152012-01-04 20:05:49 +00001708 ALOGI("flushDecoder %s without decoder present",
Andreas Huber6e3d3112011-11-28 12:36:11 -08001709 audio ? "audio" : "video");
Lajos Molnar87603c02014-08-20 19:25:30 -07001710 return;
Andreas Huber6e3d3112011-11-28 12:36:11 -08001711 }
1712
Andreas Huber1aef2112011-01-04 14:01:29 -08001713 // Make sure we don't continue to scan sources until we finish flushing.
1714 ++mScanSourcesGeneration;
Chong Zhang3b032b32015-04-17 15:49:06 -07001715 if (mScanSourcesPending) {
1716 mDeferredActions.push_back(
1717 new SimpleAction(&NuPlayer::performScanSources));
1718 mScanSourcesPending = false;
1719 }
Andreas Huber1aef2112011-01-04 14:01:29 -08001720
Chong Zhang7137ec72014-11-12 16:41:05 -08001721 decoder->signalFlush();
Andreas Huber1aef2112011-01-04 14:01:29 -08001722
1723 FlushStatus newStatus =
1724 needShutdown ? FLUSHING_DECODER_SHUTDOWN : FLUSHING_DECODER;
1725
Lajos Molnarfcd3e942015-03-31 10:06:48 -07001726 mFlushComplete[audio][false /* isDecoder */] = (mRenderer == NULL);
Andy Hung8d121d42014-10-03 09:53:53 -07001727 mFlushComplete[audio][true /* isDecoder */] = false;
Andreas Huber1aef2112011-01-04 14:01:29 -08001728 if (audio) {
Wei Jia53904f32014-07-29 10:22:53 -07001729 ALOGE_IF(mFlushingAudio != NONE,
1730 "audio flushDecoder() is called in state %d", mFlushingAudio);
Andreas Huber1aef2112011-01-04 14:01:29 -08001731 mFlushingAudio = newStatus;
Andreas Huber1aef2112011-01-04 14:01:29 -08001732 } else {
Wei Jia53904f32014-07-29 10:22:53 -07001733 ALOGE_IF(mFlushingVideo != NONE,
1734 "video flushDecoder() is called in state %d", mFlushingVideo);
Andreas Huber1aef2112011-01-04 14:01:29 -08001735 mFlushingVideo = newStatus;
Andreas Huber1aef2112011-01-04 14:01:29 -08001736 }
1737}
1738
Chong Zhangced1c2f2014-08-08 15:22:35 -07001739void NuPlayer::queueDecoderShutdown(
1740 bool audio, bool video, const sp<AMessage> &reply) {
1741 ALOGI("queueDecoderShutdown audio=%d, video=%d", audio, video);
Andreas Huber84066782011-08-16 09:34:26 -07001742
Chong Zhangced1c2f2014-08-08 15:22:35 -07001743 mDeferredActions.push_back(
Wei Jiafef808d2014-10-31 17:57:05 -07001744 new FlushDecoderAction(
1745 audio ? FLUSH_CMD_SHUTDOWN : FLUSH_CMD_NONE,
1746 video ? FLUSH_CMD_SHUTDOWN : FLUSH_CMD_NONE));
Andreas Huber84066782011-08-16 09:34:26 -07001747
Chong Zhangced1c2f2014-08-08 15:22:35 -07001748 mDeferredActions.push_back(
1749 new SimpleAction(&NuPlayer::performScanSources));
Andreas Huber84066782011-08-16 09:34:26 -07001750
Chong Zhangced1c2f2014-08-08 15:22:35 -07001751 mDeferredActions.push_back(new PostMessageAction(reply));
1752
1753 processDeferredActions();
Andreas Huber84066782011-08-16 09:34:26 -07001754}
1755
James Dong0d268a32012-08-31 12:18:27 -07001756status_t NuPlayer::setVideoScalingMode(int32_t mode) {
1757 mVideoScalingMode = mode;
Lajos Molnar1de1e252015-04-30 18:18:34 -07001758 if (mSurface != NULL) {
1759 status_t ret = native_window_set_scaling_mode(mSurface.get(), mVideoScalingMode);
James Dong0d268a32012-08-31 12:18:27 -07001760 if (ret != OK) {
1761 ALOGE("Failed to set scaling mode (%d): %s",
1762 -ret, strerror(-ret));
1763 return ret;
1764 }
1765 }
1766 return OK;
1767}
1768
Chong Zhangdcb89b32013-08-06 09:44:47 -07001769status_t NuPlayer::getTrackInfo(Parcel* reply) const {
Lajos Molnar1d15ab52015-03-04 16:46:34 -08001770 sp<AMessage> msg = new AMessage(kWhatGetTrackInfo, this);
Chong Zhangdcb89b32013-08-06 09:44:47 -07001771 msg->setPointer("reply", reply);
1772
1773 sp<AMessage> response;
1774 status_t err = msg->postAndAwaitResponse(&response);
1775 return err;
1776}
1777
Robert Shih7c4f0d72014-07-09 18:53:31 -07001778status_t NuPlayer::getSelectedTrack(int32_t type, Parcel* reply) const {
Lajos Molnar1d15ab52015-03-04 16:46:34 -08001779 sp<AMessage> msg = new AMessage(kWhatGetSelectedTrack, this);
Robert Shih7c4f0d72014-07-09 18:53:31 -07001780 msg->setPointer("reply", reply);
1781 msg->setInt32("type", type);
1782
1783 sp<AMessage> response;
1784 status_t err = msg->postAndAwaitResponse(&response);
1785 if (err == OK && response != NULL) {
1786 CHECK(response->findInt32("err", &err));
1787 }
1788 return err;
1789}
1790
Robert Shih6ffb1fd2014-10-29 16:24:32 -07001791status_t NuPlayer::selectTrack(size_t trackIndex, bool select, int64_t timeUs) {
Lajos Molnar1d15ab52015-03-04 16:46:34 -08001792 sp<AMessage> msg = new AMessage(kWhatSelectTrack, this);
Chong Zhangdcb89b32013-08-06 09:44:47 -07001793 msg->setSize("trackIndex", trackIndex);
1794 msg->setInt32("select", select);
Robert Shih6ffb1fd2014-10-29 16:24:32 -07001795 msg->setInt64("timeUs", timeUs);
Chong Zhangdcb89b32013-08-06 09:44:47 -07001796
1797 sp<AMessage> response;
1798 status_t err = msg->postAndAwaitResponse(&response);
1799
Chong Zhang404fced2014-06-11 14:45:31 -07001800 if (err != OK) {
1801 return err;
1802 }
1803
1804 if (!response->findInt32("err", &err)) {
1805 err = OK;
1806 }
1807
Chong Zhangdcb89b32013-08-06 09:44:47 -07001808 return err;
1809}
1810
Ronghua Wua73d9e02014-10-08 15:13:29 -07001811status_t NuPlayer::getCurrentPosition(int64_t *mediaUs) {
1812 sp<Renderer> renderer = mRenderer;
1813 if (renderer == NULL) {
1814 return NO_INIT;
1815 }
1816
1817 return renderer->getCurrentPosition(mediaUs);
1818}
1819
Praveen Chavane1e5d7a2015-05-19 19:09:48 -07001820void NuPlayer::getStats(Vector<sp<AMessage> > *mTrackStats) {
1821 CHECK(mTrackStats != NULL);
1822
1823 mTrackStats->clear();
1824 if (mVideoDecoder != NULL) {
1825 mTrackStats->push_back(mVideoDecoder->getStats());
1826 }
1827 if (mAudioDecoder != NULL) {
1828 mTrackStats->push_back(mAudioDecoder->getStats());
Chong Zhang7137ec72014-11-12 16:41:05 -08001829 }
Ronghua Wua73d9e02014-10-08 15:13:29 -07001830}
1831
Marco Nelissenf0b72b52014-09-16 15:43:44 -07001832sp<MetaData> NuPlayer::getFileMeta() {
1833 return mSource->getFileFormatMeta();
1834}
1835
Ronghua Wuc8a70d32015-04-29 16:26:34 -07001836float NuPlayer::getFrameRate() {
1837 sp<MetaData> meta = mSource->getFormatMeta(false /* audio */);
1838 if (meta == NULL) {
1839 return 0;
1840 }
1841 int32_t rate;
1842 if (!meta->findInt32(kKeyFrameRate, &rate)) {
1843 // fall back to try file meta
1844 sp<MetaData> fileMeta = getFileMeta();
1845 if (fileMeta == NULL) {
1846 ALOGW("source has video meta but not file meta");
1847 return -1;
1848 }
1849 int32_t fileMetaRate;
1850 if (!fileMeta->findInt32(kKeyFrameRate, &fileMetaRate)) {
1851 return -1;
1852 }
1853 return fileMetaRate;
1854 }
1855 return rate;
1856}
1857
Andreas Huberb7c8e912012-11-27 15:02:53 -08001858void NuPlayer::schedulePollDuration() {
Lajos Molnar1d15ab52015-03-04 16:46:34 -08001859 sp<AMessage> msg = new AMessage(kWhatPollDuration, this);
Andreas Huberb7c8e912012-11-27 15:02:53 -08001860 msg->setInt32("generation", mPollDurationGeneration);
1861 msg->post();
1862}
1863
1864void NuPlayer::cancelPollDuration() {
1865 ++mPollDurationGeneration;
1866}
1867
Andreas Hubera1f8ab02012-11-30 10:53:22 -08001868void NuPlayer::processDeferredActions() {
1869 while (!mDeferredActions.empty()) {
1870 // We won't execute any deferred actions until we're no longer in
1871 // an intermediate state, i.e. one more more decoders are currently
1872 // flushing or shutting down.
1873
Andreas Hubera1f8ab02012-11-30 10:53:22 -08001874 if (mFlushingAudio != NONE || mFlushingVideo != NONE) {
1875 // We're currently flushing, postpone the reset until that's
1876 // completed.
1877
1878 ALOGV("postponing action mFlushingAudio=%d, mFlushingVideo=%d",
1879 mFlushingAudio, mFlushingVideo);
1880
1881 break;
1882 }
1883
1884 sp<Action> action = *mDeferredActions.begin();
1885 mDeferredActions.erase(mDeferredActions.begin());
1886
1887 action->execute(this);
1888 }
1889}
1890
Wei Jia29840802015-05-15 17:11:38 -07001891void NuPlayer::performSeek(int64_t seekTimeUs) {
1892 ALOGV("performSeek seekTimeUs=%lld us (%.2f secs)",
Lajos Molnar6d339f12015-04-17 16:15:53 -07001893 (long long)seekTimeUs,
Wei Jia29840802015-05-15 17:11:38 -07001894 seekTimeUs / 1E6);
Andreas Hubera1f8ab02012-11-30 10:53:22 -08001895
Andy Hungadf34bf2014-09-03 18:22:22 -07001896 if (mSource == NULL) {
1897 // This happens when reset occurs right before the loop mode
1898 // asynchronously seeks to the start of the stream.
1899 LOG_ALWAYS_FATAL_IF(mAudioDecoder != NULL || mVideoDecoder != NULL,
1900 "mSource is NULL and decoders not NULL audio(%p) video(%p)",
1901 mAudioDecoder.get(), mVideoDecoder.get());
1902 return;
1903 }
Robert Shih1a5c8592015-08-04 18:07:44 -07001904 mPreviousSeekTimeUs = seekTimeUs;
Andreas Hubera1f8ab02012-11-30 10:53:22 -08001905 mSource->seekTo(seekTimeUs);
Robert Shihd3b0bbb2014-07-23 15:00:25 -07001906 ++mTimedTextGeneration;
Andreas Hubera1f8ab02012-11-30 10:53:22 -08001907
Andreas Hubera1f8ab02012-11-30 10:53:22 -08001908 // everything's flushed, continue playback.
1909}
1910
Wei Jiafef808d2014-10-31 17:57:05 -07001911void NuPlayer::performDecoderFlush(FlushCommand audio, FlushCommand video) {
1912 ALOGV("performDecoderFlush audio=%d, video=%d", audio, video);
Andreas Hubera1f8ab02012-11-30 10:53:22 -08001913
Wei Jiafef808d2014-10-31 17:57:05 -07001914 if ((audio == FLUSH_CMD_NONE || mAudioDecoder == NULL)
1915 && (video == FLUSH_CMD_NONE || mVideoDecoder == NULL)) {
Andreas Hubera1f8ab02012-11-30 10:53:22 -08001916 return;
1917 }
1918
Wei Jiafef808d2014-10-31 17:57:05 -07001919 if (audio != FLUSH_CMD_NONE && mAudioDecoder != NULL) {
1920 flushDecoder(true /* audio */, (audio == FLUSH_CMD_SHUTDOWN));
Andreas Hubera1f8ab02012-11-30 10:53:22 -08001921 }
1922
Wei Jiafef808d2014-10-31 17:57:05 -07001923 if (video != FLUSH_CMD_NONE && mVideoDecoder != NULL) {
1924 flushDecoder(false /* audio */, (video == FLUSH_CMD_SHUTDOWN));
Andreas Hubera1f8ab02012-11-30 10:53:22 -08001925 }
1926}
1927
1928void NuPlayer::performReset() {
1929 ALOGV("performReset");
1930
1931 CHECK(mAudioDecoder == NULL);
1932 CHECK(mVideoDecoder == NULL);
1933
1934 cancelPollDuration();
1935
1936 ++mScanSourcesGeneration;
1937 mScanSourcesPending = false;
1938
Lajos Molnar09524832014-07-17 14:29:51 -07001939 if (mRendererLooper != NULL) {
1940 if (mRenderer != NULL) {
1941 mRendererLooper->unregisterHandler(mRenderer->id());
1942 }
1943 mRendererLooper->stop();
1944 mRendererLooper.clear();
1945 }
Andreas Hubera1f8ab02012-11-30 10:53:22 -08001946 mRenderer.clear();
Wei Jia57568df2014-09-22 10:16:29 -07001947 ++mRendererGeneration;
Andreas Hubera1f8ab02012-11-30 10:53:22 -08001948
1949 if (mSource != NULL) {
1950 mSource->stop();
Andreas Huberb5f25f02013-02-05 10:14:26 -08001951
Andreas Hubera1f8ab02012-11-30 10:53:22 -08001952 mSource.clear();
1953 }
1954
1955 if (mDriver != NULL) {
1956 sp<NuPlayerDriver> driver = mDriver.promote();
1957 if (driver != NULL) {
1958 driver->notifyResetComplete();
1959 }
1960 }
Andreas Huber57a339c2012-12-03 11:18:00 -08001961
1962 mStarted = false;
Ronghua Wu64c2d172015-10-07 16:52:19 -07001963 mResetting = false;
Robert Shih0c61a0d2015-07-06 15:09:10 -07001964 mSourceStarted = false;
Andreas Hubera1f8ab02012-11-30 10:53:22 -08001965}
1966
1967void NuPlayer::performScanSources() {
1968 ALOGV("performScanSources");
1969
Andreas Huber57a339c2012-12-03 11:18:00 -08001970 if (!mStarted) {
1971 return;
1972 }
1973
Andreas Hubera1f8ab02012-11-30 10:53:22 -08001974 if (mAudioDecoder == NULL || mVideoDecoder == NULL) {
1975 postScanSources();
1976 }
1977}
1978
Lajos Molnar1de1e252015-04-30 18:18:34 -07001979void NuPlayer::performSetSurface(const sp<Surface> &surface) {
Andreas Huber57a339c2012-12-03 11:18:00 -08001980 ALOGV("performSetSurface");
1981
Lajos Molnar1de1e252015-04-30 18:18:34 -07001982 mSurface = surface;
Andreas Huber57a339c2012-12-03 11:18:00 -08001983
1984 // XXX - ignore error from setVideoScalingMode for now
1985 setVideoScalingMode(mVideoScalingMode);
Chong Zhang13d6faa2014-08-22 15:35:28 -07001986
1987 if (mDriver != NULL) {
1988 sp<NuPlayerDriver> driver = mDriver.promote();
1989 if (driver != NULL) {
1990 driver->notifySetSurfaceComplete();
1991 }
1992 }
Andreas Huber57a339c2012-12-03 11:18:00 -08001993}
1994
Chong Zhangf8d71772014-11-26 15:08:34 -08001995void NuPlayer::performResumeDecoders(bool needNotify) {
1996 if (needNotify) {
1997 mResumePending = true;
1998 if (mVideoDecoder == NULL) {
1999 // if audio-only, we can notify seek complete now,
2000 // as the resume operation will be relatively fast.
2001 finishResume();
2002 }
2003 }
2004
Chong Zhang7137ec72014-11-12 16:41:05 -08002005 if (mVideoDecoder != NULL) {
Chong Zhangf8d71772014-11-26 15:08:34 -08002006 // When there is continuous seek, MediaPlayer will cache the seek
2007 // position, and send down new seek request when previous seek is
2008 // complete. Let's wait for at least one video output frame before
2009 // notifying seek complete, so that the video thumbnail gets updated
2010 // when seekbar is dragged.
2011 mVideoDecoder->signalResume(needNotify);
Chong Zhang7137ec72014-11-12 16:41:05 -08002012 }
2013
2014 if (mAudioDecoder != NULL) {
Chong Zhangf8d71772014-11-26 15:08:34 -08002015 mAudioDecoder->signalResume(false /* needNotify */);
2016 }
2017}
2018
2019void NuPlayer::finishResume() {
2020 if (mResumePending) {
2021 mResumePending = false;
Wei Jia1061c9c2015-05-19 16:02:17 -07002022 notifyDriverSeekComplete();
2023 }
2024}
2025
2026void NuPlayer::notifyDriverSeekComplete() {
2027 if (mDriver != NULL) {
2028 sp<NuPlayerDriver> driver = mDriver.promote();
2029 if (driver != NULL) {
2030 driver->notifySeekComplete();
Chong Zhangf8d71772014-11-26 15:08:34 -08002031 }
Chong Zhang7137ec72014-11-12 16:41:05 -08002032 }
2033}
2034
Andreas Huber9575c962013-02-05 13:59:56 -08002035void NuPlayer::onSourceNotify(const sp<AMessage> &msg) {
2036 int32_t what;
2037 CHECK(msg->findInt32("what", &what));
2038
2039 switch (what) {
Lajos Molnarfcd3e942015-03-31 10:06:48 -07002040 case Source::kWhatInstantiateSecureDecoders:
2041 {
2042 if (mSource == NULL) {
2043 // This is a stale notification from a source that was
2044 // asynchronously preparing when the client called reset().
2045 // We handled the reset, the source is gone.
2046 break;
2047 }
2048
2049 sp<AMessage> reply;
2050 CHECK(msg->findMessage("reply", &reply));
2051 status_t err = onInstantiateSecureDecoders();
2052 reply->setInt32("err", err);
2053 reply->post();
2054 break;
2055 }
2056
Andreas Huber9575c962013-02-05 13:59:56 -08002057 case Source::kWhatPrepared:
2058 {
Andreas Huberb5f28d42013-04-25 15:11:19 -07002059 if (mSource == NULL) {
2060 // This is a stale notification from a source that was
2061 // asynchronously preparing when the client called reset().
2062 // We handled the reset, the source is gone.
2063 break;
2064 }
2065
Andreas Huberec0c5972013-02-05 14:47:13 -08002066 int32_t err;
2067 CHECK(msg->findInt32("err", &err));
2068
Lajos Molnarfcd3e942015-03-31 10:06:48 -07002069 if (err != OK) {
2070 // shut down potential secure codecs in case client never calls reset
2071 mDeferredActions.push_back(
2072 new FlushDecoderAction(FLUSH_CMD_SHUTDOWN /* audio */,
2073 FLUSH_CMD_SHUTDOWN /* video */));
2074 processDeferredActions();
2075 }
2076
Andreas Huber9575c962013-02-05 13:59:56 -08002077 sp<NuPlayerDriver> driver = mDriver.promote();
2078 if (driver != NULL) {
Marco Nelissendd114d12014-05-28 15:23:14 -07002079 // notify duration first, so that it's definitely set when
2080 // the app received the "prepare complete" callback.
2081 int64_t durationUs;
2082 if (mSource->getDuration(&durationUs) == OK) {
2083 driver->notifyDuration(durationUs);
2084 }
Andreas Huberec0c5972013-02-05 14:47:13 -08002085 driver->notifyPrepareCompleted(err);
Andreas Huber9575c962013-02-05 13:59:56 -08002086 }
Andreas Huber99759402013-04-01 14:28:31 -07002087
Andreas Huber9575c962013-02-05 13:59:56 -08002088 break;
2089 }
2090
2091 case Source::kWhatFlagsChanged:
2092 {
2093 uint32_t flags;
2094 CHECK(msg->findInt32("flags", (int32_t *)&flags));
2095
Chong Zhang4b7069d2013-09-11 12:52:43 -07002096 sp<NuPlayerDriver> driver = mDriver.promote();
2097 if (driver != NULL) {
Wei Jia895651b2014-12-10 17:31:52 -08002098 if ((flags & NuPlayer::Source::FLAG_CAN_SEEK) == 0) {
2099 driver->notifyListener(
2100 MEDIA_INFO, MEDIA_INFO_NOT_SEEKABLE, 0);
2101 }
Chong Zhang4b7069d2013-09-11 12:52:43 -07002102 driver->notifyFlagsChanged(flags);
2103 }
2104
Andreas Huber9575c962013-02-05 13:59:56 -08002105 if ((mSourceFlags & Source::FLAG_DYNAMIC_DURATION)
2106 && (!(flags & Source::FLAG_DYNAMIC_DURATION))) {
2107 cancelPollDuration();
2108 } else if (!(mSourceFlags & Source::FLAG_DYNAMIC_DURATION)
2109 && (flags & Source::FLAG_DYNAMIC_DURATION)
2110 && (mAudioDecoder != NULL || mVideoDecoder != NULL)) {
2111 schedulePollDuration();
2112 }
2113
2114 mSourceFlags = flags;
2115 break;
2116 }
2117
2118 case Source::kWhatVideoSizeChanged:
2119 {
Chong Zhangced1c2f2014-08-08 15:22:35 -07002120 sp<AMessage> format;
2121 CHECK(msg->findMessage("format", &format));
Andreas Huber9575c962013-02-05 13:59:56 -08002122
Chong Zhangced1c2f2014-08-08 15:22:35 -07002123 updateVideoSize(format);
Andreas Huber9575c962013-02-05 13:59:56 -08002124 break;
2125 }
2126
Chong Zhang2a3cc9a2014-08-21 17:48:26 -07002127 case Source::kWhatBufferingUpdate:
2128 {
2129 int32_t percentage;
2130 CHECK(msg->findInt32("percentage", &percentage));
2131
2132 notifyListener(MEDIA_BUFFERING_UPDATE, percentage, 0);
2133 break;
2134 }
2135
Chong Zhangefbb6192015-01-30 17:13:27 -08002136 case Source::kWhatPauseOnBufferingStart:
2137 {
2138 // ignore if not playing
Chong Zhang8a048332015-05-06 15:16:28 -07002139 if (mStarted) {
Chong Zhangefbb6192015-01-30 17:13:27 -08002140 ALOGI("buffer low, pausing...");
2141
Chong Zhang8a048332015-05-06 15:16:28 -07002142 mPausedForBuffering = true;
Chong Zhangefbb6192015-01-30 17:13:27 -08002143 onPause();
2144 }
Wei Jiae67ba382016-02-03 01:41:49 +00002145 // fall-thru
2146 }
Chong Zhangefbb6192015-01-30 17:13:27 -08002147
Wei Jiae67ba382016-02-03 01:41:49 +00002148 case Source::kWhatBufferingStart:
2149 {
Roger Jönssonb50e83e2013-01-21 16:26:41 +01002150 notifyListener(MEDIA_INFO, MEDIA_INFO_BUFFERING_START, 0);
2151 break;
2152 }
2153
Chong Zhangefbb6192015-01-30 17:13:27 -08002154 case Source::kWhatResumeOnBufferingEnd:
2155 {
2156 // ignore if not playing
Chong Zhang8a048332015-05-06 15:16:28 -07002157 if (mStarted) {
Chong Zhangefbb6192015-01-30 17:13:27 -08002158 ALOGI("buffer ready, resuming...");
2159
Chong Zhang8a048332015-05-06 15:16:28 -07002160 mPausedForBuffering = false;
2161
2162 // do not resume yet if client didn't unpause
2163 if (!mPausedByClient) {
2164 onResume();
2165 }
Chong Zhangefbb6192015-01-30 17:13:27 -08002166 }
Wei Jiae67ba382016-02-03 01:41:49 +00002167 // fall-thru
2168 }
Chong Zhangefbb6192015-01-30 17:13:27 -08002169
Wei Jiae67ba382016-02-03 01:41:49 +00002170 case Source::kWhatBufferingEnd:
2171 {
Roger Jönssonb50e83e2013-01-21 16:26:41 +01002172 notifyListener(MEDIA_INFO, MEDIA_INFO_BUFFERING_END, 0);
2173 break;
2174 }
2175
Chong Zhangefbb6192015-01-30 17:13:27 -08002176 case Source::kWhatCacheStats:
2177 {
2178 int32_t kbps;
2179 CHECK(msg->findInt32("bandwidth", &kbps));
2180
2181 notifyListener(MEDIA_INFO, MEDIA_INFO_NETWORK_BANDWIDTH, kbps);
2182 break;
2183 }
2184
Chong Zhangdcb89b32013-08-06 09:44:47 -07002185 case Source::kWhatSubtitleData:
2186 {
2187 sp<ABuffer> buffer;
2188 CHECK(msg->findBuffer("buffer", &buffer));
2189
Chong Zhang404fced2014-06-11 14:45:31 -07002190 sendSubtitleData(buffer, 0 /* baseIndex */);
Chong Zhangdcb89b32013-08-06 09:44:47 -07002191 break;
2192 }
2193
Robert Shih08528432015-04-08 09:06:54 -07002194 case Source::kWhatTimedMetaData:
2195 {
2196 sp<ABuffer> buffer;
2197 if (!msg->findBuffer("buffer", &buffer)) {
2198 notifyListener(MEDIA_INFO, MEDIA_INFO_METADATA_UPDATE, 0);
2199 } else {
2200 sendTimedMetaData(buffer);
2201 }
2202 break;
2203 }
2204
Robert Shihd3b0bbb2014-07-23 15:00:25 -07002205 case Source::kWhatTimedTextData:
2206 {
2207 int32_t generation;
2208 if (msg->findInt32("generation", &generation)
2209 && generation != mTimedTextGeneration) {
2210 break;
2211 }
2212
2213 sp<ABuffer> buffer;
2214 CHECK(msg->findBuffer("buffer", &buffer));
2215
2216 sp<NuPlayerDriver> driver = mDriver.promote();
2217 if (driver == NULL) {
2218 break;
2219 }
2220
2221 int posMs;
2222 int64_t timeUs, posUs;
2223 driver->getCurrentPosition(&posMs);
Patrik2 Carlsson24d484b2015-01-27 16:49:45 +01002224 posUs = (int64_t) posMs * 1000ll;
Robert Shihd3b0bbb2014-07-23 15:00:25 -07002225 CHECK(buffer->meta()->findInt64("timeUs", &timeUs));
2226
2227 if (posUs < timeUs) {
2228 if (!msg->findInt32("generation", &generation)) {
2229 msg->setInt32("generation", mTimedTextGeneration);
2230 }
2231 msg->post(timeUs - posUs);
2232 } else {
2233 sendTimedTextData(buffer);
2234 }
2235 break;
2236 }
2237
Andreas Huber14f76722013-01-15 09:04:18 -08002238 case Source::kWhatQueueDecoderShutdown:
2239 {
2240 int32_t audio, video;
2241 CHECK(msg->findInt32("audio", &audio));
2242 CHECK(msg->findInt32("video", &video));
2243
2244 sp<AMessage> reply;
2245 CHECK(msg->findMessage("reply", &reply));
2246
2247 queueDecoderShutdown(audio, video, reply);
2248 break;
2249 }
2250
Ronghua Wu80276872014-08-28 15:50:29 -07002251 case Source::kWhatDrmNoLicense:
2252 {
2253 notifyListener(MEDIA_ERROR, MEDIA_ERROR_UNKNOWN, ERROR_DRM_NO_LICENSE);
2254 break;
2255 }
2256
Andreas Huber9575c962013-02-05 13:59:56 -08002257 default:
2258 TRESPASS();
2259 }
2260}
2261
Chong Zhanga7fa1d92014-06-11 14:49:23 -07002262void NuPlayer::onClosedCaptionNotify(const sp<AMessage> &msg) {
2263 int32_t what;
2264 CHECK(msg->findInt32("what", &what));
2265
2266 switch (what) {
2267 case NuPlayer::CCDecoder::kWhatClosedCaptionData:
2268 {
2269 sp<ABuffer> buffer;
2270 CHECK(msg->findBuffer("buffer", &buffer));
2271
2272 size_t inbandTracks = 0;
2273 if (mSource != NULL) {
2274 inbandTracks = mSource->getTrackCount();
2275 }
2276
2277 sendSubtitleData(buffer, inbandTracks);
2278 break;
2279 }
2280
2281 case NuPlayer::CCDecoder::kWhatTrackAdded:
2282 {
2283 notifyListener(MEDIA_INFO, MEDIA_INFO_METADATA_UPDATE, 0);
2284
2285 break;
2286 }
2287
2288 default:
2289 TRESPASS();
2290 }
2291
2292
2293}
2294
Chong Zhang404fced2014-06-11 14:45:31 -07002295void NuPlayer::sendSubtitleData(const sp<ABuffer> &buffer, int32_t baseIndex) {
2296 int32_t trackIndex;
2297 int64_t timeUs, durationUs;
2298 CHECK(buffer->meta()->findInt32("trackIndex", &trackIndex));
2299 CHECK(buffer->meta()->findInt64("timeUs", &timeUs));
2300 CHECK(buffer->meta()->findInt64("durationUs", &durationUs));
2301
2302 Parcel in;
2303 in.writeInt32(trackIndex + baseIndex);
2304 in.writeInt64(timeUs);
2305 in.writeInt64(durationUs);
2306 in.writeInt32(buffer->size());
2307 in.writeInt32(buffer->size());
2308 in.write(buffer->data(), buffer->size());
2309
2310 notifyListener(MEDIA_SUBTITLE_DATA, 0, 0, &in);
2311}
Robert Shihd3b0bbb2014-07-23 15:00:25 -07002312
Robert Shih08528432015-04-08 09:06:54 -07002313void NuPlayer::sendTimedMetaData(const sp<ABuffer> &buffer) {
2314 int64_t timeUs;
2315 CHECK(buffer->meta()->findInt64("timeUs", &timeUs));
2316
2317 Parcel in;
2318 in.writeInt64(timeUs);
2319 in.writeInt32(buffer->size());
2320 in.writeInt32(buffer->size());
2321 in.write(buffer->data(), buffer->size());
2322
2323 notifyListener(MEDIA_META_DATA, 0, 0, &in);
2324}
2325
Robert Shihd3b0bbb2014-07-23 15:00:25 -07002326void NuPlayer::sendTimedTextData(const sp<ABuffer> &buffer) {
2327 const void *data;
2328 size_t size = 0;
2329 int64_t timeUs;
Marco Nelissen55e2f4c2015-09-04 15:57:15 -07002330 int32_t flag = TextDescriptions::IN_BAND_TEXT_3GPP;
Robert Shihd3b0bbb2014-07-23 15:00:25 -07002331
2332 AString mime;
2333 CHECK(buffer->meta()->findString("mime", &mime));
2334 CHECK(strcasecmp(mime.c_str(), MEDIA_MIMETYPE_TEXT_3GPP) == 0);
2335
2336 data = buffer->data();
2337 size = buffer->size();
2338
2339 Parcel parcel;
2340 if (size > 0) {
2341 CHECK(buffer->meta()->findInt64("timeUs", &timeUs));
Marco Nelissen55e2f4c2015-09-04 15:57:15 -07002342 int32_t global = 0;
2343 if (buffer->meta()->findInt32("global", &global) && global) {
2344 flag |= TextDescriptions::GLOBAL_DESCRIPTIONS;
2345 } else {
2346 flag |= TextDescriptions::LOCAL_DESCRIPTIONS;
2347 }
Robert Shihd3b0bbb2014-07-23 15:00:25 -07002348 TextDescriptions::getParcelOfDescriptions(
2349 (const uint8_t *)data, size, flag, timeUs / 1000, &parcel);
2350 }
2351
2352 if ((parcel.dataSize() > 0)) {
2353 notifyListener(MEDIA_TIMED_TEXT, 0, 0, &parcel);
2354 } else { // send an empty timed text
2355 notifyListener(MEDIA_TIMED_TEXT, 0, 0);
2356 }
2357}
Andreas Huberb5f25f02013-02-05 10:14:26 -08002358////////////////////////////////////////////////////////////////////////////////
2359
Chong Zhangced1c2f2014-08-08 15:22:35 -07002360sp<AMessage> NuPlayer::Source::getFormat(bool audio) {
2361 sp<MetaData> meta = getFormatMeta(audio);
2362
2363 if (meta == NULL) {
2364 return NULL;
2365 }
2366
2367 sp<AMessage> msg = new AMessage;
2368
2369 if(convertMetaDataToMessage(meta, &msg) == OK) {
2370 return msg;
2371 }
2372 return NULL;
2373}
2374
Andreas Huber9575c962013-02-05 13:59:56 -08002375void NuPlayer::Source::notifyFlagsChanged(uint32_t flags) {
2376 sp<AMessage> notify = dupNotify();
2377 notify->setInt32("what", kWhatFlagsChanged);
2378 notify->setInt32("flags", flags);
2379 notify->post();
2380}
2381
Chong Zhangced1c2f2014-08-08 15:22:35 -07002382void NuPlayer::Source::notifyVideoSizeChanged(const sp<AMessage> &format) {
Andreas Huber9575c962013-02-05 13:59:56 -08002383 sp<AMessage> notify = dupNotify();
2384 notify->setInt32("what", kWhatVideoSizeChanged);
Chong Zhangced1c2f2014-08-08 15:22:35 -07002385 notify->setMessage("format", format);
Andreas Huber9575c962013-02-05 13:59:56 -08002386 notify->post();
2387}
2388
Andreas Huberec0c5972013-02-05 14:47:13 -08002389void NuPlayer::Source::notifyPrepared(status_t err) {
Andreas Huber9575c962013-02-05 13:59:56 -08002390 sp<AMessage> notify = dupNotify();
2391 notify->setInt32("what", kWhatPrepared);
Andreas Huberec0c5972013-02-05 14:47:13 -08002392 notify->setInt32("err", err);
Andreas Huber9575c962013-02-05 13:59:56 -08002393 notify->post();
2394}
2395
Lajos Molnarfcd3e942015-03-31 10:06:48 -07002396void NuPlayer::Source::notifyInstantiateSecureDecoders(const sp<AMessage> &reply) {
2397 sp<AMessage> notify = dupNotify();
2398 notify->setInt32("what", kWhatInstantiateSecureDecoders);
2399 notify->setMessage("reply", reply);
2400 notify->post();
2401}
2402
Andreas Huber84333e02014-02-07 15:36:10 -08002403void NuPlayer::Source::onMessageReceived(const sp<AMessage> & /* msg */) {
Andreas Huberb5f25f02013-02-05 10:14:26 -08002404 TRESPASS();
2405}
2406
Andreas Huberf9334412010-12-15 15:17:42 -08002407} // namespace android