blob: d3ec122a18143ce487851fb868b83d3d0091af35 [file] [log] [blame]
Andreas Huberf9334412010-12-15 15:17:42 -08001/*
2 * Copyright (C) 2010 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17//#define LOG_NDEBUG 0
18#define LOG_TAG "NuPlayer"
19#include <utils/Log.h>
20
21#include "NuPlayer.h"
Andreas Huber5bc087c2010-12-23 10:27:40 -080022
23#include "HTTPLiveSource.h"
Andreas Huberf9334412010-12-15 15:17:42 -080024#include "NuPlayerDecoder.h"
Andreas Huber43c3e6c2011-01-05 12:17:08 -080025#include "NuPlayerDriver.h"
Andreas Huberf9334412010-12-15 15:17:42 -080026#include "NuPlayerRenderer.h"
Andreas Huber5bc087c2010-12-23 10:27:40 -080027#include "NuPlayerSource.h"
Andreas Huber2bfdd422011-10-11 15:24:07 -070028#include "RTSPSource.h"
Andreas Huber5bc087c2010-12-23 10:27:40 -080029#include "StreamingSource.h"
Andreas Huberafed0e12011-09-20 15:39:58 -070030#include "GenericSource.h"
Andreas Huber84066782011-08-16 09:34:26 -070031#include "mp4/MP4Source.h"
Andreas Huber5bc087c2010-12-23 10:27:40 -080032
33#include "ATSParser.h"
Andreas Huberf9334412010-12-15 15:17:42 -080034
Andreas Huber84066782011-08-16 09:34:26 -070035#include <cutils/properties.h> // for property_get
Andreas Huber3831a062010-12-21 10:22:33 -080036#include <media/stagefright/foundation/hexdump.h>
Andreas Huberf9334412010-12-15 15:17:42 -080037#include <media/stagefright/foundation/ABuffer.h>
38#include <media/stagefright/foundation/ADebug.h>
39#include <media/stagefright/foundation/AMessage.h>
40#include <media/stagefright/ACodec.h>
Andreas Huber3fe62152011-09-16 15:09:22 -070041#include <media/stagefright/MediaDefs.h>
Andreas Huberf9334412010-12-15 15:17:42 -080042#include <media/stagefright/MediaErrors.h>
43#include <media/stagefright/MetaData.h>
Glenn Kasten11731182011-02-08 17:26:17 -080044#include <gui/ISurfaceTexture.h>
Andreas Huberf9334412010-12-15 15:17:42 -080045
Andreas Huber3fe62152011-09-16 15:09:22 -070046#include "avc_utils.h"
47
Andreas Huber84066782011-08-16 09:34:26 -070048#include "ESDS.h"
49#include <media/stagefright/Utils.h>
50
Andreas Huberf9334412010-12-15 15:17:42 -080051namespace android {
52
53////////////////////////////////////////////////////////////////////////////////
54
55NuPlayer::NuPlayer()
Andreas Huber9b80c2b2011-06-30 15:47:02 -070056 : mUIDValid(false),
Andreas Huber3fe62152011-09-16 15:09:22 -070057 mVideoIsAVC(false),
Andreas Huber9b80c2b2011-06-30 15:47:02 -070058 mAudioEOS(false),
Andreas Huberf9334412010-12-15 15:17:42 -080059 mVideoEOS(false),
Andreas Huber5bc087c2010-12-23 10:27:40 -080060 mScanSourcesPending(false),
Andreas Huber1aef2112011-01-04 14:01:29 -080061 mScanSourcesGeneration(0),
Andreas Huberb7c8e912012-11-27 15:02:53 -080062 mPollDurationGeneration(0),
Andreas Huber6e3d3112011-11-28 12:36:11 -080063 mTimeDiscontinuityPending(false),
Andreas Huberf9334412010-12-15 15:17:42 -080064 mFlushingAudio(NONE),
Andreas Huber1aef2112011-01-04 14:01:29 -080065 mFlushingVideo(NONE),
66 mResetInProgress(false),
Andreas Huber3fe62152011-09-16 15:09:22 -070067 mResetPostponed(false),
68 mSkipRenderingAudioUntilMediaTimeUs(-1ll),
69 mSkipRenderingVideoUntilMediaTimeUs(-1ll),
70 mVideoLateByUs(0ll),
71 mNumFramesTotal(0ll),
James Dong0d268a32012-08-31 12:18:27 -070072 mNumFramesDropped(0ll),
73 mVideoScalingMode(NATIVE_WINDOW_SCALING_MODE_SCALE_TO_WINDOW) {
Andreas Huberf9334412010-12-15 15:17:42 -080074}
75
76NuPlayer::~NuPlayer() {
77}
78
Andreas Huber9b80c2b2011-06-30 15:47:02 -070079void NuPlayer::setUID(uid_t uid) {
80 mUIDValid = true;
81 mUID = uid;
82}
83
Andreas Huber43c3e6c2011-01-05 12:17:08 -080084void NuPlayer::setDriver(const wp<NuPlayerDriver> &driver) {
85 mDriver = driver;
Andreas Huberf9334412010-12-15 15:17:42 -080086}
87
88void NuPlayer::setDataSource(const sp<IStreamSource> &source) {
89 sp<AMessage> msg = new AMessage(kWhatSetDataSource, id());
90
Andreas Huber84066782011-08-16 09:34:26 -070091 char prop[PROPERTY_VALUE_MAX];
92 if (property_get("media.stagefright.use-mp4source", prop, NULL)
93 && (!strcmp(prop, "1") || !strcasecmp(prop, "true"))) {
94 msg->setObject("source", new MP4Source(source));
95 } else {
96 msg->setObject("source", new StreamingSource(source));
97 }
98
Andreas Huber5bc087c2010-12-23 10:27:40 -080099 msg->post();
100}
Andreas Huberf9334412010-12-15 15:17:42 -0800101
Andreas Huberafed0e12011-09-20 15:39:58 -0700102static bool IsHTTPLiveURL(const char *url) {
103 if (!strncasecmp("http://", url, 7)
104 || !strncasecmp("https://", url, 8)) {
105 size_t len = strlen(url);
106 if (len >= 5 && !strcasecmp(".m3u8", &url[len - 5])) {
107 return true;
108 }
109
110 if (strstr(url,"m3u8")) {
111 return true;
112 }
113 }
114
115 return false;
116}
117
Andreas Huber5bc087c2010-12-23 10:27:40 -0800118void NuPlayer::setDataSource(
119 const char *url, const KeyedVector<String8, String8> *headers) {
120 sp<AMessage> msg = new AMessage(kWhatSetDataSource, id());
121
Andreas Huberafed0e12011-09-20 15:39:58 -0700122 sp<Source> source;
123 if (IsHTTPLiveURL(url)) {
124 source = new HTTPLiveSource(url, headers, mUIDValid, mUID);
125 } else if (!strncasecmp(url, "rtsp://", 7)) {
126 source = new RTSPSource(url, headers, mUIDValid, mUID);
Andreas Huber2bfdd422011-10-11 15:24:07 -0700127 } else {
Andreas Huberafed0e12011-09-20 15:39:58 -0700128 source = new GenericSource(url, headers, mUIDValid, mUID);
Andreas Huber2bfdd422011-10-11 15:24:07 -0700129 }
130
Andreas Huberafed0e12011-09-20 15:39:58 -0700131 msg->setObject("source", source);
132 msg->post();
133}
134
135void NuPlayer::setDataSource(int fd, int64_t offset, int64_t length) {
136 sp<AMessage> msg = new AMessage(kWhatSetDataSource, id());
137
138 sp<Source> source = new GenericSource(fd, offset, length);
139 msg->setObject("source", source);
Andreas Huberf9334412010-12-15 15:17:42 -0800140 msg->post();
141}
142
Glenn Kasten11731182011-02-08 17:26:17 -0800143void NuPlayer::setVideoSurfaceTexture(const sp<ISurfaceTexture> &surfaceTexture) {
144 sp<AMessage> msg = new AMessage(kWhatSetVideoNativeWindow, id());
145 sp<SurfaceTextureClient> surfaceTextureClient(surfaceTexture != NULL ?
146 new SurfaceTextureClient(surfaceTexture) : NULL);
147 msg->setObject("native-window", new NativeWindowWrapper(surfaceTextureClient));
Andreas Huberf9334412010-12-15 15:17:42 -0800148 msg->post();
149}
150
151void NuPlayer::setAudioSink(const sp<MediaPlayerBase::AudioSink> &sink) {
152 sp<AMessage> msg = new AMessage(kWhatSetAudioSink, id());
153 msg->setObject("sink", sink);
154 msg->post();
155}
156
157void NuPlayer::start() {
158 (new AMessage(kWhatStart, id()))->post();
159}
160
Andreas Huber43c3e6c2011-01-05 12:17:08 -0800161void NuPlayer::pause() {
Andreas Huberb4082222011-01-20 15:23:04 -0800162 (new AMessage(kWhatPause, id()))->post();
Andreas Huber43c3e6c2011-01-05 12:17:08 -0800163}
164
165void NuPlayer::resume() {
Andreas Huberb4082222011-01-20 15:23:04 -0800166 (new AMessage(kWhatResume, id()))->post();
Andreas Huber43c3e6c2011-01-05 12:17:08 -0800167}
168
Andreas Huber1aef2112011-01-04 14:01:29 -0800169void NuPlayer::resetAsync() {
170 (new AMessage(kWhatReset, id()))->post();
171}
172
Andreas Huber43c3e6c2011-01-05 12:17:08 -0800173void NuPlayer::seekToAsync(int64_t seekTimeUs) {
174 sp<AMessage> msg = new AMessage(kWhatSeek, id());
175 msg->setInt64("seekTimeUs", seekTimeUs);
176 msg->post();
177}
178
Andreas Huber53df1a42010-12-22 10:03:04 -0800179// static
Andreas Huber1aef2112011-01-04 14:01:29 -0800180bool NuPlayer::IsFlushingState(FlushStatus state, bool *needShutdown) {
Andreas Huber53df1a42010-12-22 10:03:04 -0800181 switch (state) {
182 case FLUSHING_DECODER:
Andreas Huber1aef2112011-01-04 14:01:29 -0800183 if (needShutdown != NULL) {
184 *needShutdown = false;
Andreas Huber53df1a42010-12-22 10:03:04 -0800185 }
186 return true;
187
Andreas Huber1aef2112011-01-04 14:01:29 -0800188 case FLUSHING_DECODER_SHUTDOWN:
189 if (needShutdown != NULL) {
190 *needShutdown = true;
Andreas Huber53df1a42010-12-22 10:03:04 -0800191 }
192 return true;
193
194 default:
195 return false;
196 }
197}
198
Andreas Huberf9334412010-12-15 15:17:42 -0800199void NuPlayer::onMessageReceived(const sp<AMessage> &msg) {
200 switch (msg->what()) {
201 case kWhatSetDataSource:
202 {
Steve Block3856b092011-10-20 11:56:00 +0100203 ALOGV("kWhatSetDataSource");
Andreas Huberf9334412010-12-15 15:17:42 -0800204
205 CHECK(mSource == NULL);
206
Andreas Huber5bc087c2010-12-23 10:27:40 -0800207 sp<RefBase> obj;
208 CHECK(msg->findObject("source", &obj));
Andreas Huberf9334412010-12-15 15:17:42 -0800209
Andreas Huber5bc087c2010-12-23 10:27:40 -0800210 mSource = static_cast<Source *>(obj.get());
Andreas Huberf9334412010-12-15 15:17:42 -0800211 break;
212 }
213
Andreas Huberb7c8e912012-11-27 15:02:53 -0800214 case kWhatPollDuration:
215 {
216 int32_t generation;
217 CHECK(msg->findInt32("generation", &generation));
218
219 if (generation != mPollDurationGeneration) {
220 // stale
221 break;
222 }
223
224 int64_t durationUs;
225 if (mDriver != NULL && mSource->getDuration(&durationUs) == OK) {
226 sp<NuPlayerDriver> driver = mDriver.promote();
227 if (driver != NULL) {
228 driver->notifyDuration(durationUs);
229 }
230 }
231
232 msg->post(1000000ll); // poll again in a second.
233 break;
234 }
235
Glenn Kasten11731182011-02-08 17:26:17 -0800236 case kWhatSetVideoNativeWindow:
Andreas Huberf9334412010-12-15 15:17:42 -0800237 {
Steve Block3856b092011-10-20 11:56:00 +0100238 ALOGV("kWhatSetVideoNativeWindow");
Andreas Huberf9334412010-12-15 15:17:42 -0800239
240 sp<RefBase> obj;
Glenn Kasten11731182011-02-08 17:26:17 -0800241 CHECK(msg->findObject("native-window", &obj));
Andreas Huberf9334412010-12-15 15:17:42 -0800242
Glenn Kasten11731182011-02-08 17:26:17 -0800243 mNativeWindow = static_cast<NativeWindowWrapper *>(obj.get());
James Dong0d268a32012-08-31 12:18:27 -0700244
245 // XXX - ignore error from setVideoScalingMode for now
246 setVideoScalingMode(mVideoScalingMode);
Andreas Huberf9334412010-12-15 15:17:42 -0800247 break;
248 }
249
250 case kWhatSetAudioSink:
251 {
Steve Block3856b092011-10-20 11:56:00 +0100252 ALOGV("kWhatSetAudioSink");
Andreas Huberf9334412010-12-15 15:17:42 -0800253
254 sp<RefBase> obj;
255 CHECK(msg->findObject("sink", &obj));
256
257 mAudioSink = static_cast<MediaPlayerBase::AudioSink *>(obj.get());
258 break;
259 }
260
261 case kWhatStart:
262 {
Steve Block3856b092011-10-20 11:56:00 +0100263 ALOGV("kWhatStart");
Andreas Huber43c3e6c2011-01-05 12:17:08 -0800264
Andreas Huber3fe62152011-09-16 15:09:22 -0700265 mVideoIsAVC = false;
Andreas Huber1aef2112011-01-04 14:01:29 -0800266 mAudioEOS = false;
267 mVideoEOS = false;
Andreas Huber32f3cef2011-03-02 15:34:46 -0800268 mSkipRenderingAudioUntilMediaTimeUs = -1;
269 mSkipRenderingVideoUntilMediaTimeUs = -1;
Andreas Huber3fe62152011-09-16 15:09:22 -0700270 mVideoLateByUs = 0;
271 mNumFramesTotal = 0;
272 mNumFramesDropped = 0;
Andreas Huber1aef2112011-01-04 14:01:29 -0800273
Andreas Huber5bc087c2010-12-23 10:27:40 -0800274 mSource->start();
Andreas Huberf9334412010-12-15 15:17:42 -0800275
276 mRenderer = new Renderer(
277 mAudioSink,
278 new AMessage(kWhatRendererNotify, id()));
279
280 looper()->registerHandler(mRenderer);
281
Andreas Huber1aef2112011-01-04 14:01:29 -0800282 postScanSources();
Andreas Huberf9334412010-12-15 15:17:42 -0800283 break;
284 }
285
286 case kWhatScanSources:
287 {
Andreas Huber1aef2112011-01-04 14:01:29 -0800288 int32_t generation;
289 CHECK(msg->findInt32("generation", &generation));
290 if (generation != mScanSourcesGeneration) {
291 // Drop obsolete msg.
292 break;
293 }
294
Andreas Huber5bc087c2010-12-23 10:27:40 -0800295 mScanSourcesPending = false;
296
Steve Block3856b092011-10-20 11:56:00 +0100297 ALOGV("scanning sources haveAudio=%d, haveVideo=%d",
Andreas Huber43c3e6c2011-01-05 12:17:08 -0800298 mAudioDecoder != NULL, mVideoDecoder != NULL);
299
Andreas Huberb7c8e912012-11-27 15:02:53 -0800300 bool mHadAnySourcesBefore =
301 (mAudioDecoder != NULL) || (mVideoDecoder != NULL);
302
Haynes Mathew George5d246ef2012-07-09 10:36:57 -0700303 if (mNativeWindow != NULL) {
304 instantiateDecoder(false, &mVideoDecoder);
305 }
Andreas Huberf9334412010-12-15 15:17:42 -0800306
307 if (mAudioSink != NULL) {
Andreas Huber5bc087c2010-12-23 10:27:40 -0800308 instantiateDecoder(true, &mAudioDecoder);
Andreas Huberf9334412010-12-15 15:17:42 -0800309 }
310
Andreas Huberb7c8e912012-11-27 15:02:53 -0800311 if (!mHadAnySourcesBefore
312 && (mAudioDecoder != NULL || mVideoDecoder != NULL)) {
313 // This is the first time we've found anything playable.
314
315 uint32_t flags = mSource->flags();
316
317 if (flags & Source::FLAG_DYNAMIC_DURATION) {
318 schedulePollDuration();
319 }
320 }
321
Andreas Hubereac68ba2011-09-27 12:12:25 -0700322 status_t err;
323 if ((err = mSource->feedMoreTSData()) != OK) {
Andreas Huber1aef2112011-01-04 14:01:29 -0800324 if (mAudioDecoder == NULL && mVideoDecoder == NULL) {
325 // We're not currently decoding anything (no audio or
326 // video tracks found) and we just ran out of input data.
Andreas Hubereac68ba2011-09-27 12:12:25 -0700327
328 if (err == ERROR_END_OF_STREAM) {
329 notifyListener(MEDIA_PLAYBACK_COMPLETE, 0, 0);
330 } else {
331 notifyListener(MEDIA_ERROR, MEDIA_ERROR_UNKNOWN, err);
332 }
Andreas Huber1aef2112011-01-04 14:01:29 -0800333 }
Andreas Huberf9334412010-12-15 15:17:42 -0800334 break;
335 }
336
Andreas Huberfbe9d812012-08-31 14:05:27 -0700337 if ((mAudioDecoder == NULL && mAudioSink != NULL)
338 || (mVideoDecoder == NULL && mNativeWindow != NULL)) {
Andreas Huberf9334412010-12-15 15:17:42 -0800339 msg->post(100000ll);
Andreas Huber5bc087c2010-12-23 10:27:40 -0800340 mScanSourcesPending = true;
Andreas Huberf9334412010-12-15 15:17:42 -0800341 }
342 break;
343 }
344
345 case kWhatVideoNotify:
346 case kWhatAudioNotify:
347 {
348 bool audio = msg->what() == kWhatAudioNotify;
349
350 sp<AMessage> codecRequest;
351 CHECK(msg->findMessage("codec-request", &codecRequest));
352
353 int32_t what;
354 CHECK(codecRequest->findInt32("what", &what));
355
356 if (what == ACodec::kWhatFillThisBuffer) {
357 status_t err = feedDecoderInputData(
358 audio, codecRequest);
359
Andreas Huber5bc087c2010-12-23 10:27:40 -0800360 if (err == -EWOULDBLOCK) {
Andreas Hubereac68ba2011-09-27 12:12:25 -0700361 if (mSource->feedMoreTSData() == OK) {
Andreas Huber1183a4a2011-11-03 11:00:21 -0700362 msg->post(10000ll);
Andreas Huber5bc087c2010-12-23 10:27:40 -0800363 }
Andreas Huberf9334412010-12-15 15:17:42 -0800364 }
365 } else if (what == ACodec::kWhatEOS) {
Andreas Huberdc9bacd2011-09-26 10:53:29 -0700366 int32_t err;
367 CHECK(codecRequest->findInt32("err", &err));
368
369 if (err == ERROR_END_OF_STREAM) {
Steve Block3856b092011-10-20 11:56:00 +0100370 ALOGV("got %s decoder EOS", audio ? "audio" : "video");
Andreas Huberdc9bacd2011-09-26 10:53:29 -0700371 } else {
Steve Block3856b092011-10-20 11:56:00 +0100372 ALOGV("got %s decoder EOS w/ error %d",
Andreas Huberdc9bacd2011-09-26 10:53:29 -0700373 audio ? "audio" : "video",
374 err);
375 }
376
377 mRenderer->queueEOS(audio, err);
Andreas Huberf9334412010-12-15 15:17:42 -0800378 } else if (what == ACodec::kWhatFlushCompleted) {
Andreas Huber1aef2112011-01-04 14:01:29 -0800379 bool needShutdown;
Andreas Huber53df1a42010-12-22 10:03:04 -0800380
Andreas Huberf9334412010-12-15 15:17:42 -0800381 if (audio) {
Andreas Huber1aef2112011-01-04 14:01:29 -0800382 CHECK(IsFlushingState(mFlushingAudio, &needShutdown));
Andreas Huberf9334412010-12-15 15:17:42 -0800383 mFlushingAudio = FLUSHED;
384 } else {
Andreas Huber1aef2112011-01-04 14:01:29 -0800385 CHECK(IsFlushingState(mFlushingVideo, &needShutdown));
Andreas Huberf9334412010-12-15 15:17:42 -0800386 mFlushingVideo = FLUSHED;
Andreas Huber3fe62152011-09-16 15:09:22 -0700387
388 mVideoLateByUs = 0;
Andreas Huberf9334412010-12-15 15:17:42 -0800389 }
390
Steve Block3856b092011-10-20 11:56:00 +0100391 ALOGV("decoder %s flush completed", audio ? "audio" : "video");
Andreas Huberf9334412010-12-15 15:17:42 -0800392
Andreas Huber1aef2112011-01-04 14:01:29 -0800393 if (needShutdown) {
Steve Block3856b092011-10-20 11:56:00 +0100394 ALOGV("initiating %s decoder shutdown",
Andreas Huber53df1a42010-12-22 10:03:04 -0800395 audio ? "audio" : "video");
Andreas Huberf9334412010-12-15 15:17:42 -0800396
Andreas Huber53df1a42010-12-22 10:03:04 -0800397 (audio ? mAudioDecoder : mVideoDecoder)->initiateShutdown();
Andreas Huberf9334412010-12-15 15:17:42 -0800398
Andreas Huber53df1a42010-12-22 10:03:04 -0800399 if (audio) {
400 mFlushingAudio = SHUTTING_DOWN_DECODER;
401 } else {
402 mFlushingVideo = SHUTTING_DOWN_DECODER;
403 }
Andreas Huberf9334412010-12-15 15:17:42 -0800404 }
Andreas Huber3831a062010-12-21 10:22:33 -0800405
406 finishFlushIfPossible();
Andreas Huber2c2814b2010-12-15 17:18:20 -0800407 } else if (what == ACodec::kWhatOutputFormatChanged) {
Andreas Huber31e25082011-01-10 10:38:31 -0800408 if (audio) {
409 int32_t numChannels;
410 CHECK(codecRequest->findInt32("channel-count", &numChannels));
Andreas Huber2c2814b2010-12-15 17:18:20 -0800411
Andreas Huber31e25082011-01-10 10:38:31 -0800412 int32_t sampleRate;
413 CHECK(codecRequest->findInt32("sample-rate", &sampleRate));
Andreas Huber2c2814b2010-12-15 17:18:20 -0800414
Steve Block3856b092011-10-20 11:56:00 +0100415 ALOGV("Audio output format changed to %d Hz, %d channels",
Andreas Huber31e25082011-01-10 10:38:31 -0800416 sampleRate, numChannels);
Andreas Huber2c2814b2010-12-15 17:18:20 -0800417
Andreas Huber31e25082011-01-10 10:38:31 -0800418 mAudioSink->close();
Eric Laurent1948eb32012-04-13 16:50:19 -0700419
420 audio_output_flags_t flags;
421 int64_t durationUs;
422 // FIXME: we should handle the case where the video decoder is created after
423 // we receive the format change indication. Current code will just make that
424 // we select deep buffer with video which should not be a problem as it should
425 // not prevent from keeping A/V sync.
426 if (mVideoDecoder == NULL &&
427 mSource->getDuration(&durationUs) == OK &&
428 durationUs > AUDIO_SINK_MIN_DEEP_BUFFER_DURATION_US) {
429 flags = AUDIO_OUTPUT_FLAG_DEEP_BUFFER;
430 } else {
431 flags = AUDIO_OUTPUT_FLAG_NONE;
432 }
433
Andreas Huber98065552012-05-03 11:33:01 -0700434 int32_t channelMask;
435 if (!codecRequest->findInt32("channel-mask", &channelMask)) {
436 channelMask = CHANNEL_MASK_USE_CHANNEL_ORDER;
437 }
438
Andreas Huber078cfcf2011-09-15 12:25:04 -0700439 CHECK_EQ(mAudioSink->open(
440 sampleRate,
441 numChannels,
Andreas Huber98065552012-05-03 11:33:01 -0700442 (audio_channel_mask_t)channelMask,
Andreas Huber078cfcf2011-09-15 12:25:04 -0700443 AUDIO_FORMAT_PCM_16_BIT,
Eric Laurent1948eb32012-04-13 16:50:19 -0700444 8 /* bufferCount */,
445 NULL,
446 NULL,
447 flags),
Andreas Huber078cfcf2011-09-15 12:25:04 -0700448 (status_t)OK);
Andreas Huber31e25082011-01-10 10:38:31 -0800449 mAudioSink->start();
Andreas Huber2c2814b2010-12-15 17:18:20 -0800450
Andreas Huber31e25082011-01-10 10:38:31 -0800451 mRenderer->signalAudioSinkChanged();
452 } else {
453 // video
Andreas Huber3831a062010-12-21 10:22:33 -0800454
Andreas Huber31e25082011-01-10 10:38:31 -0800455 int32_t width, height;
456 CHECK(codecRequest->findInt32("width", &width));
457 CHECK(codecRequest->findInt32("height", &height));
458
459 int32_t cropLeft, cropTop, cropRight, cropBottom;
460 CHECK(codecRequest->findRect(
461 "crop",
462 &cropLeft, &cropTop, &cropRight, &cropBottom));
463
Steve Block3856b092011-10-20 11:56:00 +0100464 ALOGV("Video output format changed to %d x %d "
Andreas Hubercb67cd12011-08-26 16:02:19 -0700465 "(crop: %d x %d @ (%d, %d))",
Andreas Huber31e25082011-01-10 10:38:31 -0800466 width, height,
Andreas Hubercb67cd12011-08-26 16:02:19 -0700467 (cropRight - cropLeft + 1),
468 (cropBottom - cropTop + 1),
469 cropLeft, cropTop);
Andreas Huber31e25082011-01-10 10:38:31 -0800470
471 notifyListener(
472 MEDIA_SET_VIDEO_SIZE,
473 cropRight - cropLeft + 1,
474 cropBottom - cropTop + 1);
475 }
Andreas Huber3831a062010-12-21 10:22:33 -0800476 } else if (what == ACodec::kWhatShutdownCompleted) {
Steve Block3856b092011-10-20 11:56:00 +0100477 ALOGV("%s shutdown completed", audio ? "audio" : "video");
Andreas Huber3831a062010-12-21 10:22:33 -0800478 if (audio) {
479 mAudioDecoder.clear();
480
481 CHECK_EQ((int)mFlushingAudio, (int)SHUTTING_DOWN_DECODER);
482 mFlushingAudio = SHUT_DOWN;
483 } else {
484 mVideoDecoder.clear();
485
486 CHECK_EQ((int)mFlushingVideo, (int)SHUTTING_DOWN_DECODER);
487 mFlushingVideo = SHUT_DOWN;
488 }
489
490 finishFlushIfPossible();
Andreas Huberc92fd242011-08-16 13:48:44 -0700491 } else if (what == ACodec::kWhatError) {
Steve Block29357bc2012-01-06 19:20:56 +0000492 ALOGE("Received error from %s decoder, aborting playback.",
Andreas Huberc92fd242011-08-16 13:48:44 -0700493 audio ? "audio" : "video");
494
495 mRenderer->queueEOS(audio, UNKNOWN_ERROR);
Andreas Huber57788222012-02-21 11:47:18 -0800496 } else if (what == ACodec::kWhatDrainThisBuffer) {
Andreas Huberf9334412010-12-15 15:17:42 -0800497 renderBuffer(audio, codecRequest);
Andreas Huber57788222012-02-21 11:47:18 -0800498 } else {
499 ALOGV("Unhandled codec notification %d.", what);
Andreas Huberf9334412010-12-15 15:17:42 -0800500 }
501
502 break;
503 }
504
505 case kWhatRendererNotify:
506 {
507 int32_t what;
508 CHECK(msg->findInt32("what", &what));
509
510 if (what == Renderer::kWhatEOS) {
511 int32_t audio;
512 CHECK(msg->findInt32("audio", &audio));
513
Andreas Huberc92fd242011-08-16 13:48:44 -0700514 int32_t finalResult;
515 CHECK(msg->findInt32("finalResult", &finalResult));
516
Andreas Huberf9334412010-12-15 15:17:42 -0800517 if (audio) {
518 mAudioEOS = true;
519 } else {
520 mVideoEOS = true;
521 }
522
Andreas Huberc92fd242011-08-16 13:48:44 -0700523 if (finalResult == ERROR_END_OF_STREAM) {
Steve Block3856b092011-10-20 11:56:00 +0100524 ALOGV("reached %s EOS", audio ? "audio" : "video");
Andreas Huberc92fd242011-08-16 13:48:44 -0700525 } else {
Steve Block29357bc2012-01-06 19:20:56 +0000526 ALOGE("%s track encountered an error (%d)",
Andreas Huberc92fd242011-08-16 13:48:44 -0700527 audio ? "audio" : "video", finalResult);
528
529 notifyListener(
530 MEDIA_ERROR, MEDIA_ERROR_UNKNOWN, finalResult);
531 }
Andreas Huberf9334412010-12-15 15:17:42 -0800532
533 if ((mAudioEOS || mAudioDecoder == NULL)
534 && (mVideoEOS || mVideoDecoder == NULL)) {
535 notifyListener(MEDIA_PLAYBACK_COMPLETE, 0, 0);
536 }
Andreas Huber43c3e6c2011-01-05 12:17:08 -0800537 } else if (what == Renderer::kWhatPosition) {
538 int64_t positionUs;
539 CHECK(msg->findInt64("positionUs", &positionUs));
540
Andreas Huber3fe62152011-09-16 15:09:22 -0700541 CHECK(msg->findInt64("videoLateByUs", &mVideoLateByUs));
542
Andreas Huber43c3e6c2011-01-05 12:17:08 -0800543 if (mDriver != NULL) {
544 sp<NuPlayerDriver> driver = mDriver.promote();
545 if (driver != NULL) {
546 driver->notifyPosition(positionUs);
Andreas Huber3fe62152011-09-16 15:09:22 -0700547
548 driver->notifyFrameStats(
549 mNumFramesTotal, mNumFramesDropped);
Andreas Huber43c3e6c2011-01-05 12:17:08 -0800550 }
551 }
Andreas Huber3fe62152011-09-16 15:09:22 -0700552 } else if (what == Renderer::kWhatFlushComplete) {
Andreas Huberf9334412010-12-15 15:17:42 -0800553 int32_t audio;
554 CHECK(msg->findInt32("audio", &audio));
555
Steve Block3856b092011-10-20 11:56:00 +0100556 ALOGV("renderer %s flush completed.", audio ? "audio" : "video");
James Dongf57b4ea2012-07-20 13:38:36 -0700557 } else if (what == Renderer::kWhatVideoRenderingStart) {
558 notifyListener(MEDIA_INFO, MEDIA_INFO_RENDERING_START, 0);
Andreas Huberf9334412010-12-15 15:17:42 -0800559 }
560 break;
561 }
562
563 case kWhatMoreDataQueued:
564 {
565 break;
566 }
567
Andreas Huber1aef2112011-01-04 14:01:29 -0800568 case kWhatReset:
569 {
Steve Block3856b092011-10-20 11:56:00 +0100570 ALOGV("kWhatReset");
Andreas Huber1aef2112011-01-04 14:01:29 -0800571
Andreas Huberb7c8e912012-11-27 15:02:53 -0800572 cancelPollDuration();
573
Andreas Huberb58ce9f2011-11-28 16:27:35 -0800574 if (mRenderer != NULL) {
575 // There's an edge case where the renderer owns all output
576 // buffers and is paused, therefore the decoder will not read
577 // more input data and will never encounter the matching
578 // discontinuity. To avoid this, we resume the renderer.
579
580 if (mFlushingAudio == AWAITING_DISCONTINUITY
581 || mFlushingVideo == AWAITING_DISCONTINUITY) {
582 mRenderer->resume();
583 }
584 }
585
Andreas Huber1aef2112011-01-04 14:01:29 -0800586 if (mFlushingAudio != NONE || mFlushingVideo != NONE) {
587 // We're currently flushing, postpone the reset until that's
588 // completed.
589
Andreas Huberea9d51b2011-11-30 09:53:40 -0800590 ALOGV("postponing reset mFlushingAudio=%d, mFlushingVideo=%d",
591 mFlushingAudio, mFlushingVideo);
Andreas Huber1aef2112011-01-04 14:01:29 -0800592
593 mResetPostponed = true;
594 break;
595 }
596
597 if (mAudioDecoder == NULL && mVideoDecoder == NULL) {
598 finishReset();
599 break;
600 }
601
Andreas Huber6e3d3112011-11-28 12:36:11 -0800602 mTimeDiscontinuityPending = true;
603
Andreas Huber1aef2112011-01-04 14:01:29 -0800604 if (mAudioDecoder != NULL) {
605 flushDecoder(true /* audio */, true /* needShutdown */);
606 }
607
608 if (mVideoDecoder != NULL) {
609 flushDecoder(false /* audio */, true /* needShutdown */);
610 }
611
612 mResetInProgress = true;
613 break;
614 }
615
Andreas Huber43c3e6c2011-01-05 12:17:08 -0800616 case kWhatSeek:
617 {
618 int64_t seekTimeUs;
619 CHECK(msg->findInt64("seekTimeUs", &seekTimeUs));
620
Steve Block3856b092011-10-20 11:56:00 +0100621 ALOGV("kWhatSeek seekTimeUs=%lld us (%.2f secs)",
Andreas Huber43c3e6c2011-01-05 12:17:08 -0800622 seekTimeUs, seekTimeUs / 1E6);
623
624 mSource->seekTo(seekTimeUs);
625
626 if (mDriver != NULL) {
627 sp<NuPlayerDriver> driver = mDriver.promote();
628 if (driver != NULL) {
629 driver->notifySeekComplete();
630 }
631 }
632
633 break;
634 }
635
Andreas Huberb4082222011-01-20 15:23:04 -0800636 case kWhatPause:
637 {
638 CHECK(mRenderer != NULL);
639 mRenderer->pause();
640 break;
641 }
642
643 case kWhatResume:
644 {
645 CHECK(mRenderer != NULL);
646 mRenderer->resume();
647 break;
648 }
649
Andreas Huberf9334412010-12-15 15:17:42 -0800650 default:
651 TRESPASS();
652 break;
653 }
654}
655
Andreas Huber3831a062010-12-21 10:22:33 -0800656void NuPlayer::finishFlushIfPossible() {
657 if (mFlushingAudio != FLUSHED && mFlushingAudio != SHUT_DOWN) {
658 return;
659 }
660
661 if (mFlushingVideo != FLUSHED && mFlushingVideo != SHUT_DOWN) {
662 return;
663 }
664
Steve Block3856b092011-10-20 11:56:00 +0100665 ALOGV("both audio and video are flushed now.");
Andreas Huber3831a062010-12-21 10:22:33 -0800666
Andreas Huber6e3d3112011-11-28 12:36:11 -0800667 if (mTimeDiscontinuityPending) {
668 mRenderer->signalTimeDiscontinuity();
669 mTimeDiscontinuityPending = false;
670 }
Andreas Huber3831a062010-12-21 10:22:33 -0800671
Andreas Huber22fc52f2011-01-05 16:24:27 -0800672 if (mAudioDecoder != NULL) {
Andreas Huber3831a062010-12-21 10:22:33 -0800673 mAudioDecoder->signalResume();
674 }
675
Andreas Huber22fc52f2011-01-05 16:24:27 -0800676 if (mVideoDecoder != NULL) {
Andreas Huber3831a062010-12-21 10:22:33 -0800677 mVideoDecoder->signalResume();
678 }
679
680 mFlushingAudio = NONE;
681 mFlushingVideo = NONE;
Andreas Huber3831a062010-12-21 10:22:33 -0800682
Andreas Huber1aef2112011-01-04 14:01:29 -0800683 if (mResetInProgress) {
Steve Block3856b092011-10-20 11:56:00 +0100684 ALOGV("reset completed");
Andreas Huber1aef2112011-01-04 14:01:29 -0800685
686 mResetInProgress = false;
687 finishReset();
688 } else if (mResetPostponed) {
689 (new AMessage(kWhatReset, id()))->post();
690 mResetPostponed = false;
Andreas Huber22fc52f2011-01-05 16:24:27 -0800691 } else if (mAudioDecoder == NULL || mVideoDecoder == NULL) {
Andreas Huber1aef2112011-01-04 14:01:29 -0800692 postScanSources();
Andreas Huberf9334412010-12-15 15:17:42 -0800693 }
694}
695
Andreas Huber1aef2112011-01-04 14:01:29 -0800696void NuPlayer::finishReset() {
697 CHECK(mAudioDecoder == NULL);
698 CHECK(mVideoDecoder == NULL);
699
Andreas Huber2bfdd422011-10-11 15:24:07 -0700700 ++mScanSourcesGeneration;
701 mScanSourcesPending = false;
702
Andreas Huber1aef2112011-01-04 14:01:29 -0800703 mRenderer.clear();
Andreas Huber2bfdd422011-10-11 15:24:07 -0700704
705 if (mSource != NULL) {
706 mSource->stop();
707 mSource.clear();
708 }
Andreas Huber1aef2112011-01-04 14:01:29 -0800709
Andreas Huber43c3e6c2011-01-05 12:17:08 -0800710 if (mDriver != NULL) {
711 sp<NuPlayerDriver> driver = mDriver.promote();
712 if (driver != NULL) {
713 driver->notifyResetComplete();
714 }
715 }
Andreas Huber1aef2112011-01-04 14:01:29 -0800716}
717
718void NuPlayer::postScanSources() {
719 if (mScanSourcesPending) {
720 return;
721 }
722
723 sp<AMessage> msg = new AMessage(kWhatScanSources, id());
724 msg->setInt32("generation", mScanSourcesGeneration);
725 msg->post();
726
727 mScanSourcesPending = true;
728}
729
Andreas Huber5bc087c2010-12-23 10:27:40 -0800730status_t NuPlayer::instantiateDecoder(bool audio, sp<Decoder> *decoder) {
Andreas Huberf9334412010-12-15 15:17:42 -0800731 if (*decoder != NULL) {
732 return OK;
733 }
734
Andreas Huber84066782011-08-16 09:34:26 -0700735 sp<AMessage> format = mSource->getFormat(audio);
Andreas Huberf9334412010-12-15 15:17:42 -0800736
Andreas Huber84066782011-08-16 09:34:26 -0700737 if (format == NULL) {
Andreas Huberf9334412010-12-15 15:17:42 -0800738 return -EWOULDBLOCK;
739 }
740
Andreas Huber3fe62152011-09-16 15:09:22 -0700741 if (!audio) {
Andreas Huber84066782011-08-16 09:34:26 -0700742 AString mime;
743 CHECK(format->findString("mime", &mime));
744 mVideoIsAVC = !strcasecmp(MEDIA_MIMETYPE_VIDEO_AVC, mime.c_str());
Andreas Huber3fe62152011-09-16 15:09:22 -0700745 }
746
Andreas Huberf9334412010-12-15 15:17:42 -0800747 sp<AMessage> notify =
748 new AMessage(audio ? kWhatAudioNotify : kWhatVideoNotify,
749 id());
750
Glenn Kasten11731182011-02-08 17:26:17 -0800751 *decoder = audio ? new Decoder(notify) :
752 new Decoder(notify, mNativeWindow);
Andreas Huberf9334412010-12-15 15:17:42 -0800753 looper()->registerHandler(*decoder);
754
Andreas Huber84066782011-08-16 09:34:26 -0700755 (*decoder)->configure(format);
Andreas Huberf9334412010-12-15 15:17:42 -0800756
Andreas Huber43c3e6c2011-01-05 12:17:08 -0800757 int64_t durationUs;
758 if (mDriver != NULL && mSource->getDuration(&durationUs) == OK) {
759 sp<NuPlayerDriver> driver = mDriver.promote();
760 if (driver != NULL) {
761 driver->notifyDuration(durationUs);
762 }
763 }
764
Andreas Huberf9334412010-12-15 15:17:42 -0800765 return OK;
766}
767
768status_t NuPlayer::feedDecoderInputData(bool audio, const sp<AMessage> &msg) {
769 sp<AMessage> reply;
770 CHECK(msg->findMessage("reply", &reply));
771
Andreas Huber53df1a42010-12-22 10:03:04 -0800772 if ((audio && IsFlushingState(mFlushingAudio))
773 || (!audio && IsFlushingState(mFlushingVideo))) {
Andreas Huberf9334412010-12-15 15:17:42 -0800774 reply->setInt32("err", INFO_DISCONTINUITY);
775 reply->post();
776 return OK;
777 }
778
779 sp<ABuffer> accessUnit;
Andreas Huberf9334412010-12-15 15:17:42 -0800780
Andreas Huber3fe62152011-09-16 15:09:22 -0700781 bool dropAccessUnit;
782 do {
783 status_t err = mSource->dequeueAccessUnit(audio, &accessUnit);
Andreas Huber5bc087c2010-12-23 10:27:40 -0800784
Andreas Huber3fe62152011-09-16 15:09:22 -0700785 if (err == -EWOULDBLOCK) {
786 return err;
787 } else if (err != OK) {
788 if (err == INFO_DISCONTINUITY) {
789 int32_t type;
790 CHECK(accessUnit->meta()->findInt32("discontinuity", &type));
Andreas Huber53df1a42010-12-22 10:03:04 -0800791
Andreas Huber3fe62152011-09-16 15:09:22 -0700792 bool formatChange =
Andreas Huber6e3d3112011-11-28 12:36:11 -0800793 (audio &&
794 (type & ATSParser::DISCONTINUITY_AUDIO_FORMAT))
795 || (!audio &&
796 (type & ATSParser::DISCONTINUITY_VIDEO_FORMAT));
Andreas Huber53df1a42010-12-22 10:03:04 -0800797
Andreas Huber6e3d3112011-11-28 12:36:11 -0800798 bool timeChange = (type & ATSParser::DISCONTINUITY_TIME) != 0;
799
Steve Blockdf64d152012-01-04 20:05:49 +0000800 ALOGI("%s discontinuity (formatChange=%d, time=%d)",
Andreas Huber6e3d3112011-11-28 12:36:11 -0800801 audio ? "audio" : "video", formatChange, timeChange);
Andreas Huber32f3cef2011-03-02 15:34:46 -0800802
Andreas Huber3fe62152011-09-16 15:09:22 -0700803 if (audio) {
804 mSkipRenderingAudioUntilMediaTimeUs = -1;
805 } else {
806 mSkipRenderingVideoUntilMediaTimeUs = -1;
807 }
Andreas Huber32f3cef2011-03-02 15:34:46 -0800808
Andreas Huber6e3d3112011-11-28 12:36:11 -0800809 if (timeChange) {
810 sp<AMessage> extra;
811 if (accessUnit->meta()->findMessage("extra", &extra)
812 && extra != NULL) {
813 int64_t resumeAtMediaTimeUs;
814 if (extra->findInt64(
815 "resume-at-mediatimeUs", &resumeAtMediaTimeUs)) {
Steve Blockdf64d152012-01-04 20:05:49 +0000816 ALOGI("suppressing rendering of %s until %lld us",
Andreas Huber6e3d3112011-11-28 12:36:11 -0800817 audio ? "audio" : "video", resumeAtMediaTimeUs);
Andreas Huber3fe62152011-09-16 15:09:22 -0700818
Andreas Huber6e3d3112011-11-28 12:36:11 -0800819 if (audio) {
820 mSkipRenderingAudioUntilMediaTimeUs =
821 resumeAtMediaTimeUs;
822 } else {
823 mSkipRenderingVideoUntilMediaTimeUs =
824 resumeAtMediaTimeUs;
825 }
Andreas Huber3fe62152011-09-16 15:09:22 -0700826 }
Andreas Huber32f3cef2011-03-02 15:34:46 -0800827 }
828 }
Andreas Huber3fe62152011-09-16 15:09:22 -0700829
Andreas Huber6e3d3112011-11-28 12:36:11 -0800830 mTimeDiscontinuityPending =
831 mTimeDiscontinuityPending || timeChange;
832
833 if (formatChange || timeChange) {
834 flushDecoder(audio, formatChange);
835 } else {
836 // This stream is unaffected by the discontinuity
837
838 if (audio) {
839 mFlushingAudio = FLUSHED;
840 } else {
841 mFlushingVideo = FLUSHED;
842 }
843
844 finishFlushIfPossible();
845
846 return -EWOULDBLOCK;
847 }
Andreas Huber32f3cef2011-03-02 15:34:46 -0800848 }
849
Andreas Huber3fe62152011-09-16 15:09:22 -0700850 reply->setInt32("err", err);
851 reply->post();
852 return OK;
Andreas Huberf9334412010-12-15 15:17:42 -0800853 }
854
Andreas Huber3fe62152011-09-16 15:09:22 -0700855 if (!audio) {
856 ++mNumFramesTotal;
857 }
858
859 dropAccessUnit = false;
860 if (!audio
861 && mVideoLateByUs > 100000ll
862 && mVideoIsAVC
863 && !IsAVCReferenceFrame(accessUnit)) {
864 dropAccessUnit = true;
865 ++mNumFramesDropped;
866 }
867 } while (dropAccessUnit);
Andreas Huberf9334412010-12-15 15:17:42 -0800868
Steve Block3856b092011-10-20 11:56:00 +0100869 // ALOGV("returned a valid buffer of %s data", audio ? "audio" : "video");
Andreas Huberf9334412010-12-15 15:17:42 -0800870
871#if 0
872 int64_t mediaTimeUs;
873 CHECK(accessUnit->meta()->findInt64("timeUs", &mediaTimeUs));
Steve Block3856b092011-10-20 11:56:00 +0100874 ALOGV("feeding %s input buffer at media time %.2f secs",
Andreas Huberf9334412010-12-15 15:17:42 -0800875 audio ? "audio" : "video",
876 mediaTimeUs / 1E6);
877#endif
878
Andreas Huber2d8bedd2012-02-21 14:38:23 -0800879 reply->setBuffer("buffer", accessUnit);
Andreas Huberf9334412010-12-15 15:17:42 -0800880 reply->post();
881
882 return OK;
883}
884
885void NuPlayer::renderBuffer(bool audio, const sp<AMessage> &msg) {
Steve Block3856b092011-10-20 11:56:00 +0100886 // ALOGV("renderBuffer %s", audio ? "audio" : "video");
Andreas Huberf9334412010-12-15 15:17:42 -0800887
888 sp<AMessage> reply;
889 CHECK(msg->findMessage("reply", &reply));
890
Andreas Huber18ac5402011-08-31 15:04:25 -0700891 if (IsFlushingState(audio ? mFlushingAudio : mFlushingVideo)) {
892 // We're currently attempting to flush the decoder, in order
893 // to complete this, the decoder wants all its buffers back,
894 // so we don't want any output buffers it sent us (from before
895 // we initiated the flush) to be stuck in the renderer's queue.
896
Steve Block3856b092011-10-20 11:56:00 +0100897 ALOGV("we're still flushing the %s decoder, sending its output buffer"
Andreas Huber18ac5402011-08-31 15:04:25 -0700898 " right back.", audio ? "audio" : "video");
899
900 reply->post();
901 return;
902 }
903
Andreas Huber2d8bedd2012-02-21 14:38:23 -0800904 sp<ABuffer> buffer;
905 CHECK(msg->findBuffer("buffer", &buffer));
Andreas Huberf9334412010-12-15 15:17:42 -0800906
Andreas Huber32f3cef2011-03-02 15:34:46 -0800907 int64_t &skipUntilMediaTimeUs =
908 audio
909 ? mSkipRenderingAudioUntilMediaTimeUs
910 : mSkipRenderingVideoUntilMediaTimeUs;
911
912 if (skipUntilMediaTimeUs >= 0) {
913 int64_t mediaTimeUs;
914 CHECK(buffer->meta()->findInt64("timeUs", &mediaTimeUs));
915
916 if (mediaTimeUs < skipUntilMediaTimeUs) {
Steve Block3856b092011-10-20 11:56:00 +0100917 ALOGV("dropping %s buffer at time %lld as requested.",
Andreas Huber32f3cef2011-03-02 15:34:46 -0800918 audio ? "audio" : "video",
919 mediaTimeUs);
920
921 reply->post();
922 return;
923 }
924
925 skipUntilMediaTimeUs = -1;
926 }
927
Andreas Huberf9334412010-12-15 15:17:42 -0800928 mRenderer->queueBuffer(audio, buffer, reply);
929}
930
931void NuPlayer::notifyListener(int msg, int ext1, int ext2) {
Andreas Huber43c3e6c2011-01-05 12:17:08 -0800932 if (mDriver == NULL) {
Andreas Huberf9334412010-12-15 15:17:42 -0800933 return;
934 }
935
Andreas Huber43c3e6c2011-01-05 12:17:08 -0800936 sp<NuPlayerDriver> driver = mDriver.promote();
Andreas Huberf9334412010-12-15 15:17:42 -0800937
Andreas Huber43c3e6c2011-01-05 12:17:08 -0800938 if (driver == NULL) {
Andreas Huberf9334412010-12-15 15:17:42 -0800939 return;
940 }
941
Andreas Hubera4af2142011-10-26 15:23:31 -0700942 driver->notifyListener(msg, ext1, ext2);
Andreas Huberf9334412010-12-15 15:17:42 -0800943}
944
Andreas Huber1aef2112011-01-04 14:01:29 -0800945void NuPlayer::flushDecoder(bool audio, bool needShutdown) {
Andreas Huber6e3d3112011-11-28 12:36:11 -0800946 if ((audio && mAudioDecoder == NULL) || (!audio && mVideoDecoder == NULL)) {
Steve Blockdf64d152012-01-04 20:05:49 +0000947 ALOGI("flushDecoder %s without decoder present",
Andreas Huber6e3d3112011-11-28 12:36:11 -0800948 audio ? "audio" : "video");
949 }
950
Andreas Huber1aef2112011-01-04 14:01:29 -0800951 // Make sure we don't continue to scan sources until we finish flushing.
952 ++mScanSourcesGeneration;
Andreas Huber43c3e6c2011-01-05 12:17:08 -0800953 mScanSourcesPending = false;
Andreas Huber1aef2112011-01-04 14:01:29 -0800954
955 (audio ? mAudioDecoder : mVideoDecoder)->signalFlush();
956 mRenderer->flush(audio);
957
958 FlushStatus newStatus =
959 needShutdown ? FLUSHING_DECODER_SHUTDOWN : FLUSHING_DECODER;
960
961 if (audio) {
962 CHECK(mFlushingAudio == NONE
963 || mFlushingAudio == AWAITING_DISCONTINUITY);
964
965 mFlushingAudio = newStatus;
966
967 if (mFlushingVideo == NONE) {
968 mFlushingVideo = (mVideoDecoder != NULL)
969 ? AWAITING_DISCONTINUITY
970 : FLUSHED;
971 }
972 } else {
973 CHECK(mFlushingVideo == NONE
974 || mFlushingVideo == AWAITING_DISCONTINUITY);
975
976 mFlushingVideo = newStatus;
977
978 if (mFlushingAudio == NONE) {
979 mFlushingAudio = (mAudioDecoder != NULL)
980 ? AWAITING_DISCONTINUITY
981 : FLUSHED;
982 }
983 }
984}
985
Andreas Huber84066782011-08-16 09:34:26 -0700986sp<AMessage> NuPlayer::Source::getFormat(bool audio) {
987 sp<MetaData> meta = getFormatMeta(audio);
988
989 if (meta == NULL) {
990 return NULL;
991 }
992
993 sp<AMessage> msg = new AMessage;
994
995 if(convertMetaDataToMessage(meta, &msg) == OK) {
996 return msg;
997 }
998 return NULL;
999}
1000
James Dong0d268a32012-08-31 12:18:27 -07001001status_t NuPlayer::setVideoScalingMode(int32_t mode) {
1002 mVideoScalingMode = mode;
James Dong376074e2012-09-14 15:34:35 -07001003 if (mNativeWindow != NULL
1004 && mNativeWindow->getNativeWindow() != NULL) {
James Dong0d268a32012-08-31 12:18:27 -07001005 status_t ret = native_window_set_scaling_mode(
1006 mNativeWindow->getNativeWindow().get(), mVideoScalingMode);
1007 if (ret != OK) {
1008 ALOGE("Failed to set scaling mode (%d): %s",
1009 -ret, strerror(-ret));
1010 return ret;
1011 }
1012 }
1013 return OK;
1014}
1015
Andreas Huberb7c8e912012-11-27 15:02:53 -08001016void NuPlayer::schedulePollDuration() {
1017 sp<AMessage> msg = new AMessage(kWhatPollDuration, id());
1018 msg->setInt32("generation", mPollDurationGeneration);
1019 msg->post();
1020}
1021
1022void NuPlayer::cancelPollDuration() {
1023 ++mPollDurationGeneration;
1024}
1025
Andreas Huberf9334412010-12-15 15:17:42 -08001026} // namespace android