main.c 23 KB

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