CameraSourceTimeLapse.h revision 78eff720c86eb6d4e3d45a144df60b2ca464d2d4
165e7e6facda89927cb26594b3b65ae81b3235ebcNipun Kwatra/*
265e7e6facda89927cb26594b3b65ae81b3235ebcNipun Kwatra * Copyright (C) 2010 The Android Open Source Project
365e7e6facda89927cb26594b3b65ae81b3235ebcNipun Kwatra *
465e7e6facda89927cb26594b3b65ae81b3235ebcNipun Kwatra * Licensed under the Apache License, Version 2.0 (the "License");
565e7e6facda89927cb26594b3b65ae81b3235ebcNipun Kwatra * you may not use this file except in compliance with the License.
665e7e6facda89927cb26594b3b65ae81b3235ebcNipun Kwatra * You may obtain a copy of the License at
765e7e6facda89927cb26594b3b65ae81b3235ebcNipun Kwatra *
865e7e6facda89927cb26594b3b65ae81b3235ebcNipun Kwatra *      http://www.apache.org/licenses/LICENSE-2.0
965e7e6facda89927cb26594b3b65ae81b3235ebcNipun Kwatra *
1065e7e6facda89927cb26594b3b65ae81b3235ebcNipun Kwatra * Unless required by applicable law or agreed to in writing, software
1165e7e6facda89927cb26594b3b65ae81b3235ebcNipun Kwatra * distributed under the License is distributed on an "AS IS" BASIS,
1265e7e6facda89927cb26594b3b65ae81b3235ebcNipun Kwatra * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1365e7e6facda89927cb26594b3b65ae81b3235ebcNipun Kwatra * See the License for the specific language governing permissions and
1465e7e6facda89927cb26594b3b65ae81b3235ebcNipun Kwatra * limitations under the License.
1565e7e6facda89927cb26594b3b65ae81b3235ebcNipun Kwatra */
1665e7e6facda89927cb26594b3b65ae81b3235ebcNipun Kwatra
1765e7e6facda89927cb26594b3b65ae81b3235ebcNipun Kwatra#ifndef CAMERA_SOURCE_TIME_LAPSE_H_
1865e7e6facda89927cb26594b3b65ae81b3235ebcNipun Kwatra
1965e7e6facda89927cb26594b3b65ae81b3235ebcNipun Kwatra#define CAMERA_SOURCE_TIME_LAPSE_H_
2065e7e6facda89927cb26594b3b65ae81b3235ebcNipun Kwatra
2165e7e6facda89927cb26594b3b65ae81b3235ebcNipun Kwatra#include <pthread.h>
2265e7e6facda89927cb26594b3b65ae81b3235ebcNipun Kwatra
2365e7e6facda89927cb26594b3b65ae81b3235ebcNipun Kwatra#include <utils/RefBase.h>
2465e7e6facda89927cb26594b3b65ae81b3235ebcNipun Kwatra#include <utils/threads.h>
2565e7e6facda89927cb26594b3b65ae81b3235ebcNipun Kwatra
2665e7e6facda89927cb26594b3b65ae81b3235ebcNipun Kwatranamespace android {
2765e7e6facda89927cb26594b3b65ae81b3235ebcNipun Kwatra
2865e7e6facda89927cb26594b3b65ae81b3235ebcNipun Kwatraclass ICamera;
2965e7e6facda89927cb26594b3b65ae81b3235ebcNipun Kwatraclass IMemory;
3065e7e6facda89927cb26594b3b65ae81b3235ebcNipun Kwatraclass Camera;
3165e7e6facda89927cb26594b3b65ae81b3235ebcNipun Kwatra
3265e7e6facda89927cb26594b3b65ae81b3235ebcNipun Kwatraclass CameraSourceTimeLapse : public CameraSource {
3365e7e6facda89927cb26594b3b65ae81b3235ebcNipun Kwatrapublic:
34155e833a7a5fc3e193691324cf9326da1bc3289aNipun Kwatra    static CameraSourceTimeLapse *Create(
3565e7e6facda89927cb26594b3b65ae81b3235ebcNipun Kwatra        int64_t timeBetweenTimeLapseFrameCaptureUs,
36e4e0a6994d39c4a7cba09c5fff442b2dca1df8f8Nipun Kwatra        int32_t width, int32_t height,
3765e7e6facda89927cb26594b3b65ae81b3235ebcNipun Kwatra        int32_t videoFrameRate);
3865e7e6facda89927cb26594b3b65ae81b3235ebcNipun Kwatra
3965e7e6facda89927cb26594b3b65ae81b3235ebcNipun Kwatra    static CameraSourceTimeLapse *CreateFromCamera(const sp<Camera> &camera,
4065e7e6facda89927cb26594b3b65ae81b3235ebcNipun Kwatra        int64_t timeBetweenTimeLapseFrameCaptureUs,
41e4e0a6994d39c4a7cba09c5fff442b2dca1df8f8Nipun Kwatra        int32_t width, int32_t height,
4265e7e6facda89927cb26594b3b65ae81b3235ebcNipun Kwatra        int32_t videoFrameRate);
4365e7e6facda89927cb26594b3b65ae81b3235ebcNipun Kwatra
4465e7e6facda89927cb26594b3b65ae81b3235ebcNipun Kwatra    virtual ~CameraSourceTimeLapse();
4565e7e6facda89927cb26594b3b65ae81b3235ebcNipun Kwatra
4678eff720c86eb6d4e3d45a144df60b2ca464d2d4Nipun Kwatra    // If the frame capture interval is large, read will block for a long time.
4778eff720c86eb6d4e3d45a144df60b2ca464d2d4Nipun Kwatra    // Due to the way the mediaRecorder framework works, a stop() call from
4878eff720c86eb6d4e3d45a144df60b2ca464d2d4Nipun Kwatra    // mediaRecorder waits until the read returns, causing a long wait for
4978eff720c86eb6d4e3d45a144df60b2ca464d2d4Nipun Kwatra    // stop() to return. To avoid this, we can make read() return a copy of the
5078eff720c86eb6d4e3d45a144df60b2ca464d2d4Nipun Kwatra    // last read frame with the same time stamp frequently. This keeps the
5178eff720c86eb6d4e3d45a144df60b2ca464d2d4Nipun Kwatra    // read() call from blocking too long. Calling this function quickly
5278eff720c86eb6d4e3d45a144df60b2ca464d2d4Nipun Kwatra    // captures another frame, keeps its copy, and enables this mode of read()
5378eff720c86eb6d4e3d45a144df60b2ca464d2d4Nipun Kwatra    // returning quickly.
5478eff720c86eb6d4e3d45a144df60b2ca464d2d4Nipun Kwatra    void startQuickReadReturns();
5578eff720c86eb6d4e3d45a144df60b2ca464d2d4Nipun Kwatra
5665e7e6facda89927cb26594b3b65ae81b3235ebcNipun Kwatraprivate:
5765e7e6facda89927cb26594b3b65ae81b3235ebcNipun Kwatra    // If true, will use still camera takePicture() for time lapse frames
5865e7e6facda89927cb26594b3b65ae81b3235ebcNipun Kwatra    // If false, will use the videocamera frames instead.
5965e7e6facda89927cb26594b3b65ae81b3235ebcNipun Kwatra    bool mUseStillCameraForTimeLapse;
6065e7e6facda89927cb26594b3b65ae81b3235ebcNipun Kwatra
61c4e47d1e81c4e4403663cb911e98dbf3ada9942cNipun Kwatra    // Size of picture taken from still camera. This may be larger than the size
62c4e47d1e81c4e4403663cb911e98dbf3ada9942cNipun Kwatra    // of the video, as still camera may not support the exact video resolution
63c4e47d1e81c4e4403663cb911e98dbf3ada9942cNipun Kwatra    // demanded. See setPictureSizeToClosestSupported().
64c4e47d1e81c4e4403663cb911e98dbf3ada9942cNipun Kwatra    int32_t mPictureWidth;
65c4e47d1e81c4e4403663cb911e98dbf3ada9942cNipun Kwatra    int32_t mPictureHeight;
66c4e47d1e81c4e4403663cb911e98dbf3ada9942cNipun Kwatra
67c4e47d1e81c4e4403663cb911e98dbf3ada9942cNipun Kwatra    // size of the encoded video.
68c4e47d1e81c4e4403663cb911e98dbf3ada9942cNipun Kwatra    int32_t mVideoWidth;
69c4e47d1e81c4e4403663cb911e98dbf3ada9942cNipun Kwatra    int32_t mVideoHeight;
70c4e47d1e81c4e4403663cb911e98dbf3ada9942cNipun Kwatra
71c4e47d1e81c4e4403663cb911e98dbf3ada9942cNipun Kwatra    // True if we need to crop the still camera image to get the video frame.
72c4e47d1e81c4e4403663cb911e98dbf3ada9942cNipun Kwatra    bool mNeedCropping;
73c4e47d1e81c4e4403663cb911e98dbf3ada9942cNipun Kwatra
74c4e47d1e81c4e4403663cb911e98dbf3ada9942cNipun Kwatra    // Start location of the cropping rectangle.
75c4e47d1e81c4e4403663cb911e98dbf3ada9942cNipun Kwatra    int32_t mCropRectStartX;
76c4e47d1e81c4e4403663cb911e98dbf3ada9942cNipun Kwatra    int32_t mCropRectStartY;
77c4e47d1e81c4e4403663cb911e98dbf3ada9942cNipun Kwatra
7865e7e6facda89927cb26594b3b65ae81b3235ebcNipun Kwatra    // Time between capture of two frames during time lapse recording
7965e7e6facda89927cb26594b3b65ae81b3235ebcNipun Kwatra    // Negative value indicates that timelapse is disabled.
8065e7e6facda89927cb26594b3b65ae81b3235ebcNipun Kwatra    int64_t mTimeBetweenTimeLapseFrameCaptureUs;
8165e7e6facda89927cb26594b3b65ae81b3235ebcNipun Kwatra
8265e7e6facda89927cb26594b3b65ae81b3235ebcNipun Kwatra    // Time between two frames in final video (1/frameRate)
8365e7e6facda89927cb26594b3b65ae81b3235ebcNipun Kwatra    int64_t mTimeBetweenTimeLapseVideoFramesUs;
8465e7e6facda89927cb26594b3b65ae81b3235ebcNipun Kwatra
8565e7e6facda89927cb26594b3b65ae81b3235ebcNipun Kwatra    // Real timestamp of the last encoded time lapse frame
8665e7e6facda89927cb26594b3b65ae81b3235ebcNipun Kwatra    int64_t mLastTimeLapseFrameRealTimestampUs;
8765e7e6facda89927cb26594b3b65ae81b3235ebcNipun Kwatra
8865e7e6facda89927cb26594b3b65ae81b3235ebcNipun Kwatra    // Thread id of thread which takes still picture and sleeps in a loop.
8965e7e6facda89927cb26594b3b65ae81b3235ebcNipun Kwatra    pthread_t mThreadTimeLapse;
9065e7e6facda89927cb26594b3b65ae81b3235ebcNipun Kwatra
9165e7e6facda89927cb26594b3b65ae81b3235ebcNipun Kwatra    // Variable set in dataCallbackTimestamp() to help skipCurrentFrame()
9265e7e6facda89927cb26594b3b65ae81b3235ebcNipun Kwatra    // to know if current frame needs to be skipped.
9365e7e6facda89927cb26594b3b65ae81b3235ebcNipun Kwatra    bool mSkipCurrentFrame;
9465e7e6facda89927cb26594b3b65ae81b3235ebcNipun Kwatra
9578eff720c86eb6d4e3d45a144df60b2ca464d2d4Nipun Kwatra    // Lock for accessing mCameraIdle
9678eff720c86eb6d4e3d45a144df60b2ca464d2d4Nipun Kwatra    Mutex mCameraIdleLock;
9778eff720c86eb6d4e3d45a144df60b2ca464d2d4Nipun Kwatra
9878eff720c86eb6d4e3d45a144df60b2ca464d2d4Nipun Kwatra    // Condition variable to wait on if camera is is not yet idle. Once the
9978eff720c86eb6d4e3d45a144df60b2ca464d2d4Nipun Kwatra    // camera gets idle, this variable will be signalled.
10078eff720c86eb6d4e3d45a144df60b2ca464d2d4Nipun Kwatra    Condition mCameraIdleCondition;
10178eff720c86eb6d4e3d45a144df60b2ca464d2d4Nipun Kwatra
1020aacf105eea098a0b47761a4a9a5c4d820611f2dNipun Kwatra    // True if camera is in preview mode and ready for takePicture().
10378eff720c86eb6d4e3d45a144df60b2ca464d2d4Nipun Kwatra    // False after a call to takePicture() but before the final compressed
10478eff720c86eb6d4e3d45a144df60b2ca464d2d4Nipun Kwatra    // data callback has been called and preview has been restarted.
10578eff720c86eb6d4e3d45a144df60b2ca464d2d4Nipun Kwatra    volatile bool mCameraIdle;
10678eff720c86eb6d4e3d45a144df60b2ca464d2d4Nipun Kwatra
10778eff720c86eb6d4e3d45a144df60b2ca464d2d4Nipun Kwatra    // True if stop() is waiting for camera to get idle, i.e. for the last
10878eff720c86eb6d4e3d45a144df60b2ca464d2d4Nipun Kwatra    // takePicture() to complete. This is needed so that dataCallbackTimestamp()
10978eff720c86eb6d4e3d45a144df60b2ca464d2d4Nipun Kwatra    // can return immediately.
11078eff720c86eb6d4e3d45a144df60b2ca464d2d4Nipun Kwatra    volatile bool mStopWaitingForIdleCamera;
11178eff720c86eb6d4e3d45a144df60b2ca464d2d4Nipun Kwatra
11278eff720c86eb6d4e3d45a144df60b2ca464d2d4Nipun Kwatra    // Lock for accessing quick stop variables.
11378eff720c86eb6d4e3d45a144df60b2ca464d2d4Nipun Kwatra    Mutex mQuickStopLock;
11478eff720c86eb6d4e3d45a144df60b2ca464d2d4Nipun Kwatra
11578eff720c86eb6d4e3d45a144df60b2ca464d2d4Nipun Kwatra    // Condition variable to wake up still picture thread.
11678eff720c86eb6d4e3d45a144df60b2ca464d2d4Nipun Kwatra    Condition mTakePictureCondition;
11778eff720c86eb6d4e3d45a144df60b2ca464d2d4Nipun Kwatra
11878eff720c86eb6d4e3d45a144df60b2ca464d2d4Nipun Kwatra    // mQuickStop is set to true if we use quick read() returns, otherwise it is set
11978eff720c86eb6d4e3d45a144df60b2ca464d2d4Nipun Kwatra    // to false. Once in this mode read() return a copy of the last read frame
12078eff720c86eb6d4e3d45a144df60b2ca464d2d4Nipun Kwatra    // with the same time stamp. See startQuickReadReturns().
12178eff720c86eb6d4e3d45a144df60b2ca464d2d4Nipun Kwatra    volatile bool mQuickStop;
12278eff720c86eb6d4e3d45a144df60b2ca464d2d4Nipun Kwatra
12378eff720c86eb6d4e3d45a144df60b2ca464d2d4Nipun Kwatra    // Forces the next frame passed to dataCallbackTimestamp() to be read
12478eff720c86eb6d4e3d45a144df60b2ca464d2d4Nipun Kwatra    // as a time lapse frame. Used by startQuickReadReturns() so that the next
12578eff720c86eb6d4e3d45a144df60b2ca464d2d4Nipun Kwatra    // frame wakes up any blocking read.
12678eff720c86eb6d4e3d45a144df60b2ca464d2d4Nipun Kwatra    volatile bool mForceRead;
12778eff720c86eb6d4e3d45a144df60b2ca464d2d4Nipun Kwatra
12878eff720c86eb6d4e3d45a144df60b2ca464d2d4Nipun Kwatra    // Stores a copy of the MediaBuffer read in the last read() call after
12978eff720c86eb6d4e3d45a144df60b2ca464d2d4Nipun Kwatra    // mQuickStop was true.
13078eff720c86eb6d4e3d45a144df60b2ca464d2d4Nipun Kwatra    MediaBuffer* mLastReadBufferCopy;
13178eff720c86eb6d4e3d45a144df60b2ca464d2d4Nipun Kwatra
13278eff720c86eb6d4e3d45a144df60b2ca464d2d4Nipun Kwatra    // Status code for last read.
13378eff720c86eb6d4e3d45a144df60b2ca464d2d4Nipun Kwatra    status_t mLastReadStatus;
1340aacf105eea098a0b47761a4a9a5c4d820611f2dNipun Kwatra
13565e7e6facda89927cb26594b3b65ae81b3235ebcNipun Kwatra    CameraSourceTimeLapse(const sp<Camera> &camera,
13665e7e6facda89927cb26594b3b65ae81b3235ebcNipun Kwatra        int64_t timeBetweenTimeLapseFrameCaptureUs,
137e4e0a6994d39c4a7cba09c5fff442b2dca1df8f8Nipun Kwatra        int32_t width, int32_t height,
13865e7e6facda89927cb26594b3b65ae81b3235ebcNipun Kwatra        int32_t videoFrameRate);
13965e7e6facda89927cb26594b3b65ae81b3235ebcNipun Kwatra
14078eff720c86eb6d4e3d45a144df60b2ca464d2d4Nipun Kwatra    // Wrapper over CameraSource::signalBufferReturned() to implement quick stop.
14178eff720c86eb6d4e3d45a144df60b2ca464d2d4Nipun Kwatra    // It only handles the case when mLastReadBufferCopy is signalled. Otherwise
14278eff720c86eb6d4e3d45a144df60b2ca464d2d4Nipun Kwatra    // it calls the base class' function.
14378eff720c86eb6d4e3d45a144df60b2ca464d2d4Nipun Kwatra    virtual void signalBufferReturned(MediaBuffer* buffer);
14478eff720c86eb6d4e3d45a144df60b2ca464d2d4Nipun Kwatra
14578eff720c86eb6d4e3d45a144df60b2ca464d2d4Nipun Kwatra    // Wrapper over CameraSource::read() to implement quick stop.
14678eff720c86eb6d4e3d45a144df60b2ca464d2d4Nipun Kwatra    virtual status_t read(MediaBuffer **buffer, const ReadOptions *options = NULL);
14778eff720c86eb6d4e3d45a144df60b2ca464d2d4Nipun Kwatra
14865e7e6facda89927cb26594b3b65ae81b3235ebcNipun Kwatra    // For still camera case starts a thread which calls camera's takePicture()
14965e7e6facda89927cb26594b3b65ae81b3235ebcNipun Kwatra    // in a loop. For video camera case, just starts the camera's video recording.
15065e7e6facda89927cb26594b3b65ae81b3235ebcNipun Kwatra    virtual void startCameraRecording();
15165e7e6facda89927cb26594b3b65ae81b3235ebcNipun Kwatra
15265e7e6facda89927cb26594b3b65ae81b3235ebcNipun Kwatra    // For still camera case joins the thread created in startCameraRecording().
15365e7e6facda89927cb26594b3b65ae81b3235ebcNipun Kwatra    // For video camera case, just stops the camera's video recording.
15465e7e6facda89927cb26594b3b65ae81b3235ebcNipun Kwatra    virtual void stopCameraRecording();
15565e7e6facda89927cb26594b3b65ae81b3235ebcNipun Kwatra
15665e7e6facda89927cb26594b3b65ae81b3235ebcNipun Kwatra    // For still camera case don't need to do anything as memory is locally
15765e7e6facda89927cb26594b3b65ae81b3235ebcNipun Kwatra    // allocated with refcounting.
15865e7e6facda89927cb26594b3b65ae81b3235ebcNipun Kwatra    // For video camera case just tell the camera to release the frame.
15965e7e6facda89927cb26594b3b65ae81b3235ebcNipun Kwatra    virtual void releaseRecordingFrame(const sp<IMemory>& frame);
16065e7e6facda89927cb26594b3b65ae81b3235ebcNipun Kwatra
16165e7e6facda89927cb26594b3b65ae81b3235ebcNipun Kwatra    // mSkipCurrentFrame is set to true in dataCallbackTimestamp() if the current
16265e7e6facda89927cb26594b3b65ae81b3235ebcNipun Kwatra    // frame needs to be skipped and this function just returns the value of mSkipCurrentFrame.
16365e7e6facda89927cb26594b3b65ae81b3235ebcNipun Kwatra    virtual bool skipCurrentFrame(int64_t timestampUs);
16465e7e6facda89927cb26594b3b65ae81b3235ebcNipun Kwatra
16565e7e6facda89927cb26594b3b65ae81b3235ebcNipun Kwatra    // Handles the callback to handle raw frame data from the still camera.
16665e7e6facda89927cb26594b3b65ae81b3235ebcNipun Kwatra    // Creates a copy of the frame data as the camera can reuse the frame memory
16765e7e6facda89927cb26594b3b65ae81b3235ebcNipun Kwatra    // once this callback returns. The function also sets a new timstamp corresponding
16865e7e6facda89927cb26594b3b65ae81b3235ebcNipun Kwatra    // to one frame time ahead of the last encoded frame's time stamp. It then
16965e7e6facda89927cb26594b3b65ae81b3235ebcNipun Kwatra    // calls dataCallbackTimestamp() of the base class with the copied data and the
17065e7e6facda89927cb26594b3b65ae81b3235ebcNipun Kwatra    // modified timestamp, which will think that it recieved the frame from a video
17165e7e6facda89927cb26594b3b65ae81b3235ebcNipun Kwatra    // camera and proceed as usual.
17265e7e6facda89927cb26594b3b65ae81b3235ebcNipun Kwatra    virtual void dataCallback(int32_t msgType, const sp<IMemory> &data);
17365e7e6facda89927cb26594b3b65ae81b3235ebcNipun Kwatra
17465e7e6facda89927cb26594b3b65ae81b3235ebcNipun Kwatra    // In the video camera case calls skipFrameAndModifyTimeStamp() to modify
17565e7e6facda89927cb26594b3b65ae81b3235ebcNipun Kwatra    // timestamp and set mSkipCurrentFrame.
17665e7e6facda89927cb26594b3b65ae81b3235ebcNipun Kwatra    // Then it calls the base CameraSource::dataCallbackTimestamp()
17765e7e6facda89927cb26594b3b65ae81b3235ebcNipun Kwatra    virtual void dataCallbackTimestamp(int64_t timestampUs, int32_t msgType,
17865e7e6facda89927cb26594b3b65ae81b3235ebcNipun Kwatra            const sp<IMemory> &data);
17965e7e6facda89927cb26594b3b65ae81b3235ebcNipun Kwatra
18078eff720c86eb6d4e3d45a144df60b2ca464d2d4Nipun Kwatra    // Convenience function to fill mLastReadBufferCopy from the just read
18178eff720c86eb6d4e3d45a144df60b2ca464d2d4Nipun Kwatra    // buffer.
18278eff720c86eb6d4e3d45a144df60b2ca464d2d4Nipun Kwatra    void fillLastReadBufferCopy(MediaBuffer& sourceBuffer);
18378eff720c86eb6d4e3d45a144df60b2ca464d2d4Nipun Kwatra
184155e833a7a5fc3e193691324cf9326da1bc3289aNipun Kwatra    // If the passed in size (width x height) is a supported preview size,
185155e833a7a5fc3e193691324cf9326da1bc3289aNipun Kwatra    // the function sets the camera's preview size to it and returns true.
186155e833a7a5fc3e193691324cf9326da1bc3289aNipun Kwatra    // Otherwise returns false.
187155e833a7a5fc3e193691324cf9326da1bc3289aNipun Kwatra    bool trySettingPreviewSize(int32_t width, int32_t height);
188155e833a7a5fc3e193691324cf9326da1bc3289aNipun Kwatra
189c4e47d1e81c4e4403663cb911e98dbf3ada9942cNipun Kwatra    // The still camera may not support the demanded video width and height.
190c4e47d1e81c4e4403663cb911e98dbf3ada9942cNipun Kwatra    // We look for the supported picture sizes from the still camera and
19140e2f3f9b41f44bdb59f7708a421b87f169a6edeNipun Kwatra    // choose the smallest one with either dimensions higher than the corresponding
19240e2f3f9b41f44bdb59f7708a421b87f169a6edeNipun Kwatra    // video dimensions. The still picture will be cropped to get the video frame.
19340e2f3f9b41f44bdb59f7708a421b87f169a6edeNipun Kwatra    // The function returns true if the camera supports picture sizes greater than
19440e2f3f9b41f44bdb59f7708a421b87f169a6edeNipun Kwatra    // or equal to the passed in width and height, and false otherwise.
19540e2f3f9b41f44bdb59f7708a421b87f169a6edeNipun Kwatra    bool setPictureSizeToClosestSupported(int32_t width, int32_t height);
196c4e47d1e81c4e4403663cb911e98dbf3ada9942cNipun Kwatra
197c4e47d1e81c4e4403663cb911e98dbf3ada9942cNipun Kwatra    // Computes the offset of the rectangle from where to start cropping the
198c4e47d1e81c4e4403663cb911e98dbf3ada9942cNipun Kwatra    // still image into the video frame. We choose the center of the image to be
199c4e47d1e81c4e4403663cb911e98dbf3ada9942cNipun Kwatra    // cropped. The offset is stored in (mCropRectStartX, mCropRectStartY).
200c4e47d1e81c4e4403663cb911e98dbf3ada9942cNipun Kwatra    bool computeCropRectangleOffset();
201c4e47d1e81c4e4403663cb911e98dbf3ada9942cNipun Kwatra
202c4e47d1e81c4e4403663cb911e98dbf3ada9942cNipun Kwatra    // Crops the source data into a smaller image starting at
203c4e47d1e81c4e4403663cb911e98dbf3ada9942cNipun Kwatra    // (mCropRectStartX, mCropRectStartY) and of the size of the video frame.
204c4e47d1e81c4e4403663cb911e98dbf3ada9942cNipun Kwatra    // The data is returned into a newly allocated IMemory.
205c4e47d1e81c4e4403663cb911e98dbf3ada9942cNipun Kwatra    sp<IMemory> cropYUVImage(const sp<IMemory> &source_data);
206c4e47d1e81c4e4403663cb911e98dbf3ada9942cNipun Kwatra
20765e7e6facda89927cb26594b3b65ae81b3235ebcNipun Kwatra    // When video camera is used for time lapse capture, returns true
20865e7e6facda89927cb26594b3b65ae81b3235ebcNipun Kwatra    // until enough time has passed for the next time lapse frame. When
20965e7e6facda89927cb26594b3b65ae81b3235ebcNipun Kwatra    // the frame needs to be encoded, it returns false and also modifies
21065e7e6facda89927cb26594b3b65ae81b3235ebcNipun Kwatra    // the time stamp to be one frame time ahead of the last encoded
21165e7e6facda89927cb26594b3b65ae81b3235ebcNipun Kwatra    // frame's time stamp.
21265e7e6facda89927cb26594b3b65ae81b3235ebcNipun Kwatra    bool skipFrameAndModifyTimeStamp(int64_t *timestampUs);
21365e7e6facda89927cb26594b3b65ae81b3235ebcNipun Kwatra
21465e7e6facda89927cb26594b3b65ae81b3235ebcNipun Kwatra    // Wrapper to enter threadTimeLapseEntry()
21565e7e6facda89927cb26594b3b65ae81b3235ebcNipun Kwatra    static void *ThreadTimeLapseWrapper(void *me);
21665e7e6facda89927cb26594b3b65ae81b3235ebcNipun Kwatra
21765e7e6facda89927cb26594b3b65ae81b3235ebcNipun Kwatra    // Runs a loop which sleeps until a still picture is required
21865e7e6facda89927cb26594b3b65ae81b3235ebcNipun Kwatra    // and then calls mCamera->takePicture() to take the still picture.
21965e7e6facda89927cb26594b3b65ae81b3235ebcNipun Kwatra    // Used only in the case mUseStillCameraForTimeLapse = true.
22065e7e6facda89927cb26594b3b65ae81b3235ebcNipun Kwatra    void threadTimeLapseEntry();
22165e7e6facda89927cb26594b3b65ae81b3235ebcNipun Kwatra
2220aacf105eea098a0b47761a4a9a5c4d820611f2dNipun Kwatra    // Wrapper to enter threadStartPreview()
2230aacf105eea098a0b47761a4a9a5c4d820611f2dNipun Kwatra    static void *ThreadStartPreviewWrapper(void *me);
2240aacf105eea098a0b47761a4a9a5c4d820611f2dNipun Kwatra
2250aacf105eea098a0b47761a4a9a5c4d820611f2dNipun Kwatra    // Starts the camera's preview.
2260aacf105eea098a0b47761a4a9a5c4d820611f2dNipun Kwatra    void threadStartPreview();
2270aacf105eea098a0b47761a4a9a5c4d820611f2dNipun Kwatra
2280aacf105eea098a0b47761a4a9a5c4d820611f2dNipun Kwatra    // Starts thread ThreadStartPreviewWrapper() for restarting preview.
2290aacf105eea098a0b47761a4a9a5c4d820611f2dNipun Kwatra    // Needs to be done in a thread so that dataCallback() which calls this function
2300aacf105eea098a0b47761a4a9a5c4d820611f2dNipun Kwatra    // can return, and the camera can know that takePicture() is done.
2310aacf105eea098a0b47761a4a9a5c4d820611f2dNipun Kwatra    void restartPreview();
2320aacf105eea098a0b47761a4a9a5c4d820611f2dNipun Kwatra
23365e7e6facda89927cb26594b3b65ae81b3235ebcNipun Kwatra    // Creates a copy of source_data into a new memory of final type MemoryBase.
23465e7e6facda89927cb26594b3b65ae81b3235ebcNipun Kwatra    sp<IMemory> createIMemoryCopy(const sp<IMemory> &source_data);
23565e7e6facda89927cb26594b3b65ae81b3235ebcNipun Kwatra
23665e7e6facda89927cb26594b3b65ae81b3235ebcNipun Kwatra    CameraSourceTimeLapse(const CameraSourceTimeLapse &);
23765e7e6facda89927cb26594b3b65ae81b3235ebcNipun Kwatra    CameraSourceTimeLapse &operator=(const CameraSourceTimeLapse &);
23865e7e6facda89927cb26594b3b65ae81b3235ebcNipun Kwatra};
23965e7e6facda89927cb26594b3b65ae81b3235ebcNipun Kwatra
24065e7e6facda89927cb26594b3b65ae81b3235ebcNipun Kwatra}  // namespace android
24165e7e6facda89927cb26594b3b65ae81b3235ebcNipun Kwatra
24265e7e6facda89927cb26594b3b65ae81b3235ebcNipun Kwatra#endif  // CAMERA_SOURCE_TIME_LAPSE_H_
243