blob: c98f154c97c756570e3ae3281dbf82739740878f
1 | #ifndef __CAMERA_HW__ |
2 | #define __CAMERA_HW__ |
3 | |
4 | //#define LOG_NDEBUG 0 |
5 | #define LOG_TAG "Camera_hw" |
6 | |
7 | #include <errno.h> |
8 | #include "camera_hw.h" |
9 | |
10 | #ifdef __cplusplus |
11 | //extern "C" { |
12 | #endif |
13 | static int set_rotate_value(int camera_fd, int value) |
14 | { |
15 | int ret = 0; |
16 | struct v4l2_control ctl; |
17 | if(camera_fd<0) |
18 | return -1; |
19 | if((value!=0)&&(value!=90)&&(value!=180)&&(value!=270)){ |
20 | CAMHAL_LOGDB("Set rotate value invalid: %d.", value); |
21 | return -1; |
22 | } |
23 | memset( &ctl, 0, sizeof(ctl)); |
24 | ctl.value=value; |
25 | ctl.id = V4L2_CID_ROTATE; |
26 | ALOGD("set_rotate_value:: id =%x , value=%d",ctl.id,ctl.value); |
27 | ret = ioctl(camera_fd, VIDIOC_S_CTRL, &ctl); |
28 | if(ret<0){ |
29 | CAMHAL_LOGDB("Set rotate value fail: %s,errno=%d. ret=%d", strerror(errno),errno,ret); |
30 | } |
31 | return ret ; |
32 | } |
33 | |
34 | int camera_open(struct VideoInfo *cam_dev) |
35 | { |
36 | char dev_name[128]; |
37 | int ret; |
38 | |
39 | sprintf(dev_name, "%s%d", "/dev/video", cam_dev->idx); |
40 | cam_dev->fd = open(dev_name, O_RDWR | O_NONBLOCK); |
41 | //cam_dev->fd = open("/dev/video0", O_RDWR | O_NONBLOCK); |
42 | if (cam_dev->fd < 0){ |
43 | DBG_LOGB("open %s failed, errno=%d\n", dev_name, errno); |
44 | return -ENOTTY; |
45 | } |
46 | |
47 | ret = ioctl(cam_dev->fd, VIDIOC_QUERYCAP, &cam_dev->cap); |
48 | if (ret < 0) { |
49 | DBG_LOGB("VIDIOC_QUERYCAP, errno=%d", errno); |
50 | } |
51 | |
52 | if (!(cam_dev->cap.capabilities & V4L2_CAP_VIDEO_CAPTURE)) { |
53 | DBG_LOGB( "%s is not video capture device\n", |
54 | dev_name); |
55 | } |
56 | |
57 | if (!(cam_dev->cap.capabilities & V4L2_CAP_STREAMING)) { |
58 | DBG_LOGB( "video%d does not support streaming i/o\n", |
59 | cam_dev->idx); |
60 | } |
61 | |
62 | return ret; |
63 | } |
64 | |
65 | int setBuffersFormat(struct VideoInfo *cam_dev) |
66 | { |
67 | int ret = 0; |
68 | if ((cam_dev->preview.format.fmt.pix.width != 0) && (cam_dev->preview.format.fmt.pix.height != 0)) { |
69 | int pixelformat = cam_dev->preview.format.fmt.pix.pixelformat; |
70 | |
71 | ret = ioctl(cam_dev->fd, VIDIOC_S_FMT, &cam_dev->preview.format); |
72 | if (ret < 0) { |
73 | DBG_LOGB("Open: VIDIOC_S_FMT Failed: %s, ret=%d\n", strerror(errno), ret); |
74 | } |
75 | |
76 | CAMHAL_LOGIB("Width * Height %d x %d expect pixelfmt:%.4s, get:%.4s\n", |
77 | cam_dev->preview.format.fmt.pix.width, |
78 | cam_dev->preview.format.fmt.pix.height, |
79 | (char*)&pixelformat, |
80 | (char*)&cam_dev->preview.format.fmt.pix.pixelformat); |
81 | } |
82 | return ret; |
83 | } |
84 | |
85 | int start_capturing(struct VideoInfo *vinfo) |
86 | { |
87 | int ret = 0; |
88 | int i; |
89 | enum v4l2_buf_type type; |
90 | struct v4l2_buffer buf; |
91 | |
92 | if (vinfo->isStreaming) { |
93 | DBG_LOGA("already stream on\n"); |
94 | } |
95 | CLEAR(vinfo->preview.rb); |
96 | |
97 | vinfo->preview.rb.count = 6; |
98 | vinfo->preview.rb.type = V4L2_BUF_TYPE_VIDEO_CAPTURE; |
99 | //TODO DMABUF & ION |
100 | vinfo->preview.rb.memory = V4L2_MEMORY_MMAP; |
101 | |
102 | ret = ioctl(vinfo->fd, VIDIOC_REQBUFS, &vinfo->preview.rb); |
103 | if (ret < 0) { |
104 | DBG_LOGB("camera idx:%d does not support " |
105 | "memory mapping, errno=%d\n", vinfo->idx, errno); |
106 | } |
107 | |
108 | if (vinfo->preview.rb.count < 2) { |
109 | DBG_LOGB( "Insufficient buffer memory on /dev/video%d, errno=%d\n", |
110 | vinfo->idx, errno); |
111 | return -EINVAL; |
112 | } |
113 | |
114 | for (i = 0; i < (int)vinfo->preview.rb.count; ++i) { |
115 | |
116 | CLEAR(vinfo->preview.buf); |
117 | |
118 | vinfo->preview.buf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE; |
119 | vinfo->preview.buf.memory = V4L2_MEMORY_MMAP; |
120 | vinfo->preview.buf.index = i; |
121 | |
122 | if (-1 == ioctl(vinfo->fd, VIDIOC_QUERYBUF, &vinfo->preview.buf)){ |
123 | DBG_LOGB("VIDIOC_QUERYBUF, errno=%d", errno); |
124 | } |
125 | |
126 | vinfo->mem[i] = mmap(NULL /* start anywhere */, |
127 | vinfo->preview.buf.length, |
128 | PROT_READ | PROT_WRITE /* required */, |
129 | MAP_SHARED /* recommended */, |
130 | vinfo->fd, |
131 | vinfo->preview.buf.m.offset); |
132 | |
133 | if (MAP_FAILED == vinfo->mem[i]) { |
134 | DBG_LOGB("mmap failed, errno=%d\n", errno); |
135 | } |
136 | } |
137 | //////////////////////////////// |
138 | for (i = 0; i < (int)vinfo->preview.rb.count; ++i) { |
139 | |
140 | CLEAR(buf); |
141 | buf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE; |
142 | buf.memory = V4L2_MEMORY_MMAP; |
143 | buf.index = i; |
144 | |
145 | if (-1 == ioctl(vinfo->fd, VIDIOC_QBUF, &buf)) |
146 | DBG_LOGB("VIDIOC_QBUF failed, errno=%d\n", errno); |
147 | } |
148 | |
149 | type = V4L2_BUF_TYPE_VIDEO_CAPTURE; |
150 | if (-1 == ioctl(vinfo->fd, VIDIOC_STREAMON, &type)) |
151 | DBG_LOGB("VIDIOC_STREAMON, errno=%d\n", errno); |
152 | |
153 | vinfo->isStreaming = true; |
154 | |
155 | return 0; |
156 | } |
157 | |
158 | int stop_capturing(struct VideoInfo *vinfo) |
159 | { |
160 | enum v4l2_buf_type type; |
161 | int res = 0; |
162 | int i; |
163 | |
164 | if (!vinfo->isStreaming) |
165 | return -1; |
166 | |
167 | type = V4L2_BUF_TYPE_VIDEO_CAPTURE; |
168 | if (-1 == ioctl(vinfo->fd, VIDIOC_STREAMOFF, &type)){ |
169 | DBG_LOGB("VIDIOC_STREAMOFF, errno=%d", errno); |
170 | res = -1; |
171 | } |
172 | |
173 | for (i = 0; i < (int)vinfo->preview.rb.count; ++i) { |
174 | if (-1 == munmap(vinfo->mem[i], vinfo->preview.buf.length)) { |
175 | DBG_LOGB("munmap failed errno=%d", errno); |
176 | res = -1; |
177 | } |
178 | } |
179 | |
180 | vinfo->isStreaming = false; |
181 | |
182 | return res; |
183 | } |
184 | |
185 | int releasebuf_and_stop_capturing(struct VideoInfo *vinfo) |
186 | { |
187 | enum v4l2_buf_type type; |
188 | int res = 0 ,ret; |
189 | int i; |
190 | |
191 | if (!vinfo->isStreaming) |
192 | return -1; |
193 | |
194 | type = V4L2_BUF_TYPE_VIDEO_CAPTURE; |
195 | if (-1 == ioctl(vinfo->fd, VIDIOC_STREAMOFF, &type)){ |
196 | DBG_LOGB("VIDIOC_STREAMOFF, errno=%d", errno); |
197 | res = -1; |
198 | } |
199 | |
200 | for (i = 0; i < (int)vinfo->preview.rb.count; ++i) { |
201 | if (-1 == munmap(vinfo->mem[i], vinfo->preview.buf.length)) { |
202 | DBG_LOGB("munmap failed errno=%d", errno); |
203 | res = -1; |
204 | } |
205 | } |
206 | |
207 | vinfo->isStreaming = false; |
208 | |
209 | vinfo->preview.format.type = V4L2_BUF_TYPE_VIDEO_CAPTURE; |
210 | vinfo->preview.rb.memory = V4L2_MEMORY_MMAP; |
211 | vinfo->preview.rb.count = 0; |
212 | |
213 | ret = ioctl(vinfo->fd, VIDIOC_REQBUFS, &vinfo->preview.rb); |
214 | if (ret < 0) { |
215 | DBG_LOGB("VIDIOC_REQBUFS failed: %s", strerror(errno)); |
216 | //return ret; |
217 | }else{ |
218 | DBG_LOGA("VIDIOC_REQBUFS delete buffer success\n"); |
219 | } |
220 | |
221 | return res; |
222 | } |
223 | |
224 | |
225 | uintptr_t get_frame_phys(struct VideoInfo *vinfo) |
226 | { |
227 | CLEAR(vinfo->preview.buf); |
228 | |
229 | vinfo->preview.buf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE; |
230 | vinfo->preview.buf.memory = V4L2_MEMORY_MMAP; |
231 | |
232 | if (-1 == ioctl(vinfo->fd, VIDIOC_DQBUF, &vinfo->preview.buf)) { |
233 | switch (errno) { |
234 | case EAGAIN: |
235 | return 0; |
236 | |
237 | case EIO: |
238 | /* Could ignore EIO, see spec. */ |
239 | |
240 | /* fall through */ |
241 | |
242 | default: |
243 | DBG_LOGB("VIDIOC_DQBUF failed, errno=%d\n", errno); |
244 | exit(1); |
245 | } |
246 | DBG_LOGB("VIDIOC_DQBUF failed, errno=%d\n", errno); |
247 | } |
248 | |
249 | return (uintptr_t)vinfo->preview.buf.m.userptr; |
250 | } |
251 | |
252 | void *get_frame(struct VideoInfo *vinfo) |
253 | { |
254 | CLEAR(vinfo->preview.buf); |
255 | |
256 | vinfo->preview.buf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE; |
257 | vinfo->preview.buf.memory = V4L2_MEMORY_MMAP; |
258 | |
259 | if (-1 == ioctl(vinfo->fd, VIDIOC_DQBUF, &vinfo->preview.buf)) { |
260 | switch (errno) { |
261 | case EAGAIN: |
262 | return NULL; |
263 | |
264 | case EIO: |
265 | /* Could ignore EIO, see spec. */ |
266 | |
267 | /* fall through */ |
268 | |
269 | default: |
270 | DBG_LOGB("VIDIOC_DQBUF failed, errno=%d\n", errno); |
271 | exit(1); |
272 | } |
273 | DBG_LOGB("VIDIOC_DQBUF failed, errno=%d\n", errno); |
274 | } |
275 | //DBG_LOGA("get frame\n"); |
276 | |
277 | return vinfo->mem[vinfo->preview.buf.index]; |
278 | } |
279 | |
280 | int putback_frame(struct VideoInfo *vinfo) |
281 | { |
282 | |
283 | if (-1 == ioctl(vinfo->fd, VIDIOC_QBUF, &vinfo->preview.buf)) |
284 | DBG_LOGB("QBUF failed error=%d\n", errno); |
285 | |
286 | return 0; |
287 | } |
288 | |
289 | int start_picture(struct VideoInfo *vinfo, int rotate) |
290 | { |
291 | int ret = 0; |
292 | int i; |
293 | enum v4l2_buf_type type; |
294 | struct v4l2_buffer buf; |
295 | bool usbcamera = false; |
296 | |
297 | CLEAR(vinfo->picture.rb); |
298 | |
299 | //step 1 : ioctl VIDIOC_S_FMT |
300 | ret = ioctl(vinfo->fd, VIDIOC_S_FMT, &vinfo->picture.format); |
301 | if (ret < 0) { |
302 | DBG_LOGB("Open: VIDIOC_S_FMT Failed: %s, ret=%d\n", strerror(errno), ret); |
303 | } |
304 | |
305 | //step 2 : request buffer |
306 | vinfo->picture.rb.count = 1; |
307 | vinfo->picture.rb.type = V4L2_BUF_TYPE_VIDEO_CAPTURE; |
308 | //TODO DMABUF & ION |
309 | vinfo->picture.rb.memory = V4L2_MEMORY_MMAP; |
310 | |
311 | ret = ioctl(vinfo->fd, VIDIOC_REQBUFS, &vinfo->picture.rb); |
312 | if (ret < 0) { |
313 | DBG_LOGB("camera idx:%d does not support " |
314 | "memory mapping, errno=%d\n", vinfo->idx, errno); |
315 | } |
316 | |
317 | if (vinfo->picture.rb.count < 1) { |
318 | DBG_LOGB( "Insufficient buffer memory on /dev/video%d, errno=%d\n", |
319 | vinfo->idx, errno); |
320 | return -EINVAL; |
321 | } |
322 | |
323 | //step 3: mmap buffer |
324 | for (i = 0; i < (int)vinfo->picture.rb.count; ++i) { |
325 | |
326 | CLEAR(vinfo->picture.buf); |
327 | |
328 | vinfo->picture.buf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE; |
329 | vinfo->picture.buf.memory = V4L2_MEMORY_MMAP; |
330 | vinfo->picture.buf.index = i; |
331 | |
332 | if (-1 == ioctl(vinfo->fd, VIDIOC_QUERYBUF, &vinfo->picture.buf)){ |
333 | DBG_LOGB("VIDIOC_QUERYBUF, errno=%d", errno); |
334 | } |
335 | vinfo->mem_pic[i] = mmap(NULL /* start anywhere */, |
336 | vinfo->picture.buf.length, |
337 | PROT_READ | PROT_WRITE /* required */, |
338 | MAP_SHARED /* recommended */, |
339 | vinfo->fd, |
340 | vinfo->picture.buf.m.offset); |
341 | |
342 | if (MAP_FAILED == vinfo->mem_pic[i]) { |
343 | DBG_LOGB("mmap failed, errno=%d\n", errno); |
344 | } |
345 | } |
346 | |
347 | //step 4 : QBUF |
348 | //////////////////////////////// |
349 | for (i = 0; i < (int)vinfo->picture.rb.count; ++i) { |
350 | |
351 | CLEAR(buf); |
352 | buf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE; |
353 | buf.memory = V4L2_MEMORY_MMAP; |
354 | buf.index = i; |
355 | |
356 | if (-1 == ioctl(vinfo->fd, VIDIOC_QBUF, &buf)) |
357 | DBG_LOGB("VIDIOC_QBUF failed, errno=%d\n", errno); |
358 | } |
359 | |
360 | if (vinfo->isPicture) { |
361 | DBG_LOGA("already stream on\n"); |
362 | } |
363 | |
364 | if (strstr((const char *)vinfo->cap.driver, "uvcvideo")) { |
365 | usbcamera = true; |
366 | } |
367 | if (!usbcamera) { |
368 | set_rotate_value(vinfo->fd,rotate); |
369 | } |
370 | |
371 | //step 5: Stream ON |
372 | type = V4L2_BUF_TYPE_VIDEO_CAPTURE; |
373 | if (-1 == ioctl(vinfo->fd, VIDIOC_STREAMON, &type)) |
374 | DBG_LOGB("VIDIOC_STREAMON, errno=%d\n", errno); |
375 | vinfo->isPicture = true; |
376 | |
377 | return 0; |
378 | |
379 | } |
380 | |
381 | void *get_picture(struct VideoInfo *vinfo) |
382 | { |
383 | CLEAR(vinfo->picture.buf); |
384 | |
385 | vinfo->picture.buf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE; |
386 | vinfo->picture.buf.memory = V4L2_MEMORY_MMAP; |
387 | |
388 | if (-1 == ioctl(vinfo->fd, VIDIOC_DQBUF, &vinfo->picture.buf)) { |
389 | switch (errno) { |
390 | case EAGAIN: |
391 | return NULL; |
392 | case EIO: |
393 | /* Could ignore EIO, see spec. */ |
394 | /* fall through */ |
395 | default: |
396 | DBG_LOGB("VIDIOC_DQBUF failed, errno=%d\n", errno); |
397 | exit(1); |
398 | } |
399 | } |
400 | DBG_LOGA("get picture\n"); |
401 | return vinfo->mem_pic[vinfo->picture.buf.index]; |
402 | } |
403 | |
404 | void stop_picture(struct VideoInfo *vinfo) |
405 | { |
406 | enum v4l2_buf_type type; |
407 | struct v4l2_buffer buf; |
408 | int i; |
409 | |
410 | if (!vinfo->isPicture) |
411 | return ; |
412 | |
413 | //QBUF |
414 | for (i = 0; i < (int)vinfo->picture.rb.count; ++i) { |
415 | CLEAR(buf); |
416 | buf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE; |
417 | buf.memory = V4L2_MEMORY_MMAP; |
418 | buf.index = i; |
419 | if (-1 == ioctl(vinfo->fd, VIDIOC_QBUF, &buf)) |
420 | DBG_LOGB("VIDIOC_QBUF failed, errno=%d\n", errno); |
421 | } |
422 | |
423 | //stream off and unmap buffer |
424 | type = V4L2_BUF_TYPE_VIDEO_CAPTURE; |
425 | if (-1 == ioctl(vinfo->fd, VIDIOC_STREAMOFF, &type)) |
426 | DBG_LOGB("VIDIOC_STREAMOFF, errno=%d", errno); |
427 | |
428 | for (i = 0; i < (int)vinfo->picture.rb.count; i++) |
429 | { |
430 | if (-1 == munmap(vinfo->mem_pic[i], vinfo->picture.buf.length)) |
431 | DBG_LOGB("munmap failed errno=%d", errno); |
432 | } |
433 | |
434 | set_rotate_value(vinfo->fd,0); |
435 | vinfo->isPicture = false; |
436 | setBuffersFormat(vinfo); |
437 | start_capturing(vinfo); |
438 | |
439 | } |
440 | |
441 | void releasebuf_and_stop_picture(struct VideoInfo *vinfo) |
442 | { |
443 | enum v4l2_buf_type type; |
444 | struct v4l2_buffer buf; |
445 | int i,ret; |
446 | |
447 | if (!vinfo->isPicture) |
448 | return ; |
449 | |
450 | //QBUF |
451 | for (i = 0; i < (int)vinfo->picture.rb.count; ++i) { |
452 | CLEAR(buf); |
453 | buf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE; |
454 | buf.memory = V4L2_MEMORY_MMAP; |
455 | buf.index = i; |
456 | if (-1 == ioctl(vinfo->fd, VIDIOC_QBUF, &buf)) |
457 | DBG_LOGB("VIDIOC_QBUF failed, errno=%d\n", errno); |
458 | } |
459 | |
460 | //stream off and unmap buffer |
461 | type = V4L2_BUF_TYPE_VIDEO_CAPTURE; |
462 | if (-1 == ioctl(vinfo->fd, VIDIOC_STREAMOFF, &type)) |
463 | DBG_LOGB("VIDIOC_STREAMOFF, errno=%d", errno); |
464 | |
465 | for (i = 0; i < (int)vinfo->picture.rb.count; i++) |
466 | { |
467 | if (-1 == munmap(vinfo->mem_pic[i], vinfo->picture.buf.length)) |
468 | DBG_LOGB("munmap failed errno=%d", errno); |
469 | } |
470 | |
471 | vinfo->isPicture = false; |
472 | |
473 | vinfo->picture.format.type = V4L2_BUF_TYPE_VIDEO_CAPTURE; |
474 | vinfo->picture.rb.memory = V4L2_MEMORY_MMAP; |
475 | vinfo->picture.rb.count = 0; |
476 | ret = ioctl(vinfo->fd, VIDIOC_REQBUFS, &vinfo->picture.rb); |
477 | if (ret < 0) { |
478 | DBG_LOGB("VIDIOC_REQBUFS failed: %s", strerror(errno)); |
479 | //return ret; |
480 | }else{ |
481 | DBG_LOGA("VIDIOC_REQBUFS delete buffer success\n"); |
482 | } |
483 | |
484 | setBuffersFormat(vinfo); |
485 | start_capturing(vinfo); |
486 | } |
487 | |
488 | void camera_close(struct VideoInfo *vinfo) |
489 | { |
490 | if (NULL == vinfo) { |
491 | DBG_LOGA("vinfo is null\n"); |
492 | return ; |
493 | } |
494 | |
495 | if (-1 == close(vinfo->fd)) |
496 | DBG_LOGB("close failed, errno=%d\n", errno); |
497 | |
498 | vinfo->fd = -1; |
499 | } |
500 | #ifdef __cplusplus |
501 | //} |
502 | #endif |
503 | #endif |
504 |