tsdecrypt reads and decrypts CSA encrypted incoming mpeg transport stream over UDP/RTP using code words obtained from OSCAM or similar CAM server. tsdecrypt communicates with CAM server using cs378x (camd35 over tcp) protocol or newcamd protocol. https://georgi.unixsol.org/programs/tsdecrypt/
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.

tsdecrypt.c 23KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671
  1. /*
  2. * tsdecrypt
  3. * Copyright (C) 2011 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 for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with this program; if not, write to the Free Software
  16. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
  17. */
  18. #include <stdlib.h>
  19. #include <unistd.h>
  20. #include <getopt.h>
  21. #include <string.h>
  22. #include <sys/types.h>
  23. #include <sys/stat.h>
  24. #include <signal.h>
  25. #include <fcntl.h>
  26. #include <errno.h>
  27. #include <syslog.h>
  28. #include "data.h"
  29. #include "util.h"
  30. #include "camd.h"
  31. #include "process.h"
  32. #include "udp.h"
  33. #include "notify.h"
  34. #define FIRST_REPORT_SEC 3
  35. #define PROGRAM_NAME "tsdecrypt"
  36. static const char *program_id = PROGRAM_NAME " v" VERSION " (" GIT_VER ", build " BUILD_ID ")";
  37. static int keep_running = 1;
  38. static void LOG_func(const char *msg) {
  39. char date[64];
  40. struct tm tm;
  41. time_t now;
  42. now = time(NULL);
  43. localtime_r(&now, &tm);
  44. strftime(date, sizeof(date), "%F %H:%M:%S", localtime(&now));
  45. fprintf(stderr, "%s | %s", date, msg);
  46. }
  47. static void LOG_func_syslog(const char *msg) {
  48. syslog(LOG_INFO, msg, strlen(msg));
  49. }
  50. static const struct option long_options[] = {
  51. { "ident", required_argument, NULL, 'i' },
  52. { "daemon", required_argument, NULL, 'd' },
  53. { "syslog", no_argument, NULL, 'S' },
  54. { "syslog-host", required_argument, NULL, 'l' },
  55. { "syslog-port", required_argument, NULL, 'L' },
  56. { "notify-program", required_argument, NULL, 'N' },
  57. { "input", required_argument, NULL, 'I' },
  58. { "input-rtp", no_argument, NULL, 'R' },
  59. { "input-ignore-disc", no_argument, NULL, 'z' },
  60. { "output", required_argument, NULL, 'O' },
  61. { "output-intf", required_argument, NULL, 'o' },
  62. { "output-ttl", required_argument, NULL, 't' },
  63. { "output-filter", no_argument, NULL, 'p' },
  64. { "ca-system", required_argument, NULL, 'c' },
  65. { "caid", required_argument, NULL, 'C' },
  66. { "camd-server", required_argument, NULL, 's' },
  67. { "camd-user", required_argument, NULL, 'U' },
  68. { "camd-pass", required_argument, NULL, 'P' },
  69. { "camd-pkt-delay", required_argument, NULL, 'y' },
  70. { "emm", no_argument, NULL, 'e' },
  71. { "emm-pid", required_argument, NULL, 'Z' },
  72. { "emm-only", no_argument, NULL, 'E' },
  73. { "emm-report-time", required_argument, NULL, 'f' },
  74. { "ecm-pid", required_argument, NULL, 'X' },
  75. { "ecm-report-time", required_argument, NULL, 'H' },
  76. { "ecm-irdeto-type", required_argument, NULL, 'G' },
  77. { "ecm-no-log", no_argument , NULL, 'K' },
  78. { "cw-warn-time", required_argument, NULL, 'J' },
  79. { "debug", required_argument, NULL, 'D' },
  80. { "help", no_argument, NULL, 'h' },
  81. { "version", no_argument, NULL, 'V' },
  82. { 0, 0, 0, 0 }
  83. };
  84. static void show_help(struct ts *ts) {
  85. printf("%s\n", program_id);
  86. printf("Copyright (c) 2011 Unix Solutions Ltd.\n");
  87. printf("\n");
  88. printf(" Usage: " PROGRAM_NAME " [opts]\n");
  89. printf("\n");
  90. printf("Daemon options:\n");
  91. printf(" -i --ident <server> | Format PROVIDER/CHANNEL. Default: empty\n");
  92. printf(" -d --daemon <pidfile> | Daemonize program and write pid file.\n");
  93. printf(" -N --notify-program <prg> | Execute <prg> to report events. Default: empty\n");
  94. printf("\n");
  95. printf(" -S --syslog | Log messages using syslog.\n");
  96. printf(" -l --syslog-host <host> | Syslog server address. Default: disabled\n");
  97. printf(" -L --syslog-port <port> | Syslog server port. Default: %d\n", ts->syslog_port);
  98. printf("\n");
  99. printf("Input options:\n");
  100. printf(" -I --input <source> | Where to read from. File or multicast address.\n");
  101. printf(" . -I 224.0.0.1:5000 (multicast receive)\n");
  102. printf(" . -I file.ts (read from file)\n");
  103. printf(" . -I - (read from stdin) (default)\n");
  104. printf(" -R --input-rtp | Enable RTP input\n");
  105. printf(" -z --input-ignore-disc | Do not report discontinuty errors in input.\n");
  106. printf("\n");
  107. printf("Output options:\n");
  108. printf(" -O --output <dest> | Where to send output. File or multicast address.\n");
  109. printf(" . -O 239.0.0.1:5000 (multicast send)\n");
  110. printf(" . -O file.ts (write to file)\n");
  111. printf(" . -O - (write to stdout) (default)\n");
  112. printf(" -o --output-intf <addr> | Set multicast output interface. Default: %s\n", inet_ntoa(ts->output.intf));
  113. printf(" -t --output-ttl <ttl> | Set multicast ttl. Default: %d\n", ts->output.ttl);
  114. printf(" -p --output-filter | Enable or disable output filter. Default: %s\n", ts->pid_filter ? "enabled" : "disabled");
  115. printf("\n");
  116. printf("CA options:\n");
  117. printf(" -c --ca-system <ca_sys> | Process input EMM/ECM from <ca_sys>.\n");
  118. printf(" | Valid systems are: CONAX (default), CRYPTOWORKS,\n");
  119. printf(" . IRDETO, SECA (MEDIAGUARD), VIACCESS,\n");
  120. printf(" . VIDEOGUARD (NDS), NAGRA and DRECRYPT.\n");
  121. printf(" -C --caid <caid> | Set CAID. Default: Taken from --ca-system.\n");
  122. printf("\n");
  123. printf("CAMD server options:\n");
  124. printf(" -s --camd-server <addr> | Set CAMD server ip address and port (1.2.3.4:2233).\n");
  125. printf(" -U --camd-user <user> | Set CAMD server user. Default: %s\n", ts->camd35.user);
  126. printf(" -P --camd-pass <pass> | Set CAMD server password. Default: %s\n", ts->camd35.pass);
  127. printf(" -y --camd-pkt-delay <us> | Sleep <us> usec between sending ECM/EMM\n");
  128. printf(" . packets to CAMD. Default: %d\n", ts->packet_delay);
  129. printf("\n");
  130. printf("EMM options:\n");
  131. printf(" -e --emm | Enable sending EMM's to CAMD. Default: %s\n", ts->emm_send ? "enabled" : "disabled");
  132. printf(" -E --emm-only | Send only EMMs to CAMD, skipping ECMs and without\n");
  133. printf(" . decoding the input stream.\n");
  134. printf(" -Z --emm-pid <pid> | Force EMM pid. Default: none\n");
  135. printf(" -f --emm-report-time <sec> | Report each <sec> seconds how much EMMs have been\n");
  136. printf(" . received/processed. Set <sec> to 0 to disable\n");
  137. printf(" . the reports. Default: %d sec\n", ts->emm_report_interval);
  138. printf("\n");
  139. printf("ECM options:\n");
  140. printf(" -X --ecm-pid <pid> | Force ECM pid. Default: none\n");
  141. printf(" -H --ecm-report-time <sec> | Report each <sec> how much ECMs and CWs have been\n");
  142. printf(" . processed/skipped. Set <sec> to 0 to disable\n");
  143. printf(" . the reports. Default: %d sec\n", ts->ecm_report_interval);
  144. printf(" -G --ecm-irdeto-type <int> | Process IRDETO ECMs with type X /0..3/. Default: %d\n", ts->irdeto_ecm);
  145. printf(" -K --ecm-no-log | Disable ECM and code words logging.\n");
  146. printf(" -J --cw-warn-time <sec> | Warn if no valid code word has been received.\n");
  147. printf(" . Set <sec> to 0 to disable. Default: %d sec\n", ts->cw_warn_sec);
  148. printf("\n");
  149. printf("Misc options:\n");
  150. printf(" -D --debug <level> | Message debug level.\n");
  151. printf(" . 0 = default messages\n");
  152. printf(" . 1 = show PSI tables\n");
  153. printf(" . 2 = show EMMs\n");
  154. printf(" . 3 = show duplicate ECMs\n");
  155. printf(" . 4 = packet debug\n");
  156. printf(" . 5 = packet debug + packet dump\n");
  157. printf(" -h --help | Show help screen.\n");
  158. printf(" -V --version | Show program version.\n");
  159. printf("\n");
  160. }
  161. static int parse_io_param(struct io *io, char *opt, int open_flags, mode_t open_mode) {
  162. io->type = WTF_IO;
  163. char *p = strrchr(opt, ':');
  164. if (!p) {
  165. io->type = FILE_IO;
  166. if (strcmp(opt, "-") != 0) {
  167. io->fd = open(opt, open_flags, open_mode);
  168. if (io->fd < 0) {
  169. fprintf(stderr, "ERROR: Can not open file (%s): %s\n", opt, strerror(errno));
  170. exit(1);
  171. }
  172. }
  173. io->fname = strdup(opt);
  174. return 0;
  175. }
  176. *p = 0x00;
  177. io->type = NET_IO;
  178. io->port = atoi(p + 1);
  179. if (inet_aton(opt, &io->addr) == 0)
  180. return 1;
  181. return 0;
  182. }
  183. static void parse_options(struct ts *ts, int argc, char **argv) {
  184. int j, i, ca_err = 0, server_err = 1, input_addr_err = 0, output_addr_err = 0, output_intf_err = 0, ident_err = 0;
  185. while ( (j = getopt_long(argc, argv, "i:d:N:l:L:I:RzO:o:t:pc:C:s:U:P:y:eZ:Ef:X:H:G:KJ:D:hV", long_options, NULL)) != -1 ) {
  186. char *p = NULL;
  187. switch (j) {
  188. case 'i':
  189. strncpy(ts->ident, optarg, sizeof(ts->ident) - 1);
  190. ts->ident[sizeof(ts->ident) - 1] = 0;
  191. break;
  192. case 'd':
  193. strncpy(ts->pidfile, optarg, sizeof(ts->pidfile) - 1);
  194. ts->pidfile[sizeof(ts->pidfile) - 1] = 0;
  195. ts->daemonize = 1;
  196. break;
  197. case 'N':
  198. strncpy(ts->notify_program, optarg, sizeof(ts->notify_program) - 1);
  199. ts->notify_program[sizeof(ts->notify_program) - 1] = 0;
  200. break;
  201. case 'S':
  202. ts->syslog_active = 1;
  203. ts->syslog_remote = 0;
  204. break;
  205. case 'l':
  206. strncpy(ts->syslog_host, optarg, sizeof(ts->syslog_host) - 1);
  207. ts->syslog_host[sizeof(ts->syslog_host) - 1] = 0;
  208. ts->syslog_active = 1;
  209. ts->syslog_remote = 1;
  210. break;
  211. case 'L':
  212. ts->syslog_port = atoi(optarg);
  213. break;
  214. case 'I':
  215. input_addr_err = parse_io_param(&ts->input, optarg, O_RDONLY, 0);
  216. break;
  217. case 'R':
  218. ts->rtp_input = !ts->rtp_input;
  219. break;
  220. case 'z':
  221. ts->ts_discont = !ts->ts_discont;
  222. break;
  223. case 'O':
  224. output_addr_err = parse_io_param(&ts->output, optarg,
  225. O_CREAT | O_WRONLY | O_TRUNC,
  226. S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH);
  227. break;
  228. case 'o':
  229. if (inet_aton(optarg, &ts->output.intf) == 0)
  230. output_intf_err = 1;
  231. break;
  232. case 't':
  233. ts->output.ttl = atoi(optarg);
  234. break;
  235. case 'p':
  236. ts->pid_filter = !ts->pid_filter;
  237. break;
  238. case 'c':
  239. if (strcasecmp("IRDETO", optarg) == 0)
  240. ts->req_CA_sys = CA_IRDETO;
  241. else if (strcasecmp("CONNAX", optarg) == 0 || strcasecmp("CONAX", optarg) == 0)
  242. ts->req_CA_sys = CA_CONAX;
  243. else if (strcasecmp("CRYPTOWORKS", optarg) == 0)
  244. ts->req_CA_sys = CA_CRYPTOWORKS;
  245. else if (strcasecmp("SECA", optarg) == 0 || strcasecmp("MEDIAGUARD", optarg) == 0)
  246. ts->req_CA_sys = CA_SECA;
  247. else if (strcasecmp("VIACCESS", optarg) == 0)
  248. ts->req_CA_sys = CA_VIACCESS;
  249. else if (strcasecmp("VIDEOGUARD", optarg) == 0 || strcasecmp("NDS", optarg) == 0)
  250. ts->req_CA_sys = CA_VIDEOGUARD;
  251. else if (strcasecmp("NAGRA", optarg) == 0)
  252. ts->req_CA_sys = CA_NAGRA;
  253. else if (strcasecmp("DRE-CRYPT", optarg) == 0 || strcasecmp("DRECRYPT", optarg) == 0)
  254. ts->req_CA_sys = CA_DRECRYPT;
  255. else
  256. ca_err = 1;
  257. break;
  258. case 'C':
  259. ts->forced_caid = strtoul(optarg, NULL, 0) & 0xffff;
  260. break;
  261. case 's':
  262. p = strrchr(optarg, ':');
  263. if (p) {
  264. *p = 0x00;
  265. ts->camd35.server_port = atoi(p + 1);
  266. }
  267. if (inet_aton(optarg, &ts->camd35.server_addr) == 0)
  268. server_err = 1;
  269. else
  270. server_err = 0;
  271. break;
  272. case 'U':
  273. strncpy(ts->camd35.user, optarg, sizeof(ts->camd35.user) - 1);
  274. ts->camd35.user[sizeof(ts->camd35.user) - 1] = 0;
  275. break;
  276. case 'P':
  277. strncpy(ts->camd35.pass, optarg, sizeof(ts->camd35.pass) - 1);
  278. ts->camd35.pass[sizeof(ts->camd35.pass) - 1] = 0;
  279. break;
  280. case 'y':
  281. ts->packet_delay = atoi(optarg);
  282. if (ts->packet_delay < 0 || ts->packet_delay > 1000000)
  283. ts->packet_delay = 0;
  284. break;
  285. case 'e':
  286. ts->emm_send = !ts->emm_send;
  287. break;
  288. case 'Z':
  289. ts->forced_emm_pid = strtoul(optarg, NULL, 0) & 0x1fff;
  290. break;
  291. case 'E':
  292. ts->emm_only = 1;
  293. ts->emm_send = 1;
  294. break;
  295. case 'f':
  296. ts->emm_report_interval = strtoul(optarg, NULL, 10);
  297. if (ts->emm_report_interval > 86400)
  298. ts->emm_report_interval = 86400;
  299. break;
  300. case 'X':
  301. ts->forced_ecm_pid = strtoul(optarg, NULL, 0) & 0x1fff;
  302. break;
  303. case 'H':
  304. ts->ecm_report_interval = strtoul(optarg, NULL, 10);
  305. if (ts->ecm_report_interval > 86400)
  306. ts->ecm_report_interval = 86400;
  307. break;
  308. case 'G':
  309. ts->irdeto_ecm = atoi(optarg);
  310. break;
  311. case 'K':
  312. ts->ecm_cw_log = !ts->ecm_cw_log;
  313. break;
  314. case 'J':
  315. ts->cw_warn_sec = strtoul(optarg, NULL, 10);
  316. if (ts->cw_warn_sec > 86400)
  317. ts->cw_warn_sec = 86400;
  318. break;
  319. case 'D':
  320. ts->debug_level = atoi(optarg);
  321. break;
  322. case 'h':
  323. show_help(ts);
  324. exit(0);
  325. case 'V':
  326. printf("%s\n", program_id);
  327. exit(0);
  328. }
  329. }
  330. if (!ts->ident[0]) {
  331. if (ts->syslog_active || ts->notify_program[0])
  332. ident_err = 1;
  333. }
  334. if (ident_err || ca_err || server_err || input_addr_err || output_addr_err || ts->input.type == WTF_IO || ts->output.type == WTF_IO) {
  335. show_help(ts);
  336. if (ident_err)
  337. fprintf(stderr, "ERROR: Ident is not set, please use --ident option.\n");
  338. if (ca_err)
  339. fprintf(stderr, "ERROR: Requested CA system is unsupported.\n");
  340. if (server_err)
  341. fprintf(stderr, "ERROR: CAMD server IP address is not set or it is invalid.\n");
  342. if (input_addr_err)
  343. fprintf(stderr, "ERROR: Input IP address is invalid.\n");
  344. if (output_addr_err)
  345. fprintf(stderr, "ERROR: Output IP address is invalid.\n");
  346. if (output_intf_err)
  347. fprintf(stderr, "ERROR: Output interface address is invalid.\n");
  348. exit(1);
  349. }
  350. ts_LOGf("Ident : %s\n", ts->ident[0] ? ts->ident : "*NOT SET*");
  351. ts_LOGf("Notify prog: %s\n", ts->notify_program[0] ? ts->notify_program : "*NOT SET*");
  352. if (ts->pidfile[0])
  353. ts_LOGf("Daemonize : %s pid file.\n", ts->pidfile);
  354. else
  355. ts_LOGf("Daemonize : no daemon\n");
  356. if (ts->syslog_active) {
  357. if (ts->syslog_remote)
  358. ts_LOGf("Syslog : %s:%d\n", ts->syslog_host, ts->syslog_port);
  359. else
  360. ts_LOGf("Syslog : enabled\n");
  361. } else
  362. ts_LOGf("Syslog : disabled\n");
  363. if (ts->forced_caid)
  364. ts->req_CA_sys = ts_get_CA_sys(ts->forced_caid);
  365. if (!ts->forced_caid)
  366. ts_LOGf("CA System : %s\n", ts_get_CA_sys_txt(ts->req_CA_sys));
  367. else
  368. ts_LOGf("CA System : %s | CAID: 0x%04x (%d)\n",
  369. ts_get_CA_sys_txt(ts->req_CA_sys),
  370. ts->forced_caid, ts->forced_caid);
  371. if (ts->input.type == NET_IO) {
  372. ts_LOGf("Input addr : %s://%s:%u/\n",
  373. ts->rtp_input ? "rtp" : "udp",
  374. inet_ntoa(ts->input.addr), ts->input.port);
  375. } else if (ts->input.type == FILE_IO) {
  376. ts_LOGf("Input file : %s\n", ts->input.fd == 0 ? "STDIN" : ts->input.fname);
  377. }
  378. if (ts->req_CA_sys == CA_IRDETO)
  379. ts_LOGf("Irdeto ECM : %d\n", ts->irdeto_ecm);
  380. if (!ts->emm_only)
  381. {
  382. if (ts->output.type == NET_IO) {
  383. ts_LOGf("Output addr: udp://%s:%u/\n", inet_ntoa(ts->output.addr), ts->output.port);
  384. ts_LOGf("Output intf: %s\n", inet_ntoa(ts->output.intf));
  385. ts_LOGf("Output ttl : %d\n", ts->output.ttl);
  386. } else if (ts->output.type == FILE_IO) {
  387. ts_LOGf("Output file: %s\n", ts->output.fd == 1 ? "STDOUT" : ts->output.fname);
  388. }
  389. ts_LOGf("PID filter : %s\n", ts->pid_filter ? "enabled" : "disabled");
  390. }
  391. ts_LOGf("Server addr: tcp://%s:%u/\n", inet_ntoa(ts->camd35.server_addr), ts->camd35.server_port);
  392. ts_LOGf("Server user: %s\n", ts->camd35.user);
  393. ts_LOGf("Server pass: %s\n", ts->camd35.pass);
  394. if (ts->packet_delay)
  395. ts_LOGf("Pkt sleep : %d us (%d ms)\n", ts->packet_delay, ts->packet_delay / 1000);
  396. ts_LOGf("TS discont : %s\n", ts->ts_discont ? "report" : "ignore");
  397. ts->threaded = !(ts->input.type == FILE_IO && ts->input.fd != 0);
  398. if (ts->emm_send && ts->emm_report_interval)
  399. ts_LOGf("EMM report : %d sec\n", ts->emm_report_interval);
  400. if (ts->emm_send && ts->emm_report_interval == 0)
  401. ts_LOGf("EMM report : disabled\n");
  402. if (ts->forced_emm_pid)
  403. ts_LOGf("EMM pid : 0x%04x (%d)\n", ts->forced_emm_pid, ts->forced_emm_pid);
  404. if (ts->emm_only) {
  405. ts_LOGf("EMM only : %s\n", ts->emm_only ? "yes" : "no");
  406. } else {
  407. ts_LOGf("EMM send : %s\n", ts->emm_send ? "enabled" : "disabled");
  408. ts_LOGf("Decoding : %s\n", ts->threaded ? "threaded" : "single thread");
  409. }
  410. if (!ts->emm_only && ts->ecm_report_interval)
  411. ts_LOGf("ECM report : %d sec\n", ts->emm_report_interval);
  412. if (!ts->emm_only && ts->ecm_report_interval == 0)
  413. ts_LOGf("ECM report : disabled\n");
  414. if (ts->forced_ecm_pid)
  415. ts_LOGf("ECM pid : 0x%04x (%d)\n", ts->forced_ecm_pid, ts->forced_ecm_pid);
  416. if (!ts->emm_only && ts->cw_warn_sec)
  417. ts_LOGf("CW warning : %d sec\n", ts->cw_warn_sec);
  418. if (!ts->emm_only && ts->cw_warn_sec)
  419. ts_LOGf("CW warning : disabled\n");
  420. if (!ts->ecm_cw_log)
  421. ts_LOGf("ECM/CW log : disabled\n");
  422. for (i=0; i<(int)sizeof(ts->ident); i++) {
  423. if (!ts->ident[i])
  424. break;
  425. if (ts->ident[i] == '/')
  426. ts->ident[i] = '-';
  427. }
  428. }
  429. static void report_emms(struct ts *ts, time_t now) {
  430. ts_LOGf("EMM | Received %u and processed %u in %lu seconds.\n",
  431. ts->emm_seen_count,
  432. ts->emm_processed_count,
  433. now - ts->emm_last_report);
  434. ts->emm_last_report = now;
  435. ts->emm_seen_count = 0;
  436. ts->emm_processed_count = 0;
  437. }
  438. static void report_ecms(struct ts *ts, time_t now) {
  439. ts_LOGf("ECM | Received %u (%u dup) and processed %u in %lu seconds.\n",
  440. ts->ecm_seen_count,
  441. ts->ecm_duplicate_count,
  442. ts->ecm_processed_count,
  443. now - ts->ecm_last_report);
  444. ts->ecm_last_report = now;
  445. ts->ecm_seen_count = 0;
  446. ts->ecm_duplicate_count = 0;
  447. ts->ecm_processed_count = 0;
  448. }
  449. static void report_cw_warn(struct ts *ts, time_t now) {
  450. notify(ts, "NO_CODE_WORD", "No valid code word was received for %ld sec.",
  451. now - ts->cw_last_warn);
  452. ts_LOGf("CW | *ERR* No valid code word was received for %ld seconds!\n",
  453. now - ts->cw_last_warn);
  454. ts->cw_last_warn = now;
  455. }
  456. static void do_reports(struct ts *ts) {
  457. static int first_emm_report = 1;
  458. static int first_ecm_report = 1;
  459. time_t now = time(NULL);
  460. if (ts->emm_send && ts->emm_report_interval) {
  461. if (first_emm_report && now >= ts->emm_last_report) {
  462. first_emm_report = 0;
  463. ts->emm_last_report -= FIRST_REPORT_SEC;
  464. report_emms(ts, now);
  465. } else if ((time_t)(ts->emm_last_report + ts->emm_report_interval) <= now) {
  466. report_emms(ts, now);
  467. }
  468. }
  469. if (!ts->emm_only && ts->ecm_report_interval) {
  470. if (first_ecm_report && now >= ts->ecm_last_report) {
  471. first_ecm_report = 0;
  472. ts->ecm_last_report -= FIRST_REPORT_SEC;
  473. report_ecms(ts, now);
  474. } else if ((time_t)(ts->ecm_last_report + ts->ecm_report_interval) <= now) {
  475. report_ecms(ts, now);
  476. }
  477. }
  478. if (!ts->emm_only && !ts->key.is_valid_cw) {
  479. if ((time_t)(ts->cw_last_warn + ts->cw_warn_sec) <= now) {
  480. report_cw_warn(ts, now);
  481. }
  482. }
  483. }
  484. void signal_quit(int sig) {
  485. if (!keep_running)
  486. raise(sig);
  487. keep_running = 0;
  488. ts_LOGf("Killed %s with signal %d\n", program_id, sig);
  489. signal(sig, SIG_DFL);
  490. }
  491. #define RTP_HDR_SZ 12
  492. int main(int argc, char **argv) {
  493. ssize_t readen;
  494. int have_data = 1;
  495. int ntimeouts = 0;
  496. time_t timeout_start = time(NULL);
  497. uint8_t ts_packet[FRAME_SIZE + RTP_HDR_SZ];
  498. uint8_t rtp_hdr[2][RTP_HDR_SZ];
  499. int rtp_hdr_pos = 0, num_packets = 0;
  500. struct ts ts;
  501. memset(rtp_hdr[0], 0, RTP_HDR_SZ);
  502. memset(rtp_hdr[1], 0, RTP_HDR_SZ);
  503. data_init(&ts);
  504. parse_options(&ts, argc, argv);
  505. if (ts.pidfile[0])
  506. daemonize(ts.pidfile);
  507. if (!ts.syslog_active) {
  508. ts_set_log_func(LOG_func);
  509. } else {
  510. if (ts.syslog_remote) {
  511. ts_set_log_func(LOG);
  512. log_init(ts.ident, 1, 1, ts.syslog_host, ts.syslog_port);
  513. } else {
  514. openlog(ts.ident, LOG_NDELAY | LOG_PID, LOG_USER);
  515. ts_set_log_func(LOG_func_syslog);
  516. }
  517. }
  518. ts.notify = notify_alloc(&ts);
  519. ts_LOGf("Start %s\n", program_id);
  520. notify(&ts, "START", "Starting %s", program_id);
  521. if (ts.input.type == NET_IO && udp_connect_input(&ts.input) < 1)
  522. goto EXIT;
  523. if (ts.output.type == NET_IO && udp_connect_output(&ts.output) < 1)
  524. goto EXIT;
  525. signal(SIGCHLD, SIG_IGN);
  526. signal(SIGPIPE, SIG_IGN);
  527. signal(SIGINT , signal_quit);
  528. signal(SIGTERM, signal_quit);
  529. if (&ts.threaded) {
  530. pthread_create(&ts.decode_thread, NULL, &decode_thread, &ts);
  531. pthread_create(&ts.write_thread, NULL , &write_thread , &ts);
  532. }
  533. ts.emm_last_report = time(NULL) + FIRST_REPORT_SEC;
  534. ts.ecm_last_report = time(NULL) + FIRST_REPORT_SEC;
  535. camd_start(&ts);
  536. do {
  537. do_reports(&ts);
  538. if (ts.input.type == NET_IO) {
  539. set_log_io_errors(0);
  540. if (!ts.rtp_input) {
  541. readen = fdread_ex(ts.input.fd, (char *)ts_packet, FRAME_SIZE, 250, 4, 1);
  542. } else {
  543. readen = fdread_ex(ts.input.fd, (char *)ts_packet, FRAME_SIZE + RTP_HDR_SZ, 250, 4, 1);
  544. if (readen > RTP_HDR_SZ) {
  545. memcpy(rtp_hdr[rtp_hdr_pos], ts_packet, RTP_HDR_SZ);
  546. memmove(ts_packet, ts_packet + RTP_HDR_SZ, FRAME_SIZE);
  547. readen -= RTP_HDR_SZ;
  548. uint16_t ssrc = (rtp_hdr[rtp_hdr_pos][2] << 8) | rtp_hdr[rtp_hdr_pos][3];
  549. uint16_t pssrc = (rtp_hdr[!rtp_hdr_pos][2] << 8) | rtp_hdr[!rtp_hdr_pos][3];
  550. rtp_hdr_pos = !rtp_hdr_pos;
  551. if (pssrc + 1 != ssrc && (ssrc != 0 && pssrc != 0xffff) && num_packets > 2)
  552. if (ts.ts_discont)
  553. ts_LOGf("--- | RTP discontinuity last_ssrc %5d, curr_ssrc %5d, lost %d packet\n",
  554. pssrc, ssrc, ((ssrc - pssrc)-1) & 0xffff);
  555. num_packets++;
  556. }
  557. }
  558. set_log_io_errors(1);
  559. if (readen < 0) {
  560. ts_LOGf("--- | Input read timeout.\n");
  561. if (!ntimeouts) {
  562. timeout_start = time(NULL);
  563. notify(&ts, "INPUT_TIMEOUT", "Read timeout on input %s://%s:%u/",
  564. ts.rtp_input ? "rtp" : "udp",
  565. inet_ntoa(ts.input.addr), ts.input.port);
  566. }
  567. ntimeouts++;
  568. } else {
  569. if (ntimeouts && readen > 0) {
  570. notify(&ts, "INPUT_OK", "Data is available on input %s://%s:%u/ after %ld seconds timeout.",
  571. ts.rtp_input ? "rtp" : "udp",
  572. inet_ntoa(ts.input.addr), ts.input.port,
  573. (time(NULL) - timeout_start) + 2); // Timeout is detected when ~2 seconds there is no incoming data
  574. ntimeouts = 0;
  575. }
  576. }
  577. } else {
  578. readen = read(ts.input.fd, ts_packet, FRAME_SIZE);
  579. have_data = !(readen <= 0);
  580. }
  581. if (readen > 0)
  582. process_packets(&ts, ts_packet, readen);
  583. if (!keep_running)
  584. break;
  585. } while (have_data);
  586. EXIT:
  587. camd_stop(&ts);
  588. if (ts.threaded) {
  589. ts.decode_stop = 1;
  590. ts.write_stop = 1;
  591. if (ts.decode_thread)
  592. pthread_join(ts.decode_thread, NULL);
  593. if (ts.write_thread)
  594. pthread_join(ts.write_thread, NULL);
  595. }
  596. notify_sync(&ts, "STOP", "Stopping %s", program_id);
  597. ts_LOGf("Stop %s\n", program_id);
  598. if (ts.syslog_active) {
  599. if (ts.syslog_remote)
  600. log_close();
  601. else
  602. closelog();
  603. }
  604. if (ts.daemonize)
  605. unlink(ts.pidfile);
  606. notify_free(&ts.notify);
  607. data_free(&ts);
  608. exit(0);
  609. }