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

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