1 /** @file libuvc_internal.h
2 * @brief Implementation-specific UVC constants and structures.
5 #ifndef LIBUVC_INTERNAL_H
6 #define LIBUVC_INTERNAL_H
17 /** Converts an unaligned four-byte little-endian integer into an int32 */
18 #define DW_TO_INT(p) ((p)[0] | ((p)[1] << 8) | ((p)[2] << 16) | ((p)[3] << 24))
19 /** Converts an unaligned two-byte little-endian integer into an int16 */
20 #define SW_TO_SHORT(p) ((p)[0] | ((p)[1] << 8))
21 /** Converts an int16 into an unaligned two-byte little-endian integer */
22 #define SHORT_TO_SW(s, p) \
25 /** Converts an int32 into an unaligned four-byte little-endian integer */
26 #define INT_TO_DW(i, p) \
32 /** Selects the nth item in a doubly linked list. n=-1 selects the last item. */
33 #define DL_NTH(head, out, n) \
36 LDECLTYPE(head) dl_nth_p = (head); \
38 while (dl_nth_p && dl_nth_i > (n)) { \
39 dl_nth_p = dl_nth_p->prev; \
43 while (dl_nth_p && dl_nth_i < (n)) { \
44 dl_nth_p = dl_nth_p->next; \
53 #define UVC_DEBUG(format, ...) fprintf(stderr, "[%s:%d/%s] " format "\n", basename(__FILE__), __LINE__, __FUNCTION__, ##__VA_ARGS__)
54 #define UVC_ENTER() fprintf(stderr, "[%s:%d] begin %s\n", basename(__FILE__), __LINE__, __FUNCTION__)
55 #define UVC_EXIT(code) fprintf(stderr, "[%s:%d] end %s (%d)\n", basename(__FILE__), __LINE__, __FUNCTION__, code)
56 #define UVC_EXIT_VOID() fprintf(stderr, "[%s:%d] end %s\n", basename(__FILE__), __LINE__, __FUNCTION__)
58 #define UVC_DEBUG(format, ...)
60 #define UVC_EXIT_VOID()
61 #define UVC_EXIT(code)
64 /* http://stackoverflow.com/questions/19452971/array-size-macro-that-rejects-pointers */
65 #define IS_INDEXABLE(arg) (sizeof(arg[0]))
66 #define IS_ARRAY(arg) (IS_INDEXABLE(arg) && (((void *) &arg) == ((void *) arg)))
67 #define ARRAYSIZE(arr) (sizeof(arr) / (IS_ARRAY(arr) ? sizeof(arr[0]) : 0))
69 /** Video interface subclass code (A.2) */
70 enum uvc_int_subclass_code {
71 UVC_SC_UNDEFINED = 0x00,
72 UVC_SC_VIDEOCONTROL = 0x01,
73 UVC_SC_VIDEOSTREAMING = 0x02,
74 UVC_SC_VIDEO_INTERFACE_COLLECTION = 0x03
77 /** Video interface protocol code (A.3) */
78 enum uvc_int_proto_code {
79 UVC_PC_PROTOCOL_UNDEFINED = 0x00
82 /** VideoControl interface descriptor subtype (A.5) */
83 enum uvc_vc_desc_subtype {
84 UVC_VC_DESCRIPTOR_UNDEFINED = 0x00,
86 UVC_VC_INPUT_TERMINAL = 0x02,
87 UVC_VC_OUTPUT_TERMINAL = 0x03,
88 UVC_VC_SELECTOR_UNIT = 0x04,
89 UVC_VC_PROCESSING_UNIT = 0x05,
90 UVC_VC_EXTENSION_UNIT = 0x06
93 /** UVC endpoint descriptor subtype (A.7) */
94 enum uvc_ep_desc_subtype {
95 UVC_EP_UNDEFINED = 0x00,
96 UVC_EP_GENERAL = 0x01,
97 UVC_EP_ENDPOINT = 0x02,
98 UVC_EP_INTERRUPT = 0x03
101 /** VideoControl interface control selector (A.9.1) */
102 enum uvc_vc_ctrl_selector {
103 UVC_VC_CONTROL_UNDEFINED = 0x00,
104 UVC_VC_VIDEO_POWER_MODE_CONTROL = 0x01,
105 UVC_VC_REQUEST_ERROR_CODE_CONTROL = 0x02
108 /** Terminal control selector (A.9.2) */
109 enum uvc_term_ctrl_selector {
110 UVC_TE_CONTROL_UNDEFINED = 0x00
113 /** Selector unit control selector (A.9.3) */
114 enum uvc_su_ctrl_selector {
115 UVC_SU_CONTROL_UNDEFINED = 0x00,
116 UVC_SU_INPUT_SELECT_CONTROL = 0x01
119 /** Extension unit control selector (A.9.6) */
120 enum uvc_xu_ctrl_selector {
121 UVC_XU_CONTROL_UNDEFINED = 0x00
124 /** VideoStreaming interface control selector (A.9.7) */
125 enum uvc_vs_ctrl_selector {
126 UVC_VS_CONTROL_UNDEFINED = 0x00,
127 UVC_VS_PROBE_CONTROL = 0x01,
128 UVC_VS_COMMIT_CONTROL = 0x02,
129 UVC_VS_STILL_PROBE_CONTROL = 0x03,
130 UVC_VS_STILL_COMMIT_CONTROL = 0x04,
131 UVC_VS_STILL_IMAGE_TRIGGER_CONTROL = 0x05,
132 UVC_VS_STREAM_ERROR_CODE_CONTROL = 0x06,
133 UVC_VS_GENERATE_KEY_FRAME_CONTROL = 0x07,
134 UVC_VS_UPDATE_FRAME_SEGMENT_CONTROL = 0x08,
135 UVC_VS_SYNC_DELAY_CONTROL = 0x09
138 /** Status packet type (2.4.2.2) */
139 enum uvc_status_type {
140 UVC_STATUS_TYPE_CONTROL = 1,
141 UVC_STATUS_TYPE_STREAMING = 2
144 /** Payload header flags (2.4.3.3) */
145 #define UVC_STREAM_EOH (1 << 7)
146 #define UVC_STREAM_ERR (1 << 6)
147 #define UVC_STREAM_STI (1 << 5)
148 #define UVC_STREAM_RES (1 << 4)
149 #define UVC_STREAM_SCR (1 << 3)
150 #define UVC_STREAM_PTS (1 << 2)
151 #define UVC_STREAM_EOF (1 << 1)
152 #define UVC_STREAM_FID (1 << 0)
154 /** Control capabilities (4.1.2) */
155 #define UVC_CONTROL_CAP_GET (1 << 0)
156 #define UVC_CONTROL_CAP_SET (1 << 1)
157 #define UVC_CONTROL_CAP_DISABLED (1 << 2)
158 #define UVC_CONTROL_CAP_AUTOUPDATE (1 << 3)
159 #define UVC_CONTROL_CAP_ASYNCHRONOUS (1 << 4)
161 struct uvc_streaming_interface;
162 struct uvc_device_info;
164 /** VideoStream interface */
165 typedef struct uvc_streaming_interface {
166 struct uvc_device_info *parent;
167 struct uvc_streaming_interface *prev, *next;
168 /** Interface number */
169 uint8_t bInterfaceNumber;
170 /** Video formats that this interface provides */
171 struct uvc_format_desc *format_descs;
172 /** USB endpoint to use when communicating with this interface */
173 uint8_t bEndpointAddress;
174 uint8_t bTerminalLink;
175 } uvc_streaming_interface_t;
177 /** VideoControl interface */
178 typedef struct uvc_control_interface {
179 struct uvc_device_info *parent;
180 struct uvc_input_terminal *input_term_descs;
181 // struct uvc_output_terminal *output_term_descs;
182 struct uvc_selector_unit *selector_unit_descs;
183 struct uvc_processing_unit *processing_unit_descs;
184 struct uvc_extension_unit *extension_unit_descs;
186 uint32_t dwClockFrequency;
187 uint8_t bEndpointAddress;
188 /** Interface number */
189 uint8_t bInterfaceNumber;
190 } uvc_control_interface_t;
192 struct uvc_stream_ctrl;
195 struct uvc_context *ctx;
197 libusb_device *usb_dev;
200 typedef struct uvc_device_info {
201 /** Configuration descriptor for USB device */
202 struct libusb_config_descriptor *config;
203 /** VideoControl interface provided by device */
204 uvc_control_interface_t ctrl_if;
205 /** VideoStreaming interfaces on the device */
206 uvc_streaming_interface_t *stream_ifs;
210 set a high number of transfer buffers. This uses a lot of ram, but
211 avoids problems with scheduling delays on slow boards causing missed
212 transfers. A better approach may be to make the transfer thread FIFO
213 scheduled (if we have root).
214 We could/should change this to allow reduce it to, say, 5 by default
215 and then allow the user to change the number of buffers as required.
217 #define LIBUVC_NUM_TRANSFER_BUFS 100
219 #define LIBUVC_XFER_BUF_SIZE ( 16 * 1024 * 1024 )
221 struct uvc_stream_handle {
222 struct uvc_device_handle *devh;
223 struct uvc_stream_handle *prev, *next;
224 struct uvc_streaming_interface *stream_if;
226 /** if true, stream is running (streaming video to host) */
228 /** Current control block */
229 struct uvc_stream_ctrl cur_ctrl;
231 /* listeners may only access hold*, and only when holding a
232 * lock on cb_mutex (probably signaled with cb_cond) */
234 uint32_t seq, hold_seq;
235 uint32_t pts, hold_pts;
236 uint32_t last_scr, hold_last_scr;
237 size_t got_bytes, hold_bytes;
238 uint8_t *outbuf, *holdbuf;
239 pthread_mutex_t cb_mutex;
240 pthread_cond_t cb_cond;
242 uint32_t last_polled_seq;
243 uvc_frame_callback_t *user_cb;
245 struct libusb_transfer *transfers[LIBUVC_NUM_TRANSFER_BUFS];
246 uint8_t *transfer_bufs[LIBUVC_NUM_TRANSFER_BUFS];
247 struct uvc_frame frame;
248 enum uvc_frame_format frame_format;
251 /** Handle on an open UVC device
253 * @todo move most of this into a uvc_device struct?
255 struct uvc_device_handle {
256 struct uvc_device *dev;
257 struct uvc_device_handle *prev, *next;
258 /** Underlying USB device handle */
259 libusb_device_handle *usb_devh;
260 struct uvc_device_info *info;
261 struct libusb_transfer *status_xfer;
262 uint8_t status_buf[32];
263 /** Function to call when we receive status updates from the camera */
264 uvc_status_callback_t *status_cb;
265 void *status_user_ptr;
266 /** Function to call when we receive button events from the camera */
267 uvc_button_callback_t *button_cb;
268 void *button_user_ptr;
270 uvc_stream_handle_t *streams;
271 /** Whether the camera is an iSight that sends one header per frame */
276 /** Context within which we communicate with devices */
278 /** Underlying context for USB communication */
279 struct libusb_context *usb_ctx;
280 /** True iff libuvc initialized the underlying USB context */
282 /** List of open devices in this context */
283 uvc_device_handle_t *open_devices;
284 pthread_t handler_thread;
285 int kill_handler_thread;
288 uvc_error_t uvc_query_stream_ctrl(
289 uvc_device_handle_t *devh,
290 uvc_stream_ctrl_t *ctrl,
292 enum uvc_req_code req);
294 void uvc_start_handler_thread(uvc_context_t *ctx);
295 uvc_error_t uvc_claim_if(uvc_device_handle_t *devh, int idx);
296 uvc_error_t uvc_release_if(uvc_device_handle_t *devh, int idx);
298 #endif // !def(LIBUVC_INTERNAL_H)