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

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