tsdumper2 reads incoming mpeg transport stream over UDP/RTP and then records it to disk. The files names are generated based on preconfigured time interval. https://georgi.unixsol.org/programs/tsdumper2/
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

util.c 3.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. /*
  2. * Utility functions
  3. * Copyright (C) 2013 Unix Solutions Ltd.
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License version 2
  7. * as published by the Free Software Foundation.
  8. *
  9. * This program is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU General Public License (COPYING file) for more details.
  13. *
  14. */
  15. #include <stdio.h>
  16. #include <stdarg.h>
  17. #include <stdlib.h>
  18. #include <unistd.h>
  19. #include <ctype.h>
  20. #include <time.h>
  21. #include <string.h>
  22. #include <errno.h>
  23. #include <arpa/inet.h>
  24. #include <sys/stat.h>
  25. #include <sys/types.h>
  26. #include "tsdumper2.h"
  27. #ifdef __linux__
  28. #include <sys/prctl.h>
  29. void set_thread_name(char *thread_name) {
  30. prctl(PR_SET_NAME, thread_name, NULL, NULL, NULL);
  31. }
  32. #else
  33. void set_thread_name(char *thread_name) {
  34. (void)thread_name;
  35. }
  36. #endif
  37. int parse_host_and_port(char *input, struct io *io) {
  38. int port_set = 0;
  39. char *p, *proto;
  40. io->type = UDP;
  41. if (strlen(input) == 0)
  42. return 0;
  43. if (strstr(input, "udp://") == input) io->type = UDP;
  44. else if (strstr(input, "rtp://") == input) io->type = RTP;
  45. else
  46. die("Unsupported protocol (patch welcome): %s", input);
  47. proto = strstr(input, "://");
  48. if (proto)
  49. input = proto + 3;
  50. io->hostname = input;
  51. if (input[0] == '[') { // Detect IPv6 static address
  52. p = strrchr(input, ']');
  53. if (!p)
  54. die("Invalid IPv6 address format: %s\n", input);
  55. io->hostname = input + 1; // Remove first [
  56. *p = 0x00; // Remove last ]
  57. char *p2 = strchr(p + 1, ':');
  58. if (p2) {
  59. *p2 = 0x00;
  60. io->service = p2 + 1;
  61. port_set = 1;
  62. }
  63. } else {
  64. p = strrchr(input, ':');
  65. if (p) {
  66. *p = 0x00;
  67. io->service = p + 1;
  68. port_set = 1;
  69. }
  70. }
  71. if (io->service) {
  72. char *path = strstr(io->service, "/");
  73. if (path)
  74. path[0] = 0;
  75. }
  76. if (!port_set)
  77. die("Port is not set in the input url.");
  78. return 1;
  79. }
  80. char *my_inet_ntop(int family, struct sockaddr *addr, char *dest, int dest_len) {
  81. struct sockaddr_in *addr_v4 = (struct sockaddr_in *)addr;
  82. struct sockaddr_in6 *addr_v6 = (struct sockaddr_in6 *)addr;
  83. switch (family) {
  84. case AF_INET:
  85. return (char *)inet_ntop(AF_INET, &addr_v4->sin_addr, dest, dest_len);
  86. break;
  87. case AF_INET6:
  88. return (char *)inet_ntop(AF_INET6, &addr_v6->sin6_addr, dest, dest_len);
  89. break;
  90. default:
  91. memset(dest, 0, dest_len);
  92. strcpy(dest, "unknown");
  93. return dest;
  94. }
  95. }
  96. int create_dir(const char *dir, mode_t mode) {
  97. int ret = 0;
  98. unsigned int i;
  99. // Shortcut
  100. if (strchr(dir, '/') == NULL)
  101. return mkdir(dir, mode);
  102. char *d = strdup(dir);
  103. unsigned int dlen = strlen(dir);
  104. // Skip first char (it can be /)
  105. for (i = 1; i < dlen; i++) {
  106. if (d[i] != '/')
  107. continue;
  108. d[i] = '\0';
  109. ret = mkdir(d, mode);
  110. d[i] = '/';
  111. if (ret < 0 && errno != EEXIST)
  112. goto OUT;
  113. }
  114. ret = mkdir(d, mode);
  115. OUT:
  116. free(d);
  117. return ret;
  118. }
  119. void die(const char *fmt, ...) {
  120. va_list args;
  121. va_start(args, fmt);
  122. fprintf(stderr, "ERROR: ");
  123. vfprintf(stderr, fmt, args);
  124. if (fmt[strlen(fmt) - 1] != '\n')
  125. fprintf(stderr, "\n");
  126. va_end(args);
  127. exit(EXIT_FAILURE);
  128. }
  129. void p_err(const char *fmt, ...) {
  130. va_list args;
  131. va_start(args, fmt);
  132. fprintf(stderr, "ERROR: ");
  133. vfprintf(stderr, fmt, args);
  134. if (fmt[strlen(fmt) - 1] != '\n')
  135. fprintf(stderr, "\n");
  136. va_end(args);
  137. }
  138. void p_info(const char *fmt, ...) {
  139. if (DEBUG > 0) {
  140. struct timeval tv;
  141. gettimeofday(&tv, NULL);
  142. fprintf(stdout, "%08ld.%08ld ", (long)tv.tv_sec, (long)tv.tv_usec);
  143. char date[64];
  144. struct tm tm;
  145. localtime_r(&tv.tv_sec, &tm);
  146. strftime(date, sizeof(date), "%F %H:%M:%S", localtime_r(&tv.tv_sec, &tm));
  147. fprintf(stdout, "%s | ", date);
  148. }
  149. va_list args;
  150. va_start(args, fmt);
  151. vfprintf(stdout, fmt, args);
  152. if (fmt[strlen(fmt) - 1] != '\n')
  153. fprintf(stdout, "\n");
  154. va_end(args);
  155. fflush(stdout);
  156. }