blob: dc69f735d3b31d5faa29501326d2af60d70c3cfe [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 Huber5bc087c2010-12-23 10:27:40 -080031
32#include "ATSParser.h"
Andreas Huberf9334412010-12-15 15:17:42 -080033
Andreas Huber3831a062010-12-21 10:22:33 -080034#include <media/stagefright/foundation/hexdump.h>
Andreas Huberf9334412010-12-15 15:17:42 -080035#include <media/stagefright/foundation/ABuffer.h>
36#include <media/stagefright/foundation/ADebug.h>
37#include <media/stagefright/foundation/AMessage.h>
Andreas Huber3fe62152011-09-16 15:09:22 -070038#include <media/stagefright/MediaDefs.h>
Andreas Huberf9334412010-12-15 15:17:42 -080039#include <media/stagefright/MediaErrors.h>
40#include <media/stagefright/MetaData.h>
Andy McFadden8ba01022012-12-18 09:46:54 -080041#include <gui/IGraphicBufferProducer.h>
Andreas Huberf9334412010-12-15 15:17:42 -080042
Andreas Huber3fe62152011-09-16 15:09:22 -070043#include "avc_utils.h"
44
Andreas Huber84066782011-08-16 09:34:26 -070045#include "ESDS.h"
46#include <media/stagefright/Utils.h>
47
Andreas Huberf9334412010-12-15 15:17:42 -080048namespace android {
49
Andreas Hubera1f8ab02012-11-30 10:53:22 -080050struct NuPlayer::Action : public RefBase {
51 Action() {}
52
53 virtual void execute(NuPlayer *player) = 0;
54
55private:
56 DISALLOW_EVIL_CONSTRUCTORS(Action);
57};
58
59struct NuPlayer::SeekAction : public Action {
60 SeekAction(int64_t seekTimeUs)
61 : mSeekTimeUs(seekTimeUs) {
62 }
63
64 virtual void execute(NuPlayer *player) {
65 player->performSeek(mSeekTimeUs);
66 }
67
68private:
69 int64_t mSeekTimeUs;
70
71 DISALLOW_EVIL_CONSTRUCTORS(SeekAction);
72};
73
Andreas Huber57a339c2012-12-03 11:18:00 -080074struct NuPlayer::SetSurfaceAction : public Action {
75 SetSurfaceAction(const sp<NativeWindowWrapper> &wrapper)
76 : mWrapper(wrapper) {
77 }
78
79 virtual void execute(NuPlayer *player) {
80 player->performSetSurface(mWrapper);
81 }
82
83private:
84 sp<NativeWindowWrapper> mWrapper;
85
86 DISALLOW_EVIL_CONSTRUCTORS(SetSurfaceAction);
87};
88
Andreas Huber14f76722013-01-15 09:04:18 -080089struct NuPlayer::ShutdownDecoderAction : public Action {
90 ShutdownDecoderAction(bool audio, bool video)
91 : mAudio(audio),
92 mVideo(video) {
93 }
94
95 virtual void execute(NuPlayer *player) {
96 player->performDecoderShutdown(mAudio, mVideo);
97 }
98
99private:
100 bool mAudio;
101 bool mVideo;
102
103 DISALLOW_EVIL_CONSTRUCTORS(ShutdownDecoderAction);
104};
105
106struct NuPlayer::PostMessageAction : public Action {
107 PostMessageAction(const sp<AMessage> &msg)
108 : mMessage(msg) {
109 }
110
111 virtual void execute(NuPlayer *) {
112 mMessage->post();
113 }
114
115private:
116 sp<AMessage> mMessage;
117
118 DISALLOW_EVIL_CONSTRUCTORS(PostMessageAction);
119};
120
Andreas Hubera1f8ab02012-11-30 10:53:22 -0800121// Use this if there's no state necessary to save in order to execute
122// the action.
123struct NuPlayer::SimpleAction : public Action {
124 typedef void (NuPlayer::*ActionFunc)();
125
126 SimpleAction(ActionFunc func)
127 : mFunc(func) {
128 }
129
130 virtual void execute(NuPlayer *player) {
131 (player->*mFunc)();
132 }
133
134private:
135 ActionFunc mFunc;
136
137 DISALLOW_EVIL_CONSTRUCTORS(SimpleAction);
138};
139
Andreas Huberf9334412010-12-15 15:17:42 -0800140////////////////////////////////////////////////////////////////////////////////
141
142NuPlayer::NuPlayer()
Andreas Huber9b80c2b2011-06-30 15:47:02 -0700143 : mUIDValid(false),
Andreas Huber9575c962013-02-05 13:59:56 -0800144 mSourceFlags(0),
Andreas Huber3fe62152011-09-16 15:09:22 -0700145 mVideoIsAVC(false),
Andreas Huber9b80c2b2011-06-30 15:47:02 -0700146 mAudioEOS(false),
Andreas Huberf9334412010-12-15 15:17:42 -0800147 mVideoEOS(false),
Andreas Huber5bc087c2010-12-23 10:27:40 -0800148 mScanSourcesPending(false),
Andreas Huber1aef2112011-01-04 14:01:29 -0800149 mScanSourcesGeneration(0),
Andreas Huberb7c8e912012-11-27 15:02:53 -0800150 mPollDurationGeneration(0),
Andreas Huber6e3d3112011-11-28 12:36:11 -0800151 mTimeDiscontinuityPending(false),
Andreas Huberf9334412010-12-15 15:17:42 -0800152 mFlushingAudio(NONE),
Andreas Huber1aef2112011-01-04 14:01:29 -0800153 mFlushingVideo(NONE),
Andreas Huber3fe62152011-09-16 15:09:22 -0700154 mSkipRenderingAudioUntilMediaTimeUs(-1ll),
155 mSkipRenderingVideoUntilMediaTimeUs(-1ll),
156 mVideoLateByUs(0ll),
157 mNumFramesTotal(0ll),
James Dong0d268a32012-08-31 12:18:27 -0700158 mNumFramesDropped(0ll),
Andreas Huber57a339c2012-12-03 11:18:00 -0800159 mVideoScalingMode(NATIVE_WINDOW_SCALING_MODE_SCALE_TO_WINDOW),
160 mStarted(false) {
Andreas Huberf9334412010-12-15 15:17:42 -0800161}
162
163NuPlayer::~NuPlayer() {
164}
165
Andreas Huber9b80c2b2011-06-30 15:47:02 -0700166void NuPlayer::setUID(uid_t uid) {
167 mUIDValid = true;
168 mUID = uid;
169}
170
Andreas Huber43c3e6c2011-01-05 12:17:08 -0800171void NuPlayer::setDriver(const wp<NuPlayerDriver> &driver) {
172 mDriver = driver;
Andreas Huberf9334412010-12-15 15:17:42 -0800173}
174
Andreas Huber9575c962013-02-05 13:59:56 -0800175void NuPlayer::setDataSourceAsync(const sp<IStreamSource> &source) {
Andreas Huberf9334412010-12-15 15:17:42 -0800176 sp<AMessage> msg = new AMessage(kWhatSetDataSource, id());
177
Andreas Huberb5f25f02013-02-05 10:14:26 -0800178 sp<AMessage> notify = new AMessage(kWhatSourceNotify, id());
179
Andreas Huber240abcc2014-02-13 13:32:37 -0800180 msg->setObject("source", new StreamingSource(notify, source));
Andreas Huber5bc087c2010-12-23 10:27:40 -0800181 msg->post();
182}
Andreas Huberf9334412010-12-15 15:17:42 -0800183
Andreas Huberafed0e12011-09-20 15:39:58 -0700184static bool IsHTTPLiveURL(const char *url) {
185 if (!strncasecmp("http://", url, 7)
Andreas Huber99759402013-04-01 14:28:31 -0700186 || !strncasecmp("https://", url, 8)
187 || !strncasecmp("file://", url, 7)) {
Andreas Huberafed0e12011-09-20 15:39:58 -0700188 size_t len = strlen(url);
189 if (len >= 5 && !strcasecmp(".m3u8", &url[len - 5])) {
190 return true;
191 }
192
193 if (strstr(url,"m3u8")) {
194 return true;
195 }
196 }
197
198 return false;
199}
200
Andreas Huber9575c962013-02-05 13:59:56 -0800201void NuPlayer::setDataSourceAsync(
Andreas Huber1b86fe02014-01-29 11:13:26 -0800202 const sp<IMediaHTTPService> &httpService,
203 const char *url,
204 const KeyedVector<String8, String8> *headers) {
Andreas Huber5bc087c2010-12-23 10:27:40 -0800205 sp<AMessage> msg = new AMessage(kWhatSetDataSource, id());
Oscar Rydhé7a33b772012-02-20 10:15:48 +0100206 size_t len = strlen(url);
Andreas Huber5bc087c2010-12-23 10:27:40 -0800207
Andreas Huberb5f25f02013-02-05 10:14:26 -0800208 sp<AMessage> notify = new AMessage(kWhatSourceNotify, id());
209
Andreas Huberafed0e12011-09-20 15:39:58 -0700210 sp<Source> source;
211 if (IsHTTPLiveURL(url)) {
Andreas Huber81e68442014-02-05 11:52:33 -0800212 source = new HTTPLiveSource(notify, httpService, url, headers);
Andreas Huberafed0e12011-09-20 15:39:58 -0700213 } else if (!strncasecmp(url, "rtsp://", 7)) {
Andreas Huber1b86fe02014-01-29 11:13:26 -0800214 source = new RTSPSource(
215 notify, httpService, url, headers, mUIDValid, mUID);
Oscar Rydhé7a33b772012-02-20 10:15:48 +0100216 } else if ((!strncasecmp(url, "http://", 7)
217 || !strncasecmp(url, "https://", 8))
218 && ((len >= 4 && !strcasecmp(".sdp", &url[len - 4]))
219 || strstr(url, ".sdp?"))) {
Andreas Huber1b86fe02014-01-29 11:13:26 -0800220 source = new RTSPSource(
221 notify, httpService, url, headers, mUIDValid, mUID, true);
Andreas Huber2bfdd422011-10-11 15:24:07 -0700222 } else {
Andreas Huber81e68442014-02-05 11:52:33 -0800223 source = new GenericSource(notify, httpService, url, headers);
Andreas Huber2bfdd422011-10-11 15:24:07 -0700224 }
225
Andreas Huberafed0e12011-09-20 15:39:58 -0700226 msg->setObject("source", source);
227 msg->post();
228}
229
Andreas Huber9575c962013-02-05 13:59:56 -0800230void NuPlayer::setDataSourceAsync(int fd, int64_t offset, int64_t length) {
Andreas Huberafed0e12011-09-20 15:39:58 -0700231 sp<AMessage> msg = new AMessage(kWhatSetDataSource, id());
232
Andreas Huberb5f25f02013-02-05 10:14:26 -0800233 sp<AMessage> notify = new AMessage(kWhatSourceNotify, id());
234
235 sp<Source> source = new GenericSource(notify, fd, offset, length);
Andreas Huberafed0e12011-09-20 15:39:58 -0700236 msg->setObject("source", source);
Andreas Huberf9334412010-12-15 15:17:42 -0800237 msg->post();
238}
239
Andreas Huber9575c962013-02-05 13:59:56 -0800240void NuPlayer::prepareAsync() {
241 (new AMessage(kWhatPrepare, id()))->post();
242}
243
Andreas Huber57a339c2012-12-03 11:18:00 -0800244void NuPlayer::setVideoSurfaceTextureAsync(
Andy McFadden8ba01022012-12-18 09:46:54 -0800245 const sp<IGraphicBufferProducer> &bufferProducer) {
Glenn Kasten11731182011-02-08 17:26:17 -0800246 sp<AMessage> msg = new AMessage(kWhatSetVideoNativeWindow, id());
Andreas Huber57a339c2012-12-03 11:18:00 -0800247
Andy McFadden8ba01022012-12-18 09:46:54 -0800248 if (bufferProducer == NULL) {
Andreas Huber57a339c2012-12-03 11:18:00 -0800249 msg->setObject("native-window", NULL);
250 } else {
251 msg->setObject(
252 "native-window",
253 new NativeWindowWrapper(
Mathias Agopian1a2952a2013-02-14 17:11:27 -0800254 new Surface(bufferProducer)));
Andreas Huber57a339c2012-12-03 11:18:00 -0800255 }
256
Andreas Huberf9334412010-12-15 15:17:42 -0800257 msg->post();
258}
259
260void NuPlayer::setAudioSink(const sp<MediaPlayerBase::AudioSink> &sink) {
261 sp<AMessage> msg = new AMessage(kWhatSetAudioSink, id());
262 msg->setObject("sink", sink);
263 msg->post();
264}
265
266void NuPlayer::start() {
267 (new AMessage(kWhatStart, id()))->post();
268}
269
Andreas Huber43c3e6c2011-01-05 12:17:08 -0800270void NuPlayer::pause() {
Andreas Huberb4082222011-01-20 15:23:04 -0800271 (new AMessage(kWhatPause, id()))->post();
Andreas Huber43c3e6c2011-01-05 12:17:08 -0800272}
273
274void NuPlayer::resume() {
Andreas Huberb4082222011-01-20 15:23:04 -0800275 (new AMessage(kWhatResume, id()))->post();
Andreas Huber43c3e6c2011-01-05 12:17:08 -0800276}
277
Andreas Huber1aef2112011-01-04 14:01:29 -0800278void NuPlayer::resetAsync() {
279 (new AMessage(kWhatReset, id()))->post();
280}
281
Andreas Huber43c3e6c2011-01-05 12:17:08 -0800282void NuPlayer::seekToAsync(int64_t seekTimeUs) {
283 sp<AMessage> msg = new AMessage(kWhatSeek, id());
284 msg->setInt64("seekTimeUs", seekTimeUs);
285 msg->post();
286}
287
Andreas Huber53df1a42010-12-22 10:03:04 -0800288// static
Andreas Huber1aef2112011-01-04 14:01:29 -0800289bool NuPlayer::IsFlushingState(FlushStatus state, bool *needShutdown) {
Andreas Huber53df1a42010-12-22 10:03:04 -0800290 switch (state) {
291 case FLUSHING_DECODER:
Andreas Huber1aef2112011-01-04 14:01:29 -0800292 if (needShutdown != NULL) {
293 *needShutdown = false;
Andreas Huber53df1a42010-12-22 10:03:04 -0800294 }
295 return true;
296
Andreas Huber1aef2112011-01-04 14:01:29 -0800297 case FLUSHING_DECODER_SHUTDOWN:
298 if (needShutdown != NULL) {
299 *needShutdown = true;
Andreas Huber53df1a42010-12-22 10:03:04 -0800300 }
301 return true;
302
303 default:
304 return false;
305 }
306}
307
Chong Zhang404fced2014-06-11 14:45:31 -0700308void NuPlayer::writeTrackInfo(
309 Parcel* reply, const sp<AMessage> format) const {
310 int32_t trackType;
311 CHECK(format->findInt32("type", &trackType));
312
313 AString lang;
314 CHECK(format->findString("language", &lang));
315
316 reply->writeInt32(2); // write something non-zero
317 reply->writeInt32(trackType);
318 reply->writeString16(String16(lang.c_str()));
319
320 if (trackType == MEDIA_TRACK_TYPE_SUBTITLE) {
321 AString mime;
322 CHECK(format->findString("mime", &mime));
323
324 int32_t isAuto, isDefault, isForced;
325 CHECK(format->findInt32("auto", &isAuto));
326 CHECK(format->findInt32("default", &isDefault));
327 CHECK(format->findInt32("forced", &isForced));
328
329 reply->writeString16(String16(mime.c_str()));
330 reply->writeInt32(isAuto);
331 reply->writeInt32(isDefault);
332 reply->writeInt32(isForced);
333 }
334}
335
Andreas Huberf9334412010-12-15 15:17:42 -0800336void NuPlayer::onMessageReceived(const sp<AMessage> &msg) {
337 switch (msg->what()) {
338 case kWhatSetDataSource:
339 {
Steve Block3856b092011-10-20 11:56:00 +0100340 ALOGV("kWhatSetDataSource");
Andreas Huberf9334412010-12-15 15:17:42 -0800341
342 CHECK(mSource == NULL);
343
Andreas Huber5bc087c2010-12-23 10:27:40 -0800344 sp<RefBase> obj;
345 CHECK(msg->findObject("source", &obj));
Andreas Huberf9334412010-12-15 15:17:42 -0800346
Andreas Huber5bc087c2010-12-23 10:27:40 -0800347 mSource = static_cast<Source *>(obj.get());
Andreas Huberb5f25f02013-02-05 10:14:26 -0800348
349 looper()->registerHandler(mSource);
Andreas Huber9575c962013-02-05 13:59:56 -0800350
351 CHECK(mDriver != NULL);
352 sp<NuPlayerDriver> driver = mDriver.promote();
353 if (driver != NULL) {
354 driver->notifySetDataSourceCompleted(OK);
355 }
356 break;
357 }
358
359 case kWhatPrepare:
360 {
361 mSource->prepareAsync();
Andreas Huberf9334412010-12-15 15:17:42 -0800362 break;
363 }
364
Chong Zhangdcb89b32013-08-06 09:44:47 -0700365 case kWhatGetTrackInfo:
366 {
367 uint32_t replyID;
368 CHECK(msg->senderAwaitsResponse(&replyID));
369
Chong Zhang404fced2014-06-11 14:45:31 -0700370 Parcel* reply;
371 CHECK(msg->findPointer("reply", (void**)&reply));
372
373 size_t inbandTracks = 0;
Chong Zhangdcb89b32013-08-06 09:44:47 -0700374 if (mSource != NULL) {
Chong Zhang404fced2014-06-11 14:45:31 -0700375 inbandTracks = mSource->getTrackCount();
376 }
377
378 // total track count
379 reply->writeInt32(inbandTracks);
380
381 // write inband tracks
382 for (size_t i = 0; i < inbandTracks; ++i) {
383 writeTrackInfo(reply, mSource->getTrackInfo(i));
Chong Zhangdcb89b32013-08-06 09:44:47 -0700384 }
385
386 sp<AMessage> response = new AMessage;
Chong Zhangdcb89b32013-08-06 09:44:47 -0700387 response->postReply(replyID);
388 break;
389 }
390
391 case kWhatSelectTrack:
392 {
393 uint32_t replyID;
394 CHECK(msg->senderAwaitsResponse(&replyID));
395
Chong Zhang404fced2014-06-11 14:45:31 -0700396 size_t trackIndex;
397 int32_t select;
398 CHECK(msg->findSize("trackIndex", &trackIndex));
399 CHECK(msg->findInt32("select", &select));
400
Chong Zhangdcb89b32013-08-06 09:44:47 -0700401 status_t err = INVALID_OPERATION;
Chong Zhang404fced2014-06-11 14:45:31 -0700402
403 size_t inbandTracks = 0;
Chong Zhangdcb89b32013-08-06 09:44:47 -0700404 if (mSource != NULL) {
Chong Zhang404fced2014-06-11 14:45:31 -0700405 inbandTracks = mSource->getTrackCount();
406 }
407
408 if (trackIndex < inbandTracks) {
Chong Zhangdcb89b32013-08-06 09:44:47 -0700409 err = mSource->selectTrack(trackIndex, select);
410 }
411
412 sp<AMessage> response = new AMessage;
413 response->setInt32("err", err);
414
415 response->postReply(replyID);
416 break;
417 }
418
Andreas Huberb7c8e912012-11-27 15:02:53 -0800419 case kWhatPollDuration:
420 {
421 int32_t generation;
422 CHECK(msg->findInt32("generation", &generation));
423
424 if (generation != mPollDurationGeneration) {
425 // stale
426 break;
427 }
428
429 int64_t durationUs;
430 if (mDriver != NULL && mSource->getDuration(&durationUs) == OK) {
431 sp<NuPlayerDriver> driver = mDriver.promote();
432 if (driver != NULL) {
433 driver->notifyDuration(durationUs);
434 }
435 }
436
437 msg->post(1000000ll); // poll again in a second.
438 break;
439 }
440
Glenn Kasten11731182011-02-08 17:26:17 -0800441 case kWhatSetVideoNativeWindow:
Andreas Huberf9334412010-12-15 15:17:42 -0800442 {
Steve Block3856b092011-10-20 11:56:00 +0100443 ALOGV("kWhatSetVideoNativeWindow");
Andreas Huberf9334412010-12-15 15:17:42 -0800444
Andreas Huber57a339c2012-12-03 11:18:00 -0800445 mDeferredActions.push_back(
Andreas Huber14f76722013-01-15 09:04:18 -0800446 new ShutdownDecoderAction(
447 false /* audio */, true /* video */));
Andreas Huber57a339c2012-12-03 11:18:00 -0800448
Andreas Huberf9334412010-12-15 15:17:42 -0800449 sp<RefBase> obj;
Glenn Kasten11731182011-02-08 17:26:17 -0800450 CHECK(msg->findObject("native-window", &obj));
Andreas Huberf9334412010-12-15 15:17:42 -0800451
Andreas Huber57a339c2012-12-03 11:18:00 -0800452 mDeferredActions.push_back(
453 new SetSurfaceAction(
454 static_cast<NativeWindowWrapper *>(obj.get())));
James Dong0d268a32012-08-31 12:18:27 -0700455
Andreas Huber57a339c2012-12-03 11:18:00 -0800456 if (obj != NULL) {
457 // If there is a new surface texture, instantiate decoders
458 // again if possible.
459 mDeferredActions.push_back(
460 new SimpleAction(&NuPlayer::performScanSources));
461 }
462
463 processDeferredActions();
Andreas Huberf9334412010-12-15 15:17:42 -0800464 break;
465 }
466
467 case kWhatSetAudioSink:
468 {
Steve Block3856b092011-10-20 11:56:00 +0100469 ALOGV("kWhatSetAudioSink");
Andreas Huberf9334412010-12-15 15:17:42 -0800470
471 sp<RefBase> obj;
472 CHECK(msg->findObject("sink", &obj));
473
474 mAudioSink = static_cast<MediaPlayerBase::AudioSink *>(obj.get());
475 break;
476 }
477
478 case kWhatStart:
479 {
Steve Block3856b092011-10-20 11:56:00 +0100480 ALOGV("kWhatStart");
Andreas Huber43c3e6c2011-01-05 12:17:08 -0800481
Andreas Huber3fe62152011-09-16 15:09:22 -0700482 mVideoIsAVC = false;
Andreas Huber1aef2112011-01-04 14:01:29 -0800483 mAudioEOS = false;
484 mVideoEOS = false;
Andreas Huber32f3cef2011-03-02 15:34:46 -0800485 mSkipRenderingAudioUntilMediaTimeUs = -1;
486 mSkipRenderingVideoUntilMediaTimeUs = -1;
Andreas Huber3fe62152011-09-16 15:09:22 -0700487 mVideoLateByUs = 0;
488 mNumFramesTotal = 0;
489 mNumFramesDropped = 0;
Andreas Huber57a339c2012-12-03 11:18:00 -0800490 mStarted = true;
Andreas Huber1aef2112011-01-04 14:01:29 -0800491
Andreas Huber5bc087c2010-12-23 10:27:40 -0800492 mSource->start();
Andreas Huberf9334412010-12-15 15:17:42 -0800493
Andreas Huberd5e56232013-03-12 11:01:43 -0700494 uint32_t flags = 0;
495
496 if (mSource->isRealTime()) {
497 flags |= Renderer::FLAG_REAL_TIME;
498 }
499
Andreas Huberf9334412010-12-15 15:17:42 -0800500 mRenderer = new Renderer(
501 mAudioSink,
Andreas Huberd5e56232013-03-12 11:01:43 -0700502 new AMessage(kWhatRendererNotify, id()),
503 flags);
Andreas Huberf9334412010-12-15 15:17:42 -0800504
505 looper()->registerHandler(mRenderer);
506
Andreas Huber1aef2112011-01-04 14:01:29 -0800507 postScanSources();
Andreas Huberf9334412010-12-15 15:17:42 -0800508 break;
509 }
510
511 case kWhatScanSources:
512 {
Andreas Huber1aef2112011-01-04 14:01:29 -0800513 int32_t generation;
514 CHECK(msg->findInt32("generation", &generation));
515 if (generation != mScanSourcesGeneration) {
516 // Drop obsolete msg.
517 break;
518 }
519
Andreas Huber5bc087c2010-12-23 10:27:40 -0800520 mScanSourcesPending = false;
521
Steve Block3856b092011-10-20 11:56:00 +0100522 ALOGV("scanning sources haveAudio=%d, haveVideo=%d",
Andreas Huber43c3e6c2011-01-05 12:17:08 -0800523 mAudioDecoder != NULL, mVideoDecoder != NULL);
524
Andreas Huberb7c8e912012-11-27 15:02:53 -0800525 bool mHadAnySourcesBefore =
526 (mAudioDecoder != NULL) || (mVideoDecoder != NULL);
527
Haynes Mathew George5d246ef2012-07-09 10:36:57 -0700528 if (mNativeWindow != NULL) {
529 instantiateDecoder(false, &mVideoDecoder);
530 }
Andreas Huberf9334412010-12-15 15:17:42 -0800531
532 if (mAudioSink != NULL) {
Andreas Huber5bc087c2010-12-23 10:27:40 -0800533 instantiateDecoder(true, &mAudioDecoder);
Andreas Huberf9334412010-12-15 15:17:42 -0800534 }
535
Andreas Huberb7c8e912012-11-27 15:02:53 -0800536 if (!mHadAnySourcesBefore
537 && (mAudioDecoder != NULL || mVideoDecoder != NULL)) {
538 // This is the first time we've found anything playable.
539
Andreas Huber9575c962013-02-05 13:59:56 -0800540 if (mSourceFlags & Source::FLAG_DYNAMIC_DURATION) {
Andreas Huberb7c8e912012-11-27 15:02:53 -0800541 schedulePollDuration();
542 }
543 }
544
Andreas Hubereac68ba2011-09-27 12:12:25 -0700545 status_t err;
546 if ((err = mSource->feedMoreTSData()) != OK) {
Andreas Huber1aef2112011-01-04 14:01:29 -0800547 if (mAudioDecoder == NULL && mVideoDecoder == NULL) {
548 // We're not currently decoding anything (no audio or
549 // video tracks found) and we just ran out of input data.
Andreas Hubereac68ba2011-09-27 12:12:25 -0700550
551 if (err == ERROR_END_OF_STREAM) {
552 notifyListener(MEDIA_PLAYBACK_COMPLETE, 0, 0);
553 } else {
554 notifyListener(MEDIA_ERROR, MEDIA_ERROR_UNKNOWN, err);
555 }
Andreas Huber1aef2112011-01-04 14:01:29 -0800556 }
Andreas Huberf9334412010-12-15 15:17:42 -0800557 break;
558 }
559
Andreas Huberfbe9d812012-08-31 14:05:27 -0700560 if ((mAudioDecoder == NULL && mAudioSink != NULL)
561 || (mVideoDecoder == NULL && mNativeWindow != NULL)) {
Andreas Huberf9334412010-12-15 15:17:42 -0800562 msg->post(100000ll);
Andreas Huber5bc087c2010-12-23 10:27:40 -0800563 mScanSourcesPending = true;
Andreas Huberf9334412010-12-15 15:17:42 -0800564 }
565 break;
566 }
567
568 case kWhatVideoNotify:
569 case kWhatAudioNotify:
570 {
571 bool audio = msg->what() == kWhatAudioNotify;
572
Andreas Huberf9334412010-12-15 15:17:42 -0800573 int32_t what;
Lajos Molnar1cd13982014-01-17 15:12:51 -0800574 CHECK(msg->findInt32("what", &what));
Andreas Huberf9334412010-12-15 15:17:42 -0800575
Lajos Molnar1cd13982014-01-17 15:12:51 -0800576 if (what == Decoder::kWhatFillThisBuffer) {
Andreas Huberf9334412010-12-15 15:17:42 -0800577 status_t err = feedDecoderInputData(
Lajos Molnar1cd13982014-01-17 15:12:51 -0800578 audio, msg);
Andreas Huberf9334412010-12-15 15:17:42 -0800579
Andreas Huber5bc087c2010-12-23 10:27:40 -0800580 if (err == -EWOULDBLOCK) {
Andreas Hubereac68ba2011-09-27 12:12:25 -0700581 if (mSource->feedMoreTSData() == OK) {
Andreas Huber1183a4a2011-11-03 11:00:21 -0700582 msg->post(10000ll);
Andreas Huber5bc087c2010-12-23 10:27:40 -0800583 }
Andreas Huberf9334412010-12-15 15:17:42 -0800584 }
Lajos Molnar1cd13982014-01-17 15:12:51 -0800585 } else if (what == Decoder::kWhatEOS) {
Andreas Huberdc9bacd2011-09-26 10:53:29 -0700586 int32_t err;
Lajos Molnar1cd13982014-01-17 15:12:51 -0800587 CHECK(msg->findInt32("err", &err));
Andreas Huberdc9bacd2011-09-26 10:53:29 -0700588
589 if (err == ERROR_END_OF_STREAM) {
Steve Block3856b092011-10-20 11:56:00 +0100590 ALOGV("got %s decoder EOS", audio ? "audio" : "video");
Andreas Huberdc9bacd2011-09-26 10:53:29 -0700591 } else {
Steve Block3856b092011-10-20 11:56:00 +0100592 ALOGV("got %s decoder EOS w/ error %d",
Andreas Huberdc9bacd2011-09-26 10:53:29 -0700593 audio ? "audio" : "video",
594 err);
595 }
596
597 mRenderer->queueEOS(audio, err);
Lajos Molnar1cd13982014-01-17 15:12:51 -0800598 } else if (what == Decoder::kWhatFlushCompleted) {
Andreas Huber1aef2112011-01-04 14:01:29 -0800599 bool needShutdown;
Andreas Huber53df1a42010-12-22 10:03:04 -0800600
Andreas Huberf9334412010-12-15 15:17:42 -0800601 if (audio) {
Andreas Huber1aef2112011-01-04 14:01:29 -0800602 CHECK(IsFlushingState(mFlushingAudio, &needShutdown));
Andreas Huberf9334412010-12-15 15:17:42 -0800603 mFlushingAudio = FLUSHED;
604 } else {
Andreas Huber1aef2112011-01-04 14:01:29 -0800605 CHECK(IsFlushingState(mFlushingVideo, &needShutdown));
Andreas Huberf9334412010-12-15 15:17:42 -0800606 mFlushingVideo = FLUSHED;
Andreas Huber3fe62152011-09-16 15:09:22 -0700607
608 mVideoLateByUs = 0;
Andreas Huberf9334412010-12-15 15:17:42 -0800609 }
610
Steve Block3856b092011-10-20 11:56:00 +0100611 ALOGV("decoder %s flush completed", audio ? "audio" : "video");
Andreas Huberf9334412010-12-15 15:17:42 -0800612
Andreas Huber1aef2112011-01-04 14:01:29 -0800613 if (needShutdown) {
Steve Block3856b092011-10-20 11:56:00 +0100614 ALOGV("initiating %s decoder shutdown",
Andreas Huber53df1a42010-12-22 10:03:04 -0800615 audio ? "audio" : "video");
Andreas Huberf9334412010-12-15 15:17:42 -0800616
Andreas Huber53df1a42010-12-22 10:03:04 -0800617 (audio ? mAudioDecoder : mVideoDecoder)->initiateShutdown();
Andreas Huberf9334412010-12-15 15:17:42 -0800618
Andreas Huber53df1a42010-12-22 10:03:04 -0800619 if (audio) {
620 mFlushingAudio = SHUTTING_DOWN_DECODER;
621 } else {
622 mFlushingVideo = SHUTTING_DOWN_DECODER;
623 }
Andreas Huberf9334412010-12-15 15:17:42 -0800624 }
Andreas Huber3831a062010-12-21 10:22:33 -0800625
626 finishFlushIfPossible();
Lajos Molnar1cd13982014-01-17 15:12:51 -0800627 } else if (what == Decoder::kWhatOutputFormatChanged) {
628 sp<AMessage> format;
629 CHECK(msg->findMessage("format", &format));
630
Andreas Huber31e25082011-01-10 10:38:31 -0800631 if (audio) {
632 int32_t numChannels;
Lajos Molnar1cd13982014-01-17 15:12:51 -0800633 CHECK(format->findInt32(
Andreas Huber516dacf2012-12-03 15:20:40 -0800634 "channel-count", &numChannels));
Andreas Huber2c2814b2010-12-15 17:18:20 -0800635
Andreas Huber31e25082011-01-10 10:38:31 -0800636 int32_t sampleRate;
Lajos Molnar1cd13982014-01-17 15:12:51 -0800637 CHECK(format->findInt32("sample-rate", &sampleRate));
Andreas Huber2c2814b2010-12-15 17:18:20 -0800638
Steve Block3856b092011-10-20 11:56:00 +0100639 ALOGV("Audio output format changed to %d Hz, %d channels",
Andreas Huber31e25082011-01-10 10:38:31 -0800640 sampleRate, numChannels);
Andreas Huber2c2814b2010-12-15 17:18:20 -0800641
Andreas Huber31e25082011-01-10 10:38:31 -0800642 mAudioSink->close();
Eric Laurent1948eb32012-04-13 16:50:19 -0700643
644 audio_output_flags_t flags;
645 int64_t durationUs;
Andreas Huber516dacf2012-12-03 15:20:40 -0800646 // FIXME: we should handle the case where the video decoder
647 // is created after we receive the format change indication.
648 // Current code will just make that we select deep buffer
649 // with video which should not be a problem as it should
Eric Laurent1948eb32012-04-13 16:50:19 -0700650 // not prevent from keeping A/V sync.
651 if (mVideoDecoder == NULL &&
652 mSource->getDuration(&durationUs) == OK &&
Andreas Huber516dacf2012-12-03 15:20:40 -0800653 durationUs
654 > AUDIO_SINK_MIN_DEEP_BUFFER_DURATION_US) {
Eric Laurent1948eb32012-04-13 16:50:19 -0700655 flags = AUDIO_OUTPUT_FLAG_DEEP_BUFFER;
656 } else {
657 flags = AUDIO_OUTPUT_FLAG_NONE;
658 }
659
Andreas Huber98065552012-05-03 11:33:01 -0700660 int32_t channelMask;
Lajos Molnar1cd13982014-01-17 15:12:51 -0800661 if (!format->findInt32("channel-mask", &channelMask)) {
Andreas Huber98065552012-05-03 11:33:01 -0700662 channelMask = CHANNEL_MASK_USE_CHANNEL_ORDER;
663 }
664
Andreas Huber078cfcf2011-09-15 12:25:04 -0700665 CHECK_EQ(mAudioSink->open(
666 sampleRate,
667 numChannels,
Andreas Huber98065552012-05-03 11:33:01 -0700668 (audio_channel_mask_t)channelMask,
Andreas Huber078cfcf2011-09-15 12:25:04 -0700669 AUDIO_FORMAT_PCM_16_BIT,
Eric Laurent1948eb32012-04-13 16:50:19 -0700670 8 /* bufferCount */,
671 NULL,
672 NULL,
673 flags),
Andreas Huber078cfcf2011-09-15 12:25:04 -0700674 (status_t)OK);
Andreas Huber31e25082011-01-10 10:38:31 -0800675 mAudioSink->start();
Andreas Huber2c2814b2010-12-15 17:18:20 -0800676
Andreas Huber31e25082011-01-10 10:38:31 -0800677 mRenderer->signalAudioSinkChanged();
678 } else {
679 // video
Andreas Huber3831a062010-12-21 10:22:33 -0800680
Andreas Huber31e25082011-01-10 10:38:31 -0800681 int32_t width, height;
Lajos Molnar1cd13982014-01-17 15:12:51 -0800682 CHECK(format->findInt32("width", &width));
683 CHECK(format->findInt32("height", &height));
Andreas Huber31e25082011-01-10 10:38:31 -0800684
685 int32_t cropLeft, cropTop, cropRight, cropBottom;
Lajos Molnar1cd13982014-01-17 15:12:51 -0800686 CHECK(format->findRect(
Andreas Huber31e25082011-01-10 10:38:31 -0800687 "crop",
688 &cropLeft, &cropTop, &cropRight, &cropBottom));
689
Andreas Huber516dacf2012-12-03 15:20:40 -0800690 int32_t displayWidth = cropRight - cropLeft + 1;
691 int32_t displayHeight = cropBottom - cropTop + 1;
692
Steve Block3856b092011-10-20 11:56:00 +0100693 ALOGV("Video output format changed to %d x %d "
Andreas Hubercb67cd12011-08-26 16:02:19 -0700694 "(crop: %d x %d @ (%d, %d))",
Andreas Huber31e25082011-01-10 10:38:31 -0800695 width, height,
Andreas Huber516dacf2012-12-03 15:20:40 -0800696 displayWidth,
697 displayHeight,
Andreas Hubercb67cd12011-08-26 16:02:19 -0700698 cropLeft, cropTop);
Andreas Huber31e25082011-01-10 10:38:31 -0800699
Andreas Huber516dacf2012-12-03 15:20:40 -0800700 sp<AMessage> videoInputFormat =
701 mSource->getFormat(false /* audio */);
702
703 // Take into account sample aspect ratio if necessary:
704 int32_t sarWidth, sarHeight;
705 if (videoInputFormat->findInt32("sar-width", &sarWidth)
706 && videoInputFormat->findInt32(
707 "sar-height", &sarHeight)) {
708 ALOGV("Sample aspect ratio %d : %d",
709 sarWidth, sarHeight);
710
711 displayWidth = (displayWidth * sarWidth) / sarHeight;
712
713 ALOGV("display dimensions %d x %d",
714 displayWidth, displayHeight);
715 }
716
Andreas Huber31e25082011-01-10 10:38:31 -0800717 notifyListener(
Andreas Huber516dacf2012-12-03 15:20:40 -0800718 MEDIA_SET_VIDEO_SIZE, displayWidth, displayHeight);
Andreas Huber31e25082011-01-10 10:38:31 -0800719 }
Lajos Molnar1cd13982014-01-17 15:12:51 -0800720 } else if (what == Decoder::kWhatShutdownCompleted) {
Steve Block3856b092011-10-20 11:56:00 +0100721 ALOGV("%s shutdown completed", audio ? "audio" : "video");
Andreas Huber3831a062010-12-21 10:22:33 -0800722 if (audio) {
723 mAudioDecoder.clear();
724
725 CHECK_EQ((int)mFlushingAudio, (int)SHUTTING_DOWN_DECODER);
726 mFlushingAudio = SHUT_DOWN;
727 } else {
728 mVideoDecoder.clear();
729
730 CHECK_EQ((int)mFlushingVideo, (int)SHUTTING_DOWN_DECODER);
731 mFlushingVideo = SHUT_DOWN;
732 }
733
734 finishFlushIfPossible();
Lajos Molnar1cd13982014-01-17 15:12:51 -0800735 } else if (what == Decoder::kWhatError) {
Steve Block29357bc2012-01-06 19:20:56 +0000736 ALOGE("Received error from %s decoder, aborting playback.",
Andreas Huberc92fd242011-08-16 13:48:44 -0700737 audio ? "audio" : "video");
738
739 mRenderer->queueEOS(audio, UNKNOWN_ERROR);
Lajos Molnar1cd13982014-01-17 15:12:51 -0800740 } else if (what == Decoder::kWhatDrainThisBuffer) {
741 renderBuffer(audio, msg);
742 } else {
743 ALOGV("Unhandled decoder notification %d '%c%c%c%c'.",
Andreas Hubera1f8ab02012-11-30 10:53:22 -0800744 what,
745 what >> 24,
746 (what >> 16) & 0xff,
747 (what >> 8) & 0xff,
748 what & 0xff);
Andreas Huberf9334412010-12-15 15:17:42 -0800749 }
750
751 break;
752 }
753
754 case kWhatRendererNotify:
755 {
756 int32_t what;
757 CHECK(msg->findInt32("what", &what));
758
759 if (what == Renderer::kWhatEOS) {
760 int32_t audio;
761 CHECK(msg->findInt32("audio", &audio));
762
Andreas Huberc92fd242011-08-16 13:48:44 -0700763 int32_t finalResult;
764 CHECK(msg->findInt32("finalResult", &finalResult));
765
Andreas Huberf9334412010-12-15 15:17:42 -0800766 if (audio) {
767 mAudioEOS = true;
768 } else {
769 mVideoEOS = true;
770 }
771
Andreas Huberc92fd242011-08-16 13:48:44 -0700772 if (finalResult == ERROR_END_OF_STREAM) {
Steve Block3856b092011-10-20 11:56:00 +0100773 ALOGV("reached %s EOS", audio ? "audio" : "video");
Andreas Huberc92fd242011-08-16 13:48:44 -0700774 } else {
Steve Block29357bc2012-01-06 19:20:56 +0000775 ALOGE("%s track encountered an error (%d)",
Andreas Huberc92fd242011-08-16 13:48:44 -0700776 audio ? "audio" : "video", finalResult);
777
778 notifyListener(
779 MEDIA_ERROR, MEDIA_ERROR_UNKNOWN, finalResult);
780 }
Andreas Huberf9334412010-12-15 15:17:42 -0800781
782 if ((mAudioEOS || mAudioDecoder == NULL)
783 && (mVideoEOS || mVideoDecoder == NULL)) {
784 notifyListener(MEDIA_PLAYBACK_COMPLETE, 0, 0);
785 }
Andreas Huber43c3e6c2011-01-05 12:17:08 -0800786 } else if (what == Renderer::kWhatPosition) {
787 int64_t positionUs;
788 CHECK(msg->findInt64("positionUs", &positionUs));
789
Andreas Huber3fe62152011-09-16 15:09:22 -0700790 CHECK(msg->findInt64("videoLateByUs", &mVideoLateByUs));
791
Andreas Huber43c3e6c2011-01-05 12:17:08 -0800792 if (mDriver != NULL) {
793 sp<NuPlayerDriver> driver = mDriver.promote();
794 if (driver != NULL) {
795 driver->notifyPosition(positionUs);
Andreas Huber3fe62152011-09-16 15:09:22 -0700796
797 driver->notifyFrameStats(
798 mNumFramesTotal, mNumFramesDropped);
Andreas Huber43c3e6c2011-01-05 12:17:08 -0800799 }
800 }
Andreas Huber3fe62152011-09-16 15:09:22 -0700801 } else if (what == Renderer::kWhatFlushComplete) {
Andreas Huberf9334412010-12-15 15:17:42 -0800802 int32_t audio;
803 CHECK(msg->findInt32("audio", &audio));
804
Steve Block3856b092011-10-20 11:56:00 +0100805 ALOGV("renderer %s flush completed.", audio ? "audio" : "video");
James Dongf57b4ea2012-07-20 13:38:36 -0700806 } else if (what == Renderer::kWhatVideoRenderingStart) {
807 notifyListener(MEDIA_INFO, MEDIA_INFO_RENDERING_START, 0);
Lajos Molnarcbaffcf2013-08-14 18:30:38 -0700808 } else if (what == Renderer::kWhatMediaRenderingStart) {
809 ALOGV("media rendering started");
810 notifyListener(MEDIA_STARTED, 0, 0);
Andreas Huberf9334412010-12-15 15:17:42 -0800811 }
812 break;
813 }
814
815 case kWhatMoreDataQueued:
816 {
817 break;
818 }
819
Andreas Huber1aef2112011-01-04 14:01:29 -0800820 case kWhatReset:
821 {
Steve Block3856b092011-10-20 11:56:00 +0100822 ALOGV("kWhatReset");
Andreas Huber1aef2112011-01-04 14:01:29 -0800823
Andreas Hubera1f8ab02012-11-30 10:53:22 -0800824 mDeferredActions.push_back(
Andreas Huber14f76722013-01-15 09:04:18 -0800825 new ShutdownDecoderAction(
826 true /* audio */, true /* video */));
Andreas Huberb7c8e912012-11-27 15:02:53 -0800827
Andreas Hubera1f8ab02012-11-30 10:53:22 -0800828 mDeferredActions.push_back(
829 new SimpleAction(&NuPlayer::performReset));
Andreas Huberb58ce9f2011-11-28 16:27:35 -0800830
Andreas Hubera1f8ab02012-11-30 10:53:22 -0800831 processDeferredActions();
Andreas Huber1aef2112011-01-04 14:01:29 -0800832 break;
833 }
834
Andreas Huber43c3e6c2011-01-05 12:17:08 -0800835 case kWhatSeek:
836 {
837 int64_t seekTimeUs;
838 CHECK(msg->findInt64("seekTimeUs", &seekTimeUs));
839
Andreas Hubera1f8ab02012-11-30 10:53:22 -0800840 ALOGV("kWhatSeek seekTimeUs=%lld us", seekTimeUs);
Andreas Huber43c3e6c2011-01-05 12:17:08 -0800841
Andreas Hubera1f8ab02012-11-30 10:53:22 -0800842 mDeferredActions.push_back(
843 new SimpleAction(&NuPlayer::performDecoderFlush));
Andreas Huber43c3e6c2011-01-05 12:17:08 -0800844
Andreas Hubera1f8ab02012-11-30 10:53:22 -0800845 mDeferredActions.push_back(new SeekAction(seekTimeUs));
Andreas Huber43c3e6c2011-01-05 12:17:08 -0800846
Andreas Hubera1f8ab02012-11-30 10:53:22 -0800847 processDeferredActions();
Andreas Huber43c3e6c2011-01-05 12:17:08 -0800848 break;
849 }
850
Andreas Huberb4082222011-01-20 15:23:04 -0800851 case kWhatPause:
852 {
853 CHECK(mRenderer != NULL);
Roger Jönssonfba60da2013-01-21 17:15:45 +0100854 mSource->pause();
Andreas Huberb4082222011-01-20 15:23:04 -0800855 mRenderer->pause();
856 break;
857 }
858
859 case kWhatResume:
860 {
861 CHECK(mRenderer != NULL);
Roger Jönssonfba60da2013-01-21 17:15:45 +0100862 mSource->resume();
Andreas Huberb4082222011-01-20 15:23:04 -0800863 mRenderer->resume();
864 break;
865 }
866
Andreas Huberb5f25f02013-02-05 10:14:26 -0800867 case kWhatSourceNotify:
868 {
Andreas Huber9575c962013-02-05 13:59:56 -0800869 onSourceNotify(msg);
Andreas Huberb5f25f02013-02-05 10:14:26 -0800870 break;
871 }
872
Andreas Huberf9334412010-12-15 15:17:42 -0800873 default:
874 TRESPASS();
875 break;
876 }
877}
878
Andreas Huber3831a062010-12-21 10:22:33 -0800879void NuPlayer::finishFlushIfPossible() {
880 if (mFlushingAudio != FLUSHED && mFlushingAudio != SHUT_DOWN) {
881 return;
882 }
883
884 if (mFlushingVideo != FLUSHED && mFlushingVideo != SHUT_DOWN) {
885 return;
886 }
887
Steve Block3856b092011-10-20 11:56:00 +0100888 ALOGV("both audio and video are flushed now.");
Andreas Huber3831a062010-12-21 10:22:33 -0800889
Andreas Huber6e3d3112011-11-28 12:36:11 -0800890 if (mTimeDiscontinuityPending) {
891 mRenderer->signalTimeDiscontinuity();
892 mTimeDiscontinuityPending = false;
893 }
Andreas Huber3831a062010-12-21 10:22:33 -0800894
Andreas Huber22fc52f2011-01-05 16:24:27 -0800895 if (mAudioDecoder != NULL) {
Andreas Huber3831a062010-12-21 10:22:33 -0800896 mAudioDecoder->signalResume();
897 }
898
Andreas Huber22fc52f2011-01-05 16:24:27 -0800899 if (mVideoDecoder != NULL) {
Andreas Huber3831a062010-12-21 10:22:33 -0800900 mVideoDecoder->signalResume();
901 }
902
903 mFlushingAudio = NONE;
904 mFlushingVideo = NONE;
Andreas Huber3831a062010-12-21 10:22:33 -0800905
Andreas Hubera1f8ab02012-11-30 10:53:22 -0800906 processDeferredActions();
Andreas Huber1aef2112011-01-04 14:01:29 -0800907}
908
909void NuPlayer::postScanSources() {
910 if (mScanSourcesPending) {
911 return;
912 }
913
914 sp<AMessage> msg = new AMessage(kWhatScanSources, id());
915 msg->setInt32("generation", mScanSourcesGeneration);
916 msg->post();
917
918 mScanSourcesPending = true;
919}
920
Andreas Huber5bc087c2010-12-23 10:27:40 -0800921status_t NuPlayer::instantiateDecoder(bool audio, sp<Decoder> *decoder) {
Andreas Huberf9334412010-12-15 15:17:42 -0800922 if (*decoder != NULL) {
923 return OK;
924 }
925
Andreas Huber84066782011-08-16 09:34:26 -0700926 sp<AMessage> format = mSource->getFormat(audio);
Andreas Huberf9334412010-12-15 15:17:42 -0800927
Andreas Huber84066782011-08-16 09:34:26 -0700928 if (format == NULL) {
Andreas Huberf9334412010-12-15 15:17:42 -0800929 return -EWOULDBLOCK;
930 }
931
Andreas Huber3fe62152011-09-16 15:09:22 -0700932 if (!audio) {
Andreas Huber84066782011-08-16 09:34:26 -0700933 AString mime;
934 CHECK(format->findString("mime", &mime));
935 mVideoIsAVC = !strcasecmp(MEDIA_MIMETYPE_VIDEO_AVC, mime.c_str());
Andreas Huber3fe62152011-09-16 15:09:22 -0700936 }
937
Andreas Huberf9334412010-12-15 15:17:42 -0800938 sp<AMessage> notify =
939 new AMessage(audio ? kWhatAudioNotify : kWhatVideoNotify,
940 id());
941
Glenn Kasten11731182011-02-08 17:26:17 -0800942 *decoder = audio ? new Decoder(notify) :
943 new Decoder(notify, mNativeWindow);
Lajos Molnar1cd13982014-01-17 15:12:51 -0800944 (*decoder)->init();
Andreas Huber84066782011-08-16 09:34:26 -0700945 (*decoder)->configure(format);
Andreas Huberf9334412010-12-15 15:17:42 -0800946
Andreas Huberf9334412010-12-15 15:17:42 -0800947 return OK;
948}
949
950status_t NuPlayer::feedDecoderInputData(bool audio, const sp<AMessage> &msg) {
951 sp<AMessage> reply;
952 CHECK(msg->findMessage("reply", &reply));
953
Andreas Huber53df1a42010-12-22 10:03:04 -0800954 if ((audio && IsFlushingState(mFlushingAudio))
955 || (!audio && IsFlushingState(mFlushingVideo))) {
Andreas Huberf9334412010-12-15 15:17:42 -0800956 reply->setInt32("err", INFO_DISCONTINUITY);
957 reply->post();
958 return OK;
959 }
960
961 sp<ABuffer> accessUnit;
Andreas Huberf9334412010-12-15 15:17:42 -0800962
Andreas Huber3fe62152011-09-16 15:09:22 -0700963 bool dropAccessUnit;
964 do {
965 status_t err = mSource->dequeueAccessUnit(audio, &accessUnit);
Andreas Huber5bc087c2010-12-23 10:27:40 -0800966
Andreas Huber3fe62152011-09-16 15:09:22 -0700967 if (err == -EWOULDBLOCK) {
968 return err;
969 } else if (err != OK) {
970 if (err == INFO_DISCONTINUITY) {
971 int32_t type;
972 CHECK(accessUnit->meta()->findInt32("discontinuity", &type));
Andreas Huber53df1a42010-12-22 10:03:04 -0800973
Andreas Huber3fe62152011-09-16 15:09:22 -0700974 bool formatChange =
Andreas Huber6e3d3112011-11-28 12:36:11 -0800975 (audio &&
976 (type & ATSParser::DISCONTINUITY_AUDIO_FORMAT))
977 || (!audio &&
978 (type & ATSParser::DISCONTINUITY_VIDEO_FORMAT));
Andreas Huber53df1a42010-12-22 10:03:04 -0800979
Andreas Huber6e3d3112011-11-28 12:36:11 -0800980 bool timeChange = (type & ATSParser::DISCONTINUITY_TIME) != 0;
981
Steve Blockdf64d152012-01-04 20:05:49 +0000982 ALOGI("%s discontinuity (formatChange=%d, time=%d)",
Andreas Huber6e3d3112011-11-28 12:36:11 -0800983 audio ? "audio" : "video", formatChange, timeChange);
Andreas Huber32f3cef2011-03-02 15:34:46 -0800984
Andreas Huber3fe62152011-09-16 15:09:22 -0700985 if (audio) {
986 mSkipRenderingAudioUntilMediaTimeUs = -1;
987 } else {
988 mSkipRenderingVideoUntilMediaTimeUs = -1;
989 }
Andreas Huber32f3cef2011-03-02 15:34:46 -0800990
Andreas Huber6e3d3112011-11-28 12:36:11 -0800991 if (timeChange) {
992 sp<AMessage> extra;
993 if (accessUnit->meta()->findMessage("extra", &extra)
994 && extra != NULL) {
995 int64_t resumeAtMediaTimeUs;
996 if (extra->findInt64(
997 "resume-at-mediatimeUs", &resumeAtMediaTimeUs)) {
Steve Blockdf64d152012-01-04 20:05:49 +0000998 ALOGI("suppressing rendering of %s until %lld us",
Andreas Huber6e3d3112011-11-28 12:36:11 -0800999 audio ? "audio" : "video", resumeAtMediaTimeUs);
Andreas Huber3fe62152011-09-16 15:09:22 -07001000
Andreas Huber6e3d3112011-11-28 12:36:11 -08001001 if (audio) {
1002 mSkipRenderingAudioUntilMediaTimeUs =
1003 resumeAtMediaTimeUs;
1004 } else {
1005 mSkipRenderingVideoUntilMediaTimeUs =
1006 resumeAtMediaTimeUs;
1007 }
Andreas Huber3fe62152011-09-16 15:09:22 -07001008 }
Andreas Huber32f3cef2011-03-02 15:34:46 -08001009 }
1010 }
Andreas Huber3fe62152011-09-16 15:09:22 -07001011
Andreas Huber6e3d3112011-11-28 12:36:11 -08001012 mTimeDiscontinuityPending =
1013 mTimeDiscontinuityPending || timeChange;
1014
1015 if (formatChange || timeChange) {
Andreas Hubera1f8ab02012-11-30 10:53:22 -08001016 if (mFlushingAudio == NONE && mFlushingVideo == NONE) {
1017 // And we'll resume scanning sources once we're done
1018 // flushing.
1019 mDeferredActions.push_front(
1020 new SimpleAction(
1021 &NuPlayer::performScanSources));
1022 }
1023
Robert Shih6d0a94e2014-01-23 16:18:22 -08001024 sp<AMessage> newFormat = mSource->getFormat(audio);
1025 sp<Decoder> &decoder = audio ? mAudioDecoder : mVideoDecoder;
1026 if (formatChange && !decoder->supportsSeamlessFormatChange(newFormat)) {
1027 flushDecoder(audio, /* needShutdown = */ true);
1028 } else {
1029 flushDecoder(audio, /* needShutdown = */ false);
1030 err = OK;
1031 }
Andreas Huber6e3d3112011-11-28 12:36:11 -08001032 } else {
1033 // This stream is unaffected by the discontinuity
1034
1035 if (audio) {
1036 mFlushingAudio = FLUSHED;
1037 } else {
1038 mFlushingVideo = FLUSHED;
1039 }
1040
1041 finishFlushIfPossible();
1042
1043 return -EWOULDBLOCK;
1044 }
Andreas Huber32f3cef2011-03-02 15:34:46 -08001045 }
1046
Andreas Huber3fe62152011-09-16 15:09:22 -07001047 reply->setInt32("err", err);
1048 reply->post();
1049 return OK;
Andreas Huberf9334412010-12-15 15:17:42 -08001050 }
1051
Andreas Huber3fe62152011-09-16 15:09:22 -07001052 if (!audio) {
1053 ++mNumFramesTotal;
1054 }
1055
1056 dropAccessUnit = false;
1057 if (!audio
1058 && mVideoLateByUs > 100000ll
1059 && mVideoIsAVC
1060 && !IsAVCReferenceFrame(accessUnit)) {
1061 dropAccessUnit = true;
1062 ++mNumFramesDropped;
1063 }
1064 } while (dropAccessUnit);
Andreas Huberf9334412010-12-15 15:17:42 -08001065
Steve Block3856b092011-10-20 11:56:00 +01001066 // ALOGV("returned a valid buffer of %s data", audio ? "audio" : "video");
Andreas Huberf9334412010-12-15 15:17:42 -08001067
1068#if 0
1069 int64_t mediaTimeUs;
1070 CHECK(accessUnit->meta()->findInt64("timeUs", &mediaTimeUs));
Steve Block3856b092011-10-20 11:56:00 +01001071 ALOGV("feeding %s input buffer at media time %.2f secs",
Andreas Huberf9334412010-12-15 15:17:42 -08001072 audio ? "audio" : "video",
1073 mediaTimeUs / 1E6);
1074#endif
1075
Andreas Huber2d8bedd2012-02-21 14:38:23 -08001076 reply->setBuffer("buffer", accessUnit);
Andreas Huberf9334412010-12-15 15:17:42 -08001077 reply->post();
1078
1079 return OK;
1080}
1081
1082void NuPlayer::renderBuffer(bool audio, const sp<AMessage> &msg) {
Steve Block3856b092011-10-20 11:56:00 +01001083 // ALOGV("renderBuffer %s", audio ? "audio" : "video");
Andreas Huberf9334412010-12-15 15:17:42 -08001084
1085 sp<AMessage> reply;
1086 CHECK(msg->findMessage("reply", &reply));
1087
Andreas Huber18ac5402011-08-31 15:04:25 -07001088 if (IsFlushingState(audio ? mFlushingAudio : mFlushingVideo)) {
1089 // We're currently attempting to flush the decoder, in order
1090 // to complete this, the decoder wants all its buffers back,
1091 // so we don't want any output buffers it sent us (from before
1092 // we initiated the flush) to be stuck in the renderer's queue.
1093
Steve Block3856b092011-10-20 11:56:00 +01001094 ALOGV("we're still flushing the %s decoder, sending its output buffer"
Andreas Huber18ac5402011-08-31 15:04:25 -07001095 " right back.", audio ? "audio" : "video");
1096
1097 reply->post();
1098 return;
1099 }
1100
Andreas Huber2d8bedd2012-02-21 14:38:23 -08001101 sp<ABuffer> buffer;
1102 CHECK(msg->findBuffer("buffer", &buffer));
Andreas Huberf9334412010-12-15 15:17:42 -08001103
Andreas Huber32f3cef2011-03-02 15:34:46 -08001104 int64_t &skipUntilMediaTimeUs =
1105 audio
1106 ? mSkipRenderingAudioUntilMediaTimeUs
1107 : mSkipRenderingVideoUntilMediaTimeUs;
1108
1109 if (skipUntilMediaTimeUs >= 0) {
1110 int64_t mediaTimeUs;
1111 CHECK(buffer->meta()->findInt64("timeUs", &mediaTimeUs));
1112
1113 if (mediaTimeUs < skipUntilMediaTimeUs) {
Steve Block3856b092011-10-20 11:56:00 +01001114 ALOGV("dropping %s buffer at time %lld as requested.",
Andreas Huber32f3cef2011-03-02 15:34:46 -08001115 audio ? "audio" : "video",
1116 mediaTimeUs);
1117
1118 reply->post();
1119 return;
1120 }
1121
1122 skipUntilMediaTimeUs = -1;
1123 }
1124
Andreas Huberf9334412010-12-15 15:17:42 -08001125 mRenderer->queueBuffer(audio, buffer, reply);
1126}
1127
Chong Zhangdcb89b32013-08-06 09:44:47 -07001128void NuPlayer::notifyListener(int msg, int ext1, int ext2, const Parcel *in) {
Andreas Huber43c3e6c2011-01-05 12:17:08 -08001129 if (mDriver == NULL) {
Andreas Huberf9334412010-12-15 15:17:42 -08001130 return;
1131 }
1132
Andreas Huber43c3e6c2011-01-05 12:17:08 -08001133 sp<NuPlayerDriver> driver = mDriver.promote();
Andreas Huberf9334412010-12-15 15:17:42 -08001134
Andreas Huber43c3e6c2011-01-05 12:17:08 -08001135 if (driver == NULL) {
Andreas Huberf9334412010-12-15 15:17:42 -08001136 return;
1137 }
1138
Chong Zhangdcb89b32013-08-06 09:44:47 -07001139 driver->notifyListener(msg, ext1, ext2, in);
Andreas Huberf9334412010-12-15 15:17:42 -08001140}
1141
Andreas Huber1aef2112011-01-04 14:01:29 -08001142void NuPlayer::flushDecoder(bool audio, bool needShutdown) {
Andreas Huber14f76722013-01-15 09:04:18 -08001143 ALOGV("[%s] flushDecoder needShutdown=%d",
1144 audio ? "audio" : "video", needShutdown);
1145
Andreas Huber6e3d3112011-11-28 12:36:11 -08001146 if ((audio && mAudioDecoder == NULL) || (!audio && mVideoDecoder == NULL)) {
Steve Blockdf64d152012-01-04 20:05:49 +00001147 ALOGI("flushDecoder %s without decoder present",
Andreas Huber6e3d3112011-11-28 12:36:11 -08001148 audio ? "audio" : "video");
1149 }
1150
Andreas Huber1aef2112011-01-04 14:01:29 -08001151 // Make sure we don't continue to scan sources until we finish flushing.
1152 ++mScanSourcesGeneration;
Andreas Huber43c3e6c2011-01-05 12:17:08 -08001153 mScanSourcesPending = false;
Andreas Huber1aef2112011-01-04 14:01:29 -08001154
1155 (audio ? mAudioDecoder : mVideoDecoder)->signalFlush();
1156 mRenderer->flush(audio);
1157
1158 FlushStatus newStatus =
1159 needShutdown ? FLUSHING_DECODER_SHUTDOWN : FLUSHING_DECODER;
1160
1161 if (audio) {
1162 CHECK(mFlushingAudio == NONE
1163 || mFlushingAudio == AWAITING_DISCONTINUITY);
1164
1165 mFlushingAudio = newStatus;
1166
1167 if (mFlushingVideo == NONE) {
1168 mFlushingVideo = (mVideoDecoder != NULL)
1169 ? AWAITING_DISCONTINUITY
1170 : FLUSHED;
1171 }
1172 } else {
1173 CHECK(mFlushingVideo == NONE
1174 || mFlushingVideo == AWAITING_DISCONTINUITY);
1175
1176 mFlushingVideo = newStatus;
1177
1178 if (mFlushingAudio == NONE) {
1179 mFlushingAudio = (mAudioDecoder != NULL)
1180 ? AWAITING_DISCONTINUITY
1181 : FLUSHED;
1182 }
1183 }
1184}
1185
Andreas Huber84066782011-08-16 09:34:26 -07001186sp<AMessage> NuPlayer::Source::getFormat(bool audio) {
1187 sp<MetaData> meta = getFormatMeta(audio);
1188
1189 if (meta == NULL) {
1190 return NULL;
1191 }
1192
1193 sp<AMessage> msg = new AMessage;
1194
1195 if(convertMetaDataToMessage(meta, &msg) == OK) {
1196 return msg;
1197 }
1198 return NULL;
1199}
1200
James Dong0d268a32012-08-31 12:18:27 -07001201status_t NuPlayer::setVideoScalingMode(int32_t mode) {
1202 mVideoScalingMode = mode;
Andreas Huber57a339c2012-12-03 11:18:00 -08001203 if (mNativeWindow != NULL) {
James Dong0d268a32012-08-31 12:18:27 -07001204 status_t ret = native_window_set_scaling_mode(
1205 mNativeWindow->getNativeWindow().get(), mVideoScalingMode);
1206 if (ret != OK) {
1207 ALOGE("Failed to set scaling mode (%d): %s",
1208 -ret, strerror(-ret));
1209 return ret;
1210 }
1211 }
1212 return OK;
1213}
1214
Chong Zhangdcb89b32013-08-06 09:44:47 -07001215status_t NuPlayer::getTrackInfo(Parcel* reply) const {
1216 sp<AMessage> msg = new AMessage(kWhatGetTrackInfo, id());
1217 msg->setPointer("reply", reply);
1218
1219 sp<AMessage> response;
1220 status_t err = msg->postAndAwaitResponse(&response);
1221 return err;
1222}
1223
1224status_t NuPlayer::selectTrack(size_t trackIndex, bool select) {
1225 sp<AMessage> msg = new AMessage(kWhatSelectTrack, id());
1226 msg->setSize("trackIndex", trackIndex);
1227 msg->setInt32("select", select);
1228
1229 sp<AMessage> response;
1230 status_t err = msg->postAndAwaitResponse(&response);
1231
Chong Zhang404fced2014-06-11 14:45:31 -07001232 if (err != OK) {
1233 return err;
1234 }
1235
1236 if (!response->findInt32("err", &err)) {
1237 err = OK;
1238 }
1239
Chong Zhangdcb89b32013-08-06 09:44:47 -07001240 return err;
1241}
1242
Andreas Huberb7c8e912012-11-27 15:02:53 -08001243void NuPlayer::schedulePollDuration() {
1244 sp<AMessage> msg = new AMessage(kWhatPollDuration, id());
1245 msg->setInt32("generation", mPollDurationGeneration);
1246 msg->post();
1247}
1248
1249void NuPlayer::cancelPollDuration() {
1250 ++mPollDurationGeneration;
1251}
1252
Andreas Hubera1f8ab02012-11-30 10:53:22 -08001253void NuPlayer::processDeferredActions() {
1254 while (!mDeferredActions.empty()) {
1255 // We won't execute any deferred actions until we're no longer in
1256 // an intermediate state, i.e. one more more decoders are currently
1257 // flushing or shutting down.
1258
1259 if (mRenderer != NULL) {
1260 // There's an edge case where the renderer owns all output
1261 // buffers and is paused, therefore the decoder will not read
1262 // more input data and will never encounter the matching
1263 // discontinuity. To avoid this, we resume the renderer.
1264
1265 if (mFlushingAudio == AWAITING_DISCONTINUITY
1266 || mFlushingVideo == AWAITING_DISCONTINUITY) {
1267 mRenderer->resume();
1268 }
1269 }
1270
1271 if (mFlushingAudio != NONE || mFlushingVideo != NONE) {
1272 // We're currently flushing, postpone the reset until that's
1273 // completed.
1274
1275 ALOGV("postponing action mFlushingAudio=%d, mFlushingVideo=%d",
1276 mFlushingAudio, mFlushingVideo);
1277
1278 break;
1279 }
1280
1281 sp<Action> action = *mDeferredActions.begin();
1282 mDeferredActions.erase(mDeferredActions.begin());
1283
1284 action->execute(this);
1285 }
1286}
1287
1288void NuPlayer::performSeek(int64_t seekTimeUs) {
1289 ALOGV("performSeek seekTimeUs=%lld us (%.2f secs)",
1290 seekTimeUs,
1291 seekTimeUs / 1E6);
1292
1293 mSource->seekTo(seekTimeUs);
1294
1295 if (mDriver != NULL) {
1296 sp<NuPlayerDriver> driver = mDriver.promote();
1297 if (driver != NULL) {
1298 driver->notifyPosition(seekTimeUs);
1299 driver->notifySeekComplete();
1300 }
1301 }
1302
1303 // everything's flushed, continue playback.
1304}
1305
1306void NuPlayer::performDecoderFlush() {
1307 ALOGV("performDecoderFlush");
1308
Andreas Huberda9740e2013-04-16 10:54:03 -07001309 if (mAudioDecoder == NULL && mVideoDecoder == NULL) {
Andreas Hubera1f8ab02012-11-30 10:53:22 -08001310 return;
1311 }
1312
1313 mTimeDiscontinuityPending = true;
1314
1315 if (mAudioDecoder != NULL) {
1316 flushDecoder(true /* audio */, false /* needShutdown */);
1317 }
1318
1319 if (mVideoDecoder != NULL) {
1320 flushDecoder(false /* audio */, false /* needShutdown */);
1321 }
1322}
1323
Andreas Huber14f76722013-01-15 09:04:18 -08001324void NuPlayer::performDecoderShutdown(bool audio, bool video) {
1325 ALOGV("performDecoderShutdown audio=%d, video=%d", audio, video);
Andreas Hubera1f8ab02012-11-30 10:53:22 -08001326
Andreas Huber14f76722013-01-15 09:04:18 -08001327 if ((!audio || mAudioDecoder == NULL)
1328 && (!video || mVideoDecoder == NULL)) {
Andreas Hubera1f8ab02012-11-30 10:53:22 -08001329 return;
1330 }
1331
1332 mTimeDiscontinuityPending = true;
1333
Andreas Huber14f76722013-01-15 09:04:18 -08001334 if (mFlushingAudio == NONE && (!audio || mAudioDecoder == NULL)) {
1335 mFlushingAudio = FLUSHED;
1336 }
1337
1338 if (mFlushingVideo == NONE && (!video || mVideoDecoder == NULL)) {
1339 mFlushingVideo = FLUSHED;
1340 }
1341
1342 if (audio && mAudioDecoder != NULL) {
Andreas Hubera1f8ab02012-11-30 10:53:22 -08001343 flushDecoder(true /* audio */, true /* needShutdown */);
1344 }
1345
Andreas Huber14f76722013-01-15 09:04:18 -08001346 if (video && mVideoDecoder != NULL) {
Andreas Hubera1f8ab02012-11-30 10:53:22 -08001347 flushDecoder(false /* audio */, true /* needShutdown */);
1348 }
1349}
1350
1351void NuPlayer::performReset() {
1352 ALOGV("performReset");
1353
1354 CHECK(mAudioDecoder == NULL);
1355 CHECK(mVideoDecoder == NULL);
1356
1357 cancelPollDuration();
1358
1359 ++mScanSourcesGeneration;
1360 mScanSourcesPending = false;
1361
1362 mRenderer.clear();
1363
1364 if (mSource != NULL) {
1365 mSource->stop();
Andreas Huberb5f25f02013-02-05 10:14:26 -08001366
1367 looper()->unregisterHandler(mSource->id());
1368
Andreas Hubera1f8ab02012-11-30 10:53:22 -08001369 mSource.clear();
1370 }
1371
1372 if (mDriver != NULL) {
1373 sp<NuPlayerDriver> driver = mDriver.promote();
1374 if (driver != NULL) {
1375 driver->notifyResetComplete();
1376 }
1377 }
Andreas Huber57a339c2012-12-03 11:18:00 -08001378
1379 mStarted = false;
Andreas Hubera1f8ab02012-11-30 10:53:22 -08001380}
1381
1382void NuPlayer::performScanSources() {
1383 ALOGV("performScanSources");
1384
Andreas Huber57a339c2012-12-03 11:18:00 -08001385 if (!mStarted) {
1386 return;
1387 }
1388
Andreas Hubera1f8ab02012-11-30 10:53:22 -08001389 if (mAudioDecoder == NULL || mVideoDecoder == NULL) {
1390 postScanSources();
1391 }
1392}
1393
Andreas Huber57a339c2012-12-03 11:18:00 -08001394void NuPlayer::performSetSurface(const sp<NativeWindowWrapper> &wrapper) {
1395 ALOGV("performSetSurface");
1396
1397 mNativeWindow = wrapper;
1398
1399 // XXX - ignore error from setVideoScalingMode for now
1400 setVideoScalingMode(mVideoScalingMode);
1401
1402 if (mDriver != NULL) {
1403 sp<NuPlayerDriver> driver = mDriver.promote();
1404 if (driver != NULL) {
1405 driver->notifySetSurfaceComplete();
1406 }
1407 }
1408}
1409
Andreas Huber9575c962013-02-05 13:59:56 -08001410void NuPlayer::onSourceNotify(const sp<AMessage> &msg) {
1411 int32_t what;
1412 CHECK(msg->findInt32("what", &what));
1413
1414 switch (what) {
1415 case Source::kWhatPrepared:
1416 {
Andreas Huberb5f28d42013-04-25 15:11:19 -07001417 if (mSource == NULL) {
1418 // This is a stale notification from a source that was
1419 // asynchronously preparing when the client called reset().
1420 // We handled the reset, the source is gone.
1421 break;
1422 }
1423
Andreas Huberec0c5972013-02-05 14:47:13 -08001424 int32_t err;
1425 CHECK(msg->findInt32("err", &err));
1426
Andreas Huber9575c962013-02-05 13:59:56 -08001427 sp<NuPlayerDriver> driver = mDriver.promote();
1428 if (driver != NULL) {
Marco Nelissendd114d12014-05-28 15:23:14 -07001429 // notify duration first, so that it's definitely set when
1430 // the app received the "prepare complete" callback.
1431 int64_t durationUs;
1432 if (mSource->getDuration(&durationUs) == OK) {
1433 driver->notifyDuration(durationUs);
1434 }
Andreas Huberec0c5972013-02-05 14:47:13 -08001435 driver->notifyPrepareCompleted(err);
Andreas Huber9575c962013-02-05 13:59:56 -08001436 }
Andreas Huber99759402013-04-01 14:28:31 -07001437
Andreas Huber9575c962013-02-05 13:59:56 -08001438 break;
1439 }
1440
1441 case Source::kWhatFlagsChanged:
1442 {
1443 uint32_t flags;
1444 CHECK(msg->findInt32("flags", (int32_t *)&flags));
1445
Chong Zhang4b7069d2013-09-11 12:52:43 -07001446 sp<NuPlayerDriver> driver = mDriver.promote();
1447 if (driver != NULL) {
1448 driver->notifyFlagsChanged(flags);
1449 }
1450
Andreas Huber9575c962013-02-05 13:59:56 -08001451 if ((mSourceFlags & Source::FLAG_DYNAMIC_DURATION)
1452 && (!(flags & Source::FLAG_DYNAMIC_DURATION))) {
1453 cancelPollDuration();
1454 } else if (!(mSourceFlags & Source::FLAG_DYNAMIC_DURATION)
1455 && (flags & Source::FLAG_DYNAMIC_DURATION)
1456 && (mAudioDecoder != NULL || mVideoDecoder != NULL)) {
1457 schedulePollDuration();
1458 }
1459
1460 mSourceFlags = flags;
1461 break;
1462 }
1463
1464 case Source::kWhatVideoSizeChanged:
1465 {
1466 int32_t width, height;
1467 CHECK(msg->findInt32("width", &width));
1468 CHECK(msg->findInt32("height", &height));
1469
1470 notifyListener(MEDIA_SET_VIDEO_SIZE, width, height);
1471 break;
1472 }
1473
Roger Jönssonb50e83e2013-01-21 16:26:41 +01001474 case Source::kWhatBufferingStart:
1475 {
1476 notifyListener(MEDIA_INFO, MEDIA_INFO_BUFFERING_START, 0);
1477 break;
1478 }
1479
1480 case Source::kWhatBufferingEnd:
1481 {
1482 notifyListener(MEDIA_INFO, MEDIA_INFO_BUFFERING_END, 0);
1483 break;
1484 }
1485
Chong Zhangdcb89b32013-08-06 09:44:47 -07001486 case Source::kWhatSubtitleData:
1487 {
1488 sp<ABuffer> buffer;
1489 CHECK(msg->findBuffer("buffer", &buffer));
1490
Chong Zhang404fced2014-06-11 14:45:31 -07001491 sendSubtitleData(buffer, 0 /* baseIndex */);
Chong Zhangdcb89b32013-08-06 09:44:47 -07001492 break;
1493 }
1494
Andreas Huber14f76722013-01-15 09:04:18 -08001495 case Source::kWhatQueueDecoderShutdown:
1496 {
1497 int32_t audio, video;
1498 CHECK(msg->findInt32("audio", &audio));
1499 CHECK(msg->findInt32("video", &video));
1500
1501 sp<AMessage> reply;
1502 CHECK(msg->findMessage("reply", &reply));
1503
1504 queueDecoderShutdown(audio, video, reply);
1505 break;
1506 }
1507
Andreas Huber9575c962013-02-05 13:59:56 -08001508 default:
1509 TRESPASS();
1510 }
1511}
1512
Chong Zhang404fced2014-06-11 14:45:31 -07001513void NuPlayer::sendSubtitleData(const sp<ABuffer> &buffer, int32_t baseIndex) {
1514 int32_t trackIndex;
1515 int64_t timeUs, durationUs;
1516 CHECK(buffer->meta()->findInt32("trackIndex", &trackIndex));
1517 CHECK(buffer->meta()->findInt64("timeUs", &timeUs));
1518 CHECK(buffer->meta()->findInt64("durationUs", &durationUs));
1519
1520 Parcel in;
1521 in.writeInt32(trackIndex + baseIndex);
1522 in.writeInt64(timeUs);
1523 in.writeInt64(durationUs);
1524 in.writeInt32(buffer->size());
1525 in.writeInt32(buffer->size());
1526 in.write(buffer->data(), buffer->size());
1527
1528 notifyListener(MEDIA_SUBTITLE_DATA, 0, 0, &in);
1529}
Andreas Huberb5f25f02013-02-05 10:14:26 -08001530////////////////////////////////////////////////////////////////////////////////
1531
Andreas Huber9575c962013-02-05 13:59:56 -08001532void NuPlayer::Source::notifyFlagsChanged(uint32_t flags) {
1533 sp<AMessage> notify = dupNotify();
1534 notify->setInt32("what", kWhatFlagsChanged);
1535 notify->setInt32("flags", flags);
1536 notify->post();
1537}
1538
1539void NuPlayer::Source::notifyVideoSizeChanged(int32_t width, int32_t height) {
1540 sp<AMessage> notify = dupNotify();
1541 notify->setInt32("what", kWhatVideoSizeChanged);
1542 notify->setInt32("width", width);
1543 notify->setInt32("height", height);
1544 notify->post();
1545}
1546
Andreas Huberec0c5972013-02-05 14:47:13 -08001547void NuPlayer::Source::notifyPrepared(status_t err) {
Andreas Huber9575c962013-02-05 13:59:56 -08001548 sp<AMessage> notify = dupNotify();
1549 notify->setInt32("what", kWhatPrepared);
Andreas Huberec0c5972013-02-05 14:47:13 -08001550 notify->setInt32("err", err);
Andreas Huber9575c962013-02-05 13:59:56 -08001551 notify->post();
1552}
1553
Andreas Huber84333e02014-02-07 15:36:10 -08001554void NuPlayer::Source::onMessageReceived(const sp<AMessage> & /* msg */) {
Andreas Huberb5f25f02013-02-05 10:14:26 -08001555 TRESPASS();
1556}
1557
Andreas Huber14f76722013-01-15 09:04:18 -08001558void NuPlayer::queueDecoderShutdown(
1559 bool audio, bool video, const sp<AMessage> &reply) {
1560 ALOGI("queueDecoderShutdown audio=%d, video=%d", audio, video);
1561
1562 mDeferredActions.push_back(
1563 new ShutdownDecoderAction(audio, video));
1564
1565 mDeferredActions.push_back(
1566 new SimpleAction(&NuPlayer::performScanSources));
1567
1568 mDeferredActions.push_back(new PostMessageAction(reply));
1569
1570 processDeferredActions();
1571}
1572
Andreas Huberf9334412010-12-15 15:17:42 -08001573} // namespace android