1 /* Copyright (c) 2015-2016, The Linux Foundation. All rights reserved. 2 * 3 * Redistribution and use in source and binary forms, with or without 4 * modification, are permitted provided that the following conditions are 5 * met: 6 * * Redistributions of source code must retain the above copyright 7 * notice, this list of conditions and the following disclaimer. 8 * * Redistributions in binary form must reproduce the above 9 * copyright notice, this list of conditions and the following 10 * disclaimer in the documentation and/or other materials provided 11 * with the distribution. 12 * * Neither the name of The Linux Foundation nor the names of its 13 * contributors may be used to endorse or promote products derived 14 * from this software without specific prior written permission. 15 * 16 * THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESS OR IMPLIED 17 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF 18 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT 19 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS 20 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 21 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 22 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR 23 * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, 24 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE 25 * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN 26 * IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 27 */ 28 29 #ifndef __QCAMERAMUXER_H__ 30 #define __QCAMERAMUXER_H__ 31 32 #include "camera.h" 33 #include "QCamera2HWI.h" 34 35 namespace qcamera { 36 37 /* Struct@ qcamera_physical_descriptor_t 38 * 39 * Description@ This structure specifies various attributes 40 * physical cameras enumerated on the device 41 */ 42 typedef struct { 43 // Userspace Physical Camera ID 44 uint32_t id; 45 // Server Camera ID 46 uint32_t camera_server_id; 47 // Device version 48 uint32_t device_version; 49 // Specifies type of camera 50 cam_sync_type_t type; 51 // Specifies mode of Camera 52 cam_sync_mode_t mode; 53 // Camera Info 54 camera_info cam_info; 55 // Reference to HWI 56 QCamera2HardwareInterface *hwi; 57 // Reference to camera device structure 58 camera_device_t* dev; 59 } qcamera_physical_descriptor_t; 60 61 /* Struct@ qcamera_logical_descriptor_t 62 * 63 * Description@ This structure stores information about logical cameras 64 * and corresponding data of the physical camera that are part of 65 * this logical camera 66 */ 67 typedef struct { 68 // Camera Device to be shared to Frameworks 69 camera_device_t dev; 70 // Device version 71 uint32_t device_version; 72 // Logical Camera ID 73 uint32_t id; 74 // Logical Camera Facing 75 int32_t facing; 76 // Number of Physical camera present in this logical camera 77 uint32_t numCameras; 78 // To signify if the LINK/UNLINK established between physical cameras 79 bool bSyncOn; 80 // index of the primary physical camera session in the bundle 81 uint8_t nPrimaryPhyCamIndex; 82 // Signifies Physical Camera ID of each camera 83 uint32_t pId[MAX_NUM_CAMERA_PER_BUNDLE]; 84 // Signifies server camera ID of each camera 85 uint32_t sId[MAX_NUM_CAMERA_PER_BUNDLE]; 86 // Signifies type of each camera 87 cam_sync_type_t type[MAX_NUM_CAMERA_PER_BUNDLE]; 88 // Signifies mode of each camera 89 cam_sync_mode_t mode[MAX_NUM_CAMERA_PER_BUNDLE]; 90 } qcamera_logical_descriptor_t; 91 92 /* Struct@ cam_compose_jpeg_info_t 93 * 94 * Description@ This structure stores information about individual Jpeg images 95 * received from multiple related physical camera instances. These images would then be 96 * composed together into a single MPO image later. 97 */ 98 typedef struct { 99 // msg_type is same as data callback msg_type 100 int32_t msg_type; 101 // ptr to actual data buffer 102 camera_memory_t *buffer; 103 // index of the buffer same as received in data callback 104 unsigned int index; 105 // metadata associated with the buffer 106 camera_frame_metadata_t *metadata; 107 // user contains the caller's identity 108 // this contains a reference to the physical cam structure 109 // of the HWI instance which had requested for this data buffer 110 void *user; 111 // this indicates validity of the buffer 112 // this flag is used by multiple threads to check validity of 113 // Jpegs received by other threads 114 bool valid; 115 // frame id of the Jpeg. this is needed for frame sync between aux 116 // and main camera sessions 117 uint32_t frame_idx; 118 // release callback function to release this Jpeg memory later after 119 // composition is completed 120 camera_release_callback release_cb; 121 // cookie for the release callback function 122 void *release_cookie; 123 // release data info for what needs to be released 124 void *release_data; 125 }cam_compose_jpeg_info_t; 126 127 /* Class@ QCameraMuxer 128 * 129 * Description@ Muxer interface 130 * a) Manages the grouping of the physical cameras into a logical camera 131 * b) Muxes the operational calls from Frameworks to HWI 132 * c) Composes MPO from JPEG 133 */ 134 class QCameraMuxer { 135 136 public: 137 /* Public Methods */ 138 QCameraMuxer(uint32_t num_of_cameras); 139 virtual ~QCameraMuxer(); 140 static void getCameraMuxer(QCameraMuxer** pCamMuxer, 141 uint32_t num_of_cameras); 142 static int get_number_of_cameras(); 143 static int get_camera_info(int camera_id, struct camera_info *info); 144 static int set_callbacks(const camera_module_callbacks_t *callbacks); 145 static int open_legacy(const struct hw_module_t* module, 146 const char* id, uint32_t halVersion, struct hw_device_t** device); 147 148 static int camera_device_open(const struct hw_module_t* module, 149 const char* id, 150 struct hw_device_t** device); 151 static int close_camera_device( hw_device_t *); 152 153 /* Operation methods directly accessed by Camera Service */ 154 static camera_device_ops_t mCameraMuxerOps; 155 156 /* Start of operational methods */ 157 static int set_preview_window(struct camera_device *, 158 struct preview_stream_ops *window); 159 static void set_callBacks(struct camera_device *, 160 camera_notify_callback notify_cb, 161 camera_data_callback data_cb, 162 camera_data_timestamp_callback data_cb_timestamp, 163 camera_request_memory get_memory, 164 void *user); 165 static void enable_msg_type(struct camera_device *, int32_t msg_type); 166 static void disable_msg_type(struct camera_device *, int32_t msg_type); 167 static int msg_type_enabled(struct camera_device *, int32_t msg_type); 168 static int start_preview(struct camera_device *); 169 static void stop_preview(struct camera_device *); 170 static int preview_enabled(struct camera_device *); 171 static int store_meta_data_in_buffers(struct camera_device *, 172 int enable); 173 static int start_recording(struct camera_device *); 174 static void stop_recording(struct camera_device *); 175 static int recording_enabled(struct camera_device *); 176 static void release_recording_frame(struct camera_device *, 177 const void *opaque); 178 static int auto_focus(struct camera_device *); 179 static int cancel_auto_focus(struct camera_device *); 180 static int take_picture(struct camera_device *); 181 static int cancel_picture(struct camera_device *); 182 static int set_parameters(struct camera_device *, const char *parms); 183 static char* get_parameters(struct camera_device *); 184 static void put_parameters(struct camera_device *, char *); 185 static int send_command(struct camera_device *, 186 int32_t cmd, int32_t arg1, int32_t arg2); 187 static void release(struct camera_device *); 188 static int dump(struct camera_device *, int fd); 189 /* End of operational methods */ 190 191 static void jpeg_data_callback(int32_t msg_type, 192 const camera_memory_t *data, unsigned int index, 193 camera_frame_metadata_t *metadata, void *user, 194 uint32_t frame_idx, camera_release_callback release_cb, 195 void *release_cookie, void *release_data); 196 // add notify error msgs to the notifer queue of the primary related cam instance 197 static int32_t sendEvtNotify(int32_t msg_type, int32_t ext1, int32_t ext2); 198 // function to compose all JPEG images from all physical related camera instances 199 void composeMpo(cam_compose_jpeg_info_t* main_Jpeg, 200 cam_compose_jpeg_info_t* aux_Jpeg); 201 static void* composeMpoRoutine(void* data); 202 static bool matchFrameId(void *data, void *user_data, void *match_data); 203 static bool findPreviousJpegs(void *data, void *user_data, void *match_data); 204 static void releaseJpegInfo(void *data, void *user_data); 205 206 public: 207 /* Public Members Variables */ 208 // Jpeg and Mpo ops need to be shared between 2 HWI instances 209 // hence these are cached in the muxer alongwith Jpeg handle 210 mm_jpeg_ops_t mJpegOps; 211 mm_jpeg_mpo_ops_t mJpegMpoOps; 212 uint32_t mJpegClientHandle; 213 // Stores Camera Data Callback function 214 camera_data_callback mDataCb; 215 // Stores Camera GetMemory Callback function 216 camera_request_memory mGetMemoryCb; 217 218 private: 219 /* Private Member Variables */ 220 qcamera_physical_descriptor_t *m_pPhyCamera; 221 qcamera_logical_descriptor_t *m_pLogicalCamera; 222 const camera_module_callbacks_t *m_pCallbacks; 223 bool m_bAuxCameraExposed; 224 uint8_t m_nPhyCameras; 225 uint8_t m_nLogicalCameras; 226 227 // Main Camera session Jpeg Queue 228 QCameraQueue m_MainJpegQ; 229 // Aux Camera session Jpeg Queue 230 QCameraQueue m_AuxJpegQ; 231 // thread for mpo composition 232 QCameraCmdThread m_ComposeMpoTh; 233 // Final Mpo Jpeg Buffer 234 camera_memory_t *m_pRelCamMpoJpeg; 235 // Lock needed to synchronize between multiple composition requests 236 pthread_mutex_t m_JpegLock; 237 // this callback cookie would be used for sending Final mpo Jpeg to the framework 238 void *m_pMpoCallbackCookie; 239 // this callback cookie would be used for caching main related cam phy instance 240 // this is needed for error scenarios 241 // incase of error, we use this cookie to get HWI instance and send errors in notify cb 242 void *m_pJpegCallbackCookie; 243 // flag to indicate whether we need to dump dual camera snapshots 244 bool m_bDumpImages; 245 // flag to indicate whether MPO is enabled or not 246 bool m_bMpoEnabled; 247 // Signifies if frame sync is enabled 248 bool m_bFrameSyncEnabled; 249 // flag to indicate whether recording hint is internally set. 250 bool m_bRecordingHintInternallySet; 251 252 /* Private Member Methods */ 253 int setupLogicalCameras(); 254 int cameraDeviceOpen(int camera_id, struct hw_device_t **hw_device); 255 int getNumberOfCameras(); 256 int getCameraInfo(int camera_id, struct camera_info *info, 257 cam_sync_type_t *p_cam_type); 258 int32_t setCallbacks(const camera_module_callbacks_t *callbacks); 259 int32_t setDataCallback(camera_data_callback data_cb); 260 int32_t setMemoryCallback(camera_request_memory get_memory); 261 qcamera_logical_descriptor_t* getLogicalCamera( 262 struct camera_device * device); 263 qcamera_physical_descriptor_t* getPhysicalCamera( 264 qcamera_logical_descriptor_t* log_cam, uint32_t index); 265 int32_t getActiveNumOfPhyCam( 266 qcamera_logical_descriptor_t* log_cam, int& numOfAcitvePhyCam); 267 int32_t setMpoCallbackCookie(void* mpoCbCookie); 268 void* getMpoCallbackCookie(); 269 int32_t setMainJpegCallbackCookie(void* jpegCbCookie); 270 void* getMainJpegCallbackCookie(); 271 void setJpegHandle(uint32_t handle) { mJpegClientHandle = handle;}; 272 // function to store single JPEG from 1 related physical camera instance 273 int32_t storeJpeg(cam_sync_type_t cam_type, int32_t msg_type, 274 const camera_memory_t *data, unsigned int index, 275 camera_frame_metadata_t *metadata, void *user, 276 uint32_t frame_idx, camera_release_callback release_cb, 277 void *release_cookie, void *release_data); 278 279 };// End namespace qcamera 280 281 } 282 #endif /* __QCAMERAMUXER_H__ */ 283 284