blob: a750ad0c4fd12b5ef983f5b96b747725cc7bcccc [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
Martin Storsjoda38df52013-09-25 16:05:36 +030034#include "SoftwareRenderer.h"
35
Andreas Huber3831a062010-12-21 10:22:33 -080036#include <media/stagefright/foundation/hexdump.h>
Andreas Huberf9334412010-12-15 15:17:42 -080037#include <media/stagefright/foundation/ABuffer.h>
38#include <media/stagefright/foundation/ADebug.h>
39#include <media/stagefright/foundation/AMessage.h>
40#include <media/stagefright/ACodec.h>
Andreas Huber3fe62152011-09-16 15:09:22 -070041#include <media/stagefright/MediaDefs.h>
Andreas Huberf9334412010-12-15 15:17:42 -080042#include <media/stagefright/MediaErrors.h>
43#include <media/stagefright/MetaData.h>
Andy McFadden8ba01022012-12-18 09:46:54 -080044#include <gui/IGraphicBufferProducer.h>
Andreas Huberf9334412010-12-15 15:17:42 -080045
Andreas Huber3fe62152011-09-16 15:09:22 -070046#include "avc_utils.h"
47
Andreas Huber84066782011-08-16 09:34:26 -070048#include "ESDS.h"
49#include <media/stagefright/Utils.h>
50
Andreas Huberf9334412010-12-15 15:17:42 -080051namespace android {
52
Andreas Hubera1f8ab02012-11-30 10:53:22 -080053struct NuPlayer::Action : public RefBase {
54 Action() {}
55
56 virtual void execute(NuPlayer *player) = 0;
57
58private:
59 DISALLOW_EVIL_CONSTRUCTORS(Action);
60};
61
62struct NuPlayer::SeekAction : public Action {
63 SeekAction(int64_t seekTimeUs)
64 : mSeekTimeUs(seekTimeUs) {
65 }
66
67 virtual void execute(NuPlayer *player) {
68 player->performSeek(mSeekTimeUs);
69 }
70
71private:
72 int64_t mSeekTimeUs;
73
74 DISALLOW_EVIL_CONSTRUCTORS(SeekAction);
75};
76
Andreas Huber57a339c2012-12-03 11:18:00 -080077struct NuPlayer::SetSurfaceAction : public Action {
78 SetSurfaceAction(const sp<NativeWindowWrapper> &wrapper)
79 : mWrapper(wrapper) {
80 }
81
82 virtual void execute(NuPlayer *player) {
83 player->performSetSurface(mWrapper);
84 }
85
86private:
87 sp<NativeWindowWrapper> mWrapper;
88
89 DISALLOW_EVIL_CONSTRUCTORS(SetSurfaceAction);
90};
91
Andreas Huber14f76722013-01-15 09:04:18 -080092struct NuPlayer::ShutdownDecoderAction : public Action {
93 ShutdownDecoderAction(bool audio, bool video)
94 : mAudio(audio),
95 mVideo(video) {
96 }
97
98 virtual void execute(NuPlayer *player) {
99 player->performDecoderShutdown(mAudio, mVideo);
100 }
101
102private:
103 bool mAudio;
104 bool mVideo;
105
106 DISALLOW_EVIL_CONSTRUCTORS(ShutdownDecoderAction);
107};
108
109struct NuPlayer::PostMessageAction : public Action {
110 PostMessageAction(const sp<AMessage> &msg)
111 : mMessage(msg) {
112 }
113
114 virtual void execute(NuPlayer *) {
115 mMessage->post();
116 }
117
118private:
119 sp<AMessage> mMessage;
120
121 DISALLOW_EVIL_CONSTRUCTORS(PostMessageAction);
122};
123
Andreas Hubera1f8ab02012-11-30 10:53:22 -0800124// Use this if there's no state necessary to save in order to execute
125// the action.
126struct NuPlayer::SimpleAction : public Action {
127 typedef void (NuPlayer::*ActionFunc)();
128
129 SimpleAction(ActionFunc func)
130 : mFunc(func) {
131 }
132
133 virtual void execute(NuPlayer *player) {
134 (player->*mFunc)();
135 }
136
137private:
138 ActionFunc mFunc;
139
140 DISALLOW_EVIL_CONSTRUCTORS(SimpleAction);
141};
142
Andreas Huberf9334412010-12-15 15:17:42 -0800143////////////////////////////////////////////////////////////////////////////////
144
145NuPlayer::NuPlayer()
Andreas Huber9b80c2b2011-06-30 15:47:02 -0700146 : mUIDValid(false),
Andreas Huber9575c962013-02-05 13:59:56 -0800147 mSourceFlags(0),
Andreas Huber3fe62152011-09-16 15:09:22 -0700148 mVideoIsAVC(false),
Martin Storsjoda38df52013-09-25 16:05:36 +0300149 mNeedsSwRenderer(false),
Andreas Huber9b80c2b2011-06-30 15:47:02 -0700150 mAudioEOS(false),
Andreas Huberf9334412010-12-15 15:17:42 -0800151 mVideoEOS(false),
Andreas Huber5bc087c2010-12-23 10:27:40 -0800152 mScanSourcesPending(false),
Andreas Huber1aef2112011-01-04 14:01:29 -0800153 mScanSourcesGeneration(0),
Andreas Huberb7c8e912012-11-27 15:02:53 -0800154 mPollDurationGeneration(0),
Andreas Huber6e3d3112011-11-28 12:36:11 -0800155 mTimeDiscontinuityPending(false),
Andreas Huberf9334412010-12-15 15:17:42 -0800156 mFlushingAudio(NONE),
Andreas Huber1aef2112011-01-04 14:01:29 -0800157 mFlushingVideo(NONE),
Andreas Huber3fe62152011-09-16 15:09:22 -0700158 mSkipRenderingAudioUntilMediaTimeUs(-1ll),
159 mSkipRenderingVideoUntilMediaTimeUs(-1ll),
160 mVideoLateByUs(0ll),
161 mNumFramesTotal(0ll),
James Dong0d268a32012-08-31 12:18:27 -0700162 mNumFramesDropped(0ll),
Andreas Huber57a339c2012-12-03 11:18:00 -0800163 mVideoScalingMode(NATIVE_WINDOW_SCALING_MODE_SCALE_TO_WINDOW),
164 mStarted(false) {
Andreas Huberf9334412010-12-15 15:17:42 -0800165}
166
167NuPlayer::~NuPlayer() {
168}
169
Andreas Huber9b80c2b2011-06-30 15:47:02 -0700170void NuPlayer::setUID(uid_t uid) {
171 mUIDValid = true;
172 mUID = uid;
173}
174
Andreas Huber43c3e6c2011-01-05 12:17:08 -0800175void NuPlayer::setDriver(const wp<NuPlayerDriver> &driver) {
176 mDriver = driver;
Andreas Huberf9334412010-12-15 15:17:42 -0800177}
178
Andreas Huber9575c962013-02-05 13:59:56 -0800179void NuPlayer::setDataSourceAsync(const sp<IStreamSource> &source) {
Andreas Huberf9334412010-12-15 15:17:42 -0800180 sp<AMessage> msg = new AMessage(kWhatSetDataSource, id());
181
Andreas Huberb5f25f02013-02-05 10:14:26 -0800182 sp<AMessage> notify = new AMessage(kWhatSourceNotify, id());
183
Andreas Huber240abcc2014-02-13 13:32:37 -0800184 msg->setObject("source", new StreamingSource(notify, source));
Andreas Huber5bc087c2010-12-23 10:27:40 -0800185 msg->post();
186}
Andreas Huberf9334412010-12-15 15:17:42 -0800187
Andreas Huberafed0e12011-09-20 15:39:58 -0700188static bool IsHTTPLiveURL(const char *url) {
189 if (!strncasecmp("http://", url, 7)
Andreas Huber99759402013-04-01 14:28:31 -0700190 || !strncasecmp("https://", url, 8)
191 || !strncasecmp("file://", url, 7)) {
Andreas Huberafed0e12011-09-20 15:39:58 -0700192 size_t len = strlen(url);
193 if (len >= 5 && !strcasecmp(".m3u8", &url[len - 5])) {
194 return true;
195 }
196
197 if (strstr(url,"m3u8")) {
198 return true;
199 }
200 }
201
202 return false;
203}
204
Andreas Huber9575c962013-02-05 13:59:56 -0800205void NuPlayer::setDataSourceAsync(
Andreas Huber1b86fe02014-01-29 11:13:26 -0800206 const sp<IMediaHTTPService> &httpService,
207 const char *url,
208 const KeyedVector<String8, String8> *headers) {
Andreas Huber5bc087c2010-12-23 10:27:40 -0800209 sp<AMessage> msg = new AMessage(kWhatSetDataSource, id());
Oscar Rydhé7a33b772012-02-20 10:15:48 +0100210 size_t len = strlen(url);
Andreas Huber5bc087c2010-12-23 10:27:40 -0800211
Andreas Huberb5f25f02013-02-05 10:14:26 -0800212 sp<AMessage> notify = new AMessage(kWhatSourceNotify, id());
213
Andreas Huberafed0e12011-09-20 15:39:58 -0700214 sp<Source> source;
215 if (IsHTTPLiveURL(url)) {
Andreas Huber81e68442014-02-05 11:52:33 -0800216 source = new HTTPLiveSource(notify, httpService, url, headers);
Andreas Huberafed0e12011-09-20 15:39:58 -0700217 } else if (!strncasecmp(url, "rtsp://", 7)) {
Andreas Huber1b86fe02014-01-29 11:13:26 -0800218 source = new RTSPSource(
219 notify, httpService, url, headers, mUIDValid, mUID);
Oscar Rydhé7a33b772012-02-20 10:15:48 +0100220 } else if ((!strncasecmp(url, "http://", 7)
221 || !strncasecmp(url, "https://", 8))
222 && ((len >= 4 && !strcasecmp(".sdp", &url[len - 4]))
223 || strstr(url, ".sdp?"))) {
Andreas Huber1b86fe02014-01-29 11:13:26 -0800224 source = new RTSPSource(
225 notify, httpService, url, headers, mUIDValid, mUID, true);
Andreas Huber2bfdd422011-10-11 15:24:07 -0700226 } else {
Andreas Huber81e68442014-02-05 11:52:33 -0800227 source = new GenericSource(notify, httpService, url, headers);
Andreas Huber2bfdd422011-10-11 15:24:07 -0700228 }
229
Andreas Huberafed0e12011-09-20 15:39:58 -0700230 msg->setObject("source", source);
231 msg->post();
232}
233
Andreas Huber9575c962013-02-05 13:59:56 -0800234void NuPlayer::setDataSourceAsync(int fd, int64_t offset, int64_t length) {
Andreas Huberafed0e12011-09-20 15:39:58 -0700235 sp<AMessage> msg = new AMessage(kWhatSetDataSource, id());
236
Andreas Huberb5f25f02013-02-05 10:14:26 -0800237 sp<AMessage> notify = new AMessage(kWhatSourceNotify, id());
238
239 sp<Source> source = new GenericSource(notify, fd, offset, length);
Andreas Huberafed0e12011-09-20 15:39:58 -0700240 msg->setObject("source", source);
Andreas Huberf9334412010-12-15 15:17:42 -0800241 msg->post();
242}
243
Andreas Huber9575c962013-02-05 13:59:56 -0800244void NuPlayer::prepareAsync() {
245 (new AMessage(kWhatPrepare, id()))->post();
246}
247
Andreas Huber57a339c2012-12-03 11:18:00 -0800248void NuPlayer::setVideoSurfaceTextureAsync(
Andy McFadden8ba01022012-12-18 09:46:54 -0800249 const sp<IGraphicBufferProducer> &bufferProducer) {
Glenn Kasten11731182011-02-08 17:26:17 -0800250 sp<AMessage> msg = new AMessage(kWhatSetVideoNativeWindow, id());
Andreas Huber57a339c2012-12-03 11:18:00 -0800251
Andy McFadden8ba01022012-12-18 09:46:54 -0800252 if (bufferProducer == NULL) {
Andreas Huber57a339c2012-12-03 11:18:00 -0800253 msg->setObject("native-window", NULL);
254 } else {
255 msg->setObject(
256 "native-window",
257 new NativeWindowWrapper(
Mathias Agopian1a2952a2013-02-14 17:11:27 -0800258 new Surface(bufferProducer)));
Andreas Huber57a339c2012-12-03 11:18:00 -0800259 }
260
Andreas Huberf9334412010-12-15 15:17:42 -0800261 msg->post();
262}
263
264void NuPlayer::setAudioSink(const sp<MediaPlayerBase::AudioSink> &sink) {
265 sp<AMessage> msg = new AMessage(kWhatSetAudioSink, id());
266 msg->setObject("sink", sink);
267 msg->post();
268}
269
270void NuPlayer::start() {
271 (new AMessage(kWhatStart, id()))->post();
272}
273
Andreas Huber43c3e6c2011-01-05 12:17:08 -0800274void NuPlayer::pause() {
Andreas Huberb4082222011-01-20 15:23:04 -0800275 (new AMessage(kWhatPause, id()))->post();
Andreas Huber43c3e6c2011-01-05 12:17:08 -0800276}
277
278void NuPlayer::resume() {
Andreas Huberb4082222011-01-20 15:23:04 -0800279 (new AMessage(kWhatResume, id()))->post();
Andreas Huber43c3e6c2011-01-05 12:17:08 -0800280}
281
Andreas Huber1aef2112011-01-04 14:01:29 -0800282void NuPlayer::resetAsync() {
283 (new AMessage(kWhatReset, id()))->post();
284}
285
Andreas Huber43c3e6c2011-01-05 12:17:08 -0800286void NuPlayer::seekToAsync(int64_t seekTimeUs) {
287 sp<AMessage> msg = new AMessage(kWhatSeek, id());
288 msg->setInt64("seekTimeUs", seekTimeUs);
289 msg->post();
290}
291
Andreas Huber53df1a42010-12-22 10:03:04 -0800292// static
Andreas Huber1aef2112011-01-04 14:01:29 -0800293bool NuPlayer::IsFlushingState(FlushStatus state, bool *needShutdown) {
Andreas Huber53df1a42010-12-22 10:03:04 -0800294 switch (state) {
295 case FLUSHING_DECODER:
Andreas Huber1aef2112011-01-04 14:01:29 -0800296 if (needShutdown != NULL) {
297 *needShutdown = false;
Andreas Huber53df1a42010-12-22 10:03:04 -0800298 }
299 return true;
300
Andreas Huber1aef2112011-01-04 14:01:29 -0800301 case FLUSHING_DECODER_SHUTDOWN:
302 if (needShutdown != NULL) {
303 *needShutdown = true;
Andreas Huber53df1a42010-12-22 10:03:04 -0800304 }
305 return true;
306
307 default:
308 return false;
309 }
310}
311
Andreas Huberf9334412010-12-15 15:17:42 -0800312void NuPlayer::onMessageReceived(const sp<AMessage> &msg) {
313 switch (msg->what()) {
314 case kWhatSetDataSource:
315 {
Steve Block3856b092011-10-20 11:56:00 +0100316 ALOGV("kWhatSetDataSource");
Andreas Huberf9334412010-12-15 15:17:42 -0800317
318 CHECK(mSource == NULL);
319
Andreas Huber5bc087c2010-12-23 10:27:40 -0800320 sp<RefBase> obj;
321 CHECK(msg->findObject("source", &obj));
Andreas Huberf9334412010-12-15 15:17:42 -0800322
Andreas Huber5bc087c2010-12-23 10:27:40 -0800323 mSource = static_cast<Source *>(obj.get());
Andreas Huberb5f25f02013-02-05 10:14:26 -0800324
325 looper()->registerHandler(mSource);
Andreas Huber9575c962013-02-05 13:59:56 -0800326
327 CHECK(mDriver != NULL);
328 sp<NuPlayerDriver> driver = mDriver.promote();
329 if (driver != NULL) {
330 driver->notifySetDataSourceCompleted(OK);
331 }
332 break;
333 }
334
335 case kWhatPrepare:
336 {
337 mSource->prepareAsync();
Andreas Huberf9334412010-12-15 15:17:42 -0800338 break;
339 }
340
Chong Zhangdcb89b32013-08-06 09:44:47 -0700341 case kWhatGetTrackInfo:
342 {
343 uint32_t replyID;
344 CHECK(msg->senderAwaitsResponse(&replyID));
345
346 status_t err = INVALID_OPERATION;
347 if (mSource != NULL) {
348 Parcel* reply;
349 CHECK(msg->findPointer("reply", (void**)&reply));
350 err = mSource->getTrackInfo(reply);
351 }
352
353 sp<AMessage> response = new AMessage;
354 response->setInt32("err", err);
355
356 response->postReply(replyID);
357 break;
358 }
359
360 case kWhatSelectTrack:
361 {
362 uint32_t replyID;
363 CHECK(msg->senderAwaitsResponse(&replyID));
364
365 status_t err = INVALID_OPERATION;
366 if (mSource != NULL) {
367 size_t trackIndex;
368 int32_t select;
369 CHECK(msg->findSize("trackIndex", &trackIndex));
370 CHECK(msg->findInt32("select", &select));
371 err = mSource->selectTrack(trackIndex, select);
372 }
373
374 sp<AMessage> response = new AMessage;
375 response->setInt32("err", err);
376
377 response->postReply(replyID);
378 break;
379 }
380
Andreas Huberb7c8e912012-11-27 15:02:53 -0800381 case kWhatPollDuration:
382 {
383 int32_t generation;
384 CHECK(msg->findInt32("generation", &generation));
385
386 if (generation != mPollDurationGeneration) {
387 // stale
388 break;
389 }
390
391 int64_t durationUs;
392 if (mDriver != NULL && mSource->getDuration(&durationUs) == OK) {
393 sp<NuPlayerDriver> driver = mDriver.promote();
394 if (driver != NULL) {
395 driver->notifyDuration(durationUs);
396 }
397 }
398
399 msg->post(1000000ll); // poll again in a second.
400 break;
401 }
402
Glenn Kasten11731182011-02-08 17:26:17 -0800403 case kWhatSetVideoNativeWindow:
Andreas Huberf9334412010-12-15 15:17:42 -0800404 {
Steve Block3856b092011-10-20 11:56:00 +0100405 ALOGV("kWhatSetVideoNativeWindow");
Andreas Huberf9334412010-12-15 15:17:42 -0800406
Andreas Huber57a339c2012-12-03 11:18:00 -0800407 mDeferredActions.push_back(
Andreas Huber14f76722013-01-15 09:04:18 -0800408 new ShutdownDecoderAction(
409 false /* audio */, true /* video */));
Andreas Huber57a339c2012-12-03 11:18:00 -0800410
Andreas Huberf9334412010-12-15 15:17:42 -0800411 sp<RefBase> obj;
Glenn Kasten11731182011-02-08 17:26:17 -0800412 CHECK(msg->findObject("native-window", &obj));
Andreas Huberf9334412010-12-15 15:17:42 -0800413
Andreas Huber57a339c2012-12-03 11:18:00 -0800414 mDeferredActions.push_back(
415 new SetSurfaceAction(
416 static_cast<NativeWindowWrapper *>(obj.get())));
James Dong0d268a32012-08-31 12:18:27 -0700417
Andreas Huber57a339c2012-12-03 11:18:00 -0800418 if (obj != NULL) {
419 // If there is a new surface texture, instantiate decoders
420 // again if possible.
421 mDeferredActions.push_back(
422 new SimpleAction(&NuPlayer::performScanSources));
423 }
424
425 processDeferredActions();
Andreas Huberf9334412010-12-15 15:17:42 -0800426 break;
427 }
428
429 case kWhatSetAudioSink:
430 {
Steve Block3856b092011-10-20 11:56:00 +0100431 ALOGV("kWhatSetAudioSink");
Andreas Huberf9334412010-12-15 15:17:42 -0800432
433 sp<RefBase> obj;
434 CHECK(msg->findObject("sink", &obj));
435
436 mAudioSink = static_cast<MediaPlayerBase::AudioSink *>(obj.get());
437 break;
438 }
439
440 case kWhatStart:
441 {
Steve Block3856b092011-10-20 11:56:00 +0100442 ALOGV("kWhatStart");
Andreas Huber43c3e6c2011-01-05 12:17:08 -0800443
Andreas Huber3fe62152011-09-16 15:09:22 -0700444 mVideoIsAVC = false;
Martin Storsjoda38df52013-09-25 16:05:36 +0300445 mNeedsSwRenderer = false;
Andreas Huber1aef2112011-01-04 14:01:29 -0800446 mAudioEOS = false;
447 mVideoEOS = false;
Andreas Huber32f3cef2011-03-02 15:34:46 -0800448 mSkipRenderingAudioUntilMediaTimeUs = -1;
449 mSkipRenderingVideoUntilMediaTimeUs = -1;
Andreas Huber3fe62152011-09-16 15:09:22 -0700450 mVideoLateByUs = 0;
451 mNumFramesTotal = 0;
452 mNumFramesDropped = 0;
Andreas Huber57a339c2012-12-03 11:18:00 -0800453 mStarted = true;
Andreas Huber1aef2112011-01-04 14:01:29 -0800454
Andreas Huber5bc087c2010-12-23 10:27:40 -0800455 mSource->start();
Andreas Huberf9334412010-12-15 15:17:42 -0800456
Andreas Huberd5e56232013-03-12 11:01:43 -0700457 uint32_t flags = 0;
458
459 if (mSource->isRealTime()) {
460 flags |= Renderer::FLAG_REAL_TIME;
461 }
462
Andreas Huberf9334412010-12-15 15:17:42 -0800463 mRenderer = new Renderer(
464 mAudioSink,
Andreas Huberd5e56232013-03-12 11:01:43 -0700465 new AMessage(kWhatRendererNotify, id()),
466 flags);
Andreas Huberf9334412010-12-15 15:17:42 -0800467
468 looper()->registerHandler(mRenderer);
469
Andreas Huber1aef2112011-01-04 14:01:29 -0800470 postScanSources();
Andreas Huberf9334412010-12-15 15:17:42 -0800471 break;
472 }
473
474 case kWhatScanSources:
475 {
Andreas Huber1aef2112011-01-04 14:01:29 -0800476 int32_t generation;
477 CHECK(msg->findInt32("generation", &generation));
478 if (generation != mScanSourcesGeneration) {
479 // Drop obsolete msg.
480 break;
481 }
482
Andreas Huber5bc087c2010-12-23 10:27:40 -0800483 mScanSourcesPending = false;
484
Steve Block3856b092011-10-20 11:56:00 +0100485 ALOGV("scanning sources haveAudio=%d, haveVideo=%d",
Andreas Huber43c3e6c2011-01-05 12:17:08 -0800486 mAudioDecoder != NULL, mVideoDecoder != NULL);
487
Andreas Huberb7c8e912012-11-27 15:02:53 -0800488 bool mHadAnySourcesBefore =
489 (mAudioDecoder != NULL) || (mVideoDecoder != NULL);
490
Haynes Mathew George5d246ef2012-07-09 10:36:57 -0700491 if (mNativeWindow != NULL) {
492 instantiateDecoder(false, &mVideoDecoder);
493 }
Andreas Huberf9334412010-12-15 15:17:42 -0800494
495 if (mAudioSink != NULL) {
Andreas Huber5bc087c2010-12-23 10:27:40 -0800496 instantiateDecoder(true, &mAudioDecoder);
Andreas Huberf9334412010-12-15 15:17:42 -0800497 }
498
Andreas Huberb7c8e912012-11-27 15:02:53 -0800499 if (!mHadAnySourcesBefore
500 && (mAudioDecoder != NULL || mVideoDecoder != NULL)) {
501 // This is the first time we've found anything playable.
502
Andreas Huber9575c962013-02-05 13:59:56 -0800503 if (mSourceFlags & Source::FLAG_DYNAMIC_DURATION) {
Andreas Huberb7c8e912012-11-27 15:02:53 -0800504 schedulePollDuration();
505 }
506 }
507
Andreas Hubereac68ba2011-09-27 12:12:25 -0700508 status_t err;
509 if ((err = mSource->feedMoreTSData()) != OK) {
Andreas Huber1aef2112011-01-04 14:01:29 -0800510 if (mAudioDecoder == NULL && mVideoDecoder == NULL) {
511 // We're not currently decoding anything (no audio or
512 // video tracks found) and we just ran out of input data.
Andreas Hubereac68ba2011-09-27 12:12:25 -0700513
514 if (err == ERROR_END_OF_STREAM) {
515 notifyListener(MEDIA_PLAYBACK_COMPLETE, 0, 0);
516 } else {
517 notifyListener(MEDIA_ERROR, MEDIA_ERROR_UNKNOWN, err);
518 }
Andreas Huber1aef2112011-01-04 14:01:29 -0800519 }
Andreas Huberf9334412010-12-15 15:17:42 -0800520 break;
521 }
522
Andreas Huberfbe9d812012-08-31 14:05:27 -0700523 if ((mAudioDecoder == NULL && mAudioSink != NULL)
524 || (mVideoDecoder == NULL && mNativeWindow != NULL)) {
Andreas Huberf9334412010-12-15 15:17:42 -0800525 msg->post(100000ll);
Andreas Huber5bc087c2010-12-23 10:27:40 -0800526 mScanSourcesPending = true;
Andreas Huberf9334412010-12-15 15:17:42 -0800527 }
528 break;
529 }
530
531 case kWhatVideoNotify:
532 case kWhatAudioNotify:
533 {
534 bool audio = msg->what() == kWhatAudioNotify;
535
536 sp<AMessage> codecRequest;
537 CHECK(msg->findMessage("codec-request", &codecRequest));
538
539 int32_t what;
540 CHECK(codecRequest->findInt32("what", &what));
541
542 if (what == ACodec::kWhatFillThisBuffer) {
543 status_t err = feedDecoderInputData(
544 audio, codecRequest);
545
Andreas Huber5bc087c2010-12-23 10:27:40 -0800546 if (err == -EWOULDBLOCK) {
Andreas Hubereac68ba2011-09-27 12:12:25 -0700547 if (mSource->feedMoreTSData() == OK) {
Andreas Huber1183a4a2011-11-03 11:00:21 -0700548 msg->post(10000ll);
Andreas Huber5bc087c2010-12-23 10:27:40 -0800549 }
Andreas Huberf9334412010-12-15 15:17:42 -0800550 }
551 } else if (what == ACodec::kWhatEOS) {
Andreas Huberdc9bacd2011-09-26 10:53:29 -0700552 int32_t err;
553 CHECK(codecRequest->findInt32("err", &err));
554
555 if (err == ERROR_END_OF_STREAM) {
Steve Block3856b092011-10-20 11:56:00 +0100556 ALOGV("got %s decoder EOS", audio ? "audio" : "video");
Andreas Huberdc9bacd2011-09-26 10:53:29 -0700557 } else {
Steve Block3856b092011-10-20 11:56:00 +0100558 ALOGV("got %s decoder EOS w/ error %d",
Andreas Huberdc9bacd2011-09-26 10:53:29 -0700559 audio ? "audio" : "video",
560 err);
561 }
562
563 mRenderer->queueEOS(audio, err);
Andreas Huberf9334412010-12-15 15:17:42 -0800564 } else if (what == ACodec::kWhatFlushCompleted) {
Andreas Huber1aef2112011-01-04 14:01:29 -0800565 bool needShutdown;
Andreas Huber53df1a42010-12-22 10:03:04 -0800566
Andreas Huberf9334412010-12-15 15:17:42 -0800567 if (audio) {
Andreas Huber1aef2112011-01-04 14:01:29 -0800568 CHECK(IsFlushingState(mFlushingAudio, &needShutdown));
Andreas Huberf9334412010-12-15 15:17:42 -0800569 mFlushingAudio = FLUSHED;
570 } else {
Andreas Huber1aef2112011-01-04 14:01:29 -0800571 CHECK(IsFlushingState(mFlushingVideo, &needShutdown));
Andreas Huberf9334412010-12-15 15:17:42 -0800572 mFlushingVideo = FLUSHED;
Andreas Huber3fe62152011-09-16 15:09:22 -0700573
574 mVideoLateByUs = 0;
Andreas Huberf9334412010-12-15 15:17:42 -0800575 }
576
Steve Block3856b092011-10-20 11:56:00 +0100577 ALOGV("decoder %s flush completed", audio ? "audio" : "video");
Andreas Huberf9334412010-12-15 15:17:42 -0800578
Andreas Huber1aef2112011-01-04 14:01:29 -0800579 if (needShutdown) {
Steve Block3856b092011-10-20 11:56:00 +0100580 ALOGV("initiating %s decoder shutdown",
Andreas Huber53df1a42010-12-22 10:03:04 -0800581 audio ? "audio" : "video");
Andreas Huberf9334412010-12-15 15:17:42 -0800582
Andreas Huber53df1a42010-12-22 10:03:04 -0800583 (audio ? mAudioDecoder : mVideoDecoder)->initiateShutdown();
Andreas Huberf9334412010-12-15 15:17:42 -0800584
Andreas Huber53df1a42010-12-22 10:03:04 -0800585 if (audio) {
586 mFlushingAudio = SHUTTING_DOWN_DECODER;
587 } else {
588 mFlushingVideo = SHUTTING_DOWN_DECODER;
589 }
Andreas Huberf9334412010-12-15 15:17:42 -0800590 }
Andreas Huber3831a062010-12-21 10:22:33 -0800591
592 finishFlushIfPossible();
Andreas Huber2c2814b2010-12-15 17:18:20 -0800593 } else if (what == ACodec::kWhatOutputFormatChanged) {
Andreas Huber31e25082011-01-10 10:38:31 -0800594 if (audio) {
595 int32_t numChannels;
Andreas Huber516dacf2012-12-03 15:20:40 -0800596 CHECK(codecRequest->findInt32(
597 "channel-count", &numChannels));
Andreas Huber2c2814b2010-12-15 17:18:20 -0800598
Andreas Huber31e25082011-01-10 10:38:31 -0800599 int32_t sampleRate;
600 CHECK(codecRequest->findInt32("sample-rate", &sampleRate));
Andreas Huber2c2814b2010-12-15 17:18:20 -0800601
Steve Block3856b092011-10-20 11:56:00 +0100602 ALOGV("Audio output format changed to %d Hz, %d channels",
Andreas Huber31e25082011-01-10 10:38:31 -0800603 sampleRate, numChannels);
Andreas Huber2c2814b2010-12-15 17:18:20 -0800604
Andreas Huber31e25082011-01-10 10:38:31 -0800605 mAudioSink->close();
Eric Laurent1948eb32012-04-13 16:50:19 -0700606
607 audio_output_flags_t flags;
608 int64_t durationUs;
Andreas Huber516dacf2012-12-03 15:20:40 -0800609 // FIXME: we should handle the case where the video decoder
610 // is created after we receive the format change indication.
611 // Current code will just make that we select deep buffer
612 // with video which should not be a problem as it should
Eric Laurent1948eb32012-04-13 16:50:19 -0700613 // not prevent from keeping A/V sync.
614 if (mVideoDecoder == NULL &&
615 mSource->getDuration(&durationUs) == OK &&
Andreas Huber516dacf2012-12-03 15:20:40 -0800616 durationUs
617 > AUDIO_SINK_MIN_DEEP_BUFFER_DURATION_US) {
Eric Laurent1948eb32012-04-13 16:50:19 -0700618 flags = AUDIO_OUTPUT_FLAG_DEEP_BUFFER;
619 } else {
620 flags = AUDIO_OUTPUT_FLAG_NONE;
621 }
622
Andreas Huber98065552012-05-03 11:33:01 -0700623 int32_t channelMask;
624 if (!codecRequest->findInt32("channel-mask", &channelMask)) {
625 channelMask = CHANNEL_MASK_USE_CHANNEL_ORDER;
626 }
627
Andreas Huber078cfcf2011-09-15 12:25:04 -0700628 CHECK_EQ(mAudioSink->open(
629 sampleRate,
630 numChannels,
Andreas Huber98065552012-05-03 11:33:01 -0700631 (audio_channel_mask_t)channelMask,
Andreas Huber078cfcf2011-09-15 12:25:04 -0700632 AUDIO_FORMAT_PCM_16_BIT,
Eric Laurent1948eb32012-04-13 16:50:19 -0700633 8 /* bufferCount */,
634 NULL,
635 NULL,
636 flags),
Andreas Huber078cfcf2011-09-15 12:25:04 -0700637 (status_t)OK);
Andreas Huber31e25082011-01-10 10:38:31 -0800638 mAudioSink->start();
Andreas Huber2c2814b2010-12-15 17:18:20 -0800639
Andreas Huber31e25082011-01-10 10:38:31 -0800640 mRenderer->signalAudioSinkChanged();
641 } else {
642 // video
Andreas Huber3831a062010-12-21 10:22:33 -0800643
Andreas Huber31e25082011-01-10 10:38:31 -0800644 int32_t width, height;
645 CHECK(codecRequest->findInt32("width", &width));
646 CHECK(codecRequest->findInt32("height", &height));
647
648 int32_t cropLeft, cropTop, cropRight, cropBottom;
649 CHECK(codecRequest->findRect(
650 "crop",
651 &cropLeft, &cropTop, &cropRight, &cropBottom));
652
Andreas Huber516dacf2012-12-03 15:20:40 -0800653 int32_t displayWidth = cropRight - cropLeft + 1;
654 int32_t displayHeight = cropBottom - cropTop + 1;
655
Steve Block3856b092011-10-20 11:56:00 +0100656 ALOGV("Video output format changed to %d x %d "
Andreas Hubercb67cd12011-08-26 16:02:19 -0700657 "(crop: %d x %d @ (%d, %d))",
Andreas Huber31e25082011-01-10 10:38:31 -0800658 width, height,
Andreas Huber516dacf2012-12-03 15:20:40 -0800659 displayWidth,
660 displayHeight,
Andreas Hubercb67cd12011-08-26 16:02:19 -0700661 cropLeft, cropTop);
Andreas Huber31e25082011-01-10 10:38:31 -0800662
Andreas Huber516dacf2012-12-03 15:20:40 -0800663 sp<AMessage> videoInputFormat =
664 mSource->getFormat(false /* audio */);
665
666 // Take into account sample aspect ratio if necessary:
667 int32_t sarWidth, sarHeight;
668 if (videoInputFormat->findInt32("sar-width", &sarWidth)
669 && videoInputFormat->findInt32(
670 "sar-height", &sarHeight)) {
671 ALOGV("Sample aspect ratio %d : %d",
672 sarWidth, sarHeight);
673
674 displayWidth = (displayWidth * sarWidth) / sarHeight;
675
676 ALOGV("display dimensions %d x %d",
677 displayWidth, displayHeight);
678 }
679
Andreas Huber31e25082011-01-10 10:38:31 -0800680 notifyListener(
Andreas Huber516dacf2012-12-03 15:20:40 -0800681 MEDIA_SET_VIDEO_SIZE, displayWidth, displayHeight);
Martin Storsjoda38df52013-09-25 16:05:36 +0300682
683 if (mNeedsSwRenderer && mNativeWindow != NULL) {
684 int32_t colorFormat;
685 CHECK(codecRequest->findInt32("color-format", &colorFormat));
686
687 sp<MetaData> meta = new MetaData;
688 meta->setInt32(kKeyWidth, width);
689 meta->setInt32(kKeyHeight, height);
690 meta->setRect(kKeyCropRect, cropLeft, cropTop, cropRight, cropBottom);
691 meta->setInt32(kKeyColorFormat, colorFormat);
692
693 mRenderer->setSoftRenderer(
694 new SoftwareRenderer(mNativeWindow->getNativeWindow(), meta));
695 }
Andreas Huber31e25082011-01-10 10:38:31 -0800696 }
Andreas Huber3831a062010-12-21 10:22:33 -0800697 } else if (what == ACodec::kWhatShutdownCompleted) {
Steve Block3856b092011-10-20 11:56:00 +0100698 ALOGV("%s shutdown completed", audio ? "audio" : "video");
Andreas Huber3831a062010-12-21 10:22:33 -0800699 if (audio) {
700 mAudioDecoder.clear();
701
702 CHECK_EQ((int)mFlushingAudio, (int)SHUTTING_DOWN_DECODER);
703 mFlushingAudio = SHUT_DOWN;
704 } else {
705 mVideoDecoder.clear();
706
707 CHECK_EQ((int)mFlushingVideo, (int)SHUTTING_DOWN_DECODER);
708 mFlushingVideo = SHUT_DOWN;
709 }
710
711 finishFlushIfPossible();
Andreas Huberc92fd242011-08-16 13:48:44 -0700712 } else if (what == ACodec::kWhatError) {
Steve Block29357bc2012-01-06 19:20:56 +0000713 ALOGE("Received error from %s decoder, aborting playback.",
Andreas Huberc92fd242011-08-16 13:48:44 -0700714 audio ? "audio" : "video");
715
716 mRenderer->queueEOS(audio, UNKNOWN_ERROR);
Andreas Huber57788222012-02-21 11:47:18 -0800717 } else if (what == ACodec::kWhatDrainThisBuffer) {
Andreas Huberf9334412010-12-15 15:17:42 -0800718 renderBuffer(audio, codecRequest);
Martin Storsjoda38df52013-09-25 16:05:36 +0300719 } else if (what == ACodec::kWhatComponentAllocated) {
720 if (!audio) {
721 AString name;
722 CHECK(codecRequest->findString("componentName", &name));
723 mNeedsSwRenderer = name.startsWith("OMX.google.");
724 }
725 } else if (what != ACodec::kWhatComponentConfigured
Andreas Hubera1f8ab02012-11-30 10:53:22 -0800726 && what != ACodec::kWhatBuffersAllocated) {
727 ALOGV("Unhandled codec notification %d '%c%c%c%c'.",
728 what,
729 what >> 24,
730 (what >> 16) & 0xff,
731 (what >> 8) & 0xff,
732 what & 0xff);
Andreas Huberf9334412010-12-15 15:17:42 -0800733 }
734
735 break;
736 }
737
738 case kWhatRendererNotify:
739 {
740 int32_t what;
741 CHECK(msg->findInt32("what", &what));
742
743 if (what == Renderer::kWhatEOS) {
744 int32_t audio;
745 CHECK(msg->findInt32("audio", &audio));
746
Andreas Huberc92fd242011-08-16 13:48:44 -0700747 int32_t finalResult;
748 CHECK(msg->findInt32("finalResult", &finalResult));
749
Andreas Huberf9334412010-12-15 15:17:42 -0800750 if (audio) {
751 mAudioEOS = true;
752 } else {
753 mVideoEOS = true;
754 }
755
Andreas Huberc92fd242011-08-16 13:48:44 -0700756 if (finalResult == ERROR_END_OF_STREAM) {
Steve Block3856b092011-10-20 11:56:00 +0100757 ALOGV("reached %s EOS", audio ? "audio" : "video");
Andreas Huberc92fd242011-08-16 13:48:44 -0700758 } else {
Steve Block29357bc2012-01-06 19:20:56 +0000759 ALOGE("%s track encountered an error (%d)",
Andreas Huberc92fd242011-08-16 13:48:44 -0700760 audio ? "audio" : "video", finalResult);
761
762 notifyListener(
763 MEDIA_ERROR, MEDIA_ERROR_UNKNOWN, finalResult);
764 }
Andreas Huberf9334412010-12-15 15:17:42 -0800765
766 if ((mAudioEOS || mAudioDecoder == NULL)
767 && (mVideoEOS || mVideoDecoder == NULL)) {
768 notifyListener(MEDIA_PLAYBACK_COMPLETE, 0, 0);
769 }
Andreas Huber43c3e6c2011-01-05 12:17:08 -0800770 } else if (what == Renderer::kWhatPosition) {
771 int64_t positionUs;
772 CHECK(msg->findInt64("positionUs", &positionUs));
773
Andreas Huber3fe62152011-09-16 15:09:22 -0700774 CHECK(msg->findInt64("videoLateByUs", &mVideoLateByUs));
775
Andreas Huber43c3e6c2011-01-05 12:17:08 -0800776 if (mDriver != NULL) {
777 sp<NuPlayerDriver> driver = mDriver.promote();
778 if (driver != NULL) {
779 driver->notifyPosition(positionUs);
Andreas Huber3fe62152011-09-16 15:09:22 -0700780
781 driver->notifyFrameStats(
782 mNumFramesTotal, mNumFramesDropped);
Andreas Huber43c3e6c2011-01-05 12:17:08 -0800783 }
784 }
Andreas Huber3fe62152011-09-16 15:09:22 -0700785 } else if (what == Renderer::kWhatFlushComplete) {
Andreas Huberf9334412010-12-15 15:17:42 -0800786 int32_t audio;
787 CHECK(msg->findInt32("audio", &audio));
788
Steve Block3856b092011-10-20 11:56:00 +0100789 ALOGV("renderer %s flush completed.", audio ? "audio" : "video");
James Dongf57b4ea2012-07-20 13:38:36 -0700790 } else if (what == Renderer::kWhatVideoRenderingStart) {
791 notifyListener(MEDIA_INFO, MEDIA_INFO_RENDERING_START, 0);
Lajos Molnarcbaffcf2013-08-14 18:30:38 -0700792 } else if (what == Renderer::kWhatMediaRenderingStart) {
793 ALOGV("media rendering started");
794 notifyListener(MEDIA_STARTED, 0, 0);
Andreas Huberf9334412010-12-15 15:17:42 -0800795 }
796 break;
797 }
798
799 case kWhatMoreDataQueued:
800 {
801 break;
802 }
803
Andreas Huber1aef2112011-01-04 14:01:29 -0800804 case kWhatReset:
805 {
Steve Block3856b092011-10-20 11:56:00 +0100806 ALOGV("kWhatReset");
Andreas Huber1aef2112011-01-04 14:01:29 -0800807
Andreas Hubera1f8ab02012-11-30 10:53:22 -0800808 mDeferredActions.push_back(
Andreas Huber14f76722013-01-15 09:04:18 -0800809 new ShutdownDecoderAction(
810 true /* audio */, true /* video */));
Andreas Huberb7c8e912012-11-27 15:02:53 -0800811
Andreas Hubera1f8ab02012-11-30 10:53:22 -0800812 mDeferredActions.push_back(
813 new SimpleAction(&NuPlayer::performReset));
Andreas Huberb58ce9f2011-11-28 16:27:35 -0800814
Andreas Hubera1f8ab02012-11-30 10:53:22 -0800815 processDeferredActions();
Andreas Huber1aef2112011-01-04 14:01:29 -0800816 break;
817 }
818
Andreas Huber43c3e6c2011-01-05 12:17:08 -0800819 case kWhatSeek:
820 {
821 int64_t seekTimeUs;
822 CHECK(msg->findInt64("seekTimeUs", &seekTimeUs));
823
Andreas Hubera1f8ab02012-11-30 10:53:22 -0800824 ALOGV("kWhatSeek seekTimeUs=%lld us", seekTimeUs);
Andreas Huber43c3e6c2011-01-05 12:17:08 -0800825
Andreas Hubera1f8ab02012-11-30 10:53:22 -0800826 mDeferredActions.push_back(
827 new SimpleAction(&NuPlayer::performDecoderFlush));
Andreas Huber43c3e6c2011-01-05 12:17:08 -0800828
Andreas Hubera1f8ab02012-11-30 10:53:22 -0800829 mDeferredActions.push_back(new SeekAction(seekTimeUs));
Andreas Huber43c3e6c2011-01-05 12:17:08 -0800830
Andreas Hubera1f8ab02012-11-30 10:53:22 -0800831 processDeferredActions();
Andreas Huber43c3e6c2011-01-05 12:17:08 -0800832 break;
833 }
834
Andreas Huberb4082222011-01-20 15:23:04 -0800835 case kWhatPause:
836 {
837 CHECK(mRenderer != NULL);
Roger Jönssonfba60da2013-01-21 17:15:45 +0100838 mSource->pause();
Andreas Huberb4082222011-01-20 15:23:04 -0800839 mRenderer->pause();
840 break;
841 }
842
843 case kWhatResume:
844 {
845 CHECK(mRenderer != NULL);
Roger Jönssonfba60da2013-01-21 17:15:45 +0100846 mSource->resume();
Andreas Huberb4082222011-01-20 15:23:04 -0800847 mRenderer->resume();
848 break;
849 }
850
Andreas Huberb5f25f02013-02-05 10:14:26 -0800851 case kWhatSourceNotify:
852 {
Andreas Huber9575c962013-02-05 13:59:56 -0800853 onSourceNotify(msg);
Andreas Huberb5f25f02013-02-05 10:14:26 -0800854 break;
855 }
856
Andreas Huberf9334412010-12-15 15:17:42 -0800857 default:
858 TRESPASS();
859 break;
860 }
861}
862
Andreas Huber3831a062010-12-21 10:22:33 -0800863void NuPlayer::finishFlushIfPossible() {
864 if (mFlushingAudio != FLUSHED && mFlushingAudio != SHUT_DOWN) {
865 return;
866 }
867
868 if (mFlushingVideo != FLUSHED && mFlushingVideo != SHUT_DOWN) {
869 return;
870 }
871
Steve Block3856b092011-10-20 11:56:00 +0100872 ALOGV("both audio and video are flushed now.");
Andreas Huber3831a062010-12-21 10:22:33 -0800873
Andreas Huber6e3d3112011-11-28 12:36:11 -0800874 if (mTimeDiscontinuityPending) {
875 mRenderer->signalTimeDiscontinuity();
876 mTimeDiscontinuityPending = false;
877 }
Andreas Huber3831a062010-12-21 10:22:33 -0800878
Andreas Huber22fc52f2011-01-05 16:24:27 -0800879 if (mAudioDecoder != NULL) {
Andreas Huber3831a062010-12-21 10:22:33 -0800880 mAudioDecoder->signalResume();
881 }
882
Andreas Huber22fc52f2011-01-05 16:24:27 -0800883 if (mVideoDecoder != NULL) {
Andreas Huber3831a062010-12-21 10:22:33 -0800884 mVideoDecoder->signalResume();
885 }
886
887 mFlushingAudio = NONE;
888 mFlushingVideo = NONE;
Andreas Huber3831a062010-12-21 10:22:33 -0800889
Andreas Hubera1f8ab02012-11-30 10:53:22 -0800890 processDeferredActions();
Andreas Huber1aef2112011-01-04 14:01:29 -0800891}
892
893void NuPlayer::postScanSources() {
894 if (mScanSourcesPending) {
895 return;
896 }
897
898 sp<AMessage> msg = new AMessage(kWhatScanSources, id());
899 msg->setInt32("generation", mScanSourcesGeneration);
900 msg->post();
901
902 mScanSourcesPending = true;
903}
904
Andreas Huber5bc087c2010-12-23 10:27:40 -0800905status_t NuPlayer::instantiateDecoder(bool audio, sp<Decoder> *decoder) {
Andreas Huberf9334412010-12-15 15:17:42 -0800906 if (*decoder != NULL) {
907 return OK;
908 }
909
Andreas Huber84066782011-08-16 09:34:26 -0700910 sp<AMessage> format = mSource->getFormat(audio);
Andreas Huberf9334412010-12-15 15:17:42 -0800911
Andreas Huber84066782011-08-16 09:34:26 -0700912 if (format == NULL) {
Andreas Huberf9334412010-12-15 15:17:42 -0800913 return -EWOULDBLOCK;
914 }
915
Andreas Huber3fe62152011-09-16 15:09:22 -0700916 if (!audio) {
Andreas Huber84066782011-08-16 09:34:26 -0700917 AString mime;
918 CHECK(format->findString("mime", &mime));
919 mVideoIsAVC = !strcasecmp(MEDIA_MIMETYPE_VIDEO_AVC, mime.c_str());
Andreas Huber3fe62152011-09-16 15:09:22 -0700920 }
921
Andreas Huberf9334412010-12-15 15:17:42 -0800922 sp<AMessage> notify =
923 new AMessage(audio ? kWhatAudioNotify : kWhatVideoNotify,
924 id());
925
Glenn Kasten11731182011-02-08 17:26:17 -0800926 *decoder = audio ? new Decoder(notify) :
927 new Decoder(notify, mNativeWindow);
Andreas Huberf9334412010-12-15 15:17:42 -0800928 looper()->registerHandler(*decoder);
929
Andreas Huber84066782011-08-16 09:34:26 -0700930 (*decoder)->configure(format);
Andreas Huberf9334412010-12-15 15:17:42 -0800931
Andreas Huberf9334412010-12-15 15:17:42 -0800932 return OK;
933}
934
935status_t NuPlayer::feedDecoderInputData(bool audio, const sp<AMessage> &msg) {
936 sp<AMessage> reply;
937 CHECK(msg->findMessage("reply", &reply));
938
Andreas Huber53df1a42010-12-22 10:03:04 -0800939 if ((audio && IsFlushingState(mFlushingAudio))
940 || (!audio && IsFlushingState(mFlushingVideo))) {
Andreas Huberf9334412010-12-15 15:17:42 -0800941 reply->setInt32("err", INFO_DISCONTINUITY);
942 reply->post();
943 return OK;
944 }
945
946 sp<ABuffer> accessUnit;
Andreas Huberf9334412010-12-15 15:17:42 -0800947
Andreas Huber3fe62152011-09-16 15:09:22 -0700948 bool dropAccessUnit;
949 do {
950 status_t err = mSource->dequeueAccessUnit(audio, &accessUnit);
Andreas Huber5bc087c2010-12-23 10:27:40 -0800951
Andreas Huber3fe62152011-09-16 15:09:22 -0700952 if (err == -EWOULDBLOCK) {
953 return err;
954 } else if (err != OK) {
955 if (err == INFO_DISCONTINUITY) {
956 int32_t type;
957 CHECK(accessUnit->meta()->findInt32("discontinuity", &type));
Andreas Huber53df1a42010-12-22 10:03:04 -0800958
Andreas Huber3fe62152011-09-16 15:09:22 -0700959 bool formatChange =
Andreas Huber6e3d3112011-11-28 12:36:11 -0800960 (audio &&
961 (type & ATSParser::DISCONTINUITY_AUDIO_FORMAT))
962 || (!audio &&
963 (type & ATSParser::DISCONTINUITY_VIDEO_FORMAT));
Andreas Huber53df1a42010-12-22 10:03:04 -0800964
Andreas Huber6e3d3112011-11-28 12:36:11 -0800965 bool timeChange = (type & ATSParser::DISCONTINUITY_TIME) != 0;
966
Steve Blockdf64d152012-01-04 20:05:49 +0000967 ALOGI("%s discontinuity (formatChange=%d, time=%d)",
Andreas Huber6e3d3112011-11-28 12:36:11 -0800968 audio ? "audio" : "video", formatChange, timeChange);
Andreas Huber32f3cef2011-03-02 15:34:46 -0800969
Andreas Huber3fe62152011-09-16 15:09:22 -0700970 if (audio) {
971 mSkipRenderingAudioUntilMediaTimeUs = -1;
972 } else {
973 mSkipRenderingVideoUntilMediaTimeUs = -1;
974 }
Andreas Huber32f3cef2011-03-02 15:34:46 -0800975
Andreas Huber6e3d3112011-11-28 12:36:11 -0800976 if (timeChange) {
977 sp<AMessage> extra;
978 if (accessUnit->meta()->findMessage("extra", &extra)
979 && extra != NULL) {
980 int64_t resumeAtMediaTimeUs;
981 if (extra->findInt64(
982 "resume-at-mediatimeUs", &resumeAtMediaTimeUs)) {
Steve Blockdf64d152012-01-04 20:05:49 +0000983 ALOGI("suppressing rendering of %s until %lld us",
Andreas Huber6e3d3112011-11-28 12:36:11 -0800984 audio ? "audio" : "video", resumeAtMediaTimeUs);
Andreas Huber3fe62152011-09-16 15:09:22 -0700985
Andreas Huber6e3d3112011-11-28 12:36:11 -0800986 if (audio) {
987 mSkipRenderingAudioUntilMediaTimeUs =
988 resumeAtMediaTimeUs;
989 } else {
990 mSkipRenderingVideoUntilMediaTimeUs =
991 resumeAtMediaTimeUs;
992 }
Andreas Huber3fe62152011-09-16 15:09:22 -0700993 }
Andreas Huber32f3cef2011-03-02 15:34:46 -0800994 }
995 }
Andreas Huber3fe62152011-09-16 15:09:22 -0700996
Andreas Huber6e3d3112011-11-28 12:36:11 -0800997 mTimeDiscontinuityPending =
998 mTimeDiscontinuityPending || timeChange;
999
1000 if (formatChange || timeChange) {
Andreas Hubera1f8ab02012-11-30 10:53:22 -08001001 if (mFlushingAudio == NONE && mFlushingVideo == NONE) {
1002 // And we'll resume scanning sources once we're done
1003 // flushing.
1004 mDeferredActions.push_front(
1005 new SimpleAction(
1006 &NuPlayer::performScanSources));
1007 }
1008
Robert Shih6d0a94e2014-01-23 16:18:22 -08001009 sp<AMessage> newFormat = mSource->getFormat(audio);
1010 sp<Decoder> &decoder = audio ? mAudioDecoder : mVideoDecoder;
1011 if (formatChange && !decoder->supportsSeamlessFormatChange(newFormat)) {
1012 flushDecoder(audio, /* needShutdown = */ true);
1013 } else {
1014 flushDecoder(audio, /* needShutdown = */ false);
1015 err = OK;
1016 }
Andreas Huber6e3d3112011-11-28 12:36:11 -08001017 } else {
1018 // This stream is unaffected by the discontinuity
1019
1020 if (audio) {
1021 mFlushingAudio = FLUSHED;
1022 } else {
1023 mFlushingVideo = FLUSHED;
1024 }
1025
1026 finishFlushIfPossible();
1027
1028 return -EWOULDBLOCK;
1029 }
Andreas Huber32f3cef2011-03-02 15:34:46 -08001030 }
1031
Andreas Huber3fe62152011-09-16 15:09:22 -07001032 reply->setInt32("err", err);
1033 reply->post();
1034 return OK;
Andreas Huberf9334412010-12-15 15:17:42 -08001035 }
1036
Andreas Huber3fe62152011-09-16 15:09:22 -07001037 if (!audio) {
1038 ++mNumFramesTotal;
1039 }
1040
1041 dropAccessUnit = false;
1042 if (!audio
1043 && mVideoLateByUs > 100000ll
1044 && mVideoIsAVC
1045 && !IsAVCReferenceFrame(accessUnit)) {
1046 dropAccessUnit = true;
1047 ++mNumFramesDropped;
1048 }
1049 } while (dropAccessUnit);
Andreas Huberf9334412010-12-15 15:17:42 -08001050
Steve Block3856b092011-10-20 11:56:00 +01001051 // ALOGV("returned a valid buffer of %s data", audio ? "audio" : "video");
Andreas Huberf9334412010-12-15 15:17:42 -08001052
1053#if 0
1054 int64_t mediaTimeUs;
1055 CHECK(accessUnit->meta()->findInt64("timeUs", &mediaTimeUs));
Steve Block3856b092011-10-20 11:56:00 +01001056 ALOGV("feeding %s input buffer at media time %.2f secs",
Andreas Huberf9334412010-12-15 15:17:42 -08001057 audio ? "audio" : "video",
1058 mediaTimeUs / 1E6);
1059#endif
1060
Andreas Huber2d8bedd2012-02-21 14:38:23 -08001061 reply->setBuffer("buffer", accessUnit);
Andreas Huberf9334412010-12-15 15:17:42 -08001062 reply->post();
1063
1064 return OK;
1065}
1066
1067void NuPlayer::renderBuffer(bool audio, const sp<AMessage> &msg) {
Steve Block3856b092011-10-20 11:56:00 +01001068 // ALOGV("renderBuffer %s", audio ? "audio" : "video");
Andreas Huberf9334412010-12-15 15:17:42 -08001069
1070 sp<AMessage> reply;
1071 CHECK(msg->findMessage("reply", &reply));
1072
Andreas Huber18ac5402011-08-31 15:04:25 -07001073 if (IsFlushingState(audio ? mFlushingAudio : mFlushingVideo)) {
1074 // We're currently attempting to flush the decoder, in order
1075 // to complete this, the decoder wants all its buffers back,
1076 // so we don't want any output buffers it sent us (from before
1077 // we initiated the flush) to be stuck in the renderer's queue.
1078
Steve Block3856b092011-10-20 11:56:00 +01001079 ALOGV("we're still flushing the %s decoder, sending its output buffer"
Andreas Huber18ac5402011-08-31 15:04:25 -07001080 " right back.", audio ? "audio" : "video");
1081
1082 reply->post();
1083 return;
1084 }
1085
Andreas Huber2d8bedd2012-02-21 14:38:23 -08001086 sp<ABuffer> buffer;
1087 CHECK(msg->findBuffer("buffer", &buffer));
Andreas Huberf9334412010-12-15 15:17:42 -08001088
Andreas Huber32f3cef2011-03-02 15:34:46 -08001089 int64_t &skipUntilMediaTimeUs =
1090 audio
1091 ? mSkipRenderingAudioUntilMediaTimeUs
1092 : mSkipRenderingVideoUntilMediaTimeUs;
1093
1094 if (skipUntilMediaTimeUs >= 0) {
1095 int64_t mediaTimeUs;
1096 CHECK(buffer->meta()->findInt64("timeUs", &mediaTimeUs));
1097
1098 if (mediaTimeUs < skipUntilMediaTimeUs) {
Steve Block3856b092011-10-20 11:56:00 +01001099 ALOGV("dropping %s buffer at time %lld as requested.",
Andreas Huber32f3cef2011-03-02 15:34:46 -08001100 audio ? "audio" : "video",
1101 mediaTimeUs);
1102
1103 reply->post();
1104 return;
1105 }
1106
1107 skipUntilMediaTimeUs = -1;
1108 }
1109
Andreas Huberf9334412010-12-15 15:17:42 -08001110 mRenderer->queueBuffer(audio, buffer, reply);
1111}
1112
Chong Zhangdcb89b32013-08-06 09:44:47 -07001113void NuPlayer::notifyListener(int msg, int ext1, int ext2, const Parcel *in) {
Andreas Huber43c3e6c2011-01-05 12:17:08 -08001114 if (mDriver == NULL) {
Andreas Huberf9334412010-12-15 15:17:42 -08001115 return;
1116 }
1117
Andreas Huber43c3e6c2011-01-05 12:17:08 -08001118 sp<NuPlayerDriver> driver = mDriver.promote();
Andreas Huberf9334412010-12-15 15:17:42 -08001119
Andreas Huber43c3e6c2011-01-05 12:17:08 -08001120 if (driver == NULL) {
Andreas Huberf9334412010-12-15 15:17:42 -08001121 return;
1122 }
1123
Chong Zhangdcb89b32013-08-06 09:44:47 -07001124 driver->notifyListener(msg, ext1, ext2, in);
Andreas Huberf9334412010-12-15 15:17:42 -08001125}
1126
Andreas Huber1aef2112011-01-04 14:01:29 -08001127void NuPlayer::flushDecoder(bool audio, bool needShutdown) {
Andreas Huber14f76722013-01-15 09:04:18 -08001128 ALOGV("[%s] flushDecoder needShutdown=%d",
1129 audio ? "audio" : "video", needShutdown);
1130
Andreas Huber6e3d3112011-11-28 12:36:11 -08001131 if ((audio && mAudioDecoder == NULL) || (!audio && mVideoDecoder == NULL)) {
Steve Blockdf64d152012-01-04 20:05:49 +00001132 ALOGI("flushDecoder %s without decoder present",
Andreas Huber6e3d3112011-11-28 12:36:11 -08001133 audio ? "audio" : "video");
1134 }
1135
Andreas Huber1aef2112011-01-04 14:01:29 -08001136 // Make sure we don't continue to scan sources until we finish flushing.
1137 ++mScanSourcesGeneration;
Andreas Huber43c3e6c2011-01-05 12:17:08 -08001138 mScanSourcesPending = false;
Andreas Huber1aef2112011-01-04 14:01:29 -08001139
1140 (audio ? mAudioDecoder : mVideoDecoder)->signalFlush();
1141 mRenderer->flush(audio);
1142
1143 FlushStatus newStatus =
1144 needShutdown ? FLUSHING_DECODER_SHUTDOWN : FLUSHING_DECODER;
1145
1146 if (audio) {
1147 CHECK(mFlushingAudio == NONE
1148 || mFlushingAudio == AWAITING_DISCONTINUITY);
1149
1150 mFlushingAudio = newStatus;
1151
1152 if (mFlushingVideo == NONE) {
1153 mFlushingVideo = (mVideoDecoder != NULL)
1154 ? AWAITING_DISCONTINUITY
1155 : FLUSHED;
1156 }
1157 } else {
1158 CHECK(mFlushingVideo == NONE
1159 || mFlushingVideo == AWAITING_DISCONTINUITY);
1160
1161 mFlushingVideo = newStatus;
1162
1163 if (mFlushingAudio == NONE) {
1164 mFlushingAudio = (mAudioDecoder != NULL)
1165 ? AWAITING_DISCONTINUITY
1166 : FLUSHED;
1167 }
1168 }
1169}
1170
Andreas Huber84066782011-08-16 09:34:26 -07001171sp<AMessage> NuPlayer::Source::getFormat(bool audio) {
1172 sp<MetaData> meta = getFormatMeta(audio);
1173
1174 if (meta == NULL) {
1175 return NULL;
1176 }
1177
1178 sp<AMessage> msg = new AMessage;
1179
1180 if(convertMetaDataToMessage(meta, &msg) == OK) {
1181 return msg;
1182 }
1183 return NULL;
1184}
1185
James Dong0d268a32012-08-31 12:18:27 -07001186status_t NuPlayer::setVideoScalingMode(int32_t mode) {
1187 mVideoScalingMode = mode;
Andreas Huber57a339c2012-12-03 11:18:00 -08001188 if (mNativeWindow != NULL) {
James Dong0d268a32012-08-31 12:18:27 -07001189 status_t ret = native_window_set_scaling_mode(
1190 mNativeWindow->getNativeWindow().get(), mVideoScalingMode);
1191 if (ret != OK) {
1192 ALOGE("Failed to set scaling mode (%d): %s",
1193 -ret, strerror(-ret));
1194 return ret;
1195 }
1196 }
1197 return OK;
1198}
1199
Chong Zhangdcb89b32013-08-06 09:44:47 -07001200status_t NuPlayer::getTrackInfo(Parcel* reply) const {
1201 sp<AMessage> msg = new AMessage(kWhatGetTrackInfo, id());
1202 msg->setPointer("reply", reply);
1203
1204 sp<AMessage> response;
1205 status_t err = msg->postAndAwaitResponse(&response);
1206 return err;
1207}
1208
1209status_t NuPlayer::selectTrack(size_t trackIndex, bool select) {
1210 sp<AMessage> msg = new AMessage(kWhatSelectTrack, id());
1211 msg->setSize("trackIndex", trackIndex);
1212 msg->setInt32("select", select);
1213
1214 sp<AMessage> response;
1215 status_t err = msg->postAndAwaitResponse(&response);
1216
1217 return err;
1218}
1219
Andreas Huberb7c8e912012-11-27 15:02:53 -08001220void NuPlayer::schedulePollDuration() {
1221 sp<AMessage> msg = new AMessage(kWhatPollDuration, id());
1222 msg->setInt32("generation", mPollDurationGeneration);
1223 msg->post();
1224}
1225
1226void NuPlayer::cancelPollDuration() {
1227 ++mPollDurationGeneration;
1228}
1229
Andreas Hubera1f8ab02012-11-30 10:53:22 -08001230void NuPlayer::processDeferredActions() {
1231 while (!mDeferredActions.empty()) {
1232 // We won't execute any deferred actions until we're no longer in
1233 // an intermediate state, i.e. one more more decoders are currently
1234 // flushing or shutting down.
1235
1236 if (mRenderer != NULL) {
1237 // There's an edge case where the renderer owns all output
1238 // buffers and is paused, therefore the decoder will not read
1239 // more input data and will never encounter the matching
1240 // discontinuity. To avoid this, we resume the renderer.
1241
1242 if (mFlushingAudio == AWAITING_DISCONTINUITY
1243 || mFlushingVideo == AWAITING_DISCONTINUITY) {
1244 mRenderer->resume();
1245 }
1246 }
1247
1248 if (mFlushingAudio != NONE || mFlushingVideo != NONE) {
1249 // We're currently flushing, postpone the reset until that's
1250 // completed.
1251
1252 ALOGV("postponing action mFlushingAudio=%d, mFlushingVideo=%d",
1253 mFlushingAudio, mFlushingVideo);
1254
1255 break;
1256 }
1257
1258 sp<Action> action = *mDeferredActions.begin();
1259 mDeferredActions.erase(mDeferredActions.begin());
1260
1261 action->execute(this);
1262 }
1263}
1264
1265void NuPlayer::performSeek(int64_t seekTimeUs) {
1266 ALOGV("performSeek seekTimeUs=%lld us (%.2f secs)",
1267 seekTimeUs,
1268 seekTimeUs / 1E6);
1269
1270 mSource->seekTo(seekTimeUs);
1271
1272 if (mDriver != NULL) {
1273 sp<NuPlayerDriver> driver = mDriver.promote();
1274 if (driver != NULL) {
1275 driver->notifyPosition(seekTimeUs);
1276 driver->notifySeekComplete();
1277 }
1278 }
1279
1280 // everything's flushed, continue playback.
1281}
1282
1283void NuPlayer::performDecoderFlush() {
1284 ALOGV("performDecoderFlush");
1285
Andreas Huberda9740e2013-04-16 10:54:03 -07001286 if (mAudioDecoder == NULL && mVideoDecoder == NULL) {
Andreas Hubera1f8ab02012-11-30 10:53:22 -08001287 return;
1288 }
1289
1290 mTimeDiscontinuityPending = true;
1291
1292 if (mAudioDecoder != NULL) {
1293 flushDecoder(true /* audio */, false /* needShutdown */);
1294 }
1295
1296 if (mVideoDecoder != NULL) {
1297 flushDecoder(false /* audio */, false /* needShutdown */);
1298 }
1299}
1300
Andreas Huber14f76722013-01-15 09:04:18 -08001301void NuPlayer::performDecoderShutdown(bool audio, bool video) {
1302 ALOGV("performDecoderShutdown audio=%d, video=%d", audio, video);
Andreas Hubera1f8ab02012-11-30 10:53:22 -08001303
Andreas Huber14f76722013-01-15 09:04:18 -08001304 if ((!audio || mAudioDecoder == NULL)
1305 && (!video || mVideoDecoder == NULL)) {
Andreas Hubera1f8ab02012-11-30 10:53:22 -08001306 return;
1307 }
1308
1309 mTimeDiscontinuityPending = true;
1310
Andreas Huber14f76722013-01-15 09:04:18 -08001311 if (mFlushingAudio == NONE && (!audio || mAudioDecoder == NULL)) {
1312 mFlushingAudio = FLUSHED;
1313 }
1314
1315 if (mFlushingVideo == NONE && (!video || mVideoDecoder == NULL)) {
1316 mFlushingVideo = FLUSHED;
1317 }
1318
1319 if (audio && mAudioDecoder != NULL) {
Andreas Hubera1f8ab02012-11-30 10:53:22 -08001320 flushDecoder(true /* audio */, true /* needShutdown */);
1321 }
1322
Andreas Huber14f76722013-01-15 09:04:18 -08001323 if (video && mVideoDecoder != NULL) {
Andreas Hubera1f8ab02012-11-30 10:53:22 -08001324 flushDecoder(false /* audio */, true /* needShutdown */);
1325 }
1326}
1327
1328void NuPlayer::performReset() {
1329 ALOGV("performReset");
1330
1331 CHECK(mAudioDecoder == NULL);
1332 CHECK(mVideoDecoder == NULL);
1333
1334 cancelPollDuration();
1335
1336 ++mScanSourcesGeneration;
1337 mScanSourcesPending = false;
1338
1339 mRenderer.clear();
1340
1341 if (mSource != NULL) {
1342 mSource->stop();
Andreas Huberb5f25f02013-02-05 10:14:26 -08001343
1344 looper()->unregisterHandler(mSource->id());
1345
Andreas Hubera1f8ab02012-11-30 10:53:22 -08001346 mSource.clear();
1347 }
1348
1349 if (mDriver != NULL) {
1350 sp<NuPlayerDriver> driver = mDriver.promote();
1351 if (driver != NULL) {
1352 driver->notifyResetComplete();
1353 }
1354 }
Andreas Huber57a339c2012-12-03 11:18:00 -08001355
1356 mStarted = false;
Andreas Hubera1f8ab02012-11-30 10:53:22 -08001357}
1358
1359void NuPlayer::performScanSources() {
1360 ALOGV("performScanSources");
1361
Andreas Huber57a339c2012-12-03 11:18:00 -08001362 if (!mStarted) {
1363 return;
1364 }
1365
Andreas Hubera1f8ab02012-11-30 10:53:22 -08001366 if (mAudioDecoder == NULL || mVideoDecoder == NULL) {
1367 postScanSources();
1368 }
1369}
1370
Andreas Huber57a339c2012-12-03 11:18:00 -08001371void NuPlayer::performSetSurface(const sp<NativeWindowWrapper> &wrapper) {
1372 ALOGV("performSetSurface");
1373
1374 mNativeWindow = wrapper;
1375
1376 // XXX - ignore error from setVideoScalingMode for now
1377 setVideoScalingMode(mVideoScalingMode);
1378
1379 if (mDriver != NULL) {
1380 sp<NuPlayerDriver> driver = mDriver.promote();
1381 if (driver != NULL) {
1382 driver->notifySetSurfaceComplete();
1383 }
1384 }
1385}
1386
Andreas Huber9575c962013-02-05 13:59:56 -08001387void NuPlayer::onSourceNotify(const sp<AMessage> &msg) {
1388 int32_t what;
1389 CHECK(msg->findInt32("what", &what));
1390
1391 switch (what) {
1392 case Source::kWhatPrepared:
1393 {
Andreas Huberb5f28d42013-04-25 15:11:19 -07001394 if (mSource == NULL) {
1395 // This is a stale notification from a source that was
1396 // asynchronously preparing when the client called reset().
1397 // We handled the reset, the source is gone.
1398 break;
1399 }
1400
Andreas Huberec0c5972013-02-05 14:47:13 -08001401 int32_t err;
1402 CHECK(msg->findInt32("err", &err));
1403
Andreas Huber9575c962013-02-05 13:59:56 -08001404 sp<NuPlayerDriver> driver = mDriver.promote();
1405 if (driver != NULL) {
Andreas Huberec0c5972013-02-05 14:47:13 -08001406 driver->notifyPrepareCompleted(err);
Andreas Huber9575c962013-02-05 13:59:56 -08001407 }
Andreas Huber99759402013-04-01 14:28:31 -07001408
1409 int64_t durationUs;
1410 if (mDriver != NULL && mSource->getDuration(&durationUs) == OK) {
1411 sp<NuPlayerDriver> driver = mDriver.promote();
1412 if (driver != NULL) {
1413 driver->notifyDuration(durationUs);
1414 }
1415 }
Andreas Huber9575c962013-02-05 13:59:56 -08001416 break;
1417 }
1418
1419 case Source::kWhatFlagsChanged:
1420 {
1421 uint32_t flags;
1422 CHECK(msg->findInt32("flags", (int32_t *)&flags));
1423
Chong Zhang4b7069d2013-09-11 12:52:43 -07001424 sp<NuPlayerDriver> driver = mDriver.promote();
1425 if (driver != NULL) {
1426 driver->notifyFlagsChanged(flags);
1427 }
1428
Andreas Huber9575c962013-02-05 13:59:56 -08001429 if ((mSourceFlags & Source::FLAG_DYNAMIC_DURATION)
1430 && (!(flags & Source::FLAG_DYNAMIC_DURATION))) {
1431 cancelPollDuration();
1432 } else if (!(mSourceFlags & Source::FLAG_DYNAMIC_DURATION)
1433 && (flags & Source::FLAG_DYNAMIC_DURATION)
1434 && (mAudioDecoder != NULL || mVideoDecoder != NULL)) {
1435 schedulePollDuration();
1436 }
1437
1438 mSourceFlags = flags;
1439 break;
1440 }
1441
1442 case Source::kWhatVideoSizeChanged:
1443 {
1444 int32_t width, height;
1445 CHECK(msg->findInt32("width", &width));
1446 CHECK(msg->findInt32("height", &height));
1447
1448 notifyListener(MEDIA_SET_VIDEO_SIZE, width, height);
1449 break;
1450 }
1451
Roger Jönssonb50e83e2013-01-21 16:26:41 +01001452 case Source::kWhatBufferingStart:
1453 {
1454 notifyListener(MEDIA_INFO, MEDIA_INFO_BUFFERING_START, 0);
1455 break;
1456 }
1457
1458 case Source::kWhatBufferingEnd:
1459 {
1460 notifyListener(MEDIA_INFO, MEDIA_INFO_BUFFERING_END, 0);
1461 break;
1462 }
1463
Chong Zhangdcb89b32013-08-06 09:44:47 -07001464 case Source::kWhatSubtitleData:
1465 {
1466 sp<ABuffer> buffer;
1467 CHECK(msg->findBuffer("buffer", &buffer));
1468
1469 int32_t trackIndex;
1470 int64_t timeUs, durationUs;
1471 CHECK(buffer->meta()->findInt32("trackIndex", &trackIndex));
1472 CHECK(buffer->meta()->findInt64("timeUs", &timeUs));
1473 CHECK(buffer->meta()->findInt64("durationUs", &durationUs));
1474
1475 Parcel in;
1476 in.writeInt32(trackIndex);
1477 in.writeInt64(timeUs);
1478 in.writeInt64(durationUs);
1479 in.writeInt32(buffer->size());
1480 in.writeInt32(buffer->size());
1481 in.write(buffer->data(), buffer->size());
1482
1483 notifyListener(MEDIA_SUBTITLE_DATA, 0, 0, &in);
1484 break;
1485 }
1486
Andreas Huber14f76722013-01-15 09:04:18 -08001487 case Source::kWhatQueueDecoderShutdown:
1488 {
1489 int32_t audio, video;
1490 CHECK(msg->findInt32("audio", &audio));
1491 CHECK(msg->findInt32("video", &video));
1492
1493 sp<AMessage> reply;
1494 CHECK(msg->findMessage("reply", &reply));
1495
1496 queueDecoderShutdown(audio, video, reply);
1497 break;
1498 }
1499
Andreas Huber9575c962013-02-05 13:59:56 -08001500 default:
1501 TRESPASS();
1502 }
1503}
1504
Andreas Huberb5f25f02013-02-05 10:14:26 -08001505////////////////////////////////////////////////////////////////////////////////
1506
Andreas Huber9575c962013-02-05 13:59:56 -08001507void NuPlayer::Source::notifyFlagsChanged(uint32_t flags) {
1508 sp<AMessage> notify = dupNotify();
1509 notify->setInt32("what", kWhatFlagsChanged);
1510 notify->setInt32("flags", flags);
1511 notify->post();
1512}
1513
1514void NuPlayer::Source::notifyVideoSizeChanged(int32_t width, int32_t height) {
1515 sp<AMessage> notify = dupNotify();
1516 notify->setInt32("what", kWhatVideoSizeChanged);
1517 notify->setInt32("width", width);
1518 notify->setInt32("height", height);
1519 notify->post();
1520}
1521
Andreas Huberec0c5972013-02-05 14:47:13 -08001522void NuPlayer::Source::notifyPrepared(status_t err) {
Andreas Huber9575c962013-02-05 13:59:56 -08001523 sp<AMessage> notify = dupNotify();
1524 notify->setInt32("what", kWhatPrepared);
Andreas Huberec0c5972013-02-05 14:47:13 -08001525 notify->setInt32("err", err);
Andreas Huber9575c962013-02-05 13:59:56 -08001526 notify->post();
1527}
1528
Andreas Huber84333e02014-02-07 15:36:10 -08001529void NuPlayer::Source::onMessageReceived(const sp<AMessage> & /* msg */) {
Andreas Huberb5f25f02013-02-05 10:14:26 -08001530 TRESPASS();
1531}
1532
Andreas Huber14f76722013-01-15 09:04:18 -08001533void NuPlayer::queueDecoderShutdown(
1534 bool audio, bool video, const sp<AMessage> &reply) {
1535 ALOGI("queueDecoderShutdown audio=%d, video=%d", audio, video);
1536
1537 mDeferredActions.push_back(
1538 new ShutdownDecoderAction(audio, video));
1539
1540 mDeferredActions.push_back(
1541 new SimpleAction(&NuPlayer::performScanSources));
1542
1543 mDeferredActions.push_back(new PostMessageAction(reply));
1544
1545 processDeferredActions();
1546}
1547
Andreas Huberf9334412010-12-15 15:17:42 -08001548} // namespace android