blob: 8f7ca1f80efcff28200cc31fa97dbccddedd127e [file] [log] [blame]
Andreas Huberf9334412010-12-15 15:17:42 -08001/*
2 * Copyright (C) 2010 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17//#define LOG_NDEBUG 0
18#define LOG_TAG "NuPlayer"
19#include <utils/Log.h>
20
21#include "NuPlayer.h"
Andreas Huber5bc087c2010-12-23 10:27:40 -080022
23#include "HTTPLiveSource.h"
Chong Zhang7137ec72014-11-12 16:41:05 -080024#include "NuPlayerCCDecoder.h"
Andreas Huberf9334412010-12-15 15:17:42 -080025#include "NuPlayerDecoder.h"
Chong Zhang7137ec72014-11-12 16:41:05 -080026#include "NuPlayerDecoderBase.h"
Wei Jiabc2fb722014-07-08 16:37:57 -070027#include "NuPlayerDecoderPassThrough.h"
Andreas Huber43c3e6c2011-01-05 12:17:08 -080028#include "NuPlayerDriver.h"
Andreas Huberf9334412010-12-15 15:17:42 -080029#include "NuPlayerRenderer.h"
Andreas Huber5bc087c2010-12-23 10:27:40 -080030#include "NuPlayerSource.h"
Andreas Huber2bfdd422011-10-11 15:24:07 -070031#include "RTSPSource.h"
Andreas Huber5bc087c2010-12-23 10:27:40 -080032#include "StreamingSource.h"
Andreas Huberafed0e12011-09-20 15:39:58 -070033#include "GenericSource.h"
Robert Shihd3b0bbb2014-07-23 15:00:25 -070034#include "TextDescriptions.h"
Andreas Huber5bc087c2010-12-23 10:27:40 -080035
36#include "ATSParser.h"
Andreas Huberf9334412010-12-15 15:17:42 -080037
Lajos Molnard9fd6312014-11-06 11:00:00 -080038#include <cutils/properties.h>
39
Lajos Molnar3a474aa2015-04-24 17:10:07 -070040#include <media/AudioResamplerPublic.h>
41#include <media/AVSyncSettings.h>
42
Andreas Huber3831a062010-12-21 10:22:33 -080043#include <media/stagefright/foundation/hexdump.h>
Andreas Huberf9334412010-12-15 15:17:42 -080044#include <media/stagefright/foundation/ABuffer.h>
45#include <media/stagefright/foundation/ADebug.h>
46#include <media/stagefright/foundation/AMessage.h>
Lajos Molnar09524832014-07-17 14:29:51 -070047#include <media/stagefright/MediaBuffer.h>
Andreas Huber3fe62152011-09-16 15:09:22 -070048#include <media/stagefright/MediaDefs.h>
Andreas Huberf9334412010-12-15 15:17:42 -080049#include <media/stagefright/MediaErrors.h>
50#include <media/stagefright/MetaData.h>
Lajos Molnar1de1e252015-04-30 18:18:34 -070051
Andy McFadden8ba01022012-12-18 09:46:54 -080052#include <gui/IGraphicBufferProducer.h>
Lajos Molnar1de1e252015-04-30 18:18:34 -070053#include <gui/Surface.h>
Andreas Huberf9334412010-12-15 15:17:42 -080054
Andreas Huber3fe62152011-09-16 15:09:22 -070055#include "avc_utils.h"
56
Andreas Huber84066782011-08-16 09:34:26 -070057#include "ESDS.h"
58#include <media/stagefright/Utils.h>
59
Andreas Huberf9334412010-12-15 15:17:42 -080060namespace android {
61
Andreas Hubera1f8ab02012-11-30 10:53:22 -080062struct NuPlayer::Action : public RefBase {
63 Action() {}
64
65 virtual void execute(NuPlayer *player) = 0;
66
67private:
68 DISALLOW_EVIL_CONSTRUCTORS(Action);
69};
70
71struct NuPlayer::SeekAction : public Action {
Wei Jia29840802015-05-15 17:11:38 -070072 SeekAction(int64_t seekTimeUs)
73 : mSeekTimeUs(seekTimeUs) {
Andreas Hubera1f8ab02012-11-30 10:53:22 -080074 }
75
76 virtual void execute(NuPlayer *player) {
Wei Jia29840802015-05-15 17:11:38 -070077 player->performSeek(mSeekTimeUs);
Andreas Hubera1f8ab02012-11-30 10:53:22 -080078 }
79
80private:
81 int64_t mSeekTimeUs;
82
83 DISALLOW_EVIL_CONSTRUCTORS(SeekAction);
84};
85
Chong Zhangf8d71772014-11-26 15:08:34 -080086struct NuPlayer::ResumeDecoderAction : public Action {
87 ResumeDecoderAction(bool needNotify)
88 : mNeedNotify(needNotify) {
89 }
90
91 virtual void execute(NuPlayer *player) {
92 player->performResumeDecoders(mNeedNotify);
93 }
94
95private:
96 bool mNeedNotify;
97
98 DISALLOW_EVIL_CONSTRUCTORS(ResumeDecoderAction);
99};
100
Andreas Huber57a339c2012-12-03 11:18:00 -0800101struct NuPlayer::SetSurfaceAction : public Action {
Lajos Molnar1de1e252015-04-30 18:18:34 -0700102 SetSurfaceAction(const sp<Surface> &surface)
103 : mSurface(surface) {
Andreas Huber57a339c2012-12-03 11:18:00 -0800104 }
105
106 virtual void execute(NuPlayer *player) {
Lajos Molnar1de1e252015-04-30 18:18:34 -0700107 player->performSetSurface(mSurface);
Andreas Huber57a339c2012-12-03 11:18:00 -0800108 }
109
110private:
Lajos Molnar1de1e252015-04-30 18:18:34 -0700111 sp<Surface> mSurface;
Andreas Huber57a339c2012-12-03 11:18:00 -0800112
113 DISALLOW_EVIL_CONSTRUCTORS(SetSurfaceAction);
114};
115
Wei Jiafef808d2014-10-31 17:57:05 -0700116struct NuPlayer::FlushDecoderAction : public Action {
117 FlushDecoderAction(FlushCommand audio, FlushCommand video)
Andreas Huber14f76722013-01-15 09:04:18 -0800118 : mAudio(audio),
119 mVideo(video) {
120 }
121
122 virtual void execute(NuPlayer *player) {
Wei Jiafef808d2014-10-31 17:57:05 -0700123 player->performDecoderFlush(mAudio, mVideo);
Andreas Huber14f76722013-01-15 09:04:18 -0800124 }
125
126private:
Wei Jiafef808d2014-10-31 17:57:05 -0700127 FlushCommand mAudio;
128 FlushCommand mVideo;
Andreas Huber14f76722013-01-15 09:04:18 -0800129
Wei Jiafef808d2014-10-31 17:57:05 -0700130 DISALLOW_EVIL_CONSTRUCTORS(FlushDecoderAction);
Andreas Huber14f76722013-01-15 09:04:18 -0800131};
132
133struct NuPlayer::PostMessageAction : public Action {
134 PostMessageAction(const sp<AMessage> &msg)
135 : mMessage(msg) {
136 }
137
138 virtual void execute(NuPlayer *) {
139 mMessage->post();
140 }
141
142private:
143 sp<AMessage> mMessage;
144
145 DISALLOW_EVIL_CONSTRUCTORS(PostMessageAction);
146};
147
Andreas Hubera1f8ab02012-11-30 10:53:22 -0800148// Use this if there's no state necessary to save in order to execute
149// the action.
150struct NuPlayer::SimpleAction : public Action {
151 typedef void (NuPlayer::*ActionFunc)();
152
153 SimpleAction(ActionFunc func)
154 : mFunc(func) {
155 }
156
157 virtual void execute(NuPlayer *player) {
158 (player->*mFunc)();
159 }
160
161private:
162 ActionFunc mFunc;
163
164 DISALLOW_EVIL_CONSTRUCTORS(SimpleAction);
165};
166
Andreas Huberf9334412010-12-15 15:17:42 -0800167////////////////////////////////////////////////////////////////////////////////
168
Ronghua Wu68845c12015-07-21 09:50:48 -0700169NuPlayer::NuPlayer(pid_t pid)
Andreas Huber9b80c2b2011-06-30 15:47:02 -0700170 : mUIDValid(false),
Ronghua Wu68845c12015-07-21 09:50:48 -0700171 mPID(pid),
Andreas Huber9575c962013-02-05 13:59:56 -0800172 mSourceFlags(0),
Wei Jiabc2fb722014-07-08 16:37:57 -0700173 mOffloadAudio(false),
Wei Jia88703c32014-08-06 11:24:07 -0700174 mAudioDecoderGeneration(0),
175 mVideoDecoderGeneration(0),
Wei Jia57568df2014-09-22 10:16:29 -0700176 mRendererGeneration(0),
Robert Shih1a5c8592015-08-04 18:07:44 -0700177 mPreviousSeekTimeUs(0),
Andreas Huber9b80c2b2011-06-30 15:47:02 -0700178 mAudioEOS(false),
Andreas Huberf9334412010-12-15 15:17:42 -0800179 mVideoEOS(false),
Andreas Huber5bc087c2010-12-23 10:27:40 -0800180 mScanSourcesPending(false),
Andreas Huber1aef2112011-01-04 14:01:29 -0800181 mScanSourcesGeneration(0),
Andreas Huberb7c8e912012-11-27 15:02:53 -0800182 mPollDurationGeneration(0),
Robert Shihd3b0bbb2014-07-23 15:00:25 -0700183 mTimedTextGeneration(0),
Andreas Huberf9334412010-12-15 15:17:42 -0800184 mFlushingAudio(NONE),
Andreas Huber1aef2112011-01-04 14:01:29 -0800185 mFlushingVideo(NONE),
Chong Zhangf8d71772014-11-26 15:08:34 -0800186 mResumePending(false),
Andreas Huber57a339c2012-12-03 11:18:00 -0800187 mVideoScalingMode(NATIVE_WINDOW_SCALING_MODE_SCALE_TO_WINDOW),
Lajos Molnar3a474aa2015-04-24 17:10:07 -0700188 mPlaybackSettings(AUDIO_PLAYBACK_RATE_DEFAULT),
189 mVideoFpsHint(-1.f),
Chong Zhangefbb6192015-01-30 17:13:27 -0800190 mStarted(false),
Ronghua Wu64c2d172015-10-07 16:52:19 -0700191 mResetting(false),
Robert Shih0c61a0d2015-07-06 15:09:10 -0700192 mSourceStarted(false),
Chong Zhangefbb6192015-01-30 17:13:27 -0800193 mPaused(false),
Wei Jia71c75e02016-02-04 09:40:47 -0800194 mPausedByClient(true),
195 mPendingBufferingFlag(PENDING_BUFFERING_FLAG_NONE),
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() {
Chong Zhang48296b72014-09-14 14:28:45 -0700403 if (mSource != NULL) {
404 // During a reset, the data source might be unresponsive already, we need to
405 // disconnect explicitly so that reads exit promptly.
406 // We can't queue the disconnect request to the looper, as it might be
407 // queued behind a stuck read and never gets processed.
408 // Doing a disconnect outside the looper to allows the pending reads to exit
409 // (either successfully or with error).
410 mSource->disconnect();
411 }
412
Lajos Molnar1d15ab52015-03-04 16:46:34 -0800413 (new AMessage(kWhatReset, this))->post();
Andreas Huber1aef2112011-01-04 14:01:29 -0800414}
415
Wei Jiae427abf2014-09-22 15:21:11 -0700416void NuPlayer::seekToAsync(int64_t seekTimeUs, bool needNotify) {
Lajos Molnar1d15ab52015-03-04 16:46:34 -0800417 sp<AMessage> msg = new AMessage(kWhatSeek, this);
Andreas Huber43c3e6c2011-01-05 12:17:08 -0800418 msg->setInt64("seekTimeUs", seekTimeUs);
Wei Jiae427abf2014-09-22 15:21:11 -0700419 msg->setInt32("needNotify", needNotify);
Andreas Huber43c3e6c2011-01-05 12:17:08 -0800420 msg->post();
421}
422
Andreas Huber53df1a42010-12-22 10:03:04 -0800423
Chong Zhang404fced2014-06-11 14:45:31 -0700424void NuPlayer::writeTrackInfo(
425 Parcel* reply, const sp<AMessage> format) const {
Marco Nelissenc367ca12015-09-15 09:51:59 -0700426 if (format == NULL) {
427 ALOGE("NULL format");
428 return;
429 }
Chong Zhang404fced2014-06-11 14:45:31 -0700430 int32_t trackType;
Marco Nelissen9436e482015-09-15 10:21:53 -0700431 if (!format->findInt32("type", &trackType)) {
Marco Nelissenc367ca12015-09-15 09:51:59 -0700432 ALOGE("no track type");
433 return;
434 }
Chong Zhang404fced2014-06-11 14:45:31 -0700435
Robert Shih755106e2015-04-30 14:36:45 -0700436 AString mime;
Robert Shih2e3a4252015-05-06 10:21:15 -0700437 if (!format->findString("mime", &mime)) {
438 // Java MediaPlayer only uses mimetype for subtitle and timedtext tracks.
439 // If we can't find the mimetype here it means that we wouldn't be needing
440 // the mimetype on the Java end. We still write a placeholder mime to keep the
441 // (de)serialization logic simple.
442 if (trackType == MEDIA_TRACK_TYPE_AUDIO) {
443 mime = "audio/";
444 } else if (trackType == MEDIA_TRACK_TYPE_VIDEO) {
445 mime = "video/";
446 } else {
Marco Nelissenc367ca12015-09-15 09:51:59 -0700447 ALOGE("unknown track type: %d", trackType);
448 return;
Robert Shih2e3a4252015-05-06 10:21:15 -0700449 }
450 }
Robert Shih755106e2015-04-30 14:36:45 -0700451
Chong Zhang404fced2014-06-11 14:45:31 -0700452 AString lang;
Marco Nelissen9436e482015-09-15 10:21:53 -0700453 if (!format->findString("language", &lang)) {
Marco Nelissenc367ca12015-09-15 09:51:59 -0700454 ALOGE("no language");
455 return;
456 }
Chong Zhang404fced2014-06-11 14:45:31 -0700457
458 reply->writeInt32(2); // write something non-zero
459 reply->writeInt32(trackType);
Robert Shih755106e2015-04-30 14:36:45 -0700460 reply->writeString16(String16(mime.c_str()));
Chong Zhang404fced2014-06-11 14:45:31 -0700461 reply->writeString16(String16(lang.c_str()));
462
463 if (trackType == MEDIA_TRACK_TYPE_SUBTITLE) {
Chong Zhang404fced2014-06-11 14:45:31 -0700464 int32_t isAuto, isDefault, isForced;
465 CHECK(format->findInt32("auto", &isAuto));
466 CHECK(format->findInt32("default", &isDefault));
467 CHECK(format->findInt32("forced", &isForced));
468
Chong Zhang404fced2014-06-11 14:45:31 -0700469 reply->writeInt32(isAuto);
470 reply->writeInt32(isDefault);
471 reply->writeInt32(isForced);
472 }
473}
474
Andreas Huberf9334412010-12-15 15:17:42 -0800475void NuPlayer::onMessageReceived(const sp<AMessage> &msg) {
476 switch (msg->what()) {
477 case kWhatSetDataSource:
478 {
Steve Block3856b092011-10-20 11:56:00 +0100479 ALOGV("kWhatSetDataSource");
Andreas Huberf9334412010-12-15 15:17:42 -0800480
481 CHECK(mSource == NULL);
482
Chong Zhang3de157d2014-08-05 20:54:44 -0700483 status_t err = OK;
Andreas Huber5bc087c2010-12-23 10:27:40 -0800484 sp<RefBase> obj;
485 CHECK(msg->findObject("source", &obj));
Chong Zhang3de157d2014-08-05 20:54:44 -0700486 if (obj != NULL) {
487 mSource = static_cast<Source *>(obj.get());
Chong Zhang3de157d2014-08-05 20:54:44 -0700488 } else {
489 err = UNKNOWN_ERROR;
490 }
Andreas Huber9575c962013-02-05 13:59:56 -0800491
492 CHECK(mDriver != NULL);
493 sp<NuPlayerDriver> driver = mDriver.promote();
494 if (driver != NULL) {
Chong Zhang3de157d2014-08-05 20:54:44 -0700495 driver->notifySetDataSourceCompleted(err);
Andreas Huber9575c962013-02-05 13:59:56 -0800496 }
497 break;
498 }
499
500 case kWhatPrepare:
501 {
502 mSource->prepareAsync();
Andreas Huberf9334412010-12-15 15:17:42 -0800503 break;
504 }
505
Chong Zhangdcb89b32013-08-06 09:44:47 -0700506 case kWhatGetTrackInfo:
507 {
Lajos Molnar3f274362015-03-05 14:35:41 -0800508 sp<AReplyToken> replyID;
Chong Zhangdcb89b32013-08-06 09:44:47 -0700509 CHECK(msg->senderAwaitsResponse(&replyID));
510
Chong Zhang404fced2014-06-11 14:45:31 -0700511 Parcel* reply;
512 CHECK(msg->findPointer("reply", (void**)&reply));
513
514 size_t inbandTracks = 0;
Chong Zhangdcb89b32013-08-06 09:44:47 -0700515 if (mSource != NULL) {
Chong Zhang404fced2014-06-11 14:45:31 -0700516 inbandTracks = mSource->getTrackCount();
517 }
518
Chong Zhanga7fa1d92014-06-11 14:49:23 -0700519 size_t ccTracks = 0;
520 if (mCCDecoder != NULL) {
521 ccTracks = mCCDecoder->getTrackCount();
522 }
523
Chong Zhang404fced2014-06-11 14:45:31 -0700524 // total track count
Chong Zhanga7fa1d92014-06-11 14:49:23 -0700525 reply->writeInt32(inbandTracks + ccTracks);
Chong Zhang404fced2014-06-11 14:45:31 -0700526
527 // write inband tracks
528 for (size_t i = 0; i < inbandTracks; ++i) {
529 writeTrackInfo(reply, mSource->getTrackInfo(i));
Chong Zhangdcb89b32013-08-06 09:44:47 -0700530 }
531
Chong Zhanga7fa1d92014-06-11 14:49:23 -0700532 // write CC track
533 for (size_t i = 0; i < ccTracks; ++i) {
534 writeTrackInfo(reply, mCCDecoder->getTrackInfo(i));
535 }
536
Chong Zhangdcb89b32013-08-06 09:44:47 -0700537 sp<AMessage> response = new AMessage;
Chong Zhangdcb89b32013-08-06 09:44:47 -0700538 response->postReply(replyID);
539 break;
540 }
541
Robert Shih7c4f0d72014-07-09 18:53:31 -0700542 case kWhatGetSelectedTrack:
543 {
544 status_t err = INVALID_OPERATION;
545 if (mSource != NULL) {
546 err = OK;
547
548 int32_t type32;
549 CHECK(msg->findInt32("type", (int32_t*)&type32));
550 media_track_type type = (media_track_type)type32;
551 ssize_t selectedTrack = mSource->getSelectedTrack(type);
552
553 Parcel* reply;
554 CHECK(msg->findPointer("reply", (void**)&reply));
555 reply->writeInt32(selectedTrack);
556 }
557
558 sp<AMessage> response = new AMessage;
559 response->setInt32("err", err);
560
Lajos Molnar3f274362015-03-05 14:35:41 -0800561 sp<AReplyToken> replyID;
Robert Shih7c4f0d72014-07-09 18:53:31 -0700562 CHECK(msg->senderAwaitsResponse(&replyID));
563 response->postReply(replyID);
564 break;
565 }
566
Chong Zhangdcb89b32013-08-06 09:44:47 -0700567 case kWhatSelectTrack:
568 {
Lajos Molnar3f274362015-03-05 14:35:41 -0800569 sp<AReplyToken> replyID;
Chong Zhangdcb89b32013-08-06 09:44:47 -0700570 CHECK(msg->senderAwaitsResponse(&replyID));
571
Chong Zhang404fced2014-06-11 14:45:31 -0700572 size_t trackIndex;
573 int32_t select;
Robert Shih6ffb1fd2014-10-29 16:24:32 -0700574 int64_t timeUs;
Chong Zhang404fced2014-06-11 14:45:31 -0700575 CHECK(msg->findSize("trackIndex", &trackIndex));
576 CHECK(msg->findInt32("select", &select));
Robert Shih6ffb1fd2014-10-29 16:24:32 -0700577 CHECK(msg->findInt64("timeUs", &timeUs));
Chong Zhang404fced2014-06-11 14:45:31 -0700578
Chong Zhangdcb89b32013-08-06 09:44:47 -0700579 status_t err = INVALID_OPERATION;
Chong Zhang404fced2014-06-11 14:45:31 -0700580
581 size_t inbandTracks = 0;
Chong Zhangdcb89b32013-08-06 09:44:47 -0700582 if (mSource != NULL) {
Chong Zhang404fced2014-06-11 14:45:31 -0700583 inbandTracks = mSource->getTrackCount();
584 }
Chong Zhanga7fa1d92014-06-11 14:49:23 -0700585 size_t ccTracks = 0;
586 if (mCCDecoder != NULL) {
587 ccTracks = mCCDecoder->getTrackCount();
588 }
Chong Zhang404fced2014-06-11 14:45:31 -0700589
590 if (trackIndex < inbandTracks) {
Robert Shih6ffb1fd2014-10-29 16:24:32 -0700591 err = mSource->selectTrack(trackIndex, select, timeUs);
Robert Shihd3b0bbb2014-07-23 15:00:25 -0700592
593 if (!select && err == OK) {
594 int32_t type;
595 sp<AMessage> info = mSource->getTrackInfo(trackIndex);
596 if (info != NULL
597 && info->findInt32("type", &type)
598 && type == MEDIA_TRACK_TYPE_TIMEDTEXT) {
599 ++mTimedTextGeneration;
600 }
601 }
Chong Zhanga7fa1d92014-06-11 14:49:23 -0700602 } else {
603 trackIndex -= inbandTracks;
604
605 if (trackIndex < ccTracks) {
606 err = mCCDecoder->selectTrack(trackIndex, select);
607 }
Chong Zhangdcb89b32013-08-06 09:44:47 -0700608 }
609
610 sp<AMessage> response = new AMessage;
611 response->setInt32("err", err);
612
613 response->postReply(replyID);
614 break;
615 }
616
Andreas Huberb7c8e912012-11-27 15:02:53 -0800617 case kWhatPollDuration:
618 {
619 int32_t generation;
620 CHECK(msg->findInt32("generation", &generation));
621
622 if (generation != mPollDurationGeneration) {
623 // stale
624 break;
625 }
626
627 int64_t durationUs;
628 if (mDriver != NULL && mSource->getDuration(&durationUs) == OK) {
629 sp<NuPlayerDriver> driver = mDriver.promote();
630 if (driver != NULL) {
631 driver->notifyDuration(durationUs);
632 }
633 }
634
635 msg->post(1000000ll); // poll again in a second.
636 break;
637 }
638
Lajos Molnar1de1e252015-04-30 18:18:34 -0700639 case kWhatSetVideoSurface:
Andreas Huberf9334412010-12-15 15:17:42 -0800640 {
Andreas Huberf9334412010-12-15 15:17:42 -0800641
642 sp<RefBase> obj;
Lajos Molnar1de1e252015-04-30 18:18:34 -0700643 CHECK(msg->findObject("surface", &obj));
644 sp<Surface> surface = static_cast<Surface *>(obj.get());
Lajos Molnara81c6222015-07-10 19:17:45 -0700645
646 ALOGD("onSetVideoSurface(%p, %s video decoder)",
647 surface.get(),
Wei Jia6b7d2ed2015-08-10 15:11:54 -0700648 (mSource != NULL && mStarted && mSource->getFormat(false /* audio */) != NULL
Lajos Molnara81c6222015-07-10 19:17:45 -0700649 && mVideoDecoder != NULL) ? "have" : "no");
650
Wei Jia6b7d2ed2015-08-10 15:11:54 -0700651 // Need to check mStarted before calling mSource->getFormat because NuPlayer might
652 // be in preparing state and it could take long time.
653 // When mStarted is true, mSource must have been set.
654 if (mSource == NULL || !mStarted || mSource->getFormat(false /* audio */) == NULL
Lajos Molnara81c6222015-07-10 19:17:45 -0700655 // NOTE: mVideoDecoder's mSurface is always non-null
656 || (mVideoDecoder != NULL && mVideoDecoder->setVideoSurface(surface) == OK)) {
Lajos Molnar1de1e252015-04-30 18:18:34 -0700657 performSetSurface(surface);
Wei Jiafef808d2014-10-31 17:57:05 -0700658 break;
659 }
660
661 mDeferredActions.push_back(
662 new FlushDecoderAction(FLUSH_CMD_FLUSH /* audio */,
663 FLUSH_CMD_SHUTDOWN /* video */));
664
Lajos Molnar1de1e252015-04-30 18:18:34 -0700665 mDeferredActions.push_back(new SetSurfaceAction(surface));
James Dong0d268a32012-08-31 12:18:27 -0700666
Wei Jiac6e58412015-07-10 18:04:55 -0700667 if (obj != NULL || mAudioDecoder != NULL) {
Wei Jiafef808d2014-10-31 17:57:05 -0700668 if (mStarted) {
Andy Hung73535852014-09-05 11:42:58 -0700669 // Issue a seek to refresh the video screen only if started otherwise
670 // the extractor may not yet be started and will assert.
671 // If the video decoder is not set (perhaps audio only in this case)
672 // do not perform a seek as it is not needed.
Ronghua Wua73d9e02014-10-08 15:13:29 -0700673 int64_t currentPositionUs = 0;
674 if (getCurrentPosition(&currentPositionUs) == OK) {
675 mDeferredActions.push_back(
Wei Jia29840802015-05-15 17:11:38 -0700676 new SeekAction(currentPositionUs));
Ronghua Wua73d9e02014-10-08 15:13:29 -0700677 }
Andy Hung73535852014-09-05 11:42:58 -0700678 }
Wei Jiaac428aa2014-09-02 19:01:34 -0700679
Andreas Huber57a339c2012-12-03 11:18:00 -0800680 // If there is a new surface texture, instantiate decoders
681 // again if possible.
682 mDeferredActions.push_back(
683 new SimpleAction(&NuPlayer::performScanSources));
684 }
685
Chong Zhangf8d71772014-11-26 15:08:34 -0800686 // After a flush without shutdown, decoder is paused.
687 // Don't resume it until source seek is done, otherwise it could
Chong Zhang7137ec72014-11-12 16:41:05 -0800688 // start pulling stale data too soon.
689 mDeferredActions.push_back(
Chong Zhangf8d71772014-11-26 15:08:34 -0800690 new ResumeDecoderAction(false /* needNotify */));
Chong Zhang7137ec72014-11-12 16:41:05 -0800691
Andreas Huber57a339c2012-12-03 11:18:00 -0800692 processDeferredActions();
Andreas Huberf9334412010-12-15 15:17:42 -0800693 break;
694 }
695
696 case kWhatSetAudioSink:
697 {
Steve Block3856b092011-10-20 11:56:00 +0100698 ALOGV("kWhatSetAudioSink");
Andreas Huberf9334412010-12-15 15:17:42 -0800699
700 sp<RefBase> obj;
701 CHECK(msg->findObject("sink", &obj));
702
703 mAudioSink = static_cast<MediaPlayerBase::AudioSink *>(obj.get());
704 break;
705 }
706
707 case kWhatStart:
708 {
Steve Block3856b092011-10-20 11:56:00 +0100709 ALOGV("kWhatStart");
Wei Jia94211742014-10-28 17:09:06 -0700710 if (mStarted) {
Chong Zhang8a048332015-05-06 15:16:28 -0700711 // do not resume yet if the source is still buffering
712 if (!mPausedForBuffering) {
713 onResume();
714 }
Wei Jia94211742014-10-28 17:09:06 -0700715 } else {
716 onStart();
Lajos Molnar09524832014-07-17 14:29:51 -0700717 }
Chong Zhangefbb6192015-01-30 17:13:27 -0800718 mPausedByClient = false;
Wei Jia71c75e02016-02-04 09:40:47 -0800719 if (mPendingBufferingFlag != PENDING_BUFFERING_FLAG_NONE) {
720 notifyListener(MEDIA_INFO, mPendingBufferingFlag, 0);
721 mPendingBufferingFlag = PENDING_BUFFERING_FLAG_NONE;
722 }
Andreas Huberf9334412010-12-15 15:17:42 -0800723 break;
724 }
725
Lajos Molnar3a474aa2015-04-24 17:10:07 -0700726 case kWhatConfigPlayback:
Wei Jia98160162015-02-04 17:01:11 -0800727 {
Lajos Molnar3a474aa2015-04-24 17:10:07 -0700728 sp<AReplyToken> replyID;
729 CHECK(msg->senderAwaitsResponse(&replyID));
730 AudioPlaybackRate rate /* sanitized */;
731 readFromAMessage(msg, &rate);
732 status_t err = OK;
Wei Jia98160162015-02-04 17:01:11 -0800733 if (mRenderer != NULL) {
Wei Jiabf70feb2016-02-19 15:47:16 -0800734 // AudioSink allows only 1.f and 0.f for offload mode.
735 // For other speed, switch to non-offload mode.
736 if (mOffloadAudio && ((rate.mSpeed != 0.f && rate.mSpeed != 1.f)
737 || rate.mPitch != 1.f)) {
738 int64_t currentPositionUs;
739 if (getCurrentPosition(&currentPositionUs) != OK) {
740 currentPositionUs = mPreviousSeekTimeUs;
741 }
742
743 // Set mPlaybackSettings so that the new audio decoder can
744 // be created correctly.
745 mPlaybackSettings = rate;
746 if (!mPaused) {
747 mRenderer->pause();
748 }
Wei Jia5031b2f2016-02-25 11:19:31 -0800749 restartAudio(
Wei Jiabf70feb2016-02-19 15:47:16 -0800750 currentPositionUs, true /* forceNonOffload */,
751 true /* needsToCreateAudioDecoder */);
752 if (!mPaused) {
753 mRenderer->resume();
754 }
755 }
756
Lajos Molnar3a474aa2015-04-24 17:10:07 -0700757 err = mRenderer->setPlaybackSettings(rate);
758 }
759 if (err == OK) {
760 if (rate.mSpeed == 0.f) {
761 onPause();
Wei Jia351ce872016-02-10 13:21:59 -0800762 mPausedByClient = true;
Lajos Molnar3a474aa2015-04-24 17:10:07 -0700763 // save all other settings (using non-paused speed)
764 // so we can restore them on start
765 AudioPlaybackRate newRate = rate;
766 newRate.mSpeed = mPlaybackSettings.mSpeed;
767 mPlaybackSettings = newRate;
768 } else { /* rate.mSpeed != 0.f */
769 onResume();
Wei Jia351ce872016-02-10 13:21:59 -0800770 mPausedByClient = false;
Lajos Molnar3a474aa2015-04-24 17:10:07 -0700771 mPlaybackSettings = rate;
772 }
Wei Jia98160162015-02-04 17:01:11 -0800773 }
Ronghua Wu8db88132015-04-22 13:51:35 -0700774
775 if (mVideoDecoder != NULL) {
Ronghua Wuc8a70d32015-04-29 16:26:34 -0700776 float rate = getFrameRate();
777 if (rate > 0) {
Ronghua Wu8db88132015-04-22 13:51:35 -0700778 sp<AMessage> params = new AMessage();
Lajos Molnar3a474aa2015-04-24 17:10:07 -0700779 params->setFloat("operating-rate", rate * mPlaybackSettings.mSpeed);
Ronghua Wu8db88132015-04-22 13:51:35 -0700780 mVideoDecoder->setParameters(params);
781 }
782 }
783
Lajos Molnar3a474aa2015-04-24 17:10:07 -0700784 sp<AMessage> response = new AMessage;
785 response->setInt32("err", err);
786 response->postReply(replyID);
787 break;
788 }
789
790 case kWhatGetPlaybackSettings:
791 {
792 sp<AReplyToken> replyID;
793 CHECK(msg->senderAwaitsResponse(&replyID));
794 AudioPlaybackRate rate = mPlaybackSettings;
795 status_t err = OK;
796 if (mRenderer != NULL) {
797 err = mRenderer->getPlaybackSettings(&rate);
798 }
799 if (err == OK) {
800 // get playback settings used by renderer, as it may be
801 // slightly off due to audiosink not taking small changes.
802 mPlaybackSettings = rate;
803 if (mPaused) {
804 rate.mSpeed = 0.f;
805 }
806 }
807 sp<AMessage> response = new AMessage;
808 if (err == OK) {
809 writeToAMessage(response, rate);
810 }
811 response->setInt32("err", err);
812 response->postReply(replyID);
813 break;
814 }
815
816 case kWhatConfigSync:
817 {
818 sp<AReplyToken> replyID;
819 CHECK(msg->senderAwaitsResponse(&replyID));
820
821 ALOGV("kWhatConfigSync");
822 AVSyncSettings sync;
823 float videoFpsHint;
824 readFromAMessage(msg, &sync, &videoFpsHint);
825 status_t err = OK;
826 if (mRenderer != NULL) {
827 err = mRenderer->setSyncSettings(sync, videoFpsHint);
828 }
829 if (err == OK) {
830 mSyncSettings = sync;
831 mVideoFpsHint = videoFpsHint;
832 }
833 sp<AMessage> response = new AMessage;
834 response->setInt32("err", err);
835 response->postReply(replyID);
836 break;
837 }
838
839 case kWhatGetSyncSettings:
840 {
841 sp<AReplyToken> replyID;
842 CHECK(msg->senderAwaitsResponse(&replyID));
843 AVSyncSettings sync = mSyncSettings;
844 float videoFps = mVideoFpsHint;
845 status_t err = OK;
846 if (mRenderer != NULL) {
847 err = mRenderer->getSyncSettings(&sync, &videoFps);
848 if (err == OK) {
849 mSyncSettings = sync;
850 mVideoFpsHint = videoFps;
851 }
852 }
853 sp<AMessage> response = new AMessage;
854 if (err == OK) {
855 writeToAMessage(response, sync, videoFps);
856 }
857 response->setInt32("err", err);
858 response->postReply(replyID);
Wei Jia98160162015-02-04 17:01:11 -0800859 break;
860 }
861
Andreas Huberf9334412010-12-15 15:17:42 -0800862 case kWhatScanSources:
863 {
Andreas Huber1aef2112011-01-04 14:01:29 -0800864 int32_t generation;
865 CHECK(msg->findInt32("generation", &generation));
866 if (generation != mScanSourcesGeneration) {
867 // Drop obsolete msg.
868 break;
869 }
870
Andreas Huber5bc087c2010-12-23 10:27:40 -0800871 mScanSourcesPending = false;
872
Steve Block3856b092011-10-20 11:56:00 +0100873 ALOGV("scanning sources haveAudio=%d, haveVideo=%d",
Andreas Huber43c3e6c2011-01-05 12:17:08 -0800874 mAudioDecoder != NULL, mVideoDecoder != NULL);
875
Andreas Huberb7c8e912012-11-27 15:02:53 -0800876 bool mHadAnySourcesBefore =
877 (mAudioDecoder != NULL) || (mVideoDecoder != NULL);
Robert Shih7350b052015-10-01 15:50:14 -0700878 bool rescan = false;
Andreas Huberb7c8e912012-11-27 15:02:53 -0800879
Andy Hung282a7e32014-08-14 15:56:34 -0700880 // initialize video before audio because successful initialization of
881 // video may change deep buffer mode of audio.
Lajos Molnar1de1e252015-04-30 18:18:34 -0700882 if (mSurface != NULL) {
Robert Shih7350b052015-10-01 15:50:14 -0700883 if (instantiateDecoder(false, &mVideoDecoder) == -EWOULDBLOCK) {
884 rescan = true;
885 }
Haynes Mathew George5d246ef2012-07-09 10:36:57 -0700886 }
Andreas Huberf9334412010-12-15 15:17:42 -0800887
Ronghua Wua10fd232014-11-06 16:15:20 -0800888 // Don't try to re-open audio sink if there's an existing decoder.
889 if (mAudioSink != NULL && mAudioDecoder == NULL) {
Robert Shih7350b052015-10-01 15:50:14 -0700890 if (instantiateDecoder(true, &mAudioDecoder) == -EWOULDBLOCK) {
891 rescan = true;
892 }
Andreas Huberf9334412010-12-15 15:17:42 -0800893 }
894
Andreas Huberb7c8e912012-11-27 15:02:53 -0800895 if (!mHadAnySourcesBefore
896 && (mAudioDecoder != NULL || mVideoDecoder != NULL)) {
897 // This is the first time we've found anything playable.
898
Andreas Huber9575c962013-02-05 13:59:56 -0800899 if (mSourceFlags & Source::FLAG_DYNAMIC_DURATION) {
Andreas Huberb7c8e912012-11-27 15:02:53 -0800900 schedulePollDuration();
901 }
902 }
903
Andreas Hubereac68ba2011-09-27 12:12:25 -0700904 status_t err;
905 if ((err = mSource->feedMoreTSData()) != OK) {
Andreas Huber1aef2112011-01-04 14:01:29 -0800906 if (mAudioDecoder == NULL && mVideoDecoder == NULL) {
907 // We're not currently decoding anything (no audio or
908 // video tracks found) and we just ran out of input data.
Andreas Hubereac68ba2011-09-27 12:12:25 -0700909
910 if (err == ERROR_END_OF_STREAM) {
911 notifyListener(MEDIA_PLAYBACK_COMPLETE, 0, 0);
912 } else {
913 notifyListener(MEDIA_ERROR, MEDIA_ERROR_UNKNOWN, err);
914 }
Andreas Huber1aef2112011-01-04 14:01:29 -0800915 }
Andreas Huberf9334412010-12-15 15:17:42 -0800916 break;
917 }
918
Robert Shih7350b052015-10-01 15:50:14 -0700919 if (rescan) {
Andreas Huberf9334412010-12-15 15:17:42 -0800920 msg->post(100000ll);
Andreas Huber5bc087c2010-12-23 10:27:40 -0800921 mScanSourcesPending = true;
Andreas Huberf9334412010-12-15 15:17:42 -0800922 }
923 break;
924 }
925
926 case kWhatVideoNotify:
927 case kWhatAudioNotify:
928 {
929 bool audio = msg->what() == kWhatAudioNotify;
930
Wei Jia88703c32014-08-06 11:24:07 -0700931 int32_t currentDecoderGeneration =
932 (audio? mAudioDecoderGeneration : mVideoDecoderGeneration);
933 int32_t requesterGeneration = currentDecoderGeneration - 1;
934 CHECK(msg->findInt32("generation", &requesterGeneration));
935
936 if (requesterGeneration != currentDecoderGeneration) {
937 ALOGV("got message from old %s decoder, generation(%d:%d)",
938 audio ? "audio" : "video", requesterGeneration,
939 currentDecoderGeneration);
940 sp<AMessage> reply;
941 if (!(msg->findMessage("reply", &reply))) {
942 return;
943 }
944
945 reply->setInt32("err", INFO_DISCONTINUITY);
946 reply->post();
947 return;
948 }
949
Andreas Huberf9334412010-12-15 15:17:42 -0800950 int32_t what;
Lajos Molnar1cd13982014-01-17 15:12:51 -0800951 CHECK(msg->findInt32("what", &what));
Andreas Huberf9334412010-12-15 15:17:42 -0800952
Chong Zhang7137ec72014-11-12 16:41:05 -0800953 if (what == DecoderBase::kWhatInputDiscontinuity) {
954 int32_t formatChange;
955 CHECK(msg->findInt32("formatChange", &formatChange));
Andreas Huberf9334412010-12-15 15:17:42 -0800956
Chong Zhang7137ec72014-11-12 16:41:05 -0800957 ALOGV("%s discontinuity: formatChange %d",
958 audio ? "audio" : "video", formatChange);
959
960 if (formatChange) {
961 mDeferredActions.push_back(
962 new FlushDecoderAction(
963 audio ? FLUSH_CMD_SHUTDOWN : FLUSH_CMD_NONE,
964 audio ? FLUSH_CMD_NONE : FLUSH_CMD_SHUTDOWN));
Andreas Huberf9334412010-12-15 15:17:42 -0800965 }
Chong Zhang7137ec72014-11-12 16:41:05 -0800966
967 mDeferredActions.push_back(
968 new SimpleAction(
969 &NuPlayer::performScanSources));
970
971 processDeferredActions();
972 } else if (what == DecoderBase::kWhatEOS) {
Andreas Huberdc9bacd2011-09-26 10:53:29 -0700973 int32_t err;
Lajos Molnar1cd13982014-01-17 15:12:51 -0800974 CHECK(msg->findInt32("err", &err));
Andreas Huberdc9bacd2011-09-26 10:53:29 -0700975
976 if (err == ERROR_END_OF_STREAM) {
Steve Block3856b092011-10-20 11:56:00 +0100977 ALOGV("got %s decoder EOS", audio ? "audio" : "video");
Andreas Huberdc9bacd2011-09-26 10:53:29 -0700978 } else {
Steve Block3856b092011-10-20 11:56:00 +0100979 ALOGV("got %s decoder EOS w/ error %d",
Andreas Huberdc9bacd2011-09-26 10:53:29 -0700980 audio ? "audio" : "video",
981 err);
982 }
983
984 mRenderer->queueEOS(audio, err);
Chong Zhang7137ec72014-11-12 16:41:05 -0800985 } else if (what == DecoderBase::kWhatFlushCompleted) {
Steve Block3856b092011-10-20 11:56:00 +0100986 ALOGV("decoder %s flush completed", audio ? "audio" : "video");
Andreas Huberf9334412010-12-15 15:17:42 -0800987
Andy Hung8d121d42014-10-03 09:53:53 -0700988 handleFlushComplete(audio, true /* isDecoder */);
Andreas Huber3831a062010-12-21 10:22:33 -0800989 finishFlushIfPossible();
Chong Zhang7137ec72014-11-12 16:41:05 -0800990 } else if (what == DecoderBase::kWhatVideoSizeChanged) {
Lajos Molnar1cd13982014-01-17 15:12:51 -0800991 sp<AMessage> format;
992 CHECK(msg->findMessage("format", &format));
993
Wei Jiac6cfd702014-11-11 16:33:20 -0800994 sp<AMessage> inputFormat =
995 mSource->getFormat(false /* audio */);
Andreas Huber3831a062010-12-21 10:22:33 -0800996
Wei Jiac6cfd702014-11-11 16:33:20 -0800997 updateVideoSize(inputFormat, format);
Chong Zhang7137ec72014-11-12 16:41:05 -0800998 } else if (what == DecoderBase::kWhatShutdownCompleted) {
Steve Block3856b092011-10-20 11:56:00 +0100999 ALOGV("%s shutdown completed", audio ? "audio" : "video");
Andreas Huber3831a062010-12-21 10:22:33 -08001000 if (audio) {
1001 mAudioDecoder.clear();
Wei Jia57568df2014-09-22 10:16:29 -07001002 ++mAudioDecoderGeneration;
Andreas Huber3831a062010-12-21 10:22:33 -08001003
1004 CHECK_EQ((int)mFlushingAudio, (int)SHUTTING_DOWN_DECODER);
1005 mFlushingAudio = SHUT_DOWN;
1006 } else {
1007 mVideoDecoder.clear();
Wei Jia57568df2014-09-22 10:16:29 -07001008 ++mVideoDecoderGeneration;
Andreas Huber3831a062010-12-21 10:22:33 -08001009
1010 CHECK_EQ((int)mFlushingVideo, (int)SHUTTING_DOWN_DECODER);
1011 mFlushingVideo = SHUT_DOWN;
1012 }
1013
1014 finishFlushIfPossible();
Chong Zhangf8d71772014-11-26 15:08:34 -08001015 } else if (what == DecoderBase::kWhatResumeCompleted) {
1016 finishResume();
Chong Zhang7137ec72014-11-12 16:41:05 -08001017 } else if (what == DecoderBase::kWhatError) {
Chong Zhangf4c0a942014-08-11 15:14:10 -07001018 status_t err;
Andy Hung2abde2c2014-09-30 14:40:32 -07001019 if (!msg->findInt32("err", &err) || err == OK) {
Chong Zhangf4c0a942014-08-11 15:14:10 -07001020 err = UNKNOWN_ERROR;
1021 }
Andy Hungcf31f1e2014-09-23 14:59:01 -07001022
Andy Hung2abde2c2014-09-30 14:40:32 -07001023 // Decoder errors can be due to Source (e.g. from streaming),
1024 // or from decoding corrupted bitstreams, or from other decoder
1025 // MediaCodec operations (e.g. from an ongoing reset or seek).
Andy Hung202bce12014-12-03 11:47:36 -08001026 // They may also be due to openAudioSink failure at
1027 // decoder start or after a format change.
Andy Hung2abde2c2014-09-30 14:40:32 -07001028 //
1029 // We try to gracefully shut down the affected decoder if possible,
1030 // rather than trying to force the shutdown with something
1031 // similar to performReset(). This method can lead to a hang
1032 // if MediaCodec functions block after an error, but they should
1033 // typically return INVALID_OPERATION instead of blocking.
1034
1035 FlushStatus *flushing = audio ? &mFlushingAudio : &mFlushingVideo;
1036 ALOGE("received error(%#x) from %s decoder, flushing(%d), now shutting down",
1037 err, audio ? "audio" : "video", *flushing);
1038
1039 switch (*flushing) {
1040 case NONE:
1041 mDeferredActions.push_back(
Wei Jiafef808d2014-10-31 17:57:05 -07001042 new FlushDecoderAction(
1043 audio ? FLUSH_CMD_SHUTDOWN : FLUSH_CMD_NONE,
1044 audio ? FLUSH_CMD_NONE : FLUSH_CMD_SHUTDOWN));
Andy Hung2abde2c2014-09-30 14:40:32 -07001045 processDeferredActions();
1046 break;
1047 case FLUSHING_DECODER:
1048 *flushing = FLUSHING_DECODER_SHUTDOWN; // initiate shutdown after flush.
1049 break; // Wait for flush to complete.
1050 case FLUSHING_DECODER_SHUTDOWN:
1051 break; // Wait for flush to complete.
1052 case SHUTTING_DOWN_DECODER:
1053 break; // Wait for shutdown to complete.
1054 case FLUSHED:
1055 // Widevine source reads must stop before releasing the video decoder.
1056 if (!audio && mSource != NULL && mSourceFlags & Source::FLAG_SECURE) {
1057 mSource->stop();
Robert Shih0c61a0d2015-07-06 15:09:10 -07001058 mSourceStarted = false;
Andy Hung2abde2c2014-09-30 14:40:32 -07001059 }
1060 getDecoder(audio)->initiateShutdown(); // In the middle of a seek.
1061 *flushing = SHUTTING_DOWN_DECODER; // Shut down.
1062 break;
1063 case SHUT_DOWN:
1064 finishFlushIfPossible(); // Should not occur.
1065 break; // Finish anyways.
Marco Nelissen9e2b7912014-08-18 16:13:03 -07001066 }
Andy Hung2abde2c2014-09-30 14:40:32 -07001067 notifyListener(MEDIA_ERROR, MEDIA_ERROR_UNKNOWN, err);
Lajos Molnar1cd13982014-01-17 15:12:51 -08001068 } else {
1069 ALOGV("Unhandled decoder notification %d '%c%c%c%c'.",
Andreas Hubera1f8ab02012-11-30 10:53:22 -08001070 what,
1071 what >> 24,
1072 (what >> 16) & 0xff,
1073 (what >> 8) & 0xff,
1074 what & 0xff);
Andreas Huberf9334412010-12-15 15:17:42 -08001075 }
1076
1077 break;
1078 }
1079
1080 case kWhatRendererNotify:
1081 {
Wei Jia57568df2014-09-22 10:16:29 -07001082 int32_t requesterGeneration = mRendererGeneration - 1;
1083 CHECK(msg->findInt32("generation", &requesterGeneration));
1084 if (requesterGeneration != mRendererGeneration) {
1085 ALOGV("got message from old renderer, generation(%d:%d)",
1086 requesterGeneration, mRendererGeneration);
1087 return;
1088 }
1089
Andreas Huberf9334412010-12-15 15:17:42 -08001090 int32_t what;
1091 CHECK(msg->findInt32("what", &what));
1092
1093 if (what == Renderer::kWhatEOS) {
1094 int32_t audio;
1095 CHECK(msg->findInt32("audio", &audio));
1096
Andreas Huberc92fd242011-08-16 13:48:44 -07001097 int32_t finalResult;
1098 CHECK(msg->findInt32("finalResult", &finalResult));
1099
Andreas Huberf9334412010-12-15 15:17:42 -08001100 if (audio) {
1101 mAudioEOS = true;
1102 } else {
1103 mVideoEOS = true;
1104 }
1105
Andreas Huberc92fd242011-08-16 13:48:44 -07001106 if (finalResult == ERROR_END_OF_STREAM) {
Steve Block3856b092011-10-20 11:56:00 +01001107 ALOGV("reached %s EOS", audio ? "audio" : "video");
Andreas Huberc92fd242011-08-16 13:48:44 -07001108 } else {
Steve Block29357bc2012-01-06 19:20:56 +00001109 ALOGE("%s track encountered an error (%d)",
Andreas Huberc92fd242011-08-16 13:48:44 -07001110 audio ? "audio" : "video", finalResult);
1111
1112 notifyListener(
1113 MEDIA_ERROR, MEDIA_ERROR_UNKNOWN, finalResult);
1114 }
Andreas Huberf9334412010-12-15 15:17:42 -08001115
1116 if ((mAudioEOS || mAudioDecoder == NULL)
1117 && (mVideoEOS || mVideoDecoder == NULL)) {
1118 notifyListener(MEDIA_PLAYBACK_COMPLETE, 0, 0);
1119 }
Andreas Huber3fe62152011-09-16 15:09:22 -07001120 } else if (what == Renderer::kWhatFlushComplete) {
Andreas Huberf9334412010-12-15 15:17:42 -08001121 int32_t audio;
1122 CHECK(msg->findInt32("audio", &audio));
1123
Wei Jia3261f0d2015-10-08 13:58:33 -07001124 if (audio) {
1125 mAudioEOS = false;
1126 } else {
1127 mVideoEOS = false;
1128 }
1129
Steve Block3856b092011-10-20 11:56:00 +01001130 ALOGV("renderer %s flush completed.", audio ? "audio" : "video");
Wei Jiada4252f2015-07-14 18:15:28 -07001131 if (audio && (mFlushingAudio == NONE || mFlushingAudio == FLUSHED
1132 || mFlushingAudio == SHUT_DOWN)) {
1133 // Flush has been handled by tear down.
1134 break;
1135 }
Andy Hung8d121d42014-10-03 09:53:53 -07001136 handleFlushComplete(audio, false /* isDecoder */);
1137 finishFlushIfPossible();
James Dongf57b4ea2012-07-20 13:38:36 -07001138 } else if (what == Renderer::kWhatVideoRenderingStart) {
1139 notifyListener(MEDIA_INFO, MEDIA_INFO_RENDERING_START, 0);
Lajos Molnarcbaffcf2013-08-14 18:30:38 -07001140 } else if (what == Renderer::kWhatMediaRenderingStart) {
1141 ALOGV("media rendering started");
1142 notifyListener(MEDIA_STARTED, 0, 0);
Ronghua Wufaeb0f22015-05-21 12:20:21 -07001143 } else if (what == Renderer::kWhatAudioTearDown) {
Ronghua Wu08529172014-10-02 16:55:52 -07001144 int32_t reason;
1145 CHECK(msg->findInt32("reason", &reason));
Ronghua Wufaeb0f22015-05-21 12:20:21 -07001146 ALOGV("Tear down audio with reason %d.", reason);
Wei Jia8456ddd2016-04-22 14:46:43 -07001147 if (reason == Renderer::kDueToTimeout && !(mPaused && mOffloadAudio)) {
1148 // TimeoutWhenPaused is only for offload mode.
1149 ALOGW("Receive a stale message for teardown.");
1150 break;
1151 }
Wei Jiada4252f2015-07-14 18:15:28 -07001152 int64_t positionUs;
Robert Shih1a5c8592015-08-04 18:07:44 -07001153 if (!msg->findInt64("positionUs", &positionUs)) {
1154 positionUs = mPreviousSeekTimeUs;
1155 }
Ronghua Wufaeb0f22015-05-21 12:20:21 -07001156
Wei Jia5031b2f2016-02-25 11:19:31 -08001157 restartAudio(
Wei Jiaa05f1e32016-03-25 16:31:22 -07001158 positionUs, reason == Renderer::kForceNonOffload /* forceNonOffload */,
1159 reason != Renderer::kDueToTimeout /* needsToCreateAudioDecoder */);
Andreas Huberf9334412010-12-15 15:17:42 -08001160 }
1161 break;
1162 }
1163
1164 case kWhatMoreDataQueued:
1165 {
1166 break;
1167 }
1168
Andreas Huber1aef2112011-01-04 14:01:29 -08001169 case kWhatReset:
1170 {
Steve Block3856b092011-10-20 11:56:00 +01001171 ALOGV("kWhatReset");
Andreas Huber1aef2112011-01-04 14:01:29 -08001172
Ronghua Wu64c2d172015-10-07 16:52:19 -07001173 mResetting = true;
1174
Andreas Hubera1f8ab02012-11-30 10:53:22 -08001175 mDeferredActions.push_back(
Wei Jiafef808d2014-10-31 17:57:05 -07001176 new FlushDecoderAction(
1177 FLUSH_CMD_SHUTDOWN /* audio */,
1178 FLUSH_CMD_SHUTDOWN /* video */));
Andreas Huberb7c8e912012-11-27 15:02:53 -08001179
Andreas Hubera1f8ab02012-11-30 10:53:22 -08001180 mDeferredActions.push_back(
1181 new SimpleAction(&NuPlayer::performReset));
Andreas Huberb58ce9f2011-11-28 16:27:35 -08001182
Andreas Hubera1f8ab02012-11-30 10:53:22 -08001183 processDeferredActions();
Andreas Huber1aef2112011-01-04 14:01:29 -08001184 break;
1185 }
1186
Andreas Huber43c3e6c2011-01-05 12:17:08 -08001187 case kWhatSeek:
1188 {
1189 int64_t seekTimeUs;
Wei Jiae427abf2014-09-22 15:21:11 -07001190 int32_t needNotify;
Andreas Huber43c3e6c2011-01-05 12:17:08 -08001191 CHECK(msg->findInt64("seekTimeUs", &seekTimeUs));
Wei Jiae427abf2014-09-22 15:21:11 -07001192 CHECK(msg->findInt32("needNotify", &needNotify));
Andreas Huber43c3e6c2011-01-05 12:17:08 -08001193
Wei Jiae427abf2014-09-22 15:21:11 -07001194 ALOGV("kWhatSeek seekTimeUs=%lld us, needNotify=%d",
Lajos Molnar6d339f12015-04-17 16:15:53 -07001195 (long long)seekTimeUs, needNotify);
Andreas Huber43c3e6c2011-01-05 12:17:08 -08001196
Wei Jia1061c9c2015-05-19 16:02:17 -07001197 if (!mStarted) {
1198 // Seek before the player is started. In order to preview video,
1199 // need to start the player and pause it. This branch is called
1200 // only once if needed. After the player is started, any seek
1201 // operation will go through normal path.
Robert Shih0c61a0d2015-07-06 15:09:10 -07001202 // Audio-only cases are handled separately.
Wei Jia1061c9c2015-05-19 16:02:17 -07001203 onStart(seekTimeUs);
Robert Shih0c61a0d2015-07-06 15:09:10 -07001204 if (mStarted) {
1205 onPause();
1206 mPausedByClient = true;
1207 }
Wei Jia1061c9c2015-05-19 16:02:17 -07001208 if (needNotify) {
1209 notifyDriverSeekComplete();
1210 }
1211 break;
1212 }
1213
Wei Jia351ce872016-02-10 13:21:59 -08001214 mPendingBufferingFlag = PENDING_BUFFERING_FLAG_NONE;
1215
Andreas Hubera1f8ab02012-11-30 10:53:22 -08001216 mDeferredActions.push_back(
Wei Jiafef808d2014-10-31 17:57:05 -07001217 new FlushDecoderAction(FLUSH_CMD_FLUSH /* audio */,
1218 FLUSH_CMD_FLUSH /* video */));
Andreas Huber43c3e6c2011-01-05 12:17:08 -08001219
Wei Jiae427abf2014-09-22 15:21:11 -07001220 mDeferredActions.push_back(
Wei Jia29840802015-05-15 17:11:38 -07001221 new SeekAction(seekTimeUs));
Andreas Huber43c3e6c2011-01-05 12:17:08 -08001222
Chong Zhangf8d71772014-11-26 15:08:34 -08001223 // After a flush without shutdown, decoder is paused.
1224 // Don't resume it until source seek is done, otherwise it could
Chong Zhang7137ec72014-11-12 16:41:05 -08001225 // start pulling stale data too soon.
1226 mDeferredActions.push_back(
Chong Zhangf8d71772014-11-26 15:08:34 -08001227 new ResumeDecoderAction(needNotify));
Chong Zhang7137ec72014-11-12 16:41:05 -08001228
Andreas Hubera1f8ab02012-11-30 10:53:22 -08001229 processDeferredActions();
Andreas Huber43c3e6c2011-01-05 12:17:08 -08001230 break;
1231 }
1232
Andreas Huberb4082222011-01-20 15:23:04 -08001233 case kWhatPause:
1234 {
Chong Zhangefbb6192015-01-30 17:13:27 -08001235 onPause();
1236 mPausedByClient = true;
Andreas Huberb4082222011-01-20 15:23:04 -08001237 break;
1238 }
1239
Andreas Huberb5f25f02013-02-05 10:14:26 -08001240 case kWhatSourceNotify:
1241 {
Andreas Huber9575c962013-02-05 13:59:56 -08001242 onSourceNotify(msg);
Andreas Huberb5f25f02013-02-05 10:14:26 -08001243 break;
1244 }
1245
Chong Zhanga7fa1d92014-06-11 14:49:23 -07001246 case kWhatClosedCaptionNotify:
1247 {
1248 onClosedCaptionNotify(msg);
1249 break;
1250 }
1251
Andreas Huberf9334412010-12-15 15:17:42 -08001252 default:
1253 TRESPASS();
1254 break;
1255 }
1256}
1257
Wei Jia94211742014-10-28 17:09:06 -07001258void NuPlayer::onResume() {
Ronghua Wu64c2d172015-10-07 16:52:19 -07001259 if (!mPaused || mResetting) {
1260 ALOGD_IF(mResetting, "resetting, onResume discarded");
Chong Zhangefbb6192015-01-30 17:13:27 -08001261 return;
1262 }
1263 mPaused = false;
Wei Jia94211742014-10-28 17:09:06 -07001264 if (mSource != NULL) {
1265 mSource->resume();
1266 } else {
1267 ALOGW("resume called when source is gone or not set");
1268 }
1269 // |mAudioDecoder| may have been released due to the pause timeout, so re-create it if
1270 // needed.
1271 if (audioDecoderStillNeeded() && mAudioDecoder == NULL) {
1272 instantiateDecoder(true /* audio */, &mAudioDecoder);
1273 }
1274 if (mRenderer != NULL) {
1275 mRenderer->resume();
1276 } else {
1277 ALOGW("resume called when renderer is gone or not set");
1278 }
1279}
1280
Lajos Molnarfcd3e942015-03-31 10:06:48 -07001281status_t NuPlayer::onInstantiateSecureDecoders() {
1282 status_t err;
1283 if (!(mSourceFlags & Source::FLAG_SECURE)) {
1284 return BAD_TYPE;
1285 }
1286
1287 if (mRenderer != NULL) {
1288 ALOGE("renderer should not be set when instantiating secure decoders");
1289 return UNKNOWN_ERROR;
1290 }
1291
1292 // TRICKY: We rely on mRenderer being null, so that decoder does not start requesting
1293 // data on instantiation.
Lajos Molnar1de1e252015-04-30 18:18:34 -07001294 if (mSurface != NULL) {
Lajos Molnarfcd3e942015-03-31 10:06:48 -07001295 err = instantiateDecoder(false, &mVideoDecoder);
1296 if (err != OK) {
1297 return err;
1298 }
1299 }
1300
1301 if (mAudioSink != NULL) {
1302 err = instantiateDecoder(true, &mAudioDecoder);
1303 if (err != OK) {
1304 return err;
1305 }
1306 }
1307 return OK;
1308}
1309
Wei Jia1061c9c2015-05-19 16:02:17 -07001310void NuPlayer::onStart(int64_t startPositionUs) {
Robert Shih0c61a0d2015-07-06 15:09:10 -07001311 if (!mSourceStarted) {
1312 mSourceStarted = true;
1313 mSource->start();
1314 }
1315 if (startPositionUs > 0) {
1316 performSeek(startPositionUs);
1317 if (mSource->getFormat(false /* audio */) == NULL) {
1318 return;
1319 }
1320 }
1321
Wei Jia94211742014-10-28 17:09:06 -07001322 mOffloadAudio = false;
1323 mAudioEOS = false;
1324 mVideoEOS = false;
Wei Jia94211742014-10-28 17:09:06 -07001325 mStarted = true;
1326
Wei Jia94211742014-10-28 17:09:06 -07001327 uint32_t flags = 0;
1328
1329 if (mSource->isRealTime()) {
1330 flags |= Renderer::FLAG_REAL_TIME;
1331 }
1332
1333 sp<MetaData> audioMeta = mSource->getFormatMeta(true /* audio */);
Andy Hung12a84132015-10-20 16:09:21 -07001334 ALOGV_IF(audioMeta == NULL, "no metadata for audio source"); // video only stream
Wei Jia94211742014-10-28 17:09:06 -07001335 audio_stream_type_t streamType = AUDIO_STREAM_MUSIC;
1336 if (mAudioSink != NULL) {
1337 streamType = mAudioSink->getAudioStreamType();
1338 }
1339
1340 sp<AMessage> videoFormat = mSource->getFormat(false /* audio */);
1341
1342 mOffloadAudio =
Wei Jiabf70feb2016-02-19 15:47:16 -08001343 canOffloadStream(audioMeta, (videoFormat != NULL), mSource->isStreaming(), streamType)
1344 && (mPlaybackSettings.mSpeed == 1.f && mPlaybackSettings.mPitch == 1.f);
Wei Jia94211742014-10-28 17:09:06 -07001345 if (mOffloadAudio) {
1346 flags |= Renderer::FLAG_OFFLOAD_AUDIO;
1347 }
1348
Lajos Molnar1d15ab52015-03-04 16:46:34 -08001349 sp<AMessage> notify = new AMessage(kWhatRendererNotify, this);
Wei Jia94211742014-10-28 17:09:06 -07001350 ++mRendererGeneration;
1351 notify->setInt32("generation", mRendererGeneration);
1352 mRenderer = new Renderer(mAudioSink, notify, flags);
Wei Jia94211742014-10-28 17:09:06 -07001353 mRendererLooper = new ALooper;
1354 mRendererLooper->setName("NuPlayerRenderer");
1355 mRendererLooper->start(false, false, ANDROID_PRIORITY_AUDIO);
1356 mRendererLooper->registerHandler(mRenderer);
Lajos Molnar3a474aa2015-04-24 17:10:07 -07001357
1358 status_t err = mRenderer->setPlaybackSettings(mPlaybackSettings);
1359 if (err != OK) {
1360 mSource->stop();
Robert Shih0c61a0d2015-07-06 15:09:10 -07001361 mSourceStarted = false;
Lajos Molnar3a474aa2015-04-24 17:10:07 -07001362 notifyListener(MEDIA_ERROR, MEDIA_ERROR_UNKNOWN, err);
1363 return;
Wei Jiac8206ff2015-03-04 13:59:37 -08001364 }
Wei Jia94211742014-10-28 17:09:06 -07001365
Ronghua Wuc8a70d32015-04-29 16:26:34 -07001366 float rate = getFrameRate();
1367 if (rate > 0) {
Wei Jia94211742014-10-28 17:09:06 -07001368 mRenderer->setVideoFrameRate(rate);
1369 }
1370
Wei Jiac6cfd702014-11-11 16:33:20 -08001371 if (mVideoDecoder != NULL) {
1372 mVideoDecoder->setRenderer(mRenderer);
1373 }
1374 if (mAudioDecoder != NULL) {
1375 mAudioDecoder->setRenderer(mRenderer);
1376 }
1377
Wei Jia94211742014-10-28 17:09:06 -07001378 postScanSources();
1379}
1380
Chong Zhangefbb6192015-01-30 17:13:27 -08001381void NuPlayer::onPause() {
1382 if (mPaused) {
1383 return;
1384 }
1385 mPaused = true;
1386 if (mSource != NULL) {
1387 mSource->pause();
1388 } else {
1389 ALOGW("pause called when source is gone or not set");
1390 }
1391 if (mRenderer != NULL) {
1392 mRenderer->pause();
1393 } else {
1394 ALOGW("pause called when renderer is gone or not set");
1395 }
1396}
1397
Ronghua Wud7988b12014-10-03 15:19:10 -07001398bool NuPlayer::audioDecoderStillNeeded() {
1399 // Audio decoder is no longer needed if it's in shut/shutting down status.
1400 return ((mFlushingAudio != SHUT_DOWN) && (mFlushingAudio != SHUTTING_DOWN_DECODER));
1401}
1402
Andy Hung8d121d42014-10-03 09:53:53 -07001403void NuPlayer::handleFlushComplete(bool audio, bool isDecoder) {
1404 // We wait for both the decoder flush and the renderer flush to complete
1405 // before entering either the FLUSHED or the SHUTTING_DOWN_DECODER state.
1406
1407 mFlushComplete[audio][isDecoder] = true;
1408 if (!mFlushComplete[audio][!isDecoder]) {
1409 return;
1410 }
1411
1412 FlushStatus *state = audio ? &mFlushingAudio : &mFlushingVideo;
1413 switch (*state) {
1414 case FLUSHING_DECODER:
1415 {
1416 *state = FLUSHED;
Andy Hung8d121d42014-10-03 09:53:53 -07001417 break;
1418 }
1419
1420 case FLUSHING_DECODER_SHUTDOWN:
1421 {
1422 *state = SHUTTING_DOWN_DECODER;
1423
1424 ALOGV("initiating %s decoder shutdown", audio ? "audio" : "video");
1425 if (!audio) {
Andy Hung8d121d42014-10-03 09:53:53 -07001426 // Widevine source reads must stop before releasing the video decoder.
1427 if (mSource != NULL && mSourceFlags & Source::FLAG_SECURE) {
1428 mSource->stop();
Robert Shih0c61a0d2015-07-06 15:09:10 -07001429 mSourceStarted = false;
Andy Hung8d121d42014-10-03 09:53:53 -07001430 }
1431 }
1432 getDecoder(audio)->initiateShutdown();
1433 break;
1434 }
1435
1436 default:
1437 // decoder flush completes only occur in a flushing state.
1438 LOG_ALWAYS_FATAL_IF(isDecoder, "decoder flush in invalid state %d", *state);
1439 break;
1440 }
1441}
1442
Andreas Huber3831a062010-12-21 10:22:33 -08001443void NuPlayer::finishFlushIfPossible() {
Wei Jia53904f32014-07-29 10:22:53 -07001444 if (mFlushingAudio != NONE && mFlushingAudio != FLUSHED
1445 && mFlushingAudio != SHUT_DOWN) {
Andreas Huber3831a062010-12-21 10:22:33 -08001446 return;
1447 }
1448
Wei Jia53904f32014-07-29 10:22:53 -07001449 if (mFlushingVideo != NONE && mFlushingVideo != FLUSHED
1450 && mFlushingVideo != SHUT_DOWN) {
Andreas Huber3831a062010-12-21 10:22:33 -08001451 return;
1452 }
1453
Steve Block3856b092011-10-20 11:56:00 +01001454 ALOGV("both audio and video are flushed now.");
Andreas Huber3831a062010-12-21 10:22:33 -08001455
Andreas Huber3831a062010-12-21 10:22:33 -08001456 mFlushingAudio = NONE;
1457 mFlushingVideo = NONE;
Andreas Huber3831a062010-12-21 10:22:33 -08001458
Andy Hung8d121d42014-10-03 09:53:53 -07001459 clearFlushComplete();
1460
Andreas Hubera1f8ab02012-11-30 10:53:22 -08001461 processDeferredActions();
Andreas Huber1aef2112011-01-04 14:01:29 -08001462}
1463
1464void NuPlayer::postScanSources() {
1465 if (mScanSourcesPending) {
1466 return;
1467 }
1468
Lajos Molnar1d15ab52015-03-04 16:46:34 -08001469 sp<AMessage> msg = new AMessage(kWhatScanSources, this);
Andreas Huber1aef2112011-01-04 14:01:29 -08001470 msg->setInt32("generation", mScanSourcesGeneration);
1471 msg->post();
1472
1473 mScanSourcesPending = true;
1474}
1475
Andy Hung202bce12014-12-03 11:47:36 -08001476void NuPlayer::tryOpenAudioSinkForOffload(const sp<AMessage> &format, bool hasVideo) {
1477 // Note: This is called early in NuPlayer to determine whether offloading
1478 // is possible; otherwise the decoders call the renderer openAudioSink directly.
Andy Hung282a7e32014-08-14 15:56:34 -07001479
Andy Hung202bce12014-12-03 11:47:36 -08001480 status_t err = mRenderer->openAudioSink(
1481 format, true /* offloadOnly */, hasVideo, AUDIO_OUTPUT_FLAG_NONE, &mOffloadAudio);
1482 if (err != OK) {
1483 // Any failure we turn off mOffloadAudio.
1484 mOffloadAudio = false;
1485 } else if (mOffloadAudio) {
Chong Zhang3b9eb1f2014-10-15 17:05:08 -07001486 sp<MetaData> audioMeta =
1487 mSource->getFormatMeta(true /* audio */);
1488 sendMetaDataToHal(mAudioSink, audioMeta);
Andy Hung282a7e32014-08-14 15:56:34 -07001489 }
1490}
1491
1492void NuPlayer::closeAudioSink() {
Chong Zhang3b9eb1f2014-10-15 17:05:08 -07001493 mRenderer->closeAudioSink();
Andy Hung282a7e32014-08-14 15:56:34 -07001494}
1495
Wei Jia5031b2f2016-02-25 11:19:31 -08001496void NuPlayer::restartAudio(
Wei Jiabf70feb2016-02-19 15:47:16 -08001497 int64_t currentPositionUs, bool forceNonOffload, bool needsToCreateAudioDecoder) {
Wei Jiaa05f1e32016-03-25 16:31:22 -07001498 if (mAudioDecoder != NULL) {
1499 mAudioDecoder->pause();
1500 mAudioDecoder.clear();
1501 ++mAudioDecoderGeneration;
1502 }
Wei Jiabf70feb2016-02-19 15:47:16 -08001503 if (mFlushingAudio == FLUSHING_DECODER) {
1504 mFlushComplete[1 /* audio */][1 /* isDecoder */] = true;
1505 mFlushingAudio = FLUSHED;
1506 finishFlushIfPossible();
1507 } else if (mFlushingAudio == FLUSHING_DECODER_SHUTDOWN
1508 || mFlushingAudio == SHUTTING_DOWN_DECODER) {
1509 mFlushComplete[1 /* audio */][1 /* isDecoder */] = true;
1510 mFlushingAudio = SHUT_DOWN;
1511 finishFlushIfPossible();
1512 needsToCreateAudioDecoder = false;
1513 }
1514 if (mRenderer == NULL) {
1515 return;
1516 }
1517 closeAudioSink();
1518 mRenderer->flush(true /* audio */, false /* notifyComplete */);
1519 if (mVideoDecoder != NULL) {
1520 mRenderer->flush(false /* audio */, false /* notifyComplete */);
1521 }
1522
1523 performSeek(currentPositionUs);
1524
1525 if (forceNonOffload) {
1526 mRenderer->signalDisableOffloadAudio();
1527 mOffloadAudio = false;
1528 }
1529 if (needsToCreateAudioDecoder) {
Wei Jiaa05f1e32016-03-25 16:31:22 -07001530 instantiateDecoder(true /* audio */, &mAudioDecoder, !forceNonOffload);
Wei Jiabf70feb2016-02-19 15:47:16 -08001531 }
1532}
1533
Wei Jiae4d18c72015-07-13 17:58:11 -07001534void NuPlayer::determineAudioModeChange() {
1535 if (mSource == NULL || mAudioSink == NULL) {
1536 return;
1537 }
1538
1539 if (mRenderer == NULL) {
1540 ALOGW("No renderer can be used to determine audio mode. Use non-offload for safety.");
1541 mOffloadAudio = false;
1542 return;
1543 }
1544
1545 sp<MetaData> audioMeta = mSource->getFormatMeta(true /* audio */);
1546 sp<AMessage> videoFormat = mSource->getFormat(false /* audio */);
1547 audio_stream_type_t streamType = mAudioSink->getAudioStreamType();
1548 const bool hasVideo = (videoFormat != NULL);
1549 const bool canOffload = canOffloadStream(
Wei Jiabf70feb2016-02-19 15:47:16 -08001550 audioMeta, hasVideo, mSource->isStreaming(), streamType)
1551 && (mPlaybackSettings.mSpeed == 1.f && mPlaybackSettings.mPitch == 1.f);
Wei Jiae4d18c72015-07-13 17:58:11 -07001552 if (canOffload) {
1553 if (!mOffloadAudio) {
1554 mRenderer->signalEnableOffloadAudio();
1555 }
1556 // open audio sink early under offload mode.
1557 sp<AMessage> format = mSource->getFormat(true /*audio*/);
1558 tryOpenAudioSinkForOffload(format, hasVideo);
1559 } else {
Robert Shihe1d70192015-07-23 17:54:13 -07001560 if (mOffloadAudio) {
1561 mRenderer->signalDisableOffloadAudio();
1562 mOffloadAudio = false;
1563 }
Wei Jiae4d18c72015-07-13 17:58:11 -07001564 }
1565}
1566
Wei Jiaa05f1e32016-03-25 16:31:22 -07001567status_t NuPlayer::instantiateDecoder(
1568 bool audio, sp<DecoderBase> *decoder, bool checkAudioModeChange) {
Wei Jia566da802015-08-27 13:59:30 -07001569 // The audio decoder could be cleared by tear down. If still in shut down
1570 // process, no need to create a new audio decoder.
1571 if (*decoder != NULL || (audio && mFlushingAudio == SHUT_DOWN)) {
Andreas Huberf9334412010-12-15 15:17:42 -08001572 return OK;
1573 }
1574
Andreas Huber84066782011-08-16 09:34:26 -07001575 sp<AMessage> format = mSource->getFormat(audio);
Andreas Huberf9334412010-12-15 15:17:42 -08001576
Andreas Huber84066782011-08-16 09:34:26 -07001577 if (format == NULL) {
Robert Shih7350b052015-10-01 15:50:14 -07001578 return UNKNOWN_ERROR;
1579 } else {
1580 status_t err;
1581 if (format->findInt32("err", &err) && err) {
1582 return err;
1583 }
Andreas Huberf9334412010-12-15 15:17:42 -08001584 }
1585
Ronghua Wu8db88132015-04-22 13:51:35 -07001586 format->setInt32("priority", 0 /* realtime */);
1587
Andreas Huber3fe62152011-09-16 15:09:22 -07001588 if (!audio) {
Andreas Huber84066782011-08-16 09:34:26 -07001589 AString mime;
1590 CHECK(format->findString("mime", &mime));
Chong Zhanga7fa1d92014-06-11 14:49:23 -07001591
Lajos Molnar1d15ab52015-03-04 16:46:34 -08001592 sp<AMessage> ccNotify = new AMessage(kWhatClosedCaptionNotify, this);
Chong Zhang341ab6e2015-02-04 13:37:18 -08001593 if (mCCDecoder == NULL) {
1594 mCCDecoder = new CCDecoder(ccNotify);
1595 }
Lajos Molnar09524832014-07-17 14:29:51 -07001596
1597 if (mSourceFlags & Source::FLAG_SECURE) {
1598 format->setInt32("secure", true);
1599 }
Chong Zhang17134602015-01-07 16:14:34 -08001600
1601 if (mSourceFlags & Source::FLAG_PROTECTED) {
1602 format->setInt32("protected", true);
1603 }
Ronghua Wu8db88132015-04-22 13:51:35 -07001604
Ronghua Wuc8a70d32015-04-29 16:26:34 -07001605 float rate = getFrameRate();
1606 if (rate > 0) {
Lajos Molnar3a474aa2015-04-24 17:10:07 -07001607 format->setFloat("operating-rate", rate * mPlaybackSettings.mSpeed);
Ronghua Wu8db88132015-04-22 13:51:35 -07001608 }
Andreas Huber3fe62152011-09-16 15:09:22 -07001609 }
1610
Wei Jiabc2fb722014-07-08 16:37:57 -07001611 if (audio) {
Lajos Molnar1d15ab52015-03-04 16:46:34 -08001612 sp<AMessage> notify = new AMessage(kWhatAudioNotify, this);
Wei Jia88703c32014-08-06 11:24:07 -07001613 ++mAudioDecoderGeneration;
1614 notify->setInt32("generation", mAudioDecoderGeneration);
1615
Wei Jiaa05f1e32016-03-25 16:31:22 -07001616 if (checkAudioModeChange) {
1617 determineAudioModeChange();
1618 }
Wei Jiabc2fb722014-07-08 16:37:57 -07001619 if (mOffloadAudio) {
Wei Jia14532f22015-12-29 11:28:15 -08001620 mSource->setOffloadAudio(true /* offload */);
1621
Haynes Mathew George8b635332015-03-30 17:59:47 -07001622 const bool hasVideo = (mSource->getFormat(false /*audio */) != NULL);
1623 format->setInt32("has-video", hasVideo);
Wei Jiac6cfd702014-11-11 16:33:20 -08001624 *decoder = new DecoderPassThrough(notify, mSource, mRenderer);
Wei Jiabc2fb722014-07-08 16:37:57 -07001625 } else {
Wei Jia14532f22015-12-29 11:28:15 -08001626 mSource->setOffloadAudio(false /* offload */);
1627
Ronghua Wu68845c12015-07-21 09:50:48 -07001628 *decoder = new Decoder(notify, mSource, mPID, mRenderer);
Wei Jiabc2fb722014-07-08 16:37:57 -07001629 }
1630 } else {
Lajos Molnar1d15ab52015-03-04 16:46:34 -08001631 sp<AMessage> notify = new AMessage(kWhatVideoNotify, this);
Wei Jia88703c32014-08-06 11:24:07 -07001632 ++mVideoDecoderGeneration;
1633 notify->setInt32("generation", mVideoDecoderGeneration);
1634
Chong Zhang7137ec72014-11-12 16:41:05 -08001635 *decoder = new Decoder(
Ronghua Wu68845c12015-07-21 09:50:48 -07001636 notify, mSource, mPID, mRenderer, mSurface, mCCDecoder);
Lajos Molnard9fd6312014-11-06 11:00:00 -08001637
1638 // enable FRC if high-quality AV sync is requested, even if not
Lajos Molnar1de1e252015-04-30 18:18:34 -07001639 // directly queuing to display, as this will even improve textureview
Lajos Molnard9fd6312014-11-06 11:00:00 -08001640 // playback.
1641 {
1642 char value[PROPERTY_VALUE_MAX];
1643 if (property_get("persist.sys.media.avsync", value, NULL) &&
1644 (!strcmp("1", value) || !strcasecmp("true", value))) {
1645 format->setInt32("auto-frc", 1);
1646 }
1647 }
Wei Jiabc2fb722014-07-08 16:37:57 -07001648 }
Lajos Molnar1cd13982014-01-17 15:12:51 -08001649 (*decoder)->init();
Andreas Huber84066782011-08-16 09:34:26 -07001650 (*decoder)->configure(format);
Andreas Huberf9334412010-12-15 15:17:42 -08001651
Lajos Molnar09524832014-07-17 14:29:51 -07001652 // allocate buffers to decrypt widevine source buffers
1653 if (!audio && (mSourceFlags & Source::FLAG_SECURE)) {
1654 Vector<sp<ABuffer> > inputBufs;
1655 CHECK_EQ((*decoder)->getInputBuffers(&inputBufs), (status_t)OK);
1656
1657 Vector<MediaBuffer *> mediaBufs;
1658 for (size_t i = 0; i < inputBufs.size(); i++) {
1659 const sp<ABuffer> &buffer = inputBufs[i];
1660 MediaBuffer *mbuf = new MediaBuffer(buffer->data(), buffer->size());
1661 mediaBufs.push(mbuf);
1662 }
1663
1664 status_t err = mSource->setBuffers(audio, mediaBufs);
1665 if (err != OK) {
1666 for (size_t i = 0; i < mediaBufs.size(); ++i) {
1667 mediaBufs[i]->release();
1668 }
1669 mediaBufs.clear();
1670 ALOGE("Secure source didn't support secure mediaBufs.");
1671 return err;
1672 }
1673 }
Andreas Huberf9334412010-12-15 15:17:42 -08001674 return OK;
1675}
1676
Chong Zhangced1c2f2014-08-08 15:22:35 -07001677void NuPlayer::updateVideoSize(
1678 const sp<AMessage> &inputFormat,
1679 const sp<AMessage> &outputFormat) {
1680 if (inputFormat == NULL) {
1681 ALOGW("Unknown video size, reporting 0x0!");
1682 notifyListener(MEDIA_SET_VIDEO_SIZE, 0, 0);
1683 return;
1684 }
1685
1686 int32_t displayWidth, displayHeight;
Chong Zhangced1c2f2014-08-08 15:22:35 -07001687 if (outputFormat != NULL) {
1688 int32_t width, height;
1689 CHECK(outputFormat->findInt32("width", &width));
1690 CHECK(outputFormat->findInt32("height", &height));
1691
1692 int32_t cropLeft, cropTop, cropRight, cropBottom;
1693 CHECK(outputFormat->findRect(
1694 "crop",
1695 &cropLeft, &cropTop, &cropRight, &cropBottom));
1696
1697 displayWidth = cropRight - cropLeft + 1;
1698 displayHeight = cropBottom - cropTop + 1;
1699
1700 ALOGV("Video output format changed to %d x %d "
1701 "(crop: %d x %d @ (%d, %d))",
1702 width, height,
1703 displayWidth,
1704 displayHeight,
1705 cropLeft, cropTop);
1706 } else {
1707 CHECK(inputFormat->findInt32("width", &displayWidth));
1708 CHECK(inputFormat->findInt32("height", &displayHeight));
1709
1710 ALOGV("Video input format %d x %d", displayWidth, displayHeight);
1711 }
1712
1713 // Take into account sample aspect ratio if necessary:
1714 int32_t sarWidth, sarHeight;
1715 if (inputFormat->findInt32("sar-width", &sarWidth)
1716 && inputFormat->findInt32("sar-height", &sarHeight)) {
1717 ALOGV("Sample aspect ratio %d : %d", sarWidth, sarHeight);
1718
1719 displayWidth = (displayWidth * sarWidth) / sarHeight;
1720
1721 ALOGV("display dimensions %d x %d", displayWidth, displayHeight);
1722 }
1723
1724 int32_t rotationDegrees;
1725 if (!inputFormat->findInt32("rotation-degrees", &rotationDegrees)) {
1726 rotationDegrees = 0;
1727 }
1728
1729 if (rotationDegrees == 90 || rotationDegrees == 270) {
1730 int32_t tmp = displayWidth;
1731 displayWidth = displayHeight;
1732 displayHeight = tmp;
1733 }
1734
1735 notifyListener(
1736 MEDIA_SET_VIDEO_SIZE,
1737 displayWidth,
1738 displayHeight);
1739}
1740
Chong Zhangdcb89b32013-08-06 09:44:47 -07001741void NuPlayer::notifyListener(int msg, int ext1, int ext2, const Parcel *in) {
Andreas Huber43c3e6c2011-01-05 12:17:08 -08001742 if (mDriver == NULL) {
Andreas Huberf9334412010-12-15 15:17:42 -08001743 return;
1744 }
1745
Andreas Huber43c3e6c2011-01-05 12:17:08 -08001746 sp<NuPlayerDriver> driver = mDriver.promote();
Andreas Huberf9334412010-12-15 15:17:42 -08001747
Andreas Huber43c3e6c2011-01-05 12:17:08 -08001748 if (driver == NULL) {
Andreas Huberf9334412010-12-15 15:17:42 -08001749 return;
1750 }
1751
Chong Zhangdcb89b32013-08-06 09:44:47 -07001752 driver->notifyListener(msg, ext1, ext2, in);
Andreas Huberf9334412010-12-15 15:17:42 -08001753}
1754
Chong Zhang7137ec72014-11-12 16:41:05 -08001755void NuPlayer::flushDecoder(bool audio, bool needShutdown) {
Andreas Huber14f76722013-01-15 09:04:18 -08001756 ALOGV("[%s] flushDecoder needShutdown=%d",
1757 audio ? "audio" : "video", needShutdown);
1758
Chong Zhang7137ec72014-11-12 16:41:05 -08001759 const sp<DecoderBase> &decoder = getDecoder(audio);
Lajos Molnar87603c02014-08-20 19:25:30 -07001760 if (decoder == NULL) {
Steve Blockdf64d152012-01-04 20:05:49 +00001761 ALOGI("flushDecoder %s without decoder present",
Andreas Huber6e3d3112011-11-28 12:36:11 -08001762 audio ? "audio" : "video");
Lajos Molnar87603c02014-08-20 19:25:30 -07001763 return;
Andreas Huber6e3d3112011-11-28 12:36:11 -08001764 }
1765
Andreas Huber1aef2112011-01-04 14:01:29 -08001766 // Make sure we don't continue to scan sources until we finish flushing.
1767 ++mScanSourcesGeneration;
Chong Zhang3b032b32015-04-17 15:49:06 -07001768 if (mScanSourcesPending) {
1769 mDeferredActions.push_back(
1770 new SimpleAction(&NuPlayer::performScanSources));
1771 mScanSourcesPending = false;
1772 }
Andreas Huber1aef2112011-01-04 14:01:29 -08001773
Chong Zhang7137ec72014-11-12 16:41:05 -08001774 decoder->signalFlush();
Andreas Huber1aef2112011-01-04 14:01:29 -08001775
1776 FlushStatus newStatus =
1777 needShutdown ? FLUSHING_DECODER_SHUTDOWN : FLUSHING_DECODER;
1778
Lajos Molnarfcd3e942015-03-31 10:06:48 -07001779 mFlushComplete[audio][false /* isDecoder */] = (mRenderer == NULL);
Andy Hung8d121d42014-10-03 09:53:53 -07001780 mFlushComplete[audio][true /* isDecoder */] = false;
Andreas Huber1aef2112011-01-04 14:01:29 -08001781 if (audio) {
Wei Jia53904f32014-07-29 10:22:53 -07001782 ALOGE_IF(mFlushingAudio != NONE,
1783 "audio flushDecoder() is called in state %d", mFlushingAudio);
Andreas Huber1aef2112011-01-04 14:01:29 -08001784 mFlushingAudio = newStatus;
Andreas Huber1aef2112011-01-04 14:01:29 -08001785 } else {
Wei Jia53904f32014-07-29 10:22:53 -07001786 ALOGE_IF(mFlushingVideo != NONE,
1787 "video flushDecoder() is called in state %d", mFlushingVideo);
Andreas Huber1aef2112011-01-04 14:01:29 -08001788 mFlushingVideo = newStatus;
Andreas Huber1aef2112011-01-04 14:01:29 -08001789 }
1790}
1791
Chong Zhangced1c2f2014-08-08 15:22:35 -07001792void NuPlayer::queueDecoderShutdown(
1793 bool audio, bool video, const sp<AMessage> &reply) {
1794 ALOGI("queueDecoderShutdown audio=%d, video=%d", audio, video);
Andreas Huber84066782011-08-16 09:34:26 -07001795
Chong Zhangced1c2f2014-08-08 15:22:35 -07001796 mDeferredActions.push_back(
Wei Jiafef808d2014-10-31 17:57:05 -07001797 new FlushDecoderAction(
1798 audio ? FLUSH_CMD_SHUTDOWN : FLUSH_CMD_NONE,
1799 video ? FLUSH_CMD_SHUTDOWN : FLUSH_CMD_NONE));
Andreas Huber84066782011-08-16 09:34:26 -07001800
Chong Zhangced1c2f2014-08-08 15:22:35 -07001801 mDeferredActions.push_back(
1802 new SimpleAction(&NuPlayer::performScanSources));
Andreas Huber84066782011-08-16 09:34:26 -07001803
Chong Zhangced1c2f2014-08-08 15:22:35 -07001804 mDeferredActions.push_back(new PostMessageAction(reply));
1805
1806 processDeferredActions();
Andreas Huber84066782011-08-16 09:34:26 -07001807}
1808
James Dong0d268a32012-08-31 12:18:27 -07001809status_t NuPlayer::setVideoScalingMode(int32_t mode) {
1810 mVideoScalingMode = mode;
Lajos Molnar1de1e252015-04-30 18:18:34 -07001811 if (mSurface != NULL) {
1812 status_t ret = native_window_set_scaling_mode(mSurface.get(), mVideoScalingMode);
James Dong0d268a32012-08-31 12:18:27 -07001813 if (ret != OK) {
1814 ALOGE("Failed to set scaling mode (%d): %s",
1815 -ret, strerror(-ret));
1816 return ret;
1817 }
1818 }
1819 return OK;
1820}
1821
Chong Zhangdcb89b32013-08-06 09:44:47 -07001822status_t NuPlayer::getTrackInfo(Parcel* reply) const {
Lajos Molnar1d15ab52015-03-04 16:46:34 -08001823 sp<AMessage> msg = new AMessage(kWhatGetTrackInfo, this);
Chong Zhangdcb89b32013-08-06 09:44:47 -07001824 msg->setPointer("reply", reply);
1825
1826 sp<AMessage> response;
1827 status_t err = msg->postAndAwaitResponse(&response);
1828 return err;
1829}
1830
Robert Shih7c4f0d72014-07-09 18:53:31 -07001831status_t NuPlayer::getSelectedTrack(int32_t type, Parcel* reply) const {
Lajos Molnar1d15ab52015-03-04 16:46:34 -08001832 sp<AMessage> msg = new AMessage(kWhatGetSelectedTrack, this);
Robert Shih7c4f0d72014-07-09 18:53:31 -07001833 msg->setPointer("reply", reply);
1834 msg->setInt32("type", type);
1835
1836 sp<AMessage> response;
1837 status_t err = msg->postAndAwaitResponse(&response);
1838 if (err == OK && response != NULL) {
1839 CHECK(response->findInt32("err", &err));
1840 }
1841 return err;
1842}
1843
Robert Shih6ffb1fd2014-10-29 16:24:32 -07001844status_t NuPlayer::selectTrack(size_t trackIndex, bool select, int64_t timeUs) {
Lajos Molnar1d15ab52015-03-04 16:46:34 -08001845 sp<AMessage> msg = new AMessage(kWhatSelectTrack, this);
Chong Zhangdcb89b32013-08-06 09:44:47 -07001846 msg->setSize("trackIndex", trackIndex);
1847 msg->setInt32("select", select);
Robert Shih6ffb1fd2014-10-29 16:24:32 -07001848 msg->setInt64("timeUs", timeUs);
Chong Zhangdcb89b32013-08-06 09:44:47 -07001849
1850 sp<AMessage> response;
1851 status_t err = msg->postAndAwaitResponse(&response);
1852
Chong Zhang404fced2014-06-11 14:45:31 -07001853 if (err != OK) {
1854 return err;
1855 }
1856
1857 if (!response->findInt32("err", &err)) {
1858 err = OK;
1859 }
1860
Chong Zhangdcb89b32013-08-06 09:44:47 -07001861 return err;
1862}
1863
Ronghua Wua73d9e02014-10-08 15:13:29 -07001864status_t NuPlayer::getCurrentPosition(int64_t *mediaUs) {
1865 sp<Renderer> renderer = mRenderer;
1866 if (renderer == NULL) {
1867 return NO_INIT;
1868 }
1869
1870 return renderer->getCurrentPosition(mediaUs);
1871}
1872
Praveen Chavane1e5d7a2015-05-19 19:09:48 -07001873void NuPlayer::getStats(Vector<sp<AMessage> > *mTrackStats) {
1874 CHECK(mTrackStats != NULL);
1875
1876 mTrackStats->clear();
1877 if (mVideoDecoder != NULL) {
1878 mTrackStats->push_back(mVideoDecoder->getStats());
1879 }
1880 if (mAudioDecoder != NULL) {
1881 mTrackStats->push_back(mAudioDecoder->getStats());
Chong Zhang7137ec72014-11-12 16:41:05 -08001882 }
Ronghua Wua73d9e02014-10-08 15:13:29 -07001883}
1884
Marco Nelissenf0b72b52014-09-16 15:43:44 -07001885sp<MetaData> NuPlayer::getFileMeta() {
1886 return mSource->getFileFormatMeta();
1887}
1888
Ronghua Wuc8a70d32015-04-29 16:26:34 -07001889float NuPlayer::getFrameRate() {
1890 sp<MetaData> meta = mSource->getFormatMeta(false /* audio */);
1891 if (meta == NULL) {
1892 return 0;
1893 }
1894 int32_t rate;
1895 if (!meta->findInt32(kKeyFrameRate, &rate)) {
1896 // fall back to try file meta
1897 sp<MetaData> fileMeta = getFileMeta();
1898 if (fileMeta == NULL) {
1899 ALOGW("source has video meta but not file meta");
1900 return -1;
1901 }
1902 int32_t fileMetaRate;
1903 if (!fileMeta->findInt32(kKeyFrameRate, &fileMetaRate)) {
1904 return -1;
1905 }
1906 return fileMetaRate;
1907 }
1908 return rate;
1909}
1910
Andreas Huberb7c8e912012-11-27 15:02:53 -08001911void NuPlayer::schedulePollDuration() {
Lajos Molnar1d15ab52015-03-04 16:46:34 -08001912 sp<AMessage> msg = new AMessage(kWhatPollDuration, this);
Andreas Huberb7c8e912012-11-27 15:02:53 -08001913 msg->setInt32("generation", mPollDurationGeneration);
1914 msg->post();
1915}
1916
1917void NuPlayer::cancelPollDuration() {
1918 ++mPollDurationGeneration;
1919}
1920
Andreas Hubera1f8ab02012-11-30 10:53:22 -08001921void NuPlayer::processDeferredActions() {
1922 while (!mDeferredActions.empty()) {
1923 // We won't execute any deferred actions until we're no longer in
1924 // an intermediate state, i.e. one more more decoders are currently
1925 // flushing or shutting down.
1926
Andreas Hubera1f8ab02012-11-30 10:53:22 -08001927 if (mFlushingAudio != NONE || mFlushingVideo != NONE) {
1928 // We're currently flushing, postpone the reset until that's
1929 // completed.
1930
1931 ALOGV("postponing action mFlushingAudio=%d, mFlushingVideo=%d",
1932 mFlushingAudio, mFlushingVideo);
1933
1934 break;
1935 }
1936
1937 sp<Action> action = *mDeferredActions.begin();
1938 mDeferredActions.erase(mDeferredActions.begin());
1939
1940 action->execute(this);
1941 }
1942}
1943
Wei Jia29840802015-05-15 17:11:38 -07001944void NuPlayer::performSeek(int64_t seekTimeUs) {
1945 ALOGV("performSeek seekTimeUs=%lld us (%.2f secs)",
Lajos Molnar6d339f12015-04-17 16:15:53 -07001946 (long long)seekTimeUs,
Wei Jia29840802015-05-15 17:11:38 -07001947 seekTimeUs / 1E6);
Andreas Hubera1f8ab02012-11-30 10:53:22 -08001948
Andy Hungadf34bf2014-09-03 18:22:22 -07001949 if (mSource == NULL) {
1950 // This happens when reset occurs right before the loop mode
1951 // asynchronously seeks to the start of the stream.
1952 LOG_ALWAYS_FATAL_IF(mAudioDecoder != NULL || mVideoDecoder != NULL,
1953 "mSource is NULL and decoders not NULL audio(%p) video(%p)",
1954 mAudioDecoder.get(), mVideoDecoder.get());
1955 return;
1956 }
Robert Shih1a5c8592015-08-04 18:07:44 -07001957 mPreviousSeekTimeUs = seekTimeUs;
Andreas Hubera1f8ab02012-11-30 10:53:22 -08001958 mSource->seekTo(seekTimeUs);
Wei Jia351ce872016-02-10 13:21:59 -08001959 mPendingBufferingFlag = PENDING_BUFFERING_FLAG_NONE;
Robert Shihd3b0bbb2014-07-23 15:00:25 -07001960 ++mTimedTextGeneration;
Andreas Hubera1f8ab02012-11-30 10:53:22 -08001961
Andreas Hubera1f8ab02012-11-30 10:53:22 -08001962 // everything's flushed, continue playback.
1963}
1964
Wei Jiafef808d2014-10-31 17:57:05 -07001965void NuPlayer::performDecoderFlush(FlushCommand audio, FlushCommand video) {
1966 ALOGV("performDecoderFlush audio=%d, video=%d", audio, video);
Andreas Hubera1f8ab02012-11-30 10:53:22 -08001967
Wei Jiafef808d2014-10-31 17:57:05 -07001968 if ((audio == FLUSH_CMD_NONE || mAudioDecoder == NULL)
1969 && (video == FLUSH_CMD_NONE || mVideoDecoder == NULL)) {
Andreas Hubera1f8ab02012-11-30 10:53:22 -08001970 return;
1971 }
1972
Wei Jiafef808d2014-10-31 17:57:05 -07001973 if (audio != FLUSH_CMD_NONE && mAudioDecoder != NULL) {
1974 flushDecoder(true /* audio */, (audio == FLUSH_CMD_SHUTDOWN));
Andreas Hubera1f8ab02012-11-30 10:53:22 -08001975 }
1976
Wei Jiafef808d2014-10-31 17:57:05 -07001977 if (video != FLUSH_CMD_NONE && mVideoDecoder != NULL) {
1978 flushDecoder(false /* audio */, (video == FLUSH_CMD_SHUTDOWN));
Andreas Hubera1f8ab02012-11-30 10:53:22 -08001979 }
1980}
1981
1982void NuPlayer::performReset() {
1983 ALOGV("performReset");
1984
1985 CHECK(mAudioDecoder == NULL);
1986 CHECK(mVideoDecoder == NULL);
1987
1988 cancelPollDuration();
1989
1990 ++mScanSourcesGeneration;
1991 mScanSourcesPending = false;
1992
Lajos Molnar09524832014-07-17 14:29:51 -07001993 if (mRendererLooper != NULL) {
1994 if (mRenderer != NULL) {
1995 mRendererLooper->unregisterHandler(mRenderer->id());
1996 }
1997 mRendererLooper->stop();
1998 mRendererLooper.clear();
1999 }
Andreas Hubera1f8ab02012-11-30 10:53:22 -08002000 mRenderer.clear();
Wei Jia57568df2014-09-22 10:16:29 -07002001 ++mRendererGeneration;
Andreas Hubera1f8ab02012-11-30 10:53:22 -08002002
2003 if (mSource != NULL) {
2004 mSource->stop();
Andreas Huberb5f25f02013-02-05 10:14:26 -08002005
Andreas Hubera1f8ab02012-11-30 10:53:22 -08002006 mSource.clear();
2007 }
2008
2009 if (mDriver != NULL) {
2010 sp<NuPlayerDriver> driver = mDriver.promote();
2011 if (driver != NULL) {
2012 driver->notifyResetComplete();
2013 }
2014 }
Andreas Huber57a339c2012-12-03 11:18:00 -08002015
2016 mStarted = false;
Ronghua Wu64c2d172015-10-07 16:52:19 -07002017 mResetting = false;
Robert Shih0c61a0d2015-07-06 15:09:10 -07002018 mSourceStarted = false;
Andreas Hubera1f8ab02012-11-30 10:53:22 -08002019}
2020
2021void NuPlayer::performScanSources() {
2022 ALOGV("performScanSources");
2023
Andreas Huber57a339c2012-12-03 11:18:00 -08002024 if (!mStarted) {
2025 return;
2026 }
2027
Andreas Hubera1f8ab02012-11-30 10:53:22 -08002028 if (mAudioDecoder == NULL || mVideoDecoder == NULL) {
2029 postScanSources();
2030 }
2031}
2032
Lajos Molnar1de1e252015-04-30 18:18:34 -07002033void NuPlayer::performSetSurface(const sp<Surface> &surface) {
Andreas Huber57a339c2012-12-03 11:18:00 -08002034 ALOGV("performSetSurface");
2035
Lajos Molnar1de1e252015-04-30 18:18:34 -07002036 mSurface = surface;
Andreas Huber57a339c2012-12-03 11:18:00 -08002037
2038 // XXX - ignore error from setVideoScalingMode for now
2039 setVideoScalingMode(mVideoScalingMode);
Chong Zhang13d6faa2014-08-22 15:35:28 -07002040
2041 if (mDriver != NULL) {
2042 sp<NuPlayerDriver> driver = mDriver.promote();
2043 if (driver != NULL) {
2044 driver->notifySetSurfaceComplete();
2045 }
2046 }
Andreas Huber57a339c2012-12-03 11:18:00 -08002047}
2048
Chong Zhangf8d71772014-11-26 15:08:34 -08002049void NuPlayer::performResumeDecoders(bool needNotify) {
2050 if (needNotify) {
2051 mResumePending = true;
2052 if (mVideoDecoder == NULL) {
2053 // if audio-only, we can notify seek complete now,
2054 // as the resume operation will be relatively fast.
2055 finishResume();
2056 }
2057 }
2058
Chong Zhang7137ec72014-11-12 16:41:05 -08002059 if (mVideoDecoder != NULL) {
Chong Zhangf8d71772014-11-26 15:08:34 -08002060 // When there is continuous seek, MediaPlayer will cache the seek
2061 // position, and send down new seek request when previous seek is
2062 // complete. Let's wait for at least one video output frame before
2063 // notifying seek complete, so that the video thumbnail gets updated
2064 // when seekbar is dragged.
2065 mVideoDecoder->signalResume(needNotify);
Chong Zhang7137ec72014-11-12 16:41:05 -08002066 }
2067
2068 if (mAudioDecoder != NULL) {
Chong Zhangf8d71772014-11-26 15:08:34 -08002069 mAudioDecoder->signalResume(false /* needNotify */);
2070 }
2071}
2072
2073void NuPlayer::finishResume() {
2074 if (mResumePending) {
2075 mResumePending = false;
Wei Jia1061c9c2015-05-19 16:02:17 -07002076 notifyDriverSeekComplete();
2077 }
2078}
2079
2080void NuPlayer::notifyDriverSeekComplete() {
2081 if (mDriver != NULL) {
2082 sp<NuPlayerDriver> driver = mDriver.promote();
2083 if (driver != NULL) {
2084 driver->notifySeekComplete();
Chong Zhangf8d71772014-11-26 15:08:34 -08002085 }
Chong Zhang7137ec72014-11-12 16:41:05 -08002086 }
2087}
2088
Andreas Huber9575c962013-02-05 13:59:56 -08002089void NuPlayer::onSourceNotify(const sp<AMessage> &msg) {
2090 int32_t what;
2091 CHECK(msg->findInt32("what", &what));
2092
2093 switch (what) {
Lajos Molnarfcd3e942015-03-31 10:06:48 -07002094 case Source::kWhatInstantiateSecureDecoders:
2095 {
2096 if (mSource == NULL) {
2097 // This is a stale notification from a source that was
2098 // asynchronously preparing when the client called reset().
2099 // We handled the reset, the source is gone.
2100 break;
2101 }
2102
2103 sp<AMessage> reply;
2104 CHECK(msg->findMessage("reply", &reply));
2105 status_t err = onInstantiateSecureDecoders();
2106 reply->setInt32("err", err);
2107 reply->post();
2108 break;
2109 }
2110
Andreas Huber9575c962013-02-05 13:59:56 -08002111 case Source::kWhatPrepared:
2112 {
Andreas Huberb5f28d42013-04-25 15:11:19 -07002113 if (mSource == NULL) {
2114 // This is a stale notification from a source that was
2115 // asynchronously preparing when the client called reset().
2116 // We handled the reset, the source is gone.
2117 break;
2118 }
2119
Andreas Huberec0c5972013-02-05 14:47:13 -08002120 int32_t err;
2121 CHECK(msg->findInt32("err", &err));
2122
Lajos Molnarfcd3e942015-03-31 10:06:48 -07002123 if (err != OK) {
2124 // shut down potential secure codecs in case client never calls reset
2125 mDeferredActions.push_back(
2126 new FlushDecoderAction(FLUSH_CMD_SHUTDOWN /* audio */,
2127 FLUSH_CMD_SHUTDOWN /* video */));
2128 processDeferredActions();
2129 }
2130
Andreas Huber9575c962013-02-05 13:59:56 -08002131 sp<NuPlayerDriver> driver = mDriver.promote();
2132 if (driver != NULL) {
Marco Nelissendd114d12014-05-28 15:23:14 -07002133 // notify duration first, so that it's definitely set when
2134 // the app received the "prepare complete" callback.
2135 int64_t durationUs;
2136 if (mSource->getDuration(&durationUs) == OK) {
2137 driver->notifyDuration(durationUs);
2138 }
Andreas Huberec0c5972013-02-05 14:47:13 -08002139 driver->notifyPrepareCompleted(err);
Andreas Huber9575c962013-02-05 13:59:56 -08002140 }
Andreas Huber99759402013-04-01 14:28:31 -07002141
Andreas Huber9575c962013-02-05 13:59:56 -08002142 break;
2143 }
2144
2145 case Source::kWhatFlagsChanged:
2146 {
2147 uint32_t flags;
2148 CHECK(msg->findInt32("flags", (int32_t *)&flags));
2149
Chong Zhang4b7069d2013-09-11 12:52:43 -07002150 sp<NuPlayerDriver> driver = mDriver.promote();
2151 if (driver != NULL) {
Wei Jia895651b2014-12-10 17:31:52 -08002152 if ((flags & NuPlayer::Source::FLAG_CAN_SEEK) == 0) {
2153 driver->notifyListener(
2154 MEDIA_INFO, MEDIA_INFO_NOT_SEEKABLE, 0);
2155 }
Chong Zhang4b7069d2013-09-11 12:52:43 -07002156 driver->notifyFlagsChanged(flags);
2157 }
2158
Andreas Huber9575c962013-02-05 13:59:56 -08002159 if ((mSourceFlags & Source::FLAG_DYNAMIC_DURATION)
2160 && (!(flags & Source::FLAG_DYNAMIC_DURATION))) {
2161 cancelPollDuration();
2162 } else if (!(mSourceFlags & Source::FLAG_DYNAMIC_DURATION)
2163 && (flags & Source::FLAG_DYNAMIC_DURATION)
2164 && (mAudioDecoder != NULL || mVideoDecoder != NULL)) {
2165 schedulePollDuration();
2166 }
2167
2168 mSourceFlags = flags;
2169 break;
2170 }
2171
2172 case Source::kWhatVideoSizeChanged:
2173 {
Chong Zhangced1c2f2014-08-08 15:22:35 -07002174 sp<AMessage> format;
2175 CHECK(msg->findMessage("format", &format));
Andreas Huber9575c962013-02-05 13:59:56 -08002176
Chong Zhangced1c2f2014-08-08 15:22:35 -07002177 updateVideoSize(format);
Andreas Huber9575c962013-02-05 13:59:56 -08002178 break;
2179 }
2180
Chong Zhang2a3cc9a2014-08-21 17:48:26 -07002181 case Source::kWhatBufferingUpdate:
2182 {
2183 int32_t percentage;
2184 CHECK(msg->findInt32("percentage", &percentage));
2185
2186 notifyListener(MEDIA_BUFFERING_UPDATE, percentage, 0);
2187 break;
2188 }
2189
Chong Zhangefbb6192015-01-30 17:13:27 -08002190 case Source::kWhatPauseOnBufferingStart:
2191 {
2192 // ignore if not playing
Chong Zhang8a048332015-05-06 15:16:28 -07002193 if (mStarted) {
Chong Zhangefbb6192015-01-30 17:13:27 -08002194 ALOGI("buffer low, pausing...");
2195
Chong Zhang8a048332015-05-06 15:16:28 -07002196 mPausedForBuffering = true;
Chong Zhangefbb6192015-01-30 17:13:27 -08002197 onPause();
2198 }
Wei Jiae67ba382016-02-03 01:41:49 +00002199 // fall-thru
2200 }
Chong Zhangefbb6192015-01-30 17:13:27 -08002201
Wei Jiae67ba382016-02-03 01:41:49 +00002202 case Source::kWhatBufferingStart:
2203 {
Wei Jia71c75e02016-02-04 09:40:47 -08002204 if (mPausedByClient) {
2205 mPendingBufferingFlag = PENDING_BUFFERING_FLAG_START;
2206 } else {
2207 notifyListener(MEDIA_INFO, MEDIA_INFO_BUFFERING_START, 0);
2208 mPendingBufferingFlag = PENDING_BUFFERING_FLAG_NONE;
2209 }
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 Jiae67ba382016-02-03 01:41:49 +00002226 // fall-thru
2227 }
Chong Zhangefbb6192015-01-30 17:13:27 -08002228
Wei Jiae67ba382016-02-03 01:41:49 +00002229 case Source::kWhatBufferingEnd:
2230 {
Wei Jia3bed45a2016-02-17 11:06:47 -08002231 notifyListener(MEDIA_INFO, MEDIA_INFO_BUFFERING_END, 0);
2232 mPendingBufferingFlag = PENDING_BUFFERING_FLAG_NONE;
Roger Jönssonb50e83e2013-01-21 16:26:41 +01002233 break;
2234 }
2235
Chong Zhangefbb6192015-01-30 17:13:27 -08002236 case Source::kWhatCacheStats:
2237 {
2238 int32_t kbps;
2239 CHECK(msg->findInt32("bandwidth", &kbps));
2240
2241 notifyListener(MEDIA_INFO, MEDIA_INFO_NETWORK_BANDWIDTH, kbps);
2242 break;
2243 }
2244
Chong Zhangdcb89b32013-08-06 09:44:47 -07002245 case Source::kWhatSubtitleData:
2246 {
2247 sp<ABuffer> buffer;
2248 CHECK(msg->findBuffer("buffer", &buffer));
2249
Chong Zhang404fced2014-06-11 14:45:31 -07002250 sendSubtitleData(buffer, 0 /* baseIndex */);
Chong Zhangdcb89b32013-08-06 09:44:47 -07002251 break;
2252 }
2253
Robert Shih08528432015-04-08 09:06:54 -07002254 case Source::kWhatTimedMetaData:
2255 {
2256 sp<ABuffer> buffer;
2257 if (!msg->findBuffer("buffer", &buffer)) {
2258 notifyListener(MEDIA_INFO, MEDIA_INFO_METADATA_UPDATE, 0);
2259 } else {
2260 sendTimedMetaData(buffer);
2261 }
2262 break;
2263 }
2264
Robert Shihd3b0bbb2014-07-23 15:00:25 -07002265 case Source::kWhatTimedTextData:
2266 {
2267 int32_t generation;
2268 if (msg->findInt32("generation", &generation)
2269 && generation != mTimedTextGeneration) {
2270 break;
2271 }
2272
2273 sp<ABuffer> buffer;
2274 CHECK(msg->findBuffer("buffer", &buffer));
2275
2276 sp<NuPlayerDriver> driver = mDriver.promote();
2277 if (driver == NULL) {
2278 break;
2279 }
2280
2281 int posMs;
2282 int64_t timeUs, posUs;
2283 driver->getCurrentPosition(&posMs);
Patrik2 Carlsson24d484b2015-01-27 16:49:45 +01002284 posUs = (int64_t) posMs * 1000ll;
Robert Shihd3b0bbb2014-07-23 15:00:25 -07002285 CHECK(buffer->meta()->findInt64("timeUs", &timeUs));
2286
2287 if (posUs < timeUs) {
2288 if (!msg->findInt32("generation", &generation)) {
2289 msg->setInt32("generation", mTimedTextGeneration);
2290 }
2291 msg->post(timeUs - posUs);
2292 } else {
2293 sendTimedTextData(buffer);
2294 }
2295 break;
2296 }
2297
Andreas Huber14f76722013-01-15 09:04:18 -08002298 case Source::kWhatQueueDecoderShutdown:
2299 {
2300 int32_t audio, video;
2301 CHECK(msg->findInt32("audio", &audio));
2302 CHECK(msg->findInt32("video", &video));
2303
2304 sp<AMessage> reply;
2305 CHECK(msg->findMessage("reply", &reply));
2306
2307 queueDecoderShutdown(audio, video, reply);
2308 break;
2309 }
2310
Ronghua Wu80276872014-08-28 15:50:29 -07002311 case Source::kWhatDrmNoLicense:
2312 {
2313 notifyListener(MEDIA_ERROR, MEDIA_ERROR_UNKNOWN, ERROR_DRM_NO_LICENSE);
2314 break;
2315 }
2316
Andreas Huber9575c962013-02-05 13:59:56 -08002317 default:
2318 TRESPASS();
2319 }
2320}
2321
Chong Zhanga7fa1d92014-06-11 14:49:23 -07002322void NuPlayer::onClosedCaptionNotify(const sp<AMessage> &msg) {
2323 int32_t what;
2324 CHECK(msg->findInt32("what", &what));
2325
2326 switch (what) {
2327 case NuPlayer::CCDecoder::kWhatClosedCaptionData:
2328 {
2329 sp<ABuffer> buffer;
2330 CHECK(msg->findBuffer("buffer", &buffer));
2331
2332 size_t inbandTracks = 0;
2333 if (mSource != NULL) {
2334 inbandTracks = mSource->getTrackCount();
2335 }
2336
2337 sendSubtitleData(buffer, inbandTracks);
2338 break;
2339 }
2340
2341 case NuPlayer::CCDecoder::kWhatTrackAdded:
2342 {
2343 notifyListener(MEDIA_INFO, MEDIA_INFO_METADATA_UPDATE, 0);
2344
2345 break;
2346 }
2347
2348 default:
2349 TRESPASS();
2350 }
2351
2352
2353}
2354
Chong Zhang404fced2014-06-11 14:45:31 -07002355void NuPlayer::sendSubtitleData(const sp<ABuffer> &buffer, int32_t baseIndex) {
2356 int32_t trackIndex;
2357 int64_t timeUs, durationUs;
2358 CHECK(buffer->meta()->findInt32("trackIndex", &trackIndex));
2359 CHECK(buffer->meta()->findInt64("timeUs", &timeUs));
2360 CHECK(buffer->meta()->findInt64("durationUs", &durationUs));
2361
2362 Parcel in;
2363 in.writeInt32(trackIndex + baseIndex);
2364 in.writeInt64(timeUs);
2365 in.writeInt64(durationUs);
2366 in.writeInt32(buffer->size());
2367 in.writeInt32(buffer->size());
2368 in.write(buffer->data(), buffer->size());
2369
2370 notifyListener(MEDIA_SUBTITLE_DATA, 0, 0, &in);
2371}
Robert Shihd3b0bbb2014-07-23 15:00:25 -07002372
Robert Shih08528432015-04-08 09:06:54 -07002373void NuPlayer::sendTimedMetaData(const sp<ABuffer> &buffer) {
2374 int64_t timeUs;
2375 CHECK(buffer->meta()->findInt64("timeUs", &timeUs));
2376
2377 Parcel in;
2378 in.writeInt64(timeUs);
2379 in.writeInt32(buffer->size());
2380 in.writeInt32(buffer->size());
2381 in.write(buffer->data(), buffer->size());
2382
2383 notifyListener(MEDIA_META_DATA, 0, 0, &in);
2384}
2385
Robert Shihd3b0bbb2014-07-23 15:00:25 -07002386void NuPlayer::sendTimedTextData(const sp<ABuffer> &buffer) {
2387 const void *data;
2388 size_t size = 0;
2389 int64_t timeUs;
Marco Nelissen55e2f4c2015-09-04 15:57:15 -07002390 int32_t flag = TextDescriptions::IN_BAND_TEXT_3GPP;
Robert Shihd3b0bbb2014-07-23 15:00:25 -07002391
2392 AString mime;
2393 CHECK(buffer->meta()->findString("mime", &mime));
2394 CHECK(strcasecmp(mime.c_str(), MEDIA_MIMETYPE_TEXT_3GPP) == 0);
2395
2396 data = buffer->data();
2397 size = buffer->size();
2398
2399 Parcel parcel;
2400 if (size > 0) {
2401 CHECK(buffer->meta()->findInt64("timeUs", &timeUs));
Marco Nelissen55e2f4c2015-09-04 15:57:15 -07002402 int32_t global = 0;
2403 if (buffer->meta()->findInt32("global", &global) && global) {
2404 flag |= TextDescriptions::GLOBAL_DESCRIPTIONS;
2405 } else {
2406 flag |= TextDescriptions::LOCAL_DESCRIPTIONS;
2407 }
Robert Shihd3b0bbb2014-07-23 15:00:25 -07002408 TextDescriptions::getParcelOfDescriptions(
2409 (const uint8_t *)data, size, flag, timeUs / 1000, &parcel);
2410 }
2411
2412 if ((parcel.dataSize() > 0)) {
2413 notifyListener(MEDIA_TIMED_TEXT, 0, 0, &parcel);
2414 } else { // send an empty timed text
2415 notifyListener(MEDIA_TIMED_TEXT, 0, 0);
2416 }
2417}
Andreas Huberb5f25f02013-02-05 10:14:26 -08002418////////////////////////////////////////////////////////////////////////////////
2419
Chong Zhangced1c2f2014-08-08 15:22:35 -07002420sp<AMessage> NuPlayer::Source::getFormat(bool audio) {
2421 sp<MetaData> meta = getFormatMeta(audio);
2422
2423 if (meta == NULL) {
2424 return NULL;
2425 }
2426
2427 sp<AMessage> msg = new AMessage;
2428
2429 if(convertMetaDataToMessage(meta, &msg) == OK) {
2430 return msg;
2431 }
2432 return NULL;
2433}
2434
Andreas Huber9575c962013-02-05 13:59:56 -08002435void NuPlayer::Source::notifyFlagsChanged(uint32_t flags) {
2436 sp<AMessage> notify = dupNotify();
2437 notify->setInt32("what", kWhatFlagsChanged);
2438 notify->setInt32("flags", flags);
2439 notify->post();
2440}
2441
Chong Zhangced1c2f2014-08-08 15:22:35 -07002442void NuPlayer::Source::notifyVideoSizeChanged(const sp<AMessage> &format) {
Andreas Huber9575c962013-02-05 13:59:56 -08002443 sp<AMessage> notify = dupNotify();
2444 notify->setInt32("what", kWhatVideoSizeChanged);
Chong Zhangced1c2f2014-08-08 15:22:35 -07002445 notify->setMessage("format", format);
Andreas Huber9575c962013-02-05 13:59:56 -08002446 notify->post();
2447}
2448
Andreas Huberec0c5972013-02-05 14:47:13 -08002449void NuPlayer::Source::notifyPrepared(status_t err) {
Andreas Huber9575c962013-02-05 13:59:56 -08002450 sp<AMessage> notify = dupNotify();
2451 notify->setInt32("what", kWhatPrepared);
Andreas Huberec0c5972013-02-05 14:47:13 -08002452 notify->setInt32("err", err);
Andreas Huber9575c962013-02-05 13:59:56 -08002453 notify->post();
2454}
2455
Lajos Molnarfcd3e942015-03-31 10:06:48 -07002456void NuPlayer::Source::notifyInstantiateSecureDecoders(const sp<AMessage> &reply) {
2457 sp<AMessage> notify = dupNotify();
2458 notify->setInt32("what", kWhatInstantiateSecureDecoders);
2459 notify->setMessage("reply", reply);
2460 notify->post();
2461}
2462
Andreas Huber84333e02014-02-07 15:36:10 -08002463void NuPlayer::Source::onMessageReceived(const sp<AMessage> & /* msg */) {
Andreas Huberb5f25f02013-02-05 10:14:26 -08002464 TRESPASS();
2465}
2466
Andreas Huberf9334412010-12-15 15:17:42 -08002467} // namespace android