blob: 8e3e46078b22a6189fc0b87c290104f77ad59c8d [file] [log] [blame]
Andreas Huberf9334412010-12-15 15:17:42 -08001/*
2 * Copyright (C) 2010 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17//#define LOG_NDEBUG 0
18#define LOG_TAG "NuPlayer"
19#include <utils/Log.h>
20
21#include "NuPlayer.h"
Andreas Huber5bc087c2010-12-23 10:27:40 -080022
23#include "HTTPLiveSource.h"
Chong Zhang7137ec72014-11-12 16:41:05 -080024#include "NuPlayerCCDecoder.h"
Andreas Huberf9334412010-12-15 15:17:42 -080025#include "NuPlayerDecoder.h"
Chong Zhang7137ec72014-11-12 16:41:05 -080026#include "NuPlayerDecoderBase.h"
Wei Jiabc2fb722014-07-08 16:37:57 -070027#include "NuPlayerDecoderPassThrough.h"
Andreas Huber43c3e6c2011-01-05 12:17:08 -080028#include "NuPlayerDriver.h"
Andreas Huberf9334412010-12-15 15:17:42 -080029#include "NuPlayerRenderer.h"
Andreas Huber5bc087c2010-12-23 10:27:40 -080030#include "NuPlayerSource.h"
Andreas Huber2bfdd422011-10-11 15:24:07 -070031#include "RTSPSource.h"
Andreas Huber5bc087c2010-12-23 10:27:40 -080032#include "StreamingSource.h"
Andreas Huberafed0e12011-09-20 15:39:58 -070033#include "GenericSource.h"
Robert Shihd3b0bbb2014-07-23 15:00:25 -070034#include "TextDescriptions.h"
Andreas Huber5bc087c2010-12-23 10:27:40 -080035
36#include "ATSParser.h"
Andreas Huberf9334412010-12-15 15:17:42 -080037
Lajos Molnard9fd6312014-11-06 11:00:00 -080038#include <cutils/properties.h>
39
Lajos Molnar3a474aa2015-04-24 17:10:07 -070040#include <media/AudioResamplerPublic.h>
41#include <media/AVSyncSettings.h>
42
Andreas Huber3831a062010-12-21 10:22:33 -080043#include <media/stagefright/foundation/hexdump.h>
Andreas Huberf9334412010-12-15 15:17:42 -080044#include <media/stagefright/foundation/ABuffer.h>
45#include <media/stagefright/foundation/ADebug.h>
46#include <media/stagefright/foundation/AMessage.h>
Lajos Molnar09524832014-07-17 14:29:51 -070047#include <media/stagefright/MediaBuffer.h>
Andreas Huber3fe62152011-09-16 15:09:22 -070048#include <media/stagefright/MediaDefs.h>
Andreas Huberf9334412010-12-15 15:17:42 -080049#include <media/stagefright/MediaErrors.h>
50#include <media/stagefright/MetaData.h>
Lajos Molnar1de1e252015-04-30 18:18:34 -070051
Andy McFadden8ba01022012-12-18 09:46:54 -080052#include <gui/IGraphicBufferProducer.h>
Lajos Molnar1de1e252015-04-30 18:18:34 -070053#include <gui/Surface.h>
Andreas Huberf9334412010-12-15 15:17:42 -080054
Andreas Huber3fe62152011-09-16 15:09:22 -070055#include "avc_utils.h"
56
Andreas Huber84066782011-08-16 09:34:26 -070057#include "ESDS.h"
58#include <media/stagefright/Utils.h>
59
Andreas Huberf9334412010-12-15 15:17:42 -080060namespace android {
61
Andreas Hubera1f8ab02012-11-30 10:53:22 -080062struct NuPlayer::Action : public RefBase {
63 Action() {}
64
65 virtual void execute(NuPlayer *player) = 0;
66
67private:
68 DISALLOW_EVIL_CONSTRUCTORS(Action);
69};
70
71struct NuPlayer::SeekAction : public Action {
Wei Jia29840802015-05-15 17:11:38 -070072 SeekAction(int64_t seekTimeUs)
73 : mSeekTimeUs(seekTimeUs) {
Andreas Hubera1f8ab02012-11-30 10:53:22 -080074 }
75
76 virtual void execute(NuPlayer *player) {
Wei Jia29840802015-05-15 17:11:38 -070077 player->performSeek(mSeekTimeUs);
Andreas Hubera1f8ab02012-11-30 10:53:22 -080078 }
79
80private:
81 int64_t mSeekTimeUs;
82
83 DISALLOW_EVIL_CONSTRUCTORS(SeekAction);
84};
85
Chong Zhangf8d71772014-11-26 15:08:34 -080086struct NuPlayer::ResumeDecoderAction : public Action {
87 ResumeDecoderAction(bool needNotify)
88 : mNeedNotify(needNotify) {
89 }
90
91 virtual void execute(NuPlayer *player) {
92 player->performResumeDecoders(mNeedNotify);
93 }
94
95private:
96 bool mNeedNotify;
97
98 DISALLOW_EVIL_CONSTRUCTORS(ResumeDecoderAction);
99};
100
Andreas Huber57a339c2012-12-03 11:18:00 -0800101struct NuPlayer::SetSurfaceAction : public Action {
Lajos Molnar1de1e252015-04-30 18:18:34 -0700102 SetSurfaceAction(const sp<Surface> &surface)
103 : mSurface(surface) {
Andreas Huber57a339c2012-12-03 11:18:00 -0800104 }
105
106 virtual void execute(NuPlayer *player) {
Lajos Molnar1de1e252015-04-30 18:18:34 -0700107 player->performSetSurface(mSurface);
Andreas Huber57a339c2012-12-03 11:18:00 -0800108 }
109
110private:
Lajos Molnar1de1e252015-04-30 18:18:34 -0700111 sp<Surface> mSurface;
Andreas Huber57a339c2012-12-03 11:18:00 -0800112
113 DISALLOW_EVIL_CONSTRUCTORS(SetSurfaceAction);
114};
115
Wei Jiafef808d2014-10-31 17:57:05 -0700116struct NuPlayer::FlushDecoderAction : public Action {
117 FlushDecoderAction(FlushCommand audio, FlushCommand video)
Andreas Huber14f76722013-01-15 09:04:18 -0800118 : mAudio(audio),
119 mVideo(video) {
120 }
121
122 virtual void execute(NuPlayer *player) {
Wei Jiafef808d2014-10-31 17:57:05 -0700123 player->performDecoderFlush(mAudio, mVideo);
Andreas Huber14f76722013-01-15 09:04:18 -0800124 }
125
126private:
Wei Jiafef808d2014-10-31 17:57:05 -0700127 FlushCommand mAudio;
128 FlushCommand mVideo;
Andreas Huber14f76722013-01-15 09:04:18 -0800129
Wei Jiafef808d2014-10-31 17:57:05 -0700130 DISALLOW_EVIL_CONSTRUCTORS(FlushDecoderAction);
Andreas Huber14f76722013-01-15 09:04:18 -0800131};
132
133struct NuPlayer::PostMessageAction : public Action {
134 PostMessageAction(const sp<AMessage> &msg)
135 : mMessage(msg) {
136 }
137
138 virtual void execute(NuPlayer *) {
139 mMessage->post();
140 }
141
142private:
143 sp<AMessage> mMessage;
144
145 DISALLOW_EVIL_CONSTRUCTORS(PostMessageAction);
146};
147
Andreas Hubera1f8ab02012-11-30 10:53:22 -0800148// Use this if there's no state necessary to save in order to execute
149// the action.
150struct NuPlayer::SimpleAction : public Action {
151 typedef void (NuPlayer::*ActionFunc)();
152
153 SimpleAction(ActionFunc func)
154 : mFunc(func) {
155 }
156
157 virtual void execute(NuPlayer *player) {
158 (player->*mFunc)();
159 }
160
161private:
162 ActionFunc mFunc;
163
164 DISALLOW_EVIL_CONSTRUCTORS(SimpleAction);
165};
166
Andreas Huberf9334412010-12-15 15:17:42 -0800167////////////////////////////////////////////////////////////////////////////////
168
Ronghua Wu68845c12015-07-21 09:50:48 -0700169NuPlayer::NuPlayer(pid_t pid)
Andreas Huber9b80c2b2011-06-30 15:47:02 -0700170 : mUIDValid(false),
Ronghua Wu68845c12015-07-21 09:50:48 -0700171 mPID(pid),
Andreas Huber9575c962013-02-05 13:59:56 -0800172 mSourceFlags(0),
Wei Jiabc2fb722014-07-08 16:37:57 -0700173 mOffloadAudio(false),
Wei Jia88703c32014-08-06 11:24:07 -0700174 mAudioDecoderGeneration(0),
175 mVideoDecoderGeneration(0),
Wei Jia57568df2014-09-22 10:16:29 -0700176 mRendererGeneration(0),
Andreas Huber9b80c2b2011-06-30 15:47:02 -0700177 mAudioEOS(false),
Andreas Huberf9334412010-12-15 15:17:42 -0800178 mVideoEOS(false),
Andreas Huber5bc087c2010-12-23 10:27:40 -0800179 mScanSourcesPending(false),
Andreas Huber1aef2112011-01-04 14:01:29 -0800180 mScanSourcesGeneration(0),
Andreas Huberb7c8e912012-11-27 15:02:53 -0800181 mPollDurationGeneration(0),
Robert Shihd3b0bbb2014-07-23 15:00:25 -0700182 mTimedTextGeneration(0),
Andreas Huberf9334412010-12-15 15:17:42 -0800183 mFlushingAudio(NONE),
Andreas Huber1aef2112011-01-04 14:01:29 -0800184 mFlushingVideo(NONE),
Chong Zhangf8d71772014-11-26 15:08:34 -0800185 mResumePending(false),
Andreas Huber57a339c2012-12-03 11:18:00 -0800186 mVideoScalingMode(NATIVE_WINDOW_SCALING_MODE_SCALE_TO_WINDOW),
Lajos Molnar3a474aa2015-04-24 17:10:07 -0700187 mPlaybackSettings(AUDIO_PLAYBACK_RATE_DEFAULT),
188 mVideoFpsHint(-1.f),
Chong Zhangefbb6192015-01-30 17:13:27 -0800189 mStarted(false),
Robert Shih0c61a0d2015-07-06 15:09:10 -0700190 mSourceStarted(false),
Chong Zhangefbb6192015-01-30 17:13:27 -0800191 mPaused(false),
Chong Zhang8a048332015-05-06 15:16:28 -0700192 mPausedByClient(false),
193 mPausedForBuffering(false) {
Andy Hung8d121d42014-10-03 09:53:53 -0700194 clearFlushComplete();
Andreas Huberf9334412010-12-15 15:17:42 -0800195}
196
197NuPlayer::~NuPlayer() {
198}
199
Andreas Huber9b80c2b2011-06-30 15:47:02 -0700200void NuPlayer::setUID(uid_t uid) {
201 mUIDValid = true;
202 mUID = uid;
203}
204
Andreas Huber43c3e6c2011-01-05 12:17:08 -0800205void NuPlayer::setDriver(const wp<NuPlayerDriver> &driver) {
206 mDriver = driver;
Andreas Huberf9334412010-12-15 15:17:42 -0800207}
208
Andreas Huber9575c962013-02-05 13:59:56 -0800209void NuPlayer::setDataSourceAsync(const sp<IStreamSource> &source) {
Lajos Molnar1d15ab52015-03-04 16:46:34 -0800210 sp<AMessage> msg = new AMessage(kWhatSetDataSource, this);
Andreas Huberf9334412010-12-15 15:17:42 -0800211
Lajos Molnar1d15ab52015-03-04 16:46:34 -0800212 sp<AMessage> notify = new AMessage(kWhatSourceNotify, this);
Andreas Huberb5f25f02013-02-05 10:14:26 -0800213
Andreas Huber240abcc2014-02-13 13:32:37 -0800214 msg->setObject("source", new StreamingSource(notify, source));
Andreas Huber5bc087c2010-12-23 10:27:40 -0800215 msg->post();
216}
Andreas Huberf9334412010-12-15 15:17:42 -0800217
Andreas Huberafed0e12011-09-20 15:39:58 -0700218static bool IsHTTPLiveURL(const char *url) {
219 if (!strncasecmp("http://", url, 7)
Andreas Huber99759402013-04-01 14:28:31 -0700220 || !strncasecmp("https://", url, 8)
221 || !strncasecmp("file://", url, 7)) {
Andreas Huberafed0e12011-09-20 15:39:58 -0700222 size_t len = strlen(url);
223 if (len >= 5 && !strcasecmp(".m3u8", &url[len - 5])) {
224 return true;
225 }
226
227 if (strstr(url,"m3u8")) {
228 return true;
229 }
230 }
231
232 return false;
233}
234
Andreas Huber9575c962013-02-05 13:59:56 -0800235void NuPlayer::setDataSourceAsync(
Andreas Huber1b86fe02014-01-29 11:13:26 -0800236 const sp<IMediaHTTPService> &httpService,
237 const char *url,
238 const KeyedVector<String8, String8> *headers) {
Chong Zhang3de157d2014-08-05 20:54:44 -0700239
Lajos Molnar1d15ab52015-03-04 16:46:34 -0800240 sp<AMessage> msg = new AMessage(kWhatSetDataSource, this);
Oscar Rydhé7a33b772012-02-20 10:15:48 +0100241 size_t len = strlen(url);
Andreas Huber5bc087c2010-12-23 10:27:40 -0800242
Lajos Molnar1d15ab52015-03-04 16:46:34 -0800243 sp<AMessage> notify = new AMessage(kWhatSourceNotify, this);
Andreas Huberb5f25f02013-02-05 10:14:26 -0800244
Andreas Huberafed0e12011-09-20 15:39:58 -0700245 sp<Source> source;
246 if (IsHTTPLiveURL(url)) {
Andreas Huber81e68442014-02-05 11:52:33 -0800247 source = new HTTPLiveSource(notify, httpService, url, headers);
Andreas Huberafed0e12011-09-20 15:39:58 -0700248 } else if (!strncasecmp(url, "rtsp://", 7)) {
Andreas Huber1b86fe02014-01-29 11:13:26 -0800249 source = new RTSPSource(
250 notify, httpService, url, headers, mUIDValid, mUID);
Oscar Rydhé7a33b772012-02-20 10:15:48 +0100251 } else if ((!strncasecmp(url, "http://", 7)
252 || !strncasecmp(url, "https://", 8))
253 && ((len >= 4 && !strcasecmp(".sdp", &url[len - 4]))
254 || strstr(url, ".sdp?"))) {
Andreas Huber1b86fe02014-01-29 11:13:26 -0800255 source = new RTSPSource(
256 notify, httpService, url, headers, mUIDValid, mUID, true);
Andreas Huber2bfdd422011-10-11 15:24:07 -0700257 } else {
Chong Zhang3de157d2014-08-05 20:54:44 -0700258 sp<GenericSource> genericSource =
259 new GenericSource(notify, mUIDValid, mUID);
260 // Don't set FLAG_SECURE on mSourceFlags here for widevine.
261 // The correct flags will be updated in Source::kWhatFlagsChanged
262 // handler when GenericSource is prepared.
Andreas Huber2bfdd422011-10-11 15:24:07 -0700263
Chong Zhanga19f33e2014-08-07 15:35:07 -0700264 status_t err = genericSource->setDataSource(httpService, url, headers);
Chong Zhang3de157d2014-08-05 20:54:44 -0700265
266 if (err == OK) {
267 source = genericSource;
268 } else {
Chong Zhanga19f33e2014-08-07 15:35:07 -0700269 ALOGE("Failed to set data source!");
Chong Zhang3de157d2014-08-05 20:54:44 -0700270 }
271 }
Andreas Huberafed0e12011-09-20 15:39:58 -0700272 msg->setObject("source", source);
273 msg->post();
274}
275
Andreas Huber9575c962013-02-05 13:59:56 -0800276void NuPlayer::setDataSourceAsync(int fd, int64_t offset, int64_t length) {
Lajos Molnar1d15ab52015-03-04 16:46:34 -0800277 sp<AMessage> msg = new AMessage(kWhatSetDataSource, this);
Andreas Huberafed0e12011-09-20 15:39:58 -0700278
Lajos Molnar1d15ab52015-03-04 16:46:34 -0800279 sp<AMessage> notify = new AMessage(kWhatSourceNotify, this);
Andreas Huberb5f25f02013-02-05 10:14:26 -0800280
Chong Zhang3de157d2014-08-05 20:54:44 -0700281 sp<GenericSource> source =
282 new GenericSource(notify, mUIDValid, mUID);
283
Chong Zhanga19f33e2014-08-07 15:35:07 -0700284 status_t err = source->setDataSource(fd, offset, length);
Chong Zhang3de157d2014-08-05 20:54:44 -0700285
286 if (err != OK) {
Chong Zhanga19f33e2014-08-07 15:35:07 -0700287 ALOGE("Failed to set data source!");
Chong Zhang3de157d2014-08-05 20:54:44 -0700288 source = NULL;
289 }
290
Andreas Huberafed0e12011-09-20 15:39:58 -0700291 msg->setObject("source", source);
Andreas Huberf9334412010-12-15 15:17:42 -0800292 msg->post();
293}
294
Chris Watkins99f31602015-03-20 13:06:33 -0700295void NuPlayer::setDataSourceAsync(const sp<DataSource> &dataSource) {
296 sp<AMessage> msg = new AMessage(kWhatSetDataSource, this);
297 sp<AMessage> notify = new AMessage(kWhatSourceNotify, this);
298
299 sp<GenericSource> source = new GenericSource(notify, mUIDValid, mUID);
300 status_t err = source->setDataSource(dataSource);
301
302 if (err != OK) {
303 ALOGE("Failed to set data source!");
304 source = NULL;
305 }
306
307 msg->setObject("source", source);
308 msg->post();
309}
310
Andreas Huber9575c962013-02-05 13:59:56 -0800311void NuPlayer::prepareAsync() {
Lajos Molnar1d15ab52015-03-04 16:46:34 -0800312 (new AMessage(kWhatPrepare, this))->post();
Andreas Huber9575c962013-02-05 13:59:56 -0800313}
314
Andreas Huber57a339c2012-12-03 11:18:00 -0800315void NuPlayer::setVideoSurfaceTextureAsync(
Andy McFadden8ba01022012-12-18 09:46:54 -0800316 const sp<IGraphicBufferProducer> &bufferProducer) {
Lajos Molnar1de1e252015-04-30 18:18:34 -0700317 sp<AMessage> msg = new AMessage(kWhatSetVideoSurface, this);
Andreas Huber57a339c2012-12-03 11:18:00 -0800318
Andy McFadden8ba01022012-12-18 09:46:54 -0800319 if (bufferProducer == NULL) {
Lajos Molnar1de1e252015-04-30 18:18:34 -0700320 msg->setObject("surface", NULL);
Andreas Huber57a339c2012-12-03 11:18:00 -0800321 } else {
Lajos Molnar1de1e252015-04-30 18:18:34 -0700322 msg->setObject("surface", new Surface(bufferProducer, true /* controlledByApp */));
Andreas Huber57a339c2012-12-03 11:18:00 -0800323 }
324
Andreas Huberf9334412010-12-15 15:17:42 -0800325 msg->post();
326}
327
328void NuPlayer::setAudioSink(const sp<MediaPlayerBase::AudioSink> &sink) {
Lajos Molnar1d15ab52015-03-04 16:46:34 -0800329 sp<AMessage> msg = new AMessage(kWhatSetAudioSink, this);
Andreas Huberf9334412010-12-15 15:17:42 -0800330 msg->setObject("sink", sink);
331 msg->post();
332}
333
334void NuPlayer::start() {
Lajos Molnar1d15ab52015-03-04 16:46:34 -0800335 (new AMessage(kWhatStart, this))->post();
Andreas Huberf9334412010-12-15 15:17:42 -0800336}
337
Lajos Molnar3a474aa2015-04-24 17:10:07 -0700338status_t NuPlayer::setPlaybackSettings(const AudioPlaybackRate &rate) {
339 // do some cursory validation of the settings here. audio modes are
340 // only validated when set on the audiosink.
341 if ((rate.mSpeed != 0.f && rate.mSpeed < AUDIO_TIMESTRETCH_SPEED_MIN)
342 || rate.mSpeed > AUDIO_TIMESTRETCH_SPEED_MAX
343 || rate.mPitch < AUDIO_TIMESTRETCH_SPEED_MIN
344 || rate.mPitch > AUDIO_TIMESTRETCH_SPEED_MAX) {
345 return BAD_VALUE;
346 }
347 sp<AMessage> msg = new AMessage(kWhatConfigPlayback, this);
348 writeToAMessage(msg, rate);
349 sp<AMessage> response;
350 status_t err = msg->postAndAwaitResponse(&response);
351 if (err == OK && response != NULL) {
352 CHECK(response->findInt32("err", &err));
353 }
354 return err;
355}
356
357status_t NuPlayer::getPlaybackSettings(AudioPlaybackRate *rate /* nonnull */) {
358 sp<AMessage> msg = new AMessage(kWhatGetPlaybackSettings, this);
359 sp<AMessage> response;
360 status_t err = msg->postAndAwaitResponse(&response);
361 if (err == OK && response != NULL) {
362 CHECK(response->findInt32("err", &err));
363 if (err == OK) {
364 readFromAMessage(response, rate);
365 }
366 }
367 return err;
368}
369
370status_t NuPlayer::setSyncSettings(const AVSyncSettings &sync, float videoFpsHint) {
371 sp<AMessage> msg = new AMessage(kWhatConfigSync, this);
372 writeToAMessage(msg, sync, videoFpsHint);
373 sp<AMessage> response;
374 status_t err = msg->postAndAwaitResponse(&response);
375 if (err == OK && response != NULL) {
376 CHECK(response->findInt32("err", &err));
377 }
378 return err;
379}
380
381status_t NuPlayer::getSyncSettings(
382 AVSyncSettings *sync /* nonnull */, float *videoFps /* nonnull */) {
383 sp<AMessage> msg = new AMessage(kWhatGetSyncSettings, this);
384 sp<AMessage> response;
385 status_t err = msg->postAndAwaitResponse(&response);
386 if (err == OK && response != NULL) {
387 CHECK(response->findInt32("err", &err));
388 if (err == OK) {
389 readFromAMessage(response, sync, videoFps);
390 }
391 }
392 return err;
Wei Jia98160162015-02-04 17:01:11 -0800393}
394
Andreas Huber43c3e6c2011-01-05 12:17:08 -0800395void NuPlayer::pause() {
Lajos Molnar1d15ab52015-03-04 16:46:34 -0800396 (new AMessage(kWhatPause, this))->post();
Andreas Huber43c3e6c2011-01-05 12:17:08 -0800397}
398
Andreas Huber1aef2112011-01-04 14:01:29 -0800399void NuPlayer::resetAsync() {
Chong Zhang48296b72014-09-14 14:28:45 -0700400 if (mSource != NULL) {
401 // During a reset, the data source might be unresponsive already, we need to
402 // disconnect explicitly so that reads exit promptly.
403 // We can't queue the disconnect request to the looper, as it might be
404 // queued behind a stuck read and never gets processed.
405 // Doing a disconnect outside the looper to allows the pending reads to exit
406 // (either successfully or with error).
407 mSource->disconnect();
408 }
409
Lajos Molnar1d15ab52015-03-04 16:46:34 -0800410 (new AMessage(kWhatReset, this))->post();
Andreas Huber1aef2112011-01-04 14:01:29 -0800411}
412
Wei Jiae427abf2014-09-22 15:21:11 -0700413void NuPlayer::seekToAsync(int64_t seekTimeUs, bool needNotify) {
Lajos Molnar1d15ab52015-03-04 16:46:34 -0800414 sp<AMessage> msg = new AMessage(kWhatSeek, this);
Andreas Huber43c3e6c2011-01-05 12:17:08 -0800415 msg->setInt64("seekTimeUs", seekTimeUs);
Wei Jiae427abf2014-09-22 15:21:11 -0700416 msg->setInt32("needNotify", needNotify);
Andreas Huber43c3e6c2011-01-05 12:17:08 -0800417 msg->post();
418}
419
Andreas Huber53df1a42010-12-22 10:03:04 -0800420
Chong Zhang404fced2014-06-11 14:45:31 -0700421void NuPlayer::writeTrackInfo(
422 Parcel* reply, const sp<AMessage> format) const {
423 int32_t trackType;
424 CHECK(format->findInt32("type", &trackType));
425
Robert Shih755106e2015-04-30 14:36:45 -0700426 AString mime;
Robert Shih2e3a4252015-05-06 10:21:15 -0700427 if (!format->findString("mime", &mime)) {
428 // Java MediaPlayer only uses mimetype for subtitle and timedtext tracks.
429 // If we can't find the mimetype here it means that we wouldn't be needing
430 // the mimetype on the Java end. We still write a placeholder mime to keep the
431 // (de)serialization logic simple.
432 if (trackType == MEDIA_TRACK_TYPE_AUDIO) {
433 mime = "audio/";
434 } else if (trackType == MEDIA_TRACK_TYPE_VIDEO) {
435 mime = "video/";
436 } else {
437 TRESPASS();
438 }
439 }
Robert Shih755106e2015-04-30 14:36:45 -0700440
Chong Zhang404fced2014-06-11 14:45:31 -0700441 AString lang;
442 CHECK(format->findString("language", &lang));
443
444 reply->writeInt32(2); // write something non-zero
445 reply->writeInt32(trackType);
Robert Shih755106e2015-04-30 14:36:45 -0700446 reply->writeString16(String16(mime.c_str()));
Chong Zhang404fced2014-06-11 14:45:31 -0700447 reply->writeString16(String16(lang.c_str()));
448
449 if (trackType == MEDIA_TRACK_TYPE_SUBTITLE) {
Chong Zhang404fced2014-06-11 14:45:31 -0700450 int32_t isAuto, isDefault, isForced;
451 CHECK(format->findInt32("auto", &isAuto));
452 CHECK(format->findInt32("default", &isDefault));
453 CHECK(format->findInt32("forced", &isForced));
454
Chong Zhang404fced2014-06-11 14:45:31 -0700455 reply->writeInt32(isAuto);
456 reply->writeInt32(isDefault);
457 reply->writeInt32(isForced);
458 }
459}
460
Andreas Huberf9334412010-12-15 15:17:42 -0800461void NuPlayer::onMessageReceived(const sp<AMessage> &msg) {
462 switch (msg->what()) {
463 case kWhatSetDataSource:
464 {
Steve Block3856b092011-10-20 11:56:00 +0100465 ALOGV("kWhatSetDataSource");
Andreas Huberf9334412010-12-15 15:17:42 -0800466
467 CHECK(mSource == NULL);
468
Chong Zhang3de157d2014-08-05 20:54:44 -0700469 status_t err = OK;
Andreas Huber5bc087c2010-12-23 10:27:40 -0800470 sp<RefBase> obj;
471 CHECK(msg->findObject("source", &obj));
Chong Zhang3de157d2014-08-05 20:54:44 -0700472 if (obj != NULL) {
473 mSource = static_cast<Source *>(obj.get());
Chong Zhang3de157d2014-08-05 20:54:44 -0700474 } else {
475 err = UNKNOWN_ERROR;
476 }
Andreas Huber9575c962013-02-05 13:59:56 -0800477
478 CHECK(mDriver != NULL);
479 sp<NuPlayerDriver> driver = mDriver.promote();
480 if (driver != NULL) {
Chong Zhang3de157d2014-08-05 20:54:44 -0700481 driver->notifySetDataSourceCompleted(err);
Andreas Huber9575c962013-02-05 13:59:56 -0800482 }
483 break;
484 }
485
486 case kWhatPrepare:
487 {
488 mSource->prepareAsync();
Andreas Huberf9334412010-12-15 15:17:42 -0800489 break;
490 }
491
Chong Zhangdcb89b32013-08-06 09:44:47 -0700492 case kWhatGetTrackInfo:
493 {
Lajos Molnar3f274362015-03-05 14:35:41 -0800494 sp<AReplyToken> replyID;
Chong Zhangdcb89b32013-08-06 09:44:47 -0700495 CHECK(msg->senderAwaitsResponse(&replyID));
496
Chong Zhang404fced2014-06-11 14:45:31 -0700497 Parcel* reply;
498 CHECK(msg->findPointer("reply", (void**)&reply));
499
500 size_t inbandTracks = 0;
Chong Zhangdcb89b32013-08-06 09:44:47 -0700501 if (mSource != NULL) {
Chong Zhang404fced2014-06-11 14:45:31 -0700502 inbandTracks = mSource->getTrackCount();
503 }
504
Chong Zhanga7fa1d92014-06-11 14:49:23 -0700505 size_t ccTracks = 0;
506 if (mCCDecoder != NULL) {
507 ccTracks = mCCDecoder->getTrackCount();
508 }
509
Chong Zhang404fced2014-06-11 14:45:31 -0700510 // total track count
Chong Zhanga7fa1d92014-06-11 14:49:23 -0700511 reply->writeInt32(inbandTracks + ccTracks);
Chong Zhang404fced2014-06-11 14:45:31 -0700512
513 // write inband tracks
514 for (size_t i = 0; i < inbandTracks; ++i) {
515 writeTrackInfo(reply, mSource->getTrackInfo(i));
Chong Zhangdcb89b32013-08-06 09:44:47 -0700516 }
517
Chong Zhanga7fa1d92014-06-11 14:49:23 -0700518 // write CC track
519 for (size_t i = 0; i < ccTracks; ++i) {
520 writeTrackInfo(reply, mCCDecoder->getTrackInfo(i));
521 }
522
Chong Zhangdcb89b32013-08-06 09:44:47 -0700523 sp<AMessage> response = new AMessage;
Chong Zhangdcb89b32013-08-06 09:44:47 -0700524 response->postReply(replyID);
525 break;
526 }
527
Robert Shih7c4f0d72014-07-09 18:53:31 -0700528 case kWhatGetSelectedTrack:
529 {
530 status_t err = INVALID_OPERATION;
531 if (mSource != NULL) {
532 err = OK;
533
534 int32_t type32;
535 CHECK(msg->findInt32("type", (int32_t*)&type32));
536 media_track_type type = (media_track_type)type32;
537 ssize_t selectedTrack = mSource->getSelectedTrack(type);
538
539 Parcel* reply;
540 CHECK(msg->findPointer("reply", (void**)&reply));
541 reply->writeInt32(selectedTrack);
542 }
543
544 sp<AMessage> response = new AMessage;
545 response->setInt32("err", err);
546
Lajos Molnar3f274362015-03-05 14:35:41 -0800547 sp<AReplyToken> replyID;
Robert Shih7c4f0d72014-07-09 18:53:31 -0700548 CHECK(msg->senderAwaitsResponse(&replyID));
549 response->postReply(replyID);
550 break;
551 }
552
Chong Zhangdcb89b32013-08-06 09:44:47 -0700553 case kWhatSelectTrack:
554 {
Lajos Molnar3f274362015-03-05 14:35:41 -0800555 sp<AReplyToken> replyID;
Chong Zhangdcb89b32013-08-06 09:44:47 -0700556 CHECK(msg->senderAwaitsResponse(&replyID));
557
Chong Zhang404fced2014-06-11 14:45:31 -0700558 size_t trackIndex;
559 int32_t select;
Robert Shih6ffb1fd2014-10-29 16:24:32 -0700560 int64_t timeUs;
Chong Zhang404fced2014-06-11 14:45:31 -0700561 CHECK(msg->findSize("trackIndex", &trackIndex));
562 CHECK(msg->findInt32("select", &select));
Robert Shih6ffb1fd2014-10-29 16:24:32 -0700563 CHECK(msg->findInt64("timeUs", &timeUs));
Chong Zhang404fced2014-06-11 14:45:31 -0700564
Chong Zhangdcb89b32013-08-06 09:44:47 -0700565 status_t err = INVALID_OPERATION;
Chong Zhang404fced2014-06-11 14:45:31 -0700566
567 size_t inbandTracks = 0;
Chong Zhangdcb89b32013-08-06 09:44:47 -0700568 if (mSource != NULL) {
Chong Zhang404fced2014-06-11 14:45:31 -0700569 inbandTracks = mSource->getTrackCount();
570 }
Chong Zhanga7fa1d92014-06-11 14:49:23 -0700571 size_t ccTracks = 0;
572 if (mCCDecoder != NULL) {
573 ccTracks = mCCDecoder->getTrackCount();
574 }
Chong Zhang404fced2014-06-11 14:45:31 -0700575
576 if (trackIndex < inbandTracks) {
Robert Shih6ffb1fd2014-10-29 16:24:32 -0700577 err = mSource->selectTrack(trackIndex, select, timeUs);
Robert Shihd3b0bbb2014-07-23 15:00:25 -0700578
579 if (!select && err == OK) {
580 int32_t type;
581 sp<AMessage> info = mSource->getTrackInfo(trackIndex);
582 if (info != NULL
583 && info->findInt32("type", &type)
584 && type == MEDIA_TRACK_TYPE_TIMEDTEXT) {
585 ++mTimedTextGeneration;
586 }
587 }
Chong Zhanga7fa1d92014-06-11 14:49:23 -0700588 } else {
589 trackIndex -= inbandTracks;
590
591 if (trackIndex < ccTracks) {
592 err = mCCDecoder->selectTrack(trackIndex, select);
593 }
Chong Zhangdcb89b32013-08-06 09:44:47 -0700594 }
595
596 sp<AMessage> response = new AMessage;
597 response->setInt32("err", err);
598
599 response->postReply(replyID);
600 break;
601 }
602
Andreas Huberb7c8e912012-11-27 15:02:53 -0800603 case kWhatPollDuration:
604 {
605 int32_t generation;
606 CHECK(msg->findInt32("generation", &generation));
607
608 if (generation != mPollDurationGeneration) {
609 // stale
610 break;
611 }
612
613 int64_t durationUs;
614 if (mDriver != NULL && mSource->getDuration(&durationUs) == OK) {
615 sp<NuPlayerDriver> driver = mDriver.promote();
616 if (driver != NULL) {
617 driver->notifyDuration(durationUs);
618 }
619 }
620
621 msg->post(1000000ll); // poll again in a second.
622 break;
623 }
624
Lajos Molnar1de1e252015-04-30 18:18:34 -0700625 case kWhatSetVideoSurface:
Andreas Huberf9334412010-12-15 15:17:42 -0800626 {
Andreas Huberf9334412010-12-15 15:17:42 -0800627
628 sp<RefBase> obj;
Lajos Molnar1de1e252015-04-30 18:18:34 -0700629 CHECK(msg->findObject("surface", &obj));
630 sp<Surface> surface = static_cast<Surface *>(obj.get());
Lajos Molnara81c6222015-07-10 19:17:45 -0700631
632 ALOGD("onSetVideoSurface(%p, %s video decoder)",
633 surface.get(),
634 (mSource != NULL && mSource->getFormat(false /* audio */) != NULL
635 && mVideoDecoder != NULL) ? "have" : "no");
636
637 if (mSource == NULL || mSource->getFormat(false /* audio */) == NULL
638 // NOTE: mVideoDecoder's mSurface is always non-null
639 || (mVideoDecoder != NULL && mVideoDecoder->setVideoSurface(surface) == OK)) {
Lajos Molnar1de1e252015-04-30 18:18:34 -0700640 performSetSurface(surface);
Wei Jiafef808d2014-10-31 17:57:05 -0700641 break;
642 }
643
644 mDeferredActions.push_back(
645 new FlushDecoderAction(FLUSH_CMD_FLUSH /* audio */,
646 FLUSH_CMD_SHUTDOWN /* video */));
647
Lajos Molnar1de1e252015-04-30 18:18:34 -0700648 mDeferredActions.push_back(new SetSurfaceAction(surface));
James Dong0d268a32012-08-31 12:18:27 -0700649
Wei Jiac6e58412015-07-10 18:04:55 -0700650 if (obj != NULL || mAudioDecoder != NULL) {
Wei Jiafef808d2014-10-31 17:57:05 -0700651 if (mStarted) {
Andy Hung73535852014-09-05 11:42:58 -0700652 // Issue a seek to refresh the video screen only if started otherwise
653 // the extractor may not yet be started and will assert.
654 // If the video decoder is not set (perhaps audio only in this case)
655 // do not perform a seek as it is not needed.
Ronghua Wua73d9e02014-10-08 15:13:29 -0700656 int64_t currentPositionUs = 0;
657 if (getCurrentPosition(&currentPositionUs) == OK) {
658 mDeferredActions.push_back(
Wei Jia29840802015-05-15 17:11:38 -0700659 new SeekAction(currentPositionUs));
Ronghua Wua73d9e02014-10-08 15:13:29 -0700660 }
Andy Hung73535852014-09-05 11:42:58 -0700661 }
Wei Jiaac428aa2014-09-02 19:01:34 -0700662
Andreas Huber57a339c2012-12-03 11:18:00 -0800663 // If there is a new surface texture, instantiate decoders
664 // again if possible.
665 mDeferredActions.push_back(
666 new SimpleAction(&NuPlayer::performScanSources));
667 }
668
Chong Zhangf8d71772014-11-26 15:08:34 -0800669 // After a flush without shutdown, decoder is paused.
670 // Don't resume it until source seek is done, otherwise it could
Chong Zhang7137ec72014-11-12 16:41:05 -0800671 // start pulling stale data too soon.
672 mDeferredActions.push_back(
Chong Zhangf8d71772014-11-26 15:08:34 -0800673 new ResumeDecoderAction(false /* needNotify */));
Chong Zhang7137ec72014-11-12 16:41:05 -0800674
Andreas Huber57a339c2012-12-03 11:18:00 -0800675 processDeferredActions();
Andreas Huberf9334412010-12-15 15:17:42 -0800676 break;
677 }
678
679 case kWhatSetAudioSink:
680 {
Steve Block3856b092011-10-20 11:56:00 +0100681 ALOGV("kWhatSetAudioSink");
Andreas Huberf9334412010-12-15 15:17:42 -0800682
683 sp<RefBase> obj;
684 CHECK(msg->findObject("sink", &obj));
685
686 mAudioSink = static_cast<MediaPlayerBase::AudioSink *>(obj.get());
687 break;
688 }
689
690 case kWhatStart:
691 {
Steve Block3856b092011-10-20 11:56:00 +0100692 ALOGV("kWhatStart");
Wei Jia94211742014-10-28 17:09:06 -0700693 if (mStarted) {
Chong Zhang8a048332015-05-06 15:16:28 -0700694 // do not resume yet if the source is still buffering
695 if (!mPausedForBuffering) {
696 onResume();
697 }
Wei Jia94211742014-10-28 17:09:06 -0700698 } else {
699 onStart();
Lajos Molnar09524832014-07-17 14:29:51 -0700700 }
Chong Zhangefbb6192015-01-30 17:13:27 -0800701 mPausedByClient = false;
Andreas Huberf9334412010-12-15 15:17:42 -0800702 break;
703 }
704
Lajos Molnar3a474aa2015-04-24 17:10:07 -0700705 case kWhatConfigPlayback:
Wei Jia98160162015-02-04 17:01:11 -0800706 {
Lajos Molnar3a474aa2015-04-24 17:10:07 -0700707 sp<AReplyToken> replyID;
708 CHECK(msg->senderAwaitsResponse(&replyID));
709 AudioPlaybackRate rate /* sanitized */;
710 readFromAMessage(msg, &rate);
711 status_t err = OK;
Wei Jia98160162015-02-04 17:01:11 -0800712 if (mRenderer != NULL) {
Lajos Molnar3a474aa2015-04-24 17:10:07 -0700713 err = mRenderer->setPlaybackSettings(rate);
714 }
715 if (err == OK) {
716 if (rate.mSpeed == 0.f) {
717 onPause();
718 // save all other settings (using non-paused speed)
719 // so we can restore them on start
720 AudioPlaybackRate newRate = rate;
721 newRate.mSpeed = mPlaybackSettings.mSpeed;
722 mPlaybackSettings = newRate;
723 } else { /* rate.mSpeed != 0.f */
724 onResume();
725 mPlaybackSettings = rate;
726 }
Wei Jia98160162015-02-04 17:01:11 -0800727 }
Ronghua Wu8db88132015-04-22 13:51:35 -0700728
729 if (mVideoDecoder != NULL) {
Ronghua Wuc8a70d32015-04-29 16:26:34 -0700730 float rate = getFrameRate();
731 if (rate > 0) {
Ronghua Wu8db88132015-04-22 13:51:35 -0700732 sp<AMessage> params = new AMessage();
Lajos Molnar3a474aa2015-04-24 17:10:07 -0700733 params->setFloat("operating-rate", rate * mPlaybackSettings.mSpeed);
Ronghua Wu8db88132015-04-22 13:51:35 -0700734 mVideoDecoder->setParameters(params);
735 }
736 }
737
Lajos Molnar3a474aa2015-04-24 17:10:07 -0700738 sp<AMessage> response = new AMessage;
739 response->setInt32("err", err);
740 response->postReply(replyID);
741 break;
742 }
743
744 case kWhatGetPlaybackSettings:
745 {
746 sp<AReplyToken> replyID;
747 CHECK(msg->senderAwaitsResponse(&replyID));
748 AudioPlaybackRate rate = mPlaybackSettings;
749 status_t err = OK;
750 if (mRenderer != NULL) {
751 err = mRenderer->getPlaybackSettings(&rate);
752 }
753 if (err == OK) {
754 // get playback settings used by renderer, as it may be
755 // slightly off due to audiosink not taking small changes.
756 mPlaybackSettings = rate;
757 if (mPaused) {
758 rate.mSpeed = 0.f;
759 }
760 }
761 sp<AMessage> response = new AMessage;
762 if (err == OK) {
763 writeToAMessage(response, rate);
764 }
765 response->setInt32("err", err);
766 response->postReply(replyID);
767 break;
768 }
769
770 case kWhatConfigSync:
771 {
772 sp<AReplyToken> replyID;
773 CHECK(msg->senderAwaitsResponse(&replyID));
774
775 ALOGV("kWhatConfigSync");
776 AVSyncSettings sync;
777 float videoFpsHint;
778 readFromAMessage(msg, &sync, &videoFpsHint);
779 status_t err = OK;
780 if (mRenderer != NULL) {
781 err = mRenderer->setSyncSettings(sync, videoFpsHint);
782 }
783 if (err == OK) {
784 mSyncSettings = sync;
785 mVideoFpsHint = videoFpsHint;
786 }
787 sp<AMessage> response = new AMessage;
788 response->setInt32("err", err);
789 response->postReply(replyID);
790 break;
791 }
792
793 case kWhatGetSyncSettings:
794 {
795 sp<AReplyToken> replyID;
796 CHECK(msg->senderAwaitsResponse(&replyID));
797 AVSyncSettings sync = mSyncSettings;
798 float videoFps = mVideoFpsHint;
799 status_t err = OK;
800 if (mRenderer != NULL) {
801 err = mRenderer->getSyncSettings(&sync, &videoFps);
802 if (err == OK) {
803 mSyncSettings = sync;
804 mVideoFpsHint = videoFps;
805 }
806 }
807 sp<AMessage> response = new AMessage;
808 if (err == OK) {
809 writeToAMessage(response, sync, videoFps);
810 }
811 response->setInt32("err", err);
812 response->postReply(replyID);
Wei Jia98160162015-02-04 17:01:11 -0800813 break;
814 }
815
Andreas Huberf9334412010-12-15 15:17:42 -0800816 case kWhatScanSources:
817 {
Andreas Huber1aef2112011-01-04 14:01:29 -0800818 int32_t generation;
819 CHECK(msg->findInt32("generation", &generation));
820 if (generation != mScanSourcesGeneration) {
821 // Drop obsolete msg.
822 break;
823 }
824
Andreas Huber5bc087c2010-12-23 10:27:40 -0800825 mScanSourcesPending = false;
826
Steve Block3856b092011-10-20 11:56:00 +0100827 ALOGV("scanning sources haveAudio=%d, haveVideo=%d",
Andreas Huber43c3e6c2011-01-05 12:17:08 -0800828 mAudioDecoder != NULL, mVideoDecoder != NULL);
829
Andreas Huberb7c8e912012-11-27 15:02:53 -0800830 bool mHadAnySourcesBefore =
831 (mAudioDecoder != NULL) || (mVideoDecoder != NULL);
832
Andy Hung282a7e32014-08-14 15:56:34 -0700833 // initialize video before audio because successful initialization of
834 // video may change deep buffer mode of audio.
Lajos Molnar1de1e252015-04-30 18:18:34 -0700835 if (mSurface != NULL) {
Haynes Mathew George5d246ef2012-07-09 10:36:57 -0700836 instantiateDecoder(false, &mVideoDecoder);
837 }
Andreas Huberf9334412010-12-15 15:17:42 -0800838
Ronghua Wua10fd232014-11-06 16:15:20 -0800839 // Don't try to re-open audio sink if there's an existing decoder.
840 if (mAudioSink != NULL && mAudioDecoder == NULL) {
Andreas Huber5bc087c2010-12-23 10:27:40 -0800841 instantiateDecoder(true, &mAudioDecoder);
Andreas Huberf9334412010-12-15 15:17:42 -0800842 }
843
Andreas Huberb7c8e912012-11-27 15:02:53 -0800844 if (!mHadAnySourcesBefore
845 && (mAudioDecoder != NULL || mVideoDecoder != NULL)) {
846 // This is the first time we've found anything playable.
847
Andreas Huber9575c962013-02-05 13:59:56 -0800848 if (mSourceFlags & Source::FLAG_DYNAMIC_DURATION) {
Andreas Huberb7c8e912012-11-27 15:02:53 -0800849 schedulePollDuration();
850 }
851 }
852
Andreas Hubereac68ba2011-09-27 12:12:25 -0700853 status_t err;
854 if ((err = mSource->feedMoreTSData()) != OK) {
Andreas Huber1aef2112011-01-04 14:01:29 -0800855 if (mAudioDecoder == NULL && mVideoDecoder == NULL) {
856 // We're not currently decoding anything (no audio or
857 // video tracks found) and we just ran out of input data.
Andreas Hubereac68ba2011-09-27 12:12:25 -0700858
859 if (err == ERROR_END_OF_STREAM) {
860 notifyListener(MEDIA_PLAYBACK_COMPLETE, 0, 0);
861 } else {
862 notifyListener(MEDIA_ERROR, MEDIA_ERROR_UNKNOWN, err);
863 }
Andreas Huber1aef2112011-01-04 14:01:29 -0800864 }
Andreas Huberf9334412010-12-15 15:17:42 -0800865 break;
866 }
867
Andreas Huberfbe9d812012-08-31 14:05:27 -0700868 if ((mAudioDecoder == NULL && mAudioSink != NULL)
Lajos Molnar1de1e252015-04-30 18:18:34 -0700869 || (mVideoDecoder == NULL && mSurface != NULL)) {
Andreas Huberf9334412010-12-15 15:17:42 -0800870 msg->post(100000ll);
Andreas Huber5bc087c2010-12-23 10:27:40 -0800871 mScanSourcesPending = true;
Andreas Huberf9334412010-12-15 15:17:42 -0800872 }
873 break;
874 }
875
876 case kWhatVideoNotify:
877 case kWhatAudioNotify:
878 {
879 bool audio = msg->what() == kWhatAudioNotify;
880
Wei Jia88703c32014-08-06 11:24:07 -0700881 int32_t currentDecoderGeneration =
882 (audio? mAudioDecoderGeneration : mVideoDecoderGeneration);
883 int32_t requesterGeneration = currentDecoderGeneration - 1;
884 CHECK(msg->findInt32("generation", &requesterGeneration));
885
886 if (requesterGeneration != currentDecoderGeneration) {
887 ALOGV("got message from old %s decoder, generation(%d:%d)",
888 audio ? "audio" : "video", requesterGeneration,
889 currentDecoderGeneration);
890 sp<AMessage> reply;
891 if (!(msg->findMessage("reply", &reply))) {
892 return;
893 }
894
895 reply->setInt32("err", INFO_DISCONTINUITY);
896 reply->post();
897 return;
898 }
899
Andreas Huberf9334412010-12-15 15:17:42 -0800900 int32_t what;
Lajos Molnar1cd13982014-01-17 15:12:51 -0800901 CHECK(msg->findInt32("what", &what));
Andreas Huberf9334412010-12-15 15:17:42 -0800902
Chong Zhang7137ec72014-11-12 16:41:05 -0800903 if (what == DecoderBase::kWhatInputDiscontinuity) {
904 int32_t formatChange;
905 CHECK(msg->findInt32("formatChange", &formatChange));
Andreas Huberf9334412010-12-15 15:17:42 -0800906
Chong Zhang7137ec72014-11-12 16:41:05 -0800907 ALOGV("%s discontinuity: formatChange %d",
908 audio ? "audio" : "video", formatChange);
909
910 if (formatChange) {
911 mDeferredActions.push_back(
912 new FlushDecoderAction(
913 audio ? FLUSH_CMD_SHUTDOWN : FLUSH_CMD_NONE,
914 audio ? FLUSH_CMD_NONE : FLUSH_CMD_SHUTDOWN));
Andreas Huberf9334412010-12-15 15:17:42 -0800915 }
Chong Zhang7137ec72014-11-12 16:41:05 -0800916
917 mDeferredActions.push_back(
918 new SimpleAction(
919 &NuPlayer::performScanSources));
920
921 processDeferredActions();
922 } else if (what == DecoderBase::kWhatEOS) {
Andreas Huberdc9bacd2011-09-26 10:53:29 -0700923 int32_t err;
Lajos Molnar1cd13982014-01-17 15:12:51 -0800924 CHECK(msg->findInt32("err", &err));
Andreas Huberdc9bacd2011-09-26 10:53:29 -0700925
926 if (err == ERROR_END_OF_STREAM) {
Steve Block3856b092011-10-20 11:56:00 +0100927 ALOGV("got %s decoder EOS", audio ? "audio" : "video");
Andreas Huberdc9bacd2011-09-26 10:53:29 -0700928 } else {
Steve Block3856b092011-10-20 11:56:00 +0100929 ALOGV("got %s decoder EOS w/ error %d",
Andreas Huberdc9bacd2011-09-26 10:53:29 -0700930 audio ? "audio" : "video",
931 err);
932 }
933
934 mRenderer->queueEOS(audio, err);
Chong Zhang7137ec72014-11-12 16:41:05 -0800935 } else if (what == DecoderBase::kWhatFlushCompleted) {
Steve Block3856b092011-10-20 11:56:00 +0100936 ALOGV("decoder %s flush completed", audio ? "audio" : "video");
Andreas Huberf9334412010-12-15 15:17:42 -0800937
Andy Hung8d121d42014-10-03 09:53:53 -0700938 handleFlushComplete(audio, true /* isDecoder */);
Andreas Huber3831a062010-12-21 10:22:33 -0800939 finishFlushIfPossible();
Chong Zhang7137ec72014-11-12 16:41:05 -0800940 } else if (what == DecoderBase::kWhatVideoSizeChanged) {
Lajos Molnar1cd13982014-01-17 15:12:51 -0800941 sp<AMessage> format;
942 CHECK(msg->findMessage("format", &format));
943
Wei Jiac6cfd702014-11-11 16:33:20 -0800944 sp<AMessage> inputFormat =
945 mSource->getFormat(false /* audio */);
Andreas Huber3831a062010-12-21 10:22:33 -0800946
Wei Jiac6cfd702014-11-11 16:33:20 -0800947 updateVideoSize(inputFormat, format);
Chong Zhang7137ec72014-11-12 16:41:05 -0800948 } else if (what == DecoderBase::kWhatShutdownCompleted) {
Steve Block3856b092011-10-20 11:56:00 +0100949 ALOGV("%s shutdown completed", audio ? "audio" : "video");
Andreas Huber3831a062010-12-21 10:22:33 -0800950 if (audio) {
951 mAudioDecoder.clear();
Wei Jia57568df2014-09-22 10:16:29 -0700952 ++mAudioDecoderGeneration;
Andreas Huber3831a062010-12-21 10:22:33 -0800953
954 CHECK_EQ((int)mFlushingAudio, (int)SHUTTING_DOWN_DECODER);
955 mFlushingAudio = SHUT_DOWN;
956 } else {
957 mVideoDecoder.clear();
Wei Jia57568df2014-09-22 10:16:29 -0700958 ++mVideoDecoderGeneration;
Andreas Huber3831a062010-12-21 10:22:33 -0800959
960 CHECK_EQ((int)mFlushingVideo, (int)SHUTTING_DOWN_DECODER);
961 mFlushingVideo = SHUT_DOWN;
962 }
963
964 finishFlushIfPossible();
Chong Zhangf8d71772014-11-26 15:08:34 -0800965 } else if (what == DecoderBase::kWhatResumeCompleted) {
966 finishResume();
Chong Zhang7137ec72014-11-12 16:41:05 -0800967 } else if (what == DecoderBase::kWhatError) {
Chong Zhangf4c0a942014-08-11 15:14:10 -0700968 status_t err;
Andy Hung2abde2c2014-09-30 14:40:32 -0700969 if (!msg->findInt32("err", &err) || err == OK) {
Chong Zhangf4c0a942014-08-11 15:14:10 -0700970 err = UNKNOWN_ERROR;
971 }
Andy Hungcf31f1e2014-09-23 14:59:01 -0700972
Andy Hung2abde2c2014-09-30 14:40:32 -0700973 // Decoder errors can be due to Source (e.g. from streaming),
974 // or from decoding corrupted bitstreams, or from other decoder
975 // MediaCodec operations (e.g. from an ongoing reset or seek).
Andy Hung202bce12014-12-03 11:47:36 -0800976 // They may also be due to openAudioSink failure at
977 // decoder start or after a format change.
Andy Hung2abde2c2014-09-30 14:40:32 -0700978 //
979 // We try to gracefully shut down the affected decoder if possible,
980 // rather than trying to force the shutdown with something
981 // similar to performReset(). This method can lead to a hang
982 // if MediaCodec functions block after an error, but they should
983 // typically return INVALID_OPERATION instead of blocking.
984
985 FlushStatus *flushing = audio ? &mFlushingAudio : &mFlushingVideo;
986 ALOGE("received error(%#x) from %s decoder, flushing(%d), now shutting down",
987 err, audio ? "audio" : "video", *flushing);
988
989 switch (*flushing) {
990 case NONE:
991 mDeferredActions.push_back(
Wei Jiafef808d2014-10-31 17:57:05 -0700992 new FlushDecoderAction(
993 audio ? FLUSH_CMD_SHUTDOWN : FLUSH_CMD_NONE,
994 audio ? FLUSH_CMD_NONE : FLUSH_CMD_SHUTDOWN));
Andy Hung2abde2c2014-09-30 14:40:32 -0700995 processDeferredActions();
996 break;
997 case FLUSHING_DECODER:
998 *flushing = FLUSHING_DECODER_SHUTDOWN; // initiate shutdown after flush.
999 break; // Wait for flush to complete.
1000 case FLUSHING_DECODER_SHUTDOWN:
1001 break; // Wait for flush to complete.
1002 case SHUTTING_DOWN_DECODER:
1003 break; // Wait for shutdown to complete.
1004 case FLUSHED:
1005 // Widevine source reads must stop before releasing the video decoder.
1006 if (!audio && mSource != NULL && mSourceFlags & Source::FLAG_SECURE) {
1007 mSource->stop();
Robert Shih0c61a0d2015-07-06 15:09:10 -07001008 mSourceStarted = false;
Andy Hung2abde2c2014-09-30 14:40:32 -07001009 }
1010 getDecoder(audio)->initiateShutdown(); // In the middle of a seek.
1011 *flushing = SHUTTING_DOWN_DECODER; // Shut down.
1012 break;
1013 case SHUT_DOWN:
1014 finishFlushIfPossible(); // Should not occur.
1015 break; // Finish anyways.
Marco Nelissen9e2b7912014-08-18 16:13:03 -07001016 }
Andy Hung2abde2c2014-09-30 14:40:32 -07001017 notifyListener(MEDIA_ERROR, MEDIA_ERROR_UNKNOWN, err);
Lajos Molnar1cd13982014-01-17 15:12:51 -08001018 } else {
1019 ALOGV("Unhandled decoder notification %d '%c%c%c%c'.",
Andreas Hubera1f8ab02012-11-30 10:53:22 -08001020 what,
1021 what >> 24,
1022 (what >> 16) & 0xff,
1023 (what >> 8) & 0xff,
1024 what & 0xff);
Andreas Huberf9334412010-12-15 15:17:42 -08001025 }
1026
1027 break;
1028 }
1029
1030 case kWhatRendererNotify:
1031 {
Wei Jia57568df2014-09-22 10:16:29 -07001032 int32_t requesterGeneration = mRendererGeneration - 1;
1033 CHECK(msg->findInt32("generation", &requesterGeneration));
1034 if (requesterGeneration != mRendererGeneration) {
1035 ALOGV("got message from old renderer, generation(%d:%d)",
1036 requesterGeneration, mRendererGeneration);
1037 return;
1038 }
1039
Andreas Huberf9334412010-12-15 15:17:42 -08001040 int32_t what;
1041 CHECK(msg->findInt32("what", &what));
1042
1043 if (what == Renderer::kWhatEOS) {
1044 int32_t audio;
1045 CHECK(msg->findInt32("audio", &audio));
1046
Andreas Huberc92fd242011-08-16 13:48:44 -07001047 int32_t finalResult;
1048 CHECK(msg->findInt32("finalResult", &finalResult));
1049
Andreas Huberf9334412010-12-15 15:17:42 -08001050 if (audio) {
1051 mAudioEOS = true;
1052 } else {
1053 mVideoEOS = true;
1054 }
1055
Andreas Huberc92fd242011-08-16 13:48:44 -07001056 if (finalResult == ERROR_END_OF_STREAM) {
Steve Block3856b092011-10-20 11:56:00 +01001057 ALOGV("reached %s EOS", audio ? "audio" : "video");
Andreas Huberc92fd242011-08-16 13:48:44 -07001058 } else {
Steve Block29357bc2012-01-06 19:20:56 +00001059 ALOGE("%s track encountered an error (%d)",
Andreas Huberc92fd242011-08-16 13:48:44 -07001060 audio ? "audio" : "video", finalResult);
1061
1062 notifyListener(
1063 MEDIA_ERROR, MEDIA_ERROR_UNKNOWN, finalResult);
1064 }
Andreas Huberf9334412010-12-15 15:17:42 -08001065
1066 if ((mAudioEOS || mAudioDecoder == NULL)
1067 && (mVideoEOS || mVideoDecoder == NULL)) {
1068 notifyListener(MEDIA_PLAYBACK_COMPLETE, 0, 0);
1069 }
Andreas Huber3fe62152011-09-16 15:09:22 -07001070 } else if (what == Renderer::kWhatFlushComplete) {
Andreas Huberf9334412010-12-15 15:17:42 -08001071 int32_t audio;
1072 CHECK(msg->findInt32("audio", &audio));
1073
Steve Block3856b092011-10-20 11:56:00 +01001074 ALOGV("renderer %s flush completed.", audio ? "audio" : "video");
Wei Jiada4252f2015-07-14 18:15:28 -07001075 if (audio && (mFlushingAudio == NONE || mFlushingAudio == FLUSHED
1076 || mFlushingAudio == SHUT_DOWN)) {
1077 // Flush has been handled by tear down.
1078 break;
1079 }
Andy Hung8d121d42014-10-03 09:53:53 -07001080 handleFlushComplete(audio, false /* isDecoder */);
1081 finishFlushIfPossible();
James Dongf57b4ea2012-07-20 13:38:36 -07001082 } else if (what == Renderer::kWhatVideoRenderingStart) {
1083 notifyListener(MEDIA_INFO, MEDIA_INFO_RENDERING_START, 0);
Lajos Molnarcbaffcf2013-08-14 18:30:38 -07001084 } else if (what == Renderer::kWhatMediaRenderingStart) {
1085 ALOGV("media rendering started");
1086 notifyListener(MEDIA_STARTED, 0, 0);
Ronghua Wufaeb0f22015-05-21 12:20:21 -07001087 } else if (what == Renderer::kWhatAudioTearDown) {
Ronghua Wu08529172014-10-02 16:55:52 -07001088 int32_t reason;
1089 CHECK(msg->findInt32("reason", &reason));
Ronghua Wufaeb0f22015-05-21 12:20:21 -07001090 ALOGV("Tear down audio with reason %d.", reason);
Wei Jia3a2956d2014-07-22 16:01:33 -07001091 mAudioDecoder.clear();
Wei Jia57568df2014-09-22 10:16:29 -07001092 ++mAudioDecoderGeneration;
Wei Jiada4252f2015-07-14 18:15:28 -07001093 bool needsToCreateAudioDecoder = true;
1094 if (mFlushingAudio == FLUSHING_DECODER) {
1095 mFlushComplete[1 /* audio */][1 /* isDecoder */] = true;
1096 mFlushingAudio = FLUSHED;
1097 finishFlushIfPossible();
1098 } else if (mFlushingAudio == FLUSHING_DECODER_SHUTDOWN
1099 || mFlushingAudio == SHUTTING_DOWN_DECODER) {
1100 mFlushComplete[1 /* audio */][1 /* isDecoder */] = true;
1101 mFlushingAudio = SHUT_DOWN;
1102 finishFlushIfPossible();
1103 needsToCreateAudioDecoder = false;
1104 }
1105 if (mRenderer == NULL) {
1106 break;
1107 }
1108 closeAudioSink();
Chong Zhang7137ec72014-11-12 16:41:05 -08001109 mRenderer->flush(
1110 true /* audio */, false /* notifyComplete */);
Wei Jia3a2956d2014-07-22 16:01:33 -07001111 if (mVideoDecoder != NULL) {
Chong Zhang7137ec72014-11-12 16:41:05 -08001112 mRenderer->flush(
1113 false /* audio */, false /* notifyComplete */);
Wei Jia3a2956d2014-07-22 16:01:33 -07001114 }
Wei Jia3a2956d2014-07-22 16:01:33 -07001115
Wei Jiada4252f2015-07-14 18:15:28 -07001116 int64_t positionUs;
1117 CHECK(msg->findInt64("positionUs", &positionUs));
Wei Jia29840802015-05-15 17:11:38 -07001118 performSeek(positionUs);
Ronghua Wufaeb0f22015-05-21 12:20:21 -07001119
Wei Jiada4252f2015-07-14 18:15:28 -07001120 if (reason == Renderer::kDueToError && needsToCreateAudioDecoder) {
Ronghua Wu08529172014-10-02 16:55:52 -07001121 instantiateDecoder(true /* audio */, &mAudioDecoder);
1122 }
Andreas Huberf9334412010-12-15 15:17:42 -08001123 }
1124 break;
1125 }
1126
1127 case kWhatMoreDataQueued:
1128 {
1129 break;
1130 }
1131
Andreas Huber1aef2112011-01-04 14:01:29 -08001132 case kWhatReset:
1133 {
Steve Block3856b092011-10-20 11:56:00 +01001134 ALOGV("kWhatReset");
Andreas Huber1aef2112011-01-04 14:01:29 -08001135
Andreas Hubera1f8ab02012-11-30 10:53:22 -08001136 mDeferredActions.push_back(
Wei Jiafef808d2014-10-31 17:57:05 -07001137 new FlushDecoderAction(
1138 FLUSH_CMD_SHUTDOWN /* audio */,
1139 FLUSH_CMD_SHUTDOWN /* video */));
Andreas Huberb7c8e912012-11-27 15:02:53 -08001140
Andreas Hubera1f8ab02012-11-30 10:53:22 -08001141 mDeferredActions.push_back(
1142 new SimpleAction(&NuPlayer::performReset));
Andreas Huberb58ce9f2011-11-28 16:27:35 -08001143
Andreas Hubera1f8ab02012-11-30 10:53:22 -08001144 processDeferredActions();
Andreas Huber1aef2112011-01-04 14:01:29 -08001145 break;
1146 }
1147
Andreas Huber43c3e6c2011-01-05 12:17:08 -08001148 case kWhatSeek:
1149 {
1150 int64_t seekTimeUs;
Wei Jiae427abf2014-09-22 15:21:11 -07001151 int32_t needNotify;
Andreas Huber43c3e6c2011-01-05 12:17:08 -08001152 CHECK(msg->findInt64("seekTimeUs", &seekTimeUs));
Wei Jiae427abf2014-09-22 15:21:11 -07001153 CHECK(msg->findInt32("needNotify", &needNotify));
Andreas Huber43c3e6c2011-01-05 12:17:08 -08001154
Wei Jiae427abf2014-09-22 15:21:11 -07001155 ALOGV("kWhatSeek seekTimeUs=%lld us, needNotify=%d",
Lajos Molnar6d339f12015-04-17 16:15:53 -07001156 (long long)seekTimeUs, needNotify);
Andreas Huber43c3e6c2011-01-05 12:17:08 -08001157
Wei Jia1061c9c2015-05-19 16:02:17 -07001158 if (!mStarted) {
1159 // Seek before the player is started. In order to preview video,
1160 // need to start the player and pause it. This branch is called
1161 // only once if needed. After the player is started, any seek
1162 // operation will go through normal path.
Robert Shih0c61a0d2015-07-06 15:09:10 -07001163 // Audio-only cases are handled separately.
Wei Jia1061c9c2015-05-19 16:02:17 -07001164 onStart(seekTimeUs);
Robert Shih0c61a0d2015-07-06 15:09:10 -07001165 if (mStarted) {
1166 onPause();
1167 mPausedByClient = true;
1168 }
Wei Jia1061c9c2015-05-19 16:02:17 -07001169 if (needNotify) {
1170 notifyDriverSeekComplete();
1171 }
1172 break;
1173 }
1174
Andreas Hubera1f8ab02012-11-30 10:53:22 -08001175 mDeferredActions.push_back(
Wei Jiafef808d2014-10-31 17:57:05 -07001176 new FlushDecoderAction(FLUSH_CMD_FLUSH /* audio */,
1177 FLUSH_CMD_FLUSH /* video */));
Andreas Huber43c3e6c2011-01-05 12:17:08 -08001178
Wei Jiae427abf2014-09-22 15:21:11 -07001179 mDeferredActions.push_back(
Wei Jia29840802015-05-15 17:11:38 -07001180 new SeekAction(seekTimeUs));
Andreas Huber43c3e6c2011-01-05 12:17:08 -08001181
Chong Zhangf8d71772014-11-26 15:08:34 -08001182 // After a flush without shutdown, decoder is paused.
1183 // Don't resume it until source seek is done, otherwise it could
Chong Zhang7137ec72014-11-12 16:41:05 -08001184 // start pulling stale data too soon.
1185 mDeferredActions.push_back(
Chong Zhangf8d71772014-11-26 15:08:34 -08001186 new ResumeDecoderAction(needNotify));
Chong Zhang7137ec72014-11-12 16:41:05 -08001187
Andreas Hubera1f8ab02012-11-30 10:53:22 -08001188 processDeferredActions();
Andreas Huber43c3e6c2011-01-05 12:17:08 -08001189 break;
1190 }
1191
Andreas Huberb4082222011-01-20 15:23:04 -08001192 case kWhatPause:
1193 {
Chong Zhangefbb6192015-01-30 17:13:27 -08001194 onPause();
1195 mPausedByClient = true;
Andreas Huberb4082222011-01-20 15:23:04 -08001196 break;
1197 }
1198
Andreas Huberb5f25f02013-02-05 10:14:26 -08001199 case kWhatSourceNotify:
1200 {
Andreas Huber9575c962013-02-05 13:59:56 -08001201 onSourceNotify(msg);
Andreas Huberb5f25f02013-02-05 10:14:26 -08001202 break;
1203 }
1204
Chong Zhanga7fa1d92014-06-11 14:49:23 -07001205 case kWhatClosedCaptionNotify:
1206 {
1207 onClosedCaptionNotify(msg);
1208 break;
1209 }
1210
Andreas Huberf9334412010-12-15 15:17:42 -08001211 default:
1212 TRESPASS();
1213 break;
1214 }
1215}
1216
Wei Jia94211742014-10-28 17:09:06 -07001217void NuPlayer::onResume() {
Chong Zhangefbb6192015-01-30 17:13:27 -08001218 if (!mPaused) {
1219 return;
1220 }
1221 mPaused = false;
Wei Jia94211742014-10-28 17:09:06 -07001222 if (mSource != NULL) {
1223 mSource->resume();
1224 } else {
1225 ALOGW("resume called when source is gone or not set");
1226 }
1227 // |mAudioDecoder| may have been released due to the pause timeout, so re-create it if
1228 // needed.
1229 if (audioDecoderStillNeeded() && mAudioDecoder == NULL) {
1230 instantiateDecoder(true /* audio */, &mAudioDecoder);
1231 }
1232 if (mRenderer != NULL) {
1233 mRenderer->resume();
1234 } else {
1235 ALOGW("resume called when renderer is gone or not set");
1236 }
1237}
1238
Lajos Molnarfcd3e942015-03-31 10:06:48 -07001239status_t NuPlayer::onInstantiateSecureDecoders() {
1240 status_t err;
1241 if (!(mSourceFlags & Source::FLAG_SECURE)) {
1242 return BAD_TYPE;
1243 }
1244
1245 if (mRenderer != NULL) {
1246 ALOGE("renderer should not be set when instantiating secure decoders");
1247 return UNKNOWN_ERROR;
1248 }
1249
1250 // TRICKY: We rely on mRenderer being null, so that decoder does not start requesting
1251 // data on instantiation.
Lajos Molnar1de1e252015-04-30 18:18:34 -07001252 if (mSurface != NULL) {
Lajos Molnarfcd3e942015-03-31 10:06:48 -07001253 err = instantiateDecoder(false, &mVideoDecoder);
1254 if (err != OK) {
1255 return err;
1256 }
1257 }
1258
1259 if (mAudioSink != NULL) {
1260 err = instantiateDecoder(true, &mAudioDecoder);
1261 if (err != OK) {
1262 return err;
1263 }
1264 }
1265 return OK;
1266}
1267
Wei Jia1061c9c2015-05-19 16:02:17 -07001268void NuPlayer::onStart(int64_t startPositionUs) {
Robert Shih0c61a0d2015-07-06 15:09:10 -07001269 if (!mSourceStarted) {
1270 mSourceStarted = true;
1271 mSource->start();
1272 }
1273 if (startPositionUs > 0) {
1274 performSeek(startPositionUs);
1275 if (mSource->getFormat(false /* audio */) == NULL) {
1276 return;
1277 }
1278 }
1279
Wei Jia94211742014-10-28 17:09:06 -07001280 mOffloadAudio = false;
1281 mAudioEOS = false;
1282 mVideoEOS = false;
Wei Jia94211742014-10-28 17:09:06 -07001283 mStarted = true;
1284
Wei Jia94211742014-10-28 17:09:06 -07001285 uint32_t flags = 0;
1286
1287 if (mSource->isRealTime()) {
1288 flags |= Renderer::FLAG_REAL_TIME;
1289 }
1290
1291 sp<MetaData> audioMeta = mSource->getFormatMeta(true /* audio */);
1292 audio_stream_type_t streamType = AUDIO_STREAM_MUSIC;
1293 if (mAudioSink != NULL) {
1294 streamType = mAudioSink->getAudioStreamType();
1295 }
1296
1297 sp<AMessage> videoFormat = mSource->getFormat(false /* audio */);
1298
1299 mOffloadAudio =
Ronghua Wu02cb98d2015-05-27 11:02:54 -07001300 canOffloadStream(audioMeta, (videoFormat != NULL), mSource->isStreaming(), streamType);
Wei Jia94211742014-10-28 17:09:06 -07001301 if (mOffloadAudio) {
1302 flags |= Renderer::FLAG_OFFLOAD_AUDIO;
1303 }
1304
Lajos Molnar1d15ab52015-03-04 16:46:34 -08001305 sp<AMessage> notify = new AMessage(kWhatRendererNotify, this);
Wei Jia94211742014-10-28 17:09:06 -07001306 ++mRendererGeneration;
1307 notify->setInt32("generation", mRendererGeneration);
1308 mRenderer = new Renderer(mAudioSink, notify, flags);
Wei Jia94211742014-10-28 17:09:06 -07001309 mRendererLooper = new ALooper;
1310 mRendererLooper->setName("NuPlayerRenderer");
1311 mRendererLooper->start(false, false, ANDROID_PRIORITY_AUDIO);
1312 mRendererLooper->registerHandler(mRenderer);
Lajos Molnar3a474aa2015-04-24 17:10:07 -07001313
1314 status_t err = mRenderer->setPlaybackSettings(mPlaybackSettings);
1315 if (err != OK) {
1316 mSource->stop();
Robert Shih0c61a0d2015-07-06 15:09:10 -07001317 mSourceStarted = false;
Lajos Molnar3a474aa2015-04-24 17:10:07 -07001318 notifyListener(MEDIA_ERROR, MEDIA_ERROR_UNKNOWN, err);
1319 return;
Wei Jiac8206ff2015-03-04 13:59:37 -08001320 }
Wei Jia94211742014-10-28 17:09:06 -07001321
Ronghua Wuc8a70d32015-04-29 16:26:34 -07001322 float rate = getFrameRate();
1323 if (rate > 0) {
Wei Jia94211742014-10-28 17:09:06 -07001324 mRenderer->setVideoFrameRate(rate);
1325 }
1326
Wei Jiac6cfd702014-11-11 16:33:20 -08001327 if (mVideoDecoder != NULL) {
1328 mVideoDecoder->setRenderer(mRenderer);
1329 }
1330 if (mAudioDecoder != NULL) {
1331 mAudioDecoder->setRenderer(mRenderer);
1332 }
1333
Wei Jia94211742014-10-28 17:09:06 -07001334 postScanSources();
1335}
1336
Chong Zhangefbb6192015-01-30 17:13:27 -08001337void NuPlayer::onPause() {
1338 if (mPaused) {
1339 return;
1340 }
1341 mPaused = true;
1342 if (mSource != NULL) {
1343 mSource->pause();
1344 } else {
1345 ALOGW("pause called when source is gone or not set");
1346 }
1347 if (mRenderer != NULL) {
1348 mRenderer->pause();
1349 } else {
1350 ALOGW("pause called when renderer is gone or not set");
1351 }
1352}
1353
Ronghua Wud7988b12014-10-03 15:19:10 -07001354bool NuPlayer::audioDecoderStillNeeded() {
1355 // Audio decoder is no longer needed if it's in shut/shutting down status.
1356 return ((mFlushingAudio != SHUT_DOWN) && (mFlushingAudio != SHUTTING_DOWN_DECODER));
1357}
1358
Andy Hung8d121d42014-10-03 09:53:53 -07001359void NuPlayer::handleFlushComplete(bool audio, bool isDecoder) {
1360 // We wait for both the decoder flush and the renderer flush to complete
1361 // before entering either the FLUSHED or the SHUTTING_DOWN_DECODER state.
1362
1363 mFlushComplete[audio][isDecoder] = true;
1364 if (!mFlushComplete[audio][!isDecoder]) {
1365 return;
1366 }
1367
1368 FlushStatus *state = audio ? &mFlushingAudio : &mFlushingVideo;
1369 switch (*state) {
1370 case FLUSHING_DECODER:
1371 {
1372 *state = FLUSHED;
Andy Hung8d121d42014-10-03 09:53:53 -07001373 break;
1374 }
1375
1376 case FLUSHING_DECODER_SHUTDOWN:
1377 {
1378 *state = SHUTTING_DOWN_DECODER;
1379
1380 ALOGV("initiating %s decoder shutdown", audio ? "audio" : "video");
1381 if (!audio) {
Andy Hung8d121d42014-10-03 09:53:53 -07001382 // Widevine source reads must stop before releasing the video decoder.
1383 if (mSource != NULL && mSourceFlags & Source::FLAG_SECURE) {
1384 mSource->stop();
Robert Shih0c61a0d2015-07-06 15:09:10 -07001385 mSourceStarted = false;
Andy Hung8d121d42014-10-03 09:53:53 -07001386 }
1387 }
1388 getDecoder(audio)->initiateShutdown();
1389 break;
1390 }
1391
1392 default:
1393 // decoder flush completes only occur in a flushing state.
1394 LOG_ALWAYS_FATAL_IF(isDecoder, "decoder flush in invalid state %d", *state);
1395 break;
1396 }
1397}
1398
Andreas Huber3831a062010-12-21 10:22:33 -08001399void NuPlayer::finishFlushIfPossible() {
Wei Jia53904f32014-07-29 10:22:53 -07001400 if (mFlushingAudio != NONE && mFlushingAudio != FLUSHED
1401 && mFlushingAudio != SHUT_DOWN) {
Andreas Huber3831a062010-12-21 10:22:33 -08001402 return;
1403 }
1404
Wei Jia53904f32014-07-29 10:22:53 -07001405 if (mFlushingVideo != NONE && mFlushingVideo != FLUSHED
1406 && mFlushingVideo != SHUT_DOWN) {
Andreas Huber3831a062010-12-21 10:22:33 -08001407 return;
1408 }
1409
Steve Block3856b092011-10-20 11:56:00 +01001410 ALOGV("both audio and video are flushed now.");
Andreas Huber3831a062010-12-21 10:22:33 -08001411
Andreas Huber3831a062010-12-21 10:22:33 -08001412 mFlushingAudio = NONE;
1413 mFlushingVideo = NONE;
Andreas Huber3831a062010-12-21 10:22:33 -08001414
Andy Hung8d121d42014-10-03 09:53:53 -07001415 clearFlushComplete();
1416
Andreas Hubera1f8ab02012-11-30 10:53:22 -08001417 processDeferredActions();
Andreas Huber1aef2112011-01-04 14:01:29 -08001418}
1419
1420void NuPlayer::postScanSources() {
1421 if (mScanSourcesPending) {
1422 return;
1423 }
1424
Lajos Molnar1d15ab52015-03-04 16:46:34 -08001425 sp<AMessage> msg = new AMessage(kWhatScanSources, this);
Andreas Huber1aef2112011-01-04 14:01:29 -08001426 msg->setInt32("generation", mScanSourcesGeneration);
1427 msg->post();
1428
1429 mScanSourcesPending = true;
1430}
1431
Andy Hung202bce12014-12-03 11:47:36 -08001432void NuPlayer::tryOpenAudioSinkForOffload(const sp<AMessage> &format, bool hasVideo) {
1433 // Note: This is called early in NuPlayer to determine whether offloading
1434 // is possible; otherwise the decoders call the renderer openAudioSink directly.
Andy Hung282a7e32014-08-14 15:56:34 -07001435
Andy Hung202bce12014-12-03 11:47:36 -08001436 status_t err = mRenderer->openAudioSink(
1437 format, true /* offloadOnly */, hasVideo, AUDIO_OUTPUT_FLAG_NONE, &mOffloadAudio);
1438 if (err != OK) {
1439 // Any failure we turn off mOffloadAudio.
1440 mOffloadAudio = false;
1441 } else if (mOffloadAudio) {
Chong Zhang3b9eb1f2014-10-15 17:05:08 -07001442 sp<MetaData> audioMeta =
1443 mSource->getFormatMeta(true /* audio */);
1444 sendMetaDataToHal(mAudioSink, audioMeta);
Andy Hung282a7e32014-08-14 15:56:34 -07001445 }
1446}
1447
1448void NuPlayer::closeAudioSink() {
Chong Zhang3b9eb1f2014-10-15 17:05:08 -07001449 mRenderer->closeAudioSink();
Andy Hung282a7e32014-08-14 15:56:34 -07001450}
1451
Wei Jiae4d18c72015-07-13 17:58:11 -07001452void NuPlayer::determineAudioModeChange() {
1453 if (mSource == NULL || mAudioSink == NULL) {
1454 return;
1455 }
1456
1457 if (mRenderer == NULL) {
1458 ALOGW("No renderer can be used to determine audio mode. Use non-offload for safety.");
1459 mOffloadAudio = false;
1460 return;
1461 }
1462
1463 sp<MetaData> audioMeta = mSource->getFormatMeta(true /* audio */);
1464 sp<AMessage> videoFormat = mSource->getFormat(false /* audio */);
1465 audio_stream_type_t streamType = mAudioSink->getAudioStreamType();
1466 const bool hasVideo = (videoFormat != NULL);
1467 const bool canOffload = canOffloadStream(
1468 audioMeta, hasVideo, mSource->isStreaming(), streamType);
1469 if (canOffload) {
1470 if (!mOffloadAudio) {
1471 mRenderer->signalEnableOffloadAudio();
1472 }
1473 // open audio sink early under offload mode.
1474 sp<AMessage> format = mSource->getFormat(true /*audio*/);
1475 tryOpenAudioSinkForOffload(format, hasVideo);
1476 } else {
1477 mRenderer->signalDisableOffloadAudio();
1478 mOffloadAudio = false;
1479 }
1480}
1481
Chong Zhang7137ec72014-11-12 16:41:05 -08001482status_t NuPlayer::instantiateDecoder(bool audio, sp<DecoderBase> *decoder) {
Andreas Huberf9334412010-12-15 15:17:42 -08001483 if (*decoder != NULL) {
1484 return OK;
1485 }
1486
Andreas Huber84066782011-08-16 09:34:26 -07001487 sp<AMessage> format = mSource->getFormat(audio);
Andreas Huberf9334412010-12-15 15:17:42 -08001488
Andreas Huber84066782011-08-16 09:34:26 -07001489 if (format == NULL) {
Andreas Huberf9334412010-12-15 15:17:42 -08001490 return -EWOULDBLOCK;
1491 }
1492
Ronghua Wu8db88132015-04-22 13:51:35 -07001493 format->setInt32("priority", 0 /* realtime */);
1494
Andreas Huber3fe62152011-09-16 15:09:22 -07001495 if (!audio) {
Andreas Huber84066782011-08-16 09:34:26 -07001496 AString mime;
1497 CHECK(format->findString("mime", &mime));
Chong Zhanga7fa1d92014-06-11 14:49:23 -07001498
Lajos Molnar1d15ab52015-03-04 16:46:34 -08001499 sp<AMessage> ccNotify = new AMessage(kWhatClosedCaptionNotify, this);
Chong Zhang341ab6e2015-02-04 13:37:18 -08001500 if (mCCDecoder == NULL) {
1501 mCCDecoder = new CCDecoder(ccNotify);
1502 }
Lajos Molnar09524832014-07-17 14:29:51 -07001503
1504 if (mSourceFlags & Source::FLAG_SECURE) {
1505 format->setInt32("secure", true);
1506 }
Chong Zhang17134602015-01-07 16:14:34 -08001507
1508 if (mSourceFlags & Source::FLAG_PROTECTED) {
1509 format->setInt32("protected", true);
1510 }
Ronghua Wu8db88132015-04-22 13:51:35 -07001511
Ronghua Wuc8a70d32015-04-29 16:26:34 -07001512 float rate = getFrameRate();
1513 if (rate > 0) {
Lajos Molnar3a474aa2015-04-24 17:10:07 -07001514 format->setFloat("operating-rate", rate * mPlaybackSettings.mSpeed);
Ronghua Wu8db88132015-04-22 13:51:35 -07001515 }
Andreas Huber3fe62152011-09-16 15:09:22 -07001516 }
1517
Wei Jiabc2fb722014-07-08 16:37:57 -07001518 if (audio) {
Lajos Molnar1d15ab52015-03-04 16:46:34 -08001519 sp<AMessage> notify = new AMessage(kWhatAudioNotify, this);
Wei Jia88703c32014-08-06 11:24:07 -07001520 ++mAudioDecoderGeneration;
1521 notify->setInt32("generation", mAudioDecoderGeneration);
1522
Wei Jiae4d18c72015-07-13 17:58:11 -07001523 determineAudioModeChange();
Wei Jiabc2fb722014-07-08 16:37:57 -07001524 if (mOffloadAudio) {
Haynes Mathew George8b635332015-03-30 17:59:47 -07001525 const bool hasVideo = (mSource->getFormat(false /*audio */) != NULL);
1526 format->setInt32("has-video", hasVideo);
Wei Jiac6cfd702014-11-11 16:33:20 -08001527 *decoder = new DecoderPassThrough(notify, mSource, mRenderer);
Wei Jiabc2fb722014-07-08 16:37:57 -07001528 } else {
Ronghua Wu68845c12015-07-21 09:50:48 -07001529 *decoder = new Decoder(notify, mSource, mPID, mRenderer);
Wei Jiabc2fb722014-07-08 16:37:57 -07001530 }
1531 } else {
Lajos Molnar1d15ab52015-03-04 16:46:34 -08001532 sp<AMessage> notify = new AMessage(kWhatVideoNotify, this);
Wei Jia88703c32014-08-06 11:24:07 -07001533 ++mVideoDecoderGeneration;
1534 notify->setInt32("generation", mVideoDecoderGeneration);
1535
Chong Zhang7137ec72014-11-12 16:41:05 -08001536 *decoder = new Decoder(
Ronghua Wu68845c12015-07-21 09:50:48 -07001537 notify, mSource, mPID, mRenderer, mSurface, mCCDecoder);
Lajos Molnard9fd6312014-11-06 11:00:00 -08001538
1539 // enable FRC if high-quality AV sync is requested, even if not
Lajos Molnar1de1e252015-04-30 18:18:34 -07001540 // directly queuing to display, as this will even improve textureview
Lajos Molnard9fd6312014-11-06 11:00:00 -08001541 // playback.
1542 {
1543 char value[PROPERTY_VALUE_MAX];
1544 if (property_get("persist.sys.media.avsync", value, NULL) &&
1545 (!strcmp("1", value) || !strcasecmp("true", value))) {
1546 format->setInt32("auto-frc", 1);
1547 }
1548 }
Wei Jiabc2fb722014-07-08 16:37:57 -07001549 }
Lajos Molnar1cd13982014-01-17 15:12:51 -08001550 (*decoder)->init();
Andreas Huber84066782011-08-16 09:34:26 -07001551 (*decoder)->configure(format);
Andreas Huberf9334412010-12-15 15:17:42 -08001552
Lajos Molnar09524832014-07-17 14:29:51 -07001553 // allocate buffers to decrypt widevine source buffers
1554 if (!audio && (mSourceFlags & Source::FLAG_SECURE)) {
1555 Vector<sp<ABuffer> > inputBufs;
1556 CHECK_EQ((*decoder)->getInputBuffers(&inputBufs), (status_t)OK);
1557
1558 Vector<MediaBuffer *> mediaBufs;
1559 for (size_t i = 0; i < inputBufs.size(); i++) {
1560 const sp<ABuffer> &buffer = inputBufs[i];
1561 MediaBuffer *mbuf = new MediaBuffer(buffer->data(), buffer->size());
1562 mediaBufs.push(mbuf);
1563 }
1564
1565 status_t err = mSource->setBuffers(audio, mediaBufs);
1566 if (err != OK) {
1567 for (size_t i = 0; i < mediaBufs.size(); ++i) {
1568 mediaBufs[i]->release();
1569 }
1570 mediaBufs.clear();
1571 ALOGE("Secure source didn't support secure mediaBufs.");
1572 return err;
1573 }
1574 }
Andreas Huberf9334412010-12-15 15:17:42 -08001575 return OK;
1576}
1577
Chong Zhangced1c2f2014-08-08 15:22:35 -07001578void NuPlayer::updateVideoSize(
1579 const sp<AMessage> &inputFormat,
1580 const sp<AMessage> &outputFormat) {
1581 if (inputFormat == NULL) {
1582 ALOGW("Unknown video size, reporting 0x0!");
1583 notifyListener(MEDIA_SET_VIDEO_SIZE, 0, 0);
1584 return;
1585 }
1586
1587 int32_t displayWidth, displayHeight;
Chong Zhangced1c2f2014-08-08 15:22:35 -07001588 if (outputFormat != NULL) {
1589 int32_t width, height;
1590 CHECK(outputFormat->findInt32("width", &width));
1591 CHECK(outputFormat->findInt32("height", &height));
1592
1593 int32_t cropLeft, cropTop, cropRight, cropBottom;
1594 CHECK(outputFormat->findRect(
1595 "crop",
1596 &cropLeft, &cropTop, &cropRight, &cropBottom));
1597
1598 displayWidth = cropRight - cropLeft + 1;
1599 displayHeight = cropBottom - cropTop + 1;
1600
1601 ALOGV("Video output format changed to %d x %d "
1602 "(crop: %d x %d @ (%d, %d))",
1603 width, height,
1604 displayWidth,
1605 displayHeight,
1606 cropLeft, cropTop);
1607 } else {
1608 CHECK(inputFormat->findInt32("width", &displayWidth));
1609 CHECK(inputFormat->findInt32("height", &displayHeight));
1610
1611 ALOGV("Video input format %d x %d", displayWidth, displayHeight);
1612 }
1613
1614 // Take into account sample aspect ratio if necessary:
1615 int32_t sarWidth, sarHeight;
1616 if (inputFormat->findInt32("sar-width", &sarWidth)
1617 && inputFormat->findInt32("sar-height", &sarHeight)) {
1618 ALOGV("Sample aspect ratio %d : %d", sarWidth, sarHeight);
1619
1620 displayWidth = (displayWidth * sarWidth) / sarHeight;
1621
1622 ALOGV("display dimensions %d x %d", displayWidth, displayHeight);
1623 }
1624
1625 int32_t rotationDegrees;
1626 if (!inputFormat->findInt32("rotation-degrees", &rotationDegrees)) {
1627 rotationDegrees = 0;
1628 }
1629
1630 if (rotationDegrees == 90 || rotationDegrees == 270) {
1631 int32_t tmp = displayWidth;
1632 displayWidth = displayHeight;
1633 displayHeight = tmp;
1634 }
1635
1636 notifyListener(
1637 MEDIA_SET_VIDEO_SIZE,
1638 displayWidth,
1639 displayHeight);
1640}
1641
Chong Zhangdcb89b32013-08-06 09:44:47 -07001642void NuPlayer::notifyListener(int msg, int ext1, int ext2, const Parcel *in) {
Andreas Huber43c3e6c2011-01-05 12:17:08 -08001643 if (mDriver == NULL) {
Andreas Huberf9334412010-12-15 15:17:42 -08001644 return;
1645 }
1646
Andreas Huber43c3e6c2011-01-05 12:17:08 -08001647 sp<NuPlayerDriver> driver = mDriver.promote();
Andreas Huberf9334412010-12-15 15:17:42 -08001648
Andreas Huber43c3e6c2011-01-05 12:17:08 -08001649 if (driver == NULL) {
Andreas Huberf9334412010-12-15 15:17:42 -08001650 return;
1651 }
1652
Chong Zhangdcb89b32013-08-06 09:44:47 -07001653 driver->notifyListener(msg, ext1, ext2, in);
Andreas Huberf9334412010-12-15 15:17:42 -08001654}
1655
Chong Zhang7137ec72014-11-12 16:41:05 -08001656void NuPlayer::flushDecoder(bool audio, bool needShutdown) {
Andreas Huber14f76722013-01-15 09:04:18 -08001657 ALOGV("[%s] flushDecoder needShutdown=%d",
1658 audio ? "audio" : "video", needShutdown);
1659
Chong Zhang7137ec72014-11-12 16:41:05 -08001660 const sp<DecoderBase> &decoder = getDecoder(audio);
Lajos Molnar87603c02014-08-20 19:25:30 -07001661 if (decoder == NULL) {
Steve Blockdf64d152012-01-04 20:05:49 +00001662 ALOGI("flushDecoder %s without decoder present",
Andreas Huber6e3d3112011-11-28 12:36:11 -08001663 audio ? "audio" : "video");
Lajos Molnar87603c02014-08-20 19:25:30 -07001664 return;
Andreas Huber6e3d3112011-11-28 12:36:11 -08001665 }
1666
Andreas Huber1aef2112011-01-04 14:01:29 -08001667 // Make sure we don't continue to scan sources until we finish flushing.
1668 ++mScanSourcesGeneration;
Chong Zhang3b032b32015-04-17 15:49:06 -07001669 if (mScanSourcesPending) {
1670 mDeferredActions.push_back(
1671 new SimpleAction(&NuPlayer::performScanSources));
1672 mScanSourcesPending = false;
1673 }
Andreas Huber1aef2112011-01-04 14:01:29 -08001674
Chong Zhang7137ec72014-11-12 16:41:05 -08001675 decoder->signalFlush();
Andreas Huber1aef2112011-01-04 14:01:29 -08001676
1677 FlushStatus newStatus =
1678 needShutdown ? FLUSHING_DECODER_SHUTDOWN : FLUSHING_DECODER;
1679
Lajos Molnarfcd3e942015-03-31 10:06:48 -07001680 mFlushComplete[audio][false /* isDecoder */] = (mRenderer == NULL);
Andy Hung8d121d42014-10-03 09:53:53 -07001681 mFlushComplete[audio][true /* isDecoder */] = false;
Andreas Huber1aef2112011-01-04 14:01:29 -08001682 if (audio) {
Wei Jia53904f32014-07-29 10:22:53 -07001683 ALOGE_IF(mFlushingAudio != NONE,
1684 "audio flushDecoder() is called in state %d", mFlushingAudio);
Andreas Huber1aef2112011-01-04 14:01:29 -08001685 mFlushingAudio = newStatus;
Andreas Huber1aef2112011-01-04 14:01:29 -08001686 } else {
Wei Jia53904f32014-07-29 10:22:53 -07001687 ALOGE_IF(mFlushingVideo != NONE,
1688 "video flushDecoder() is called in state %d", mFlushingVideo);
Andreas Huber1aef2112011-01-04 14:01:29 -08001689 mFlushingVideo = newStatus;
Andreas Huber1aef2112011-01-04 14:01:29 -08001690 }
1691}
1692
Chong Zhangced1c2f2014-08-08 15:22:35 -07001693void NuPlayer::queueDecoderShutdown(
1694 bool audio, bool video, const sp<AMessage> &reply) {
1695 ALOGI("queueDecoderShutdown audio=%d, video=%d", audio, video);
Andreas Huber84066782011-08-16 09:34:26 -07001696
Chong Zhangced1c2f2014-08-08 15:22:35 -07001697 mDeferredActions.push_back(
Wei Jiafef808d2014-10-31 17:57:05 -07001698 new FlushDecoderAction(
1699 audio ? FLUSH_CMD_SHUTDOWN : FLUSH_CMD_NONE,
1700 video ? FLUSH_CMD_SHUTDOWN : FLUSH_CMD_NONE));
Andreas Huber84066782011-08-16 09:34:26 -07001701
Chong Zhangced1c2f2014-08-08 15:22:35 -07001702 mDeferredActions.push_back(
1703 new SimpleAction(&NuPlayer::performScanSources));
Andreas Huber84066782011-08-16 09:34:26 -07001704
Chong Zhangced1c2f2014-08-08 15:22:35 -07001705 mDeferredActions.push_back(new PostMessageAction(reply));
1706
1707 processDeferredActions();
Andreas Huber84066782011-08-16 09:34:26 -07001708}
1709
James Dong0d268a32012-08-31 12:18:27 -07001710status_t NuPlayer::setVideoScalingMode(int32_t mode) {
1711 mVideoScalingMode = mode;
Lajos Molnar1de1e252015-04-30 18:18:34 -07001712 if (mSurface != NULL) {
1713 status_t ret = native_window_set_scaling_mode(mSurface.get(), mVideoScalingMode);
James Dong0d268a32012-08-31 12:18:27 -07001714 if (ret != OK) {
1715 ALOGE("Failed to set scaling mode (%d): %s",
1716 -ret, strerror(-ret));
1717 return ret;
1718 }
1719 }
1720 return OK;
1721}
1722
Chong Zhangdcb89b32013-08-06 09:44:47 -07001723status_t NuPlayer::getTrackInfo(Parcel* reply) const {
Lajos Molnar1d15ab52015-03-04 16:46:34 -08001724 sp<AMessage> msg = new AMessage(kWhatGetTrackInfo, this);
Chong Zhangdcb89b32013-08-06 09:44:47 -07001725 msg->setPointer("reply", reply);
1726
1727 sp<AMessage> response;
1728 status_t err = msg->postAndAwaitResponse(&response);
1729 return err;
1730}
1731
Robert Shih7c4f0d72014-07-09 18:53:31 -07001732status_t NuPlayer::getSelectedTrack(int32_t type, Parcel* reply) const {
Lajos Molnar1d15ab52015-03-04 16:46:34 -08001733 sp<AMessage> msg = new AMessage(kWhatGetSelectedTrack, this);
Robert Shih7c4f0d72014-07-09 18:53:31 -07001734 msg->setPointer("reply", reply);
1735 msg->setInt32("type", type);
1736
1737 sp<AMessage> response;
1738 status_t err = msg->postAndAwaitResponse(&response);
1739 if (err == OK && response != NULL) {
1740 CHECK(response->findInt32("err", &err));
1741 }
1742 return err;
1743}
1744
Robert Shih6ffb1fd2014-10-29 16:24:32 -07001745status_t NuPlayer::selectTrack(size_t trackIndex, bool select, int64_t timeUs) {
Lajos Molnar1d15ab52015-03-04 16:46:34 -08001746 sp<AMessage> msg = new AMessage(kWhatSelectTrack, this);
Chong Zhangdcb89b32013-08-06 09:44:47 -07001747 msg->setSize("trackIndex", trackIndex);
1748 msg->setInt32("select", select);
Robert Shih6ffb1fd2014-10-29 16:24:32 -07001749 msg->setInt64("timeUs", timeUs);
Chong Zhangdcb89b32013-08-06 09:44:47 -07001750
1751 sp<AMessage> response;
1752 status_t err = msg->postAndAwaitResponse(&response);
1753
Chong Zhang404fced2014-06-11 14:45:31 -07001754 if (err != OK) {
1755 return err;
1756 }
1757
1758 if (!response->findInt32("err", &err)) {
1759 err = OK;
1760 }
1761
Chong Zhangdcb89b32013-08-06 09:44:47 -07001762 return err;
1763}
1764
Ronghua Wua73d9e02014-10-08 15:13:29 -07001765status_t NuPlayer::getCurrentPosition(int64_t *mediaUs) {
1766 sp<Renderer> renderer = mRenderer;
1767 if (renderer == NULL) {
1768 return NO_INIT;
1769 }
1770
1771 return renderer->getCurrentPosition(mediaUs);
1772}
1773
Praveen Chavane1e5d7a2015-05-19 19:09:48 -07001774void NuPlayer::getStats(Vector<sp<AMessage> > *mTrackStats) {
1775 CHECK(mTrackStats != NULL);
1776
1777 mTrackStats->clear();
1778 if (mVideoDecoder != NULL) {
1779 mTrackStats->push_back(mVideoDecoder->getStats());
1780 }
1781 if (mAudioDecoder != NULL) {
1782 mTrackStats->push_back(mAudioDecoder->getStats());
Chong Zhang7137ec72014-11-12 16:41:05 -08001783 }
Ronghua Wua73d9e02014-10-08 15:13:29 -07001784}
1785
Marco Nelissenf0b72b52014-09-16 15:43:44 -07001786sp<MetaData> NuPlayer::getFileMeta() {
1787 return mSource->getFileFormatMeta();
1788}
1789
Ronghua Wuc8a70d32015-04-29 16:26:34 -07001790float NuPlayer::getFrameRate() {
1791 sp<MetaData> meta = mSource->getFormatMeta(false /* audio */);
1792 if (meta == NULL) {
1793 return 0;
1794 }
1795 int32_t rate;
1796 if (!meta->findInt32(kKeyFrameRate, &rate)) {
1797 // fall back to try file meta
1798 sp<MetaData> fileMeta = getFileMeta();
1799 if (fileMeta == NULL) {
1800 ALOGW("source has video meta but not file meta");
1801 return -1;
1802 }
1803 int32_t fileMetaRate;
1804 if (!fileMeta->findInt32(kKeyFrameRate, &fileMetaRate)) {
1805 return -1;
1806 }
1807 return fileMetaRate;
1808 }
1809 return rate;
1810}
1811
Andreas Huberb7c8e912012-11-27 15:02:53 -08001812void NuPlayer::schedulePollDuration() {
Lajos Molnar1d15ab52015-03-04 16:46:34 -08001813 sp<AMessage> msg = new AMessage(kWhatPollDuration, this);
Andreas Huberb7c8e912012-11-27 15:02:53 -08001814 msg->setInt32("generation", mPollDurationGeneration);
1815 msg->post();
1816}
1817
1818void NuPlayer::cancelPollDuration() {
1819 ++mPollDurationGeneration;
1820}
1821
Andreas Hubera1f8ab02012-11-30 10:53:22 -08001822void NuPlayer::processDeferredActions() {
1823 while (!mDeferredActions.empty()) {
1824 // We won't execute any deferred actions until we're no longer in
1825 // an intermediate state, i.e. one more more decoders are currently
1826 // flushing or shutting down.
1827
Andreas Hubera1f8ab02012-11-30 10:53:22 -08001828 if (mFlushingAudio != NONE || mFlushingVideo != NONE) {
1829 // We're currently flushing, postpone the reset until that's
1830 // completed.
1831
1832 ALOGV("postponing action mFlushingAudio=%d, mFlushingVideo=%d",
1833 mFlushingAudio, mFlushingVideo);
1834
1835 break;
1836 }
1837
1838 sp<Action> action = *mDeferredActions.begin();
1839 mDeferredActions.erase(mDeferredActions.begin());
1840
1841 action->execute(this);
1842 }
1843}
1844
Wei Jia29840802015-05-15 17:11:38 -07001845void NuPlayer::performSeek(int64_t seekTimeUs) {
1846 ALOGV("performSeek seekTimeUs=%lld us (%.2f secs)",
Lajos Molnar6d339f12015-04-17 16:15:53 -07001847 (long long)seekTimeUs,
Wei Jia29840802015-05-15 17:11:38 -07001848 seekTimeUs / 1E6);
Andreas Hubera1f8ab02012-11-30 10:53:22 -08001849
Andy Hungadf34bf2014-09-03 18:22:22 -07001850 if (mSource == NULL) {
1851 // This happens when reset occurs right before the loop mode
1852 // asynchronously seeks to the start of the stream.
1853 LOG_ALWAYS_FATAL_IF(mAudioDecoder != NULL || mVideoDecoder != NULL,
1854 "mSource is NULL and decoders not NULL audio(%p) video(%p)",
1855 mAudioDecoder.get(), mVideoDecoder.get());
1856 return;
1857 }
Andreas Hubera1f8ab02012-11-30 10:53:22 -08001858 mSource->seekTo(seekTimeUs);
Robert Shihd3b0bbb2014-07-23 15:00:25 -07001859 ++mTimedTextGeneration;
Andreas Hubera1f8ab02012-11-30 10:53:22 -08001860
Andreas Hubera1f8ab02012-11-30 10:53:22 -08001861 // everything's flushed, continue playback.
1862}
1863
Wei Jiafef808d2014-10-31 17:57:05 -07001864void NuPlayer::performDecoderFlush(FlushCommand audio, FlushCommand video) {
1865 ALOGV("performDecoderFlush audio=%d, video=%d", audio, video);
Andreas Hubera1f8ab02012-11-30 10:53:22 -08001866
Wei Jiafef808d2014-10-31 17:57:05 -07001867 if ((audio == FLUSH_CMD_NONE || mAudioDecoder == NULL)
1868 && (video == FLUSH_CMD_NONE || mVideoDecoder == NULL)) {
Andreas Hubera1f8ab02012-11-30 10:53:22 -08001869 return;
1870 }
1871
Wei Jiafef808d2014-10-31 17:57:05 -07001872 if (audio != FLUSH_CMD_NONE && mAudioDecoder != NULL) {
1873 flushDecoder(true /* audio */, (audio == FLUSH_CMD_SHUTDOWN));
Andreas Hubera1f8ab02012-11-30 10:53:22 -08001874 }
1875
Wei Jiafef808d2014-10-31 17:57:05 -07001876 if (video != FLUSH_CMD_NONE && mVideoDecoder != NULL) {
1877 flushDecoder(false /* audio */, (video == FLUSH_CMD_SHUTDOWN));
Andreas Hubera1f8ab02012-11-30 10:53:22 -08001878 }
1879}
1880
1881void NuPlayer::performReset() {
1882 ALOGV("performReset");
1883
1884 CHECK(mAudioDecoder == NULL);
1885 CHECK(mVideoDecoder == NULL);
1886
1887 cancelPollDuration();
1888
1889 ++mScanSourcesGeneration;
1890 mScanSourcesPending = false;
1891
Lajos Molnar09524832014-07-17 14:29:51 -07001892 if (mRendererLooper != NULL) {
1893 if (mRenderer != NULL) {
1894 mRendererLooper->unregisterHandler(mRenderer->id());
1895 }
1896 mRendererLooper->stop();
1897 mRendererLooper.clear();
1898 }
Andreas Hubera1f8ab02012-11-30 10:53:22 -08001899 mRenderer.clear();
Wei Jia57568df2014-09-22 10:16:29 -07001900 ++mRendererGeneration;
Andreas Hubera1f8ab02012-11-30 10:53:22 -08001901
1902 if (mSource != NULL) {
1903 mSource->stop();
Andreas Huberb5f25f02013-02-05 10:14:26 -08001904
Andreas Hubera1f8ab02012-11-30 10:53:22 -08001905 mSource.clear();
1906 }
1907
1908 if (mDriver != NULL) {
1909 sp<NuPlayerDriver> driver = mDriver.promote();
1910 if (driver != NULL) {
1911 driver->notifyResetComplete();
1912 }
1913 }
Andreas Huber57a339c2012-12-03 11:18:00 -08001914
1915 mStarted = false;
Robert Shih0c61a0d2015-07-06 15:09:10 -07001916 mSourceStarted = false;
Andreas Hubera1f8ab02012-11-30 10:53:22 -08001917}
1918
1919void NuPlayer::performScanSources() {
1920 ALOGV("performScanSources");
1921
Andreas Huber57a339c2012-12-03 11:18:00 -08001922 if (!mStarted) {
1923 return;
1924 }
1925
Andreas Hubera1f8ab02012-11-30 10:53:22 -08001926 if (mAudioDecoder == NULL || mVideoDecoder == NULL) {
1927 postScanSources();
1928 }
1929}
1930
Lajos Molnar1de1e252015-04-30 18:18:34 -07001931void NuPlayer::performSetSurface(const sp<Surface> &surface) {
Andreas Huber57a339c2012-12-03 11:18:00 -08001932 ALOGV("performSetSurface");
1933
Lajos Molnar1de1e252015-04-30 18:18:34 -07001934 mSurface = surface;
Andreas Huber57a339c2012-12-03 11:18:00 -08001935
1936 // XXX - ignore error from setVideoScalingMode for now
1937 setVideoScalingMode(mVideoScalingMode);
Chong Zhang13d6faa2014-08-22 15:35:28 -07001938
1939 if (mDriver != NULL) {
1940 sp<NuPlayerDriver> driver = mDriver.promote();
1941 if (driver != NULL) {
1942 driver->notifySetSurfaceComplete();
1943 }
1944 }
Andreas Huber57a339c2012-12-03 11:18:00 -08001945}
1946
Chong Zhangf8d71772014-11-26 15:08:34 -08001947void NuPlayer::performResumeDecoders(bool needNotify) {
1948 if (needNotify) {
1949 mResumePending = true;
1950 if (mVideoDecoder == NULL) {
1951 // if audio-only, we can notify seek complete now,
1952 // as the resume operation will be relatively fast.
1953 finishResume();
1954 }
1955 }
1956
Chong Zhang7137ec72014-11-12 16:41:05 -08001957 if (mVideoDecoder != NULL) {
Chong Zhangf8d71772014-11-26 15:08:34 -08001958 // When there is continuous seek, MediaPlayer will cache the seek
1959 // position, and send down new seek request when previous seek is
1960 // complete. Let's wait for at least one video output frame before
1961 // notifying seek complete, so that the video thumbnail gets updated
1962 // when seekbar is dragged.
1963 mVideoDecoder->signalResume(needNotify);
Chong Zhang7137ec72014-11-12 16:41:05 -08001964 }
1965
1966 if (mAudioDecoder != NULL) {
Chong Zhangf8d71772014-11-26 15:08:34 -08001967 mAudioDecoder->signalResume(false /* needNotify */);
1968 }
1969}
1970
1971void NuPlayer::finishResume() {
1972 if (mResumePending) {
1973 mResumePending = false;
Wei Jia1061c9c2015-05-19 16:02:17 -07001974 notifyDriverSeekComplete();
1975 }
1976}
1977
1978void NuPlayer::notifyDriverSeekComplete() {
1979 if (mDriver != NULL) {
1980 sp<NuPlayerDriver> driver = mDriver.promote();
1981 if (driver != NULL) {
1982 driver->notifySeekComplete();
Chong Zhangf8d71772014-11-26 15:08:34 -08001983 }
Chong Zhang7137ec72014-11-12 16:41:05 -08001984 }
1985}
1986
Andreas Huber9575c962013-02-05 13:59:56 -08001987void NuPlayer::onSourceNotify(const sp<AMessage> &msg) {
1988 int32_t what;
1989 CHECK(msg->findInt32("what", &what));
1990
1991 switch (what) {
Lajos Molnarfcd3e942015-03-31 10:06:48 -07001992 case Source::kWhatInstantiateSecureDecoders:
1993 {
1994 if (mSource == NULL) {
1995 // This is a stale notification from a source that was
1996 // asynchronously preparing when the client called reset().
1997 // We handled the reset, the source is gone.
1998 break;
1999 }
2000
2001 sp<AMessage> reply;
2002 CHECK(msg->findMessage("reply", &reply));
2003 status_t err = onInstantiateSecureDecoders();
2004 reply->setInt32("err", err);
2005 reply->post();
2006 break;
2007 }
2008
Andreas Huber9575c962013-02-05 13:59:56 -08002009 case Source::kWhatPrepared:
2010 {
Andreas Huberb5f28d42013-04-25 15:11:19 -07002011 if (mSource == NULL) {
2012 // This is a stale notification from a source that was
2013 // asynchronously preparing when the client called reset().
2014 // We handled the reset, the source is gone.
2015 break;
2016 }
2017
Andreas Huberec0c5972013-02-05 14:47:13 -08002018 int32_t err;
2019 CHECK(msg->findInt32("err", &err));
2020
Lajos Molnarfcd3e942015-03-31 10:06:48 -07002021 if (err != OK) {
2022 // shut down potential secure codecs in case client never calls reset
2023 mDeferredActions.push_back(
2024 new FlushDecoderAction(FLUSH_CMD_SHUTDOWN /* audio */,
2025 FLUSH_CMD_SHUTDOWN /* video */));
2026 processDeferredActions();
2027 }
2028
Andreas Huber9575c962013-02-05 13:59:56 -08002029 sp<NuPlayerDriver> driver = mDriver.promote();
2030 if (driver != NULL) {
Marco Nelissendd114d12014-05-28 15:23:14 -07002031 // notify duration first, so that it's definitely set when
2032 // the app received the "prepare complete" callback.
2033 int64_t durationUs;
2034 if (mSource->getDuration(&durationUs) == OK) {
2035 driver->notifyDuration(durationUs);
2036 }
Andreas Huberec0c5972013-02-05 14:47:13 -08002037 driver->notifyPrepareCompleted(err);
Andreas Huber9575c962013-02-05 13:59:56 -08002038 }
Andreas Huber99759402013-04-01 14:28:31 -07002039
Andreas Huber9575c962013-02-05 13:59:56 -08002040 break;
2041 }
2042
2043 case Source::kWhatFlagsChanged:
2044 {
2045 uint32_t flags;
2046 CHECK(msg->findInt32("flags", (int32_t *)&flags));
2047
Chong Zhang4b7069d2013-09-11 12:52:43 -07002048 sp<NuPlayerDriver> driver = mDriver.promote();
2049 if (driver != NULL) {
Wei Jia895651b2014-12-10 17:31:52 -08002050 if ((flags & NuPlayer::Source::FLAG_CAN_SEEK) == 0) {
2051 driver->notifyListener(
2052 MEDIA_INFO, MEDIA_INFO_NOT_SEEKABLE, 0);
2053 }
Chong Zhang4b7069d2013-09-11 12:52:43 -07002054 driver->notifyFlagsChanged(flags);
2055 }
2056
Andreas Huber9575c962013-02-05 13:59:56 -08002057 if ((mSourceFlags & Source::FLAG_DYNAMIC_DURATION)
2058 && (!(flags & Source::FLAG_DYNAMIC_DURATION))) {
2059 cancelPollDuration();
2060 } else if (!(mSourceFlags & Source::FLAG_DYNAMIC_DURATION)
2061 && (flags & Source::FLAG_DYNAMIC_DURATION)
2062 && (mAudioDecoder != NULL || mVideoDecoder != NULL)) {
2063 schedulePollDuration();
2064 }
2065
2066 mSourceFlags = flags;
2067 break;
2068 }
2069
2070 case Source::kWhatVideoSizeChanged:
2071 {
Chong Zhangced1c2f2014-08-08 15:22:35 -07002072 sp<AMessage> format;
2073 CHECK(msg->findMessage("format", &format));
Andreas Huber9575c962013-02-05 13:59:56 -08002074
Chong Zhangced1c2f2014-08-08 15:22:35 -07002075 updateVideoSize(format);
Andreas Huber9575c962013-02-05 13:59:56 -08002076 break;
2077 }
2078
Chong Zhang2a3cc9a2014-08-21 17:48:26 -07002079 case Source::kWhatBufferingUpdate:
2080 {
2081 int32_t percentage;
2082 CHECK(msg->findInt32("percentage", &percentage));
2083
2084 notifyListener(MEDIA_BUFFERING_UPDATE, percentage, 0);
2085 break;
2086 }
2087
Chong Zhangefbb6192015-01-30 17:13:27 -08002088 case Source::kWhatPauseOnBufferingStart:
2089 {
2090 // ignore if not playing
Chong Zhang8a048332015-05-06 15:16:28 -07002091 if (mStarted) {
Chong Zhangefbb6192015-01-30 17:13:27 -08002092 ALOGI("buffer low, pausing...");
2093
Chong Zhang8a048332015-05-06 15:16:28 -07002094 mPausedForBuffering = true;
Chong Zhangefbb6192015-01-30 17:13:27 -08002095 onPause();
2096 }
2097 // fall-thru
2098 }
2099
Roger Jönssonb50e83e2013-01-21 16:26:41 +01002100 case Source::kWhatBufferingStart:
2101 {
2102 notifyListener(MEDIA_INFO, MEDIA_INFO_BUFFERING_START, 0);
2103 break;
2104 }
2105
Chong Zhangefbb6192015-01-30 17:13:27 -08002106 case Source::kWhatResumeOnBufferingEnd:
2107 {
2108 // ignore if not playing
Chong Zhang8a048332015-05-06 15:16:28 -07002109 if (mStarted) {
Chong Zhangefbb6192015-01-30 17:13:27 -08002110 ALOGI("buffer ready, resuming...");
2111
Chong Zhang8a048332015-05-06 15:16:28 -07002112 mPausedForBuffering = false;
2113
2114 // do not resume yet if client didn't unpause
2115 if (!mPausedByClient) {
2116 onResume();
2117 }
Chong Zhangefbb6192015-01-30 17:13:27 -08002118 }
2119 // fall-thru
2120 }
2121
Roger Jönssonb50e83e2013-01-21 16:26:41 +01002122 case Source::kWhatBufferingEnd:
2123 {
2124 notifyListener(MEDIA_INFO, MEDIA_INFO_BUFFERING_END, 0);
2125 break;
2126 }
2127
Chong Zhangefbb6192015-01-30 17:13:27 -08002128 case Source::kWhatCacheStats:
2129 {
2130 int32_t kbps;
2131 CHECK(msg->findInt32("bandwidth", &kbps));
2132
2133 notifyListener(MEDIA_INFO, MEDIA_INFO_NETWORK_BANDWIDTH, kbps);
2134 break;
2135 }
2136
Chong Zhangdcb89b32013-08-06 09:44:47 -07002137 case Source::kWhatSubtitleData:
2138 {
2139 sp<ABuffer> buffer;
2140 CHECK(msg->findBuffer("buffer", &buffer));
2141
Chong Zhang404fced2014-06-11 14:45:31 -07002142 sendSubtitleData(buffer, 0 /* baseIndex */);
Chong Zhangdcb89b32013-08-06 09:44:47 -07002143 break;
2144 }
2145
Robert Shih08528432015-04-08 09:06:54 -07002146 case Source::kWhatTimedMetaData:
2147 {
2148 sp<ABuffer> buffer;
2149 if (!msg->findBuffer("buffer", &buffer)) {
2150 notifyListener(MEDIA_INFO, MEDIA_INFO_METADATA_UPDATE, 0);
2151 } else {
2152 sendTimedMetaData(buffer);
2153 }
2154 break;
2155 }
2156
Robert Shihd3b0bbb2014-07-23 15:00:25 -07002157 case Source::kWhatTimedTextData:
2158 {
2159 int32_t generation;
2160 if (msg->findInt32("generation", &generation)
2161 && generation != mTimedTextGeneration) {
2162 break;
2163 }
2164
2165 sp<ABuffer> buffer;
2166 CHECK(msg->findBuffer("buffer", &buffer));
2167
2168 sp<NuPlayerDriver> driver = mDriver.promote();
2169 if (driver == NULL) {
2170 break;
2171 }
2172
2173 int posMs;
2174 int64_t timeUs, posUs;
2175 driver->getCurrentPosition(&posMs);
2176 posUs = posMs * 1000;
2177 CHECK(buffer->meta()->findInt64("timeUs", &timeUs));
2178
2179 if (posUs < timeUs) {
2180 if (!msg->findInt32("generation", &generation)) {
2181 msg->setInt32("generation", mTimedTextGeneration);
2182 }
2183 msg->post(timeUs - posUs);
2184 } else {
2185 sendTimedTextData(buffer);
2186 }
2187 break;
2188 }
2189
Andreas Huber14f76722013-01-15 09:04:18 -08002190 case Source::kWhatQueueDecoderShutdown:
2191 {
2192 int32_t audio, video;
2193 CHECK(msg->findInt32("audio", &audio));
2194 CHECK(msg->findInt32("video", &video));
2195
2196 sp<AMessage> reply;
2197 CHECK(msg->findMessage("reply", &reply));
2198
2199 queueDecoderShutdown(audio, video, reply);
2200 break;
2201 }
2202
Ronghua Wu80276872014-08-28 15:50:29 -07002203 case Source::kWhatDrmNoLicense:
2204 {
2205 notifyListener(MEDIA_ERROR, MEDIA_ERROR_UNKNOWN, ERROR_DRM_NO_LICENSE);
2206 break;
2207 }
2208
Andreas Huber9575c962013-02-05 13:59:56 -08002209 default:
2210 TRESPASS();
2211 }
2212}
2213
Chong Zhanga7fa1d92014-06-11 14:49:23 -07002214void NuPlayer::onClosedCaptionNotify(const sp<AMessage> &msg) {
2215 int32_t what;
2216 CHECK(msg->findInt32("what", &what));
2217
2218 switch (what) {
2219 case NuPlayer::CCDecoder::kWhatClosedCaptionData:
2220 {
2221 sp<ABuffer> buffer;
2222 CHECK(msg->findBuffer("buffer", &buffer));
2223
2224 size_t inbandTracks = 0;
2225 if (mSource != NULL) {
2226 inbandTracks = mSource->getTrackCount();
2227 }
2228
2229 sendSubtitleData(buffer, inbandTracks);
2230 break;
2231 }
2232
2233 case NuPlayer::CCDecoder::kWhatTrackAdded:
2234 {
2235 notifyListener(MEDIA_INFO, MEDIA_INFO_METADATA_UPDATE, 0);
2236
2237 break;
2238 }
2239
2240 default:
2241 TRESPASS();
2242 }
2243
2244
2245}
2246
Chong Zhang404fced2014-06-11 14:45:31 -07002247void NuPlayer::sendSubtitleData(const sp<ABuffer> &buffer, int32_t baseIndex) {
2248 int32_t trackIndex;
2249 int64_t timeUs, durationUs;
2250 CHECK(buffer->meta()->findInt32("trackIndex", &trackIndex));
2251 CHECK(buffer->meta()->findInt64("timeUs", &timeUs));
2252 CHECK(buffer->meta()->findInt64("durationUs", &durationUs));
2253
2254 Parcel in;
2255 in.writeInt32(trackIndex + baseIndex);
2256 in.writeInt64(timeUs);
2257 in.writeInt64(durationUs);
2258 in.writeInt32(buffer->size());
2259 in.writeInt32(buffer->size());
2260 in.write(buffer->data(), buffer->size());
2261
2262 notifyListener(MEDIA_SUBTITLE_DATA, 0, 0, &in);
2263}
Robert Shihd3b0bbb2014-07-23 15:00:25 -07002264
Robert Shih08528432015-04-08 09:06:54 -07002265void NuPlayer::sendTimedMetaData(const sp<ABuffer> &buffer) {
2266 int64_t timeUs;
2267 CHECK(buffer->meta()->findInt64("timeUs", &timeUs));
2268
2269 Parcel in;
2270 in.writeInt64(timeUs);
2271 in.writeInt32(buffer->size());
2272 in.writeInt32(buffer->size());
2273 in.write(buffer->data(), buffer->size());
2274
2275 notifyListener(MEDIA_META_DATA, 0, 0, &in);
2276}
2277
Robert Shihd3b0bbb2014-07-23 15:00:25 -07002278void NuPlayer::sendTimedTextData(const sp<ABuffer> &buffer) {
2279 const void *data;
2280 size_t size = 0;
2281 int64_t timeUs;
2282 int32_t flag = TextDescriptions::LOCAL_DESCRIPTIONS;
2283
2284 AString mime;
2285 CHECK(buffer->meta()->findString("mime", &mime));
2286 CHECK(strcasecmp(mime.c_str(), MEDIA_MIMETYPE_TEXT_3GPP) == 0);
2287
2288 data = buffer->data();
2289 size = buffer->size();
2290
2291 Parcel parcel;
2292 if (size > 0) {
2293 CHECK(buffer->meta()->findInt64("timeUs", &timeUs));
2294 flag |= TextDescriptions::IN_BAND_TEXT_3GPP;
2295 TextDescriptions::getParcelOfDescriptions(
2296 (const uint8_t *)data, size, flag, timeUs / 1000, &parcel);
2297 }
2298
2299 if ((parcel.dataSize() > 0)) {
2300 notifyListener(MEDIA_TIMED_TEXT, 0, 0, &parcel);
2301 } else { // send an empty timed text
2302 notifyListener(MEDIA_TIMED_TEXT, 0, 0);
2303 }
2304}
Andreas Huberb5f25f02013-02-05 10:14:26 -08002305////////////////////////////////////////////////////////////////////////////////
2306
Chong Zhangced1c2f2014-08-08 15:22:35 -07002307sp<AMessage> NuPlayer::Source::getFormat(bool audio) {
2308 sp<MetaData> meta = getFormatMeta(audio);
2309
2310 if (meta == NULL) {
2311 return NULL;
2312 }
2313
2314 sp<AMessage> msg = new AMessage;
2315
2316 if(convertMetaDataToMessage(meta, &msg) == OK) {
2317 return msg;
2318 }
2319 return NULL;
2320}
2321
Andreas Huber9575c962013-02-05 13:59:56 -08002322void NuPlayer::Source::notifyFlagsChanged(uint32_t flags) {
2323 sp<AMessage> notify = dupNotify();
2324 notify->setInt32("what", kWhatFlagsChanged);
2325 notify->setInt32("flags", flags);
2326 notify->post();
2327}
2328
Chong Zhangced1c2f2014-08-08 15:22:35 -07002329void NuPlayer::Source::notifyVideoSizeChanged(const sp<AMessage> &format) {
Andreas Huber9575c962013-02-05 13:59:56 -08002330 sp<AMessage> notify = dupNotify();
2331 notify->setInt32("what", kWhatVideoSizeChanged);
Chong Zhangced1c2f2014-08-08 15:22:35 -07002332 notify->setMessage("format", format);
Andreas Huber9575c962013-02-05 13:59:56 -08002333 notify->post();
2334}
2335
Andreas Huberec0c5972013-02-05 14:47:13 -08002336void NuPlayer::Source::notifyPrepared(status_t err) {
Andreas Huber9575c962013-02-05 13:59:56 -08002337 sp<AMessage> notify = dupNotify();
2338 notify->setInt32("what", kWhatPrepared);
Andreas Huberec0c5972013-02-05 14:47:13 -08002339 notify->setInt32("err", err);
Andreas Huber9575c962013-02-05 13:59:56 -08002340 notify->post();
2341}
2342
Lajos Molnarfcd3e942015-03-31 10:06:48 -07002343void NuPlayer::Source::notifyInstantiateSecureDecoders(const sp<AMessage> &reply) {
2344 sp<AMessage> notify = dupNotify();
2345 notify->setInt32("what", kWhatInstantiateSecureDecoders);
2346 notify->setMessage("reply", reply);
2347 notify->post();
2348}
2349
Andreas Huber84333e02014-02-07 15:36:10 -08002350void NuPlayer::Source::onMessageReceived(const sp<AMessage> & /* msg */) {
Andreas Huberb5f25f02013-02-05 10:14:26 -08002351 TRESPASS();
2352}
2353
Andreas Huberf9334412010-12-15 15:17:42 -08002354} // namespace android