blob: ff2787328b037bd848533fd8c7e799f37cecafb5 [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 CHECK_EQ(what, (int32_t)Renderer::kWhatFlushComplete);
554
555 int32_t audio;
556 CHECK(msg->findInt32("audio", &audio));
557
Steve Block3856b092011-10-20 11:56:00 +0100558 ALOGV("renderer %s flush completed.", audio ? "audio" : "video");
James Dongf57b4ea2012-07-20 13:38:36 -0700559 } else if (what == Renderer::kWhatVideoRenderingStart) {
560 notifyListener(MEDIA_INFO, MEDIA_INFO_RENDERING_START, 0);
Andreas Huberf9334412010-12-15 15:17:42 -0800561 }
562 break;
563 }
564
565 case kWhatMoreDataQueued:
566 {
567 break;
568 }
569
Andreas Huber1aef2112011-01-04 14:01:29 -0800570 case kWhatReset:
571 {
Steve Block3856b092011-10-20 11:56:00 +0100572 ALOGV("kWhatReset");
Andreas Huber1aef2112011-01-04 14:01:29 -0800573
Andreas Huberb7c8e912012-11-27 15:02:53 -0800574 cancelPollDuration();
575
Andreas Huberb58ce9f2011-11-28 16:27:35 -0800576 if (mRenderer != NULL) {
577 // There's an edge case where the renderer owns all output
578 // buffers and is paused, therefore the decoder will not read
579 // more input data and will never encounter the matching
580 // discontinuity. To avoid this, we resume the renderer.
581
582 if (mFlushingAudio == AWAITING_DISCONTINUITY
583 || mFlushingVideo == AWAITING_DISCONTINUITY) {
584 mRenderer->resume();
585 }
586 }
587
Andreas Huber1aef2112011-01-04 14:01:29 -0800588 if (mFlushingAudio != NONE || mFlushingVideo != NONE) {
589 // We're currently flushing, postpone the reset until that's
590 // completed.
591
Andreas Huberea9d51b2011-11-30 09:53:40 -0800592 ALOGV("postponing reset mFlushingAudio=%d, mFlushingVideo=%d",
593 mFlushingAudio, mFlushingVideo);
Andreas Huber1aef2112011-01-04 14:01:29 -0800594
595 mResetPostponed = true;
596 break;
597 }
598
599 if (mAudioDecoder == NULL && mVideoDecoder == NULL) {
600 finishReset();
601 break;
602 }
603
Andreas Huber6e3d3112011-11-28 12:36:11 -0800604 mTimeDiscontinuityPending = true;
605
Andreas Huber1aef2112011-01-04 14:01:29 -0800606 if (mAudioDecoder != NULL) {
607 flushDecoder(true /* audio */, true /* needShutdown */);
608 }
609
610 if (mVideoDecoder != NULL) {
611 flushDecoder(false /* audio */, true /* needShutdown */);
612 }
613
614 mResetInProgress = true;
615 break;
616 }
617
Andreas Huber43c3e6c2011-01-05 12:17:08 -0800618 case kWhatSeek:
619 {
620 int64_t seekTimeUs;
621 CHECK(msg->findInt64("seekTimeUs", &seekTimeUs));
622
Steve Block3856b092011-10-20 11:56:00 +0100623 ALOGV("kWhatSeek seekTimeUs=%lld us (%.2f secs)",
Andreas Huber43c3e6c2011-01-05 12:17:08 -0800624 seekTimeUs, seekTimeUs / 1E6);
625
626 mSource->seekTo(seekTimeUs);
627
628 if (mDriver != NULL) {
629 sp<NuPlayerDriver> driver = mDriver.promote();
630 if (driver != NULL) {
631 driver->notifySeekComplete();
632 }
633 }
634
635 break;
636 }
637
Andreas Huberb4082222011-01-20 15:23:04 -0800638 case kWhatPause:
639 {
640 CHECK(mRenderer != NULL);
641 mRenderer->pause();
642 break;
643 }
644
645 case kWhatResume:
646 {
647 CHECK(mRenderer != NULL);
648 mRenderer->resume();
649 break;
650 }
651
Andreas Huberf9334412010-12-15 15:17:42 -0800652 default:
653 TRESPASS();
654 break;
655 }
656}
657
Andreas Huber3831a062010-12-21 10:22:33 -0800658void NuPlayer::finishFlushIfPossible() {
659 if (mFlushingAudio != FLUSHED && mFlushingAudio != SHUT_DOWN) {
660 return;
661 }
662
663 if (mFlushingVideo != FLUSHED && mFlushingVideo != SHUT_DOWN) {
664 return;
665 }
666
Steve Block3856b092011-10-20 11:56:00 +0100667 ALOGV("both audio and video are flushed now.");
Andreas Huber3831a062010-12-21 10:22:33 -0800668
Andreas Huber6e3d3112011-11-28 12:36:11 -0800669 if (mTimeDiscontinuityPending) {
670 mRenderer->signalTimeDiscontinuity();
671 mTimeDiscontinuityPending = false;
672 }
Andreas Huber3831a062010-12-21 10:22:33 -0800673
Andreas Huber22fc52f2011-01-05 16:24:27 -0800674 if (mAudioDecoder != NULL) {
Andreas Huber3831a062010-12-21 10:22:33 -0800675 mAudioDecoder->signalResume();
676 }
677
Andreas Huber22fc52f2011-01-05 16:24:27 -0800678 if (mVideoDecoder != NULL) {
Andreas Huber3831a062010-12-21 10:22:33 -0800679 mVideoDecoder->signalResume();
680 }
681
682 mFlushingAudio = NONE;
683 mFlushingVideo = NONE;
Andreas Huber3831a062010-12-21 10:22:33 -0800684
Andreas Huber1aef2112011-01-04 14:01:29 -0800685 if (mResetInProgress) {
Steve Block3856b092011-10-20 11:56:00 +0100686 ALOGV("reset completed");
Andreas Huber1aef2112011-01-04 14:01:29 -0800687
688 mResetInProgress = false;
689 finishReset();
690 } else if (mResetPostponed) {
691 (new AMessage(kWhatReset, id()))->post();
692 mResetPostponed = false;
Andreas Huber22fc52f2011-01-05 16:24:27 -0800693 } else if (mAudioDecoder == NULL || mVideoDecoder == NULL) {
Andreas Huber1aef2112011-01-04 14:01:29 -0800694 postScanSources();
Andreas Huberf9334412010-12-15 15:17:42 -0800695 }
696}
697
Andreas Huber1aef2112011-01-04 14:01:29 -0800698void NuPlayer::finishReset() {
699 CHECK(mAudioDecoder == NULL);
700 CHECK(mVideoDecoder == NULL);
701
Andreas Huber2bfdd422011-10-11 15:24:07 -0700702 ++mScanSourcesGeneration;
703 mScanSourcesPending = false;
704
Andreas Huber1aef2112011-01-04 14:01:29 -0800705 mRenderer.clear();
Andreas Huber2bfdd422011-10-11 15:24:07 -0700706
707 if (mSource != NULL) {
708 mSource->stop();
709 mSource.clear();
710 }
Andreas Huber1aef2112011-01-04 14:01:29 -0800711
Andreas Huber43c3e6c2011-01-05 12:17:08 -0800712 if (mDriver != NULL) {
713 sp<NuPlayerDriver> driver = mDriver.promote();
714 if (driver != NULL) {
715 driver->notifyResetComplete();
716 }
717 }
Andreas Huber1aef2112011-01-04 14:01:29 -0800718}
719
720void NuPlayer::postScanSources() {
721 if (mScanSourcesPending) {
722 return;
723 }
724
725 sp<AMessage> msg = new AMessage(kWhatScanSources, id());
726 msg->setInt32("generation", mScanSourcesGeneration);
727 msg->post();
728
729 mScanSourcesPending = true;
730}
731
Andreas Huber5bc087c2010-12-23 10:27:40 -0800732status_t NuPlayer::instantiateDecoder(bool audio, sp<Decoder> *decoder) {
Andreas Huberf9334412010-12-15 15:17:42 -0800733 if (*decoder != NULL) {
734 return OK;
735 }
736
Andreas Huber84066782011-08-16 09:34:26 -0700737 sp<AMessage> format = mSource->getFormat(audio);
Andreas Huberf9334412010-12-15 15:17:42 -0800738
Andreas Huber84066782011-08-16 09:34:26 -0700739 if (format == NULL) {
Andreas Huberf9334412010-12-15 15:17:42 -0800740 return -EWOULDBLOCK;
741 }
742
Andreas Huber3fe62152011-09-16 15:09:22 -0700743 if (!audio) {
Andreas Huber84066782011-08-16 09:34:26 -0700744 AString mime;
745 CHECK(format->findString("mime", &mime));
746 mVideoIsAVC = !strcasecmp(MEDIA_MIMETYPE_VIDEO_AVC, mime.c_str());
Andreas Huber3fe62152011-09-16 15:09:22 -0700747 }
748
Andreas Huberf9334412010-12-15 15:17:42 -0800749 sp<AMessage> notify =
750 new AMessage(audio ? kWhatAudioNotify : kWhatVideoNotify,
751 id());
752
Glenn Kasten11731182011-02-08 17:26:17 -0800753 *decoder = audio ? new Decoder(notify) :
754 new Decoder(notify, mNativeWindow);
Andreas Huberf9334412010-12-15 15:17:42 -0800755 looper()->registerHandler(*decoder);
756
Andreas Huber84066782011-08-16 09:34:26 -0700757 (*decoder)->configure(format);
Andreas Huberf9334412010-12-15 15:17:42 -0800758
Andreas Huber43c3e6c2011-01-05 12:17:08 -0800759 int64_t durationUs;
760 if (mDriver != NULL && mSource->getDuration(&durationUs) == OK) {
761 sp<NuPlayerDriver> driver = mDriver.promote();
762 if (driver != NULL) {
763 driver->notifyDuration(durationUs);
764 }
765 }
766
Andreas Huberf9334412010-12-15 15:17:42 -0800767 return OK;
768}
769
770status_t NuPlayer::feedDecoderInputData(bool audio, const sp<AMessage> &msg) {
771 sp<AMessage> reply;
772 CHECK(msg->findMessage("reply", &reply));
773
Andreas Huber53df1a42010-12-22 10:03:04 -0800774 if ((audio && IsFlushingState(mFlushingAudio))
775 || (!audio && IsFlushingState(mFlushingVideo))) {
Andreas Huberf9334412010-12-15 15:17:42 -0800776 reply->setInt32("err", INFO_DISCONTINUITY);
777 reply->post();
778 return OK;
779 }
780
781 sp<ABuffer> accessUnit;
Andreas Huberf9334412010-12-15 15:17:42 -0800782
Andreas Huber3fe62152011-09-16 15:09:22 -0700783 bool dropAccessUnit;
784 do {
785 status_t err = mSource->dequeueAccessUnit(audio, &accessUnit);
Andreas Huber5bc087c2010-12-23 10:27:40 -0800786
Andreas Huber3fe62152011-09-16 15:09:22 -0700787 if (err == -EWOULDBLOCK) {
788 return err;
789 } else if (err != OK) {
790 if (err == INFO_DISCONTINUITY) {
791 int32_t type;
792 CHECK(accessUnit->meta()->findInt32("discontinuity", &type));
Andreas Huber53df1a42010-12-22 10:03:04 -0800793
Andreas Huber3fe62152011-09-16 15:09:22 -0700794 bool formatChange =
Andreas Huber6e3d3112011-11-28 12:36:11 -0800795 (audio &&
796 (type & ATSParser::DISCONTINUITY_AUDIO_FORMAT))
797 || (!audio &&
798 (type & ATSParser::DISCONTINUITY_VIDEO_FORMAT));
Andreas Huber53df1a42010-12-22 10:03:04 -0800799
Andreas Huber6e3d3112011-11-28 12:36:11 -0800800 bool timeChange = (type & ATSParser::DISCONTINUITY_TIME) != 0;
801
Steve Blockdf64d152012-01-04 20:05:49 +0000802 ALOGI("%s discontinuity (formatChange=%d, time=%d)",
Andreas Huber6e3d3112011-11-28 12:36:11 -0800803 audio ? "audio" : "video", formatChange, timeChange);
Andreas Huber32f3cef2011-03-02 15:34:46 -0800804
Andreas Huber3fe62152011-09-16 15:09:22 -0700805 if (audio) {
806 mSkipRenderingAudioUntilMediaTimeUs = -1;
807 } else {
808 mSkipRenderingVideoUntilMediaTimeUs = -1;
809 }
Andreas Huber32f3cef2011-03-02 15:34:46 -0800810
Andreas Huber6e3d3112011-11-28 12:36:11 -0800811 if (timeChange) {
812 sp<AMessage> extra;
813 if (accessUnit->meta()->findMessage("extra", &extra)
814 && extra != NULL) {
815 int64_t resumeAtMediaTimeUs;
816 if (extra->findInt64(
817 "resume-at-mediatimeUs", &resumeAtMediaTimeUs)) {
Steve Blockdf64d152012-01-04 20:05:49 +0000818 ALOGI("suppressing rendering of %s until %lld us",
Andreas Huber6e3d3112011-11-28 12:36:11 -0800819 audio ? "audio" : "video", resumeAtMediaTimeUs);
Andreas Huber3fe62152011-09-16 15:09:22 -0700820
Andreas Huber6e3d3112011-11-28 12:36:11 -0800821 if (audio) {
822 mSkipRenderingAudioUntilMediaTimeUs =
823 resumeAtMediaTimeUs;
824 } else {
825 mSkipRenderingVideoUntilMediaTimeUs =
826 resumeAtMediaTimeUs;
827 }
Andreas Huber3fe62152011-09-16 15:09:22 -0700828 }
Andreas Huber32f3cef2011-03-02 15:34:46 -0800829 }
830 }
Andreas Huber3fe62152011-09-16 15:09:22 -0700831
Andreas Huber6e3d3112011-11-28 12:36:11 -0800832 mTimeDiscontinuityPending =
833 mTimeDiscontinuityPending || timeChange;
834
835 if (formatChange || timeChange) {
836 flushDecoder(audio, formatChange);
837 } else {
838 // This stream is unaffected by the discontinuity
839
840 if (audio) {
841 mFlushingAudio = FLUSHED;
842 } else {
843 mFlushingVideo = FLUSHED;
844 }
845
846 finishFlushIfPossible();
847
848 return -EWOULDBLOCK;
849 }
Andreas Huber32f3cef2011-03-02 15:34:46 -0800850 }
851
Andreas Huber3fe62152011-09-16 15:09:22 -0700852 reply->setInt32("err", err);
853 reply->post();
854 return OK;
Andreas Huberf9334412010-12-15 15:17:42 -0800855 }
856
Andreas Huber3fe62152011-09-16 15:09:22 -0700857 if (!audio) {
858 ++mNumFramesTotal;
859 }
860
861 dropAccessUnit = false;
862 if (!audio
863 && mVideoLateByUs > 100000ll
864 && mVideoIsAVC
865 && !IsAVCReferenceFrame(accessUnit)) {
866 dropAccessUnit = true;
867 ++mNumFramesDropped;
868 }
869 } while (dropAccessUnit);
Andreas Huberf9334412010-12-15 15:17:42 -0800870
Steve Block3856b092011-10-20 11:56:00 +0100871 // ALOGV("returned a valid buffer of %s data", audio ? "audio" : "video");
Andreas Huberf9334412010-12-15 15:17:42 -0800872
873#if 0
874 int64_t mediaTimeUs;
875 CHECK(accessUnit->meta()->findInt64("timeUs", &mediaTimeUs));
Steve Block3856b092011-10-20 11:56:00 +0100876 ALOGV("feeding %s input buffer at media time %.2f secs",
Andreas Huberf9334412010-12-15 15:17:42 -0800877 audio ? "audio" : "video",
878 mediaTimeUs / 1E6);
879#endif
880
Andreas Huber2d8bedd2012-02-21 14:38:23 -0800881 reply->setBuffer("buffer", accessUnit);
Andreas Huberf9334412010-12-15 15:17:42 -0800882 reply->post();
883
884 return OK;
885}
886
887void NuPlayer::renderBuffer(bool audio, const sp<AMessage> &msg) {
Steve Block3856b092011-10-20 11:56:00 +0100888 // ALOGV("renderBuffer %s", audio ? "audio" : "video");
Andreas Huberf9334412010-12-15 15:17:42 -0800889
890 sp<AMessage> reply;
891 CHECK(msg->findMessage("reply", &reply));
892
Andreas Huber18ac5402011-08-31 15:04:25 -0700893 if (IsFlushingState(audio ? mFlushingAudio : mFlushingVideo)) {
894 // We're currently attempting to flush the decoder, in order
895 // to complete this, the decoder wants all its buffers back,
896 // so we don't want any output buffers it sent us (from before
897 // we initiated the flush) to be stuck in the renderer's queue.
898
Steve Block3856b092011-10-20 11:56:00 +0100899 ALOGV("we're still flushing the %s decoder, sending its output buffer"
Andreas Huber18ac5402011-08-31 15:04:25 -0700900 " right back.", audio ? "audio" : "video");
901
902 reply->post();
903 return;
904 }
905
Andreas Huber2d8bedd2012-02-21 14:38:23 -0800906 sp<ABuffer> buffer;
907 CHECK(msg->findBuffer("buffer", &buffer));
Andreas Huberf9334412010-12-15 15:17:42 -0800908
Andreas Huber32f3cef2011-03-02 15:34:46 -0800909 int64_t &skipUntilMediaTimeUs =
910 audio
911 ? mSkipRenderingAudioUntilMediaTimeUs
912 : mSkipRenderingVideoUntilMediaTimeUs;
913
914 if (skipUntilMediaTimeUs >= 0) {
915 int64_t mediaTimeUs;
916 CHECK(buffer->meta()->findInt64("timeUs", &mediaTimeUs));
917
918 if (mediaTimeUs < skipUntilMediaTimeUs) {
Steve Block3856b092011-10-20 11:56:00 +0100919 ALOGV("dropping %s buffer at time %lld as requested.",
Andreas Huber32f3cef2011-03-02 15:34:46 -0800920 audio ? "audio" : "video",
921 mediaTimeUs);
922
923 reply->post();
924 return;
925 }
926
927 skipUntilMediaTimeUs = -1;
928 }
929
Andreas Huberf9334412010-12-15 15:17:42 -0800930 mRenderer->queueBuffer(audio, buffer, reply);
931}
932
933void NuPlayer::notifyListener(int msg, int ext1, int ext2) {
Andreas Huber43c3e6c2011-01-05 12:17:08 -0800934 if (mDriver == NULL) {
Andreas Huberf9334412010-12-15 15:17:42 -0800935 return;
936 }
937
Andreas Huber43c3e6c2011-01-05 12:17:08 -0800938 sp<NuPlayerDriver> driver = mDriver.promote();
Andreas Huberf9334412010-12-15 15:17:42 -0800939
Andreas Huber43c3e6c2011-01-05 12:17:08 -0800940 if (driver == NULL) {
Andreas Huberf9334412010-12-15 15:17:42 -0800941 return;
942 }
943
Andreas Hubera4af2142011-10-26 15:23:31 -0700944 driver->notifyListener(msg, ext1, ext2);
Andreas Huberf9334412010-12-15 15:17:42 -0800945}
946
Andreas Huber1aef2112011-01-04 14:01:29 -0800947void NuPlayer::flushDecoder(bool audio, bool needShutdown) {
Andreas Huber6e3d3112011-11-28 12:36:11 -0800948 if ((audio && mAudioDecoder == NULL) || (!audio && mVideoDecoder == NULL)) {
Steve Blockdf64d152012-01-04 20:05:49 +0000949 ALOGI("flushDecoder %s without decoder present",
Andreas Huber6e3d3112011-11-28 12:36:11 -0800950 audio ? "audio" : "video");
951 }
952
Andreas Huber1aef2112011-01-04 14:01:29 -0800953 // Make sure we don't continue to scan sources until we finish flushing.
954 ++mScanSourcesGeneration;
Andreas Huber43c3e6c2011-01-05 12:17:08 -0800955 mScanSourcesPending = false;
Andreas Huber1aef2112011-01-04 14:01:29 -0800956
957 (audio ? mAudioDecoder : mVideoDecoder)->signalFlush();
958 mRenderer->flush(audio);
959
960 FlushStatus newStatus =
961 needShutdown ? FLUSHING_DECODER_SHUTDOWN : FLUSHING_DECODER;
962
963 if (audio) {
964 CHECK(mFlushingAudio == NONE
965 || mFlushingAudio == AWAITING_DISCONTINUITY);
966
967 mFlushingAudio = newStatus;
968
969 if (mFlushingVideo == NONE) {
970 mFlushingVideo = (mVideoDecoder != NULL)
971 ? AWAITING_DISCONTINUITY
972 : FLUSHED;
973 }
974 } else {
975 CHECK(mFlushingVideo == NONE
976 || mFlushingVideo == AWAITING_DISCONTINUITY);
977
978 mFlushingVideo = newStatus;
979
980 if (mFlushingAudio == NONE) {
981 mFlushingAudio = (mAudioDecoder != NULL)
982 ? AWAITING_DISCONTINUITY
983 : FLUSHED;
984 }
985 }
986}
987
Andreas Huber84066782011-08-16 09:34:26 -0700988sp<AMessage> NuPlayer::Source::getFormat(bool audio) {
989 sp<MetaData> meta = getFormatMeta(audio);
990
991 if (meta == NULL) {
992 return NULL;
993 }
994
995 sp<AMessage> msg = new AMessage;
996
997 if(convertMetaDataToMessage(meta, &msg) == OK) {
998 return msg;
999 }
1000 return NULL;
1001}
1002
James Dong0d268a32012-08-31 12:18:27 -07001003status_t NuPlayer::setVideoScalingMode(int32_t mode) {
1004 mVideoScalingMode = mode;
James Dong376074e2012-09-14 15:34:35 -07001005 if (mNativeWindow != NULL
1006 && mNativeWindow->getNativeWindow() != NULL) {
James Dong0d268a32012-08-31 12:18:27 -07001007 status_t ret = native_window_set_scaling_mode(
1008 mNativeWindow->getNativeWindow().get(), mVideoScalingMode);
1009 if (ret != OK) {
1010 ALOGE("Failed to set scaling mode (%d): %s",
1011 -ret, strerror(-ret));
1012 return ret;
1013 }
1014 }
1015 return OK;
1016}
1017
Andreas Huberb7c8e912012-11-27 15:02:53 -08001018void NuPlayer::schedulePollDuration() {
1019 sp<AMessage> msg = new AMessage(kWhatPollDuration, id());
1020 msg->setInt32("generation", mPollDurationGeneration);
1021 msg->post();
1022}
1023
1024void NuPlayer::cancelPollDuration() {
1025 ++mPollDurationGeneration;
1026}
1027
Andreas Huberf9334412010-12-15 15:17:42 -08001028} // namespace android