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.

tsdumper2.c 9.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318
  1. /*
  2. * tsdumper2
  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 <stdlib.h>
  16. #include <stdarg.h>
  17. #include <unistd.h>
  18. #include <getopt.h>
  19. #include <string.h>
  20. #include <sys/types.h>
  21. #include <sys/stat.h>
  22. #include <signal.h>
  23. #include <fcntl.h>
  24. #include <errno.h>
  25. #include <sys/resource.h>
  26. #include "tsdumper2.h"
  27. extern int ai_family;
  28. #define PROGRAM_NAME "tsdumper2"
  29. static const char *program_id = PROGRAM_NAME " v" VERSION " (git-" GIT_VER ", build date " BUILD_ID ")";
  30. static int keep_running = 1;
  31. static unsigned long long total_read;
  32. static const char short_options[] = "n:s:d:i:z46DhV";
  33. static const struct option long_options[] = {
  34. { "prefix", required_argument, NULL, 'n' },
  35. { "seconds", required_argument, NULL, 's' },
  36. { "output-dir", required_argument, NULL, 'd' },
  37. { "create-dirs", no_argument, NULL, 'D' },
  38. { "input", required_argument, NULL, 'i' },
  39. { "input-ignore-disc", no_argument, NULL, 'z' },
  40. { "ipv4", no_argument, NULL, '4' },
  41. { "ipv6", no_argument, NULL, '6' },
  42. { "help", no_argument, NULL, 'h' },
  43. { "version", no_argument, NULL, 'V' },
  44. { 0, 0, 0, 0 }
  45. };
  46. static void show_help(struct ts *ts) {
  47. printf("%s\n", program_id);
  48. printf("Copyright (C) 2013 Unix Solutions Ltd.\n");
  49. printf("\n");
  50. printf(" Usage: " PROGRAM_NAME " -n <name> -i <input>\n");
  51. printf("\n");
  52. printf("Settings:\n");
  53. printf(" -n --prefix <name> | Filename prefix.\n");
  54. printf(" -s --seconds <seconds> | How much to save (default: %u sec).\n", ts->rotate_secs);
  55. printf(" -d --output-dir <dir> | Startup directory (default: %s).\n", ts->output_dir);
  56. printf(" -D --create-dirs | Save files in subdirs YYYY/MM/DD/HH/file.\n");
  57. printf("\n");
  58. printf("Input options:\n");
  59. printf(" -i --input <source> | Where to read from.\n");
  60. printf(" . -i udp://224.0.0.1:5000 (v4 multicast)\n");
  61. printf(" . -i udp://[ff01::1111]:5000 (v6 multicast)\n");
  62. printf(" . -i rtp://224.0.0.1:5000 (v4 RTP input)\n");
  63. printf(" . -i rtp://[ff01::1111]:5000 (v6 RTP input)\n");
  64. printf(" -z --input-ignore-disc | Do not report discontinuty errors in input.\n");
  65. printf(" -4 --ipv4 | Use only IPv4 addresses.\n");
  66. printf(" -6 --ipv6 | Use only IPv6 addresses.\n");
  67. printf("\n");
  68. printf("Misc options:\n");
  69. printf(" -h --help | Show help screen.\n");
  70. printf(" -V --version | Show program version.\n");
  71. printf("\n");
  72. }
  73. extern char *optarg;
  74. extern int optind, opterr, optopt;
  75. static void parse_options(struct ts *ts, int argc, char **argv) {
  76. int j, input_addr_err = 1;
  77. opterr = 0; // Prevent printing of error messages for unknown options in getopt()
  78. while ((j = getopt_long(argc, argv, short_options, long_options, NULL)) != -1) {
  79. if (j == '?')
  80. die("Unknown parameter '%s'.\n", argv[optind - 1]);
  81. switch (j) {
  82. case 'n': // --prefix
  83. ts->prefix = optarg;
  84. if (strlen(optarg) >= PREFIX_MAX_LENGTH)
  85. die("Prefix is longer than %d characters!", PREFIX_MAX_LENGTH);
  86. break;
  87. case 's': // --seconds
  88. ts->rotate_secs = atoi(optarg);
  89. break;
  90. case 'd': // --output-dir
  91. ts->output_dir = optarg;
  92. break;
  93. case 'D': // --create-dirs
  94. ts->create_dirs = !ts->create_dirs;
  95. break;
  96. case 'i': // --input
  97. input_addr_err = !parse_host_and_port(optarg, &ts->input);
  98. break;
  99. case 'z': // --input-ignore-disc
  100. ts->ts_discont = !ts->ts_discont;
  101. break;
  102. case '4': // --ipv4
  103. ai_family = AF_INET;
  104. break;
  105. case '6': // --ipv6
  106. ai_family = AF_INET6;
  107. break;
  108. case 'h': // --help
  109. show_help(ts);
  110. exit(EXIT_SUCCESS);
  111. case 'V': // --version
  112. printf("%s\n", program_id);
  113. exit(EXIT_SUCCESS);
  114. }
  115. }
  116. if (input_addr_err || !ts->prefix) {
  117. show_help(ts);
  118. if (!ts->prefix)
  119. fprintf(stderr, "ERROR: File name prefix is not set (--prefix XXX | -n XXX).\n");
  120. if (input_addr_err)
  121. fprintf(stderr, "ERROR: Input address is invalid (--input XXX | -i XXX).\n");
  122. exit(EXIT_FAILURE);
  123. }
  124. p_info("Prefix : %s\n", ts->prefix);
  125. p_info("Input addr : %s://%s:%s/\n",
  126. ts->input.type == UDP ? "udp" :
  127. ts->input.type == RTP ? "rtp" : "???",
  128. ts->input.hostname, ts->input.service);
  129. p_info("Seconds : %u\n", ts->rotate_secs);
  130. p_info("Output dir : %s (create directories: %s)\n", ts->output_dir,
  131. ts->create_dirs ? "YES" : "no");
  132. if (chdir(ts->output_dir) < 0)
  133. die("Can not change directory to %s: %s\n", ts->output_dir, strerror(errno));
  134. }
  135. void signal_quit(int sig) {
  136. if (!keep_running)
  137. raise(sig);
  138. keep_running = 0;
  139. p_info("Killed %s with signal %d\n", program_id, sig);
  140. signal(sig, SIG_DFL);
  141. }
  142. static void clear_packet(struct packet *p) {
  143. p->ts.tv_sec = 0;
  144. p->ts.tv_usec = 0;
  145. p->data_len = 0;
  146. p->allocated = 0;
  147. p->in_use = 0;
  148. }
  149. struct packet *alloc_packet(struct ts *ts) {
  150. // check for free static allocations
  151. struct packet *p;
  152. int i;
  153. for (i = 0; i < NUM_PACKETS; i++) {
  154. p = &ts->packets[i];
  155. if (!p->in_use) {
  156. p->in_use = 1;
  157. p_dbg2("STATIC packet, num %d\n", p->num);
  158. goto OUT;
  159. }
  160. }
  161. // Dynamically allocate packet
  162. p = malloc(sizeof(struct packet));
  163. if (!p)
  164. die("Can't alloc %lu bytes.\n", (unsigned long)sizeof(struct packet));
  165. clear_packet(p);
  166. p->num = time(NULL);
  167. p->allocated = 1;
  168. p_dbg2("ALLOC packet, num %d\n", p->num);
  169. OUT:
  170. return p;
  171. }
  172. void free_packet(struct packet *packet) {
  173. if (!packet->allocated) {
  174. clear_packet(packet);
  175. return;
  176. }
  177. p_dbg2("FREE packet, num %d\n", packet->num);
  178. free(packet);
  179. }
  180. #define RTP_HDR_SZ 12
  181. static uint8_t ts_packet[FRAME_SIZE + RTP_HDR_SZ];
  182. static uint8_t rtp_hdr[2][RTP_HDR_SZ];
  183. static struct ts ts;
  184. int main(int argc, char **argv) {
  185. int i;
  186. int have_data = 1;
  187. int ntimeouts = 0;
  188. int rtp_hdr_pos = 0, num_packets = 0;
  189. struct rlimit rl;
  190. if (getrlimit(RLIMIT_STACK, &rl) == 0) {
  191. if (rl.rlim_cur > THREAD_STACK_SIZE) {
  192. rl.rlim_cur = THREAD_STACK_SIZE;
  193. setrlimit(RLIMIT_STACK, &rl);
  194. }
  195. }
  196. memset(rtp_hdr[0], 0, RTP_HDR_SZ);
  197. memset(rtp_hdr[1], 0, RTP_HDR_SZ);
  198. for (i = 0; i < NUM_PACKETS; i++) {
  199. struct packet *p = &ts.packets[i];
  200. p->num = i + 1;
  201. }
  202. ts.ts_discont = 1;
  203. ts.output_dir = ".";
  204. ts.rotate_secs = 60;
  205. ts.current_packet = alloc_packet(&ts);
  206. ts.output_fd = -1;
  207. pthread_attr_init(&ts.thread_attr);
  208. size_t stack_size;
  209. pthread_attr_getstacksize(&ts.thread_attr, &stack_size);
  210. if (stack_size > THREAD_STACK_SIZE)
  211. pthread_attr_setstacksize(&ts.thread_attr, THREAD_STACK_SIZE);
  212. parse_options(&ts, argc, argv);
  213. ts.packet_queue = queue_new();
  214. p_info("Start %s\n", program_id);
  215. switch (ts.input.type) {
  216. case UDP:
  217. case RTP:
  218. if (udp_connect_input(&ts.input) < 1)
  219. exit(EXIT_FAILURE);
  220. break;
  221. }
  222. signal(SIGCHLD, SIG_IGN);
  223. signal(SIGPIPE, SIG_IGN);
  224. signal(SIGINT , signal_quit);
  225. signal(SIGTERM, signal_quit);
  226. pthread_create(&ts.write_thread , &ts.thread_attr, &write_thread , &ts);
  227. int data_received = 0;
  228. do {
  229. ssize_t readen = -1;
  230. set_log_io_errors(0);
  231. switch (ts.input.type) {
  232. case UDP:
  233. readen = fdread_ex(ts.input.fd, (char *)ts_packet, FRAME_SIZE, 250, 4, 1);
  234. break;
  235. case RTP:
  236. readen = fdread_ex(ts.input.fd, (char *)ts_packet, FRAME_SIZE + RTP_HDR_SZ, 250, 4, 1);
  237. if (readen > RTP_HDR_SZ) {
  238. memcpy(rtp_hdr[rtp_hdr_pos], ts_packet, RTP_HDR_SZ);
  239. memmove(ts_packet, ts_packet + RTP_HDR_SZ, FRAME_SIZE);
  240. readen -= RTP_HDR_SZ;
  241. uint16_t ssrc = (rtp_hdr[rtp_hdr_pos][2] << 8) | rtp_hdr[rtp_hdr_pos][3];
  242. uint16_t pssrc = (rtp_hdr[!rtp_hdr_pos][2] << 8) | rtp_hdr[!rtp_hdr_pos][3];
  243. rtp_hdr_pos = !rtp_hdr_pos;
  244. if (pssrc + 1 != ssrc && (ssrc != 0 && pssrc != 0xffff) && num_packets > 2)
  245. if (ts.ts_discont)
  246. p_info(" *** RTP discontinuity last_ssrc %5d, curr_ssrc %5d, lost %d packet ***\n",
  247. pssrc, ssrc, ((ssrc - pssrc)-1) & 0xffff);
  248. num_packets++;
  249. }
  250. break;
  251. }
  252. set_log_io_errors(1);
  253. if (readen < 0) {
  254. p_info(" *** Input read timeout ***\n");
  255. data_received = 0;
  256. ntimeouts++;
  257. } else {
  258. if (ntimeouts && readen > 0) {
  259. ntimeouts = 0;
  260. }
  261. }
  262. if (readen > 0) {
  263. if (!data_received) {
  264. p_info("Data received.\n");
  265. data_received = 1;
  266. }
  267. total_read += readen;
  268. process_packets(&ts, ts_packet, readen);
  269. }
  270. if (!keep_running)
  271. break;
  272. } while (have_data);
  273. queue_add(ts.packet_queue, ts.current_packet);
  274. queue_add(ts.packet_queue, NULL); // Exit write_thread
  275. pthread_join(ts.write_thread, NULL);
  276. p_info("Stop %s (bytes_processed:%llu).\n", program_id, total_read);
  277. queue_free(&ts.packet_queue);
  278. pthread_attr_destroy(&ts.thread_attr);
  279. exit(EXIT_SUCCESS);
  280. }