blob: 1f557062bbace2ed887ffb2d692e6b74f27c637f [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),
Chong Zhangefbb6192015-01-30 17:13:27 -0800183 mStarted(false),
184 mPaused(false),
185 mPausedByClient(false) {
Andy Hung8d121d42014-10-03 09:53:53 -0700186 clearFlushComplete();
Andreas Huberf9334412010-12-15 15:17:42 -0800187}
188
189NuPlayer::~NuPlayer() {
190}
191
Andreas Huber9b80c2b2011-06-30 15:47:02 -0700192void NuPlayer::setUID(uid_t uid) {
193 mUIDValid = true;
194 mUID = uid;
195}
196
Andreas Huber43c3e6c2011-01-05 12:17:08 -0800197void NuPlayer::setDriver(const wp<NuPlayerDriver> &driver) {
198 mDriver = driver;
Andreas Huberf9334412010-12-15 15:17:42 -0800199}
200
Andreas Huber9575c962013-02-05 13:59:56 -0800201void NuPlayer::setDataSourceAsync(const sp<IStreamSource> &source) {
Andreas Huberf9334412010-12-15 15:17:42 -0800202 sp<AMessage> msg = new AMessage(kWhatSetDataSource, id());
203
Andreas Huberb5f25f02013-02-05 10:14:26 -0800204 sp<AMessage> notify = new AMessage(kWhatSourceNotify, id());
205
Andreas Huber240abcc2014-02-13 13:32:37 -0800206 msg->setObject("source", new StreamingSource(notify, source));
Andreas Huber5bc087c2010-12-23 10:27:40 -0800207 msg->post();
208}
Andreas Huberf9334412010-12-15 15:17:42 -0800209
Andreas Huberafed0e12011-09-20 15:39:58 -0700210static bool IsHTTPLiveURL(const char *url) {
211 if (!strncasecmp("http://", url, 7)
Andreas Huber99759402013-04-01 14:28:31 -0700212 || !strncasecmp("https://", url, 8)
213 || !strncasecmp("file://", url, 7)) {
Andreas Huberafed0e12011-09-20 15:39:58 -0700214 size_t len = strlen(url);
215 if (len >= 5 && !strcasecmp(".m3u8", &url[len - 5])) {
216 return true;
217 }
218
219 if (strstr(url,"m3u8")) {
220 return true;
221 }
222 }
223
224 return false;
225}
226
Andreas Huber9575c962013-02-05 13:59:56 -0800227void NuPlayer::setDataSourceAsync(
Andreas Huber1b86fe02014-01-29 11:13:26 -0800228 const sp<IMediaHTTPService> &httpService,
229 const char *url,
230 const KeyedVector<String8, String8> *headers) {
Chong Zhang3de157d2014-08-05 20:54:44 -0700231
Andreas Huber5bc087c2010-12-23 10:27:40 -0800232 sp<AMessage> msg = new AMessage(kWhatSetDataSource, id());
Oscar Rydhé7a33b772012-02-20 10:15:48 +0100233 size_t len = strlen(url);
Andreas Huber5bc087c2010-12-23 10:27:40 -0800234
Andreas Huberb5f25f02013-02-05 10:14:26 -0800235 sp<AMessage> notify = new AMessage(kWhatSourceNotify, id());
236
Andreas Huberafed0e12011-09-20 15:39:58 -0700237 sp<Source> source;
238 if (IsHTTPLiveURL(url)) {
Andreas Huber81e68442014-02-05 11:52:33 -0800239 source = new HTTPLiveSource(notify, httpService, url, headers);
Andreas Huberafed0e12011-09-20 15:39:58 -0700240 } else if (!strncasecmp(url, "rtsp://", 7)) {
Andreas Huber1b86fe02014-01-29 11:13:26 -0800241 source = new RTSPSource(
242 notify, httpService, url, headers, mUIDValid, mUID);
Oscar Rydhé7a33b772012-02-20 10:15:48 +0100243 } else if ((!strncasecmp(url, "http://", 7)
244 || !strncasecmp(url, "https://", 8))
245 && ((len >= 4 && !strcasecmp(".sdp", &url[len - 4]))
246 || strstr(url, ".sdp?"))) {
Andreas Huber1b86fe02014-01-29 11:13:26 -0800247 source = new RTSPSource(
248 notify, httpService, url, headers, mUIDValid, mUID, true);
Andreas Huber2bfdd422011-10-11 15:24:07 -0700249 } else {
Chong Zhang3de157d2014-08-05 20:54:44 -0700250 sp<GenericSource> genericSource =
251 new GenericSource(notify, mUIDValid, mUID);
252 // Don't set FLAG_SECURE on mSourceFlags here for widevine.
253 // The correct flags will be updated in Source::kWhatFlagsChanged
254 // handler when GenericSource is prepared.
Andreas Huber2bfdd422011-10-11 15:24:07 -0700255
Chong Zhanga19f33e2014-08-07 15:35:07 -0700256 status_t err = genericSource->setDataSource(httpService, url, headers);
Chong Zhang3de157d2014-08-05 20:54:44 -0700257
258 if (err == OK) {
259 source = genericSource;
260 } else {
Chong Zhanga19f33e2014-08-07 15:35:07 -0700261 ALOGE("Failed to set data source!");
Chong Zhang3de157d2014-08-05 20:54:44 -0700262 }
263 }
Andreas Huberafed0e12011-09-20 15:39:58 -0700264 msg->setObject("source", source);
265 msg->post();
266}
267
Andreas Huber9575c962013-02-05 13:59:56 -0800268void NuPlayer::setDataSourceAsync(int fd, int64_t offset, int64_t length) {
Andreas Huberafed0e12011-09-20 15:39:58 -0700269 sp<AMessage> msg = new AMessage(kWhatSetDataSource, id());
270
Andreas Huberb5f25f02013-02-05 10:14:26 -0800271 sp<AMessage> notify = new AMessage(kWhatSourceNotify, id());
272
Chong Zhang3de157d2014-08-05 20:54:44 -0700273 sp<GenericSource> source =
274 new GenericSource(notify, mUIDValid, mUID);
275
Chong Zhanga19f33e2014-08-07 15:35:07 -0700276 status_t err = source->setDataSource(fd, offset, length);
Chong Zhang3de157d2014-08-05 20:54:44 -0700277
278 if (err != OK) {
Chong Zhanga19f33e2014-08-07 15:35:07 -0700279 ALOGE("Failed to set data source!");
Chong Zhang3de157d2014-08-05 20:54:44 -0700280 source = NULL;
281 }
282
Andreas Huberafed0e12011-09-20 15:39:58 -0700283 msg->setObject("source", source);
Andreas Huberf9334412010-12-15 15:17:42 -0800284 msg->post();
285}
286
Andreas Huber9575c962013-02-05 13:59:56 -0800287void NuPlayer::prepareAsync() {
288 (new AMessage(kWhatPrepare, id()))->post();
289}
290
Andreas Huber57a339c2012-12-03 11:18:00 -0800291void NuPlayer::setVideoSurfaceTextureAsync(
Andy McFadden8ba01022012-12-18 09:46:54 -0800292 const sp<IGraphicBufferProducer> &bufferProducer) {
Glenn Kasten11731182011-02-08 17:26:17 -0800293 sp<AMessage> msg = new AMessage(kWhatSetVideoNativeWindow, id());
Andreas Huber57a339c2012-12-03 11:18:00 -0800294
Andy McFadden8ba01022012-12-18 09:46:54 -0800295 if (bufferProducer == NULL) {
Andreas Huber57a339c2012-12-03 11:18:00 -0800296 msg->setObject("native-window", NULL);
297 } else {
298 msg->setObject(
299 "native-window",
300 new NativeWindowWrapper(
Wei Jia9c03a402014-08-26 15:24:43 -0700301 new Surface(bufferProducer, true /* controlledByApp */)));
Andreas Huber57a339c2012-12-03 11:18:00 -0800302 }
303
Andreas Huberf9334412010-12-15 15:17:42 -0800304 msg->post();
305}
306
307void NuPlayer::setAudioSink(const sp<MediaPlayerBase::AudioSink> &sink) {
308 sp<AMessage> msg = new AMessage(kWhatSetAudioSink, id());
309 msg->setObject("sink", sink);
310 msg->post();
311}
312
313void NuPlayer::start() {
314 (new AMessage(kWhatStart, id()))->post();
315}
316
Andreas Huber43c3e6c2011-01-05 12:17:08 -0800317void NuPlayer::pause() {
Andreas Huberb4082222011-01-20 15:23:04 -0800318 (new AMessage(kWhatPause, id()))->post();
Andreas Huber43c3e6c2011-01-05 12:17:08 -0800319}
320
Andreas Huber1aef2112011-01-04 14:01:29 -0800321void NuPlayer::resetAsync() {
Chong Zhang48296b72014-09-14 14:28:45 -0700322 if (mSource != NULL) {
323 // During a reset, the data source might be unresponsive already, we need to
324 // disconnect explicitly so that reads exit promptly.
325 // We can't queue the disconnect request to the looper, as it might be
326 // queued behind a stuck read and never gets processed.
327 // Doing a disconnect outside the looper to allows the pending reads to exit
328 // (either successfully or with error).
329 mSource->disconnect();
330 }
331
Andreas Huber1aef2112011-01-04 14:01:29 -0800332 (new AMessage(kWhatReset, id()))->post();
333}
334
Wei Jiae427abf2014-09-22 15:21:11 -0700335void NuPlayer::seekToAsync(int64_t seekTimeUs, bool needNotify) {
Andreas Huber43c3e6c2011-01-05 12:17:08 -0800336 sp<AMessage> msg = new AMessage(kWhatSeek, id());
337 msg->setInt64("seekTimeUs", seekTimeUs);
Wei Jiae427abf2014-09-22 15:21:11 -0700338 msg->setInt32("needNotify", needNotify);
Andreas Huber43c3e6c2011-01-05 12:17:08 -0800339 msg->post();
340}
341
Andreas Huber53df1a42010-12-22 10:03:04 -0800342
Chong Zhang404fced2014-06-11 14:45:31 -0700343void NuPlayer::writeTrackInfo(
344 Parcel* reply, const sp<AMessage> format) const {
345 int32_t trackType;
346 CHECK(format->findInt32("type", &trackType));
347
348 AString lang;
349 CHECK(format->findString("language", &lang));
350
351 reply->writeInt32(2); // write something non-zero
352 reply->writeInt32(trackType);
353 reply->writeString16(String16(lang.c_str()));
354
355 if (trackType == MEDIA_TRACK_TYPE_SUBTITLE) {
356 AString mime;
357 CHECK(format->findString("mime", &mime));
358
359 int32_t isAuto, isDefault, isForced;
360 CHECK(format->findInt32("auto", &isAuto));
361 CHECK(format->findInt32("default", &isDefault));
362 CHECK(format->findInt32("forced", &isForced));
363
364 reply->writeString16(String16(mime.c_str()));
365 reply->writeInt32(isAuto);
366 reply->writeInt32(isDefault);
367 reply->writeInt32(isForced);
368 }
369}
370
Andreas Huberf9334412010-12-15 15:17:42 -0800371void NuPlayer::onMessageReceived(const sp<AMessage> &msg) {
372 switch (msg->what()) {
373 case kWhatSetDataSource:
374 {
Steve Block3856b092011-10-20 11:56:00 +0100375 ALOGV("kWhatSetDataSource");
Andreas Huberf9334412010-12-15 15:17:42 -0800376
377 CHECK(mSource == NULL);
378
Chong Zhang3de157d2014-08-05 20:54:44 -0700379 status_t err = OK;
Andreas Huber5bc087c2010-12-23 10:27:40 -0800380 sp<RefBase> obj;
381 CHECK(msg->findObject("source", &obj));
Chong Zhang3de157d2014-08-05 20:54:44 -0700382 if (obj != NULL) {
383 mSource = static_cast<Source *>(obj.get());
Chong Zhang3de157d2014-08-05 20:54:44 -0700384 } else {
385 err = UNKNOWN_ERROR;
386 }
Andreas Huber9575c962013-02-05 13:59:56 -0800387
388 CHECK(mDriver != NULL);
389 sp<NuPlayerDriver> driver = mDriver.promote();
390 if (driver != NULL) {
Chong Zhang3de157d2014-08-05 20:54:44 -0700391 driver->notifySetDataSourceCompleted(err);
Andreas Huber9575c962013-02-05 13:59:56 -0800392 }
393 break;
394 }
395
396 case kWhatPrepare:
397 {
398 mSource->prepareAsync();
Andreas Huberf9334412010-12-15 15:17:42 -0800399 break;
400 }
401
Chong Zhangdcb89b32013-08-06 09:44:47 -0700402 case kWhatGetTrackInfo:
403 {
404 uint32_t replyID;
405 CHECK(msg->senderAwaitsResponse(&replyID));
406
Chong Zhang404fced2014-06-11 14:45:31 -0700407 Parcel* reply;
408 CHECK(msg->findPointer("reply", (void**)&reply));
409
410 size_t inbandTracks = 0;
Chong Zhangdcb89b32013-08-06 09:44:47 -0700411 if (mSource != NULL) {
Chong Zhang404fced2014-06-11 14:45:31 -0700412 inbandTracks = mSource->getTrackCount();
413 }
414
Chong Zhanga7fa1d92014-06-11 14:49:23 -0700415 size_t ccTracks = 0;
416 if (mCCDecoder != NULL) {
417 ccTracks = mCCDecoder->getTrackCount();
418 }
419
Chong Zhang404fced2014-06-11 14:45:31 -0700420 // total track count
Chong Zhanga7fa1d92014-06-11 14:49:23 -0700421 reply->writeInt32(inbandTracks + ccTracks);
Chong Zhang404fced2014-06-11 14:45:31 -0700422
423 // write inband tracks
424 for (size_t i = 0; i < inbandTracks; ++i) {
425 writeTrackInfo(reply, mSource->getTrackInfo(i));
Chong Zhangdcb89b32013-08-06 09:44:47 -0700426 }
427
Chong Zhanga7fa1d92014-06-11 14:49:23 -0700428 // write CC track
429 for (size_t i = 0; i < ccTracks; ++i) {
430 writeTrackInfo(reply, mCCDecoder->getTrackInfo(i));
431 }
432
Chong Zhangdcb89b32013-08-06 09:44:47 -0700433 sp<AMessage> response = new AMessage;
Chong Zhangdcb89b32013-08-06 09:44:47 -0700434 response->postReply(replyID);
435 break;
436 }
437
Robert Shih7c4f0d72014-07-09 18:53:31 -0700438 case kWhatGetSelectedTrack:
439 {
440 status_t err = INVALID_OPERATION;
441 if (mSource != NULL) {
442 err = OK;
443
444 int32_t type32;
445 CHECK(msg->findInt32("type", (int32_t*)&type32));
446 media_track_type type = (media_track_type)type32;
447 ssize_t selectedTrack = mSource->getSelectedTrack(type);
448
449 Parcel* reply;
450 CHECK(msg->findPointer("reply", (void**)&reply));
451 reply->writeInt32(selectedTrack);
452 }
453
454 sp<AMessage> response = new AMessage;
455 response->setInt32("err", err);
456
457 uint32_t replyID;
458 CHECK(msg->senderAwaitsResponse(&replyID));
459 response->postReply(replyID);
460 break;
461 }
462
Chong Zhangdcb89b32013-08-06 09:44:47 -0700463 case kWhatSelectTrack:
464 {
465 uint32_t replyID;
466 CHECK(msg->senderAwaitsResponse(&replyID));
467
Chong Zhang404fced2014-06-11 14:45:31 -0700468 size_t trackIndex;
469 int32_t select;
Robert Shih6ffb1fd2014-10-29 16:24:32 -0700470 int64_t timeUs;
Chong Zhang404fced2014-06-11 14:45:31 -0700471 CHECK(msg->findSize("trackIndex", &trackIndex));
472 CHECK(msg->findInt32("select", &select));
Robert Shih6ffb1fd2014-10-29 16:24:32 -0700473 CHECK(msg->findInt64("timeUs", &timeUs));
Chong Zhang404fced2014-06-11 14:45:31 -0700474
Chong Zhangdcb89b32013-08-06 09:44:47 -0700475 status_t err = INVALID_OPERATION;
Chong Zhang404fced2014-06-11 14:45:31 -0700476
477 size_t inbandTracks = 0;
Chong Zhangdcb89b32013-08-06 09:44:47 -0700478 if (mSource != NULL) {
Chong Zhang404fced2014-06-11 14:45:31 -0700479 inbandTracks = mSource->getTrackCount();
480 }
Chong Zhanga7fa1d92014-06-11 14:49:23 -0700481 size_t ccTracks = 0;
482 if (mCCDecoder != NULL) {
483 ccTracks = mCCDecoder->getTrackCount();
484 }
Chong Zhang404fced2014-06-11 14:45:31 -0700485
486 if (trackIndex < inbandTracks) {
Robert Shih6ffb1fd2014-10-29 16:24:32 -0700487 err = mSource->selectTrack(trackIndex, select, timeUs);
Robert Shihd3b0bbb2014-07-23 15:00:25 -0700488
489 if (!select && err == OK) {
490 int32_t type;
491 sp<AMessage> info = mSource->getTrackInfo(trackIndex);
492 if (info != NULL
493 && info->findInt32("type", &type)
494 && type == MEDIA_TRACK_TYPE_TIMEDTEXT) {
495 ++mTimedTextGeneration;
496 }
497 }
Chong Zhanga7fa1d92014-06-11 14:49:23 -0700498 } else {
499 trackIndex -= inbandTracks;
500
501 if (trackIndex < ccTracks) {
502 err = mCCDecoder->selectTrack(trackIndex, select);
503 }
Chong Zhangdcb89b32013-08-06 09:44:47 -0700504 }
505
506 sp<AMessage> response = new AMessage;
507 response->setInt32("err", err);
508
509 response->postReply(replyID);
510 break;
511 }
512
Andreas Huberb7c8e912012-11-27 15:02:53 -0800513 case kWhatPollDuration:
514 {
515 int32_t generation;
516 CHECK(msg->findInt32("generation", &generation));
517
518 if (generation != mPollDurationGeneration) {
519 // stale
520 break;
521 }
522
523 int64_t durationUs;
524 if (mDriver != NULL && mSource->getDuration(&durationUs) == OK) {
525 sp<NuPlayerDriver> driver = mDriver.promote();
526 if (driver != NULL) {
527 driver->notifyDuration(durationUs);
528 }
529 }
530
531 msg->post(1000000ll); // poll again in a second.
532 break;
533 }
534
Glenn Kasten11731182011-02-08 17:26:17 -0800535 case kWhatSetVideoNativeWindow:
Andreas Huberf9334412010-12-15 15:17:42 -0800536 {
Steve Block3856b092011-10-20 11:56:00 +0100537 ALOGV("kWhatSetVideoNativeWindow");
Andreas Huberf9334412010-12-15 15:17:42 -0800538
539 sp<RefBase> obj;
Glenn Kasten11731182011-02-08 17:26:17 -0800540 CHECK(msg->findObject("native-window", &obj));
Andreas Huberf9334412010-12-15 15:17:42 -0800541
Lajos Molnarf4849522014-12-10 16:58:57 -0800542 if (mSource == NULL || mSource->getFormat(false /* audio */) == NULL) {
Wei Jiafef808d2014-10-31 17:57:05 -0700543 performSetSurface(static_cast<NativeWindowWrapper *>(obj.get()));
544 break;
545 }
546
547 mDeferredActions.push_back(
548 new FlushDecoderAction(FLUSH_CMD_FLUSH /* audio */,
549 FLUSH_CMD_SHUTDOWN /* video */));
550
Andreas Huber57a339c2012-12-03 11:18:00 -0800551 mDeferredActions.push_back(
552 new SetSurfaceAction(
553 static_cast<NativeWindowWrapper *>(obj.get())));
James Dong0d268a32012-08-31 12:18:27 -0700554
Andreas Huber57a339c2012-12-03 11:18:00 -0800555 if (obj != NULL) {
Wei Jiafef808d2014-10-31 17:57:05 -0700556 if (mStarted) {
Andy Hung73535852014-09-05 11:42:58 -0700557 // Issue a seek to refresh the video screen only if started otherwise
558 // the extractor may not yet be started and will assert.
559 // If the video decoder is not set (perhaps audio only in this case)
560 // do not perform a seek as it is not needed.
Ronghua Wua73d9e02014-10-08 15:13:29 -0700561 int64_t currentPositionUs = 0;
562 if (getCurrentPosition(&currentPositionUs) == OK) {
563 mDeferredActions.push_back(
564 new SeekAction(currentPositionUs, false /* needNotify */));
565 }
Andy Hung73535852014-09-05 11:42:58 -0700566 }
Wei Jiaac428aa2014-09-02 19:01:34 -0700567
Andreas Huber57a339c2012-12-03 11:18:00 -0800568 // If there is a new surface texture, instantiate decoders
569 // again if possible.
570 mDeferredActions.push_back(
571 new SimpleAction(&NuPlayer::performScanSources));
572 }
573
Chong Zhangf8d71772014-11-26 15:08:34 -0800574 // After a flush without shutdown, decoder is paused.
575 // Don't resume it until source seek is done, otherwise it could
Chong Zhang7137ec72014-11-12 16:41:05 -0800576 // start pulling stale data too soon.
577 mDeferredActions.push_back(
Chong Zhangf8d71772014-11-26 15:08:34 -0800578 new ResumeDecoderAction(false /* needNotify */));
Chong Zhang7137ec72014-11-12 16:41:05 -0800579
Andreas Huber57a339c2012-12-03 11:18:00 -0800580 processDeferredActions();
Andreas Huberf9334412010-12-15 15:17:42 -0800581 break;
582 }
583
584 case kWhatSetAudioSink:
585 {
Steve Block3856b092011-10-20 11:56:00 +0100586 ALOGV("kWhatSetAudioSink");
Andreas Huberf9334412010-12-15 15:17:42 -0800587
588 sp<RefBase> obj;
589 CHECK(msg->findObject("sink", &obj));
590
591 mAudioSink = static_cast<MediaPlayerBase::AudioSink *>(obj.get());
592 break;
593 }
594
595 case kWhatStart:
596 {
Steve Block3856b092011-10-20 11:56:00 +0100597 ALOGV("kWhatStart");
Wei Jia94211742014-10-28 17:09:06 -0700598 if (mStarted) {
599 onResume();
600 } else {
601 onStart();
Lajos Molnar09524832014-07-17 14:29:51 -0700602 }
Chong Zhangefbb6192015-01-30 17:13:27 -0800603 mPausedByClient = false;
Andreas Huberf9334412010-12-15 15:17:42 -0800604 break;
605 }
606
607 case kWhatScanSources:
608 {
Andreas Huber1aef2112011-01-04 14:01:29 -0800609 int32_t generation;
610 CHECK(msg->findInt32("generation", &generation));
611 if (generation != mScanSourcesGeneration) {
612 // Drop obsolete msg.
613 break;
614 }
615
Andreas Huber5bc087c2010-12-23 10:27:40 -0800616 mScanSourcesPending = false;
617
Steve Block3856b092011-10-20 11:56:00 +0100618 ALOGV("scanning sources haveAudio=%d, haveVideo=%d",
Andreas Huber43c3e6c2011-01-05 12:17:08 -0800619 mAudioDecoder != NULL, mVideoDecoder != NULL);
620
Andreas Huberb7c8e912012-11-27 15:02:53 -0800621 bool mHadAnySourcesBefore =
622 (mAudioDecoder != NULL) || (mVideoDecoder != NULL);
623
Andy Hung282a7e32014-08-14 15:56:34 -0700624 // initialize video before audio because successful initialization of
625 // video may change deep buffer mode of audio.
Haynes Mathew George5d246ef2012-07-09 10:36:57 -0700626 if (mNativeWindow != NULL) {
627 instantiateDecoder(false, &mVideoDecoder);
628 }
Andreas Huberf9334412010-12-15 15:17:42 -0800629
Ronghua Wua10fd232014-11-06 16:15:20 -0800630 // Don't try to re-open audio sink if there's an existing decoder.
631 if (mAudioSink != NULL && mAudioDecoder == NULL) {
632 sp<MetaData> audioMeta = mSource->getFormatMeta(true /* audio */);
633 sp<AMessage> videoFormat = mSource->getFormat(false /* audio */);
634 audio_stream_type_t streamType = mAudioSink->getAudioStreamType();
Andy Hung202bce12014-12-03 11:47:36 -0800635 const bool hasVideo = (videoFormat != NULL);
636 const bool canOffload = canOffloadStream(
637 audioMeta, hasVideo, true /* is_streaming */, streamType);
Ronghua Wua10fd232014-11-06 16:15:20 -0800638 if (canOffload) {
639 if (!mOffloadAudio) {
640 mRenderer->signalEnableOffloadAudio();
641 }
Andy Hung282a7e32014-08-14 15:56:34 -0700642 // open audio sink early under offload mode.
643 sp<AMessage> format = mSource->getFormat(true /*audio*/);
Andy Hung202bce12014-12-03 11:47:36 -0800644 tryOpenAudioSinkForOffload(format, hasVideo);
Andy Hung282a7e32014-08-14 15:56:34 -0700645 }
Andreas Huber5bc087c2010-12-23 10:27:40 -0800646 instantiateDecoder(true, &mAudioDecoder);
Andreas Huberf9334412010-12-15 15:17:42 -0800647 }
648
Andreas Huberb7c8e912012-11-27 15:02:53 -0800649 if (!mHadAnySourcesBefore
650 && (mAudioDecoder != NULL || mVideoDecoder != NULL)) {
651 // This is the first time we've found anything playable.
652
Andreas Huber9575c962013-02-05 13:59:56 -0800653 if (mSourceFlags & Source::FLAG_DYNAMIC_DURATION) {
Andreas Huberb7c8e912012-11-27 15:02:53 -0800654 schedulePollDuration();
655 }
656 }
657
Andreas Hubereac68ba2011-09-27 12:12:25 -0700658 status_t err;
659 if ((err = mSource->feedMoreTSData()) != OK) {
Andreas Huber1aef2112011-01-04 14:01:29 -0800660 if (mAudioDecoder == NULL && mVideoDecoder == NULL) {
661 // We're not currently decoding anything (no audio or
662 // video tracks found) and we just ran out of input data.
Andreas Hubereac68ba2011-09-27 12:12:25 -0700663
664 if (err == ERROR_END_OF_STREAM) {
665 notifyListener(MEDIA_PLAYBACK_COMPLETE, 0, 0);
666 } else {
667 notifyListener(MEDIA_ERROR, MEDIA_ERROR_UNKNOWN, err);
668 }
Andreas Huber1aef2112011-01-04 14:01:29 -0800669 }
Andreas Huberf9334412010-12-15 15:17:42 -0800670 break;
671 }
672
Andreas Huberfbe9d812012-08-31 14:05:27 -0700673 if ((mAudioDecoder == NULL && mAudioSink != NULL)
674 || (mVideoDecoder == NULL && mNativeWindow != NULL)) {
Andreas Huberf9334412010-12-15 15:17:42 -0800675 msg->post(100000ll);
Andreas Huber5bc087c2010-12-23 10:27:40 -0800676 mScanSourcesPending = true;
Andreas Huberf9334412010-12-15 15:17:42 -0800677 }
678 break;
679 }
680
681 case kWhatVideoNotify:
682 case kWhatAudioNotify:
683 {
684 bool audio = msg->what() == kWhatAudioNotify;
685
Wei Jia88703c32014-08-06 11:24:07 -0700686 int32_t currentDecoderGeneration =
687 (audio? mAudioDecoderGeneration : mVideoDecoderGeneration);
688 int32_t requesterGeneration = currentDecoderGeneration - 1;
689 CHECK(msg->findInt32("generation", &requesterGeneration));
690
691 if (requesterGeneration != currentDecoderGeneration) {
692 ALOGV("got message from old %s decoder, generation(%d:%d)",
693 audio ? "audio" : "video", requesterGeneration,
694 currentDecoderGeneration);
695 sp<AMessage> reply;
696 if (!(msg->findMessage("reply", &reply))) {
697 return;
698 }
699
700 reply->setInt32("err", INFO_DISCONTINUITY);
701 reply->post();
702 return;
703 }
704
Andreas Huberf9334412010-12-15 15:17:42 -0800705 int32_t what;
Lajos Molnar1cd13982014-01-17 15:12:51 -0800706 CHECK(msg->findInt32("what", &what));
Andreas Huberf9334412010-12-15 15:17:42 -0800707
Chong Zhang7137ec72014-11-12 16:41:05 -0800708 if (what == DecoderBase::kWhatInputDiscontinuity) {
709 int32_t formatChange;
710 CHECK(msg->findInt32("formatChange", &formatChange));
Andreas Huberf9334412010-12-15 15:17:42 -0800711
Chong Zhang7137ec72014-11-12 16:41:05 -0800712 ALOGV("%s discontinuity: formatChange %d",
713 audio ? "audio" : "video", formatChange);
714
715 if (formatChange) {
716 mDeferredActions.push_back(
717 new FlushDecoderAction(
718 audio ? FLUSH_CMD_SHUTDOWN : FLUSH_CMD_NONE,
719 audio ? FLUSH_CMD_NONE : FLUSH_CMD_SHUTDOWN));
Andreas Huberf9334412010-12-15 15:17:42 -0800720 }
Chong Zhang7137ec72014-11-12 16:41:05 -0800721
722 mDeferredActions.push_back(
723 new SimpleAction(
724 &NuPlayer::performScanSources));
725
726 processDeferredActions();
727 } else if (what == DecoderBase::kWhatEOS) {
Andreas Huberdc9bacd2011-09-26 10:53:29 -0700728 int32_t err;
Lajos Molnar1cd13982014-01-17 15:12:51 -0800729 CHECK(msg->findInt32("err", &err));
Andreas Huberdc9bacd2011-09-26 10:53:29 -0700730
731 if (err == ERROR_END_OF_STREAM) {
Steve Block3856b092011-10-20 11:56:00 +0100732 ALOGV("got %s decoder EOS", audio ? "audio" : "video");
Andreas Huberdc9bacd2011-09-26 10:53:29 -0700733 } else {
Steve Block3856b092011-10-20 11:56:00 +0100734 ALOGV("got %s decoder EOS w/ error %d",
Andreas Huberdc9bacd2011-09-26 10:53:29 -0700735 audio ? "audio" : "video",
736 err);
737 }
738
739 mRenderer->queueEOS(audio, err);
Chong Zhang7137ec72014-11-12 16:41:05 -0800740 } else if (what == DecoderBase::kWhatFlushCompleted) {
Steve Block3856b092011-10-20 11:56:00 +0100741 ALOGV("decoder %s flush completed", audio ? "audio" : "video");
Andreas Huberf9334412010-12-15 15:17:42 -0800742
Andy Hung8d121d42014-10-03 09:53:53 -0700743 handleFlushComplete(audio, true /* isDecoder */);
Andreas Huber3831a062010-12-21 10:22:33 -0800744 finishFlushIfPossible();
Chong Zhang7137ec72014-11-12 16:41:05 -0800745 } else if (what == DecoderBase::kWhatVideoSizeChanged) {
Lajos Molnar1cd13982014-01-17 15:12:51 -0800746 sp<AMessage> format;
747 CHECK(msg->findMessage("format", &format));
748
Wei Jiac6cfd702014-11-11 16:33:20 -0800749 sp<AMessage> inputFormat =
750 mSource->getFormat(false /* audio */);
Andreas Huber3831a062010-12-21 10:22:33 -0800751
Wei Jiac6cfd702014-11-11 16:33:20 -0800752 updateVideoSize(inputFormat, format);
Chong Zhang7137ec72014-11-12 16:41:05 -0800753 } else if (what == DecoderBase::kWhatShutdownCompleted) {
Steve Block3856b092011-10-20 11:56:00 +0100754 ALOGV("%s shutdown completed", audio ? "audio" : "video");
Andreas Huber3831a062010-12-21 10:22:33 -0800755 if (audio) {
756 mAudioDecoder.clear();
Wei Jia57568df2014-09-22 10:16:29 -0700757 ++mAudioDecoderGeneration;
Andreas Huber3831a062010-12-21 10:22:33 -0800758
759 CHECK_EQ((int)mFlushingAudio, (int)SHUTTING_DOWN_DECODER);
760 mFlushingAudio = SHUT_DOWN;
761 } else {
762 mVideoDecoder.clear();
Wei Jia57568df2014-09-22 10:16:29 -0700763 ++mVideoDecoderGeneration;
Andreas Huber3831a062010-12-21 10:22:33 -0800764
765 CHECK_EQ((int)mFlushingVideo, (int)SHUTTING_DOWN_DECODER);
766 mFlushingVideo = SHUT_DOWN;
767 }
768
769 finishFlushIfPossible();
Chong Zhangf8d71772014-11-26 15:08:34 -0800770 } else if (what == DecoderBase::kWhatResumeCompleted) {
771 finishResume();
Chong Zhang7137ec72014-11-12 16:41:05 -0800772 } else if (what == DecoderBase::kWhatError) {
Chong Zhangf4c0a942014-08-11 15:14:10 -0700773 status_t err;
Andy Hung2abde2c2014-09-30 14:40:32 -0700774 if (!msg->findInt32("err", &err) || err == OK) {
Chong Zhangf4c0a942014-08-11 15:14:10 -0700775 err = UNKNOWN_ERROR;
776 }
Andy Hungcf31f1e2014-09-23 14:59:01 -0700777
Andy Hung2abde2c2014-09-30 14:40:32 -0700778 // Decoder errors can be due to Source (e.g. from streaming),
779 // or from decoding corrupted bitstreams, or from other decoder
780 // MediaCodec operations (e.g. from an ongoing reset or seek).
Andy Hung202bce12014-12-03 11:47:36 -0800781 // They may also be due to openAudioSink failure at
782 // decoder start or after a format change.
Andy Hung2abde2c2014-09-30 14:40:32 -0700783 //
784 // We try to gracefully shut down the affected decoder if possible,
785 // rather than trying to force the shutdown with something
786 // similar to performReset(). This method can lead to a hang
787 // if MediaCodec functions block after an error, but they should
788 // typically return INVALID_OPERATION instead of blocking.
789
790 FlushStatus *flushing = audio ? &mFlushingAudio : &mFlushingVideo;
791 ALOGE("received error(%#x) from %s decoder, flushing(%d), now shutting down",
792 err, audio ? "audio" : "video", *flushing);
793
794 switch (*flushing) {
795 case NONE:
796 mDeferredActions.push_back(
Wei Jiafef808d2014-10-31 17:57:05 -0700797 new FlushDecoderAction(
798 audio ? FLUSH_CMD_SHUTDOWN : FLUSH_CMD_NONE,
799 audio ? FLUSH_CMD_NONE : FLUSH_CMD_SHUTDOWN));
Andy Hung2abde2c2014-09-30 14:40:32 -0700800 processDeferredActions();
801 break;
802 case FLUSHING_DECODER:
803 *flushing = FLUSHING_DECODER_SHUTDOWN; // initiate shutdown after flush.
804 break; // Wait for flush to complete.
805 case FLUSHING_DECODER_SHUTDOWN:
806 break; // Wait for flush to complete.
807 case SHUTTING_DOWN_DECODER:
808 break; // Wait for shutdown to complete.
809 case FLUSHED:
810 // Widevine source reads must stop before releasing the video decoder.
811 if (!audio && mSource != NULL && mSourceFlags & Source::FLAG_SECURE) {
812 mSource->stop();
813 }
814 getDecoder(audio)->initiateShutdown(); // In the middle of a seek.
815 *flushing = SHUTTING_DOWN_DECODER; // Shut down.
816 break;
817 case SHUT_DOWN:
818 finishFlushIfPossible(); // Should not occur.
819 break; // Finish anyways.
Marco Nelissen9e2b7912014-08-18 16:13:03 -0700820 }
Andy Hung2abde2c2014-09-30 14:40:32 -0700821 notifyListener(MEDIA_ERROR, MEDIA_ERROR_UNKNOWN, err);
Lajos Molnar1cd13982014-01-17 15:12:51 -0800822 } else {
823 ALOGV("Unhandled decoder notification %d '%c%c%c%c'.",
Andreas Hubera1f8ab02012-11-30 10:53:22 -0800824 what,
825 what >> 24,
826 (what >> 16) & 0xff,
827 (what >> 8) & 0xff,
828 what & 0xff);
Andreas Huberf9334412010-12-15 15:17:42 -0800829 }
830
831 break;
832 }
833
834 case kWhatRendererNotify:
835 {
Wei Jia57568df2014-09-22 10:16:29 -0700836 int32_t requesterGeneration = mRendererGeneration - 1;
837 CHECK(msg->findInt32("generation", &requesterGeneration));
838 if (requesterGeneration != mRendererGeneration) {
839 ALOGV("got message from old renderer, generation(%d:%d)",
840 requesterGeneration, mRendererGeneration);
841 return;
842 }
843
Andreas Huberf9334412010-12-15 15:17:42 -0800844 int32_t what;
845 CHECK(msg->findInt32("what", &what));
846
847 if (what == Renderer::kWhatEOS) {
848 int32_t audio;
849 CHECK(msg->findInt32("audio", &audio));
850
Andreas Huberc92fd242011-08-16 13:48:44 -0700851 int32_t finalResult;
852 CHECK(msg->findInt32("finalResult", &finalResult));
853
Andreas Huberf9334412010-12-15 15:17:42 -0800854 if (audio) {
855 mAudioEOS = true;
856 } else {
857 mVideoEOS = true;
858 }
859
Andreas Huberc92fd242011-08-16 13:48:44 -0700860 if (finalResult == ERROR_END_OF_STREAM) {
Steve Block3856b092011-10-20 11:56:00 +0100861 ALOGV("reached %s EOS", audio ? "audio" : "video");
Andreas Huberc92fd242011-08-16 13:48:44 -0700862 } else {
Steve Block29357bc2012-01-06 19:20:56 +0000863 ALOGE("%s track encountered an error (%d)",
Andreas Huberc92fd242011-08-16 13:48:44 -0700864 audio ? "audio" : "video", finalResult);
865
866 notifyListener(
867 MEDIA_ERROR, MEDIA_ERROR_UNKNOWN, finalResult);
868 }
Andreas Huberf9334412010-12-15 15:17:42 -0800869
870 if ((mAudioEOS || mAudioDecoder == NULL)
871 && (mVideoEOS || mVideoDecoder == NULL)) {
872 notifyListener(MEDIA_PLAYBACK_COMPLETE, 0, 0);
873 }
Andreas Huber3fe62152011-09-16 15:09:22 -0700874 } else if (what == Renderer::kWhatFlushComplete) {
Andreas Huberf9334412010-12-15 15:17:42 -0800875 int32_t audio;
876 CHECK(msg->findInt32("audio", &audio));
877
Steve Block3856b092011-10-20 11:56:00 +0100878 ALOGV("renderer %s flush completed.", audio ? "audio" : "video");
Andy Hung8d121d42014-10-03 09:53:53 -0700879 handleFlushComplete(audio, false /* isDecoder */);
880 finishFlushIfPossible();
James Dongf57b4ea2012-07-20 13:38:36 -0700881 } else if (what == Renderer::kWhatVideoRenderingStart) {
882 notifyListener(MEDIA_INFO, MEDIA_INFO_RENDERING_START, 0);
Lajos Molnarcbaffcf2013-08-14 18:30:38 -0700883 } else if (what == Renderer::kWhatMediaRenderingStart) {
884 ALOGV("media rendering started");
885 notifyListener(MEDIA_STARTED, 0, 0);
Wei Jia3a2956d2014-07-22 16:01:33 -0700886 } else if (what == Renderer::kWhatAudioOffloadTearDown) {
Ronghua Wua10fd232014-11-06 16:15:20 -0800887 ALOGV("Tear down audio offload, fall back to s/w path if due to error.");
Wei Jia3a2956d2014-07-22 16:01:33 -0700888 int64_t positionUs;
889 CHECK(msg->findInt64("positionUs", &positionUs));
Ronghua Wu08529172014-10-02 16:55:52 -0700890 int32_t reason;
891 CHECK(msg->findInt32("reason", &reason));
Andy Hung282a7e32014-08-14 15:56:34 -0700892 closeAudioSink();
Wei Jia3a2956d2014-07-22 16:01:33 -0700893 mAudioDecoder.clear();
Wei Jia57568df2014-09-22 10:16:29 -0700894 ++mAudioDecoderGeneration;
Chong Zhang7137ec72014-11-12 16:41:05 -0800895 mRenderer->flush(
896 true /* audio */, false /* notifyComplete */);
Wei Jia3a2956d2014-07-22 16:01:33 -0700897 if (mVideoDecoder != NULL) {
Chong Zhang7137ec72014-11-12 16:41:05 -0800898 mRenderer->flush(
899 false /* audio */, false /* notifyComplete */);
Wei Jia3a2956d2014-07-22 16:01:33 -0700900 }
Wei Jia3a2956d2014-07-22 16:01:33 -0700901
Wei Jiae427abf2014-09-22 15:21:11 -0700902 performSeek(positionUs, false /* needNotify */);
Ronghua Wu08529172014-10-02 16:55:52 -0700903 if (reason == Renderer::kDueToError) {
Ronghua Wua10fd232014-11-06 16:15:20 -0800904 mRenderer->signalDisableOffloadAudio();
905 mOffloadAudio = false;
Ronghua Wu08529172014-10-02 16:55:52 -0700906 instantiateDecoder(true /* audio */, &mAudioDecoder);
907 }
Andreas Huberf9334412010-12-15 15:17:42 -0800908 }
909 break;
910 }
911
912 case kWhatMoreDataQueued:
913 {
914 break;
915 }
916
Andreas Huber1aef2112011-01-04 14:01:29 -0800917 case kWhatReset:
918 {
Steve Block3856b092011-10-20 11:56:00 +0100919 ALOGV("kWhatReset");
Andreas Huber1aef2112011-01-04 14:01:29 -0800920
Andreas Hubera1f8ab02012-11-30 10:53:22 -0800921 mDeferredActions.push_back(
Wei Jiafef808d2014-10-31 17:57:05 -0700922 new FlushDecoderAction(
923 FLUSH_CMD_SHUTDOWN /* audio */,
924 FLUSH_CMD_SHUTDOWN /* video */));
Andreas Huberb7c8e912012-11-27 15:02:53 -0800925
Andreas Hubera1f8ab02012-11-30 10:53:22 -0800926 mDeferredActions.push_back(
927 new SimpleAction(&NuPlayer::performReset));
Andreas Huberb58ce9f2011-11-28 16:27:35 -0800928
Andreas Hubera1f8ab02012-11-30 10:53:22 -0800929 processDeferredActions();
Andreas Huber1aef2112011-01-04 14:01:29 -0800930 break;
931 }
932
Andreas Huber43c3e6c2011-01-05 12:17:08 -0800933 case kWhatSeek:
934 {
935 int64_t seekTimeUs;
Wei Jiae427abf2014-09-22 15:21:11 -0700936 int32_t needNotify;
Andreas Huber43c3e6c2011-01-05 12:17:08 -0800937 CHECK(msg->findInt64("seekTimeUs", &seekTimeUs));
Wei Jiae427abf2014-09-22 15:21:11 -0700938 CHECK(msg->findInt32("needNotify", &needNotify));
Andreas Huber43c3e6c2011-01-05 12:17:08 -0800939
Wei Jiae427abf2014-09-22 15:21:11 -0700940 ALOGV("kWhatSeek seekTimeUs=%lld us, needNotify=%d",
941 seekTimeUs, needNotify);
Andreas Huber43c3e6c2011-01-05 12:17:08 -0800942
Andreas Hubera1f8ab02012-11-30 10:53:22 -0800943 mDeferredActions.push_back(
Wei Jiafef808d2014-10-31 17:57:05 -0700944 new FlushDecoderAction(FLUSH_CMD_FLUSH /* audio */,
945 FLUSH_CMD_FLUSH /* video */));
Andreas Huber43c3e6c2011-01-05 12:17:08 -0800946
Wei Jiae427abf2014-09-22 15:21:11 -0700947 mDeferredActions.push_back(
948 new SeekAction(seekTimeUs, needNotify));
Andreas Huber43c3e6c2011-01-05 12:17:08 -0800949
Chong Zhangf8d71772014-11-26 15:08:34 -0800950 // After a flush without shutdown, decoder is paused.
951 // Don't resume it until source seek is done, otherwise it could
Chong Zhang7137ec72014-11-12 16:41:05 -0800952 // start pulling stale data too soon.
953 mDeferredActions.push_back(
Chong Zhangf8d71772014-11-26 15:08:34 -0800954 new ResumeDecoderAction(needNotify));
Chong Zhang7137ec72014-11-12 16:41:05 -0800955
Andreas Hubera1f8ab02012-11-30 10:53:22 -0800956 processDeferredActions();
Andreas Huber43c3e6c2011-01-05 12:17:08 -0800957 break;
958 }
959
Andreas Huberb4082222011-01-20 15:23:04 -0800960 case kWhatPause:
961 {
Chong Zhangefbb6192015-01-30 17:13:27 -0800962 onPause();
963 mPausedByClient = true;
Andreas Huberb4082222011-01-20 15:23:04 -0800964 break;
965 }
966
Andreas Huberb5f25f02013-02-05 10:14:26 -0800967 case kWhatSourceNotify:
968 {
Andreas Huber9575c962013-02-05 13:59:56 -0800969 onSourceNotify(msg);
Andreas Huberb5f25f02013-02-05 10:14:26 -0800970 break;
971 }
972
Chong Zhanga7fa1d92014-06-11 14:49:23 -0700973 case kWhatClosedCaptionNotify:
974 {
975 onClosedCaptionNotify(msg);
976 break;
977 }
978
Andreas Huberf9334412010-12-15 15:17:42 -0800979 default:
980 TRESPASS();
981 break;
982 }
983}
984
Wei Jia94211742014-10-28 17:09:06 -0700985void NuPlayer::onResume() {
Chong Zhangefbb6192015-01-30 17:13:27 -0800986 if (!mPaused) {
987 return;
988 }
989 mPaused = false;
Wei Jia94211742014-10-28 17:09:06 -0700990 if (mSource != NULL) {
991 mSource->resume();
992 } else {
993 ALOGW("resume called when source is gone or not set");
994 }
995 // |mAudioDecoder| may have been released due to the pause timeout, so re-create it if
996 // needed.
997 if (audioDecoderStillNeeded() && mAudioDecoder == NULL) {
998 instantiateDecoder(true /* audio */, &mAudioDecoder);
999 }
1000 if (mRenderer != NULL) {
1001 mRenderer->resume();
1002 } else {
1003 ALOGW("resume called when renderer is gone or not set");
1004 }
1005}
1006
1007void NuPlayer::onStart() {
Wei Jia94211742014-10-28 17:09:06 -07001008 mOffloadAudio = false;
1009 mAudioEOS = false;
1010 mVideoEOS = false;
Wei Jia94211742014-10-28 17:09:06 -07001011 mStarted = true;
1012
1013 /* instantiate decoders now for secure playback */
1014 if (mSourceFlags & Source::FLAG_SECURE) {
1015 if (mNativeWindow != NULL) {
1016 instantiateDecoder(false, &mVideoDecoder);
1017 }
1018
1019 if (mAudioSink != NULL) {
1020 instantiateDecoder(true, &mAudioDecoder);
1021 }
1022 }
1023
1024 mSource->start();
1025
1026 uint32_t flags = 0;
1027
1028 if (mSource->isRealTime()) {
1029 flags |= Renderer::FLAG_REAL_TIME;
1030 }
1031
1032 sp<MetaData> audioMeta = mSource->getFormatMeta(true /* audio */);
1033 audio_stream_type_t streamType = AUDIO_STREAM_MUSIC;
1034 if (mAudioSink != NULL) {
1035 streamType = mAudioSink->getAudioStreamType();
1036 }
1037
1038 sp<AMessage> videoFormat = mSource->getFormat(false /* audio */);
1039
1040 mOffloadAudio =
1041 canOffloadStream(audioMeta, (videoFormat != NULL),
1042 true /* is_streaming */, streamType);
1043 if (mOffloadAudio) {
1044 flags |= Renderer::FLAG_OFFLOAD_AUDIO;
1045 }
1046
1047 sp<AMessage> notify = new AMessage(kWhatRendererNotify, id());
1048 ++mRendererGeneration;
1049 notify->setInt32("generation", mRendererGeneration);
1050 mRenderer = new Renderer(mAudioSink, notify, flags);
1051
1052 mRendererLooper = new ALooper;
1053 mRendererLooper->setName("NuPlayerRenderer");
1054 mRendererLooper->start(false, false, ANDROID_PRIORITY_AUDIO);
1055 mRendererLooper->registerHandler(mRenderer);
1056
1057 sp<MetaData> meta = getFileMeta();
1058 int32_t rate;
1059 if (meta != NULL
1060 && meta->findInt32(kKeyFrameRate, &rate) && rate > 0) {
1061 mRenderer->setVideoFrameRate(rate);
1062 }
1063
Wei Jiac6cfd702014-11-11 16:33:20 -08001064 if (mVideoDecoder != NULL) {
1065 mVideoDecoder->setRenderer(mRenderer);
1066 }
1067 if (mAudioDecoder != NULL) {
1068 mAudioDecoder->setRenderer(mRenderer);
1069 }
1070
Wei Jia94211742014-10-28 17:09:06 -07001071 postScanSources();
1072}
1073
Chong Zhangefbb6192015-01-30 17:13:27 -08001074void NuPlayer::onPause() {
1075 if (mPaused) {
1076 return;
1077 }
1078 mPaused = true;
1079 if (mSource != NULL) {
1080 mSource->pause();
1081 } else {
1082 ALOGW("pause called when source is gone or not set");
1083 }
1084 if (mRenderer != NULL) {
1085 mRenderer->pause();
1086 } else {
1087 ALOGW("pause called when renderer is gone or not set");
1088 }
1089}
1090
Ronghua Wud7988b12014-10-03 15:19:10 -07001091bool NuPlayer::audioDecoderStillNeeded() {
1092 // Audio decoder is no longer needed if it's in shut/shutting down status.
1093 return ((mFlushingAudio != SHUT_DOWN) && (mFlushingAudio != SHUTTING_DOWN_DECODER));
1094}
1095
Andy Hung8d121d42014-10-03 09:53:53 -07001096void NuPlayer::handleFlushComplete(bool audio, bool isDecoder) {
1097 // We wait for both the decoder flush and the renderer flush to complete
1098 // before entering either the FLUSHED or the SHUTTING_DOWN_DECODER state.
1099
1100 mFlushComplete[audio][isDecoder] = true;
1101 if (!mFlushComplete[audio][!isDecoder]) {
1102 return;
1103 }
1104
1105 FlushStatus *state = audio ? &mFlushingAudio : &mFlushingVideo;
1106 switch (*state) {
1107 case FLUSHING_DECODER:
1108 {
1109 *state = FLUSHED;
Andy Hung8d121d42014-10-03 09:53:53 -07001110 break;
1111 }
1112
1113 case FLUSHING_DECODER_SHUTDOWN:
1114 {
1115 *state = SHUTTING_DOWN_DECODER;
1116
1117 ALOGV("initiating %s decoder shutdown", audio ? "audio" : "video");
1118 if (!audio) {
Andy Hung8d121d42014-10-03 09:53:53 -07001119 // Widevine source reads must stop before releasing the video decoder.
1120 if (mSource != NULL && mSourceFlags & Source::FLAG_SECURE) {
1121 mSource->stop();
1122 }
1123 }
1124 getDecoder(audio)->initiateShutdown();
1125 break;
1126 }
1127
1128 default:
1129 // decoder flush completes only occur in a flushing state.
1130 LOG_ALWAYS_FATAL_IF(isDecoder, "decoder flush in invalid state %d", *state);
1131 break;
1132 }
1133}
1134
Andreas Huber3831a062010-12-21 10:22:33 -08001135void NuPlayer::finishFlushIfPossible() {
Wei Jia53904f32014-07-29 10:22:53 -07001136 if (mFlushingAudio != NONE && mFlushingAudio != FLUSHED
1137 && mFlushingAudio != SHUT_DOWN) {
Andreas Huber3831a062010-12-21 10:22:33 -08001138 return;
1139 }
1140
Wei Jia53904f32014-07-29 10:22:53 -07001141 if (mFlushingVideo != NONE && mFlushingVideo != FLUSHED
1142 && mFlushingVideo != SHUT_DOWN) {
Andreas Huber3831a062010-12-21 10:22:33 -08001143 return;
1144 }
1145
Steve Block3856b092011-10-20 11:56:00 +01001146 ALOGV("both audio and video are flushed now.");
Andreas Huber3831a062010-12-21 10:22:33 -08001147
Andreas Huber3831a062010-12-21 10:22:33 -08001148 mFlushingAudio = NONE;
1149 mFlushingVideo = NONE;
Andreas Huber3831a062010-12-21 10:22:33 -08001150
Andy Hung8d121d42014-10-03 09:53:53 -07001151 clearFlushComplete();
1152
Andreas Hubera1f8ab02012-11-30 10:53:22 -08001153 processDeferredActions();
Andreas Huber1aef2112011-01-04 14:01:29 -08001154}
1155
1156void NuPlayer::postScanSources() {
1157 if (mScanSourcesPending) {
1158 return;
1159 }
1160
1161 sp<AMessage> msg = new AMessage(kWhatScanSources, id());
1162 msg->setInt32("generation", mScanSourcesGeneration);
1163 msg->post();
1164
1165 mScanSourcesPending = true;
1166}
1167
Andy Hung202bce12014-12-03 11:47:36 -08001168void NuPlayer::tryOpenAudioSinkForOffload(const sp<AMessage> &format, bool hasVideo) {
1169 // Note: This is called early in NuPlayer to determine whether offloading
1170 // is possible; otherwise the decoders call the renderer openAudioSink directly.
Andy Hung282a7e32014-08-14 15:56:34 -07001171
Andy Hung202bce12014-12-03 11:47:36 -08001172 status_t err = mRenderer->openAudioSink(
1173 format, true /* offloadOnly */, hasVideo, AUDIO_OUTPUT_FLAG_NONE, &mOffloadAudio);
1174 if (err != OK) {
1175 // Any failure we turn off mOffloadAudio.
1176 mOffloadAudio = false;
1177 } else if (mOffloadAudio) {
Chong Zhang3b9eb1f2014-10-15 17:05:08 -07001178 sp<MetaData> audioMeta =
1179 mSource->getFormatMeta(true /* audio */);
1180 sendMetaDataToHal(mAudioSink, audioMeta);
Andy Hung282a7e32014-08-14 15:56:34 -07001181 }
1182}
1183
1184void NuPlayer::closeAudioSink() {
Chong Zhang3b9eb1f2014-10-15 17:05:08 -07001185 mRenderer->closeAudioSink();
Andy Hung282a7e32014-08-14 15:56:34 -07001186}
1187
Chong Zhang7137ec72014-11-12 16:41:05 -08001188status_t NuPlayer::instantiateDecoder(bool audio, sp<DecoderBase> *decoder) {
Andreas Huberf9334412010-12-15 15:17:42 -08001189 if (*decoder != NULL) {
1190 return OK;
1191 }
1192
Andreas Huber84066782011-08-16 09:34:26 -07001193 sp<AMessage> format = mSource->getFormat(audio);
Andreas Huberf9334412010-12-15 15:17:42 -08001194
Andreas Huber84066782011-08-16 09:34:26 -07001195 if (format == NULL) {
Andreas Huberf9334412010-12-15 15:17:42 -08001196 return -EWOULDBLOCK;
1197 }
1198
Andreas Huber3fe62152011-09-16 15:09:22 -07001199 if (!audio) {
Andreas Huber84066782011-08-16 09:34:26 -07001200 AString mime;
1201 CHECK(format->findString("mime", &mime));
Chong Zhanga7fa1d92014-06-11 14:49:23 -07001202
1203 sp<AMessage> ccNotify = new AMessage(kWhatClosedCaptionNotify, id());
1204 mCCDecoder = new CCDecoder(ccNotify);
Lajos Molnar09524832014-07-17 14:29:51 -07001205
1206 if (mSourceFlags & Source::FLAG_SECURE) {
1207 format->setInt32("secure", true);
1208 }
Chong Zhang17134602015-01-07 16:14:34 -08001209
1210 if (mSourceFlags & Source::FLAG_PROTECTED) {
1211 format->setInt32("protected", true);
1212 }
Andreas Huber3fe62152011-09-16 15:09:22 -07001213 }
1214
Wei Jiabc2fb722014-07-08 16:37:57 -07001215 if (audio) {
Wei Jia88703c32014-08-06 11:24:07 -07001216 sp<AMessage> notify = new AMessage(kWhatAudioNotify, id());
1217 ++mAudioDecoderGeneration;
1218 notify->setInt32("generation", mAudioDecoderGeneration);
1219
Wei Jiabc2fb722014-07-08 16:37:57 -07001220 if (mOffloadAudio) {
Wei Jiac6cfd702014-11-11 16:33:20 -08001221 *decoder = new DecoderPassThrough(notify, mSource, mRenderer);
Wei Jiabc2fb722014-07-08 16:37:57 -07001222 } else {
Wei Jiac6cfd702014-11-11 16:33:20 -08001223 *decoder = new Decoder(notify, mSource, mRenderer);
Wei Jiabc2fb722014-07-08 16:37:57 -07001224 }
1225 } else {
Wei Jia88703c32014-08-06 11:24:07 -07001226 sp<AMessage> notify = new AMessage(kWhatVideoNotify, id());
1227 ++mVideoDecoderGeneration;
1228 notify->setInt32("generation", mVideoDecoderGeneration);
1229
Chong Zhang7137ec72014-11-12 16:41:05 -08001230 *decoder = new Decoder(
1231 notify, mSource, mRenderer, mNativeWindow, mCCDecoder);
Lajos Molnard9fd6312014-11-06 11:00:00 -08001232
1233 // enable FRC if high-quality AV sync is requested, even if not
1234 // queuing to native window, as this will even improve textureview
1235 // playback.
1236 {
1237 char value[PROPERTY_VALUE_MAX];
1238 if (property_get("persist.sys.media.avsync", value, NULL) &&
1239 (!strcmp("1", value) || !strcasecmp("true", value))) {
1240 format->setInt32("auto-frc", 1);
1241 }
1242 }
Wei Jiabc2fb722014-07-08 16:37:57 -07001243 }
Lajos Molnar1cd13982014-01-17 15:12:51 -08001244 (*decoder)->init();
Andreas Huber84066782011-08-16 09:34:26 -07001245 (*decoder)->configure(format);
Andreas Huberf9334412010-12-15 15:17:42 -08001246
Lajos Molnar09524832014-07-17 14:29:51 -07001247 // allocate buffers to decrypt widevine source buffers
1248 if (!audio && (mSourceFlags & Source::FLAG_SECURE)) {
1249 Vector<sp<ABuffer> > inputBufs;
1250 CHECK_EQ((*decoder)->getInputBuffers(&inputBufs), (status_t)OK);
1251
1252 Vector<MediaBuffer *> mediaBufs;
1253 for (size_t i = 0; i < inputBufs.size(); i++) {
1254 const sp<ABuffer> &buffer = inputBufs[i];
1255 MediaBuffer *mbuf = new MediaBuffer(buffer->data(), buffer->size());
1256 mediaBufs.push(mbuf);
1257 }
1258
1259 status_t err = mSource->setBuffers(audio, mediaBufs);
1260 if (err != OK) {
1261 for (size_t i = 0; i < mediaBufs.size(); ++i) {
1262 mediaBufs[i]->release();
1263 }
1264 mediaBufs.clear();
1265 ALOGE("Secure source didn't support secure mediaBufs.");
1266 return err;
1267 }
1268 }
Andreas Huberf9334412010-12-15 15:17:42 -08001269 return OK;
1270}
1271
Chong Zhangced1c2f2014-08-08 15:22:35 -07001272void NuPlayer::updateVideoSize(
1273 const sp<AMessage> &inputFormat,
1274 const sp<AMessage> &outputFormat) {
1275 if (inputFormat == NULL) {
1276 ALOGW("Unknown video size, reporting 0x0!");
1277 notifyListener(MEDIA_SET_VIDEO_SIZE, 0, 0);
1278 return;
1279 }
1280
1281 int32_t displayWidth, displayHeight;
1282 int32_t cropLeft, cropTop, cropRight, cropBottom;
1283
1284 if (outputFormat != NULL) {
1285 int32_t width, height;
1286 CHECK(outputFormat->findInt32("width", &width));
1287 CHECK(outputFormat->findInt32("height", &height));
1288
1289 int32_t cropLeft, cropTop, cropRight, cropBottom;
1290 CHECK(outputFormat->findRect(
1291 "crop",
1292 &cropLeft, &cropTop, &cropRight, &cropBottom));
1293
1294 displayWidth = cropRight - cropLeft + 1;
1295 displayHeight = cropBottom - cropTop + 1;
1296
1297 ALOGV("Video output format changed to %d x %d "
1298 "(crop: %d x %d @ (%d, %d))",
1299 width, height,
1300 displayWidth,
1301 displayHeight,
1302 cropLeft, cropTop);
1303 } else {
1304 CHECK(inputFormat->findInt32("width", &displayWidth));
1305 CHECK(inputFormat->findInt32("height", &displayHeight));
1306
1307 ALOGV("Video input format %d x %d", displayWidth, displayHeight);
1308 }
1309
1310 // Take into account sample aspect ratio if necessary:
1311 int32_t sarWidth, sarHeight;
1312 if (inputFormat->findInt32("sar-width", &sarWidth)
1313 && inputFormat->findInt32("sar-height", &sarHeight)) {
1314 ALOGV("Sample aspect ratio %d : %d", sarWidth, sarHeight);
1315
1316 displayWidth = (displayWidth * sarWidth) / sarHeight;
1317
1318 ALOGV("display dimensions %d x %d", displayWidth, displayHeight);
1319 }
1320
1321 int32_t rotationDegrees;
1322 if (!inputFormat->findInt32("rotation-degrees", &rotationDegrees)) {
1323 rotationDegrees = 0;
1324 }
1325
1326 if (rotationDegrees == 90 || rotationDegrees == 270) {
1327 int32_t tmp = displayWidth;
1328 displayWidth = displayHeight;
1329 displayHeight = tmp;
1330 }
1331
1332 notifyListener(
1333 MEDIA_SET_VIDEO_SIZE,
1334 displayWidth,
1335 displayHeight);
1336}
1337
Chong Zhangdcb89b32013-08-06 09:44:47 -07001338void NuPlayer::notifyListener(int msg, int ext1, int ext2, const Parcel *in) {
Andreas Huber43c3e6c2011-01-05 12:17:08 -08001339 if (mDriver == NULL) {
Andreas Huberf9334412010-12-15 15:17:42 -08001340 return;
1341 }
1342
Andreas Huber43c3e6c2011-01-05 12:17:08 -08001343 sp<NuPlayerDriver> driver = mDriver.promote();
Andreas Huberf9334412010-12-15 15:17:42 -08001344
Andreas Huber43c3e6c2011-01-05 12:17:08 -08001345 if (driver == NULL) {
Andreas Huberf9334412010-12-15 15:17:42 -08001346 return;
1347 }
1348
Chong Zhangdcb89b32013-08-06 09:44:47 -07001349 driver->notifyListener(msg, ext1, ext2, in);
Andreas Huberf9334412010-12-15 15:17:42 -08001350}
1351
Chong Zhang7137ec72014-11-12 16:41:05 -08001352void NuPlayer::flushDecoder(bool audio, bool needShutdown) {
Andreas Huber14f76722013-01-15 09:04:18 -08001353 ALOGV("[%s] flushDecoder needShutdown=%d",
1354 audio ? "audio" : "video", needShutdown);
1355
Chong Zhang7137ec72014-11-12 16:41:05 -08001356 const sp<DecoderBase> &decoder = getDecoder(audio);
Lajos Molnar87603c02014-08-20 19:25:30 -07001357 if (decoder == NULL) {
Steve Blockdf64d152012-01-04 20:05:49 +00001358 ALOGI("flushDecoder %s without decoder present",
Andreas Huber6e3d3112011-11-28 12:36:11 -08001359 audio ? "audio" : "video");
Lajos Molnar87603c02014-08-20 19:25:30 -07001360 return;
Andreas Huber6e3d3112011-11-28 12:36:11 -08001361 }
1362
Andreas Huber1aef2112011-01-04 14:01:29 -08001363 // Make sure we don't continue to scan sources until we finish flushing.
1364 ++mScanSourcesGeneration;
Andreas Huber43c3e6c2011-01-05 12:17:08 -08001365 mScanSourcesPending = false;
Andreas Huber1aef2112011-01-04 14:01:29 -08001366
Chong Zhang7137ec72014-11-12 16:41:05 -08001367 decoder->signalFlush();
Andreas Huber1aef2112011-01-04 14:01:29 -08001368
1369 FlushStatus newStatus =
1370 needShutdown ? FLUSHING_DECODER_SHUTDOWN : FLUSHING_DECODER;
1371
Andy Hung8d121d42014-10-03 09:53:53 -07001372 mFlushComplete[audio][false /* isDecoder */] = false;
1373 mFlushComplete[audio][true /* isDecoder */] = false;
Andreas Huber1aef2112011-01-04 14:01:29 -08001374 if (audio) {
Wei Jia53904f32014-07-29 10:22:53 -07001375 ALOGE_IF(mFlushingAudio != NONE,
1376 "audio flushDecoder() is called in state %d", mFlushingAudio);
Andreas Huber1aef2112011-01-04 14:01:29 -08001377 mFlushingAudio = newStatus;
Andreas Huber1aef2112011-01-04 14:01:29 -08001378 } else {
Wei Jia53904f32014-07-29 10:22:53 -07001379 ALOGE_IF(mFlushingVideo != NONE,
1380 "video flushDecoder() is called in state %d", mFlushingVideo);
Andreas Huber1aef2112011-01-04 14:01:29 -08001381 mFlushingVideo = newStatus;
Andreas Huber1aef2112011-01-04 14:01:29 -08001382 }
1383}
1384
Chong Zhangced1c2f2014-08-08 15:22:35 -07001385void NuPlayer::queueDecoderShutdown(
1386 bool audio, bool video, const sp<AMessage> &reply) {
1387 ALOGI("queueDecoderShutdown audio=%d, video=%d", audio, video);
Andreas Huber84066782011-08-16 09:34:26 -07001388
Chong Zhangced1c2f2014-08-08 15:22:35 -07001389 mDeferredActions.push_back(
Wei Jiafef808d2014-10-31 17:57:05 -07001390 new FlushDecoderAction(
1391 audio ? FLUSH_CMD_SHUTDOWN : FLUSH_CMD_NONE,
1392 video ? FLUSH_CMD_SHUTDOWN : FLUSH_CMD_NONE));
Andreas Huber84066782011-08-16 09:34:26 -07001393
Chong Zhangced1c2f2014-08-08 15:22:35 -07001394 mDeferredActions.push_back(
1395 new SimpleAction(&NuPlayer::performScanSources));
Andreas Huber84066782011-08-16 09:34:26 -07001396
Chong Zhangced1c2f2014-08-08 15:22:35 -07001397 mDeferredActions.push_back(new PostMessageAction(reply));
1398
1399 processDeferredActions();
Andreas Huber84066782011-08-16 09:34:26 -07001400}
1401
James Dong0d268a32012-08-31 12:18:27 -07001402status_t NuPlayer::setVideoScalingMode(int32_t mode) {
1403 mVideoScalingMode = mode;
Andreas Huber57a339c2012-12-03 11:18:00 -08001404 if (mNativeWindow != NULL) {
James Dong0d268a32012-08-31 12:18:27 -07001405 status_t ret = native_window_set_scaling_mode(
1406 mNativeWindow->getNativeWindow().get(), mVideoScalingMode);
1407 if (ret != OK) {
1408 ALOGE("Failed to set scaling mode (%d): %s",
1409 -ret, strerror(-ret));
1410 return ret;
1411 }
1412 }
1413 return OK;
1414}
1415
Chong Zhangdcb89b32013-08-06 09:44:47 -07001416status_t NuPlayer::getTrackInfo(Parcel* reply) const {
1417 sp<AMessage> msg = new AMessage(kWhatGetTrackInfo, id());
1418 msg->setPointer("reply", reply);
1419
1420 sp<AMessage> response;
1421 status_t err = msg->postAndAwaitResponse(&response);
1422 return err;
1423}
1424
Robert Shih7c4f0d72014-07-09 18:53:31 -07001425status_t NuPlayer::getSelectedTrack(int32_t type, Parcel* reply) const {
1426 sp<AMessage> msg = new AMessage(kWhatGetSelectedTrack, id());
1427 msg->setPointer("reply", reply);
1428 msg->setInt32("type", type);
1429
1430 sp<AMessage> response;
1431 status_t err = msg->postAndAwaitResponse(&response);
1432 if (err == OK && response != NULL) {
1433 CHECK(response->findInt32("err", &err));
1434 }
1435 return err;
1436}
1437
Robert Shih6ffb1fd2014-10-29 16:24:32 -07001438status_t NuPlayer::selectTrack(size_t trackIndex, bool select, int64_t timeUs) {
Chong Zhangdcb89b32013-08-06 09:44:47 -07001439 sp<AMessage> msg = new AMessage(kWhatSelectTrack, id());
1440 msg->setSize("trackIndex", trackIndex);
1441 msg->setInt32("select", select);
Robert Shih6ffb1fd2014-10-29 16:24:32 -07001442 msg->setInt64("timeUs", timeUs);
Chong Zhangdcb89b32013-08-06 09:44:47 -07001443
1444 sp<AMessage> response;
1445 status_t err = msg->postAndAwaitResponse(&response);
1446
Chong Zhang404fced2014-06-11 14:45:31 -07001447 if (err != OK) {
1448 return err;
1449 }
1450
1451 if (!response->findInt32("err", &err)) {
1452 err = OK;
1453 }
1454
Chong Zhangdcb89b32013-08-06 09:44:47 -07001455 return err;
1456}
1457
Ronghua Wua73d9e02014-10-08 15:13:29 -07001458status_t NuPlayer::getCurrentPosition(int64_t *mediaUs) {
1459 sp<Renderer> renderer = mRenderer;
1460 if (renderer == NULL) {
1461 return NO_INIT;
1462 }
1463
1464 return renderer->getCurrentPosition(mediaUs);
1465}
1466
1467void NuPlayer::getStats(int64_t *numFramesTotal, int64_t *numFramesDropped) {
Chong Zhang7137ec72014-11-12 16:41:05 -08001468 sp<DecoderBase> decoder = getDecoder(false /* audio */);
1469 if (decoder != NULL) {
1470 decoder->getStats(numFramesTotal, numFramesDropped);
1471 } else {
1472 *numFramesTotal = 0;
1473 *numFramesDropped = 0;
1474 }
Ronghua Wua73d9e02014-10-08 15:13:29 -07001475}
1476
Marco Nelissenf0b72b52014-09-16 15:43:44 -07001477sp<MetaData> NuPlayer::getFileMeta() {
1478 return mSource->getFileFormatMeta();
1479}
1480
Andreas Huberb7c8e912012-11-27 15:02:53 -08001481void NuPlayer::schedulePollDuration() {
1482 sp<AMessage> msg = new AMessage(kWhatPollDuration, id());
1483 msg->setInt32("generation", mPollDurationGeneration);
1484 msg->post();
1485}
1486
1487void NuPlayer::cancelPollDuration() {
1488 ++mPollDurationGeneration;
1489}
1490
Andreas Hubera1f8ab02012-11-30 10:53:22 -08001491void NuPlayer::processDeferredActions() {
1492 while (!mDeferredActions.empty()) {
1493 // We won't execute any deferred actions until we're no longer in
1494 // an intermediate state, i.e. one more more decoders are currently
1495 // flushing or shutting down.
1496
Andreas Hubera1f8ab02012-11-30 10:53:22 -08001497 if (mFlushingAudio != NONE || mFlushingVideo != NONE) {
1498 // We're currently flushing, postpone the reset until that's
1499 // completed.
1500
1501 ALOGV("postponing action mFlushingAudio=%d, mFlushingVideo=%d",
1502 mFlushingAudio, mFlushingVideo);
1503
1504 break;
1505 }
1506
1507 sp<Action> action = *mDeferredActions.begin();
1508 mDeferredActions.erase(mDeferredActions.begin());
1509
1510 action->execute(this);
1511 }
1512}
1513
Wei Jiae427abf2014-09-22 15:21:11 -07001514void NuPlayer::performSeek(int64_t seekTimeUs, bool needNotify) {
1515 ALOGV("performSeek seekTimeUs=%lld us (%.2f secs), needNotify(%d)",
Andreas Hubera1f8ab02012-11-30 10:53:22 -08001516 seekTimeUs,
Wei Jiae427abf2014-09-22 15:21:11 -07001517 seekTimeUs / 1E6,
1518 needNotify);
Andreas Hubera1f8ab02012-11-30 10:53:22 -08001519
Andy Hungadf34bf2014-09-03 18:22:22 -07001520 if (mSource == NULL) {
1521 // This happens when reset occurs right before the loop mode
1522 // asynchronously seeks to the start of the stream.
1523 LOG_ALWAYS_FATAL_IF(mAudioDecoder != NULL || mVideoDecoder != NULL,
1524 "mSource is NULL and decoders not NULL audio(%p) video(%p)",
1525 mAudioDecoder.get(), mVideoDecoder.get());
1526 return;
1527 }
Andreas Hubera1f8ab02012-11-30 10:53:22 -08001528 mSource->seekTo(seekTimeUs);
Robert Shihd3b0bbb2014-07-23 15:00:25 -07001529 ++mTimedTextGeneration;
Andreas Hubera1f8ab02012-11-30 10:53:22 -08001530
Andreas Hubera1f8ab02012-11-30 10:53:22 -08001531 // everything's flushed, continue playback.
1532}
1533
Wei Jiafef808d2014-10-31 17:57:05 -07001534void NuPlayer::performDecoderFlush(FlushCommand audio, FlushCommand video) {
1535 ALOGV("performDecoderFlush audio=%d, video=%d", audio, video);
Andreas Hubera1f8ab02012-11-30 10:53:22 -08001536
Wei Jiafef808d2014-10-31 17:57:05 -07001537 if ((audio == FLUSH_CMD_NONE || mAudioDecoder == NULL)
1538 && (video == FLUSH_CMD_NONE || mVideoDecoder == NULL)) {
Andreas Hubera1f8ab02012-11-30 10:53:22 -08001539 return;
1540 }
1541
Wei Jiafef808d2014-10-31 17:57:05 -07001542 if (audio != FLUSH_CMD_NONE && mAudioDecoder != NULL) {
1543 flushDecoder(true /* audio */, (audio == FLUSH_CMD_SHUTDOWN));
Andreas Hubera1f8ab02012-11-30 10:53:22 -08001544 }
1545
Wei Jiafef808d2014-10-31 17:57:05 -07001546 if (video != FLUSH_CMD_NONE && mVideoDecoder != NULL) {
1547 flushDecoder(false /* audio */, (video == FLUSH_CMD_SHUTDOWN));
Andreas Hubera1f8ab02012-11-30 10:53:22 -08001548 }
1549}
1550
1551void NuPlayer::performReset() {
1552 ALOGV("performReset");
1553
1554 CHECK(mAudioDecoder == NULL);
1555 CHECK(mVideoDecoder == NULL);
1556
1557 cancelPollDuration();
1558
1559 ++mScanSourcesGeneration;
1560 mScanSourcesPending = false;
1561
Lajos Molnar09524832014-07-17 14:29:51 -07001562 if (mRendererLooper != NULL) {
1563 if (mRenderer != NULL) {
1564 mRendererLooper->unregisterHandler(mRenderer->id());
1565 }
1566 mRendererLooper->stop();
1567 mRendererLooper.clear();
1568 }
Andreas Hubera1f8ab02012-11-30 10:53:22 -08001569 mRenderer.clear();
Wei Jia57568df2014-09-22 10:16:29 -07001570 ++mRendererGeneration;
Andreas Hubera1f8ab02012-11-30 10:53:22 -08001571
1572 if (mSource != NULL) {
1573 mSource->stop();
Andreas Huberb5f25f02013-02-05 10:14:26 -08001574
Andreas Hubera1f8ab02012-11-30 10:53:22 -08001575 mSource.clear();
1576 }
1577
1578 if (mDriver != NULL) {
1579 sp<NuPlayerDriver> driver = mDriver.promote();
1580 if (driver != NULL) {
1581 driver->notifyResetComplete();
1582 }
1583 }
Andreas Huber57a339c2012-12-03 11:18:00 -08001584
1585 mStarted = false;
Andreas Hubera1f8ab02012-11-30 10:53:22 -08001586}
1587
1588void NuPlayer::performScanSources() {
1589 ALOGV("performScanSources");
1590
Andreas Huber57a339c2012-12-03 11:18:00 -08001591 if (!mStarted) {
1592 return;
1593 }
1594
Andreas Hubera1f8ab02012-11-30 10:53:22 -08001595 if (mAudioDecoder == NULL || mVideoDecoder == NULL) {
1596 postScanSources();
1597 }
1598}
1599
Andreas Huber57a339c2012-12-03 11:18:00 -08001600void NuPlayer::performSetSurface(const sp<NativeWindowWrapper> &wrapper) {
1601 ALOGV("performSetSurface");
1602
1603 mNativeWindow = wrapper;
1604
1605 // XXX - ignore error from setVideoScalingMode for now
1606 setVideoScalingMode(mVideoScalingMode);
Chong Zhang13d6faa2014-08-22 15:35:28 -07001607
1608 if (mDriver != NULL) {
1609 sp<NuPlayerDriver> driver = mDriver.promote();
1610 if (driver != NULL) {
1611 driver->notifySetSurfaceComplete();
1612 }
1613 }
Andreas Huber57a339c2012-12-03 11:18:00 -08001614}
1615
Chong Zhangf8d71772014-11-26 15:08:34 -08001616void NuPlayer::performResumeDecoders(bool needNotify) {
1617 if (needNotify) {
1618 mResumePending = true;
1619 if (mVideoDecoder == NULL) {
1620 // if audio-only, we can notify seek complete now,
1621 // as the resume operation will be relatively fast.
1622 finishResume();
1623 }
1624 }
1625
Chong Zhang7137ec72014-11-12 16:41:05 -08001626 if (mVideoDecoder != NULL) {
Chong Zhangf8d71772014-11-26 15:08:34 -08001627 // When there is continuous seek, MediaPlayer will cache the seek
1628 // position, and send down new seek request when previous seek is
1629 // complete. Let's wait for at least one video output frame before
1630 // notifying seek complete, so that the video thumbnail gets updated
1631 // when seekbar is dragged.
1632 mVideoDecoder->signalResume(needNotify);
Chong Zhang7137ec72014-11-12 16:41:05 -08001633 }
1634
1635 if (mAudioDecoder != NULL) {
Chong Zhangf8d71772014-11-26 15:08:34 -08001636 mAudioDecoder->signalResume(false /* needNotify */);
1637 }
1638}
1639
1640void NuPlayer::finishResume() {
1641 if (mResumePending) {
1642 mResumePending = false;
1643 if (mDriver != NULL) {
1644 sp<NuPlayerDriver> driver = mDriver.promote();
1645 if (driver != NULL) {
1646 driver->notifySeekComplete();
1647 }
1648 }
Chong Zhang7137ec72014-11-12 16:41:05 -08001649 }
1650}
1651
Andreas Huber9575c962013-02-05 13:59:56 -08001652void NuPlayer::onSourceNotify(const sp<AMessage> &msg) {
1653 int32_t what;
1654 CHECK(msg->findInt32("what", &what));
1655
1656 switch (what) {
1657 case Source::kWhatPrepared:
1658 {
Andreas Huberb5f28d42013-04-25 15:11:19 -07001659 if (mSource == NULL) {
1660 // This is a stale notification from a source that was
1661 // asynchronously preparing when the client called reset().
1662 // We handled the reset, the source is gone.
1663 break;
1664 }
1665
Andreas Huberec0c5972013-02-05 14:47:13 -08001666 int32_t err;
1667 CHECK(msg->findInt32("err", &err));
1668
Andreas Huber9575c962013-02-05 13:59:56 -08001669 sp<NuPlayerDriver> driver = mDriver.promote();
1670 if (driver != NULL) {
Marco Nelissendd114d12014-05-28 15:23:14 -07001671 // notify duration first, so that it's definitely set when
1672 // the app received the "prepare complete" callback.
1673 int64_t durationUs;
1674 if (mSource->getDuration(&durationUs) == OK) {
1675 driver->notifyDuration(durationUs);
1676 }
Andreas Huberec0c5972013-02-05 14:47:13 -08001677 driver->notifyPrepareCompleted(err);
Andreas Huber9575c962013-02-05 13:59:56 -08001678 }
Andreas Huber99759402013-04-01 14:28:31 -07001679
Andreas Huber9575c962013-02-05 13:59:56 -08001680 break;
1681 }
1682
1683 case Source::kWhatFlagsChanged:
1684 {
1685 uint32_t flags;
1686 CHECK(msg->findInt32("flags", (int32_t *)&flags));
1687
Chong Zhang4b7069d2013-09-11 12:52:43 -07001688 sp<NuPlayerDriver> driver = mDriver.promote();
1689 if (driver != NULL) {
Wei Jia895651b2014-12-10 17:31:52 -08001690 if ((flags & NuPlayer::Source::FLAG_CAN_SEEK) == 0) {
1691 driver->notifyListener(
1692 MEDIA_INFO, MEDIA_INFO_NOT_SEEKABLE, 0);
1693 }
Chong Zhang4b7069d2013-09-11 12:52:43 -07001694 driver->notifyFlagsChanged(flags);
1695 }
1696
Andreas Huber9575c962013-02-05 13:59:56 -08001697 if ((mSourceFlags & Source::FLAG_DYNAMIC_DURATION)
1698 && (!(flags & Source::FLAG_DYNAMIC_DURATION))) {
1699 cancelPollDuration();
1700 } else if (!(mSourceFlags & Source::FLAG_DYNAMIC_DURATION)
1701 && (flags & Source::FLAG_DYNAMIC_DURATION)
1702 && (mAudioDecoder != NULL || mVideoDecoder != NULL)) {
1703 schedulePollDuration();
1704 }
1705
1706 mSourceFlags = flags;
1707 break;
1708 }
1709
1710 case Source::kWhatVideoSizeChanged:
1711 {
Chong Zhangced1c2f2014-08-08 15:22:35 -07001712 sp<AMessage> format;
1713 CHECK(msg->findMessage("format", &format));
Andreas Huber9575c962013-02-05 13:59:56 -08001714
Chong Zhangced1c2f2014-08-08 15:22:35 -07001715 updateVideoSize(format);
Andreas Huber9575c962013-02-05 13:59:56 -08001716 break;
1717 }
1718
Chong Zhang2a3cc9a2014-08-21 17:48:26 -07001719 case Source::kWhatBufferingUpdate:
1720 {
1721 int32_t percentage;
1722 CHECK(msg->findInt32("percentage", &percentage));
1723
1724 notifyListener(MEDIA_BUFFERING_UPDATE, percentage, 0);
1725 break;
1726 }
1727
Chong Zhangefbb6192015-01-30 17:13:27 -08001728 case Source::kWhatPauseOnBufferingStart:
1729 {
1730 // ignore if not playing
1731 if (mStarted && !mPausedByClient) {
1732 ALOGI("buffer low, pausing...");
1733
1734 onPause();
1735 }
1736 // fall-thru
1737 }
1738
Roger Jönssonb50e83e2013-01-21 16:26:41 +01001739 case Source::kWhatBufferingStart:
1740 {
1741 notifyListener(MEDIA_INFO, MEDIA_INFO_BUFFERING_START, 0);
1742 break;
1743 }
1744
Chong Zhangefbb6192015-01-30 17:13:27 -08001745 case Source::kWhatResumeOnBufferingEnd:
1746 {
1747 // ignore if not playing
1748 if (mStarted && !mPausedByClient) {
1749 ALOGI("buffer ready, resuming...");
1750
1751 onResume();
1752 }
1753 // fall-thru
1754 }
1755
Roger Jönssonb50e83e2013-01-21 16:26:41 +01001756 case Source::kWhatBufferingEnd:
1757 {
1758 notifyListener(MEDIA_INFO, MEDIA_INFO_BUFFERING_END, 0);
1759 break;
1760 }
1761
Chong Zhangefbb6192015-01-30 17:13:27 -08001762 case Source::kWhatCacheStats:
1763 {
1764 int32_t kbps;
1765 CHECK(msg->findInt32("bandwidth", &kbps));
1766
1767 notifyListener(MEDIA_INFO, MEDIA_INFO_NETWORK_BANDWIDTH, kbps);
1768 break;
1769 }
1770
Chong Zhangdcb89b32013-08-06 09:44:47 -07001771 case Source::kWhatSubtitleData:
1772 {
1773 sp<ABuffer> buffer;
1774 CHECK(msg->findBuffer("buffer", &buffer));
1775
Chong Zhang404fced2014-06-11 14:45:31 -07001776 sendSubtitleData(buffer, 0 /* baseIndex */);
Chong Zhangdcb89b32013-08-06 09:44:47 -07001777 break;
1778 }
1779
Robert Shihd3b0bbb2014-07-23 15:00:25 -07001780 case Source::kWhatTimedTextData:
1781 {
1782 int32_t generation;
1783 if (msg->findInt32("generation", &generation)
1784 && generation != mTimedTextGeneration) {
1785 break;
1786 }
1787
1788 sp<ABuffer> buffer;
1789 CHECK(msg->findBuffer("buffer", &buffer));
1790
1791 sp<NuPlayerDriver> driver = mDriver.promote();
1792 if (driver == NULL) {
1793 break;
1794 }
1795
1796 int posMs;
1797 int64_t timeUs, posUs;
1798 driver->getCurrentPosition(&posMs);
1799 posUs = posMs * 1000;
1800 CHECK(buffer->meta()->findInt64("timeUs", &timeUs));
1801
1802 if (posUs < timeUs) {
1803 if (!msg->findInt32("generation", &generation)) {
1804 msg->setInt32("generation", mTimedTextGeneration);
1805 }
1806 msg->post(timeUs - posUs);
1807 } else {
1808 sendTimedTextData(buffer);
1809 }
1810 break;
1811 }
1812
Andreas Huber14f76722013-01-15 09:04:18 -08001813 case Source::kWhatQueueDecoderShutdown:
1814 {
1815 int32_t audio, video;
1816 CHECK(msg->findInt32("audio", &audio));
1817 CHECK(msg->findInt32("video", &video));
1818
1819 sp<AMessage> reply;
1820 CHECK(msg->findMessage("reply", &reply));
1821
1822 queueDecoderShutdown(audio, video, reply);
1823 break;
1824 }
1825
Ronghua Wu80276872014-08-28 15:50:29 -07001826 case Source::kWhatDrmNoLicense:
1827 {
1828 notifyListener(MEDIA_ERROR, MEDIA_ERROR_UNKNOWN, ERROR_DRM_NO_LICENSE);
1829 break;
1830 }
1831
Andreas Huber9575c962013-02-05 13:59:56 -08001832 default:
1833 TRESPASS();
1834 }
1835}
1836
Chong Zhanga7fa1d92014-06-11 14:49:23 -07001837void NuPlayer::onClosedCaptionNotify(const sp<AMessage> &msg) {
1838 int32_t what;
1839 CHECK(msg->findInt32("what", &what));
1840
1841 switch (what) {
1842 case NuPlayer::CCDecoder::kWhatClosedCaptionData:
1843 {
1844 sp<ABuffer> buffer;
1845 CHECK(msg->findBuffer("buffer", &buffer));
1846
1847 size_t inbandTracks = 0;
1848 if (mSource != NULL) {
1849 inbandTracks = mSource->getTrackCount();
1850 }
1851
1852 sendSubtitleData(buffer, inbandTracks);
1853 break;
1854 }
1855
1856 case NuPlayer::CCDecoder::kWhatTrackAdded:
1857 {
1858 notifyListener(MEDIA_INFO, MEDIA_INFO_METADATA_UPDATE, 0);
1859
1860 break;
1861 }
1862
1863 default:
1864 TRESPASS();
1865 }
1866
1867
1868}
1869
Chong Zhang404fced2014-06-11 14:45:31 -07001870void NuPlayer::sendSubtitleData(const sp<ABuffer> &buffer, int32_t baseIndex) {
1871 int32_t trackIndex;
1872 int64_t timeUs, durationUs;
1873 CHECK(buffer->meta()->findInt32("trackIndex", &trackIndex));
1874 CHECK(buffer->meta()->findInt64("timeUs", &timeUs));
1875 CHECK(buffer->meta()->findInt64("durationUs", &durationUs));
1876
1877 Parcel in;
1878 in.writeInt32(trackIndex + baseIndex);
1879 in.writeInt64(timeUs);
1880 in.writeInt64(durationUs);
1881 in.writeInt32(buffer->size());
1882 in.writeInt32(buffer->size());
1883 in.write(buffer->data(), buffer->size());
1884
1885 notifyListener(MEDIA_SUBTITLE_DATA, 0, 0, &in);
1886}
Robert Shihd3b0bbb2014-07-23 15:00:25 -07001887
1888void NuPlayer::sendTimedTextData(const sp<ABuffer> &buffer) {
1889 const void *data;
1890 size_t size = 0;
1891 int64_t timeUs;
1892 int32_t flag = TextDescriptions::LOCAL_DESCRIPTIONS;
1893
1894 AString mime;
1895 CHECK(buffer->meta()->findString("mime", &mime));
1896 CHECK(strcasecmp(mime.c_str(), MEDIA_MIMETYPE_TEXT_3GPP) == 0);
1897
1898 data = buffer->data();
1899 size = buffer->size();
1900
1901 Parcel parcel;
1902 if (size > 0) {
1903 CHECK(buffer->meta()->findInt64("timeUs", &timeUs));
1904 flag |= TextDescriptions::IN_BAND_TEXT_3GPP;
1905 TextDescriptions::getParcelOfDescriptions(
1906 (const uint8_t *)data, size, flag, timeUs / 1000, &parcel);
1907 }
1908
1909 if ((parcel.dataSize() > 0)) {
1910 notifyListener(MEDIA_TIMED_TEXT, 0, 0, &parcel);
1911 } else { // send an empty timed text
1912 notifyListener(MEDIA_TIMED_TEXT, 0, 0);
1913 }
1914}
Andreas Huberb5f25f02013-02-05 10:14:26 -08001915////////////////////////////////////////////////////////////////////////////////
1916
Chong Zhangced1c2f2014-08-08 15:22:35 -07001917sp<AMessage> NuPlayer::Source::getFormat(bool audio) {
1918 sp<MetaData> meta = getFormatMeta(audio);
1919
1920 if (meta == NULL) {
1921 return NULL;
1922 }
1923
1924 sp<AMessage> msg = new AMessage;
1925
1926 if(convertMetaDataToMessage(meta, &msg) == OK) {
1927 return msg;
1928 }
1929 return NULL;
1930}
1931
Andreas Huber9575c962013-02-05 13:59:56 -08001932void NuPlayer::Source::notifyFlagsChanged(uint32_t flags) {
1933 sp<AMessage> notify = dupNotify();
1934 notify->setInt32("what", kWhatFlagsChanged);
1935 notify->setInt32("flags", flags);
1936 notify->post();
1937}
1938
Chong Zhangced1c2f2014-08-08 15:22:35 -07001939void NuPlayer::Source::notifyVideoSizeChanged(const sp<AMessage> &format) {
Andreas Huber9575c962013-02-05 13:59:56 -08001940 sp<AMessage> notify = dupNotify();
1941 notify->setInt32("what", kWhatVideoSizeChanged);
Chong Zhangced1c2f2014-08-08 15:22:35 -07001942 notify->setMessage("format", format);
Andreas Huber9575c962013-02-05 13:59:56 -08001943 notify->post();
1944}
1945
Andreas Huberec0c5972013-02-05 14:47:13 -08001946void NuPlayer::Source::notifyPrepared(status_t err) {
Andreas Huber9575c962013-02-05 13:59:56 -08001947 sp<AMessage> notify = dupNotify();
1948 notify->setInt32("what", kWhatPrepared);
Andreas Huberec0c5972013-02-05 14:47:13 -08001949 notify->setInt32("err", err);
Andreas Huber9575c962013-02-05 13:59:56 -08001950 notify->post();
1951}
1952
Andreas Huber84333e02014-02-07 15:36:10 -08001953void NuPlayer::Source::onMessageReceived(const sp<AMessage> & /* msg */) {
Andreas Huberb5f25f02013-02-05 10:14:26 -08001954 TRESPASS();
1955}
1956
Andreas Huberf9334412010-12-15 15:17:42 -08001957} // namespace android