main.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904
  1. #include <errno.h>
  2. #include <fcntl.h>
  3. #include <linux/videodev2.h>
  4. #include <linux/media.h>
  5. #include <linux/v4l2-subdev.h>
  6. #include <sys/ioctl.h>
  7. #include <sys/mman.h>
  8. #include <sys/stat.h>
  9. #include <time.h>
  10. #include <linux/kdev_t.h>
  11. #include <sys/sysmacros.h>
  12. #include <asm/errno.h>
  13. #include <gtk/gtk.h>
  14. #include "ini.h"
  15. #include "bayer.h"
  16. enum io_method {
  17. IO_METHOD_READ,
  18. IO_METHOD_MMAP,
  19. IO_METHOD_USERPTR,
  20. };
  21. struct buffer {
  22. void *start;
  23. size_t length;
  24. };
  25. struct buffer *buffers;
  26. static int *outbuffer;
  27. static unsigned int n_buffers;
  28. static enum io_method io = IO_METHOD_MMAP;
  29. // Rear camera
  30. static char *rear_dev_name;
  31. static int *rear_entity_id;
  32. static char *rear_dev[20];
  33. static int rear_width = -1;
  34. static int rear_height = -1;
  35. static int rear_rotate = 0;
  36. static int rear_fmt = V4L2_PIX_FMT_RGB24;
  37. static int rear_mbus = MEDIA_BUS_FMT_RGB888_1X24;
  38. // Front camera
  39. static char *front_dev_name;
  40. static int *front_entity_id;
  41. static char *front_dev[20];
  42. static int front_width = -1;
  43. static int front_height = -1;
  44. static int front_rotate = 0;
  45. static int front_fmt = V4L2_PIX_FMT_RGB24;
  46. static int front_mbus = MEDIA_BUS_FMT_RGB888_1X24;
  47. // Camera interface
  48. static char *media_drv_name;
  49. static int *interface_entity_id;
  50. static char *dev_name[20];
  51. static int media_fd;
  52. // State
  53. static int current_width = -1;
  54. static int current_height = -1;
  55. static int current_fmt = 0;
  56. static int current_rotate = 0;
  57. static int capture = 0;
  58. static cairo_surface_t *surface = NULL;
  59. static int preview_width = -1;
  60. static int preview_height = -1;
  61. // Widgets
  62. GObject *preview;
  63. static int
  64. xioctl(int fd, int request, void *arg)
  65. {
  66. int r;
  67. do {
  68. r = ioctl(fd, request, arg);
  69. } while (r == -1 && errno == EINTR);
  70. return r;
  71. }
  72. static void
  73. errno_exit(const char *s)
  74. {
  75. fprintf(stderr, "%s error %d, %s\n", s, errno, strerror(errno));
  76. exit(EXIT_FAILURE);
  77. }
  78. static void
  79. start_capturing(int fd)
  80. {
  81. enum v4l2_buf_type type;
  82. switch (io) {
  83. case IO_METHOD_READ:
  84. /* Nothing to do. */
  85. break;
  86. case IO_METHOD_MMAP:
  87. for (int i = 0; i < n_buffers; ++i) {
  88. struct v4l2_buffer buf = {
  89. .type = V4L2_BUF_TYPE_VIDEO_CAPTURE,
  90. .memory = V4L2_MEMORY_MMAP,
  91. .index = i,
  92. };
  93. if (xioctl(fd, VIDIOC_QBUF, &buf) == -1) {
  94. errno_exit("VIDIOC_QBUF");
  95. }
  96. }
  97. type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
  98. if (xioctl(fd, VIDIOC_STREAMON, &type) == -1) {
  99. errno_exit("VIDIOC_STREAMON");
  100. }
  101. break;
  102. case IO_METHOD_USERPTR:
  103. for (int i = 0; i < n_buffers; ++i) {
  104. struct v4l2_buffer buf = {
  105. .type = V4L2_BUF_TYPE_VIDEO_CAPTURE,
  106. .memory = V4L2_MEMORY_USERPTR,
  107. .index = i,
  108. };
  109. buf.m.userptr = (unsigned long) buffers[i].start;
  110. buf.length = buffers[i].length;
  111. if (xioctl(fd, VIDIOC_QBUF, &buf) == -1) {
  112. errno_exit("VIDIOC_QBUF");
  113. }
  114. }
  115. type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
  116. if (xioctl(fd, VIDIOC_STREAMON, &type) == -1) {
  117. errno_exit("VIDIOC_STREAMON");
  118. }
  119. break;
  120. }
  121. }
  122. static void
  123. init_mmap(int fd)
  124. {
  125. struct v4l2_requestbuffers req = {0};
  126. req.count = 4;
  127. req.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
  128. req.memory = V4L2_MEMORY_MMAP;
  129. if (xioctl(fd, VIDIOC_REQBUFS, &req) == -1) {
  130. if (errno == EINVAL) {
  131. fprintf(stderr, "%s does not support memory mapping",
  132. *dev_name);
  133. exit(EXIT_FAILURE);
  134. } else {
  135. errno_exit("VIDIOC_REQBUFS");
  136. }
  137. }
  138. if (req.count < 2) {
  139. fprintf(stderr, "Insufficient buffer memory on %s\n",
  140. *dev_name);
  141. exit(EXIT_FAILURE);
  142. }
  143. buffers = calloc(req.count, sizeof(buffers[0]));
  144. if (!buffers) {
  145. fprintf(stderr, "Out of memory\\n");
  146. exit(EXIT_FAILURE);
  147. }
  148. for (n_buffers = 0; n_buffers < req.count; ++n_buffers) {
  149. struct v4l2_buffer buf = {
  150. .type = V4L2_BUF_TYPE_VIDEO_CAPTURE,
  151. .memory = V4L2_MEMORY_MMAP,
  152. .index = n_buffers,
  153. };
  154. if (xioctl(fd, VIDIOC_QUERYBUF, &buf) == -1) {
  155. errno_exit("VIDIOC_QUERYBUF");
  156. }
  157. buffers[n_buffers].length = buf.length;
  158. buffers[n_buffers].start = mmap(NULL /* start anywhere */,
  159. buf.length,
  160. PROT_READ | PROT_WRITE /* required */,
  161. MAP_SHARED /* recommended */,
  162. fd, buf.m.offset);
  163. if (MAP_FAILED == buffers[n_buffers].start) {
  164. errno_exit("mmap");
  165. }
  166. }
  167. }
  168. static void
  169. init_sensor(char* fn, int width, int height, int mbus)
  170. {
  171. int fd;
  172. struct v4l2_subdev_format fmt;
  173. fd = open(fn, O_RDWR);
  174. g_printerr("Setting sensor to %dx%d fmt %d\n",
  175. width, height, mbus);
  176. fmt.pad = 0;
  177. fmt.which = V4L2_SUBDEV_FORMAT_ACTIVE;
  178. fmt.format.code = mbus;
  179. fmt.format.width = width;
  180. fmt.format.height = height;
  181. fmt.format.field = V4L2_FIELD_ANY;
  182. if (xioctl(fd, VIDIOC_SUBDEV_S_FMT, &fmt) == -1) {
  183. errno_exit("VIDIOC_SUBDEV_S_FMT");
  184. }
  185. g_printerr("Driver returned %dx%d fmt %d\n",
  186. fmt.format.width, fmt.format.height,
  187. fmt.format.code);
  188. close(fd);
  189. }
  190. static void
  191. init_device(int fd)
  192. {
  193. struct v4l2_capability cap;
  194. if (xioctl(fd, VIDIOC_QUERYCAP, &cap) == -1) {
  195. if (errno == EINVAL) {
  196. fprintf(stderr, "%s is no V4L2 device\n",
  197. *dev_name);
  198. exit(EXIT_FAILURE);
  199. } else {
  200. errno_exit("VIDIOC_QUERYCAP");
  201. }
  202. }
  203. if (!(cap.capabilities & V4L2_CAP_VIDEO_CAPTURE)) {
  204. fprintf(stderr, "%s is no video capture device\n",
  205. *dev_name);
  206. exit(EXIT_FAILURE);
  207. }
  208. switch (io) {
  209. case IO_METHOD_READ:
  210. if (!(cap.capabilities & V4L2_CAP_READWRITE)) {
  211. fprintf(stderr, "%s does not support read i/o\n",
  212. *dev_name);
  213. exit(EXIT_FAILURE);
  214. }
  215. break;
  216. case IO_METHOD_MMAP:
  217. case IO_METHOD_USERPTR:
  218. if (!(cap.capabilities & V4L2_CAP_STREAMING)) {
  219. fprintf(stderr, "%s does not support streaming i/o\n",
  220. *dev_name);
  221. exit(EXIT_FAILURE);
  222. }
  223. break;
  224. }
  225. /* Select video input, video standard and tune here. */
  226. struct v4l2_cropcap cropcap = {
  227. .type = V4L2_BUF_TYPE_VIDEO_CAPTURE,
  228. };
  229. struct v4l2_crop crop = {0};
  230. if (xioctl(fd, VIDIOC_CROPCAP, &cropcap) == 0) {
  231. crop.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
  232. crop.c = cropcap.defrect; /* reset to default */
  233. if (xioctl(fd, VIDIOC_S_CROP, &crop) == -1) {
  234. switch (errno) {
  235. case EINVAL:
  236. /* Cropping not supported. */
  237. break;
  238. default:
  239. /* Errors ignored. */
  240. break;
  241. }
  242. }
  243. } else {
  244. /* Errors ignored. */
  245. }
  246. struct v4l2_format fmt = {
  247. .type = V4L2_BUF_TYPE_VIDEO_CAPTURE,
  248. };
  249. if (current_width > 0) {
  250. g_printerr("Setting camera to %dx%d fmt %d\n",
  251. current_width, current_height, current_fmt);
  252. fmt.fmt.pix.width = current_width;
  253. fmt.fmt.pix.height = current_height;
  254. fmt.fmt.pix.pixelformat = current_fmt;
  255. fmt.fmt.pix.field = V4L2_FIELD_ANY;
  256. if (xioctl(fd, VIDIOC_S_FMT, &fmt) == -1) {
  257. errno_exit("VIDIOC_S_FMT");
  258. }
  259. g_printerr("Driver returned %dx%d fmt %d\n",
  260. fmt.fmt.pix.width, fmt.fmt.pix.height,
  261. fmt.fmt.pix.pixelformat);
  262. /* Note VIDIOC_S_FMT may change width and height. */
  263. } else {
  264. g_printerr("Querying camera format\n");
  265. /* Preserve original settings as set by v4l2-ctl for example */
  266. if (xioctl(fd, VIDIOC_G_FMT, &fmt) == -1) {
  267. errno_exit("VIDIOC_G_FMT");
  268. }
  269. g_printerr("Driver returned %dx%d fmt %d\n",
  270. fmt.fmt.pix.width, fmt.fmt.pix.height,
  271. fmt.fmt.pix.pixelformat);
  272. current_width = fmt.fmt.pix.width;
  273. current_height = fmt.fmt.pix.height;
  274. }
  275. current_fmt = fmt.fmt.pix.pixelformat;
  276. /* Buggy driver paranoia. */
  277. unsigned int min = fmt.fmt.pix.width * 2;
  278. if (fmt.fmt.pix.bytesperline < min) {
  279. fmt.fmt.pix.bytesperline = min;
  280. }
  281. min = fmt.fmt.pix.bytesperline * fmt.fmt.pix.height;
  282. if (fmt.fmt.pix.sizeimage < min) {
  283. fmt.fmt.pix.sizeimage = min;
  284. }
  285. switch (io) {
  286. case IO_METHOD_READ:
  287. //init_read(fmt.fmt.pix.sizeimage);
  288. break;
  289. case IO_METHOD_MMAP:
  290. init_mmap(fd);
  291. break;
  292. case IO_METHOD_USERPTR:
  293. //init_userp(fmt.fmt.pix.sizeimage);
  294. break;
  295. }
  296. }
  297. static void
  298. process_image(const int *p, int size)
  299. {
  300. clock_t t;
  301. time_t rawtime;
  302. uint8_t *pixels;
  303. double time_taken;
  304. char fname[255];
  305. GdkPixbuf *pixbuf;
  306. GdkPixbuf *pixbufrot;
  307. GError *error = NULL;
  308. double scale;
  309. cairo_t *cr;
  310. t = clock();
  311. dc1394bayer_method_t method = DC1394_BAYER_METHOD_DOWNSAMPLE;
  312. dc1394color_filter_t filter = DC1394_COLOR_FILTER_BGGR;
  313. if(capture){
  314. method = DC1394_BAYER_METHOD_SIMPLE;
  315. // method = DC1394_BAYER_METHOD_VNG is slightly sharper but takes 10 seconds;
  316. pixbuf = gdk_pixbuf_new(GDK_COLORSPACE_RGB, FALSE, 8, current_width, current_height);
  317. }else{
  318. pixbuf = gdk_pixbuf_new(GDK_COLORSPACE_RGB, FALSE, 8, current_width/2, current_height/2);
  319. }
  320. pixels = gdk_pixbuf_get_pixels(pixbuf);
  321. dc1394_bayer_decoding_8bit((const uint8_t*)p, pixels, current_width, current_height, filter, method);
  322. if (current_rotate == 0) {
  323. pixbufrot = pixbuf;
  324. } else if (current_rotate == 90) {
  325. pixbufrot = gdk_pixbuf_rotate_simple(pixbuf, GDK_PIXBUF_ROTATE_COUNTERCLOCKWISE);
  326. } else if (current_rotate == 180) {
  327. pixbufrot = gdk_pixbuf_rotate_simple(pixbuf, GDK_PIXBUF_ROTATE_UPSIDEDOWN);
  328. } else if (current_rotate == 270) {
  329. pixbufrot = gdk_pixbuf_rotate_simple(pixbuf, GDK_PIXBUF_ROTATE_CLOCKWISE);
  330. }
  331. if (capture){
  332. time(&rawtime);
  333. sprintf(fname, "%s/Pictures/Photo-%s.jpg", getenv("HOME"), ctime(&rawtime));
  334. printf("Saving image\n");
  335. gdk_pixbuf_save(pixbufrot, fname, "jpeg", &error, "quality", "85", NULL);
  336. if(error != NULL) {
  337. g_printerr(error->message);
  338. g_clear_error(&error);
  339. }
  340. } else {
  341. scale = (double)preview_width/gdk_pixbuf_get_width(pixbufrot);
  342. cr = cairo_create(surface);
  343. cairo_set_source_rgb(cr, 0,0,0);
  344. cairo_paint(cr);
  345. gdk_cairo_set_source_pixbuf(cr, pixbufrot, 0, 0);
  346. cairo_scale(cr, scale, scale);
  347. cairo_pattern_set_extend(cairo_get_source(cr), CAIRO_EXTEND_NONE);
  348. cairo_paint(cr);
  349. gtk_widget_queue_draw_area(preview, 0, 0, preview_width, preview_height);
  350. }
  351. capture = 0;
  352. t = clock() - t;
  353. time_taken = ((double)t)/CLOCKS_PER_SEC;
  354. printf("%f fps\n", 1.0/time_taken);
  355. }
  356. static gboolean
  357. preview_draw (GtkWidget *widget, cairo_t *cr, gpointer data)
  358. {
  359. cairo_set_source_surface(cr, surface, 0,0);
  360. cairo_paint(cr);
  361. return FALSE;
  362. }
  363. static gboolean
  364. preview_configure (GtkWidget *widget, GdkEventConfigure *event)
  365. {
  366. cairo_t *cr;
  367. if (surface)
  368. cairo_surface_destroy(surface);
  369. surface = gdk_window_create_similar_surface (gtk_widget_get_window(widget),
  370. CAIRO_CONTENT_COLOR,
  371. gtk_widget_get_allocated_width (widget),
  372. gtk_widget_get_allocated_height (widget));
  373. preview_width = gtk_widget_get_allocated_width(widget);
  374. preview_height = gtk_widget_get_allocated_height(widget);
  375. cr = cairo_create(surface);
  376. cairo_set_source_rgb(cr, 0, 0, 0);
  377. cairo_paint(cr);
  378. cairo_destroy(cr);
  379. return TRUE;
  380. }
  381. static int
  382. read_frame(int fd)
  383. {
  384. struct v4l2_buffer buf = {0};
  385. switch (io) {
  386. case IO_METHOD_READ:
  387. if (read(fd, buffers[0].start, buffers[0].length) == -1) {
  388. switch (errno) {
  389. case EAGAIN:
  390. return 0;
  391. case EIO:
  392. /* Could ignore EIO, see spec. */
  393. /* fallthrough */
  394. default:
  395. errno_exit("read");
  396. break;
  397. }
  398. }
  399. process_image(buffers[0].start, buffers[0].length);
  400. break;
  401. case IO_METHOD_MMAP:
  402. buf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
  403. buf.memory = V4L2_MEMORY_MMAP;
  404. if (xioctl(fd, VIDIOC_DQBUF, &buf) == -1) {
  405. switch (errno) {
  406. case EAGAIN:
  407. return 0;
  408. case EIO:
  409. /* Could ignore EIO, see spec. */
  410. /* fallthrough */
  411. default:
  412. errno_exit("VIDIOC_DQBUF");
  413. break;
  414. }
  415. }
  416. //assert(buf.index < n_buffers);
  417. process_image(buffers[buf.index].start, buf.bytesused);
  418. if (xioctl(fd, VIDIOC_QBUF, &buf) == -1) {
  419. errno_exit("VIDIOC_QBUF");
  420. }
  421. break;
  422. case IO_METHOD_USERPTR:
  423. buf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
  424. buf.memory = V4L2_MEMORY_USERPTR;
  425. if (xioctl(fd, VIDIOC_DQBUF, &buf) == -1) {
  426. switch (errno) {
  427. case EAGAIN:
  428. return 0;
  429. case EIO:
  430. /* Could ignore EIO, see spec. */
  431. /* fallthrough */
  432. default:
  433. errno_exit("VIDIOC_DQBUF");
  434. break;
  435. }
  436. }
  437. unsigned int i;
  438. for (i = 0; i < n_buffers; ++i) {
  439. if (buf.m.userptr == (unsigned long) buffers[i].start
  440. && buf.length == buffers[i].length) {
  441. break;
  442. }
  443. }
  444. //assert(i < n_buffers);
  445. process_image((void *) buf.m.userptr, buf.bytesused);
  446. if (xioctl(fd, VIDIOC_QBUF, &buf) == -1) {
  447. errno_exit("VIDIOC_QBUF");
  448. }
  449. break;
  450. }
  451. return 1;
  452. }
  453. static gboolean
  454. get_frame(int fd)
  455. {
  456. while (1) {
  457. fd_set fds;
  458. struct timeval tv;
  459. int r;
  460. FD_ZERO(&fds);
  461. FD_SET(fd, &fds);
  462. /* Timeout. */
  463. tv.tv_sec = 2;
  464. tv.tv_usec = 0;
  465. r = select(fd + 1, &fds, NULL, NULL, &tv);
  466. if (r == -1) {
  467. if (EINTR == errno) {
  468. continue;
  469. }
  470. errno_exit("select");
  471. } else if (r == 0) {
  472. fprintf(stderr, "select timeout\\n");
  473. exit(EXIT_FAILURE);
  474. }
  475. if (read_frame(fd)) {
  476. break;
  477. }
  478. /* EAGAIN - continue select loop. */
  479. }
  480. return TRUE;
  481. }
  482. static int
  483. config_ini_handler(void *user, const char *section, const char *name,
  484. const char *value)
  485. {
  486. if (strcmp(section, "rear") == 0) {
  487. if (strcmp(name, "width") == 0) {
  488. rear_width = strtol(value, NULL, 10);
  489. } else if (strcmp(name, "height") == 0) {
  490. rear_height = strtol(value, NULL, 10);
  491. } else if (strcmp(name, "rotate") == 0) {
  492. rear_rotate = strtol(value, NULL, 10);
  493. } else if (strcmp(name, "fmt") == 0) {
  494. if (strcmp(value, "RGB") == 0) {
  495. rear_fmt = V4L2_PIX_FMT_RGB24;
  496. } else if (strcmp(value, "UYVY") == 0) {
  497. rear_fmt = V4L2_PIX_FMT_UYVY;
  498. } else if (strcmp(value, "YUYV") == 0) {
  499. rear_fmt = V4L2_PIX_FMT_YUYV;
  500. } else if (strcmp(value, "JPEG") == 0) {
  501. rear_fmt = V4L2_PIX_FMT_JPEG;
  502. } else if (strcmp(value, "NV12") == 0) {
  503. rear_fmt = V4L2_PIX_FMT_NV12;
  504. } else if (strcmp(value, "YUV420") == 0
  505. || strcmp(value, "I420") == 0
  506. || strcmp(value, "YU12") == 0) {
  507. rear_fmt = V4L2_PIX_FMT_YUV420;
  508. } else if (strcmp(value, "YVU420") == 0
  509. || strcmp(value, "YV12") == 0) {
  510. rear_fmt = V4L2_PIX_FMT_YVU420;
  511. } else if (strcmp(value, "RGGB8") == 0) {
  512. rear_fmt = V4L2_PIX_FMT_SRGGB8;
  513. } else if (strcmp(value, "BGGR8") == 0) {
  514. rear_fmt = V4L2_PIX_FMT_SBGGR8;
  515. rear_mbus = MEDIA_BUS_FMT_SBGGR8_1X8;
  516. } else if (strcmp(value, "GRBG8") == 0) {
  517. rear_fmt = V4L2_PIX_FMT_SGRBG8;
  518. } else if (strcmp(value, "GBRG8") == 0) {
  519. rear_fmt = V4L2_PIX_FMT_SGBRG8;
  520. } else {
  521. g_printerr("Unsupported pixelformat %s\n", value);
  522. exit(1);
  523. }
  524. } else if (strcmp(name, "driver") == 0){
  525. rear_dev_name = strdup(value);
  526. } else {
  527. g_printerr("Unknown key '%s' in [rear]\n", name);
  528. exit(1);
  529. }
  530. } else if (strcmp(section, "front") == 0) {
  531. if (strcmp(name, "width") == 0) {
  532. front_width = strtol(value, NULL, 10);
  533. } else if (strcmp(name, "height") == 0) {
  534. front_height = strtol(value, NULL, 10);
  535. } else if (strcmp(name, "rotate") == 0) {
  536. front_rotate = strtol(value, NULL, 10);
  537. } else if (strcmp(name, "fmt") == 0) {
  538. if (strcmp(value, "RGB") == 0) {
  539. front_fmt = V4L2_PIX_FMT_RGB24;
  540. } else if (strcmp(value, "UYVY") == 0) {
  541. front_fmt = V4L2_PIX_FMT_UYVY;
  542. } else if (strcmp(value, "YUYV") == 0) {
  543. front_fmt = V4L2_PIX_FMT_YUYV;
  544. } else if (strcmp(value, "JPEG") == 0) {
  545. front_fmt = V4L2_PIX_FMT_JPEG;
  546. } else if (strcmp(value, "NV12") == 0) {
  547. front_fmt = V4L2_PIX_FMT_NV12;
  548. } else if (strcmp(value, "YUV420") == 0
  549. || strcmp(value, "I420") == 0
  550. || strcmp(value, "YU12") == 0) {
  551. front_fmt = V4L2_PIX_FMT_YUV420;
  552. } else if (strcmp(value, "YVU420") == 0
  553. || strcmp(value, "YV12") == 0) {
  554. front_fmt = V4L2_PIX_FMT_YVU420;
  555. } else if (strcmp(value, "RGGB8") == 0) {
  556. front_fmt = V4L2_PIX_FMT_SRGGB8;
  557. } else if (strcmp(value, "BGGR8") == 0) {
  558. front_fmt = V4L2_PIX_FMT_SBGGR8;
  559. front_mbus = MEDIA_BUS_FMT_SBGGR8_1X8;
  560. } else if (strcmp(value, "GRBG8") == 0) {
  561. front_fmt = V4L2_PIX_FMT_SGRBG8;
  562. } else if (strcmp(value, "GBRG8") == 0) {
  563. front_fmt = V4L2_PIX_FMT_SGBRG8;
  564. } else {
  565. g_printerr("Unsupported pixelformat %s\n", value);
  566. exit(1);
  567. }
  568. } else if (strcmp(name, "driver") == 0){
  569. front_dev_name = strdup(value);
  570. } else {
  571. g_printerr("Unknown key '%s' in [front]\n", name);
  572. exit(1);
  573. }
  574. } else if (strcmp(section, "device") == 0) {
  575. if (strcmp(name, "csi") == 0) {
  576. media_drv_name = strdup(value);
  577. } else {
  578. g_printerr("Unknown key '%s' in [device]\n", name);
  579. exit(1);
  580. }
  581. } else {
  582. g_printerr("Unknown section '%s' in config file\n", section);
  583. exit(1);
  584. }
  585. return 1;
  586. }
  587. int
  588. find_dev_node(int maj, int min, char* fnbuf)
  589. {
  590. DIR *d;
  591. struct dirent *dir;
  592. struct stat info;
  593. d = opendir("/dev");
  594. while ((dir = readdir(d)) != NULL) {
  595. sprintf(fnbuf, "/dev/%s", dir->d_name);
  596. stat(fnbuf, &info);
  597. if (!S_ISCHR(info.st_mode)){
  598. continue;
  599. }
  600. if (major(info.st_rdev) == maj && minor(info.st_rdev) == min) {
  601. return 0;
  602. }
  603. }
  604. return -1;
  605. }
  606. int
  607. setup_rear()
  608. {
  609. struct media_link_desc link = {0};
  610. // Disable the interface<->front link
  611. link.flags = 0;
  612. link.source.entity = front_entity_id;
  613. link.source.index = 0;
  614. link.sink.entity = interface_entity_id;
  615. link.sink.index = 0;
  616. if(xioctl(media_fd, MEDIA_IOC_SETUP_LINK, &link) < 0){
  617. g_printerr("Could not disable front camera link\n");
  618. return -1;
  619. }
  620. // Enable the interface<->rear link
  621. link.flags = MEDIA_LNK_FL_ENABLED;
  622. link.source.entity = rear_entity_id;
  623. link.source.index = 0;
  624. link.sink.entity = interface_entity_id;
  625. link.sink.index = 0;
  626. if(xioctl(media_fd, MEDIA_IOC_SETUP_LINK, &link) < 0){
  627. g_printerr("Could not enable rear camera link\n");
  628. return -1;
  629. }
  630. current_width = rear_width;
  631. current_height = rear_height;
  632. current_fmt = rear_fmt;
  633. current_rotate = rear_rotate;
  634. // Find camera node
  635. init_sensor(rear_dev, rear_width, rear_height, rear_mbus);
  636. return 0;
  637. }
  638. int
  639. setup_front()
  640. {
  641. struct media_link_desc link = {0};
  642. // Disable the interface<->rear link
  643. link.flags = 0;
  644. link.source.entity = rear_entity_id;
  645. link.source.index = 0;
  646. link.sink.entity = interface_entity_id;
  647. link.sink.index = 0;
  648. if(xioctl(media_fd, MEDIA_IOC_SETUP_LINK, &link) < 0){
  649. g_printerr("Could not disable front camera link\n");
  650. return -1;
  651. }
  652. // Enable the interface<->rear link
  653. link.flags = MEDIA_LNK_FL_ENABLED;
  654. link.source.entity = front_entity_id;
  655. link.source.index = 0;
  656. link.sink.entity = interface_entity_id;
  657. link.sink.index = 0;
  658. if(xioctl(media_fd, MEDIA_IOC_SETUP_LINK, &link) < 0){
  659. g_printerr("Could not enable rear camera link\n");
  660. return -1;
  661. }
  662. current_width = front_width;
  663. current_height = front_height;
  664. current_fmt = front_fmt;
  665. current_rotate = front_rotate;
  666. // Find camera node
  667. init_sensor(front_dev, front_width, front_height, front_mbus);
  668. return 0;
  669. }
  670. int
  671. find_cameras()
  672. {
  673. struct media_entity_desc entity = {0};
  674. int ret;
  675. int found = 0;
  676. while (1) {
  677. entity.id = entity.id | MEDIA_ENT_ID_FLAG_NEXT;
  678. ret = xioctl(media_fd, MEDIA_IOC_ENUM_ENTITIES, &entity);
  679. if (ret < 0){
  680. break;
  681. }
  682. printf("At node %s, (0x%x)\n", entity.name, entity.type);
  683. if(strncmp(entity.name, front_dev_name, strlen(front_dev_name)) == 0) {
  684. front_entity_id = entity.id;
  685. find_dev_node(entity.dev.major, entity.dev.minor, front_dev);
  686. printf("Found front cam, is %s at %s\n", entity.name, front_dev);
  687. found++;
  688. }
  689. if(strncmp(entity.name, rear_dev_name, strlen(rear_dev_name)) == 0) {
  690. rear_entity_id = entity.id;
  691. find_dev_node(entity.dev.major, entity.dev.minor, rear_dev);
  692. printf("Found rear cam, is %s at %s\n", entity.name, rear_dev);
  693. found++;
  694. }
  695. if (entity.type == MEDIA_ENT_F_IO_V4L) {
  696. interface_entity_id = entity.id;
  697. find_dev_node(entity.dev.major, entity.dev.minor, dev_name);
  698. printf("Found v4l2 interface node at %s\n", dev_name);
  699. }
  700. }
  701. if(found < 2){
  702. return -1;
  703. }
  704. return 0;
  705. }
  706. int
  707. find_media_fd()
  708. {
  709. DIR *d;
  710. struct dirent *dir;
  711. int fd;
  712. char fnbuf[20];
  713. struct media_device_info mdi = {0};
  714. d = opendir("/dev");
  715. while ((dir = readdir(d)) != NULL) {
  716. if(strncmp(dir->d_name, "media", 5) == 0) {
  717. sprintf(fnbuf, "/dev/%s", dir->d_name);
  718. printf("Checking %s\n", fnbuf);
  719. fd = open(fnbuf, O_RDWR);
  720. xioctl(fd, MEDIA_IOC_DEVICE_INFO, &mdi);
  721. printf("Found media device: %s\n", mdi.driver);
  722. if (strcmp(mdi.driver, media_drv_name) == 0){
  723. media_fd = fd;
  724. return 0;
  725. }
  726. close(fd);
  727. }
  728. }
  729. return 1;
  730. }
  731. void
  732. on_shutter_clicked(GtkWidget *widget, gpointer user_data)
  733. {
  734. capture = 1;
  735. }
  736. int
  737. main(int argc, char *argv[])
  738. {
  739. if (argc != 2) {
  740. g_printerr("Usage: camera configfile\n");
  741. return 1;
  742. }
  743. GError *error = NULL;
  744. gtk_init(&argc, &argv);
  745. g_object_set(gtk_settings_get_default(), "gtk-application-prefer-dark-theme", TRUE, NULL);
  746. GtkBuilder *builder = gtk_builder_new();
  747. char *glade_file = "/usr/share/camera/ui/camera.glade";
  748. if (access("camera.glade", F_OK) != -1) {
  749. glade_file = "camera.glade";
  750. }
  751. if (gtk_builder_add_from_file(builder, glade_file, &error) == 0) {
  752. g_printerr("Error loading file: %s\n", error->message);
  753. g_clear_error(&error);
  754. return 1;
  755. }
  756. GObject *window = gtk_builder_get_object(builder, "window");
  757. GObject *preview_box = gtk_builder_get_object(builder, "preview_box");
  758. GObject *shutter = gtk_builder_get_object(builder, "shutter");
  759. GObject *settings_btn = gtk_builder_get_object(builder, "settings");
  760. preview = gtk_builder_get_object(builder, "preview");
  761. g_signal_connect(window, "destroy", G_CALLBACK(gtk_main_quit), NULL);
  762. g_signal_connect(shutter, "clicked", G_CALLBACK(on_shutter_clicked), NULL);
  763. g_signal_connect(preview, "draw", G_CALLBACK(preview_draw), NULL);
  764. g_signal_connect(preview, "configure-event", G_CALLBACK(preview_configure), NULL);
  765. GtkCssProvider *provider = gtk_css_provider_new();
  766. if (access("camera.css", F_OK) != -1) {
  767. gtk_css_provider_load_from_path(provider, "camera.css", NULL);
  768. } else {
  769. gtk_css_provider_load_from_path(provider, "/usr/share/camera/ui/camera.css", NULL);
  770. }
  771. GtkStyleContext *context = gtk_widget_get_style_context(preview_box);
  772. gtk_style_context_add_provider(context,
  773. GTK_STYLE_PROVIDER(provider),
  774. GTK_STYLE_PROVIDER_PRIORITY_USER);
  775. int result = ini_parse(argv[1], config_ini_handler, NULL);
  776. if (result == -1) {
  777. g_printerr("Config file not found\n");
  778. return 1;
  779. } else if (result == -2) {
  780. g_printerr("Could not allocate memory to parse config file\n");
  781. return 1;
  782. } else if (result != 0) {
  783. g_printerr("Could not parse config file\n");
  784. return 1;
  785. }
  786. if (find_media_fd() == -1) {
  787. g_printerr("Could not find the media node\n");
  788. return 1;
  789. }
  790. if (find_cameras() == -1) {
  791. g_printerr("Could not find the cameras\n");
  792. return 1;
  793. }
  794. setup_rear();
  795. int fd = open(dev_name, O_RDWR);
  796. if (fd == -1) {
  797. g_printerr("Error opening video device: %s\n", dev_name);
  798. return 1;
  799. }
  800. init_device(fd);
  801. start_capturing(fd);
  802. // Get a new frame every 34ms ~30fps
  803. printf("window show\n");
  804. gtk_widget_show(window);
  805. g_idle_add(get_frame, fd);
  806. gtk_main();
  807. return 0;
  808. }