Home | History | Annotate | Download | only in HAL
      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 #include "QCamera3HWI.h"
     35 
     36 namespace qcamera {
     37 
     38 /* Struct@ qcamera_physical_descriptor_t
     39  *
     40  *  Description@ This structure specifies various attributes
     41  *      physical cameras enumerated on the device
     42  */
     43 typedef struct {
     44     // Userspace Physical Camera ID
     45     uint32_t id;
     46     // Server Camera ID
     47     uint32_t camera_server_id;
     48     // Device version
     49     uint32_t device_version;
     50     // Specifies type of camera
     51     cam_sync_type_t type;
     52     // Specifies mode of Camera
     53     cam_sync_mode_t mode;
     54     // Camera Info
     55     camera_info cam_info;
     56     // Reference to HWI
     57     QCamera2HardwareInterface *hwi;
     58     // Reference to camera device structure
     59     camera_device_t* dev;
     60 } qcamera_physical_descriptor_t;
     61 
     62 /* Struct@ qcamera_logical_descriptor_t
     63  *
     64  *  Description@ This structure stores information about logical cameras
     65  *      and corresponding data of the physical camera that are part of
     66  *      this logical camera
     67  */
     68 typedef struct {
     69     // Camera Device to be shared to Frameworks
     70     camera_device_t dev;
     71     // Device version
     72     uint32_t device_version;
     73     // Logical Camera ID
     74     uint32_t id;
     75     // Logical Camera Facing
     76     int32_t facing;
     77     // Number of Physical camera present in this logical camera
     78     uint32_t numCameras;
     79     // To signify if the LINK/UNLINK established between physical cameras
     80     bool bSyncOn;
     81     // index of the primary physical camera session in the bundle
     82     uint8_t nPrimaryPhyCamIndex;
     83     // Signifies Physical Camera ID of each camera
     84     uint32_t pId[MAX_NUM_CAMERA_PER_BUNDLE];
     85     // Signifies server camera ID of each camera
     86     uint32_t sId[MAX_NUM_CAMERA_PER_BUNDLE];
     87     // Signifies type of each camera
     88     cam_sync_type_t type[MAX_NUM_CAMERA_PER_BUNDLE];
     89     // Signifies mode of each camera
     90     cam_sync_mode_t mode[MAX_NUM_CAMERA_PER_BUNDLE];
     91 } qcamera_logical_descriptor_t;
     92 
     93 /* Struct@ cam_compose_jpeg_info_t
     94  *
     95  *  Description@ This structure stores information about individual Jpeg images
     96  *  received from multiple related physical camera instances. These images would then be
     97  *  composed together into a single MPO image later.
     98  */
     99 typedef struct {
    100     // msg_type is same as data callback msg_type
    101     int32_t msg_type;
    102     // ptr to actual data buffer
    103     camera_memory_t *buffer;
    104     // index of the buffer same as received in data callback
    105     unsigned int index;
    106     // metadata associated with the buffer
    107     camera_frame_metadata_t *metadata;
    108     // user contains the caller's identity
    109     // this contains a reference to the physical cam structure
    110     // of the HWI instance which had requested for this data buffer
    111     void *user;
    112     // this indicates validity of the buffer
    113     // this flag is used by multiple threads to check validity of
    114     // Jpegs received by other threads
    115     bool valid;
    116     // frame id of the Jpeg. this is needed for frame sync between aux
    117     // and main camera sessions
    118     uint32_t frame_idx;
    119     // release callback function to release this Jpeg memory later after
    120     // composition is completed
    121     camera_release_callback release_cb;
    122     // cookie for the release callback function
    123     void *release_cookie;
    124     // release data info for what needs to be released
    125     void *release_data;
    126 }cam_compose_jpeg_info_t;
    127 
    128 /* Class@ QCameraMuxer
    129  *
    130  * Description@ Muxer interface
    131  *    a) Manages the grouping of the physical cameras into a logical camera
    132  *    b) Muxes the operational calls from Frameworks to HWI
    133  *    c) Composes MPO from JPEG
    134  */
    135 class QCameraMuxer {
    136 
    137 public:
    138     /* Public Methods   */
    139     QCameraMuxer(uint32_t num_of_cameras);
    140     virtual ~QCameraMuxer();
    141     static void getCameraMuxer(QCameraMuxer** pCamMuxer,
    142             uint32_t num_of_cameras);
    143     static int get_number_of_cameras();
    144     static int get_camera_info(int camera_id, struct camera_info *info);
    145     static int set_callbacks(const camera_module_callbacks_t *callbacks);
    146     static int open_legacy(const struct hw_module_t* module,
    147             const char* id, uint32_t halVersion, struct hw_device_t** device);
    148 
    149     static int camera_device_open(const struct hw_module_t* module,
    150             const char* id,
    151             struct hw_device_t** device);
    152     static int close_camera_device( hw_device_t *);
    153 
    154     /* Operation methods directly accessed by Camera Service */
    155     static camera_device_ops_t mCameraMuxerOps;
    156 
    157     /* Start of operational methods */
    158     static int set_preview_window(struct camera_device *,
    159             struct preview_stream_ops *window);
    160     static void set_callBacks(struct camera_device *,
    161             camera_notify_callback notify_cb,
    162             camera_data_callback data_cb,
    163             camera_data_timestamp_callback data_cb_timestamp,
    164             camera_request_memory get_memory,
    165             void *user);
    166     static void enable_msg_type(struct camera_device *, int32_t msg_type);
    167     static void disable_msg_type(struct camera_device *, int32_t msg_type);
    168     static int msg_type_enabled(struct camera_device *, int32_t msg_type);
    169     static int start_preview(struct camera_device *);
    170     static void stop_preview(struct camera_device *);
    171     static int preview_enabled(struct camera_device *);
    172     static int store_meta_data_in_buffers(struct camera_device *,
    173             int enable);
    174     static int start_recording(struct camera_device *);
    175     static void stop_recording(struct camera_device *);
    176     static int recording_enabled(struct camera_device *);
    177     static void release_recording_frame(struct camera_device *,
    178               const void *opaque);
    179     static int auto_focus(struct camera_device *);
    180     static int cancel_auto_focus(struct camera_device *);
    181     static int take_picture(struct camera_device *);
    182     static int cancel_picture(struct camera_device *);
    183     static int set_parameters(struct camera_device *, const char *parms);
    184     static char* get_parameters(struct camera_device *);
    185     static void put_parameters(struct camera_device *, char *);
    186     static int send_command(struct camera_device *,
    187           int32_t cmd, int32_t arg1, int32_t arg2);
    188     static void release(struct camera_device *);
    189     static int dump(struct camera_device *, int fd);
    190     /* End of operational methods */
    191 
    192     static void jpeg_data_callback(int32_t msg_type,
    193             const camera_memory_t *data, unsigned int index,
    194             camera_frame_metadata_t *metadata, void *user,
    195             uint32_t frame_idx, camera_release_callback release_cb,
    196             void *release_cookie, void *release_data);
    197     // add notify error msgs to the notifer queue of the primary related cam instance
    198     static int32_t sendEvtNotify(int32_t msg_type, int32_t ext1, int32_t ext2);
    199     // function to compose all JPEG images from all physical related camera instances
    200     void composeMpo(cam_compose_jpeg_info_t* main_Jpeg,
    201         cam_compose_jpeg_info_t* aux_Jpeg);
    202     static void* composeMpoRoutine(void* data);
    203     static bool matchFrameId(void *data, void *user_data, void *match_data);
    204     static bool findPreviousJpegs(void *data, void *user_data, void *match_data);
    205     static void releaseJpegInfo(void *data, void *user_data);
    206 
    207 public:
    208     /* Public Members  Variables   */
    209     // Jpeg and Mpo ops need to be shared between 2 HWI instances
    210     // hence these are cached in the muxer alongwith Jpeg handle
    211     mm_jpeg_ops_t mJpegOps;
    212     mm_jpeg_mpo_ops_t mJpegMpoOps;
    213     uint32_t mJpegClientHandle;
    214     // Stores Camera Data Callback function
    215     camera_data_callback mDataCb;
    216     // Stores Camera GetMemory Callback function
    217     camera_request_memory mGetMemoryCb;
    218 
    219 private:
    220     /* Private Member Variables  */
    221     qcamera_physical_descriptor_t *m_pPhyCamera;
    222     qcamera_logical_descriptor_t *m_pLogicalCamera;
    223     const camera_module_callbacks_t *m_pCallbacks;
    224     bool m_bAuxCameraExposed;
    225     uint8_t m_nPhyCameras;
    226     uint8_t m_nLogicalCameras;
    227 
    228     // Main Camera session Jpeg Queue
    229     QCameraQueue m_MainJpegQ;
    230     // Aux Camera session Jpeg Queue
    231     QCameraQueue m_AuxJpegQ;
    232     // thread for mpo composition
    233     QCameraCmdThread m_ComposeMpoTh;
    234     // Final Mpo Jpeg Buffer
    235     camera_memory_t *m_pRelCamMpoJpeg;
    236     // Lock needed to synchronize between multiple composition requests
    237     pthread_mutex_t m_JpegLock;
    238     // this callback cookie would be used for sending Final mpo Jpeg to the framework
    239     void *m_pMpoCallbackCookie;
    240     // this callback cookie would be used for caching main related cam phy instance
    241     // this is needed for error scenarios
    242     // incase of error, we use this cookie to get HWI instance and send errors in notify cb
    243     void *m_pJpegCallbackCookie;
    244     // flag to indicate whether we need to dump dual camera snapshots
    245     bool m_bDumpImages;
    246     // flag to indicate whether MPO is enabled or not
    247     bool m_bMpoEnabled;
    248     // Signifies if frame sync is enabled
    249     bool m_bFrameSyncEnabled;
    250     // flag to indicate whether recording hint is internally set.
    251     bool m_bRecordingHintInternallySet;
    252 
    253     /* Private Member Methods */
    254     int setupLogicalCameras();
    255     int cameraDeviceOpen(int camera_id, struct hw_device_t **hw_device);
    256     int getNumberOfCameras();
    257     int getCameraInfo(int camera_id, struct camera_info *info,
    258             cam_sync_type_t *p_cam_type);
    259     int32_t setCallbacks(const camera_module_callbacks_t *callbacks);
    260     int32_t setDataCallback(camera_data_callback data_cb);
    261     int32_t setMemoryCallback(camera_request_memory get_memory);
    262     qcamera_logical_descriptor_t* getLogicalCamera(
    263             struct camera_device * device);
    264     qcamera_physical_descriptor_t* getPhysicalCamera(
    265             qcamera_logical_descriptor_t* log_cam, uint32_t index);
    266     int32_t getActiveNumOfPhyCam(
    267             qcamera_logical_descriptor_t* log_cam, int& numOfAcitvePhyCam);
    268     int32_t setMpoCallbackCookie(void* mpoCbCookie);
    269     void* getMpoCallbackCookie();
    270     int32_t setMainJpegCallbackCookie(void* jpegCbCookie);
    271     void* getMainJpegCallbackCookie();
    272     void setJpegHandle(uint32_t handle) { mJpegClientHandle = handle;};
    273     // function to store single JPEG from 1 related physical camera instance
    274     int32_t storeJpeg(cam_sync_type_t cam_type, int32_t msg_type,
    275             const camera_memory_t *data, unsigned int index,
    276             camera_frame_metadata_t *metadata, void *user,
    277             uint32_t frame_idx, camera_release_callback release_cb,
    278             void *release_cookie, void *release_data);
    279 
    280 };// End namespace qcamera
    281 
    282 }
    283 #endif /* __QCAMERAMUXER_H__ */
    284 
    285