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