blob: 3669a5bd70e71e119b480e3d806dbf0c2cc88207 [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 Huber5bc087c2010-12-23 10:27:40 -0800215 const char *url, const KeyedVector<String8, String8> *headers) {
216 sp<AMessage> msg = new AMessage(kWhatSetDataSource, id());
Oscar Rydhé7a33b772012-02-20 10:15:48 +0100217 size_t len = strlen(url);
Andreas Huber5bc087c2010-12-23 10:27:40 -0800218
Andreas Huberb5f25f02013-02-05 10:14:26 -0800219 sp<AMessage> notify = new AMessage(kWhatSourceNotify, id());
220
Andreas Huberafed0e12011-09-20 15:39:58 -0700221 sp<Source> source;
222 if (IsHTTPLiveURL(url)) {
Andreas Huberb5f25f02013-02-05 10:14:26 -0800223 source = new HTTPLiveSource(notify, url, headers, mUIDValid, mUID);
Andreas Huberafed0e12011-09-20 15:39:58 -0700224 } else if (!strncasecmp(url, "rtsp://", 7)) {
Andreas Huberb5f25f02013-02-05 10:14:26 -0800225 source = new RTSPSource(notify, url, headers, mUIDValid, mUID);
Oscar Rydhé7a33b772012-02-20 10:15:48 +0100226 } else if ((!strncasecmp(url, "http://", 7)
227 || !strncasecmp(url, "https://", 8))
228 && ((len >= 4 && !strcasecmp(".sdp", &url[len - 4]))
229 || strstr(url, ".sdp?"))) {
230 source = new RTSPSource(notify, url, headers, mUIDValid, mUID, true);
Andreas Huber2bfdd422011-10-11 15:24:07 -0700231 } else {
Andreas Huberb5f25f02013-02-05 10:14:26 -0800232 source = new GenericSource(notify, url, headers, mUIDValid, mUID);
Andreas Huber2bfdd422011-10-11 15:24:07 -0700233 }
234
Andreas Huberafed0e12011-09-20 15:39:58 -0700235 msg->setObject("source", source);
236 msg->post();
237}
238
Andreas Huber9575c962013-02-05 13:59:56 -0800239void NuPlayer::setDataSourceAsync(int fd, int64_t offset, int64_t length) {
Andreas Huberafed0e12011-09-20 15:39:58 -0700240 sp<AMessage> msg = new AMessage(kWhatSetDataSource, id());
241
Andreas Huberb5f25f02013-02-05 10:14:26 -0800242 sp<AMessage> notify = new AMessage(kWhatSourceNotify, id());
243
244 sp<Source> source = new GenericSource(notify, fd, offset, length);
Andreas Huberafed0e12011-09-20 15:39:58 -0700245 msg->setObject("source", source);
Andreas Huberf9334412010-12-15 15:17:42 -0800246 msg->post();
247}
248
Andreas Huber9575c962013-02-05 13:59:56 -0800249void NuPlayer::prepareAsync() {
250 (new AMessage(kWhatPrepare, id()))->post();
251}
252
Andreas Huber57a339c2012-12-03 11:18:00 -0800253void NuPlayer::setVideoSurfaceTextureAsync(
Andy McFadden8ba01022012-12-18 09:46:54 -0800254 const sp<IGraphicBufferProducer> &bufferProducer) {
Glenn Kasten11731182011-02-08 17:26:17 -0800255 sp<AMessage> msg = new AMessage(kWhatSetVideoNativeWindow, id());
Andreas Huber57a339c2012-12-03 11:18:00 -0800256
Andy McFadden8ba01022012-12-18 09:46:54 -0800257 if (bufferProducer == NULL) {
Andreas Huber57a339c2012-12-03 11:18:00 -0800258 msg->setObject("native-window", NULL);
259 } else {
260 msg->setObject(
261 "native-window",
262 new NativeWindowWrapper(
Mathias Agopian1a2952a2013-02-14 17:11:27 -0800263 new Surface(bufferProducer)));
Andreas Huber57a339c2012-12-03 11:18:00 -0800264 }
265
Andreas Huberf9334412010-12-15 15:17:42 -0800266 msg->post();
267}
268
269void NuPlayer::setAudioSink(const sp<MediaPlayerBase::AudioSink> &sink) {
270 sp<AMessage> msg = new AMessage(kWhatSetAudioSink, id());
271 msg->setObject("sink", sink);
272 msg->post();
273}
274
275void NuPlayer::start() {
276 (new AMessage(kWhatStart, id()))->post();
277}
278
Andreas Huber43c3e6c2011-01-05 12:17:08 -0800279void NuPlayer::pause() {
Andreas Huberb4082222011-01-20 15:23:04 -0800280 (new AMessage(kWhatPause, id()))->post();
Andreas Huber43c3e6c2011-01-05 12:17:08 -0800281}
282
283void NuPlayer::resume() {
Andreas Huberb4082222011-01-20 15:23:04 -0800284 (new AMessage(kWhatResume, id()))->post();
Andreas Huber43c3e6c2011-01-05 12:17:08 -0800285}
286
Andreas Huber1aef2112011-01-04 14:01:29 -0800287void NuPlayer::resetAsync() {
288 (new AMessage(kWhatReset, id()))->post();
289}
290
Andreas Huber43c3e6c2011-01-05 12:17:08 -0800291void NuPlayer::seekToAsync(int64_t seekTimeUs) {
292 sp<AMessage> msg = new AMessage(kWhatSeek, id());
293 msg->setInt64("seekTimeUs", seekTimeUs);
294 msg->post();
295}
296
Andreas Huber53df1a42010-12-22 10:03:04 -0800297// static
Andreas Huber1aef2112011-01-04 14:01:29 -0800298bool NuPlayer::IsFlushingState(FlushStatus state, bool *needShutdown) {
Andreas Huber53df1a42010-12-22 10:03:04 -0800299 switch (state) {
300 case FLUSHING_DECODER:
Andreas Huber1aef2112011-01-04 14:01:29 -0800301 if (needShutdown != NULL) {
302 *needShutdown = false;
Andreas Huber53df1a42010-12-22 10:03:04 -0800303 }
304 return true;
305
Andreas Huber1aef2112011-01-04 14:01:29 -0800306 case FLUSHING_DECODER_SHUTDOWN:
307 if (needShutdown != NULL) {
308 *needShutdown = true;
Andreas Huber53df1a42010-12-22 10:03:04 -0800309 }
310 return true;
311
312 default:
313 return false;
314 }
315}
316
Andreas Huberf9334412010-12-15 15:17:42 -0800317void NuPlayer::onMessageReceived(const sp<AMessage> &msg) {
318 switch (msg->what()) {
319 case kWhatSetDataSource:
320 {
Steve Block3856b092011-10-20 11:56:00 +0100321 ALOGV("kWhatSetDataSource");
Andreas Huberf9334412010-12-15 15:17:42 -0800322
323 CHECK(mSource == NULL);
324
Andreas Huber5bc087c2010-12-23 10:27:40 -0800325 sp<RefBase> obj;
326 CHECK(msg->findObject("source", &obj));
Andreas Huberf9334412010-12-15 15:17:42 -0800327
Andreas Huber5bc087c2010-12-23 10:27:40 -0800328 mSource = static_cast<Source *>(obj.get());
Andreas Huberb5f25f02013-02-05 10:14:26 -0800329
330 looper()->registerHandler(mSource);
Andreas Huber9575c962013-02-05 13:59:56 -0800331
332 CHECK(mDriver != NULL);
333 sp<NuPlayerDriver> driver = mDriver.promote();
334 if (driver != NULL) {
335 driver->notifySetDataSourceCompleted(OK);
336 }
337 break;
338 }
339
340 case kWhatPrepare:
341 {
342 mSource->prepareAsync();
Andreas Huberf9334412010-12-15 15:17:42 -0800343 break;
344 }
345
Chong Zhangdcb89b32013-08-06 09:44:47 -0700346 case kWhatGetTrackInfo:
347 {
348 uint32_t replyID;
349 CHECK(msg->senderAwaitsResponse(&replyID));
350
351 status_t err = INVALID_OPERATION;
352 if (mSource != NULL) {
353 Parcel* reply;
354 CHECK(msg->findPointer("reply", (void**)&reply));
355 err = mSource->getTrackInfo(reply);
356 }
357
358 sp<AMessage> response = new AMessage;
359 response->setInt32("err", err);
360
361 response->postReply(replyID);
362 break;
363 }
364
365 case kWhatSelectTrack:
366 {
367 uint32_t replyID;
368 CHECK(msg->senderAwaitsResponse(&replyID));
369
370 status_t err = INVALID_OPERATION;
371 if (mSource != NULL) {
372 size_t trackIndex;
373 int32_t select;
374 CHECK(msg->findSize("trackIndex", &trackIndex));
375 CHECK(msg->findInt32("select", &select));
376 err = mSource->selectTrack(trackIndex, select);
377 }
378
379 sp<AMessage> response = new AMessage;
380 response->setInt32("err", err);
381
382 response->postReply(replyID);
383 break;
384 }
385
Andreas Huberb7c8e912012-11-27 15:02:53 -0800386 case kWhatPollDuration:
387 {
388 int32_t generation;
389 CHECK(msg->findInt32("generation", &generation));
390
391 if (generation != mPollDurationGeneration) {
392 // stale
393 break;
394 }
395
396 int64_t durationUs;
397 if (mDriver != NULL && mSource->getDuration(&durationUs) == OK) {
398 sp<NuPlayerDriver> driver = mDriver.promote();
399 if (driver != NULL) {
400 driver->notifyDuration(durationUs);
401 }
402 }
403
404 msg->post(1000000ll); // poll again in a second.
405 break;
406 }
407
Glenn Kasten11731182011-02-08 17:26:17 -0800408 case kWhatSetVideoNativeWindow:
Andreas Huberf9334412010-12-15 15:17:42 -0800409 {
Steve Block3856b092011-10-20 11:56:00 +0100410 ALOGV("kWhatSetVideoNativeWindow");
Andreas Huberf9334412010-12-15 15:17:42 -0800411
Andreas Huber57a339c2012-12-03 11:18:00 -0800412 mDeferredActions.push_back(
Andreas Huber14f76722013-01-15 09:04:18 -0800413 new ShutdownDecoderAction(
414 false /* audio */, true /* video */));
Andreas Huber57a339c2012-12-03 11:18:00 -0800415
Andreas Huberf9334412010-12-15 15:17:42 -0800416 sp<RefBase> obj;
Glenn Kasten11731182011-02-08 17:26:17 -0800417 CHECK(msg->findObject("native-window", &obj));
Andreas Huberf9334412010-12-15 15:17:42 -0800418
Andreas Huber57a339c2012-12-03 11:18:00 -0800419 mDeferredActions.push_back(
420 new SetSurfaceAction(
421 static_cast<NativeWindowWrapper *>(obj.get())));
James Dong0d268a32012-08-31 12:18:27 -0700422
Andreas Huber57a339c2012-12-03 11:18:00 -0800423 if (obj != NULL) {
424 // If there is a new surface texture, instantiate decoders
425 // again if possible.
426 mDeferredActions.push_back(
427 new SimpleAction(&NuPlayer::performScanSources));
428 }
429
430 processDeferredActions();
Andreas Huberf9334412010-12-15 15:17:42 -0800431 break;
432 }
433
434 case kWhatSetAudioSink:
435 {
Steve Block3856b092011-10-20 11:56:00 +0100436 ALOGV("kWhatSetAudioSink");
Andreas Huberf9334412010-12-15 15:17:42 -0800437
438 sp<RefBase> obj;
439 CHECK(msg->findObject("sink", &obj));
440
441 mAudioSink = static_cast<MediaPlayerBase::AudioSink *>(obj.get());
442 break;
443 }
444
445 case kWhatStart:
446 {
Steve Block3856b092011-10-20 11:56:00 +0100447 ALOGV("kWhatStart");
Andreas Huber43c3e6c2011-01-05 12:17:08 -0800448
Andreas Huber3fe62152011-09-16 15:09:22 -0700449 mVideoIsAVC = false;
Martin Storsjoda38df52013-09-25 16:05:36 +0300450 mNeedsSwRenderer = false;
Andreas Huber1aef2112011-01-04 14:01:29 -0800451 mAudioEOS = false;
452 mVideoEOS = false;
Andreas Huber32f3cef2011-03-02 15:34:46 -0800453 mSkipRenderingAudioUntilMediaTimeUs = -1;
454 mSkipRenderingVideoUntilMediaTimeUs = -1;
Andreas Huber3fe62152011-09-16 15:09:22 -0700455 mVideoLateByUs = 0;
456 mNumFramesTotal = 0;
457 mNumFramesDropped = 0;
Andreas Huber57a339c2012-12-03 11:18:00 -0800458 mStarted = true;
Andreas Huber1aef2112011-01-04 14:01:29 -0800459
Andreas Huber5bc087c2010-12-23 10:27:40 -0800460 mSource->start();
Andreas Huberf9334412010-12-15 15:17:42 -0800461
Andreas Huberd5e56232013-03-12 11:01:43 -0700462 uint32_t flags = 0;
463
464 if (mSource->isRealTime()) {
465 flags |= Renderer::FLAG_REAL_TIME;
466 }
467
Andreas Huberf9334412010-12-15 15:17:42 -0800468 mRenderer = new Renderer(
469 mAudioSink,
Andreas Huberd5e56232013-03-12 11:01:43 -0700470 new AMessage(kWhatRendererNotify, id()),
471 flags);
Andreas Huberf9334412010-12-15 15:17:42 -0800472
473 looper()->registerHandler(mRenderer);
474
Andreas Huber1aef2112011-01-04 14:01:29 -0800475 postScanSources();
Andreas Huberf9334412010-12-15 15:17:42 -0800476 break;
477 }
478
479 case kWhatScanSources:
480 {
Andreas Huber1aef2112011-01-04 14:01:29 -0800481 int32_t generation;
482 CHECK(msg->findInt32("generation", &generation));
483 if (generation != mScanSourcesGeneration) {
484 // Drop obsolete msg.
485 break;
486 }
487
Andreas Huber5bc087c2010-12-23 10:27:40 -0800488 mScanSourcesPending = false;
489
Steve Block3856b092011-10-20 11:56:00 +0100490 ALOGV("scanning sources haveAudio=%d, haveVideo=%d",
Andreas Huber43c3e6c2011-01-05 12:17:08 -0800491 mAudioDecoder != NULL, mVideoDecoder != NULL);
492
Andreas Huberb7c8e912012-11-27 15:02:53 -0800493 bool mHadAnySourcesBefore =
494 (mAudioDecoder != NULL) || (mVideoDecoder != NULL);
495
Haynes Mathew George5d246ef2012-07-09 10:36:57 -0700496 if (mNativeWindow != NULL) {
497 instantiateDecoder(false, &mVideoDecoder);
498 }
Andreas Huberf9334412010-12-15 15:17:42 -0800499
500 if (mAudioSink != NULL) {
Andreas Huber5bc087c2010-12-23 10:27:40 -0800501 instantiateDecoder(true, &mAudioDecoder);
Andreas Huberf9334412010-12-15 15:17:42 -0800502 }
503
Andreas Huberb7c8e912012-11-27 15:02:53 -0800504 if (!mHadAnySourcesBefore
505 && (mAudioDecoder != NULL || mVideoDecoder != NULL)) {
506 // This is the first time we've found anything playable.
507
Andreas Huber9575c962013-02-05 13:59:56 -0800508 if (mSourceFlags & Source::FLAG_DYNAMIC_DURATION) {
Andreas Huberb7c8e912012-11-27 15:02:53 -0800509 schedulePollDuration();
510 }
511 }
512
Andreas Hubereac68ba2011-09-27 12:12:25 -0700513 status_t err;
514 if ((err = mSource->feedMoreTSData()) != OK) {
Andreas Huber1aef2112011-01-04 14:01:29 -0800515 if (mAudioDecoder == NULL && mVideoDecoder == NULL) {
516 // We're not currently decoding anything (no audio or
517 // video tracks found) and we just ran out of input data.
Andreas Hubereac68ba2011-09-27 12:12:25 -0700518
519 if (err == ERROR_END_OF_STREAM) {
520 notifyListener(MEDIA_PLAYBACK_COMPLETE, 0, 0);
521 } else {
522 notifyListener(MEDIA_ERROR, MEDIA_ERROR_UNKNOWN, err);
523 }
Andreas Huber1aef2112011-01-04 14:01:29 -0800524 }
Andreas Huberf9334412010-12-15 15:17:42 -0800525 break;
526 }
527
Andreas Huberfbe9d812012-08-31 14:05:27 -0700528 if ((mAudioDecoder == NULL && mAudioSink != NULL)
529 || (mVideoDecoder == NULL && mNativeWindow != NULL)) {
Andreas Huberf9334412010-12-15 15:17:42 -0800530 msg->post(100000ll);
Andreas Huber5bc087c2010-12-23 10:27:40 -0800531 mScanSourcesPending = true;
Andreas Huberf9334412010-12-15 15:17:42 -0800532 }
533 break;
534 }
535
536 case kWhatVideoNotify:
537 case kWhatAudioNotify:
538 {
539 bool audio = msg->what() == kWhatAudioNotify;
540
541 sp<AMessage> codecRequest;
542 CHECK(msg->findMessage("codec-request", &codecRequest));
543
544 int32_t what;
545 CHECK(codecRequest->findInt32("what", &what));
546
547 if (what == ACodec::kWhatFillThisBuffer) {
548 status_t err = feedDecoderInputData(
549 audio, codecRequest);
550
Andreas Huber5bc087c2010-12-23 10:27:40 -0800551 if (err == -EWOULDBLOCK) {
Andreas Hubereac68ba2011-09-27 12:12:25 -0700552 if (mSource->feedMoreTSData() == OK) {
Andreas Huber1183a4a2011-11-03 11:00:21 -0700553 msg->post(10000ll);
Andreas Huber5bc087c2010-12-23 10:27:40 -0800554 }
Andreas Huberf9334412010-12-15 15:17:42 -0800555 }
556 } else if (what == ACodec::kWhatEOS) {
Andreas Huberdc9bacd2011-09-26 10:53:29 -0700557 int32_t err;
558 CHECK(codecRequest->findInt32("err", &err));
559
560 if (err == ERROR_END_OF_STREAM) {
Steve Block3856b092011-10-20 11:56:00 +0100561 ALOGV("got %s decoder EOS", audio ? "audio" : "video");
Andreas Huberdc9bacd2011-09-26 10:53:29 -0700562 } else {
Steve Block3856b092011-10-20 11:56:00 +0100563 ALOGV("got %s decoder EOS w/ error %d",
Andreas Huberdc9bacd2011-09-26 10:53:29 -0700564 audio ? "audio" : "video",
565 err);
566 }
567
568 mRenderer->queueEOS(audio, err);
Andreas Huberf9334412010-12-15 15:17:42 -0800569 } else if (what == ACodec::kWhatFlushCompleted) {
Andreas Huber1aef2112011-01-04 14:01:29 -0800570 bool needShutdown;
Andreas Huber53df1a42010-12-22 10:03:04 -0800571
Andreas Huberf9334412010-12-15 15:17:42 -0800572 if (audio) {
Andreas Huber1aef2112011-01-04 14:01:29 -0800573 CHECK(IsFlushingState(mFlushingAudio, &needShutdown));
Andreas Huberf9334412010-12-15 15:17:42 -0800574 mFlushingAudio = FLUSHED;
575 } else {
Andreas Huber1aef2112011-01-04 14:01:29 -0800576 CHECK(IsFlushingState(mFlushingVideo, &needShutdown));
Andreas Huberf9334412010-12-15 15:17:42 -0800577 mFlushingVideo = FLUSHED;
Andreas Huber3fe62152011-09-16 15:09:22 -0700578
579 mVideoLateByUs = 0;
Andreas Huberf9334412010-12-15 15:17:42 -0800580 }
581
Steve Block3856b092011-10-20 11:56:00 +0100582 ALOGV("decoder %s flush completed", audio ? "audio" : "video");
Andreas Huberf9334412010-12-15 15:17:42 -0800583
Andreas Huber1aef2112011-01-04 14:01:29 -0800584 if (needShutdown) {
Steve Block3856b092011-10-20 11:56:00 +0100585 ALOGV("initiating %s decoder shutdown",
Andreas Huber53df1a42010-12-22 10:03:04 -0800586 audio ? "audio" : "video");
Andreas Huberf9334412010-12-15 15:17:42 -0800587
Andreas Huber53df1a42010-12-22 10:03:04 -0800588 (audio ? mAudioDecoder : mVideoDecoder)->initiateShutdown();
Andreas Huberf9334412010-12-15 15:17:42 -0800589
Andreas Huber53df1a42010-12-22 10:03:04 -0800590 if (audio) {
591 mFlushingAudio = SHUTTING_DOWN_DECODER;
592 } else {
593 mFlushingVideo = SHUTTING_DOWN_DECODER;
594 }
Andreas Huberf9334412010-12-15 15:17:42 -0800595 }
Andreas Huber3831a062010-12-21 10:22:33 -0800596
597 finishFlushIfPossible();
Andreas Huber2c2814b2010-12-15 17:18:20 -0800598 } else if (what == ACodec::kWhatOutputFormatChanged) {
Andreas Huber31e25082011-01-10 10:38:31 -0800599 if (audio) {
600 int32_t numChannels;
Andreas Huber516dacf2012-12-03 15:20:40 -0800601 CHECK(codecRequest->findInt32(
602 "channel-count", &numChannels));
Andreas Huber2c2814b2010-12-15 17:18:20 -0800603
Andreas Huber31e25082011-01-10 10:38:31 -0800604 int32_t sampleRate;
605 CHECK(codecRequest->findInt32("sample-rate", &sampleRate));
Andreas Huber2c2814b2010-12-15 17:18:20 -0800606
Steve Block3856b092011-10-20 11:56:00 +0100607 ALOGV("Audio output format changed to %d Hz, %d channels",
Andreas Huber31e25082011-01-10 10:38:31 -0800608 sampleRate, numChannels);
Andreas Huber2c2814b2010-12-15 17:18:20 -0800609
Andreas Huber31e25082011-01-10 10:38:31 -0800610 mAudioSink->close();
Eric Laurent1948eb32012-04-13 16:50:19 -0700611
612 audio_output_flags_t flags;
613 int64_t durationUs;
Andreas Huber516dacf2012-12-03 15:20:40 -0800614 // FIXME: we should handle the case where the video decoder
615 // is created after we receive the format change indication.
616 // Current code will just make that we select deep buffer
617 // with video which should not be a problem as it should
Eric Laurent1948eb32012-04-13 16:50:19 -0700618 // not prevent from keeping A/V sync.
619 if (mVideoDecoder == NULL &&
620 mSource->getDuration(&durationUs) == OK &&
Andreas Huber516dacf2012-12-03 15:20:40 -0800621 durationUs
622 > AUDIO_SINK_MIN_DEEP_BUFFER_DURATION_US) {
Eric Laurent1948eb32012-04-13 16:50:19 -0700623 flags = AUDIO_OUTPUT_FLAG_DEEP_BUFFER;
624 } else {
625 flags = AUDIO_OUTPUT_FLAG_NONE;
626 }
627
Andreas Huber98065552012-05-03 11:33:01 -0700628 int32_t channelMask;
629 if (!codecRequest->findInt32("channel-mask", &channelMask)) {
630 channelMask = CHANNEL_MASK_USE_CHANNEL_ORDER;
631 }
632
Andreas Huber078cfcf2011-09-15 12:25:04 -0700633 CHECK_EQ(mAudioSink->open(
634 sampleRate,
635 numChannels,
Andreas Huber98065552012-05-03 11:33:01 -0700636 (audio_channel_mask_t)channelMask,
Andreas Huber078cfcf2011-09-15 12:25:04 -0700637 AUDIO_FORMAT_PCM_16_BIT,
Eric Laurent1948eb32012-04-13 16:50:19 -0700638 8 /* bufferCount */,
639 NULL,
640 NULL,
641 flags),
Andreas Huber078cfcf2011-09-15 12:25:04 -0700642 (status_t)OK);
Andreas Huber31e25082011-01-10 10:38:31 -0800643 mAudioSink->start();
Andreas Huber2c2814b2010-12-15 17:18:20 -0800644
Andreas Huber31e25082011-01-10 10:38:31 -0800645 mRenderer->signalAudioSinkChanged();
646 } else {
647 // video
Andreas Huber3831a062010-12-21 10:22:33 -0800648
Andreas Huber31e25082011-01-10 10:38:31 -0800649 int32_t width, height;
650 CHECK(codecRequest->findInt32("width", &width));
651 CHECK(codecRequest->findInt32("height", &height));
652
653 int32_t cropLeft, cropTop, cropRight, cropBottom;
654 CHECK(codecRequest->findRect(
655 "crop",
656 &cropLeft, &cropTop, &cropRight, &cropBottom));
657
Andreas Huber516dacf2012-12-03 15:20:40 -0800658 int32_t displayWidth = cropRight - cropLeft + 1;
659 int32_t displayHeight = cropBottom - cropTop + 1;
660
Steve Block3856b092011-10-20 11:56:00 +0100661 ALOGV("Video output format changed to %d x %d "
Andreas Hubercb67cd12011-08-26 16:02:19 -0700662 "(crop: %d x %d @ (%d, %d))",
Andreas Huber31e25082011-01-10 10:38:31 -0800663 width, height,
Andreas Huber516dacf2012-12-03 15:20:40 -0800664 displayWidth,
665 displayHeight,
Andreas Hubercb67cd12011-08-26 16:02:19 -0700666 cropLeft, cropTop);
Andreas Huber31e25082011-01-10 10:38:31 -0800667
Andreas Huber516dacf2012-12-03 15:20:40 -0800668 sp<AMessage> videoInputFormat =
669 mSource->getFormat(false /* audio */);
670
671 // Take into account sample aspect ratio if necessary:
672 int32_t sarWidth, sarHeight;
673 if (videoInputFormat->findInt32("sar-width", &sarWidth)
674 && videoInputFormat->findInt32(
675 "sar-height", &sarHeight)) {
676 ALOGV("Sample aspect ratio %d : %d",
677 sarWidth, sarHeight);
678
679 displayWidth = (displayWidth * sarWidth) / sarHeight;
680
681 ALOGV("display dimensions %d x %d",
682 displayWidth, displayHeight);
683 }
684
Andreas Huber31e25082011-01-10 10:38:31 -0800685 notifyListener(
Andreas Huber516dacf2012-12-03 15:20:40 -0800686 MEDIA_SET_VIDEO_SIZE, displayWidth, displayHeight);
Martin Storsjoda38df52013-09-25 16:05:36 +0300687
688 if (mNeedsSwRenderer && mNativeWindow != NULL) {
689 int32_t colorFormat;
690 CHECK(codecRequest->findInt32("color-format", &colorFormat));
691
692 sp<MetaData> meta = new MetaData;
693 meta->setInt32(kKeyWidth, width);
694 meta->setInt32(kKeyHeight, height);
695 meta->setRect(kKeyCropRect, cropLeft, cropTop, cropRight, cropBottom);
696 meta->setInt32(kKeyColorFormat, colorFormat);
697
698 mRenderer->setSoftRenderer(
699 new SoftwareRenderer(mNativeWindow->getNativeWindow(), meta));
700 }
Andreas Huber31e25082011-01-10 10:38:31 -0800701 }
Andreas Huber3831a062010-12-21 10:22:33 -0800702 } else if (what == ACodec::kWhatShutdownCompleted) {
Steve Block3856b092011-10-20 11:56:00 +0100703 ALOGV("%s shutdown completed", audio ? "audio" : "video");
Andreas Huber3831a062010-12-21 10:22:33 -0800704 if (audio) {
705 mAudioDecoder.clear();
706
707 CHECK_EQ((int)mFlushingAudio, (int)SHUTTING_DOWN_DECODER);
708 mFlushingAudio = SHUT_DOWN;
709 } else {
710 mVideoDecoder.clear();
711
712 CHECK_EQ((int)mFlushingVideo, (int)SHUTTING_DOWN_DECODER);
713 mFlushingVideo = SHUT_DOWN;
714 }
715
716 finishFlushIfPossible();
Andreas Huberc92fd242011-08-16 13:48:44 -0700717 } else if (what == ACodec::kWhatError) {
Steve Block29357bc2012-01-06 19:20:56 +0000718 ALOGE("Received error from %s decoder, aborting playback.",
Andreas Huberc92fd242011-08-16 13:48:44 -0700719 audio ? "audio" : "video");
720
721 mRenderer->queueEOS(audio, UNKNOWN_ERROR);
Andreas Huber57788222012-02-21 11:47:18 -0800722 } else if (what == ACodec::kWhatDrainThisBuffer) {
Andreas Huberf9334412010-12-15 15:17:42 -0800723 renderBuffer(audio, codecRequest);
Martin Storsjoda38df52013-09-25 16:05:36 +0300724 } else if (what == ACodec::kWhatComponentAllocated) {
725 if (!audio) {
726 AString name;
727 CHECK(codecRequest->findString("componentName", &name));
728 mNeedsSwRenderer = name.startsWith("OMX.google.");
729 }
730 } else if (what != ACodec::kWhatComponentConfigured
Andreas Hubera1f8ab02012-11-30 10:53:22 -0800731 && what != ACodec::kWhatBuffersAllocated) {
732 ALOGV("Unhandled codec notification %d '%c%c%c%c'.",
733 what,
734 what >> 24,
735 (what >> 16) & 0xff,
736 (what >> 8) & 0xff,
737 what & 0xff);
Andreas Huberf9334412010-12-15 15:17:42 -0800738 }
739
740 break;
741 }
742
743 case kWhatRendererNotify:
744 {
745 int32_t what;
746 CHECK(msg->findInt32("what", &what));
747
748 if (what == Renderer::kWhatEOS) {
749 int32_t audio;
750 CHECK(msg->findInt32("audio", &audio));
751
Andreas Huberc92fd242011-08-16 13:48:44 -0700752 int32_t finalResult;
753 CHECK(msg->findInt32("finalResult", &finalResult));
754
Andreas Huberf9334412010-12-15 15:17:42 -0800755 if (audio) {
756 mAudioEOS = true;
757 } else {
758 mVideoEOS = true;
759 }
760
Andreas Huberc92fd242011-08-16 13:48:44 -0700761 if (finalResult == ERROR_END_OF_STREAM) {
Steve Block3856b092011-10-20 11:56:00 +0100762 ALOGV("reached %s EOS", audio ? "audio" : "video");
Andreas Huberc92fd242011-08-16 13:48:44 -0700763 } else {
Steve Block29357bc2012-01-06 19:20:56 +0000764 ALOGE("%s track encountered an error (%d)",
Andreas Huberc92fd242011-08-16 13:48:44 -0700765 audio ? "audio" : "video", finalResult);
766
767 notifyListener(
768 MEDIA_ERROR, MEDIA_ERROR_UNKNOWN, finalResult);
769 }
Andreas Huberf9334412010-12-15 15:17:42 -0800770
771 if ((mAudioEOS || mAudioDecoder == NULL)
772 && (mVideoEOS || mVideoDecoder == NULL)) {
773 notifyListener(MEDIA_PLAYBACK_COMPLETE, 0, 0);
774 }
Andreas Huber43c3e6c2011-01-05 12:17:08 -0800775 } else if (what == Renderer::kWhatPosition) {
776 int64_t positionUs;
777 CHECK(msg->findInt64("positionUs", &positionUs));
778
Andreas Huber3fe62152011-09-16 15:09:22 -0700779 CHECK(msg->findInt64("videoLateByUs", &mVideoLateByUs));
780
Andreas Huber43c3e6c2011-01-05 12:17:08 -0800781 if (mDriver != NULL) {
782 sp<NuPlayerDriver> driver = mDriver.promote();
783 if (driver != NULL) {
784 driver->notifyPosition(positionUs);
Andreas Huber3fe62152011-09-16 15:09:22 -0700785
786 driver->notifyFrameStats(
787 mNumFramesTotal, mNumFramesDropped);
Andreas Huber43c3e6c2011-01-05 12:17:08 -0800788 }
789 }
Andreas Huber3fe62152011-09-16 15:09:22 -0700790 } else if (what == Renderer::kWhatFlushComplete) {
Andreas Huberf9334412010-12-15 15:17:42 -0800791 int32_t audio;
792 CHECK(msg->findInt32("audio", &audio));
793
Steve Block3856b092011-10-20 11:56:00 +0100794 ALOGV("renderer %s flush completed.", audio ? "audio" : "video");
James Dongf57b4ea2012-07-20 13:38:36 -0700795 } else if (what == Renderer::kWhatVideoRenderingStart) {
796 notifyListener(MEDIA_INFO, MEDIA_INFO_RENDERING_START, 0);
Lajos Molnarcbaffcf2013-08-14 18:30:38 -0700797 } else if (what == Renderer::kWhatMediaRenderingStart) {
798 ALOGV("media rendering started");
799 notifyListener(MEDIA_STARTED, 0, 0);
Andreas Huberf9334412010-12-15 15:17:42 -0800800 }
801 break;
802 }
803
804 case kWhatMoreDataQueued:
805 {
806 break;
807 }
808
Andreas Huber1aef2112011-01-04 14:01:29 -0800809 case kWhatReset:
810 {
Steve Block3856b092011-10-20 11:56:00 +0100811 ALOGV("kWhatReset");
Andreas Huber1aef2112011-01-04 14:01:29 -0800812
Andreas Hubera1f8ab02012-11-30 10:53:22 -0800813 mDeferredActions.push_back(
Andreas Huber14f76722013-01-15 09:04:18 -0800814 new ShutdownDecoderAction(
815 true /* audio */, true /* video */));
Andreas Huberb7c8e912012-11-27 15:02:53 -0800816
Andreas Hubera1f8ab02012-11-30 10:53:22 -0800817 mDeferredActions.push_back(
818 new SimpleAction(&NuPlayer::performReset));
Andreas Huberb58ce9f2011-11-28 16:27:35 -0800819
Andreas Hubera1f8ab02012-11-30 10:53:22 -0800820 processDeferredActions();
Andreas Huber1aef2112011-01-04 14:01:29 -0800821 break;
822 }
823
Andreas Huber43c3e6c2011-01-05 12:17:08 -0800824 case kWhatSeek:
825 {
826 int64_t seekTimeUs;
827 CHECK(msg->findInt64("seekTimeUs", &seekTimeUs));
828
Andreas Hubera1f8ab02012-11-30 10:53:22 -0800829 ALOGV("kWhatSeek seekTimeUs=%lld us", seekTimeUs);
Andreas Huber43c3e6c2011-01-05 12:17:08 -0800830
Andreas Hubera1f8ab02012-11-30 10:53:22 -0800831 mDeferredActions.push_back(
832 new SimpleAction(&NuPlayer::performDecoderFlush));
Andreas Huber43c3e6c2011-01-05 12:17:08 -0800833
Andreas Hubera1f8ab02012-11-30 10:53:22 -0800834 mDeferredActions.push_back(new SeekAction(seekTimeUs));
Andreas Huber43c3e6c2011-01-05 12:17:08 -0800835
Andreas Hubera1f8ab02012-11-30 10:53:22 -0800836 processDeferredActions();
Andreas Huber43c3e6c2011-01-05 12:17:08 -0800837 break;
838 }
839
Andreas Huberb4082222011-01-20 15:23:04 -0800840 case kWhatPause:
841 {
842 CHECK(mRenderer != NULL);
Roger Jönssonfba60da2013-01-21 17:15:45 +0100843 mSource->pause();
Andreas Huberb4082222011-01-20 15:23:04 -0800844 mRenderer->pause();
845 break;
846 }
847
848 case kWhatResume:
849 {
850 CHECK(mRenderer != NULL);
Roger Jönssonfba60da2013-01-21 17:15:45 +0100851 mSource->resume();
Andreas Huberb4082222011-01-20 15:23:04 -0800852 mRenderer->resume();
853 break;
854 }
855
Andreas Huberb5f25f02013-02-05 10:14:26 -0800856 case kWhatSourceNotify:
857 {
Andreas Huber9575c962013-02-05 13:59:56 -0800858 onSourceNotify(msg);
Andreas Huberb5f25f02013-02-05 10:14:26 -0800859 break;
860 }
861
Andreas Huberf9334412010-12-15 15:17:42 -0800862 default:
863 TRESPASS();
864 break;
865 }
866}
867
Andreas Huber3831a062010-12-21 10:22:33 -0800868void NuPlayer::finishFlushIfPossible() {
869 if (mFlushingAudio != FLUSHED && mFlushingAudio != SHUT_DOWN) {
870 return;
871 }
872
873 if (mFlushingVideo != FLUSHED && mFlushingVideo != SHUT_DOWN) {
874 return;
875 }
876
Steve Block3856b092011-10-20 11:56:00 +0100877 ALOGV("both audio and video are flushed now.");
Andreas Huber3831a062010-12-21 10:22:33 -0800878
Andreas Huber6e3d3112011-11-28 12:36:11 -0800879 if (mTimeDiscontinuityPending) {
880 mRenderer->signalTimeDiscontinuity();
881 mTimeDiscontinuityPending = false;
882 }
Andreas Huber3831a062010-12-21 10:22:33 -0800883
Andreas Huber22fc52f2011-01-05 16:24:27 -0800884 if (mAudioDecoder != NULL) {
Andreas Huber3831a062010-12-21 10:22:33 -0800885 mAudioDecoder->signalResume();
886 }
887
Andreas Huber22fc52f2011-01-05 16:24:27 -0800888 if (mVideoDecoder != NULL) {
Andreas Huber3831a062010-12-21 10:22:33 -0800889 mVideoDecoder->signalResume();
890 }
891
892 mFlushingAudio = NONE;
893 mFlushingVideo = NONE;
Andreas Huber3831a062010-12-21 10:22:33 -0800894
Andreas Hubera1f8ab02012-11-30 10:53:22 -0800895 processDeferredActions();
Andreas Huber1aef2112011-01-04 14:01:29 -0800896}
897
898void NuPlayer::postScanSources() {
899 if (mScanSourcesPending) {
900 return;
901 }
902
903 sp<AMessage> msg = new AMessage(kWhatScanSources, id());
904 msg->setInt32("generation", mScanSourcesGeneration);
905 msg->post();
906
907 mScanSourcesPending = true;
908}
909
Andreas Huber5bc087c2010-12-23 10:27:40 -0800910status_t NuPlayer::instantiateDecoder(bool audio, sp<Decoder> *decoder) {
Andreas Huberf9334412010-12-15 15:17:42 -0800911 if (*decoder != NULL) {
912 return OK;
913 }
914
Andreas Huber84066782011-08-16 09:34:26 -0700915 sp<AMessage> format = mSource->getFormat(audio);
Andreas Huberf9334412010-12-15 15:17:42 -0800916
Andreas Huber84066782011-08-16 09:34:26 -0700917 if (format == NULL) {
Andreas Huberf9334412010-12-15 15:17:42 -0800918 return -EWOULDBLOCK;
919 }
920
Andreas Huber3fe62152011-09-16 15:09:22 -0700921 if (!audio) {
Andreas Huber84066782011-08-16 09:34:26 -0700922 AString mime;
923 CHECK(format->findString("mime", &mime));
924 mVideoIsAVC = !strcasecmp(MEDIA_MIMETYPE_VIDEO_AVC, mime.c_str());
Andreas Huber3fe62152011-09-16 15:09:22 -0700925 }
926
Andreas Huberf9334412010-12-15 15:17:42 -0800927 sp<AMessage> notify =
928 new AMessage(audio ? kWhatAudioNotify : kWhatVideoNotify,
929 id());
930
Glenn Kasten11731182011-02-08 17:26:17 -0800931 *decoder = audio ? new Decoder(notify) :
932 new Decoder(notify, mNativeWindow);
Andreas Huberf9334412010-12-15 15:17:42 -0800933 looper()->registerHandler(*decoder);
934
Andreas Huber84066782011-08-16 09:34:26 -0700935 (*decoder)->configure(format);
Andreas Huberf9334412010-12-15 15:17:42 -0800936
Andreas Huberf9334412010-12-15 15:17:42 -0800937 return OK;
938}
939
940status_t NuPlayer::feedDecoderInputData(bool audio, const sp<AMessage> &msg) {
941 sp<AMessage> reply;
942 CHECK(msg->findMessage("reply", &reply));
943
Andreas Huber53df1a42010-12-22 10:03:04 -0800944 if ((audio && IsFlushingState(mFlushingAudio))
945 || (!audio && IsFlushingState(mFlushingVideo))) {
Andreas Huberf9334412010-12-15 15:17:42 -0800946 reply->setInt32("err", INFO_DISCONTINUITY);
947 reply->post();
948 return OK;
949 }
950
951 sp<ABuffer> accessUnit;
Andreas Huberf9334412010-12-15 15:17:42 -0800952
Andreas Huber3fe62152011-09-16 15:09:22 -0700953 bool dropAccessUnit;
954 do {
955 status_t err = mSource->dequeueAccessUnit(audio, &accessUnit);
Andreas Huber5bc087c2010-12-23 10:27:40 -0800956
Andreas Huber3fe62152011-09-16 15:09:22 -0700957 if (err == -EWOULDBLOCK) {
958 return err;
959 } else if (err != OK) {
960 if (err == INFO_DISCONTINUITY) {
961 int32_t type;
962 CHECK(accessUnit->meta()->findInt32("discontinuity", &type));
Andreas Huber53df1a42010-12-22 10:03:04 -0800963
Andreas Huber3fe62152011-09-16 15:09:22 -0700964 bool formatChange =
Andreas Huber6e3d3112011-11-28 12:36:11 -0800965 (audio &&
966 (type & ATSParser::DISCONTINUITY_AUDIO_FORMAT))
967 || (!audio &&
968 (type & ATSParser::DISCONTINUITY_VIDEO_FORMAT));
Andreas Huber53df1a42010-12-22 10:03:04 -0800969
Andreas Huber6e3d3112011-11-28 12:36:11 -0800970 bool timeChange = (type & ATSParser::DISCONTINUITY_TIME) != 0;
971
Steve Blockdf64d152012-01-04 20:05:49 +0000972 ALOGI("%s discontinuity (formatChange=%d, time=%d)",
Andreas Huber6e3d3112011-11-28 12:36:11 -0800973 audio ? "audio" : "video", formatChange, timeChange);
Andreas Huber32f3cef2011-03-02 15:34:46 -0800974
Andreas Huber3fe62152011-09-16 15:09:22 -0700975 if (audio) {
976 mSkipRenderingAudioUntilMediaTimeUs = -1;
977 } else {
978 mSkipRenderingVideoUntilMediaTimeUs = -1;
979 }
Andreas Huber32f3cef2011-03-02 15:34:46 -0800980
Andreas Huber6e3d3112011-11-28 12:36:11 -0800981 if (timeChange) {
982 sp<AMessage> extra;
983 if (accessUnit->meta()->findMessage("extra", &extra)
984 && extra != NULL) {
985 int64_t resumeAtMediaTimeUs;
986 if (extra->findInt64(
987 "resume-at-mediatimeUs", &resumeAtMediaTimeUs)) {
Steve Blockdf64d152012-01-04 20:05:49 +0000988 ALOGI("suppressing rendering of %s until %lld us",
Andreas Huber6e3d3112011-11-28 12:36:11 -0800989 audio ? "audio" : "video", resumeAtMediaTimeUs);
Andreas Huber3fe62152011-09-16 15:09:22 -0700990
Andreas Huber6e3d3112011-11-28 12:36:11 -0800991 if (audio) {
992 mSkipRenderingAudioUntilMediaTimeUs =
993 resumeAtMediaTimeUs;
994 } else {
995 mSkipRenderingVideoUntilMediaTimeUs =
996 resumeAtMediaTimeUs;
997 }
Andreas Huber3fe62152011-09-16 15:09:22 -0700998 }
Andreas Huber32f3cef2011-03-02 15:34:46 -0800999 }
1000 }
Andreas Huber3fe62152011-09-16 15:09:22 -07001001
Andreas Huber6e3d3112011-11-28 12:36:11 -08001002 mTimeDiscontinuityPending =
1003 mTimeDiscontinuityPending || timeChange;
1004
1005 if (formatChange || timeChange) {
Andreas Hubera1f8ab02012-11-30 10:53:22 -08001006 if (mFlushingAudio == NONE && mFlushingVideo == NONE) {
1007 // And we'll resume scanning sources once we're done
1008 // flushing.
1009 mDeferredActions.push_front(
1010 new SimpleAction(
1011 &NuPlayer::performScanSources));
1012 }
1013
Andreas Huber6e3d3112011-11-28 12:36:11 -08001014 flushDecoder(audio, formatChange);
1015 } else {
1016 // This stream is unaffected by the discontinuity
1017
1018 if (audio) {
1019 mFlushingAudio = FLUSHED;
1020 } else {
1021 mFlushingVideo = FLUSHED;
1022 }
1023
1024 finishFlushIfPossible();
1025
1026 return -EWOULDBLOCK;
1027 }
Andreas Huber32f3cef2011-03-02 15:34:46 -08001028 }
1029
Andreas Huber3fe62152011-09-16 15:09:22 -07001030 reply->setInt32("err", err);
1031 reply->post();
1032 return OK;
Andreas Huberf9334412010-12-15 15:17:42 -08001033 }
1034
Andreas Huber3fe62152011-09-16 15:09:22 -07001035 if (!audio) {
1036 ++mNumFramesTotal;
1037 }
1038
1039 dropAccessUnit = false;
1040 if (!audio
1041 && mVideoLateByUs > 100000ll
1042 && mVideoIsAVC
1043 && !IsAVCReferenceFrame(accessUnit)) {
1044 dropAccessUnit = true;
1045 ++mNumFramesDropped;
1046 }
1047 } while (dropAccessUnit);
Andreas Huberf9334412010-12-15 15:17:42 -08001048
Steve Block3856b092011-10-20 11:56:00 +01001049 // ALOGV("returned a valid buffer of %s data", audio ? "audio" : "video");
Andreas Huberf9334412010-12-15 15:17:42 -08001050
1051#if 0
1052 int64_t mediaTimeUs;
1053 CHECK(accessUnit->meta()->findInt64("timeUs", &mediaTimeUs));
Steve Block3856b092011-10-20 11:56:00 +01001054 ALOGV("feeding %s input buffer at media time %.2f secs",
Andreas Huberf9334412010-12-15 15:17:42 -08001055 audio ? "audio" : "video",
1056 mediaTimeUs / 1E6);
1057#endif
1058
Andreas Huber2d8bedd2012-02-21 14:38:23 -08001059 reply->setBuffer("buffer", accessUnit);
Andreas Huberf9334412010-12-15 15:17:42 -08001060 reply->post();
1061
1062 return OK;
1063}
1064
1065void NuPlayer::renderBuffer(bool audio, const sp<AMessage> &msg) {
Steve Block3856b092011-10-20 11:56:00 +01001066 // ALOGV("renderBuffer %s", audio ? "audio" : "video");
Andreas Huberf9334412010-12-15 15:17:42 -08001067
1068 sp<AMessage> reply;
1069 CHECK(msg->findMessage("reply", &reply));
1070
Andreas Huber18ac5402011-08-31 15:04:25 -07001071 if (IsFlushingState(audio ? mFlushingAudio : mFlushingVideo)) {
1072 // We're currently attempting to flush the decoder, in order
1073 // to complete this, the decoder wants all its buffers back,
1074 // so we don't want any output buffers it sent us (from before
1075 // we initiated the flush) to be stuck in the renderer's queue.
1076
Steve Block3856b092011-10-20 11:56:00 +01001077 ALOGV("we're still flushing the %s decoder, sending its output buffer"
Andreas Huber18ac5402011-08-31 15:04:25 -07001078 " right back.", audio ? "audio" : "video");
1079
1080 reply->post();
1081 return;
1082 }
1083
Andreas Huber2d8bedd2012-02-21 14:38:23 -08001084 sp<ABuffer> buffer;
1085 CHECK(msg->findBuffer("buffer", &buffer));
Andreas Huberf9334412010-12-15 15:17:42 -08001086
Andreas Huber32f3cef2011-03-02 15:34:46 -08001087 int64_t &skipUntilMediaTimeUs =
1088 audio
1089 ? mSkipRenderingAudioUntilMediaTimeUs
1090 : mSkipRenderingVideoUntilMediaTimeUs;
1091
1092 if (skipUntilMediaTimeUs >= 0) {
1093 int64_t mediaTimeUs;
1094 CHECK(buffer->meta()->findInt64("timeUs", &mediaTimeUs));
1095
1096 if (mediaTimeUs < skipUntilMediaTimeUs) {
Steve Block3856b092011-10-20 11:56:00 +01001097 ALOGV("dropping %s buffer at time %lld as requested.",
Andreas Huber32f3cef2011-03-02 15:34:46 -08001098 audio ? "audio" : "video",
1099 mediaTimeUs);
1100
1101 reply->post();
1102 return;
1103 }
1104
1105 skipUntilMediaTimeUs = -1;
1106 }
1107
Andreas Huberf9334412010-12-15 15:17:42 -08001108 mRenderer->queueBuffer(audio, buffer, reply);
1109}
1110
Chong Zhangdcb89b32013-08-06 09:44:47 -07001111void NuPlayer::notifyListener(int msg, int ext1, int ext2, const Parcel *in) {
Andreas Huber43c3e6c2011-01-05 12:17:08 -08001112 if (mDriver == NULL) {
Andreas Huberf9334412010-12-15 15:17:42 -08001113 return;
1114 }
1115
Andreas Huber43c3e6c2011-01-05 12:17:08 -08001116 sp<NuPlayerDriver> driver = mDriver.promote();
Andreas Huberf9334412010-12-15 15:17:42 -08001117
Andreas Huber43c3e6c2011-01-05 12:17:08 -08001118 if (driver == NULL) {
Andreas Huberf9334412010-12-15 15:17:42 -08001119 return;
1120 }
1121
Chong Zhangdcb89b32013-08-06 09:44:47 -07001122 driver->notifyListener(msg, ext1, ext2, in);
Andreas Huberf9334412010-12-15 15:17:42 -08001123}
1124
Andreas Huber1aef2112011-01-04 14:01:29 -08001125void NuPlayer::flushDecoder(bool audio, bool needShutdown) {
Andreas Huber14f76722013-01-15 09:04:18 -08001126 ALOGV("[%s] flushDecoder needShutdown=%d",
1127 audio ? "audio" : "video", needShutdown);
1128
Andreas Huber6e3d3112011-11-28 12:36:11 -08001129 if ((audio && mAudioDecoder == NULL) || (!audio && mVideoDecoder == NULL)) {
Steve Blockdf64d152012-01-04 20:05:49 +00001130 ALOGI("flushDecoder %s without decoder present",
Andreas Huber6e3d3112011-11-28 12:36:11 -08001131 audio ? "audio" : "video");
1132 }
1133
Andreas Huber1aef2112011-01-04 14:01:29 -08001134 // Make sure we don't continue to scan sources until we finish flushing.
1135 ++mScanSourcesGeneration;
Andreas Huber43c3e6c2011-01-05 12:17:08 -08001136 mScanSourcesPending = false;
Andreas Huber1aef2112011-01-04 14:01:29 -08001137
1138 (audio ? mAudioDecoder : mVideoDecoder)->signalFlush();
1139 mRenderer->flush(audio);
1140
1141 FlushStatus newStatus =
1142 needShutdown ? FLUSHING_DECODER_SHUTDOWN : FLUSHING_DECODER;
1143
1144 if (audio) {
1145 CHECK(mFlushingAudio == NONE
1146 || mFlushingAudio == AWAITING_DISCONTINUITY);
1147
1148 mFlushingAudio = newStatus;
1149
1150 if (mFlushingVideo == NONE) {
1151 mFlushingVideo = (mVideoDecoder != NULL)
1152 ? AWAITING_DISCONTINUITY
1153 : FLUSHED;
1154 }
1155 } else {
1156 CHECK(mFlushingVideo == NONE
1157 || mFlushingVideo == AWAITING_DISCONTINUITY);
1158
1159 mFlushingVideo = newStatus;
1160
1161 if (mFlushingAudio == NONE) {
1162 mFlushingAudio = (mAudioDecoder != NULL)
1163 ? AWAITING_DISCONTINUITY
1164 : FLUSHED;
1165 }
1166 }
1167}
1168
Andreas Huber84066782011-08-16 09:34:26 -07001169sp<AMessage> NuPlayer::Source::getFormat(bool audio) {
1170 sp<MetaData> meta = getFormatMeta(audio);
1171
1172 if (meta == NULL) {
1173 return NULL;
1174 }
1175
1176 sp<AMessage> msg = new AMessage;
1177
1178 if(convertMetaDataToMessage(meta, &msg) == OK) {
1179 return msg;
1180 }
1181 return NULL;
1182}
1183
James Dong0d268a32012-08-31 12:18:27 -07001184status_t NuPlayer::setVideoScalingMode(int32_t mode) {
1185 mVideoScalingMode = mode;
Andreas Huber57a339c2012-12-03 11:18:00 -08001186 if (mNativeWindow != NULL) {
James Dong0d268a32012-08-31 12:18:27 -07001187 status_t ret = native_window_set_scaling_mode(
1188 mNativeWindow->getNativeWindow().get(), mVideoScalingMode);
1189 if (ret != OK) {
1190 ALOGE("Failed to set scaling mode (%d): %s",
1191 -ret, strerror(-ret));
1192 return ret;
1193 }
1194 }
1195 return OK;
1196}
1197
Chong Zhangdcb89b32013-08-06 09:44:47 -07001198status_t NuPlayer::getTrackInfo(Parcel* reply) const {
1199 sp<AMessage> msg = new AMessage(kWhatGetTrackInfo, id());
1200 msg->setPointer("reply", reply);
1201
1202 sp<AMessage> response;
1203 status_t err = msg->postAndAwaitResponse(&response);
1204 return err;
1205}
1206
1207status_t NuPlayer::selectTrack(size_t trackIndex, bool select) {
1208 sp<AMessage> msg = new AMessage(kWhatSelectTrack, id());
1209 msg->setSize("trackIndex", trackIndex);
1210 msg->setInt32("select", select);
1211
1212 sp<AMessage> response;
1213 status_t err = msg->postAndAwaitResponse(&response);
1214
1215 return err;
1216}
1217
Andreas Huberb7c8e912012-11-27 15:02:53 -08001218void NuPlayer::schedulePollDuration() {
1219 sp<AMessage> msg = new AMessage(kWhatPollDuration, id());
1220 msg->setInt32("generation", mPollDurationGeneration);
1221 msg->post();
1222}
1223
1224void NuPlayer::cancelPollDuration() {
1225 ++mPollDurationGeneration;
1226}
1227
Andreas Hubera1f8ab02012-11-30 10:53:22 -08001228void NuPlayer::processDeferredActions() {
1229 while (!mDeferredActions.empty()) {
1230 // We won't execute any deferred actions until we're no longer in
1231 // an intermediate state, i.e. one more more decoders are currently
1232 // flushing or shutting down.
1233
1234 if (mRenderer != NULL) {
1235 // There's an edge case where the renderer owns all output
1236 // buffers and is paused, therefore the decoder will not read
1237 // more input data and will never encounter the matching
1238 // discontinuity. To avoid this, we resume the renderer.
1239
1240 if (mFlushingAudio == AWAITING_DISCONTINUITY
1241 || mFlushingVideo == AWAITING_DISCONTINUITY) {
1242 mRenderer->resume();
1243 }
1244 }
1245
1246 if (mFlushingAudio != NONE || mFlushingVideo != NONE) {
1247 // We're currently flushing, postpone the reset until that's
1248 // completed.
1249
1250 ALOGV("postponing action mFlushingAudio=%d, mFlushingVideo=%d",
1251 mFlushingAudio, mFlushingVideo);
1252
1253 break;
1254 }
1255
1256 sp<Action> action = *mDeferredActions.begin();
1257 mDeferredActions.erase(mDeferredActions.begin());
1258
1259 action->execute(this);
1260 }
1261}
1262
1263void NuPlayer::performSeek(int64_t seekTimeUs) {
1264 ALOGV("performSeek seekTimeUs=%lld us (%.2f secs)",
1265 seekTimeUs,
1266 seekTimeUs / 1E6);
1267
1268 mSource->seekTo(seekTimeUs);
1269
1270 if (mDriver != NULL) {
1271 sp<NuPlayerDriver> driver = mDriver.promote();
1272 if (driver != NULL) {
1273 driver->notifyPosition(seekTimeUs);
1274 driver->notifySeekComplete();
1275 }
1276 }
1277
1278 // everything's flushed, continue playback.
1279}
1280
1281void NuPlayer::performDecoderFlush() {
1282 ALOGV("performDecoderFlush");
1283
Andreas Huberda9740e2013-04-16 10:54:03 -07001284 if (mAudioDecoder == NULL && mVideoDecoder == NULL) {
Andreas Hubera1f8ab02012-11-30 10:53:22 -08001285 return;
1286 }
1287
1288 mTimeDiscontinuityPending = true;
1289
1290 if (mAudioDecoder != NULL) {
1291 flushDecoder(true /* audio */, false /* needShutdown */);
1292 }
1293
1294 if (mVideoDecoder != NULL) {
1295 flushDecoder(false /* audio */, false /* needShutdown */);
1296 }
1297}
1298
Andreas Huber14f76722013-01-15 09:04:18 -08001299void NuPlayer::performDecoderShutdown(bool audio, bool video) {
1300 ALOGV("performDecoderShutdown audio=%d, video=%d", audio, video);
Andreas Hubera1f8ab02012-11-30 10:53:22 -08001301
Andreas Huber14f76722013-01-15 09:04:18 -08001302 if ((!audio || mAudioDecoder == NULL)
1303 && (!video || mVideoDecoder == NULL)) {
Andreas Hubera1f8ab02012-11-30 10:53:22 -08001304 return;
1305 }
1306
1307 mTimeDiscontinuityPending = true;
1308
Andreas Huber14f76722013-01-15 09:04:18 -08001309 if (mFlushingAudio == NONE && (!audio || mAudioDecoder == NULL)) {
1310 mFlushingAudio = FLUSHED;
1311 }
1312
1313 if (mFlushingVideo == NONE && (!video || mVideoDecoder == NULL)) {
1314 mFlushingVideo = FLUSHED;
1315 }
1316
1317 if (audio && mAudioDecoder != NULL) {
Andreas Hubera1f8ab02012-11-30 10:53:22 -08001318 flushDecoder(true /* audio */, true /* needShutdown */);
1319 }
1320
Andreas Huber14f76722013-01-15 09:04:18 -08001321 if (video && mVideoDecoder != NULL) {
Andreas Hubera1f8ab02012-11-30 10:53:22 -08001322 flushDecoder(false /* audio */, true /* needShutdown */);
1323 }
1324}
1325
1326void NuPlayer::performReset() {
1327 ALOGV("performReset");
1328
1329 CHECK(mAudioDecoder == NULL);
1330 CHECK(mVideoDecoder == NULL);
1331
1332 cancelPollDuration();
1333
1334 ++mScanSourcesGeneration;
1335 mScanSourcesPending = false;
1336
1337 mRenderer.clear();
1338
1339 if (mSource != NULL) {
1340 mSource->stop();
Andreas Huberb5f25f02013-02-05 10:14:26 -08001341
1342 looper()->unregisterHandler(mSource->id());
1343
Andreas Hubera1f8ab02012-11-30 10:53:22 -08001344 mSource.clear();
1345 }
1346
1347 if (mDriver != NULL) {
1348 sp<NuPlayerDriver> driver = mDriver.promote();
1349 if (driver != NULL) {
1350 driver->notifyResetComplete();
1351 }
1352 }
Andreas Huber57a339c2012-12-03 11:18:00 -08001353
1354 mStarted = false;
Andreas Hubera1f8ab02012-11-30 10:53:22 -08001355}
1356
1357void NuPlayer::performScanSources() {
1358 ALOGV("performScanSources");
1359
Andreas Huber57a339c2012-12-03 11:18:00 -08001360 if (!mStarted) {
1361 return;
1362 }
1363
Andreas Hubera1f8ab02012-11-30 10:53:22 -08001364 if (mAudioDecoder == NULL || mVideoDecoder == NULL) {
1365 postScanSources();
1366 }
1367}
1368
Andreas Huber57a339c2012-12-03 11:18:00 -08001369void NuPlayer::performSetSurface(const sp<NativeWindowWrapper> &wrapper) {
1370 ALOGV("performSetSurface");
1371
1372 mNativeWindow = wrapper;
1373
1374 // XXX - ignore error from setVideoScalingMode for now
1375 setVideoScalingMode(mVideoScalingMode);
1376
1377 if (mDriver != NULL) {
1378 sp<NuPlayerDriver> driver = mDriver.promote();
1379 if (driver != NULL) {
1380 driver->notifySetSurfaceComplete();
1381 }
1382 }
1383}
1384
Andreas Huber9575c962013-02-05 13:59:56 -08001385void NuPlayer::onSourceNotify(const sp<AMessage> &msg) {
1386 int32_t what;
1387 CHECK(msg->findInt32("what", &what));
1388
1389 switch (what) {
1390 case Source::kWhatPrepared:
1391 {
Andreas Huberb5f28d42013-04-25 15:11:19 -07001392 if (mSource == NULL) {
1393 // This is a stale notification from a source that was
1394 // asynchronously preparing when the client called reset().
1395 // We handled the reset, the source is gone.
1396 break;
1397 }
1398
Andreas Huberec0c5972013-02-05 14:47:13 -08001399 int32_t err;
1400 CHECK(msg->findInt32("err", &err));
1401
Andreas Huber9575c962013-02-05 13:59:56 -08001402 sp<NuPlayerDriver> driver = mDriver.promote();
1403 if (driver != NULL) {
Andreas Huberec0c5972013-02-05 14:47:13 -08001404 driver->notifyPrepareCompleted(err);
Andreas Huber9575c962013-02-05 13:59:56 -08001405 }
Andreas Huber99759402013-04-01 14:28:31 -07001406
1407 int64_t durationUs;
1408 if (mDriver != NULL && mSource->getDuration(&durationUs) == OK) {
1409 sp<NuPlayerDriver> driver = mDriver.promote();
1410 if (driver != NULL) {
1411 driver->notifyDuration(durationUs);
1412 }
1413 }
Andreas Huber9575c962013-02-05 13:59:56 -08001414 break;
1415 }
1416
1417 case Source::kWhatFlagsChanged:
1418 {
1419 uint32_t flags;
1420 CHECK(msg->findInt32("flags", (int32_t *)&flags));
1421
Chong Zhang4b7069d2013-09-11 12:52:43 -07001422 sp<NuPlayerDriver> driver = mDriver.promote();
1423 if (driver != NULL) {
1424 driver->notifyFlagsChanged(flags);
1425 }
1426
Andreas Huber9575c962013-02-05 13:59:56 -08001427 if ((mSourceFlags & Source::FLAG_DYNAMIC_DURATION)
1428 && (!(flags & Source::FLAG_DYNAMIC_DURATION))) {
1429 cancelPollDuration();
1430 } else if (!(mSourceFlags & Source::FLAG_DYNAMIC_DURATION)
1431 && (flags & Source::FLAG_DYNAMIC_DURATION)
1432 && (mAudioDecoder != NULL || mVideoDecoder != NULL)) {
1433 schedulePollDuration();
1434 }
1435
1436 mSourceFlags = flags;
1437 break;
1438 }
1439
1440 case Source::kWhatVideoSizeChanged:
1441 {
1442 int32_t width, height;
1443 CHECK(msg->findInt32("width", &width));
1444 CHECK(msg->findInt32("height", &height));
1445
1446 notifyListener(MEDIA_SET_VIDEO_SIZE, width, height);
1447 break;
1448 }
1449
Roger Jönssonb50e83e2013-01-21 16:26:41 +01001450 case Source::kWhatBufferingStart:
1451 {
1452 notifyListener(MEDIA_INFO, MEDIA_INFO_BUFFERING_START, 0);
1453 break;
1454 }
1455
1456 case Source::kWhatBufferingEnd:
1457 {
1458 notifyListener(MEDIA_INFO, MEDIA_INFO_BUFFERING_END, 0);
1459 break;
1460 }
1461
Chong Zhangdcb89b32013-08-06 09:44:47 -07001462 case Source::kWhatSubtitleData:
1463 {
1464 sp<ABuffer> buffer;
1465 CHECK(msg->findBuffer("buffer", &buffer));
1466
1467 int32_t trackIndex;
1468 int64_t timeUs, durationUs;
1469 CHECK(buffer->meta()->findInt32("trackIndex", &trackIndex));
1470 CHECK(buffer->meta()->findInt64("timeUs", &timeUs));
1471 CHECK(buffer->meta()->findInt64("durationUs", &durationUs));
1472
1473 Parcel in;
1474 in.writeInt32(trackIndex);
1475 in.writeInt64(timeUs);
1476 in.writeInt64(durationUs);
1477 in.writeInt32(buffer->size());
1478 in.writeInt32(buffer->size());
1479 in.write(buffer->data(), buffer->size());
1480
1481 notifyListener(MEDIA_SUBTITLE_DATA, 0, 0, &in);
1482 break;
1483 }
1484
Andreas Huber14f76722013-01-15 09:04:18 -08001485 case Source::kWhatQueueDecoderShutdown:
1486 {
1487 int32_t audio, video;
1488 CHECK(msg->findInt32("audio", &audio));
1489 CHECK(msg->findInt32("video", &video));
1490
1491 sp<AMessage> reply;
1492 CHECK(msg->findMessage("reply", &reply));
1493
1494 queueDecoderShutdown(audio, video, reply);
1495 break;
1496 }
1497
Andreas Huber9575c962013-02-05 13:59:56 -08001498 default:
1499 TRESPASS();
1500 }
1501}
1502
Andreas Huberb5f25f02013-02-05 10:14:26 -08001503////////////////////////////////////////////////////////////////////////////////
1504
Andreas Huber9575c962013-02-05 13:59:56 -08001505void NuPlayer::Source::notifyFlagsChanged(uint32_t flags) {
1506 sp<AMessage> notify = dupNotify();
1507 notify->setInt32("what", kWhatFlagsChanged);
1508 notify->setInt32("flags", flags);
1509 notify->post();
1510}
1511
1512void NuPlayer::Source::notifyVideoSizeChanged(int32_t width, int32_t height) {
1513 sp<AMessage> notify = dupNotify();
1514 notify->setInt32("what", kWhatVideoSizeChanged);
1515 notify->setInt32("width", width);
1516 notify->setInt32("height", height);
1517 notify->post();
1518}
1519
Andreas Huberec0c5972013-02-05 14:47:13 -08001520void NuPlayer::Source::notifyPrepared(status_t err) {
Andreas Huber9575c962013-02-05 13:59:56 -08001521 sp<AMessage> notify = dupNotify();
1522 notify->setInt32("what", kWhatPrepared);
Andreas Huberec0c5972013-02-05 14:47:13 -08001523 notify->setInt32("err", err);
Andreas Huber9575c962013-02-05 13:59:56 -08001524 notify->post();
1525}
1526
Andreas Huberb5f25f02013-02-05 10:14:26 -08001527void NuPlayer::Source::onMessageReceived(const sp<AMessage> &msg) {
1528 TRESPASS();
1529}
1530
Andreas Huber14f76722013-01-15 09:04:18 -08001531void NuPlayer::queueDecoderShutdown(
1532 bool audio, bool video, const sp<AMessage> &reply) {
1533 ALOGI("queueDecoderShutdown audio=%d, video=%d", audio, video);
1534
1535 mDeferredActions.push_back(
1536 new ShutdownDecoderAction(audio, video));
1537
1538 mDeferredActions.push_back(
1539 new SimpleAction(&NuPlayer::performScanSources));
1540
1541 mDeferredActions.push_back(new PostMessageAction(reply));
1542
1543 processDeferredActions();
1544}
1545
Andreas Huberf9334412010-12-15 15:17:42 -08001546} // namespace android