main.c 23 KB

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