blob: 25d55a31640d904c6600a3797e0205a5d73b54b1 [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
Robert Shih0523da82014-01-23 16:18:22 -08001014 sp<AMessage> newFormat = mSource->getFormat(audio);
1015 sp<Decoder> &decoder = audio ? mAudioDecoder : mVideoDecoder;
1016 if (formatChange && !decoder->supportsSeamlessFormatChange(newFormat)) {
1017 flushDecoder(audio, /* needShutdown = */ true);
1018 } else {
1019 flushDecoder(audio, /* needShutdown = */ false);
1020 err = OK;
1021 }
Andreas Huber6e3d3112011-11-28 12:36:11 -08001022 } else {
1023 // This stream is unaffected by the discontinuity
1024
1025 if (audio) {
1026 mFlushingAudio = FLUSHED;
1027 } else {
1028 mFlushingVideo = FLUSHED;
1029 }
1030
1031 finishFlushIfPossible();
1032
1033 return -EWOULDBLOCK;
1034 }
Andreas Huber32f3cef2011-03-02 15:34:46 -08001035 }
1036
Andreas Huber3fe62152011-09-16 15:09:22 -07001037 reply->setInt32("err", err);
1038 reply->post();
1039 return OK;
Andreas Huberf9334412010-12-15 15:17:42 -08001040 }
1041
Andreas Huber3fe62152011-09-16 15:09:22 -07001042 if (!audio) {
1043 ++mNumFramesTotal;
1044 }
1045
1046 dropAccessUnit = false;
1047 if (!audio
1048 && mVideoLateByUs > 100000ll
1049 && mVideoIsAVC
1050 && !IsAVCReferenceFrame(accessUnit)) {
1051 dropAccessUnit = true;
1052 ++mNumFramesDropped;
1053 }
1054 } while (dropAccessUnit);
Andreas Huberf9334412010-12-15 15:17:42 -08001055
Steve Block3856b092011-10-20 11:56:00 +01001056 // ALOGV("returned a valid buffer of %s data", audio ? "audio" : "video");
Andreas Huberf9334412010-12-15 15:17:42 -08001057
1058#if 0
1059 int64_t mediaTimeUs;
1060 CHECK(accessUnit->meta()->findInt64("timeUs", &mediaTimeUs));
Steve Block3856b092011-10-20 11:56:00 +01001061 ALOGV("feeding %s input buffer at media time %.2f secs",
Andreas Huberf9334412010-12-15 15:17:42 -08001062 audio ? "audio" : "video",
1063 mediaTimeUs / 1E6);
1064#endif
1065
Andreas Huber2d8bedd2012-02-21 14:38:23 -08001066 reply->setBuffer("buffer", accessUnit);
Andreas Huberf9334412010-12-15 15:17:42 -08001067 reply->post();
1068
1069 return OK;
1070}
1071
1072void NuPlayer::renderBuffer(bool audio, const sp<AMessage> &msg) {
Steve Block3856b092011-10-20 11:56:00 +01001073 // ALOGV("renderBuffer %s", audio ? "audio" : "video");
Andreas Huberf9334412010-12-15 15:17:42 -08001074
1075 sp<AMessage> reply;
1076 CHECK(msg->findMessage("reply", &reply));
1077
Andreas Huber18ac5402011-08-31 15:04:25 -07001078 if (IsFlushingState(audio ? mFlushingAudio : mFlushingVideo)) {
1079 // We're currently attempting to flush the decoder, in order
1080 // to complete this, the decoder wants all its buffers back,
1081 // so we don't want any output buffers it sent us (from before
1082 // we initiated the flush) to be stuck in the renderer's queue.
1083
Steve Block3856b092011-10-20 11:56:00 +01001084 ALOGV("we're still flushing the %s decoder, sending its output buffer"
Andreas Huber18ac5402011-08-31 15:04:25 -07001085 " right back.", audio ? "audio" : "video");
1086
1087 reply->post();
1088 return;
1089 }
1090
Andreas Huber2d8bedd2012-02-21 14:38:23 -08001091 sp<ABuffer> buffer;
1092 CHECK(msg->findBuffer("buffer", &buffer));
Andreas Huberf9334412010-12-15 15:17:42 -08001093
Andreas Huber32f3cef2011-03-02 15:34:46 -08001094 int64_t &skipUntilMediaTimeUs =
1095 audio
1096 ? mSkipRenderingAudioUntilMediaTimeUs
1097 : mSkipRenderingVideoUntilMediaTimeUs;
1098
1099 if (skipUntilMediaTimeUs >= 0) {
1100 int64_t mediaTimeUs;
1101 CHECK(buffer->meta()->findInt64("timeUs", &mediaTimeUs));
1102
1103 if (mediaTimeUs < skipUntilMediaTimeUs) {
Steve Block3856b092011-10-20 11:56:00 +01001104 ALOGV("dropping %s buffer at time %lld as requested.",
Andreas Huber32f3cef2011-03-02 15:34:46 -08001105 audio ? "audio" : "video",
1106 mediaTimeUs);
1107
1108 reply->post();
1109 return;
1110 }
1111
1112 skipUntilMediaTimeUs = -1;
1113 }
1114
Andreas Huberf9334412010-12-15 15:17:42 -08001115 mRenderer->queueBuffer(audio, buffer, reply);
1116}
1117
Chong Zhangdcb89b32013-08-06 09:44:47 -07001118void NuPlayer::notifyListener(int msg, int ext1, int ext2, const Parcel *in) {
Andreas Huber43c3e6c2011-01-05 12:17:08 -08001119 if (mDriver == NULL) {
Andreas Huberf9334412010-12-15 15:17:42 -08001120 return;
1121 }
1122
Andreas Huber43c3e6c2011-01-05 12:17:08 -08001123 sp<NuPlayerDriver> driver = mDriver.promote();
Andreas Huberf9334412010-12-15 15:17:42 -08001124
Andreas Huber43c3e6c2011-01-05 12:17:08 -08001125 if (driver == NULL) {
Andreas Huberf9334412010-12-15 15:17:42 -08001126 return;
1127 }
1128
Chong Zhangdcb89b32013-08-06 09:44:47 -07001129 driver->notifyListener(msg, ext1, ext2, in);
Andreas Huberf9334412010-12-15 15:17:42 -08001130}
1131
Andreas Huber1aef2112011-01-04 14:01:29 -08001132void NuPlayer::flushDecoder(bool audio, bool needShutdown) {
Andreas Huber14f76722013-01-15 09:04:18 -08001133 ALOGV("[%s] flushDecoder needShutdown=%d",
1134 audio ? "audio" : "video", needShutdown);
1135
Andreas Huber6e3d3112011-11-28 12:36:11 -08001136 if ((audio && mAudioDecoder == NULL) || (!audio && mVideoDecoder == NULL)) {
Steve Blockdf64d152012-01-04 20:05:49 +00001137 ALOGI("flushDecoder %s without decoder present",
Andreas Huber6e3d3112011-11-28 12:36:11 -08001138 audio ? "audio" : "video");
1139 }
1140
Andreas Huber1aef2112011-01-04 14:01:29 -08001141 // Make sure we don't continue to scan sources until we finish flushing.
1142 ++mScanSourcesGeneration;
Andreas Huber43c3e6c2011-01-05 12:17:08 -08001143 mScanSourcesPending = false;
Andreas Huber1aef2112011-01-04 14:01:29 -08001144
1145 (audio ? mAudioDecoder : mVideoDecoder)->signalFlush();
1146 mRenderer->flush(audio);
1147
1148 FlushStatus newStatus =
1149 needShutdown ? FLUSHING_DECODER_SHUTDOWN : FLUSHING_DECODER;
1150
1151 if (audio) {
1152 CHECK(mFlushingAudio == NONE
1153 || mFlushingAudio == AWAITING_DISCONTINUITY);
1154
1155 mFlushingAudio = newStatus;
1156
1157 if (mFlushingVideo == NONE) {
1158 mFlushingVideo = (mVideoDecoder != NULL)
1159 ? AWAITING_DISCONTINUITY
1160 : FLUSHED;
1161 }
1162 } else {
1163 CHECK(mFlushingVideo == NONE
1164 || mFlushingVideo == AWAITING_DISCONTINUITY);
1165
1166 mFlushingVideo = newStatus;
1167
1168 if (mFlushingAudio == NONE) {
1169 mFlushingAudio = (mAudioDecoder != NULL)
1170 ? AWAITING_DISCONTINUITY
1171 : FLUSHED;
1172 }
1173 }
1174}
1175
Andreas Huber84066782011-08-16 09:34:26 -07001176sp<AMessage> NuPlayer::Source::getFormat(bool audio) {
1177 sp<MetaData> meta = getFormatMeta(audio);
1178
1179 if (meta == NULL) {
1180 return NULL;
1181 }
1182
1183 sp<AMessage> msg = new AMessage;
1184
1185 if(convertMetaDataToMessage(meta, &msg) == OK) {
1186 return msg;
1187 }
1188 return NULL;
1189}
1190
James Dong0d268a32012-08-31 12:18:27 -07001191status_t NuPlayer::setVideoScalingMode(int32_t mode) {
1192 mVideoScalingMode = mode;
Andreas Huber57a339c2012-12-03 11:18:00 -08001193 if (mNativeWindow != NULL) {
James Dong0d268a32012-08-31 12:18:27 -07001194 status_t ret = native_window_set_scaling_mode(
1195 mNativeWindow->getNativeWindow().get(), mVideoScalingMode);
1196 if (ret != OK) {
1197 ALOGE("Failed to set scaling mode (%d): %s",
1198 -ret, strerror(-ret));
1199 return ret;
1200 }
1201 }
1202 return OK;
1203}
1204
Chong Zhangdcb89b32013-08-06 09:44:47 -07001205status_t NuPlayer::getTrackInfo(Parcel* reply) const {
1206 sp<AMessage> msg = new AMessage(kWhatGetTrackInfo, id());
1207 msg->setPointer("reply", reply);
1208
1209 sp<AMessage> response;
1210 status_t err = msg->postAndAwaitResponse(&response);
1211 return err;
1212}
1213
1214status_t NuPlayer::selectTrack(size_t trackIndex, bool select) {
1215 sp<AMessage> msg = new AMessage(kWhatSelectTrack, id());
1216 msg->setSize("trackIndex", trackIndex);
1217 msg->setInt32("select", select);
1218
1219 sp<AMessage> response;
1220 status_t err = msg->postAndAwaitResponse(&response);
1221
1222 return err;
1223}
1224
Andreas Huberb7c8e912012-11-27 15:02:53 -08001225void NuPlayer::schedulePollDuration() {
1226 sp<AMessage> msg = new AMessage(kWhatPollDuration, id());
1227 msg->setInt32("generation", mPollDurationGeneration);
1228 msg->post();
1229}
1230
1231void NuPlayer::cancelPollDuration() {
1232 ++mPollDurationGeneration;
1233}
1234
Andreas Hubera1f8ab02012-11-30 10:53:22 -08001235void NuPlayer::processDeferredActions() {
1236 while (!mDeferredActions.empty()) {
1237 // We won't execute any deferred actions until we're no longer in
1238 // an intermediate state, i.e. one more more decoders are currently
1239 // flushing or shutting down.
1240
1241 if (mRenderer != NULL) {
1242 // There's an edge case where the renderer owns all output
1243 // buffers and is paused, therefore the decoder will not read
1244 // more input data and will never encounter the matching
1245 // discontinuity. To avoid this, we resume the renderer.
1246
1247 if (mFlushingAudio == AWAITING_DISCONTINUITY
1248 || mFlushingVideo == AWAITING_DISCONTINUITY) {
1249 mRenderer->resume();
1250 }
1251 }
1252
1253 if (mFlushingAudio != NONE || mFlushingVideo != NONE) {
1254 // We're currently flushing, postpone the reset until that's
1255 // completed.
1256
1257 ALOGV("postponing action mFlushingAudio=%d, mFlushingVideo=%d",
1258 mFlushingAudio, mFlushingVideo);
1259
1260 break;
1261 }
1262
1263 sp<Action> action = *mDeferredActions.begin();
1264 mDeferredActions.erase(mDeferredActions.begin());
1265
1266 action->execute(this);
1267 }
1268}
1269
1270void NuPlayer::performSeek(int64_t seekTimeUs) {
1271 ALOGV("performSeek seekTimeUs=%lld us (%.2f secs)",
1272 seekTimeUs,
1273 seekTimeUs / 1E6);
1274
1275 mSource->seekTo(seekTimeUs);
1276
1277 if (mDriver != NULL) {
1278 sp<NuPlayerDriver> driver = mDriver.promote();
1279 if (driver != NULL) {
1280 driver->notifyPosition(seekTimeUs);
1281 driver->notifySeekComplete();
1282 }
1283 }
1284
1285 // everything's flushed, continue playback.
1286}
1287
1288void NuPlayer::performDecoderFlush() {
1289 ALOGV("performDecoderFlush");
1290
Andreas Huberda9740e2013-04-16 10:54:03 -07001291 if (mAudioDecoder == NULL && mVideoDecoder == NULL) {
Andreas Hubera1f8ab02012-11-30 10:53:22 -08001292 return;
1293 }
1294
1295 mTimeDiscontinuityPending = true;
1296
1297 if (mAudioDecoder != NULL) {
1298 flushDecoder(true /* audio */, false /* needShutdown */);
1299 }
1300
1301 if (mVideoDecoder != NULL) {
1302 flushDecoder(false /* audio */, false /* needShutdown */);
1303 }
1304}
1305
Andreas Huber14f76722013-01-15 09:04:18 -08001306void NuPlayer::performDecoderShutdown(bool audio, bool video) {
1307 ALOGV("performDecoderShutdown audio=%d, video=%d", audio, video);
Andreas Hubera1f8ab02012-11-30 10:53:22 -08001308
Andreas Huber14f76722013-01-15 09:04:18 -08001309 if ((!audio || mAudioDecoder == NULL)
1310 && (!video || mVideoDecoder == NULL)) {
Andreas Hubera1f8ab02012-11-30 10:53:22 -08001311 return;
1312 }
1313
1314 mTimeDiscontinuityPending = true;
1315
Andreas Huber14f76722013-01-15 09:04:18 -08001316 if (mFlushingAudio == NONE && (!audio || mAudioDecoder == NULL)) {
1317 mFlushingAudio = FLUSHED;
1318 }
1319
1320 if (mFlushingVideo == NONE && (!video || mVideoDecoder == NULL)) {
1321 mFlushingVideo = FLUSHED;
1322 }
1323
1324 if (audio && mAudioDecoder != NULL) {
Andreas Hubera1f8ab02012-11-30 10:53:22 -08001325 flushDecoder(true /* audio */, true /* needShutdown */);
1326 }
1327
Andreas Huber14f76722013-01-15 09:04:18 -08001328 if (video && mVideoDecoder != NULL) {
Andreas Hubera1f8ab02012-11-30 10:53:22 -08001329 flushDecoder(false /* audio */, true /* needShutdown */);
1330 }
1331}
1332
1333void NuPlayer::performReset() {
1334 ALOGV("performReset");
1335
1336 CHECK(mAudioDecoder == NULL);
1337 CHECK(mVideoDecoder == NULL);
1338
1339 cancelPollDuration();
1340
1341 ++mScanSourcesGeneration;
1342 mScanSourcesPending = false;
1343
1344 mRenderer.clear();
1345
1346 if (mSource != NULL) {
1347 mSource->stop();
Andreas Huberb5f25f02013-02-05 10:14:26 -08001348
1349 looper()->unregisterHandler(mSource->id());
1350
Andreas Hubera1f8ab02012-11-30 10:53:22 -08001351 mSource.clear();
1352 }
1353
1354 if (mDriver != NULL) {
1355 sp<NuPlayerDriver> driver = mDriver.promote();
1356 if (driver != NULL) {
1357 driver->notifyResetComplete();
1358 }
1359 }
Andreas Huber57a339c2012-12-03 11:18:00 -08001360
1361 mStarted = false;
Andreas Hubera1f8ab02012-11-30 10:53:22 -08001362}
1363
1364void NuPlayer::performScanSources() {
1365 ALOGV("performScanSources");
1366
Andreas Huber57a339c2012-12-03 11:18:00 -08001367 if (!mStarted) {
1368 return;
1369 }
1370
Andreas Hubera1f8ab02012-11-30 10:53:22 -08001371 if (mAudioDecoder == NULL || mVideoDecoder == NULL) {
1372 postScanSources();
1373 }
1374}
1375
Andreas Huber57a339c2012-12-03 11:18:00 -08001376void NuPlayer::performSetSurface(const sp<NativeWindowWrapper> &wrapper) {
1377 ALOGV("performSetSurface");
1378
1379 mNativeWindow = wrapper;
1380
1381 // XXX - ignore error from setVideoScalingMode for now
1382 setVideoScalingMode(mVideoScalingMode);
1383
1384 if (mDriver != NULL) {
1385 sp<NuPlayerDriver> driver = mDriver.promote();
1386 if (driver != NULL) {
1387 driver->notifySetSurfaceComplete();
1388 }
1389 }
1390}
1391
Andreas Huber9575c962013-02-05 13:59:56 -08001392void NuPlayer::onSourceNotify(const sp<AMessage> &msg) {
1393 int32_t what;
1394 CHECK(msg->findInt32("what", &what));
1395
1396 switch (what) {
1397 case Source::kWhatPrepared:
1398 {
Andreas Huberb5f28d42013-04-25 15:11:19 -07001399 if (mSource == NULL) {
1400 // This is a stale notification from a source that was
1401 // asynchronously preparing when the client called reset().
1402 // We handled the reset, the source is gone.
1403 break;
1404 }
1405
Andreas Huberec0c5972013-02-05 14:47:13 -08001406 int32_t err;
1407 CHECK(msg->findInt32("err", &err));
1408
Andreas Huber9575c962013-02-05 13:59:56 -08001409 sp<NuPlayerDriver> driver = mDriver.promote();
1410 if (driver != NULL) {
Andreas Huberec0c5972013-02-05 14:47:13 -08001411 driver->notifyPrepareCompleted(err);
Andreas Huber9575c962013-02-05 13:59:56 -08001412 }
Andreas Huber99759402013-04-01 14:28:31 -07001413
1414 int64_t durationUs;
1415 if (mDriver != NULL && mSource->getDuration(&durationUs) == OK) {
1416 sp<NuPlayerDriver> driver = mDriver.promote();
1417 if (driver != NULL) {
1418 driver->notifyDuration(durationUs);
1419 }
1420 }
Andreas Huber9575c962013-02-05 13:59:56 -08001421 break;
1422 }
1423
1424 case Source::kWhatFlagsChanged:
1425 {
1426 uint32_t flags;
1427 CHECK(msg->findInt32("flags", (int32_t *)&flags));
1428
Chong Zhang4b7069d2013-09-11 12:52:43 -07001429 sp<NuPlayerDriver> driver = mDriver.promote();
1430 if (driver != NULL) {
1431 driver->notifyFlagsChanged(flags);
1432 }
1433
Andreas Huber9575c962013-02-05 13:59:56 -08001434 if ((mSourceFlags & Source::FLAG_DYNAMIC_DURATION)
1435 && (!(flags & Source::FLAG_DYNAMIC_DURATION))) {
1436 cancelPollDuration();
1437 } else if (!(mSourceFlags & Source::FLAG_DYNAMIC_DURATION)
1438 && (flags & Source::FLAG_DYNAMIC_DURATION)
1439 && (mAudioDecoder != NULL || mVideoDecoder != NULL)) {
1440 schedulePollDuration();
1441 }
1442
1443 mSourceFlags = flags;
1444 break;
1445 }
1446
1447 case Source::kWhatVideoSizeChanged:
1448 {
1449 int32_t width, height;
1450 CHECK(msg->findInt32("width", &width));
1451 CHECK(msg->findInt32("height", &height));
1452
1453 notifyListener(MEDIA_SET_VIDEO_SIZE, width, height);
1454 break;
1455 }
1456
Roger Jönssonb50e83e2013-01-21 16:26:41 +01001457 case Source::kWhatBufferingStart:
1458 {
1459 notifyListener(MEDIA_INFO, MEDIA_INFO_BUFFERING_START, 0);
1460 break;
1461 }
1462
1463 case Source::kWhatBufferingEnd:
1464 {
1465 notifyListener(MEDIA_INFO, MEDIA_INFO_BUFFERING_END, 0);
1466 break;
1467 }
1468
Chong Zhangdcb89b32013-08-06 09:44:47 -07001469 case Source::kWhatSubtitleData:
1470 {
1471 sp<ABuffer> buffer;
1472 CHECK(msg->findBuffer("buffer", &buffer));
1473
1474 int32_t trackIndex;
1475 int64_t timeUs, durationUs;
1476 CHECK(buffer->meta()->findInt32("trackIndex", &trackIndex));
1477 CHECK(buffer->meta()->findInt64("timeUs", &timeUs));
1478 CHECK(buffer->meta()->findInt64("durationUs", &durationUs));
1479
1480 Parcel in;
1481 in.writeInt32(trackIndex);
1482 in.writeInt64(timeUs);
1483 in.writeInt64(durationUs);
1484 in.writeInt32(buffer->size());
1485 in.writeInt32(buffer->size());
1486 in.write(buffer->data(), buffer->size());
1487
1488 notifyListener(MEDIA_SUBTITLE_DATA, 0, 0, &in);
1489 break;
1490 }
1491
Andreas Huber14f76722013-01-15 09:04:18 -08001492 case Source::kWhatQueueDecoderShutdown:
1493 {
1494 int32_t audio, video;
1495 CHECK(msg->findInt32("audio", &audio));
1496 CHECK(msg->findInt32("video", &video));
1497
1498 sp<AMessage> reply;
1499 CHECK(msg->findMessage("reply", &reply));
1500
1501 queueDecoderShutdown(audio, video, reply);
1502 break;
1503 }
1504
Andreas Huber9575c962013-02-05 13:59:56 -08001505 default:
1506 TRESPASS();
1507 }
1508}
1509
Andreas Huberb5f25f02013-02-05 10:14:26 -08001510////////////////////////////////////////////////////////////////////////////////
1511
Andreas Huber9575c962013-02-05 13:59:56 -08001512void NuPlayer::Source::notifyFlagsChanged(uint32_t flags) {
1513 sp<AMessage> notify = dupNotify();
1514 notify->setInt32("what", kWhatFlagsChanged);
1515 notify->setInt32("flags", flags);
1516 notify->post();
1517}
1518
1519void NuPlayer::Source::notifyVideoSizeChanged(int32_t width, int32_t height) {
1520 sp<AMessage> notify = dupNotify();
1521 notify->setInt32("what", kWhatVideoSizeChanged);
1522 notify->setInt32("width", width);
1523 notify->setInt32("height", height);
1524 notify->post();
1525}
1526
Andreas Huberec0c5972013-02-05 14:47:13 -08001527void NuPlayer::Source::notifyPrepared(status_t err) {
Andreas Huber9575c962013-02-05 13:59:56 -08001528 sp<AMessage> notify = dupNotify();
1529 notify->setInt32("what", kWhatPrepared);
Andreas Huberec0c5972013-02-05 14:47:13 -08001530 notify->setInt32("err", err);
Andreas Huber9575c962013-02-05 13:59:56 -08001531 notify->post();
1532}
1533
Andreas Huberb5f25f02013-02-05 10:14:26 -08001534void NuPlayer::Source::onMessageReceived(const sp<AMessage> &msg) {
1535 TRESPASS();
1536}
1537
Andreas Huber14f76722013-01-15 09:04:18 -08001538void NuPlayer::queueDecoderShutdown(
1539 bool audio, bool video, const sp<AMessage> &reply) {
1540 ALOGI("queueDecoderShutdown audio=%d, video=%d", audio, video);
1541
1542 mDeferredActions.push_back(
1543 new ShutdownDecoderAction(audio, video));
1544
1545 mDeferredActions.push_back(
1546 new SimpleAction(&NuPlayer::performScanSources));
1547
1548 mDeferredActions.push_back(new PostMessageAction(reply));
1549
1550 processDeferredActions();
1551}
1552
Andreas Huberf9334412010-12-15 15:17:42 -08001553} // namespace android