CameraSourceTimeLapse.h revision 155e833a7a5fc3e193691324cf9326da1bc3289a
1/*
2 * Copyright (C) 2010 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 *      http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#ifndef CAMERA_SOURCE_TIME_LAPSE_H_
18
19#define CAMERA_SOURCE_TIME_LAPSE_H_
20
21#include <pthread.h>
22
23#include <utils/RefBase.h>
24#include <utils/threads.h>
25
26namespace android {
27
28class ICamera;
29class IMemory;
30class Camera;
31
32class CameraSourceTimeLapse : public CameraSource {
33public:
34    static CameraSourceTimeLapse *Create(
35        int64_t timeBetweenTimeLapseFrameCaptureUs,
36        int32_t width, int32_t height,
37        int32_t videoFrameRate);
38
39    static CameraSourceTimeLapse *CreateFromCamera(const sp<Camera> &camera,
40        int64_t timeBetweenTimeLapseFrameCaptureUs,
41        int32_t width, int32_t height,
42        int32_t videoFrameRate);
43
44    virtual ~CameraSourceTimeLapse();
45
46private:
47    // If true, will use still camera takePicture() for time lapse frames
48    // If false, will use the videocamera frames instead.
49    bool mUseStillCameraForTimeLapse;
50
51    // Size of picture taken from still camera. This may be larger than the size
52    // of the video, as still camera may not support the exact video resolution
53    // demanded. See setPictureSizeToClosestSupported().
54    int32_t mPictureWidth;
55    int32_t mPictureHeight;
56
57    // size of the encoded video.
58    int32_t mVideoWidth;
59    int32_t mVideoHeight;
60
61    // True if we need to crop the still camera image to get the video frame.
62    bool mNeedCropping;
63
64    // Start location of the cropping rectangle.
65    int32_t mCropRectStartX;
66    int32_t mCropRectStartY;
67
68    // Time between capture of two frames during time lapse recording
69    // Negative value indicates that timelapse is disabled.
70    int64_t mTimeBetweenTimeLapseFrameCaptureUs;
71
72    // Time between two frames in final video (1/frameRate)
73    int64_t mTimeBetweenTimeLapseVideoFramesUs;
74
75    // Real timestamp of the last encoded time lapse frame
76    int64_t mLastTimeLapseFrameRealTimestampUs;
77
78    // Thread id of thread which takes still picture and sleeps in a loop.
79    pthread_t mThreadTimeLapse;
80
81    // Variable set in dataCallbackTimestamp() to help skipCurrentFrame()
82    // to know if current frame needs to be skipped.
83    bool mSkipCurrentFrame;
84
85    // True if camera is in preview mode and ready for takePicture().
86    bool mCameraIdle;
87
88    CameraSourceTimeLapse(const sp<Camera> &camera,
89        int64_t timeBetweenTimeLapseFrameCaptureUs,
90        int32_t width, int32_t height,
91        int32_t videoFrameRate);
92
93    // For still camera case starts a thread which calls camera's takePicture()
94    // in a loop. For video camera case, just starts the camera's video recording.
95    virtual void startCameraRecording();
96
97    // For still camera case joins the thread created in startCameraRecording().
98    // For video camera case, just stops the camera's video recording.
99    virtual void stopCameraRecording();
100
101    // For still camera case don't need to do anything as memory is locally
102    // allocated with refcounting.
103    // For video camera case just tell the camera to release the frame.
104    virtual void releaseRecordingFrame(const sp<IMemory>& frame);
105
106    // mSkipCurrentFrame is set to true in dataCallbackTimestamp() if the current
107    // frame needs to be skipped and this function just returns the value of mSkipCurrentFrame.
108    virtual bool skipCurrentFrame(int64_t timestampUs);
109
110    // Handles the callback to handle raw frame data from the still camera.
111    // Creates a copy of the frame data as the camera can reuse the frame memory
112    // once this callback returns. The function also sets a new timstamp corresponding
113    // to one frame time ahead of the last encoded frame's time stamp. It then
114    // calls dataCallbackTimestamp() of the base class with the copied data and the
115    // modified timestamp, which will think that it recieved the frame from a video
116    // camera and proceed as usual.
117    virtual void dataCallback(int32_t msgType, const sp<IMemory> &data);
118
119    // In the video camera case calls skipFrameAndModifyTimeStamp() to modify
120    // timestamp and set mSkipCurrentFrame.
121    // Then it calls the base CameraSource::dataCallbackTimestamp()
122    virtual void dataCallbackTimestamp(int64_t timestampUs, int32_t msgType,
123            const sp<IMemory> &data);
124
125    // If the passed in size (width x height) is a supported preview size,
126    // the function sets the camera's preview size to it and returns true.
127    // Otherwise returns false.
128    bool trySettingPreviewSize(int32_t width, int32_t height);
129
130    // The still camera may not support the demanded video width and height.
131    // We look for the supported picture sizes from the still camera and
132    // choose the smallest one with either dimensions higher than the corresponding
133    // video dimensions. The still picture will be cropped to get the video frame.
134    // The function returns true if the camera supports picture sizes greater than
135    // or equal to the passed in width and height, and false otherwise.
136    bool setPictureSizeToClosestSupported(int32_t width, int32_t height);
137
138    // Computes the offset of the rectangle from where to start cropping the
139    // still image into the video frame. We choose the center of the image to be
140    // cropped. The offset is stored in (mCropRectStartX, mCropRectStartY).
141    bool computeCropRectangleOffset();
142
143    // Crops the source data into a smaller image starting at
144    // (mCropRectStartX, mCropRectStartY) and of the size of the video frame.
145    // The data is returned into a newly allocated IMemory.
146    sp<IMemory> cropYUVImage(const sp<IMemory> &source_data);
147
148    // When video camera is used for time lapse capture, returns true
149    // until enough time has passed for the next time lapse frame. When
150    // the frame needs to be encoded, it returns false and also modifies
151    // the time stamp to be one frame time ahead of the last encoded
152    // frame's time stamp.
153    bool skipFrameAndModifyTimeStamp(int64_t *timestampUs);
154
155    // Wrapper to enter threadTimeLapseEntry()
156    static void *ThreadTimeLapseWrapper(void *me);
157
158    // Runs a loop which sleeps until a still picture is required
159    // and then calls mCamera->takePicture() to take the still picture.
160    // Used only in the case mUseStillCameraForTimeLapse = true.
161    void threadTimeLapseEntry();
162
163    // Wrapper to enter threadStartPreview()
164    static void *ThreadStartPreviewWrapper(void *me);
165
166    // Starts the camera's preview.
167    void threadStartPreview();
168
169    // Starts thread ThreadStartPreviewWrapper() for restarting preview.
170    // Needs to be done in a thread so that dataCallback() which calls this function
171    // can return, and the camera can know that takePicture() is done.
172    void restartPreview();
173
174    // Creates a copy of source_data into a new memory of final type MemoryBase.
175    sp<IMemory> createIMemoryCopy(const sp<IMemory> &source_data);
176
177    CameraSourceTimeLapse(const CameraSourceTimeLapse &);
178    CameraSourceTimeLapse &operator=(const CameraSourceTimeLapse &);
179};
180
181}  // namespace android
182
183#endif  // CAMERA_SOURCE_TIME_LAPSE_H_
184