blob: 8b0ecadc752f72d0c5a7d3ff0b27f6b4b1fabd86 [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),
Robert Shih0c61a0d2015-07-06 15:09:10 -0700191 mSourceStarted(false),
Chong Zhangefbb6192015-01-30 17:13:27 -0800192 mPaused(false),
Chong Zhang8a048332015-05-06 15:16:28 -0700193 mPausedByClient(false),
194 mPausedForBuffering(false) {
Andy Hung8d121d42014-10-03 09:53:53 -0700195 clearFlushComplete();
Andreas Huberf9334412010-12-15 15:17:42 -0800196}
197
198NuPlayer::~NuPlayer() {
199}
200
Andreas Huber9b80c2b2011-06-30 15:47:02 -0700201void NuPlayer::setUID(uid_t uid) {
202 mUIDValid = true;
203 mUID = uid;
204}
205
Andreas Huber43c3e6c2011-01-05 12:17:08 -0800206void NuPlayer::setDriver(const wp<NuPlayerDriver> &driver) {
207 mDriver = driver;
Andreas Huberf9334412010-12-15 15:17:42 -0800208}
209
Andreas Huber9575c962013-02-05 13:59:56 -0800210void NuPlayer::setDataSourceAsync(const sp<IStreamSource> &source) {
Lajos Molnar1d15ab52015-03-04 16:46:34 -0800211 sp<AMessage> msg = new AMessage(kWhatSetDataSource, this);
Andreas Huberf9334412010-12-15 15:17:42 -0800212
Lajos Molnar1d15ab52015-03-04 16:46:34 -0800213 sp<AMessage> notify = new AMessage(kWhatSourceNotify, this);
Andreas Huberb5f25f02013-02-05 10:14:26 -0800214
Andreas Huber240abcc2014-02-13 13:32:37 -0800215 msg->setObject("source", new StreamingSource(notify, source));
Andreas Huber5bc087c2010-12-23 10:27:40 -0800216 msg->post();
217}
Andreas Huberf9334412010-12-15 15:17:42 -0800218
Andreas Huberafed0e12011-09-20 15:39:58 -0700219static bool IsHTTPLiveURL(const char *url) {
220 if (!strncasecmp("http://", url, 7)
Andreas Huber99759402013-04-01 14:28:31 -0700221 || !strncasecmp("https://", url, 8)
222 || !strncasecmp("file://", url, 7)) {
Andreas Huberafed0e12011-09-20 15:39:58 -0700223 size_t len = strlen(url);
224 if (len >= 5 && !strcasecmp(".m3u8", &url[len - 5])) {
225 return true;
226 }
227
228 if (strstr(url,"m3u8")) {
229 return true;
230 }
231 }
232
233 return false;
234}
235
Andreas Huber9575c962013-02-05 13:59:56 -0800236void NuPlayer::setDataSourceAsync(
Andreas Huber1b86fe02014-01-29 11:13:26 -0800237 const sp<IMediaHTTPService> &httpService,
238 const char *url,
239 const KeyedVector<String8, String8> *headers) {
Chong Zhang3de157d2014-08-05 20:54:44 -0700240
Lajos Molnar1d15ab52015-03-04 16:46:34 -0800241 sp<AMessage> msg = new AMessage(kWhatSetDataSource, this);
Oscar Rydhé7a33b772012-02-20 10:15:48 +0100242 size_t len = strlen(url);
Andreas Huber5bc087c2010-12-23 10:27:40 -0800243
Lajos Molnar1d15ab52015-03-04 16:46:34 -0800244 sp<AMessage> notify = new AMessage(kWhatSourceNotify, this);
Andreas Huberb5f25f02013-02-05 10:14:26 -0800245
Andreas Huberafed0e12011-09-20 15:39:58 -0700246 sp<Source> source;
247 if (IsHTTPLiveURL(url)) {
Andreas Huber81e68442014-02-05 11:52:33 -0800248 source = new HTTPLiveSource(notify, httpService, url, headers);
Andreas Huberafed0e12011-09-20 15:39:58 -0700249 } else if (!strncasecmp(url, "rtsp://", 7)) {
Andreas Huber1b86fe02014-01-29 11:13:26 -0800250 source = new RTSPSource(
251 notify, httpService, url, headers, mUIDValid, mUID);
Oscar Rydhé7a33b772012-02-20 10:15:48 +0100252 } else if ((!strncasecmp(url, "http://", 7)
253 || !strncasecmp(url, "https://", 8))
254 && ((len >= 4 && !strcasecmp(".sdp", &url[len - 4]))
255 || strstr(url, ".sdp?"))) {
Andreas Huber1b86fe02014-01-29 11:13:26 -0800256 source = new RTSPSource(
257 notify, httpService, url, headers, mUIDValid, mUID, true);
Andreas Huber2bfdd422011-10-11 15:24:07 -0700258 } else {
Chong Zhang3de157d2014-08-05 20:54:44 -0700259 sp<GenericSource> genericSource =
260 new GenericSource(notify, mUIDValid, mUID);
261 // Don't set FLAG_SECURE on mSourceFlags here for widevine.
262 // The correct flags will be updated in Source::kWhatFlagsChanged
263 // handler when GenericSource is prepared.
Andreas Huber2bfdd422011-10-11 15:24:07 -0700264
Chong Zhanga19f33e2014-08-07 15:35:07 -0700265 status_t err = genericSource->setDataSource(httpService, url, headers);
Chong Zhang3de157d2014-08-05 20:54:44 -0700266
267 if (err == OK) {
268 source = genericSource;
269 } else {
Chong Zhanga19f33e2014-08-07 15:35:07 -0700270 ALOGE("Failed to set data source!");
Chong Zhang3de157d2014-08-05 20:54:44 -0700271 }
272 }
Andreas Huberafed0e12011-09-20 15:39:58 -0700273 msg->setObject("source", source);
274 msg->post();
275}
276
Andreas Huber9575c962013-02-05 13:59:56 -0800277void NuPlayer::setDataSourceAsync(int fd, int64_t offset, int64_t length) {
Lajos Molnar1d15ab52015-03-04 16:46:34 -0800278 sp<AMessage> msg = new AMessage(kWhatSetDataSource, this);
Andreas Huberafed0e12011-09-20 15:39:58 -0700279
Lajos Molnar1d15ab52015-03-04 16:46:34 -0800280 sp<AMessage> notify = new AMessage(kWhatSourceNotify, this);
Andreas Huberb5f25f02013-02-05 10:14:26 -0800281
Chong Zhang3de157d2014-08-05 20:54:44 -0700282 sp<GenericSource> source =
283 new GenericSource(notify, mUIDValid, mUID);
284
Chong Zhanga19f33e2014-08-07 15:35:07 -0700285 status_t err = source->setDataSource(fd, offset, length);
Chong Zhang3de157d2014-08-05 20:54:44 -0700286
287 if (err != OK) {
Chong Zhanga19f33e2014-08-07 15:35:07 -0700288 ALOGE("Failed to set data source!");
Chong Zhang3de157d2014-08-05 20:54:44 -0700289 source = NULL;
290 }
291
Andreas Huberafed0e12011-09-20 15:39:58 -0700292 msg->setObject("source", source);
Andreas Huberf9334412010-12-15 15:17:42 -0800293 msg->post();
294}
295
Chris Watkins99f31602015-03-20 13:06:33 -0700296void NuPlayer::setDataSourceAsync(const sp<DataSource> &dataSource) {
297 sp<AMessage> msg = new AMessage(kWhatSetDataSource, this);
298 sp<AMessage> notify = new AMessage(kWhatSourceNotify, this);
299
300 sp<GenericSource> source = new GenericSource(notify, mUIDValid, mUID);
301 status_t err = source->setDataSource(dataSource);
302
303 if (err != OK) {
304 ALOGE("Failed to set data source!");
305 source = NULL;
306 }
307
308 msg->setObject("source", source);
309 msg->post();
310}
311
Andreas Huber9575c962013-02-05 13:59:56 -0800312void NuPlayer::prepareAsync() {
Lajos Molnar1d15ab52015-03-04 16:46:34 -0800313 (new AMessage(kWhatPrepare, this))->post();
Andreas Huber9575c962013-02-05 13:59:56 -0800314}
315
Andreas Huber57a339c2012-12-03 11:18:00 -0800316void NuPlayer::setVideoSurfaceTextureAsync(
Andy McFadden8ba01022012-12-18 09:46:54 -0800317 const sp<IGraphicBufferProducer> &bufferProducer) {
Lajos Molnar1de1e252015-04-30 18:18:34 -0700318 sp<AMessage> msg = new AMessage(kWhatSetVideoSurface, this);
Andreas Huber57a339c2012-12-03 11:18:00 -0800319
Andy McFadden8ba01022012-12-18 09:46:54 -0800320 if (bufferProducer == NULL) {
Lajos Molnar1de1e252015-04-30 18:18:34 -0700321 msg->setObject("surface", NULL);
Andreas Huber57a339c2012-12-03 11:18:00 -0800322 } else {
Lajos Molnar1de1e252015-04-30 18:18:34 -0700323 msg->setObject("surface", new Surface(bufferProducer, true /* controlledByApp */));
Andreas Huber57a339c2012-12-03 11:18:00 -0800324 }
325
Andreas Huberf9334412010-12-15 15:17:42 -0800326 msg->post();
327}
328
329void NuPlayer::setAudioSink(const sp<MediaPlayerBase::AudioSink> &sink) {
Lajos Molnar1d15ab52015-03-04 16:46:34 -0800330 sp<AMessage> msg = new AMessage(kWhatSetAudioSink, this);
Andreas Huberf9334412010-12-15 15:17:42 -0800331 msg->setObject("sink", sink);
332 msg->post();
333}
334
335void NuPlayer::start() {
Lajos Molnar1d15ab52015-03-04 16:46:34 -0800336 (new AMessage(kWhatStart, this))->post();
Andreas Huberf9334412010-12-15 15:17:42 -0800337}
338
Lajos Molnar3a474aa2015-04-24 17:10:07 -0700339status_t NuPlayer::setPlaybackSettings(const AudioPlaybackRate &rate) {
340 // do some cursory validation of the settings here. audio modes are
341 // only validated when set on the audiosink.
342 if ((rate.mSpeed != 0.f && rate.mSpeed < AUDIO_TIMESTRETCH_SPEED_MIN)
343 || rate.mSpeed > AUDIO_TIMESTRETCH_SPEED_MAX
344 || rate.mPitch < AUDIO_TIMESTRETCH_SPEED_MIN
345 || rate.mPitch > AUDIO_TIMESTRETCH_SPEED_MAX) {
346 return BAD_VALUE;
347 }
348 sp<AMessage> msg = new AMessage(kWhatConfigPlayback, this);
349 writeToAMessage(msg, rate);
350 sp<AMessage> response;
351 status_t err = msg->postAndAwaitResponse(&response);
352 if (err == OK && response != NULL) {
353 CHECK(response->findInt32("err", &err));
354 }
355 return err;
356}
357
358status_t NuPlayer::getPlaybackSettings(AudioPlaybackRate *rate /* nonnull */) {
359 sp<AMessage> msg = new AMessage(kWhatGetPlaybackSettings, this);
360 sp<AMessage> response;
361 status_t err = msg->postAndAwaitResponse(&response);
362 if (err == OK && response != NULL) {
363 CHECK(response->findInt32("err", &err));
364 if (err == OK) {
365 readFromAMessage(response, rate);
366 }
367 }
368 return err;
369}
370
371status_t NuPlayer::setSyncSettings(const AVSyncSettings &sync, float videoFpsHint) {
372 sp<AMessage> msg = new AMessage(kWhatConfigSync, this);
373 writeToAMessage(msg, sync, videoFpsHint);
374 sp<AMessage> response;
375 status_t err = msg->postAndAwaitResponse(&response);
376 if (err == OK && response != NULL) {
377 CHECK(response->findInt32("err", &err));
378 }
379 return err;
380}
381
382status_t NuPlayer::getSyncSettings(
383 AVSyncSettings *sync /* nonnull */, float *videoFps /* nonnull */) {
384 sp<AMessage> msg = new AMessage(kWhatGetSyncSettings, this);
385 sp<AMessage> response;
386 status_t err = msg->postAndAwaitResponse(&response);
387 if (err == OK && response != NULL) {
388 CHECK(response->findInt32("err", &err));
389 if (err == OK) {
390 readFromAMessage(response, sync, videoFps);
391 }
392 }
393 return err;
Wei Jia98160162015-02-04 17:01:11 -0800394}
395
Andreas Huber43c3e6c2011-01-05 12:17:08 -0800396void NuPlayer::pause() {
Lajos Molnar1d15ab52015-03-04 16:46:34 -0800397 (new AMessage(kWhatPause, this))->post();
Andreas Huber43c3e6c2011-01-05 12:17:08 -0800398}
399
Andreas Huber1aef2112011-01-04 14:01:29 -0800400void NuPlayer::resetAsync() {
Chong Zhang48296b72014-09-14 14:28:45 -0700401 if (mSource != NULL) {
402 // During a reset, the data source might be unresponsive already, we need to
403 // disconnect explicitly so that reads exit promptly.
404 // We can't queue the disconnect request to the looper, as it might be
405 // queued behind a stuck read and never gets processed.
406 // Doing a disconnect outside the looper to allows the pending reads to exit
407 // (either successfully or with error).
408 mSource->disconnect();
409 }
410
Lajos Molnar1d15ab52015-03-04 16:46:34 -0800411 (new AMessage(kWhatReset, this))->post();
Andreas Huber1aef2112011-01-04 14:01:29 -0800412}
413
Wei Jiae427abf2014-09-22 15:21:11 -0700414void NuPlayer::seekToAsync(int64_t seekTimeUs, bool needNotify) {
Lajos Molnar1d15ab52015-03-04 16:46:34 -0800415 sp<AMessage> msg = new AMessage(kWhatSeek, this);
Andreas Huber43c3e6c2011-01-05 12:17:08 -0800416 msg->setInt64("seekTimeUs", seekTimeUs);
Wei Jiae427abf2014-09-22 15:21:11 -0700417 msg->setInt32("needNotify", needNotify);
Andreas Huber43c3e6c2011-01-05 12:17:08 -0800418 msg->post();
419}
420
Andreas Huber53df1a42010-12-22 10:03:04 -0800421
Chong Zhang404fced2014-06-11 14:45:31 -0700422void NuPlayer::writeTrackInfo(
423 Parcel* reply, const sp<AMessage> format) const {
Marco Nelissenc367ca12015-09-15 09:51:59 -0700424 if (format == NULL) {
425 ALOGE("NULL format");
426 return;
427 }
Chong Zhang404fced2014-06-11 14:45:31 -0700428 int32_t trackType;
Marco Nelissen9436e482015-09-15 10:21:53 -0700429 if (!format->findInt32("type", &trackType)) {
Marco Nelissenc367ca12015-09-15 09:51:59 -0700430 ALOGE("no track type");
431 return;
432 }
Chong Zhang404fced2014-06-11 14:45:31 -0700433
Robert Shih755106e2015-04-30 14:36:45 -0700434 AString mime;
Robert Shih2e3a4252015-05-06 10:21:15 -0700435 if (!format->findString("mime", &mime)) {
436 // Java MediaPlayer only uses mimetype for subtitle and timedtext tracks.
437 // If we can't find the mimetype here it means that we wouldn't be needing
438 // the mimetype on the Java end. We still write a placeholder mime to keep the
439 // (de)serialization logic simple.
440 if (trackType == MEDIA_TRACK_TYPE_AUDIO) {
441 mime = "audio/";
442 } else if (trackType == MEDIA_TRACK_TYPE_VIDEO) {
443 mime = "video/";
444 } else {
Marco Nelissenc367ca12015-09-15 09:51:59 -0700445 ALOGE("unknown track type: %d", trackType);
446 return;
Robert Shih2e3a4252015-05-06 10:21:15 -0700447 }
448 }
Robert Shih755106e2015-04-30 14:36:45 -0700449
Chong Zhang404fced2014-06-11 14:45:31 -0700450 AString lang;
Marco Nelissen9436e482015-09-15 10:21:53 -0700451 if (!format->findString("language", &lang)) {
Marco Nelissenc367ca12015-09-15 09:51:59 -0700452 ALOGE("no language");
453 return;
454 }
Chong Zhang404fced2014-06-11 14:45:31 -0700455
456 reply->writeInt32(2); // write something non-zero
457 reply->writeInt32(trackType);
Robert Shih755106e2015-04-30 14:36:45 -0700458 reply->writeString16(String16(mime.c_str()));
Chong Zhang404fced2014-06-11 14:45:31 -0700459 reply->writeString16(String16(lang.c_str()));
460
461 if (trackType == MEDIA_TRACK_TYPE_SUBTITLE) {
Chong Zhang404fced2014-06-11 14:45:31 -0700462 int32_t isAuto, isDefault, isForced;
463 CHECK(format->findInt32("auto", &isAuto));
464 CHECK(format->findInt32("default", &isDefault));
465 CHECK(format->findInt32("forced", &isForced));
466
Chong Zhang404fced2014-06-11 14:45:31 -0700467 reply->writeInt32(isAuto);
468 reply->writeInt32(isDefault);
469 reply->writeInt32(isForced);
470 }
471}
472
Andreas Huberf9334412010-12-15 15:17:42 -0800473void NuPlayer::onMessageReceived(const sp<AMessage> &msg) {
474 switch (msg->what()) {
475 case kWhatSetDataSource:
476 {
Steve Block3856b092011-10-20 11:56:00 +0100477 ALOGV("kWhatSetDataSource");
Andreas Huberf9334412010-12-15 15:17:42 -0800478
479 CHECK(mSource == NULL);
480
Chong Zhang3de157d2014-08-05 20:54:44 -0700481 status_t err = OK;
Andreas Huber5bc087c2010-12-23 10:27:40 -0800482 sp<RefBase> obj;
483 CHECK(msg->findObject("source", &obj));
Chong Zhang3de157d2014-08-05 20:54:44 -0700484 if (obj != NULL) {
485 mSource = static_cast<Source *>(obj.get());
Chong Zhang3de157d2014-08-05 20:54:44 -0700486 } else {
487 err = UNKNOWN_ERROR;
488 }
Andreas Huber9575c962013-02-05 13:59:56 -0800489
490 CHECK(mDriver != NULL);
491 sp<NuPlayerDriver> driver = mDriver.promote();
492 if (driver != NULL) {
Chong Zhang3de157d2014-08-05 20:54:44 -0700493 driver->notifySetDataSourceCompleted(err);
Andreas Huber9575c962013-02-05 13:59:56 -0800494 }
495 break;
496 }
497
498 case kWhatPrepare:
499 {
500 mSource->prepareAsync();
Andreas Huberf9334412010-12-15 15:17:42 -0800501 break;
502 }
503
Chong Zhangdcb89b32013-08-06 09:44:47 -0700504 case kWhatGetTrackInfo:
505 {
Lajos Molnar3f274362015-03-05 14:35:41 -0800506 sp<AReplyToken> replyID;
Chong Zhangdcb89b32013-08-06 09:44:47 -0700507 CHECK(msg->senderAwaitsResponse(&replyID));
508
Chong Zhang404fced2014-06-11 14:45:31 -0700509 Parcel* reply;
510 CHECK(msg->findPointer("reply", (void**)&reply));
511
512 size_t inbandTracks = 0;
Chong Zhangdcb89b32013-08-06 09:44:47 -0700513 if (mSource != NULL) {
Chong Zhang404fced2014-06-11 14:45:31 -0700514 inbandTracks = mSource->getTrackCount();
515 }
516
Chong Zhanga7fa1d92014-06-11 14:49:23 -0700517 size_t ccTracks = 0;
518 if (mCCDecoder != NULL) {
519 ccTracks = mCCDecoder->getTrackCount();
520 }
521
Chong Zhang404fced2014-06-11 14:45:31 -0700522 // total track count
Chong Zhanga7fa1d92014-06-11 14:49:23 -0700523 reply->writeInt32(inbandTracks + ccTracks);
Chong Zhang404fced2014-06-11 14:45:31 -0700524
525 // write inband tracks
526 for (size_t i = 0; i < inbandTracks; ++i) {
527 writeTrackInfo(reply, mSource->getTrackInfo(i));
Chong Zhangdcb89b32013-08-06 09:44:47 -0700528 }
529
Chong Zhanga7fa1d92014-06-11 14:49:23 -0700530 // write CC track
531 for (size_t i = 0; i < ccTracks; ++i) {
532 writeTrackInfo(reply, mCCDecoder->getTrackInfo(i));
533 }
534
Chong Zhangdcb89b32013-08-06 09:44:47 -0700535 sp<AMessage> response = new AMessage;
Chong Zhangdcb89b32013-08-06 09:44:47 -0700536 response->postReply(replyID);
537 break;
538 }
539
Robert Shih7c4f0d72014-07-09 18:53:31 -0700540 case kWhatGetSelectedTrack:
541 {
542 status_t err = INVALID_OPERATION;
543 if (mSource != NULL) {
544 err = OK;
545
546 int32_t type32;
547 CHECK(msg->findInt32("type", (int32_t*)&type32));
548 media_track_type type = (media_track_type)type32;
549 ssize_t selectedTrack = mSource->getSelectedTrack(type);
550
551 Parcel* reply;
552 CHECK(msg->findPointer("reply", (void**)&reply));
553 reply->writeInt32(selectedTrack);
554 }
555
556 sp<AMessage> response = new AMessage;
557 response->setInt32("err", err);
558
Lajos Molnar3f274362015-03-05 14:35:41 -0800559 sp<AReplyToken> replyID;
Robert Shih7c4f0d72014-07-09 18:53:31 -0700560 CHECK(msg->senderAwaitsResponse(&replyID));
561 response->postReply(replyID);
562 break;
563 }
564
Chong Zhangdcb89b32013-08-06 09:44:47 -0700565 case kWhatSelectTrack:
566 {
Lajos Molnar3f274362015-03-05 14:35:41 -0800567 sp<AReplyToken> replyID;
Chong Zhangdcb89b32013-08-06 09:44:47 -0700568 CHECK(msg->senderAwaitsResponse(&replyID));
569
Chong Zhang404fced2014-06-11 14:45:31 -0700570 size_t trackIndex;
571 int32_t select;
Robert Shih6ffb1fd2014-10-29 16:24:32 -0700572 int64_t timeUs;
Chong Zhang404fced2014-06-11 14:45:31 -0700573 CHECK(msg->findSize("trackIndex", &trackIndex));
574 CHECK(msg->findInt32("select", &select));
Robert Shih6ffb1fd2014-10-29 16:24:32 -0700575 CHECK(msg->findInt64("timeUs", &timeUs));
Chong Zhang404fced2014-06-11 14:45:31 -0700576
Chong Zhangdcb89b32013-08-06 09:44:47 -0700577 status_t err = INVALID_OPERATION;
Chong Zhang404fced2014-06-11 14:45:31 -0700578
579 size_t inbandTracks = 0;
Chong Zhangdcb89b32013-08-06 09:44:47 -0700580 if (mSource != NULL) {
Chong Zhang404fced2014-06-11 14:45:31 -0700581 inbandTracks = mSource->getTrackCount();
582 }
Chong Zhanga7fa1d92014-06-11 14:49:23 -0700583 size_t ccTracks = 0;
584 if (mCCDecoder != NULL) {
585 ccTracks = mCCDecoder->getTrackCount();
586 }
Chong Zhang404fced2014-06-11 14:45:31 -0700587
588 if (trackIndex < inbandTracks) {
Robert Shih6ffb1fd2014-10-29 16:24:32 -0700589 err = mSource->selectTrack(trackIndex, select, timeUs);
Robert Shihd3b0bbb2014-07-23 15:00:25 -0700590
591 if (!select && err == OK) {
592 int32_t type;
593 sp<AMessage> info = mSource->getTrackInfo(trackIndex);
594 if (info != NULL
595 && info->findInt32("type", &type)
596 && type == MEDIA_TRACK_TYPE_TIMEDTEXT) {
597 ++mTimedTextGeneration;
598 }
599 }
Chong Zhanga7fa1d92014-06-11 14:49:23 -0700600 } else {
601 trackIndex -= inbandTracks;
602
603 if (trackIndex < ccTracks) {
604 err = mCCDecoder->selectTrack(trackIndex, select);
605 }
Chong Zhangdcb89b32013-08-06 09:44:47 -0700606 }
607
608 sp<AMessage> response = new AMessage;
609 response->setInt32("err", err);
610
611 response->postReply(replyID);
612 break;
613 }
614
Andreas Huberb7c8e912012-11-27 15:02:53 -0800615 case kWhatPollDuration:
616 {
617 int32_t generation;
618 CHECK(msg->findInt32("generation", &generation));
619
620 if (generation != mPollDurationGeneration) {
621 // stale
622 break;
623 }
624
625 int64_t durationUs;
626 if (mDriver != NULL && mSource->getDuration(&durationUs) == OK) {
627 sp<NuPlayerDriver> driver = mDriver.promote();
628 if (driver != NULL) {
629 driver->notifyDuration(durationUs);
630 }
631 }
632
633 msg->post(1000000ll); // poll again in a second.
634 break;
635 }
636
Lajos Molnar1de1e252015-04-30 18:18:34 -0700637 case kWhatSetVideoSurface:
Andreas Huberf9334412010-12-15 15:17:42 -0800638 {
Andreas Huberf9334412010-12-15 15:17:42 -0800639
640 sp<RefBase> obj;
Lajos Molnar1de1e252015-04-30 18:18:34 -0700641 CHECK(msg->findObject("surface", &obj));
642 sp<Surface> surface = static_cast<Surface *>(obj.get());
Lajos Molnara81c6222015-07-10 19:17:45 -0700643
644 ALOGD("onSetVideoSurface(%p, %s video decoder)",
645 surface.get(),
Wei Jia6b7d2ed2015-08-10 15:11:54 -0700646 (mSource != NULL && mStarted && mSource->getFormat(false /* audio */) != NULL
Lajos Molnara81c6222015-07-10 19:17:45 -0700647 && mVideoDecoder != NULL) ? "have" : "no");
648
Wei Jia6b7d2ed2015-08-10 15:11:54 -0700649 // Need to check mStarted before calling mSource->getFormat because NuPlayer might
650 // be in preparing state and it could take long time.
651 // When mStarted is true, mSource must have been set.
652 if (mSource == NULL || !mStarted || mSource->getFormat(false /* audio */) == NULL
Lajos Molnara81c6222015-07-10 19:17:45 -0700653 // NOTE: mVideoDecoder's mSurface is always non-null
654 || (mVideoDecoder != NULL && mVideoDecoder->setVideoSurface(surface) == OK)) {
Lajos Molnar1de1e252015-04-30 18:18:34 -0700655 performSetSurface(surface);
Wei Jiafef808d2014-10-31 17:57:05 -0700656 break;
657 }
658
659 mDeferredActions.push_back(
660 new FlushDecoderAction(FLUSH_CMD_FLUSH /* audio */,
661 FLUSH_CMD_SHUTDOWN /* video */));
662
Lajos Molnar1de1e252015-04-30 18:18:34 -0700663 mDeferredActions.push_back(new SetSurfaceAction(surface));
James Dong0d268a32012-08-31 12:18:27 -0700664
Wei Jiac6e58412015-07-10 18:04:55 -0700665 if (obj != NULL || mAudioDecoder != NULL) {
Wei Jiafef808d2014-10-31 17:57:05 -0700666 if (mStarted) {
Andy Hung73535852014-09-05 11:42:58 -0700667 // Issue a seek to refresh the video screen only if started otherwise
668 // the extractor may not yet be started and will assert.
669 // If the video decoder is not set (perhaps audio only in this case)
670 // do not perform a seek as it is not needed.
Ronghua Wua73d9e02014-10-08 15:13:29 -0700671 int64_t currentPositionUs = 0;
672 if (getCurrentPosition(&currentPositionUs) == OK) {
673 mDeferredActions.push_back(
Wei Jia29840802015-05-15 17:11:38 -0700674 new SeekAction(currentPositionUs));
Ronghua Wua73d9e02014-10-08 15:13:29 -0700675 }
Andy Hung73535852014-09-05 11:42:58 -0700676 }
Wei Jiaac428aa2014-09-02 19:01:34 -0700677
Andreas Huber57a339c2012-12-03 11:18:00 -0800678 // If there is a new surface texture, instantiate decoders
679 // again if possible.
680 mDeferredActions.push_back(
681 new SimpleAction(&NuPlayer::performScanSources));
682 }
683
Chong Zhangf8d71772014-11-26 15:08:34 -0800684 // After a flush without shutdown, decoder is paused.
685 // Don't resume it until source seek is done, otherwise it could
Chong Zhang7137ec72014-11-12 16:41:05 -0800686 // start pulling stale data too soon.
687 mDeferredActions.push_back(
Chong Zhangf8d71772014-11-26 15:08:34 -0800688 new ResumeDecoderAction(false /* needNotify */));
Chong Zhang7137ec72014-11-12 16:41:05 -0800689
Andreas Huber57a339c2012-12-03 11:18:00 -0800690 processDeferredActions();
Andreas Huberf9334412010-12-15 15:17:42 -0800691 break;
692 }
693
694 case kWhatSetAudioSink:
695 {
Steve Block3856b092011-10-20 11:56:00 +0100696 ALOGV("kWhatSetAudioSink");
Andreas Huberf9334412010-12-15 15:17:42 -0800697
698 sp<RefBase> obj;
699 CHECK(msg->findObject("sink", &obj));
700
701 mAudioSink = static_cast<MediaPlayerBase::AudioSink *>(obj.get());
702 break;
703 }
704
705 case kWhatStart:
706 {
Steve Block3856b092011-10-20 11:56:00 +0100707 ALOGV("kWhatStart");
Wei Jia94211742014-10-28 17:09:06 -0700708 if (mStarted) {
Chong Zhang8a048332015-05-06 15:16:28 -0700709 // do not resume yet if the source is still buffering
710 if (!mPausedForBuffering) {
711 onResume();
712 }
Wei Jia94211742014-10-28 17:09:06 -0700713 } else {
714 onStart();
Lajos Molnar09524832014-07-17 14:29:51 -0700715 }
Chong Zhangefbb6192015-01-30 17:13:27 -0800716 mPausedByClient = false;
Andreas Huberf9334412010-12-15 15:17:42 -0800717 break;
718 }
719
Lajos Molnar3a474aa2015-04-24 17:10:07 -0700720 case kWhatConfigPlayback:
Wei Jia98160162015-02-04 17:01:11 -0800721 {
Lajos Molnar3a474aa2015-04-24 17:10:07 -0700722 sp<AReplyToken> replyID;
723 CHECK(msg->senderAwaitsResponse(&replyID));
724 AudioPlaybackRate rate /* sanitized */;
725 readFromAMessage(msg, &rate);
726 status_t err = OK;
Wei Jia98160162015-02-04 17:01:11 -0800727 if (mRenderer != NULL) {
Lajos Molnar3a474aa2015-04-24 17:10:07 -0700728 err = mRenderer->setPlaybackSettings(rate);
729 }
730 if (err == OK) {
731 if (rate.mSpeed == 0.f) {
732 onPause();
733 // save all other settings (using non-paused speed)
734 // so we can restore them on start
735 AudioPlaybackRate newRate = rate;
736 newRate.mSpeed = mPlaybackSettings.mSpeed;
737 mPlaybackSettings = newRate;
738 } else { /* rate.mSpeed != 0.f */
739 onResume();
740 mPlaybackSettings = rate;
741 }
Wei Jia98160162015-02-04 17:01:11 -0800742 }
Ronghua Wu8db88132015-04-22 13:51:35 -0700743
744 if (mVideoDecoder != NULL) {
Ronghua Wuc8a70d32015-04-29 16:26:34 -0700745 float rate = getFrameRate();
746 if (rate > 0) {
Ronghua Wu8db88132015-04-22 13:51:35 -0700747 sp<AMessage> params = new AMessage();
Lajos Molnar3a474aa2015-04-24 17:10:07 -0700748 params->setFloat("operating-rate", rate * mPlaybackSettings.mSpeed);
Ronghua Wu8db88132015-04-22 13:51:35 -0700749 mVideoDecoder->setParameters(params);
750 }
751 }
752
Lajos Molnar3a474aa2015-04-24 17:10:07 -0700753 sp<AMessage> response = new AMessage;
754 response->setInt32("err", err);
755 response->postReply(replyID);
756 break;
757 }
758
759 case kWhatGetPlaybackSettings:
760 {
761 sp<AReplyToken> replyID;
762 CHECK(msg->senderAwaitsResponse(&replyID));
763 AudioPlaybackRate rate = mPlaybackSettings;
764 status_t err = OK;
765 if (mRenderer != NULL) {
766 err = mRenderer->getPlaybackSettings(&rate);
767 }
768 if (err == OK) {
769 // get playback settings used by renderer, as it may be
770 // slightly off due to audiosink not taking small changes.
771 mPlaybackSettings = rate;
772 if (mPaused) {
773 rate.mSpeed = 0.f;
774 }
775 }
776 sp<AMessage> response = new AMessage;
777 if (err == OK) {
778 writeToAMessage(response, rate);
779 }
780 response->setInt32("err", err);
781 response->postReply(replyID);
782 break;
783 }
784
785 case kWhatConfigSync:
786 {
787 sp<AReplyToken> replyID;
788 CHECK(msg->senderAwaitsResponse(&replyID));
789
790 ALOGV("kWhatConfigSync");
791 AVSyncSettings sync;
792 float videoFpsHint;
793 readFromAMessage(msg, &sync, &videoFpsHint);
794 status_t err = OK;
795 if (mRenderer != NULL) {
796 err = mRenderer->setSyncSettings(sync, videoFpsHint);
797 }
798 if (err == OK) {
799 mSyncSettings = sync;
800 mVideoFpsHint = videoFpsHint;
801 }
802 sp<AMessage> response = new AMessage;
803 response->setInt32("err", err);
804 response->postReply(replyID);
805 break;
806 }
807
808 case kWhatGetSyncSettings:
809 {
810 sp<AReplyToken> replyID;
811 CHECK(msg->senderAwaitsResponse(&replyID));
812 AVSyncSettings sync = mSyncSettings;
813 float videoFps = mVideoFpsHint;
814 status_t err = OK;
815 if (mRenderer != NULL) {
816 err = mRenderer->getSyncSettings(&sync, &videoFps);
817 if (err == OK) {
818 mSyncSettings = sync;
819 mVideoFpsHint = videoFps;
820 }
821 }
822 sp<AMessage> response = new AMessage;
823 if (err == OK) {
824 writeToAMessage(response, sync, videoFps);
825 }
826 response->setInt32("err", err);
827 response->postReply(replyID);
Wei Jia98160162015-02-04 17:01:11 -0800828 break;
829 }
830
Andreas Huberf9334412010-12-15 15:17:42 -0800831 case kWhatScanSources:
832 {
Andreas Huber1aef2112011-01-04 14:01:29 -0800833 int32_t generation;
834 CHECK(msg->findInt32("generation", &generation));
835 if (generation != mScanSourcesGeneration) {
836 // Drop obsolete msg.
837 break;
838 }
839
Andreas Huber5bc087c2010-12-23 10:27:40 -0800840 mScanSourcesPending = false;
841
Steve Block3856b092011-10-20 11:56:00 +0100842 ALOGV("scanning sources haveAudio=%d, haveVideo=%d",
Andreas Huber43c3e6c2011-01-05 12:17:08 -0800843 mAudioDecoder != NULL, mVideoDecoder != NULL);
844
Andreas Huberb7c8e912012-11-27 15:02:53 -0800845 bool mHadAnySourcesBefore =
846 (mAudioDecoder != NULL) || (mVideoDecoder != NULL);
847
Andy Hung282a7e32014-08-14 15:56:34 -0700848 // initialize video before audio because successful initialization of
849 // video may change deep buffer mode of audio.
Lajos Molnar1de1e252015-04-30 18:18:34 -0700850 if (mSurface != NULL) {
Haynes Mathew George5d246ef2012-07-09 10:36:57 -0700851 instantiateDecoder(false, &mVideoDecoder);
852 }
Andreas Huberf9334412010-12-15 15:17:42 -0800853
Ronghua Wua10fd232014-11-06 16:15:20 -0800854 // Don't try to re-open audio sink if there's an existing decoder.
855 if (mAudioSink != NULL && mAudioDecoder == NULL) {
Andreas Huber5bc087c2010-12-23 10:27:40 -0800856 instantiateDecoder(true, &mAudioDecoder);
Andreas Huberf9334412010-12-15 15:17:42 -0800857 }
858
Andreas Huberb7c8e912012-11-27 15:02:53 -0800859 if (!mHadAnySourcesBefore
860 && (mAudioDecoder != NULL || mVideoDecoder != NULL)) {
861 // This is the first time we've found anything playable.
862
Andreas Huber9575c962013-02-05 13:59:56 -0800863 if (mSourceFlags & Source::FLAG_DYNAMIC_DURATION) {
Andreas Huberb7c8e912012-11-27 15:02:53 -0800864 schedulePollDuration();
865 }
866 }
867
Andreas Hubereac68ba2011-09-27 12:12:25 -0700868 status_t err;
869 if ((err = mSource->feedMoreTSData()) != OK) {
Andreas Huber1aef2112011-01-04 14:01:29 -0800870 if (mAudioDecoder == NULL && mVideoDecoder == NULL) {
871 // We're not currently decoding anything (no audio or
872 // video tracks found) and we just ran out of input data.
Andreas Hubereac68ba2011-09-27 12:12:25 -0700873
874 if (err == ERROR_END_OF_STREAM) {
875 notifyListener(MEDIA_PLAYBACK_COMPLETE, 0, 0);
876 } else {
877 notifyListener(MEDIA_ERROR, MEDIA_ERROR_UNKNOWN, err);
878 }
Andreas Huber1aef2112011-01-04 14:01:29 -0800879 }
Andreas Huberf9334412010-12-15 15:17:42 -0800880 break;
881 }
882
Andreas Huberfbe9d812012-08-31 14:05:27 -0700883 if ((mAudioDecoder == NULL && mAudioSink != NULL)
Lajos Molnar1de1e252015-04-30 18:18:34 -0700884 || (mVideoDecoder == NULL && mSurface != NULL)) {
Andreas Huberf9334412010-12-15 15:17:42 -0800885 msg->post(100000ll);
Andreas Huber5bc087c2010-12-23 10:27:40 -0800886 mScanSourcesPending = true;
Andreas Huberf9334412010-12-15 15:17:42 -0800887 }
888 break;
889 }
890
891 case kWhatVideoNotify:
892 case kWhatAudioNotify:
893 {
894 bool audio = msg->what() == kWhatAudioNotify;
895
Wei Jia88703c32014-08-06 11:24:07 -0700896 int32_t currentDecoderGeneration =
897 (audio? mAudioDecoderGeneration : mVideoDecoderGeneration);
898 int32_t requesterGeneration = currentDecoderGeneration - 1;
899 CHECK(msg->findInt32("generation", &requesterGeneration));
900
901 if (requesterGeneration != currentDecoderGeneration) {
902 ALOGV("got message from old %s decoder, generation(%d:%d)",
903 audio ? "audio" : "video", requesterGeneration,
904 currentDecoderGeneration);
905 sp<AMessage> reply;
906 if (!(msg->findMessage("reply", &reply))) {
907 return;
908 }
909
910 reply->setInt32("err", INFO_DISCONTINUITY);
911 reply->post();
912 return;
913 }
914
Andreas Huberf9334412010-12-15 15:17:42 -0800915 int32_t what;
Lajos Molnar1cd13982014-01-17 15:12:51 -0800916 CHECK(msg->findInt32("what", &what));
Andreas Huberf9334412010-12-15 15:17:42 -0800917
Chong Zhang7137ec72014-11-12 16:41:05 -0800918 if (what == DecoderBase::kWhatInputDiscontinuity) {
919 int32_t formatChange;
920 CHECK(msg->findInt32("formatChange", &formatChange));
Andreas Huberf9334412010-12-15 15:17:42 -0800921
Chong Zhang7137ec72014-11-12 16:41:05 -0800922 ALOGV("%s discontinuity: formatChange %d",
923 audio ? "audio" : "video", formatChange);
924
925 if (formatChange) {
926 mDeferredActions.push_back(
927 new FlushDecoderAction(
928 audio ? FLUSH_CMD_SHUTDOWN : FLUSH_CMD_NONE,
929 audio ? FLUSH_CMD_NONE : FLUSH_CMD_SHUTDOWN));
Andreas Huberf9334412010-12-15 15:17:42 -0800930 }
Chong Zhang7137ec72014-11-12 16:41:05 -0800931
932 mDeferredActions.push_back(
933 new SimpleAction(
934 &NuPlayer::performScanSources));
935
936 processDeferredActions();
937 } else if (what == DecoderBase::kWhatEOS) {
Andreas Huberdc9bacd2011-09-26 10:53:29 -0700938 int32_t err;
Lajos Molnar1cd13982014-01-17 15:12:51 -0800939 CHECK(msg->findInt32("err", &err));
Andreas Huberdc9bacd2011-09-26 10:53:29 -0700940
941 if (err == ERROR_END_OF_STREAM) {
Steve Block3856b092011-10-20 11:56:00 +0100942 ALOGV("got %s decoder EOS", audio ? "audio" : "video");
Andreas Huberdc9bacd2011-09-26 10:53:29 -0700943 } else {
Steve Block3856b092011-10-20 11:56:00 +0100944 ALOGV("got %s decoder EOS w/ error %d",
Andreas Huberdc9bacd2011-09-26 10:53:29 -0700945 audio ? "audio" : "video",
946 err);
947 }
948
949 mRenderer->queueEOS(audio, err);
Chong Zhang7137ec72014-11-12 16:41:05 -0800950 } else if (what == DecoderBase::kWhatFlushCompleted) {
Steve Block3856b092011-10-20 11:56:00 +0100951 ALOGV("decoder %s flush completed", audio ? "audio" : "video");
Andreas Huberf9334412010-12-15 15:17:42 -0800952
Andy Hung8d121d42014-10-03 09:53:53 -0700953 handleFlushComplete(audio, true /* isDecoder */);
Andreas Huber3831a062010-12-21 10:22:33 -0800954 finishFlushIfPossible();
Chong Zhang7137ec72014-11-12 16:41:05 -0800955 } else if (what == DecoderBase::kWhatVideoSizeChanged) {
Lajos Molnar1cd13982014-01-17 15:12:51 -0800956 sp<AMessage> format;
957 CHECK(msg->findMessage("format", &format));
958
Wei Jiac6cfd702014-11-11 16:33:20 -0800959 sp<AMessage> inputFormat =
960 mSource->getFormat(false /* audio */);
Andreas Huber3831a062010-12-21 10:22:33 -0800961
Wei Jiac6cfd702014-11-11 16:33:20 -0800962 updateVideoSize(inputFormat, format);
Chong Zhang7137ec72014-11-12 16:41:05 -0800963 } else if (what == DecoderBase::kWhatShutdownCompleted) {
Steve Block3856b092011-10-20 11:56:00 +0100964 ALOGV("%s shutdown completed", audio ? "audio" : "video");
Andreas Huber3831a062010-12-21 10:22:33 -0800965 if (audio) {
966 mAudioDecoder.clear();
Wei Jia57568df2014-09-22 10:16:29 -0700967 ++mAudioDecoderGeneration;
Andreas Huber3831a062010-12-21 10:22:33 -0800968
969 CHECK_EQ((int)mFlushingAudio, (int)SHUTTING_DOWN_DECODER);
970 mFlushingAudio = SHUT_DOWN;
971 } else {
972 mVideoDecoder.clear();
Wei Jia57568df2014-09-22 10:16:29 -0700973 ++mVideoDecoderGeneration;
Andreas Huber3831a062010-12-21 10:22:33 -0800974
975 CHECK_EQ((int)mFlushingVideo, (int)SHUTTING_DOWN_DECODER);
976 mFlushingVideo = SHUT_DOWN;
977 }
978
979 finishFlushIfPossible();
Chong Zhangf8d71772014-11-26 15:08:34 -0800980 } else if (what == DecoderBase::kWhatResumeCompleted) {
981 finishResume();
Chong Zhang7137ec72014-11-12 16:41:05 -0800982 } else if (what == DecoderBase::kWhatError) {
Chong Zhangf4c0a942014-08-11 15:14:10 -0700983 status_t err;
Andy Hung2abde2c2014-09-30 14:40:32 -0700984 if (!msg->findInt32("err", &err) || err == OK) {
Chong Zhangf4c0a942014-08-11 15:14:10 -0700985 err = UNKNOWN_ERROR;
986 }
Andy Hungcf31f1e2014-09-23 14:59:01 -0700987
Andy Hung2abde2c2014-09-30 14:40:32 -0700988 // Decoder errors can be due to Source (e.g. from streaming),
989 // or from decoding corrupted bitstreams, or from other decoder
990 // MediaCodec operations (e.g. from an ongoing reset or seek).
Andy Hung202bce12014-12-03 11:47:36 -0800991 // They may also be due to openAudioSink failure at
992 // decoder start or after a format change.
Andy Hung2abde2c2014-09-30 14:40:32 -0700993 //
994 // We try to gracefully shut down the affected decoder if possible,
995 // rather than trying to force the shutdown with something
996 // similar to performReset(). This method can lead to a hang
997 // if MediaCodec functions block after an error, but they should
998 // typically return INVALID_OPERATION instead of blocking.
999
1000 FlushStatus *flushing = audio ? &mFlushingAudio : &mFlushingVideo;
1001 ALOGE("received error(%#x) from %s decoder, flushing(%d), now shutting down",
1002 err, audio ? "audio" : "video", *flushing);
1003
1004 switch (*flushing) {
1005 case NONE:
1006 mDeferredActions.push_back(
Wei Jiafef808d2014-10-31 17:57:05 -07001007 new FlushDecoderAction(
1008 audio ? FLUSH_CMD_SHUTDOWN : FLUSH_CMD_NONE,
1009 audio ? FLUSH_CMD_NONE : FLUSH_CMD_SHUTDOWN));
Andy Hung2abde2c2014-09-30 14:40:32 -07001010 processDeferredActions();
1011 break;
1012 case FLUSHING_DECODER:
1013 *flushing = FLUSHING_DECODER_SHUTDOWN; // initiate shutdown after flush.
1014 break; // Wait for flush to complete.
1015 case FLUSHING_DECODER_SHUTDOWN:
1016 break; // Wait for flush to complete.
1017 case SHUTTING_DOWN_DECODER:
1018 break; // Wait for shutdown to complete.
1019 case FLUSHED:
1020 // Widevine source reads must stop before releasing the video decoder.
1021 if (!audio && mSource != NULL && mSourceFlags & Source::FLAG_SECURE) {
1022 mSource->stop();
Robert Shih0c61a0d2015-07-06 15:09:10 -07001023 mSourceStarted = false;
Andy Hung2abde2c2014-09-30 14:40:32 -07001024 }
1025 getDecoder(audio)->initiateShutdown(); // In the middle of a seek.
1026 *flushing = SHUTTING_DOWN_DECODER; // Shut down.
1027 break;
1028 case SHUT_DOWN:
1029 finishFlushIfPossible(); // Should not occur.
1030 break; // Finish anyways.
Marco Nelissen9e2b7912014-08-18 16:13:03 -07001031 }
Andy Hung2abde2c2014-09-30 14:40:32 -07001032 notifyListener(MEDIA_ERROR, MEDIA_ERROR_UNKNOWN, err);
Lajos Molnar1cd13982014-01-17 15:12:51 -08001033 } else {
1034 ALOGV("Unhandled decoder notification %d '%c%c%c%c'.",
Andreas Hubera1f8ab02012-11-30 10:53:22 -08001035 what,
1036 what >> 24,
1037 (what >> 16) & 0xff,
1038 (what >> 8) & 0xff,
1039 what & 0xff);
Andreas Huberf9334412010-12-15 15:17:42 -08001040 }
1041
1042 break;
1043 }
1044
1045 case kWhatRendererNotify:
1046 {
Wei Jia57568df2014-09-22 10:16:29 -07001047 int32_t requesterGeneration = mRendererGeneration - 1;
1048 CHECK(msg->findInt32("generation", &requesterGeneration));
1049 if (requesterGeneration != mRendererGeneration) {
1050 ALOGV("got message from old renderer, generation(%d:%d)",
1051 requesterGeneration, mRendererGeneration);
1052 return;
1053 }
1054
Andreas Huberf9334412010-12-15 15:17:42 -08001055 int32_t what;
1056 CHECK(msg->findInt32("what", &what));
1057
1058 if (what == Renderer::kWhatEOS) {
1059 int32_t audio;
1060 CHECK(msg->findInt32("audio", &audio));
1061
Andreas Huberc92fd242011-08-16 13:48:44 -07001062 int32_t finalResult;
1063 CHECK(msg->findInt32("finalResult", &finalResult));
1064
Andreas Huberf9334412010-12-15 15:17:42 -08001065 if (audio) {
1066 mAudioEOS = true;
1067 } else {
1068 mVideoEOS = true;
1069 }
1070
Andreas Huberc92fd242011-08-16 13:48:44 -07001071 if (finalResult == ERROR_END_OF_STREAM) {
Steve Block3856b092011-10-20 11:56:00 +01001072 ALOGV("reached %s EOS", audio ? "audio" : "video");
Andreas Huberc92fd242011-08-16 13:48:44 -07001073 } else {
Steve Block29357bc2012-01-06 19:20:56 +00001074 ALOGE("%s track encountered an error (%d)",
Andreas Huberc92fd242011-08-16 13:48:44 -07001075 audio ? "audio" : "video", finalResult);
1076
1077 notifyListener(
1078 MEDIA_ERROR, MEDIA_ERROR_UNKNOWN, finalResult);
1079 }
Andreas Huberf9334412010-12-15 15:17:42 -08001080
1081 if ((mAudioEOS || mAudioDecoder == NULL)
1082 && (mVideoEOS || mVideoDecoder == NULL)) {
1083 notifyListener(MEDIA_PLAYBACK_COMPLETE, 0, 0);
1084 }
Andreas Huber3fe62152011-09-16 15:09:22 -07001085 } else if (what == Renderer::kWhatFlushComplete) {
Andreas Huberf9334412010-12-15 15:17:42 -08001086 int32_t audio;
1087 CHECK(msg->findInt32("audio", &audio));
1088
Wei Jia3261f0d2015-10-08 13:58:33 -07001089 if (audio) {
1090 mAudioEOS = false;
1091 } else {
1092 mVideoEOS = false;
1093 }
1094
Steve Block3856b092011-10-20 11:56:00 +01001095 ALOGV("renderer %s flush completed.", audio ? "audio" : "video");
Wei Jiada4252f2015-07-14 18:15:28 -07001096 if (audio && (mFlushingAudio == NONE || mFlushingAudio == FLUSHED
1097 || mFlushingAudio == SHUT_DOWN)) {
1098 // Flush has been handled by tear down.
1099 break;
1100 }
Andy Hung8d121d42014-10-03 09:53:53 -07001101 handleFlushComplete(audio, false /* isDecoder */);
1102 finishFlushIfPossible();
James Dongf57b4ea2012-07-20 13:38:36 -07001103 } else if (what == Renderer::kWhatVideoRenderingStart) {
1104 notifyListener(MEDIA_INFO, MEDIA_INFO_RENDERING_START, 0);
Lajos Molnarcbaffcf2013-08-14 18:30:38 -07001105 } else if (what == Renderer::kWhatMediaRenderingStart) {
1106 ALOGV("media rendering started");
1107 notifyListener(MEDIA_STARTED, 0, 0);
Ronghua Wufaeb0f22015-05-21 12:20:21 -07001108 } else if (what == Renderer::kWhatAudioTearDown) {
Ronghua Wu08529172014-10-02 16:55:52 -07001109 int32_t reason;
1110 CHECK(msg->findInt32("reason", &reason));
Ronghua Wufaeb0f22015-05-21 12:20:21 -07001111 ALOGV("Tear down audio with reason %d.", reason);
Wei Jia3a2956d2014-07-22 16:01:33 -07001112 mAudioDecoder.clear();
Wei Jia57568df2014-09-22 10:16:29 -07001113 ++mAudioDecoderGeneration;
Wei Jiada4252f2015-07-14 18:15:28 -07001114 bool needsToCreateAudioDecoder = true;
1115 if (mFlushingAudio == FLUSHING_DECODER) {
1116 mFlushComplete[1 /* audio */][1 /* isDecoder */] = true;
1117 mFlushingAudio = FLUSHED;
1118 finishFlushIfPossible();
1119 } else if (mFlushingAudio == FLUSHING_DECODER_SHUTDOWN
1120 || mFlushingAudio == SHUTTING_DOWN_DECODER) {
1121 mFlushComplete[1 /* audio */][1 /* isDecoder */] = true;
1122 mFlushingAudio = SHUT_DOWN;
1123 finishFlushIfPossible();
1124 needsToCreateAudioDecoder = false;
1125 }
1126 if (mRenderer == NULL) {
1127 break;
1128 }
1129 closeAudioSink();
Chong Zhang7137ec72014-11-12 16:41:05 -08001130 mRenderer->flush(
1131 true /* audio */, false /* notifyComplete */);
Wei Jia3a2956d2014-07-22 16:01:33 -07001132 if (mVideoDecoder != NULL) {
Chong Zhang7137ec72014-11-12 16:41:05 -08001133 mRenderer->flush(
1134 false /* audio */, false /* notifyComplete */);
Wei Jia3a2956d2014-07-22 16:01:33 -07001135 }
Wei Jia3a2956d2014-07-22 16:01:33 -07001136
Wei Jiada4252f2015-07-14 18:15:28 -07001137 int64_t positionUs;
Robert Shih1a5c8592015-08-04 18:07:44 -07001138 if (!msg->findInt64("positionUs", &positionUs)) {
1139 positionUs = mPreviousSeekTimeUs;
1140 }
Wei Jia29840802015-05-15 17:11:38 -07001141 performSeek(positionUs);
Ronghua Wufaeb0f22015-05-21 12:20:21 -07001142
Wei Jiada4252f2015-07-14 18:15:28 -07001143 if (reason == Renderer::kDueToError && needsToCreateAudioDecoder) {
Ronghua Wu08529172014-10-02 16:55:52 -07001144 instantiateDecoder(true /* audio */, &mAudioDecoder);
1145 }
Andreas Huberf9334412010-12-15 15:17:42 -08001146 }
1147 break;
1148 }
1149
1150 case kWhatMoreDataQueued:
1151 {
1152 break;
1153 }
1154
Andreas Huber1aef2112011-01-04 14:01:29 -08001155 case kWhatReset:
1156 {
Steve Block3856b092011-10-20 11:56:00 +01001157 ALOGV("kWhatReset");
Andreas Huber1aef2112011-01-04 14:01:29 -08001158
Andreas Hubera1f8ab02012-11-30 10:53:22 -08001159 mDeferredActions.push_back(
Wei Jiafef808d2014-10-31 17:57:05 -07001160 new FlushDecoderAction(
1161 FLUSH_CMD_SHUTDOWN /* audio */,
1162 FLUSH_CMD_SHUTDOWN /* video */));
Andreas Huberb7c8e912012-11-27 15:02:53 -08001163
Andreas Hubera1f8ab02012-11-30 10:53:22 -08001164 mDeferredActions.push_back(
1165 new SimpleAction(&NuPlayer::performReset));
Andreas Huberb58ce9f2011-11-28 16:27:35 -08001166
Andreas Hubera1f8ab02012-11-30 10:53:22 -08001167 processDeferredActions();
Andreas Huber1aef2112011-01-04 14:01:29 -08001168 break;
1169 }
1170
Andreas Huber43c3e6c2011-01-05 12:17:08 -08001171 case kWhatSeek:
1172 {
1173 int64_t seekTimeUs;
Wei Jiae427abf2014-09-22 15:21:11 -07001174 int32_t needNotify;
Andreas Huber43c3e6c2011-01-05 12:17:08 -08001175 CHECK(msg->findInt64("seekTimeUs", &seekTimeUs));
Wei Jiae427abf2014-09-22 15:21:11 -07001176 CHECK(msg->findInt32("needNotify", &needNotify));
Andreas Huber43c3e6c2011-01-05 12:17:08 -08001177
Wei Jiae427abf2014-09-22 15:21:11 -07001178 ALOGV("kWhatSeek seekTimeUs=%lld us, needNotify=%d",
Lajos Molnar6d339f12015-04-17 16:15:53 -07001179 (long long)seekTimeUs, needNotify);
Andreas Huber43c3e6c2011-01-05 12:17:08 -08001180
Wei Jia1061c9c2015-05-19 16:02:17 -07001181 if (!mStarted) {
1182 // Seek before the player is started. In order to preview video,
1183 // need to start the player and pause it. This branch is called
1184 // only once if needed. After the player is started, any seek
1185 // operation will go through normal path.
Robert Shih0c61a0d2015-07-06 15:09:10 -07001186 // Audio-only cases are handled separately.
Wei Jia1061c9c2015-05-19 16:02:17 -07001187 onStart(seekTimeUs);
Robert Shih0c61a0d2015-07-06 15:09:10 -07001188 if (mStarted) {
1189 onPause();
1190 mPausedByClient = true;
1191 }
Wei Jia1061c9c2015-05-19 16:02:17 -07001192 if (needNotify) {
1193 notifyDriverSeekComplete();
1194 }
1195 break;
1196 }
1197
Andreas Hubera1f8ab02012-11-30 10:53:22 -08001198 mDeferredActions.push_back(
Wei Jiafef808d2014-10-31 17:57:05 -07001199 new FlushDecoderAction(FLUSH_CMD_FLUSH /* audio */,
1200 FLUSH_CMD_FLUSH /* video */));
Andreas Huber43c3e6c2011-01-05 12:17:08 -08001201
Wei Jiae427abf2014-09-22 15:21:11 -07001202 mDeferredActions.push_back(
Wei Jia29840802015-05-15 17:11:38 -07001203 new SeekAction(seekTimeUs));
Andreas Huber43c3e6c2011-01-05 12:17:08 -08001204
Chong Zhangf8d71772014-11-26 15:08:34 -08001205 // After a flush without shutdown, decoder is paused.
1206 // Don't resume it until source seek is done, otherwise it could
Chong Zhang7137ec72014-11-12 16:41:05 -08001207 // start pulling stale data too soon.
1208 mDeferredActions.push_back(
Chong Zhangf8d71772014-11-26 15:08:34 -08001209 new ResumeDecoderAction(needNotify));
Chong Zhang7137ec72014-11-12 16:41:05 -08001210
Andreas Hubera1f8ab02012-11-30 10:53:22 -08001211 processDeferredActions();
Andreas Huber43c3e6c2011-01-05 12:17:08 -08001212 break;
1213 }
1214
Andreas Huberb4082222011-01-20 15:23:04 -08001215 case kWhatPause:
1216 {
Chong Zhangefbb6192015-01-30 17:13:27 -08001217 onPause();
1218 mPausedByClient = true;
Andreas Huberb4082222011-01-20 15:23:04 -08001219 break;
1220 }
1221
Andreas Huberb5f25f02013-02-05 10:14:26 -08001222 case kWhatSourceNotify:
1223 {
Andreas Huber9575c962013-02-05 13:59:56 -08001224 onSourceNotify(msg);
Andreas Huberb5f25f02013-02-05 10:14:26 -08001225 break;
1226 }
1227
Chong Zhanga7fa1d92014-06-11 14:49:23 -07001228 case kWhatClosedCaptionNotify:
1229 {
1230 onClosedCaptionNotify(msg);
1231 break;
1232 }
1233
Andreas Huberf9334412010-12-15 15:17:42 -08001234 default:
1235 TRESPASS();
1236 break;
1237 }
1238}
1239
Wei Jia94211742014-10-28 17:09:06 -07001240void NuPlayer::onResume() {
Chong Zhangefbb6192015-01-30 17:13:27 -08001241 if (!mPaused) {
1242 return;
1243 }
1244 mPaused = false;
Wei Jia94211742014-10-28 17:09:06 -07001245 if (mSource != NULL) {
1246 mSource->resume();
1247 } else {
1248 ALOGW("resume called when source is gone or not set");
1249 }
1250 // |mAudioDecoder| may have been released due to the pause timeout, so re-create it if
1251 // needed.
1252 if (audioDecoderStillNeeded() && mAudioDecoder == NULL) {
1253 instantiateDecoder(true /* audio */, &mAudioDecoder);
1254 }
1255 if (mRenderer != NULL) {
1256 mRenderer->resume();
1257 } else {
1258 ALOGW("resume called when renderer is gone or not set");
1259 }
1260}
1261
Lajos Molnarfcd3e942015-03-31 10:06:48 -07001262status_t NuPlayer::onInstantiateSecureDecoders() {
1263 status_t err;
1264 if (!(mSourceFlags & Source::FLAG_SECURE)) {
1265 return BAD_TYPE;
1266 }
1267
1268 if (mRenderer != NULL) {
1269 ALOGE("renderer should not be set when instantiating secure decoders");
1270 return UNKNOWN_ERROR;
1271 }
1272
1273 // TRICKY: We rely on mRenderer being null, so that decoder does not start requesting
1274 // data on instantiation.
Lajos Molnar1de1e252015-04-30 18:18:34 -07001275 if (mSurface != NULL) {
Lajos Molnarfcd3e942015-03-31 10:06:48 -07001276 err = instantiateDecoder(false, &mVideoDecoder);
1277 if (err != OK) {
1278 return err;
1279 }
1280 }
1281
1282 if (mAudioSink != NULL) {
1283 err = instantiateDecoder(true, &mAudioDecoder);
1284 if (err != OK) {
1285 return err;
1286 }
1287 }
1288 return OK;
1289}
1290
Wei Jia1061c9c2015-05-19 16:02:17 -07001291void NuPlayer::onStart(int64_t startPositionUs) {
Robert Shih0c61a0d2015-07-06 15:09:10 -07001292 if (!mSourceStarted) {
1293 mSourceStarted = true;
1294 mSource->start();
1295 }
1296 if (startPositionUs > 0) {
1297 performSeek(startPositionUs);
1298 if (mSource->getFormat(false /* audio */) == NULL) {
1299 return;
1300 }
1301 }
1302
Wei Jia94211742014-10-28 17:09:06 -07001303 mOffloadAudio = false;
1304 mAudioEOS = false;
1305 mVideoEOS = false;
Wei Jia94211742014-10-28 17:09:06 -07001306 mStarted = true;
1307
Wei Jia94211742014-10-28 17:09:06 -07001308 uint32_t flags = 0;
1309
1310 if (mSource->isRealTime()) {
1311 flags |= Renderer::FLAG_REAL_TIME;
1312 }
1313
1314 sp<MetaData> audioMeta = mSource->getFormatMeta(true /* audio */);
Marco Nelissen9436e482015-09-15 10:21:53 -07001315 if (audioMeta == NULL) {
1316 ALOGE("no metadata for audio source");
1317 return;
1318 }
Wei Jia94211742014-10-28 17:09:06 -07001319 audio_stream_type_t streamType = AUDIO_STREAM_MUSIC;
1320 if (mAudioSink != NULL) {
1321 streamType = mAudioSink->getAudioStreamType();
1322 }
1323
1324 sp<AMessage> videoFormat = mSource->getFormat(false /* audio */);
1325
1326 mOffloadAudio =
Ronghua Wu02cb98d2015-05-27 11:02:54 -07001327 canOffloadStream(audioMeta, (videoFormat != NULL), mSource->isStreaming(), streamType);
Wei Jia94211742014-10-28 17:09:06 -07001328 if (mOffloadAudio) {
1329 flags |= Renderer::FLAG_OFFLOAD_AUDIO;
1330 }
1331
Lajos Molnar1d15ab52015-03-04 16:46:34 -08001332 sp<AMessage> notify = new AMessage(kWhatRendererNotify, this);
Wei Jia94211742014-10-28 17:09:06 -07001333 ++mRendererGeneration;
1334 notify->setInt32("generation", mRendererGeneration);
1335 mRenderer = new Renderer(mAudioSink, notify, flags);
Wei Jia94211742014-10-28 17:09:06 -07001336 mRendererLooper = new ALooper;
1337 mRendererLooper->setName("NuPlayerRenderer");
1338 mRendererLooper->start(false, false, ANDROID_PRIORITY_AUDIO);
1339 mRendererLooper->registerHandler(mRenderer);
Lajos Molnar3a474aa2015-04-24 17:10:07 -07001340
1341 status_t err = mRenderer->setPlaybackSettings(mPlaybackSettings);
1342 if (err != OK) {
1343 mSource->stop();
Robert Shih0c61a0d2015-07-06 15:09:10 -07001344 mSourceStarted = false;
Lajos Molnar3a474aa2015-04-24 17:10:07 -07001345 notifyListener(MEDIA_ERROR, MEDIA_ERROR_UNKNOWN, err);
1346 return;
Wei Jiac8206ff2015-03-04 13:59:37 -08001347 }
Wei Jia94211742014-10-28 17:09:06 -07001348
Ronghua Wuc8a70d32015-04-29 16:26:34 -07001349 float rate = getFrameRate();
1350 if (rate > 0) {
Wei Jia94211742014-10-28 17:09:06 -07001351 mRenderer->setVideoFrameRate(rate);
1352 }
1353
Wei Jiac6cfd702014-11-11 16:33:20 -08001354 if (mVideoDecoder != NULL) {
1355 mVideoDecoder->setRenderer(mRenderer);
1356 }
1357 if (mAudioDecoder != NULL) {
1358 mAudioDecoder->setRenderer(mRenderer);
1359 }
1360
Wei Jia94211742014-10-28 17:09:06 -07001361 postScanSources();
1362}
1363
Chong Zhangefbb6192015-01-30 17:13:27 -08001364void NuPlayer::onPause() {
1365 if (mPaused) {
1366 return;
1367 }
1368 mPaused = true;
1369 if (mSource != NULL) {
1370 mSource->pause();
1371 } else {
1372 ALOGW("pause called when source is gone or not set");
1373 }
1374 if (mRenderer != NULL) {
1375 mRenderer->pause();
1376 } else {
1377 ALOGW("pause called when renderer is gone or not set");
1378 }
1379}
1380
Ronghua Wud7988b12014-10-03 15:19:10 -07001381bool NuPlayer::audioDecoderStillNeeded() {
1382 // Audio decoder is no longer needed if it's in shut/shutting down status.
1383 return ((mFlushingAudio != SHUT_DOWN) && (mFlushingAudio != SHUTTING_DOWN_DECODER));
1384}
1385
Andy Hung8d121d42014-10-03 09:53:53 -07001386void NuPlayer::handleFlushComplete(bool audio, bool isDecoder) {
1387 // We wait for both the decoder flush and the renderer flush to complete
1388 // before entering either the FLUSHED or the SHUTTING_DOWN_DECODER state.
1389
1390 mFlushComplete[audio][isDecoder] = true;
1391 if (!mFlushComplete[audio][!isDecoder]) {
1392 return;
1393 }
1394
1395 FlushStatus *state = audio ? &mFlushingAudio : &mFlushingVideo;
1396 switch (*state) {
1397 case FLUSHING_DECODER:
1398 {
1399 *state = FLUSHED;
Andy Hung8d121d42014-10-03 09:53:53 -07001400 break;
1401 }
1402
1403 case FLUSHING_DECODER_SHUTDOWN:
1404 {
1405 *state = SHUTTING_DOWN_DECODER;
1406
1407 ALOGV("initiating %s decoder shutdown", audio ? "audio" : "video");
1408 if (!audio) {
Andy Hung8d121d42014-10-03 09:53:53 -07001409 // Widevine source reads must stop before releasing the video decoder.
1410 if (mSource != NULL && mSourceFlags & Source::FLAG_SECURE) {
1411 mSource->stop();
Robert Shih0c61a0d2015-07-06 15:09:10 -07001412 mSourceStarted = false;
Andy Hung8d121d42014-10-03 09:53:53 -07001413 }
1414 }
1415 getDecoder(audio)->initiateShutdown();
1416 break;
1417 }
1418
1419 default:
1420 // decoder flush completes only occur in a flushing state.
1421 LOG_ALWAYS_FATAL_IF(isDecoder, "decoder flush in invalid state %d", *state);
1422 break;
1423 }
1424}
1425
Andreas Huber3831a062010-12-21 10:22:33 -08001426void NuPlayer::finishFlushIfPossible() {
Wei Jia53904f32014-07-29 10:22:53 -07001427 if (mFlushingAudio != NONE && mFlushingAudio != FLUSHED
1428 && mFlushingAudio != SHUT_DOWN) {
Andreas Huber3831a062010-12-21 10:22:33 -08001429 return;
1430 }
1431
Wei Jia53904f32014-07-29 10:22:53 -07001432 if (mFlushingVideo != NONE && mFlushingVideo != FLUSHED
1433 && mFlushingVideo != SHUT_DOWN) {
Andreas Huber3831a062010-12-21 10:22:33 -08001434 return;
1435 }
1436
Steve Block3856b092011-10-20 11:56:00 +01001437 ALOGV("both audio and video are flushed now.");
Andreas Huber3831a062010-12-21 10:22:33 -08001438
Andreas Huber3831a062010-12-21 10:22:33 -08001439 mFlushingAudio = NONE;
1440 mFlushingVideo = NONE;
Andreas Huber3831a062010-12-21 10:22:33 -08001441
Andy Hung8d121d42014-10-03 09:53:53 -07001442 clearFlushComplete();
1443
Andreas Hubera1f8ab02012-11-30 10:53:22 -08001444 processDeferredActions();
Andreas Huber1aef2112011-01-04 14:01:29 -08001445}
1446
1447void NuPlayer::postScanSources() {
1448 if (mScanSourcesPending) {
1449 return;
1450 }
1451
Lajos Molnar1d15ab52015-03-04 16:46:34 -08001452 sp<AMessage> msg = new AMessage(kWhatScanSources, this);
Andreas Huber1aef2112011-01-04 14:01:29 -08001453 msg->setInt32("generation", mScanSourcesGeneration);
1454 msg->post();
1455
1456 mScanSourcesPending = true;
1457}
1458
Andy Hung202bce12014-12-03 11:47:36 -08001459void NuPlayer::tryOpenAudioSinkForOffload(const sp<AMessage> &format, bool hasVideo) {
1460 // Note: This is called early in NuPlayer to determine whether offloading
1461 // is possible; otherwise the decoders call the renderer openAudioSink directly.
Andy Hung282a7e32014-08-14 15:56:34 -07001462
Andy Hung202bce12014-12-03 11:47:36 -08001463 status_t err = mRenderer->openAudioSink(
1464 format, true /* offloadOnly */, hasVideo, AUDIO_OUTPUT_FLAG_NONE, &mOffloadAudio);
1465 if (err != OK) {
1466 // Any failure we turn off mOffloadAudio.
1467 mOffloadAudio = false;
1468 } else if (mOffloadAudio) {
Chong Zhang3b9eb1f2014-10-15 17:05:08 -07001469 sp<MetaData> audioMeta =
1470 mSource->getFormatMeta(true /* audio */);
1471 sendMetaDataToHal(mAudioSink, audioMeta);
Andy Hung282a7e32014-08-14 15:56:34 -07001472 }
1473}
1474
1475void NuPlayer::closeAudioSink() {
Chong Zhang3b9eb1f2014-10-15 17:05:08 -07001476 mRenderer->closeAudioSink();
Andy Hung282a7e32014-08-14 15:56:34 -07001477}
1478
Wei Jiae4d18c72015-07-13 17:58:11 -07001479void NuPlayer::determineAudioModeChange() {
1480 if (mSource == NULL || mAudioSink == NULL) {
1481 return;
1482 }
1483
1484 if (mRenderer == NULL) {
1485 ALOGW("No renderer can be used to determine audio mode. Use non-offload for safety.");
1486 mOffloadAudio = false;
1487 return;
1488 }
1489
1490 sp<MetaData> audioMeta = mSource->getFormatMeta(true /* audio */);
1491 sp<AMessage> videoFormat = mSource->getFormat(false /* audio */);
1492 audio_stream_type_t streamType = mAudioSink->getAudioStreamType();
1493 const bool hasVideo = (videoFormat != NULL);
1494 const bool canOffload = canOffloadStream(
1495 audioMeta, hasVideo, mSource->isStreaming(), streamType);
1496 if (canOffload) {
1497 if (!mOffloadAudio) {
1498 mRenderer->signalEnableOffloadAudio();
1499 }
1500 // open audio sink early under offload mode.
1501 sp<AMessage> format = mSource->getFormat(true /*audio*/);
1502 tryOpenAudioSinkForOffload(format, hasVideo);
1503 } else {
Robert Shihe1d70192015-07-23 17:54:13 -07001504 if (mOffloadAudio) {
1505 mRenderer->signalDisableOffloadAudio();
1506 mOffloadAudio = false;
1507 }
Wei Jiae4d18c72015-07-13 17:58:11 -07001508 }
1509}
1510
Chong Zhang7137ec72014-11-12 16:41:05 -08001511status_t NuPlayer::instantiateDecoder(bool audio, sp<DecoderBase> *decoder) {
Wei Jia566da802015-08-27 13:59:30 -07001512 // The audio decoder could be cleared by tear down. If still in shut down
1513 // process, no need to create a new audio decoder.
1514 if (*decoder != NULL || (audio && mFlushingAudio == SHUT_DOWN)) {
Andreas Huberf9334412010-12-15 15:17:42 -08001515 return OK;
1516 }
1517
Andreas Huber84066782011-08-16 09:34:26 -07001518 sp<AMessage> format = mSource->getFormat(audio);
Andreas Huberf9334412010-12-15 15:17:42 -08001519
Andreas Huber84066782011-08-16 09:34:26 -07001520 if (format == NULL) {
Andreas Huberf9334412010-12-15 15:17:42 -08001521 return -EWOULDBLOCK;
1522 }
1523
Ronghua Wu8db88132015-04-22 13:51:35 -07001524 format->setInt32("priority", 0 /* realtime */);
1525
Andreas Huber3fe62152011-09-16 15:09:22 -07001526 if (!audio) {
Andreas Huber84066782011-08-16 09:34:26 -07001527 AString mime;
1528 CHECK(format->findString("mime", &mime));
Chong Zhanga7fa1d92014-06-11 14:49:23 -07001529
Lajos Molnar1d15ab52015-03-04 16:46:34 -08001530 sp<AMessage> ccNotify = new AMessage(kWhatClosedCaptionNotify, this);
Chong Zhang341ab6e2015-02-04 13:37:18 -08001531 if (mCCDecoder == NULL) {
1532 mCCDecoder = new CCDecoder(ccNotify);
1533 }
Lajos Molnar09524832014-07-17 14:29:51 -07001534
1535 if (mSourceFlags & Source::FLAG_SECURE) {
1536 format->setInt32("secure", true);
1537 }
Chong Zhang17134602015-01-07 16:14:34 -08001538
1539 if (mSourceFlags & Source::FLAG_PROTECTED) {
1540 format->setInt32("protected", true);
1541 }
Ronghua Wu8db88132015-04-22 13:51:35 -07001542
Ronghua Wuc8a70d32015-04-29 16:26:34 -07001543 float rate = getFrameRate();
1544 if (rate > 0) {
Lajos Molnar3a474aa2015-04-24 17:10:07 -07001545 format->setFloat("operating-rate", rate * mPlaybackSettings.mSpeed);
Ronghua Wu8db88132015-04-22 13:51:35 -07001546 }
Andreas Huber3fe62152011-09-16 15:09:22 -07001547 }
1548
Wei Jiabc2fb722014-07-08 16:37:57 -07001549 if (audio) {
Lajos Molnar1d15ab52015-03-04 16:46:34 -08001550 sp<AMessage> notify = new AMessage(kWhatAudioNotify, this);
Wei Jia88703c32014-08-06 11:24:07 -07001551 ++mAudioDecoderGeneration;
1552 notify->setInt32("generation", mAudioDecoderGeneration);
1553
Wei Jiae4d18c72015-07-13 17:58:11 -07001554 determineAudioModeChange();
Wei Jiabc2fb722014-07-08 16:37:57 -07001555 if (mOffloadAudio) {
Haynes Mathew George8b635332015-03-30 17:59:47 -07001556 const bool hasVideo = (mSource->getFormat(false /*audio */) != NULL);
1557 format->setInt32("has-video", hasVideo);
Wei Jiac6cfd702014-11-11 16:33:20 -08001558 *decoder = new DecoderPassThrough(notify, mSource, mRenderer);
Wei Jiabc2fb722014-07-08 16:37:57 -07001559 } else {
Ronghua Wu68845c12015-07-21 09:50:48 -07001560 *decoder = new Decoder(notify, mSource, mPID, mRenderer);
Wei Jiabc2fb722014-07-08 16:37:57 -07001561 }
1562 } else {
Lajos Molnar1d15ab52015-03-04 16:46:34 -08001563 sp<AMessage> notify = new AMessage(kWhatVideoNotify, this);
Wei Jia88703c32014-08-06 11:24:07 -07001564 ++mVideoDecoderGeneration;
1565 notify->setInt32("generation", mVideoDecoderGeneration);
1566
Chong Zhang7137ec72014-11-12 16:41:05 -08001567 *decoder = new Decoder(
Ronghua Wu68845c12015-07-21 09:50:48 -07001568 notify, mSource, mPID, mRenderer, mSurface, mCCDecoder);
Lajos Molnard9fd6312014-11-06 11:00:00 -08001569
1570 // enable FRC if high-quality AV sync is requested, even if not
Lajos Molnar1de1e252015-04-30 18:18:34 -07001571 // directly queuing to display, as this will even improve textureview
Lajos Molnard9fd6312014-11-06 11:00:00 -08001572 // playback.
1573 {
1574 char value[PROPERTY_VALUE_MAX];
1575 if (property_get("persist.sys.media.avsync", value, NULL) &&
1576 (!strcmp("1", value) || !strcasecmp("true", value))) {
1577 format->setInt32("auto-frc", 1);
1578 }
1579 }
Wei Jiabc2fb722014-07-08 16:37:57 -07001580 }
Lajos Molnar1cd13982014-01-17 15:12:51 -08001581 (*decoder)->init();
Andreas Huber84066782011-08-16 09:34:26 -07001582 (*decoder)->configure(format);
Andreas Huberf9334412010-12-15 15:17:42 -08001583
Lajos Molnar09524832014-07-17 14:29:51 -07001584 // allocate buffers to decrypt widevine source buffers
1585 if (!audio && (mSourceFlags & Source::FLAG_SECURE)) {
1586 Vector<sp<ABuffer> > inputBufs;
1587 CHECK_EQ((*decoder)->getInputBuffers(&inputBufs), (status_t)OK);
1588
1589 Vector<MediaBuffer *> mediaBufs;
1590 for (size_t i = 0; i < inputBufs.size(); i++) {
1591 const sp<ABuffer> &buffer = inputBufs[i];
1592 MediaBuffer *mbuf = new MediaBuffer(buffer->data(), buffer->size());
1593 mediaBufs.push(mbuf);
1594 }
1595
1596 status_t err = mSource->setBuffers(audio, mediaBufs);
1597 if (err != OK) {
1598 for (size_t i = 0; i < mediaBufs.size(); ++i) {
1599 mediaBufs[i]->release();
1600 }
1601 mediaBufs.clear();
1602 ALOGE("Secure source didn't support secure mediaBufs.");
1603 return err;
1604 }
1605 }
Andreas Huberf9334412010-12-15 15:17:42 -08001606 return OK;
1607}
1608
Chong Zhangced1c2f2014-08-08 15:22:35 -07001609void NuPlayer::updateVideoSize(
1610 const sp<AMessage> &inputFormat,
1611 const sp<AMessage> &outputFormat) {
1612 if (inputFormat == NULL) {
1613 ALOGW("Unknown video size, reporting 0x0!");
1614 notifyListener(MEDIA_SET_VIDEO_SIZE, 0, 0);
1615 return;
1616 }
1617
1618 int32_t displayWidth, displayHeight;
Chong Zhangced1c2f2014-08-08 15:22:35 -07001619 if (outputFormat != NULL) {
1620 int32_t width, height;
1621 CHECK(outputFormat->findInt32("width", &width));
1622 CHECK(outputFormat->findInt32("height", &height));
1623
1624 int32_t cropLeft, cropTop, cropRight, cropBottom;
1625 CHECK(outputFormat->findRect(
1626 "crop",
1627 &cropLeft, &cropTop, &cropRight, &cropBottom));
1628
1629 displayWidth = cropRight - cropLeft + 1;
1630 displayHeight = cropBottom - cropTop + 1;
1631
1632 ALOGV("Video output format changed to %d x %d "
1633 "(crop: %d x %d @ (%d, %d))",
1634 width, height,
1635 displayWidth,
1636 displayHeight,
1637 cropLeft, cropTop);
1638 } else {
1639 CHECK(inputFormat->findInt32("width", &displayWidth));
1640 CHECK(inputFormat->findInt32("height", &displayHeight));
1641
1642 ALOGV("Video input format %d x %d", displayWidth, displayHeight);
1643 }
1644
1645 // Take into account sample aspect ratio if necessary:
1646 int32_t sarWidth, sarHeight;
1647 if (inputFormat->findInt32("sar-width", &sarWidth)
1648 && inputFormat->findInt32("sar-height", &sarHeight)) {
1649 ALOGV("Sample aspect ratio %d : %d", sarWidth, sarHeight);
1650
1651 displayWidth = (displayWidth * sarWidth) / sarHeight;
1652
1653 ALOGV("display dimensions %d x %d", displayWidth, displayHeight);
1654 }
1655
1656 int32_t rotationDegrees;
1657 if (!inputFormat->findInt32("rotation-degrees", &rotationDegrees)) {
1658 rotationDegrees = 0;
1659 }
1660
1661 if (rotationDegrees == 90 || rotationDegrees == 270) {
1662 int32_t tmp = displayWidth;
1663 displayWidth = displayHeight;
1664 displayHeight = tmp;
1665 }
1666
1667 notifyListener(
1668 MEDIA_SET_VIDEO_SIZE,
1669 displayWidth,
1670 displayHeight);
1671}
1672
Chong Zhangdcb89b32013-08-06 09:44:47 -07001673void NuPlayer::notifyListener(int msg, int ext1, int ext2, const Parcel *in) {
Andreas Huber43c3e6c2011-01-05 12:17:08 -08001674 if (mDriver == NULL) {
Andreas Huberf9334412010-12-15 15:17:42 -08001675 return;
1676 }
1677
Andreas Huber43c3e6c2011-01-05 12:17:08 -08001678 sp<NuPlayerDriver> driver = mDriver.promote();
Andreas Huberf9334412010-12-15 15:17:42 -08001679
Andreas Huber43c3e6c2011-01-05 12:17:08 -08001680 if (driver == NULL) {
Andreas Huberf9334412010-12-15 15:17:42 -08001681 return;
1682 }
1683
Chong Zhangdcb89b32013-08-06 09:44:47 -07001684 driver->notifyListener(msg, ext1, ext2, in);
Andreas Huberf9334412010-12-15 15:17:42 -08001685}
1686
Chong Zhang7137ec72014-11-12 16:41:05 -08001687void NuPlayer::flushDecoder(bool audio, bool needShutdown) {
Andreas Huber14f76722013-01-15 09:04:18 -08001688 ALOGV("[%s] flushDecoder needShutdown=%d",
1689 audio ? "audio" : "video", needShutdown);
1690
Chong Zhang7137ec72014-11-12 16:41:05 -08001691 const sp<DecoderBase> &decoder = getDecoder(audio);
Lajos Molnar87603c02014-08-20 19:25:30 -07001692 if (decoder == NULL) {
Steve Blockdf64d152012-01-04 20:05:49 +00001693 ALOGI("flushDecoder %s without decoder present",
Andreas Huber6e3d3112011-11-28 12:36:11 -08001694 audio ? "audio" : "video");
Lajos Molnar87603c02014-08-20 19:25:30 -07001695 return;
Andreas Huber6e3d3112011-11-28 12:36:11 -08001696 }
1697
Andreas Huber1aef2112011-01-04 14:01:29 -08001698 // Make sure we don't continue to scan sources until we finish flushing.
1699 ++mScanSourcesGeneration;
Chong Zhang3b032b32015-04-17 15:49:06 -07001700 if (mScanSourcesPending) {
1701 mDeferredActions.push_back(
1702 new SimpleAction(&NuPlayer::performScanSources));
1703 mScanSourcesPending = false;
1704 }
Andreas Huber1aef2112011-01-04 14:01:29 -08001705
Chong Zhang7137ec72014-11-12 16:41:05 -08001706 decoder->signalFlush();
Andreas Huber1aef2112011-01-04 14:01:29 -08001707
1708 FlushStatus newStatus =
1709 needShutdown ? FLUSHING_DECODER_SHUTDOWN : FLUSHING_DECODER;
1710
Lajos Molnarfcd3e942015-03-31 10:06:48 -07001711 mFlushComplete[audio][false /* isDecoder */] = (mRenderer == NULL);
Andy Hung8d121d42014-10-03 09:53:53 -07001712 mFlushComplete[audio][true /* isDecoder */] = false;
Andreas Huber1aef2112011-01-04 14:01:29 -08001713 if (audio) {
Wei Jia53904f32014-07-29 10:22:53 -07001714 ALOGE_IF(mFlushingAudio != NONE,
1715 "audio flushDecoder() is called in state %d", mFlushingAudio);
Andreas Huber1aef2112011-01-04 14:01:29 -08001716 mFlushingAudio = newStatus;
Andreas Huber1aef2112011-01-04 14:01:29 -08001717 } else {
Wei Jia53904f32014-07-29 10:22:53 -07001718 ALOGE_IF(mFlushingVideo != NONE,
1719 "video flushDecoder() is called in state %d", mFlushingVideo);
Andreas Huber1aef2112011-01-04 14:01:29 -08001720 mFlushingVideo = newStatus;
Andreas Huber1aef2112011-01-04 14:01:29 -08001721 }
1722}
1723
Chong Zhangced1c2f2014-08-08 15:22:35 -07001724void NuPlayer::queueDecoderShutdown(
1725 bool audio, bool video, const sp<AMessage> &reply) {
1726 ALOGI("queueDecoderShutdown audio=%d, video=%d", audio, video);
Andreas Huber84066782011-08-16 09:34:26 -07001727
Chong Zhangced1c2f2014-08-08 15:22:35 -07001728 mDeferredActions.push_back(
Wei Jiafef808d2014-10-31 17:57:05 -07001729 new FlushDecoderAction(
1730 audio ? FLUSH_CMD_SHUTDOWN : FLUSH_CMD_NONE,
1731 video ? FLUSH_CMD_SHUTDOWN : FLUSH_CMD_NONE));
Andreas Huber84066782011-08-16 09:34:26 -07001732
Chong Zhangced1c2f2014-08-08 15:22:35 -07001733 mDeferredActions.push_back(
1734 new SimpleAction(&NuPlayer::performScanSources));
Andreas Huber84066782011-08-16 09:34:26 -07001735
Chong Zhangced1c2f2014-08-08 15:22:35 -07001736 mDeferredActions.push_back(new PostMessageAction(reply));
1737
1738 processDeferredActions();
Andreas Huber84066782011-08-16 09:34:26 -07001739}
1740
James Dong0d268a32012-08-31 12:18:27 -07001741status_t NuPlayer::setVideoScalingMode(int32_t mode) {
1742 mVideoScalingMode = mode;
Lajos Molnar1de1e252015-04-30 18:18:34 -07001743 if (mSurface != NULL) {
1744 status_t ret = native_window_set_scaling_mode(mSurface.get(), mVideoScalingMode);
James Dong0d268a32012-08-31 12:18:27 -07001745 if (ret != OK) {
1746 ALOGE("Failed to set scaling mode (%d): %s",
1747 -ret, strerror(-ret));
1748 return ret;
1749 }
1750 }
1751 return OK;
1752}
1753
Chong Zhangdcb89b32013-08-06 09:44:47 -07001754status_t NuPlayer::getTrackInfo(Parcel* reply) const {
Lajos Molnar1d15ab52015-03-04 16:46:34 -08001755 sp<AMessage> msg = new AMessage(kWhatGetTrackInfo, this);
Chong Zhangdcb89b32013-08-06 09:44:47 -07001756 msg->setPointer("reply", reply);
1757
1758 sp<AMessage> response;
1759 status_t err = msg->postAndAwaitResponse(&response);
1760 return err;
1761}
1762
Robert Shih7c4f0d72014-07-09 18:53:31 -07001763status_t NuPlayer::getSelectedTrack(int32_t type, Parcel* reply) const {
Lajos Molnar1d15ab52015-03-04 16:46:34 -08001764 sp<AMessage> msg = new AMessage(kWhatGetSelectedTrack, this);
Robert Shih7c4f0d72014-07-09 18:53:31 -07001765 msg->setPointer("reply", reply);
1766 msg->setInt32("type", type);
1767
1768 sp<AMessage> response;
1769 status_t err = msg->postAndAwaitResponse(&response);
1770 if (err == OK && response != NULL) {
1771 CHECK(response->findInt32("err", &err));
1772 }
1773 return err;
1774}
1775
Robert Shih6ffb1fd2014-10-29 16:24:32 -07001776status_t NuPlayer::selectTrack(size_t trackIndex, bool select, int64_t timeUs) {
Lajos Molnar1d15ab52015-03-04 16:46:34 -08001777 sp<AMessage> msg = new AMessage(kWhatSelectTrack, this);
Chong Zhangdcb89b32013-08-06 09:44:47 -07001778 msg->setSize("trackIndex", trackIndex);
1779 msg->setInt32("select", select);
Robert Shih6ffb1fd2014-10-29 16:24:32 -07001780 msg->setInt64("timeUs", timeUs);
Chong Zhangdcb89b32013-08-06 09:44:47 -07001781
1782 sp<AMessage> response;
1783 status_t err = msg->postAndAwaitResponse(&response);
1784
Chong Zhang404fced2014-06-11 14:45:31 -07001785 if (err != OK) {
1786 return err;
1787 }
1788
1789 if (!response->findInt32("err", &err)) {
1790 err = OK;
1791 }
1792
Chong Zhangdcb89b32013-08-06 09:44:47 -07001793 return err;
1794}
1795
Ronghua Wua73d9e02014-10-08 15:13:29 -07001796status_t NuPlayer::getCurrentPosition(int64_t *mediaUs) {
1797 sp<Renderer> renderer = mRenderer;
1798 if (renderer == NULL) {
1799 return NO_INIT;
1800 }
1801
1802 return renderer->getCurrentPosition(mediaUs);
1803}
1804
Praveen Chavane1e5d7a2015-05-19 19:09:48 -07001805void NuPlayer::getStats(Vector<sp<AMessage> > *mTrackStats) {
1806 CHECK(mTrackStats != NULL);
1807
1808 mTrackStats->clear();
1809 if (mVideoDecoder != NULL) {
1810 mTrackStats->push_back(mVideoDecoder->getStats());
1811 }
1812 if (mAudioDecoder != NULL) {
1813 mTrackStats->push_back(mAudioDecoder->getStats());
Chong Zhang7137ec72014-11-12 16:41:05 -08001814 }
Ronghua Wua73d9e02014-10-08 15:13:29 -07001815}
1816
Marco Nelissenf0b72b52014-09-16 15:43:44 -07001817sp<MetaData> NuPlayer::getFileMeta() {
1818 return mSource->getFileFormatMeta();
1819}
1820
Ronghua Wuc8a70d32015-04-29 16:26:34 -07001821float NuPlayer::getFrameRate() {
1822 sp<MetaData> meta = mSource->getFormatMeta(false /* audio */);
1823 if (meta == NULL) {
1824 return 0;
1825 }
1826 int32_t rate;
1827 if (!meta->findInt32(kKeyFrameRate, &rate)) {
1828 // fall back to try file meta
1829 sp<MetaData> fileMeta = getFileMeta();
1830 if (fileMeta == NULL) {
1831 ALOGW("source has video meta but not file meta");
1832 return -1;
1833 }
1834 int32_t fileMetaRate;
1835 if (!fileMeta->findInt32(kKeyFrameRate, &fileMetaRate)) {
1836 return -1;
1837 }
1838 return fileMetaRate;
1839 }
1840 return rate;
1841}
1842
Andreas Huberb7c8e912012-11-27 15:02:53 -08001843void NuPlayer::schedulePollDuration() {
Lajos Molnar1d15ab52015-03-04 16:46:34 -08001844 sp<AMessage> msg = new AMessage(kWhatPollDuration, this);
Andreas Huberb7c8e912012-11-27 15:02:53 -08001845 msg->setInt32("generation", mPollDurationGeneration);
1846 msg->post();
1847}
1848
1849void NuPlayer::cancelPollDuration() {
1850 ++mPollDurationGeneration;
1851}
1852
Andreas Hubera1f8ab02012-11-30 10:53:22 -08001853void NuPlayer::processDeferredActions() {
1854 while (!mDeferredActions.empty()) {
1855 // We won't execute any deferred actions until we're no longer in
1856 // an intermediate state, i.e. one more more decoders are currently
1857 // flushing or shutting down.
1858
Andreas Hubera1f8ab02012-11-30 10:53:22 -08001859 if (mFlushingAudio != NONE || mFlushingVideo != NONE) {
1860 // We're currently flushing, postpone the reset until that's
1861 // completed.
1862
1863 ALOGV("postponing action mFlushingAudio=%d, mFlushingVideo=%d",
1864 mFlushingAudio, mFlushingVideo);
1865
1866 break;
1867 }
1868
1869 sp<Action> action = *mDeferredActions.begin();
1870 mDeferredActions.erase(mDeferredActions.begin());
1871
1872 action->execute(this);
1873 }
1874}
1875
Wei Jia29840802015-05-15 17:11:38 -07001876void NuPlayer::performSeek(int64_t seekTimeUs) {
1877 ALOGV("performSeek seekTimeUs=%lld us (%.2f secs)",
Lajos Molnar6d339f12015-04-17 16:15:53 -07001878 (long long)seekTimeUs,
Wei Jia29840802015-05-15 17:11:38 -07001879 seekTimeUs / 1E6);
Andreas Hubera1f8ab02012-11-30 10:53:22 -08001880
Andy Hungadf34bf2014-09-03 18:22:22 -07001881 if (mSource == NULL) {
1882 // This happens when reset occurs right before the loop mode
1883 // asynchronously seeks to the start of the stream.
1884 LOG_ALWAYS_FATAL_IF(mAudioDecoder != NULL || mVideoDecoder != NULL,
1885 "mSource is NULL and decoders not NULL audio(%p) video(%p)",
1886 mAudioDecoder.get(), mVideoDecoder.get());
1887 return;
1888 }
Robert Shih1a5c8592015-08-04 18:07:44 -07001889 mPreviousSeekTimeUs = seekTimeUs;
Andreas Hubera1f8ab02012-11-30 10:53:22 -08001890 mSource->seekTo(seekTimeUs);
Robert Shihd3b0bbb2014-07-23 15:00:25 -07001891 ++mTimedTextGeneration;
Andreas Hubera1f8ab02012-11-30 10:53:22 -08001892
Andreas Hubera1f8ab02012-11-30 10:53:22 -08001893 // everything's flushed, continue playback.
1894}
1895
Wei Jiafef808d2014-10-31 17:57:05 -07001896void NuPlayer::performDecoderFlush(FlushCommand audio, FlushCommand video) {
1897 ALOGV("performDecoderFlush audio=%d, video=%d", audio, video);
Andreas Hubera1f8ab02012-11-30 10:53:22 -08001898
Wei Jiafef808d2014-10-31 17:57:05 -07001899 if ((audio == FLUSH_CMD_NONE || mAudioDecoder == NULL)
1900 && (video == FLUSH_CMD_NONE || mVideoDecoder == NULL)) {
Andreas Hubera1f8ab02012-11-30 10:53:22 -08001901 return;
1902 }
1903
Wei Jiafef808d2014-10-31 17:57:05 -07001904 if (audio != FLUSH_CMD_NONE && mAudioDecoder != NULL) {
1905 flushDecoder(true /* audio */, (audio == FLUSH_CMD_SHUTDOWN));
Andreas Hubera1f8ab02012-11-30 10:53:22 -08001906 }
1907
Wei Jiafef808d2014-10-31 17:57:05 -07001908 if (video != FLUSH_CMD_NONE && mVideoDecoder != NULL) {
1909 flushDecoder(false /* audio */, (video == FLUSH_CMD_SHUTDOWN));
Andreas Hubera1f8ab02012-11-30 10:53:22 -08001910 }
1911}
1912
1913void NuPlayer::performReset() {
1914 ALOGV("performReset");
1915
1916 CHECK(mAudioDecoder == NULL);
1917 CHECK(mVideoDecoder == NULL);
1918
1919 cancelPollDuration();
1920
1921 ++mScanSourcesGeneration;
1922 mScanSourcesPending = false;
1923
Lajos Molnar09524832014-07-17 14:29:51 -07001924 if (mRendererLooper != NULL) {
1925 if (mRenderer != NULL) {
1926 mRendererLooper->unregisterHandler(mRenderer->id());
1927 }
1928 mRendererLooper->stop();
1929 mRendererLooper.clear();
1930 }
Andreas Hubera1f8ab02012-11-30 10:53:22 -08001931 mRenderer.clear();
Wei Jia57568df2014-09-22 10:16:29 -07001932 ++mRendererGeneration;
Andreas Hubera1f8ab02012-11-30 10:53:22 -08001933
1934 if (mSource != NULL) {
1935 mSource->stop();
Andreas Huberb5f25f02013-02-05 10:14:26 -08001936
Andreas Hubera1f8ab02012-11-30 10:53:22 -08001937 mSource.clear();
1938 }
1939
1940 if (mDriver != NULL) {
1941 sp<NuPlayerDriver> driver = mDriver.promote();
1942 if (driver != NULL) {
1943 driver->notifyResetComplete();
1944 }
1945 }
Andreas Huber57a339c2012-12-03 11:18:00 -08001946
1947 mStarted = false;
Robert Shih0c61a0d2015-07-06 15:09:10 -07001948 mSourceStarted = false;
Andreas Hubera1f8ab02012-11-30 10:53:22 -08001949}
1950
1951void NuPlayer::performScanSources() {
1952 ALOGV("performScanSources");
1953
Andreas Huber57a339c2012-12-03 11:18:00 -08001954 if (!mStarted) {
1955 return;
1956 }
1957
Andreas Hubera1f8ab02012-11-30 10:53:22 -08001958 if (mAudioDecoder == NULL || mVideoDecoder == NULL) {
1959 postScanSources();
1960 }
1961}
1962
Lajos Molnar1de1e252015-04-30 18:18:34 -07001963void NuPlayer::performSetSurface(const sp<Surface> &surface) {
Andreas Huber57a339c2012-12-03 11:18:00 -08001964 ALOGV("performSetSurface");
1965
Lajos Molnar1de1e252015-04-30 18:18:34 -07001966 mSurface = surface;
Andreas Huber57a339c2012-12-03 11:18:00 -08001967
1968 // XXX - ignore error from setVideoScalingMode for now
1969 setVideoScalingMode(mVideoScalingMode);
Chong Zhang13d6faa2014-08-22 15:35:28 -07001970
1971 if (mDriver != NULL) {
1972 sp<NuPlayerDriver> driver = mDriver.promote();
1973 if (driver != NULL) {
1974 driver->notifySetSurfaceComplete();
1975 }
1976 }
Andreas Huber57a339c2012-12-03 11:18:00 -08001977}
1978
Chong Zhangf8d71772014-11-26 15:08:34 -08001979void NuPlayer::performResumeDecoders(bool needNotify) {
1980 if (needNotify) {
1981 mResumePending = true;
1982 if (mVideoDecoder == NULL) {
1983 // if audio-only, we can notify seek complete now,
1984 // as the resume operation will be relatively fast.
1985 finishResume();
1986 }
1987 }
1988
Chong Zhang7137ec72014-11-12 16:41:05 -08001989 if (mVideoDecoder != NULL) {
Chong Zhangf8d71772014-11-26 15:08:34 -08001990 // When there is continuous seek, MediaPlayer will cache the seek
1991 // position, and send down new seek request when previous seek is
1992 // complete. Let's wait for at least one video output frame before
1993 // notifying seek complete, so that the video thumbnail gets updated
1994 // when seekbar is dragged.
1995 mVideoDecoder->signalResume(needNotify);
Chong Zhang7137ec72014-11-12 16:41:05 -08001996 }
1997
1998 if (mAudioDecoder != NULL) {
Chong Zhangf8d71772014-11-26 15:08:34 -08001999 mAudioDecoder->signalResume(false /* needNotify */);
2000 }
2001}
2002
2003void NuPlayer::finishResume() {
2004 if (mResumePending) {
2005 mResumePending = false;
Wei Jia1061c9c2015-05-19 16:02:17 -07002006 notifyDriverSeekComplete();
2007 }
2008}
2009
2010void NuPlayer::notifyDriverSeekComplete() {
2011 if (mDriver != NULL) {
2012 sp<NuPlayerDriver> driver = mDriver.promote();
2013 if (driver != NULL) {
2014 driver->notifySeekComplete();
Chong Zhangf8d71772014-11-26 15:08:34 -08002015 }
Chong Zhang7137ec72014-11-12 16:41:05 -08002016 }
2017}
2018
Andreas Huber9575c962013-02-05 13:59:56 -08002019void NuPlayer::onSourceNotify(const sp<AMessage> &msg) {
2020 int32_t what;
2021 CHECK(msg->findInt32("what", &what));
2022
2023 switch (what) {
Lajos Molnarfcd3e942015-03-31 10:06:48 -07002024 case Source::kWhatInstantiateSecureDecoders:
2025 {
2026 if (mSource == NULL) {
2027 // This is a stale notification from a source that was
2028 // asynchronously preparing when the client called reset().
2029 // We handled the reset, the source is gone.
2030 break;
2031 }
2032
2033 sp<AMessage> reply;
2034 CHECK(msg->findMessage("reply", &reply));
2035 status_t err = onInstantiateSecureDecoders();
2036 reply->setInt32("err", err);
2037 reply->post();
2038 break;
2039 }
2040
Andreas Huber9575c962013-02-05 13:59:56 -08002041 case Source::kWhatPrepared:
2042 {
Andreas Huberb5f28d42013-04-25 15:11:19 -07002043 if (mSource == NULL) {
2044 // This is a stale notification from a source that was
2045 // asynchronously preparing when the client called reset().
2046 // We handled the reset, the source is gone.
2047 break;
2048 }
2049
Andreas Huberec0c5972013-02-05 14:47:13 -08002050 int32_t err;
2051 CHECK(msg->findInt32("err", &err));
2052
Lajos Molnarfcd3e942015-03-31 10:06:48 -07002053 if (err != OK) {
2054 // shut down potential secure codecs in case client never calls reset
2055 mDeferredActions.push_back(
2056 new FlushDecoderAction(FLUSH_CMD_SHUTDOWN /* audio */,
2057 FLUSH_CMD_SHUTDOWN /* video */));
2058 processDeferredActions();
2059 }
2060
Andreas Huber9575c962013-02-05 13:59:56 -08002061 sp<NuPlayerDriver> driver = mDriver.promote();
2062 if (driver != NULL) {
Marco Nelissendd114d12014-05-28 15:23:14 -07002063 // notify duration first, so that it's definitely set when
2064 // the app received the "prepare complete" callback.
2065 int64_t durationUs;
2066 if (mSource->getDuration(&durationUs) == OK) {
2067 driver->notifyDuration(durationUs);
2068 }
Andreas Huberec0c5972013-02-05 14:47:13 -08002069 driver->notifyPrepareCompleted(err);
Andreas Huber9575c962013-02-05 13:59:56 -08002070 }
Andreas Huber99759402013-04-01 14:28:31 -07002071
Andreas Huber9575c962013-02-05 13:59:56 -08002072 break;
2073 }
2074
2075 case Source::kWhatFlagsChanged:
2076 {
2077 uint32_t flags;
2078 CHECK(msg->findInt32("flags", (int32_t *)&flags));
2079
Chong Zhang4b7069d2013-09-11 12:52:43 -07002080 sp<NuPlayerDriver> driver = mDriver.promote();
2081 if (driver != NULL) {
Wei Jia895651b2014-12-10 17:31:52 -08002082 if ((flags & NuPlayer::Source::FLAG_CAN_SEEK) == 0) {
2083 driver->notifyListener(
2084 MEDIA_INFO, MEDIA_INFO_NOT_SEEKABLE, 0);
2085 }
Chong Zhang4b7069d2013-09-11 12:52:43 -07002086 driver->notifyFlagsChanged(flags);
2087 }
2088
Andreas Huber9575c962013-02-05 13:59:56 -08002089 if ((mSourceFlags & Source::FLAG_DYNAMIC_DURATION)
2090 && (!(flags & Source::FLAG_DYNAMIC_DURATION))) {
2091 cancelPollDuration();
2092 } else if (!(mSourceFlags & Source::FLAG_DYNAMIC_DURATION)
2093 && (flags & Source::FLAG_DYNAMIC_DURATION)
2094 && (mAudioDecoder != NULL || mVideoDecoder != NULL)) {
2095 schedulePollDuration();
2096 }
2097
2098 mSourceFlags = flags;
2099 break;
2100 }
2101
2102 case Source::kWhatVideoSizeChanged:
2103 {
Chong Zhangced1c2f2014-08-08 15:22:35 -07002104 sp<AMessage> format;
2105 CHECK(msg->findMessage("format", &format));
Andreas Huber9575c962013-02-05 13:59:56 -08002106
Chong Zhangced1c2f2014-08-08 15:22:35 -07002107 updateVideoSize(format);
Andreas Huber9575c962013-02-05 13:59:56 -08002108 break;
2109 }
2110
Chong Zhang2a3cc9a2014-08-21 17:48:26 -07002111 case Source::kWhatBufferingUpdate:
2112 {
2113 int32_t percentage;
2114 CHECK(msg->findInt32("percentage", &percentage));
2115
2116 notifyListener(MEDIA_BUFFERING_UPDATE, percentage, 0);
2117 break;
2118 }
2119
Chong Zhangefbb6192015-01-30 17:13:27 -08002120 case Source::kWhatPauseOnBufferingStart:
2121 {
2122 // ignore if not playing
Chong Zhang8a048332015-05-06 15:16:28 -07002123 if (mStarted) {
Chong Zhangefbb6192015-01-30 17:13:27 -08002124 ALOGI("buffer low, pausing...");
2125
Chong Zhang8a048332015-05-06 15:16:28 -07002126 mPausedForBuffering = true;
Chong Zhangefbb6192015-01-30 17:13:27 -08002127 onPause();
2128 }
2129 // fall-thru
2130 }
2131
Roger Jönssonb50e83e2013-01-21 16:26:41 +01002132 case Source::kWhatBufferingStart:
2133 {
2134 notifyListener(MEDIA_INFO, MEDIA_INFO_BUFFERING_START, 0);
2135 break;
2136 }
2137
Chong Zhangefbb6192015-01-30 17:13:27 -08002138 case Source::kWhatResumeOnBufferingEnd:
2139 {
2140 // ignore if not playing
Chong Zhang8a048332015-05-06 15:16:28 -07002141 if (mStarted) {
Chong Zhangefbb6192015-01-30 17:13:27 -08002142 ALOGI("buffer ready, resuming...");
2143
Chong Zhang8a048332015-05-06 15:16:28 -07002144 mPausedForBuffering = false;
2145
2146 // do not resume yet if client didn't unpause
2147 if (!mPausedByClient) {
2148 onResume();
2149 }
Chong Zhangefbb6192015-01-30 17:13:27 -08002150 }
2151 // fall-thru
2152 }
2153
Roger Jönssonb50e83e2013-01-21 16:26:41 +01002154 case Source::kWhatBufferingEnd:
2155 {
2156 notifyListener(MEDIA_INFO, MEDIA_INFO_BUFFERING_END, 0);
2157 break;
2158 }
2159
Chong Zhangefbb6192015-01-30 17:13:27 -08002160 case Source::kWhatCacheStats:
2161 {
2162 int32_t kbps;
2163 CHECK(msg->findInt32("bandwidth", &kbps));
2164
2165 notifyListener(MEDIA_INFO, MEDIA_INFO_NETWORK_BANDWIDTH, kbps);
2166 break;
2167 }
2168
Chong Zhangdcb89b32013-08-06 09:44:47 -07002169 case Source::kWhatSubtitleData:
2170 {
2171 sp<ABuffer> buffer;
2172 CHECK(msg->findBuffer("buffer", &buffer));
2173
Chong Zhang404fced2014-06-11 14:45:31 -07002174 sendSubtitleData(buffer, 0 /* baseIndex */);
Chong Zhangdcb89b32013-08-06 09:44:47 -07002175 break;
2176 }
2177
Robert Shih08528432015-04-08 09:06:54 -07002178 case Source::kWhatTimedMetaData:
2179 {
2180 sp<ABuffer> buffer;
2181 if (!msg->findBuffer("buffer", &buffer)) {
2182 notifyListener(MEDIA_INFO, MEDIA_INFO_METADATA_UPDATE, 0);
2183 } else {
2184 sendTimedMetaData(buffer);
2185 }
2186 break;
2187 }
2188
Robert Shihd3b0bbb2014-07-23 15:00:25 -07002189 case Source::kWhatTimedTextData:
2190 {
2191 int32_t generation;
2192 if (msg->findInt32("generation", &generation)
2193 && generation != mTimedTextGeneration) {
2194 break;
2195 }
2196
2197 sp<ABuffer> buffer;
2198 CHECK(msg->findBuffer("buffer", &buffer));
2199
2200 sp<NuPlayerDriver> driver = mDriver.promote();
2201 if (driver == NULL) {
2202 break;
2203 }
2204
2205 int posMs;
2206 int64_t timeUs, posUs;
2207 driver->getCurrentPosition(&posMs);
2208 posUs = posMs * 1000;
2209 CHECK(buffer->meta()->findInt64("timeUs", &timeUs));
2210
2211 if (posUs < timeUs) {
2212 if (!msg->findInt32("generation", &generation)) {
2213 msg->setInt32("generation", mTimedTextGeneration);
2214 }
2215 msg->post(timeUs - posUs);
2216 } else {
2217 sendTimedTextData(buffer);
2218 }
2219 break;
2220 }
2221
Andreas Huber14f76722013-01-15 09:04:18 -08002222 case Source::kWhatQueueDecoderShutdown:
2223 {
2224 int32_t audio, video;
2225 CHECK(msg->findInt32("audio", &audio));
2226 CHECK(msg->findInt32("video", &video));
2227
2228 sp<AMessage> reply;
2229 CHECK(msg->findMessage("reply", &reply));
2230
2231 queueDecoderShutdown(audio, video, reply);
2232 break;
2233 }
2234
Ronghua Wu80276872014-08-28 15:50:29 -07002235 case Source::kWhatDrmNoLicense:
2236 {
2237 notifyListener(MEDIA_ERROR, MEDIA_ERROR_UNKNOWN, ERROR_DRM_NO_LICENSE);
2238 break;
2239 }
2240
Andreas Huber9575c962013-02-05 13:59:56 -08002241 default:
2242 TRESPASS();
2243 }
2244}
2245
Chong Zhanga7fa1d92014-06-11 14:49:23 -07002246void NuPlayer::onClosedCaptionNotify(const sp<AMessage> &msg) {
2247 int32_t what;
2248 CHECK(msg->findInt32("what", &what));
2249
2250 switch (what) {
2251 case NuPlayer::CCDecoder::kWhatClosedCaptionData:
2252 {
2253 sp<ABuffer> buffer;
2254 CHECK(msg->findBuffer("buffer", &buffer));
2255
2256 size_t inbandTracks = 0;
2257 if (mSource != NULL) {
2258 inbandTracks = mSource->getTrackCount();
2259 }
2260
2261 sendSubtitleData(buffer, inbandTracks);
2262 break;
2263 }
2264
2265 case NuPlayer::CCDecoder::kWhatTrackAdded:
2266 {
2267 notifyListener(MEDIA_INFO, MEDIA_INFO_METADATA_UPDATE, 0);
2268
2269 break;
2270 }
2271
2272 default:
2273 TRESPASS();
2274 }
2275
2276
2277}
2278
Chong Zhang404fced2014-06-11 14:45:31 -07002279void NuPlayer::sendSubtitleData(const sp<ABuffer> &buffer, int32_t baseIndex) {
2280 int32_t trackIndex;
2281 int64_t timeUs, durationUs;
2282 CHECK(buffer->meta()->findInt32("trackIndex", &trackIndex));
2283 CHECK(buffer->meta()->findInt64("timeUs", &timeUs));
2284 CHECK(buffer->meta()->findInt64("durationUs", &durationUs));
2285
2286 Parcel in;
2287 in.writeInt32(trackIndex + baseIndex);
2288 in.writeInt64(timeUs);
2289 in.writeInt64(durationUs);
2290 in.writeInt32(buffer->size());
2291 in.writeInt32(buffer->size());
2292 in.write(buffer->data(), buffer->size());
2293
2294 notifyListener(MEDIA_SUBTITLE_DATA, 0, 0, &in);
2295}
Robert Shihd3b0bbb2014-07-23 15:00:25 -07002296
Robert Shih08528432015-04-08 09:06:54 -07002297void NuPlayer::sendTimedMetaData(const sp<ABuffer> &buffer) {
2298 int64_t timeUs;
2299 CHECK(buffer->meta()->findInt64("timeUs", &timeUs));
2300
2301 Parcel in;
2302 in.writeInt64(timeUs);
2303 in.writeInt32(buffer->size());
2304 in.writeInt32(buffer->size());
2305 in.write(buffer->data(), buffer->size());
2306
2307 notifyListener(MEDIA_META_DATA, 0, 0, &in);
2308}
2309
Robert Shihd3b0bbb2014-07-23 15:00:25 -07002310void NuPlayer::sendTimedTextData(const sp<ABuffer> &buffer) {
2311 const void *data;
2312 size_t size = 0;
2313 int64_t timeUs;
Marco Nelissen55e2f4c2015-09-04 15:57:15 -07002314 int32_t flag = TextDescriptions::IN_BAND_TEXT_3GPP;
Robert Shihd3b0bbb2014-07-23 15:00:25 -07002315
2316 AString mime;
2317 CHECK(buffer->meta()->findString("mime", &mime));
2318 CHECK(strcasecmp(mime.c_str(), MEDIA_MIMETYPE_TEXT_3GPP) == 0);
2319
2320 data = buffer->data();
2321 size = buffer->size();
2322
2323 Parcel parcel;
2324 if (size > 0) {
2325 CHECK(buffer->meta()->findInt64("timeUs", &timeUs));
Marco Nelissen55e2f4c2015-09-04 15:57:15 -07002326 int32_t global = 0;
2327 if (buffer->meta()->findInt32("global", &global) && global) {
2328 flag |= TextDescriptions::GLOBAL_DESCRIPTIONS;
2329 } else {
2330 flag |= TextDescriptions::LOCAL_DESCRIPTIONS;
2331 }
Robert Shihd3b0bbb2014-07-23 15:00:25 -07002332 TextDescriptions::getParcelOfDescriptions(
2333 (const uint8_t *)data, size, flag, timeUs / 1000, &parcel);
2334 }
2335
2336 if ((parcel.dataSize() > 0)) {
2337 notifyListener(MEDIA_TIMED_TEXT, 0, 0, &parcel);
2338 } else { // send an empty timed text
2339 notifyListener(MEDIA_TIMED_TEXT, 0, 0);
2340 }
2341}
Andreas Huberb5f25f02013-02-05 10:14:26 -08002342////////////////////////////////////////////////////////////////////////////////
2343
Chong Zhangced1c2f2014-08-08 15:22:35 -07002344sp<AMessage> NuPlayer::Source::getFormat(bool audio) {
2345 sp<MetaData> meta = getFormatMeta(audio);
2346
2347 if (meta == NULL) {
2348 return NULL;
2349 }
2350
2351 sp<AMessage> msg = new AMessage;
2352
2353 if(convertMetaDataToMessage(meta, &msg) == OK) {
2354 return msg;
2355 }
2356 return NULL;
2357}
2358
Andreas Huber9575c962013-02-05 13:59:56 -08002359void NuPlayer::Source::notifyFlagsChanged(uint32_t flags) {
2360 sp<AMessage> notify = dupNotify();
2361 notify->setInt32("what", kWhatFlagsChanged);
2362 notify->setInt32("flags", flags);
2363 notify->post();
2364}
2365
Chong Zhangced1c2f2014-08-08 15:22:35 -07002366void NuPlayer::Source::notifyVideoSizeChanged(const sp<AMessage> &format) {
Andreas Huber9575c962013-02-05 13:59:56 -08002367 sp<AMessage> notify = dupNotify();
2368 notify->setInt32("what", kWhatVideoSizeChanged);
Chong Zhangced1c2f2014-08-08 15:22:35 -07002369 notify->setMessage("format", format);
Andreas Huber9575c962013-02-05 13:59:56 -08002370 notify->post();
2371}
2372
Andreas Huberec0c5972013-02-05 14:47:13 -08002373void NuPlayer::Source::notifyPrepared(status_t err) {
Andreas Huber9575c962013-02-05 13:59:56 -08002374 sp<AMessage> notify = dupNotify();
2375 notify->setInt32("what", kWhatPrepared);
Andreas Huberec0c5972013-02-05 14:47:13 -08002376 notify->setInt32("err", err);
Andreas Huber9575c962013-02-05 13:59:56 -08002377 notify->post();
2378}
2379
Lajos Molnarfcd3e942015-03-31 10:06:48 -07002380void NuPlayer::Source::notifyInstantiateSecureDecoders(const sp<AMessage> &reply) {
2381 sp<AMessage> notify = dupNotify();
2382 notify->setInt32("what", kWhatInstantiateSecureDecoders);
2383 notify->setMessage("reply", reply);
2384 notify->post();
2385}
2386
Andreas Huber84333e02014-02-07 15:36:10 -08002387void NuPlayer::Source::onMessageReceived(const sp<AMessage> & /* msg */) {
Andreas Huberb5f25f02013-02-05 10:14:26 -08002388 TRESPASS();
2389}
2390
Andreas Huberf9334412010-12-15 15:17:42 -08002391} // namespace android