blob: 4e3009386e65f889460fd23a94eb5f54497552f2 [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>
Andy McFadden8ba01022012-12-18 09:46:54 -080051#include <gui/IGraphicBufferProducer.h>
Andreas Huberf9334412010-12-15 15:17:42 -080052
Andreas Huber3fe62152011-09-16 15:09:22 -070053#include "avc_utils.h"
54
Andreas Huber84066782011-08-16 09:34:26 -070055#include "ESDS.h"
56#include <media/stagefright/Utils.h>
57
Andreas Huberf9334412010-12-15 15:17:42 -080058namespace android {
59
Andreas Hubera1f8ab02012-11-30 10:53:22 -080060struct NuPlayer::Action : public RefBase {
61 Action() {}
62
63 virtual void execute(NuPlayer *player) = 0;
64
65private:
66 DISALLOW_EVIL_CONSTRUCTORS(Action);
67};
68
69struct NuPlayer::SeekAction : public Action {
Wei Jiae427abf2014-09-22 15:21:11 -070070 SeekAction(int64_t seekTimeUs, bool needNotify)
71 : mSeekTimeUs(seekTimeUs),
72 mNeedNotify(needNotify) {
Andreas Hubera1f8ab02012-11-30 10:53:22 -080073 }
74
75 virtual void execute(NuPlayer *player) {
Wei Jiae427abf2014-09-22 15:21:11 -070076 player->performSeek(mSeekTimeUs, mNeedNotify);
Andreas Hubera1f8ab02012-11-30 10:53:22 -080077 }
78
79private:
80 int64_t mSeekTimeUs;
Wei Jiae427abf2014-09-22 15:21:11 -070081 bool mNeedNotify;
Andreas Hubera1f8ab02012-11-30 10:53:22 -080082
83 DISALLOW_EVIL_CONSTRUCTORS(SeekAction);
84};
85
Chong Zhangf8d71772014-11-26 15:08:34 -080086struct NuPlayer::ResumeDecoderAction : public Action {
87 ResumeDecoderAction(bool needNotify)
88 : mNeedNotify(needNotify) {
89 }
90
91 virtual void execute(NuPlayer *player) {
92 player->performResumeDecoders(mNeedNotify);
93 }
94
95private:
96 bool mNeedNotify;
97
98 DISALLOW_EVIL_CONSTRUCTORS(ResumeDecoderAction);
99};
100
Andreas Huber57a339c2012-12-03 11:18:00 -0800101struct NuPlayer::SetSurfaceAction : public Action {
102 SetSurfaceAction(const sp<NativeWindowWrapper> &wrapper)
103 : mWrapper(wrapper) {
104 }
105
106 virtual void execute(NuPlayer *player) {
107 player->performSetSurface(mWrapper);
108 }
109
110private:
111 sp<NativeWindowWrapper> mWrapper;
112
113 DISALLOW_EVIL_CONSTRUCTORS(SetSurfaceAction);
114};
115
Wei Jiafef808d2014-10-31 17:57:05 -0700116struct NuPlayer::FlushDecoderAction : public Action {
117 FlushDecoderAction(FlushCommand audio, FlushCommand video)
Andreas Huber14f76722013-01-15 09:04:18 -0800118 : mAudio(audio),
119 mVideo(video) {
120 }
121
122 virtual void execute(NuPlayer *player) {
Wei Jiafef808d2014-10-31 17:57:05 -0700123 player->performDecoderFlush(mAudio, mVideo);
Andreas Huber14f76722013-01-15 09:04:18 -0800124 }
125
126private:
Wei Jiafef808d2014-10-31 17:57:05 -0700127 FlushCommand mAudio;
128 FlushCommand mVideo;
Andreas Huber14f76722013-01-15 09:04:18 -0800129
Wei Jiafef808d2014-10-31 17:57:05 -0700130 DISALLOW_EVIL_CONSTRUCTORS(FlushDecoderAction);
Andreas Huber14f76722013-01-15 09:04:18 -0800131};
132
133struct NuPlayer::PostMessageAction : public Action {
134 PostMessageAction(const sp<AMessage> &msg)
135 : mMessage(msg) {
136 }
137
138 virtual void execute(NuPlayer *) {
139 mMessage->post();
140 }
141
142private:
143 sp<AMessage> mMessage;
144
145 DISALLOW_EVIL_CONSTRUCTORS(PostMessageAction);
146};
147
Andreas Hubera1f8ab02012-11-30 10:53:22 -0800148// Use this if there's no state necessary to save in order to execute
149// the action.
150struct NuPlayer::SimpleAction : public Action {
151 typedef void (NuPlayer::*ActionFunc)();
152
153 SimpleAction(ActionFunc func)
154 : mFunc(func) {
155 }
156
157 virtual void execute(NuPlayer *player) {
158 (player->*mFunc)();
159 }
160
161private:
162 ActionFunc mFunc;
163
164 DISALLOW_EVIL_CONSTRUCTORS(SimpleAction);
165};
166
Andreas Huberf9334412010-12-15 15:17:42 -0800167////////////////////////////////////////////////////////////////////////////////
168
169NuPlayer::NuPlayer()
Andreas Huber9b80c2b2011-06-30 15:47:02 -0700170 : mUIDValid(false),
Andreas Huber9575c962013-02-05 13:59:56 -0800171 mSourceFlags(0),
Wei Jiabc2fb722014-07-08 16:37:57 -0700172 mOffloadAudio(false),
Wei Jia88703c32014-08-06 11:24:07 -0700173 mAudioDecoderGeneration(0),
174 mVideoDecoderGeneration(0),
Wei Jia57568df2014-09-22 10:16:29 -0700175 mRendererGeneration(0),
Andreas Huber9b80c2b2011-06-30 15:47:02 -0700176 mAudioEOS(false),
Andreas Huberf9334412010-12-15 15:17:42 -0800177 mVideoEOS(false),
Andreas Huber5bc087c2010-12-23 10:27:40 -0800178 mScanSourcesPending(false),
Andreas Huber1aef2112011-01-04 14:01:29 -0800179 mScanSourcesGeneration(0),
Andreas Huberb7c8e912012-11-27 15:02:53 -0800180 mPollDurationGeneration(0),
Robert Shihd3b0bbb2014-07-23 15:00:25 -0700181 mTimedTextGeneration(0),
Andreas Huberf9334412010-12-15 15:17:42 -0800182 mFlushingAudio(NONE),
Andreas Huber1aef2112011-01-04 14:01:29 -0800183 mFlushingVideo(NONE),
Chong Zhangf8d71772014-11-26 15:08:34 -0800184 mResumePending(false),
Andreas Huber57a339c2012-12-03 11:18:00 -0800185 mVideoScalingMode(NATIVE_WINDOW_SCALING_MODE_SCALE_TO_WINDOW),
Lajos Molnar3a474aa2015-04-24 17:10:07 -0700186 mPlaybackSettings(AUDIO_PLAYBACK_RATE_DEFAULT),
187 mVideoFpsHint(-1.f),
Chong Zhangefbb6192015-01-30 17:13:27 -0800188 mStarted(false),
189 mPaused(false),
190 mPausedByClient(false) {
Andy Hung8d121d42014-10-03 09:53:53 -0700191 clearFlushComplete();
Andreas Huberf9334412010-12-15 15:17:42 -0800192}
193
194NuPlayer::~NuPlayer() {
195}
196
Andreas Huber9b80c2b2011-06-30 15:47:02 -0700197void NuPlayer::setUID(uid_t uid) {
198 mUIDValid = true;
199 mUID = uid;
200}
201
Andreas Huber43c3e6c2011-01-05 12:17:08 -0800202void NuPlayer::setDriver(const wp<NuPlayerDriver> &driver) {
203 mDriver = driver;
Andreas Huberf9334412010-12-15 15:17:42 -0800204}
205
Andreas Huber9575c962013-02-05 13:59:56 -0800206void NuPlayer::setDataSourceAsync(const sp<IStreamSource> &source) {
Lajos Molnar1d15ab52015-03-04 16:46:34 -0800207 sp<AMessage> msg = new AMessage(kWhatSetDataSource, this);
Andreas Huberf9334412010-12-15 15:17:42 -0800208
Lajos Molnar1d15ab52015-03-04 16:46:34 -0800209 sp<AMessage> notify = new AMessage(kWhatSourceNotify, this);
Andreas Huberb5f25f02013-02-05 10:14:26 -0800210
Andreas Huber240abcc2014-02-13 13:32:37 -0800211 msg->setObject("source", new StreamingSource(notify, source));
Andreas Huber5bc087c2010-12-23 10:27:40 -0800212 msg->post();
213}
Andreas Huberf9334412010-12-15 15:17:42 -0800214
Andreas Huberafed0e12011-09-20 15:39:58 -0700215static bool IsHTTPLiveURL(const char *url) {
216 if (!strncasecmp("http://", url, 7)
Andreas Huber99759402013-04-01 14:28:31 -0700217 || !strncasecmp("https://", url, 8)
218 || !strncasecmp("file://", url, 7)) {
Andreas Huberafed0e12011-09-20 15:39:58 -0700219 size_t len = strlen(url);
220 if (len >= 5 && !strcasecmp(".m3u8", &url[len - 5])) {
221 return true;
222 }
223
224 if (strstr(url,"m3u8")) {
225 return true;
226 }
227 }
228
229 return false;
230}
231
Andreas Huber9575c962013-02-05 13:59:56 -0800232void NuPlayer::setDataSourceAsync(
Andreas Huber1b86fe02014-01-29 11:13:26 -0800233 const sp<IMediaHTTPService> &httpService,
234 const char *url,
235 const KeyedVector<String8, String8> *headers) {
Chong Zhang3de157d2014-08-05 20:54:44 -0700236
Lajos Molnar1d15ab52015-03-04 16:46:34 -0800237 sp<AMessage> msg = new AMessage(kWhatSetDataSource, this);
Oscar Rydhé7a33b772012-02-20 10:15:48 +0100238 size_t len = strlen(url);
Andreas Huber5bc087c2010-12-23 10:27:40 -0800239
Lajos Molnar1d15ab52015-03-04 16:46:34 -0800240 sp<AMessage> notify = new AMessage(kWhatSourceNotify, this);
Andreas Huberb5f25f02013-02-05 10:14:26 -0800241
Andreas Huberafed0e12011-09-20 15:39:58 -0700242 sp<Source> source;
243 if (IsHTTPLiveURL(url)) {
Andreas Huber81e68442014-02-05 11:52:33 -0800244 source = new HTTPLiveSource(notify, httpService, url, headers);
Andreas Huberafed0e12011-09-20 15:39:58 -0700245 } else if (!strncasecmp(url, "rtsp://", 7)) {
Andreas Huber1b86fe02014-01-29 11:13:26 -0800246 source = new RTSPSource(
247 notify, httpService, url, headers, mUIDValid, mUID);
Oscar Rydhé7a33b772012-02-20 10:15:48 +0100248 } else if ((!strncasecmp(url, "http://", 7)
249 || !strncasecmp(url, "https://", 8))
250 && ((len >= 4 && !strcasecmp(".sdp", &url[len - 4]))
251 || strstr(url, ".sdp?"))) {
Andreas Huber1b86fe02014-01-29 11:13:26 -0800252 source = new RTSPSource(
253 notify, httpService, url, headers, mUIDValid, mUID, true);
Andreas Huber2bfdd422011-10-11 15:24:07 -0700254 } else {
Chong Zhang3de157d2014-08-05 20:54:44 -0700255 sp<GenericSource> genericSource =
256 new GenericSource(notify, mUIDValid, mUID);
257 // Don't set FLAG_SECURE on mSourceFlags here for widevine.
258 // The correct flags will be updated in Source::kWhatFlagsChanged
259 // handler when GenericSource is prepared.
Andreas Huber2bfdd422011-10-11 15:24:07 -0700260
Chong Zhanga19f33e2014-08-07 15:35:07 -0700261 status_t err = genericSource->setDataSource(httpService, url, headers);
Chong Zhang3de157d2014-08-05 20:54:44 -0700262
263 if (err == OK) {
264 source = genericSource;
265 } else {
Chong Zhanga19f33e2014-08-07 15:35:07 -0700266 ALOGE("Failed to set data source!");
Chong Zhang3de157d2014-08-05 20:54:44 -0700267 }
268 }
Andreas Huberafed0e12011-09-20 15:39:58 -0700269 msg->setObject("source", source);
270 msg->post();
271}
272
Andreas Huber9575c962013-02-05 13:59:56 -0800273void NuPlayer::setDataSourceAsync(int fd, int64_t offset, int64_t length) {
Lajos Molnar1d15ab52015-03-04 16:46:34 -0800274 sp<AMessage> msg = new AMessage(kWhatSetDataSource, this);
Andreas Huberafed0e12011-09-20 15:39:58 -0700275
Lajos Molnar1d15ab52015-03-04 16:46:34 -0800276 sp<AMessage> notify = new AMessage(kWhatSourceNotify, this);
Andreas Huberb5f25f02013-02-05 10:14:26 -0800277
Chong Zhang3de157d2014-08-05 20:54:44 -0700278 sp<GenericSource> source =
279 new GenericSource(notify, mUIDValid, mUID);
280
Chong Zhanga19f33e2014-08-07 15:35:07 -0700281 status_t err = source->setDataSource(fd, offset, length);
Chong Zhang3de157d2014-08-05 20:54:44 -0700282
283 if (err != OK) {
Chong Zhanga19f33e2014-08-07 15:35:07 -0700284 ALOGE("Failed to set data source!");
Chong Zhang3de157d2014-08-05 20:54:44 -0700285 source = NULL;
286 }
287
Andreas Huberafed0e12011-09-20 15:39:58 -0700288 msg->setObject("source", source);
Andreas Huberf9334412010-12-15 15:17:42 -0800289 msg->post();
290}
291
Chris Watkins99f31602015-03-20 13:06:33 -0700292void NuPlayer::setDataSourceAsync(const sp<DataSource> &dataSource) {
293 sp<AMessage> msg = new AMessage(kWhatSetDataSource, this);
294 sp<AMessage> notify = new AMessage(kWhatSourceNotify, this);
295
296 sp<GenericSource> source = new GenericSource(notify, mUIDValid, mUID);
297 status_t err = source->setDataSource(dataSource);
298
299 if (err != OK) {
300 ALOGE("Failed to set data source!");
301 source = NULL;
302 }
303
304 msg->setObject("source", source);
305 msg->post();
306}
307
Andreas Huber9575c962013-02-05 13:59:56 -0800308void NuPlayer::prepareAsync() {
Lajos Molnar1d15ab52015-03-04 16:46:34 -0800309 (new AMessage(kWhatPrepare, this))->post();
Andreas Huber9575c962013-02-05 13:59:56 -0800310}
311
Andreas Huber57a339c2012-12-03 11:18:00 -0800312void NuPlayer::setVideoSurfaceTextureAsync(
Andy McFadden8ba01022012-12-18 09:46:54 -0800313 const sp<IGraphicBufferProducer> &bufferProducer) {
Lajos Molnar1d15ab52015-03-04 16:46:34 -0800314 sp<AMessage> msg = new AMessage(kWhatSetVideoNativeWindow, this);
Andreas Huber57a339c2012-12-03 11:18:00 -0800315
Andy McFadden8ba01022012-12-18 09:46:54 -0800316 if (bufferProducer == NULL) {
Andreas Huber57a339c2012-12-03 11:18:00 -0800317 msg->setObject("native-window", NULL);
318 } else {
319 msg->setObject(
320 "native-window",
321 new NativeWindowWrapper(
Wei Jia9c03a402014-08-26 15:24:43 -0700322 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
Glenn Kasten11731182011-02-08 17:26:17 -0800613 case kWhatSetVideoNativeWindow:
Andreas Huberf9334412010-12-15 15:17:42 -0800614 {
Steve Block3856b092011-10-20 11:56:00 +0100615 ALOGV("kWhatSetVideoNativeWindow");
Andreas Huberf9334412010-12-15 15:17:42 -0800616
617 sp<RefBase> obj;
Glenn Kasten11731182011-02-08 17:26:17 -0800618 CHECK(msg->findObject("native-window", &obj));
Andreas Huberf9334412010-12-15 15:17:42 -0800619
Lajos Molnarf4849522014-12-10 16:58:57 -0800620 if (mSource == NULL || mSource->getFormat(false /* audio */) == NULL) {
Wei Jiafef808d2014-10-31 17:57:05 -0700621 performSetSurface(static_cast<NativeWindowWrapper *>(obj.get()));
622 break;
623 }
624
625 mDeferredActions.push_back(
626 new FlushDecoderAction(FLUSH_CMD_FLUSH /* audio */,
627 FLUSH_CMD_SHUTDOWN /* video */));
628
Andreas Huber57a339c2012-12-03 11:18:00 -0800629 mDeferredActions.push_back(
630 new SetSurfaceAction(
631 static_cast<NativeWindowWrapper *>(obj.get())));
James Dong0d268a32012-08-31 12:18:27 -0700632
Andreas Huber57a339c2012-12-03 11:18:00 -0800633 if (obj != NULL) {
Wei Jiafef808d2014-10-31 17:57:05 -0700634 if (mStarted) {
Andy Hung73535852014-09-05 11:42:58 -0700635 // Issue a seek to refresh the video screen only if started otherwise
636 // the extractor may not yet be started and will assert.
637 // If the video decoder is not set (perhaps audio only in this case)
638 // do not perform a seek as it is not needed.
Ronghua Wua73d9e02014-10-08 15:13:29 -0700639 int64_t currentPositionUs = 0;
640 if (getCurrentPosition(&currentPositionUs) == OK) {
641 mDeferredActions.push_back(
642 new SeekAction(currentPositionUs, false /* needNotify */));
643 }
Andy Hung73535852014-09-05 11:42:58 -0700644 }
Wei Jiaac428aa2014-09-02 19:01:34 -0700645
Andreas Huber57a339c2012-12-03 11:18:00 -0800646 // If there is a new surface texture, instantiate decoders
647 // again if possible.
648 mDeferredActions.push_back(
649 new SimpleAction(&NuPlayer::performScanSources));
650 }
651
Chong Zhangf8d71772014-11-26 15:08:34 -0800652 // After a flush without shutdown, decoder is paused.
653 // Don't resume it until source seek is done, otherwise it could
Chong Zhang7137ec72014-11-12 16:41:05 -0800654 // start pulling stale data too soon.
655 mDeferredActions.push_back(
Chong Zhangf8d71772014-11-26 15:08:34 -0800656 new ResumeDecoderAction(false /* needNotify */));
Chong Zhang7137ec72014-11-12 16:41:05 -0800657
Andreas Huber57a339c2012-12-03 11:18:00 -0800658 processDeferredActions();
Andreas Huberf9334412010-12-15 15:17:42 -0800659 break;
660 }
661
662 case kWhatSetAudioSink:
663 {
Steve Block3856b092011-10-20 11:56:00 +0100664 ALOGV("kWhatSetAudioSink");
Andreas Huberf9334412010-12-15 15:17:42 -0800665
666 sp<RefBase> obj;
667 CHECK(msg->findObject("sink", &obj));
668
669 mAudioSink = static_cast<MediaPlayerBase::AudioSink *>(obj.get());
670 break;
671 }
672
673 case kWhatStart:
674 {
Steve Block3856b092011-10-20 11:56:00 +0100675 ALOGV("kWhatStart");
Wei Jia94211742014-10-28 17:09:06 -0700676 if (mStarted) {
677 onResume();
678 } else {
679 onStart();
Lajos Molnar09524832014-07-17 14:29:51 -0700680 }
Chong Zhangefbb6192015-01-30 17:13:27 -0800681 mPausedByClient = false;
Andreas Huberf9334412010-12-15 15:17:42 -0800682 break;
683 }
684
Lajos Molnar3a474aa2015-04-24 17:10:07 -0700685 case kWhatConfigPlayback:
Wei Jia98160162015-02-04 17:01:11 -0800686 {
Lajos Molnar3a474aa2015-04-24 17:10:07 -0700687 sp<AReplyToken> replyID;
688 CHECK(msg->senderAwaitsResponse(&replyID));
689 AudioPlaybackRate rate /* sanitized */;
690 readFromAMessage(msg, &rate);
691 status_t err = OK;
Wei Jia98160162015-02-04 17:01:11 -0800692 if (mRenderer != NULL) {
Lajos Molnar3a474aa2015-04-24 17:10:07 -0700693 err = mRenderer->setPlaybackSettings(rate);
694 }
695 if (err == OK) {
696 if (rate.mSpeed == 0.f) {
697 onPause();
698 // save all other settings (using non-paused speed)
699 // so we can restore them on start
700 AudioPlaybackRate newRate = rate;
701 newRate.mSpeed = mPlaybackSettings.mSpeed;
702 mPlaybackSettings = newRate;
703 } else { /* rate.mSpeed != 0.f */
704 onResume();
705 mPlaybackSettings = rate;
706 }
Wei Jia98160162015-02-04 17:01:11 -0800707 }
Ronghua Wu8db88132015-04-22 13:51:35 -0700708
709 if (mVideoDecoder != NULL) {
710 sp<MetaData> meta = getFileMeta();
711 int32_t rate;
712 if (meta != NULL && meta->findInt32(kKeyFrameRate, &rate) && rate > 0) {
713 sp<AMessage> params = new AMessage();
Lajos Molnar3a474aa2015-04-24 17:10:07 -0700714 params->setFloat("operating-rate", rate * mPlaybackSettings.mSpeed);
Ronghua Wu8db88132015-04-22 13:51:35 -0700715 mVideoDecoder->setParameters(params);
716 }
717 }
718
Lajos Molnar3a474aa2015-04-24 17:10:07 -0700719 sp<AMessage> response = new AMessage;
720 response->setInt32("err", err);
721 response->postReply(replyID);
722 break;
723 }
724
725 case kWhatGetPlaybackSettings:
726 {
727 sp<AReplyToken> replyID;
728 CHECK(msg->senderAwaitsResponse(&replyID));
729 AudioPlaybackRate rate = mPlaybackSettings;
730 status_t err = OK;
731 if (mRenderer != NULL) {
732 err = mRenderer->getPlaybackSettings(&rate);
733 }
734 if (err == OK) {
735 // get playback settings used by renderer, as it may be
736 // slightly off due to audiosink not taking small changes.
737 mPlaybackSettings = rate;
738 if (mPaused) {
739 rate.mSpeed = 0.f;
740 }
741 }
742 sp<AMessage> response = new AMessage;
743 if (err == OK) {
744 writeToAMessage(response, rate);
745 }
746 response->setInt32("err", err);
747 response->postReply(replyID);
748 break;
749 }
750
751 case kWhatConfigSync:
752 {
753 sp<AReplyToken> replyID;
754 CHECK(msg->senderAwaitsResponse(&replyID));
755
756 ALOGV("kWhatConfigSync");
757 AVSyncSettings sync;
758 float videoFpsHint;
759 readFromAMessage(msg, &sync, &videoFpsHint);
760 status_t err = OK;
761 if (mRenderer != NULL) {
762 err = mRenderer->setSyncSettings(sync, videoFpsHint);
763 }
764 if (err == OK) {
765 mSyncSettings = sync;
766 mVideoFpsHint = videoFpsHint;
767 }
768 sp<AMessage> response = new AMessage;
769 response->setInt32("err", err);
770 response->postReply(replyID);
771 break;
772 }
773
774 case kWhatGetSyncSettings:
775 {
776 sp<AReplyToken> replyID;
777 CHECK(msg->senderAwaitsResponse(&replyID));
778 AVSyncSettings sync = mSyncSettings;
779 float videoFps = mVideoFpsHint;
780 status_t err = OK;
781 if (mRenderer != NULL) {
782 err = mRenderer->getSyncSettings(&sync, &videoFps);
783 if (err == OK) {
784 mSyncSettings = sync;
785 mVideoFpsHint = videoFps;
786 }
787 }
788 sp<AMessage> response = new AMessage;
789 if (err == OK) {
790 writeToAMessage(response, sync, videoFps);
791 }
792 response->setInt32("err", err);
793 response->postReply(replyID);
Wei Jia98160162015-02-04 17:01:11 -0800794 break;
795 }
796
Andreas Huberf9334412010-12-15 15:17:42 -0800797 case kWhatScanSources:
798 {
Andreas Huber1aef2112011-01-04 14:01:29 -0800799 int32_t generation;
800 CHECK(msg->findInt32("generation", &generation));
801 if (generation != mScanSourcesGeneration) {
802 // Drop obsolete msg.
803 break;
804 }
805
Andreas Huber5bc087c2010-12-23 10:27:40 -0800806 mScanSourcesPending = false;
807
Steve Block3856b092011-10-20 11:56:00 +0100808 ALOGV("scanning sources haveAudio=%d, haveVideo=%d",
Andreas Huber43c3e6c2011-01-05 12:17:08 -0800809 mAudioDecoder != NULL, mVideoDecoder != NULL);
810
Andreas Huberb7c8e912012-11-27 15:02:53 -0800811 bool mHadAnySourcesBefore =
812 (mAudioDecoder != NULL) || (mVideoDecoder != NULL);
813
Andy Hung282a7e32014-08-14 15:56:34 -0700814 // initialize video before audio because successful initialization of
815 // video may change deep buffer mode of audio.
Haynes Mathew George5d246ef2012-07-09 10:36:57 -0700816 if (mNativeWindow != NULL) {
817 instantiateDecoder(false, &mVideoDecoder);
818 }
Andreas Huberf9334412010-12-15 15:17:42 -0800819
Ronghua Wua10fd232014-11-06 16:15:20 -0800820 // Don't try to re-open audio sink if there's an existing decoder.
821 if (mAudioSink != NULL && mAudioDecoder == NULL) {
822 sp<MetaData> audioMeta = mSource->getFormatMeta(true /* audio */);
823 sp<AMessage> videoFormat = mSource->getFormat(false /* audio */);
824 audio_stream_type_t streamType = mAudioSink->getAudioStreamType();
Andy Hung202bce12014-12-03 11:47:36 -0800825 const bool hasVideo = (videoFormat != NULL);
826 const bool canOffload = canOffloadStream(
827 audioMeta, hasVideo, true /* is_streaming */, streamType);
Ronghua Wua10fd232014-11-06 16:15:20 -0800828 if (canOffload) {
829 if (!mOffloadAudio) {
830 mRenderer->signalEnableOffloadAudio();
831 }
Andy Hung282a7e32014-08-14 15:56:34 -0700832 // open audio sink early under offload mode.
833 sp<AMessage> format = mSource->getFormat(true /*audio*/);
Andy Hung202bce12014-12-03 11:47:36 -0800834 tryOpenAudioSinkForOffload(format, hasVideo);
Andy Hung282a7e32014-08-14 15:56:34 -0700835 }
Andreas Huber5bc087c2010-12-23 10:27:40 -0800836 instantiateDecoder(true, &mAudioDecoder);
Andreas Huberf9334412010-12-15 15:17:42 -0800837 }
838
Andreas Huberb7c8e912012-11-27 15:02:53 -0800839 if (!mHadAnySourcesBefore
840 && (mAudioDecoder != NULL || mVideoDecoder != NULL)) {
841 // This is the first time we've found anything playable.
842
Andreas Huber9575c962013-02-05 13:59:56 -0800843 if (mSourceFlags & Source::FLAG_DYNAMIC_DURATION) {
Andreas Huberb7c8e912012-11-27 15:02:53 -0800844 schedulePollDuration();
845 }
846 }
847
Andreas Hubereac68ba2011-09-27 12:12:25 -0700848 status_t err;
849 if ((err = mSource->feedMoreTSData()) != OK) {
Andreas Huber1aef2112011-01-04 14:01:29 -0800850 if (mAudioDecoder == NULL && mVideoDecoder == NULL) {
851 // We're not currently decoding anything (no audio or
852 // video tracks found) and we just ran out of input data.
Andreas Hubereac68ba2011-09-27 12:12:25 -0700853
854 if (err == ERROR_END_OF_STREAM) {
855 notifyListener(MEDIA_PLAYBACK_COMPLETE, 0, 0);
856 } else {
857 notifyListener(MEDIA_ERROR, MEDIA_ERROR_UNKNOWN, err);
858 }
Andreas Huber1aef2112011-01-04 14:01:29 -0800859 }
Andreas Huberf9334412010-12-15 15:17:42 -0800860 break;
861 }
862
Andreas Huberfbe9d812012-08-31 14:05:27 -0700863 if ((mAudioDecoder == NULL && mAudioSink != NULL)
864 || (mVideoDecoder == NULL && mNativeWindow != NULL)) {
Andreas Huberf9334412010-12-15 15:17:42 -0800865 msg->post(100000ll);
Andreas Huber5bc087c2010-12-23 10:27:40 -0800866 mScanSourcesPending = true;
Andreas Huberf9334412010-12-15 15:17:42 -0800867 }
868 break;
869 }
870
871 case kWhatVideoNotify:
872 case kWhatAudioNotify:
873 {
874 bool audio = msg->what() == kWhatAudioNotify;
875
Wei Jia88703c32014-08-06 11:24:07 -0700876 int32_t currentDecoderGeneration =
877 (audio? mAudioDecoderGeneration : mVideoDecoderGeneration);
878 int32_t requesterGeneration = currentDecoderGeneration - 1;
879 CHECK(msg->findInt32("generation", &requesterGeneration));
880
881 if (requesterGeneration != currentDecoderGeneration) {
882 ALOGV("got message from old %s decoder, generation(%d:%d)",
883 audio ? "audio" : "video", requesterGeneration,
884 currentDecoderGeneration);
885 sp<AMessage> reply;
886 if (!(msg->findMessage("reply", &reply))) {
887 return;
888 }
889
890 reply->setInt32("err", INFO_DISCONTINUITY);
891 reply->post();
892 return;
893 }
894
Andreas Huberf9334412010-12-15 15:17:42 -0800895 int32_t what;
Lajos Molnar1cd13982014-01-17 15:12:51 -0800896 CHECK(msg->findInt32("what", &what));
Andreas Huberf9334412010-12-15 15:17:42 -0800897
Chong Zhang7137ec72014-11-12 16:41:05 -0800898 if (what == DecoderBase::kWhatInputDiscontinuity) {
899 int32_t formatChange;
900 CHECK(msg->findInt32("formatChange", &formatChange));
Andreas Huberf9334412010-12-15 15:17:42 -0800901
Chong Zhang7137ec72014-11-12 16:41:05 -0800902 ALOGV("%s discontinuity: formatChange %d",
903 audio ? "audio" : "video", formatChange);
904
905 if (formatChange) {
906 mDeferredActions.push_back(
907 new FlushDecoderAction(
908 audio ? FLUSH_CMD_SHUTDOWN : FLUSH_CMD_NONE,
909 audio ? FLUSH_CMD_NONE : FLUSH_CMD_SHUTDOWN));
Andreas Huberf9334412010-12-15 15:17:42 -0800910 }
Chong Zhang7137ec72014-11-12 16:41:05 -0800911
912 mDeferredActions.push_back(
913 new SimpleAction(
914 &NuPlayer::performScanSources));
915
916 processDeferredActions();
917 } else if (what == DecoderBase::kWhatEOS) {
Andreas Huberdc9bacd2011-09-26 10:53:29 -0700918 int32_t err;
Lajos Molnar1cd13982014-01-17 15:12:51 -0800919 CHECK(msg->findInt32("err", &err));
Andreas Huberdc9bacd2011-09-26 10:53:29 -0700920
921 if (err == ERROR_END_OF_STREAM) {
Steve Block3856b092011-10-20 11:56:00 +0100922 ALOGV("got %s decoder EOS", audio ? "audio" : "video");
Andreas Huberdc9bacd2011-09-26 10:53:29 -0700923 } else {
Steve Block3856b092011-10-20 11:56:00 +0100924 ALOGV("got %s decoder EOS w/ error %d",
Andreas Huberdc9bacd2011-09-26 10:53:29 -0700925 audio ? "audio" : "video",
926 err);
927 }
928
929 mRenderer->queueEOS(audio, err);
Chong Zhang7137ec72014-11-12 16:41:05 -0800930 } else if (what == DecoderBase::kWhatFlushCompleted) {
Steve Block3856b092011-10-20 11:56:00 +0100931 ALOGV("decoder %s flush completed", audio ? "audio" : "video");
Andreas Huberf9334412010-12-15 15:17:42 -0800932
Andy Hung8d121d42014-10-03 09:53:53 -0700933 handleFlushComplete(audio, true /* isDecoder */);
Andreas Huber3831a062010-12-21 10:22:33 -0800934 finishFlushIfPossible();
Chong Zhang7137ec72014-11-12 16:41:05 -0800935 } else if (what == DecoderBase::kWhatVideoSizeChanged) {
Lajos Molnar1cd13982014-01-17 15:12:51 -0800936 sp<AMessage> format;
937 CHECK(msg->findMessage("format", &format));
938
Wei Jiac6cfd702014-11-11 16:33:20 -0800939 sp<AMessage> inputFormat =
940 mSource->getFormat(false /* audio */);
Andreas Huber3831a062010-12-21 10:22:33 -0800941
Wei Jiac6cfd702014-11-11 16:33:20 -0800942 updateVideoSize(inputFormat, format);
Chong Zhang7137ec72014-11-12 16:41:05 -0800943 } else if (what == DecoderBase::kWhatShutdownCompleted) {
Steve Block3856b092011-10-20 11:56:00 +0100944 ALOGV("%s shutdown completed", audio ? "audio" : "video");
Andreas Huber3831a062010-12-21 10:22:33 -0800945 if (audio) {
946 mAudioDecoder.clear();
Wei Jia57568df2014-09-22 10:16:29 -0700947 ++mAudioDecoderGeneration;
Andreas Huber3831a062010-12-21 10:22:33 -0800948
949 CHECK_EQ((int)mFlushingAudio, (int)SHUTTING_DOWN_DECODER);
950 mFlushingAudio = SHUT_DOWN;
951 } else {
952 mVideoDecoder.clear();
Wei Jia57568df2014-09-22 10:16:29 -0700953 ++mVideoDecoderGeneration;
Andreas Huber3831a062010-12-21 10:22:33 -0800954
955 CHECK_EQ((int)mFlushingVideo, (int)SHUTTING_DOWN_DECODER);
956 mFlushingVideo = SHUT_DOWN;
957 }
958
959 finishFlushIfPossible();
Chong Zhangf8d71772014-11-26 15:08:34 -0800960 } else if (what == DecoderBase::kWhatResumeCompleted) {
961 finishResume();
Chong Zhang7137ec72014-11-12 16:41:05 -0800962 } else if (what == DecoderBase::kWhatError) {
Chong Zhangf4c0a942014-08-11 15:14:10 -0700963 status_t err;
Andy Hung2abde2c2014-09-30 14:40:32 -0700964 if (!msg->findInt32("err", &err) || err == OK) {
Chong Zhangf4c0a942014-08-11 15:14:10 -0700965 err = UNKNOWN_ERROR;
966 }
Andy Hungcf31f1e2014-09-23 14:59:01 -0700967
Andy Hung2abde2c2014-09-30 14:40:32 -0700968 // Decoder errors can be due to Source (e.g. from streaming),
969 // or from decoding corrupted bitstreams, or from other decoder
970 // MediaCodec operations (e.g. from an ongoing reset or seek).
Andy Hung202bce12014-12-03 11:47:36 -0800971 // They may also be due to openAudioSink failure at
972 // decoder start or after a format change.
Andy Hung2abde2c2014-09-30 14:40:32 -0700973 //
974 // We try to gracefully shut down the affected decoder if possible,
975 // rather than trying to force the shutdown with something
976 // similar to performReset(). This method can lead to a hang
977 // if MediaCodec functions block after an error, but they should
978 // typically return INVALID_OPERATION instead of blocking.
979
980 FlushStatus *flushing = audio ? &mFlushingAudio : &mFlushingVideo;
981 ALOGE("received error(%#x) from %s decoder, flushing(%d), now shutting down",
982 err, audio ? "audio" : "video", *flushing);
983
984 switch (*flushing) {
985 case NONE:
986 mDeferredActions.push_back(
Wei Jiafef808d2014-10-31 17:57:05 -0700987 new FlushDecoderAction(
988 audio ? FLUSH_CMD_SHUTDOWN : FLUSH_CMD_NONE,
989 audio ? FLUSH_CMD_NONE : FLUSH_CMD_SHUTDOWN));
Andy Hung2abde2c2014-09-30 14:40:32 -0700990 processDeferredActions();
991 break;
992 case FLUSHING_DECODER:
993 *flushing = FLUSHING_DECODER_SHUTDOWN; // initiate shutdown after flush.
994 break; // Wait for flush to complete.
995 case FLUSHING_DECODER_SHUTDOWN:
996 break; // Wait for flush to complete.
997 case SHUTTING_DOWN_DECODER:
998 break; // Wait for shutdown to complete.
999 case FLUSHED:
1000 // Widevine source reads must stop before releasing the video decoder.
1001 if (!audio && mSource != NULL && mSourceFlags & Source::FLAG_SECURE) {
1002 mSource->stop();
1003 }
1004 getDecoder(audio)->initiateShutdown(); // In the middle of a seek.
1005 *flushing = SHUTTING_DOWN_DECODER; // Shut down.
1006 break;
1007 case SHUT_DOWN:
1008 finishFlushIfPossible(); // Should not occur.
1009 break; // Finish anyways.
Marco Nelissen9e2b7912014-08-18 16:13:03 -07001010 }
Andy Hung2abde2c2014-09-30 14:40:32 -07001011 notifyListener(MEDIA_ERROR, MEDIA_ERROR_UNKNOWN, err);
Lajos Molnar1cd13982014-01-17 15:12:51 -08001012 } else {
1013 ALOGV("Unhandled decoder notification %d '%c%c%c%c'.",
Andreas Hubera1f8ab02012-11-30 10:53:22 -08001014 what,
1015 what >> 24,
1016 (what >> 16) & 0xff,
1017 (what >> 8) & 0xff,
1018 what & 0xff);
Andreas Huberf9334412010-12-15 15:17:42 -08001019 }
1020
1021 break;
1022 }
1023
1024 case kWhatRendererNotify:
1025 {
Wei Jia57568df2014-09-22 10:16:29 -07001026 int32_t requesterGeneration = mRendererGeneration - 1;
1027 CHECK(msg->findInt32("generation", &requesterGeneration));
1028 if (requesterGeneration != mRendererGeneration) {
1029 ALOGV("got message from old renderer, generation(%d:%d)",
1030 requesterGeneration, mRendererGeneration);
1031 return;
1032 }
1033
Andreas Huberf9334412010-12-15 15:17:42 -08001034 int32_t what;
1035 CHECK(msg->findInt32("what", &what));
1036
1037 if (what == Renderer::kWhatEOS) {
1038 int32_t audio;
1039 CHECK(msg->findInt32("audio", &audio));
1040
Andreas Huberc92fd242011-08-16 13:48:44 -07001041 int32_t finalResult;
1042 CHECK(msg->findInt32("finalResult", &finalResult));
1043
Andreas Huberf9334412010-12-15 15:17:42 -08001044 if (audio) {
1045 mAudioEOS = true;
1046 } else {
1047 mVideoEOS = true;
1048 }
1049
Andreas Huberc92fd242011-08-16 13:48:44 -07001050 if (finalResult == ERROR_END_OF_STREAM) {
Steve Block3856b092011-10-20 11:56:00 +01001051 ALOGV("reached %s EOS", audio ? "audio" : "video");
Andreas Huberc92fd242011-08-16 13:48:44 -07001052 } else {
Steve Block29357bc2012-01-06 19:20:56 +00001053 ALOGE("%s track encountered an error (%d)",
Andreas Huberc92fd242011-08-16 13:48:44 -07001054 audio ? "audio" : "video", finalResult);
1055
1056 notifyListener(
1057 MEDIA_ERROR, MEDIA_ERROR_UNKNOWN, finalResult);
1058 }
Andreas Huberf9334412010-12-15 15:17:42 -08001059
1060 if ((mAudioEOS || mAudioDecoder == NULL)
1061 && (mVideoEOS || mVideoDecoder == NULL)) {
1062 notifyListener(MEDIA_PLAYBACK_COMPLETE, 0, 0);
1063 }
Andreas Huber3fe62152011-09-16 15:09:22 -07001064 } else if (what == Renderer::kWhatFlushComplete) {
Andreas Huberf9334412010-12-15 15:17:42 -08001065 int32_t audio;
1066 CHECK(msg->findInt32("audio", &audio));
1067
Steve Block3856b092011-10-20 11:56:00 +01001068 ALOGV("renderer %s flush completed.", audio ? "audio" : "video");
Andy Hung8d121d42014-10-03 09:53:53 -07001069 handleFlushComplete(audio, false /* isDecoder */);
1070 finishFlushIfPossible();
James Dongf57b4ea2012-07-20 13:38:36 -07001071 } else if (what == Renderer::kWhatVideoRenderingStart) {
1072 notifyListener(MEDIA_INFO, MEDIA_INFO_RENDERING_START, 0);
Lajos Molnarcbaffcf2013-08-14 18:30:38 -07001073 } else if (what == Renderer::kWhatMediaRenderingStart) {
1074 ALOGV("media rendering started");
1075 notifyListener(MEDIA_STARTED, 0, 0);
Wei Jia3a2956d2014-07-22 16:01:33 -07001076 } else if (what == Renderer::kWhatAudioOffloadTearDown) {
Ronghua Wua10fd232014-11-06 16:15:20 -08001077 ALOGV("Tear down audio offload, fall back to s/w path if due to error.");
Wei Jia3a2956d2014-07-22 16:01:33 -07001078 int64_t positionUs;
1079 CHECK(msg->findInt64("positionUs", &positionUs));
Ronghua Wu08529172014-10-02 16:55:52 -07001080 int32_t reason;
1081 CHECK(msg->findInt32("reason", &reason));
Andy Hung282a7e32014-08-14 15:56:34 -07001082 closeAudioSink();
Wei Jia3a2956d2014-07-22 16:01:33 -07001083 mAudioDecoder.clear();
Wei Jia57568df2014-09-22 10:16:29 -07001084 ++mAudioDecoderGeneration;
Chong Zhang7137ec72014-11-12 16:41:05 -08001085 mRenderer->flush(
1086 true /* audio */, false /* notifyComplete */);
Wei Jia3a2956d2014-07-22 16:01:33 -07001087 if (mVideoDecoder != NULL) {
Chong Zhang7137ec72014-11-12 16:41:05 -08001088 mRenderer->flush(
1089 false /* audio */, false /* notifyComplete */);
Wei Jia3a2956d2014-07-22 16:01:33 -07001090 }
Wei Jia3a2956d2014-07-22 16:01:33 -07001091
Wei Jiae427abf2014-09-22 15:21:11 -07001092 performSeek(positionUs, false /* needNotify */);
Ronghua Wu08529172014-10-02 16:55:52 -07001093 if (reason == Renderer::kDueToError) {
Ronghua Wua10fd232014-11-06 16:15:20 -08001094 mRenderer->signalDisableOffloadAudio();
1095 mOffloadAudio = false;
Ronghua Wu08529172014-10-02 16:55:52 -07001096 instantiateDecoder(true /* audio */, &mAudioDecoder);
1097 }
Andreas Huberf9334412010-12-15 15:17:42 -08001098 }
1099 break;
1100 }
1101
1102 case kWhatMoreDataQueued:
1103 {
1104 break;
1105 }
1106
Andreas Huber1aef2112011-01-04 14:01:29 -08001107 case kWhatReset:
1108 {
Steve Block3856b092011-10-20 11:56:00 +01001109 ALOGV("kWhatReset");
Andreas Huber1aef2112011-01-04 14:01:29 -08001110
Andreas Hubera1f8ab02012-11-30 10:53:22 -08001111 mDeferredActions.push_back(
Wei Jiafef808d2014-10-31 17:57:05 -07001112 new FlushDecoderAction(
1113 FLUSH_CMD_SHUTDOWN /* audio */,
1114 FLUSH_CMD_SHUTDOWN /* video */));
Andreas Huberb7c8e912012-11-27 15:02:53 -08001115
Andreas Hubera1f8ab02012-11-30 10:53:22 -08001116 mDeferredActions.push_back(
1117 new SimpleAction(&NuPlayer::performReset));
Andreas Huberb58ce9f2011-11-28 16:27:35 -08001118
Andreas Hubera1f8ab02012-11-30 10:53:22 -08001119 processDeferredActions();
Andreas Huber1aef2112011-01-04 14:01:29 -08001120 break;
1121 }
1122
Andreas Huber43c3e6c2011-01-05 12:17:08 -08001123 case kWhatSeek:
1124 {
1125 int64_t seekTimeUs;
Wei Jiae427abf2014-09-22 15:21:11 -07001126 int32_t needNotify;
Andreas Huber43c3e6c2011-01-05 12:17:08 -08001127 CHECK(msg->findInt64("seekTimeUs", &seekTimeUs));
Wei Jiae427abf2014-09-22 15:21:11 -07001128 CHECK(msg->findInt32("needNotify", &needNotify));
Andreas Huber43c3e6c2011-01-05 12:17:08 -08001129
Wei Jiae427abf2014-09-22 15:21:11 -07001130 ALOGV("kWhatSeek seekTimeUs=%lld us, needNotify=%d",
Lajos Molnar6d339f12015-04-17 16:15:53 -07001131 (long long)seekTimeUs, needNotify);
Andreas Huber43c3e6c2011-01-05 12:17:08 -08001132
Andreas Hubera1f8ab02012-11-30 10:53:22 -08001133 mDeferredActions.push_back(
Wei Jiafef808d2014-10-31 17:57:05 -07001134 new FlushDecoderAction(FLUSH_CMD_FLUSH /* audio */,
1135 FLUSH_CMD_FLUSH /* video */));
Andreas Huber43c3e6c2011-01-05 12:17:08 -08001136
Wei Jiae427abf2014-09-22 15:21:11 -07001137 mDeferredActions.push_back(
1138 new SeekAction(seekTimeUs, needNotify));
Andreas Huber43c3e6c2011-01-05 12:17:08 -08001139
Chong Zhangf8d71772014-11-26 15:08:34 -08001140 // After a flush without shutdown, decoder is paused.
1141 // Don't resume it until source seek is done, otherwise it could
Chong Zhang7137ec72014-11-12 16:41:05 -08001142 // start pulling stale data too soon.
1143 mDeferredActions.push_back(
Chong Zhangf8d71772014-11-26 15:08:34 -08001144 new ResumeDecoderAction(needNotify));
Chong Zhang7137ec72014-11-12 16:41:05 -08001145
Andreas Hubera1f8ab02012-11-30 10:53:22 -08001146 processDeferredActions();
Andreas Huber43c3e6c2011-01-05 12:17:08 -08001147 break;
1148 }
1149
Andreas Huberb4082222011-01-20 15:23:04 -08001150 case kWhatPause:
1151 {
Chong Zhangefbb6192015-01-30 17:13:27 -08001152 onPause();
1153 mPausedByClient = true;
Andreas Huberb4082222011-01-20 15:23:04 -08001154 break;
1155 }
1156
Andreas Huberb5f25f02013-02-05 10:14:26 -08001157 case kWhatSourceNotify:
1158 {
Andreas Huber9575c962013-02-05 13:59:56 -08001159 onSourceNotify(msg);
Andreas Huberb5f25f02013-02-05 10:14:26 -08001160 break;
1161 }
1162
Chong Zhanga7fa1d92014-06-11 14:49:23 -07001163 case kWhatClosedCaptionNotify:
1164 {
1165 onClosedCaptionNotify(msg);
1166 break;
1167 }
1168
Andreas Huberf9334412010-12-15 15:17:42 -08001169 default:
1170 TRESPASS();
1171 break;
1172 }
1173}
1174
Wei Jia94211742014-10-28 17:09:06 -07001175void NuPlayer::onResume() {
Chong Zhangefbb6192015-01-30 17:13:27 -08001176 if (!mPaused) {
1177 return;
1178 }
1179 mPaused = false;
Wei Jia94211742014-10-28 17:09:06 -07001180 if (mSource != NULL) {
1181 mSource->resume();
1182 } else {
1183 ALOGW("resume called when source is gone or not set");
1184 }
1185 // |mAudioDecoder| may have been released due to the pause timeout, so re-create it if
1186 // needed.
1187 if (audioDecoderStillNeeded() && mAudioDecoder == NULL) {
1188 instantiateDecoder(true /* audio */, &mAudioDecoder);
1189 }
1190 if (mRenderer != NULL) {
1191 mRenderer->resume();
1192 } else {
1193 ALOGW("resume called when renderer is gone or not set");
1194 }
1195}
1196
Lajos Molnarfcd3e942015-03-31 10:06:48 -07001197status_t NuPlayer::onInstantiateSecureDecoders() {
1198 status_t err;
1199 if (!(mSourceFlags & Source::FLAG_SECURE)) {
1200 return BAD_TYPE;
1201 }
1202
1203 if (mRenderer != NULL) {
1204 ALOGE("renderer should not be set when instantiating secure decoders");
1205 return UNKNOWN_ERROR;
1206 }
1207
1208 // TRICKY: We rely on mRenderer being null, so that decoder does not start requesting
1209 // data on instantiation.
1210 if (mNativeWindow != NULL) {
1211 err = instantiateDecoder(false, &mVideoDecoder);
1212 if (err != OK) {
1213 return err;
1214 }
1215 }
1216
1217 if (mAudioSink != NULL) {
1218 err = instantiateDecoder(true, &mAudioDecoder);
1219 if (err != OK) {
1220 return err;
1221 }
1222 }
1223 return OK;
1224}
1225
Wei Jia94211742014-10-28 17:09:06 -07001226void NuPlayer::onStart() {
Wei Jia94211742014-10-28 17:09:06 -07001227 mOffloadAudio = false;
1228 mAudioEOS = false;
1229 mVideoEOS = false;
Wei Jia94211742014-10-28 17:09:06 -07001230 mStarted = true;
1231
Wei Jia94211742014-10-28 17:09:06 -07001232 mSource->start();
1233
1234 uint32_t flags = 0;
1235
1236 if (mSource->isRealTime()) {
1237 flags |= Renderer::FLAG_REAL_TIME;
1238 }
1239
1240 sp<MetaData> audioMeta = mSource->getFormatMeta(true /* audio */);
1241 audio_stream_type_t streamType = AUDIO_STREAM_MUSIC;
1242 if (mAudioSink != NULL) {
1243 streamType = mAudioSink->getAudioStreamType();
1244 }
1245
1246 sp<AMessage> videoFormat = mSource->getFormat(false /* audio */);
1247
1248 mOffloadAudio =
1249 canOffloadStream(audioMeta, (videoFormat != NULL),
1250 true /* is_streaming */, streamType);
1251 if (mOffloadAudio) {
1252 flags |= Renderer::FLAG_OFFLOAD_AUDIO;
1253 }
1254
Lajos Molnar1d15ab52015-03-04 16:46:34 -08001255 sp<AMessage> notify = new AMessage(kWhatRendererNotify, this);
Wei Jia94211742014-10-28 17:09:06 -07001256 ++mRendererGeneration;
1257 notify->setInt32("generation", mRendererGeneration);
1258 mRenderer = new Renderer(mAudioSink, notify, flags);
Wei Jia94211742014-10-28 17:09:06 -07001259 mRendererLooper = new ALooper;
1260 mRendererLooper->setName("NuPlayerRenderer");
1261 mRendererLooper->start(false, false, ANDROID_PRIORITY_AUDIO);
1262 mRendererLooper->registerHandler(mRenderer);
Lajos Molnar3a474aa2015-04-24 17:10:07 -07001263
1264 status_t err = mRenderer->setPlaybackSettings(mPlaybackSettings);
1265 if (err != OK) {
1266 mSource->stop();
1267 notifyListener(MEDIA_ERROR, MEDIA_ERROR_UNKNOWN, err);
1268 return;
Wei Jiac8206ff2015-03-04 13:59:37 -08001269 }
Wei Jia94211742014-10-28 17:09:06 -07001270
1271 sp<MetaData> meta = getFileMeta();
1272 int32_t rate;
1273 if (meta != NULL
1274 && meta->findInt32(kKeyFrameRate, &rate) && rate > 0) {
1275 mRenderer->setVideoFrameRate(rate);
1276 }
1277
Wei Jiac6cfd702014-11-11 16:33:20 -08001278 if (mVideoDecoder != NULL) {
1279 mVideoDecoder->setRenderer(mRenderer);
1280 }
1281 if (mAudioDecoder != NULL) {
1282 mAudioDecoder->setRenderer(mRenderer);
1283 }
1284
Wei Jia94211742014-10-28 17:09:06 -07001285 postScanSources();
1286}
1287
Chong Zhangefbb6192015-01-30 17:13:27 -08001288void NuPlayer::onPause() {
1289 if (mPaused) {
1290 return;
1291 }
1292 mPaused = true;
1293 if (mSource != NULL) {
1294 mSource->pause();
1295 } else {
1296 ALOGW("pause called when source is gone or not set");
1297 }
1298 if (mRenderer != NULL) {
1299 mRenderer->pause();
1300 } else {
1301 ALOGW("pause called when renderer is gone or not set");
1302 }
1303}
1304
Ronghua Wud7988b12014-10-03 15:19:10 -07001305bool NuPlayer::audioDecoderStillNeeded() {
1306 // Audio decoder is no longer needed if it's in shut/shutting down status.
1307 return ((mFlushingAudio != SHUT_DOWN) && (mFlushingAudio != SHUTTING_DOWN_DECODER));
1308}
1309
Andy Hung8d121d42014-10-03 09:53:53 -07001310void NuPlayer::handleFlushComplete(bool audio, bool isDecoder) {
1311 // We wait for both the decoder flush and the renderer flush to complete
1312 // before entering either the FLUSHED or the SHUTTING_DOWN_DECODER state.
1313
1314 mFlushComplete[audio][isDecoder] = true;
1315 if (!mFlushComplete[audio][!isDecoder]) {
1316 return;
1317 }
1318
1319 FlushStatus *state = audio ? &mFlushingAudio : &mFlushingVideo;
1320 switch (*state) {
1321 case FLUSHING_DECODER:
1322 {
1323 *state = FLUSHED;
Andy Hung8d121d42014-10-03 09:53:53 -07001324 break;
1325 }
1326
1327 case FLUSHING_DECODER_SHUTDOWN:
1328 {
1329 *state = SHUTTING_DOWN_DECODER;
1330
1331 ALOGV("initiating %s decoder shutdown", audio ? "audio" : "video");
1332 if (!audio) {
Andy Hung8d121d42014-10-03 09:53:53 -07001333 // Widevine source reads must stop before releasing the video decoder.
1334 if (mSource != NULL && mSourceFlags & Source::FLAG_SECURE) {
1335 mSource->stop();
1336 }
1337 }
1338 getDecoder(audio)->initiateShutdown();
1339 break;
1340 }
1341
1342 default:
1343 // decoder flush completes only occur in a flushing state.
1344 LOG_ALWAYS_FATAL_IF(isDecoder, "decoder flush in invalid state %d", *state);
1345 break;
1346 }
1347}
1348
Andreas Huber3831a062010-12-21 10:22:33 -08001349void NuPlayer::finishFlushIfPossible() {
Wei Jia53904f32014-07-29 10:22:53 -07001350 if (mFlushingAudio != NONE && mFlushingAudio != FLUSHED
1351 && mFlushingAudio != SHUT_DOWN) {
Andreas Huber3831a062010-12-21 10:22:33 -08001352 return;
1353 }
1354
Wei Jia53904f32014-07-29 10:22:53 -07001355 if (mFlushingVideo != NONE && mFlushingVideo != FLUSHED
1356 && mFlushingVideo != SHUT_DOWN) {
Andreas Huber3831a062010-12-21 10:22:33 -08001357 return;
1358 }
1359
Steve Block3856b092011-10-20 11:56:00 +01001360 ALOGV("both audio and video are flushed now.");
Andreas Huber3831a062010-12-21 10:22:33 -08001361
Andreas Huber3831a062010-12-21 10:22:33 -08001362 mFlushingAudio = NONE;
1363 mFlushingVideo = NONE;
Andreas Huber3831a062010-12-21 10:22:33 -08001364
Andy Hung8d121d42014-10-03 09:53:53 -07001365 clearFlushComplete();
1366
Andreas Hubera1f8ab02012-11-30 10:53:22 -08001367 processDeferredActions();
Andreas Huber1aef2112011-01-04 14:01:29 -08001368}
1369
1370void NuPlayer::postScanSources() {
1371 if (mScanSourcesPending) {
1372 return;
1373 }
1374
Lajos Molnar1d15ab52015-03-04 16:46:34 -08001375 sp<AMessage> msg = new AMessage(kWhatScanSources, this);
Andreas Huber1aef2112011-01-04 14:01:29 -08001376 msg->setInt32("generation", mScanSourcesGeneration);
1377 msg->post();
1378
1379 mScanSourcesPending = true;
1380}
1381
Andy Hung202bce12014-12-03 11:47:36 -08001382void NuPlayer::tryOpenAudioSinkForOffload(const sp<AMessage> &format, bool hasVideo) {
1383 // Note: This is called early in NuPlayer to determine whether offloading
1384 // is possible; otherwise the decoders call the renderer openAudioSink directly.
Andy Hung282a7e32014-08-14 15:56:34 -07001385
Andy Hung202bce12014-12-03 11:47:36 -08001386 status_t err = mRenderer->openAudioSink(
1387 format, true /* offloadOnly */, hasVideo, AUDIO_OUTPUT_FLAG_NONE, &mOffloadAudio);
1388 if (err != OK) {
1389 // Any failure we turn off mOffloadAudio.
1390 mOffloadAudio = false;
1391 } else if (mOffloadAudio) {
Chong Zhang3b9eb1f2014-10-15 17:05:08 -07001392 sp<MetaData> audioMeta =
1393 mSource->getFormatMeta(true /* audio */);
1394 sendMetaDataToHal(mAudioSink, audioMeta);
Andy Hung282a7e32014-08-14 15:56:34 -07001395 }
1396}
1397
1398void NuPlayer::closeAudioSink() {
Chong Zhang3b9eb1f2014-10-15 17:05:08 -07001399 mRenderer->closeAudioSink();
Andy Hung282a7e32014-08-14 15:56:34 -07001400}
1401
Chong Zhang7137ec72014-11-12 16:41:05 -08001402status_t NuPlayer::instantiateDecoder(bool audio, sp<DecoderBase> *decoder) {
Andreas Huberf9334412010-12-15 15:17:42 -08001403 if (*decoder != NULL) {
1404 return OK;
1405 }
1406
Andreas Huber84066782011-08-16 09:34:26 -07001407 sp<AMessage> format = mSource->getFormat(audio);
Andreas Huberf9334412010-12-15 15:17:42 -08001408
Andreas Huber84066782011-08-16 09:34:26 -07001409 if (format == NULL) {
Andreas Huberf9334412010-12-15 15:17:42 -08001410 return -EWOULDBLOCK;
1411 }
1412
Ronghua Wu8db88132015-04-22 13:51:35 -07001413 format->setInt32("priority", 0 /* realtime */);
1414
Andreas Huber3fe62152011-09-16 15:09:22 -07001415 if (!audio) {
Andreas Huber84066782011-08-16 09:34:26 -07001416 AString mime;
1417 CHECK(format->findString("mime", &mime));
Chong Zhanga7fa1d92014-06-11 14:49:23 -07001418
Lajos Molnar1d15ab52015-03-04 16:46:34 -08001419 sp<AMessage> ccNotify = new AMessage(kWhatClosedCaptionNotify, this);
Chong Zhang341ab6e2015-02-04 13:37:18 -08001420 if (mCCDecoder == NULL) {
1421 mCCDecoder = new CCDecoder(ccNotify);
1422 }
Lajos Molnar09524832014-07-17 14:29:51 -07001423
1424 if (mSourceFlags & Source::FLAG_SECURE) {
1425 format->setInt32("secure", true);
1426 }
Chong Zhang17134602015-01-07 16:14:34 -08001427
1428 if (mSourceFlags & Source::FLAG_PROTECTED) {
1429 format->setInt32("protected", true);
1430 }
Ronghua Wu8db88132015-04-22 13:51:35 -07001431
1432 sp<MetaData> meta = getFileMeta();
1433 int32_t rate;
1434 if (meta != NULL && meta->findInt32(kKeyFrameRate, &rate) && rate > 0) {
Lajos Molnar3a474aa2015-04-24 17:10:07 -07001435 format->setFloat("operating-rate", rate * mPlaybackSettings.mSpeed);
Ronghua Wu8db88132015-04-22 13:51:35 -07001436 }
Andreas Huber3fe62152011-09-16 15:09:22 -07001437 }
1438
Wei Jiabc2fb722014-07-08 16:37:57 -07001439 if (audio) {
Lajos Molnar1d15ab52015-03-04 16:46:34 -08001440 sp<AMessage> notify = new AMessage(kWhatAudioNotify, this);
Wei Jia88703c32014-08-06 11:24:07 -07001441 ++mAudioDecoderGeneration;
1442 notify->setInt32("generation", mAudioDecoderGeneration);
1443
Wei Jiabc2fb722014-07-08 16:37:57 -07001444 if (mOffloadAudio) {
Haynes Mathew George8b635332015-03-30 17:59:47 -07001445 const bool hasVideo = (mSource->getFormat(false /*audio */) != NULL);
1446 format->setInt32("has-video", hasVideo);
Wei Jiac6cfd702014-11-11 16:33:20 -08001447 *decoder = new DecoderPassThrough(notify, mSource, mRenderer);
Wei Jiabc2fb722014-07-08 16:37:57 -07001448 } else {
Wei Jiac6cfd702014-11-11 16:33:20 -08001449 *decoder = new Decoder(notify, mSource, mRenderer);
Wei Jiabc2fb722014-07-08 16:37:57 -07001450 }
1451 } else {
Lajos Molnar1d15ab52015-03-04 16:46:34 -08001452 sp<AMessage> notify = new AMessage(kWhatVideoNotify, this);
Wei Jia88703c32014-08-06 11:24:07 -07001453 ++mVideoDecoderGeneration;
1454 notify->setInt32("generation", mVideoDecoderGeneration);
1455
Chong Zhang7137ec72014-11-12 16:41:05 -08001456 *decoder = new Decoder(
1457 notify, mSource, mRenderer, mNativeWindow, mCCDecoder);
Lajos Molnard9fd6312014-11-06 11:00:00 -08001458
1459 // enable FRC if high-quality AV sync is requested, even if not
1460 // queuing to native window, as this will even improve textureview
1461 // playback.
1462 {
1463 char value[PROPERTY_VALUE_MAX];
1464 if (property_get("persist.sys.media.avsync", value, NULL) &&
1465 (!strcmp("1", value) || !strcasecmp("true", value))) {
1466 format->setInt32("auto-frc", 1);
1467 }
1468 }
Wei Jiabc2fb722014-07-08 16:37:57 -07001469 }
Lajos Molnar1cd13982014-01-17 15:12:51 -08001470 (*decoder)->init();
Andreas Huber84066782011-08-16 09:34:26 -07001471 (*decoder)->configure(format);
Andreas Huberf9334412010-12-15 15:17:42 -08001472
Lajos Molnar09524832014-07-17 14:29:51 -07001473 // allocate buffers to decrypt widevine source buffers
1474 if (!audio && (mSourceFlags & Source::FLAG_SECURE)) {
1475 Vector<sp<ABuffer> > inputBufs;
1476 CHECK_EQ((*decoder)->getInputBuffers(&inputBufs), (status_t)OK);
1477
1478 Vector<MediaBuffer *> mediaBufs;
1479 for (size_t i = 0; i < inputBufs.size(); i++) {
1480 const sp<ABuffer> &buffer = inputBufs[i];
1481 MediaBuffer *mbuf = new MediaBuffer(buffer->data(), buffer->size());
1482 mediaBufs.push(mbuf);
1483 }
1484
1485 status_t err = mSource->setBuffers(audio, mediaBufs);
1486 if (err != OK) {
1487 for (size_t i = 0; i < mediaBufs.size(); ++i) {
1488 mediaBufs[i]->release();
1489 }
1490 mediaBufs.clear();
1491 ALOGE("Secure source didn't support secure mediaBufs.");
1492 return err;
1493 }
1494 }
Andreas Huberf9334412010-12-15 15:17:42 -08001495 return OK;
1496}
1497
Chong Zhangced1c2f2014-08-08 15:22:35 -07001498void NuPlayer::updateVideoSize(
1499 const sp<AMessage> &inputFormat,
1500 const sp<AMessage> &outputFormat) {
1501 if (inputFormat == NULL) {
1502 ALOGW("Unknown video size, reporting 0x0!");
1503 notifyListener(MEDIA_SET_VIDEO_SIZE, 0, 0);
1504 return;
1505 }
1506
1507 int32_t displayWidth, displayHeight;
Chong Zhangced1c2f2014-08-08 15:22:35 -07001508 if (outputFormat != NULL) {
1509 int32_t width, height;
1510 CHECK(outputFormat->findInt32("width", &width));
1511 CHECK(outputFormat->findInt32("height", &height));
1512
1513 int32_t cropLeft, cropTop, cropRight, cropBottom;
1514 CHECK(outputFormat->findRect(
1515 "crop",
1516 &cropLeft, &cropTop, &cropRight, &cropBottom));
1517
1518 displayWidth = cropRight - cropLeft + 1;
1519 displayHeight = cropBottom - cropTop + 1;
1520
1521 ALOGV("Video output format changed to %d x %d "
1522 "(crop: %d x %d @ (%d, %d))",
1523 width, height,
1524 displayWidth,
1525 displayHeight,
1526 cropLeft, cropTop);
1527 } else {
1528 CHECK(inputFormat->findInt32("width", &displayWidth));
1529 CHECK(inputFormat->findInt32("height", &displayHeight));
1530
1531 ALOGV("Video input format %d x %d", displayWidth, displayHeight);
1532 }
1533
1534 // Take into account sample aspect ratio if necessary:
1535 int32_t sarWidth, sarHeight;
1536 if (inputFormat->findInt32("sar-width", &sarWidth)
1537 && inputFormat->findInt32("sar-height", &sarHeight)) {
1538 ALOGV("Sample aspect ratio %d : %d", sarWidth, sarHeight);
1539
1540 displayWidth = (displayWidth * sarWidth) / sarHeight;
1541
1542 ALOGV("display dimensions %d x %d", displayWidth, displayHeight);
1543 }
1544
1545 int32_t rotationDegrees;
1546 if (!inputFormat->findInt32("rotation-degrees", &rotationDegrees)) {
1547 rotationDegrees = 0;
1548 }
1549
1550 if (rotationDegrees == 90 || rotationDegrees == 270) {
1551 int32_t tmp = displayWidth;
1552 displayWidth = displayHeight;
1553 displayHeight = tmp;
1554 }
1555
1556 notifyListener(
1557 MEDIA_SET_VIDEO_SIZE,
1558 displayWidth,
1559 displayHeight);
1560}
1561
Chong Zhangdcb89b32013-08-06 09:44:47 -07001562void NuPlayer::notifyListener(int msg, int ext1, int ext2, const Parcel *in) {
Andreas Huber43c3e6c2011-01-05 12:17:08 -08001563 if (mDriver == NULL) {
Andreas Huberf9334412010-12-15 15:17:42 -08001564 return;
1565 }
1566
Andreas Huber43c3e6c2011-01-05 12:17:08 -08001567 sp<NuPlayerDriver> driver = mDriver.promote();
Andreas Huberf9334412010-12-15 15:17:42 -08001568
Andreas Huber43c3e6c2011-01-05 12:17:08 -08001569 if (driver == NULL) {
Andreas Huberf9334412010-12-15 15:17:42 -08001570 return;
1571 }
1572
Chong Zhangdcb89b32013-08-06 09:44:47 -07001573 driver->notifyListener(msg, ext1, ext2, in);
Andreas Huberf9334412010-12-15 15:17:42 -08001574}
1575
Chong Zhang7137ec72014-11-12 16:41:05 -08001576void NuPlayer::flushDecoder(bool audio, bool needShutdown) {
Andreas Huber14f76722013-01-15 09:04:18 -08001577 ALOGV("[%s] flushDecoder needShutdown=%d",
1578 audio ? "audio" : "video", needShutdown);
1579
Chong Zhang7137ec72014-11-12 16:41:05 -08001580 const sp<DecoderBase> &decoder = getDecoder(audio);
Lajos Molnar87603c02014-08-20 19:25:30 -07001581 if (decoder == NULL) {
Steve Blockdf64d152012-01-04 20:05:49 +00001582 ALOGI("flushDecoder %s without decoder present",
Andreas Huber6e3d3112011-11-28 12:36:11 -08001583 audio ? "audio" : "video");
Lajos Molnar87603c02014-08-20 19:25:30 -07001584 return;
Andreas Huber6e3d3112011-11-28 12:36:11 -08001585 }
1586
Andreas Huber1aef2112011-01-04 14:01:29 -08001587 // Make sure we don't continue to scan sources until we finish flushing.
1588 ++mScanSourcesGeneration;
Chong Zhang3b032b32015-04-17 15:49:06 -07001589 if (mScanSourcesPending) {
1590 mDeferredActions.push_back(
1591 new SimpleAction(&NuPlayer::performScanSources));
1592 mScanSourcesPending = false;
1593 }
Andreas Huber1aef2112011-01-04 14:01:29 -08001594
Chong Zhang7137ec72014-11-12 16:41:05 -08001595 decoder->signalFlush();
Andreas Huber1aef2112011-01-04 14:01:29 -08001596
1597 FlushStatus newStatus =
1598 needShutdown ? FLUSHING_DECODER_SHUTDOWN : FLUSHING_DECODER;
1599
Lajos Molnarfcd3e942015-03-31 10:06:48 -07001600 mFlushComplete[audio][false /* isDecoder */] = (mRenderer == NULL);
Andy Hung8d121d42014-10-03 09:53:53 -07001601 mFlushComplete[audio][true /* isDecoder */] = false;
Andreas Huber1aef2112011-01-04 14:01:29 -08001602 if (audio) {
Wei Jia53904f32014-07-29 10:22:53 -07001603 ALOGE_IF(mFlushingAudio != NONE,
1604 "audio flushDecoder() is called in state %d", mFlushingAudio);
Andreas Huber1aef2112011-01-04 14:01:29 -08001605 mFlushingAudio = newStatus;
Andreas Huber1aef2112011-01-04 14:01:29 -08001606 } else {
Wei Jia53904f32014-07-29 10:22:53 -07001607 ALOGE_IF(mFlushingVideo != NONE,
1608 "video flushDecoder() is called in state %d", mFlushingVideo);
Andreas Huber1aef2112011-01-04 14:01:29 -08001609 mFlushingVideo = newStatus;
Andreas Huber1aef2112011-01-04 14:01:29 -08001610 }
1611}
1612
Chong Zhangced1c2f2014-08-08 15:22:35 -07001613void NuPlayer::queueDecoderShutdown(
1614 bool audio, bool video, const sp<AMessage> &reply) {
1615 ALOGI("queueDecoderShutdown audio=%d, video=%d", audio, video);
Andreas Huber84066782011-08-16 09:34:26 -07001616
Chong Zhangced1c2f2014-08-08 15:22:35 -07001617 mDeferredActions.push_back(
Wei Jiafef808d2014-10-31 17:57:05 -07001618 new FlushDecoderAction(
1619 audio ? FLUSH_CMD_SHUTDOWN : FLUSH_CMD_NONE,
1620 video ? FLUSH_CMD_SHUTDOWN : FLUSH_CMD_NONE));
Andreas Huber84066782011-08-16 09:34:26 -07001621
Chong Zhangced1c2f2014-08-08 15:22:35 -07001622 mDeferredActions.push_back(
1623 new SimpleAction(&NuPlayer::performScanSources));
Andreas Huber84066782011-08-16 09:34:26 -07001624
Chong Zhangced1c2f2014-08-08 15:22:35 -07001625 mDeferredActions.push_back(new PostMessageAction(reply));
1626
1627 processDeferredActions();
Andreas Huber84066782011-08-16 09:34:26 -07001628}
1629
James Dong0d268a32012-08-31 12:18:27 -07001630status_t NuPlayer::setVideoScalingMode(int32_t mode) {
1631 mVideoScalingMode = mode;
Andreas Huber57a339c2012-12-03 11:18:00 -08001632 if (mNativeWindow != NULL) {
James Dong0d268a32012-08-31 12:18:27 -07001633 status_t ret = native_window_set_scaling_mode(
1634 mNativeWindow->getNativeWindow().get(), mVideoScalingMode);
1635 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
Andreas Huber57a339c2012-12-03 11:18:00 -08001828void NuPlayer::performSetSurface(const sp<NativeWindowWrapper> &wrapper) {
1829 ALOGV("performSetSurface");
1830
1831 mNativeWindow = wrapper;
1832
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
1984 if (mStarted && !mPausedByClient) {
1985 ALOGI("buffer low, pausing...");
1986
1987 onPause();
1988 }
1989 // fall-thru
1990 }
1991
Roger Jönssonb50e83e2013-01-21 16:26:41 +01001992 case Source::kWhatBufferingStart:
1993 {
1994 notifyListener(MEDIA_INFO, MEDIA_INFO_BUFFERING_START, 0);
1995 break;
1996 }
1997
Chong Zhangefbb6192015-01-30 17:13:27 -08001998 case Source::kWhatResumeOnBufferingEnd:
1999 {
2000 // ignore if not playing
2001 if (mStarted && !mPausedByClient) {
2002 ALOGI("buffer ready, resuming...");
2003
2004 onResume();
2005 }
2006 // fall-thru
2007 }
2008
Roger Jönssonb50e83e2013-01-21 16:26:41 +01002009 case Source::kWhatBufferingEnd:
2010 {
2011 notifyListener(MEDIA_INFO, MEDIA_INFO_BUFFERING_END, 0);
2012 break;
2013 }
2014
Chong Zhangefbb6192015-01-30 17:13:27 -08002015 case Source::kWhatCacheStats:
2016 {
2017 int32_t kbps;
2018 CHECK(msg->findInt32("bandwidth", &kbps));
2019
2020 notifyListener(MEDIA_INFO, MEDIA_INFO_NETWORK_BANDWIDTH, kbps);
2021 break;
2022 }
2023
Chong Zhangdcb89b32013-08-06 09:44:47 -07002024 case Source::kWhatSubtitleData:
2025 {
2026 sp<ABuffer> buffer;
2027 CHECK(msg->findBuffer("buffer", &buffer));
2028
Chong Zhang404fced2014-06-11 14:45:31 -07002029 sendSubtitleData(buffer, 0 /* baseIndex */);
Chong Zhangdcb89b32013-08-06 09:44:47 -07002030 break;
2031 }
2032
Robert Shih08528432015-04-08 09:06:54 -07002033 case Source::kWhatTimedMetaData:
2034 {
2035 sp<ABuffer> buffer;
2036 if (!msg->findBuffer("buffer", &buffer)) {
2037 notifyListener(MEDIA_INFO, MEDIA_INFO_METADATA_UPDATE, 0);
2038 } else {
2039 sendTimedMetaData(buffer);
2040 }
2041 break;
2042 }
2043
Robert Shihd3b0bbb2014-07-23 15:00:25 -07002044 case Source::kWhatTimedTextData:
2045 {
2046 int32_t generation;
2047 if (msg->findInt32("generation", &generation)
2048 && generation != mTimedTextGeneration) {
2049 break;
2050 }
2051
2052 sp<ABuffer> buffer;
2053 CHECK(msg->findBuffer("buffer", &buffer));
2054
2055 sp<NuPlayerDriver> driver = mDriver.promote();
2056 if (driver == NULL) {
2057 break;
2058 }
2059
2060 int posMs;
2061 int64_t timeUs, posUs;
2062 driver->getCurrentPosition(&posMs);
2063 posUs = posMs * 1000;
2064 CHECK(buffer->meta()->findInt64("timeUs", &timeUs));
2065
2066 if (posUs < timeUs) {
2067 if (!msg->findInt32("generation", &generation)) {
2068 msg->setInt32("generation", mTimedTextGeneration);
2069 }
2070 msg->post(timeUs - posUs);
2071 } else {
2072 sendTimedTextData(buffer);
2073 }
2074 break;
2075 }
2076
Andreas Huber14f76722013-01-15 09:04:18 -08002077 case Source::kWhatQueueDecoderShutdown:
2078 {
2079 int32_t audio, video;
2080 CHECK(msg->findInt32("audio", &audio));
2081 CHECK(msg->findInt32("video", &video));
2082
2083 sp<AMessage> reply;
2084 CHECK(msg->findMessage("reply", &reply));
2085
2086 queueDecoderShutdown(audio, video, reply);
2087 break;
2088 }
2089
Ronghua Wu80276872014-08-28 15:50:29 -07002090 case Source::kWhatDrmNoLicense:
2091 {
2092 notifyListener(MEDIA_ERROR, MEDIA_ERROR_UNKNOWN, ERROR_DRM_NO_LICENSE);
2093 break;
2094 }
2095
Andreas Huber9575c962013-02-05 13:59:56 -08002096 default:
2097 TRESPASS();
2098 }
2099}
2100
Chong Zhanga7fa1d92014-06-11 14:49:23 -07002101void NuPlayer::onClosedCaptionNotify(const sp<AMessage> &msg) {
2102 int32_t what;
2103 CHECK(msg->findInt32("what", &what));
2104
2105 switch (what) {
2106 case NuPlayer::CCDecoder::kWhatClosedCaptionData:
2107 {
2108 sp<ABuffer> buffer;
2109 CHECK(msg->findBuffer("buffer", &buffer));
2110
2111 size_t inbandTracks = 0;
2112 if (mSource != NULL) {
2113 inbandTracks = mSource->getTrackCount();
2114 }
2115
2116 sendSubtitleData(buffer, inbandTracks);
2117 break;
2118 }
2119
2120 case NuPlayer::CCDecoder::kWhatTrackAdded:
2121 {
2122 notifyListener(MEDIA_INFO, MEDIA_INFO_METADATA_UPDATE, 0);
2123
2124 break;
2125 }
2126
2127 default:
2128 TRESPASS();
2129 }
2130
2131
2132}
2133
Chong Zhang404fced2014-06-11 14:45:31 -07002134void NuPlayer::sendSubtitleData(const sp<ABuffer> &buffer, int32_t baseIndex) {
2135 int32_t trackIndex;
2136 int64_t timeUs, durationUs;
2137 CHECK(buffer->meta()->findInt32("trackIndex", &trackIndex));
2138 CHECK(buffer->meta()->findInt64("timeUs", &timeUs));
2139 CHECK(buffer->meta()->findInt64("durationUs", &durationUs));
2140
2141 Parcel in;
2142 in.writeInt32(trackIndex + baseIndex);
2143 in.writeInt64(timeUs);
2144 in.writeInt64(durationUs);
2145 in.writeInt32(buffer->size());
2146 in.writeInt32(buffer->size());
2147 in.write(buffer->data(), buffer->size());
2148
2149 notifyListener(MEDIA_SUBTITLE_DATA, 0, 0, &in);
2150}
Robert Shihd3b0bbb2014-07-23 15:00:25 -07002151
Robert Shih08528432015-04-08 09:06:54 -07002152void NuPlayer::sendTimedMetaData(const sp<ABuffer> &buffer) {
2153 int64_t timeUs;
2154 CHECK(buffer->meta()->findInt64("timeUs", &timeUs));
2155
2156 Parcel in;
2157 in.writeInt64(timeUs);
2158 in.writeInt32(buffer->size());
2159 in.writeInt32(buffer->size());
2160 in.write(buffer->data(), buffer->size());
2161
2162 notifyListener(MEDIA_META_DATA, 0, 0, &in);
2163}
2164
Robert Shihd3b0bbb2014-07-23 15:00:25 -07002165void NuPlayer::sendTimedTextData(const sp<ABuffer> &buffer) {
2166 const void *data;
2167 size_t size = 0;
2168 int64_t timeUs;
2169 int32_t flag = TextDescriptions::LOCAL_DESCRIPTIONS;
2170
2171 AString mime;
2172 CHECK(buffer->meta()->findString("mime", &mime));
2173 CHECK(strcasecmp(mime.c_str(), MEDIA_MIMETYPE_TEXT_3GPP) == 0);
2174
2175 data = buffer->data();
2176 size = buffer->size();
2177
2178 Parcel parcel;
2179 if (size > 0) {
2180 CHECK(buffer->meta()->findInt64("timeUs", &timeUs));
2181 flag |= TextDescriptions::IN_BAND_TEXT_3GPP;
2182 TextDescriptions::getParcelOfDescriptions(
2183 (const uint8_t *)data, size, flag, timeUs / 1000, &parcel);
2184 }
2185
2186 if ((parcel.dataSize() > 0)) {
2187 notifyListener(MEDIA_TIMED_TEXT, 0, 0, &parcel);
2188 } else { // send an empty timed text
2189 notifyListener(MEDIA_TIMED_TEXT, 0, 0);
2190 }
2191}
Andreas Huberb5f25f02013-02-05 10:14:26 -08002192////////////////////////////////////////////////////////////////////////////////
2193
Chong Zhangced1c2f2014-08-08 15:22:35 -07002194sp<AMessage> NuPlayer::Source::getFormat(bool audio) {
2195 sp<MetaData> meta = getFormatMeta(audio);
2196
2197 if (meta == NULL) {
2198 return NULL;
2199 }
2200
2201 sp<AMessage> msg = new AMessage;
2202
2203 if(convertMetaDataToMessage(meta, &msg) == OK) {
2204 return msg;
2205 }
2206 return NULL;
2207}
2208
Andreas Huber9575c962013-02-05 13:59:56 -08002209void NuPlayer::Source::notifyFlagsChanged(uint32_t flags) {
2210 sp<AMessage> notify = dupNotify();
2211 notify->setInt32("what", kWhatFlagsChanged);
2212 notify->setInt32("flags", flags);
2213 notify->post();
2214}
2215
Chong Zhangced1c2f2014-08-08 15:22:35 -07002216void NuPlayer::Source::notifyVideoSizeChanged(const sp<AMessage> &format) {
Andreas Huber9575c962013-02-05 13:59:56 -08002217 sp<AMessage> notify = dupNotify();
2218 notify->setInt32("what", kWhatVideoSizeChanged);
Chong Zhangced1c2f2014-08-08 15:22:35 -07002219 notify->setMessage("format", format);
Andreas Huber9575c962013-02-05 13:59:56 -08002220 notify->post();
2221}
2222
Andreas Huberec0c5972013-02-05 14:47:13 -08002223void NuPlayer::Source::notifyPrepared(status_t err) {
Andreas Huber9575c962013-02-05 13:59:56 -08002224 sp<AMessage> notify = dupNotify();
2225 notify->setInt32("what", kWhatPrepared);
Andreas Huberec0c5972013-02-05 14:47:13 -08002226 notify->setInt32("err", err);
Andreas Huber9575c962013-02-05 13:59:56 -08002227 notify->post();
2228}
2229
Lajos Molnarfcd3e942015-03-31 10:06:48 -07002230void NuPlayer::Source::notifyInstantiateSecureDecoders(const sp<AMessage> &reply) {
2231 sp<AMessage> notify = dupNotify();
2232 notify->setInt32("what", kWhatInstantiateSecureDecoders);
2233 notify->setMessage("reply", reply);
2234 notify->post();
2235}
2236
Andreas Huber84333e02014-02-07 15:36:10 -08002237void NuPlayer::Source::onMessageReceived(const sp<AMessage> & /* msg */) {
Andreas Huberb5f25f02013-02-05 10:14:26 -08002238 TRESPASS();
2239}
2240
Andreas Huberf9334412010-12-15 15:17:42 -08002241} // namespace android