blob: 63b12a93db761fa6ade9353dda0c05e06d5c1975 [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;
427 CHECK(format->findString("mime", &mime));
428
Chong Zhang404fced2014-06-11 14:45:31 -0700429 AString lang;
430 CHECK(format->findString("language", &lang));
431
432 reply->writeInt32(2); // write something non-zero
433 reply->writeInt32(trackType);
Robert Shih755106e2015-04-30 14:36:45 -0700434 reply->writeString16(String16(mime.c_str()));
Chong Zhang404fced2014-06-11 14:45:31 -0700435 reply->writeString16(String16(lang.c_str()));
436
437 if (trackType == MEDIA_TRACK_TYPE_SUBTITLE) {
Chong Zhang404fced2014-06-11 14:45:31 -0700438 int32_t isAuto, isDefault, isForced;
439 CHECK(format->findInt32("auto", &isAuto));
440 CHECK(format->findInt32("default", &isDefault));
441 CHECK(format->findInt32("forced", &isForced));
442
Chong Zhang404fced2014-06-11 14:45:31 -0700443 reply->writeInt32(isAuto);
444 reply->writeInt32(isDefault);
445 reply->writeInt32(isForced);
446 }
447}
448
Andreas Huberf9334412010-12-15 15:17:42 -0800449void NuPlayer::onMessageReceived(const sp<AMessage> &msg) {
450 switch (msg->what()) {
451 case kWhatSetDataSource:
452 {
Steve Block3856b092011-10-20 11:56:00 +0100453 ALOGV("kWhatSetDataSource");
Andreas Huberf9334412010-12-15 15:17:42 -0800454
455 CHECK(mSource == NULL);
456
Chong Zhang3de157d2014-08-05 20:54:44 -0700457 status_t err = OK;
Andreas Huber5bc087c2010-12-23 10:27:40 -0800458 sp<RefBase> obj;
459 CHECK(msg->findObject("source", &obj));
Chong Zhang3de157d2014-08-05 20:54:44 -0700460 if (obj != NULL) {
461 mSource = static_cast<Source *>(obj.get());
Chong Zhang3de157d2014-08-05 20:54:44 -0700462 } else {
463 err = UNKNOWN_ERROR;
464 }
Andreas Huber9575c962013-02-05 13:59:56 -0800465
466 CHECK(mDriver != NULL);
467 sp<NuPlayerDriver> driver = mDriver.promote();
468 if (driver != NULL) {
Chong Zhang3de157d2014-08-05 20:54:44 -0700469 driver->notifySetDataSourceCompleted(err);
Andreas Huber9575c962013-02-05 13:59:56 -0800470 }
471 break;
472 }
473
474 case kWhatPrepare:
475 {
476 mSource->prepareAsync();
Andreas Huberf9334412010-12-15 15:17:42 -0800477 break;
478 }
479
Chong Zhangdcb89b32013-08-06 09:44:47 -0700480 case kWhatGetTrackInfo:
481 {
Lajos Molnar3f274362015-03-05 14:35:41 -0800482 sp<AReplyToken> replyID;
Chong Zhangdcb89b32013-08-06 09:44:47 -0700483 CHECK(msg->senderAwaitsResponse(&replyID));
484
Chong Zhang404fced2014-06-11 14:45:31 -0700485 Parcel* reply;
486 CHECK(msg->findPointer("reply", (void**)&reply));
487
488 size_t inbandTracks = 0;
Chong Zhangdcb89b32013-08-06 09:44:47 -0700489 if (mSource != NULL) {
Chong Zhang404fced2014-06-11 14:45:31 -0700490 inbandTracks = mSource->getTrackCount();
491 }
492
Chong Zhanga7fa1d92014-06-11 14:49:23 -0700493 size_t ccTracks = 0;
494 if (mCCDecoder != NULL) {
495 ccTracks = mCCDecoder->getTrackCount();
496 }
497
Chong Zhang404fced2014-06-11 14:45:31 -0700498 // total track count
Chong Zhanga7fa1d92014-06-11 14:49:23 -0700499 reply->writeInt32(inbandTracks + ccTracks);
Chong Zhang404fced2014-06-11 14:45:31 -0700500
501 // write inband tracks
502 for (size_t i = 0; i < inbandTracks; ++i) {
503 writeTrackInfo(reply, mSource->getTrackInfo(i));
Chong Zhangdcb89b32013-08-06 09:44:47 -0700504 }
505
Chong Zhanga7fa1d92014-06-11 14:49:23 -0700506 // write CC track
507 for (size_t i = 0; i < ccTracks; ++i) {
508 writeTrackInfo(reply, mCCDecoder->getTrackInfo(i));
509 }
510
Chong Zhangdcb89b32013-08-06 09:44:47 -0700511 sp<AMessage> response = new AMessage;
Chong Zhangdcb89b32013-08-06 09:44:47 -0700512 response->postReply(replyID);
513 break;
514 }
515
Robert Shih7c4f0d72014-07-09 18:53:31 -0700516 case kWhatGetSelectedTrack:
517 {
518 status_t err = INVALID_OPERATION;
519 if (mSource != NULL) {
520 err = OK;
521
522 int32_t type32;
523 CHECK(msg->findInt32("type", (int32_t*)&type32));
524 media_track_type type = (media_track_type)type32;
525 ssize_t selectedTrack = mSource->getSelectedTrack(type);
526
527 Parcel* reply;
528 CHECK(msg->findPointer("reply", (void**)&reply));
529 reply->writeInt32(selectedTrack);
530 }
531
532 sp<AMessage> response = new AMessage;
533 response->setInt32("err", err);
534
Lajos Molnar3f274362015-03-05 14:35:41 -0800535 sp<AReplyToken> replyID;
Robert Shih7c4f0d72014-07-09 18:53:31 -0700536 CHECK(msg->senderAwaitsResponse(&replyID));
537 response->postReply(replyID);
538 break;
539 }
540
Chong Zhangdcb89b32013-08-06 09:44:47 -0700541 case kWhatSelectTrack:
542 {
Lajos Molnar3f274362015-03-05 14:35:41 -0800543 sp<AReplyToken> replyID;
Chong Zhangdcb89b32013-08-06 09:44:47 -0700544 CHECK(msg->senderAwaitsResponse(&replyID));
545
Chong Zhang404fced2014-06-11 14:45:31 -0700546 size_t trackIndex;
547 int32_t select;
Robert Shih6ffb1fd2014-10-29 16:24:32 -0700548 int64_t timeUs;
Chong Zhang404fced2014-06-11 14:45:31 -0700549 CHECK(msg->findSize("trackIndex", &trackIndex));
550 CHECK(msg->findInt32("select", &select));
Robert Shih6ffb1fd2014-10-29 16:24:32 -0700551 CHECK(msg->findInt64("timeUs", &timeUs));
Chong Zhang404fced2014-06-11 14:45:31 -0700552
Chong Zhangdcb89b32013-08-06 09:44:47 -0700553 status_t err = INVALID_OPERATION;
Chong Zhang404fced2014-06-11 14:45:31 -0700554
555 size_t inbandTracks = 0;
Chong Zhangdcb89b32013-08-06 09:44:47 -0700556 if (mSource != NULL) {
Chong Zhang404fced2014-06-11 14:45:31 -0700557 inbandTracks = mSource->getTrackCount();
558 }
Chong Zhanga7fa1d92014-06-11 14:49:23 -0700559 size_t ccTracks = 0;
560 if (mCCDecoder != NULL) {
561 ccTracks = mCCDecoder->getTrackCount();
562 }
Chong Zhang404fced2014-06-11 14:45:31 -0700563
564 if (trackIndex < inbandTracks) {
Robert Shih6ffb1fd2014-10-29 16:24:32 -0700565 err = mSource->selectTrack(trackIndex, select, timeUs);
Robert Shihd3b0bbb2014-07-23 15:00:25 -0700566
567 if (!select && err == OK) {
568 int32_t type;
569 sp<AMessage> info = mSource->getTrackInfo(trackIndex);
570 if (info != NULL
571 && info->findInt32("type", &type)
572 && type == MEDIA_TRACK_TYPE_TIMEDTEXT) {
573 ++mTimedTextGeneration;
574 }
575 }
Chong Zhanga7fa1d92014-06-11 14:49:23 -0700576 } else {
577 trackIndex -= inbandTracks;
578
579 if (trackIndex < ccTracks) {
580 err = mCCDecoder->selectTrack(trackIndex, select);
581 }
Chong Zhangdcb89b32013-08-06 09:44:47 -0700582 }
583
584 sp<AMessage> response = new AMessage;
585 response->setInt32("err", err);
586
587 response->postReply(replyID);
588 break;
589 }
590
Andreas Huberb7c8e912012-11-27 15:02:53 -0800591 case kWhatPollDuration:
592 {
593 int32_t generation;
594 CHECK(msg->findInt32("generation", &generation));
595
596 if (generation != mPollDurationGeneration) {
597 // stale
598 break;
599 }
600
601 int64_t durationUs;
602 if (mDriver != NULL && mSource->getDuration(&durationUs) == OK) {
603 sp<NuPlayerDriver> driver = mDriver.promote();
604 if (driver != NULL) {
605 driver->notifyDuration(durationUs);
606 }
607 }
608
609 msg->post(1000000ll); // poll again in a second.
610 break;
611 }
612
Lajos Molnar1de1e252015-04-30 18:18:34 -0700613 case kWhatSetVideoSurface:
Andreas Huberf9334412010-12-15 15:17:42 -0800614 {
Lajos Molnar1de1e252015-04-30 18:18:34 -0700615 ALOGV("kWhatSetVideoSurface");
Andreas Huberf9334412010-12-15 15:17:42 -0800616
617 sp<RefBase> obj;
Lajos Molnar1de1e252015-04-30 18:18:34 -0700618 CHECK(msg->findObject("surface", &obj));
619 sp<Surface> surface = static_cast<Surface *>(obj.get());
Lajos Molnarf4849522014-12-10 16:58:57 -0800620 if (mSource == NULL || mSource->getFormat(false /* audio */) == NULL) {
Lajos Molnar1de1e252015-04-30 18:18:34 -0700621 performSetSurface(surface);
Wei Jiafef808d2014-10-31 17:57:05 -0700622 break;
623 }
624
625 mDeferredActions.push_back(
626 new FlushDecoderAction(FLUSH_CMD_FLUSH /* audio */,
627 FLUSH_CMD_SHUTDOWN /* video */));
628
Lajos Molnar1de1e252015-04-30 18:18:34 -0700629 mDeferredActions.push_back(new SetSurfaceAction(surface));
James Dong0d268a32012-08-31 12:18:27 -0700630
Andreas Huber57a339c2012-12-03 11:18:00 -0800631 if (obj != NULL) {
Wei Jiafef808d2014-10-31 17:57:05 -0700632 if (mStarted) {
Andy Hung73535852014-09-05 11:42:58 -0700633 // Issue a seek to refresh the video screen only if started otherwise
634 // the extractor may not yet be started and will assert.
635 // If the video decoder is not set (perhaps audio only in this case)
636 // do not perform a seek as it is not needed.
Ronghua Wua73d9e02014-10-08 15:13:29 -0700637 int64_t currentPositionUs = 0;
638 if (getCurrentPosition(&currentPositionUs) == OK) {
639 mDeferredActions.push_back(
640 new SeekAction(currentPositionUs, false /* needNotify */));
641 }
Andy Hung73535852014-09-05 11:42:58 -0700642 }
Wei Jiaac428aa2014-09-02 19:01:34 -0700643
Andreas Huber57a339c2012-12-03 11:18:00 -0800644 // If there is a new surface texture, instantiate decoders
645 // again if possible.
646 mDeferredActions.push_back(
647 new SimpleAction(&NuPlayer::performScanSources));
648 }
649
Chong Zhangf8d71772014-11-26 15:08:34 -0800650 // After a flush without shutdown, decoder is paused.
651 // Don't resume it until source seek is done, otherwise it could
Chong Zhang7137ec72014-11-12 16:41:05 -0800652 // start pulling stale data too soon.
653 mDeferredActions.push_back(
Chong Zhangf8d71772014-11-26 15:08:34 -0800654 new ResumeDecoderAction(false /* needNotify */));
Chong Zhang7137ec72014-11-12 16:41:05 -0800655
Andreas Huber57a339c2012-12-03 11:18:00 -0800656 processDeferredActions();
Andreas Huberf9334412010-12-15 15:17:42 -0800657 break;
658 }
659
660 case kWhatSetAudioSink:
661 {
Steve Block3856b092011-10-20 11:56:00 +0100662 ALOGV("kWhatSetAudioSink");
Andreas Huberf9334412010-12-15 15:17:42 -0800663
664 sp<RefBase> obj;
665 CHECK(msg->findObject("sink", &obj));
666
667 mAudioSink = static_cast<MediaPlayerBase::AudioSink *>(obj.get());
668 break;
669 }
670
671 case kWhatStart:
672 {
Steve Block3856b092011-10-20 11:56:00 +0100673 ALOGV("kWhatStart");
Wei Jia94211742014-10-28 17:09:06 -0700674 if (mStarted) {
Chong Zhang8a048332015-05-06 15:16:28 -0700675 // do not resume yet if the source is still buffering
676 if (!mPausedForBuffering) {
677 onResume();
678 }
Wei Jia94211742014-10-28 17:09:06 -0700679 } else {
680 onStart();
Lajos Molnar09524832014-07-17 14:29:51 -0700681 }
Chong Zhangefbb6192015-01-30 17:13:27 -0800682 mPausedByClient = false;
Andreas Huberf9334412010-12-15 15:17:42 -0800683 break;
684 }
685
Lajos Molnar3a474aa2015-04-24 17:10:07 -0700686 case kWhatConfigPlayback:
Wei Jia98160162015-02-04 17:01:11 -0800687 {
Lajos Molnar3a474aa2015-04-24 17:10:07 -0700688 sp<AReplyToken> replyID;
689 CHECK(msg->senderAwaitsResponse(&replyID));
690 AudioPlaybackRate rate /* sanitized */;
691 readFromAMessage(msg, &rate);
692 status_t err = OK;
Wei Jia98160162015-02-04 17:01:11 -0800693 if (mRenderer != NULL) {
Lajos Molnar3a474aa2015-04-24 17:10:07 -0700694 err = mRenderer->setPlaybackSettings(rate);
695 }
696 if (err == OK) {
697 if (rate.mSpeed == 0.f) {
698 onPause();
699 // save all other settings (using non-paused speed)
700 // so we can restore them on start
701 AudioPlaybackRate newRate = rate;
702 newRate.mSpeed = mPlaybackSettings.mSpeed;
703 mPlaybackSettings = newRate;
704 } else { /* rate.mSpeed != 0.f */
705 onResume();
706 mPlaybackSettings = rate;
707 }
Wei Jia98160162015-02-04 17:01:11 -0800708 }
Ronghua Wu8db88132015-04-22 13:51:35 -0700709
710 if (mVideoDecoder != NULL) {
711 sp<MetaData> meta = getFileMeta();
712 int32_t rate;
713 if (meta != NULL && meta->findInt32(kKeyFrameRate, &rate) && rate > 0) {
714 sp<AMessage> params = new AMessage();
Lajos Molnar3a474aa2015-04-24 17:10:07 -0700715 params->setFloat("operating-rate", rate * mPlaybackSettings.mSpeed);
Ronghua Wu8db88132015-04-22 13:51:35 -0700716 mVideoDecoder->setParameters(params);
717 }
718 }
719
Lajos Molnar3a474aa2015-04-24 17:10:07 -0700720 sp<AMessage> response = new AMessage;
721 response->setInt32("err", err);
722 response->postReply(replyID);
723 break;
724 }
725
726 case kWhatGetPlaybackSettings:
727 {
728 sp<AReplyToken> replyID;
729 CHECK(msg->senderAwaitsResponse(&replyID));
730 AudioPlaybackRate rate = mPlaybackSettings;
731 status_t err = OK;
732 if (mRenderer != NULL) {
733 err = mRenderer->getPlaybackSettings(&rate);
734 }
735 if (err == OK) {
736 // get playback settings used by renderer, as it may be
737 // slightly off due to audiosink not taking small changes.
738 mPlaybackSettings = rate;
739 if (mPaused) {
740 rate.mSpeed = 0.f;
741 }
742 }
743 sp<AMessage> response = new AMessage;
744 if (err == OK) {
745 writeToAMessage(response, rate);
746 }
747 response->setInt32("err", err);
748 response->postReply(replyID);
749 break;
750 }
751
752 case kWhatConfigSync:
753 {
754 sp<AReplyToken> replyID;
755 CHECK(msg->senderAwaitsResponse(&replyID));
756
757 ALOGV("kWhatConfigSync");
758 AVSyncSettings sync;
759 float videoFpsHint;
760 readFromAMessage(msg, &sync, &videoFpsHint);
761 status_t err = OK;
762 if (mRenderer != NULL) {
763 err = mRenderer->setSyncSettings(sync, videoFpsHint);
764 }
765 if (err == OK) {
766 mSyncSettings = sync;
767 mVideoFpsHint = videoFpsHint;
768 }
769 sp<AMessage> response = new AMessage;
770 response->setInt32("err", err);
771 response->postReply(replyID);
772 break;
773 }
774
775 case kWhatGetSyncSettings:
776 {
777 sp<AReplyToken> replyID;
778 CHECK(msg->senderAwaitsResponse(&replyID));
779 AVSyncSettings sync = mSyncSettings;
780 float videoFps = mVideoFpsHint;
781 status_t err = OK;
782 if (mRenderer != NULL) {
783 err = mRenderer->getSyncSettings(&sync, &videoFps);
784 if (err == OK) {
785 mSyncSettings = sync;
786 mVideoFpsHint = videoFps;
787 }
788 }
789 sp<AMessage> response = new AMessage;
790 if (err == OK) {
791 writeToAMessage(response, sync, videoFps);
792 }
793 response->setInt32("err", err);
794 response->postReply(replyID);
Wei Jia98160162015-02-04 17:01:11 -0800795 break;
796 }
797
Andreas Huberf9334412010-12-15 15:17:42 -0800798 case kWhatScanSources:
799 {
Andreas Huber1aef2112011-01-04 14:01:29 -0800800 int32_t generation;
801 CHECK(msg->findInt32("generation", &generation));
802 if (generation != mScanSourcesGeneration) {
803 // Drop obsolete msg.
804 break;
805 }
806
Andreas Huber5bc087c2010-12-23 10:27:40 -0800807 mScanSourcesPending = false;
808
Steve Block3856b092011-10-20 11:56:00 +0100809 ALOGV("scanning sources haveAudio=%d, haveVideo=%d",
Andreas Huber43c3e6c2011-01-05 12:17:08 -0800810 mAudioDecoder != NULL, mVideoDecoder != NULL);
811
Andreas Huberb7c8e912012-11-27 15:02:53 -0800812 bool mHadAnySourcesBefore =
813 (mAudioDecoder != NULL) || (mVideoDecoder != NULL);
814
Andy Hung282a7e32014-08-14 15:56:34 -0700815 // initialize video before audio because successful initialization of
816 // video may change deep buffer mode of audio.
Lajos Molnar1de1e252015-04-30 18:18:34 -0700817 if (mSurface != NULL) {
Haynes Mathew George5d246ef2012-07-09 10:36:57 -0700818 instantiateDecoder(false, &mVideoDecoder);
819 }
Andreas Huberf9334412010-12-15 15:17:42 -0800820
Ronghua Wua10fd232014-11-06 16:15:20 -0800821 // Don't try to re-open audio sink if there's an existing decoder.
822 if (mAudioSink != NULL && mAudioDecoder == NULL) {
823 sp<MetaData> audioMeta = mSource->getFormatMeta(true /* audio */);
824 sp<AMessage> videoFormat = mSource->getFormat(false /* audio */);
825 audio_stream_type_t streamType = mAudioSink->getAudioStreamType();
Andy Hung202bce12014-12-03 11:47:36 -0800826 const bool hasVideo = (videoFormat != NULL);
827 const bool canOffload = canOffloadStream(
828 audioMeta, hasVideo, true /* is_streaming */, streamType);
Ronghua Wua10fd232014-11-06 16:15:20 -0800829 if (canOffload) {
830 if (!mOffloadAudio) {
831 mRenderer->signalEnableOffloadAudio();
832 }
Andy Hung282a7e32014-08-14 15:56:34 -0700833 // open audio sink early under offload mode.
834 sp<AMessage> format = mSource->getFormat(true /*audio*/);
Andy Hung202bce12014-12-03 11:47:36 -0800835 tryOpenAudioSinkForOffload(format, hasVideo);
Andy Hung282a7e32014-08-14 15:56:34 -0700836 }
Andreas Huber5bc087c2010-12-23 10:27:40 -0800837 instantiateDecoder(true, &mAudioDecoder);
Andreas Huberf9334412010-12-15 15:17:42 -0800838 }
839
Andreas Huberb7c8e912012-11-27 15:02:53 -0800840 if (!mHadAnySourcesBefore
841 && (mAudioDecoder != NULL || mVideoDecoder != NULL)) {
842 // This is the first time we've found anything playable.
843
Andreas Huber9575c962013-02-05 13:59:56 -0800844 if (mSourceFlags & Source::FLAG_DYNAMIC_DURATION) {
Andreas Huberb7c8e912012-11-27 15:02:53 -0800845 schedulePollDuration();
846 }
847 }
848
Andreas Hubereac68ba2011-09-27 12:12:25 -0700849 status_t err;
850 if ((err = mSource->feedMoreTSData()) != OK) {
Andreas Huber1aef2112011-01-04 14:01:29 -0800851 if (mAudioDecoder == NULL && mVideoDecoder == NULL) {
852 // We're not currently decoding anything (no audio or
853 // video tracks found) and we just ran out of input data.
Andreas Hubereac68ba2011-09-27 12:12:25 -0700854
855 if (err == ERROR_END_OF_STREAM) {
856 notifyListener(MEDIA_PLAYBACK_COMPLETE, 0, 0);
857 } else {
858 notifyListener(MEDIA_ERROR, MEDIA_ERROR_UNKNOWN, err);
859 }
Andreas Huber1aef2112011-01-04 14:01:29 -0800860 }
Andreas Huberf9334412010-12-15 15:17:42 -0800861 break;
862 }
863
Andreas Huberfbe9d812012-08-31 14:05:27 -0700864 if ((mAudioDecoder == NULL && mAudioSink != NULL)
Lajos Molnar1de1e252015-04-30 18:18:34 -0700865 || (mVideoDecoder == NULL && mSurface != NULL)) {
Andreas Huberf9334412010-12-15 15:17:42 -0800866 msg->post(100000ll);
Andreas Huber5bc087c2010-12-23 10:27:40 -0800867 mScanSourcesPending = true;
Andreas Huberf9334412010-12-15 15:17:42 -0800868 }
869 break;
870 }
871
872 case kWhatVideoNotify:
873 case kWhatAudioNotify:
874 {
875 bool audio = msg->what() == kWhatAudioNotify;
876
Wei Jia88703c32014-08-06 11:24:07 -0700877 int32_t currentDecoderGeneration =
878 (audio? mAudioDecoderGeneration : mVideoDecoderGeneration);
879 int32_t requesterGeneration = currentDecoderGeneration - 1;
880 CHECK(msg->findInt32("generation", &requesterGeneration));
881
882 if (requesterGeneration != currentDecoderGeneration) {
883 ALOGV("got message from old %s decoder, generation(%d:%d)",
884 audio ? "audio" : "video", requesterGeneration,
885 currentDecoderGeneration);
886 sp<AMessage> reply;
887 if (!(msg->findMessage("reply", &reply))) {
888 return;
889 }
890
891 reply->setInt32("err", INFO_DISCONTINUITY);
892 reply->post();
893 return;
894 }
895
Andreas Huberf9334412010-12-15 15:17:42 -0800896 int32_t what;
Lajos Molnar1cd13982014-01-17 15:12:51 -0800897 CHECK(msg->findInt32("what", &what));
Andreas Huberf9334412010-12-15 15:17:42 -0800898
Chong Zhang7137ec72014-11-12 16:41:05 -0800899 if (what == DecoderBase::kWhatInputDiscontinuity) {
900 int32_t formatChange;
901 CHECK(msg->findInt32("formatChange", &formatChange));
Andreas Huberf9334412010-12-15 15:17:42 -0800902
Chong Zhang7137ec72014-11-12 16:41:05 -0800903 ALOGV("%s discontinuity: formatChange %d",
904 audio ? "audio" : "video", formatChange);
905
906 if (formatChange) {
907 mDeferredActions.push_back(
908 new FlushDecoderAction(
909 audio ? FLUSH_CMD_SHUTDOWN : FLUSH_CMD_NONE,
910 audio ? FLUSH_CMD_NONE : FLUSH_CMD_SHUTDOWN));
Andreas Huberf9334412010-12-15 15:17:42 -0800911 }
Chong Zhang7137ec72014-11-12 16:41:05 -0800912
913 mDeferredActions.push_back(
914 new SimpleAction(
915 &NuPlayer::performScanSources));
916
917 processDeferredActions();
918 } else if (what == DecoderBase::kWhatEOS) {
Andreas Huberdc9bacd2011-09-26 10:53:29 -0700919 int32_t err;
Lajos Molnar1cd13982014-01-17 15:12:51 -0800920 CHECK(msg->findInt32("err", &err));
Andreas Huberdc9bacd2011-09-26 10:53:29 -0700921
922 if (err == ERROR_END_OF_STREAM) {
Steve Block3856b092011-10-20 11:56:00 +0100923 ALOGV("got %s decoder EOS", audio ? "audio" : "video");
Andreas Huberdc9bacd2011-09-26 10:53:29 -0700924 } else {
Steve Block3856b092011-10-20 11:56:00 +0100925 ALOGV("got %s decoder EOS w/ error %d",
Andreas Huberdc9bacd2011-09-26 10:53:29 -0700926 audio ? "audio" : "video",
927 err);
928 }
929
930 mRenderer->queueEOS(audio, err);
Chong Zhang7137ec72014-11-12 16:41:05 -0800931 } else if (what == DecoderBase::kWhatFlushCompleted) {
Steve Block3856b092011-10-20 11:56:00 +0100932 ALOGV("decoder %s flush completed", audio ? "audio" : "video");
Andreas Huberf9334412010-12-15 15:17:42 -0800933
Andy Hung8d121d42014-10-03 09:53:53 -0700934 handleFlushComplete(audio, true /* isDecoder */);
Andreas Huber3831a062010-12-21 10:22:33 -0800935 finishFlushIfPossible();
Chong Zhang7137ec72014-11-12 16:41:05 -0800936 } else if (what == DecoderBase::kWhatVideoSizeChanged) {
Lajos Molnar1cd13982014-01-17 15:12:51 -0800937 sp<AMessage> format;
938 CHECK(msg->findMessage("format", &format));
939
Wei Jiac6cfd702014-11-11 16:33:20 -0800940 sp<AMessage> inputFormat =
941 mSource->getFormat(false /* audio */);
Andreas Huber3831a062010-12-21 10:22:33 -0800942
Wei Jiac6cfd702014-11-11 16:33:20 -0800943 updateVideoSize(inputFormat, format);
Chong Zhang7137ec72014-11-12 16:41:05 -0800944 } else if (what == DecoderBase::kWhatShutdownCompleted) {
Steve Block3856b092011-10-20 11:56:00 +0100945 ALOGV("%s shutdown completed", audio ? "audio" : "video");
Andreas Huber3831a062010-12-21 10:22:33 -0800946 if (audio) {
947 mAudioDecoder.clear();
Wei Jia57568df2014-09-22 10:16:29 -0700948 ++mAudioDecoderGeneration;
Andreas Huber3831a062010-12-21 10:22:33 -0800949
950 CHECK_EQ((int)mFlushingAudio, (int)SHUTTING_DOWN_DECODER);
951 mFlushingAudio = SHUT_DOWN;
952 } else {
953 mVideoDecoder.clear();
Wei Jia57568df2014-09-22 10:16:29 -0700954 ++mVideoDecoderGeneration;
Andreas Huber3831a062010-12-21 10:22:33 -0800955
956 CHECK_EQ((int)mFlushingVideo, (int)SHUTTING_DOWN_DECODER);
957 mFlushingVideo = SHUT_DOWN;
958 }
959
960 finishFlushIfPossible();
Chong Zhangf8d71772014-11-26 15:08:34 -0800961 } else if (what == DecoderBase::kWhatResumeCompleted) {
962 finishResume();
Chong Zhang7137ec72014-11-12 16:41:05 -0800963 } else if (what == DecoderBase::kWhatError) {
Chong Zhangf4c0a942014-08-11 15:14:10 -0700964 status_t err;
Andy Hung2abde2c2014-09-30 14:40:32 -0700965 if (!msg->findInt32("err", &err) || err == OK) {
Chong Zhangf4c0a942014-08-11 15:14:10 -0700966 err = UNKNOWN_ERROR;
967 }
Andy Hungcf31f1e2014-09-23 14:59:01 -0700968
Andy Hung2abde2c2014-09-30 14:40:32 -0700969 // Decoder errors can be due to Source (e.g. from streaming),
970 // or from decoding corrupted bitstreams, or from other decoder
971 // MediaCodec operations (e.g. from an ongoing reset or seek).
Andy Hung202bce12014-12-03 11:47:36 -0800972 // They may also be due to openAudioSink failure at
973 // decoder start or after a format change.
Andy Hung2abde2c2014-09-30 14:40:32 -0700974 //
975 // We try to gracefully shut down the affected decoder if possible,
976 // rather than trying to force the shutdown with something
977 // similar to performReset(). This method can lead to a hang
978 // if MediaCodec functions block after an error, but they should
979 // typically return INVALID_OPERATION instead of blocking.
980
981 FlushStatus *flushing = audio ? &mFlushingAudio : &mFlushingVideo;
982 ALOGE("received error(%#x) from %s decoder, flushing(%d), now shutting down",
983 err, audio ? "audio" : "video", *flushing);
984
985 switch (*flushing) {
986 case NONE:
987 mDeferredActions.push_back(
Wei Jiafef808d2014-10-31 17:57:05 -0700988 new FlushDecoderAction(
989 audio ? FLUSH_CMD_SHUTDOWN : FLUSH_CMD_NONE,
990 audio ? FLUSH_CMD_NONE : FLUSH_CMD_SHUTDOWN));
Andy Hung2abde2c2014-09-30 14:40:32 -0700991 processDeferredActions();
992 break;
993 case FLUSHING_DECODER:
994 *flushing = FLUSHING_DECODER_SHUTDOWN; // initiate shutdown after flush.
995 break; // Wait for flush to complete.
996 case FLUSHING_DECODER_SHUTDOWN:
997 break; // Wait for flush to complete.
998 case SHUTTING_DOWN_DECODER:
999 break; // Wait for shutdown to complete.
1000 case FLUSHED:
1001 // Widevine source reads must stop before releasing the video decoder.
1002 if (!audio && mSource != NULL && mSourceFlags & Source::FLAG_SECURE) {
1003 mSource->stop();
1004 }
1005 getDecoder(audio)->initiateShutdown(); // In the middle of a seek.
1006 *flushing = SHUTTING_DOWN_DECODER; // Shut down.
1007 break;
1008 case SHUT_DOWN:
1009 finishFlushIfPossible(); // Should not occur.
1010 break; // Finish anyways.
Marco Nelissen9e2b7912014-08-18 16:13:03 -07001011 }
Andy Hung2abde2c2014-09-30 14:40:32 -07001012 notifyListener(MEDIA_ERROR, MEDIA_ERROR_UNKNOWN, err);
Lajos Molnar1cd13982014-01-17 15:12:51 -08001013 } else {
1014 ALOGV("Unhandled decoder notification %d '%c%c%c%c'.",
Andreas Hubera1f8ab02012-11-30 10:53:22 -08001015 what,
1016 what >> 24,
1017 (what >> 16) & 0xff,
1018 (what >> 8) & 0xff,
1019 what & 0xff);
Andreas Huberf9334412010-12-15 15:17:42 -08001020 }
1021
1022 break;
1023 }
1024
1025 case kWhatRendererNotify:
1026 {
Wei Jia57568df2014-09-22 10:16:29 -07001027 int32_t requesterGeneration = mRendererGeneration - 1;
1028 CHECK(msg->findInt32("generation", &requesterGeneration));
1029 if (requesterGeneration != mRendererGeneration) {
1030 ALOGV("got message from old renderer, generation(%d:%d)",
1031 requesterGeneration, mRendererGeneration);
1032 return;
1033 }
1034
Andreas Huberf9334412010-12-15 15:17:42 -08001035 int32_t what;
1036 CHECK(msg->findInt32("what", &what));
1037
1038 if (what == Renderer::kWhatEOS) {
1039 int32_t audio;
1040 CHECK(msg->findInt32("audio", &audio));
1041
Andreas Huberc92fd242011-08-16 13:48:44 -07001042 int32_t finalResult;
1043 CHECK(msg->findInt32("finalResult", &finalResult));
1044
Andreas Huberf9334412010-12-15 15:17:42 -08001045 if (audio) {
1046 mAudioEOS = true;
1047 } else {
1048 mVideoEOS = true;
1049 }
1050
Andreas Huberc92fd242011-08-16 13:48:44 -07001051 if (finalResult == ERROR_END_OF_STREAM) {
Steve Block3856b092011-10-20 11:56:00 +01001052 ALOGV("reached %s EOS", audio ? "audio" : "video");
Andreas Huberc92fd242011-08-16 13:48:44 -07001053 } else {
Steve Block29357bc2012-01-06 19:20:56 +00001054 ALOGE("%s track encountered an error (%d)",
Andreas Huberc92fd242011-08-16 13:48:44 -07001055 audio ? "audio" : "video", finalResult);
1056
1057 notifyListener(
1058 MEDIA_ERROR, MEDIA_ERROR_UNKNOWN, finalResult);
1059 }
Andreas Huberf9334412010-12-15 15:17:42 -08001060
1061 if ((mAudioEOS || mAudioDecoder == NULL)
1062 && (mVideoEOS || mVideoDecoder == NULL)) {
1063 notifyListener(MEDIA_PLAYBACK_COMPLETE, 0, 0);
1064 }
Andreas Huber3fe62152011-09-16 15:09:22 -07001065 } else if (what == Renderer::kWhatFlushComplete) {
Andreas Huberf9334412010-12-15 15:17:42 -08001066 int32_t audio;
1067 CHECK(msg->findInt32("audio", &audio));
1068
Steve Block3856b092011-10-20 11:56:00 +01001069 ALOGV("renderer %s flush completed.", audio ? "audio" : "video");
Andy Hung8d121d42014-10-03 09:53:53 -07001070 handleFlushComplete(audio, false /* isDecoder */);
1071 finishFlushIfPossible();
James Dongf57b4ea2012-07-20 13:38:36 -07001072 } else if (what == Renderer::kWhatVideoRenderingStart) {
1073 notifyListener(MEDIA_INFO, MEDIA_INFO_RENDERING_START, 0);
Lajos Molnarcbaffcf2013-08-14 18:30:38 -07001074 } else if (what == Renderer::kWhatMediaRenderingStart) {
1075 ALOGV("media rendering started");
1076 notifyListener(MEDIA_STARTED, 0, 0);
Wei Jia3a2956d2014-07-22 16:01:33 -07001077 } else if (what == Renderer::kWhatAudioOffloadTearDown) {
Ronghua Wua10fd232014-11-06 16:15:20 -08001078 ALOGV("Tear down audio offload, fall back to s/w path if due to error.");
Wei Jia3a2956d2014-07-22 16:01:33 -07001079 int64_t positionUs;
1080 CHECK(msg->findInt64("positionUs", &positionUs));
Ronghua Wu08529172014-10-02 16:55:52 -07001081 int32_t reason;
1082 CHECK(msg->findInt32("reason", &reason));
Andy Hung282a7e32014-08-14 15:56:34 -07001083 closeAudioSink();
Wei Jia3a2956d2014-07-22 16:01:33 -07001084 mAudioDecoder.clear();
Wei Jia57568df2014-09-22 10:16:29 -07001085 ++mAudioDecoderGeneration;
Chong Zhang7137ec72014-11-12 16:41:05 -08001086 mRenderer->flush(
1087 true /* audio */, false /* notifyComplete */);
Wei Jia3a2956d2014-07-22 16:01:33 -07001088 if (mVideoDecoder != NULL) {
Chong Zhang7137ec72014-11-12 16:41:05 -08001089 mRenderer->flush(
1090 false /* audio */, false /* notifyComplete */);
Wei Jia3a2956d2014-07-22 16:01:33 -07001091 }
Wei Jia3a2956d2014-07-22 16:01:33 -07001092
Wei Jiae427abf2014-09-22 15:21:11 -07001093 performSeek(positionUs, false /* needNotify */);
Ronghua Wu08529172014-10-02 16:55:52 -07001094 if (reason == Renderer::kDueToError) {
Ronghua Wua10fd232014-11-06 16:15:20 -08001095 mRenderer->signalDisableOffloadAudio();
1096 mOffloadAudio = false;
Ronghua Wu08529172014-10-02 16:55:52 -07001097 instantiateDecoder(true /* audio */, &mAudioDecoder);
1098 }
Andreas Huberf9334412010-12-15 15:17:42 -08001099 }
1100 break;
1101 }
1102
1103 case kWhatMoreDataQueued:
1104 {
1105 break;
1106 }
1107
Andreas Huber1aef2112011-01-04 14:01:29 -08001108 case kWhatReset:
1109 {
Steve Block3856b092011-10-20 11:56:00 +01001110 ALOGV("kWhatReset");
Andreas Huber1aef2112011-01-04 14:01:29 -08001111
Andreas Hubera1f8ab02012-11-30 10:53:22 -08001112 mDeferredActions.push_back(
Wei Jiafef808d2014-10-31 17:57:05 -07001113 new FlushDecoderAction(
1114 FLUSH_CMD_SHUTDOWN /* audio */,
1115 FLUSH_CMD_SHUTDOWN /* video */));
Andreas Huberb7c8e912012-11-27 15:02:53 -08001116
Andreas Hubera1f8ab02012-11-30 10:53:22 -08001117 mDeferredActions.push_back(
1118 new SimpleAction(&NuPlayer::performReset));
Andreas Huberb58ce9f2011-11-28 16:27:35 -08001119
Andreas Hubera1f8ab02012-11-30 10:53:22 -08001120 processDeferredActions();
Andreas Huber1aef2112011-01-04 14:01:29 -08001121 break;
1122 }
1123
Andreas Huber43c3e6c2011-01-05 12:17:08 -08001124 case kWhatSeek:
1125 {
1126 int64_t seekTimeUs;
Wei Jiae427abf2014-09-22 15:21:11 -07001127 int32_t needNotify;
Andreas Huber43c3e6c2011-01-05 12:17:08 -08001128 CHECK(msg->findInt64("seekTimeUs", &seekTimeUs));
Wei Jiae427abf2014-09-22 15:21:11 -07001129 CHECK(msg->findInt32("needNotify", &needNotify));
Andreas Huber43c3e6c2011-01-05 12:17:08 -08001130
Wei Jiae427abf2014-09-22 15:21:11 -07001131 ALOGV("kWhatSeek seekTimeUs=%lld us, needNotify=%d",
Lajos Molnar6d339f12015-04-17 16:15:53 -07001132 (long long)seekTimeUs, needNotify);
Andreas Huber43c3e6c2011-01-05 12:17:08 -08001133
Andreas Hubera1f8ab02012-11-30 10:53:22 -08001134 mDeferredActions.push_back(
Wei Jiafef808d2014-10-31 17:57:05 -07001135 new FlushDecoderAction(FLUSH_CMD_FLUSH /* audio */,
1136 FLUSH_CMD_FLUSH /* video */));
Andreas Huber43c3e6c2011-01-05 12:17:08 -08001137
Wei Jiae427abf2014-09-22 15:21:11 -07001138 mDeferredActions.push_back(
1139 new SeekAction(seekTimeUs, needNotify));
Andreas Huber43c3e6c2011-01-05 12:17:08 -08001140
Chong Zhangf8d71772014-11-26 15:08:34 -08001141 // After a flush without shutdown, decoder is paused.
1142 // Don't resume it until source seek is done, otherwise it could
Chong Zhang7137ec72014-11-12 16:41:05 -08001143 // start pulling stale data too soon.
1144 mDeferredActions.push_back(
Chong Zhangf8d71772014-11-26 15:08:34 -08001145 new ResumeDecoderAction(needNotify));
Chong Zhang7137ec72014-11-12 16:41:05 -08001146
Andreas Hubera1f8ab02012-11-30 10:53:22 -08001147 processDeferredActions();
Andreas Huber43c3e6c2011-01-05 12:17:08 -08001148 break;
1149 }
1150
Andreas Huberb4082222011-01-20 15:23:04 -08001151 case kWhatPause:
1152 {
Chong Zhangefbb6192015-01-30 17:13:27 -08001153 onPause();
1154 mPausedByClient = true;
Andreas Huberb4082222011-01-20 15:23:04 -08001155 break;
1156 }
1157
Andreas Huberb5f25f02013-02-05 10:14:26 -08001158 case kWhatSourceNotify:
1159 {
Andreas Huber9575c962013-02-05 13:59:56 -08001160 onSourceNotify(msg);
Andreas Huberb5f25f02013-02-05 10:14:26 -08001161 break;
1162 }
1163
Chong Zhanga7fa1d92014-06-11 14:49:23 -07001164 case kWhatClosedCaptionNotify:
1165 {
1166 onClosedCaptionNotify(msg);
1167 break;
1168 }
1169
Andreas Huberf9334412010-12-15 15:17:42 -08001170 default:
1171 TRESPASS();
1172 break;
1173 }
1174}
1175
Wei Jia94211742014-10-28 17:09:06 -07001176void NuPlayer::onResume() {
Chong Zhangefbb6192015-01-30 17:13:27 -08001177 if (!mPaused) {
1178 return;
1179 }
1180 mPaused = false;
Wei Jia94211742014-10-28 17:09:06 -07001181 if (mSource != NULL) {
1182 mSource->resume();
1183 } else {
1184 ALOGW("resume called when source is gone or not set");
1185 }
1186 // |mAudioDecoder| may have been released due to the pause timeout, so re-create it if
1187 // needed.
1188 if (audioDecoderStillNeeded() && mAudioDecoder == NULL) {
1189 instantiateDecoder(true /* audio */, &mAudioDecoder);
1190 }
1191 if (mRenderer != NULL) {
1192 mRenderer->resume();
1193 } else {
1194 ALOGW("resume called when renderer is gone or not set");
1195 }
1196}
1197
Lajos Molnarfcd3e942015-03-31 10:06:48 -07001198status_t NuPlayer::onInstantiateSecureDecoders() {
1199 status_t err;
1200 if (!(mSourceFlags & Source::FLAG_SECURE)) {
1201 return BAD_TYPE;
1202 }
1203
1204 if (mRenderer != NULL) {
1205 ALOGE("renderer should not be set when instantiating secure decoders");
1206 return UNKNOWN_ERROR;
1207 }
1208
1209 // TRICKY: We rely on mRenderer being null, so that decoder does not start requesting
1210 // data on instantiation.
Lajos Molnar1de1e252015-04-30 18:18:34 -07001211 if (mSurface != NULL) {
Lajos Molnarfcd3e942015-03-31 10:06:48 -07001212 err = instantiateDecoder(false, &mVideoDecoder);
1213 if (err != OK) {
1214 return err;
1215 }
1216 }
1217
1218 if (mAudioSink != NULL) {
1219 err = instantiateDecoder(true, &mAudioDecoder);
1220 if (err != OK) {
1221 return err;
1222 }
1223 }
1224 return OK;
1225}
1226
Wei Jia94211742014-10-28 17:09:06 -07001227void NuPlayer::onStart() {
Wei Jia94211742014-10-28 17:09:06 -07001228 mOffloadAudio = false;
1229 mAudioEOS = false;
1230 mVideoEOS = false;
Wei Jia94211742014-10-28 17:09:06 -07001231 mStarted = true;
1232
Wei Jia94211742014-10-28 17:09:06 -07001233 mSource->start();
1234
1235 uint32_t flags = 0;
1236
1237 if (mSource->isRealTime()) {
1238 flags |= Renderer::FLAG_REAL_TIME;
1239 }
1240
1241 sp<MetaData> audioMeta = mSource->getFormatMeta(true /* audio */);
1242 audio_stream_type_t streamType = AUDIO_STREAM_MUSIC;
1243 if (mAudioSink != NULL) {
1244 streamType = mAudioSink->getAudioStreamType();
1245 }
1246
1247 sp<AMessage> videoFormat = mSource->getFormat(false /* audio */);
1248
1249 mOffloadAudio =
1250 canOffloadStream(audioMeta, (videoFormat != NULL),
1251 true /* is_streaming */, streamType);
1252 if (mOffloadAudio) {
1253 flags |= Renderer::FLAG_OFFLOAD_AUDIO;
1254 }
1255
Lajos Molnar1d15ab52015-03-04 16:46:34 -08001256 sp<AMessage> notify = new AMessage(kWhatRendererNotify, this);
Wei Jia94211742014-10-28 17:09:06 -07001257 ++mRendererGeneration;
1258 notify->setInt32("generation", mRendererGeneration);
1259 mRenderer = new Renderer(mAudioSink, notify, flags);
Wei Jia94211742014-10-28 17:09:06 -07001260 mRendererLooper = new ALooper;
1261 mRendererLooper->setName("NuPlayerRenderer");
1262 mRendererLooper->start(false, false, ANDROID_PRIORITY_AUDIO);
1263 mRendererLooper->registerHandler(mRenderer);
Lajos Molnar3a474aa2015-04-24 17:10:07 -07001264
1265 status_t err = mRenderer->setPlaybackSettings(mPlaybackSettings);
1266 if (err != OK) {
1267 mSource->stop();
1268 notifyListener(MEDIA_ERROR, MEDIA_ERROR_UNKNOWN, err);
1269 return;
Wei Jiac8206ff2015-03-04 13:59:37 -08001270 }
Wei Jia94211742014-10-28 17:09:06 -07001271
1272 sp<MetaData> meta = getFileMeta();
1273 int32_t rate;
1274 if (meta != NULL
1275 && meta->findInt32(kKeyFrameRate, &rate) && rate > 0) {
1276 mRenderer->setVideoFrameRate(rate);
1277 }
1278
Wei Jiac6cfd702014-11-11 16:33:20 -08001279 if (mVideoDecoder != NULL) {
1280 mVideoDecoder->setRenderer(mRenderer);
1281 }
1282 if (mAudioDecoder != NULL) {
1283 mAudioDecoder->setRenderer(mRenderer);
1284 }
1285
Wei Jia94211742014-10-28 17:09:06 -07001286 postScanSources();
1287}
1288
Chong Zhangefbb6192015-01-30 17:13:27 -08001289void NuPlayer::onPause() {
1290 if (mPaused) {
1291 return;
1292 }
1293 mPaused = true;
1294 if (mSource != NULL) {
1295 mSource->pause();
1296 } else {
1297 ALOGW("pause called when source is gone or not set");
1298 }
1299 if (mRenderer != NULL) {
1300 mRenderer->pause();
1301 } else {
1302 ALOGW("pause called when renderer is gone or not set");
1303 }
1304}
1305
Ronghua Wud7988b12014-10-03 15:19:10 -07001306bool NuPlayer::audioDecoderStillNeeded() {
1307 // Audio decoder is no longer needed if it's in shut/shutting down status.
1308 return ((mFlushingAudio != SHUT_DOWN) && (mFlushingAudio != SHUTTING_DOWN_DECODER));
1309}
1310
Andy Hung8d121d42014-10-03 09:53:53 -07001311void NuPlayer::handleFlushComplete(bool audio, bool isDecoder) {
1312 // We wait for both the decoder flush and the renderer flush to complete
1313 // before entering either the FLUSHED or the SHUTTING_DOWN_DECODER state.
1314
1315 mFlushComplete[audio][isDecoder] = true;
1316 if (!mFlushComplete[audio][!isDecoder]) {
1317 return;
1318 }
1319
1320 FlushStatus *state = audio ? &mFlushingAudio : &mFlushingVideo;
1321 switch (*state) {
1322 case FLUSHING_DECODER:
1323 {
1324 *state = FLUSHED;
Andy Hung8d121d42014-10-03 09:53:53 -07001325 break;
1326 }
1327
1328 case FLUSHING_DECODER_SHUTDOWN:
1329 {
1330 *state = SHUTTING_DOWN_DECODER;
1331
1332 ALOGV("initiating %s decoder shutdown", audio ? "audio" : "video");
1333 if (!audio) {
Andy Hung8d121d42014-10-03 09:53:53 -07001334 // Widevine source reads must stop before releasing the video decoder.
1335 if (mSource != NULL && mSourceFlags & Source::FLAG_SECURE) {
1336 mSource->stop();
1337 }
1338 }
1339 getDecoder(audio)->initiateShutdown();
1340 break;
1341 }
1342
1343 default:
1344 // decoder flush completes only occur in a flushing state.
1345 LOG_ALWAYS_FATAL_IF(isDecoder, "decoder flush in invalid state %d", *state);
1346 break;
1347 }
1348}
1349
Andreas Huber3831a062010-12-21 10:22:33 -08001350void NuPlayer::finishFlushIfPossible() {
Wei Jia53904f32014-07-29 10:22:53 -07001351 if (mFlushingAudio != NONE && mFlushingAudio != FLUSHED
1352 && mFlushingAudio != SHUT_DOWN) {
Andreas Huber3831a062010-12-21 10:22:33 -08001353 return;
1354 }
1355
Wei Jia53904f32014-07-29 10:22:53 -07001356 if (mFlushingVideo != NONE && mFlushingVideo != FLUSHED
1357 && mFlushingVideo != SHUT_DOWN) {
Andreas Huber3831a062010-12-21 10:22:33 -08001358 return;
1359 }
1360
Steve Block3856b092011-10-20 11:56:00 +01001361 ALOGV("both audio and video are flushed now.");
Andreas Huber3831a062010-12-21 10:22:33 -08001362
Andreas Huber3831a062010-12-21 10:22:33 -08001363 mFlushingAudio = NONE;
1364 mFlushingVideo = NONE;
Andreas Huber3831a062010-12-21 10:22:33 -08001365
Andy Hung8d121d42014-10-03 09:53:53 -07001366 clearFlushComplete();
1367
Andreas Hubera1f8ab02012-11-30 10:53:22 -08001368 processDeferredActions();
Andreas Huber1aef2112011-01-04 14:01:29 -08001369}
1370
1371void NuPlayer::postScanSources() {
1372 if (mScanSourcesPending) {
1373 return;
1374 }
1375
Lajos Molnar1d15ab52015-03-04 16:46:34 -08001376 sp<AMessage> msg = new AMessage(kWhatScanSources, this);
Andreas Huber1aef2112011-01-04 14:01:29 -08001377 msg->setInt32("generation", mScanSourcesGeneration);
1378 msg->post();
1379
1380 mScanSourcesPending = true;
1381}
1382
Andy Hung202bce12014-12-03 11:47:36 -08001383void NuPlayer::tryOpenAudioSinkForOffload(const sp<AMessage> &format, bool hasVideo) {
1384 // Note: This is called early in NuPlayer to determine whether offloading
1385 // is possible; otherwise the decoders call the renderer openAudioSink directly.
Andy Hung282a7e32014-08-14 15:56:34 -07001386
Andy Hung202bce12014-12-03 11:47:36 -08001387 status_t err = mRenderer->openAudioSink(
1388 format, true /* offloadOnly */, hasVideo, AUDIO_OUTPUT_FLAG_NONE, &mOffloadAudio);
1389 if (err != OK) {
1390 // Any failure we turn off mOffloadAudio.
1391 mOffloadAudio = false;
1392 } else if (mOffloadAudio) {
Chong Zhang3b9eb1f2014-10-15 17:05:08 -07001393 sp<MetaData> audioMeta =
1394 mSource->getFormatMeta(true /* audio */);
1395 sendMetaDataToHal(mAudioSink, audioMeta);
Andy Hung282a7e32014-08-14 15:56:34 -07001396 }
1397}
1398
1399void NuPlayer::closeAudioSink() {
Chong Zhang3b9eb1f2014-10-15 17:05:08 -07001400 mRenderer->closeAudioSink();
Andy Hung282a7e32014-08-14 15:56:34 -07001401}
1402
Chong Zhang7137ec72014-11-12 16:41:05 -08001403status_t NuPlayer::instantiateDecoder(bool audio, sp<DecoderBase> *decoder) {
Andreas Huberf9334412010-12-15 15:17:42 -08001404 if (*decoder != NULL) {
1405 return OK;
1406 }
1407
Andreas Huber84066782011-08-16 09:34:26 -07001408 sp<AMessage> format = mSource->getFormat(audio);
Andreas Huberf9334412010-12-15 15:17:42 -08001409
Andreas Huber84066782011-08-16 09:34:26 -07001410 if (format == NULL) {
Andreas Huberf9334412010-12-15 15:17:42 -08001411 return -EWOULDBLOCK;
1412 }
1413
Ronghua Wu8db88132015-04-22 13:51:35 -07001414 format->setInt32("priority", 0 /* realtime */);
1415
Andreas Huber3fe62152011-09-16 15:09:22 -07001416 if (!audio) {
Andreas Huber84066782011-08-16 09:34:26 -07001417 AString mime;
1418 CHECK(format->findString("mime", &mime));
Chong Zhanga7fa1d92014-06-11 14:49:23 -07001419
Lajos Molnar1d15ab52015-03-04 16:46:34 -08001420 sp<AMessage> ccNotify = new AMessage(kWhatClosedCaptionNotify, this);
Chong Zhang341ab6e2015-02-04 13:37:18 -08001421 if (mCCDecoder == NULL) {
1422 mCCDecoder = new CCDecoder(ccNotify);
1423 }
Lajos Molnar09524832014-07-17 14:29:51 -07001424
1425 if (mSourceFlags & Source::FLAG_SECURE) {
1426 format->setInt32("secure", true);
1427 }
Chong Zhang17134602015-01-07 16:14:34 -08001428
1429 if (mSourceFlags & Source::FLAG_PROTECTED) {
1430 format->setInt32("protected", true);
1431 }
Ronghua Wu8db88132015-04-22 13:51:35 -07001432
1433 sp<MetaData> meta = getFileMeta();
1434 int32_t rate;
1435 if (meta != NULL && meta->findInt32(kKeyFrameRate, &rate) && rate > 0) {
Lajos Molnar3a474aa2015-04-24 17:10:07 -07001436 format->setFloat("operating-rate", rate * mPlaybackSettings.mSpeed);
Ronghua Wu8db88132015-04-22 13:51:35 -07001437 }
Andreas Huber3fe62152011-09-16 15:09:22 -07001438 }
1439
Wei Jiabc2fb722014-07-08 16:37:57 -07001440 if (audio) {
Lajos Molnar1d15ab52015-03-04 16:46:34 -08001441 sp<AMessage> notify = new AMessage(kWhatAudioNotify, this);
Wei Jia88703c32014-08-06 11:24:07 -07001442 ++mAudioDecoderGeneration;
1443 notify->setInt32("generation", mAudioDecoderGeneration);
1444
Wei Jiabc2fb722014-07-08 16:37:57 -07001445 if (mOffloadAudio) {
Haynes Mathew George8b635332015-03-30 17:59:47 -07001446 const bool hasVideo = (mSource->getFormat(false /*audio */) != NULL);
1447 format->setInt32("has-video", hasVideo);
Wei Jiac6cfd702014-11-11 16:33:20 -08001448 *decoder = new DecoderPassThrough(notify, mSource, mRenderer);
Wei Jiabc2fb722014-07-08 16:37:57 -07001449 } else {
Wei Jiac6cfd702014-11-11 16:33:20 -08001450 *decoder = new Decoder(notify, mSource, mRenderer);
Wei Jiabc2fb722014-07-08 16:37:57 -07001451 }
1452 } else {
Lajos Molnar1d15ab52015-03-04 16:46:34 -08001453 sp<AMessage> notify = new AMessage(kWhatVideoNotify, this);
Wei Jia88703c32014-08-06 11:24:07 -07001454 ++mVideoDecoderGeneration;
1455 notify->setInt32("generation", mVideoDecoderGeneration);
1456
Chong Zhang7137ec72014-11-12 16:41:05 -08001457 *decoder = new Decoder(
Lajos Molnar1de1e252015-04-30 18:18:34 -07001458 notify, mSource, mRenderer, mSurface, mCCDecoder);
Lajos Molnard9fd6312014-11-06 11:00:00 -08001459
1460 // enable FRC if high-quality AV sync is requested, even if not
Lajos Molnar1de1e252015-04-30 18:18:34 -07001461 // directly queuing to display, as this will even improve textureview
Lajos Molnard9fd6312014-11-06 11:00:00 -08001462 // playback.
1463 {
1464 char value[PROPERTY_VALUE_MAX];
1465 if (property_get("persist.sys.media.avsync", value, NULL) &&
1466 (!strcmp("1", value) || !strcasecmp("true", value))) {
1467 format->setInt32("auto-frc", 1);
1468 }
1469 }
Wei Jiabc2fb722014-07-08 16:37:57 -07001470 }
Lajos Molnar1cd13982014-01-17 15:12:51 -08001471 (*decoder)->init();
Andreas Huber84066782011-08-16 09:34:26 -07001472 (*decoder)->configure(format);
Andreas Huberf9334412010-12-15 15:17:42 -08001473
Lajos Molnar09524832014-07-17 14:29:51 -07001474 // allocate buffers to decrypt widevine source buffers
1475 if (!audio && (mSourceFlags & Source::FLAG_SECURE)) {
1476 Vector<sp<ABuffer> > inputBufs;
1477 CHECK_EQ((*decoder)->getInputBuffers(&inputBufs), (status_t)OK);
1478
1479 Vector<MediaBuffer *> mediaBufs;
1480 for (size_t i = 0; i < inputBufs.size(); i++) {
1481 const sp<ABuffer> &buffer = inputBufs[i];
1482 MediaBuffer *mbuf = new MediaBuffer(buffer->data(), buffer->size());
1483 mediaBufs.push(mbuf);
1484 }
1485
1486 status_t err = mSource->setBuffers(audio, mediaBufs);
1487 if (err != OK) {
1488 for (size_t i = 0; i < mediaBufs.size(); ++i) {
1489 mediaBufs[i]->release();
1490 }
1491 mediaBufs.clear();
1492 ALOGE("Secure source didn't support secure mediaBufs.");
1493 return err;
1494 }
1495 }
Andreas Huberf9334412010-12-15 15:17:42 -08001496 return OK;
1497}
1498
Chong Zhangced1c2f2014-08-08 15:22:35 -07001499void NuPlayer::updateVideoSize(
1500 const sp<AMessage> &inputFormat,
1501 const sp<AMessage> &outputFormat) {
1502 if (inputFormat == NULL) {
1503 ALOGW("Unknown video size, reporting 0x0!");
1504 notifyListener(MEDIA_SET_VIDEO_SIZE, 0, 0);
1505 return;
1506 }
1507
1508 int32_t displayWidth, displayHeight;
Chong Zhangced1c2f2014-08-08 15:22:35 -07001509 if (outputFormat != NULL) {
1510 int32_t width, height;
1511 CHECK(outputFormat->findInt32("width", &width));
1512 CHECK(outputFormat->findInt32("height", &height));
1513
1514 int32_t cropLeft, cropTop, cropRight, cropBottom;
1515 CHECK(outputFormat->findRect(
1516 "crop",
1517 &cropLeft, &cropTop, &cropRight, &cropBottom));
1518
1519 displayWidth = cropRight - cropLeft + 1;
1520 displayHeight = cropBottom - cropTop + 1;
1521
1522 ALOGV("Video output format changed to %d x %d "
1523 "(crop: %d x %d @ (%d, %d))",
1524 width, height,
1525 displayWidth,
1526 displayHeight,
1527 cropLeft, cropTop);
1528 } else {
1529 CHECK(inputFormat->findInt32("width", &displayWidth));
1530 CHECK(inputFormat->findInt32("height", &displayHeight));
1531
1532 ALOGV("Video input format %d x %d", displayWidth, displayHeight);
1533 }
1534
1535 // Take into account sample aspect ratio if necessary:
1536 int32_t sarWidth, sarHeight;
1537 if (inputFormat->findInt32("sar-width", &sarWidth)
1538 && inputFormat->findInt32("sar-height", &sarHeight)) {
1539 ALOGV("Sample aspect ratio %d : %d", sarWidth, sarHeight);
1540
1541 displayWidth = (displayWidth * sarWidth) / sarHeight;
1542
1543 ALOGV("display dimensions %d x %d", displayWidth, displayHeight);
1544 }
1545
1546 int32_t rotationDegrees;
1547 if (!inputFormat->findInt32("rotation-degrees", &rotationDegrees)) {
1548 rotationDegrees = 0;
1549 }
1550
1551 if (rotationDegrees == 90 || rotationDegrees == 270) {
1552 int32_t tmp = displayWidth;
1553 displayWidth = displayHeight;
1554 displayHeight = tmp;
1555 }
1556
1557 notifyListener(
1558 MEDIA_SET_VIDEO_SIZE,
1559 displayWidth,
1560 displayHeight);
1561}
1562
Chong Zhangdcb89b32013-08-06 09:44:47 -07001563void NuPlayer::notifyListener(int msg, int ext1, int ext2, const Parcel *in) {
Andreas Huber43c3e6c2011-01-05 12:17:08 -08001564 if (mDriver == NULL) {
Andreas Huberf9334412010-12-15 15:17:42 -08001565 return;
1566 }
1567
Andreas Huber43c3e6c2011-01-05 12:17:08 -08001568 sp<NuPlayerDriver> driver = mDriver.promote();
Andreas Huberf9334412010-12-15 15:17:42 -08001569
Andreas Huber43c3e6c2011-01-05 12:17:08 -08001570 if (driver == NULL) {
Andreas Huberf9334412010-12-15 15:17:42 -08001571 return;
1572 }
1573
Chong Zhangdcb89b32013-08-06 09:44:47 -07001574 driver->notifyListener(msg, ext1, ext2, in);
Andreas Huberf9334412010-12-15 15:17:42 -08001575}
1576
Chong Zhang7137ec72014-11-12 16:41:05 -08001577void NuPlayer::flushDecoder(bool audio, bool needShutdown) {
Andreas Huber14f76722013-01-15 09:04:18 -08001578 ALOGV("[%s] flushDecoder needShutdown=%d",
1579 audio ? "audio" : "video", needShutdown);
1580
Chong Zhang7137ec72014-11-12 16:41:05 -08001581 const sp<DecoderBase> &decoder = getDecoder(audio);
Lajos Molnar87603c02014-08-20 19:25:30 -07001582 if (decoder == NULL) {
Steve Blockdf64d152012-01-04 20:05:49 +00001583 ALOGI("flushDecoder %s without decoder present",
Andreas Huber6e3d3112011-11-28 12:36:11 -08001584 audio ? "audio" : "video");
Lajos Molnar87603c02014-08-20 19:25:30 -07001585 return;
Andreas Huber6e3d3112011-11-28 12:36:11 -08001586 }
1587
Andreas Huber1aef2112011-01-04 14:01:29 -08001588 // Make sure we don't continue to scan sources until we finish flushing.
1589 ++mScanSourcesGeneration;
Chong Zhang3b032b32015-04-17 15:49:06 -07001590 if (mScanSourcesPending) {
1591 mDeferredActions.push_back(
1592 new SimpleAction(&NuPlayer::performScanSources));
1593 mScanSourcesPending = false;
1594 }
Andreas Huber1aef2112011-01-04 14:01:29 -08001595
Chong Zhang7137ec72014-11-12 16:41:05 -08001596 decoder->signalFlush();
Andreas Huber1aef2112011-01-04 14:01:29 -08001597
1598 FlushStatus newStatus =
1599 needShutdown ? FLUSHING_DECODER_SHUTDOWN : FLUSHING_DECODER;
1600
Lajos Molnarfcd3e942015-03-31 10:06:48 -07001601 mFlushComplete[audio][false /* isDecoder */] = (mRenderer == NULL);
Andy Hung8d121d42014-10-03 09:53:53 -07001602 mFlushComplete[audio][true /* isDecoder */] = false;
Andreas Huber1aef2112011-01-04 14:01:29 -08001603 if (audio) {
Wei Jia53904f32014-07-29 10:22:53 -07001604 ALOGE_IF(mFlushingAudio != NONE,
1605 "audio flushDecoder() is called in state %d", mFlushingAudio);
Andreas Huber1aef2112011-01-04 14:01:29 -08001606 mFlushingAudio = newStatus;
Andreas Huber1aef2112011-01-04 14:01:29 -08001607 } else {
Wei Jia53904f32014-07-29 10:22:53 -07001608 ALOGE_IF(mFlushingVideo != NONE,
1609 "video flushDecoder() is called in state %d", mFlushingVideo);
Andreas Huber1aef2112011-01-04 14:01:29 -08001610 mFlushingVideo = newStatus;
Andreas Huber1aef2112011-01-04 14:01:29 -08001611 }
1612}
1613
Chong Zhangced1c2f2014-08-08 15:22:35 -07001614void NuPlayer::queueDecoderShutdown(
1615 bool audio, bool video, const sp<AMessage> &reply) {
1616 ALOGI("queueDecoderShutdown audio=%d, video=%d", audio, video);
Andreas Huber84066782011-08-16 09:34:26 -07001617
Chong Zhangced1c2f2014-08-08 15:22:35 -07001618 mDeferredActions.push_back(
Wei Jiafef808d2014-10-31 17:57:05 -07001619 new FlushDecoderAction(
1620 audio ? FLUSH_CMD_SHUTDOWN : FLUSH_CMD_NONE,
1621 video ? FLUSH_CMD_SHUTDOWN : FLUSH_CMD_NONE));
Andreas Huber84066782011-08-16 09:34:26 -07001622
Chong Zhangced1c2f2014-08-08 15:22:35 -07001623 mDeferredActions.push_back(
1624 new SimpleAction(&NuPlayer::performScanSources));
Andreas Huber84066782011-08-16 09:34:26 -07001625
Chong Zhangced1c2f2014-08-08 15:22:35 -07001626 mDeferredActions.push_back(new PostMessageAction(reply));
1627
1628 processDeferredActions();
Andreas Huber84066782011-08-16 09:34:26 -07001629}
1630
James Dong0d268a32012-08-31 12:18:27 -07001631status_t NuPlayer::setVideoScalingMode(int32_t mode) {
1632 mVideoScalingMode = mode;
Lajos Molnar1de1e252015-04-30 18:18:34 -07001633 if (mSurface != NULL) {
1634 status_t ret = native_window_set_scaling_mode(mSurface.get(), mVideoScalingMode);
James Dong0d268a32012-08-31 12:18:27 -07001635 if (ret != OK) {
1636 ALOGE("Failed to set scaling mode (%d): %s",
1637 -ret, strerror(-ret));
1638 return ret;
1639 }
1640 }
1641 return OK;
1642}
1643
Chong Zhangdcb89b32013-08-06 09:44:47 -07001644status_t NuPlayer::getTrackInfo(Parcel* reply) const {
Lajos Molnar1d15ab52015-03-04 16:46:34 -08001645 sp<AMessage> msg = new AMessage(kWhatGetTrackInfo, this);
Chong Zhangdcb89b32013-08-06 09:44:47 -07001646 msg->setPointer("reply", reply);
1647
1648 sp<AMessage> response;
1649 status_t err = msg->postAndAwaitResponse(&response);
1650 return err;
1651}
1652
Robert Shih7c4f0d72014-07-09 18:53:31 -07001653status_t NuPlayer::getSelectedTrack(int32_t type, Parcel* reply) const {
Lajos Molnar1d15ab52015-03-04 16:46:34 -08001654 sp<AMessage> msg = new AMessage(kWhatGetSelectedTrack, this);
Robert Shih7c4f0d72014-07-09 18:53:31 -07001655 msg->setPointer("reply", reply);
1656 msg->setInt32("type", type);
1657
1658 sp<AMessage> response;
1659 status_t err = msg->postAndAwaitResponse(&response);
1660 if (err == OK && response != NULL) {
1661 CHECK(response->findInt32("err", &err));
1662 }
1663 return err;
1664}
1665
Robert Shih6ffb1fd2014-10-29 16:24:32 -07001666status_t NuPlayer::selectTrack(size_t trackIndex, bool select, int64_t timeUs) {
Lajos Molnar1d15ab52015-03-04 16:46:34 -08001667 sp<AMessage> msg = new AMessage(kWhatSelectTrack, this);
Chong Zhangdcb89b32013-08-06 09:44:47 -07001668 msg->setSize("trackIndex", trackIndex);
1669 msg->setInt32("select", select);
Robert Shih6ffb1fd2014-10-29 16:24:32 -07001670 msg->setInt64("timeUs", timeUs);
Chong Zhangdcb89b32013-08-06 09:44:47 -07001671
1672 sp<AMessage> response;
1673 status_t err = msg->postAndAwaitResponse(&response);
1674
Chong Zhang404fced2014-06-11 14:45:31 -07001675 if (err != OK) {
1676 return err;
1677 }
1678
1679 if (!response->findInt32("err", &err)) {
1680 err = OK;
1681 }
1682
Chong Zhangdcb89b32013-08-06 09:44:47 -07001683 return err;
1684}
1685
Ronghua Wua73d9e02014-10-08 15:13:29 -07001686status_t NuPlayer::getCurrentPosition(int64_t *mediaUs) {
1687 sp<Renderer> renderer = mRenderer;
1688 if (renderer == NULL) {
1689 return NO_INIT;
1690 }
1691
1692 return renderer->getCurrentPosition(mediaUs);
1693}
1694
1695void NuPlayer::getStats(int64_t *numFramesTotal, int64_t *numFramesDropped) {
Chong Zhang7137ec72014-11-12 16:41:05 -08001696 sp<DecoderBase> decoder = getDecoder(false /* audio */);
1697 if (decoder != NULL) {
1698 decoder->getStats(numFramesTotal, numFramesDropped);
1699 } else {
1700 *numFramesTotal = 0;
1701 *numFramesDropped = 0;
1702 }
Ronghua Wua73d9e02014-10-08 15:13:29 -07001703}
1704
Marco Nelissenf0b72b52014-09-16 15:43:44 -07001705sp<MetaData> NuPlayer::getFileMeta() {
1706 return mSource->getFileFormatMeta();
1707}
1708
Andreas Huberb7c8e912012-11-27 15:02:53 -08001709void NuPlayer::schedulePollDuration() {
Lajos Molnar1d15ab52015-03-04 16:46:34 -08001710 sp<AMessage> msg = new AMessage(kWhatPollDuration, this);
Andreas Huberb7c8e912012-11-27 15:02:53 -08001711 msg->setInt32("generation", mPollDurationGeneration);
1712 msg->post();
1713}
1714
1715void NuPlayer::cancelPollDuration() {
1716 ++mPollDurationGeneration;
1717}
1718
Andreas Hubera1f8ab02012-11-30 10:53:22 -08001719void NuPlayer::processDeferredActions() {
1720 while (!mDeferredActions.empty()) {
1721 // We won't execute any deferred actions until we're no longer in
1722 // an intermediate state, i.e. one more more decoders are currently
1723 // flushing or shutting down.
1724
Andreas Hubera1f8ab02012-11-30 10:53:22 -08001725 if (mFlushingAudio != NONE || mFlushingVideo != NONE) {
1726 // We're currently flushing, postpone the reset until that's
1727 // completed.
1728
1729 ALOGV("postponing action mFlushingAudio=%d, mFlushingVideo=%d",
1730 mFlushingAudio, mFlushingVideo);
1731
1732 break;
1733 }
1734
1735 sp<Action> action = *mDeferredActions.begin();
1736 mDeferredActions.erase(mDeferredActions.begin());
1737
1738 action->execute(this);
1739 }
1740}
1741
Wei Jiae427abf2014-09-22 15:21:11 -07001742void NuPlayer::performSeek(int64_t seekTimeUs, bool needNotify) {
1743 ALOGV("performSeek seekTimeUs=%lld us (%.2f secs), needNotify(%d)",
Lajos Molnar6d339f12015-04-17 16:15:53 -07001744 (long long)seekTimeUs,
Wei Jiae427abf2014-09-22 15:21:11 -07001745 seekTimeUs / 1E6,
1746 needNotify);
Andreas Hubera1f8ab02012-11-30 10:53:22 -08001747
Andy Hungadf34bf2014-09-03 18:22:22 -07001748 if (mSource == NULL) {
1749 // This happens when reset occurs right before the loop mode
1750 // asynchronously seeks to the start of the stream.
1751 LOG_ALWAYS_FATAL_IF(mAudioDecoder != NULL || mVideoDecoder != NULL,
1752 "mSource is NULL and decoders not NULL audio(%p) video(%p)",
1753 mAudioDecoder.get(), mVideoDecoder.get());
1754 return;
1755 }
Andreas Hubera1f8ab02012-11-30 10:53:22 -08001756 mSource->seekTo(seekTimeUs);
Robert Shihd3b0bbb2014-07-23 15:00:25 -07001757 ++mTimedTextGeneration;
Andreas Hubera1f8ab02012-11-30 10:53:22 -08001758
Andreas Hubera1f8ab02012-11-30 10:53:22 -08001759 // everything's flushed, continue playback.
1760}
1761
Wei Jiafef808d2014-10-31 17:57:05 -07001762void NuPlayer::performDecoderFlush(FlushCommand audio, FlushCommand video) {
1763 ALOGV("performDecoderFlush audio=%d, video=%d", audio, video);
Andreas Hubera1f8ab02012-11-30 10:53:22 -08001764
Wei Jiafef808d2014-10-31 17:57:05 -07001765 if ((audio == FLUSH_CMD_NONE || mAudioDecoder == NULL)
1766 && (video == FLUSH_CMD_NONE || mVideoDecoder == NULL)) {
Andreas Hubera1f8ab02012-11-30 10:53:22 -08001767 return;
1768 }
1769
Wei Jiafef808d2014-10-31 17:57:05 -07001770 if (audio != FLUSH_CMD_NONE && mAudioDecoder != NULL) {
1771 flushDecoder(true /* audio */, (audio == FLUSH_CMD_SHUTDOWN));
Andreas Hubera1f8ab02012-11-30 10:53:22 -08001772 }
1773
Wei Jiafef808d2014-10-31 17:57:05 -07001774 if (video != FLUSH_CMD_NONE && mVideoDecoder != NULL) {
1775 flushDecoder(false /* audio */, (video == FLUSH_CMD_SHUTDOWN));
Andreas Hubera1f8ab02012-11-30 10:53:22 -08001776 }
1777}
1778
1779void NuPlayer::performReset() {
1780 ALOGV("performReset");
1781
1782 CHECK(mAudioDecoder == NULL);
1783 CHECK(mVideoDecoder == NULL);
1784
1785 cancelPollDuration();
1786
1787 ++mScanSourcesGeneration;
1788 mScanSourcesPending = false;
1789
Lajos Molnar09524832014-07-17 14:29:51 -07001790 if (mRendererLooper != NULL) {
1791 if (mRenderer != NULL) {
1792 mRendererLooper->unregisterHandler(mRenderer->id());
1793 }
1794 mRendererLooper->stop();
1795 mRendererLooper.clear();
1796 }
Andreas Hubera1f8ab02012-11-30 10:53:22 -08001797 mRenderer.clear();
Wei Jia57568df2014-09-22 10:16:29 -07001798 ++mRendererGeneration;
Andreas Hubera1f8ab02012-11-30 10:53:22 -08001799
1800 if (mSource != NULL) {
1801 mSource->stop();
Andreas Huberb5f25f02013-02-05 10:14:26 -08001802
Andreas Hubera1f8ab02012-11-30 10:53:22 -08001803 mSource.clear();
1804 }
1805
1806 if (mDriver != NULL) {
1807 sp<NuPlayerDriver> driver = mDriver.promote();
1808 if (driver != NULL) {
1809 driver->notifyResetComplete();
1810 }
1811 }
Andreas Huber57a339c2012-12-03 11:18:00 -08001812
1813 mStarted = false;
Andreas Hubera1f8ab02012-11-30 10:53:22 -08001814}
1815
1816void NuPlayer::performScanSources() {
1817 ALOGV("performScanSources");
1818
Andreas Huber57a339c2012-12-03 11:18:00 -08001819 if (!mStarted) {
1820 return;
1821 }
1822
Andreas Hubera1f8ab02012-11-30 10:53:22 -08001823 if (mAudioDecoder == NULL || mVideoDecoder == NULL) {
1824 postScanSources();
1825 }
1826}
1827
Lajos Molnar1de1e252015-04-30 18:18:34 -07001828void NuPlayer::performSetSurface(const sp<Surface> &surface) {
Andreas Huber57a339c2012-12-03 11:18:00 -08001829 ALOGV("performSetSurface");
1830
Lajos Molnar1de1e252015-04-30 18:18:34 -07001831 mSurface = surface;
Andreas Huber57a339c2012-12-03 11:18:00 -08001832
1833 // XXX - ignore error from setVideoScalingMode for now
1834 setVideoScalingMode(mVideoScalingMode);
Chong Zhang13d6faa2014-08-22 15:35:28 -07001835
1836 if (mDriver != NULL) {
1837 sp<NuPlayerDriver> driver = mDriver.promote();
1838 if (driver != NULL) {
1839 driver->notifySetSurfaceComplete();
1840 }
1841 }
Andreas Huber57a339c2012-12-03 11:18:00 -08001842}
1843
Chong Zhangf8d71772014-11-26 15:08:34 -08001844void NuPlayer::performResumeDecoders(bool needNotify) {
1845 if (needNotify) {
1846 mResumePending = true;
1847 if (mVideoDecoder == NULL) {
1848 // if audio-only, we can notify seek complete now,
1849 // as the resume operation will be relatively fast.
1850 finishResume();
1851 }
1852 }
1853
Chong Zhang7137ec72014-11-12 16:41:05 -08001854 if (mVideoDecoder != NULL) {
Chong Zhangf8d71772014-11-26 15:08:34 -08001855 // When there is continuous seek, MediaPlayer will cache the seek
1856 // position, and send down new seek request when previous seek is
1857 // complete. Let's wait for at least one video output frame before
1858 // notifying seek complete, so that the video thumbnail gets updated
1859 // when seekbar is dragged.
1860 mVideoDecoder->signalResume(needNotify);
Chong Zhang7137ec72014-11-12 16:41:05 -08001861 }
1862
1863 if (mAudioDecoder != NULL) {
Chong Zhangf8d71772014-11-26 15:08:34 -08001864 mAudioDecoder->signalResume(false /* needNotify */);
1865 }
1866}
1867
1868void NuPlayer::finishResume() {
1869 if (mResumePending) {
1870 mResumePending = false;
1871 if (mDriver != NULL) {
1872 sp<NuPlayerDriver> driver = mDriver.promote();
1873 if (driver != NULL) {
1874 driver->notifySeekComplete();
1875 }
1876 }
Chong Zhang7137ec72014-11-12 16:41:05 -08001877 }
1878}
1879
Andreas Huber9575c962013-02-05 13:59:56 -08001880void NuPlayer::onSourceNotify(const sp<AMessage> &msg) {
1881 int32_t what;
1882 CHECK(msg->findInt32("what", &what));
1883
1884 switch (what) {
Lajos Molnarfcd3e942015-03-31 10:06:48 -07001885 case Source::kWhatInstantiateSecureDecoders:
1886 {
1887 if (mSource == NULL) {
1888 // This is a stale notification from a source that was
1889 // asynchronously preparing when the client called reset().
1890 // We handled the reset, the source is gone.
1891 break;
1892 }
1893
1894 sp<AMessage> reply;
1895 CHECK(msg->findMessage("reply", &reply));
1896 status_t err = onInstantiateSecureDecoders();
1897 reply->setInt32("err", err);
1898 reply->post();
1899 break;
1900 }
1901
Andreas Huber9575c962013-02-05 13:59:56 -08001902 case Source::kWhatPrepared:
1903 {
Andreas Huberb5f28d42013-04-25 15:11:19 -07001904 if (mSource == NULL) {
1905 // This is a stale notification from a source that was
1906 // asynchronously preparing when the client called reset().
1907 // We handled the reset, the source is gone.
1908 break;
1909 }
1910
Andreas Huberec0c5972013-02-05 14:47:13 -08001911 int32_t err;
1912 CHECK(msg->findInt32("err", &err));
1913
Lajos Molnarfcd3e942015-03-31 10:06:48 -07001914 if (err != OK) {
1915 // shut down potential secure codecs in case client never calls reset
1916 mDeferredActions.push_back(
1917 new FlushDecoderAction(FLUSH_CMD_SHUTDOWN /* audio */,
1918 FLUSH_CMD_SHUTDOWN /* video */));
1919 processDeferredActions();
1920 }
1921
Andreas Huber9575c962013-02-05 13:59:56 -08001922 sp<NuPlayerDriver> driver = mDriver.promote();
1923 if (driver != NULL) {
Marco Nelissendd114d12014-05-28 15:23:14 -07001924 // notify duration first, so that it's definitely set when
1925 // the app received the "prepare complete" callback.
1926 int64_t durationUs;
1927 if (mSource->getDuration(&durationUs) == OK) {
1928 driver->notifyDuration(durationUs);
1929 }
Andreas Huberec0c5972013-02-05 14:47:13 -08001930 driver->notifyPrepareCompleted(err);
Andreas Huber9575c962013-02-05 13:59:56 -08001931 }
Andreas Huber99759402013-04-01 14:28:31 -07001932
Andreas Huber9575c962013-02-05 13:59:56 -08001933 break;
1934 }
1935
1936 case Source::kWhatFlagsChanged:
1937 {
1938 uint32_t flags;
1939 CHECK(msg->findInt32("flags", (int32_t *)&flags));
1940
Chong Zhang4b7069d2013-09-11 12:52:43 -07001941 sp<NuPlayerDriver> driver = mDriver.promote();
1942 if (driver != NULL) {
Wei Jia895651b2014-12-10 17:31:52 -08001943 if ((flags & NuPlayer::Source::FLAG_CAN_SEEK) == 0) {
1944 driver->notifyListener(
1945 MEDIA_INFO, MEDIA_INFO_NOT_SEEKABLE, 0);
1946 }
Chong Zhang4b7069d2013-09-11 12:52:43 -07001947 driver->notifyFlagsChanged(flags);
1948 }
1949
Andreas Huber9575c962013-02-05 13:59:56 -08001950 if ((mSourceFlags & Source::FLAG_DYNAMIC_DURATION)
1951 && (!(flags & Source::FLAG_DYNAMIC_DURATION))) {
1952 cancelPollDuration();
1953 } else if (!(mSourceFlags & Source::FLAG_DYNAMIC_DURATION)
1954 && (flags & Source::FLAG_DYNAMIC_DURATION)
1955 && (mAudioDecoder != NULL || mVideoDecoder != NULL)) {
1956 schedulePollDuration();
1957 }
1958
1959 mSourceFlags = flags;
1960 break;
1961 }
1962
1963 case Source::kWhatVideoSizeChanged:
1964 {
Chong Zhangced1c2f2014-08-08 15:22:35 -07001965 sp<AMessage> format;
1966 CHECK(msg->findMessage("format", &format));
Andreas Huber9575c962013-02-05 13:59:56 -08001967
Chong Zhangced1c2f2014-08-08 15:22:35 -07001968 updateVideoSize(format);
Andreas Huber9575c962013-02-05 13:59:56 -08001969 break;
1970 }
1971
Chong Zhang2a3cc9a2014-08-21 17:48:26 -07001972 case Source::kWhatBufferingUpdate:
1973 {
1974 int32_t percentage;
1975 CHECK(msg->findInt32("percentage", &percentage));
1976
1977 notifyListener(MEDIA_BUFFERING_UPDATE, percentage, 0);
1978 break;
1979 }
1980
Chong Zhangefbb6192015-01-30 17:13:27 -08001981 case Source::kWhatPauseOnBufferingStart:
1982 {
1983 // ignore if not playing
Chong Zhang8a048332015-05-06 15:16:28 -07001984 if (mStarted) {
Chong Zhangefbb6192015-01-30 17:13:27 -08001985 ALOGI("buffer low, pausing...");
1986
Chong Zhang8a048332015-05-06 15:16:28 -07001987 mPausedForBuffering = true;
Chong Zhangefbb6192015-01-30 17:13:27 -08001988 onPause();
1989 }
1990 // fall-thru
1991 }
1992
Roger Jönssonb50e83e2013-01-21 16:26:41 +01001993 case Source::kWhatBufferingStart:
1994 {
1995 notifyListener(MEDIA_INFO, MEDIA_INFO_BUFFERING_START, 0);
1996 break;
1997 }
1998
Chong Zhangefbb6192015-01-30 17:13:27 -08001999 case Source::kWhatResumeOnBufferingEnd:
2000 {
2001 // ignore if not playing
Chong Zhang8a048332015-05-06 15:16:28 -07002002 if (mStarted) {
Chong Zhangefbb6192015-01-30 17:13:27 -08002003 ALOGI("buffer ready, resuming...");
2004
Chong Zhang8a048332015-05-06 15:16:28 -07002005 mPausedForBuffering = false;
2006
2007 // do not resume yet if client didn't unpause
2008 if (!mPausedByClient) {
2009 onResume();
2010 }
Chong Zhangefbb6192015-01-30 17:13:27 -08002011 }
2012 // fall-thru
2013 }
2014
Roger Jönssonb50e83e2013-01-21 16:26:41 +01002015 case Source::kWhatBufferingEnd:
2016 {
2017 notifyListener(MEDIA_INFO, MEDIA_INFO_BUFFERING_END, 0);
2018 break;
2019 }
2020
Chong Zhangefbb6192015-01-30 17:13:27 -08002021 case Source::kWhatCacheStats:
2022 {
2023 int32_t kbps;
2024 CHECK(msg->findInt32("bandwidth", &kbps));
2025
2026 notifyListener(MEDIA_INFO, MEDIA_INFO_NETWORK_BANDWIDTH, kbps);
2027 break;
2028 }
2029
Chong Zhangdcb89b32013-08-06 09:44:47 -07002030 case Source::kWhatSubtitleData:
2031 {
2032 sp<ABuffer> buffer;
2033 CHECK(msg->findBuffer("buffer", &buffer));
2034
Chong Zhang404fced2014-06-11 14:45:31 -07002035 sendSubtitleData(buffer, 0 /* baseIndex */);
Chong Zhangdcb89b32013-08-06 09:44:47 -07002036 break;
2037 }
2038
Robert Shih08528432015-04-08 09:06:54 -07002039 case Source::kWhatTimedMetaData:
2040 {
2041 sp<ABuffer> buffer;
2042 if (!msg->findBuffer("buffer", &buffer)) {
2043 notifyListener(MEDIA_INFO, MEDIA_INFO_METADATA_UPDATE, 0);
2044 } else {
2045 sendTimedMetaData(buffer);
2046 }
2047 break;
2048 }
2049
Robert Shihd3b0bbb2014-07-23 15:00:25 -07002050 case Source::kWhatTimedTextData:
2051 {
2052 int32_t generation;
2053 if (msg->findInt32("generation", &generation)
2054 && generation != mTimedTextGeneration) {
2055 break;
2056 }
2057
2058 sp<ABuffer> buffer;
2059 CHECK(msg->findBuffer("buffer", &buffer));
2060
2061 sp<NuPlayerDriver> driver = mDriver.promote();
2062 if (driver == NULL) {
2063 break;
2064 }
2065
2066 int posMs;
2067 int64_t timeUs, posUs;
2068 driver->getCurrentPosition(&posMs);
2069 posUs = posMs * 1000;
2070 CHECK(buffer->meta()->findInt64("timeUs", &timeUs));
2071
2072 if (posUs < timeUs) {
2073 if (!msg->findInt32("generation", &generation)) {
2074 msg->setInt32("generation", mTimedTextGeneration);
2075 }
2076 msg->post(timeUs - posUs);
2077 } else {
2078 sendTimedTextData(buffer);
2079 }
2080 break;
2081 }
2082
Andreas Huber14f76722013-01-15 09:04:18 -08002083 case Source::kWhatQueueDecoderShutdown:
2084 {
2085 int32_t audio, video;
2086 CHECK(msg->findInt32("audio", &audio));
2087 CHECK(msg->findInt32("video", &video));
2088
2089 sp<AMessage> reply;
2090 CHECK(msg->findMessage("reply", &reply));
2091
2092 queueDecoderShutdown(audio, video, reply);
2093 break;
2094 }
2095
Ronghua Wu80276872014-08-28 15:50:29 -07002096 case Source::kWhatDrmNoLicense:
2097 {
2098 notifyListener(MEDIA_ERROR, MEDIA_ERROR_UNKNOWN, ERROR_DRM_NO_LICENSE);
2099 break;
2100 }
2101
Andreas Huber9575c962013-02-05 13:59:56 -08002102 default:
2103 TRESPASS();
2104 }
2105}
2106
Chong Zhanga7fa1d92014-06-11 14:49:23 -07002107void NuPlayer::onClosedCaptionNotify(const sp<AMessage> &msg) {
2108 int32_t what;
2109 CHECK(msg->findInt32("what", &what));
2110
2111 switch (what) {
2112 case NuPlayer::CCDecoder::kWhatClosedCaptionData:
2113 {
2114 sp<ABuffer> buffer;
2115 CHECK(msg->findBuffer("buffer", &buffer));
2116
2117 size_t inbandTracks = 0;
2118 if (mSource != NULL) {
2119 inbandTracks = mSource->getTrackCount();
2120 }
2121
2122 sendSubtitleData(buffer, inbandTracks);
2123 break;
2124 }
2125
2126 case NuPlayer::CCDecoder::kWhatTrackAdded:
2127 {
2128 notifyListener(MEDIA_INFO, MEDIA_INFO_METADATA_UPDATE, 0);
2129
2130 break;
2131 }
2132
2133 default:
2134 TRESPASS();
2135 }
2136
2137
2138}
2139
Chong Zhang404fced2014-06-11 14:45:31 -07002140void NuPlayer::sendSubtitleData(const sp<ABuffer> &buffer, int32_t baseIndex) {
2141 int32_t trackIndex;
2142 int64_t timeUs, durationUs;
2143 CHECK(buffer->meta()->findInt32("trackIndex", &trackIndex));
2144 CHECK(buffer->meta()->findInt64("timeUs", &timeUs));
2145 CHECK(buffer->meta()->findInt64("durationUs", &durationUs));
2146
2147 Parcel in;
2148 in.writeInt32(trackIndex + baseIndex);
2149 in.writeInt64(timeUs);
2150 in.writeInt64(durationUs);
2151 in.writeInt32(buffer->size());
2152 in.writeInt32(buffer->size());
2153 in.write(buffer->data(), buffer->size());
2154
2155 notifyListener(MEDIA_SUBTITLE_DATA, 0, 0, &in);
2156}
Robert Shihd3b0bbb2014-07-23 15:00:25 -07002157
Robert Shih08528432015-04-08 09:06:54 -07002158void NuPlayer::sendTimedMetaData(const sp<ABuffer> &buffer) {
2159 int64_t timeUs;
2160 CHECK(buffer->meta()->findInt64("timeUs", &timeUs));
2161
2162 Parcel in;
2163 in.writeInt64(timeUs);
2164 in.writeInt32(buffer->size());
2165 in.writeInt32(buffer->size());
2166 in.write(buffer->data(), buffer->size());
2167
2168 notifyListener(MEDIA_META_DATA, 0, 0, &in);
2169}
2170
Robert Shihd3b0bbb2014-07-23 15:00:25 -07002171void NuPlayer::sendTimedTextData(const sp<ABuffer> &buffer) {
2172 const void *data;
2173 size_t size = 0;
2174 int64_t timeUs;
2175 int32_t flag = TextDescriptions::LOCAL_DESCRIPTIONS;
2176
2177 AString mime;
2178 CHECK(buffer->meta()->findString("mime", &mime));
2179 CHECK(strcasecmp(mime.c_str(), MEDIA_MIMETYPE_TEXT_3GPP) == 0);
2180
2181 data = buffer->data();
2182 size = buffer->size();
2183
2184 Parcel parcel;
2185 if (size > 0) {
2186 CHECK(buffer->meta()->findInt64("timeUs", &timeUs));
2187 flag |= TextDescriptions::IN_BAND_TEXT_3GPP;
2188 TextDescriptions::getParcelOfDescriptions(
2189 (const uint8_t *)data, size, flag, timeUs / 1000, &parcel);
2190 }
2191
2192 if ((parcel.dataSize() > 0)) {
2193 notifyListener(MEDIA_TIMED_TEXT, 0, 0, &parcel);
2194 } else { // send an empty timed text
2195 notifyListener(MEDIA_TIMED_TEXT, 0, 0);
2196 }
2197}
Andreas Huberb5f25f02013-02-05 10:14:26 -08002198////////////////////////////////////////////////////////////////////////////////
2199
Chong Zhangced1c2f2014-08-08 15:22:35 -07002200sp<AMessage> NuPlayer::Source::getFormat(bool audio) {
2201 sp<MetaData> meta = getFormatMeta(audio);
2202
2203 if (meta == NULL) {
2204 return NULL;
2205 }
2206
2207 sp<AMessage> msg = new AMessage;
2208
2209 if(convertMetaDataToMessage(meta, &msg) == OK) {
2210 return msg;
2211 }
2212 return NULL;
2213}
2214
Andreas Huber9575c962013-02-05 13:59:56 -08002215void NuPlayer::Source::notifyFlagsChanged(uint32_t flags) {
2216 sp<AMessage> notify = dupNotify();
2217 notify->setInt32("what", kWhatFlagsChanged);
2218 notify->setInt32("flags", flags);
2219 notify->post();
2220}
2221
Chong Zhangced1c2f2014-08-08 15:22:35 -07002222void NuPlayer::Source::notifyVideoSizeChanged(const sp<AMessage> &format) {
Andreas Huber9575c962013-02-05 13:59:56 -08002223 sp<AMessage> notify = dupNotify();
2224 notify->setInt32("what", kWhatVideoSizeChanged);
Chong Zhangced1c2f2014-08-08 15:22:35 -07002225 notify->setMessage("format", format);
Andreas Huber9575c962013-02-05 13:59:56 -08002226 notify->post();
2227}
2228
Andreas Huberec0c5972013-02-05 14:47:13 -08002229void NuPlayer::Source::notifyPrepared(status_t err) {
Andreas Huber9575c962013-02-05 13:59:56 -08002230 sp<AMessage> notify = dupNotify();
2231 notify->setInt32("what", kWhatPrepared);
Andreas Huberec0c5972013-02-05 14:47:13 -08002232 notify->setInt32("err", err);
Andreas Huber9575c962013-02-05 13:59:56 -08002233 notify->post();
2234}
2235
Lajos Molnarfcd3e942015-03-31 10:06:48 -07002236void NuPlayer::Source::notifyInstantiateSecureDecoders(const sp<AMessage> &reply) {
2237 sp<AMessage> notify = dupNotify();
2238 notify->setInt32("what", kWhatInstantiateSecureDecoders);
2239 notify->setMessage("reply", reply);
2240 notify->post();
2241}
2242
Andreas Huber84333e02014-02-07 15:36:10 -08002243void NuPlayer::Source::onMessageReceived(const sp<AMessage> & /* msg */) {
Andreas Huberb5f25f02013-02-05 10:14:26 -08002244 TRESPASS();
2245}
2246
Andreas Huberf9334412010-12-15 15:17:42 -08002247} // namespace android