blob: 3420cc932a1cc127838dd0821d4b33e833a72b69 [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 Jia3a2956d2014-07-22 16:01:33 -07001117 mAudioDecoder.clear();
Wei Jia57568df2014-09-22 10:16:29 -07001118 ++mAudioDecoderGeneration;
Wei Jiada4252f2015-07-14 18:15:28 -07001119 bool needsToCreateAudioDecoder = true;
1120 if (mFlushingAudio == FLUSHING_DECODER) {
1121 mFlushComplete[1 /* audio */][1 /* isDecoder */] = true;
1122 mFlushingAudio = FLUSHED;
1123 finishFlushIfPossible();
1124 } else if (mFlushingAudio == FLUSHING_DECODER_SHUTDOWN
1125 || mFlushingAudio == SHUTTING_DOWN_DECODER) {
1126 mFlushComplete[1 /* audio */][1 /* isDecoder */] = true;
1127 mFlushingAudio = SHUT_DOWN;
1128 finishFlushIfPossible();
1129 needsToCreateAudioDecoder = false;
1130 }
1131 if (mRenderer == NULL) {
1132 break;
1133 }
1134 closeAudioSink();
Chong Zhang7137ec72014-11-12 16:41:05 -08001135 mRenderer->flush(
1136 true /* audio */, false /* notifyComplete */);
Wei Jia3a2956d2014-07-22 16:01:33 -07001137 if (mVideoDecoder != NULL) {
Chong Zhang7137ec72014-11-12 16:41:05 -08001138 mRenderer->flush(
1139 false /* audio */, false /* notifyComplete */);
Wei Jia3a2956d2014-07-22 16:01:33 -07001140 }
Wei Jia3a2956d2014-07-22 16:01:33 -07001141
Wei Jiada4252f2015-07-14 18:15:28 -07001142 int64_t positionUs;
Robert Shih1a5c8592015-08-04 18:07:44 -07001143 if (!msg->findInt64("positionUs", &positionUs)) {
1144 positionUs = mPreviousSeekTimeUs;
1145 }
Wei Jia29840802015-05-15 17:11:38 -07001146 performSeek(positionUs);
Ronghua Wufaeb0f22015-05-21 12:20:21 -07001147
Wei Jiada4252f2015-07-14 18:15:28 -07001148 if (reason == Renderer::kDueToError && needsToCreateAudioDecoder) {
Ronghua Wu08529172014-10-02 16:55:52 -07001149 instantiateDecoder(true /* audio */, &mAudioDecoder);
1150 }
Andreas Huberf9334412010-12-15 15:17:42 -08001151 }
1152 break;
1153 }
1154
1155 case kWhatMoreDataQueued:
1156 {
1157 break;
1158 }
1159
Andreas Huber1aef2112011-01-04 14:01:29 -08001160 case kWhatReset:
1161 {
Steve Block3856b092011-10-20 11:56:00 +01001162 ALOGV("kWhatReset");
Andreas Huber1aef2112011-01-04 14:01:29 -08001163
Ronghua Wu64c2d172015-10-07 16:52:19 -07001164 mResetting = true;
1165
Andreas Hubera1f8ab02012-11-30 10:53:22 -08001166 mDeferredActions.push_back(
Wei Jiafef808d2014-10-31 17:57:05 -07001167 new FlushDecoderAction(
1168 FLUSH_CMD_SHUTDOWN /* audio */,
1169 FLUSH_CMD_SHUTDOWN /* video */));
Andreas Huberb7c8e912012-11-27 15:02:53 -08001170
Andreas Hubera1f8ab02012-11-30 10:53:22 -08001171 mDeferredActions.push_back(
1172 new SimpleAction(&NuPlayer::performReset));
Andreas Huberb58ce9f2011-11-28 16:27:35 -08001173
Andreas Hubera1f8ab02012-11-30 10:53:22 -08001174 processDeferredActions();
Andreas Huber1aef2112011-01-04 14:01:29 -08001175 break;
1176 }
1177
Andreas Huber43c3e6c2011-01-05 12:17:08 -08001178 case kWhatSeek:
1179 {
1180 int64_t seekTimeUs;
Wei Jiae427abf2014-09-22 15:21:11 -07001181 int32_t needNotify;
Andreas Huber43c3e6c2011-01-05 12:17:08 -08001182 CHECK(msg->findInt64("seekTimeUs", &seekTimeUs));
Wei Jiae427abf2014-09-22 15:21:11 -07001183 CHECK(msg->findInt32("needNotify", &needNotify));
Andreas Huber43c3e6c2011-01-05 12:17:08 -08001184
Wei Jiae427abf2014-09-22 15:21:11 -07001185 ALOGV("kWhatSeek seekTimeUs=%lld us, needNotify=%d",
Lajos Molnar6d339f12015-04-17 16:15:53 -07001186 (long long)seekTimeUs, needNotify);
Andreas Huber43c3e6c2011-01-05 12:17:08 -08001187
Wei Jia1061c9c2015-05-19 16:02:17 -07001188 if (!mStarted) {
1189 // Seek before the player is started. In order to preview video,
1190 // need to start the player and pause it. This branch is called
1191 // only once if needed. After the player is started, any seek
1192 // operation will go through normal path.
Robert Shih0c61a0d2015-07-06 15:09:10 -07001193 // Audio-only cases are handled separately.
Wei Jia1061c9c2015-05-19 16:02:17 -07001194 onStart(seekTimeUs);
Robert Shih0c61a0d2015-07-06 15:09:10 -07001195 if (mStarted) {
1196 onPause();
1197 mPausedByClient = true;
1198 }
Wei Jia1061c9c2015-05-19 16:02:17 -07001199 if (needNotify) {
1200 notifyDriverSeekComplete();
1201 }
1202 break;
1203 }
1204
Andreas Hubera1f8ab02012-11-30 10:53:22 -08001205 mDeferredActions.push_back(
Wei Jiafef808d2014-10-31 17:57:05 -07001206 new FlushDecoderAction(FLUSH_CMD_FLUSH /* audio */,
1207 FLUSH_CMD_FLUSH /* video */));
Andreas Huber43c3e6c2011-01-05 12:17:08 -08001208
Wei Jiae427abf2014-09-22 15:21:11 -07001209 mDeferredActions.push_back(
Wei Jia29840802015-05-15 17:11:38 -07001210 new SeekAction(seekTimeUs));
Andreas Huber43c3e6c2011-01-05 12:17:08 -08001211
Chong Zhangf8d71772014-11-26 15:08:34 -08001212 // After a flush without shutdown, decoder is paused.
1213 // Don't resume it until source seek is done, otherwise it could
Chong Zhang7137ec72014-11-12 16:41:05 -08001214 // start pulling stale data too soon.
1215 mDeferredActions.push_back(
Chong Zhangf8d71772014-11-26 15:08:34 -08001216 new ResumeDecoderAction(needNotify));
Chong Zhang7137ec72014-11-12 16:41:05 -08001217
Andreas Hubera1f8ab02012-11-30 10:53:22 -08001218 processDeferredActions();
Andreas Huber43c3e6c2011-01-05 12:17:08 -08001219 break;
1220 }
1221
Andreas Huberb4082222011-01-20 15:23:04 -08001222 case kWhatPause:
1223 {
Chong Zhangefbb6192015-01-30 17:13:27 -08001224 onPause();
1225 mPausedByClient = true;
Andreas Huberb4082222011-01-20 15:23:04 -08001226 break;
1227 }
1228
Andreas Huberb5f25f02013-02-05 10:14:26 -08001229 case kWhatSourceNotify:
1230 {
Andreas Huber9575c962013-02-05 13:59:56 -08001231 onSourceNotify(msg);
Andreas Huberb5f25f02013-02-05 10:14:26 -08001232 break;
1233 }
1234
Chong Zhanga7fa1d92014-06-11 14:49:23 -07001235 case kWhatClosedCaptionNotify:
1236 {
1237 onClosedCaptionNotify(msg);
1238 break;
1239 }
1240
Andreas Huberf9334412010-12-15 15:17:42 -08001241 default:
1242 TRESPASS();
1243 break;
1244 }
1245}
1246
Wei Jia94211742014-10-28 17:09:06 -07001247void NuPlayer::onResume() {
Ronghua Wu64c2d172015-10-07 16:52:19 -07001248 if (!mPaused || mResetting) {
1249 ALOGD_IF(mResetting, "resetting, onResume discarded");
Chong Zhangefbb6192015-01-30 17:13:27 -08001250 return;
1251 }
1252 mPaused = false;
Wei Jia94211742014-10-28 17:09:06 -07001253 if (mSource != NULL) {
1254 mSource->resume();
1255 } else {
1256 ALOGW("resume called when source is gone or not set");
1257 }
1258 // |mAudioDecoder| may have been released due to the pause timeout, so re-create it if
1259 // needed.
1260 if (audioDecoderStillNeeded() && mAudioDecoder == NULL) {
1261 instantiateDecoder(true /* audio */, &mAudioDecoder);
1262 }
1263 if (mRenderer != NULL) {
1264 mRenderer->resume();
1265 } else {
1266 ALOGW("resume called when renderer is gone or not set");
1267 }
1268}
1269
Lajos Molnarfcd3e942015-03-31 10:06:48 -07001270status_t NuPlayer::onInstantiateSecureDecoders() {
1271 status_t err;
1272 if (!(mSourceFlags & Source::FLAG_SECURE)) {
1273 return BAD_TYPE;
1274 }
1275
1276 if (mRenderer != NULL) {
1277 ALOGE("renderer should not be set when instantiating secure decoders");
1278 return UNKNOWN_ERROR;
1279 }
1280
1281 // TRICKY: We rely on mRenderer being null, so that decoder does not start requesting
1282 // data on instantiation.
Lajos Molnar1de1e252015-04-30 18:18:34 -07001283 if (mSurface != NULL) {
Lajos Molnarfcd3e942015-03-31 10:06:48 -07001284 err = instantiateDecoder(false, &mVideoDecoder);
1285 if (err != OK) {
1286 return err;
1287 }
1288 }
1289
1290 if (mAudioSink != NULL) {
1291 err = instantiateDecoder(true, &mAudioDecoder);
1292 if (err != OK) {
1293 return err;
1294 }
1295 }
1296 return OK;
1297}
1298
Wei Jia1061c9c2015-05-19 16:02:17 -07001299void NuPlayer::onStart(int64_t startPositionUs) {
Robert Shih0c61a0d2015-07-06 15:09:10 -07001300 if (!mSourceStarted) {
1301 mSourceStarted = true;
1302 mSource->start();
1303 }
1304 if (startPositionUs > 0) {
1305 performSeek(startPositionUs);
1306 if (mSource->getFormat(false /* audio */) == NULL) {
1307 return;
1308 }
1309 }
1310
Wei Jia94211742014-10-28 17:09:06 -07001311 mOffloadAudio = false;
1312 mAudioEOS = false;
1313 mVideoEOS = false;
Wei Jia94211742014-10-28 17:09:06 -07001314 mStarted = true;
1315
Wei Jia94211742014-10-28 17:09:06 -07001316 uint32_t flags = 0;
1317
1318 if (mSource->isRealTime()) {
1319 flags |= Renderer::FLAG_REAL_TIME;
1320 }
1321
1322 sp<MetaData> audioMeta = mSource->getFormatMeta(true /* audio */);
Andy Hung12a84132015-10-20 16:09:21 -07001323 ALOGV_IF(audioMeta == NULL, "no metadata for audio source"); // video only stream
Wei Jia94211742014-10-28 17:09:06 -07001324 audio_stream_type_t streamType = AUDIO_STREAM_MUSIC;
1325 if (mAudioSink != NULL) {
1326 streamType = mAudioSink->getAudioStreamType();
1327 }
1328
1329 sp<AMessage> videoFormat = mSource->getFormat(false /* audio */);
1330
1331 mOffloadAudio =
Ronghua Wu02cb98d2015-05-27 11:02:54 -07001332 canOffloadStream(audioMeta, (videoFormat != NULL), mSource->isStreaming(), streamType);
Wei Jia94211742014-10-28 17:09:06 -07001333 if (mOffloadAudio) {
1334 flags |= Renderer::FLAG_OFFLOAD_AUDIO;
1335 }
1336
Lajos Molnar1d15ab52015-03-04 16:46:34 -08001337 sp<AMessage> notify = new AMessage(kWhatRendererNotify, this);
Wei Jia94211742014-10-28 17:09:06 -07001338 ++mRendererGeneration;
1339 notify->setInt32("generation", mRendererGeneration);
1340 mRenderer = new Renderer(mAudioSink, notify, flags);
Wei Jia94211742014-10-28 17:09:06 -07001341 mRendererLooper = new ALooper;
1342 mRendererLooper->setName("NuPlayerRenderer");
1343 mRendererLooper->start(false, false, ANDROID_PRIORITY_AUDIO);
1344 mRendererLooper->registerHandler(mRenderer);
Lajos Molnar3a474aa2015-04-24 17:10:07 -07001345
1346 status_t err = mRenderer->setPlaybackSettings(mPlaybackSettings);
1347 if (err != OK) {
1348 mSource->stop();
Robert Shih0c61a0d2015-07-06 15:09:10 -07001349 mSourceStarted = false;
Lajos Molnar3a474aa2015-04-24 17:10:07 -07001350 notifyListener(MEDIA_ERROR, MEDIA_ERROR_UNKNOWN, err);
1351 return;
Wei Jiac8206ff2015-03-04 13:59:37 -08001352 }
Wei Jia94211742014-10-28 17:09:06 -07001353
Ronghua Wuc8a70d32015-04-29 16:26:34 -07001354 float rate = getFrameRate();
1355 if (rate > 0) {
Wei Jia94211742014-10-28 17:09:06 -07001356 mRenderer->setVideoFrameRate(rate);
1357 }
1358
Wei Jiac6cfd702014-11-11 16:33:20 -08001359 if (mVideoDecoder != NULL) {
1360 mVideoDecoder->setRenderer(mRenderer);
1361 }
1362 if (mAudioDecoder != NULL) {
1363 mAudioDecoder->setRenderer(mRenderer);
1364 }
1365
Wei Jia94211742014-10-28 17:09:06 -07001366 postScanSources();
1367}
1368
Chong Zhangefbb6192015-01-30 17:13:27 -08001369void NuPlayer::onPause() {
1370 if (mPaused) {
1371 return;
1372 }
1373 mPaused = true;
1374 if (mSource != NULL) {
1375 mSource->pause();
1376 } else {
1377 ALOGW("pause called when source is gone or not set");
1378 }
1379 if (mRenderer != NULL) {
1380 mRenderer->pause();
1381 } else {
1382 ALOGW("pause called when renderer is gone or not set");
1383 }
1384}
1385
Ronghua Wud7988b12014-10-03 15:19:10 -07001386bool NuPlayer::audioDecoderStillNeeded() {
1387 // Audio decoder is no longer needed if it's in shut/shutting down status.
1388 return ((mFlushingAudio != SHUT_DOWN) && (mFlushingAudio != SHUTTING_DOWN_DECODER));
1389}
1390
Andy Hung8d121d42014-10-03 09:53:53 -07001391void NuPlayer::handleFlushComplete(bool audio, bool isDecoder) {
1392 // We wait for both the decoder flush and the renderer flush to complete
1393 // before entering either the FLUSHED or the SHUTTING_DOWN_DECODER state.
1394
1395 mFlushComplete[audio][isDecoder] = true;
1396 if (!mFlushComplete[audio][!isDecoder]) {
1397 return;
1398 }
1399
1400 FlushStatus *state = audio ? &mFlushingAudio : &mFlushingVideo;
1401 switch (*state) {
1402 case FLUSHING_DECODER:
1403 {
1404 *state = FLUSHED;
Andy Hung8d121d42014-10-03 09:53:53 -07001405 break;
1406 }
1407
1408 case FLUSHING_DECODER_SHUTDOWN:
1409 {
1410 *state = SHUTTING_DOWN_DECODER;
1411
1412 ALOGV("initiating %s decoder shutdown", audio ? "audio" : "video");
1413 if (!audio) {
Andy Hung8d121d42014-10-03 09:53:53 -07001414 // Widevine source reads must stop before releasing the video decoder.
1415 if (mSource != NULL && mSourceFlags & Source::FLAG_SECURE) {
1416 mSource->stop();
Robert Shih0c61a0d2015-07-06 15:09:10 -07001417 mSourceStarted = false;
Andy Hung8d121d42014-10-03 09:53:53 -07001418 }
1419 }
1420 getDecoder(audio)->initiateShutdown();
1421 break;
1422 }
1423
1424 default:
1425 // decoder flush completes only occur in a flushing state.
1426 LOG_ALWAYS_FATAL_IF(isDecoder, "decoder flush in invalid state %d", *state);
1427 break;
1428 }
1429}
1430
Andreas Huber3831a062010-12-21 10:22:33 -08001431void NuPlayer::finishFlushIfPossible() {
Wei Jia53904f32014-07-29 10:22:53 -07001432 if (mFlushingAudio != NONE && mFlushingAudio != FLUSHED
1433 && mFlushingAudio != SHUT_DOWN) {
Andreas Huber3831a062010-12-21 10:22:33 -08001434 return;
1435 }
1436
Wei Jia53904f32014-07-29 10:22:53 -07001437 if (mFlushingVideo != NONE && mFlushingVideo != FLUSHED
1438 && mFlushingVideo != SHUT_DOWN) {
Andreas Huber3831a062010-12-21 10:22:33 -08001439 return;
1440 }
1441
Steve Block3856b092011-10-20 11:56:00 +01001442 ALOGV("both audio and video are flushed now.");
Andreas Huber3831a062010-12-21 10:22:33 -08001443
Andreas Huber3831a062010-12-21 10:22:33 -08001444 mFlushingAudio = NONE;
1445 mFlushingVideo = NONE;
Andreas Huber3831a062010-12-21 10:22:33 -08001446
Andy Hung8d121d42014-10-03 09:53:53 -07001447 clearFlushComplete();
1448
Andreas Hubera1f8ab02012-11-30 10:53:22 -08001449 processDeferredActions();
Andreas Huber1aef2112011-01-04 14:01:29 -08001450}
1451
1452void NuPlayer::postScanSources() {
1453 if (mScanSourcesPending) {
1454 return;
1455 }
1456
Lajos Molnar1d15ab52015-03-04 16:46:34 -08001457 sp<AMessage> msg = new AMessage(kWhatScanSources, this);
Andreas Huber1aef2112011-01-04 14:01:29 -08001458 msg->setInt32("generation", mScanSourcesGeneration);
1459 msg->post();
1460
1461 mScanSourcesPending = true;
1462}
1463
Andy Hung202bce12014-12-03 11:47:36 -08001464void NuPlayer::tryOpenAudioSinkForOffload(const sp<AMessage> &format, bool hasVideo) {
1465 // Note: This is called early in NuPlayer to determine whether offloading
1466 // is possible; otherwise the decoders call the renderer openAudioSink directly.
Andy Hung282a7e32014-08-14 15:56:34 -07001467
Andy Hung202bce12014-12-03 11:47:36 -08001468 status_t err = mRenderer->openAudioSink(
1469 format, true /* offloadOnly */, hasVideo, AUDIO_OUTPUT_FLAG_NONE, &mOffloadAudio);
1470 if (err != OK) {
1471 // Any failure we turn off mOffloadAudio.
1472 mOffloadAudio = false;
1473 } else if (mOffloadAudio) {
Chong Zhang3b9eb1f2014-10-15 17:05:08 -07001474 sp<MetaData> audioMeta =
1475 mSource->getFormatMeta(true /* audio */);
1476 sendMetaDataToHal(mAudioSink, audioMeta);
Andy Hung282a7e32014-08-14 15:56:34 -07001477 }
1478}
1479
1480void NuPlayer::closeAudioSink() {
Chong Zhang3b9eb1f2014-10-15 17:05:08 -07001481 mRenderer->closeAudioSink();
Andy Hung282a7e32014-08-14 15:56:34 -07001482}
1483
Wei Jiae4d18c72015-07-13 17:58:11 -07001484void NuPlayer::determineAudioModeChange() {
1485 if (mSource == NULL || mAudioSink == NULL) {
1486 return;
1487 }
1488
1489 if (mRenderer == NULL) {
1490 ALOGW("No renderer can be used to determine audio mode. Use non-offload for safety.");
1491 mOffloadAudio = false;
1492 return;
1493 }
1494
1495 sp<MetaData> audioMeta = mSource->getFormatMeta(true /* audio */);
1496 sp<AMessage> videoFormat = mSource->getFormat(false /* audio */);
1497 audio_stream_type_t streamType = mAudioSink->getAudioStreamType();
1498 const bool hasVideo = (videoFormat != NULL);
1499 const bool canOffload = canOffloadStream(
1500 audioMeta, hasVideo, mSource->isStreaming(), streamType);
1501 if (canOffload) {
1502 if (!mOffloadAudio) {
1503 mRenderer->signalEnableOffloadAudio();
1504 }
1505 // open audio sink early under offload mode.
1506 sp<AMessage> format = mSource->getFormat(true /*audio*/);
1507 tryOpenAudioSinkForOffload(format, hasVideo);
1508 } else {
Robert Shihe1d70192015-07-23 17:54:13 -07001509 if (mOffloadAudio) {
1510 mRenderer->signalDisableOffloadAudio();
1511 mOffloadAudio = false;
1512 }
Wei Jiae4d18c72015-07-13 17:58:11 -07001513 }
1514}
1515
Chong Zhang7137ec72014-11-12 16:41:05 -08001516status_t NuPlayer::instantiateDecoder(bool audio, sp<DecoderBase> *decoder) {
Wei Jia566da802015-08-27 13:59:30 -07001517 // The audio decoder could be cleared by tear down. If still in shut down
1518 // process, no need to create a new audio decoder.
1519 if (*decoder != NULL || (audio && mFlushingAudio == SHUT_DOWN)) {
Andreas Huberf9334412010-12-15 15:17:42 -08001520 return OK;
1521 }
1522
Andreas Huber84066782011-08-16 09:34:26 -07001523 sp<AMessage> format = mSource->getFormat(audio);
Andreas Huberf9334412010-12-15 15:17:42 -08001524
Andreas Huber84066782011-08-16 09:34:26 -07001525 if (format == NULL) {
Robert Shih7350b052015-10-01 15:50:14 -07001526 return UNKNOWN_ERROR;
1527 } else {
1528 status_t err;
1529 if (format->findInt32("err", &err) && err) {
1530 return err;
1531 }
Andreas Huberf9334412010-12-15 15:17:42 -08001532 }
1533
Ronghua Wu8db88132015-04-22 13:51:35 -07001534 format->setInt32("priority", 0 /* realtime */);
1535
Andreas Huber3fe62152011-09-16 15:09:22 -07001536 if (!audio) {
Andreas Huber84066782011-08-16 09:34:26 -07001537 AString mime;
1538 CHECK(format->findString("mime", &mime));
Chong Zhanga7fa1d92014-06-11 14:49:23 -07001539
Lajos Molnar1d15ab52015-03-04 16:46:34 -08001540 sp<AMessage> ccNotify = new AMessage(kWhatClosedCaptionNotify, this);
Chong Zhang341ab6e2015-02-04 13:37:18 -08001541 if (mCCDecoder == NULL) {
1542 mCCDecoder = new CCDecoder(ccNotify);
1543 }
Lajos Molnar09524832014-07-17 14:29:51 -07001544
1545 if (mSourceFlags & Source::FLAG_SECURE) {
1546 format->setInt32("secure", true);
1547 }
Chong Zhang17134602015-01-07 16:14:34 -08001548
1549 if (mSourceFlags & Source::FLAG_PROTECTED) {
1550 format->setInt32("protected", true);
1551 }
Ronghua Wu8db88132015-04-22 13:51:35 -07001552
Ronghua Wuc8a70d32015-04-29 16:26:34 -07001553 float rate = getFrameRate();
1554 if (rate > 0) {
Lajos Molnar3a474aa2015-04-24 17:10:07 -07001555 format->setFloat("operating-rate", rate * mPlaybackSettings.mSpeed);
Ronghua Wu8db88132015-04-22 13:51:35 -07001556 }
Andreas Huber3fe62152011-09-16 15:09:22 -07001557 }
1558
Wei Jiabc2fb722014-07-08 16:37:57 -07001559 if (audio) {
Lajos Molnar1d15ab52015-03-04 16:46:34 -08001560 sp<AMessage> notify = new AMessage(kWhatAudioNotify, this);
Wei Jia88703c32014-08-06 11:24:07 -07001561 ++mAudioDecoderGeneration;
1562 notify->setInt32("generation", mAudioDecoderGeneration);
1563
Wei Jiae4d18c72015-07-13 17:58:11 -07001564 determineAudioModeChange();
Wei Jiabc2fb722014-07-08 16:37:57 -07001565 if (mOffloadAudio) {
Haynes Mathew George8b635332015-03-30 17:59:47 -07001566 const bool hasVideo = (mSource->getFormat(false /*audio */) != NULL);
1567 format->setInt32("has-video", hasVideo);
Wei Jiac6cfd702014-11-11 16:33:20 -08001568 *decoder = new DecoderPassThrough(notify, mSource, mRenderer);
Wei Jiabc2fb722014-07-08 16:37:57 -07001569 } else {
Ronghua Wu68845c12015-07-21 09:50:48 -07001570 *decoder = new Decoder(notify, mSource, mPID, mRenderer);
Wei Jiabc2fb722014-07-08 16:37:57 -07001571 }
1572 } else {
Lajos Molnar1d15ab52015-03-04 16:46:34 -08001573 sp<AMessage> notify = new AMessage(kWhatVideoNotify, this);
Wei Jia88703c32014-08-06 11:24:07 -07001574 ++mVideoDecoderGeneration;
1575 notify->setInt32("generation", mVideoDecoderGeneration);
1576
Chong Zhang7137ec72014-11-12 16:41:05 -08001577 *decoder = new Decoder(
Ronghua Wu68845c12015-07-21 09:50:48 -07001578 notify, mSource, mPID, mRenderer, mSurface, mCCDecoder);
Lajos Molnard9fd6312014-11-06 11:00:00 -08001579
1580 // enable FRC if high-quality AV sync is requested, even if not
Lajos Molnar1de1e252015-04-30 18:18:34 -07001581 // directly queuing to display, as this will even improve textureview
Lajos Molnard9fd6312014-11-06 11:00:00 -08001582 // playback.
1583 {
1584 char value[PROPERTY_VALUE_MAX];
1585 if (property_get("persist.sys.media.avsync", value, NULL) &&
1586 (!strcmp("1", value) || !strcasecmp("true", value))) {
1587 format->setInt32("auto-frc", 1);
1588 }
1589 }
Wei Jiabc2fb722014-07-08 16:37:57 -07001590 }
Lajos Molnar1cd13982014-01-17 15:12:51 -08001591 (*decoder)->init();
Andreas Huber84066782011-08-16 09:34:26 -07001592 (*decoder)->configure(format);
Andreas Huberf9334412010-12-15 15:17:42 -08001593
Lajos Molnar09524832014-07-17 14:29:51 -07001594 // allocate buffers to decrypt widevine source buffers
1595 if (!audio && (mSourceFlags & Source::FLAG_SECURE)) {
1596 Vector<sp<ABuffer> > inputBufs;
1597 CHECK_EQ((*decoder)->getInputBuffers(&inputBufs), (status_t)OK);
1598
1599 Vector<MediaBuffer *> mediaBufs;
1600 for (size_t i = 0; i < inputBufs.size(); i++) {
1601 const sp<ABuffer> &buffer = inputBufs[i];
1602 MediaBuffer *mbuf = new MediaBuffer(buffer->data(), buffer->size());
1603 mediaBufs.push(mbuf);
1604 }
1605
1606 status_t err = mSource->setBuffers(audio, mediaBufs);
1607 if (err != OK) {
1608 for (size_t i = 0; i < mediaBufs.size(); ++i) {
1609 mediaBufs[i]->release();
1610 }
1611 mediaBufs.clear();
1612 ALOGE("Secure source didn't support secure mediaBufs.");
1613 return err;
1614 }
1615 }
Andreas Huberf9334412010-12-15 15:17:42 -08001616 return OK;
1617}
1618
Chong Zhangced1c2f2014-08-08 15:22:35 -07001619void NuPlayer::updateVideoSize(
1620 const sp<AMessage> &inputFormat,
1621 const sp<AMessage> &outputFormat) {
1622 if (inputFormat == NULL) {
1623 ALOGW("Unknown video size, reporting 0x0!");
1624 notifyListener(MEDIA_SET_VIDEO_SIZE, 0, 0);
1625 return;
1626 }
1627
1628 int32_t displayWidth, displayHeight;
Chong Zhangced1c2f2014-08-08 15:22:35 -07001629 if (outputFormat != NULL) {
1630 int32_t width, height;
1631 CHECK(outputFormat->findInt32("width", &width));
1632 CHECK(outputFormat->findInt32("height", &height));
1633
1634 int32_t cropLeft, cropTop, cropRight, cropBottom;
1635 CHECK(outputFormat->findRect(
1636 "crop",
1637 &cropLeft, &cropTop, &cropRight, &cropBottom));
1638
1639 displayWidth = cropRight - cropLeft + 1;
1640 displayHeight = cropBottom - cropTop + 1;
1641
1642 ALOGV("Video output format changed to %d x %d "
1643 "(crop: %d x %d @ (%d, %d))",
1644 width, height,
1645 displayWidth,
1646 displayHeight,
1647 cropLeft, cropTop);
1648 } else {
1649 CHECK(inputFormat->findInt32("width", &displayWidth));
1650 CHECK(inputFormat->findInt32("height", &displayHeight));
1651
1652 ALOGV("Video input format %d x %d", displayWidth, displayHeight);
1653 }
1654
1655 // Take into account sample aspect ratio if necessary:
1656 int32_t sarWidth, sarHeight;
1657 if (inputFormat->findInt32("sar-width", &sarWidth)
1658 && inputFormat->findInt32("sar-height", &sarHeight)) {
1659 ALOGV("Sample aspect ratio %d : %d", sarWidth, sarHeight);
1660
1661 displayWidth = (displayWidth * sarWidth) / sarHeight;
1662
1663 ALOGV("display dimensions %d x %d", displayWidth, displayHeight);
1664 }
1665
1666 int32_t rotationDegrees;
1667 if (!inputFormat->findInt32("rotation-degrees", &rotationDegrees)) {
1668 rotationDegrees = 0;
1669 }
1670
1671 if (rotationDegrees == 90 || rotationDegrees == 270) {
1672 int32_t tmp = displayWidth;
1673 displayWidth = displayHeight;
1674 displayHeight = tmp;
1675 }
1676
1677 notifyListener(
1678 MEDIA_SET_VIDEO_SIZE,
1679 displayWidth,
1680 displayHeight);
1681}
1682
Chong Zhangdcb89b32013-08-06 09:44:47 -07001683void NuPlayer::notifyListener(int msg, int ext1, int ext2, const Parcel *in) {
Andreas Huber43c3e6c2011-01-05 12:17:08 -08001684 if (mDriver == NULL) {
Andreas Huberf9334412010-12-15 15:17:42 -08001685 return;
1686 }
1687
Andreas Huber43c3e6c2011-01-05 12:17:08 -08001688 sp<NuPlayerDriver> driver = mDriver.promote();
Andreas Huberf9334412010-12-15 15:17:42 -08001689
Andreas Huber43c3e6c2011-01-05 12:17:08 -08001690 if (driver == NULL) {
Andreas Huberf9334412010-12-15 15:17:42 -08001691 return;
1692 }
1693
Chong Zhangdcb89b32013-08-06 09:44:47 -07001694 driver->notifyListener(msg, ext1, ext2, in);
Andreas Huberf9334412010-12-15 15:17:42 -08001695}
1696
Chong Zhang7137ec72014-11-12 16:41:05 -08001697void NuPlayer::flushDecoder(bool audio, bool needShutdown) {
Andreas Huber14f76722013-01-15 09:04:18 -08001698 ALOGV("[%s] flushDecoder needShutdown=%d",
1699 audio ? "audio" : "video", needShutdown);
1700
Chong Zhang7137ec72014-11-12 16:41:05 -08001701 const sp<DecoderBase> &decoder = getDecoder(audio);
Lajos Molnar87603c02014-08-20 19:25:30 -07001702 if (decoder == NULL) {
Steve Blockdf64d152012-01-04 20:05:49 +00001703 ALOGI("flushDecoder %s without decoder present",
Andreas Huber6e3d3112011-11-28 12:36:11 -08001704 audio ? "audio" : "video");
Lajos Molnar87603c02014-08-20 19:25:30 -07001705 return;
Andreas Huber6e3d3112011-11-28 12:36:11 -08001706 }
1707
Andreas Huber1aef2112011-01-04 14:01:29 -08001708 // Make sure we don't continue to scan sources until we finish flushing.
1709 ++mScanSourcesGeneration;
Chong Zhang3b032b32015-04-17 15:49:06 -07001710 if (mScanSourcesPending) {
1711 mDeferredActions.push_back(
1712 new SimpleAction(&NuPlayer::performScanSources));
1713 mScanSourcesPending = false;
1714 }
Andreas Huber1aef2112011-01-04 14:01:29 -08001715
Chong Zhang7137ec72014-11-12 16:41:05 -08001716 decoder->signalFlush();
Andreas Huber1aef2112011-01-04 14:01:29 -08001717
1718 FlushStatus newStatus =
1719 needShutdown ? FLUSHING_DECODER_SHUTDOWN : FLUSHING_DECODER;
1720
Lajos Molnarfcd3e942015-03-31 10:06:48 -07001721 mFlushComplete[audio][false /* isDecoder */] = (mRenderer == NULL);
Andy Hung8d121d42014-10-03 09:53:53 -07001722 mFlushComplete[audio][true /* isDecoder */] = false;
Andreas Huber1aef2112011-01-04 14:01:29 -08001723 if (audio) {
Wei Jia53904f32014-07-29 10:22:53 -07001724 ALOGE_IF(mFlushingAudio != NONE,
1725 "audio flushDecoder() is called in state %d", mFlushingAudio);
Andreas Huber1aef2112011-01-04 14:01:29 -08001726 mFlushingAudio = newStatus;
Andreas Huber1aef2112011-01-04 14:01:29 -08001727 } else {
Wei Jia53904f32014-07-29 10:22:53 -07001728 ALOGE_IF(mFlushingVideo != NONE,
1729 "video flushDecoder() is called in state %d", mFlushingVideo);
Andreas Huber1aef2112011-01-04 14:01:29 -08001730 mFlushingVideo = newStatus;
Andreas Huber1aef2112011-01-04 14:01:29 -08001731 }
1732}
1733
Chong Zhangced1c2f2014-08-08 15:22:35 -07001734void NuPlayer::queueDecoderShutdown(
1735 bool audio, bool video, const sp<AMessage> &reply) {
1736 ALOGI("queueDecoderShutdown audio=%d, video=%d", audio, video);
Andreas Huber84066782011-08-16 09:34:26 -07001737
Chong Zhangced1c2f2014-08-08 15:22:35 -07001738 mDeferredActions.push_back(
Wei Jiafef808d2014-10-31 17:57:05 -07001739 new FlushDecoderAction(
1740 audio ? FLUSH_CMD_SHUTDOWN : FLUSH_CMD_NONE,
1741 video ? FLUSH_CMD_SHUTDOWN : FLUSH_CMD_NONE));
Andreas Huber84066782011-08-16 09:34:26 -07001742
Chong Zhangced1c2f2014-08-08 15:22:35 -07001743 mDeferredActions.push_back(
1744 new SimpleAction(&NuPlayer::performScanSources));
Andreas Huber84066782011-08-16 09:34:26 -07001745
Chong Zhangced1c2f2014-08-08 15:22:35 -07001746 mDeferredActions.push_back(new PostMessageAction(reply));
1747
1748 processDeferredActions();
Andreas Huber84066782011-08-16 09:34:26 -07001749}
1750
James Dong0d268a32012-08-31 12:18:27 -07001751status_t NuPlayer::setVideoScalingMode(int32_t mode) {
1752 mVideoScalingMode = mode;
Lajos Molnar1de1e252015-04-30 18:18:34 -07001753 if (mSurface != NULL) {
1754 status_t ret = native_window_set_scaling_mode(mSurface.get(), mVideoScalingMode);
James Dong0d268a32012-08-31 12:18:27 -07001755 if (ret != OK) {
1756 ALOGE("Failed to set scaling mode (%d): %s",
1757 -ret, strerror(-ret));
1758 return ret;
1759 }
1760 }
1761 return OK;
1762}
1763
Chong Zhangdcb89b32013-08-06 09:44:47 -07001764status_t NuPlayer::getTrackInfo(Parcel* reply) const {
Lajos Molnar1d15ab52015-03-04 16:46:34 -08001765 sp<AMessage> msg = new AMessage(kWhatGetTrackInfo, this);
Chong Zhangdcb89b32013-08-06 09:44:47 -07001766 msg->setPointer("reply", reply);
1767
1768 sp<AMessage> response;
1769 status_t err = msg->postAndAwaitResponse(&response);
1770 return err;
1771}
1772
Robert Shih7c4f0d72014-07-09 18:53:31 -07001773status_t NuPlayer::getSelectedTrack(int32_t type, Parcel* reply) const {
Lajos Molnar1d15ab52015-03-04 16:46:34 -08001774 sp<AMessage> msg = new AMessage(kWhatGetSelectedTrack, this);
Robert Shih7c4f0d72014-07-09 18:53:31 -07001775 msg->setPointer("reply", reply);
1776 msg->setInt32("type", type);
1777
1778 sp<AMessage> response;
1779 status_t err = msg->postAndAwaitResponse(&response);
1780 if (err == OK && response != NULL) {
1781 CHECK(response->findInt32("err", &err));
1782 }
1783 return err;
1784}
1785
Robert Shih6ffb1fd2014-10-29 16:24:32 -07001786status_t NuPlayer::selectTrack(size_t trackIndex, bool select, int64_t timeUs) {
Lajos Molnar1d15ab52015-03-04 16:46:34 -08001787 sp<AMessage> msg = new AMessage(kWhatSelectTrack, this);
Chong Zhangdcb89b32013-08-06 09:44:47 -07001788 msg->setSize("trackIndex", trackIndex);
1789 msg->setInt32("select", select);
Robert Shih6ffb1fd2014-10-29 16:24:32 -07001790 msg->setInt64("timeUs", timeUs);
Chong Zhangdcb89b32013-08-06 09:44:47 -07001791
1792 sp<AMessage> response;
1793 status_t err = msg->postAndAwaitResponse(&response);
1794
Chong Zhang404fced2014-06-11 14:45:31 -07001795 if (err != OK) {
1796 return err;
1797 }
1798
1799 if (!response->findInt32("err", &err)) {
1800 err = OK;
1801 }
1802
Chong Zhangdcb89b32013-08-06 09:44:47 -07001803 return err;
1804}
1805
Ronghua Wua73d9e02014-10-08 15:13:29 -07001806status_t NuPlayer::getCurrentPosition(int64_t *mediaUs) {
1807 sp<Renderer> renderer = mRenderer;
1808 if (renderer == NULL) {
1809 return NO_INIT;
1810 }
1811
1812 return renderer->getCurrentPosition(mediaUs);
1813}
1814
Praveen Chavane1e5d7a2015-05-19 19:09:48 -07001815void NuPlayer::getStats(Vector<sp<AMessage> > *mTrackStats) {
1816 CHECK(mTrackStats != NULL);
1817
1818 mTrackStats->clear();
1819 if (mVideoDecoder != NULL) {
1820 mTrackStats->push_back(mVideoDecoder->getStats());
1821 }
1822 if (mAudioDecoder != NULL) {
1823 mTrackStats->push_back(mAudioDecoder->getStats());
Chong Zhang7137ec72014-11-12 16:41:05 -08001824 }
Ronghua Wua73d9e02014-10-08 15:13:29 -07001825}
1826
Marco Nelissenf0b72b52014-09-16 15:43:44 -07001827sp<MetaData> NuPlayer::getFileMeta() {
1828 return mSource->getFileFormatMeta();
1829}
1830
Ronghua Wuc8a70d32015-04-29 16:26:34 -07001831float NuPlayer::getFrameRate() {
1832 sp<MetaData> meta = mSource->getFormatMeta(false /* audio */);
1833 if (meta == NULL) {
1834 return 0;
1835 }
1836 int32_t rate;
1837 if (!meta->findInt32(kKeyFrameRate, &rate)) {
1838 // fall back to try file meta
1839 sp<MetaData> fileMeta = getFileMeta();
1840 if (fileMeta == NULL) {
1841 ALOGW("source has video meta but not file meta");
1842 return -1;
1843 }
1844 int32_t fileMetaRate;
1845 if (!fileMeta->findInt32(kKeyFrameRate, &fileMetaRate)) {
1846 return -1;
1847 }
1848 return fileMetaRate;
1849 }
1850 return rate;
1851}
1852
Andreas Huberb7c8e912012-11-27 15:02:53 -08001853void NuPlayer::schedulePollDuration() {
Lajos Molnar1d15ab52015-03-04 16:46:34 -08001854 sp<AMessage> msg = new AMessage(kWhatPollDuration, this);
Andreas Huberb7c8e912012-11-27 15:02:53 -08001855 msg->setInt32("generation", mPollDurationGeneration);
1856 msg->post();
1857}
1858
1859void NuPlayer::cancelPollDuration() {
1860 ++mPollDurationGeneration;
1861}
1862
Andreas Hubera1f8ab02012-11-30 10:53:22 -08001863void NuPlayer::processDeferredActions() {
1864 while (!mDeferredActions.empty()) {
1865 // We won't execute any deferred actions until we're no longer in
1866 // an intermediate state, i.e. one more more decoders are currently
1867 // flushing or shutting down.
1868
Andreas Hubera1f8ab02012-11-30 10:53:22 -08001869 if (mFlushingAudio != NONE || mFlushingVideo != NONE) {
1870 // We're currently flushing, postpone the reset until that's
1871 // completed.
1872
1873 ALOGV("postponing action mFlushingAudio=%d, mFlushingVideo=%d",
1874 mFlushingAudio, mFlushingVideo);
1875
1876 break;
1877 }
1878
1879 sp<Action> action = *mDeferredActions.begin();
1880 mDeferredActions.erase(mDeferredActions.begin());
1881
1882 action->execute(this);
1883 }
1884}
1885
Wei Jia29840802015-05-15 17:11:38 -07001886void NuPlayer::performSeek(int64_t seekTimeUs) {
1887 ALOGV("performSeek seekTimeUs=%lld us (%.2f secs)",
Lajos Molnar6d339f12015-04-17 16:15:53 -07001888 (long long)seekTimeUs,
Wei Jia29840802015-05-15 17:11:38 -07001889 seekTimeUs / 1E6);
Andreas Hubera1f8ab02012-11-30 10:53:22 -08001890
Andy Hungadf34bf2014-09-03 18:22:22 -07001891 if (mSource == NULL) {
1892 // This happens when reset occurs right before the loop mode
1893 // asynchronously seeks to the start of the stream.
1894 LOG_ALWAYS_FATAL_IF(mAudioDecoder != NULL || mVideoDecoder != NULL,
1895 "mSource is NULL and decoders not NULL audio(%p) video(%p)",
1896 mAudioDecoder.get(), mVideoDecoder.get());
1897 return;
1898 }
Robert Shih1a5c8592015-08-04 18:07:44 -07001899 mPreviousSeekTimeUs = seekTimeUs;
Andreas Hubera1f8ab02012-11-30 10:53:22 -08001900 mSource->seekTo(seekTimeUs);
Robert Shihd3b0bbb2014-07-23 15:00:25 -07001901 ++mTimedTextGeneration;
Andreas Hubera1f8ab02012-11-30 10:53:22 -08001902
Andreas Hubera1f8ab02012-11-30 10:53:22 -08001903 // everything's flushed, continue playback.
1904}
1905
Wei Jiafef808d2014-10-31 17:57:05 -07001906void NuPlayer::performDecoderFlush(FlushCommand audio, FlushCommand video) {
1907 ALOGV("performDecoderFlush audio=%d, video=%d", audio, video);
Andreas Hubera1f8ab02012-11-30 10:53:22 -08001908
Wei Jiafef808d2014-10-31 17:57:05 -07001909 if ((audio == FLUSH_CMD_NONE || mAudioDecoder == NULL)
1910 && (video == FLUSH_CMD_NONE || mVideoDecoder == NULL)) {
Andreas Hubera1f8ab02012-11-30 10:53:22 -08001911 return;
1912 }
1913
Wei Jiafef808d2014-10-31 17:57:05 -07001914 if (audio != FLUSH_CMD_NONE && mAudioDecoder != NULL) {
1915 flushDecoder(true /* audio */, (audio == FLUSH_CMD_SHUTDOWN));
Andreas Hubera1f8ab02012-11-30 10:53:22 -08001916 }
1917
Wei Jiafef808d2014-10-31 17:57:05 -07001918 if (video != FLUSH_CMD_NONE && mVideoDecoder != NULL) {
1919 flushDecoder(false /* audio */, (video == FLUSH_CMD_SHUTDOWN));
Andreas Hubera1f8ab02012-11-30 10:53:22 -08001920 }
1921}
1922
1923void NuPlayer::performReset() {
1924 ALOGV("performReset");
1925
1926 CHECK(mAudioDecoder == NULL);
1927 CHECK(mVideoDecoder == NULL);
1928
1929 cancelPollDuration();
1930
1931 ++mScanSourcesGeneration;
1932 mScanSourcesPending = false;
1933
Lajos Molnar09524832014-07-17 14:29:51 -07001934 if (mRendererLooper != NULL) {
1935 if (mRenderer != NULL) {
1936 mRendererLooper->unregisterHandler(mRenderer->id());
1937 }
1938 mRendererLooper->stop();
1939 mRendererLooper.clear();
1940 }
Andreas Hubera1f8ab02012-11-30 10:53:22 -08001941 mRenderer.clear();
Wei Jia57568df2014-09-22 10:16:29 -07001942 ++mRendererGeneration;
Andreas Hubera1f8ab02012-11-30 10:53:22 -08001943
1944 if (mSource != NULL) {
1945 mSource->stop();
Andreas Huberb5f25f02013-02-05 10:14:26 -08001946
Andreas Hubera1f8ab02012-11-30 10:53:22 -08001947 mSource.clear();
1948 }
1949
1950 if (mDriver != NULL) {
1951 sp<NuPlayerDriver> driver = mDriver.promote();
1952 if (driver != NULL) {
1953 driver->notifyResetComplete();
1954 }
1955 }
Andreas Huber57a339c2012-12-03 11:18:00 -08001956
1957 mStarted = false;
Ronghua Wu64c2d172015-10-07 16:52:19 -07001958 mResetting = false;
Robert Shih0c61a0d2015-07-06 15:09:10 -07001959 mSourceStarted = false;
Andreas Hubera1f8ab02012-11-30 10:53:22 -08001960}
1961
1962void NuPlayer::performScanSources() {
1963 ALOGV("performScanSources");
1964
Andreas Huber57a339c2012-12-03 11:18:00 -08001965 if (!mStarted) {
1966 return;
1967 }
1968
Andreas Hubera1f8ab02012-11-30 10:53:22 -08001969 if (mAudioDecoder == NULL || mVideoDecoder == NULL) {
1970 postScanSources();
1971 }
1972}
1973
Lajos Molnar1de1e252015-04-30 18:18:34 -07001974void NuPlayer::performSetSurface(const sp<Surface> &surface) {
Andreas Huber57a339c2012-12-03 11:18:00 -08001975 ALOGV("performSetSurface");
1976
Lajos Molnar1de1e252015-04-30 18:18:34 -07001977 mSurface = surface;
Andreas Huber57a339c2012-12-03 11:18:00 -08001978
1979 // XXX - ignore error from setVideoScalingMode for now
1980 setVideoScalingMode(mVideoScalingMode);
Chong Zhang13d6faa2014-08-22 15:35:28 -07001981
1982 if (mDriver != NULL) {
1983 sp<NuPlayerDriver> driver = mDriver.promote();
1984 if (driver != NULL) {
1985 driver->notifySetSurfaceComplete();
1986 }
1987 }
Andreas Huber57a339c2012-12-03 11:18:00 -08001988}
1989
Chong Zhangf8d71772014-11-26 15:08:34 -08001990void NuPlayer::performResumeDecoders(bool needNotify) {
1991 if (needNotify) {
1992 mResumePending = true;
1993 if (mVideoDecoder == NULL) {
1994 // if audio-only, we can notify seek complete now,
1995 // as the resume operation will be relatively fast.
1996 finishResume();
1997 }
1998 }
1999
Chong Zhang7137ec72014-11-12 16:41:05 -08002000 if (mVideoDecoder != NULL) {
Chong Zhangf8d71772014-11-26 15:08:34 -08002001 // When there is continuous seek, MediaPlayer will cache the seek
2002 // position, and send down new seek request when previous seek is
2003 // complete. Let's wait for at least one video output frame before
2004 // notifying seek complete, so that the video thumbnail gets updated
2005 // when seekbar is dragged.
2006 mVideoDecoder->signalResume(needNotify);
Chong Zhang7137ec72014-11-12 16:41:05 -08002007 }
2008
2009 if (mAudioDecoder != NULL) {
Chong Zhangf8d71772014-11-26 15:08:34 -08002010 mAudioDecoder->signalResume(false /* needNotify */);
2011 }
2012}
2013
2014void NuPlayer::finishResume() {
2015 if (mResumePending) {
2016 mResumePending = false;
Wei Jia1061c9c2015-05-19 16:02:17 -07002017 notifyDriverSeekComplete();
2018 }
2019}
2020
2021void NuPlayer::notifyDriverSeekComplete() {
2022 if (mDriver != NULL) {
2023 sp<NuPlayerDriver> driver = mDriver.promote();
2024 if (driver != NULL) {
2025 driver->notifySeekComplete();
Chong Zhangf8d71772014-11-26 15:08:34 -08002026 }
Chong Zhang7137ec72014-11-12 16:41:05 -08002027 }
2028}
2029
Andreas Huber9575c962013-02-05 13:59:56 -08002030void NuPlayer::onSourceNotify(const sp<AMessage> &msg) {
2031 int32_t what;
2032 CHECK(msg->findInt32("what", &what));
2033
2034 switch (what) {
Lajos Molnarfcd3e942015-03-31 10:06:48 -07002035 case Source::kWhatInstantiateSecureDecoders:
2036 {
2037 if (mSource == NULL) {
2038 // This is a stale notification from a source that was
2039 // asynchronously preparing when the client called reset().
2040 // We handled the reset, the source is gone.
2041 break;
2042 }
2043
2044 sp<AMessage> reply;
2045 CHECK(msg->findMessage("reply", &reply));
2046 status_t err = onInstantiateSecureDecoders();
2047 reply->setInt32("err", err);
2048 reply->post();
2049 break;
2050 }
2051
Andreas Huber9575c962013-02-05 13:59:56 -08002052 case Source::kWhatPrepared:
2053 {
Andreas Huberb5f28d42013-04-25 15:11:19 -07002054 if (mSource == NULL) {
2055 // This is a stale notification from a source that was
2056 // asynchronously preparing when the client called reset().
2057 // We handled the reset, the source is gone.
2058 break;
2059 }
2060
Andreas Huberec0c5972013-02-05 14:47:13 -08002061 int32_t err;
2062 CHECK(msg->findInt32("err", &err));
2063
Lajos Molnarfcd3e942015-03-31 10:06:48 -07002064 if (err != OK) {
2065 // shut down potential secure codecs in case client never calls reset
2066 mDeferredActions.push_back(
2067 new FlushDecoderAction(FLUSH_CMD_SHUTDOWN /* audio */,
2068 FLUSH_CMD_SHUTDOWN /* video */));
2069 processDeferredActions();
2070 }
2071
Andreas Huber9575c962013-02-05 13:59:56 -08002072 sp<NuPlayerDriver> driver = mDriver.promote();
2073 if (driver != NULL) {
Marco Nelissendd114d12014-05-28 15:23:14 -07002074 // notify duration first, so that it's definitely set when
2075 // the app received the "prepare complete" callback.
2076 int64_t durationUs;
2077 if (mSource->getDuration(&durationUs) == OK) {
2078 driver->notifyDuration(durationUs);
2079 }
Andreas Huberec0c5972013-02-05 14:47:13 -08002080 driver->notifyPrepareCompleted(err);
Andreas Huber9575c962013-02-05 13:59:56 -08002081 }
Andreas Huber99759402013-04-01 14:28:31 -07002082
Andreas Huber9575c962013-02-05 13:59:56 -08002083 break;
2084 }
2085
2086 case Source::kWhatFlagsChanged:
2087 {
2088 uint32_t flags;
2089 CHECK(msg->findInt32("flags", (int32_t *)&flags));
2090
Chong Zhang4b7069d2013-09-11 12:52:43 -07002091 sp<NuPlayerDriver> driver = mDriver.promote();
2092 if (driver != NULL) {
Wei Jia895651b2014-12-10 17:31:52 -08002093 if ((flags & NuPlayer::Source::FLAG_CAN_SEEK) == 0) {
2094 driver->notifyListener(
2095 MEDIA_INFO, MEDIA_INFO_NOT_SEEKABLE, 0);
2096 }
Chong Zhang4b7069d2013-09-11 12:52:43 -07002097 driver->notifyFlagsChanged(flags);
2098 }
2099
Andreas Huber9575c962013-02-05 13:59:56 -08002100 if ((mSourceFlags & Source::FLAG_DYNAMIC_DURATION)
2101 && (!(flags & Source::FLAG_DYNAMIC_DURATION))) {
2102 cancelPollDuration();
2103 } else if (!(mSourceFlags & Source::FLAG_DYNAMIC_DURATION)
2104 && (flags & Source::FLAG_DYNAMIC_DURATION)
2105 && (mAudioDecoder != NULL || mVideoDecoder != NULL)) {
2106 schedulePollDuration();
2107 }
2108
2109 mSourceFlags = flags;
2110 break;
2111 }
2112
2113 case Source::kWhatVideoSizeChanged:
2114 {
Chong Zhangced1c2f2014-08-08 15:22:35 -07002115 sp<AMessage> format;
2116 CHECK(msg->findMessage("format", &format));
Andreas Huber9575c962013-02-05 13:59:56 -08002117
Chong Zhangced1c2f2014-08-08 15:22:35 -07002118 updateVideoSize(format);
Andreas Huber9575c962013-02-05 13:59:56 -08002119 break;
2120 }
2121
Chong Zhang2a3cc9a2014-08-21 17:48:26 -07002122 case Source::kWhatBufferingUpdate:
2123 {
2124 int32_t percentage;
2125 CHECK(msg->findInt32("percentage", &percentage));
2126
2127 notifyListener(MEDIA_BUFFERING_UPDATE, percentage, 0);
2128 break;
2129 }
2130
Chong Zhangefbb6192015-01-30 17:13:27 -08002131 case Source::kWhatPauseOnBufferingStart:
2132 {
2133 // ignore if not playing
Chong Zhang8a048332015-05-06 15:16:28 -07002134 if (mStarted) {
Chong Zhangefbb6192015-01-30 17:13:27 -08002135 ALOGI("buffer low, pausing...");
2136
Chong Zhang8a048332015-05-06 15:16:28 -07002137 mPausedForBuffering = true;
Chong Zhangefbb6192015-01-30 17:13:27 -08002138 onPause();
2139 }
2140 // fall-thru
2141 }
2142
Roger Jönssonb50e83e2013-01-21 16:26:41 +01002143 case Source::kWhatBufferingStart:
2144 {
2145 notifyListener(MEDIA_INFO, MEDIA_INFO_BUFFERING_START, 0);
2146 break;
2147 }
2148
Chong Zhangefbb6192015-01-30 17:13:27 -08002149 case Source::kWhatResumeOnBufferingEnd:
2150 {
2151 // ignore if not playing
Chong Zhang8a048332015-05-06 15:16:28 -07002152 if (mStarted) {
Chong Zhangefbb6192015-01-30 17:13:27 -08002153 ALOGI("buffer ready, resuming...");
2154
Chong Zhang8a048332015-05-06 15:16:28 -07002155 mPausedForBuffering = false;
2156
2157 // do not resume yet if client didn't unpause
2158 if (!mPausedByClient) {
2159 onResume();
2160 }
Chong Zhangefbb6192015-01-30 17:13:27 -08002161 }
2162 // fall-thru
2163 }
2164
Roger Jönssonb50e83e2013-01-21 16:26:41 +01002165 case Source::kWhatBufferingEnd:
2166 {
2167 notifyListener(MEDIA_INFO, MEDIA_INFO_BUFFERING_END, 0);
2168 break;
2169 }
2170
Chong Zhangefbb6192015-01-30 17:13:27 -08002171 case Source::kWhatCacheStats:
2172 {
2173 int32_t kbps;
2174 CHECK(msg->findInt32("bandwidth", &kbps));
2175
2176 notifyListener(MEDIA_INFO, MEDIA_INFO_NETWORK_BANDWIDTH, kbps);
2177 break;
2178 }
2179
Chong Zhangdcb89b32013-08-06 09:44:47 -07002180 case Source::kWhatSubtitleData:
2181 {
2182 sp<ABuffer> buffer;
2183 CHECK(msg->findBuffer("buffer", &buffer));
2184
Chong Zhang404fced2014-06-11 14:45:31 -07002185 sendSubtitleData(buffer, 0 /* baseIndex */);
Chong Zhangdcb89b32013-08-06 09:44:47 -07002186 break;
2187 }
2188
Robert Shih08528432015-04-08 09:06:54 -07002189 case Source::kWhatTimedMetaData:
2190 {
2191 sp<ABuffer> buffer;
2192 if (!msg->findBuffer("buffer", &buffer)) {
2193 notifyListener(MEDIA_INFO, MEDIA_INFO_METADATA_UPDATE, 0);
2194 } else {
2195 sendTimedMetaData(buffer);
2196 }
2197 break;
2198 }
2199
Robert Shihd3b0bbb2014-07-23 15:00:25 -07002200 case Source::kWhatTimedTextData:
2201 {
2202 int32_t generation;
2203 if (msg->findInt32("generation", &generation)
2204 && generation != mTimedTextGeneration) {
2205 break;
2206 }
2207
2208 sp<ABuffer> buffer;
2209 CHECK(msg->findBuffer("buffer", &buffer));
2210
2211 sp<NuPlayerDriver> driver = mDriver.promote();
2212 if (driver == NULL) {
2213 break;
2214 }
2215
2216 int posMs;
2217 int64_t timeUs, posUs;
2218 driver->getCurrentPosition(&posMs);
Patrik2 Carlsson24d484b2015-01-27 16:49:45 +01002219 posUs = (int64_t) posMs * 1000ll;
Robert Shihd3b0bbb2014-07-23 15:00:25 -07002220 CHECK(buffer->meta()->findInt64("timeUs", &timeUs));
2221
2222 if (posUs < timeUs) {
2223 if (!msg->findInt32("generation", &generation)) {
2224 msg->setInt32("generation", mTimedTextGeneration);
2225 }
2226 msg->post(timeUs - posUs);
2227 } else {
2228 sendTimedTextData(buffer);
2229 }
2230 break;
2231 }
2232
Andreas Huber14f76722013-01-15 09:04:18 -08002233 case Source::kWhatQueueDecoderShutdown:
2234 {
2235 int32_t audio, video;
2236 CHECK(msg->findInt32("audio", &audio));
2237 CHECK(msg->findInt32("video", &video));
2238
2239 sp<AMessage> reply;
2240 CHECK(msg->findMessage("reply", &reply));
2241
2242 queueDecoderShutdown(audio, video, reply);
2243 break;
2244 }
2245
Ronghua Wu80276872014-08-28 15:50:29 -07002246 case Source::kWhatDrmNoLicense:
2247 {
2248 notifyListener(MEDIA_ERROR, MEDIA_ERROR_UNKNOWN, ERROR_DRM_NO_LICENSE);
2249 break;
2250 }
2251
Andreas Huber9575c962013-02-05 13:59:56 -08002252 default:
2253 TRESPASS();
2254 }
2255}
2256
Chong Zhanga7fa1d92014-06-11 14:49:23 -07002257void NuPlayer::onClosedCaptionNotify(const sp<AMessage> &msg) {
2258 int32_t what;
2259 CHECK(msg->findInt32("what", &what));
2260
2261 switch (what) {
2262 case NuPlayer::CCDecoder::kWhatClosedCaptionData:
2263 {
2264 sp<ABuffer> buffer;
2265 CHECK(msg->findBuffer("buffer", &buffer));
2266
2267 size_t inbandTracks = 0;
2268 if (mSource != NULL) {
2269 inbandTracks = mSource->getTrackCount();
2270 }
2271
2272 sendSubtitleData(buffer, inbandTracks);
2273 break;
2274 }
2275
2276 case NuPlayer::CCDecoder::kWhatTrackAdded:
2277 {
2278 notifyListener(MEDIA_INFO, MEDIA_INFO_METADATA_UPDATE, 0);
2279
2280 break;
2281 }
2282
2283 default:
2284 TRESPASS();
2285 }
2286
2287
2288}
2289
Chong Zhang404fced2014-06-11 14:45:31 -07002290void NuPlayer::sendSubtitleData(const sp<ABuffer> &buffer, int32_t baseIndex) {
2291 int32_t trackIndex;
2292 int64_t timeUs, durationUs;
2293 CHECK(buffer->meta()->findInt32("trackIndex", &trackIndex));
2294 CHECK(buffer->meta()->findInt64("timeUs", &timeUs));
2295 CHECK(buffer->meta()->findInt64("durationUs", &durationUs));
2296
2297 Parcel in;
2298 in.writeInt32(trackIndex + baseIndex);
2299 in.writeInt64(timeUs);
2300 in.writeInt64(durationUs);
2301 in.writeInt32(buffer->size());
2302 in.writeInt32(buffer->size());
2303 in.write(buffer->data(), buffer->size());
2304
2305 notifyListener(MEDIA_SUBTITLE_DATA, 0, 0, &in);
2306}
Robert Shihd3b0bbb2014-07-23 15:00:25 -07002307
Robert Shih08528432015-04-08 09:06:54 -07002308void NuPlayer::sendTimedMetaData(const sp<ABuffer> &buffer) {
2309 int64_t timeUs;
2310 CHECK(buffer->meta()->findInt64("timeUs", &timeUs));
2311
2312 Parcel in;
2313 in.writeInt64(timeUs);
2314 in.writeInt32(buffer->size());
2315 in.writeInt32(buffer->size());
2316 in.write(buffer->data(), buffer->size());
2317
2318 notifyListener(MEDIA_META_DATA, 0, 0, &in);
2319}
2320
Robert Shihd3b0bbb2014-07-23 15:00:25 -07002321void NuPlayer::sendTimedTextData(const sp<ABuffer> &buffer) {
2322 const void *data;
2323 size_t size = 0;
2324 int64_t timeUs;
Marco Nelissen55e2f4c2015-09-04 15:57:15 -07002325 int32_t flag = TextDescriptions::IN_BAND_TEXT_3GPP;
Robert Shihd3b0bbb2014-07-23 15:00:25 -07002326
2327 AString mime;
2328 CHECK(buffer->meta()->findString("mime", &mime));
2329 CHECK(strcasecmp(mime.c_str(), MEDIA_MIMETYPE_TEXT_3GPP) == 0);
2330
2331 data = buffer->data();
2332 size = buffer->size();
2333
2334 Parcel parcel;
2335 if (size > 0) {
2336 CHECK(buffer->meta()->findInt64("timeUs", &timeUs));
Marco Nelissen55e2f4c2015-09-04 15:57:15 -07002337 int32_t global = 0;
2338 if (buffer->meta()->findInt32("global", &global) && global) {
2339 flag |= TextDescriptions::GLOBAL_DESCRIPTIONS;
2340 } else {
2341 flag |= TextDescriptions::LOCAL_DESCRIPTIONS;
2342 }
Robert Shihd3b0bbb2014-07-23 15:00:25 -07002343 TextDescriptions::getParcelOfDescriptions(
2344 (const uint8_t *)data, size, flag, timeUs / 1000, &parcel);
2345 }
2346
2347 if ((parcel.dataSize() > 0)) {
2348 notifyListener(MEDIA_TIMED_TEXT, 0, 0, &parcel);
2349 } else { // send an empty timed text
2350 notifyListener(MEDIA_TIMED_TEXT, 0, 0);
2351 }
2352}
Andreas Huberb5f25f02013-02-05 10:14:26 -08002353////////////////////////////////////////////////////////////////////////////////
2354
Chong Zhangced1c2f2014-08-08 15:22:35 -07002355sp<AMessage> NuPlayer::Source::getFormat(bool audio) {
2356 sp<MetaData> meta = getFormatMeta(audio);
2357
2358 if (meta == NULL) {
2359 return NULL;
2360 }
2361
2362 sp<AMessage> msg = new AMessage;
2363
2364 if(convertMetaDataToMessage(meta, &msg) == OK) {
2365 return msg;
2366 }
2367 return NULL;
2368}
2369
Andreas Huber9575c962013-02-05 13:59:56 -08002370void NuPlayer::Source::notifyFlagsChanged(uint32_t flags) {
2371 sp<AMessage> notify = dupNotify();
2372 notify->setInt32("what", kWhatFlagsChanged);
2373 notify->setInt32("flags", flags);
2374 notify->post();
2375}
2376
Chong Zhangced1c2f2014-08-08 15:22:35 -07002377void NuPlayer::Source::notifyVideoSizeChanged(const sp<AMessage> &format) {
Andreas Huber9575c962013-02-05 13:59:56 -08002378 sp<AMessage> notify = dupNotify();
2379 notify->setInt32("what", kWhatVideoSizeChanged);
Chong Zhangced1c2f2014-08-08 15:22:35 -07002380 notify->setMessage("format", format);
Andreas Huber9575c962013-02-05 13:59:56 -08002381 notify->post();
2382}
2383
Andreas Huberec0c5972013-02-05 14:47:13 -08002384void NuPlayer::Source::notifyPrepared(status_t err) {
Andreas Huber9575c962013-02-05 13:59:56 -08002385 sp<AMessage> notify = dupNotify();
2386 notify->setInt32("what", kWhatPrepared);
Andreas Huberec0c5972013-02-05 14:47:13 -08002387 notify->setInt32("err", err);
Andreas Huber9575c962013-02-05 13:59:56 -08002388 notify->post();
2389}
2390
Lajos Molnarfcd3e942015-03-31 10:06:48 -07002391void NuPlayer::Source::notifyInstantiateSecureDecoders(const sp<AMessage> &reply) {
2392 sp<AMessage> notify = dupNotify();
2393 notify->setInt32("what", kWhatInstantiateSecureDecoders);
2394 notify->setMessage("reply", reply);
2395 notify->post();
2396}
2397
Andreas Huber84333e02014-02-07 15:36:10 -08002398void NuPlayer::Source::onMessageReceived(const sp<AMessage> & /* msg */) {
Andreas Huberb5f25f02013-02-05 10:14:26 -08002399 TRESPASS();
2400}
2401
Andreas Huberf9334412010-12-15 15:17:42 -08002402} // namespace android