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

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