main.c 22 KB

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