blob: 93fe59482cebefd41a729e786f378cf37edbf163 [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
Martin Storsjoda38df52013-09-25 16:05:36 +030035#include "SoftwareRenderer.h"
36
Andreas Huber84066782011-08-16 09:34:26 -070037#include <cutils/properties.h> // for property_get
Andreas Huber3831a062010-12-21 10:22:33 -080038#include <media/stagefright/foundation/hexdump.h>
Andreas Huberf9334412010-12-15 15:17:42 -080039#include <media/stagefright/foundation/ABuffer.h>
40#include <media/stagefright/foundation/ADebug.h>
41#include <media/stagefright/foundation/AMessage.h>
42#include <media/stagefright/ACodec.h>
Andreas Huber3fe62152011-09-16 15:09:22 -070043#include <media/stagefright/MediaDefs.h>
Andreas Huberf9334412010-12-15 15:17:42 -080044#include <media/stagefright/MediaErrors.h>
45#include <media/stagefright/MetaData.h>
Andy McFadden8ba01022012-12-18 09:46:54 -080046#include <gui/IGraphicBufferProducer.h>
Andreas Huberf9334412010-12-15 15:17:42 -080047
Andreas Huber3fe62152011-09-16 15:09:22 -070048#include "avc_utils.h"
49
Andreas Huber84066782011-08-16 09:34:26 -070050#include "ESDS.h"
51#include <media/stagefright/Utils.h>
52
Andreas Huberf9334412010-12-15 15:17:42 -080053namespace android {
54
Andreas Hubera1f8ab02012-11-30 10:53:22 -080055struct NuPlayer::Action : public RefBase {
56 Action() {}
57
58 virtual void execute(NuPlayer *player) = 0;
59
60private:
61 DISALLOW_EVIL_CONSTRUCTORS(Action);
62};
63
64struct NuPlayer::SeekAction : public Action {
65 SeekAction(int64_t seekTimeUs)
66 : mSeekTimeUs(seekTimeUs) {
67 }
68
69 virtual void execute(NuPlayer *player) {
70 player->performSeek(mSeekTimeUs);
71 }
72
73private:
74 int64_t mSeekTimeUs;
75
76 DISALLOW_EVIL_CONSTRUCTORS(SeekAction);
77};
78
Andreas Huber57a339c2012-12-03 11:18:00 -080079struct NuPlayer::SetSurfaceAction : public Action {
80 SetSurfaceAction(const sp<NativeWindowWrapper> &wrapper)
81 : mWrapper(wrapper) {
82 }
83
84 virtual void execute(NuPlayer *player) {
85 player->performSetSurface(mWrapper);
86 }
87
88private:
89 sp<NativeWindowWrapper> mWrapper;
90
91 DISALLOW_EVIL_CONSTRUCTORS(SetSurfaceAction);
92};
93
Andreas Huber14f76722013-01-15 09:04:18 -080094struct NuPlayer::ShutdownDecoderAction : public Action {
95 ShutdownDecoderAction(bool audio, bool video)
96 : mAudio(audio),
97 mVideo(video) {
98 }
99
100 virtual void execute(NuPlayer *player) {
101 player->performDecoderShutdown(mAudio, mVideo);
102 }
103
104private:
105 bool mAudio;
106 bool mVideo;
107
108 DISALLOW_EVIL_CONSTRUCTORS(ShutdownDecoderAction);
109};
110
111struct NuPlayer::PostMessageAction : public Action {
112 PostMessageAction(const sp<AMessage> &msg)
113 : mMessage(msg) {
114 }
115
116 virtual void execute(NuPlayer *) {
117 mMessage->post();
118 }
119
120private:
121 sp<AMessage> mMessage;
122
123 DISALLOW_EVIL_CONSTRUCTORS(PostMessageAction);
124};
125
Andreas Hubera1f8ab02012-11-30 10:53:22 -0800126// Use this if there's no state necessary to save in order to execute
127// the action.
128struct NuPlayer::SimpleAction : public Action {
129 typedef void (NuPlayer::*ActionFunc)();
130
131 SimpleAction(ActionFunc func)
132 : mFunc(func) {
133 }
134
135 virtual void execute(NuPlayer *player) {
136 (player->*mFunc)();
137 }
138
139private:
140 ActionFunc mFunc;
141
142 DISALLOW_EVIL_CONSTRUCTORS(SimpleAction);
143};
144
Andreas Huberf9334412010-12-15 15:17:42 -0800145////////////////////////////////////////////////////////////////////////////////
146
147NuPlayer::NuPlayer()
Andreas Huber9b80c2b2011-06-30 15:47:02 -0700148 : mUIDValid(false),
Andreas Huber9575c962013-02-05 13:59:56 -0800149 mSourceFlags(0),
Andreas Huber3fe62152011-09-16 15:09:22 -0700150 mVideoIsAVC(false),
Martin Storsjoda38df52013-09-25 16:05:36 +0300151 mNeedsSwRenderer(false),
Andreas Huber9b80c2b2011-06-30 15:47:02 -0700152 mAudioEOS(false),
Andreas Huberf9334412010-12-15 15:17:42 -0800153 mVideoEOS(false),
Andreas Huber5bc087c2010-12-23 10:27:40 -0800154 mScanSourcesPending(false),
Andreas Huber1aef2112011-01-04 14:01:29 -0800155 mScanSourcesGeneration(0),
Andreas Huberb7c8e912012-11-27 15:02:53 -0800156 mPollDurationGeneration(0),
Andreas Huber6e3d3112011-11-28 12:36:11 -0800157 mTimeDiscontinuityPending(false),
Andreas Huberf9334412010-12-15 15:17:42 -0800158 mFlushingAudio(NONE),
Andreas Huber1aef2112011-01-04 14:01:29 -0800159 mFlushingVideo(NONE),
Andreas Huber3fe62152011-09-16 15:09:22 -0700160 mSkipRenderingAudioUntilMediaTimeUs(-1ll),
161 mSkipRenderingVideoUntilMediaTimeUs(-1ll),
162 mVideoLateByUs(0ll),
163 mNumFramesTotal(0ll),
James Dong0d268a32012-08-31 12:18:27 -0700164 mNumFramesDropped(0ll),
Andreas Huber57a339c2012-12-03 11:18:00 -0800165 mVideoScalingMode(NATIVE_WINDOW_SCALING_MODE_SCALE_TO_WINDOW),
166 mStarted(false) {
Andreas Huberf9334412010-12-15 15:17:42 -0800167}
168
169NuPlayer::~NuPlayer() {
170}
171
Andreas Huber9b80c2b2011-06-30 15:47:02 -0700172void NuPlayer::setUID(uid_t uid) {
173 mUIDValid = true;
174 mUID = uid;
175}
176
Andreas Huber43c3e6c2011-01-05 12:17:08 -0800177void NuPlayer::setDriver(const wp<NuPlayerDriver> &driver) {
178 mDriver = driver;
Andreas Huberf9334412010-12-15 15:17:42 -0800179}
180
Andreas Huber9575c962013-02-05 13:59:56 -0800181void NuPlayer::setDataSourceAsync(const sp<IStreamSource> &source) {
Andreas Huberf9334412010-12-15 15:17:42 -0800182 sp<AMessage> msg = new AMessage(kWhatSetDataSource, id());
183
Andreas Huberb5f25f02013-02-05 10:14:26 -0800184 sp<AMessage> notify = new AMessage(kWhatSourceNotify, id());
185
Andreas Huber84066782011-08-16 09:34:26 -0700186 char prop[PROPERTY_VALUE_MAX];
187 if (property_get("media.stagefright.use-mp4source", prop, NULL)
188 && (!strcmp(prop, "1") || !strcasecmp(prop, "true"))) {
Andreas Huberb5f25f02013-02-05 10:14:26 -0800189 msg->setObject("source", new MP4Source(notify, source));
Andreas Huber84066782011-08-16 09:34:26 -0700190 } else {
Andreas Huberb5f25f02013-02-05 10:14:26 -0800191 msg->setObject("source", new StreamingSource(notify, source));
Andreas Huber84066782011-08-16 09:34:26 -0700192 }
193
Andreas Huber5bc087c2010-12-23 10:27:40 -0800194 msg->post();
195}
Andreas Huberf9334412010-12-15 15:17:42 -0800196
Andreas Huberafed0e12011-09-20 15:39:58 -0700197static bool IsHTTPLiveURL(const char *url) {
198 if (!strncasecmp("http://", url, 7)
Andreas Huber99759402013-04-01 14:28:31 -0700199 || !strncasecmp("https://", url, 8)
200 || !strncasecmp("file://", url, 7)) {
Andreas Huberafed0e12011-09-20 15:39:58 -0700201 size_t len = strlen(url);
202 if (len >= 5 && !strcasecmp(".m3u8", &url[len - 5])) {
203 return true;
204 }
205
206 if (strstr(url,"m3u8")) {
207 return true;
208 }
209 }
210
211 return false;
212}
213
Andreas Huber9575c962013-02-05 13:59:56 -0800214void NuPlayer::setDataSourceAsync(
Andreas Huber1b86fe02014-01-29 11:13:26 -0800215 const sp<IMediaHTTPService> &httpService,
216 const char *url,
217 const KeyedVector<String8, String8> *headers) {
Andreas Huber5bc087c2010-12-23 10:27:40 -0800218 sp<AMessage> msg = new AMessage(kWhatSetDataSource, id());
Oscar Rydhé7a33b772012-02-20 10:15:48 +0100219 size_t len = strlen(url);
Andreas Huber5bc087c2010-12-23 10:27:40 -0800220
Andreas Huberb5f25f02013-02-05 10:14:26 -0800221 sp<AMessage> notify = new AMessage(kWhatSourceNotify, id());
222
Andreas Huberafed0e12011-09-20 15:39:58 -0700223 sp<Source> source;
224 if (IsHTTPLiveURL(url)) {
Andreas Huber1b86fe02014-01-29 11:13:26 -0800225 source = new HTTPLiveSource(
226 notify, httpService, url, headers, mUIDValid, mUID);
Andreas Huberafed0e12011-09-20 15:39:58 -0700227 } else if (!strncasecmp(url, "rtsp://", 7)) {
Andreas Huber1b86fe02014-01-29 11:13:26 -0800228 source = new RTSPSource(
229 notify, httpService, url, headers, mUIDValid, mUID);
Oscar Rydhé7a33b772012-02-20 10:15:48 +0100230 } else if ((!strncasecmp(url, "http://", 7)
231 || !strncasecmp(url, "https://", 8))
232 && ((len >= 4 && !strcasecmp(".sdp", &url[len - 4]))
233 || strstr(url, ".sdp?"))) {
Andreas Huber1b86fe02014-01-29 11:13:26 -0800234 source = new RTSPSource(
235 notify, httpService, url, headers, mUIDValid, mUID, true);
Andreas Huber2bfdd422011-10-11 15:24:07 -0700236 } else {
Andreas Huber1b86fe02014-01-29 11:13:26 -0800237 source = new GenericSource(
238 notify, httpService, url, headers, mUIDValid, mUID);
Andreas Huber2bfdd422011-10-11 15:24:07 -0700239 }
240
Andreas Huberafed0e12011-09-20 15:39:58 -0700241 msg->setObject("source", source);
242 msg->post();
243}
244
Andreas Huber9575c962013-02-05 13:59:56 -0800245void NuPlayer::setDataSourceAsync(int fd, int64_t offset, int64_t length) {
Andreas Huberafed0e12011-09-20 15:39:58 -0700246 sp<AMessage> msg = new AMessage(kWhatSetDataSource, id());
247
Andreas Huberb5f25f02013-02-05 10:14:26 -0800248 sp<AMessage> notify = new AMessage(kWhatSourceNotify, id());
249
250 sp<Source> source = new GenericSource(notify, fd, offset, length);
Andreas Huberafed0e12011-09-20 15:39:58 -0700251 msg->setObject("source", source);
Andreas Huberf9334412010-12-15 15:17:42 -0800252 msg->post();
253}
254
Andreas Huber9575c962013-02-05 13:59:56 -0800255void NuPlayer::prepareAsync() {
256 (new AMessage(kWhatPrepare, id()))->post();
257}
258
Andreas Huber57a339c2012-12-03 11:18:00 -0800259void NuPlayer::setVideoSurfaceTextureAsync(
Andy McFadden8ba01022012-12-18 09:46:54 -0800260 const sp<IGraphicBufferProducer> &bufferProducer) {
Glenn Kasten11731182011-02-08 17:26:17 -0800261 sp<AMessage> msg = new AMessage(kWhatSetVideoNativeWindow, id());
Andreas Huber57a339c2012-12-03 11:18:00 -0800262
Andy McFadden8ba01022012-12-18 09:46:54 -0800263 if (bufferProducer == NULL) {
Andreas Huber57a339c2012-12-03 11:18:00 -0800264 msg->setObject("native-window", NULL);
265 } else {
266 msg->setObject(
267 "native-window",
268 new NativeWindowWrapper(
Mathias Agopian1a2952a2013-02-14 17:11:27 -0800269 new Surface(bufferProducer)));
Andreas Huber57a339c2012-12-03 11:18:00 -0800270 }
271
Andreas Huberf9334412010-12-15 15:17:42 -0800272 msg->post();
273}
274
275void NuPlayer::setAudioSink(const sp<MediaPlayerBase::AudioSink> &sink) {
276 sp<AMessage> msg = new AMessage(kWhatSetAudioSink, id());
277 msg->setObject("sink", sink);
278 msg->post();
279}
280
281void NuPlayer::start() {
282 (new AMessage(kWhatStart, id()))->post();
283}
284
Andreas Huber43c3e6c2011-01-05 12:17:08 -0800285void NuPlayer::pause() {
Andreas Huberb4082222011-01-20 15:23:04 -0800286 (new AMessage(kWhatPause, id()))->post();
Andreas Huber43c3e6c2011-01-05 12:17:08 -0800287}
288
289void NuPlayer::resume() {
Andreas Huberb4082222011-01-20 15:23:04 -0800290 (new AMessage(kWhatResume, id()))->post();
Andreas Huber43c3e6c2011-01-05 12:17:08 -0800291}
292
Andreas Huber1aef2112011-01-04 14:01:29 -0800293void NuPlayer::resetAsync() {
294 (new AMessage(kWhatReset, id()))->post();
295}
296
Andreas Huber43c3e6c2011-01-05 12:17:08 -0800297void NuPlayer::seekToAsync(int64_t seekTimeUs) {
298 sp<AMessage> msg = new AMessage(kWhatSeek, id());
299 msg->setInt64("seekTimeUs", seekTimeUs);
300 msg->post();
301}
302
Andreas Huber53df1a42010-12-22 10:03:04 -0800303// static
Andreas Huber1aef2112011-01-04 14:01:29 -0800304bool NuPlayer::IsFlushingState(FlushStatus state, bool *needShutdown) {
Andreas Huber53df1a42010-12-22 10:03:04 -0800305 switch (state) {
306 case FLUSHING_DECODER:
Andreas Huber1aef2112011-01-04 14:01:29 -0800307 if (needShutdown != NULL) {
308 *needShutdown = false;
Andreas Huber53df1a42010-12-22 10:03:04 -0800309 }
310 return true;
311
Andreas Huber1aef2112011-01-04 14:01:29 -0800312 case FLUSHING_DECODER_SHUTDOWN:
313 if (needShutdown != NULL) {
314 *needShutdown = true;
Andreas Huber53df1a42010-12-22 10:03:04 -0800315 }
316 return true;
317
318 default:
319 return false;
320 }
321}
322
Andreas Huberf9334412010-12-15 15:17:42 -0800323void NuPlayer::onMessageReceived(const sp<AMessage> &msg) {
324 switch (msg->what()) {
325 case kWhatSetDataSource:
326 {
Steve Block3856b092011-10-20 11:56:00 +0100327 ALOGV("kWhatSetDataSource");
Andreas Huberf9334412010-12-15 15:17:42 -0800328
329 CHECK(mSource == NULL);
330
Andreas Huber5bc087c2010-12-23 10:27:40 -0800331 sp<RefBase> obj;
332 CHECK(msg->findObject("source", &obj));
Andreas Huberf9334412010-12-15 15:17:42 -0800333
Andreas Huber5bc087c2010-12-23 10:27:40 -0800334 mSource = static_cast<Source *>(obj.get());
Andreas Huberb5f25f02013-02-05 10:14:26 -0800335
336 looper()->registerHandler(mSource);
Andreas Huber9575c962013-02-05 13:59:56 -0800337
338 CHECK(mDriver != NULL);
339 sp<NuPlayerDriver> driver = mDriver.promote();
340 if (driver != NULL) {
341 driver->notifySetDataSourceCompleted(OK);
342 }
343 break;
344 }
345
346 case kWhatPrepare:
347 {
348 mSource->prepareAsync();
Andreas Huberf9334412010-12-15 15:17:42 -0800349 break;
350 }
351
Chong Zhangdcb89b32013-08-06 09:44:47 -0700352 case kWhatGetTrackInfo:
353 {
354 uint32_t replyID;
355 CHECK(msg->senderAwaitsResponse(&replyID));
356
357 status_t err = INVALID_OPERATION;
358 if (mSource != NULL) {
359 Parcel* reply;
360 CHECK(msg->findPointer("reply", (void**)&reply));
361 err = mSource->getTrackInfo(reply);
362 }
363
364 sp<AMessage> response = new AMessage;
365 response->setInt32("err", err);
366
367 response->postReply(replyID);
368 break;
369 }
370
371 case kWhatSelectTrack:
372 {
373 uint32_t replyID;
374 CHECK(msg->senderAwaitsResponse(&replyID));
375
376 status_t err = INVALID_OPERATION;
377 if (mSource != NULL) {
378 size_t trackIndex;
379 int32_t select;
380 CHECK(msg->findSize("trackIndex", &trackIndex));
381 CHECK(msg->findInt32("select", &select));
382 err = mSource->selectTrack(trackIndex, select);
383 }
384
385 sp<AMessage> response = new AMessage;
386 response->setInt32("err", err);
387
388 response->postReply(replyID);
389 break;
390 }
391
Andreas Huberb7c8e912012-11-27 15:02:53 -0800392 case kWhatPollDuration:
393 {
394 int32_t generation;
395 CHECK(msg->findInt32("generation", &generation));
396
397 if (generation != mPollDurationGeneration) {
398 // stale
399 break;
400 }
401
402 int64_t durationUs;
403 if (mDriver != NULL && mSource->getDuration(&durationUs) == OK) {
404 sp<NuPlayerDriver> driver = mDriver.promote();
405 if (driver != NULL) {
406 driver->notifyDuration(durationUs);
407 }
408 }
409
410 msg->post(1000000ll); // poll again in a second.
411 break;
412 }
413
Glenn Kasten11731182011-02-08 17:26:17 -0800414 case kWhatSetVideoNativeWindow:
Andreas Huberf9334412010-12-15 15:17:42 -0800415 {
Steve Block3856b092011-10-20 11:56:00 +0100416 ALOGV("kWhatSetVideoNativeWindow");
Andreas Huberf9334412010-12-15 15:17:42 -0800417
Andreas Huber57a339c2012-12-03 11:18:00 -0800418 mDeferredActions.push_back(
Andreas Huber14f76722013-01-15 09:04:18 -0800419 new ShutdownDecoderAction(
420 false /* audio */, true /* video */));
Andreas Huber57a339c2012-12-03 11:18:00 -0800421
Andreas Huberf9334412010-12-15 15:17:42 -0800422 sp<RefBase> obj;
Glenn Kasten11731182011-02-08 17:26:17 -0800423 CHECK(msg->findObject("native-window", &obj));
Andreas Huberf9334412010-12-15 15:17:42 -0800424
Andreas Huber57a339c2012-12-03 11:18:00 -0800425 mDeferredActions.push_back(
426 new SetSurfaceAction(
427 static_cast<NativeWindowWrapper *>(obj.get())));
James Dong0d268a32012-08-31 12:18:27 -0700428
Andreas Huber57a339c2012-12-03 11:18:00 -0800429 if (obj != NULL) {
430 // If there is a new surface texture, instantiate decoders
431 // again if possible.
432 mDeferredActions.push_back(
433 new SimpleAction(&NuPlayer::performScanSources));
434 }
435
436 processDeferredActions();
Andreas Huberf9334412010-12-15 15:17:42 -0800437 break;
438 }
439
440 case kWhatSetAudioSink:
441 {
Steve Block3856b092011-10-20 11:56:00 +0100442 ALOGV("kWhatSetAudioSink");
Andreas Huberf9334412010-12-15 15:17:42 -0800443
444 sp<RefBase> obj;
445 CHECK(msg->findObject("sink", &obj));
446
447 mAudioSink = static_cast<MediaPlayerBase::AudioSink *>(obj.get());
448 break;
449 }
450
451 case kWhatStart:
452 {
Steve Block3856b092011-10-20 11:56:00 +0100453 ALOGV("kWhatStart");
Andreas Huber43c3e6c2011-01-05 12:17:08 -0800454
Andreas Huber3fe62152011-09-16 15:09:22 -0700455 mVideoIsAVC = false;
Martin Storsjoda38df52013-09-25 16:05:36 +0300456 mNeedsSwRenderer = false;
Andreas Huber1aef2112011-01-04 14:01:29 -0800457 mAudioEOS = false;
458 mVideoEOS = false;
Andreas Huber32f3cef2011-03-02 15:34:46 -0800459 mSkipRenderingAudioUntilMediaTimeUs = -1;
460 mSkipRenderingVideoUntilMediaTimeUs = -1;
Andreas Huber3fe62152011-09-16 15:09:22 -0700461 mVideoLateByUs = 0;
462 mNumFramesTotal = 0;
463 mNumFramesDropped = 0;
Andreas Huber57a339c2012-12-03 11:18:00 -0800464 mStarted = true;
Andreas Huber1aef2112011-01-04 14:01:29 -0800465
Andreas Huber5bc087c2010-12-23 10:27:40 -0800466 mSource->start();
Andreas Huberf9334412010-12-15 15:17:42 -0800467
Andreas Huberd5e56232013-03-12 11:01:43 -0700468 uint32_t flags = 0;
469
470 if (mSource->isRealTime()) {
471 flags |= Renderer::FLAG_REAL_TIME;
472 }
473
Andreas Huberf9334412010-12-15 15:17:42 -0800474 mRenderer = new Renderer(
475 mAudioSink,
Andreas Huberd5e56232013-03-12 11:01:43 -0700476 new AMessage(kWhatRendererNotify, id()),
477 flags);
Andreas Huberf9334412010-12-15 15:17:42 -0800478
479 looper()->registerHandler(mRenderer);
480
Andreas Huber1aef2112011-01-04 14:01:29 -0800481 postScanSources();
Andreas Huberf9334412010-12-15 15:17:42 -0800482 break;
483 }
484
485 case kWhatScanSources:
486 {
Andreas Huber1aef2112011-01-04 14:01:29 -0800487 int32_t generation;
488 CHECK(msg->findInt32("generation", &generation));
489 if (generation != mScanSourcesGeneration) {
490 // Drop obsolete msg.
491 break;
492 }
493
Andreas Huber5bc087c2010-12-23 10:27:40 -0800494 mScanSourcesPending = false;
495
Steve Block3856b092011-10-20 11:56:00 +0100496 ALOGV("scanning sources haveAudio=%d, haveVideo=%d",
Andreas Huber43c3e6c2011-01-05 12:17:08 -0800497 mAudioDecoder != NULL, mVideoDecoder != NULL);
498
Andreas Huberb7c8e912012-11-27 15:02:53 -0800499 bool mHadAnySourcesBefore =
500 (mAudioDecoder != NULL) || (mVideoDecoder != NULL);
501
Haynes Mathew George5d246ef2012-07-09 10:36:57 -0700502 if (mNativeWindow != NULL) {
503 instantiateDecoder(false, &mVideoDecoder);
504 }
Andreas Huberf9334412010-12-15 15:17:42 -0800505
506 if (mAudioSink != NULL) {
Andreas Huber5bc087c2010-12-23 10:27:40 -0800507 instantiateDecoder(true, &mAudioDecoder);
Andreas Huberf9334412010-12-15 15:17:42 -0800508 }
509
Andreas Huberb7c8e912012-11-27 15:02:53 -0800510 if (!mHadAnySourcesBefore
511 && (mAudioDecoder != NULL || mVideoDecoder != NULL)) {
512 // This is the first time we've found anything playable.
513
Andreas Huber9575c962013-02-05 13:59:56 -0800514 if (mSourceFlags & Source::FLAG_DYNAMIC_DURATION) {
Andreas Huberb7c8e912012-11-27 15:02:53 -0800515 schedulePollDuration();
516 }
517 }
518
Andreas Hubereac68ba2011-09-27 12:12:25 -0700519 status_t err;
520 if ((err = mSource->feedMoreTSData()) != OK) {
Andreas Huber1aef2112011-01-04 14:01:29 -0800521 if (mAudioDecoder == NULL && mVideoDecoder == NULL) {
522 // We're not currently decoding anything (no audio or
523 // video tracks found) and we just ran out of input data.
Andreas Hubereac68ba2011-09-27 12:12:25 -0700524
525 if (err == ERROR_END_OF_STREAM) {
526 notifyListener(MEDIA_PLAYBACK_COMPLETE, 0, 0);
527 } else {
528 notifyListener(MEDIA_ERROR, MEDIA_ERROR_UNKNOWN, err);
529 }
Andreas Huber1aef2112011-01-04 14:01:29 -0800530 }
Andreas Huberf9334412010-12-15 15:17:42 -0800531 break;
532 }
533
Andreas Huberfbe9d812012-08-31 14:05:27 -0700534 if ((mAudioDecoder == NULL && mAudioSink != NULL)
535 || (mVideoDecoder == NULL && mNativeWindow != NULL)) {
Andreas Huberf9334412010-12-15 15:17:42 -0800536 msg->post(100000ll);
Andreas Huber5bc087c2010-12-23 10:27:40 -0800537 mScanSourcesPending = true;
Andreas Huberf9334412010-12-15 15:17:42 -0800538 }
539 break;
540 }
541
542 case kWhatVideoNotify:
543 case kWhatAudioNotify:
544 {
545 bool audio = msg->what() == kWhatAudioNotify;
546
547 sp<AMessage> codecRequest;
548 CHECK(msg->findMessage("codec-request", &codecRequest));
549
550 int32_t what;
551 CHECK(codecRequest->findInt32("what", &what));
552
553 if (what == ACodec::kWhatFillThisBuffer) {
554 status_t err = feedDecoderInputData(
555 audio, codecRequest);
556
Andreas Huber5bc087c2010-12-23 10:27:40 -0800557 if (err == -EWOULDBLOCK) {
Andreas Hubereac68ba2011-09-27 12:12:25 -0700558 if (mSource->feedMoreTSData() == OK) {
Andreas Huber1183a4a2011-11-03 11:00:21 -0700559 msg->post(10000ll);
Andreas Huber5bc087c2010-12-23 10:27:40 -0800560 }
Andreas Huberf9334412010-12-15 15:17:42 -0800561 }
562 } else if (what == ACodec::kWhatEOS) {
Andreas Huberdc9bacd2011-09-26 10:53:29 -0700563 int32_t err;
564 CHECK(codecRequest->findInt32("err", &err));
565
566 if (err == ERROR_END_OF_STREAM) {
Steve Block3856b092011-10-20 11:56:00 +0100567 ALOGV("got %s decoder EOS", audio ? "audio" : "video");
Andreas Huberdc9bacd2011-09-26 10:53:29 -0700568 } else {
Steve Block3856b092011-10-20 11:56:00 +0100569 ALOGV("got %s decoder EOS w/ error %d",
Andreas Huberdc9bacd2011-09-26 10:53:29 -0700570 audio ? "audio" : "video",
571 err);
572 }
573
574 mRenderer->queueEOS(audio, err);
Andreas Huberf9334412010-12-15 15:17:42 -0800575 } else if (what == ACodec::kWhatFlushCompleted) {
Andreas Huber1aef2112011-01-04 14:01:29 -0800576 bool needShutdown;
Andreas Huber53df1a42010-12-22 10:03:04 -0800577
Andreas Huberf9334412010-12-15 15:17:42 -0800578 if (audio) {
Andreas Huber1aef2112011-01-04 14:01:29 -0800579 CHECK(IsFlushingState(mFlushingAudio, &needShutdown));
Andreas Huberf9334412010-12-15 15:17:42 -0800580 mFlushingAudio = FLUSHED;
581 } else {
Andreas Huber1aef2112011-01-04 14:01:29 -0800582 CHECK(IsFlushingState(mFlushingVideo, &needShutdown));
Andreas Huberf9334412010-12-15 15:17:42 -0800583 mFlushingVideo = FLUSHED;
Andreas Huber3fe62152011-09-16 15:09:22 -0700584
585 mVideoLateByUs = 0;
Andreas Huberf9334412010-12-15 15:17:42 -0800586 }
587
Steve Block3856b092011-10-20 11:56:00 +0100588 ALOGV("decoder %s flush completed", audio ? "audio" : "video");
Andreas Huberf9334412010-12-15 15:17:42 -0800589
Andreas Huber1aef2112011-01-04 14:01:29 -0800590 if (needShutdown) {
Steve Block3856b092011-10-20 11:56:00 +0100591 ALOGV("initiating %s decoder shutdown",
Andreas Huber53df1a42010-12-22 10:03:04 -0800592 audio ? "audio" : "video");
Andreas Huberf9334412010-12-15 15:17:42 -0800593
Andreas Huber53df1a42010-12-22 10:03:04 -0800594 (audio ? mAudioDecoder : mVideoDecoder)->initiateShutdown();
Andreas Huberf9334412010-12-15 15:17:42 -0800595
Andreas Huber53df1a42010-12-22 10:03:04 -0800596 if (audio) {
597 mFlushingAudio = SHUTTING_DOWN_DECODER;
598 } else {
599 mFlushingVideo = SHUTTING_DOWN_DECODER;
600 }
Andreas Huberf9334412010-12-15 15:17:42 -0800601 }
Andreas Huber3831a062010-12-21 10:22:33 -0800602
603 finishFlushIfPossible();
Andreas Huber2c2814b2010-12-15 17:18:20 -0800604 } else if (what == ACodec::kWhatOutputFormatChanged) {
Andreas Huber31e25082011-01-10 10:38:31 -0800605 if (audio) {
606 int32_t numChannels;
Andreas Huber516dacf2012-12-03 15:20:40 -0800607 CHECK(codecRequest->findInt32(
608 "channel-count", &numChannels));
Andreas Huber2c2814b2010-12-15 17:18:20 -0800609
Andreas Huber31e25082011-01-10 10:38:31 -0800610 int32_t sampleRate;
611 CHECK(codecRequest->findInt32("sample-rate", &sampleRate));
Andreas Huber2c2814b2010-12-15 17:18:20 -0800612
Steve Block3856b092011-10-20 11:56:00 +0100613 ALOGV("Audio output format changed to %d Hz, %d channels",
Andreas Huber31e25082011-01-10 10:38:31 -0800614 sampleRate, numChannels);
Andreas Huber2c2814b2010-12-15 17:18:20 -0800615
Andreas Huber31e25082011-01-10 10:38:31 -0800616 mAudioSink->close();
Eric Laurent1948eb32012-04-13 16:50:19 -0700617
618 audio_output_flags_t flags;
619 int64_t durationUs;
Andreas Huber516dacf2012-12-03 15:20:40 -0800620 // FIXME: we should handle the case where the video decoder
621 // is created after we receive the format change indication.
622 // Current code will just make that we select deep buffer
623 // with video which should not be a problem as it should
Eric Laurent1948eb32012-04-13 16:50:19 -0700624 // not prevent from keeping A/V sync.
625 if (mVideoDecoder == NULL &&
626 mSource->getDuration(&durationUs) == OK &&
Andreas Huber516dacf2012-12-03 15:20:40 -0800627 durationUs
628 > AUDIO_SINK_MIN_DEEP_BUFFER_DURATION_US) {
Eric Laurent1948eb32012-04-13 16:50:19 -0700629 flags = AUDIO_OUTPUT_FLAG_DEEP_BUFFER;
630 } else {
631 flags = AUDIO_OUTPUT_FLAG_NONE;
632 }
633
Andreas Huber98065552012-05-03 11:33:01 -0700634 int32_t channelMask;
635 if (!codecRequest->findInt32("channel-mask", &channelMask)) {
636 channelMask = CHANNEL_MASK_USE_CHANNEL_ORDER;
637 }
638
Andreas Huber078cfcf2011-09-15 12:25:04 -0700639 CHECK_EQ(mAudioSink->open(
640 sampleRate,
641 numChannels,
Andreas Huber98065552012-05-03 11:33:01 -0700642 (audio_channel_mask_t)channelMask,
Andreas Huber078cfcf2011-09-15 12:25:04 -0700643 AUDIO_FORMAT_PCM_16_BIT,
Eric Laurent1948eb32012-04-13 16:50:19 -0700644 8 /* bufferCount */,
645 NULL,
646 NULL,
647 flags),
Andreas Huber078cfcf2011-09-15 12:25:04 -0700648 (status_t)OK);
Andreas Huber31e25082011-01-10 10:38:31 -0800649 mAudioSink->start();
Andreas Huber2c2814b2010-12-15 17:18:20 -0800650
Andreas Huber31e25082011-01-10 10:38:31 -0800651 mRenderer->signalAudioSinkChanged();
652 } else {
653 // video
Andreas Huber3831a062010-12-21 10:22:33 -0800654
Andreas Huber31e25082011-01-10 10:38:31 -0800655 int32_t width, height;
656 CHECK(codecRequest->findInt32("width", &width));
657 CHECK(codecRequest->findInt32("height", &height));
658
659 int32_t cropLeft, cropTop, cropRight, cropBottom;
660 CHECK(codecRequest->findRect(
661 "crop",
662 &cropLeft, &cropTop, &cropRight, &cropBottom));
663
Andreas Huber516dacf2012-12-03 15:20:40 -0800664 int32_t displayWidth = cropRight - cropLeft + 1;
665 int32_t displayHeight = cropBottom - cropTop + 1;
666
Steve Block3856b092011-10-20 11:56:00 +0100667 ALOGV("Video output format changed to %d x %d "
Andreas Hubercb67cd12011-08-26 16:02:19 -0700668 "(crop: %d x %d @ (%d, %d))",
Andreas Huber31e25082011-01-10 10:38:31 -0800669 width, height,
Andreas Huber516dacf2012-12-03 15:20:40 -0800670 displayWidth,
671 displayHeight,
Andreas Hubercb67cd12011-08-26 16:02:19 -0700672 cropLeft, cropTop);
Andreas Huber31e25082011-01-10 10:38:31 -0800673
Andreas Huber516dacf2012-12-03 15:20:40 -0800674 sp<AMessage> videoInputFormat =
675 mSource->getFormat(false /* audio */);
676
677 // Take into account sample aspect ratio if necessary:
678 int32_t sarWidth, sarHeight;
679 if (videoInputFormat->findInt32("sar-width", &sarWidth)
680 && videoInputFormat->findInt32(
681 "sar-height", &sarHeight)) {
682 ALOGV("Sample aspect ratio %d : %d",
683 sarWidth, sarHeight);
684
685 displayWidth = (displayWidth * sarWidth) / sarHeight;
686
687 ALOGV("display dimensions %d x %d",
688 displayWidth, displayHeight);
689 }
690
Andreas Huber31e25082011-01-10 10:38:31 -0800691 notifyListener(
Andreas Huber516dacf2012-12-03 15:20:40 -0800692 MEDIA_SET_VIDEO_SIZE, displayWidth, displayHeight);
Martin Storsjoda38df52013-09-25 16:05:36 +0300693
694 if (mNeedsSwRenderer && mNativeWindow != NULL) {
695 int32_t colorFormat;
696 CHECK(codecRequest->findInt32("color-format", &colorFormat));
697
698 sp<MetaData> meta = new MetaData;
699 meta->setInt32(kKeyWidth, width);
700 meta->setInt32(kKeyHeight, height);
701 meta->setRect(kKeyCropRect, cropLeft, cropTop, cropRight, cropBottom);
702 meta->setInt32(kKeyColorFormat, colorFormat);
703
704 mRenderer->setSoftRenderer(
705 new SoftwareRenderer(mNativeWindow->getNativeWindow(), meta));
706 }
Andreas Huber31e25082011-01-10 10:38:31 -0800707 }
Andreas Huber3831a062010-12-21 10:22:33 -0800708 } else if (what == ACodec::kWhatShutdownCompleted) {
Steve Block3856b092011-10-20 11:56:00 +0100709 ALOGV("%s shutdown completed", audio ? "audio" : "video");
Andreas Huber3831a062010-12-21 10:22:33 -0800710 if (audio) {
711 mAudioDecoder.clear();
712
713 CHECK_EQ((int)mFlushingAudio, (int)SHUTTING_DOWN_DECODER);
714 mFlushingAudio = SHUT_DOWN;
715 } else {
716 mVideoDecoder.clear();
717
718 CHECK_EQ((int)mFlushingVideo, (int)SHUTTING_DOWN_DECODER);
719 mFlushingVideo = SHUT_DOWN;
720 }
721
722 finishFlushIfPossible();
Andreas Huberc92fd242011-08-16 13:48:44 -0700723 } else if (what == ACodec::kWhatError) {
Steve Block29357bc2012-01-06 19:20:56 +0000724 ALOGE("Received error from %s decoder, aborting playback.",
Andreas Huberc92fd242011-08-16 13:48:44 -0700725 audio ? "audio" : "video");
726
727 mRenderer->queueEOS(audio, UNKNOWN_ERROR);
Andreas Huber57788222012-02-21 11:47:18 -0800728 } else if (what == ACodec::kWhatDrainThisBuffer) {
Andreas Huberf9334412010-12-15 15:17:42 -0800729 renderBuffer(audio, codecRequest);
Martin Storsjoda38df52013-09-25 16:05:36 +0300730 } else if (what == ACodec::kWhatComponentAllocated) {
731 if (!audio) {
732 AString name;
733 CHECK(codecRequest->findString("componentName", &name));
734 mNeedsSwRenderer = name.startsWith("OMX.google.");
735 }
736 } else if (what != ACodec::kWhatComponentConfigured
Andreas Hubera1f8ab02012-11-30 10:53:22 -0800737 && what != ACodec::kWhatBuffersAllocated) {
738 ALOGV("Unhandled codec notification %d '%c%c%c%c'.",
739 what,
740 what >> 24,
741 (what >> 16) & 0xff,
742 (what >> 8) & 0xff,
743 what & 0xff);
Andreas Huberf9334412010-12-15 15:17:42 -0800744 }
745
746 break;
747 }
748
749 case kWhatRendererNotify:
750 {
751 int32_t what;
752 CHECK(msg->findInt32("what", &what));
753
754 if (what == Renderer::kWhatEOS) {
755 int32_t audio;
756 CHECK(msg->findInt32("audio", &audio));
757
Andreas Huberc92fd242011-08-16 13:48:44 -0700758 int32_t finalResult;
759 CHECK(msg->findInt32("finalResult", &finalResult));
760
Andreas Huberf9334412010-12-15 15:17:42 -0800761 if (audio) {
762 mAudioEOS = true;
763 } else {
764 mVideoEOS = true;
765 }
766
Andreas Huberc92fd242011-08-16 13:48:44 -0700767 if (finalResult == ERROR_END_OF_STREAM) {
Steve Block3856b092011-10-20 11:56:00 +0100768 ALOGV("reached %s EOS", audio ? "audio" : "video");
Andreas Huberc92fd242011-08-16 13:48:44 -0700769 } else {
Steve Block29357bc2012-01-06 19:20:56 +0000770 ALOGE("%s track encountered an error (%d)",
Andreas Huberc92fd242011-08-16 13:48:44 -0700771 audio ? "audio" : "video", finalResult);
772
773 notifyListener(
774 MEDIA_ERROR, MEDIA_ERROR_UNKNOWN, finalResult);
775 }
Andreas Huberf9334412010-12-15 15:17:42 -0800776
777 if ((mAudioEOS || mAudioDecoder == NULL)
778 && (mVideoEOS || mVideoDecoder == NULL)) {
779 notifyListener(MEDIA_PLAYBACK_COMPLETE, 0, 0);
780 }
Andreas Huber43c3e6c2011-01-05 12:17:08 -0800781 } else if (what == Renderer::kWhatPosition) {
782 int64_t positionUs;
783 CHECK(msg->findInt64("positionUs", &positionUs));
784
Andreas Huber3fe62152011-09-16 15:09:22 -0700785 CHECK(msg->findInt64("videoLateByUs", &mVideoLateByUs));
786
Andreas Huber43c3e6c2011-01-05 12:17:08 -0800787 if (mDriver != NULL) {
788 sp<NuPlayerDriver> driver = mDriver.promote();
789 if (driver != NULL) {
790 driver->notifyPosition(positionUs);
Andreas Huber3fe62152011-09-16 15:09:22 -0700791
792 driver->notifyFrameStats(
793 mNumFramesTotal, mNumFramesDropped);
Andreas Huber43c3e6c2011-01-05 12:17:08 -0800794 }
795 }
Andreas Huber3fe62152011-09-16 15:09:22 -0700796 } else if (what == Renderer::kWhatFlushComplete) {
Andreas Huberf9334412010-12-15 15:17:42 -0800797 int32_t audio;
798 CHECK(msg->findInt32("audio", &audio));
799
Steve Block3856b092011-10-20 11:56:00 +0100800 ALOGV("renderer %s flush completed.", audio ? "audio" : "video");
James Dongf57b4ea2012-07-20 13:38:36 -0700801 } else if (what == Renderer::kWhatVideoRenderingStart) {
802 notifyListener(MEDIA_INFO, MEDIA_INFO_RENDERING_START, 0);
Lajos Molnarcbaffcf2013-08-14 18:30:38 -0700803 } else if (what == Renderer::kWhatMediaRenderingStart) {
804 ALOGV("media rendering started");
805 notifyListener(MEDIA_STARTED, 0, 0);
Andreas Huberf9334412010-12-15 15:17:42 -0800806 }
807 break;
808 }
809
810 case kWhatMoreDataQueued:
811 {
812 break;
813 }
814
Andreas Huber1aef2112011-01-04 14:01:29 -0800815 case kWhatReset:
816 {
Steve Block3856b092011-10-20 11:56:00 +0100817 ALOGV("kWhatReset");
Andreas Huber1aef2112011-01-04 14:01:29 -0800818
Andreas Hubera1f8ab02012-11-30 10:53:22 -0800819 mDeferredActions.push_back(
Andreas Huber14f76722013-01-15 09:04:18 -0800820 new ShutdownDecoderAction(
821 true /* audio */, true /* video */));
Andreas Huberb7c8e912012-11-27 15:02:53 -0800822
Andreas Hubera1f8ab02012-11-30 10:53:22 -0800823 mDeferredActions.push_back(
824 new SimpleAction(&NuPlayer::performReset));
Andreas Huberb58ce9f2011-11-28 16:27:35 -0800825
Andreas Hubera1f8ab02012-11-30 10:53:22 -0800826 processDeferredActions();
Andreas Huber1aef2112011-01-04 14:01:29 -0800827 break;
828 }
829
Andreas Huber43c3e6c2011-01-05 12:17:08 -0800830 case kWhatSeek:
831 {
832 int64_t seekTimeUs;
833 CHECK(msg->findInt64("seekTimeUs", &seekTimeUs));
834
Andreas Hubera1f8ab02012-11-30 10:53:22 -0800835 ALOGV("kWhatSeek seekTimeUs=%lld us", seekTimeUs);
Andreas Huber43c3e6c2011-01-05 12:17:08 -0800836
Andreas Hubera1f8ab02012-11-30 10:53:22 -0800837 mDeferredActions.push_back(
838 new SimpleAction(&NuPlayer::performDecoderFlush));
Andreas Huber43c3e6c2011-01-05 12:17:08 -0800839
Andreas Hubera1f8ab02012-11-30 10:53:22 -0800840 mDeferredActions.push_back(new SeekAction(seekTimeUs));
Andreas Huber43c3e6c2011-01-05 12:17:08 -0800841
Andreas Hubera1f8ab02012-11-30 10:53:22 -0800842 processDeferredActions();
Andreas Huber43c3e6c2011-01-05 12:17:08 -0800843 break;
844 }
845
Andreas Huberb4082222011-01-20 15:23:04 -0800846 case kWhatPause:
847 {
848 CHECK(mRenderer != NULL);
Roger Jönssonfba60da2013-01-21 17:15:45 +0100849 mSource->pause();
Andreas Huberb4082222011-01-20 15:23:04 -0800850 mRenderer->pause();
851 break;
852 }
853
854 case kWhatResume:
855 {
856 CHECK(mRenderer != NULL);
Roger Jönssonfba60da2013-01-21 17:15:45 +0100857 mSource->resume();
Andreas Huberb4082222011-01-20 15:23:04 -0800858 mRenderer->resume();
859 break;
860 }
861
Andreas Huberb5f25f02013-02-05 10:14:26 -0800862 case kWhatSourceNotify:
863 {
Andreas Huber9575c962013-02-05 13:59:56 -0800864 onSourceNotify(msg);
Andreas Huberb5f25f02013-02-05 10:14:26 -0800865 break;
866 }
867
Andreas Huberf9334412010-12-15 15:17:42 -0800868 default:
869 TRESPASS();
870 break;
871 }
872}
873
Andreas Huber3831a062010-12-21 10:22:33 -0800874void NuPlayer::finishFlushIfPossible() {
875 if (mFlushingAudio != FLUSHED && mFlushingAudio != SHUT_DOWN) {
876 return;
877 }
878
879 if (mFlushingVideo != FLUSHED && mFlushingVideo != SHUT_DOWN) {
880 return;
881 }
882
Steve Block3856b092011-10-20 11:56:00 +0100883 ALOGV("both audio and video are flushed now.");
Andreas Huber3831a062010-12-21 10:22:33 -0800884
Andreas Huber6e3d3112011-11-28 12:36:11 -0800885 if (mTimeDiscontinuityPending) {
886 mRenderer->signalTimeDiscontinuity();
887 mTimeDiscontinuityPending = false;
888 }
Andreas Huber3831a062010-12-21 10:22:33 -0800889
Andreas Huber22fc52f2011-01-05 16:24:27 -0800890 if (mAudioDecoder != NULL) {
Andreas Huber3831a062010-12-21 10:22:33 -0800891 mAudioDecoder->signalResume();
892 }
893
Andreas Huber22fc52f2011-01-05 16:24:27 -0800894 if (mVideoDecoder != NULL) {
Andreas Huber3831a062010-12-21 10:22:33 -0800895 mVideoDecoder->signalResume();
896 }
897
898 mFlushingAudio = NONE;
899 mFlushingVideo = NONE;
Andreas Huber3831a062010-12-21 10:22:33 -0800900
Andreas Hubera1f8ab02012-11-30 10:53:22 -0800901 processDeferredActions();
Andreas Huber1aef2112011-01-04 14:01:29 -0800902}
903
904void NuPlayer::postScanSources() {
905 if (mScanSourcesPending) {
906 return;
907 }
908
909 sp<AMessage> msg = new AMessage(kWhatScanSources, id());
910 msg->setInt32("generation", mScanSourcesGeneration);
911 msg->post();
912
913 mScanSourcesPending = true;
914}
915
Andreas Huber5bc087c2010-12-23 10:27:40 -0800916status_t NuPlayer::instantiateDecoder(bool audio, sp<Decoder> *decoder) {
Andreas Huberf9334412010-12-15 15:17:42 -0800917 if (*decoder != NULL) {
918 return OK;
919 }
920
Andreas Huber84066782011-08-16 09:34:26 -0700921 sp<AMessage> format = mSource->getFormat(audio);
Andreas Huberf9334412010-12-15 15:17:42 -0800922
Andreas Huber84066782011-08-16 09:34:26 -0700923 if (format == NULL) {
Andreas Huberf9334412010-12-15 15:17:42 -0800924 return -EWOULDBLOCK;
925 }
926
Andreas Huber3fe62152011-09-16 15:09:22 -0700927 if (!audio) {
Andreas Huber84066782011-08-16 09:34:26 -0700928 AString mime;
929 CHECK(format->findString("mime", &mime));
930 mVideoIsAVC = !strcasecmp(MEDIA_MIMETYPE_VIDEO_AVC, mime.c_str());
Andreas Huber3fe62152011-09-16 15:09:22 -0700931 }
932
Andreas Huberf9334412010-12-15 15:17:42 -0800933 sp<AMessage> notify =
934 new AMessage(audio ? kWhatAudioNotify : kWhatVideoNotify,
935 id());
936
Glenn Kasten11731182011-02-08 17:26:17 -0800937 *decoder = audio ? new Decoder(notify) :
938 new Decoder(notify, mNativeWindow);
Andreas Huberf9334412010-12-15 15:17:42 -0800939 looper()->registerHandler(*decoder);
940
Andreas Huber84066782011-08-16 09:34:26 -0700941 (*decoder)->configure(format);
Andreas Huberf9334412010-12-15 15:17:42 -0800942
Andreas Huberf9334412010-12-15 15:17:42 -0800943 return OK;
944}
945
946status_t NuPlayer::feedDecoderInputData(bool audio, const sp<AMessage> &msg) {
947 sp<AMessage> reply;
948 CHECK(msg->findMessage("reply", &reply));
949
Andreas Huber53df1a42010-12-22 10:03:04 -0800950 if ((audio && IsFlushingState(mFlushingAudio))
951 || (!audio && IsFlushingState(mFlushingVideo))) {
Andreas Huberf9334412010-12-15 15:17:42 -0800952 reply->setInt32("err", INFO_DISCONTINUITY);
953 reply->post();
954 return OK;
955 }
956
957 sp<ABuffer> accessUnit;
Andreas Huberf9334412010-12-15 15:17:42 -0800958
Andreas Huber3fe62152011-09-16 15:09:22 -0700959 bool dropAccessUnit;
960 do {
961 status_t err = mSource->dequeueAccessUnit(audio, &accessUnit);
Andreas Huber5bc087c2010-12-23 10:27:40 -0800962
Andreas Huber3fe62152011-09-16 15:09:22 -0700963 if (err == -EWOULDBLOCK) {
964 return err;
965 } else if (err != OK) {
966 if (err == INFO_DISCONTINUITY) {
967 int32_t type;
968 CHECK(accessUnit->meta()->findInt32("discontinuity", &type));
Andreas Huber53df1a42010-12-22 10:03:04 -0800969
Andreas Huber3fe62152011-09-16 15:09:22 -0700970 bool formatChange =
Andreas Huber6e3d3112011-11-28 12:36:11 -0800971 (audio &&
972 (type & ATSParser::DISCONTINUITY_AUDIO_FORMAT))
973 || (!audio &&
974 (type & ATSParser::DISCONTINUITY_VIDEO_FORMAT));
Andreas Huber53df1a42010-12-22 10:03:04 -0800975
Andreas Huber6e3d3112011-11-28 12:36:11 -0800976 bool timeChange = (type & ATSParser::DISCONTINUITY_TIME) != 0;
977
Steve Blockdf64d152012-01-04 20:05:49 +0000978 ALOGI("%s discontinuity (formatChange=%d, time=%d)",
Andreas Huber6e3d3112011-11-28 12:36:11 -0800979 audio ? "audio" : "video", formatChange, timeChange);
Andreas Huber32f3cef2011-03-02 15:34:46 -0800980
Andreas Huber3fe62152011-09-16 15:09:22 -0700981 if (audio) {
982 mSkipRenderingAudioUntilMediaTimeUs = -1;
983 } else {
984 mSkipRenderingVideoUntilMediaTimeUs = -1;
985 }
Andreas Huber32f3cef2011-03-02 15:34:46 -0800986
Andreas Huber6e3d3112011-11-28 12:36:11 -0800987 if (timeChange) {
988 sp<AMessage> extra;
989 if (accessUnit->meta()->findMessage("extra", &extra)
990 && extra != NULL) {
991 int64_t resumeAtMediaTimeUs;
992 if (extra->findInt64(
993 "resume-at-mediatimeUs", &resumeAtMediaTimeUs)) {
Steve Blockdf64d152012-01-04 20:05:49 +0000994 ALOGI("suppressing rendering of %s until %lld us",
Andreas Huber6e3d3112011-11-28 12:36:11 -0800995 audio ? "audio" : "video", resumeAtMediaTimeUs);
Andreas Huber3fe62152011-09-16 15:09:22 -0700996
Andreas Huber6e3d3112011-11-28 12:36:11 -0800997 if (audio) {
998 mSkipRenderingAudioUntilMediaTimeUs =
999 resumeAtMediaTimeUs;
1000 } else {
1001 mSkipRenderingVideoUntilMediaTimeUs =
1002 resumeAtMediaTimeUs;
1003 }
Andreas Huber3fe62152011-09-16 15:09:22 -07001004 }
Andreas Huber32f3cef2011-03-02 15:34:46 -08001005 }
1006 }
Andreas Huber3fe62152011-09-16 15:09:22 -07001007
Andreas Huber6e3d3112011-11-28 12:36:11 -08001008 mTimeDiscontinuityPending =
1009 mTimeDiscontinuityPending || timeChange;
1010
1011 if (formatChange || timeChange) {
Andreas Hubera1f8ab02012-11-30 10:53:22 -08001012 if (mFlushingAudio == NONE && mFlushingVideo == NONE) {
1013 // And we'll resume scanning sources once we're done
1014 // flushing.
1015 mDeferredActions.push_front(
1016 new SimpleAction(
1017 &NuPlayer::performScanSources));
1018 }
1019
Andreas Huber6e3d3112011-11-28 12:36:11 -08001020 flushDecoder(audio, formatChange);
1021 } else {
1022 // This stream is unaffected by the discontinuity
1023
1024 if (audio) {
1025 mFlushingAudio = FLUSHED;
1026 } else {
1027 mFlushingVideo = FLUSHED;
1028 }
1029
1030 finishFlushIfPossible();
1031
1032 return -EWOULDBLOCK;
1033 }
Andreas Huber32f3cef2011-03-02 15:34:46 -08001034 }
1035
Andreas Huber3fe62152011-09-16 15:09:22 -07001036 reply->setInt32("err", err);
1037 reply->post();
1038 return OK;
Andreas Huberf9334412010-12-15 15:17:42 -08001039 }
1040
Andreas Huber3fe62152011-09-16 15:09:22 -07001041 if (!audio) {
1042 ++mNumFramesTotal;
1043 }
1044
1045 dropAccessUnit = false;
1046 if (!audio
1047 && mVideoLateByUs > 100000ll
1048 && mVideoIsAVC
1049 && !IsAVCReferenceFrame(accessUnit)) {
1050 dropAccessUnit = true;
1051 ++mNumFramesDropped;
1052 }
1053 } while (dropAccessUnit);
Andreas Huberf9334412010-12-15 15:17:42 -08001054
Steve Block3856b092011-10-20 11:56:00 +01001055 // ALOGV("returned a valid buffer of %s data", audio ? "audio" : "video");
Andreas Huberf9334412010-12-15 15:17:42 -08001056
1057#if 0
1058 int64_t mediaTimeUs;
1059 CHECK(accessUnit->meta()->findInt64("timeUs", &mediaTimeUs));
Steve Block3856b092011-10-20 11:56:00 +01001060 ALOGV("feeding %s input buffer at media time %.2f secs",
Andreas Huberf9334412010-12-15 15:17:42 -08001061 audio ? "audio" : "video",
1062 mediaTimeUs / 1E6);
1063#endif
1064
Andreas Huber2d8bedd2012-02-21 14:38:23 -08001065 reply->setBuffer("buffer", accessUnit);
Andreas Huberf9334412010-12-15 15:17:42 -08001066 reply->post();
1067
1068 return OK;
1069}
1070
1071void NuPlayer::renderBuffer(bool audio, const sp<AMessage> &msg) {
Steve Block3856b092011-10-20 11:56:00 +01001072 // ALOGV("renderBuffer %s", audio ? "audio" : "video");
Andreas Huberf9334412010-12-15 15:17:42 -08001073
1074 sp<AMessage> reply;
1075 CHECK(msg->findMessage("reply", &reply));
1076
Andreas Huber18ac5402011-08-31 15:04:25 -07001077 if (IsFlushingState(audio ? mFlushingAudio : mFlushingVideo)) {
1078 // We're currently attempting to flush the decoder, in order
1079 // to complete this, the decoder wants all its buffers back,
1080 // so we don't want any output buffers it sent us (from before
1081 // we initiated the flush) to be stuck in the renderer's queue.
1082
Steve Block3856b092011-10-20 11:56:00 +01001083 ALOGV("we're still flushing the %s decoder, sending its output buffer"
Andreas Huber18ac5402011-08-31 15:04:25 -07001084 " right back.", audio ? "audio" : "video");
1085
1086 reply->post();
1087 return;
1088 }
1089
Andreas Huber2d8bedd2012-02-21 14:38:23 -08001090 sp<ABuffer> buffer;
1091 CHECK(msg->findBuffer("buffer", &buffer));
Andreas Huberf9334412010-12-15 15:17:42 -08001092
Andreas Huber32f3cef2011-03-02 15:34:46 -08001093 int64_t &skipUntilMediaTimeUs =
1094 audio
1095 ? mSkipRenderingAudioUntilMediaTimeUs
1096 : mSkipRenderingVideoUntilMediaTimeUs;
1097
1098 if (skipUntilMediaTimeUs >= 0) {
1099 int64_t mediaTimeUs;
1100 CHECK(buffer->meta()->findInt64("timeUs", &mediaTimeUs));
1101
1102 if (mediaTimeUs < skipUntilMediaTimeUs) {
Steve Block3856b092011-10-20 11:56:00 +01001103 ALOGV("dropping %s buffer at time %lld as requested.",
Andreas Huber32f3cef2011-03-02 15:34:46 -08001104 audio ? "audio" : "video",
1105 mediaTimeUs);
1106
1107 reply->post();
1108 return;
1109 }
1110
1111 skipUntilMediaTimeUs = -1;
1112 }
1113
Andreas Huberf9334412010-12-15 15:17:42 -08001114 mRenderer->queueBuffer(audio, buffer, reply);
1115}
1116
Chong Zhangdcb89b32013-08-06 09:44:47 -07001117void NuPlayer::notifyListener(int msg, int ext1, int ext2, const Parcel *in) {
Andreas Huber43c3e6c2011-01-05 12:17:08 -08001118 if (mDriver == NULL) {
Andreas Huberf9334412010-12-15 15:17:42 -08001119 return;
1120 }
1121
Andreas Huber43c3e6c2011-01-05 12:17:08 -08001122 sp<NuPlayerDriver> driver = mDriver.promote();
Andreas Huberf9334412010-12-15 15:17:42 -08001123
Andreas Huber43c3e6c2011-01-05 12:17:08 -08001124 if (driver == NULL) {
Andreas Huberf9334412010-12-15 15:17:42 -08001125 return;
1126 }
1127
Chong Zhangdcb89b32013-08-06 09:44:47 -07001128 driver->notifyListener(msg, ext1, ext2, in);
Andreas Huberf9334412010-12-15 15:17:42 -08001129}
1130
Andreas Huber1aef2112011-01-04 14:01:29 -08001131void NuPlayer::flushDecoder(bool audio, bool needShutdown) {
Andreas Huber14f76722013-01-15 09:04:18 -08001132 ALOGV("[%s] flushDecoder needShutdown=%d",
1133 audio ? "audio" : "video", needShutdown);
1134
Andreas Huber6e3d3112011-11-28 12:36:11 -08001135 if ((audio && mAudioDecoder == NULL) || (!audio && mVideoDecoder == NULL)) {
Steve Blockdf64d152012-01-04 20:05:49 +00001136 ALOGI("flushDecoder %s without decoder present",
Andreas Huber6e3d3112011-11-28 12:36:11 -08001137 audio ? "audio" : "video");
1138 }
1139
Andreas Huber1aef2112011-01-04 14:01:29 -08001140 // Make sure we don't continue to scan sources until we finish flushing.
1141 ++mScanSourcesGeneration;
Andreas Huber43c3e6c2011-01-05 12:17:08 -08001142 mScanSourcesPending = false;
Andreas Huber1aef2112011-01-04 14:01:29 -08001143
1144 (audio ? mAudioDecoder : mVideoDecoder)->signalFlush();
1145 mRenderer->flush(audio);
1146
1147 FlushStatus newStatus =
1148 needShutdown ? FLUSHING_DECODER_SHUTDOWN : FLUSHING_DECODER;
1149
1150 if (audio) {
1151 CHECK(mFlushingAudio == NONE
1152 || mFlushingAudio == AWAITING_DISCONTINUITY);
1153
1154 mFlushingAudio = newStatus;
1155
1156 if (mFlushingVideo == NONE) {
1157 mFlushingVideo = (mVideoDecoder != NULL)
1158 ? AWAITING_DISCONTINUITY
1159 : FLUSHED;
1160 }
1161 } else {
1162 CHECK(mFlushingVideo == NONE
1163 || mFlushingVideo == AWAITING_DISCONTINUITY);
1164
1165 mFlushingVideo = newStatus;
1166
1167 if (mFlushingAudio == NONE) {
1168 mFlushingAudio = (mAudioDecoder != NULL)
1169 ? AWAITING_DISCONTINUITY
1170 : FLUSHED;
1171 }
1172 }
1173}
1174
Andreas Huber84066782011-08-16 09:34:26 -07001175sp<AMessage> NuPlayer::Source::getFormat(bool audio) {
1176 sp<MetaData> meta = getFormatMeta(audio);
1177
1178 if (meta == NULL) {
1179 return NULL;
1180 }
1181
1182 sp<AMessage> msg = new AMessage;
1183
1184 if(convertMetaDataToMessage(meta, &msg) == OK) {
1185 return msg;
1186 }
1187 return NULL;
1188}
1189
James Dong0d268a32012-08-31 12:18:27 -07001190status_t NuPlayer::setVideoScalingMode(int32_t mode) {
1191 mVideoScalingMode = mode;
Andreas Huber57a339c2012-12-03 11:18:00 -08001192 if (mNativeWindow != NULL) {
James Dong0d268a32012-08-31 12:18:27 -07001193 status_t ret = native_window_set_scaling_mode(
1194 mNativeWindow->getNativeWindow().get(), mVideoScalingMode);
1195 if (ret != OK) {
1196 ALOGE("Failed to set scaling mode (%d): %s",
1197 -ret, strerror(-ret));
1198 return ret;
1199 }
1200 }
1201 return OK;
1202}
1203
Chong Zhangdcb89b32013-08-06 09:44:47 -07001204status_t NuPlayer::getTrackInfo(Parcel* reply) const {
1205 sp<AMessage> msg = new AMessage(kWhatGetTrackInfo, id());
1206 msg->setPointer("reply", reply);
1207
1208 sp<AMessage> response;
1209 status_t err = msg->postAndAwaitResponse(&response);
1210 return err;
1211}
1212
1213status_t NuPlayer::selectTrack(size_t trackIndex, bool select) {
1214 sp<AMessage> msg = new AMessage(kWhatSelectTrack, id());
1215 msg->setSize("trackIndex", trackIndex);
1216 msg->setInt32("select", select);
1217
1218 sp<AMessage> response;
1219 status_t err = msg->postAndAwaitResponse(&response);
1220
1221 return err;
1222}
1223
Andreas Huberb7c8e912012-11-27 15:02:53 -08001224void NuPlayer::schedulePollDuration() {
1225 sp<AMessage> msg = new AMessage(kWhatPollDuration, id());
1226 msg->setInt32("generation", mPollDurationGeneration);
1227 msg->post();
1228}
1229
1230void NuPlayer::cancelPollDuration() {
1231 ++mPollDurationGeneration;
1232}
1233
Andreas Hubera1f8ab02012-11-30 10:53:22 -08001234void NuPlayer::processDeferredActions() {
1235 while (!mDeferredActions.empty()) {
1236 // We won't execute any deferred actions until we're no longer in
1237 // an intermediate state, i.e. one more more decoders are currently
1238 // flushing or shutting down.
1239
1240 if (mRenderer != NULL) {
1241 // There's an edge case where the renderer owns all output
1242 // buffers and is paused, therefore the decoder will not read
1243 // more input data and will never encounter the matching
1244 // discontinuity. To avoid this, we resume the renderer.
1245
1246 if (mFlushingAudio == AWAITING_DISCONTINUITY
1247 || mFlushingVideo == AWAITING_DISCONTINUITY) {
1248 mRenderer->resume();
1249 }
1250 }
1251
1252 if (mFlushingAudio != NONE || mFlushingVideo != NONE) {
1253 // We're currently flushing, postpone the reset until that's
1254 // completed.
1255
1256 ALOGV("postponing action mFlushingAudio=%d, mFlushingVideo=%d",
1257 mFlushingAudio, mFlushingVideo);
1258
1259 break;
1260 }
1261
1262 sp<Action> action = *mDeferredActions.begin();
1263 mDeferredActions.erase(mDeferredActions.begin());
1264
1265 action->execute(this);
1266 }
1267}
1268
1269void NuPlayer::performSeek(int64_t seekTimeUs) {
1270 ALOGV("performSeek seekTimeUs=%lld us (%.2f secs)",
1271 seekTimeUs,
1272 seekTimeUs / 1E6);
1273
1274 mSource->seekTo(seekTimeUs);
1275
1276 if (mDriver != NULL) {
1277 sp<NuPlayerDriver> driver = mDriver.promote();
1278 if (driver != NULL) {
1279 driver->notifyPosition(seekTimeUs);
1280 driver->notifySeekComplete();
1281 }
1282 }
1283
1284 // everything's flushed, continue playback.
1285}
1286
1287void NuPlayer::performDecoderFlush() {
1288 ALOGV("performDecoderFlush");
1289
Andreas Huberda9740e2013-04-16 10:54:03 -07001290 if (mAudioDecoder == NULL && mVideoDecoder == NULL) {
Andreas Hubera1f8ab02012-11-30 10:53:22 -08001291 return;
1292 }
1293
1294 mTimeDiscontinuityPending = true;
1295
1296 if (mAudioDecoder != NULL) {
1297 flushDecoder(true /* audio */, false /* needShutdown */);
1298 }
1299
1300 if (mVideoDecoder != NULL) {
1301 flushDecoder(false /* audio */, false /* needShutdown */);
1302 }
1303}
1304
Andreas Huber14f76722013-01-15 09:04:18 -08001305void NuPlayer::performDecoderShutdown(bool audio, bool video) {
1306 ALOGV("performDecoderShutdown audio=%d, video=%d", audio, video);
Andreas Hubera1f8ab02012-11-30 10:53:22 -08001307
Andreas Huber14f76722013-01-15 09:04:18 -08001308 if ((!audio || mAudioDecoder == NULL)
1309 && (!video || mVideoDecoder == NULL)) {
Andreas Hubera1f8ab02012-11-30 10:53:22 -08001310 return;
1311 }
1312
1313 mTimeDiscontinuityPending = true;
1314
Andreas Huber14f76722013-01-15 09:04:18 -08001315 if (mFlushingAudio == NONE && (!audio || mAudioDecoder == NULL)) {
1316 mFlushingAudio = FLUSHED;
1317 }
1318
1319 if (mFlushingVideo == NONE && (!video || mVideoDecoder == NULL)) {
1320 mFlushingVideo = FLUSHED;
1321 }
1322
1323 if (audio && mAudioDecoder != NULL) {
Andreas Hubera1f8ab02012-11-30 10:53:22 -08001324 flushDecoder(true /* audio */, true /* needShutdown */);
1325 }
1326
Andreas Huber14f76722013-01-15 09:04:18 -08001327 if (video && mVideoDecoder != NULL) {
Andreas Hubera1f8ab02012-11-30 10:53:22 -08001328 flushDecoder(false /* audio */, true /* needShutdown */);
1329 }
1330}
1331
1332void NuPlayer::performReset() {
1333 ALOGV("performReset");
1334
1335 CHECK(mAudioDecoder == NULL);
1336 CHECK(mVideoDecoder == NULL);
1337
1338 cancelPollDuration();
1339
1340 ++mScanSourcesGeneration;
1341 mScanSourcesPending = false;
1342
1343 mRenderer.clear();
1344
1345 if (mSource != NULL) {
1346 mSource->stop();
Andreas Huberb5f25f02013-02-05 10:14:26 -08001347
1348 looper()->unregisterHandler(mSource->id());
1349
Andreas Hubera1f8ab02012-11-30 10:53:22 -08001350 mSource.clear();
1351 }
1352
1353 if (mDriver != NULL) {
1354 sp<NuPlayerDriver> driver = mDriver.promote();
1355 if (driver != NULL) {
1356 driver->notifyResetComplete();
1357 }
1358 }
Andreas Huber57a339c2012-12-03 11:18:00 -08001359
1360 mStarted = false;
Andreas Hubera1f8ab02012-11-30 10:53:22 -08001361}
1362
1363void NuPlayer::performScanSources() {
1364 ALOGV("performScanSources");
1365
Andreas Huber57a339c2012-12-03 11:18:00 -08001366 if (!mStarted) {
1367 return;
1368 }
1369
Andreas Hubera1f8ab02012-11-30 10:53:22 -08001370 if (mAudioDecoder == NULL || mVideoDecoder == NULL) {
1371 postScanSources();
1372 }
1373}
1374
Andreas Huber57a339c2012-12-03 11:18:00 -08001375void NuPlayer::performSetSurface(const sp<NativeWindowWrapper> &wrapper) {
1376 ALOGV("performSetSurface");
1377
1378 mNativeWindow = wrapper;
1379
1380 // XXX - ignore error from setVideoScalingMode for now
1381 setVideoScalingMode(mVideoScalingMode);
1382
1383 if (mDriver != NULL) {
1384 sp<NuPlayerDriver> driver = mDriver.promote();
1385 if (driver != NULL) {
1386 driver->notifySetSurfaceComplete();
1387 }
1388 }
1389}
1390
Andreas Huber9575c962013-02-05 13:59:56 -08001391void NuPlayer::onSourceNotify(const sp<AMessage> &msg) {
1392 int32_t what;
1393 CHECK(msg->findInt32("what", &what));
1394
1395 switch (what) {
1396 case Source::kWhatPrepared:
1397 {
Andreas Huberb5f28d42013-04-25 15:11:19 -07001398 if (mSource == NULL) {
1399 // This is a stale notification from a source that was
1400 // asynchronously preparing when the client called reset().
1401 // We handled the reset, the source is gone.
1402 break;
1403 }
1404
Andreas Huberec0c5972013-02-05 14:47:13 -08001405 int32_t err;
1406 CHECK(msg->findInt32("err", &err));
1407
Andreas Huber9575c962013-02-05 13:59:56 -08001408 sp<NuPlayerDriver> driver = mDriver.promote();
1409 if (driver != NULL) {
Andreas Huberec0c5972013-02-05 14:47:13 -08001410 driver->notifyPrepareCompleted(err);
Andreas Huber9575c962013-02-05 13:59:56 -08001411 }
Andreas Huber99759402013-04-01 14:28:31 -07001412
1413 int64_t durationUs;
1414 if (mDriver != NULL && mSource->getDuration(&durationUs) == OK) {
1415 sp<NuPlayerDriver> driver = mDriver.promote();
1416 if (driver != NULL) {
1417 driver->notifyDuration(durationUs);
1418 }
1419 }
Andreas Huber9575c962013-02-05 13:59:56 -08001420 break;
1421 }
1422
1423 case Source::kWhatFlagsChanged:
1424 {
1425 uint32_t flags;
1426 CHECK(msg->findInt32("flags", (int32_t *)&flags));
1427
Chong Zhang4b7069d2013-09-11 12:52:43 -07001428 sp<NuPlayerDriver> driver = mDriver.promote();
1429 if (driver != NULL) {
1430 driver->notifyFlagsChanged(flags);
1431 }
1432
Andreas Huber9575c962013-02-05 13:59:56 -08001433 if ((mSourceFlags & Source::FLAG_DYNAMIC_DURATION)
1434 && (!(flags & Source::FLAG_DYNAMIC_DURATION))) {
1435 cancelPollDuration();
1436 } else if (!(mSourceFlags & Source::FLAG_DYNAMIC_DURATION)
1437 && (flags & Source::FLAG_DYNAMIC_DURATION)
1438 && (mAudioDecoder != NULL || mVideoDecoder != NULL)) {
1439 schedulePollDuration();
1440 }
1441
1442 mSourceFlags = flags;
1443 break;
1444 }
1445
1446 case Source::kWhatVideoSizeChanged:
1447 {
1448 int32_t width, height;
1449 CHECK(msg->findInt32("width", &width));
1450 CHECK(msg->findInt32("height", &height));
1451
1452 notifyListener(MEDIA_SET_VIDEO_SIZE, width, height);
1453 break;
1454 }
1455
Roger Jönssonb50e83e2013-01-21 16:26:41 +01001456 case Source::kWhatBufferingStart:
1457 {
1458 notifyListener(MEDIA_INFO, MEDIA_INFO_BUFFERING_START, 0);
1459 break;
1460 }
1461
1462 case Source::kWhatBufferingEnd:
1463 {
1464 notifyListener(MEDIA_INFO, MEDIA_INFO_BUFFERING_END, 0);
1465 break;
1466 }
1467
Chong Zhangdcb89b32013-08-06 09:44:47 -07001468 case Source::kWhatSubtitleData:
1469 {
1470 sp<ABuffer> buffer;
1471 CHECK(msg->findBuffer("buffer", &buffer));
1472
1473 int32_t trackIndex;
1474 int64_t timeUs, durationUs;
1475 CHECK(buffer->meta()->findInt32("trackIndex", &trackIndex));
1476 CHECK(buffer->meta()->findInt64("timeUs", &timeUs));
1477 CHECK(buffer->meta()->findInt64("durationUs", &durationUs));
1478
1479 Parcel in;
1480 in.writeInt32(trackIndex);
1481 in.writeInt64(timeUs);
1482 in.writeInt64(durationUs);
1483 in.writeInt32(buffer->size());
1484 in.writeInt32(buffer->size());
1485 in.write(buffer->data(), buffer->size());
1486
1487 notifyListener(MEDIA_SUBTITLE_DATA, 0, 0, &in);
1488 break;
1489 }
1490
Andreas Huber14f76722013-01-15 09:04:18 -08001491 case Source::kWhatQueueDecoderShutdown:
1492 {
1493 int32_t audio, video;
1494 CHECK(msg->findInt32("audio", &audio));
1495 CHECK(msg->findInt32("video", &video));
1496
1497 sp<AMessage> reply;
1498 CHECK(msg->findMessage("reply", &reply));
1499
1500 queueDecoderShutdown(audio, video, reply);
1501 break;
1502 }
1503
Andreas Huber9575c962013-02-05 13:59:56 -08001504 default:
1505 TRESPASS();
1506 }
1507}
1508
Andreas Huberb5f25f02013-02-05 10:14:26 -08001509////////////////////////////////////////////////////////////////////////////////
1510
Andreas Huber9575c962013-02-05 13:59:56 -08001511void NuPlayer::Source::notifyFlagsChanged(uint32_t flags) {
1512 sp<AMessage> notify = dupNotify();
1513 notify->setInt32("what", kWhatFlagsChanged);
1514 notify->setInt32("flags", flags);
1515 notify->post();
1516}
1517
1518void NuPlayer::Source::notifyVideoSizeChanged(int32_t width, int32_t height) {
1519 sp<AMessage> notify = dupNotify();
1520 notify->setInt32("what", kWhatVideoSizeChanged);
1521 notify->setInt32("width", width);
1522 notify->setInt32("height", height);
1523 notify->post();
1524}
1525
Andreas Huberec0c5972013-02-05 14:47:13 -08001526void NuPlayer::Source::notifyPrepared(status_t err) {
Andreas Huber9575c962013-02-05 13:59:56 -08001527 sp<AMessage> notify = dupNotify();
1528 notify->setInt32("what", kWhatPrepared);
Andreas Huberec0c5972013-02-05 14:47:13 -08001529 notify->setInt32("err", err);
Andreas Huber9575c962013-02-05 13:59:56 -08001530 notify->post();
1531}
1532
Andreas Huberb5f25f02013-02-05 10:14:26 -08001533void NuPlayer::Source::onMessageReceived(const sp<AMessage> &msg) {
1534 TRESPASS();
1535}
1536
Andreas Huber14f76722013-01-15 09:04:18 -08001537void NuPlayer::queueDecoderShutdown(
1538 bool audio, bool video, const sp<AMessage> &reply) {
1539 ALOGI("queueDecoderShutdown audio=%d, video=%d", audio, video);
1540
1541 mDeferredActions.push_back(
1542 new ShutdownDecoderAction(audio, video));
1543
1544 mDeferredActions.push_back(
1545 new SimpleAction(&NuPlayer::performScanSources));
1546
1547 mDeferredActions.push_back(new PostMessageAction(reply));
1548
1549 processDeferredActions();
1550}
1551
Andreas Huberf9334412010-12-15 15:17:42 -08001552} // namespace android