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