blob: 09fefb0bda43bc643778b8563934d22d4f884512 [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
Andreas Huber3831a062010-12-21 10:22:33 -080040#include <media/stagefright/foundation/hexdump.h>
Andreas Huberf9334412010-12-15 15:17:42 -080041#include <media/stagefright/foundation/ABuffer.h>
42#include <media/stagefright/foundation/ADebug.h>
43#include <media/stagefright/foundation/AMessage.h>
Lajos Molnar09524832014-07-17 14:29:51 -070044#include <media/stagefright/MediaBuffer.h>
Andreas Huber3fe62152011-09-16 15:09:22 -070045#include <media/stagefright/MediaDefs.h>
Andreas Huberf9334412010-12-15 15:17:42 -080046#include <media/stagefright/MediaErrors.h>
47#include <media/stagefright/MetaData.h>
Andy McFadden8ba01022012-12-18 09:46:54 -080048#include <gui/IGraphicBufferProducer.h>
Andreas Huberf9334412010-12-15 15:17:42 -080049
Andreas Huber3fe62152011-09-16 15:09:22 -070050#include "avc_utils.h"
51
Andreas Huber84066782011-08-16 09:34:26 -070052#include "ESDS.h"
53#include <media/stagefright/Utils.h>
54
Andreas Huberf9334412010-12-15 15:17:42 -080055namespace android {
56
Andreas Hubera1f8ab02012-11-30 10:53:22 -080057struct NuPlayer::Action : public RefBase {
58 Action() {}
59
60 virtual void execute(NuPlayer *player) = 0;
61
62private:
63 DISALLOW_EVIL_CONSTRUCTORS(Action);
64};
65
66struct NuPlayer::SeekAction : public Action {
Wei Jiae427abf2014-09-22 15:21:11 -070067 SeekAction(int64_t seekTimeUs, bool needNotify)
68 : mSeekTimeUs(seekTimeUs),
69 mNeedNotify(needNotify) {
Andreas Hubera1f8ab02012-11-30 10:53:22 -080070 }
71
72 virtual void execute(NuPlayer *player) {
Wei Jiae427abf2014-09-22 15:21:11 -070073 player->performSeek(mSeekTimeUs, mNeedNotify);
Andreas Hubera1f8ab02012-11-30 10:53:22 -080074 }
75
76private:
77 int64_t mSeekTimeUs;
Wei Jiae427abf2014-09-22 15:21:11 -070078 bool mNeedNotify;
Andreas Hubera1f8ab02012-11-30 10:53:22 -080079
80 DISALLOW_EVIL_CONSTRUCTORS(SeekAction);
81};
82
Chong Zhangf8d71772014-11-26 15:08:34 -080083struct NuPlayer::ResumeDecoderAction : public Action {
84 ResumeDecoderAction(bool needNotify)
85 : mNeedNotify(needNotify) {
86 }
87
88 virtual void execute(NuPlayer *player) {
89 player->performResumeDecoders(mNeedNotify);
90 }
91
92private:
93 bool mNeedNotify;
94
95 DISALLOW_EVIL_CONSTRUCTORS(ResumeDecoderAction);
96};
97
Andreas Huber57a339c2012-12-03 11:18:00 -080098struct NuPlayer::SetSurfaceAction : public Action {
99 SetSurfaceAction(const sp<NativeWindowWrapper> &wrapper)
100 : mWrapper(wrapper) {
101 }
102
103 virtual void execute(NuPlayer *player) {
104 player->performSetSurface(mWrapper);
105 }
106
107private:
108 sp<NativeWindowWrapper> mWrapper;
109
110 DISALLOW_EVIL_CONSTRUCTORS(SetSurfaceAction);
111};
112
Wei Jiafef808d2014-10-31 17:57:05 -0700113struct NuPlayer::FlushDecoderAction : public Action {
114 FlushDecoderAction(FlushCommand audio, FlushCommand video)
Andreas Huber14f76722013-01-15 09:04:18 -0800115 : mAudio(audio),
116 mVideo(video) {
117 }
118
119 virtual void execute(NuPlayer *player) {
Wei Jiafef808d2014-10-31 17:57:05 -0700120 player->performDecoderFlush(mAudio, mVideo);
Andreas Huber14f76722013-01-15 09:04:18 -0800121 }
122
123private:
Wei Jiafef808d2014-10-31 17:57:05 -0700124 FlushCommand mAudio;
125 FlushCommand mVideo;
Andreas Huber14f76722013-01-15 09:04:18 -0800126
Wei Jiafef808d2014-10-31 17:57:05 -0700127 DISALLOW_EVIL_CONSTRUCTORS(FlushDecoderAction);
Andreas Huber14f76722013-01-15 09:04:18 -0800128};
129
130struct NuPlayer::PostMessageAction : public Action {
131 PostMessageAction(const sp<AMessage> &msg)
132 : mMessage(msg) {
133 }
134
135 virtual void execute(NuPlayer *) {
136 mMessage->post();
137 }
138
139private:
140 sp<AMessage> mMessage;
141
142 DISALLOW_EVIL_CONSTRUCTORS(PostMessageAction);
143};
144
Andreas Hubera1f8ab02012-11-30 10:53:22 -0800145// Use this if there's no state necessary to save in order to execute
146// the action.
147struct NuPlayer::SimpleAction : public Action {
148 typedef void (NuPlayer::*ActionFunc)();
149
150 SimpleAction(ActionFunc func)
151 : mFunc(func) {
152 }
153
154 virtual void execute(NuPlayer *player) {
155 (player->*mFunc)();
156 }
157
158private:
159 ActionFunc mFunc;
160
161 DISALLOW_EVIL_CONSTRUCTORS(SimpleAction);
162};
163
Andreas Huberf9334412010-12-15 15:17:42 -0800164////////////////////////////////////////////////////////////////////////////////
165
166NuPlayer::NuPlayer()
Andreas Huber9b80c2b2011-06-30 15:47:02 -0700167 : mUIDValid(false),
Andreas Huber9575c962013-02-05 13:59:56 -0800168 mSourceFlags(0),
Wei Jiabc2fb722014-07-08 16:37:57 -0700169 mOffloadAudio(false),
Wei Jia88703c32014-08-06 11:24:07 -0700170 mAudioDecoderGeneration(0),
171 mVideoDecoderGeneration(0),
Wei Jia57568df2014-09-22 10:16:29 -0700172 mRendererGeneration(0),
Andreas Huber9b80c2b2011-06-30 15:47:02 -0700173 mAudioEOS(false),
Andreas Huberf9334412010-12-15 15:17:42 -0800174 mVideoEOS(false),
Andreas Huber5bc087c2010-12-23 10:27:40 -0800175 mScanSourcesPending(false),
Andreas Huber1aef2112011-01-04 14:01:29 -0800176 mScanSourcesGeneration(0),
Andreas Huberb7c8e912012-11-27 15:02:53 -0800177 mPollDurationGeneration(0),
Robert Shihd3b0bbb2014-07-23 15:00:25 -0700178 mTimedTextGeneration(0),
Andreas Huberf9334412010-12-15 15:17:42 -0800179 mFlushingAudio(NONE),
Andreas Huber1aef2112011-01-04 14:01:29 -0800180 mFlushingVideo(NONE),
Chong Zhangf8d71772014-11-26 15:08:34 -0800181 mResumePending(false),
Andreas Huber57a339c2012-12-03 11:18:00 -0800182 mVideoScalingMode(NATIVE_WINDOW_SCALING_MODE_SCALE_TO_WINDOW),
Wei Jia98160162015-02-04 17:01:11 -0800183 mPlaybackRate(1.0),
Chong Zhangefbb6192015-01-30 17:13:27 -0800184 mStarted(false),
185 mPaused(false),
186 mPausedByClient(false) {
Andy Hung8d121d42014-10-03 09:53:53 -0700187 clearFlushComplete();
Andreas Huberf9334412010-12-15 15:17:42 -0800188}
189
190NuPlayer::~NuPlayer() {
191}
192
Andreas Huber9b80c2b2011-06-30 15:47:02 -0700193void NuPlayer::setUID(uid_t uid) {
194 mUIDValid = true;
195 mUID = uid;
196}
197
Andreas Huber43c3e6c2011-01-05 12:17:08 -0800198void NuPlayer::setDriver(const wp<NuPlayerDriver> &driver) {
199 mDriver = driver;
Andreas Huberf9334412010-12-15 15:17:42 -0800200}
201
Andreas Huber9575c962013-02-05 13:59:56 -0800202void NuPlayer::setDataSourceAsync(const sp<IStreamSource> &source) {
Lajos Molnar1d15ab52015-03-04 16:46:34 -0800203 sp<AMessage> msg = new AMessage(kWhatSetDataSource, this);
Andreas Huberf9334412010-12-15 15:17:42 -0800204
Lajos Molnar1d15ab52015-03-04 16:46:34 -0800205 sp<AMessage> notify = new AMessage(kWhatSourceNotify, this);
Andreas Huberb5f25f02013-02-05 10:14:26 -0800206
Andreas Huber240abcc2014-02-13 13:32:37 -0800207 msg->setObject("source", new StreamingSource(notify, source));
Andreas Huber5bc087c2010-12-23 10:27:40 -0800208 msg->post();
209}
Andreas Huberf9334412010-12-15 15:17:42 -0800210
Andreas Huberafed0e12011-09-20 15:39:58 -0700211static bool IsHTTPLiveURL(const char *url) {
212 if (!strncasecmp("http://", url, 7)
Andreas Huber99759402013-04-01 14:28:31 -0700213 || !strncasecmp("https://", url, 8)
214 || !strncasecmp("file://", url, 7)) {
Andreas Huberafed0e12011-09-20 15:39:58 -0700215 size_t len = strlen(url);
216 if (len >= 5 && !strcasecmp(".m3u8", &url[len - 5])) {
217 return true;
218 }
219
220 if (strstr(url,"m3u8")) {
221 return true;
222 }
223 }
224
225 return false;
226}
227
Andreas Huber9575c962013-02-05 13:59:56 -0800228void NuPlayer::setDataSourceAsync(
Andreas Huber1b86fe02014-01-29 11:13:26 -0800229 const sp<IMediaHTTPService> &httpService,
230 const char *url,
231 const KeyedVector<String8, String8> *headers) {
Chong Zhang3de157d2014-08-05 20:54:44 -0700232
Lajos Molnar1d15ab52015-03-04 16:46:34 -0800233 sp<AMessage> msg = new AMessage(kWhatSetDataSource, this);
Oscar Rydhé7a33b772012-02-20 10:15:48 +0100234 size_t len = strlen(url);
Andreas Huber5bc087c2010-12-23 10:27:40 -0800235
Lajos Molnar1d15ab52015-03-04 16:46:34 -0800236 sp<AMessage> notify = new AMessage(kWhatSourceNotify, this);
Andreas Huberb5f25f02013-02-05 10:14:26 -0800237
Andreas Huberafed0e12011-09-20 15:39:58 -0700238 sp<Source> source;
239 if (IsHTTPLiveURL(url)) {
Andreas Huber81e68442014-02-05 11:52:33 -0800240 source = new HTTPLiveSource(notify, httpService, url, headers);
Andreas Huberafed0e12011-09-20 15:39:58 -0700241 } else if (!strncasecmp(url, "rtsp://", 7)) {
Andreas Huber1b86fe02014-01-29 11:13:26 -0800242 source = new RTSPSource(
243 notify, httpService, url, headers, mUIDValid, mUID);
Oscar Rydhé7a33b772012-02-20 10:15:48 +0100244 } else if ((!strncasecmp(url, "http://", 7)
245 || !strncasecmp(url, "https://", 8))
246 && ((len >= 4 && !strcasecmp(".sdp", &url[len - 4]))
247 || strstr(url, ".sdp?"))) {
Andreas Huber1b86fe02014-01-29 11:13:26 -0800248 source = new RTSPSource(
249 notify, httpService, url, headers, mUIDValid, mUID, true);
Andreas Huber2bfdd422011-10-11 15:24:07 -0700250 } else {
Chong Zhang3de157d2014-08-05 20:54:44 -0700251 sp<GenericSource> genericSource =
252 new GenericSource(notify, mUIDValid, mUID);
253 // Don't set FLAG_SECURE on mSourceFlags here for widevine.
254 // The correct flags will be updated in Source::kWhatFlagsChanged
255 // handler when GenericSource is prepared.
Andreas Huber2bfdd422011-10-11 15:24:07 -0700256
Chong Zhanga19f33e2014-08-07 15:35:07 -0700257 status_t err = genericSource->setDataSource(httpService, url, headers);
Chong Zhang3de157d2014-08-05 20:54:44 -0700258
259 if (err == OK) {
260 source = genericSource;
261 } else {
Chong Zhanga19f33e2014-08-07 15:35:07 -0700262 ALOGE("Failed to set data source!");
Chong Zhang3de157d2014-08-05 20:54:44 -0700263 }
264 }
Andreas Huberafed0e12011-09-20 15:39:58 -0700265 msg->setObject("source", source);
266 msg->post();
267}
268
Andreas Huber9575c962013-02-05 13:59:56 -0800269void NuPlayer::setDataSourceAsync(int fd, int64_t offset, int64_t length) {
Lajos Molnar1d15ab52015-03-04 16:46:34 -0800270 sp<AMessage> msg = new AMessage(kWhatSetDataSource, this);
Andreas Huberafed0e12011-09-20 15:39:58 -0700271
Lajos Molnar1d15ab52015-03-04 16:46:34 -0800272 sp<AMessage> notify = new AMessage(kWhatSourceNotify, this);
Andreas Huberb5f25f02013-02-05 10:14:26 -0800273
Chong Zhang3de157d2014-08-05 20:54:44 -0700274 sp<GenericSource> source =
275 new GenericSource(notify, mUIDValid, mUID);
276
Chong Zhanga19f33e2014-08-07 15:35:07 -0700277 status_t err = source->setDataSource(fd, offset, length);
Chong Zhang3de157d2014-08-05 20:54:44 -0700278
279 if (err != OK) {
Chong Zhanga19f33e2014-08-07 15:35:07 -0700280 ALOGE("Failed to set data source!");
Chong Zhang3de157d2014-08-05 20:54:44 -0700281 source = NULL;
282 }
283
Andreas Huberafed0e12011-09-20 15:39:58 -0700284 msg->setObject("source", source);
Andreas Huberf9334412010-12-15 15:17:42 -0800285 msg->post();
286}
287
Chris Watkins99f31602015-03-20 13:06:33 -0700288void NuPlayer::setDataSourceAsync(const sp<DataSource> &dataSource) {
289 sp<AMessage> msg = new AMessage(kWhatSetDataSource, this);
290 sp<AMessage> notify = new AMessage(kWhatSourceNotify, this);
291
292 sp<GenericSource> source = new GenericSource(notify, mUIDValid, mUID);
293 status_t err = source->setDataSource(dataSource);
294
295 if (err != OK) {
296 ALOGE("Failed to set data source!");
297 source = NULL;
298 }
299
300 msg->setObject("source", source);
301 msg->post();
302}
303
Andreas Huber9575c962013-02-05 13:59:56 -0800304void NuPlayer::prepareAsync() {
Lajos Molnar1d15ab52015-03-04 16:46:34 -0800305 (new AMessage(kWhatPrepare, this))->post();
Andreas Huber9575c962013-02-05 13:59:56 -0800306}
307
Andreas Huber57a339c2012-12-03 11:18:00 -0800308void NuPlayer::setVideoSurfaceTextureAsync(
Andy McFadden8ba01022012-12-18 09:46:54 -0800309 const sp<IGraphicBufferProducer> &bufferProducer) {
Lajos Molnar1d15ab52015-03-04 16:46:34 -0800310 sp<AMessage> msg = new AMessage(kWhatSetVideoNativeWindow, this);
Andreas Huber57a339c2012-12-03 11:18:00 -0800311
Andy McFadden8ba01022012-12-18 09:46:54 -0800312 if (bufferProducer == NULL) {
Andreas Huber57a339c2012-12-03 11:18:00 -0800313 msg->setObject("native-window", NULL);
314 } else {
315 msg->setObject(
316 "native-window",
317 new NativeWindowWrapper(
Wei Jia9c03a402014-08-26 15:24:43 -0700318 new Surface(bufferProducer, true /* controlledByApp */)));
Andreas Huber57a339c2012-12-03 11:18:00 -0800319 }
320
Andreas Huberf9334412010-12-15 15:17:42 -0800321 msg->post();
322}
323
324void NuPlayer::setAudioSink(const sp<MediaPlayerBase::AudioSink> &sink) {
Lajos Molnar1d15ab52015-03-04 16:46:34 -0800325 sp<AMessage> msg = new AMessage(kWhatSetAudioSink, this);
Andreas Huberf9334412010-12-15 15:17:42 -0800326 msg->setObject("sink", sink);
327 msg->post();
328}
329
330void NuPlayer::start() {
Lajos Molnar1d15ab52015-03-04 16:46:34 -0800331 (new AMessage(kWhatStart, this))->post();
Andreas Huberf9334412010-12-15 15:17:42 -0800332}
333
Wei Jia98160162015-02-04 17:01:11 -0800334void NuPlayer::setPlaybackRate(float rate) {
Lajos Molnar1d15ab52015-03-04 16:46:34 -0800335 sp<AMessage> msg = new AMessage(kWhatSetRate, this);
Wei Jia98160162015-02-04 17:01:11 -0800336 msg->setFloat("rate", rate);
337 msg->post();
338}
339
Andreas Huber43c3e6c2011-01-05 12:17:08 -0800340void NuPlayer::pause() {
Lajos Molnar1d15ab52015-03-04 16:46:34 -0800341 (new AMessage(kWhatPause, this))->post();
Andreas Huber43c3e6c2011-01-05 12:17:08 -0800342}
343
Andreas Huber1aef2112011-01-04 14:01:29 -0800344void NuPlayer::resetAsync() {
Chong Zhang48296b72014-09-14 14:28:45 -0700345 if (mSource != NULL) {
346 // During a reset, the data source might be unresponsive already, we need to
347 // disconnect explicitly so that reads exit promptly.
348 // We can't queue the disconnect request to the looper, as it might be
349 // queued behind a stuck read and never gets processed.
350 // Doing a disconnect outside the looper to allows the pending reads to exit
351 // (either successfully or with error).
352 mSource->disconnect();
353 }
354
Lajos Molnar1d15ab52015-03-04 16:46:34 -0800355 (new AMessage(kWhatReset, this))->post();
Andreas Huber1aef2112011-01-04 14:01:29 -0800356}
357
Wei Jiae427abf2014-09-22 15:21:11 -0700358void NuPlayer::seekToAsync(int64_t seekTimeUs, bool needNotify) {
Lajos Molnar1d15ab52015-03-04 16:46:34 -0800359 sp<AMessage> msg = new AMessage(kWhatSeek, this);
Andreas Huber43c3e6c2011-01-05 12:17:08 -0800360 msg->setInt64("seekTimeUs", seekTimeUs);
Wei Jiae427abf2014-09-22 15:21:11 -0700361 msg->setInt32("needNotify", needNotify);
Andreas Huber43c3e6c2011-01-05 12:17:08 -0800362 msg->post();
363}
364
Andreas Huber53df1a42010-12-22 10:03:04 -0800365
Chong Zhang404fced2014-06-11 14:45:31 -0700366void NuPlayer::writeTrackInfo(
367 Parcel* reply, const sp<AMessage> format) const {
368 int32_t trackType;
369 CHECK(format->findInt32("type", &trackType));
370
Robert Shih755106e2015-04-30 14:36:45 -0700371 AString mime;
372 CHECK(format->findString("mime", &mime));
373
Chong Zhang404fced2014-06-11 14:45:31 -0700374 AString lang;
375 CHECK(format->findString("language", &lang));
376
377 reply->writeInt32(2); // write something non-zero
378 reply->writeInt32(trackType);
Robert Shih755106e2015-04-30 14:36:45 -0700379 reply->writeString16(String16(mime.c_str()));
Chong Zhang404fced2014-06-11 14:45:31 -0700380 reply->writeString16(String16(lang.c_str()));
381
382 if (trackType == MEDIA_TRACK_TYPE_SUBTITLE) {
Chong Zhang404fced2014-06-11 14:45:31 -0700383 int32_t isAuto, isDefault, isForced;
384 CHECK(format->findInt32("auto", &isAuto));
385 CHECK(format->findInt32("default", &isDefault));
386 CHECK(format->findInt32("forced", &isForced));
387
Chong Zhang404fced2014-06-11 14:45:31 -0700388 reply->writeInt32(isAuto);
389 reply->writeInt32(isDefault);
390 reply->writeInt32(isForced);
391 }
392}
393
Andreas Huberf9334412010-12-15 15:17:42 -0800394void NuPlayer::onMessageReceived(const sp<AMessage> &msg) {
395 switch (msg->what()) {
396 case kWhatSetDataSource:
397 {
Steve Block3856b092011-10-20 11:56:00 +0100398 ALOGV("kWhatSetDataSource");
Andreas Huberf9334412010-12-15 15:17:42 -0800399
400 CHECK(mSource == NULL);
401
Chong Zhang3de157d2014-08-05 20:54:44 -0700402 status_t err = OK;
Andreas Huber5bc087c2010-12-23 10:27:40 -0800403 sp<RefBase> obj;
404 CHECK(msg->findObject("source", &obj));
Chong Zhang3de157d2014-08-05 20:54:44 -0700405 if (obj != NULL) {
406 mSource = static_cast<Source *>(obj.get());
Chong Zhang3de157d2014-08-05 20:54:44 -0700407 } else {
408 err = UNKNOWN_ERROR;
409 }
Andreas Huber9575c962013-02-05 13:59:56 -0800410
411 CHECK(mDriver != NULL);
412 sp<NuPlayerDriver> driver = mDriver.promote();
413 if (driver != NULL) {
Chong Zhang3de157d2014-08-05 20:54:44 -0700414 driver->notifySetDataSourceCompleted(err);
Andreas Huber9575c962013-02-05 13:59:56 -0800415 }
416 break;
417 }
418
419 case kWhatPrepare:
420 {
421 mSource->prepareAsync();
Andreas Huberf9334412010-12-15 15:17:42 -0800422 break;
423 }
424
Chong Zhangdcb89b32013-08-06 09:44:47 -0700425 case kWhatGetTrackInfo:
426 {
Lajos Molnar3f274362015-03-05 14:35:41 -0800427 sp<AReplyToken> replyID;
Chong Zhangdcb89b32013-08-06 09:44:47 -0700428 CHECK(msg->senderAwaitsResponse(&replyID));
429
Chong Zhang404fced2014-06-11 14:45:31 -0700430 Parcel* reply;
431 CHECK(msg->findPointer("reply", (void**)&reply));
432
433 size_t inbandTracks = 0;
Chong Zhangdcb89b32013-08-06 09:44:47 -0700434 if (mSource != NULL) {
Chong Zhang404fced2014-06-11 14:45:31 -0700435 inbandTracks = mSource->getTrackCount();
436 }
437
Chong Zhanga7fa1d92014-06-11 14:49:23 -0700438 size_t ccTracks = 0;
439 if (mCCDecoder != NULL) {
440 ccTracks = mCCDecoder->getTrackCount();
441 }
442
Chong Zhang404fced2014-06-11 14:45:31 -0700443 // total track count
Chong Zhanga7fa1d92014-06-11 14:49:23 -0700444 reply->writeInt32(inbandTracks + ccTracks);
Chong Zhang404fced2014-06-11 14:45:31 -0700445
446 // write inband tracks
447 for (size_t i = 0; i < inbandTracks; ++i) {
448 writeTrackInfo(reply, mSource->getTrackInfo(i));
Chong Zhangdcb89b32013-08-06 09:44:47 -0700449 }
450
Chong Zhanga7fa1d92014-06-11 14:49:23 -0700451 // write CC track
452 for (size_t i = 0; i < ccTracks; ++i) {
453 writeTrackInfo(reply, mCCDecoder->getTrackInfo(i));
454 }
455
Chong Zhangdcb89b32013-08-06 09:44:47 -0700456 sp<AMessage> response = new AMessage;
Chong Zhangdcb89b32013-08-06 09:44:47 -0700457 response->postReply(replyID);
458 break;
459 }
460
Robert Shih7c4f0d72014-07-09 18:53:31 -0700461 case kWhatGetSelectedTrack:
462 {
463 status_t err = INVALID_OPERATION;
464 if (mSource != NULL) {
465 err = OK;
466
467 int32_t type32;
468 CHECK(msg->findInt32("type", (int32_t*)&type32));
469 media_track_type type = (media_track_type)type32;
470 ssize_t selectedTrack = mSource->getSelectedTrack(type);
471
472 Parcel* reply;
473 CHECK(msg->findPointer("reply", (void**)&reply));
474 reply->writeInt32(selectedTrack);
475 }
476
477 sp<AMessage> response = new AMessage;
478 response->setInt32("err", err);
479
Lajos Molnar3f274362015-03-05 14:35:41 -0800480 sp<AReplyToken> replyID;
Robert Shih7c4f0d72014-07-09 18:53:31 -0700481 CHECK(msg->senderAwaitsResponse(&replyID));
482 response->postReply(replyID);
483 break;
484 }
485
Chong Zhangdcb89b32013-08-06 09:44:47 -0700486 case kWhatSelectTrack:
487 {
Lajos Molnar3f274362015-03-05 14:35:41 -0800488 sp<AReplyToken> replyID;
Chong Zhangdcb89b32013-08-06 09:44:47 -0700489 CHECK(msg->senderAwaitsResponse(&replyID));
490
Chong Zhang404fced2014-06-11 14:45:31 -0700491 size_t trackIndex;
492 int32_t select;
Robert Shih6ffb1fd2014-10-29 16:24:32 -0700493 int64_t timeUs;
Chong Zhang404fced2014-06-11 14:45:31 -0700494 CHECK(msg->findSize("trackIndex", &trackIndex));
495 CHECK(msg->findInt32("select", &select));
Robert Shih6ffb1fd2014-10-29 16:24:32 -0700496 CHECK(msg->findInt64("timeUs", &timeUs));
Chong Zhang404fced2014-06-11 14:45:31 -0700497
Chong Zhangdcb89b32013-08-06 09:44:47 -0700498 status_t err = INVALID_OPERATION;
Chong Zhang404fced2014-06-11 14:45:31 -0700499
500 size_t inbandTracks = 0;
Chong Zhangdcb89b32013-08-06 09:44:47 -0700501 if (mSource != NULL) {
Chong Zhang404fced2014-06-11 14:45:31 -0700502 inbandTracks = mSource->getTrackCount();
503 }
Chong Zhanga7fa1d92014-06-11 14:49:23 -0700504 size_t ccTracks = 0;
505 if (mCCDecoder != NULL) {
506 ccTracks = mCCDecoder->getTrackCount();
507 }
Chong Zhang404fced2014-06-11 14:45:31 -0700508
509 if (trackIndex < inbandTracks) {
Robert Shih6ffb1fd2014-10-29 16:24:32 -0700510 err = mSource->selectTrack(trackIndex, select, timeUs);
Robert Shihd3b0bbb2014-07-23 15:00:25 -0700511
512 if (!select && err == OK) {
513 int32_t type;
514 sp<AMessage> info = mSource->getTrackInfo(trackIndex);
515 if (info != NULL
516 && info->findInt32("type", &type)
517 && type == MEDIA_TRACK_TYPE_TIMEDTEXT) {
518 ++mTimedTextGeneration;
519 }
520 }
Chong Zhanga7fa1d92014-06-11 14:49:23 -0700521 } else {
522 trackIndex -= inbandTracks;
523
524 if (trackIndex < ccTracks) {
525 err = mCCDecoder->selectTrack(trackIndex, select);
526 }
Chong Zhangdcb89b32013-08-06 09:44:47 -0700527 }
528
529 sp<AMessage> response = new AMessage;
530 response->setInt32("err", err);
531
532 response->postReply(replyID);
533 break;
534 }
535
Andreas Huberb7c8e912012-11-27 15:02:53 -0800536 case kWhatPollDuration:
537 {
538 int32_t generation;
539 CHECK(msg->findInt32("generation", &generation));
540
541 if (generation != mPollDurationGeneration) {
542 // stale
543 break;
544 }
545
546 int64_t durationUs;
547 if (mDriver != NULL && mSource->getDuration(&durationUs) == OK) {
548 sp<NuPlayerDriver> driver = mDriver.promote();
549 if (driver != NULL) {
550 driver->notifyDuration(durationUs);
551 }
552 }
553
554 msg->post(1000000ll); // poll again in a second.
555 break;
556 }
557
Glenn Kasten11731182011-02-08 17:26:17 -0800558 case kWhatSetVideoNativeWindow:
Andreas Huberf9334412010-12-15 15:17:42 -0800559 {
Steve Block3856b092011-10-20 11:56:00 +0100560 ALOGV("kWhatSetVideoNativeWindow");
Andreas Huberf9334412010-12-15 15:17:42 -0800561
562 sp<RefBase> obj;
Glenn Kasten11731182011-02-08 17:26:17 -0800563 CHECK(msg->findObject("native-window", &obj));
Andreas Huberf9334412010-12-15 15:17:42 -0800564
Lajos Molnarf4849522014-12-10 16:58:57 -0800565 if (mSource == NULL || mSource->getFormat(false /* audio */) == NULL) {
Wei Jiafef808d2014-10-31 17:57:05 -0700566 performSetSurface(static_cast<NativeWindowWrapper *>(obj.get()));
567 break;
568 }
569
570 mDeferredActions.push_back(
571 new FlushDecoderAction(FLUSH_CMD_FLUSH /* audio */,
572 FLUSH_CMD_SHUTDOWN /* video */));
573
Andreas Huber57a339c2012-12-03 11:18:00 -0800574 mDeferredActions.push_back(
575 new SetSurfaceAction(
576 static_cast<NativeWindowWrapper *>(obj.get())));
James Dong0d268a32012-08-31 12:18:27 -0700577
Andreas Huber57a339c2012-12-03 11:18:00 -0800578 if (obj != NULL) {
Wei Jiafef808d2014-10-31 17:57:05 -0700579 if (mStarted) {
Andy Hung73535852014-09-05 11:42:58 -0700580 // Issue a seek to refresh the video screen only if started otherwise
581 // the extractor may not yet be started and will assert.
582 // If the video decoder is not set (perhaps audio only in this case)
583 // do not perform a seek as it is not needed.
Ronghua Wua73d9e02014-10-08 15:13:29 -0700584 int64_t currentPositionUs = 0;
585 if (getCurrentPosition(&currentPositionUs) == OK) {
586 mDeferredActions.push_back(
587 new SeekAction(currentPositionUs, false /* needNotify */));
588 }
Andy Hung73535852014-09-05 11:42:58 -0700589 }
Wei Jiaac428aa2014-09-02 19:01:34 -0700590
Andreas Huber57a339c2012-12-03 11:18:00 -0800591 // If there is a new surface texture, instantiate decoders
592 // again if possible.
593 mDeferredActions.push_back(
594 new SimpleAction(&NuPlayer::performScanSources));
595 }
596
Chong Zhangf8d71772014-11-26 15:08:34 -0800597 // After a flush without shutdown, decoder is paused.
598 // Don't resume it until source seek is done, otherwise it could
Chong Zhang7137ec72014-11-12 16:41:05 -0800599 // start pulling stale data too soon.
600 mDeferredActions.push_back(
Chong Zhangf8d71772014-11-26 15:08:34 -0800601 new ResumeDecoderAction(false /* needNotify */));
Chong Zhang7137ec72014-11-12 16:41:05 -0800602
Andreas Huber57a339c2012-12-03 11:18:00 -0800603 processDeferredActions();
Andreas Huberf9334412010-12-15 15:17:42 -0800604 break;
605 }
606
607 case kWhatSetAudioSink:
608 {
Steve Block3856b092011-10-20 11:56:00 +0100609 ALOGV("kWhatSetAudioSink");
Andreas Huberf9334412010-12-15 15:17:42 -0800610
611 sp<RefBase> obj;
612 CHECK(msg->findObject("sink", &obj));
613
614 mAudioSink = static_cast<MediaPlayerBase::AudioSink *>(obj.get());
615 break;
616 }
617
618 case kWhatStart:
619 {
Steve Block3856b092011-10-20 11:56:00 +0100620 ALOGV("kWhatStart");
Wei Jia94211742014-10-28 17:09:06 -0700621 if (mStarted) {
622 onResume();
623 } else {
624 onStart();
Lajos Molnar09524832014-07-17 14:29:51 -0700625 }
Chong Zhangefbb6192015-01-30 17:13:27 -0800626 mPausedByClient = false;
Andreas Huberf9334412010-12-15 15:17:42 -0800627 break;
628 }
629
Wei Jia98160162015-02-04 17:01:11 -0800630 case kWhatSetRate:
631 {
632 ALOGV("kWhatSetRate");
633 CHECK(msg->findFloat("rate", &mPlaybackRate));
634 if (mRenderer != NULL) {
635 mRenderer->setPlaybackRate(mPlaybackRate);
636 }
Ronghua Wu8db88132015-04-22 13:51:35 -0700637
638 if (mVideoDecoder != NULL) {
639 sp<MetaData> meta = getFileMeta();
640 int32_t rate;
641 if (meta != NULL && meta->findInt32(kKeyFrameRate, &rate) && rate > 0) {
642 sp<AMessage> params = new AMessage();
643 params->setFloat("operating-rate", rate * mPlaybackRate);
644 mVideoDecoder->setParameters(params);
645 }
646 }
647
Wei Jia98160162015-02-04 17:01:11 -0800648 break;
649 }
650
Andreas Huberf9334412010-12-15 15:17:42 -0800651 case kWhatScanSources:
652 {
Andreas Huber1aef2112011-01-04 14:01:29 -0800653 int32_t generation;
654 CHECK(msg->findInt32("generation", &generation));
655 if (generation != mScanSourcesGeneration) {
656 // Drop obsolete msg.
657 break;
658 }
659
Andreas Huber5bc087c2010-12-23 10:27:40 -0800660 mScanSourcesPending = false;
661
Steve Block3856b092011-10-20 11:56:00 +0100662 ALOGV("scanning sources haveAudio=%d, haveVideo=%d",
Andreas Huber43c3e6c2011-01-05 12:17:08 -0800663 mAudioDecoder != NULL, mVideoDecoder != NULL);
664
Andreas Huberb7c8e912012-11-27 15:02:53 -0800665 bool mHadAnySourcesBefore =
666 (mAudioDecoder != NULL) || (mVideoDecoder != NULL);
667
Andy Hung282a7e32014-08-14 15:56:34 -0700668 // initialize video before audio because successful initialization of
669 // video may change deep buffer mode of audio.
Haynes Mathew George5d246ef2012-07-09 10:36:57 -0700670 if (mNativeWindow != NULL) {
671 instantiateDecoder(false, &mVideoDecoder);
672 }
Andreas Huberf9334412010-12-15 15:17:42 -0800673
Ronghua Wua10fd232014-11-06 16:15:20 -0800674 // Don't try to re-open audio sink if there's an existing decoder.
675 if (mAudioSink != NULL && mAudioDecoder == NULL) {
676 sp<MetaData> audioMeta = mSource->getFormatMeta(true /* audio */);
677 sp<AMessage> videoFormat = mSource->getFormat(false /* audio */);
678 audio_stream_type_t streamType = mAudioSink->getAudioStreamType();
Andy Hung202bce12014-12-03 11:47:36 -0800679 const bool hasVideo = (videoFormat != NULL);
680 const bool canOffload = canOffloadStream(
681 audioMeta, hasVideo, true /* is_streaming */, streamType);
Ronghua Wua10fd232014-11-06 16:15:20 -0800682 if (canOffload) {
683 if (!mOffloadAudio) {
684 mRenderer->signalEnableOffloadAudio();
685 }
Andy Hung282a7e32014-08-14 15:56:34 -0700686 // open audio sink early under offload mode.
687 sp<AMessage> format = mSource->getFormat(true /*audio*/);
Andy Hung202bce12014-12-03 11:47:36 -0800688 tryOpenAudioSinkForOffload(format, hasVideo);
Andy Hung282a7e32014-08-14 15:56:34 -0700689 }
Andreas Huber5bc087c2010-12-23 10:27:40 -0800690 instantiateDecoder(true, &mAudioDecoder);
Andreas Huberf9334412010-12-15 15:17:42 -0800691 }
692
Andreas Huberb7c8e912012-11-27 15:02:53 -0800693 if (!mHadAnySourcesBefore
694 && (mAudioDecoder != NULL || mVideoDecoder != NULL)) {
695 // This is the first time we've found anything playable.
696
Andreas Huber9575c962013-02-05 13:59:56 -0800697 if (mSourceFlags & Source::FLAG_DYNAMIC_DURATION) {
Andreas Huberb7c8e912012-11-27 15:02:53 -0800698 schedulePollDuration();
699 }
700 }
701
Andreas Hubereac68ba2011-09-27 12:12:25 -0700702 status_t err;
703 if ((err = mSource->feedMoreTSData()) != OK) {
Andreas Huber1aef2112011-01-04 14:01:29 -0800704 if (mAudioDecoder == NULL && mVideoDecoder == NULL) {
705 // We're not currently decoding anything (no audio or
706 // video tracks found) and we just ran out of input data.
Andreas Hubereac68ba2011-09-27 12:12:25 -0700707
708 if (err == ERROR_END_OF_STREAM) {
709 notifyListener(MEDIA_PLAYBACK_COMPLETE, 0, 0);
710 } else {
711 notifyListener(MEDIA_ERROR, MEDIA_ERROR_UNKNOWN, err);
712 }
Andreas Huber1aef2112011-01-04 14:01:29 -0800713 }
Andreas Huberf9334412010-12-15 15:17:42 -0800714 break;
715 }
716
Andreas Huberfbe9d812012-08-31 14:05:27 -0700717 if ((mAudioDecoder == NULL && mAudioSink != NULL)
718 || (mVideoDecoder == NULL && mNativeWindow != NULL)) {
Andreas Huberf9334412010-12-15 15:17:42 -0800719 msg->post(100000ll);
Andreas Huber5bc087c2010-12-23 10:27:40 -0800720 mScanSourcesPending = true;
Andreas Huberf9334412010-12-15 15:17:42 -0800721 }
722 break;
723 }
724
725 case kWhatVideoNotify:
726 case kWhatAudioNotify:
727 {
728 bool audio = msg->what() == kWhatAudioNotify;
729
Wei Jia88703c32014-08-06 11:24:07 -0700730 int32_t currentDecoderGeneration =
731 (audio? mAudioDecoderGeneration : mVideoDecoderGeneration);
732 int32_t requesterGeneration = currentDecoderGeneration - 1;
733 CHECK(msg->findInt32("generation", &requesterGeneration));
734
735 if (requesterGeneration != currentDecoderGeneration) {
736 ALOGV("got message from old %s decoder, generation(%d:%d)",
737 audio ? "audio" : "video", requesterGeneration,
738 currentDecoderGeneration);
739 sp<AMessage> reply;
740 if (!(msg->findMessage("reply", &reply))) {
741 return;
742 }
743
744 reply->setInt32("err", INFO_DISCONTINUITY);
745 reply->post();
746 return;
747 }
748
Andreas Huberf9334412010-12-15 15:17:42 -0800749 int32_t what;
Lajos Molnar1cd13982014-01-17 15:12:51 -0800750 CHECK(msg->findInt32("what", &what));
Andreas Huberf9334412010-12-15 15:17:42 -0800751
Chong Zhang7137ec72014-11-12 16:41:05 -0800752 if (what == DecoderBase::kWhatInputDiscontinuity) {
753 int32_t formatChange;
754 CHECK(msg->findInt32("formatChange", &formatChange));
Andreas Huberf9334412010-12-15 15:17:42 -0800755
Chong Zhang7137ec72014-11-12 16:41:05 -0800756 ALOGV("%s discontinuity: formatChange %d",
757 audio ? "audio" : "video", formatChange);
758
759 if (formatChange) {
760 mDeferredActions.push_back(
761 new FlushDecoderAction(
762 audio ? FLUSH_CMD_SHUTDOWN : FLUSH_CMD_NONE,
763 audio ? FLUSH_CMD_NONE : FLUSH_CMD_SHUTDOWN));
Andreas Huberf9334412010-12-15 15:17:42 -0800764 }
Chong Zhang7137ec72014-11-12 16:41:05 -0800765
766 mDeferredActions.push_back(
767 new SimpleAction(
768 &NuPlayer::performScanSources));
769
770 processDeferredActions();
771 } else if (what == DecoderBase::kWhatEOS) {
Andreas Huberdc9bacd2011-09-26 10:53:29 -0700772 int32_t err;
Lajos Molnar1cd13982014-01-17 15:12:51 -0800773 CHECK(msg->findInt32("err", &err));
Andreas Huberdc9bacd2011-09-26 10:53:29 -0700774
775 if (err == ERROR_END_OF_STREAM) {
Steve Block3856b092011-10-20 11:56:00 +0100776 ALOGV("got %s decoder EOS", audio ? "audio" : "video");
Andreas Huberdc9bacd2011-09-26 10:53:29 -0700777 } else {
Steve Block3856b092011-10-20 11:56:00 +0100778 ALOGV("got %s decoder EOS w/ error %d",
Andreas Huberdc9bacd2011-09-26 10:53:29 -0700779 audio ? "audio" : "video",
780 err);
781 }
782
783 mRenderer->queueEOS(audio, err);
Chong Zhang7137ec72014-11-12 16:41:05 -0800784 } else if (what == DecoderBase::kWhatFlushCompleted) {
Steve Block3856b092011-10-20 11:56:00 +0100785 ALOGV("decoder %s flush completed", audio ? "audio" : "video");
Andreas Huberf9334412010-12-15 15:17:42 -0800786
Andy Hung8d121d42014-10-03 09:53:53 -0700787 handleFlushComplete(audio, true /* isDecoder */);
Andreas Huber3831a062010-12-21 10:22:33 -0800788 finishFlushIfPossible();
Chong Zhang7137ec72014-11-12 16:41:05 -0800789 } else if (what == DecoderBase::kWhatVideoSizeChanged) {
Lajos Molnar1cd13982014-01-17 15:12:51 -0800790 sp<AMessage> format;
791 CHECK(msg->findMessage("format", &format));
792
Wei Jiac6cfd702014-11-11 16:33:20 -0800793 sp<AMessage> inputFormat =
794 mSource->getFormat(false /* audio */);
Andreas Huber3831a062010-12-21 10:22:33 -0800795
Wei Jiac6cfd702014-11-11 16:33:20 -0800796 updateVideoSize(inputFormat, format);
Chong Zhang7137ec72014-11-12 16:41:05 -0800797 } else if (what == DecoderBase::kWhatShutdownCompleted) {
Steve Block3856b092011-10-20 11:56:00 +0100798 ALOGV("%s shutdown completed", audio ? "audio" : "video");
Andreas Huber3831a062010-12-21 10:22:33 -0800799 if (audio) {
800 mAudioDecoder.clear();
Wei Jia57568df2014-09-22 10:16:29 -0700801 ++mAudioDecoderGeneration;
Andreas Huber3831a062010-12-21 10:22:33 -0800802
803 CHECK_EQ((int)mFlushingAudio, (int)SHUTTING_DOWN_DECODER);
804 mFlushingAudio = SHUT_DOWN;
805 } else {
806 mVideoDecoder.clear();
Wei Jia57568df2014-09-22 10:16:29 -0700807 ++mVideoDecoderGeneration;
Andreas Huber3831a062010-12-21 10:22:33 -0800808
809 CHECK_EQ((int)mFlushingVideo, (int)SHUTTING_DOWN_DECODER);
810 mFlushingVideo = SHUT_DOWN;
811 }
812
813 finishFlushIfPossible();
Chong Zhangf8d71772014-11-26 15:08:34 -0800814 } else if (what == DecoderBase::kWhatResumeCompleted) {
815 finishResume();
Chong Zhang7137ec72014-11-12 16:41:05 -0800816 } else if (what == DecoderBase::kWhatError) {
Chong Zhangf4c0a942014-08-11 15:14:10 -0700817 status_t err;
Andy Hung2abde2c2014-09-30 14:40:32 -0700818 if (!msg->findInt32("err", &err) || err == OK) {
Chong Zhangf4c0a942014-08-11 15:14:10 -0700819 err = UNKNOWN_ERROR;
820 }
Andy Hungcf31f1e2014-09-23 14:59:01 -0700821
Andy Hung2abde2c2014-09-30 14:40:32 -0700822 // Decoder errors can be due to Source (e.g. from streaming),
823 // or from decoding corrupted bitstreams, or from other decoder
824 // MediaCodec operations (e.g. from an ongoing reset or seek).
Andy Hung202bce12014-12-03 11:47:36 -0800825 // They may also be due to openAudioSink failure at
826 // decoder start or after a format change.
Andy Hung2abde2c2014-09-30 14:40:32 -0700827 //
828 // We try to gracefully shut down the affected decoder if possible,
829 // rather than trying to force the shutdown with something
830 // similar to performReset(). This method can lead to a hang
831 // if MediaCodec functions block after an error, but they should
832 // typically return INVALID_OPERATION instead of blocking.
833
834 FlushStatus *flushing = audio ? &mFlushingAudio : &mFlushingVideo;
835 ALOGE("received error(%#x) from %s decoder, flushing(%d), now shutting down",
836 err, audio ? "audio" : "video", *flushing);
837
838 switch (*flushing) {
839 case NONE:
840 mDeferredActions.push_back(
Wei Jiafef808d2014-10-31 17:57:05 -0700841 new FlushDecoderAction(
842 audio ? FLUSH_CMD_SHUTDOWN : FLUSH_CMD_NONE,
843 audio ? FLUSH_CMD_NONE : FLUSH_CMD_SHUTDOWN));
Andy Hung2abde2c2014-09-30 14:40:32 -0700844 processDeferredActions();
845 break;
846 case FLUSHING_DECODER:
847 *flushing = FLUSHING_DECODER_SHUTDOWN; // initiate shutdown after flush.
848 break; // Wait for flush to complete.
849 case FLUSHING_DECODER_SHUTDOWN:
850 break; // Wait for flush to complete.
851 case SHUTTING_DOWN_DECODER:
852 break; // Wait for shutdown to complete.
853 case FLUSHED:
854 // Widevine source reads must stop before releasing the video decoder.
855 if (!audio && mSource != NULL && mSourceFlags & Source::FLAG_SECURE) {
856 mSource->stop();
857 }
858 getDecoder(audio)->initiateShutdown(); // In the middle of a seek.
859 *flushing = SHUTTING_DOWN_DECODER; // Shut down.
860 break;
861 case SHUT_DOWN:
862 finishFlushIfPossible(); // Should not occur.
863 break; // Finish anyways.
Marco Nelissen9e2b7912014-08-18 16:13:03 -0700864 }
Andy Hung2abde2c2014-09-30 14:40:32 -0700865 notifyListener(MEDIA_ERROR, MEDIA_ERROR_UNKNOWN, err);
Lajos Molnar1cd13982014-01-17 15:12:51 -0800866 } else {
867 ALOGV("Unhandled decoder notification %d '%c%c%c%c'.",
Andreas Hubera1f8ab02012-11-30 10:53:22 -0800868 what,
869 what >> 24,
870 (what >> 16) & 0xff,
871 (what >> 8) & 0xff,
872 what & 0xff);
Andreas Huberf9334412010-12-15 15:17:42 -0800873 }
874
875 break;
876 }
877
878 case kWhatRendererNotify:
879 {
Wei Jia57568df2014-09-22 10:16:29 -0700880 int32_t requesterGeneration = mRendererGeneration - 1;
881 CHECK(msg->findInt32("generation", &requesterGeneration));
882 if (requesterGeneration != mRendererGeneration) {
883 ALOGV("got message from old renderer, generation(%d:%d)",
884 requesterGeneration, mRendererGeneration);
885 return;
886 }
887
Andreas Huberf9334412010-12-15 15:17:42 -0800888 int32_t what;
889 CHECK(msg->findInt32("what", &what));
890
891 if (what == Renderer::kWhatEOS) {
892 int32_t audio;
893 CHECK(msg->findInt32("audio", &audio));
894
Andreas Huberc92fd242011-08-16 13:48:44 -0700895 int32_t finalResult;
896 CHECK(msg->findInt32("finalResult", &finalResult));
897
Andreas Huberf9334412010-12-15 15:17:42 -0800898 if (audio) {
899 mAudioEOS = true;
900 } else {
901 mVideoEOS = true;
902 }
903
Andreas Huberc92fd242011-08-16 13:48:44 -0700904 if (finalResult == ERROR_END_OF_STREAM) {
Steve Block3856b092011-10-20 11:56:00 +0100905 ALOGV("reached %s EOS", audio ? "audio" : "video");
Andreas Huberc92fd242011-08-16 13:48:44 -0700906 } else {
Steve Block29357bc2012-01-06 19:20:56 +0000907 ALOGE("%s track encountered an error (%d)",
Andreas Huberc92fd242011-08-16 13:48:44 -0700908 audio ? "audio" : "video", finalResult);
909
910 notifyListener(
911 MEDIA_ERROR, MEDIA_ERROR_UNKNOWN, finalResult);
912 }
Andreas Huberf9334412010-12-15 15:17:42 -0800913
914 if ((mAudioEOS || mAudioDecoder == NULL)
915 && (mVideoEOS || mVideoDecoder == NULL)) {
916 notifyListener(MEDIA_PLAYBACK_COMPLETE, 0, 0);
917 }
Andreas Huber3fe62152011-09-16 15:09:22 -0700918 } else if (what == Renderer::kWhatFlushComplete) {
Andreas Huberf9334412010-12-15 15:17:42 -0800919 int32_t audio;
920 CHECK(msg->findInt32("audio", &audio));
921
Steve Block3856b092011-10-20 11:56:00 +0100922 ALOGV("renderer %s flush completed.", audio ? "audio" : "video");
Andy Hung8d121d42014-10-03 09:53:53 -0700923 handleFlushComplete(audio, false /* isDecoder */);
924 finishFlushIfPossible();
James Dongf57b4ea2012-07-20 13:38:36 -0700925 } else if (what == Renderer::kWhatVideoRenderingStart) {
926 notifyListener(MEDIA_INFO, MEDIA_INFO_RENDERING_START, 0);
Lajos Molnarcbaffcf2013-08-14 18:30:38 -0700927 } else if (what == Renderer::kWhatMediaRenderingStart) {
928 ALOGV("media rendering started");
929 notifyListener(MEDIA_STARTED, 0, 0);
Wei Jia3a2956d2014-07-22 16:01:33 -0700930 } else if (what == Renderer::kWhatAudioOffloadTearDown) {
Ronghua Wua10fd232014-11-06 16:15:20 -0800931 ALOGV("Tear down audio offload, fall back to s/w path if due to error.");
Wei Jia3a2956d2014-07-22 16:01:33 -0700932 int64_t positionUs;
933 CHECK(msg->findInt64("positionUs", &positionUs));
Ronghua Wu08529172014-10-02 16:55:52 -0700934 int32_t reason;
935 CHECK(msg->findInt32("reason", &reason));
Andy Hung282a7e32014-08-14 15:56:34 -0700936 closeAudioSink();
Wei Jia3a2956d2014-07-22 16:01:33 -0700937 mAudioDecoder.clear();
Wei Jia57568df2014-09-22 10:16:29 -0700938 ++mAudioDecoderGeneration;
Chong Zhang7137ec72014-11-12 16:41:05 -0800939 mRenderer->flush(
940 true /* audio */, false /* notifyComplete */);
Wei Jia3a2956d2014-07-22 16:01:33 -0700941 if (mVideoDecoder != NULL) {
Chong Zhang7137ec72014-11-12 16:41:05 -0800942 mRenderer->flush(
943 false /* audio */, false /* notifyComplete */);
Wei Jia3a2956d2014-07-22 16:01:33 -0700944 }
Wei Jia3a2956d2014-07-22 16:01:33 -0700945
Wei Jiae427abf2014-09-22 15:21:11 -0700946 performSeek(positionUs, false /* needNotify */);
Ronghua Wu08529172014-10-02 16:55:52 -0700947 if (reason == Renderer::kDueToError) {
Ronghua Wua10fd232014-11-06 16:15:20 -0800948 mRenderer->signalDisableOffloadAudio();
949 mOffloadAudio = false;
Ronghua Wu08529172014-10-02 16:55:52 -0700950 instantiateDecoder(true /* audio */, &mAudioDecoder);
951 }
Andreas Huberf9334412010-12-15 15:17:42 -0800952 }
953 break;
954 }
955
956 case kWhatMoreDataQueued:
957 {
958 break;
959 }
960
Andreas Huber1aef2112011-01-04 14:01:29 -0800961 case kWhatReset:
962 {
Steve Block3856b092011-10-20 11:56:00 +0100963 ALOGV("kWhatReset");
Andreas Huber1aef2112011-01-04 14:01:29 -0800964
Andreas Hubera1f8ab02012-11-30 10:53:22 -0800965 mDeferredActions.push_back(
Wei Jiafef808d2014-10-31 17:57:05 -0700966 new FlushDecoderAction(
967 FLUSH_CMD_SHUTDOWN /* audio */,
968 FLUSH_CMD_SHUTDOWN /* video */));
Andreas Huberb7c8e912012-11-27 15:02:53 -0800969
Andreas Hubera1f8ab02012-11-30 10:53:22 -0800970 mDeferredActions.push_back(
971 new SimpleAction(&NuPlayer::performReset));
Andreas Huberb58ce9f2011-11-28 16:27:35 -0800972
Andreas Hubera1f8ab02012-11-30 10:53:22 -0800973 processDeferredActions();
Andreas Huber1aef2112011-01-04 14:01:29 -0800974 break;
975 }
976
Andreas Huber43c3e6c2011-01-05 12:17:08 -0800977 case kWhatSeek:
978 {
979 int64_t seekTimeUs;
Wei Jiae427abf2014-09-22 15:21:11 -0700980 int32_t needNotify;
Andreas Huber43c3e6c2011-01-05 12:17:08 -0800981 CHECK(msg->findInt64("seekTimeUs", &seekTimeUs));
Wei Jiae427abf2014-09-22 15:21:11 -0700982 CHECK(msg->findInt32("needNotify", &needNotify));
Andreas Huber43c3e6c2011-01-05 12:17:08 -0800983
Wei Jiae427abf2014-09-22 15:21:11 -0700984 ALOGV("kWhatSeek seekTimeUs=%lld us, needNotify=%d",
Lajos Molnar6d339f12015-04-17 16:15:53 -0700985 (long long)seekTimeUs, needNotify);
Andreas Huber43c3e6c2011-01-05 12:17:08 -0800986
Andreas Hubera1f8ab02012-11-30 10:53:22 -0800987 mDeferredActions.push_back(
Wei Jiafef808d2014-10-31 17:57:05 -0700988 new FlushDecoderAction(FLUSH_CMD_FLUSH /* audio */,
989 FLUSH_CMD_FLUSH /* video */));
Andreas Huber43c3e6c2011-01-05 12:17:08 -0800990
Wei Jiae427abf2014-09-22 15:21:11 -0700991 mDeferredActions.push_back(
992 new SeekAction(seekTimeUs, needNotify));
Andreas Huber43c3e6c2011-01-05 12:17:08 -0800993
Chong Zhangf8d71772014-11-26 15:08:34 -0800994 // After a flush without shutdown, decoder is paused.
995 // Don't resume it until source seek is done, otherwise it could
Chong Zhang7137ec72014-11-12 16:41:05 -0800996 // start pulling stale data too soon.
997 mDeferredActions.push_back(
Chong Zhangf8d71772014-11-26 15:08:34 -0800998 new ResumeDecoderAction(needNotify));
Chong Zhang7137ec72014-11-12 16:41:05 -0800999
Andreas Hubera1f8ab02012-11-30 10:53:22 -08001000 processDeferredActions();
Andreas Huber43c3e6c2011-01-05 12:17:08 -08001001 break;
1002 }
1003
Andreas Huberb4082222011-01-20 15:23:04 -08001004 case kWhatPause:
1005 {
Chong Zhangefbb6192015-01-30 17:13:27 -08001006 onPause();
1007 mPausedByClient = true;
Andreas Huberb4082222011-01-20 15:23:04 -08001008 break;
1009 }
1010
Andreas Huberb5f25f02013-02-05 10:14:26 -08001011 case kWhatSourceNotify:
1012 {
Andreas Huber9575c962013-02-05 13:59:56 -08001013 onSourceNotify(msg);
Andreas Huberb5f25f02013-02-05 10:14:26 -08001014 break;
1015 }
1016
Chong Zhanga7fa1d92014-06-11 14:49:23 -07001017 case kWhatClosedCaptionNotify:
1018 {
1019 onClosedCaptionNotify(msg);
1020 break;
1021 }
1022
Andreas Huberf9334412010-12-15 15:17:42 -08001023 default:
1024 TRESPASS();
1025 break;
1026 }
1027}
1028
Wei Jia94211742014-10-28 17:09:06 -07001029void NuPlayer::onResume() {
Chong Zhangefbb6192015-01-30 17:13:27 -08001030 if (!mPaused) {
1031 return;
1032 }
1033 mPaused = false;
Wei Jia94211742014-10-28 17:09:06 -07001034 if (mSource != NULL) {
1035 mSource->resume();
1036 } else {
1037 ALOGW("resume called when source is gone or not set");
1038 }
1039 // |mAudioDecoder| may have been released due to the pause timeout, so re-create it if
1040 // needed.
1041 if (audioDecoderStillNeeded() && mAudioDecoder == NULL) {
1042 instantiateDecoder(true /* audio */, &mAudioDecoder);
1043 }
1044 if (mRenderer != NULL) {
1045 mRenderer->resume();
1046 } else {
1047 ALOGW("resume called when renderer is gone or not set");
1048 }
1049}
1050
Lajos Molnarfcd3e942015-03-31 10:06:48 -07001051status_t NuPlayer::onInstantiateSecureDecoders() {
1052 status_t err;
1053 if (!(mSourceFlags & Source::FLAG_SECURE)) {
1054 return BAD_TYPE;
1055 }
1056
1057 if (mRenderer != NULL) {
1058 ALOGE("renderer should not be set when instantiating secure decoders");
1059 return UNKNOWN_ERROR;
1060 }
1061
1062 // TRICKY: We rely on mRenderer being null, so that decoder does not start requesting
1063 // data on instantiation.
1064 if (mNativeWindow != NULL) {
1065 err = instantiateDecoder(false, &mVideoDecoder);
1066 if (err != OK) {
1067 return err;
1068 }
1069 }
1070
1071 if (mAudioSink != NULL) {
1072 err = instantiateDecoder(true, &mAudioDecoder);
1073 if (err != OK) {
1074 return err;
1075 }
1076 }
1077 return OK;
1078}
1079
Wei Jia94211742014-10-28 17:09:06 -07001080void NuPlayer::onStart() {
Wei Jia94211742014-10-28 17:09:06 -07001081 mOffloadAudio = false;
1082 mAudioEOS = false;
1083 mVideoEOS = false;
Wei Jia94211742014-10-28 17:09:06 -07001084 mStarted = true;
1085
Wei Jia94211742014-10-28 17:09:06 -07001086 mSource->start();
1087
1088 uint32_t flags = 0;
1089
1090 if (mSource->isRealTime()) {
1091 flags |= Renderer::FLAG_REAL_TIME;
1092 }
1093
1094 sp<MetaData> audioMeta = mSource->getFormatMeta(true /* audio */);
1095 audio_stream_type_t streamType = AUDIO_STREAM_MUSIC;
1096 if (mAudioSink != NULL) {
1097 streamType = mAudioSink->getAudioStreamType();
1098 }
1099
1100 sp<AMessage> videoFormat = mSource->getFormat(false /* audio */);
1101
1102 mOffloadAudio =
1103 canOffloadStream(audioMeta, (videoFormat != NULL),
1104 true /* is_streaming */, streamType);
1105 if (mOffloadAudio) {
1106 flags |= Renderer::FLAG_OFFLOAD_AUDIO;
1107 }
1108
Lajos Molnar1d15ab52015-03-04 16:46:34 -08001109 sp<AMessage> notify = new AMessage(kWhatRendererNotify, this);
Wei Jia94211742014-10-28 17:09:06 -07001110 ++mRendererGeneration;
1111 notify->setInt32("generation", mRendererGeneration);
1112 mRenderer = new Renderer(mAudioSink, notify, flags);
Wei Jia94211742014-10-28 17:09:06 -07001113 mRendererLooper = new ALooper;
1114 mRendererLooper->setName("NuPlayerRenderer");
1115 mRendererLooper->start(false, false, ANDROID_PRIORITY_AUDIO);
1116 mRendererLooper->registerHandler(mRenderer);
Wei Jiac8206ff2015-03-04 13:59:37 -08001117 if (mPlaybackRate != 1.0) {
1118 mRenderer->setPlaybackRate(mPlaybackRate);
1119 }
Wei Jia94211742014-10-28 17:09:06 -07001120
1121 sp<MetaData> meta = getFileMeta();
1122 int32_t rate;
1123 if (meta != NULL
1124 && meta->findInt32(kKeyFrameRate, &rate) && rate > 0) {
1125 mRenderer->setVideoFrameRate(rate);
1126 }
1127
Wei Jiac6cfd702014-11-11 16:33:20 -08001128 if (mVideoDecoder != NULL) {
1129 mVideoDecoder->setRenderer(mRenderer);
1130 }
1131 if (mAudioDecoder != NULL) {
1132 mAudioDecoder->setRenderer(mRenderer);
1133 }
1134
Wei Jia94211742014-10-28 17:09:06 -07001135 postScanSources();
1136}
1137
Chong Zhangefbb6192015-01-30 17:13:27 -08001138void NuPlayer::onPause() {
1139 if (mPaused) {
1140 return;
1141 }
1142 mPaused = true;
1143 if (mSource != NULL) {
1144 mSource->pause();
1145 } else {
1146 ALOGW("pause called when source is gone or not set");
1147 }
1148 if (mRenderer != NULL) {
1149 mRenderer->pause();
1150 } else {
1151 ALOGW("pause called when renderer is gone or not set");
1152 }
1153}
1154
Ronghua Wud7988b12014-10-03 15:19:10 -07001155bool NuPlayer::audioDecoderStillNeeded() {
1156 // Audio decoder is no longer needed if it's in shut/shutting down status.
1157 return ((mFlushingAudio != SHUT_DOWN) && (mFlushingAudio != SHUTTING_DOWN_DECODER));
1158}
1159
Andy Hung8d121d42014-10-03 09:53:53 -07001160void NuPlayer::handleFlushComplete(bool audio, bool isDecoder) {
1161 // We wait for both the decoder flush and the renderer flush to complete
1162 // before entering either the FLUSHED or the SHUTTING_DOWN_DECODER state.
1163
1164 mFlushComplete[audio][isDecoder] = true;
1165 if (!mFlushComplete[audio][!isDecoder]) {
1166 return;
1167 }
1168
1169 FlushStatus *state = audio ? &mFlushingAudio : &mFlushingVideo;
1170 switch (*state) {
1171 case FLUSHING_DECODER:
1172 {
1173 *state = FLUSHED;
Andy Hung8d121d42014-10-03 09:53:53 -07001174 break;
1175 }
1176
1177 case FLUSHING_DECODER_SHUTDOWN:
1178 {
1179 *state = SHUTTING_DOWN_DECODER;
1180
1181 ALOGV("initiating %s decoder shutdown", audio ? "audio" : "video");
1182 if (!audio) {
Andy Hung8d121d42014-10-03 09:53:53 -07001183 // Widevine source reads must stop before releasing the video decoder.
1184 if (mSource != NULL && mSourceFlags & Source::FLAG_SECURE) {
1185 mSource->stop();
1186 }
1187 }
1188 getDecoder(audio)->initiateShutdown();
1189 break;
1190 }
1191
1192 default:
1193 // decoder flush completes only occur in a flushing state.
1194 LOG_ALWAYS_FATAL_IF(isDecoder, "decoder flush in invalid state %d", *state);
1195 break;
1196 }
1197}
1198
Andreas Huber3831a062010-12-21 10:22:33 -08001199void NuPlayer::finishFlushIfPossible() {
Wei Jia53904f32014-07-29 10:22:53 -07001200 if (mFlushingAudio != NONE && mFlushingAudio != FLUSHED
1201 && mFlushingAudio != SHUT_DOWN) {
Andreas Huber3831a062010-12-21 10:22:33 -08001202 return;
1203 }
1204
Wei Jia53904f32014-07-29 10:22:53 -07001205 if (mFlushingVideo != NONE && mFlushingVideo != FLUSHED
1206 && mFlushingVideo != SHUT_DOWN) {
Andreas Huber3831a062010-12-21 10:22:33 -08001207 return;
1208 }
1209
Steve Block3856b092011-10-20 11:56:00 +01001210 ALOGV("both audio and video are flushed now.");
Andreas Huber3831a062010-12-21 10:22:33 -08001211
Andreas Huber3831a062010-12-21 10:22:33 -08001212 mFlushingAudio = NONE;
1213 mFlushingVideo = NONE;
Andreas Huber3831a062010-12-21 10:22:33 -08001214
Andy Hung8d121d42014-10-03 09:53:53 -07001215 clearFlushComplete();
1216
Andreas Hubera1f8ab02012-11-30 10:53:22 -08001217 processDeferredActions();
Andreas Huber1aef2112011-01-04 14:01:29 -08001218}
1219
1220void NuPlayer::postScanSources() {
1221 if (mScanSourcesPending) {
1222 return;
1223 }
1224
Lajos Molnar1d15ab52015-03-04 16:46:34 -08001225 sp<AMessage> msg = new AMessage(kWhatScanSources, this);
Andreas Huber1aef2112011-01-04 14:01:29 -08001226 msg->setInt32("generation", mScanSourcesGeneration);
1227 msg->post();
1228
1229 mScanSourcesPending = true;
1230}
1231
Andy Hung202bce12014-12-03 11:47:36 -08001232void NuPlayer::tryOpenAudioSinkForOffload(const sp<AMessage> &format, bool hasVideo) {
1233 // Note: This is called early in NuPlayer to determine whether offloading
1234 // is possible; otherwise the decoders call the renderer openAudioSink directly.
Andy Hung282a7e32014-08-14 15:56:34 -07001235
Andy Hung202bce12014-12-03 11:47:36 -08001236 status_t err = mRenderer->openAudioSink(
1237 format, true /* offloadOnly */, hasVideo, AUDIO_OUTPUT_FLAG_NONE, &mOffloadAudio);
1238 if (err != OK) {
1239 // Any failure we turn off mOffloadAudio.
1240 mOffloadAudio = false;
1241 } else if (mOffloadAudio) {
Chong Zhang3b9eb1f2014-10-15 17:05:08 -07001242 sp<MetaData> audioMeta =
1243 mSource->getFormatMeta(true /* audio */);
1244 sendMetaDataToHal(mAudioSink, audioMeta);
Andy Hung282a7e32014-08-14 15:56:34 -07001245 }
1246}
1247
1248void NuPlayer::closeAudioSink() {
Chong Zhang3b9eb1f2014-10-15 17:05:08 -07001249 mRenderer->closeAudioSink();
Andy Hung282a7e32014-08-14 15:56:34 -07001250}
1251
Chong Zhang7137ec72014-11-12 16:41:05 -08001252status_t NuPlayer::instantiateDecoder(bool audio, sp<DecoderBase> *decoder) {
Andreas Huberf9334412010-12-15 15:17:42 -08001253 if (*decoder != NULL) {
1254 return OK;
1255 }
1256
Andreas Huber84066782011-08-16 09:34:26 -07001257 sp<AMessage> format = mSource->getFormat(audio);
Andreas Huberf9334412010-12-15 15:17:42 -08001258
Andreas Huber84066782011-08-16 09:34:26 -07001259 if (format == NULL) {
Andreas Huberf9334412010-12-15 15:17:42 -08001260 return -EWOULDBLOCK;
1261 }
1262
Ronghua Wu8db88132015-04-22 13:51:35 -07001263 format->setInt32("priority", 0 /* realtime */);
1264
Andreas Huber3fe62152011-09-16 15:09:22 -07001265 if (!audio) {
Andreas Huber84066782011-08-16 09:34:26 -07001266 AString mime;
1267 CHECK(format->findString("mime", &mime));
Chong Zhanga7fa1d92014-06-11 14:49:23 -07001268
Lajos Molnar1d15ab52015-03-04 16:46:34 -08001269 sp<AMessage> ccNotify = new AMessage(kWhatClosedCaptionNotify, this);
Chong Zhang341ab6e2015-02-04 13:37:18 -08001270 if (mCCDecoder == NULL) {
1271 mCCDecoder = new CCDecoder(ccNotify);
1272 }
Lajos Molnar09524832014-07-17 14:29:51 -07001273
1274 if (mSourceFlags & Source::FLAG_SECURE) {
1275 format->setInt32("secure", true);
1276 }
Chong Zhang17134602015-01-07 16:14:34 -08001277
1278 if (mSourceFlags & Source::FLAG_PROTECTED) {
1279 format->setInt32("protected", true);
1280 }
Ronghua Wu8db88132015-04-22 13:51:35 -07001281
1282 sp<MetaData> meta = getFileMeta();
1283 int32_t rate;
1284 if (meta != NULL && meta->findInt32(kKeyFrameRate, &rate) && rate > 0) {
1285 format->setFloat("operating-rate", rate * mPlaybackRate);
1286 }
Andreas Huber3fe62152011-09-16 15:09:22 -07001287 }
1288
Wei Jiabc2fb722014-07-08 16:37:57 -07001289 if (audio) {
Lajos Molnar1d15ab52015-03-04 16:46:34 -08001290 sp<AMessage> notify = new AMessage(kWhatAudioNotify, this);
Wei Jia88703c32014-08-06 11:24:07 -07001291 ++mAudioDecoderGeneration;
1292 notify->setInt32("generation", mAudioDecoderGeneration);
1293
Wei Jiabc2fb722014-07-08 16:37:57 -07001294 if (mOffloadAudio) {
Haynes Mathew George8b635332015-03-30 17:59:47 -07001295 const bool hasVideo = (mSource->getFormat(false /*audio */) != NULL);
1296 format->setInt32("has-video", hasVideo);
Wei Jiac6cfd702014-11-11 16:33:20 -08001297 *decoder = new DecoderPassThrough(notify, mSource, mRenderer);
Wei Jiabc2fb722014-07-08 16:37:57 -07001298 } else {
Wei Jiac6cfd702014-11-11 16:33:20 -08001299 *decoder = new Decoder(notify, mSource, mRenderer);
Wei Jiabc2fb722014-07-08 16:37:57 -07001300 }
1301 } else {
Lajos Molnar1d15ab52015-03-04 16:46:34 -08001302 sp<AMessage> notify = new AMessage(kWhatVideoNotify, this);
Wei Jia88703c32014-08-06 11:24:07 -07001303 ++mVideoDecoderGeneration;
1304 notify->setInt32("generation", mVideoDecoderGeneration);
1305
Chong Zhang7137ec72014-11-12 16:41:05 -08001306 *decoder = new Decoder(
1307 notify, mSource, mRenderer, mNativeWindow, mCCDecoder);
Lajos Molnard9fd6312014-11-06 11:00:00 -08001308
1309 // enable FRC if high-quality AV sync is requested, even if not
1310 // queuing to native window, as this will even improve textureview
1311 // playback.
1312 {
1313 char value[PROPERTY_VALUE_MAX];
1314 if (property_get("persist.sys.media.avsync", value, NULL) &&
1315 (!strcmp("1", value) || !strcasecmp("true", value))) {
1316 format->setInt32("auto-frc", 1);
1317 }
1318 }
Wei Jiabc2fb722014-07-08 16:37:57 -07001319 }
Lajos Molnar1cd13982014-01-17 15:12:51 -08001320 (*decoder)->init();
Andreas Huber84066782011-08-16 09:34:26 -07001321 (*decoder)->configure(format);
Andreas Huberf9334412010-12-15 15:17:42 -08001322
Lajos Molnar09524832014-07-17 14:29:51 -07001323 // allocate buffers to decrypt widevine source buffers
1324 if (!audio && (mSourceFlags & Source::FLAG_SECURE)) {
1325 Vector<sp<ABuffer> > inputBufs;
1326 CHECK_EQ((*decoder)->getInputBuffers(&inputBufs), (status_t)OK);
1327
1328 Vector<MediaBuffer *> mediaBufs;
1329 for (size_t i = 0; i < inputBufs.size(); i++) {
1330 const sp<ABuffer> &buffer = inputBufs[i];
1331 MediaBuffer *mbuf = new MediaBuffer(buffer->data(), buffer->size());
1332 mediaBufs.push(mbuf);
1333 }
1334
1335 status_t err = mSource->setBuffers(audio, mediaBufs);
1336 if (err != OK) {
1337 for (size_t i = 0; i < mediaBufs.size(); ++i) {
1338 mediaBufs[i]->release();
1339 }
1340 mediaBufs.clear();
1341 ALOGE("Secure source didn't support secure mediaBufs.");
1342 return err;
1343 }
1344 }
Andreas Huberf9334412010-12-15 15:17:42 -08001345 return OK;
1346}
1347
Chong Zhangced1c2f2014-08-08 15:22:35 -07001348void NuPlayer::updateVideoSize(
1349 const sp<AMessage> &inputFormat,
1350 const sp<AMessage> &outputFormat) {
1351 if (inputFormat == NULL) {
1352 ALOGW("Unknown video size, reporting 0x0!");
1353 notifyListener(MEDIA_SET_VIDEO_SIZE, 0, 0);
1354 return;
1355 }
1356
1357 int32_t displayWidth, displayHeight;
Chong Zhangced1c2f2014-08-08 15:22:35 -07001358 if (outputFormat != NULL) {
1359 int32_t width, height;
1360 CHECK(outputFormat->findInt32("width", &width));
1361 CHECK(outputFormat->findInt32("height", &height));
1362
1363 int32_t cropLeft, cropTop, cropRight, cropBottom;
1364 CHECK(outputFormat->findRect(
1365 "crop",
1366 &cropLeft, &cropTop, &cropRight, &cropBottom));
1367
1368 displayWidth = cropRight - cropLeft + 1;
1369 displayHeight = cropBottom - cropTop + 1;
1370
1371 ALOGV("Video output format changed to %d x %d "
1372 "(crop: %d x %d @ (%d, %d))",
1373 width, height,
1374 displayWidth,
1375 displayHeight,
1376 cropLeft, cropTop);
1377 } else {
1378 CHECK(inputFormat->findInt32("width", &displayWidth));
1379 CHECK(inputFormat->findInt32("height", &displayHeight));
1380
1381 ALOGV("Video input format %d x %d", displayWidth, displayHeight);
1382 }
1383
1384 // Take into account sample aspect ratio if necessary:
1385 int32_t sarWidth, sarHeight;
1386 if (inputFormat->findInt32("sar-width", &sarWidth)
1387 && inputFormat->findInt32("sar-height", &sarHeight)) {
1388 ALOGV("Sample aspect ratio %d : %d", sarWidth, sarHeight);
1389
1390 displayWidth = (displayWidth * sarWidth) / sarHeight;
1391
1392 ALOGV("display dimensions %d x %d", displayWidth, displayHeight);
1393 }
1394
1395 int32_t rotationDegrees;
1396 if (!inputFormat->findInt32("rotation-degrees", &rotationDegrees)) {
1397 rotationDegrees = 0;
1398 }
1399
1400 if (rotationDegrees == 90 || rotationDegrees == 270) {
1401 int32_t tmp = displayWidth;
1402 displayWidth = displayHeight;
1403 displayHeight = tmp;
1404 }
1405
1406 notifyListener(
1407 MEDIA_SET_VIDEO_SIZE,
1408 displayWidth,
1409 displayHeight);
1410}
1411
Chong Zhangdcb89b32013-08-06 09:44:47 -07001412void NuPlayer::notifyListener(int msg, int ext1, int ext2, const Parcel *in) {
Andreas Huber43c3e6c2011-01-05 12:17:08 -08001413 if (mDriver == NULL) {
Andreas Huberf9334412010-12-15 15:17:42 -08001414 return;
1415 }
1416
Andreas Huber43c3e6c2011-01-05 12:17:08 -08001417 sp<NuPlayerDriver> driver = mDriver.promote();
Andreas Huberf9334412010-12-15 15:17:42 -08001418
Andreas Huber43c3e6c2011-01-05 12:17:08 -08001419 if (driver == NULL) {
Andreas Huberf9334412010-12-15 15:17:42 -08001420 return;
1421 }
1422
Chong Zhangdcb89b32013-08-06 09:44:47 -07001423 driver->notifyListener(msg, ext1, ext2, in);
Andreas Huberf9334412010-12-15 15:17:42 -08001424}
1425
Chong Zhang7137ec72014-11-12 16:41:05 -08001426void NuPlayer::flushDecoder(bool audio, bool needShutdown) {
Andreas Huber14f76722013-01-15 09:04:18 -08001427 ALOGV("[%s] flushDecoder needShutdown=%d",
1428 audio ? "audio" : "video", needShutdown);
1429
Chong Zhang7137ec72014-11-12 16:41:05 -08001430 const sp<DecoderBase> &decoder = getDecoder(audio);
Lajos Molnar87603c02014-08-20 19:25:30 -07001431 if (decoder == NULL) {
Steve Blockdf64d152012-01-04 20:05:49 +00001432 ALOGI("flushDecoder %s without decoder present",
Andreas Huber6e3d3112011-11-28 12:36:11 -08001433 audio ? "audio" : "video");
Lajos Molnar87603c02014-08-20 19:25:30 -07001434 return;
Andreas Huber6e3d3112011-11-28 12:36:11 -08001435 }
1436
Andreas Huber1aef2112011-01-04 14:01:29 -08001437 // Make sure we don't continue to scan sources until we finish flushing.
1438 ++mScanSourcesGeneration;
Chong Zhang3b032b32015-04-17 15:49:06 -07001439 if (mScanSourcesPending) {
1440 mDeferredActions.push_back(
1441 new SimpleAction(&NuPlayer::performScanSources));
1442 mScanSourcesPending = false;
1443 }
Andreas Huber1aef2112011-01-04 14:01:29 -08001444
Chong Zhang7137ec72014-11-12 16:41:05 -08001445 decoder->signalFlush();
Andreas Huber1aef2112011-01-04 14:01:29 -08001446
1447 FlushStatus newStatus =
1448 needShutdown ? FLUSHING_DECODER_SHUTDOWN : FLUSHING_DECODER;
1449
Lajos Molnarfcd3e942015-03-31 10:06:48 -07001450 mFlushComplete[audio][false /* isDecoder */] = (mRenderer == NULL);
Andy Hung8d121d42014-10-03 09:53:53 -07001451 mFlushComplete[audio][true /* isDecoder */] = false;
Andreas Huber1aef2112011-01-04 14:01:29 -08001452 if (audio) {
Wei Jia53904f32014-07-29 10:22:53 -07001453 ALOGE_IF(mFlushingAudio != NONE,
1454 "audio flushDecoder() is called in state %d", mFlushingAudio);
Andreas Huber1aef2112011-01-04 14:01:29 -08001455 mFlushingAudio = newStatus;
Andreas Huber1aef2112011-01-04 14:01:29 -08001456 } else {
Wei Jia53904f32014-07-29 10:22:53 -07001457 ALOGE_IF(mFlushingVideo != NONE,
1458 "video flushDecoder() is called in state %d", mFlushingVideo);
Andreas Huber1aef2112011-01-04 14:01:29 -08001459 mFlushingVideo = newStatus;
Andreas Huber1aef2112011-01-04 14:01:29 -08001460 }
1461}
1462
Chong Zhangced1c2f2014-08-08 15:22:35 -07001463void NuPlayer::queueDecoderShutdown(
1464 bool audio, bool video, const sp<AMessage> &reply) {
1465 ALOGI("queueDecoderShutdown audio=%d, video=%d", audio, video);
Andreas Huber84066782011-08-16 09:34:26 -07001466
Chong Zhangced1c2f2014-08-08 15:22:35 -07001467 mDeferredActions.push_back(
Wei Jiafef808d2014-10-31 17:57:05 -07001468 new FlushDecoderAction(
1469 audio ? FLUSH_CMD_SHUTDOWN : FLUSH_CMD_NONE,
1470 video ? FLUSH_CMD_SHUTDOWN : FLUSH_CMD_NONE));
Andreas Huber84066782011-08-16 09:34:26 -07001471
Chong Zhangced1c2f2014-08-08 15:22:35 -07001472 mDeferredActions.push_back(
1473 new SimpleAction(&NuPlayer::performScanSources));
Andreas Huber84066782011-08-16 09:34:26 -07001474
Chong Zhangced1c2f2014-08-08 15:22:35 -07001475 mDeferredActions.push_back(new PostMessageAction(reply));
1476
1477 processDeferredActions();
Andreas Huber84066782011-08-16 09:34:26 -07001478}
1479
James Dong0d268a32012-08-31 12:18:27 -07001480status_t NuPlayer::setVideoScalingMode(int32_t mode) {
1481 mVideoScalingMode = mode;
Andreas Huber57a339c2012-12-03 11:18:00 -08001482 if (mNativeWindow != NULL) {
James Dong0d268a32012-08-31 12:18:27 -07001483 status_t ret = native_window_set_scaling_mode(
1484 mNativeWindow->getNativeWindow().get(), mVideoScalingMode);
1485 if (ret != OK) {
1486 ALOGE("Failed to set scaling mode (%d): %s",
1487 -ret, strerror(-ret));
1488 return ret;
1489 }
1490 }
1491 return OK;
1492}
1493
Chong Zhangdcb89b32013-08-06 09:44:47 -07001494status_t NuPlayer::getTrackInfo(Parcel* reply) const {
Lajos Molnar1d15ab52015-03-04 16:46:34 -08001495 sp<AMessage> msg = new AMessage(kWhatGetTrackInfo, this);
Chong Zhangdcb89b32013-08-06 09:44:47 -07001496 msg->setPointer("reply", reply);
1497
1498 sp<AMessage> response;
1499 status_t err = msg->postAndAwaitResponse(&response);
1500 return err;
1501}
1502
Robert Shih7c4f0d72014-07-09 18:53:31 -07001503status_t NuPlayer::getSelectedTrack(int32_t type, Parcel* reply) const {
Lajos Molnar1d15ab52015-03-04 16:46:34 -08001504 sp<AMessage> msg = new AMessage(kWhatGetSelectedTrack, this);
Robert Shih7c4f0d72014-07-09 18:53:31 -07001505 msg->setPointer("reply", reply);
1506 msg->setInt32("type", type);
1507
1508 sp<AMessage> response;
1509 status_t err = msg->postAndAwaitResponse(&response);
1510 if (err == OK && response != NULL) {
1511 CHECK(response->findInt32("err", &err));
1512 }
1513 return err;
1514}
1515
Robert Shih6ffb1fd2014-10-29 16:24:32 -07001516status_t NuPlayer::selectTrack(size_t trackIndex, bool select, int64_t timeUs) {
Lajos Molnar1d15ab52015-03-04 16:46:34 -08001517 sp<AMessage> msg = new AMessage(kWhatSelectTrack, this);
Chong Zhangdcb89b32013-08-06 09:44:47 -07001518 msg->setSize("trackIndex", trackIndex);
1519 msg->setInt32("select", select);
Robert Shih6ffb1fd2014-10-29 16:24:32 -07001520 msg->setInt64("timeUs", timeUs);
Chong Zhangdcb89b32013-08-06 09:44:47 -07001521
1522 sp<AMessage> response;
1523 status_t err = msg->postAndAwaitResponse(&response);
1524
Chong Zhang404fced2014-06-11 14:45:31 -07001525 if (err != OK) {
1526 return err;
1527 }
1528
1529 if (!response->findInt32("err", &err)) {
1530 err = OK;
1531 }
1532
Chong Zhangdcb89b32013-08-06 09:44:47 -07001533 return err;
1534}
1535
Ronghua Wua73d9e02014-10-08 15:13:29 -07001536status_t NuPlayer::getCurrentPosition(int64_t *mediaUs) {
1537 sp<Renderer> renderer = mRenderer;
1538 if (renderer == NULL) {
1539 return NO_INIT;
1540 }
1541
1542 return renderer->getCurrentPosition(mediaUs);
1543}
1544
1545void NuPlayer::getStats(int64_t *numFramesTotal, int64_t *numFramesDropped) {
Chong Zhang7137ec72014-11-12 16:41:05 -08001546 sp<DecoderBase> decoder = getDecoder(false /* audio */);
1547 if (decoder != NULL) {
1548 decoder->getStats(numFramesTotal, numFramesDropped);
1549 } else {
1550 *numFramesTotal = 0;
1551 *numFramesDropped = 0;
1552 }
Ronghua Wua73d9e02014-10-08 15:13:29 -07001553}
1554
Marco Nelissenf0b72b52014-09-16 15:43:44 -07001555sp<MetaData> NuPlayer::getFileMeta() {
1556 return mSource->getFileFormatMeta();
1557}
1558
Andreas Huberb7c8e912012-11-27 15:02:53 -08001559void NuPlayer::schedulePollDuration() {
Lajos Molnar1d15ab52015-03-04 16:46:34 -08001560 sp<AMessage> msg = new AMessage(kWhatPollDuration, this);
Andreas Huberb7c8e912012-11-27 15:02:53 -08001561 msg->setInt32("generation", mPollDurationGeneration);
1562 msg->post();
1563}
1564
1565void NuPlayer::cancelPollDuration() {
1566 ++mPollDurationGeneration;
1567}
1568
Andreas Hubera1f8ab02012-11-30 10:53:22 -08001569void NuPlayer::processDeferredActions() {
1570 while (!mDeferredActions.empty()) {
1571 // We won't execute any deferred actions until we're no longer in
1572 // an intermediate state, i.e. one more more decoders are currently
1573 // flushing or shutting down.
1574
Andreas Hubera1f8ab02012-11-30 10:53:22 -08001575 if (mFlushingAudio != NONE || mFlushingVideo != NONE) {
1576 // We're currently flushing, postpone the reset until that's
1577 // completed.
1578
1579 ALOGV("postponing action mFlushingAudio=%d, mFlushingVideo=%d",
1580 mFlushingAudio, mFlushingVideo);
1581
1582 break;
1583 }
1584
1585 sp<Action> action = *mDeferredActions.begin();
1586 mDeferredActions.erase(mDeferredActions.begin());
1587
1588 action->execute(this);
1589 }
1590}
1591
Wei Jiae427abf2014-09-22 15:21:11 -07001592void NuPlayer::performSeek(int64_t seekTimeUs, bool needNotify) {
1593 ALOGV("performSeek seekTimeUs=%lld us (%.2f secs), needNotify(%d)",
Lajos Molnar6d339f12015-04-17 16:15:53 -07001594 (long long)seekTimeUs,
Wei Jiae427abf2014-09-22 15:21:11 -07001595 seekTimeUs / 1E6,
1596 needNotify);
Andreas Hubera1f8ab02012-11-30 10:53:22 -08001597
Andy Hungadf34bf2014-09-03 18:22:22 -07001598 if (mSource == NULL) {
1599 // This happens when reset occurs right before the loop mode
1600 // asynchronously seeks to the start of the stream.
1601 LOG_ALWAYS_FATAL_IF(mAudioDecoder != NULL || mVideoDecoder != NULL,
1602 "mSource is NULL and decoders not NULL audio(%p) video(%p)",
1603 mAudioDecoder.get(), mVideoDecoder.get());
1604 return;
1605 }
Andreas Hubera1f8ab02012-11-30 10:53:22 -08001606 mSource->seekTo(seekTimeUs);
Robert Shihd3b0bbb2014-07-23 15:00:25 -07001607 ++mTimedTextGeneration;
Andreas Hubera1f8ab02012-11-30 10:53:22 -08001608
Andreas Hubera1f8ab02012-11-30 10:53:22 -08001609 // everything's flushed, continue playback.
1610}
1611
Wei Jiafef808d2014-10-31 17:57:05 -07001612void NuPlayer::performDecoderFlush(FlushCommand audio, FlushCommand video) {
1613 ALOGV("performDecoderFlush audio=%d, video=%d", audio, video);
Andreas Hubera1f8ab02012-11-30 10:53:22 -08001614
Wei Jiafef808d2014-10-31 17:57:05 -07001615 if ((audio == FLUSH_CMD_NONE || mAudioDecoder == NULL)
1616 && (video == FLUSH_CMD_NONE || mVideoDecoder == NULL)) {
Andreas Hubera1f8ab02012-11-30 10:53:22 -08001617 return;
1618 }
1619
Wei Jiafef808d2014-10-31 17:57:05 -07001620 if (audio != FLUSH_CMD_NONE && mAudioDecoder != NULL) {
1621 flushDecoder(true /* audio */, (audio == FLUSH_CMD_SHUTDOWN));
Andreas Hubera1f8ab02012-11-30 10:53:22 -08001622 }
1623
Wei Jiafef808d2014-10-31 17:57:05 -07001624 if (video != FLUSH_CMD_NONE && mVideoDecoder != NULL) {
1625 flushDecoder(false /* audio */, (video == FLUSH_CMD_SHUTDOWN));
Andreas Hubera1f8ab02012-11-30 10:53:22 -08001626 }
1627}
1628
1629void NuPlayer::performReset() {
1630 ALOGV("performReset");
1631
1632 CHECK(mAudioDecoder == NULL);
1633 CHECK(mVideoDecoder == NULL);
1634
1635 cancelPollDuration();
1636
1637 ++mScanSourcesGeneration;
1638 mScanSourcesPending = false;
1639
Lajos Molnar09524832014-07-17 14:29:51 -07001640 if (mRendererLooper != NULL) {
1641 if (mRenderer != NULL) {
1642 mRendererLooper->unregisterHandler(mRenderer->id());
1643 }
1644 mRendererLooper->stop();
1645 mRendererLooper.clear();
1646 }
Andreas Hubera1f8ab02012-11-30 10:53:22 -08001647 mRenderer.clear();
Wei Jia57568df2014-09-22 10:16:29 -07001648 ++mRendererGeneration;
Andreas Hubera1f8ab02012-11-30 10:53:22 -08001649
1650 if (mSource != NULL) {
1651 mSource->stop();
Andreas Huberb5f25f02013-02-05 10:14:26 -08001652
Andreas Hubera1f8ab02012-11-30 10:53:22 -08001653 mSource.clear();
1654 }
1655
1656 if (mDriver != NULL) {
1657 sp<NuPlayerDriver> driver = mDriver.promote();
1658 if (driver != NULL) {
1659 driver->notifyResetComplete();
1660 }
1661 }
Andreas Huber57a339c2012-12-03 11:18:00 -08001662
1663 mStarted = false;
Andreas Hubera1f8ab02012-11-30 10:53:22 -08001664}
1665
1666void NuPlayer::performScanSources() {
1667 ALOGV("performScanSources");
1668
Andreas Huber57a339c2012-12-03 11:18:00 -08001669 if (!mStarted) {
1670 return;
1671 }
1672
Andreas Hubera1f8ab02012-11-30 10:53:22 -08001673 if (mAudioDecoder == NULL || mVideoDecoder == NULL) {
1674 postScanSources();
1675 }
1676}
1677
Andreas Huber57a339c2012-12-03 11:18:00 -08001678void NuPlayer::performSetSurface(const sp<NativeWindowWrapper> &wrapper) {
1679 ALOGV("performSetSurface");
1680
1681 mNativeWindow = wrapper;
1682
1683 // XXX - ignore error from setVideoScalingMode for now
1684 setVideoScalingMode(mVideoScalingMode);
Chong Zhang13d6faa2014-08-22 15:35:28 -07001685
1686 if (mDriver != NULL) {
1687 sp<NuPlayerDriver> driver = mDriver.promote();
1688 if (driver != NULL) {
1689 driver->notifySetSurfaceComplete();
1690 }
1691 }
Andreas Huber57a339c2012-12-03 11:18:00 -08001692}
1693
Chong Zhangf8d71772014-11-26 15:08:34 -08001694void NuPlayer::performResumeDecoders(bool needNotify) {
1695 if (needNotify) {
1696 mResumePending = true;
1697 if (mVideoDecoder == NULL) {
1698 // if audio-only, we can notify seek complete now,
1699 // as the resume operation will be relatively fast.
1700 finishResume();
1701 }
1702 }
1703
Chong Zhang7137ec72014-11-12 16:41:05 -08001704 if (mVideoDecoder != NULL) {
Chong Zhangf8d71772014-11-26 15:08:34 -08001705 // When there is continuous seek, MediaPlayer will cache the seek
1706 // position, and send down new seek request when previous seek is
1707 // complete. Let's wait for at least one video output frame before
1708 // notifying seek complete, so that the video thumbnail gets updated
1709 // when seekbar is dragged.
1710 mVideoDecoder->signalResume(needNotify);
Chong Zhang7137ec72014-11-12 16:41:05 -08001711 }
1712
1713 if (mAudioDecoder != NULL) {
Chong Zhangf8d71772014-11-26 15:08:34 -08001714 mAudioDecoder->signalResume(false /* needNotify */);
1715 }
1716}
1717
1718void NuPlayer::finishResume() {
1719 if (mResumePending) {
1720 mResumePending = false;
1721 if (mDriver != NULL) {
1722 sp<NuPlayerDriver> driver = mDriver.promote();
1723 if (driver != NULL) {
1724 driver->notifySeekComplete();
1725 }
1726 }
Chong Zhang7137ec72014-11-12 16:41:05 -08001727 }
1728}
1729
Andreas Huber9575c962013-02-05 13:59:56 -08001730void NuPlayer::onSourceNotify(const sp<AMessage> &msg) {
1731 int32_t what;
1732 CHECK(msg->findInt32("what", &what));
1733
1734 switch (what) {
Lajos Molnarfcd3e942015-03-31 10:06:48 -07001735 case Source::kWhatInstantiateSecureDecoders:
1736 {
1737 if (mSource == NULL) {
1738 // This is a stale notification from a source that was
1739 // asynchronously preparing when the client called reset().
1740 // We handled the reset, the source is gone.
1741 break;
1742 }
1743
1744 sp<AMessage> reply;
1745 CHECK(msg->findMessage("reply", &reply));
1746 status_t err = onInstantiateSecureDecoders();
1747 reply->setInt32("err", err);
1748 reply->post();
1749 break;
1750 }
1751
Andreas Huber9575c962013-02-05 13:59:56 -08001752 case Source::kWhatPrepared:
1753 {
Andreas Huberb5f28d42013-04-25 15:11:19 -07001754 if (mSource == NULL) {
1755 // This is a stale notification from a source that was
1756 // asynchronously preparing when the client called reset().
1757 // We handled the reset, the source is gone.
1758 break;
1759 }
1760
Andreas Huberec0c5972013-02-05 14:47:13 -08001761 int32_t err;
1762 CHECK(msg->findInt32("err", &err));
1763
Lajos Molnarfcd3e942015-03-31 10:06:48 -07001764 if (err != OK) {
1765 // shut down potential secure codecs in case client never calls reset
1766 mDeferredActions.push_back(
1767 new FlushDecoderAction(FLUSH_CMD_SHUTDOWN /* audio */,
1768 FLUSH_CMD_SHUTDOWN /* video */));
1769 processDeferredActions();
1770 }
1771
Andreas Huber9575c962013-02-05 13:59:56 -08001772 sp<NuPlayerDriver> driver = mDriver.promote();
1773 if (driver != NULL) {
Marco Nelissendd114d12014-05-28 15:23:14 -07001774 // notify duration first, so that it's definitely set when
1775 // the app received the "prepare complete" callback.
1776 int64_t durationUs;
1777 if (mSource->getDuration(&durationUs) == OK) {
1778 driver->notifyDuration(durationUs);
1779 }
Andreas Huberec0c5972013-02-05 14:47:13 -08001780 driver->notifyPrepareCompleted(err);
Andreas Huber9575c962013-02-05 13:59:56 -08001781 }
Andreas Huber99759402013-04-01 14:28:31 -07001782
Andreas Huber9575c962013-02-05 13:59:56 -08001783 break;
1784 }
1785
1786 case Source::kWhatFlagsChanged:
1787 {
1788 uint32_t flags;
1789 CHECK(msg->findInt32("flags", (int32_t *)&flags));
1790
Chong Zhang4b7069d2013-09-11 12:52:43 -07001791 sp<NuPlayerDriver> driver = mDriver.promote();
1792 if (driver != NULL) {
Wei Jia895651b2014-12-10 17:31:52 -08001793 if ((flags & NuPlayer::Source::FLAG_CAN_SEEK) == 0) {
1794 driver->notifyListener(
1795 MEDIA_INFO, MEDIA_INFO_NOT_SEEKABLE, 0);
1796 }
Chong Zhang4b7069d2013-09-11 12:52:43 -07001797 driver->notifyFlagsChanged(flags);
1798 }
1799
Andreas Huber9575c962013-02-05 13:59:56 -08001800 if ((mSourceFlags & Source::FLAG_DYNAMIC_DURATION)
1801 && (!(flags & Source::FLAG_DYNAMIC_DURATION))) {
1802 cancelPollDuration();
1803 } else if (!(mSourceFlags & Source::FLAG_DYNAMIC_DURATION)
1804 && (flags & Source::FLAG_DYNAMIC_DURATION)
1805 && (mAudioDecoder != NULL || mVideoDecoder != NULL)) {
1806 schedulePollDuration();
1807 }
1808
1809 mSourceFlags = flags;
1810 break;
1811 }
1812
1813 case Source::kWhatVideoSizeChanged:
1814 {
Chong Zhangced1c2f2014-08-08 15:22:35 -07001815 sp<AMessage> format;
1816 CHECK(msg->findMessage("format", &format));
Andreas Huber9575c962013-02-05 13:59:56 -08001817
Chong Zhangced1c2f2014-08-08 15:22:35 -07001818 updateVideoSize(format);
Andreas Huber9575c962013-02-05 13:59:56 -08001819 break;
1820 }
1821
Chong Zhang2a3cc9a2014-08-21 17:48:26 -07001822 case Source::kWhatBufferingUpdate:
1823 {
1824 int32_t percentage;
1825 CHECK(msg->findInt32("percentage", &percentage));
1826
1827 notifyListener(MEDIA_BUFFERING_UPDATE, percentage, 0);
1828 break;
1829 }
1830
Chong Zhangefbb6192015-01-30 17:13:27 -08001831 case Source::kWhatPauseOnBufferingStart:
1832 {
1833 // ignore if not playing
1834 if (mStarted && !mPausedByClient) {
1835 ALOGI("buffer low, pausing...");
1836
1837 onPause();
1838 }
1839 // fall-thru
1840 }
1841
Roger Jönssonb50e83e2013-01-21 16:26:41 +01001842 case Source::kWhatBufferingStart:
1843 {
1844 notifyListener(MEDIA_INFO, MEDIA_INFO_BUFFERING_START, 0);
1845 break;
1846 }
1847
Chong Zhangefbb6192015-01-30 17:13:27 -08001848 case Source::kWhatResumeOnBufferingEnd:
1849 {
1850 // ignore if not playing
1851 if (mStarted && !mPausedByClient) {
1852 ALOGI("buffer ready, resuming...");
1853
1854 onResume();
1855 }
1856 // fall-thru
1857 }
1858
Roger Jönssonb50e83e2013-01-21 16:26:41 +01001859 case Source::kWhatBufferingEnd:
1860 {
1861 notifyListener(MEDIA_INFO, MEDIA_INFO_BUFFERING_END, 0);
1862 break;
1863 }
1864
Chong Zhangefbb6192015-01-30 17:13:27 -08001865 case Source::kWhatCacheStats:
1866 {
1867 int32_t kbps;
1868 CHECK(msg->findInt32("bandwidth", &kbps));
1869
1870 notifyListener(MEDIA_INFO, MEDIA_INFO_NETWORK_BANDWIDTH, kbps);
1871 break;
1872 }
1873
Chong Zhangdcb89b32013-08-06 09:44:47 -07001874 case Source::kWhatSubtitleData:
1875 {
1876 sp<ABuffer> buffer;
1877 CHECK(msg->findBuffer("buffer", &buffer));
1878
Chong Zhang404fced2014-06-11 14:45:31 -07001879 sendSubtitleData(buffer, 0 /* baseIndex */);
Chong Zhangdcb89b32013-08-06 09:44:47 -07001880 break;
1881 }
1882
Robert Shih08528432015-04-08 09:06:54 -07001883 case Source::kWhatTimedMetaData:
1884 {
1885 sp<ABuffer> buffer;
1886 if (!msg->findBuffer("buffer", &buffer)) {
1887 notifyListener(MEDIA_INFO, MEDIA_INFO_METADATA_UPDATE, 0);
1888 } else {
1889 sendTimedMetaData(buffer);
1890 }
1891 break;
1892 }
1893
Robert Shihd3b0bbb2014-07-23 15:00:25 -07001894 case Source::kWhatTimedTextData:
1895 {
1896 int32_t generation;
1897 if (msg->findInt32("generation", &generation)
1898 && generation != mTimedTextGeneration) {
1899 break;
1900 }
1901
1902 sp<ABuffer> buffer;
1903 CHECK(msg->findBuffer("buffer", &buffer));
1904
1905 sp<NuPlayerDriver> driver = mDriver.promote();
1906 if (driver == NULL) {
1907 break;
1908 }
1909
1910 int posMs;
1911 int64_t timeUs, posUs;
1912 driver->getCurrentPosition(&posMs);
1913 posUs = posMs * 1000;
1914 CHECK(buffer->meta()->findInt64("timeUs", &timeUs));
1915
1916 if (posUs < timeUs) {
1917 if (!msg->findInt32("generation", &generation)) {
1918 msg->setInt32("generation", mTimedTextGeneration);
1919 }
1920 msg->post(timeUs - posUs);
1921 } else {
1922 sendTimedTextData(buffer);
1923 }
1924 break;
1925 }
1926
Andreas Huber14f76722013-01-15 09:04:18 -08001927 case Source::kWhatQueueDecoderShutdown:
1928 {
1929 int32_t audio, video;
1930 CHECK(msg->findInt32("audio", &audio));
1931 CHECK(msg->findInt32("video", &video));
1932
1933 sp<AMessage> reply;
1934 CHECK(msg->findMessage("reply", &reply));
1935
1936 queueDecoderShutdown(audio, video, reply);
1937 break;
1938 }
1939
Ronghua Wu80276872014-08-28 15:50:29 -07001940 case Source::kWhatDrmNoLicense:
1941 {
1942 notifyListener(MEDIA_ERROR, MEDIA_ERROR_UNKNOWN, ERROR_DRM_NO_LICENSE);
1943 break;
1944 }
1945
Andreas Huber9575c962013-02-05 13:59:56 -08001946 default:
1947 TRESPASS();
1948 }
1949}
1950
Chong Zhanga7fa1d92014-06-11 14:49:23 -07001951void NuPlayer::onClosedCaptionNotify(const sp<AMessage> &msg) {
1952 int32_t what;
1953 CHECK(msg->findInt32("what", &what));
1954
1955 switch (what) {
1956 case NuPlayer::CCDecoder::kWhatClosedCaptionData:
1957 {
1958 sp<ABuffer> buffer;
1959 CHECK(msg->findBuffer("buffer", &buffer));
1960
1961 size_t inbandTracks = 0;
1962 if (mSource != NULL) {
1963 inbandTracks = mSource->getTrackCount();
1964 }
1965
1966 sendSubtitleData(buffer, inbandTracks);
1967 break;
1968 }
1969
1970 case NuPlayer::CCDecoder::kWhatTrackAdded:
1971 {
1972 notifyListener(MEDIA_INFO, MEDIA_INFO_METADATA_UPDATE, 0);
1973
1974 break;
1975 }
1976
1977 default:
1978 TRESPASS();
1979 }
1980
1981
1982}
1983
Chong Zhang404fced2014-06-11 14:45:31 -07001984void NuPlayer::sendSubtitleData(const sp<ABuffer> &buffer, int32_t baseIndex) {
1985 int32_t trackIndex;
1986 int64_t timeUs, durationUs;
1987 CHECK(buffer->meta()->findInt32("trackIndex", &trackIndex));
1988 CHECK(buffer->meta()->findInt64("timeUs", &timeUs));
1989 CHECK(buffer->meta()->findInt64("durationUs", &durationUs));
1990
1991 Parcel in;
1992 in.writeInt32(trackIndex + baseIndex);
1993 in.writeInt64(timeUs);
1994 in.writeInt64(durationUs);
1995 in.writeInt32(buffer->size());
1996 in.writeInt32(buffer->size());
1997 in.write(buffer->data(), buffer->size());
1998
1999 notifyListener(MEDIA_SUBTITLE_DATA, 0, 0, &in);
2000}
Robert Shihd3b0bbb2014-07-23 15:00:25 -07002001
Robert Shih08528432015-04-08 09:06:54 -07002002void NuPlayer::sendTimedMetaData(const sp<ABuffer> &buffer) {
2003 int64_t timeUs;
2004 CHECK(buffer->meta()->findInt64("timeUs", &timeUs));
2005
2006 Parcel in;
2007 in.writeInt64(timeUs);
2008 in.writeInt32(buffer->size());
2009 in.writeInt32(buffer->size());
2010 in.write(buffer->data(), buffer->size());
2011
2012 notifyListener(MEDIA_META_DATA, 0, 0, &in);
2013}
2014
Robert Shihd3b0bbb2014-07-23 15:00:25 -07002015void NuPlayer::sendTimedTextData(const sp<ABuffer> &buffer) {
2016 const void *data;
2017 size_t size = 0;
2018 int64_t timeUs;
2019 int32_t flag = TextDescriptions::LOCAL_DESCRIPTIONS;
2020
2021 AString mime;
2022 CHECK(buffer->meta()->findString("mime", &mime));
2023 CHECK(strcasecmp(mime.c_str(), MEDIA_MIMETYPE_TEXT_3GPP) == 0);
2024
2025 data = buffer->data();
2026 size = buffer->size();
2027
2028 Parcel parcel;
2029 if (size > 0) {
2030 CHECK(buffer->meta()->findInt64("timeUs", &timeUs));
2031 flag |= TextDescriptions::IN_BAND_TEXT_3GPP;
2032 TextDescriptions::getParcelOfDescriptions(
2033 (const uint8_t *)data, size, flag, timeUs / 1000, &parcel);
2034 }
2035
2036 if ((parcel.dataSize() > 0)) {
2037 notifyListener(MEDIA_TIMED_TEXT, 0, 0, &parcel);
2038 } else { // send an empty timed text
2039 notifyListener(MEDIA_TIMED_TEXT, 0, 0);
2040 }
2041}
Andreas Huberb5f25f02013-02-05 10:14:26 -08002042////////////////////////////////////////////////////////////////////////////////
2043
Chong Zhangced1c2f2014-08-08 15:22:35 -07002044sp<AMessage> NuPlayer::Source::getFormat(bool audio) {
2045 sp<MetaData> meta = getFormatMeta(audio);
2046
2047 if (meta == NULL) {
2048 return NULL;
2049 }
2050
2051 sp<AMessage> msg = new AMessage;
2052
2053 if(convertMetaDataToMessage(meta, &msg) == OK) {
2054 return msg;
2055 }
2056 return NULL;
2057}
2058
Andreas Huber9575c962013-02-05 13:59:56 -08002059void NuPlayer::Source::notifyFlagsChanged(uint32_t flags) {
2060 sp<AMessage> notify = dupNotify();
2061 notify->setInt32("what", kWhatFlagsChanged);
2062 notify->setInt32("flags", flags);
2063 notify->post();
2064}
2065
Chong Zhangced1c2f2014-08-08 15:22:35 -07002066void NuPlayer::Source::notifyVideoSizeChanged(const sp<AMessage> &format) {
Andreas Huber9575c962013-02-05 13:59:56 -08002067 sp<AMessage> notify = dupNotify();
2068 notify->setInt32("what", kWhatVideoSizeChanged);
Chong Zhangced1c2f2014-08-08 15:22:35 -07002069 notify->setMessage("format", format);
Andreas Huber9575c962013-02-05 13:59:56 -08002070 notify->post();
2071}
2072
Andreas Huberec0c5972013-02-05 14:47:13 -08002073void NuPlayer::Source::notifyPrepared(status_t err) {
Andreas Huber9575c962013-02-05 13:59:56 -08002074 sp<AMessage> notify = dupNotify();
2075 notify->setInt32("what", kWhatPrepared);
Andreas Huberec0c5972013-02-05 14:47:13 -08002076 notify->setInt32("err", err);
Andreas Huber9575c962013-02-05 13:59:56 -08002077 notify->post();
2078}
2079
Lajos Molnarfcd3e942015-03-31 10:06:48 -07002080void NuPlayer::Source::notifyInstantiateSecureDecoders(const sp<AMessage> &reply) {
2081 sp<AMessage> notify = dupNotify();
2082 notify->setInt32("what", kWhatInstantiateSecureDecoders);
2083 notify->setMessage("reply", reply);
2084 notify->post();
2085}
2086
Andreas Huber84333e02014-02-07 15:36:10 -08002087void NuPlayer::Source::onMessageReceived(const sp<AMessage> & /* msg */) {
Andreas Huberb5f25f02013-02-05 10:14:26 -08002088 TRESPASS();
2089}
2090
Andreas Huberf9334412010-12-15 15:17:42 -08002091} // namespace android