blob: 7ad822b11ff447a8e8bf231570f70d0d2785618f [file] [log] [blame]
Dharmaray Kundargi643290d2011-01-16 16:02:42 -08001/*
2 * Copyright (C) 2011 NXP Software
3 * Copyright (C) 2011 The Android Open Source Project
4 *
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 */
17
18#define LOG_NDEBUG 1
19#define LOG_TAG "PreviewPlayer"
20#include <utils/Log.h>
21
22#include <dlfcn.h>
23
24#include "include/ARTSPController.h"
25#include "PreviewPlayer.h"
26#include "DummyAudioSource.h"
27#include "DummyVideoSource.h"
28#include "VideoEditorSRC.h"
29#include "include/LiveSession.h"
30#include "include/NuCachedSource2.h"
31#include "include/ThrottledSource.h"
32
33
34#include "PreviewRenderer.h"
35
36#include <binder/IPCThreadState.h>
37#include <media/stagefright/DataSource.h>
38#include <media/stagefright/FileSource.h>
39#include <media/stagefright/MediaBuffer.h>
40#include <media/stagefright/MediaDefs.h>
41#include <media/stagefright/MediaExtractor.h>
42#include <media/stagefright/MediaDebug.h>
43#include <media/stagefright/MediaSource.h>
44#include <media/stagefright/MetaData.h>
45#include <media/stagefright/OMXCodec.h>
46
47#include <surfaceflinger/Surface.h>
48#include <media/stagefright/foundation/ALooper.h>
49
50namespace android {
51
52
53struct PreviewPlayerEvent : public TimedEventQueue::Event {
54 PreviewPlayerEvent(
55 PreviewPlayer *player,
56 void (PreviewPlayer::*method)())
57 : mPlayer(player),
58 mMethod(method) {
59 }
60
61protected:
62 virtual ~PreviewPlayerEvent() {}
63
64 virtual void fire(TimedEventQueue *queue, int64_t /* now_us */) {
65 (mPlayer->*mMethod)();
66 }
67
68private:
69 PreviewPlayer *mPlayer;
70 void (PreviewPlayer::*mMethod)();
71
72 PreviewPlayerEvent(const PreviewPlayerEvent &);
73 PreviewPlayerEvent &operator=(const PreviewPlayerEvent &);
74};
75
76
77struct PreviewLocalRenderer : public PreviewPlayerRenderer {
78 PreviewLocalRenderer(
79 bool previewOnly,
80 OMX_COLOR_FORMATTYPE colorFormat,
81 const sp<Surface> &surface,
82 size_t displayWidth, size_t displayHeight,
83 size_t decodedWidth, size_t decodedHeight,
84 int32_t rotationDegrees = 0)
85 : mTarget(NULL) {
86 init(previewOnly,
87 colorFormat, surface,
88 displayWidth, displayHeight,
89 decodedWidth, decodedHeight,
90 rotationDegrees);
91 }
92
93 virtual void render(MediaBuffer *buffer) {
94 render((const uint8_t *)buffer->data() + buffer->range_offset(),
95 buffer->range_length());
96 }
97
98 void render(const void *data, size_t size) {
99 mTarget->render(data, size, NULL);
100 }
101 void render() {
102 mTarget->renderYV12();
103 }
104 void getBuffer(uint8_t **data, size_t *stride) {
105 mTarget->getBufferYV12(data, stride);
106 }
107
108protected:
109 virtual ~PreviewLocalRenderer() {
110 delete mTarget;
111 mTarget = NULL;
112 }
113
114private:
115 PreviewRenderer *mTarget;
116
117 void init(
118 bool previewOnly,
119 OMX_COLOR_FORMATTYPE colorFormat,
120 const sp<Surface> &surface,
121 size_t displayWidth, size_t displayHeight,
122 size_t decodedWidth, size_t decodedHeight,
123 int32_t rotationDegrees = 0);
124
125 PreviewLocalRenderer(const PreviewLocalRenderer &);
126 PreviewLocalRenderer &operator=(const PreviewLocalRenderer &);;
127};
128
129void PreviewLocalRenderer::init(
130 bool previewOnly,
131 OMX_COLOR_FORMATTYPE colorFormat,
132 const sp<Surface> &surface,
133 size_t displayWidth, size_t displayHeight,
134 size_t decodedWidth, size_t decodedHeight,
135 int32_t rotationDegrees) {
136 mTarget = new PreviewRenderer(
137 colorFormat, surface, displayWidth, displayHeight,
138 decodedWidth, decodedHeight, rotationDegrees);
139}
140
141PreviewPlayer::PreviewPlayer()
142 : AwesomePlayer(),
143 mFrameRGBBuffer(NULL),
Dharmaray Kundargi35cb2de2011-01-19 19:09:27 -0800144 mFrameYUVBuffer(NULL),
145 mReportedWidth(0),
Dharmaray Kundargie6c07502011-01-21 16:58:31 -0800146 mReportedHeight(0),
147 mCurrFramingEffectIndex(0) {
Dharmaray Kundargi643290d2011-01-16 16:02:42 -0800148
149 mVideoRenderer = NULL;
150 mLastVideoBuffer = NULL;
151 mSuspensionState = NULL;
152 mEffectsSettings = NULL;
153 mAudioMixStoryBoardTS = 0;
154 mCurrentMediaBeginCutTime = 0;
155 mCurrentMediaVolumeValue = 0;
156 mNumberEffects = 0;
157 mDecodedVideoTs = 0;
158 mDecVideoTsStoryBoard = 0;
159 mCurrentVideoEffect = VIDEO_EFFECT_NONE;
160 mProgressCbInterval = 0;
161 mNumberDecVideoFrames = 0;
Dharmaray Kundargie6c07502011-01-21 16:58:31 -0800162 mOverlayUpdateEventPosted = false;
Dharmaray Kundargi643290d2011-01-16 16:02:42 -0800163
164 mVideoEvent = new PreviewPlayerEvent(this, &PreviewPlayer::onVideoEvent);
165 mVideoEventPending = false;
166 mStreamDoneEvent = new PreviewPlayerEvent(this,
Dharmaray Kundargie6c07502011-01-21 16:58:31 -0800167 &AwesomePlayer::onStreamDone);
Dharmaray Kundargi643290d2011-01-16 16:02:42 -0800168
169 mStreamDoneEventPending = false;
170
171 mCheckAudioStatusEvent = new PreviewPlayerEvent(
Dharmaray Kundargie6c07502011-01-21 16:58:31 -0800172 this, &AwesomePlayer::onCheckAudioStatus);
Dharmaray Kundargi643290d2011-01-16 16:02:42 -0800173
174 mAudioStatusEventPending = false;
175
176 mProgressCbEvent = new PreviewPlayerEvent(this,
Dharmaray Kundargie6c07502011-01-21 16:58:31 -0800177 &PreviewPlayer::onProgressCbEvent);
Dharmaray Kundargi643290d2011-01-16 16:02:42 -0800178
Dharmaray Kundargie6c07502011-01-21 16:58:31 -0800179 mOverlayUpdateEvent = new PreviewPlayerEvent(this,
180 &PreviewPlayer::onUpdateOverlayEvent);
Dharmaray Kundargi643290d2011-01-16 16:02:42 -0800181 mProgressCbEventPending = false;
Dharmaray Kundargie6c07502011-01-21 16:58:31 -0800182
183 mOverlayUpdateEventPending = false;
Dharmaray Kundargi643290d2011-01-16 16:02:42 -0800184 mResizedVideoBuffer = NULL;
185 mVideoResizedOrCropped = false;
186 mRenderingMode = (M4xVSS_MediaRendering)MEDIA_RENDERING_INVALID;
187 mIsFiftiesEffectStarted = false;
188 reset();
189}
190
191PreviewPlayer::~PreviewPlayer() {
192
193 if (mQueueStarted) {
194 mQueue.stop();
195 }
196
197 reset();
198
199 if(mResizedVideoBuffer != NULL) {
200 M4OSA_free((M4OSA_MemAddr32)(mResizedVideoBuffer->data()));
201 mResizedVideoBuffer = NULL;
202 }
203
204 mVideoRenderer.clear();
205 mVideoRenderer = NULL;
206}
207
208void PreviewPlayer::cancelPlayerEvents(bool keepBufferingGoing) {
209 mQueue.cancelEvent(mVideoEvent->eventID());
210 mVideoEventPending = false;
211 mQueue.cancelEvent(mStreamDoneEvent->eventID());
212 mStreamDoneEventPending = false;
213 mQueue.cancelEvent(mCheckAudioStatusEvent->eventID());
214 mAudioStatusEventPending = false;
215
216 mQueue.cancelEvent(mProgressCbEvent->eventID());
217 mProgressCbEventPending = false;
218}
219
220status_t PreviewPlayer::setDataSource(
221 const char *uri, const KeyedVector<String8, String8> *headers) {
222 Mutex::Autolock autoLock(mLock);
223 return setDataSource_l(uri, headers);
224}
225
226status_t PreviewPlayer::setDataSource_l(
227 const char *uri, const KeyedVector<String8, String8> *headers) {
228 reset_l();
229
230 mUri = uri;
231
232 if (headers) {
233 mUriHeaders = *headers;
234 }
235
236 // The actual work will be done during preparation in the call to
237 // ::finishSetDataSource_l to avoid blocking the calling thread in
238 // setDataSource for any significant time.
239 return OK;
240}
241
242status_t PreviewPlayer::setDataSource_l(const sp<MediaExtractor> &extractor) {
243 bool haveAudio = false;
244 bool haveVideo = false;
245 for (size_t i = 0; i < extractor->countTracks(); ++i) {
246 sp<MetaData> meta = extractor->getTrackMetaData(i);
247
248 const char *mime;
249 CHECK(meta->findCString(kKeyMIMEType, &mime));
250
251 if (!haveVideo && !strncasecmp(mime, "video/", 6)) {
252 setVideoSource(extractor->getTrack(i));
253 haveVideo = true;
254 } else if (!haveAudio && !strncasecmp(mime, "audio/", 6)) {
255 setAudioSource(extractor->getTrack(i));
256 haveAudio = true;
257
258 if (!strcasecmp(mime, MEDIA_MIMETYPE_AUDIO_VORBIS)) {
259 // Only do this for vorbis audio, none of the other audio
260 // formats even support this ringtone specific hack and
261 // retrieving the metadata on some extractors may turn out
262 // to be very expensive.
263 sp<MetaData> fileMeta = extractor->getMetaData();
264 int32_t loop;
265 if (fileMeta != NULL
266 && fileMeta->findInt32(kKeyAutoLoop, &loop)
267 && loop != 0) {
268 mFlags |= AUTO_LOOPING;
269 }
270 }
271 }
272
273 if (haveAudio && haveVideo) {
274 break;
275 }
276 }
277
278 /* Add the support for Dummy audio*/
279 if( !haveAudio ){
280 LOGV("PreviewPlayer: setDataSource_l Dummyaudiocreation started");
281
282 mAudioTrack = DummyAudioSource::Create(32000, 2, 20000,
283 ((mPlayEndTimeMsec)*1000));
284 LOGV("PreviewPlayer: setDataSource_l Dummyauiosource created");
285 if(mAudioTrack != NULL) {
286 haveAudio = true;
287 }
288 }
289
290 if (!haveAudio && !haveVideo) {
291 return UNKNOWN_ERROR;
292 }
293
294 mExtractorFlags = extractor->flags();
295 return OK;
296}
297
298status_t PreviewPlayer::setDataSource_l_jpg() {
299 M4OSA_ERR err = M4NO_ERROR;
300 LOGV("PreviewPlayer: setDataSource_l_jpg started");
301
302 mAudioSource = DummyAudioSource::Create(32000, 2, 20000,
303 ((mPlayEndTimeMsec)*1000));
304 LOGV("PreviewPlayer: setDataSource_l_jpg Dummyaudiosource created");
305 if(mAudioSource != NULL) {
306 setAudioSource(mAudioSource);
307 }
308 status_t error = mAudioSource->start();
309 if (error != OK) {
310 LOGV("Error starting dummy audio source");
311 mAudioSource.clear();
312 return err;
313 }
314
315 mDurationUs = (mPlayEndTimeMsec - mPlayBeginTimeMsec)*1000;
316
317 mVideoSource = DummyVideoSource::Create(mVideoWidth, mVideoHeight,
318 mDurationUs, mUri);
Dharmaray Kundargi35cb2de2011-01-19 19:09:27 -0800319 mReportedWidth = mVideoWidth;
320 mReportedHeight = mVideoHeight;
321
Dharmaray Kundargi643290d2011-01-16 16:02:42 -0800322 setVideoSource(mVideoSource);
323 status_t err1 = mVideoSource->start();
324 if (err1 != OK) {
325 mVideoSource.clear();
326 return err;
327 }
328
329 mIsVideoSourceJpg = true;
330 return OK;
331}
332
333void PreviewPlayer::reset() {
334 Mutex::Autolock autoLock(mLock);
335 reset_l();
336}
337
338void PreviewPlayer::reset_l() {
339
340 if (mFlags & PREPARING) {
341 mFlags |= PREPARE_CANCELLED;
342 }
343
344 while (mFlags & PREPARING) {
345 mPreparedCondition.wait(mLock);
346 }
347
348 cancelPlayerEvents();
349 mAudioTrack.clear();
350 mVideoTrack.clear();
351
352 // Shutdown audio first, so that the respone to the reset request
353 // appears to happen instantaneously as far as the user is concerned
354 // If we did this later, audio would continue playing while we
355 // shutdown the video-related resources and the player appear to
356 // not be as responsive to a reset request.
357 if (mAudioPlayer == NULL && mAudioSource != NULL) {
358 // If we had an audio player, it would have effectively
359 // taken possession of the audio source and stopped it when
360 // _it_ is stopped. Otherwise this is still our responsibility.
361 mAudioSource->stop();
362 }
363 mAudioSource.clear();
364
365 mTimeSource = NULL;
366
367 delete mAudioPlayer;
368 mAudioPlayer = NULL;
369
370 if (mLastVideoBuffer) {
371 mLastVideoBuffer->release();
372 mLastVideoBuffer = NULL;
373 }
374
375 if (mVideoBuffer) {
376 mVideoBuffer->release();
377 mVideoBuffer = NULL;
378 }
379
380 if (mVideoSource != NULL) {
381 mVideoSource->stop();
382
383 // The following hack is necessary to ensure that the OMX
384 // component is completely released by the time we may try
385 // to instantiate it again.
386 wp<MediaSource> tmp = mVideoSource;
387 mVideoSource.clear();
388 while (tmp.promote() != NULL) {
389 usleep(1000);
390 }
391 IPCThreadState::self()->flushCommands();
392 }
393
394 mDurationUs = -1;
395 mFlags = 0;
396 mExtractorFlags = 0;
397 mVideoWidth = mVideoHeight = -1;
398 mTimeSourceDeltaUs = 0;
399 mVideoTimeUs = 0;
400
401 mSeeking = false;
402 mSeekNotificationSent = false;
403 mSeekTimeUs = 0;
404
405 mUri.setTo("");
406 mUriHeaders.clear();
407
408 mFileSource.clear();
409
410 delete mSuspensionState;
411 mSuspensionState = NULL;
412
413 mCurrentVideoEffect = VIDEO_EFFECT_NONE;
414 mIsVideoSourceJpg = false;
415 mFrameRGBBuffer = NULL;
416 if(mFrameYUVBuffer != NULL) {
417 M4OSA_free((M4OSA_MemAddr32)mFrameYUVBuffer);
418 mFrameYUVBuffer = NULL;
419 }
420}
421
422void PreviewPlayer::partial_reset_l() {
423
424 if (mLastVideoBuffer) {
425 mLastVideoBuffer->release();
426 mLastVideoBuffer = NULL;
427 }
428
429 /* call base struct */
430 AwesomePlayer::partial_reset_l();
431
432}
433
434status_t PreviewPlayer::play() {
435 Mutex::Autolock autoLock(mLock);
436
437 mFlags &= ~CACHE_UNDERRUN;
438
439 return play_l();
440}
441
442status_t PreviewPlayer::play_l() {
443VideoEditorAudioPlayer *mVePlayer;
444 if (mFlags & PLAYING) {
445 return OK;
446 }
447 mStartNextPlayer = false;
448
449 if (!(mFlags & PREPARED)) {
450 status_t err = prepare_l();
451
452 if (err != OK) {
453 return err;
454 }
455 }
456
457 mFlags |= PLAYING;
458 mFlags |= FIRST_FRAME;
459
460 bool deferredAudioSeek = false;
461
462 if (mAudioSource != NULL) {
463 if (mAudioPlayer == NULL) {
464 if (mAudioSink != NULL) {
465
466 mAudioPlayer = new VideoEditorAudioPlayer(mAudioSink, this);
467 mVePlayer =
468 (VideoEditorAudioPlayer*)mAudioPlayer;
469
470 mAudioPlayer->setSource(mAudioSource);
471
472 mVePlayer->setAudioMixSettings(
473 mPreviewPlayerAudioMixSettings);
474
475 mVePlayer->setAudioMixPCMFileHandle(
476 mAudioMixPCMFileHandle);
477
478 mVePlayer->setAudioMixStoryBoardSkimTimeStamp(
479 mAudioMixStoryBoardTS, mCurrentMediaBeginCutTime,
480 mCurrentMediaVolumeValue);
481
482 // We've already started the MediaSource in order to enable
483 // the prefetcher to read its data.
484 status_t err = mVePlayer->start(
485 true /* sourceAlreadyStarted */);
486
487 if (err != OK) {
488 delete mAudioPlayer;
489 mAudioPlayer = NULL;
490
491 mFlags &= ~(PLAYING | FIRST_FRAME);
492 return err;
493 }
494
495 mTimeSource = mVePlayer; //mAudioPlayer;
496
497 deferredAudioSeek = true;
498 mWatchForAudioSeekComplete = false;
499 mWatchForAudioEOS = true;
500 }
501 } else {
502 mVePlayer->resume();
503 }
504
505 }
506
507 if (mTimeSource == NULL && mAudioPlayer == NULL) {
508 mTimeSource = &mSystemTimeSource;
509 }
510
511 if (mVideoSource != NULL) {
512 // Kick off video playback
513 postVideoEvent_l();
514 }
515
516 if (deferredAudioSeek) {
517 // If there was a seek request while we were paused
518 // and we're just starting up again, honor the request now.
519 seekAudioIfNecessary_l();
520 }
521
522 if (mFlags & AT_EOS) {
523 // Legacy behaviour, if a stream finishes playing and then
524 // is started again, we play from the start...
525 seekTo_l(0);
526 }
527
528 return OK;
529}
530
531
532void PreviewPlayer::initRenderer_l() {
533 if (mSurface != NULL || mISurface != NULL) {
534 sp<MetaData> meta = mVideoSource->getFormat();
535
536 int32_t format;
537 const char *component;
538 int32_t decodedWidth, decodedHeight;
539 CHECK(meta->findInt32(kKeyColorFormat, &format));
540 CHECK(meta->findCString(kKeyDecoderComponent, &component));
541 CHECK(meta->findInt32(kKeyWidth, &decodedWidth));
542 CHECK(meta->findInt32(kKeyHeight, &decodedHeight));
543
544 // Must ensure that mVideoRenderer's destructor is actually executed
545 // before creating a new one.
546 IPCThreadState::self()->flushCommands();
547
548 // always use localrenderer since decoded buffers are modified
549 // by postprocessing module
550 // Other decoders are instantiated locally and as a consequence
551 // allocate their buffers in local address space.
552 if(mVideoRenderer == NULL) {
553
554 mVideoRenderer = new PreviewLocalRenderer(
555 false, // previewOnly
556 (OMX_COLOR_FORMATTYPE)format,
557 mSurface,
558 mOutputVideoWidth, mOutputVideoHeight,
559 mOutputVideoWidth, mOutputVideoHeight);
560 }
561 }
562}
563
564
565void PreviewPlayer::setISurface(const sp<ISurface> &isurface) {
566 Mutex::Autolock autoLock(mLock);
567 mISurface = isurface;
568}
569
570
571status_t PreviewPlayer::seekTo(int64_t timeUs) {
572
573 if ((mExtractorFlags & MediaExtractor::CAN_SEEK) || (mIsVideoSourceJpg)) {
574 Mutex::Autolock autoLock(mLock);
575 return seekTo_l(timeUs);
576 }
577
578 return OK;
579}
580
581
582status_t PreviewPlayer::getVideoDimensions(
583 int32_t *width, int32_t *height) const {
584 Mutex::Autolock autoLock(mLock);
585
586 if (mVideoWidth < 0 || mVideoHeight < 0) {
587 return UNKNOWN_ERROR;
588 }
589
590 *width = mVideoWidth;
591 *height = mVideoHeight;
592
593 return OK;
594}
595
596
597status_t PreviewPlayer::initAudioDecoder() {
598 sp<MetaData> meta = mAudioTrack->getFormat();
599 const char *mime;
600 CHECK(meta->findCString(kKeyMIMEType, &mime));
601
602 if (!strcasecmp(mime, MEDIA_MIMETYPE_AUDIO_RAW)) {
603 mAudioSource = mAudioTrack;
604 } else {
605 sp<MediaSource> aRawSource;
606 aRawSource = OMXCodec::Create(
607 mClient.interface(), mAudioTrack->getFormat(),
608 false, // createEncoder
609 mAudioTrack);
610
611 if(aRawSource != NULL) {
612 LOGV("initAudioDecoder: new VideoEditorSRC");
613 mAudioSource = new VideoEditorSRC(aRawSource);
614 }
615 }
616
617 if (mAudioSource != NULL) {
618 int64_t durationUs;
619 if (mAudioTrack->getFormat()->findInt64(kKeyDuration, &durationUs)) {
620 Mutex::Autolock autoLock(mMiscStateLock);
621 if (mDurationUs < 0 || durationUs > mDurationUs) {
622 mDurationUs = durationUs;
623 }
624 }
625 status_t err = mAudioSource->start();
626
627 if (err != OK) {
628 mAudioSource.clear();
629 return err;
630 }
631 } else if (!strcasecmp(mime, MEDIA_MIMETYPE_AUDIO_QCELP)) {
632 // For legacy reasons we're simply going to ignore the absence
633 // of an audio decoder for QCELP instead of aborting playback
634 // altogether.
635 return OK;
636 }
637
638 return mAudioSource != NULL ? OK : UNKNOWN_ERROR;
639}
640
641
642status_t PreviewPlayer::initVideoDecoder(uint32_t flags) {
643
644 mVideoSource = OMXCodec::Create(
645 mClient.interface(), mVideoTrack->getFormat(),
646 false,
647 mVideoTrack,
648 NULL, flags);
649
650 if (mVideoSource != NULL) {
651 int64_t durationUs;
652 if (mVideoTrack->getFormat()->findInt64(kKeyDuration, &durationUs)) {
653 Mutex::Autolock autoLock(mMiscStateLock);
654 if (mDurationUs < 0 || durationUs > mDurationUs) {
655 mDurationUs = durationUs;
656 }
657 }
658
659 CHECK(mVideoTrack->getFormat()->findInt32(kKeyWidth, &mVideoWidth));
660 CHECK(mVideoTrack->getFormat()->findInt32(kKeyHeight, &mVideoHeight));
661
Dharmaray Kundargi35cb2de2011-01-19 19:09:27 -0800662 mReportedWidth = mVideoWidth;
663 mReportedHeight = mVideoHeight;
664
Dharmaray Kundargi643290d2011-01-16 16:02:42 -0800665 status_t err = mVideoSource->start();
666
667 if (err != OK) {
668 mVideoSource.clear();
669 return err;
670 }
671 }
672
673 return mVideoSource != NULL ? OK : UNKNOWN_ERROR;
674}
675
676
677void PreviewPlayer::onVideoEvent() {
678 uint32_t i=0;
679 bool bAppliedVideoEffect = false;
680 M4OSA_ERR err1 = M4NO_ERROR;
681 int64_t imageFrameTimeUs = 0;
682
683 Mutex::Autolock autoLock(mLock);
684 if (!mVideoEventPending) {
685 // The event has been cancelled in reset_l() but had already
686 // been scheduled for execution at that time.
687 return;
688 }
689 mVideoEventPending = false;
690
691 TimeSource *ts_st = &mSystemTimeSource;
692 int64_t timeStartUs = ts_st->getRealTimeUs();
693
694 if (mSeeking) {
695 if (mLastVideoBuffer) {
696 mLastVideoBuffer->release();
697 mLastVideoBuffer = NULL;
698 }
699
700
701 if(mAudioSource != NULL) {
702
703 // We're going to seek the video source first, followed by
704 // the audio source.
705 // In order to avoid jumps in the DataSource offset caused by
706 // the audio codec prefetching data from the old locations
707 // while the video codec is already reading data from the new
708 // locations, we'll "pause" the audio source, causing it to
709 // stop reading input data until a subsequent seek.
710
711 if (mAudioPlayer != NULL) {
712 mAudioPlayer->pause();
713 }
714 mAudioSource->pause();
715 }
716 }
717
718 if (!mVideoBuffer) {
719 MediaSource::ReadOptions options;
720 if (mSeeking) {
721 LOGV("LV PLAYER seeking to %lld us (%.2f secs)", mSeekTimeUs,
722 mSeekTimeUs / 1E6);
723
724 options.setSeekTo(
725 mSeekTimeUs, MediaSource::ReadOptions::SEEK_CLOSEST);
726 }
727 for (;;) {
728 status_t err = mVideoSource->read(&mVideoBuffer, &options);
729 options.clearSeekTo();
730
731 if (err != OK) {
732 CHECK_EQ(mVideoBuffer, NULL);
733
734 if (err == INFO_FORMAT_CHANGED) {
735 LOGV("LV PLAYER VideoSource signalled format change");
736 notifyVideoSize_l();
Dharmaray Kundargi35cb2de2011-01-19 19:09:27 -0800737 sp<MetaData> meta = mVideoSource->getFormat();
Dharmaray Kundargi643290d2011-01-16 16:02:42 -0800738
Dharmaray Kundargi35cb2de2011-01-19 19:09:27 -0800739 CHECK(meta->findInt32(kKeyWidth, &mReportedWidth));
740 CHECK(meta->findInt32(kKeyHeight, &mReportedHeight));
Dharmaray Kundargi643290d2011-01-16 16:02:42 -0800741 if (mVideoRenderer != NULL) {
742 mVideoRendererIsPreview = false;
743 initRenderer_l();
744 }
745 continue;
746 }
747 // So video playback is complete, but we may still have
748 // a seek request pending that needs to be applied // to the audio track. if (mSeeking) { LOGV("video stream ended while seeking!"); } finishSeekIfNecessary(-1);
749 LOGV("PreviewPlayer: onVideoEvent EOS reached.");
750 mFlags |= VIDEO_AT_EOS;
751 postStreamDoneEvent_l(err);
752 return;
753 }
754
755 if (mVideoBuffer->range_length() == 0) {
756 // Some decoders, notably the PV AVC software decoder
757 // return spurious empty buffers that we just want to ignore.
758
759 mVideoBuffer->release();
760 mVideoBuffer = NULL;
761 continue;
762 }
763
764 int64_t videoTimeUs;
765 CHECK(mVideoBuffer->meta_data()->findInt64(kKeyTime, &videoTimeUs));
766
767 if((videoTimeUs/1000) < mPlayBeginTimeMsec) {
768 // Frames are before begin cut time
769 // Donot render
770 mVideoBuffer->release();
771 mVideoBuffer = NULL;
772 continue;
773 }
774
775 break;
776 }
777 }
778
779 mNumberDecVideoFrames++;
780
781 int64_t timeUs;
782 CHECK(mVideoBuffer->meta_data()->findInt64(kKeyTime, &timeUs));
783
784 {
785 Mutex::Autolock autoLock(mMiscStateLock);
786 mVideoTimeUs = timeUs;
787 }
788
789 mDecodedVideoTs = timeUs;
790
791 if(!mStartNextPlayer) {
792 int64_t playbackTimeRemaining = (mPlayEndTimeMsec*1000) - timeUs;
793 if(playbackTimeRemaining <= 1500000) {
794 //When less than 1.5 sec of playback left
795 // send notification to start next player
796
797 mStartNextPlayer = true;
798 notifyListener_l(0xAAAAAAAA);
799 }
800 }
801
802 bool wasSeeking = mSeeking;
803 finishSeekIfNecessary(timeUs);
804
805 TimeSource *ts = (mFlags & AUDIO_AT_EOS) ? &mSystemTimeSource : mTimeSource;
806
807 if(ts == NULL) {
808 mVideoBuffer->release();
809 mVideoBuffer = NULL;
810 return;
811 }
812
813 if(!mIsVideoSourceJpg) {
814 if (mFlags & FIRST_FRAME) {
815 mFlags &= ~FIRST_FRAME;
816
817 mTimeSourceDeltaUs = ts->getRealTimeUs() - timeUs;
818 }
819
820 int64_t realTimeUs, mediaTimeUs;
821 if (!(mFlags & AUDIO_AT_EOS) && mAudioPlayer != NULL
822 && mAudioPlayer->getMediaTimeMapping(&realTimeUs, &mediaTimeUs)) {
823 mTimeSourceDeltaUs = realTimeUs - mediaTimeUs;
824 }
825
826 int64_t nowUs = ts->getRealTimeUs() - mTimeSourceDeltaUs;
827
828 int64_t latenessUs = nowUs - timeUs;
829
830 if (wasSeeking) {
831 // Let's display the first frame after seeking right away. latenessUs = 0; }
832 LOGV("Audio time stamp = %lld and video time stamp = %lld",
833 ts->getRealTimeUs(),timeUs);
834 if (latenessUs > 40000) {
835 // We're more than 40ms late.
836
837 LOGV("LV PLAYER we're late by %lld us (%.2f secs)",
838 latenessUs, latenessUs / 1E6);
839
840 mVideoBuffer->release();
841 mVideoBuffer = NULL;
842
843 postVideoEvent_l();
844 return;
845 }
846
847 if (latenessUs < -10000) {
848 // We're more than 10ms early.
849 LOGV("We're more than 10ms early, lateness %lld", latenessUs);
850
851 postVideoEvent_l(10000);
852 return;
853 }
854 }
855
856 if (mVideoRendererIsPreview || mVideoRenderer == NULL) {
857 mVideoRendererIsPreview = false;
858
859 initRenderer_l();
860 }
861
862 // If timestamp exceeds endCutTime of clip, donot render
863 if((timeUs/1000) > mPlayEndTimeMsec) {
864 if (mLastVideoBuffer) {
865 mLastVideoBuffer->release();
866 mLastVideoBuffer = NULL;
867 }
868 mLastVideoBuffer = mVideoBuffer;
869 mVideoBuffer = NULL;
870 mFlags |= VIDEO_AT_EOS;
871 mFlags |= AUDIO_AT_EOS;
872 LOGI("PreviewPlayer: onVideoEvent timeUs > mPlayEndTime; send EOS..");
Dharmaray Kundargie6c07502011-01-21 16:58:31 -0800873 if (mOverlayUpdateEventPosted) {
874 mOverlayUpdateEventPosted = false;
875 postOverlayUpdateEvent_l();
876 }
Dharmaray Kundargi643290d2011-01-16 16:02:42 -0800877 postStreamDoneEvent_l(ERROR_END_OF_STREAM);
878 return;
879 }
880
881 // Post processing to apply video effects
882 for(i=0;i<mNumberEffects;i++) {
883 // First check if effect starttime matches the clip being previewed
884 if((mEffectsSettings[i].uiStartTime < (mDecVideoTsStoryBoard/1000)) ||
885 (mEffectsSettings[i].uiStartTime >=
886 ((mDecVideoTsStoryBoard/1000) + mPlayEndTimeMsec - mPlayBeginTimeMsec)))
887 {
888 // This effect doesn't belong to this clip, check next one
889 continue;
890 }
891 // Check if effect applies to this particular frame timestamp
892 if((mEffectsSettings[i].uiStartTime <=
893 (((timeUs+mDecVideoTsStoryBoard)/1000)-mPlayBeginTimeMsec)) &&
894 ((mEffectsSettings[i].uiStartTime+mEffectsSettings[i].uiDuration) >=
895 (((timeUs+mDecVideoTsStoryBoard)/1000)-mPlayBeginTimeMsec))
896 && (mEffectsSettings[i].uiDuration != 0)) {
897
898 setVideoPostProcessingNode(
899 mEffectsSettings[i].VideoEffectType, TRUE);
900 }
901 else {
902 setVideoPostProcessingNode(
903 mEffectsSettings[i].VideoEffectType, FALSE);
904 }
Dharmaray Kundargi643290d2011-01-16 16:02:42 -0800905 }
906
Dharmaray Kundargie6c07502011-01-21 16:58:31 -0800907 //Provide the overlay Update indication when there is an overlay effect
Dharmaray Kundargid01ef562011-01-26 21:11:00 -0800908 if (mCurrentVideoEffect & VIDEO_EFFECT_FRAMING) {
909 mCurrentVideoEffect &= ~VIDEO_EFFECT_FRAMING; //never apply framing here.
Dharmaray Kundargie6c07502011-01-21 16:58:31 -0800910 if (!mOverlayUpdateEventPosted) {
911
912 // Find the effect in effectSettings array
913 int index;
914 for (index = 0; index < mNumberEffects; index++) {
915 M4OSA_UInt32 timeMs = mDecodedVideoTs/1000;
916 M4OSA_UInt32 timeOffset = mDecVideoTsStoryBoard/1000;
917 if(mEffectsSettings[index].VideoEffectType ==
918 M4xVSS_kVideoEffectType_Framing) {
919 if (((mEffectsSettings[index].uiStartTime + 1) <= timeMs + timeOffset) &&
920 ((mEffectsSettings[index].uiStartTime - 1 +
921 mEffectsSettings[index].uiDuration) >= timeMs + timeOffset))
922 {
923 break;
924 }
925 }
926 }
927 if (index < mNumberEffects) {
928 mCurrFramingEffectIndex = index;
929 mOverlayUpdateEventPosted = true;
930 postOverlayUpdateEvent_l();
931 LOGV("Framing index = %d", mCurrFramingEffectIndex);
932 } else {
933 LOGV("No framing effects found");
934 }
935 }
936
937 } else if (mOverlayUpdateEventPosted) {
938 //Post the event when the overlay is no more valid
939 LOGV("Overlay is Done");
940 mOverlayUpdateEventPosted = false;
941 postOverlayUpdateEvent_l();
942 }
943
944
945 if (mCurrentVideoEffect != VIDEO_EFFECT_NONE) {
Dharmaray Kundargi643290d2011-01-16 16:02:42 -0800946 err1 = doVideoPostProcessing();
947 if(err1 != M4NO_ERROR) {
948 LOGE("doVideoPostProcessing returned err");
949 bAppliedVideoEffect = false;
950 }
951 else {
952 bAppliedVideoEffect = true;
953 }
954 }
955 else {
956 bAppliedVideoEffect = false;
957 if(mRenderingMode != MEDIA_RENDERING_INVALID) {
958 // No effects to be applied, but media rendering to be done
959 err1 = doMediaRendering();
960 if(err1 != M4NO_ERROR) {
961 LOGE("doMediaRendering returned err");
962 //Use original mVideoBuffer for rendering
963 mVideoResizedOrCropped = false;
964 }
965 }
966 }
967
968 if (mVideoRenderer != NULL) {
969 LOGV("mVideoRenderer CALL render()");
970 mVideoRenderer->render();
971 }
972
973 if (mLastVideoBuffer) {
974 mLastVideoBuffer->release();
975 mLastVideoBuffer = NULL;
976 }
977
978 mLastVideoBuffer = mVideoBuffer;
979 mVideoBuffer = NULL;
980
981 // Post progress callback based on callback interval set
982 if(mNumberDecVideoFrames >= mProgressCbInterval) {
983 postProgressCallbackEvent_l();
984 mNumberDecVideoFrames = 0; // reset counter
985 }
986
987 // if reached EndCutTime of clip, post EOS event
988 if((timeUs/1000) >= mPlayEndTimeMsec) {
989 LOGV("PreviewPlayer: onVideoEvent EOS.");
990 mFlags |= VIDEO_AT_EOS;
991 mFlags |= AUDIO_AT_EOS;
992 postStreamDoneEvent_l(ERROR_END_OF_STREAM);
993 }
994 else {
995 if(!mIsVideoSourceJpg) {
996 postVideoEvent_l();
997 }
998 else {
999 postVideoEvent_l(33000);
1000 }
1001 }
1002}
1003
1004status_t PreviewPlayer::prepare() {
1005 Mutex::Autolock autoLock(mLock);
1006 return prepare_l();
1007}
1008
1009status_t PreviewPlayer::prepare_l() {
1010 if (mFlags & PREPARED) {
1011 return OK;
1012 }
1013
1014 if (mFlags & PREPARING) {
1015 return UNKNOWN_ERROR;
1016 }
1017
1018 mIsAsyncPrepare = false;
1019 status_t err = prepareAsync_l();
1020
1021 if (err != OK) {
1022 return err;
1023 }
1024
1025 while (mFlags & PREPARING) {
1026 mPreparedCondition.wait(mLock);
1027 }
1028
1029 return mPrepareResult;
1030}
1031
1032status_t PreviewPlayer::prepareAsync_l() {
1033 if (mFlags & PREPARING) {
1034 return UNKNOWN_ERROR; // async prepare already pending
1035 }
1036
1037 if (!mQueueStarted) {
1038 mQueue.start();
1039 mQueueStarted = true;
1040 }
1041
1042 mFlags |= PREPARING;
1043 mAsyncPrepareEvent = new PreviewPlayerEvent(
1044 this, &PreviewPlayer::onPrepareAsyncEvent);
1045
1046 mQueue.postEvent(mAsyncPrepareEvent);
1047
1048 return OK;
1049}
1050
1051status_t PreviewPlayer::finishSetDataSource_l() {
1052 sp<DataSource> dataSource;
1053 sp<MediaExtractor> extractor;
1054
1055 dataSource = DataSource::CreateFromURI(mUri.string(), &mUriHeaders);
1056
1057 if (dataSource == NULL) {
1058 return UNKNOWN_ERROR;
1059 }
1060
1061 //If file type is .rgb, then no need to check for Extractor
1062 int uriLen = strlen(mUri);
1063 int startOffset = uriLen - 4;
1064 if(!strncasecmp(mUri+startOffset, ".rgb", 4)) {
1065 extractor = NULL;
1066 }
1067 else {
1068 extractor = MediaExtractor::Create(dataSource,
1069 MEDIA_MIMETYPE_CONTAINER_MPEG4);
1070 }
1071
1072 if (extractor == NULL) {
1073 LOGV("PreviewPlayer::finishSetDataSource_l extractor == NULL");
1074 return setDataSource_l_jpg();
1075 }
1076
1077 return setDataSource_l(extractor);
1078}
1079
1080
1081// static
1082bool PreviewPlayer::ContinuePreparation(void *cookie) {
1083 PreviewPlayer *me = static_cast<PreviewPlayer *>(cookie);
1084
1085 return (me->mFlags & PREPARE_CANCELLED) == 0;
1086}
1087
1088void PreviewPlayer::onPrepareAsyncEvent() {
1089 Mutex::Autolock autoLock(mLock);
1090 LOGV("onPrepareAsyncEvent");
1091
1092 if (mFlags & PREPARE_CANCELLED) {
1093 LOGI("LV PLAYER prepare was cancelled before doing anything");
1094 abortPrepare(UNKNOWN_ERROR);
1095 return;
1096 }
1097
1098 if (mUri.size() > 0) {
1099 status_t err = finishSetDataSource_l();
1100
1101 if (err != OK) {
1102 abortPrepare(err);
1103 return;
1104 }
1105 }
1106
1107 if (mVideoTrack != NULL && mVideoSource == NULL) {
1108 status_t err = initVideoDecoder(OMXCodec::kHardwareCodecsOnly);
1109
1110 if (err != OK) {
1111 abortPrepare(err);
1112 return;
1113 }
1114 }
1115
1116 if (mAudioTrack != NULL && mAudioSource == NULL) {
1117 status_t err = initAudioDecoder();
1118
1119 if (err != OK) {
1120 abortPrepare(err);
1121 return;
1122 }
1123 }
1124 finishAsyncPrepare_l();
1125
1126}
1127
1128void PreviewPlayer::finishAsyncPrepare_l() {
1129 if (mIsAsyncPrepare) {
1130 if (mVideoSource == NULL) {
1131 LOGV("finishAsyncPrepare_l: MEDIA_SET_VIDEO_SIZE 0 0 ");
1132 notifyListener_l(MEDIA_SET_VIDEO_SIZE, 0, 0);
1133 } else {
1134 LOGV("finishAsyncPrepare_l: MEDIA_SET_VIDEO_SIZE");
1135 notifyVideoSize_l();
1136 }
1137 LOGV("finishAsyncPrepare_l: MEDIA_PREPARED");
1138 notifyListener_l(MEDIA_PREPARED);
1139 }
1140
1141 mPrepareResult = OK;
1142 mFlags &= ~(PREPARING|PREPARE_CANCELLED);
1143 mFlags |= PREPARED;
1144 mAsyncPrepareEvent = NULL;
1145 mPreparedCondition.broadcast();
1146}
1147
1148status_t PreviewPlayer::suspend() {
1149 LOGV("suspend");
1150 Mutex::Autolock autoLock(mLock);
1151
1152 if (mSuspensionState != NULL) {
1153 if (mLastVideoBuffer == NULL) {
1154 //go into here if video is suspended again
1155 //after resuming without being played between
1156 //them
1157 SuspensionState *state = mSuspensionState;
1158 mSuspensionState = NULL;
1159 reset_l();
1160 mSuspensionState = state;
1161 return OK;
1162 }
1163
1164 delete mSuspensionState;
1165 mSuspensionState = NULL;
1166 }
1167
1168 if (mFlags & PREPARING) {
1169 mFlags |= PREPARE_CANCELLED;
1170 }
1171
1172 while (mFlags & PREPARING) {
1173 mPreparedCondition.wait(mLock);
1174 }
1175
1176 SuspensionState *state = new SuspensionState;
1177 state->mUri = mUri;
1178 state->mUriHeaders = mUriHeaders;
1179 state->mFileSource = mFileSource;
1180
1181 state->mFlags = mFlags & (PLAYING | AUTO_LOOPING | LOOPING | AT_EOS);
1182 getPosition(&state->mPositionUs);
1183
1184 if (mLastVideoBuffer) {
1185 size_t size = mLastVideoBuffer->range_length();
1186 if (size) {
1187 int32_t unreadable;
1188 if (!mLastVideoBuffer->meta_data()->findInt32(
1189 kKeyIsUnreadable, &unreadable)
1190 || unreadable == 0) {
1191 state->mLastVideoFrameSize = size;
1192 state->mLastVideoFrame = malloc(size);
1193 memcpy(state->mLastVideoFrame,
1194 (const uint8_t *)mLastVideoBuffer->data()
1195 + mLastVideoBuffer->range_offset(),
1196 size);
1197
1198 state->mVideoWidth = mVideoWidth;
1199 state->mVideoHeight = mVideoHeight;
1200
1201 sp<MetaData> meta = mVideoSource->getFormat();
1202 CHECK(meta->findInt32(kKeyColorFormat, &state->mColorFormat));
1203 CHECK(meta->findInt32(kKeyWidth, &state->mDecodedWidth));
1204 CHECK(meta->findInt32(kKeyHeight, &state->mDecodedHeight));
1205 } else {
1206 LOGV("Unable to save last video frame, we have no access to "
1207 "the decoded video data.");
1208 }
1209 }
1210 }
1211
1212 reset_l();
1213
1214 mSuspensionState = state;
1215
1216 return OK;
1217}
1218
1219status_t PreviewPlayer::resume() {
1220 LOGV("resume");
1221 Mutex::Autolock autoLock(mLock);
1222
1223 if (mSuspensionState == NULL) {
1224 return INVALID_OPERATION;
1225 }
1226
1227 SuspensionState *state = mSuspensionState;
1228 mSuspensionState = NULL;
1229
1230 status_t err;
1231 if (state->mFileSource != NULL) {
1232 err = AwesomePlayer::setDataSource_l(state->mFileSource);
1233
1234 if (err == OK) {
1235 mFileSource = state->mFileSource;
1236 }
1237 } else {
1238 err = AwesomePlayer::setDataSource_l(state->mUri, &state->mUriHeaders);
1239 }
1240
1241 if (err != OK) {
1242 delete state;
1243 state = NULL;
1244
1245 return err;
1246 }
1247
1248 seekTo_l(state->mPositionUs);
1249
1250 mFlags = state->mFlags & (AUTO_LOOPING | LOOPING | AT_EOS);
1251
1252 if (state->mLastVideoFrame && (mSurface != NULL || mISurface != NULL)) {
1253 mVideoRenderer =
1254 new PreviewLocalRenderer(
1255 true, // previewOnly
1256 (OMX_COLOR_FORMATTYPE)state->mColorFormat,
1257 mSurface,
1258 state->mVideoWidth,
1259 state->mVideoHeight,
1260 state->mDecodedWidth,
1261 state->mDecodedHeight);
1262
1263 mVideoRendererIsPreview = true;
1264
1265 ((PreviewLocalRenderer *)mVideoRenderer.get())->render(
1266 state->mLastVideoFrame, state->mLastVideoFrameSize);
1267 }
1268
1269 if (state->mFlags & PLAYING) {
1270 play_l();
1271 }
1272
1273 mSuspensionState = state;
1274 state = NULL;
1275
1276 return OK;
1277}
1278
1279
1280status_t PreviewPlayer::loadEffectsSettings(
1281 M4VSS3GPP_EffectSettings* pEffectSettings, int nEffects) {
1282 M4OSA_UInt32 i = 0, rgbSize = 0;
1283 M4VIFI_UInt8 *tmp = M4OSA_NULL;
1284
1285 mNumberEffects = nEffects;
1286 mEffectsSettings = pEffectSettings;
1287 return OK;
1288}
1289
1290status_t PreviewPlayer::loadAudioMixSettings(
1291 M4xVSS_AudioMixingSettings* pAudioMixSettings) {
1292
1293 LOGV("PreviewPlayer: loadAudioMixSettings: ");
1294 mPreviewPlayerAudioMixSettings = pAudioMixSettings;
1295 return OK;
1296}
1297
1298status_t PreviewPlayer::setAudioMixPCMFileHandle(
1299 M4OSA_Context pAudioMixPCMFileHandle) {
1300
1301 LOGV("PreviewPlayer: setAudioMixPCMFileHandle: ");
1302 mAudioMixPCMFileHandle = pAudioMixPCMFileHandle;
1303 return OK;
1304}
1305
1306status_t PreviewPlayer::setAudioMixStoryBoardParam(
1307 M4OSA_UInt32 audioMixStoryBoardTS,
1308 M4OSA_UInt32 currentMediaBeginCutTime,
1309 M4OSA_UInt32 primaryTrackVolValue ) {
1310
1311 mAudioMixStoryBoardTS = audioMixStoryBoardTS;
1312 mCurrentMediaBeginCutTime = currentMediaBeginCutTime;
1313 mCurrentMediaVolumeValue = primaryTrackVolValue;
1314 return OK;
1315}
1316
1317status_t PreviewPlayer::setPlaybackBeginTime(uint32_t msec) {
1318
1319 mPlayBeginTimeMsec = msec;
1320 return OK;
1321}
1322
1323status_t PreviewPlayer::setPlaybackEndTime(uint32_t msec) {
1324
1325 mPlayEndTimeMsec = msec;
1326 return OK;
1327}
1328
1329status_t PreviewPlayer::setStoryboardStartTime(uint32_t msec) {
1330
1331 mStoryboardStartTimeMsec = msec;
1332 mDecVideoTsStoryBoard = mStoryboardStartTimeMsec*1000;
1333 return OK;
1334}
1335
1336status_t PreviewPlayer::setProgressCallbackInterval(uint32_t cbInterval) {
1337
1338 mProgressCbInterval = cbInterval;
1339 return OK;
1340}
1341
1342
1343status_t PreviewPlayer::setMediaRenderingMode(
1344 M4xVSS_MediaRendering mode,
1345 M4VIDEOEDITING_VideoFrameSize outputVideoSize) {
1346
1347 mRenderingMode = mode;
1348
1349 /* reset boolean for each clip*/
1350 mVideoResizedOrCropped = false;
1351
1352 switch(outputVideoSize) {
1353 case M4VIDEOEDITING_kSQCIF:
1354 mOutputVideoWidth = 128;
1355 mOutputVideoHeight = 96;
1356 break;
1357
1358 case M4VIDEOEDITING_kQQVGA:
1359 mOutputVideoWidth = 160;
1360 mOutputVideoHeight = 120;
1361 break;
1362
1363 case M4VIDEOEDITING_kQCIF:
1364 mOutputVideoWidth = 176;
1365 mOutputVideoHeight = 144;
1366 break;
1367
1368 case M4VIDEOEDITING_kQVGA:
1369 mOutputVideoWidth = 320;
1370 mOutputVideoHeight = 240;
1371 break;
1372
1373 case M4VIDEOEDITING_kCIF:
1374 mOutputVideoWidth = 352;
1375 mOutputVideoHeight = 288;
1376 break;
1377
1378 case M4VIDEOEDITING_kVGA:
1379 mOutputVideoWidth = 640;
1380 mOutputVideoHeight = 480;
1381 break;
1382
1383 case M4VIDEOEDITING_kWVGA:
1384 mOutputVideoWidth = 800;
1385 mOutputVideoHeight = 480;
1386 break;
1387
1388 case M4VIDEOEDITING_kNTSC:
1389 mOutputVideoWidth = 720;
1390 mOutputVideoHeight = 480;
1391 break;
1392
1393 case M4VIDEOEDITING_k640_360:
1394 mOutputVideoWidth = 640;
1395 mOutputVideoHeight = 360;
1396 break;
1397
1398 case M4VIDEOEDITING_k854_480:
1399 mOutputVideoWidth = 854;
1400 mOutputVideoHeight = 480;
1401 break;
1402
1403 case M4VIDEOEDITING_kHD1280:
1404 mOutputVideoWidth = 1280;
1405 mOutputVideoHeight = 720;
1406 break;
1407
1408 case M4VIDEOEDITING_kHD1080:
1409 mOutputVideoWidth = 1080;
1410 mOutputVideoHeight = 720;
1411 break;
1412
1413 case M4VIDEOEDITING_kHD960:
1414 mOutputVideoWidth = 960;
1415 mOutputVideoHeight = 720;
1416 break;
1417
1418 default:
1419 LOGE("unsupported output video size set");
1420 return BAD_VALUE;
1421 }
1422
1423 return OK;
1424}
1425
1426M4OSA_ERR PreviewPlayer::doMediaRendering() {
1427 M4OSA_ERR err = M4NO_ERROR;
1428 M4VIFI_ImagePlane planeIn[3], planeOut[3];
1429 M4VIFI_UInt8 *inBuffer = M4OSA_NULL, *finalOutputBuffer = M4OSA_NULL;
1430 M4VIFI_UInt8 *tempOutputBuffer= M4OSA_NULL;
1431 size_t videoBufferSize = 0;
1432 M4OSA_UInt32 frameSize = 0, i=0, index =0, nFrameCount =0, bufferOffset =0;
1433 int32_t colorFormat = 0;
1434
1435 if(!mIsVideoSourceJpg) {
1436 sp<MetaData> meta = mVideoSource->getFormat();
1437 CHECK(meta->findInt32(kKeyColorFormat, &colorFormat));
1438 }
1439 else {
1440 colorFormat = OMX_COLOR_FormatYUV420Planar;
1441 }
1442
1443 videoBufferSize = mVideoBuffer->size();
1444 frameSize = (mVideoWidth*mVideoHeight*3) >> 1;
1445
1446 uint8_t* outBuffer;
1447 size_t outBufferStride = 0;
1448
1449 mVideoRenderer->getBuffer(&outBuffer, &outBufferStride);
1450
1451 bufferOffset = index*frameSize;
1452 inBuffer = (M4OSA_UInt8 *)mVideoBuffer->data()+
1453 mVideoBuffer->range_offset()+bufferOffset;
1454
1455
1456 /* In plane*/
1457 prepareYUV420ImagePlane(planeIn, mVideoWidth,
Dharmaray Kundargi35cb2de2011-01-19 19:09:27 -08001458 mVideoHeight, (M4VIFI_UInt8 *)inBuffer, mReportedWidth, mReportedHeight);
Dharmaray Kundargi643290d2011-01-16 16:02:42 -08001459
1460 // Set the output YUV420 plane to be compatible with YV12 format
1461 // W & H even
1462 // YVU instead of YUV
1463 // align buffers on 32 bits
1464
1465 //In YV12 format, sizes must be even
1466 M4OSA_UInt32 yv12PlaneWidth = ((mOutputVideoWidth +1)>>1)<<1;
1467 M4OSA_UInt32 yv12PlaneHeight = ((mOutputVideoHeight+1)>>1)<<1;
1468
1469 prepareYV12ImagePlane(planeOut, yv12PlaneWidth, yv12PlaneHeight,
1470 (M4OSA_UInt32)outBufferStride, (M4VIFI_UInt8 *)outBuffer);
1471
1472
1473 err = applyRenderingMode(planeIn, planeOut, mRenderingMode);
1474
1475 if(err != M4NO_ERROR)
1476 {
1477 LOGE("doMediaRendering: applyRenderingMode returned err=0x%x", err);
1478 return err;
1479 }
1480 mVideoResizedOrCropped = true;
1481
1482 return err;
1483}
1484
1485status_t PreviewPlayer::resetJniCallbackTimeStamp() {
1486
1487 mDecVideoTsStoryBoard = mStoryboardStartTimeMsec*1000;
1488 return OK;
1489}
1490
1491void PreviewPlayer::postProgressCallbackEvent_l() {
1492 if (mProgressCbEventPending) {
1493 return;
1494 }
1495 mProgressCbEventPending = true;
1496
1497 mQueue.postEvent(mProgressCbEvent);
1498}
1499
Dharmaray Kundargie6c07502011-01-21 16:58:31 -08001500
Dharmaray Kundargi643290d2011-01-16 16:02:42 -08001501void PreviewPlayer::onProgressCbEvent() {
1502 Mutex::Autolock autoLock(mLock);
1503 if (!mProgressCbEventPending) {
1504 return;
1505 }
1506 mProgressCbEventPending = false;
1507 // If playback starts from previous I-frame,
1508 // then send frame storyboard duration
1509 if((mDecodedVideoTs/1000) < mPlayBeginTimeMsec) {
1510 notifyListener_l(MEDIA_INFO, 0, mDecVideoTsStoryBoard/1000);
1511 }
1512 else {
1513 notifyListener_l(MEDIA_INFO, 0,
1514 (((mDecodedVideoTs+mDecVideoTsStoryBoard)/1000)-mPlayBeginTimeMsec));
1515 }
1516}
1517
Dharmaray Kundargie6c07502011-01-21 16:58:31 -08001518void PreviewPlayer::postOverlayUpdateEvent_l() {
1519 if (mOverlayUpdateEventPending) {
1520 return;
1521 }
1522 mOverlayUpdateEventPending = true;
1523 mQueue.postEvent(mOverlayUpdateEvent);
1524}
1525
1526void PreviewPlayer::onUpdateOverlayEvent() {
1527 Mutex::Autolock autoLock(mLock);
1528
1529 if (!mOverlayUpdateEventPending) {
1530 return;
1531 }
1532 mOverlayUpdateEventPending = false;
1533
1534 int updateState;
1535 if (mOverlayUpdateEventPosted) {
1536 updateState = 1;
1537 } else {
1538 updateState = 0;
1539 }
1540 notifyListener_l(0xBBBBBBBB, updateState, mCurrFramingEffectIndex);
1541}
1542
1543
Dharmaray Kundargi643290d2011-01-16 16:02:42 -08001544void PreviewPlayer::setVideoPostProcessingNode(
1545 M4VSS3GPP_VideoEffectType type, M4OSA_Bool enable) {
1546
1547 uint32_t effect = VIDEO_EFFECT_NONE;
1548
1549 //Map M4VSS3GPP_VideoEffectType to local enum
1550 switch(type) {
1551 case M4VSS3GPP_kVideoEffectType_FadeFromBlack:
1552 effect = VIDEO_EFFECT_FADEFROMBLACK;
1553 break;
1554
1555 case M4VSS3GPP_kVideoEffectType_FadeToBlack:
1556 effect = VIDEO_EFFECT_FADETOBLACK;
1557 break;
1558
1559 case M4VSS3GPP_kVideoEffectType_CurtainOpening:
1560 effect = VIDEO_EFFECT_CURTAINOPEN;
1561 break;
1562
1563 case M4VSS3GPP_kVideoEffectType_CurtainClosing:
1564 effect = VIDEO_EFFECT_CURTAINCLOSE;
1565 break;
1566
1567 case M4xVSS_kVideoEffectType_BlackAndWhite:
1568 effect = VIDEO_EFFECT_BLACKANDWHITE;
1569 break;
1570
1571 case M4xVSS_kVideoEffectType_Pink:
1572 effect = VIDEO_EFFECT_PINK;
1573 break;
1574
1575 case M4xVSS_kVideoEffectType_Green:
1576 effect = VIDEO_EFFECT_GREEN;
1577 break;
1578
1579 case M4xVSS_kVideoEffectType_Sepia:
1580 effect = VIDEO_EFFECT_SEPIA;
1581 break;
1582
1583 case M4xVSS_kVideoEffectType_Negative:
1584 effect = VIDEO_EFFECT_NEGATIVE;
1585 break;
1586
1587 case M4xVSS_kVideoEffectType_Framing:
1588 effect = VIDEO_EFFECT_FRAMING;
1589 break;
1590
1591 case M4xVSS_kVideoEffectType_Fifties:
1592 effect = VIDEO_EFFECT_FIFTIES;
1593 break;
1594
1595 case M4xVSS_kVideoEffectType_ColorRGB16:
1596 effect = VIDEO_EFFECT_COLOR_RGB16;
1597 break;
1598
1599 case M4xVSS_kVideoEffectType_Gradient:
1600 effect = VIDEO_EFFECT_GRADIENT;
1601 break;
1602
1603 default:
1604 effect = VIDEO_EFFECT_NONE;
1605 break;
1606 }
1607
1608 if(enable == M4OSA_TRUE) {
1609 //If already set, then no need to set again
1610 if(!(mCurrentVideoEffect & effect)) {
1611 mCurrentVideoEffect |= effect;
1612 if(effect == VIDEO_EFFECT_FIFTIES) {
1613 mIsFiftiesEffectStarted = true;
1614 }
1615 }
1616 }
1617 else {
1618 //Reset only if already set
1619 if(mCurrentVideoEffect & effect) {
1620 mCurrentVideoEffect &= ~effect;
1621 }
1622 }
1623}
1624
1625status_t PreviewPlayer::setImageClipProperties(uint32_t width,uint32_t height) {
1626 mVideoWidth = width;
1627 mVideoHeight = height;
1628 return OK;
1629}
1630
1631
1632M4OSA_ERR PreviewPlayer::doVideoPostProcessing() {
1633 M4OSA_ERR err = M4NO_ERROR;
1634 vePostProcessParams postProcessParams;
1635 int32_t colorFormat = 0;
1636
1637
1638 if(!mIsVideoSourceJpg) {
1639 sp<MetaData> meta = mVideoSource->getFormat();
1640 CHECK(meta->findInt32(kKeyColorFormat, &colorFormat));
1641 }
1642 else {
1643 colorFormat = OMX_COLOR_FormatYUV420Planar;
1644 }
1645
1646 if((colorFormat == OMX_COLOR_FormatYUV420SemiPlanar) ||
1647 (colorFormat == 0x7FA30C00)) {
1648 LOGE("doVideoPostProcessing: colorFormat YUV420Sp not supported");
1649 return M4ERR_UNSUPPORTED_MEDIA_TYPE;
1650 }
1651
1652 postProcessParams.vidBuffer = (M4VIFI_UInt8*)mVideoBuffer->data()
1653 + mVideoBuffer->range_offset();
1654
1655 postProcessParams.videoWidth = mVideoWidth;
1656 postProcessParams.videoHeight = mVideoHeight;
1657 postProcessParams.timeMs = mDecodedVideoTs/1000;
1658 postProcessParams.timeOffset = mDecVideoTsStoryBoard/1000;
1659 postProcessParams.effectsSettings = mEffectsSettings;
1660 postProcessParams.numberEffects = mNumberEffects;
1661 postProcessParams.outVideoWidth = mOutputVideoWidth;
1662 postProcessParams.outVideoHeight = mOutputVideoHeight;
1663 postProcessParams.currentVideoEffect = mCurrentVideoEffect;
1664 postProcessParams.renderingMode = mRenderingMode;
1665 if(mIsFiftiesEffectStarted == M4OSA_TRUE) {
1666 postProcessParams.isFiftiesEffectStarted = M4OSA_TRUE;
1667 mIsFiftiesEffectStarted = M4OSA_FALSE;
1668 }
1669 else {
1670 postProcessParams.isFiftiesEffectStarted = M4OSA_FALSE;
1671 }
1672
1673 postProcessParams.overlayFrameRGBBuffer = mFrameRGBBuffer;
1674 postProcessParams.overlayFrameYUVBuffer = mFrameYUVBuffer;
1675 mVideoRenderer->getBuffer(&(postProcessParams.pOutBuffer), &(postProcessParams.outBufferStride));
Dharmaray Kundargi35cb2de2011-01-19 19:09:27 -08001676 err = applyEffectsAndRenderingMode(&postProcessParams, mReportedWidth, mReportedHeight);
Dharmaray Kundargi643290d2011-01-16 16:02:42 -08001677
1678 return err;
1679}
1680
1681status_t PreviewPlayer::readFirstVideoFrame() {
1682 LOGV("PreviewPlayer::readFirstVideoFrame");
1683
1684 if (!mVideoBuffer) {
1685 MediaSource::ReadOptions options;
1686 if (mSeeking) {
1687 LOGV("LV PLAYER seeking to %lld us (%.2f secs)", mSeekTimeUs,
1688 mSeekTimeUs / 1E6);
1689
1690 options.setSeekTo(
1691 mSeekTimeUs, MediaSource::ReadOptions::SEEK_CLOSEST);
1692 }
1693 for (;;) {
1694 status_t err = mVideoSource->read(&mVideoBuffer, &options);
1695 options.clearSeekTo();
1696
1697 if (err != OK) {
1698 CHECK_EQ(mVideoBuffer, NULL);
1699
1700 if (err == INFO_FORMAT_CHANGED) {
1701 LOGV("LV PLAYER VideoSource signalled format change");
1702 notifyVideoSize_l();
Dharmaray Kundargi35cb2de2011-01-19 19:09:27 -08001703 sp<MetaData> meta = mVideoSource->getFormat();
1704
1705 CHECK(meta->findInt32(kKeyWidth, &mReportedWidth));
1706 CHECK(meta->findInt32(kKeyHeight, &mReportedHeight));
Dharmaray Kundargi643290d2011-01-16 16:02:42 -08001707
1708 if (mVideoRenderer != NULL) {
1709 mVideoRendererIsPreview = false;
1710 initRenderer_l();
1711 }
1712 continue;
1713 }
1714 LOGV("PreviewPlayer: onVideoEvent EOS reached.");
1715 mFlags |= VIDEO_AT_EOS;
1716 postStreamDoneEvent_l(err);
1717 return OK;
1718 }
1719
1720 if (mVideoBuffer->range_length() == 0) {
1721 // Some decoders, notably the PV AVC software decoder
1722 // return spurious empty buffers that we just want to ignore.
1723
1724 mVideoBuffer->release();
1725 mVideoBuffer = NULL;
1726 continue;
1727 }
1728
1729 int64_t videoTimeUs;
1730 CHECK(mVideoBuffer->meta_data()->findInt64(kKeyTime, &videoTimeUs));
1731
1732 if((videoTimeUs/1000) < mPlayBeginTimeMsec) {
1733 // buffers are before begin cut time
1734 // ignore them
1735 //LOGI("PreviewPlayer: Ignoring buffers before begin cut time");
1736 mVideoBuffer->release();
1737 mVideoBuffer = NULL;
1738 continue;
1739 }
1740
1741 break;
1742 }
1743 }
1744
1745 int64_t timeUs;
1746 CHECK(mVideoBuffer->meta_data()->findInt64(kKeyTime, &timeUs));
1747
1748 {
1749 Mutex::Autolock autoLock(mMiscStateLock);
1750 mVideoTimeUs = timeUs;
1751 }
1752
1753 mDecodedVideoTs = timeUs;
1754
1755 return OK;
1756
1757}
1758
1759} // namespace android