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.8KB

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