CameraSourceTimeLapse.h revision 0aacf105eea098a0b47761a4a9a5c4d820611f2d
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(bool useStillCameraForTimeLapse,
35        int64_t timeBetweenTimeLapseFrameCaptureUs,
36        int32_t videoFrameRate);
37
38    static CameraSourceTimeLapse *CreateFromCamera(const sp<Camera> &camera,
39        bool useStillCameraForTimeLapse,
40        int64_t timeBetweenTimeLapseFrameCaptureUs,
41        int32_t videoFrameRate);
42
43    virtual ~CameraSourceTimeLapse();
44
45private:
46    // If true, will use still camera takePicture() for time lapse frames
47    // If false, will use the videocamera frames instead.
48    bool mUseStillCameraForTimeLapse;
49
50    // Time between capture of two frames during time lapse recording
51    // Negative value indicates that timelapse is disabled.
52    int64_t mTimeBetweenTimeLapseFrameCaptureUs;
53
54    // Time between two frames in final video (1/frameRate)
55    int64_t mTimeBetweenTimeLapseVideoFramesUs;
56
57    // Real timestamp of the last encoded time lapse frame
58    int64_t mLastTimeLapseFrameRealTimestampUs;
59
60    // Thread id of thread which takes still picture and sleeps in a loop.
61    pthread_t mThreadTimeLapse;
62
63    // Variable set in dataCallbackTimestamp() to help skipCurrentFrame()
64    // to know if current frame needs to be skipped.
65    bool mSkipCurrentFrame;
66
67    // True if camera is in preview mode and ready for takePicture().
68    bool mCameraIdle;
69
70    CameraSourceTimeLapse(const sp<Camera> &camera,
71        bool useStillCameraForTimeLapse,
72        int64_t timeBetweenTimeLapseFrameCaptureUs,
73        int32_t videoFrameRate);
74
75    // For still camera case starts a thread which calls camera's takePicture()
76    // in a loop. For video camera case, just starts the camera's video recording.
77    virtual void startCameraRecording();
78
79    // For still camera case joins the thread created in startCameraRecording().
80    // For video camera case, just stops the camera's video recording.
81    virtual void stopCameraRecording();
82
83    // For still camera case don't need to do anything as memory is locally
84    // allocated with refcounting.
85    // For video camera case just tell the camera to release the frame.
86    virtual void releaseRecordingFrame(const sp<IMemory>& frame);
87
88    // mSkipCurrentFrame is set to true in dataCallbackTimestamp() if the current
89    // frame needs to be skipped and this function just returns the value of mSkipCurrentFrame.
90    virtual bool skipCurrentFrame(int64_t timestampUs);
91
92    // Handles the callback to handle raw frame data from the still camera.
93    // Creates a copy of the frame data as the camera can reuse the frame memory
94    // once this callback returns. The function also sets a new timstamp corresponding
95    // to one frame time ahead of the last encoded frame's time stamp. It then
96    // calls dataCallbackTimestamp() of the base class with the copied data and the
97    // modified timestamp, which will think that it recieved the frame from a video
98    // camera and proceed as usual.
99    virtual void dataCallback(int32_t msgType, const sp<IMemory> &data);
100
101    // In the video camera case calls skipFrameAndModifyTimeStamp() to modify
102    // timestamp and set mSkipCurrentFrame.
103    // Then it calls the base CameraSource::dataCallbackTimestamp()
104    virtual void dataCallbackTimestamp(int64_t timestampUs, int32_t msgType,
105            const sp<IMemory> &data);
106
107    // When video camera is used for time lapse capture, returns true
108    // until enough time has passed for the next time lapse frame. When
109    // the frame needs to be encoded, it returns false and also modifies
110    // the time stamp to be one frame time ahead of the last encoded
111    // frame's time stamp.
112    bool skipFrameAndModifyTimeStamp(int64_t *timestampUs);
113
114    // Wrapper to enter threadTimeLapseEntry()
115    static void *ThreadTimeLapseWrapper(void *me);
116
117    // Runs a loop which sleeps until a still picture is required
118    // and then calls mCamera->takePicture() to take the still picture.
119    // Used only in the case mUseStillCameraForTimeLapse = true.
120    void threadTimeLapseEntry();
121
122    // Wrapper to enter threadStartPreview()
123    static void *ThreadStartPreviewWrapper(void *me);
124
125    // Starts the camera's preview.
126    void threadStartPreview();
127
128    // Starts thread ThreadStartPreviewWrapper() for restarting preview.
129    // Needs to be done in a thread so that dataCallback() which calls this function
130    // can return, and the camera can know that takePicture() is done.
131    void restartPreview();
132
133    // Creates a copy of source_data into a new memory of final type MemoryBase.
134    sp<IMemory> createIMemoryCopy(const sp<IMemory> &source_data);
135
136    CameraSourceTimeLapse(const CameraSourceTimeLapse &);
137    CameraSourceTimeLapse &operator=(const CameraSourceTimeLapse &);
138};
139
140}  // namespace android
141
142#endif  // CAMERA_SOURCE_TIME_LAPSE_H_
143