blob: 9329f5b8dac65d964e26f35edaae35fd2e7615cf [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>
38#include <media/stagefright/ACodec.h>
Andreas Huber3fe62152011-09-16 15:09:22 -070039#include <media/stagefright/MediaDefs.h>
Andreas Huberf9334412010-12-15 15:17:42 -080040#include <media/stagefright/MediaErrors.h>
41#include <media/stagefright/MetaData.h>
Andy McFadden8ba01022012-12-18 09:46:54 -080042#include <gui/IGraphicBufferProducer.h>
Andreas Huberf9334412010-12-15 15:17:42 -080043
Andreas Huber3fe62152011-09-16 15:09:22 -070044#include "avc_utils.h"
45
Andreas Huber84066782011-08-16 09:34:26 -070046#include "ESDS.h"
47#include <media/stagefright/Utils.h>
48
Andreas Huberf9334412010-12-15 15:17:42 -080049namespace android {
50
Andreas Hubera1f8ab02012-11-30 10:53:22 -080051struct NuPlayer::Action : public RefBase {
52 Action() {}
53
54 virtual void execute(NuPlayer *player) = 0;
55
56private:
57 DISALLOW_EVIL_CONSTRUCTORS(Action);
58};
59
60struct NuPlayer::SeekAction : public Action {
61 SeekAction(int64_t seekTimeUs)
62 : mSeekTimeUs(seekTimeUs) {
63 }
64
65 virtual void execute(NuPlayer *player) {
66 player->performSeek(mSeekTimeUs);
67 }
68
69private:
70 int64_t mSeekTimeUs;
71
72 DISALLOW_EVIL_CONSTRUCTORS(SeekAction);
73};
74
Andreas Huber57a339c2012-12-03 11:18:00 -080075struct NuPlayer::SetSurfaceAction : public Action {
76 SetSurfaceAction(const sp<NativeWindowWrapper> &wrapper)
77 : mWrapper(wrapper) {
78 }
79
80 virtual void execute(NuPlayer *player) {
81 player->performSetSurface(mWrapper);
82 }
83
84private:
85 sp<NativeWindowWrapper> mWrapper;
86
87 DISALLOW_EVIL_CONSTRUCTORS(SetSurfaceAction);
88};
89
Andreas Huber14f76722013-01-15 09:04:18 -080090struct NuPlayer::ShutdownDecoderAction : public Action {
91 ShutdownDecoderAction(bool audio, bool video)
92 : mAudio(audio),
93 mVideo(video) {
94 }
95
96 virtual void execute(NuPlayer *player) {
97 player->performDecoderShutdown(mAudio, mVideo);
98 }
99
100private:
101 bool mAudio;
102 bool mVideo;
103
104 DISALLOW_EVIL_CONSTRUCTORS(ShutdownDecoderAction);
105};
106
107struct NuPlayer::PostMessageAction : public Action {
108 PostMessageAction(const sp<AMessage> &msg)
109 : mMessage(msg) {
110 }
111
112 virtual void execute(NuPlayer *) {
113 mMessage->post();
114 }
115
116private:
117 sp<AMessage> mMessage;
118
119 DISALLOW_EVIL_CONSTRUCTORS(PostMessageAction);
120};
121
Andreas Hubera1f8ab02012-11-30 10:53:22 -0800122// Use this if there's no state necessary to save in order to execute
123// the action.
124struct NuPlayer::SimpleAction : public Action {
125 typedef void (NuPlayer::*ActionFunc)();
126
127 SimpleAction(ActionFunc func)
128 : mFunc(func) {
129 }
130
131 virtual void execute(NuPlayer *player) {
132 (player->*mFunc)();
133 }
134
135private:
136 ActionFunc mFunc;
137
138 DISALLOW_EVIL_CONSTRUCTORS(SimpleAction);
139};
140
Andreas Huberf9334412010-12-15 15:17:42 -0800141////////////////////////////////////////////////////////////////////////////////
142
143NuPlayer::NuPlayer()
Andreas Huber9b80c2b2011-06-30 15:47:02 -0700144 : mUIDValid(false),
Andreas Huber9575c962013-02-05 13:59:56 -0800145 mSourceFlags(0),
Andreas Huber3fe62152011-09-16 15:09:22 -0700146 mVideoIsAVC(false),
Andreas Huber9b80c2b2011-06-30 15:47:02 -0700147 mAudioEOS(false),
Andreas Huberf9334412010-12-15 15:17:42 -0800148 mVideoEOS(false),
Andreas Huber5bc087c2010-12-23 10:27:40 -0800149 mScanSourcesPending(false),
Andreas Huber1aef2112011-01-04 14:01:29 -0800150 mScanSourcesGeneration(0),
Andreas Huberb7c8e912012-11-27 15:02:53 -0800151 mPollDurationGeneration(0),
Andreas Huber6e3d3112011-11-28 12:36:11 -0800152 mTimeDiscontinuityPending(false),
Andreas Huberf9334412010-12-15 15:17:42 -0800153 mFlushingAudio(NONE),
Andreas Huber1aef2112011-01-04 14:01:29 -0800154 mFlushingVideo(NONE),
Andreas Huber3fe62152011-09-16 15:09:22 -0700155 mSkipRenderingAudioUntilMediaTimeUs(-1ll),
156 mSkipRenderingVideoUntilMediaTimeUs(-1ll),
157 mVideoLateByUs(0ll),
158 mNumFramesTotal(0ll),
James Dong0d268a32012-08-31 12:18:27 -0700159 mNumFramesDropped(0ll),
Andreas Huber57a339c2012-12-03 11:18:00 -0800160 mVideoScalingMode(NATIVE_WINDOW_SCALING_MODE_SCALE_TO_WINDOW),
161 mStarted(false) {
Andreas Huberf9334412010-12-15 15:17:42 -0800162}
163
164NuPlayer::~NuPlayer() {
165}
166
Andreas Huber9b80c2b2011-06-30 15:47:02 -0700167void NuPlayer::setUID(uid_t uid) {
168 mUIDValid = true;
169 mUID = uid;
170}
171
Andreas Huber43c3e6c2011-01-05 12:17:08 -0800172void NuPlayer::setDriver(const wp<NuPlayerDriver> &driver) {
173 mDriver = driver;
Andreas Huberf9334412010-12-15 15:17:42 -0800174}
175
Andreas Huber9575c962013-02-05 13:59:56 -0800176void NuPlayer::setDataSourceAsync(const sp<IStreamSource> &source) {
Andreas Huberf9334412010-12-15 15:17:42 -0800177 sp<AMessage> msg = new AMessage(kWhatSetDataSource, id());
178
Andreas Huberb5f25f02013-02-05 10:14:26 -0800179 sp<AMessage> notify = new AMessage(kWhatSourceNotify, id());
180
Andreas Huber240abcc2014-02-13 13:32:37 -0800181 msg->setObject("source", new StreamingSource(notify, source));
Andreas Huber5bc087c2010-12-23 10:27:40 -0800182 msg->post();
183}
Andreas Huberf9334412010-12-15 15:17:42 -0800184
Andreas Huberafed0e12011-09-20 15:39:58 -0700185static bool IsHTTPLiveURL(const char *url) {
186 if (!strncasecmp("http://", url, 7)
Andreas Huber99759402013-04-01 14:28:31 -0700187 || !strncasecmp("https://", url, 8)
188 || !strncasecmp("file://", url, 7)) {
Andreas Huberafed0e12011-09-20 15:39:58 -0700189 size_t len = strlen(url);
190 if (len >= 5 && !strcasecmp(".m3u8", &url[len - 5])) {
191 return true;
192 }
193
194 if (strstr(url,"m3u8")) {
195 return true;
196 }
197 }
198
199 return false;
200}
201
Andreas Huber9575c962013-02-05 13:59:56 -0800202void NuPlayer::setDataSourceAsync(
Andreas Huber1b86fe02014-01-29 11:13:26 -0800203 const sp<IMediaHTTPService> &httpService,
204 const char *url,
205 const KeyedVector<String8, String8> *headers) {
Andreas Huber5bc087c2010-12-23 10:27:40 -0800206 sp<AMessage> msg = new AMessage(kWhatSetDataSource, id());
Oscar Rydhé7a33b772012-02-20 10:15:48 +0100207 size_t len = strlen(url);
Andreas Huber5bc087c2010-12-23 10:27:40 -0800208
Andreas Huberb5f25f02013-02-05 10:14:26 -0800209 sp<AMessage> notify = new AMessage(kWhatSourceNotify, id());
210
Andreas Huberafed0e12011-09-20 15:39:58 -0700211 sp<Source> source;
212 if (IsHTTPLiveURL(url)) {
Andreas Huber81e68442014-02-05 11:52:33 -0800213 source = new HTTPLiveSource(notify, httpService, url, headers);
Andreas Huberafed0e12011-09-20 15:39:58 -0700214 } else if (!strncasecmp(url, "rtsp://", 7)) {
Andreas Huber1b86fe02014-01-29 11:13:26 -0800215 source = new RTSPSource(
216 notify, httpService, url, headers, mUIDValid, mUID);
Oscar Rydhé7a33b772012-02-20 10:15:48 +0100217 } else if ((!strncasecmp(url, "http://", 7)
218 || !strncasecmp(url, "https://", 8))
219 && ((len >= 4 && !strcasecmp(".sdp", &url[len - 4]))
220 || strstr(url, ".sdp?"))) {
Andreas Huber1b86fe02014-01-29 11:13:26 -0800221 source = new RTSPSource(
222 notify, httpService, url, headers, mUIDValid, mUID, true);
Andreas Huber2bfdd422011-10-11 15:24:07 -0700223 } else {
Andreas Huber81e68442014-02-05 11:52:33 -0800224 source = new GenericSource(notify, httpService, url, headers);
Andreas Huber2bfdd422011-10-11 15:24:07 -0700225 }
226
Andreas Huberafed0e12011-09-20 15:39:58 -0700227 msg->setObject("source", source);
228 msg->post();
229}
230
Andreas Huber9575c962013-02-05 13:59:56 -0800231void NuPlayer::setDataSourceAsync(int fd, int64_t offset, int64_t length) {
Andreas Huberafed0e12011-09-20 15:39:58 -0700232 sp<AMessage> msg = new AMessage(kWhatSetDataSource, id());
233
Andreas Huberb5f25f02013-02-05 10:14:26 -0800234 sp<AMessage> notify = new AMessage(kWhatSourceNotify, id());
235
236 sp<Source> source = new GenericSource(notify, fd, offset, length);
Andreas Huberafed0e12011-09-20 15:39:58 -0700237 msg->setObject("source", source);
Andreas Huberf9334412010-12-15 15:17:42 -0800238 msg->post();
239}
240
Andreas Huber9575c962013-02-05 13:59:56 -0800241void NuPlayer::prepareAsync() {
242 (new AMessage(kWhatPrepare, id()))->post();
243}
244
Andreas Huber57a339c2012-12-03 11:18:00 -0800245void NuPlayer::setVideoSurfaceTextureAsync(
Andy McFadden8ba01022012-12-18 09:46:54 -0800246 const sp<IGraphicBufferProducer> &bufferProducer) {
Glenn Kasten11731182011-02-08 17:26:17 -0800247 sp<AMessage> msg = new AMessage(kWhatSetVideoNativeWindow, id());
Andreas Huber57a339c2012-12-03 11:18:00 -0800248
Andy McFadden8ba01022012-12-18 09:46:54 -0800249 if (bufferProducer == NULL) {
Andreas Huber57a339c2012-12-03 11:18:00 -0800250 msg->setObject("native-window", NULL);
251 } else {
252 msg->setObject(
253 "native-window",
254 new NativeWindowWrapper(
Mathias Agopian1a2952a2013-02-14 17:11:27 -0800255 new Surface(bufferProducer)));
Andreas Huber57a339c2012-12-03 11:18:00 -0800256 }
257
Andreas Huberf9334412010-12-15 15:17:42 -0800258 msg->post();
259}
260
261void NuPlayer::setAudioSink(const sp<MediaPlayerBase::AudioSink> &sink) {
262 sp<AMessage> msg = new AMessage(kWhatSetAudioSink, id());
263 msg->setObject("sink", sink);
264 msg->post();
265}
266
267void NuPlayer::start() {
268 (new AMessage(kWhatStart, id()))->post();
269}
270
Andreas Huber43c3e6c2011-01-05 12:17:08 -0800271void NuPlayer::pause() {
Andreas Huberb4082222011-01-20 15:23:04 -0800272 (new AMessage(kWhatPause, id()))->post();
Andreas Huber43c3e6c2011-01-05 12:17:08 -0800273}
274
275void NuPlayer::resume() {
Andreas Huberb4082222011-01-20 15:23:04 -0800276 (new AMessage(kWhatResume, id()))->post();
Andreas Huber43c3e6c2011-01-05 12:17:08 -0800277}
278
Andreas Huber1aef2112011-01-04 14:01:29 -0800279void NuPlayer::resetAsync() {
280 (new AMessage(kWhatReset, id()))->post();
281}
282
Andreas Huber43c3e6c2011-01-05 12:17:08 -0800283void NuPlayer::seekToAsync(int64_t seekTimeUs) {
284 sp<AMessage> msg = new AMessage(kWhatSeek, id());
285 msg->setInt64("seekTimeUs", seekTimeUs);
286 msg->post();
287}
288
Andreas Huber53df1a42010-12-22 10:03:04 -0800289// static
Andreas Huber1aef2112011-01-04 14:01:29 -0800290bool NuPlayer::IsFlushingState(FlushStatus state, bool *needShutdown) {
Andreas Huber53df1a42010-12-22 10:03:04 -0800291 switch (state) {
292 case FLUSHING_DECODER:
Andreas Huber1aef2112011-01-04 14:01:29 -0800293 if (needShutdown != NULL) {
294 *needShutdown = false;
Andreas Huber53df1a42010-12-22 10:03:04 -0800295 }
296 return true;
297
Andreas Huber1aef2112011-01-04 14:01:29 -0800298 case FLUSHING_DECODER_SHUTDOWN:
299 if (needShutdown != NULL) {
300 *needShutdown = true;
Andreas Huber53df1a42010-12-22 10:03:04 -0800301 }
302 return true;
303
304 default:
305 return false;
306 }
307}
308
Andreas Huberf9334412010-12-15 15:17:42 -0800309void NuPlayer::onMessageReceived(const sp<AMessage> &msg) {
310 switch (msg->what()) {
311 case kWhatSetDataSource:
312 {
Steve Block3856b092011-10-20 11:56:00 +0100313 ALOGV("kWhatSetDataSource");
Andreas Huberf9334412010-12-15 15:17:42 -0800314
315 CHECK(mSource == NULL);
316
Andreas Huber5bc087c2010-12-23 10:27:40 -0800317 sp<RefBase> obj;
318 CHECK(msg->findObject("source", &obj));
Andreas Huberf9334412010-12-15 15:17:42 -0800319
Andreas Huber5bc087c2010-12-23 10:27:40 -0800320 mSource = static_cast<Source *>(obj.get());
Andreas Huberb5f25f02013-02-05 10:14:26 -0800321
322 looper()->registerHandler(mSource);
Andreas Huber9575c962013-02-05 13:59:56 -0800323
324 CHECK(mDriver != NULL);
325 sp<NuPlayerDriver> driver = mDriver.promote();
326 if (driver != NULL) {
327 driver->notifySetDataSourceCompleted(OK);
328 }
329 break;
330 }
331
332 case kWhatPrepare:
333 {
334 mSource->prepareAsync();
Andreas Huberf9334412010-12-15 15:17:42 -0800335 break;
336 }
337
Chong Zhangdcb89b32013-08-06 09:44:47 -0700338 case kWhatGetTrackInfo:
339 {
340 uint32_t replyID;
341 CHECK(msg->senderAwaitsResponse(&replyID));
342
343 status_t err = INVALID_OPERATION;
344 if (mSource != NULL) {
345 Parcel* reply;
346 CHECK(msg->findPointer("reply", (void**)&reply));
347 err = mSource->getTrackInfo(reply);
348 }
349
350 sp<AMessage> response = new AMessage;
351 response->setInt32("err", err);
352
353 response->postReply(replyID);
354 break;
355 }
356
357 case kWhatSelectTrack:
358 {
359 uint32_t replyID;
360 CHECK(msg->senderAwaitsResponse(&replyID));
361
362 status_t err = INVALID_OPERATION;
363 if (mSource != NULL) {
364 size_t trackIndex;
365 int32_t select;
366 CHECK(msg->findSize("trackIndex", &trackIndex));
367 CHECK(msg->findInt32("select", &select));
368 err = mSource->selectTrack(trackIndex, select);
369 }
370
371 sp<AMessage> response = new AMessage;
372 response->setInt32("err", err);
373
374 response->postReply(replyID);
375 break;
376 }
377
Andreas Huberb7c8e912012-11-27 15:02:53 -0800378 case kWhatPollDuration:
379 {
380 int32_t generation;
381 CHECK(msg->findInt32("generation", &generation));
382
383 if (generation != mPollDurationGeneration) {
384 // stale
385 break;
386 }
387
388 int64_t durationUs;
389 if (mDriver != NULL && mSource->getDuration(&durationUs) == OK) {
390 sp<NuPlayerDriver> driver = mDriver.promote();
391 if (driver != NULL) {
392 driver->notifyDuration(durationUs);
393 }
394 }
395
396 msg->post(1000000ll); // poll again in a second.
397 break;
398 }
399
Glenn Kasten11731182011-02-08 17:26:17 -0800400 case kWhatSetVideoNativeWindow:
Andreas Huberf9334412010-12-15 15:17:42 -0800401 {
Steve Block3856b092011-10-20 11:56:00 +0100402 ALOGV("kWhatSetVideoNativeWindow");
Andreas Huberf9334412010-12-15 15:17:42 -0800403
Andreas Huber57a339c2012-12-03 11:18:00 -0800404 mDeferredActions.push_back(
Andreas Huber14f76722013-01-15 09:04:18 -0800405 new ShutdownDecoderAction(
406 false /* audio */, true /* video */));
Andreas Huber57a339c2012-12-03 11:18:00 -0800407
Andreas Huberf9334412010-12-15 15:17:42 -0800408 sp<RefBase> obj;
Glenn Kasten11731182011-02-08 17:26:17 -0800409 CHECK(msg->findObject("native-window", &obj));
Andreas Huberf9334412010-12-15 15:17:42 -0800410
Andreas Huber57a339c2012-12-03 11:18:00 -0800411 mDeferredActions.push_back(
412 new SetSurfaceAction(
413 static_cast<NativeWindowWrapper *>(obj.get())));
James Dong0d268a32012-08-31 12:18:27 -0700414
Andreas Huber57a339c2012-12-03 11:18:00 -0800415 if (obj != NULL) {
416 // If there is a new surface texture, instantiate decoders
417 // again if possible.
418 mDeferredActions.push_back(
419 new SimpleAction(&NuPlayer::performScanSources));
420 }
421
422 processDeferredActions();
Andreas Huberf9334412010-12-15 15:17:42 -0800423 break;
424 }
425
426 case kWhatSetAudioSink:
427 {
Steve Block3856b092011-10-20 11:56:00 +0100428 ALOGV("kWhatSetAudioSink");
Andreas Huberf9334412010-12-15 15:17:42 -0800429
430 sp<RefBase> obj;
431 CHECK(msg->findObject("sink", &obj));
432
433 mAudioSink = static_cast<MediaPlayerBase::AudioSink *>(obj.get());
434 break;
435 }
436
437 case kWhatStart:
438 {
Steve Block3856b092011-10-20 11:56:00 +0100439 ALOGV("kWhatStart");
Andreas Huber43c3e6c2011-01-05 12:17:08 -0800440
Andreas Huber3fe62152011-09-16 15:09:22 -0700441 mVideoIsAVC = false;
Andreas Huber1aef2112011-01-04 14:01:29 -0800442 mAudioEOS = false;
443 mVideoEOS = false;
Andreas Huber32f3cef2011-03-02 15:34:46 -0800444 mSkipRenderingAudioUntilMediaTimeUs = -1;
445 mSkipRenderingVideoUntilMediaTimeUs = -1;
Andreas Huber3fe62152011-09-16 15:09:22 -0700446 mVideoLateByUs = 0;
447 mNumFramesTotal = 0;
448 mNumFramesDropped = 0;
Andreas Huber57a339c2012-12-03 11:18:00 -0800449 mStarted = true;
Andreas Huber1aef2112011-01-04 14:01:29 -0800450
Andreas Huber5bc087c2010-12-23 10:27:40 -0800451 mSource->start();
Andreas Huberf9334412010-12-15 15:17:42 -0800452
Andreas Huberd5e56232013-03-12 11:01:43 -0700453 uint32_t flags = 0;
454
455 if (mSource->isRealTime()) {
456 flags |= Renderer::FLAG_REAL_TIME;
457 }
458
Andreas Huberf9334412010-12-15 15:17:42 -0800459 mRenderer = new Renderer(
460 mAudioSink,
Andreas Huberd5e56232013-03-12 11:01:43 -0700461 new AMessage(kWhatRendererNotify, id()),
462 flags);
Andreas Huberf9334412010-12-15 15:17:42 -0800463
464 looper()->registerHandler(mRenderer);
465
Andreas Huber1aef2112011-01-04 14:01:29 -0800466 postScanSources();
Andreas Huberf9334412010-12-15 15:17:42 -0800467 break;
468 }
469
470 case kWhatScanSources:
471 {
Andreas Huber1aef2112011-01-04 14:01:29 -0800472 int32_t generation;
473 CHECK(msg->findInt32("generation", &generation));
474 if (generation != mScanSourcesGeneration) {
475 // Drop obsolete msg.
476 break;
477 }
478
Andreas Huber5bc087c2010-12-23 10:27:40 -0800479 mScanSourcesPending = false;
480
Steve Block3856b092011-10-20 11:56:00 +0100481 ALOGV("scanning sources haveAudio=%d, haveVideo=%d",
Andreas Huber43c3e6c2011-01-05 12:17:08 -0800482 mAudioDecoder != NULL, mVideoDecoder != NULL);
483
Andreas Huberb7c8e912012-11-27 15:02:53 -0800484 bool mHadAnySourcesBefore =
485 (mAudioDecoder != NULL) || (mVideoDecoder != NULL);
486
Haynes Mathew George5d246ef2012-07-09 10:36:57 -0700487 if (mNativeWindow != NULL) {
488 instantiateDecoder(false, &mVideoDecoder);
489 }
Andreas Huberf9334412010-12-15 15:17:42 -0800490
491 if (mAudioSink != NULL) {
Andreas Huber5bc087c2010-12-23 10:27:40 -0800492 instantiateDecoder(true, &mAudioDecoder);
Andreas Huberf9334412010-12-15 15:17:42 -0800493 }
494
Andreas Huberb7c8e912012-11-27 15:02:53 -0800495 if (!mHadAnySourcesBefore
496 && (mAudioDecoder != NULL || mVideoDecoder != NULL)) {
497 // This is the first time we've found anything playable.
498
Andreas Huber9575c962013-02-05 13:59:56 -0800499 if (mSourceFlags & Source::FLAG_DYNAMIC_DURATION) {
Andreas Huberb7c8e912012-11-27 15:02:53 -0800500 schedulePollDuration();
501 }
502 }
503
Andreas Hubereac68ba2011-09-27 12:12:25 -0700504 status_t err;
505 if ((err = mSource->feedMoreTSData()) != OK) {
Andreas Huber1aef2112011-01-04 14:01:29 -0800506 if (mAudioDecoder == NULL && mVideoDecoder == NULL) {
507 // We're not currently decoding anything (no audio or
508 // video tracks found) and we just ran out of input data.
Andreas Hubereac68ba2011-09-27 12:12:25 -0700509
510 if (err == ERROR_END_OF_STREAM) {
511 notifyListener(MEDIA_PLAYBACK_COMPLETE, 0, 0);
512 } else {
513 notifyListener(MEDIA_ERROR, MEDIA_ERROR_UNKNOWN, err);
514 }
Andreas Huber1aef2112011-01-04 14:01:29 -0800515 }
Andreas Huberf9334412010-12-15 15:17:42 -0800516 break;
517 }
518
Andreas Huberfbe9d812012-08-31 14:05:27 -0700519 if ((mAudioDecoder == NULL && mAudioSink != NULL)
520 || (mVideoDecoder == NULL && mNativeWindow != NULL)) {
Andreas Huberf9334412010-12-15 15:17:42 -0800521 msg->post(100000ll);
Andreas Huber5bc087c2010-12-23 10:27:40 -0800522 mScanSourcesPending = true;
Andreas Huberf9334412010-12-15 15:17:42 -0800523 }
524 break;
525 }
526
527 case kWhatVideoNotify:
528 case kWhatAudioNotify:
529 {
530 bool audio = msg->what() == kWhatAudioNotify;
531
532 sp<AMessage> codecRequest;
533 CHECK(msg->findMessage("codec-request", &codecRequest));
534
535 int32_t what;
536 CHECK(codecRequest->findInt32("what", &what));
537
538 if (what == ACodec::kWhatFillThisBuffer) {
539 status_t err = feedDecoderInputData(
540 audio, codecRequest);
541
Andreas Huber5bc087c2010-12-23 10:27:40 -0800542 if (err == -EWOULDBLOCK) {
Andreas Hubereac68ba2011-09-27 12:12:25 -0700543 if (mSource->feedMoreTSData() == OK) {
Andreas Huber1183a4a2011-11-03 11:00:21 -0700544 msg->post(10000ll);
Andreas Huber5bc087c2010-12-23 10:27:40 -0800545 }
Andreas Huberf9334412010-12-15 15:17:42 -0800546 }
547 } else if (what == ACodec::kWhatEOS) {
Andreas Huberdc9bacd2011-09-26 10:53:29 -0700548 int32_t err;
549 CHECK(codecRequest->findInt32("err", &err));
550
551 if (err == ERROR_END_OF_STREAM) {
Steve Block3856b092011-10-20 11:56:00 +0100552 ALOGV("got %s decoder EOS", audio ? "audio" : "video");
Andreas Huberdc9bacd2011-09-26 10:53:29 -0700553 } else {
Steve Block3856b092011-10-20 11:56:00 +0100554 ALOGV("got %s decoder EOS w/ error %d",
Andreas Huberdc9bacd2011-09-26 10:53:29 -0700555 audio ? "audio" : "video",
556 err);
557 }
558
559 mRenderer->queueEOS(audio, err);
Andreas Huberf9334412010-12-15 15:17:42 -0800560 } else if (what == ACodec::kWhatFlushCompleted) {
Andreas Huber1aef2112011-01-04 14:01:29 -0800561 bool needShutdown;
Andreas Huber53df1a42010-12-22 10:03:04 -0800562
Andreas Huberf9334412010-12-15 15:17:42 -0800563 if (audio) {
Andreas Huber1aef2112011-01-04 14:01:29 -0800564 CHECK(IsFlushingState(mFlushingAudio, &needShutdown));
Andreas Huberf9334412010-12-15 15:17:42 -0800565 mFlushingAudio = FLUSHED;
566 } else {
Andreas Huber1aef2112011-01-04 14:01:29 -0800567 CHECK(IsFlushingState(mFlushingVideo, &needShutdown));
Andreas Huberf9334412010-12-15 15:17:42 -0800568 mFlushingVideo = FLUSHED;
Andreas Huber3fe62152011-09-16 15:09:22 -0700569
570 mVideoLateByUs = 0;
Andreas Huberf9334412010-12-15 15:17:42 -0800571 }
572
Steve Block3856b092011-10-20 11:56:00 +0100573 ALOGV("decoder %s flush completed", audio ? "audio" : "video");
Andreas Huberf9334412010-12-15 15:17:42 -0800574
Andreas Huber1aef2112011-01-04 14:01:29 -0800575 if (needShutdown) {
Steve Block3856b092011-10-20 11:56:00 +0100576 ALOGV("initiating %s decoder shutdown",
Andreas Huber53df1a42010-12-22 10:03:04 -0800577 audio ? "audio" : "video");
Andreas Huberf9334412010-12-15 15:17:42 -0800578
Andreas Huber53df1a42010-12-22 10:03:04 -0800579 (audio ? mAudioDecoder : mVideoDecoder)->initiateShutdown();
Andreas Huberf9334412010-12-15 15:17:42 -0800580
Andreas Huber53df1a42010-12-22 10:03:04 -0800581 if (audio) {
582 mFlushingAudio = SHUTTING_DOWN_DECODER;
583 } else {
584 mFlushingVideo = SHUTTING_DOWN_DECODER;
585 }
Andreas Huberf9334412010-12-15 15:17:42 -0800586 }
Andreas Huber3831a062010-12-21 10:22:33 -0800587
588 finishFlushIfPossible();
Andreas Huber2c2814b2010-12-15 17:18:20 -0800589 } else if (what == ACodec::kWhatOutputFormatChanged) {
Andreas Huber31e25082011-01-10 10:38:31 -0800590 if (audio) {
591 int32_t numChannels;
Andreas Huber516dacf2012-12-03 15:20:40 -0800592 CHECK(codecRequest->findInt32(
593 "channel-count", &numChannels));
Andreas Huber2c2814b2010-12-15 17:18:20 -0800594
Andreas Huber31e25082011-01-10 10:38:31 -0800595 int32_t sampleRate;
596 CHECK(codecRequest->findInt32("sample-rate", &sampleRate));
Andreas Huber2c2814b2010-12-15 17:18:20 -0800597
Steve Block3856b092011-10-20 11:56:00 +0100598 ALOGV("Audio output format changed to %d Hz, %d channels",
Andreas Huber31e25082011-01-10 10:38:31 -0800599 sampleRate, numChannels);
Andreas Huber2c2814b2010-12-15 17:18:20 -0800600
Andreas Huber31e25082011-01-10 10:38:31 -0800601 mAudioSink->close();
Eric Laurent1948eb32012-04-13 16:50:19 -0700602
603 audio_output_flags_t flags;
604 int64_t durationUs;
Andreas Huber516dacf2012-12-03 15:20:40 -0800605 // FIXME: we should handle the case where the video decoder
606 // is created after we receive the format change indication.
607 // Current code will just make that we select deep buffer
608 // with video which should not be a problem as it should
Eric Laurent1948eb32012-04-13 16:50:19 -0700609 // not prevent from keeping A/V sync.
610 if (mVideoDecoder == NULL &&
611 mSource->getDuration(&durationUs) == OK &&
Andreas Huber516dacf2012-12-03 15:20:40 -0800612 durationUs
613 > AUDIO_SINK_MIN_DEEP_BUFFER_DURATION_US) {
Eric Laurent1948eb32012-04-13 16:50:19 -0700614 flags = AUDIO_OUTPUT_FLAG_DEEP_BUFFER;
615 } else {
616 flags = AUDIO_OUTPUT_FLAG_NONE;
617 }
618
Andreas Huber98065552012-05-03 11:33:01 -0700619 int32_t channelMask;
620 if (!codecRequest->findInt32("channel-mask", &channelMask)) {
621 channelMask = CHANNEL_MASK_USE_CHANNEL_ORDER;
622 }
623
Andreas Huber078cfcf2011-09-15 12:25:04 -0700624 CHECK_EQ(mAudioSink->open(
625 sampleRate,
626 numChannels,
Andreas Huber98065552012-05-03 11:33:01 -0700627 (audio_channel_mask_t)channelMask,
Andreas Huber078cfcf2011-09-15 12:25:04 -0700628 AUDIO_FORMAT_PCM_16_BIT,
Eric Laurent1948eb32012-04-13 16:50:19 -0700629 8 /* bufferCount */,
630 NULL,
631 NULL,
632 flags),
Andreas Huber078cfcf2011-09-15 12:25:04 -0700633 (status_t)OK);
Andreas Huber31e25082011-01-10 10:38:31 -0800634 mAudioSink->start();
Andreas Huber2c2814b2010-12-15 17:18:20 -0800635
Andreas Huber31e25082011-01-10 10:38:31 -0800636 mRenderer->signalAudioSinkChanged();
637 } else {
638 // video
Andreas Huber3831a062010-12-21 10:22:33 -0800639
Andreas Huber31e25082011-01-10 10:38:31 -0800640 int32_t width, height;
641 CHECK(codecRequest->findInt32("width", &width));
642 CHECK(codecRequest->findInt32("height", &height));
643
644 int32_t cropLeft, cropTop, cropRight, cropBottom;
645 CHECK(codecRequest->findRect(
646 "crop",
647 &cropLeft, &cropTop, &cropRight, &cropBottom));
648
Andreas Huber516dacf2012-12-03 15:20:40 -0800649 int32_t displayWidth = cropRight - cropLeft + 1;
650 int32_t displayHeight = cropBottom - cropTop + 1;
651
Steve Block3856b092011-10-20 11:56:00 +0100652 ALOGV("Video output format changed to %d x %d "
Andreas Hubercb67cd12011-08-26 16:02:19 -0700653 "(crop: %d x %d @ (%d, %d))",
Andreas Huber31e25082011-01-10 10:38:31 -0800654 width, height,
Andreas Huber516dacf2012-12-03 15:20:40 -0800655 displayWidth,
656 displayHeight,
Andreas Hubercb67cd12011-08-26 16:02:19 -0700657 cropLeft, cropTop);
Andreas Huber31e25082011-01-10 10:38:31 -0800658
Andreas Huber516dacf2012-12-03 15:20:40 -0800659 sp<AMessage> videoInputFormat =
660 mSource->getFormat(false /* audio */);
661
662 // Take into account sample aspect ratio if necessary:
663 int32_t sarWidth, sarHeight;
664 if (videoInputFormat->findInt32("sar-width", &sarWidth)
665 && videoInputFormat->findInt32(
666 "sar-height", &sarHeight)) {
667 ALOGV("Sample aspect ratio %d : %d",
668 sarWidth, sarHeight);
669
670 displayWidth = (displayWidth * sarWidth) / sarHeight;
671
672 ALOGV("display dimensions %d x %d",
673 displayWidth, displayHeight);
674 }
675
Andreas Huber31e25082011-01-10 10:38:31 -0800676 notifyListener(
Andreas Huber516dacf2012-12-03 15:20:40 -0800677 MEDIA_SET_VIDEO_SIZE, displayWidth, displayHeight);
Andreas Huber31e25082011-01-10 10:38:31 -0800678 }
Andreas Huber3831a062010-12-21 10:22:33 -0800679 } else if (what == ACodec::kWhatShutdownCompleted) {
Steve Block3856b092011-10-20 11:56:00 +0100680 ALOGV("%s shutdown completed", audio ? "audio" : "video");
Andreas Huber3831a062010-12-21 10:22:33 -0800681 if (audio) {
682 mAudioDecoder.clear();
683
684 CHECK_EQ((int)mFlushingAudio, (int)SHUTTING_DOWN_DECODER);
685 mFlushingAudio = SHUT_DOWN;
686 } else {
687 mVideoDecoder.clear();
688
689 CHECK_EQ((int)mFlushingVideo, (int)SHUTTING_DOWN_DECODER);
690 mFlushingVideo = SHUT_DOWN;
691 }
692
693 finishFlushIfPossible();
Andreas Huberc92fd242011-08-16 13:48:44 -0700694 } else if (what == ACodec::kWhatError) {
Steve Block29357bc2012-01-06 19:20:56 +0000695 ALOGE("Received error from %s decoder, aborting playback.",
Andreas Huberc92fd242011-08-16 13:48:44 -0700696 audio ? "audio" : "video");
697
698 mRenderer->queueEOS(audio, UNKNOWN_ERROR);
Andreas Huber57788222012-02-21 11:47:18 -0800699 } else if (what == ACodec::kWhatDrainThisBuffer) {
Andreas Huberf9334412010-12-15 15:17:42 -0800700 renderBuffer(audio, codecRequest);
Lajos Molnar259f1622014-02-21 15:39:01 -0800701 } else if (what != ACodec::kWhatComponentAllocated
702 && what != ACodec::kWhatComponentConfigured
Andreas Hubera1f8ab02012-11-30 10:53:22 -0800703 && what != ACodec::kWhatBuffersAllocated) {
704 ALOGV("Unhandled codec notification %d '%c%c%c%c'.",
705 what,
706 what >> 24,
707 (what >> 16) & 0xff,
708 (what >> 8) & 0xff,
709 what & 0xff);
Andreas Huberf9334412010-12-15 15:17:42 -0800710 }
711
712 break;
713 }
714
715 case kWhatRendererNotify:
716 {
717 int32_t what;
718 CHECK(msg->findInt32("what", &what));
719
720 if (what == Renderer::kWhatEOS) {
721 int32_t audio;
722 CHECK(msg->findInt32("audio", &audio));
723
Andreas Huberc92fd242011-08-16 13:48:44 -0700724 int32_t finalResult;
725 CHECK(msg->findInt32("finalResult", &finalResult));
726
Andreas Huberf9334412010-12-15 15:17:42 -0800727 if (audio) {
728 mAudioEOS = true;
729 } else {
730 mVideoEOS = true;
731 }
732
Andreas Huberc92fd242011-08-16 13:48:44 -0700733 if (finalResult == ERROR_END_OF_STREAM) {
Steve Block3856b092011-10-20 11:56:00 +0100734 ALOGV("reached %s EOS", audio ? "audio" : "video");
Andreas Huberc92fd242011-08-16 13:48:44 -0700735 } else {
Steve Block29357bc2012-01-06 19:20:56 +0000736 ALOGE("%s track encountered an error (%d)",
Andreas Huberc92fd242011-08-16 13:48:44 -0700737 audio ? "audio" : "video", finalResult);
738
739 notifyListener(
740 MEDIA_ERROR, MEDIA_ERROR_UNKNOWN, finalResult);
741 }
Andreas Huberf9334412010-12-15 15:17:42 -0800742
743 if ((mAudioEOS || mAudioDecoder == NULL)
744 && (mVideoEOS || mVideoDecoder == NULL)) {
745 notifyListener(MEDIA_PLAYBACK_COMPLETE, 0, 0);
746 }
Andreas Huber43c3e6c2011-01-05 12:17:08 -0800747 } else if (what == Renderer::kWhatPosition) {
748 int64_t positionUs;
749 CHECK(msg->findInt64("positionUs", &positionUs));
750
Andreas Huber3fe62152011-09-16 15:09:22 -0700751 CHECK(msg->findInt64("videoLateByUs", &mVideoLateByUs));
752
Andreas Huber43c3e6c2011-01-05 12:17:08 -0800753 if (mDriver != NULL) {
754 sp<NuPlayerDriver> driver = mDriver.promote();
755 if (driver != NULL) {
756 driver->notifyPosition(positionUs);
Andreas Huber3fe62152011-09-16 15:09:22 -0700757
758 driver->notifyFrameStats(
759 mNumFramesTotal, mNumFramesDropped);
Andreas Huber43c3e6c2011-01-05 12:17:08 -0800760 }
761 }
Andreas Huber3fe62152011-09-16 15:09:22 -0700762 } else if (what == Renderer::kWhatFlushComplete) {
Andreas Huberf9334412010-12-15 15:17:42 -0800763 int32_t audio;
764 CHECK(msg->findInt32("audio", &audio));
765
Steve Block3856b092011-10-20 11:56:00 +0100766 ALOGV("renderer %s flush completed.", audio ? "audio" : "video");
James Dongf57b4ea2012-07-20 13:38:36 -0700767 } else if (what == Renderer::kWhatVideoRenderingStart) {
768 notifyListener(MEDIA_INFO, MEDIA_INFO_RENDERING_START, 0);
Lajos Molnarcbaffcf2013-08-14 18:30:38 -0700769 } else if (what == Renderer::kWhatMediaRenderingStart) {
770 ALOGV("media rendering started");
771 notifyListener(MEDIA_STARTED, 0, 0);
Andreas Huberf9334412010-12-15 15:17:42 -0800772 }
773 break;
774 }
775
776 case kWhatMoreDataQueued:
777 {
778 break;
779 }
780
Andreas Huber1aef2112011-01-04 14:01:29 -0800781 case kWhatReset:
782 {
Steve Block3856b092011-10-20 11:56:00 +0100783 ALOGV("kWhatReset");
Andreas Huber1aef2112011-01-04 14:01:29 -0800784
Andreas Hubera1f8ab02012-11-30 10:53:22 -0800785 mDeferredActions.push_back(
Andreas Huber14f76722013-01-15 09:04:18 -0800786 new ShutdownDecoderAction(
787 true /* audio */, true /* video */));
Andreas Huberb7c8e912012-11-27 15:02:53 -0800788
Andreas Hubera1f8ab02012-11-30 10:53:22 -0800789 mDeferredActions.push_back(
790 new SimpleAction(&NuPlayer::performReset));
Andreas Huberb58ce9f2011-11-28 16:27:35 -0800791
Andreas Hubera1f8ab02012-11-30 10:53:22 -0800792 processDeferredActions();
Andreas Huber1aef2112011-01-04 14:01:29 -0800793 break;
794 }
795
Andreas Huber43c3e6c2011-01-05 12:17:08 -0800796 case kWhatSeek:
797 {
798 int64_t seekTimeUs;
799 CHECK(msg->findInt64("seekTimeUs", &seekTimeUs));
800
Andreas Hubera1f8ab02012-11-30 10:53:22 -0800801 ALOGV("kWhatSeek seekTimeUs=%lld us", seekTimeUs);
Andreas Huber43c3e6c2011-01-05 12:17:08 -0800802
Andreas Hubera1f8ab02012-11-30 10:53:22 -0800803 mDeferredActions.push_back(
804 new SimpleAction(&NuPlayer::performDecoderFlush));
Andreas Huber43c3e6c2011-01-05 12:17:08 -0800805
Andreas Hubera1f8ab02012-11-30 10:53:22 -0800806 mDeferredActions.push_back(new SeekAction(seekTimeUs));
Andreas Huber43c3e6c2011-01-05 12:17:08 -0800807
Andreas Hubera1f8ab02012-11-30 10:53:22 -0800808 processDeferredActions();
Andreas Huber43c3e6c2011-01-05 12:17:08 -0800809 break;
810 }
811
Andreas Huberb4082222011-01-20 15:23:04 -0800812 case kWhatPause:
813 {
814 CHECK(mRenderer != NULL);
Roger Jönssonfba60da2013-01-21 17:15:45 +0100815 mSource->pause();
Andreas Huberb4082222011-01-20 15:23:04 -0800816 mRenderer->pause();
817 break;
818 }
819
820 case kWhatResume:
821 {
822 CHECK(mRenderer != NULL);
Roger Jönssonfba60da2013-01-21 17:15:45 +0100823 mSource->resume();
Andreas Huberb4082222011-01-20 15:23:04 -0800824 mRenderer->resume();
825 break;
826 }
827
Andreas Huberb5f25f02013-02-05 10:14:26 -0800828 case kWhatSourceNotify:
829 {
Andreas Huber9575c962013-02-05 13:59:56 -0800830 onSourceNotify(msg);
Andreas Huberb5f25f02013-02-05 10:14:26 -0800831 break;
832 }
833
Andreas Huberf9334412010-12-15 15:17:42 -0800834 default:
835 TRESPASS();
836 break;
837 }
838}
839
Andreas Huber3831a062010-12-21 10:22:33 -0800840void NuPlayer::finishFlushIfPossible() {
841 if (mFlushingAudio != FLUSHED && mFlushingAudio != SHUT_DOWN) {
842 return;
843 }
844
845 if (mFlushingVideo != FLUSHED && mFlushingVideo != SHUT_DOWN) {
846 return;
847 }
848
Steve Block3856b092011-10-20 11:56:00 +0100849 ALOGV("both audio and video are flushed now.");
Andreas Huber3831a062010-12-21 10:22:33 -0800850
Andreas Huber6e3d3112011-11-28 12:36:11 -0800851 if (mTimeDiscontinuityPending) {
852 mRenderer->signalTimeDiscontinuity();
853 mTimeDiscontinuityPending = false;
854 }
Andreas Huber3831a062010-12-21 10:22:33 -0800855
Andreas Huber22fc52f2011-01-05 16:24:27 -0800856 if (mAudioDecoder != NULL) {
Andreas Huber3831a062010-12-21 10:22:33 -0800857 mAudioDecoder->signalResume();
858 }
859
Andreas Huber22fc52f2011-01-05 16:24:27 -0800860 if (mVideoDecoder != NULL) {
Andreas Huber3831a062010-12-21 10:22:33 -0800861 mVideoDecoder->signalResume();
862 }
863
864 mFlushingAudio = NONE;
865 mFlushingVideo = NONE;
Andreas Huber3831a062010-12-21 10:22:33 -0800866
Andreas Hubera1f8ab02012-11-30 10:53:22 -0800867 processDeferredActions();
Andreas Huber1aef2112011-01-04 14:01:29 -0800868}
869
870void NuPlayer::postScanSources() {
871 if (mScanSourcesPending) {
872 return;
873 }
874
875 sp<AMessage> msg = new AMessage(kWhatScanSources, id());
876 msg->setInt32("generation", mScanSourcesGeneration);
877 msg->post();
878
879 mScanSourcesPending = true;
880}
881
Andreas Huber5bc087c2010-12-23 10:27:40 -0800882status_t NuPlayer::instantiateDecoder(bool audio, sp<Decoder> *decoder) {
Andreas Huberf9334412010-12-15 15:17:42 -0800883 if (*decoder != NULL) {
884 return OK;
885 }
886
Andreas Huber84066782011-08-16 09:34:26 -0700887 sp<AMessage> format = mSource->getFormat(audio);
Andreas Huberf9334412010-12-15 15:17:42 -0800888
Andreas Huber84066782011-08-16 09:34:26 -0700889 if (format == NULL) {
Andreas Huberf9334412010-12-15 15:17:42 -0800890 return -EWOULDBLOCK;
891 }
892
Andreas Huber3fe62152011-09-16 15:09:22 -0700893 if (!audio) {
Andreas Huber84066782011-08-16 09:34:26 -0700894 AString mime;
895 CHECK(format->findString("mime", &mime));
896 mVideoIsAVC = !strcasecmp(MEDIA_MIMETYPE_VIDEO_AVC, mime.c_str());
Andreas Huber3fe62152011-09-16 15:09:22 -0700897 }
898
Andreas Huberf9334412010-12-15 15:17:42 -0800899 sp<AMessage> notify =
900 new AMessage(audio ? kWhatAudioNotify : kWhatVideoNotify,
901 id());
902
Glenn Kasten11731182011-02-08 17:26:17 -0800903 *decoder = audio ? new Decoder(notify) :
904 new Decoder(notify, mNativeWindow);
Andreas Huberf9334412010-12-15 15:17:42 -0800905 looper()->registerHandler(*decoder);
906
Andreas Huber84066782011-08-16 09:34:26 -0700907 (*decoder)->configure(format);
Andreas Huberf9334412010-12-15 15:17:42 -0800908
Andreas Huberf9334412010-12-15 15:17:42 -0800909 return OK;
910}
911
912status_t NuPlayer::feedDecoderInputData(bool audio, const sp<AMessage> &msg) {
913 sp<AMessage> reply;
914 CHECK(msg->findMessage("reply", &reply));
915
Andreas Huber53df1a42010-12-22 10:03:04 -0800916 if ((audio && IsFlushingState(mFlushingAudio))
917 || (!audio && IsFlushingState(mFlushingVideo))) {
Andreas Huberf9334412010-12-15 15:17:42 -0800918 reply->setInt32("err", INFO_DISCONTINUITY);
919 reply->post();
920 return OK;
921 }
922
923 sp<ABuffer> accessUnit;
Andreas Huberf9334412010-12-15 15:17:42 -0800924
Andreas Huber3fe62152011-09-16 15:09:22 -0700925 bool dropAccessUnit;
926 do {
927 status_t err = mSource->dequeueAccessUnit(audio, &accessUnit);
Andreas Huber5bc087c2010-12-23 10:27:40 -0800928
Andreas Huber3fe62152011-09-16 15:09:22 -0700929 if (err == -EWOULDBLOCK) {
930 return err;
931 } else if (err != OK) {
932 if (err == INFO_DISCONTINUITY) {
933 int32_t type;
934 CHECK(accessUnit->meta()->findInt32("discontinuity", &type));
Andreas Huber53df1a42010-12-22 10:03:04 -0800935
Andreas Huber3fe62152011-09-16 15:09:22 -0700936 bool formatChange =
Andreas Huber6e3d3112011-11-28 12:36:11 -0800937 (audio &&
938 (type & ATSParser::DISCONTINUITY_AUDIO_FORMAT))
939 || (!audio &&
940 (type & ATSParser::DISCONTINUITY_VIDEO_FORMAT));
Andreas Huber53df1a42010-12-22 10:03:04 -0800941
Andreas Huber6e3d3112011-11-28 12:36:11 -0800942 bool timeChange = (type & ATSParser::DISCONTINUITY_TIME) != 0;
943
Steve Blockdf64d152012-01-04 20:05:49 +0000944 ALOGI("%s discontinuity (formatChange=%d, time=%d)",
Andreas Huber6e3d3112011-11-28 12:36:11 -0800945 audio ? "audio" : "video", formatChange, timeChange);
Andreas Huber32f3cef2011-03-02 15:34:46 -0800946
Andreas Huber3fe62152011-09-16 15:09:22 -0700947 if (audio) {
948 mSkipRenderingAudioUntilMediaTimeUs = -1;
949 } else {
950 mSkipRenderingVideoUntilMediaTimeUs = -1;
951 }
Andreas Huber32f3cef2011-03-02 15:34:46 -0800952
Andreas Huber6e3d3112011-11-28 12:36:11 -0800953 if (timeChange) {
954 sp<AMessage> extra;
955 if (accessUnit->meta()->findMessage("extra", &extra)
956 && extra != NULL) {
957 int64_t resumeAtMediaTimeUs;
958 if (extra->findInt64(
959 "resume-at-mediatimeUs", &resumeAtMediaTimeUs)) {
Steve Blockdf64d152012-01-04 20:05:49 +0000960 ALOGI("suppressing rendering of %s until %lld us",
Andreas Huber6e3d3112011-11-28 12:36:11 -0800961 audio ? "audio" : "video", resumeAtMediaTimeUs);
Andreas Huber3fe62152011-09-16 15:09:22 -0700962
Andreas Huber6e3d3112011-11-28 12:36:11 -0800963 if (audio) {
964 mSkipRenderingAudioUntilMediaTimeUs =
965 resumeAtMediaTimeUs;
966 } else {
967 mSkipRenderingVideoUntilMediaTimeUs =
968 resumeAtMediaTimeUs;
969 }
Andreas Huber3fe62152011-09-16 15:09:22 -0700970 }
Andreas Huber32f3cef2011-03-02 15:34:46 -0800971 }
972 }
Andreas Huber3fe62152011-09-16 15:09:22 -0700973
Andreas Huber6e3d3112011-11-28 12:36:11 -0800974 mTimeDiscontinuityPending =
975 mTimeDiscontinuityPending || timeChange;
976
977 if (formatChange || timeChange) {
Andreas Hubera1f8ab02012-11-30 10:53:22 -0800978 if (mFlushingAudio == NONE && mFlushingVideo == NONE) {
979 // And we'll resume scanning sources once we're done
980 // flushing.
981 mDeferredActions.push_front(
982 new SimpleAction(
983 &NuPlayer::performScanSources));
984 }
985
Robert Shih6d0a94e2014-01-23 16:18:22 -0800986 sp<AMessage> newFormat = mSource->getFormat(audio);
987 sp<Decoder> &decoder = audio ? mAudioDecoder : mVideoDecoder;
988 if (formatChange && !decoder->supportsSeamlessFormatChange(newFormat)) {
989 flushDecoder(audio, /* needShutdown = */ true);
990 } else {
991 flushDecoder(audio, /* needShutdown = */ false);
992 err = OK;
993 }
Andreas Huber6e3d3112011-11-28 12:36:11 -0800994 } else {
995 // This stream is unaffected by the discontinuity
996
997 if (audio) {
998 mFlushingAudio = FLUSHED;
999 } else {
1000 mFlushingVideo = FLUSHED;
1001 }
1002
1003 finishFlushIfPossible();
1004
1005 return -EWOULDBLOCK;
1006 }
Andreas Huber32f3cef2011-03-02 15:34:46 -08001007 }
1008
Andreas Huber3fe62152011-09-16 15:09:22 -07001009 reply->setInt32("err", err);
1010 reply->post();
1011 return OK;
Andreas Huberf9334412010-12-15 15:17:42 -08001012 }
1013
Andreas Huber3fe62152011-09-16 15:09:22 -07001014 if (!audio) {
1015 ++mNumFramesTotal;
1016 }
1017
1018 dropAccessUnit = false;
1019 if (!audio
1020 && mVideoLateByUs > 100000ll
1021 && mVideoIsAVC
1022 && !IsAVCReferenceFrame(accessUnit)) {
1023 dropAccessUnit = true;
1024 ++mNumFramesDropped;
1025 }
1026 } while (dropAccessUnit);
Andreas Huberf9334412010-12-15 15:17:42 -08001027
Steve Block3856b092011-10-20 11:56:00 +01001028 // ALOGV("returned a valid buffer of %s data", audio ? "audio" : "video");
Andreas Huberf9334412010-12-15 15:17:42 -08001029
1030#if 0
1031 int64_t mediaTimeUs;
1032 CHECK(accessUnit->meta()->findInt64("timeUs", &mediaTimeUs));
Steve Block3856b092011-10-20 11:56:00 +01001033 ALOGV("feeding %s input buffer at media time %.2f secs",
Andreas Huberf9334412010-12-15 15:17:42 -08001034 audio ? "audio" : "video",
1035 mediaTimeUs / 1E6);
1036#endif
1037
Andreas Huber2d8bedd2012-02-21 14:38:23 -08001038 reply->setBuffer("buffer", accessUnit);
Andreas Huberf9334412010-12-15 15:17:42 -08001039 reply->post();
1040
1041 return OK;
1042}
1043
1044void NuPlayer::renderBuffer(bool audio, const sp<AMessage> &msg) {
Steve Block3856b092011-10-20 11:56:00 +01001045 // ALOGV("renderBuffer %s", audio ? "audio" : "video");
Andreas Huberf9334412010-12-15 15:17:42 -08001046
1047 sp<AMessage> reply;
1048 CHECK(msg->findMessage("reply", &reply));
1049
Andreas Huber18ac5402011-08-31 15:04:25 -07001050 if (IsFlushingState(audio ? mFlushingAudio : mFlushingVideo)) {
1051 // We're currently attempting to flush the decoder, in order
1052 // to complete this, the decoder wants all its buffers back,
1053 // so we don't want any output buffers it sent us (from before
1054 // we initiated the flush) to be stuck in the renderer's queue.
1055
Steve Block3856b092011-10-20 11:56:00 +01001056 ALOGV("we're still flushing the %s decoder, sending its output buffer"
Andreas Huber18ac5402011-08-31 15:04:25 -07001057 " right back.", audio ? "audio" : "video");
1058
1059 reply->post();
1060 return;
1061 }
1062
Andreas Huber2d8bedd2012-02-21 14:38:23 -08001063 sp<ABuffer> buffer;
1064 CHECK(msg->findBuffer("buffer", &buffer));
Andreas Huberf9334412010-12-15 15:17:42 -08001065
Andreas Huber32f3cef2011-03-02 15:34:46 -08001066 int64_t &skipUntilMediaTimeUs =
1067 audio
1068 ? mSkipRenderingAudioUntilMediaTimeUs
1069 : mSkipRenderingVideoUntilMediaTimeUs;
1070
1071 if (skipUntilMediaTimeUs >= 0) {
1072 int64_t mediaTimeUs;
1073 CHECK(buffer->meta()->findInt64("timeUs", &mediaTimeUs));
1074
1075 if (mediaTimeUs < skipUntilMediaTimeUs) {
Steve Block3856b092011-10-20 11:56:00 +01001076 ALOGV("dropping %s buffer at time %lld as requested.",
Andreas Huber32f3cef2011-03-02 15:34:46 -08001077 audio ? "audio" : "video",
1078 mediaTimeUs);
1079
1080 reply->post();
1081 return;
1082 }
1083
1084 skipUntilMediaTimeUs = -1;
1085 }
1086
Andreas Huberf9334412010-12-15 15:17:42 -08001087 mRenderer->queueBuffer(audio, buffer, reply);
1088}
1089
Chong Zhangdcb89b32013-08-06 09:44:47 -07001090void NuPlayer::notifyListener(int msg, int ext1, int ext2, const Parcel *in) {
Andreas Huber43c3e6c2011-01-05 12:17:08 -08001091 if (mDriver == NULL) {
Andreas Huberf9334412010-12-15 15:17:42 -08001092 return;
1093 }
1094
Andreas Huber43c3e6c2011-01-05 12:17:08 -08001095 sp<NuPlayerDriver> driver = mDriver.promote();
Andreas Huberf9334412010-12-15 15:17:42 -08001096
Andreas Huber43c3e6c2011-01-05 12:17:08 -08001097 if (driver == NULL) {
Andreas Huberf9334412010-12-15 15:17:42 -08001098 return;
1099 }
1100
Chong Zhangdcb89b32013-08-06 09:44:47 -07001101 driver->notifyListener(msg, ext1, ext2, in);
Andreas Huberf9334412010-12-15 15:17:42 -08001102}
1103
Andreas Huber1aef2112011-01-04 14:01:29 -08001104void NuPlayer::flushDecoder(bool audio, bool needShutdown) {
Andreas Huber14f76722013-01-15 09:04:18 -08001105 ALOGV("[%s] flushDecoder needShutdown=%d",
1106 audio ? "audio" : "video", needShutdown);
1107
Andreas Huber6e3d3112011-11-28 12:36:11 -08001108 if ((audio && mAudioDecoder == NULL) || (!audio && mVideoDecoder == NULL)) {
Steve Blockdf64d152012-01-04 20:05:49 +00001109 ALOGI("flushDecoder %s without decoder present",
Andreas Huber6e3d3112011-11-28 12:36:11 -08001110 audio ? "audio" : "video");
1111 }
1112
Andreas Huber1aef2112011-01-04 14:01:29 -08001113 // Make sure we don't continue to scan sources until we finish flushing.
1114 ++mScanSourcesGeneration;
Andreas Huber43c3e6c2011-01-05 12:17:08 -08001115 mScanSourcesPending = false;
Andreas Huber1aef2112011-01-04 14:01:29 -08001116
1117 (audio ? mAudioDecoder : mVideoDecoder)->signalFlush();
1118 mRenderer->flush(audio);
1119
1120 FlushStatus newStatus =
1121 needShutdown ? FLUSHING_DECODER_SHUTDOWN : FLUSHING_DECODER;
1122
1123 if (audio) {
1124 CHECK(mFlushingAudio == NONE
1125 || mFlushingAudio == AWAITING_DISCONTINUITY);
1126
1127 mFlushingAudio = newStatus;
1128
1129 if (mFlushingVideo == NONE) {
1130 mFlushingVideo = (mVideoDecoder != NULL)
1131 ? AWAITING_DISCONTINUITY
1132 : FLUSHED;
1133 }
1134 } else {
1135 CHECK(mFlushingVideo == NONE
1136 || mFlushingVideo == AWAITING_DISCONTINUITY);
1137
1138 mFlushingVideo = newStatus;
1139
1140 if (mFlushingAudio == NONE) {
1141 mFlushingAudio = (mAudioDecoder != NULL)
1142 ? AWAITING_DISCONTINUITY
1143 : FLUSHED;
1144 }
1145 }
1146}
1147
Andreas Huber84066782011-08-16 09:34:26 -07001148sp<AMessage> NuPlayer::Source::getFormat(bool audio) {
1149 sp<MetaData> meta = getFormatMeta(audio);
1150
1151 if (meta == NULL) {
1152 return NULL;
1153 }
1154
1155 sp<AMessage> msg = new AMessage;
1156
1157 if(convertMetaDataToMessage(meta, &msg) == OK) {
1158 return msg;
1159 }
1160 return NULL;
1161}
1162
James Dong0d268a32012-08-31 12:18:27 -07001163status_t NuPlayer::setVideoScalingMode(int32_t mode) {
1164 mVideoScalingMode = mode;
Andreas Huber57a339c2012-12-03 11:18:00 -08001165 if (mNativeWindow != NULL) {
James Dong0d268a32012-08-31 12:18:27 -07001166 status_t ret = native_window_set_scaling_mode(
1167 mNativeWindow->getNativeWindow().get(), mVideoScalingMode);
1168 if (ret != OK) {
1169 ALOGE("Failed to set scaling mode (%d): %s",
1170 -ret, strerror(-ret));
1171 return ret;
1172 }
1173 }
1174 return OK;
1175}
1176
Chong Zhangdcb89b32013-08-06 09:44:47 -07001177status_t NuPlayer::getTrackInfo(Parcel* reply) const {
1178 sp<AMessage> msg = new AMessage(kWhatGetTrackInfo, id());
1179 msg->setPointer("reply", reply);
1180
1181 sp<AMessage> response;
1182 status_t err = msg->postAndAwaitResponse(&response);
1183 return err;
1184}
1185
1186status_t NuPlayer::selectTrack(size_t trackIndex, bool select) {
1187 sp<AMessage> msg = new AMessage(kWhatSelectTrack, id());
1188 msg->setSize("trackIndex", trackIndex);
1189 msg->setInt32("select", select);
1190
1191 sp<AMessage> response;
1192 status_t err = msg->postAndAwaitResponse(&response);
1193
1194 return err;
1195}
1196
Andreas Huberb7c8e912012-11-27 15:02:53 -08001197void NuPlayer::schedulePollDuration() {
1198 sp<AMessage> msg = new AMessage(kWhatPollDuration, id());
1199 msg->setInt32("generation", mPollDurationGeneration);
1200 msg->post();
1201}
1202
1203void NuPlayer::cancelPollDuration() {
1204 ++mPollDurationGeneration;
1205}
1206
Andreas Hubera1f8ab02012-11-30 10:53:22 -08001207void NuPlayer::processDeferredActions() {
1208 while (!mDeferredActions.empty()) {
1209 // We won't execute any deferred actions until we're no longer in
1210 // an intermediate state, i.e. one more more decoders are currently
1211 // flushing or shutting down.
1212
1213 if (mRenderer != NULL) {
1214 // There's an edge case where the renderer owns all output
1215 // buffers and is paused, therefore the decoder will not read
1216 // more input data and will never encounter the matching
1217 // discontinuity. To avoid this, we resume the renderer.
1218
1219 if (mFlushingAudio == AWAITING_DISCONTINUITY
1220 || mFlushingVideo == AWAITING_DISCONTINUITY) {
1221 mRenderer->resume();
1222 }
1223 }
1224
1225 if (mFlushingAudio != NONE || mFlushingVideo != NONE) {
1226 // We're currently flushing, postpone the reset until that's
1227 // completed.
1228
1229 ALOGV("postponing action mFlushingAudio=%d, mFlushingVideo=%d",
1230 mFlushingAudio, mFlushingVideo);
1231
1232 break;
1233 }
1234
1235 sp<Action> action = *mDeferredActions.begin();
1236 mDeferredActions.erase(mDeferredActions.begin());
1237
1238 action->execute(this);
1239 }
1240}
1241
1242void NuPlayer::performSeek(int64_t seekTimeUs) {
1243 ALOGV("performSeek seekTimeUs=%lld us (%.2f secs)",
1244 seekTimeUs,
1245 seekTimeUs / 1E6);
1246
1247 mSource->seekTo(seekTimeUs);
1248
1249 if (mDriver != NULL) {
1250 sp<NuPlayerDriver> driver = mDriver.promote();
1251 if (driver != NULL) {
1252 driver->notifyPosition(seekTimeUs);
1253 driver->notifySeekComplete();
1254 }
1255 }
1256
1257 // everything's flushed, continue playback.
1258}
1259
1260void NuPlayer::performDecoderFlush() {
1261 ALOGV("performDecoderFlush");
1262
Andreas Huberda9740e2013-04-16 10:54:03 -07001263 if (mAudioDecoder == NULL && mVideoDecoder == NULL) {
Andreas Hubera1f8ab02012-11-30 10:53:22 -08001264 return;
1265 }
1266
1267 mTimeDiscontinuityPending = true;
1268
1269 if (mAudioDecoder != NULL) {
1270 flushDecoder(true /* audio */, false /* needShutdown */);
1271 }
1272
1273 if (mVideoDecoder != NULL) {
1274 flushDecoder(false /* audio */, false /* needShutdown */);
1275 }
1276}
1277
Andreas Huber14f76722013-01-15 09:04:18 -08001278void NuPlayer::performDecoderShutdown(bool audio, bool video) {
1279 ALOGV("performDecoderShutdown audio=%d, video=%d", audio, video);
Andreas Hubera1f8ab02012-11-30 10:53:22 -08001280
Andreas Huber14f76722013-01-15 09:04:18 -08001281 if ((!audio || mAudioDecoder == NULL)
1282 && (!video || mVideoDecoder == NULL)) {
Andreas Hubera1f8ab02012-11-30 10:53:22 -08001283 return;
1284 }
1285
1286 mTimeDiscontinuityPending = true;
1287
Andreas Huber14f76722013-01-15 09:04:18 -08001288 if (mFlushingAudio == NONE && (!audio || mAudioDecoder == NULL)) {
1289 mFlushingAudio = FLUSHED;
1290 }
1291
1292 if (mFlushingVideo == NONE && (!video || mVideoDecoder == NULL)) {
1293 mFlushingVideo = FLUSHED;
1294 }
1295
1296 if (audio && mAudioDecoder != NULL) {
Andreas Hubera1f8ab02012-11-30 10:53:22 -08001297 flushDecoder(true /* audio */, true /* needShutdown */);
1298 }
1299
Andreas Huber14f76722013-01-15 09:04:18 -08001300 if (video && mVideoDecoder != NULL) {
Andreas Hubera1f8ab02012-11-30 10:53:22 -08001301 flushDecoder(false /* audio */, true /* needShutdown */);
1302 }
1303}
1304
1305void NuPlayer::performReset() {
1306 ALOGV("performReset");
1307
1308 CHECK(mAudioDecoder == NULL);
1309 CHECK(mVideoDecoder == NULL);
1310
1311 cancelPollDuration();
1312
1313 ++mScanSourcesGeneration;
1314 mScanSourcesPending = false;
1315
1316 mRenderer.clear();
1317
1318 if (mSource != NULL) {
1319 mSource->stop();
Andreas Huberb5f25f02013-02-05 10:14:26 -08001320
1321 looper()->unregisterHandler(mSource->id());
1322
Andreas Hubera1f8ab02012-11-30 10:53:22 -08001323 mSource.clear();
1324 }
1325
1326 if (mDriver != NULL) {
1327 sp<NuPlayerDriver> driver = mDriver.promote();
1328 if (driver != NULL) {
1329 driver->notifyResetComplete();
1330 }
1331 }
Andreas Huber57a339c2012-12-03 11:18:00 -08001332
1333 mStarted = false;
Andreas Hubera1f8ab02012-11-30 10:53:22 -08001334}
1335
1336void NuPlayer::performScanSources() {
1337 ALOGV("performScanSources");
1338
Andreas Huber57a339c2012-12-03 11:18:00 -08001339 if (!mStarted) {
1340 return;
1341 }
1342
Andreas Hubera1f8ab02012-11-30 10:53:22 -08001343 if (mAudioDecoder == NULL || mVideoDecoder == NULL) {
1344 postScanSources();
1345 }
1346}
1347
Andreas Huber57a339c2012-12-03 11:18:00 -08001348void NuPlayer::performSetSurface(const sp<NativeWindowWrapper> &wrapper) {
1349 ALOGV("performSetSurface");
1350
1351 mNativeWindow = wrapper;
1352
1353 // XXX - ignore error from setVideoScalingMode for now
1354 setVideoScalingMode(mVideoScalingMode);
1355
1356 if (mDriver != NULL) {
1357 sp<NuPlayerDriver> driver = mDriver.promote();
1358 if (driver != NULL) {
1359 driver->notifySetSurfaceComplete();
1360 }
1361 }
1362}
1363
Andreas Huber9575c962013-02-05 13:59:56 -08001364void NuPlayer::onSourceNotify(const sp<AMessage> &msg) {
1365 int32_t what;
1366 CHECK(msg->findInt32("what", &what));
1367
1368 switch (what) {
1369 case Source::kWhatPrepared:
1370 {
Andreas Huberb5f28d42013-04-25 15:11:19 -07001371 if (mSource == NULL) {
1372 // This is a stale notification from a source that was
1373 // asynchronously preparing when the client called reset().
1374 // We handled the reset, the source is gone.
1375 break;
1376 }
1377
Andreas Huberec0c5972013-02-05 14:47:13 -08001378 int32_t err;
1379 CHECK(msg->findInt32("err", &err));
1380
Andreas Huber9575c962013-02-05 13:59:56 -08001381 sp<NuPlayerDriver> driver = mDriver.promote();
1382 if (driver != NULL) {
Andreas Huberec0c5972013-02-05 14:47:13 -08001383 driver->notifyPrepareCompleted(err);
Andreas Huber9575c962013-02-05 13:59:56 -08001384 }
Andreas Huber99759402013-04-01 14:28:31 -07001385
1386 int64_t durationUs;
1387 if (mDriver != NULL && mSource->getDuration(&durationUs) == OK) {
1388 sp<NuPlayerDriver> driver = mDriver.promote();
1389 if (driver != NULL) {
1390 driver->notifyDuration(durationUs);
1391 }
1392 }
Andreas Huber9575c962013-02-05 13:59:56 -08001393 break;
1394 }
1395
1396 case Source::kWhatFlagsChanged:
1397 {
1398 uint32_t flags;
1399 CHECK(msg->findInt32("flags", (int32_t *)&flags));
1400
Chong Zhang4b7069d2013-09-11 12:52:43 -07001401 sp<NuPlayerDriver> driver = mDriver.promote();
1402 if (driver != NULL) {
1403 driver->notifyFlagsChanged(flags);
1404 }
1405
Andreas Huber9575c962013-02-05 13:59:56 -08001406 if ((mSourceFlags & Source::FLAG_DYNAMIC_DURATION)
1407 && (!(flags & Source::FLAG_DYNAMIC_DURATION))) {
1408 cancelPollDuration();
1409 } else if (!(mSourceFlags & Source::FLAG_DYNAMIC_DURATION)
1410 && (flags & Source::FLAG_DYNAMIC_DURATION)
1411 && (mAudioDecoder != NULL || mVideoDecoder != NULL)) {
1412 schedulePollDuration();
1413 }
1414
1415 mSourceFlags = flags;
1416 break;
1417 }
1418
1419 case Source::kWhatVideoSizeChanged:
1420 {
1421 int32_t width, height;
1422 CHECK(msg->findInt32("width", &width));
1423 CHECK(msg->findInt32("height", &height));
1424
1425 notifyListener(MEDIA_SET_VIDEO_SIZE, width, height);
1426 break;
1427 }
1428
Roger Jönssonb50e83e2013-01-21 16:26:41 +01001429 case Source::kWhatBufferingStart:
1430 {
1431 notifyListener(MEDIA_INFO, MEDIA_INFO_BUFFERING_START, 0);
1432 break;
1433 }
1434
1435 case Source::kWhatBufferingEnd:
1436 {
1437 notifyListener(MEDIA_INFO, MEDIA_INFO_BUFFERING_END, 0);
1438 break;
1439 }
1440
Chong Zhangdcb89b32013-08-06 09:44:47 -07001441 case Source::kWhatSubtitleData:
1442 {
1443 sp<ABuffer> buffer;
1444 CHECK(msg->findBuffer("buffer", &buffer));
1445
1446 int32_t trackIndex;
1447 int64_t timeUs, durationUs;
1448 CHECK(buffer->meta()->findInt32("trackIndex", &trackIndex));
1449 CHECK(buffer->meta()->findInt64("timeUs", &timeUs));
1450 CHECK(buffer->meta()->findInt64("durationUs", &durationUs));
1451
1452 Parcel in;
1453 in.writeInt32(trackIndex);
1454 in.writeInt64(timeUs);
1455 in.writeInt64(durationUs);
1456 in.writeInt32(buffer->size());
1457 in.writeInt32(buffer->size());
1458 in.write(buffer->data(), buffer->size());
1459
1460 notifyListener(MEDIA_SUBTITLE_DATA, 0, 0, &in);
1461 break;
1462 }
1463
Andreas Huber14f76722013-01-15 09:04:18 -08001464 case Source::kWhatQueueDecoderShutdown:
1465 {
1466 int32_t audio, video;
1467 CHECK(msg->findInt32("audio", &audio));
1468 CHECK(msg->findInt32("video", &video));
1469
1470 sp<AMessage> reply;
1471 CHECK(msg->findMessage("reply", &reply));
1472
1473 queueDecoderShutdown(audio, video, reply);
1474 break;
1475 }
1476
Andreas Huber9575c962013-02-05 13:59:56 -08001477 default:
1478 TRESPASS();
1479 }
1480}
1481
Andreas Huberb5f25f02013-02-05 10:14:26 -08001482////////////////////////////////////////////////////////////////////////////////
1483
Andreas Huber9575c962013-02-05 13:59:56 -08001484void NuPlayer::Source::notifyFlagsChanged(uint32_t flags) {
1485 sp<AMessage> notify = dupNotify();
1486 notify->setInt32("what", kWhatFlagsChanged);
1487 notify->setInt32("flags", flags);
1488 notify->post();
1489}
1490
1491void NuPlayer::Source::notifyVideoSizeChanged(int32_t width, int32_t height) {
1492 sp<AMessage> notify = dupNotify();
1493 notify->setInt32("what", kWhatVideoSizeChanged);
1494 notify->setInt32("width", width);
1495 notify->setInt32("height", height);
1496 notify->post();
1497}
1498
Andreas Huberec0c5972013-02-05 14:47:13 -08001499void NuPlayer::Source::notifyPrepared(status_t err) {
Andreas Huber9575c962013-02-05 13:59:56 -08001500 sp<AMessage> notify = dupNotify();
1501 notify->setInt32("what", kWhatPrepared);
Andreas Huberec0c5972013-02-05 14:47:13 -08001502 notify->setInt32("err", err);
Andreas Huber9575c962013-02-05 13:59:56 -08001503 notify->post();
1504}
1505
Andreas Huber84333e02014-02-07 15:36:10 -08001506void NuPlayer::Source::onMessageReceived(const sp<AMessage> & /* msg */) {
Andreas Huberb5f25f02013-02-05 10:14:26 -08001507 TRESPASS();
1508}
1509
Andreas Huber14f76722013-01-15 09:04:18 -08001510void NuPlayer::queueDecoderShutdown(
1511 bool audio, bool video, const sp<AMessage> &reply) {
1512 ALOGI("queueDecoderShutdown audio=%d, video=%d", audio, video);
1513
1514 mDeferredActions.push_back(
1515 new ShutdownDecoderAction(audio, video));
1516
1517 mDeferredActions.push_back(
1518 new SimpleAction(&NuPlayer::performScanSources));
1519
1520 mDeferredActions.push_back(new PostMessageAction(reply));
1521
1522 processDeferredActions();
1523}
1524
Andreas Huberf9334412010-12-15 15:17:42 -08001525} // namespace android