blob: 746055cc34476d940bcca06d1eb49b9cb56e9585 [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
Andreas Hubera1f8ab02012-11-30 10:53:22 -080053struct NuPlayer::Action : public RefBase {
54 Action() {}
55
56 virtual void execute(NuPlayer *player) = 0;
57
58private:
59 DISALLOW_EVIL_CONSTRUCTORS(Action);
60};
61
62struct NuPlayer::SeekAction : public Action {
63 SeekAction(int64_t seekTimeUs)
64 : mSeekTimeUs(seekTimeUs) {
65 }
66
67 virtual void execute(NuPlayer *player) {
68 player->performSeek(mSeekTimeUs);
69 }
70
71private:
72 int64_t mSeekTimeUs;
73
74 DISALLOW_EVIL_CONSTRUCTORS(SeekAction);
75};
76
77// Use this if there's no state necessary to save in order to execute
78// the action.
79struct NuPlayer::SimpleAction : public Action {
80 typedef void (NuPlayer::*ActionFunc)();
81
82 SimpleAction(ActionFunc func)
83 : mFunc(func) {
84 }
85
86 virtual void execute(NuPlayer *player) {
87 (player->*mFunc)();
88 }
89
90private:
91 ActionFunc mFunc;
92
93 DISALLOW_EVIL_CONSTRUCTORS(SimpleAction);
94};
95
Andreas Huberf9334412010-12-15 15:17:42 -080096////////////////////////////////////////////////////////////////////////////////
97
98NuPlayer::NuPlayer()
Andreas Huber9b80c2b2011-06-30 15:47:02 -070099 : mUIDValid(false),
Andreas Huber3fe62152011-09-16 15:09:22 -0700100 mVideoIsAVC(false),
Andreas Huber9b80c2b2011-06-30 15:47:02 -0700101 mAudioEOS(false),
Andreas Huberf9334412010-12-15 15:17:42 -0800102 mVideoEOS(false),
Andreas Huber5bc087c2010-12-23 10:27:40 -0800103 mScanSourcesPending(false),
Andreas Huber1aef2112011-01-04 14:01:29 -0800104 mScanSourcesGeneration(0),
Andreas Huberb7c8e912012-11-27 15:02:53 -0800105 mPollDurationGeneration(0),
Andreas Huber6e3d3112011-11-28 12:36:11 -0800106 mTimeDiscontinuityPending(false),
Andreas Huberf9334412010-12-15 15:17:42 -0800107 mFlushingAudio(NONE),
Andreas Huber1aef2112011-01-04 14:01:29 -0800108 mFlushingVideo(NONE),
Andreas Huber3fe62152011-09-16 15:09:22 -0700109 mSkipRenderingAudioUntilMediaTimeUs(-1ll),
110 mSkipRenderingVideoUntilMediaTimeUs(-1ll),
111 mVideoLateByUs(0ll),
112 mNumFramesTotal(0ll),
James Dong0d268a32012-08-31 12:18:27 -0700113 mNumFramesDropped(0ll),
114 mVideoScalingMode(NATIVE_WINDOW_SCALING_MODE_SCALE_TO_WINDOW) {
Andreas Huberf9334412010-12-15 15:17:42 -0800115}
116
117NuPlayer::~NuPlayer() {
118}
119
Andreas Huber9b80c2b2011-06-30 15:47:02 -0700120void NuPlayer::setUID(uid_t uid) {
121 mUIDValid = true;
122 mUID = uid;
123}
124
Andreas Huber43c3e6c2011-01-05 12:17:08 -0800125void NuPlayer::setDriver(const wp<NuPlayerDriver> &driver) {
126 mDriver = driver;
Andreas Huberf9334412010-12-15 15:17:42 -0800127}
128
129void NuPlayer::setDataSource(const sp<IStreamSource> &source) {
130 sp<AMessage> msg = new AMessage(kWhatSetDataSource, id());
131
Andreas Huber84066782011-08-16 09:34:26 -0700132 char prop[PROPERTY_VALUE_MAX];
133 if (property_get("media.stagefright.use-mp4source", prop, NULL)
134 && (!strcmp(prop, "1") || !strcasecmp(prop, "true"))) {
135 msg->setObject("source", new MP4Source(source));
136 } else {
137 msg->setObject("source", new StreamingSource(source));
138 }
139
Andreas Huber5bc087c2010-12-23 10:27:40 -0800140 msg->post();
141}
Andreas Huberf9334412010-12-15 15:17:42 -0800142
Andreas Huberafed0e12011-09-20 15:39:58 -0700143static bool IsHTTPLiveURL(const char *url) {
144 if (!strncasecmp("http://", url, 7)
145 || !strncasecmp("https://", url, 8)) {
146 size_t len = strlen(url);
147 if (len >= 5 && !strcasecmp(".m3u8", &url[len - 5])) {
148 return true;
149 }
150
151 if (strstr(url,"m3u8")) {
152 return true;
153 }
154 }
155
156 return false;
157}
158
Andreas Huber5bc087c2010-12-23 10:27:40 -0800159void NuPlayer::setDataSource(
160 const char *url, const KeyedVector<String8, String8> *headers) {
161 sp<AMessage> msg = new AMessage(kWhatSetDataSource, id());
162
Andreas Huberafed0e12011-09-20 15:39:58 -0700163 sp<Source> source;
164 if (IsHTTPLiveURL(url)) {
165 source = new HTTPLiveSource(url, headers, mUIDValid, mUID);
166 } else if (!strncasecmp(url, "rtsp://", 7)) {
167 source = new RTSPSource(url, headers, mUIDValid, mUID);
Andreas Huber2bfdd422011-10-11 15:24:07 -0700168 } else {
Andreas Huberafed0e12011-09-20 15:39:58 -0700169 source = new GenericSource(url, headers, mUIDValid, mUID);
Andreas Huber2bfdd422011-10-11 15:24:07 -0700170 }
171
Andreas Huberafed0e12011-09-20 15:39:58 -0700172 msg->setObject("source", source);
173 msg->post();
174}
175
176void NuPlayer::setDataSource(int fd, int64_t offset, int64_t length) {
177 sp<AMessage> msg = new AMessage(kWhatSetDataSource, id());
178
179 sp<Source> source = new GenericSource(fd, offset, length);
180 msg->setObject("source", source);
Andreas Huberf9334412010-12-15 15:17:42 -0800181 msg->post();
182}
183
Glenn Kasten11731182011-02-08 17:26:17 -0800184void NuPlayer::setVideoSurfaceTexture(const sp<ISurfaceTexture> &surfaceTexture) {
185 sp<AMessage> msg = new AMessage(kWhatSetVideoNativeWindow, id());
186 sp<SurfaceTextureClient> surfaceTextureClient(surfaceTexture != NULL ?
187 new SurfaceTextureClient(surfaceTexture) : NULL);
188 msg->setObject("native-window", new NativeWindowWrapper(surfaceTextureClient));
Andreas Huberf9334412010-12-15 15:17:42 -0800189 msg->post();
190}
191
192void NuPlayer::setAudioSink(const sp<MediaPlayerBase::AudioSink> &sink) {
193 sp<AMessage> msg = new AMessage(kWhatSetAudioSink, id());
194 msg->setObject("sink", sink);
195 msg->post();
196}
197
198void NuPlayer::start() {
199 (new AMessage(kWhatStart, id()))->post();
200}
201
Andreas Huber43c3e6c2011-01-05 12:17:08 -0800202void NuPlayer::pause() {
Andreas Huberb4082222011-01-20 15:23:04 -0800203 (new AMessage(kWhatPause, id()))->post();
Andreas Huber43c3e6c2011-01-05 12:17:08 -0800204}
205
206void NuPlayer::resume() {
Andreas Huberb4082222011-01-20 15:23:04 -0800207 (new AMessage(kWhatResume, id()))->post();
Andreas Huber43c3e6c2011-01-05 12:17:08 -0800208}
209
Andreas Huber1aef2112011-01-04 14:01:29 -0800210void NuPlayer::resetAsync() {
211 (new AMessage(kWhatReset, id()))->post();
212}
213
Andreas Huber43c3e6c2011-01-05 12:17:08 -0800214void NuPlayer::seekToAsync(int64_t seekTimeUs) {
215 sp<AMessage> msg = new AMessage(kWhatSeek, id());
216 msg->setInt64("seekTimeUs", seekTimeUs);
217 msg->post();
218}
219
Andreas Huber53df1a42010-12-22 10:03:04 -0800220// static
Andreas Huber1aef2112011-01-04 14:01:29 -0800221bool NuPlayer::IsFlushingState(FlushStatus state, bool *needShutdown) {
Andreas Huber53df1a42010-12-22 10:03:04 -0800222 switch (state) {
223 case FLUSHING_DECODER:
Andreas Huber1aef2112011-01-04 14:01:29 -0800224 if (needShutdown != NULL) {
225 *needShutdown = false;
Andreas Huber53df1a42010-12-22 10:03:04 -0800226 }
227 return true;
228
Andreas Huber1aef2112011-01-04 14:01:29 -0800229 case FLUSHING_DECODER_SHUTDOWN:
230 if (needShutdown != NULL) {
231 *needShutdown = true;
Andreas Huber53df1a42010-12-22 10:03:04 -0800232 }
233 return true;
234
235 default:
236 return false;
237 }
238}
239
Andreas Huberf9334412010-12-15 15:17:42 -0800240void NuPlayer::onMessageReceived(const sp<AMessage> &msg) {
241 switch (msg->what()) {
242 case kWhatSetDataSource:
243 {
Steve Block3856b092011-10-20 11:56:00 +0100244 ALOGV("kWhatSetDataSource");
Andreas Huberf9334412010-12-15 15:17:42 -0800245
246 CHECK(mSource == NULL);
247
Andreas Huber5bc087c2010-12-23 10:27:40 -0800248 sp<RefBase> obj;
249 CHECK(msg->findObject("source", &obj));
Andreas Huberf9334412010-12-15 15:17:42 -0800250
Andreas Huber5bc087c2010-12-23 10:27:40 -0800251 mSource = static_cast<Source *>(obj.get());
Andreas Huberf9334412010-12-15 15:17:42 -0800252 break;
253 }
254
Andreas Huberb7c8e912012-11-27 15:02:53 -0800255 case kWhatPollDuration:
256 {
257 int32_t generation;
258 CHECK(msg->findInt32("generation", &generation));
259
260 if (generation != mPollDurationGeneration) {
261 // stale
262 break;
263 }
264
265 int64_t durationUs;
266 if (mDriver != NULL && mSource->getDuration(&durationUs) == OK) {
267 sp<NuPlayerDriver> driver = mDriver.promote();
268 if (driver != NULL) {
269 driver->notifyDuration(durationUs);
270 }
271 }
272
273 msg->post(1000000ll); // poll again in a second.
274 break;
275 }
276
Glenn Kasten11731182011-02-08 17:26:17 -0800277 case kWhatSetVideoNativeWindow:
Andreas Huberf9334412010-12-15 15:17:42 -0800278 {
Steve Block3856b092011-10-20 11:56:00 +0100279 ALOGV("kWhatSetVideoNativeWindow");
Andreas Huberf9334412010-12-15 15:17:42 -0800280
281 sp<RefBase> obj;
Glenn Kasten11731182011-02-08 17:26:17 -0800282 CHECK(msg->findObject("native-window", &obj));
Andreas Huberf9334412010-12-15 15:17:42 -0800283
Glenn Kasten11731182011-02-08 17:26:17 -0800284 mNativeWindow = static_cast<NativeWindowWrapper *>(obj.get());
James Dong0d268a32012-08-31 12:18:27 -0700285
286 // XXX - ignore error from setVideoScalingMode for now
287 setVideoScalingMode(mVideoScalingMode);
Andreas Huberf9334412010-12-15 15:17:42 -0800288 break;
289 }
290
291 case kWhatSetAudioSink:
292 {
Steve Block3856b092011-10-20 11:56:00 +0100293 ALOGV("kWhatSetAudioSink");
Andreas Huberf9334412010-12-15 15:17:42 -0800294
295 sp<RefBase> obj;
296 CHECK(msg->findObject("sink", &obj));
297
298 mAudioSink = static_cast<MediaPlayerBase::AudioSink *>(obj.get());
299 break;
300 }
301
302 case kWhatStart:
303 {
Steve Block3856b092011-10-20 11:56:00 +0100304 ALOGV("kWhatStart");
Andreas Huber43c3e6c2011-01-05 12:17:08 -0800305
Andreas Huber3fe62152011-09-16 15:09:22 -0700306 mVideoIsAVC = false;
Andreas Huber1aef2112011-01-04 14:01:29 -0800307 mAudioEOS = false;
308 mVideoEOS = false;
Andreas Huber32f3cef2011-03-02 15:34:46 -0800309 mSkipRenderingAudioUntilMediaTimeUs = -1;
310 mSkipRenderingVideoUntilMediaTimeUs = -1;
Andreas Huber3fe62152011-09-16 15:09:22 -0700311 mVideoLateByUs = 0;
312 mNumFramesTotal = 0;
313 mNumFramesDropped = 0;
Andreas Huber1aef2112011-01-04 14:01:29 -0800314
Andreas Huber5bc087c2010-12-23 10:27:40 -0800315 mSource->start();
Andreas Huberf9334412010-12-15 15:17:42 -0800316
317 mRenderer = new Renderer(
318 mAudioSink,
319 new AMessage(kWhatRendererNotify, id()));
320
321 looper()->registerHandler(mRenderer);
322
Andreas Huber1aef2112011-01-04 14:01:29 -0800323 postScanSources();
Andreas Huberf9334412010-12-15 15:17:42 -0800324 break;
325 }
326
327 case kWhatScanSources:
328 {
Andreas Huber1aef2112011-01-04 14:01:29 -0800329 int32_t generation;
330 CHECK(msg->findInt32("generation", &generation));
331 if (generation != mScanSourcesGeneration) {
332 // Drop obsolete msg.
333 break;
334 }
335
Andreas Huber5bc087c2010-12-23 10:27:40 -0800336 mScanSourcesPending = false;
337
Steve Block3856b092011-10-20 11:56:00 +0100338 ALOGV("scanning sources haveAudio=%d, haveVideo=%d",
Andreas Huber43c3e6c2011-01-05 12:17:08 -0800339 mAudioDecoder != NULL, mVideoDecoder != NULL);
340
Andreas Huberb7c8e912012-11-27 15:02:53 -0800341 bool mHadAnySourcesBefore =
342 (mAudioDecoder != NULL) || (mVideoDecoder != NULL);
343
Haynes Mathew George5d246ef2012-07-09 10:36:57 -0700344 if (mNativeWindow != NULL) {
345 instantiateDecoder(false, &mVideoDecoder);
346 }
Andreas Huberf9334412010-12-15 15:17:42 -0800347
348 if (mAudioSink != NULL) {
Andreas Huber5bc087c2010-12-23 10:27:40 -0800349 instantiateDecoder(true, &mAudioDecoder);
Andreas Huberf9334412010-12-15 15:17:42 -0800350 }
351
Andreas Huberb7c8e912012-11-27 15:02:53 -0800352 if (!mHadAnySourcesBefore
353 && (mAudioDecoder != NULL || mVideoDecoder != NULL)) {
354 // This is the first time we've found anything playable.
355
356 uint32_t flags = mSource->flags();
357
358 if (flags & Source::FLAG_DYNAMIC_DURATION) {
359 schedulePollDuration();
360 }
361 }
362
Andreas Hubereac68ba2011-09-27 12:12:25 -0700363 status_t err;
364 if ((err = mSource->feedMoreTSData()) != OK) {
Andreas Huber1aef2112011-01-04 14:01:29 -0800365 if (mAudioDecoder == NULL && mVideoDecoder == NULL) {
366 // We're not currently decoding anything (no audio or
367 // video tracks found) and we just ran out of input data.
Andreas Hubereac68ba2011-09-27 12:12:25 -0700368
369 if (err == ERROR_END_OF_STREAM) {
370 notifyListener(MEDIA_PLAYBACK_COMPLETE, 0, 0);
371 } else {
372 notifyListener(MEDIA_ERROR, MEDIA_ERROR_UNKNOWN, err);
373 }
Andreas Huber1aef2112011-01-04 14:01:29 -0800374 }
Andreas Huberf9334412010-12-15 15:17:42 -0800375 break;
376 }
377
Andreas Huberfbe9d812012-08-31 14:05:27 -0700378 if ((mAudioDecoder == NULL && mAudioSink != NULL)
379 || (mVideoDecoder == NULL && mNativeWindow != NULL)) {
Andreas Huberf9334412010-12-15 15:17:42 -0800380 msg->post(100000ll);
Andreas Huber5bc087c2010-12-23 10:27:40 -0800381 mScanSourcesPending = true;
Andreas Huberf9334412010-12-15 15:17:42 -0800382 }
383 break;
384 }
385
386 case kWhatVideoNotify:
387 case kWhatAudioNotify:
388 {
389 bool audio = msg->what() == kWhatAudioNotify;
390
391 sp<AMessage> codecRequest;
392 CHECK(msg->findMessage("codec-request", &codecRequest));
393
394 int32_t what;
395 CHECK(codecRequest->findInt32("what", &what));
396
397 if (what == ACodec::kWhatFillThisBuffer) {
398 status_t err = feedDecoderInputData(
399 audio, codecRequest);
400
Andreas Huber5bc087c2010-12-23 10:27:40 -0800401 if (err == -EWOULDBLOCK) {
Andreas Hubereac68ba2011-09-27 12:12:25 -0700402 if (mSource->feedMoreTSData() == OK) {
Andreas Huber1183a4a2011-11-03 11:00:21 -0700403 msg->post(10000ll);
Andreas Huber5bc087c2010-12-23 10:27:40 -0800404 }
Andreas Huberf9334412010-12-15 15:17:42 -0800405 }
406 } else if (what == ACodec::kWhatEOS) {
Andreas Huberdc9bacd2011-09-26 10:53:29 -0700407 int32_t err;
408 CHECK(codecRequest->findInt32("err", &err));
409
410 if (err == ERROR_END_OF_STREAM) {
Steve Block3856b092011-10-20 11:56:00 +0100411 ALOGV("got %s decoder EOS", audio ? "audio" : "video");
Andreas Huberdc9bacd2011-09-26 10:53:29 -0700412 } else {
Steve Block3856b092011-10-20 11:56:00 +0100413 ALOGV("got %s decoder EOS w/ error %d",
Andreas Huberdc9bacd2011-09-26 10:53:29 -0700414 audio ? "audio" : "video",
415 err);
416 }
417
418 mRenderer->queueEOS(audio, err);
Andreas Huberf9334412010-12-15 15:17:42 -0800419 } else if (what == ACodec::kWhatFlushCompleted) {
Andreas Huber1aef2112011-01-04 14:01:29 -0800420 bool needShutdown;
Andreas Huber53df1a42010-12-22 10:03:04 -0800421
Andreas Huberf9334412010-12-15 15:17:42 -0800422 if (audio) {
Andreas Huber1aef2112011-01-04 14:01:29 -0800423 CHECK(IsFlushingState(mFlushingAudio, &needShutdown));
Andreas Huberf9334412010-12-15 15:17:42 -0800424 mFlushingAudio = FLUSHED;
425 } else {
Andreas Huber1aef2112011-01-04 14:01:29 -0800426 CHECK(IsFlushingState(mFlushingVideo, &needShutdown));
Andreas Huberf9334412010-12-15 15:17:42 -0800427 mFlushingVideo = FLUSHED;
Andreas Huber3fe62152011-09-16 15:09:22 -0700428
429 mVideoLateByUs = 0;
Andreas Huberf9334412010-12-15 15:17:42 -0800430 }
431
Steve Block3856b092011-10-20 11:56:00 +0100432 ALOGV("decoder %s flush completed", audio ? "audio" : "video");
Andreas Huberf9334412010-12-15 15:17:42 -0800433
Andreas Huber1aef2112011-01-04 14:01:29 -0800434 if (needShutdown) {
Steve Block3856b092011-10-20 11:56:00 +0100435 ALOGV("initiating %s decoder shutdown",
Andreas Huber53df1a42010-12-22 10:03:04 -0800436 audio ? "audio" : "video");
Andreas Huberf9334412010-12-15 15:17:42 -0800437
Andreas Huber53df1a42010-12-22 10:03:04 -0800438 (audio ? mAudioDecoder : mVideoDecoder)->initiateShutdown();
Andreas Huberf9334412010-12-15 15:17:42 -0800439
Andreas Huber53df1a42010-12-22 10:03:04 -0800440 if (audio) {
441 mFlushingAudio = SHUTTING_DOWN_DECODER;
442 } else {
443 mFlushingVideo = SHUTTING_DOWN_DECODER;
444 }
Andreas Huberf9334412010-12-15 15:17:42 -0800445 }
Andreas Huber3831a062010-12-21 10:22:33 -0800446
447 finishFlushIfPossible();
Andreas Huber2c2814b2010-12-15 17:18:20 -0800448 } else if (what == ACodec::kWhatOutputFormatChanged) {
Andreas Huber31e25082011-01-10 10:38:31 -0800449 if (audio) {
450 int32_t numChannels;
Andreas Huber516dacf2012-12-03 15:20:40 -0800451 CHECK(codecRequest->findInt32(
452 "channel-count", &numChannels));
Andreas Huber2c2814b2010-12-15 17:18:20 -0800453
Andreas Huber31e25082011-01-10 10:38:31 -0800454 int32_t sampleRate;
455 CHECK(codecRequest->findInt32("sample-rate", &sampleRate));
Andreas Huber2c2814b2010-12-15 17:18:20 -0800456
Steve Block3856b092011-10-20 11:56:00 +0100457 ALOGV("Audio output format changed to %d Hz, %d channels",
Andreas Huber31e25082011-01-10 10:38:31 -0800458 sampleRate, numChannels);
Andreas Huber2c2814b2010-12-15 17:18:20 -0800459
Andreas Huber31e25082011-01-10 10:38:31 -0800460 mAudioSink->close();
Eric Laurent1948eb32012-04-13 16:50:19 -0700461
462 audio_output_flags_t flags;
463 int64_t durationUs;
Andreas Huber516dacf2012-12-03 15:20:40 -0800464 // FIXME: we should handle the case where the video decoder
465 // is created after we receive the format change indication.
466 // Current code will just make that we select deep buffer
467 // with video which should not be a problem as it should
Eric Laurent1948eb32012-04-13 16:50:19 -0700468 // not prevent from keeping A/V sync.
469 if (mVideoDecoder == NULL &&
470 mSource->getDuration(&durationUs) == OK &&
Andreas Huber516dacf2012-12-03 15:20:40 -0800471 durationUs
472 > AUDIO_SINK_MIN_DEEP_BUFFER_DURATION_US) {
Eric Laurent1948eb32012-04-13 16:50:19 -0700473 flags = AUDIO_OUTPUT_FLAG_DEEP_BUFFER;
474 } else {
475 flags = AUDIO_OUTPUT_FLAG_NONE;
476 }
477
Andreas Huber98065552012-05-03 11:33:01 -0700478 int32_t channelMask;
479 if (!codecRequest->findInt32("channel-mask", &channelMask)) {
480 channelMask = CHANNEL_MASK_USE_CHANNEL_ORDER;
481 }
482
Andreas Huber078cfcf2011-09-15 12:25:04 -0700483 CHECK_EQ(mAudioSink->open(
484 sampleRate,
485 numChannels,
Andreas Huber98065552012-05-03 11:33:01 -0700486 (audio_channel_mask_t)channelMask,
Andreas Huber078cfcf2011-09-15 12:25:04 -0700487 AUDIO_FORMAT_PCM_16_BIT,
Eric Laurent1948eb32012-04-13 16:50:19 -0700488 8 /* bufferCount */,
489 NULL,
490 NULL,
491 flags),
Andreas Huber078cfcf2011-09-15 12:25:04 -0700492 (status_t)OK);
Andreas Huber31e25082011-01-10 10:38:31 -0800493 mAudioSink->start();
Andreas Huber2c2814b2010-12-15 17:18:20 -0800494
Andreas Huber31e25082011-01-10 10:38:31 -0800495 mRenderer->signalAudioSinkChanged();
496 } else {
497 // video
Andreas Huber3831a062010-12-21 10:22:33 -0800498
Andreas Huber31e25082011-01-10 10:38:31 -0800499 int32_t width, height;
500 CHECK(codecRequest->findInt32("width", &width));
501 CHECK(codecRequest->findInt32("height", &height));
502
503 int32_t cropLeft, cropTop, cropRight, cropBottom;
504 CHECK(codecRequest->findRect(
505 "crop",
506 &cropLeft, &cropTop, &cropRight, &cropBottom));
507
Andreas Huber516dacf2012-12-03 15:20:40 -0800508 int32_t displayWidth = cropRight - cropLeft + 1;
509 int32_t displayHeight = cropBottom - cropTop + 1;
510
Steve Block3856b092011-10-20 11:56:00 +0100511 ALOGV("Video output format changed to %d x %d "
Andreas Hubercb67cd12011-08-26 16:02:19 -0700512 "(crop: %d x %d @ (%d, %d))",
Andreas Huber31e25082011-01-10 10:38:31 -0800513 width, height,
Andreas Huber516dacf2012-12-03 15:20:40 -0800514 displayWidth,
515 displayHeight,
Andreas Hubercb67cd12011-08-26 16:02:19 -0700516 cropLeft, cropTop);
Andreas Huber31e25082011-01-10 10:38:31 -0800517
Andreas Huber516dacf2012-12-03 15:20:40 -0800518 sp<AMessage> videoInputFormat =
519 mSource->getFormat(false /* audio */);
520
521 // Take into account sample aspect ratio if necessary:
522 int32_t sarWidth, sarHeight;
523 if (videoInputFormat->findInt32("sar-width", &sarWidth)
524 && videoInputFormat->findInt32(
525 "sar-height", &sarHeight)) {
526 ALOGV("Sample aspect ratio %d : %d",
527 sarWidth, sarHeight);
528
529 displayWidth = (displayWidth * sarWidth) / sarHeight;
530
531 ALOGV("display dimensions %d x %d",
532 displayWidth, displayHeight);
533 }
534
Andreas Huber31e25082011-01-10 10:38:31 -0800535 notifyListener(
Andreas Huber516dacf2012-12-03 15:20:40 -0800536 MEDIA_SET_VIDEO_SIZE, displayWidth, displayHeight);
Andreas Huber31e25082011-01-10 10:38:31 -0800537 }
Andreas Huber3831a062010-12-21 10:22:33 -0800538 } else if (what == ACodec::kWhatShutdownCompleted) {
Steve Block3856b092011-10-20 11:56:00 +0100539 ALOGV("%s shutdown completed", audio ? "audio" : "video");
Andreas Huber3831a062010-12-21 10:22:33 -0800540 if (audio) {
541 mAudioDecoder.clear();
542
543 CHECK_EQ((int)mFlushingAudio, (int)SHUTTING_DOWN_DECODER);
544 mFlushingAudio = SHUT_DOWN;
545 } else {
546 mVideoDecoder.clear();
547
548 CHECK_EQ((int)mFlushingVideo, (int)SHUTTING_DOWN_DECODER);
549 mFlushingVideo = SHUT_DOWN;
550 }
551
552 finishFlushIfPossible();
Andreas Huberc92fd242011-08-16 13:48:44 -0700553 } else if (what == ACodec::kWhatError) {
Steve Block29357bc2012-01-06 19:20:56 +0000554 ALOGE("Received error from %s decoder, aborting playback.",
Andreas Huberc92fd242011-08-16 13:48:44 -0700555 audio ? "audio" : "video");
556
557 mRenderer->queueEOS(audio, UNKNOWN_ERROR);
Andreas Huber57788222012-02-21 11:47:18 -0800558 } else if (what == ACodec::kWhatDrainThisBuffer) {
Andreas Huberf9334412010-12-15 15:17:42 -0800559 renderBuffer(audio, codecRequest);
Andreas Hubera1f8ab02012-11-30 10:53:22 -0800560 } else if (what != ACodec::kWhatComponentAllocated
561 && what != ACodec::kWhatComponentConfigured
562 && what != ACodec::kWhatBuffersAllocated) {
563 ALOGV("Unhandled codec notification %d '%c%c%c%c'.",
564 what,
565 what >> 24,
566 (what >> 16) & 0xff,
567 (what >> 8) & 0xff,
568 what & 0xff);
Andreas Huberf9334412010-12-15 15:17:42 -0800569 }
570
571 break;
572 }
573
574 case kWhatRendererNotify:
575 {
576 int32_t what;
577 CHECK(msg->findInt32("what", &what));
578
579 if (what == Renderer::kWhatEOS) {
580 int32_t audio;
581 CHECK(msg->findInt32("audio", &audio));
582
Andreas Huberc92fd242011-08-16 13:48:44 -0700583 int32_t finalResult;
584 CHECK(msg->findInt32("finalResult", &finalResult));
585
Andreas Huberf9334412010-12-15 15:17:42 -0800586 if (audio) {
587 mAudioEOS = true;
588 } else {
589 mVideoEOS = true;
590 }
591
Andreas Huberc92fd242011-08-16 13:48:44 -0700592 if (finalResult == ERROR_END_OF_STREAM) {
Steve Block3856b092011-10-20 11:56:00 +0100593 ALOGV("reached %s EOS", audio ? "audio" : "video");
Andreas Huberc92fd242011-08-16 13:48:44 -0700594 } else {
Steve Block29357bc2012-01-06 19:20:56 +0000595 ALOGE("%s track encountered an error (%d)",
Andreas Huberc92fd242011-08-16 13:48:44 -0700596 audio ? "audio" : "video", finalResult);
597
598 notifyListener(
599 MEDIA_ERROR, MEDIA_ERROR_UNKNOWN, finalResult);
600 }
Andreas Huberf9334412010-12-15 15:17:42 -0800601
602 if ((mAudioEOS || mAudioDecoder == NULL)
603 && (mVideoEOS || mVideoDecoder == NULL)) {
604 notifyListener(MEDIA_PLAYBACK_COMPLETE, 0, 0);
605 }
Andreas Huber43c3e6c2011-01-05 12:17:08 -0800606 } else if (what == Renderer::kWhatPosition) {
607 int64_t positionUs;
608 CHECK(msg->findInt64("positionUs", &positionUs));
609
Andreas Huber3fe62152011-09-16 15:09:22 -0700610 CHECK(msg->findInt64("videoLateByUs", &mVideoLateByUs));
611
Andreas Huber43c3e6c2011-01-05 12:17:08 -0800612 if (mDriver != NULL) {
613 sp<NuPlayerDriver> driver = mDriver.promote();
614 if (driver != NULL) {
615 driver->notifyPosition(positionUs);
Andreas Huber3fe62152011-09-16 15:09:22 -0700616
617 driver->notifyFrameStats(
618 mNumFramesTotal, mNumFramesDropped);
Andreas Huber43c3e6c2011-01-05 12:17:08 -0800619 }
620 }
Andreas Huber3fe62152011-09-16 15:09:22 -0700621 } else if (what == Renderer::kWhatFlushComplete) {
Andreas Huberf9334412010-12-15 15:17:42 -0800622 int32_t audio;
623 CHECK(msg->findInt32("audio", &audio));
624
Steve Block3856b092011-10-20 11:56:00 +0100625 ALOGV("renderer %s flush completed.", audio ? "audio" : "video");
James Dongf57b4ea2012-07-20 13:38:36 -0700626 } else if (what == Renderer::kWhatVideoRenderingStart) {
627 notifyListener(MEDIA_INFO, MEDIA_INFO_RENDERING_START, 0);
Andreas Huberf9334412010-12-15 15:17:42 -0800628 }
629 break;
630 }
631
632 case kWhatMoreDataQueued:
633 {
634 break;
635 }
636
Andreas Huber1aef2112011-01-04 14:01:29 -0800637 case kWhatReset:
638 {
Steve Block3856b092011-10-20 11:56:00 +0100639 ALOGV("kWhatReset");
Andreas Huber1aef2112011-01-04 14:01:29 -0800640
Andreas Hubera1f8ab02012-11-30 10:53:22 -0800641 mDeferredActions.push_back(
642 new SimpleAction(&NuPlayer::performDecoderShutdown));
Andreas Huberb7c8e912012-11-27 15:02:53 -0800643
Andreas Hubera1f8ab02012-11-30 10:53:22 -0800644 mDeferredActions.push_back(
645 new SimpleAction(&NuPlayer::performReset));
Andreas Huberb58ce9f2011-11-28 16:27:35 -0800646
Andreas Hubera1f8ab02012-11-30 10:53:22 -0800647 processDeferredActions();
Andreas Huber1aef2112011-01-04 14:01:29 -0800648 break;
649 }
650
Andreas Huber43c3e6c2011-01-05 12:17:08 -0800651 case kWhatSeek:
652 {
653 int64_t seekTimeUs;
654 CHECK(msg->findInt64("seekTimeUs", &seekTimeUs));
655
Andreas Hubera1f8ab02012-11-30 10:53:22 -0800656 ALOGV("kWhatSeek seekTimeUs=%lld us", seekTimeUs);
Andreas Huber43c3e6c2011-01-05 12:17:08 -0800657
Andreas Hubera1f8ab02012-11-30 10:53:22 -0800658 mDeferredActions.push_back(
659 new SimpleAction(&NuPlayer::performDecoderFlush));
Andreas Huber43c3e6c2011-01-05 12:17:08 -0800660
Andreas Hubera1f8ab02012-11-30 10:53:22 -0800661 mDeferredActions.push_back(new SeekAction(seekTimeUs));
Andreas Huber43c3e6c2011-01-05 12:17:08 -0800662
Andreas Hubera1f8ab02012-11-30 10:53:22 -0800663 processDeferredActions();
Andreas Huber43c3e6c2011-01-05 12:17:08 -0800664 break;
665 }
666
Andreas Huberb4082222011-01-20 15:23:04 -0800667 case kWhatPause:
668 {
669 CHECK(mRenderer != NULL);
670 mRenderer->pause();
671 break;
672 }
673
674 case kWhatResume:
675 {
676 CHECK(mRenderer != NULL);
677 mRenderer->resume();
678 break;
679 }
680
Andreas Huberf9334412010-12-15 15:17:42 -0800681 default:
682 TRESPASS();
683 break;
684 }
685}
686
Andreas Huber3831a062010-12-21 10:22:33 -0800687void NuPlayer::finishFlushIfPossible() {
688 if (mFlushingAudio != FLUSHED && mFlushingAudio != SHUT_DOWN) {
689 return;
690 }
691
692 if (mFlushingVideo != FLUSHED && mFlushingVideo != SHUT_DOWN) {
693 return;
694 }
695
Steve Block3856b092011-10-20 11:56:00 +0100696 ALOGV("both audio and video are flushed now.");
Andreas Huber3831a062010-12-21 10:22:33 -0800697
Andreas Huber6e3d3112011-11-28 12:36:11 -0800698 if (mTimeDiscontinuityPending) {
699 mRenderer->signalTimeDiscontinuity();
700 mTimeDiscontinuityPending = false;
701 }
Andreas Huber3831a062010-12-21 10:22:33 -0800702
Andreas Huber22fc52f2011-01-05 16:24:27 -0800703 if (mAudioDecoder != NULL) {
Andreas Huber3831a062010-12-21 10:22:33 -0800704 mAudioDecoder->signalResume();
705 }
706
Andreas Huber22fc52f2011-01-05 16:24:27 -0800707 if (mVideoDecoder != NULL) {
Andreas Huber3831a062010-12-21 10:22:33 -0800708 mVideoDecoder->signalResume();
709 }
710
711 mFlushingAudio = NONE;
712 mFlushingVideo = NONE;
Andreas Huber3831a062010-12-21 10:22:33 -0800713
Andreas Hubera1f8ab02012-11-30 10:53:22 -0800714 processDeferredActions();
Andreas Huber1aef2112011-01-04 14:01:29 -0800715}
716
717void NuPlayer::postScanSources() {
718 if (mScanSourcesPending) {
719 return;
720 }
721
722 sp<AMessage> msg = new AMessage(kWhatScanSources, id());
723 msg->setInt32("generation", mScanSourcesGeneration);
724 msg->post();
725
726 mScanSourcesPending = true;
727}
728
Andreas Huber5bc087c2010-12-23 10:27:40 -0800729status_t NuPlayer::instantiateDecoder(bool audio, sp<Decoder> *decoder) {
Andreas Huberf9334412010-12-15 15:17:42 -0800730 if (*decoder != NULL) {
731 return OK;
732 }
733
Andreas Huber84066782011-08-16 09:34:26 -0700734 sp<AMessage> format = mSource->getFormat(audio);
Andreas Huberf9334412010-12-15 15:17:42 -0800735
Andreas Huber84066782011-08-16 09:34:26 -0700736 if (format == NULL) {
Andreas Huberf9334412010-12-15 15:17:42 -0800737 return -EWOULDBLOCK;
738 }
739
Andreas Huber3fe62152011-09-16 15:09:22 -0700740 if (!audio) {
Andreas Huber84066782011-08-16 09:34:26 -0700741 AString mime;
742 CHECK(format->findString("mime", &mime));
743 mVideoIsAVC = !strcasecmp(MEDIA_MIMETYPE_VIDEO_AVC, mime.c_str());
Andreas Huber3fe62152011-09-16 15:09:22 -0700744 }
745
Andreas Huberf9334412010-12-15 15:17:42 -0800746 sp<AMessage> notify =
747 new AMessage(audio ? kWhatAudioNotify : kWhatVideoNotify,
748 id());
749
Glenn Kasten11731182011-02-08 17:26:17 -0800750 *decoder = audio ? new Decoder(notify) :
751 new Decoder(notify, mNativeWindow);
Andreas Huberf9334412010-12-15 15:17:42 -0800752 looper()->registerHandler(*decoder);
753
Andreas Huber84066782011-08-16 09:34:26 -0700754 (*decoder)->configure(format);
Andreas Huberf9334412010-12-15 15:17:42 -0800755
Andreas Huber43c3e6c2011-01-05 12:17:08 -0800756 int64_t durationUs;
757 if (mDriver != NULL && mSource->getDuration(&durationUs) == OK) {
758 sp<NuPlayerDriver> driver = mDriver.promote();
759 if (driver != NULL) {
760 driver->notifyDuration(durationUs);
761 }
762 }
763
Andreas Huberf9334412010-12-15 15:17:42 -0800764 return OK;
765}
766
767status_t NuPlayer::feedDecoderInputData(bool audio, const sp<AMessage> &msg) {
768 sp<AMessage> reply;
769 CHECK(msg->findMessage("reply", &reply));
770
Andreas Huber53df1a42010-12-22 10:03:04 -0800771 if ((audio && IsFlushingState(mFlushingAudio))
772 || (!audio && IsFlushingState(mFlushingVideo))) {
Andreas Huberf9334412010-12-15 15:17:42 -0800773 reply->setInt32("err", INFO_DISCONTINUITY);
774 reply->post();
775 return OK;
776 }
777
778 sp<ABuffer> accessUnit;
Andreas Huberf9334412010-12-15 15:17:42 -0800779
Andreas Huber3fe62152011-09-16 15:09:22 -0700780 bool dropAccessUnit;
781 do {
782 status_t err = mSource->dequeueAccessUnit(audio, &accessUnit);
Andreas Huber5bc087c2010-12-23 10:27:40 -0800783
Andreas Huber3fe62152011-09-16 15:09:22 -0700784 if (err == -EWOULDBLOCK) {
785 return err;
786 } else if (err != OK) {
787 if (err == INFO_DISCONTINUITY) {
788 int32_t type;
789 CHECK(accessUnit->meta()->findInt32("discontinuity", &type));
Andreas Huber53df1a42010-12-22 10:03:04 -0800790
Andreas Huber3fe62152011-09-16 15:09:22 -0700791 bool formatChange =
Andreas Huber6e3d3112011-11-28 12:36:11 -0800792 (audio &&
793 (type & ATSParser::DISCONTINUITY_AUDIO_FORMAT))
794 || (!audio &&
795 (type & ATSParser::DISCONTINUITY_VIDEO_FORMAT));
Andreas Huber53df1a42010-12-22 10:03:04 -0800796
Andreas Huber6e3d3112011-11-28 12:36:11 -0800797 bool timeChange = (type & ATSParser::DISCONTINUITY_TIME) != 0;
798
Steve Blockdf64d152012-01-04 20:05:49 +0000799 ALOGI("%s discontinuity (formatChange=%d, time=%d)",
Andreas Huber6e3d3112011-11-28 12:36:11 -0800800 audio ? "audio" : "video", formatChange, timeChange);
Andreas Huber32f3cef2011-03-02 15:34:46 -0800801
Andreas Huber3fe62152011-09-16 15:09:22 -0700802 if (audio) {
803 mSkipRenderingAudioUntilMediaTimeUs = -1;
804 } else {
805 mSkipRenderingVideoUntilMediaTimeUs = -1;
806 }
Andreas Huber32f3cef2011-03-02 15:34:46 -0800807
Andreas Huber6e3d3112011-11-28 12:36:11 -0800808 if (timeChange) {
809 sp<AMessage> extra;
810 if (accessUnit->meta()->findMessage("extra", &extra)
811 && extra != NULL) {
812 int64_t resumeAtMediaTimeUs;
813 if (extra->findInt64(
814 "resume-at-mediatimeUs", &resumeAtMediaTimeUs)) {
Steve Blockdf64d152012-01-04 20:05:49 +0000815 ALOGI("suppressing rendering of %s until %lld us",
Andreas Huber6e3d3112011-11-28 12:36:11 -0800816 audio ? "audio" : "video", resumeAtMediaTimeUs);
Andreas Huber3fe62152011-09-16 15:09:22 -0700817
Andreas Huber6e3d3112011-11-28 12:36:11 -0800818 if (audio) {
819 mSkipRenderingAudioUntilMediaTimeUs =
820 resumeAtMediaTimeUs;
821 } else {
822 mSkipRenderingVideoUntilMediaTimeUs =
823 resumeAtMediaTimeUs;
824 }
Andreas Huber3fe62152011-09-16 15:09:22 -0700825 }
Andreas Huber32f3cef2011-03-02 15:34:46 -0800826 }
827 }
Andreas Huber3fe62152011-09-16 15:09:22 -0700828
Andreas Huber6e3d3112011-11-28 12:36:11 -0800829 mTimeDiscontinuityPending =
830 mTimeDiscontinuityPending || timeChange;
831
832 if (formatChange || timeChange) {
Andreas Hubera1f8ab02012-11-30 10:53:22 -0800833 if (mFlushingAudio == NONE && mFlushingVideo == NONE) {
834 // And we'll resume scanning sources once we're done
835 // flushing.
836 mDeferredActions.push_front(
837 new SimpleAction(
838 &NuPlayer::performScanSources));
839 }
840
Andreas Huber6e3d3112011-11-28 12:36:11 -0800841 flushDecoder(audio, formatChange);
842 } else {
843 // This stream is unaffected by the discontinuity
844
845 if (audio) {
846 mFlushingAudio = FLUSHED;
847 } else {
848 mFlushingVideo = FLUSHED;
849 }
850
851 finishFlushIfPossible();
852
853 return -EWOULDBLOCK;
854 }
Andreas Huber32f3cef2011-03-02 15:34:46 -0800855 }
856
Andreas Huber3fe62152011-09-16 15:09:22 -0700857 reply->setInt32("err", err);
858 reply->post();
859 return OK;
Andreas Huberf9334412010-12-15 15:17:42 -0800860 }
861
Andreas Huber3fe62152011-09-16 15:09:22 -0700862 if (!audio) {
863 ++mNumFramesTotal;
864 }
865
866 dropAccessUnit = false;
867 if (!audio
868 && mVideoLateByUs > 100000ll
869 && mVideoIsAVC
870 && !IsAVCReferenceFrame(accessUnit)) {
871 dropAccessUnit = true;
872 ++mNumFramesDropped;
873 }
874 } while (dropAccessUnit);
Andreas Huberf9334412010-12-15 15:17:42 -0800875
Steve Block3856b092011-10-20 11:56:00 +0100876 // ALOGV("returned a valid buffer of %s data", audio ? "audio" : "video");
Andreas Huberf9334412010-12-15 15:17:42 -0800877
878#if 0
879 int64_t mediaTimeUs;
880 CHECK(accessUnit->meta()->findInt64("timeUs", &mediaTimeUs));
Steve Block3856b092011-10-20 11:56:00 +0100881 ALOGV("feeding %s input buffer at media time %.2f secs",
Andreas Huberf9334412010-12-15 15:17:42 -0800882 audio ? "audio" : "video",
883 mediaTimeUs / 1E6);
884#endif
885
Andreas Huber2d8bedd2012-02-21 14:38:23 -0800886 reply->setBuffer("buffer", accessUnit);
Andreas Huberf9334412010-12-15 15:17:42 -0800887 reply->post();
888
889 return OK;
890}
891
892void NuPlayer::renderBuffer(bool audio, const sp<AMessage> &msg) {
Steve Block3856b092011-10-20 11:56:00 +0100893 // ALOGV("renderBuffer %s", audio ? "audio" : "video");
Andreas Huberf9334412010-12-15 15:17:42 -0800894
895 sp<AMessage> reply;
896 CHECK(msg->findMessage("reply", &reply));
897
Andreas Huber18ac5402011-08-31 15:04:25 -0700898 if (IsFlushingState(audio ? mFlushingAudio : mFlushingVideo)) {
899 // We're currently attempting to flush the decoder, in order
900 // to complete this, the decoder wants all its buffers back,
901 // so we don't want any output buffers it sent us (from before
902 // we initiated the flush) to be stuck in the renderer's queue.
903
Steve Block3856b092011-10-20 11:56:00 +0100904 ALOGV("we're still flushing the %s decoder, sending its output buffer"
Andreas Huber18ac5402011-08-31 15:04:25 -0700905 " right back.", audio ? "audio" : "video");
906
907 reply->post();
908 return;
909 }
910
Andreas Huber2d8bedd2012-02-21 14:38:23 -0800911 sp<ABuffer> buffer;
912 CHECK(msg->findBuffer("buffer", &buffer));
Andreas Huberf9334412010-12-15 15:17:42 -0800913
Andreas Huber32f3cef2011-03-02 15:34:46 -0800914 int64_t &skipUntilMediaTimeUs =
915 audio
916 ? mSkipRenderingAudioUntilMediaTimeUs
917 : mSkipRenderingVideoUntilMediaTimeUs;
918
919 if (skipUntilMediaTimeUs >= 0) {
920 int64_t mediaTimeUs;
921 CHECK(buffer->meta()->findInt64("timeUs", &mediaTimeUs));
922
923 if (mediaTimeUs < skipUntilMediaTimeUs) {
Steve Block3856b092011-10-20 11:56:00 +0100924 ALOGV("dropping %s buffer at time %lld as requested.",
Andreas Huber32f3cef2011-03-02 15:34:46 -0800925 audio ? "audio" : "video",
926 mediaTimeUs);
927
928 reply->post();
929 return;
930 }
931
932 skipUntilMediaTimeUs = -1;
933 }
934
Andreas Huberf9334412010-12-15 15:17:42 -0800935 mRenderer->queueBuffer(audio, buffer, reply);
936}
937
938void NuPlayer::notifyListener(int msg, int ext1, int ext2) {
Andreas Huber43c3e6c2011-01-05 12:17:08 -0800939 if (mDriver == NULL) {
Andreas Huberf9334412010-12-15 15:17:42 -0800940 return;
941 }
942
Andreas Huber43c3e6c2011-01-05 12:17:08 -0800943 sp<NuPlayerDriver> driver = mDriver.promote();
Andreas Huberf9334412010-12-15 15:17:42 -0800944
Andreas Huber43c3e6c2011-01-05 12:17:08 -0800945 if (driver == NULL) {
Andreas Huberf9334412010-12-15 15:17:42 -0800946 return;
947 }
948
Andreas Hubera4af2142011-10-26 15:23:31 -0700949 driver->notifyListener(msg, ext1, ext2);
Andreas Huberf9334412010-12-15 15:17:42 -0800950}
951
Andreas Huber1aef2112011-01-04 14:01:29 -0800952void NuPlayer::flushDecoder(bool audio, bool needShutdown) {
Andreas Huber6e3d3112011-11-28 12:36:11 -0800953 if ((audio && mAudioDecoder == NULL) || (!audio && mVideoDecoder == NULL)) {
Steve Blockdf64d152012-01-04 20:05:49 +0000954 ALOGI("flushDecoder %s without decoder present",
Andreas Huber6e3d3112011-11-28 12:36:11 -0800955 audio ? "audio" : "video");
956 }
957
Andreas Huber1aef2112011-01-04 14:01:29 -0800958 // Make sure we don't continue to scan sources until we finish flushing.
959 ++mScanSourcesGeneration;
Andreas Huber43c3e6c2011-01-05 12:17:08 -0800960 mScanSourcesPending = false;
Andreas Huber1aef2112011-01-04 14:01:29 -0800961
962 (audio ? mAudioDecoder : mVideoDecoder)->signalFlush();
963 mRenderer->flush(audio);
964
965 FlushStatus newStatus =
966 needShutdown ? FLUSHING_DECODER_SHUTDOWN : FLUSHING_DECODER;
967
968 if (audio) {
969 CHECK(mFlushingAudio == NONE
970 || mFlushingAudio == AWAITING_DISCONTINUITY);
971
972 mFlushingAudio = newStatus;
973
974 if (mFlushingVideo == NONE) {
975 mFlushingVideo = (mVideoDecoder != NULL)
976 ? AWAITING_DISCONTINUITY
977 : FLUSHED;
978 }
979 } else {
980 CHECK(mFlushingVideo == NONE
981 || mFlushingVideo == AWAITING_DISCONTINUITY);
982
983 mFlushingVideo = newStatus;
984
985 if (mFlushingAudio == NONE) {
986 mFlushingAudio = (mAudioDecoder != NULL)
987 ? AWAITING_DISCONTINUITY
988 : FLUSHED;
989 }
990 }
991}
992
Andreas Huber84066782011-08-16 09:34:26 -0700993sp<AMessage> NuPlayer::Source::getFormat(bool audio) {
994 sp<MetaData> meta = getFormatMeta(audio);
995
996 if (meta == NULL) {
997 return NULL;
998 }
999
1000 sp<AMessage> msg = new AMessage;
1001
1002 if(convertMetaDataToMessage(meta, &msg) == OK) {
1003 return msg;
1004 }
1005 return NULL;
1006}
1007
James Dong0d268a32012-08-31 12:18:27 -07001008status_t NuPlayer::setVideoScalingMode(int32_t mode) {
1009 mVideoScalingMode = mode;
James Dong376074e2012-09-14 15:34:35 -07001010 if (mNativeWindow != NULL
1011 && mNativeWindow->getNativeWindow() != NULL) {
James Dong0d268a32012-08-31 12:18:27 -07001012 status_t ret = native_window_set_scaling_mode(
1013 mNativeWindow->getNativeWindow().get(), mVideoScalingMode);
1014 if (ret != OK) {
1015 ALOGE("Failed to set scaling mode (%d): %s",
1016 -ret, strerror(-ret));
1017 return ret;
1018 }
1019 }
1020 return OK;
1021}
1022
Andreas Huberb7c8e912012-11-27 15:02:53 -08001023void NuPlayer::schedulePollDuration() {
1024 sp<AMessage> msg = new AMessage(kWhatPollDuration, id());
1025 msg->setInt32("generation", mPollDurationGeneration);
1026 msg->post();
1027}
1028
1029void NuPlayer::cancelPollDuration() {
1030 ++mPollDurationGeneration;
1031}
1032
Andreas Hubera1f8ab02012-11-30 10:53:22 -08001033void NuPlayer::processDeferredActions() {
1034 while (!mDeferredActions.empty()) {
1035 // We won't execute any deferred actions until we're no longer in
1036 // an intermediate state, i.e. one more more decoders are currently
1037 // flushing or shutting down.
1038
1039 if (mRenderer != NULL) {
1040 // There's an edge case where the renderer owns all output
1041 // buffers and is paused, therefore the decoder will not read
1042 // more input data and will never encounter the matching
1043 // discontinuity. To avoid this, we resume the renderer.
1044
1045 if (mFlushingAudio == AWAITING_DISCONTINUITY
1046 || mFlushingVideo == AWAITING_DISCONTINUITY) {
1047 mRenderer->resume();
1048 }
1049 }
1050
1051 if (mFlushingAudio != NONE || mFlushingVideo != NONE) {
1052 // We're currently flushing, postpone the reset until that's
1053 // completed.
1054
1055 ALOGV("postponing action mFlushingAudio=%d, mFlushingVideo=%d",
1056 mFlushingAudio, mFlushingVideo);
1057
1058 break;
1059 }
1060
1061 sp<Action> action = *mDeferredActions.begin();
1062 mDeferredActions.erase(mDeferredActions.begin());
1063
1064 action->execute(this);
1065 }
1066}
1067
1068void NuPlayer::performSeek(int64_t seekTimeUs) {
1069 ALOGV("performSeek seekTimeUs=%lld us (%.2f secs)",
1070 seekTimeUs,
1071 seekTimeUs / 1E6);
1072
1073 mSource->seekTo(seekTimeUs);
1074
1075 if (mDriver != NULL) {
1076 sp<NuPlayerDriver> driver = mDriver.promote();
1077 if (driver != NULL) {
1078 driver->notifyPosition(seekTimeUs);
1079 driver->notifySeekComplete();
1080 }
1081 }
1082
1083 // everything's flushed, continue playback.
1084}
1085
1086void NuPlayer::performDecoderFlush() {
1087 ALOGV("performDecoderFlush");
1088
1089 if (mAudioDecoder != NULL && mVideoDecoder == NULL) {
1090 return;
1091 }
1092
1093 mTimeDiscontinuityPending = true;
1094
1095 if (mAudioDecoder != NULL) {
1096 flushDecoder(true /* audio */, false /* needShutdown */);
1097 }
1098
1099 if (mVideoDecoder != NULL) {
1100 flushDecoder(false /* audio */, false /* needShutdown */);
1101 }
1102}
1103
1104void NuPlayer::performDecoderShutdown() {
1105 ALOGV("performDecoderShutdown");
1106
1107 if (mAudioDecoder != NULL && mVideoDecoder == NULL) {
1108 return;
1109 }
1110
1111 mTimeDiscontinuityPending = true;
1112
1113 if (mAudioDecoder != NULL) {
1114 flushDecoder(true /* audio */, true /* needShutdown */);
1115 }
1116
1117 if (mVideoDecoder != NULL) {
1118 flushDecoder(false /* audio */, true /* needShutdown */);
1119 }
1120}
1121
1122void NuPlayer::performReset() {
1123 ALOGV("performReset");
1124
1125 CHECK(mAudioDecoder == NULL);
1126 CHECK(mVideoDecoder == NULL);
1127
1128 cancelPollDuration();
1129
1130 ++mScanSourcesGeneration;
1131 mScanSourcesPending = false;
1132
1133 mRenderer.clear();
1134
1135 if (mSource != NULL) {
1136 mSource->stop();
1137 mSource.clear();
1138 }
1139
1140 if (mDriver != NULL) {
1141 sp<NuPlayerDriver> driver = mDriver.promote();
1142 if (driver != NULL) {
1143 driver->notifyResetComplete();
1144 }
1145 }
1146}
1147
1148void NuPlayer::performScanSources() {
1149 ALOGV("performScanSources");
1150
1151 if (mAudioDecoder == NULL || mVideoDecoder == NULL) {
1152 postScanSources();
1153 }
1154}
1155
Andreas Huberf9334412010-12-15 15:17:42 -08001156} // namespace android