1 /*
2  * Copyright (C) 2011 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 HW_EMULATOR_CAMERA_CALLBACK_NOTIFIER_H
18 #define HW_EMULATOR_CAMERA_CALLBACK_NOTIFIER_H
19 
20 /*
21  * Contains declaration of a class CallbackNotifier that manages callbacks set
22  * via set_callbacks, enable_msg_type, and disable_msg_type camera HAL API.
23  */
24 
25 #include <utils/List.h>
26 #include <CameraParameters.h>
27 
28 using ::android::hardware::camera::common::V1_0::helper::CameraParameters;
29 using ::android::hardware::camera::common::V1_0::helper::Size;
30 
31 namespace android {
32 
33 class EmulatedCameraDevice;
34 class FrameProducer;
35 
36 /* Manages callbacks set via set_callbacks, enable_msg_type, and disable_msg_type
37  * camera HAL API.
38  *
39  * Objects of this class are contained in EmulatedCamera objects, and handle
40  * relevant camera API callbacks.
41  * Locking considerations. Apparently, it's not allowed to call callbacks
42  * registered in this class, while holding a lock: recursion is quite possible,
43  * which will cause a deadlock.
44  */
45 class CallbackNotifier {
46 public:
47     /* Constructs CallbackNotifier instance. */
48     CallbackNotifier();
49 
50     /* Destructs CallbackNotifier instance. */
51     ~CallbackNotifier();
52 
53     /****************************************************************************
54      * Camera API
55      ***************************************************************************/
56 
57 public:
58     /* Actual handler for camera_device_ops_t::set_callbacks callback.
59      * This method is called by the containing emulated camera object when it is
60      * handing the camera_device_ops_t::set_callbacks callback.
61      */
62     void setCallbacks(camera_notify_callback notify_cb,
63                       camera_data_callback data_cb,
64                       camera_data_timestamp_callback data_cb_timestamp,
65                       camera_request_memory get_memory,
66                       void* user);
67 
68     /* Actual handler for camera_device_ops_t::enable_msg_type callback.
69      * This method is called by the containing emulated camera object when it is
70      * handing the camera_device_ops_t::enable_msg_type callback.
71      */
72     void enableMessage(uint msg_type);
73 
74     /* Actual handler for camera_device_ops_t::disable_msg_type callback.
75      * This method is called by the containing emulated camera object when it is
76      * handing the camera_device_ops_t::disable_msg_type callback.
77      */
78     void disableMessage(uint msg_type);
79 
80     /* Actual handler for camera_device_ops_t::store_meta_data_in_buffers
81      * callback. This method is called by the containing emulated camera object
82      * when it is handing the camera_device_ops_t::store_meta_data_in_buffers
83      * callback.
84      * Return:
85      *  NO_ERROR on success, or an appropriate error status.
86      */
87     status_t storeMetaDataInBuffers(bool enable);
88 
89     /* Enables video recording.
90      * This method is called by the containing emulated camera object when it is
91      * handing the camera_device_ops_t::start_recording callback.
92      * Param:
93      *  fps - Video frame frequency. This parameter determins when a frame
94      *      received via onNextFrameAvailable call will be pushed through the
95      *      callback.
96      * Return:
97      *  NO_ERROR on success, or an appropriate error status.
98      */
99     status_t enableVideoRecording(int fps);
100 
101     /* Disables video recording.
102      * This method is called by the containing emulated camera object when it is
103      * handing the camera_device_ops_t::stop_recording callback.
104      */
105     void disableVideoRecording();
106 
107     /* Releases video frame, sent to the framework.
108      * This method is called by the containing emulated camera object when it is
109      * handing the camera_device_ops_t::release_recording_frame callback.
110      */
111     void releaseRecordingFrame(const void* opaque);
112 
113     /* Send a message to the notify callback that auto-focus has completed.
114      * This method is called from the containing emulated camera object when it
115      * has received confirmation from the camera device that auto-focusing is
116      * completed.
117      */
118     void autoFocusComplete();
119 
120     /* Actual handler for camera_device_ops_t::msg_type_enabled callback.
121      * This method is called by the containing emulated camera object when it is
122      * handing the camera_device_ops_t::msg_type_enabled callback.
123      * Note: this method doesn't grab a lock while checking message status, since
124      * upon exit the status would be undefined anyway. So, grab a lock before
125      * calling this method if you care about persisting a defined message status.
126      * Return:
127      *  0 if message is disabled, or non-zero value, if message is enabled.
128      */
isMessageEnabled(uint msg_type)129     inline int isMessageEnabled(uint msg_type)
130     {
131         return mMessageEnabler & msg_type;
132     }
133 
134     /* Checks id video recording is enabled.
135      * This method is called by the containing emulated camera object when it is
136      * handing the camera_device_ops_t::recording_enabled callback.
137      * Note: this method doesn't grab a lock while checking video recordin status,
138      * since upon exit the status would be undefined anyway. So, grab a lock
139      * before calling this method if you care about persisting of a defined video
140      * recording status.
141      * Return:
142      *  true if video recording is enabled, or false if it is disabled.
143      */
isVideoRecordingEnabled()144     inline bool isVideoRecordingEnabled() const
145     {
146         return mVideoRecEnabled;
147     }
148 
149     /****************************************************************************
150      * Public API
151      ***************************************************************************/
152 
153 public:
154     /* Resets the callback notifier. */
155     void cleanupCBNotifier();
156 
157     /* Next frame is available in the camera device.
158      * This is a notification callback that is invoked by the camera device when
159      * a new frame is available. The captured frame is available through the
160      * |camera_dev| obejct.
161      * Note that most likely this method is called in context of a worker thread
162      * that camera device has created for frame capturing.
163      * Param:
164      * timestamp - Frame's timestamp.
165      * camera_dev - Camera device instance that delivered the frame.
166      */
167     void onNextFrameAvailable(nsecs_t timestamp,
168                               EmulatedCameraDevice* camera_dev);
169 
170     /* Entry point for notifications that occur in camera device.
171      * Param:
172      *  err - CAMERA_ERROR_XXX error code.
173      */
174     void onCameraDeviceError(int err);
175 
176     /* Sets, or resets taking picture state.
177      * This state control whether or not to notify the framework about compressed
178      * image, shutter, and other picture related events.
179      */
setTakingPicture(bool taking)180     void setTakingPicture(bool taking)
181     {
182         mTakingPicture = taking;
183     }
184 
185     /* Sets JPEG quality used to compress frame during picture taking. */
setJpegQuality(int jpeg_quality)186     void setJpegQuality(int jpeg_quality)
187     {
188         mJpegQuality = jpeg_quality;
189     }
190 
191     /* Sets the camera parameters that will be used to populate exif data in the
192      * picture.
193      */
setCameraParameters(CameraParameters cameraParameters)194     void setCameraParameters(CameraParameters cameraParameters)
195     {
196         mCameraParameters = cameraParameters;
197     }
198 
199     /****************************************************************************
200      * Private API
201      ***************************************************************************/
202 
203 protected:
204     /* Checks if it's time to push new video frame.
205      * Note that this method must be called while object is locked.
206      * Param:
207      *  timestamp - Timestamp for the new frame. */
208     bool isNewVideoFrameTime(nsecs_t timestamp);
209 
210     /****************************************************************************
211      * Data members
212      ***************************************************************************/
213 
214 protected:
215     /* Locks this instance for data change. */
216     Mutex                           mObjectLock;
217 
218     /*
219      * Callbacks, registered in set_callbacks.
220      */
221 
222     camera_notify_callback          mNotifyCB;
223     camera_data_callback            mDataCB;
224     camera_data_timestamp_callback  mDataCBTimestamp;
225     camera_request_memory           mGetMemoryCB;
226     void*                           mCBOpaque;
227 
228     /* video frame queue for the CameraHeapMemory destruction */
229     List<camera_memory_t*>          mCameraMemoryTs;
230 
231     /* Timestamp when last frame has been delivered to the framework. */
232     nsecs_t                         mLastFrameTimestamp;
233 
234     /* Video frequency in nanosec. */
235     nsecs_t                         mFrameRefreshFreq;
236 
237     /* Message enabler. */
238     uint32_t                        mMessageEnabler;
239 
240     /* JPEG quality used to compress frame during picture taking. */
241     int                             mJpegQuality;
242 
243     /* Camera parameters used for EXIF data in picture */
244     CameraParameters                mCameraParameters;
245 
246     /* Video recording status. */
247     bool                            mVideoRecEnabled;
248 
249     /* Picture taking status. */
250     bool                            mTakingPicture;
251 };
252 
253 }; /* namespace android */
254 
255 #endif  /* HW_EMULATOR_CAMERA_CALLBACK_NOTIFIER_H */
256