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 8.9KB

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