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 <ui/GraphicBufferMapper.h> 31 #include "EmulatedBaseCamera.h" 32 #include "EmulatedCameraDevice.h" 33 #include "PreviewWindow.h" 34 #include "CallbackNotifier.h" 35 36 using ::android::hardware::camera::common::V1_0::helper::CameraParameters; 37 using ::android::hardware::camera::common::V1_0::helper::Size; 38 39 namespace android { 40 41 /* Encapsulates functionality common to all version 1.0 emulated camera devices 42 * ("fake", "webcam", "file stream", etc.). 43 * 44 * Note that EmulatedCameraFactory instantiates object of this class just once, 45 * when EmulatedCameraFactory instance gets constructed. Connection to / 46 * disconnection from the actual camera device is handled by calls to 47 * connectDevice(), and closeCamera() methods of this class that are ivoked in 48 * response to hw_module_methods_t::open, and camera_device::close callbacks. 49 */ 50 class EmulatedCamera : public camera_device, public EmulatedBaseCamera { 51 public: 52 /* Constructs EmulatedCamera instance. 53 * Param: 54 * cameraId - Zero based camera identifier, which is an index of the camera 55 * instance in camera factory's array. 56 * module - Emulated camera HAL module descriptor. 57 */ 58 EmulatedCamera(int cameraId, 59 struct hw_module_t* module, 60 GraphicBufferMapper* gbm); 61 62 /* Destructs EmulatedCamera instance. */ 63 virtual ~EmulatedCamera(); 64 65 /**************************************************************************** 66 * Abstract API 67 ***************************************************************************/ 68 69 public: 70 /* Gets emulated camera device used by this instance of the emulated camera. 71 */ 72 virtual EmulatedCameraDevice* getCameraDevice() = 0; 73 74 /**************************************************************************** 75 * Public API 76 ***************************************************************************/ 77 78 public: 79 /** Override of base class method */ 80 virtual status_t Initialize(); 81 82 /* Next frame is available in the camera device. 83 * This is a notification callback that is invoked by the camera device when 84 * a new frame is available. The captured frame is available through 85 * the |camera_dev| object. Remember to create a 86 * EmulatedCameraDevice::FrameLock instance to lock the frame before 87 * accessing it. 88 * Note that most likely this method is called in context of a worker thread 89 * that camera device has created for frame capturing. 90 * Param: 91 * timestamp - Frame's timestamp. 92 * camera_dev - Camera device instance that delivered the frame. 93 */ 94 virtual void onNextFrameAvailable(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 /* Signal to the callback notifier that a pictuer is being taken. */ 104 void setTakingPicture(bool takingPicture); 105 106 /**************************************************************************** 107 * Camera API implementation 108 ***************************************************************************/ 109 110 public: 111 /** Override of base class method */ 112 virtual status_t connectCamera(hw_device_t** device); 113 114 /** Override of base class method */ 115 virtual status_t closeCamera(); 116 117 /** Override of base class method */ 118 virtual status_t getCameraInfo(struct camera_info* info); 119 120 /**************************************************************************** 121 * Camera API implementation. 122 * These methods are called from the camera API callback routines. 123 ***************************************************************************/ 124 125 public: 126 /* Signal that a requested auto-focus has completed. This will be called 127 * from the camera device's worker thread. */ 128 void autoFocusComplete(); 129 130 protected: 131 /* Actual handler for camera_device_ops_t::set_preview_window callback. 132 * NOTE: When this method is called the object is locked. 133 * Note that failures in this method are reported as negave EXXX statuses. 134 */ 135 virtual status_t setPreviewWindow(struct preview_stream_ops *window); 136 137 /* Actual handler for camera_device_ops_t::set_callbacks callback. 138 * NOTE: When this method is called the object is locked. 139 */ 140 virtual void setCallbacks(camera_notify_callback notify_cb, 141 camera_data_callback data_cb, 142 camera_data_timestamp_callback data_cb_timestamp, 143 camera_request_memory get_memory, 144 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 callback. 182 * NOTE: When this method is called the object is locked. 183 * Note that 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 private: 303 status_t getConfiguredPixelFormat(uint32_t* pixelFormat) const; 304 status_t getConfiguredFrameSize(int* width, int* height) const; 305 306 307 /**************************************************************************** 308 * Camera API callbacks as defined by camera_device_ops structure. 309 * See hardware/libhardware/include/hardware/camera.h for information on 310 * each of these callbacks. Implemented in this class, these callbacks simply 311 * dispatch the call into an instance of EmulatedCamera class defined by the 312 * 'camera_device' parameter. 313 ***************************************************************************/ 314 315 private: 316 static int set_preview_window(struct camera_device* dev, 317 struct preview_stream_ops* window); 318 319 static void set_callbacks(struct camera_device* dev, 320 camera_notify_callback notify_cb, 321 camera_data_callback data_cb, 322 camera_data_timestamp_callback data_cb_timestamp, 323 camera_request_memory get_memory, 324 void* user); 325 326 static void enable_msg_type(struct camera_device* dev, int32_t msg_type); 327 328 static void disable_msg_type(struct camera_device* dev, int32_t msg_type); 329 330 static int msg_type_enabled(struct camera_device* dev, int32_t msg_type); 331 332 static int start_preview(struct camera_device* dev); 333 334 static void stop_preview(struct camera_device* dev); 335 336 static int preview_enabled(struct camera_device* dev); 337 338 static int store_meta_data_in_buffers(struct camera_device* dev, int enable); 339 340 static int start_recording(struct camera_device* dev); 341 342 static void stop_recording(struct camera_device* dev); 343 344 static int recording_enabled(struct camera_device* dev); 345 346 static void release_recording_frame(struct camera_device* dev, 347 const void* opaque); 348 349 static int auto_focus(struct camera_device* dev); 350 351 static int cancel_auto_focus(struct camera_device* dev); 352 353 static int take_picture(struct camera_device* dev); 354 355 static int cancel_picture(struct camera_device* dev); 356 357 static int set_parameters(struct camera_device* dev, const char* parms); 358 359 static char* get_parameters(struct camera_device* dev); 360 361 static void put_parameters(struct camera_device* dev, char* params); 362 363 static int send_command(struct camera_device* dev, 364 int32_t cmd, 365 int32_t arg1, 366 int32_t arg2); 367 368 static void release(struct camera_device* dev); 369 370 static int dump(struct camera_device* dev, int fd); 371 372 static int close(struct hw_device_t* device); 373 374 /**************************************************************************** 375 * Data members 376 ***************************************************************************/ 377 378 protected: 379 /* Locks this instance for parameters, state, etc. change. */ 380 Mutex mObjectLock; 381 382 /* Camera parameters. */ 383 CameraParameters mParameters; 384 385 /* Preview window. */ 386 PreviewWindow mPreviewWindow; 387 388 /* Callback notifier. */ 389 CallbackNotifier mCallbackNotifier; 390 391 private: 392 /* Registered callbacks implementing camera API. */ 393 static camera_device_ops_t mDeviceOps; 394 395 /**************************************************************************** 396 * Common keys 397 ***************************************************************************/ 398 399 public: 400 static const char FACING_KEY[]; 401 static const char ORIENTATION_KEY[]; 402 static const char RECORDING_HINT_KEY[]; 403 404 /**************************************************************************** 405 * Common string values 406 ***************************************************************************/ 407 408 /* Possible values for FACING_KEY */ 409 static const char FACING_BACK[]; 410 static const char FACING_FRONT[]; 411 }; 412 413 }; /* namespace android */ 414 415 #endif /* HW_EMULATOR_CAMERA_EMULATED_CAMERA_H */ 416