1643290dc4c83da23b1b8ff4ed71118203274bb15Dharmaray Kundargi/*
2643290dc4c83da23b1b8ff4ed71118203274bb15Dharmaray Kundargi * Copyright (C) 2011 The Android Open Source Project
3643290dc4c83da23b1b8ff4ed71118203274bb15Dharmaray Kundargi *
4643290dc4c83da23b1b8ff4ed71118203274bb15Dharmaray Kundargi * Licensed under the Apache License, Version 2.0 (the "License");
5643290dc4c83da23b1b8ff4ed71118203274bb15Dharmaray Kundargi * you may not use this file except in compliance with the License.
6643290dc4c83da23b1b8ff4ed71118203274bb15Dharmaray Kundargi * You may obtain a copy of the License at
7643290dc4c83da23b1b8ff4ed71118203274bb15Dharmaray Kundargi *
8643290dc4c83da23b1b8ff4ed71118203274bb15Dharmaray Kundargi *      http://www.apache.org/licenses/LICENSE-2.0
9643290dc4c83da23b1b8ff4ed71118203274bb15Dharmaray Kundargi *
10643290dc4c83da23b1b8ff4ed71118203274bb15Dharmaray Kundargi * Unless required by applicable law or agreed to in writing, software
11643290dc4c83da23b1b8ff4ed71118203274bb15Dharmaray Kundargi * distributed under the License is distributed on an "AS IS" BASIS,
12643290dc4c83da23b1b8ff4ed71118203274bb15Dharmaray Kundargi * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13643290dc4c83da23b1b8ff4ed71118203274bb15Dharmaray Kundargi * See the License for the specific language governing permissions and
14643290dc4c83da23b1b8ff4ed71118203274bb15Dharmaray Kundargi * limitations under the License.
15643290dc4c83da23b1b8ff4ed71118203274bb15Dharmaray Kundargi */
16643290dc4c83da23b1b8ff4ed71118203274bb15Dharmaray Kundargi
17bfece17c8f63de4d4b686da50f8ecac2f6f8ec53Santosh Madhava
1800f742c087d92e5452d29be1fb668022b5f8a6c7James Dong// #define LOG_NDEBUG 0
19643290dc4c83da23b1b8ff4ed71118203274bb15Dharmaray Kundargi#define LOG_TAG "PreviewPlayer"
20643290dc4c83da23b1b8ff4ed71118203274bb15Dharmaray Kundargi#include <utils/Log.h>
21643290dc4c83da23b1b8ff4ed71118203274bb15Dharmaray Kundargi
22643290dc4c83da23b1b8ff4ed71118203274bb15Dharmaray Kundargi#include <binder/IPCThreadState.h>
2300f742c087d92e5452d29be1fb668022b5f8a6c7James Dong#include <binder/IServiceManager.h>
2400f742c087d92e5452d29be1fb668022b5f8a6c7James Dong#include <media/IMediaPlayerService.h>
25643290dc4c83da23b1b8ff4ed71118203274bb15Dharmaray Kundargi#include <media/stagefright/DataSource.h>
26643290dc4c83da23b1b8ff4ed71118203274bb15Dharmaray Kundargi#include <media/stagefright/MediaBuffer.h>
27643290dc4c83da23b1b8ff4ed71118203274bb15Dharmaray Kundargi#include <media/stagefright/MediaDefs.h>
28643290dc4c83da23b1b8ff4ed71118203274bb15Dharmaray Kundargi#include <media/stagefright/MediaExtractor.h>
29643290dc4c83da23b1b8ff4ed71118203274bb15Dharmaray Kundargi#include <media/stagefright/MediaSource.h>
30643290dc4c83da23b1b8ff4ed71118203274bb15Dharmaray Kundargi#include <media/stagefright/MetaData.h>
31643290dc4c83da23b1b8ff4ed71118203274bb15Dharmaray Kundargi#include <media/stagefright/OMXCodec.h>
3200f742c087d92e5452d29be1fb668022b5f8a6c7James Dong#include <media/stagefright/foundation/ADebug.h>
3390b6191067990a7a3ee947d7ce08c64f780f0de7Mathias Agopian#include <gui/Surface.h>
3400f742c087d92e5452d29be1fb668022b5f8a6c7James Dong#include <gui/ISurfaceTexture.h>
3500f742c087d92e5452d29be1fb668022b5f8a6c7James Dong#include <gui/SurfaceTextureClient.h>
3600f742c087d92e5452d29be1fb668022b5f8a6c7James Dong
3700f742c087d92e5452d29be1fb668022b5f8a6c7James Dong#include "VideoEditorPreviewController.h"
3800f742c087d92e5452d29be1fb668022b5f8a6c7James Dong#include "DummyAudioSource.h"
3900f742c087d92e5452d29be1fb668022b5f8a6c7James Dong#include "DummyVideoSource.h"
4000f742c087d92e5452d29be1fb668022b5f8a6c7James Dong#include "VideoEditorSRC.h"
4100f742c087d92e5452d29be1fb668022b5f8a6c7James Dong#include "PreviewPlayer.h"
42643290dc4c83da23b1b8ff4ed71118203274bb15Dharmaray Kundargi
43643290dc4c83da23b1b8ff4ed71118203274bb15Dharmaray Kundarginamespace android {
44643290dc4c83da23b1b8ff4ed71118203274bb15Dharmaray Kundargi
45643290dc4c83da23b1b8ff4ed71118203274bb15Dharmaray Kundargi
4600f742c087d92e5452d29be1fb668022b5f8a6c7James Dongvoid addBatteryData(uint32_t params) {
4700f742c087d92e5452d29be1fb668022b5f8a6c7James Dong    sp<IBinder> binder =
4800f742c087d92e5452d29be1fb668022b5f8a6c7James Dong        defaultServiceManager()->getService(String16("media.player"));
4900f742c087d92e5452d29be1fb668022b5f8a6c7James Dong    sp<IMediaPlayerService> service = interface_cast<IMediaPlayerService>(binder);
5000f742c087d92e5452d29be1fb668022b5f8a6c7James Dong    CHECK(service.get() != NULL);
5100f742c087d92e5452d29be1fb668022b5f8a6c7James Dong
5200f742c087d92e5452d29be1fb668022b5f8a6c7James Dong    service->addBatteryData(params);
5300f742c087d92e5452d29be1fb668022b5f8a6c7James Dong}
5400f742c087d92e5452d29be1fb668022b5f8a6c7James Dong
55643290dc4c83da23b1b8ff4ed71118203274bb15Dharmaray Kundargistruct PreviewPlayerEvent : public TimedEventQueue::Event {
56643290dc4c83da23b1b8ff4ed71118203274bb15Dharmaray Kundargi    PreviewPlayerEvent(
57643290dc4c83da23b1b8ff4ed71118203274bb15Dharmaray Kundargi            PreviewPlayer *player,
58643290dc4c83da23b1b8ff4ed71118203274bb15Dharmaray Kundargi            void (PreviewPlayer::*method)())
59643290dc4c83da23b1b8ff4ed71118203274bb15Dharmaray Kundargi        : mPlayer(player),
60643290dc4c83da23b1b8ff4ed71118203274bb15Dharmaray Kundargi          mMethod(method) {
61643290dc4c83da23b1b8ff4ed71118203274bb15Dharmaray Kundargi    }
62643290dc4c83da23b1b8ff4ed71118203274bb15Dharmaray Kundargi
63643290dc4c83da23b1b8ff4ed71118203274bb15Dharmaray Kundargiprotected:
64643290dc4c83da23b1b8ff4ed71118203274bb15Dharmaray Kundargi    virtual ~PreviewPlayerEvent() {}
65643290dc4c83da23b1b8ff4ed71118203274bb15Dharmaray Kundargi
66643290dc4c83da23b1b8ff4ed71118203274bb15Dharmaray Kundargi    virtual void fire(TimedEventQueue *queue, int64_t /* now_us */) {
67643290dc4c83da23b1b8ff4ed71118203274bb15Dharmaray Kundargi        (mPlayer->*mMethod)();
68643290dc4c83da23b1b8ff4ed71118203274bb15Dharmaray Kundargi    }
69643290dc4c83da23b1b8ff4ed71118203274bb15Dharmaray Kundargi
70643290dc4c83da23b1b8ff4ed71118203274bb15Dharmaray Kundargiprivate:
71643290dc4c83da23b1b8ff4ed71118203274bb15Dharmaray Kundargi    PreviewPlayer *mPlayer;
72643290dc4c83da23b1b8ff4ed71118203274bb15Dharmaray Kundargi    void (PreviewPlayer::*mMethod)();
73643290dc4c83da23b1b8ff4ed71118203274bb15Dharmaray Kundargi
74643290dc4c83da23b1b8ff4ed71118203274bb15Dharmaray Kundargi    PreviewPlayerEvent(const PreviewPlayerEvent &);
75643290dc4c83da23b1b8ff4ed71118203274bb15Dharmaray Kundargi    PreviewPlayerEvent &operator=(const PreviewPlayerEvent &);
76643290dc4c83da23b1b8ff4ed71118203274bb15Dharmaray Kundargi};
77643290dc4c83da23b1b8ff4ed71118203274bb15Dharmaray Kundargi
7843fcc396614a587851e2b7c4cea2876ec58b8648Chih-Chung ChangPreviewPlayer::PreviewPlayer(NativeWindowRenderer* renderer)
7900f742c087d92e5452d29be1fb668022b5f8a6c7James Dong    : mQueueStarted(false),
8000f742c087d92e5452d29be1fb668022b5f8a6c7James Dong      mTimeSource(NULL),
8100f742c087d92e5452d29be1fb668022b5f8a6c7James Dong      mVideoRendererIsPreview(false),
8200f742c087d92e5452d29be1fb668022b5f8a6c7James Dong      mAudioPlayer(NULL),
8300f742c087d92e5452d29be1fb668022b5f8a6c7James Dong      mDisplayWidth(0),
8400f742c087d92e5452d29be1fb668022b5f8a6c7James Dong      mDisplayHeight(0),
8500f742c087d92e5452d29be1fb668022b5f8a6c7James Dong      mFlags(0),
8600f742c087d92e5452d29be1fb668022b5f8a6c7James Dong      mExtractorFlags(0),
8700f742c087d92e5452d29be1fb668022b5f8a6c7James Dong      mVideoBuffer(NULL),
8800f742c087d92e5452d29be1fb668022b5f8a6c7James Dong      mLastVideoTimeUs(-1),
8943fcc396614a587851e2b7c4cea2876ec58b8648Chih-Chung Chang      mNativeWindowRenderer(renderer),
9043fcc396614a587851e2b7c4cea2876ec58b8648Chih-Chung Chang      mCurrFramingEffectIndex(0),
91408e25b2508d97f7ae0b39acf46ab42b7c223c44Basavapatna Dattaguru      mFrameRGBBuffer(NULL),
9243fcc396614a587851e2b7c4cea2876ec58b8648Chih-Chung Chang      mFrameYUVBuffer(NULL) {
93643290dc4c83da23b1b8ff4ed71118203274bb15Dharmaray Kundargi
9400f742c087d92e5452d29be1fb668022b5f8a6c7James Dong    CHECK_EQ(mClient.connect(), (status_t)OK);
9500f742c087d92e5452d29be1fb668022b5f8a6c7James Dong    DataSource::RegisterDefaultSniffers();
9600f742c087d92e5452d29be1fb668022b5f8a6c7James Dong
9700f742c087d92e5452d29be1fb668022b5f8a6c7James Dong
98643290dc4c83da23b1b8ff4ed71118203274bb15Dharmaray Kundargi    mVideoRenderer = NULL;
99643290dc4c83da23b1b8ff4ed71118203274bb15Dharmaray Kundargi    mEffectsSettings = NULL;
100727f9e1e8e1306af761765e480ac5499224db646James Dong    mAudioPlayer = NULL;
101643290dc4c83da23b1b8ff4ed71118203274bb15Dharmaray Kundargi    mAudioMixStoryBoardTS = 0;
102643290dc4c83da23b1b8ff4ed71118203274bb15Dharmaray Kundargi    mCurrentMediaBeginCutTime = 0;
103643290dc4c83da23b1b8ff4ed71118203274bb15Dharmaray Kundargi    mCurrentMediaVolumeValue = 0;
104643290dc4c83da23b1b8ff4ed71118203274bb15Dharmaray Kundargi    mNumberEffects = 0;
105643290dc4c83da23b1b8ff4ed71118203274bb15Dharmaray Kundargi    mDecodedVideoTs = 0;
106643290dc4c83da23b1b8ff4ed71118203274bb15Dharmaray Kundargi    mDecVideoTsStoryBoard = 0;
107643290dc4c83da23b1b8ff4ed71118203274bb15Dharmaray Kundargi    mCurrentVideoEffect = VIDEO_EFFECT_NONE;
108643290dc4c83da23b1b8ff4ed71118203274bb15Dharmaray Kundargi    mProgressCbInterval = 0;
109643290dc4c83da23b1b8ff4ed71118203274bb15Dharmaray Kundargi    mNumberDecVideoFrames = 0;
110e6c0750cd2927967f93a8811954f889d23fadb6eDharmaray Kundargi    mOverlayUpdateEventPosted = false;
1111c97d9ab52a288d24ea54499de435277cc1a3d68Rajneesh Chowdury    mIsChangeSourceRequired = true;
112643290dc4c83da23b1b8ff4ed71118203274bb15Dharmaray Kundargi
113643290dc4c83da23b1b8ff4ed71118203274bb15Dharmaray Kundargi    mVideoEvent = new PreviewPlayerEvent(this, &PreviewPlayer::onVideoEvent);
114643290dc4c83da23b1b8ff4ed71118203274bb15Dharmaray Kundargi    mVideoEventPending = false;
11500f742c087d92e5452d29be1fb668022b5f8a6c7James Dong    mVideoLagEvent = new PreviewPlayerEvent(this, &PreviewPlayer::onVideoLagUpdate);
11600f742c087d92e5452d29be1fb668022b5f8a6c7James Dong    mVideoEventPending = false;
117643290dc4c83da23b1b8ff4ed71118203274bb15Dharmaray Kundargi    mCheckAudioStatusEvent = new PreviewPlayerEvent(
11800f742c087d92e5452d29be1fb668022b5f8a6c7James Dong            this, &PreviewPlayer::onCheckAudioStatus);
119643290dc4c83da23b1b8ff4ed71118203274bb15Dharmaray Kundargi    mAudioStatusEventPending = false;
12000f742c087d92e5452d29be1fb668022b5f8a6c7James Dong    mStreamDoneEvent = new PreviewPlayerEvent(
12100f742c087d92e5452d29be1fb668022b5f8a6c7James Dong            this, &PreviewPlayer::onStreamDone);
12200f742c087d92e5452d29be1fb668022b5f8a6c7James Dong    mStreamDoneEventPending = false;
123643290dc4c83da23b1b8ff4ed71118203274bb15Dharmaray Kundargi    mProgressCbEvent = new PreviewPlayerEvent(this,
124e6c0750cd2927967f93a8811954f889d23fadb6eDharmaray Kundargi         &PreviewPlayer::onProgressCbEvent);
125643290dc4c83da23b1b8ff4ed71118203274bb15Dharmaray Kundargi
126e6c0750cd2927967f93a8811954f889d23fadb6eDharmaray Kundargi    mOverlayUpdateEvent = new PreviewPlayerEvent(this,
127e6c0750cd2927967f93a8811954f889d23fadb6eDharmaray Kundargi        &PreviewPlayer::onUpdateOverlayEvent);
128643290dc4c83da23b1b8ff4ed71118203274bb15Dharmaray Kundargi    mProgressCbEventPending = false;
129e6c0750cd2927967f93a8811954f889d23fadb6eDharmaray Kundargi
130e6c0750cd2927967f93a8811954f889d23fadb6eDharmaray Kundargi    mOverlayUpdateEventPending = false;
131643290dc4c83da23b1b8ff4ed71118203274bb15Dharmaray Kundargi    mRenderingMode = (M4xVSS_MediaRendering)MEDIA_RENDERING_INVALID;
132643290dc4c83da23b1b8ff4ed71118203274bb15Dharmaray Kundargi    mIsFiftiesEffectStarted = false;
133643290dc4c83da23b1b8ff4ed71118203274bb15Dharmaray Kundargi    reset();
134643290dc4c83da23b1b8ff4ed71118203274bb15Dharmaray Kundargi}
135643290dc4c83da23b1b8ff4ed71118203274bb15Dharmaray Kundargi
136643290dc4c83da23b1b8ff4ed71118203274bb15Dharmaray KundargiPreviewPlayer::~PreviewPlayer() {
137643290dc4c83da23b1b8ff4ed71118203274bb15Dharmaray Kundargi
138643290dc4c83da23b1b8ff4ed71118203274bb15Dharmaray Kundargi    if (mQueueStarted) {
139643290dc4c83da23b1b8ff4ed71118203274bb15Dharmaray Kundargi        mQueue.stop();
140643290dc4c83da23b1b8ff4ed71118203274bb15Dharmaray Kundargi    }
141643290dc4c83da23b1b8ff4ed71118203274bb15Dharmaray Kundargi
142643290dc4c83da23b1b8ff4ed71118203274bb15Dharmaray Kundargi    reset();
143643290dc4c83da23b1b8ff4ed71118203274bb15Dharmaray Kundargi
14443fcc396614a587851e2b7c4cea2876ec58b8648Chih-Chung Chang    if (mVideoRenderer) {
14543fcc396614a587851e2b7c4cea2876ec58b8648Chih-Chung Chang        mNativeWindowRenderer->destroyRenderInput(mVideoRenderer);
146643290dc4c83da23b1b8ff4ed71118203274bb15Dharmaray Kundargi    }
14700f742c087d92e5452d29be1fb668022b5f8a6c7James Dong
14800f742c087d92e5452d29be1fb668022b5f8a6c7James Dong    Mutex::Autolock lock(mLock);
14900f742c087d92e5452d29be1fb668022b5f8a6c7James Dong    clear_l();
15000f742c087d92e5452d29be1fb668022b5f8a6c7James Dong    mClient.disconnect();
151643290dc4c83da23b1b8ff4ed71118203274bb15Dharmaray Kundargi}
152643290dc4c83da23b1b8ff4ed71118203274bb15Dharmaray Kundargi
15300f742c087d92e5452d29be1fb668022b5f8a6c7James Dongvoid PreviewPlayer::cancelPlayerEvents_l(bool updateProgressCb) {
154643290dc4c83da23b1b8ff4ed71118203274bb15Dharmaray Kundargi    mQueue.cancelEvent(mVideoEvent->eventID());
155643290dc4c83da23b1b8ff4ed71118203274bb15Dharmaray Kundargi    mVideoEventPending = false;
156643290dc4c83da23b1b8ff4ed71118203274bb15Dharmaray Kundargi    mQueue.cancelEvent(mStreamDoneEvent->eventID());
157643290dc4c83da23b1b8ff4ed71118203274bb15Dharmaray Kundargi    mStreamDoneEventPending = false;
158643290dc4c83da23b1b8ff4ed71118203274bb15Dharmaray Kundargi    mQueue.cancelEvent(mCheckAudioStatusEvent->eventID());
159643290dc4c83da23b1b8ff4ed71118203274bb15Dharmaray Kundargi    mAudioStatusEventPending = false;
16000f742c087d92e5452d29be1fb668022b5f8a6c7James Dong    mQueue.cancelEvent(mVideoLagEvent->eventID());
16100f742c087d92e5452d29be1fb668022b5f8a6c7James Dong    mVideoLagEventPending = false;
16200f742c087d92e5452d29be1fb668022b5f8a6c7James Dong    if (updateProgressCb) {
16300f742c087d92e5452d29be1fb668022b5f8a6c7James Dong        mQueue.cancelEvent(mProgressCbEvent->eventID());
16400f742c087d92e5452d29be1fb668022b5f8a6c7James Dong        mProgressCbEventPending = false;
16500f742c087d92e5452d29be1fb668022b5f8a6c7James Dong    }
166643290dc4c83da23b1b8ff4ed71118203274bb15Dharmaray Kundargi}
167643290dc4c83da23b1b8ff4ed71118203274bb15Dharmaray Kundargi
168daeb5b33861147fda0096f9c794be6f341d884c2James Dongstatus_t PreviewPlayer::setDataSource(const char *path) {
169643290dc4c83da23b1b8ff4ed71118203274bb15Dharmaray Kundargi    Mutex::Autolock autoLock(mLock);
170daeb5b33861147fda0096f9c794be6f341d884c2James Dong    return setDataSource_l(path);
171643290dc4c83da23b1b8ff4ed71118203274bb15Dharmaray Kundargi}
172643290dc4c83da23b1b8ff4ed71118203274bb15Dharmaray Kundargi
173daeb5b33861147fda0096f9c794be6f341d884c2James Dongstatus_t PreviewPlayer::setDataSource_l(const char *path) {
174643290dc4c83da23b1b8ff4ed71118203274bb15Dharmaray Kundargi    reset_l();
175643290dc4c83da23b1b8ff4ed71118203274bb15Dharmaray Kundargi
176daeb5b33861147fda0096f9c794be6f341d884c2James Dong    mUri = path;
177643290dc4c83da23b1b8ff4ed71118203274bb15Dharmaray Kundargi
178643290dc4c83da23b1b8ff4ed71118203274bb15Dharmaray Kundargi    // The actual work will be done during preparation in the call to
179643290dc4c83da23b1b8ff4ed71118203274bb15Dharmaray Kundargi    // ::finishSetDataSource_l to avoid blocking the calling thread in
180643290dc4c83da23b1b8ff4ed71118203274bb15Dharmaray Kundargi    // setDataSource for any significant time.
181643290dc4c83da23b1b8ff4ed71118203274bb15Dharmaray Kundargi    return OK;
182643290dc4c83da23b1b8ff4ed71118203274bb15Dharmaray Kundargi}
183643290dc4c83da23b1b8ff4ed71118203274bb15Dharmaray Kundargi
184643290dc4c83da23b1b8ff4ed71118203274bb15Dharmaray Kundargistatus_t PreviewPlayer::setDataSource_l(const sp<MediaExtractor> &extractor) {
185643290dc4c83da23b1b8ff4ed71118203274bb15Dharmaray Kundargi    bool haveAudio = false;
186643290dc4c83da23b1b8ff4ed71118203274bb15Dharmaray Kundargi    bool haveVideo = false;
187643290dc4c83da23b1b8ff4ed71118203274bb15Dharmaray Kundargi    for (size_t i = 0; i < extractor->countTracks(); ++i) {
188643290dc4c83da23b1b8ff4ed71118203274bb15Dharmaray Kundargi        sp<MetaData> meta = extractor->getTrackMetaData(i);
189643290dc4c83da23b1b8ff4ed71118203274bb15Dharmaray Kundargi
190643290dc4c83da23b1b8ff4ed71118203274bb15Dharmaray Kundargi        const char *mime;
191643290dc4c83da23b1b8ff4ed71118203274bb15Dharmaray Kundargi        CHECK(meta->findCString(kKeyMIMEType, &mime));
192643290dc4c83da23b1b8ff4ed71118203274bb15Dharmaray Kundargi
193643290dc4c83da23b1b8ff4ed71118203274bb15Dharmaray Kundargi        if (!haveVideo && !strncasecmp(mime, "video/", 6)) {
194643290dc4c83da23b1b8ff4ed71118203274bb15Dharmaray Kundargi            setVideoSource(extractor->getTrack(i));
195643290dc4c83da23b1b8ff4ed71118203274bb15Dharmaray Kundargi            haveVideo = true;
196643290dc4c83da23b1b8ff4ed71118203274bb15Dharmaray Kundargi        } else if (!haveAudio && !strncasecmp(mime, "audio/", 6)) {
197643290dc4c83da23b1b8ff4ed71118203274bb15Dharmaray Kundargi            setAudioSource(extractor->getTrack(i));
198643290dc4c83da23b1b8ff4ed71118203274bb15Dharmaray Kundargi            haveAudio = true;
199643290dc4c83da23b1b8ff4ed71118203274bb15Dharmaray Kundargi
200643290dc4c83da23b1b8ff4ed71118203274bb15Dharmaray Kundargi            if (!strcasecmp(mime, MEDIA_MIMETYPE_AUDIO_VORBIS)) {
201643290dc4c83da23b1b8ff4ed71118203274bb15Dharmaray Kundargi                // Only do this for vorbis audio, none of the other audio
202643290dc4c83da23b1b8ff4ed71118203274bb15Dharmaray Kundargi                // formats even support this ringtone specific hack and
203643290dc4c83da23b1b8ff4ed71118203274bb15Dharmaray Kundargi                // retrieving the metadata on some extractors may turn out
204643290dc4c83da23b1b8ff4ed71118203274bb15Dharmaray Kundargi                // to be very expensive.
205643290dc4c83da23b1b8ff4ed71118203274bb15Dharmaray Kundargi                sp<MetaData> fileMeta = extractor->getMetaData();
206643290dc4c83da23b1b8ff4ed71118203274bb15Dharmaray Kundargi                int32_t loop;
207643290dc4c83da23b1b8ff4ed71118203274bb15Dharmaray Kundargi                if (fileMeta != NULL
208643290dc4c83da23b1b8ff4ed71118203274bb15Dharmaray Kundargi                        && fileMeta->findInt32(kKeyAutoLoop, &loop)
209643290dc4c83da23b1b8ff4ed71118203274bb15Dharmaray Kundargi                         && loop != 0) {
210643290dc4c83da23b1b8ff4ed71118203274bb15Dharmaray Kundargi                    mFlags |= AUTO_LOOPING;
211643290dc4c83da23b1b8ff4ed71118203274bb15Dharmaray Kundargi                }
212643290dc4c83da23b1b8ff4ed71118203274bb15Dharmaray Kundargi            }
213643290dc4c83da23b1b8ff4ed71118203274bb15Dharmaray Kundargi        }
214643290dc4c83da23b1b8ff4ed71118203274bb15Dharmaray Kundargi
215643290dc4c83da23b1b8ff4ed71118203274bb15Dharmaray Kundargi        if (haveAudio && haveVideo) {
216643290dc4c83da23b1b8ff4ed71118203274bb15Dharmaray Kundargi            break;
217643290dc4c83da23b1b8ff4ed71118203274bb15Dharmaray Kundargi        }
218643290dc4c83da23b1b8ff4ed71118203274bb15Dharmaray Kundargi    }
219643290dc4c83da23b1b8ff4ed71118203274bb15Dharmaray Kundargi
220643290dc4c83da23b1b8ff4ed71118203274bb15Dharmaray Kundargi    /* Add the support for Dummy audio*/
221643290dc4c83da23b1b8ff4ed71118203274bb15Dharmaray Kundargi    if( !haveAudio ){
222643290dc4c83da23b1b8ff4ed71118203274bb15Dharmaray Kundargi        mAudioTrack = DummyAudioSource::Create(32000, 2, 20000,
223ebf40154a222c864a0aaf807702fc4eb6b9cf40fRaghavender Palla                                              ((mPlayEndTimeMsec)*1000LL));
224643290dc4c83da23b1b8ff4ed71118203274bb15Dharmaray Kundargi        if(mAudioTrack != NULL) {
225643290dc4c83da23b1b8ff4ed71118203274bb15Dharmaray Kundargi            haveAudio = true;
226643290dc4c83da23b1b8ff4ed71118203274bb15Dharmaray Kundargi        }
227643290dc4c83da23b1b8ff4ed71118203274bb15Dharmaray Kundargi    }
228643290dc4c83da23b1b8ff4ed71118203274bb15Dharmaray Kundargi
229643290dc4c83da23b1b8ff4ed71118203274bb15Dharmaray Kundargi    if (!haveAudio && !haveVideo) {
230643290dc4c83da23b1b8ff4ed71118203274bb15Dharmaray Kundargi        return UNKNOWN_ERROR;
231643290dc4c83da23b1b8ff4ed71118203274bb15Dharmaray Kundargi    }
232643290dc4c83da23b1b8ff4ed71118203274bb15Dharmaray Kundargi
233643290dc4c83da23b1b8ff4ed71118203274bb15Dharmaray Kundargi    mExtractorFlags = extractor->flags();
234643290dc4c83da23b1b8ff4ed71118203274bb15Dharmaray Kundargi    return OK;
235643290dc4c83da23b1b8ff4ed71118203274bb15Dharmaray Kundargi}
236643290dc4c83da23b1b8ff4ed71118203274bb15Dharmaray Kundargi
237643290dc4c83da23b1b8ff4ed71118203274bb15Dharmaray Kundargistatus_t PreviewPlayer::setDataSource_l_jpg() {
23800f742c087d92e5452d29be1fb668022b5f8a6c7James Dong    ALOGV("setDataSource_l_jpg");
23900f742c087d92e5452d29be1fb668022b5f8a6c7James Dong
240643290dc4c83da23b1b8ff4ed71118203274bb15Dharmaray Kundargi    M4OSA_ERR err = M4NO_ERROR;
241643290dc4c83da23b1b8ff4ed71118203274bb15Dharmaray Kundargi
242643290dc4c83da23b1b8ff4ed71118203274bb15Dharmaray Kundargi    mAudioSource = DummyAudioSource::Create(32000, 2, 20000,
243ebf40154a222c864a0aaf807702fc4eb6b9cf40fRaghavender Palla                                          ((mPlayEndTimeMsec)*1000LL));
244643290dc4c83da23b1b8ff4ed71118203274bb15Dharmaray Kundargi    if(mAudioSource != NULL) {
245643290dc4c83da23b1b8ff4ed71118203274bb15Dharmaray Kundargi        setAudioSource(mAudioSource);
246643290dc4c83da23b1b8ff4ed71118203274bb15Dharmaray Kundargi    }
247643290dc4c83da23b1b8ff4ed71118203274bb15Dharmaray Kundargi    status_t error = mAudioSource->start();
248643290dc4c83da23b1b8ff4ed71118203274bb15Dharmaray Kundargi    if (error != OK) {
24900f742c087d92e5452d29be1fb668022b5f8a6c7James Dong        ALOGE("Error starting dummy audio source");
250643290dc4c83da23b1b8ff4ed71118203274bb15Dharmaray Kundargi        mAudioSource.clear();
251643290dc4c83da23b1b8ff4ed71118203274bb15Dharmaray Kundargi        return err;
252643290dc4c83da23b1b8ff4ed71118203274bb15Dharmaray Kundargi    }
253643290dc4c83da23b1b8ff4ed71118203274bb15Dharmaray Kundargi
254ebf40154a222c864a0aaf807702fc4eb6b9cf40fRaghavender Palla    mDurationUs = (mPlayEndTimeMsec - mPlayBeginTimeMsec)*1000LL;
255643290dc4c83da23b1b8ff4ed71118203274bb15Dharmaray Kundargi
256643290dc4c83da23b1b8ff4ed71118203274bb15Dharmaray Kundargi    mVideoSource = DummyVideoSource::Create(mVideoWidth, mVideoHeight,
257643290dc4c83da23b1b8ff4ed71118203274bb15Dharmaray Kundargi                                            mDurationUs, mUri);
25835cb2de64cb6482a08f446e80733e7d344a0dcacDharmaray Kundargi
25943fcc396614a587851e2b7c4cea2876ec58b8648Chih-Chung Chang    updateSizeToRender(mVideoSource->getFormat());
260643290dc4c83da23b1b8ff4ed71118203274bb15Dharmaray Kundargi    setVideoSource(mVideoSource);
261643290dc4c83da23b1b8ff4ed71118203274bb15Dharmaray Kundargi    status_t err1 = mVideoSource->start();
262643290dc4c83da23b1b8ff4ed71118203274bb15Dharmaray Kundargi    if (err1 != OK) {
263643290dc4c83da23b1b8ff4ed71118203274bb15Dharmaray Kundargi        mVideoSource.clear();
264643290dc4c83da23b1b8ff4ed71118203274bb15Dharmaray Kundargi        return err;
265643290dc4c83da23b1b8ff4ed71118203274bb15Dharmaray Kundargi    }
266643290dc4c83da23b1b8ff4ed71118203274bb15Dharmaray Kundargi
267643290dc4c83da23b1b8ff4ed71118203274bb15Dharmaray Kundargi    mIsVideoSourceJpg = true;
268643290dc4c83da23b1b8ff4ed71118203274bb15Dharmaray Kundargi    return OK;
269643290dc4c83da23b1b8ff4ed71118203274bb15Dharmaray Kundargi}
270643290dc4c83da23b1b8ff4ed71118203274bb15Dharmaray Kundargi
271643290dc4c83da23b1b8ff4ed71118203274bb15Dharmaray Kundargivoid PreviewPlayer::reset_l() {
272643290dc4c83da23b1b8ff4ed71118203274bb15Dharmaray Kundargi
273643290dc4c83da23b1b8ff4ed71118203274bb15Dharmaray Kundargi    if (mFlags & PREPARING) {
274643290dc4c83da23b1b8ff4ed71118203274bb15Dharmaray Kundargi        mFlags |= PREPARE_CANCELLED;
275643290dc4c83da23b1b8ff4ed71118203274bb15Dharmaray Kundargi    }
276643290dc4c83da23b1b8ff4ed71118203274bb15Dharmaray Kundargi
277643290dc4c83da23b1b8ff4ed71118203274bb15Dharmaray Kundargi    while (mFlags & PREPARING) {
278643290dc4c83da23b1b8ff4ed71118203274bb15Dharmaray Kundargi        mPreparedCondition.wait(mLock);
279643290dc4c83da23b1b8ff4ed71118203274bb15Dharmaray Kundargi    }
280643290dc4c83da23b1b8ff4ed71118203274bb15Dharmaray Kundargi
28100f742c087d92e5452d29be1fb668022b5f8a6c7James Dong    cancelPlayerEvents_l();
282643290dc4c83da23b1b8ff4ed71118203274bb15Dharmaray Kundargi    mAudioTrack.clear();
283643290dc4c83da23b1b8ff4ed71118203274bb15Dharmaray Kundargi    mVideoTrack.clear();
284643290dc4c83da23b1b8ff4ed71118203274bb15Dharmaray Kundargi
285643290dc4c83da23b1b8ff4ed71118203274bb15Dharmaray Kundargi    // Shutdown audio first, so that the respone to the reset request
286643290dc4c83da23b1b8ff4ed71118203274bb15Dharmaray Kundargi    // appears to happen instantaneously as far as the user is concerned
287643290dc4c83da23b1b8ff4ed71118203274bb15Dharmaray Kundargi    // If we did this later, audio would continue playing while we
288643290dc4c83da23b1b8ff4ed71118203274bb15Dharmaray Kundargi    // shutdown the video-related resources and the player appear to
289643290dc4c83da23b1b8ff4ed71118203274bb15Dharmaray Kundargi    // not be as responsive to a reset request.
290643290dc4c83da23b1b8ff4ed71118203274bb15Dharmaray Kundargi    if (mAudioPlayer == NULL && mAudioSource != NULL) {
291643290dc4c83da23b1b8ff4ed71118203274bb15Dharmaray Kundargi        // If we had an audio player, it would have effectively
292643290dc4c83da23b1b8ff4ed71118203274bb15Dharmaray Kundargi        // taken possession of the audio source and stopped it when
293643290dc4c83da23b1b8ff4ed71118203274bb15Dharmaray Kundargi        // _it_ is stopped. Otherwise this is still our responsibility.
294643290dc4c83da23b1b8ff4ed71118203274bb15Dharmaray Kundargi        mAudioSource->stop();
295643290dc4c83da23b1b8ff4ed71118203274bb15Dharmaray Kundargi    }
296643290dc4c83da23b1b8ff4ed71118203274bb15Dharmaray Kundargi    mAudioSource.clear();
297643290dc4c83da23b1b8ff4ed71118203274bb15Dharmaray Kundargi
298643290dc4c83da23b1b8ff4ed71118203274bb15Dharmaray Kundargi    mTimeSource = NULL;
299643290dc4c83da23b1b8ff4ed71118203274bb15Dharmaray Kundargi
3001c97d9ab52a288d24ea54499de435277cc1a3d68Rajneesh Chowdury    //Single audio player instance used
3011c97d9ab52a288d24ea54499de435277cc1a3d68Rajneesh Chowdury    //So donot delete it here
3021c97d9ab52a288d24ea54499de435277cc1a3d68Rajneesh Chowdury    //It is deleted from PreviewController class
3031c97d9ab52a288d24ea54499de435277cc1a3d68Rajneesh Chowdury    //delete mAudioPlayer;
304643290dc4c83da23b1b8ff4ed71118203274bb15Dharmaray Kundargi    mAudioPlayer = NULL;
305643290dc4c83da23b1b8ff4ed71118203274bb15Dharmaray Kundargi
306643290dc4c83da23b1b8ff4ed71118203274bb15Dharmaray Kundargi    if (mVideoBuffer) {
307643290dc4c83da23b1b8ff4ed71118203274bb15Dharmaray Kundargi        mVideoBuffer->release();
308643290dc4c83da23b1b8ff4ed71118203274bb15Dharmaray Kundargi        mVideoBuffer = NULL;
309643290dc4c83da23b1b8ff4ed71118203274bb15Dharmaray Kundargi    }
310643290dc4c83da23b1b8ff4ed71118203274bb15Dharmaray Kundargi
311643290dc4c83da23b1b8ff4ed71118203274bb15Dharmaray Kundargi    if (mVideoSource != NULL) {
312643290dc4c83da23b1b8ff4ed71118203274bb15Dharmaray Kundargi        mVideoSource->stop();
313643290dc4c83da23b1b8ff4ed71118203274bb15Dharmaray Kundargi
314643290dc4c83da23b1b8ff4ed71118203274bb15Dharmaray Kundargi        // The following hack is necessary to ensure that the OMX
315643290dc4c83da23b1b8ff4ed71118203274bb15Dharmaray Kundargi        // component is completely released by the time we may try
316643290dc4c83da23b1b8ff4ed71118203274bb15Dharmaray Kundargi        // to instantiate it again.
317643290dc4c83da23b1b8ff4ed71118203274bb15Dharmaray Kundargi        wp<MediaSource> tmp = mVideoSource;
318643290dc4c83da23b1b8ff4ed71118203274bb15Dharmaray Kundargi        mVideoSource.clear();
319643290dc4c83da23b1b8ff4ed71118203274bb15Dharmaray Kundargi        while (tmp.promote() != NULL) {
320643290dc4c83da23b1b8ff4ed71118203274bb15Dharmaray Kundargi            usleep(1000);
321643290dc4c83da23b1b8ff4ed71118203274bb15Dharmaray Kundargi        }
322643290dc4c83da23b1b8ff4ed71118203274bb15Dharmaray Kundargi        IPCThreadState::self()->flushCommands();
323643290dc4c83da23b1b8ff4ed71118203274bb15Dharmaray Kundargi    }
324643290dc4c83da23b1b8ff4ed71118203274bb15Dharmaray Kundargi
325643290dc4c83da23b1b8ff4ed71118203274bb15Dharmaray Kundargi    mDurationUs = -1;
326643290dc4c83da23b1b8ff4ed71118203274bb15Dharmaray Kundargi    mFlags = 0;
327643290dc4c83da23b1b8ff4ed71118203274bb15Dharmaray Kundargi    mExtractorFlags = 0;
328643290dc4c83da23b1b8ff4ed71118203274bb15Dharmaray Kundargi    mVideoWidth = mVideoHeight = -1;
329643290dc4c83da23b1b8ff4ed71118203274bb15Dharmaray Kundargi    mTimeSourceDeltaUs = 0;
330643290dc4c83da23b1b8ff4ed71118203274bb15Dharmaray Kundargi    mVideoTimeUs = 0;
331643290dc4c83da23b1b8ff4ed71118203274bb15Dharmaray Kundargi
332a5872f770c50da1db555d53e062a9896ea4321d5Andreas Huber    mSeeking = NO_SEEK;
333643290dc4c83da23b1b8ff4ed71118203274bb15Dharmaray Kundargi    mSeekNotificationSent = false;
334643290dc4c83da23b1b8ff4ed71118203274bb15Dharmaray Kundargi    mSeekTimeUs = 0;
335643290dc4c83da23b1b8ff4ed71118203274bb15Dharmaray Kundargi
336643290dc4c83da23b1b8ff4ed71118203274bb15Dharmaray Kundargi    mUri.setTo("");
337643290dc4c83da23b1b8ff4ed71118203274bb15Dharmaray Kundargi
338643290dc4c83da23b1b8ff4ed71118203274bb15Dharmaray Kundargi    mCurrentVideoEffect = VIDEO_EFFECT_NONE;
339643290dc4c83da23b1b8ff4ed71118203274bb15Dharmaray Kundargi    mIsVideoSourceJpg = false;
340643290dc4c83da23b1b8ff4ed71118203274bb15Dharmaray Kundargi    mFrameRGBBuffer = NULL;
341643290dc4c83da23b1b8ff4ed71118203274bb15Dharmaray Kundargi    if(mFrameYUVBuffer != NULL) {
342694816d7291f17364502ac5d3319684a0b180860Shyam Pallapothu        free(mFrameYUVBuffer);
343643290dc4c83da23b1b8ff4ed71118203274bb15Dharmaray Kundargi        mFrameYUVBuffer = NULL;
344643290dc4c83da23b1b8ff4ed71118203274bb15Dharmaray Kundargi    }
345643290dc4c83da23b1b8ff4ed71118203274bb15Dharmaray Kundargi}
346643290dc4c83da23b1b8ff4ed71118203274bb15Dharmaray Kundargi
347643290dc4c83da23b1b8ff4ed71118203274bb15Dharmaray Kundargistatus_t PreviewPlayer::play() {
34800f742c087d92e5452d29be1fb668022b5f8a6c7James Dong    ALOGV("play");
349643290dc4c83da23b1b8ff4ed71118203274bb15Dharmaray Kundargi    Mutex::Autolock autoLock(mLock);
350643290dc4c83da23b1b8ff4ed71118203274bb15Dharmaray Kundargi
351643290dc4c83da23b1b8ff4ed71118203274bb15Dharmaray Kundargi    mFlags &= ~CACHE_UNDERRUN;
352e018023b4844fef7880bf5e45f706afcb10c33b6Hong Teng    mFlags &= ~INFORMED_AV_EOS;
353643290dc4c83da23b1b8ff4ed71118203274bb15Dharmaray Kundargi    return play_l();
354643290dc4c83da23b1b8ff4ed71118203274bb15Dharmaray Kundargi}
355643290dc4c83da23b1b8ff4ed71118203274bb15Dharmaray Kundargi
3565bc7fb407ce1bab13d4a4a67d34a1a3192ee3186Dheeraj Sharmastatus_t PreviewPlayer::startAudioPlayer_l() {
35700f742c087d92e5452d29be1fb668022b5f8a6c7James Dong    ALOGV("startAudioPlayer_l");
3585bc7fb407ce1bab13d4a4a67d34a1a3192ee3186Dheeraj Sharma    CHECK(!(mFlags & AUDIO_RUNNING));
3595bc7fb407ce1bab13d4a4a67d34a1a3192ee3186Dheeraj Sharma
3605bc7fb407ce1bab13d4a4a67d34a1a3192ee3186Dheeraj Sharma    if (mAudioSource == NULL || mAudioPlayer == NULL) {
3615bc7fb407ce1bab13d4a4a67d34a1a3192ee3186Dheeraj Sharma        return OK;
3625bc7fb407ce1bab13d4a4a67d34a1a3192ee3186Dheeraj Sharma    }
3635bc7fb407ce1bab13d4a4a67d34a1a3192ee3186Dheeraj Sharma
3645bc7fb407ce1bab13d4a4a67d34a1a3192ee3186Dheeraj Sharma    if (!(mFlags & AUDIOPLAYER_STARTED)) {
3655bc7fb407ce1bab13d4a4a67d34a1a3192ee3186Dheeraj Sharma        mFlags |= AUDIOPLAYER_STARTED;
3665bc7fb407ce1bab13d4a4a67d34a1a3192ee3186Dheeraj Sharma
3675bc7fb407ce1bab13d4a4a67d34a1a3192ee3186Dheeraj Sharma        // We've already started the MediaSource in order to enable
3685bc7fb407ce1bab13d4a4a67d34a1a3192ee3186Dheeraj Sharma        // the prefetcher to read its data.
369727f9e1e8e1306af761765e480ac5499224db646James Dong        status_t err = mAudioPlayer->start(
3705bc7fb407ce1bab13d4a4a67d34a1a3192ee3186Dheeraj Sharma                true /* sourceAlreadyStarted */);
3715bc7fb407ce1bab13d4a4a67d34a1a3192ee3186Dheeraj Sharma
3725bc7fb407ce1bab13d4a4a67d34a1a3192ee3186Dheeraj Sharma        if (err != OK) {
3735bc7fb407ce1bab13d4a4a67d34a1a3192ee3186Dheeraj Sharma            notifyListener_l(MEDIA_ERROR, MEDIA_ERROR_UNKNOWN, err);
3745bc7fb407ce1bab13d4a4a67d34a1a3192ee3186Dheeraj Sharma            return err;
3755bc7fb407ce1bab13d4a4a67d34a1a3192ee3186Dheeraj Sharma        }
3765bc7fb407ce1bab13d4a4a67d34a1a3192ee3186Dheeraj Sharma    } else {
377727f9e1e8e1306af761765e480ac5499224db646James Dong        mAudioPlayer->resume();
3785bc7fb407ce1bab13d4a4a67d34a1a3192ee3186Dheeraj Sharma    }
3795bc7fb407ce1bab13d4a4a67d34a1a3192ee3186Dheeraj Sharma
3805bc7fb407ce1bab13d4a4a67d34a1a3192ee3186Dheeraj Sharma    mFlags |= AUDIO_RUNNING;
3815bc7fb407ce1bab13d4a4a67d34a1a3192ee3186Dheeraj Sharma
3825bc7fb407ce1bab13d4a4a67d34a1a3192ee3186Dheeraj Sharma    mWatchForAudioEOS = true;
3835bc7fb407ce1bab13d4a4a67d34a1a3192ee3186Dheeraj Sharma
3845bc7fb407ce1bab13d4a4a67d34a1a3192ee3186Dheeraj Sharma    return OK;
3855bc7fb407ce1bab13d4a4a67d34a1a3192ee3186Dheeraj Sharma}
3865bc7fb407ce1bab13d4a4a67d34a1a3192ee3186Dheeraj Sharma
387727f9e1e8e1306af761765e480ac5499224db646James Dongstatus_t PreviewPlayer::setAudioPlayer(VideoEditorAudioPlayer *audioPlayer) {
38800f742c087d92e5452d29be1fb668022b5f8a6c7James Dong    ALOGV("setAudioPlayer");
3891c97d9ab52a288d24ea54499de435277cc1a3d68Rajneesh Chowdury    Mutex::Autolock autoLock(mLock);
3901c97d9ab52a288d24ea54499de435277cc1a3d68Rajneesh Chowdury    CHECK(!(mFlags & PLAYING));
3911c97d9ab52a288d24ea54499de435277cc1a3d68Rajneesh Chowdury    mAudioPlayer = audioPlayer;
3921c97d9ab52a288d24ea54499de435277cc1a3d68Rajneesh Chowdury
3932703f23af496c13cfa39cc7e157fa12d1cb4c169Steve Block    ALOGV("SetAudioPlayer");
3941c97d9ab52a288d24ea54499de435277cc1a3d68Rajneesh Chowdury    mIsChangeSourceRequired = true;
3951c97d9ab52a288d24ea54499de435277cc1a3d68Rajneesh Chowdury
3961c97d9ab52a288d24ea54499de435277cc1a3d68Rajneesh Chowdury    // check if the new and old source are dummy
397727f9e1e8e1306af761765e480ac5499224db646James Dong    sp<MediaSource> anAudioSource = mAudioPlayer->getSource();
3981c97d9ab52a288d24ea54499de435277cc1a3d68Rajneesh Chowdury    if (anAudioSource == NULL) {
3991c97d9ab52a288d24ea54499de435277cc1a3d68Rajneesh Chowdury        // Audio player does not have any source set.
4002703f23af496c13cfa39cc7e157fa12d1cb4c169Steve Block        ALOGV("setAudioPlayer: Audio player does not have any source set");
4011c97d9ab52a288d24ea54499de435277cc1a3d68Rajneesh Chowdury        return OK;
4021c97d9ab52a288d24ea54499de435277cc1a3d68Rajneesh Chowdury    }
4031c97d9ab52a288d24ea54499de435277cc1a3d68Rajneesh Chowdury
4049a8c9a848da5a4a06c5402d6716efa66b8818d37Rajneesh Chowdury    // If new video source is not dummy, then always change source
4059a8c9a848da5a4a06c5402d6716efa66b8818d37Rajneesh Chowdury    // Else audio player continues using old audio source and there are
4069a8c9a848da5a4a06c5402d6716efa66b8818d37Rajneesh Chowdury    // frame drops to maintain AV sync
4079a8c9a848da5a4a06c5402d6716efa66b8818d37Rajneesh Chowdury    sp<MetaData> meta;
4089a8c9a848da5a4a06c5402d6716efa66b8818d37Rajneesh Chowdury    if (mVideoSource != NULL) {
4099a8c9a848da5a4a06c5402d6716efa66b8818d37Rajneesh Chowdury        meta = mVideoSource->getFormat();
4109a8c9a848da5a4a06c5402d6716efa66b8818d37Rajneesh Chowdury        const char *pVidSrcType;
4119a8c9a848da5a4a06c5402d6716efa66b8818d37Rajneesh Chowdury        if (meta->findCString(kKeyDecoderComponent, &pVidSrcType)) {
4129a8c9a848da5a4a06c5402d6716efa66b8818d37Rajneesh Chowdury            if (strcmp(pVidSrcType, "DummyVideoSource") != 0) {
4132703f23af496c13cfa39cc7e157fa12d1cb4c169Steve Block                ALOGV(" Video clip with silent audio; need to change source");
4149a8c9a848da5a4a06c5402d6716efa66b8818d37Rajneesh Chowdury                return OK;
4159a8c9a848da5a4a06c5402d6716efa66b8818d37Rajneesh Chowdury            }
4169a8c9a848da5a4a06c5402d6716efa66b8818d37Rajneesh Chowdury        }
4179a8c9a848da5a4a06c5402d6716efa66b8818d37Rajneesh Chowdury    }
4189a8c9a848da5a4a06c5402d6716efa66b8818d37Rajneesh Chowdury
4191c97d9ab52a288d24ea54499de435277cc1a3d68Rajneesh Chowdury    const char *pSrcType1;
4201c97d9ab52a288d24ea54499de435277cc1a3d68Rajneesh Chowdury    const char *pSrcType2;
4219a8c9a848da5a4a06c5402d6716efa66b8818d37Rajneesh Chowdury    meta = anAudioSource->getFormat();
4221c97d9ab52a288d24ea54499de435277cc1a3d68Rajneesh Chowdury
4231c97d9ab52a288d24ea54499de435277cc1a3d68Rajneesh Chowdury    if (meta->findCString(kKeyDecoderComponent, &pSrcType1)) {
4241c97d9ab52a288d24ea54499de435277cc1a3d68Rajneesh Chowdury        if (strcmp(pSrcType1, "DummyAudioSource") == 0) {
4251c97d9ab52a288d24ea54499de435277cc1a3d68Rajneesh Chowdury            meta = mAudioSource->getFormat();
4261c97d9ab52a288d24ea54499de435277cc1a3d68Rajneesh Chowdury            if (meta->findCString(kKeyDecoderComponent, &pSrcType2)) {
4271c97d9ab52a288d24ea54499de435277cc1a3d68Rajneesh Chowdury                if (strcmp(pSrcType2, "DummyAudioSource") == 0) {
4281c97d9ab52a288d24ea54499de435277cc1a3d68Rajneesh Chowdury                    mIsChangeSourceRequired = false;
4291c97d9ab52a288d24ea54499de435277cc1a3d68Rajneesh Chowdury                    // Just set the new play duration for the existing source
4301c97d9ab52a288d24ea54499de435277cc1a3d68Rajneesh Chowdury                    MediaSource *pMediaSrc = anAudioSource.get();
4311c97d9ab52a288d24ea54499de435277cc1a3d68Rajneesh Chowdury                    DummyAudioSource *pDummyAudioSource = (DummyAudioSource*)pMediaSrc;
4321c97d9ab52a288d24ea54499de435277cc1a3d68Rajneesh Chowdury                    //Increment the duration of audio source
433ebf40154a222c864a0aaf807702fc4eb6b9cf40fRaghavender Palla                    pDummyAudioSource->setDuration(
434ebf40154a222c864a0aaf807702fc4eb6b9cf40fRaghavender Palla                        (int64_t)((mPlayEndTimeMsec)*1000LL));
435b6e2b5cc5b71986b06cbd9c471b63eb5878a7aeaRajneesh Chowdury
436b6e2b5cc5b71986b06cbd9c471b63eb5878a7aeaRajneesh Chowdury                    // Stop the new audio source
437b6e2b5cc5b71986b06cbd9c471b63eb5878a7aeaRajneesh Chowdury                    // since we continue using old source
4382703f23af496c13cfa39cc7e157fa12d1cb4c169Steve Block                    ALOGV("setAudioPlayer: stop new audio source");
439b6e2b5cc5b71986b06cbd9c471b63eb5878a7aeaRajneesh Chowdury                    mAudioSource->stop();
4401c97d9ab52a288d24ea54499de435277cc1a3d68Rajneesh Chowdury                }
4411c97d9ab52a288d24ea54499de435277cc1a3d68Rajneesh Chowdury            }
4421c97d9ab52a288d24ea54499de435277cc1a3d68Rajneesh Chowdury        }
4431c97d9ab52a288d24ea54499de435277cc1a3d68Rajneesh Chowdury    }
4441c97d9ab52a288d24ea54499de435277cc1a3d68Rajneesh Chowdury
4451c97d9ab52a288d24ea54499de435277cc1a3d68Rajneesh Chowdury    return OK;
4461c97d9ab52a288d24ea54499de435277cc1a3d68Rajneesh Chowdury}
4471c97d9ab52a288d24ea54499de435277cc1a3d68Rajneesh Chowdury
4481c97d9ab52a288d24ea54499de435277cc1a3d68Rajneesh Chowduryvoid PreviewPlayer::onStreamDone() {
44900f742c087d92e5452d29be1fb668022b5f8a6c7James Dong    ALOGV("onStreamDone");
4501c97d9ab52a288d24ea54499de435277cc1a3d68Rajneesh Chowdury    // Posted whenever any stream finishes playing.
4511c97d9ab52a288d24ea54499de435277cc1a3d68Rajneesh Chowdury
4521c97d9ab52a288d24ea54499de435277cc1a3d68Rajneesh Chowdury    Mutex::Autolock autoLock(mLock);
4531c97d9ab52a288d24ea54499de435277cc1a3d68Rajneesh Chowdury    if (!mStreamDoneEventPending) {
4541c97d9ab52a288d24ea54499de435277cc1a3d68Rajneesh Chowdury        return;
4551c97d9ab52a288d24ea54499de435277cc1a3d68Rajneesh Chowdury    }
4561c97d9ab52a288d24ea54499de435277cc1a3d68Rajneesh Chowdury    mStreamDoneEventPending = false;
4571c97d9ab52a288d24ea54499de435277cc1a3d68Rajneesh Chowdury
4581c97d9ab52a288d24ea54499de435277cc1a3d68Rajneesh Chowdury    if (mStreamDoneStatus != ERROR_END_OF_STREAM) {
4592703f23af496c13cfa39cc7e157fa12d1cb4c169Steve Block        ALOGV("MEDIA_ERROR %d", mStreamDoneStatus);
4601c97d9ab52a288d24ea54499de435277cc1a3d68Rajneesh Chowdury
4611c97d9ab52a288d24ea54499de435277cc1a3d68Rajneesh Chowdury        notifyListener_l(
4621c97d9ab52a288d24ea54499de435277cc1a3d68Rajneesh Chowdury                MEDIA_ERROR, MEDIA_ERROR_UNKNOWN, mStreamDoneStatus);
4631c97d9ab52a288d24ea54499de435277cc1a3d68Rajneesh Chowdury
4641c97d9ab52a288d24ea54499de435277cc1a3d68Rajneesh Chowdury        pause_l(true /* at eos */);
4651c97d9ab52a288d24ea54499de435277cc1a3d68Rajneesh Chowdury
4661c97d9ab52a288d24ea54499de435277cc1a3d68Rajneesh Chowdury        mFlags |= AT_EOS;
4671c97d9ab52a288d24ea54499de435277cc1a3d68Rajneesh Chowdury        return;
4681c97d9ab52a288d24ea54499de435277cc1a3d68Rajneesh Chowdury    }
4691c97d9ab52a288d24ea54499de435277cc1a3d68Rajneesh Chowdury
4701c97d9ab52a288d24ea54499de435277cc1a3d68Rajneesh Chowdury    const bool allDone =
4711c97d9ab52a288d24ea54499de435277cc1a3d68Rajneesh Chowdury        (mVideoSource == NULL || (mFlags & VIDEO_AT_EOS))
4721c97d9ab52a288d24ea54499de435277cc1a3d68Rajneesh Chowdury            && (mAudioSource == NULL || (mFlags & AUDIO_AT_EOS));
4731c97d9ab52a288d24ea54499de435277cc1a3d68Rajneesh Chowdury
4741c97d9ab52a288d24ea54499de435277cc1a3d68Rajneesh Chowdury    if (!allDone) {
4751c97d9ab52a288d24ea54499de435277cc1a3d68Rajneesh Chowdury        return;
4761c97d9ab52a288d24ea54499de435277cc1a3d68Rajneesh Chowdury    }
4771c97d9ab52a288d24ea54499de435277cc1a3d68Rajneesh Chowdury
4781c97d9ab52a288d24ea54499de435277cc1a3d68Rajneesh Chowdury    if (mFlags & (LOOPING | AUTO_LOOPING)) {
4791c97d9ab52a288d24ea54499de435277cc1a3d68Rajneesh Chowdury        seekTo_l(0);
4801c97d9ab52a288d24ea54499de435277cc1a3d68Rajneesh Chowdury
4811c97d9ab52a288d24ea54499de435277cc1a3d68Rajneesh Chowdury        if (mVideoSource != NULL) {
4821c97d9ab52a288d24ea54499de435277cc1a3d68Rajneesh Chowdury            postVideoEvent_l();
4831c97d9ab52a288d24ea54499de435277cc1a3d68Rajneesh Chowdury        }
4841c97d9ab52a288d24ea54499de435277cc1a3d68Rajneesh Chowdury    } else {
4852703f23af496c13cfa39cc7e157fa12d1cb4c169Steve Block        ALOGV("MEDIA_PLAYBACK_COMPLETE");
4861c97d9ab52a288d24ea54499de435277cc1a3d68Rajneesh Chowdury        //pause before sending event
4871c97d9ab52a288d24ea54499de435277cc1a3d68Rajneesh Chowdury        pause_l(true /* at eos */);
488fa31daff175709d2c58d61d1354daa9495696631Raghavender Palla
489fa31daff175709d2c58d61d1354daa9495696631Raghavender Palla        //This lock is used to syncronize onStreamDone() in PreviewPlayer and
490fa31daff175709d2c58d61d1354daa9495696631Raghavender Palla        //stopPreview() in PreviewController
491fa31daff175709d2c58d61d1354daa9495696631Raghavender Palla        Mutex::Autolock autoLock(mLockControl);
492e018023b4844fef7880bf5e45f706afcb10c33b6Hong Teng        /* Make sure PreviewPlayer only notifies MEDIA_PLAYBACK_COMPLETE once for each clip!
493e018023b4844fef7880bf5e45f706afcb10c33b6Hong Teng         * It happens twice in following scenario.
494e018023b4844fef7880bf5e45f706afcb10c33b6Hong Teng         * To make the clips in preview storyboard are played and switched smoothly,
495e018023b4844fef7880bf5e45f706afcb10c33b6Hong Teng         * PreviewController uses two PreviewPlayer instances and one AudioPlayer.
496e018023b4844fef7880bf5e45f706afcb10c33b6Hong Teng         * The two PreviewPlayer use the same AudioPlayer to play the audio,
497e018023b4844fef7880bf5e45f706afcb10c33b6Hong Teng         * and change the audio source of the AudioPlayer.
498e018023b4844fef7880bf5e45f706afcb10c33b6Hong Teng         * If the audio source of current playing clip and next clip are dummy
499e018023b4844fef7880bf5e45f706afcb10c33b6Hong Teng         * audio source(image or video without audio), it will not change the audio source
500e018023b4844fef7880bf5e45f706afcb10c33b6Hong Teng         * to avoid the "audio glitch", and keep using the current audio source.
501e018023b4844fef7880bf5e45f706afcb10c33b6Hong Teng         * When the video of current clip reached the EOS, PreviewPlayer will set EOS flag
502e018023b4844fef7880bf5e45f706afcb10c33b6Hong Teng         * for video and audio, and it will notify MEDIA_PLAYBACK_COMPLETE.
503e018023b4844fef7880bf5e45f706afcb10c33b6Hong Teng         * But the audio(dummy audio source) is still playing(for next clip),
504e018023b4844fef7880bf5e45f706afcb10c33b6Hong Teng         * and when it reached the EOS, and video reached EOS,
505e018023b4844fef7880bf5e45f706afcb10c33b6Hong Teng         * PreviewPlayer will notify MEDIA_PLAYBACK_COMPLETE again. */
506e018023b4844fef7880bf5e45f706afcb10c33b6Hong Teng        if (!(mFlags & INFORMED_AV_EOS)) {
507e018023b4844fef7880bf5e45f706afcb10c33b6Hong Teng            notifyListener_l(MEDIA_PLAYBACK_COMPLETE);
508e018023b4844fef7880bf5e45f706afcb10c33b6Hong Teng            mFlags |= INFORMED_AV_EOS;
509e018023b4844fef7880bf5e45f706afcb10c33b6Hong Teng        }
5101c97d9ab52a288d24ea54499de435277cc1a3d68Rajneesh Chowdury        mFlags |= AT_EOS;
5112703f23af496c13cfa39cc7e157fa12d1cb4c169Steve Block        ALOGV("onStreamDone end");
512fa31daff175709d2c58d61d1354daa9495696631Raghavender Palla        return;
5131c97d9ab52a288d24ea54499de435277cc1a3d68Rajneesh Chowdury    }
5141c97d9ab52a288d24ea54499de435277cc1a3d68Rajneesh Chowdury}
5151c97d9ab52a288d24ea54499de435277cc1a3d68Rajneesh Chowdury
5161c97d9ab52a288d24ea54499de435277cc1a3d68Rajneesh Chowdury
517643290dc4c83da23b1b8ff4ed71118203274bb15Dharmaray Kundargistatus_t PreviewPlayer::play_l() {
51800f742c087d92e5452d29be1fb668022b5f8a6c7James Dong    ALOGV("play_l");
5195bc7fb407ce1bab13d4a4a67d34a1a3192ee3186Dheeraj Sharma
5208b95de2c4d57a0a07d7f4c59f2ddd52a140a361eRajneesh Chowdury    mFlags &= ~SEEK_PREVIEW;
5218b95de2c4d57a0a07d7f4c59f2ddd52a140a361eRajneesh Chowdury
522643290dc4c83da23b1b8ff4ed71118203274bb15Dharmaray Kundargi    if (mFlags & PLAYING) {
523643290dc4c83da23b1b8ff4ed71118203274bb15Dharmaray Kundargi        return OK;
524643290dc4c83da23b1b8ff4ed71118203274bb15Dharmaray Kundargi    }
525643290dc4c83da23b1b8ff4ed71118203274bb15Dharmaray Kundargi    mStartNextPlayer = false;
526643290dc4c83da23b1b8ff4ed71118203274bb15Dharmaray Kundargi
527643290dc4c83da23b1b8ff4ed71118203274bb15Dharmaray Kundargi    if (!(mFlags & PREPARED)) {
528643290dc4c83da23b1b8ff4ed71118203274bb15Dharmaray Kundargi        status_t err = prepare_l();
529643290dc4c83da23b1b8ff4ed71118203274bb15Dharmaray Kundargi
530643290dc4c83da23b1b8ff4ed71118203274bb15Dharmaray Kundargi        if (err != OK) {
531643290dc4c83da23b1b8ff4ed71118203274bb15Dharmaray Kundargi            return err;
532643290dc4c83da23b1b8ff4ed71118203274bb15Dharmaray Kundargi        }
533643290dc4c83da23b1b8ff4ed71118203274bb15Dharmaray Kundargi    }
534643290dc4c83da23b1b8ff4ed71118203274bb15Dharmaray Kundargi
535643290dc4c83da23b1b8ff4ed71118203274bb15Dharmaray Kundargi    mFlags |= PLAYING;
536643290dc4c83da23b1b8ff4ed71118203274bb15Dharmaray Kundargi    mFlags |= FIRST_FRAME;
537643290dc4c83da23b1b8ff4ed71118203274bb15Dharmaray Kundargi
538643290dc4c83da23b1b8ff4ed71118203274bb15Dharmaray Kundargi    bool deferredAudioSeek = false;
539643290dc4c83da23b1b8ff4ed71118203274bb15Dharmaray Kundargi
540643290dc4c83da23b1b8ff4ed71118203274bb15Dharmaray Kundargi    if (mAudioSource != NULL) {
541643290dc4c83da23b1b8ff4ed71118203274bb15Dharmaray Kundargi        if (mAudioPlayer == NULL) {
542643290dc4c83da23b1b8ff4ed71118203274bb15Dharmaray Kundargi            if (mAudioSink != NULL) {
543643290dc4c83da23b1b8ff4ed71118203274bb15Dharmaray Kundargi
544643290dc4c83da23b1b8ff4ed71118203274bb15Dharmaray Kundargi                mAudioPlayer = new VideoEditorAudioPlayer(mAudioSink, this);
545643290dc4c83da23b1b8ff4ed71118203274bb15Dharmaray Kundargi                mAudioPlayer->setSource(mAudioSource);
546643290dc4c83da23b1b8ff4ed71118203274bb15Dharmaray Kundargi
547727f9e1e8e1306af761765e480ac5499224db646James Dong                mAudioPlayer->setAudioMixSettings(
548643290dc4c83da23b1b8ff4ed71118203274bb15Dharmaray Kundargi                 mPreviewPlayerAudioMixSettings);
549643290dc4c83da23b1b8ff4ed71118203274bb15Dharmaray Kundargi
550727f9e1e8e1306af761765e480ac5499224db646James Dong                mAudioPlayer->setAudioMixPCMFileHandle(
551643290dc4c83da23b1b8ff4ed71118203274bb15Dharmaray Kundargi                 mAudioMixPCMFileHandle);
552643290dc4c83da23b1b8ff4ed71118203274bb15Dharmaray Kundargi
553727f9e1e8e1306af761765e480ac5499224db646James Dong                mAudioPlayer->setAudioMixStoryBoardSkimTimeStamp(
554643290dc4c83da23b1b8ff4ed71118203274bb15Dharmaray Kundargi                 mAudioMixStoryBoardTS, mCurrentMediaBeginCutTime,
555643290dc4c83da23b1b8ff4ed71118203274bb15Dharmaray Kundargi                 mCurrentMediaVolumeValue);
556643290dc4c83da23b1b8ff4ed71118203274bb15Dharmaray Kundargi
5571c97d9ab52a288d24ea54499de435277cc1a3d68Rajneesh Chowdury                 mFlags |= AUDIOPLAYER_STARTED;
5581c97d9ab52a288d24ea54499de435277cc1a3d68Rajneesh Chowdury                // We've already started the MediaSource in order to enable
5591c97d9ab52a288d24ea54499de435277cc1a3d68Rajneesh Chowdury                // the prefetcher to read its data.
560727f9e1e8e1306af761765e480ac5499224db646James Dong                status_t err = mAudioPlayer->start(
5611c97d9ab52a288d24ea54499de435277cc1a3d68Rajneesh Chowdury                        true /* sourceAlreadyStarted */);
562643290dc4c83da23b1b8ff4ed71118203274bb15Dharmaray Kundargi
5631c97d9ab52a288d24ea54499de435277cc1a3d68Rajneesh Chowdury                if (err != OK) {
5641c97d9ab52a288d24ea54499de435277cc1a3d68Rajneesh Chowdury                    //delete mAudioPlayer;
5651c97d9ab52a288d24ea54499de435277cc1a3d68Rajneesh Chowdury                    mAudioPlayer = NULL;
5661c97d9ab52a288d24ea54499de435277cc1a3d68Rajneesh Chowdury
5671c97d9ab52a288d24ea54499de435277cc1a3d68Rajneesh Chowdury                    mFlags &= ~(PLAYING | FIRST_FRAME);
5681c97d9ab52a288d24ea54499de435277cc1a3d68Rajneesh Chowdury                    return err;
5691c97d9ab52a288d24ea54499de435277cc1a3d68Rajneesh Chowdury                }
5701c97d9ab52a288d24ea54499de435277cc1a3d68Rajneesh Chowdury
571727f9e1e8e1306af761765e480ac5499224db646James Dong                mTimeSource = mAudioPlayer;
5721c97d9ab52a288d24ea54499de435277cc1a3d68Rajneesh Chowdury                mFlags |= AUDIO_RUNNING;
573643290dc4c83da23b1b8ff4ed71118203274bb15Dharmaray Kundargi                deferredAudioSeek = true;
574643290dc4c83da23b1b8ff4ed71118203274bb15Dharmaray Kundargi                mWatchForAudioSeekComplete = false;
575643290dc4c83da23b1b8ff4ed71118203274bb15Dharmaray Kundargi                mWatchForAudioEOS = true;
576643290dc4c83da23b1b8ff4ed71118203274bb15Dharmaray Kundargi            }
5771c97d9ab52a288d24ea54499de435277cc1a3d68Rajneesh Chowdury        } else {
578727f9e1e8e1306af761765e480ac5499224db646James Dong            bool isAudioPlayerStarted = mAudioPlayer->isStarted();
5795bc7fb407ce1bab13d4a4a67d34a1a3192ee3186Dheeraj Sharma
5801c97d9ab52a288d24ea54499de435277cc1a3d68Rajneesh Chowdury            if (mIsChangeSourceRequired == true) {
5812703f23af496c13cfa39cc7e157fa12d1cb4c169Steve Block                ALOGV("play_l: Change audio source required");
582643290dc4c83da23b1b8ff4ed71118203274bb15Dharmaray Kundargi
5831c97d9ab52a288d24ea54499de435277cc1a3d68Rajneesh Chowdury                if (isAudioPlayerStarted == true) {
584727f9e1e8e1306af761765e480ac5499224db646James Dong                    mAudioPlayer->pause();
5851c97d9ab52a288d24ea54499de435277cc1a3d68Rajneesh Chowdury                }
5865bc7fb407ce1bab13d4a4a67d34a1a3192ee3186Dheeraj Sharma
587727f9e1e8e1306af761765e480ac5499224db646James Dong                mAudioPlayer->setSource(mAudioSource);
588727f9e1e8e1306af761765e480ac5499224db646James Dong                mAudioPlayer->setObserver(this);
5891c97d9ab52a288d24ea54499de435277cc1a3d68Rajneesh Chowdury
590727f9e1e8e1306af761765e480ac5499224db646James Dong                mAudioPlayer->setAudioMixSettings(
5911c97d9ab52a288d24ea54499de435277cc1a3d68Rajneesh Chowdury                 mPreviewPlayerAudioMixSettings);
5921c97d9ab52a288d24ea54499de435277cc1a3d68Rajneesh Chowdury
593727f9e1e8e1306af761765e480ac5499224db646James Dong                mAudioPlayer->setAudioMixStoryBoardSkimTimeStamp(
5941c97d9ab52a288d24ea54499de435277cc1a3d68Rajneesh Chowdury                    mAudioMixStoryBoardTS, mCurrentMediaBeginCutTime,
5951c97d9ab52a288d24ea54499de435277cc1a3d68Rajneesh Chowdury                    mCurrentMediaVolumeValue);
5961c97d9ab52a288d24ea54499de435277cc1a3d68Rajneesh Chowdury
5971c97d9ab52a288d24ea54499de435277cc1a3d68Rajneesh Chowdury                if (isAudioPlayerStarted == true) {
598727f9e1e8e1306af761765e480ac5499224db646James Dong                    mAudioPlayer->resume();
5991c97d9ab52a288d24ea54499de435277cc1a3d68Rajneesh Chowdury                } else {
6001c97d9ab52a288d24ea54499de435277cc1a3d68Rajneesh Chowdury                    status_t err = OK;
601727f9e1e8e1306af761765e480ac5499224db646James Dong                    err = mAudioPlayer->start(true);
6021c97d9ab52a288d24ea54499de435277cc1a3d68Rajneesh Chowdury                    if (err != OK) {
6031c97d9ab52a288d24ea54499de435277cc1a3d68Rajneesh Chowdury                        mAudioPlayer = NULL;
604727f9e1e8e1306af761765e480ac5499224db646James Dong                        mAudioPlayer = NULL;
6051c97d9ab52a288d24ea54499de435277cc1a3d68Rajneesh Chowdury
6061c97d9ab52a288d24ea54499de435277cc1a3d68Rajneesh Chowdury                        mFlags &= ~(PLAYING | FIRST_FRAME);
6071c97d9ab52a288d24ea54499de435277cc1a3d68Rajneesh Chowdury                        return err;
6081c97d9ab52a288d24ea54499de435277cc1a3d68Rajneesh Chowdury                    }
6091c97d9ab52a288d24ea54499de435277cc1a3d68Rajneesh Chowdury                }
6101c97d9ab52a288d24ea54499de435277cc1a3d68Rajneesh Chowdury            } else {
6112703f23af496c13cfa39cc7e157fa12d1cb4c169Steve Block                ALOGV("play_l: No Source change required");
612727f9e1e8e1306af761765e480ac5499224db646James Dong                mAudioPlayer->setAudioMixStoryBoardSkimTimeStamp(
6131c97d9ab52a288d24ea54499de435277cc1a3d68Rajneesh Chowdury                    mAudioMixStoryBoardTS, mCurrentMediaBeginCutTime,
6141c97d9ab52a288d24ea54499de435277cc1a3d68Rajneesh Chowdury                    mCurrentMediaVolumeValue);
6151c97d9ab52a288d24ea54499de435277cc1a3d68Rajneesh Chowdury
616727f9e1e8e1306af761765e480ac5499224db646James Dong                mAudioPlayer->resume();
6175bc7fb407ce1bab13d4a4a67d34a1a3192ee3186Dheeraj Sharma            }
6181c97d9ab52a288d24ea54499de435277cc1a3d68Rajneesh Chowdury
6191c97d9ab52a288d24ea54499de435277cc1a3d68Rajneesh Chowdury            mFlags |= AUDIOPLAYER_STARTED;
6201c97d9ab52a288d24ea54499de435277cc1a3d68Rajneesh Chowdury            mFlags |= AUDIO_RUNNING;
621727f9e1e8e1306af761765e480ac5499224db646James Dong            mTimeSource = mAudioPlayer;
6221c97d9ab52a288d24ea54499de435277cc1a3d68Rajneesh Chowdury            deferredAudioSeek = true;
6231c97d9ab52a288d24ea54499de435277cc1a3d68Rajneesh Chowdury            mWatchForAudioSeekComplete = false;
6241c97d9ab52a288d24ea54499de435277cc1a3d68Rajneesh Chowdury            mWatchForAudioEOS = true;
6255bc7fb407ce1bab13d4a4a67d34a1a3192ee3186Dheeraj Sharma        }
626643290dc4c83da23b1b8ff4ed71118203274bb15Dharmaray Kundargi    }
627643290dc4c83da23b1b8ff4ed71118203274bb15Dharmaray Kundargi
628643290dc4c83da23b1b8ff4ed71118203274bb15Dharmaray Kundargi    if (mTimeSource == NULL && mAudioPlayer == NULL) {
629643290dc4c83da23b1b8ff4ed71118203274bb15Dharmaray Kundargi        mTimeSource = &mSystemTimeSource;
630643290dc4c83da23b1b8ff4ed71118203274bb15Dharmaray Kundargi    }
631643290dc4c83da23b1b8ff4ed71118203274bb15Dharmaray Kundargi
63253c567c3355255ed12dd9f5d5a1c58f821770329Dharmaray Kundargi    // Set the seek option for Image source files and read.
63353c567c3355255ed12dd9f5d5a1c58f821770329Dharmaray Kundargi    // This resets the timestamping for image play
63453c567c3355255ed12dd9f5d5a1c58f821770329Dharmaray Kundargi    if (mIsVideoSourceJpg) {
63553c567c3355255ed12dd9f5d5a1c58f821770329Dharmaray Kundargi        MediaSource::ReadOptions options;
63653c567c3355255ed12dd9f5d5a1c58f821770329Dharmaray Kundargi        MediaBuffer *aLocalBuffer;
63753c567c3355255ed12dd9f5d5a1c58f821770329Dharmaray Kundargi        options.setSeekTo(mSeekTimeUs);
63853c567c3355255ed12dd9f5d5a1c58f821770329Dharmaray Kundargi        mVideoSource->read(&aLocalBuffer, &options);
6394ca3e5df4b809210b240cd7d0489922017d87a6dSantosh Madhava        aLocalBuffer->release();
64053c567c3355255ed12dd9f5d5a1c58f821770329Dharmaray Kundargi    }
64153c567c3355255ed12dd9f5d5a1c58f821770329Dharmaray Kundargi
642643290dc4c83da23b1b8ff4ed71118203274bb15Dharmaray Kundargi    if (mVideoSource != NULL) {
643643290dc4c83da23b1b8ff4ed71118203274bb15Dharmaray Kundargi        // Kick off video playback
644643290dc4c83da23b1b8ff4ed71118203274bb15Dharmaray Kundargi        postVideoEvent_l();
645643290dc4c83da23b1b8ff4ed71118203274bb15Dharmaray Kundargi    }
646643290dc4c83da23b1b8ff4ed71118203274bb15Dharmaray Kundargi
647643290dc4c83da23b1b8ff4ed71118203274bb15Dharmaray Kundargi    if (deferredAudioSeek) {
648643290dc4c83da23b1b8ff4ed71118203274bb15Dharmaray Kundargi        // If there was a seek request while we were paused
649643290dc4c83da23b1b8ff4ed71118203274bb15Dharmaray Kundargi        // and we're just starting up again, honor the request now.
650643290dc4c83da23b1b8ff4ed71118203274bb15Dharmaray Kundargi        seekAudioIfNecessary_l();
651643290dc4c83da23b1b8ff4ed71118203274bb15Dharmaray Kundargi    }
652643290dc4c83da23b1b8ff4ed71118203274bb15Dharmaray Kundargi
653643290dc4c83da23b1b8ff4ed71118203274bb15Dharmaray Kundargi    if (mFlags & AT_EOS) {
654643290dc4c83da23b1b8ff4ed71118203274bb15Dharmaray Kundargi        // Legacy behaviour, if a stream finishes playing and then
655643290dc4c83da23b1b8ff4ed71118203274bb15Dharmaray Kundargi        // is started again, we play from the start...
656643290dc4c83da23b1b8ff4ed71118203274bb15Dharmaray Kundargi        seekTo_l(0);
657643290dc4c83da23b1b8ff4ed71118203274bb15Dharmaray Kundargi    }
658643290dc4c83da23b1b8ff4ed71118203274bb15Dharmaray Kundargi
659643290dc4c83da23b1b8ff4ed71118203274bb15Dharmaray Kundargi    return OK;
660643290dc4c83da23b1b8ff4ed71118203274bb15Dharmaray Kundargi}
661643290dc4c83da23b1b8ff4ed71118203274bb15Dharmaray Kundargi
662643290dc4c83da23b1b8ff4ed71118203274bb15Dharmaray Kundargi
663bfece17c8f63de4d4b686da50f8ecac2f6f8ec53Santosh Madhavastatus_t PreviewPlayer::initRenderer_l() {
6646e2242974c109e85bac50550970fe84009c76f77Mathias Agopian    if (mSurface != NULL) {
665643290dc4c83da23b1b8ff4ed71118203274bb15Dharmaray Kundargi        if(mVideoRenderer == NULL) {
66643fcc396614a587851e2b7c4cea2876ec58b8648Chih-Chung Chang            mVideoRenderer = mNativeWindowRenderer->createRenderInput();
66743fcc396614a587851e2b7c4cea2876ec58b8648Chih-Chung Chang            if (mVideoSource != NULL) {
66843fcc396614a587851e2b7c4cea2876ec58b8648Chih-Chung Chang                updateSizeToRender(mVideoSource->getFormat());
669bfece17c8f63de4d4b686da50f8ecac2f6f8ec53Santosh Madhava            }
670643290dc4c83da23b1b8ff4ed71118203274bb15Dharmaray Kundargi        }
671643290dc4c83da23b1b8ff4ed71118203274bb15Dharmaray Kundargi    }
672bfece17c8f63de4d4b686da50f8ecac2f6f8ec53Santosh Madhava    return OK;
673643290dc4c83da23b1b8ff4ed71118203274bb15Dharmaray Kundargi}
674643290dc4c83da23b1b8ff4ed71118203274bb15Dharmaray Kundargi
675643290dc4c83da23b1b8ff4ed71118203274bb15Dharmaray Kundargi
676643290dc4c83da23b1b8ff4ed71118203274bb15Dharmaray Kundargistatus_t PreviewPlayer::seekTo(int64_t timeUs) {
67700f742c087d92e5452d29be1fb668022b5f8a6c7James Dong    Mutex::Autolock autoLock(mLock);
678643290dc4c83da23b1b8ff4ed71118203274bb15Dharmaray Kundargi    if ((mExtractorFlags & MediaExtractor::CAN_SEEK) || (mIsVideoSourceJpg)) {
679643290dc4c83da23b1b8ff4ed71118203274bb15Dharmaray Kundargi        return seekTo_l(timeUs);
680643290dc4c83da23b1b8ff4ed71118203274bb15Dharmaray Kundargi    }
681643290dc4c83da23b1b8ff4ed71118203274bb15Dharmaray Kundargi
682643290dc4c83da23b1b8ff4ed71118203274bb15Dharmaray Kundargi    return OK;
683643290dc4c83da23b1b8ff4ed71118203274bb15Dharmaray Kundargi}
684643290dc4c83da23b1b8ff4ed71118203274bb15Dharmaray Kundargi
685643290dc4c83da23b1b8ff4ed71118203274bb15Dharmaray Kundargi
686643290dc4c83da23b1b8ff4ed71118203274bb15Dharmaray Kundargistatus_t PreviewPlayer::getVideoDimensions(
687643290dc4c83da23b1b8ff4ed71118203274bb15Dharmaray Kundargi        int32_t *width, int32_t *height) const {
688643290dc4c83da23b1b8ff4ed71118203274bb15Dharmaray Kundargi    Mutex::Autolock autoLock(mLock);
689643290dc4c83da23b1b8ff4ed71118203274bb15Dharmaray Kundargi
690643290dc4c83da23b1b8ff4ed71118203274bb15Dharmaray Kundargi    if (mVideoWidth < 0 || mVideoHeight < 0) {
691643290dc4c83da23b1b8ff4ed71118203274bb15Dharmaray Kundargi        return UNKNOWN_ERROR;
692643290dc4c83da23b1b8ff4ed71118203274bb15Dharmaray Kundargi    }
693643290dc4c83da23b1b8ff4ed71118203274bb15Dharmaray Kundargi
694643290dc4c83da23b1b8ff4ed71118203274bb15Dharmaray Kundargi    *width = mVideoWidth;
695643290dc4c83da23b1b8ff4ed71118203274bb15Dharmaray Kundargi    *height = mVideoHeight;
696643290dc4c83da23b1b8ff4ed71118203274bb15Dharmaray Kundargi
697643290dc4c83da23b1b8ff4ed71118203274bb15Dharmaray Kundargi    return OK;
698643290dc4c83da23b1b8ff4ed71118203274bb15Dharmaray Kundargi}
699643290dc4c83da23b1b8ff4ed71118203274bb15Dharmaray Kundargi
700643290dc4c83da23b1b8ff4ed71118203274bb15Dharmaray Kundargi
70100f742c087d92e5452d29be1fb668022b5f8a6c7James Dongstatus_t PreviewPlayer::initAudioDecoder_l() {
702643290dc4c83da23b1b8ff4ed71118203274bb15Dharmaray Kundargi    sp<MetaData> meta = mAudioTrack->getFormat();
703643290dc4c83da23b1b8ff4ed71118203274bb15Dharmaray Kundargi    const char *mime;
704643290dc4c83da23b1b8ff4ed71118203274bb15Dharmaray Kundargi    CHECK(meta->findCString(kKeyMIMEType, &mime));
705643290dc4c83da23b1b8ff4ed71118203274bb15Dharmaray Kundargi
706643290dc4c83da23b1b8ff4ed71118203274bb15Dharmaray Kundargi    if (!strcasecmp(mime, MEDIA_MIMETYPE_AUDIO_RAW)) {
707643290dc4c83da23b1b8ff4ed71118203274bb15Dharmaray Kundargi        mAudioSource = mAudioTrack;
708643290dc4c83da23b1b8ff4ed71118203274bb15Dharmaray Kundargi    } else {
709643290dc4c83da23b1b8ff4ed71118203274bb15Dharmaray Kundargi        sp<MediaSource> aRawSource;
710643290dc4c83da23b1b8ff4ed71118203274bb15Dharmaray Kundargi        aRawSource = OMXCodec::Create(
711643290dc4c83da23b1b8ff4ed71118203274bb15Dharmaray Kundargi                mClient.interface(), mAudioTrack->getFormat(),
712643290dc4c83da23b1b8ff4ed71118203274bb15Dharmaray Kundargi                false, // createEncoder
713643290dc4c83da23b1b8ff4ed71118203274bb15Dharmaray Kundargi                mAudioTrack);
714643290dc4c83da23b1b8ff4ed71118203274bb15Dharmaray Kundargi
715643290dc4c83da23b1b8ff4ed71118203274bb15Dharmaray Kundargi        if(aRawSource != NULL) {
716643290dc4c83da23b1b8ff4ed71118203274bb15Dharmaray Kundargi            mAudioSource = new VideoEditorSRC(aRawSource);
717643290dc4c83da23b1b8ff4ed71118203274bb15Dharmaray Kundargi        }
718643290dc4c83da23b1b8ff4ed71118203274bb15Dharmaray Kundargi    }
719643290dc4c83da23b1b8ff4ed71118203274bb15Dharmaray Kundargi
720643290dc4c83da23b1b8ff4ed71118203274bb15Dharmaray Kundargi    if (mAudioSource != NULL) {
721643290dc4c83da23b1b8ff4ed71118203274bb15Dharmaray Kundargi        int64_t durationUs;
722643290dc4c83da23b1b8ff4ed71118203274bb15Dharmaray Kundargi        if (mAudioTrack->getFormat()->findInt64(kKeyDuration, &durationUs)) {
72300f742c087d92e5452d29be1fb668022b5f8a6c7James Dong            setDuration_l(durationUs);
724643290dc4c83da23b1b8ff4ed71118203274bb15Dharmaray Kundargi        }
725643290dc4c83da23b1b8ff4ed71118203274bb15Dharmaray Kundargi        status_t err = mAudioSource->start();
726643290dc4c83da23b1b8ff4ed71118203274bb15Dharmaray Kundargi
727643290dc4c83da23b1b8ff4ed71118203274bb15Dharmaray Kundargi        if (err != OK) {
728643290dc4c83da23b1b8ff4ed71118203274bb15Dharmaray Kundargi            mAudioSource.clear();
729643290dc4c83da23b1b8ff4ed71118203274bb15Dharmaray Kundargi            return err;
730643290dc4c83da23b1b8ff4ed71118203274bb15Dharmaray Kundargi        }
731643290dc4c83da23b1b8ff4ed71118203274bb15Dharmaray Kundargi    } else if (!strcasecmp(mime, MEDIA_MIMETYPE_AUDIO_QCELP)) {
732643290dc4c83da23b1b8ff4ed71118203274bb15Dharmaray Kundargi        // For legacy reasons we're simply going to ignore the absence
733643290dc4c83da23b1b8ff4ed71118203274bb15Dharmaray Kundargi        // of an audio decoder for QCELP instead of aborting playback
734643290dc4c83da23b1b8ff4ed71118203274bb15Dharmaray Kundargi        // altogether.
735643290dc4c83da23b1b8ff4ed71118203274bb15Dharmaray Kundargi        return OK;
736643290dc4c83da23b1b8ff4ed71118203274bb15Dharmaray Kundargi    }
737643290dc4c83da23b1b8ff4ed71118203274bb15Dharmaray Kundargi
738643290dc4c83da23b1b8ff4ed71118203274bb15Dharmaray Kundargi    return mAudioSource != NULL ? OK : UNKNOWN_ERROR;
739643290dc4c83da23b1b8ff4ed71118203274bb15Dharmaray Kundargi}
740643290dc4c83da23b1b8ff4ed71118203274bb15Dharmaray Kundargi
74100f742c087d92e5452d29be1fb668022b5f8a6c7James Dongstatus_t PreviewPlayer::initVideoDecoder_l(uint32_t flags) {
74243fcc396614a587851e2b7c4cea2876ec58b8648Chih-Chung Chang    initRenderer_l();
74343fcc396614a587851e2b7c4cea2876ec58b8648Chih-Chung Chang
74443fcc396614a587851e2b7c4cea2876ec58b8648Chih-Chung Chang    if (mVideoRenderer == NULL) {
745f8bd29c66308c820d579b3efb0942a53a277e2c5Steve Block        ALOGE("Cannot create renderer");
74643fcc396614a587851e2b7c4cea2876ec58b8648Chih-Chung Chang        return UNKNOWN_ERROR;
74743fcc396614a587851e2b7c4cea2876ec58b8648Chih-Chung Chang    }
74843fcc396614a587851e2b7c4cea2876ec58b8648Chih-Chung Chang
749643290dc4c83da23b1b8ff4ed71118203274bb15Dharmaray Kundargi    mVideoSource = OMXCodec::Create(
750643290dc4c83da23b1b8ff4ed71118203274bb15Dharmaray Kundargi            mClient.interface(), mVideoTrack->getFormat(),
751643290dc4c83da23b1b8ff4ed71118203274bb15Dharmaray Kundargi            false,
752643290dc4c83da23b1b8ff4ed71118203274bb15Dharmaray Kundargi            mVideoTrack,
75343fcc396614a587851e2b7c4cea2876ec58b8648Chih-Chung Chang            NULL, flags, mVideoRenderer->getTargetWindow());
754643290dc4c83da23b1b8ff4ed71118203274bb15Dharmaray Kundargi
755643290dc4c83da23b1b8ff4ed71118203274bb15Dharmaray Kundargi    if (mVideoSource != NULL) {
756643290dc4c83da23b1b8ff4ed71118203274bb15Dharmaray Kundargi        int64_t durationUs;
757643290dc4c83da23b1b8ff4ed71118203274bb15Dharmaray Kundargi        if (mVideoTrack->getFormat()->findInt64(kKeyDuration, &durationUs)) {
75800f742c087d92e5452d29be1fb668022b5f8a6c7James Dong            setDuration_l(durationUs);
759643290dc4c83da23b1b8ff4ed71118203274bb15Dharmaray Kundargi        }
760643290dc4c83da23b1b8ff4ed71118203274bb15Dharmaray Kundargi
76143fcc396614a587851e2b7c4cea2876ec58b8648Chih-Chung Chang        updateSizeToRender(mVideoTrack->getFormat());
76235cb2de64cb6482a08f446e80733e7d344a0dcacDharmaray Kundargi
763643290dc4c83da23b1b8ff4ed71118203274bb15Dharmaray Kundargi        status_t err = mVideoSource->start();
764643290dc4c83da23b1b8ff4ed71118203274bb15Dharmaray Kundargi
765643290dc4c83da23b1b8ff4ed71118203274bb15Dharmaray Kundargi        if (err != OK) {
766643290dc4c83da23b1b8ff4ed71118203274bb15Dharmaray Kundargi            mVideoSource.clear();
767643290dc4c83da23b1b8ff4ed71118203274bb15Dharmaray Kundargi            return err;
768643290dc4c83da23b1b8ff4ed71118203274bb15Dharmaray Kundargi        }
769643290dc4c83da23b1b8ff4ed71118203274bb15Dharmaray Kundargi    }
770643290dc4c83da23b1b8ff4ed71118203274bb15Dharmaray Kundargi
771643290dc4c83da23b1b8ff4ed71118203274bb15Dharmaray Kundargi    return mVideoSource != NULL ? OK : UNKNOWN_ERROR;
772643290dc4c83da23b1b8ff4ed71118203274bb15Dharmaray Kundargi}
773643290dc4c83da23b1b8ff4ed71118203274bb15Dharmaray Kundargi
774643290dc4c83da23b1b8ff4ed71118203274bb15Dharmaray Kundargi
775643290dc4c83da23b1b8ff4ed71118203274bb15Dharmaray Kundargivoid PreviewPlayer::onVideoEvent() {
776643290dc4c83da23b1b8ff4ed71118203274bb15Dharmaray Kundargi    uint32_t i=0;
777643290dc4c83da23b1b8ff4ed71118203274bb15Dharmaray Kundargi    M4OSA_ERR err1 = M4NO_ERROR;
778643290dc4c83da23b1b8ff4ed71118203274bb15Dharmaray Kundargi    int64_t imageFrameTimeUs = 0;
779643290dc4c83da23b1b8ff4ed71118203274bb15Dharmaray Kundargi
780643290dc4c83da23b1b8ff4ed71118203274bb15Dharmaray Kundargi    Mutex::Autolock autoLock(mLock);
781643290dc4c83da23b1b8ff4ed71118203274bb15Dharmaray Kundargi    if (!mVideoEventPending) {
782643290dc4c83da23b1b8ff4ed71118203274bb15Dharmaray Kundargi        // The event has been cancelled in reset_l() but had already
783643290dc4c83da23b1b8ff4ed71118203274bb15Dharmaray Kundargi        // been scheduled for execution at that time.
784643290dc4c83da23b1b8ff4ed71118203274bb15Dharmaray Kundargi        return;
785643290dc4c83da23b1b8ff4ed71118203274bb15Dharmaray Kundargi    }
786643290dc4c83da23b1b8ff4ed71118203274bb15Dharmaray Kundargi    mVideoEventPending = false;
787643290dc4c83da23b1b8ff4ed71118203274bb15Dharmaray Kundargi
7889e4c36a044e264c644d7fa9f9894659a6fc47a6fAndreas Huber    if (mFlags & SEEK_PREVIEW) {
7899e4c36a044e264c644d7fa9f9894659a6fc47a6fAndreas Huber        mFlags &= ~SEEK_PREVIEW;
7909e4c36a044e264c644d7fa9f9894659a6fc47a6fAndreas Huber        return;
7919e4c36a044e264c644d7fa9f9894659a6fc47a6fAndreas Huber    }
7929e4c36a044e264c644d7fa9f9894659a6fc47a6fAndreas Huber
793643290dc4c83da23b1b8ff4ed71118203274bb15Dharmaray Kundargi    TimeSource *ts_st =  &mSystemTimeSource;
794643290dc4c83da23b1b8ff4ed71118203274bb15Dharmaray Kundargi    int64_t timeStartUs = ts_st->getRealTimeUs();
795643290dc4c83da23b1b8ff4ed71118203274bb15Dharmaray Kundargi
796a5872f770c50da1db555d53e062a9896ea4321d5Andreas Huber    if (mSeeking != NO_SEEK) {
797643290dc4c83da23b1b8ff4ed71118203274bb15Dharmaray Kundargi
798643290dc4c83da23b1b8ff4ed71118203274bb15Dharmaray Kundargi        if(mAudioSource != NULL) {
799643290dc4c83da23b1b8ff4ed71118203274bb15Dharmaray Kundargi
800643290dc4c83da23b1b8ff4ed71118203274bb15Dharmaray Kundargi            // We're going to seek the video source first, followed by
801643290dc4c83da23b1b8ff4ed71118203274bb15Dharmaray Kundargi            // the audio source.
802643290dc4c83da23b1b8ff4ed71118203274bb15Dharmaray Kundargi            // In order to avoid jumps in the DataSource offset caused by
803643290dc4c83da23b1b8ff4ed71118203274bb15Dharmaray Kundargi            // the audio codec prefetching data from the old locations
804643290dc4c83da23b1b8ff4ed71118203274bb15Dharmaray Kundargi            // while the video codec is already reading data from the new
805643290dc4c83da23b1b8ff4ed71118203274bb15Dharmaray Kundargi            // locations, we'll "pause" the audio source, causing it to
806643290dc4c83da23b1b8ff4ed71118203274bb15Dharmaray Kundargi            // stop reading input data until a subsequent seek.
807643290dc4c83da23b1b8ff4ed71118203274bb15Dharmaray Kundargi
8085bc7fb407ce1bab13d4a4a67d34a1a3192ee3186Dheeraj Sharma            if (mAudioPlayer != NULL && (mFlags & AUDIO_RUNNING)) {
809643290dc4c83da23b1b8ff4ed71118203274bb15Dharmaray Kundargi                mAudioPlayer->pause();
8105bc7fb407ce1bab13d4a4a67d34a1a3192ee3186Dheeraj Sharma                mFlags &= ~AUDIO_RUNNING;
811643290dc4c83da23b1b8ff4ed71118203274bb15Dharmaray Kundargi            }
812643290dc4c83da23b1b8ff4ed71118203274bb15Dharmaray Kundargi            mAudioSource->pause();
813643290dc4c83da23b1b8ff4ed71118203274bb15Dharmaray Kundargi        }
814643290dc4c83da23b1b8ff4ed71118203274bb15Dharmaray Kundargi    }
815643290dc4c83da23b1b8ff4ed71118203274bb15Dharmaray Kundargi
816643290dc4c83da23b1b8ff4ed71118203274bb15Dharmaray Kundargi    if (!mVideoBuffer) {
817643290dc4c83da23b1b8ff4ed71118203274bb15Dharmaray Kundargi        MediaSource::ReadOptions options;
818a5872f770c50da1db555d53e062a9896ea4321d5Andreas Huber        if (mSeeking != NO_SEEK) {
8192703f23af496c13cfa39cc7e157fa12d1cb4c169Steve Block            ALOGV("LV PLAYER seeking to %lld us (%.2f secs)", mSeekTimeUs,
820643290dc4c83da23b1b8ff4ed71118203274bb15Dharmaray Kundargi                                                      mSeekTimeUs / 1E6);
821643290dc4c83da23b1b8ff4ed71118203274bb15Dharmaray Kundargi
822643290dc4c83da23b1b8ff4ed71118203274bb15Dharmaray Kundargi            options.setSeekTo(
823643290dc4c83da23b1b8ff4ed71118203274bb15Dharmaray Kundargi                    mSeekTimeUs, MediaSource::ReadOptions::SEEK_CLOSEST);
824643290dc4c83da23b1b8ff4ed71118203274bb15Dharmaray Kundargi        }
825643290dc4c83da23b1b8ff4ed71118203274bb15Dharmaray Kundargi        for (;;) {
82643fcc396614a587851e2b7c4cea2876ec58b8648Chih-Chung Chang            status_t err = mVideoSource->read(&mVideoBuffer, &options);
827643290dc4c83da23b1b8ff4ed71118203274bb15Dharmaray Kundargi            options.clearSeekTo();
828643290dc4c83da23b1b8ff4ed71118203274bb15Dharmaray Kundargi
829643290dc4c83da23b1b8ff4ed71118203274bb15Dharmaray Kundargi            if (err != OK) {
83000f742c087d92e5452d29be1fb668022b5f8a6c7James Dong                CHECK(!mVideoBuffer);
831643290dc4c83da23b1b8ff4ed71118203274bb15Dharmaray Kundargi
832643290dc4c83da23b1b8ff4ed71118203274bb15Dharmaray Kundargi                if (err == INFO_FORMAT_CHANGED) {
8332703f23af496c13cfa39cc7e157fa12d1cb4c169Steve Block                    ALOGV("LV PLAYER VideoSource signalled format change");
834643290dc4c83da23b1b8ff4ed71118203274bb15Dharmaray Kundargi                    notifyVideoSize_l();
835643290dc4c83da23b1b8ff4ed71118203274bb15Dharmaray Kundargi
836643290dc4c83da23b1b8ff4ed71118203274bb15Dharmaray Kundargi                    if (mVideoRenderer != NULL) {
837643290dc4c83da23b1b8ff4ed71118203274bb15Dharmaray Kundargi                        mVideoRendererIsPreview = false;
838bfece17c8f63de4d4b686da50f8ecac2f6f8ec53Santosh Madhava                        err = initRenderer_l();
8394ca3e5df4b809210b240cd7d0489922017d87a6dSantosh Madhava                        if (err != OK) {
8404ca3e5df4b809210b240cd7d0489922017d87a6dSantosh Madhava                            postStreamDoneEvent_l(err);
8414ca3e5df4b809210b240cd7d0489922017d87a6dSantosh Madhava                        }
842bfece17c8f63de4d4b686da50f8ecac2f6f8ec53Santosh Madhava
843643290dc4c83da23b1b8ff4ed71118203274bb15Dharmaray Kundargi                    }
84443fcc396614a587851e2b7c4cea2876ec58b8648Chih-Chung Chang
84543fcc396614a587851e2b7c4cea2876ec58b8648Chih-Chung Chang                    updateSizeToRender(mVideoSource->getFormat());
846643290dc4c83da23b1b8ff4ed71118203274bb15Dharmaray Kundargi                    continue;
847643290dc4c83da23b1b8ff4ed71118203274bb15Dharmaray Kundargi                }
848643290dc4c83da23b1b8ff4ed71118203274bb15Dharmaray Kundargi                // So video playback is complete, but we may still have
849342f932f6312be84a48512216b561ead42b53199Santosh Madhava                // a seek request pending that needs to be applied to the audio track
850a5872f770c50da1db555d53e062a9896ea4321d5Andreas Huber                if (mSeeking != NO_SEEK) {
8512703f23af496c13cfa39cc7e157fa12d1cb4c169Steve Block                    ALOGV("video stream ended while seeking!");
852342f932f6312be84a48512216b561ead42b53199Santosh Madhava                }
853342f932f6312be84a48512216b561ead42b53199Santosh Madhava                finishSeekIfNecessary(-1);
8542703f23af496c13cfa39cc7e157fa12d1cb4c169Steve Block                ALOGV("PreviewPlayer: onVideoEvent EOS reached.");
855643290dc4c83da23b1b8ff4ed71118203274bb15Dharmaray Kundargi                mFlags |= VIDEO_AT_EOS;
8561c97d9ab52a288d24ea54499de435277cc1a3d68Rajneesh Chowdury                mFlags |= AUDIO_AT_EOS;
8574f4efef8357f4d8b23ccfb1b29db34175bf72627Dheeraj Sharma                mOverlayUpdateEventPosted = false;
858643290dc4c83da23b1b8ff4ed71118203274bb15Dharmaray Kundargi                postStreamDoneEvent_l(err);
859b2d6e0f74a12e5cce5b429e646172c63346346c3Santosh Madhava                // Set the last decoded timestamp to duration
860ebf40154a222c864a0aaf807702fc4eb6b9cf40fRaghavender Palla                mDecodedVideoTs = (mPlayEndTimeMsec*1000LL);
861643290dc4c83da23b1b8ff4ed71118203274bb15Dharmaray Kundargi                return;
862643290dc4c83da23b1b8ff4ed71118203274bb15Dharmaray Kundargi            }
863643290dc4c83da23b1b8ff4ed71118203274bb15Dharmaray Kundargi
864643290dc4c83da23b1b8ff4ed71118203274bb15Dharmaray Kundargi            if (mVideoBuffer->range_length() == 0) {
865643290dc4c83da23b1b8ff4ed71118203274bb15Dharmaray Kundargi                // Some decoders, notably the PV AVC software decoder
866643290dc4c83da23b1b8ff4ed71118203274bb15Dharmaray Kundargi                // return spurious empty buffers that we just want to ignore.
867643290dc4c83da23b1b8ff4ed71118203274bb15Dharmaray Kundargi
868643290dc4c83da23b1b8ff4ed71118203274bb15Dharmaray Kundargi                mVideoBuffer->release();
869643290dc4c83da23b1b8ff4ed71118203274bb15Dharmaray Kundargi                mVideoBuffer = NULL;
870643290dc4c83da23b1b8ff4ed71118203274bb15Dharmaray Kundargi                continue;
871643290dc4c83da23b1b8ff4ed71118203274bb15Dharmaray Kundargi            }
872643290dc4c83da23b1b8ff4ed71118203274bb15Dharmaray Kundargi
873643290dc4c83da23b1b8ff4ed71118203274bb15Dharmaray Kundargi            int64_t videoTimeUs;
874643290dc4c83da23b1b8ff4ed71118203274bb15Dharmaray Kundargi            CHECK(mVideoBuffer->meta_data()->findInt64(kKeyTime, &videoTimeUs));
875643290dc4c83da23b1b8ff4ed71118203274bb15Dharmaray Kundargi
876a5872f770c50da1db555d53e062a9896ea4321d5Andreas Huber            if (mSeeking != NO_SEEK) {
877b2d6e0f74a12e5cce5b429e646172c63346346c3Santosh Madhava                if (videoTimeUs < mSeekTimeUs) {
878b2d6e0f74a12e5cce5b429e646172c63346346c3Santosh Madhava                    // buffers are before seek time
879b2d6e0f74a12e5cce5b429e646172c63346346c3Santosh Madhava                    // ignore them
880b2d6e0f74a12e5cce5b429e646172c63346346c3Santosh Madhava                    mVideoBuffer->release();
881b2d6e0f74a12e5cce5b429e646172c63346346c3Santosh Madhava                    mVideoBuffer = NULL;
882b2d6e0f74a12e5cce5b429e646172c63346346c3Santosh Madhava                    continue;
883b2d6e0f74a12e5cce5b429e646172c63346346c3Santosh Madhava                }
884b2d6e0f74a12e5cce5b429e646172c63346346c3Santosh Madhava            } else {
885b2d6e0f74a12e5cce5b429e646172c63346346c3Santosh Madhava                if((videoTimeUs/1000) < mPlayBeginTimeMsec) {
886b2d6e0f74a12e5cce5b429e646172c63346346c3Santosh Madhava                    // Frames are before begin cut time
887b2d6e0f74a12e5cce5b429e646172c63346346c3Santosh Madhava                    // Donot render
888b2d6e0f74a12e5cce5b429e646172c63346346c3Santosh Madhava                    mVideoBuffer->release();
889b2d6e0f74a12e5cce5b429e646172c63346346c3Santosh Madhava                    mVideoBuffer = NULL;
890b2d6e0f74a12e5cce5b429e646172c63346346c3Santosh Madhava                    continue;
891b2d6e0f74a12e5cce5b429e646172c63346346c3Santosh Madhava                }
892643290dc4c83da23b1b8ff4ed71118203274bb15Dharmaray Kundargi            }
893643290dc4c83da23b1b8ff4ed71118203274bb15Dharmaray Kundargi            break;
894643290dc4c83da23b1b8ff4ed71118203274bb15Dharmaray Kundargi        }
895643290dc4c83da23b1b8ff4ed71118203274bb15Dharmaray Kundargi    }
896643290dc4c83da23b1b8ff4ed71118203274bb15Dharmaray Kundargi
897643290dc4c83da23b1b8ff4ed71118203274bb15Dharmaray Kundargi    mNumberDecVideoFrames++;
898643290dc4c83da23b1b8ff4ed71118203274bb15Dharmaray Kundargi
899643290dc4c83da23b1b8ff4ed71118203274bb15Dharmaray Kundargi    int64_t timeUs;
900643290dc4c83da23b1b8ff4ed71118203274bb15Dharmaray Kundargi    CHECK(mVideoBuffer->meta_data()->findInt64(kKeyTime, &timeUs));
90100f742c087d92e5452d29be1fb668022b5f8a6c7James Dong    setPosition_l(timeUs);
902643290dc4c83da23b1b8ff4ed71118203274bb15Dharmaray Kundargi
90300f742c087d92e5452d29be1fb668022b5f8a6c7James Dong    if (!mStartNextPlayer) {
90400f742c087d92e5452d29be1fb668022b5f8a6c7James Dong        int64_t playbackTimeRemaining = (mPlayEndTimeMsec * 1000LL) - timeUs;
90500f742c087d92e5452d29be1fb668022b5f8a6c7James Dong        if (playbackTimeRemaining <= 1500000) {
906643290dc4c83da23b1b8ff4ed71118203274bb15Dharmaray Kundargi            //When less than 1.5 sec of playback left
907643290dc4c83da23b1b8ff4ed71118203274bb15Dharmaray Kundargi            // send notification to start next player
908643290dc4c83da23b1b8ff4ed71118203274bb15Dharmaray Kundargi
909643290dc4c83da23b1b8ff4ed71118203274bb15Dharmaray Kundargi            mStartNextPlayer = true;
910643290dc4c83da23b1b8ff4ed71118203274bb15Dharmaray Kundargi            notifyListener_l(0xAAAAAAAA);
911643290dc4c83da23b1b8ff4ed71118203274bb15Dharmaray Kundargi        }
912643290dc4c83da23b1b8ff4ed71118203274bb15Dharmaray Kundargi    }
913643290dc4c83da23b1b8ff4ed71118203274bb15Dharmaray Kundargi
914a5872f770c50da1db555d53e062a9896ea4321d5Andreas Huber    SeekType wasSeeking = mSeeking;
915643290dc4c83da23b1b8ff4ed71118203274bb15Dharmaray Kundargi    finishSeekIfNecessary(timeUs);
9165bc7fb407ce1bab13d4a4a67d34a1a3192ee3186Dheeraj Sharma    if (mAudioPlayer != NULL && !(mFlags & (AUDIO_RUNNING))) {
9175bc7fb407ce1bab13d4a4a67d34a1a3192ee3186Dheeraj Sharma        status_t err = startAudioPlayer_l();
9185bc7fb407ce1bab13d4a4a67d34a1a3192ee3186Dheeraj Sharma        if (err != OK) {
919f8bd29c66308c820d579b3efb0942a53a277e2c5Steve Block            ALOGE("Starting the audio player failed w/ err %d", err);
9205bc7fb407ce1bab13d4a4a67d34a1a3192ee3186Dheeraj Sharma            return;
9215bc7fb407ce1bab13d4a4a67d34a1a3192ee3186Dheeraj Sharma        }
9225bc7fb407ce1bab13d4a4a67d34a1a3192ee3186Dheeraj Sharma    }
923643290dc4c83da23b1b8ff4ed71118203274bb15Dharmaray Kundargi
924643290dc4c83da23b1b8ff4ed71118203274bb15Dharmaray Kundargi    TimeSource *ts = (mFlags & AUDIO_AT_EOS) ? &mSystemTimeSource : mTimeSource;
925643290dc4c83da23b1b8ff4ed71118203274bb15Dharmaray Kundargi
926643290dc4c83da23b1b8ff4ed71118203274bb15Dharmaray Kundargi    if(ts == NULL) {
927643290dc4c83da23b1b8ff4ed71118203274bb15Dharmaray Kundargi        mVideoBuffer->release();
928643290dc4c83da23b1b8ff4ed71118203274bb15Dharmaray Kundargi        mVideoBuffer = NULL;
929643290dc4c83da23b1b8ff4ed71118203274bb15Dharmaray Kundargi        return;
930643290dc4c83da23b1b8ff4ed71118203274bb15Dharmaray Kundargi    }
931643290dc4c83da23b1b8ff4ed71118203274bb15Dharmaray Kundargi
932643290dc4c83da23b1b8ff4ed71118203274bb15Dharmaray Kundargi    if(!mIsVideoSourceJpg) {
933643290dc4c83da23b1b8ff4ed71118203274bb15Dharmaray Kundargi        if (mFlags & FIRST_FRAME) {
934643290dc4c83da23b1b8ff4ed71118203274bb15Dharmaray Kundargi            mFlags &= ~FIRST_FRAME;
935643290dc4c83da23b1b8ff4ed71118203274bb15Dharmaray Kundargi
936643290dc4c83da23b1b8ff4ed71118203274bb15Dharmaray Kundargi            mTimeSourceDeltaUs = ts->getRealTimeUs() - timeUs;
937643290dc4c83da23b1b8ff4ed71118203274bb15Dharmaray Kundargi        }
938643290dc4c83da23b1b8ff4ed71118203274bb15Dharmaray Kundargi
939643290dc4c83da23b1b8ff4ed71118203274bb15Dharmaray Kundargi        int64_t realTimeUs, mediaTimeUs;
940643290dc4c83da23b1b8ff4ed71118203274bb15Dharmaray Kundargi        if (!(mFlags & AUDIO_AT_EOS) && mAudioPlayer != NULL
941643290dc4c83da23b1b8ff4ed71118203274bb15Dharmaray Kundargi            && mAudioPlayer->getMediaTimeMapping(&realTimeUs, &mediaTimeUs)) {
942643290dc4c83da23b1b8ff4ed71118203274bb15Dharmaray Kundargi            mTimeSourceDeltaUs = realTimeUs - mediaTimeUs;
943643290dc4c83da23b1b8ff4ed71118203274bb15Dharmaray Kundargi        }
944643290dc4c83da23b1b8ff4ed71118203274bb15Dharmaray Kundargi
945643290dc4c83da23b1b8ff4ed71118203274bb15Dharmaray Kundargi        int64_t nowUs = ts->getRealTimeUs() - mTimeSourceDeltaUs;
946643290dc4c83da23b1b8ff4ed71118203274bb15Dharmaray Kundargi
947643290dc4c83da23b1b8ff4ed71118203274bb15Dharmaray Kundargi        int64_t latenessUs = nowUs - timeUs;
948643290dc4c83da23b1b8ff4ed71118203274bb15Dharmaray Kundargi
949a5872f770c50da1db555d53e062a9896ea4321d5Andreas Huber        if (wasSeeking != NO_SEEK) {
950342f932f6312be84a48512216b561ead42b53199Santosh Madhava            // Let's display the first frame after seeking right away.
951342f932f6312be84a48512216b561ead42b53199Santosh Madhava            latenessUs = 0;
952342f932f6312be84a48512216b561ead42b53199Santosh Madhava        }
9532703f23af496c13cfa39cc7e157fa12d1cb4c169Steve Block        ALOGV("Audio time stamp = %lld and video time stamp = %lld",
954643290dc4c83da23b1b8ff4ed71118203274bb15Dharmaray Kundargi                                            ts->getRealTimeUs(),timeUs);
955643290dc4c83da23b1b8ff4ed71118203274bb15Dharmaray Kundargi        if (latenessUs > 40000) {
956643290dc4c83da23b1b8ff4ed71118203274bb15Dharmaray Kundargi            // We're more than 40ms late.
957643290dc4c83da23b1b8ff4ed71118203274bb15Dharmaray Kundargi
9582703f23af496c13cfa39cc7e157fa12d1cb4c169Steve Block            ALOGV("LV PLAYER we're late by %lld us (%.2f secs)",
959643290dc4c83da23b1b8ff4ed71118203274bb15Dharmaray Kundargi                                           latenessUs, latenessUs / 1E6);
960643290dc4c83da23b1b8ff4ed71118203274bb15Dharmaray Kundargi
961643290dc4c83da23b1b8ff4ed71118203274bb15Dharmaray Kundargi            mVideoBuffer->release();
962643290dc4c83da23b1b8ff4ed71118203274bb15Dharmaray Kundargi            mVideoBuffer = NULL;
9634f155f0cffa2414545854e899dff4861187e1f68Dharmaray Kundargi            postVideoEvent_l(0);
964643290dc4c83da23b1b8ff4ed71118203274bb15Dharmaray Kundargi            return;
965643290dc4c83da23b1b8ff4ed71118203274bb15Dharmaray Kundargi        }
966643290dc4c83da23b1b8ff4ed71118203274bb15Dharmaray Kundargi
9674f155f0cffa2414545854e899dff4861187e1f68Dharmaray Kundargi        if (latenessUs < -25000) {
9684f155f0cffa2414545854e899dff4861187e1f68Dharmaray Kundargi            // We're more than 25ms early.
9692703f23af496c13cfa39cc7e157fa12d1cb4c169Steve Block            ALOGV("We're more than 25ms early, lateness %lld", latenessUs);
970643290dc4c83da23b1b8ff4ed71118203274bb15Dharmaray Kundargi
9714f155f0cffa2414545854e899dff4861187e1f68Dharmaray Kundargi            postVideoEvent_l(25000);
972643290dc4c83da23b1b8ff4ed71118203274bb15Dharmaray Kundargi            return;
973643290dc4c83da23b1b8ff4ed71118203274bb15Dharmaray Kundargi        }
974643290dc4c83da23b1b8ff4ed71118203274bb15Dharmaray Kundargi    }
975643290dc4c83da23b1b8ff4ed71118203274bb15Dharmaray Kundargi
976643290dc4c83da23b1b8ff4ed71118203274bb15Dharmaray Kundargi    if (mVideoRendererIsPreview || mVideoRenderer == NULL) {
977643290dc4c83da23b1b8ff4ed71118203274bb15Dharmaray Kundargi        mVideoRendererIsPreview = false;
978643290dc4c83da23b1b8ff4ed71118203274bb15Dharmaray Kundargi
979bfece17c8f63de4d4b686da50f8ecac2f6f8ec53Santosh Madhava        status_t err = initRenderer_l();
9804ca3e5df4b809210b240cd7d0489922017d87a6dSantosh Madhava        if (err != OK) {
9814ca3e5df4b809210b240cd7d0489922017d87a6dSantosh Madhava            postStreamDoneEvent_l(err);
9824ca3e5df4b809210b240cd7d0489922017d87a6dSantosh Madhava        }
983643290dc4c83da23b1b8ff4ed71118203274bb15Dharmaray Kundargi    }
984643290dc4c83da23b1b8ff4ed71118203274bb15Dharmaray Kundargi
985643290dc4c83da23b1b8ff4ed71118203274bb15Dharmaray Kundargi    // If timestamp exceeds endCutTime of clip, donot render
986643290dc4c83da23b1b8ff4ed71118203274bb15Dharmaray Kundargi    if((timeUs/1000) > mPlayEndTimeMsec) {
987cece4b32d340f0ca514160647b9883e3777bc376Chih-Chung Chang        mVideoBuffer->release();
988643290dc4c83da23b1b8ff4ed71118203274bb15Dharmaray Kundargi        mVideoBuffer = NULL;
989643290dc4c83da23b1b8ff4ed71118203274bb15Dharmaray Kundargi        mFlags |= VIDEO_AT_EOS;
990643290dc4c83da23b1b8ff4ed71118203274bb15Dharmaray Kundargi        mFlags |= AUDIO_AT_EOS;
9912703f23af496c13cfa39cc7e157fa12d1cb4c169Steve Block        ALOGV("PreviewPlayer: onVideoEvent timeUs > mPlayEndTime; send EOS..");
9924f4efef8357f4d8b23ccfb1b29db34175bf72627Dheeraj Sharma        mOverlayUpdateEventPosted = false;
993fe288aa71450ebe04eb74d06a3ea887c6009aacbSantosh Madhava        // Set the last decoded timestamp to duration
994ebf40154a222c864a0aaf807702fc4eb6b9cf40fRaghavender Palla        mDecodedVideoTs = (mPlayEndTimeMsec*1000LL);
995643290dc4c83da23b1b8ff4ed71118203274bb15Dharmaray Kundargi        postStreamDoneEvent_l(ERROR_END_OF_STREAM);
996643290dc4c83da23b1b8ff4ed71118203274bb15Dharmaray Kundargi        return;
997643290dc4c83da23b1b8ff4ed71118203274bb15Dharmaray Kundargi    }
998b2d6e0f74a12e5cce5b429e646172c63346346c3Santosh Madhava    // Capture the frame timestamp to be rendered
999b2d6e0f74a12e5cce5b429e646172c63346346c3Santosh Madhava    mDecodedVideoTs = timeUs;
1000643290dc4c83da23b1b8ff4ed71118203274bb15Dharmaray Kundargi
1001643290dc4c83da23b1b8ff4ed71118203274bb15Dharmaray Kundargi    // Post processing to apply video effects
1002643290dc4c83da23b1b8ff4ed71118203274bb15Dharmaray Kundargi    for(i=0;i<mNumberEffects;i++) {
1003643290dc4c83da23b1b8ff4ed71118203274bb15Dharmaray Kundargi        // First check if effect starttime matches the clip being previewed
1004643290dc4c83da23b1b8ff4ed71118203274bb15Dharmaray Kundargi        if((mEffectsSettings[i].uiStartTime < (mDecVideoTsStoryBoard/1000)) ||
1005643290dc4c83da23b1b8ff4ed71118203274bb15Dharmaray Kundargi        (mEffectsSettings[i].uiStartTime >=
1006643290dc4c83da23b1b8ff4ed71118203274bb15Dharmaray Kundargi         ((mDecVideoTsStoryBoard/1000) + mPlayEndTimeMsec - mPlayBeginTimeMsec)))
1007643290dc4c83da23b1b8ff4ed71118203274bb15Dharmaray Kundargi        {
1008643290dc4c83da23b1b8ff4ed71118203274bb15Dharmaray Kundargi            // This effect doesn't belong to this clip, check next one
1009643290dc4c83da23b1b8ff4ed71118203274bb15Dharmaray Kundargi            continue;
1010643290dc4c83da23b1b8ff4ed71118203274bb15Dharmaray Kundargi        }
1011643290dc4c83da23b1b8ff4ed71118203274bb15Dharmaray Kundargi        // Check if effect applies to this particular frame timestamp
1012643290dc4c83da23b1b8ff4ed71118203274bb15Dharmaray Kundargi        if((mEffectsSettings[i].uiStartTime <=
1013643290dc4c83da23b1b8ff4ed71118203274bb15Dharmaray Kundargi         (((timeUs+mDecVideoTsStoryBoard)/1000)-mPlayBeginTimeMsec)) &&
1014643290dc4c83da23b1b8ff4ed71118203274bb15Dharmaray Kundargi            ((mEffectsSettings[i].uiStartTime+mEffectsSettings[i].uiDuration) >=
1015643290dc4c83da23b1b8ff4ed71118203274bb15Dharmaray Kundargi             (((timeUs+mDecVideoTsStoryBoard)/1000)-mPlayBeginTimeMsec))
1016643290dc4c83da23b1b8ff4ed71118203274bb15Dharmaray Kundargi              && (mEffectsSettings[i].uiDuration != 0)) {
1017643290dc4c83da23b1b8ff4ed71118203274bb15Dharmaray Kundargi            setVideoPostProcessingNode(
1018643290dc4c83da23b1b8ff4ed71118203274bb15Dharmaray Kundargi             mEffectsSettings[i].VideoEffectType, TRUE);
1019643290dc4c83da23b1b8ff4ed71118203274bb15Dharmaray Kundargi        }
1020643290dc4c83da23b1b8ff4ed71118203274bb15Dharmaray Kundargi        else {
1021643290dc4c83da23b1b8ff4ed71118203274bb15Dharmaray Kundargi            setVideoPostProcessingNode(
1022643290dc4c83da23b1b8ff4ed71118203274bb15Dharmaray Kundargi             mEffectsSettings[i].VideoEffectType, FALSE);
1023643290dc4c83da23b1b8ff4ed71118203274bb15Dharmaray Kundargi        }
1024e6c0750cd2927967f93a8811954f889d23fadb6eDharmaray Kundargi    }
1025643290dc4c83da23b1b8ff4ed71118203274bb15Dharmaray Kundargi
1026e6c0750cd2927967f93a8811954f889d23fadb6eDharmaray Kundargi    //Provide the overlay Update indication when there is an overlay effect
1027d01ef568f96253fe5859526bb67b13fc5e17b544Dharmaray Kundargi    if (mCurrentVideoEffect & VIDEO_EFFECT_FRAMING) {
1028d01ef568f96253fe5859526bb67b13fc5e17b544Dharmaray Kundargi        mCurrentVideoEffect &= ~VIDEO_EFFECT_FRAMING; //never apply framing here.
1029e6c0750cd2927967f93a8811954f889d23fadb6eDharmaray Kundargi        if (!mOverlayUpdateEventPosted) {
1030e6c0750cd2927967f93a8811954f889d23fadb6eDharmaray Kundargi            // Find the effect in effectSettings array
1031408e25b2508d97f7ae0b39acf46ab42b7c223c44Basavapatna Dattaguru            M4OSA_UInt32 index;
1032e6c0750cd2927967f93a8811954f889d23fadb6eDharmaray Kundargi            for (index = 0; index < mNumberEffects; index++) {
1033e6c0750cd2927967f93a8811954f889d23fadb6eDharmaray Kundargi                M4OSA_UInt32 timeMs = mDecodedVideoTs/1000;
1034e6c0750cd2927967f93a8811954f889d23fadb6eDharmaray Kundargi                M4OSA_UInt32 timeOffset = mDecVideoTsStoryBoard/1000;
1035e6c0750cd2927967f93a8811954f889d23fadb6eDharmaray Kundargi                if(mEffectsSettings[index].VideoEffectType ==
1036408e25b2508d97f7ae0b39acf46ab42b7c223c44Basavapatna Dattaguru                    (M4VSS3GPP_VideoEffectType)M4xVSS_kVideoEffectType_Framing) {
1037254c8dfe514fd45eef251763639f63615379570fDharmaray Kundargi                    if (((mEffectsSettings[index].uiStartTime + 1) <=
1038254c8dfe514fd45eef251763639f63615379570fDharmaray Kundargi                        timeMs + timeOffset - mPlayBeginTimeMsec) &&
1039e6c0750cd2927967f93a8811954f889d23fadb6eDharmaray Kundargi                        ((mEffectsSettings[index].uiStartTime - 1 +
1040254c8dfe514fd45eef251763639f63615379570fDharmaray Kundargi                        mEffectsSettings[index].uiDuration) >=
1041254c8dfe514fd45eef251763639f63615379570fDharmaray Kundargi                        timeMs + timeOffset - mPlayBeginTimeMsec))
1042e6c0750cd2927967f93a8811954f889d23fadb6eDharmaray Kundargi                    {
1043e6c0750cd2927967f93a8811954f889d23fadb6eDharmaray Kundargi                        break;
1044e6c0750cd2927967f93a8811954f889d23fadb6eDharmaray Kundargi                    }
1045e6c0750cd2927967f93a8811954f889d23fadb6eDharmaray Kundargi                }
1046e6c0750cd2927967f93a8811954f889d23fadb6eDharmaray Kundargi            }
1047e6c0750cd2927967f93a8811954f889d23fadb6eDharmaray Kundargi            if (index < mNumberEffects) {
1048e6c0750cd2927967f93a8811954f889d23fadb6eDharmaray Kundargi                mCurrFramingEffectIndex = index;
1049e6c0750cd2927967f93a8811954f889d23fadb6eDharmaray Kundargi                mOverlayUpdateEventPosted = true;
1050e6c0750cd2927967f93a8811954f889d23fadb6eDharmaray Kundargi                postOverlayUpdateEvent_l();
105100f742c087d92e5452d29be1fb668022b5f8a6c7James Dong                ALOGV("Framing index = %ld", mCurrFramingEffectIndex);
1052e6c0750cd2927967f93a8811954f889d23fadb6eDharmaray Kundargi            } else {
10532703f23af496c13cfa39cc7e157fa12d1cb4c169Steve Block                ALOGV("No framing effects found");
1054e6c0750cd2927967f93a8811954f889d23fadb6eDharmaray Kundargi            }
1055e6c0750cd2927967f93a8811954f889d23fadb6eDharmaray Kundargi        }
1056e6c0750cd2927967f93a8811954f889d23fadb6eDharmaray Kundargi
1057e6c0750cd2927967f93a8811954f889d23fadb6eDharmaray Kundargi    } else if (mOverlayUpdateEventPosted) {
1058e6c0750cd2927967f93a8811954f889d23fadb6eDharmaray Kundargi        //Post the event when the overlay is no more valid
10592703f23af496c13cfa39cc7e157fa12d1cb4c169Steve Block        ALOGV("Overlay is Done");
1060e6c0750cd2927967f93a8811954f889d23fadb6eDharmaray Kundargi        mOverlayUpdateEventPosted = false;
1061e6c0750cd2927967f93a8811954f889d23fadb6eDharmaray Kundargi        postOverlayUpdateEvent_l();
1062643290dc4c83da23b1b8ff4ed71118203274bb15Dharmaray Kundargi    }
1063643290dc4c83da23b1b8ff4ed71118203274bb15Dharmaray Kundargi
1064643290dc4c83da23b1b8ff4ed71118203274bb15Dharmaray Kundargi    if (mVideoRenderer != NULL) {
106543fcc396614a587851e2b7c4cea2876ec58b8648Chih-Chung Chang        mVideoRenderer->render(mVideoBuffer, mCurrentVideoEffect,
106643fcc396614a587851e2b7c4cea2876ec58b8648Chih-Chung Chang                mRenderingMode, mIsVideoSourceJpg);
1067643290dc4c83da23b1b8ff4ed71118203274bb15Dharmaray Kundargi    }
1068643290dc4c83da23b1b8ff4ed71118203274bb15Dharmaray Kundargi
1069cece4b32d340f0ca514160647b9883e3777bc376Chih-Chung Chang    mVideoBuffer->release();
1070643290dc4c83da23b1b8ff4ed71118203274bb15Dharmaray Kundargi    mVideoBuffer = NULL;
1071643290dc4c83da23b1b8ff4ed71118203274bb15Dharmaray Kundargi
1072643290dc4c83da23b1b8ff4ed71118203274bb15Dharmaray Kundargi    // Post progress callback based on callback interval set
1073643290dc4c83da23b1b8ff4ed71118203274bb15Dharmaray Kundargi    if(mNumberDecVideoFrames >= mProgressCbInterval) {
1074643290dc4c83da23b1b8ff4ed71118203274bb15Dharmaray Kundargi        postProgressCallbackEvent_l();
1075643290dc4c83da23b1b8ff4ed71118203274bb15Dharmaray Kundargi        mNumberDecVideoFrames = 0;  // reset counter
1076643290dc4c83da23b1b8ff4ed71118203274bb15Dharmaray Kundargi    }
1077643290dc4c83da23b1b8ff4ed71118203274bb15Dharmaray Kundargi
1078643290dc4c83da23b1b8ff4ed71118203274bb15Dharmaray Kundargi    // if reached EndCutTime of clip, post EOS event
1079643290dc4c83da23b1b8ff4ed71118203274bb15Dharmaray Kundargi    if((timeUs/1000) >= mPlayEndTimeMsec) {
10802703f23af496c13cfa39cc7e157fa12d1cb4c169Steve Block        ALOGV("PreviewPlayer: onVideoEvent EOS.");
1081643290dc4c83da23b1b8ff4ed71118203274bb15Dharmaray Kundargi        mFlags |= VIDEO_AT_EOS;
1082643290dc4c83da23b1b8ff4ed71118203274bb15Dharmaray Kundargi        mFlags |= AUDIO_AT_EOS;
10834f4efef8357f4d8b23ccfb1b29db34175bf72627Dheeraj Sharma        mOverlayUpdateEventPosted = false;
1084fe288aa71450ebe04eb74d06a3ea887c6009aacbSantosh Madhava        // Set the last decoded timestamp to duration
1085ebf40154a222c864a0aaf807702fc4eb6b9cf40fRaghavender Palla        mDecodedVideoTs = (mPlayEndTimeMsec*1000LL);
1086643290dc4c83da23b1b8ff4ed71118203274bb15Dharmaray Kundargi        postStreamDoneEvent_l(ERROR_END_OF_STREAM);
1087643290dc4c83da23b1b8ff4ed71118203274bb15Dharmaray Kundargi    }
1088643290dc4c83da23b1b8ff4ed71118203274bb15Dharmaray Kundargi    else {
1089a5872f770c50da1db555d53e062a9896ea4321d5Andreas Huber        if ((wasSeeking != NO_SEEK) && (mFlags & SEEK_PREVIEW)) {
10901c97d9ab52a288d24ea54499de435277cc1a3d68Rajneesh Chowdury            mFlags &= ~SEEK_PREVIEW;
10911c97d9ab52a288d24ea54499de435277cc1a3d68Rajneesh Chowdury            return;
10921c97d9ab52a288d24ea54499de435277cc1a3d68Rajneesh Chowdury        }
10931c97d9ab52a288d24ea54499de435277cc1a3d68Rajneesh Chowdury
1094643290dc4c83da23b1b8ff4ed71118203274bb15Dharmaray Kundargi        if(!mIsVideoSourceJpg) {
10954f155f0cffa2414545854e899dff4861187e1f68Dharmaray Kundargi            postVideoEvent_l(0);
1096643290dc4c83da23b1b8ff4ed71118203274bb15Dharmaray Kundargi        }
1097643290dc4c83da23b1b8ff4ed71118203274bb15Dharmaray Kundargi        else {
1098643290dc4c83da23b1b8ff4ed71118203274bb15Dharmaray Kundargi            postVideoEvent_l(33000);
1099643290dc4c83da23b1b8ff4ed71118203274bb15Dharmaray Kundargi        }
1100643290dc4c83da23b1b8ff4ed71118203274bb15Dharmaray Kundargi    }
1101643290dc4c83da23b1b8ff4ed71118203274bb15Dharmaray Kundargi}
1102643290dc4c83da23b1b8ff4ed71118203274bb15Dharmaray Kundargi
1103643290dc4c83da23b1b8ff4ed71118203274bb15Dharmaray Kundargistatus_t PreviewPlayer::prepare() {
110400f742c087d92e5452d29be1fb668022b5f8a6c7James Dong    ALOGV("prepare");
1105643290dc4c83da23b1b8ff4ed71118203274bb15Dharmaray Kundargi    Mutex::Autolock autoLock(mLock);
1106643290dc4c83da23b1b8ff4ed71118203274bb15Dharmaray Kundargi    return prepare_l();
1107643290dc4c83da23b1b8ff4ed71118203274bb15Dharmaray Kundargi}
1108643290dc4c83da23b1b8ff4ed71118203274bb15Dharmaray Kundargi
1109