2 * Copyright (C) 2011 The Android Open Source Project
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 #ifndef HW_EMULATOR_CAMERA_EMULATED_CAMERA_H
18 #define HW_EMULATOR_CAMERA_EMULATED_CAMERA_H
21 * Contains declaration of a class EmulatedCamera that encapsulates
22 * functionality common to all version 1.0 emulated camera devices ("fake",
23 * "webcam", "video file", etc.). Instances of this class (for each emulated
24 * camera) are created during the construction of the EmulatedCameraFactory
25 * instance. This class serves as an entry point for all camera API calls that
26 * defined by camera_device_ops_t API.
29 #include <camera/CameraParameters.h>
30 #include "EmulatedBaseCamera.h"
31 #include "EmulatedCameraDevice.h"
32 #include "PreviewWindow.h"
33 #include "CallbackNotifier.h"
37 /* Encapsulates functionality common to all version 1.0 emulated camera devices
38 * ("fake", "webcam", "file stream", etc.).
40 * Note that EmulatedCameraFactory instantiates object of this class just once,
41 * when EmulatedCameraFactory instance gets constructed. Connection to /
42 * disconnection from the actual camera device is handled by calls to
43 * connectDevice(), and closeCamera() methods of this class that are ivoked in
44 * response to hw_module_methods_t::open, and camera_device::close callbacks.
46 class EmulatedCamera : public camera_device, public EmulatedBaseCamera {
48 /* Constructs EmulatedCamera instance.
50 * cameraId - Zero based camera identifier, which is an index of the camera
51 * instance in camera factory's array.
52 * module - Emulated camera HAL module descriptor.
54 EmulatedCamera(int cameraId,
55 struct hw_module_t* module);
57 /* Destructs EmulatedCamera instance. */
58 virtual ~EmulatedCamera();
60 /****************************************************************************
62 ***************************************************************************/
65 /* Gets emulated camera device used by this instance of the emulated camera.
67 virtual EmulatedCameraDevice* getCameraDevice() = 0;
69 /****************************************************************************
71 ***************************************************************************/
74 /** Override of base class method */
75 virtual status_t Initialize();
77 /* Next frame is available in the camera device.
78 * This is a notification callback that is invoked by the camera device when
79 * a new frame is available.
80 * Note that most likely this method is called in context of a worker thread
81 * that camera device has created for frame capturing.
83 * frame - Captured frame, or NULL if camera device didn't pull the frame
84 * yet. If NULL is passed in this parameter use GetCurrentFrame method
85 * of the camera device class to obtain the next frame. Also note that
86 * the size of the frame that is passed here (as well as the frame
87 * returned from the GetCurrentFrame method) is defined by the current
88 * frame settings (width + height + pixel format) for the camera device.
89 * timestamp - Frame's timestamp.
90 * camera_dev - Camera device instance that delivered the frame.
92 virtual void onNextFrameAvailable(const void* frame,
94 EmulatedCameraDevice* camera_dev);
96 /* Entry point for notifications that occur in camera device.
98 * err - CAMERA_ERROR_XXX error code.
100 virtual void onCameraDeviceError(int err);
102 /****************************************************************************
103 * Camera API implementation
104 ***************************************************************************/
107 /** Override of base class method */
108 virtual status_t connectCamera(hw_device_t** device);
110 /** Override of base class method */
111 virtual status_t closeCamera();
113 /** Override of base class method */
114 virtual status_t getCameraInfo(struct camera_info* info);
116 /****************************************************************************
117 * Camera API implementation.
118 * These methods are called from the camera API callback routines.
119 ***************************************************************************/
122 /* Actual handler for camera_device_ops_t::set_preview_window callback.
123 * NOTE: When this method is called the object is locked.
124 * Note that failures in this method are reported as negave EXXX statuses.
126 virtual status_t setPreviewWindow(struct preview_stream_ops *window);
128 /* Actual handler for camera_device_ops_t::set_callbacks callback.
129 * NOTE: When this method is called the object is locked.
131 virtual void setCallbacks(camera_notify_callback notify_cb,
132 camera_data_callback data_cb,
133 camera_data_timestamp_callback data_cb_timestamp,
134 camera_request_memory get_memory,
137 /* Actual handler for camera_device_ops_t::enable_msg_type callback.
138 * NOTE: When this method is called the object is locked.
140 virtual void enableMsgType(int32_t msg_type);
142 /* Actual handler for camera_device_ops_t::disable_msg_type callback.
143 * NOTE: When this method is called the object is locked.
145 virtual void disableMsgType(int32_t msg_type);
147 /* Actual handler for camera_device_ops_t::msg_type_enabled callback.
148 * NOTE: When this method is called the object is locked.
150 * 0 if message(s) is (are) disabled, != 0 if enabled.
152 virtual int isMsgTypeEnabled(int32_t msg_type);
154 /* Actual handler for camera_device_ops_t::start_preview callback.
155 * NOTE: When this method is called the object is locked.
156 * Note that failures in this method are reported as negave EXXX statuses.
158 virtual status_t startPreview();
160 /* Actual handler for camera_device_ops_t::stop_preview callback.
161 * NOTE: When this method is called the object is locked.
163 virtual void stopPreview();
165 /* Actual handler for camera_device_ops_t::preview_enabled callback.
166 * NOTE: When this method is called the object is locked.
168 * 0 if preview is disabled, != 0 if enabled.
170 virtual int isPreviewEnabled();
172 /* Actual handler for camera_device_ops_t::store_meta_data_in_buffers callback.
173 * NOTE: When this method is called the object is locked.
174 * Note that failures in this method are reported as negave EXXX statuses.
176 virtual status_t storeMetaDataInBuffers(int enable);
178 /* Actual handler for camera_device_ops_t::start_recording callback.
179 * NOTE: When this method is called the object is locked.
180 * Note that failures in this method are reported as negave EXXX statuses.
182 virtual status_t startRecording();
184 /* Actual handler for camera_device_ops_t::stop_recording callback.
185 * NOTE: When this method is called the object is locked.
187 virtual void stopRecording();
189 /* Actual handler for camera_device_ops_t::recording_enabled callback.
190 * NOTE: When this method is called the object is locked.
192 * 0 if recording is disabled, != 0 if enabled.
194 virtual int isRecordingEnabled();
196 /* Actual handler for camera_device_ops_t::release_recording_frame callback.
197 * NOTE: When this method is called the object is locked.
199 virtual void releaseRecordingFrame(const void* opaque);
201 /* Actual handler for camera_device_ops_t::auto_focus callback.
202 * NOTE: When this method is called the object is locked.
203 * Note that failures in this method are reported as negave EXXX statuses.
205 virtual status_t setAutoFocus();
207 /* Actual handler for camera_device_ops_t::cancel_auto_focus callback.
208 * NOTE: When this method is called the object is locked.
209 * Note that failures in this method are reported as negave EXXX statuses.
211 virtual status_t cancelAutoFocus();
213 /* Actual handler for camera_device_ops_t::take_picture callback.
214 * NOTE: When this method is called the object is locked.
215 * Note that failures in this method are reported as negave EXXX statuses.
217 virtual status_t takePicture();
219 /* Actual handler for camera_device_ops_t::cancel_picture callback.
220 * NOTE: When this method is called the object is locked.
221 * Note that failures in this method are reported as negave EXXX statuses.
223 virtual status_t cancelPicture();
225 /* Actual handler for camera_device_ops_t::set_parameters callback.
226 * NOTE: When this method is called the object is locked.
227 * Note that failures in this method are reported as negave EXXX statuses.
229 virtual status_t setParameters(const char* parms);
231 /* Actual handler for camera_device_ops_t::get_parameters callback.
232 * NOTE: When this method is called the object is locked.
234 * Flattened parameters string. The caller will free the buffer allocated
235 * for the string by calling camera_device_ops_t::put_parameters callback.
237 virtual char* getParameters();
239 /* Actual handler for camera_device_ops_t::put_parameters callback.
240 * Called to free the string returned from camera_device_ops_t::get_parameters
241 * callback. There is nothing more to it: the name of the callback is just
243 * NOTE: When this method is called the object is locked.
245 virtual void putParameters(char* params);
247 /* Actual handler for camera_device_ops_t::send_command callback.
248 * NOTE: When this method is called the object is locked.
249 * Note that failures in this method are reported as negave EXXX statuses.
251 virtual status_t sendCommand(int32_t cmd, int32_t arg1, int32_t arg2);
253 /* Actual handler for camera_device_ops_t::release callback.
254 * NOTE: When this method is called the object is locked.
256 virtual void releaseCamera();
258 /* Actual handler for camera_device_ops_t::dump callback.
259 * NOTE: When this method is called the object is locked.
260 * Note that failures in this method are reported as negave EXXX statuses.
262 virtual status_t dumpCamera(int fd);
264 /****************************************************************************
265 * Preview management.
266 ***************************************************************************/
270 * Note that when this method is called mPreviewWindow may be NULL,
271 * indicating that framework has an intention to start displaying video
272 * frames, but didn't create the preview window yet.
274 * NO_ERROR on success, or an appropriate error status on failure.
276 virtual status_t doStartPreview();
279 * This method reverts DoStartPreview.
281 * NO_ERROR on success, or an appropriate error status on failure.
283 virtual status_t doStopPreview();
285 /****************************************************************************
287 ***************************************************************************/
290 /* Cleans up camera when released. */
291 virtual status_t cleanupCamera();
293 /****************************************************************************
294 * Camera API callbacks as defined by camera_device_ops structure.
295 * See hardware/libhardware/include/hardware/camera.h for information on
296 * each of these callbacks. Implemented in this class, these callbacks simply
297 * dispatch the call into an instance of EmulatedCamera class defined by the
298 * 'camera_device' parameter.
299 ***************************************************************************/
302 static int set_preview_window(struct camera_device* dev,
303 struct preview_stream_ops* window);
305 static void set_callbacks(struct camera_device* dev,
306 camera_notify_callback notify_cb,
307 camera_data_callback data_cb,
308 camera_data_timestamp_callback data_cb_timestamp,
309 camera_request_memory get_memory,
312 static void enable_msg_type(struct camera_device* dev, int32_t msg_type);
314 static void disable_msg_type(struct camera_device* dev, int32_t msg_type);
316 static int msg_type_enabled(struct camera_device* dev, int32_t msg_type);
318 static int start_preview(struct camera_device* dev);
320 static void stop_preview(struct camera_device* dev);
322 static int preview_enabled(struct camera_device* dev);
324 static int store_meta_data_in_buffers(struct camera_device* dev, int enable);
326 static int start_recording(struct camera_device* dev);
328 static void stop_recording(struct camera_device* dev);
330 static int recording_enabled(struct camera_device* dev);
332 static void release_recording_frame(struct camera_device* dev,
335 static int auto_focus(struct camera_device* dev);
337 static int cancel_auto_focus(struct camera_device* dev);
339 static int take_picture(struct camera_device* dev);
341 static int cancel_picture(struct camera_device* dev);
343 static int set_parameters(struct camera_device* dev, const char* parms);
345 static char* get_parameters(struct camera_device* dev);
347 static void put_parameters(struct camera_device* dev, char* params);
349 static int send_command(struct camera_device* dev,
354 static void release(struct camera_device* dev);
356 static int dump(struct camera_device* dev, int fd);
358 static int close(struct hw_device_t* device);
360 /****************************************************************************
362 ***************************************************************************/
365 /* Locks this instance for parameters, state, etc. change. */
368 /* Camera parameters. */
369 CameraParameters mParameters;
371 /* Preview window. */
372 PreviewWindow mPreviewWindow;
374 /* Callback notifier. */
375 CallbackNotifier mCallbackNotifier;
378 /* Registered callbacks implementing camera API. */
379 static camera_device_ops_t mDeviceOps;
381 /****************************************************************************
383 ***************************************************************************/
386 static const char FACING_KEY[];
387 static const char ORIENTATION_KEY[];
388 static const char RECORDING_HINT_KEY[];
390 /****************************************************************************
391 * Common string values
392 ***************************************************************************/
394 /* Possible values for FACING_KEY */
395 static const char FACING_BACK[];
396 static const char FACING_FRONT[];
399 }; /* namespace android */
401 #endif /* HW_EMULATOR_CAMERA_EMULATED_CAMERA_H */