blob: ac4999380bec719b72e366e1e6323f3ebf768a90 [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>
Wonsik Kim7e34bf52016-08-23 00:09:18 +090042#include <media/MediaCodecBuffer.h>
Lajos Molnar3a474aa2015-04-24 17:10:07 -070043
Andreas Huber3831a062010-12-21 10:22:33 -080044#include <media/stagefright/foundation/hexdump.h>
Andreas Huberf9334412010-12-15 15:17:42 -080045#include <media/stagefright/foundation/ABuffer.h>
46#include <media/stagefright/foundation/ADebug.h>
47#include <media/stagefright/foundation/AMessage.h>
Lajos Molnar09524832014-07-17 14:29:51 -070048#include <media/stagefright/MediaBuffer.h>
Andreas Huber3fe62152011-09-16 15:09:22 -070049#include <media/stagefright/MediaDefs.h>
Andreas Huberf9334412010-12-15 15:17:42 -080050#include <media/stagefright/MediaErrors.h>
51#include <media/stagefright/MetaData.h>
Lajos Molnar1de1e252015-04-30 18:18:34 -070052
Andy McFadden8ba01022012-12-18 09:46:54 -080053#include <gui/IGraphicBufferProducer.h>
Lajos Molnar1de1e252015-04-30 18:18:34 -070054#include <gui/Surface.h>
Andreas Huberf9334412010-12-15 15:17:42 -080055
Andreas Huber3fe62152011-09-16 15:09:22 -070056#include "avc_utils.h"
57
Andreas Huber84066782011-08-16 09:34:26 -070058#include "ESDS.h"
59#include <media/stagefright/Utils.h>
60
Andreas Huberf9334412010-12-15 15:17:42 -080061namespace android {
62
Andreas Hubera1f8ab02012-11-30 10:53:22 -080063struct NuPlayer::Action : public RefBase {
64 Action() {}
65
66 virtual void execute(NuPlayer *player) = 0;
67
68private:
69 DISALLOW_EVIL_CONSTRUCTORS(Action);
70};
71
72struct NuPlayer::SeekAction : public Action {
Chih-Hung Hsieh090ef602016-04-27 10:39:54 -070073 explicit SeekAction(int64_t seekTimeUs)
Wei Jia29840802015-05-15 17:11:38 -070074 : mSeekTimeUs(seekTimeUs) {
Andreas Hubera1f8ab02012-11-30 10:53:22 -080075 }
76
77 virtual void execute(NuPlayer *player) {
Wei Jia29840802015-05-15 17:11:38 -070078 player->performSeek(mSeekTimeUs);
Andreas Hubera1f8ab02012-11-30 10:53:22 -080079 }
80
81private:
82 int64_t mSeekTimeUs;
83
84 DISALLOW_EVIL_CONSTRUCTORS(SeekAction);
85};
86
Chong Zhangf8d71772014-11-26 15:08:34 -080087struct NuPlayer::ResumeDecoderAction : public Action {
Chih-Hung Hsieh090ef602016-04-27 10:39:54 -070088 explicit ResumeDecoderAction(bool needNotify)
Chong Zhangf8d71772014-11-26 15:08:34 -080089 : mNeedNotify(needNotify) {
90 }
91
92 virtual void execute(NuPlayer *player) {
93 player->performResumeDecoders(mNeedNotify);
94 }
95
96private:
97 bool mNeedNotify;
98
99 DISALLOW_EVIL_CONSTRUCTORS(ResumeDecoderAction);
100};
101
Andreas Huber57a339c2012-12-03 11:18:00 -0800102struct NuPlayer::SetSurfaceAction : public Action {
Chih-Hung Hsieh090ef602016-04-27 10:39:54 -0700103 explicit SetSurfaceAction(const sp<Surface> &surface)
Lajos Molnar1de1e252015-04-30 18:18:34 -0700104 : mSurface(surface) {
Andreas Huber57a339c2012-12-03 11:18:00 -0800105 }
106
107 virtual void execute(NuPlayer *player) {
Lajos Molnar1de1e252015-04-30 18:18:34 -0700108 player->performSetSurface(mSurface);
Andreas Huber57a339c2012-12-03 11:18:00 -0800109 }
110
111private:
Lajos Molnar1de1e252015-04-30 18:18:34 -0700112 sp<Surface> mSurface;
Andreas Huber57a339c2012-12-03 11:18:00 -0800113
114 DISALLOW_EVIL_CONSTRUCTORS(SetSurfaceAction);
115};
116
Wei Jiafef808d2014-10-31 17:57:05 -0700117struct NuPlayer::FlushDecoderAction : public Action {
118 FlushDecoderAction(FlushCommand audio, FlushCommand video)
Andreas Huber14f76722013-01-15 09:04:18 -0800119 : mAudio(audio),
120 mVideo(video) {
121 }
122
123 virtual void execute(NuPlayer *player) {
Wei Jiafef808d2014-10-31 17:57:05 -0700124 player->performDecoderFlush(mAudio, mVideo);
Andreas Huber14f76722013-01-15 09:04:18 -0800125 }
126
127private:
Wei Jiafef808d2014-10-31 17:57:05 -0700128 FlushCommand mAudio;
129 FlushCommand mVideo;
Andreas Huber14f76722013-01-15 09:04:18 -0800130
Wei Jiafef808d2014-10-31 17:57:05 -0700131 DISALLOW_EVIL_CONSTRUCTORS(FlushDecoderAction);
Andreas Huber14f76722013-01-15 09:04:18 -0800132};
133
134struct NuPlayer::PostMessageAction : public Action {
Chih-Hung Hsieh090ef602016-04-27 10:39:54 -0700135 explicit PostMessageAction(const sp<AMessage> &msg)
Andreas Huber14f76722013-01-15 09:04:18 -0800136 : mMessage(msg) {
137 }
138
139 virtual void execute(NuPlayer *) {
140 mMessage->post();
141 }
142
143private:
144 sp<AMessage> mMessage;
145
146 DISALLOW_EVIL_CONSTRUCTORS(PostMessageAction);
147};
148
Andreas Hubera1f8ab02012-11-30 10:53:22 -0800149// Use this if there's no state necessary to save in order to execute
150// the action.
151struct NuPlayer::SimpleAction : public Action {
152 typedef void (NuPlayer::*ActionFunc)();
153
Chih-Hung Hsieh090ef602016-04-27 10:39:54 -0700154 explicit SimpleAction(ActionFunc func)
Andreas Hubera1f8ab02012-11-30 10:53:22 -0800155 : mFunc(func) {
156 }
157
158 virtual void execute(NuPlayer *player) {
159 (player->*mFunc)();
160 }
161
162private:
163 ActionFunc mFunc;
164
165 DISALLOW_EVIL_CONSTRUCTORS(SimpleAction);
166};
167
Andreas Huberf9334412010-12-15 15:17:42 -0800168////////////////////////////////////////////////////////////////////////////////
169
Ronghua Wu68845c12015-07-21 09:50:48 -0700170NuPlayer::NuPlayer(pid_t pid)
Andreas Huber9b80c2b2011-06-30 15:47:02 -0700171 : mUIDValid(false),
Ronghua Wu68845c12015-07-21 09:50:48 -0700172 mPID(pid),
Andreas Huber9575c962013-02-05 13:59:56 -0800173 mSourceFlags(0),
Wei Jiabc2fb722014-07-08 16:37:57 -0700174 mOffloadAudio(false),
Wei Jia88703c32014-08-06 11:24:07 -0700175 mAudioDecoderGeneration(0),
176 mVideoDecoderGeneration(0),
Wei Jia57568df2014-09-22 10:16:29 -0700177 mRendererGeneration(0),
Robert Shih1a5c8592015-08-04 18:07:44 -0700178 mPreviousSeekTimeUs(0),
Andreas Huber9b80c2b2011-06-30 15:47:02 -0700179 mAudioEOS(false),
Andreas Huberf9334412010-12-15 15:17:42 -0800180 mVideoEOS(false),
Andreas Huber5bc087c2010-12-23 10:27:40 -0800181 mScanSourcesPending(false),
Andreas Huber1aef2112011-01-04 14:01:29 -0800182 mScanSourcesGeneration(0),
Andreas Huberb7c8e912012-11-27 15:02:53 -0800183 mPollDurationGeneration(0),
Robert Shihd3b0bbb2014-07-23 15:00:25 -0700184 mTimedTextGeneration(0),
Andreas Huberf9334412010-12-15 15:17:42 -0800185 mFlushingAudio(NONE),
Andreas Huber1aef2112011-01-04 14:01:29 -0800186 mFlushingVideo(NONE),
Chong Zhangf8d71772014-11-26 15:08:34 -0800187 mResumePending(false),
Andreas Huber57a339c2012-12-03 11:18:00 -0800188 mVideoScalingMode(NATIVE_WINDOW_SCALING_MODE_SCALE_TO_WINDOW),
Lajos Molnar3a474aa2015-04-24 17:10:07 -0700189 mPlaybackSettings(AUDIO_PLAYBACK_RATE_DEFAULT),
190 mVideoFpsHint(-1.f),
Chong Zhangefbb6192015-01-30 17:13:27 -0800191 mStarted(false),
Wei Jia8a092d32016-06-03 14:57:24 -0700192 mPrepared(false),
Ronghua Wu64c2d172015-10-07 16:52:19 -0700193 mResetting(false),
Robert Shih0c61a0d2015-07-06 15:09:10 -0700194 mSourceStarted(false),
Chong Zhangefbb6192015-01-30 17:13:27 -0800195 mPaused(false),
Wei Jia71c75e02016-02-04 09:40:47 -0800196 mPausedByClient(true),
Chong Zhang8a048332015-05-06 15:16:28 -0700197 mPausedForBuffering(false) {
Andy Hung8d121d42014-10-03 09:53:53 -0700198 clearFlushComplete();
Andreas Huberf9334412010-12-15 15:17:42 -0800199}
200
201NuPlayer::~NuPlayer() {
202}
203
Andreas Huber9b80c2b2011-06-30 15:47:02 -0700204void NuPlayer::setUID(uid_t uid) {
205 mUIDValid = true;
206 mUID = uid;
207}
208
Andreas Huber43c3e6c2011-01-05 12:17:08 -0800209void NuPlayer::setDriver(const wp<NuPlayerDriver> &driver) {
210 mDriver = driver;
Andreas Huberf9334412010-12-15 15:17:42 -0800211}
212
Andreas Huber9575c962013-02-05 13:59:56 -0800213void NuPlayer::setDataSourceAsync(const sp<IStreamSource> &source) {
Lajos Molnar1d15ab52015-03-04 16:46:34 -0800214 sp<AMessage> msg = new AMessage(kWhatSetDataSource, this);
Andreas Huberf9334412010-12-15 15:17:42 -0800215
Lajos Molnar1d15ab52015-03-04 16:46:34 -0800216 sp<AMessage> notify = new AMessage(kWhatSourceNotify, this);
Andreas Huberb5f25f02013-02-05 10:14:26 -0800217
Andreas Huber240abcc2014-02-13 13:32:37 -0800218 msg->setObject("source", new StreamingSource(notify, source));
Andreas Huber5bc087c2010-12-23 10:27:40 -0800219 msg->post();
220}
Andreas Huberf9334412010-12-15 15:17:42 -0800221
Andreas Huberafed0e12011-09-20 15:39:58 -0700222static bool IsHTTPLiveURL(const char *url) {
223 if (!strncasecmp("http://", url, 7)
Andreas Huber99759402013-04-01 14:28:31 -0700224 || !strncasecmp("https://", url, 8)
225 || !strncasecmp("file://", url, 7)) {
Andreas Huberafed0e12011-09-20 15:39:58 -0700226 size_t len = strlen(url);
227 if (len >= 5 && !strcasecmp(".m3u8", &url[len - 5])) {
228 return true;
229 }
230
231 if (strstr(url,"m3u8")) {
232 return true;
233 }
234 }
235
236 return false;
237}
238
Andreas Huber9575c962013-02-05 13:59:56 -0800239void NuPlayer::setDataSourceAsync(
Andreas Huber1b86fe02014-01-29 11:13:26 -0800240 const sp<IMediaHTTPService> &httpService,
241 const char *url,
242 const KeyedVector<String8, String8> *headers) {
Chong Zhang3de157d2014-08-05 20:54:44 -0700243
Lajos Molnar1d15ab52015-03-04 16:46:34 -0800244 sp<AMessage> msg = new AMessage(kWhatSetDataSource, this);
Oscar Rydhé7a33b772012-02-20 10:15:48 +0100245 size_t len = strlen(url);
Andreas Huber5bc087c2010-12-23 10:27:40 -0800246
Lajos Molnar1d15ab52015-03-04 16:46:34 -0800247 sp<AMessage> notify = new AMessage(kWhatSourceNotify, this);
Andreas Huberb5f25f02013-02-05 10:14:26 -0800248
Andreas Huberafed0e12011-09-20 15:39:58 -0700249 sp<Source> source;
250 if (IsHTTPLiveURL(url)) {
Andreas Huber81e68442014-02-05 11:52:33 -0800251 source = new HTTPLiveSource(notify, httpService, url, headers);
Andreas Huberafed0e12011-09-20 15:39:58 -0700252 } else if (!strncasecmp(url, "rtsp://", 7)) {
Andreas Huber1b86fe02014-01-29 11:13:26 -0800253 source = new RTSPSource(
254 notify, httpService, url, headers, mUIDValid, mUID);
Oscar Rydhé7a33b772012-02-20 10:15:48 +0100255 } else if ((!strncasecmp(url, "http://", 7)
256 || !strncasecmp(url, "https://", 8))
257 && ((len >= 4 && !strcasecmp(".sdp", &url[len - 4]))
258 || strstr(url, ".sdp?"))) {
Andreas Huber1b86fe02014-01-29 11:13:26 -0800259 source = new RTSPSource(
260 notify, httpService, url, headers, mUIDValid, mUID, true);
Andreas Huber2bfdd422011-10-11 15:24:07 -0700261 } else {
Chong Zhang3de157d2014-08-05 20:54:44 -0700262 sp<GenericSource> genericSource =
263 new GenericSource(notify, mUIDValid, mUID);
264 // Don't set FLAG_SECURE on mSourceFlags here for widevine.
265 // The correct flags will be updated in Source::kWhatFlagsChanged
266 // handler when GenericSource is prepared.
Andreas Huber2bfdd422011-10-11 15:24:07 -0700267
Chong Zhanga19f33e2014-08-07 15:35:07 -0700268 status_t err = genericSource->setDataSource(httpService, url, headers);
Chong Zhang3de157d2014-08-05 20:54:44 -0700269
270 if (err == OK) {
271 source = genericSource;
272 } else {
Chong Zhanga19f33e2014-08-07 15:35:07 -0700273 ALOGE("Failed to set data source!");
Chong Zhang3de157d2014-08-05 20:54:44 -0700274 }
275 }
Andreas Huberafed0e12011-09-20 15:39:58 -0700276 msg->setObject("source", source);
277 msg->post();
278}
279
Andreas Huber9575c962013-02-05 13:59:56 -0800280void NuPlayer::setDataSourceAsync(int fd, int64_t offset, int64_t length) {
Lajos Molnar1d15ab52015-03-04 16:46:34 -0800281 sp<AMessage> msg = new AMessage(kWhatSetDataSource, this);
Andreas Huberafed0e12011-09-20 15:39:58 -0700282
Lajos Molnar1d15ab52015-03-04 16:46:34 -0800283 sp<AMessage> notify = new AMessage(kWhatSourceNotify, this);
Andreas Huberb5f25f02013-02-05 10:14:26 -0800284
Chong Zhang3de157d2014-08-05 20:54:44 -0700285 sp<GenericSource> source =
286 new GenericSource(notify, mUIDValid, mUID);
287
Chong Zhanga19f33e2014-08-07 15:35:07 -0700288 status_t err = source->setDataSource(fd, offset, length);
Chong Zhang3de157d2014-08-05 20:54:44 -0700289
290 if (err != OK) {
Chong Zhanga19f33e2014-08-07 15:35:07 -0700291 ALOGE("Failed to set data source!");
Chong Zhang3de157d2014-08-05 20:54:44 -0700292 source = NULL;
293 }
294
Andreas Huberafed0e12011-09-20 15:39:58 -0700295 msg->setObject("source", source);
Andreas Huberf9334412010-12-15 15:17:42 -0800296 msg->post();
297}
298
Chris Watkins99f31602015-03-20 13:06:33 -0700299void NuPlayer::setDataSourceAsync(const sp<DataSource> &dataSource) {
300 sp<AMessage> msg = new AMessage(kWhatSetDataSource, this);
301 sp<AMessage> notify = new AMessage(kWhatSourceNotify, this);
302
303 sp<GenericSource> source = new GenericSource(notify, mUIDValid, mUID);
304 status_t err = source->setDataSource(dataSource);
305
306 if (err != OK) {
307 ALOGE("Failed to set data source!");
308 source = NULL;
309 }
310
311 msg->setObject("source", source);
312 msg->post();
313}
314
Andreas Huber9575c962013-02-05 13:59:56 -0800315void NuPlayer::prepareAsync() {
Lajos Molnar1d15ab52015-03-04 16:46:34 -0800316 (new AMessage(kWhatPrepare, this))->post();
Andreas Huber9575c962013-02-05 13:59:56 -0800317}
318
Andreas Huber57a339c2012-12-03 11:18:00 -0800319void NuPlayer::setVideoSurfaceTextureAsync(
Andy McFadden8ba01022012-12-18 09:46:54 -0800320 const sp<IGraphicBufferProducer> &bufferProducer) {
Lajos Molnar1de1e252015-04-30 18:18:34 -0700321 sp<AMessage> msg = new AMessage(kWhatSetVideoSurface, this);
Andreas Huber57a339c2012-12-03 11:18:00 -0800322
Andy McFadden8ba01022012-12-18 09:46:54 -0800323 if (bufferProducer == NULL) {
Lajos Molnar1de1e252015-04-30 18:18:34 -0700324 msg->setObject("surface", NULL);
Andreas Huber57a339c2012-12-03 11:18:00 -0800325 } else {
Lajos Molnar1de1e252015-04-30 18:18:34 -0700326 msg->setObject("surface", new Surface(bufferProducer, true /* controlledByApp */));
Andreas Huber57a339c2012-12-03 11:18:00 -0800327 }
328
Andreas Huberf9334412010-12-15 15:17:42 -0800329 msg->post();
330}
331
332void NuPlayer::setAudioSink(const sp<MediaPlayerBase::AudioSink> &sink) {
Lajos Molnar1d15ab52015-03-04 16:46:34 -0800333 sp<AMessage> msg = new AMessage(kWhatSetAudioSink, this);
Andreas Huberf9334412010-12-15 15:17:42 -0800334 msg->setObject("sink", sink);
335 msg->post();
336}
337
338void NuPlayer::start() {
Lajos Molnar1d15ab52015-03-04 16:46:34 -0800339 (new AMessage(kWhatStart, this))->post();
Andreas Huberf9334412010-12-15 15:17:42 -0800340}
341
Lajos Molnar3a474aa2015-04-24 17:10:07 -0700342status_t NuPlayer::setPlaybackSettings(const AudioPlaybackRate &rate) {
343 // do some cursory validation of the settings here. audio modes are
344 // only validated when set on the audiosink.
345 if ((rate.mSpeed != 0.f && rate.mSpeed < AUDIO_TIMESTRETCH_SPEED_MIN)
346 || rate.mSpeed > AUDIO_TIMESTRETCH_SPEED_MAX
347 || rate.mPitch < AUDIO_TIMESTRETCH_SPEED_MIN
348 || rate.mPitch > AUDIO_TIMESTRETCH_SPEED_MAX) {
349 return BAD_VALUE;
350 }
351 sp<AMessage> msg = new AMessage(kWhatConfigPlayback, this);
352 writeToAMessage(msg, rate);
353 sp<AMessage> response;
354 status_t err = msg->postAndAwaitResponse(&response);
355 if (err == OK && response != NULL) {
356 CHECK(response->findInt32("err", &err));
357 }
358 return err;
359}
360
361status_t NuPlayer::getPlaybackSettings(AudioPlaybackRate *rate /* nonnull */) {
362 sp<AMessage> msg = new AMessage(kWhatGetPlaybackSettings, this);
363 sp<AMessage> response;
364 status_t err = msg->postAndAwaitResponse(&response);
365 if (err == OK && response != NULL) {
366 CHECK(response->findInt32("err", &err));
367 if (err == OK) {
368 readFromAMessage(response, rate);
369 }
370 }
371 return err;
372}
373
374status_t NuPlayer::setSyncSettings(const AVSyncSettings &sync, float videoFpsHint) {
375 sp<AMessage> msg = new AMessage(kWhatConfigSync, this);
376 writeToAMessage(msg, sync, videoFpsHint);
377 sp<AMessage> response;
378 status_t err = msg->postAndAwaitResponse(&response);
379 if (err == OK && response != NULL) {
380 CHECK(response->findInt32("err", &err));
381 }
382 return err;
383}
384
385status_t NuPlayer::getSyncSettings(
386 AVSyncSettings *sync /* nonnull */, float *videoFps /* nonnull */) {
387 sp<AMessage> msg = new AMessage(kWhatGetSyncSettings, this);
388 sp<AMessage> response;
389 status_t err = msg->postAndAwaitResponse(&response);
390 if (err == OK && response != NULL) {
391 CHECK(response->findInt32("err", &err));
392 if (err == OK) {
393 readFromAMessage(response, sync, videoFps);
394 }
395 }
396 return err;
Wei Jia98160162015-02-04 17:01:11 -0800397}
398
Andreas Huber43c3e6c2011-01-05 12:17:08 -0800399void NuPlayer::pause() {
Lajos Molnar1d15ab52015-03-04 16:46:34 -0800400 (new AMessage(kWhatPause, this))->post();
Andreas Huber43c3e6c2011-01-05 12:17:08 -0800401}
402
Andreas Huber1aef2112011-01-04 14:01:29 -0800403void NuPlayer::resetAsync() {
Wei Jiac45a4b22016-04-15 15:30:23 -0700404 sp<Source> source;
405 {
406 Mutex::Autolock autoLock(mSourceLock);
407 source = mSource;
408 }
409
410 if (source != NULL) {
Chong Zhang48296b72014-09-14 14:28:45 -0700411 // During a reset, the data source might be unresponsive already, we need to
412 // disconnect explicitly so that reads exit promptly.
413 // We can't queue the disconnect request to the looper, as it might be
414 // queued behind a stuck read and never gets processed.
415 // Doing a disconnect outside the looper to allows the pending reads to exit
416 // (either successfully or with error).
Wei Jiac45a4b22016-04-15 15:30:23 -0700417 source->disconnect();
Chong Zhang48296b72014-09-14 14:28:45 -0700418 }
419
Lajos Molnar1d15ab52015-03-04 16:46:34 -0800420 (new AMessage(kWhatReset, this))->post();
Andreas Huber1aef2112011-01-04 14:01:29 -0800421}
422
Wei Jiae427abf2014-09-22 15:21:11 -0700423void NuPlayer::seekToAsync(int64_t seekTimeUs, bool needNotify) {
Lajos Molnar1d15ab52015-03-04 16:46:34 -0800424 sp<AMessage> msg = new AMessage(kWhatSeek, this);
Andreas Huber43c3e6c2011-01-05 12:17:08 -0800425 msg->setInt64("seekTimeUs", seekTimeUs);
Wei Jiae427abf2014-09-22 15:21:11 -0700426 msg->setInt32("needNotify", needNotify);
Andreas Huber43c3e6c2011-01-05 12:17:08 -0800427 msg->post();
428}
429
Andreas Huber53df1a42010-12-22 10:03:04 -0800430
Chong Zhang404fced2014-06-11 14:45:31 -0700431void NuPlayer::writeTrackInfo(
Chih-Hung Hsiehe964d4e2016-08-09 14:31:32 -0700432 Parcel* reply, const sp<AMessage>& format) const {
Marco Nelissenc367ca12015-09-15 09:51:59 -0700433 if (format == NULL) {
434 ALOGE("NULL format");
435 return;
436 }
Chong Zhang404fced2014-06-11 14:45:31 -0700437 int32_t trackType;
Marco Nelissen9436e482015-09-15 10:21:53 -0700438 if (!format->findInt32("type", &trackType)) {
Marco Nelissenc367ca12015-09-15 09:51:59 -0700439 ALOGE("no track type");
440 return;
441 }
Chong Zhang404fced2014-06-11 14:45:31 -0700442
Robert Shih755106e2015-04-30 14:36:45 -0700443 AString mime;
Robert Shih2e3a4252015-05-06 10:21:15 -0700444 if (!format->findString("mime", &mime)) {
445 // Java MediaPlayer only uses mimetype for subtitle and timedtext tracks.
446 // If we can't find the mimetype here it means that we wouldn't be needing
447 // the mimetype on the Java end. We still write a placeholder mime to keep the
448 // (de)serialization logic simple.
449 if (trackType == MEDIA_TRACK_TYPE_AUDIO) {
450 mime = "audio/";
451 } else if (trackType == MEDIA_TRACK_TYPE_VIDEO) {
452 mime = "video/";
453 } else {
Marco Nelissenc367ca12015-09-15 09:51:59 -0700454 ALOGE("unknown track type: %d", trackType);
455 return;
Robert Shih2e3a4252015-05-06 10:21:15 -0700456 }
457 }
Robert Shih755106e2015-04-30 14:36:45 -0700458
Chong Zhang404fced2014-06-11 14:45:31 -0700459 AString lang;
Marco Nelissen9436e482015-09-15 10:21:53 -0700460 if (!format->findString("language", &lang)) {
Marco Nelissenc367ca12015-09-15 09:51:59 -0700461 ALOGE("no language");
462 return;
463 }
Chong Zhang404fced2014-06-11 14:45:31 -0700464
465 reply->writeInt32(2); // write something non-zero
466 reply->writeInt32(trackType);
Robert Shih755106e2015-04-30 14:36:45 -0700467 reply->writeString16(String16(mime.c_str()));
Chong Zhang404fced2014-06-11 14:45:31 -0700468 reply->writeString16(String16(lang.c_str()));
469
470 if (trackType == MEDIA_TRACK_TYPE_SUBTITLE) {
Chong Zhang404fced2014-06-11 14:45:31 -0700471 int32_t isAuto, isDefault, isForced;
472 CHECK(format->findInt32("auto", &isAuto));
473 CHECK(format->findInt32("default", &isDefault));
474 CHECK(format->findInt32("forced", &isForced));
475
Chong Zhang404fced2014-06-11 14:45:31 -0700476 reply->writeInt32(isAuto);
477 reply->writeInt32(isDefault);
478 reply->writeInt32(isForced);
479 }
480}
481
Andreas Huberf9334412010-12-15 15:17:42 -0800482void NuPlayer::onMessageReceived(const sp<AMessage> &msg) {
483 switch (msg->what()) {
484 case kWhatSetDataSource:
485 {
Steve Block3856b092011-10-20 11:56:00 +0100486 ALOGV("kWhatSetDataSource");
Andreas Huberf9334412010-12-15 15:17:42 -0800487
488 CHECK(mSource == NULL);
489
Chong Zhang3de157d2014-08-05 20:54:44 -0700490 status_t err = OK;
Andreas Huber5bc087c2010-12-23 10:27:40 -0800491 sp<RefBase> obj;
492 CHECK(msg->findObject("source", &obj));
Chong Zhang3de157d2014-08-05 20:54:44 -0700493 if (obj != NULL) {
Wei Jiac45a4b22016-04-15 15:30:23 -0700494 Mutex::Autolock autoLock(mSourceLock);
Chong Zhang3de157d2014-08-05 20:54:44 -0700495 mSource = static_cast<Source *>(obj.get());
Chong Zhang3de157d2014-08-05 20:54:44 -0700496 } else {
497 err = UNKNOWN_ERROR;
498 }
Andreas Huber9575c962013-02-05 13:59:56 -0800499
500 CHECK(mDriver != NULL);
501 sp<NuPlayerDriver> driver = mDriver.promote();
502 if (driver != NULL) {
Chong Zhang3de157d2014-08-05 20:54:44 -0700503 driver->notifySetDataSourceCompleted(err);
Andreas Huber9575c962013-02-05 13:59:56 -0800504 }
505 break;
506 }
507
508 case kWhatPrepare:
509 {
510 mSource->prepareAsync();
Andreas Huberf9334412010-12-15 15:17:42 -0800511 break;
512 }
513
Chong Zhangdcb89b32013-08-06 09:44:47 -0700514 case kWhatGetTrackInfo:
515 {
Lajos Molnar3f274362015-03-05 14:35:41 -0800516 sp<AReplyToken> replyID;
Chong Zhangdcb89b32013-08-06 09:44:47 -0700517 CHECK(msg->senderAwaitsResponse(&replyID));
518
Chong Zhang404fced2014-06-11 14:45:31 -0700519 Parcel* reply;
520 CHECK(msg->findPointer("reply", (void**)&reply));
521
522 size_t inbandTracks = 0;
Chong Zhangdcb89b32013-08-06 09:44:47 -0700523 if (mSource != NULL) {
Chong Zhang404fced2014-06-11 14:45:31 -0700524 inbandTracks = mSource->getTrackCount();
525 }
526
Chong Zhanga7fa1d92014-06-11 14:49:23 -0700527 size_t ccTracks = 0;
528 if (mCCDecoder != NULL) {
529 ccTracks = mCCDecoder->getTrackCount();
530 }
531
Chong Zhang404fced2014-06-11 14:45:31 -0700532 // total track count
Chong Zhanga7fa1d92014-06-11 14:49:23 -0700533 reply->writeInt32(inbandTracks + ccTracks);
Chong Zhang404fced2014-06-11 14:45:31 -0700534
535 // write inband tracks
536 for (size_t i = 0; i < inbandTracks; ++i) {
537 writeTrackInfo(reply, mSource->getTrackInfo(i));
Chong Zhangdcb89b32013-08-06 09:44:47 -0700538 }
539
Chong Zhanga7fa1d92014-06-11 14:49:23 -0700540 // write CC track
541 for (size_t i = 0; i < ccTracks; ++i) {
542 writeTrackInfo(reply, mCCDecoder->getTrackInfo(i));
543 }
544
Chong Zhangdcb89b32013-08-06 09:44:47 -0700545 sp<AMessage> response = new AMessage;
Chong Zhangdcb89b32013-08-06 09:44:47 -0700546 response->postReply(replyID);
547 break;
548 }
549
Robert Shih7c4f0d72014-07-09 18:53:31 -0700550 case kWhatGetSelectedTrack:
551 {
552 status_t err = INVALID_OPERATION;
553 if (mSource != NULL) {
554 err = OK;
555
556 int32_t type32;
557 CHECK(msg->findInt32("type", (int32_t*)&type32));
558 media_track_type type = (media_track_type)type32;
559 ssize_t selectedTrack = mSource->getSelectedTrack(type);
560
561 Parcel* reply;
562 CHECK(msg->findPointer("reply", (void**)&reply));
563 reply->writeInt32(selectedTrack);
564 }
565
566 sp<AMessage> response = new AMessage;
567 response->setInt32("err", err);
568
Lajos Molnar3f274362015-03-05 14:35:41 -0800569 sp<AReplyToken> replyID;
Robert Shih7c4f0d72014-07-09 18:53:31 -0700570 CHECK(msg->senderAwaitsResponse(&replyID));
571 response->postReply(replyID);
572 break;
573 }
574
Chong Zhangdcb89b32013-08-06 09:44:47 -0700575 case kWhatSelectTrack:
576 {
Lajos Molnar3f274362015-03-05 14:35:41 -0800577 sp<AReplyToken> replyID;
Chong Zhangdcb89b32013-08-06 09:44:47 -0700578 CHECK(msg->senderAwaitsResponse(&replyID));
579
Chong Zhang404fced2014-06-11 14:45:31 -0700580 size_t trackIndex;
581 int32_t select;
Robert Shih6ffb1fd2014-10-29 16:24:32 -0700582 int64_t timeUs;
Chong Zhang404fced2014-06-11 14:45:31 -0700583 CHECK(msg->findSize("trackIndex", &trackIndex));
584 CHECK(msg->findInt32("select", &select));
Robert Shih6ffb1fd2014-10-29 16:24:32 -0700585 CHECK(msg->findInt64("timeUs", &timeUs));
Chong Zhang404fced2014-06-11 14:45:31 -0700586
Chong Zhangdcb89b32013-08-06 09:44:47 -0700587 status_t err = INVALID_OPERATION;
Chong Zhang404fced2014-06-11 14:45:31 -0700588
589 size_t inbandTracks = 0;
Chong Zhangdcb89b32013-08-06 09:44:47 -0700590 if (mSource != NULL) {
Chong Zhang404fced2014-06-11 14:45:31 -0700591 inbandTracks = mSource->getTrackCount();
592 }
Chong Zhanga7fa1d92014-06-11 14:49:23 -0700593 size_t ccTracks = 0;
594 if (mCCDecoder != NULL) {
595 ccTracks = mCCDecoder->getTrackCount();
596 }
Chong Zhang404fced2014-06-11 14:45:31 -0700597
598 if (trackIndex < inbandTracks) {
Robert Shih6ffb1fd2014-10-29 16:24:32 -0700599 err = mSource->selectTrack(trackIndex, select, timeUs);
Robert Shihd3b0bbb2014-07-23 15:00:25 -0700600
601 if (!select && err == OK) {
602 int32_t type;
603 sp<AMessage> info = mSource->getTrackInfo(trackIndex);
604 if (info != NULL
605 && info->findInt32("type", &type)
606 && type == MEDIA_TRACK_TYPE_TIMEDTEXT) {
607 ++mTimedTextGeneration;
608 }
609 }
Chong Zhanga7fa1d92014-06-11 14:49:23 -0700610 } else {
611 trackIndex -= inbandTracks;
612
613 if (trackIndex < ccTracks) {
614 err = mCCDecoder->selectTrack(trackIndex, select);
615 }
Chong Zhangdcb89b32013-08-06 09:44:47 -0700616 }
617
618 sp<AMessage> response = new AMessage;
619 response->setInt32("err", err);
620
621 response->postReply(replyID);
622 break;
623 }
624
Andreas Huberb7c8e912012-11-27 15:02:53 -0800625 case kWhatPollDuration:
626 {
627 int32_t generation;
628 CHECK(msg->findInt32("generation", &generation));
629
630 if (generation != mPollDurationGeneration) {
631 // stale
632 break;
633 }
634
635 int64_t durationUs;
636 if (mDriver != NULL && mSource->getDuration(&durationUs) == OK) {
637 sp<NuPlayerDriver> driver = mDriver.promote();
638 if (driver != NULL) {
639 driver->notifyDuration(durationUs);
640 }
641 }
642
643 msg->post(1000000ll); // poll again in a second.
644 break;
645 }
646
Lajos Molnar1de1e252015-04-30 18:18:34 -0700647 case kWhatSetVideoSurface:
Andreas Huberf9334412010-12-15 15:17:42 -0800648 {
Andreas Huberf9334412010-12-15 15:17:42 -0800649
650 sp<RefBase> obj;
Lajos Molnar1de1e252015-04-30 18:18:34 -0700651 CHECK(msg->findObject("surface", &obj));
652 sp<Surface> surface = static_cast<Surface *>(obj.get());
Lajos Molnara81c6222015-07-10 19:17:45 -0700653
654 ALOGD("onSetVideoSurface(%p, %s video decoder)",
655 surface.get(),
Wei Jia6b7d2ed2015-08-10 15:11:54 -0700656 (mSource != NULL && mStarted && mSource->getFormat(false /* audio */) != NULL
Lajos Molnara81c6222015-07-10 19:17:45 -0700657 && mVideoDecoder != NULL) ? "have" : "no");
658
Wei Jia6b7d2ed2015-08-10 15:11:54 -0700659 // Need to check mStarted before calling mSource->getFormat because NuPlayer might
660 // be in preparing state and it could take long time.
661 // When mStarted is true, mSource must have been set.
662 if (mSource == NULL || !mStarted || mSource->getFormat(false /* audio */) == NULL
Lajos Molnara81c6222015-07-10 19:17:45 -0700663 // NOTE: mVideoDecoder's mSurface is always non-null
664 || (mVideoDecoder != NULL && mVideoDecoder->setVideoSurface(surface) == OK)) {
Lajos Molnar1de1e252015-04-30 18:18:34 -0700665 performSetSurface(surface);
Wei Jiafef808d2014-10-31 17:57:05 -0700666 break;
667 }
668
669 mDeferredActions.push_back(
670 new FlushDecoderAction(FLUSH_CMD_FLUSH /* audio */,
671 FLUSH_CMD_SHUTDOWN /* video */));
672
Lajos Molnar1de1e252015-04-30 18:18:34 -0700673 mDeferredActions.push_back(new SetSurfaceAction(surface));
James Dong0d268a32012-08-31 12:18:27 -0700674
Wei Jiac6e58412015-07-10 18:04:55 -0700675 if (obj != NULL || mAudioDecoder != NULL) {
Wei Jiafef808d2014-10-31 17:57:05 -0700676 if (mStarted) {
Andy Hung73535852014-09-05 11:42:58 -0700677 // Issue a seek to refresh the video screen only if started otherwise
678 // the extractor may not yet be started and will assert.
679 // If the video decoder is not set (perhaps audio only in this case)
680 // do not perform a seek as it is not needed.
Ronghua Wua73d9e02014-10-08 15:13:29 -0700681 int64_t currentPositionUs = 0;
682 if (getCurrentPosition(&currentPositionUs) == OK) {
683 mDeferredActions.push_back(
Wei Jia29840802015-05-15 17:11:38 -0700684 new SeekAction(currentPositionUs));
Ronghua Wua73d9e02014-10-08 15:13:29 -0700685 }
Andy Hung73535852014-09-05 11:42:58 -0700686 }
Wei Jiaac428aa2014-09-02 19:01:34 -0700687
Andreas Huber57a339c2012-12-03 11:18:00 -0800688 // If there is a new surface texture, instantiate decoders
689 // again if possible.
690 mDeferredActions.push_back(
691 new SimpleAction(&NuPlayer::performScanSources));
692 }
693
Chong Zhangf8d71772014-11-26 15:08:34 -0800694 // After a flush without shutdown, decoder is paused.
695 // Don't resume it until source seek is done, otherwise it could
Chong Zhang7137ec72014-11-12 16:41:05 -0800696 // start pulling stale data too soon.
697 mDeferredActions.push_back(
Chong Zhangf8d71772014-11-26 15:08:34 -0800698 new ResumeDecoderAction(false /* needNotify */));
Chong Zhang7137ec72014-11-12 16:41:05 -0800699
Andreas Huber57a339c2012-12-03 11:18:00 -0800700 processDeferredActions();
Andreas Huberf9334412010-12-15 15:17:42 -0800701 break;
702 }
703
704 case kWhatSetAudioSink:
705 {
Steve Block3856b092011-10-20 11:56:00 +0100706 ALOGV("kWhatSetAudioSink");
Andreas Huberf9334412010-12-15 15:17:42 -0800707
708 sp<RefBase> obj;
709 CHECK(msg->findObject("sink", &obj));
710
711 mAudioSink = static_cast<MediaPlayerBase::AudioSink *>(obj.get());
712 break;
713 }
714
715 case kWhatStart:
716 {
Steve Block3856b092011-10-20 11:56:00 +0100717 ALOGV("kWhatStart");
Wei Jia94211742014-10-28 17:09:06 -0700718 if (mStarted) {
Chong Zhang8a048332015-05-06 15:16:28 -0700719 // do not resume yet if the source is still buffering
720 if (!mPausedForBuffering) {
721 onResume();
722 }
Wei Jia94211742014-10-28 17:09:06 -0700723 } else {
724 onStart();
Lajos Molnar09524832014-07-17 14:29:51 -0700725 }
Chong Zhangefbb6192015-01-30 17:13:27 -0800726 mPausedByClient = false;
Andreas Huberf9334412010-12-15 15:17:42 -0800727 break;
728 }
729
Lajos Molnar3a474aa2015-04-24 17:10:07 -0700730 case kWhatConfigPlayback:
Wei Jia98160162015-02-04 17:01:11 -0800731 {
Lajos Molnar3a474aa2015-04-24 17:10:07 -0700732 sp<AReplyToken> replyID;
733 CHECK(msg->senderAwaitsResponse(&replyID));
734 AudioPlaybackRate rate /* sanitized */;
735 readFromAMessage(msg, &rate);
736 status_t err = OK;
Wei Jia98160162015-02-04 17:01:11 -0800737 if (mRenderer != NULL) {
Wei Jiabf70feb2016-02-19 15:47:16 -0800738 // AudioSink allows only 1.f and 0.f for offload mode.
739 // For other speed, switch to non-offload mode.
740 if (mOffloadAudio && ((rate.mSpeed != 0.f && rate.mSpeed != 1.f)
741 || rate.mPitch != 1.f)) {
742 int64_t currentPositionUs;
743 if (getCurrentPosition(&currentPositionUs) != OK) {
744 currentPositionUs = mPreviousSeekTimeUs;
745 }
746
747 // Set mPlaybackSettings so that the new audio decoder can
748 // be created correctly.
749 mPlaybackSettings = rate;
750 if (!mPaused) {
751 mRenderer->pause();
752 }
Wei Jia5031b2f2016-02-25 11:19:31 -0800753 restartAudio(
Wei Jiabf70feb2016-02-19 15:47:16 -0800754 currentPositionUs, true /* forceNonOffload */,
755 true /* needsToCreateAudioDecoder */);
756 if (!mPaused) {
757 mRenderer->resume();
758 }
759 }
760
Lajos Molnar3a474aa2015-04-24 17:10:07 -0700761 err = mRenderer->setPlaybackSettings(rate);
762 }
763 if (err == OK) {
764 if (rate.mSpeed == 0.f) {
765 onPause();
Wei Jia351ce872016-02-10 13:21:59 -0800766 mPausedByClient = true;
Lajos Molnar3a474aa2015-04-24 17:10:07 -0700767 // save all other settings (using non-paused speed)
768 // so we can restore them on start
769 AudioPlaybackRate newRate = rate;
770 newRate.mSpeed = mPlaybackSettings.mSpeed;
771 mPlaybackSettings = newRate;
772 } else { /* rate.mSpeed != 0.f */
Lajos Molnar3a474aa2015-04-24 17:10:07 -0700773 mPlaybackSettings = rate;
Wei Jia8a092d32016-06-03 14:57:24 -0700774 if (mStarted) {
775 // do not resume yet if the source is still buffering
776 if (!mPausedForBuffering) {
777 onResume();
778 }
779 } else if (mPrepared) {
780 onStart();
781 }
782
783 mPausedByClient = false;
Lajos Molnar3a474aa2015-04-24 17:10:07 -0700784 }
Wei Jia98160162015-02-04 17:01:11 -0800785 }
Ronghua Wu8db88132015-04-22 13:51:35 -0700786
787 if (mVideoDecoder != NULL) {
Praveen Chavanbbaa1442016-04-08 13:33:49 -0700788 sp<AMessage> params = new AMessage();
789 params->setFloat("playback-speed", mPlaybackSettings.mSpeed);
790 mVideoDecoder->setParameters(params);
Ronghua Wu8db88132015-04-22 13:51:35 -0700791 }
792
Lajos Molnar3a474aa2015-04-24 17:10:07 -0700793 sp<AMessage> response = new AMessage;
794 response->setInt32("err", err);
795 response->postReply(replyID);
796 break;
797 }
798
799 case kWhatGetPlaybackSettings:
800 {
801 sp<AReplyToken> replyID;
802 CHECK(msg->senderAwaitsResponse(&replyID));
803 AudioPlaybackRate rate = mPlaybackSettings;
804 status_t err = OK;
805 if (mRenderer != NULL) {
806 err = mRenderer->getPlaybackSettings(&rate);
807 }
808 if (err == OK) {
809 // get playback settings used by renderer, as it may be
810 // slightly off due to audiosink not taking small changes.
811 mPlaybackSettings = rate;
812 if (mPaused) {
813 rate.mSpeed = 0.f;
814 }
815 }
816 sp<AMessage> response = new AMessage;
817 if (err == OK) {
818 writeToAMessage(response, rate);
819 }
820 response->setInt32("err", err);
821 response->postReply(replyID);
822 break;
823 }
824
825 case kWhatConfigSync:
826 {
827 sp<AReplyToken> replyID;
828 CHECK(msg->senderAwaitsResponse(&replyID));
829
830 ALOGV("kWhatConfigSync");
831 AVSyncSettings sync;
832 float videoFpsHint;
833 readFromAMessage(msg, &sync, &videoFpsHint);
834 status_t err = OK;
835 if (mRenderer != NULL) {
836 err = mRenderer->setSyncSettings(sync, videoFpsHint);
837 }
838 if (err == OK) {
839 mSyncSettings = sync;
840 mVideoFpsHint = videoFpsHint;
841 }
842 sp<AMessage> response = new AMessage;
843 response->setInt32("err", err);
844 response->postReply(replyID);
845 break;
846 }
847
848 case kWhatGetSyncSettings:
849 {
850 sp<AReplyToken> replyID;
851 CHECK(msg->senderAwaitsResponse(&replyID));
852 AVSyncSettings sync = mSyncSettings;
853 float videoFps = mVideoFpsHint;
854 status_t err = OK;
855 if (mRenderer != NULL) {
856 err = mRenderer->getSyncSettings(&sync, &videoFps);
857 if (err == OK) {
858 mSyncSettings = sync;
859 mVideoFpsHint = videoFps;
860 }
861 }
862 sp<AMessage> response = new AMessage;
863 if (err == OK) {
864 writeToAMessage(response, sync, videoFps);
865 }
866 response->setInt32("err", err);
867 response->postReply(replyID);
Wei Jia98160162015-02-04 17:01:11 -0800868 break;
869 }
870
Andreas Huberf9334412010-12-15 15:17:42 -0800871 case kWhatScanSources:
872 {
Andreas Huber1aef2112011-01-04 14:01:29 -0800873 int32_t generation;
874 CHECK(msg->findInt32("generation", &generation));
875 if (generation != mScanSourcesGeneration) {
876 // Drop obsolete msg.
877 break;
878 }
879
Andreas Huber5bc087c2010-12-23 10:27:40 -0800880 mScanSourcesPending = false;
881
Steve Block3856b092011-10-20 11:56:00 +0100882 ALOGV("scanning sources haveAudio=%d, haveVideo=%d",
Andreas Huber43c3e6c2011-01-05 12:17:08 -0800883 mAudioDecoder != NULL, mVideoDecoder != NULL);
884
Andreas Huberb7c8e912012-11-27 15:02:53 -0800885 bool mHadAnySourcesBefore =
886 (mAudioDecoder != NULL) || (mVideoDecoder != NULL);
Robert Shih7350b052015-10-01 15:50:14 -0700887 bool rescan = false;
Andreas Huberb7c8e912012-11-27 15:02:53 -0800888
Andy Hung282a7e32014-08-14 15:56:34 -0700889 // initialize video before audio because successful initialization of
890 // video may change deep buffer mode of audio.
Lajos Molnar1de1e252015-04-30 18:18:34 -0700891 if (mSurface != NULL) {
Robert Shih7350b052015-10-01 15:50:14 -0700892 if (instantiateDecoder(false, &mVideoDecoder) == -EWOULDBLOCK) {
893 rescan = true;
894 }
Haynes Mathew George5d246ef2012-07-09 10:36:57 -0700895 }
Andreas Huberf9334412010-12-15 15:17:42 -0800896
Ronghua Wua10fd232014-11-06 16:15:20 -0800897 // Don't try to re-open audio sink if there's an existing decoder.
898 if (mAudioSink != NULL && mAudioDecoder == NULL) {
Robert Shih7350b052015-10-01 15:50:14 -0700899 if (instantiateDecoder(true, &mAudioDecoder) == -EWOULDBLOCK) {
900 rescan = true;
901 }
Andreas Huberf9334412010-12-15 15:17:42 -0800902 }
903
Andreas Huberb7c8e912012-11-27 15:02:53 -0800904 if (!mHadAnySourcesBefore
905 && (mAudioDecoder != NULL || mVideoDecoder != NULL)) {
906 // This is the first time we've found anything playable.
907
Andreas Huber9575c962013-02-05 13:59:56 -0800908 if (mSourceFlags & Source::FLAG_DYNAMIC_DURATION) {
Andreas Huberb7c8e912012-11-27 15:02:53 -0800909 schedulePollDuration();
910 }
911 }
912
Andreas Hubereac68ba2011-09-27 12:12:25 -0700913 status_t err;
914 if ((err = mSource->feedMoreTSData()) != OK) {
Andreas Huber1aef2112011-01-04 14:01:29 -0800915 if (mAudioDecoder == NULL && mVideoDecoder == NULL) {
916 // We're not currently decoding anything (no audio or
917 // video tracks found) and we just ran out of input data.
Andreas Hubereac68ba2011-09-27 12:12:25 -0700918
919 if (err == ERROR_END_OF_STREAM) {
920 notifyListener(MEDIA_PLAYBACK_COMPLETE, 0, 0);
921 } else {
922 notifyListener(MEDIA_ERROR, MEDIA_ERROR_UNKNOWN, err);
923 }
Andreas Huber1aef2112011-01-04 14:01:29 -0800924 }
Andreas Huberf9334412010-12-15 15:17:42 -0800925 break;
926 }
927
Robert Shih7350b052015-10-01 15:50:14 -0700928 if (rescan) {
Andreas Huberf9334412010-12-15 15:17:42 -0800929 msg->post(100000ll);
Andreas Huber5bc087c2010-12-23 10:27:40 -0800930 mScanSourcesPending = true;
Andreas Huberf9334412010-12-15 15:17:42 -0800931 }
932 break;
933 }
934
935 case kWhatVideoNotify:
936 case kWhatAudioNotify:
937 {
938 bool audio = msg->what() == kWhatAudioNotify;
939
Wei Jia88703c32014-08-06 11:24:07 -0700940 int32_t currentDecoderGeneration =
941 (audio? mAudioDecoderGeneration : mVideoDecoderGeneration);
942 int32_t requesterGeneration = currentDecoderGeneration - 1;
943 CHECK(msg->findInt32("generation", &requesterGeneration));
944
945 if (requesterGeneration != currentDecoderGeneration) {
946 ALOGV("got message from old %s decoder, generation(%d:%d)",
947 audio ? "audio" : "video", requesterGeneration,
948 currentDecoderGeneration);
949 sp<AMessage> reply;
950 if (!(msg->findMessage("reply", &reply))) {
951 return;
952 }
953
954 reply->setInt32("err", INFO_DISCONTINUITY);
955 reply->post();
956 return;
957 }
958
Andreas Huberf9334412010-12-15 15:17:42 -0800959 int32_t what;
Lajos Molnar1cd13982014-01-17 15:12:51 -0800960 CHECK(msg->findInt32("what", &what));
Andreas Huberf9334412010-12-15 15:17:42 -0800961
Chong Zhang7137ec72014-11-12 16:41:05 -0800962 if (what == DecoderBase::kWhatInputDiscontinuity) {
963 int32_t formatChange;
964 CHECK(msg->findInt32("formatChange", &formatChange));
Andreas Huberf9334412010-12-15 15:17:42 -0800965
Chong Zhang7137ec72014-11-12 16:41:05 -0800966 ALOGV("%s discontinuity: formatChange %d",
967 audio ? "audio" : "video", formatChange);
968
969 if (formatChange) {
970 mDeferredActions.push_back(
971 new FlushDecoderAction(
972 audio ? FLUSH_CMD_SHUTDOWN : FLUSH_CMD_NONE,
973 audio ? FLUSH_CMD_NONE : FLUSH_CMD_SHUTDOWN));
Andreas Huberf9334412010-12-15 15:17:42 -0800974 }
Chong Zhang7137ec72014-11-12 16:41:05 -0800975
976 mDeferredActions.push_back(
977 new SimpleAction(
978 &NuPlayer::performScanSources));
979
980 processDeferredActions();
981 } else if (what == DecoderBase::kWhatEOS) {
Andreas Huberdc9bacd2011-09-26 10:53:29 -0700982 int32_t err;
Lajos Molnar1cd13982014-01-17 15:12:51 -0800983 CHECK(msg->findInt32("err", &err));
Andreas Huberdc9bacd2011-09-26 10:53:29 -0700984
985 if (err == ERROR_END_OF_STREAM) {
Steve Block3856b092011-10-20 11:56:00 +0100986 ALOGV("got %s decoder EOS", audio ? "audio" : "video");
Andreas Huberdc9bacd2011-09-26 10:53:29 -0700987 } else {
Steve Block3856b092011-10-20 11:56:00 +0100988 ALOGV("got %s decoder EOS w/ error %d",
Andreas Huberdc9bacd2011-09-26 10:53:29 -0700989 audio ? "audio" : "video",
990 err);
991 }
992
993 mRenderer->queueEOS(audio, err);
Chong Zhang7137ec72014-11-12 16:41:05 -0800994 } else if (what == DecoderBase::kWhatFlushCompleted) {
Steve Block3856b092011-10-20 11:56:00 +0100995 ALOGV("decoder %s flush completed", audio ? "audio" : "video");
Andreas Huberf9334412010-12-15 15:17:42 -0800996
Andy Hung8d121d42014-10-03 09:53:53 -0700997 handleFlushComplete(audio, true /* isDecoder */);
Andreas Huber3831a062010-12-21 10:22:33 -0800998 finishFlushIfPossible();
Chong Zhang7137ec72014-11-12 16:41:05 -0800999 } else if (what == DecoderBase::kWhatVideoSizeChanged) {
Lajos Molnar1cd13982014-01-17 15:12:51 -08001000 sp<AMessage> format;
1001 CHECK(msg->findMessage("format", &format));
1002
Wei Jiac6cfd702014-11-11 16:33:20 -08001003 sp<AMessage> inputFormat =
1004 mSource->getFormat(false /* audio */);
Andreas Huber3831a062010-12-21 10:22:33 -08001005
Bartosz Dolewski26936f72014-12-11 12:47:08 +01001006 setVideoScalingMode(mVideoScalingMode);
Wei Jiac6cfd702014-11-11 16:33:20 -08001007 updateVideoSize(inputFormat, format);
Chong Zhang7137ec72014-11-12 16:41:05 -08001008 } else if (what == DecoderBase::kWhatShutdownCompleted) {
Steve Block3856b092011-10-20 11:56:00 +01001009 ALOGV("%s shutdown completed", audio ? "audio" : "video");
Andreas Huber3831a062010-12-21 10:22:33 -08001010 if (audio) {
1011 mAudioDecoder.clear();
Wei Jia57568df2014-09-22 10:16:29 -07001012 ++mAudioDecoderGeneration;
Andreas Huber3831a062010-12-21 10:22:33 -08001013
1014 CHECK_EQ((int)mFlushingAudio, (int)SHUTTING_DOWN_DECODER);
1015 mFlushingAudio = SHUT_DOWN;
1016 } else {
1017 mVideoDecoder.clear();
Wei Jia57568df2014-09-22 10:16:29 -07001018 ++mVideoDecoderGeneration;
Andreas Huber3831a062010-12-21 10:22:33 -08001019
1020 CHECK_EQ((int)mFlushingVideo, (int)SHUTTING_DOWN_DECODER);
1021 mFlushingVideo = SHUT_DOWN;
1022 }
1023
1024 finishFlushIfPossible();
Chong Zhangf8d71772014-11-26 15:08:34 -08001025 } else if (what == DecoderBase::kWhatResumeCompleted) {
1026 finishResume();
Chong Zhang7137ec72014-11-12 16:41:05 -08001027 } else if (what == DecoderBase::kWhatError) {
Chong Zhangf4c0a942014-08-11 15:14:10 -07001028 status_t err;
Andy Hung2abde2c2014-09-30 14:40:32 -07001029 if (!msg->findInt32("err", &err) || err == OK) {
Chong Zhangf4c0a942014-08-11 15:14:10 -07001030 err = UNKNOWN_ERROR;
1031 }
Andy Hungcf31f1e2014-09-23 14:59:01 -07001032
Andy Hung2abde2c2014-09-30 14:40:32 -07001033 // Decoder errors can be due to Source (e.g. from streaming),
1034 // or from decoding corrupted bitstreams, or from other decoder
1035 // MediaCodec operations (e.g. from an ongoing reset or seek).
Andy Hung202bce12014-12-03 11:47:36 -08001036 // They may also be due to openAudioSink failure at
1037 // decoder start or after a format change.
Andy Hung2abde2c2014-09-30 14:40:32 -07001038 //
1039 // We try to gracefully shut down the affected decoder if possible,
1040 // rather than trying to force the shutdown with something
1041 // similar to performReset(). This method can lead to a hang
1042 // if MediaCodec functions block after an error, but they should
1043 // typically return INVALID_OPERATION instead of blocking.
1044
1045 FlushStatus *flushing = audio ? &mFlushingAudio : &mFlushingVideo;
1046 ALOGE("received error(%#x) from %s decoder, flushing(%d), now shutting down",
1047 err, audio ? "audio" : "video", *flushing);
1048
1049 switch (*flushing) {
1050 case NONE:
1051 mDeferredActions.push_back(
Wei Jiafef808d2014-10-31 17:57:05 -07001052 new FlushDecoderAction(
1053 audio ? FLUSH_CMD_SHUTDOWN : FLUSH_CMD_NONE,
1054 audio ? FLUSH_CMD_NONE : FLUSH_CMD_SHUTDOWN));
Andy Hung2abde2c2014-09-30 14:40:32 -07001055 processDeferredActions();
1056 break;
1057 case FLUSHING_DECODER:
1058 *flushing = FLUSHING_DECODER_SHUTDOWN; // initiate shutdown after flush.
1059 break; // Wait for flush to complete.
1060 case FLUSHING_DECODER_SHUTDOWN:
1061 break; // Wait for flush to complete.
1062 case SHUTTING_DOWN_DECODER:
1063 break; // Wait for shutdown to complete.
1064 case FLUSHED:
1065 // Widevine source reads must stop before releasing the video decoder.
1066 if (!audio && mSource != NULL && mSourceFlags & Source::FLAG_SECURE) {
1067 mSource->stop();
Robert Shih0c61a0d2015-07-06 15:09:10 -07001068 mSourceStarted = false;
Andy Hung2abde2c2014-09-30 14:40:32 -07001069 }
1070 getDecoder(audio)->initiateShutdown(); // In the middle of a seek.
1071 *flushing = SHUTTING_DOWN_DECODER; // Shut down.
1072 break;
1073 case SHUT_DOWN:
1074 finishFlushIfPossible(); // Should not occur.
1075 break; // Finish anyways.
Marco Nelissen9e2b7912014-08-18 16:13:03 -07001076 }
Andy Hung2abde2c2014-09-30 14:40:32 -07001077 notifyListener(MEDIA_ERROR, MEDIA_ERROR_UNKNOWN, err);
Lajos Molnar1cd13982014-01-17 15:12:51 -08001078 } else {
1079 ALOGV("Unhandled decoder notification %d '%c%c%c%c'.",
Andreas Hubera1f8ab02012-11-30 10:53:22 -08001080 what,
1081 what >> 24,
1082 (what >> 16) & 0xff,
1083 (what >> 8) & 0xff,
1084 what & 0xff);
Andreas Huberf9334412010-12-15 15:17:42 -08001085 }
1086
1087 break;
1088 }
1089
1090 case kWhatRendererNotify:
1091 {
Wei Jia57568df2014-09-22 10:16:29 -07001092 int32_t requesterGeneration = mRendererGeneration - 1;
1093 CHECK(msg->findInt32("generation", &requesterGeneration));
1094 if (requesterGeneration != mRendererGeneration) {
1095 ALOGV("got message from old renderer, generation(%d:%d)",
1096 requesterGeneration, mRendererGeneration);
1097 return;
1098 }
1099
Andreas Huberf9334412010-12-15 15:17:42 -08001100 int32_t what;
1101 CHECK(msg->findInt32("what", &what));
1102
1103 if (what == Renderer::kWhatEOS) {
1104 int32_t audio;
1105 CHECK(msg->findInt32("audio", &audio));
1106
Andreas Huberc92fd242011-08-16 13:48:44 -07001107 int32_t finalResult;
1108 CHECK(msg->findInt32("finalResult", &finalResult));
1109
Andreas Huberf9334412010-12-15 15:17:42 -08001110 if (audio) {
1111 mAudioEOS = true;
1112 } else {
1113 mVideoEOS = true;
1114 }
1115
Andreas Huberc92fd242011-08-16 13:48:44 -07001116 if (finalResult == ERROR_END_OF_STREAM) {
Steve Block3856b092011-10-20 11:56:00 +01001117 ALOGV("reached %s EOS", audio ? "audio" : "video");
Andreas Huberc92fd242011-08-16 13:48:44 -07001118 } else {
Steve Block29357bc2012-01-06 19:20:56 +00001119 ALOGE("%s track encountered an error (%d)",
Andreas Huberc92fd242011-08-16 13:48:44 -07001120 audio ? "audio" : "video", finalResult);
1121
1122 notifyListener(
1123 MEDIA_ERROR, MEDIA_ERROR_UNKNOWN, finalResult);
1124 }
Andreas Huberf9334412010-12-15 15:17:42 -08001125
1126 if ((mAudioEOS || mAudioDecoder == NULL)
1127 && (mVideoEOS || mVideoDecoder == NULL)) {
1128 notifyListener(MEDIA_PLAYBACK_COMPLETE, 0, 0);
1129 }
Andreas Huber3fe62152011-09-16 15:09:22 -07001130 } else if (what == Renderer::kWhatFlushComplete) {
Andreas Huberf9334412010-12-15 15:17:42 -08001131 int32_t audio;
1132 CHECK(msg->findInt32("audio", &audio));
1133
Wei Jia3261f0d2015-10-08 13:58:33 -07001134 if (audio) {
1135 mAudioEOS = false;
1136 } else {
1137 mVideoEOS = false;
1138 }
1139
Steve Block3856b092011-10-20 11:56:00 +01001140 ALOGV("renderer %s flush completed.", audio ? "audio" : "video");
Wei Jiada4252f2015-07-14 18:15:28 -07001141 if (audio && (mFlushingAudio == NONE || mFlushingAudio == FLUSHED
1142 || mFlushingAudio == SHUT_DOWN)) {
1143 // Flush has been handled by tear down.
1144 break;
1145 }
Andy Hung8d121d42014-10-03 09:53:53 -07001146 handleFlushComplete(audio, false /* isDecoder */);
1147 finishFlushIfPossible();
James Dongf57b4ea2012-07-20 13:38:36 -07001148 } else if (what == Renderer::kWhatVideoRenderingStart) {
1149 notifyListener(MEDIA_INFO, MEDIA_INFO_RENDERING_START, 0);
Lajos Molnarcbaffcf2013-08-14 18:30:38 -07001150 } else if (what == Renderer::kWhatMediaRenderingStart) {
1151 ALOGV("media rendering started");
1152 notifyListener(MEDIA_STARTED, 0, 0);
Ronghua Wufaeb0f22015-05-21 12:20:21 -07001153 } else if (what == Renderer::kWhatAudioTearDown) {
Ronghua Wu08529172014-10-02 16:55:52 -07001154 int32_t reason;
1155 CHECK(msg->findInt32("reason", &reason));
Ronghua Wufaeb0f22015-05-21 12:20:21 -07001156 ALOGV("Tear down audio with reason %d.", reason);
Wei Jia8456ddd2016-04-22 14:46:43 -07001157 if (reason == Renderer::kDueToTimeout && !(mPaused && mOffloadAudio)) {
1158 // TimeoutWhenPaused is only for offload mode.
1159 ALOGW("Receive a stale message for teardown.");
1160 break;
1161 }
Wei Jiada4252f2015-07-14 18:15:28 -07001162 int64_t positionUs;
Robert Shih1a5c8592015-08-04 18:07:44 -07001163 if (!msg->findInt64("positionUs", &positionUs)) {
1164 positionUs = mPreviousSeekTimeUs;
1165 }
Ronghua Wufaeb0f22015-05-21 12:20:21 -07001166
Wei Jia5031b2f2016-02-25 11:19:31 -08001167 restartAudio(
Wei Jiaa05f1e32016-03-25 16:31:22 -07001168 positionUs, reason == Renderer::kForceNonOffload /* forceNonOffload */,
1169 reason != Renderer::kDueToTimeout /* needsToCreateAudioDecoder */);
Andreas Huberf9334412010-12-15 15:17:42 -08001170 }
1171 break;
1172 }
1173
1174 case kWhatMoreDataQueued:
1175 {
1176 break;
1177 }
1178
Andreas Huber1aef2112011-01-04 14:01:29 -08001179 case kWhatReset:
1180 {
Steve Block3856b092011-10-20 11:56:00 +01001181 ALOGV("kWhatReset");
Andreas Huber1aef2112011-01-04 14:01:29 -08001182
Ronghua Wu64c2d172015-10-07 16:52:19 -07001183 mResetting = true;
1184
Andreas Hubera1f8ab02012-11-30 10:53:22 -08001185 mDeferredActions.push_back(
Wei Jiafef808d2014-10-31 17:57:05 -07001186 new FlushDecoderAction(
1187 FLUSH_CMD_SHUTDOWN /* audio */,
1188 FLUSH_CMD_SHUTDOWN /* video */));
Andreas Huberb7c8e912012-11-27 15:02:53 -08001189
Andreas Hubera1f8ab02012-11-30 10:53:22 -08001190 mDeferredActions.push_back(
1191 new SimpleAction(&NuPlayer::performReset));
Andreas Huberb58ce9f2011-11-28 16:27:35 -08001192
Andreas Hubera1f8ab02012-11-30 10:53:22 -08001193 processDeferredActions();
Andreas Huber1aef2112011-01-04 14:01:29 -08001194 break;
1195 }
1196
Andreas Huber43c3e6c2011-01-05 12:17:08 -08001197 case kWhatSeek:
1198 {
1199 int64_t seekTimeUs;
Wei Jiae427abf2014-09-22 15:21:11 -07001200 int32_t needNotify;
Andreas Huber43c3e6c2011-01-05 12:17:08 -08001201 CHECK(msg->findInt64("seekTimeUs", &seekTimeUs));
Wei Jiae427abf2014-09-22 15:21:11 -07001202 CHECK(msg->findInt32("needNotify", &needNotify));
Andreas Huber43c3e6c2011-01-05 12:17:08 -08001203
Wei Jiae427abf2014-09-22 15:21:11 -07001204 ALOGV("kWhatSeek seekTimeUs=%lld us, needNotify=%d",
Lajos Molnar6d339f12015-04-17 16:15:53 -07001205 (long long)seekTimeUs, needNotify);
Andreas Huber43c3e6c2011-01-05 12:17:08 -08001206
Wei Jia1061c9c2015-05-19 16:02:17 -07001207 if (!mStarted) {
1208 // Seek before the player is started. In order to preview video,
1209 // need to start the player and pause it. This branch is called
1210 // only once if needed. After the player is started, any seek
1211 // operation will go through normal path.
Robert Shih0c61a0d2015-07-06 15:09:10 -07001212 // Audio-only cases are handled separately.
Wei Jia1061c9c2015-05-19 16:02:17 -07001213 onStart(seekTimeUs);
Robert Shih0c61a0d2015-07-06 15:09:10 -07001214 if (mStarted) {
1215 onPause();
1216 mPausedByClient = true;
1217 }
Wei Jia1061c9c2015-05-19 16:02:17 -07001218 if (needNotify) {
1219 notifyDriverSeekComplete();
1220 }
1221 break;
1222 }
1223
Andreas Hubera1f8ab02012-11-30 10:53:22 -08001224 mDeferredActions.push_back(
Wei Jiafef808d2014-10-31 17:57:05 -07001225 new FlushDecoderAction(FLUSH_CMD_FLUSH /* audio */,
1226 FLUSH_CMD_FLUSH /* video */));
Andreas Huber43c3e6c2011-01-05 12:17:08 -08001227
Wei Jiae427abf2014-09-22 15:21:11 -07001228 mDeferredActions.push_back(
Wei Jia29840802015-05-15 17:11:38 -07001229 new SeekAction(seekTimeUs));
Andreas Huber43c3e6c2011-01-05 12:17:08 -08001230
Chong Zhangf8d71772014-11-26 15:08:34 -08001231 // After a flush without shutdown, decoder is paused.
1232 // Don't resume it until source seek is done, otherwise it could
Chong Zhang7137ec72014-11-12 16:41:05 -08001233 // start pulling stale data too soon.
1234 mDeferredActions.push_back(
Chong Zhangf8d71772014-11-26 15:08:34 -08001235 new ResumeDecoderAction(needNotify));
Chong Zhang7137ec72014-11-12 16:41:05 -08001236
Andreas Hubera1f8ab02012-11-30 10:53:22 -08001237 processDeferredActions();
Andreas Huber43c3e6c2011-01-05 12:17:08 -08001238 break;
1239 }
1240
Andreas Huberb4082222011-01-20 15:23:04 -08001241 case kWhatPause:
1242 {
Chong Zhangefbb6192015-01-30 17:13:27 -08001243 onPause();
1244 mPausedByClient = true;
Andreas Huberb4082222011-01-20 15:23:04 -08001245 break;
1246 }
1247
Andreas Huberb5f25f02013-02-05 10:14:26 -08001248 case kWhatSourceNotify:
1249 {
Andreas Huber9575c962013-02-05 13:59:56 -08001250 onSourceNotify(msg);
Andreas Huberb5f25f02013-02-05 10:14:26 -08001251 break;
1252 }
1253
Chong Zhanga7fa1d92014-06-11 14:49:23 -07001254 case kWhatClosedCaptionNotify:
1255 {
1256 onClosedCaptionNotify(msg);
1257 break;
1258 }
1259
Andreas Huberf9334412010-12-15 15:17:42 -08001260 default:
1261 TRESPASS();
1262 break;
1263 }
1264}
1265
Wei Jia94211742014-10-28 17:09:06 -07001266void NuPlayer::onResume() {
Ronghua Wu64c2d172015-10-07 16:52:19 -07001267 if (!mPaused || mResetting) {
1268 ALOGD_IF(mResetting, "resetting, onResume discarded");
Chong Zhangefbb6192015-01-30 17:13:27 -08001269 return;
1270 }
1271 mPaused = false;
Wei Jia94211742014-10-28 17:09:06 -07001272 if (mSource != NULL) {
1273 mSource->resume();
1274 } else {
1275 ALOGW("resume called when source is gone or not set");
1276 }
1277 // |mAudioDecoder| may have been released due to the pause timeout, so re-create it if
1278 // needed.
1279 if (audioDecoderStillNeeded() && mAudioDecoder == NULL) {
1280 instantiateDecoder(true /* audio */, &mAudioDecoder);
1281 }
1282 if (mRenderer != NULL) {
1283 mRenderer->resume();
1284 } else {
1285 ALOGW("resume called when renderer is gone or not set");
1286 }
1287}
1288
Lajos Molnarfcd3e942015-03-31 10:06:48 -07001289status_t NuPlayer::onInstantiateSecureDecoders() {
1290 status_t err;
1291 if (!(mSourceFlags & Source::FLAG_SECURE)) {
1292 return BAD_TYPE;
1293 }
1294
1295 if (mRenderer != NULL) {
1296 ALOGE("renderer should not be set when instantiating secure decoders");
1297 return UNKNOWN_ERROR;
1298 }
1299
1300 // TRICKY: We rely on mRenderer being null, so that decoder does not start requesting
1301 // data on instantiation.
Lajos Molnar1de1e252015-04-30 18:18:34 -07001302 if (mSurface != NULL) {
Lajos Molnarfcd3e942015-03-31 10:06:48 -07001303 err = instantiateDecoder(false, &mVideoDecoder);
1304 if (err != OK) {
1305 return err;
1306 }
1307 }
1308
1309 if (mAudioSink != NULL) {
1310 err = instantiateDecoder(true, &mAudioDecoder);
1311 if (err != OK) {
1312 return err;
1313 }
1314 }
1315 return OK;
1316}
1317
Wei Jia1061c9c2015-05-19 16:02:17 -07001318void NuPlayer::onStart(int64_t startPositionUs) {
Robert Shih0c61a0d2015-07-06 15:09:10 -07001319 if (!mSourceStarted) {
1320 mSourceStarted = true;
1321 mSource->start();
1322 }
1323 if (startPositionUs > 0) {
1324 performSeek(startPositionUs);
1325 if (mSource->getFormat(false /* audio */) == NULL) {
1326 return;
1327 }
1328 }
1329
Wei Jia94211742014-10-28 17:09:06 -07001330 mOffloadAudio = false;
1331 mAudioEOS = false;
1332 mVideoEOS = false;
Wei Jia94211742014-10-28 17:09:06 -07001333 mStarted = true;
Wei Jiafe8fe7d2016-06-08 10:29:25 -07001334 mPaused = false;
Wei Jia94211742014-10-28 17:09:06 -07001335
Wei Jia94211742014-10-28 17:09:06 -07001336 uint32_t flags = 0;
1337
1338 if (mSource->isRealTime()) {
1339 flags |= Renderer::FLAG_REAL_TIME;
1340 }
1341
1342 sp<MetaData> audioMeta = mSource->getFormatMeta(true /* audio */);
Andy Hung12a84132015-10-20 16:09:21 -07001343 ALOGV_IF(audioMeta == NULL, "no metadata for audio source"); // video only stream
Wei Jia94211742014-10-28 17:09:06 -07001344 audio_stream_type_t streamType = AUDIO_STREAM_MUSIC;
1345 if (mAudioSink != NULL) {
1346 streamType = mAudioSink->getAudioStreamType();
1347 }
1348
1349 sp<AMessage> videoFormat = mSource->getFormat(false /* audio */);
1350
1351 mOffloadAudio =
Wei Jiabf70feb2016-02-19 15:47:16 -08001352 canOffloadStream(audioMeta, (videoFormat != NULL), mSource->isStreaming(), streamType)
1353 && (mPlaybackSettings.mSpeed == 1.f && mPlaybackSettings.mPitch == 1.f);
Wei Jia94211742014-10-28 17:09:06 -07001354 if (mOffloadAudio) {
1355 flags |= Renderer::FLAG_OFFLOAD_AUDIO;
1356 }
1357
Lajos Molnar1d15ab52015-03-04 16:46:34 -08001358 sp<AMessage> notify = new AMessage(kWhatRendererNotify, this);
Wei Jia94211742014-10-28 17:09:06 -07001359 ++mRendererGeneration;
1360 notify->setInt32("generation", mRendererGeneration);
1361 mRenderer = new Renderer(mAudioSink, notify, flags);
Wei Jia94211742014-10-28 17:09:06 -07001362 mRendererLooper = new ALooper;
1363 mRendererLooper->setName("NuPlayerRenderer");
1364 mRendererLooper->start(false, false, ANDROID_PRIORITY_AUDIO);
1365 mRendererLooper->registerHandler(mRenderer);
Lajos Molnar3a474aa2015-04-24 17:10:07 -07001366
1367 status_t err = mRenderer->setPlaybackSettings(mPlaybackSettings);
1368 if (err != OK) {
1369 mSource->stop();
Robert Shih0c61a0d2015-07-06 15:09:10 -07001370 mSourceStarted = false;
Lajos Molnar3a474aa2015-04-24 17:10:07 -07001371 notifyListener(MEDIA_ERROR, MEDIA_ERROR_UNKNOWN, err);
1372 return;
Wei Jiac8206ff2015-03-04 13:59:37 -08001373 }
Wei Jia94211742014-10-28 17:09:06 -07001374
Ronghua Wuc8a70d32015-04-29 16:26:34 -07001375 float rate = getFrameRate();
1376 if (rate > 0) {
Wei Jia94211742014-10-28 17:09:06 -07001377 mRenderer->setVideoFrameRate(rate);
1378 }
1379
Wei Jiac6cfd702014-11-11 16:33:20 -08001380 if (mVideoDecoder != NULL) {
1381 mVideoDecoder->setRenderer(mRenderer);
1382 }
1383 if (mAudioDecoder != NULL) {
1384 mAudioDecoder->setRenderer(mRenderer);
1385 }
1386
Wei Jia94211742014-10-28 17:09:06 -07001387 postScanSources();
1388}
1389
Chong Zhangefbb6192015-01-30 17:13:27 -08001390void NuPlayer::onPause() {
1391 if (mPaused) {
1392 return;
1393 }
1394 mPaused = true;
1395 if (mSource != NULL) {
1396 mSource->pause();
1397 } else {
1398 ALOGW("pause called when source is gone or not set");
1399 }
1400 if (mRenderer != NULL) {
1401 mRenderer->pause();
1402 } else {
1403 ALOGW("pause called when renderer is gone or not set");
1404 }
1405}
1406
Ronghua Wud7988b12014-10-03 15:19:10 -07001407bool NuPlayer::audioDecoderStillNeeded() {
1408 // Audio decoder is no longer needed if it's in shut/shutting down status.
1409 return ((mFlushingAudio != SHUT_DOWN) && (mFlushingAudio != SHUTTING_DOWN_DECODER));
1410}
1411
Andy Hung8d121d42014-10-03 09:53:53 -07001412void NuPlayer::handleFlushComplete(bool audio, bool isDecoder) {
1413 // We wait for both the decoder flush and the renderer flush to complete
1414 // before entering either the FLUSHED or the SHUTTING_DOWN_DECODER state.
1415
1416 mFlushComplete[audio][isDecoder] = true;
1417 if (!mFlushComplete[audio][!isDecoder]) {
1418 return;
1419 }
1420
1421 FlushStatus *state = audio ? &mFlushingAudio : &mFlushingVideo;
1422 switch (*state) {
1423 case FLUSHING_DECODER:
1424 {
1425 *state = FLUSHED;
Andy Hung8d121d42014-10-03 09:53:53 -07001426 break;
1427 }
1428
1429 case FLUSHING_DECODER_SHUTDOWN:
1430 {
1431 *state = SHUTTING_DOWN_DECODER;
1432
1433 ALOGV("initiating %s decoder shutdown", audio ? "audio" : "video");
1434 if (!audio) {
Andy Hung8d121d42014-10-03 09:53:53 -07001435 // Widevine source reads must stop before releasing the video decoder.
1436 if (mSource != NULL && mSourceFlags & Source::FLAG_SECURE) {
1437 mSource->stop();
Robert Shih0c61a0d2015-07-06 15:09:10 -07001438 mSourceStarted = false;
Andy Hung8d121d42014-10-03 09:53:53 -07001439 }
1440 }
1441 getDecoder(audio)->initiateShutdown();
1442 break;
1443 }
1444
1445 default:
1446 // decoder flush completes only occur in a flushing state.
1447 LOG_ALWAYS_FATAL_IF(isDecoder, "decoder flush in invalid state %d", *state);
1448 break;
1449 }
1450}
1451
Andreas Huber3831a062010-12-21 10:22:33 -08001452void NuPlayer::finishFlushIfPossible() {
Wei Jia53904f32014-07-29 10:22:53 -07001453 if (mFlushingAudio != NONE && mFlushingAudio != FLUSHED
1454 && mFlushingAudio != SHUT_DOWN) {
Andreas Huber3831a062010-12-21 10:22:33 -08001455 return;
1456 }
1457
Wei Jia53904f32014-07-29 10:22:53 -07001458 if (mFlushingVideo != NONE && mFlushingVideo != FLUSHED
1459 && mFlushingVideo != SHUT_DOWN) {
Andreas Huber3831a062010-12-21 10:22:33 -08001460 return;
1461 }
1462
Steve Block3856b092011-10-20 11:56:00 +01001463 ALOGV("both audio and video are flushed now.");
Andreas Huber3831a062010-12-21 10:22:33 -08001464
Andreas Huber3831a062010-12-21 10:22:33 -08001465 mFlushingAudio = NONE;
1466 mFlushingVideo = NONE;
Andreas Huber3831a062010-12-21 10:22:33 -08001467
Andy Hung8d121d42014-10-03 09:53:53 -07001468 clearFlushComplete();
1469
Andreas Hubera1f8ab02012-11-30 10:53:22 -08001470 processDeferredActions();
Andreas Huber1aef2112011-01-04 14:01:29 -08001471}
1472
1473void NuPlayer::postScanSources() {
1474 if (mScanSourcesPending) {
1475 return;
1476 }
1477
Lajos Molnar1d15ab52015-03-04 16:46:34 -08001478 sp<AMessage> msg = new AMessage(kWhatScanSources, this);
Andreas Huber1aef2112011-01-04 14:01:29 -08001479 msg->setInt32("generation", mScanSourcesGeneration);
1480 msg->post();
1481
1482 mScanSourcesPending = true;
1483}
1484
Wei Jia41cd4632016-05-13 10:53:30 -07001485void NuPlayer::tryOpenAudioSinkForOffload(
1486 const sp<AMessage> &format, const sp<MetaData> &audioMeta, bool hasVideo) {
Andy Hung202bce12014-12-03 11:47:36 -08001487 // Note: This is called early in NuPlayer to determine whether offloading
1488 // is possible; otherwise the decoders call the renderer openAudioSink directly.
Andy Hung282a7e32014-08-14 15:56:34 -07001489
Andy Hung202bce12014-12-03 11:47:36 -08001490 status_t err = mRenderer->openAudioSink(
1491 format, true /* offloadOnly */, hasVideo, AUDIO_OUTPUT_FLAG_NONE, &mOffloadAudio);
1492 if (err != OK) {
1493 // Any failure we turn off mOffloadAudio.
1494 mOffloadAudio = false;
1495 } else if (mOffloadAudio) {
Chong Zhang3b9eb1f2014-10-15 17:05:08 -07001496 sendMetaDataToHal(mAudioSink, audioMeta);
Andy Hung282a7e32014-08-14 15:56:34 -07001497 }
1498}
1499
1500void NuPlayer::closeAudioSink() {
Chong Zhang3b9eb1f2014-10-15 17:05:08 -07001501 mRenderer->closeAudioSink();
Andy Hung282a7e32014-08-14 15:56:34 -07001502}
1503
Wei Jia5031b2f2016-02-25 11:19:31 -08001504void NuPlayer::restartAudio(
Wei Jiabf70feb2016-02-19 15:47:16 -08001505 int64_t currentPositionUs, bool forceNonOffload, bool needsToCreateAudioDecoder) {
Wei Jiaa05f1e32016-03-25 16:31:22 -07001506 if (mAudioDecoder != NULL) {
1507 mAudioDecoder->pause();
1508 mAudioDecoder.clear();
1509 ++mAudioDecoderGeneration;
1510 }
Wei Jiabf70feb2016-02-19 15:47:16 -08001511 if (mFlushingAudio == FLUSHING_DECODER) {
1512 mFlushComplete[1 /* audio */][1 /* isDecoder */] = true;
1513 mFlushingAudio = FLUSHED;
1514 finishFlushIfPossible();
1515 } else if (mFlushingAudio == FLUSHING_DECODER_SHUTDOWN
1516 || mFlushingAudio == SHUTTING_DOWN_DECODER) {
1517 mFlushComplete[1 /* audio */][1 /* isDecoder */] = true;
1518 mFlushingAudio = SHUT_DOWN;
1519 finishFlushIfPossible();
1520 needsToCreateAudioDecoder = false;
1521 }
1522 if (mRenderer == NULL) {
1523 return;
1524 }
1525 closeAudioSink();
1526 mRenderer->flush(true /* audio */, false /* notifyComplete */);
1527 if (mVideoDecoder != NULL) {
1528 mRenderer->flush(false /* audio */, false /* notifyComplete */);
1529 }
1530
1531 performSeek(currentPositionUs);
1532
1533 if (forceNonOffload) {
1534 mRenderer->signalDisableOffloadAudio();
1535 mOffloadAudio = false;
1536 }
1537 if (needsToCreateAudioDecoder) {
Wei Jiaa05f1e32016-03-25 16:31:22 -07001538 instantiateDecoder(true /* audio */, &mAudioDecoder, !forceNonOffload);
Wei Jiabf70feb2016-02-19 15:47:16 -08001539 }
1540}
1541
Wei Jia41cd4632016-05-13 10:53:30 -07001542void NuPlayer::determineAudioModeChange(const sp<AMessage> &audioFormat) {
Wei Jiae4d18c72015-07-13 17:58:11 -07001543 if (mSource == NULL || mAudioSink == NULL) {
1544 return;
1545 }
1546
1547 if (mRenderer == NULL) {
1548 ALOGW("No renderer can be used to determine audio mode. Use non-offload for safety.");
1549 mOffloadAudio = false;
1550 return;
1551 }
1552
1553 sp<MetaData> audioMeta = mSource->getFormatMeta(true /* audio */);
1554 sp<AMessage> videoFormat = mSource->getFormat(false /* audio */);
1555 audio_stream_type_t streamType = mAudioSink->getAudioStreamType();
1556 const bool hasVideo = (videoFormat != NULL);
1557 const bool canOffload = canOffloadStream(
Wei Jiabf70feb2016-02-19 15:47:16 -08001558 audioMeta, hasVideo, mSource->isStreaming(), streamType)
1559 && (mPlaybackSettings.mSpeed == 1.f && mPlaybackSettings.mPitch == 1.f);
Wei Jiae4d18c72015-07-13 17:58:11 -07001560 if (canOffload) {
1561 if (!mOffloadAudio) {
1562 mRenderer->signalEnableOffloadAudio();
1563 }
1564 // open audio sink early under offload mode.
Wei Jia41cd4632016-05-13 10:53:30 -07001565 tryOpenAudioSinkForOffload(audioFormat, audioMeta, hasVideo);
Wei Jiae4d18c72015-07-13 17:58:11 -07001566 } else {
Robert Shihe1d70192015-07-23 17:54:13 -07001567 if (mOffloadAudio) {
1568 mRenderer->signalDisableOffloadAudio();
1569 mOffloadAudio = false;
1570 }
Wei Jiae4d18c72015-07-13 17:58:11 -07001571 }
1572}
1573
Wei Jiaa05f1e32016-03-25 16:31:22 -07001574status_t NuPlayer::instantiateDecoder(
1575 bool audio, sp<DecoderBase> *decoder, bool checkAudioModeChange) {
Wei Jia566da802015-08-27 13:59:30 -07001576 // The audio decoder could be cleared by tear down. If still in shut down
1577 // process, no need to create a new audio decoder.
1578 if (*decoder != NULL || (audio && mFlushingAudio == SHUT_DOWN)) {
Andreas Huberf9334412010-12-15 15:17:42 -08001579 return OK;
1580 }
1581
Andreas Huber84066782011-08-16 09:34:26 -07001582 sp<AMessage> format = mSource->getFormat(audio);
Andreas Huberf9334412010-12-15 15:17:42 -08001583
Andreas Huber84066782011-08-16 09:34:26 -07001584 if (format == NULL) {
Robert Shih7350b052015-10-01 15:50:14 -07001585 return UNKNOWN_ERROR;
1586 } else {
1587 status_t err;
1588 if (format->findInt32("err", &err) && err) {
1589 return err;
1590 }
Andreas Huberf9334412010-12-15 15:17:42 -08001591 }
1592
Ronghua Wu8db88132015-04-22 13:51:35 -07001593 format->setInt32("priority", 0 /* realtime */);
1594
Andreas Huber3fe62152011-09-16 15:09:22 -07001595 if (!audio) {
Andreas Huber84066782011-08-16 09:34:26 -07001596 AString mime;
1597 CHECK(format->findString("mime", &mime));
Chong Zhanga7fa1d92014-06-11 14:49:23 -07001598
Lajos Molnar1d15ab52015-03-04 16:46:34 -08001599 sp<AMessage> ccNotify = new AMessage(kWhatClosedCaptionNotify, this);
Chong Zhang341ab6e2015-02-04 13:37:18 -08001600 if (mCCDecoder == NULL) {
1601 mCCDecoder = new CCDecoder(ccNotify);
1602 }
Lajos Molnar09524832014-07-17 14:29:51 -07001603
1604 if (mSourceFlags & Source::FLAG_SECURE) {
1605 format->setInt32("secure", true);
1606 }
Chong Zhang17134602015-01-07 16:14:34 -08001607
1608 if (mSourceFlags & Source::FLAG_PROTECTED) {
1609 format->setInt32("protected", true);
1610 }
Ronghua Wu8db88132015-04-22 13:51:35 -07001611
Ronghua Wuc8a70d32015-04-29 16:26:34 -07001612 float rate = getFrameRate();
1613 if (rate > 0) {
Lajos Molnar3a474aa2015-04-24 17:10:07 -07001614 format->setFloat("operating-rate", rate * mPlaybackSettings.mSpeed);
Ronghua Wu8db88132015-04-22 13:51:35 -07001615 }
Andreas Huber3fe62152011-09-16 15:09:22 -07001616 }
1617
Wei Jiabc2fb722014-07-08 16:37:57 -07001618 if (audio) {
Lajos Molnar1d15ab52015-03-04 16:46:34 -08001619 sp<AMessage> notify = new AMessage(kWhatAudioNotify, this);
Wei Jia88703c32014-08-06 11:24:07 -07001620 ++mAudioDecoderGeneration;
1621 notify->setInt32("generation", mAudioDecoderGeneration);
1622
Wei Jiaa05f1e32016-03-25 16:31:22 -07001623 if (checkAudioModeChange) {
Wei Jia41cd4632016-05-13 10:53:30 -07001624 determineAudioModeChange(format);
Wei Jiaa05f1e32016-03-25 16:31:22 -07001625 }
Wei Jiabc2fb722014-07-08 16:37:57 -07001626 if (mOffloadAudio) {
Wei Jia14532f22015-12-29 11:28:15 -08001627 mSource->setOffloadAudio(true /* offload */);
1628
Haynes Mathew George8b635332015-03-30 17:59:47 -07001629 const bool hasVideo = (mSource->getFormat(false /*audio */) != NULL);
1630 format->setInt32("has-video", hasVideo);
Wei Jiac6cfd702014-11-11 16:33:20 -08001631 *decoder = new DecoderPassThrough(notify, mSource, mRenderer);
Wei Jiabc2fb722014-07-08 16:37:57 -07001632 } else {
Wei Jia14532f22015-12-29 11:28:15 -08001633 mSource->setOffloadAudio(false /* offload */);
1634
Ronghua Wu68845c12015-07-21 09:50:48 -07001635 *decoder = new Decoder(notify, mSource, mPID, mRenderer);
Wei Jiabc2fb722014-07-08 16:37:57 -07001636 }
1637 } else {
Lajos Molnar1d15ab52015-03-04 16:46:34 -08001638 sp<AMessage> notify = new AMessage(kWhatVideoNotify, this);
Wei Jia88703c32014-08-06 11:24:07 -07001639 ++mVideoDecoderGeneration;
1640 notify->setInt32("generation", mVideoDecoderGeneration);
1641
Chong Zhang7137ec72014-11-12 16:41:05 -08001642 *decoder = new Decoder(
Ronghua Wu68845c12015-07-21 09:50:48 -07001643 notify, mSource, mPID, mRenderer, mSurface, mCCDecoder);
Lajos Molnard9fd6312014-11-06 11:00:00 -08001644
1645 // enable FRC if high-quality AV sync is requested, even if not
Lajos Molnar1de1e252015-04-30 18:18:34 -07001646 // directly queuing to display, as this will even improve textureview
Lajos Molnard9fd6312014-11-06 11:00:00 -08001647 // playback.
1648 {
1649 char value[PROPERTY_VALUE_MAX];
1650 if (property_get("persist.sys.media.avsync", value, NULL) &&
1651 (!strcmp("1", value) || !strcasecmp("true", value))) {
1652 format->setInt32("auto-frc", 1);
1653 }
1654 }
Wei Jiabc2fb722014-07-08 16:37:57 -07001655 }
Lajos Molnar1cd13982014-01-17 15:12:51 -08001656 (*decoder)->init();
Andreas Huber84066782011-08-16 09:34:26 -07001657 (*decoder)->configure(format);
Andreas Huberf9334412010-12-15 15:17:42 -08001658
Lajos Molnar09524832014-07-17 14:29:51 -07001659 // allocate buffers to decrypt widevine source buffers
1660 if (!audio && (mSourceFlags & Source::FLAG_SECURE)) {
Wonsik Kim7e34bf52016-08-23 00:09:18 +09001661 Vector<sp<MediaCodecBuffer> > inputBufs;
Lajos Molnar09524832014-07-17 14:29:51 -07001662 CHECK_EQ((*decoder)->getInputBuffers(&inputBufs), (status_t)OK);
1663
1664 Vector<MediaBuffer *> mediaBufs;
1665 for (size_t i = 0; i < inputBufs.size(); i++) {
Wonsik Kim7e34bf52016-08-23 00:09:18 +09001666 const sp<MediaCodecBuffer> &buffer = inputBufs[i];
Lajos Molnar09524832014-07-17 14:29:51 -07001667 MediaBuffer *mbuf = new MediaBuffer(buffer->data(), buffer->size());
1668 mediaBufs.push(mbuf);
1669 }
1670
1671 status_t err = mSource->setBuffers(audio, mediaBufs);
1672 if (err != OK) {
1673 for (size_t i = 0; i < mediaBufs.size(); ++i) {
1674 mediaBufs[i]->release();
1675 }
1676 mediaBufs.clear();
1677 ALOGE("Secure source didn't support secure mediaBufs.");
1678 return err;
1679 }
1680 }
Praveen Chavanbbaa1442016-04-08 13:33:49 -07001681
1682 if (!audio) {
1683 sp<AMessage> params = new AMessage();
1684 float rate = getFrameRate();
1685 if (rate > 0) {
1686 params->setFloat("frame-rate-total", rate);
1687 }
1688
1689 sp<MetaData> fileMeta = getFileMeta();
1690 if (fileMeta != NULL) {
1691 int32_t videoTemporalLayerCount;
1692 if (fileMeta->findInt32(kKeyTemporalLayerCount, &videoTemporalLayerCount)
1693 && videoTemporalLayerCount > 0) {
1694 params->setInt32("temporal-layer-count", videoTemporalLayerCount);
1695 }
1696 }
1697
1698 if (params->countEntries() > 0) {
1699 (*decoder)->setParameters(params);
1700 }
1701 }
Andreas Huberf9334412010-12-15 15:17:42 -08001702 return OK;
1703}
1704
Chong Zhangced1c2f2014-08-08 15:22:35 -07001705void NuPlayer::updateVideoSize(
1706 const sp<AMessage> &inputFormat,
1707 const sp<AMessage> &outputFormat) {
1708 if (inputFormat == NULL) {
1709 ALOGW("Unknown video size, reporting 0x0!");
1710 notifyListener(MEDIA_SET_VIDEO_SIZE, 0, 0);
1711 return;
1712 }
1713
1714 int32_t displayWidth, displayHeight;
Chong Zhangced1c2f2014-08-08 15:22:35 -07001715 if (outputFormat != NULL) {
1716 int32_t width, height;
1717 CHECK(outputFormat->findInt32("width", &width));
1718 CHECK(outputFormat->findInt32("height", &height));
1719
1720 int32_t cropLeft, cropTop, cropRight, cropBottom;
1721 CHECK(outputFormat->findRect(
1722 "crop",
1723 &cropLeft, &cropTop, &cropRight, &cropBottom));
1724
1725 displayWidth = cropRight - cropLeft + 1;
1726 displayHeight = cropBottom - cropTop + 1;
1727
1728 ALOGV("Video output format changed to %d x %d "
1729 "(crop: %d x %d @ (%d, %d))",
1730 width, height,
1731 displayWidth,
1732 displayHeight,
1733 cropLeft, cropTop);
1734 } else {
1735 CHECK(inputFormat->findInt32("width", &displayWidth));
1736 CHECK(inputFormat->findInt32("height", &displayHeight));
1737
1738 ALOGV("Video input format %d x %d", displayWidth, displayHeight);
1739 }
1740
1741 // Take into account sample aspect ratio if necessary:
1742 int32_t sarWidth, sarHeight;
1743 if (inputFormat->findInt32("sar-width", &sarWidth)
1744 && inputFormat->findInt32("sar-height", &sarHeight)) {
1745 ALOGV("Sample aspect ratio %d : %d", sarWidth, sarHeight);
1746
1747 displayWidth = (displayWidth * sarWidth) / sarHeight;
1748
1749 ALOGV("display dimensions %d x %d", displayWidth, displayHeight);
1750 }
1751
1752 int32_t rotationDegrees;
1753 if (!inputFormat->findInt32("rotation-degrees", &rotationDegrees)) {
1754 rotationDegrees = 0;
1755 }
1756
1757 if (rotationDegrees == 90 || rotationDegrees == 270) {
1758 int32_t tmp = displayWidth;
1759 displayWidth = displayHeight;
1760 displayHeight = tmp;
1761 }
1762
1763 notifyListener(
1764 MEDIA_SET_VIDEO_SIZE,
1765 displayWidth,
1766 displayHeight);
1767}
1768
Chong Zhangdcb89b32013-08-06 09:44:47 -07001769void NuPlayer::notifyListener(int msg, int ext1, int ext2, const Parcel *in) {
Andreas Huber43c3e6c2011-01-05 12:17:08 -08001770 if (mDriver == NULL) {
Andreas Huberf9334412010-12-15 15:17:42 -08001771 return;
1772 }
1773
Andreas Huber43c3e6c2011-01-05 12:17:08 -08001774 sp<NuPlayerDriver> driver = mDriver.promote();
Andreas Huberf9334412010-12-15 15:17:42 -08001775
Andreas Huber43c3e6c2011-01-05 12:17:08 -08001776 if (driver == NULL) {
Andreas Huberf9334412010-12-15 15:17:42 -08001777 return;
1778 }
1779
Chong Zhangdcb89b32013-08-06 09:44:47 -07001780 driver->notifyListener(msg, ext1, ext2, in);
Andreas Huberf9334412010-12-15 15:17:42 -08001781}
1782
Chong Zhang7137ec72014-11-12 16:41:05 -08001783void NuPlayer::flushDecoder(bool audio, bool needShutdown) {
Andreas Huber14f76722013-01-15 09:04:18 -08001784 ALOGV("[%s] flushDecoder needShutdown=%d",
1785 audio ? "audio" : "video", needShutdown);
1786
Chong Zhang7137ec72014-11-12 16:41:05 -08001787 const sp<DecoderBase> &decoder = getDecoder(audio);
Lajos Molnar87603c02014-08-20 19:25:30 -07001788 if (decoder == NULL) {
Steve Blockdf64d152012-01-04 20:05:49 +00001789 ALOGI("flushDecoder %s without decoder present",
Andreas Huber6e3d3112011-11-28 12:36:11 -08001790 audio ? "audio" : "video");
Lajos Molnar87603c02014-08-20 19:25:30 -07001791 return;
Andreas Huber6e3d3112011-11-28 12:36:11 -08001792 }
1793
Andreas Huber1aef2112011-01-04 14:01:29 -08001794 // Make sure we don't continue to scan sources until we finish flushing.
1795 ++mScanSourcesGeneration;
Chong Zhang3b032b32015-04-17 15:49:06 -07001796 if (mScanSourcesPending) {
Toshikazu Saitocbcbb792015-09-15 14:53:01 +09001797 if (!needShutdown) {
1798 mDeferredActions.push_back(
1799 new SimpleAction(&NuPlayer::performScanSources));
1800 }
Chong Zhang3b032b32015-04-17 15:49:06 -07001801 mScanSourcesPending = false;
1802 }
Andreas Huber1aef2112011-01-04 14:01:29 -08001803
Chong Zhang7137ec72014-11-12 16:41:05 -08001804 decoder->signalFlush();
Andreas Huber1aef2112011-01-04 14:01:29 -08001805
1806 FlushStatus newStatus =
1807 needShutdown ? FLUSHING_DECODER_SHUTDOWN : FLUSHING_DECODER;
1808
Lajos Molnarfcd3e942015-03-31 10:06:48 -07001809 mFlushComplete[audio][false /* isDecoder */] = (mRenderer == NULL);
Andy Hung8d121d42014-10-03 09:53:53 -07001810 mFlushComplete[audio][true /* isDecoder */] = false;
Andreas Huber1aef2112011-01-04 14:01:29 -08001811 if (audio) {
Wei Jia53904f32014-07-29 10:22:53 -07001812 ALOGE_IF(mFlushingAudio != NONE,
1813 "audio flushDecoder() is called in state %d", mFlushingAudio);
Andreas Huber1aef2112011-01-04 14:01:29 -08001814 mFlushingAudio = newStatus;
Andreas Huber1aef2112011-01-04 14:01:29 -08001815 } else {
Wei Jia53904f32014-07-29 10:22:53 -07001816 ALOGE_IF(mFlushingVideo != NONE,
1817 "video flushDecoder() is called in state %d", mFlushingVideo);
Andreas Huber1aef2112011-01-04 14:01:29 -08001818 mFlushingVideo = newStatus;
Andreas Huber1aef2112011-01-04 14:01:29 -08001819 }
1820}
1821
Chong Zhangced1c2f2014-08-08 15:22:35 -07001822void NuPlayer::queueDecoderShutdown(
1823 bool audio, bool video, const sp<AMessage> &reply) {
1824 ALOGI("queueDecoderShutdown audio=%d, video=%d", audio, video);
Andreas Huber84066782011-08-16 09:34:26 -07001825
Chong Zhangced1c2f2014-08-08 15:22:35 -07001826 mDeferredActions.push_back(
Wei Jiafef808d2014-10-31 17:57:05 -07001827 new FlushDecoderAction(
1828 audio ? FLUSH_CMD_SHUTDOWN : FLUSH_CMD_NONE,
1829 video ? FLUSH_CMD_SHUTDOWN : FLUSH_CMD_NONE));
Andreas Huber84066782011-08-16 09:34:26 -07001830
Chong Zhangced1c2f2014-08-08 15:22:35 -07001831 mDeferredActions.push_back(
1832 new SimpleAction(&NuPlayer::performScanSources));
Andreas Huber84066782011-08-16 09:34:26 -07001833
Chong Zhangced1c2f2014-08-08 15:22:35 -07001834 mDeferredActions.push_back(new PostMessageAction(reply));
1835
1836 processDeferredActions();
Andreas Huber84066782011-08-16 09:34:26 -07001837}
1838
James Dong0d268a32012-08-31 12:18:27 -07001839status_t NuPlayer::setVideoScalingMode(int32_t mode) {
1840 mVideoScalingMode = mode;
Lajos Molnar1de1e252015-04-30 18:18:34 -07001841 if (mSurface != NULL) {
1842 status_t ret = native_window_set_scaling_mode(mSurface.get(), mVideoScalingMode);
James Dong0d268a32012-08-31 12:18:27 -07001843 if (ret != OK) {
1844 ALOGE("Failed to set scaling mode (%d): %s",
1845 -ret, strerror(-ret));
1846 return ret;
1847 }
1848 }
1849 return OK;
1850}
1851
Chong Zhangdcb89b32013-08-06 09:44:47 -07001852status_t NuPlayer::getTrackInfo(Parcel* reply) const {
Lajos Molnar1d15ab52015-03-04 16:46:34 -08001853 sp<AMessage> msg = new AMessage(kWhatGetTrackInfo, this);
Chong Zhangdcb89b32013-08-06 09:44:47 -07001854 msg->setPointer("reply", reply);
1855
1856 sp<AMessage> response;
1857 status_t err = msg->postAndAwaitResponse(&response);
1858 return err;
1859}
1860
Robert Shih7c4f0d72014-07-09 18:53:31 -07001861status_t NuPlayer::getSelectedTrack(int32_t type, Parcel* reply) const {
Lajos Molnar1d15ab52015-03-04 16:46:34 -08001862 sp<AMessage> msg = new AMessage(kWhatGetSelectedTrack, this);
Robert Shih7c4f0d72014-07-09 18:53:31 -07001863 msg->setPointer("reply", reply);
1864 msg->setInt32("type", type);
1865
1866 sp<AMessage> response;
1867 status_t err = msg->postAndAwaitResponse(&response);
1868 if (err == OK && response != NULL) {
1869 CHECK(response->findInt32("err", &err));
1870 }
1871 return err;
1872}
1873
Robert Shih6ffb1fd2014-10-29 16:24:32 -07001874status_t NuPlayer::selectTrack(size_t trackIndex, bool select, int64_t timeUs) {
Lajos Molnar1d15ab52015-03-04 16:46:34 -08001875 sp<AMessage> msg = new AMessage(kWhatSelectTrack, this);
Chong Zhangdcb89b32013-08-06 09:44:47 -07001876 msg->setSize("trackIndex", trackIndex);
1877 msg->setInt32("select", select);
Robert Shih6ffb1fd2014-10-29 16:24:32 -07001878 msg->setInt64("timeUs", timeUs);
Chong Zhangdcb89b32013-08-06 09:44:47 -07001879
1880 sp<AMessage> response;
1881 status_t err = msg->postAndAwaitResponse(&response);
1882
Chong Zhang404fced2014-06-11 14:45:31 -07001883 if (err != OK) {
1884 return err;
1885 }
1886
1887 if (!response->findInt32("err", &err)) {
1888 err = OK;
1889 }
1890
Chong Zhangdcb89b32013-08-06 09:44:47 -07001891 return err;
1892}
1893
Ronghua Wua73d9e02014-10-08 15:13:29 -07001894status_t NuPlayer::getCurrentPosition(int64_t *mediaUs) {
1895 sp<Renderer> renderer = mRenderer;
1896 if (renderer == NULL) {
1897 return NO_INIT;
1898 }
1899
1900 return renderer->getCurrentPosition(mediaUs);
1901}
1902
Praveen Chavane1e5d7a2015-05-19 19:09:48 -07001903void NuPlayer::getStats(Vector<sp<AMessage> > *mTrackStats) {
1904 CHECK(mTrackStats != NULL);
1905
1906 mTrackStats->clear();
1907 if (mVideoDecoder != NULL) {
1908 mTrackStats->push_back(mVideoDecoder->getStats());
1909 }
1910 if (mAudioDecoder != NULL) {
1911 mTrackStats->push_back(mAudioDecoder->getStats());
Chong Zhang7137ec72014-11-12 16:41:05 -08001912 }
Ronghua Wua73d9e02014-10-08 15:13:29 -07001913}
1914
Marco Nelissenf0b72b52014-09-16 15:43:44 -07001915sp<MetaData> NuPlayer::getFileMeta() {
1916 return mSource->getFileFormatMeta();
1917}
1918
Ronghua Wuc8a70d32015-04-29 16:26:34 -07001919float NuPlayer::getFrameRate() {
1920 sp<MetaData> meta = mSource->getFormatMeta(false /* audio */);
1921 if (meta == NULL) {
1922 return 0;
1923 }
1924 int32_t rate;
1925 if (!meta->findInt32(kKeyFrameRate, &rate)) {
1926 // fall back to try file meta
1927 sp<MetaData> fileMeta = getFileMeta();
1928 if (fileMeta == NULL) {
1929 ALOGW("source has video meta but not file meta");
1930 return -1;
1931 }
1932 int32_t fileMetaRate;
1933 if (!fileMeta->findInt32(kKeyFrameRate, &fileMetaRate)) {
1934 return -1;
1935 }
1936 return fileMetaRate;
1937 }
1938 return rate;
1939}
1940
Andreas Huberb7c8e912012-11-27 15:02:53 -08001941void NuPlayer::schedulePollDuration() {
Lajos Molnar1d15ab52015-03-04 16:46:34 -08001942 sp<AMessage> msg = new AMessage(kWhatPollDuration, this);
Andreas Huberb7c8e912012-11-27 15:02:53 -08001943 msg->setInt32("generation", mPollDurationGeneration);
1944 msg->post();
1945}
1946
1947void NuPlayer::cancelPollDuration() {
1948 ++mPollDurationGeneration;
1949}
1950
Andreas Hubera1f8ab02012-11-30 10:53:22 -08001951void NuPlayer::processDeferredActions() {
1952 while (!mDeferredActions.empty()) {
1953 // We won't execute any deferred actions until we're no longer in
1954 // an intermediate state, i.e. one more more decoders are currently
1955 // flushing or shutting down.
1956
Andreas Hubera1f8ab02012-11-30 10:53:22 -08001957 if (mFlushingAudio != NONE || mFlushingVideo != NONE) {
1958 // We're currently flushing, postpone the reset until that's
1959 // completed.
1960
1961 ALOGV("postponing action mFlushingAudio=%d, mFlushingVideo=%d",
1962 mFlushingAudio, mFlushingVideo);
1963
1964 break;
1965 }
1966
1967 sp<Action> action = *mDeferredActions.begin();
1968 mDeferredActions.erase(mDeferredActions.begin());
1969
1970 action->execute(this);
1971 }
1972}
1973
Wei Jia29840802015-05-15 17:11:38 -07001974void NuPlayer::performSeek(int64_t seekTimeUs) {
1975 ALOGV("performSeek seekTimeUs=%lld us (%.2f secs)",
Lajos Molnar6d339f12015-04-17 16:15:53 -07001976 (long long)seekTimeUs,
Wei Jia29840802015-05-15 17:11:38 -07001977 seekTimeUs / 1E6);
Andreas Hubera1f8ab02012-11-30 10:53:22 -08001978
Andy Hungadf34bf2014-09-03 18:22:22 -07001979 if (mSource == NULL) {
1980 // This happens when reset occurs right before the loop mode
1981 // asynchronously seeks to the start of the stream.
1982 LOG_ALWAYS_FATAL_IF(mAudioDecoder != NULL || mVideoDecoder != NULL,
1983 "mSource is NULL and decoders not NULL audio(%p) video(%p)",
1984 mAudioDecoder.get(), mVideoDecoder.get());
1985 return;
1986 }
Robert Shih1a5c8592015-08-04 18:07:44 -07001987 mPreviousSeekTimeUs = seekTimeUs;
Andreas Hubera1f8ab02012-11-30 10:53:22 -08001988 mSource->seekTo(seekTimeUs);
Robert Shihd3b0bbb2014-07-23 15:00:25 -07001989 ++mTimedTextGeneration;
Andreas Hubera1f8ab02012-11-30 10:53:22 -08001990
Andreas Hubera1f8ab02012-11-30 10:53:22 -08001991 // everything's flushed, continue playback.
1992}
1993
Wei Jiafef808d2014-10-31 17:57:05 -07001994void NuPlayer::performDecoderFlush(FlushCommand audio, FlushCommand video) {
1995 ALOGV("performDecoderFlush audio=%d, video=%d", audio, video);
Andreas Hubera1f8ab02012-11-30 10:53:22 -08001996
Wei Jiafef808d2014-10-31 17:57:05 -07001997 if ((audio == FLUSH_CMD_NONE || mAudioDecoder == NULL)
1998 && (video == FLUSH_CMD_NONE || mVideoDecoder == NULL)) {
Andreas Hubera1f8ab02012-11-30 10:53:22 -08001999 return;
2000 }
2001
Wei Jiafef808d2014-10-31 17:57:05 -07002002 if (audio != FLUSH_CMD_NONE && mAudioDecoder != NULL) {
2003 flushDecoder(true /* audio */, (audio == FLUSH_CMD_SHUTDOWN));
Andreas Hubera1f8ab02012-11-30 10:53:22 -08002004 }
2005
Wei Jiafef808d2014-10-31 17:57:05 -07002006 if (video != FLUSH_CMD_NONE && mVideoDecoder != NULL) {
2007 flushDecoder(false /* audio */, (video == FLUSH_CMD_SHUTDOWN));
Andreas Hubera1f8ab02012-11-30 10:53:22 -08002008 }
2009}
2010
2011void NuPlayer::performReset() {
2012 ALOGV("performReset");
2013
2014 CHECK(mAudioDecoder == NULL);
2015 CHECK(mVideoDecoder == NULL);
2016
2017 cancelPollDuration();
2018
2019 ++mScanSourcesGeneration;
2020 mScanSourcesPending = false;
2021
Lajos Molnar09524832014-07-17 14:29:51 -07002022 if (mRendererLooper != NULL) {
2023 if (mRenderer != NULL) {
2024 mRendererLooper->unregisterHandler(mRenderer->id());
2025 }
2026 mRendererLooper->stop();
2027 mRendererLooper.clear();
2028 }
Andreas Hubera1f8ab02012-11-30 10:53:22 -08002029 mRenderer.clear();
Wei Jia57568df2014-09-22 10:16:29 -07002030 ++mRendererGeneration;
Andreas Hubera1f8ab02012-11-30 10:53:22 -08002031
2032 if (mSource != NULL) {
2033 mSource->stop();
Andreas Huberb5f25f02013-02-05 10:14:26 -08002034
Wei Jiac45a4b22016-04-15 15:30:23 -07002035 Mutex::Autolock autoLock(mSourceLock);
Andreas Hubera1f8ab02012-11-30 10:53:22 -08002036 mSource.clear();
2037 }
2038
2039 if (mDriver != NULL) {
2040 sp<NuPlayerDriver> driver = mDriver.promote();
2041 if (driver != NULL) {
2042 driver->notifyResetComplete();
2043 }
2044 }
Andreas Huber57a339c2012-12-03 11:18:00 -08002045
2046 mStarted = false;
Wei Jia8a092d32016-06-03 14:57:24 -07002047 mPrepared = false;
Ronghua Wu64c2d172015-10-07 16:52:19 -07002048 mResetting = false;
Robert Shih0c61a0d2015-07-06 15:09:10 -07002049 mSourceStarted = false;
Andreas Hubera1f8ab02012-11-30 10:53:22 -08002050}
2051
2052void NuPlayer::performScanSources() {
2053 ALOGV("performScanSources");
2054
Andreas Huber57a339c2012-12-03 11:18:00 -08002055 if (!mStarted) {
2056 return;
2057 }
2058
Andreas Hubera1f8ab02012-11-30 10:53:22 -08002059 if (mAudioDecoder == NULL || mVideoDecoder == NULL) {
2060 postScanSources();
2061 }
2062}
2063
Lajos Molnar1de1e252015-04-30 18:18:34 -07002064void NuPlayer::performSetSurface(const sp<Surface> &surface) {
Andreas Huber57a339c2012-12-03 11:18:00 -08002065 ALOGV("performSetSurface");
2066
Lajos Molnar1de1e252015-04-30 18:18:34 -07002067 mSurface = surface;
Andreas Huber57a339c2012-12-03 11:18:00 -08002068
2069 // XXX - ignore error from setVideoScalingMode for now
2070 setVideoScalingMode(mVideoScalingMode);
Chong Zhang13d6faa2014-08-22 15:35:28 -07002071
2072 if (mDriver != NULL) {
2073 sp<NuPlayerDriver> driver = mDriver.promote();
2074 if (driver != NULL) {
2075 driver->notifySetSurfaceComplete();
2076 }
2077 }
Andreas Huber57a339c2012-12-03 11:18:00 -08002078}
2079
Chong Zhangf8d71772014-11-26 15:08:34 -08002080void NuPlayer::performResumeDecoders(bool needNotify) {
2081 if (needNotify) {
2082 mResumePending = true;
2083 if (mVideoDecoder == NULL) {
2084 // if audio-only, we can notify seek complete now,
2085 // as the resume operation will be relatively fast.
2086 finishResume();
2087 }
2088 }
2089
Chong Zhang7137ec72014-11-12 16:41:05 -08002090 if (mVideoDecoder != NULL) {
Chong Zhangf8d71772014-11-26 15:08:34 -08002091 // When there is continuous seek, MediaPlayer will cache the seek
2092 // position, and send down new seek request when previous seek is
2093 // complete. Let's wait for at least one video output frame before
2094 // notifying seek complete, so that the video thumbnail gets updated
2095 // when seekbar is dragged.
2096 mVideoDecoder->signalResume(needNotify);
Chong Zhang7137ec72014-11-12 16:41:05 -08002097 }
2098
2099 if (mAudioDecoder != NULL) {
Chong Zhangf8d71772014-11-26 15:08:34 -08002100 mAudioDecoder->signalResume(false /* needNotify */);
2101 }
2102}
2103
2104void NuPlayer::finishResume() {
2105 if (mResumePending) {
2106 mResumePending = false;
Wei Jia1061c9c2015-05-19 16:02:17 -07002107 notifyDriverSeekComplete();
2108 }
2109}
2110
2111void NuPlayer::notifyDriverSeekComplete() {
2112 if (mDriver != NULL) {
2113 sp<NuPlayerDriver> driver = mDriver.promote();
2114 if (driver != NULL) {
2115 driver->notifySeekComplete();
Chong Zhangf8d71772014-11-26 15:08:34 -08002116 }
Chong Zhang7137ec72014-11-12 16:41:05 -08002117 }
2118}
2119
Andreas Huber9575c962013-02-05 13:59:56 -08002120void NuPlayer::onSourceNotify(const sp<AMessage> &msg) {
2121 int32_t what;
2122 CHECK(msg->findInt32("what", &what));
2123
2124 switch (what) {
Lajos Molnarfcd3e942015-03-31 10:06:48 -07002125 case Source::kWhatInstantiateSecureDecoders:
2126 {
2127 if (mSource == NULL) {
2128 // This is a stale notification from a source that was
2129 // asynchronously preparing when the client called reset().
2130 // We handled the reset, the source is gone.
2131 break;
2132 }
2133
2134 sp<AMessage> reply;
2135 CHECK(msg->findMessage("reply", &reply));
2136 status_t err = onInstantiateSecureDecoders();
2137 reply->setInt32("err", err);
2138 reply->post();
2139 break;
2140 }
2141
Andreas Huber9575c962013-02-05 13:59:56 -08002142 case Source::kWhatPrepared:
2143 {
Andreas Huberb5f28d42013-04-25 15:11:19 -07002144 if (mSource == NULL) {
2145 // This is a stale notification from a source that was
2146 // asynchronously preparing when the client called reset().
2147 // We handled the reset, the source is gone.
2148 break;
2149 }
2150
Andreas Huberec0c5972013-02-05 14:47:13 -08002151 int32_t err;
2152 CHECK(msg->findInt32("err", &err));
2153
Lajos Molnarfcd3e942015-03-31 10:06:48 -07002154 if (err != OK) {
2155 // shut down potential secure codecs in case client never calls reset
2156 mDeferredActions.push_back(
2157 new FlushDecoderAction(FLUSH_CMD_SHUTDOWN /* audio */,
2158 FLUSH_CMD_SHUTDOWN /* video */));
2159 processDeferredActions();
Wei Jia8a092d32016-06-03 14:57:24 -07002160 } else {
2161 mPrepared = true;
Lajos Molnarfcd3e942015-03-31 10:06:48 -07002162 }
2163
Andreas Huber9575c962013-02-05 13:59:56 -08002164 sp<NuPlayerDriver> driver = mDriver.promote();
2165 if (driver != NULL) {
Marco Nelissendd114d12014-05-28 15:23:14 -07002166 // notify duration first, so that it's definitely set when
2167 // the app received the "prepare complete" callback.
2168 int64_t durationUs;
2169 if (mSource->getDuration(&durationUs) == OK) {
2170 driver->notifyDuration(durationUs);
2171 }
Andreas Huberec0c5972013-02-05 14:47:13 -08002172 driver->notifyPrepareCompleted(err);
Andreas Huber9575c962013-02-05 13:59:56 -08002173 }
Andreas Huber99759402013-04-01 14:28:31 -07002174
Andreas Huber9575c962013-02-05 13:59:56 -08002175 break;
2176 }
2177
2178 case Source::kWhatFlagsChanged:
2179 {
2180 uint32_t flags;
2181 CHECK(msg->findInt32("flags", (int32_t *)&flags));
2182
Chong Zhang4b7069d2013-09-11 12:52:43 -07002183 sp<NuPlayerDriver> driver = mDriver.promote();
2184 if (driver != NULL) {
Wei Jia895651b2014-12-10 17:31:52 -08002185 if ((flags & NuPlayer::Source::FLAG_CAN_SEEK) == 0) {
2186 driver->notifyListener(
2187 MEDIA_INFO, MEDIA_INFO_NOT_SEEKABLE, 0);
2188 }
Chong Zhang4b7069d2013-09-11 12:52:43 -07002189 driver->notifyFlagsChanged(flags);
2190 }
2191
Andreas Huber9575c962013-02-05 13:59:56 -08002192 if ((mSourceFlags & Source::FLAG_DYNAMIC_DURATION)
2193 && (!(flags & Source::FLAG_DYNAMIC_DURATION))) {
2194 cancelPollDuration();
2195 } else if (!(mSourceFlags & Source::FLAG_DYNAMIC_DURATION)
2196 && (flags & Source::FLAG_DYNAMIC_DURATION)
2197 && (mAudioDecoder != NULL || mVideoDecoder != NULL)) {
2198 schedulePollDuration();
2199 }
2200
2201 mSourceFlags = flags;
2202 break;
2203 }
2204
2205 case Source::kWhatVideoSizeChanged:
2206 {
Chong Zhangced1c2f2014-08-08 15:22:35 -07002207 sp<AMessage> format;
2208 CHECK(msg->findMessage("format", &format));
Andreas Huber9575c962013-02-05 13:59:56 -08002209
Chong Zhangced1c2f2014-08-08 15:22:35 -07002210 updateVideoSize(format);
Andreas Huber9575c962013-02-05 13:59:56 -08002211 break;
2212 }
2213
Chong Zhang2a3cc9a2014-08-21 17:48:26 -07002214 case Source::kWhatBufferingUpdate:
2215 {
2216 int32_t percentage;
2217 CHECK(msg->findInt32("percentage", &percentage));
2218
2219 notifyListener(MEDIA_BUFFERING_UPDATE, percentage, 0);
2220 break;
2221 }
2222
Chong Zhangefbb6192015-01-30 17:13:27 -08002223 case Source::kWhatPauseOnBufferingStart:
2224 {
2225 // ignore if not playing
Chong Zhang8a048332015-05-06 15:16:28 -07002226 if (mStarted) {
Chong Zhangefbb6192015-01-30 17:13:27 -08002227 ALOGI("buffer low, pausing...");
2228
Chong Zhang8a048332015-05-06 15:16:28 -07002229 mPausedForBuffering = true;
Chong Zhangefbb6192015-01-30 17:13:27 -08002230 onPause();
2231 }
Wei Jiabfe82072016-05-20 09:21:50 -07002232 notifyListener(MEDIA_INFO, MEDIA_INFO_BUFFERING_START, 0);
Roger Jönssonb50e83e2013-01-21 16:26:41 +01002233 break;
2234 }
2235
Chong Zhangefbb6192015-01-30 17:13:27 -08002236 case Source::kWhatResumeOnBufferingEnd:
2237 {
2238 // ignore if not playing
Chong Zhang8a048332015-05-06 15:16:28 -07002239 if (mStarted) {
Chong Zhangefbb6192015-01-30 17:13:27 -08002240 ALOGI("buffer ready, resuming...");
2241
Chong Zhang8a048332015-05-06 15:16:28 -07002242 mPausedForBuffering = false;
2243
2244 // do not resume yet if client didn't unpause
2245 if (!mPausedByClient) {
2246 onResume();
2247 }
Chong Zhangefbb6192015-01-30 17:13:27 -08002248 }
Wei Jia3bed45a2016-02-17 11:06:47 -08002249 notifyListener(MEDIA_INFO, MEDIA_INFO_BUFFERING_END, 0);
Roger Jönssonb50e83e2013-01-21 16:26:41 +01002250 break;
2251 }
2252
Chong Zhangefbb6192015-01-30 17:13:27 -08002253 case Source::kWhatCacheStats:
2254 {
2255 int32_t kbps;
2256 CHECK(msg->findInt32("bandwidth", &kbps));
2257
2258 notifyListener(MEDIA_INFO, MEDIA_INFO_NETWORK_BANDWIDTH, kbps);
2259 break;
2260 }
2261
Chong Zhangdcb89b32013-08-06 09:44:47 -07002262 case Source::kWhatSubtitleData:
2263 {
2264 sp<ABuffer> buffer;
2265 CHECK(msg->findBuffer("buffer", &buffer));
2266
Chong Zhang404fced2014-06-11 14:45:31 -07002267 sendSubtitleData(buffer, 0 /* baseIndex */);
Chong Zhangdcb89b32013-08-06 09:44:47 -07002268 break;
2269 }
2270
Robert Shih08528432015-04-08 09:06:54 -07002271 case Source::kWhatTimedMetaData:
2272 {
2273 sp<ABuffer> buffer;
2274 if (!msg->findBuffer("buffer", &buffer)) {
2275 notifyListener(MEDIA_INFO, MEDIA_INFO_METADATA_UPDATE, 0);
2276 } else {
2277 sendTimedMetaData(buffer);
2278 }
2279 break;
2280 }
2281
Robert Shihd3b0bbb2014-07-23 15:00:25 -07002282 case Source::kWhatTimedTextData:
2283 {
2284 int32_t generation;
2285 if (msg->findInt32("generation", &generation)
2286 && generation != mTimedTextGeneration) {
2287 break;
2288 }
2289
2290 sp<ABuffer> buffer;
2291 CHECK(msg->findBuffer("buffer", &buffer));
2292
2293 sp<NuPlayerDriver> driver = mDriver.promote();
2294 if (driver == NULL) {
2295 break;
2296 }
2297
2298 int posMs;
2299 int64_t timeUs, posUs;
2300 driver->getCurrentPosition(&posMs);
Patrik2 Carlsson24d484b2015-01-27 16:49:45 +01002301 posUs = (int64_t) posMs * 1000ll;
Robert Shihd3b0bbb2014-07-23 15:00:25 -07002302 CHECK(buffer->meta()->findInt64("timeUs", &timeUs));
2303
2304 if (posUs < timeUs) {
2305 if (!msg->findInt32("generation", &generation)) {
2306 msg->setInt32("generation", mTimedTextGeneration);
2307 }
2308 msg->post(timeUs - posUs);
2309 } else {
2310 sendTimedTextData(buffer);
2311 }
2312 break;
2313 }
2314
Andreas Huber14f76722013-01-15 09:04:18 -08002315 case Source::kWhatQueueDecoderShutdown:
2316 {
2317 int32_t audio, video;
2318 CHECK(msg->findInt32("audio", &audio));
2319 CHECK(msg->findInt32("video", &video));
2320
2321 sp<AMessage> reply;
2322 CHECK(msg->findMessage("reply", &reply));
2323
2324 queueDecoderShutdown(audio, video, reply);
2325 break;
2326 }
2327
Ronghua Wu80276872014-08-28 15:50:29 -07002328 case Source::kWhatDrmNoLicense:
2329 {
2330 notifyListener(MEDIA_ERROR, MEDIA_ERROR_UNKNOWN, ERROR_DRM_NO_LICENSE);
2331 break;
2332 }
2333
Andreas Huber9575c962013-02-05 13:59:56 -08002334 default:
2335 TRESPASS();
2336 }
2337}
2338
Chong Zhanga7fa1d92014-06-11 14:49:23 -07002339void NuPlayer::onClosedCaptionNotify(const sp<AMessage> &msg) {
2340 int32_t what;
2341 CHECK(msg->findInt32("what", &what));
2342
2343 switch (what) {
2344 case NuPlayer::CCDecoder::kWhatClosedCaptionData:
2345 {
2346 sp<ABuffer> buffer;
2347 CHECK(msg->findBuffer("buffer", &buffer));
2348
2349 size_t inbandTracks = 0;
2350 if (mSource != NULL) {
2351 inbandTracks = mSource->getTrackCount();
2352 }
2353
2354 sendSubtitleData(buffer, inbandTracks);
2355 break;
2356 }
2357
2358 case NuPlayer::CCDecoder::kWhatTrackAdded:
2359 {
2360 notifyListener(MEDIA_INFO, MEDIA_INFO_METADATA_UPDATE, 0);
2361
2362 break;
2363 }
2364
2365 default:
2366 TRESPASS();
2367 }
2368
2369
2370}
2371
Chong Zhang404fced2014-06-11 14:45:31 -07002372void NuPlayer::sendSubtitleData(const sp<ABuffer> &buffer, int32_t baseIndex) {
2373 int32_t trackIndex;
2374 int64_t timeUs, durationUs;
2375 CHECK(buffer->meta()->findInt32("trackIndex", &trackIndex));
2376 CHECK(buffer->meta()->findInt64("timeUs", &timeUs));
2377 CHECK(buffer->meta()->findInt64("durationUs", &durationUs));
2378
2379 Parcel in;
2380 in.writeInt32(trackIndex + baseIndex);
2381 in.writeInt64(timeUs);
2382 in.writeInt64(durationUs);
2383 in.writeInt32(buffer->size());
2384 in.writeInt32(buffer->size());
2385 in.write(buffer->data(), buffer->size());
2386
2387 notifyListener(MEDIA_SUBTITLE_DATA, 0, 0, &in);
2388}
Robert Shihd3b0bbb2014-07-23 15:00:25 -07002389
Robert Shih08528432015-04-08 09:06:54 -07002390void NuPlayer::sendTimedMetaData(const sp<ABuffer> &buffer) {
2391 int64_t timeUs;
2392 CHECK(buffer->meta()->findInt64("timeUs", &timeUs));
2393
2394 Parcel in;
2395 in.writeInt64(timeUs);
2396 in.writeInt32(buffer->size());
2397 in.writeInt32(buffer->size());
2398 in.write(buffer->data(), buffer->size());
2399
2400 notifyListener(MEDIA_META_DATA, 0, 0, &in);
2401}
2402
Robert Shihd3b0bbb2014-07-23 15:00:25 -07002403void NuPlayer::sendTimedTextData(const sp<ABuffer> &buffer) {
2404 const void *data;
2405 size_t size = 0;
2406 int64_t timeUs;
Marco Nelissen55e2f4c2015-09-04 15:57:15 -07002407 int32_t flag = TextDescriptions::IN_BAND_TEXT_3GPP;
Robert Shihd3b0bbb2014-07-23 15:00:25 -07002408
2409 AString mime;
2410 CHECK(buffer->meta()->findString("mime", &mime));
2411 CHECK(strcasecmp(mime.c_str(), MEDIA_MIMETYPE_TEXT_3GPP) == 0);
2412
2413 data = buffer->data();
2414 size = buffer->size();
2415
2416 Parcel parcel;
2417 if (size > 0) {
2418 CHECK(buffer->meta()->findInt64("timeUs", &timeUs));
Marco Nelissen55e2f4c2015-09-04 15:57:15 -07002419 int32_t global = 0;
2420 if (buffer->meta()->findInt32("global", &global) && global) {
2421 flag |= TextDescriptions::GLOBAL_DESCRIPTIONS;
2422 } else {
2423 flag |= TextDescriptions::LOCAL_DESCRIPTIONS;
2424 }
Robert Shihd3b0bbb2014-07-23 15:00:25 -07002425 TextDescriptions::getParcelOfDescriptions(
2426 (const uint8_t *)data, size, flag, timeUs / 1000, &parcel);
2427 }
2428
2429 if ((parcel.dataSize() > 0)) {
2430 notifyListener(MEDIA_TIMED_TEXT, 0, 0, &parcel);
2431 } else { // send an empty timed text
2432 notifyListener(MEDIA_TIMED_TEXT, 0, 0);
2433 }
2434}
Andreas Huberb5f25f02013-02-05 10:14:26 -08002435////////////////////////////////////////////////////////////////////////////////
2436
Chong Zhangced1c2f2014-08-08 15:22:35 -07002437sp<AMessage> NuPlayer::Source::getFormat(bool audio) {
2438 sp<MetaData> meta = getFormatMeta(audio);
2439
2440 if (meta == NULL) {
2441 return NULL;
2442 }
2443
2444 sp<AMessage> msg = new AMessage;
2445
2446 if(convertMetaDataToMessage(meta, &msg) == OK) {
2447 return msg;
2448 }
2449 return NULL;
2450}
2451
Andreas Huber9575c962013-02-05 13:59:56 -08002452void NuPlayer::Source::notifyFlagsChanged(uint32_t flags) {
2453 sp<AMessage> notify = dupNotify();
2454 notify->setInt32("what", kWhatFlagsChanged);
2455 notify->setInt32("flags", flags);
2456 notify->post();
2457}
2458
Chong Zhangced1c2f2014-08-08 15:22:35 -07002459void NuPlayer::Source::notifyVideoSizeChanged(const sp<AMessage> &format) {
Andreas Huber9575c962013-02-05 13:59:56 -08002460 sp<AMessage> notify = dupNotify();
2461 notify->setInt32("what", kWhatVideoSizeChanged);
Chong Zhangced1c2f2014-08-08 15:22:35 -07002462 notify->setMessage("format", format);
Andreas Huber9575c962013-02-05 13:59:56 -08002463 notify->post();
2464}
2465
Andreas Huberec0c5972013-02-05 14:47:13 -08002466void NuPlayer::Source::notifyPrepared(status_t err) {
Andreas Huber9575c962013-02-05 13:59:56 -08002467 sp<AMessage> notify = dupNotify();
2468 notify->setInt32("what", kWhatPrepared);
Andreas Huberec0c5972013-02-05 14:47:13 -08002469 notify->setInt32("err", err);
Andreas Huber9575c962013-02-05 13:59:56 -08002470 notify->post();
2471}
2472
Lajos Molnarfcd3e942015-03-31 10:06:48 -07002473void NuPlayer::Source::notifyInstantiateSecureDecoders(const sp<AMessage> &reply) {
2474 sp<AMessage> notify = dupNotify();
2475 notify->setInt32("what", kWhatInstantiateSecureDecoders);
2476 notify->setMessage("reply", reply);
2477 notify->post();
2478}
2479
Andreas Huber84333e02014-02-07 15:36:10 -08002480void NuPlayer::Source::onMessageReceived(const sp<AMessage> & /* msg */) {
Andreas Huberb5f25f02013-02-05 10:14:26 -08002481 TRESPASS();
2482}
2483
Andreas Huberf9334412010-12-15 15:17:42 -08002484} // namespace android