blob: 1fbb9573a8b64c4ee85b1f4dfcb0b29a9ca7e83e [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() {
Wei Jiac45a4b22016-04-15 15:30:23 -0700403 sp<Source> source;
404 {
405 Mutex::Autolock autoLock(mSourceLock);
406 source = mSource;
407 }
408
409 if (source != NULL) {
Chong Zhang48296b72014-09-14 14:28:45 -0700410 // During a reset, the data source might be unresponsive already, we need to
411 // disconnect explicitly so that reads exit promptly.
412 // We can't queue the disconnect request to the looper, as it might be
413 // queued behind a stuck read and never gets processed.
414 // Doing a disconnect outside the looper to allows the pending reads to exit
415 // (either successfully or with error).
Wei Jiac45a4b22016-04-15 15:30:23 -0700416 source->disconnect();
Chong Zhang48296b72014-09-14 14:28:45 -0700417 }
418
Lajos Molnar1d15ab52015-03-04 16:46:34 -0800419 (new AMessage(kWhatReset, this))->post();
Andreas Huber1aef2112011-01-04 14:01:29 -0800420}
421
Wei Jiae427abf2014-09-22 15:21:11 -0700422void NuPlayer::seekToAsync(int64_t seekTimeUs, bool needNotify) {
Lajos Molnar1d15ab52015-03-04 16:46:34 -0800423 sp<AMessage> msg = new AMessage(kWhatSeek, this);
Andreas Huber43c3e6c2011-01-05 12:17:08 -0800424 msg->setInt64("seekTimeUs", seekTimeUs);
Wei Jiae427abf2014-09-22 15:21:11 -0700425 msg->setInt32("needNotify", needNotify);
Andreas Huber43c3e6c2011-01-05 12:17:08 -0800426 msg->post();
427}
428
Andreas Huber53df1a42010-12-22 10:03:04 -0800429
Chong Zhang404fced2014-06-11 14:45:31 -0700430void NuPlayer::writeTrackInfo(
431 Parcel* reply, const sp<AMessage> format) const {
Marco Nelissenc367ca12015-09-15 09:51:59 -0700432 if (format == NULL) {
433 ALOGE("NULL format");
434 return;
435 }
Chong Zhang404fced2014-06-11 14:45:31 -0700436 int32_t trackType;
Marco Nelissen9436e482015-09-15 10:21:53 -0700437 if (!format->findInt32("type", &trackType)) {
Marco Nelissenc367ca12015-09-15 09:51:59 -0700438 ALOGE("no track type");
439 return;
440 }
Chong Zhang404fced2014-06-11 14:45:31 -0700441
Robert Shih755106e2015-04-30 14:36:45 -0700442 AString mime;
Robert Shih2e3a4252015-05-06 10:21:15 -0700443 if (!format->findString("mime", &mime)) {
444 // Java MediaPlayer only uses mimetype for subtitle and timedtext tracks.
445 // If we can't find the mimetype here it means that we wouldn't be needing
446 // the mimetype on the Java end. We still write a placeholder mime to keep the
447 // (de)serialization logic simple.
448 if (trackType == MEDIA_TRACK_TYPE_AUDIO) {
449 mime = "audio/";
450 } else if (trackType == MEDIA_TRACK_TYPE_VIDEO) {
451 mime = "video/";
452 } else {
Marco Nelissenc367ca12015-09-15 09:51:59 -0700453 ALOGE("unknown track type: %d", trackType);
454 return;
Robert Shih2e3a4252015-05-06 10:21:15 -0700455 }
456 }
Robert Shih755106e2015-04-30 14:36:45 -0700457
Chong Zhang404fced2014-06-11 14:45:31 -0700458 AString lang;
Marco Nelissen9436e482015-09-15 10:21:53 -0700459 if (!format->findString("language", &lang)) {
Marco Nelissenc367ca12015-09-15 09:51:59 -0700460 ALOGE("no language");
461 return;
462 }
Chong Zhang404fced2014-06-11 14:45:31 -0700463
464 reply->writeInt32(2); // write something non-zero
465 reply->writeInt32(trackType);
Robert Shih755106e2015-04-30 14:36:45 -0700466 reply->writeString16(String16(mime.c_str()));
Chong Zhang404fced2014-06-11 14:45:31 -0700467 reply->writeString16(String16(lang.c_str()));
468
469 if (trackType == MEDIA_TRACK_TYPE_SUBTITLE) {
Chong Zhang404fced2014-06-11 14:45:31 -0700470 int32_t isAuto, isDefault, isForced;
471 CHECK(format->findInt32("auto", &isAuto));
472 CHECK(format->findInt32("default", &isDefault));
473 CHECK(format->findInt32("forced", &isForced));
474
Chong Zhang404fced2014-06-11 14:45:31 -0700475 reply->writeInt32(isAuto);
476 reply->writeInt32(isDefault);
477 reply->writeInt32(isForced);
478 }
479}
480
Andreas Huberf9334412010-12-15 15:17:42 -0800481void NuPlayer::onMessageReceived(const sp<AMessage> &msg) {
482 switch (msg->what()) {
483 case kWhatSetDataSource:
484 {
Steve Block3856b092011-10-20 11:56:00 +0100485 ALOGV("kWhatSetDataSource");
Andreas Huberf9334412010-12-15 15:17:42 -0800486
487 CHECK(mSource == NULL);
488
Chong Zhang3de157d2014-08-05 20:54:44 -0700489 status_t err = OK;
Andreas Huber5bc087c2010-12-23 10:27:40 -0800490 sp<RefBase> obj;
491 CHECK(msg->findObject("source", &obj));
Chong Zhang3de157d2014-08-05 20:54:44 -0700492 if (obj != NULL) {
Wei Jiac45a4b22016-04-15 15:30:23 -0700493 Mutex::Autolock autoLock(mSourceLock);
Chong Zhang3de157d2014-08-05 20:54:44 -0700494 mSource = static_cast<Source *>(obj.get());
Chong Zhang3de157d2014-08-05 20:54:44 -0700495 } else {
496 err = UNKNOWN_ERROR;
497 }
Andreas Huber9575c962013-02-05 13:59:56 -0800498
499 CHECK(mDriver != NULL);
500 sp<NuPlayerDriver> driver = mDriver.promote();
501 if (driver != NULL) {
Chong Zhang3de157d2014-08-05 20:54:44 -0700502 driver->notifySetDataSourceCompleted(err);
Andreas Huber9575c962013-02-05 13:59:56 -0800503 }
504 break;
505 }
506
507 case kWhatPrepare:
508 {
509 mSource->prepareAsync();
Andreas Huberf9334412010-12-15 15:17:42 -0800510 break;
511 }
512
Chong Zhangdcb89b32013-08-06 09:44:47 -0700513 case kWhatGetTrackInfo:
514 {
Lajos Molnar3f274362015-03-05 14:35:41 -0800515 sp<AReplyToken> replyID;
Chong Zhangdcb89b32013-08-06 09:44:47 -0700516 CHECK(msg->senderAwaitsResponse(&replyID));
517
Chong Zhang404fced2014-06-11 14:45:31 -0700518 Parcel* reply;
519 CHECK(msg->findPointer("reply", (void**)&reply));
520
521 size_t inbandTracks = 0;
Chong Zhangdcb89b32013-08-06 09:44:47 -0700522 if (mSource != NULL) {
Chong Zhang404fced2014-06-11 14:45:31 -0700523 inbandTracks = mSource->getTrackCount();
524 }
525
Chong Zhanga7fa1d92014-06-11 14:49:23 -0700526 size_t ccTracks = 0;
527 if (mCCDecoder != NULL) {
528 ccTracks = mCCDecoder->getTrackCount();
529 }
530
Chong Zhang404fced2014-06-11 14:45:31 -0700531 // total track count
Chong Zhanga7fa1d92014-06-11 14:49:23 -0700532 reply->writeInt32(inbandTracks + ccTracks);
Chong Zhang404fced2014-06-11 14:45:31 -0700533
534 // write inband tracks
535 for (size_t i = 0; i < inbandTracks; ++i) {
536 writeTrackInfo(reply, mSource->getTrackInfo(i));
Chong Zhangdcb89b32013-08-06 09:44:47 -0700537 }
538
Chong Zhanga7fa1d92014-06-11 14:49:23 -0700539 // write CC track
540 for (size_t i = 0; i < ccTracks; ++i) {
541 writeTrackInfo(reply, mCCDecoder->getTrackInfo(i));
542 }
543
Chong Zhangdcb89b32013-08-06 09:44:47 -0700544 sp<AMessage> response = new AMessage;
Chong Zhangdcb89b32013-08-06 09:44:47 -0700545 response->postReply(replyID);
546 break;
547 }
548
Robert Shih7c4f0d72014-07-09 18:53:31 -0700549 case kWhatGetSelectedTrack:
550 {
551 status_t err = INVALID_OPERATION;
552 if (mSource != NULL) {
553 err = OK;
554
555 int32_t type32;
556 CHECK(msg->findInt32("type", (int32_t*)&type32));
557 media_track_type type = (media_track_type)type32;
558 ssize_t selectedTrack = mSource->getSelectedTrack(type);
559
560 Parcel* reply;
561 CHECK(msg->findPointer("reply", (void**)&reply));
562 reply->writeInt32(selectedTrack);
563 }
564
565 sp<AMessage> response = new AMessage;
566 response->setInt32("err", err);
567
Lajos Molnar3f274362015-03-05 14:35:41 -0800568 sp<AReplyToken> replyID;
Robert Shih7c4f0d72014-07-09 18:53:31 -0700569 CHECK(msg->senderAwaitsResponse(&replyID));
570 response->postReply(replyID);
571 break;
572 }
573
Chong Zhangdcb89b32013-08-06 09:44:47 -0700574 case kWhatSelectTrack:
575 {
Lajos Molnar3f274362015-03-05 14:35:41 -0800576 sp<AReplyToken> replyID;
Chong Zhangdcb89b32013-08-06 09:44:47 -0700577 CHECK(msg->senderAwaitsResponse(&replyID));
578
Chong Zhang404fced2014-06-11 14:45:31 -0700579 size_t trackIndex;
580 int32_t select;
Robert Shih6ffb1fd2014-10-29 16:24:32 -0700581 int64_t timeUs;
Chong Zhang404fced2014-06-11 14:45:31 -0700582 CHECK(msg->findSize("trackIndex", &trackIndex));
583 CHECK(msg->findInt32("select", &select));
Robert Shih6ffb1fd2014-10-29 16:24:32 -0700584 CHECK(msg->findInt64("timeUs", &timeUs));
Chong Zhang404fced2014-06-11 14:45:31 -0700585
Chong Zhangdcb89b32013-08-06 09:44:47 -0700586 status_t err = INVALID_OPERATION;
Chong Zhang404fced2014-06-11 14:45:31 -0700587
588 size_t inbandTracks = 0;
Chong Zhangdcb89b32013-08-06 09:44:47 -0700589 if (mSource != NULL) {
Chong Zhang404fced2014-06-11 14:45:31 -0700590 inbandTracks = mSource->getTrackCount();
591 }
Chong Zhanga7fa1d92014-06-11 14:49:23 -0700592 size_t ccTracks = 0;
593 if (mCCDecoder != NULL) {
594 ccTracks = mCCDecoder->getTrackCount();
595 }
Chong Zhang404fced2014-06-11 14:45:31 -0700596
597 if (trackIndex < inbandTracks) {
Robert Shih6ffb1fd2014-10-29 16:24:32 -0700598 err = mSource->selectTrack(trackIndex, select, timeUs);
Robert Shihd3b0bbb2014-07-23 15:00:25 -0700599
600 if (!select && err == OK) {
601 int32_t type;
602 sp<AMessage> info = mSource->getTrackInfo(trackIndex);
603 if (info != NULL
604 && info->findInt32("type", &type)
605 && type == MEDIA_TRACK_TYPE_TIMEDTEXT) {
606 ++mTimedTextGeneration;
607 }
608 }
Chong Zhanga7fa1d92014-06-11 14:49:23 -0700609 } else {
610 trackIndex -= inbandTracks;
611
612 if (trackIndex < ccTracks) {
613 err = mCCDecoder->selectTrack(trackIndex, select);
614 }
Chong Zhangdcb89b32013-08-06 09:44:47 -0700615 }
616
617 sp<AMessage> response = new AMessage;
618 response->setInt32("err", err);
619
620 response->postReply(replyID);
621 break;
622 }
623
Andreas Huberb7c8e912012-11-27 15:02:53 -0800624 case kWhatPollDuration:
625 {
626 int32_t generation;
627 CHECK(msg->findInt32("generation", &generation));
628
629 if (generation != mPollDurationGeneration) {
630 // stale
631 break;
632 }
633
634 int64_t durationUs;
635 if (mDriver != NULL && mSource->getDuration(&durationUs) == OK) {
636 sp<NuPlayerDriver> driver = mDriver.promote();
637 if (driver != NULL) {
638 driver->notifyDuration(durationUs);
639 }
640 }
641
642 msg->post(1000000ll); // poll again in a second.
643 break;
644 }
645
Lajos Molnar1de1e252015-04-30 18:18:34 -0700646 case kWhatSetVideoSurface:
Andreas Huberf9334412010-12-15 15:17:42 -0800647 {
Andreas Huberf9334412010-12-15 15:17:42 -0800648
649 sp<RefBase> obj;
Lajos Molnar1de1e252015-04-30 18:18:34 -0700650 CHECK(msg->findObject("surface", &obj));
651 sp<Surface> surface = static_cast<Surface *>(obj.get());
Lajos Molnara81c6222015-07-10 19:17:45 -0700652
653 ALOGD("onSetVideoSurface(%p, %s video decoder)",
654 surface.get(),
Wei Jia6b7d2ed2015-08-10 15:11:54 -0700655 (mSource != NULL && mStarted && mSource->getFormat(false /* audio */) != NULL
Lajos Molnara81c6222015-07-10 19:17:45 -0700656 && mVideoDecoder != NULL) ? "have" : "no");
657
Wei Jia6b7d2ed2015-08-10 15:11:54 -0700658 // Need to check mStarted before calling mSource->getFormat because NuPlayer might
659 // be in preparing state and it could take long time.
660 // When mStarted is true, mSource must have been set.
661 if (mSource == NULL || !mStarted || mSource->getFormat(false /* audio */) == NULL
Lajos Molnara81c6222015-07-10 19:17:45 -0700662 // NOTE: mVideoDecoder's mSurface is always non-null
663 || (mVideoDecoder != NULL && mVideoDecoder->setVideoSurface(surface) == OK)) {
Lajos Molnar1de1e252015-04-30 18:18:34 -0700664 performSetSurface(surface);
Wei Jiafef808d2014-10-31 17:57:05 -0700665 break;
666 }
667
668 mDeferredActions.push_back(
669 new FlushDecoderAction(FLUSH_CMD_FLUSH /* audio */,
670 FLUSH_CMD_SHUTDOWN /* video */));
671
Lajos Molnar1de1e252015-04-30 18:18:34 -0700672 mDeferredActions.push_back(new SetSurfaceAction(surface));
James Dong0d268a32012-08-31 12:18:27 -0700673
Wei Jiac6e58412015-07-10 18:04:55 -0700674 if (obj != NULL || mAudioDecoder != NULL) {
Wei Jiafef808d2014-10-31 17:57:05 -0700675 if (mStarted) {
Andy Hung73535852014-09-05 11:42:58 -0700676 // Issue a seek to refresh the video screen only if started otherwise
677 // the extractor may not yet be started and will assert.
678 // If the video decoder is not set (perhaps audio only in this case)
679 // do not perform a seek as it is not needed.
Ronghua Wua73d9e02014-10-08 15:13:29 -0700680 int64_t currentPositionUs = 0;
681 if (getCurrentPosition(&currentPositionUs) == OK) {
682 mDeferredActions.push_back(
Wei Jia29840802015-05-15 17:11:38 -0700683 new SeekAction(currentPositionUs));
Ronghua Wua73d9e02014-10-08 15:13:29 -0700684 }
Andy Hung73535852014-09-05 11:42:58 -0700685 }
Wei Jiaac428aa2014-09-02 19:01:34 -0700686
Andreas Huber57a339c2012-12-03 11:18:00 -0800687 // If there is a new surface texture, instantiate decoders
688 // again if possible.
689 mDeferredActions.push_back(
690 new SimpleAction(&NuPlayer::performScanSources));
691 }
692
Chong Zhangf8d71772014-11-26 15:08:34 -0800693 // After a flush without shutdown, decoder is paused.
694 // Don't resume it until source seek is done, otherwise it could
Chong Zhang7137ec72014-11-12 16:41:05 -0800695 // start pulling stale data too soon.
696 mDeferredActions.push_back(
Chong Zhangf8d71772014-11-26 15:08:34 -0800697 new ResumeDecoderAction(false /* needNotify */));
Chong Zhang7137ec72014-11-12 16:41:05 -0800698
Andreas Huber57a339c2012-12-03 11:18:00 -0800699 processDeferredActions();
Andreas Huberf9334412010-12-15 15:17:42 -0800700 break;
701 }
702
703 case kWhatSetAudioSink:
704 {
Steve Block3856b092011-10-20 11:56:00 +0100705 ALOGV("kWhatSetAudioSink");
Andreas Huberf9334412010-12-15 15:17:42 -0800706
707 sp<RefBase> obj;
708 CHECK(msg->findObject("sink", &obj));
709
710 mAudioSink = static_cast<MediaPlayerBase::AudioSink *>(obj.get());
711 break;
712 }
713
714 case kWhatStart:
715 {
Steve Block3856b092011-10-20 11:56:00 +0100716 ALOGV("kWhatStart");
Wei Jia94211742014-10-28 17:09:06 -0700717 if (mStarted) {
Chong Zhang8a048332015-05-06 15:16:28 -0700718 // do not resume yet if the source is still buffering
719 if (!mPausedForBuffering) {
720 onResume();
721 }
Wei Jia94211742014-10-28 17:09:06 -0700722 } else {
723 onStart();
Lajos Molnar09524832014-07-17 14:29:51 -0700724 }
Chong Zhangefbb6192015-01-30 17:13:27 -0800725 mPausedByClient = false;
Wei Jia71c75e02016-02-04 09:40:47 -0800726 if (mPendingBufferingFlag != PENDING_BUFFERING_FLAG_NONE) {
727 notifyListener(MEDIA_INFO, mPendingBufferingFlag, 0);
728 mPendingBufferingFlag = PENDING_BUFFERING_FLAG_NONE;
729 }
Andreas Huberf9334412010-12-15 15:17:42 -0800730 break;
731 }
732
Lajos Molnar3a474aa2015-04-24 17:10:07 -0700733 case kWhatConfigPlayback:
Wei Jia98160162015-02-04 17:01:11 -0800734 {
Lajos Molnar3a474aa2015-04-24 17:10:07 -0700735 sp<AReplyToken> replyID;
736 CHECK(msg->senderAwaitsResponse(&replyID));
737 AudioPlaybackRate rate /* sanitized */;
738 readFromAMessage(msg, &rate);
739 status_t err = OK;
Wei Jia98160162015-02-04 17:01:11 -0800740 if (mRenderer != NULL) {
Wei Jiabf70feb2016-02-19 15:47:16 -0800741 // AudioSink allows only 1.f and 0.f for offload mode.
742 // For other speed, switch to non-offload mode.
743 if (mOffloadAudio && ((rate.mSpeed != 0.f && rate.mSpeed != 1.f)
744 || rate.mPitch != 1.f)) {
745 int64_t currentPositionUs;
746 if (getCurrentPosition(&currentPositionUs) != OK) {
747 currentPositionUs = mPreviousSeekTimeUs;
748 }
749
750 // Set mPlaybackSettings so that the new audio decoder can
751 // be created correctly.
752 mPlaybackSettings = rate;
753 if (!mPaused) {
754 mRenderer->pause();
755 }
Wei Jia5031b2f2016-02-25 11:19:31 -0800756 restartAudio(
Wei Jiabf70feb2016-02-19 15:47:16 -0800757 currentPositionUs, true /* forceNonOffload */,
758 true /* needsToCreateAudioDecoder */);
759 if (!mPaused) {
760 mRenderer->resume();
761 }
762 }
763
Lajos Molnar3a474aa2015-04-24 17:10:07 -0700764 err = mRenderer->setPlaybackSettings(rate);
765 }
766 if (err == OK) {
767 if (rate.mSpeed == 0.f) {
768 onPause();
Wei Jia351ce872016-02-10 13:21:59 -0800769 mPausedByClient = true;
Lajos Molnar3a474aa2015-04-24 17:10:07 -0700770 // save all other settings (using non-paused speed)
771 // so we can restore them on start
772 AudioPlaybackRate newRate = rate;
773 newRate.mSpeed = mPlaybackSettings.mSpeed;
774 mPlaybackSettings = newRate;
775 } else { /* rate.mSpeed != 0.f */
776 onResume();
Wei Jia351ce872016-02-10 13:21:59 -0800777 mPausedByClient = false;
Lajos Molnar3a474aa2015-04-24 17:10:07 -0700778 mPlaybackSettings = rate;
779 }
Wei Jia98160162015-02-04 17:01:11 -0800780 }
Ronghua Wu8db88132015-04-22 13:51:35 -0700781
782 if (mVideoDecoder != NULL) {
Ronghua Wuc8a70d32015-04-29 16:26:34 -0700783 float rate = getFrameRate();
784 if (rate > 0) {
Ronghua Wu8db88132015-04-22 13:51:35 -0700785 sp<AMessage> params = new AMessage();
Lajos Molnar3a474aa2015-04-24 17:10:07 -0700786 params->setFloat("operating-rate", rate * mPlaybackSettings.mSpeed);
Ronghua Wu8db88132015-04-22 13:51:35 -0700787 mVideoDecoder->setParameters(params);
788 }
789 }
790
Lajos Molnar3a474aa2015-04-24 17:10:07 -0700791 sp<AMessage> response = new AMessage;
792 response->setInt32("err", err);
793 response->postReply(replyID);
794 break;
795 }
796
797 case kWhatGetPlaybackSettings:
798 {
799 sp<AReplyToken> replyID;
800 CHECK(msg->senderAwaitsResponse(&replyID));
801 AudioPlaybackRate rate = mPlaybackSettings;
802 status_t err = OK;
803 if (mRenderer != NULL) {
804 err = mRenderer->getPlaybackSettings(&rate);
805 }
806 if (err == OK) {
807 // get playback settings used by renderer, as it may be
808 // slightly off due to audiosink not taking small changes.
809 mPlaybackSettings = rate;
810 if (mPaused) {
811 rate.mSpeed = 0.f;
812 }
813 }
814 sp<AMessage> response = new AMessage;
815 if (err == OK) {
816 writeToAMessage(response, rate);
817 }
818 response->setInt32("err", err);
819 response->postReply(replyID);
820 break;
821 }
822
823 case kWhatConfigSync:
824 {
825 sp<AReplyToken> replyID;
826 CHECK(msg->senderAwaitsResponse(&replyID));
827
828 ALOGV("kWhatConfigSync");
829 AVSyncSettings sync;
830 float videoFpsHint;
831 readFromAMessage(msg, &sync, &videoFpsHint);
832 status_t err = OK;
833 if (mRenderer != NULL) {
834 err = mRenderer->setSyncSettings(sync, videoFpsHint);
835 }
836 if (err == OK) {
837 mSyncSettings = sync;
838 mVideoFpsHint = videoFpsHint;
839 }
840 sp<AMessage> response = new AMessage;
841 response->setInt32("err", err);
842 response->postReply(replyID);
843 break;
844 }
845
846 case kWhatGetSyncSettings:
847 {
848 sp<AReplyToken> replyID;
849 CHECK(msg->senderAwaitsResponse(&replyID));
850 AVSyncSettings sync = mSyncSettings;
851 float videoFps = mVideoFpsHint;
852 status_t err = OK;
853 if (mRenderer != NULL) {
854 err = mRenderer->getSyncSettings(&sync, &videoFps);
855 if (err == OK) {
856 mSyncSettings = sync;
857 mVideoFpsHint = videoFps;
858 }
859 }
860 sp<AMessage> response = new AMessage;
861 if (err == OK) {
862 writeToAMessage(response, sync, videoFps);
863 }
864 response->setInt32("err", err);
865 response->postReply(replyID);
Wei Jia98160162015-02-04 17:01:11 -0800866 break;
867 }
868
Andreas Huberf9334412010-12-15 15:17:42 -0800869 case kWhatScanSources:
870 {
Andreas Huber1aef2112011-01-04 14:01:29 -0800871 int32_t generation;
872 CHECK(msg->findInt32("generation", &generation));
873 if (generation != mScanSourcesGeneration) {
874 // Drop obsolete msg.
875 break;
876 }
877
Andreas Huber5bc087c2010-12-23 10:27:40 -0800878 mScanSourcesPending = false;
879
Steve Block3856b092011-10-20 11:56:00 +0100880 ALOGV("scanning sources haveAudio=%d, haveVideo=%d",
Andreas Huber43c3e6c2011-01-05 12:17:08 -0800881 mAudioDecoder != NULL, mVideoDecoder != NULL);
882
Andreas Huberb7c8e912012-11-27 15:02:53 -0800883 bool mHadAnySourcesBefore =
884 (mAudioDecoder != NULL) || (mVideoDecoder != NULL);
Robert Shih7350b052015-10-01 15:50:14 -0700885 bool rescan = false;
Andreas Huberb7c8e912012-11-27 15:02:53 -0800886
Andy Hung282a7e32014-08-14 15:56:34 -0700887 // initialize video before audio because successful initialization of
888 // video may change deep buffer mode of audio.
Lajos Molnar1de1e252015-04-30 18:18:34 -0700889 if (mSurface != NULL) {
Robert Shih7350b052015-10-01 15:50:14 -0700890 if (instantiateDecoder(false, &mVideoDecoder) == -EWOULDBLOCK) {
891 rescan = true;
892 }
Haynes Mathew George5d246ef2012-07-09 10:36:57 -0700893 }
Andreas Huberf9334412010-12-15 15:17:42 -0800894
Ronghua Wua10fd232014-11-06 16:15:20 -0800895 // Don't try to re-open audio sink if there's an existing decoder.
896 if (mAudioSink != NULL && mAudioDecoder == NULL) {
Robert Shih7350b052015-10-01 15:50:14 -0700897 if (instantiateDecoder(true, &mAudioDecoder) == -EWOULDBLOCK) {
898 rescan = true;
899 }
Andreas Huberf9334412010-12-15 15:17:42 -0800900 }
901
Andreas Huberb7c8e912012-11-27 15:02:53 -0800902 if (!mHadAnySourcesBefore
903 && (mAudioDecoder != NULL || mVideoDecoder != NULL)) {
904 // This is the first time we've found anything playable.
905
Andreas Huber9575c962013-02-05 13:59:56 -0800906 if (mSourceFlags & Source::FLAG_DYNAMIC_DURATION) {
Andreas Huberb7c8e912012-11-27 15:02:53 -0800907 schedulePollDuration();
908 }
909 }
910
Andreas Hubereac68ba2011-09-27 12:12:25 -0700911 status_t err;
912 if ((err = mSource->feedMoreTSData()) != OK) {
Andreas Huber1aef2112011-01-04 14:01:29 -0800913 if (mAudioDecoder == NULL && mVideoDecoder == NULL) {
914 // We're not currently decoding anything (no audio or
915 // video tracks found) and we just ran out of input data.
Andreas Hubereac68ba2011-09-27 12:12:25 -0700916
917 if (err == ERROR_END_OF_STREAM) {
918 notifyListener(MEDIA_PLAYBACK_COMPLETE, 0, 0);
919 } else {
920 notifyListener(MEDIA_ERROR, MEDIA_ERROR_UNKNOWN, err);
921 }
Andreas Huber1aef2112011-01-04 14:01:29 -0800922 }
Andreas Huberf9334412010-12-15 15:17:42 -0800923 break;
924 }
925
Robert Shih7350b052015-10-01 15:50:14 -0700926 if (rescan) {
Andreas Huberf9334412010-12-15 15:17:42 -0800927 msg->post(100000ll);
Andreas Huber5bc087c2010-12-23 10:27:40 -0800928 mScanSourcesPending = true;
Andreas Huberf9334412010-12-15 15:17:42 -0800929 }
930 break;
931 }
932
933 case kWhatVideoNotify:
934 case kWhatAudioNotify:
935 {
936 bool audio = msg->what() == kWhatAudioNotify;
937
Wei Jia88703c32014-08-06 11:24:07 -0700938 int32_t currentDecoderGeneration =
939 (audio? mAudioDecoderGeneration : mVideoDecoderGeneration);
940 int32_t requesterGeneration = currentDecoderGeneration - 1;
941 CHECK(msg->findInt32("generation", &requesterGeneration));
942
943 if (requesterGeneration != currentDecoderGeneration) {
944 ALOGV("got message from old %s decoder, generation(%d:%d)",
945 audio ? "audio" : "video", requesterGeneration,
946 currentDecoderGeneration);
947 sp<AMessage> reply;
948 if (!(msg->findMessage("reply", &reply))) {
949 return;
950 }
951
952 reply->setInt32("err", INFO_DISCONTINUITY);
953 reply->post();
954 return;
955 }
956
Andreas Huberf9334412010-12-15 15:17:42 -0800957 int32_t what;
Lajos Molnar1cd13982014-01-17 15:12:51 -0800958 CHECK(msg->findInt32("what", &what));
Andreas Huberf9334412010-12-15 15:17:42 -0800959
Chong Zhang7137ec72014-11-12 16:41:05 -0800960 if (what == DecoderBase::kWhatInputDiscontinuity) {
961 int32_t formatChange;
962 CHECK(msg->findInt32("formatChange", &formatChange));
Andreas Huberf9334412010-12-15 15:17:42 -0800963
Chong Zhang7137ec72014-11-12 16:41:05 -0800964 ALOGV("%s discontinuity: formatChange %d",
965 audio ? "audio" : "video", formatChange);
966
967 if (formatChange) {
968 mDeferredActions.push_back(
969 new FlushDecoderAction(
970 audio ? FLUSH_CMD_SHUTDOWN : FLUSH_CMD_NONE,
971 audio ? FLUSH_CMD_NONE : FLUSH_CMD_SHUTDOWN));
Andreas Huberf9334412010-12-15 15:17:42 -0800972 }
Chong Zhang7137ec72014-11-12 16:41:05 -0800973
974 mDeferredActions.push_back(
975 new SimpleAction(
976 &NuPlayer::performScanSources));
977
978 processDeferredActions();
979 } else if (what == DecoderBase::kWhatEOS) {
Andreas Huberdc9bacd2011-09-26 10:53:29 -0700980 int32_t err;
Lajos Molnar1cd13982014-01-17 15:12:51 -0800981 CHECK(msg->findInt32("err", &err));
Andreas Huberdc9bacd2011-09-26 10:53:29 -0700982
983 if (err == ERROR_END_OF_STREAM) {
Steve Block3856b092011-10-20 11:56:00 +0100984 ALOGV("got %s decoder EOS", audio ? "audio" : "video");
Andreas Huberdc9bacd2011-09-26 10:53:29 -0700985 } else {
Steve Block3856b092011-10-20 11:56:00 +0100986 ALOGV("got %s decoder EOS w/ error %d",
Andreas Huberdc9bacd2011-09-26 10:53:29 -0700987 audio ? "audio" : "video",
988 err);
989 }
990
991 mRenderer->queueEOS(audio, err);
Chong Zhang7137ec72014-11-12 16:41:05 -0800992 } else if (what == DecoderBase::kWhatFlushCompleted) {
Steve Block3856b092011-10-20 11:56:00 +0100993 ALOGV("decoder %s flush completed", audio ? "audio" : "video");
Andreas Huberf9334412010-12-15 15:17:42 -0800994
Andy Hung8d121d42014-10-03 09:53:53 -0700995 handleFlushComplete(audio, true /* isDecoder */);
Andreas Huber3831a062010-12-21 10:22:33 -0800996 finishFlushIfPossible();
Chong Zhang7137ec72014-11-12 16:41:05 -0800997 } else if (what == DecoderBase::kWhatVideoSizeChanged) {
Lajos Molnar1cd13982014-01-17 15:12:51 -0800998 sp<AMessage> format;
999 CHECK(msg->findMessage("format", &format));
1000
Wei Jiac6cfd702014-11-11 16:33:20 -08001001 sp<AMessage> inputFormat =
1002 mSource->getFormat(false /* audio */);
Andreas Huber3831a062010-12-21 10:22:33 -08001003
Wei Jiac6cfd702014-11-11 16:33:20 -08001004 updateVideoSize(inputFormat, format);
Chong Zhang7137ec72014-11-12 16:41:05 -08001005 } else if (what == DecoderBase::kWhatShutdownCompleted) {
Steve Block3856b092011-10-20 11:56:00 +01001006 ALOGV("%s shutdown completed", audio ? "audio" : "video");
Andreas Huber3831a062010-12-21 10:22:33 -08001007 if (audio) {
1008 mAudioDecoder.clear();
Wei Jia57568df2014-09-22 10:16:29 -07001009 ++mAudioDecoderGeneration;
Andreas Huber3831a062010-12-21 10:22:33 -08001010
1011 CHECK_EQ((int)mFlushingAudio, (int)SHUTTING_DOWN_DECODER);
1012 mFlushingAudio = SHUT_DOWN;
1013 } else {
1014 mVideoDecoder.clear();
Wei Jia57568df2014-09-22 10:16:29 -07001015 ++mVideoDecoderGeneration;
Andreas Huber3831a062010-12-21 10:22:33 -08001016
1017 CHECK_EQ((int)mFlushingVideo, (int)SHUTTING_DOWN_DECODER);
1018 mFlushingVideo = SHUT_DOWN;
1019 }
1020
1021 finishFlushIfPossible();
Chong Zhangf8d71772014-11-26 15:08:34 -08001022 } else if (what == DecoderBase::kWhatResumeCompleted) {
1023 finishResume();
Chong Zhang7137ec72014-11-12 16:41:05 -08001024 } else if (what == DecoderBase::kWhatError) {
Chong Zhangf4c0a942014-08-11 15:14:10 -07001025 status_t err;
Andy Hung2abde2c2014-09-30 14:40:32 -07001026 if (!msg->findInt32("err", &err) || err == OK) {
Chong Zhangf4c0a942014-08-11 15:14:10 -07001027 err = UNKNOWN_ERROR;
1028 }
Andy Hungcf31f1e2014-09-23 14:59:01 -07001029
Andy Hung2abde2c2014-09-30 14:40:32 -07001030 // Decoder errors can be due to Source (e.g. from streaming),
1031 // or from decoding corrupted bitstreams, or from other decoder
1032 // MediaCodec operations (e.g. from an ongoing reset or seek).
Andy Hung202bce12014-12-03 11:47:36 -08001033 // They may also be due to openAudioSink failure at
1034 // decoder start or after a format change.
Andy Hung2abde2c2014-09-30 14:40:32 -07001035 //
1036 // We try to gracefully shut down the affected decoder if possible,
1037 // rather than trying to force the shutdown with something
1038 // similar to performReset(). This method can lead to a hang
1039 // if MediaCodec functions block after an error, but they should
1040 // typically return INVALID_OPERATION instead of blocking.
1041
1042 FlushStatus *flushing = audio ? &mFlushingAudio : &mFlushingVideo;
1043 ALOGE("received error(%#x) from %s decoder, flushing(%d), now shutting down",
1044 err, audio ? "audio" : "video", *flushing);
1045
1046 switch (*flushing) {
1047 case NONE:
1048 mDeferredActions.push_back(
Wei Jiafef808d2014-10-31 17:57:05 -07001049 new FlushDecoderAction(
1050 audio ? FLUSH_CMD_SHUTDOWN : FLUSH_CMD_NONE,
1051 audio ? FLUSH_CMD_NONE : FLUSH_CMD_SHUTDOWN));
Andy Hung2abde2c2014-09-30 14:40:32 -07001052 processDeferredActions();
1053 break;
1054 case FLUSHING_DECODER:
1055 *flushing = FLUSHING_DECODER_SHUTDOWN; // initiate shutdown after flush.
1056 break; // Wait for flush to complete.
1057 case FLUSHING_DECODER_SHUTDOWN:
1058 break; // Wait for flush to complete.
1059 case SHUTTING_DOWN_DECODER:
1060 break; // Wait for shutdown to complete.
1061 case FLUSHED:
1062 // Widevine source reads must stop before releasing the video decoder.
1063 if (!audio && mSource != NULL && mSourceFlags & Source::FLAG_SECURE) {
1064 mSource->stop();
Robert Shih0c61a0d2015-07-06 15:09:10 -07001065 mSourceStarted = false;
Andy Hung2abde2c2014-09-30 14:40:32 -07001066 }
1067 getDecoder(audio)->initiateShutdown(); // In the middle of a seek.
1068 *flushing = SHUTTING_DOWN_DECODER; // Shut down.
1069 break;
1070 case SHUT_DOWN:
1071 finishFlushIfPossible(); // Should not occur.
1072 break; // Finish anyways.
Marco Nelissen9e2b7912014-08-18 16:13:03 -07001073 }
Andy Hung2abde2c2014-09-30 14:40:32 -07001074 notifyListener(MEDIA_ERROR, MEDIA_ERROR_UNKNOWN, err);
Lajos Molnar1cd13982014-01-17 15:12:51 -08001075 } else {
1076 ALOGV("Unhandled decoder notification %d '%c%c%c%c'.",
Andreas Hubera1f8ab02012-11-30 10:53:22 -08001077 what,
1078 what >> 24,
1079 (what >> 16) & 0xff,
1080 (what >> 8) & 0xff,
1081 what & 0xff);
Andreas Huberf9334412010-12-15 15:17:42 -08001082 }
1083
1084 break;
1085 }
1086
1087 case kWhatRendererNotify:
1088 {
Wei Jia57568df2014-09-22 10:16:29 -07001089 int32_t requesterGeneration = mRendererGeneration - 1;
1090 CHECK(msg->findInt32("generation", &requesterGeneration));
1091 if (requesterGeneration != mRendererGeneration) {
1092 ALOGV("got message from old renderer, generation(%d:%d)",
1093 requesterGeneration, mRendererGeneration);
1094 return;
1095 }
1096
Andreas Huberf9334412010-12-15 15:17:42 -08001097 int32_t what;
1098 CHECK(msg->findInt32("what", &what));
1099
1100 if (what == Renderer::kWhatEOS) {
1101 int32_t audio;
1102 CHECK(msg->findInt32("audio", &audio));
1103
Andreas Huberc92fd242011-08-16 13:48:44 -07001104 int32_t finalResult;
1105 CHECK(msg->findInt32("finalResult", &finalResult));
1106
Andreas Huberf9334412010-12-15 15:17:42 -08001107 if (audio) {
1108 mAudioEOS = true;
1109 } else {
1110 mVideoEOS = true;
1111 }
1112
Andreas Huberc92fd242011-08-16 13:48:44 -07001113 if (finalResult == ERROR_END_OF_STREAM) {
Steve Block3856b092011-10-20 11:56:00 +01001114 ALOGV("reached %s EOS", audio ? "audio" : "video");
Andreas Huberc92fd242011-08-16 13:48:44 -07001115 } else {
Steve Block29357bc2012-01-06 19:20:56 +00001116 ALOGE("%s track encountered an error (%d)",
Andreas Huberc92fd242011-08-16 13:48:44 -07001117 audio ? "audio" : "video", finalResult);
1118
1119 notifyListener(
1120 MEDIA_ERROR, MEDIA_ERROR_UNKNOWN, finalResult);
1121 }
Andreas Huberf9334412010-12-15 15:17:42 -08001122
1123 if ((mAudioEOS || mAudioDecoder == NULL)
1124 && (mVideoEOS || mVideoDecoder == NULL)) {
1125 notifyListener(MEDIA_PLAYBACK_COMPLETE, 0, 0);
1126 }
Andreas Huber3fe62152011-09-16 15:09:22 -07001127 } else if (what == Renderer::kWhatFlushComplete) {
Andreas Huberf9334412010-12-15 15:17:42 -08001128 int32_t audio;
1129 CHECK(msg->findInt32("audio", &audio));
1130
Wei Jia3261f0d2015-10-08 13:58:33 -07001131 if (audio) {
1132 mAudioEOS = false;
1133 } else {
1134 mVideoEOS = false;
1135 }
1136
Steve Block3856b092011-10-20 11:56:00 +01001137 ALOGV("renderer %s flush completed.", audio ? "audio" : "video");
Wei Jiada4252f2015-07-14 18:15:28 -07001138 if (audio && (mFlushingAudio == NONE || mFlushingAudio == FLUSHED
1139 || mFlushingAudio == SHUT_DOWN)) {
1140 // Flush has been handled by tear down.
1141 break;
1142 }
Andy Hung8d121d42014-10-03 09:53:53 -07001143 handleFlushComplete(audio, false /* isDecoder */);
1144 finishFlushIfPossible();
James Dongf57b4ea2012-07-20 13:38:36 -07001145 } else if (what == Renderer::kWhatVideoRenderingStart) {
1146 notifyListener(MEDIA_INFO, MEDIA_INFO_RENDERING_START, 0);
Lajos Molnarcbaffcf2013-08-14 18:30:38 -07001147 } else if (what == Renderer::kWhatMediaRenderingStart) {
1148 ALOGV("media rendering started");
1149 notifyListener(MEDIA_STARTED, 0, 0);
Ronghua Wufaeb0f22015-05-21 12:20:21 -07001150 } else if (what == Renderer::kWhatAudioTearDown) {
Ronghua Wu08529172014-10-02 16:55:52 -07001151 int32_t reason;
1152 CHECK(msg->findInt32("reason", &reason));
Ronghua Wufaeb0f22015-05-21 12:20:21 -07001153 ALOGV("Tear down audio with reason %d.", reason);
Wei Jia8456ddd2016-04-22 14:46:43 -07001154 if (reason == Renderer::kDueToTimeout && !(mPaused && mOffloadAudio)) {
1155 // TimeoutWhenPaused is only for offload mode.
1156 ALOGW("Receive a stale message for teardown.");
1157 break;
1158 }
Wei Jiada4252f2015-07-14 18:15:28 -07001159 int64_t positionUs;
Robert Shih1a5c8592015-08-04 18:07:44 -07001160 if (!msg->findInt64("positionUs", &positionUs)) {
1161 positionUs = mPreviousSeekTimeUs;
1162 }
Ronghua Wufaeb0f22015-05-21 12:20:21 -07001163
Wei Jia5031b2f2016-02-25 11:19:31 -08001164 restartAudio(
Wei Jiaa05f1e32016-03-25 16:31:22 -07001165 positionUs, reason == Renderer::kForceNonOffload /* forceNonOffload */,
1166 reason != Renderer::kDueToTimeout /* needsToCreateAudioDecoder */);
Andreas Huberf9334412010-12-15 15:17:42 -08001167 }
1168 break;
1169 }
1170
1171 case kWhatMoreDataQueued:
1172 {
1173 break;
1174 }
1175
Andreas Huber1aef2112011-01-04 14:01:29 -08001176 case kWhatReset:
1177 {
Steve Block3856b092011-10-20 11:56:00 +01001178 ALOGV("kWhatReset");
Andreas Huber1aef2112011-01-04 14:01:29 -08001179
Ronghua Wu64c2d172015-10-07 16:52:19 -07001180 mResetting = true;
1181
Andreas Hubera1f8ab02012-11-30 10:53:22 -08001182 mDeferredActions.push_back(
Wei Jiafef808d2014-10-31 17:57:05 -07001183 new FlushDecoderAction(
1184 FLUSH_CMD_SHUTDOWN /* audio */,
1185 FLUSH_CMD_SHUTDOWN /* video */));
Andreas Huberb7c8e912012-11-27 15:02:53 -08001186
Andreas Hubera1f8ab02012-11-30 10:53:22 -08001187 mDeferredActions.push_back(
1188 new SimpleAction(&NuPlayer::performReset));
Andreas Huberb58ce9f2011-11-28 16:27:35 -08001189
Andreas Hubera1f8ab02012-11-30 10:53:22 -08001190 processDeferredActions();
Andreas Huber1aef2112011-01-04 14:01:29 -08001191 break;
1192 }
1193
Andreas Huber43c3e6c2011-01-05 12:17:08 -08001194 case kWhatSeek:
1195 {
1196 int64_t seekTimeUs;
Wei Jiae427abf2014-09-22 15:21:11 -07001197 int32_t needNotify;
Andreas Huber43c3e6c2011-01-05 12:17:08 -08001198 CHECK(msg->findInt64("seekTimeUs", &seekTimeUs));
Wei Jiae427abf2014-09-22 15:21:11 -07001199 CHECK(msg->findInt32("needNotify", &needNotify));
Andreas Huber43c3e6c2011-01-05 12:17:08 -08001200
Wei Jiae427abf2014-09-22 15:21:11 -07001201 ALOGV("kWhatSeek seekTimeUs=%lld us, needNotify=%d",
Lajos Molnar6d339f12015-04-17 16:15:53 -07001202 (long long)seekTimeUs, needNotify);
Andreas Huber43c3e6c2011-01-05 12:17:08 -08001203
Wei Jia1061c9c2015-05-19 16:02:17 -07001204 if (!mStarted) {
1205 // Seek before the player is started. In order to preview video,
1206 // need to start the player and pause it. This branch is called
1207 // only once if needed. After the player is started, any seek
1208 // operation will go through normal path.
Robert Shih0c61a0d2015-07-06 15:09:10 -07001209 // Audio-only cases are handled separately.
Wei Jia1061c9c2015-05-19 16:02:17 -07001210 onStart(seekTimeUs);
Robert Shih0c61a0d2015-07-06 15:09:10 -07001211 if (mStarted) {
1212 onPause();
1213 mPausedByClient = true;
1214 }
Wei Jia1061c9c2015-05-19 16:02:17 -07001215 if (needNotify) {
1216 notifyDriverSeekComplete();
1217 }
1218 break;
1219 }
1220
Wei Jia351ce872016-02-10 13:21:59 -08001221 mPendingBufferingFlag = PENDING_BUFFERING_FLAG_NONE;
1222
Andreas Hubera1f8ab02012-11-30 10:53:22 -08001223 mDeferredActions.push_back(
Wei Jiafef808d2014-10-31 17:57:05 -07001224 new FlushDecoderAction(FLUSH_CMD_FLUSH /* audio */,
1225 FLUSH_CMD_FLUSH /* video */));
Andreas Huber43c3e6c2011-01-05 12:17:08 -08001226
Wei Jiae427abf2014-09-22 15:21:11 -07001227 mDeferredActions.push_back(
Wei Jia29840802015-05-15 17:11:38 -07001228 new SeekAction(seekTimeUs));
Andreas Huber43c3e6c2011-01-05 12:17:08 -08001229
Chong Zhangf8d71772014-11-26 15:08:34 -08001230 // After a flush without shutdown, decoder is paused.
1231 // Don't resume it until source seek is done, otherwise it could
Chong Zhang7137ec72014-11-12 16:41:05 -08001232 // start pulling stale data too soon.
1233 mDeferredActions.push_back(
Chong Zhangf8d71772014-11-26 15:08:34 -08001234 new ResumeDecoderAction(needNotify));
Chong Zhang7137ec72014-11-12 16:41:05 -08001235
Andreas Hubera1f8ab02012-11-30 10:53:22 -08001236 processDeferredActions();
Andreas Huber43c3e6c2011-01-05 12:17:08 -08001237 break;
1238 }
1239
Andreas Huberb4082222011-01-20 15:23:04 -08001240 case kWhatPause:
1241 {
Chong Zhangefbb6192015-01-30 17:13:27 -08001242 onPause();
1243 mPausedByClient = true;
Andreas Huberb4082222011-01-20 15:23:04 -08001244 break;
1245 }
1246
Andreas Huberb5f25f02013-02-05 10:14:26 -08001247 case kWhatSourceNotify:
1248 {
Andreas Huber9575c962013-02-05 13:59:56 -08001249 onSourceNotify(msg);
Andreas Huberb5f25f02013-02-05 10:14:26 -08001250 break;
1251 }
1252
Chong Zhanga7fa1d92014-06-11 14:49:23 -07001253 case kWhatClosedCaptionNotify:
1254 {
1255 onClosedCaptionNotify(msg);
1256 break;
1257 }
1258
Andreas Huberf9334412010-12-15 15:17:42 -08001259 default:
1260 TRESPASS();
1261 break;
1262 }
1263}
1264
Wei Jia94211742014-10-28 17:09:06 -07001265void NuPlayer::onResume() {
Ronghua Wu64c2d172015-10-07 16:52:19 -07001266 if (!mPaused || mResetting) {
1267 ALOGD_IF(mResetting, "resetting, onResume discarded");
Chong Zhangefbb6192015-01-30 17:13:27 -08001268 return;
1269 }
1270 mPaused = false;
Wei Jia94211742014-10-28 17:09:06 -07001271 if (mSource != NULL) {
1272 mSource->resume();
1273 } else {
1274 ALOGW("resume called when source is gone or not set");
1275 }
1276 // |mAudioDecoder| may have been released due to the pause timeout, so re-create it if
1277 // needed.
1278 if (audioDecoderStillNeeded() && mAudioDecoder == NULL) {
1279 instantiateDecoder(true /* audio */, &mAudioDecoder);
1280 }
1281 if (mRenderer != NULL) {
1282 mRenderer->resume();
1283 } else {
1284 ALOGW("resume called when renderer is gone or not set");
1285 }
1286}
1287
Lajos Molnarfcd3e942015-03-31 10:06:48 -07001288status_t NuPlayer::onInstantiateSecureDecoders() {
1289 status_t err;
1290 if (!(mSourceFlags & Source::FLAG_SECURE)) {
1291 return BAD_TYPE;
1292 }
1293
1294 if (mRenderer != NULL) {
1295 ALOGE("renderer should not be set when instantiating secure decoders");
1296 return UNKNOWN_ERROR;
1297 }
1298
1299 // TRICKY: We rely on mRenderer being null, so that decoder does not start requesting
1300 // data on instantiation.
Lajos Molnar1de1e252015-04-30 18:18:34 -07001301 if (mSurface != NULL) {
Lajos Molnarfcd3e942015-03-31 10:06:48 -07001302 err = instantiateDecoder(false, &mVideoDecoder);
1303 if (err != OK) {
1304 return err;
1305 }
1306 }
1307
1308 if (mAudioSink != NULL) {
1309 err = instantiateDecoder(true, &mAudioDecoder);
1310 if (err != OK) {
1311 return err;
1312 }
1313 }
1314 return OK;
1315}
1316
Wei Jia1061c9c2015-05-19 16:02:17 -07001317void NuPlayer::onStart(int64_t startPositionUs) {
Robert Shih0c61a0d2015-07-06 15:09:10 -07001318 if (!mSourceStarted) {
1319 mSourceStarted = true;
1320 mSource->start();
1321 }
1322 if (startPositionUs > 0) {
1323 performSeek(startPositionUs);
1324 if (mSource->getFormat(false /* audio */) == NULL) {
1325 return;
1326 }
1327 }
1328
Wei Jia94211742014-10-28 17:09:06 -07001329 mOffloadAudio = false;
1330 mAudioEOS = false;
1331 mVideoEOS = false;
Wei Jia94211742014-10-28 17:09:06 -07001332 mStarted = true;
1333
Wei Jia94211742014-10-28 17:09:06 -07001334 uint32_t flags = 0;
1335
1336 if (mSource->isRealTime()) {
1337 flags |= Renderer::FLAG_REAL_TIME;
1338 }
1339
1340 sp<MetaData> audioMeta = mSource->getFormatMeta(true /* audio */);
Andy Hung12a84132015-10-20 16:09:21 -07001341 ALOGV_IF(audioMeta == NULL, "no metadata for audio source"); // video only stream
Wei Jia94211742014-10-28 17:09:06 -07001342 audio_stream_type_t streamType = AUDIO_STREAM_MUSIC;
1343 if (mAudioSink != NULL) {
1344 streamType = mAudioSink->getAudioStreamType();
1345 }
1346
1347 sp<AMessage> videoFormat = mSource->getFormat(false /* audio */);
1348
1349 mOffloadAudio =
Wei Jiabf70feb2016-02-19 15:47:16 -08001350 canOffloadStream(audioMeta, (videoFormat != NULL), mSource->isStreaming(), streamType)
1351 && (mPlaybackSettings.mSpeed == 1.f && mPlaybackSettings.mPitch == 1.f);
Wei Jia94211742014-10-28 17:09:06 -07001352 if (mOffloadAudio) {
1353 flags |= Renderer::FLAG_OFFLOAD_AUDIO;
1354 }
1355
Lajos Molnar1d15ab52015-03-04 16:46:34 -08001356 sp<AMessage> notify = new AMessage(kWhatRendererNotify, this);
Wei Jia94211742014-10-28 17:09:06 -07001357 ++mRendererGeneration;
1358 notify->setInt32("generation", mRendererGeneration);
1359 mRenderer = new Renderer(mAudioSink, notify, flags);
Wei Jia94211742014-10-28 17:09:06 -07001360 mRendererLooper = new ALooper;
1361 mRendererLooper->setName("NuPlayerRenderer");
1362 mRendererLooper->start(false, false, ANDROID_PRIORITY_AUDIO);
1363 mRendererLooper->registerHandler(mRenderer);
Lajos Molnar3a474aa2015-04-24 17:10:07 -07001364
1365 status_t err = mRenderer->setPlaybackSettings(mPlaybackSettings);
1366 if (err != OK) {
1367 mSource->stop();
Robert Shih0c61a0d2015-07-06 15:09:10 -07001368 mSourceStarted = false;
Lajos Molnar3a474aa2015-04-24 17:10:07 -07001369 notifyListener(MEDIA_ERROR, MEDIA_ERROR_UNKNOWN, err);
1370 return;
Wei Jiac8206ff2015-03-04 13:59:37 -08001371 }
Wei Jia94211742014-10-28 17:09:06 -07001372
Ronghua Wuc8a70d32015-04-29 16:26:34 -07001373 float rate = getFrameRate();
1374 if (rate > 0) {
Wei Jia94211742014-10-28 17:09:06 -07001375 mRenderer->setVideoFrameRate(rate);
1376 }
1377
Wei Jiac6cfd702014-11-11 16:33:20 -08001378 if (mVideoDecoder != NULL) {
1379 mVideoDecoder->setRenderer(mRenderer);
1380 }
1381 if (mAudioDecoder != NULL) {
1382 mAudioDecoder->setRenderer(mRenderer);
1383 }
1384
Wei Jia94211742014-10-28 17:09:06 -07001385 postScanSources();
1386}
1387
Chong Zhangefbb6192015-01-30 17:13:27 -08001388void NuPlayer::onPause() {
1389 if (mPaused) {
1390 return;
1391 }
1392 mPaused = true;
1393 if (mSource != NULL) {
1394 mSource->pause();
1395 } else {
1396 ALOGW("pause called when source is gone or not set");
1397 }
1398 if (mRenderer != NULL) {
1399 mRenderer->pause();
1400 } else {
1401 ALOGW("pause called when renderer is gone or not set");
1402 }
1403}
1404
Ronghua Wud7988b12014-10-03 15:19:10 -07001405bool NuPlayer::audioDecoderStillNeeded() {
1406 // Audio decoder is no longer needed if it's in shut/shutting down status.
1407 return ((mFlushingAudio != SHUT_DOWN) && (mFlushingAudio != SHUTTING_DOWN_DECODER));
1408}
1409
Andy Hung8d121d42014-10-03 09:53:53 -07001410void NuPlayer::handleFlushComplete(bool audio, bool isDecoder) {
1411 // We wait for both the decoder flush and the renderer flush to complete
1412 // before entering either the FLUSHED or the SHUTTING_DOWN_DECODER state.
1413
1414 mFlushComplete[audio][isDecoder] = true;
1415 if (!mFlushComplete[audio][!isDecoder]) {
1416 return;
1417 }
1418
1419 FlushStatus *state = audio ? &mFlushingAudio : &mFlushingVideo;
1420 switch (*state) {
1421 case FLUSHING_DECODER:
1422 {
1423 *state = FLUSHED;
Andy Hung8d121d42014-10-03 09:53:53 -07001424 break;
1425 }
1426
1427 case FLUSHING_DECODER_SHUTDOWN:
1428 {
1429 *state = SHUTTING_DOWN_DECODER;
1430
1431 ALOGV("initiating %s decoder shutdown", audio ? "audio" : "video");
1432 if (!audio) {
Andy Hung8d121d42014-10-03 09:53:53 -07001433 // Widevine source reads must stop before releasing the video decoder.
1434 if (mSource != NULL && mSourceFlags & Source::FLAG_SECURE) {
1435 mSource->stop();
Robert Shih0c61a0d2015-07-06 15:09:10 -07001436 mSourceStarted = false;
Andy Hung8d121d42014-10-03 09:53:53 -07001437 }
1438 }
1439 getDecoder(audio)->initiateShutdown();
1440 break;
1441 }
1442
1443 default:
1444 // decoder flush completes only occur in a flushing state.
1445 LOG_ALWAYS_FATAL_IF(isDecoder, "decoder flush in invalid state %d", *state);
1446 break;
1447 }
1448}
1449
Andreas Huber3831a062010-12-21 10:22:33 -08001450void NuPlayer::finishFlushIfPossible() {
Wei Jia53904f32014-07-29 10:22:53 -07001451 if (mFlushingAudio != NONE && mFlushingAudio != FLUSHED
1452 && mFlushingAudio != SHUT_DOWN) {
Andreas Huber3831a062010-12-21 10:22:33 -08001453 return;
1454 }
1455
Wei Jia53904f32014-07-29 10:22:53 -07001456 if (mFlushingVideo != NONE && mFlushingVideo != FLUSHED
1457 && mFlushingVideo != SHUT_DOWN) {
Andreas Huber3831a062010-12-21 10:22:33 -08001458 return;
1459 }
1460
Steve Block3856b092011-10-20 11:56:00 +01001461 ALOGV("both audio and video are flushed now.");
Andreas Huber3831a062010-12-21 10:22:33 -08001462
Andreas Huber3831a062010-12-21 10:22:33 -08001463 mFlushingAudio = NONE;
1464 mFlushingVideo = NONE;
Andreas Huber3831a062010-12-21 10:22:33 -08001465
Andy Hung8d121d42014-10-03 09:53:53 -07001466 clearFlushComplete();
1467
Andreas Hubera1f8ab02012-11-30 10:53:22 -08001468 processDeferredActions();
Andreas Huber1aef2112011-01-04 14:01:29 -08001469}
1470
1471void NuPlayer::postScanSources() {
1472 if (mScanSourcesPending) {
1473 return;
1474 }
1475
Lajos Molnar1d15ab52015-03-04 16:46:34 -08001476 sp<AMessage> msg = new AMessage(kWhatScanSources, this);
Andreas Huber1aef2112011-01-04 14:01:29 -08001477 msg->setInt32("generation", mScanSourcesGeneration);
1478 msg->post();
1479
1480 mScanSourcesPending = true;
1481}
1482
Wei Jia41cd4632016-05-13 10:53:30 -07001483void NuPlayer::tryOpenAudioSinkForOffload(
1484 const sp<AMessage> &format, const sp<MetaData> &audioMeta, bool hasVideo) {
Andy Hung202bce12014-12-03 11:47:36 -08001485 // Note: This is called early in NuPlayer to determine whether offloading
1486 // is possible; otherwise the decoders call the renderer openAudioSink directly.
Andy Hung282a7e32014-08-14 15:56:34 -07001487
Andy Hung202bce12014-12-03 11:47:36 -08001488 status_t err = mRenderer->openAudioSink(
1489 format, true /* offloadOnly */, hasVideo, AUDIO_OUTPUT_FLAG_NONE, &mOffloadAudio);
1490 if (err != OK) {
1491 // Any failure we turn off mOffloadAudio.
1492 mOffloadAudio = false;
1493 } else if (mOffloadAudio) {
Chong Zhang3b9eb1f2014-10-15 17:05:08 -07001494 sendMetaDataToHal(mAudioSink, audioMeta);
Andy Hung282a7e32014-08-14 15:56:34 -07001495 }
1496}
1497
1498void NuPlayer::closeAudioSink() {
Chong Zhang3b9eb1f2014-10-15 17:05:08 -07001499 mRenderer->closeAudioSink();
Andy Hung282a7e32014-08-14 15:56:34 -07001500}
1501
Wei Jia5031b2f2016-02-25 11:19:31 -08001502void NuPlayer::restartAudio(
Wei Jiabf70feb2016-02-19 15:47:16 -08001503 int64_t currentPositionUs, bool forceNonOffload, bool needsToCreateAudioDecoder) {
Wei Jiaa05f1e32016-03-25 16:31:22 -07001504 if (mAudioDecoder != NULL) {
1505 mAudioDecoder->pause();
1506 mAudioDecoder.clear();
1507 ++mAudioDecoderGeneration;
1508 }
Wei Jiabf70feb2016-02-19 15:47:16 -08001509 if (mFlushingAudio == FLUSHING_DECODER) {
1510 mFlushComplete[1 /* audio */][1 /* isDecoder */] = true;
1511 mFlushingAudio = FLUSHED;
1512 finishFlushIfPossible();
1513 } else if (mFlushingAudio == FLUSHING_DECODER_SHUTDOWN
1514 || mFlushingAudio == SHUTTING_DOWN_DECODER) {
1515 mFlushComplete[1 /* audio */][1 /* isDecoder */] = true;
1516 mFlushingAudio = SHUT_DOWN;
1517 finishFlushIfPossible();
1518 needsToCreateAudioDecoder = false;
1519 }
1520 if (mRenderer == NULL) {
1521 return;
1522 }
1523 closeAudioSink();
1524 mRenderer->flush(true /* audio */, false /* notifyComplete */);
1525 if (mVideoDecoder != NULL) {
1526 mRenderer->flush(false /* audio */, false /* notifyComplete */);
1527 }
1528
1529 performSeek(currentPositionUs);
1530
1531 if (forceNonOffload) {
1532 mRenderer->signalDisableOffloadAudio();
1533 mOffloadAudio = false;
1534 }
1535 if (needsToCreateAudioDecoder) {
Wei Jiaa05f1e32016-03-25 16:31:22 -07001536 instantiateDecoder(true /* audio */, &mAudioDecoder, !forceNonOffload);
Wei Jiabf70feb2016-02-19 15:47:16 -08001537 }
1538}
1539
Wei Jia41cd4632016-05-13 10:53:30 -07001540void NuPlayer::determineAudioModeChange(const sp<AMessage> &audioFormat) {
Wei Jiae4d18c72015-07-13 17:58:11 -07001541 if (mSource == NULL || mAudioSink == NULL) {
1542 return;
1543 }
1544
1545 if (mRenderer == NULL) {
1546 ALOGW("No renderer can be used to determine audio mode. Use non-offload for safety.");
1547 mOffloadAudio = false;
1548 return;
1549 }
1550
1551 sp<MetaData> audioMeta = mSource->getFormatMeta(true /* audio */);
1552 sp<AMessage> videoFormat = mSource->getFormat(false /* audio */);
1553 audio_stream_type_t streamType = mAudioSink->getAudioStreamType();
1554 const bool hasVideo = (videoFormat != NULL);
1555 const bool canOffload = canOffloadStream(
Wei Jiabf70feb2016-02-19 15:47:16 -08001556 audioMeta, hasVideo, mSource->isStreaming(), streamType)
1557 && (mPlaybackSettings.mSpeed == 1.f && mPlaybackSettings.mPitch == 1.f);
Wei Jiae4d18c72015-07-13 17:58:11 -07001558 if (canOffload) {
1559 if (!mOffloadAudio) {
1560 mRenderer->signalEnableOffloadAudio();
1561 }
1562 // open audio sink early under offload mode.
Wei Jia41cd4632016-05-13 10:53:30 -07001563 tryOpenAudioSinkForOffload(audioFormat, audioMeta, hasVideo);
Wei Jiae4d18c72015-07-13 17:58:11 -07001564 } else {
Robert Shihe1d70192015-07-23 17:54:13 -07001565 if (mOffloadAudio) {
1566 mRenderer->signalDisableOffloadAudio();
1567 mOffloadAudio = false;
1568 }
Wei Jiae4d18c72015-07-13 17:58:11 -07001569 }
1570}
1571
Wei Jiaa05f1e32016-03-25 16:31:22 -07001572status_t NuPlayer::instantiateDecoder(
1573 bool audio, sp<DecoderBase> *decoder, bool checkAudioModeChange) {
Wei Jia566da802015-08-27 13:59:30 -07001574 // The audio decoder could be cleared by tear down. If still in shut down
1575 // process, no need to create a new audio decoder.
1576 if (*decoder != NULL || (audio && mFlushingAudio == SHUT_DOWN)) {
Andreas Huberf9334412010-12-15 15:17:42 -08001577 return OK;
1578 }
1579
Andreas Huber84066782011-08-16 09:34:26 -07001580 sp<AMessage> format = mSource->getFormat(audio);
Andreas Huberf9334412010-12-15 15:17:42 -08001581
Andreas Huber84066782011-08-16 09:34:26 -07001582 if (format == NULL) {
Robert Shih7350b052015-10-01 15:50:14 -07001583 return UNKNOWN_ERROR;
1584 } else {
1585 status_t err;
1586 if (format->findInt32("err", &err) && err) {
1587 return err;
1588 }
Andreas Huberf9334412010-12-15 15:17:42 -08001589 }
1590
Ronghua Wu8db88132015-04-22 13:51:35 -07001591 format->setInt32("priority", 0 /* realtime */);
1592
Andreas Huber3fe62152011-09-16 15:09:22 -07001593 if (!audio) {
Andreas Huber84066782011-08-16 09:34:26 -07001594 AString mime;
1595 CHECK(format->findString("mime", &mime));
Chong Zhanga7fa1d92014-06-11 14:49:23 -07001596
Lajos Molnar1d15ab52015-03-04 16:46:34 -08001597 sp<AMessage> ccNotify = new AMessage(kWhatClosedCaptionNotify, this);
Chong Zhang341ab6e2015-02-04 13:37:18 -08001598 if (mCCDecoder == NULL) {
1599 mCCDecoder = new CCDecoder(ccNotify);
1600 }
Lajos Molnar09524832014-07-17 14:29:51 -07001601
1602 if (mSourceFlags & Source::FLAG_SECURE) {
1603 format->setInt32("secure", true);
1604 }
Chong Zhang17134602015-01-07 16:14:34 -08001605
1606 if (mSourceFlags & Source::FLAG_PROTECTED) {
1607 format->setInt32("protected", true);
1608 }
Ronghua Wu8db88132015-04-22 13:51:35 -07001609
Ronghua Wuc8a70d32015-04-29 16:26:34 -07001610 float rate = getFrameRate();
1611 if (rate > 0) {
Lajos Molnar3a474aa2015-04-24 17:10:07 -07001612 format->setFloat("operating-rate", rate * mPlaybackSettings.mSpeed);
Ronghua Wu8db88132015-04-22 13:51:35 -07001613 }
Andreas Huber3fe62152011-09-16 15:09:22 -07001614 }
1615
Wei Jiabc2fb722014-07-08 16:37:57 -07001616 if (audio) {
Lajos Molnar1d15ab52015-03-04 16:46:34 -08001617 sp<AMessage> notify = new AMessage(kWhatAudioNotify, this);
Wei Jia88703c32014-08-06 11:24:07 -07001618 ++mAudioDecoderGeneration;
1619 notify->setInt32("generation", mAudioDecoderGeneration);
1620
Wei Jiaa05f1e32016-03-25 16:31:22 -07001621 if (checkAudioModeChange) {
Wei Jia41cd4632016-05-13 10:53:30 -07001622 determineAudioModeChange(format);
Wei Jiaa05f1e32016-03-25 16:31:22 -07001623 }
Wei Jiabc2fb722014-07-08 16:37:57 -07001624 if (mOffloadAudio) {
Wei Jia14532f22015-12-29 11:28:15 -08001625 mSource->setOffloadAudio(true /* offload */);
1626
Haynes Mathew George8b635332015-03-30 17:59:47 -07001627 const bool hasVideo = (mSource->getFormat(false /*audio */) != NULL);
1628 format->setInt32("has-video", hasVideo);
Wei Jiac6cfd702014-11-11 16:33:20 -08001629 *decoder = new DecoderPassThrough(notify, mSource, mRenderer);
Wei Jiabc2fb722014-07-08 16:37:57 -07001630 } else {
Wei Jia14532f22015-12-29 11:28:15 -08001631 mSource->setOffloadAudio(false /* offload */);
1632
Ronghua Wu68845c12015-07-21 09:50:48 -07001633 *decoder = new Decoder(notify, mSource, mPID, mRenderer);
Wei Jiabc2fb722014-07-08 16:37:57 -07001634 }
1635 } else {
Lajos Molnar1d15ab52015-03-04 16:46:34 -08001636 sp<AMessage> notify = new AMessage(kWhatVideoNotify, this);
Wei Jia88703c32014-08-06 11:24:07 -07001637 ++mVideoDecoderGeneration;
1638 notify->setInt32("generation", mVideoDecoderGeneration);
1639
Chong Zhang7137ec72014-11-12 16:41:05 -08001640 *decoder = new Decoder(
Ronghua Wu68845c12015-07-21 09:50:48 -07001641 notify, mSource, mPID, mRenderer, mSurface, mCCDecoder);
Lajos Molnard9fd6312014-11-06 11:00:00 -08001642
1643 // enable FRC if high-quality AV sync is requested, even if not
Lajos Molnar1de1e252015-04-30 18:18:34 -07001644 // directly queuing to display, as this will even improve textureview
Lajos Molnard9fd6312014-11-06 11:00:00 -08001645 // playback.
1646 {
1647 char value[PROPERTY_VALUE_MAX];
1648 if (property_get("persist.sys.media.avsync", value, NULL) &&
1649 (!strcmp("1", value) || !strcasecmp("true", value))) {
1650 format->setInt32("auto-frc", 1);
1651 }
1652 }
Wei Jiabc2fb722014-07-08 16:37:57 -07001653 }
Lajos Molnar1cd13982014-01-17 15:12:51 -08001654 (*decoder)->init();
Andreas Huber84066782011-08-16 09:34:26 -07001655 (*decoder)->configure(format);
Andreas Huberf9334412010-12-15 15:17:42 -08001656
Lajos Molnar09524832014-07-17 14:29:51 -07001657 // allocate buffers to decrypt widevine source buffers
1658 if (!audio && (mSourceFlags & Source::FLAG_SECURE)) {
1659 Vector<sp<ABuffer> > inputBufs;
1660 CHECK_EQ((*decoder)->getInputBuffers(&inputBufs), (status_t)OK);
1661
1662 Vector<MediaBuffer *> mediaBufs;
1663 for (size_t i = 0; i < inputBufs.size(); i++) {
1664 const sp<ABuffer> &buffer = inputBufs[i];
1665 MediaBuffer *mbuf = new MediaBuffer(buffer->data(), buffer->size());
1666 mediaBufs.push(mbuf);
1667 }
1668
1669 status_t err = mSource->setBuffers(audio, mediaBufs);
1670 if (err != OK) {
1671 for (size_t i = 0; i < mediaBufs.size(); ++i) {
1672 mediaBufs[i]->release();
1673 }
1674 mediaBufs.clear();
1675 ALOGE("Secure source didn't support secure mediaBufs.");
1676 return err;
1677 }
1678 }
Andreas Huberf9334412010-12-15 15:17:42 -08001679 return OK;
1680}
1681
Chong Zhangced1c2f2014-08-08 15:22:35 -07001682void NuPlayer::updateVideoSize(
1683 const sp<AMessage> &inputFormat,
1684 const sp<AMessage> &outputFormat) {
1685 if (inputFormat == NULL) {
1686 ALOGW("Unknown video size, reporting 0x0!");
1687 notifyListener(MEDIA_SET_VIDEO_SIZE, 0, 0);
1688 return;
1689 }
1690
1691 int32_t displayWidth, displayHeight;
Chong Zhangced1c2f2014-08-08 15:22:35 -07001692 if (outputFormat != NULL) {
1693 int32_t width, height;
1694 CHECK(outputFormat->findInt32("width", &width));
1695 CHECK(outputFormat->findInt32("height", &height));
1696
1697 int32_t cropLeft, cropTop, cropRight, cropBottom;
1698 CHECK(outputFormat->findRect(
1699 "crop",
1700 &cropLeft, &cropTop, &cropRight, &cropBottom));
1701
1702 displayWidth = cropRight - cropLeft + 1;
1703 displayHeight = cropBottom - cropTop + 1;
1704
1705 ALOGV("Video output format changed to %d x %d "
1706 "(crop: %d x %d @ (%d, %d))",
1707 width, height,
1708 displayWidth,
1709 displayHeight,
1710 cropLeft, cropTop);
1711 } else {
1712 CHECK(inputFormat->findInt32("width", &displayWidth));
1713 CHECK(inputFormat->findInt32("height", &displayHeight));
1714
1715 ALOGV("Video input format %d x %d", displayWidth, displayHeight);
1716 }
1717
1718 // Take into account sample aspect ratio if necessary:
1719 int32_t sarWidth, sarHeight;
1720 if (inputFormat->findInt32("sar-width", &sarWidth)
1721 && inputFormat->findInt32("sar-height", &sarHeight)) {
1722 ALOGV("Sample aspect ratio %d : %d", sarWidth, sarHeight);
1723
1724 displayWidth = (displayWidth * sarWidth) / sarHeight;
1725
1726 ALOGV("display dimensions %d x %d", displayWidth, displayHeight);
1727 }
1728
1729 int32_t rotationDegrees;
1730 if (!inputFormat->findInt32("rotation-degrees", &rotationDegrees)) {
1731 rotationDegrees = 0;
1732 }
1733
1734 if (rotationDegrees == 90 || rotationDegrees == 270) {
1735 int32_t tmp = displayWidth;
1736 displayWidth = displayHeight;
1737 displayHeight = tmp;
1738 }
1739
1740 notifyListener(
1741 MEDIA_SET_VIDEO_SIZE,
1742 displayWidth,
1743 displayHeight);
1744}
1745
Chong Zhangdcb89b32013-08-06 09:44:47 -07001746void NuPlayer::notifyListener(int msg, int ext1, int ext2, const Parcel *in) {
Andreas Huber43c3e6c2011-01-05 12:17:08 -08001747 if (mDriver == NULL) {
Andreas Huberf9334412010-12-15 15:17:42 -08001748 return;
1749 }
1750
Andreas Huber43c3e6c2011-01-05 12:17:08 -08001751 sp<NuPlayerDriver> driver = mDriver.promote();
Andreas Huberf9334412010-12-15 15:17:42 -08001752
Andreas Huber43c3e6c2011-01-05 12:17:08 -08001753 if (driver == NULL) {
Andreas Huberf9334412010-12-15 15:17:42 -08001754 return;
1755 }
1756
Chong Zhangdcb89b32013-08-06 09:44:47 -07001757 driver->notifyListener(msg, ext1, ext2, in);
Andreas Huberf9334412010-12-15 15:17:42 -08001758}
1759
Chong Zhang7137ec72014-11-12 16:41:05 -08001760void NuPlayer::flushDecoder(bool audio, bool needShutdown) {
Andreas Huber14f76722013-01-15 09:04:18 -08001761 ALOGV("[%s] flushDecoder needShutdown=%d",
1762 audio ? "audio" : "video", needShutdown);
1763
Chong Zhang7137ec72014-11-12 16:41:05 -08001764 const sp<DecoderBase> &decoder = getDecoder(audio);
Lajos Molnar87603c02014-08-20 19:25:30 -07001765 if (decoder == NULL) {
Steve Blockdf64d152012-01-04 20:05:49 +00001766 ALOGI("flushDecoder %s without decoder present",
Andreas Huber6e3d3112011-11-28 12:36:11 -08001767 audio ? "audio" : "video");
Lajos Molnar87603c02014-08-20 19:25:30 -07001768 return;
Andreas Huber6e3d3112011-11-28 12:36:11 -08001769 }
1770
Andreas Huber1aef2112011-01-04 14:01:29 -08001771 // Make sure we don't continue to scan sources until we finish flushing.
1772 ++mScanSourcesGeneration;
Chong Zhang3b032b32015-04-17 15:49:06 -07001773 if (mScanSourcesPending) {
1774 mDeferredActions.push_back(
1775 new SimpleAction(&NuPlayer::performScanSources));
1776 mScanSourcesPending = false;
1777 }
Andreas Huber1aef2112011-01-04 14:01:29 -08001778
Chong Zhang7137ec72014-11-12 16:41:05 -08001779 decoder->signalFlush();
Andreas Huber1aef2112011-01-04 14:01:29 -08001780
1781 FlushStatus newStatus =
1782 needShutdown ? FLUSHING_DECODER_SHUTDOWN : FLUSHING_DECODER;
1783
Lajos Molnarfcd3e942015-03-31 10:06:48 -07001784 mFlushComplete[audio][false /* isDecoder */] = (mRenderer == NULL);
Andy Hung8d121d42014-10-03 09:53:53 -07001785 mFlushComplete[audio][true /* isDecoder */] = false;
Andreas Huber1aef2112011-01-04 14:01:29 -08001786 if (audio) {
Wei Jia53904f32014-07-29 10:22:53 -07001787 ALOGE_IF(mFlushingAudio != NONE,
1788 "audio flushDecoder() is called in state %d", mFlushingAudio);
Andreas Huber1aef2112011-01-04 14:01:29 -08001789 mFlushingAudio = newStatus;
Andreas Huber1aef2112011-01-04 14:01:29 -08001790 } else {
Wei Jia53904f32014-07-29 10:22:53 -07001791 ALOGE_IF(mFlushingVideo != NONE,
1792 "video flushDecoder() is called in state %d", mFlushingVideo);
Andreas Huber1aef2112011-01-04 14:01:29 -08001793 mFlushingVideo = newStatus;
Andreas Huber1aef2112011-01-04 14:01:29 -08001794 }
1795}
1796
Chong Zhangced1c2f2014-08-08 15:22:35 -07001797void NuPlayer::queueDecoderShutdown(
1798 bool audio, bool video, const sp<AMessage> &reply) {
1799 ALOGI("queueDecoderShutdown audio=%d, video=%d", audio, video);
Andreas Huber84066782011-08-16 09:34:26 -07001800
Chong Zhangced1c2f2014-08-08 15:22:35 -07001801 mDeferredActions.push_back(
Wei Jiafef808d2014-10-31 17:57:05 -07001802 new FlushDecoderAction(
1803 audio ? FLUSH_CMD_SHUTDOWN : FLUSH_CMD_NONE,
1804 video ? FLUSH_CMD_SHUTDOWN : FLUSH_CMD_NONE));
Andreas Huber84066782011-08-16 09:34:26 -07001805
Chong Zhangced1c2f2014-08-08 15:22:35 -07001806 mDeferredActions.push_back(
1807 new SimpleAction(&NuPlayer::performScanSources));
Andreas Huber84066782011-08-16 09:34:26 -07001808
Chong Zhangced1c2f2014-08-08 15:22:35 -07001809 mDeferredActions.push_back(new PostMessageAction(reply));
1810
1811 processDeferredActions();
Andreas Huber84066782011-08-16 09:34:26 -07001812}
1813
James Dong0d268a32012-08-31 12:18:27 -07001814status_t NuPlayer::setVideoScalingMode(int32_t mode) {
1815 mVideoScalingMode = mode;
Lajos Molnar1de1e252015-04-30 18:18:34 -07001816 if (mSurface != NULL) {
1817 status_t ret = native_window_set_scaling_mode(mSurface.get(), mVideoScalingMode);
James Dong0d268a32012-08-31 12:18:27 -07001818 if (ret != OK) {
1819 ALOGE("Failed to set scaling mode (%d): %s",
1820 -ret, strerror(-ret));
1821 return ret;
1822 }
1823 }
1824 return OK;
1825}
1826
Chong Zhangdcb89b32013-08-06 09:44:47 -07001827status_t NuPlayer::getTrackInfo(Parcel* reply) const {
Lajos Molnar1d15ab52015-03-04 16:46:34 -08001828 sp<AMessage> msg = new AMessage(kWhatGetTrackInfo, this);
Chong Zhangdcb89b32013-08-06 09:44:47 -07001829 msg->setPointer("reply", reply);
1830
1831 sp<AMessage> response;
1832 status_t err = msg->postAndAwaitResponse(&response);
1833 return err;
1834}
1835
Robert Shih7c4f0d72014-07-09 18:53:31 -07001836status_t NuPlayer::getSelectedTrack(int32_t type, Parcel* reply) const {
Lajos Molnar1d15ab52015-03-04 16:46:34 -08001837 sp<AMessage> msg = new AMessage(kWhatGetSelectedTrack, this);
Robert Shih7c4f0d72014-07-09 18:53:31 -07001838 msg->setPointer("reply", reply);
1839 msg->setInt32("type", type);
1840
1841 sp<AMessage> response;
1842 status_t err = msg->postAndAwaitResponse(&response);
1843 if (err == OK && response != NULL) {
1844 CHECK(response->findInt32("err", &err));
1845 }
1846 return err;
1847}
1848
Robert Shih6ffb1fd2014-10-29 16:24:32 -07001849status_t NuPlayer::selectTrack(size_t trackIndex, bool select, int64_t timeUs) {
Lajos Molnar1d15ab52015-03-04 16:46:34 -08001850 sp<AMessage> msg = new AMessage(kWhatSelectTrack, this);
Chong Zhangdcb89b32013-08-06 09:44:47 -07001851 msg->setSize("trackIndex", trackIndex);
1852 msg->setInt32("select", select);
Robert Shih6ffb1fd2014-10-29 16:24:32 -07001853 msg->setInt64("timeUs", timeUs);
Chong Zhangdcb89b32013-08-06 09:44:47 -07001854
1855 sp<AMessage> response;
1856 status_t err = msg->postAndAwaitResponse(&response);
1857
Chong Zhang404fced2014-06-11 14:45:31 -07001858 if (err != OK) {
1859 return err;
1860 }
1861
1862 if (!response->findInt32("err", &err)) {
1863 err = OK;
1864 }
1865
Chong Zhangdcb89b32013-08-06 09:44:47 -07001866 return err;
1867}
1868
Ronghua Wua73d9e02014-10-08 15:13:29 -07001869status_t NuPlayer::getCurrentPosition(int64_t *mediaUs) {
1870 sp<Renderer> renderer = mRenderer;
1871 if (renderer == NULL) {
1872 return NO_INIT;
1873 }
1874
1875 return renderer->getCurrentPosition(mediaUs);
1876}
1877
Praveen Chavane1e5d7a2015-05-19 19:09:48 -07001878void NuPlayer::getStats(Vector<sp<AMessage> > *mTrackStats) {
1879 CHECK(mTrackStats != NULL);
1880
1881 mTrackStats->clear();
1882 if (mVideoDecoder != NULL) {
1883 mTrackStats->push_back(mVideoDecoder->getStats());
1884 }
1885 if (mAudioDecoder != NULL) {
1886 mTrackStats->push_back(mAudioDecoder->getStats());
Chong Zhang7137ec72014-11-12 16:41:05 -08001887 }
Ronghua Wua73d9e02014-10-08 15:13:29 -07001888}
1889
Marco Nelissenf0b72b52014-09-16 15:43:44 -07001890sp<MetaData> NuPlayer::getFileMeta() {
1891 return mSource->getFileFormatMeta();
1892}
1893
Ronghua Wuc8a70d32015-04-29 16:26:34 -07001894float NuPlayer::getFrameRate() {
1895 sp<MetaData> meta = mSource->getFormatMeta(false /* audio */);
1896 if (meta == NULL) {
1897 return 0;
1898 }
1899 int32_t rate;
1900 if (!meta->findInt32(kKeyFrameRate, &rate)) {
1901 // fall back to try file meta
1902 sp<MetaData> fileMeta = getFileMeta();
1903 if (fileMeta == NULL) {
1904 ALOGW("source has video meta but not file meta");
1905 return -1;
1906 }
1907 int32_t fileMetaRate;
1908 if (!fileMeta->findInt32(kKeyFrameRate, &fileMetaRate)) {
1909 return -1;
1910 }
1911 return fileMetaRate;
1912 }
1913 return rate;
1914}
1915
Andreas Huberb7c8e912012-11-27 15:02:53 -08001916void NuPlayer::schedulePollDuration() {
Lajos Molnar1d15ab52015-03-04 16:46:34 -08001917 sp<AMessage> msg = new AMessage(kWhatPollDuration, this);
Andreas Huberb7c8e912012-11-27 15:02:53 -08001918 msg->setInt32("generation", mPollDurationGeneration);
1919 msg->post();
1920}
1921
1922void NuPlayer::cancelPollDuration() {
1923 ++mPollDurationGeneration;
1924}
1925
Andreas Hubera1f8ab02012-11-30 10:53:22 -08001926void NuPlayer::processDeferredActions() {
1927 while (!mDeferredActions.empty()) {
1928 // We won't execute any deferred actions until we're no longer in
1929 // an intermediate state, i.e. one more more decoders are currently
1930 // flushing or shutting down.
1931
Andreas Hubera1f8ab02012-11-30 10:53:22 -08001932 if (mFlushingAudio != NONE || mFlushingVideo != NONE) {
1933 // We're currently flushing, postpone the reset until that's
1934 // completed.
1935
1936 ALOGV("postponing action mFlushingAudio=%d, mFlushingVideo=%d",
1937 mFlushingAudio, mFlushingVideo);
1938
1939 break;
1940 }
1941
1942 sp<Action> action = *mDeferredActions.begin();
1943 mDeferredActions.erase(mDeferredActions.begin());
1944
1945 action->execute(this);
1946 }
1947}
1948
Wei Jia29840802015-05-15 17:11:38 -07001949void NuPlayer::performSeek(int64_t seekTimeUs) {
1950 ALOGV("performSeek seekTimeUs=%lld us (%.2f secs)",
Lajos Molnar6d339f12015-04-17 16:15:53 -07001951 (long long)seekTimeUs,
Wei Jia29840802015-05-15 17:11:38 -07001952 seekTimeUs / 1E6);
Andreas Hubera1f8ab02012-11-30 10:53:22 -08001953
Andy Hungadf34bf2014-09-03 18:22:22 -07001954 if (mSource == NULL) {
1955 // This happens when reset occurs right before the loop mode
1956 // asynchronously seeks to the start of the stream.
1957 LOG_ALWAYS_FATAL_IF(mAudioDecoder != NULL || mVideoDecoder != NULL,
1958 "mSource is NULL and decoders not NULL audio(%p) video(%p)",
1959 mAudioDecoder.get(), mVideoDecoder.get());
1960 return;
1961 }
Robert Shih1a5c8592015-08-04 18:07:44 -07001962 mPreviousSeekTimeUs = seekTimeUs;
Andreas Hubera1f8ab02012-11-30 10:53:22 -08001963 mSource->seekTo(seekTimeUs);
Wei Jia351ce872016-02-10 13:21:59 -08001964 mPendingBufferingFlag = PENDING_BUFFERING_FLAG_NONE;
Robert Shihd3b0bbb2014-07-23 15:00:25 -07001965 ++mTimedTextGeneration;
Andreas Hubera1f8ab02012-11-30 10:53:22 -08001966
Andreas Hubera1f8ab02012-11-30 10:53:22 -08001967 // everything's flushed, continue playback.
1968}
1969
Wei Jiafef808d2014-10-31 17:57:05 -07001970void NuPlayer::performDecoderFlush(FlushCommand audio, FlushCommand video) {
1971 ALOGV("performDecoderFlush audio=%d, video=%d", audio, video);
Andreas Hubera1f8ab02012-11-30 10:53:22 -08001972
Wei Jiafef808d2014-10-31 17:57:05 -07001973 if ((audio == FLUSH_CMD_NONE || mAudioDecoder == NULL)
1974 && (video == FLUSH_CMD_NONE || mVideoDecoder == NULL)) {
Andreas Hubera1f8ab02012-11-30 10:53:22 -08001975 return;
1976 }
1977
Wei Jiafef808d2014-10-31 17:57:05 -07001978 if (audio != FLUSH_CMD_NONE && mAudioDecoder != NULL) {
1979 flushDecoder(true /* audio */, (audio == FLUSH_CMD_SHUTDOWN));
Andreas Hubera1f8ab02012-11-30 10:53:22 -08001980 }
1981
Wei Jiafef808d2014-10-31 17:57:05 -07001982 if (video != FLUSH_CMD_NONE && mVideoDecoder != NULL) {
1983 flushDecoder(false /* audio */, (video == FLUSH_CMD_SHUTDOWN));
Andreas Hubera1f8ab02012-11-30 10:53:22 -08001984 }
1985}
1986
1987void NuPlayer::performReset() {
1988 ALOGV("performReset");
1989
1990 CHECK(mAudioDecoder == NULL);
1991 CHECK(mVideoDecoder == NULL);
1992
1993 cancelPollDuration();
1994
1995 ++mScanSourcesGeneration;
1996 mScanSourcesPending = false;
1997
Lajos Molnar09524832014-07-17 14:29:51 -07001998 if (mRendererLooper != NULL) {
1999 if (mRenderer != NULL) {
2000 mRendererLooper->unregisterHandler(mRenderer->id());
2001 }
2002 mRendererLooper->stop();
2003 mRendererLooper.clear();
2004 }
Andreas Hubera1f8ab02012-11-30 10:53:22 -08002005 mRenderer.clear();
Wei Jia57568df2014-09-22 10:16:29 -07002006 ++mRendererGeneration;
Andreas Hubera1f8ab02012-11-30 10:53:22 -08002007
2008 if (mSource != NULL) {
2009 mSource->stop();
Andreas Huberb5f25f02013-02-05 10:14:26 -08002010
Wei Jiac45a4b22016-04-15 15:30:23 -07002011 Mutex::Autolock autoLock(mSourceLock);
Andreas Hubera1f8ab02012-11-30 10:53:22 -08002012 mSource.clear();
2013 }
2014
2015 if (mDriver != NULL) {
2016 sp<NuPlayerDriver> driver = mDriver.promote();
2017 if (driver != NULL) {
2018 driver->notifyResetComplete();
2019 }
2020 }
Andreas Huber57a339c2012-12-03 11:18:00 -08002021
2022 mStarted = false;
Ronghua Wu64c2d172015-10-07 16:52:19 -07002023 mResetting = false;
Robert Shih0c61a0d2015-07-06 15:09:10 -07002024 mSourceStarted = false;
Andreas Hubera1f8ab02012-11-30 10:53:22 -08002025}
2026
2027void NuPlayer::performScanSources() {
2028 ALOGV("performScanSources");
2029
Andreas Huber57a339c2012-12-03 11:18:00 -08002030 if (!mStarted) {
2031 return;
2032 }
2033
Andreas Hubera1f8ab02012-11-30 10:53:22 -08002034 if (mAudioDecoder == NULL || mVideoDecoder == NULL) {
2035 postScanSources();
2036 }
2037}
2038
Lajos Molnar1de1e252015-04-30 18:18:34 -07002039void NuPlayer::performSetSurface(const sp<Surface> &surface) {
Andreas Huber57a339c2012-12-03 11:18:00 -08002040 ALOGV("performSetSurface");
2041
Lajos Molnar1de1e252015-04-30 18:18:34 -07002042 mSurface = surface;
Andreas Huber57a339c2012-12-03 11:18:00 -08002043
2044 // XXX - ignore error from setVideoScalingMode for now
2045 setVideoScalingMode(mVideoScalingMode);
Chong Zhang13d6faa2014-08-22 15:35:28 -07002046
2047 if (mDriver != NULL) {
2048 sp<NuPlayerDriver> driver = mDriver.promote();
2049 if (driver != NULL) {
2050 driver->notifySetSurfaceComplete();
2051 }
2052 }
Andreas Huber57a339c2012-12-03 11:18:00 -08002053}
2054
Chong Zhangf8d71772014-11-26 15:08:34 -08002055void NuPlayer::performResumeDecoders(bool needNotify) {
2056 if (needNotify) {
2057 mResumePending = true;
2058 if (mVideoDecoder == NULL) {
2059 // if audio-only, we can notify seek complete now,
2060 // as the resume operation will be relatively fast.
2061 finishResume();
2062 }
2063 }
2064
Chong Zhang7137ec72014-11-12 16:41:05 -08002065 if (mVideoDecoder != NULL) {
Chong Zhangf8d71772014-11-26 15:08:34 -08002066 // When there is continuous seek, MediaPlayer will cache the seek
2067 // position, and send down new seek request when previous seek is
2068 // complete. Let's wait for at least one video output frame before
2069 // notifying seek complete, so that the video thumbnail gets updated
2070 // when seekbar is dragged.
2071 mVideoDecoder->signalResume(needNotify);
Chong Zhang7137ec72014-11-12 16:41:05 -08002072 }
2073
2074 if (mAudioDecoder != NULL) {
Chong Zhangf8d71772014-11-26 15:08:34 -08002075 mAudioDecoder->signalResume(false /* needNotify */);
2076 }
2077}
2078
2079void NuPlayer::finishResume() {
2080 if (mResumePending) {
2081 mResumePending = false;
Wei Jia1061c9c2015-05-19 16:02:17 -07002082 notifyDriverSeekComplete();
2083 }
2084}
2085
2086void NuPlayer::notifyDriverSeekComplete() {
2087 if (mDriver != NULL) {
2088 sp<NuPlayerDriver> driver = mDriver.promote();
2089 if (driver != NULL) {
2090 driver->notifySeekComplete();
Chong Zhangf8d71772014-11-26 15:08:34 -08002091 }
Chong Zhang7137ec72014-11-12 16:41:05 -08002092 }
2093}
2094
Andreas Huber9575c962013-02-05 13:59:56 -08002095void NuPlayer::onSourceNotify(const sp<AMessage> &msg) {
2096 int32_t what;
2097 CHECK(msg->findInt32("what", &what));
2098
2099 switch (what) {
Lajos Molnarfcd3e942015-03-31 10:06:48 -07002100 case Source::kWhatInstantiateSecureDecoders:
2101 {
2102 if (mSource == NULL) {
2103 // This is a stale notification from a source that was
2104 // asynchronously preparing when the client called reset().
2105 // We handled the reset, the source is gone.
2106 break;
2107 }
2108
2109 sp<AMessage> reply;
2110 CHECK(msg->findMessage("reply", &reply));
2111 status_t err = onInstantiateSecureDecoders();
2112 reply->setInt32("err", err);
2113 reply->post();
2114 break;
2115 }
2116
Andreas Huber9575c962013-02-05 13:59:56 -08002117 case Source::kWhatPrepared:
2118 {
Andreas Huberb5f28d42013-04-25 15:11:19 -07002119 if (mSource == NULL) {
2120 // This is a stale notification from a source that was
2121 // asynchronously preparing when the client called reset().
2122 // We handled the reset, the source is gone.
2123 break;
2124 }
2125
Andreas Huberec0c5972013-02-05 14:47:13 -08002126 int32_t err;
2127 CHECK(msg->findInt32("err", &err));
2128
Lajos Molnarfcd3e942015-03-31 10:06:48 -07002129 if (err != OK) {
2130 // shut down potential secure codecs in case client never calls reset
2131 mDeferredActions.push_back(
2132 new FlushDecoderAction(FLUSH_CMD_SHUTDOWN /* audio */,
2133 FLUSH_CMD_SHUTDOWN /* video */));
2134 processDeferredActions();
2135 }
2136
Andreas Huber9575c962013-02-05 13:59:56 -08002137 sp<NuPlayerDriver> driver = mDriver.promote();
2138 if (driver != NULL) {
Marco Nelissendd114d12014-05-28 15:23:14 -07002139 // notify duration first, so that it's definitely set when
2140 // the app received the "prepare complete" callback.
2141 int64_t durationUs;
2142 if (mSource->getDuration(&durationUs) == OK) {
2143 driver->notifyDuration(durationUs);
2144 }
Andreas Huberec0c5972013-02-05 14:47:13 -08002145 driver->notifyPrepareCompleted(err);
Andreas Huber9575c962013-02-05 13:59:56 -08002146 }
Andreas Huber99759402013-04-01 14:28:31 -07002147
Andreas Huber9575c962013-02-05 13:59:56 -08002148 break;
2149 }
2150
2151 case Source::kWhatFlagsChanged:
2152 {
2153 uint32_t flags;
2154 CHECK(msg->findInt32("flags", (int32_t *)&flags));
2155
Chong Zhang4b7069d2013-09-11 12:52:43 -07002156 sp<NuPlayerDriver> driver = mDriver.promote();
2157 if (driver != NULL) {
Wei Jia895651b2014-12-10 17:31:52 -08002158 if ((flags & NuPlayer::Source::FLAG_CAN_SEEK) == 0) {
2159 driver->notifyListener(
2160 MEDIA_INFO, MEDIA_INFO_NOT_SEEKABLE, 0);
2161 }
Chong Zhang4b7069d2013-09-11 12:52:43 -07002162 driver->notifyFlagsChanged(flags);
2163 }
2164
Andreas Huber9575c962013-02-05 13:59:56 -08002165 if ((mSourceFlags & Source::FLAG_DYNAMIC_DURATION)
2166 && (!(flags & Source::FLAG_DYNAMIC_DURATION))) {
2167 cancelPollDuration();
2168 } else if (!(mSourceFlags & Source::FLAG_DYNAMIC_DURATION)
2169 && (flags & Source::FLAG_DYNAMIC_DURATION)
2170 && (mAudioDecoder != NULL || mVideoDecoder != NULL)) {
2171 schedulePollDuration();
2172 }
2173
2174 mSourceFlags = flags;
2175 break;
2176 }
2177
2178 case Source::kWhatVideoSizeChanged:
2179 {
Chong Zhangced1c2f2014-08-08 15:22:35 -07002180 sp<AMessage> format;
2181 CHECK(msg->findMessage("format", &format));
Andreas Huber9575c962013-02-05 13:59:56 -08002182
Chong Zhangced1c2f2014-08-08 15:22:35 -07002183 updateVideoSize(format);
Andreas Huber9575c962013-02-05 13:59:56 -08002184 break;
2185 }
2186
Chong Zhang2a3cc9a2014-08-21 17:48:26 -07002187 case Source::kWhatBufferingUpdate:
2188 {
2189 int32_t percentage;
2190 CHECK(msg->findInt32("percentage", &percentage));
2191
2192 notifyListener(MEDIA_BUFFERING_UPDATE, percentage, 0);
2193 break;
2194 }
2195
Chong Zhangefbb6192015-01-30 17:13:27 -08002196 case Source::kWhatPauseOnBufferingStart:
2197 {
2198 // ignore if not playing
Chong Zhang8a048332015-05-06 15:16:28 -07002199 if (mStarted) {
Chong Zhangefbb6192015-01-30 17:13:27 -08002200 ALOGI("buffer low, pausing...");
2201
Chong Zhang8a048332015-05-06 15:16:28 -07002202 mPausedForBuffering = true;
Chong Zhangefbb6192015-01-30 17:13:27 -08002203 onPause();
2204 }
Wei Jiae67ba382016-02-03 01:41:49 +00002205 // fall-thru
2206 }
Chong Zhangefbb6192015-01-30 17:13:27 -08002207
Wei Jiae67ba382016-02-03 01:41:49 +00002208 case Source::kWhatBufferingStart:
2209 {
Wei Jia71c75e02016-02-04 09:40:47 -08002210 if (mPausedByClient) {
2211 mPendingBufferingFlag = PENDING_BUFFERING_FLAG_START;
2212 } else {
2213 notifyListener(MEDIA_INFO, MEDIA_INFO_BUFFERING_START, 0);
2214 mPendingBufferingFlag = PENDING_BUFFERING_FLAG_NONE;
2215 }
Roger Jönssonb50e83e2013-01-21 16:26:41 +01002216 break;
2217 }
2218
Chong Zhangefbb6192015-01-30 17:13:27 -08002219 case Source::kWhatResumeOnBufferingEnd:
2220 {
2221 // ignore if not playing
Chong Zhang8a048332015-05-06 15:16:28 -07002222 if (mStarted) {
Chong Zhangefbb6192015-01-30 17:13:27 -08002223 ALOGI("buffer ready, resuming...");
2224
Chong Zhang8a048332015-05-06 15:16:28 -07002225 mPausedForBuffering = false;
2226
2227 // do not resume yet if client didn't unpause
2228 if (!mPausedByClient) {
2229 onResume();
2230 }
Chong Zhangefbb6192015-01-30 17:13:27 -08002231 }
Wei Jiae67ba382016-02-03 01:41:49 +00002232 // fall-thru
2233 }
Chong Zhangefbb6192015-01-30 17:13:27 -08002234
Wei Jiae67ba382016-02-03 01:41:49 +00002235 case Source::kWhatBufferingEnd:
2236 {
Wei Jia3bed45a2016-02-17 11:06:47 -08002237 notifyListener(MEDIA_INFO, MEDIA_INFO_BUFFERING_END, 0);
2238 mPendingBufferingFlag = PENDING_BUFFERING_FLAG_NONE;
Roger Jönssonb50e83e2013-01-21 16:26:41 +01002239 break;
2240 }
2241
Chong Zhangefbb6192015-01-30 17:13:27 -08002242 case Source::kWhatCacheStats:
2243 {
2244 int32_t kbps;
2245 CHECK(msg->findInt32("bandwidth", &kbps));
2246
2247 notifyListener(MEDIA_INFO, MEDIA_INFO_NETWORK_BANDWIDTH, kbps);
2248 break;
2249 }
2250
Chong Zhangdcb89b32013-08-06 09:44:47 -07002251 case Source::kWhatSubtitleData:
2252 {
2253 sp<ABuffer> buffer;
2254 CHECK(msg->findBuffer("buffer", &buffer));
2255
Chong Zhang404fced2014-06-11 14:45:31 -07002256 sendSubtitleData(buffer, 0 /* baseIndex */);
Chong Zhangdcb89b32013-08-06 09:44:47 -07002257 break;
2258 }
2259
Robert Shih08528432015-04-08 09:06:54 -07002260 case Source::kWhatTimedMetaData:
2261 {
2262 sp<ABuffer> buffer;
2263 if (!msg->findBuffer("buffer", &buffer)) {
2264 notifyListener(MEDIA_INFO, MEDIA_INFO_METADATA_UPDATE, 0);
2265 } else {
2266 sendTimedMetaData(buffer);
2267 }
2268 break;
2269 }
2270
Robert Shihd3b0bbb2014-07-23 15:00:25 -07002271 case Source::kWhatTimedTextData:
2272 {
2273 int32_t generation;
2274 if (msg->findInt32("generation", &generation)
2275 && generation != mTimedTextGeneration) {
2276 break;
2277 }
2278
2279 sp<ABuffer> buffer;
2280 CHECK(msg->findBuffer("buffer", &buffer));
2281
2282 sp<NuPlayerDriver> driver = mDriver.promote();
2283 if (driver == NULL) {
2284 break;
2285 }
2286
2287 int posMs;
2288 int64_t timeUs, posUs;
2289 driver->getCurrentPosition(&posMs);
Patrik2 Carlsson24d484b2015-01-27 16:49:45 +01002290 posUs = (int64_t) posMs * 1000ll;
Robert Shihd3b0bbb2014-07-23 15:00:25 -07002291 CHECK(buffer->meta()->findInt64("timeUs", &timeUs));
2292
2293 if (posUs < timeUs) {
2294 if (!msg->findInt32("generation", &generation)) {
2295 msg->setInt32("generation", mTimedTextGeneration);
2296 }
2297 msg->post(timeUs - posUs);
2298 } else {
2299 sendTimedTextData(buffer);
2300 }
2301 break;
2302 }
2303
Andreas Huber14f76722013-01-15 09:04:18 -08002304 case Source::kWhatQueueDecoderShutdown:
2305 {
2306 int32_t audio, video;
2307 CHECK(msg->findInt32("audio", &audio));
2308 CHECK(msg->findInt32("video", &video));
2309
2310 sp<AMessage> reply;
2311 CHECK(msg->findMessage("reply", &reply));
2312
2313 queueDecoderShutdown(audio, video, reply);
2314 break;
2315 }
2316
Ronghua Wu80276872014-08-28 15:50:29 -07002317 case Source::kWhatDrmNoLicense:
2318 {
2319 notifyListener(MEDIA_ERROR, MEDIA_ERROR_UNKNOWN, ERROR_DRM_NO_LICENSE);
2320 break;
2321 }
2322
Andreas Huber9575c962013-02-05 13:59:56 -08002323 default:
2324 TRESPASS();
2325 }
2326}
2327
Chong Zhanga7fa1d92014-06-11 14:49:23 -07002328void NuPlayer::onClosedCaptionNotify(const sp<AMessage> &msg) {
2329 int32_t what;
2330 CHECK(msg->findInt32("what", &what));
2331
2332 switch (what) {
2333 case NuPlayer::CCDecoder::kWhatClosedCaptionData:
2334 {
2335 sp<ABuffer> buffer;
2336 CHECK(msg->findBuffer("buffer", &buffer));
2337
2338 size_t inbandTracks = 0;
2339 if (mSource != NULL) {
2340 inbandTracks = mSource->getTrackCount();
2341 }
2342
2343 sendSubtitleData(buffer, inbandTracks);
2344 break;
2345 }
2346
2347 case NuPlayer::CCDecoder::kWhatTrackAdded:
2348 {
2349 notifyListener(MEDIA_INFO, MEDIA_INFO_METADATA_UPDATE, 0);
2350
2351 break;
2352 }
2353
2354 default:
2355 TRESPASS();
2356 }
2357
2358
2359}
2360
Chong Zhang404fced2014-06-11 14:45:31 -07002361void NuPlayer::sendSubtitleData(const sp<ABuffer> &buffer, int32_t baseIndex) {
2362 int32_t trackIndex;
2363 int64_t timeUs, durationUs;
2364 CHECK(buffer->meta()->findInt32("trackIndex", &trackIndex));
2365 CHECK(buffer->meta()->findInt64("timeUs", &timeUs));
2366 CHECK(buffer->meta()->findInt64("durationUs", &durationUs));
2367
2368 Parcel in;
2369 in.writeInt32(trackIndex + baseIndex);
2370 in.writeInt64(timeUs);
2371 in.writeInt64(durationUs);
2372 in.writeInt32(buffer->size());
2373 in.writeInt32(buffer->size());
2374 in.write(buffer->data(), buffer->size());
2375
2376 notifyListener(MEDIA_SUBTITLE_DATA, 0, 0, &in);
2377}
Robert Shihd3b0bbb2014-07-23 15:00:25 -07002378
Robert Shih08528432015-04-08 09:06:54 -07002379void NuPlayer::sendTimedMetaData(const sp<ABuffer> &buffer) {
2380 int64_t timeUs;
2381 CHECK(buffer->meta()->findInt64("timeUs", &timeUs));
2382
2383 Parcel in;
2384 in.writeInt64(timeUs);
2385 in.writeInt32(buffer->size());
2386 in.writeInt32(buffer->size());
2387 in.write(buffer->data(), buffer->size());
2388
2389 notifyListener(MEDIA_META_DATA, 0, 0, &in);
2390}
2391
Robert Shihd3b0bbb2014-07-23 15:00:25 -07002392void NuPlayer::sendTimedTextData(const sp<ABuffer> &buffer) {
2393 const void *data;
2394 size_t size = 0;
2395 int64_t timeUs;
Marco Nelissen55e2f4c2015-09-04 15:57:15 -07002396 int32_t flag = TextDescriptions::IN_BAND_TEXT_3GPP;
Robert Shihd3b0bbb2014-07-23 15:00:25 -07002397
2398 AString mime;
2399 CHECK(buffer->meta()->findString("mime", &mime));
2400 CHECK(strcasecmp(mime.c_str(), MEDIA_MIMETYPE_TEXT_3GPP) == 0);
2401
2402 data = buffer->data();
2403 size = buffer->size();
2404
2405 Parcel parcel;
2406 if (size > 0) {
2407 CHECK(buffer->meta()->findInt64("timeUs", &timeUs));
Marco Nelissen55e2f4c2015-09-04 15:57:15 -07002408 int32_t global = 0;
2409 if (buffer->meta()->findInt32("global", &global) && global) {
2410 flag |= TextDescriptions::GLOBAL_DESCRIPTIONS;
2411 } else {
2412 flag |= TextDescriptions::LOCAL_DESCRIPTIONS;
2413 }
Robert Shihd3b0bbb2014-07-23 15:00:25 -07002414 TextDescriptions::getParcelOfDescriptions(
2415 (const uint8_t *)data, size, flag, timeUs / 1000, &parcel);
2416 }
2417
2418 if ((parcel.dataSize() > 0)) {
2419 notifyListener(MEDIA_TIMED_TEXT, 0, 0, &parcel);
2420 } else { // send an empty timed text
2421 notifyListener(MEDIA_TIMED_TEXT, 0, 0);
2422 }
2423}
Andreas Huberb5f25f02013-02-05 10:14:26 -08002424////////////////////////////////////////////////////////////////////////////////
2425
Chong Zhangced1c2f2014-08-08 15:22:35 -07002426sp<AMessage> NuPlayer::Source::getFormat(bool audio) {
2427 sp<MetaData> meta = getFormatMeta(audio);
2428
2429 if (meta == NULL) {
2430 return NULL;
2431 }
2432
2433 sp<AMessage> msg = new AMessage;
2434
2435 if(convertMetaDataToMessage(meta, &msg) == OK) {
2436 return msg;
2437 }
2438 return NULL;
2439}
2440
Andreas Huber9575c962013-02-05 13:59:56 -08002441void NuPlayer::Source::notifyFlagsChanged(uint32_t flags) {
2442 sp<AMessage> notify = dupNotify();
2443 notify->setInt32("what", kWhatFlagsChanged);
2444 notify->setInt32("flags", flags);
2445 notify->post();
2446}
2447
Chong Zhangced1c2f2014-08-08 15:22:35 -07002448void NuPlayer::Source::notifyVideoSizeChanged(const sp<AMessage> &format) {
Andreas Huber9575c962013-02-05 13:59:56 -08002449 sp<AMessage> notify = dupNotify();
2450 notify->setInt32("what", kWhatVideoSizeChanged);
Chong Zhangced1c2f2014-08-08 15:22:35 -07002451 notify->setMessage("format", format);
Andreas Huber9575c962013-02-05 13:59:56 -08002452 notify->post();
2453}
2454
Andreas Huberec0c5972013-02-05 14:47:13 -08002455void NuPlayer::Source::notifyPrepared(status_t err) {
Andreas Huber9575c962013-02-05 13:59:56 -08002456 sp<AMessage> notify = dupNotify();
2457 notify->setInt32("what", kWhatPrepared);
Andreas Huberec0c5972013-02-05 14:47:13 -08002458 notify->setInt32("err", err);
Andreas Huber9575c962013-02-05 13:59:56 -08002459 notify->post();
2460}
2461
Lajos Molnarfcd3e942015-03-31 10:06:48 -07002462void NuPlayer::Source::notifyInstantiateSecureDecoders(const sp<AMessage> &reply) {
2463 sp<AMessage> notify = dupNotify();
2464 notify->setInt32("what", kWhatInstantiateSecureDecoders);
2465 notify->setMessage("reply", reply);
2466 notify->post();
2467}
2468
Andreas Huber84333e02014-02-07 15:36:10 -08002469void NuPlayer::Source::onMessageReceived(const sp<AMessage> & /* msg */) {
Andreas Huberb5f25f02013-02-05 10:14:26 -08002470 TRESPASS();
2471}
2472
Andreas Huberf9334412010-12-15 15:17:42 -08002473} // namespace android