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_EMULATED_CAMERA_H 18 #define HW_EMULATOR_CAMERA_EMULATED_CAMERA_H 19 20 /* 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. 27 */ 28 29 #include <CameraParameters.h> 30 using ::android::hardware::camera::common::V1_0::helper::CameraParameters; 31 using ::android::hardware::camera::common::V1_0::helper::Size; 32 33 #include "CallbackNotifier.h" 34 #include "EmulatedBaseCamera.h" 35 #include "EmulatedCameraDevice.h" 36 #include "PreviewWindow.h" 37 38 namespace android { 39 40 /* Encapsulates functionality common to all version 1.0 emulated camera devices 41 * ("fake", "webcam", "file stream", etc.). 42 * 43 * Note that EmulatedCameraFactory instantiates object of this class just once, 44 * when EmulatedCameraFactory instance gets constructed. Connection to / 45 * disconnection from the actual camera device is handled by calls to 46 * connectDevice(), and closeCamera() methods of this class that are ivoked in 47 * response to hw_module_methods_t::open, and camera_device::close callbacks. 48 */ 49 class EmulatedCamera : public camera_device, public EmulatedBaseCamera { 50 public: 51 /* Constructs EmulatedCamera instance. 52 * Param: 53 * cameraId - Zero based camera identifier, which is an index of the camera 54 * instance in camera factory's array. 55 * module - Emulated camera HAL module descriptor. 56 */ 57 EmulatedCamera(int cameraId, struct hw_module_t* module); 58 59 /* Destructs EmulatedCamera instance. */ 60 virtual ~EmulatedCamera(); 61 62 /**************************************************************************** 63 * Abstract API 64 ***************************************************************************/ 65 66 public: 67 /* Gets emulated camera device used by this instance of the emulated camera. 68 */ 69 virtual EmulatedCameraDevice* getCameraDevice() = 0; 70 71 /**************************************************************************** 72 * Public API 73 ***************************************************************************/ 74 75 public: 76 /** Override of base class method */ 77 virtual status_t Initialize(const cuttlefish::CameraDefinition& properties); 78 79 /* Next frame is available in the camera device. 80 * This is a notification callback that is invoked by the camera device when 81 * a new frame is available. 82 * Note that most likely this method is called in context of a worker thread 83 * that camera device has created for frame capturing. 84 * Param: 85 * frame - Captured frame, or NULL if camera device didn't pull the frame 86 * yet. If NULL is passed in this parameter use GetCurrentFrame method 87 * of the camera device class to obtain the next frame. Also note that 88 * the size of the frame that is passed here (as well as the frame 89 * returned from the GetCurrentFrame method) is defined by the current 90 * frame settings (width + height + pixel format) for the camera device. 91 * timestamp - Frame's timestamp. 92 * camera_dev - Camera device instance that delivered the frame. 93 */ 94 virtual void onNextFrameAvailable(const void* frame, nsecs_t timestamp, 95 EmulatedCameraDevice* camera_dev); 96 97 /* Entry point for notifications that occur in camera device. 98 * Param: 99 * err - CAMERA_ERROR_XXX error code. 100 */ 101 virtual void onCameraDeviceError(int err); 102 103 /* Device acquired focus. 104 * This is a notification callback that is invoked by the camera device 105 * when focusing operation (requested by client) completes. 106 */ 107 virtual void onCameraFocusAcquired(); 108 109 /**************************************************************************** 110 * Camera API implementation 111 ***************************************************************************/ 112 113 public: 114 /** Override of base class method */ 115 virtual status_t connectCamera(hw_device_t** device); 116 117 /** Override of base class method */ 118 virtual status_t closeCamera(); 119 120 /** Override of base class method */ 121 virtual status_t getCameraInfo(struct camera_info* info); 122 123 /** Override of base class method */ 124 virtual const CameraParameters* getCameraParameters() override; 125 126 /**************************************************************************** 127 * Camera API implementation. 128 * These methods are called from the camera API callback routines. 129 ***************************************************************************/ 130 131 protected: 132 /* Actual handler for camera_device_ops_t::set_preview_window callback. 133 * NOTE: When this method is called the object is locked. 134 * Note that failures in this method are reported as negave EXXX statuses. 135 */ 136 virtual status_t setPreviewWindow(struct preview_stream_ops* window); 137 138 /* Actual handler for camera_device_ops_t::set_callbacks callback. 139 * NOTE: When this method is called the object is locked. 140 */ 141 virtual void setCallbacks(camera_notify_callback notify_cb, 142 camera_data_callback data_cb, 143 camera_data_timestamp_callback data_cb_timestamp, 144 camera_request_memory get_memory, void* user); 145 146 /* Actual handler for camera_device_ops_t::enable_msg_type callback. 147 * NOTE: When this method is called the object is locked. 148 */ 149 virtual void enableMsgType(int32_t msg_type); 150 151 /* Actual handler for camera_device_ops_t::disable_msg_type callback. 152 * NOTE: When this method is called the object is locked. 153 */ 154 virtual void disableMsgType(int32_t msg_type); 155 156 /* Actual handler for camera_device_ops_t::msg_type_enabled callback. 157 * NOTE: When this method is called the object is locked. 158 * Return: 159 * 0 if message(s) is (are) disabled, != 0 if enabled. 160 */ 161 virtual int isMsgTypeEnabled(int32_t msg_type); 162 163 /* Actual handler for camera_device_ops_t::start_preview callback. 164 * NOTE: When this method is called the object is locked. 165 * Note that failures in this method are reported as negave EXXX statuses. 166 */ 167 virtual status_t startPreview(); 168 169 /* Actual handler for camera_device_ops_t::stop_preview callback. 170 * NOTE: When this method is called the object is locked. 171 */ 172 virtual void stopPreview(); 173 174 /* Actual handler for camera_device_ops_t::preview_enabled callback. 175 * NOTE: When this method is called the object is locked. 176 * Return: 177 * 0 if preview is disabled, != 0 if enabled. 178 */ 179 virtual int isPreviewEnabled(); 180 181 /* Actual handler for camera_device_ops_t::store_meta_data_in_buffers 182 * callback. NOTE: When this method is called the object is locked. Note that 183 * failures in this method are reported as negave EXXX statuses. 184 */ 185 virtual status_t storeMetaDataInBuffers(int enable); 186 187 /* Actual handler for camera_device_ops_t::start_recording callback. 188 * NOTE: When this method is called the object is locked. 189 * Note that failures in this method are reported as negave EXXX statuses. 190 */ 191 virtual status_t startRecording(); 192 193 /* Actual handler for camera_device_ops_t::stop_recording callback. 194 * NOTE: When this method is called the object is locked. 195 */ 196 virtual void stopRecording(); 197 198 /* Actual handler for camera_device_ops_t::recording_enabled callback. 199 * NOTE: When this method is called the object is locked. 200 * Return: 201 * 0 if recording is disabled, != 0 if enabled. 202 */ 203 virtual int isRecordingEnabled(); 204 205 /* Actual handler for camera_device_ops_t::release_recording_frame callback. 206 * NOTE: When this method is called the object is locked. 207 */ 208 virtual void releaseRecordingFrame(const void* opaque); 209 210 /* Actual handler for camera_device_ops_t::auto_focus callback. 211 * NOTE: When this method is called the object is locked. 212 * Note that failures in this method are reported as negave EXXX statuses. 213 */ 214 virtual status_t setAutoFocus(); 215 216 /* Actual handler for camera_device_ops_t::cancel_auto_focus callback. 217 * NOTE: When this method is called the object is locked. 218 * Note that failures in this method are reported as negave EXXX statuses. 219 */ 220 virtual status_t cancelAutoFocus(); 221 222 /* Actual handler for camera_device_ops_t::take_picture callback. 223 * NOTE: When this method is called the object is locked. 224 * Note that failures in this method are reported as negave EXXX statuses. 225 */ 226 virtual status_t takePicture(); 227 228 /* Actual handler for camera_device_ops_t::cancel_picture callback. 229 * NOTE: When this method is called the object is locked. 230 * Note that failures in this method are reported as negave EXXX statuses. 231 */ 232 virtual status_t cancelPicture(); 233 234 /* Actual handler for camera_device_ops_t::set_parameters callback. 235 * NOTE: When this method is called the object is locked. 236 * Note that failures in this method are reported as negave EXXX statuses. 237 */ 238 virtual status_t setParameters(const char* parms); 239 240 /* Actual handler for camera_device_ops_t::get_parameters callback. 241 * NOTE: When this method is called the object is locked. 242 * Return: 243 * Flattened parameters string. The caller will free the buffer allocated 244 * for the string by calling camera_device_ops_t::put_parameters callback. 245 */ 246 virtual char* getParameters(); 247 248 /* Actual handler for camera_device_ops_t::put_parameters callback. 249 * Called to free the string returned from camera_device_ops_t::get_parameters 250 * callback. There is nothing more to it: the name of the callback is just 251 * misleading. 252 * NOTE: When this method is called the object is locked. 253 */ 254 virtual void putParameters(char* params); 255 256 /* Actual handler for camera_device_ops_t::send_command callback. 257 * NOTE: When this method is called the object is locked. 258 * Note that failures in this method are reported as negave EXXX statuses. 259 */ 260 virtual status_t sendCommand(int32_t cmd, int32_t arg1, int32_t arg2); 261 262 /* Actual handler for camera_device_ops_t::release callback. 263 * NOTE: When this method is called the object is locked. 264 */ 265 virtual void releaseCamera(); 266 267 /* Actual handler for camera_device_ops_t::dump callback. 268 * NOTE: When this method is called the object is locked. 269 * Note that failures in this method are reported as negave EXXX statuses. 270 */ 271 virtual status_t dumpCamera(int fd); 272 273 /**************************************************************************** 274 * Preview management. 275 ***************************************************************************/ 276 277 protected: 278 /* Starts preview. 279 * Note that when this method is called mPreviewWindow may be NULL, 280 * indicating that framework has an intention to start displaying video 281 * frames, but didn't create the preview window yet. 282 * Return: 283 * NO_ERROR on success, or an appropriate error status on failure. 284 */ 285 virtual status_t doStartPreview(); 286 287 /* Stops preview. 288 * This method reverts DoStartPreview. 289 * Return: 290 * NO_ERROR on success, or an appropriate error status on failure. 291 */ 292 virtual status_t doStopPreview(); 293 294 /**************************************************************************** 295 * Private API. 296 ***************************************************************************/ 297 298 protected: 299 /* Cleans up camera when released. */ 300 virtual status_t cleanupCamera(); 301 302 /**************************************************************************** 303 * Camera API callbacks as defined by camera_device_ops structure. 304 * See hardware/libhardware/include/hardware/camera.h for information on 305 * each of these callbacks. Implemented in this class, these callbacks simply 306 * dispatch the call into an instance of EmulatedCamera class defined by the 307 * 'camera_device' parameter. 308 ***************************************************************************/ 309 310 private: 311 static int set_preview_window(struct camera_device* dev, 312 struct preview_stream_ops* window); 313 314 static void set_callbacks(struct camera_device* dev, 315 camera_notify_callback notify_cb, 316 camera_data_callback data_cb, 317 camera_data_timestamp_callback data_cb_timestamp, 318 camera_request_memory get_memory, void* user); 319 320 static void enable_msg_type(struct camera_device* dev, int32_t msg_type); 321 322 static void disable_msg_type(struct camera_device* dev, int32_t msg_type); 323 324 static int msg_type_enabled(struct camera_device* dev, int32_t msg_type); 325 326 static int start_preview(struct camera_device* dev); 327 328 static void stop_preview(struct camera_device* dev); 329 330 static int preview_enabled(struct camera_device* dev); 331 332 static int store_meta_data_in_buffers(struct camera_device* dev, int enable); 333 334 static int start_recording(struct camera_device* dev); 335 336 static void stop_recording(struct camera_device* dev); 337 338 static int recording_enabled(struct camera_device* dev); 339 340 static void release_recording_frame(struct camera_device* dev, 341 const void* opaque); 342 343 static int auto_focus(struct camera_device* dev); 344 345 static int cancel_auto_focus(struct camera_device* dev); 346 347 static int take_picture(struct camera_device* dev); 348 349 static int cancel_picture(struct camera_device* dev); 350 351 static int set_parameters(struct camera_device* dev, const char* parms); 352 353 static char* get_parameters(struct camera_device* dev); 354 355 static void put_parameters(struct camera_device* dev, char* params); 356 357 static int send_command(struct camera_device* dev, int32_t cmd, int32_t arg1, 358 int32_t arg2); 359 360 static void release(struct camera_device* dev); 361 362 static int dump(struct camera_device* dev, int fd); 363 364 static int close(struct hw_device_t* device); 365 366 /**************************************************************************** 367 * Data members 368 ***************************************************************************/ 369 370 protected: 371 /* Locks this instance for parameters, state, etc. change. */ 372 Mutex mObjectLock; 373 374 /* Camera parameters. */ 375 CameraParameters mParameters; 376 377 /* Preview window. */ 378 PreviewWindow mPreviewWindow; 379 380 /* Callback notifier. */ 381 CallbackNotifier mCallbackNotifier; 382 383 private: 384 /* Registered callbacks implementing camera API. */ 385 static camera_device_ops_t mDeviceOps; 386 387 /**************************************************************************** 388 * Common keys 389 ***************************************************************************/ 390 391 public: 392 static const char FACING_KEY[]; 393 static const char ORIENTATION_KEY[]; 394 static const char RECORDING_HINT_KEY[]; 395 396 /**************************************************************************** 397 * Common string values 398 ***************************************************************************/ 399 400 /* Possible values for FACING_KEY */ 401 static const char FACING_BACK[]; 402 static const char FACING_FRONT[]; 403 }; 404 405 }; /* namespace android */ 406 407 #endif /* HW_EMULATOR_CAMERA_EMULATED_CAMERA_H */ 408