blob: 857e70345e7d3c3b7ef6ad0b50ae5bfaf9675bf0 [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"
Andreas Huberf9334412010-12-15 15:17:42 -080024#include "NuPlayerDecoder.h"
Andreas Huber43c3e6c2011-01-05 12:17:08 -080025#include "NuPlayerDriver.h"
Andreas Huberf9334412010-12-15 15:17:42 -080026#include "NuPlayerRenderer.h"
Andreas Huber5bc087c2010-12-23 10:27:40 -080027#include "NuPlayerSource.h"
Andreas Huber2bfdd422011-10-11 15:24:07 -070028#include "RTSPSource.h"
Andreas Huber5bc087c2010-12-23 10:27:40 -080029#include "StreamingSource.h"
Andreas Huberafed0e12011-09-20 15:39:58 -070030#include "GenericSource.h"
Andreas Huber5bc087c2010-12-23 10:27:40 -080031
32#include "ATSParser.h"
Andreas Huberf9334412010-12-15 15:17:42 -080033
Andreas Huber3831a062010-12-21 10:22:33 -080034#include <media/stagefright/foundation/hexdump.h>
Andreas Huberf9334412010-12-15 15:17:42 -080035#include <media/stagefright/foundation/ABuffer.h>
36#include <media/stagefright/foundation/ADebug.h>
37#include <media/stagefright/foundation/AMessage.h>
Andreas Huber3fe62152011-09-16 15:09:22 -070038#include <media/stagefright/MediaDefs.h>
Andreas Huberf9334412010-12-15 15:17:42 -080039#include <media/stagefright/MediaErrors.h>
40#include <media/stagefright/MetaData.h>
Andy McFadden8ba01022012-12-18 09:46:54 -080041#include <gui/IGraphicBufferProducer.h>
Andreas Huberf9334412010-12-15 15:17:42 -080042
Andreas Huber3fe62152011-09-16 15:09:22 -070043#include "avc_utils.h"
44
Andreas Huber84066782011-08-16 09:34:26 -070045#include "ESDS.h"
46#include <media/stagefright/Utils.h>
47
Andreas Huberf9334412010-12-15 15:17:42 -080048namespace android {
49
Andreas Hubera1f8ab02012-11-30 10:53:22 -080050struct NuPlayer::Action : public RefBase {
51 Action() {}
52
53 virtual void execute(NuPlayer *player) = 0;
54
55private:
56 DISALLOW_EVIL_CONSTRUCTORS(Action);
57};
58
59struct NuPlayer::SeekAction : public Action {
60 SeekAction(int64_t seekTimeUs)
61 : mSeekTimeUs(seekTimeUs) {
62 }
63
64 virtual void execute(NuPlayer *player) {
65 player->performSeek(mSeekTimeUs);
66 }
67
68private:
69 int64_t mSeekTimeUs;
70
71 DISALLOW_EVIL_CONSTRUCTORS(SeekAction);
72};
73
Andreas Huber57a339c2012-12-03 11:18:00 -080074struct NuPlayer::SetSurfaceAction : public Action {
75 SetSurfaceAction(const sp<NativeWindowWrapper> &wrapper)
76 : mWrapper(wrapper) {
77 }
78
79 virtual void execute(NuPlayer *player) {
80 player->performSetSurface(mWrapper);
81 }
82
83private:
84 sp<NativeWindowWrapper> mWrapper;
85
86 DISALLOW_EVIL_CONSTRUCTORS(SetSurfaceAction);
87};
88
Andreas Huber14f76722013-01-15 09:04:18 -080089struct NuPlayer::ShutdownDecoderAction : public Action {
90 ShutdownDecoderAction(bool audio, bool video)
91 : mAudio(audio),
92 mVideo(video) {
93 }
94
95 virtual void execute(NuPlayer *player) {
96 player->performDecoderShutdown(mAudio, mVideo);
97 }
98
99private:
100 bool mAudio;
101 bool mVideo;
102
103 DISALLOW_EVIL_CONSTRUCTORS(ShutdownDecoderAction);
104};
105
106struct NuPlayer::PostMessageAction : public Action {
107 PostMessageAction(const sp<AMessage> &msg)
108 : mMessage(msg) {
109 }
110
111 virtual void execute(NuPlayer *) {
112 mMessage->post();
113 }
114
115private:
116 sp<AMessage> mMessage;
117
118 DISALLOW_EVIL_CONSTRUCTORS(PostMessageAction);
119};
120
Andreas Hubera1f8ab02012-11-30 10:53:22 -0800121// Use this if there's no state necessary to save in order to execute
122// the action.
123struct NuPlayer::SimpleAction : public Action {
124 typedef void (NuPlayer::*ActionFunc)();
125
126 SimpleAction(ActionFunc func)
127 : mFunc(func) {
128 }
129
130 virtual void execute(NuPlayer *player) {
131 (player->*mFunc)();
132 }
133
134private:
135 ActionFunc mFunc;
136
137 DISALLOW_EVIL_CONSTRUCTORS(SimpleAction);
138};
139
Andreas Huberf9334412010-12-15 15:17:42 -0800140////////////////////////////////////////////////////////////////////////////////
141
142NuPlayer::NuPlayer()
Andreas Huber9b80c2b2011-06-30 15:47:02 -0700143 : mUIDValid(false),
Andreas Huber9575c962013-02-05 13:59:56 -0800144 mSourceFlags(0),
Andreas Huber3fe62152011-09-16 15:09:22 -0700145 mVideoIsAVC(false),
Andreas Huber9b80c2b2011-06-30 15:47:02 -0700146 mAudioEOS(false),
Andreas Huberf9334412010-12-15 15:17:42 -0800147 mVideoEOS(false),
Andreas Huber5bc087c2010-12-23 10:27:40 -0800148 mScanSourcesPending(false),
Andreas Huber1aef2112011-01-04 14:01:29 -0800149 mScanSourcesGeneration(0),
Andreas Huberb7c8e912012-11-27 15:02:53 -0800150 mPollDurationGeneration(0),
Andreas Huber6e3d3112011-11-28 12:36:11 -0800151 mTimeDiscontinuityPending(false),
Andreas Huberf9334412010-12-15 15:17:42 -0800152 mFlushingAudio(NONE),
Andreas Huber1aef2112011-01-04 14:01:29 -0800153 mFlushingVideo(NONE),
Andreas Huber3fe62152011-09-16 15:09:22 -0700154 mSkipRenderingAudioUntilMediaTimeUs(-1ll),
155 mSkipRenderingVideoUntilMediaTimeUs(-1ll),
156 mVideoLateByUs(0ll),
157 mNumFramesTotal(0ll),
James Dong0d268a32012-08-31 12:18:27 -0700158 mNumFramesDropped(0ll),
Andreas Huber57a339c2012-12-03 11:18:00 -0800159 mVideoScalingMode(NATIVE_WINDOW_SCALING_MODE_SCALE_TO_WINDOW),
160 mStarted(false) {
Andreas Huberf9334412010-12-15 15:17:42 -0800161}
162
163NuPlayer::~NuPlayer() {
164}
165
Andreas Huber9b80c2b2011-06-30 15:47:02 -0700166void NuPlayer::setUID(uid_t uid) {
167 mUIDValid = true;
168 mUID = uid;
169}
170
Andreas Huber43c3e6c2011-01-05 12:17:08 -0800171void NuPlayer::setDriver(const wp<NuPlayerDriver> &driver) {
172 mDriver = driver;
Andreas Huberf9334412010-12-15 15:17:42 -0800173}
174
Andreas Huber9575c962013-02-05 13:59:56 -0800175void NuPlayer::setDataSourceAsync(const sp<IStreamSource> &source) {
Andreas Huberf9334412010-12-15 15:17:42 -0800176 sp<AMessage> msg = new AMessage(kWhatSetDataSource, id());
177
Andreas Huberb5f25f02013-02-05 10:14:26 -0800178 sp<AMessage> notify = new AMessage(kWhatSourceNotify, id());
179
Andreas Huber240abcc2014-02-13 13:32:37 -0800180 msg->setObject("source", new StreamingSource(notify, source));
Andreas Huber5bc087c2010-12-23 10:27:40 -0800181 msg->post();
182}
Andreas Huberf9334412010-12-15 15:17:42 -0800183
Andreas Huberafed0e12011-09-20 15:39:58 -0700184static bool IsHTTPLiveURL(const char *url) {
185 if (!strncasecmp("http://", url, 7)
Andreas Huber99759402013-04-01 14:28:31 -0700186 || !strncasecmp("https://", url, 8)
187 || !strncasecmp("file://", url, 7)) {
Andreas Huberafed0e12011-09-20 15:39:58 -0700188 size_t len = strlen(url);
189 if (len >= 5 && !strcasecmp(".m3u8", &url[len - 5])) {
190 return true;
191 }
192
193 if (strstr(url,"m3u8")) {
194 return true;
195 }
196 }
197
198 return false;
199}
200
Andreas Huber9575c962013-02-05 13:59:56 -0800201void NuPlayer::setDataSourceAsync(
Andreas Huber1b86fe02014-01-29 11:13:26 -0800202 const sp<IMediaHTTPService> &httpService,
203 const char *url,
204 const KeyedVector<String8, String8> *headers) {
Andreas Huber5bc087c2010-12-23 10:27:40 -0800205 sp<AMessage> msg = new AMessage(kWhatSetDataSource, id());
Oscar Rydhé7a33b772012-02-20 10:15:48 +0100206 size_t len = strlen(url);
Andreas Huber5bc087c2010-12-23 10:27:40 -0800207
Andreas Huberb5f25f02013-02-05 10:14:26 -0800208 sp<AMessage> notify = new AMessage(kWhatSourceNotify, id());
209
Andreas Huberafed0e12011-09-20 15:39:58 -0700210 sp<Source> source;
211 if (IsHTTPLiveURL(url)) {
Andreas Huber81e68442014-02-05 11:52:33 -0800212 source = new HTTPLiveSource(notify, httpService, url, headers);
Andreas Huberafed0e12011-09-20 15:39:58 -0700213 } else if (!strncasecmp(url, "rtsp://", 7)) {
Andreas Huber1b86fe02014-01-29 11:13:26 -0800214 source = new RTSPSource(
215 notify, httpService, url, headers, mUIDValid, mUID);
Oscar Rydhé7a33b772012-02-20 10:15:48 +0100216 } else if ((!strncasecmp(url, "http://", 7)
217 || !strncasecmp(url, "https://", 8))
218 && ((len >= 4 && !strcasecmp(".sdp", &url[len - 4]))
219 || strstr(url, ".sdp?"))) {
Andreas Huber1b86fe02014-01-29 11:13:26 -0800220 source = new RTSPSource(
221 notify, httpService, url, headers, mUIDValid, mUID, true);
Andreas Huber2bfdd422011-10-11 15:24:07 -0700222 } else {
Andreas Huber81e68442014-02-05 11:52:33 -0800223 source = new GenericSource(notify, httpService, url, headers);
Andreas Huber2bfdd422011-10-11 15:24:07 -0700224 }
225
Andreas Huberafed0e12011-09-20 15:39:58 -0700226 msg->setObject("source", source);
227 msg->post();
228}
229
Andreas Huber9575c962013-02-05 13:59:56 -0800230void NuPlayer::setDataSourceAsync(int fd, int64_t offset, int64_t length) {
Andreas Huberafed0e12011-09-20 15:39:58 -0700231 sp<AMessage> msg = new AMessage(kWhatSetDataSource, id());
232
Andreas Huberb5f25f02013-02-05 10:14:26 -0800233 sp<AMessage> notify = new AMessage(kWhatSourceNotify, id());
234
235 sp<Source> source = new GenericSource(notify, fd, offset, length);
Andreas Huberafed0e12011-09-20 15:39:58 -0700236 msg->setObject("source", source);
Andreas Huberf9334412010-12-15 15:17:42 -0800237 msg->post();
238}
239
Andreas Huber9575c962013-02-05 13:59:56 -0800240void NuPlayer::prepareAsync() {
241 (new AMessage(kWhatPrepare, id()))->post();
242}
243
Andreas Huber57a339c2012-12-03 11:18:00 -0800244void NuPlayer::setVideoSurfaceTextureAsync(
Andy McFadden8ba01022012-12-18 09:46:54 -0800245 const sp<IGraphicBufferProducer> &bufferProducer) {
Glenn Kasten11731182011-02-08 17:26:17 -0800246 sp<AMessage> msg = new AMessage(kWhatSetVideoNativeWindow, id());
Andreas Huber57a339c2012-12-03 11:18:00 -0800247
Andy McFadden8ba01022012-12-18 09:46:54 -0800248 if (bufferProducer == NULL) {
Andreas Huber57a339c2012-12-03 11:18:00 -0800249 msg->setObject("native-window", NULL);
250 } else {
251 msg->setObject(
252 "native-window",
253 new NativeWindowWrapper(
Mathias Agopian1a2952a2013-02-14 17:11:27 -0800254 new Surface(bufferProducer)));
Andreas Huber57a339c2012-12-03 11:18:00 -0800255 }
256
Andreas Huberf9334412010-12-15 15:17:42 -0800257 msg->post();
258}
259
260void NuPlayer::setAudioSink(const sp<MediaPlayerBase::AudioSink> &sink) {
261 sp<AMessage> msg = new AMessage(kWhatSetAudioSink, id());
262 msg->setObject("sink", sink);
263 msg->post();
264}
265
266void NuPlayer::start() {
267 (new AMessage(kWhatStart, id()))->post();
268}
269
Andreas Huber43c3e6c2011-01-05 12:17:08 -0800270void NuPlayer::pause() {
Andreas Huberb4082222011-01-20 15:23:04 -0800271 (new AMessage(kWhatPause, id()))->post();
Andreas Huber43c3e6c2011-01-05 12:17:08 -0800272}
273
274void NuPlayer::resume() {
Andreas Huberb4082222011-01-20 15:23:04 -0800275 (new AMessage(kWhatResume, id()))->post();
Andreas Huber43c3e6c2011-01-05 12:17:08 -0800276}
277
Andreas Huber1aef2112011-01-04 14:01:29 -0800278void NuPlayer::resetAsync() {
279 (new AMessage(kWhatReset, id()))->post();
280}
281
Andreas Huber43c3e6c2011-01-05 12:17:08 -0800282void NuPlayer::seekToAsync(int64_t seekTimeUs) {
283 sp<AMessage> msg = new AMessage(kWhatSeek, id());
284 msg->setInt64("seekTimeUs", seekTimeUs);
285 msg->post();
286}
287
Andreas Huber53df1a42010-12-22 10:03:04 -0800288// static
Andreas Huber1aef2112011-01-04 14:01:29 -0800289bool NuPlayer::IsFlushingState(FlushStatus state, bool *needShutdown) {
Andreas Huber53df1a42010-12-22 10:03:04 -0800290 switch (state) {
291 case FLUSHING_DECODER:
Andreas Huber1aef2112011-01-04 14:01:29 -0800292 if (needShutdown != NULL) {
293 *needShutdown = false;
Andreas Huber53df1a42010-12-22 10:03:04 -0800294 }
295 return true;
296
Andreas Huber1aef2112011-01-04 14:01:29 -0800297 case FLUSHING_DECODER_SHUTDOWN:
298 if (needShutdown != NULL) {
299 *needShutdown = true;
Andreas Huber53df1a42010-12-22 10:03:04 -0800300 }
301 return true;
302
303 default:
304 return false;
305 }
306}
307
Andreas Huberf9334412010-12-15 15:17:42 -0800308void NuPlayer::onMessageReceived(const sp<AMessage> &msg) {
309 switch (msg->what()) {
310 case kWhatSetDataSource:
311 {
Steve Block3856b092011-10-20 11:56:00 +0100312 ALOGV("kWhatSetDataSource");
Andreas Huberf9334412010-12-15 15:17:42 -0800313
314 CHECK(mSource == NULL);
315
Andreas Huber5bc087c2010-12-23 10:27:40 -0800316 sp<RefBase> obj;
317 CHECK(msg->findObject("source", &obj));
Andreas Huberf9334412010-12-15 15:17:42 -0800318
Andreas Huber5bc087c2010-12-23 10:27:40 -0800319 mSource = static_cast<Source *>(obj.get());
Andreas Huberb5f25f02013-02-05 10:14:26 -0800320
321 looper()->registerHandler(mSource);
Andreas Huber9575c962013-02-05 13:59:56 -0800322
323 CHECK(mDriver != NULL);
324 sp<NuPlayerDriver> driver = mDriver.promote();
325 if (driver != NULL) {
326 driver->notifySetDataSourceCompleted(OK);
327 }
328 break;
329 }
330
331 case kWhatPrepare:
332 {
333 mSource->prepareAsync();
Andreas Huberf9334412010-12-15 15:17:42 -0800334 break;
335 }
336
Chong Zhangdcb89b32013-08-06 09:44:47 -0700337 case kWhatGetTrackInfo:
338 {
339 uint32_t replyID;
340 CHECK(msg->senderAwaitsResponse(&replyID));
341
342 status_t err = INVALID_OPERATION;
343 if (mSource != NULL) {
344 Parcel* reply;
345 CHECK(msg->findPointer("reply", (void**)&reply));
346 err = mSource->getTrackInfo(reply);
347 }
348
349 sp<AMessage> response = new AMessage;
350 response->setInt32("err", err);
351
352 response->postReply(replyID);
353 break;
354 }
355
356 case kWhatSelectTrack:
357 {
358 uint32_t replyID;
359 CHECK(msg->senderAwaitsResponse(&replyID));
360
361 status_t err = INVALID_OPERATION;
362 if (mSource != NULL) {
363 size_t trackIndex;
364 int32_t select;
365 CHECK(msg->findSize("trackIndex", &trackIndex));
366 CHECK(msg->findInt32("select", &select));
367 err = mSource->selectTrack(trackIndex, select);
368 }
369
370 sp<AMessage> response = new AMessage;
371 response->setInt32("err", err);
372
373 response->postReply(replyID);
374 break;
375 }
376
Andreas Huberb7c8e912012-11-27 15:02:53 -0800377 case kWhatPollDuration:
378 {
379 int32_t generation;
380 CHECK(msg->findInt32("generation", &generation));
381
382 if (generation != mPollDurationGeneration) {
383 // stale
384 break;
385 }
386
387 int64_t durationUs;
388 if (mDriver != NULL && mSource->getDuration(&durationUs) == OK) {
389 sp<NuPlayerDriver> driver = mDriver.promote();
390 if (driver != NULL) {
391 driver->notifyDuration(durationUs);
392 }
393 }
394
395 msg->post(1000000ll); // poll again in a second.
396 break;
397 }
398
Glenn Kasten11731182011-02-08 17:26:17 -0800399 case kWhatSetVideoNativeWindow:
Andreas Huberf9334412010-12-15 15:17:42 -0800400 {
Steve Block3856b092011-10-20 11:56:00 +0100401 ALOGV("kWhatSetVideoNativeWindow");
Andreas Huberf9334412010-12-15 15:17:42 -0800402
Andreas Huber57a339c2012-12-03 11:18:00 -0800403 mDeferredActions.push_back(
Andreas Huber14f76722013-01-15 09:04:18 -0800404 new ShutdownDecoderAction(
405 false /* audio */, true /* video */));
Andreas Huber57a339c2012-12-03 11:18:00 -0800406
Andreas Huberf9334412010-12-15 15:17:42 -0800407 sp<RefBase> obj;
Glenn Kasten11731182011-02-08 17:26:17 -0800408 CHECK(msg->findObject("native-window", &obj));
Andreas Huberf9334412010-12-15 15:17:42 -0800409
Andreas Huber57a339c2012-12-03 11:18:00 -0800410 mDeferredActions.push_back(
411 new SetSurfaceAction(
412 static_cast<NativeWindowWrapper *>(obj.get())));
James Dong0d268a32012-08-31 12:18:27 -0700413
Andreas Huber57a339c2012-12-03 11:18:00 -0800414 if (obj != NULL) {
415 // If there is a new surface texture, instantiate decoders
416 // again if possible.
417 mDeferredActions.push_back(
418 new SimpleAction(&NuPlayer::performScanSources));
419 }
420
421 processDeferredActions();
Andreas Huberf9334412010-12-15 15:17:42 -0800422 break;
423 }
424
425 case kWhatSetAudioSink:
426 {
Steve Block3856b092011-10-20 11:56:00 +0100427 ALOGV("kWhatSetAudioSink");
Andreas Huberf9334412010-12-15 15:17:42 -0800428
429 sp<RefBase> obj;
430 CHECK(msg->findObject("sink", &obj));
431
432 mAudioSink = static_cast<MediaPlayerBase::AudioSink *>(obj.get());
433 break;
434 }
435
436 case kWhatStart:
437 {
Steve Block3856b092011-10-20 11:56:00 +0100438 ALOGV("kWhatStart");
Andreas Huber43c3e6c2011-01-05 12:17:08 -0800439
Andreas Huber3fe62152011-09-16 15:09:22 -0700440 mVideoIsAVC = false;
Andreas Huber1aef2112011-01-04 14:01:29 -0800441 mAudioEOS = false;
442 mVideoEOS = false;
Andreas Huber32f3cef2011-03-02 15:34:46 -0800443 mSkipRenderingAudioUntilMediaTimeUs = -1;
444 mSkipRenderingVideoUntilMediaTimeUs = -1;
Andreas Huber3fe62152011-09-16 15:09:22 -0700445 mVideoLateByUs = 0;
446 mNumFramesTotal = 0;
447 mNumFramesDropped = 0;
Andreas Huber57a339c2012-12-03 11:18:00 -0800448 mStarted = true;
Andreas Huber1aef2112011-01-04 14:01:29 -0800449
Andreas Huber5bc087c2010-12-23 10:27:40 -0800450 mSource->start();
Andreas Huberf9334412010-12-15 15:17:42 -0800451
Andreas Huberd5e56232013-03-12 11:01:43 -0700452 uint32_t flags = 0;
453
454 if (mSource->isRealTime()) {
455 flags |= Renderer::FLAG_REAL_TIME;
456 }
457
Andreas Huberf9334412010-12-15 15:17:42 -0800458 mRenderer = new Renderer(
459 mAudioSink,
Andreas Huberd5e56232013-03-12 11:01:43 -0700460 new AMessage(kWhatRendererNotify, id()),
461 flags);
Andreas Huberf9334412010-12-15 15:17:42 -0800462
463 looper()->registerHandler(mRenderer);
464
Andreas Huber1aef2112011-01-04 14:01:29 -0800465 postScanSources();
Andreas Huberf9334412010-12-15 15:17:42 -0800466 break;
467 }
468
469 case kWhatScanSources:
470 {
Andreas Huber1aef2112011-01-04 14:01:29 -0800471 int32_t generation;
472 CHECK(msg->findInt32("generation", &generation));
473 if (generation != mScanSourcesGeneration) {
474 // Drop obsolete msg.
475 break;
476 }
477
Andreas Huber5bc087c2010-12-23 10:27:40 -0800478 mScanSourcesPending = false;
479
Steve Block3856b092011-10-20 11:56:00 +0100480 ALOGV("scanning sources haveAudio=%d, haveVideo=%d",
Andreas Huber43c3e6c2011-01-05 12:17:08 -0800481 mAudioDecoder != NULL, mVideoDecoder != NULL);
482
Andreas Huberb7c8e912012-11-27 15:02:53 -0800483 bool mHadAnySourcesBefore =
484 (mAudioDecoder != NULL) || (mVideoDecoder != NULL);
485
Haynes Mathew George5d246ef2012-07-09 10:36:57 -0700486 if (mNativeWindow != NULL) {
487 instantiateDecoder(false, &mVideoDecoder);
488 }
Andreas Huberf9334412010-12-15 15:17:42 -0800489
490 if (mAudioSink != NULL) {
Andreas Huber5bc087c2010-12-23 10:27:40 -0800491 instantiateDecoder(true, &mAudioDecoder);
Andreas Huberf9334412010-12-15 15:17:42 -0800492 }
493
Andreas Huberb7c8e912012-11-27 15:02:53 -0800494 if (!mHadAnySourcesBefore
495 && (mAudioDecoder != NULL || mVideoDecoder != NULL)) {
496 // This is the first time we've found anything playable.
497
Andreas Huber9575c962013-02-05 13:59:56 -0800498 if (mSourceFlags & Source::FLAG_DYNAMIC_DURATION) {
Andreas Huberb7c8e912012-11-27 15:02:53 -0800499 schedulePollDuration();
500 }
501 }
502
Andreas Hubereac68ba2011-09-27 12:12:25 -0700503 status_t err;
504 if ((err = mSource->feedMoreTSData()) != OK) {
Andreas Huber1aef2112011-01-04 14:01:29 -0800505 if (mAudioDecoder == NULL && mVideoDecoder == NULL) {
506 // We're not currently decoding anything (no audio or
507 // video tracks found) and we just ran out of input data.
Andreas Hubereac68ba2011-09-27 12:12:25 -0700508
509 if (err == ERROR_END_OF_STREAM) {
510 notifyListener(MEDIA_PLAYBACK_COMPLETE, 0, 0);
511 } else {
512 notifyListener(MEDIA_ERROR, MEDIA_ERROR_UNKNOWN, err);
513 }
Andreas Huber1aef2112011-01-04 14:01:29 -0800514 }
Andreas Huberf9334412010-12-15 15:17:42 -0800515 break;
516 }
517
Andreas Huberfbe9d812012-08-31 14:05:27 -0700518 if ((mAudioDecoder == NULL && mAudioSink != NULL)
519 || (mVideoDecoder == NULL && mNativeWindow != NULL)) {
Andreas Huberf9334412010-12-15 15:17:42 -0800520 msg->post(100000ll);
Andreas Huber5bc087c2010-12-23 10:27:40 -0800521 mScanSourcesPending = true;
Andreas Huberf9334412010-12-15 15:17:42 -0800522 }
523 break;
524 }
525
526 case kWhatVideoNotify:
527 case kWhatAudioNotify:
528 {
529 bool audio = msg->what() == kWhatAudioNotify;
530
Andreas Huberf9334412010-12-15 15:17:42 -0800531 int32_t what;
Lajos Molnar1cd13982014-01-17 15:12:51 -0800532 CHECK(msg->findInt32("what", &what));
Andreas Huberf9334412010-12-15 15:17:42 -0800533
Lajos Molnar1cd13982014-01-17 15:12:51 -0800534 if (what == Decoder::kWhatFillThisBuffer) {
Andreas Huberf9334412010-12-15 15:17:42 -0800535 status_t err = feedDecoderInputData(
Lajos Molnar1cd13982014-01-17 15:12:51 -0800536 audio, msg);
Andreas Huberf9334412010-12-15 15:17:42 -0800537
Andreas Huber5bc087c2010-12-23 10:27:40 -0800538 if (err == -EWOULDBLOCK) {
Andreas Hubereac68ba2011-09-27 12:12:25 -0700539 if (mSource->feedMoreTSData() == OK) {
Andreas Huber1183a4a2011-11-03 11:00:21 -0700540 msg->post(10000ll);
Andreas Huber5bc087c2010-12-23 10:27:40 -0800541 }
Andreas Huberf9334412010-12-15 15:17:42 -0800542 }
Lajos Molnar1cd13982014-01-17 15:12:51 -0800543 } else if (what == Decoder::kWhatEOS) {
Andreas Huberdc9bacd2011-09-26 10:53:29 -0700544 int32_t err;
Lajos Molnar1cd13982014-01-17 15:12:51 -0800545 CHECK(msg->findInt32("err", &err));
Andreas Huberdc9bacd2011-09-26 10:53:29 -0700546
547 if (err == ERROR_END_OF_STREAM) {
Steve Block3856b092011-10-20 11:56:00 +0100548 ALOGV("got %s decoder EOS", audio ? "audio" : "video");
Andreas Huberdc9bacd2011-09-26 10:53:29 -0700549 } else {
Steve Block3856b092011-10-20 11:56:00 +0100550 ALOGV("got %s decoder EOS w/ error %d",
Andreas Huberdc9bacd2011-09-26 10:53:29 -0700551 audio ? "audio" : "video",
552 err);
553 }
554
555 mRenderer->queueEOS(audio, err);
Lajos Molnar1cd13982014-01-17 15:12:51 -0800556 } else if (what == Decoder::kWhatFlushCompleted) {
Andreas Huber1aef2112011-01-04 14:01:29 -0800557 bool needShutdown;
Andreas Huber53df1a42010-12-22 10:03:04 -0800558
Andreas Huberf9334412010-12-15 15:17:42 -0800559 if (audio) {
Andreas Huber1aef2112011-01-04 14:01:29 -0800560 CHECK(IsFlushingState(mFlushingAudio, &needShutdown));
Andreas Huberf9334412010-12-15 15:17:42 -0800561 mFlushingAudio = FLUSHED;
562 } else {
Andreas Huber1aef2112011-01-04 14:01:29 -0800563 CHECK(IsFlushingState(mFlushingVideo, &needShutdown));
Andreas Huberf9334412010-12-15 15:17:42 -0800564 mFlushingVideo = FLUSHED;
Andreas Huber3fe62152011-09-16 15:09:22 -0700565
566 mVideoLateByUs = 0;
Andreas Huberf9334412010-12-15 15:17:42 -0800567 }
568
Steve Block3856b092011-10-20 11:56:00 +0100569 ALOGV("decoder %s flush completed", audio ? "audio" : "video");
Andreas Huberf9334412010-12-15 15:17:42 -0800570
Andreas Huber1aef2112011-01-04 14:01:29 -0800571 if (needShutdown) {
Steve Block3856b092011-10-20 11:56:00 +0100572 ALOGV("initiating %s decoder shutdown",
Andreas Huber53df1a42010-12-22 10:03:04 -0800573 audio ? "audio" : "video");
Andreas Huberf9334412010-12-15 15:17:42 -0800574
Andreas Huber53df1a42010-12-22 10:03:04 -0800575 (audio ? mAudioDecoder : mVideoDecoder)->initiateShutdown();
Andreas Huberf9334412010-12-15 15:17:42 -0800576
Andreas Huber53df1a42010-12-22 10:03:04 -0800577 if (audio) {
578 mFlushingAudio = SHUTTING_DOWN_DECODER;
579 } else {
580 mFlushingVideo = SHUTTING_DOWN_DECODER;
581 }
Andreas Huberf9334412010-12-15 15:17:42 -0800582 }
Andreas Huber3831a062010-12-21 10:22:33 -0800583
584 finishFlushIfPossible();
Lajos Molnar1cd13982014-01-17 15:12:51 -0800585 } else if (what == Decoder::kWhatOutputFormatChanged) {
586 sp<AMessage> format;
587 CHECK(msg->findMessage("format", &format));
588
Andreas Huber31e25082011-01-10 10:38:31 -0800589 if (audio) {
590 int32_t numChannels;
Lajos Molnar1cd13982014-01-17 15:12:51 -0800591 CHECK(format->findInt32(
Andreas Huber516dacf2012-12-03 15:20:40 -0800592 "channel-count", &numChannels));
Andreas Huber2c2814b2010-12-15 17:18:20 -0800593
Andreas Huber31e25082011-01-10 10:38:31 -0800594 int32_t sampleRate;
Lajos Molnar1cd13982014-01-17 15:12:51 -0800595 CHECK(format->findInt32("sample-rate", &sampleRate));
Andreas Huber2c2814b2010-12-15 17:18:20 -0800596
Steve Block3856b092011-10-20 11:56:00 +0100597 ALOGV("Audio output format changed to %d Hz, %d channels",
Andreas Huber31e25082011-01-10 10:38:31 -0800598 sampleRate, numChannels);
Andreas Huber2c2814b2010-12-15 17:18:20 -0800599
Andreas Huber31e25082011-01-10 10:38:31 -0800600 mAudioSink->close();
Eric Laurent1948eb32012-04-13 16:50:19 -0700601
602 audio_output_flags_t flags;
603 int64_t durationUs;
Andreas Huber516dacf2012-12-03 15:20:40 -0800604 // FIXME: we should handle the case where the video decoder
605 // is created after we receive the format change indication.
606 // Current code will just make that we select deep buffer
607 // with video which should not be a problem as it should
Eric Laurent1948eb32012-04-13 16:50:19 -0700608 // not prevent from keeping A/V sync.
609 if (mVideoDecoder == NULL &&
610 mSource->getDuration(&durationUs) == OK &&
Andreas Huber516dacf2012-12-03 15:20:40 -0800611 durationUs
612 > AUDIO_SINK_MIN_DEEP_BUFFER_DURATION_US) {
Eric Laurent1948eb32012-04-13 16:50:19 -0700613 flags = AUDIO_OUTPUT_FLAG_DEEP_BUFFER;
614 } else {
615 flags = AUDIO_OUTPUT_FLAG_NONE;
616 }
617
Andreas Huber98065552012-05-03 11:33:01 -0700618 int32_t channelMask;
Lajos Molnar1cd13982014-01-17 15:12:51 -0800619 if (!format->findInt32("channel-mask", &channelMask)) {
Andreas Huber98065552012-05-03 11:33:01 -0700620 channelMask = CHANNEL_MASK_USE_CHANNEL_ORDER;
621 }
622
Andreas Huber078cfcf2011-09-15 12:25:04 -0700623 CHECK_EQ(mAudioSink->open(
624 sampleRate,
625 numChannels,
Andreas Huber98065552012-05-03 11:33:01 -0700626 (audio_channel_mask_t)channelMask,
Andreas Huber078cfcf2011-09-15 12:25:04 -0700627 AUDIO_FORMAT_PCM_16_BIT,
Eric Laurent1948eb32012-04-13 16:50:19 -0700628 8 /* bufferCount */,
629 NULL,
630 NULL,
631 flags),
Andreas Huber078cfcf2011-09-15 12:25:04 -0700632 (status_t)OK);
Andreas Huber31e25082011-01-10 10:38:31 -0800633 mAudioSink->start();
Andreas Huber2c2814b2010-12-15 17:18:20 -0800634
Andreas Huber31e25082011-01-10 10:38:31 -0800635 mRenderer->signalAudioSinkChanged();
636 } else {
637 // video
Andreas Huber3831a062010-12-21 10:22:33 -0800638
Andreas Huber31e25082011-01-10 10:38:31 -0800639 int32_t width, height;
Lajos Molnar1cd13982014-01-17 15:12:51 -0800640 CHECK(format->findInt32("width", &width));
641 CHECK(format->findInt32("height", &height));
Andreas Huber31e25082011-01-10 10:38:31 -0800642
643 int32_t cropLeft, cropTop, cropRight, cropBottom;
Lajos Molnar1cd13982014-01-17 15:12:51 -0800644 CHECK(format->findRect(
Andreas Huber31e25082011-01-10 10:38:31 -0800645 "crop",
646 &cropLeft, &cropTop, &cropRight, &cropBottom));
647
Andreas Huber516dacf2012-12-03 15:20:40 -0800648 int32_t displayWidth = cropRight - cropLeft + 1;
649 int32_t displayHeight = cropBottom - cropTop + 1;
650
Steve Block3856b092011-10-20 11:56:00 +0100651 ALOGV("Video output format changed to %d x %d "
Andreas Hubercb67cd12011-08-26 16:02:19 -0700652 "(crop: %d x %d @ (%d, %d))",
Andreas Huber31e25082011-01-10 10:38:31 -0800653 width, height,
Andreas Huber516dacf2012-12-03 15:20:40 -0800654 displayWidth,
655 displayHeight,
Andreas Hubercb67cd12011-08-26 16:02:19 -0700656 cropLeft, cropTop);
Andreas Huber31e25082011-01-10 10:38:31 -0800657
Andreas Huber516dacf2012-12-03 15:20:40 -0800658 sp<AMessage> videoInputFormat =
659 mSource->getFormat(false /* audio */);
660
661 // Take into account sample aspect ratio if necessary:
662 int32_t sarWidth, sarHeight;
663 if (videoInputFormat->findInt32("sar-width", &sarWidth)
664 && videoInputFormat->findInt32(
665 "sar-height", &sarHeight)) {
666 ALOGV("Sample aspect ratio %d : %d",
667 sarWidth, sarHeight);
668
669 displayWidth = (displayWidth * sarWidth) / sarHeight;
670
671 ALOGV("display dimensions %d x %d",
672 displayWidth, displayHeight);
673 }
674
Andreas Huber31e25082011-01-10 10:38:31 -0800675 notifyListener(
Andreas Huber516dacf2012-12-03 15:20:40 -0800676 MEDIA_SET_VIDEO_SIZE, displayWidth, displayHeight);
Andreas Huber31e25082011-01-10 10:38:31 -0800677 }
Lajos Molnar1cd13982014-01-17 15:12:51 -0800678 } else if (what == Decoder::kWhatShutdownCompleted) {
Steve Block3856b092011-10-20 11:56:00 +0100679 ALOGV("%s shutdown completed", audio ? "audio" : "video");
Andreas Huber3831a062010-12-21 10:22:33 -0800680 if (audio) {
681 mAudioDecoder.clear();
682
683 CHECK_EQ((int)mFlushingAudio, (int)SHUTTING_DOWN_DECODER);
684 mFlushingAudio = SHUT_DOWN;
685 } else {
686 mVideoDecoder.clear();
687
688 CHECK_EQ((int)mFlushingVideo, (int)SHUTTING_DOWN_DECODER);
689 mFlushingVideo = SHUT_DOWN;
690 }
691
692 finishFlushIfPossible();
Lajos Molnar1cd13982014-01-17 15:12:51 -0800693 } else if (what == Decoder::kWhatError) {
Steve Block29357bc2012-01-06 19:20:56 +0000694 ALOGE("Received error from %s decoder, aborting playback.",
Andreas Huberc92fd242011-08-16 13:48:44 -0700695 audio ? "audio" : "video");
696
697 mRenderer->queueEOS(audio, UNKNOWN_ERROR);
Lajos Molnar1cd13982014-01-17 15:12:51 -0800698 } else if (what == Decoder::kWhatDrainThisBuffer) {
699 renderBuffer(audio, msg);
700 } else {
701 ALOGV("Unhandled decoder notification %d '%c%c%c%c'.",
Andreas Hubera1f8ab02012-11-30 10:53:22 -0800702 what,
703 what >> 24,
704 (what >> 16) & 0xff,
705 (what >> 8) & 0xff,
706 what & 0xff);
Andreas Huberf9334412010-12-15 15:17:42 -0800707 }
708
709 break;
710 }
711
712 case kWhatRendererNotify:
713 {
714 int32_t what;
715 CHECK(msg->findInt32("what", &what));
716
717 if (what == Renderer::kWhatEOS) {
718 int32_t audio;
719 CHECK(msg->findInt32("audio", &audio));
720
Andreas Huberc92fd242011-08-16 13:48:44 -0700721 int32_t finalResult;
722 CHECK(msg->findInt32("finalResult", &finalResult));
723
Andreas Huberf9334412010-12-15 15:17:42 -0800724 if (audio) {
725 mAudioEOS = true;
726 } else {
727 mVideoEOS = true;
728 }
729
Andreas Huberc92fd242011-08-16 13:48:44 -0700730 if (finalResult == ERROR_END_OF_STREAM) {
Steve Block3856b092011-10-20 11:56:00 +0100731 ALOGV("reached %s EOS", audio ? "audio" : "video");
Andreas Huberc92fd242011-08-16 13:48:44 -0700732 } else {
Steve Block29357bc2012-01-06 19:20:56 +0000733 ALOGE("%s track encountered an error (%d)",
Andreas Huberc92fd242011-08-16 13:48:44 -0700734 audio ? "audio" : "video", finalResult);
735
736 notifyListener(
737 MEDIA_ERROR, MEDIA_ERROR_UNKNOWN, finalResult);
738 }
Andreas Huberf9334412010-12-15 15:17:42 -0800739
740 if ((mAudioEOS || mAudioDecoder == NULL)
741 && (mVideoEOS || mVideoDecoder == NULL)) {
742 notifyListener(MEDIA_PLAYBACK_COMPLETE, 0, 0);
743 }
Andreas Huber43c3e6c2011-01-05 12:17:08 -0800744 } else if (what == Renderer::kWhatPosition) {
745 int64_t positionUs;
746 CHECK(msg->findInt64("positionUs", &positionUs));
747
Andreas Huber3fe62152011-09-16 15:09:22 -0700748 CHECK(msg->findInt64("videoLateByUs", &mVideoLateByUs));
749
Andreas Huber43c3e6c2011-01-05 12:17:08 -0800750 if (mDriver != NULL) {
751 sp<NuPlayerDriver> driver = mDriver.promote();
752 if (driver != NULL) {
753 driver->notifyPosition(positionUs);
Andreas Huber3fe62152011-09-16 15:09:22 -0700754
755 driver->notifyFrameStats(
756 mNumFramesTotal, mNumFramesDropped);
Andreas Huber43c3e6c2011-01-05 12:17:08 -0800757 }
758 }
Andreas Huber3fe62152011-09-16 15:09:22 -0700759 } else if (what == Renderer::kWhatFlushComplete) {
Andreas Huberf9334412010-12-15 15:17:42 -0800760 int32_t audio;
761 CHECK(msg->findInt32("audio", &audio));
762
Steve Block3856b092011-10-20 11:56:00 +0100763 ALOGV("renderer %s flush completed.", audio ? "audio" : "video");
James Dongf57b4ea2012-07-20 13:38:36 -0700764 } else if (what == Renderer::kWhatVideoRenderingStart) {
765 notifyListener(MEDIA_INFO, MEDIA_INFO_RENDERING_START, 0);
Lajos Molnarcbaffcf2013-08-14 18:30:38 -0700766 } else if (what == Renderer::kWhatMediaRenderingStart) {
767 ALOGV("media rendering started");
768 notifyListener(MEDIA_STARTED, 0, 0);
Andreas Huberf9334412010-12-15 15:17:42 -0800769 }
770 break;
771 }
772
773 case kWhatMoreDataQueued:
774 {
775 break;
776 }
777
Andreas Huber1aef2112011-01-04 14:01:29 -0800778 case kWhatReset:
779 {
Steve Block3856b092011-10-20 11:56:00 +0100780 ALOGV("kWhatReset");
Andreas Huber1aef2112011-01-04 14:01:29 -0800781
Andreas Hubera1f8ab02012-11-30 10:53:22 -0800782 mDeferredActions.push_back(
Andreas Huber14f76722013-01-15 09:04:18 -0800783 new ShutdownDecoderAction(
784 true /* audio */, true /* video */));
Andreas Huberb7c8e912012-11-27 15:02:53 -0800785
Andreas Hubera1f8ab02012-11-30 10:53:22 -0800786 mDeferredActions.push_back(
787 new SimpleAction(&NuPlayer::performReset));
Andreas Huberb58ce9f2011-11-28 16:27:35 -0800788
Andreas Hubera1f8ab02012-11-30 10:53:22 -0800789 processDeferredActions();
Andreas Huber1aef2112011-01-04 14:01:29 -0800790 break;
791 }
792
Andreas Huber43c3e6c2011-01-05 12:17:08 -0800793 case kWhatSeek:
794 {
795 int64_t seekTimeUs;
796 CHECK(msg->findInt64("seekTimeUs", &seekTimeUs));
797
Andreas Hubera1f8ab02012-11-30 10:53:22 -0800798 ALOGV("kWhatSeek seekTimeUs=%lld us", seekTimeUs);
Andreas Huber43c3e6c2011-01-05 12:17:08 -0800799
Andreas Hubera1f8ab02012-11-30 10:53:22 -0800800 mDeferredActions.push_back(
801 new SimpleAction(&NuPlayer::performDecoderFlush));
Andreas Huber43c3e6c2011-01-05 12:17:08 -0800802
Andreas Hubera1f8ab02012-11-30 10:53:22 -0800803 mDeferredActions.push_back(new SeekAction(seekTimeUs));
Andreas Huber43c3e6c2011-01-05 12:17:08 -0800804
Andreas Hubera1f8ab02012-11-30 10:53:22 -0800805 processDeferredActions();
Andreas Huber43c3e6c2011-01-05 12:17:08 -0800806 break;
807 }
808
Andreas Huberb4082222011-01-20 15:23:04 -0800809 case kWhatPause:
810 {
811 CHECK(mRenderer != NULL);
Roger Jönssonfba60da2013-01-21 17:15:45 +0100812 mSource->pause();
Andreas Huberb4082222011-01-20 15:23:04 -0800813 mRenderer->pause();
814 break;
815 }
816
817 case kWhatResume:
818 {
819 CHECK(mRenderer != NULL);
Roger Jönssonfba60da2013-01-21 17:15:45 +0100820 mSource->resume();
Andreas Huberb4082222011-01-20 15:23:04 -0800821 mRenderer->resume();
822 break;
823 }
824
Andreas Huberb5f25f02013-02-05 10:14:26 -0800825 case kWhatSourceNotify:
826 {
Andreas Huber9575c962013-02-05 13:59:56 -0800827 onSourceNotify(msg);
Andreas Huberb5f25f02013-02-05 10:14:26 -0800828 break;
829 }
830
Andreas Huberf9334412010-12-15 15:17:42 -0800831 default:
832 TRESPASS();
833 break;
834 }
835}
836
Andreas Huber3831a062010-12-21 10:22:33 -0800837void NuPlayer::finishFlushIfPossible() {
838 if (mFlushingAudio != FLUSHED && mFlushingAudio != SHUT_DOWN) {
839 return;
840 }
841
842 if (mFlushingVideo != FLUSHED && mFlushingVideo != SHUT_DOWN) {
843 return;
844 }
845
Steve Block3856b092011-10-20 11:56:00 +0100846 ALOGV("both audio and video are flushed now.");
Andreas Huber3831a062010-12-21 10:22:33 -0800847
Andreas Huber6e3d3112011-11-28 12:36:11 -0800848 if (mTimeDiscontinuityPending) {
849 mRenderer->signalTimeDiscontinuity();
850 mTimeDiscontinuityPending = false;
851 }
Andreas Huber3831a062010-12-21 10:22:33 -0800852
Andreas Huber22fc52f2011-01-05 16:24:27 -0800853 if (mAudioDecoder != NULL) {
Andreas Huber3831a062010-12-21 10:22:33 -0800854 mAudioDecoder->signalResume();
855 }
856
Andreas Huber22fc52f2011-01-05 16:24:27 -0800857 if (mVideoDecoder != NULL) {
Andreas Huber3831a062010-12-21 10:22:33 -0800858 mVideoDecoder->signalResume();
859 }
860
861 mFlushingAudio = NONE;
862 mFlushingVideo = NONE;
Andreas Huber3831a062010-12-21 10:22:33 -0800863
Andreas Hubera1f8ab02012-11-30 10:53:22 -0800864 processDeferredActions();
Andreas Huber1aef2112011-01-04 14:01:29 -0800865}
866
867void NuPlayer::postScanSources() {
868 if (mScanSourcesPending) {
869 return;
870 }
871
872 sp<AMessage> msg = new AMessage(kWhatScanSources, id());
873 msg->setInt32("generation", mScanSourcesGeneration);
874 msg->post();
875
876 mScanSourcesPending = true;
877}
878
Andreas Huber5bc087c2010-12-23 10:27:40 -0800879status_t NuPlayer::instantiateDecoder(bool audio, sp<Decoder> *decoder) {
Andreas Huberf9334412010-12-15 15:17:42 -0800880 if (*decoder != NULL) {
881 return OK;
882 }
883
Andreas Huber84066782011-08-16 09:34:26 -0700884 sp<AMessage> format = mSource->getFormat(audio);
Andreas Huberf9334412010-12-15 15:17:42 -0800885
Andreas Huber84066782011-08-16 09:34:26 -0700886 if (format == NULL) {
Andreas Huberf9334412010-12-15 15:17:42 -0800887 return -EWOULDBLOCK;
888 }
889
Andreas Huber3fe62152011-09-16 15:09:22 -0700890 if (!audio) {
Andreas Huber84066782011-08-16 09:34:26 -0700891 AString mime;
892 CHECK(format->findString("mime", &mime));
893 mVideoIsAVC = !strcasecmp(MEDIA_MIMETYPE_VIDEO_AVC, mime.c_str());
Andreas Huber3fe62152011-09-16 15:09:22 -0700894 }
895
Andreas Huberf9334412010-12-15 15:17:42 -0800896 sp<AMessage> notify =
897 new AMessage(audio ? kWhatAudioNotify : kWhatVideoNotify,
898 id());
899
Glenn Kasten11731182011-02-08 17:26:17 -0800900 *decoder = audio ? new Decoder(notify) :
901 new Decoder(notify, mNativeWindow);
Lajos Molnar1cd13982014-01-17 15:12:51 -0800902 (*decoder)->init();
Andreas Huber84066782011-08-16 09:34:26 -0700903 (*decoder)->configure(format);
Andreas Huberf9334412010-12-15 15:17:42 -0800904
Andreas Huberf9334412010-12-15 15:17:42 -0800905 return OK;
906}
907
908status_t NuPlayer::feedDecoderInputData(bool audio, const sp<AMessage> &msg) {
909 sp<AMessage> reply;
910 CHECK(msg->findMessage("reply", &reply));
911
Andreas Huber53df1a42010-12-22 10:03:04 -0800912 if ((audio && IsFlushingState(mFlushingAudio))
913 || (!audio && IsFlushingState(mFlushingVideo))) {
Andreas Huberf9334412010-12-15 15:17:42 -0800914 reply->setInt32("err", INFO_DISCONTINUITY);
915 reply->post();
916 return OK;
917 }
918
919 sp<ABuffer> accessUnit;
Andreas Huberf9334412010-12-15 15:17:42 -0800920
Andreas Huber3fe62152011-09-16 15:09:22 -0700921 bool dropAccessUnit;
922 do {
923 status_t err = mSource->dequeueAccessUnit(audio, &accessUnit);
Andreas Huber5bc087c2010-12-23 10:27:40 -0800924
Andreas Huber3fe62152011-09-16 15:09:22 -0700925 if (err == -EWOULDBLOCK) {
926 return err;
927 } else if (err != OK) {
928 if (err == INFO_DISCONTINUITY) {
929 int32_t type;
930 CHECK(accessUnit->meta()->findInt32("discontinuity", &type));
Andreas Huber53df1a42010-12-22 10:03:04 -0800931
Andreas Huber3fe62152011-09-16 15:09:22 -0700932 bool formatChange =
Andreas Huber6e3d3112011-11-28 12:36:11 -0800933 (audio &&
934 (type & ATSParser::DISCONTINUITY_AUDIO_FORMAT))
935 || (!audio &&
936 (type & ATSParser::DISCONTINUITY_VIDEO_FORMAT));
Andreas Huber53df1a42010-12-22 10:03:04 -0800937
Andreas Huber6e3d3112011-11-28 12:36:11 -0800938 bool timeChange = (type & ATSParser::DISCONTINUITY_TIME) != 0;
939
Steve Blockdf64d152012-01-04 20:05:49 +0000940 ALOGI("%s discontinuity (formatChange=%d, time=%d)",
Andreas Huber6e3d3112011-11-28 12:36:11 -0800941 audio ? "audio" : "video", formatChange, timeChange);
Andreas Huber32f3cef2011-03-02 15:34:46 -0800942
Andreas Huber3fe62152011-09-16 15:09:22 -0700943 if (audio) {
944 mSkipRenderingAudioUntilMediaTimeUs = -1;
945 } else {
946 mSkipRenderingVideoUntilMediaTimeUs = -1;
947 }
Andreas Huber32f3cef2011-03-02 15:34:46 -0800948
Andreas Huber6e3d3112011-11-28 12:36:11 -0800949 if (timeChange) {
950 sp<AMessage> extra;
951 if (accessUnit->meta()->findMessage("extra", &extra)
952 && extra != NULL) {
953 int64_t resumeAtMediaTimeUs;
954 if (extra->findInt64(
955 "resume-at-mediatimeUs", &resumeAtMediaTimeUs)) {
Steve Blockdf64d152012-01-04 20:05:49 +0000956 ALOGI("suppressing rendering of %s until %lld us",
Andreas Huber6e3d3112011-11-28 12:36:11 -0800957 audio ? "audio" : "video", resumeAtMediaTimeUs);
Andreas Huber3fe62152011-09-16 15:09:22 -0700958
Andreas Huber6e3d3112011-11-28 12:36:11 -0800959 if (audio) {
960 mSkipRenderingAudioUntilMediaTimeUs =
961 resumeAtMediaTimeUs;
962 } else {
963 mSkipRenderingVideoUntilMediaTimeUs =
964 resumeAtMediaTimeUs;
965 }
Andreas Huber3fe62152011-09-16 15:09:22 -0700966 }
Andreas Huber32f3cef2011-03-02 15:34:46 -0800967 }
968 }
Andreas Huber3fe62152011-09-16 15:09:22 -0700969
Andreas Huber6e3d3112011-11-28 12:36:11 -0800970 mTimeDiscontinuityPending =
971 mTimeDiscontinuityPending || timeChange;
972
973 if (formatChange || timeChange) {
Andreas Hubera1f8ab02012-11-30 10:53:22 -0800974 if (mFlushingAudio == NONE && mFlushingVideo == NONE) {
975 // And we'll resume scanning sources once we're done
976 // flushing.
977 mDeferredActions.push_front(
978 new SimpleAction(
979 &NuPlayer::performScanSources));
980 }
981
Robert Shih6d0a94e2014-01-23 16:18:22 -0800982 sp<AMessage> newFormat = mSource->getFormat(audio);
983 sp<Decoder> &decoder = audio ? mAudioDecoder : mVideoDecoder;
984 if (formatChange && !decoder->supportsSeamlessFormatChange(newFormat)) {
985 flushDecoder(audio, /* needShutdown = */ true);
986 } else {
987 flushDecoder(audio, /* needShutdown = */ false);
988 err = OK;
989 }
Andreas Huber6e3d3112011-11-28 12:36:11 -0800990 } else {
991 // This stream is unaffected by the discontinuity
992
993 if (audio) {
994 mFlushingAudio = FLUSHED;
995 } else {
996 mFlushingVideo = FLUSHED;
997 }
998
999 finishFlushIfPossible();
1000
1001 return -EWOULDBLOCK;
1002 }
Andreas Huber32f3cef2011-03-02 15:34:46 -08001003 }
1004
Andreas Huber3fe62152011-09-16 15:09:22 -07001005 reply->setInt32("err", err);
1006 reply->post();
1007 return OK;
Andreas Huberf9334412010-12-15 15:17:42 -08001008 }
1009
Andreas Huber3fe62152011-09-16 15:09:22 -07001010 if (!audio) {
1011 ++mNumFramesTotal;
1012 }
1013
1014 dropAccessUnit = false;
1015 if (!audio
1016 && mVideoLateByUs > 100000ll
1017 && mVideoIsAVC
1018 && !IsAVCReferenceFrame(accessUnit)) {
1019 dropAccessUnit = true;
1020 ++mNumFramesDropped;
1021 }
1022 } while (dropAccessUnit);
Andreas Huberf9334412010-12-15 15:17:42 -08001023
Steve Block3856b092011-10-20 11:56:00 +01001024 // ALOGV("returned a valid buffer of %s data", audio ? "audio" : "video");
Andreas Huberf9334412010-12-15 15:17:42 -08001025
1026#if 0
1027 int64_t mediaTimeUs;
1028 CHECK(accessUnit->meta()->findInt64("timeUs", &mediaTimeUs));
Steve Block3856b092011-10-20 11:56:00 +01001029 ALOGV("feeding %s input buffer at media time %.2f secs",
Andreas Huberf9334412010-12-15 15:17:42 -08001030 audio ? "audio" : "video",
1031 mediaTimeUs / 1E6);
1032#endif
1033
Andreas Huber2d8bedd2012-02-21 14:38:23 -08001034 reply->setBuffer("buffer", accessUnit);
Andreas Huberf9334412010-12-15 15:17:42 -08001035 reply->post();
1036
1037 return OK;
1038}
1039
1040void NuPlayer::renderBuffer(bool audio, const sp<AMessage> &msg) {
Steve Block3856b092011-10-20 11:56:00 +01001041 // ALOGV("renderBuffer %s", audio ? "audio" : "video");
Andreas Huberf9334412010-12-15 15:17:42 -08001042
1043 sp<AMessage> reply;
1044 CHECK(msg->findMessage("reply", &reply));
1045
Andreas Huber18ac5402011-08-31 15:04:25 -07001046 if (IsFlushingState(audio ? mFlushingAudio : mFlushingVideo)) {
1047 // We're currently attempting to flush the decoder, in order
1048 // to complete this, the decoder wants all its buffers back,
1049 // so we don't want any output buffers it sent us (from before
1050 // we initiated the flush) to be stuck in the renderer's queue.
1051
Steve Block3856b092011-10-20 11:56:00 +01001052 ALOGV("we're still flushing the %s decoder, sending its output buffer"
Andreas Huber18ac5402011-08-31 15:04:25 -07001053 " right back.", audio ? "audio" : "video");
1054
1055 reply->post();
1056 return;
1057 }
1058
Andreas Huber2d8bedd2012-02-21 14:38:23 -08001059 sp<ABuffer> buffer;
1060 CHECK(msg->findBuffer("buffer", &buffer));
Andreas Huberf9334412010-12-15 15:17:42 -08001061
Andreas Huber32f3cef2011-03-02 15:34:46 -08001062 int64_t &skipUntilMediaTimeUs =
1063 audio
1064 ? mSkipRenderingAudioUntilMediaTimeUs
1065 : mSkipRenderingVideoUntilMediaTimeUs;
1066
1067 if (skipUntilMediaTimeUs >= 0) {
1068 int64_t mediaTimeUs;
1069 CHECK(buffer->meta()->findInt64("timeUs", &mediaTimeUs));
1070
1071 if (mediaTimeUs < skipUntilMediaTimeUs) {
Steve Block3856b092011-10-20 11:56:00 +01001072 ALOGV("dropping %s buffer at time %lld as requested.",
Andreas Huber32f3cef2011-03-02 15:34:46 -08001073 audio ? "audio" : "video",
1074 mediaTimeUs);
1075
1076 reply->post();
1077 return;
1078 }
1079
1080 skipUntilMediaTimeUs = -1;
1081 }
1082
Andreas Huberf9334412010-12-15 15:17:42 -08001083 mRenderer->queueBuffer(audio, buffer, reply);
1084}
1085
Chong Zhangdcb89b32013-08-06 09:44:47 -07001086void NuPlayer::notifyListener(int msg, int ext1, int ext2, const Parcel *in) {
Andreas Huber43c3e6c2011-01-05 12:17:08 -08001087 if (mDriver == NULL) {
Andreas Huberf9334412010-12-15 15:17:42 -08001088 return;
1089 }
1090
Andreas Huber43c3e6c2011-01-05 12:17:08 -08001091 sp<NuPlayerDriver> driver = mDriver.promote();
Andreas Huberf9334412010-12-15 15:17:42 -08001092
Andreas Huber43c3e6c2011-01-05 12:17:08 -08001093 if (driver == NULL) {
Andreas Huberf9334412010-12-15 15:17:42 -08001094 return;
1095 }
1096
Chong Zhangdcb89b32013-08-06 09:44:47 -07001097 driver->notifyListener(msg, ext1, ext2, in);
Andreas Huberf9334412010-12-15 15:17:42 -08001098}
1099
Andreas Huber1aef2112011-01-04 14:01:29 -08001100void NuPlayer::flushDecoder(bool audio, bool needShutdown) {
Andreas Huber14f76722013-01-15 09:04:18 -08001101 ALOGV("[%s] flushDecoder needShutdown=%d",
1102 audio ? "audio" : "video", needShutdown);
1103
Andreas Huber6e3d3112011-11-28 12:36:11 -08001104 if ((audio && mAudioDecoder == NULL) || (!audio && mVideoDecoder == NULL)) {
Steve Blockdf64d152012-01-04 20:05:49 +00001105 ALOGI("flushDecoder %s without decoder present",
Andreas Huber6e3d3112011-11-28 12:36:11 -08001106 audio ? "audio" : "video");
1107 }
1108
Andreas Huber1aef2112011-01-04 14:01:29 -08001109 // Make sure we don't continue to scan sources until we finish flushing.
1110 ++mScanSourcesGeneration;
Andreas Huber43c3e6c2011-01-05 12:17:08 -08001111 mScanSourcesPending = false;
Andreas Huber1aef2112011-01-04 14:01:29 -08001112
1113 (audio ? mAudioDecoder : mVideoDecoder)->signalFlush();
1114 mRenderer->flush(audio);
1115
1116 FlushStatus newStatus =
1117 needShutdown ? FLUSHING_DECODER_SHUTDOWN : FLUSHING_DECODER;
1118
1119 if (audio) {
1120 CHECK(mFlushingAudio == NONE
1121 || mFlushingAudio == AWAITING_DISCONTINUITY);
1122
1123 mFlushingAudio = newStatus;
1124
1125 if (mFlushingVideo == NONE) {
1126 mFlushingVideo = (mVideoDecoder != NULL)
1127 ? AWAITING_DISCONTINUITY
1128 : FLUSHED;
1129 }
1130 } else {
1131 CHECK(mFlushingVideo == NONE
1132 || mFlushingVideo == AWAITING_DISCONTINUITY);
1133
1134 mFlushingVideo = newStatus;
1135
1136 if (mFlushingAudio == NONE) {
1137 mFlushingAudio = (mAudioDecoder != NULL)
1138 ? AWAITING_DISCONTINUITY
1139 : FLUSHED;
1140 }
1141 }
1142}
1143
Andreas Huber84066782011-08-16 09:34:26 -07001144sp<AMessage> NuPlayer::Source::getFormat(bool audio) {
1145 sp<MetaData> meta = getFormatMeta(audio);
1146
1147 if (meta == NULL) {
1148 return NULL;
1149 }
1150
1151 sp<AMessage> msg = new AMessage;
1152
1153 if(convertMetaDataToMessage(meta, &msg) == OK) {
1154 return msg;
1155 }
1156 return NULL;
1157}
1158
James Dong0d268a32012-08-31 12:18:27 -07001159status_t NuPlayer::setVideoScalingMode(int32_t mode) {
1160 mVideoScalingMode = mode;
Andreas Huber57a339c2012-12-03 11:18:00 -08001161 if (mNativeWindow != NULL) {
James Dong0d268a32012-08-31 12:18:27 -07001162 status_t ret = native_window_set_scaling_mode(
1163 mNativeWindow->getNativeWindow().get(), mVideoScalingMode);
1164 if (ret != OK) {
1165 ALOGE("Failed to set scaling mode (%d): %s",
1166 -ret, strerror(-ret));
1167 return ret;
1168 }
1169 }
1170 return OK;
1171}
1172
Chong Zhangdcb89b32013-08-06 09:44:47 -07001173status_t NuPlayer::getTrackInfo(Parcel* reply) const {
1174 sp<AMessage> msg = new AMessage(kWhatGetTrackInfo, id());
1175 msg->setPointer("reply", reply);
1176
1177 sp<AMessage> response;
1178 status_t err = msg->postAndAwaitResponse(&response);
1179 return err;
1180}
1181
1182status_t NuPlayer::selectTrack(size_t trackIndex, bool select) {
1183 sp<AMessage> msg = new AMessage(kWhatSelectTrack, id());
1184 msg->setSize("trackIndex", trackIndex);
1185 msg->setInt32("select", select);
1186
1187 sp<AMessage> response;
1188 status_t err = msg->postAndAwaitResponse(&response);
1189
1190 return err;
1191}
1192
Andreas Huberb7c8e912012-11-27 15:02:53 -08001193void NuPlayer::schedulePollDuration() {
1194 sp<AMessage> msg = new AMessage(kWhatPollDuration, id());
1195 msg->setInt32("generation", mPollDurationGeneration);
1196 msg->post();
1197}
1198
1199void NuPlayer::cancelPollDuration() {
1200 ++mPollDurationGeneration;
1201}
1202
Andreas Hubera1f8ab02012-11-30 10:53:22 -08001203void NuPlayer::processDeferredActions() {
1204 while (!mDeferredActions.empty()) {
1205 // We won't execute any deferred actions until we're no longer in
1206 // an intermediate state, i.e. one more more decoders are currently
1207 // flushing or shutting down.
1208
1209 if (mRenderer != NULL) {
1210 // There's an edge case where the renderer owns all output
1211 // buffers and is paused, therefore the decoder will not read
1212 // more input data and will never encounter the matching
1213 // discontinuity. To avoid this, we resume the renderer.
1214
1215 if (mFlushingAudio == AWAITING_DISCONTINUITY
1216 || mFlushingVideo == AWAITING_DISCONTINUITY) {
1217 mRenderer->resume();
1218 }
1219 }
1220
1221 if (mFlushingAudio != NONE || mFlushingVideo != NONE) {
1222 // We're currently flushing, postpone the reset until that's
1223 // completed.
1224
1225 ALOGV("postponing action mFlushingAudio=%d, mFlushingVideo=%d",
1226 mFlushingAudio, mFlushingVideo);
1227
1228 break;
1229 }
1230
1231 sp<Action> action = *mDeferredActions.begin();
1232 mDeferredActions.erase(mDeferredActions.begin());
1233
1234 action->execute(this);
1235 }
1236}
1237
1238void NuPlayer::performSeek(int64_t seekTimeUs) {
1239 ALOGV("performSeek seekTimeUs=%lld us (%.2f secs)",
1240 seekTimeUs,
1241 seekTimeUs / 1E6);
1242
1243 mSource->seekTo(seekTimeUs);
1244
1245 if (mDriver != NULL) {
1246 sp<NuPlayerDriver> driver = mDriver.promote();
1247 if (driver != NULL) {
1248 driver->notifyPosition(seekTimeUs);
1249 driver->notifySeekComplete();
1250 }
1251 }
1252
1253 // everything's flushed, continue playback.
1254}
1255
1256void NuPlayer::performDecoderFlush() {
1257 ALOGV("performDecoderFlush");
1258
Andreas Huberda9740e2013-04-16 10:54:03 -07001259 if (mAudioDecoder == NULL && mVideoDecoder == NULL) {
Andreas Hubera1f8ab02012-11-30 10:53:22 -08001260 return;
1261 }
1262
1263 mTimeDiscontinuityPending = true;
1264
1265 if (mAudioDecoder != NULL) {
1266 flushDecoder(true /* audio */, false /* needShutdown */);
1267 }
1268
1269 if (mVideoDecoder != NULL) {
1270 flushDecoder(false /* audio */, false /* needShutdown */);
1271 }
1272}
1273
Andreas Huber14f76722013-01-15 09:04:18 -08001274void NuPlayer::performDecoderShutdown(bool audio, bool video) {
1275 ALOGV("performDecoderShutdown audio=%d, video=%d", audio, video);
Andreas Hubera1f8ab02012-11-30 10:53:22 -08001276
Andreas Huber14f76722013-01-15 09:04:18 -08001277 if ((!audio || mAudioDecoder == NULL)
1278 && (!video || mVideoDecoder == NULL)) {
Andreas Hubera1f8ab02012-11-30 10:53:22 -08001279 return;
1280 }
1281
1282 mTimeDiscontinuityPending = true;
1283
Andreas Huber14f76722013-01-15 09:04:18 -08001284 if (mFlushingAudio == NONE && (!audio || mAudioDecoder == NULL)) {
1285 mFlushingAudio = FLUSHED;
1286 }
1287
1288 if (mFlushingVideo == NONE && (!video || mVideoDecoder == NULL)) {
1289 mFlushingVideo = FLUSHED;
1290 }
1291
1292 if (audio && mAudioDecoder != NULL) {
Andreas Hubera1f8ab02012-11-30 10:53:22 -08001293 flushDecoder(true /* audio */, true /* needShutdown */);
1294 }
1295
Andreas Huber14f76722013-01-15 09:04:18 -08001296 if (video && mVideoDecoder != NULL) {
Andreas Hubera1f8ab02012-11-30 10:53:22 -08001297 flushDecoder(false /* audio */, true /* needShutdown */);
1298 }
1299}
1300
1301void NuPlayer::performReset() {
1302 ALOGV("performReset");
1303
1304 CHECK(mAudioDecoder == NULL);
1305 CHECK(mVideoDecoder == NULL);
1306
1307 cancelPollDuration();
1308
1309 ++mScanSourcesGeneration;
1310 mScanSourcesPending = false;
1311
1312 mRenderer.clear();
1313
1314 if (mSource != NULL) {
1315 mSource->stop();
Andreas Huberb5f25f02013-02-05 10:14:26 -08001316
1317 looper()->unregisterHandler(mSource->id());
1318
Andreas Hubera1f8ab02012-11-30 10:53:22 -08001319 mSource.clear();
1320 }
1321
1322 if (mDriver != NULL) {
1323 sp<NuPlayerDriver> driver = mDriver.promote();
1324 if (driver != NULL) {
1325 driver->notifyResetComplete();
1326 }
1327 }
Andreas Huber57a339c2012-12-03 11:18:00 -08001328
1329 mStarted = false;
Andreas Hubera1f8ab02012-11-30 10:53:22 -08001330}
1331
1332void NuPlayer::performScanSources() {
1333 ALOGV("performScanSources");
1334
Andreas Huber57a339c2012-12-03 11:18:00 -08001335 if (!mStarted) {
1336 return;
1337 }
1338
Andreas Hubera1f8ab02012-11-30 10:53:22 -08001339 if (mAudioDecoder == NULL || mVideoDecoder == NULL) {
1340 postScanSources();
1341 }
1342}
1343
Andreas Huber57a339c2012-12-03 11:18:00 -08001344void NuPlayer::performSetSurface(const sp<NativeWindowWrapper> &wrapper) {
1345 ALOGV("performSetSurface");
1346
1347 mNativeWindow = wrapper;
1348
1349 // XXX - ignore error from setVideoScalingMode for now
1350 setVideoScalingMode(mVideoScalingMode);
1351
1352 if (mDriver != NULL) {
1353 sp<NuPlayerDriver> driver = mDriver.promote();
1354 if (driver != NULL) {
1355 driver->notifySetSurfaceComplete();
1356 }
1357 }
1358}
1359
Andreas Huber9575c962013-02-05 13:59:56 -08001360void NuPlayer::onSourceNotify(const sp<AMessage> &msg) {
1361 int32_t what;
1362 CHECK(msg->findInt32("what", &what));
1363
1364 switch (what) {
1365 case Source::kWhatPrepared:
1366 {
Andreas Huberb5f28d42013-04-25 15:11:19 -07001367 if (mSource == NULL) {
1368 // This is a stale notification from a source that was
1369 // asynchronously preparing when the client called reset().
1370 // We handled the reset, the source is gone.
1371 break;
1372 }
1373
Andreas Huberec0c5972013-02-05 14:47:13 -08001374 int32_t err;
1375 CHECK(msg->findInt32("err", &err));
1376
Andreas Huber9575c962013-02-05 13:59:56 -08001377 sp<NuPlayerDriver> driver = mDriver.promote();
1378 if (driver != NULL) {
Marco Nelissendd114d12014-05-28 15:23:14 -07001379 // notify duration first, so that it's definitely set when
1380 // the app received the "prepare complete" callback.
1381 int64_t durationUs;
1382 if (mSource->getDuration(&durationUs) == OK) {
1383 driver->notifyDuration(durationUs);
1384 }
Andreas Huberec0c5972013-02-05 14:47:13 -08001385 driver->notifyPrepareCompleted(err);
Andreas Huber9575c962013-02-05 13:59:56 -08001386 }
Andreas Huber99759402013-04-01 14:28:31 -07001387
Andreas Huber9575c962013-02-05 13:59:56 -08001388 break;
1389 }
1390
1391 case Source::kWhatFlagsChanged:
1392 {
1393 uint32_t flags;
1394 CHECK(msg->findInt32("flags", (int32_t *)&flags));
1395
Chong Zhang4b7069d2013-09-11 12:52:43 -07001396 sp<NuPlayerDriver> driver = mDriver.promote();
1397 if (driver != NULL) {
1398 driver->notifyFlagsChanged(flags);
1399 }
1400
Andreas Huber9575c962013-02-05 13:59:56 -08001401 if ((mSourceFlags & Source::FLAG_DYNAMIC_DURATION)
1402 && (!(flags & Source::FLAG_DYNAMIC_DURATION))) {
1403 cancelPollDuration();
1404 } else if (!(mSourceFlags & Source::FLAG_DYNAMIC_DURATION)
1405 && (flags & Source::FLAG_DYNAMIC_DURATION)
1406 && (mAudioDecoder != NULL || mVideoDecoder != NULL)) {
1407 schedulePollDuration();
1408 }
1409
1410 mSourceFlags = flags;
1411 break;
1412 }
1413
1414 case Source::kWhatVideoSizeChanged:
1415 {
1416 int32_t width, height;
1417 CHECK(msg->findInt32("width", &width));
1418 CHECK(msg->findInt32("height", &height));
1419
1420 notifyListener(MEDIA_SET_VIDEO_SIZE, width, height);
1421 break;
1422 }
1423
Roger Jönssonb50e83e2013-01-21 16:26:41 +01001424 case Source::kWhatBufferingStart:
1425 {
1426 notifyListener(MEDIA_INFO, MEDIA_INFO_BUFFERING_START, 0);
1427 break;
1428 }
1429
1430 case Source::kWhatBufferingEnd:
1431 {
1432 notifyListener(MEDIA_INFO, MEDIA_INFO_BUFFERING_END, 0);
1433 break;
1434 }
1435
Chong Zhangdcb89b32013-08-06 09:44:47 -07001436 case Source::kWhatSubtitleData:
1437 {
1438 sp<ABuffer> buffer;
1439 CHECK(msg->findBuffer("buffer", &buffer));
1440
1441 int32_t trackIndex;
1442 int64_t timeUs, durationUs;
1443 CHECK(buffer->meta()->findInt32("trackIndex", &trackIndex));
1444 CHECK(buffer->meta()->findInt64("timeUs", &timeUs));
1445 CHECK(buffer->meta()->findInt64("durationUs", &durationUs));
1446
1447 Parcel in;
1448 in.writeInt32(trackIndex);
1449 in.writeInt64(timeUs);
1450 in.writeInt64(durationUs);
1451 in.writeInt32(buffer->size());
1452 in.writeInt32(buffer->size());
1453 in.write(buffer->data(), buffer->size());
1454
1455 notifyListener(MEDIA_SUBTITLE_DATA, 0, 0, &in);
1456 break;
1457 }
1458
Andreas Huber14f76722013-01-15 09:04:18 -08001459 case Source::kWhatQueueDecoderShutdown:
1460 {
1461 int32_t audio, video;
1462 CHECK(msg->findInt32("audio", &audio));
1463 CHECK(msg->findInt32("video", &video));
1464
1465 sp<AMessage> reply;
1466 CHECK(msg->findMessage("reply", &reply));
1467
1468 queueDecoderShutdown(audio, video, reply);
1469 break;
1470 }
1471
Andreas Huber9575c962013-02-05 13:59:56 -08001472 default:
1473 TRESPASS();
1474 }
1475}
1476
Andreas Huberb5f25f02013-02-05 10:14:26 -08001477////////////////////////////////////////////////////////////////////////////////
1478
Andreas Huber9575c962013-02-05 13:59:56 -08001479void NuPlayer::Source::notifyFlagsChanged(uint32_t flags) {
1480 sp<AMessage> notify = dupNotify();
1481 notify->setInt32("what", kWhatFlagsChanged);
1482 notify->setInt32("flags", flags);
1483 notify->post();
1484}
1485
1486void NuPlayer::Source::notifyVideoSizeChanged(int32_t width, int32_t height) {
1487 sp<AMessage> notify = dupNotify();
1488 notify->setInt32("what", kWhatVideoSizeChanged);
1489 notify->setInt32("width", width);
1490 notify->setInt32("height", height);
1491 notify->post();
1492}
1493
Andreas Huberec0c5972013-02-05 14:47:13 -08001494void NuPlayer::Source::notifyPrepared(status_t err) {
Andreas Huber9575c962013-02-05 13:59:56 -08001495 sp<AMessage> notify = dupNotify();
1496 notify->setInt32("what", kWhatPrepared);
Andreas Huberec0c5972013-02-05 14:47:13 -08001497 notify->setInt32("err", err);
Andreas Huber9575c962013-02-05 13:59:56 -08001498 notify->post();
1499}
1500
Andreas Huber84333e02014-02-07 15:36:10 -08001501void NuPlayer::Source::onMessageReceived(const sp<AMessage> & /* msg */) {
Andreas Huberb5f25f02013-02-05 10:14:26 -08001502 TRESPASS();
1503}
1504
Andreas Huber14f76722013-01-15 09:04:18 -08001505void NuPlayer::queueDecoderShutdown(
1506 bool audio, bool video, const sp<AMessage> &reply) {
1507 ALOGI("queueDecoderShutdown audio=%d, video=%d", audio, video);
1508
1509 mDeferredActions.push_back(
1510 new ShutdownDecoderAction(audio, video));
1511
1512 mDeferredActions.push_back(
1513 new SimpleAction(&NuPlayer::performScanSources));
1514
1515 mDeferredActions.push_back(new PostMessageAction(reply));
1516
1517 processDeferredActions();
1518}
1519
Andreas Huberf9334412010-12-15 15:17:42 -08001520} // namespace android