main.c 23 KB

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