1f9b80182bcd32d563c23c12b4ac06517fd6da531Nipun Kwatra/*
2f9b80182bcd32d563c23c12b4ac06517fd6da531Nipun Kwatra * Copyright (C) 2010 The Android Open Source Project
3f9b80182bcd32d563c23c12b4ac06517fd6da531Nipun Kwatra *
4f9b80182bcd32d563c23c12b4ac06517fd6da531Nipun Kwatra * Licensed under the Apache License, Version 2.0 (the "License");
5f9b80182bcd32d563c23c12b4ac06517fd6da531Nipun Kwatra * you may not use this file except in compliance with the License.
6f9b80182bcd32d563c23c12b4ac06517fd6da531Nipun Kwatra * You may obtain a copy of the License at
7f9b80182bcd32d563c23c12b4ac06517fd6da531Nipun Kwatra *
8f9b80182bcd32d563c23c12b4ac06517fd6da531Nipun Kwatra *      http://www.apache.org/licenses/LICENSE-2.0
9f9b80182bcd32d563c23c12b4ac06517fd6da531Nipun Kwatra *
10f9b80182bcd32d563c23c12b4ac06517fd6da531Nipun Kwatra * Unless required by applicable law or agreed to in writing, software
11f9b80182bcd32d563c23c12b4ac06517fd6da531Nipun Kwatra * distributed under the License is distributed on an "AS IS" BASIS,
12f9b80182bcd32d563c23c12b4ac06517fd6da531Nipun Kwatra * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13f9b80182bcd32d563c23c12b4ac06517fd6da531Nipun Kwatra * See the License for the specific language governing permissions and
14f9b80182bcd32d563c23c12b4ac06517fd6da531Nipun Kwatra * limitations under the License.
15f9b80182bcd32d563c23c12b4ac06517fd6da531Nipun Kwatra */
16f9b80182bcd32d563c23c12b4ac06517fd6da531Nipun Kwatra
17f9b80182bcd32d563c23c12b4ac06517fd6da531Nipun Kwatra#ifndef CAMERA_SOURCE_TIME_LAPSE_H_
18f9b80182bcd32d563c23c12b4ac06517fd6da531Nipun Kwatra
19f9b80182bcd32d563c23c12b4ac06517fd6da531Nipun Kwatra#define CAMERA_SOURCE_TIME_LAPSE_H_
20f9b80182bcd32d563c23c12b4ac06517fd6da531Nipun Kwatra
21f9b80182bcd32d563c23c12b4ac06517fd6da531Nipun Kwatra#include <pthread.h>
22f9b80182bcd32d563c23c12b4ac06517fd6da531Nipun Kwatra
23f9b80182bcd32d563c23c12b4ac06517fd6da531Nipun Kwatra#include <utils/RefBase.h>
24f9b80182bcd32d563c23c12b4ac06517fd6da531Nipun Kwatra#include <utils/threads.h>
25f9b80182bcd32d563c23c12b4ac06517fd6da531Nipun Kwatra
26f9b80182bcd32d563c23c12b4ac06517fd6da531Nipun Kwatranamespace android {
27f9b80182bcd32d563c23c12b4ac06517fd6da531Nipun Kwatra
28f9b80182bcd32d563c23c12b4ac06517fd6da531Nipun Kwatraclass ICamera;
29f9b80182bcd32d563c23c12b4ac06517fd6da531Nipun Kwatraclass IMemory;
30f9b80182bcd32d563c23c12b4ac06517fd6da531Nipun Kwatraclass Camera;
31f9b80182bcd32d563c23c12b4ac06517fd6da531Nipun Kwatra
32f9b80182bcd32d563c23c12b4ac06517fd6da531Nipun Kwatraclass CameraSourceTimeLapse : public CameraSource {
33f9b80182bcd32d563c23c12b4ac06517fd6da531Nipun Kwatrapublic:
340c128b67f066b2e691348d5375c2da47b84f69acJames Dong    static CameraSourceTimeLapse *CreateFromCamera(
350c128b67f066b2e691348d5375c2da47b84f69acJames Dong        const sp<ICamera> &camera,
3642419ce28a09eb63e29a8fef87e6f5534f41902fWu-cheng Li        const sp<ICameraRecordingProxy> &proxy,
370c128b67f066b2e691348d5375c2da47b84f69acJames Dong        int32_t cameraId,
380c128b67f066b2e691348d5375c2da47b84f69acJames Dong        Size videoSize,
390c128b67f066b2e691348d5375c2da47b84f69acJames Dong        int32_t videoFrameRate,
400c128b67f066b2e691348d5375c2da47b84f69acJames Dong        const sp<Surface>& surface,
410c128b67f066b2e691348d5375c2da47b84f69acJames Dong        int64_t timeBetweenTimeLapseFrameCaptureUs);
42f9b80182bcd32d563c23c12b4ac06517fd6da531Nipun Kwatra
43f9b80182bcd32d563c23c12b4ac06517fd6da531Nipun Kwatra    virtual ~CameraSourceTimeLapse();
44f9b80182bcd32d563c23c12b4ac06517fd6da531Nipun Kwatra
457553cf74e67a83440139b34a098b90a2ad6ed86dNipun Kwatra    // If the frame capture interval is large, read will block for a long time.
467553cf74e67a83440139b34a098b90a2ad6ed86dNipun Kwatra    // Due to the way the mediaRecorder framework works, a stop() call from
477553cf74e67a83440139b34a098b90a2ad6ed86dNipun Kwatra    // mediaRecorder waits until the read returns, causing a long wait for
487553cf74e67a83440139b34a098b90a2ad6ed86dNipun Kwatra    // stop() to return. To avoid this, we can make read() return a copy of the
497553cf74e67a83440139b34a098b90a2ad6ed86dNipun Kwatra    // last read frame with the same time stamp frequently. This keeps the
507553cf74e67a83440139b34a098b90a2ad6ed86dNipun Kwatra    // read() call from blocking too long. Calling this function quickly
517553cf74e67a83440139b34a098b90a2ad6ed86dNipun Kwatra    // captures another frame, keeps its copy, and enables this mode of read()
527553cf74e67a83440139b34a098b90a2ad6ed86dNipun Kwatra    // returning quickly.
537553cf74e67a83440139b34a098b90a2ad6ed86dNipun Kwatra    void startQuickReadReturns();
547553cf74e67a83440139b34a098b90a2ad6ed86dNipun Kwatra
55f9b80182bcd32d563c23c12b4ac06517fd6da531Nipun Kwatraprivate:
56dce4beb17982faeb6308bd7ee86c684514a3bad3Nipun Kwatra    // size of the encoded video.
57dce4beb17982faeb6308bd7ee86c684514a3bad3Nipun Kwatra    int32_t mVideoWidth;
58dce4beb17982faeb6308bd7ee86c684514a3bad3Nipun Kwatra    int32_t mVideoHeight;
59dce4beb17982faeb6308bd7ee86c684514a3bad3Nipun Kwatra
60f9b80182bcd32d563c23c12b4ac06517fd6da531Nipun Kwatra    // Time between two frames in final video (1/frameRate)
61f9b80182bcd32d563c23c12b4ac06517fd6da531Nipun Kwatra    int64_t mTimeBetweenTimeLapseVideoFramesUs;
62f9b80182bcd32d563c23c12b4ac06517fd6da531Nipun Kwatra
63f9b80182bcd32d563c23c12b4ac06517fd6da531Nipun Kwatra    // Real timestamp of the last encoded time lapse frame
64f9b80182bcd32d563c23c12b4ac06517fd6da531Nipun Kwatra    int64_t mLastTimeLapseFrameRealTimestampUs;
65f9b80182bcd32d563c23c12b4ac06517fd6da531Nipun Kwatra
66f9b80182bcd32d563c23c12b4ac06517fd6da531Nipun Kwatra    // Variable set in dataCallbackTimestamp() to help skipCurrentFrame()
67f9b80182bcd32d563c23c12b4ac06517fd6da531Nipun Kwatra    // to know if current frame needs to be skipped.
68f9b80182bcd32d563c23c12b4ac06517fd6da531Nipun Kwatra    bool mSkipCurrentFrame;
69f9b80182bcd32d563c23c12b4ac06517fd6da531Nipun Kwatra
707553cf74e67a83440139b34a098b90a2ad6ed86dNipun Kwatra    // Lock for accessing mCameraIdle
717553cf74e67a83440139b34a098b90a2ad6ed86dNipun Kwatra    Mutex mCameraIdleLock;
727553cf74e67a83440139b34a098b90a2ad6ed86dNipun Kwatra
737553cf74e67a83440139b34a098b90a2ad6ed86dNipun Kwatra    // Condition variable to wait on if camera is is not yet idle. Once the
747553cf74e67a83440139b34a098b90a2ad6ed86dNipun Kwatra    // camera gets idle, this variable will be signalled.
757553cf74e67a83440139b34a098b90a2ad6ed86dNipun Kwatra    Condition mCameraIdleCondition;
767553cf74e67a83440139b34a098b90a2ad6ed86dNipun Kwatra
774cd8672662b0e56f6b8b5c7e3cf6ced8c1b15638Nipun Kwatra    // True if camera is in preview mode and ready for takePicture().
787553cf74e67a83440139b34a098b90a2ad6ed86dNipun Kwatra    // False after a call to takePicture() but before the final compressed
797553cf74e67a83440139b34a098b90a2ad6ed86dNipun Kwatra    // data callback has been called and preview has been restarted.
807553cf74e67a83440139b34a098b90a2ad6ed86dNipun Kwatra    volatile bool mCameraIdle;
817553cf74e67a83440139b34a098b90a2ad6ed86dNipun Kwatra
827553cf74e67a83440139b34a098b90a2ad6ed86dNipun Kwatra    // True if stop() is waiting for camera to get idle, i.e. for the last
837553cf74e67a83440139b34a098b90a2ad6ed86dNipun Kwatra    // takePicture() to complete. This is needed so that dataCallbackTimestamp()
847553cf74e67a83440139b34a098b90a2ad6ed86dNipun Kwatra    // can return immediately.
857553cf74e67a83440139b34a098b90a2ad6ed86dNipun Kwatra    volatile bool mStopWaitingForIdleCamera;
867553cf74e67a83440139b34a098b90a2ad6ed86dNipun Kwatra
877553cf74e67a83440139b34a098b90a2ad6ed86dNipun Kwatra    // Lock for accessing quick stop variables.
887553cf74e67a83440139b34a098b90a2ad6ed86dNipun Kwatra    Mutex mQuickStopLock;
897553cf74e67a83440139b34a098b90a2ad6ed86dNipun Kwatra
907553cf74e67a83440139b34a098b90a2ad6ed86dNipun Kwatra    // mQuickStop is set to true if we use quick read() returns, otherwise it is set
917553cf74e67a83440139b34a098b90a2ad6ed86dNipun Kwatra    // to false. Once in this mode read() return a copy of the last read frame
927553cf74e67a83440139b34a098b90a2ad6ed86dNipun Kwatra    // with the same time stamp. See startQuickReadReturns().
937553cf74e67a83440139b34a098b90a2ad6ed86dNipun Kwatra    volatile bool mQuickStop;
947553cf74e67a83440139b34a098b90a2ad6ed86dNipun Kwatra
957553cf74e67a83440139b34a098b90a2ad6ed86dNipun Kwatra    // Forces the next frame passed to dataCallbackTimestamp() to be read
967553cf74e67a83440139b34a098b90a2ad6ed86dNipun Kwatra    // as a time lapse frame. Used by startQuickReadReturns() so that the next
977553cf74e67a83440139b34a098b90a2ad6ed86dNipun Kwatra    // frame wakes up any blocking read.
987553cf74e67a83440139b34a098b90a2ad6ed86dNipun Kwatra    volatile bool mForceRead;
997553cf74e67a83440139b34a098b90a2ad6ed86dNipun Kwatra
1007553cf74e67a83440139b34a098b90a2ad6ed86dNipun Kwatra    // Stores a copy of the MediaBuffer read in the last read() call after
1017553cf74e67a83440139b34a098b90a2ad6ed86dNipun Kwatra    // mQuickStop was true.
1027553cf74e67a83440139b34a098b90a2ad6ed86dNipun Kwatra    MediaBuffer* mLastReadBufferCopy;
1037553cf74e67a83440139b34a098b90a2ad6ed86dNipun Kwatra
1047553cf74e67a83440139b34a098b90a2ad6ed86dNipun Kwatra    // Status code for last read.
1057553cf74e67a83440139b34a098b90a2ad6ed86dNipun Kwatra    status_t mLastReadStatus;
1064cd8672662b0e56f6b8b5c7e3cf6ced8c1b15638Nipun Kwatra
1070c128b67f066b2e691348d5375c2da47b84f69acJames Dong    CameraSourceTimeLapse(
1080c128b67f066b2e691348d5375c2da47b84f69acJames Dong        const sp<ICamera> &camera,
10942419ce28a09eb63e29a8fef87e6f5534f41902fWu-cheng Li        const sp<ICameraRecordingProxy> &proxy,
1100c128b67f066b2e691348d5375c2da47b84f69acJames Dong        int32_t cameraId,
1110c128b67f066b2e691348d5375c2da47b84f69acJames Dong        Size videoSize,
1120c128b67f066b2e691348d5375c2da47b84f69acJames Dong        int32_t videoFrameRate,
1130c128b67f066b2e691348d5375c2da47b84f69acJames Dong        const sp<Surface>& surface,
1140c128b67f066b2e691348d5375c2da47b84f69acJames Dong        int64_t timeBetweenTimeLapseFrameCaptureUs);
115f9b80182bcd32d563c23c12b4ac06517fd6da531Nipun Kwatra
1167553cf74e67a83440139b34a098b90a2ad6ed86dNipun Kwatra    // Wrapper over CameraSource::signalBufferReturned() to implement quick stop.
1177553cf74e67a83440139b34a098b90a2ad6ed86dNipun Kwatra    // It only handles the case when mLastReadBufferCopy is signalled. Otherwise
1187553cf74e67a83440139b34a098b90a2ad6ed86dNipun Kwatra    // it calls the base class' function.
1197553cf74e67a83440139b34a098b90a2ad6ed86dNipun Kwatra    virtual void signalBufferReturned(MediaBuffer* buffer);
1207553cf74e67a83440139b34a098b90a2ad6ed86dNipun Kwatra
1217553cf74e67a83440139b34a098b90a2ad6ed86dNipun Kwatra    // Wrapper over CameraSource::read() to implement quick stop.
1227553cf74e67a83440139b34a098b90a2ad6ed86dNipun Kwatra    virtual status_t read(MediaBuffer **buffer, const ReadOptions *options = NULL);
1237553cf74e67a83440139b34a098b90a2ad6ed86dNipun Kwatra
124f9b80182bcd32d563c23c12b4ac06517fd6da531Nipun Kwatra    // For video camera case, just stops the camera's video recording.
125f9b80182bcd32d563c23c12b4ac06517fd6da531Nipun Kwatra    virtual void stopCameraRecording();
126f9b80182bcd32d563c23c12b4ac06517fd6da531Nipun Kwatra
127f9b80182bcd32d563c23c12b4ac06517fd6da531Nipun Kwatra    // mSkipCurrentFrame is set to true in dataCallbackTimestamp() if the current
128f9b80182bcd32d563c23c12b4ac06517fd6da531Nipun Kwatra    // frame needs to be skipped and this function just returns the value of mSkipCurrentFrame.
129f9b80182bcd32d563c23c12b4ac06517fd6da531Nipun Kwatra    virtual bool skipCurrentFrame(int64_t timestampUs);
130f9b80182bcd32d563c23c12b4ac06517fd6da531Nipun Kwatra
131f9b80182bcd32d563c23c12b4ac06517fd6da531Nipun Kwatra    // In the video camera case calls skipFrameAndModifyTimeStamp() to modify
132f9b80182bcd32d563c23c12b4ac06517fd6da531Nipun Kwatra    // timestamp and set mSkipCurrentFrame.
133f9b80182bcd32d563c23c12b4ac06517fd6da531Nipun Kwatra    // Then it calls the base CameraSource::dataCallbackTimestamp()
134f9b80182bcd32d563c23c12b4ac06517fd6da531Nipun Kwatra    virtual void dataCallbackTimestamp(int64_t timestampUs, int32_t msgType,
135f9b80182bcd32d563c23c12b4ac06517fd6da531Nipun Kwatra            const sp<IMemory> &data);
136f9b80182bcd32d563c23c12b4ac06517fd6da531Nipun Kwatra
1377553cf74e67a83440139b34a098b90a2ad6ed86dNipun Kwatra    // Convenience function to fill mLastReadBufferCopy from the just read
1387553cf74e67a83440139b34a098b90a2ad6ed86dNipun Kwatra    // buffer.
1397553cf74e67a83440139b34a098b90a2ad6ed86dNipun Kwatra    void fillLastReadBufferCopy(MediaBuffer& sourceBuffer);
1407553cf74e67a83440139b34a098b90a2ad6ed86dNipun Kwatra
141a1d2d8f7a5fd956ab82acc641415e09ff6c00a7eJames Dong    // If the passed in size (width x height) is a supported video/preview size,
142a1d2d8f7a5fd956ab82acc641415e09ff6c00a7eJames Dong    // the function sets the camera's video/preview size to it and returns true.
1434a857e620fecd91d051d8e58f573b5ff30d81aafNipun Kwatra    // Otherwise returns false.
144a1d2d8f7a5fd956ab82acc641415e09ff6c00a7eJames Dong    bool trySettingVideoSize(int32_t width, int32_t height);
1454a857e620fecd91d051d8e58f573b5ff30d81aafNipun Kwatra
146f9b80182bcd32d563c23c12b4ac06517fd6da531Nipun Kwatra    // When video camera is used for time lapse capture, returns true
147f9b80182bcd32d563c23c12b4ac06517fd6da531Nipun Kwatra    // until enough time has passed for the next time lapse frame. When
148f9b80182bcd32d563c23c12b4ac06517fd6da531Nipun Kwatra    // the frame needs to be encoded, it returns false and also modifies
149f9b80182bcd32d563c23c12b4ac06517fd6da531Nipun Kwatra    // the time stamp to be one frame time ahead of the last encoded
150f9b80182bcd32d563c23c12b4ac06517fd6da531Nipun Kwatra    // frame's time stamp.
151f9b80182bcd32d563c23c12b4ac06517fd6da531Nipun Kwatra    bool skipFrameAndModifyTimeStamp(int64_t *timestampUs);
152f9b80182bcd32d563c23c12b4ac06517fd6da531Nipun Kwatra
153f9b80182bcd32d563c23c12b4ac06517fd6da531Nipun Kwatra    // Wrapper to enter threadTimeLapseEntry()
154f9b80182bcd32d563c23c12b4ac06517fd6da531Nipun Kwatra    static void *ThreadTimeLapseWrapper(void *me);
155f9b80182bcd32d563c23c12b4ac06517fd6da531Nipun Kwatra
156f9b80182bcd32d563c23c12b4ac06517fd6da531Nipun Kwatra    // Creates a copy of source_data into a new memory of final type MemoryBase.
157f9b80182bcd32d563c23c12b4ac06517fd6da531Nipun Kwatra    sp<IMemory> createIMemoryCopy(const sp<IMemory> &source_data);
158f9b80182bcd32d563c23c12b4ac06517fd6da531Nipun Kwatra
159f9b80182bcd32d563c23c12b4ac06517fd6da531Nipun Kwatra    CameraSourceTimeLapse(const CameraSourceTimeLapse &);
160f9b80182bcd32d563c23c12b4ac06517fd6da531Nipun Kwatra    CameraSourceTimeLapse &operator=(const CameraSourceTimeLapse &);
161f9b80182bcd32d563c23c12b4ac06517fd6da531Nipun Kwatra};
162f9b80182bcd32d563c23c12b4ac06517fd6da531Nipun Kwatra
163f9b80182bcd32d563c23c12b4ac06517fd6da531Nipun Kwatra}  // namespace android
164f9b80182bcd32d563c23c12b4ac06517fd6da531Nipun Kwatra
165f9b80182bcd32d563c23c12b4ac06517fd6da531Nipun Kwatra#endif  // CAMERA_SOURCE_TIME_LAPSE_H_
166