blob: e1735fa69fce26b3e88c84d869b2c4d406aba5fc [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 Huber84066782011-08-16 09:34:26 -070031#include "mp4/MP4Source.h"
Andreas Huber5bc087c2010-12-23 10:27:40 -080032
33#include "ATSParser.h"
Andreas Huberf9334412010-12-15 15:17:42 -080034
Andreas Huber84066782011-08-16 09:34:26 -070035#include <cutils/properties.h> // for property_get
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),
Andreas Huber9b80c2b2011-06-30 15:47:02 -0700149 mAudioEOS(false),
Andreas Huberf9334412010-12-15 15:17:42 -0800150 mVideoEOS(false),
Andreas Huber5bc087c2010-12-23 10:27:40 -0800151 mScanSourcesPending(false),
Andreas Huber1aef2112011-01-04 14:01:29 -0800152 mScanSourcesGeneration(0),
Andreas Huberb7c8e912012-11-27 15:02:53 -0800153 mPollDurationGeneration(0),
Andreas Huber6e3d3112011-11-28 12:36:11 -0800154 mTimeDiscontinuityPending(false),
Andreas Huberf9334412010-12-15 15:17:42 -0800155 mFlushingAudio(NONE),
Andreas Huber1aef2112011-01-04 14:01:29 -0800156 mFlushingVideo(NONE),
Andreas Huber3fe62152011-09-16 15:09:22 -0700157 mSkipRenderingAudioUntilMediaTimeUs(-1ll),
158 mSkipRenderingVideoUntilMediaTimeUs(-1ll),
159 mVideoLateByUs(0ll),
160 mNumFramesTotal(0ll),
James Dong0d268a32012-08-31 12:18:27 -0700161 mNumFramesDropped(0ll),
Andreas Huber57a339c2012-12-03 11:18:00 -0800162 mVideoScalingMode(NATIVE_WINDOW_SCALING_MODE_SCALE_TO_WINDOW),
163 mStarted(false) {
Andreas Huberf9334412010-12-15 15:17:42 -0800164}
165
166NuPlayer::~NuPlayer() {
167}
168
Andreas Huber9b80c2b2011-06-30 15:47:02 -0700169void NuPlayer::setUID(uid_t uid) {
170 mUIDValid = true;
171 mUID = uid;
172}
173
Andreas Huber43c3e6c2011-01-05 12:17:08 -0800174void NuPlayer::setDriver(const wp<NuPlayerDriver> &driver) {
175 mDriver = driver;
Andreas Huberf9334412010-12-15 15:17:42 -0800176}
177
Andreas Huber9575c962013-02-05 13:59:56 -0800178void NuPlayer::setDataSourceAsync(const sp<IStreamSource> &source) {
Andreas Huberf9334412010-12-15 15:17:42 -0800179 sp<AMessage> msg = new AMessage(kWhatSetDataSource, id());
180
Andreas Huberb5f25f02013-02-05 10:14:26 -0800181 sp<AMessage> notify = new AMessage(kWhatSourceNotify, id());
182
Andreas Huber84066782011-08-16 09:34:26 -0700183 char prop[PROPERTY_VALUE_MAX];
184 if (property_get("media.stagefright.use-mp4source", prop, NULL)
185 && (!strcmp(prop, "1") || !strcasecmp(prop, "true"))) {
Andreas Huberb5f25f02013-02-05 10:14:26 -0800186 msg->setObject("source", new MP4Source(notify, source));
Andreas Huber84066782011-08-16 09:34:26 -0700187 } else {
Andreas Huberb5f25f02013-02-05 10:14:26 -0800188 msg->setObject("source", new StreamingSource(notify, source));
Andreas Huber84066782011-08-16 09:34:26 -0700189 }
190
Andreas Huber5bc087c2010-12-23 10:27:40 -0800191 msg->post();
192}
Andreas Huberf9334412010-12-15 15:17:42 -0800193
Andreas Huberafed0e12011-09-20 15:39:58 -0700194static bool IsHTTPLiveURL(const char *url) {
195 if (!strncasecmp("http://", url, 7)
Andreas Huber99759402013-04-01 14:28:31 -0700196 || !strncasecmp("https://", url, 8)
197 || !strncasecmp("file://", url, 7)) {
Andreas Huberafed0e12011-09-20 15:39:58 -0700198 size_t len = strlen(url);
199 if (len >= 5 && !strcasecmp(".m3u8", &url[len - 5])) {
200 return true;
201 }
202
203 if (strstr(url,"m3u8")) {
204 return true;
205 }
206 }
207
208 return false;
209}
210
Andreas Huber9575c962013-02-05 13:59:56 -0800211void NuPlayer::setDataSourceAsync(
Andreas Huber5bc087c2010-12-23 10:27:40 -0800212 const char *url, const KeyedVector<String8, String8> *headers) {
213 sp<AMessage> msg = new AMessage(kWhatSetDataSource, id());
Oscar Rydhé7a33b772012-02-20 10:15:48 +0100214 size_t len = strlen(url);
Andreas Huber5bc087c2010-12-23 10:27:40 -0800215
Andreas Huberb5f25f02013-02-05 10:14:26 -0800216 sp<AMessage> notify = new AMessage(kWhatSourceNotify, id());
217
Andreas Huberafed0e12011-09-20 15:39:58 -0700218 sp<Source> source;
219 if (IsHTTPLiveURL(url)) {
Andreas Huberb5f25f02013-02-05 10:14:26 -0800220 source = new HTTPLiveSource(notify, url, headers, mUIDValid, mUID);
Andreas Huberafed0e12011-09-20 15:39:58 -0700221 } else if (!strncasecmp(url, "rtsp://", 7)) {
Andreas Huberb5f25f02013-02-05 10:14:26 -0800222 source = new RTSPSource(notify, url, headers, mUIDValid, mUID);
Oscar Rydhé7a33b772012-02-20 10:15:48 +0100223 } else if ((!strncasecmp(url, "http://", 7)
224 || !strncasecmp(url, "https://", 8))
225 && ((len >= 4 && !strcasecmp(".sdp", &url[len - 4]))
226 || strstr(url, ".sdp?"))) {
227 source = new RTSPSource(notify, url, headers, mUIDValid, mUID, true);
Andreas Huber2bfdd422011-10-11 15:24:07 -0700228 } else {
Andreas Huberb5f25f02013-02-05 10:14:26 -0800229 source = new GenericSource(notify, url, headers, mUIDValid, mUID);
Andreas Huber2bfdd422011-10-11 15:24:07 -0700230 }
231
Andreas Huberafed0e12011-09-20 15:39:58 -0700232 msg->setObject("source", source);
233 msg->post();
234}
235
Andreas Huber9575c962013-02-05 13:59:56 -0800236void NuPlayer::setDataSourceAsync(int fd, int64_t offset, int64_t length) {
Andreas Huberafed0e12011-09-20 15:39:58 -0700237 sp<AMessage> msg = new AMessage(kWhatSetDataSource, id());
238
Andreas Huberb5f25f02013-02-05 10:14:26 -0800239 sp<AMessage> notify = new AMessage(kWhatSourceNotify, id());
240
241 sp<Source> source = new GenericSource(notify, fd, offset, length);
Andreas Huberafed0e12011-09-20 15:39:58 -0700242 msg->setObject("source", source);
Andreas Huberf9334412010-12-15 15:17:42 -0800243 msg->post();
244}
245
Andreas Huber9575c962013-02-05 13:59:56 -0800246void NuPlayer::prepareAsync() {
247 (new AMessage(kWhatPrepare, id()))->post();
248}
249
Andreas Huber57a339c2012-12-03 11:18:00 -0800250void NuPlayer::setVideoSurfaceTextureAsync(
Andy McFadden8ba01022012-12-18 09:46:54 -0800251 const sp<IGraphicBufferProducer> &bufferProducer) {
Glenn Kasten11731182011-02-08 17:26:17 -0800252 sp<AMessage> msg = new AMessage(kWhatSetVideoNativeWindow, id());
Andreas Huber57a339c2012-12-03 11:18:00 -0800253
Andy McFadden8ba01022012-12-18 09:46:54 -0800254 if (bufferProducer == NULL) {
Andreas Huber57a339c2012-12-03 11:18:00 -0800255 msg->setObject("native-window", NULL);
256 } else {
257 msg->setObject(
258 "native-window",
259 new NativeWindowWrapper(
Mathias Agopian1a2952a2013-02-14 17:11:27 -0800260 new Surface(bufferProducer)));
Andreas Huber57a339c2012-12-03 11:18:00 -0800261 }
262
Andreas Huberf9334412010-12-15 15:17:42 -0800263 msg->post();
264}
265
266void NuPlayer::setAudioSink(const sp<MediaPlayerBase::AudioSink> &sink) {
267 sp<AMessage> msg = new AMessage(kWhatSetAudioSink, id());
268 msg->setObject("sink", sink);
269 msg->post();
270}
271
272void NuPlayer::start() {
273 (new AMessage(kWhatStart, id()))->post();
274}
275
Andreas Huber43c3e6c2011-01-05 12:17:08 -0800276void NuPlayer::pause() {
Andreas Huberb4082222011-01-20 15:23:04 -0800277 (new AMessage(kWhatPause, id()))->post();
Andreas Huber43c3e6c2011-01-05 12:17:08 -0800278}
279
280void NuPlayer::resume() {
Andreas Huberb4082222011-01-20 15:23:04 -0800281 (new AMessage(kWhatResume, id()))->post();
Andreas Huber43c3e6c2011-01-05 12:17:08 -0800282}
283
Andreas Huber1aef2112011-01-04 14:01:29 -0800284void NuPlayer::resetAsync() {
285 (new AMessage(kWhatReset, id()))->post();
286}
287
Andreas Huber43c3e6c2011-01-05 12:17:08 -0800288void NuPlayer::seekToAsync(int64_t seekTimeUs) {
289 sp<AMessage> msg = new AMessage(kWhatSeek, id());
290 msg->setInt64("seekTimeUs", seekTimeUs);
291 msg->post();
292}
293
Andreas Huber53df1a42010-12-22 10:03:04 -0800294// static
Andreas Huber1aef2112011-01-04 14:01:29 -0800295bool NuPlayer::IsFlushingState(FlushStatus state, bool *needShutdown) {
Andreas Huber53df1a42010-12-22 10:03:04 -0800296 switch (state) {
297 case FLUSHING_DECODER:
Andreas Huber1aef2112011-01-04 14:01:29 -0800298 if (needShutdown != NULL) {
299 *needShutdown = false;
Andreas Huber53df1a42010-12-22 10:03:04 -0800300 }
301 return true;
302
Andreas Huber1aef2112011-01-04 14:01:29 -0800303 case FLUSHING_DECODER_SHUTDOWN:
304 if (needShutdown != NULL) {
305 *needShutdown = true;
Andreas Huber53df1a42010-12-22 10:03:04 -0800306 }
307 return true;
308
309 default:
310 return false;
311 }
312}
313
Andreas Huberf9334412010-12-15 15:17:42 -0800314void NuPlayer::onMessageReceived(const sp<AMessage> &msg) {
315 switch (msg->what()) {
316 case kWhatSetDataSource:
317 {
Steve Block3856b092011-10-20 11:56:00 +0100318 ALOGV("kWhatSetDataSource");
Andreas Huberf9334412010-12-15 15:17:42 -0800319
320 CHECK(mSource == NULL);
321
Andreas Huber5bc087c2010-12-23 10:27:40 -0800322 sp<RefBase> obj;
323 CHECK(msg->findObject("source", &obj));
Andreas Huberf9334412010-12-15 15:17:42 -0800324
Andreas Huber5bc087c2010-12-23 10:27:40 -0800325 mSource = static_cast<Source *>(obj.get());
Andreas Huberb5f25f02013-02-05 10:14:26 -0800326
327 looper()->registerHandler(mSource);
Andreas Huber9575c962013-02-05 13:59:56 -0800328
329 CHECK(mDriver != NULL);
330 sp<NuPlayerDriver> driver = mDriver.promote();
331 if (driver != NULL) {
332 driver->notifySetDataSourceCompleted(OK);
333 }
334 break;
335 }
336
337 case kWhatPrepare:
338 {
339 mSource->prepareAsync();
Andreas Huberf9334412010-12-15 15:17:42 -0800340 break;
341 }
342
Chong Zhangdcb89b32013-08-06 09:44:47 -0700343 case kWhatGetTrackInfo:
344 {
345 uint32_t replyID;
346 CHECK(msg->senderAwaitsResponse(&replyID));
347
348 status_t err = INVALID_OPERATION;
349 if (mSource != NULL) {
350 Parcel* reply;
351 CHECK(msg->findPointer("reply", (void**)&reply));
352 err = mSource->getTrackInfo(reply);
353 }
354
355 sp<AMessage> response = new AMessage;
356 response->setInt32("err", err);
357
358 response->postReply(replyID);
359 break;
360 }
361
362 case kWhatSelectTrack:
363 {
364 uint32_t replyID;
365 CHECK(msg->senderAwaitsResponse(&replyID));
366
367 status_t err = INVALID_OPERATION;
368 if (mSource != NULL) {
369 size_t trackIndex;
370 int32_t select;
371 CHECK(msg->findSize("trackIndex", &trackIndex));
372 CHECK(msg->findInt32("select", &select));
373 err = mSource->selectTrack(trackIndex, select);
374 }
375
376 sp<AMessage> response = new AMessage;
377 response->setInt32("err", err);
378
379 response->postReply(replyID);
380 break;
381 }
382
Andreas Huberb7c8e912012-11-27 15:02:53 -0800383 case kWhatPollDuration:
384 {
385 int32_t generation;
386 CHECK(msg->findInt32("generation", &generation));
387
388 if (generation != mPollDurationGeneration) {
389 // stale
390 break;
391 }
392
393 int64_t durationUs;
394 if (mDriver != NULL && mSource->getDuration(&durationUs) == OK) {
395 sp<NuPlayerDriver> driver = mDriver.promote();
396 if (driver != NULL) {
397 driver->notifyDuration(durationUs);
398 }
399 }
400
401 msg->post(1000000ll); // poll again in a second.
402 break;
403 }
404
Glenn Kasten11731182011-02-08 17:26:17 -0800405 case kWhatSetVideoNativeWindow:
Andreas Huberf9334412010-12-15 15:17:42 -0800406 {
Steve Block3856b092011-10-20 11:56:00 +0100407 ALOGV("kWhatSetVideoNativeWindow");
Andreas Huberf9334412010-12-15 15:17:42 -0800408
Andreas Huber57a339c2012-12-03 11:18:00 -0800409 mDeferredActions.push_back(
Andreas Huber14f76722013-01-15 09:04:18 -0800410 new ShutdownDecoderAction(
411 false /* audio */, true /* video */));
Andreas Huber57a339c2012-12-03 11:18:00 -0800412
Andreas Huberf9334412010-12-15 15:17:42 -0800413 sp<RefBase> obj;
Glenn Kasten11731182011-02-08 17:26:17 -0800414 CHECK(msg->findObject("native-window", &obj));
Andreas Huberf9334412010-12-15 15:17:42 -0800415
Andreas Huber57a339c2012-12-03 11:18:00 -0800416 mDeferredActions.push_back(
417 new SetSurfaceAction(
418 static_cast<NativeWindowWrapper *>(obj.get())));
James Dong0d268a32012-08-31 12:18:27 -0700419
Andreas Huber57a339c2012-12-03 11:18:00 -0800420 if (obj != NULL) {
421 // If there is a new surface texture, instantiate decoders
422 // again if possible.
423 mDeferredActions.push_back(
424 new SimpleAction(&NuPlayer::performScanSources));
425 }
426
427 processDeferredActions();
Andreas Huberf9334412010-12-15 15:17:42 -0800428 break;
429 }
430
431 case kWhatSetAudioSink:
432 {
Steve Block3856b092011-10-20 11:56:00 +0100433 ALOGV("kWhatSetAudioSink");
Andreas Huberf9334412010-12-15 15:17:42 -0800434
435 sp<RefBase> obj;
436 CHECK(msg->findObject("sink", &obj));
437
438 mAudioSink = static_cast<MediaPlayerBase::AudioSink *>(obj.get());
439 break;
440 }
441
442 case kWhatStart:
443 {
Steve Block3856b092011-10-20 11:56:00 +0100444 ALOGV("kWhatStart");
Andreas Huber43c3e6c2011-01-05 12:17:08 -0800445
Andreas Huber3fe62152011-09-16 15:09:22 -0700446 mVideoIsAVC = false;
Andreas Huber1aef2112011-01-04 14:01:29 -0800447 mAudioEOS = false;
448 mVideoEOS = false;
Andreas Huber32f3cef2011-03-02 15:34:46 -0800449 mSkipRenderingAudioUntilMediaTimeUs = -1;
450 mSkipRenderingVideoUntilMediaTimeUs = -1;
Andreas Huber3fe62152011-09-16 15:09:22 -0700451 mVideoLateByUs = 0;
452 mNumFramesTotal = 0;
453 mNumFramesDropped = 0;
Andreas Huber57a339c2012-12-03 11:18:00 -0800454 mStarted = true;
Andreas Huber1aef2112011-01-04 14:01:29 -0800455
Andreas Huber5bc087c2010-12-23 10:27:40 -0800456 mSource->start();
Andreas Huberf9334412010-12-15 15:17:42 -0800457
Andreas Huberd5e56232013-03-12 11:01:43 -0700458 uint32_t flags = 0;
459
460 if (mSource->isRealTime()) {
461 flags |= Renderer::FLAG_REAL_TIME;
462 }
463
Andreas Huberf9334412010-12-15 15:17:42 -0800464 mRenderer = new Renderer(
465 mAudioSink,
Andreas Huberd5e56232013-03-12 11:01:43 -0700466 new AMessage(kWhatRendererNotify, id()),
467 flags);
Andreas Huberf9334412010-12-15 15:17:42 -0800468
469 looper()->registerHandler(mRenderer);
470
Andreas Huber1aef2112011-01-04 14:01:29 -0800471 postScanSources();
Andreas Huberf9334412010-12-15 15:17:42 -0800472 break;
473 }
474
475 case kWhatScanSources:
476 {
Andreas Huber1aef2112011-01-04 14:01:29 -0800477 int32_t generation;
478 CHECK(msg->findInt32("generation", &generation));
479 if (generation != mScanSourcesGeneration) {
480 // Drop obsolete msg.
481 break;
482 }
483
Andreas Huber5bc087c2010-12-23 10:27:40 -0800484 mScanSourcesPending = false;
485
Steve Block3856b092011-10-20 11:56:00 +0100486 ALOGV("scanning sources haveAudio=%d, haveVideo=%d",
Andreas Huber43c3e6c2011-01-05 12:17:08 -0800487 mAudioDecoder != NULL, mVideoDecoder != NULL);
488
Andreas Huberb7c8e912012-11-27 15:02:53 -0800489 bool mHadAnySourcesBefore =
490 (mAudioDecoder != NULL) || (mVideoDecoder != NULL);
491
Haynes Mathew George5d246ef2012-07-09 10:36:57 -0700492 if (mNativeWindow != NULL) {
493 instantiateDecoder(false, &mVideoDecoder);
494 }
Andreas Huberf9334412010-12-15 15:17:42 -0800495
496 if (mAudioSink != NULL) {
Andreas Huber5bc087c2010-12-23 10:27:40 -0800497 instantiateDecoder(true, &mAudioDecoder);
Andreas Huberf9334412010-12-15 15:17:42 -0800498 }
499
Andreas Huberb7c8e912012-11-27 15:02:53 -0800500 if (!mHadAnySourcesBefore
501 && (mAudioDecoder != NULL || mVideoDecoder != NULL)) {
502 // This is the first time we've found anything playable.
503
Andreas Huber9575c962013-02-05 13:59:56 -0800504 if (mSourceFlags & Source::FLAG_DYNAMIC_DURATION) {
Andreas Huberb7c8e912012-11-27 15:02:53 -0800505 schedulePollDuration();
506 }
507 }
508
Andreas Hubereac68ba2011-09-27 12:12:25 -0700509 status_t err;
510 if ((err = mSource->feedMoreTSData()) != OK) {
Andreas Huber1aef2112011-01-04 14:01:29 -0800511 if (mAudioDecoder == NULL && mVideoDecoder == NULL) {
512 // We're not currently decoding anything (no audio or
513 // video tracks found) and we just ran out of input data.
Andreas Hubereac68ba2011-09-27 12:12:25 -0700514
515 if (err == ERROR_END_OF_STREAM) {
516 notifyListener(MEDIA_PLAYBACK_COMPLETE, 0, 0);
517 } else {
518 notifyListener(MEDIA_ERROR, MEDIA_ERROR_UNKNOWN, err);
519 }
Andreas Huber1aef2112011-01-04 14:01:29 -0800520 }
Andreas Huberf9334412010-12-15 15:17:42 -0800521 break;
522 }
523
Andreas Huberfbe9d812012-08-31 14:05:27 -0700524 if ((mAudioDecoder == NULL && mAudioSink != NULL)
525 || (mVideoDecoder == NULL && mNativeWindow != NULL)) {
Andreas Huberf9334412010-12-15 15:17:42 -0800526 msg->post(100000ll);
Andreas Huber5bc087c2010-12-23 10:27:40 -0800527 mScanSourcesPending = true;
Andreas Huberf9334412010-12-15 15:17:42 -0800528 }
529 break;
530 }
531
532 case kWhatVideoNotify:
533 case kWhatAudioNotify:
534 {
535 bool audio = msg->what() == kWhatAudioNotify;
536
537 sp<AMessage> codecRequest;
538 CHECK(msg->findMessage("codec-request", &codecRequest));
539
540 int32_t what;
541 CHECK(codecRequest->findInt32("what", &what));
542
543 if (what == ACodec::kWhatFillThisBuffer) {
544 status_t err = feedDecoderInputData(
545 audio, codecRequest);
546
Andreas Huber5bc087c2010-12-23 10:27:40 -0800547 if (err == -EWOULDBLOCK) {
Andreas Hubereac68ba2011-09-27 12:12:25 -0700548 if (mSource->feedMoreTSData() == OK) {
Andreas Huber1183a4a2011-11-03 11:00:21 -0700549 msg->post(10000ll);
Andreas Huber5bc087c2010-12-23 10:27:40 -0800550 }
Andreas Huberf9334412010-12-15 15:17:42 -0800551 }
552 } else if (what == ACodec::kWhatEOS) {
Andreas Huberdc9bacd2011-09-26 10:53:29 -0700553 int32_t err;
554 CHECK(codecRequest->findInt32("err", &err));
555
556 if (err == ERROR_END_OF_STREAM) {
Steve Block3856b092011-10-20 11:56:00 +0100557 ALOGV("got %s decoder EOS", audio ? "audio" : "video");
Andreas Huberdc9bacd2011-09-26 10:53:29 -0700558 } else {
Steve Block3856b092011-10-20 11:56:00 +0100559 ALOGV("got %s decoder EOS w/ error %d",
Andreas Huberdc9bacd2011-09-26 10:53:29 -0700560 audio ? "audio" : "video",
561 err);
562 }
563
564 mRenderer->queueEOS(audio, err);
Andreas Huberf9334412010-12-15 15:17:42 -0800565 } else if (what == ACodec::kWhatFlushCompleted) {
Andreas Huber1aef2112011-01-04 14:01:29 -0800566 bool needShutdown;
Andreas Huber53df1a42010-12-22 10:03:04 -0800567
Andreas Huberf9334412010-12-15 15:17:42 -0800568 if (audio) {
Andreas Huber1aef2112011-01-04 14:01:29 -0800569 CHECK(IsFlushingState(mFlushingAudio, &needShutdown));
Andreas Huberf9334412010-12-15 15:17:42 -0800570 mFlushingAudio = FLUSHED;
571 } else {
Andreas Huber1aef2112011-01-04 14:01:29 -0800572 CHECK(IsFlushingState(mFlushingVideo, &needShutdown));
Andreas Huberf9334412010-12-15 15:17:42 -0800573 mFlushingVideo = FLUSHED;
Andreas Huber3fe62152011-09-16 15:09:22 -0700574
575 mVideoLateByUs = 0;
Andreas Huberf9334412010-12-15 15:17:42 -0800576 }
577
Steve Block3856b092011-10-20 11:56:00 +0100578 ALOGV("decoder %s flush completed", audio ? "audio" : "video");
Andreas Huberf9334412010-12-15 15:17:42 -0800579
Andreas Huber1aef2112011-01-04 14:01:29 -0800580 if (needShutdown) {
Steve Block3856b092011-10-20 11:56:00 +0100581 ALOGV("initiating %s decoder shutdown",
Andreas Huber53df1a42010-12-22 10:03:04 -0800582 audio ? "audio" : "video");
Andreas Huberf9334412010-12-15 15:17:42 -0800583
Andreas Huber53df1a42010-12-22 10:03:04 -0800584 (audio ? mAudioDecoder : mVideoDecoder)->initiateShutdown();
Andreas Huberf9334412010-12-15 15:17:42 -0800585
Andreas Huber53df1a42010-12-22 10:03:04 -0800586 if (audio) {
587 mFlushingAudio = SHUTTING_DOWN_DECODER;
588 } else {
589 mFlushingVideo = SHUTTING_DOWN_DECODER;
590 }
Andreas Huberf9334412010-12-15 15:17:42 -0800591 }
Andreas Huber3831a062010-12-21 10:22:33 -0800592
593 finishFlushIfPossible();
Andreas Huber2c2814b2010-12-15 17:18:20 -0800594 } else if (what == ACodec::kWhatOutputFormatChanged) {
Andreas Huber31e25082011-01-10 10:38:31 -0800595 if (audio) {
596 int32_t numChannels;
Andreas Huber516dacf2012-12-03 15:20:40 -0800597 CHECK(codecRequest->findInt32(
598 "channel-count", &numChannels));
Andreas Huber2c2814b2010-12-15 17:18:20 -0800599
Andreas Huber31e25082011-01-10 10:38:31 -0800600 int32_t sampleRate;
601 CHECK(codecRequest->findInt32("sample-rate", &sampleRate));
Andreas Huber2c2814b2010-12-15 17:18:20 -0800602
Steve Block3856b092011-10-20 11:56:00 +0100603 ALOGV("Audio output format changed to %d Hz, %d channels",
Andreas Huber31e25082011-01-10 10:38:31 -0800604 sampleRate, numChannels);
Andreas Huber2c2814b2010-12-15 17:18:20 -0800605
Andreas Huber31e25082011-01-10 10:38:31 -0800606 mAudioSink->close();
Eric Laurent1948eb32012-04-13 16:50:19 -0700607
608 audio_output_flags_t flags;
609 int64_t durationUs;
Andreas Huber516dacf2012-12-03 15:20:40 -0800610 // FIXME: we should handle the case where the video decoder
611 // is created after we receive the format change indication.
612 // Current code will just make that we select deep buffer
613 // with video which should not be a problem as it should
Eric Laurent1948eb32012-04-13 16:50:19 -0700614 // not prevent from keeping A/V sync.
615 if (mVideoDecoder == NULL &&
616 mSource->getDuration(&durationUs) == OK &&
Andreas Huber516dacf2012-12-03 15:20:40 -0800617 durationUs
618 > AUDIO_SINK_MIN_DEEP_BUFFER_DURATION_US) {
Eric Laurent1948eb32012-04-13 16:50:19 -0700619 flags = AUDIO_OUTPUT_FLAG_DEEP_BUFFER;
620 } else {
621 flags = AUDIO_OUTPUT_FLAG_NONE;
622 }
623
Andreas Huber98065552012-05-03 11:33:01 -0700624 int32_t channelMask;
625 if (!codecRequest->findInt32("channel-mask", &channelMask)) {
626 channelMask = CHANNEL_MASK_USE_CHANNEL_ORDER;
627 }
628
Andreas Huber078cfcf2011-09-15 12:25:04 -0700629 CHECK_EQ(mAudioSink->open(
630 sampleRate,
631 numChannels,
Andreas Huber98065552012-05-03 11:33:01 -0700632 (audio_channel_mask_t)channelMask,
Andreas Huber078cfcf2011-09-15 12:25:04 -0700633 AUDIO_FORMAT_PCM_16_BIT,
Eric Laurent1948eb32012-04-13 16:50:19 -0700634 8 /* bufferCount */,
635 NULL,
636 NULL,
637 flags),
Andreas Huber078cfcf2011-09-15 12:25:04 -0700638 (status_t)OK);
Andreas Huber31e25082011-01-10 10:38:31 -0800639 mAudioSink->start();
Andreas Huber2c2814b2010-12-15 17:18:20 -0800640
Andreas Huber31e25082011-01-10 10:38:31 -0800641 mRenderer->signalAudioSinkChanged();
642 } else {
643 // video
Andreas Huber3831a062010-12-21 10:22:33 -0800644
Andreas Huber31e25082011-01-10 10:38:31 -0800645 int32_t width, height;
646 CHECK(codecRequest->findInt32("width", &width));
647 CHECK(codecRequest->findInt32("height", &height));
648
649 int32_t cropLeft, cropTop, cropRight, cropBottom;
650 CHECK(codecRequest->findRect(
651 "crop",
652 &cropLeft, &cropTop, &cropRight, &cropBottom));
653
Andreas Huber516dacf2012-12-03 15:20:40 -0800654 int32_t displayWidth = cropRight - cropLeft + 1;
655 int32_t displayHeight = cropBottom - cropTop + 1;
656
Steve Block3856b092011-10-20 11:56:00 +0100657 ALOGV("Video output format changed to %d x %d "
Andreas Hubercb67cd12011-08-26 16:02:19 -0700658 "(crop: %d x %d @ (%d, %d))",
Andreas Huber31e25082011-01-10 10:38:31 -0800659 width, height,
Andreas Huber516dacf2012-12-03 15:20:40 -0800660 displayWidth,
661 displayHeight,
Andreas Hubercb67cd12011-08-26 16:02:19 -0700662 cropLeft, cropTop);
Andreas Huber31e25082011-01-10 10:38:31 -0800663
Andreas Huber516dacf2012-12-03 15:20:40 -0800664 sp<AMessage> videoInputFormat =
665 mSource->getFormat(false /* audio */);
666
667 // Take into account sample aspect ratio if necessary:
668 int32_t sarWidth, sarHeight;
669 if (videoInputFormat->findInt32("sar-width", &sarWidth)
670 && videoInputFormat->findInt32(
671 "sar-height", &sarHeight)) {
672 ALOGV("Sample aspect ratio %d : %d",
673 sarWidth, sarHeight);
674
675 displayWidth = (displayWidth * sarWidth) / sarHeight;
676
677 ALOGV("display dimensions %d x %d",
678 displayWidth, displayHeight);
679 }
680
Andreas Huber31e25082011-01-10 10:38:31 -0800681 notifyListener(
Andreas Huber516dacf2012-12-03 15:20:40 -0800682 MEDIA_SET_VIDEO_SIZE, displayWidth, displayHeight);
Andreas Huber31e25082011-01-10 10:38:31 -0800683 }
Andreas Huber3831a062010-12-21 10:22:33 -0800684 } else if (what == ACodec::kWhatShutdownCompleted) {
Steve Block3856b092011-10-20 11:56:00 +0100685 ALOGV("%s shutdown completed", audio ? "audio" : "video");
Andreas Huber3831a062010-12-21 10:22:33 -0800686 if (audio) {
687 mAudioDecoder.clear();
688
689 CHECK_EQ((int)mFlushingAudio, (int)SHUTTING_DOWN_DECODER);
690 mFlushingAudio = SHUT_DOWN;
691 } else {
692 mVideoDecoder.clear();
693
694 CHECK_EQ((int)mFlushingVideo, (int)SHUTTING_DOWN_DECODER);
695 mFlushingVideo = SHUT_DOWN;
696 }
697
698 finishFlushIfPossible();
Andreas Huberc92fd242011-08-16 13:48:44 -0700699 } else if (what == ACodec::kWhatError) {
Steve Block29357bc2012-01-06 19:20:56 +0000700 ALOGE("Received error from %s decoder, aborting playback.",
Andreas Huberc92fd242011-08-16 13:48:44 -0700701 audio ? "audio" : "video");
702
703 mRenderer->queueEOS(audio, UNKNOWN_ERROR);
Andreas Huber57788222012-02-21 11:47:18 -0800704 } else if (what == ACodec::kWhatDrainThisBuffer) {
Andreas Huberf9334412010-12-15 15:17:42 -0800705 renderBuffer(audio, codecRequest);
Andreas Hubera1f8ab02012-11-30 10:53:22 -0800706 } else if (what != ACodec::kWhatComponentAllocated
707 && what != ACodec::kWhatComponentConfigured
708 && what != ACodec::kWhatBuffersAllocated) {
709 ALOGV("Unhandled codec notification %d '%c%c%c%c'.",
710 what,
711 what >> 24,
712 (what >> 16) & 0xff,
713 (what >> 8) & 0xff,
714 what & 0xff);
Andreas Huberf9334412010-12-15 15:17:42 -0800715 }
716
717 break;
718 }
719
720 case kWhatRendererNotify:
721 {
722 int32_t what;
723 CHECK(msg->findInt32("what", &what));
724
725 if (what == Renderer::kWhatEOS) {
726 int32_t audio;
727 CHECK(msg->findInt32("audio", &audio));
728
Andreas Huberc92fd242011-08-16 13:48:44 -0700729 int32_t finalResult;
730 CHECK(msg->findInt32("finalResult", &finalResult));
731
Andreas Huberf9334412010-12-15 15:17:42 -0800732 if (audio) {
733 mAudioEOS = true;
734 } else {
735 mVideoEOS = true;
736 }
737
Andreas Huberc92fd242011-08-16 13:48:44 -0700738 if (finalResult == ERROR_END_OF_STREAM) {
Steve Block3856b092011-10-20 11:56:00 +0100739 ALOGV("reached %s EOS", audio ? "audio" : "video");
Andreas Huberc92fd242011-08-16 13:48:44 -0700740 } else {
Steve Block29357bc2012-01-06 19:20:56 +0000741 ALOGE("%s track encountered an error (%d)",
Andreas Huberc92fd242011-08-16 13:48:44 -0700742 audio ? "audio" : "video", finalResult);
743
744 notifyListener(
745 MEDIA_ERROR, MEDIA_ERROR_UNKNOWN, finalResult);
746 }
Andreas Huberf9334412010-12-15 15:17:42 -0800747
748 if ((mAudioEOS || mAudioDecoder == NULL)
749 && (mVideoEOS || mVideoDecoder == NULL)) {
750 notifyListener(MEDIA_PLAYBACK_COMPLETE, 0, 0);
751 }
Andreas Huber43c3e6c2011-01-05 12:17:08 -0800752 } else if (what == Renderer::kWhatPosition) {
753 int64_t positionUs;
754 CHECK(msg->findInt64("positionUs", &positionUs));
755
Andreas Huber3fe62152011-09-16 15:09:22 -0700756 CHECK(msg->findInt64("videoLateByUs", &mVideoLateByUs));
757
Andreas Huber43c3e6c2011-01-05 12:17:08 -0800758 if (mDriver != NULL) {
759 sp<NuPlayerDriver> driver = mDriver.promote();
760 if (driver != NULL) {
761 driver->notifyPosition(positionUs);
Andreas Huber3fe62152011-09-16 15:09:22 -0700762
763 driver->notifyFrameStats(
764 mNumFramesTotal, mNumFramesDropped);
Andreas Huber43c3e6c2011-01-05 12:17:08 -0800765 }
766 }
Andreas Huber3fe62152011-09-16 15:09:22 -0700767 } else if (what == Renderer::kWhatFlushComplete) {
Andreas Huberf9334412010-12-15 15:17:42 -0800768 int32_t audio;
769 CHECK(msg->findInt32("audio", &audio));
770
Steve Block3856b092011-10-20 11:56:00 +0100771 ALOGV("renderer %s flush completed.", audio ? "audio" : "video");
James Dongf57b4ea2012-07-20 13:38:36 -0700772 } else if (what == Renderer::kWhatVideoRenderingStart) {
773 notifyListener(MEDIA_INFO, MEDIA_INFO_RENDERING_START, 0);
Lajos Molnarcbaffcf2013-08-14 18:30:38 -0700774 } else if (what == Renderer::kWhatMediaRenderingStart) {
775 ALOGV("media rendering started");
776 notifyListener(MEDIA_STARTED, 0, 0);
Andreas Huberf9334412010-12-15 15:17:42 -0800777 }
778 break;
779 }
780
781 case kWhatMoreDataQueued:
782 {
783 break;
784 }
785
Andreas Huber1aef2112011-01-04 14:01:29 -0800786 case kWhatReset:
787 {
Steve Block3856b092011-10-20 11:56:00 +0100788 ALOGV("kWhatReset");
Andreas Huber1aef2112011-01-04 14:01:29 -0800789
Andreas Hubera1f8ab02012-11-30 10:53:22 -0800790 mDeferredActions.push_back(
Andreas Huber14f76722013-01-15 09:04:18 -0800791 new ShutdownDecoderAction(
792 true /* audio */, true /* video */));
Andreas Huberb7c8e912012-11-27 15:02:53 -0800793
Andreas Hubera1f8ab02012-11-30 10:53:22 -0800794 mDeferredActions.push_back(
795 new SimpleAction(&NuPlayer::performReset));
Andreas Huberb58ce9f2011-11-28 16:27:35 -0800796
Andreas Hubera1f8ab02012-11-30 10:53:22 -0800797 processDeferredActions();
Andreas Huber1aef2112011-01-04 14:01:29 -0800798 break;
799 }
800
Andreas Huber43c3e6c2011-01-05 12:17:08 -0800801 case kWhatSeek:
802 {
803 int64_t seekTimeUs;
804 CHECK(msg->findInt64("seekTimeUs", &seekTimeUs));
805
Andreas Hubera1f8ab02012-11-30 10:53:22 -0800806 ALOGV("kWhatSeek seekTimeUs=%lld us", seekTimeUs);
Andreas Huber43c3e6c2011-01-05 12:17:08 -0800807
Andreas Hubera1f8ab02012-11-30 10:53:22 -0800808 mDeferredActions.push_back(
809 new SimpleAction(&NuPlayer::performDecoderFlush));
Andreas Huber43c3e6c2011-01-05 12:17:08 -0800810
Andreas Hubera1f8ab02012-11-30 10:53:22 -0800811 mDeferredActions.push_back(new SeekAction(seekTimeUs));
Andreas Huber43c3e6c2011-01-05 12:17:08 -0800812
Andreas Hubera1f8ab02012-11-30 10:53:22 -0800813 processDeferredActions();
Andreas Huber43c3e6c2011-01-05 12:17:08 -0800814 break;
815 }
816
Andreas Huberb4082222011-01-20 15:23:04 -0800817 case kWhatPause:
818 {
819 CHECK(mRenderer != NULL);
Roger Jönssonfba60da2013-01-21 17:15:45 +0100820 mSource->pause();
Andreas Huberb4082222011-01-20 15:23:04 -0800821 mRenderer->pause();
822 break;
823 }
824
825 case kWhatResume:
826 {
827 CHECK(mRenderer != NULL);
Roger Jönssonfba60da2013-01-21 17:15:45 +0100828 mSource->resume();
Andreas Huberb4082222011-01-20 15:23:04 -0800829 mRenderer->resume();
830 break;
831 }
832
Andreas Huberb5f25f02013-02-05 10:14:26 -0800833 case kWhatSourceNotify:
834 {
Andreas Huber9575c962013-02-05 13:59:56 -0800835 onSourceNotify(msg);
Andreas Huberb5f25f02013-02-05 10:14:26 -0800836 break;
837 }
838
Andreas Huberf9334412010-12-15 15:17:42 -0800839 default:
840 TRESPASS();
841 break;
842 }
843}
844
Andreas Huber3831a062010-12-21 10:22:33 -0800845void NuPlayer::finishFlushIfPossible() {
846 if (mFlushingAudio != FLUSHED && mFlushingAudio != SHUT_DOWN) {
847 return;
848 }
849
850 if (mFlushingVideo != FLUSHED && mFlushingVideo != SHUT_DOWN) {
851 return;
852 }
853
Steve Block3856b092011-10-20 11:56:00 +0100854 ALOGV("both audio and video are flushed now.");
Andreas Huber3831a062010-12-21 10:22:33 -0800855
Andreas Huber6e3d3112011-11-28 12:36:11 -0800856 if (mTimeDiscontinuityPending) {
857 mRenderer->signalTimeDiscontinuity();
858 mTimeDiscontinuityPending = false;
859 }
Andreas Huber3831a062010-12-21 10:22:33 -0800860
Andreas Huber22fc52f2011-01-05 16:24:27 -0800861 if (mAudioDecoder != NULL) {
Andreas Huber3831a062010-12-21 10:22:33 -0800862 mAudioDecoder->signalResume();
863 }
864
Andreas Huber22fc52f2011-01-05 16:24:27 -0800865 if (mVideoDecoder != NULL) {
Andreas Huber3831a062010-12-21 10:22:33 -0800866 mVideoDecoder->signalResume();
867 }
868
869 mFlushingAudio = NONE;
870 mFlushingVideo = NONE;
Andreas Huber3831a062010-12-21 10:22:33 -0800871
Andreas Hubera1f8ab02012-11-30 10:53:22 -0800872 processDeferredActions();
Andreas Huber1aef2112011-01-04 14:01:29 -0800873}
874
875void NuPlayer::postScanSources() {
876 if (mScanSourcesPending) {
877 return;
878 }
879
880 sp<AMessage> msg = new AMessage(kWhatScanSources, id());
881 msg->setInt32("generation", mScanSourcesGeneration);
882 msg->post();
883
884 mScanSourcesPending = true;
885}
886
Andreas Huber5bc087c2010-12-23 10:27:40 -0800887status_t NuPlayer::instantiateDecoder(bool audio, sp<Decoder> *decoder) {
Andreas Huberf9334412010-12-15 15:17:42 -0800888 if (*decoder != NULL) {
889 return OK;
890 }
891
Andreas Huber84066782011-08-16 09:34:26 -0700892 sp<AMessage> format = mSource->getFormat(audio);
Andreas Huberf9334412010-12-15 15:17:42 -0800893
Andreas Huber84066782011-08-16 09:34:26 -0700894 if (format == NULL) {
Andreas Huberf9334412010-12-15 15:17:42 -0800895 return -EWOULDBLOCK;
896 }
897
Andreas Huber3fe62152011-09-16 15:09:22 -0700898 if (!audio) {
Andreas Huber84066782011-08-16 09:34:26 -0700899 AString mime;
900 CHECK(format->findString("mime", &mime));
901 mVideoIsAVC = !strcasecmp(MEDIA_MIMETYPE_VIDEO_AVC, mime.c_str());
Andreas Huber3fe62152011-09-16 15:09:22 -0700902 }
903
Andreas Huberf9334412010-12-15 15:17:42 -0800904 sp<AMessage> notify =
905 new AMessage(audio ? kWhatAudioNotify : kWhatVideoNotify,
906 id());
907
Glenn Kasten11731182011-02-08 17:26:17 -0800908 *decoder = audio ? new Decoder(notify) :
909 new Decoder(notify, mNativeWindow);
Andreas Huberf9334412010-12-15 15:17:42 -0800910 looper()->registerHandler(*decoder);
911
Andreas Huber84066782011-08-16 09:34:26 -0700912 (*decoder)->configure(format);
Andreas Huberf9334412010-12-15 15:17:42 -0800913
Andreas Huberf9334412010-12-15 15:17:42 -0800914 return OK;
915}
916
917status_t NuPlayer::feedDecoderInputData(bool audio, const sp<AMessage> &msg) {
918 sp<AMessage> reply;
919 CHECK(msg->findMessage("reply", &reply));
920
Andreas Huber53df1a42010-12-22 10:03:04 -0800921 if ((audio && IsFlushingState(mFlushingAudio))
922 || (!audio && IsFlushingState(mFlushingVideo))) {
Andreas Huberf9334412010-12-15 15:17:42 -0800923 reply->setInt32("err", INFO_DISCONTINUITY);
924 reply->post();
925 return OK;
926 }
927
928 sp<ABuffer> accessUnit;
Andreas Huberf9334412010-12-15 15:17:42 -0800929
Andreas Huber3fe62152011-09-16 15:09:22 -0700930 bool dropAccessUnit;
931 do {
932 status_t err = mSource->dequeueAccessUnit(audio, &accessUnit);
Andreas Huber5bc087c2010-12-23 10:27:40 -0800933
Andreas Huber3fe62152011-09-16 15:09:22 -0700934 if (err == -EWOULDBLOCK) {
935 return err;
936 } else if (err != OK) {
937 if (err == INFO_DISCONTINUITY) {
938 int32_t type;
939 CHECK(accessUnit->meta()->findInt32("discontinuity", &type));
Andreas Huber53df1a42010-12-22 10:03:04 -0800940
Andreas Huber3fe62152011-09-16 15:09:22 -0700941 bool formatChange =
Andreas Huber6e3d3112011-11-28 12:36:11 -0800942 (audio &&
943 (type & ATSParser::DISCONTINUITY_AUDIO_FORMAT))
944 || (!audio &&
945 (type & ATSParser::DISCONTINUITY_VIDEO_FORMAT));
Andreas Huber53df1a42010-12-22 10:03:04 -0800946
Andreas Huber6e3d3112011-11-28 12:36:11 -0800947 bool timeChange = (type & ATSParser::DISCONTINUITY_TIME) != 0;
948
Steve Blockdf64d152012-01-04 20:05:49 +0000949 ALOGI("%s discontinuity (formatChange=%d, time=%d)",
Andreas Huber6e3d3112011-11-28 12:36:11 -0800950 audio ? "audio" : "video", formatChange, timeChange);
Andreas Huber32f3cef2011-03-02 15:34:46 -0800951
Andreas Huber3fe62152011-09-16 15:09:22 -0700952 if (audio) {
953 mSkipRenderingAudioUntilMediaTimeUs = -1;
954 } else {
955 mSkipRenderingVideoUntilMediaTimeUs = -1;
956 }
Andreas Huber32f3cef2011-03-02 15:34:46 -0800957
Andreas Huber6e3d3112011-11-28 12:36:11 -0800958 if (timeChange) {
959 sp<AMessage> extra;
960 if (accessUnit->meta()->findMessage("extra", &extra)
961 && extra != NULL) {
962 int64_t resumeAtMediaTimeUs;
963 if (extra->findInt64(
964 "resume-at-mediatimeUs", &resumeAtMediaTimeUs)) {
Steve Blockdf64d152012-01-04 20:05:49 +0000965 ALOGI("suppressing rendering of %s until %lld us",
Andreas Huber6e3d3112011-11-28 12:36:11 -0800966 audio ? "audio" : "video", resumeAtMediaTimeUs);
Andreas Huber3fe62152011-09-16 15:09:22 -0700967
Andreas Huber6e3d3112011-11-28 12:36:11 -0800968 if (audio) {
969 mSkipRenderingAudioUntilMediaTimeUs =
970 resumeAtMediaTimeUs;
971 } else {
972 mSkipRenderingVideoUntilMediaTimeUs =
973 resumeAtMediaTimeUs;
974 }
Andreas Huber3fe62152011-09-16 15:09:22 -0700975 }
Andreas Huber32f3cef2011-03-02 15:34:46 -0800976 }
977 }
Andreas Huber3fe62152011-09-16 15:09:22 -0700978
Andreas Huber6e3d3112011-11-28 12:36:11 -0800979 mTimeDiscontinuityPending =
980 mTimeDiscontinuityPending || timeChange;
981
982 if (formatChange || timeChange) {
Andreas Hubera1f8ab02012-11-30 10:53:22 -0800983 if (mFlushingAudio == NONE && mFlushingVideo == NONE) {
984 // And we'll resume scanning sources once we're done
985 // flushing.
986 mDeferredActions.push_front(
987 new SimpleAction(
988 &NuPlayer::performScanSources));
989 }
990
Andreas Huber6e3d3112011-11-28 12:36:11 -0800991 flushDecoder(audio, formatChange);
992 } else {
993 // This stream is unaffected by the discontinuity
994
995 if (audio) {
996 mFlushingAudio = FLUSHED;
997 } else {
998 mFlushingVideo = FLUSHED;
999 }
1000
1001 finishFlushIfPossible();
1002
1003 return -EWOULDBLOCK;
1004 }
Andreas Huber32f3cef2011-03-02 15:34:46 -08001005 }
1006
Andreas Huber3fe62152011-09-16 15:09:22 -07001007 reply->setInt32("err", err);
1008 reply->post();
1009 return OK;
Andreas Huberf9334412010-12-15 15:17:42 -08001010 }
1011
Andreas Huber3fe62152011-09-16 15:09:22 -07001012 if (!audio) {
1013 ++mNumFramesTotal;
1014 }
1015
1016 dropAccessUnit = false;
1017 if (!audio
1018 && mVideoLateByUs > 100000ll
1019 && mVideoIsAVC
1020 && !IsAVCReferenceFrame(accessUnit)) {
1021 dropAccessUnit = true;
1022 ++mNumFramesDropped;
1023 }
1024 } while (dropAccessUnit);
Andreas Huberf9334412010-12-15 15:17:42 -08001025
Steve Block3856b092011-10-20 11:56:00 +01001026 // ALOGV("returned a valid buffer of %s data", audio ? "audio" : "video");
Andreas Huberf9334412010-12-15 15:17:42 -08001027
1028#if 0
1029 int64_t mediaTimeUs;
1030 CHECK(accessUnit->meta()->findInt64("timeUs", &mediaTimeUs));
Steve Block3856b092011-10-20 11:56:00 +01001031 ALOGV("feeding %s input buffer at media time %.2f secs",
Andreas Huberf9334412010-12-15 15:17:42 -08001032 audio ? "audio" : "video",
1033 mediaTimeUs / 1E6);
1034#endif
1035
Andreas Huber2d8bedd2012-02-21 14:38:23 -08001036 reply->setBuffer("buffer", accessUnit);
Andreas Huberf9334412010-12-15 15:17:42 -08001037 reply->post();
1038
1039 return OK;
1040}
1041
1042void NuPlayer::renderBuffer(bool audio, const sp<AMessage> &msg) {
Steve Block3856b092011-10-20 11:56:00 +01001043 // ALOGV("renderBuffer %s", audio ? "audio" : "video");
Andreas Huberf9334412010-12-15 15:17:42 -08001044
1045 sp<AMessage> reply;
1046 CHECK(msg->findMessage("reply", &reply));
1047
Andreas Huber18ac5402011-08-31 15:04:25 -07001048 if (IsFlushingState(audio ? mFlushingAudio : mFlushingVideo)) {
1049 // We're currently attempting to flush the decoder, in order
1050 // to complete this, the decoder wants all its buffers back,
1051 // so we don't want any output buffers it sent us (from before
1052 // we initiated the flush) to be stuck in the renderer's queue.
1053
Steve Block3856b092011-10-20 11:56:00 +01001054 ALOGV("we're still flushing the %s decoder, sending its output buffer"
Andreas Huber18ac5402011-08-31 15:04:25 -07001055 " right back.", audio ? "audio" : "video");
1056
1057 reply->post();
1058 return;
1059 }
1060
Andreas Huber2d8bedd2012-02-21 14:38:23 -08001061 sp<ABuffer> buffer;
1062 CHECK(msg->findBuffer("buffer", &buffer));
Andreas Huberf9334412010-12-15 15:17:42 -08001063
Andreas Huber32f3cef2011-03-02 15:34:46 -08001064 int64_t &skipUntilMediaTimeUs =
1065 audio
1066 ? mSkipRenderingAudioUntilMediaTimeUs
1067 : mSkipRenderingVideoUntilMediaTimeUs;
1068
1069 if (skipUntilMediaTimeUs >= 0) {
1070 int64_t mediaTimeUs;
1071 CHECK(buffer->meta()->findInt64("timeUs", &mediaTimeUs));
1072
1073 if (mediaTimeUs < skipUntilMediaTimeUs) {
Steve Block3856b092011-10-20 11:56:00 +01001074 ALOGV("dropping %s buffer at time %lld as requested.",
Andreas Huber32f3cef2011-03-02 15:34:46 -08001075 audio ? "audio" : "video",
1076 mediaTimeUs);
1077
1078 reply->post();
1079 return;
1080 }
1081
1082 skipUntilMediaTimeUs = -1;
1083 }
1084
Andreas Huberf9334412010-12-15 15:17:42 -08001085 mRenderer->queueBuffer(audio, buffer, reply);
1086}
1087
Chong Zhangdcb89b32013-08-06 09:44:47 -07001088void NuPlayer::notifyListener(int msg, int ext1, int ext2, const Parcel *in) {
Andreas Huber43c3e6c2011-01-05 12:17:08 -08001089 if (mDriver == NULL) {
Andreas Huberf9334412010-12-15 15:17:42 -08001090 return;
1091 }
1092
Andreas Huber43c3e6c2011-01-05 12:17:08 -08001093 sp<NuPlayerDriver> driver = mDriver.promote();
Andreas Huberf9334412010-12-15 15:17:42 -08001094
Andreas Huber43c3e6c2011-01-05 12:17:08 -08001095 if (driver == NULL) {
Andreas Huberf9334412010-12-15 15:17:42 -08001096 return;
1097 }
1098
Chong Zhangdcb89b32013-08-06 09:44:47 -07001099 driver->notifyListener(msg, ext1, ext2, in);
Andreas Huberf9334412010-12-15 15:17:42 -08001100}
1101
Andreas Huber1aef2112011-01-04 14:01:29 -08001102void NuPlayer::flushDecoder(bool audio, bool needShutdown) {
Andreas Huber14f76722013-01-15 09:04:18 -08001103 ALOGV("[%s] flushDecoder needShutdown=%d",
1104 audio ? "audio" : "video", needShutdown);
1105
Andreas Huber6e3d3112011-11-28 12:36:11 -08001106 if ((audio && mAudioDecoder == NULL) || (!audio && mVideoDecoder == NULL)) {
Steve Blockdf64d152012-01-04 20:05:49 +00001107 ALOGI("flushDecoder %s without decoder present",
Andreas Huber6e3d3112011-11-28 12:36:11 -08001108 audio ? "audio" : "video");
1109 }
1110
Andreas Huber1aef2112011-01-04 14:01:29 -08001111 // Make sure we don't continue to scan sources until we finish flushing.
1112 ++mScanSourcesGeneration;
Andreas Huber43c3e6c2011-01-05 12:17:08 -08001113 mScanSourcesPending = false;
Andreas Huber1aef2112011-01-04 14:01:29 -08001114
1115 (audio ? mAudioDecoder : mVideoDecoder)->signalFlush();
1116 mRenderer->flush(audio);
1117
1118 FlushStatus newStatus =
1119 needShutdown ? FLUSHING_DECODER_SHUTDOWN : FLUSHING_DECODER;
1120
1121 if (audio) {
1122 CHECK(mFlushingAudio == NONE
1123 || mFlushingAudio == AWAITING_DISCONTINUITY);
1124
1125 mFlushingAudio = newStatus;
1126
1127 if (mFlushingVideo == NONE) {
1128 mFlushingVideo = (mVideoDecoder != NULL)
1129 ? AWAITING_DISCONTINUITY
1130 : FLUSHED;
1131 }
1132 } else {
1133 CHECK(mFlushingVideo == NONE
1134 || mFlushingVideo == AWAITING_DISCONTINUITY);
1135
1136 mFlushingVideo = newStatus;
1137
1138 if (mFlushingAudio == NONE) {
1139 mFlushingAudio = (mAudioDecoder != NULL)
1140 ? AWAITING_DISCONTINUITY
1141 : FLUSHED;
1142 }
1143 }
1144}
1145
Andreas Huber84066782011-08-16 09:34:26 -07001146sp<AMessage> NuPlayer::Source::getFormat(bool audio) {
1147 sp<MetaData> meta = getFormatMeta(audio);
1148
1149 if (meta == NULL) {
1150 return NULL;
1151 }
1152
1153 sp<AMessage> msg = new AMessage;
1154
1155 if(convertMetaDataToMessage(meta, &msg) == OK) {
1156 return msg;
1157 }
1158 return NULL;
1159}
1160
James Dong0d268a32012-08-31 12:18:27 -07001161status_t NuPlayer::setVideoScalingMode(int32_t mode) {
1162 mVideoScalingMode = mode;
Andreas Huber57a339c2012-12-03 11:18:00 -08001163 if (mNativeWindow != NULL) {
James Dong0d268a32012-08-31 12:18:27 -07001164 status_t ret = native_window_set_scaling_mode(
1165 mNativeWindow->getNativeWindow().get(), mVideoScalingMode);
1166 if (ret != OK) {
1167 ALOGE("Failed to set scaling mode (%d): %s",
1168 -ret, strerror(-ret));
1169 return ret;
1170 }
1171 }
1172 return OK;
1173}
1174
Chong Zhangdcb89b32013-08-06 09:44:47 -07001175status_t NuPlayer::getTrackInfo(Parcel* reply) const {
1176 sp<AMessage> msg = new AMessage(kWhatGetTrackInfo, id());
1177 msg->setPointer("reply", reply);
1178
1179 sp<AMessage> response;
1180 status_t err = msg->postAndAwaitResponse(&response);
1181 return err;
1182}
1183
1184status_t NuPlayer::selectTrack(size_t trackIndex, bool select) {
1185 sp<AMessage> msg = new AMessage(kWhatSelectTrack, id());
1186 msg->setSize("trackIndex", trackIndex);
1187 msg->setInt32("select", select);
1188
1189 sp<AMessage> response;
1190 status_t err = msg->postAndAwaitResponse(&response);
1191
1192 return err;
1193}
1194
Andreas Huberb7c8e912012-11-27 15:02:53 -08001195void NuPlayer::schedulePollDuration() {
1196 sp<AMessage> msg = new AMessage(kWhatPollDuration, id());
1197 msg->setInt32("generation", mPollDurationGeneration);
1198 msg->post();
1199}
1200
1201void NuPlayer::cancelPollDuration() {
1202 ++mPollDurationGeneration;
1203}
1204
Andreas Hubera1f8ab02012-11-30 10:53:22 -08001205void NuPlayer::processDeferredActions() {
1206 while (!mDeferredActions.empty()) {
1207 // We won't execute any deferred actions until we're no longer in
1208 // an intermediate state, i.e. one more more decoders are currently
1209 // flushing or shutting down.
1210
1211 if (mRenderer != NULL) {
1212 // There's an edge case where the renderer owns all output
1213 // buffers and is paused, therefore the decoder will not read
1214 // more input data and will never encounter the matching
1215 // discontinuity. To avoid this, we resume the renderer.
1216
1217 if (mFlushingAudio == AWAITING_DISCONTINUITY
1218 || mFlushingVideo == AWAITING_DISCONTINUITY) {
1219 mRenderer->resume();
1220 }
1221 }
1222
1223 if (mFlushingAudio != NONE || mFlushingVideo != NONE) {
1224 // We're currently flushing, postpone the reset until that's
1225 // completed.
1226
1227 ALOGV("postponing action mFlushingAudio=%d, mFlushingVideo=%d",
1228 mFlushingAudio, mFlushingVideo);
1229
1230 break;
1231 }
1232
1233 sp<Action> action = *mDeferredActions.begin();
1234 mDeferredActions.erase(mDeferredActions.begin());
1235
1236 action->execute(this);
1237 }
1238}
1239
1240void NuPlayer::performSeek(int64_t seekTimeUs) {
1241 ALOGV("performSeek seekTimeUs=%lld us (%.2f secs)",
1242 seekTimeUs,
1243 seekTimeUs / 1E6);
1244
1245 mSource->seekTo(seekTimeUs);
1246
1247 if (mDriver != NULL) {
1248 sp<NuPlayerDriver> driver = mDriver.promote();
1249 if (driver != NULL) {
1250 driver->notifyPosition(seekTimeUs);
1251 driver->notifySeekComplete();
1252 }
1253 }
1254
1255 // everything's flushed, continue playback.
1256}
1257
1258void NuPlayer::performDecoderFlush() {
1259 ALOGV("performDecoderFlush");
1260
Andreas Huberda9740e2013-04-16 10:54:03 -07001261 if (mAudioDecoder == NULL && mVideoDecoder == NULL) {
Andreas Hubera1f8ab02012-11-30 10:53:22 -08001262 return;
1263 }
1264
1265 mTimeDiscontinuityPending = true;
1266
1267 if (mAudioDecoder != NULL) {
1268 flushDecoder(true /* audio */, false /* needShutdown */);
1269 }
1270
1271 if (mVideoDecoder != NULL) {
1272 flushDecoder(false /* audio */, false /* needShutdown */);
1273 }
1274}
1275
Andreas Huber14f76722013-01-15 09:04:18 -08001276void NuPlayer::performDecoderShutdown(bool audio, bool video) {
1277 ALOGV("performDecoderShutdown audio=%d, video=%d", audio, video);
Andreas Hubera1f8ab02012-11-30 10:53:22 -08001278
Andreas Huber14f76722013-01-15 09:04:18 -08001279 if ((!audio || mAudioDecoder == NULL)
1280 && (!video || mVideoDecoder == NULL)) {
Andreas Hubera1f8ab02012-11-30 10:53:22 -08001281 return;
1282 }
1283
1284 mTimeDiscontinuityPending = true;
1285
Andreas Huber14f76722013-01-15 09:04:18 -08001286 if (mFlushingAudio == NONE && (!audio || mAudioDecoder == NULL)) {
1287 mFlushingAudio = FLUSHED;
1288 }
1289
1290 if (mFlushingVideo == NONE && (!video || mVideoDecoder == NULL)) {
1291 mFlushingVideo = FLUSHED;
1292 }
1293
1294 if (audio && mAudioDecoder != NULL) {
Andreas Hubera1f8ab02012-11-30 10:53:22 -08001295 flushDecoder(true /* audio */, true /* needShutdown */);
1296 }
1297
Andreas Huber14f76722013-01-15 09:04:18 -08001298 if (video && mVideoDecoder != NULL) {
Andreas Hubera1f8ab02012-11-30 10:53:22 -08001299 flushDecoder(false /* audio */, true /* needShutdown */);
1300 }
1301}
1302
1303void NuPlayer::performReset() {
1304 ALOGV("performReset");
1305
1306 CHECK(mAudioDecoder == NULL);
1307 CHECK(mVideoDecoder == NULL);
1308
1309 cancelPollDuration();
1310
1311 ++mScanSourcesGeneration;
1312 mScanSourcesPending = false;
1313
1314 mRenderer.clear();
1315
1316 if (mSource != NULL) {
1317 mSource->stop();
Andreas Huberb5f25f02013-02-05 10:14:26 -08001318
1319 looper()->unregisterHandler(mSource->id());
1320
Andreas Hubera1f8ab02012-11-30 10:53:22 -08001321 mSource.clear();
1322 }
1323
1324 if (mDriver != NULL) {
1325 sp<NuPlayerDriver> driver = mDriver.promote();
1326 if (driver != NULL) {
1327 driver->notifyResetComplete();
1328 }
1329 }
Andreas Huber57a339c2012-12-03 11:18:00 -08001330
1331 mStarted = false;
Andreas Hubera1f8ab02012-11-30 10:53:22 -08001332}
1333
1334void NuPlayer::performScanSources() {
1335 ALOGV("performScanSources");
1336
Andreas Huber57a339c2012-12-03 11:18:00 -08001337 if (!mStarted) {
1338 return;
1339 }
1340
Andreas Hubera1f8ab02012-11-30 10:53:22 -08001341 if (mAudioDecoder == NULL || mVideoDecoder == NULL) {
1342 postScanSources();
1343 }
1344}
1345
Andreas Huber57a339c2012-12-03 11:18:00 -08001346void NuPlayer::performSetSurface(const sp<NativeWindowWrapper> &wrapper) {
1347 ALOGV("performSetSurface");
1348
1349 mNativeWindow = wrapper;
1350
1351 // XXX - ignore error from setVideoScalingMode for now
1352 setVideoScalingMode(mVideoScalingMode);
1353
1354 if (mDriver != NULL) {
1355 sp<NuPlayerDriver> driver = mDriver.promote();
1356 if (driver != NULL) {
1357 driver->notifySetSurfaceComplete();
1358 }
1359 }
1360}
1361
Andreas Huber9575c962013-02-05 13:59:56 -08001362void NuPlayer::onSourceNotify(const sp<AMessage> &msg) {
1363 int32_t what;
1364 CHECK(msg->findInt32("what", &what));
1365
1366 switch (what) {
1367 case Source::kWhatPrepared:
1368 {
Andreas Huberb5f28d42013-04-25 15:11:19 -07001369 if (mSource == NULL) {
1370 // This is a stale notification from a source that was
1371 // asynchronously preparing when the client called reset().
1372 // We handled the reset, the source is gone.
1373 break;
1374 }
1375
Andreas Huberec0c5972013-02-05 14:47:13 -08001376 int32_t err;
1377 CHECK(msg->findInt32("err", &err));
1378
Andreas Huber9575c962013-02-05 13:59:56 -08001379 sp<NuPlayerDriver> driver = mDriver.promote();
1380 if (driver != NULL) {
Andreas Huberec0c5972013-02-05 14:47:13 -08001381 driver->notifyPrepareCompleted(err);
Andreas Huber9575c962013-02-05 13:59:56 -08001382 }
Andreas Huber99759402013-04-01 14:28:31 -07001383
1384 int64_t durationUs;
1385 if (mDriver != NULL && mSource->getDuration(&durationUs) == OK) {
1386 sp<NuPlayerDriver> driver = mDriver.promote();
1387 if (driver != NULL) {
1388 driver->notifyDuration(durationUs);
1389 }
1390 }
Andreas Huber9575c962013-02-05 13:59:56 -08001391 break;
1392 }
1393
1394 case Source::kWhatFlagsChanged:
1395 {
1396 uint32_t flags;
1397 CHECK(msg->findInt32("flags", (int32_t *)&flags));
1398
1399 if ((mSourceFlags & Source::FLAG_DYNAMIC_DURATION)
1400 && (!(flags & Source::FLAG_DYNAMIC_DURATION))) {
1401 cancelPollDuration();
1402 } else if (!(mSourceFlags & Source::FLAG_DYNAMIC_DURATION)
1403 && (flags & Source::FLAG_DYNAMIC_DURATION)
1404 && (mAudioDecoder != NULL || mVideoDecoder != NULL)) {
1405 schedulePollDuration();
1406 }
1407
1408 mSourceFlags = flags;
1409 break;
1410 }
1411
1412 case Source::kWhatVideoSizeChanged:
1413 {
1414 int32_t width, height;
1415 CHECK(msg->findInt32("width", &width));
1416 CHECK(msg->findInt32("height", &height));
1417
1418 notifyListener(MEDIA_SET_VIDEO_SIZE, width, height);
1419 break;
1420 }
1421
Roger Jönssonb50e83e2013-01-21 16:26:41 +01001422 case Source::kWhatBufferingStart:
1423 {
1424 notifyListener(MEDIA_INFO, MEDIA_INFO_BUFFERING_START, 0);
1425 break;
1426 }
1427
1428 case Source::kWhatBufferingEnd:
1429 {
1430 notifyListener(MEDIA_INFO, MEDIA_INFO_BUFFERING_END, 0);
1431 break;
1432 }
1433
Chong Zhangdcb89b32013-08-06 09:44:47 -07001434 case Source::kWhatSubtitleData:
1435 {
1436 sp<ABuffer> buffer;
1437 CHECK(msg->findBuffer("buffer", &buffer));
1438
1439 int32_t trackIndex;
1440 int64_t timeUs, durationUs;
1441 CHECK(buffer->meta()->findInt32("trackIndex", &trackIndex));
1442 CHECK(buffer->meta()->findInt64("timeUs", &timeUs));
1443 CHECK(buffer->meta()->findInt64("durationUs", &durationUs));
1444
1445 Parcel in;
1446 in.writeInt32(trackIndex);
1447 in.writeInt64(timeUs);
1448 in.writeInt64(durationUs);
1449 in.writeInt32(buffer->size());
1450 in.writeInt32(buffer->size());
1451 in.write(buffer->data(), buffer->size());
1452
1453 notifyListener(MEDIA_SUBTITLE_DATA, 0, 0, &in);
1454 break;
1455 }
1456
Andreas Huber14f76722013-01-15 09:04:18 -08001457 case Source::kWhatQueueDecoderShutdown:
1458 {
1459 int32_t audio, video;
1460 CHECK(msg->findInt32("audio", &audio));
1461 CHECK(msg->findInt32("video", &video));
1462
1463 sp<AMessage> reply;
1464 CHECK(msg->findMessage("reply", &reply));
1465
1466 queueDecoderShutdown(audio, video, reply);
1467 break;
1468 }
1469
Andreas Huber9575c962013-02-05 13:59:56 -08001470 default:
1471 TRESPASS();
1472 }
1473}
1474
Andreas Huberb5f25f02013-02-05 10:14:26 -08001475////////////////////////////////////////////////////////////////////////////////
1476
Andreas Huber9575c962013-02-05 13:59:56 -08001477void NuPlayer::Source::notifyFlagsChanged(uint32_t flags) {
1478 sp<AMessage> notify = dupNotify();
1479 notify->setInt32("what", kWhatFlagsChanged);
1480 notify->setInt32("flags", flags);
1481 notify->post();
1482}
1483
1484void NuPlayer::Source::notifyVideoSizeChanged(int32_t width, int32_t height) {
1485 sp<AMessage> notify = dupNotify();
1486 notify->setInt32("what", kWhatVideoSizeChanged);
1487 notify->setInt32("width", width);
1488 notify->setInt32("height", height);
1489 notify->post();
1490}
1491
Andreas Huberec0c5972013-02-05 14:47:13 -08001492void NuPlayer::Source::notifyPrepared(status_t err) {
Andreas Huber9575c962013-02-05 13:59:56 -08001493 sp<AMessage> notify = dupNotify();
1494 notify->setInt32("what", kWhatPrepared);
Andreas Huberec0c5972013-02-05 14:47:13 -08001495 notify->setInt32("err", err);
Andreas Huber9575c962013-02-05 13:59:56 -08001496 notify->post();
1497}
1498
Andreas Huberb5f25f02013-02-05 10:14:26 -08001499void NuPlayer::Source::onMessageReceived(const sp<AMessage> &msg) {
1500 TRESPASS();
1501}
1502
Andreas Huber14f76722013-01-15 09:04:18 -08001503void NuPlayer::queueDecoderShutdown(
1504 bool audio, bool video, const sp<AMessage> &reply) {
1505 ALOGI("queueDecoderShutdown audio=%d, video=%d", audio, video);
1506
1507 mDeferredActions.push_back(
1508 new ShutdownDecoderAction(audio, video));
1509
1510 mDeferredActions.push_back(
1511 new SimpleAction(&NuPlayer::performScanSources));
1512
1513 mDeferredActions.push_back(new PostMessageAction(reply));
1514
1515 processDeferredActions();
1516}
1517
Andreas Huberf9334412010-12-15 15:17:42 -08001518} // namespace android