stream pushing ok without access permission of /dev/video0
[rtmpclient.git] / app / src / main / jni / libuvc-0.0.6 / src / init.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 \mainpage libuvc: a cross-platform library for USB video devices
36
37 \b libuvc is a library that supports enumeration, control and streaming
38 for USB Video Class (UVC) devices, such as consumer webcams.
39
40 \section features Features
41 \li UVC device \ref device "discovery and management" API
42 \li \ref streaming "Video streaming" (device to host) with asynchronous/callback and synchronous/polling modes
43 \li Read/write access to standard \ref ctrl "device settings"
44 \li \ref frame "Conversion" between various formats: RGB, YUV, JPEG, etc.
45 \li Tested on Mac and Linux, portable to Windows and some BSDs
46
47 \section roadmap Roadmap
48 \li Bulk-mode image capture
49 \li One-shot image capture
50 \li Improved support for standard settings
51 \li Support for "extended" (vendor-defined) settings
52
53 \section misc Misc.
54 \p The source code can be found at https://github.com/ktossell/libuvc. To build
55 the library, install <a href="http://libusb.org/">libusb</a> 1.0+ and run:
56
57 \code
58 $ git clone https://github.com/ktossell/libuvc.git
59 $ cd libuvc
60 $ mkdir build
61 $ cd build
62 $ cmake -DCMAKE_BUILD_TYPE=Release ..
63 $ make && make install
64 \endcode
65
66 \section Example
67 In this example, libuvc is used to acquire images in a 30 fps, 640x480
68 YUV stream from a UVC device such as a standard webcam.
69
70 \include example.c
71
72 */
73
74 /**
75  * @defgroup init Library initialization/deinitialization
76  * @brief Setup routines used to construct UVC access contexts
77  */
78 #include "libuvc/libuvc.h"
79 #include "libuvc/libuvc_internal.h"
80
81 /** @internal
82  * @brief Event handler thread
83  * There's one of these per UVC context.
84  * @todo We shouldn't run this if we don't own the USB context
85  */
86 void *_uvc_handle_events(void *arg) {
87   uvc_context_t *ctx = (uvc_context_t *) arg;
88
89   while (!ctx->kill_handler_thread)
90     libusb_handle_events_completed(ctx->usb_ctx, &ctx->kill_handler_thread);
91   return NULL;
92 }
93
94 /** @brief Initializes the UVC context
95  * @ingroup init
96  *
97  * @note If you provide your own USB context, you must handle
98  * libusb event processing using a function such as libusb_handle_events.
99  *
100  * @param[out] pctx The location where the context reference should be stored.
101  * @param[in]  usb_ctx Optional USB context to use
102  * @return Error opening context or UVC_SUCCESS
103  */
104 uvc_error_t uvc_init(uvc_context_t **pctx, struct libusb_context *usb_ctx) {
105   uvc_error_t ret = UVC_SUCCESS;
106   uvc_context_t *ctx = calloc(1, sizeof(*ctx));
107
108   if (usb_ctx == NULL) {
109     ret = libusb_init(&ctx->usb_ctx);
110     ctx->own_usb_ctx = 1;
111     if (ret != UVC_SUCCESS) {
112       free(ctx);
113       ctx = NULL;
114     }
115   } else {
116     ctx->own_usb_ctx = 0;
117     ctx->usb_ctx = usb_ctx;
118   }
119
120   if (ctx != NULL)
121     *pctx = ctx;
122
123   return ret;
124 }
125
126 /**
127  * @brief Closes the UVC context, shutting down any active cameras.
128  * @ingroup init
129  *
130  * @note This function invalides any existing references to the context's
131  * cameras.
132  *
133  * If no USB context was provided to #uvc_init, the UVC-specific USB
134  * context will be destroyed.
135  *
136  * @param ctx UVC context to shut down
137  */
138 void uvc_exit(uvc_context_t *ctx) {
139   uvc_device_handle_t *devh;
140
141   DL_FOREACH(ctx->open_devices, devh) {
142     uvc_close(devh);
143   }
144
145   if (ctx->own_usb_ctx)
146     libusb_exit(ctx->usb_ctx);
147
148   free(ctx);
149 }
150
151 /**
152  * @internal
153  * @brief Spawns a handler thread for the context
154  * @ingroup init
155  *
156  * This should be called at the end of a successful uvc_open if no devices
157  * are already open (and being handled).
158  */
159 void uvc_start_handler_thread(uvc_context_t *ctx) {
160   if (ctx->own_usb_ctx)
161     pthread_create(&ctx->handler_thread, NULL, _uvc_handle_events, (void*) ctx);
162 }
163