d30962866b6f1018247868e0a339acf5b80ac2dd
[rtmpclient.git] / app / src / main / jni / libuvc-0.0.6 / src / stream.c
1 /*********************************************************************
2 * Software License Agreement (BSD License)
3 *
4 *  Copyright (C) 2010-2012 Ken Tossell
5 *  All rights reserved.
6 *
7 *  Redistribution and use in source and binary forms, with or without
8 *  modification, are permitted provided that the following conditions
9 *  are met:
10 *
11 *   * Redistributions of source code must retain the above copyright
12 *     notice, this list of conditions and the following disclaimer.
13 *   * Redistributions in binary form must reproduce the above
14 *     copyright notice, this list of conditions and the following
15 *     disclaimer in the documentation and/or other materials provided
16 *     with the distribution.
17 *   * Neither the name of the author nor other contributors may be
18 *     used to endorse or promote products derived from this software
19 *     without specific prior written permission.
20 *
21 *  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22 *  "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23 *  LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
24 *  FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
25 *  COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
26 *  INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
27 *  BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
28 *  LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
29 *  CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30 *  LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
31 *  ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
32 *  POSSIBILITY OF SUCH DAMAGE.
33 *********************************************************************/
34 /**
35  * @defgroup streaming Streaming control functions
36  * @brief Tools for creating, managing and consuming video streams
37  */
38
39 #include "libuvc/libuvc.h"
40 #include "libuvc/libuvc_internal.h"
41 #include "errno.h"
42
43 #ifdef _MSC_VER
44
45 #define DELTA_EPOCH_IN_MICROSECS  116444736000000000Ui64
46
47 // gettimeofday - get time of day for Windows;
48 // A gettimeofday implementation for Microsoft Windows;
49 // Public domain code, author "ponnada";
50 int gettimeofday(struct timeval *tv, struct timezone *tz)
51 {
52     FILETIME ft;
53     unsigned __int64 tmpres = 0;
54     static int tzflag = 0;
55     if (NULL != tv)
56     {
57         GetSystemTimeAsFileTime(&ft);
58         tmpres |= ft.dwHighDateTime;
59         tmpres <<= 32;
60         tmpres |= ft.dwLowDateTime;
61         tmpres /= 10;
62         tmpres -= DELTA_EPOCH_IN_MICROSECS;
63         tv->tv_sec = (long)(tmpres / 1000000UL);
64         tv->tv_usec = (long)(tmpres % 1000000UL);
65     }
66     return 0;
67 }
68 #endif // _MSC_VER
69 uvc_frame_desc_t *uvc_find_frame_desc_stream(uvc_stream_handle_t *strmh,
70     uint16_t format_id, uint16_t frame_id);
71 uvc_frame_desc_t *uvc_find_frame_desc(uvc_device_handle_t *devh,
72     uint16_t format_id, uint16_t frame_id);
73 void *_uvc_user_caller(void *arg);
74 void _uvc_populate_frame(uvc_stream_handle_t *strmh);
75
76 struct format_table_entry {
77   enum uvc_frame_format format;
78   uint8_t abstract_fmt;
79   uint8_t guid[16];
80   int children_count;
81   enum uvc_frame_format *children;
82 };
83
84 struct format_table_entry *_get_format_entry(enum uvc_frame_format format) {
85   #define ABS_FMT(_fmt, _num, ...) \
86     case _fmt: { \
87     static enum uvc_frame_format _fmt##_children[] = __VA_ARGS__; \
88     static struct format_table_entry _fmt##_entry = { \
89       _fmt, 0, {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}, _num, _fmt##_children }; \
90     return &_fmt##_entry; }
91
92   #define FMT(_fmt, ...) \
93     case _fmt: { \
94     static struct format_table_entry _fmt##_entry = { \
95       _fmt, 0, __VA_ARGS__, 0, NULL }; \
96     return &_fmt##_entry; }
97
98   switch(format) {
99     /* Define new formats here */
100     ABS_FMT(UVC_FRAME_FORMAT_ANY, 2,
101       {UVC_FRAME_FORMAT_UNCOMPRESSED, UVC_FRAME_FORMAT_COMPRESSED})
102
103     ABS_FMT(UVC_FRAME_FORMAT_UNCOMPRESSED, 4,
104       {UVC_FRAME_FORMAT_YUYV, UVC_FRAME_FORMAT_UYVY, UVC_FRAME_FORMAT_GRAY8,
105       UVC_FRAME_FORMAT_GRAY16})
106     FMT(UVC_FRAME_FORMAT_YUYV,
107       {'Y',  'U',  'Y',  '2', 0x00, 0x00, 0x10, 0x00, 0x80, 0x00, 0x00, 0xaa, 0x00, 0x38, 0x9b, 0x71})
108     FMT(UVC_FRAME_FORMAT_UYVY,
109       {'U',  'Y',  'V',  'Y', 0x00, 0x00, 0x10, 0x00, 0x80, 0x00, 0x00, 0xaa, 0x00, 0x38, 0x9b, 0x71})
110     FMT(UVC_FRAME_FORMAT_GRAY8,
111       {'Y',  '8',  '0',  '0', 0x00, 0x00, 0x10, 0x00, 0x80, 0x00, 0x00, 0xaa, 0x00, 0x38, 0x9b, 0x71})
112     FMT(UVC_FRAME_FORMAT_GRAY16,
113       {'Y',  '1',  '6',  ' ', 0x00, 0x00, 0x10, 0x00, 0x80, 0x00, 0x00, 0xaa, 0x00, 0x38, 0x9b, 0x71})
114     FMT(UVC_FRAME_FORMAT_BY8,
115       {'B',  'Y',  '8',  ' ', 0x00, 0x00, 0x10, 0x00, 0x80, 0x00, 0x00, 0xaa, 0x00, 0x38, 0x9b, 0x71})
116     FMT(UVC_FRAME_FORMAT_BA81,
117       {'B',  'A',  '8',  '1', 0x00, 0x00, 0x10, 0x00, 0x80, 0x00, 0x00, 0xaa, 0x00, 0x38, 0x9b, 0x71})
118     FMT(UVC_FRAME_FORMAT_SGRBG8,
119       {'G',  'R',  'B',  'G', 0x00, 0x00, 0x10, 0x00, 0x80, 0x00, 0x00, 0xaa, 0x00, 0x38, 0x9b, 0x71})
120     FMT(UVC_FRAME_FORMAT_SGBRG8,
121       {'G',  'B',  'R',  'G', 0x00, 0x00, 0x10, 0x00, 0x80, 0x00, 0x00, 0xaa, 0x00, 0x38, 0x9b, 0x71})
122     FMT(UVC_FRAME_FORMAT_SRGGB8,
123       {'R',  'G',  'G',  'B', 0x00, 0x00, 0x10, 0x00, 0x80, 0x00, 0x00, 0xaa, 0x00, 0x38, 0x9b, 0x71})
124     FMT(UVC_FRAME_FORMAT_SBGGR8,
125       {'B',  'G',  'G',  'R', 0x00, 0x00, 0x10, 0x00, 0x80, 0x00, 0x00, 0xaa, 0x00, 0x38, 0x9b, 0x71})
126     ABS_FMT(UVC_FRAME_FORMAT_COMPRESSED, 1,
127       {UVC_FRAME_FORMAT_MJPEG})
128     FMT(UVC_FRAME_FORMAT_MJPEG,
129       {'M',  'J',  'P',  'G'})
130
131     default:
132       return NULL;
133   }
134
135   #undef ABS_FMT
136   #undef FMT
137 }
138
139 static uint8_t _uvc_frame_format_matches_guid(enum uvc_frame_format fmt, uint8_t guid[16]) {
140   struct format_table_entry *format;
141   int child_idx;
142
143   format = _get_format_entry(fmt);
144   if (!format)
145     return 0;
146
147   if (!format->abstract_fmt && !memcmp(guid, format->guid, 16))
148     return 1;
149
150   for (child_idx = 0; child_idx < format->children_count; child_idx++) {
151     if (_uvc_frame_format_matches_guid(format->children[child_idx], guid))
152       return 1;
153   }
154
155   return 0;
156 }
157
158 static enum uvc_frame_format uvc_frame_format_for_guid(uint8_t guid[16]) {
159   struct format_table_entry *format;
160   enum uvc_frame_format fmt;
161
162   for (fmt = 0; fmt < UVC_FRAME_FORMAT_COUNT; ++fmt) {
163     format = _get_format_entry(fmt);
164     if (!format || format->abstract_fmt)
165       continue;
166     if (!memcmp(format->guid, guid, 16))
167       return format->format;
168   }
169
170   return UVC_FRAME_FORMAT_UNKNOWN;
171 }
172
173 /** @internal
174  * Run a streaming control query
175  * @param[in] devh UVC device
176  * @param[in,out] ctrl Control block
177  * @param[in] probe Whether this is a probe query or a commit query
178  * @param[in] req Query type
179  */
180 uvc_error_t uvc_query_stream_ctrl(
181     uvc_device_handle_t *devh,
182     uvc_stream_ctrl_t *ctrl,
183     uint8_t probe,
184     enum uvc_req_code req) {
185   uint8_t buf[34];
186   size_t len;
187   uvc_error_t err;
188
189   memset(buf, 0, sizeof(buf));
190
191   if (devh->info->ctrl_if.bcdUVC >= 0x0110)
192     len = 34;
193   else
194     len = 26;
195
196   /* prepare for a SET transfer */
197   if (req == UVC_SET_CUR) {
198     SHORT_TO_SW(ctrl->bmHint, buf);
199     buf[2] = ctrl->bFormatIndex;
200     buf[3] = ctrl->bFrameIndex;
201     INT_TO_DW(ctrl->dwFrameInterval, buf + 4);
202     SHORT_TO_SW(ctrl->wKeyFrameRate, buf + 8);
203     SHORT_TO_SW(ctrl->wPFrameRate, buf + 10);
204     SHORT_TO_SW(ctrl->wCompQuality, buf + 12);
205     SHORT_TO_SW(ctrl->wCompWindowSize, buf + 14);
206     SHORT_TO_SW(ctrl->wDelay, buf + 16);
207     INT_TO_DW(ctrl->dwMaxVideoFrameSize, buf + 18);
208     INT_TO_DW(ctrl->dwMaxPayloadTransferSize, buf + 22);
209
210     if (len == 34) {
211       INT_TO_DW ( ctrl->dwClockFrequency, buf + 26 );
212       buf[30] = ctrl->bmFramingInfo;
213       buf[31] = ctrl->bPreferredVersion;
214       buf[32] = ctrl->bMinVersion;
215       buf[33] = ctrl->bMaxVersion;
216       /** @todo support UVC 1.1 */
217     }
218   }
219
220   /* do the transfer */
221   err = libusb_control_transfer(
222       devh->usb_devh,
223       req == UVC_SET_CUR ? 0x21 : 0xA1,
224       req,
225       probe ? (UVC_VS_PROBE_CONTROL << 8) : (UVC_VS_COMMIT_CONTROL << 8),
226       ctrl->bInterfaceNumber,
227       buf, len, 0
228   );
229
230   if (err <= 0) {
231     return err;
232   }
233
234   /* now decode following a GET transfer */
235   if (req != UVC_SET_CUR) {
236     ctrl->bmHint = SW_TO_SHORT(buf);
237     ctrl->bFormatIndex = buf[2];
238     ctrl->bFrameIndex = buf[3];
239     ctrl->dwFrameInterval = DW_TO_INT(buf + 4);
240     ctrl->wKeyFrameRate = SW_TO_SHORT(buf + 8);
241     ctrl->wPFrameRate = SW_TO_SHORT(buf + 10);
242     ctrl->wCompQuality = SW_TO_SHORT(buf + 12);
243     ctrl->wCompWindowSize = SW_TO_SHORT(buf + 14);
244     ctrl->wDelay = SW_TO_SHORT(buf + 16);
245     ctrl->dwMaxVideoFrameSize = DW_TO_INT(buf + 18);
246     ctrl->dwMaxPayloadTransferSize = DW_TO_INT(buf + 22);
247
248     if (len == 34) {
249       ctrl->dwClockFrequency = DW_TO_INT ( buf + 26 );
250       ctrl->bmFramingInfo = buf[30];
251       ctrl->bPreferredVersion = buf[31];
252       ctrl->bMinVersion = buf[32];
253       ctrl->bMaxVersion = buf[33];
254       /** @todo support UVC 1.1 */
255     }
256     else
257       ctrl->dwClockFrequency = devh->info->ctrl_if.dwClockFrequency;
258
259     /* fix up block for cameras that fail to set dwMax* */
260     if (ctrl->dwMaxVideoFrameSize == 0) {
261       uvc_frame_desc_t *frame = uvc_find_frame_desc(devh, ctrl->bFormatIndex, ctrl->bFrameIndex);
262
263       if (frame) {
264         ctrl->dwMaxVideoFrameSize = frame->dwMaxVideoFrameBufferSize;
265       }
266     }
267   }
268
269   return UVC_SUCCESS;
270 }
271
272 /** @brief Reconfigure stream with a new stream format.
273  * @ingroup streaming
274  *
275  * This may be executed whether or not the stream is running.
276  *
277  * @param[in] strmh Stream handle
278  * @param[in] ctrl Control block, processed using {uvc_probe_stream_ctrl} or
279  *             {uvc_get_stream_ctrl_format_size}
280  */
281 uvc_error_t uvc_stream_ctrl(uvc_stream_handle_t *strmh, uvc_stream_ctrl_t *ctrl) {
282   uvc_error_t ret;
283
284   if (strmh->stream_if->bInterfaceNumber != ctrl->bInterfaceNumber)
285     return UVC_ERROR_INVALID_PARAM;
286
287   /* @todo Allow the stream to be modified without restarting the stream */
288   if (strmh->running)
289     return UVC_ERROR_BUSY;
290
291   ret = uvc_query_stream_ctrl(strmh->devh, ctrl, 0, UVC_SET_CUR);
292   if (ret != UVC_SUCCESS)
293     return ret;
294
295   strmh->cur_ctrl = *ctrl;
296   return UVC_SUCCESS;
297 }
298
299 /** @internal
300  * @brief Find the descriptor for a specific frame configuration
301  * @param stream_if Stream interface
302  * @param format_id Index of format class descriptor
303  * @param frame_id Index of frame descriptor
304  */
305 static uvc_frame_desc_t *_uvc_find_frame_desc_stream_if(uvc_streaming_interface_t *stream_if,
306     uint16_t format_id, uint16_t frame_id) {
307  
308   uvc_format_desc_t *format = NULL;
309   uvc_frame_desc_t *frame = NULL;
310
311   DL_FOREACH(stream_if->format_descs, format) {
312     if (format->bFormatIndex == format_id) {
313       DL_FOREACH(format->frame_descs, frame) {
314         if (frame->bFrameIndex == frame_id)
315           return frame;
316       }
317     }
318   }
319
320   return NULL;
321 }
322
323 uvc_frame_desc_t *uvc_find_frame_desc_stream(uvc_stream_handle_t *strmh,
324     uint16_t format_id, uint16_t frame_id) {
325   return _uvc_find_frame_desc_stream_if(strmh->stream_if, format_id, frame_id);
326 }
327
328 /** @internal
329  * @brief Find the descriptor for a specific frame configuration
330  * @param devh UVC device
331  * @param format_id Index of format class descriptor
332  * @param frame_id Index of frame descriptor
333  */
334 uvc_frame_desc_t *uvc_find_frame_desc(uvc_device_handle_t *devh,
335     uint16_t format_id, uint16_t frame_id) {
336  
337   uvc_streaming_interface_t *stream_if;
338   uvc_frame_desc_t *frame;
339
340   DL_FOREACH(devh->info->stream_ifs, stream_if) {
341     frame = _uvc_find_frame_desc_stream_if(stream_if, format_id, frame_id);
342     if (frame)
343       return frame;
344   }
345
346   return NULL;
347 }
348
349 /** Get a negotiated streaming control block for some common parameters.
350  * @ingroup streaming
351  *
352  * @param[in] devh Device handle
353  * @param[in,out] ctrl Control block
354  * @param[in] format_class Type of streaming format
355  * @param[in] width Desired frame width
356  * @param[in] height Desired frame height
357  * @param[in] fps Frame rate, frames per second
358  */
359 uvc_error_t uvc_get_stream_ctrl_format_size(
360     uvc_device_handle_t *devh,
361     uvc_stream_ctrl_t *ctrl,
362     enum uvc_frame_format cf,
363     int width, int height,
364     int fps) {
365   uvc_streaming_interface_t *stream_if;
366
367   /* find a matching frame descriptor and interval */
368   DL_FOREACH(devh->info->stream_ifs, stream_if) {
369     uvc_format_desc_t *format;
370
371     DL_FOREACH(stream_if->format_descs, format) {
372       uvc_frame_desc_t *frame;
373
374       if (!_uvc_frame_format_matches_guid(cf, format->guidFormat))
375         continue;
376
377       DL_FOREACH(format->frame_descs, frame) {
378         if (frame->wWidth != width || frame->wHeight != height)
379           continue;
380
381         uint32_t *interval;
382
383         ctrl->bInterfaceNumber = stream_if->bInterfaceNumber;
384         UVC_DEBUG("claiming streaming interface %d", stream_if->bInterfaceNumber );
385         uvc_claim_if(devh, ctrl->bInterfaceNumber);
386         /* get the max values */
387         uvc_query_stream_ctrl( devh, ctrl, 1, UVC_GET_MAX);
388
389         if (frame->intervals) {
390           for (interval = frame->intervals; *interval; ++interval) {
391             // allow a fps rate of zero to mean "accept first rate available"
392             if (10000000 / *interval == (unsigned int) fps || fps == 0) {
393
394               ctrl->bmHint = (1 << 0); /* don't negotiate interval */
395               ctrl->bFormatIndex = format->bFormatIndex;
396               ctrl->bFrameIndex = frame->bFrameIndex;
397               ctrl->dwFrameInterval = *interval;
398
399               goto found;
400             }
401           }
402         } else {
403           uint32_t interval_100ns = 10000000 / fps;
404           uint32_t interval_offset = interval_100ns - frame->dwMinFrameInterval;
405
406           if (interval_100ns >= frame->dwMinFrameInterval
407               && interval_100ns <= frame->dwMaxFrameInterval
408               && !(interval_offset
409                    && (interval_offset % frame->dwFrameIntervalStep))) {
410
411             ctrl->bmHint = (1 << 0);
412             ctrl->bFormatIndex = format->bFormatIndex;
413             ctrl->bFrameIndex = frame->bFrameIndex;
414             ctrl->dwFrameInterval = interval_100ns;
415
416             goto found;
417           }
418         }
419       }
420     }
421   }
422
423   return UVC_ERROR_INVALID_MODE;
424
425 found:
426   return uvc_probe_stream_ctrl(devh, ctrl);
427 }
428
429 /** @internal
430  * Negotiate streaming parameters with the device
431  *
432  * @param[in] devh UVC device
433  * @param[in,out] ctrl Control block
434  */
435 uvc_error_t uvc_probe_stream_ctrl(
436     uvc_device_handle_t *devh,
437     uvc_stream_ctrl_t *ctrl) {
438  
439   uvc_query_stream_ctrl(
440       devh, ctrl, 1, UVC_SET_CUR
441   );
442
443   uvc_query_stream_ctrl(
444       devh, ctrl, 1, UVC_GET_CUR
445   );
446
447   /** @todo make sure that worked */
448   return UVC_SUCCESS;
449 }
450
451 /** @internal
452  * @brief Swap the working buffer with the presented buffer and notify consumers
453  */
454 void _uvc_swap_buffers(uvc_stream_handle_t *strmh) {
455   uint8_t *tmp_buf;
456
457   pthread_mutex_lock(&strmh->cb_mutex);
458
459   /* swap the buffers */
460   tmp_buf = strmh->holdbuf;
461   strmh->hold_bytes = strmh->got_bytes;
462   strmh->holdbuf = strmh->outbuf;
463   strmh->outbuf = tmp_buf;
464   strmh->hold_last_scr = strmh->last_scr;
465   strmh->hold_pts = strmh->pts;
466   strmh->hold_seq = strmh->seq;
467
468   pthread_cond_broadcast(&strmh->cb_cond);
469   pthread_mutex_unlock(&strmh->cb_mutex);
470
471   strmh->seq++;
472   strmh->got_bytes = 0;
473   strmh->last_scr = 0;
474   strmh->pts = 0;
475 }
476
477 /** @internal
478  * @brief Process a payload transfer
479  * 
480  * Processes stream, places frames into buffer, signals listeners
481  * (such as user callback thread and any polling thread) on new frame
482  *
483  * @param payload Contents of the payload transfer, either a packet (isochronous) or a full
484  * transfer (bulk mode)
485  * @param payload_len Length of the payload transfer
486  */
487 void _uvc_process_payload(uvc_stream_handle_t *strmh, uint8_t *payload, size_t payload_len) {
488   size_t header_len;
489   uint8_t header_info;
490   size_t data_len;
491
492   /* magic numbers for identifying header packets from some iSight cameras */
493   static uint8_t isight_tag[] = {
494     0x11, 0x22, 0x33, 0x44,
495     0xde, 0xad, 0xbe, 0xef, 0xde, 0xad, 0xfa, 0xce
496   };
497
498   /* ignore empty payload transfers */
499   if (payload_len == 0)
500     return;
501
502   /* Certain iSight cameras have strange behavior: They send header
503    * information in a packet with no image data, and then the following
504    * packets have only image data, with no more headers until the next frame.
505    *
506    * The iSight header: len(1), flags(1 or 2), 0x11223344(4),
507    * 0xdeadbeefdeadface(8), ??(16)
508    */
509
510   if (strmh->devh->is_isight &&
511       (payload_len < 14 || memcmp(isight_tag, payload + 2, sizeof(isight_tag))) &&
512       (payload_len < 15 || memcmp(isight_tag, payload + 3, sizeof(isight_tag)))) {
513     /* The payload transfer doesn't have any iSight magic, so it's all image data */
514     header_len = 0;
515     data_len = payload_len;
516   } else {
517     header_len = payload[0];
518
519     if (header_len > payload_len) {
520       UVC_DEBUG("bogus packet: actual_len=%zd, header_len=%zd\n", payload_len, header_len);
521       return;
522     }
523
524     if (strmh->devh->is_isight)
525       data_len = 0;
526     else
527       data_len = payload_len - header_len;
528   }
529
530   if (header_len < 2) {
531     header_info = 0;
532   } else {
533     /** @todo we should be checking the end-of-header bit */
534     size_t variable_offset = 2;
535
536     header_info = payload[1];
537
538     if (header_info & 0x40) {
539       UVC_DEBUG("bad packet: error bit set");
540       return;
541     }
542
543     if (strmh->fid != (header_info & 1) && strmh->got_bytes != 0) {
544       /* The frame ID bit was flipped, but we have image data sitting
545          around from prior transfers. This means the camera didn't send
546          an EOF for the last transfer of the previous frame. */
547       _uvc_swap_buffers(strmh);
548     }
549
550     strmh->fid = header_info & 1;
551
552     if (header_info & (1 << 2)) {
553       strmh->pts = DW_TO_INT(payload + variable_offset);
554       variable_offset += 4;
555     }
556
557     if (header_info & (1 << 3)) {
558       /** @todo read the SOF token counter */
559       strmh->last_scr = DW_TO_INT(payload + variable_offset);
560       variable_offset += 6;
561     }
562   }
563
564   if (data_len > 0) {
565     memcpy(strmh->outbuf + strmh->got_bytes, payload + header_len, data_len);
566     strmh->got_bytes += data_len;
567
568     if (header_info & (1 << 1)) {
569       /* The EOF bit is set, so publish the complete frame */
570       _uvc_swap_buffers(strmh);
571     }
572   }
573 }
574
575 /** @internal
576  * @brief Stream transfer callback
577  *
578  * Processes stream, places frames into buffer, signals listeners
579  * (such as user callback thread and any polling thread) on new frame
580  *
581  * @param transfer Active transfer
582  */
583 void LIBUSB_CALL _uvc_stream_callback(struct libusb_transfer *transfer) {
584   uvc_stream_handle_t *strmh = transfer->user_data;
585
586   int resubmit = 1;
587
588   switch (transfer->status) {
589   case LIBUSB_TRANSFER_COMPLETED:
590     if (transfer->num_iso_packets == 0) {
591       /* This is a bulk mode transfer, so it just has one payload transfer */
592       _uvc_process_payload(strmh, transfer->buffer, transfer->actual_length);
593     } else {
594       /* This is an isochronous mode transfer, so each packet has a payload transfer */
595       int packet_id;
596
597       for (packet_id = 0; packet_id < transfer->num_iso_packets; ++packet_id) {
598         uint8_t *pktbuf;
599         struct libusb_iso_packet_descriptor *pkt;
600
601         pkt = transfer->iso_packet_desc + packet_id;
602
603         if (pkt->status != 0) {
604           UVC_DEBUG("bad packet (isochronous transfer); status: %d", pkt->status);
605           continue;
606         }
607
608         pktbuf = libusb_get_iso_packet_buffer_simple(transfer, packet_id);
609
610         _uvc_process_payload(strmh, pktbuf, pkt->actual_length);
611
612       }
613     }
614     break;
615   case LIBUSB_TRANSFER_CANCELLED: 
616   case LIBUSB_TRANSFER_ERROR:
617   case LIBUSB_TRANSFER_NO_DEVICE: {
618     int i;
619     UVC_DEBUG("not retrying transfer, status = %d", transfer->status);
620     pthread_mutex_lock(&strmh->cb_mutex);
621
622     /* Mark transfer as deleted. */
623     for(i=0; i < LIBUVC_NUM_TRANSFER_BUFS; i++) {
624       if(strmh->transfers[i] == transfer) {
625         UVC_DEBUG("Freeing transfer %d (%p)", i, transfer);
626         free(transfer->buffer);
627         libusb_free_transfer(transfer);
628         strmh->transfers[i] = NULL;
629         break;
630       }
631     }
632     if(i == LIBUVC_NUM_TRANSFER_BUFS ) {
633       UVC_DEBUG("transfer %p not found; not freeing!", transfer);
634     }
635
636     resubmit = 0;
637
638     pthread_cond_broadcast(&strmh->cb_cond);
639     pthread_mutex_unlock(&strmh->cb_mutex);
640
641     break;
642   }
643   case LIBUSB_TRANSFER_TIMED_OUT:
644   case LIBUSB_TRANSFER_STALL:
645   case LIBUSB_TRANSFER_OVERFLOW:
646     UVC_DEBUG("retrying transfer, status = %d", transfer->status);
647     break;
648   }
649   
650   if ( resubmit ) {
651     if ( strmh->running ) {
652       libusb_submit_transfer(transfer);
653     } else {
654       int i;
655       pthread_mutex_lock(&strmh->cb_mutex);
656
657       /* Mark transfer as deleted. */
658       for(i=0; i < LIBUVC_NUM_TRANSFER_BUFS; i++) {
659         if(strmh->transfers[i] == transfer) {
660           UVC_DEBUG("Freeing orphan transfer %d (%p)", i, transfer);
661           free(transfer->buffer);
662           libusb_free_transfer(transfer);
663           strmh->transfers[i] = NULL;
664         }
665       }
666       if(i == LIBUVC_NUM_TRANSFER_BUFS ) {
667         UVC_DEBUG("orphan transfer %p not found; not freeing!", transfer);
668       }
669
670       pthread_cond_broadcast(&strmh->cb_cond);
671       pthread_mutex_unlock(&strmh->cb_mutex);
672     }
673   }
674 }
675
676 /** Begin streaming video from the camera into the callback function.
677  * @ingroup streaming
678  *
679  * @param devh UVC device
680  * @param ctrl Control block, processed using {uvc_probe_stream_ctrl} or
681  *             {uvc_get_stream_ctrl_format_size}
682  * @param cb   User callback function. See {uvc_frame_callback_t} for restrictions.
683  * @param flags Stream setup flags, currently undefined. Set this to zero. The lower bit
684  * is reserved for backward compatibility.
685  */
686 uvc_error_t uvc_start_streaming(
687     uvc_device_handle_t *devh,
688     uvc_stream_ctrl_t *ctrl,
689     uvc_frame_callback_t *cb,
690     void *user_ptr,
691     uint8_t flags
692 ) {
693   uvc_error_t ret;
694   uvc_stream_handle_t *strmh;
695
696   ret = uvc_stream_open_ctrl(devh, &strmh, ctrl);
697   if (ret != UVC_SUCCESS)
698     return ret;
699
700   ret = uvc_stream_start(strmh, cb, user_ptr, flags);
701   if (ret != UVC_SUCCESS) {
702     uvc_stream_close(strmh);
703     return ret;
704   }
705
706   return UVC_SUCCESS;
707 }
708
709 /** Begin streaming video from the camera into the callback function.
710  * @ingroup streaming
711  *
712  * @deprecated The stream type (bulk vs. isochronous) will be determined by the
713  * type of interface associated with the uvc_stream_ctrl_t parameter, regardless
714  * of whether the caller requests isochronous streaming. Please switch to
715  * uvc_start_streaming().
716  *
717  * @param devh UVC device
718  * @param ctrl Control block, processed using {uvc_probe_stream_ctrl} or
719  *             {uvc_get_stream_ctrl_format_size}
720  * @param cb   User callback function. See {uvc_frame_callback_t} for restrictions.
721  */
722 uvc_error_t uvc_start_iso_streaming(
723     uvc_device_handle_t *devh,
724     uvc_stream_ctrl_t *ctrl,
725     uvc_frame_callback_t *cb,
726     void *user_ptr
727 ) {
728   return uvc_start_streaming(devh, ctrl, cb, user_ptr, 0);
729 }
730
731 static uvc_stream_handle_t *_uvc_get_stream_by_interface(uvc_device_handle_t *devh, int interface_idx) {
732   uvc_stream_handle_t *strmh;
733
734   DL_FOREACH(devh->streams, strmh) {
735     if (strmh->stream_if->bInterfaceNumber == interface_idx)
736       return strmh;
737   }
738
739   return NULL;
740 }
741
742 static uvc_streaming_interface_t *_uvc_get_stream_if(uvc_device_handle_t *devh, int interface_idx) {
743   uvc_streaming_interface_t *stream_if;
744
745   DL_FOREACH(devh->info->stream_ifs, stream_if) {
746     if (stream_if->bInterfaceNumber == interface_idx)
747       return stream_if;
748   }
749   
750   return NULL;
751 }
752
753 /** Open a new video stream.
754  * @ingroup streaming
755  *
756  * @param devh UVC device
757  * @param ctrl Control block, processed using {uvc_probe_stream_ctrl} or
758  *             {uvc_get_stream_ctrl_format_size}
759  */
760 uvc_error_t uvc_stream_open_ctrl(uvc_device_handle_t *devh, uvc_stream_handle_t **strmhp, uvc_stream_ctrl_t *ctrl) {
761   /* Chosen frame and format descriptors */
762   uvc_stream_handle_t *strmh = NULL;
763   uvc_streaming_interface_t *stream_if;
764   uvc_error_t ret;
765
766   UVC_ENTER();
767
768   if (_uvc_get_stream_by_interface(devh, ctrl->bInterfaceNumber) != NULL) {
769     ret = UVC_ERROR_BUSY; /* Stream is already opened */
770     goto fail;
771   }
772
773   stream_if = _uvc_get_stream_if(devh, ctrl->bInterfaceNumber);
774   if (!stream_if) {
775     ret = UVC_ERROR_INVALID_PARAM;
776     goto fail;
777   }
778
779   strmh = calloc(1, sizeof(*strmh));
780   if (!strmh) {
781     ret = UVC_ERROR_NO_MEM;
782     goto fail;
783   }
784   strmh->devh = devh;
785   strmh->stream_if = stream_if;
786   strmh->frame.library_owns_data = 1;
787
788   ret = uvc_claim_if(strmh->devh, strmh->stream_if->bInterfaceNumber);
789   if (ret != UVC_SUCCESS)
790     goto fail;
791
792   ret = uvc_stream_ctrl(strmh, ctrl);
793   if (ret != UVC_SUCCESS)
794     goto fail;
795
796   // Set up the streaming status and data space
797   strmh->running = 0;
798   /** @todo take only what we need */
799   strmh->outbuf = malloc( LIBUVC_XFER_BUF_SIZE );
800   strmh->holdbuf = malloc( LIBUVC_XFER_BUF_SIZE );
801    
802   pthread_mutex_init(&strmh->cb_mutex, NULL);
803   pthread_cond_init(&strmh->cb_cond, NULL);
804
805   DL_APPEND(devh->streams, strmh);
806
807   *strmhp = strmh;
808
809   UVC_EXIT(0);
810   return UVC_SUCCESS;
811
812 fail:
813   if(strmh)
814     free(strmh);
815   UVC_EXIT(ret);
816   return ret;
817 }
818
819 /** Begin streaming video from the stream into the callback function.
820  * @ingroup streaming
821  *
822  * @param strmh UVC stream
823  * @param cb   User callback function. See {uvc_frame_callback_t} for restrictions.
824  * @param flags Stream setup flags, currently undefined. Set this to zero. The lower bit
825  * is reserved for backward compatibility.
826  */
827 uvc_error_t uvc_stream_start(
828     uvc_stream_handle_t *strmh,
829     uvc_frame_callback_t *cb,
830     void *user_ptr,
831     uint8_t flags
832 ) {
833   /* USB interface we'll be using */
834   const struct libusb_interface *interface;
835   int interface_id;
836   char isochronous;
837   uvc_frame_desc_t *frame_desc;
838   uvc_format_desc_t *format_desc;
839   uvc_stream_ctrl_t *ctrl;
840   uvc_error_t ret;
841   /* Total amount of data per transfer */
842   size_t total_transfer_size = 0;
843   struct libusb_transfer *transfer;
844   int transfer_id;
845
846   ctrl = &strmh->cur_ctrl;
847
848   UVC_ENTER();
849
850   if (strmh->running) {
851     UVC_EXIT(UVC_ERROR_BUSY);
852     return UVC_ERROR_BUSY;
853   }
854
855   strmh->running = 1;
856   strmh->seq = 1;
857   strmh->fid = 0;
858   strmh->pts = 0;
859   strmh->last_scr = 0;
860
861   frame_desc = uvc_find_frame_desc_stream(strmh, ctrl->bFormatIndex, ctrl->bFrameIndex);
862   if (!frame_desc) {
863     ret = UVC_ERROR_INVALID_PARAM;
864     goto fail;
865   }
866   format_desc = frame_desc->parent;
867
868   strmh->frame_format = uvc_frame_format_for_guid(format_desc->guidFormat);
869   if (strmh->frame_format == UVC_FRAME_FORMAT_UNKNOWN) {
870     ret = UVC_ERROR_NOT_SUPPORTED;
871     goto fail;
872   }
873
874   // Get the interface that provides the chosen format and frame configuration
875   interface_id = strmh->stream_if->bInterfaceNumber;
876   interface = &strmh->devh->info->config->interface[interface_id];
877
878   /* A VS interface uses isochronous transfers iff it has multiple altsettings.
879    * (UVC 1.5: 2.4.3. VideoStreaming Interface) */
880   isochronous = interface->num_altsetting > 1;
881
882   if (isochronous) {
883     /* For isochronous streaming, we choose an appropriate altsetting for the endpoint
884      * and set up several transfers */
885     const struct libusb_interface_descriptor *altsetting = 0;
886     const struct libusb_endpoint_descriptor *endpoint;
887     /* The greatest number of bytes that the device might provide, per packet, in this
888      * configuration */
889     size_t config_bytes_per_packet;
890     /* Number of packets per transfer */
891     size_t packets_per_transfer = 0;
892     /* Size of packet transferable from the chosen endpoint */
893     size_t endpoint_bytes_per_packet = 0;
894     /* Index of the altsetting */
895     int alt_idx, ep_idx;
896     
897     config_bytes_per_packet = strmh->cur_ctrl.dwMaxPayloadTransferSize;
898
899     /* Go through the altsettings and find one whose packets are at least
900      * as big as our format's maximum per-packet usage. Assume that the
901      * packet sizes are increasing. */
902     for (alt_idx = 0; alt_idx < interface->num_altsetting; alt_idx++) {
903       altsetting = interface->altsetting + alt_idx;
904       endpoint_bytes_per_packet = 0;
905
906       /* Find the endpoint with the number specified in the VS header */
907       for (ep_idx = 0; ep_idx < altsetting->bNumEndpoints; ep_idx++) {
908         endpoint = altsetting->endpoint + ep_idx;
909
910         if (endpoint->bEndpointAddress == format_desc->parent->bEndpointAddress) {
911           endpoint_bytes_per_packet = endpoint->wMaxPacketSize;
912           // wMaxPacketSize: [unused:2 (multiplier-1):3 size:11]
913           endpoint_bytes_per_packet = (endpoint_bytes_per_packet & 0x07ff) *
914                                       (((endpoint_bytes_per_packet >> 11) & 3) + 1);
915           break;
916         }
917       }
918
919       if (endpoint_bytes_per_packet >= config_bytes_per_packet) {
920         /* Transfers will be at most one frame long: Divide the maximum frame size
921          * by the size of the endpoint and round up */
922         packets_per_transfer = (ctrl->dwMaxVideoFrameSize +
923                                 endpoint_bytes_per_packet - 1) / endpoint_bytes_per_packet;
924
925         /* But keep a reasonable limit: Otherwise we start dropping data */
926         if (packets_per_transfer > 32)
927           packets_per_transfer = 32;
928         
929         total_transfer_size = packets_per_transfer * endpoint_bytes_per_packet;
930         break;
931       }
932     }
933
934     /* If we searched through all the altsettings and found nothing usable */
935     if (alt_idx == interface->num_altsetting) {
936       ret = UVC_ERROR_INVALID_MODE;
937       goto fail;
938     }
939
940     /* Select the altsetting */
941     ret = libusb_set_interface_alt_setting(strmh->devh->usb_devh,
942                                            altsetting->bInterfaceNumber,
943                                            altsetting->bAlternateSetting);
944     if (ret != UVC_SUCCESS) {
945       UVC_DEBUG("libusb_set_interface_alt_setting failed");
946       goto fail;
947     }
948
949     /* Set up the transfers */
950     for (transfer_id = 0; transfer_id < LIBUVC_NUM_TRANSFER_BUFS; ++transfer_id) {
951       transfer = libusb_alloc_transfer(packets_per_transfer);
952       strmh->transfers[transfer_id] = transfer;      
953       strmh->transfer_bufs[transfer_id] = malloc(total_transfer_size);
954
955       libusb_fill_iso_transfer(
956         transfer, strmh->devh->usb_devh, format_desc->parent->bEndpointAddress,
957         strmh->transfer_bufs[transfer_id],
958         total_transfer_size, packets_per_transfer, _uvc_stream_callback, (void*) strmh, 5000);
959
960       libusb_set_iso_packet_lengths(transfer, endpoint_bytes_per_packet);
961     }
962   } else {
963     for (transfer_id = 0; transfer_id < LIBUVC_NUM_TRANSFER_BUFS;
964         ++transfer_id) {
965       transfer = libusb_alloc_transfer(0);
966       strmh->transfers[transfer_id] = transfer;
967       strmh->transfer_bufs[transfer_id] = malloc (
968           strmh->cur_ctrl.dwMaxPayloadTransferSize );
969       libusb_fill_bulk_transfer ( transfer, strmh->devh->usb_devh,
970           format_desc->parent->bEndpointAddress,
971           strmh->transfer_bufs[transfer_id],
972           strmh->cur_ctrl.dwMaxPayloadTransferSize, _uvc_stream_callback,
973           ( void* ) strmh, 5000 );
974     }
975   }
976
977   strmh->user_cb = cb;
978   strmh->user_ptr = user_ptr;
979
980   /* If the user wants it, set up a thread that calls the user's function
981    * with the contents of each frame.
982    */
983   if (cb) {
984     pthread_create(&strmh->cb_thread, NULL, _uvc_user_caller, (void*) strmh);
985   }
986
987   for (transfer_id = 0; transfer_id < LIBUVC_NUM_TRANSFER_BUFS;
988       transfer_id++) {
989     ret = libusb_submit_transfer(strmh->transfers[transfer_id]);
990     if (ret != UVC_SUCCESS) {
991       UVC_DEBUG("libusb_submit_transfer failed: %d",ret);
992       break;
993     }
994   }
995
996   if ( ret != UVC_SUCCESS && transfer_id > 0 ) {
997     for ( ; transfer_id < LIBUVC_NUM_TRANSFER_BUFS; transfer_id++) {
998       free ( strmh->transfers[transfer_id]->buffer );
999       libusb_free_transfer ( strmh->transfers[transfer_id]);
1000       strmh->transfers[transfer_id] = 0;
1001     }
1002     ret = UVC_SUCCESS;
1003   }
1004
1005   UVC_EXIT(ret);
1006   return ret;
1007 fail:
1008   strmh->running = 0;
1009   UVC_EXIT(ret);
1010   return ret;
1011 }
1012
1013 /** Begin streaming video from the stream into the callback function.
1014  * @ingroup streaming
1015  *
1016  * @deprecated The stream type (bulk vs. isochronous) will be determined by the
1017  * type of interface associated with the uvc_stream_ctrl_t parameter, regardless
1018  * of whether the caller requests isochronous streaming. Please switch to
1019  * uvc_stream_start().
1020  *
1021  * @param strmh UVC stream
1022  * @param cb   User callback function. See {uvc_frame_callback_t} for restrictions.
1023  */
1024 uvc_error_t uvc_stream_start_iso(
1025     uvc_stream_handle_t *strmh,
1026     uvc_frame_callback_t *cb,
1027     void *user_ptr
1028 ) {
1029   return uvc_stream_start(strmh, cb, user_ptr, 0);
1030 }
1031
1032 /** @internal
1033  * @brief User callback runner thread
1034  * @note There should be at most one of these per currently streaming device
1035  * @param arg Device handle
1036  */
1037 void *_uvc_user_caller(void *arg) {
1038   uvc_stream_handle_t *strmh = (uvc_stream_handle_t *) arg;
1039
1040   uint32_t last_seq = 0;
1041
1042   do {
1043     pthread_mutex_lock(&strmh->cb_mutex);
1044
1045     while (strmh->running && last_seq == strmh->hold_seq) {
1046       pthread_cond_wait(&strmh->cb_cond, &strmh->cb_mutex);
1047     }
1048
1049     if (!strmh->running) {
1050       pthread_mutex_unlock(&strmh->cb_mutex);
1051       break;
1052     }
1053     
1054     last_seq = strmh->hold_seq;
1055     _uvc_populate_frame(strmh);
1056     
1057     pthread_mutex_unlock(&strmh->cb_mutex);
1058     
1059     strmh->user_cb(&strmh->frame, strmh->user_ptr);
1060   } while(1);
1061
1062   return NULL; // return value ignored
1063 }
1064
1065 /** @internal
1066  * @brief Populate the fields of a frame to be handed to user code
1067  * must be called with stream cb lock held!
1068  */
1069 void _uvc_populate_frame(uvc_stream_handle_t *strmh) {
1070   uvc_frame_t *frame = &strmh->frame;
1071   uvc_frame_desc_t *frame_desc;
1072
1073   /** @todo this stuff that hits the main config cache should really happen
1074    * in start() so that only one thread hits these data. all of this stuff
1075    * is going to be reopen_on_change anyway
1076    */
1077
1078   frame_desc = uvc_find_frame_desc(strmh->devh, strmh->cur_ctrl.bFormatIndex,
1079                                    strmh->cur_ctrl.bFrameIndex);
1080
1081   frame->frame_format = strmh->frame_format;
1082   
1083   frame->width = frame_desc->wWidth;
1084   frame->height = frame_desc->wHeight;
1085   
1086   switch (frame->frame_format) {
1087   case UVC_FRAME_FORMAT_YUYV:
1088     frame->step = frame->width * 2;
1089     break;
1090   case UVC_FRAME_FORMAT_MJPEG:
1091     frame->step = 0;
1092     break;
1093   default:
1094     frame->step = 0;
1095     break;
1096   }
1097
1098   frame->sequence = strmh->hold_seq;
1099   /** @todo set the frame time */
1100   // frame->capture_time
1101
1102   /* copy the image data from the hold buffer to the frame (unnecessary extra buf?) */
1103   if (frame->data_bytes < strmh->hold_bytes) {
1104     frame->data = realloc(frame->data, strmh->hold_bytes);
1105   }
1106   frame->data_bytes = strmh->hold_bytes;
1107   memcpy(frame->data, strmh->holdbuf, frame->data_bytes);
1108
1109
1110
1111 }
1112
1113 /** Poll for a frame
1114  * @ingroup streaming
1115  *
1116  * @param devh UVC device
1117  * @param[out] frame Location to store pointer to captured frame (NULL on error)
1118  * @param timeout_us >0: Wait at most N microseconds; 0: Wait indefinitely; -1: return immediately
1119  */
1120 uvc_error_t uvc_stream_get_frame(uvc_stream_handle_t *strmh,
1121                           uvc_frame_t **frame,
1122                           int32_t timeout_us) {
1123   time_t add_secs;
1124   time_t add_nsecs;
1125   struct timespec ts;
1126   struct timeval tv;
1127
1128   if (!strmh->running)
1129     return UVC_ERROR_INVALID_PARAM;
1130
1131   if (strmh->user_cb)
1132     return UVC_ERROR_CALLBACK_EXISTS;
1133
1134   pthread_mutex_lock(&strmh->cb_mutex);
1135
1136   if (strmh->last_polled_seq < strmh->hold_seq) {
1137     _uvc_populate_frame(strmh);
1138     *frame = &strmh->frame;
1139     strmh->last_polled_seq = strmh->hold_seq;
1140   } else if (timeout_us != -1) {
1141     if (timeout_us == 0) {
1142       pthread_cond_wait(&strmh->cb_cond, &strmh->cb_mutex);
1143     } else {
1144       add_secs = timeout_us / 1000000;
1145       add_nsecs = (timeout_us % 1000000) * 1000;
1146       ts.tv_sec = 0;
1147       ts.tv_nsec = 0;
1148
1149 #if _POSIX_TIMERS > 0
1150       clock_gettime(CLOCK_REALTIME, &ts);
1151 #else
1152       gettimeofday(&tv, NULL);
1153       ts.tv_sec = tv.tv_sec;
1154       ts.tv_nsec = tv.tv_usec * 1000;
1155 #endif
1156
1157       ts.tv_sec += add_secs;
1158       ts.tv_nsec += add_nsecs;
1159
1160       /* pthread_cond_timedwait FAILS with EINVAL if ts.tv_nsec > 1000000000 (1 billion)
1161        * Since we are just adding values to the timespec, we have to increment the seconds if nanoseconds is greater than 1 billion,
1162        * and then re-adjust the nanoseconds in the correct range.
1163        * */
1164       ts.tv_sec += ts.tv_nsec / 1000000000;
1165       ts.tv_nsec = ts.tv_nsec % 1000000000;
1166
1167       int err = pthread_cond_timedwait(&strmh->cb_cond, &strmh->cb_mutex, &ts);
1168
1169       //TODO: How should we handle EINVAL?
1170       switch(err){
1171       case EINVAL:
1172           *frame = NULL;
1173           return UVC_ERROR_OTHER;
1174       case ETIMEDOUT:
1175           *frame = NULL;
1176           return UVC_ERROR_TIMEOUT;
1177       }
1178     }
1179     
1180     if (strmh->last_polled_seq < strmh->hold_seq) {
1181       _uvc_populate_frame(strmh);
1182       *frame = &strmh->frame;
1183       strmh->last_polled_seq = strmh->hold_seq;
1184     } else {
1185       *frame = NULL;
1186     }
1187   } else {
1188     *frame = NULL;
1189   }
1190
1191   pthread_mutex_unlock(&strmh->cb_mutex);
1192
1193   return UVC_SUCCESS;
1194 }
1195
1196 /** @brief Stop streaming video
1197  * @ingroup streaming
1198  *
1199  * Closes all streams, ends threads and cancels pollers
1200  *
1201  * @param devh UVC device
1202  */
1203 void uvc_stop_streaming(uvc_device_handle_t *devh) {
1204   uvc_stream_handle_t *strmh, *strmh_tmp;
1205
1206   DL_FOREACH_SAFE(devh->streams, strmh, strmh_tmp) {
1207     uvc_stream_close(strmh);
1208   }
1209 }
1210
1211 /** @brief Stop stream.
1212  * @ingroup streaming
1213  *
1214  * Stops stream, ends threads and cancels pollers
1215  *
1216  * @param devh UVC device
1217  */
1218 uvc_error_t uvc_stream_stop(uvc_stream_handle_t *strmh) {
1219   int i;
1220
1221   if (!strmh->running)
1222     return UVC_ERROR_INVALID_PARAM;
1223
1224   strmh->running = 0;
1225
1226   pthread_mutex_lock(&strmh->cb_mutex);
1227
1228   for(i=0; i < LIBUVC_NUM_TRANSFER_BUFS; i++) {
1229     if(strmh->transfers[i] != NULL) {
1230       int res = libusb_cancel_transfer(strmh->transfers[i]);
1231       if(res < 0 && res != LIBUSB_ERROR_NOT_FOUND ) {
1232         free(strmh->transfers[i]->buffer);
1233         libusb_free_transfer(strmh->transfers[i]);
1234         strmh->transfers[i] = NULL;
1235       }
1236     }
1237   }
1238
1239   /* Wait for transfers to complete/cancel */
1240   do {
1241     for(i=0; i < LIBUVC_NUM_TRANSFER_BUFS; i++) {
1242       if(strmh->transfers[i] != NULL)
1243         break;
1244     }
1245     if(i == LIBUVC_NUM_TRANSFER_BUFS )
1246       break;
1247     pthread_cond_wait(&strmh->cb_cond, &strmh->cb_mutex);
1248   } while(1);
1249   // Kick the user thread awake
1250   pthread_cond_broadcast(&strmh->cb_cond);
1251   pthread_mutex_unlock(&strmh->cb_mutex);
1252
1253   /** @todo stop the actual stream, camera side? */
1254
1255   if (strmh->user_cb) {
1256     /* wait for the thread to stop (triggered by
1257      * LIBUSB_TRANSFER_CANCELLED transfer) */
1258     pthread_join(strmh->cb_thread, NULL);
1259   }
1260
1261   return UVC_SUCCESS;
1262 }
1263
1264 /** @brief Close stream.
1265  * @ingroup streaming
1266  *
1267  * Closes stream, frees handle and all streaming resources.
1268  *
1269  * @param strmh UVC stream handle
1270  */
1271 void uvc_stream_close(uvc_stream_handle_t *strmh) {
1272   if (strmh->running)
1273     uvc_stream_stop(strmh);
1274
1275   uvc_release_if(strmh->devh, strmh->stream_if->bInterfaceNumber);
1276
1277   if (strmh->frame.data)
1278     free(strmh->frame.data);
1279
1280   free(strmh->outbuf);
1281   free(strmh->holdbuf);
1282
1283   pthread_cond_destroy(&strmh->cb_cond);
1284   pthread_mutex_destroy(&strmh->cb_mutex);
1285
1286   DL_DELETE(strmh->devh->streams, strmh);
1287   free(strmh);
1288 }