blob: b411f3453b53789d48a85883a2d964bd5492d56f [file] [log] [blame]
Andreas Huberf9334412010-12-15 15:17:42 -08001/*
2 * Copyright (C) 2010 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17//#define LOG_NDEBUG 0
18#define LOG_TAG "NuPlayer"
19#include <utils/Log.h>
20
21#include "NuPlayer.h"
Andreas Huber5bc087c2010-12-23 10:27:40 -080022
23#include "HTTPLiveSource.h"
Andreas Huberf9334412010-12-15 15:17:42 -080024#include "NuPlayerDecoder.h"
Andreas Huber43c3e6c2011-01-05 12:17:08 -080025#include "NuPlayerDriver.h"
Andreas Huberf9334412010-12-15 15:17:42 -080026#include "NuPlayerRenderer.h"
Andreas Huber5bc087c2010-12-23 10:27:40 -080027#include "NuPlayerSource.h"
Andreas Huber2bfdd422011-10-11 15:24:07 -070028#include "RTSPSource.h"
Andreas Huber5bc087c2010-12-23 10:27:40 -080029#include "StreamingSource.h"
Andreas Huberafed0e12011-09-20 15:39:58 -070030#include "GenericSource.h"
Andreas Huber84066782011-08-16 09:34:26 -070031#include "mp4/MP4Source.h"
Andreas Huber5bc087c2010-12-23 10:27:40 -080032
33#include "ATSParser.h"
Andreas Huberf9334412010-12-15 15:17:42 -080034
Andreas Huber84066782011-08-16 09:34:26 -070035#include <cutils/properties.h> // for property_get
Andreas Huber3831a062010-12-21 10:22:33 -080036#include <media/stagefright/foundation/hexdump.h>
Andreas Huberf9334412010-12-15 15:17:42 -080037#include <media/stagefright/foundation/ABuffer.h>
38#include <media/stagefright/foundation/ADebug.h>
39#include <media/stagefright/foundation/AMessage.h>
40#include <media/stagefright/ACodec.h>
Andreas Huber3fe62152011-09-16 15:09:22 -070041#include <media/stagefright/MediaDefs.h>
Andreas Huberf9334412010-12-15 15:17:42 -080042#include <media/stagefright/MediaErrors.h>
43#include <media/stagefright/MetaData.h>
Andy McFadden8ba01022012-12-18 09:46:54 -080044#include <gui/IGraphicBufferProducer.h>
Andreas Huberf9334412010-12-15 15:17:42 -080045
Andreas Huber3fe62152011-09-16 15:09:22 -070046#include "avc_utils.h"
47
Andreas Huber84066782011-08-16 09:34:26 -070048#include "ESDS.h"
49#include <media/stagefright/Utils.h>
50
Andreas Huberf9334412010-12-15 15:17:42 -080051namespace android {
52
Andreas Hubera1f8ab02012-11-30 10:53:22 -080053struct NuPlayer::Action : public RefBase {
54 Action() {}
55
56 virtual void execute(NuPlayer *player) = 0;
57
58private:
59 DISALLOW_EVIL_CONSTRUCTORS(Action);
60};
61
62struct NuPlayer::SeekAction : public Action {
63 SeekAction(int64_t seekTimeUs)
64 : mSeekTimeUs(seekTimeUs) {
65 }
66
67 virtual void execute(NuPlayer *player) {
68 player->performSeek(mSeekTimeUs);
69 }
70
71private:
72 int64_t mSeekTimeUs;
73
74 DISALLOW_EVIL_CONSTRUCTORS(SeekAction);
75};
76
Andreas Huber57a339c2012-12-03 11:18:00 -080077struct NuPlayer::SetSurfaceAction : public Action {
78 SetSurfaceAction(const sp<NativeWindowWrapper> &wrapper)
79 : mWrapper(wrapper) {
80 }
81
82 virtual void execute(NuPlayer *player) {
83 player->performSetSurface(mWrapper);
84 }
85
86private:
87 sp<NativeWindowWrapper> mWrapper;
88
89 DISALLOW_EVIL_CONSTRUCTORS(SetSurfaceAction);
90};
91
Andreas Huber14f76722013-01-15 09:04:18 -080092struct NuPlayer::ShutdownDecoderAction : public Action {
93 ShutdownDecoderAction(bool audio, bool video)
94 : mAudio(audio),
95 mVideo(video) {
96 }
97
98 virtual void execute(NuPlayer *player) {
99 player->performDecoderShutdown(mAudio, mVideo);
100 }
101
102private:
103 bool mAudio;
104 bool mVideo;
105
106 DISALLOW_EVIL_CONSTRUCTORS(ShutdownDecoderAction);
107};
108
109struct NuPlayer::PostMessageAction : public Action {
110 PostMessageAction(const sp<AMessage> &msg)
111 : mMessage(msg) {
112 }
113
114 virtual void execute(NuPlayer *) {
115 mMessage->post();
116 }
117
118private:
119 sp<AMessage> mMessage;
120
121 DISALLOW_EVIL_CONSTRUCTORS(PostMessageAction);
122};
123
Andreas Hubera1f8ab02012-11-30 10:53:22 -0800124// Use this if there's no state necessary to save in order to execute
125// the action.
126struct NuPlayer::SimpleAction : public Action {
127 typedef void (NuPlayer::*ActionFunc)();
128
129 SimpleAction(ActionFunc func)
130 : mFunc(func) {
131 }
132
133 virtual void execute(NuPlayer *player) {
134 (player->*mFunc)();
135 }
136
137private:
138 ActionFunc mFunc;
139
140 DISALLOW_EVIL_CONSTRUCTORS(SimpleAction);
141};
142
Andreas Huberf9334412010-12-15 15:17:42 -0800143////////////////////////////////////////////////////////////////////////////////
144
145NuPlayer::NuPlayer()
Andreas Huber9b80c2b2011-06-30 15:47:02 -0700146 : mUIDValid(false),
Andreas Huber9575c962013-02-05 13:59:56 -0800147 mSourceFlags(0),
Andreas Huber3fe62152011-09-16 15:09:22 -0700148 mVideoIsAVC(false),
Andreas Huber9b80c2b2011-06-30 15:47:02 -0700149 mAudioEOS(false),
Andreas Huberf9334412010-12-15 15:17:42 -0800150 mVideoEOS(false),
Andreas Huber5bc087c2010-12-23 10:27:40 -0800151 mScanSourcesPending(false),
Andreas Huber1aef2112011-01-04 14:01:29 -0800152 mScanSourcesGeneration(0),
Andreas Huberb7c8e912012-11-27 15:02:53 -0800153 mPollDurationGeneration(0),
Andreas Huber6e3d3112011-11-28 12:36:11 -0800154 mTimeDiscontinuityPending(false),
Andreas Huberf9334412010-12-15 15:17:42 -0800155 mFlushingAudio(NONE),
Andreas Huber1aef2112011-01-04 14:01:29 -0800156 mFlushingVideo(NONE),
Andreas Huber3fe62152011-09-16 15:09:22 -0700157 mSkipRenderingAudioUntilMediaTimeUs(-1ll),
158 mSkipRenderingVideoUntilMediaTimeUs(-1ll),
159 mVideoLateByUs(0ll),
160 mNumFramesTotal(0ll),
James Dong0d268a32012-08-31 12:18:27 -0700161 mNumFramesDropped(0ll),
Andreas Huber57a339c2012-12-03 11:18:00 -0800162 mVideoScalingMode(NATIVE_WINDOW_SCALING_MODE_SCALE_TO_WINDOW),
163 mStarted(false) {
Andreas Huberf9334412010-12-15 15:17:42 -0800164}
165
166NuPlayer::~NuPlayer() {
167}
168
Andreas Huber9b80c2b2011-06-30 15:47:02 -0700169void NuPlayer::setUID(uid_t uid) {
170 mUIDValid = true;
171 mUID = uid;
172}
173
Andreas Huber43c3e6c2011-01-05 12:17:08 -0800174void NuPlayer::setDriver(const wp<NuPlayerDriver> &driver) {
175 mDriver = driver;
Andreas Huberf9334412010-12-15 15:17:42 -0800176}
177
Andreas Huber9575c962013-02-05 13:59:56 -0800178void NuPlayer::setDataSourceAsync(const sp<IStreamSource> &source) {
Andreas Huberf9334412010-12-15 15:17:42 -0800179 sp<AMessage> msg = new AMessage(kWhatSetDataSource, id());
180
Andreas Huberb5f25f02013-02-05 10:14:26 -0800181 sp<AMessage> notify = new AMessage(kWhatSourceNotify, id());
182
Andreas Huber84066782011-08-16 09:34:26 -0700183 char prop[PROPERTY_VALUE_MAX];
184 if (property_get("media.stagefright.use-mp4source", prop, NULL)
185 && (!strcmp(prop, "1") || !strcasecmp(prop, "true"))) {
Andreas Huberb5f25f02013-02-05 10:14:26 -0800186 msg->setObject("source", new MP4Source(notify, source));
Andreas Huber84066782011-08-16 09:34:26 -0700187 } else {
Andreas Huberb5f25f02013-02-05 10:14:26 -0800188 msg->setObject("source", new StreamingSource(notify, source));
Andreas Huber84066782011-08-16 09:34:26 -0700189 }
190
Andreas Huber5bc087c2010-12-23 10:27:40 -0800191 msg->post();
192}
Andreas Huberf9334412010-12-15 15:17:42 -0800193
Andreas Huberafed0e12011-09-20 15:39:58 -0700194static bool IsHTTPLiveURL(const char *url) {
195 if (!strncasecmp("http://", url, 7)
Andreas Huber99759402013-04-01 14:28:31 -0700196 || !strncasecmp("https://", url, 8)
197 || !strncasecmp("file://", url, 7)) {
Andreas Huberafed0e12011-09-20 15:39:58 -0700198 size_t len = strlen(url);
199 if (len >= 5 && !strcasecmp(".m3u8", &url[len - 5])) {
200 return true;
201 }
202
203 if (strstr(url,"m3u8")) {
204 return true;
205 }
206 }
207
208 return false;
209}
210
Andreas Huber9575c962013-02-05 13:59:56 -0800211void NuPlayer::setDataSourceAsync(
Andreas Huber5bc087c2010-12-23 10:27:40 -0800212 const char *url, const KeyedVector<String8, String8> *headers) {
213 sp<AMessage> msg = new AMessage(kWhatSetDataSource, id());
Oscar Rydhé7a33b772012-02-20 10:15:48 +0100214 size_t len = strlen(url);
Andreas Huber5bc087c2010-12-23 10:27:40 -0800215
Andreas Huberb5f25f02013-02-05 10:14:26 -0800216 sp<AMessage> notify = new AMessage(kWhatSourceNotify, id());
217
Andreas Huberafed0e12011-09-20 15:39:58 -0700218 sp<Source> source;
219 if (IsHTTPLiveURL(url)) {
Andreas Huberb5f25f02013-02-05 10:14:26 -0800220 source = new HTTPLiveSource(notify, url, headers, mUIDValid, mUID);
Andreas Huberafed0e12011-09-20 15:39:58 -0700221 } else if (!strncasecmp(url, "rtsp://", 7)) {
Andreas Huberb5f25f02013-02-05 10:14:26 -0800222 source = new RTSPSource(notify, url, headers, mUIDValid, mUID);
Oscar Rydhé7a33b772012-02-20 10:15:48 +0100223 } else if ((!strncasecmp(url, "http://", 7)
224 || !strncasecmp(url, "https://", 8))
225 && ((len >= 4 && !strcasecmp(".sdp", &url[len - 4]))
226 || strstr(url, ".sdp?"))) {
227 source = new RTSPSource(notify, url, headers, mUIDValid, mUID, true);
Andreas Huber2bfdd422011-10-11 15:24:07 -0700228 } else {
Andreas Huberb5f25f02013-02-05 10:14:26 -0800229 source = new GenericSource(notify, url, headers, mUIDValid, mUID);
Andreas Huber2bfdd422011-10-11 15:24:07 -0700230 }
231
Andreas Huberafed0e12011-09-20 15:39:58 -0700232 msg->setObject("source", source);
233 msg->post();
234}
235
Andreas Huber9575c962013-02-05 13:59:56 -0800236void NuPlayer::setDataSourceAsync(int fd, int64_t offset, int64_t length) {
Andreas Huberafed0e12011-09-20 15:39:58 -0700237 sp<AMessage> msg = new AMessage(kWhatSetDataSource, id());
238
Andreas Huberb5f25f02013-02-05 10:14:26 -0800239 sp<AMessage> notify = new AMessage(kWhatSourceNotify, id());
240
241 sp<Source> source = new GenericSource(notify, fd, offset, length);
Andreas Huberafed0e12011-09-20 15:39:58 -0700242 msg->setObject("source", source);
Andreas Huberf9334412010-12-15 15:17:42 -0800243 msg->post();
244}
245
Andreas Huber9575c962013-02-05 13:59:56 -0800246void NuPlayer::prepareAsync() {
247 (new AMessage(kWhatPrepare, id()))->post();
248}
249
Andreas Huber57a339c2012-12-03 11:18:00 -0800250void NuPlayer::setVideoSurfaceTextureAsync(
Andy McFadden8ba01022012-12-18 09:46:54 -0800251 const sp<IGraphicBufferProducer> &bufferProducer) {
Glenn Kasten11731182011-02-08 17:26:17 -0800252 sp<AMessage> msg = new AMessage(kWhatSetVideoNativeWindow, id());
Andreas Huber57a339c2012-12-03 11:18:00 -0800253
Andy McFadden8ba01022012-12-18 09:46:54 -0800254 if (bufferProducer == NULL) {
Andreas Huber57a339c2012-12-03 11:18:00 -0800255 msg->setObject("native-window", NULL);
256 } else {
257 msg->setObject(
258 "native-window",
259 new NativeWindowWrapper(
Mathias Agopian1a2952a2013-02-14 17:11:27 -0800260 new Surface(bufferProducer)));
Andreas Huber57a339c2012-12-03 11:18:00 -0800261 }
262
Andreas Huberf9334412010-12-15 15:17:42 -0800263 msg->post();
264}
265
266void NuPlayer::setAudioSink(const sp<MediaPlayerBase::AudioSink> &sink) {
267 sp<AMessage> msg = new AMessage(kWhatSetAudioSink, id());
268 msg->setObject("sink", sink);
269 msg->post();
270}
271
272void NuPlayer::start() {
273 (new AMessage(kWhatStart, id()))->post();
274}
275
Andreas Huber43c3e6c2011-01-05 12:17:08 -0800276void NuPlayer::pause() {
Andreas Huberb4082222011-01-20 15:23:04 -0800277 (new AMessage(kWhatPause, id()))->post();
Andreas Huber43c3e6c2011-01-05 12:17:08 -0800278}
279
280void NuPlayer::resume() {
Andreas Huberb4082222011-01-20 15:23:04 -0800281 (new AMessage(kWhatResume, id()))->post();
Andreas Huber43c3e6c2011-01-05 12:17:08 -0800282}
283
Andreas Huber1aef2112011-01-04 14:01:29 -0800284void NuPlayer::resetAsync() {
285 (new AMessage(kWhatReset, id()))->post();
286}
287
Andreas Huber43c3e6c2011-01-05 12:17:08 -0800288void NuPlayer::seekToAsync(int64_t seekTimeUs) {
289 sp<AMessage> msg = new AMessage(kWhatSeek, id());
290 msg->setInt64("seekTimeUs", seekTimeUs);
291 msg->post();
292}
293
Andreas Huber53df1a42010-12-22 10:03:04 -0800294// static
Andreas Huber1aef2112011-01-04 14:01:29 -0800295bool NuPlayer::IsFlushingState(FlushStatus state, bool *needShutdown) {
Andreas Huber53df1a42010-12-22 10:03:04 -0800296 switch (state) {
297 case FLUSHING_DECODER:
Andreas Huber1aef2112011-01-04 14:01:29 -0800298 if (needShutdown != NULL) {
299 *needShutdown = false;
Andreas Huber53df1a42010-12-22 10:03:04 -0800300 }
301 return true;
302
Andreas Huber1aef2112011-01-04 14:01:29 -0800303 case FLUSHING_DECODER_SHUTDOWN:
304 if (needShutdown != NULL) {
305 *needShutdown = true;
Andreas Huber53df1a42010-12-22 10:03:04 -0800306 }
307 return true;
308
309 default:
310 return false;
311 }
312}
313
Andreas Huberf9334412010-12-15 15:17:42 -0800314void NuPlayer::onMessageReceived(const sp<AMessage> &msg) {
315 switch (msg->what()) {
316 case kWhatSetDataSource:
317 {
Steve Block3856b092011-10-20 11:56:00 +0100318 ALOGV("kWhatSetDataSource");
Andreas Huberf9334412010-12-15 15:17:42 -0800319
320 CHECK(mSource == NULL);
321
Andreas Huber5bc087c2010-12-23 10:27:40 -0800322 sp<RefBase> obj;
323 CHECK(msg->findObject("source", &obj));
Andreas Huberf9334412010-12-15 15:17:42 -0800324
Andreas Huber5bc087c2010-12-23 10:27:40 -0800325 mSource = static_cast<Source *>(obj.get());
Andreas Huberb5f25f02013-02-05 10:14:26 -0800326
327 looper()->registerHandler(mSource);
Andreas Huber9575c962013-02-05 13:59:56 -0800328
329 CHECK(mDriver != NULL);
330 sp<NuPlayerDriver> driver = mDriver.promote();
331 if (driver != NULL) {
332 driver->notifySetDataSourceCompleted(OK);
333 }
334 break;
335 }
336
337 case kWhatPrepare:
338 {
339 mSource->prepareAsync();
Andreas Huberf9334412010-12-15 15:17:42 -0800340 break;
341 }
342
Andreas Huberb7c8e912012-11-27 15:02:53 -0800343 case kWhatPollDuration:
344 {
345 int32_t generation;
346 CHECK(msg->findInt32("generation", &generation));
347
348 if (generation != mPollDurationGeneration) {
349 // stale
350 break;
351 }
352
353 int64_t durationUs;
354 if (mDriver != NULL && mSource->getDuration(&durationUs) == OK) {
355 sp<NuPlayerDriver> driver = mDriver.promote();
356 if (driver != NULL) {
357 driver->notifyDuration(durationUs);
358 }
359 }
360
361 msg->post(1000000ll); // poll again in a second.
362 break;
363 }
364
Glenn Kasten11731182011-02-08 17:26:17 -0800365 case kWhatSetVideoNativeWindow:
Andreas Huberf9334412010-12-15 15:17:42 -0800366 {
Steve Block3856b092011-10-20 11:56:00 +0100367 ALOGV("kWhatSetVideoNativeWindow");
Andreas Huberf9334412010-12-15 15:17:42 -0800368
Andreas Huber57a339c2012-12-03 11:18:00 -0800369 mDeferredActions.push_back(
Andreas Huber14f76722013-01-15 09:04:18 -0800370 new ShutdownDecoderAction(
371 false /* audio */, true /* video */));
Andreas Huber57a339c2012-12-03 11:18:00 -0800372
Andreas Huberf9334412010-12-15 15:17:42 -0800373 sp<RefBase> obj;
Glenn Kasten11731182011-02-08 17:26:17 -0800374 CHECK(msg->findObject("native-window", &obj));
Andreas Huberf9334412010-12-15 15:17:42 -0800375
Andreas Huber57a339c2012-12-03 11:18:00 -0800376 mDeferredActions.push_back(
377 new SetSurfaceAction(
378 static_cast<NativeWindowWrapper *>(obj.get())));
James Dong0d268a32012-08-31 12:18:27 -0700379
Andreas Huber57a339c2012-12-03 11:18:00 -0800380 if (obj != NULL) {
381 // If there is a new surface texture, instantiate decoders
382 // again if possible.
383 mDeferredActions.push_back(
384 new SimpleAction(&NuPlayer::performScanSources));
385 }
386
387 processDeferredActions();
Andreas Huberf9334412010-12-15 15:17:42 -0800388 break;
389 }
390
391 case kWhatSetAudioSink:
392 {
Steve Block3856b092011-10-20 11:56:00 +0100393 ALOGV("kWhatSetAudioSink");
Andreas Huberf9334412010-12-15 15:17:42 -0800394
395 sp<RefBase> obj;
396 CHECK(msg->findObject("sink", &obj));
397
398 mAudioSink = static_cast<MediaPlayerBase::AudioSink *>(obj.get());
399 break;
400 }
401
402 case kWhatStart:
403 {
Steve Block3856b092011-10-20 11:56:00 +0100404 ALOGV("kWhatStart");
Andreas Huber43c3e6c2011-01-05 12:17:08 -0800405
Andreas Huber3fe62152011-09-16 15:09:22 -0700406 mVideoIsAVC = false;
Andreas Huber1aef2112011-01-04 14:01:29 -0800407 mAudioEOS = false;
408 mVideoEOS = false;
Andreas Huber32f3cef2011-03-02 15:34:46 -0800409 mSkipRenderingAudioUntilMediaTimeUs = -1;
410 mSkipRenderingVideoUntilMediaTimeUs = -1;
Andreas Huber3fe62152011-09-16 15:09:22 -0700411 mVideoLateByUs = 0;
412 mNumFramesTotal = 0;
413 mNumFramesDropped = 0;
Andreas Huber57a339c2012-12-03 11:18:00 -0800414 mStarted = true;
Andreas Huber1aef2112011-01-04 14:01:29 -0800415
Andreas Huber5bc087c2010-12-23 10:27:40 -0800416 mSource->start();
Andreas Huberf9334412010-12-15 15:17:42 -0800417
Andreas Huberd5e56232013-03-12 11:01:43 -0700418 uint32_t flags = 0;
419
420 if (mSource->isRealTime()) {
421 flags |= Renderer::FLAG_REAL_TIME;
422 }
423
Andreas Huberf9334412010-12-15 15:17:42 -0800424 mRenderer = new Renderer(
425 mAudioSink,
Andreas Huberd5e56232013-03-12 11:01:43 -0700426 new AMessage(kWhatRendererNotify, id()),
427 flags);
Andreas Huberf9334412010-12-15 15:17:42 -0800428
429 looper()->registerHandler(mRenderer);
430
Andreas Huber1aef2112011-01-04 14:01:29 -0800431 postScanSources();
Andreas Huberf9334412010-12-15 15:17:42 -0800432 break;
433 }
434
435 case kWhatScanSources:
436 {
Andreas Huber1aef2112011-01-04 14:01:29 -0800437 int32_t generation;
438 CHECK(msg->findInt32("generation", &generation));
439 if (generation != mScanSourcesGeneration) {
440 // Drop obsolete msg.
441 break;
442 }
443
Andreas Huber5bc087c2010-12-23 10:27:40 -0800444 mScanSourcesPending = false;
445
Steve Block3856b092011-10-20 11:56:00 +0100446 ALOGV("scanning sources haveAudio=%d, haveVideo=%d",
Andreas Huber43c3e6c2011-01-05 12:17:08 -0800447 mAudioDecoder != NULL, mVideoDecoder != NULL);
448
Andreas Huberb7c8e912012-11-27 15:02:53 -0800449 bool mHadAnySourcesBefore =
450 (mAudioDecoder != NULL) || (mVideoDecoder != NULL);
451
Haynes Mathew George5d246ef2012-07-09 10:36:57 -0700452 if (mNativeWindow != NULL) {
453 instantiateDecoder(false, &mVideoDecoder);
454 }
Andreas Huberf9334412010-12-15 15:17:42 -0800455
456 if (mAudioSink != NULL) {
Andreas Huber5bc087c2010-12-23 10:27:40 -0800457 instantiateDecoder(true, &mAudioDecoder);
Andreas Huberf9334412010-12-15 15:17:42 -0800458 }
459
Andreas Huberb7c8e912012-11-27 15:02:53 -0800460 if (!mHadAnySourcesBefore
461 && (mAudioDecoder != NULL || mVideoDecoder != NULL)) {
462 // This is the first time we've found anything playable.
463
Andreas Huber9575c962013-02-05 13:59:56 -0800464 if (mSourceFlags & Source::FLAG_DYNAMIC_DURATION) {
Andreas Huberb7c8e912012-11-27 15:02:53 -0800465 schedulePollDuration();
466 }
467 }
468
Andreas Hubereac68ba2011-09-27 12:12:25 -0700469 status_t err;
470 if ((err = mSource->feedMoreTSData()) != OK) {
Andreas Huber1aef2112011-01-04 14:01:29 -0800471 if (mAudioDecoder == NULL && mVideoDecoder == NULL) {
472 // We're not currently decoding anything (no audio or
473 // video tracks found) and we just ran out of input data.
Andreas Hubereac68ba2011-09-27 12:12:25 -0700474
475 if (err == ERROR_END_OF_STREAM) {
476 notifyListener(MEDIA_PLAYBACK_COMPLETE, 0, 0);
477 } else {
478 notifyListener(MEDIA_ERROR, MEDIA_ERROR_UNKNOWN, err);
479 }
Andreas Huber1aef2112011-01-04 14:01:29 -0800480 }
Andreas Huberf9334412010-12-15 15:17:42 -0800481 break;
482 }
483
Andreas Huberfbe9d812012-08-31 14:05:27 -0700484 if ((mAudioDecoder == NULL && mAudioSink != NULL)
485 || (mVideoDecoder == NULL && mNativeWindow != NULL)) {
Andreas Huberf9334412010-12-15 15:17:42 -0800486 msg->post(100000ll);
Andreas Huber5bc087c2010-12-23 10:27:40 -0800487 mScanSourcesPending = true;
Andreas Huberf9334412010-12-15 15:17:42 -0800488 }
489 break;
490 }
491
492 case kWhatVideoNotify:
493 case kWhatAudioNotify:
494 {
495 bool audio = msg->what() == kWhatAudioNotify;
496
497 sp<AMessage> codecRequest;
498 CHECK(msg->findMessage("codec-request", &codecRequest));
499
500 int32_t what;
501 CHECK(codecRequest->findInt32("what", &what));
502
503 if (what == ACodec::kWhatFillThisBuffer) {
504 status_t err = feedDecoderInputData(
505 audio, codecRequest);
506
Andreas Huber5bc087c2010-12-23 10:27:40 -0800507 if (err == -EWOULDBLOCK) {
Andreas Hubereac68ba2011-09-27 12:12:25 -0700508 if (mSource->feedMoreTSData() == OK) {
Andreas Huber1183a4a2011-11-03 11:00:21 -0700509 msg->post(10000ll);
Andreas Huber5bc087c2010-12-23 10:27:40 -0800510 }
Andreas Huberf9334412010-12-15 15:17:42 -0800511 }
512 } else if (what == ACodec::kWhatEOS) {
Andreas Huberdc9bacd2011-09-26 10:53:29 -0700513 int32_t err;
514 CHECK(codecRequest->findInt32("err", &err));
515
516 if (err == ERROR_END_OF_STREAM) {
Steve Block3856b092011-10-20 11:56:00 +0100517 ALOGV("got %s decoder EOS", audio ? "audio" : "video");
Andreas Huberdc9bacd2011-09-26 10:53:29 -0700518 } else {
Steve Block3856b092011-10-20 11:56:00 +0100519 ALOGV("got %s decoder EOS w/ error %d",
Andreas Huberdc9bacd2011-09-26 10:53:29 -0700520 audio ? "audio" : "video",
521 err);
522 }
523
524 mRenderer->queueEOS(audio, err);
Andreas Huberf9334412010-12-15 15:17:42 -0800525 } else if (what == ACodec::kWhatFlushCompleted) {
Andreas Huber1aef2112011-01-04 14:01:29 -0800526 bool needShutdown;
Andreas Huber53df1a42010-12-22 10:03:04 -0800527
Andreas Huberf9334412010-12-15 15:17:42 -0800528 if (audio) {
Andreas Huber1aef2112011-01-04 14:01:29 -0800529 CHECK(IsFlushingState(mFlushingAudio, &needShutdown));
Andreas Huberf9334412010-12-15 15:17:42 -0800530 mFlushingAudio = FLUSHED;
531 } else {
Andreas Huber1aef2112011-01-04 14:01:29 -0800532 CHECK(IsFlushingState(mFlushingVideo, &needShutdown));
Andreas Huberf9334412010-12-15 15:17:42 -0800533 mFlushingVideo = FLUSHED;
Andreas Huber3fe62152011-09-16 15:09:22 -0700534
535 mVideoLateByUs = 0;
Andreas Huberf9334412010-12-15 15:17:42 -0800536 }
537
Steve Block3856b092011-10-20 11:56:00 +0100538 ALOGV("decoder %s flush completed", audio ? "audio" : "video");
Andreas Huberf9334412010-12-15 15:17:42 -0800539
Andreas Huber1aef2112011-01-04 14:01:29 -0800540 if (needShutdown) {
Steve Block3856b092011-10-20 11:56:00 +0100541 ALOGV("initiating %s decoder shutdown",
Andreas Huber53df1a42010-12-22 10:03:04 -0800542 audio ? "audio" : "video");
Andreas Huberf9334412010-12-15 15:17:42 -0800543
Andreas Huber53df1a42010-12-22 10:03:04 -0800544 (audio ? mAudioDecoder : mVideoDecoder)->initiateShutdown();
Andreas Huberf9334412010-12-15 15:17:42 -0800545
Andreas Huber53df1a42010-12-22 10:03:04 -0800546 if (audio) {
547 mFlushingAudio = SHUTTING_DOWN_DECODER;
548 } else {
549 mFlushingVideo = SHUTTING_DOWN_DECODER;
550 }
Andreas Huberf9334412010-12-15 15:17:42 -0800551 }
Andreas Huber3831a062010-12-21 10:22:33 -0800552
553 finishFlushIfPossible();
Andreas Huber2c2814b2010-12-15 17:18:20 -0800554 } else if (what == ACodec::kWhatOutputFormatChanged) {
Andreas Huber31e25082011-01-10 10:38:31 -0800555 if (audio) {
556 int32_t numChannels;
Andreas Huber516dacf2012-12-03 15:20:40 -0800557 CHECK(codecRequest->findInt32(
558 "channel-count", &numChannels));
Andreas Huber2c2814b2010-12-15 17:18:20 -0800559
Andreas Huber31e25082011-01-10 10:38:31 -0800560 int32_t sampleRate;
561 CHECK(codecRequest->findInt32("sample-rate", &sampleRate));
Andreas Huber2c2814b2010-12-15 17:18:20 -0800562
Steve Block3856b092011-10-20 11:56:00 +0100563 ALOGV("Audio output format changed to %d Hz, %d channels",
Andreas Huber31e25082011-01-10 10:38:31 -0800564 sampleRate, numChannels);
Andreas Huber2c2814b2010-12-15 17:18:20 -0800565
Andreas Huber31e25082011-01-10 10:38:31 -0800566 mAudioSink->close();
Eric Laurent1948eb32012-04-13 16:50:19 -0700567
568 audio_output_flags_t flags;
569 int64_t durationUs;
Andreas Huber516dacf2012-12-03 15:20:40 -0800570 // FIXME: we should handle the case where the video decoder
571 // is created after we receive the format change indication.
572 // Current code will just make that we select deep buffer
573 // with video which should not be a problem as it should
Eric Laurent1948eb32012-04-13 16:50:19 -0700574 // not prevent from keeping A/V sync.
575 if (mVideoDecoder == NULL &&
576 mSource->getDuration(&durationUs) == OK &&
Andreas Huber516dacf2012-12-03 15:20:40 -0800577 durationUs
578 > AUDIO_SINK_MIN_DEEP_BUFFER_DURATION_US) {
Eric Laurent1948eb32012-04-13 16:50:19 -0700579 flags = AUDIO_OUTPUT_FLAG_DEEP_BUFFER;
580 } else {
581 flags = AUDIO_OUTPUT_FLAG_NONE;
582 }
583
Andreas Huber98065552012-05-03 11:33:01 -0700584 int32_t channelMask;
585 if (!codecRequest->findInt32("channel-mask", &channelMask)) {
586 channelMask = CHANNEL_MASK_USE_CHANNEL_ORDER;
587 }
588
Andreas Huber078cfcf2011-09-15 12:25:04 -0700589 CHECK_EQ(mAudioSink->open(
590 sampleRate,
591 numChannels,
Andreas Huber98065552012-05-03 11:33:01 -0700592 (audio_channel_mask_t)channelMask,
Andreas Huber078cfcf2011-09-15 12:25:04 -0700593 AUDIO_FORMAT_PCM_16_BIT,
Eric Laurent1948eb32012-04-13 16:50:19 -0700594 8 /* bufferCount */,
595 NULL,
596 NULL,
597 flags),
Andreas Huber078cfcf2011-09-15 12:25:04 -0700598 (status_t)OK);
Andreas Huber31e25082011-01-10 10:38:31 -0800599 mAudioSink->start();
Andreas Huber2c2814b2010-12-15 17:18:20 -0800600
Andreas Huber31e25082011-01-10 10:38:31 -0800601 mRenderer->signalAudioSinkChanged();
602 } else {
603 // video
Andreas Huber3831a062010-12-21 10:22:33 -0800604
Andreas Huber31e25082011-01-10 10:38:31 -0800605 int32_t width, height;
606 CHECK(codecRequest->findInt32("width", &width));
607 CHECK(codecRequest->findInt32("height", &height));
608
609 int32_t cropLeft, cropTop, cropRight, cropBottom;
610 CHECK(codecRequest->findRect(
611 "crop",
612 &cropLeft, &cropTop, &cropRight, &cropBottom));
613
Andreas Huber516dacf2012-12-03 15:20:40 -0800614 int32_t displayWidth = cropRight - cropLeft + 1;
615 int32_t displayHeight = cropBottom - cropTop + 1;
616
Steve Block3856b092011-10-20 11:56:00 +0100617 ALOGV("Video output format changed to %d x %d "
Andreas Hubercb67cd12011-08-26 16:02:19 -0700618 "(crop: %d x %d @ (%d, %d))",
Andreas Huber31e25082011-01-10 10:38:31 -0800619 width, height,
Andreas Huber516dacf2012-12-03 15:20:40 -0800620 displayWidth,
621 displayHeight,
Andreas Hubercb67cd12011-08-26 16:02:19 -0700622 cropLeft, cropTop);
Andreas Huber31e25082011-01-10 10:38:31 -0800623
Andreas Huber516dacf2012-12-03 15:20:40 -0800624 sp<AMessage> videoInputFormat =
625 mSource->getFormat(false /* audio */);
626
627 // Take into account sample aspect ratio if necessary:
628 int32_t sarWidth, sarHeight;
629 if (videoInputFormat->findInt32("sar-width", &sarWidth)
630 && videoInputFormat->findInt32(
631 "sar-height", &sarHeight)) {
632 ALOGV("Sample aspect ratio %d : %d",
633 sarWidth, sarHeight);
634
635 displayWidth = (displayWidth * sarWidth) / sarHeight;
636
637 ALOGV("display dimensions %d x %d",
638 displayWidth, displayHeight);
639 }
640
Andreas Huber31e25082011-01-10 10:38:31 -0800641 notifyListener(
Andreas Huber516dacf2012-12-03 15:20:40 -0800642 MEDIA_SET_VIDEO_SIZE, displayWidth, displayHeight);
Andreas Huber31e25082011-01-10 10:38:31 -0800643 }
Andreas Huber3831a062010-12-21 10:22:33 -0800644 } else if (what == ACodec::kWhatShutdownCompleted) {
Steve Block3856b092011-10-20 11:56:00 +0100645 ALOGV("%s shutdown completed", audio ? "audio" : "video");
Andreas Huber3831a062010-12-21 10:22:33 -0800646 if (audio) {
647 mAudioDecoder.clear();
648
649 CHECK_EQ((int)mFlushingAudio, (int)SHUTTING_DOWN_DECODER);
650 mFlushingAudio = SHUT_DOWN;
651 } else {
652 mVideoDecoder.clear();
653
654 CHECK_EQ((int)mFlushingVideo, (int)SHUTTING_DOWN_DECODER);
655 mFlushingVideo = SHUT_DOWN;
656 }
657
658 finishFlushIfPossible();
Andreas Huberc92fd242011-08-16 13:48:44 -0700659 } else if (what == ACodec::kWhatError) {
Steve Block29357bc2012-01-06 19:20:56 +0000660 ALOGE("Received error from %s decoder, aborting playback.",
Andreas Huberc92fd242011-08-16 13:48:44 -0700661 audio ? "audio" : "video");
662
663 mRenderer->queueEOS(audio, UNKNOWN_ERROR);
Andreas Huber57788222012-02-21 11:47:18 -0800664 } else if (what == ACodec::kWhatDrainThisBuffer) {
Andreas Huberf9334412010-12-15 15:17:42 -0800665 renderBuffer(audio, codecRequest);
Andreas Hubera1f8ab02012-11-30 10:53:22 -0800666 } else if (what != ACodec::kWhatComponentAllocated
667 && what != ACodec::kWhatComponentConfigured
668 && what != ACodec::kWhatBuffersAllocated) {
669 ALOGV("Unhandled codec notification %d '%c%c%c%c'.",
670 what,
671 what >> 24,
672 (what >> 16) & 0xff,
673 (what >> 8) & 0xff,
674 what & 0xff);
Andreas Huberf9334412010-12-15 15:17:42 -0800675 }
676
677 break;
678 }
679
680 case kWhatRendererNotify:
681 {
682 int32_t what;
683 CHECK(msg->findInt32("what", &what));
684
685 if (what == Renderer::kWhatEOS) {
686 int32_t audio;
687 CHECK(msg->findInt32("audio", &audio));
688
Andreas Huberc92fd242011-08-16 13:48:44 -0700689 int32_t finalResult;
690 CHECK(msg->findInt32("finalResult", &finalResult));
691
Andreas Huberf9334412010-12-15 15:17:42 -0800692 if (audio) {
693 mAudioEOS = true;
694 } else {
695 mVideoEOS = true;
696 }
697
Andreas Huberc92fd242011-08-16 13:48:44 -0700698 if (finalResult == ERROR_END_OF_STREAM) {
Steve Block3856b092011-10-20 11:56:00 +0100699 ALOGV("reached %s EOS", audio ? "audio" : "video");
Andreas Huberc92fd242011-08-16 13:48:44 -0700700 } else {
Steve Block29357bc2012-01-06 19:20:56 +0000701 ALOGE("%s track encountered an error (%d)",
Andreas Huberc92fd242011-08-16 13:48:44 -0700702 audio ? "audio" : "video", finalResult);
703
704 notifyListener(
705 MEDIA_ERROR, MEDIA_ERROR_UNKNOWN, finalResult);
706 }
Andreas Huberf9334412010-12-15 15:17:42 -0800707
708 if ((mAudioEOS || mAudioDecoder == NULL)
709 && (mVideoEOS || mVideoDecoder == NULL)) {
710 notifyListener(MEDIA_PLAYBACK_COMPLETE, 0, 0);
711 }
Andreas Huber43c3e6c2011-01-05 12:17:08 -0800712 } else if (what == Renderer::kWhatPosition) {
713 int64_t positionUs;
714 CHECK(msg->findInt64("positionUs", &positionUs));
715
Andreas Huber3fe62152011-09-16 15:09:22 -0700716 CHECK(msg->findInt64("videoLateByUs", &mVideoLateByUs));
717
Andreas Huber43c3e6c2011-01-05 12:17:08 -0800718 if (mDriver != NULL) {
719 sp<NuPlayerDriver> driver = mDriver.promote();
720 if (driver != NULL) {
721 driver->notifyPosition(positionUs);
Andreas Huber3fe62152011-09-16 15:09:22 -0700722
723 driver->notifyFrameStats(
724 mNumFramesTotal, mNumFramesDropped);
Andreas Huber43c3e6c2011-01-05 12:17:08 -0800725 }
726 }
Andreas Huber3fe62152011-09-16 15:09:22 -0700727 } else if (what == Renderer::kWhatFlushComplete) {
Andreas Huberf9334412010-12-15 15:17:42 -0800728 int32_t audio;
729 CHECK(msg->findInt32("audio", &audio));
730
Steve Block3856b092011-10-20 11:56:00 +0100731 ALOGV("renderer %s flush completed.", audio ? "audio" : "video");
James Dongf57b4ea2012-07-20 13:38:36 -0700732 } else if (what == Renderer::kWhatVideoRenderingStart) {
733 notifyListener(MEDIA_INFO, MEDIA_INFO_RENDERING_START, 0);
Lajos Molnarcbaffcf2013-08-14 18:30:38 -0700734 } else if (what == Renderer::kWhatMediaRenderingStart) {
735 ALOGV("media rendering started");
736 notifyListener(MEDIA_STARTED, 0, 0);
Andreas Huberf9334412010-12-15 15:17:42 -0800737 }
738 break;
739 }
740
741 case kWhatMoreDataQueued:
742 {
743 break;
744 }
745
Andreas Huber1aef2112011-01-04 14:01:29 -0800746 case kWhatReset:
747 {
Steve Block3856b092011-10-20 11:56:00 +0100748 ALOGV("kWhatReset");
Andreas Huber1aef2112011-01-04 14:01:29 -0800749
Andreas Hubera1f8ab02012-11-30 10:53:22 -0800750 mDeferredActions.push_back(
Andreas Huber14f76722013-01-15 09:04:18 -0800751 new ShutdownDecoderAction(
752 true /* audio */, true /* video */));
Andreas Huberb7c8e912012-11-27 15:02:53 -0800753
Andreas Hubera1f8ab02012-11-30 10:53:22 -0800754 mDeferredActions.push_back(
755 new SimpleAction(&NuPlayer::performReset));
Andreas Huberb58ce9f2011-11-28 16:27:35 -0800756
Andreas Hubera1f8ab02012-11-30 10:53:22 -0800757 processDeferredActions();
Andreas Huber1aef2112011-01-04 14:01:29 -0800758 break;
759 }
760
Andreas Huber43c3e6c2011-01-05 12:17:08 -0800761 case kWhatSeek:
762 {
763 int64_t seekTimeUs;
764 CHECK(msg->findInt64("seekTimeUs", &seekTimeUs));
765
Andreas Hubera1f8ab02012-11-30 10:53:22 -0800766 ALOGV("kWhatSeek seekTimeUs=%lld us", seekTimeUs);
Andreas Huber43c3e6c2011-01-05 12:17:08 -0800767
Andreas Hubera1f8ab02012-11-30 10:53:22 -0800768 mDeferredActions.push_back(
769 new SimpleAction(&NuPlayer::performDecoderFlush));
Andreas Huber43c3e6c2011-01-05 12:17:08 -0800770
Andreas Hubera1f8ab02012-11-30 10:53:22 -0800771 mDeferredActions.push_back(new SeekAction(seekTimeUs));
Andreas Huber43c3e6c2011-01-05 12:17:08 -0800772
Andreas Hubera1f8ab02012-11-30 10:53:22 -0800773 processDeferredActions();
Andreas Huber43c3e6c2011-01-05 12:17:08 -0800774 break;
775 }
776
Andreas Huberb4082222011-01-20 15:23:04 -0800777 case kWhatPause:
778 {
779 CHECK(mRenderer != NULL);
Roger Jönssonfba60da2013-01-21 17:15:45 +0100780 mSource->pause();
Andreas Huberb4082222011-01-20 15:23:04 -0800781 mRenderer->pause();
782 break;
783 }
784
785 case kWhatResume:
786 {
787 CHECK(mRenderer != NULL);
Roger Jönssonfba60da2013-01-21 17:15:45 +0100788 mSource->resume();
Andreas Huberb4082222011-01-20 15:23:04 -0800789 mRenderer->resume();
790 break;
791 }
792
Andreas Huberb5f25f02013-02-05 10:14:26 -0800793 case kWhatSourceNotify:
794 {
Andreas Huber9575c962013-02-05 13:59:56 -0800795 onSourceNotify(msg);
Andreas Huberb5f25f02013-02-05 10:14:26 -0800796 break;
797 }
798
Andreas Huberf9334412010-12-15 15:17:42 -0800799 default:
800 TRESPASS();
801 break;
802 }
803}
804
Andreas Huber3831a062010-12-21 10:22:33 -0800805void NuPlayer::finishFlushIfPossible() {
806 if (mFlushingAudio != FLUSHED && mFlushingAudio != SHUT_DOWN) {
807 return;
808 }
809
810 if (mFlushingVideo != FLUSHED && mFlushingVideo != SHUT_DOWN) {
811 return;
812 }
813
Steve Block3856b092011-10-20 11:56:00 +0100814 ALOGV("both audio and video are flushed now.");
Andreas Huber3831a062010-12-21 10:22:33 -0800815
Andreas Huber6e3d3112011-11-28 12:36:11 -0800816 if (mTimeDiscontinuityPending) {
817 mRenderer->signalTimeDiscontinuity();
818 mTimeDiscontinuityPending = false;
819 }
Andreas Huber3831a062010-12-21 10:22:33 -0800820
Andreas Huber22fc52f2011-01-05 16:24:27 -0800821 if (mAudioDecoder != NULL) {
Andreas Huber3831a062010-12-21 10:22:33 -0800822 mAudioDecoder->signalResume();
823 }
824
Andreas Huber22fc52f2011-01-05 16:24:27 -0800825 if (mVideoDecoder != NULL) {
Andreas Huber3831a062010-12-21 10:22:33 -0800826 mVideoDecoder->signalResume();
827 }
828
829 mFlushingAudio = NONE;
830 mFlushingVideo = NONE;
Andreas Huber3831a062010-12-21 10:22:33 -0800831
Andreas Hubera1f8ab02012-11-30 10:53:22 -0800832 processDeferredActions();
Andreas Huber1aef2112011-01-04 14:01:29 -0800833}
834
835void NuPlayer::postScanSources() {
836 if (mScanSourcesPending) {
837 return;
838 }
839
840 sp<AMessage> msg = new AMessage(kWhatScanSources, id());
841 msg->setInt32("generation", mScanSourcesGeneration);
842 msg->post();
843
844 mScanSourcesPending = true;
845}
846
Andreas Huber5bc087c2010-12-23 10:27:40 -0800847status_t NuPlayer::instantiateDecoder(bool audio, sp<Decoder> *decoder) {
Andreas Huberf9334412010-12-15 15:17:42 -0800848 if (*decoder != NULL) {
849 return OK;
850 }
851
Andreas Huber84066782011-08-16 09:34:26 -0700852 sp<AMessage> format = mSource->getFormat(audio);
Andreas Huberf9334412010-12-15 15:17:42 -0800853
Andreas Huber84066782011-08-16 09:34:26 -0700854 if (format == NULL) {
Andreas Huberf9334412010-12-15 15:17:42 -0800855 return -EWOULDBLOCK;
856 }
857
Andreas Huber3fe62152011-09-16 15:09:22 -0700858 if (!audio) {
Andreas Huber84066782011-08-16 09:34:26 -0700859 AString mime;
860 CHECK(format->findString("mime", &mime));
861 mVideoIsAVC = !strcasecmp(MEDIA_MIMETYPE_VIDEO_AVC, mime.c_str());
Andreas Huber3fe62152011-09-16 15:09:22 -0700862 }
863
Andreas Huberf9334412010-12-15 15:17:42 -0800864 sp<AMessage> notify =
865 new AMessage(audio ? kWhatAudioNotify : kWhatVideoNotify,
866 id());
867
Glenn Kasten11731182011-02-08 17:26:17 -0800868 *decoder = audio ? new Decoder(notify) :
869 new Decoder(notify, mNativeWindow);
Andreas Huberf9334412010-12-15 15:17:42 -0800870 looper()->registerHandler(*decoder);
871
Andreas Huber84066782011-08-16 09:34:26 -0700872 (*decoder)->configure(format);
Andreas Huberf9334412010-12-15 15:17:42 -0800873
Andreas Huberf9334412010-12-15 15:17:42 -0800874 return OK;
875}
876
877status_t NuPlayer::feedDecoderInputData(bool audio, const sp<AMessage> &msg) {
878 sp<AMessage> reply;
879 CHECK(msg->findMessage("reply", &reply));
880
Andreas Huber53df1a42010-12-22 10:03:04 -0800881 if ((audio && IsFlushingState(mFlushingAudio))
882 || (!audio && IsFlushingState(mFlushingVideo))) {
Andreas Huberf9334412010-12-15 15:17:42 -0800883 reply->setInt32("err", INFO_DISCONTINUITY);
884 reply->post();
885 return OK;
886 }
887
888 sp<ABuffer> accessUnit;
Andreas Huberf9334412010-12-15 15:17:42 -0800889
Andreas Huber3fe62152011-09-16 15:09:22 -0700890 bool dropAccessUnit;
891 do {
892 status_t err = mSource->dequeueAccessUnit(audio, &accessUnit);
Andreas Huber5bc087c2010-12-23 10:27:40 -0800893
Andreas Huber3fe62152011-09-16 15:09:22 -0700894 if (err == -EWOULDBLOCK) {
895 return err;
896 } else if (err != OK) {
897 if (err == INFO_DISCONTINUITY) {
898 int32_t type;
899 CHECK(accessUnit->meta()->findInt32("discontinuity", &type));
Andreas Huber53df1a42010-12-22 10:03:04 -0800900
Andreas Huber3fe62152011-09-16 15:09:22 -0700901 bool formatChange =
Andreas Huber6e3d3112011-11-28 12:36:11 -0800902 (audio &&
903 (type & ATSParser::DISCONTINUITY_AUDIO_FORMAT))
904 || (!audio &&
905 (type & ATSParser::DISCONTINUITY_VIDEO_FORMAT));
Andreas Huber53df1a42010-12-22 10:03:04 -0800906
Andreas Huber6e3d3112011-11-28 12:36:11 -0800907 bool timeChange = (type & ATSParser::DISCONTINUITY_TIME) != 0;
908
Steve Blockdf64d152012-01-04 20:05:49 +0000909 ALOGI("%s discontinuity (formatChange=%d, time=%d)",
Andreas Huber6e3d3112011-11-28 12:36:11 -0800910 audio ? "audio" : "video", formatChange, timeChange);
Andreas Huber32f3cef2011-03-02 15:34:46 -0800911
Andreas Huber3fe62152011-09-16 15:09:22 -0700912 if (audio) {
913 mSkipRenderingAudioUntilMediaTimeUs = -1;
914 } else {
915 mSkipRenderingVideoUntilMediaTimeUs = -1;
916 }
Andreas Huber32f3cef2011-03-02 15:34:46 -0800917
Andreas Huber6e3d3112011-11-28 12:36:11 -0800918 if (timeChange) {
919 sp<AMessage> extra;
920 if (accessUnit->meta()->findMessage("extra", &extra)
921 && extra != NULL) {
922 int64_t resumeAtMediaTimeUs;
923 if (extra->findInt64(
924 "resume-at-mediatimeUs", &resumeAtMediaTimeUs)) {
Steve Blockdf64d152012-01-04 20:05:49 +0000925 ALOGI("suppressing rendering of %s until %lld us",
Andreas Huber6e3d3112011-11-28 12:36:11 -0800926 audio ? "audio" : "video", resumeAtMediaTimeUs);
Andreas Huber3fe62152011-09-16 15:09:22 -0700927
Andreas Huber6e3d3112011-11-28 12:36:11 -0800928 if (audio) {
929 mSkipRenderingAudioUntilMediaTimeUs =
930 resumeAtMediaTimeUs;
931 } else {
932 mSkipRenderingVideoUntilMediaTimeUs =
933 resumeAtMediaTimeUs;
934 }
Andreas Huber3fe62152011-09-16 15:09:22 -0700935 }
Andreas Huber32f3cef2011-03-02 15:34:46 -0800936 }
937 }
Andreas Huber3fe62152011-09-16 15:09:22 -0700938
Andreas Huber6e3d3112011-11-28 12:36:11 -0800939 mTimeDiscontinuityPending =
940 mTimeDiscontinuityPending || timeChange;
941
942 if (formatChange || timeChange) {
Andreas Hubera1f8ab02012-11-30 10:53:22 -0800943 if (mFlushingAudio == NONE && mFlushingVideo == NONE) {
944 // And we'll resume scanning sources once we're done
945 // flushing.
946 mDeferredActions.push_front(
947 new SimpleAction(
948 &NuPlayer::performScanSources));
949 }
950
Andreas Huber6e3d3112011-11-28 12:36:11 -0800951 flushDecoder(audio, formatChange);
952 } else {
953 // This stream is unaffected by the discontinuity
954
955 if (audio) {
956 mFlushingAudio = FLUSHED;
957 } else {
958 mFlushingVideo = FLUSHED;
959 }
960
961 finishFlushIfPossible();
962
963 return -EWOULDBLOCK;
964 }
Andreas Huber32f3cef2011-03-02 15:34:46 -0800965 }
966
Andreas Huber3fe62152011-09-16 15:09:22 -0700967 reply->setInt32("err", err);
968 reply->post();
969 return OK;
Andreas Huberf9334412010-12-15 15:17:42 -0800970 }
971
Andreas Huber3fe62152011-09-16 15:09:22 -0700972 if (!audio) {
973 ++mNumFramesTotal;
974 }
975
976 dropAccessUnit = false;
977 if (!audio
978 && mVideoLateByUs > 100000ll
979 && mVideoIsAVC
980 && !IsAVCReferenceFrame(accessUnit)) {
981 dropAccessUnit = true;
982 ++mNumFramesDropped;
983 }
984 } while (dropAccessUnit);
Andreas Huberf9334412010-12-15 15:17:42 -0800985
Steve Block3856b092011-10-20 11:56:00 +0100986 // ALOGV("returned a valid buffer of %s data", audio ? "audio" : "video");
Andreas Huberf9334412010-12-15 15:17:42 -0800987
988#if 0
989 int64_t mediaTimeUs;
990 CHECK(accessUnit->meta()->findInt64("timeUs", &mediaTimeUs));
Steve Block3856b092011-10-20 11:56:00 +0100991 ALOGV("feeding %s input buffer at media time %.2f secs",
Andreas Huberf9334412010-12-15 15:17:42 -0800992 audio ? "audio" : "video",
993 mediaTimeUs / 1E6);
994#endif
995
Andreas Huber2d8bedd2012-02-21 14:38:23 -0800996 reply->setBuffer("buffer", accessUnit);
Andreas Huberf9334412010-12-15 15:17:42 -0800997 reply->post();
998
999 return OK;
1000}
1001
1002void NuPlayer::renderBuffer(bool audio, const sp<AMessage> &msg) {
Steve Block3856b092011-10-20 11:56:00 +01001003 // ALOGV("renderBuffer %s", audio ? "audio" : "video");
Andreas Huberf9334412010-12-15 15:17:42 -08001004
1005 sp<AMessage> reply;
1006 CHECK(msg->findMessage("reply", &reply));
1007
Andreas Huber18ac5402011-08-31 15:04:25 -07001008 if (IsFlushingState(audio ? mFlushingAudio : mFlushingVideo)) {
1009 // We're currently attempting to flush the decoder, in order
1010 // to complete this, the decoder wants all its buffers back,
1011 // so we don't want any output buffers it sent us (from before
1012 // we initiated the flush) to be stuck in the renderer's queue.
1013
Steve Block3856b092011-10-20 11:56:00 +01001014 ALOGV("we're still flushing the %s decoder, sending its output buffer"
Andreas Huber18ac5402011-08-31 15:04:25 -07001015 " right back.", audio ? "audio" : "video");
1016
1017 reply->post();
1018 return;
1019 }
1020
Andreas Huber2d8bedd2012-02-21 14:38:23 -08001021 sp<ABuffer> buffer;
1022 CHECK(msg->findBuffer("buffer", &buffer));
Andreas Huberf9334412010-12-15 15:17:42 -08001023
Andreas Huber32f3cef2011-03-02 15:34:46 -08001024 int64_t &skipUntilMediaTimeUs =
1025 audio
1026 ? mSkipRenderingAudioUntilMediaTimeUs
1027 : mSkipRenderingVideoUntilMediaTimeUs;
1028
1029 if (skipUntilMediaTimeUs >= 0) {
1030 int64_t mediaTimeUs;
1031 CHECK(buffer->meta()->findInt64("timeUs", &mediaTimeUs));
1032
1033 if (mediaTimeUs < skipUntilMediaTimeUs) {
Steve Block3856b092011-10-20 11:56:00 +01001034 ALOGV("dropping %s buffer at time %lld as requested.",
Andreas Huber32f3cef2011-03-02 15:34:46 -08001035 audio ? "audio" : "video",
1036 mediaTimeUs);
1037
1038 reply->post();
1039 return;
1040 }
1041
1042 skipUntilMediaTimeUs = -1;
1043 }
1044
Andreas Huberf9334412010-12-15 15:17:42 -08001045 mRenderer->queueBuffer(audio, buffer, reply);
1046}
1047
1048void NuPlayer::notifyListener(int msg, int ext1, int ext2) {
Andreas Huber43c3e6c2011-01-05 12:17:08 -08001049 if (mDriver == NULL) {
Andreas Huberf9334412010-12-15 15:17:42 -08001050 return;
1051 }
1052
Andreas Huber43c3e6c2011-01-05 12:17:08 -08001053 sp<NuPlayerDriver> driver = mDriver.promote();
Andreas Huberf9334412010-12-15 15:17:42 -08001054
Andreas Huber43c3e6c2011-01-05 12:17:08 -08001055 if (driver == NULL) {
Andreas Huberf9334412010-12-15 15:17:42 -08001056 return;
1057 }
1058
Andreas Hubera4af2142011-10-26 15:23:31 -07001059 driver->notifyListener(msg, ext1, ext2);
Andreas Huberf9334412010-12-15 15:17:42 -08001060}
1061
Andreas Huber1aef2112011-01-04 14:01:29 -08001062void NuPlayer::flushDecoder(bool audio, bool needShutdown) {
Andreas Huber14f76722013-01-15 09:04:18 -08001063 ALOGV("[%s] flushDecoder needShutdown=%d",
1064 audio ? "audio" : "video", needShutdown);
1065
Andreas Huber6e3d3112011-11-28 12:36:11 -08001066 if ((audio && mAudioDecoder == NULL) || (!audio && mVideoDecoder == NULL)) {
Steve Blockdf64d152012-01-04 20:05:49 +00001067 ALOGI("flushDecoder %s without decoder present",
Andreas Huber6e3d3112011-11-28 12:36:11 -08001068 audio ? "audio" : "video");
1069 }
1070
Andreas Huber1aef2112011-01-04 14:01:29 -08001071 // Make sure we don't continue to scan sources until we finish flushing.
1072 ++mScanSourcesGeneration;
Andreas Huber43c3e6c2011-01-05 12:17:08 -08001073 mScanSourcesPending = false;
Andreas Huber1aef2112011-01-04 14:01:29 -08001074
1075 (audio ? mAudioDecoder : mVideoDecoder)->signalFlush();
1076 mRenderer->flush(audio);
1077
1078 FlushStatus newStatus =
1079 needShutdown ? FLUSHING_DECODER_SHUTDOWN : FLUSHING_DECODER;
1080
1081 if (audio) {
1082 CHECK(mFlushingAudio == NONE
1083 || mFlushingAudio == AWAITING_DISCONTINUITY);
1084
1085 mFlushingAudio = newStatus;
1086
1087 if (mFlushingVideo == NONE) {
1088 mFlushingVideo = (mVideoDecoder != NULL)
1089 ? AWAITING_DISCONTINUITY
1090 : FLUSHED;
1091 }
1092 } else {
1093 CHECK(mFlushingVideo == NONE
1094 || mFlushingVideo == AWAITING_DISCONTINUITY);
1095
1096 mFlushingVideo = newStatus;
1097
1098 if (mFlushingAudio == NONE) {
1099 mFlushingAudio = (mAudioDecoder != NULL)
1100 ? AWAITING_DISCONTINUITY
1101 : FLUSHED;
1102 }
1103 }
1104}
1105
Andreas Huber84066782011-08-16 09:34:26 -07001106sp<AMessage> NuPlayer::Source::getFormat(bool audio) {
1107 sp<MetaData> meta = getFormatMeta(audio);
1108
1109 if (meta == NULL) {
1110 return NULL;
1111 }
1112
1113 sp<AMessage> msg = new AMessage;
1114
1115 if(convertMetaDataToMessage(meta, &msg) == OK) {
1116 return msg;
1117 }
1118 return NULL;
1119}
1120
James Dong0d268a32012-08-31 12:18:27 -07001121status_t NuPlayer::setVideoScalingMode(int32_t mode) {
1122 mVideoScalingMode = mode;
Andreas Huber57a339c2012-12-03 11:18:00 -08001123 if (mNativeWindow != NULL) {
James Dong0d268a32012-08-31 12:18:27 -07001124 status_t ret = native_window_set_scaling_mode(
1125 mNativeWindow->getNativeWindow().get(), mVideoScalingMode);
1126 if (ret != OK) {
1127 ALOGE("Failed to set scaling mode (%d): %s",
1128 -ret, strerror(-ret));
1129 return ret;
1130 }
1131 }
1132 return OK;
1133}
1134
Andreas Huberb7c8e912012-11-27 15:02:53 -08001135void NuPlayer::schedulePollDuration() {
1136 sp<AMessage> msg = new AMessage(kWhatPollDuration, id());
1137 msg->setInt32("generation", mPollDurationGeneration);
1138 msg->post();
1139}
1140
1141void NuPlayer::cancelPollDuration() {
1142 ++mPollDurationGeneration;
1143}
1144
Andreas Hubera1f8ab02012-11-30 10:53:22 -08001145void NuPlayer::processDeferredActions() {
1146 while (!mDeferredActions.empty()) {
1147 // We won't execute any deferred actions until we're no longer in
1148 // an intermediate state, i.e. one more more decoders are currently
1149 // flushing or shutting down.
1150
1151 if (mRenderer != NULL) {
1152 // There's an edge case where the renderer owns all output
1153 // buffers and is paused, therefore the decoder will not read
1154 // more input data and will never encounter the matching
1155 // discontinuity. To avoid this, we resume the renderer.
1156
1157 if (mFlushingAudio == AWAITING_DISCONTINUITY
1158 || mFlushingVideo == AWAITING_DISCONTINUITY) {
1159 mRenderer->resume();
1160 }
1161 }
1162
1163 if (mFlushingAudio != NONE || mFlushingVideo != NONE) {
1164 // We're currently flushing, postpone the reset until that's
1165 // completed.
1166
1167 ALOGV("postponing action mFlushingAudio=%d, mFlushingVideo=%d",
1168 mFlushingAudio, mFlushingVideo);
1169
1170 break;
1171 }
1172
1173 sp<Action> action = *mDeferredActions.begin();
1174 mDeferredActions.erase(mDeferredActions.begin());
1175
1176 action->execute(this);
1177 }
1178}
1179
1180void NuPlayer::performSeek(int64_t seekTimeUs) {
1181 ALOGV("performSeek seekTimeUs=%lld us (%.2f secs)",
1182 seekTimeUs,
1183 seekTimeUs / 1E6);
1184
1185 mSource->seekTo(seekTimeUs);
1186
1187 if (mDriver != NULL) {
1188 sp<NuPlayerDriver> driver = mDriver.promote();
1189 if (driver != NULL) {
1190 driver->notifyPosition(seekTimeUs);
1191 driver->notifySeekComplete();
1192 }
1193 }
1194
1195 // everything's flushed, continue playback.
1196}
1197
1198void NuPlayer::performDecoderFlush() {
1199 ALOGV("performDecoderFlush");
1200
Andreas Huberda9740e2013-04-16 10:54:03 -07001201 if (mAudioDecoder == NULL && mVideoDecoder == NULL) {
Andreas Hubera1f8ab02012-11-30 10:53:22 -08001202 return;
1203 }
1204
1205 mTimeDiscontinuityPending = true;
1206
1207 if (mAudioDecoder != NULL) {
1208 flushDecoder(true /* audio */, false /* needShutdown */);
1209 }
1210
1211 if (mVideoDecoder != NULL) {
1212 flushDecoder(false /* audio */, false /* needShutdown */);
1213 }
1214}
1215
Andreas Huber14f76722013-01-15 09:04:18 -08001216void NuPlayer::performDecoderShutdown(bool audio, bool video) {
1217 ALOGV("performDecoderShutdown audio=%d, video=%d", audio, video);
Andreas Hubera1f8ab02012-11-30 10:53:22 -08001218
Andreas Huber14f76722013-01-15 09:04:18 -08001219 if ((!audio || mAudioDecoder == NULL)
1220 && (!video || mVideoDecoder == NULL)) {
Andreas Hubera1f8ab02012-11-30 10:53:22 -08001221 return;
1222 }
1223
1224 mTimeDiscontinuityPending = true;
1225
Andreas Huber14f76722013-01-15 09:04:18 -08001226 if (mFlushingAudio == NONE && (!audio || mAudioDecoder == NULL)) {
1227 mFlushingAudio = FLUSHED;
1228 }
1229
1230 if (mFlushingVideo == NONE && (!video || mVideoDecoder == NULL)) {
1231 mFlushingVideo = FLUSHED;
1232 }
1233
1234 if (audio && mAudioDecoder != NULL) {
Andreas Hubera1f8ab02012-11-30 10:53:22 -08001235 flushDecoder(true /* audio */, true /* needShutdown */);
1236 }
1237
Andreas Huber14f76722013-01-15 09:04:18 -08001238 if (video && mVideoDecoder != NULL) {
Andreas Hubera1f8ab02012-11-30 10:53:22 -08001239 flushDecoder(false /* audio */, true /* needShutdown */);
1240 }
1241}
1242
1243void NuPlayer::performReset() {
1244 ALOGV("performReset");
1245
1246 CHECK(mAudioDecoder == NULL);
1247 CHECK(mVideoDecoder == NULL);
1248
1249 cancelPollDuration();
1250
1251 ++mScanSourcesGeneration;
1252 mScanSourcesPending = false;
1253
1254 mRenderer.clear();
1255
1256 if (mSource != NULL) {
1257 mSource->stop();
Andreas Huberb5f25f02013-02-05 10:14:26 -08001258
1259 looper()->unregisterHandler(mSource->id());
1260
Andreas Hubera1f8ab02012-11-30 10:53:22 -08001261 mSource.clear();
1262 }
1263
1264 if (mDriver != NULL) {
1265 sp<NuPlayerDriver> driver = mDriver.promote();
1266 if (driver != NULL) {
1267 driver->notifyResetComplete();
1268 }
1269 }
Andreas Huber57a339c2012-12-03 11:18:00 -08001270
1271 mStarted = false;
Andreas Hubera1f8ab02012-11-30 10:53:22 -08001272}
1273
1274void NuPlayer::performScanSources() {
1275 ALOGV("performScanSources");
1276
Andreas Huber57a339c2012-12-03 11:18:00 -08001277 if (!mStarted) {
1278 return;
1279 }
1280
Andreas Hubera1f8ab02012-11-30 10:53:22 -08001281 if (mAudioDecoder == NULL || mVideoDecoder == NULL) {
1282 postScanSources();
1283 }
1284}
1285
Andreas Huber57a339c2012-12-03 11:18:00 -08001286void NuPlayer::performSetSurface(const sp<NativeWindowWrapper> &wrapper) {
1287 ALOGV("performSetSurface");
1288
1289 mNativeWindow = wrapper;
1290
1291 // XXX - ignore error from setVideoScalingMode for now
1292 setVideoScalingMode(mVideoScalingMode);
1293
1294 if (mDriver != NULL) {
1295 sp<NuPlayerDriver> driver = mDriver.promote();
1296 if (driver != NULL) {
1297 driver->notifySetSurfaceComplete();
1298 }
1299 }
1300}
1301
Andreas Huber9575c962013-02-05 13:59:56 -08001302void NuPlayer::onSourceNotify(const sp<AMessage> &msg) {
1303 int32_t what;
1304 CHECK(msg->findInt32("what", &what));
1305
1306 switch (what) {
1307 case Source::kWhatPrepared:
1308 {
Andreas Huberb5f28d42013-04-25 15:11:19 -07001309 if (mSource == NULL) {
1310 // This is a stale notification from a source that was
1311 // asynchronously preparing when the client called reset().
1312 // We handled the reset, the source is gone.
1313 break;
1314 }
1315
Andreas Huberec0c5972013-02-05 14:47:13 -08001316 int32_t err;
1317 CHECK(msg->findInt32("err", &err));
1318
Andreas Huber9575c962013-02-05 13:59:56 -08001319 sp<NuPlayerDriver> driver = mDriver.promote();
1320 if (driver != NULL) {
Andreas Huberec0c5972013-02-05 14:47:13 -08001321 driver->notifyPrepareCompleted(err);
Andreas Huber9575c962013-02-05 13:59:56 -08001322 }
Andreas Huber99759402013-04-01 14:28:31 -07001323
1324 int64_t durationUs;
1325 if (mDriver != NULL && mSource->getDuration(&durationUs) == OK) {
1326 sp<NuPlayerDriver> driver = mDriver.promote();
1327 if (driver != NULL) {
1328 driver->notifyDuration(durationUs);
1329 }
1330 }
Andreas Huber9575c962013-02-05 13:59:56 -08001331 break;
1332 }
1333
1334 case Source::kWhatFlagsChanged:
1335 {
1336 uint32_t flags;
1337 CHECK(msg->findInt32("flags", (int32_t *)&flags));
1338
1339 if ((mSourceFlags & Source::FLAG_DYNAMIC_DURATION)
1340 && (!(flags & Source::FLAG_DYNAMIC_DURATION))) {
1341 cancelPollDuration();
1342 } else if (!(mSourceFlags & Source::FLAG_DYNAMIC_DURATION)
1343 && (flags & Source::FLAG_DYNAMIC_DURATION)
1344 && (mAudioDecoder != NULL || mVideoDecoder != NULL)) {
1345 schedulePollDuration();
1346 }
1347
1348 mSourceFlags = flags;
1349 break;
1350 }
1351
1352 case Source::kWhatVideoSizeChanged:
1353 {
1354 int32_t width, height;
1355 CHECK(msg->findInt32("width", &width));
1356 CHECK(msg->findInt32("height", &height));
1357
1358 notifyListener(MEDIA_SET_VIDEO_SIZE, width, height);
1359 break;
1360 }
1361
Roger Jönssonb50e83e2013-01-21 16:26:41 +01001362 case Source::kWhatBufferingStart:
1363 {
1364 notifyListener(MEDIA_INFO, MEDIA_INFO_BUFFERING_START, 0);
1365 break;
1366 }
1367
1368 case Source::kWhatBufferingEnd:
1369 {
1370 notifyListener(MEDIA_INFO, MEDIA_INFO_BUFFERING_END, 0);
1371 break;
1372 }
1373
Andreas Huber14f76722013-01-15 09:04:18 -08001374 case Source::kWhatQueueDecoderShutdown:
1375 {
1376 int32_t audio, video;
1377 CHECK(msg->findInt32("audio", &audio));
1378 CHECK(msg->findInt32("video", &video));
1379
1380 sp<AMessage> reply;
1381 CHECK(msg->findMessage("reply", &reply));
1382
1383 queueDecoderShutdown(audio, video, reply);
1384 break;
1385 }
1386
Andreas Huber9575c962013-02-05 13:59:56 -08001387 default:
1388 TRESPASS();
1389 }
1390}
1391
Andreas Huberb5f25f02013-02-05 10:14:26 -08001392////////////////////////////////////////////////////////////////////////////////
1393
Andreas Huber9575c962013-02-05 13:59:56 -08001394void NuPlayer::Source::notifyFlagsChanged(uint32_t flags) {
1395 sp<AMessage> notify = dupNotify();
1396 notify->setInt32("what", kWhatFlagsChanged);
1397 notify->setInt32("flags", flags);
1398 notify->post();
1399}
1400
1401void NuPlayer::Source::notifyVideoSizeChanged(int32_t width, int32_t height) {
1402 sp<AMessage> notify = dupNotify();
1403 notify->setInt32("what", kWhatVideoSizeChanged);
1404 notify->setInt32("width", width);
1405 notify->setInt32("height", height);
1406 notify->post();
1407}
1408
Andreas Huberec0c5972013-02-05 14:47:13 -08001409void NuPlayer::Source::notifyPrepared(status_t err) {
Andreas Huber9575c962013-02-05 13:59:56 -08001410 sp<AMessage> notify = dupNotify();
1411 notify->setInt32("what", kWhatPrepared);
Andreas Huberec0c5972013-02-05 14:47:13 -08001412 notify->setInt32("err", err);
Andreas Huber9575c962013-02-05 13:59:56 -08001413 notify->post();
1414}
1415
Andreas Huberb5f25f02013-02-05 10:14:26 -08001416void NuPlayer::Source::onMessageReceived(const sp<AMessage> &msg) {
1417 TRESPASS();
1418}
1419
Andreas Huber14f76722013-01-15 09:04:18 -08001420void NuPlayer::queueDecoderShutdown(
1421 bool audio, bool video, const sp<AMessage> &reply) {
1422 ALOGI("queueDecoderShutdown audio=%d, video=%d", audio, video);
1423
1424 mDeferredActions.push_back(
1425 new ShutdownDecoderAction(audio, video));
1426
1427 mDeferredActions.push_back(
1428 new SimpleAction(&NuPlayer::performScanSources));
1429
1430 mDeferredActions.push_back(new PostMessageAction(reply));
1431
1432 processDeferredActions();
1433}
1434
Andreas Huberf9334412010-12-15 15:17:42 -08001435} // namespace android