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

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