main.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887
  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. enum io_method {
  19. IO_METHOD_READ,
  20. IO_METHOD_MMAP,
  21. IO_METHOD_USERPTR,
  22. };
  23. struct buffer {
  24. void *start;
  25. size_t length;
  26. };
  27. struct buffer *buffers;
  28. static unsigned int n_buffers;
  29. // Rear camera
  30. static char *rear_dev_name;
  31. static unsigned int rear_entity_id;
  32. static char rear_dev[260];
  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 unsigned int front_entity_id;
  41. static char front_dev[260];
  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 unsigned int interface_entity_id;
  50. static char dev_name[260];
  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. GtkWidget *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. if (!(cap.capabilities & V4L2_CAP_STREAMING)) {
  218. fprintf(stderr, "%s does not support streaming i/o\n",
  219. dev_name);
  220. exit(EXIT_FAILURE);
  221. }
  222. /* Select video input, video standard and tune here. */
  223. struct v4l2_cropcap cropcap = {
  224. .type = V4L2_BUF_TYPE_VIDEO_CAPTURE,
  225. };
  226. struct v4l2_crop crop = {0};
  227. if (xioctl(fd, VIDIOC_CROPCAP, &cropcap) == 0) {
  228. crop.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
  229. crop.c = cropcap.defrect; /* reset to default */
  230. if (xioctl(fd, VIDIOC_S_CROP, &crop) == -1) {
  231. switch (errno) {
  232. case EINVAL:
  233. /* Cropping not supported. */
  234. break;
  235. default:
  236. /* Errors ignored. */
  237. break;
  238. }
  239. }
  240. } else {
  241. /* Errors ignored. */
  242. }
  243. struct v4l2_format fmt = {
  244. .type = V4L2_BUF_TYPE_VIDEO_CAPTURE,
  245. };
  246. if (current_width > 0) {
  247. g_printerr("Setting camera to %dx%d fmt %d\n",
  248. current_width, current_height, current_fmt);
  249. fmt.fmt.pix.width = current_width;
  250. fmt.fmt.pix.height = current_height;
  251. fmt.fmt.pix.pixelformat = current_fmt;
  252. fmt.fmt.pix.field = V4L2_FIELD_ANY;
  253. if (xioctl(fd, VIDIOC_S_FMT, &fmt) == -1) {
  254. errno_exit("VIDIOC_S_FMT");
  255. }
  256. g_printerr("Driver returned %dx%d fmt %d\n",
  257. fmt.fmt.pix.width, fmt.fmt.pix.height,
  258. fmt.fmt.pix.pixelformat);
  259. /* Note VIDIOC_S_FMT may change width and height. */
  260. } else {
  261. g_printerr("Querying camera format\n");
  262. /* Preserve original settings as set by v4l2-ctl for example */
  263. if (xioctl(fd, VIDIOC_G_FMT, &fmt) == -1) {
  264. errno_exit("VIDIOC_G_FMT");
  265. }
  266. g_printerr("Driver returned %dx%d fmt %d\n",
  267. fmt.fmt.pix.width, fmt.fmt.pix.height,
  268. fmt.fmt.pix.pixelformat);
  269. current_width = fmt.fmt.pix.width;
  270. current_height = fmt.fmt.pix.height;
  271. }
  272. current_fmt = fmt.fmt.pix.pixelformat;
  273. /* Buggy driver paranoia. */
  274. unsigned int min = fmt.fmt.pix.width * 2;
  275. if (fmt.fmt.pix.bytesperline < min) {
  276. fmt.fmt.pix.bytesperline = min;
  277. }
  278. min = fmt.fmt.pix.bytesperline * fmt.fmt.pix.height;
  279. if (fmt.fmt.pix.sizeimage < min) {
  280. fmt.fmt.pix.sizeimage = min;
  281. }
  282. init_mmap(fd);
  283. }
  284. static void
  285. process_image(const int *p, int size)
  286. {
  287. clock_t t;
  288. time_t rawtime;
  289. struct tm tim;
  290. uint8_t *pixels;
  291. double time_taken;
  292. char fname[255];
  293. char timestamp[30];
  294. GdkPixbuf *pixbuf;
  295. GdkPixbuf *pixbufrot;
  296. GError *error = NULL;
  297. double scale;
  298. cairo_t *cr;
  299. t = clock();
  300. dc1394bayer_method_t method = DC1394_BAYER_METHOD_DOWNSAMPLE;
  301. dc1394color_filter_t filter = DC1394_COLOR_FILTER_BGGR;
  302. if (capture) {
  303. method = DC1394_BAYER_METHOD_SIMPLE;
  304. // method = DC1394_BAYER_METHOD_VNG is slightly sharper but takes 10 seconds;
  305. pixbuf = gdk_pixbuf_new(GDK_COLORSPACE_RGB, FALSE, 8, current_width, current_height);
  306. } else {
  307. pixbuf = gdk_pixbuf_new(GDK_COLORSPACE_RGB, FALSE, 8, current_width / 2, current_height / 2);
  308. }
  309. pixels = gdk_pixbuf_get_pixels(pixbuf);
  310. dc1394_bayer_decoding_8bit((const uint8_t *) p, pixels, current_width, current_height, filter, method);
  311. if (current_rotate == 0) {
  312. pixbufrot = pixbuf;
  313. } else if (current_rotate == 90) {
  314. pixbufrot = gdk_pixbuf_rotate_simple(pixbuf, GDK_PIXBUF_ROTATE_COUNTERCLOCKWISE);
  315. } else if (current_rotate == 180) {
  316. pixbufrot = gdk_pixbuf_rotate_simple(pixbuf, GDK_PIXBUF_ROTATE_UPSIDEDOWN);
  317. } else if (current_rotate == 270) {
  318. pixbufrot = gdk_pixbuf_rotate_simple(pixbuf, GDK_PIXBUF_ROTATE_CLOCKWISE);
  319. }
  320. if (capture) {
  321. time(&rawtime);
  322. tim = *(localtime(&rawtime));
  323. strftime(timestamp, 30, "%F %T", &tim);
  324. sprintf(fname, "%s/Pictures/Photo-%s.jpg", getenv("HOME"), timestamp);
  325. printf("Saving image\n");
  326. gdk_pixbuf_save(pixbufrot, fname, "jpeg", &error, "quality", "85", NULL);
  327. if (error != NULL) {
  328. g_printerr(error->message);
  329. g_clear_error(&error);
  330. }
  331. } else {
  332. scale = (double) preview_width / gdk_pixbuf_get_width(pixbufrot);
  333. cr = cairo_create(surface);
  334. cairo_set_source_rgb(cr, 0, 0, 0);
  335. cairo_paint(cr);
  336. cairo_scale(cr, scale, scale);
  337. gdk_cairo_set_source_pixbuf(cr, pixbufrot, 0, 0);
  338. cairo_pattern_set_extend(cairo_get_source(cr), CAIRO_EXTEND_NONE);
  339. cairo_paint(cr);
  340. gtk_widget_queue_draw_area(preview, 0, 0, preview_width, preview_height);
  341. }
  342. capture = 0;
  343. t = clock() - t;
  344. time_taken = ((double) t) / CLOCKS_PER_SEC;
  345. printf("%f fps\n", 1.0 / time_taken);
  346. }
  347. static gboolean
  348. preview_draw(GtkWidget *widget, cairo_t *cr, gpointer data)
  349. {
  350. cairo_set_source_surface(cr, surface, 0, 0);
  351. cairo_paint(cr);
  352. return FALSE;
  353. }
  354. static gboolean
  355. preview_configure(GtkWidget *widget, GdkEventConfigure *event)
  356. {
  357. cairo_t *cr;
  358. if (surface)
  359. cairo_surface_destroy(surface);
  360. surface = gdk_window_create_similar_surface(gtk_widget_get_window(widget),
  361. CAIRO_CONTENT_COLOR,
  362. gtk_widget_get_allocated_width(widget),
  363. gtk_widget_get_allocated_height(widget));
  364. preview_width = gtk_widget_get_allocated_width(widget);
  365. preview_height = gtk_widget_get_allocated_height(widget);
  366. cr = cairo_create(surface);
  367. cairo_set_source_rgb(cr, 0, 0, 0);
  368. cairo_paint(cr);
  369. cairo_destroy(cr);
  370. return TRUE;
  371. }
  372. static int
  373. read_frame(int fd)
  374. {
  375. struct v4l2_buffer buf = {0};
  376. buf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
  377. buf.memory = V4L2_MEMORY_MMAP;
  378. if (xioctl(fd, VIDIOC_DQBUF, &buf) == -1) {
  379. switch (errno) {
  380. case EAGAIN:
  381. return 0;
  382. case EIO:
  383. /* Could ignore EIO, see spec. */
  384. /* fallthrough */
  385. default:
  386. errno_exit("VIDIOC_DQBUF");
  387. break;
  388. }
  389. }
  390. //assert(buf.index < n_buffers);
  391. process_image(buffers[buf.index].start, buf.bytesused);
  392. if (xioctl(fd, VIDIOC_QBUF, &buf) == -1) {
  393. errno_exit("VIDIOC_QBUF");
  394. }
  395. return 1;
  396. }
  397. gboolean
  398. get_frame()
  399. {
  400. if (ready == 0)
  401. return TRUE;
  402. while (1) {
  403. fd_set fds;
  404. struct timeval tv;
  405. int r;
  406. FD_ZERO(&fds);
  407. FD_SET(video_fd, &fds);
  408. /* Timeout. */
  409. tv.tv_sec = 2;
  410. tv.tv_usec = 0;
  411. r = select(video_fd + 1, &fds, NULL, NULL, &tv);
  412. if (r == -1) {
  413. if (EINTR == errno) {
  414. continue;
  415. }
  416. errno_exit("select");
  417. } else if (r == 0) {
  418. fprintf(stderr, "select timeout\\n");
  419. exit(EXIT_FAILURE);
  420. }
  421. if (read_frame(video_fd)) {
  422. break;
  423. }
  424. /* EAGAIN - continue select loop. */
  425. }
  426. return TRUE;
  427. }
  428. int
  429. strtoint(const char *nptr, char **endptr, int base)
  430. {
  431. long x = strtol(nptr, endptr, base);
  432. assert(x <= INT_MAX);
  433. return (int) x;
  434. }
  435. static int
  436. config_ini_handler(void *user, const char *section, const char *name,
  437. const char *value)
  438. {
  439. if (strcmp(section, "rear") == 0) {
  440. if (strcmp(name, "width") == 0) {
  441. rear_width = strtoint(value, NULL, 10);
  442. } else if (strcmp(name, "height") == 0) {
  443. rear_height = strtoint(value, NULL, 10);
  444. } else if (strcmp(name, "rotate") == 0) {
  445. rear_rotate = strtoint(value, NULL, 10);
  446. } else if (strcmp(name, "fmt") == 0) {
  447. if (strcmp(value, "RGB") == 0) {
  448. rear_fmt = V4L2_PIX_FMT_RGB24;
  449. } else if (strcmp(value, "UYVY") == 0) {
  450. rear_fmt = V4L2_PIX_FMT_UYVY;
  451. } else if (strcmp(value, "YUYV") == 0) {
  452. rear_fmt = V4L2_PIX_FMT_YUYV;
  453. } else if (strcmp(value, "JPEG") == 0) {
  454. rear_fmt = V4L2_PIX_FMT_JPEG;
  455. } else if (strcmp(value, "NV12") == 0) {
  456. rear_fmt = V4L2_PIX_FMT_NV12;
  457. } else if (strcmp(value, "YUV420") == 0
  458. || strcmp(value, "I420") == 0
  459. || strcmp(value, "YU12") == 0) {
  460. rear_fmt = V4L2_PIX_FMT_YUV420;
  461. } else if (strcmp(value, "YVU420") == 0
  462. || strcmp(value, "YV12") == 0) {
  463. rear_fmt = V4L2_PIX_FMT_YVU420;
  464. } else if (strcmp(value, "RGGB8") == 0) {
  465. rear_fmt = V4L2_PIX_FMT_SRGGB8;
  466. } else if (strcmp(value, "BGGR8") == 0) {
  467. rear_fmt = V4L2_PIX_FMT_SBGGR8;
  468. rear_mbus = MEDIA_BUS_FMT_SBGGR8_1X8;
  469. } else if (strcmp(value, "GRBG8") == 0) {
  470. rear_fmt = V4L2_PIX_FMT_SGRBG8;
  471. } else if (strcmp(value, "GBRG8") == 0) {
  472. rear_fmt = V4L2_PIX_FMT_SGBRG8;
  473. } else {
  474. g_printerr("Unsupported pixelformat %s\n", value);
  475. exit(1);
  476. }
  477. } else if (strcmp(name, "driver") == 0) {
  478. rear_dev_name = strdup(value);
  479. } else {
  480. g_printerr("Unknown key '%s' in [rear]\n", name);
  481. exit(1);
  482. }
  483. } else if (strcmp(section, "front") == 0) {
  484. if (strcmp(name, "width") == 0) {
  485. front_width = strtoint(value, NULL, 10);
  486. } else if (strcmp(name, "height") == 0) {
  487. front_height = strtoint(value, NULL, 10);
  488. } else if (strcmp(name, "rotate") == 0) {
  489. front_rotate = strtoint(value, NULL, 10);
  490. } else if (strcmp(name, "fmt") == 0) {
  491. if (strcmp(value, "RGB") == 0) {
  492. front_fmt = V4L2_PIX_FMT_RGB24;
  493. } else if (strcmp(value, "UYVY") == 0) {
  494. front_fmt = V4L2_PIX_FMT_UYVY;
  495. } else if (strcmp(value, "YUYV") == 0) {
  496. front_fmt = V4L2_PIX_FMT_YUYV;
  497. } else if (strcmp(value, "JPEG") == 0) {
  498. front_fmt = V4L2_PIX_FMT_JPEG;
  499. } else if (strcmp(value, "NV12") == 0) {
  500. front_fmt = V4L2_PIX_FMT_NV12;
  501. } else if (strcmp(value, "YUV420") == 0
  502. || strcmp(value, "I420") == 0
  503. || strcmp(value, "YU12") == 0) {
  504. front_fmt = V4L2_PIX_FMT_YUV420;
  505. } else if (strcmp(value, "YVU420") == 0
  506. || strcmp(value, "YV12") == 0) {
  507. front_fmt = V4L2_PIX_FMT_YVU420;
  508. } else if (strcmp(value, "RGGB8") == 0) {
  509. front_fmt = V4L2_PIX_FMT_SRGGB8;
  510. } else if (strcmp(value, "BGGR8") == 0) {
  511. front_fmt = V4L2_PIX_FMT_SBGGR8;
  512. front_mbus = MEDIA_BUS_FMT_SBGGR8_1X8;
  513. } else if (strcmp(value, "GRBG8") == 0) {
  514. front_fmt = V4L2_PIX_FMT_SGRBG8;
  515. } else if (strcmp(value, "GBRG8") == 0) {
  516. front_fmt = V4L2_PIX_FMT_SGBRG8;
  517. } else {
  518. g_printerr("Unsupported pixelformat %s\n", value);
  519. exit(1);
  520. }
  521. } else if (strcmp(name, "driver") == 0) {
  522. front_dev_name = strdup(value);
  523. } else {
  524. g_printerr("Unknown key '%s' in [front]\n", name);
  525. exit(1);
  526. }
  527. } else if (strcmp(section, "device") == 0) {
  528. if (strcmp(name, "csi") == 0) {
  529. media_drv_name = strdup(value);
  530. } else {
  531. g_printerr("Unknown key '%s' in [device]\n", name);
  532. exit(1);
  533. }
  534. } else {
  535. g_printerr("Unknown section '%s' in config file\n", section);
  536. exit(1);
  537. }
  538. return 1;
  539. }
  540. int
  541. find_dev_node(int maj, int min, char *fnbuf)
  542. {
  543. DIR *d;
  544. struct dirent *dir;
  545. struct stat info;
  546. d = opendir("/dev");
  547. while ((dir = readdir(d)) != NULL) {
  548. sprintf(fnbuf, "/dev/%s", dir->d_name);
  549. stat(fnbuf, &info);
  550. if (!S_ISCHR(info.st_mode)) {
  551. continue;
  552. }
  553. if (major(info.st_rdev) == maj && minor(info.st_rdev) == min) {
  554. return 0;
  555. }
  556. }
  557. return -1;
  558. }
  559. int
  560. setup_rear()
  561. {
  562. struct media_link_desc link = {0};
  563. // Disable the interface<->front link
  564. link.flags = 0;
  565. link.source.entity = front_entity_id;
  566. link.source.index = 0;
  567. link.sink.entity = interface_entity_id;
  568. link.sink.index = 0;
  569. if (xioctl(media_fd, MEDIA_IOC_SETUP_LINK, &link) < 0) {
  570. g_printerr("Could not disable front camera link\n");
  571. return -1;
  572. }
  573. // Enable the interface<->rear link
  574. link.flags = MEDIA_LNK_FL_ENABLED;
  575. link.source.entity = rear_entity_id;
  576. link.source.index = 0;
  577. link.sink.entity = interface_entity_id;
  578. link.sink.index = 0;
  579. if (xioctl(media_fd, MEDIA_IOC_SETUP_LINK, &link) < 0) {
  580. g_printerr("Could not enable rear camera link\n");
  581. return -1;
  582. }
  583. current_width = rear_width;
  584. current_height = rear_height;
  585. current_fmt = rear_fmt;
  586. current_rotate = rear_rotate;
  587. // Find camera node
  588. init_sensor(rear_dev, rear_width, rear_height, rear_mbus);
  589. return 0;
  590. }
  591. int
  592. setup_front()
  593. {
  594. struct media_link_desc link = {0};
  595. // Disable the interface<->rear link
  596. link.flags = 0;
  597. link.source.entity = rear_entity_id;
  598. link.source.index = 0;
  599. link.sink.entity = interface_entity_id;
  600. link.sink.index = 0;
  601. if (xioctl(media_fd, MEDIA_IOC_SETUP_LINK, &link) < 0) {
  602. g_printerr("Could not disable rear camera link\n");
  603. return -1;
  604. }
  605. // Enable the interface<->rear link
  606. link.flags = MEDIA_LNK_FL_ENABLED;
  607. link.source.entity = front_entity_id;
  608. link.source.index = 0;
  609. link.sink.entity = interface_entity_id;
  610. link.sink.index = 0;
  611. if (xioctl(media_fd, MEDIA_IOC_SETUP_LINK, &link) < 0) {
  612. g_printerr("Could not enable front camera link\n");
  613. return -1;
  614. }
  615. current_width = front_width;
  616. current_height = front_height;
  617. current_fmt = front_fmt;
  618. current_rotate = front_rotate;
  619. // Find camera node
  620. init_sensor(front_dev, front_width, front_height, front_mbus);
  621. return 0;
  622. }
  623. int
  624. find_cameras()
  625. {
  626. struct media_entity_desc entity = {0};
  627. int ret;
  628. int found = 0;
  629. while (1) {
  630. entity.id = entity.id | MEDIA_ENT_ID_FLAG_NEXT;
  631. ret = xioctl(media_fd, MEDIA_IOC_ENUM_ENTITIES, &entity);
  632. if (ret < 0) {
  633. break;
  634. }
  635. printf("At node %s, (0x%x)\n", entity.name, entity.type);
  636. if (strncmp(entity.name, front_dev_name, strlen(front_dev_name)) == 0) {
  637. front_entity_id = entity.id;
  638. find_dev_node(entity.dev.major, entity.dev.minor, front_dev);
  639. printf("Found front cam, is %s at %s\n", entity.name, front_dev);
  640. found++;
  641. }
  642. if (strncmp(entity.name, rear_dev_name, strlen(rear_dev_name)) == 0) {
  643. rear_entity_id = entity.id;
  644. find_dev_node(entity.dev.major, entity.dev.minor, rear_dev);
  645. printf("Found rear cam, is %s at %s\n", entity.name, rear_dev);
  646. found++;
  647. }
  648. if (entity.type == MEDIA_ENT_F_IO_V4L) {
  649. interface_entity_id = entity.id;
  650. find_dev_node(entity.dev.major, entity.dev.minor, dev_name);
  651. printf("Found v4l2 interface node at %s\n", dev_name);
  652. }
  653. }
  654. if (found < 2) {
  655. return -1;
  656. }
  657. return 0;
  658. }
  659. int
  660. find_media_fd()
  661. {
  662. DIR *d;
  663. struct dirent *dir;
  664. int fd;
  665. char fnbuf[261];
  666. struct media_device_info mdi = {0};
  667. d = opendir("/dev");
  668. while ((dir = readdir(d)) != NULL) {
  669. if (strncmp(dir->d_name, "media", 5) == 0) {
  670. sprintf(fnbuf, "/dev/%s", dir->d_name);
  671. printf("Checking %s\n", fnbuf);
  672. fd = open(fnbuf, O_RDWR);
  673. xioctl(fd, MEDIA_IOC_DEVICE_INFO, &mdi);
  674. printf("Found media device: %s\n", mdi.driver);
  675. if (strcmp(mdi.driver, media_drv_name) == 0) {
  676. media_fd = fd;
  677. return 0;
  678. }
  679. close(fd);
  680. }
  681. }
  682. return 1;
  683. }
  684. void
  685. on_shutter_clicked(GtkWidget *widget, gpointer user_data)
  686. {
  687. capture = 1;
  688. }
  689. void
  690. on_camera_switch_clicked(GtkWidget *widget, gpointer user_data)
  691. {
  692. stop_capturing(video_fd);
  693. close(current_fd);
  694. if (current_is_rear == 1) {
  695. setup_front();
  696. current_is_rear = 0;
  697. } else {
  698. setup_rear();
  699. current_is_rear = 1;
  700. }
  701. printf("close() = %d\n", close(video_fd));
  702. printf("Opening %s again\n", dev_name);
  703. video_fd = open(dev_name, O_RDWR);
  704. if (video_fd == -1) {
  705. g_printerr("Error opening video device: %s\n", dev_name);
  706. return;
  707. }
  708. init_device(video_fd);
  709. start_capturing(video_fd);
  710. }
  711. int
  712. main(int argc, char *argv[])
  713. {
  714. if (argc != 2) {
  715. g_printerr("Usage: camera configfile\n");
  716. return 1;
  717. }
  718. GError *error = NULL;
  719. gtk_init(&argc, &argv);
  720. g_object_set(gtk_settings_get_default(), "gtk-application-prefer-dark-theme", TRUE, NULL);
  721. GtkBuilder *builder = gtk_builder_new();
  722. char *glade_file = "/usr/share/megapixels/ui/camera.glade";
  723. if (access("camera.glade", F_OK) != -1) {
  724. glade_file = "camera.glade";
  725. }
  726. if (gtk_builder_add_from_file(builder, glade_file, &error) == 0) {
  727. g_printerr("Error loading file: %s\n", error->message);
  728. g_clear_error(&error);
  729. return 1;
  730. }
  731. GtkWidget *window = GTK_WIDGET(gtk_builder_get_object(builder, "window"));
  732. GtkWidget *preview_box = GTK_WIDGET(gtk_builder_get_object(builder, "preview_box"));
  733. GtkWidget *shutter = GTK_WIDGET(gtk_builder_get_object(builder, "shutter"));
  734. GtkWidget *switch_btn = GTK_WIDGET(gtk_builder_get_object(builder, "switch_camera"));
  735. GtkWidget *settings_btn = GTK_WIDGET(gtk_builder_get_object(builder, "settings"));
  736. preview = GTK_WIDGET(gtk_builder_get_object(builder, "preview"));
  737. g_signal_connect(window, "destroy", G_CALLBACK(gtk_main_quit), NULL);
  738. g_signal_connect(shutter, "clicked", G_CALLBACK(on_shutter_clicked), NULL);
  739. g_signal_connect(switch_btn, "clicked", G_CALLBACK(on_camera_switch_clicked), NULL);
  740. g_signal_connect(preview, "draw", G_CALLBACK(preview_draw), NULL);
  741. g_signal_connect(preview, "configure-event", G_CALLBACK(preview_configure), NULL);
  742. GtkCssProvider *provider = gtk_css_provider_new();
  743. if (access("camera.css", F_OK) != -1) {
  744. gtk_css_provider_load_from_path(provider, "camera.css", NULL);
  745. } else {
  746. gtk_css_provider_load_from_path(provider, "/usr/share/camera/ui/camera.css", NULL);
  747. }
  748. GtkStyleContext *context = gtk_widget_get_style_context(preview_box);
  749. gtk_style_context_add_provider(context,
  750. GTK_STYLE_PROVIDER(provider),
  751. GTK_STYLE_PROVIDER_PRIORITY_USER);
  752. int result = ini_parse(argv[1], config_ini_handler, NULL);
  753. if (result == -1) {
  754. g_printerr("Config file not found\n");
  755. return 1;
  756. } else if (result == -2) {
  757. g_printerr("Could not allocate memory to parse config file\n");
  758. return 1;
  759. } else if (result != 0) {
  760. g_printerr("Could not parse config file\n");
  761. return 1;
  762. }
  763. if (find_media_fd() == -1) {
  764. g_printerr("Could not find the media node\n");
  765. return 1;
  766. }
  767. if (find_cameras() == -1) {
  768. g_printerr("Could not find the cameras\n");
  769. return 1;
  770. }
  771. setup_rear();
  772. int fd = open(dev_name, O_RDWR);
  773. if (fd == -1) {
  774. g_printerr("Error opening video device: %s\n", dev_name);
  775. return 1;
  776. }
  777. video_fd = fd;
  778. init_device(fd);
  779. start_capturing(fd);
  780. // Get a new frame every 34ms ~30fps
  781. printf("window show\n");
  782. gtk_widget_show(window);
  783. g_idle_add((GSourceFunc)get_frame, NULL);
  784. gtk_main();
  785. return 0;
  786. }