blob: 9963353535ac39c1dd5edde9e9dfdbb8f3449678 [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),
192 mPausedByClient(false) {
Andy Hung8d121d42014-10-03 09:53:53 -0700193 clearFlushComplete();
Andreas Huberf9334412010-12-15 15:17:42 -0800194}
195
196NuPlayer::~NuPlayer() {
197}
198
Andreas Huber9b80c2b2011-06-30 15:47:02 -0700199void NuPlayer::setUID(uid_t uid) {
200 mUIDValid = true;
201 mUID = uid;
202}
203
Andreas Huber43c3e6c2011-01-05 12:17:08 -0800204void NuPlayer::setDriver(const wp<NuPlayerDriver> &driver) {
205 mDriver = driver;
Andreas Huberf9334412010-12-15 15:17:42 -0800206}
207
Andreas Huber9575c962013-02-05 13:59:56 -0800208void NuPlayer::setDataSourceAsync(const sp<IStreamSource> &source) {
Lajos Molnar1d15ab52015-03-04 16:46:34 -0800209 sp<AMessage> msg = new AMessage(kWhatSetDataSource, this);
Andreas Huberf9334412010-12-15 15:17:42 -0800210
Lajos Molnar1d15ab52015-03-04 16:46:34 -0800211 sp<AMessage> notify = new AMessage(kWhatSourceNotify, this);
Andreas Huberb5f25f02013-02-05 10:14:26 -0800212
Andreas Huber240abcc2014-02-13 13:32:37 -0800213 msg->setObject("source", new StreamingSource(notify, source));
Andreas Huber5bc087c2010-12-23 10:27:40 -0800214 msg->post();
215}
Andreas Huberf9334412010-12-15 15:17:42 -0800216
Andreas Huberafed0e12011-09-20 15:39:58 -0700217static bool IsHTTPLiveURL(const char *url) {
218 if (!strncasecmp("http://", url, 7)
Andreas Huber99759402013-04-01 14:28:31 -0700219 || !strncasecmp("https://", url, 8)
220 || !strncasecmp("file://", url, 7)) {
Andreas Huberafed0e12011-09-20 15:39:58 -0700221 size_t len = strlen(url);
222 if (len >= 5 && !strcasecmp(".m3u8", &url[len - 5])) {
223 return true;
224 }
225
226 if (strstr(url,"m3u8")) {
227 return true;
228 }
229 }
230
231 return false;
232}
233
Andreas Huber9575c962013-02-05 13:59:56 -0800234void NuPlayer::setDataSourceAsync(
Andreas Huber1b86fe02014-01-29 11:13:26 -0800235 const sp<IMediaHTTPService> &httpService,
236 const char *url,
237 const KeyedVector<String8, String8> *headers) {
Chong Zhang3de157d2014-08-05 20:54:44 -0700238
Lajos Molnar1d15ab52015-03-04 16:46:34 -0800239 sp<AMessage> msg = new AMessage(kWhatSetDataSource, this);
Oscar Rydhé7a33b772012-02-20 10:15:48 +0100240 size_t len = strlen(url);
Andreas Huber5bc087c2010-12-23 10:27:40 -0800241
Lajos Molnar1d15ab52015-03-04 16:46:34 -0800242 sp<AMessage> notify = new AMessage(kWhatSourceNotify, this);
Andreas Huberb5f25f02013-02-05 10:14:26 -0800243
Andreas Huberafed0e12011-09-20 15:39:58 -0700244 sp<Source> source;
245 if (IsHTTPLiveURL(url)) {
Andreas Huber81e68442014-02-05 11:52:33 -0800246 source = new HTTPLiveSource(notify, httpService, url, headers);
Andreas Huberafed0e12011-09-20 15:39:58 -0700247 } else if (!strncasecmp(url, "rtsp://", 7)) {
Andreas Huber1b86fe02014-01-29 11:13:26 -0800248 source = new RTSPSource(
249 notify, httpService, url, headers, mUIDValid, mUID);
Oscar Rydhé7a33b772012-02-20 10:15:48 +0100250 } else if ((!strncasecmp(url, "http://", 7)
251 || !strncasecmp(url, "https://", 8))
252 && ((len >= 4 && !strcasecmp(".sdp", &url[len - 4]))
253 || strstr(url, ".sdp?"))) {
Andreas Huber1b86fe02014-01-29 11:13:26 -0800254 source = new RTSPSource(
255 notify, httpService, url, headers, mUIDValid, mUID, true);
Andreas Huber2bfdd422011-10-11 15:24:07 -0700256 } else {
Chong Zhang3de157d2014-08-05 20:54:44 -0700257 sp<GenericSource> genericSource =
258 new GenericSource(notify, mUIDValid, mUID);
259 // Don't set FLAG_SECURE on mSourceFlags here for widevine.
260 // The correct flags will be updated in Source::kWhatFlagsChanged
261 // handler when GenericSource is prepared.
Andreas Huber2bfdd422011-10-11 15:24:07 -0700262
Chong Zhanga19f33e2014-08-07 15:35:07 -0700263 status_t err = genericSource->setDataSource(httpService, url, headers);
Chong Zhang3de157d2014-08-05 20:54:44 -0700264
265 if (err == OK) {
266 source = genericSource;
267 } else {
Chong Zhanga19f33e2014-08-07 15:35:07 -0700268 ALOGE("Failed to set data source!");
Chong Zhang3de157d2014-08-05 20:54:44 -0700269 }
270 }
Andreas Huberafed0e12011-09-20 15:39:58 -0700271 msg->setObject("source", source);
272 msg->post();
273}
274
Andreas Huber9575c962013-02-05 13:59:56 -0800275void NuPlayer::setDataSourceAsync(int fd, int64_t offset, int64_t length) {
Lajos Molnar1d15ab52015-03-04 16:46:34 -0800276 sp<AMessage> msg = new AMessage(kWhatSetDataSource, this);
Andreas Huberafed0e12011-09-20 15:39:58 -0700277
Lajos Molnar1d15ab52015-03-04 16:46:34 -0800278 sp<AMessage> notify = new AMessage(kWhatSourceNotify, this);
Andreas Huberb5f25f02013-02-05 10:14:26 -0800279
Chong Zhang3de157d2014-08-05 20:54:44 -0700280 sp<GenericSource> source =
281 new GenericSource(notify, mUIDValid, mUID);
282
Chong Zhanga19f33e2014-08-07 15:35:07 -0700283 status_t err = source->setDataSource(fd, offset, length);
Chong Zhang3de157d2014-08-05 20:54:44 -0700284
285 if (err != OK) {
Chong Zhanga19f33e2014-08-07 15:35:07 -0700286 ALOGE("Failed to set data source!");
Chong Zhang3de157d2014-08-05 20:54:44 -0700287 source = NULL;
288 }
289
Andreas Huberafed0e12011-09-20 15:39:58 -0700290 msg->setObject("source", source);
Andreas Huberf9334412010-12-15 15:17:42 -0800291 msg->post();
292}
293
Chris Watkins99f31602015-03-20 13:06:33 -0700294void NuPlayer::setDataSourceAsync(const sp<DataSource> &dataSource) {
295 sp<AMessage> msg = new AMessage(kWhatSetDataSource, this);
296 sp<AMessage> notify = new AMessage(kWhatSourceNotify, this);
297
298 sp<GenericSource> source = new GenericSource(notify, mUIDValid, mUID);
299 status_t err = source->setDataSource(dataSource);
300
301 if (err != OK) {
302 ALOGE("Failed to set data source!");
303 source = NULL;
304 }
305
306 msg->setObject("source", source);
307 msg->post();
308}
309
Andreas Huber9575c962013-02-05 13:59:56 -0800310void NuPlayer::prepareAsync() {
Lajos Molnar1d15ab52015-03-04 16:46:34 -0800311 (new AMessage(kWhatPrepare, this))->post();
Andreas Huber9575c962013-02-05 13:59:56 -0800312}
313
Andreas Huber57a339c2012-12-03 11:18:00 -0800314void NuPlayer::setVideoSurfaceTextureAsync(
Andy McFadden8ba01022012-12-18 09:46:54 -0800315 const sp<IGraphicBufferProducer> &bufferProducer) {
Lajos Molnar1de1e252015-04-30 18:18:34 -0700316 sp<AMessage> msg = new AMessage(kWhatSetVideoSurface, this);
Andreas Huber57a339c2012-12-03 11:18:00 -0800317
Andy McFadden8ba01022012-12-18 09:46:54 -0800318 if (bufferProducer == NULL) {
Lajos Molnar1de1e252015-04-30 18:18:34 -0700319 msg->setObject("surface", NULL);
Andreas Huber57a339c2012-12-03 11:18:00 -0800320 } else {
Lajos Molnar1de1e252015-04-30 18:18:34 -0700321 msg->setObject("surface", new Surface(bufferProducer, true /* controlledByApp */));
Andreas Huber57a339c2012-12-03 11:18:00 -0800322 }
323
Andreas Huberf9334412010-12-15 15:17:42 -0800324 msg->post();
325}
326
327void NuPlayer::setAudioSink(const sp<MediaPlayerBase::AudioSink> &sink) {
Lajos Molnar1d15ab52015-03-04 16:46:34 -0800328 sp<AMessage> msg = new AMessage(kWhatSetAudioSink, this);
Andreas Huberf9334412010-12-15 15:17:42 -0800329 msg->setObject("sink", sink);
330 msg->post();
331}
332
333void NuPlayer::start() {
Lajos Molnar1d15ab52015-03-04 16:46:34 -0800334 (new AMessage(kWhatStart, this))->post();
Andreas Huberf9334412010-12-15 15:17:42 -0800335}
336
Lajos Molnar3a474aa2015-04-24 17:10:07 -0700337status_t NuPlayer::setPlaybackSettings(const AudioPlaybackRate &rate) {
338 // do some cursory validation of the settings here. audio modes are
339 // only validated when set on the audiosink.
340 if ((rate.mSpeed != 0.f && rate.mSpeed < AUDIO_TIMESTRETCH_SPEED_MIN)
341 || rate.mSpeed > AUDIO_TIMESTRETCH_SPEED_MAX
342 || rate.mPitch < AUDIO_TIMESTRETCH_SPEED_MIN
343 || rate.mPitch > AUDIO_TIMESTRETCH_SPEED_MAX) {
344 return BAD_VALUE;
345 }
346 sp<AMessage> msg = new AMessage(kWhatConfigPlayback, this);
347 writeToAMessage(msg, rate);
348 sp<AMessage> response;
349 status_t err = msg->postAndAwaitResponse(&response);
350 if (err == OK && response != NULL) {
351 CHECK(response->findInt32("err", &err));
352 }
353 return err;
354}
355
356status_t NuPlayer::getPlaybackSettings(AudioPlaybackRate *rate /* nonnull */) {
357 sp<AMessage> msg = new AMessage(kWhatGetPlaybackSettings, this);
358 sp<AMessage> response;
359 status_t err = msg->postAndAwaitResponse(&response);
360 if (err == OK && response != NULL) {
361 CHECK(response->findInt32("err", &err));
362 if (err == OK) {
363 readFromAMessage(response, rate);
364 }
365 }
366 return err;
367}
368
369status_t NuPlayer::setSyncSettings(const AVSyncSettings &sync, float videoFpsHint) {
370 sp<AMessage> msg = new AMessage(kWhatConfigSync, this);
371 writeToAMessage(msg, sync, videoFpsHint);
372 sp<AMessage> response;
373 status_t err = msg->postAndAwaitResponse(&response);
374 if (err == OK && response != NULL) {
375 CHECK(response->findInt32("err", &err));
376 }
377 return err;
378}
379
380status_t NuPlayer::getSyncSettings(
381 AVSyncSettings *sync /* nonnull */, float *videoFps /* nonnull */) {
382 sp<AMessage> msg = new AMessage(kWhatGetSyncSettings, this);
383 sp<AMessage> response;
384 status_t err = msg->postAndAwaitResponse(&response);
385 if (err == OK && response != NULL) {
386 CHECK(response->findInt32("err", &err));
387 if (err == OK) {
388 readFromAMessage(response, sync, videoFps);
389 }
390 }
391 return err;
Wei Jia98160162015-02-04 17:01:11 -0800392}
393
Andreas Huber43c3e6c2011-01-05 12:17:08 -0800394void NuPlayer::pause() {
Lajos Molnar1d15ab52015-03-04 16:46:34 -0800395 (new AMessage(kWhatPause, this))->post();
Andreas Huber43c3e6c2011-01-05 12:17:08 -0800396}
397
Andreas Huber1aef2112011-01-04 14:01:29 -0800398void NuPlayer::resetAsync() {
Chong Zhang48296b72014-09-14 14:28:45 -0700399 if (mSource != NULL) {
400 // During a reset, the data source might be unresponsive already, we need to
401 // disconnect explicitly so that reads exit promptly.
402 // We can't queue the disconnect request to the looper, as it might be
403 // queued behind a stuck read and never gets processed.
404 // Doing a disconnect outside the looper to allows the pending reads to exit
405 // (either successfully or with error).
406 mSource->disconnect();
407 }
408
Lajos Molnar1d15ab52015-03-04 16:46:34 -0800409 (new AMessage(kWhatReset, this))->post();
Andreas Huber1aef2112011-01-04 14:01:29 -0800410}
411
Wei Jiae427abf2014-09-22 15:21:11 -0700412void NuPlayer::seekToAsync(int64_t seekTimeUs, bool needNotify) {
Lajos Molnar1d15ab52015-03-04 16:46:34 -0800413 sp<AMessage> msg = new AMessage(kWhatSeek, this);
Andreas Huber43c3e6c2011-01-05 12:17:08 -0800414 msg->setInt64("seekTimeUs", seekTimeUs);
Wei Jiae427abf2014-09-22 15:21:11 -0700415 msg->setInt32("needNotify", needNotify);
Andreas Huber43c3e6c2011-01-05 12:17:08 -0800416 msg->post();
417}
418
Andreas Huber53df1a42010-12-22 10:03:04 -0800419
Chong Zhang404fced2014-06-11 14:45:31 -0700420void NuPlayer::writeTrackInfo(
421 Parcel* reply, const sp<AMessage> format) const {
422 int32_t trackType;
423 CHECK(format->findInt32("type", &trackType));
424
Robert Shih755106e2015-04-30 14:36:45 -0700425 AString mime;
426 CHECK(format->findString("mime", &mime));
427
Chong Zhang404fced2014-06-11 14:45:31 -0700428 AString lang;
429 CHECK(format->findString("language", &lang));
430
431 reply->writeInt32(2); // write something non-zero
432 reply->writeInt32(trackType);
Robert Shih755106e2015-04-30 14:36:45 -0700433 reply->writeString16(String16(mime.c_str()));
Chong Zhang404fced2014-06-11 14:45:31 -0700434 reply->writeString16(String16(lang.c_str()));
435
436 if (trackType == MEDIA_TRACK_TYPE_SUBTITLE) {
Chong Zhang404fced2014-06-11 14:45:31 -0700437 int32_t isAuto, isDefault, isForced;
438 CHECK(format->findInt32("auto", &isAuto));
439 CHECK(format->findInt32("default", &isDefault));
440 CHECK(format->findInt32("forced", &isForced));
441
Chong Zhang404fced2014-06-11 14:45:31 -0700442 reply->writeInt32(isAuto);
443 reply->writeInt32(isDefault);
444 reply->writeInt32(isForced);
445 }
446}
447
Andreas Huberf9334412010-12-15 15:17:42 -0800448void NuPlayer::onMessageReceived(const sp<AMessage> &msg) {
449 switch (msg->what()) {
450 case kWhatSetDataSource:
451 {
Steve Block3856b092011-10-20 11:56:00 +0100452 ALOGV("kWhatSetDataSource");
Andreas Huberf9334412010-12-15 15:17:42 -0800453
454 CHECK(mSource == NULL);
455
Chong Zhang3de157d2014-08-05 20:54:44 -0700456 status_t err = OK;
Andreas Huber5bc087c2010-12-23 10:27:40 -0800457 sp<RefBase> obj;
458 CHECK(msg->findObject("source", &obj));
Chong Zhang3de157d2014-08-05 20:54:44 -0700459 if (obj != NULL) {
460 mSource = static_cast<Source *>(obj.get());
Chong Zhang3de157d2014-08-05 20:54:44 -0700461 } else {
462 err = UNKNOWN_ERROR;
463 }
Andreas Huber9575c962013-02-05 13:59:56 -0800464
465 CHECK(mDriver != NULL);
466 sp<NuPlayerDriver> driver = mDriver.promote();
467 if (driver != NULL) {
Chong Zhang3de157d2014-08-05 20:54:44 -0700468 driver->notifySetDataSourceCompleted(err);
Andreas Huber9575c962013-02-05 13:59:56 -0800469 }
470 break;
471 }
472
473 case kWhatPrepare:
474 {
475 mSource->prepareAsync();
Andreas Huberf9334412010-12-15 15:17:42 -0800476 break;
477 }
478
Chong Zhangdcb89b32013-08-06 09:44:47 -0700479 case kWhatGetTrackInfo:
480 {
Lajos Molnar3f274362015-03-05 14:35:41 -0800481 sp<AReplyToken> replyID;
Chong Zhangdcb89b32013-08-06 09:44:47 -0700482 CHECK(msg->senderAwaitsResponse(&replyID));
483
Chong Zhang404fced2014-06-11 14:45:31 -0700484 Parcel* reply;
485 CHECK(msg->findPointer("reply", (void**)&reply));
486
487 size_t inbandTracks = 0;
Chong Zhangdcb89b32013-08-06 09:44:47 -0700488 if (mSource != NULL) {
Chong Zhang404fced2014-06-11 14:45:31 -0700489 inbandTracks = mSource->getTrackCount();
490 }
491
Chong Zhanga7fa1d92014-06-11 14:49:23 -0700492 size_t ccTracks = 0;
493 if (mCCDecoder != NULL) {
494 ccTracks = mCCDecoder->getTrackCount();
495 }
496
Chong Zhang404fced2014-06-11 14:45:31 -0700497 // total track count
Chong Zhanga7fa1d92014-06-11 14:49:23 -0700498 reply->writeInt32(inbandTracks + ccTracks);
Chong Zhang404fced2014-06-11 14:45:31 -0700499
500 // write inband tracks
501 for (size_t i = 0; i < inbandTracks; ++i) {
502 writeTrackInfo(reply, mSource->getTrackInfo(i));
Chong Zhangdcb89b32013-08-06 09:44:47 -0700503 }
504
Chong Zhanga7fa1d92014-06-11 14:49:23 -0700505 // write CC track
506 for (size_t i = 0; i < ccTracks; ++i) {
507 writeTrackInfo(reply, mCCDecoder->getTrackInfo(i));
508 }
509
Chong Zhangdcb89b32013-08-06 09:44:47 -0700510 sp<AMessage> response = new AMessage;
Chong Zhangdcb89b32013-08-06 09:44:47 -0700511 response->postReply(replyID);
512 break;
513 }
514
Robert Shih7c4f0d72014-07-09 18:53:31 -0700515 case kWhatGetSelectedTrack:
516 {
517 status_t err = INVALID_OPERATION;
518 if (mSource != NULL) {
519 err = OK;
520
521 int32_t type32;
522 CHECK(msg->findInt32("type", (int32_t*)&type32));
523 media_track_type type = (media_track_type)type32;
524 ssize_t selectedTrack = mSource->getSelectedTrack(type);
525
526 Parcel* reply;
527 CHECK(msg->findPointer("reply", (void**)&reply));
528 reply->writeInt32(selectedTrack);
529 }
530
531 sp<AMessage> response = new AMessage;
532 response->setInt32("err", err);
533
Lajos Molnar3f274362015-03-05 14:35:41 -0800534 sp<AReplyToken> replyID;
Robert Shih7c4f0d72014-07-09 18:53:31 -0700535 CHECK(msg->senderAwaitsResponse(&replyID));
536 response->postReply(replyID);
537 break;
538 }
539
Chong Zhangdcb89b32013-08-06 09:44:47 -0700540 case kWhatSelectTrack:
541 {
Lajos Molnar3f274362015-03-05 14:35:41 -0800542 sp<AReplyToken> replyID;
Chong Zhangdcb89b32013-08-06 09:44:47 -0700543 CHECK(msg->senderAwaitsResponse(&replyID));
544
Chong Zhang404fced2014-06-11 14:45:31 -0700545 size_t trackIndex;
546 int32_t select;
Robert Shih6ffb1fd2014-10-29 16:24:32 -0700547 int64_t timeUs;
Chong Zhang404fced2014-06-11 14:45:31 -0700548 CHECK(msg->findSize("trackIndex", &trackIndex));
549 CHECK(msg->findInt32("select", &select));
Robert Shih6ffb1fd2014-10-29 16:24:32 -0700550 CHECK(msg->findInt64("timeUs", &timeUs));
Chong Zhang404fced2014-06-11 14:45:31 -0700551
Chong Zhangdcb89b32013-08-06 09:44:47 -0700552 status_t err = INVALID_OPERATION;
Chong Zhang404fced2014-06-11 14:45:31 -0700553
554 size_t inbandTracks = 0;
Chong Zhangdcb89b32013-08-06 09:44:47 -0700555 if (mSource != NULL) {
Chong Zhang404fced2014-06-11 14:45:31 -0700556 inbandTracks = mSource->getTrackCount();
557 }
Chong Zhanga7fa1d92014-06-11 14:49:23 -0700558 size_t ccTracks = 0;
559 if (mCCDecoder != NULL) {
560 ccTracks = mCCDecoder->getTrackCount();
561 }
Chong Zhang404fced2014-06-11 14:45:31 -0700562
563 if (trackIndex < inbandTracks) {
Robert Shih6ffb1fd2014-10-29 16:24:32 -0700564 err = mSource->selectTrack(trackIndex, select, timeUs);
Robert Shihd3b0bbb2014-07-23 15:00:25 -0700565
566 if (!select && err == OK) {
567 int32_t type;
568 sp<AMessage> info = mSource->getTrackInfo(trackIndex);
569 if (info != NULL
570 && info->findInt32("type", &type)
571 && type == MEDIA_TRACK_TYPE_TIMEDTEXT) {
572 ++mTimedTextGeneration;
573 }
574 }
Chong Zhanga7fa1d92014-06-11 14:49:23 -0700575 } else {
576 trackIndex -= inbandTracks;
577
578 if (trackIndex < ccTracks) {
579 err = mCCDecoder->selectTrack(trackIndex, select);
580 }
Chong Zhangdcb89b32013-08-06 09:44:47 -0700581 }
582
583 sp<AMessage> response = new AMessage;
584 response->setInt32("err", err);
585
586 response->postReply(replyID);
587 break;
588 }
589
Andreas Huberb7c8e912012-11-27 15:02:53 -0800590 case kWhatPollDuration:
591 {
592 int32_t generation;
593 CHECK(msg->findInt32("generation", &generation));
594
595 if (generation != mPollDurationGeneration) {
596 // stale
597 break;
598 }
599
600 int64_t durationUs;
601 if (mDriver != NULL && mSource->getDuration(&durationUs) == OK) {
602 sp<NuPlayerDriver> driver = mDriver.promote();
603 if (driver != NULL) {
604 driver->notifyDuration(durationUs);
605 }
606 }
607
608 msg->post(1000000ll); // poll again in a second.
609 break;
610 }
611
Lajos Molnar1de1e252015-04-30 18:18:34 -0700612 case kWhatSetVideoSurface:
Andreas Huberf9334412010-12-15 15:17:42 -0800613 {
Lajos Molnar1de1e252015-04-30 18:18:34 -0700614 ALOGV("kWhatSetVideoSurface");
Andreas Huberf9334412010-12-15 15:17:42 -0800615
616 sp<RefBase> obj;
Lajos Molnar1de1e252015-04-30 18:18:34 -0700617 CHECK(msg->findObject("surface", &obj));
618 sp<Surface> surface = static_cast<Surface *>(obj.get());
Lajos Molnarf4849522014-12-10 16:58:57 -0800619 if (mSource == NULL || mSource->getFormat(false /* audio */) == NULL) {
Lajos Molnar1de1e252015-04-30 18:18:34 -0700620 performSetSurface(surface);
Wei Jiafef808d2014-10-31 17:57:05 -0700621 break;
622 }
623
624 mDeferredActions.push_back(
625 new FlushDecoderAction(FLUSH_CMD_FLUSH /* audio */,
626 FLUSH_CMD_SHUTDOWN /* video */));
627
Lajos Molnar1de1e252015-04-30 18:18:34 -0700628 mDeferredActions.push_back(new SetSurfaceAction(surface));
James Dong0d268a32012-08-31 12:18:27 -0700629
Andreas Huber57a339c2012-12-03 11:18:00 -0800630 if (obj != NULL) {
Wei Jiafef808d2014-10-31 17:57:05 -0700631 if (mStarted) {
Andy Hung73535852014-09-05 11:42:58 -0700632 // Issue a seek to refresh the video screen only if started otherwise
633 // the extractor may not yet be started and will assert.
634 // If the video decoder is not set (perhaps audio only in this case)
635 // do not perform a seek as it is not needed.
Ronghua Wua73d9e02014-10-08 15:13:29 -0700636 int64_t currentPositionUs = 0;
637 if (getCurrentPosition(&currentPositionUs) == OK) {
638 mDeferredActions.push_back(
639 new SeekAction(currentPositionUs, false /* needNotify */));
640 }
Andy Hung73535852014-09-05 11:42:58 -0700641 }
Wei Jiaac428aa2014-09-02 19:01:34 -0700642
Andreas Huber57a339c2012-12-03 11:18:00 -0800643 // If there is a new surface texture, instantiate decoders
644 // again if possible.
645 mDeferredActions.push_back(
646 new SimpleAction(&NuPlayer::performScanSources));
647 }
648
Chong Zhangf8d71772014-11-26 15:08:34 -0800649 // After a flush without shutdown, decoder is paused.
650 // Don't resume it until source seek is done, otherwise it could
Chong Zhang7137ec72014-11-12 16:41:05 -0800651 // start pulling stale data too soon.
652 mDeferredActions.push_back(
Chong Zhangf8d71772014-11-26 15:08:34 -0800653 new ResumeDecoderAction(false /* needNotify */));
Chong Zhang7137ec72014-11-12 16:41:05 -0800654
Andreas Huber57a339c2012-12-03 11:18:00 -0800655 processDeferredActions();
Andreas Huberf9334412010-12-15 15:17:42 -0800656 break;
657 }
658
659 case kWhatSetAudioSink:
660 {
Steve Block3856b092011-10-20 11:56:00 +0100661 ALOGV("kWhatSetAudioSink");
Andreas Huberf9334412010-12-15 15:17:42 -0800662
663 sp<RefBase> obj;
664 CHECK(msg->findObject("sink", &obj));
665
666 mAudioSink = static_cast<MediaPlayerBase::AudioSink *>(obj.get());
667 break;
668 }
669
670 case kWhatStart:
671 {
Steve Block3856b092011-10-20 11:56:00 +0100672 ALOGV("kWhatStart");
Wei Jia94211742014-10-28 17:09:06 -0700673 if (mStarted) {
674 onResume();
675 } else {
676 onStart();
Lajos Molnar09524832014-07-17 14:29:51 -0700677 }
Chong Zhangefbb6192015-01-30 17:13:27 -0800678 mPausedByClient = false;
Andreas Huberf9334412010-12-15 15:17:42 -0800679 break;
680 }
681
Lajos Molnar3a474aa2015-04-24 17:10:07 -0700682 case kWhatConfigPlayback:
Wei Jia98160162015-02-04 17:01:11 -0800683 {
Lajos Molnar3a474aa2015-04-24 17:10:07 -0700684 sp<AReplyToken> replyID;
685 CHECK(msg->senderAwaitsResponse(&replyID));
686 AudioPlaybackRate rate /* sanitized */;
687 readFromAMessage(msg, &rate);
688 status_t err = OK;
Wei Jia98160162015-02-04 17:01:11 -0800689 if (mRenderer != NULL) {
Lajos Molnar3a474aa2015-04-24 17:10:07 -0700690 err = mRenderer->setPlaybackSettings(rate);
691 }
692 if (err == OK) {
693 if (rate.mSpeed == 0.f) {
694 onPause();
695 // save all other settings (using non-paused speed)
696 // so we can restore them on start
697 AudioPlaybackRate newRate = rate;
698 newRate.mSpeed = mPlaybackSettings.mSpeed;
699 mPlaybackSettings = newRate;
700 } else { /* rate.mSpeed != 0.f */
701 onResume();
702 mPlaybackSettings = rate;
703 }
Wei Jia98160162015-02-04 17:01:11 -0800704 }
Ronghua Wu8db88132015-04-22 13:51:35 -0700705
706 if (mVideoDecoder != NULL) {
707 sp<MetaData> meta = getFileMeta();
708 int32_t rate;
709 if (meta != NULL && meta->findInt32(kKeyFrameRate, &rate) && rate > 0) {
710 sp<AMessage> params = new AMessage();
Lajos Molnar3a474aa2015-04-24 17:10:07 -0700711 params->setFloat("operating-rate", rate * mPlaybackSettings.mSpeed);
Ronghua Wu8db88132015-04-22 13:51:35 -0700712 mVideoDecoder->setParameters(params);
713 }
714 }
715
Lajos Molnar3a474aa2015-04-24 17:10:07 -0700716 sp<AMessage> response = new AMessage;
717 response->setInt32("err", err);
718 response->postReply(replyID);
719 break;
720 }
721
722 case kWhatGetPlaybackSettings:
723 {
724 sp<AReplyToken> replyID;
725 CHECK(msg->senderAwaitsResponse(&replyID));
726 AudioPlaybackRate rate = mPlaybackSettings;
727 status_t err = OK;
728 if (mRenderer != NULL) {
729 err = mRenderer->getPlaybackSettings(&rate);
730 }
731 if (err == OK) {
732 // get playback settings used by renderer, as it may be
733 // slightly off due to audiosink not taking small changes.
734 mPlaybackSettings = rate;
735 if (mPaused) {
736 rate.mSpeed = 0.f;
737 }
738 }
739 sp<AMessage> response = new AMessage;
740 if (err == OK) {
741 writeToAMessage(response, rate);
742 }
743 response->setInt32("err", err);
744 response->postReply(replyID);
745 break;
746 }
747
748 case kWhatConfigSync:
749 {
750 sp<AReplyToken> replyID;
751 CHECK(msg->senderAwaitsResponse(&replyID));
752
753 ALOGV("kWhatConfigSync");
754 AVSyncSettings sync;
755 float videoFpsHint;
756 readFromAMessage(msg, &sync, &videoFpsHint);
757 status_t err = OK;
758 if (mRenderer != NULL) {
759 err = mRenderer->setSyncSettings(sync, videoFpsHint);
760 }
761 if (err == OK) {
762 mSyncSettings = sync;
763 mVideoFpsHint = videoFpsHint;
764 }
765 sp<AMessage> response = new AMessage;
766 response->setInt32("err", err);
767 response->postReply(replyID);
768 break;
769 }
770
771 case kWhatGetSyncSettings:
772 {
773 sp<AReplyToken> replyID;
774 CHECK(msg->senderAwaitsResponse(&replyID));
775 AVSyncSettings sync = mSyncSettings;
776 float videoFps = mVideoFpsHint;
777 status_t err = OK;
778 if (mRenderer != NULL) {
779 err = mRenderer->getSyncSettings(&sync, &videoFps);
780 if (err == OK) {
781 mSyncSettings = sync;
782 mVideoFpsHint = videoFps;
783 }
784 }
785 sp<AMessage> response = new AMessage;
786 if (err == OK) {
787 writeToAMessage(response, sync, videoFps);
788 }
789 response->setInt32("err", err);
790 response->postReply(replyID);
Wei Jia98160162015-02-04 17:01:11 -0800791 break;
792 }
793
Andreas Huberf9334412010-12-15 15:17:42 -0800794 case kWhatScanSources:
795 {
Andreas Huber1aef2112011-01-04 14:01:29 -0800796 int32_t generation;
797 CHECK(msg->findInt32("generation", &generation));
798 if (generation != mScanSourcesGeneration) {
799 // Drop obsolete msg.
800 break;
801 }
802
Andreas Huber5bc087c2010-12-23 10:27:40 -0800803 mScanSourcesPending = false;
804
Steve Block3856b092011-10-20 11:56:00 +0100805 ALOGV("scanning sources haveAudio=%d, haveVideo=%d",
Andreas Huber43c3e6c2011-01-05 12:17:08 -0800806 mAudioDecoder != NULL, mVideoDecoder != NULL);
807
Andreas Huberb7c8e912012-11-27 15:02:53 -0800808 bool mHadAnySourcesBefore =
809 (mAudioDecoder != NULL) || (mVideoDecoder != NULL);
810
Andy Hung282a7e32014-08-14 15:56:34 -0700811 // initialize video before audio because successful initialization of
812 // video may change deep buffer mode of audio.
Lajos Molnar1de1e252015-04-30 18:18:34 -0700813 if (mSurface != NULL) {
Haynes Mathew George5d246ef2012-07-09 10:36:57 -0700814 instantiateDecoder(false, &mVideoDecoder);
815 }
Andreas Huberf9334412010-12-15 15:17:42 -0800816
Ronghua Wua10fd232014-11-06 16:15:20 -0800817 // Don't try to re-open audio sink if there's an existing decoder.
818 if (mAudioSink != NULL && mAudioDecoder == NULL) {
819 sp<MetaData> audioMeta = mSource->getFormatMeta(true /* audio */);
820 sp<AMessage> videoFormat = mSource->getFormat(false /* audio */);
821 audio_stream_type_t streamType = mAudioSink->getAudioStreamType();
Andy Hung202bce12014-12-03 11:47:36 -0800822 const bool hasVideo = (videoFormat != NULL);
823 const bool canOffload = canOffloadStream(
824 audioMeta, hasVideo, true /* is_streaming */, streamType);
Ronghua Wua10fd232014-11-06 16:15:20 -0800825 if (canOffload) {
826 if (!mOffloadAudio) {
827 mRenderer->signalEnableOffloadAudio();
828 }
Andy Hung282a7e32014-08-14 15:56:34 -0700829 // open audio sink early under offload mode.
830 sp<AMessage> format = mSource->getFormat(true /*audio*/);
Andy Hung202bce12014-12-03 11:47:36 -0800831 tryOpenAudioSinkForOffload(format, hasVideo);
Andy Hung282a7e32014-08-14 15:56:34 -0700832 }
Andreas Huber5bc087c2010-12-23 10:27:40 -0800833 instantiateDecoder(true, &mAudioDecoder);
Andreas Huberf9334412010-12-15 15:17:42 -0800834 }
835
Andreas Huberb7c8e912012-11-27 15:02:53 -0800836 if (!mHadAnySourcesBefore
837 && (mAudioDecoder != NULL || mVideoDecoder != NULL)) {
838 // This is the first time we've found anything playable.
839
Andreas Huber9575c962013-02-05 13:59:56 -0800840 if (mSourceFlags & Source::FLAG_DYNAMIC_DURATION) {
Andreas Huberb7c8e912012-11-27 15:02:53 -0800841 schedulePollDuration();
842 }
843 }
844
Andreas Hubereac68ba2011-09-27 12:12:25 -0700845 status_t err;
846 if ((err = mSource->feedMoreTSData()) != OK) {
Andreas Huber1aef2112011-01-04 14:01:29 -0800847 if (mAudioDecoder == NULL && mVideoDecoder == NULL) {
848 // We're not currently decoding anything (no audio or
849 // video tracks found) and we just ran out of input data.
Andreas Hubereac68ba2011-09-27 12:12:25 -0700850
851 if (err == ERROR_END_OF_STREAM) {
852 notifyListener(MEDIA_PLAYBACK_COMPLETE, 0, 0);
853 } else {
854 notifyListener(MEDIA_ERROR, MEDIA_ERROR_UNKNOWN, err);
855 }
Andreas Huber1aef2112011-01-04 14:01:29 -0800856 }
Andreas Huberf9334412010-12-15 15:17:42 -0800857 break;
858 }
859
Andreas Huberfbe9d812012-08-31 14:05:27 -0700860 if ((mAudioDecoder == NULL && mAudioSink != NULL)
Lajos Molnar1de1e252015-04-30 18:18:34 -0700861 || (mVideoDecoder == NULL && mSurface != NULL)) {
Andreas Huberf9334412010-12-15 15:17:42 -0800862 msg->post(100000ll);
Andreas Huber5bc087c2010-12-23 10:27:40 -0800863 mScanSourcesPending = true;
Andreas Huberf9334412010-12-15 15:17:42 -0800864 }
865 break;
866 }
867
868 case kWhatVideoNotify:
869 case kWhatAudioNotify:
870 {
871 bool audio = msg->what() == kWhatAudioNotify;
872
Wei Jia88703c32014-08-06 11:24:07 -0700873 int32_t currentDecoderGeneration =
874 (audio? mAudioDecoderGeneration : mVideoDecoderGeneration);
875 int32_t requesterGeneration = currentDecoderGeneration - 1;
876 CHECK(msg->findInt32("generation", &requesterGeneration));
877
878 if (requesterGeneration != currentDecoderGeneration) {
879 ALOGV("got message from old %s decoder, generation(%d:%d)",
880 audio ? "audio" : "video", requesterGeneration,
881 currentDecoderGeneration);
882 sp<AMessage> reply;
883 if (!(msg->findMessage("reply", &reply))) {
884 return;
885 }
886
887 reply->setInt32("err", INFO_DISCONTINUITY);
888 reply->post();
889 return;
890 }
891
Andreas Huberf9334412010-12-15 15:17:42 -0800892 int32_t what;
Lajos Molnar1cd13982014-01-17 15:12:51 -0800893 CHECK(msg->findInt32("what", &what));
Andreas Huberf9334412010-12-15 15:17:42 -0800894
Chong Zhang7137ec72014-11-12 16:41:05 -0800895 if (what == DecoderBase::kWhatInputDiscontinuity) {
896 int32_t formatChange;
897 CHECK(msg->findInt32("formatChange", &formatChange));
Andreas Huberf9334412010-12-15 15:17:42 -0800898
Chong Zhang7137ec72014-11-12 16:41:05 -0800899 ALOGV("%s discontinuity: formatChange %d",
900 audio ? "audio" : "video", formatChange);
901
902 if (formatChange) {
903 mDeferredActions.push_back(
904 new FlushDecoderAction(
905 audio ? FLUSH_CMD_SHUTDOWN : FLUSH_CMD_NONE,
906 audio ? FLUSH_CMD_NONE : FLUSH_CMD_SHUTDOWN));
Andreas Huberf9334412010-12-15 15:17:42 -0800907 }
Chong Zhang7137ec72014-11-12 16:41:05 -0800908
909 mDeferredActions.push_back(
910 new SimpleAction(
911 &NuPlayer::performScanSources));
912
913 processDeferredActions();
914 } else if (what == DecoderBase::kWhatEOS) {
Andreas Huberdc9bacd2011-09-26 10:53:29 -0700915 int32_t err;
Lajos Molnar1cd13982014-01-17 15:12:51 -0800916 CHECK(msg->findInt32("err", &err));
Andreas Huberdc9bacd2011-09-26 10:53:29 -0700917
918 if (err == ERROR_END_OF_STREAM) {
Steve Block3856b092011-10-20 11:56:00 +0100919 ALOGV("got %s decoder EOS", audio ? "audio" : "video");
Andreas Huberdc9bacd2011-09-26 10:53:29 -0700920 } else {
Steve Block3856b092011-10-20 11:56:00 +0100921 ALOGV("got %s decoder EOS w/ error %d",
Andreas Huberdc9bacd2011-09-26 10:53:29 -0700922 audio ? "audio" : "video",
923 err);
924 }
925
926 mRenderer->queueEOS(audio, err);
Chong Zhang7137ec72014-11-12 16:41:05 -0800927 } else if (what == DecoderBase::kWhatFlushCompleted) {
Steve Block3856b092011-10-20 11:56:00 +0100928 ALOGV("decoder %s flush completed", audio ? "audio" : "video");
Andreas Huberf9334412010-12-15 15:17:42 -0800929
Andy Hung8d121d42014-10-03 09:53:53 -0700930 handleFlushComplete(audio, true /* isDecoder */);
Andreas Huber3831a062010-12-21 10:22:33 -0800931 finishFlushIfPossible();
Chong Zhang7137ec72014-11-12 16:41:05 -0800932 } else if (what == DecoderBase::kWhatVideoSizeChanged) {
Lajos Molnar1cd13982014-01-17 15:12:51 -0800933 sp<AMessage> format;
934 CHECK(msg->findMessage("format", &format));
935
Wei Jiac6cfd702014-11-11 16:33:20 -0800936 sp<AMessage> inputFormat =
937 mSource->getFormat(false /* audio */);
Andreas Huber3831a062010-12-21 10:22:33 -0800938
Wei Jiac6cfd702014-11-11 16:33:20 -0800939 updateVideoSize(inputFormat, format);
Chong Zhang7137ec72014-11-12 16:41:05 -0800940 } else if (what == DecoderBase::kWhatShutdownCompleted) {
Steve Block3856b092011-10-20 11:56:00 +0100941 ALOGV("%s shutdown completed", audio ? "audio" : "video");
Andreas Huber3831a062010-12-21 10:22:33 -0800942 if (audio) {
943 mAudioDecoder.clear();
Wei Jia57568df2014-09-22 10:16:29 -0700944 ++mAudioDecoderGeneration;
Andreas Huber3831a062010-12-21 10:22:33 -0800945
946 CHECK_EQ((int)mFlushingAudio, (int)SHUTTING_DOWN_DECODER);
947 mFlushingAudio = SHUT_DOWN;
948 } else {
949 mVideoDecoder.clear();
Wei Jia57568df2014-09-22 10:16:29 -0700950 ++mVideoDecoderGeneration;
Andreas Huber3831a062010-12-21 10:22:33 -0800951
952 CHECK_EQ((int)mFlushingVideo, (int)SHUTTING_DOWN_DECODER);
953 mFlushingVideo = SHUT_DOWN;
954 }
955
956 finishFlushIfPossible();
Chong Zhangf8d71772014-11-26 15:08:34 -0800957 } else if (what == DecoderBase::kWhatResumeCompleted) {
958 finishResume();
Chong Zhang7137ec72014-11-12 16:41:05 -0800959 } else if (what == DecoderBase::kWhatError) {
Chong Zhangf4c0a942014-08-11 15:14:10 -0700960 status_t err;
Andy Hung2abde2c2014-09-30 14:40:32 -0700961 if (!msg->findInt32("err", &err) || err == OK) {
Chong Zhangf4c0a942014-08-11 15:14:10 -0700962 err = UNKNOWN_ERROR;
963 }
Andy Hungcf31f1e2014-09-23 14:59:01 -0700964
Andy Hung2abde2c2014-09-30 14:40:32 -0700965 // Decoder errors can be due to Source (e.g. from streaming),
966 // or from decoding corrupted bitstreams, or from other decoder
967 // MediaCodec operations (e.g. from an ongoing reset or seek).
Andy Hung202bce12014-12-03 11:47:36 -0800968 // They may also be due to openAudioSink failure at
969 // decoder start or after a format change.
Andy Hung2abde2c2014-09-30 14:40:32 -0700970 //
971 // We try to gracefully shut down the affected decoder if possible,
972 // rather than trying to force the shutdown with something
973 // similar to performReset(). This method can lead to a hang
974 // if MediaCodec functions block after an error, but they should
975 // typically return INVALID_OPERATION instead of blocking.
976
977 FlushStatus *flushing = audio ? &mFlushingAudio : &mFlushingVideo;
978 ALOGE("received error(%#x) from %s decoder, flushing(%d), now shutting down",
979 err, audio ? "audio" : "video", *flushing);
980
981 switch (*flushing) {
982 case NONE:
983 mDeferredActions.push_back(
Wei Jiafef808d2014-10-31 17:57:05 -0700984 new FlushDecoderAction(
985 audio ? FLUSH_CMD_SHUTDOWN : FLUSH_CMD_NONE,
986 audio ? FLUSH_CMD_NONE : FLUSH_CMD_SHUTDOWN));
Andy Hung2abde2c2014-09-30 14:40:32 -0700987 processDeferredActions();
988 break;
989 case FLUSHING_DECODER:
990 *flushing = FLUSHING_DECODER_SHUTDOWN; // initiate shutdown after flush.
991 break; // Wait for flush to complete.
992 case FLUSHING_DECODER_SHUTDOWN:
993 break; // Wait for flush to complete.
994 case SHUTTING_DOWN_DECODER:
995 break; // Wait for shutdown to complete.
996 case FLUSHED:
997 // Widevine source reads must stop before releasing the video decoder.
998 if (!audio && mSource != NULL && mSourceFlags & Source::FLAG_SECURE) {
999 mSource->stop();
1000 }
1001 getDecoder(audio)->initiateShutdown(); // In the middle of a seek.
1002 *flushing = SHUTTING_DOWN_DECODER; // Shut down.
1003 break;
1004 case SHUT_DOWN:
1005 finishFlushIfPossible(); // Should not occur.
1006 break; // Finish anyways.
Marco Nelissen9e2b7912014-08-18 16:13:03 -07001007 }
Andy Hung2abde2c2014-09-30 14:40:32 -07001008 notifyListener(MEDIA_ERROR, MEDIA_ERROR_UNKNOWN, err);
Lajos Molnar1cd13982014-01-17 15:12:51 -08001009 } else {
1010 ALOGV("Unhandled decoder notification %d '%c%c%c%c'.",
Andreas Hubera1f8ab02012-11-30 10:53:22 -08001011 what,
1012 what >> 24,
1013 (what >> 16) & 0xff,
1014 (what >> 8) & 0xff,
1015 what & 0xff);
Andreas Huberf9334412010-12-15 15:17:42 -08001016 }
1017
1018 break;
1019 }
1020
1021 case kWhatRendererNotify:
1022 {
Wei Jia57568df2014-09-22 10:16:29 -07001023 int32_t requesterGeneration = mRendererGeneration - 1;
1024 CHECK(msg->findInt32("generation", &requesterGeneration));
1025 if (requesterGeneration != mRendererGeneration) {
1026 ALOGV("got message from old renderer, generation(%d:%d)",
1027 requesterGeneration, mRendererGeneration);
1028 return;
1029 }
1030
Andreas Huberf9334412010-12-15 15:17:42 -08001031 int32_t what;
1032 CHECK(msg->findInt32("what", &what));
1033
1034 if (what == Renderer::kWhatEOS) {
1035 int32_t audio;
1036 CHECK(msg->findInt32("audio", &audio));
1037
Andreas Huberc92fd242011-08-16 13:48:44 -07001038 int32_t finalResult;
1039 CHECK(msg->findInt32("finalResult", &finalResult));
1040
Andreas Huberf9334412010-12-15 15:17:42 -08001041 if (audio) {
1042 mAudioEOS = true;
1043 } else {
1044 mVideoEOS = true;
1045 }
1046
Andreas Huberc92fd242011-08-16 13:48:44 -07001047 if (finalResult == ERROR_END_OF_STREAM) {
Steve Block3856b092011-10-20 11:56:00 +01001048 ALOGV("reached %s EOS", audio ? "audio" : "video");
Andreas Huberc92fd242011-08-16 13:48:44 -07001049 } else {
Steve Block29357bc2012-01-06 19:20:56 +00001050 ALOGE("%s track encountered an error (%d)",
Andreas Huberc92fd242011-08-16 13:48:44 -07001051 audio ? "audio" : "video", finalResult);
1052
1053 notifyListener(
1054 MEDIA_ERROR, MEDIA_ERROR_UNKNOWN, finalResult);
1055 }
Andreas Huberf9334412010-12-15 15:17:42 -08001056
1057 if ((mAudioEOS || mAudioDecoder == NULL)
1058 && (mVideoEOS || mVideoDecoder == NULL)) {
1059 notifyListener(MEDIA_PLAYBACK_COMPLETE, 0, 0);
1060 }
Andreas Huber3fe62152011-09-16 15:09:22 -07001061 } else if (what == Renderer::kWhatFlushComplete) {
Andreas Huberf9334412010-12-15 15:17:42 -08001062 int32_t audio;
1063 CHECK(msg->findInt32("audio", &audio));
1064
Steve Block3856b092011-10-20 11:56:00 +01001065 ALOGV("renderer %s flush completed.", audio ? "audio" : "video");
Andy Hung8d121d42014-10-03 09:53:53 -07001066 handleFlushComplete(audio, false /* isDecoder */);
1067 finishFlushIfPossible();
James Dongf57b4ea2012-07-20 13:38:36 -07001068 } else if (what == Renderer::kWhatVideoRenderingStart) {
1069 notifyListener(MEDIA_INFO, MEDIA_INFO_RENDERING_START, 0);
Lajos Molnarcbaffcf2013-08-14 18:30:38 -07001070 } else if (what == Renderer::kWhatMediaRenderingStart) {
1071 ALOGV("media rendering started");
1072 notifyListener(MEDIA_STARTED, 0, 0);
Wei Jia3a2956d2014-07-22 16:01:33 -07001073 } else if (what == Renderer::kWhatAudioOffloadTearDown) {
Ronghua Wua10fd232014-11-06 16:15:20 -08001074 ALOGV("Tear down audio offload, fall back to s/w path if due to error.");
Wei Jia3a2956d2014-07-22 16:01:33 -07001075 int64_t positionUs;
1076 CHECK(msg->findInt64("positionUs", &positionUs));
Ronghua Wu08529172014-10-02 16:55:52 -07001077 int32_t reason;
1078 CHECK(msg->findInt32("reason", &reason));
Andy Hung282a7e32014-08-14 15:56:34 -07001079 closeAudioSink();
Wei Jia3a2956d2014-07-22 16:01:33 -07001080 mAudioDecoder.clear();
Wei Jia57568df2014-09-22 10:16:29 -07001081 ++mAudioDecoderGeneration;
Chong Zhang7137ec72014-11-12 16:41:05 -08001082 mRenderer->flush(
1083 true /* audio */, false /* notifyComplete */);
Wei Jia3a2956d2014-07-22 16:01:33 -07001084 if (mVideoDecoder != NULL) {
Chong Zhang7137ec72014-11-12 16:41:05 -08001085 mRenderer->flush(
1086 false /* audio */, false /* notifyComplete */);
Wei Jia3a2956d2014-07-22 16:01:33 -07001087 }
Wei Jia3a2956d2014-07-22 16:01:33 -07001088
Wei Jiae427abf2014-09-22 15:21:11 -07001089 performSeek(positionUs, false /* needNotify */);
Ronghua Wu08529172014-10-02 16:55:52 -07001090 if (reason == Renderer::kDueToError) {
Ronghua Wua10fd232014-11-06 16:15:20 -08001091 mRenderer->signalDisableOffloadAudio();
1092 mOffloadAudio = false;
Ronghua Wu08529172014-10-02 16:55:52 -07001093 instantiateDecoder(true /* audio */, &mAudioDecoder);
1094 }
Andreas Huberf9334412010-12-15 15:17:42 -08001095 }
1096 break;
1097 }
1098
1099 case kWhatMoreDataQueued:
1100 {
1101 break;
1102 }
1103
Andreas Huber1aef2112011-01-04 14:01:29 -08001104 case kWhatReset:
1105 {
Steve Block3856b092011-10-20 11:56:00 +01001106 ALOGV("kWhatReset");
Andreas Huber1aef2112011-01-04 14:01:29 -08001107
Andreas Hubera1f8ab02012-11-30 10:53:22 -08001108 mDeferredActions.push_back(
Wei Jiafef808d2014-10-31 17:57:05 -07001109 new FlushDecoderAction(
1110 FLUSH_CMD_SHUTDOWN /* audio */,
1111 FLUSH_CMD_SHUTDOWN /* video */));
Andreas Huberb7c8e912012-11-27 15:02:53 -08001112
Andreas Hubera1f8ab02012-11-30 10:53:22 -08001113 mDeferredActions.push_back(
1114 new SimpleAction(&NuPlayer::performReset));
Andreas Huberb58ce9f2011-11-28 16:27:35 -08001115
Andreas Hubera1f8ab02012-11-30 10:53:22 -08001116 processDeferredActions();
Andreas Huber1aef2112011-01-04 14:01:29 -08001117 break;
1118 }
1119
Andreas Huber43c3e6c2011-01-05 12:17:08 -08001120 case kWhatSeek:
1121 {
1122 int64_t seekTimeUs;
Wei Jiae427abf2014-09-22 15:21:11 -07001123 int32_t needNotify;
Andreas Huber43c3e6c2011-01-05 12:17:08 -08001124 CHECK(msg->findInt64("seekTimeUs", &seekTimeUs));
Wei Jiae427abf2014-09-22 15:21:11 -07001125 CHECK(msg->findInt32("needNotify", &needNotify));
Andreas Huber43c3e6c2011-01-05 12:17:08 -08001126
Wei Jiae427abf2014-09-22 15:21:11 -07001127 ALOGV("kWhatSeek seekTimeUs=%lld us, needNotify=%d",
Lajos Molnar6d339f12015-04-17 16:15:53 -07001128 (long long)seekTimeUs, needNotify);
Andreas Huber43c3e6c2011-01-05 12:17:08 -08001129
Andreas Hubera1f8ab02012-11-30 10:53:22 -08001130 mDeferredActions.push_back(
Wei Jiafef808d2014-10-31 17:57:05 -07001131 new FlushDecoderAction(FLUSH_CMD_FLUSH /* audio */,
1132 FLUSH_CMD_FLUSH /* video */));
Andreas Huber43c3e6c2011-01-05 12:17:08 -08001133
Wei Jiae427abf2014-09-22 15:21:11 -07001134 mDeferredActions.push_back(
1135 new SeekAction(seekTimeUs, needNotify));
Andreas Huber43c3e6c2011-01-05 12:17:08 -08001136
Chong Zhangf8d71772014-11-26 15:08:34 -08001137 // After a flush without shutdown, decoder is paused.
1138 // Don't resume it until source seek is done, otherwise it could
Chong Zhang7137ec72014-11-12 16:41:05 -08001139 // start pulling stale data too soon.
1140 mDeferredActions.push_back(
Chong Zhangf8d71772014-11-26 15:08:34 -08001141 new ResumeDecoderAction(needNotify));
Chong Zhang7137ec72014-11-12 16:41:05 -08001142
Andreas Hubera1f8ab02012-11-30 10:53:22 -08001143 processDeferredActions();
Andreas Huber43c3e6c2011-01-05 12:17:08 -08001144 break;
1145 }
1146
Andreas Huberb4082222011-01-20 15:23:04 -08001147 case kWhatPause:
1148 {
Chong Zhangefbb6192015-01-30 17:13:27 -08001149 onPause();
1150 mPausedByClient = true;
Andreas Huberb4082222011-01-20 15:23:04 -08001151 break;
1152 }
1153
Andreas Huberb5f25f02013-02-05 10:14:26 -08001154 case kWhatSourceNotify:
1155 {
Andreas Huber9575c962013-02-05 13:59:56 -08001156 onSourceNotify(msg);
Andreas Huberb5f25f02013-02-05 10:14:26 -08001157 break;
1158 }
1159
Chong Zhanga7fa1d92014-06-11 14:49:23 -07001160 case kWhatClosedCaptionNotify:
1161 {
1162 onClosedCaptionNotify(msg);
1163 break;
1164 }
1165
Andreas Huberf9334412010-12-15 15:17:42 -08001166 default:
1167 TRESPASS();
1168 break;
1169 }
1170}
1171
Wei Jia94211742014-10-28 17:09:06 -07001172void NuPlayer::onResume() {
Chong Zhangefbb6192015-01-30 17:13:27 -08001173 if (!mPaused) {
1174 return;
1175 }
1176 mPaused = false;
Wei Jia94211742014-10-28 17:09:06 -07001177 if (mSource != NULL) {
1178 mSource->resume();
1179 } else {
1180 ALOGW("resume called when source is gone or not set");
1181 }
1182 // |mAudioDecoder| may have been released due to the pause timeout, so re-create it if
1183 // needed.
1184 if (audioDecoderStillNeeded() && mAudioDecoder == NULL) {
1185 instantiateDecoder(true /* audio */, &mAudioDecoder);
1186 }
1187 if (mRenderer != NULL) {
1188 mRenderer->resume();
1189 } else {
1190 ALOGW("resume called when renderer is gone or not set");
1191 }
1192}
1193
Lajos Molnarfcd3e942015-03-31 10:06:48 -07001194status_t NuPlayer::onInstantiateSecureDecoders() {
1195 status_t err;
1196 if (!(mSourceFlags & Source::FLAG_SECURE)) {
1197 return BAD_TYPE;
1198 }
1199
1200 if (mRenderer != NULL) {
1201 ALOGE("renderer should not be set when instantiating secure decoders");
1202 return UNKNOWN_ERROR;
1203 }
1204
1205 // TRICKY: We rely on mRenderer being null, so that decoder does not start requesting
1206 // data on instantiation.
Lajos Molnar1de1e252015-04-30 18:18:34 -07001207 if (mSurface != NULL) {
Lajos Molnarfcd3e942015-03-31 10:06:48 -07001208 err = instantiateDecoder(false, &mVideoDecoder);
1209 if (err != OK) {
1210 return err;
1211 }
1212 }
1213
1214 if (mAudioSink != NULL) {
1215 err = instantiateDecoder(true, &mAudioDecoder);
1216 if (err != OK) {
1217 return err;
1218 }
1219 }
1220 return OK;
1221}
1222
Wei Jia94211742014-10-28 17:09:06 -07001223void NuPlayer::onStart() {
Wei Jia94211742014-10-28 17:09:06 -07001224 mOffloadAudio = false;
1225 mAudioEOS = false;
1226 mVideoEOS = false;
Wei Jia94211742014-10-28 17:09:06 -07001227 mStarted = true;
1228
Wei Jia94211742014-10-28 17:09:06 -07001229 mSource->start();
1230
1231 uint32_t flags = 0;
1232
1233 if (mSource->isRealTime()) {
1234 flags |= Renderer::FLAG_REAL_TIME;
1235 }
1236
1237 sp<MetaData> audioMeta = mSource->getFormatMeta(true /* audio */);
1238 audio_stream_type_t streamType = AUDIO_STREAM_MUSIC;
1239 if (mAudioSink != NULL) {
1240 streamType = mAudioSink->getAudioStreamType();
1241 }
1242
1243 sp<AMessage> videoFormat = mSource->getFormat(false /* audio */);
1244
1245 mOffloadAudio =
1246 canOffloadStream(audioMeta, (videoFormat != NULL),
1247 true /* is_streaming */, streamType);
1248 if (mOffloadAudio) {
1249 flags |= Renderer::FLAG_OFFLOAD_AUDIO;
1250 }
1251
Lajos Molnar1d15ab52015-03-04 16:46:34 -08001252 sp<AMessage> notify = new AMessage(kWhatRendererNotify, this);
Wei Jia94211742014-10-28 17:09:06 -07001253 ++mRendererGeneration;
1254 notify->setInt32("generation", mRendererGeneration);
1255 mRenderer = new Renderer(mAudioSink, notify, flags);
Wei Jia94211742014-10-28 17:09:06 -07001256 mRendererLooper = new ALooper;
1257 mRendererLooper->setName("NuPlayerRenderer");
1258 mRendererLooper->start(false, false, ANDROID_PRIORITY_AUDIO);
1259 mRendererLooper->registerHandler(mRenderer);
Lajos Molnar3a474aa2015-04-24 17:10:07 -07001260
1261 status_t err = mRenderer->setPlaybackSettings(mPlaybackSettings);
1262 if (err != OK) {
1263 mSource->stop();
1264 notifyListener(MEDIA_ERROR, MEDIA_ERROR_UNKNOWN, err);
1265 return;
Wei Jiac8206ff2015-03-04 13:59:37 -08001266 }
Wei Jia94211742014-10-28 17:09:06 -07001267
1268 sp<MetaData> meta = getFileMeta();
1269 int32_t rate;
1270 if (meta != NULL
1271 && meta->findInt32(kKeyFrameRate, &rate) && rate > 0) {
1272 mRenderer->setVideoFrameRate(rate);
1273 }
1274
Wei Jiac6cfd702014-11-11 16:33:20 -08001275 if (mVideoDecoder != NULL) {
1276 mVideoDecoder->setRenderer(mRenderer);
1277 }
1278 if (mAudioDecoder != NULL) {
1279 mAudioDecoder->setRenderer(mRenderer);
1280 }
1281
Wei Jia94211742014-10-28 17:09:06 -07001282 postScanSources();
1283}
1284
Chong Zhangefbb6192015-01-30 17:13:27 -08001285void NuPlayer::onPause() {
1286 if (mPaused) {
1287 return;
1288 }
1289 mPaused = true;
1290 if (mSource != NULL) {
1291 mSource->pause();
1292 } else {
1293 ALOGW("pause called when source is gone or not set");
1294 }
1295 if (mRenderer != NULL) {
1296 mRenderer->pause();
1297 } else {
1298 ALOGW("pause called when renderer is gone or not set");
1299 }
1300}
1301
Ronghua Wud7988b12014-10-03 15:19:10 -07001302bool NuPlayer::audioDecoderStillNeeded() {
1303 // Audio decoder is no longer needed if it's in shut/shutting down status.
1304 return ((mFlushingAudio != SHUT_DOWN) && (mFlushingAudio != SHUTTING_DOWN_DECODER));
1305}
1306
Andy Hung8d121d42014-10-03 09:53:53 -07001307void NuPlayer::handleFlushComplete(bool audio, bool isDecoder) {
1308 // We wait for both the decoder flush and the renderer flush to complete
1309 // before entering either the FLUSHED or the SHUTTING_DOWN_DECODER state.
1310
1311 mFlushComplete[audio][isDecoder] = true;
1312 if (!mFlushComplete[audio][!isDecoder]) {
1313 return;
1314 }
1315
1316 FlushStatus *state = audio ? &mFlushingAudio : &mFlushingVideo;
1317 switch (*state) {
1318 case FLUSHING_DECODER:
1319 {
1320 *state = FLUSHED;
Andy Hung8d121d42014-10-03 09:53:53 -07001321 break;
1322 }
1323
1324 case FLUSHING_DECODER_SHUTDOWN:
1325 {
1326 *state = SHUTTING_DOWN_DECODER;
1327
1328 ALOGV("initiating %s decoder shutdown", audio ? "audio" : "video");
1329 if (!audio) {
Andy Hung8d121d42014-10-03 09:53:53 -07001330 // Widevine source reads must stop before releasing the video decoder.
1331 if (mSource != NULL && mSourceFlags & Source::FLAG_SECURE) {
1332 mSource->stop();
1333 }
1334 }
1335 getDecoder(audio)->initiateShutdown();
1336 break;
1337 }
1338
1339 default:
1340 // decoder flush completes only occur in a flushing state.
1341 LOG_ALWAYS_FATAL_IF(isDecoder, "decoder flush in invalid state %d", *state);
1342 break;
1343 }
1344}
1345
Andreas Huber3831a062010-12-21 10:22:33 -08001346void NuPlayer::finishFlushIfPossible() {
Wei Jia53904f32014-07-29 10:22:53 -07001347 if (mFlushingAudio != NONE && mFlushingAudio != FLUSHED
1348 && mFlushingAudio != SHUT_DOWN) {
Andreas Huber3831a062010-12-21 10:22:33 -08001349 return;
1350 }
1351
Wei Jia53904f32014-07-29 10:22:53 -07001352 if (mFlushingVideo != NONE && mFlushingVideo != FLUSHED
1353 && mFlushingVideo != SHUT_DOWN) {
Andreas Huber3831a062010-12-21 10:22:33 -08001354 return;
1355 }
1356
Steve Block3856b092011-10-20 11:56:00 +01001357 ALOGV("both audio and video are flushed now.");
Andreas Huber3831a062010-12-21 10:22:33 -08001358
Andreas Huber3831a062010-12-21 10:22:33 -08001359 mFlushingAudio = NONE;
1360 mFlushingVideo = NONE;
Andreas Huber3831a062010-12-21 10:22:33 -08001361
Andy Hung8d121d42014-10-03 09:53:53 -07001362 clearFlushComplete();
1363
Andreas Hubera1f8ab02012-11-30 10:53:22 -08001364 processDeferredActions();
Andreas Huber1aef2112011-01-04 14:01:29 -08001365}
1366
1367void NuPlayer::postScanSources() {
1368 if (mScanSourcesPending) {
1369 return;
1370 }
1371
Lajos Molnar1d15ab52015-03-04 16:46:34 -08001372 sp<AMessage> msg = new AMessage(kWhatScanSources, this);
Andreas Huber1aef2112011-01-04 14:01:29 -08001373 msg->setInt32("generation", mScanSourcesGeneration);
1374 msg->post();
1375
1376 mScanSourcesPending = true;
1377}
1378
Andy Hung202bce12014-12-03 11:47:36 -08001379void NuPlayer::tryOpenAudioSinkForOffload(const sp<AMessage> &format, bool hasVideo) {
1380 // Note: This is called early in NuPlayer to determine whether offloading
1381 // is possible; otherwise the decoders call the renderer openAudioSink directly.
Andy Hung282a7e32014-08-14 15:56:34 -07001382
Andy Hung202bce12014-12-03 11:47:36 -08001383 status_t err = mRenderer->openAudioSink(
1384 format, true /* offloadOnly */, hasVideo, AUDIO_OUTPUT_FLAG_NONE, &mOffloadAudio);
1385 if (err != OK) {
1386 // Any failure we turn off mOffloadAudio.
1387 mOffloadAudio = false;
1388 } else if (mOffloadAudio) {
Chong Zhang3b9eb1f2014-10-15 17:05:08 -07001389 sp<MetaData> audioMeta =
1390 mSource->getFormatMeta(true /* audio */);
1391 sendMetaDataToHal(mAudioSink, audioMeta);
Andy Hung282a7e32014-08-14 15:56:34 -07001392 }
1393}
1394
1395void NuPlayer::closeAudioSink() {
Chong Zhang3b9eb1f2014-10-15 17:05:08 -07001396 mRenderer->closeAudioSink();
Andy Hung282a7e32014-08-14 15:56:34 -07001397}
1398
Chong Zhang7137ec72014-11-12 16:41:05 -08001399status_t NuPlayer::instantiateDecoder(bool audio, sp<DecoderBase> *decoder) {
Andreas Huberf9334412010-12-15 15:17:42 -08001400 if (*decoder != NULL) {
1401 return OK;
1402 }
1403
Andreas Huber84066782011-08-16 09:34:26 -07001404 sp<AMessage> format = mSource->getFormat(audio);
Andreas Huberf9334412010-12-15 15:17:42 -08001405
Andreas Huber84066782011-08-16 09:34:26 -07001406 if (format == NULL) {
Andreas Huberf9334412010-12-15 15:17:42 -08001407 return -EWOULDBLOCK;
1408 }
1409
Ronghua Wu8db88132015-04-22 13:51:35 -07001410 format->setInt32("priority", 0 /* realtime */);
1411
Andreas Huber3fe62152011-09-16 15:09:22 -07001412 if (!audio) {
Andreas Huber84066782011-08-16 09:34:26 -07001413 AString mime;
1414 CHECK(format->findString("mime", &mime));
Chong Zhanga7fa1d92014-06-11 14:49:23 -07001415
Lajos Molnar1d15ab52015-03-04 16:46:34 -08001416 sp<AMessage> ccNotify = new AMessage(kWhatClosedCaptionNotify, this);
Chong Zhang341ab6e2015-02-04 13:37:18 -08001417 if (mCCDecoder == NULL) {
1418 mCCDecoder = new CCDecoder(ccNotify);
1419 }
Lajos Molnar09524832014-07-17 14:29:51 -07001420
1421 if (mSourceFlags & Source::FLAG_SECURE) {
1422 format->setInt32("secure", true);
1423 }
Chong Zhang17134602015-01-07 16:14:34 -08001424
1425 if (mSourceFlags & Source::FLAG_PROTECTED) {
1426 format->setInt32("protected", true);
1427 }
Ronghua Wu8db88132015-04-22 13:51:35 -07001428
1429 sp<MetaData> meta = getFileMeta();
1430 int32_t rate;
1431 if (meta != NULL && meta->findInt32(kKeyFrameRate, &rate) && rate > 0) {
Lajos Molnar3a474aa2015-04-24 17:10:07 -07001432 format->setFloat("operating-rate", rate * mPlaybackSettings.mSpeed);
Ronghua Wu8db88132015-04-22 13:51:35 -07001433 }
Andreas Huber3fe62152011-09-16 15:09:22 -07001434 }
1435
Wei Jiabc2fb722014-07-08 16:37:57 -07001436 if (audio) {
Lajos Molnar1d15ab52015-03-04 16:46:34 -08001437 sp<AMessage> notify = new AMessage(kWhatAudioNotify, this);
Wei Jia88703c32014-08-06 11:24:07 -07001438 ++mAudioDecoderGeneration;
1439 notify->setInt32("generation", mAudioDecoderGeneration);
1440
Wei Jiabc2fb722014-07-08 16:37:57 -07001441 if (mOffloadAudio) {
Haynes Mathew George8b635332015-03-30 17:59:47 -07001442 const bool hasVideo = (mSource->getFormat(false /*audio */) != NULL);
1443 format->setInt32("has-video", hasVideo);
Wei Jiac6cfd702014-11-11 16:33:20 -08001444 *decoder = new DecoderPassThrough(notify, mSource, mRenderer);
Wei Jiabc2fb722014-07-08 16:37:57 -07001445 } else {
Wei Jiac6cfd702014-11-11 16:33:20 -08001446 *decoder = new Decoder(notify, mSource, mRenderer);
Wei Jiabc2fb722014-07-08 16:37:57 -07001447 }
1448 } else {
Lajos Molnar1d15ab52015-03-04 16:46:34 -08001449 sp<AMessage> notify = new AMessage(kWhatVideoNotify, this);
Wei Jia88703c32014-08-06 11:24:07 -07001450 ++mVideoDecoderGeneration;
1451 notify->setInt32("generation", mVideoDecoderGeneration);
1452
Chong Zhang7137ec72014-11-12 16:41:05 -08001453 *decoder = new Decoder(
Lajos Molnar1de1e252015-04-30 18:18:34 -07001454 notify, mSource, mRenderer, mSurface, mCCDecoder);
Lajos Molnard9fd6312014-11-06 11:00:00 -08001455
1456 // enable FRC if high-quality AV sync is requested, even if not
Lajos Molnar1de1e252015-04-30 18:18:34 -07001457 // directly queuing to display, as this will even improve textureview
Lajos Molnard9fd6312014-11-06 11:00:00 -08001458 // playback.
1459 {
1460 char value[PROPERTY_VALUE_MAX];
1461 if (property_get("persist.sys.media.avsync", value, NULL) &&
1462 (!strcmp("1", value) || !strcasecmp("true", value))) {
1463 format->setInt32("auto-frc", 1);
1464 }
1465 }
Wei Jiabc2fb722014-07-08 16:37:57 -07001466 }
Lajos Molnar1cd13982014-01-17 15:12:51 -08001467 (*decoder)->init();
Andreas Huber84066782011-08-16 09:34:26 -07001468 (*decoder)->configure(format);
Andreas Huberf9334412010-12-15 15:17:42 -08001469
Lajos Molnar09524832014-07-17 14:29:51 -07001470 // allocate buffers to decrypt widevine source buffers
1471 if (!audio && (mSourceFlags & Source::FLAG_SECURE)) {
1472 Vector<sp<ABuffer> > inputBufs;
1473 CHECK_EQ((*decoder)->getInputBuffers(&inputBufs), (status_t)OK);
1474
1475 Vector<MediaBuffer *> mediaBufs;
1476 for (size_t i = 0; i < inputBufs.size(); i++) {
1477 const sp<ABuffer> &buffer = inputBufs[i];
1478 MediaBuffer *mbuf = new MediaBuffer(buffer->data(), buffer->size());
1479 mediaBufs.push(mbuf);
1480 }
1481
1482 status_t err = mSource->setBuffers(audio, mediaBufs);
1483 if (err != OK) {
1484 for (size_t i = 0; i < mediaBufs.size(); ++i) {
1485 mediaBufs[i]->release();
1486 }
1487 mediaBufs.clear();
1488 ALOGE("Secure source didn't support secure mediaBufs.");
1489 return err;
1490 }
1491 }
Andreas Huberf9334412010-12-15 15:17:42 -08001492 return OK;
1493}
1494
Chong Zhangced1c2f2014-08-08 15:22:35 -07001495void NuPlayer::updateVideoSize(
1496 const sp<AMessage> &inputFormat,
1497 const sp<AMessage> &outputFormat) {
1498 if (inputFormat == NULL) {
1499 ALOGW("Unknown video size, reporting 0x0!");
1500 notifyListener(MEDIA_SET_VIDEO_SIZE, 0, 0);
1501 return;
1502 }
1503
1504 int32_t displayWidth, displayHeight;
Chong Zhangced1c2f2014-08-08 15:22:35 -07001505 if (outputFormat != NULL) {
1506 int32_t width, height;
1507 CHECK(outputFormat->findInt32("width", &width));
1508 CHECK(outputFormat->findInt32("height", &height));
1509
1510 int32_t cropLeft, cropTop, cropRight, cropBottom;
1511 CHECK(outputFormat->findRect(
1512 "crop",
1513 &cropLeft, &cropTop, &cropRight, &cropBottom));
1514
1515 displayWidth = cropRight - cropLeft + 1;
1516 displayHeight = cropBottom - cropTop + 1;
1517
1518 ALOGV("Video output format changed to %d x %d "
1519 "(crop: %d x %d @ (%d, %d))",
1520 width, height,
1521 displayWidth,
1522 displayHeight,
1523 cropLeft, cropTop);
1524 } else {
1525 CHECK(inputFormat->findInt32("width", &displayWidth));
1526 CHECK(inputFormat->findInt32("height", &displayHeight));
1527
1528 ALOGV("Video input format %d x %d", displayWidth, displayHeight);
1529 }
1530
1531 // Take into account sample aspect ratio if necessary:
1532 int32_t sarWidth, sarHeight;
1533 if (inputFormat->findInt32("sar-width", &sarWidth)
1534 && inputFormat->findInt32("sar-height", &sarHeight)) {
1535 ALOGV("Sample aspect ratio %d : %d", sarWidth, sarHeight);
1536
1537 displayWidth = (displayWidth * sarWidth) / sarHeight;
1538
1539 ALOGV("display dimensions %d x %d", displayWidth, displayHeight);
1540 }
1541
1542 int32_t rotationDegrees;
1543 if (!inputFormat->findInt32("rotation-degrees", &rotationDegrees)) {
1544 rotationDegrees = 0;
1545 }
1546
1547 if (rotationDegrees == 90 || rotationDegrees == 270) {
1548 int32_t tmp = displayWidth;
1549 displayWidth = displayHeight;
1550 displayHeight = tmp;
1551 }
1552
1553 notifyListener(
1554 MEDIA_SET_VIDEO_SIZE,
1555 displayWidth,
1556 displayHeight);
1557}
1558
Chong Zhangdcb89b32013-08-06 09:44:47 -07001559void NuPlayer::notifyListener(int msg, int ext1, int ext2, const Parcel *in) {
Andreas Huber43c3e6c2011-01-05 12:17:08 -08001560 if (mDriver == NULL) {
Andreas Huberf9334412010-12-15 15:17:42 -08001561 return;
1562 }
1563
Andreas Huber43c3e6c2011-01-05 12:17:08 -08001564 sp<NuPlayerDriver> driver = mDriver.promote();
Andreas Huberf9334412010-12-15 15:17:42 -08001565
Andreas Huber43c3e6c2011-01-05 12:17:08 -08001566 if (driver == NULL) {
Andreas Huberf9334412010-12-15 15:17:42 -08001567 return;
1568 }
1569
Chong Zhangdcb89b32013-08-06 09:44:47 -07001570 driver->notifyListener(msg, ext1, ext2, in);
Andreas Huberf9334412010-12-15 15:17:42 -08001571}
1572
Chong Zhang7137ec72014-11-12 16:41:05 -08001573void NuPlayer::flushDecoder(bool audio, bool needShutdown) {
Andreas Huber14f76722013-01-15 09:04:18 -08001574 ALOGV("[%s] flushDecoder needShutdown=%d",
1575 audio ? "audio" : "video", needShutdown);
1576
Chong Zhang7137ec72014-11-12 16:41:05 -08001577 const sp<DecoderBase> &decoder = getDecoder(audio);
Lajos Molnar87603c02014-08-20 19:25:30 -07001578 if (decoder == NULL) {
Steve Blockdf64d152012-01-04 20:05:49 +00001579 ALOGI("flushDecoder %s without decoder present",
Andreas Huber6e3d3112011-11-28 12:36:11 -08001580 audio ? "audio" : "video");
Lajos Molnar87603c02014-08-20 19:25:30 -07001581 return;
Andreas Huber6e3d3112011-11-28 12:36:11 -08001582 }
1583
Andreas Huber1aef2112011-01-04 14:01:29 -08001584 // Make sure we don't continue to scan sources until we finish flushing.
1585 ++mScanSourcesGeneration;
Chong Zhang3b032b32015-04-17 15:49:06 -07001586 if (mScanSourcesPending) {
1587 mDeferredActions.push_back(
1588 new SimpleAction(&NuPlayer::performScanSources));
1589 mScanSourcesPending = false;
1590 }
Andreas Huber1aef2112011-01-04 14:01:29 -08001591
Chong Zhang7137ec72014-11-12 16:41:05 -08001592 decoder->signalFlush();
Andreas Huber1aef2112011-01-04 14:01:29 -08001593
1594 FlushStatus newStatus =
1595 needShutdown ? FLUSHING_DECODER_SHUTDOWN : FLUSHING_DECODER;
1596
Lajos Molnarfcd3e942015-03-31 10:06:48 -07001597 mFlushComplete[audio][false /* isDecoder */] = (mRenderer == NULL);
Andy Hung8d121d42014-10-03 09:53:53 -07001598 mFlushComplete[audio][true /* isDecoder */] = false;
Andreas Huber1aef2112011-01-04 14:01:29 -08001599 if (audio) {
Wei Jia53904f32014-07-29 10:22:53 -07001600 ALOGE_IF(mFlushingAudio != NONE,
1601 "audio flushDecoder() is called in state %d", mFlushingAudio);
Andreas Huber1aef2112011-01-04 14:01:29 -08001602 mFlushingAudio = newStatus;
Andreas Huber1aef2112011-01-04 14:01:29 -08001603 } else {
Wei Jia53904f32014-07-29 10:22:53 -07001604 ALOGE_IF(mFlushingVideo != NONE,
1605 "video flushDecoder() is called in state %d", mFlushingVideo);
Andreas Huber1aef2112011-01-04 14:01:29 -08001606 mFlushingVideo = newStatus;
Andreas Huber1aef2112011-01-04 14:01:29 -08001607 }
1608}
1609
Chong Zhangced1c2f2014-08-08 15:22:35 -07001610void NuPlayer::queueDecoderShutdown(
1611 bool audio, bool video, const sp<AMessage> &reply) {
1612 ALOGI("queueDecoderShutdown audio=%d, video=%d", audio, video);
Andreas Huber84066782011-08-16 09:34:26 -07001613
Chong Zhangced1c2f2014-08-08 15:22:35 -07001614 mDeferredActions.push_back(
Wei Jiafef808d2014-10-31 17:57:05 -07001615 new FlushDecoderAction(
1616 audio ? FLUSH_CMD_SHUTDOWN : FLUSH_CMD_NONE,
1617 video ? FLUSH_CMD_SHUTDOWN : FLUSH_CMD_NONE));
Andreas Huber84066782011-08-16 09:34:26 -07001618
Chong Zhangced1c2f2014-08-08 15:22:35 -07001619 mDeferredActions.push_back(
1620 new SimpleAction(&NuPlayer::performScanSources));
Andreas Huber84066782011-08-16 09:34:26 -07001621
Chong Zhangced1c2f2014-08-08 15:22:35 -07001622 mDeferredActions.push_back(new PostMessageAction(reply));
1623
1624 processDeferredActions();
Andreas Huber84066782011-08-16 09:34:26 -07001625}
1626
James Dong0d268a32012-08-31 12:18:27 -07001627status_t NuPlayer::setVideoScalingMode(int32_t mode) {
1628 mVideoScalingMode = mode;
Lajos Molnar1de1e252015-04-30 18:18:34 -07001629 if (mSurface != NULL) {
1630 status_t ret = native_window_set_scaling_mode(mSurface.get(), mVideoScalingMode);
James Dong0d268a32012-08-31 12:18:27 -07001631 if (ret != OK) {
1632 ALOGE("Failed to set scaling mode (%d): %s",
1633 -ret, strerror(-ret));
1634 return ret;
1635 }
1636 }
1637 return OK;
1638}
1639
Chong Zhangdcb89b32013-08-06 09:44:47 -07001640status_t NuPlayer::getTrackInfo(Parcel* reply) const {
Lajos Molnar1d15ab52015-03-04 16:46:34 -08001641 sp<AMessage> msg = new AMessage(kWhatGetTrackInfo, this);
Chong Zhangdcb89b32013-08-06 09:44:47 -07001642 msg->setPointer("reply", reply);
1643
1644 sp<AMessage> response;
1645 status_t err = msg->postAndAwaitResponse(&response);
1646 return err;
1647}
1648
Robert Shih7c4f0d72014-07-09 18:53:31 -07001649status_t NuPlayer::getSelectedTrack(int32_t type, Parcel* reply) const {
Lajos Molnar1d15ab52015-03-04 16:46:34 -08001650 sp<AMessage> msg = new AMessage(kWhatGetSelectedTrack, this);
Robert Shih7c4f0d72014-07-09 18:53:31 -07001651 msg->setPointer("reply", reply);
1652 msg->setInt32("type", type);
1653
1654 sp<AMessage> response;
1655 status_t err = msg->postAndAwaitResponse(&response);
1656 if (err == OK && response != NULL) {
1657 CHECK(response->findInt32("err", &err));
1658 }
1659 return err;
1660}
1661
Robert Shih6ffb1fd2014-10-29 16:24:32 -07001662status_t NuPlayer::selectTrack(size_t trackIndex, bool select, int64_t timeUs) {
Lajos Molnar1d15ab52015-03-04 16:46:34 -08001663 sp<AMessage> msg = new AMessage(kWhatSelectTrack, this);
Chong Zhangdcb89b32013-08-06 09:44:47 -07001664 msg->setSize("trackIndex", trackIndex);
1665 msg->setInt32("select", select);
Robert Shih6ffb1fd2014-10-29 16:24:32 -07001666 msg->setInt64("timeUs", timeUs);
Chong Zhangdcb89b32013-08-06 09:44:47 -07001667
1668 sp<AMessage> response;
1669 status_t err = msg->postAndAwaitResponse(&response);
1670
Chong Zhang404fced2014-06-11 14:45:31 -07001671 if (err != OK) {
1672 return err;
1673 }
1674
1675 if (!response->findInt32("err", &err)) {
1676 err = OK;
1677 }
1678
Chong Zhangdcb89b32013-08-06 09:44:47 -07001679 return err;
1680}
1681
Ronghua Wua73d9e02014-10-08 15:13:29 -07001682status_t NuPlayer::getCurrentPosition(int64_t *mediaUs) {
1683 sp<Renderer> renderer = mRenderer;
1684 if (renderer == NULL) {
1685 return NO_INIT;
1686 }
1687
1688 return renderer->getCurrentPosition(mediaUs);
1689}
1690
1691void NuPlayer::getStats(int64_t *numFramesTotal, int64_t *numFramesDropped) {
Chong Zhang7137ec72014-11-12 16:41:05 -08001692 sp<DecoderBase> decoder = getDecoder(false /* audio */);
1693 if (decoder != NULL) {
1694 decoder->getStats(numFramesTotal, numFramesDropped);
1695 } else {
1696 *numFramesTotal = 0;
1697 *numFramesDropped = 0;
1698 }
Ronghua Wua73d9e02014-10-08 15:13:29 -07001699}
1700
Marco Nelissenf0b72b52014-09-16 15:43:44 -07001701sp<MetaData> NuPlayer::getFileMeta() {
1702 return mSource->getFileFormatMeta();
1703}
1704
Andreas Huberb7c8e912012-11-27 15:02:53 -08001705void NuPlayer::schedulePollDuration() {
Lajos Molnar1d15ab52015-03-04 16:46:34 -08001706 sp<AMessage> msg = new AMessage(kWhatPollDuration, this);
Andreas Huberb7c8e912012-11-27 15:02:53 -08001707 msg->setInt32("generation", mPollDurationGeneration);
1708 msg->post();
1709}
1710
1711void NuPlayer::cancelPollDuration() {
1712 ++mPollDurationGeneration;
1713}
1714
Andreas Hubera1f8ab02012-11-30 10:53:22 -08001715void NuPlayer::processDeferredActions() {
1716 while (!mDeferredActions.empty()) {
1717 // We won't execute any deferred actions until we're no longer in
1718 // an intermediate state, i.e. one more more decoders are currently
1719 // flushing or shutting down.
1720
Andreas Hubera1f8ab02012-11-30 10:53:22 -08001721 if (mFlushingAudio != NONE || mFlushingVideo != NONE) {
1722 // We're currently flushing, postpone the reset until that's
1723 // completed.
1724
1725 ALOGV("postponing action mFlushingAudio=%d, mFlushingVideo=%d",
1726 mFlushingAudio, mFlushingVideo);
1727
1728 break;
1729 }
1730
1731 sp<Action> action = *mDeferredActions.begin();
1732 mDeferredActions.erase(mDeferredActions.begin());
1733
1734 action->execute(this);
1735 }
1736}
1737
Wei Jiae427abf2014-09-22 15:21:11 -07001738void NuPlayer::performSeek(int64_t seekTimeUs, bool needNotify) {
1739 ALOGV("performSeek seekTimeUs=%lld us (%.2f secs), needNotify(%d)",
Lajos Molnar6d339f12015-04-17 16:15:53 -07001740 (long long)seekTimeUs,
Wei Jiae427abf2014-09-22 15:21:11 -07001741 seekTimeUs / 1E6,
1742 needNotify);
Andreas Hubera1f8ab02012-11-30 10:53:22 -08001743
Andy Hungadf34bf2014-09-03 18:22:22 -07001744 if (mSource == NULL) {
1745 // This happens when reset occurs right before the loop mode
1746 // asynchronously seeks to the start of the stream.
1747 LOG_ALWAYS_FATAL_IF(mAudioDecoder != NULL || mVideoDecoder != NULL,
1748 "mSource is NULL and decoders not NULL audio(%p) video(%p)",
1749 mAudioDecoder.get(), mVideoDecoder.get());
1750 return;
1751 }
Andreas Hubera1f8ab02012-11-30 10:53:22 -08001752 mSource->seekTo(seekTimeUs);
Robert Shihd3b0bbb2014-07-23 15:00:25 -07001753 ++mTimedTextGeneration;
Andreas Hubera1f8ab02012-11-30 10:53:22 -08001754
Andreas Hubera1f8ab02012-11-30 10:53:22 -08001755 // everything's flushed, continue playback.
1756}
1757
Wei Jiafef808d2014-10-31 17:57:05 -07001758void NuPlayer::performDecoderFlush(FlushCommand audio, FlushCommand video) {
1759 ALOGV("performDecoderFlush audio=%d, video=%d", audio, video);
Andreas Hubera1f8ab02012-11-30 10:53:22 -08001760
Wei Jiafef808d2014-10-31 17:57:05 -07001761 if ((audio == FLUSH_CMD_NONE || mAudioDecoder == NULL)
1762 && (video == FLUSH_CMD_NONE || mVideoDecoder == NULL)) {
Andreas Hubera1f8ab02012-11-30 10:53:22 -08001763 return;
1764 }
1765
Wei Jiafef808d2014-10-31 17:57:05 -07001766 if (audio != FLUSH_CMD_NONE && mAudioDecoder != NULL) {
1767 flushDecoder(true /* audio */, (audio == FLUSH_CMD_SHUTDOWN));
Andreas Hubera1f8ab02012-11-30 10:53:22 -08001768 }
1769
Wei Jiafef808d2014-10-31 17:57:05 -07001770 if (video != FLUSH_CMD_NONE && mVideoDecoder != NULL) {
1771 flushDecoder(false /* audio */, (video == FLUSH_CMD_SHUTDOWN));
Andreas Hubera1f8ab02012-11-30 10:53:22 -08001772 }
1773}
1774
1775void NuPlayer::performReset() {
1776 ALOGV("performReset");
1777
1778 CHECK(mAudioDecoder == NULL);
1779 CHECK(mVideoDecoder == NULL);
1780
1781 cancelPollDuration();
1782
1783 ++mScanSourcesGeneration;
1784 mScanSourcesPending = false;
1785
Lajos Molnar09524832014-07-17 14:29:51 -07001786 if (mRendererLooper != NULL) {
1787 if (mRenderer != NULL) {
1788 mRendererLooper->unregisterHandler(mRenderer->id());
1789 }
1790 mRendererLooper->stop();
1791 mRendererLooper.clear();
1792 }
Andreas Hubera1f8ab02012-11-30 10:53:22 -08001793 mRenderer.clear();
Wei Jia57568df2014-09-22 10:16:29 -07001794 ++mRendererGeneration;
Andreas Hubera1f8ab02012-11-30 10:53:22 -08001795
1796 if (mSource != NULL) {
1797 mSource->stop();
Andreas Huberb5f25f02013-02-05 10:14:26 -08001798
Andreas Hubera1f8ab02012-11-30 10:53:22 -08001799 mSource.clear();
1800 }
1801
1802 if (mDriver != NULL) {
1803 sp<NuPlayerDriver> driver = mDriver.promote();
1804 if (driver != NULL) {
1805 driver->notifyResetComplete();
1806 }
1807 }
Andreas Huber57a339c2012-12-03 11:18:00 -08001808
1809 mStarted = false;
Andreas Hubera1f8ab02012-11-30 10:53:22 -08001810}
1811
1812void NuPlayer::performScanSources() {
1813 ALOGV("performScanSources");
1814
Andreas Huber57a339c2012-12-03 11:18:00 -08001815 if (!mStarted) {
1816 return;
1817 }
1818
Andreas Hubera1f8ab02012-11-30 10:53:22 -08001819 if (mAudioDecoder == NULL || mVideoDecoder == NULL) {
1820 postScanSources();
1821 }
1822}
1823
Lajos Molnar1de1e252015-04-30 18:18:34 -07001824void NuPlayer::performSetSurface(const sp<Surface> &surface) {
Andreas Huber57a339c2012-12-03 11:18:00 -08001825 ALOGV("performSetSurface");
1826
Lajos Molnar1de1e252015-04-30 18:18:34 -07001827 mSurface = surface;
Andreas Huber57a339c2012-12-03 11:18:00 -08001828
1829 // XXX - ignore error from setVideoScalingMode for now
1830 setVideoScalingMode(mVideoScalingMode);
Chong Zhang13d6faa2014-08-22 15:35:28 -07001831
1832 if (mDriver != NULL) {
1833 sp<NuPlayerDriver> driver = mDriver.promote();
1834 if (driver != NULL) {
1835 driver->notifySetSurfaceComplete();
1836 }
1837 }
Andreas Huber57a339c2012-12-03 11:18:00 -08001838}
1839
Chong Zhangf8d71772014-11-26 15:08:34 -08001840void NuPlayer::performResumeDecoders(bool needNotify) {
1841 if (needNotify) {
1842 mResumePending = true;
1843 if (mVideoDecoder == NULL) {
1844 // if audio-only, we can notify seek complete now,
1845 // as the resume operation will be relatively fast.
1846 finishResume();
1847 }
1848 }
1849
Chong Zhang7137ec72014-11-12 16:41:05 -08001850 if (mVideoDecoder != NULL) {
Chong Zhangf8d71772014-11-26 15:08:34 -08001851 // When there is continuous seek, MediaPlayer will cache the seek
1852 // position, and send down new seek request when previous seek is
1853 // complete. Let's wait for at least one video output frame before
1854 // notifying seek complete, so that the video thumbnail gets updated
1855 // when seekbar is dragged.
1856 mVideoDecoder->signalResume(needNotify);
Chong Zhang7137ec72014-11-12 16:41:05 -08001857 }
1858
1859 if (mAudioDecoder != NULL) {
Chong Zhangf8d71772014-11-26 15:08:34 -08001860 mAudioDecoder->signalResume(false /* needNotify */);
1861 }
1862}
1863
1864void NuPlayer::finishResume() {
1865 if (mResumePending) {
1866 mResumePending = false;
1867 if (mDriver != NULL) {
1868 sp<NuPlayerDriver> driver = mDriver.promote();
1869 if (driver != NULL) {
1870 driver->notifySeekComplete();
1871 }
1872 }
Chong Zhang7137ec72014-11-12 16:41:05 -08001873 }
1874}
1875
Andreas Huber9575c962013-02-05 13:59:56 -08001876void NuPlayer::onSourceNotify(const sp<AMessage> &msg) {
1877 int32_t what;
1878 CHECK(msg->findInt32("what", &what));
1879
1880 switch (what) {
Lajos Molnarfcd3e942015-03-31 10:06:48 -07001881 case Source::kWhatInstantiateSecureDecoders:
1882 {
1883 if (mSource == NULL) {
1884 // This is a stale notification from a source that was
1885 // asynchronously preparing when the client called reset().
1886 // We handled the reset, the source is gone.
1887 break;
1888 }
1889
1890 sp<AMessage> reply;
1891 CHECK(msg->findMessage("reply", &reply));
1892 status_t err = onInstantiateSecureDecoders();
1893 reply->setInt32("err", err);
1894 reply->post();
1895 break;
1896 }
1897
Andreas Huber9575c962013-02-05 13:59:56 -08001898 case Source::kWhatPrepared:
1899 {
Andreas Huberb5f28d42013-04-25 15:11:19 -07001900 if (mSource == NULL) {
1901 // This is a stale notification from a source that was
1902 // asynchronously preparing when the client called reset().
1903 // We handled the reset, the source is gone.
1904 break;
1905 }
1906
Andreas Huberec0c5972013-02-05 14:47:13 -08001907 int32_t err;
1908 CHECK(msg->findInt32("err", &err));
1909
Lajos Molnarfcd3e942015-03-31 10:06:48 -07001910 if (err != OK) {
1911 // shut down potential secure codecs in case client never calls reset
1912 mDeferredActions.push_back(
1913 new FlushDecoderAction(FLUSH_CMD_SHUTDOWN /* audio */,
1914 FLUSH_CMD_SHUTDOWN /* video */));
1915 processDeferredActions();
1916 }
1917
Andreas Huber9575c962013-02-05 13:59:56 -08001918 sp<NuPlayerDriver> driver = mDriver.promote();
1919 if (driver != NULL) {
Marco Nelissendd114d12014-05-28 15:23:14 -07001920 // notify duration first, so that it's definitely set when
1921 // the app received the "prepare complete" callback.
1922 int64_t durationUs;
1923 if (mSource->getDuration(&durationUs) == OK) {
1924 driver->notifyDuration(durationUs);
1925 }
Andreas Huberec0c5972013-02-05 14:47:13 -08001926 driver->notifyPrepareCompleted(err);
Andreas Huber9575c962013-02-05 13:59:56 -08001927 }
Andreas Huber99759402013-04-01 14:28:31 -07001928
Andreas Huber9575c962013-02-05 13:59:56 -08001929 break;
1930 }
1931
1932 case Source::kWhatFlagsChanged:
1933 {
1934 uint32_t flags;
1935 CHECK(msg->findInt32("flags", (int32_t *)&flags));
1936
Chong Zhang4b7069d2013-09-11 12:52:43 -07001937 sp<NuPlayerDriver> driver = mDriver.promote();
1938 if (driver != NULL) {
Wei Jia895651b2014-12-10 17:31:52 -08001939 if ((flags & NuPlayer::Source::FLAG_CAN_SEEK) == 0) {
1940 driver->notifyListener(
1941 MEDIA_INFO, MEDIA_INFO_NOT_SEEKABLE, 0);
1942 }
Chong Zhang4b7069d2013-09-11 12:52:43 -07001943 driver->notifyFlagsChanged(flags);
1944 }
1945
Andreas Huber9575c962013-02-05 13:59:56 -08001946 if ((mSourceFlags & Source::FLAG_DYNAMIC_DURATION)
1947 && (!(flags & Source::FLAG_DYNAMIC_DURATION))) {
1948 cancelPollDuration();
1949 } else if (!(mSourceFlags & Source::FLAG_DYNAMIC_DURATION)
1950 && (flags & Source::FLAG_DYNAMIC_DURATION)
1951 && (mAudioDecoder != NULL || mVideoDecoder != NULL)) {
1952 schedulePollDuration();
1953 }
1954
1955 mSourceFlags = flags;
1956 break;
1957 }
1958
1959 case Source::kWhatVideoSizeChanged:
1960 {
Chong Zhangced1c2f2014-08-08 15:22:35 -07001961 sp<AMessage> format;
1962 CHECK(msg->findMessage("format", &format));
Andreas Huber9575c962013-02-05 13:59:56 -08001963
Chong Zhangced1c2f2014-08-08 15:22:35 -07001964 updateVideoSize(format);
Andreas Huber9575c962013-02-05 13:59:56 -08001965 break;
1966 }
1967
Chong Zhang2a3cc9a2014-08-21 17:48:26 -07001968 case Source::kWhatBufferingUpdate:
1969 {
1970 int32_t percentage;
1971 CHECK(msg->findInt32("percentage", &percentage));
1972
1973 notifyListener(MEDIA_BUFFERING_UPDATE, percentage, 0);
1974 break;
1975 }
1976
Chong Zhangefbb6192015-01-30 17:13:27 -08001977 case Source::kWhatPauseOnBufferingStart:
1978 {
1979 // ignore if not playing
1980 if (mStarted && !mPausedByClient) {
1981 ALOGI("buffer low, pausing...");
1982
1983 onPause();
1984 }
1985 // fall-thru
1986 }
1987
Roger Jönssonb50e83e2013-01-21 16:26:41 +01001988 case Source::kWhatBufferingStart:
1989 {
1990 notifyListener(MEDIA_INFO, MEDIA_INFO_BUFFERING_START, 0);
1991 break;
1992 }
1993
Chong Zhangefbb6192015-01-30 17:13:27 -08001994 case Source::kWhatResumeOnBufferingEnd:
1995 {
1996 // ignore if not playing
1997 if (mStarted && !mPausedByClient) {
1998 ALOGI("buffer ready, resuming...");
1999
2000 onResume();
2001 }
2002 // fall-thru
2003 }
2004
Roger Jönssonb50e83e2013-01-21 16:26:41 +01002005 case Source::kWhatBufferingEnd:
2006 {
2007 notifyListener(MEDIA_INFO, MEDIA_INFO_BUFFERING_END, 0);
2008 break;
2009 }
2010
Chong Zhangefbb6192015-01-30 17:13:27 -08002011 case Source::kWhatCacheStats:
2012 {
2013 int32_t kbps;
2014 CHECK(msg->findInt32("bandwidth", &kbps));
2015
2016 notifyListener(MEDIA_INFO, MEDIA_INFO_NETWORK_BANDWIDTH, kbps);
2017 break;
2018 }
2019
Chong Zhangdcb89b32013-08-06 09:44:47 -07002020 case Source::kWhatSubtitleData:
2021 {
2022 sp<ABuffer> buffer;
2023 CHECK(msg->findBuffer("buffer", &buffer));
2024
Chong Zhang404fced2014-06-11 14:45:31 -07002025 sendSubtitleData(buffer, 0 /* baseIndex */);
Chong Zhangdcb89b32013-08-06 09:44:47 -07002026 break;
2027 }
2028
Robert Shih08528432015-04-08 09:06:54 -07002029 case Source::kWhatTimedMetaData:
2030 {
2031 sp<ABuffer> buffer;
2032 if (!msg->findBuffer("buffer", &buffer)) {
2033 notifyListener(MEDIA_INFO, MEDIA_INFO_METADATA_UPDATE, 0);
2034 } else {
2035 sendTimedMetaData(buffer);
2036 }
2037 break;
2038 }
2039
Robert Shihd3b0bbb2014-07-23 15:00:25 -07002040 case Source::kWhatTimedTextData:
2041 {
2042 int32_t generation;
2043 if (msg->findInt32("generation", &generation)
2044 && generation != mTimedTextGeneration) {
2045 break;
2046 }
2047
2048 sp<ABuffer> buffer;
2049 CHECK(msg->findBuffer("buffer", &buffer));
2050
2051 sp<NuPlayerDriver> driver = mDriver.promote();
2052 if (driver == NULL) {
2053 break;
2054 }
2055
2056 int posMs;
2057 int64_t timeUs, posUs;
2058 driver->getCurrentPosition(&posMs);
2059 posUs = posMs * 1000;
2060 CHECK(buffer->meta()->findInt64("timeUs", &timeUs));
2061
2062 if (posUs < timeUs) {
2063 if (!msg->findInt32("generation", &generation)) {
2064 msg->setInt32("generation", mTimedTextGeneration);
2065 }
2066 msg->post(timeUs - posUs);
2067 } else {
2068 sendTimedTextData(buffer);
2069 }
2070 break;
2071 }
2072
Andreas Huber14f76722013-01-15 09:04:18 -08002073 case Source::kWhatQueueDecoderShutdown:
2074 {
2075 int32_t audio, video;
2076 CHECK(msg->findInt32("audio", &audio));
2077 CHECK(msg->findInt32("video", &video));
2078
2079 sp<AMessage> reply;
2080 CHECK(msg->findMessage("reply", &reply));
2081
2082 queueDecoderShutdown(audio, video, reply);
2083 break;
2084 }
2085
Ronghua Wu80276872014-08-28 15:50:29 -07002086 case Source::kWhatDrmNoLicense:
2087 {
2088 notifyListener(MEDIA_ERROR, MEDIA_ERROR_UNKNOWN, ERROR_DRM_NO_LICENSE);
2089 break;
2090 }
2091
Andreas Huber9575c962013-02-05 13:59:56 -08002092 default:
2093 TRESPASS();
2094 }
2095}
2096
Chong Zhanga7fa1d92014-06-11 14:49:23 -07002097void NuPlayer::onClosedCaptionNotify(const sp<AMessage> &msg) {
2098 int32_t what;
2099 CHECK(msg->findInt32("what", &what));
2100
2101 switch (what) {
2102 case NuPlayer::CCDecoder::kWhatClosedCaptionData:
2103 {
2104 sp<ABuffer> buffer;
2105 CHECK(msg->findBuffer("buffer", &buffer));
2106
2107 size_t inbandTracks = 0;
2108 if (mSource != NULL) {
2109 inbandTracks = mSource->getTrackCount();
2110 }
2111
2112 sendSubtitleData(buffer, inbandTracks);
2113 break;
2114 }
2115
2116 case NuPlayer::CCDecoder::kWhatTrackAdded:
2117 {
2118 notifyListener(MEDIA_INFO, MEDIA_INFO_METADATA_UPDATE, 0);
2119
2120 break;
2121 }
2122
2123 default:
2124 TRESPASS();
2125 }
2126
2127
2128}
2129
Chong Zhang404fced2014-06-11 14:45:31 -07002130void NuPlayer::sendSubtitleData(const sp<ABuffer> &buffer, int32_t baseIndex) {
2131 int32_t trackIndex;
2132 int64_t timeUs, durationUs;
2133 CHECK(buffer->meta()->findInt32("trackIndex", &trackIndex));
2134 CHECK(buffer->meta()->findInt64("timeUs", &timeUs));
2135 CHECK(buffer->meta()->findInt64("durationUs", &durationUs));
2136
2137 Parcel in;
2138 in.writeInt32(trackIndex + baseIndex);
2139 in.writeInt64(timeUs);
2140 in.writeInt64(durationUs);
2141 in.writeInt32(buffer->size());
2142 in.writeInt32(buffer->size());
2143 in.write(buffer->data(), buffer->size());
2144
2145 notifyListener(MEDIA_SUBTITLE_DATA, 0, 0, &in);
2146}
Robert Shihd3b0bbb2014-07-23 15:00:25 -07002147
Robert Shih08528432015-04-08 09:06:54 -07002148void NuPlayer::sendTimedMetaData(const sp<ABuffer> &buffer) {
2149 int64_t timeUs;
2150 CHECK(buffer->meta()->findInt64("timeUs", &timeUs));
2151
2152 Parcel in;
2153 in.writeInt64(timeUs);
2154 in.writeInt32(buffer->size());
2155 in.writeInt32(buffer->size());
2156 in.write(buffer->data(), buffer->size());
2157
2158 notifyListener(MEDIA_META_DATA, 0, 0, &in);
2159}
2160
Robert Shihd3b0bbb2014-07-23 15:00:25 -07002161void NuPlayer::sendTimedTextData(const sp<ABuffer> &buffer) {
2162 const void *data;
2163 size_t size = 0;
2164 int64_t timeUs;
2165 int32_t flag = TextDescriptions::LOCAL_DESCRIPTIONS;
2166
2167 AString mime;
2168 CHECK(buffer->meta()->findString("mime", &mime));
2169 CHECK(strcasecmp(mime.c_str(), MEDIA_MIMETYPE_TEXT_3GPP) == 0);
2170
2171 data = buffer->data();
2172 size = buffer->size();
2173
2174 Parcel parcel;
2175 if (size > 0) {
2176 CHECK(buffer->meta()->findInt64("timeUs", &timeUs));
2177 flag |= TextDescriptions::IN_BAND_TEXT_3GPP;
2178 TextDescriptions::getParcelOfDescriptions(
2179 (const uint8_t *)data, size, flag, timeUs / 1000, &parcel);
2180 }
2181
2182 if ((parcel.dataSize() > 0)) {
2183 notifyListener(MEDIA_TIMED_TEXT, 0, 0, &parcel);
2184 } else { // send an empty timed text
2185 notifyListener(MEDIA_TIMED_TEXT, 0, 0);
2186 }
2187}
Andreas Huberb5f25f02013-02-05 10:14:26 -08002188////////////////////////////////////////////////////////////////////////////////
2189
Chong Zhangced1c2f2014-08-08 15:22:35 -07002190sp<AMessage> NuPlayer::Source::getFormat(bool audio) {
2191 sp<MetaData> meta = getFormatMeta(audio);
2192
2193 if (meta == NULL) {
2194 return NULL;
2195 }
2196
2197 sp<AMessage> msg = new AMessage;
2198
2199 if(convertMetaDataToMessage(meta, &msg) == OK) {
2200 return msg;
2201 }
2202 return NULL;
2203}
2204
Andreas Huber9575c962013-02-05 13:59:56 -08002205void NuPlayer::Source::notifyFlagsChanged(uint32_t flags) {
2206 sp<AMessage> notify = dupNotify();
2207 notify->setInt32("what", kWhatFlagsChanged);
2208 notify->setInt32("flags", flags);
2209 notify->post();
2210}
2211
Chong Zhangced1c2f2014-08-08 15:22:35 -07002212void NuPlayer::Source::notifyVideoSizeChanged(const sp<AMessage> &format) {
Andreas Huber9575c962013-02-05 13:59:56 -08002213 sp<AMessage> notify = dupNotify();
2214 notify->setInt32("what", kWhatVideoSizeChanged);
Chong Zhangced1c2f2014-08-08 15:22:35 -07002215 notify->setMessage("format", format);
Andreas Huber9575c962013-02-05 13:59:56 -08002216 notify->post();
2217}
2218
Andreas Huberec0c5972013-02-05 14:47:13 -08002219void NuPlayer::Source::notifyPrepared(status_t err) {
Andreas Huber9575c962013-02-05 13:59:56 -08002220 sp<AMessage> notify = dupNotify();
2221 notify->setInt32("what", kWhatPrepared);
Andreas Huberec0c5972013-02-05 14:47:13 -08002222 notify->setInt32("err", err);
Andreas Huber9575c962013-02-05 13:59:56 -08002223 notify->post();
2224}
2225
Lajos Molnarfcd3e942015-03-31 10:06:48 -07002226void NuPlayer::Source::notifyInstantiateSecureDecoders(const sp<AMessage> &reply) {
2227 sp<AMessage> notify = dupNotify();
2228 notify->setInt32("what", kWhatInstantiateSecureDecoders);
2229 notify->setMessage("reply", reply);
2230 notify->post();
2231}
2232
Andreas Huber84333e02014-02-07 15:36:10 -08002233void NuPlayer::Source::onMessageReceived(const sp<AMessage> & /* msg */) {
Andreas Huberb5f25f02013-02-05 10:14:26 -08002234 TRESPASS();
2235}
2236
Andreas Huberf9334412010-12-15 15:17:42 -08002237} // namespace android