blob: eb77af59be39cea8ee7af9fd2bbd4c888ff4ca11 [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),
Wei Jia8a092d32016-06-03 14:57:24 -0700191 mPrepared(false),
Ronghua Wu64c2d172015-10-07 16:52:19 -0700192 mResetting(false),
Robert Shih0c61a0d2015-07-06 15:09:10 -0700193 mSourceStarted(false),
Chong Zhangefbb6192015-01-30 17:13:27 -0800194 mPaused(false),
Wei Jia71c75e02016-02-04 09:40:47 -0800195 mPausedByClient(true),
Chong Zhang8a048332015-05-06 15:16:28 -0700196 mPausedForBuffering(false) {
Andy Hung8d121d42014-10-03 09:53:53 -0700197 clearFlushComplete();
Andreas Huberf9334412010-12-15 15:17:42 -0800198}
199
200NuPlayer::~NuPlayer() {
201}
202
Andreas Huber9b80c2b2011-06-30 15:47:02 -0700203void NuPlayer::setUID(uid_t uid) {
204 mUIDValid = true;
205 mUID = uid;
206}
207
Andreas Huber43c3e6c2011-01-05 12:17:08 -0800208void NuPlayer::setDriver(const wp<NuPlayerDriver> &driver) {
209 mDriver = driver;
Andreas Huberf9334412010-12-15 15:17:42 -0800210}
211
Andreas Huber9575c962013-02-05 13:59:56 -0800212void NuPlayer::setDataSourceAsync(const sp<IStreamSource> &source) {
Lajos Molnar1d15ab52015-03-04 16:46:34 -0800213 sp<AMessage> msg = new AMessage(kWhatSetDataSource, this);
Andreas Huberf9334412010-12-15 15:17:42 -0800214
Lajos Molnar1d15ab52015-03-04 16:46:34 -0800215 sp<AMessage> notify = new AMessage(kWhatSourceNotify, this);
Andreas Huberb5f25f02013-02-05 10:14:26 -0800216
Andreas Huber240abcc2014-02-13 13:32:37 -0800217 msg->setObject("source", new StreamingSource(notify, source));
Andreas Huber5bc087c2010-12-23 10:27:40 -0800218 msg->post();
219}
Andreas Huberf9334412010-12-15 15:17:42 -0800220
Andreas Huberafed0e12011-09-20 15:39:58 -0700221static bool IsHTTPLiveURL(const char *url) {
222 if (!strncasecmp("http://", url, 7)
Andreas Huber99759402013-04-01 14:28:31 -0700223 || !strncasecmp("https://", url, 8)
224 || !strncasecmp("file://", url, 7)) {
Andreas Huberafed0e12011-09-20 15:39:58 -0700225 size_t len = strlen(url);
226 if (len >= 5 && !strcasecmp(".m3u8", &url[len - 5])) {
227 return true;
228 }
229
230 if (strstr(url,"m3u8")) {
231 return true;
232 }
233 }
234
235 return false;
236}
237
Andreas Huber9575c962013-02-05 13:59:56 -0800238void NuPlayer::setDataSourceAsync(
Andreas Huber1b86fe02014-01-29 11:13:26 -0800239 const sp<IMediaHTTPService> &httpService,
240 const char *url,
241 const KeyedVector<String8, String8> *headers) {
Chong Zhang3de157d2014-08-05 20:54:44 -0700242
Lajos Molnar1d15ab52015-03-04 16:46:34 -0800243 sp<AMessage> msg = new AMessage(kWhatSetDataSource, this);
Oscar Rydhé7a33b772012-02-20 10:15:48 +0100244 size_t len = strlen(url);
Andreas Huber5bc087c2010-12-23 10:27:40 -0800245
Lajos Molnar1d15ab52015-03-04 16:46:34 -0800246 sp<AMessage> notify = new AMessage(kWhatSourceNotify, this);
Andreas Huberb5f25f02013-02-05 10:14:26 -0800247
Andreas Huberafed0e12011-09-20 15:39:58 -0700248 sp<Source> source;
249 if (IsHTTPLiveURL(url)) {
Andreas Huber81e68442014-02-05 11:52:33 -0800250 source = new HTTPLiveSource(notify, httpService, url, headers);
Andreas Huberafed0e12011-09-20 15:39:58 -0700251 } else if (!strncasecmp(url, "rtsp://", 7)) {
Andreas Huber1b86fe02014-01-29 11:13:26 -0800252 source = new RTSPSource(
253 notify, httpService, url, headers, mUIDValid, mUID);
Oscar Rydhé7a33b772012-02-20 10:15:48 +0100254 } else if ((!strncasecmp(url, "http://", 7)
255 || !strncasecmp(url, "https://", 8))
256 && ((len >= 4 && !strcasecmp(".sdp", &url[len - 4]))
257 || strstr(url, ".sdp?"))) {
Andreas Huber1b86fe02014-01-29 11:13:26 -0800258 source = new RTSPSource(
259 notify, httpService, url, headers, mUIDValid, mUID, true);
Andreas Huber2bfdd422011-10-11 15:24:07 -0700260 } else {
Chong Zhang3de157d2014-08-05 20:54:44 -0700261 sp<GenericSource> genericSource =
262 new GenericSource(notify, mUIDValid, mUID);
263 // Don't set FLAG_SECURE on mSourceFlags here for widevine.
264 // The correct flags will be updated in Source::kWhatFlagsChanged
265 // handler when GenericSource is prepared.
Andreas Huber2bfdd422011-10-11 15:24:07 -0700266
Chong Zhanga19f33e2014-08-07 15:35:07 -0700267 status_t err = genericSource->setDataSource(httpService, url, headers);
Chong Zhang3de157d2014-08-05 20:54:44 -0700268
269 if (err == OK) {
270 source = genericSource;
271 } else {
Chong Zhanga19f33e2014-08-07 15:35:07 -0700272 ALOGE("Failed to set data source!");
Chong Zhang3de157d2014-08-05 20:54:44 -0700273 }
274 }
Andreas Huberafed0e12011-09-20 15:39:58 -0700275 msg->setObject("source", source);
276 msg->post();
277}
278
Andreas Huber9575c962013-02-05 13:59:56 -0800279void NuPlayer::setDataSourceAsync(int fd, int64_t offset, int64_t length) {
Lajos Molnar1d15ab52015-03-04 16:46:34 -0800280 sp<AMessage> msg = new AMessage(kWhatSetDataSource, this);
Andreas Huberafed0e12011-09-20 15:39:58 -0700281
Lajos Molnar1d15ab52015-03-04 16:46:34 -0800282 sp<AMessage> notify = new AMessage(kWhatSourceNotify, this);
Andreas Huberb5f25f02013-02-05 10:14:26 -0800283
Chong Zhang3de157d2014-08-05 20:54:44 -0700284 sp<GenericSource> source =
285 new GenericSource(notify, mUIDValid, mUID);
286
Chong Zhanga19f33e2014-08-07 15:35:07 -0700287 status_t err = source->setDataSource(fd, offset, length);
Chong Zhang3de157d2014-08-05 20:54:44 -0700288
289 if (err != OK) {
Chong Zhanga19f33e2014-08-07 15:35:07 -0700290 ALOGE("Failed to set data source!");
Chong Zhang3de157d2014-08-05 20:54:44 -0700291 source = NULL;
292 }
293
Andreas Huberafed0e12011-09-20 15:39:58 -0700294 msg->setObject("source", source);
Andreas Huberf9334412010-12-15 15:17:42 -0800295 msg->post();
296}
297
Chris Watkins99f31602015-03-20 13:06:33 -0700298void NuPlayer::setDataSourceAsync(const sp<DataSource> &dataSource) {
299 sp<AMessage> msg = new AMessage(kWhatSetDataSource, this);
300 sp<AMessage> notify = new AMessage(kWhatSourceNotify, this);
301
302 sp<GenericSource> source = new GenericSource(notify, mUIDValid, mUID);
303 status_t err = source->setDataSource(dataSource);
304
305 if (err != OK) {
306 ALOGE("Failed to set data source!");
307 source = NULL;
308 }
309
310 msg->setObject("source", source);
311 msg->post();
312}
313
Andreas Huber9575c962013-02-05 13:59:56 -0800314void NuPlayer::prepareAsync() {
Lajos Molnar1d15ab52015-03-04 16:46:34 -0800315 (new AMessage(kWhatPrepare, this))->post();
Andreas Huber9575c962013-02-05 13:59:56 -0800316}
317
Andreas Huber57a339c2012-12-03 11:18:00 -0800318void NuPlayer::setVideoSurfaceTextureAsync(
Andy McFadden8ba01022012-12-18 09:46:54 -0800319 const sp<IGraphicBufferProducer> &bufferProducer) {
Lajos Molnar1de1e252015-04-30 18:18:34 -0700320 sp<AMessage> msg = new AMessage(kWhatSetVideoSurface, this);
Andreas Huber57a339c2012-12-03 11:18:00 -0800321
Andy McFadden8ba01022012-12-18 09:46:54 -0800322 if (bufferProducer == NULL) {
Lajos Molnar1de1e252015-04-30 18:18:34 -0700323 msg->setObject("surface", NULL);
Andreas Huber57a339c2012-12-03 11:18:00 -0800324 } else {
Lajos Molnar1de1e252015-04-30 18:18:34 -0700325 msg->setObject("surface", new Surface(bufferProducer, true /* controlledByApp */));
Andreas Huber57a339c2012-12-03 11:18:00 -0800326 }
327
Andreas Huberf9334412010-12-15 15:17:42 -0800328 msg->post();
329}
330
331void NuPlayer::setAudioSink(const sp<MediaPlayerBase::AudioSink> &sink) {
Lajos Molnar1d15ab52015-03-04 16:46:34 -0800332 sp<AMessage> msg = new AMessage(kWhatSetAudioSink, this);
Andreas Huberf9334412010-12-15 15:17:42 -0800333 msg->setObject("sink", sink);
334 msg->post();
335}
336
337void NuPlayer::start() {
Lajos Molnar1d15ab52015-03-04 16:46:34 -0800338 (new AMessage(kWhatStart, this))->post();
Andreas Huberf9334412010-12-15 15:17:42 -0800339}
340
Lajos Molnar3a474aa2015-04-24 17:10:07 -0700341status_t NuPlayer::setPlaybackSettings(const AudioPlaybackRate &rate) {
342 // do some cursory validation of the settings here. audio modes are
343 // only validated when set on the audiosink.
344 if ((rate.mSpeed != 0.f && rate.mSpeed < AUDIO_TIMESTRETCH_SPEED_MIN)
345 || rate.mSpeed > AUDIO_TIMESTRETCH_SPEED_MAX
346 || rate.mPitch < AUDIO_TIMESTRETCH_SPEED_MIN
347 || rate.mPitch > AUDIO_TIMESTRETCH_SPEED_MAX) {
348 return BAD_VALUE;
349 }
350 sp<AMessage> msg = new AMessage(kWhatConfigPlayback, this);
351 writeToAMessage(msg, rate);
352 sp<AMessage> response;
353 status_t err = msg->postAndAwaitResponse(&response);
354 if (err == OK && response != NULL) {
355 CHECK(response->findInt32("err", &err));
356 }
357 return err;
358}
359
360status_t NuPlayer::getPlaybackSettings(AudioPlaybackRate *rate /* nonnull */) {
361 sp<AMessage> msg = new AMessage(kWhatGetPlaybackSettings, this);
362 sp<AMessage> response;
363 status_t err = msg->postAndAwaitResponse(&response);
364 if (err == OK && response != NULL) {
365 CHECK(response->findInt32("err", &err));
366 if (err == OK) {
367 readFromAMessage(response, rate);
368 }
369 }
370 return err;
371}
372
373status_t NuPlayer::setSyncSettings(const AVSyncSettings &sync, float videoFpsHint) {
374 sp<AMessage> msg = new AMessage(kWhatConfigSync, this);
375 writeToAMessage(msg, sync, videoFpsHint);
376 sp<AMessage> response;
377 status_t err = msg->postAndAwaitResponse(&response);
378 if (err == OK && response != NULL) {
379 CHECK(response->findInt32("err", &err));
380 }
381 return err;
382}
383
384status_t NuPlayer::getSyncSettings(
385 AVSyncSettings *sync /* nonnull */, float *videoFps /* nonnull */) {
386 sp<AMessage> msg = new AMessage(kWhatGetSyncSettings, this);
387 sp<AMessage> response;
388 status_t err = msg->postAndAwaitResponse(&response);
389 if (err == OK && response != NULL) {
390 CHECK(response->findInt32("err", &err));
391 if (err == OK) {
392 readFromAMessage(response, sync, videoFps);
393 }
394 }
395 return err;
Wei Jia98160162015-02-04 17:01:11 -0800396}
397
Andreas Huber43c3e6c2011-01-05 12:17:08 -0800398void NuPlayer::pause() {
Lajos Molnar1d15ab52015-03-04 16:46:34 -0800399 (new AMessage(kWhatPause, this))->post();
Andreas Huber43c3e6c2011-01-05 12:17:08 -0800400}
401
Andreas Huber1aef2112011-01-04 14:01:29 -0800402void NuPlayer::resetAsync() {
Wei Jiac45a4b22016-04-15 15:30:23 -0700403 sp<Source> source;
404 {
405 Mutex::Autolock autoLock(mSourceLock);
406 source = mSource;
407 }
408
409 if (source != NULL) {
Chong Zhang48296b72014-09-14 14:28:45 -0700410 // During a reset, the data source might be unresponsive already, we need to
411 // disconnect explicitly so that reads exit promptly.
412 // We can't queue the disconnect request to the looper, as it might be
413 // queued behind a stuck read and never gets processed.
414 // Doing a disconnect outside the looper to allows the pending reads to exit
415 // (either successfully or with error).
Wei Jiac45a4b22016-04-15 15:30:23 -0700416 source->disconnect();
Chong Zhang48296b72014-09-14 14:28:45 -0700417 }
418
Lajos Molnar1d15ab52015-03-04 16:46:34 -0800419 (new AMessage(kWhatReset, this))->post();
Andreas Huber1aef2112011-01-04 14:01:29 -0800420}
421
Wei Jiae427abf2014-09-22 15:21:11 -0700422void NuPlayer::seekToAsync(int64_t seekTimeUs, bool needNotify) {
Lajos Molnar1d15ab52015-03-04 16:46:34 -0800423 sp<AMessage> msg = new AMessage(kWhatSeek, this);
Andreas Huber43c3e6c2011-01-05 12:17:08 -0800424 msg->setInt64("seekTimeUs", seekTimeUs);
Wei Jiae427abf2014-09-22 15:21:11 -0700425 msg->setInt32("needNotify", needNotify);
Andreas Huber43c3e6c2011-01-05 12:17:08 -0800426 msg->post();
427}
428
Andreas Huber53df1a42010-12-22 10:03:04 -0800429
Chong Zhang404fced2014-06-11 14:45:31 -0700430void NuPlayer::writeTrackInfo(
431 Parcel* reply, const sp<AMessage> format) const {
Marco Nelissenc367ca12015-09-15 09:51:59 -0700432 if (format == NULL) {
433 ALOGE("NULL format");
434 return;
435 }
Chong Zhang404fced2014-06-11 14:45:31 -0700436 int32_t trackType;
Marco Nelissen9436e482015-09-15 10:21:53 -0700437 if (!format->findInt32("type", &trackType)) {
Marco Nelissenc367ca12015-09-15 09:51:59 -0700438 ALOGE("no track type");
439 return;
440 }
Chong Zhang404fced2014-06-11 14:45:31 -0700441
Robert Shih755106e2015-04-30 14:36:45 -0700442 AString mime;
Robert Shih2e3a4252015-05-06 10:21:15 -0700443 if (!format->findString("mime", &mime)) {
444 // Java MediaPlayer only uses mimetype for subtitle and timedtext tracks.
445 // If we can't find the mimetype here it means that we wouldn't be needing
446 // the mimetype on the Java end. We still write a placeholder mime to keep the
447 // (de)serialization logic simple.
448 if (trackType == MEDIA_TRACK_TYPE_AUDIO) {
449 mime = "audio/";
450 } else if (trackType == MEDIA_TRACK_TYPE_VIDEO) {
451 mime = "video/";
452 } else {
Marco Nelissenc367ca12015-09-15 09:51:59 -0700453 ALOGE("unknown track type: %d", trackType);
454 return;
Robert Shih2e3a4252015-05-06 10:21:15 -0700455 }
456 }
Robert Shih755106e2015-04-30 14:36:45 -0700457
Chong Zhang404fced2014-06-11 14:45:31 -0700458 AString lang;
Marco Nelissen9436e482015-09-15 10:21:53 -0700459 if (!format->findString("language", &lang)) {
Marco Nelissenc367ca12015-09-15 09:51:59 -0700460 ALOGE("no language");
461 return;
462 }
Chong Zhang404fced2014-06-11 14:45:31 -0700463
464 reply->writeInt32(2); // write something non-zero
465 reply->writeInt32(trackType);
Robert Shih755106e2015-04-30 14:36:45 -0700466 reply->writeString16(String16(mime.c_str()));
Chong Zhang404fced2014-06-11 14:45:31 -0700467 reply->writeString16(String16(lang.c_str()));
468
469 if (trackType == MEDIA_TRACK_TYPE_SUBTITLE) {
Chong Zhang404fced2014-06-11 14:45:31 -0700470 int32_t isAuto, isDefault, isForced;
471 CHECK(format->findInt32("auto", &isAuto));
472 CHECK(format->findInt32("default", &isDefault));
473 CHECK(format->findInt32("forced", &isForced));
474
Chong Zhang404fced2014-06-11 14:45:31 -0700475 reply->writeInt32(isAuto);
476 reply->writeInt32(isDefault);
477 reply->writeInt32(isForced);
478 }
479}
480
Andreas Huberf9334412010-12-15 15:17:42 -0800481void NuPlayer::onMessageReceived(const sp<AMessage> &msg) {
482 switch (msg->what()) {
483 case kWhatSetDataSource:
484 {
Steve Block3856b092011-10-20 11:56:00 +0100485 ALOGV("kWhatSetDataSource");
Andreas Huberf9334412010-12-15 15:17:42 -0800486
487 CHECK(mSource == NULL);
488
Chong Zhang3de157d2014-08-05 20:54:44 -0700489 status_t err = OK;
Andreas Huber5bc087c2010-12-23 10:27:40 -0800490 sp<RefBase> obj;
491 CHECK(msg->findObject("source", &obj));
Chong Zhang3de157d2014-08-05 20:54:44 -0700492 if (obj != NULL) {
Wei Jiac45a4b22016-04-15 15:30:23 -0700493 Mutex::Autolock autoLock(mSourceLock);
Chong Zhang3de157d2014-08-05 20:54:44 -0700494 mSource = static_cast<Source *>(obj.get());
Chong Zhang3de157d2014-08-05 20:54:44 -0700495 } else {
496 err = UNKNOWN_ERROR;
497 }
Andreas Huber9575c962013-02-05 13:59:56 -0800498
499 CHECK(mDriver != NULL);
500 sp<NuPlayerDriver> driver = mDriver.promote();
501 if (driver != NULL) {
Chong Zhang3de157d2014-08-05 20:54:44 -0700502 driver->notifySetDataSourceCompleted(err);
Andreas Huber9575c962013-02-05 13:59:56 -0800503 }
504 break;
505 }
506
507 case kWhatPrepare:
508 {
509 mSource->prepareAsync();
Andreas Huberf9334412010-12-15 15:17:42 -0800510 break;
511 }
512
Chong Zhangdcb89b32013-08-06 09:44:47 -0700513 case kWhatGetTrackInfo:
514 {
Lajos Molnar3f274362015-03-05 14:35:41 -0800515 sp<AReplyToken> replyID;
Chong Zhangdcb89b32013-08-06 09:44:47 -0700516 CHECK(msg->senderAwaitsResponse(&replyID));
517
Chong Zhang404fced2014-06-11 14:45:31 -0700518 Parcel* reply;
519 CHECK(msg->findPointer("reply", (void**)&reply));
520
521 size_t inbandTracks = 0;
Chong Zhangdcb89b32013-08-06 09:44:47 -0700522 if (mSource != NULL) {
Chong Zhang404fced2014-06-11 14:45:31 -0700523 inbandTracks = mSource->getTrackCount();
524 }
525
Chong Zhanga7fa1d92014-06-11 14:49:23 -0700526 size_t ccTracks = 0;
527 if (mCCDecoder != NULL) {
528 ccTracks = mCCDecoder->getTrackCount();
529 }
530
Chong Zhang404fced2014-06-11 14:45:31 -0700531 // total track count
Chong Zhanga7fa1d92014-06-11 14:49:23 -0700532 reply->writeInt32(inbandTracks + ccTracks);
Chong Zhang404fced2014-06-11 14:45:31 -0700533
534 // write inband tracks
535 for (size_t i = 0; i < inbandTracks; ++i) {
536 writeTrackInfo(reply, mSource->getTrackInfo(i));
Chong Zhangdcb89b32013-08-06 09:44:47 -0700537 }
538
Chong Zhanga7fa1d92014-06-11 14:49:23 -0700539 // write CC track
540 for (size_t i = 0; i < ccTracks; ++i) {
541 writeTrackInfo(reply, mCCDecoder->getTrackInfo(i));
542 }
543
Chong Zhangdcb89b32013-08-06 09:44:47 -0700544 sp<AMessage> response = new AMessage;
Chong Zhangdcb89b32013-08-06 09:44:47 -0700545 response->postReply(replyID);
546 break;
547 }
548
Robert Shih7c4f0d72014-07-09 18:53:31 -0700549 case kWhatGetSelectedTrack:
550 {
551 status_t err = INVALID_OPERATION;
552 if (mSource != NULL) {
553 err = OK;
554
555 int32_t type32;
556 CHECK(msg->findInt32("type", (int32_t*)&type32));
557 media_track_type type = (media_track_type)type32;
558 ssize_t selectedTrack = mSource->getSelectedTrack(type);
559
560 Parcel* reply;
561 CHECK(msg->findPointer("reply", (void**)&reply));
562 reply->writeInt32(selectedTrack);
563 }
564
565 sp<AMessage> response = new AMessage;
566 response->setInt32("err", err);
567
Lajos Molnar3f274362015-03-05 14:35:41 -0800568 sp<AReplyToken> replyID;
Robert Shih7c4f0d72014-07-09 18:53:31 -0700569 CHECK(msg->senderAwaitsResponse(&replyID));
570 response->postReply(replyID);
571 break;
572 }
573
Chong Zhangdcb89b32013-08-06 09:44:47 -0700574 case kWhatSelectTrack:
575 {
Lajos Molnar3f274362015-03-05 14:35:41 -0800576 sp<AReplyToken> replyID;
Chong Zhangdcb89b32013-08-06 09:44:47 -0700577 CHECK(msg->senderAwaitsResponse(&replyID));
578
Chong Zhang404fced2014-06-11 14:45:31 -0700579 size_t trackIndex;
580 int32_t select;
Robert Shih6ffb1fd2014-10-29 16:24:32 -0700581 int64_t timeUs;
Chong Zhang404fced2014-06-11 14:45:31 -0700582 CHECK(msg->findSize("trackIndex", &trackIndex));
583 CHECK(msg->findInt32("select", &select));
Robert Shih6ffb1fd2014-10-29 16:24:32 -0700584 CHECK(msg->findInt64("timeUs", &timeUs));
Chong Zhang404fced2014-06-11 14:45:31 -0700585
Chong Zhangdcb89b32013-08-06 09:44:47 -0700586 status_t err = INVALID_OPERATION;
Chong Zhang404fced2014-06-11 14:45:31 -0700587
588 size_t inbandTracks = 0;
Chong Zhangdcb89b32013-08-06 09:44:47 -0700589 if (mSource != NULL) {
Chong Zhang404fced2014-06-11 14:45:31 -0700590 inbandTracks = mSource->getTrackCount();
591 }
Chong Zhanga7fa1d92014-06-11 14:49:23 -0700592 size_t ccTracks = 0;
593 if (mCCDecoder != NULL) {
594 ccTracks = mCCDecoder->getTrackCount();
595 }
Chong Zhang404fced2014-06-11 14:45:31 -0700596
597 if (trackIndex < inbandTracks) {
Robert Shih6ffb1fd2014-10-29 16:24:32 -0700598 err = mSource->selectTrack(trackIndex, select, timeUs);
Robert Shihd3b0bbb2014-07-23 15:00:25 -0700599
600 if (!select && err == OK) {
601 int32_t type;
602 sp<AMessage> info = mSource->getTrackInfo(trackIndex);
603 if (info != NULL
604 && info->findInt32("type", &type)
605 && type == MEDIA_TRACK_TYPE_TIMEDTEXT) {
606 ++mTimedTextGeneration;
607 }
608 }
Chong Zhanga7fa1d92014-06-11 14:49:23 -0700609 } else {
610 trackIndex -= inbandTracks;
611
612 if (trackIndex < ccTracks) {
613 err = mCCDecoder->selectTrack(trackIndex, select);
614 }
Chong Zhangdcb89b32013-08-06 09:44:47 -0700615 }
616
617 sp<AMessage> response = new AMessage;
618 response->setInt32("err", err);
619
620 response->postReply(replyID);
621 break;
622 }
623
Andreas Huberb7c8e912012-11-27 15:02:53 -0800624 case kWhatPollDuration:
625 {
626 int32_t generation;
627 CHECK(msg->findInt32("generation", &generation));
628
629 if (generation != mPollDurationGeneration) {
630 // stale
631 break;
632 }
633
634 int64_t durationUs;
635 if (mDriver != NULL && mSource->getDuration(&durationUs) == OK) {
636 sp<NuPlayerDriver> driver = mDriver.promote();
637 if (driver != NULL) {
638 driver->notifyDuration(durationUs);
639 }
640 }
641
642 msg->post(1000000ll); // poll again in a second.
643 break;
644 }
645
Lajos Molnar1de1e252015-04-30 18:18:34 -0700646 case kWhatSetVideoSurface:
Andreas Huberf9334412010-12-15 15:17:42 -0800647 {
Andreas Huberf9334412010-12-15 15:17:42 -0800648
649 sp<RefBase> obj;
Lajos Molnar1de1e252015-04-30 18:18:34 -0700650 CHECK(msg->findObject("surface", &obj));
651 sp<Surface> surface = static_cast<Surface *>(obj.get());
Lajos Molnara81c6222015-07-10 19:17:45 -0700652
653 ALOGD("onSetVideoSurface(%p, %s video decoder)",
654 surface.get(),
Wei Jia6b7d2ed2015-08-10 15:11:54 -0700655 (mSource != NULL && mStarted && mSource->getFormat(false /* audio */) != NULL
Lajos Molnara81c6222015-07-10 19:17:45 -0700656 && mVideoDecoder != NULL) ? "have" : "no");
657
Wei Jia6b7d2ed2015-08-10 15:11:54 -0700658 // Need to check mStarted before calling mSource->getFormat because NuPlayer might
659 // be in preparing state and it could take long time.
660 // When mStarted is true, mSource must have been set.
661 if (mSource == NULL || !mStarted || mSource->getFormat(false /* audio */) == NULL
Lajos Molnara81c6222015-07-10 19:17:45 -0700662 // NOTE: mVideoDecoder's mSurface is always non-null
663 || (mVideoDecoder != NULL && mVideoDecoder->setVideoSurface(surface) == OK)) {
Lajos Molnar1de1e252015-04-30 18:18:34 -0700664 performSetSurface(surface);
Wei Jiafef808d2014-10-31 17:57:05 -0700665 break;
666 }
667
668 mDeferredActions.push_back(
669 new FlushDecoderAction(FLUSH_CMD_FLUSH /* audio */,
670 FLUSH_CMD_SHUTDOWN /* video */));
671
Lajos Molnar1de1e252015-04-30 18:18:34 -0700672 mDeferredActions.push_back(new SetSurfaceAction(surface));
James Dong0d268a32012-08-31 12:18:27 -0700673
Wei Jiac6e58412015-07-10 18:04:55 -0700674 if (obj != NULL || mAudioDecoder != NULL) {
Wei Jiafef808d2014-10-31 17:57:05 -0700675 if (mStarted) {
Andy Hung73535852014-09-05 11:42:58 -0700676 // Issue a seek to refresh the video screen only if started otherwise
677 // the extractor may not yet be started and will assert.
678 // If the video decoder is not set (perhaps audio only in this case)
679 // do not perform a seek as it is not needed.
Ronghua Wua73d9e02014-10-08 15:13:29 -0700680 int64_t currentPositionUs = 0;
681 if (getCurrentPosition(&currentPositionUs) == OK) {
682 mDeferredActions.push_back(
Wei Jia29840802015-05-15 17:11:38 -0700683 new SeekAction(currentPositionUs));
Ronghua Wua73d9e02014-10-08 15:13:29 -0700684 }
Andy Hung73535852014-09-05 11:42:58 -0700685 }
Wei Jiaac428aa2014-09-02 19:01:34 -0700686
Andreas Huber57a339c2012-12-03 11:18:00 -0800687 // If there is a new surface texture, instantiate decoders
688 // again if possible.
689 mDeferredActions.push_back(
690 new SimpleAction(&NuPlayer::performScanSources));
691 }
692
Chong Zhangf8d71772014-11-26 15:08:34 -0800693 // After a flush without shutdown, decoder is paused.
694 // Don't resume it until source seek is done, otherwise it could
Chong Zhang7137ec72014-11-12 16:41:05 -0800695 // start pulling stale data too soon.
696 mDeferredActions.push_back(
Chong Zhangf8d71772014-11-26 15:08:34 -0800697 new ResumeDecoderAction(false /* needNotify */));
Chong Zhang7137ec72014-11-12 16:41:05 -0800698
Andreas Huber57a339c2012-12-03 11:18:00 -0800699 processDeferredActions();
Andreas Huberf9334412010-12-15 15:17:42 -0800700 break;
701 }
702
703 case kWhatSetAudioSink:
704 {
Steve Block3856b092011-10-20 11:56:00 +0100705 ALOGV("kWhatSetAudioSink");
Andreas Huberf9334412010-12-15 15:17:42 -0800706
707 sp<RefBase> obj;
708 CHECK(msg->findObject("sink", &obj));
709
710 mAudioSink = static_cast<MediaPlayerBase::AudioSink *>(obj.get());
711 break;
712 }
713
714 case kWhatStart:
715 {
Steve Block3856b092011-10-20 11:56:00 +0100716 ALOGV("kWhatStart");
Wei Jia94211742014-10-28 17:09:06 -0700717 if (mStarted) {
Chong Zhang8a048332015-05-06 15:16:28 -0700718 // do not resume yet if the source is still buffering
719 if (!mPausedForBuffering) {
720 onResume();
721 }
Wei Jia94211742014-10-28 17:09:06 -0700722 } else {
723 onStart();
Lajos Molnar09524832014-07-17 14:29:51 -0700724 }
Chong Zhangefbb6192015-01-30 17:13:27 -0800725 mPausedByClient = false;
Andreas Huberf9334412010-12-15 15:17:42 -0800726 break;
727 }
728
Lajos Molnar3a474aa2015-04-24 17:10:07 -0700729 case kWhatConfigPlayback:
Wei Jia98160162015-02-04 17:01:11 -0800730 {
Lajos Molnar3a474aa2015-04-24 17:10:07 -0700731 sp<AReplyToken> replyID;
732 CHECK(msg->senderAwaitsResponse(&replyID));
733 AudioPlaybackRate rate /* sanitized */;
734 readFromAMessage(msg, &rate);
735 status_t err = OK;
Wei Jia98160162015-02-04 17:01:11 -0800736 if (mRenderer != NULL) {
Wei Jiabf70feb2016-02-19 15:47:16 -0800737 // AudioSink allows only 1.f and 0.f for offload mode.
738 // For other speed, switch to non-offload mode.
739 if (mOffloadAudio && ((rate.mSpeed != 0.f && rate.mSpeed != 1.f)
740 || rate.mPitch != 1.f)) {
741 int64_t currentPositionUs;
742 if (getCurrentPosition(&currentPositionUs) != OK) {
743 currentPositionUs = mPreviousSeekTimeUs;
744 }
745
746 // Set mPlaybackSettings so that the new audio decoder can
747 // be created correctly.
748 mPlaybackSettings = rate;
749 if (!mPaused) {
750 mRenderer->pause();
751 }
Wei Jia5031b2f2016-02-25 11:19:31 -0800752 restartAudio(
Wei Jiabf70feb2016-02-19 15:47:16 -0800753 currentPositionUs, true /* forceNonOffload */,
754 true /* needsToCreateAudioDecoder */);
755 if (!mPaused) {
756 mRenderer->resume();
757 }
758 }
759
Lajos Molnar3a474aa2015-04-24 17:10:07 -0700760 err = mRenderer->setPlaybackSettings(rate);
761 }
762 if (err == OK) {
763 if (rate.mSpeed == 0.f) {
764 onPause();
Wei Jia351ce872016-02-10 13:21:59 -0800765 mPausedByClient = true;
Lajos Molnar3a474aa2015-04-24 17:10:07 -0700766 // save all other settings (using non-paused speed)
767 // so we can restore them on start
768 AudioPlaybackRate newRate = rate;
769 newRate.mSpeed = mPlaybackSettings.mSpeed;
770 mPlaybackSettings = newRate;
771 } else { /* rate.mSpeed != 0.f */
Lajos Molnar3a474aa2015-04-24 17:10:07 -0700772 mPlaybackSettings = rate;
Wei Jia8a092d32016-06-03 14:57:24 -0700773 if (mStarted) {
774 // do not resume yet if the source is still buffering
775 if (!mPausedForBuffering) {
776 onResume();
777 }
778 } else if (mPrepared) {
779 onStart();
780 }
781
782 mPausedByClient = false;
Lajos Molnar3a474aa2015-04-24 17:10:07 -0700783 }
Wei Jia98160162015-02-04 17:01:11 -0800784 }
Ronghua Wu8db88132015-04-22 13:51:35 -0700785
786 if (mVideoDecoder != NULL) {
Ronghua Wuc8a70d32015-04-29 16:26:34 -0700787 float rate = getFrameRate();
788 if (rate > 0) {
Ronghua Wu8db88132015-04-22 13:51:35 -0700789 sp<AMessage> params = new AMessage();
Lajos Molnar3a474aa2015-04-24 17:10:07 -0700790 params->setFloat("operating-rate", rate * mPlaybackSettings.mSpeed);
Ronghua Wu8db88132015-04-22 13:51:35 -0700791 mVideoDecoder->setParameters(params);
792 }
793 }
794
Lajos Molnar3a474aa2015-04-24 17:10:07 -0700795 sp<AMessage> response = new AMessage;
796 response->setInt32("err", err);
797 response->postReply(replyID);
798 break;
799 }
800
801 case kWhatGetPlaybackSettings:
802 {
803 sp<AReplyToken> replyID;
804 CHECK(msg->senderAwaitsResponse(&replyID));
805 AudioPlaybackRate rate = mPlaybackSettings;
806 status_t err = OK;
807 if (mRenderer != NULL) {
808 err = mRenderer->getPlaybackSettings(&rate);
809 }
810 if (err == OK) {
811 // get playback settings used by renderer, as it may be
812 // slightly off due to audiosink not taking small changes.
813 mPlaybackSettings = rate;
814 if (mPaused) {
815 rate.mSpeed = 0.f;
816 }
817 }
818 sp<AMessage> response = new AMessage;
819 if (err == OK) {
820 writeToAMessage(response, rate);
821 }
822 response->setInt32("err", err);
823 response->postReply(replyID);
824 break;
825 }
826
827 case kWhatConfigSync:
828 {
829 sp<AReplyToken> replyID;
830 CHECK(msg->senderAwaitsResponse(&replyID));
831
832 ALOGV("kWhatConfigSync");
833 AVSyncSettings sync;
834 float videoFpsHint;
835 readFromAMessage(msg, &sync, &videoFpsHint);
836 status_t err = OK;
837 if (mRenderer != NULL) {
838 err = mRenderer->setSyncSettings(sync, videoFpsHint);
839 }
840 if (err == OK) {
841 mSyncSettings = sync;
842 mVideoFpsHint = videoFpsHint;
843 }
844 sp<AMessage> response = new AMessage;
845 response->setInt32("err", err);
846 response->postReply(replyID);
847 break;
848 }
849
850 case kWhatGetSyncSettings:
851 {
852 sp<AReplyToken> replyID;
853 CHECK(msg->senderAwaitsResponse(&replyID));
854 AVSyncSettings sync = mSyncSettings;
855 float videoFps = mVideoFpsHint;
856 status_t err = OK;
857 if (mRenderer != NULL) {
858 err = mRenderer->getSyncSettings(&sync, &videoFps);
859 if (err == OK) {
860 mSyncSettings = sync;
861 mVideoFpsHint = videoFps;
862 }
863 }
864 sp<AMessage> response = new AMessage;
865 if (err == OK) {
866 writeToAMessage(response, sync, videoFps);
867 }
868 response->setInt32("err", err);
869 response->postReply(replyID);
Wei Jia98160162015-02-04 17:01:11 -0800870 break;
871 }
872
Andreas Huberf9334412010-12-15 15:17:42 -0800873 case kWhatScanSources:
874 {
Andreas Huber1aef2112011-01-04 14:01:29 -0800875 int32_t generation;
876 CHECK(msg->findInt32("generation", &generation));
877 if (generation != mScanSourcesGeneration) {
878 // Drop obsolete msg.
879 break;
880 }
881
Andreas Huber5bc087c2010-12-23 10:27:40 -0800882 mScanSourcesPending = false;
883
Steve Block3856b092011-10-20 11:56:00 +0100884 ALOGV("scanning sources haveAudio=%d, haveVideo=%d",
Andreas Huber43c3e6c2011-01-05 12:17:08 -0800885 mAudioDecoder != NULL, mVideoDecoder != NULL);
886
Andreas Huberb7c8e912012-11-27 15:02:53 -0800887 bool mHadAnySourcesBefore =
888 (mAudioDecoder != NULL) || (mVideoDecoder != NULL);
Robert Shih7350b052015-10-01 15:50:14 -0700889 bool rescan = false;
Andreas Huberb7c8e912012-11-27 15:02:53 -0800890
Andy Hung282a7e32014-08-14 15:56:34 -0700891 // initialize video before audio because successful initialization of
892 // video may change deep buffer mode of audio.
Lajos Molnar1de1e252015-04-30 18:18:34 -0700893 if (mSurface != NULL) {
Robert Shih7350b052015-10-01 15:50:14 -0700894 if (instantiateDecoder(false, &mVideoDecoder) == -EWOULDBLOCK) {
895 rescan = true;
896 }
Haynes Mathew George5d246ef2012-07-09 10:36:57 -0700897 }
Andreas Huberf9334412010-12-15 15:17:42 -0800898
Ronghua Wua10fd232014-11-06 16:15:20 -0800899 // Don't try to re-open audio sink if there's an existing decoder.
900 if (mAudioSink != NULL && mAudioDecoder == NULL) {
Robert Shih7350b052015-10-01 15:50:14 -0700901 if (instantiateDecoder(true, &mAudioDecoder) == -EWOULDBLOCK) {
902 rescan = true;
903 }
Andreas Huberf9334412010-12-15 15:17:42 -0800904 }
905
Andreas Huberb7c8e912012-11-27 15:02:53 -0800906 if (!mHadAnySourcesBefore
907 && (mAudioDecoder != NULL || mVideoDecoder != NULL)) {
908 // This is the first time we've found anything playable.
909
Andreas Huber9575c962013-02-05 13:59:56 -0800910 if (mSourceFlags & Source::FLAG_DYNAMIC_DURATION) {
Andreas Huberb7c8e912012-11-27 15:02:53 -0800911 schedulePollDuration();
912 }
913 }
914
Andreas Hubereac68ba2011-09-27 12:12:25 -0700915 status_t err;
916 if ((err = mSource->feedMoreTSData()) != OK) {
Andreas Huber1aef2112011-01-04 14:01:29 -0800917 if (mAudioDecoder == NULL && mVideoDecoder == NULL) {
918 // We're not currently decoding anything (no audio or
919 // video tracks found) and we just ran out of input data.
Andreas Hubereac68ba2011-09-27 12:12:25 -0700920
921 if (err == ERROR_END_OF_STREAM) {
922 notifyListener(MEDIA_PLAYBACK_COMPLETE, 0, 0);
923 } else {
924 notifyListener(MEDIA_ERROR, MEDIA_ERROR_UNKNOWN, err);
925 }
Andreas Huber1aef2112011-01-04 14:01:29 -0800926 }
Andreas Huberf9334412010-12-15 15:17:42 -0800927 break;
928 }
929
Robert Shih7350b052015-10-01 15:50:14 -0700930 if (rescan) {
Andreas Huberf9334412010-12-15 15:17:42 -0800931 msg->post(100000ll);
Andreas Huber5bc087c2010-12-23 10:27:40 -0800932 mScanSourcesPending = true;
Andreas Huberf9334412010-12-15 15:17:42 -0800933 }
934 break;
935 }
936
937 case kWhatVideoNotify:
938 case kWhatAudioNotify:
939 {
940 bool audio = msg->what() == kWhatAudioNotify;
941
Wei Jia88703c32014-08-06 11:24:07 -0700942 int32_t currentDecoderGeneration =
943 (audio? mAudioDecoderGeneration : mVideoDecoderGeneration);
944 int32_t requesterGeneration = currentDecoderGeneration - 1;
945 CHECK(msg->findInt32("generation", &requesterGeneration));
946
947 if (requesterGeneration != currentDecoderGeneration) {
948 ALOGV("got message from old %s decoder, generation(%d:%d)",
949 audio ? "audio" : "video", requesterGeneration,
950 currentDecoderGeneration);
951 sp<AMessage> reply;
952 if (!(msg->findMessage("reply", &reply))) {
953 return;
954 }
955
956 reply->setInt32("err", INFO_DISCONTINUITY);
957 reply->post();
958 return;
959 }
960
Andreas Huberf9334412010-12-15 15:17:42 -0800961 int32_t what;
Lajos Molnar1cd13982014-01-17 15:12:51 -0800962 CHECK(msg->findInt32("what", &what));
Andreas Huberf9334412010-12-15 15:17:42 -0800963
Chong Zhang7137ec72014-11-12 16:41:05 -0800964 if (what == DecoderBase::kWhatInputDiscontinuity) {
965 int32_t formatChange;
966 CHECK(msg->findInt32("formatChange", &formatChange));
Andreas Huberf9334412010-12-15 15:17:42 -0800967
Chong Zhang7137ec72014-11-12 16:41:05 -0800968 ALOGV("%s discontinuity: formatChange %d",
969 audio ? "audio" : "video", formatChange);
970
971 if (formatChange) {
972 mDeferredActions.push_back(
973 new FlushDecoderAction(
974 audio ? FLUSH_CMD_SHUTDOWN : FLUSH_CMD_NONE,
975 audio ? FLUSH_CMD_NONE : FLUSH_CMD_SHUTDOWN));
Andreas Huberf9334412010-12-15 15:17:42 -0800976 }
Chong Zhang7137ec72014-11-12 16:41:05 -0800977
978 mDeferredActions.push_back(
979 new SimpleAction(
980 &NuPlayer::performScanSources));
981
982 processDeferredActions();
983 } else if (what == DecoderBase::kWhatEOS) {
Andreas Huberdc9bacd2011-09-26 10:53:29 -0700984 int32_t err;
Lajos Molnar1cd13982014-01-17 15:12:51 -0800985 CHECK(msg->findInt32("err", &err));
Andreas Huberdc9bacd2011-09-26 10:53:29 -0700986
987 if (err == ERROR_END_OF_STREAM) {
Steve Block3856b092011-10-20 11:56:00 +0100988 ALOGV("got %s decoder EOS", audio ? "audio" : "video");
Andreas Huberdc9bacd2011-09-26 10:53:29 -0700989 } else {
Steve Block3856b092011-10-20 11:56:00 +0100990 ALOGV("got %s decoder EOS w/ error %d",
Andreas Huberdc9bacd2011-09-26 10:53:29 -0700991 audio ? "audio" : "video",
992 err);
993 }
994
995 mRenderer->queueEOS(audio, err);
Chong Zhang7137ec72014-11-12 16:41:05 -0800996 } else if (what == DecoderBase::kWhatFlushCompleted) {
Steve Block3856b092011-10-20 11:56:00 +0100997 ALOGV("decoder %s flush completed", audio ? "audio" : "video");
Andreas Huberf9334412010-12-15 15:17:42 -0800998
Andy Hung8d121d42014-10-03 09:53:53 -0700999 handleFlushComplete(audio, true /* isDecoder */);
Andreas Huber3831a062010-12-21 10:22:33 -08001000 finishFlushIfPossible();
Chong Zhang7137ec72014-11-12 16:41:05 -08001001 } else if (what == DecoderBase::kWhatVideoSizeChanged) {
Lajos Molnar1cd13982014-01-17 15:12:51 -08001002 sp<AMessage> format;
1003 CHECK(msg->findMessage("format", &format));
1004
Wei Jiac6cfd702014-11-11 16:33:20 -08001005 sp<AMessage> inputFormat =
1006 mSource->getFormat(false /* audio */);
Andreas Huber3831a062010-12-21 10:22:33 -08001007
Wei Jiac6cfd702014-11-11 16:33:20 -08001008 updateVideoSize(inputFormat, format);
Chong Zhang7137ec72014-11-12 16:41:05 -08001009 } else if (what == DecoderBase::kWhatShutdownCompleted) {
Steve Block3856b092011-10-20 11:56:00 +01001010 ALOGV("%s shutdown completed", audio ? "audio" : "video");
Andreas Huber3831a062010-12-21 10:22:33 -08001011 if (audio) {
1012 mAudioDecoder.clear();
Wei Jia57568df2014-09-22 10:16:29 -07001013 ++mAudioDecoderGeneration;
Andreas Huber3831a062010-12-21 10:22:33 -08001014
1015 CHECK_EQ((int)mFlushingAudio, (int)SHUTTING_DOWN_DECODER);
1016 mFlushingAudio = SHUT_DOWN;
1017 } else {
1018 mVideoDecoder.clear();
Wei Jia57568df2014-09-22 10:16:29 -07001019 ++mVideoDecoderGeneration;
Andreas Huber3831a062010-12-21 10:22:33 -08001020
1021 CHECK_EQ((int)mFlushingVideo, (int)SHUTTING_DOWN_DECODER);
1022 mFlushingVideo = SHUT_DOWN;
1023 }
1024
1025 finishFlushIfPossible();
Chong Zhangf8d71772014-11-26 15:08:34 -08001026 } else if (what == DecoderBase::kWhatResumeCompleted) {
1027 finishResume();
Chong Zhang7137ec72014-11-12 16:41:05 -08001028 } else if (what == DecoderBase::kWhatError) {
Chong Zhangf4c0a942014-08-11 15:14:10 -07001029 status_t err;
Andy Hung2abde2c2014-09-30 14:40:32 -07001030 if (!msg->findInt32("err", &err) || err == OK) {
Chong Zhangf4c0a942014-08-11 15:14:10 -07001031 err = UNKNOWN_ERROR;
1032 }
Andy Hungcf31f1e2014-09-23 14:59:01 -07001033
Andy Hung2abde2c2014-09-30 14:40:32 -07001034 // Decoder errors can be due to Source (e.g. from streaming),
1035 // or from decoding corrupted bitstreams, or from other decoder
1036 // MediaCodec operations (e.g. from an ongoing reset or seek).
Andy Hung202bce12014-12-03 11:47:36 -08001037 // They may also be due to openAudioSink failure at
1038 // decoder start or after a format change.
Andy Hung2abde2c2014-09-30 14:40:32 -07001039 //
1040 // We try to gracefully shut down the affected decoder if possible,
1041 // rather than trying to force the shutdown with something
1042 // similar to performReset(). This method can lead to a hang
1043 // if MediaCodec functions block after an error, but they should
1044 // typically return INVALID_OPERATION instead of blocking.
1045
1046 FlushStatus *flushing = audio ? &mFlushingAudio : &mFlushingVideo;
1047 ALOGE("received error(%#x) from %s decoder, flushing(%d), now shutting down",
1048 err, audio ? "audio" : "video", *flushing);
1049
1050 switch (*flushing) {
1051 case NONE:
1052 mDeferredActions.push_back(
Wei Jiafef808d2014-10-31 17:57:05 -07001053 new FlushDecoderAction(
1054 audio ? FLUSH_CMD_SHUTDOWN : FLUSH_CMD_NONE,
1055 audio ? FLUSH_CMD_NONE : FLUSH_CMD_SHUTDOWN));
Andy Hung2abde2c2014-09-30 14:40:32 -07001056 processDeferredActions();
1057 break;
1058 case FLUSHING_DECODER:
1059 *flushing = FLUSHING_DECODER_SHUTDOWN; // initiate shutdown after flush.
1060 break; // Wait for flush to complete.
1061 case FLUSHING_DECODER_SHUTDOWN:
1062 break; // Wait for flush to complete.
1063 case SHUTTING_DOWN_DECODER:
1064 break; // Wait for shutdown to complete.
1065 case FLUSHED:
1066 // Widevine source reads must stop before releasing the video decoder.
1067 if (!audio && mSource != NULL && mSourceFlags & Source::FLAG_SECURE) {
1068 mSource->stop();
Robert Shih0c61a0d2015-07-06 15:09:10 -07001069 mSourceStarted = false;
Andy Hung2abde2c2014-09-30 14:40:32 -07001070 }
1071 getDecoder(audio)->initiateShutdown(); // In the middle of a seek.
1072 *flushing = SHUTTING_DOWN_DECODER; // Shut down.
1073 break;
1074 case SHUT_DOWN:
1075 finishFlushIfPossible(); // Should not occur.
1076 break; // Finish anyways.
Marco Nelissen9e2b7912014-08-18 16:13:03 -07001077 }
Andy Hung2abde2c2014-09-30 14:40:32 -07001078 notifyListener(MEDIA_ERROR, MEDIA_ERROR_UNKNOWN, err);
Lajos Molnar1cd13982014-01-17 15:12:51 -08001079 } else {
1080 ALOGV("Unhandled decoder notification %d '%c%c%c%c'.",
Andreas Hubera1f8ab02012-11-30 10:53:22 -08001081 what,
1082 what >> 24,
1083 (what >> 16) & 0xff,
1084 (what >> 8) & 0xff,
1085 what & 0xff);
Andreas Huberf9334412010-12-15 15:17:42 -08001086 }
1087
1088 break;
1089 }
1090
1091 case kWhatRendererNotify:
1092 {
Wei Jia57568df2014-09-22 10:16:29 -07001093 int32_t requesterGeneration = mRendererGeneration - 1;
1094 CHECK(msg->findInt32("generation", &requesterGeneration));
1095 if (requesterGeneration != mRendererGeneration) {
1096 ALOGV("got message from old renderer, generation(%d:%d)",
1097 requesterGeneration, mRendererGeneration);
1098 return;
1099 }
1100
Andreas Huberf9334412010-12-15 15:17:42 -08001101 int32_t what;
1102 CHECK(msg->findInt32("what", &what));
1103
1104 if (what == Renderer::kWhatEOS) {
1105 int32_t audio;
1106 CHECK(msg->findInt32("audio", &audio));
1107
Andreas Huberc92fd242011-08-16 13:48:44 -07001108 int32_t finalResult;
1109 CHECK(msg->findInt32("finalResult", &finalResult));
1110
Andreas Huberf9334412010-12-15 15:17:42 -08001111 if (audio) {
1112 mAudioEOS = true;
1113 } else {
1114 mVideoEOS = true;
1115 }
1116
Andreas Huberc92fd242011-08-16 13:48:44 -07001117 if (finalResult == ERROR_END_OF_STREAM) {
Steve Block3856b092011-10-20 11:56:00 +01001118 ALOGV("reached %s EOS", audio ? "audio" : "video");
Andreas Huberc92fd242011-08-16 13:48:44 -07001119 } else {
Steve Block29357bc2012-01-06 19:20:56 +00001120 ALOGE("%s track encountered an error (%d)",
Andreas Huberc92fd242011-08-16 13:48:44 -07001121 audio ? "audio" : "video", finalResult);
1122
1123 notifyListener(
1124 MEDIA_ERROR, MEDIA_ERROR_UNKNOWN, finalResult);
1125 }
Andreas Huberf9334412010-12-15 15:17:42 -08001126
1127 if ((mAudioEOS || mAudioDecoder == NULL)
1128 && (mVideoEOS || mVideoDecoder == NULL)) {
1129 notifyListener(MEDIA_PLAYBACK_COMPLETE, 0, 0);
1130 }
Andreas Huber3fe62152011-09-16 15:09:22 -07001131 } else if (what == Renderer::kWhatFlushComplete) {
Andreas Huberf9334412010-12-15 15:17:42 -08001132 int32_t audio;
1133 CHECK(msg->findInt32("audio", &audio));
1134
Wei Jia3261f0d2015-10-08 13:58:33 -07001135 if (audio) {
1136 mAudioEOS = false;
1137 } else {
1138 mVideoEOS = false;
1139 }
1140
Steve Block3856b092011-10-20 11:56:00 +01001141 ALOGV("renderer %s flush completed.", audio ? "audio" : "video");
Wei Jiada4252f2015-07-14 18:15:28 -07001142 if (audio && (mFlushingAudio == NONE || mFlushingAudio == FLUSHED
1143 || mFlushingAudio == SHUT_DOWN)) {
1144 // Flush has been handled by tear down.
1145 break;
1146 }
Andy Hung8d121d42014-10-03 09:53:53 -07001147 handleFlushComplete(audio, false /* isDecoder */);
1148 finishFlushIfPossible();
James Dongf57b4ea2012-07-20 13:38:36 -07001149 } else if (what == Renderer::kWhatVideoRenderingStart) {
1150 notifyListener(MEDIA_INFO, MEDIA_INFO_RENDERING_START, 0);
Lajos Molnarcbaffcf2013-08-14 18:30:38 -07001151 } else if (what == Renderer::kWhatMediaRenderingStart) {
1152 ALOGV("media rendering started");
1153 notifyListener(MEDIA_STARTED, 0, 0);
Ronghua Wufaeb0f22015-05-21 12:20:21 -07001154 } else if (what == Renderer::kWhatAudioTearDown) {
Ronghua Wu08529172014-10-02 16:55:52 -07001155 int32_t reason;
1156 CHECK(msg->findInt32("reason", &reason));
Ronghua Wufaeb0f22015-05-21 12:20:21 -07001157 ALOGV("Tear down audio with reason %d.", reason);
Wei Jia8456ddd2016-04-22 14:46:43 -07001158 if (reason == Renderer::kDueToTimeout && !(mPaused && mOffloadAudio)) {
1159 // TimeoutWhenPaused is only for offload mode.
1160 ALOGW("Receive a stale message for teardown.");
1161 break;
1162 }
Wei Jiada4252f2015-07-14 18:15:28 -07001163 int64_t positionUs;
Robert Shih1a5c8592015-08-04 18:07:44 -07001164 if (!msg->findInt64("positionUs", &positionUs)) {
1165 positionUs = mPreviousSeekTimeUs;
1166 }
Ronghua Wufaeb0f22015-05-21 12:20:21 -07001167
Wei Jia5031b2f2016-02-25 11:19:31 -08001168 restartAudio(
Wei Jiaa05f1e32016-03-25 16:31:22 -07001169 positionUs, reason == Renderer::kForceNonOffload /* forceNonOffload */,
1170 reason != Renderer::kDueToTimeout /* needsToCreateAudioDecoder */);
Andreas Huberf9334412010-12-15 15:17:42 -08001171 }
1172 break;
1173 }
1174
1175 case kWhatMoreDataQueued:
1176 {
1177 break;
1178 }
1179
Andreas Huber1aef2112011-01-04 14:01:29 -08001180 case kWhatReset:
1181 {
Steve Block3856b092011-10-20 11:56:00 +01001182 ALOGV("kWhatReset");
Andreas Huber1aef2112011-01-04 14:01:29 -08001183
Ronghua Wu64c2d172015-10-07 16:52:19 -07001184 mResetting = true;
1185
Andreas Hubera1f8ab02012-11-30 10:53:22 -08001186 mDeferredActions.push_back(
Wei Jiafef808d2014-10-31 17:57:05 -07001187 new FlushDecoderAction(
1188 FLUSH_CMD_SHUTDOWN /* audio */,
1189 FLUSH_CMD_SHUTDOWN /* video */));
Andreas Huberb7c8e912012-11-27 15:02:53 -08001190
Andreas Hubera1f8ab02012-11-30 10:53:22 -08001191 mDeferredActions.push_back(
1192 new SimpleAction(&NuPlayer::performReset));
Andreas Huberb58ce9f2011-11-28 16:27:35 -08001193
Andreas Hubera1f8ab02012-11-30 10:53:22 -08001194 processDeferredActions();
Andreas Huber1aef2112011-01-04 14:01:29 -08001195 break;
1196 }
1197
Andreas Huber43c3e6c2011-01-05 12:17:08 -08001198 case kWhatSeek:
1199 {
1200 int64_t seekTimeUs;
Wei Jiae427abf2014-09-22 15:21:11 -07001201 int32_t needNotify;
Andreas Huber43c3e6c2011-01-05 12:17:08 -08001202 CHECK(msg->findInt64("seekTimeUs", &seekTimeUs));
Wei Jiae427abf2014-09-22 15:21:11 -07001203 CHECK(msg->findInt32("needNotify", &needNotify));
Andreas Huber43c3e6c2011-01-05 12:17:08 -08001204
Wei Jiae427abf2014-09-22 15:21:11 -07001205 ALOGV("kWhatSeek seekTimeUs=%lld us, needNotify=%d",
Lajos Molnar6d339f12015-04-17 16:15:53 -07001206 (long long)seekTimeUs, needNotify);
Andreas Huber43c3e6c2011-01-05 12:17:08 -08001207
Wei Jia1061c9c2015-05-19 16:02:17 -07001208 if (!mStarted) {
1209 // Seek before the player is started. In order to preview video,
1210 // need to start the player and pause it. This branch is called
1211 // only once if needed. After the player is started, any seek
1212 // operation will go through normal path.
Robert Shih0c61a0d2015-07-06 15:09:10 -07001213 // Audio-only cases are handled separately.
Wei Jia1061c9c2015-05-19 16:02:17 -07001214 onStart(seekTimeUs);
Robert Shih0c61a0d2015-07-06 15:09:10 -07001215 if (mStarted) {
1216 onPause();
1217 mPausedByClient = true;
1218 }
Wei Jia1061c9c2015-05-19 16:02:17 -07001219 if (needNotify) {
1220 notifyDriverSeekComplete();
1221 }
1222 break;
1223 }
1224
Andreas Hubera1f8ab02012-11-30 10:53:22 -08001225 mDeferredActions.push_back(
Wei Jiafef808d2014-10-31 17:57:05 -07001226 new FlushDecoderAction(FLUSH_CMD_FLUSH /* audio */,
1227 FLUSH_CMD_FLUSH /* video */));
Andreas Huber43c3e6c2011-01-05 12:17:08 -08001228
Wei Jiae427abf2014-09-22 15:21:11 -07001229 mDeferredActions.push_back(
Wei Jia29840802015-05-15 17:11:38 -07001230 new SeekAction(seekTimeUs));
Andreas Huber43c3e6c2011-01-05 12:17:08 -08001231
Chong Zhangf8d71772014-11-26 15:08:34 -08001232 // After a flush without shutdown, decoder is paused.
1233 // Don't resume it until source seek is done, otherwise it could
Chong Zhang7137ec72014-11-12 16:41:05 -08001234 // start pulling stale data too soon.
1235 mDeferredActions.push_back(
Chong Zhangf8d71772014-11-26 15:08:34 -08001236 new ResumeDecoderAction(needNotify));
Chong Zhang7137ec72014-11-12 16:41:05 -08001237
Andreas Hubera1f8ab02012-11-30 10:53:22 -08001238 processDeferredActions();
Andreas Huber43c3e6c2011-01-05 12:17:08 -08001239 break;
1240 }
1241
Andreas Huberb4082222011-01-20 15:23:04 -08001242 case kWhatPause:
1243 {
Chong Zhangefbb6192015-01-30 17:13:27 -08001244 onPause();
1245 mPausedByClient = true;
Andreas Huberb4082222011-01-20 15:23:04 -08001246 break;
1247 }
1248
Andreas Huberb5f25f02013-02-05 10:14:26 -08001249 case kWhatSourceNotify:
1250 {
Andreas Huber9575c962013-02-05 13:59:56 -08001251 onSourceNotify(msg);
Andreas Huberb5f25f02013-02-05 10:14:26 -08001252 break;
1253 }
1254
Chong Zhanga7fa1d92014-06-11 14:49:23 -07001255 case kWhatClosedCaptionNotify:
1256 {
1257 onClosedCaptionNotify(msg);
1258 break;
1259 }
1260
Andreas Huberf9334412010-12-15 15:17:42 -08001261 default:
1262 TRESPASS();
1263 break;
1264 }
1265}
1266
Wei Jia94211742014-10-28 17:09:06 -07001267void NuPlayer::onResume() {
Ronghua Wu64c2d172015-10-07 16:52:19 -07001268 if (!mPaused || mResetting) {
1269 ALOGD_IF(mResetting, "resetting, onResume discarded");
Chong Zhangefbb6192015-01-30 17:13:27 -08001270 return;
1271 }
1272 mPaused = false;
Wei Jia94211742014-10-28 17:09:06 -07001273 if (mSource != NULL) {
1274 mSource->resume();
1275 } else {
1276 ALOGW("resume called when source is gone or not set");
1277 }
1278 // |mAudioDecoder| may have been released due to the pause timeout, so re-create it if
1279 // needed.
1280 if (audioDecoderStillNeeded() && mAudioDecoder == NULL) {
1281 instantiateDecoder(true /* audio */, &mAudioDecoder);
1282 }
1283 if (mRenderer != NULL) {
1284 mRenderer->resume();
1285 } else {
1286 ALOGW("resume called when renderer is gone or not set");
1287 }
1288}
1289
Lajos Molnarfcd3e942015-03-31 10:06:48 -07001290status_t NuPlayer::onInstantiateSecureDecoders() {
1291 status_t err;
1292 if (!(mSourceFlags & Source::FLAG_SECURE)) {
1293 return BAD_TYPE;
1294 }
1295
1296 if (mRenderer != NULL) {
1297 ALOGE("renderer should not be set when instantiating secure decoders");
1298 return UNKNOWN_ERROR;
1299 }
1300
1301 // TRICKY: We rely on mRenderer being null, so that decoder does not start requesting
1302 // data on instantiation.
Lajos Molnar1de1e252015-04-30 18:18:34 -07001303 if (mSurface != NULL) {
Lajos Molnarfcd3e942015-03-31 10:06:48 -07001304 err = instantiateDecoder(false, &mVideoDecoder);
1305 if (err != OK) {
1306 return err;
1307 }
1308 }
1309
1310 if (mAudioSink != NULL) {
1311 err = instantiateDecoder(true, &mAudioDecoder);
1312 if (err != OK) {
1313 return err;
1314 }
1315 }
1316 return OK;
1317}
1318
Wei Jia1061c9c2015-05-19 16:02:17 -07001319void NuPlayer::onStart(int64_t startPositionUs) {
Robert Shih0c61a0d2015-07-06 15:09:10 -07001320 if (!mSourceStarted) {
1321 mSourceStarted = true;
1322 mSource->start();
1323 }
1324 if (startPositionUs > 0) {
1325 performSeek(startPositionUs);
1326 if (mSource->getFormat(false /* audio */) == NULL) {
1327 return;
1328 }
1329 }
1330
Wei Jia94211742014-10-28 17:09:06 -07001331 mOffloadAudio = false;
1332 mAudioEOS = false;
1333 mVideoEOS = false;
Wei Jia94211742014-10-28 17:09:06 -07001334 mStarted = true;
1335
Wei Jia94211742014-10-28 17:09:06 -07001336 uint32_t flags = 0;
1337
1338 if (mSource->isRealTime()) {
1339 flags |= Renderer::FLAG_REAL_TIME;
1340 }
1341
1342 sp<MetaData> audioMeta = mSource->getFormatMeta(true /* audio */);
Andy Hung12a84132015-10-20 16:09:21 -07001343 ALOGV_IF(audioMeta == NULL, "no metadata for audio source"); // video only stream
Wei Jia94211742014-10-28 17:09:06 -07001344 audio_stream_type_t streamType = AUDIO_STREAM_MUSIC;
1345 if (mAudioSink != NULL) {
1346 streamType = mAudioSink->getAudioStreamType();
1347 }
1348
1349 sp<AMessage> videoFormat = mSource->getFormat(false /* audio */);
1350
1351 mOffloadAudio =
Wei Jiabf70feb2016-02-19 15:47:16 -08001352 canOffloadStream(audioMeta, (videoFormat != NULL), mSource->isStreaming(), streamType)
1353 && (mPlaybackSettings.mSpeed == 1.f && mPlaybackSettings.mPitch == 1.f);
Wei Jia94211742014-10-28 17:09:06 -07001354 if (mOffloadAudio) {
1355 flags |= Renderer::FLAG_OFFLOAD_AUDIO;
1356 }
1357
Lajos Molnar1d15ab52015-03-04 16:46:34 -08001358 sp<AMessage> notify = new AMessage(kWhatRendererNotify, this);
Wei Jia94211742014-10-28 17:09:06 -07001359 ++mRendererGeneration;
1360 notify->setInt32("generation", mRendererGeneration);
1361 mRenderer = new Renderer(mAudioSink, notify, flags);
Wei Jia94211742014-10-28 17:09:06 -07001362 mRendererLooper = new ALooper;
1363 mRendererLooper->setName("NuPlayerRenderer");
1364 mRendererLooper->start(false, false, ANDROID_PRIORITY_AUDIO);
1365 mRendererLooper->registerHandler(mRenderer);
Lajos Molnar3a474aa2015-04-24 17:10:07 -07001366
1367 status_t err = mRenderer->setPlaybackSettings(mPlaybackSettings);
1368 if (err != OK) {
1369 mSource->stop();
Robert Shih0c61a0d2015-07-06 15:09:10 -07001370 mSourceStarted = false;
Lajos Molnar3a474aa2015-04-24 17:10:07 -07001371 notifyListener(MEDIA_ERROR, MEDIA_ERROR_UNKNOWN, err);
1372 return;
Wei Jiac8206ff2015-03-04 13:59:37 -08001373 }
Wei Jia94211742014-10-28 17:09:06 -07001374
Ronghua Wuc8a70d32015-04-29 16:26:34 -07001375 float rate = getFrameRate();
1376 if (rate > 0) {
Wei Jia94211742014-10-28 17:09:06 -07001377 mRenderer->setVideoFrameRate(rate);
1378 }
1379
Wei Jiac6cfd702014-11-11 16:33:20 -08001380 if (mVideoDecoder != NULL) {
1381 mVideoDecoder->setRenderer(mRenderer);
1382 }
1383 if (mAudioDecoder != NULL) {
1384 mAudioDecoder->setRenderer(mRenderer);
1385 }
1386
Wei Jia94211742014-10-28 17:09:06 -07001387 postScanSources();
1388}
1389
Chong Zhangefbb6192015-01-30 17:13:27 -08001390void NuPlayer::onPause() {
1391 if (mPaused) {
1392 return;
1393 }
1394 mPaused = true;
1395 if (mSource != NULL) {
1396 mSource->pause();
1397 } else {
1398 ALOGW("pause called when source is gone or not set");
1399 }
1400 if (mRenderer != NULL) {
1401 mRenderer->pause();
1402 } else {
1403 ALOGW("pause called when renderer is gone or not set");
1404 }
1405}
1406
Ronghua Wud7988b12014-10-03 15:19:10 -07001407bool NuPlayer::audioDecoderStillNeeded() {
1408 // Audio decoder is no longer needed if it's in shut/shutting down status.
1409 return ((mFlushingAudio != SHUT_DOWN) && (mFlushingAudio != SHUTTING_DOWN_DECODER));
1410}
1411
Andy Hung8d121d42014-10-03 09:53:53 -07001412void NuPlayer::handleFlushComplete(bool audio, bool isDecoder) {
1413 // We wait for both the decoder flush and the renderer flush to complete
1414 // before entering either the FLUSHED or the SHUTTING_DOWN_DECODER state.
1415
1416 mFlushComplete[audio][isDecoder] = true;
1417 if (!mFlushComplete[audio][!isDecoder]) {
1418 return;
1419 }
1420
1421 FlushStatus *state = audio ? &mFlushingAudio : &mFlushingVideo;
1422 switch (*state) {
1423 case FLUSHING_DECODER:
1424 {
1425 *state = FLUSHED;
Andy Hung8d121d42014-10-03 09:53:53 -07001426 break;
1427 }
1428
1429 case FLUSHING_DECODER_SHUTDOWN:
1430 {
1431 *state = SHUTTING_DOWN_DECODER;
1432
1433 ALOGV("initiating %s decoder shutdown", audio ? "audio" : "video");
1434 if (!audio) {
Andy Hung8d121d42014-10-03 09:53:53 -07001435 // Widevine source reads must stop before releasing the video decoder.
1436 if (mSource != NULL && mSourceFlags & Source::FLAG_SECURE) {
1437 mSource->stop();
Robert Shih0c61a0d2015-07-06 15:09:10 -07001438 mSourceStarted = false;
Andy Hung8d121d42014-10-03 09:53:53 -07001439 }
1440 }
1441 getDecoder(audio)->initiateShutdown();
1442 break;
1443 }
1444
1445 default:
1446 // decoder flush completes only occur in a flushing state.
1447 LOG_ALWAYS_FATAL_IF(isDecoder, "decoder flush in invalid state %d", *state);
1448 break;
1449 }
1450}
1451
Andreas Huber3831a062010-12-21 10:22:33 -08001452void NuPlayer::finishFlushIfPossible() {
Wei Jia53904f32014-07-29 10:22:53 -07001453 if (mFlushingAudio != NONE && mFlushingAudio != FLUSHED
1454 && mFlushingAudio != SHUT_DOWN) {
Andreas Huber3831a062010-12-21 10:22:33 -08001455 return;
1456 }
1457
Wei Jia53904f32014-07-29 10:22:53 -07001458 if (mFlushingVideo != NONE && mFlushingVideo != FLUSHED
1459 && mFlushingVideo != SHUT_DOWN) {
Andreas Huber3831a062010-12-21 10:22:33 -08001460 return;
1461 }
1462
Steve Block3856b092011-10-20 11:56:00 +01001463 ALOGV("both audio and video are flushed now.");
Andreas Huber3831a062010-12-21 10:22:33 -08001464
Andreas Huber3831a062010-12-21 10:22:33 -08001465 mFlushingAudio = NONE;
1466 mFlushingVideo = NONE;
Andreas Huber3831a062010-12-21 10:22:33 -08001467
Andy Hung8d121d42014-10-03 09:53:53 -07001468 clearFlushComplete();
1469
Andreas Hubera1f8ab02012-11-30 10:53:22 -08001470 processDeferredActions();
Andreas Huber1aef2112011-01-04 14:01:29 -08001471}
1472
1473void NuPlayer::postScanSources() {
1474 if (mScanSourcesPending) {
1475 return;
1476 }
1477
Lajos Molnar1d15ab52015-03-04 16:46:34 -08001478 sp<AMessage> msg = new AMessage(kWhatScanSources, this);
Andreas Huber1aef2112011-01-04 14:01:29 -08001479 msg->setInt32("generation", mScanSourcesGeneration);
1480 msg->post();
1481
1482 mScanSourcesPending = true;
1483}
1484
Wei Jia41cd4632016-05-13 10:53:30 -07001485void NuPlayer::tryOpenAudioSinkForOffload(
1486 const sp<AMessage> &format, const sp<MetaData> &audioMeta, bool hasVideo) {
Andy Hung202bce12014-12-03 11:47:36 -08001487 // Note: This is called early in NuPlayer to determine whether offloading
1488 // is possible; otherwise the decoders call the renderer openAudioSink directly.
Andy Hung282a7e32014-08-14 15:56:34 -07001489
Andy Hung202bce12014-12-03 11:47:36 -08001490 status_t err = mRenderer->openAudioSink(
1491 format, true /* offloadOnly */, hasVideo, AUDIO_OUTPUT_FLAG_NONE, &mOffloadAudio);
1492 if (err != OK) {
1493 // Any failure we turn off mOffloadAudio.
1494 mOffloadAudio = false;
1495 } else if (mOffloadAudio) {
Chong Zhang3b9eb1f2014-10-15 17:05:08 -07001496 sendMetaDataToHal(mAudioSink, audioMeta);
Andy Hung282a7e32014-08-14 15:56:34 -07001497 }
1498}
1499
1500void NuPlayer::closeAudioSink() {
Chong Zhang3b9eb1f2014-10-15 17:05:08 -07001501 mRenderer->closeAudioSink();
Andy Hung282a7e32014-08-14 15:56:34 -07001502}
1503
Wei Jia5031b2f2016-02-25 11:19:31 -08001504void NuPlayer::restartAudio(
Wei Jiabf70feb2016-02-19 15:47:16 -08001505 int64_t currentPositionUs, bool forceNonOffload, bool needsToCreateAudioDecoder) {
Wei Jiaa05f1e32016-03-25 16:31:22 -07001506 if (mAudioDecoder != NULL) {
1507 mAudioDecoder->pause();
1508 mAudioDecoder.clear();
1509 ++mAudioDecoderGeneration;
1510 }
Wei Jiabf70feb2016-02-19 15:47:16 -08001511 if (mFlushingAudio == FLUSHING_DECODER) {
1512 mFlushComplete[1 /* audio */][1 /* isDecoder */] = true;
1513 mFlushingAudio = FLUSHED;
1514 finishFlushIfPossible();
1515 } else if (mFlushingAudio == FLUSHING_DECODER_SHUTDOWN
1516 || mFlushingAudio == SHUTTING_DOWN_DECODER) {
1517 mFlushComplete[1 /* audio */][1 /* isDecoder */] = true;
1518 mFlushingAudio = SHUT_DOWN;
1519 finishFlushIfPossible();
1520 needsToCreateAudioDecoder = false;
1521 }
1522 if (mRenderer == NULL) {
1523 return;
1524 }
1525 closeAudioSink();
1526 mRenderer->flush(true /* audio */, false /* notifyComplete */);
1527 if (mVideoDecoder != NULL) {
1528 mRenderer->flush(false /* audio */, false /* notifyComplete */);
1529 }
1530
1531 performSeek(currentPositionUs);
1532
1533 if (forceNonOffload) {
1534 mRenderer->signalDisableOffloadAudio();
1535 mOffloadAudio = false;
1536 }
1537 if (needsToCreateAudioDecoder) {
Wei Jiaa05f1e32016-03-25 16:31:22 -07001538 instantiateDecoder(true /* audio */, &mAudioDecoder, !forceNonOffload);
Wei Jiabf70feb2016-02-19 15:47:16 -08001539 }
1540}
1541
Wei Jia41cd4632016-05-13 10:53:30 -07001542void NuPlayer::determineAudioModeChange(const sp<AMessage> &audioFormat) {
Wei Jiae4d18c72015-07-13 17:58:11 -07001543 if (mSource == NULL || mAudioSink == NULL) {
1544 return;
1545 }
1546
1547 if (mRenderer == NULL) {
1548 ALOGW("No renderer can be used to determine audio mode. Use non-offload for safety.");
1549 mOffloadAudio = false;
1550 return;
1551 }
1552
1553 sp<MetaData> audioMeta = mSource->getFormatMeta(true /* audio */);
1554 sp<AMessage> videoFormat = mSource->getFormat(false /* audio */);
1555 audio_stream_type_t streamType = mAudioSink->getAudioStreamType();
1556 const bool hasVideo = (videoFormat != NULL);
1557 const bool canOffload = canOffloadStream(
Wei Jiabf70feb2016-02-19 15:47:16 -08001558 audioMeta, hasVideo, mSource->isStreaming(), streamType)
1559 && (mPlaybackSettings.mSpeed == 1.f && mPlaybackSettings.mPitch == 1.f);
Wei Jiae4d18c72015-07-13 17:58:11 -07001560 if (canOffload) {
1561 if (!mOffloadAudio) {
1562 mRenderer->signalEnableOffloadAudio();
1563 }
1564 // open audio sink early under offload mode.
Wei Jia41cd4632016-05-13 10:53:30 -07001565 tryOpenAudioSinkForOffload(audioFormat, audioMeta, hasVideo);
Wei Jiae4d18c72015-07-13 17:58:11 -07001566 } else {
Robert Shihe1d70192015-07-23 17:54:13 -07001567 if (mOffloadAudio) {
1568 mRenderer->signalDisableOffloadAudio();
1569 mOffloadAudio = false;
1570 }
Wei Jiae4d18c72015-07-13 17:58:11 -07001571 }
1572}
1573
Wei Jiaa05f1e32016-03-25 16:31:22 -07001574status_t NuPlayer::instantiateDecoder(
1575 bool audio, sp<DecoderBase> *decoder, bool checkAudioModeChange) {
Wei Jia566da802015-08-27 13:59:30 -07001576 // The audio decoder could be cleared by tear down. If still in shut down
1577 // process, no need to create a new audio decoder.
1578 if (*decoder != NULL || (audio && mFlushingAudio == SHUT_DOWN)) {
Andreas Huberf9334412010-12-15 15:17:42 -08001579 return OK;
1580 }
1581
Andreas Huber84066782011-08-16 09:34:26 -07001582 sp<AMessage> format = mSource->getFormat(audio);
Andreas Huberf9334412010-12-15 15:17:42 -08001583
Andreas Huber84066782011-08-16 09:34:26 -07001584 if (format == NULL) {
Robert Shih7350b052015-10-01 15:50:14 -07001585 return UNKNOWN_ERROR;
1586 } else {
1587 status_t err;
1588 if (format->findInt32("err", &err) && err) {
1589 return err;
1590 }
Andreas Huberf9334412010-12-15 15:17:42 -08001591 }
1592
Ronghua Wu8db88132015-04-22 13:51:35 -07001593 format->setInt32("priority", 0 /* realtime */);
1594
Andreas Huber3fe62152011-09-16 15:09:22 -07001595 if (!audio) {
Andreas Huber84066782011-08-16 09:34:26 -07001596 AString mime;
1597 CHECK(format->findString("mime", &mime));
Chong Zhanga7fa1d92014-06-11 14:49:23 -07001598
Lajos Molnar1d15ab52015-03-04 16:46:34 -08001599 sp<AMessage> ccNotify = new AMessage(kWhatClosedCaptionNotify, this);
Chong Zhang341ab6e2015-02-04 13:37:18 -08001600 if (mCCDecoder == NULL) {
1601 mCCDecoder = new CCDecoder(ccNotify);
1602 }
Lajos Molnar09524832014-07-17 14:29:51 -07001603
1604 if (mSourceFlags & Source::FLAG_SECURE) {
1605 format->setInt32("secure", true);
1606 }
Chong Zhang17134602015-01-07 16:14:34 -08001607
1608 if (mSourceFlags & Source::FLAG_PROTECTED) {
1609 format->setInt32("protected", true);
1610 }
Ronghua Wu8db88132015-04-22 13:51:35 -07001611
Ronghua Wuc8a70d32015-04-29 16:26:34 -07001612 float rate = getFrameRate();
1613 if (rate > 0) {
Lajos Molnar3a474aa2015-04-24 17:10:07 -07001614 format->setFloat("operating-rate", rate * mPlaybackSettings.mSpeed);
Ronghua Wu8db88132015-04-22 13:51:35 -07001615 }
Andreas Huber3fe62152011-09-16 15:09:22 -07001616 }
1617
Wei Jiabc2fb722014-07-08 16:37:57 -07001618 if (audio) {
Lajos Molnar1d15ab52015-03-04 16:46:34 -08001619 sp<AMessage> notify = new AMessage(kWhatAudioNotify, this);
Wei Jia88703c32014-08-06 11:24:07 -07001620 ++mAudioDecoderGeneration;
1621 notify->setInt32("generation", mAudioDecoderGeneration);
1622
Wei Jiaa05f1e32016-03-25 16:31:22 -07001623 if (checkAudioModeChange) {
Wei Jia41cd4632016-05-13 10:53:30 -07001624 determineAudioModeChange(format);
Wei Jiaa05f1e32016-03-25 16:31:22 -07001625 }
Wei Jiabc2fb722014-07-08 16:37:57 -07001626 if (mOffloadAudio) {
Wei Jia14532f22015-12-29 11:28:15 -08001627 mSource->setOffloadAudio(true /* offload */);
1628
Haynes Mathew George8b635332015-03-30 17:59:47 -07001629 const bool hasVideo = (mSource->getFormat(false /*audio */) != NULL);
1630 format->setInt32("has-video", hasVideo);
Wei Jiac6cfd702014-11-11 16:33:20 -08001631 *decoder = new DecoderPassThrough(notify, mSource, mRenderer);
Wei Jiabc2fb722014-07-08 16:37:57 -07001632 } else {
Wei Jia14532f22015-12-29 11:28:15 -08001633 mSource->setOffloadAudio(false /* offload */);
1634
Ronghua Wu68845c12015-07-21 09:50:48 -07001635 *decoder = new Decoder(notify, mSource, mPID, mRenderer);
Wei Jiabc2fb722014-07-08 16:37:57 -07001636 }
1637 } else {
Lajos Molnar1d15ab52015-03-04 16:46:34 -08001638 sp<AMessage> notify = new AMessage(kWhatVideoNotify, this);
Wei Jia88703c32014-08-06 11:24:07 -07001639 ++mVideoDecoderGeneration;
1640 notify->setInt32("generation", mVideoDecoderGeneration);
1641
Chong Zhang7137ec72014-11-12 16:41:05 -08001642 *decoder = new Decoder(
Ronghua Wu68845c12015-07-21 09:50:48 -07001643 notify, mSource, mPID, mRenderer, mSurface, mCCDecoder);
Lajos Molnard9fd6312014-11-06 11:00:00 -08001644
1645 // enable FRC if high-quality AV sync is requested, even if not
Lajos Molnar1de1e252015-04-30 18:18:34 -07001646 // directly queuing to display, as this will even improve textureview
Lajos Molnard9fd6312014-11-06 11:00:00 -08001647 // playback.
1648 {
1649 char value[PROPERTY_VALUE_MAX];
1650 if (property_get("persist.sys.media.avsync", value, NULL) &&
1651 (!strcmp("1", value) || !strcasecmp("true", value))) {
1652 format->setInt32("auto-frc", 1);
1653 }
1654 }
Wei Jiabc2fb722014-07-08 16:37:57 -07001655 }
Lajos Molnar1cd13982014-01-17 15:12:51 -08001656 (*decoder)->init();
Andreas Huber84066782011-08-16 09:34:26 -07001657 (*decoder)->configure(format);
Andreas Huberf9334412010-12-15 15:17:42 -08001658
Lajos Molnar09524832014-07-17 14:29:51 -07001659 // allocate buffers to decrypt widevine source buffers
1660 if (!audio && (mSourceFlags & Source::FLAG_SECURE)) {
1661 Vector<sp<ABuffer> > inputBufs;
1662 CHECK_EQ((*decoder)->getInputBuffers(&inputBufs), (status_t)OK);
1663
1664 Vector<MediaBuffer *> mediaBufs;
1665 for (size_t i = 0; i < inputBufs.size(); i++) {
1666 const sp<ABuffer> &buffer = inputBufs[i];
1667 MediaBuffer *mbuf = new MediaBuffer(buffer->data(), buffer->size());
1668 mediaBufs.push(mbuf);
1669 }
1670
1671 status_t err = mSource->setBuffers(audio, mediaBufs);
1672 if (err != OK) {
1673 for (size_t i = 0; i < mediaBufs.size(); ++i) {
1674 mediaBufs[i]->release();
1675 }
1676 mediaBufs.clear();
1677 ALOGE("Secure source didn't support secure mediaBufs.");
1678 return err;
1679 }
1680 }
Andreas Huberf9334412010-12-15 15:17:42 -08001681 return OK;
1682}
1683
Chong Zhangced1c2f2014-08-08 15:22:35 -07001684void NuPlayer::updateVideoSize(
1685 const sp<AMessage> &inputFormat,
1686 const sp<AMessage> &outputFormat) {
1687 if (inputFormat == NULL) {
1688 ALOGW("Unknown video size, reporting 0x0!");
1689 notifyListener(MEDIA_SET_VIDEO_SIZE, 0, 0);
1690 return;
1691 }
1692
1693 int32_t displayWidth, displayHeight;
Chong Zhangced1c2f2014-08-08 15:22:35 -07001694 if (outputFormat != NULL) {
1695 int32_t width, height;
1696 CHECK(outputFormat->findInt32("width", &width));
1697 CHECK(outputFormat->findInt32("height", &height));
1698
1699 int32_t cropLeft, cropTop, cropRight, cropBottom;
1700 CHECK(outputFormat->findRect(
1701 "crop",
1702 &cropLeft, &cropTop, &cropRight, &cropBottom));
1703
1704 displayWidth = cropRight - cropLeft + 1;
1705 displayHeight = cropBottom - cropTop + 1;
1706
1707 ALOGV("Video output format changed to %d x %d "
1708 "(crop: %d x %d @ (%d, %d))",
1709 width, height,
1710 displayWidth,
1711 displayHeight,
1712 cropLeft, cropTop);
1713 } else {
1714 CHECK(inputFormat->findInt32("width", &displayWidth));
1715 CHECK(inputFormat->findInt32("height", &displayHeight));
1716
1717 ALOGV("Video input format %d x %d", displayWidth, displayHeight);
1718 }
1719
1720 // Take into account sample aspect ratio if necessary:
1721 int32_t sarWidth, sarHeight;
1722 if (inputFormat->findInt32("sar-width", &sarWidth)
1723 && inputFormat->findInt32("sar-height", &sarHeight)) {
1724 ALOGV("Sample aspect ratio %d : %d", sarWidth, sarHeight);
1725
1726 displayWidth = (displayWidth * sarWidth) / sarHeight;
1727
1728 ALOGV("display dimensions %d x %d", displayWidth, displayHeight);
1729 }
1730
1731 int32_t rotationDegrees;
1732 if (!inputFormat->findInt32("rotation-degrees", &rotationDegrees)) {
1733 rotationDegrees = 0;
1734 }
1735
1736 if (rotationDegrees == 90 || rotationDegrees == 270) {
1737 int32_t tmp = displayWidth;
1738 displayWidth = displayHeight;
1739 displayHeight = tmp;
1740 }
1741
1742 notifyListener(
1743 MEDIA_SET_VIDEO_SIZE,
1744 displayWidth,
1745 displayHeight);
1746}
1747
Chong Zhangdcb89b32013-08-06 09:44:47 -07001748void NuPlayer::notifyListener(int msg, int ext1, int ext2, const Parcel *in) {
Andreas Huber43c3e6c2011-01-05 12:17:08 -08001749 if (mDriver == NULL) {
Andreas Huberf9334412010-12-15 15:17:42 -08001750 return;
1751 }
1752
Andreas Huber43c3e6c2011-01-05 12:17:08 -08001753 sp<NuPlayerDriver> driver = mDriver.promote();
Andreas Huberf9334412010-12-15 15:17:42 -08001754
Andreas Huber43c3e6c2011-01-05 12:17:08 -08001755 if (driver == NULL) {
Andreas Huberf9334412010-12-15 15:17:42 -08001756 return;
1757 }
1758
Chong Zhangdcb89b32013-08-06 09:44:47 -07001759 driver->notifyListener(msg, ext1, ext2, in);
Andreas Huberf9334412010-12-15 15:17:42 -08001760}
1761
Chong Zhang7137ec72014-11-12 16:41:05 -08001762void NuPlayer::flushDecoder(bool audio, bool needShutdown) {
Andreas Huber14f76722013-01-15 09:04:18 -08001763 ALOGV("[%s] flushDecoder needShutdown=%d",
1764 audio ? "audio" : "video", needShutdown);
1765
Chong Zhang7137ec72014-11-12 16:41:05 -08001766 const sp<DecoderBase> &decoder = getDecoder(audio);
Lajos Molnar87603c02014-08-20 19:25:30 -07001767 if (decoder == NULL) {
Steve Blockdf64d152012-01-04 20:05:49 +00001768 ALOGI("flushDecoder %s without decoder present",
Andreas Huber6e3d3112011-11-28 12:36:11 -08001769 audio ? "audio" : "video");
Lajos Molnar87603c02014-08-20 19:25:30 -07001770 return;
Andreas Huber6e3d3112011-11-28 12:36:11 -08001771 }
1772
Andreas Huber1aef2112011-01-04 14:01:29 -08001773 // Make sure we don't continue to scan sources until we finish flushing.
1774 ++mScanSourcesGeneration;
Chong Zhang3b032b32015-04-17 15:49:06 -07001775 if (mScanSourcesPending) {
1776 mDeferredActions.push_back(
1777 new SimpleAction(&NuPlayer::performScanSources));
1778 mScanSourcesPending = false;
1779 }
Andreas Huber1aef2112011-01-04 14:01:29 -08001780
Chong Zhang7137ec72014-11-12 16:41:05 -08001781 decoder->signalFlush();
Andreas Huber1aef2112011-01-04 14:01:29 -08001782
1783 FlushStatus newStatus =
1784 needShutdown ? FLUSHING_DECODER_SHUTDOWN : FLUSHING_DECODER;
1785
Lajos Molnarfcd3e942015-03-31 10:06:48 -07001786 mFlushComplete[audio][false /* isDecoder */] = (mRenderer == NULL);
Andy Hung8d121d42014-10-03 09:53:53 -07001787 mFlushComplete[audio][true /* isDecoder */] = false;
Andreas Huber1aef2112011-01-04 14:01:29 -08001788 if (audio) {
Wei Jia53904f32014-07-29 10:22:53 -07001789 ALOGE_IF(mFlushingAudio != NONE,
1790 "audio flushDecoder() is called in state %d", mFlushingAudio);
Andreas Huber1aef2112011-01-04 14:01:29 -08001791 mFlushingAudio = newStatus;
Andreas Huber1aef2112011-01-04 14:01:29 -08001792 } else {
Wei Jia53904f32014-07-29 10:22:53 -07001793 ALOGE_IF(mFlushingVideo != NONE,
1794 "video flushDecoder() is called in state %d", mFlushingVideo);
Andreas Huber1aef2112011-01-04 14:01:29 -08001795 mFlushingVideo = newStatus;
Andreas Huber1aef2112011-01-04 14:01:29 -08001796 }
1797}
1798
Chong Zhangced1c2f2014-08-08 15:22:35 -07001799void NuPlayer::queueDecoderShutdown(
1800 bool audio, bool video, const sp<AMessage> &reply) {
1801 ALOGI("queueDecoderShutdown audio=%d, video=%d", audio, video);
Andreas Huber84066782011-08-16 09:34:26 -07001802
Chong Zhangced1c2f2014-08-08 15:22:35 -07001803 mDeferredActions.push_back(
Wei Jiafef808d2014-10-31 17:57:05 -07001804 new FlushDecoderAction(
1805 audio ? FLUSH_CMD_SHUTDOWN : FLUSH_CMD_NONE,
1806 video ? FLUSH_CMD_SHUTDOWN : FLUSH_CMD_NONE));
Andreas Huber84066782011-08-16 09:34:26 -07001807
Chong Zhangced1c2f2014-08-08 15:22:35 -07001808 mDeferredActions.push_back(
1809 new SimpleAction(&NuPlayer::performScanSources));
Andreas Huber84066782011-08-16 09:34:26 -07001810
Chong Zhangced1c2f2014-08-08 15:22:35 -07001811 mDeferredActions.push_back(new PostMessageAction(reply));
1812
1813 processDeferredActions();
Andreas Huber84066782011-08-16 09:34:26 -07001814}
1815
James Dong0d268a32012-08-31 12:18:27 -07001816status_t NuPlayer::setVideoScalingMode(int32_t mode) {
1817 mVideoScalingMode = mode;
Lajos Molnar1de1e252015-04-30 18:18:34 -07001818 if (mSurface != NULL) {
1819 status_t ret = native_window_set_scaling_mode(mSurface.get(), mVideoScalingMode);
James Dong0d268a32012-08-31 12:18:27 -07001820 if (ret != OK) {
1821 ALOGE("Failed to set scaling mode (%d): %s",
1822 -ret, strerror(-ret));
1823 return ret;
1824 }
1825 }
1826 return OK;
1827}
1828
Chong Zhangdcb89b32013-08-06 09:44:47 -07001829status_t NuPlayer::getTrackInfo(Parcel* reply) const {
Lajos Molnar1d15ab52015-03-04 16:46:34 -08001830 sp<AMessage> msg = new AMessage(kWhatGetTrackInfo, this);
Chong Zhangdcb89b32013-08-06 09:44:47 -07001831 msg->setPointer("reply", reply);
1832
1833 sp<AMessage> response;
1834 status_t err = msg->postAndAwaitResponse(&response);
1835 return err;
1836}
1837
Robert Shih7c4f0d72014-07-09 18:53:31 -07001838status_t NuPlayer::getSelectedTrack(int32_t type, Parcel* reply) const {
Lajos Molnar1d15ab52015-03-04 16:46:34 -08001839 sp<AMessage> msg = new AMessage(kWhatGetSelectedTrack, this);
Robert Shih7c4f0d72014-07-09 18:53:31 -07001840 msg->setPointer("reply", reply);
1841 msg->setInt32("type", type);
1842
1843 sp<AMessage> response;
1844 status_t err = msg->postAndAwaitResponse(&response);
1845 if (err == OK && response != NULL) {
1846 CHECK(response->findInt32("err", &err));
1847 }
1848 return err;
1849}
1850
Robert Shih6ffb1fd2014-10-29 16:24:32 -07001851status_t NuPlayer::selectTrack(size_t trackIndex, bool select, int64_t timeUs) {
Lajos Molnar1d15ab52015-03-04 16:46:34 -08001852 sp<AMessage> msg = new AMessage(kWhatSelectTrack, this);
Chong Zhangdcb89b32013-08-06 09:44:47 -07001853 msg->setSize("trackIndex", trackIndex);
1854 msg->setInt32("select", select);
Robert Shih6ffb1fd2014-10-29 16:24:32 -07001855 msg->setInt64("timeUs", timeUs);
Chong Zhangdcb89b32013-08-06 09:44:47 -07001856
1857 sp<AMessage> response;
1858 status_t err = msg->postAndAwaitResponse(&response);
1859
Chong Zhang404fced2014-06-11 14:45:31 -07001860 if (err != OK) {
1861 return err;
1862 }
1863
1864 if (!response->findInt32("err", &err)) {
1865 err = OK;
1866 }
1867
Chong Zhangdcb89b32013-08-06 09:44:47 -07001868 return err;
1869}
1870
Ronghua Wua73d9e02014-10-08 15:13:29 -07001871status_t NuPlayer::getCurrentPosition(int64_t *mediaUs) {
1872 sp<Renderer> renderer = mRenderer;
1873 if (renderer == NULL) {
1874 return NO_INIT;
1875 }
1876
1877 return renderer->getCurrentPosition(mediaUs);
1878}
1879
Praveen Chavane1e5d7a2015-05-19 19:09:48 -07001880void NuPlayer::getStats(Vector<sp<AMessage> > *mTrackStats) {
1881 CHECK(mTrackStats != NULL);
1882
1883 mTrackStats->clear();
1884 if (mVideoDecoder != NULL) {
1885 mTrackStats->push_back(mVideoDecoder->getStats());
1886 }
1887 if (mAudioDecoder != NULL) {
1888 mTrackStats->push_back(mAudioDecoder->getStats());
Chong Zhang7137ec72014-11-12 16:41:05 -08001889 }
Ronghua Wua73d9e02014-10-08 15:13:29 -07001890}
1891
Marco Nelissenf0b72b52014-09-16 15:43:44 -07001892sp<MetaData> NuPlayer::getFileMeta() {
1893 return mSource->getFileFormatMeta();
1894}
1895
Ronghua Wuc8a70d32015-04-29 16:26:34 -07001896float NuPlayer::getFrameRate() {
1897 sp<MetaData> meta = mSource->getFormatMeta(false /* audio */);
1898 if (meta == NULL) {
1899 return 0;
1900 }
1901 int32_t rate;
1902 if (!meta->findInt32(kKeyFrameRate, &rate)) {
1903 // fall back to try file meta
1904 sp<MetaData> fileMeta = getFileMeta();
1905 if (fileMeta == NULL) {
1906 ALOGW("source has video meta but not file meta");
1907 return -1;
1908 }
1909 int32_t fileMetaRate;
1910 if (!fileMeta->findInt32(kKeyFrameRate, &fileMetaRate)) {
1911 return -1;
1912 }
1913 return fileMetaRate;
1914 }
1915 return rate;
1916}
1917
Andreas Huberb7c8e912012-11-27 15:02:53 -08001918void NuPlayer::schedulePollDuration() {
Lajos Molnar1d15ab52015-03-04 16:46:34 -08001919 sp<AMessage> msg = new AMessage(kWhatPollDuration, this);
Andreas Huberb7c8e912012-11-27 15:02:53 -08001920 msg->setInt32("generation", mPollDurationGeneration);
1921 msg->post();
1922}
1923
1924void NuPlayer::cancelPollDuration() {
1925 ++mPollDurationGeneration;
1926}
1927
Andreas Hubera1f8ab02012-11-30 10:53:22 -08001928void NuPlayer::processDeferredActions() {
1929 while (!mDeferredActions.empty()) {
1930 // We won't execute any deferred actions until we're no longer in
1931 // an intermediate state, i.e. one more more decoders are currently
1932 // flushing or shutting down.
1933
Andreas Hubera1f8ab02012-11-30 10:53:22 -08001934 if (mFlushingAudio != NONE || mFlushingVideo != NONE) {
1935 // We're currently flushing, postpone the reset until that's
1936 // completed.
1937
1938 ALOGV("postponing action mFlushingAudio=%d, mFlushingVideo=%d",
1939 mFlushingAudio, mFlushingVideo);
1940
1941 break;
1942 }
1943
1944 sp<Action> action = *mDeferredActions.begin();
1945 mDeferredActions.erase(mDeferredActions.begin());
1946
1947 action->execute(this);
1948 }
1949}
1950
Wei Jia29840802015-05-15 17:11:38 -07001951void NuPlayer::performSeek(int64_t seekTimeUs) {
1952 ALOGV("performSeek seekTimeUs=%lld us (%.2f secs)",
Lajos Molnar6d339f12015-04-17 16:15:53 -07001953 (long long)seekTimeUs,
Wei Jia29840802015-05-15 17:11:38 -07001954 seekTimeUs / 1E6);
Andreas Hubera1f8ab02012-11-30 10:53:22 -08001955
Andy Hungadf34bf2014-09-03 18:22:22 -07001956 if (mSource == NULL) {
1957 // This happens when reset occurs right before the loop mode
1958 // asynchronously seeks to the start of the stream.
1959 LOG_ALWAYS_FATAL_IF(mAudioDecoder != NULL || mVideoDecoder != NULL,
1960 "mSource is NULL and decoders not NULL audio(%p) video(%p)",
1961 mAudioDecoder.get(), mVideoDecoder.get());
1962 return;
1963 }
Robert Shih1a5c8592015-08-04 18:07:44 -07001964 mPreviousSeekTimeUs = seekTimeUs;
Andreas Hubera1f8ab02012-11-30 10:53:22 -08001965 mSource->seekTo(seekTimeUs);
Robert Shihd3b0bbb2014-07-23 15:00:25 -07001966 ++mTimedTextGeneration;
Andreas Hubera1f8ab02012-11-30 10:53:22 -08001967
Andreas Hubera1f8ab02012-11-30 10:53:22 -08001968 // everything's flushed, continue playback.
1969}
1970
Wei Jiafef808d2014-10-31 17:57:05 -07001971void NuPlayer::performDecoderFlush(FlushCommand audio, FlushCommand video) {
1972 ALOGV("performDecoderFlush audio=%d, video=%d", audio, video);
Andreas Hubera1f8ab02012-11-30 10:53:22 -08001973
Wei Jiafef808d2014-10-31 17:57:05 -07001974 if ((audio == FLUSH_CMD_NONE || mAudioDecoder == NULL)
1975 && (video == FLUSH_CMD_NONE || mVideoDecoder == NULL)) {
Andreas Hubera1f8ab02012-11-30 10:53:22 -08001976 return;
1977 }
1978
Wei Jiafef808d2014-10-31 17:57:05 -07001979 if (audio != FLUSH_CMD_NONE && mAudioDecoder != NULL) {
1980 flushDecoder(true /* audio */, (audio == FLUSH_CMD_SHUTDOWN));
Andreas Hubera1f8ab02012-11-30 10:53:22 -08001981 }
1982
Wei Jiafef808d2014-10-31 17:57:05 -07001983 if (video != FLUSH_CMD_NONE && mVideoDecoder != NULL) {
1984 flushDecoder(false /* audio */, (video == FLUSH_CMD_SHUTDOWN));
Andreas Hubera1f8ab02012-11-30 10:53:22 -08001985 }
1986}
1987
1988void NuPlayer::performReset() {
1989 ALOGV("performReset");
1990
1991 CHECK(mAudioDecoder == NULL);
1992 CHECK(mVideoDecoder == NULL);
1993
1994 cancelPollDuration();
1995
1996 ++mScanSourcesGeneration;
1997 mScanSourcesPending = false;
1998
Lajos Molnar09524832014-07-17 14:29:51 -07001999 if (mRendererLooper != NULL) {
2000 if (mRenderer != NULL) {
2001 mRendererLooper->unregisterHandler(mRenderer->id());
2002 }
2003 mRendererLooper->stop();
2004 mRendererLooper.clear();
2005 }
Andreas Hubera1f8ab02012-11-30 10:53:22 -08002006 mRenderer.clear();
Wei Jia57568df2014-09-22 10:16:29 -07002007 ++mRendererGeneration;
Andreas Hubera1f8ab02012-11-30 10:53:22 -08002008
2009 if (mSource != NULL) {
2010 mSource->stop();
Andreas Huberb5f25f02013-02-05 10:14:26 -08002011
Wei Jiac45a4b22016-04-15 15:30:23 -07002012 Mutex::Autolock autoLock(mSourceLock);
Andreas Hubera1f8ab02012-11-30 10:53:22 -08002013 mSource.clear();
2014 }
2015
2016 if (mDriver != NULL) {
2017 sp<NuPlayerDriver> driver = mDriver.promote();
2018 if (driver != NULL) {
2019 driver->notifyResetComplete();
2020 }
2021 }
Andreas Huber57a339c2012-12-03 11:18:00 -08002022
2023 mStarted = false;
Wei Jia8a092d32016-06-03 14:57:24 -07002024 mPrepared = false;
Ronghua Wu64c2d172015-10-07 16:52:19 -07002025 mResetting = false;
Robert Shih0c61a0d2015-07-06 15:09:10 -07002026 mSourceStarted = false;
Andreas Hubera1f8ab02012-11-30 10:53:22 -08002027}
2028
2029void NuPlayer::performScanSources() {
2030 ALOGV("performScanSources");
2031
Andreas Huber57a339c2012-12-03 11:18:00 -08002032 if (!mStarted) {
2033 return;
2034 }
2035
Andreas Hubera1f8ab02012-11-30 10:53:22 -08002036 if (mAudioDecoder == NULL || mVideoDecoder == NULL) {
2037 postScanSources();
2038 }
2039}
2040
Lajos Molnar1de1e252015-04-30 18:18:34 -07002041void NuPlayer::performSetSurface(const sp<Surface> &surface) {
Andreas Huber57a339c2012-12-03 11:18:00 -08002042 ALOGV("performSetSurface");
2043
Lajos Molnar1de1e252015-04-30 18:18:34 -07002044 mSurface = surface;
Andreas Huber57a339c2012-12-03 11:18:00 -08002045
2046 // XXX - ignore error from setVideoScalingMode for now
2047 setVideoScalingMode(mVideoScalingMode);
Chong Zhang13d6faa2014-08-22 15:35:28 -07002048
2049 if (mDriver != NULL) {
2050 sp<NuPlayerDriver> driver = mDriver.promote();
2051 if (driver != NULL) {
2052 driver->notifySetSurfaceComplete();
2053 }
2054 }
Andreas Huber57a339c2012-12-03 11:18:00 -08002055}
2056
Chong Zhangf8d71772014-11-26 15:08:34 -08002057void NuPlayer::performResumeDecoders(bool needNotify) {
2058 if (needNotify) {
2059 mResumePending = true;
2060 if (mVideoDecoder == NULL) {
2061 // if audio-only, we can notify seek complete now,
2062 // as the resume operation will be relatively fast.
2063 finishResume();
2064 }
2065 }
2066
Chong Zhang7137ec72014-11-12 16:41:05 -08002067 if (mVideoDecoder != NULL) {
Chong Zhangf8d71772014-11-26 15:08:34 -08002068 // When there is continuous seek, MediaPlayer will cache the seek
2069 // position, and send down new seek request when previous seek is
2070 // complete. Let's wait for at least one video output frame before
2071 // notifying seek complete, so that the video thumbnail gets updated
2072 // when seekbar is dragged.
2073 mVideoDecoder->signalResume(needNotify);
Chong Zhang7137ec72014-11-12 16:41:05 -08002074 }
2075
2076 if (mAudioDecoder != NULL) {
Chong Zhangf8d71772014-11-26 15:08:34 -08002077 mAudioDecoder->signalResume(false /* needNotify */);
2078 }
2079}
2080
2081void NuPlayer::finishResume() {
2082 if (mResumePending) {
2083 mResumePending = false;
Wei Jia1061c9c2015-05-19 16:02:17 -07002084 notifyDriverSeekComplete();
2085 }
2086}
2087
2088void NuPlayer::notifyDriverSeekComplete() {
2089 if (mDriver != NULL) {
2090 sp<NuPlayerDriver> driver = mDriver.promote();
2091 if (driver != NULL) {
2092 driver->notifySeekComplete();
Chong Zhangf8d71772014-11-26 15:08:34 -08002093 }
Chong Zhang7137ec72014-11-12 16:41:05 -08002094 }
2095}
2096
Andreas Huber9575c962013-02-05 13:59:56 -08002097void NuPlayer::onSourceNotify(const sp<AMessage> &msg) {
2098 int32_t what;
2099 CHECK(msg->findInt32("what", &what));
2100
2101 switch (what) {
Lajos Molnarfcd3e942015-03-31 10:06:48 -07002102 case Source::kWhatInstantiateSecureDecoders:
2103 {
2104 if (mSource == NULL) {
2105 // This is a stale notification from a source that was
2106 // asynchronously preparing when the client called reset().
2107 // We handled the reset, the source is gone.
2108 break;
2109 }
2110
2111 sp<AMessage> reply;
2112 CHECK(msg->findMessage("reply", &reply));
2113 status_t err = onInstantiateSecureDecoders();
2114 reply->setInt32("err", err);
2115 reply->post();
2116 break;
2117 }
2118
Andreas Huber9575c962013-02-05 13:59:56 -08002119 case Source::kWhatPrepared:
2120 {
Andreas Huberb5f28d42013-04-25 15:11:19 -07002121 if (mSource == NULL) {
2122 // This is a stale notification from a source that was
2123 // asynchronously preparing when the client called reset().
2124 // We handled the reset, the source is gone.
2125 break;
2126 }
2127
Andreas Huberec0c5972013-02-05 14:47:13 -08002128 int32_t err;
2129 CHECK(msg->findInt32("err", &err));
2130
Lajos Molnarfcd3e942015-03-31 10:06:48 -07002131 if (err != OK) {
2132 // shut down potential secure codecs in case client never calls reset
2133 mDeferredActions.push_back(
2134 new FlushDecoderAction(FLUSH_CMD_SHUTDOWN /* audio */,
2135 FLUSH_CMD_SHUTDOWN /* video */));
2136 processDeferredActions();
Wei Jia8a092d32016-06-03 14:57:24 -07002137 } else {
2138 mPrepared = true;
Lajos Molnarfcd3e942015-03-31 10:06:48 -07002139 }
2140
Andreas Huber9575c962013-02-05 13:59:56 -08002141 sp<NuPlayerDriver> driver = mDriver.promote();
2142 if (driver != NULL) {
Marco Nelissendd114d12014-05-28 15:23:14 -07002143 // notify duration first, so that it's definitely set when
2144 // the app received the "prepare complete" callback.
2145 int64_t durationUs;
2146 if (mSource->getDuration(&durationUs) == OK) {
2147 driver->notifyDuration(durationUs);
2148 }
Andreas Huberec0c5972013-02-05 14:47:13 -08002149 driver->notifyPrepareCompleted(err);
Andreas Huber9575c962013-02-05 13:59:56 -08002150 }
Andreas Huber99759402013-04-01 14:28:31 -07002151
Andreas Huber9575c962013-02-05 13:59:56 -08002152 break;
2153 }
2154
2155 case Source::kWhatFlagsChanged:
2156 {
2157 uint32_t flags;
2158 CHECK(msg->findInt32("flags", (int32_t *)&flags));
2159
Chong Zhang4b7069d2013-09-11 12:52:43 -07002160 sp<NuPlayerDriver> driver = mDriver.promote();
2161 if (driver != NULL) {
Wei Jia895651b2014-12-10 17:31:52 -08002162 if ((flags & NuPlayer::Source::FLAG_CAN_SEEK) == 0) {
2163 driver->notifyListener(
2164 MEDIA_INFO, MEDIA_INFO_NOT_SEEKABLE, 0);
2165 }
Chong Zhang4b7069d2013-09-11 12:52:43 -07002166 driver->notifyFlagsChanged(flags);
2167 }
2168
Andreas Huber9575c962013-02-05 13:59:56 -08002169 if ((mSourceFlags & Source::FLAG_DYNAMIC_DURATION)
2170 && (!(flags & Source::FLAG_DYNAMIC_DURATION))) {
2171 cancelPollDuration();
2172 } else if (!(mSourceFlags & Source::FLAG_DYNAMIC_DURATION)
2173 && (flags & Source::FLAG_DYNAMIC_DURATION)
2174 && (mAudioDecoder != NULL || mVideoDecoder != NULL)) {
2175 schedulePollDuration();
2176 }
2177
2178 mSourceFlags = flags;
2179 break;
2180 }
2181
2182 case Source::kWhatVideoSizeChanged:
2183 {
Chong Zhangced1c2f2014-08-08 15:22:35 -07002184 sp<AMessage> format;
2185 CHECK(msg->findMessage("format", &format));
Andreas Huber9575c962013-02-05 13:59:56 -08002186
Chong Zhangced1c2f2014-08-08 15:22:35 -07002187 updateVideoSize(format);
Andreas Huber9575c962013-02-05 13:59:56 -08002188 break;
2189 }
2190
Chong Zhang2a3cc9a2014-08-21 17:48:26 -07002191 case Source::kWhatBufferingUpdate:
2192 {
2193 int32_t percentage;
2194 CHECK(msg->findInt32("percentage", &percentage));
2195
2196 notifyListener(MEDIA_BUFFERING_UPDATE, percentage, 0);
2197 break;
2198 }
2199
Chong Zhangefbb6192015-01-30 17:13:27 -08002200 case Source::kWhatPauseOnBufferingStart:
2201 {
2202 // ignore if not playing
Chong Zhang8a048332015-05-06 15:16:28 -07002203 if (mStarted) {
Chong Zhangefbb6192015-01-30 17:13:27 -08002204 ALOGI("buffer low, pausing...");
2205
Chong Zhang8a048332015-05-06 15:16:28 -07002206 mPausedForBuffering = true;
Chong Zhangefbb6192015-01-30 17:13:27 -08002207 onPause();
2208 }
Wei Jiabfe82072016-05-20 09:21:50 -07002209 notifyListener(MEDIA_INFO, MEDIA_INFO_BUFFERING_START, 0);
Roger Jönssonb50e83e2013-01-21 16:26:41 +01002210 break;
2211 }
2212
Chong Zhangefbb6192015-01-30 17:13:27 -08002213 case Source::kWhatResumeOnBufferingEnd:
2214 {
2215 // ignore if not playing
Chong Zhang8a048332015-05-06 15:16:28 -07002216 if (mStarted) {
Chong Zhangefbb6192015-01-30 17:13:27 -08002217 ALOGI("buffer ready, resuming...");
2218
Chong Zhang8a048332015-05-06 15:16:28 -07002219 mPausedForBuffering = false;
2220
2221 // do not resume yet if client didn't unpause
2222 if (!mPausedByClient) {
2223 onResume();
2224 }
Chong Zhangefbb6192015-01-30 17:13:27 -08002225 }
Wei Jia3bed45a2016-02-17 11:06:47 -08002226 notifyListener(MEDIA_INFO, MEDIA_INFO_BUFFERING_END, 0);
Roger Jönssonb50e83e2013-01-21 16:26:41 +01002227 break;
2228 }
2229
Chong Zhangefbb6192015-01-30 17:13:27 -08002230 case Source::kWhatCacheStats:
2231 {
2232 int32_t kbps;
2233 CHECK(msg->findInt32("bandwidth", &kbps));
2234
2235 notifyListener(MEDIA_INFO, MEDIA_INFO_NETWORK_BANDWIDTH, kbps);
2236 break;
2237 }
2238
Chong Zhangdcb89b32013-08-06 09:44:47 -07002239 case Source::kWhatSubtitleData:
2240 {
2241 sp<ABuffer> buffer;
2242 CHECK(msg->findBuffer("buffer", &buffer));
2243
Chong Zhang404fced2014-06-11 14:45:31 -07002244 sendSubtitleData(buffer, 0 /* baseIndex */);
Chong Zhangdcb89b32013-08-06 09:44:47 -07002245 break;
2246 }
2247
Robert Shih08528432015-04-08 09:06:54 -07002248 case Source::kWhatTimedMetaData:
2249 {
2250 sp<ABuffer> buffer;
2251 if (!msg->findBuffer("buffer", &buffer)) {
2252 notifyListener(MEDIA_INFO, MEDIA_INFO_METADATA_UPDATE, 0);
2253 } else {
2254 sendTimedMetaData(buffer);
2255 }
2256 break;
2257 }
2258
Robert Shihd3b0bbb2014-07-23 15:00:25 -07002259 case Source::kWhatTimedTextData:
2260 {
2261 int32_t generation;
2262 if (msg->findInt32("generation", &generation)
2263 && generation != mTimedTextGeneration) {
2264 break;
2265 }
2266
2267 sp<ABuffer> buffer;
2268 CHECK(msg->findBuffer("buffer", &buffer));
2269
2270 sp<NuPlayerDriver> driver = mDriver.promote();
2271 if (driver == NULL) {
2272 break;
2273 }
2274
2275 int posMs;
2276 int64_t timeUs, posUs;
2277 driver->getCurrentPosition(&posMs);
Patrik2 Carlsson24d484b2015-01-27 16:49:45 +01002278 posUs = (int64_t) posMs * 1000ll;
Robert Shihd3b0bbb2014-07-23 15:00:25 -07002279 CHECK(buffer->meta()->findInt64("timeUs", &timeUs));
2280
2281 if (posUs < timeUs) {
2282 if (!msg->findInt32("generation", &generation)) {
2283 msg->setInt32("generation", mTimedTextGeneration);
2284 }
2285 msg->post(timeUs - posUs);
2286 } else {
2287 sendTimedTextData(buffer);
2288 }
2289 break;
2290 }
2291
Andreas Huber14f76722013-01-15 09:04:18 -08002292 case Source::kWhatQueueDecoderShutdown:
2293 {
2294 int32_t audio, video;
2295 CHECK(msg->findInt32("audio", &audio));
2296 CHECK(msg->findInt32("video", &video));
2297
2298 sp<AMessage> reply;
2299 CHECK(msg->findMessage("reply", &reply));
2300
2301 queueDecoderShutdown(audio, video, reply);
2302 break;
2303 }
2304
Ronghua Wu80276872014-08-28 15:50:29 -07002305 case Source::kWhatDrmNoLicense:
2306 {
2307 notifyListener(MEDIA_ERROR, MEDIA_ERROR_UNKNOWN, ERROR_DRM_NO_LICENSE);
2308 break;
2309 }
2310
Andreas Huber9575c962013-02-05 13:59:56 -08002311 default:
2312 TRESPASS();
2313 }
2314}
2315
Chong Zhanga7fa1d92014-06-11 14:49:23 -07002316void NuPlayer::onClosedCaptionNotify(const sp<AMessage> &msg) {
2317 int32_t what;
2318 CHECK(msg->findInt32("what", &what));
2319
2320 switch (what) {
2321 case NuPlayer::CCDecoder::kWhatClosedCaptionData:
2322 {
2323 sp<ABuffer> buffer;
2324 CHECK(msg->findBuffer("buffer", &buffer));
2325
2326 size_t inbandTracks = 0;
2327 if (mSource != NULL) {
2328 inbandTracks = mSource->getTrackCount();
2329 }
2330
2331 sendSubtitleData(buffer, inbandTracks);
2332 break;
2333 }
2334
2335 case NuPlayer::CCDecoder::kWhatTrackAdded:
2336 {
2337 notifyListener(MEDIA_INFO, MEDIA_INFO_METADATA_UPDATE, 0);
2338
2339 break;
2340 }
2341
2342 default:
2343 TRESPASS();
2344 }
2345
2346
2347}
2348
Chong Zhang404fced2014-06-11 14:45:31 -07002349void NuPlayer::sendSubtitleData(const sp<ABuffer> &buffer, int32_t baseIndex) {
2350 int32_t trackIndex;
2351 int64_t timeUs, durationUs;
2352 CHECK(buffer->meta()->findInt32("trackIndex", &trackIndex));
2353 CHECK(buffer->meta()->findInt64("timeUs", &timeUs));
2354 CHECK(buffer->meta()->findInt64("durationUs", &durationUs));
2355
2356 Parcel in;
2357 in.writeInt32(trackIndex + baseIndex);
2358 in.writeInt64(timeUs);
2359 in.writeInt64(durationUs);
2360 in.writeInt32(buffer->size());
2361 in.writeInt32(buffer->size());
2362 in.write(buffer->data(), buffer->size());
2363
2364 notifyListener(MEDIA_SUBTITLE_DATA, 0, 0, &in);
2365}
Robert Shihd3b0bbb2014-07-23 15:00:25 -07002366
Robert Shih08528432015-04-08 09:06:54 -07002367void NuPlayer::sendTimedMetaData(const sp<ABuffer> &buffer) {
2368 int64_t timeUs;
2369 CHECK(buffer->meta()->findInt64("timeUs", &timeUs));
2370
2371 Parcel in;
2372 in.writeInt64(timeUs);
2373 in.writeInt32(buffer->size());
2374 in.writeInt32(buffer->size());
2375 in.write(buffer->data(), buffer->size());
2376
2377 notifyListener(MEDIA_META_DATA, 0, 0, &in);
2378}
2379
Robert Shihd3b0bbb2014-07-23 15:00:25 -07002380void NuPlayer::sendTimedTextData(const sp<ABuffer> &buffer) {
2381 const void *data;
2382 size_t size = 0;
2383 int64_t timeUs;
Marco Nelissen55e2f4c2015-09-04 15:57:15 -07002384 int32_t flag = TextDescriptions::IN_BAND_TEXT_3GPP;
Robert Shihd3b0bbb2014-07-23 15:00:25 -07002385
2386 AString mime;
2387 CHECK(buffer->meta()->findString("mime", &mime));
2388 CHECK(strcasecmp(mime.c_str(), MEDIA_MIMETYPE_TEXT_3GPP) == 0);
2389
2390 data = buffer->data();
2391 size = buffer->size();
2392
2393 Parcel parcel;
2394 if (size > 0) {
2395 CHECK(buffer->meta()->findInt64("timeUs", &timeUs));
Marco Nelissen55e2f4c2015-09-04 15:57:15 -07002396 int32_t global = 0;
2397 if (buffer->meta()->findInt32("global", &global) && global) {
2398 flag |= TextDescriptions::GLOBAL_DESCRIPTIONS;
2399 } else {
2400 flag |= TextDescriptions::LOCAL_DESCRIPTIONS;
2401 }
Robert Shihd3b0bbb2014-07-23 15:00:25 -07002402 TextDescriptions::getParcelOfDescriptions(
2403 (const uint8_t *)data, size, flag, timeUs / 1000, &parcel);
2404 }
2405
2406 if ((parcel.dataSize() > 0)) {
2407 notifyListener(MEDIA_TIMED_TEXT, 0, 0, &parcel);
2408 } else { // send an empty timed text
2409 notifyListener(MEDIA_TIMED_TEXT, 0, 0);
2410 }
2411}
Andreas Huberb5f25f02013-02-05 10:14:26 -08002412////////////////////////////////////////////////////////////////////////////////
2413
Chong Zhangced1c2f2014-08-08 15:22:35 -07002414sp<AMessage> NuPlayer::Source::getFormat(bool audio) {
2415 sp<MetaData> meta = getFormatMeta(audio);
2416
2417 if (meta == NULL) {
2418 return NULL;
2419 }
2420
2421 sp<AMessage> msg = new AMessage;
2422
2423 if(convertMetaDataToMessage(meta, &msg) == OK) {
2424 return msg;
2425 }
2426 return NULL;
2427}
2428
Andreas Huber9575c962013-02-05 13:59:56 -08002429void NuPlayer::Source::notifyFlagsChanged(uint32_t flags) {
2430 sp<AMessage> notify = dupNotify();
2431 notify->setInt32("what", kWhatFlagsChanged);
2432 notify->setInt32("flags", flags);
2433 notify->post();
2434}
2435
Chong Zhangced1c2f2014-08-08 15:22:35 -07002436void NuPlayer::Source::notifyVideoSizeChanged(const sp<AMessage> &format) {
Andreas Huber9575c962013-02-05 13:59:56 -08002437 sp<AMessage> notify = dupNotify();
2438 notify->setInt32("what", kWhatVideoSizeChanged);
Chong Zhangced1c2f2014-08-08 15:22:35 -07002439 notify->setMessage("format", format);
Andreas Huber9575c962013-02-05 13:59:56 -08002440 notify->post();
2441}
2442
Andreas Huberec0c5972013-02-05 14:47:13 -08002443void NuPlayer::Source::notifyPrepared(status_t err) {
Andreas Huber9575c962013-02-05 13:59:56 -08002444 sp<AMessage> notify = dupNotify();
2445 notify->setInt32("what", kWhatPrepared);
Andreas Huberec0c5972013-02-05 14:47:13 -08002446 notify->setInt32("err", err);
Andreas Huber9575c962013-02-05 13:59:56 -08002447 notify->post();
2448}
2449
Lajos Molnarfcd3e942015-03-31 10:06:48 -07002450void NuPlayer::Source::notifyInstantiateSecureDecoders(const sp<AMessage> &reply) {
2451 sp<AMessage> notify = dupNotify();
2452 notify->setInt32("what", kWhatInstantiateSecureDecoders);
2453 notify->setMessage("reply", reply);
2454 notify->post();
2455}
2456
Andreas Huber84333e02014-02-07 15:36:10 -08002457void NuPlayer::Source::onMessageReceived(const sp<AMessage> & /* msg */) {
Andreas Huberb5f25f02013-02-05 10:14:26 -08002458 TRESPASS();
2459}
2460
Andreas Huberf9334412010-12-15 15:17:42 -08002461} // namespace android