forked from ricohapi/libuvc-theta-sample
/
thetauvc.c
287 lines (226 loc) · 6.27 KB
/
thetauvc.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
/*
Copyright 2020 K. Takeo. All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions
are met:
1. Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above
copyright notice, this list of conditions and the following
disclaimer in the documentation and/or other materials provided
with the distribution.
3. Neither the name of the author nor other contributors may be
used to endorse or promote products derived from this software
without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
POSSIBILITY OF SUCH DAMAGE.
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include "libuvc/libuvc.h"
#include "thetauvc.h"
#define USBVID_RICOH 0x05ca
#define USBPID_THETAV_UVC 0x2712
#define USBPID_THETAZ1_UVC 0x2715
#define USBPID_THETAX_UVC 0x2717
struct thetauvc_mode {
unsigned int mode;
unsigned int width;
unsigned int height;
unsigned int fps;
};
typedef struct thetauvc_mode thetauvc_mode_t;
static thetauvc_mode_t stream_mode[] = {
{
.mode = THETAUVC_MODE_UHD_2997,
.width = 3840,
.height = 1920,
.fps = 29
},
{
.mode = THETAUVC_MODE_FHD_2997,
.width = 1920,
.height = 960,
.fps = 29
},
{
.mode = THETAUVC_MODE_NUM,
.width = 0,
.height = 0,
.fps = 0
}
};
uvc_error_t
thetauvc_find_devices(uvc_context_t *ctx, uvc_device_t ***devs)
{
uvc_device_t **devlist, **ret;
uvc_device_t *dev;
uvc_error_t res;
int idx, devcnt;
res = uvc_find_devices(ctx, &devlist, USBVID_RICOH, 0, NULL);
if (res != UVC_SUCCESS) {
return res;
}
ret = (uvc_device_t **)malloc(sizeof(uvc_device_t *));
if (ret == NULL) {
uvc_free_device_list(devlist, 1);
return UVC_ERROR_NO_MEM;
}
*ret = NULL;
for (idx = 0, devcnt = 0; (dev = devlist[idx]) != NULL; idx++) {
uvc_device_descriptor_t *desc;
if (uvc_get_device_descriptor(dev, &desc) != UVC_SUCCESS)
continue;
if (desc->idProduct == USBPID_THETAV_UVC
|| desc->idProduct == USBPID_THETAZ1_UVC
|| desc->idProduct == USBPID_THETAX_UVC) {
void *tmp_ptr;
devcnt++;
tmp_ptr = realloc(ret, (devcnt+1) * sizeof(uvc_device_t *));
if (tmp_ptr == NULL) {
uvc_free_device_list(devlist, 1);
uvc_free_device_descriptor(desc);
free(ret);
return UVC_ERROR_NO_MEM;
}
ret = tmp_ptr;
ret[devcnt-1] = dev;
ret[devcnt] = NULL;
}
uvc_free_device_descriptor(desc);
}
for (idx=0; idx < devcnt; idx++)
uvc_ref_device(ret[idx]);
uvc_free_device_list(devlist, 1);
if (devcnt) {
*devs = ret;
return UVC_SUCCESS;
} else {
free(ret);
return UVC_ERROR_NO_DEVICE;
}
}
uvc_error_t
thetauvc_print_devices(uvc_context_t *ctx, FILE *fp)
{
uvc_device_t **devlist;
uvc_error_t res;
FILE *outfp;
int idx;
outfp = (fp == NULL) ? stdout : fp;
res = thetauvc_find_devices(ctx, &devlist);
if (res != UVC_SUCCESS) {
uvc_perror(res,"");
return res;
}
fprintf(outfp, "No : %-18s : %-10s\n", "Product", "Serial");
for (idx = 0; devlist[idx] != NULL; idx++) {
uvc_device_descriptor_t *desc;
if (uvc_get_device_descriptor(devlist[idx], &desc) != UVC_SUCCESS)
continue;
fprintf(outfp, "%2d : %-18s : %-10s\n", idx, desc->product,
desc->serialNumber);
uvc_free_device_descriptor(desc);
}
uvc_free_device_list(devlist, 1);
return UVC_SUCCESS;
}
uvc_error_t
thetauvc_find_device(uvc_context_t *ctx, uvc_device_t **devh, unsigned int index)
{
uvc_device_t **devlist;
uvc_error_t res;
unsigned int idx;
res = thetauvc_find_devices(ctx, &devlist);
if (res != UVC_SUCCESS)
return res;
for (idx = 0; idx <= index; idx++) {
if (devlist[idx] == NULL) {
uvc_free_device_list(devlist, 1);
return UVC_ERROR_NO_DEVICE;
}
}
uvc_ref_device(devlist[index]);
*devh = devlist[index];
uvc_free_device_list(devlist, 1);
return UVC_SUCCESS;
}
uvc_error_t
thetauvc_find_device_by_serial(uvc_context_t *ctx, uvc_device_t **devh,
const char *serial)
{
uvc_device_t **devlist, *dev;
uvc_error_t res;
unsigned int idx;
int found;
res = thetauvc_find_devices(ctx, &devlist);
if (res != UVC_SUCCESS)
return res;
found = 0;
if (serial == NULL) {
dev = devlist[0];
found = 1;
} else {
uvc_device_descriptor_t *desc;
idx = 0;
do {
dev = devlist[idx++];
if (dev == NULL) {
res = UVC_ERROR_NO_DEVICE;
break;
}
if (uvc_get_device_descriptor(dev, &desc) != UVC_SUCCESS)
continue;
if (desc->serialNumber && !strcmp(desc->serialNumber, serial))
found=1;
uvc_free_device_descriptor(desc);
} while (found != 1);
}
if (dev != NULL) {
uvc_ref_device(dev);
*devh = dev;
}
uvc_free_device_list(devlist, 1);
return res;
}
uvc_error_t
thetauvc_get_stream_ctrl_format_size(uvc_device_handle_t *devh,
unsigned int mode, uvc_stream_ctrl_t *ctrl)
{
uvc_error_t res;
thetauvc_mode_t *m;
if (!(mode < THETAUVC_MODE_NUM))
return UVC_ERROR_INVALID_MODE;
m = &stream_mode[mode];
res = uvc_get_stream_ctrl_format_size(devh, ctrl,
UVC_FRAME_FORMAT_H264, m->width, m->height, m->fps);
return res;
}
uvc_error_t
thetauvc_run_streaming(uvc_device_t *dev, uvc_device_handle_t **devh,
unsigned int mode, uvc_frame_callback_t *cb, void *pdata)
{
uvc_error_t res;
uvc_stream_ctrl_t ctrl;
res = uvc_open(dev, devh);
if (res != UVC_SUCCESS)
return res;
thetauvc_get_stream_ctrl_format_size(*devh, mode, &ctrl);
if (res != UVC_SUCCESS)
printf("error");
res = uvc_start_streaming(*devh, &ctrl, cb, pdata, 0);
uvc_close(*devh);
return UVC_SUCCESS;
}