blob: 1fb4365dd5907353368cba7af922d35d2aa037aa [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 Jiae427abf2014-09-22 15:21:11 -070072 SeekAction(int64_t seekTimeUs, bool needNotify)
73 : mSeekTimeUs(seekTimeUs),
74 mNeedNotify(needNotify) {
Andreas Hubera1f8ab02012-11-30 10:53:22 -080075 }
76
77 virtual void execute(NuPlayer *player) {
Wei Jiae427abf2014-09-22 15:21:11 -070078 player->performSeek(mSeekTimeUs, mNeedNotify);
Andreas Hubera1f8ab02012-11-30 10:53:22 -080079 }
80
81private:
82 int64_t mSeekTimeUs;
Wei Jiae427abf2014-09-22 15:21:11 -070083 bool mNeedNotify;
Andreas Hubera1f8ab02012-11-30 10:53:22 -080084
85 DISALLOW_EVIL_CONSTRUCTORS(SeekAction);
86};
87
Chong Zhangf8d71772014-11-26 15:08:34 -080088struct NuPlayer::ResumeDecoderAction : public Action {
89 ResumeDecoderAction(bool needNotify)
90 : mNeedNotify(needNotify) {
91 }
92
93 virtual void execute(NuPlayer *player) {
94 player->performResumeDecoders(mNeedNotify);
95 }
96
97private:
98 bool mNeedNotify;
99
100 DISALLOW_EVIL_CONSTRUCTORS(ResumeDecoderAction);
101};
102
Andreas Huber57a339c2012-12-03 11:18:00 -0800103struct NuPlayer::SetSurfaceAction : public Action {
Lajos Molnar1de1e252015-04-30 18:18:34 -0700104 SetSurfaceAction(const sp<Surface> &surface)
105 : mSurface(surface) {
Andreas Huber57a339c2012-12-03 11:18:00 -0800106 }
107
108 virtual void execute(NuPlayer *player) {
Lajos Molnar1de1e252015-04-30 18:18:34 -0700109 player->performSetSurface(mSurface);
Andreas Huber57a339c2012-12-03 11:18:00 -0800110 }
111
112private:
Lajos Molnar1de1e252015-04-30 18:18:34 -0700113 sp<Surface> mSurface;
Andreas Huber57a339c2012-12-03 11:18:00 -0800114
115 DISALLOW_EVIL_CONSTRUCTORS(SetSurfaceAction);
116};
117
Wei Jiafef808d2014-10-31 17:57:05 -0700118struct NuPlayer::FlushDecoderAction : public Action {
119 FlushDecoderAction(FlushCommand audio, FlushCommand video)
Andreas Huber14f76722013-01-15 09:04:18 -0800120 : mAudio(audio),
121 mVideo(video) {
122 }
123
124 virtual void execute(NuPlayer *player) {
Wei Jiafef808d2014-10-31 17:57:05 -0700125 player->performDecoderFlush(mAudio, mVideo);
Andreas Huber14f76722013-01-15 09:04:18 -0800126 }
127
128private:
Wei Jiafef808d2014-10-31 17:57:05 -0700129 FlushCommand mAudio;
130 FlushCommand mVideo;
Andreas Huber14f76722013-01-15 09:04:18 -0800131
Wei Jiafef808d2014-10-31 17:57:05 -0700132 DISALLOW_EVIL_CONSTRUCTORS(FlushDecoderAction);
Andreas Huber14f76722013-01-15 09:04:18 -0800133};
134
135struct NuPlayer::PostMessageAction : public Action {
136 PostMessageAction(const sp<AMessage> &msg)
137 : mMessage(msg) {
138 }
139
140 virtual void execute(NuPlayer *) {
141 mMessage->post();
142 }
143
144private:
145 sp<AMessage> mMessage;
146
147 DISALLOW_EVIL_CONSTRUCTORS(PostMessageAction);
148};
149
Andreas Hubera1f8ab02012-11-30 10:53:22 -0800150// Use this if there's no state necessary to save in order to execute
151// the action.
152struct NuPlayer::SimpleAction : public Action {
153 typedef void (NuPlayer::*ActionFunc)();
154
155 SimpleAction(ActionFunc func)
156 : mFunc(func) {
157 }
158
159 virtual void execute(NuPlayer *player) {
160 (player->*mFunc)();
161 }
162
163private:
164 ActionFunc mFunc;
165
166 DISALLOW_EVIL_CONSTRUCTORS(SimpleAction);
167};
168
Andreas Huberf9334412010-12-15 15:17:42 -0800169////////////////////////////////////////////////////////////////////////////////
170
171NuPlayer::NuPlayer()
Andreas Huber9b80c2b2011-06-30 15:47:02 -0700172 : mUIDValid(false),
Andreas Huber9575c962013-02-05 13:59:56 -0800173 mSourceFlags(0),
Wei Jiabc2fb722014-07-08 16:37:57 -0700174 mOffloadAudio(false),
Wei Jia88703c32014-08-06 11:24:07 -0700175 mAudioDecoderGeneration(0),
176 mVideoDecoderGeneration(0),
Wei Jia57568df2014-09-22 10:16:29 -0700177 mRendererGeneration(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),
191 mPaused(false),
Chong Zhang8a048332015-05-06 15:16:28 -0700192 mPausedByClient(false),
193 mPausedForBuffering(false) {
Andy Hung8d121d42014-10-03 09:53:53 -0700194 clearFlushComplete();
Andreas Huberf9334412010-12-15 15:17:42 -0800195}
196
197NuPlayer::~NuPlayer() {
198}
199
Andreas Huber9b80c2b2011-06-30 15:47:02 -0700200void NuPlayer::setUID(uid_t uid) {
201 mUIDValid = true;
202 mUID = uid;
203}
204
Andreas Huber43c3e6c2011-01-05 12:17:08 -0800205void NuPlayer::setDriver(const wp<NuPlayerDriver> &driver) {
206 mDriver = driver;
Andreas Huberf9334412010-12-15 15:17:42 -0800207}
208
Andreas Huber9575c962013-02-05 13:59:56 -0800209void NuPlayer::setDataSourceAsync(const sp<IStreamSource> &source) {
Lajos Molnar1d15ab52015-03-04 16:46:34 -0800210 sp<AMessage> msg = new AMessage(kWhatSetDataSource, this);
Andreas Huberf9334412010-12-15 15:17:42 -0800211
Lajos Molnar1d15ab52015-03-04 16:46:34 -0800212 sp<AMessage> notify = new AMessage(kWhatSourceNotify, this);
Andreas Huberb5f25f02013-02-05 10:14:26 -0800213
Andreas Huber240abcc2014-02-13 13:32:37 -0800214 msg->setObject("source", new StreamingSource(notify, source));
Andreas Huber5bc087c2010-12-23 10:27:40 -0800215 msg->post();
216}
Andreas Huberf9334412010-12-15 15:17:42 -0800217
Andreas Huberafed0e12011-09-20 15:39:58 -0700218static bool IsHTTPLiveURL(const char *url) {
219 if (!strncasecmp("http://", url, 7)
Andreas Huber99759402013-04-01 14:28:31 -0700220 || !strncasecmp("https://", url, 8)
221 || !strncasecmp("file://", url, 7)) {
Andreas Huberafed0e12011-09-20 15:39:58 -0700222 size_t len = strlen(url);
223 if (len >= 5 && !strcasecmp(".m3u8", &url[len - 5])) {
224 return true;
225 }
226
227 if (strstr(url,"m3u8")) {
228 return true;
229 }
230 }
231
232 return false;
233}
234
Andreas Huber9575c962013-02-05 13:59:56 -0800235void NuPlayer::setDataSourceAsync(
Andreas Huber1b86fe02014-01-29 11:13:26 -0800236 const sp<IMediaHTTPService> &httpService,
237 const char *url,
238 const KeyedVector<String8, String8> *headers) {
Chong Zhang3de157d2014-08-05 20:54:44 -0700239
Lajos Molnar1d15ab52015-03-04 16:46:34 -0800240 sp<AMessage> msg = new AMessage(kWhatSetDataSource, this);
Oscar Rydhé7a33b772012-02-20 10:15:48 +0100241 size_t len = strlen(url);
Andreas Huber5bc087c2010-12-23 10:27:40 -0800242
Lajos Molnar1d15ab52015-03-04 16:46:34 -0800243 sp<AMessage> notify = new AMessage(kWhatSourceNotify, this);
Andreas Huberb5f25f02013-02-05 10:14:26 -0800244
Andreas Huberafed0e12011-09-20 15:39:58 -0700245 sp<Source> source;
246 if (IsHTTPLiveURL(url)) {
Andreas Huber81e68442014-02-05 11:52:33 -0800247 source = new HTTPLiveSource(notify, httpService, url, headers);
Andreas Huberafed0e12011-09-20 15:39:58 -0700248 } else if (!strncasecmp(url, "rtsp://", 7)) {
Andreas Huber1b86fe02014-01-29 11:13:26 -0800249 source = new RTSPSource(
250 notify, httpService, url, headers, mUIDValid, mUID);
Oscar Rydhé7a33b772012-02-20 10:15:48 +0100251 } else if ((!strncasecmp(url, "http://", 7)
252 || !strncasecmp(url, "https://", 8))
253 && ((len >= 4 && !strcasecmp(".sdp", &url[len - 4]))
254 || strstr(url, ".sdp?"))) {
Andreas Huber1b86fe02014-01-29 11:13:26 -0800255 source = new RTSPSource(
256 notify, httpService, url, headers, mUIDValid, mUID, true);
Andreas Huber2bfdd422011-10-11 15:24:07 -0700257 } else {
Chong Zhang3de157d2014-08-05 20:54:44 -0700258 sp<GenericSource> genericSource =
259 new GenericSource(notify, mUIDValid, mUID);
260 // Don't set FLAG_SECURE on mSourceFlags here for widevine.
261 // The correct flags will be updated in Source::kWhatFlagsChanged
262 // handler when GenericSource is prepared.
Andreas Huber2bfdd422011-10-11 15:24:07 -0700263
Chong Zhanga19f33e2014-08-07 15:35:07 -0700264 status_t err = genericSource->setDataSource(httpService, url, headers);
Chong Zhang3de157d2014-08-05 20:54:44 -0700265
266 if (err == OK) {
267 source = genericSource;
268 } else {
Chong Zhanga19f33e2014-08-07 15:35:07 -0700269 ALOGE("Failed to set data source!");
Chong Zhang3de157d2014-08-05 20:54:44 -0700270 }
271 }
Andreas Huberafed0e12011-09-20 15:39:58 -0700272 msg->setObject("source", source);
273 msg->post();
274}
275
Andreas Huber9575c962013-02-05 13:59:56 -0800276void NuPlayer::setDataSourceAsync(int fd, int64_t offset, int64_t length) {
Lajos Molnar1d15ab52015-03-04 16:46:34 -0800277 sp<AMessage> msg = new AMessage(kWhatSetDataSource, this);
Andreas Huberafed0e12011-09-20 15:39:58 -0700278
Lajos Molnar1d15ab52015-03-04 16:46:34 -0800279 sp<AMessage> notify = new AMessage(kWhatSourceNotify, this);
Andreas Huberb5f25f02013-02-05 10:14:26 -0800280
Chong Zhang3de157d2014-08-05 20:54:44 -0700281 sp<GenericSource> source =
282 new GenericSource(notify, mUIDValid, mUID);
283
Chong Zhanga19f33e2014-08-07 15:35:07 -0700284 status_t err = source->setDataSource(fd, offset, length);
Chong Zhang3de157d2014-08-05 20:54:44 -0700285
286 if (err != OK) {
Chong Zhanga19f33e2014-08-07 15:35:07 -0700287 ALOGE("Failed to set data source!");
Chong Zhang3de157d2014-08-05 20:54:44 -0700288 source = NULL;
289 }
290
Andreas Huberafed0e12011-09-20 15:39:58 -0700291 msg->setObject("source", source);
Andreas Huberf9334412010-12-15 15:17:42 -0800292 msg->post();
293}
294
Chris Watkins99f31602015-03-20 13:06:33 -0700295void NuPlayer::setDataSourceAsync(const sp<DataSource> &dataSource) {
296 sp<AMessage> msg = new AMessage(kWhatSetDataSource, this);
297 sp<AMessage> notify = new AMessage(kWhatSourceNotify, this);
298
299 sp<GenericSource> source = new GenericSource(notify, mUIDValid, mUID);
300 status_t err = source->setDataSource(dataSource);
301
302 if (err != OK) {
303 ALOGE("Failed to set data source!");
304 source = NULL;
305 }
306
307 msg->setObject("source", source);
308 msg->post();
309}
310
Andreas Huber9575c962013-02-05 13:59:56 -0800311void NuPlayer::prepareAsync() {
Lajos Molnar1d15ab52015-03-04 16:46:34 -0800312 (new AMessage(kWhatPrepare, this))->post();
Andreas Huber9575c962013-02-05 13:59:56 -0800313}
314
Andreas Huber57a339c2012-12-03 11:18:00 -0800315void NuPlayer::setVideoSurfaceTextureAsync(
Andy McFadden8ba01022012-12-18 09:46:54 -0800316 const sp<IGraphicBufferProducer> &bufferProducer) {
Lajos Molnar1de1e252015-04-30 18:18:34 -0700317 sp<AMessage> msg = new AMessage(kWhatSetVideoSurface, this);
Andreas Huber57a339c2012-12-03 11:18:00 -0800318
Andy McFadden8ba01022012-12-18 09:46:54 -0800319 if (bufferProducer == NULL) {
Lajos Molnar1de1e252015-04-30 18:18:34 -0700320 msg->setObject("surface", NULL);
Andreas Huber57a339c2012-12-03 11:18:00 -0800321 } else {
Lajos Molnar1de1e252015-04-30 18:18:34 -0700322 msg->setObject("surface", new Surface(bufferProducer, true /* controlledByApp */));
Andreas Huber57a339c2012-12-03 11:18:00 -0800323 }
324
Andreas Huberf9334412010-12-15 15:17:42 -0800325 msg->post();
326}
327
328void NuPlayer::setAudioSink(const sp<MediaPlayerBase::AudioSink> &sink) {
Lajos Molnar1d15ab52015-03-04 16:46:34 -0800329 sp<AMessage> msg = new AMessage(kWhatSetAudioSink, this);
Andreas Huberf9334412010-12-15 15:17:42 -0800330 msg->setObject("sink", sink);
331 msg->post();
332}
333
334void NuPlayer::start() {
Lajos Molnar1d15ab52015-03-04 16:46:34 -0800335 (new AMessage(kWhatStart, this))->post();
Andreas Huberf9334412010-12-15 15:17:42 -0800336}
337
Lajos Molnar3a474aa2015-04-24 17:10:07 -0700338status_t NuPlayer::setPlaybackSettings(const AudioPlaybackRate &rate) {
339 // do some cursory validation of the settings here. audio modes are
340 // only validated when set on the audiosink.
341 if ((rate.mSpeed != 0.f && rate.mSpeed < AUDIO_TIMESTRETCH_SPEED_MIN)
342 || rate.mSpeed > AUDIO_TIMESTRETCH_SPEED_MAX
343 || rate.mPitch < AUDIO_TIMESTRETCH_SPEED_MIN
344 || rate.mPitch > AUDIO_TIMESTRETCH_SPEED_MAX) {
345 return BAD_VALUE;
346 }
347 sp<AMessage> msg = new AMessage(kWhatConfigPlayback, this);
348 writeToAMessage(msg, rate);
349 sp<AMessage> response;
350 status_t err = msg->postAndAwaitResponse(&response);
351 if (err == OK && response != NULL) {
352 CHECK(response->findInt32("err", &err));
353 }
354 return err;
355}
356
357status_t NuPlayer::getPlaybackSettings(AudioPlaybackRate *rate /* nonnull */) {
358 sp<AMessage> msg = new AMessage(kWhatGetPlaybackSettings, this);
359 sp<AMessage> response;
360 status_t err = msg->postAndAwaitResponse(&response);
361 if (err == OK && response != NULL) {
362 CHECK(response->findInt32("err", &err));
363 if (err == OK) {
364 readFromAMessage(response, rate);
365 }
366 }
367 return err;
368}
369
370status_t NuPlayer::setSyncSettings(const AVSyncSettings &sync, float videoFpsHint) {
371 sp<AMessage> msg = new AMessage(kWhatConfigSync, this);
372 writeToAMessage(msg, sync, videoFpsHint);
373 sp<AMessage> response;
374 status_t err = msg->postAndAwaitResponse(&response);
375 if (err == OK && response != NULL) {
376 CHECK(response->findInt32("err", &err));
377 }
378 return err;
379}
380
381status_t NuPlayer::getSyncSettings(
382 AVSyncSettings *sync /* nonnull */, float *videoFps /* nonnull */) {
383 sp<AMessage> msg = new AMessage(kWhatGetSyncSettings, this);
384 sp<AMessage> response;
385 status_t err = msg->postAndAwaitResponse(&response);
386 if (err == OK && response != NULL) {
387 CHECK(response->findInt32("err", &err));
388 if (err == OK) {
389 readFromAMessage(response, sync, videoFps);
390 }
391 }
392 return err;
Wei Jia98160162015-02-04 17:01:11 -0800393}
394
Andreas Huber43c3e6c2011-01-05 12:17:08 -0800395void NuPlayer::pause() {
Lajos Molnar1d15ab52015-03-04 16:46:34 -0800396 (new AMessage(kWhatPause, this))->post();
Andreas Huber43c3e6c2011-01-05 12:17:08 -0800397}
398
Andreas Huber1aef2112011-01-04 14:01:29 -0800399void NuPlayer::resetAsync() {
Chong Zhang48296b72014-09-14 14:28:45 -0700400 if (mSource != NULL) {
401 // During a reset, the data source might be unresponsive already, we need to
402 // disconnect explicitly so that reads exit promptly.
403 // We can't queue the disconnect request to the looper, as it might be
404 // queued behind a stuck read and never gets processed.
405 // Doing a disconnect outside the looper to allows the pending reads to exit
406 // (either successfully or with error).
407 mSource->disconnect();
408 }
409
Lajos Molnar1d15ab52015-03-04 16:46:34 -0800410 (new AMessage(kWhatReset, this))->post();
Andreas Huber1aef2112011-01-04 14:01:29 -0800411}
412
Wei Jiae427abf2014-09-22 15:21:11 -0700413void NuPlayer::seekToAsync(int64_t seekTimeUs, bool needNotify) {
Lajos Molnar1d15ab52015-03-04 16:46:34 -0800414 sp<AMessage> msg = new AMessage(kWhatSeek, this);
Andreas Huber43c3e6c2011-01-05 12:17:08 -0800415 msg->setInt64("seekTimeUs", seekTimeUs);
Wei Jiae427abf2014-09-22 15:21:11 -0700416 msg->setInt32("needNotify", needNotify);
Andreas Huber43c3e6c2011-01-05 12:17:08 -0800417 msg->post();
418}
419
Andreas Huber53df1a42010-12-22 10:03:04 -0800420
Chong Zhang404fced2014-06-11 14:45:31 -0700421void NuPlayer::writeTrackInfo(
422 Parcel* reply, const sp<AMessage> format) const {
423 int32_t trackType;
424 CHECK(format->findInt32("type", &trackType));
425
Robert Shih755106e2015-04-30 14:36:45 -0700426 AString mime;
Robert Shih2e3a4252015-05-06 10:21:15 -0700427 if (!format->findString("mime", &mime)) {
428 // Java MediaPlayer only uses mimetype for subtitle and timedtext tracks.
429 // If we can't find the mimetype here it means that we wouldn't be needing
430 // the mimetype on the Java end. We still write a placeholder mime to keep the
431 // (de)serialization logic simple.
432 if (trackType == MEDIA_TRACK_TYPE_AUDIO) {
433 mime = "audio/";
434 } else if (trackType == MEDIA_TRACK_TYPE_VIDEO) {
435 mime = "video/";
436 } else {
437 TRESPASS();
438 }
439 }
Robert Shih755106e2015-04-30 14:36:45 -0700440
Chong Zhang404fced2014-06-11 14:45:31 -0700441 AString lang;
442 CHECK(format->findString("language", &lang));
443
444 reply->writeInt32(2); // write something non-zero
445 reply->writeInt32(trackType);
Robert Shih755106e2015-04-30 14:36:45 -0700446 reply->writeString16(String16(mime.c_str()));
Chong Zhang404fced2014-06-11 14:45:31 -0700447 reply->writeString16(String16(lang.c_str()));
448
449 if (trackType == MEDIA_TRACK_TYPE_SUBTITLE) {
Chong Zhang404fced2014-06-11 14:45:31 -0700450 int32_t isAuto, isDefault, isForced;
451 CHECK(format->findInt32("auto", &isAuto));
452 CHECK(format->findInt32("default", &isDefault));
453 CHECK(format->findInt32("forced", &isForced));
454
Chong Zhang404fced2014-06-11 14:45:31 -0700455 reply->writeInt32(isAuto);
456 reply->writeInt32(isDefault);
457 reply->writeInt32(isForced);
458 }
459}
460
Andreas Huberf9334412010-12-15 15:17:42 -0800461void NuPlayer::onMessageReceived(const sp<AMessage> &msg) {
462 switch (msg->what()) {
463 case kWhatSetDataSource:
464 {
Steve Block3856b092011-10-20 11:56:00 +0100465 ALOGV("kWhatSetDataSource");
Andreas Huberf9334412010-12-15 15:17:42 -0800466
467 CHECK(mSource == NULL);
468
Chong Zhang3de157d2014-08-05 20:54:44 -0700469 status_t err = OK;
Andreas Huber5bc087c2010-12-23 10:27:40 -0800470 sp<RefBase> obj;
471 CHECK(msg->findObject("source", &obj));
Chong Zhang3de157d2014-08-05 20:54:44 -0700472 if (obj != NULL) {
473 mSource = static_cast<Source *>(obj.get());
Chong Zhang3de157d2014-08-05 20:54:44 -0700474 } else {
475 err = UNKNOWN_ERROR;
476 }
Andreas Huber9575c962013-02-05 13:59:56 -0800477
478 CHECK(mDriver != NULL);
479 sp<NuPlayerDriver> driver = mDriver.promote();
480 if (driver != NULL) {
Chong Zhang3de157d2014-08-05 20:54:44 -0700481 driver->notifySetDataSourceCompleted(err);
Andreas Huber9575c962013-02-05 13:59:56 -0800482 }
483 break;
484 }
485
486 case kWhatPrepare:
487 {
488 mSource->prepareAsync();
Andreas Huberf9334412010-12-15 15:17:42 -0800489 break;
490 }
491
Chong Zhangdcb89b32013-08-06 09:44:47 -0700492 case kWhatGetTrackInfo:
493 {
Lajos Molnar3f274362015-03-05 14:35:41 -0800494 sp<AReplyToken> replyID;
Chong Zhangdcb89b32013-08-06 09:44:47 -0700495 CHECK(msg->senderAwaitsResponse(&replyID));
496
Chong Zhang404fced2014-06-11 14:45:31 -0700497 Parcel* reply;
498 CHECK(msg->findPointer("reply", (void**)&reply));
499
500 size_t inbandTracks = 0;
Chong Zhangdcb89b32013-08-06 09:44:47 -0700501 if (mSource != NULL) {
Chong Zhang404fced2014-06-11 14:45:31 -0700502 inbandTracks = mSource->getTrackCount();
503 }
504
Chong Zhanga7fa1d92014-06-11 14:49:23 -0700505 size_t ccTracks = 0;
506 if (mCCDecoder != NULL) {
507 ccTracks = mCCDecoder->getTrackCount();
508 }
509
Chong Zhang404fced2014-06-11 14:45:31 -0700510 // total track count
Chong Zhanga7fa1d92014-06-11 14:49:23 -0700511 reply->writeInt32(inbandTracks + ccTracks);
Chong Zhang404fced2014-06-11 14:45:31 -0700512
513 // write inband tracks
514 for (size_t i = 0; i < inbandTracks; ++i) {
515 writeTrackInfo(reply, mSource->getTrackInfo(i));
Chong Zhangdcb89b32013-08-06 09:44:47 -0700516 }
517
Chong Zhanga7fa1d92014-06-11 14:49:23 -0700518 // write CC track
519 for (size_t i = 0; i < ccTracks; ++i) {
520 writeTrackInfo(reply, mCCDecoder->getTrackInfo(i));
521 }
522
Chong Zhangdcb89b32013-08-06 09:44:47 -0700523 sp<AMessage> response = new AMessage;
Chong Zhangdcb89b32013-08-06 09:44:47 -0700524 response->postReply(replyID);
525 break;
526 }
527
Robert Shih7c4f0d72014-07-09 18:53:31 -0700528 case kWhatGetSelectedTrack:
529 {
530 status_t err = INVALID_OPERATION;
531 if (mSource != NULL) {
532 err = OK;
533
534 int32_t type32;
535 CHECK(msg->findInt32("type", (int32_t*)&type32));
536 media_track_type type = (media_track_type)type32;
537 ssize_t selectedTrack = mSource->getSelectedTrack(type);
538
539 Parcel* reply;
540 CHECK(msg->findPointer("reply", (void**)&reply));
541 reply->writeInt32(selectedTrack);
542 }
543
544 sp<AMessage> response = new AMessage;
545 response->setInt32("err", err);
546
Lajos Molnar3f274362015-03-05 14:35:41 -0800547 sp<AReplyToken> replyID;
Robert Shih7c4f0d72014-07-09 18:53:31 -0700548 CHECK(msg->senderAwaitsResponse(&replyID));
549 response->postReply(replyID);
550 break;
551 }
552
Chong Zhangdcb89b32013-08-06 09:44:47 -0700553 case kWhatSelectTrack:
554 {
Lajos Molnar3f274362015-03-05 14:35:41 -0800555 sp<AReplyToken> replyID;
Chong Zhangdcb89b32013-08-06 09:44:47 -0700556 CHECK(msg->senderAwaitsResponse(&replyID));
557
Chong Zhang404fced2014-06-11 14:45:31 -0700558 size_t trackIndex;
559 int32_t select;
Robert Shih6ffb1fd2014-10-29 16:24:32 -0700560 int64_t timeUs;
Chong Zhang404fced2014-06-11 14:45:31 -0700561 CHECK(msg->findSize("trackIndex", &trackIndex));
562 CHECK(msg->findInt32("select", &select));
Robert Shih6ffb1fd2014-10-29 16:24:32 -0700563 CHECK(msg->findInt64("timeUs", &timeUs));
Chong Zhang404fced2014-06-11 14:45:31 -0700564
Chong Zhangdcb89b32013-08-06 09:44:47 -0700565 status_t err = INVALID_OPERATION;
Chong Zhang404fced2014-06-11 14:45:31 -0700566
567 size_t inbandTracks = 0;
Chong Zhangdcb89b32013-08-06 09:44:47 -0700568 if (mSource != NULL) {
Chong Zhang404fced2014-06-11 14:45:31 -0700569 inbandTracks = mSource->getTrackCount();
570 }
Chong Zhanga7fa1d92014-06-11 14:49:23 -0700571 size_t ccTracks = 0;
572 if (mCCDecoder != NULL) {
573 ccTracks = mCCDecoder->getTrackCount();
574 }
Chong Zhang404fced2014-06-11 14:45:31 -0700575
576 if (trackIndex < inbandTracks) {
Robert Shih6ffb1fd2014-10-29 16:24:32 -0700577 err = mSource->selectTrack(trackIndex, select, timeUs);
Robert Shihd3b0bbb2014-07-23 15:00:25 -0700578
579 if (!select && err == OK) {
580 int32_t type;
581 sp<AMessage> info = mSource->getTrackInfo(trackIndex);
582 if (info != NULL
583 && info->findInt32("type", &type)
584 && type == MEDIA_TRACK_TYPE_TIMEDTEXT) {
585 ++mTimedTextGeneration;
586 }
587 }
Chong Zhanga7fa1d92014-06-11 14:49:23 -0700588 } else {
589 trackIndex -= inbandTracks;
590
591 if (trackIndex < ccTracks) {
592 err = mCCDecoder->selectTrack(trackIndex, select);
593 }
Chong Zhangdcb89b32013-08-06 09:44:47 -0700594 }
595
596 sp<AMessage> response = new AMessage;
597 response->setInt32("err", err);
598
599 response->postReply(replyID);
600 break;
601 }
602
Andreas Huberb7c8e912012-11-27 15:02:53 -0800603 case kWhatPollDuration:
604 {
605 int32_t generation;
606 CHECK(msg->findInt32("generation", &generation));
607
608 if (generation != mPollDurationGeneration) {
609 // stale
610 break;
611 }
612
613 int64_t durationUs;
614 if (mDriver != NULL && mSource->getDuration(&durationUs) == OK) {
615 sp<NuPlayerDriver> driver = mDriver.promote();
616 if (driver != NULL) {
617 driver->notifyDuration(durationUs);
618 }
619 }
620
621 msg->post(1000000ll); // poll again in a second.
622 break;
623 }
624
Lajos Molnar1de1e252015-04-30 18:18:34 -0700625 case kWhatSetVideoSurface:
Andreas Huberf9334412010-12-15 15:17:42 -0800626 {
Lajos Molnar1de1e252015-04-30 18:18:34 -0700627 ALOGV("kWhatSetVideoSurface");
Andreas Huberf9334412010-12-15 15:17:42 -0800628
629 sp<RefBase> obj;
Lajos Molnar1de1e252015-04-30 18:18:34 -0700630 CHECK(msg->findObject("surface", &obj));
631 sp<Surface> surface = static_cast<Surface *>(obj.get());
Lajos Molnarf4849522014-12-10 16:58:57 -0800632 if (mSource == NULL || mSource->getFormat(false /* audio */) == NULL) {
Lajos Molnar1de1e252015-04-30 18:18:34 -0700633 performSetSurface(surface);
Wei Jiafef808d2014-10-31 17:57:05 -0700634 break;
635 }
636
637 mDeferredActions.push_back(
638 new FlushDecoderAction(FLUSH_CMD_FLUSH /* audio */,
639 FLUSH_CMD_SHUTDOWN /* video */));
640
Lajos Molnar1de1e252015-04-30 18:18:34 -0700641 mDeferredActions.push_back(new SetSurfaceAction(surface));
James Dong0d268a32012-08-31 12:18:27 -0700642
Andreas Huber57a339c2012-12-03 11:18:00 -0800643 if (obj != NULL) {
Wei Jiafef808d2014-10-31 17:57:05 -0700644 if (mStarted) {
Andy Hung73535852014-09-05 11:42:58 -0700645 // Issue a seek to refresh the video screen only if started otherwise
646 // the extractor may not yet be started and will assert.
647 // If the video decoder is not set (perhaps audio only in this case)
648 // do not perform a seek as it is not needed.
Ronghua Wua73d9e02014-10-08 15:13:29 -0700649 int64_t currentPositionUs = 0;
650 if (getCurrentPosition(&currentPositionUs) == OK) {
651 mDeferredActions.push_back(
652 new SeekAction(currentPositionUs, false /* needNotify */));
653 }
Andy Hung73535852014-09-05 11:42:58 -0700654 }
Wei Jiaac428aa2014-09-02 19:01:34 -0700655
Andreas Huber57a339c2012-12-03 11:18:00 -0800656 // If there is a new surface texture, instantiate decoders
657 // again if possible.
658 mDeferredActions.push_back(
659 new SimpleAction(&NuPlayer::performScanSources));
660 }
661
Chong Zhangf8d71772014-11-26 15:08:34 -0800662 // After a flush without shutdown, decoder is paused.
663 // Don't resume it until source seek is done, otherwise it could
Chong Zhang7137ec72014-11-12 16:41:05 -0800664 // start pulling stale data too soon.
665 mDeferredActions.push_back(
Chong Zhangf8d71772014-11-26 15:08:34 -0800666 new ResumeDecoderAction(false /* needNotify */));
Chong Zhang7137ec72014-11-12 16:41:05 -0800667
Andreas Huber57a339c2012-12-03 11:18:00 -0800668 processDeferredActions();
Andreas Huberf9334412010-12-15 15:17:42 -0800669 break;
670 }
671
672 case kWhatSetAudioSink:
673 {
Steve Block3856b092011-10-20 11:56:00 +0100674 ALOGV("kWhatSetAudioSink");
Andreas Huberf9334412010-12-15 15:17:42 -0800675
676 sp<RefBase> obj;
677 CHECK(msg->findObject("sink", &obj));
678
679 mAudioSink = static_cast<MediaPlayerBase::AudioSink *>(obj.get());
680 break;
681 }
682
683 case kWhatStart:
684 {
Steve Block3856b092011-10-20 11:56:00 +0100685 ALOGV("kWhatStart");
Wei Jia94211742014-10-28 17:09:06 -0700686 if (mStarted) {
Chong Zhang8a048332015-05-06 15:16:28 -0700687 // do not resume yet if the source is still buffering
688 if (!mPausedForBuffering) {
689 onResume();
690 }
Wei Jia94211742014-10-28 17:09:06 -0700691 } else {
692 onStart();
Lajos Molnar09524832014-07-17 14:29:51 -0700693 }
Chong Zhangefbb6192015-01-30 17:13:27 -0800694 mPausedByClient = false;
Andreas Huberf9334412010-12-15 15:17:42 -0800695 break;
696 }
697
Lajos Molnar3a474aa2015-04-24 17:10:07 -0700698 case kWhatConfigPlayback:
Wei Jia98160162015-02-04 17:01:11 -0800699 {
Lajos Molnar3a474aa2015-04-24 17:10:07 -0700700 sp<AReplyToken> replyID;
701 CHECK(msg->senderAwaitsResponse(&replyID));
702 AudioPlaybackRate rate /* sanitized */;
703 readFromAMessage(msg, &rate);
704 status_t err = OK;
Wei Jia98160162015-02-04 17:01:11 -0800705 if (mRenderer != NULL) {
Lajos Molnar3a474aa2015-04-24 17:10:07 -0700706 err = mRenderer->setPlaybackSettings(rate);
707 }
708 if (err == OK) {
709 if (rate.mSpeed == 0.f) {
710 onPause();
711 // save all other settings (using non-paused speed)
712 // so we can restore them on start
713 AudioPlaybackRate newRate = rate;
714 newRate.mSpeed = mPlaybackSettings.mSpeed;
715 mPlaybackSettings = newRate;
716 } else { /* rate.mSpeed != 0.f */
717 onResume();
718 mPlaybackSettings = rate;
719 }
Wei Jia98160162015-02-04 17:01:11 -0800720 }
Ronghua Wu8db88132015-04-22 13:51:35 -0700721
722 if (mVideoDecoder != NULL) {
Ronghua Wuc8a70d32015-04-29 16:26:34 -0700723 float rate = getFrameRate();
724 if (rate > 0) {
Ronghua Wu8db88132015-04-22 13:51:35 -0700725 sp<AMessage> params = new AMessage();
Lajos Molnar3a474aa2015-04-24 17:10:07 -0700726 params->setFloat("operating-rate", rate * mPlaybackSettings.mSpeed);
Ronghua Wu8db88132015-04-22 13:51:35 -0700727 mVideoDecoder->setParameters(params);
728 }
729 }
730
Lajos Molnar3a474aa2015-04-24 17:10:07 -0700731 sp<AMessage> response = new AMessage;
732 response->setInt32("err", err);
733 response->postReply(replyID);
734 break;
735 }
736
737 case kWhatGetPlaybackSettings:
738 {
739 sp<AReplyToken> replyID;
740 CHECK(msg->senderAwaitsResponse(&replyID));
741 AudioPlaybackRate rate = mPlaybackSettings;
742 status_t err = OK;
743 if (mRenderer != NULL) {
744 err = mRenderer->getPlaybackSettings(&rate);
745 }
746 if (err == OK) {
747 // get playback settings used by renderer, as it may be
748 // slightly off due to audiosink not taking small changes.
749 mPlaybackSettings = rate;
750 if (mPaused) {
751 rate.mSpeed = 0.f;
752 }
753 }
754 sp<AMessage> response = new AMessage;
755 if (err == OK) {
756 writeToAMessage(response, rate);
757 }
758 response->setInt32("err", err);
759 response->postReply(replyID);
760 break;
761 }
762
763 case kWhatConfigSync:
764 {
765 sp<AReplyToken> replyID;
766 CHECK(msg->senderAwaitsResponse(&replyID));
767
768 ALOGV("kWhatConfigSync");
769 AVSyncSettings sync;
770 float videoFpsHint;
771 readFromAMessage(msg, &sync, &videoFpsHint);
772 status_t err = OK;
773 if (mRenderer != NULL) {
774 err = mRenderer->setSyncSettings(sync, videoFpsHint);
775 }
776 if (err == OK) {
777 mSyncSettings = sync;
778 mVideoFpsHint = videoFpsHint;
779 }
780 sp<AMessage> response = new AMessage;
781 response->setInt32("err", err);
782 response->postReply(replyID);
783 break;
784 }
785
786 case kWhatGetSyncSettings:
787 {
788 sp<AReplyToken> replyID;
789 CHECK(msg->senderAwaitsResponse(&replyID));
790 AVSyncSettings sync = mSyncSettings;
791 float videoFps = mVideoFpsHint;
792 status_t err = OK;
793 if (mRenderer != NULL) {
794 err = mRenderer->getSyncSettings(&sync, &videoFps);
795 if (err == OK) {
796 mSyncSettings = sync;
797 mVideoFpsHint = videoFps;
798 }
799 }
800 sp<AMessage> response = new AMessage;
801 if (err == OK) {
802 writeToAMessage(response, sync, videoFps);
803 }
804 response->setInt32("err", err);
805 response->postReply(replyID);
Wei Jia98160162015-02-04 17:01:11 -0800806 break;
807 }
808
Andreas Huberf9334412010-12-15 15:17:42 -0800809 case kWhatScanSources:
810 {
Andreas Huber1aef2112011-01-04 14:01:29 -0800811 int32_t generation;
812 CHECK(msg->findInt32("generation", &generation));
813 if (generation != mScanSourcesGeneration) {
814 // Drop obsolete msg.
815 break;
816 }
817
Andreas Huber5bc087c2010-12-23 10:27:40 -0800818 mScanSourcesPending = false;
819
Steve Block3856b092011-10-20 11:56:00 +0100820 ALOGV("scanning sources haveAudio=%d, haveVideo=%d",
Andreas Huber43c3e6c2011-01-05 12:17:08 -0800821 mAudioDecoder != NULL, mVideoDecoder != NULL);
822
Andreas Huberb7c8e912012-11-27 15:02:53 -0800823 bool mHadAnySourcesBefore =
824 (mAudioDecoder != NULL) || (mVideoDecoder != NULL);
825
Andy Hung282a7e32014-08-14 15:56:34 -0700826 // initialize video before audio because successful initialization of
827 // video may change deep buffer mode of audio.
Lajos Molnar1de1e252015-04-30 18:18:34 -0700828 if (mSurface != NULL) {
Haynes Mathew George5d246ef2012-07-09 10:36:57 -0700829 instantiateDecoder(false, &mVideoDecoder);
830 }
Andreas Huberf9334412010-12-15 15:17:42 -0800831
Ronghua Wua10fd232014-11-06 16:15:20 -0800832 // Don't try to re-open audio sink if there's an existing decoder.
833 if (mAudioSink != NULL && mAudioDecoder == NULL) {
834 sp<MetaData> audioMeta = mSource->getFormatMeta(true /* audio */);
835 sp<AMessage> videoFormat = mSource->getFormat(false /* audio */);
836 audio_stream_type_t streamType = mAudioSink->getAudioStreamType();
Andy Hung202bce12014-12-03 11:47:36 -0800837 const bool hasVideo = (videoFormat != NULL);
838 const bool canOffload = canOffloadStream(
839 audioMeta, hasVideo, true /* is_streaming */, streamType);
Ronghua Wua10fd232014-11-06 16:15:20 -0800840 if (canOffload) {
841 if (!mOffloadAudio) {
842 mRenderer->signalEnableOffloadAudio();
843 }
Andy Hung282a7e32014-08-14 15:56:34 -0700844 // open audio sink early under offload mode.
845 sp<AMessage> format = mSource->getFormat(true /*audio*/);
Andy Hung202bce12014-12-03 11:47:36 -0800846 tryOpenAudioSinkForOffload(format, hasVideo);
Andy Hung282a7e32014-08-14 15:56:34 -0700847 }
Andreas Huber5bc087c2010-12-23 10:27:40 -0800848 instantiateDecoder(true, &mAudioDecoder);
Andreas Huberf9334412010-12-15 15:17:42 -0800849 }
850
Andreas Huberb7c8e912012-11-27 15:02:53 -0800851 if (!mHadAnySourcesBefore
852 && (mAudioDecoder != NULL || mVideoDecoder != NULL)) {
853 // This is the first time we've found anything playable.
854
Andreas Huber9575c962013-02-05 13:59:56 -0800855 if (mSourceFlags & Source::FLAG_DYNAMIC_DURATION) {
Andreas Huberb7c8e912012-11-27 15:02:53 -0800856 schedulePollDuration();
857 }
858 }
859
Andreas Hubereac68ba2011-09-27 12:12:25 -0700860 status_t err;
861 if ((err = mSource->feedMoreTSData()) != OK) {
Andreas Huber1aef2112011-01-04 14:01:29 -0800862 if (mAudioDecoder == NULL && mVideoDecoder == NULL) {
863 // We're not currently decoding anything (no audio or
864 // video tracks found) and we just ran out of input data.
Andreas Hubereac68ba2011-09-27 12:12:25 -0700865
866 if (err == ERROR_END_OF_STREAM) {
867 notifyListener(MEDIA_PLAYBACK_COMPLETE, 0, 0);
868 } else {
869 notifyListener(MEDIA_ERROR, MEDIA_ERROR_UNKNOWN, err);
870 }
Andreas Huber1aef2112011-01-04 14:01:29 -0800871 }
Andreas Huberf9334412010-12-15 15:17:42 -0800872 break;
873 }
874
Andreas Huberfbe9d812012-08-31 14:05:27 -0700875 if ((mAudioDecoder == NULL && mAudioSink != NULL)
Lajos Molnar1de1e252015-04-30 18:18:34 -0700876 || (mVideoDecoder == NULL && mSurface != NULL)) {
Andreas Huberf9334412010-12-15 15:17:42 -0800877 msg->post(100000ll);
Andreas Huber5bc087c2010-12-23 10:27:40 -0800878 mScanSourcesPending = true;
Andreas Huberf9334412010-12-15 15:17:42 -0800879 }
880 break;
881 }
882
883 case kWhatVideoNotify:
884 case kWhatAudioNotify:
885 {
886 bool audio = msg->what() == kWhatAudioNotify;
887
Wei Jia88703c32014-08-06 11:24:07 -0700888 int32_t currentDecoderGeneration =
889 (audio? mAudioDecoderGeneration : mVideoDecoderGeneration);
890 int32_t requesterGeneration = currentDecoderGeneration - 1;
891 CHECK(msg->findInt32("generation", &requesterGeneration));
892
893 if (requesterGeneration != currentDecoderGeneration) {
894 ALOGV("got message from old %s decoder, generation(%d:%d)",
895 audio ? "audio" : "video", requesterGeneration,
896 currentDecoderGeneration);
897 sp<AMessage> reply;
898 if (!(msg->findMessage("reply", &reply))) {
899 return;
900 }
901
902 reply->setInt32("err", INFO_DISCONTINUITY);
903 reply->post();
904 return;
905 }
906
Andreas Huberf9334412010-12-15 15:17:42 -0800907 int32_t what;
Lajos Molnar1cd13982014-01-17 15:12:51 -0800908 CHECK(msg->findInt32("what", &what));
Andreas Huberf9334412010-12-15 15:17:42 -0800909
Chong Zhang7137ec72014-11-12 16:41:05 -0800910 if (what == DecoderBase::kWhatInputDiscontinuity) {
911 int32_t formatChange;
912 CHECK(msg->findInt32("formatChange", &formatChange));
Andreas Huberf9334412010-12-15 15:17:42 -0800913
Chong Zhang7137ec72014-11-12 16:41:05 -0800914 ALOGV("%s discontinuity: formatChange %d",
915 audio ? "audio" : "video", formatChange);
916
917 if (formatChange) {
918 mDeferredActions.push_back(
919 new FlushDecoderAction(
920 audio ? FLUSH_CMD_SHUTDOWN : FLUSH_CMD_NONE,
921 audio ? FLUSH_CMD_NONE : FLUSH_CMD_SHUTDOWN));
Andreas Huberf9334412010-12-15 15:17:42 -0800922 }
Chong Zhang7137ec72014-11-12 16:41:05 -0800923
924 mDeferredActions.push_back(
925 new SimpleAction(
926 &NuPlayer::performScanSources));
927
928 processDeferredActions();
929 } else if (what == DecoderBase::kWhatEOS) {
Andreas Huberdc9bacd2011-09-26 10:53:29 -0700930 int32_t err;
Lajos Molnar1cd13982014-01-17 15:12:51 -0800931 CHECK(msg->findInt32("err", &err));
Andreas Huberdc9bacd2011-09-26 10:53:29 -0700932
933 if (err == ERROR_END_OF_STREAM) {
Steve Block3856b092011-10-20 11:56:00 +0100934 ALOGV("got %s decoder EOS", audio ? "audio" : "video");
Andreas Huberdc9bacd2011-09-26 10:53:29 -0700935 } else {
Steve Block3856b092011-10-20 11:56:00 +0100936 ALOGV("got %s decoder EOS w/ error %d",
Andreas Huberdc9bacd2011-09-26 10:53:29 -0700937 audio ? "audio" : "video",
938 err);
939 }
940
941 mRenderer->queueEOS(audio, err);
Chong Zhang7137ec72014-11-12 16:41:05 -0800942 } else if (what == DecoderBase::kWhatFlushCompleted) {
Steve Block3856b092011-10-20 11:56:00 +0100943 ALOGV("decoder %s flush completed", audio ? "audio" : "video");
Andreas Huberf9334412010-12-15 15:17:42 -0800944
Andy Hung8d121d42014-10-03 09:53:53 -0700945 handleFlushComplete(audio, true /* isDecoder */);
Andreas Huber3831a062010-12-21 10:22:33 -0800946 finishFlushIfPossible();
Chong Zhang7137ec72014-11-12 16:41:05 -0800947 } else if (what == DecoderBase::kWhatVideoSizeChanged) {
Lajos Molnar1cd13982014-01-17 15:12:51 -0800948 sp<AMessage> format;
949 CHECK(msg->findMessage("format", &format));
950
Wei Jiac6cfd702014-11-11 16:33:20 -0800951 sp<AMessage> inputFormat =
952 mSource->getFormat(false /* audio */);
Andreas Huber3831a062010-12-21 10:22:33 -0800953
Wei Jiac6cfd702014-11-11 16:33:20 -0800954 updateVideoSize(inputFormat, format);
Chong Zhang7137ec72014-11-12 16:41:05 -0800955 } else if (what == DecoderBase::kWhatShutdownCompleted) {
Steve Block3856b092011-10-20 11:56:00 +0100956 ALOGV("%s shutdown completed", audio ? "audio" : "video");
Andreas Huber3831a062010-12-21 10:22:33 -0800957 if (audio) {
958 mAudioDecoder.clear();
Wei Jia57568df2014-09-22 10:16:29 -0700959 ++mAudioDecoderGeneration;
Andreas Huber3831a062010-12-21 10:22:33 -0800960
961 CHECK_EQ((int)mFlushingAudio, (int)SHUTTING_DOWN_DECODER);
962 mFlushingAudio = SHUT_DOWN;
963 } else {
964 mVideoDecoder.clear();
Wei Jia57568df2014-09-22 10:16:29 -0700965 ++mVideoDecoderGeneration;
Andreas Huber3831a062010-12-21 10:22:33 -0800966
967 CHECK_EQ((int)mFlushingVideo, (int)SHUTTING_DOWN_DECODER);
968 mFlushingVideo = SHUT_DOWN;
969 }
970
971 finishFlushIfPossible();
Chong Zhangf8d71772014-11-26 15:08:34 -0800972 } else if (what == DecoderBase::kWhatResumeCompleted) {
973 finishResume();
Chong Zhang7137ec72014-11-12 16:41:05 -0800974 } else if (what == DecoderBase::kWhatError) {
Chong Zhangf4c0a942014-08-11 15:14:10 -0700975 status_t err;
Andy Hung2abde2c2014-09-30 14:40:32 -0700976 if (!msg->findInt32("err", &err) || err == OK) {
Chong Zhangf4c0a942014-08-11 15:14:10 -0700977 err = UNKNOWN_ERROR;
978 }
Andy Hungcf31f1e2014-09-23 14:59:01 -0700979
Andy Hung2abde2c2014-09-30 14:40:32 -0700980 // Decoder errors can be due to Source (e.g. from streaming),
981 // or from decoding corrupted bitstreams, or from other decoder
982 // MediaCodec operations (e.g. from an ongoing reset or seek).
Andy Hung202bce12014-12-03 11:47:36 -0800983 // They may also be due to openAudioSink failure at
984 // decoder start or after a format change.
Andy Hung2abde2c2014-09-30 14:40:32 -0700985 //
986 // We try to gracefully shut down the affected decoder if possible,
987 // rather than trying to force the shutdown with something
988 // similar to performReset(). This method can lead to a hang
989 // if MediaCodec functions block after an error, but they should
990 // typically return INVALID_OPERATION instead of blocking.
991
992 FlushStatus *flushing = audio ? &mFlushingAudio : &mFlushingVideo;
993 ALOGE("received error(%#x) from %s decoder, flushing(%d), now shutting down",
994 err, audio ? "audio" : "video", *flushing);
995
996 switch (*flushing) {
997 case NONE:
998 mDeferredActions.push_back(
Wei Jiafef808d2014-10-31 17:57:05 -0700999 new FlushDecoderAction(
1000 audio ? FLUSH_CMD_SHUTDOWN : FLUSH_CMD_NONE,
1001 audio ? FLUSH_CMD_NONE : FLUSH_CMD_SHUTDOWN));
Andy Hung2abde2c2014-09-30 14:40:32 -07001002 processDeferredActions();
1003 break;
1004 case FLUSHING_DECODER:
1005 *flushing = FLUSHING_DECODER_SHUTDOWN; // initiate shutdown after flush.
1006 break; // Wait for flush to complete.
1007 case FLUSHING_DECODER_SHUTDOWN:
1008 break; // Wait for flush to complete.
1009 case SHUTTING_DOWN_DECODER:
1010 break; // Wait for shutdown to complete.
1011 case FLUSHED:
1012 // Widevine source reads must stop before releasing the video decoder.
1013 if (!audio && mSource != NULL && mSourceFlags & Source::FLAG_SECURE) {
1014 mSource->stop();
1015 }
1016 getDecoder(audio)->initiateShutdown(); // In the middle of a seek.
1017 *flushing = SHUTTING_DOWN_DECODER; // Shut down.
1018 break;
1019 case SHUT_DOWN:
1020 finishFlushIfPossible(); // Should not occur.
1021 break; // Finish anyways.
Marco Nelissen9e2b7912014-08-18 16:13:03 -07001022 }
Andy Hung2abde2c2014-09-30 14:40:32 -07001023 notifyListener(MEDIA_ERROR, MEDIA_ERROR_UNKNOWN, err);
Lajos Molnar1cd13982014-01-17 15:12:51 -08001024 } else {
1025 ALOGV("Unhandled decoder notification %d '%c%c%c%c'.",
Andreas Hubera1f8ab02012-11-30 10:53:22 -08001026 what,
1027 what >> 24,
1028 (what >> 16) & 0xff,
1029 (what >> 8) & 0xff,
1030 what & 0xff);
Andreas Huberf9334412010-12-15 15:17:42 -08001031 }
1032
1033 break;
1034 }
1035
1036 case kWhatRendererNotify:
1037 {
Wei Jia57568df2014-09-22 10:16:29 -07001038 int32_t requesterGeneration = mRendererGeneration - 1;
1039 CHECK(msg->findInt32("generation", &requesterGeneration));
1040 if (requesterGeneration != mRendererGeneration) {
1041 ALOGV("got message from old renderer, generation(%d:%d)",
1042 requesterGeneration, mRendererGeneration);
1043 return;
1044 }
1045
Andreas Huberf9334412010-12-15 15:17:42 -08001046 int32_t what;
1047 CHECK(msg->findInt32("what", &what));
1048
1049 if (what == Renderer::kWhatEOS) {
1050 int32_t audio;
1051 CHECK(msg->findInt32("audio", &audio));
1052
Andreas Huberc92fd242011-08-16 13:48:44 -07001053 int32_t finalResult;
1054 CHECK(msg->findInt32("finalResult", &finalResult));
1055
Andreas Huberf9334412010-12-15 15:17:42 -08001056 if (audio) {
1057 mAudioEOS = true;
1058 } else {
1059 mVideoEOS = true;
1060 }
1061
Andreas Huberc92fd242011-08-16 13:48:44 -07001062 if (finalResult == ERROR_END_OF_STREAM) {
Steve Block3856b092011-10-20 11:56:00 +01001063 ALOGV("reached %s EOS", audio ? "audio" : "video");
Andreas Huberc92fd242011-08-16 13:48:44 -07001064 } else {
Steve Block29357bc2012-01-06 19:20:56 +00001065 ALOGE("%s track encountered an error (%d)",
Andreas Huberc92fd242011-08-16 13:48:44 -07001066 audio ? "audio" : "video", finalResult);
1067
1068 notifyListener(
1069 MEDIA_ERROR, MEDIA_ERROR_UNKNOWN, finalResult);
1070 }
Andreas Huberf9334412010-12-15 15:17:42 -08001071
1072 if ((mAudioEOS || mAudioDecoder == NULL)
1073 && (mVideoEOS || mVideoDecoder == NULL)) {
1074 notifyListener(MEDIA_PLAYBACK_COMPLETE, 0, 0);
1075 }
Andreas Huber3fe62152011-09-16 15:09:22 -07001076 } else if (what == Renderer::kWhatFlushComplete) {
Andreas Huberf9334412010-12-15 15:17:42 -08001077 int32_t audio;
1078 CHECK(msg->findInt32("audio", &audio));
1079
Steve Block3856b092011-10-20 11:56:00 +01001080 ALOGV("renderer %s flush completed.", audio ? "audio" : "video");
Andy Hung8d121d42014-10-03 09:53:53 -07001081 handleFlushComplete(audio, false /* isDecoder */);
1082 finishFlushIfPossible();
James Dongf57b4ea2012-07-20 13:38:36 -07001083 } else if (what == Renderer::kWhatVideoRenderingStart) {
1084 notifyListener(MEDIA_INFO, MEDIA_INFO_RENDERING_START, 0);
Lajos Molnarcbaffcf2013-08-14 18:30:38 -07001085 } else if (what == Renderer::kWhatMediaRenderingStart) {
1086 ALOGV("media rendering started");
1087 notifyListener(MEDIA_STARTED, 0, 0);
Wei Jia3a2956d2014-07-22 16:01:33 -07001088 } else if (what == Renderer::kWhatAudioOffloadTearDown) {
Ronghua Wua10fd232014-11-06 16:15:20 -08001089 ALOGV("Tear down audio offload, fall back to s/w path if due to error.");
Wei Jia3a2956d2014-07-22 16:01:33 -07001090 int64_t positionUs;
1091 CHECK(msg->findInt64("positionUs", &positionUs));
Ronghua Wu08529172014-10-02 16:55:52 -07001092 int32_t reason;
1093 CHECK(msg->findInt32("reason", &reason));
Andy Hung282a7e32014-08-14 15:56:34 -07001094 closeAudioSink();
Wei Jia3a2956d2014-07-22 16:01:33 -07001095 mAudioDecoder.clear();
Wei Jia57568df2014-09-22 10:16:29 -07001096 ++mAudioDecoderGeneration;
Chong Zhang7137ec72014-11-12 16:41:05 -08001097 mRenderer->flush(
1098 true /* audio */, false /* notifyComplete */);
Wei Jia3a2956d2014-07-22 16:01:33 -07001099 if (mVideoDecoder != NULL) {
Chong Zhang7137ec72014-11-12 16:41:05 -08001100 mRenderer->flush(
1101 false /* audio */, false /* notifyComplete */);
Wei Jia3a2956d2014-07-22 16:01:33 -07001102 }
Wei Jia3a2956d2014-07-22 16:01:33 -07001103
Wei Jiae427abf2014-09-22 15:21:11 -07001104 performSeek(positionUs, false /* needNotify */);
Ronghua Wu08529172014-10-02 16:55:52 -07001105 if (reason == Renderer::kDueToError) {
Ronghua Wua10fd232014-11-06 16:15:20 -08001106 mRenderer->signalDisableOffloadAudio();
1107 mOffloadAudio = false;
Ronghua Wu08529172014-10-02 16:55:52 -07001108 instantiateDecoder(true /* audio */, &mAudioDecoder);
1109 }
Andreas Huberf9334412010-12-15 15:17:42 -08001110 }
1111 break;
1112 }
1113
1114 case kWhatMoreDataQueued:
1115 {
1116 break;
1117 }
1118
Andreas Huber1aef2112011-01-04 14:01:29 -08001119 case kWhatReset:
1120 {
Steve Block3856b092011-10-20 11:56:00 +01001121 ALOGV("kWhatReset");
Andreas Huber1aef2112011-01-04 14:01:29 -08001122
Andreas Hubera1f8ab02012-11-30 10:53:22 -08001123 mDeferredActions.push_back(
Wei Jiafef808d2014-10-31 17:57:05 -07001124 new FlushDecoderAction(
1125 FLUSH_CMD_SHUTDOWN /* audio */,
1126 FLUSH_CMD_SHUTDOWN /* video */));
Andreas Huberb7c8e912012-11-27 15:02:53 -08001127
Andreas Hubera1f8ab02012-11-30 10:53:22 -08001128 mDeferredActions.push_back(
1129 new SimpleAction(&NuPlayer::performReset));
Andreas Huberb58ce9f2011-11-28 16:27:35 -08001130
Andreas Hubera1f8ab02012-11-30 10:53:22 -08001131 processDeferredActions();
Andreas Huber1aef2112011-01-04 14:01:29 -08001132 break;
1133 }
1134
Andreas Huber43c3e6c2011-01-05 12:17:08 -08001135 case kWhatSeek:
1136 {
1137 int64_t seekTimeUs;
Wei Jiae427abf2014-09-22 15:21:11 -07001138 int32_t needNotify;
Andreas Huber43c3e6c2011-01-05 12:17:08 -08001139 CHECK(msg->findInt64("seekTimeUs", &seekTimeUs));
Wei Jiae427abf2014-09-22 15:21:11 -07001140 CHECK(msg->findInt32("needNotify", &needNotify));
Andreas Huber43c3e6c2011-01-05 12:17:08 -08001141
Wei Jiae427abf2014-09-22 15:21:11 -07001142 ALOGV("kWhatSeek seekTimeUs=%lld us, needNotify=%d",
Lajos Molnar6d339f12015-04-17 16:15:53 -07001143 (long long)seekTimeUs, needNotify);
Andreas Huber43c3e6c2011-01-05 12:17:08 -08001144
Andreas Hubera1f8ab02012-11-30 10:53:22 -08001145 mDeferredActions.push_back(
Wei Jiafef808d2014-10-31 17:57:05 -07001146 new FlushDecoderAction(FLUSH_CMD_FLUSH /* audio */,
1147 FLUSH_CMD_FLUSH /* video */));
Andreas Huber43c3e6c2011-01-05 12:17:08 -08001148
Wei Jiae427abf2014-09-22 15:21:11 -07001149 mDeferredActions.push_back(
1150 new SeekAction(seekTimeUs, needNotify));
Andreas Huber43c3e6c2011-01-05 12:17:08 -08001151
Chong Zhangf8d71772014-11-26 15:08:34 -08001152 // After a flush without shutdown, decoder is paused.
1153 // Don't resume it until source seek is done, otherwise it could
Chong Zhang7137ec72014-11-12 16:41:05 -08001154 // start pulling stale data too soon.
1155 mDeferredActions.push_back(
Chong Zhangf8d71772014-11-26 15:08:34 -08001156 new ResumeDecoderAction(needNotify));
Chong Zhang7137ec72014-11-12 16:41:05 -08001157
Andreas Hubera1f8ab02012-11-30 10:53:22 -08001158 processDeferredActions();
Andreas Huber43c3e6c2011-01-05 12:17:08 -08001159 break;
1160 }
1161
Andreas Huberb4082222011-01-20 15:23:04 -08001162 case kWhatPause:
1163 {
Chong Zhangefbb6192015-01-30 17:13:27 -08001164 onPause();
1165 mPausedByClient = true;
Andreas Huberb4082222011-01-20 15:23:04 -08001166 break;
1167 }
1168
Andreas Huberb5f25f02013-02-05 10:14:26 -08001169 case kWhatSourceNotify:
1170 {
Andreas Huber9575c962013-02-05 13:59:56 -08001171 onSourceNotify(msg);
Andreas Huberb5f25f02013-02-05 10:14:26 -08001172 break;
1173 }
1174
Chong Zhanga7fa1d92014-06-11 14:49:23 -07001175 case kWhatClosedCaptionNotify:
1176 {
1177 onClosedCaptionNotify(msg);
1178 break;
1179 }
1180
Andreas Huberf9334412010-12-15 15:17:42 -08001181 default:
1182 TRESPASS();
1183 break;
1184 }
1185}
1186
Wei Jia94211742014-10-28 17:09:06 -07001187void NuPlayer::onResume() {
Chong Zhangefbb6192015-01-30 17:13:27 -08001188 if (!mPaused) {
1189 return;
1190 }
1191 mPaused = false;
Wei Jia94211742014-10-28 17:09:06 -07001192 if (mSource != NULL) {
1193 mSource->resume();
1194 } else {
1195 ALOGW("resume called when source is gone or not set");
1196 }
1197 // |mAudioDecoder| may have been released due to the pause timeout, so re-create it if
1198 // needed.
1199 if (audioDecoderStillNeeded() && mAudioDecoder == NULL) {
1200 instantiateDecoder(true /* audio */, &mAudioDecoder);
1201 }
1202 if (mRenderer != NULL) {
1203 mRenderer->resume();
1204 } else {
1205 ALOGW("resume called when renderer is gone or not set");
1206 }
1207}
1208
Lajos Molnarfcd3e942015-03-31 10:06:48 -07001209status_t NuPlayer::onInstantiateSecureDecoders() {
1210 status_t err;
1211 if (!(mSourceFlags & Source::FLAG_SECURE)) {
1212 return BAD_TYPE;
1213 }
1214
1215 if (mRenderer != NULL) {
1216 ALOGE("renderer should not be set when instantiating secure decoders");
1217 return UNKNOWN_ERROR;
1218 }
1219
1220 // TRICKY: We rely on mRenderer being null, so that decoder does not start requesting
1221 // data on instantiation.
Lajos Molnar1de1e252015-04-30 18:18:34 -07001222 if (mSurface != NULL) {
Lajos Molnarfcd3e942015-03-31 10:06:48 -07001223 err = instantiateDecoder(false, &mVideoDecoder);
1224 if (err != OK) {
1225 return err;
1226 }
1227 }
1228
1229 if (mAudioSink != NULL) {
1230 err = instantiateDecoder(true, &mAudioDecoder);
1231 if (err != OK) {
1232 return err;
1233 }
1234 }
1235 return OK;
1236}
1237
Wei Jia94211742014-10-28 17:09:06 -07001238void NuPlayer::onStart() {
Wei Jia94211742014-10-28 17:09:06 -07001239 mOffloadAudio = false;
1240 mAudioEOS = false;
1241 mVideoEOS = false;
Wei Jia94211742014-10-28 17:09:06 -07001242 mStarted = true;
1243
Wei Jia94211742014-10-28 17:09:06 -07001244 mSource->start();
1245
1246 uint32_t flags = 0;
1247
1248 if (mSource->isRealTime()) {
1249 flags |= Renderer::FLAG_REAL_TIME;
1250 }
1251
1252 sp<MetaData> audioMeta = mSource->getFormatMeta(true /* audio */);
1253 audio_stream_type_t streamType = AUDIO_STREAM_MUSIC;
1254 if (mAudioSink != NULL) {
1255 streamType = mAudioSink->getAudioStreamType();
1256 }
1257
1258 sp<AMessage> videoFormat = mSource->getFormat(false /* audio */);
1259
1260 mOffloadAudio =
1261 canOffloadStream(audioMeta, (videoFormat != NULL),
1262 true /* is_streaming */, streamType);
1263 if (mOffloadAudio) {
1264 flags |= Renderer::FLAG_OFFLOAD_AUDIO;
1265 }
1266
Lajos Molnar1d15ab52015-03-04 16:46:34 -08001267 sp<AMessage> notify = new AMessage(kWhatRendererNotify, this);
Wei Jia94211742014-10-28 17:09:06 -07001268 ++mRendererGeneration;
1269 notify->setInt32("generation", mRendererGeneration);
1270 mRenderer = new Renderer(mAudioSink, notify, flags);
Wei Jia94211742014-10-28 17:09:06 -07001271 mRendererLooper = new ALooper;
1272 mRendererLooper->setName("NuPlayerRenderer");
1273 mRendererLooper->start(false, false, ANDROID_PRIORITY_AUDIO);
1274 mRendererLooper->registerHandler(mRenderer);
Lajos Molnar3a474aa2015-04-24 17:10:07 -07001275
1276 status_t err = mRenderer->setPlaybackSettings(mPlaybackSettings);
1277 if (err != OK) {
1278 mSource->stop();
1279 notifyListener(MEDIA_ERROR, MEDIA_ERROR_UNKNOWN, err);
1280 return;
Wei Jiac8206ff2015-03-04 13:59:37 -08001281 }
Wei Jia94211742014-10-28 17:09:06 -07001282
Ronghua Wuc8a70d32015-04-29 16:26:34 -07001283 float rate = getFrameRate();
1284 if (rate > 0) {
Wei Jia94211742014-10-28 17:09:06 -07001285 mRenderer->setVideoFrameRate(rate);
1286 }
1287
Wei Jiac6cfd702014-11-11 16:33:20 -08001288 if (mVideoDecoder != NULL) {
1289 mVideoDecoder->setRenderer(mRenderer);
1290 }
1291 if (mAudioDecoder != NULL) {
1292 mAudioDecoder->setRenderer(mRenderer);
1293 }
1294
Wei Jia94211742014-10-28 17:09:06 -07001295 postScanSources();
1296}
1297
Chong Zhangefbb6192015-01-30 17:13:27 -08001298void NuPlayer::onPause() {
1299 if (mPaused) {
1300 return;
1301 }
1302 mPaused = true;
1303 if (mSource != NULL) {
1304 mSource->pause();
1305 } else {
1306 ALOGW("pause called when source is gone or not set");
1307 }
1308 if (mRenderer != NULL) {
1309 mRenderer->pause();
1310 } else {
1311 ALOGW("pause called when renderer is gone or not set");
1312 }
1313}
1314
Ronghua Wud7988b12014-10-03 15:19:10 -07001315bool NuPlayer::audioDecoderStillNeeded() {
1316 // Audio decoder is no longer needed if it's in shut/shutting down status.
1317 return ((mFlushingAudio != SHUT_DOWN) && (mFlushingAudio != SHUTTING_DOWN_DECODER));
1318}
1319
Andy Hung8d121d42014-10-03 09:53:53 -07001320void NuPlayer::handleFlushComplete(bool audio, bool isDecoder) {
1321 // We wait for both the decoder flush and the renderer flush to complete
1322 // before entering either the FLUSHED or the SHUTTING_DOWN_DECODER state.
1323
1324 mFlushComplete[audio][isDecoder] = true;
1325 if (!mFlushComplete[audio][!isDecoder]) {
1326 return;
1327 }
1328
1329 FlushStatus *state = audio ? &mFlushingAudio : &mFlushingVideo;
1330 switch (*state) {
1331 case FLUSHING_DECODER:
1332 {
1333 *state = FLUSHED;
Andy Hung8d121d42014-10-03 09:53:53 -07001334 break;
1335 }
1336
1337 case FLUSHING_DECODER_SHUTDOWN:
1338 {
1339 *state = SHUTTING_DOWN_DECODER;
1340
1341 ALOGV("initiating %s decoder shutdown", audio ? "audio" : "video");
1342 if (!audio) {
Andy Hung8d121d42014-10-03 09:53:53 -07001343 // Widevine source reads must stop before releasing the video decoder.
1344 if (mSource != NULL && mSourceFlags & Source::FLAG_SECURE) {
1345 mSource->stop();
1346 }
1347 }
1348 getDecoder(audio)->initiateShutdown();
1349 break;
1350 }
1351
1352 default:
1353 // decoder flush completes only occur in a flushing state.
1354 LOG_ALWAYS_FATAL_IF(isDecoder, "decoder flush in invalid state %d", *state);
1355 break;
1356 }
1357}
1358
Andreas Huber3831a062010-12-21 10:22:33 -08001359void NuPlayer::finishFlushIfPossible() {
Wei Jia53904f32014-07-29 10:22:53 -07001360 if (mFlushingAudio != NONE && mFlushingAudio != FLUSHED
1361 && mFlushingAudio != SHUT_DOWN) {
Andreas Huber3831a062010-12-21 10:22:33 -08001362 return;
1363 }
1364
Wei Jia53904f32014-07-29 10:22:53 -07001365 if (mFlushingVideo != NONE && mFlushingVideo != FLUSHED
1366 && mFlushingVideo != SHUT_DOWN) {
Andreas Huber3831a062010-12-21 10:22:33 -08001367 return;
1368 }
1369
Steve Block3856b092011-10-20 11:56:00 +01001370 ALOGV("both audio and video are flushed now.");
Andreas Huber3831a062010-12-21 10:22:33 -08001371
Andreas Huber3831a062010-12-21 10:22:33 -08001372 mFlushingAudio = NONE;
1373 mFlushingVideo = NONE;
Andreas Huber3831a062010-12-21 10:22:33 -08001374
Andy Hung8d121d42014-10-03 09:53:53 -07001375 clearFlushComplete();
1376
Andreas Hubera1f8ab02012-11-30 10:53:22 -08001377 processDeferredActions();
Andreas Huber1aef2112011-01-04 14:01:29 -08001378}
1379
1380void NuPlayer::postScanSources() {
1381 if (mScanSourcesPending) {
1382 return;
1383 }
1384
Lajos Molnar1d15ab52015-03-04 16:46:34 -08001385 sp<AMessage> msg = new AMessage(kWhatScanSources, this);
Andreas Huber1aef2112011-01-04 14:01:29 -08001386 msg->setInt32("generation", mScanSourcesGeneration);
1387 msg->post();
1388
1389 mScanSourcesPending = true;
1390}
1391
Andy Hung202bce12014-12-03 11:47:36 -08001392void NuPlayer::tryOpenAudioSinkForOffload(const sp<AMessage> &format, bool hasVideo) {
1393 // Note: This is called early in NuPlayer to determine whether offloading
1394 // is possible; otherwise the decoders call the renderer openAudioSink directly.
Andy Hung282a7e32014-08-14 15:56:34 -07001395
Andy Hung202bce12014-12-03 11:47:36 -08001396 status_t err = mRenderer->openAudioSink(
1397 format, true /* offloadOnly */, hasVideo, AUDIO_OUTPUT_FLAG_NONE, &mOffloadAudio);
1398 if (err != OK) {
1399 // Any failure we turn off mOffloadAudio.
1400 mOffloadAudio = false;
1401 } else if (mOffloadAudio) {
Chong Zhang3b9eb1f2014-10-15 17:05:08 -07001402 sp<MetaData> audioMeta =
1403 mSource->getFormatMeta(true /* audio */);
1404 sendMetaDataToHal(mAudioSink, audioMeta);
Andy Hung282a7e32014-08-14 15:56:34 -07001405 }
1406}
1407
1408void NuPlayer::closeAudioSink() {
Chong Zhang3b9eb1f2014-10-15 17:05:08 -07001409 mRenderer->closeAudioSink();
Andy Hung282a7e32014-08-14 15:56:34 -07001410}
1411
Chong Zhang7137ec72014-11-12 16:41:05 -08001412status_t NuPlayer::instantiateDecoder(bool audio, sp<DecoderBase> *decoder) {
Andreas Huberf9334412010-12-15 15:17:42 -08001413 if (*decoder != NULL) {
1414 return OK;
1415 }
1416
Andreas Huber84066782011-08-16 09:34:26 -07001417 sp<AMessage> format = mSource->getFormat(audio);
Andreas Huberf9334412010-12-15 15:17:42 -08001418
Andreas Huber84066782011-08-16 09:34:26 -07001419 if (format == NULL) {
Andreas Huberf9334412010-12-15 15:17:42 -08001420 return -EWOULDBLOCK;
1421 }
1422
Ronghua Wu8db88132015-04-22 13:51:35 -07001423 format->setInt32("priority", 0 /* realtime */);
1424
Andreas Huber3fe62152011-09-16 15:09:22 -07001425 if (!audio) {
Andreas Huber84066782011-08-16 09:34:26 -07001426 AString mime;
1427 CHECK(format->findString("mime", &mime));
Chong Zhanga7fa1d92014-06-11 14:49:23 -07001428
Lajos Molnar1d15ab52015-03-04 16:46:34 -08001429 sp<AMessage> ccNotify = new AMessage(kWhatClosedCaptionNotify, this);
Chong Zhang341ab6e2015-02-04 13:37:18 -08001430 if (mCCDecoder == NULL) {
1431 mCCDecoder = new CCDecoder(ccNotify);
1432 }
Lajos Molnar09524832014-07-17 14:29:51 -07001433
1434 if (mSourceFlags & Source::FLAG_SECURE) {
1435 format->setInt32("secure", true);
1436 }
Chong Zhang17134602015-01-07 16:14:34 -08001437
1438 if (mSourceFlags & Source::FLAG_PROTECTED) {
1439 format->setInt32("protected", true);
1440 }
Ronghua Wu8db88132015-04-22 13:51:35 -07001441
Ronghua Wuc8a70d32015-04-29 16:26:34 -07001442 float rate = getFrameRate();
1443 if (rate > 0) {
Lajos Molnar3a474aa2015-04-24 17:10:07 -07001444 format->setFloat("operating-rate", rate * mPlaybackSettings.mSpeed);
Ronghua Wu8db88132015-04-22 13:51:35 -07001445 }
Andreas Huber3fe62152011-09-16 15:09:22 -07001446 }
1447
Wei Jiabc2fb722014-07-08 16:37:57 -07001448 if (audio) {
Lajos Molnar1d15ab52015-03-04 16:46:34 -08001449 sp<AMessage> notify = new AMessage(kWhatAudioNotify, this);
Wei Jia88703c32014-08-06 11:24:07 -07001450 ++mAudioDecoderGeneration;
1451 notify->setInt32("generation", mAudioDecoderGeneration);
1452
Wei Jiabc2fb722014-07-08 16:37:57 -07001453 if (mOffloadAudio) {
Haynes Mathew George8b635332015-03-30 17:59:47 -07001454 const bool hasVideo = (mSource->getFormat(false /*audio */) != NULL);
1455 format->setInt32("has-video", hasVideo);
Wei Jiac6cfd702014-11-11 16:33:20 -08001456 *decoder = new DecoderPassThrough(notify, mSource, mRenderer);
Wei Jiabc2fb722014-07-08 16:37:57 -07001457 } else {
Wei Jiac6cfd702014-11-11 16:33:20 -08001458 *decoder = new Decoder(notify, mSource, mRenderer);
Wei Jiabc2fb722014-07-08 16:37:57 -07001459 }
1460 } else {
Lajos Molnar1d15ab52015-03-04 16:46:34 -08001461 sp<AMessage> notify = new AMessage(kWhatVideoNotify, this);
Wei Jia88703c32014-08-06 11:24:07 -07001462 ++mVideoDecoderGeneration;
1463 notify->setInt32("generation", mVideoDecoderGeneration);
1464
Chong Zhang7137ec72014-11-12 16:41:05 -08001465 *decoder = new Decoder(
Lajos Molnar1de1e252015-04-30 18:18:34 -07001466 notify, mSource, mRenderer, mSurface, mCCDecoder);
Lajos Molnard9fd6312014-11-06 11:00:00 -08001467
1468 // enable FRC if high-quality AV sync is requested, even if not
Lajos Molnar1de1e252015-04-30 18:18:34 -07001469 // directly queuing to display, as this will even improve textureview
Lajos Molnard9fd6312014-11-06 11:00:00 -08001470 // playback.
1471 {
1472 char value[PROPERTY_VALUE_MAX];
1473 if (property_get("persist.sys.media.avsync", value, NULL) &&
1474 (!strcmp("1", value) || !strcasecmp("true", value))) {
1475 format->setInt32("auto-frc", 1);
1476 }
1477 }
Wei Jiabc2fb722014-07-08 16:37:57 -07001478 }
Lajos Molnar1cd13982014-01-17 15:12:51 -08001479 (*decoder)->init();
Andreas Huber84066782011-08-16 09:34:26 -07001480 (*decoder)->configure(format);
Andreas Huberf9334412010-12-15 15:17:42 -08001481
Lajos Molnar09524832014-07-17 14:29:51 -07001482 // allocate buffers to decrypt widevine source buffers
1483 if (!audio && (mSourceFlags & Source::FLAG_SECURE)) {
1484 Vector<sp<ABuffer> > inputBufs;
1485 CHECK_EQ((*decoder)->getInputBuffers(&inputBufs), (status_t)OK);
1486
1487 Vector<MediaBuffer *> mediaBufs;
1488 for (size_t i = 0; i < inputBufs.size(); i++) {
1489 const sp<ABuffer> &buffer = inputBufs[i];
1490 MediaBuffer *mbuf = new MediaBuffer(buffer->data(), buffer->size());
1491 mediaBufs.push(mbuf);
1492 }
1493
1494 status_t err = mSource->setBuffers(audio, mediaBufs);
1495 if (err != OK) {
1496 for (size_t i = 0; i < mediaBufs.size(); ++i) {
1497 mediaBufs[i]->release();
1498 }
1499 mediaBufs.clear();
1500 ALOGE("Secure source didn't support secure mediaBufs.");
1501 return err;
1502 }
1503 }
Andreas Huberf9334412010-12-15 15:17:42 -08001504 return OK;
1505}
1506
Chong Zhangced1c2f2014-08-08 15:22:35 -07001507void NuPlayer::updateVideoSize(
1508 const sp<AMessage> &inputFormat,
1509 const sp<AMessage> &outputFormat) {
1510 if (inputFormat == NULL) {
1511 ALOGW("Unknown video size, reporting 0x0!");
1512 notifyListener(MEDIA_SET_VIDEO_SIZE, 0, 0);
1513 return;
1514 }
1515
1516 int32_t displayWidth, displayHeight;
Chong Zhangced1c2f2014-08-08 15:22:35 -07001517 if (outputFormat != NULL) {
1518 int32_t width, height;
1519 CHECK(outputFormat->findInt32("width", &width));
1520 CHECK(outputFormat->findInt32("height", &height));
1521
1522 int32_t cropLeft, cropTop, cropRight, cropBottom;
1523 CHECK(outputFormat->findRect(
1524 "crop",
1525 &cropLeft, &cropTop, &cropRight, &cropBottom));
1526
1527 displayWidth = cropRight - cropLeft + 1;
1528 displayHeight = cropBottom - cropTop + 1;
1529
1530 ALOGV("Video output format changed to %d x %d "
1531 "(crop: %d x %d @ (%d, %d))",
1532 width, height,
1533 displayWidth,
1534 displayHeight,
1535 cropLeft, cropTop);
1536 } else {
1537 CHECK(inputFormat->findInt32("width", &displayWidth));
1538 CHECK(inputFormat->findInt32("height", &displayHeight));
1539
1540 ALOGV("Video input format %d x %d", displayWidth, displayHeight);
1541 }
1542
1543 // Take into account sample aspect ratio if necessary:
1544 int32_t sarWidth, sarHeight;
1545 if (inputFormat->findInt32("sar-width", &sarWidth)
1546 && inputFormat->findInt32("sar-height", &sarHeight)) {
1547 ALOGV("Sample aspect ratio %d : %d", sarWidth, sarHeight);
1548
1549 displayWidth = (displayWidth * sarWidth) / sarHeight;
1550
1551 ALOGV("display dimensions %d x %d", displayWidth, displayHeight);
1552 }
1553
1554 int32_t rotationDegrees;
1555 if (!inputFormat->findInt32("rotation-degrees", &rotationDegrees)) {
1556 rotationDegrees = 0;
1557 }
1558
1559 if (rotationDegrees == 90 || rotationDegrees == 270) {
1560 int32_t tmp = displayWidth;
1561 displayWidth = displayHeight;
1562 displayHeight = tmp;
1563 }
1564
1565 notifyListener(
1566 MEDIA_SET_VIDEO_SIZE,
1567 displayWidth,
1568 displayHeight);
1569}
1570
Chong Zhangdcb89b32013-08-06 09:44:47 -07001571void NuPlayer::notifyListener(int msg, int ext1, int ext2, const Parcel *in) {
Andreas Huber43c3e6c2011-01-05 12:17:08 -08001572 if (mDriver == NULL) {
Andreas Huberf9334412010-12-15 15:17:42 -08001573 return;
1574 }
1575
Andreas Huber43c3e6c2011-01-05 12:17:08 -08001576 sp<NuPlayerDriver> driver = mDriver.promote();
Andreas Huberf9334412010-12-15 15:17:42 -08001577
Andreas Huber43c3e6c2011-01-05 12:17:08 -08001578 if (driver == NULL) {
Andreas Huberf9334412010-12-15 15:17:42 -08001579 return;
1580 }
1581
Chong Zhangdcb89b32013-08-06 09:44:47 -07001582 driver->notifyListener(msg, ext1, ext2, in);
Andreas Huberf9334412010-12-15 15:17:42 -08001583}
1584
Chong Zhang7137ec72014-11-12 16:41:05 -08001585void NuPlayer::flushDecoder(bool audio, bool needShutdown) {
Andreas Huber14f76722013-01-15 09:04:18 -08001586 ALOGV("[%s] flushDecoder needShutdown=%d",
1587 audio ? "audio" : "video", needShutdown);
1588
Chong Zhang7137ec72014-11-12 16:41:05 -08001589 const sp<DecoderBase> &decoder = getDecoder(audio);
Lajos Molnar87603c02014-08-20 19:25:30 -07001590 if (decoder == NULL) {
Steve Blockdf64d152012-01-04 20:05:49 +00001591 ALOGI("flushDecoder %s without decoder present",
Andreas Huber6e3d3112011-11-28 12:36:11 -08001592 audio ? "audio" : "video");
Lajos Molnar87603c02014-08-20 19:25:30 -07001593 return;
Andreas Huber6e3d3112011-11-28 12:36:11 -08001594 }
1595
Andreas Huber1aef2112011-01-04 14:01:29 -08001596 // Make sure we don't continue to scan sources until we finish flushing.
1597 ++mScanSourcesGeneration;
Chong Zhang3b032b32015-04-17 15:49:06 -07001598 if (mScanSourcesPending) {
1599 mDeferredActions.push_back(
1600 new SimpleAction(&NuPlayer::performScanSources));
1601 mScanSourcesPending = false;
1602 }
Andreas Huber1aef2112011-01-04 14:01:29 -08001603
Chong Zhang7137ec72014-11-12 16:41:05 -08001604 decoder->signalFlush();
Andreas Huber1aef2112011-01-04 14:01:29 -08001605
1606 FlushStatus newStatus =
1607 needShutdown ? FLUSHING_DECODER_SHUTDOWN : FLUSHING_DECODER;
1608
Lajos Molnarfcd3e942015-03-31 10:06:48 -07001609 mFlushComplete[audio][false /* isDecoder */] = (mRenderer == NULL);
Andy Hung8d121d42014-10-03 09:53:53 -07001610 mFlushComplete[audio][true /* isDecoder */] = false;
Andreas Huber1aef2112011-01-04 14:01:29 -08001611 if (audio) {
Wei Jia53904f32014-07-29 10:22:53 -07001612 ALOGE_IF(mFlushingAudio != NONE,
1613 "audio flushDecoder() is called in state %d", mFlushingAudio);
Andreas Huber1aef2112011-01-04 14:01:29 -08001614 mFlushingAudio = newStatus;
Andreas Huber1aef2112011-01-04 14:01:29 -08001615 } else {
Wei Jia53904f32014-07-29 10:22:53 -07001616 ALOGE_IF(mFlushingVideo != NONE,
1617 "video flushDecoder() is called in state %d", mFlushingVideo);
Andreas Huber1aef2112011-01-04 14:01:29 -08001618 mFlushingVideo = newStatus;
Andreas Huber1aef2112011-01-04 14:01:29 -08001619 }
1620}
1621
Chong Zhangced1c2f2014-08-08 15:22:35 -07001622void NuPlayer::queueDecoderShutdown(
1623 bool audio, bool video, const sp<AMessage> &reply) {
1624 ALOGI("queueDecoderShutdown audio=%d, video=%d", audio, video);
Andreas Huber84066782011-08-16 09:34:26 -07001625
Chong Zhangced1c2f2014-08-08 15:22:35 -07001626 mDeferredActions.push_back(
Wei Jiafef808d2014-10-31 17:57:05 -07001627 new FlushDecoderAction(
1628 audio ? FLUSH_CMD_SHUTDOWN : FLUSH_CMD_NONE,
1629 video ? FLUSH_CMD_SHUTDOWN : FLUSH_CMD_NONE));
Andreas Huber84066782011-08-16 09:34:26 -07001630
Chong Zhangced1c2f2014-08-08 15:22:35 -07001631 mDeferredActions.push_back(
1632 new SimpleAction(&NuPlayer::performScanSources));
Andreas Huber84066782011-08-16 09:34:26 -07001633
Chong Zhangced1c2f2014-08-08 15:22:35 -07001634 mDeferredActions.push_back(new PostMessageAction(reply));
1635
1636 processDeferredActions();
Andreas Huber84066782011-08-16 09:34:26 -07001637}
1638
James Dong0d268a32012-08-31 12:18:27 -07001639status_t NuPlayer::setVideoScalingMode(int32_t mode) {
1640 mVideoScalingMode = mode;
Lajos Molnar1de1e252015-04-30 18:18:34 -07001641 if (mSurface != NULL) {
1642 status_t ret = native_window_set_scaling_mode(mSurface.get(), mVideoScalingMode);
James Dong0d268a32012-08-31 12:18:27 -07001643 if (ret != OK) {
1644 ALOGE("Failed to set scaling mode (%d): %s",
1645 -ret, strerror(-ret));
1646 return ret;
1647 }
1648 }
1649 return OK;
1650}
1651
Chong Zhangdcb89b32013-08-06 09:44:47 -07001652status_t NuPlayer::getTrackInfo(Parcel* reply) const {
Lajos Molnar1d15ab52015-03-04 16:46:34 -08001653 sp<AMessage> msg = new AMessage(kWhatGetTrackInfo, this);
Chong Zhangdcb89b32013-08-06 09:44:47 -07001654 msg->setPointer("reply", reply);
1655
1656 sp<AMessage> response;
1657 status_t err = msg->postAndAwaitResponse(&response);
1658 return err;
1659}
1660
Robert Shih7c4f0d72014-07-09 18:53:31 -07001661status_t NuPlayer::getSelectedTrack(int32_t type, Parcel* reply) const {
Lajos Molnar1d15ab52015-03-04 16:46:34 -08001662 sp<AMessage> msg = new AMessage(kWhatGetSelectedTrack, this);
Robert Shih7c4f0d72014-07-09 18:53:31 -07001663 msg->setPointer("reply", reply);
1664 msg->setInt32("type", type);
1665
1666 sp<AMessage> response;
1667 status_t err = msg->postAndAwaitResponse(&response);
1668 if (err == OK && response != NULL) {
1669 CHECK(response->findInt32("err", &err));
1670 }
1671 return err;
1672}
1673
Robert Shih6ffb1fd2014-10-29 16:24:32 -07001674status_t NuPlayer::selectTrack(size_t trackIndex, bool select, int64_t timeUs) {
Lajos Molnar1d15ab52015-03-04 16:46:34 -08001675 sp<AMessage> msg = new AMessage(kWhatSelectTrack, this);
Chong Zhangdcb89b32013-08-06 09:44:47 -07001676 msg->setSize("trackIndex", trackIndex);
1677 msg->setInt32("select", select);
Robert Shih6ffb1fd2014-10-29 16:24:32 -07001678 msg->setInt64("timeUs", timeUs);
Chong Zhangdcb89b32013-08-06 09:44:47 -07001679
1680 sp<AMessage> response;
1681 status_t err = msg->postAndAwaitResponse(&response);
1682
Chong Zhang404fced2014-06-11 14:45:31 -07001683 if (err != OK) {
1684 return err;
1685 }
1686
1687 if (!response->findInt32("err", &err)) {
1688 err = OK;
1689 }
1690
Chong Zhangdcb89b32013-08-06 09:44:47 -07001691 return err;
1692}
1693
Ronghua Wua73d9e02014-10-08 15:13:29 -07001694status_t NuPlayer::getCurrentPosition(int64_t *mediaUs) {
1695 sp<Renderer> renderer = mRenderer;
1696 if (renderer == NULL) {
1697 return NO_INIT;
1698 }
1699
1700 return renderer->getCurrentPosition(mediaUs);
1701}
1702
1703void NuPlayer::getStats(int64_t *numFramesTotal, int64_t *numFramesDropped) {
Chong Zhang7137ec72014-11-12 16:41:05 -08001704 sp<DecoderBase> decoder = getDecoder(false /* audio */);
1705 if (decoder != NULL) {
1706 decoder->getStats(numFramesTotal, numFramesDropped);
1707 } else {
1708 *numFramesTotal = 0;
1709 *numFramesDropped = 0;
1710 }
Ronghua Wua73d9e02014-10-08 15:13:29 -07001711}
1712
Marco Nelissenf0b72b52014-09-16 15:43:44 -07001713sp<MetaData> NuPlayer::getFileMeta() {
1714 return mSource->getFileFormatMeta();
1715}
1716
Ronghua Wuc8a70d32015-04-29 16:26:34 -07001717float NuPlayer::getFrameRate() {
1718 sp<MetaData> meta = mSource->getFormatMeta(false /* audio */);
1719 if (meta == NULL) {
1720 return 0;
1721 }
1722 int32_t rate;
1723 if (!meta->findInt32(kKeyFrameRate, &rate)) {
1724 // fall back to try file meta
1725 sp<MetaData> fileMeta = getFileMeta();
1726 if (fileMeta == NULL) {
1727 ALOGW("source has video meta but not file meta");
1728 return -1;
1729 }
1730 int32_t fileMetaRate;
1731 if (!fileMeta->findInt32(kKeyFrameRate, &fileMetaRate)) {
1732 return -1;
1733 }
1734 return fileMetaRate;
1735 }
1736 return rate;
1737}
1738
Andreas Huberb7c8e912012-11-27 15:02:53 -08001739void NuPlayer::schedulePollDuration() {
Lajos Molnar1d15ab52015-03-04 16:46:34 -08001740 sp<AMessage> msg = new AMessage(kWhatPollDuration, this);
Andreas Huberb7c8e912012-11-27 15:02:53 -08001741 msg->setInt32("generation", mPollDurationGeneration);
1742 msg->post();
1743}
1744
1745void NuPlayer::cancelPollDuration() {
1746 ++mPollDurationGeneration;
1747}
1748
Andreas Hubera1f8ab02012-11-30 10:53:22 -08001749void NuPlayer::processDeferredActions() {
1750 while (!mDeferredActions.empty()) {
1751 // We won't execute any deferred actions until we're no longer in
1752 // an intermediate state, i.e. one more more decoders are currently
1753 // flushing or shutting down.
1754
Andreas Hubera1f8ab02012-11-30 10:53:22 -08001755 if (mFlushingAudio != NONE || mFlushingVideo != NONE) {
1756 // We're currently flushing, postpone the reset until that's
1757 // completed.
1758
1759 ALOGV("postponing action mFlushingAudio=%d, mFlushingVideo=%d",
1760 mFlushingAudio, mFlushingVideo);
1761
1762 break;
1763 }
1764
1765 sp<Action> action = *mDeferredActions.begin();
1766 mDeferredActions.erase(mDeferredActions.begin());
1767
1768 action->execute(this);
1769 }
1770}
1771
Wei Jiae427abf2014-09-22 15:21:11 -07001772void NuPlayer::performSeek(int64_t seekTimeUs, bool needNotify) {
1773 ALOGV("performSeek seekTimeUs=%lld us (%.2f secs), needNotify(%d)",
Lajos Molnar6d339f12015-04-17 16:15:53 -07001774 (long long)seekTimeUs,
Wei Jiae427abf2014-09-22 15:21:11 -07001775 seekTimeUs / 1E6,
1776 needNotify);
Andreas Hubera1f8ab02012-11-30 10:53:22 -08001777
Andy Hungadf34bf2014-09-03 18:22:22 -07001778 if (mSource == NULL) {
1779 // This happens when reset occurs right before the loop mode
1780 // asynchronously seeks to the start of the stream.
1781 LOG_ALWAYS_FATAL_IF(mAudioDecoder != NULL || mVideoDecoder != NULL,
1782 "mSource is NULL and decoders not NULL audio(%p) video(%p)",
1783 mAudioDecoder.get(), mVideoDecoder.get());
1784 return;
1785 }
Andreas Hubera1f8ab02012-11-30 10:53:22 -08001786 mSource->seekTo(seekTimeUs);
Robert Shihd3b0bbb2014-07-23 15:00:25 -07001787 ++mTimedTextGeneration;
Andreas Hubera1f8ab02012-11-30 10:53:22 -08001788
Andreas Hubera1f8ab02012-11-30 10:53:22 -08001789 // everything's flushed, continue playback.
1790}
1791
Wei Jiafef808d2014-10-31 17:57:05 -07001792void NuPlayer::performDecoderFlush(FlushCommand audio, FlushCommand video) {
1793 ALOGV("performDecoderFlush audio=%d, video=%d", audio, video);
Andreas Hubera1f8ab02012-11-30 10:53:22 -08001794
Wei Jiafef808d2014-10-31 17:57:05 -07001795 if ((audio == FLUSH_CMD_NONE || mAudioDecoder == NULL)
1796 && (video == FLUSH_CMD_NONE || mVideoDecoder == NULL)) {
Andreas Hubera1f8ab02012-11-30 10:53:22 -08001797 return;
1798 }
1799
Wei Jiafef808d2014-10-31 17:57:05 -07001800 if (audio != FLUSH_CMD_NONE && mAudioDecoder != NULL) {
1801 flushDecoder(true /* audio */, (audio == FLUSH_CMD_SHUTDOWN));
Andreas Hubera1f8ab02012-11-30 10:53:22 -08001802 }
1803
Wei Jiafef808d2014-10-31 17:57:05 -07001804 if (video != FLUSH_CMD_NONE && mVideoDecoder != NULL) {
1805 flushDecoder(false /* audio */, (video == FLUSH_CMD_SHUTDOWN));
Andreas Hubera1f8ab02012-11-30 10:53:22 -08001806 }
1807}
1808
1809void NuPlayer::performReset() {
1810 ALOGV("performReset");
1811
1812 CHECK(mAudioDecoder == NULL);
1813 CHECK(mVideoDecoder == NULL);
1814
1815 cancelPollDuration();
1816
1817 ++mScanSourcesGeneration;
1818 mScanSourcesPending = false;
1819
Lajos Molnar09524832014-07-17 14:29:51 -07001820 if (mRendererLooper != NULL) {
1821 if (mRenderer != NULL) {
1822 mRendererLooper->unregisterHandler(mRenderer->id());
1823 }
1824 mRendererLooper->stop();
1825 mRendererLooper.clear();
1826 }
Andreas Hubera1f8ab02012-11-30 10:53:22 -08001827 mRenderer.clear();
Wei Jia57568df2014-09-22 10:16:29 -07001828 ++mRendererGeneration;
Andreas Hubera1f8ab02012-11-30 10:53:22 -08001829
1830 if (mSource != NULL) {
1831 mSource->stop();
Andreas Huberb5f25f02013-02-05 10:14:26 -08001832
Andreas Hubera1f8ab02012-11-30 10:53:22 -08001833 mSource.clear();
1834 }
1835
1836 if (mDriver != NULL) {
1837 sp<NuPlayerDriver> driver = mDriver.promote();
1838 if (driver != NULL) {
1839 driver->notifyResetComplete();
1840 }
1841 }
Andreas Huber57a339c2012-12-03 11:18:00 -08001842
1843 mStarted = false;
Andreas Hubera1f8ab02012-11-30 10:53:22 -08001844}
1845
1846void NuPlayer::performScanSources() {
1847 ALOGV("performScanSources");
1848
Andreas Huber57a339c2012-12-03 11:18:00 -08001849 if (!mStarted) {
1850 return;
1851 }
1852
Andreas Hubera1f8ab02012-11-30 10:53:22 -08001853 if (mAudioDecoder == NULL || mVideoDecoder == NULL) {
1854 postScanSources();
1855 }
1856}
1857
Lajos Molnar1de1e252015-04-30 18:18:34 -07001858void NuPlayer::performSetSurface(const sp<Surface> &surface) {
Andreas Huber57a339c2012-12-03 11:18:00 -08001859 ALOGV("performSetSurface");
1860
Lajos Molnar1de1e252015-04-30 18:18:34 -07001861 mSurface = surface;
Andreas Huber57a339c2012-12-03 11:18:00 -08001862
1863 // XXX - ignore error from setVideoScalingMode for now
1864 setVideoScalingMode(mVideoScalingMode);
Chong Zhang13d6faa2014-08-22 15:35:28 -07001865
1866 if (mDriver != NULL) {
1867 sp<NuPlayerDriver> driver = mDriver.promote();
1868 if (driver != NULL) {
1869 driver->notifySetSurfaceComplete();
1870 }
1871 }
Andreas Huber57a339c2012-12-03 11:18:00 -08001872}
1873
Chong Zhangf8d71772014-11-26 15:08:34 -08001874void NuPlayer::performResumeDecoders(bool needNotify) {
1875 if (needNotify) {
1876 mResumePending = true;
1877 if (mVideoDecoder == NULL) {
1878 // if audio-only, we can notify seek complete now,
1879 // as the resume operation will be relatively fast.
1880 finishResume();
1881 }
1882 }
1883
Chong Zhang7137ec72014-11-12 16:41:05 -08001884 if (mVideoDecoder != NULL) {
Chong Zhangf8d71772014-11-26 15:08:34 -08001885 // When there is continuous seek, MediaPlayer will cache the seek
1886 // position, and send down new seek request when previous seek is
1887 // complete. Let's wait for at least one video output frame before
1888 // notifying seek complete, so that the video thumbnail gets updated
1889 // when seekbar is dragged.
1890 mVideoDecoder->signalResume(needNotify);
Chong Zhang7137ec72014-11-12 16:41:05 -08001891 }
1892
1893 if (mAudioDecoder != NULL) {
Chong Zhangf8d71772014-11-26 15:08:34 -08001894 mAudioDecoder->signalResume(false /* needNotify */);
1895 }
1896}
1897
1898void NuPlayer::finishResume() {
1899 if (mResumePending) {
1900 mResumePending = false;
1901 if (mDriver != NULL) {
1902 sp<NuPlayerDriver> driver = mDriver.promote();
1903 if (driver != NULL) {
1904 driver->notifySeekComplete();
1905 }
1906 }
Chong Zhang7137ec72014-11-12 16:41:05 -08001907 }
1908}
1909
Andreas Huber9575c962013-02-05 13:59:56 -08001910void NuPlayer::onSourceNotify(const sp<AMessage> &msg) {
1911 int32_t what;
1912 CHECK(msg->findInt32("what", &what));
1913
1914 switch (what) {
Lajos Molnarfcd3e942015-03-31 10:06:48 -07001915 case Source::kWhatInstantiateSecureDecoders:
1916 {
1917 if (mSource == NULL) {
1918 // This is a stale notification from a source that was
1919 // asynchronously preparing when the client called reset().
1920 // We handled the reset, the source is gone.
1921 break;
1922 }
1923
1924 sp<AMessage> reply;
1925 CHECK(msg->findMessage("reply", &reply));
1926 status_t err = onInstantiateSecureDecoders();
1927 reply->setInt32("err", err);
1928 reply->post();
1929 break;
1930 }
1931
Andreas Huber9575c962013-02-05 13:59:56 -08001932 case Source::kWhatPrepared:
1933 {
Andreas Huberb5f28d42013-04-25 15:11:19 -07001934 if (mSource == NULL) {
1935 // This is a stale notification from a source that was
1936 // asynchronously preparing when the client called reset().
1937 // We handled the reset, the source is gone.
1938 break;
1939 }
1940
Andreas Huberec0c5972013-02-05 14:47:13 -08001941 int32_t err;
1942 CHECK(msg->findInt32("err", &err));
1943
Lajos Molnarfcd3e942015-03-31 10:06:48 -07001944 if (err != OK) {
1945 // shut down potential secure codecs in case client never calls reset
1946 mDeferredActions.push_back(
1947 new FlushDecoderAction(FLUSH_CMD_SHUTDOWN /* audio */,
1948 FLUSH_CMD_SHUTDOWN /* video */));
1949 processDeferredActions();
1950 }
1951
Andreas Huber9575c962013-02-05 13:59:56 -08001952 sp<NuPlayerDriver> driver = mDriver.promote();
1953 if (driver != NULL) {
Marco Nelissendd114d12014-05-28 15:23:14 -07001954 // notify duration first, so that it's definitely set when
1955 // the app received the "prepare complete" callback.
1956 int64_t durationUs;
1957 if (mSource->getDuration(&durationUs) == OK) {
1958 driver->notifyDuration(durationUs);
1959 }
Andreas Huberec0c5972013-02-05 14:47:13 -08001960 driver->notifyPrepareCompleted(err);
Andreas Huber9575c962013-02-05 13:59:56 -08001961 }
Andreas Huber99759402013-04-01 14:28:31 -07001962
Andreas Huber9575c962013-02-05 13:59:56 -08001963 break;
1964 }
1965
1966 case Source::kWhatFlagsChanged:
1967 {
1968 uint32_t flags;
1969 CHECK(msg->findInt32("flags", (int32_t *)&flags));
1970
Chong Zhang4b7069d2013-09-11 12:52:43 -07001971 sp<NuPlayerDriver> driver = mDriver.promote();
1972 if (driver != NULL) {
Wei Jia895651b2014-12-10 17:31:52 -08001973 if ((flags & NuPlayer::Source::FLAG_CAN_SEEK) == 0) {
1974 driver->notifyListener(
1975 MEDIA_INFO, MEDIA_INFO_NOT_SEEKABLE, 0);
1976 }
Chong Zhang4b7069d2013-09-11 12:52:43 -07001977 driver->notifyFlagsChanged(flags);
1978 }
1979
Andreas Huber9575c962013-02-05 13:59:56 -08001980 if ((mSourceFlags & Source::FLAG_DYNAMIC_DURATION)
1981 && (!(flags & Source::FLAG_DYNAMIC_DURATION))) {
1982 cancelPollDuration();
1983 } else if (!(mSourceFlags & Source::FLAG_DYNAMIC_DURATION)
1984 && (flags & Source::FLAG_DYNAMIC_DURATION)
1985 && (mAudioDecoder != NULL || mVideoDecoder != NULL)) {
1986 schedulePollDuration();
1987 }
1988
1989 mSourceFlags = flags;
1990 break;
1991 }
1992
1993 case Source::kWhatVideoSizeChanged:
1994 {
Chong Zhangced1c2f2014-08-08 15:22:35 -07001995 sp<AMessage> format;
1996 CHECK(msg->findMessage("format", &format));
Andreas Huber9575c962013-02-05 13:59:56 -08001997
Chong Zhangced1c2f2014-08-08 15:22:35 -07001998 updateVideoSize(format);
Andreas Huber9575c962013-02-05 13:59:56 -08001999 break;
2000 }
2001
Chong Zhang2a3cc9a2014-08-21 17:48:26 -07002002 case Source::kWhatBufferingUpdate:
2003 {
2004 int32_t percentage;
2005 CHECK(msg->findInt32("percentage", &percentage));
2006
2007 notifyListener(MEDIA_BUFFERING_UPDATE, percentage, 0);
2008 break;
2009 }
2010
Chong Zhangefbb6192015-01-30 17:13:27 -08002011 case Source::kWhatPauseOnBufferingStart:
2012 {
2013 // ignore if not playing
Chong Zhang8a048332015-05-06 15:16:28 -07002014 if (mStarted) {
Chong Zhangefbb6192015-01-30 17:13:27 -08002015 ALOGI("buffer low, pausing...");
2016
Chong Zhang8a048332015-05-06 15:16:28 -07002017 mPausedForBuffering = true;
Chong Zhangefbb6192015-01-30 17:13:27 -08002018 onPause();
2019 }
2020 // fall-thru
2021 }
2022
Roger Jönssonb50e83e2013-01-21 16:26:41 +01002023 case Source::kWhatBufferingStart:
2024 {
2025 notifyListener(MEDIA_INFO, MEDIA_INFO_BUFFERING_START, 0);
2026 break;
2027 }
2028
Chong Zhangefbb6192015-01-30 17:13:27 -08002029 case Source::kWhatResumeOnBufferingEnd:
2030 {
2031 // ignore if not playing
Chong Zhang8a048332015-05-06 15:16:28 -07002032 if (mStarted) {
Chong Zhangefbb6192015-01-30 17:13:27 -08002033 ALOGI("buffer ready, resuming...");
2034
Chong Zhang8a048332015-05-06 15:16:28 -07002035 mPausedForBuffering = false;
2036
2037 // do not resume yet if client didn't unpause
2038 if (!mPausedByClient) {
2039 onResume();
2040 }
Chong Zhangefbb6192015-01-30 17:13:27 -08002041 }
2042 // fall-thru
2043 }
2044
Roger Jönssonb50e83e2013-01-21 16:26:41 +01002045 case Source::kWhatBufferingEnd:
2046 {
2047 notifyListener(MEDIA_INFO, MEDIA_INFO_BUFFERING_END, 0);
2048 break;
2049 }
2050
Chong Zhangefbb6192015-01-30 17:13:27 -08002051 case Source::kWhatCacheStats:
2052 {
2053 int32_t kbps;
2054 CHECK(msg->findInt32("bandwidth", &kbps));
2055
2056 notifyListener(MEDIA_INFO, MEDIA_INFO_NETWORK_BANDWIDTH, kbps);
2057 break;
2058 }
2059
Chong Zhangdcb89b32013-08-06 09:44:47 -07002060 case Source::kWhatSubtitleData:
2061 {
2062 sp<ABuffer> buffer;
2063 CHECK(msg->findBuffer("buffer", &buffer));
2064
Chong Zhang404fced2014-06-11 14:45:31 -07002065 sendSubtitleData(buffer, 0 /* baseIndex */);
Chong Zhangdcb89b32013-08-06 09:44:47 -07002066 break;
2067 }
2068
Robert Shih08528432015-04-08 09:06:54 -07002069 case Source::kWhatTimedMetaData:
2070 {
2071 sp<ABuffer> buffer;
2072 if (!msg->findBuffer("buffer", &buffer)) {
2073 notifyListener(MEDIA_INFO, MEDIA_INFO_METADATA_UPDATE, 0);
2074 } else {
2075 sendTimedMetaData(buffer);
2076 }
2077 break;
2078 }
2079
Robert Shihd3b0bbb2014-07-23 15:00:25 -07002080 case Source::kWhatTimedTextData:
2081 {
2082 int32_t generation;
2083 if (msg->findInt32("generation", &generation)
2084 && generation != mTimedTextGeneration) {
2085 break;
2086 }
2087
2088 sp<ABuffer> buffer;
2089 CHECK(msg->findBuffer("buffer", &buffer));
2090
2091 sp<NuPlayerDriver> driver = mDriver.promote();
2092 if (driver == NULL) {
2093 break;
2094 }
2095
2096 int posMs;
2097 int64_t timeUs, posUs;
2098 driver->getCurrentPosition(&posMs);
2099 posUs = posMs * 1000;
2100 CHECK(buffer->meta()->findInt64("timeUs", &timeUs));
2101
2102 if (posUs < timeUs) {
2103 if (!msg->findInt32("generation", &generation)) {
2104 msg->setInt32("generation", mTimedTextGeneration);
2105 }
2106 msg->post(timeUs - posUs);
2107 } else {
2108 sendTimedTextData(buffer);
2109 }
2110 break;
2111 }
2112
Andreas Huber14f76722013-01-15 09:04:18 -08002113 case Source::kWhatQueueDecoderShutdown:
2114 {
2115 int32_t audio, video;
2116 CHECK(msg->findInt32("audio", &audio));
2117 CHECK(msg->findInt32("video", &video));
2118
2119 sp<AMessage> reply;
2120 CHECK(msg->findMessage("reply", &reply));
2121
2122 queueDecoderShutdown(audio, video, reply);
2123 break;
2124 }
2125
Ronghua Wu80276872014-08-28 15:50:29 -07002126 case Source::kWhatDrmNoLicense:
2127 {
2128 notifyListener(MEDIA_ERROR, MEDIA_ERROR_UNKNOWN, ERROR_DRM_NO_LICENSE);
2129 break;
2130 }
2131
Andreas Huber9575c962013-02-05 13:59:56 -08002132 default:
2133 TRESPASS();
2134 }
2135}
2136
Chong Zhanga7fa1d92014-06-11 14:49:23 -07002137void NuPlayer::onClosedCaptionNotify(const sp<AMessage> &msg) {
2138 int32_t what;
2139 CHECK(msg->findInt32("what", &what));
2140
2141 switch (what) {
2142 case NuPlayer::CCDecoder::kWhatClosedCaptionData:
2143 {
2144 sp<ABuffer> buffer;
2145 CHECK(msg->findBuffer("buffer", &buffer));
2146
2147 size_t inbandTracks = 0;
2148 if (mSource != NULL) {
2149 inbandTracks = mSource->getTrackCount();
2150 }
2151
2152 sendSubtitleData(buffer, inbandTracks);
2153 break;
2154 }
2155
2156 case NuPlayer::CCDecoder::kWhatTrackAdded:
2157 {
2158 notifyListener(MEDIA_INFO, MEDIA_INFO_METADATA_UPDATE, 0);
2159
2160 break;
2161 }
2162
2163 default:
2164 TRESPASS();
2165 }
2166
2167
2168}
2169
Chong Zhang404fced2014-06-11 14:45:31 -07002170void NuPlayer::sendSubtitleData(const sp<ABuffer> &buffer, int32_t baseIndex) {
2171 int32_t trackIndex;
2172 int64_t timeUs, durationUs;
2173 CHECK(buffer->meta()->findInt32("trackIndex", &trackIndex));
2174 CHECK(buffer->meta()->findInt64("timeUs", &timeUs));
2175 CHECK(buffer->meta()->findInt64("durationUs", &durationUs));
2176
2177 Parcel in;
2178 in.writeInt32(trackIndex + baseIndex);
2179 in.writeInt64(timeUs);
2180 in.writeInt64(durationUs);
2181 in.writeInt32(buffer->size());
2182 in.writeInt32(buffer->size());
2183 in.write(buffer->data(), buffer->size());
2184
2185 notifyListener(MEDIA_SUBTITLE_DATA, 0, 0, &in);
2186}
Robert Shihd3b0bbb2014-07-23 15:00:25 -07002187
Robert Shih08528432015-04-08 09:06:54 -07002188void NuPlayer::sendTimedMetaData(const sp<ABuffer> &buffer) {
2189 int64_t timeUs;
2190 CHECK(buffer->meta()->findInt64("timeUs", &timeUs));
2191
2192 Parcel in;
2193 in.writeInt64(timeUs);
2194 in.writeInt32(buffer->size());
2195 in.writeInt32(buffer->size());
2196 in.write(buffer->data(), buffer->size());
2197
2198 notifyListener(MEDIA_META_DATA, 0, 0, &in);
2199}
2200
Robert Shihd3b0bbb2014-07-23 15:00:25 -07002201void NuPlayer::sendTimedTextData(const sp<ABuffer> &buffer) {
2202 const void *data;
2203 size_t size = 0;
2204 int64_t timeUs;
2205 int32_t flag = TextDescriptions::LOCAL_DESCRIPTIONS;
2206
2207 AString mime;
2208 CHECK(buffer->meta()->findString("mime", &mime));
2209 CHECK(strcasecmp(mime.c_str(), MEDIA_MIMETYPE_TEXT_3GPP) == 0);
2210
2211 data = buffer->data();
2212 size = buffer->size();
2213
2214 Parcel parcel;
2215 if (size > 0) {
2216 CHECK(buffer->meta()->findInt64("timeUs", &timeUs));
2217 flag |= TextDescriptions::IN_BAND_TEXT_3GPP;
2218 TextDescriptions::getParcelOfDescriptions(
2219 (const uint8_t *)data, size, flag, timeUs / 1000, &parcel);
2220 }
2221
2222 if ((parcel.dataSize() > 0)) {
2223 notifyListener(MEDIA_TIMED_TEXT, 0, 0, &parcel);
2224 } else { // send an empty timed text
2225 notifyListener(MEDIA_TIMED_TEXT, 0, 0);
2226 }
2227}
Andreas Huberb5f25f02013-02-05 10:14:26 -08002228////////////////////////////////////////////////////////////////////////////////
2229
Chong Zhangced1c2f2014-08-08 15:22:35 -07002230sp<AMessage> NuPlayer::Source::getFormat(bool audio) {
2231 sp<MetaData> meta = getFormatMeta(audio);
2232
2233 if (meta == NULL) {
2234 return NULL;
2235 }
2236
2237 sp<AMessage> msg = new AMessage;
2238
2239 if(convertMetaDataToMessage(meta, &msg) == OK) {
2240 return msg;
2241 }
2242 return NULL;
2243}
2244
Andreas Huber9575c962013-02-05 13:59:56 -08002245void NuPlayer::Source::notifyFlagsChanged(uint32_t flags) {
2246 sp<AMessage> notify = dupNotify();
2247 notify->setInt32("what", kWhatFlagsChanged);
2248 notify->setInt32("flags", flags);
2249 notify->post();
2250}
2251
Chong Zhangced1c2f2014-08-08 15:22:35 -07002252void NuPlayer::Source::notifyVideoSizeChanged(const sp<AMessage> &format) {
Andreas Huber9575c962013-02-05 13:59:56 -08002253 sp<AMessage> notify = dupNotify();
2254 notify->setInt32("what", kWhatVideoSizeChanged);
Chong Zhangced1c2f2014-08-08 15:22:35 -07002255 notify->setMessage("format", format);
Andreas Huber9575c962013-02-05 13:59:56 -08002256 notify->post();
2257}
2258
Andreas Huberec0c5972013-02-05 14:47:13 -08002259void NuPlayer::Source::notifyPrepared(status_t err) {
Andreas Huber9575c962013-02-05 13:59:56 -08002260 sp<AMessage> notify = dupNotify();
2261 notify->setInt32("what", kWhatPrepared);
Andreas Huberec0c5972013-02-05 14:47:13 -08002262 notify->setInt32("err", err);
Andreas Huber9575c962013-02-05 13:59:56 -08002263 notify->post();
2264}
2265
Lajos Molnarfcd3e942015-03-31 10:06:48 -07002266void NuPlayer::Source::notifyInstantiateSecureDecoders(const sp<AMessage> &reply) {
2267 sp<AMessage> notify = dupNotify();
2268 notify->setInt32("what", kWhatInstantiateSecureDecoders);
2269 notify->setMessage("reply", reply);
2270 notify->post();
2271}
2272
Andreas Huber84333e02014-02-07 15:36:10 -08002273void NuPlayer::Source::onMessageReceived(const sp<AMessage> & /* msg */) {
Andreas Huberb5f25f02013-02-05 10:14:26 -08002274 TRESPASS();
2275}
2276
Andreas Huberf9334412010-12-15 15:17:42 -08002277} // namespace android