captioner.c 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. /**********************************************************************************************/
  2. /* The MIT License */
  3. /* */
  4. /* Copyright 2016-2016 Twitch Interactive, Inc. or its affiliates. All Rights Reserved. */
  5. /* */
  6. /* Permission is hereby granted, free of charge, to any person obtaining a copy */
  7. /* of this software and associated documentation files (the "Software"), to deal */
  8. /* in the Software without restriction, including without limitation the rights */
  9. /* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell */
  10. /* copies of the Software, and to permit persons to whom the Software is */
  11. /* furnished to do so, subject to the following conditions: */
  12. /* */
  13. /* The above copyright notice and this permission notice shall be included in */
  14. /* all copies or substantial portions of the Software. */
  15. /* */
  16. /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR */
  17. /* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, */
  18. /* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE */
  19. /* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER */
  20. /* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, */
  21. /* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN */
  22. /* THE SOFTWARE. */
  23. /**********************************************************************************************/
  24. #include <stdlib.h>
  25. #include <unistd.h>
  26. #include <fcntl.h>
  27. #include <errno.h>
  28. #include <linux/input.h>
  29. #include <string.h>
  30. #include <stdio.h>
  31. #include "caption.h"
  32. #include "flv.h"
  33. char charcode[] = {
  34. 0, 0, '1', '2', '3', '4', '5', '6', '7', '8', '9', '0', '-', '=', 0, 0,
  35. 'Q', 'W', 'E', 'R', 'T', 'Y', 'U', 'I', 'O', 'P', '[', ']','\n', 0, 'A', 'S',
  36. 'D', 'F', 'G', 'H', 'J', 'K', 'L', ';', '\'', '`', 0, '\\', 'Z', 'X', 'C', 'V',
  37. 'B', 'N', 'M', ',', '.', '/', 0, '*', 0, ' ', 0, 0, 0, 0, 0, 0,
  38. };
  39. int data_ready (int fd)
  40. {
  41. fd_set set;
  42. struct timeval timeout = {0,0};
  43. FD_ZERO (&set);
  44. FD_SET (fd,&set);
  45. int cnt = select (fd+1, &set, 0, 0, &timeout);
  46. FD_ZERO (&set);
  47. return (0 < cnt);
  48. }
  49. #define MAX_CAP_LENGTH (SCREEN_ROWS*SCREEN_COLS)
  50. int main (int argc, char** argv)
  51. {
  52. int fd;
  53. ssize_t n;
  54. flvtag_t tag;
  55. struct input_event ev;
  56. int has_audio, has_video;
  57. const char* dev = argv[1];
  58. char text[MAX_CAP_LENGTH+1];
  59. memset (text,0,MAX_CAP_LENGTH+1);
  60. FILE* flv = flv_open_read ("-");
  61. FILE* out = flv_open_write ("-");
  62. fd = open (dev, O_RDONLY);
  63. if (fd == -1) {
  64. fprintf (stderr, "Cannot open %s: %s.\n", dev, strerror (errno));
  65. return EXIT_FAILURE;
  66. }
  67. if (!flv_read_header (flv,&has_audio,&has_video)) {
  68. fprintf (stderr,"%s is not an flv file\n", argv[1]);
  69. return EXIT_FAILURE;
  70. }
  71. if (!flv_write_header (out,has_audio,has_video)) {
  72. return EXIT_FAILURE;
  73. }
  74. flvtag_init (&tag);
  75. while (flv_read_tag (flv,&tag)) {
  76. if (flvtag_avcpackettype_nalu == flvtag_avcpackettype (&tag)) {
  77. if (data_ready (fd)) {
  78. n = read (fd, &ev, sizeof ev);
  79. if (n == (ssize_t)-1) {
  80. if (errno == EINTR) {
  81. continue;
  82. } else {
  83. break;
  84. }
  85. } else if (n != sizeof ev) {
  86. errno = EIO;
  87. break;
  88. }
  89. int len = strlen (text);
  90. char c = (EV_KEY == ev.type && 1 == ev.value && ev.code < 64) ? charcode[ev.code] : 0;
  91. if (0 < len && '\n' == c) {
  92. fprintf (stderr,"='%s'\n", text);
  93. flvtag_addcaption (&tag, text);
  94. memset (text,0,MAX_CAP_LENGTH+1);
  95. } else if (0 != c && len < MAX_CAP_LENGTH) {
  96. text[len] = c;
  97. }
  98. }
  99. }
  100. flv_write_tag (out,&tag);
  101. }
  102. return EXIT_SUCCESS;
  103. }