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