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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410
  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 "tables.h"
  23. #include "util.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. // scramble_idx 2 == even key
  95. // scramble_idx 3 == odd key
  96. ts_packet_set_not_scrambled(ts_packet);
  97. uint8_t payload_ofs = ts_packet_get_payload_offset(ts_packet);
  98. dvbcsa_decrypt(ts->key.csakey[scramble_idx - 2], ts_packet + payload_ofs, 188 - payload_ofs);
  99. } else {
  100. // Can't decrypt the packet just make it NULL packet
  101. if (ts->pid_filter)
  102. ts_packet_set_pid(ts_packet, 0x1fff);
  103. }
  104. }
  105. }
  106. static void decode_buffer(struct ts *ts, uint8_t *data, int data_len) {
  107. int i;
  108. int batch_sz = dvbcsa_bs_batch_size(); // 32?
  109. int even_packets = 0;
  110. int odd_packets = 0;
  111. struct dvbcsa_bs_batch_s even_pcks[batch_sz + 1];
  112. struct dvbcsa_bs_batch_s odd_pcks [batch_sz + 1];
  113. int scramble_idx_old = 0;
  114. // Prepare batch structure
  115. for (i = 0; i < batch_sz; i++) {
  116. uint8_t *ts_packet = data + (i * 188);
  117. uint16_t pid = ts_packet_get_pid(ts_packet);
  118. if (pidmap_get(&ts->pidmap, pid) && ts_packet_is_scrambled(ts_packet)) {
  119. if (ts->key.is_valid_cw) {
  120. int scramble_idx = ts_packet_get_scrambled(ts_packet);
  121. if (!scramble_idx_old)
  122. scramble_idx_old = scramble_idx;
  123. uint8_t payload_ofs = ts_packet_get_payload_offset(ts_packet);
  124. if (scramble_idx == 2) { // scramble_idx 2 == even key
  125. even_pcks[even_packets].data = ts_packet + payload_ofs;
  126. even_pcks[even_packets].len = 188 - payload_ofs;
  127. even_packets++;
  128. }
  129. if (scramble_idx == 3) { // scramble_idx 3 == odd key
  130. odd_pcks[odd_packets].data = ts_packet + payload_ofs;
  131. odd_pcks[odd_packets].len = 188 - payload_ofs;
  132. odd_packets++;
  133. }
  134. ts_packet_set_not_scrambled(ts_packet);
  135. if (scramble_idx_old != scramble_idx && !ts->camd.constant_codeword) {
  136. struct timeval tv;
  137. gettimeofday(&tv, NULL);
  138. ts_LOGf("CWC | SID 0x%04x ------------ EcmTime: %5llu ms CW_time: %5llu ms\n",
  139. ts->service_id,
  140. timeval_diff_msec(&ts->ecm_change_time, &tv),
  141. timeval_diff_msec(&ts->key.ts_keyset, &tv));
  142. }
  143. scramble_idx_old = scramble_idx;
  144. } else {
  145. if (ts->pid_filter)
  146. ts_packet_set_pid(ts_packet, 0x1fff);
  147. }
  148. }
  149. }
  150. // Decode packets
  151. if (even_packets) {
  152. even_pcks[even_packets].data = NULL; // Last one...
  153. dvbcsa_bs_decrypt(ts->key.bs_csakey[0], even_pcks, 184);
  154. }
  155. if (odd_packets) {
  156. odd_pcks[odd_packets].data = NULL; // Last one...
  157. dvbcsa_bs_decrypt(ts->key.bs_csakey[1], odd_pcks, 184);
  158. }
  159. // Fill write buffer
  160. for (i=0; i<data_len; i += 188) {
  161. uint8_t *ts_packet = data + i;
  162. if (!ts->pid_filter) {
  163. cbuf_fill(ts->write_buf, ts_packet, 188);
  164. } else {
  165. uint16_t pid = ts_packet_get_pid(ts_packet);
  166. if (pidmap_get(&ts->pidmap, pid)) // PAT or allowed PIDs
  167. cbuf_fill(ts->write_buf, ts_packet, 188);
  168. }
  169. }
  170. }
  171. void *decode_thread(void *_ts) {
  172. struct ts *ts = _ts;
  173. uint8_t *data;
  174. int data_size;
  175. int req_size = 188 * dvbcsa_bs_batch_size();
  176. set_thread_name("tsdec-decode");
  177. while (!ts->decode_stop) {
  178. data = cbuf_peek(ts->decode_buf, req_size, &data_size);
  179. if (data_size < req_size) {
  180. usleep(1000);
  181. continue;
  182. }
  183. data = cbuf_get(ts->decode_buf, req_size, &data_size);
  184. if (data)
  185. decode_buffer(ts, data, data_size);
  186. }
  187. do { // Flush data
  188. data = cbuf_get(ts->decode_buf, req_size, &data_size);
  189. if (data)
  190. decode_buffer(ts, data, data_size);
  191. } while(data);
  192. return NULL;
  193. }
  194. static inline void output_write(struct ts *ts, uint8_t *data, unsigned int data_size) {
  195. if (!data)
  196. return;
  197. if (!ts->rtp_output) {
  198. write(ts->output.fd, data, data_size);
  199. } else {
  200. struct iovec iov[2];
  201. uint8_t rtp_header[12];
  202. uint32_t rtime = get_time() * 9 / 100;
  203. ts->rtp_seqnum++;
  204. rtp_header[ 0] = 0x80;
  205. rtp_header[ 1] = 33; // MPEG TS rtp payload type
  206. rtp_header[ 2] = ts->rtp_seqnum >> 8;
  207. rtp_header[ 3] = ts->rtp_seqnum & 0xff;
  208. rtp_header[ 4] = (rtime >> 24) & 0xff;
  209. rtp_header[ 5] = (rtime >> 16) & 0xff;
  210. rtp_header[ 6] = (rtime >> 8) & 0xff;
  211. rtp_header[ 7] = rtime & 0xff;
  212. rtp_header[ 8] = (ts->rtp_ssrc >> 24) & 0xff;
  213. rtp_header[ 9] = (ts->rtp_ssrc >> 16) & 0xff;
  214. rtp_header[10] = (ts->rtp_ssrc >> 8) & 0xff;
  215. rtp_header[11] = ts->rtp_ssrc & 0xff;
  216. iov[0].iov_base = rtp_header;
  217. iov[0].iov_len = sizeof(rtp_header);
  218. iov[1].iov_base = data;
  219. iov[1].iov_len = data_size;
  220. writev(ts->output.fd, iov, 2);
  221. }
  222. }
  223. void *write_thread(void *_ts) {
  224. struct ts *ts = _ts;
  225. uint8_t *data;
  226. int data_size;
  227. set_thread_name("tsdec-write");
  228. while (!ts->write_stop) {
  229. data_size = 0;
  230. data = cbuf_peek(ts->write_buf, FRAME_SIZE, &data_size);
  231. if (data_size < FRAME_SIZE) {
  232. usleep(5000);
  233. continue;
  234. }
  235. data = cbuf_get (ts->write_buf, FRAME_SIZE, &data_size);
  236. output_write(ts, data, data_size);
  237. }
  238. do { // Flush data
  239. data = cbuf_get(ts->write_buf, FRAME_SIZE, &data_size);
  240. output_write(ts, data, data_size);
  241. } while(data);
  242. return NULL;
  243. }
  244. static void detect_discontinuity(struct ts *ts, uint8_t *ts_packet) {
  245. uint16_t pid;
  246. uint8_t cur_cc, last_cc;
  247. if (!ts->ts_discont)
  248. return;
  249. pid = ts_packet_get_pid(ts_packet);
  250. cur_cc = ts_packet_get_cont(ts_packet);
  251. if (!pidmap_get(&ts->pid_seen, pid)) {
  252. if (strcmp(get_pid_desc(ts, pid), "Unknown") == 0)
  253. return;
  254. pidmap_set(&ts->pid_seen, pid);
  255. pidmap_set_val(&ts->cc, pid, cur_cc);
  256. ts_LOGf("NEW | Input PID 0x%04x appeared (%s)\n",
  257. pid, get_pid_desc(ts, pid));
  258. return;
  259. }
  260. last_cc = pidmap_get(&ts->cc, pid);
  261. if (last_cc != cur_cc && ((last_cc + 1) & 0x0f) != cur_cc)
  262. ts_LOGf("--- | TS discontinuity on PID 0x%04x expected %2d got %2d /%d/ (%s)\n",
  263. pid,
  264. ((last_cc + 1) & 0x0f), cur_cc,
  265. (cur_cc - ((last_cc + 1) & 0x0f)) & 0x0f,
  266. get_pid_desc(ts, pid));
  267. pidmap_set_val(&ts->cc, pid, cur_cc);
  268. }
  269. void process_packets(struct ts *ts, uint8_t *data, ssize_t data_len) {
  270. ssize_t i;
  271. int64_t now = get_time();
  272. for (i=0; i<data_len; i += 188) {
  273. uint8_t *ts_packet = data + i;
  274. uint16_t pid = ts_packet_get_pid(ts_packet);
  275. if (ts->pid_report)
  276. ts->pid_stats[pid]++;
  277. ts_pack_shown = 0;
  278. process_pat(ts, pid, ts_packet);
  279. process_cat(ts, pid, ts_packet);
  280. process_pmt(ts, pid, ts_packet);
  281. process_sdt(ts, pid, ts_packet);
  282. process_emm(ts, pid, ts_packet);
  283. process_ecm(ts, pid, ts_packet);
  284. detect_discontinuity(ts, ts_packet);
  285. if (!ts_pack_shown)
  286. dump_ts_pack(ts, pid, ts_packet);
  287. if (ts->emm_only)
  288. continue;
  289. // Return rewritten PAT
  290. if (pid == 0x00 && ts->pid_filter && ts->genpat->initialized) {
  291. if (!ts_packet_is_pusi(ts_packet))
  292. continue;
  293. ts_packet_set_cont(ts->genpat->section_header->packet_data, ts->genpat_cc);
  294. ts->genpat->ts_header.continuity = ts->genpat_cc;
  295. ts_packet = ts->genpat->section_header->packet_data;
  296. ts->genpat_cc = (ts->genpat_cc + 1) & 0x0f;
  297. }
  298. if (ts->threaded) {
  299. // Add to decode buffer. The decoder thread will handle it
  300. if (ts->input_buffer_time == 0) {
  301. // No input buffer, move packets to decoding buffer
  302. if (cbuf_fill(ts->decode_buf, ts_packet, 188) != 0) {
  303. ts_LOGf("Decode buffer is full, waiting...\n");
  304. cbuf_dump(ts->decode_buf);
  305. usleep(10000);
  306. }
  307. } else {
  308. // Handle input buffer
  309. struct packet_buf *p = malloc(sizeof(struct packet_buf));
  310. p->time = now + (ts->input_buffer_time * 1000); //buffer time is in ms, p->time is in us
  311. memcpy(p->data, ts_packet, 188);
  312. list_add(ts->input_buffer, p);
  313. // Move packets to decrypt buffer
  314. LNODE *lc, *lctmp;
  315. list_for_each(ts->input_buffer, lc, lctmp) {
  316. p = lc->data;
  317. if (p->time <= now) {
  318. if (cbuf_fill(ts->decode_buf, p->data, 188) != 0) {
  319. ts_LOGf("Decode buffer is full, waiting...\n");
  320. cbuf_dump(ts->decode_buf);
  321. usleep(10000);
  322. }
  323. list_del(ts->input_buffer, &lc);
  324. free(p);
  325. } else {
  326. break;
  327. }
  328. }
  329. }
  330. } else {
  331. int allowed_pid = pidmap_get(&ts->pidmap, pid);
  332. if (allowed_pid) // PAT or allowed PIDs
  333. decode_packet(ts, ts_packet);
  334. if (ts->pid_filter) {
  335. if (allowed_pid) // PAT or allowed PIDs
  336. output_write(ts, ts_packet, 188);
  337. } else {
  338. output_write(ts, ts_packet, 188);
  339. }
  340. }
  341. ts_pack++;
  342. }
  343. }
  344. void show_pid_report(struct ts *ts) {
  345. int i;
  346. if (!ts->pid_report)
  347. return;
  348. for (i = 0; i < MAX_PIDS; i++) {
  349. if (ts->pid_stats[i]) {
  350. ts_LOGf("PID | %8u packets with PID 0x%04x (%4u) %s\n",
  351. ts->pid_stats[i], i, i, get_pid_desc(ts, i));
  352. }
  353. }
  354. }