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.

process.c 12KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454
  1. /*
  2. * Process packets
  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 (COPYING file) for more details.
  13. *
  14. */
  15. #include <unistd.h>
  16. #include <string.h>
  17. #include <sys/uio.h>
  18. #include "data.h"
  19. #include "csa.h"
  20. #include "tables.h"
  21. #include "util.h"
  22. #include "notify.h"
  23. static unsigned long ts_pack;
  24. static int ts_pack_shown;
  25. char *get_pid_desc(struct ts *ts, uint16_t pid) {
  26. int i;
  27. uint16_t nitpid = 0x0010, pmtpid = 0xffff, pcrpid = 0xffff;
  28. if (ts->pat->initialized) {
  29. for (i=0;i<ts->pat->programs_num;i++) {
  30. struct ts_pat_program *prg = ts->pat->programs[i];
  31. if (prg->pid) {
  32. if (prg->program == 0)
  33. nitpid = prg->pid;
  34. }
  35. }
  36. }
  37. if (ts->pmt->initialized) {
  38. pmtpid = ts->pmt->ts_header.pid;
  39. pcrpid = ts->pmt->PCR_pid;
  40. for (i=0;i<ts->pmt->streams_num;i++) {
  41. struct ts_pmt_stream *stream = ts->pmt->streams[i];
  42. if (pid == stream->pid)
  43. return h222_stream_type_desc(stream->stream_type);
  44. }
  45. }
  46. switch (pid) {
  47. case 0x0000: return "PAT"; break;
  48. case 0x0001: return "CAT"; break;
  49. case 0x0011: return "SDT"; break;
  50. case 0x0012: return "EPG"; break;
  51. case 0x0014: return "TDT/TOT"; break;
  52. }
  53. if (pid == nitpid) return "NIT";
  54. else if (pid == pmtpid) return "PMT";
  55. else if (pid == pcrpid) return "PCR";
  56. else if (pid == ts->emm_pid) return "EMM";
  57. else if (pid == ts->ecm_pid) return "ECM";
  58. return "Unknown";
  59. }
  60. void show_ts_pack(struct ts *ts, uint16_t pid, char *wtf, char *extra, uint8_t *ts_packet) {
  61. char pdump[188 * 6];
  62. char cw1_dump[8 * 6];
  63. char cw2_dump[8 * 6];
  64. if (ts->debug_level >= 4) {
  65. if (ts_pack_shown)
  66. return;
  67. if (ts->debug_level >= 5)
  68. ts_hex_dump_buf(pdump, 188 * 6, ts_packet, 188, 0);
  69. int stype = ts_packet_get_scrambled(ts_packet);
  70. ts_hex_dump_buf(cw1_dump, 8 * 6, ts->key.cw , 8, 0);
  71. ts_hex_dump_buf(cw2_dump, 8 * 6, ts->key.cw + 8, 8, 0);
  72. fprintf(stderr, "@ %s %s %03x %5ld %7ld | %s %s | %s %s\n",
  73. stype == 0 ? "------" :
  74. stype == 2 ? "even 0" :
  75. stype == 3 ? "odd 1" : "??????",
  76. wtf,
  77. pid,
  78. ts_pack, ts_pack * 188,
  79. cw1_dump, cw2_dump, extra ? extra : wtf,
  80. ts->debug_level >= 5 ? pdump : "");
  81. }
  82. }
  83. static void dump_ts_pack(struct ts *ts, uint16_t pid, uint8_t *ts_packet) {
  84. if (pid == 0x010) show_ts_pack(ts, pid, "nit", NULL, ts_packet);
  85. else if (pid == 0x11) show_ts_pack(ts, pid, "sdt", NULL, ts_packet);
  86. else if (pid == 0x12) show_ts_pack(ts, pid, "epg", NULL, ts_packet);
  87. else show_ts_pack(ts, pid, "---", NULL, ts_packet);
  88. }
  89. static void decode_packet(struct ts *ts, uint8_t *ts_packet) {
  90. int scramble_idx = ts_packet_get_scrambled(ts_packet);
  91. if (scramble_idx > 1) {
  92. if (ts->key.is_valid_cw) {
  93. csa_decrypt_single_packet(ts->key.csakey, ts_packet);
  94. } else {
  95. // Can't decrypt the packet just make it NULL packet
  96. if (ts->pid_filter)
  97. ts_packet_set_pid(ts_packet, 0x1fff);
  98. }
  99. }
  100. }
  101. static void decode_buffer(struct ts *ts, uint8_t *data, int data_len) {
  102. int i;
  103. int batch_sz = csa_get_batch_size(); // Tested with 32 for libdvbcsa, 70 for FFdecsa (must be multiplied by 2)
  104. int even_packets = 0;
  105. int odd_packets = 0;
  106. struct csa_batch even_pcks[batch_sz + 1];
  107. struct csa_batch odd_pcks [batch_sz + 1];
  108. uint8_t *ff_even_pcks[batch_sz * 2 + 1];
  109. uint8_t *ff_odd_pcks [batch_sz * 2 + 1];
  110. int scramble_idx_old = 0;
  111. time_t now = time(NULL);
  112. // Prepare batch structure
  113. for (i = 0; i < batch_sz; i++) {
  114. uint8_t *ts_packet = data + (i * 188);
  115. uint16_t pid = ts_packet_get_pid(ts_packet);
  116. if (pidmap_get(&ts->pidmap, pid) && ts_packet_is_scrambled(ts_packet)) {
  117. if (ts_packet_is_scrambled(ts_packet) && ts->last_scrambled_packet_ts != now) {
  118. ts->stream_is_encrypted = 1;
  119. ts->last_scrambled_packet_ts = now;
  120. }
  121. if (ts->key.is_valid_cw) {
  122. int scramble_idx = ts_packet_get_scrambled(ts_packet);
  123. if (!scramble_idx_old)
  124. scramble_idx_old = scramble_idx;
  125. if (use_dvbcsa) {
  126. uint8_t payload_ofs = ts_packet_get_payload_offset(ts_packet);
  127. if (scramble_idx == 2) { // scramble_idx 2 == even key
  128. even_pcks[even_packets].data = ts_packet + payload_ofs;
  129. even_pcks[even_packets].len = 188 - payload_ofs;
  130. even_packets++;
  131. }
  132. if (scramble_idx == 3) { // scramble_idx 3 == odd key
  133. odd_pcks[odd_packets].data = ts_packet + payload_ofs;
  134. odd_pcks[odd_packets].len = 188 - payload_ofs;
  135. odd_packets++;
  136. }
  137. ts_packet_set_not_scrambled(ts_packet);
  138. }
  139. if (use_ffdecsa) {
  140. if (scramble_idx == 2) { // scramble_idx 2 == even key
  141. ff_even_pcks[even_packets * 2 ] = ts_packet;
  142. ff_even_pcks[even_packets * 2 + 1] = ts_packet + 188;
  143. even_packets++;
  144. }
  145. if (scramble_idx == 3) { // scramble_idx 3 == odd key
  146. ff_odd_pcks[odd_packets * 2 ] = ts_packet;
  147. ff_odd_pcks[odd_packets * 2 + 1] = ts_packet + 188;
  148. odd_packets++;
  149. }
  150. }
  151. if (scramble_idx_old != scramble_idx && !ts->camd.constant_codeword) {
  152. struct timeval tv;
  153. gettimeofday(&tv, NULL);
  154. ts_LOGf("CWC | SID 0x%04x ------------ EcmTime: %5llu ms CW_time: %5llu ms\n",
  155. ts->service_id,
  156. timeval_diff_msec(&ts->ecm_change_time, &tv),
  157. timeval_diff_msec(&ts->key.ts_keyset, &tv));
  158. }
  159. scramble_idx_old = scramble_idx;
  160. } else {
  161. if (ts->pid_filter)
  162. ts_packet_set_pid(ts_packet, 0x1fff);
  163. }
  164. }
  165. }
  166. if (ts->last_scrambled_packet_ts && ts->last_scrambled_packet_ts < now - 5) {
  167. ts_LOGf("N/E | No encrypted packet came during the last 5 seconds, disabling NO_CODE_WORD notifcation\n");
  168. notify(ts, "STREAM_NOT_ENCRYPTED", "No encrypted packets were seen in the last 5 seconds.");
  169. ts->stream_is_encrypted = 0;
  170. ts->last_scrambled_packet_ts = 0;
  171. }
  172. // Decode packets
  173. if (even_packets) {
  174. if (use_dvbcsa) {
  175. even_pcks[even_packets].data = NULL; // Last one...
  176. csa_decrypt_multiple_even(ts->key.csakey, even_pcks);
  177. }
  178. if (use_ffdecsa) {
  179. ff_even_pcks[even_packets * 2] = NULL;
  180. csa_decrypt_multiple_ff(ts->key.csakey, ff_even_pcks);
  181. }
  182. }
  183. if (odd_packets) {
  184. if (use_dvbcsa) {
  185. odd_pcks[odd_packets].data = NULL; // Last one...
  186. csa_decrypt_multiple_odd(ts->key.csakey, odd_pcks);
  187. }
  188. if (use_ffdecsa) {
  189. ff_odd_pcks[odd_packets * 2] = NULL;
  190. csa_decrypt_multiple_ff(ts->key.csakey, ff_odd_pcks);
  191. }
  192. }
  193. // Fill write buffer
  194. for (i=0; i<data_len; i += 188) {
  195. uint8_t *ts_packet = data + i;
  196. if (!ts->pid_filter) {
  197. cbuf_fill(ts->write_buf, ts_packet, 188);
  198. } else {
  199. uint16_t pid = ts_packet_get_pid(ts_packet);
  200. if (pidmap_get(&ts->pidmap, pid)) // PAT or allowed PIDs
  201. cbuf_fill(ts->write_buf, ts_packet, 188);
  202. }
  203. }
  204. }
  205. void *decode_thread(void *_ts) {
  206. struct ts *ts = _ts;
  207. uint8_t *data;
  208. int data_size;
  209. int req_size = 188 * csa_get_batch_size();
  210. set_thread_name("tsdec-decode");
  211. while (!ts->decode_stop) {
  212. cbuf_peek(ts->decode_buf, req_size, &data_size);
  213. if (data_size < req_size) {
  214. usleep(1000);
  215. continue;
  216. }
  217. data = cbuf_get(ts->decode_buf, req_size, &data_size);
  218. if (data)
  219. decode_buffer(ts, data, data_size);
  220. }
  221. do { // Flush data
  222. data = cbuf_get(ts->decode_buf, req_size, &data_size);
  223. if (data)
  224. decode_buffer(ts, data, data_size);
  225. } while(data);
  226. return NULL;
  227. }
  228. static inline void output_write(struct ts *ts, uint8_t *data, unsigned int data_size) {
  229. if (!data)
  230. return;
  231. if (ts->no_output_on_error && !ts->camd.key->is_valid_cw)
  232. return;
  233. if (!ts->rtp_output) {
  234. if (write(ts->output.fd, data, data_size) < 0) {
  235. perror("write(output_fd)");
  236. return;
  237. }
  238. } else {
  239. struct iovec iov[2];
  240. uint8_t rtp_header[12];
  241. uint32_t rtime = get_time() * 9 / 100;
  242. ts->rtp_seqnum++;
  243. rtp_header[ 0] = 0x80;
  244. rtp_header[ 1] = 33; // MPEG TS rtp payload type
  245. rtp_header[ 2] = ts->rtp_seqnum >> 8;
  246. rtp_header[ 3] = ts->rtp_seqnum & 0xff;
  247. rtp_header[ 4] = (rtime >> 24) & 0xff;
  248. rtp_header[ 5] = (rtime >> 16) & 0xff;
  249. rtp_header[ 6] = (rtime >> 8) & 0xff;
  250. rtp_header[ 7] = rtime & 0xff;
  251. rtp_header[ 8] = (ts->rtp_ssrc >> 24) & 0xff;
  252. rtp_header[ 9] = (ts->rtp_ssrc >> 16) & 0xff;
  253. rtp_header[10] = (ts->rtp_ssrc >> 8) & 0xff;
  254. rtp_header[11] = ts->rtp_ssrc & 0xff;
  255. iov[0].iov_base = rtp_header;
  256. iov[0].iov_len = sizeof(rtp_header);
  257. iov[1].iov_base = data;
  258. iov[1].iov_len = data_size;
  259. if (writev(ts->output.fd, iov, 2) < 0) {
  260. perror("writev(output_fd)");
  261. return;
  262. }
  263. }
  264. }
  265. void *write_thread(void *_ts) {
  266. struct ts *ts = _ts;
  267. uint8_t *data;
  268. int data_size;
  269. set_thread_name("tsdec-write");
  270. while (!ts->write_stop) {
  271. data_size = 0;
  272. cbuf_peek(ts->write_buf, FRAME_SIZE, &data_size);
  273. if (data_size < FRAME_SIZE) {
  274. usleep(5000);
  275. continue;
  276. }
  277. data = cbuf_get (ts->write_buf, FRAME_SIZE, &data_size);
  278. output_write(ts, data, data_size);
  279. }
  280. do { // Flush data
  281. data = cbuf_get(ts->write_buf, FRAME_SIZE, &data_size);
  282. output_write(ts, data, data_size);
  283. } while(data);
  284. return NULL;
  285. }
  286. static void detect_discontinuity(struct ts *ts, uint8_t *ts_packet) {
  287. uint16_t pid;
  288. uint8_t cur_cc, last_cc;
  289. if (!ts->ts_discont)
  290. return;
  291. pid = ts_packet_get_pid(ts_packet);
  292. cur_cc = ts_packet_get_cont(ts_packet);
  293. if (!pidmap_get(&ts->pid_seen, pid)) {
  294. if (strcmp(get_pid_desc(ts, pid), "Unknown") == 0)
  295. return;
  296. pidmap_set(&ts->pid_seen, pid);
  297. pidmap_set_val(&ts->cc, pid, cur_cc);
  298. ts_LOGf("NEW | Input PID 0x%04x appeared (%s)\n",
  299. pid, get_pid_desc(ts, pid));
  300. return;
  301. }
  302. last_cc = pidmap_get(&ts->cc, pid);
  303. if (last_cc != cur_cc && ((last_cc + 1) & 0x0f) != cur_cc)
  304. ts_LOGf("--- | TS discontinuity on PID 0x%04x expected %2d got %2d /%d/ (%s)\n",
  305. pid,
  306. ((last_cc + 1) & 0x0f), cur_cc,
  307. (cur_cc - ((last_cc + 1) & 0x0f)) & 0x0f,
  308. get_pid_desc(ts, pid));
  309. pidmap_set_val(&ts->cc, pid, cur_cc);
  310. }
  311. void process_packets(struct ts *ts, uint8_t *data, ssize_t data_len) {
  312. ssize_t i;
  313. int64_t now = get_time();
  314. for (i=0; i<data_len; i += 188) {
  315. uint8_t *ts_packet = data + i;
  316. uint16_t pid = ts_packet_get_pid(ts_packet);
  317. if (ts->pid_report)
  318. ts->pid_stats[pid]++;
  319. ts_pack_shown = 0;
  320. process_pat(ts, pid, ts_packet);
  321. process_cat(ts, pid, ts_packet);
  322. process_pmt(ts, pid, ts_packet);
  323. process_sdt(ts, pid, ts_packet);
  324. process_emm(ts, pid, ts_packet);
  325. process_ecm(ts, pid, ts_packet);
  326. detect_discontinuity(ts, ts_packet);
  327. if (!ts_pack_shown)
  328. dump_ts_pack(ts, pid, ts_packet);
  329. if (!ts->output_stream)
  330. continue;
  331. // Return rewritten PAT
  332. if (pid == 0x00 && ts->pid_filter && ts->genpat->initialized) {
  333. if (!ts_packet_is_pusi(ts_packet))
  334. continue;
  335. ts_packet_set_cont(ts->genpat->section_header->packet_data, ts->genpat_cc);
  336. ts->genpat->ts_header.continuity = ts->genpat_cc;
  337. ts_packet = ts->genpat->section_header->packet_data;
  338. ts->genpat_cc = (ts->genpat_cc + 1) & 0x0f;
  339. }
  340. if (ts->threaded) {
  341. // Add to decode buffer. The decoder thread will handle it
  342. if (ts->input_buffer_time == 0) {
  343. // No input buffer, move packets to decoding buffer
  344. if (cbuf_fill(ts->decode_buf, ts_packet, 188) != 0) {
  345. ts_LOGf("Decode buffer is full, waiting...\n");
  346. cbuf_dump(ts->decode_buf);
  347. usleep(10000);
  348. }
  349. } else {
  350. // Handle input buffer
  351. struct packet_buf *p = malloc(sizeof(struct packet_buf));
  352. p->time = now + (ts->input_buffer_time * 1000); //buffer time is in ms, p->time is in us
  353. memcpy(p->data, ts_packet, 188);
  354. list_add(ts->input_buffer, p);
  355. // Move packets to decrypt buffer
  356. LNODE *lc, *lctmp;
  357. list_for_each(ts->input_buffer, lc, lctmp) {
  358. p = lc->data;
  359. if (p->time <= now) {
  360. if (cbuf_fill(ts->decode_buf, p->data, 188) != 0) {
  361. ts_LOGf("Decode buffer is full, waiting...\n");
  362. cbuf_dump(ts->decode_buf);
  363. usleep(10000);
  364. }
  365. list_del(ts->input_buffer, &lc);
  366. free(p);
  367. } else {
  368. break;
  369. }
  370. }
  371. }
  372. } else {
  373. int allowed_pid = pidmap_get(&ts->pidmap, pid);
  374. if (allowed_pid) // PAT or allowed PIDs
  375. decode_packet(ts, ts_packet);
  376. if (ts->pid_filter) {
  377. if (allowed_pid) // PAT or allowed PIDs
  378. output_write(ts, ts_packet, 188);
  379. } else {
  380. output_write(ts, ts_packet, 188);
  381. }
  382. }
  383. ts_pack++;
  384. }
  385. }
  386. void show_pid_report(struct ts *ts) {
  387. int i;
  388. if (!ts->pid_report)
  389. return;
  390. for (i = 0; i < MAX_PIDS; i++) {
  391. if (ts->pid_stats[i]) {
  392. ts_LOGf("PID | %8u packets with PID 0x%04x (%4u) %s\n",
  393. ts->pid_stats[i], i, i, get_pid_desc(ts, i));
  394. }
  395. }
  396. }