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