blob: b333043779891749549cb5228408f0e1e00c8c86 [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
Chong Zhanga7fa1d92014-06-11 14:49:23 -0700378 size_t ccTracks = 0;
379 if (mCCDecoder != NULL) {
380 ccTracks = mCCDecoder->getTrackCount();
381 }
382
Chong Zhang404fced2014-06-11 14:45:31 -0700383 // total track count
Chong Zhanga7fa1d92014-06-11 14:49:23 -0700384 reply->writeInt32(inbandTracks + ccTracks);
Chong Zhang404fced2014-06-11 14:45:31 -0700385
386 // write inband tracks
387 for (size_t i = 0; i < inbandTracks; ++i) {
388 writeTrackInfo(reply, mSource->getTrackInfo(i));
Chong Zhangdcb89b32013-08-06 09:44:47 -0700389 }
390
Chong Zhanga7fa1d92014-06-11 14:49:23 -0700391 // write CC track
392 for (size_t i = 0; i < ccTracks; ++i) {
393 writeTrackInfo(reply, mCCDecoder->getTrackInfo(i));
394 }
395
Chong Zhangdcb89b32013-08-06 09:44:47 -0700396 sp<AMessage> response = new AMessage;
Chong Zhangdcb89b32013-08-06 09:44:47 -0700397 response->postReply(replyID);
398 break;
399 }
400
401 case kWhatSelectTrack:
402 {
403 uint32_t replyID;
404 CHECK(msg->senderAwaitsResponse(&replyID));
405
Chong Zhang404fced2014-06-11 14:45:31 -0700406 size_t trackIndex;
407 int32_t select;
408 CHECK(msg->findSize("trackIndex", &trackIndex));
409 CHECK(msg->findInt32("select", &select));
410
Chong Zhangdcb89b32013-08-06 09:44:47 -0700411 status_t err = INVALID_OPERATION;
Chong Zhang404fced2014-06-11 14:45:31 -0700412
413 size_t inbandTracks = 0;
Chong Zhangdcb89b32013-08-06 09:44:47 -0700414 if (mSource != NULL) {
Chong Zhang404fced2014-06-11 14:45:31 -0700415 inbandTracks = mSource->getTrackCount();
416 }
Chong Zhanga7fa1d92014-06-11 14:49:23 -0700417 size_t ccTracks = 0;
418 if (mCCDecoder != NULL) {
419 ccTracks = mCCDecoder->getTrackCount();
420 }
Chong Zhang404fced2014-06-11 14:45:31 -0700421
422 if (trackIndex < inbandTracks) {
Chong Zhangdcb89b32013-08-06 09:44:47 -0700423 err = mSource->selectTrack(trackIndex, select);
Chong Zhanga7fa1d92014-06-11 14:49:23 -0700424 } else {
425 trackIndex -= inbandTracks;
426
427 if (trackIndex < ccTracks) {
428 err = mCCDecoder->selectTrack(trackIndex, select);
429 }
Chong Zhangdcb89b32013-08-06 09:44:47 -0700430 }
431
432 sp<AMessage> response = new AMessage;
433 response->setInt32("err", err);
434
435 response->postReply(replyID);
436 break;
437 }
438
Andreas Huberb7c8e912012-11-27 15:02:53 -0800439 case kWhatPollDuration:
440 {
441 int32_t generation;
442 CHECK(msg->findInt32("generation", &generation));
443
444 if (generation != mPollDurationGeneration) {
445 // stale
446 break;
447 }
448
449 int64_t durationUs;
450 if (mDriver != NULL && mSource->getDuration(&durationUs) == OK) {
451 sp<NuPlayerDriver> driver = mDriver.promote();
452 if (driver != NULL) {
453 driver->notifyDuration(durationUs);
454 }
455 }
456
457 msg->post(1000000ll); // poll again in a second.
458 break;
459 }
460
Glenn Kasten11731182011-02-08 17:26:17 -0800461 case kWhatSetVideoNativeWindow:
Andreas Huberf9334412010-12-15 15:17:42 -0800462 {
Steve Block3856b092011-10-20 11:56:00 +0100463 ALOGV("kWhatSetVideoNativeWindow");
Andreas Huberf9334412010-12-15 15:17:42 -0800464
Andreas Huber57a339c2012-12-03 11:18:00 -0800465 mDeferredActions.push_back(
Andreas Huber14f76722013-01-15 09:04:18 -0800466 new ShutdownDecoderAction(
467 false /* audio */, true /* video */));
Andreas Huber57a339c2012-12-03 11:18:00 -0800468
Andreas Huberf9334412010-12-15 15:17:42 -0800469 sp<RefBase> obj;
Glenn Kasten11731182011-02-08 17:26:17 -0800470 CHECK(msg->findObject("native-window", &obj));
Andreas Huberf9334412010-12-15 15:17:42 -0800471
Andreas Huber57a339c2012-12-03 11:18:00 -0800472 mDeferredActions.push_back(
473 new SetSurfaceAction(
474 static_cast<NativeWindowWrapper *>(obj.get())));
James Dong0d268a32012-08-31 12:18:27 -0700475
Andreas Huber57a339c2012-12-03 11:18:00 -0800476 if (obj != NULL) {
477 // If there is a new surface texture, instantiate decoders
478 // again if possible.
479 mDeferredActions.push_back(
480 new SimpleAction(&NuPlayer::performScanSources));
481 }
482
483 processDeferredActions();
Andreas Huberf9334412010-12-15 15:17:42 -0800484 break;
485 }
486
487 case kWhatSetAudioSink:
488 {
Steve Block3856b092011-10-20 11:56:00 +0100489 ALOGV("kWhatSetAudioSink");
Andreas Huberf9334412010-12-15 15:17:42 -0800490
491 sp<RefBase> obj;
492 CHECK(msg->findObject("sink", &obj));
493
494 mAudioSink = static_cast<MediaPlayerBase::AudioSink *>(obj.get());
495 break;
496 }
497
498 case kWhatStart:
499 {
Steve Block3856b092011-10-20 11:56:00 +0100500 ALOGV("kWhatStart");
Andreas Huber43c3e6c2011-01-05 12:17:08 -0800501
Andreas Huber3fe62152011-09-16 15:09:22 -0700502 mVideoIsAVC = false;
Andreas Huber1aef2112011-01-04 14:01:29 -0800503 mAudioEOS = false;
504 mVideoEOS = false;
Andreas Huber32f3cef2011-03-02 15:34:46 -0800505 mSkipRenderingAudioUntilMediaTimeUs = -1;
506 mSkipRenderingVideoUntilMediaTimeUs = -1;
Andreas Huber3fe62152011-09-16 15:09:22 -0700507 mVideoLateByUs = 0;
508 mNumFramesTotal = 0;
509 mNumFramesDropped = 0;
Andreas Huber57a339c2012-12-03 11:18:00 -0800510 mStarted = true;
Andreas Huber1aef2112011-01-04 14:01:29 -0800511
Andreas Huber5bc087c2010-12-23 10:27:40 -0800512 mSource->start();
Andreas Huberf9334412010-12-15 15:17:42 -0800513
Andreas Huberd5e56232013-03-12 11:01:43 -0700514 uint32_t flags = 0;
515
516 if (mSource->isRealTime()) {
517 flags |= Renderer::FLAG_REAL_TIME;
518 }
519
Andreas Huberf9334412010-12-15 15:17:42 -0800520 mRenderer = new Renderer(
521 mAudioSink,
Andreas Huberd5e56232013-03-12 11:01:43 -0700522 new AMessage(kWhatRendererNotify, id()),
523 flags);
Andreas Huberf9334412010-12-15 15:17:42 -0800524
525 looper()->registerHandler(mRenderer);
526
Andreas Huber1aef2112011-01-04 14:01:29 -0800527 postScanSources();
Andreas Huberf9334412010-12-15 15:17:42 -0800528 break;
529 }
530
531 case kWhatScanSources:
532 {
Andreas Huber1aef2112011-01-04 14:01:29 -0800533 int32_t generation;
534 CHECK(msg->findInt32("generation", &generation));
535 if (generation != mScanSourcesGeneration) {
536 // Drop obsolete msg.
537 break;
538 }
539
Andreas Huber5bc087c2010-12-23 10:27:40 -0800540 mScanSourcesPending = false;
541
Steve Block3856b092011-10-20 11:56:00 +0100542 ALOGV("scanning sources haveAudio=%d, haveVideo=%d",
Andreas Huber43c3e6c2011-01-05 12:17:08 -0800543 mAudioDecoder != NULL, mVideoDecoder != NULL);
544
Andreas Huberb7c8e912012-11-27 15:02:53 -0800545 bool mHadAnySourcesBefore =
546 (mAudioDecoder != NULL) || (mVideoDecoder != NULL);
547
Haynes Mathew George5d246ef2012-07-09 10:36:57 -0700548 if (mNativeWindow != NULL) {
549 instantiateDecoder(false, &mVideoDecoder);
550 }
Andreas Huberf9334412010-12-15 15:17:42 -0800551
552 if (mAudioSink != NULL) {
Andreas Huber5bc087c2010-12-23 10:27:40 -0800553 instantiateDecoder(true, &mAudioDecoder);
Andreas Huberf9334412010-12-15 15:17:42 -0800554 }
555
Andreas Huberb7c8e912012-11-27 15:02:53 -0800556 if (!mHadAnySourcesBefore
557 && (mAudioDecoder != NULL || mVideoDecoder != NULL)) {
558 // This is the first time we've found anything playable.
559
Andreas Huber9575c962013-02-05 13:59:56 -0800560 if (mSourceFlags & Source::FLAG_DYNAMIC_DURATION) {
Andreas Huberb7c8e912012-11-27 15:02:53 -0800561 schedulePollDuration();
562 }
563 }
564
Andreas Hubereac68ba2011-09-27 12:12:25 -0700565 status_t err;
566 if ((err = mSource->feedMoreTSData()) != OK) {
Andreas Huber1aef2112011-01-04 14:01:29 -0800567 if (mAudioDecoder == NULL && mVideoDecoder == NULL) {
568 // We're not currently decoding anything (no audio or
569 // video tracks found) and we just ran out of input data.
Andreas Hubereac68ba2011-09-27 12:12:25 -0700570
571 if (err == ERROR_END_OF_STREAM) {
572 notifyListener(MEDIA_PLAYBACK_COMPLETE, 0, 0);
573 } else {
574 notifyListener(MEDIA_ERROR, MEDIA_ERROR_UNKNOWN, err);
575 }
Andreas Huber1aef2112011-01-04 14:01:29 -0800576 }
Andreas Huberf9334412010-12-15 15:17:42 -0800577 break;
578 }
579
Andreas Huberfbe9d812012-08-31 14:05:27 -0700580 if ((mAudioDecoder == NULL && mAudioSink != NULL)
581 || (mVideoDecoder == NULL && mNativeWindow != NULL)) {
Andreas Huberf9334412010-12-15 15:17:42 -0800582 msg->post(100000ll);
Andreas Huber5bc087c2010-12-23 10:27:40 -0800583 mScanSourcesPending = true;
Andreas Huberf9334412010-12-15 15:17:42 -0800584 }
585 break;
586 }
587
588 case kWhatVideoNotify:
589 case kWhatAudioNotify:
590 {
591 bool audio = msg->what() == kWhatAudioNotify;
592
Andreas Huberf9334412010-12-15 15:17:42 -0800593 int32_t what;
Lajos Molnar1cd13982014-01-17 15:12:51 -0800594 CHECK(msg->findInt32("what", &what));
Andreas Huberf9334412010-12-15 15:17:42 -0800595
Lajos Molnar1cd13982014-01-17 15:12:51 -0800596 if (what == Decoder::kWhatFillThisBuffer) {
Andreas Huberf9334412010-12-15 15:17:42 -0800597 status_t err = feedDecoderInputData(
Lajos Molnar1cd13982014-01-17 15:12:51 -0800598 audio, msg);
Andreas Huberf9334412010-12-15 15:17:42 -0800599
Andreas Huber5bc087c2010-12-23 10:27:40 -0800600 if (err == -EWOULDBLOCK) {
Andreas Hubereac68ba2011-09-27 12:12:25 -0700601 if (mSource->feedMoreTSData() == OK) {
Andreas Huber1183a4a2011-11-03 11:00:21 -0700602 msg->post(10000ll);
Andreas Huber5bc087c2010-12-23 10:27:40 -0800603 }
Andreas Huberf9334412010-12-15 15:17:42 -0800604 }
Lajos Molnar1cd13982014-01-17 15:12:51 -0800605 } else if (what == Decoder::kWhatEOS) {
Andreas Huberdc9bacd2011-09-26 10:53:29 -0700606 int32_t err;
Lajos Molnar1cd13982014-01-17 15:12:51 -0800607 CHECK(msg->findInt32("err", &err));
Andreas Huberdc9bacd2011-09-26 10:53:29 -0700608
609 if (err == ERROR_END_OF_STREAM) {
Steve Block3856b092011-10-20 11:56:00 +0100610 ALOGV("got %s decoder EOS", audio ? "audio" : "video");
Andreas Huberdc9bacd2011-09-26 10:53:29 -0700611 } else {
Steve Block3856b092011-10-20 11:56:00 +0100612 ALOGV("got %s decoder EOS w/ error %d",
Andreas Huberdc9bacd2011-09-26 10:53:29 -0700613 audio ? "audio" : "video",
614 err);
615 }
616
617 mRenderer->queueEOS(audio, err);
Lajos Molnar1cd13982014-01-17 15:12:51 -0800618 } else if (what == Decoder::kWhatFlushCompleted) {
Andreas Huber1aef2112011-01-04 14:01:29 -0800619 bool needShutdown;
Andreas Huber53df1a42010-12-22 10:03:04 -0800620
Andreas Huberf9334412010-12-15 15:17:42 -0800621 if (audio) {
Andreas Huber1aef2112011-01-04 14:01:29 -0800622 CHECK(IsFlushingState(mFlushingAudio, &needShutdown));
Andreas Huberf9334412010-12-15 15:17:42 -0800623 mFlushingAudio = FLUSHED;
624 } else {
Andreas Huber1aef2112011-01-04 14:01:29 -0800625 CHECK(IsFlushingState(mFlushingVideo, &needShutdown));
Andreas Huberf9334412010-12-15 15:17:42 -0800626 mFlushingVideo = FLUSHED;
Andreas Huber3fe62152011-09-16 15:09:22 -0700627
628 mVideoLateByUs = 0;
Andreas Huberf9334412010-12-15 15:17:42 -0800629 }
630
Steve Block3856b092011-10-20 11:56:00 +0100631 ALOGV("decoder %s flush completed", audio ? "audio" : "video");
Andreas Huberf9334412010-12-15 15:17:42 -0800632
Andreas Huber1aef2112011-01-04 14:01:29 -0800633 if (needShutdown) {
Steve Block3856b092011-10-20 11:56:00 +0100634 ALOGV("initiating %s decoder shutdown",
Andreas Huber53df1a42010-12-22 10:03:04 -0800635 audio ? "audio" : "video");
Andreas Huberf9334412010-12-15 15:17:42 -0800636
Andreas Huber53df1a42010-12-22 10:03:04 -0800637 (audio ? mAudioDecoder : mVideoDecoder)->initiateShutdown();
Andreas Huberf9334412010-12-15 15:17:42 -0800638
Andreas Huber53df1a42010-12-22 10:03:04 -0800639 if (audio) {
640 mFlushingAudio = SHUTTING_DOWN_DECODER;
641 } else {
642 mFlushingVideo = SHUTTING_DOWN_DECODER;
643 }
Andreas Huberf9334412010-12-15 15:17:42 -0800644 }
Andreas Huber3831a062010-12-21 10:22:33 -0800645
646 finishFlushIfPossible();
Lajos Molnar1cd13982014-01-17 15:12:51 -0800647 } else if (what == Decoder::kWhatOutputFormatChanged) {
648 sp<AMessage> format;
649 CHECK(msg->findMessage("format", &format));
650
Andreas Huber31e25082011-01-10 10:38:31 -0800651 if (audio) {
652 int32_t numChannels;
Lajos Molnar1cd13982014-01-17 15:12:51 -0800653 CHECK(format->findInt32(
Andreas Huber516dacf2012-12-03 15:20:40 -0800654 "channel-count", &numChannels));
Andreas Huber2c2814b2010-12-15 17:18:20 -0800655
Andreas Huber31e25082011-01-10 10:38:31 -0800656 int32_t sampleRate;
Lajos Molnar1cd13982014-01-17 15:12:51 -0800657 CHECK(format->findInt32("sample-rate", &sampleRate));
Andreas Huber2c2814b2010-12-15 17:18:20 -0800658
Steve Block3856b092011-10-20 11:56:00 +0100659 ALOGV("Audio output format changed to %d Hz, %d channels",
Andreas Huber31e25082011-01-10 10:38:31 -0800660 sampleRate, numChannels);
Andreas Huber2c2814b2010-12-15 17:18:20 -0800661
Andreas Huber31e25082011-01-10 10:38:31 -0800662 mAudioSink->close();
Eric Laurent1948eb32012-04-13 16:50:19 -0700663
664 audio_output_flags_t flags;
665 int64_t durationUs;
Andreas Huber516dacf2012-12-03 15:20:40 -0800666 // FIXME: we should handle the case where the video decoder
667 // is created after we receive the format change indication.
668 // Current code will just make that we select deep buffer
669 // with video which should not be a problem as it should
Eric Laurent1948eb32012-04-13 16:50:19 -0700670 // not prevent from keeping A/V sync.
671 if (mVideoDecoder == NULL &&
672 mSource->getDuration(&durationUs) == OK &&
Andreas Huber516dacf2012-12-03 15:20:40 -0800673 durationUs
674 > AUDIO_SINK_MIN_DEEP_BUFFER_DURATION_US) {
Eric Laurent1948eb32012-04-13 16:50:19 -0700675 flags = AUDIO_OUTPUT_FLAG_DEEP_BUFFER;
676 } else {
677 flags = AUDIO_OUTPUT_FLAG_NONE;
678 }
679
Andreas Huber98065552012-05-03 11:33:01 -0700680 int32_t channelMask;
Lajos Molnar1cd13982014-01-17 15:12:51 -0800681 if (!format->findInt32("channel-mask", &channelMask)) {
Andreas Huber98065552012-05-03 11:33:01 -0700682 channelMask = CHANNEL_MASK_USE_CHANNEL_ORDER;
683 }
684
Andreas Huber078cfcf2011-09-15 12:25:04 -0700685 CHECK_EQ(mAudioSink->open(
686 sampleRate,
687 numChannels,
Andreas Huber98065552012-05-03 11:33:01 -0700688 (audio_channel_mask_t)channelMask,
Andreas Huber078cfcf2011-09-15 12:25:04 -0700689 AUDIO_FORMAT_PCM_16_BIT,
Eric Laurent1948eb32012-04-13 16:50:19 -0700690 8 /* bufferCount */,
691 NULL,
692 NULL,
693 flags),
Andreas Huber078cfcf2011-09-15 12:25:04 -0700694 (status_t)OK);
Andreas Huber31e25082011-01-10 10:38:31 -0800695 mAudioSink->start();
Andreas Huber2c2814b2010-12-15 17:18:20 -0800696
Andreas Huber31e25082011-01-10 10:38:31 -0800697 mRenderer->signalAudioSinkChanged();
698 } else {
699 // video
Andreas Huber3831a062010-12-21 10:22:33 -0800700
Andreas Huber31e25082011-01-10 10:38:31 -0800701 int32_t width, height;
Lajos Molnar1cd13982014-01-17 15:12:51 -0800702 CHECK(format->findInt32("width", &width));
703 CHECK(format->findInt32("height", &height));
Andreas Huber31e25082011-01-10 10:38:31 -0800704
705 int32_t cropLeft, cropTop, cropRight, cropBottom;
Lajos Molnar1cd13982014-01-17 15:12:51 -0800706 CHECK(format->findRect(
Andreas Huber31e25082011-01-10 10:38:31 -0800707 "crop",
708 &cropLeft, &cropTop, &cropRight, &cropBottom));
709
Andreas Huber516dacf2012-12-03 15:20:40 -0800710 int32_t displayWidth = cropRight - cropLeft + 1;
711 int32_t displayHeight = cropBottom - cropTop + 1;
712
Steve Block3856b092011-10-20 11:56:00 +0100713 ALOGV("Video output format changed to %d x %d "
Andreas Hubercb67cd12011-08-26 16:02:19 -0700714 "(crop: %d x %d @ (%d, %d))",
Andreas Huber31e25082011-01-10 10:38:31 -0800715 width, height,
Andreas Huber516dacf2012-12-03 15:20:40 -0800716 displayWidth,
717 displayHeight,
Andreas Hubercb67cd12011-08-26 16:02:19 -0700718 cropLeft, cropTop);
Andreas Huber31e25082011-01-10 10:38:31 -0800719
Andreas Huber516dacf2012-12-03 15:20:40 -0800720 sp<AMessage> videoInputFormat =
721 mSource->getFormat(false /* audio */);
722
723 // Take into account sample aspect ratio if necessary:
724 int32_t sarWidth, sarHeight;
725 if (videoInputFormat->findInt32("sar-width", &sarWidth)
726 && videoInputFormat->findInt32(
727 "sar-height", &sarHeight)) {
728 ALOGV("Sample aspect ratio %d : %d",
729 sarWidth, sarHeight);
730
731 displayWidth = (displayWidth * sarWidth) / sarHeight;
732
733 ALOGV("display dimensions %d x %d",
734 displayWidth, displayHeight);
735 }
736
Andreas Huber31e25082011-01-10 10:38:31 -0800737 notifyListener(
Andreas Huber516dacf2012-12-03 15:20:40 -0800738 MEDIA_SET_VIDEO_SIZE, displayWidth, displayHeight);
Andreas Huber31e25082011-01-10 10:38:31 -0800739 }
Lajos Molnar1cd13982014-01-17 15:12:51 -0800740 } else if (what == Decoder::kWhatShutdownCompleted) {
Steve Block3856b092011-10-20 11:56:00 +0100741 ALOGV("%s shutdown completed", audio ? "audio" : "video");
Andreas Huber3831a062010-12-21 10:22:33 -0800742 if (audio) {
743 mAudioDecoder.clear();
744
745 CHECK_EQ((int)mFlushingAudio, (int)SHUTTING_DOWN_DECODER);
746 mFlushingAudio = SHUT_DOWN;
747 } else {
748 mVideoDecoder.clear();
749
750 CHECK_EQ((int)mFlushingVideo, (int)SHUTTING_DOWN_DECODER);
751 mFlushingVideo = SHUT_DOWN;
752 }
753
754 finishFlushIfPossible();
Lajos Molnar1cd13982014-01-17 15:12:51 -0800755 } else if (what == Decoder::kWhatError) {
Steve Block29357bc2012-01-06 19:20:56 +0000756 ALOGE("Received error from %s decoder, aborting playback.",
Andreas Huberc92fd242011-08-16 13:48:44 -0700757 audio ? "audio" : "video");
758
759 mRenderer->queueEOS(audio, UNKNOWN_ERROR);
Lajos Molnar1cd13982014-01-17 15:12:51 -0800760 } else if (what == Decoder::kWhatDrainThisBuffer) {
761 renderBuffer(audio, msg);
762 } else {
763 ALOGV("Unhandled decoder notification %d '%c%c%c%c'.",
Andreas Hubera1f8ab02012-11-30 10:53:22 -0800764 what,
765 what >> 24,
766 (what >> 16) & 0xff,
767 (what >> 8) & 0xff,
768 what & 0xff);
Andreas Huberf9334412010-12-15 15:17:42 -0800769 }
770
771 break;
772 }
773
774 case kWhatRendererNotify:
775 {
776 int32_t what;
777 CHECK(msg->findInt32("what", &what));
778
779 if (what == Renderer::kWhatEOS) {
780 int32_t audio;
781 CHECK(msg->findInt32("audio", &audio));
782
Andreas Huberc92fd242011-08-16 13:48:44 -0700783 int32_t finalResult;
784 CHECK(msg->findInt32("finalResult", &finalResult));
785
Andreas Huberf9334412010-12-15 15:17:42 -0800786 if (audio) {
787 mAudioEOS = true;
788 } else {
789 mVideoEOS = true;
790 }
791
Andreas Huberc92fd242011-08-16 13:48:44 -0700792 if (finalResult == ERROR_END_OF_STREAM) {
Steve Block3856b092011-10-20 11:56:00 +0100793 ALOGV("reached %s EOS", audio ? "audio" : "video");
Andreas Huberc92fd242011-08-16 13:48:44 -0700794 } else {
Steve Block29357bc2012-01-06 19:20:56 +0000795 ALOGE("%s track encountered an error (%d)",
Andreas Huberc92fd242011-08-16 13:48:44 -0700796 audio ? "audio" : "video", finalResult);
797
798 notifyListener(
799 MEDIA_ERROR, MEDIA_ERROR_UNKNOWN, finalResult);
800 }
Andreas Huberf9334412010-12-15 15:17:42 -0800801
802 if ((mAudioEOS || mAudioDecoder == NULL)
803 && (mVideoEOS || mVideoDecoder == NULL)) {
804 notifyListener(MEDIA_PLAYBACK_COMPLETE, 0, 0);
805 }
Andreas Huber43c3e6c2011-01-05 12:17:08 -0800806 } else if (what == Renderer::kWhatPosition) {
807 int64_t positionUs;
808 CHECK(msg->findInt64("positionUs", &positionUs));
809
Andreas Huber3fe62152011-09-16 15:09:22 -0700810 CHECK(msg->findInt64("videoLateByUs", &mVideoLateByUs));
811
Andreas Huber43c3e6c2011-01-05 12:17:08 -0800812 if (mDriver != NULL) {
813 sp<NuPlayerDriver> driver = mDriver.promote();
814 if (driver != NULL) {
815 driver->notifyPosition(positionUs);
Andreas Huber3fe62152011-09-16 15:09:22 -0700816
817 driver->notifyFrameStats(
818 mNumFramesTotal, mNumFramesDropped);
Andreas Huber43c3e6c2011-01-05 12:17:08 -0800819 }
820 }
Andreas Huber3fe62152011-09-16 15:09:22 -0700821 } else if (what == Renderer::kWhatFlushComplete) {
Andreas Huberf9334412010-12-15 15:17:42 -0800822 int32_t audio;
823 CHECK(msg->findInt32("audio", &audio));
824
Steve Block3856b092011-10-20 11:56:00 +0100825 ALOGV("renderer %s flush completed.", audio ? "audio" : "video");
James Dongf57b4ea2012-07-20 13:38:36 -0700826 } else if (what == Renderer::kWhatVideoRenderingStart) {
827 notifyListener(MEDIA_INFO, MEDIA_INFO_RENDERING_START, 0);
Lajos Molnarcbaffcf2013-08-14 18:30:38 -0700828 } else if (what == Renderer::kWhatMediaRenderingStart) {
829 ALOGV("media rendering started");
830 notifyListener(MEDIA_STARTED, 0, 0);
Andreas Huberf9334412010-12-15 15:17:42 -0800831 }
832 break;
833 }
834
835 case kWhatMoreDataQueued:
836 {
837 break;
838 }
839
Andreas Huber1aef2112011-01-04 14:01:29 -0800840 case kWhatReset:
841 {
Steve Block3856b092011-10-20 11:56:00 +0100842 ALOGV("kWhatReset");
Andreas Huber1aef2112011-01-04 14:01:29 -0800843
Andreas Hubera1f8ab02012-11-30 10:53:22 -0800844 mDeferredActions.push_back(
Andreas Huber14f76722013-01-15 09:04:18 -0800845 new ShutdownDecoderAction(
846 true /* audio */, true /* video */));
Andreas Huberb7c8e912012-11-27 15:02:53 -0800847
Andreas Hubera1f8ab02012-11-30 10:53:22 -0800848 mDeferredActions.push_back(
849 new SimpleAction(&NuPlayer::performReset));
Andreas Huberb58ce9f2011-11-28 16:27:35 -0800850
Andreas Hubera1f8ab02012-11-30 10:53:22 -0800851 processDeferredActions();
Andreas Huber1aef2112011-01-04 14:01:29 -0800852 break;
853 }
854
Andreas Huber43c3e6c2011-01-05 12:17:08 -0800855 case kWhatSeek:
856 {
857 int64_t seekTimeUs;
858 CHECK(msg->findInt64("seekTimeUs", &seekTimeUs));
859
Andreas Hubera1f8ab02012-11-30 10:53:22 -0800860 ALOGV("kWhatSeek seekTimeUs=%lld us", seekTimeUs);
Andreas Huber43c3e6c2011-01-05 12:17:08 -0800861
Andreas Hubera1f8ab02012-11-30 10:53:22 -0800862 mDeferredActions.push_back(
863 new SimpleAction(&NuPlayer::performDecoderFlush));
Andreas Huber43c3e6c2011-01-05 12:17:08 -0800864
Andreas Hubera1f8ab02012-11-30 10:53:22 -0800865 mDeferredActions.push_back(new SeekAction(seekTimeUs));
Andreas Huber43c3e6c2011-01-05 12:17:08 -0800866
Andreas Hubera1f8ab02012-11-30 10:53:22 -0800867 processDeferredActions();
Andreas Huber43c3e6c2011-01-05 12:17:08 -0800868 break;
869 }
870
Andreas Huberb4082222011-01-20 15:23:04 -0800871 case kWhatPause:
872 {
873 CHECK(mRenderer != NULL);
Roger Jönssonfba60da2013-01-21 17:15:45 +0100874 mSource->pause();
Andreas Huberb4082222011-01-20 15:23:04 -0800875 mRenderer->pause();
876 break;
877 }
878
879 case kWhatResume:
880 {
881 CHECK(mRenderer != NULL);
Roger Jönssonfba60da2013-01-21 17:15:45 +0100882 mSource->resume();
Andreas Huberb4082222011-01-20 15:23:04 -0800883 mRenderer->resume();
884 break;
885 }
886
Andreas Huberb5f25f02013-02-05 10:14:26 -0800887 case kWhatSourceNotify:
888 {
Andreas Huber9575c962013-02-05 13:59:56 -0800889 onSourceNotify(msg);
Andreas Huberb5f25f02013-02-05 10:14:26 -0800890 break;
891 }
892
Chong Zhanga7fa1d92014-06-11 14:49:23 -0700893 case kWhatClosedCaptionNotify:
894 {
895 onClosedCaptionNotify(msg);
896 break;
897 }
898
Andreas Huberf9334412010-12-15 15:17:42 -0800899 default:
900 TRESPASS();
901 break;
902 }
903}
904
Andreas Huber3831a062010-12-21 10:22:33 -0800905void NuPlayer::finishFlushIfPossible() {
906 if (mFlushingAudio != FLUSHED && mFlushingAudio != SHUT_DOWN) {
907 return;
908 }
909
910 if (mFlushingVideo != FLUSHED && mFlushingVideo != SHUT_DOWN) {
911 return;
912 }
913
Steve Block3856b092011-10-20 11:56:00 +0100914 ALOGV("both audio and video are flushed now.");
Andreas Huber3831a062010-12-21 10:22:33 -0800915
Andreas Huber6e3d3112011-11-28 12:36:11 -0800916 if (mTimeDiscontinuityPending) {
917 mRenderer->signalTimeDiscontinuity();
918 mTimeDiscontinuityPending = false;
919 }
Andreas Huber3831a062010-12-21 10:22:33 -0800920
Andreas Huber22fc52f2011-01-05 16:24:27 -0800921 if (mAudioDecoder != NULL) {
Andreas Huber3831a062010-12-21 10:22:33 -0800922 mAudioDecoder->signalResume();
923 }
924
Andreas Huber22fc52f2011-01-05 16:24:27 -0800925 if (mVideoDecoder != NULL) {
Andreas Huber3831a062010-12-21 10:22:33 -0800926 mVideoDecoder->signalResume();
927 }
928
929 mFlushingAudio = NONE;
930 mFlushingVideo = NONE;
Andreas Huber3831a062010-12-21 10:22:33 -0800931
Andreas Hubera1f8ab02012-11-30 10:53:22 -0800932 processDeferredActions();
Andreas Huber1aef2112011-01-04 14:01:29 -0800933}
934
935void NuPlayer::postScanSources() {
936 if (mScanSourcesPending) {
937 return;
938 }
939
940 sp<AMessage> msg = new AMessage(kWhatScanSources, id());
941 msg->setInt32("generation", mScanSourcesGeneration);
942 msg->post();
943
944 mScanSourcesPending = true;
945}
946
Andreas Huber5bc087c2010-12-23 10:27:40 -0800947status_t NuPlayer::instantiateDecoder(bool audio, sp<Decoder> *decoder) {
Andreas Huberf9334412010-12-15 15:17:42 -0800948 if (*decoder != NULL) {
949 return OK;
950 }
951
Andreas Huber84066782011-08-16 09:34:26 -0700952 sp<AMessage> format = mSource->getFormat(audio);
Andreas Huberf9334412010-12-15 15:17:42 -0800953
Andreas Huber84066782011-08-16 09:34:26 -0700954 if (format == NULL) {
Andreas Huberf9334412010-12-15 15:17:42 -0800955 return -EWOULDBLOCK;
956 }
957
Andreas Huber3fe62152011-09-16 15:09:22 -0700958 if (!audio) {
Andreas Huber84066782011-08-16 09:34:26 -0700959 AString mime;
960 CHECK(format->findString("mime", &mime));
961 mVideoIsAVC = !strcasecmp(MEDIA_MIMETYPE_VIDEO_AVC, mime.c_str());
Chong Zhanga7fa1d92014-06-11 14:49:23 -0700962
963 sp<AMessage> ccNotify = new AMessage(kWhatClosedCaptionNotify, id());
964 mCCDecoder = new CCDecoder(ccNotify);
Andreas Huber3fe62152011-09-16 15:09:22 -0700965 }
966
Andreas Huberf9334412010-12-15 15:17:42 -0800967 sp<AMessage> notify =
968 new AMessage(audio ? kWhatAudioNotify : kWhatVideoNotify,
969 id());
970
Glenn Kasten11731182011-02-08 17:26:17 -0800971 *decoder = audio ? new Decoder(notify) :
972 new Decoder(notify, mNativeWindow);
Lajos Molnar1cd13982014-01-17 15:12:51 -0800973 (*decoder)->init();
Andreas Huber84066782011-08-16 09:34:26 -0700974 (*decoder)->configure(format);
Andreas Huberf9334412010-12-15 15:17:42 -0800975
Andreas Huberf9334412010-12-15 15:17:42 -0800976 return OK;
977}
978
979status_t NuPlayer::feedDecoderInputData(bool audio, const sp<AMessage> &msg) {
980 sp<AMessage> reply;
981 CHECK(msg->findMessage("reply", &reply));
982
Andreas Huber53df1a42010-12-22 10:03:04 -0800983 if ((audio && IsFlushingState(mFlushingAudio))
984 || (!audio && IsFlushingState(mFlushingVideo))) {
Andreas Huberf9334412010-12-15 15:17:42 -0800985 reply->setInt32("err", INFO_DISCONTINUITY);
986 reply->post();
987 return OK;
988 }
989
990 sp<ABuffer> accessUnit;
Andreas Huberf9334412010-12-15 15:17:42 -0800991
Andreas Huber3fe62152011-09-16 15:09:22 -0700992 bool dropAccessUnit;
993 do {
994 status_t err = mSource->dequeueAccessUnit(audio, &accessUnit);
Andreas Huber5bc087c2010-12-23 10:27:40 -0800995
Andreas Huber3fe62152011-09-16 15:09:22 -0700996 if (err == -EWOULDBLOCK) {
997 return err;
998 } else if (err != OK) {
999 if (err == INFO_DISCONTINUITY) {
1000 int32_t type;
1001 CHECK(accessUnit->meta()->findInt32("discontinuity", &type));
Andreas Huber53df1a42010-12-22 10:03:04 -08001002
Andreas Huber3fe62152011-09-16 15:09:22 -07001003 bool formatChange =
Andreas Huber6e3d3112011-11-28 12:36:11 -08001004 (audio &&
1005 (type & ATSParser::DISCONTINUITY_AUDIO_FORMAT))
1006 || (!audio &&
1007 (type & ATSParser::DISCONTINUITY_VIDEO_FORMAT));
Andreas Huber53df1a42010-12-22 10:03:04 -08001008
Andreas Huber6e3d3112011-11-28 12:36:11 -08001009 bool timeChange = (type & ATSParser::DISCONTINUITY_TIME) != 0;
1010
Steve Blockdf64d152012-01-04 20:05:49 +00001011 ALOGI("%s discontinuity (formatChange=%d, time=%d)",
Andreas Huber6e3d3112011-11-28 12:36:11 -08001012 audio ? "audio" : "video", formatChange, timeChange);
Andreas Huber32f3cef2011-03-02 15:34:46 -08001013
Andreas Huber3fe62152011-09-16 15:09:22 -07001014 if (audio) {
1015 mSkipRenderingAudioUntilMediaTimeUs = -1;
1016 } else {
1017 mSkipRenderingVideoUntilMediaTimeUs = -1;
1018 }
Andreas Huber32f3cef2011-03-02 15:34:46 -08001019
Andreas Huber6e3d3112011-11-28 12:36:11 -08001020 if (timeChange) {
1021 sp<AMessage> extra;
1022 if (accessUnit->meta()->findMessage("extra", &extra)
1023 && extra != NULL) {
1024 int64_t resumeAtMediaTimeUs;
1025 if (extra->findInt64(
1026 "resume-at-mediatimeUs", &resumeAtMediaTimeUs)) {
Steve Blockdf64d152012-01-04 20:05:49 +00001027 ALOGI("suppressing rendering of %s until %lld us",
Andreas Huber6e3d3112011-11-28 12:36:11 -08001028 audio ? "audio" : "video", resumeAtMediaTimeUs);
Andreas Huber3fe62152011-09-16 15:09:22 -07001029
Andreas Huber6e3d3112011-11-28 12:36:11 -08001030 if (audio) {
1031 mSkipRenderingAudioUntilMediaTimeUs =
1032 resumeAtMediaTimeUs;
1033 } else {
1034 mSkipRenderingVideoUntilMediaTimeUs =
1035 resumeAtMediaTimeUs;
1036 }
Andreas Huber3fe62152011-09-16 15:09:22 -07001037 }
Andreas Huber32f3cef2011-03-02 15:34:46 -08001038 }
1039 }
Andreas Huber3fe62152011-09-16 15:09:22 -07001040
Andreas Huber6e3d3112011-11-28 12:36:11 -08001041 mTimeDiscontinuityPending =
1042 mTimeDiscontinuityPending || timeChange;
1043
1044 if (formatChange || timeChange) {
Andreas Hubera1f8ab02012-11-30 10:53:22 -08001045 if (mFlushingAudio == NONE && mFlushingVideo == NONE) {
1046 // And we'll resume scanning sources once we're done
1047 // flushing.
1048 mDeferredActions.push_front(
1049 new SimpleAction(
1050 &NuPlayer::performScanSources));
1051 }
1052
Robert Shih6d0a94e2014-01-23 16:18:22 -08001053 sp<AMessage> newFormat = mSource->getFormat(audio);
1054 sp<Decoder> &decoder = audio ? mAudioDecoder : mVideoDecoder;
1055 if (formatChange && !decoder->supportsSeamlessFormatChange(newFormat)) {
1056 flushDecoder(audio, /* needShutdown = */ true);
1057 } else {
1058 flushDecoder(audio, /* needShutdown = */ false);
1059 err = OK;
1060 }
Andreas Huber6e3d3112011-11-28 12:36:11 -08001061 } else {
1062 // This stream is unaffected by the discontinuity
1063
1064 if (audio) {
1065 mFlushingAudio = FLUSHED;
1066 } else {
1067 mFlushingVideo = FLUSHED;
1068 }
1069
1070 finishFlushIfPossible();
1071
1072 return -EWOULDBLOCK;
1073 }
Andreas Huber32f3cef2011-03-02 15:34:46 -08001074 }
1075
Andreas Huber3fe62152011-09-16 15:09:22 -07001076 reply->setInt32("err", err);
1077 reply->post();
1078 return OK;
Andreas Huberf9334412010-12-15 15:17:42 -08001079 }
1080
Andreas Huber3fe62152011-09-16 15:09:22 -07001081 if (!audio) {
1082 ++mNumFramesTotal;
1083 }
1084
1085 dropAccessUnit = false;
1086 if (!audio
1087 && mVideoLateByUs > 100000ll
1088 && mVideoIsAVC
1089 && !IsAVCReferenceFrame(accessUnit)) {
1090 dropAccessUnit = true;
1091 ++mNumFramesDropped;
1092 }
1093 } while (dropAccessUnit);
Andreas Huberf9334412010-12-15 15:17:42 -08001094
Steve Block3856b092011-10-20 11:56:00 +01001095 // ALOGV("returned a valid buffer of %s data", audio ? "audio" : "video");
Andreas Huberf9334412010-12-15 15:17:42 -08001096
1097#if 0
1098 int64_t mediaTimeUs;
1099 CHECK(accessUnit->meta()->findInt64("timeUs", &mediaTimeUs));
Steve Block3856b092011-10-20 11:56:00 +01001100 ALOGV("feeding %s input buffer at media time %.2f secs",
Andreas Huberf9334412010-12-15 15:17:42 -08001101 audio ? "audio" : "video",
1102 mediaTimeUs / 1E6);
1103#endif
1104
Chong Zhanga7fa1d92014-06-11 14:49:23 -07001105 if (!audio) {
1106 mCCDecoder->decode(accessUnit);
1107 }
1108
Andreas Huber2d8bedd2012-02-21 14:38:23 -08001109 reply->setBuffer("buffer", accessUnit);
Andreas Huberf9334412010-12-15 15:17:42 -08001110 reply->post();
1111
1112 return OK;
1113}
1114
1115void NuPlayer::renderBuffer(bool audio, const sp<AMessage> &msg) {
Steve Block3856b092011-10-20 11:56:00 +01001116 // ALOGV("renderBuffer %s", audio ? "audio" : "video");
Andreas Huberf9334412010-12-15 15:17:42 -08001117
1118 sp<AMessage> reply;
1119 CHECK(msg->findMessage("reply", &reply));
1120
Andreas Huber18ac5402011-08-31 15:04:25 -07001121 if (IsFlushingState(audio ? mFlushingAudio : mFlushingVideo)) {
1122 // We're currently attempting to flush the decoder, in order
1123 // to complete this, the decoder wants all its buffers back,
1124 // so we don't want any output buffers it sent us (from before
1125 // we initiated the flush) to be stuck in the renderer's queue.
1126
Steve Block3856b092011-10-20 11:56:00 +01001127 ALOGV("we're still flushing the %s decoder, sending its output buffer"
Andreas Huber18ac5402011-08-31 15:04:25 -07001128 " right back.", audio ? "audio" : "video");
1129
1130 reply->post();
1131 return;
1132 }
1133
Andreas Huber2d8bedd2012-02-21 14:38:23 -08001134 sp<ABuffer> buffer;
1135 CHECK(msg->findBuffer("buffer", &buffer));
Andreas Huberf9334412010-12-15 15:17:42 -08001136
Chong Zhanga7fa1d92014-06-11 14:49:23 -07001137 int64_t mediaTimeUs;
1138 CHECK(buffer->meta()->findInt64("timeUs", &mediaTimeUs));
1139
Andreas Huber32f3cef2011-03-02 15:34:46 -08001140 int64_t &skipUntilMediaTimeUs =
1141 audio
1142 ? mSkipRenderingAudioUntilMediaTimeUs
1143 : mSkipRenderingVideoUntilMediaTimeUs;
1144
1145 if (skipUntilMediaTimeUs >= 0) {
Andreas Huber32f3cef2011-03-02 15:34:46 -08001146
1147 if (mediaTimeUs < skipUntilMediaTimeUs) {
Steve Block3856b092011-10-20 11:56:00 +01001148 ALOGV("dropping %s buffer at time %lld as requested.",
Andreas Huber32f3cef2011-03-02 15:34:46 -08001149 audio ? "audio" : "video",
1150 mediaTimeUs);
1151
1152 reply->post();
1153 return;
1154 }
1155
1156 skipUntilMediaTimeUs = -1;
1157 }
1158
Chong Zhanga7fa1d92014-06-11 14:49:23 -07001159 if (!audio && mCCDecoder->isSelected()) {
1160 mCCDecoder->display(mediaTimeUs);
1161 }
1162
Andreas Huberf9334412010-12-15 15:17:42 -08001163 mRenderer->queueBuffer(audio, buffer, reply);
1164}
1165
Chong Zhangdcb89b32013-08-06 09:44:47 -07001166void NuPlayer::notifyListener(int msg, int ext1, int ext2, const Parcel *in) {
Andreas Huber43c3e6c2011-01-05 12:17:08 -08001167 if (mDriver == NULL) {
Andreas Huberf9334412010-12-15 15:17:42 -08001168 return;
1169 }
1170
Andreas Huber43c3e6c2011-01-05 12:17:08 -08001171 sp<NuPlayerDriver> driver = mDriver.promote();
Andreas Huberf9334412010-12-15 15:17:42 -08001172
Andreas Huber43c3e6c2011-01-05 12:17:08 -08001173 if (driver == NULL) {
Andreas Huberf9334412010-12-15 15:17:42 -08001174 return;
1175 }
1176
Chong Zhangdcb89b32013-08-06 09:44:47 -07001177 driver->notifyListener(msg, ext1, ext2, in);
Andreas Huberf9334412010-12-15 15:17:42 -08001178}
1179
Andreas Huber1aef2112011-01-04 14:01:29 -08001180void NuPlayer::flushDecoder(bool audio, bool needShutdown) {
Andreas Huber14f76722013-01-15 09:04:18 -08001181 ALOGV("[%s] flushDecoder needShutdown=%d",
1182 audio ? "audio" : "video", needShutdown);
1183
Andreas Huber6e3d3112011-11-28 12:36:11 -08001184 if ((audio && mAudioDecoder == NULL) || (!audio && mVideoDecoder == NULL)) {
Steve Blockdf64d152012-01-04 20:05:49 +00001185 ALOGI("flushDecoder %s without decoder present",
Andreas Huber6e3d3112011-11-28 12:36:11 -08001186 audio ? "audio" : "video");
1187 }
1188
Andreas Huber1aef2112011-01-04 14:01:29 -08001189 // Make sure we don't continue to scan sources until we finish flushing.
1190 ++mScanSourcesGeneration;
Andreas Huber43c3e6c2011-01-05 12:17:08 -08001191 mScanSourcesPending = false;
Andreas Huber1aef2112011-01-04 14:01:29 -08001192
1193 (audio ? mAudioDecoder : mVideoDecoder)->signalFlush();
1194 mRenderer->flush(audio);
1195
1196 FlushStatus newStatus =
1197 needShutdown ? FLUSHING_DECODER_SHUTDOWN : FLUSHING_DECODER;
1198
1199 if (audio) {
1200 CHECK(mFlushingAudio == NONE
1201 || mFlushingAudio == AWAITING_DISCONTINUITY);
1202
1203 mFlushingAudio = newStatus;
1204
1205 if (mFlushingVideo == NONE) {
1206 mFlushingVideo = (mVideoDecoder != NULL)
1207 ? AWAITING_DISCONTINUITY
1208 : FLUSHED;
1209 }
1210 } else {
1211 CHECK(mFlushingVideo == NONE
1212 || mFlushingVideo == AWAITING_DISCONTINUITY);
1213
1214 mFlushingVideo = newStatus;
1215
1216 if (mFlushingAudio == NONE) {
1217 mFlushingAudio = (mAudioDecoder != NULL)
1218 ? AWAITING_DISCONTINUITY
1219 : FLUSHED;
1220 }
1221 }
1222}
1223
Andreas Huber84066782011-08-16 09:34:26 -07001224sp<AMessage> NuPlayer::Source::getFormat(bool audio) {
1225 sp<MetaData> meta = getFormatMeta(audio);
1226
1227 if (meta == NULL) {
1228 return NULL;
1229 }
1230
1231 sp<AMessage> msg = new AMessage;
1232
1233 if(convertMetaDataToMessage(meta, &msg) == OK) {
1234 return msg;
1235 }
1236 return NULL;
1237}
1238
James Dong0d268a32012-08-31 12:18:27 -07001239status_t NuPlayer::setVideoScalingMode(int32_t mode) {
1240 mVideoScalingMode = mode;
Andreas Huber57a339c2012-12-03 11:18:00 -08001241 if (mNativeWindow != NULL) {
James Dong0d268a32012-08-31 12:18:27 -07001242 status_t ret = native_window_set_scaling_mode(
1243 mNativeWindow->getNativeWindow().get(), mVideoScalingMode);
1244 if (ret != OK) {
1245 ALOGE("Failed to set scaling mode (%d): %s",
1246 -ret, strerror(-ret));
1247 return ret;
1248 }
1249 }
1250 return OK;
1251}
1252
Chong Zhangdcb89b32013-08-06 09:44:47 -07001253status_t NuPlayer::getTrackInfo(Parcel* reply) const {
1254 sp<AMessage> msg = new AMessage(kWhatGetTrackInfo, id());
1255 msg->setPointer("reply", reply);
1256
1257 sp<AMessage> response;
1258 status_t err = msg->postAndAwaitResponse(&response);
1259 return err;
1260}
1261
1262status_t NuPlayer::selectTrack(size_t trackIndex, bool select) {
1263 sp<AMessage> msg = new AMessage(kWhatSelectTrack, id());
1264 msg->setSize("trackIndex", trackIndex);
1265 msg->setInt32("select", select);
1266
1267 sp<AMessage> response;
1268 status_t err = msg->postAndAwaitResponse(&response);
1269
Chong Zhang404fced2014-06-11 14:45:31 -07001270 if (err != OK) {
1271 return err;
1272 }
1273
1274 if (!response->findInt32("err", &err)) {
1275 err = OK;
1276 }
1277
Chong Zhangdcb89b32013-08-06 09:44:47 -07001278 return err;
1279}
1280
Andreas Huberb7c8e912012-11-27 15:02:53 -08001281void NuPlayer::schedulePollDuration() {
1282 sp<AMessage> msg = new AMessage(kWhatPollDuration, id());
1283 msg->setInt32("generation", mPollDurationGeneration);
1284 msg->post();
1285}
1286
1287void NuPlayer::cancelPollDuration() {
1288 ++mPollDurationGeneration;
1289}
1290
Andreas Hubera1f8ab02012-11-30 10:53:22 -08001291void NuPlayer::processDeferredActions() {
1292 while (!mDeferredActions.empty()) {
1293 // We won't execute any deferred actions until we're no longer in
1294 // an intermediate state, i.e. one more more decoders are currently
1295 // flushing or shutting down.
1296
1297 if (mRenderer != NULL) {
1298 // There's an edge case where the renderer owns all output
1299 // buffers and is paused, therefore the decoder will not read
1300 // more input data and will never encounter the matching
1301 // discontinuity. To avoid this, we resume the renderer.
1302
1303 if (mFlushingAudio == AWAITING_DISCONTINUITY
1304 || mFlushingVideo == AWAITING_DISCONTINUITY) {
1305 mRenderer->resume();
1306 }
1307 }
1308
1309 if (mFlushingAudio != NONE || mFlushingVideo != NONE) {
1310 // We're currently flushing, postpone the reset until that's
1311 // completed.
1312
1313 ALOGV("postponing action mFlushingAudio=%d, mFlushingVideo=%d",
1314 mFlushingAudio, mFlushingVideo);
1315
1316 break;
1317 }
1318
1319 sp<Action> action = *mDeferredActions.begin();
1320 mDeferredActions.erase(mDeferredActions.begin());
1321
1322 action->execute(this);
1323 }
1324}
1325
1326void NuPlayer::performSeek(int64_t seekTimeUs) {
1327 ALOGV("performSeek seekTimeUs=%lld us (%.2f secs)",
1328 seekTimeUs,
1329 seekTimeUs / 1E6);
1330
1331 mSource->seekTo(seekTimeUs);
1332
1333 if (mDriver != NULL) {
1334 sp<NuPlayerDriver> driver = mDriver.promote();
1335 if (driver != NULL) {
1336 driver->notifyPosition(seekTimeUs);
1337 driver->notifySeekComplete();
1338 }
1339 }
1340
1341 // everything's flushed, continue playback.
1342}
1343
1344void NuPlayer::performDecoderFlush() {
1345 ALOGV("performDecoderFlush");
1346
Andreas Huberda9740e2013-04-16 10:54:03 -07001347 if (mAudioDecoder == NULL && mVideoDecoder == NULL) {
Andreas Hubera1f8ab02012-11-30 10:53:22 -08001348 return;
1349 }
1350
1351 mTimeDiscontinuityPending = true;
1352
1353 if (mAudioDecoder != NULL) {
1354 flushDecoder(true /* audio */, false /* needShutdown */);
1355 }
1356
1357 if (mVideoDecoder != NULL) {
1358 flushDecoder(false /* audio */, false /* needShutdown */);
1359 }
1360}
1361
Andreas Huber14f76722013-01-15 09:04:18 -08001362void NuPlayer::performDecoderShutdown(bool audio, bool video) {
1363 ALOGV("performDecoderShutdown audio=%d, video=%d", audio, video);
Andreas Hubera1f8ab02012-11-30 10:53:22 -08001364
Andreas Huber14f76722013-01-15 09:04:18 -08001365 if ((!audio || mAudioDecoder == NULL)
1366 && (!video || mVideoDecoder == NULL)) {
Andreas Hubera1f8ab02012-11-30 10:53:22 -08001367 return;
1368 }
1369
1370 mTimeDiscontinuityPending = true;
1371
Andreas Huber14f76722013-01-15 09:04:18 -08001372 if (mFlushingAudio == NONE && (!audio || mAudioDecoder == NULL)) {
1373 mFlushingAudio = FLUSHED;
1374 }
1375
1376 if (mFlushingVideo == NONE && (!video || mVideoDecoder == NULL)) {
1377 mFlushingVideo = FLUSHED;
1378 }
1379
1380 if (audio && mAudioDecoder != NULL) {
Andreas Hubera1f8ab02012-11-30 10:53:22 -08001381 flushDecoder(true /* audio */, true /* needShutdown */);
1382 }
1383
Andreas Huber14f76722013-01-15 09:04:18 -08001384 if (video && mVideoDecoder != NULL) {
Andreas Hubera1f8ab02012-11-30 10:53:22 -08001385 flushDecoder(false /* audio */, true /* needShutdown */);
1386 }
1387}
1388
1389void NuPlayer::performReset() {
1390 ALOGV("performReset");
1391
1392 CHECK(mAudioDecoder == NULL);
1393 CHECK(mVideoDecoder == NULL);
1394
1395 cancelPollDuration();
1396
1397 ++mScanSourcesGeneration;
1398 mScanSourcesPending = false;
1399
1400 mRenderer.clear();
1401
1402 if (mSource != NULL) {
1403 mSource->stop();
Andreas Huberb5f25f02013-02-05 10:14:26 -08001404
1405 looper()->unregisterHandler(mSource->id());
1406
Andreas Hubera1f8ab02012-11-30 10:53:22 -08001407 mSource.clear();
1408 }
1409
1410 if (mDriver != NULL) {
1411 sp<NuPlayerDriver> driver = mDriver.promote();
1412 if (driver != NULL) {
1413 driver->notifyResetComplete();
1414 }
1415 }
Andreas Huber57a339c2012-12-03 11:18:00 -08001416
1417 mStarted = false;
Andreas Hubera1f8ab02012-11-30 10:53:22 -08001418}
1419
1420void NuPlayer::performScanSources() {
1421 ALOGV("performScanSources");
1422
Andreas Huber57a339c2012-12-03 11:18:00 -08001423 if (!mStarted) {
1424 return;
1425 }
1426
Andreas Hubera1f8ab02012-11-30 10:53:22 -08001427 if (mAudioDecoder == NULL || mVideoDecoder == NULL) {
1428 postScanSources();
1429 }
1430}
1431
Andreas Huber57a339c2012-12-03 11:18:00 -08001432void NuPlayer::performSetSurface(const sp<NativeWindowWrapper> &wrapper) {
1433 ALOGV("performSetSurface");
1434
1435 mNativeWindow = wrapper;
1436
1437 // XXX - ignore error from setVideoScalingMode for now
1438 setVideoScalingMode(mVideoScalingMode);
1439
1440 if (mDriver != NULL) {
1441 sp<NuPlayerDriver> driver = mDriver.promote();
1442 if (driver != NULL) {
1443 driver->notifySetSurfaceComplete();
1444 }
1445 }
1446}
1447
Andreas Huber9575c962013-02-05 13:59:56 -08001448void NuPlayer::onSourceNotify(const sp<AMessage> &msg) {
1449 int32_t what;
1450 CHECK(msg->findInt32("what", &what));
1451
1452 switch (what) {
1453 case Source::kWhatPrepared:
1454 {
Andreas Huberb5f28d42013-04-25 15:11:19 -07001455 if (mSource == NULL) {
1456 // This is a stale notification from a source that was
1457 // asynchronously preparing when the client called reset().
1458 // We handled the reset, the source is gone.
1459 break;
1460 }
1461
Andreas Huberec0c5972013-02-05 14:47:13 -08001462 int32_t err;
1463 CHECK(msg->findInt32("err", &err));
1464
Andreas Huber9575c962013-02-05 13:59:56 -08001465 sp<NuPlayerDriver> driver = mDriver.promote();
1466 if (driver != NULL) {
Marco Nelissendd114d12014-05-28 15:23:14 -07001467 // notify duration first, so that it's definitely set when
1468 // the app received the "prepare complete" callback.
1469 int64_t durationUs;
1470 if (mSource->getDuration(&durationUs) == OK) {
1471 driver->notifyDuration(durationUs);
1472 }
Andreas Huberec0c5972013-02-05 14:47:13 -08001473 driver->notifyPrepareCompleted(err);
Andreas Huber9575c962013-02-05 13:59:56 -08001474 }
Andreas Huber99759402013-04-01 14:28:31 -07001475
Andreas Huber9575c962013-02-05 13:59:56 -08001476 break;
1477 }
1478
1479 case Source::kWhatFlagsChanged:
1480 {
1481 uint32_t flags;
1482 CHECK(msg->findInt32("flags", (int32_t *)&flags));
1483
Chong Zhang4b7069d2013-09-11 12:52:43 -07001484 sp<NuPlayerDriver> driver = mDriver.promote();
1485 if (driver != NULL) {
1486 driver->notifyFlagsChanged(flags);
1487 }
1488
Andreas Huber9575c962013-02-05 13:59:56 -08001489 if ((mSourceFlags & Source::FLAG_DYNAMIC_DURATION)
1490 && (!(flags & Source::FLAG_DYNAMIC_DURATION))) {
1491 cancelPollDuration();
1492 } else if (!(mSourceFlags & Source::FLAG_DYNAMIC_DURATION)
1493 && (flags & Source::FLAG_DYNAMIC_DURATION)
1494 && (mAudioDecoder != NULL || mVideoDecoder != NULL)) {
1495 schedulePollDuration();
1496 }
1497
1498 mSourceFlags = flags;
1499 break;
1500 }
1501
1502 case Source::kWhatVideoSizeChanged:
1503 {
1504 int32_t width, height;
1505 CHECK(msg->findInt32("width", &width));
1506 CHECK(msg->findInt32("height", &height));
1507
1508 notifyListener(MEDIA_SET_VIDEO_SIZE, width, height);
1509 break;
1510 }
1511
Roger Jönssonb50e83e2013-01-21 16:26:41 +01001512 case Source::kWhatBufferingStart:
1513 {
1514 notifyListener(MEDIA_INFO, MEDIA_INFO_BUFFERING_START, 0);
1515 break;
1516 }
1517
1518 case Source::kWhatBufferingEnd:
1519 {
1520 notifyListener(MEDIA_INFO, MEDIA_INFO_BUFFERING_END, 0);
1521 break;
1522 }
1523
Chong Zhangdcb89b32013-08-06 09:44:47 -07001524 case Source::kWhatSubtitleData:
1525 {
1526 sp<ABuffer> buffer;
1527 CHECK(msg->findBuffer("buffer", &buffer));
1528
Chong Zhang404fced2014-06-11 14:45:31 -07001529 sendSubtitleData(buffer, 0 /* baseIndex */);
Chong Zhangdcb89b32013-08-06 09:44:47 -07001530 break;
1531 }
1532
Andreas Huber14f76722013-01-15 09:04:18 -08001533 case Source::kWhatQueueDecoderShutdown:
1534 {
1535 int32_t audio, video;
1536 CHECK(msg->findInt32("audio", &audio));
1537 CHECK(msg->findInt32("video", &video));
1538
1539 sp<AMessage> reply;
1540 CHECK(msg->findMessage("reply", &reply));
1541
1542 queueDecoderShutdown(audio, video, reply);
1543 break;
1544 }
1545
Andreas Huber9575c962013-02-05 13:59:56 -08001546 default:
1547 TRESPASS();
1548 }
1549}
1550
Chong Zhanga7fa1d92014-06-11 14:49:23 -07001551void NuPlayer::onClosedCaptionNotify(const sp<AMessage> &msg) {
1552 int32_t what;
1553 CHECK(msg->findInt32("what", &what));
1554
1555 switch (what) {
1556 case NuPlayer::CCDecoder::kWhatClosedCaptionData:
1557 {
1558 sp<ABuffer> buffer;
1559 CHECK(msg->findBuffer("buffer", &buffer));
1560
1561 size_t inbandTracks = 0;
1562 if (mSource != NULL) {
1563 inbandTracks = mSource->getTrackCount();
1564 }
1565
1566 sendSubtitleData(buffer, inbandTracks);
1567 break;
1568 }
1569
1570 case NuPlayer::CCDecoder::kWhatTrackAdded:
1571 {
1572 notifyListener(MEDIA_INFO, MEDIA_INFO_METADATA_UPDATE, 0);
1573
1574 break;
1575 }
1576
1577 default:
1578 TRESPASS();
1579 }
1580
1581
1582}
1583
Chong Zhang404fced2014-06-11 14:45:31 -07001584void NuPlayer::sendSubtitleData(const sp<ABuffer> &buffer, int32_t baseIndex) {
1585 int32_t trackIndex;
1586 int64_t timeUs, durationUs;
1587 CHECK(buffer->meta()->findInt32("trackIndex", &trackIndex));
1588 CHECK(buffer->meta()->findInt64("timeUs", &timeUs));
1589 CHECK(buffer->meta()->findInt64("durationUs", &durationUs));
1590
1591 Parcel in;
1592 in.writeInt32(trackIndex + baseIndex);
1593 in.writeInt64(timeUs);
1594 in.writeInt64(durationUs);
1595 in.writeInt32(buffer->size());
1596 in.writeInt32(buffer->size());
1597 in.write(buffer->data(), buffer->size());
1598
1599 notifyListener(MEDIA_SUBTITLE_DATA, 0, 0, &in);
1600}
Andreas Huberb5f25f02013-02-05 10:14:26 -08001601////////////////////////////////////////////////////////////////////////////////
1602
Andreas Huber9575c962013-02-05 13:59:56 -08001603void NuPlayer::Source::notifyFlagsChanged(uint32_t flags) {
1604 sp<AMessage> notify = dupNotify();
1605 notify->setInt32("what", kWhatFlagsChanged);
1606 notify->setInt32("flags", flags);
1607 notify->post();
1608}
1609
1610void NuPlayer::Source::notifyVideoSizeChanged(int32_t width, int32_t height) {
1611 sp<AMessage> notify = dupNotify();
1612 notify->setInt32("what", kWhatVideoSizeChanged);
1613 notify->setInt32("width", width);
1614 notify->setInt32("height", height);
1615 notify->post();
1616}
1617
Andreas Huberec0c5972013-02-05 14:47:13 -08001618void NuPlayer::Source::notifyPrepared(status_t err) {
Andreas Huber9575c962013-02-05 13:59:56 -08001619 sp<AMessage> notify = dupNotify();
1620 notify->setInt32("what", kWhatPrepared);
Andreas Huberec0c5972013-02-05 14:47:13 -08001621 notify->setInt32("err", err);
Andreas Huber9575c962013-02-05 13:59:56 -08001622 notify->post();
1623}
1624
Andreas Huber84333e02014-02-07 15:36:10 -08001625void NuPlayer::Source::onMessageReceived(const sp<AMessage> & /* msg */) {
Andreas Huberb5f25f02013-02-05 10:14:26 -08001626 TRESPASS();
1627}
1628
Andreas Huber14f76722013-01-15 09:04:18 -08001629void NuPlayer::queueDecoderShutdown(
1630 bool audio, bool video, const sp<AMessage> &reply) {
1631 ALOGI("queueDecoderShutdown audio=%d, video=%d", audio, video);
1632
1633 mDeferredActions.push_back(
1634 new ShutdownDecoderAction(audio, video));
1635
1636 mDeferredActions.push_back(
1637 new SimpleAction(&NuPlayer::performScanSources));
1638
1639 mDeferredActions.push_back(new PostMessageAction(reply));
1640
1641 processDeferredActions();
1642}
1643
Andreas Huberf9334412010-12-15 15:17:42 -08001644} // namespace android