blob: 817395aaa3c156a00124bdb6dd3569a2cd4c9885 [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 Huber81e68442014-02-05 11:52:33 -0800225 source = new HTTPLiveSource(notify, httpService, url, headers);
Andreas Huberafed0e12011-09-20 15:39:58 -0700226 } else if (!strncasecmp(url, "rtsp://", 7)) {
Andreas Huber1b86fe02014-01-29 11:13:26 -0800227 source = new RTSPSource(
228 notify, httpService, url, headers, mUIDValid, mUID);
Oscar Rydhé7a33b772012-02-20 10:15:48 +0100229 } else if ((!strncasecmp(url, "http://", 7)
230 || !strncasecmp(url, "https://", 8))
231 && ((len >= 4 && !strcasecmp(".sdp", &url[len - 4]))
232 || strstr(url, ".sdp?"))) {
Andreas Huber1b86fe02014-01-29 11:13:26 -0800233 source = new RTSPSource(
234 notify, httpService, url, headers, mUIDValid, mUID, true);
Andreas Huber2bfdd422011-10-11 15:24:07 -0700235 } else {
Andreas Huber81e68442014-02-05 11:52:33 -0800236 source = new GenericSource(notify, httpService, url, headers);
Andreas Huber2bfdd422011-10-11 15:24:07 -0700237 }
238
Andreas Huberafed0e12011-09-20 15:39:58 -0700239 msg->setObject("source", source);
240 msg->post();
241}
242
Andreas Huber9575c962013-02-05 13:59:56 -0800243void NuPlayer::setDataSourceAsync(int fd, int64_t offset, int64_t length) {
Andreas Huberafed0e12011-09-20 15:39:58 -0700244 sp<AMessage> msg = new AMessage(kWhatSetDataSource, id());
245
Andreas Huberb5f25f02013-02-05 10:14:26 -0800246 sp<AMessage> notify = new AMessage(kWhatSourceNotify, id());
247
248 sp<Source> source = new GenericSource(notify, fd, offset, length);
Andreas Huberafed0e12011-09-20 15:39:58 -0700249 msg->setObject("source", source);
Andreas Huberf9334412010-12-15 15:17:42 -0800250 msg->post();
251}
252
Andreas Huber9575c962013-02-05 13:59:56 -0800253void NuPlayer::prepareAsync() {
254 (new AMessage(kWhatPrepare, id()))->post();
255}
256
Andreas Huber57a339c2012-12-03 11:18:00 -0800257void NuPlayer::setVideoSurfaceTextureAsync(
Andy McFadden8ba01022012-12-18 09:46:54 -0800258 const sp<IGraphicBufferProducer> &bufferProducer) {
Glenn Kasten11731182011-02-08 17:26:17 -0800259 sp<AMessage> msg = new AMessage(kWhatSetVideoNativeWindow, id());
Andreas Huber57a339c2012-12-03 11:18:00 -0800260
Andy McFadden8ba01022012-12-18 09:46:54 -0800261 if (bufferProducer == NULL) {
Andreas Huber57a339c2012-12-03 11:18:00 -0800262 msg->setObject("native-window", NULL);
263 } else {
264 msg->setObject(
265 "native-window",
266 new NativeWindowWrapper(
Mathias Agopian1a2952a2013-02-14 17:11:27 -0800267 new Surface(bufferProducer)));
Andreas Huber57a339c2012-12-03 11:18:00 -0800268 }
269
Andreas Huberf9334412010-12-15 15:17:42 -0800270 msg->post();
271}
272
273void NuPlayer::setAudioSink(const sp<MediaPlayerBase::AudioSink> &sink) {
274 sp<AMessage> msg = new AMessage(kWhatSetAudioSink, id());
275 msg->setObject("sink", sink);
276 msg->post();
277}
278
279void NuPlayer::start() {
280 (new AMessage(kWhatStart, id()))->post();
281}
282
Andreas Huber43c3e6c2011-01-05 12:17:08 -0800283void NuPlayer::pause() {
Andreas Huberb4082222011-01-20 15:23:04 -0800284 (new AMessage(kWhatPause, id()))->post();
Andreas Huber43c3e6c2011-01-05 12:17:08 -0800285}
286
287void NuPlayer::resume() {
Andreas Huberb4082222011-01-20 15:23:04 -0800288 (new AMessage(kWhatResume, id()))->post();
Andreas Huber43c3e6c2011-01-05 12:17:08 -0800289}
290
Andreas Huber1aef2112011-01-04 14:01:29 -0800291void NuPlayer::resetAsync() {
292 (new AMessage(kWhatReset, id()))->post();
293}
294
Andreas Huber43c3e6c2011-01-05 12:17:08 -0800295void NuPlayer::seekToAsync(int64_t seekTimeUs) {
296 sp<AMessage> msg = new AMessage(kWhatSeek, id());
297 msg->setInt64("seekTimeUs", seekTimeUs);
298 msg->post();
299}
300
Andreas Huber53df1a42010-12-22 10:03:04 -0800301// static
Andreas Huber1aef2112011-01-04 14:01:29 -0800302bool NuPlayer::IsFlushingState(FlushStatus state, bool *needShutdown) {
Andreas Huber53df1a42010-12-22 10:03:04 -0800303 switch (state) {
304 case FLUSHING_DECODER:
Andreas Huber1aef2112011-01-04 14:01:29 -0800305 if (needShutdown != NULL) {
306 *needShutdown = false;
Andreas Huber53df1a42010-12-22 10:03:04 -0800307 }
308 return true;
309
Andreas Huber1aef2112011-01-04 14:01:29 -0800310 case FLUSHING_DECODER_SHUTDOWN:
311 if (needShutdown != NULL) {
312 *needShutdown = true;
Andreas Huber53df1a42010-12-22 10:03:04 -0800313 }
314 return true;
315
316 default:
317 return false;
318 }
319}
320
Andreas Huberf9334412010-12-15 15:17:42 -0800321void NuPlayer::onMessageReceived(const sp<AMessage> &msg) {
322 switch (msg->what()) {
323 case kWhatSetDataSource:
324 {
Steve Block3856b092011-10-20 11:56:00 +0100325 ALOGV("kWhatSetDataSource");
Andreas Huberf9334412010-12-15 15:17:42 -0800326
327 CHECK(mSource == NULL);
328
Andreas Huber5bc087c2010-12-23 10:27:40 -0800329 sp<RefBase> obj;
330 CHECK(msg->findObject("source", &obj));
Andreas Huberf9334412010-12-15 15:17:42 -0800331
Andreas Huber5bc087c2010-12-23 10:27:40 -0800332 mSource = static_cast<Source *>(obj.get());
Andreas Huberb5f25f02013-02-05 10:14:26 -0800333
334 looper()->registerHandler(mSource);
Andreas Huber9575c962013-02-05 13:59:56 -0800335
336 CHECK(mDriver != NULL);
337 sp<NuPlayerDriver> driver = mDriver.promote();
338 if (driver != NULL) {
339 driver->notifySetDataSourceCompleted(OK);
340 }
341 break;
342 }
343
344 case kWhatPrepare:
345 {
346 mSource->prepareAsync();
Andreas Huberf9334412010-12-15 15:17:42 -0800347 break;
348 }
349
Chong Zhangdcb89b32013-08-06 09:44:47 -0700350 case kWhatGetTrackInfo:
351 {
352 uint32_t replyID;
353 CHECK(msg->senderAwaitsResponse(&replyID));
354
355 status_t err = INVALID_OPERATION;
356 if (mSource != NULL) {
357 Parcel* reply;
358 CHECK(msg->findPointer("reply", (void**)&reply));
359 err = mSource->getTrackInfo(reply);
360 }
361
362 sp<AMessage> response = new AMessage;
363 response->setInt32("err", err);
364
365 response->postReply(replyID);
366 break;
367 }
368
369 case kWhatSelectTrack:
370 {
371 uint32_t replyID;
372 CHECK(msg->senderAwaitsResponse(&replyID));
373
374 status_t err = INVALID_OPERATION;
375 if (mSource != NULL) {
376 size_t trackIndex;
377 int32_t select;
378 CHECK(msg->findSize("trackIndex", &trackIndex));
379 CHECK(msg->findInt32("select", &select));
380 err = mSource->selectTrack(trackIndex, select);
381 }
382
383 sp<AMessage> response = new AMessage;
384 response->setInt32("err", err);
385
386 response->postReply(replyID);
387 break;
388 }
389
Andreas Huberb7c8e912012-11-27 15:02:53 -0800390 case kWhatPollDuration:
391 {
392 int32_t generation;
393 CHECK(msg->findInt32("generation", &generation));
394
395 if (generation != mPollDurationGeneration) {
396 // stale
397 break;
398 }
399
400 int64_t durationUs;
401 if (mDriver != NULL && mSource->getDuration(&durationUs) == OK) {
402 sp<NuPlayerDriver> driver = mDriver.promote();
403 if (driver != NULL) {
404 driver->notifyDuration(durationUs);
405 }
406 }
407
408 msg->post(1000000ll); // poll again in a second.
409 break;
410 }
411
Glenn Kasten11731182011-02-08 17:26:17 -0800412 case kWhatSetVideoNativeWindow:
Andreas Huberf9334412010-12-15 15:17:42 -0800413 {
Steve Block3856b092011-10-20 11:56:00 +0100414 ALOGV("kWhatSetVideoNativeWindow");
Andreas Huberf9334412010-12-15 15:17:42 -0800415
Andreas Huber57a339c2012-12-03 11:18:00 -0800416 mDeferredActions.push_back(
Andreas Huber14f76722013-01-15 09:04:18 -0800417 new ShutdownDecoderAction(
418 false /* audio */, true /* video */));
Andreas Huber57a339c2012-12-03 11:18:00 -0800419
Andreas Huberf9334412010-12-15 15:17:42 -0800420 sp<RefBase> obj;
Glenn Kasten11731182011-02-08 17:26:17 -0800421 CHECK(msg->findObject("native-window", &obj));
Andreas Huberf9334412010-12-15 15:17:42 -0800422
Andreas Huber57a339c2012-12-03 11:18:00 -0800423 mDeferredActions.push_back(
424 new SetSurfaceAction(
425 static_cast<NativeWindowWrapper *>(obj.get())));
James Dong0d268a32012-08-31 12:18:27 -0700426
Andreas Huber57a339c2012-12-03 11:18:00 -0800427 if (obj != NULL) {
428 // If there is a new surface texture, instantiate decoders
429 // again if possible.
430 mDeferredActions.push_back(
431 new SimpleAction(&NuPlayer::performScanSources));
432 }
433
434 processDeferredActions();
Andreas Huberf9334412010-12-15 15:17:42 -0800435 break;
436 }
437
438 case kWhatSetAudioSink:
439 {
Steve Block3856b092011-10-20 11:56:00 +0100440 ALOGV("kWhatSetAudioSink");
Andreas Huberf9334412010-12-15 15:17:42 -0800441
442 sp<RefBase> obj;
443 CHECK(msg->findObject("sink", &obj));
444
445 mAudioSink = static_cast<MediaPlayerBase::AudioSink *>(obj.get());
446 break;
447 }
448
449 case kWhatStart:
450 {
Steve Block3856b092011-10-20 11:56:00 +0100451 ALOGV("kWhatStart");
Andreas Huber43c3e6c2011-01-05 12:17:08 -0800452
Andreas Huber3fe62152011-09-16 15:09:22 -0700453 mVideoIsAVC = false;
Martin Storsjoda38df52013-09-25 16:05:36 +0300454 mNeedsSwRenderer = false;
Andreas Huber1aef2112011-01-04 14:01:29 -0800455 mAudioEOS = false;
456 mVideoEOS = false;
Andreas Huber32f3cef2011-03-02 15:34:46 -0800457 mSkipRenderingAudioUntilMediaTimeUs = -1;
458 mSkipRenderingVideoUntilMediaTimeUs = -1;
Andreas Huber3fe62152011-09-16 15:09:22 -0700459 mVideoLateByUs = 0;
460 mNumFramesTotal = 0;
461 mNumFramesDropped = 0;
Andreas Huber57a339c2012-12-03 11:18:00 -0800462 mStarted = true;
Andreas Huber1aef2112011-01-04 14:01:29 -0800463
Andreas Huber5bc087c2010-12-23 10:27:40 -0800464 mSource->start();
Andreas Huberf9334412010-12-15 15:17:42 -0800465
Andreas Huberd5e56232013-03-12 11:01:43 -0700466 uint32_t flags = 0;
467
468 if (mSource->isRealTime()) {
469 flags |= Renderer::FLAG_REAL_TIME;
470 }
471
Andreas Huberf9334412010-12-15 15:17:42 -0800472 mRenderer = new Renderer(
473 mAudioSink,
Andreas Huberd5e56232013-03-12 11:01:43 -0700474 new AMessage(kWhatRendererNotify, id()),
475 flags);
Andreas Huberf9334412010-12-15 15:17:42 -0800476
477 looper()->registerHandler(mRenderer);
478
Andreas Huber1aef2112011-01-04 14:01:29 -0800479 postScanSources();
Andreas Huberf9334412010-12-15 15:17:42 -0800480 break;
481 }
482
483 case kWhatScanSources:
484 {
Andreas Huber1aef2112011-01-04 14:01:29 -0800485 int32_t generation;
486 CHECK(msg->findInt32("generation", &generation));
487 if (generation != mScanSourcesGeneration) {
488 // Drop obsolete msg.
489 break;
490 }
491
Andreas Huber5bc087c2010-12-23 10:27:40 -0800492 mScanSourcesPending = false;
493
Steve Block3856b092011-10-20 11:56:00 +0100494 ALOGV("scanning sources haveAudio=%d, haveVideo=%d",
Andreas Huber43c3e6c2011-01-05 12:17:08 -0800495 mAudioDecoder != NULL, mVideoDecoder != NULL);
496
Andreas Huberb7c8e912012-11-27 15:02:53 -0800497 bool mHadAnySourcesBefore =
498 (mAudioDecoder != NULL) || (mVideoDecoder != NULL);
499
Haynes Mathew George5d246ef2012-07-09 10:36:57 -0700500 if (mNativeWindow != NULL) {
501 instantiateDecoder(false, &mVideoDecoder);
502 }
Andreas Huberf9334412010-12-15 15:17:42 -0800503
504 if (mAudioSink != NULL) {
Andreas Huber5bc087c2010-12-23 10:27:40 -0800505 instantiateDecoder(true, &mAudioDecoder);
Andreas Huberf9334412010-12-15 15:17:42 -0800506 }
507
Andreas Huberb7c8e912012-11-27 15:02:53 -0800508 if (!mHadAnySourcesBefore
509 && (mAudioDecoder != NULL || mVideoDecoder != NULL)) {
510 // This is the first time we've found anything playable.
511
Andreas Huber9575c962013-02-05 13:59:56 -0800512 if (mSourceFlags & Source::FLAG_DYNAMIC_DURATION) {
Andreas Huberb7c8e912012-11-27 15:02:53 -0800513 schedulePollDuration();
514 }
515 }
516
Andreas Hubereac68ba2011-09-27 12:12:25 -0700517 status_t err;
518 if ((err = mSource->feedMoreTSData()) != OK) {
Andreas Huber1aef2112011-01-04 14:01:29 -0800519 if (mAudioDecoder == NULL && mVideoDecoder == NULL) {
520 // We're not currently decoding anything (no audio or
521 // video tracks found) and we just ran out of input data.
Andreas Hubereac68ba2011-09-27 12:12:25 -0700522
523 if (err == ERROR_END_OF_STREAM) {
524 notifyListener(MEDIA_PLAYBACK_COMPLETE, 0, 0);
525 } else {
526 notifyListener(MEDIA_ERROR, MEDIA_ERROR_UNKNOWN, err);
527 }
Andreas Huber1aef2112011-01-04 14:01:29 -0800528 }
Andreas Huberf9334412010-12-15 15:17:42 -0800529 break;
530 }
531
Andreas Huberfbe9d812012-08-31 14:05:27 -0700532 if ((mAudioDecoder == NULL && mAudioSink != NULL)
533 || (mVideoDecoder == NULL && mNativeWindow != NULL)) {
Andreas Huberf9334412010-12-15 15:17:42 -0800534 msg->post(100000ll);
Andreas Huber5bc087c2010-12-23 10:27:40 -0800535 mScanSourcesPending = true;
Andreas Huberf9334412010-12-15 15:17:42 -0800536 }
537 break;
538 }
539
540 case kWhatVideoNotify:
541 case kWhatAudioNotify:
542 {
543 bool audio = msg->what() == kWhatAudioNotify;
544
545 sp<AMessage> codecRequest;
546 CHECK(msg->findMessage("codec-request", &codecRequest));
547
548 int32_t what;
549 CHECK(codecRequest->findInt32("what", &what));
550
551 if (what == ACodec::kWhatFillThisBuffer) {
552 status_t err = feedDecoderInputData(
553 audio, codecRequest);
554
Andreas Huber5bc087c2010-12-23 10:27:40 -0800555 if (err == -EWOULDBLOCK) {
Andreas Hubereac68ba2011-09-27 12:12:25 -0700556 if (mSource->feedMoreTSData() == OK) {
Andreas Huber1183a4a2011-11-03 11:00:21 -0700557 msg->post(10000ll);
Andreas Huber5bc087c2010-12-23 10:27:40 -0800558 }
Andreas Huberf9334412010-12-15 15:17:42 -0800559 }
560 } else if (what == ACodec::kWhatEOS) {
Andreas Huberdc9bacd2011-09-26 10:53:29 -0700561 int32_t err;
562 CHECK(codecRequest->findInt32("err", &err));
563
564 if (err == ERROR_END_OF_STREAM) {
Steve Block3856b092011-10-20 11:56:00 +0100565 ALOGV("got %s decoder EOS", audio ? "audio" : "video");
Andreas Huberdc9bacd2011-09-26 10:53:29 -0700566 } else {
Steve Block3856b092011-10-20 11:56:00 +0100567 ALOGV("got %s decoder EOS w/ error %d",
Andreas Huberdc9bacd2011-09-26 10:53:29 -0700568 audio ? "audio" : "video",
569 err);
570 }
571
572 mRenderer->queueEOS(audio, err);
Andreas Huberf9334412010-12-15 15:17:42 -0800573 } else if (what == ACodec::kWhatFlushCompleted) {
Andreas Huber1aef2112011-01-04 14:01:29 -0800574 bool needShutdown;
Andreas Huber53df1a42010-12-22 10:03:04 -0800575
Andreas Huberf9334412010-12-15 15:17:42 -0800576 if (audio) {
Andreas Huber1aef2112011-01-04 14:01:29 -0800577 CHECK(IsFlushingState(mFlushingAudio, &needShutdown));
Andreas Huberf9334412010-12-15 15:17:42 -0800578 mFlushingAudio = FLUSHED;
579 } else {
Andreas Huber1aef2112011-01-04 14:01:29 -0800580 CHECK(IsFlushingState(mFlushingVideo, &needShutdown));
Andreas Huberf9334412010-12-15 15:17:42 -0800581 mFlushingVideo = FLUSHED;
Andreas Huber3fe62152011-09-16 15:09:22 -0700582
583 mVideoLateByUs = 0;
Andreas Huberf9334412010-12-15 15:17:42 -0800584 }
585
Steve Block3856b092011-10-20 11:56:00 +0100586 ALOGV("decoder %s flush completed", audio ? "audio" : "video");
Andreas Huberf9334412010-12-15 15:17:42 -0800587
Andreas Huber1aef2112011-01-04 14:01:29 -0800588 if (needShutdown) {
Steve Block3856b092011-10-20 11:56:00 +0100589 ALOGV("initiating %s decoder shutdown",
Andreas Huber53df1a42010-12-22 10:03:04 -0800590 audio ? "audio" : "video");
Andreas Huberf9334412010-12-15 15:17:42 -0800591
Andreas Huber53df1a42010-12-22 10:03:04 -0800592 (audio ? mAudioDecoder : mVideoDecoder)->initiateShutdown();
Andreas Huberf9334412010-12-15 15:17:42 -0800593
Andreas Huber53df1a42010-12-22 10:03:04 -0800594 if (audio) {
595 mFlushingAudio = SHUTTING_DOWN_DECODER;
596 } else {
597 mFlushingVideo = SHUTTING_DOWN_DECODER;
598 }
Andreas Huberf9334412010-12-15 15:17:42 -0800599 }
Andreas Huber3831a062010-12-21 10:22:33 -0800600
601 finishFlushIfPossible();
Andreas Huber2c2814b2010-12-15 17:18:20 -0800602 } else if (what == ACodec::kWhatOutputFormatChanged) {
Andreas Huber31e25082011-01-10 10:38:31 -0800603 if (audio) {
604 int32_t numChannels;
Andreas Huber516dacf2012-12-03 15:20:40 -0800605 CHECK(codecRequest->findInt32(
606 "channel-count", &numChannels));
Andreas Huber2c2814b2010-12-15 17:18:20 -0800607
Andreas Huber31e25082011-01-10 10:38:31 -0800608 int32_t sampleRate;
609 CHECK(codecRequest->findInt32("sample-rate", &sampleRate));
Andreas Huber2c2814b2010-12-15 17:18:20 -0800610
Steve Block3856b092011-10-20 11:56:00 +0100611 ALOGV("Audio output format changed to %d Hz, %d channels",
Andreas Huber31e25082011-01-10 10:38:31 -0800612 sampleRate, numChannels);
Andreas Huber2c2814b2010-12-15 17:18:20 -0800613
Andreas Huber31e25082011-01-10 10:38:31 -0800614 mAudioSink->close();
Eric Laurent1948eb32012-04-13 16:50:19 -0700615
616 audio_output_flags_t flags;
617 int64_t durationUs;
Andreas Huber516dacf2012-12-03 15:20:40 -0800618 // FIXME: we should handle the case where the video decoder
619 // is created after we receive the format change indication.
620 // Current code will just make that we select deep buffer
621 // with video which should not be a problem as it should
Eric Laurent1948eb32012-04-13 16:50:19 -0700622 // not prevent from keeping A/V sync.
623 if (mVideoDecoder == NULL &&
624 mSource->getDuration(&durationUs) == OK &&
Andreas Huber516dacf2012-12-03 15:20:40 -0800625 durationUs
626 > AUDIO_SINK_MIN_DEEP_BUFFER_DURATION_US) {
Eric Laurent1948eb32012-04-13 16:50:19 -0700627 flags = AUDIO_OUTPUT_FLAG_DEEP_BUFFER;
628 } else {
629 flags = AUDIO_OUTPUT_FLAG_NONE;
630 }
631
Andreas Huber98065552012-05-03 11:33:01 -0700632 int32_t channelMask;
633 if (!codecRequest->findInt32("channel-mask", &channelMask)) {
634 channelMask = CHANNEL_MASK_USE_CHANNEL_ORDER;
635 }
636
Andreas Huber078cfcf2011-09-15 12:25:04 -0700637 CHECK_EQ(mAudioSink->open(
638 sampleRate,
639 numChannels,
Andreas Huber98065552012-05-03 11:33:01 -0700640 (audio_channel_mask_t)channelMask,
Andreas Huber078cfcf2011-09-15 12:25:04 -0700641 AUDIO_FORMAT_PCM_16_BIT,
Eric Laurent1948eb32012-04-13 16:50:19 -0700642 8 /* bufferCount */,
643 NULL,
644 NULL,
645 flags),
Andreas Huber078cfcf2011-09-15 12:25:04 -0700646 (status_t)OK);
Andreas Huber31e25082011-01-10 10:38:31 -0800647 mAudioSink->start();
Andreas Huber2c2814b2010-12-15 17:18:20 -0800648
Andreas Huber31e25082011-01-10 10:38:31 -0800649 mRenderer->signalAudioSinkChanged();
650 } else {
651 // video
Andreas Huber3831a062010-12-21 10:22:33 -0800652
Andreas Huber31e25082011-01-10 10:38:31 -0800653 int32_t width, height;
654 CHECK(codecRequest->findInt32("width", &width));
655 CHECK(codecRequest->findInt32("height", &height));
656
657 int32_t cropLeft, cropTop, cropRight, cropBottom;
658 CHECK(codecRequest->findRect(
659 "crop",
660 &cropLeft, &cropTop, &cropRight, &cropBottom));
661
Andreas Huber516dacf2012-12-03 15:20:40 -0800662 int32_t displayWidth = cropRight - cropLeft + 1;
663 int32_t displayHeight = cropBottom - cropTop + 1;
664
Steve Block3856b092011-10-20 11:56:00 +0100665 ALOGV("Video output format changed to %d x %d "
Andreas Hubercb67cd12011-08-26 16:02:19 -0700666 "(crop: %d x %d @ (%d, %d))",
Andreas Huber31e25082011-01-10 10:38:31 -0800667 width, height,
Andreas Huber516dacf2012-12-03 15:20:40 -0800668 displayWidth,
669 displayHeight,
Andreas Hubercb67cd12011-08-26 16:02:19 -0700670 cropLeft, cropTop);
Andreas Huber31e25082011-01-10 10:38:31 -0800671
Andreas Huber516dacf2012-12-03 15:20:40 -0800672 sp<AMessage> videoInputFormat =
673 mSource->getFormat(false /* audio */);
674
675 // Take into account sample aspect ratio if necessary:
676 int32_t sarWidth, sarHeight;
677 if (videoInputFormat->findInt32("sar-width", &sarWidth)
678 && videoInputFormat->findInt32(
679 "sar-height", &sarHeight)) {
680 ALOGV("Sample aspect ratio %d : %d",
681 sarWidth, sarHeight);
682
683 displayWidth = (displayWidth * sarWidth) / sarHeight;
684
685 ALOGV("display dimensions %d x %d",
686 displayWidth, displayHeight);
687 }
688
Andreas Huber31e25082011-01-10 10:38:31 -0800689 notifyListener(
Andreas Huber516dacf2012-12-03 15:20:40 -0800690 MEDIA_SET_VIDEO_SIZE, displayWidth, displayHeight);
Martin Storsjoda38df52013-09-25 16:05:36 +0300691
692 if (mNeedsSwRenderer && mNativeWindow != NULL) {
693 int32_t colorFormat;
694 CHECK(codecRequest->findInt32("color-format", &colorFormat));
695
696 sp<MetaData> meta = new MetaData;
697 meta->setInt32(kKeyWidth, width);
698 meta->setInt32(kKeyHeight, height);
699 meta->setRect(kKeyCropRect, cropLeft, cropTop, cropRight, cropBottom);
700 meta->setInt32(kKeyColorFormat, colorFormat);
701
702 mRenderer->setSoftRenderer(
703 new SoftwareRenderer(mNativeWindow->getNativeWindow(), meta));
704 }
Andreas Huber31e25082011-01-10 10:38:31 -0800705 }
Andreas Huber3831a062010-12-21 10:22:33 -0800706 } else if (what == ACodec::kWhatShutdownCompleted) {
Steve Block3856b092011-10-20 11:56:00 +0100707 ALOGV("%s shutdown completed", audio ? "audio" : "video");
Andreas Huber3831a062010-12-21 10:22:33 -0800708 if (audio) {
709 mAudioDecoder.clear();
710
711 CHECK_EQ((int)mFlushingAudio, (int)SHUTTING_DOWN_DECODER);
712 mFlushingAudio = SHUT_DOWN;
713 } else {
714 mVideoDecoder.clear();
715
716 CHECK_EQ((int)mFlushingVideo, (int)SHUTTING_DOWN_DECODER);
717 mFlushingVideo = SHUT_DOWN;
718 }
719
720 finishFlushIfPossible();
Andreas Huberc92fd242011-08-16 13:48:44 -0700721 } else if (what == ACodec::kWhatError) {
Steve Block29357bc2012-01-06 19:20:56 +0000722 ALOGE("Received error from %s decoder, aborting playback.",
Andreas Huberc92fd242011-08-16 13:48:44 -0700723 audio ? "audio" : "video");
724
725 mRenderer->queueEOS(audio, UNKNOWN_ERROR);
Andreas Huber57788222012-02-21 11:47:18 -0800726 } else if (what == ACodec::kWhatDrainThisBuffer) {
Andreas Huberf9334412010-12-15 15:17:42 -0800727 renderBuffer(audio, codecRequest);
Martin Storsjoda38df52013-09-25 16:05:36 +0300728 } else if (what == ACodec::kWhatComponentAllocated) {
729 if (!audio) {
730 AString name;
731 CHECK(codecRequest->findString("componentName", &name));
732 mNeedsSwRenderer = name.startsWith("OMX.google.");
733 }
734 } else if (what != ACodec::kWhatComponentConfigured
Andreas Hubera1f8ab02012-11-30 10:53:22 -0800735 && what != ACodec::kWhatBuffersAllocated) {
736 ALOGV("Unhandled codec notification %d '%c%c%c%c'.",
737 what,
738 what >> 24,
739 (what >> 16) & 0xff,
740 (what >> 8) & 0xff,
741 what & 0xff);
Andreas Huberf9334412010-12-15 15:17:42 -0800742 }
743
744 break;
745 }
746
747 case kWhatRendererNotify:
748 {
749 int32_t what;
750 CHECK(msg->findInt32("what", &what));
751
752 if (what == Renderer::kWhatEOS) {
753 int32_t audio;
754 CHECK(msg->findInt32("audio", &audio));
755
Andreas Huberc92fd242011-08-16 13:48:44 -0700756 int32_t finalResult;
757 CHECK(msg->findInt32("finalResult", &finalResult));
758
Andreas Huberf9334412010-12-15 15:17:42 -0800759 if (audio) {
760 mAudioEOS = true;
761 } else {
762 mVideoEOS = true;
763 }
764
Andreas Huberc92fd242011-08-16 13:48:44 -0700765 if (finalResult == ERROR_END_OF_STREAM) {
Steve Block3856b092011-10-20 11:56:00 +0100766 ALOGV("reached %s EOS", audio ? "audio" : "video");
Andreas Huberc92fd242011-08-16 13:48:44 -0700767 } else {
Steve Block29357bc2012-01-06 19:20:56 +0000768 ALOGE("%s track encountered an error (%d)",
Andreas Huberc92fd242011-08-16 13:48:44 -0700769 audio ? "audio" : "video", finalResult);
770
771 notifyListener(
772 MEDIA_ERROR, MEDIA_ERROR_UNKNOWN, finalResult);
773 }
Andreas Huberf9334412010-12-15 15:17:42 -0800774
775 if ((mAudioEOS || mAudioDecoder == NULL)
776 && (mVideoEOS || mVideoDecoder == NULL)) {
777 notifyListener(MEDIA_PLAYBACK_COMPLETE, 0, 0);
778 }
Andreas Huber43c3e6c2011-01-05 12:17:08 -0800779 } else if (what == Renderer::kWhatPosition) {
780 int64_t positionUs;
781 CHECK(msg->findInt64("positionUs", &positionUs));
782
Andreas Huber3fe62152011-09-16 15:09:22 -0700783 CHECK(msg->findInt64("videoLateByUs", &mVideoLateByUs));
784
Andreas Huber43c3e6c2011-01-05 12:17:08 -0800785 if (mDriver != NULL) {
786 sp<NuPlayerDriver> driver = mDriver.promote();
787 if (driver != NULL) {
788 driver->notifyPosition(positionUs);
Andreas Huber3fe62152011-09-16 15:09:22 -0700789
790 driver->notifyFrameStats(
791 mNumFramesTotal, mNumFramesDropped);
Andreas Huber43c3e6c2011-01-05 12:17:08 -0800792 }
793 }
Andreas Huber3fe62152011-09-16 15:09:22 -0700794 } else if (what == Renderer::kWhatFlushComplete) {
Andreas Huberf9334412010-12-15 15:17:42 -0800795 int32_t audio;
796 CHECK(msg->findInt32("audio", &audio));
797
Steve Block3856b092011-10-20 11:56:00 +0100798 ALOGV("renderer %s flush completed.", audio ? "audio" : "video");
James Dongf57b4ea2012-07-20 13:38:36 -0700799 } else if (what == Renderer::kWhatVideoRenderingStart) {
800 notifyListener(MEDIA_INFO, MEDIA_INFO_RENDERING_START, 0);
Lajos Molnarcbaffcf2013-08-14 18:30:38 -0700801 } else if (what == Renderer::kWhatMediaRenderingStart) {
802 ALOGV("media rendering started");
803 notifyListener(MEDIA_STARTED, 0, 0);
Andreas Huberf9334412010-12-15 15:17:42 -0800804 }
805 break;
806 }
807
808 case kWhatMoreDataQueued:
809 {
810 break;
811 }
812
Andreas Huber1aef2112011-01-04 14:01:29 -0800813 case kWhatReset:
814 {
Steve Block3856b092011-10-20 11:56:00 +0100815 ALOGV("kWhatReset");
Andreas Huber1aef2112011-01-04 14:01:29 -0800816
Andreas Hubera1f8ab02012-11-30 10:53:22 -0800817 mDeferredActions.push_back(
Andreas Huber14f76722013-01-15 09:04:18 -0800818 new ShutdownDecoderAction(
819 true /* audio */, true /* video */));
Andreas Huberb7c8e912012-11-27 15:02:53 -0800820
Andreas Hubera1f8ab02012-11-30 10:53:22 -0800821 mDeferredActions.push_back(
822 new SimpleAction(&NuPlayer::performReset));
Andreas Huberb58ce9f2011-11-28 16:27:35 -0800823
Andreas Hubera1f8ab02012-11-30 10:53:22 -0800824 processDeferredActions();
Andreas Huber1aef2112011-01-04 14:01:29 -0800825 break;
826 }
827
Andreas Huber43c3e6c2011-01-05 12:17:08 -0800828 case kWhatSeek:
829 {
830 int64_t seekTimeUs;
831 CHECK(msg->findInt64("seekTimeUs", &seekTimeUs));
832
Andreas Hubera1f8ab02012-11-30 10:53:22 -0800833 ALOGV("kWhatSeek seekTimeUs=%lld us", seekTimeUs);
Andreas Huber43c3e6c2011-01-05 12:17:08 -0800834
Andreas Hubera1f8ab02012-11-30 10:53:22 -0800835 mDeferredActions.push_back(
836 new SimpleAction(&NuPlayer::performDecoderFlush));
Andreas Huber43c3e6c2011-01-05 12:17:08 -0800837
Andreas Hubera1f8ab02012-11-30 10:53:22 -0800838 mDeferredActions.push_back(new SeekAction(seekTimeUs));
Andreas Huber43c3e6c2011-01-05 12:17:08 -0800839
Andreas Hubera1f8ab02012-11-30 10:53:22 -0800840 processDeferredActions();
Andreas Huber43c3e6c2011-01-05 12:17:08 -0800841 break;
842 }
843
Andreas Huberb4082222011-01-20 15:23:04 -0800844 case kWhatPause:
845 {
846 CHECK(mRenderer != NULL);
Roger Jönssonfba60da2013-01-21 17:15:45 +0100847 mSource->pause();
Andreas Huberb4082222011-01-20 15:23:04 -0800848 mRenderer->pause();
849 break;
850 }
851
852 case kWhatResume:
853 {
854 CHECK(mRenderer != NULL);
Roger Jönssonfba60da2013-01-21 17:15:45 +0100855 mSource->resume();
Andreas Huberb4082222011-01-20 15:23:04 -0800856 mRenderer->resume();
857 break;
858 }
859
Andreas Huberb5f25f02013-02-05 10:14:26 -0800860 case kWhatSourceNotify:
861 {
Andreas Huber9575c962013-02-05 13:59:56 -0800862 onSourceNotify(msg);
Andreas Huberb5f25f02013-02-05 10:14:26 -0800863 break;
864 }
865
Andreas Huberf9334412010-12-15 15:17:42 -0800866 default:
867 TRESPASS();
868 break;
869 }
870}
871
Andreas Huber3831a062010-12-21 10:22:33 -0800872void NuPlayer::finishFlushIfPossible() {
873 if (mFlushingAudio != FLUSHED && mFlushingAudio != SHUT_DOWN) {
874 return;
875 }
876
877 if (mFlushingVideo != FLUSHED && mFlushingVideo != SHUT_DOWN) {
878 return;
879 }
880
Steve Block3856b092011-10-20 11:56:00 +0100881 ALOGV("both audio and video are flushed now.");
Andreas Huber3831a062010-12-21 10:22:33 -0800882
Andreas Huber6e3d3112011-11-28 12:36:11 -0800883 if (mTimeDiscontinuityPending) {
884 mRenderer->signalTimeDiscontinuity();
885 mTimeDiscontinuityPending = false;
886 }
Andreas Huber3831a062010-12-21 10:22:33 -0800887
Andreas Huber22fc52f2011-01-05 16:24:27 -0800888 if (mAudioDecoder != NULL) {
Andreas Huber3831a062010-12-21 10:22:33 -0800889 mAudioDecoder->signalResume();
890 }
891
Andreas Huber22fc52f2011-01-05 16:24:27 -0800892 if (mVideoDecoder != NULL) {
Andreas Huber3831a062010-12-21 10:22:33 -0800893 mVideoDecoder->signalResume();
894 }
895
896 mFlushingAudio = NONE;
897 mFlushingVideo = NONE;
Andreas Huber3831a062010-12-21 10:22:33 -0800898
Andreas Hubera1f8ab02012-11-30 10:53:22 -0800899 processDeferredActions();
Andreas Huber1aef2112011-01-04 14:01:29 -0800900}
901
902void NuPlayer::postScanSources() {
903 if (mScanSourcesPending) {
904 return;
905 }
906
907 sp<AMessage> msg = new AMessage(kWhatScanSources, id());
908 msg->setInt32("generation", mScanSourcesGeneration);
909 msg->post();
910
911 mScanSourcesPending = true;
912}
913
Andreas Huber5bc087c2010-12-23 10:27:40 -0800914status_t NuPlayer::instantiateDecoder(bool audio, sp<Decoder> *decoder) {
Andreas Huberf9334412010-12-15 15:17:42 -0800915 if (*decoder != NULL) {
916 return OK;
917 }
918
Andreas Huber84066782011-08-16 09:34:26 -0700919 sp<AMessage> format = mSource->getFormat(audio);
Andreas Huberf9334412010-12-15 15:17:42 -0800920
Andreas Huber84066782011-08-16 09:34:26 -0700921 if (format == NULL) {
Andreas Huberf9334412010-12-15 15:17:42 -0800922 return -EWOULDBLOCK;
923 }
924
Andreas Huber3fe62152011-09-16 15:09:22 -0700925 if (!audio) {
Andreas Huber84066782011-08-16 09:34:26 -0700926 AString mime;
927 CHECK(format->findString("mime", &mime));
928 mVideoIsAVC = !strcasecmp(MEDIA_MIMETYPE_VIDEO_AVC, mime.c_str());
Andreas Huber3fe62152011-09-16 15:09:22 -0700929 }
930
Andreas Huberf9334412010-12-15 15:17:42 -0800931 sp<AMessage> notify =
932 new AMessage(audio ? kWhatAudioNotify : kWhatVideoNotify,
933 id());
934
Glenn Kasten11731182011-02-08 17:26:17 -0800935 *decoder = audio ? new Decoder(notify) :
936 new Decoder(notify, mNativeWindow);
Andreas Huberf9334412010-12-15 15:17:42 -0800937 looper()->registerHandler(*decoder);
938
Andreas Huber84066782011-08-16 09:34:26 -0700939 (*decoder)->configure(format);
Andreas Huberf9334412010-12-15 15:17:42 -0800940
Andreas Huberf9334412010-12-15 15:17:42 -0800941 return OK;
942}
943
944status_t NuPlayer::feedDecoderInputData(bool audio, const sp<AMessage> &msg) {
945 sp<AMessage> reply;
946 CHECK(msg->findMessage("reply", &reply));
947
Andreas Huber53df1a42010-12-22 10:03:04 -0800948 if ((audio && IsFlushingState(mFlushingAudio))
949 || (!audio && IsFlushingState(mFlushingVideo))) {
Andreas Huberf9334412010-12-15 15:17:42 -0800950 reply->setInt32("err", INFO_DISCONTINUITY);
951 reply->post();
952 return OK;
953 }
954
955 sp<ABuffer> accessUnit;
Andreas Huberf9334412010-12-15 15:17:42 -0800956
Andreas Huber3fe62152011-09-16 15:09:22 -0700957 bool dropAccessUnit;
958 do {
959 status_t err = mSource->dequeueAccessUnit(audio, &accessUnit);
Andreas Huber5bc087c2010-12-23 10:27:40 -0800960
Andreas Huber3fe62152011-09-16 15:09:22 -0700961 if (err == -EWOULDBLOCK) {
962 return err;
963 } else if (err != OK) {
964 if (err == INFO_DISCONTINUITY) {
965 int32_t type;
966 CHECK(accessUnit->meta()->findInt32("discontinuity", &type));
Andreas Huber53df1a42010-12-22 10:03:04 -0800967
Andreas Huber3fe62152011-09-16 15:09:22 -0700968 bool formatChange =
Andreas Huber6e3d3112011-11-28 12:36:11 -0800969 (audio &&
970 (type & ATSParser::DISCONTINUITY_AUDIO_FORMAT))
971 || (!audio &&
972 (type & ATSParser::DISCONTINUITY_VIDEO_FORMAT));
Andreas Huber53df1a42010-12-22 10:03:04 -0800973
Andreas Huber6e3d3112011-11-28 12:36:11 -0800974 bool timeChange = (type & ATSParser::DISCONTINUITY_TIME) != 0;
975
Steve Blockdf64d152012-01-04 20:05:49 +0000976 ALOGI("%s discontinuity (formatChange=%d, time=%d)",
Andreas Huber6e3d3112011-11-28 12:36:11 -0800977 audio ? "audio" : "video", formatChange, timeChange);
Andreas Huber32f3cef2011-03-02 15:34:46 -0800978
Andreas Huber3fe62152011-09-16 15:09:22 -0700979 if (audio) {
980 mSkipRenderingAudioUntilMediaTimeUs = -1;
981 } else {
982 mSkipRenderingVideoUntilMediaTimeUs = -1;
983 }
Andreas Huber32f3cef2011-03-02 15:34:46 -0800984
Andreas Huber6e3d3112011-11-28 12:36:11 -0800985 if (timeChange) {
986 sp<AMessage> extra;
987 if (accessUnit->meta()->findMessage("extra", &extra)
988 && extra != NULL) {
989 int64_t resumeAtMediaTimeUs;
990 if (extra->findInt64(
991 "resume-at-mediatimeUs", &resumeAtMediaTimeUs)) {
Steve Blockdf64d152012-01-04 20:05:49 +0000992 ALOGI("suppressing rendering of %s until %lld us",
Andreas Huber6e3d3112011-11-28 12:36:11 -0800993 audio ? "audio" : "video", resumeAtMediaTimeUs);
Andreas Huber3fe62152011-09-16 15:09:22 -0700994
Andreas Huber6e3d3112011-11-28 12:36:11 -0800995 if (audio) {
996 mSkipRenderingAudioUntilMediaTimeUs =
997 resumeAtMediaTimeUs;
998 } else {
999 mSkipRenderingVideoUntilMediaTimeUs =
1000 resumeAtMediaTimeUs;
1001 }
Andreas Huber3fe62152011-09-16 15:09:22 -07001002 }
Andreas Huber32f3cef2011-03-02 15:34:46 -08001003 }
1004 }
Andreas Huber3fe62152011-09-16 15:09:22 -07001005
Andreas Huber6e3d3112011-11-28 12:36:11 -08001006 mTimeDiscontinuityPending =
1007 mTimeDiscontinuityPending || timeChange;
1008
1009 if (formatChange || timeChange) {
Andreas Hubera1f8ab02012-11-30 10:53:22 -08001010 if (mFlushingAudio == NONE && mFlushingVideo == NONE) {
1011 // And we'll resume scanning sources once we're done
1012 // flushing.
1013 mDeferredActions.push_front(
1014 new SimpleAction(
1015 &NuPlayer::performScanSources));
1016 }
1017
Andreas Huber6e3d3112011-11-28 12:36:11 -08001018 flushDecoder(audio, formatChange);
1019 } else {
1020 // This stream is unaffected by the discontinuity
1021
1022 if (audio) {
1023 mFlushingAudio = FLUSHED;
1024 } else {
1025 mFlushingVideo = FLUSHED;
1026 }
1027
1028 finishFlushIfPossible();
1029
1030 return -EWOULDBLOCK;
1031 }
Andreas Huber32f3cef2011-03-02 15:34:46 -08001032 }
1033
Andreas Huber3fe62152011-09-16 15:09:22 -07001034 reply->setInt32("err", err);
1035 reply->post();
1036 return OK;
Andreas Huberf9334412010-12-15 15:17:42 -08001037 }
1038
Andreas Huber3fe62152011-09-16 15:09:22 -07001039 if (!audio) {
1040 ++mNumFramesTotal;
1041 }
1042
1043 dropAccessUnit = false;
1044 if (!audio
1045 && mVideoLateByUs > 100000ll
1046 && mVideoIsAVC
1047 && !IsAVCReferenceFrame(accessUnit)) {
1048 dropAccessUnit = true;
1049 ++mNumFramesDropped;
1050 }
1051 } while (dropAccessUnit);
Andreas Huberf9334412010-12-15 15:17:42 -08001052
Steve Block3856b092011-10-20 11:56:00 +01001053 // ALOGV("returned a valid buffer of %s data", audio ? "audio" : "video");
Andreas Huberf9334412010-12-15 15:17:42 -08001054
1055#if 0
1056 int64_t mediaTimeUs;
1057 CHECK(accessUnit->meta()->findInt64("timeUs", &mediaTimeUs));
Steve Block3856b092011-10-20 11:56:00 +01001058 ALOGV("feeding %s input buffer at media time %.2f secs",
Andreas Huberf9334412010-12-15 15:17:42 -08001059 audio ? "audio" : "video",
1060 mediaTimeUs / 1E6);
1061#endif
1062
Andreas Huber2d8bedd2012-02-21 14:38:23 -08001063 reply->setBuffer("buffer", accessUnit);
Andreas Huberf9334412010-12-15 15:17:42 -08001064 reply->post();
1065
1066 return OK;
1067}
1068
1069void NuPlayer::renderBuffer(bool audio, const sp<AMessage> &msg) {
Steve Block3856b092011-10-20 11:56:00 +01001070 // ALOGV("renderBuffer %s", audio ? "audio" : "video");
Andreas Huberf9334412010-12-15 15:17:42 -08001071
1072 sp<AMessage> reply;
1073 CHECK(msg->findMessage("reply", &reply));
1074
Andreas Huber18ac5402011-08-31 15:04:25 -07001075 if (IsFlushingState(audio ? mFlushingAudio : mFlushingVideo)) {
1076 // We're currently attempting to flush the decoder, in order
1077 // to complete this, the decoder wants all its buffers back,
1078 // so we don't want any output buffers it sent us (from before
1079 // we initiated the flush) to be stuck in the renderer's queue.
1080
Steve Block3856b092011-10-20 11:56:00 +01001081 ALOGV("we're still flushing the %s decoder, sending its output buffer"
Andreas Huber18ac5402011-08-31 15:04:25 -07001082 " right back.", audio ? "audio" : "video");
1083
1084 reply->post();
1085 return;
1086 }
1087
Andreas Huber2d8bedd2012-02-21 14:38:23 -08001088 sp<ABuffer> buffer;
1089 CHECK(msg->findBuffer("buffer", &buffer));
Andreas Huberf9334412010-12-15 15:17:42 -08001090
Andreas Huber32f3cef2011-03-02 15:34:46 -08001091 int64_t &skipUntilMediaTimeUs =
1092 audio
1093 ? mSkipRenderingAudioUntilMediaTimeUs
1094 : mSkipRenderingVideoUntilMediaTimeUs;
1095
1096 if (skipUntilMediaTimeUs >= 0) {
1097 int64_t mediaTimeUs;
1098 CHECK(buffer->meta()->findInt64("timeUs", &mediaTimeUs));
1099
1100 if (mediaTimeUs < skipUntilMediaTimeUs) {
Steve Block3856b092011-10-20 11:56:00 +01001101 ALOGV("dropping %s buffer at time %lld as requested.",
Andreas Huber32f3cef2011-03-02 15:34:46 -08001102 audio ? "audio" : "video",
1103 mediaTimeUs);
1104
1105 reply->post();
1106 return;
1107 }
1108
1109 skipUntilMediaTimeUs = -1;
1110 }
1111
Andreas Huberf9334412010-12-15 15:17:42 -08001112 mRenderer->queueBuffer(audio, buffer, reply);
1113}
1114
Chong Zhangdcb89b32013-08-06 09:44:47 -07001115void NuPlayer::notifyListener(int msg, int ext1, int ext2, const Parcel *in) {
Andreas Huber43c3e6c2011-01-05 12:17:08 -08001116 if (mDriver == NULL) {
Andreas Huberf9334412010-12-15 15:17:42 -08001117 return;
1118 }
1119
Andreas Huber43c3e6c2011-01-05 12:17:08 -08001120 sp<NuPlayerDriver> driver = mDriver.promote();
Andreas Huberf9334412010-12-15 15:17:42 -08001121
Andreas Huber43c3e6c2011-01-05 12:17:08 -08001122 if (driver == NULL) {
Andreas Huberf9334412010-12-15 15:17:42 -08001123 return;
1124 }
1125
Chong Zhangdcb89b32013-08-06 09:44:47 -07001126 driver->notifyListener(msg, ext1, ext2, in);
Andreas Huberf9334412010-12-15 15:17:42 -08001127}
1128
Andreas Huber1aef2112011-01-04 14:01:29 -08001129void NuPlayer::flushDecoder(bool audio, bool needShutdown) {
Andreas Huber14f76722013-01-15 09:04:18 -08001130 ALOGV("[%s] flushDecoder needShutdown=%d",
1131 audio ? "audio" : "video", needShutdown);
1132
Andreas Huber6e3d3112011-11-28 12:36:11 -08001133 if ((audio && mAudioDecoder == NULL) || (!audio && mVideoDecoder == NULL)) {
Steve Blockdf64d152012-01-04 20:05:49 +00001134 ALOGI("flushDecoder %s without decoder present",
Andreas Huber6e3d3112011-11-28 12:36:11 -08001135 audio ? "audio" : "video");
1136 }
1137
Andreas Huber1aef2112011-01-04 14:01:29 -08001138 // Make sure we don't continue to scan sources until we finish flushing.
1139 ++mScanSourcesGeneration;
Andreas Huber43c3e6c2011-01-05 12:17:08 -08001140 mScanSourcesPending = false;
Andreas Huber1aef2112011-01-04 14:01:29 -08001141
1142 (audio ? mAudioDecoder : mVideoDecoder)->signalFlush();
1143 mRenderer->flush(audio);
1144
1145 FlushStatus newStatus =
1146 needShutdown ? FLUSHING_DECODER_SHUTDOWN : FLUSHING_DECODER;
1147
1148 if (audio) {
1149 CHECK(mFlushingAudio == NONE
1150 || mFlushingAudio == AWAITING_DISCONTINUITY);
1151
1152 mFlushingAudio = newStatus;
1153
1154 if (mFlushingVideo == NONE) {
1155 mFlushingVideo = (mVideoDecoder != NULL)
1156 ? AWAITING_DISCONTINUITY
1157 : FLUSHED;
1158 }
1159 } else {
1160 CHECK(mFlushingVideo == NONE
1161 || mFlushingVideo == AWAITING_DISCONTINUITY);
1162
1163 mFlushingVideo = newStatus;
1164
1165 if (mFlushingAudio == NONE) {
1166 mFlushingAudio = (mAudioDecoder != NULL)
1167 ? AWAITING_DISCONTINUITY
1168 : FLUSHED;
1169 }
1170 }
1171}
1172
Andreas Huber84066782011-08-16 09:34:26 -07001173sp<AMessage> NuPlayer::Source::getFormat(bool audio) {
1174 sp<MetaData> meta = getFormatMeta(audio);
1175
1176 if (meta == NULL) {
1177 return NULL;
1178 }
1179
1180 sp<AMessage> msg = new AMessage;
1181
1182 if(convertMetaDataToMessage(meta, &msg) == OK) {
1183 return msg;
1184 }
1185 return NULL;
1186}
1187
James Dong0d268a32012-08-31 12:18:27 -07001188status_t NuPlayer::setVideoScalingMode(int32_t mode) {
1189 mVideoScalingMode = mode;
Andreas Huber57a339c2012-12-03 11:18:00 -08001190 if (mNativeWindow != NULL) {
James Dong0d268a32012-08-31 12:18:27 -07001191 status_t ret = native_window_set_scaling_mode(
1192 mNativeWindow->getNativeWindow().get(), mVideoScalingMode);
1193 if (ret != OK) {
1194 ALOGE("Failed to set scaling mode (%d): %s",
1195 -ret, strerror(-ret));
1196 return ret;
1197 }
1198 }
1199 return OK;
1200}
1201
Chong Zhangdcb89b32013-08-06 09:44:47 -07001202status_t NuPlayer::getTrackInfo(Parcel* reply) const {
1203 sp<AMessage> msg = new AMessage(kWhatGetTrackInfo, id());
1204 msg->setPointer("reply", reply);
1205
1206 sp<AMessage> response;
1207 status_t err = msg->postAndAwaitResponse(&response);
1208 return err;
1209}
1210
1211status_t NuPlayer::selectTrack(size_t trackIndex, bool select) {
1212 sp<AMessage> msg = new AMessage(kWhatSelectTrack, id());
1213 msg->setSize("trackIndex", trackIndex);
1214 msg->setInt32("select", select);
1215
1216 sp<AMessage> response;
1217 status_t err = msg->postAndAwaitResponse(&response);
1218
1219 return err;
1220}
1221
Andreas Huberb7c8e912012-11-27 15:02:53 -08001222void NuPlayer::schedulePollDuration() {
1223 sp<AMessage> msg = new AMessage(kWhatPollDuration, id());
1224 msg->setInt32("generation", mPollDurationGeneration);
1225 msg->post();
1226}
1227
1228void NuPlayer::cancelPollDuration() {
1229 ++mPollDurationGeneration;
1230}
1231
Andreas Hubera1f8ab02012-11-30 10:53:22 -08001232void NuPlayer::processDeferredActions() {
1233 while (!mDeferredActions.empty()) {
1234 // We won't execute any deferred actions until we're no longer in
1235 // an intermediate state, i.e. one more more decoders are currently
1236 // flushing or shutting down.
1237
1238 if (mRenderer != NULL) {
1239 // There's an edge case where the renderer owns all output
1240 // buffers and is paused, therefore the decoder will not read
1241 // more input data and will never encounter the matching
1242 // discontinuity. To avoid this, we resume the renderer.
1243
1244 if (mFlushingAudio == AWAITING_DISCONTINUITY
1245 || mFlushingVideo == AWAITING_DISCONTINUITY) {
1246 mRenderer->resume();
1247 }
1248 }
1249
1250 if (mFlushingAudio != NONE || mFlushingVideo != NONE) {
1251 // We're currently flushing, postpone the reset until that's
1252 // completed.
1253
1254 ALOGV("postponing action mFlushingAudio=%d, mFlushingVideo=%d",
1255 mFlushingAudio, mFlushingVideo);
1256
1257 break;
1258 }
1259
1260 sp<Action> action = *mDeferredActions.begin();
1261 mDeferredActions.erase(mDeferredActions.begin());
1262
1263 action->execute(this);
1264 }
1265}
1266
1267void NuPlayer::performSeek(int64_t seekTimeUs) {
1268 ALOGV("performSeek seekTimeUs=%lld us (%.2f secs)",
1269 seekTimeUs,
1270 seekTimeUs / 1E6);
1271
1272 mSource->seekTo(seekTimeUs);
1273
1274 if (mDriver != NULL) {
1275 sp<NuPlayerDriver> driver = mDriver.promote();
1276 if (driver != NULL) {
1277 driver->notifyPosition(seekTimeUs);
1278 driver->notifySeekComplete();
1279 }
1280 }
1281
1282 // everything's flushed, continue playback.
1283}
1284
1285void NuPlayer::performDecoderFlush() {
1286 ALOGV("performDecoderFlush");
1287
Andreas Huberda9740e2013-04-16 10:54:03 -07001288 if (mAudioDecoder == NULL && mVideoDecoder == NULL) {
Andreas Hubera1f8ab02012-11-30 10:53:22 -08001289 return;
1290 }
1291
1292 mTimeDiscontinuityPending = true;
1293
1294 if (mAudioDecoder != NULL) {
1295 flushDecoder(true /* audio */, false /* needShutdown */);
1296 }
1297
1298 if (mVideoDecoder != NULL) {
1299 flushDecoder(false /* audio */, false /* needShutdown */);
1300 }
1301}
1302
Andreas Huber14f76722013-01-15 09:04:18 -08001303void NuPlayer::performDecoderShutdown(bool audio, bool video) {
1304 ALOGV("performDecoderShutdown audio=%d, video=%d", audio, video);
Andreas Hubera1f8ab02012-11-30 10:53:22 -08001305
Andreas Huber14f76722013-01-15 09:04:18 -08001306 if ((!audio || mAudioDecoder == NULL)
1307 && (!video || mVideoDecoder == NULL)) {
Andreas Hubera1f8ab02012-11-30 10:53:22 -08001308 return;
1309 }
1310
1311 mTimeDiscontinuityPending = true;
1312
Andreas Huber14f76722013-01-15 09:04:18 -08001313 if (mFlushingAudio == NONE && (!audio || mAudioDecoder == NULL)) {
1314 mFlushingAudio = FLUSHED;
1315 }
1316
1317 if (mFlushingVideo == NONE && (!video || mVideoDecoder == NULL)) {
1318 mFlushingVideo = FLUSHED;
1319 }
1320
1321 if (audio && mAudioDecoder != NULL) {
Andreas Hubera1f8ab02012-11-30 10:53:22 -08001322 flushDecoder(true /* audio */, true /* needShutdown */);
1323 }
1324
Andreas Huber14f76722013-01-15 09:04:18 -08001325 if (video && mVideoDecoder != NULL) {
Andreas Hubera1f8ab02012-11-30 10:53:22 -08001326 flushDecoder(false /* audio */, true /* needShutdown */);
1327 }
1328}
1329
1330void NuPlayer::performReset() {
1331 ALOGV("performReset");
1332
1333 CHECK(mAudioDecoder == NULL);
1334 CHECK(mVideoDecoder == NULL);
1335
1336 cancelPollDuration();
1337
1338 ++mScanSourcesGeneration;
1339 mScanSourcesPending = false;
1340
1341 mRenderer.clear();
1342
1343 if (mSource != NULL) {
1344 mSource->stop();
Andreas Huberb5f25f02013-02-05 10:14:26 -08001345
1346 looper()->unregisterHandler(mSource->id());
1347
Andreas Hubera1f8ab02012-11-30 10:53:22 -08001348 mSource.clear();
1349 }
1350
1351 if (mDriver != NULL) {
1352 sp<NuPlayerDriver> driver = mDriver.promote();
1353 if (driver != NULL) {
1354 driver->notifyResetComplete();
1355 }
1356 }
Andreas Huber57a339c2012-12-03 11:18:00 -08001357
1358 mStarted = false;
Andreas Hubera1f8ab02012-11-30 10:53:22 -08001359}
1360
1361void NuPlayer::performScanSources() {
1362 ALOGV("performScanSources");
1363
Andreas Huber57a339c2012-12-03 11:18:00 -08001364 if (!mStarted) {
1365 return;
1366 }
1367
Andreas Hubera1f8ab02012-11-30 10:53:22 -08001368 if (mAudioDecoder == NULL || mVideoDecoder == NULL) {
1369 postScanSources();
1370 }
1371}
1372
Andreas Huber57a339c2012-12-03 11:18:00 -08001373void NuPlayer::performSetSurface(const sp<NativeWindowWrapper> &wrapper) {
1374 ALOGV("performSetSurface");
1375
1376 mNativeWindow = wrapper;
1377
1378 // XXX - ignore error from setVideoScalingMode for now
1379 setVideoScalingMode(mVideoScalingMode);
1380
1381 if (mDriver != NULL) {
1382 sp<NuPlayerDriver> driver = mDriver.promote();
1383 if (driver != NULL) {
1384 driver->notifySetSurfaceComplete();
1385 }
1386 }
1387}
1388
Andreas Huber9575c962013-02-05 13:59:56 -08001389void NuPlayer::onSourceNotify(const sp<AMessage> &msg) {
1390 int32_t what;
1391 CHECK(msg->findInt32("what", &what));
1392
1393 switch (what) {
1394 case Source::kWhatPrepared:
1395 {
Andreas Huberb5f28d42013-04-25 15:11:19 -07001396 if (mSource == NULL) {
1397 // This is a stale notification from a source that was
1398 // asynchronously preparing when the client called reset().
1399 // We handled the reset, the source is gone.
1400 break;
1401 }
1402
Andreas Huberec0c5972013-02-05 14:47:13 -08001403 int32_t err;
1404 CHECK(msg->findInt32("err", &err));
1405
Andreas Huber9575c962013-02-05 13:59:56 -08001406 sp<NuPlayerDriver> driver = mDriver.promote();
1407 if (driver != NULL) {
Andreas Huberec0c5972013-02-05 14:47:13 -08001408 driver->notifyPrepareCompleted(err);
Andreas Huber9575c962013-02-05 13:59:56 -08001409 }
Andreas Huber99759402013-04-01 14:28:31 -07001410
1411 int64_t durationUs;
1412 if (mDriver != NULL && mSource->getDuration(&durationUs) == OK) {
1413 sp<NuPlayerDriver> driver = mDriver.promote();
1414 if (driver != NULL) {
1415 driver->notifyDuration(durationUs);
1416 }
1417 }
Andreas Huber9575c962013-02-05 13:59:56 -08001418 break;
1419 }
1420
1421 case Source::kWhatFlagsChanged:
1422 {
1423 uint32_t flags;
1424 CHECK(msg->findInt32("flags", (int32_t *)&flags));
1425
Chong Zhang4b7069d2013-09-11 12:52:43 -07001426 sp<NuPlayerDriver> driver = mDriver.promote();
1427 if (driver != NULL) {
1428 driver->notifyFlagsChanged(flags);
1429 }
1430
Andreas Huber9575c962013-02-05 13:59:56 -08001431 if ((mSourceFlags & Source::FLAG_DYNAMIC_DURATION)
1432 && (!(flags & Source::FLAG_DYNAMIC_DURATION))) {
1433 cancelPollDuration();
1434 } else if (!(mSourceFlags & Source::FLAG_DYNAMIC_DURATION)
1435 && (flags & Source::FLAG_DYNAMIC_DURATION)
1436 && (mAudioDecoder != NULL || mVideoDecoder != NULL)) {
1437 schedulePollDuration();
1438 }
1439
1440 mSourceFlags = flags;
1441 break;
1442 }
1443
1444 case Source::kWhatVideoSizeChanged:
1445 {
1446 int32_t width, height;
1447 CHECK(msg->findInt32("width", &width));
1448 CHECK(msg->findInt32("height", &height));
1449
1450 notifyListener(MEDIA_SET_VIDEO_SIZE, width, height);
1451 break;
1452 }
1453
Roger Jönssonb50e83e2013-01-21 16:26:41 +01001454 case Source::kWhatBufferingStart:
1455 {
1456 notifyListener(MEDIA_INFO, MEDIA_INFO_BUFFERING_START, 0);
1457 break;
1458 }
1459
1460 case Source::kWhatBufferingEnd:
1461 {
1462 notifyListener(MEDIA_INFO, MEDIA_INFO_BUFFERING_END, 0);
1463 break;
1464 }
1465
Chong Zhangdcb89b32013-08-06 09:44:47 -07001466 case Source::kWhatSubtitleData:
1467 {
1468 sp<ABuffer> buffer;
1469 CHECK(msg->findBuffer("buffer", &buffer));
1470
1471 int32_t trackIndex;
1472 int64_t timeUs, durationUs;
1473 CHECK(buffer->meta()->findInt32("trackIndex", &trackIndex));
1474 CHECK(buffer->meta()->findInt64("timeUs", &timeUs));
1475 CHECK(buffer->meta()->findInt64("durationUs", &durationUs));
1476
1477 Parcel in;
1478 in.writeInt32(trackIndex);
1479 in.writeInt64(timeUs);
1480 in.writeInt64(durationUs);
1481 in.writeInt32(buffer->size());
1482 in.writeInt32(buffer->size());
1483 in.write(buffer->data(), buffer->size());
1484
1485 notifyListener(MEDIA_SUBTITLE_DATA, 0, 0, &in);
1486 break;
1487 }
1488
Andreas Huber14f76722013-01-15 09:04:18 -08001489 case Source::kWhatQueueDecoderShutdown:
1490 {
1491 int32_t audio, video;
1492 CHECK(msg->findInt32("audio", &audio));
1493 CHECK(msg->findInt32("video", &video));
1494
1495 sp<AMessage> reply;
1496 CHECK(msg->findMessage("reply", &reply));
1497
1498 queueDecoderShutdown(audio, video, reply);
1499 break;
1500 }
1501
Andreas Huber9575c962013-02-05 13:59:56 -08001502 default:
1503 TRESPASS();
1504 }
1505}
1506
Andreas Huberb5f25f02013-02-05 10:14:26 -08001507////////////////////////////////////////////////////////////////////////////////
1508
Andreas Huber9575c962013-02-05 13:59:56 -08001509void NuPlayer::Source::notifyFlagsChanged(uint32_t flags) {
1510 sp<AMessage> notify = dupNotify();
1511 notify->setInt32("what", kWhatFlagsChanged);
1512 notify->setInt32("flags", flags);
1513 notify->post();
1514}
1515
1516void NuPlayer::Source::notifyVideoSizeChanged(int32_t width, int32_t height) {
1517 sp<AMessage> notify = dupNotify();
1518 notify->setInt32("what", kWhatVideoSizeChanged);
1519 notify->setInt32("width", width);
1520 notify->setInt32("height", height);
1521 notify->post();
1522}
1523
Andreas Huberec0c5972013-02-05 14:47:13 -08001524void NuPlayer::Source::notifyPrepared(status_t err) {
Andreas Huber9575c962013-02-05 13:59:56 -08001525 sp<AMessage> notify = dupNotify();
1526 notify->setInt32("what", kWhatPrepared);
Andreas Huberec0c5972013-02-05 14:47:13 -08001527 notify->setInt32("err", err);
Andreas Huber9575c962013-02-05 13:59:56 -08001528 notify->post();
1529}
1530
Andreas Huberb5f25f02013-02-05 10:14:26 -08001531void NuPlayer::Source::onMessageReceived(const sp<AMessage> &msg) {
1532 TRESPASS();
1533}
1534
Andreas Huber14f76722013-01-15 09:04:18 -08001535void NuPlayer::queueDecoderShutdown(
1536 bool audio, bool video, const sp<AMessage> &reply) {
1537 ALOGI("queueDecoderShutdown audio=%d, video=%d", audio, video);
1538
1539 mDeferredActions.push_back(
1540 new ShutdownDecoderAction(audio, video));
1541
1542 mDeferredActions.push_back(
1543 new SimpleAction(&NuPlayer::performScanSources));
1544
1545 mDeferredActions.push_back(new PostMessageAction(reply));
1546
1547 processDeferredActions();
1548}
1549
Andreas Huberf9334412010-12-15 15:17:42 -08001550} // namespace android