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