PageRenderTime 60ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 1ms

/dvbstream.c

https://bitbucket.org/bluzee/dvbstream
C | 1645 lines | 1244 code | 165 blank | 236 comment | 392 complexity | e436117d7f0f5a2bfb0c2fcaafdbb92b MD5 | raw file
Possible License(s): GPL-2.0
  1. /*
  2. dvbstream - RTP-ize a DVB transport stream.
  3. (C) Dave Chapman <dave@dchapman.com> 2001, 2002.
  4. The latest version can be found at http://www.linuxstb.org/dvbstream
  5. Copyright notice:
  6. This program is free software; you can redistribute it and/or modify
  7. it under the terms of the GNU General Public License as published by
  8. the Free Software Foundation; either version 2 of the License, or
  9. (at your option) any later version.
  10. This program is distributed in the hope that it will be useful,
  11. but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. GNU General Public License for more details.
  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., 675 Mass Ave, Cambridge, MA 02139, USA.
  17. */
  18. // Linux includes:
  19. #include <stdio.h>
  20. #include <stdlib.h>
  21. #include <ctype.h>
  22. #include <sys/ioctl.h>
  23. #include <sys/time.h>
  24. #include <sys/poll.h>
  25. #include <sys/stat.h>
  26. #include <resolv.h>
  27. #include <fcntl.h>
  28. #include <unistd.h>
  29. #include <signal.h>
  30. #include <values.h>
  31. #include <string.h>
  32. #include <sys/socket.h>
  33. #include <netinet/in.h>
  34. #include <netdb.h>
  35. // DVB includes:
  36. #include <linux/dvb/dmx.h>
  37. #include <linux/dvb/frontend.h>
  38. #include "rtp.h"
  39. #include "mpegtools/transform.h"
  40. #include "mpegtools/remux.h"
  41. #include "tune.h"
  42. // The default telnet port.
  43. #define DEFAULT_PORT 12345
  44. #define USAGE "\nUSAGE: dvbstream tpid1 tpid2 tpid3 .. tpid8\n\n"
  45. #define PACKET_SIZE 188
  46. // How often (in seconds) to update the "now" variable
  47. #define ALARM_TIME 5
  48. /* Thanks to Giancarlo Baracchino for this fix */
  49. #define MTU 1500
  50. #define IP_HEADER_SIZE 20
  51. #define UDP_HEADER_SIZE 8
  52. #define RTP_HEADER_SIZE 12
  53. #define MAX_RTP_SIZE (MTU-IP_HEADER_SIZE-UDP_HEADER_SIZE-RTP_HEADER_SIZE)
  54. #define writes(f,x) write((f),(x),strlen(x))
  55. /* Signal handling code shamelessly copied from VDR by Klaus Schmidinger
  56. - see http://www.cadsoft.de/people/kls/vdr/index.htm */
  57. unsigned int SLOF=(11700*1000UL);
  58. unsigned int LOF1=(5150*1000UL);
  59. unsigned int LOF2=(10750*1000UL);
  60. char* frontenddev[4]={"/dev/dvb/adapter0/frontend0","/dev/dvb/adapter1/frontend0","/dev/dvb/adapter2/frontend0","/dev/dvb/adapter3/frontend0"};
  61. char* dvrdev[4]={"/dev/dvb/adapter0/dvr0","/dev/dvb/adapter1/dvr0","/dev/dvb/adapter2/dvr0","/dev/dvb/adapter3/dvr0"};
  62. char* demuxdev[4]={"/dev/dvb/adapter0/demux0","/dev/dvb/adapter1/demux0","/dev/dvb/adapter2/demux0","/dev/dvb/adapter3/demux0"};
  63. int card=0;
  64. long now;
  65. long real_start_time;
  66. int Interrupted=0;
  67. fe_spectral_inversion_t specInv=INVERSION_AUTO;
  68. int tone=-1;
  69. fe_modulation_t modulation=CONSTELLATION_DEFAULT;
  70. fe_transmit_mode_t TransmissionMode=TRANSMISSION_MODE_DEFAULT;
  71. fe_bandwidth_t bandWidth=BANDWIDTH_DEFAULT;
  72. fe_guard_interval_t guardInterval=GUARD_INTERVAL_DEFAULT;
  73. fe_code_rate_t HP_CodeRate=HP_CODERATE_DEFAULT, LP_CodeRate=LP_CODERATE_DEFAULT;
  74. fe_hierarchy_t hier=HIERARCHY_DEFAULT;
  75. unsigned char diseqc=0;
  76. char pol=0;
  77. int streamtype = RTP;
  78. static int use_stdin=0;
  79. #define PID_MODE 0
  80. #define PROG_MODE 1
  81. static int selection_mode = PID_MODE;
  82. int open_fe(int* fd_frontend) {
  83. if((*fd_frontend = open(frontenddev[card],O_RDWR | O_NONBLOCK)) < 0){
  84. perror("FRONTEND DEVICE: ");
  85. return -1;
  86. }
  87. return 1;
  88. }
  89. static void SignalHandler(int signum) {
  90. struct timeval tv;
  91. if (signum == SIGALRM) {
  92. gettimeofday(&tv,(struct timezone*) NULL);
  93. now=tv.tv_sec-real_start_time;
  94. alarm(ALARM_TIME);
  95. } else if (signum != SIGPIPE) {
  96. Interrupted=signum;
  97. }
  98. signal(signum,SignalHandler);
  99. }
  100. long getmsec() {
  101. struct timeval tv;
  102. gettimeofday(&tv,(struct timezone*) NULL);
  103. return(tv.tv_sec%1000000)*1000 + tv.tv_usec/1000;
  104. }
  105. // There seems to be a limit of 16 simultaneous filters in the driver
  106. #define MAX_CHANNELS 16
  107. void set_ts_filt(int fd,uint16_t pid, dmx_pes_type_t pestype)
  108. {
  109. struct dmx_pes_filter_params pesFilterParams;
  110. //fprintf(stderr,"Setting filter for PID %d\n",pid);
  111. pesFilterParams.pid = pid;
  112. pesFilterParams.input = DMX_IN_FRONTEND;
  113. pesFilterParams.output = DMX_OUT_TS_TAP;
  114. pesFilterParams.pes_type = pestype;
  115. pesFilterParams.flags = DMX_IMMEDIATE_START;
  116. if (ioctl(fd, DMX_SET_PES_FILTER, &pesFilterParams) < 0) {
  117. fprintf(stderr,"Failed setting filter for pid %i: ",pid);
  118. perror("DMX SET PES FILTER");
  119. }
  120. }
  121. void make_nonblock(int f) {
  122. int oldflags;
  123. if ((oldflags=fcntl(f,F_GETFL,0)) < 0) {
  124. perror("F_GETFL");
  125. }
  126. oldflags|=O_NONBLOCK;
  127. if (fcntl(f,F_SETFL,oldflags) < 0) {
  128. perror("F_SETFL");
  129. }
  130. }
  131. typedef enum {STREAM_ON,STREAM_OFF} state_t;
  132. int socketIn, ns;
  133. int pids[MAX_CHANNELS];
  134. int pestypes[MAX_CHANNELS];
  135. unsigned char hi_mappids[8192];
  136. unsigned char lo_mappids[8192];
  137. int fd_frontend;
  138. int pid,pid2;
  139. int connectionOpen;
  140. int fromlen;
  141. char hostname[64];
  142. char in_ch;
  143. struct hostent *hp;
  144. struct sockaddr_in name, fsin;
  145. int ReUseAddr=1;
  146. int oldflags;
  147. int npids = 0;
  148. int fd[MAX_CHANNELS];
  149. int to_stdout = 0; /* to stdout instead of rtp stream */
  150. /* rtp */
  151. struct rtpheader hdr;
  152. struct sockaddr_in sOut;
  153. int socketOut;
  154. ipack pa, pv;
  155. #define IPACKS 2048
  156. #define TS_SIZE 188
  157. #define IN_SIZE TS_SIZE
  158. /*
  159. int process_telnet() {
  160. char cmd[1024];
  161. int cmd_i=0;
  162. int i;
  163. char* ch;
  164. dmx_pes_type_t pestype;
  165. unsigned long freq=0;
  166. unsigned long srate=0;
  167. // Open a new telnet session if a client is trying to connect
  168. if (ns==-1) {
  169. if ((ns = accept(socketIn, (struct sockaddr *)&fsin, &fromlen)) > 0) {
  170. make_nonblock(ns);
  171. cmd_i=0;
  172. cmd[0]=0;
  173. printf("Opened connection\n");
  174. writes(ns,"220-DVBSTREAM - ");
  175. writes(ns,hostname);
  176. writes(ns,"\r\nDONE\r\n");
  177. connectionOpen=1;
  178. }
  179. }
  180. // If a telnet session is open, receive and process any input
  181. if (connectionOpen) {
  182. // Read in at most a line of text - any ctrl character ends the line
  183. while (read(ns,&in_ch,1)>0) {
  184. if (in_ch < 32) break;
  185. // Prevent buffer overflows
  186. if (cmd_i < 1024-1) {
  187. cmd[cmd_i++]=in_ch;
  188. cmd[cmd_i]=0;
  189. }
  190. }
  191. if (in_ch > 0) {
  192. if (cmd_i > 0) {
  193. printf("CMD: \"%s\"\n",cmd);
  194. if (strcasecmp(cmd,"QUIT")==0) {
  195. writes(ns,"DONE\r\n");
  196. close(ns);
  197. ns=-1;
  198. connectionOpen=0;
  199. printf("Closed connection\n");
  200. } else if (strcasecmp(cmd,"STOP")==0) {
  201. writes(ns,"STOP\n");
  202. for (i=0;i<npids;i++) {
  203. if (ioctl(fd[i], DMX_STOP) < 0) {
  204. perror("DMX_STOP");
  205. }
  206. }
  207. for (i=0;i<8192;i++) {
  208. hi_mappids[i]=(i >> 8);
  209. lo_mappids[i]=(i&0xff);
  210. }
  211. writes(ns,"DONE\r\n");
  212. } else if (strncasecmp(cmd,"ADD",3)==0) {
  213. i=4;
  214. if ((cmd[3]=='V') || (cmd[3]=='v')) pestype=DMX_PES_VIDEO;
  215. else if ((cmd[3]=='A') || (cmd[3]=='a')) pestype=DMX_PES_AUDIO;
  216. else if ((cmd[3]=='T') || (cmd[3]=='t')) pestype=DMX_PES_TELETEXT;
  217. else { pestype=DMX_PES_OTHER; i=3; }
  218. while (cmd[i]==' ') i++;
  219. if ((ch=(char*)strstr(&cmd[i],":"))!=NULL) {
  220. pid2=atoi(&ch[1]);
  221. ch[0]=0;
  222. } else {
  223. pid2=-1;
  224. }
  225. pid=atoi(&cmd[i]);
  226. if (pid) {
  227. if (npids == MAX_CHANNELS) {
  228. fprintf(stderr,"\nsorry, you can only set up to 8 filters.\n\n");
  229. return(-1);
  230. } else {
  231. pestypes[npids]=pestype;
  232. pestype=DMX_PES_OTHER;
  233. pids[npids]=pid;
  234. if (pid2!=-1) {
  235. hi_mappids[pid]=pid2>>8;
  236. lo_mappids[pid]=pid2&0xff;
  237. fprintf(stderr,"Mapping %d to %d\n",pid,pid2);
  238. }
  239. if((fd[npids] = open(demuxdev[card],O_RDWR|O_NONBLOCK)) < 0){
  240. fprintf(stderr,"FD %i: ",i);
  241. perror("DEMUX DEVICE: ");
  242. } else {
  243. set_ts_filt(fd[npids],pids[npids],pestypes[npids]);
  244. npids++;
  245. }
  246. }
  247. }
  248. writes(ns,"DONE\r\n");
  249. } else if (strcasecmp(cmd,"START")==0) {
  250. writes(ns,"START\n");
  251. for (i=0;i<npids;i++) {
  252. set_ts_filt(fd[i],pids[i],pestypes[i]);
  253. }
  254. writes(ns,"DONE\r\n");
  255. } else if (strncasecmp(cmd,"TUNE",4)==0) {
  256. for (i=0;i<8192;i++) {
  257. hi_mappids[i]=(i >> 8);
  258. lo_mappids[i]=(i&0xff);
  259. }
  260. for (i=0;i<npids;i++) {
  261. if (ioctl(fd[i], DMX_STOP) < 0) {
  262. perror("DMX_STOP");
  263. close(fd[i]);
  264. }
  265. }
  266. npids=0;
  267. i=4;
  268. while (cmd[i]==' ') i++;
  269. freq=atoi(&cmd[i]);
  270. while ((cmd[i]!=' ') && (cmd[i]!=0)) i++;
  271. if (cmd[i]!=0) {
  272. while (cmd[i]==' ') i++;
  273. pol=cmd[i];
  274. while ((cmd[i]!=' ') && (cmd[i]!=0)) i++;
  275. if (cmd[i]!=0) {
  276. while (cmd[i]==' ') i++;
  277. srate=atoi(&cmd[i])*1000UL;
  278. fprintf(stderr,"Tuning to %ld,%ld,%c\n",freq,srate,pol);
  279. if(!use_stdin)
  280. tune_it(fd_frontend,freq,srate,pol,tone,specInv,diseqc,modulation,HP_CodeRate,TransmissionMode,guardInterval,bandWidth, LP_CodeRate, hier);
  281. }
  282. }
  283. }
  284. cmd_i=0;
  285. cmd[0]=0;
  286. writes(ns,"DONE\r\n");
  287. }
  288. }
  289. }
  290. return(0);
  291. }
  292. */
  293. /* The output routine for sending a PS */
  294. void my_write_out(uint8_t *buf, int count,void *p)
  295. {
  296. /* to fix: change this buffer size and check for overflow */
  297. static uint8_t out_buffer[1000000];
  298. static int out_buffer_n=0;
  299. int i;
  300. if (to_stdout) {
  301. /* This one is easy. */
  302. write(STDOUT_FILENO, buf, count);
  303. } else { /* We are streaming it. */
  304. /* Copy data to write to the end of out_buffer */
  305. memcpy(&out_buffer[out_buffer_n],buf,count);
  306. out_buffer_n+=count;
  307. /* Send as many full packets as possible */
  308. i=0;
  309. while ((i + MAX_RTP_SIZE) < out_buffer_n) {
  310. hdr.timestamp = getmsec()*90;
  311. sendrtp2(socketOut,&sOut,&hdr,&out_buffer[i],MAX_RTP_SIZE);
  312. i+=MAX_RTP_SIZE;
  313. }
  314. /* Move whatever data is left to the start of the buffer */
  315. memmove(&out_buffer[0],&out_buffer[i],out_buffer_n-i);
  316. out_buffer_n-=i;
  317. }
  318. }
  319. void my_ts_to_ps( uint8_t* buf, uint16_t pida, uint16_t pidv)
  320. {
  321. uint16_t pid;
  322. ipack *p;
  323. uint8_t off = 0;
  324. pid = get_pid(buf+1);
  325. if (!(buf[3]&0x10)) // no payload?
  326. return;
  327. if (pid == pidv){
  328. p = &pv;
  329. } else {
  330. if (pid == pida){
  331. p = &pa;
  332. } else return;
  333. }
  334. if ( buf[1]&0x40) {
  335. if (p->plength == MMAX_PLENGTH-6){
  336. p->plength = p->found-6;
  337. p->found = 0;
  338. send_ipack(p);
  339. reset_ipack(p);
  340. }
  341. }
  342. if ( buf[3] & 0x20) { // adaptation field?
  343. off = buf[4] + 1;
  344. }
  345. instant_repack(buf+4+off, TS_SIZE-4-off, p);
  346. }
  347. typedef uint8_t PID_BIT_MAP[1024];
  348. static PID_BIT_MAP SI_PIDS;
  349. static PID_BIT_MAP USER_PIDS;
  350. typedef struct {
  351. char *filename;
  352. int fd;
  353. int pids[MAX_CHANNELS];
  354. int num;
  355. int pid_cnt;
  356. int progs[MAX_CHANNELS];
  357. int progs_cnt;
  358. uint8_t **prognames;
  359. int prognames_cnt;
  360. PID_BIT_MAP pidmap;
  361. long start_time; // in seconds
  362. long end_time; // in seconds
  363. int socket;
  364. struct rtpheader hdr;
  365. struct sockaddr_in sOut;
  366. unsigned char buf[MTU];
  367. unsigned char net[20];
  368. int pos;
  369. int port;
  370. } pids_map_t;
  371. pids_map_t *pids_map;
  372. int map_cnt;
  373. //1024 section payload +1 pointer +256 pointer value
  374. #define SECTION_LEN 1281
  375. typedef struct {
  376. uint8_t buf[SECTION_LEN];
  377. unsigned int pos;
  378. } section_t;
  379. typedef struct {
  380. int program;
  381. int pmt_pid;
  382. } pat_entry;
  383. static struct {
  384. int len; //section length
  385. int version;
  386. section_t section;
  387. pat_entry *entries;
  388. int entries_cnt;
  389. } PAT;
  390. #define MAX_PIDS 202
  391. typedef struct {
  392. section_t section;
  393. int version;
  394. int pids[MAX_PIDS];
  395. int pids_cnt;
  396. uint8_t name[256];
  397. } pmt_t;
  398. struct {
  399. pmt_t *entries;
  400. int cnt;
  401. } PMT;
  402. static struct {
  403. int len; //section length
  404. int version;
  405. section_t section;
  406. } SDT;
  407. static int SI_fd[MAX_CHANNELS];
  408. static int SI_fd_cnt = 0;
  409. #define SDT_PID 0x11
  410. #define getbit(buf, pid) (buf[(pid)/8] & (1 << ((pid) % 8)))
  411. #define mysetbit(buf, pid) buf[(pid)/8] |= (1 << ((pid) % 8))
  412. #define clearbits(buf) memset(buf, 0, sizeof(PID_BIT_MAP))
  413. #define setallbits(buf) memset(buf, 0xFF, sizeof(PID_BIT_MAP))
  414. #define min(x, y) ((x) <= (y) ? (x) : (y))
  415. void update_bitmaps()
  416. {
  417. int i, j, k, n;
  418. for(i = 0; i < map_cnt; i++)
  419. {
  420. clearbits(pids_map[i].pidmap);
  421. mysetbit(pids_map[i].pidmap, 0);
  422. for(j = 0; j < MAX_CHANNELS; j++)
  423. {
  424. if(pids_map[i].pids[j] == -1) break;
  425. if(pids_map[i].pids[j] == 8192)
  426. {
  427. setallbits(pids_map[i].pidmap);
  428. break;
  429. }
  430. mysetbit(pids_map[i].pidmap, pids_map[i].pids[j]);
  431. for(k = 0; k < PMT.cnt; k++)
  432. {
  433. for(n = 0; n < PMT.entries[k].pids_cnt; n++)
  434. {
  435. if(PMT.entries[k].pids[n] == pids_map[i].pids[j])
  436. {
  437. //add the pmt_pid to the map
  438. //fprintf(stderr, "ADDING TO map %d PMT n. %d with PID: %d, j: %d\n", i, k, PAT.entries[k].pmt_pid, j);
  439. mysetbit(pids_map[i].pidmap, PAT.entries[k].pmt_pid);
  440. }
  441. }
  442. }
  443. }
  444. }
  445. for(j = 0; j < map_cnt; j++)
  446. {
  447. for(k = 0; k < pids_map[j].progs_cnt; k++)
  448. {
  449. for(i = 0; i < PAT.entries_cnt; i++)
  450. {
  451. if(pids_map[j].progs[k] == PAT.entries[i].program)
  452. {
  453. mysetbit(pids_map[j].pidmap, PAT.entries[i].pmt_pid);
  454. mysetbit(pids_map[j].pidmap, SDT_PID);
  455. for(n = 0; n < PMT.entries[i].pids_cnt; n++)
  456. {
  457. int pid = PMT.entries[i].pids[n];
  458. mysetbit(pids_map[j].pidmap, pid);
  459. //fprintf(stderr, "\nADDED to map %d PROG pid %d, prog: %d", j, pid, PAT.entries[i].program);
  460. }
  461. }
  462. }
  463. }
  464. }
  465. for(i = 0; i < map_cnt; i++)
  466. {
  467. for(j = 0; j < pids_map[i].prognames_cnt; j++)
  468. {
  469. for(k = 0; k < PMT.cnt; k++)
  470. {
  471. if(!strcmp(pids_map[i].prognames[j], PMT.entries[k].name))
  472. {
  473. mysetbit(pids_map[i].pidmap, PAT.entries[k].pmt_pid);
  474. mysetbit(pids_map[i].pidmap, SDT_PID);
  475. for(n = 0; n < PMT.entries[k].pids_cnt; n++)
  476. {
  477. int pid = PMT.entries[k].pids[n];
  478. mysetbit(pids_map[i].pidmap, pid);
  479. //fprintf(stderr, "\nADDED to map %d PROG pid %d, prog: %d", j, pid, PAT.entries[k].program);
  480. }
  481. }
  482. }
  483. }
  484. }
  485. }
  486. static int collect_section(section_t *section, int pusi, uint8_t *buf, unsigned int len)
  487. {
  488. int skip, slen;
  489. uint8_t *ptr;
  490. if(pusi)
  491. section->pos = 0;
  492. if(section->pos + len > SECTION_LEN)
  493. return 0;
  494. memcpy(&(section->buf[section->pos]), buf, len);
  495. section->pos += len;
  496. skip = section->buf[0];
  497. if(skip + 4 > section->pos)
  498. return 0;
  499. ptr = &(section->buf[skip + 1]);
  500. slen = ((ptr[1] & 0x0f) << 8) | ptr[2];
  501. if(section->pos < (skip+1+3+slen))
  502. return 0;
  503. return skip+1;
  504. }
  505. static int parse_pat(int pusi, uint8_t *b, int l)
  506. {
  507. unsigned int i, j, vers, seclen, num, skip;
  508. uint8_t *buf;
  509. skip = collect_section(&PAT.section, pusi, b, l);
  510. if(!skip)
  511. return 0;
  512. //now we know the section is complete
  513. PAT.section.pos = 0;
  514. buf = &(PAT.section.buf[skip]);
  515. if(buf[0] != 0) //pat id
  516. return 0;
  517. if(!(buf[5] & 1)) //not yet valid
  518. return 0;
  519. vers = (buf[5] >> 1) & 0x1F;
  520. if(PAT.version == vers) //PAT didn't change
  521. return 1;
  522. clearbits(SI_PIDS);
  523. mysetbit(SI_PIDS, 0);
  524. mysetbit(SI_PIDS, SDT_PID);
  525. seclen = ((buf[1] & 0x0F) << 8) | buf[2];
  526. num = (seclen - 9) / 4;
  527. if(PAT.entries_cnt != num)
  528. {
  529. PAT.entries = realloc(PAT.entries, sizeof(pat_entry)*num);
  530. PAT.entries_cnt = num;
  531. PMT.entries = realloc(PMT.entries, sizeof(pmt_t)*num);
  532. if(!PMT.entries) return 0;
  533. PMT.cnt = num;
  534. }
  535. i = 8;
  536. j = 0;
  537. for(j=0; j<num; j++)
  538. {
  539. PAT.entries[j].program = (buf[i] << 8) | buf[i+1];
  540. PAT.entries[j].pmt_pid = ((buf[i+2] & 0x1F) << 8) | buf[i+3];
  541. mysetbit(SI_PIDS, PAT.entries[j].pmt_pid);
  542. i += 4;
  543. //fprintf(stderr, "PROGRAM: %d, pmt_pid: %d\n", PAT.entries[j].program, PAT.entries[j].pmt_pid);
  544. PMT.entries[j].section.pos = SECTION_LEN+1;
  545. PMT.entries[j].version = -1;
  546. PMT.entries[j].name[0] = 0;
  547. }
  548. SDT.version=-1;
  549. SDT.section.pos = SECTION_LEN+1;
  550. PAT.version = vers;
  551. return 2;
  552. }
  553. static void add_pmt_pids()
  554. {
  555. int i;
  556. PID_BIT_MAP simap;
  557. for(i = 0; i < SI_fd_cnt; i++)
  558. close(SI_fd[i]);
  559. clearbits(simap);
  560. mysetbit(simap, 0);
  561. for(i=0; i<min(PAT.entries_cnt, MAX_CHANNELS); i++)
  562. {
  563. if(getbit(USER_PIDS, PAT.entries[i].pmt_pid)) continue;
  564. if(getbit(simap, PAT.entries[i].pmt_pid)) continue;
  565. if((SI_fd[i] = open(demuxdev[card], O_RDWR|O_NONBLOCK)) < 0)
  566. {
  567. fprintf(stderr,"COULDN'T OPEN DEMUX %i: for pid: %d", i, PAT.entries[i].pmt_pid);
  568. return;
  569. }
  570. //fprintf(stderr, "\nADDED PMT PID: %d\n", PAT.entries[i].pmt_pid);
  571. set_ts_filt(SI_fd[i], PAT.entries[i].pmt_pid, DMX_PES_OTHER);
  572. mysetbit(simap, PAT.entries[i].pmt_pid);
  573. SI_fd_cnt++;
  574. }
  575. }
  576. static int parse_pmt(int pusi, pmt_t *pmt, uint8_t *b, int l)
  577. {
  578. unsigned int i, version, seclen, skip, prog, pcr_pid, pid;
  579. uint8_t *buf;
  580. skip = collect_section(&(pmt->section), pusi, b, l);
  581. if(!skip)
  582. return 0;
  583. //now we know the section is complete
  584. pmt->section.pos = 0;
  585. pmt->pids_cnt = 0;
  586. buf = &(pmt->section.buf[skip]);
  587. if(buf[0] != 2) //pmt id
  588. return 0;
  589. if(!(buf[5] & 1)) //not yet valid
  590. return 0;
  591. prog = (buf[3] << 8) | buf[4];
  592. version = (buf[5] >> 1) & 0x1F;
  593. if(pmt->version == version) //PMT didn't change
  594. return 1;
  595. seclen = ((buf[1] & 0x0F) << 8) | buf[2];
  596. pcr_pid = ((buf[8] & 0x1F) << 8) | buf[9];
  597. pmt->pids[pmt->pids_cnt++] = pcr_pid;
  598. //fprintf(stderr, "\nPROGRAM: %d, pcr_pid: %d, version: %d vs %d\n", prog, pcr_pid, pmt->version, version);
  599. skip = ((buf[10] & 0x0F) << 8) | buf[11];
  600. if(skip+12 > seclen)
  601. return 0;
  602. i = skip+12;
  603. while(i+5<seclen)
  604. {
  605. pid = ((buf[i+1] & 0x1F) << 8) | buf[i+2];
  606. pmt->pids[pmt->pids_cnt++] = pid;
  607. skip = ((buf[i+3] & 0x0F) << 8) | buf[i+4];
  608. i += skip+5;
  609. //fprintf(stderr, "prog %d, PID: %d, count: %d, type: 0x%x\n", prog, pid, pmt->pids_cnt, buf[i]);
  610. }
  611. pmt->version = version;
  612. return 2;
  613. }
  614. static int parse_sdt(int pusi, uint8_t *b, int l)
  615. {
  616. unsigned int i, version, seclen, skip, prog, k, descr_len, found, len;
  617. uint8_t *buf;
  618. skip = collect_section(&(SDT.section), pusi, b, l);
  619. if(!skip)
  620. return 0;
  621. buf = &(SDT.section.buf[skip]);
  622. if(buf[0] != 0x42) //pmt id
  623. return 0;
  624. if(!(buf[5] & 1)) //not yet valid
  625. return 0;
  626. version = (buf[5] >> 1) & 0x1F;
  627. if(SDT.version == version) //SDT didn't change
  628. return 1;
  629. seclen = ((buf[1] & 0x0F) << 8) | buf[2];
  630. if(seclen < 12)
  631. return 0;
  632. i = 11;
  633. while(i < seclen - 4)
  634. {
  635. descr_len = ((buf[i+3] & 0x0F) << 8) | buf[i+4];
  636. if(i+5+descr_len >= seclen)
  637. break;
  638. prog = (buf[i] << 8) | buf[i+1];
  639. found = -1;
  640. k = 0;
  641. for(k = 0; k < PAT.entries_cnt, k < PMT.cnt; k++)
  642. if(PAT.entries[k].program == prog)
  643. {
  644. found = k;
  645. if(k != -1)
  646. {
  647. int j, len, dlen;
  648. j = i + 5;
  649. len = dlen = 0;
  650. while(len < descr_len)
  651. {
  652. int provider_len, name_len, n;
  653. pmt_t *pmt;
  654. n = j;
  655. dlen = buf[n+1];
  656. if(len + dlen > descr_len) break;
  657. if(buf[n] == 0x48)
  658. {
  659. provider_len = buf[n+3];
  660. if(provider_len + 2 > dlen)
  661. break;
  662. n += 4 + provider_len;
  663. name_len = buf[n];
  664. if(provider_len + 3 + name_len > dlen)
  665. break;
  666. pmt = &PMT.entries[k];
  667. memcpy(pmt->name, &buf[n+1], name_len);
  668. pmt->name[name_len] = 0;
  669. fprintf(stderr, "Program n. %d, name: '%s'\n", prog, pmt->name);
  670. }
  671. len += dlen+2;
  672. j += dlen+2;
  673. }
  674. }
  675. }
  676. i += 5 + descr_len;
  677. }
  678. SDT.version = version;
  679. return 2;
  680. }
  681. static int parse_ts_packet(uint8_t *buf)
  682. {
  683. int pid, l, af, pusi;
  684. if(buf[0] != 0x47)
  685. return 0;
  686. pusi = buf[1] & 0x40;
  687. pid = ((buf[1] & 0x1F) << 8) | buf[2];
  688. af = (buf[3] >> 4) & 0x03;
  689. l = 4;
  690. if(af == 2) //only adaption
  691. return 0;
  692. else if(af == 3)
  693. l += buf[4] + 1;
  694. if(l >= TS_SIZE - 4)
  695. return 0;
  696. if(pid == 0)
  697. {
  698. if(parse_pat(pusi, &buf[l], TS_SIZE - l) == 2)
  699. add_pmt_pids();
  700. }
  701. else if(pid == SDT_PID)
  702. {
  703. if(parse_sdt(pusi, &buf[l], TS_SIZE - l) == 2)
  704. {
  705. int i;
  706. for(i = 0; i < PMT.cnt; i++)
  707. {
  708. PMT.entries[i].section.pos = SECTION_LEN+1;
  709. PMT.entries[i].version = -1;
  710. }
  711. update_bitmaps();
  712. }
  713. }
  714. else
  715. {
  716. int i;
  717. for(i=0; i<PAT.entries_cnt; i++)
  718. {
  719. if(pid==PAT.entries[i].pmt_pid)
  720. {
  721. if(parse_pmt(pusi, &PMT.entries[i], &buf[l], TS_SIZE - l) == 2)
  722. update_bitmaps();
  723. }
  724. }
  725. }
  726. }
  727. static int is_string(char *s)
  728. {
  729. int i, n, len;
  730. n = 0;
  731. len = strlen(s);
  732. for(i = 0; i < len; i++)
  733. if(isdigit(s[i]))
  734. n++;
  735. return (n != len);
  736. }
  737. int main(int argc, char **argv)
  738. {
  739. // state_t state=STREAM_OFF;
  740. #ifdef ENABLE_TELNET
  741. unsigned short int port=DEFAULT_PORT;
  742. #endif
  743. int fd_dvr;
  744. int i,j;
  745. uint8_t buf[MTU];
  746. struct pollfd pfds[2]; // DVR device and Telnet connection
  747. unsigned int secs = -1;
  748. unsigned long freq=0;
  749. unsigned long srate=1000000;
  750. int count;
  751. char* ch;
  752. dmx_pes_type_t pestype;
  753. int bytes_read;
  754. int do_analyse=0;
  755. unsigned char* free_bytes;
  756. int output_type=RTP_TS;
  757. int64_t counts[8192];
  758. double f;
  759. long start_time=-1;
  760. long end_time=-1;
  761. struct timeval tv;
  762. int found;
  763. unsigned int delsys = SYS_DVBS;
  764. int stream_whole_TS=0;
  765. /* Output: {uni,multi,broad}cast socket */
  766. char ipOut[20];
  767. int portOut;
  768. int ttl = 2;
  769. pids_map = NULL;
  770. map_cnt = 0;
  771. fprintf(stderr,"dvbstream v0.7 - (C) Dave Chapman 2001-2004\n");
  772. fprintf(stderr,"Released under the GPL.\n");
  773. fprintf(stderr,"*WARNING* Diseqc switch numbering has changed. See help.\n");
  774. /* Initialise PID map */
  775. for (i=0;i<8192;i++) {
  776. hi_mappids[i]=(i >> 8);
  777. lo_mappids[i]=(i&0xff);
  778. counts[i]=0;
  779. }
  780. memset(&PAT, 0, sizeof(PAT));
  781. PAT.version = -1;
  782. PAT.section.pos = SECTION_LEN+1;
  783. memset(&PMT, 0, sizeof(PMT));
  784. clearbits(SI_PIDS);
  785. mysetbit(SI_PIDS, 0);
  786. memset(&SDT, 0, sizeof(SDT));
  787. SDT.section.pos = SECTION_LEN+1;
  788. SDT.version = -1;
  789. mysetbit(SI_PIDS, SDT_PID);
  790. clearbits(USER_PIDS);
  791. mysetbit(USER_PIDS, 0);
  792. /* Set default IP and port */
  793. strcpy(ipOut,"127.0.0.1");
  794. portOut = 1234;
  795. if (argc==1) {
  796. fprintf(stderr,"Usage: dvbstream [OPTIONS] pid1 pid2 ... pid8\n\n");
  797. fprintf(stderr,"-i IP multicast address\n");
  798. fprintf(stderr,"-r IP multicast port\n");
  799. fprintf(stderr,"-net ip:prt IP address:port combination to be followed by pids list. Can be repeated to generate multiple RTP streams\n");
  800. fprintf(stderr,"-o Stream to stdout instead of network\n");
  801. fprintf(stderr,"-o:file.ts Stream to named file instead of network\n");
  802. fprintf(stderr,"-n secs Stop after secs seconds\n");
  803. fprintf(stderr,"-from n Start saving the file previously specified with -o: syntax in n minutes time\n");
  804. fprintf(stderr,"-to n Stop saving the file previously specified with -o: syntax in n minutes time\n");
  805. fprintf(stderr,"-ps Convert stream to Program Stream format (needs exactly 2 pids)\n");
  806. fprintf(stderr,"-v vpid Decode video PID (full cards only)\n");
  807. fprintf(stderr,"-a apid Decode audio PID (full cards only)\n");
  808. fprintf(stderr,"-t ttpid Decode teletext PID (full cards only)\n");
  809. fprintf(stderr,"\nStandard tuning options:\n\n");
  810. fprintf(stderr,"-f freq absolute Frequency (DVB-S in Hz or DVB-T in Hz)\n");
  811. fprintf(stderr," or L-band Frequency (DVB-S in Hz or DVB-T in Hz)\n");
  812. fprintf(stderr," or C Band 4 digits Mhz Ku Band 5 digits Mhz\n");
  813. fprintf(stderr,"-SL slof S-LOF(DVB-S only)\n");
  814. fprintf(stderr,"-L1 LOF1 LOF1(DVB-S only C Band default 5150)\n");
  815. fprintf(stderr,"-L2 LOF2 LOF2(DVB-S only Ku Band default 10750)\n");
  816. fprintf(stderr,"-p [H,V] Polarity (DVB-S only)\n");
  817. fprintf(stderr,"-s N Symbol rate (DVB-S or DVB-C default 1000000)\n");
  818. fprintf(stderr,"-S system System 1=DVBS 2=DVBS2 3=ATSC 4=DVBT (Default DVBS)\n");
  819. fprintf(stderr,"\nAdvanced tuning options:\n\n");
  820. fprintf(stderr,"-c [0-3] Use DVB card #[0-3]\n");
  821. fprintf(stderr,"-D [0-4AB] DiSEqC command (Burst A or B, Commited 0-3, 4=None )\n\n");
  822. fprintf(stderr,"-I [0|1|2] 0=Spectrum Inversion off, 1=Spectrum Inversion on, 2=auto\n");
  823. fprintf(stderr,"-qam X DVB-T/C and ATSC modulation - 16%s, 32%s, 64%s, 128%s or 256%s\n",(CONSTELLATION_DEFAULT==QAM_16 ? " (default)" : ""),(CONSTELLATION_DEFAULT==QAM_32 ? " (default)" : ""),(CONSTELLATION_DEFAULT==QAM_64 ? " (default)" : ""),(CONSTELLATION_DEFAULT==QAM_128 ? " (default)" : ""),(CONSTELLATION_DEFAULT==QAM_256 ? " (default)" : ""));
  824. #ifdef DVB_ATSC
  825. fprintf(stderr,"-vsb X ATSC modulation - 8, 16\n");
  826. #endif
  827. fprintf(stderr,"-gi N DVB-T guard interval 1_N (N=32%s, 16%s, 8%s or 4%s)\n",(GUARD_INTERVAL_DEFAULT==GUARD_INTERVAL_1_32 ? " (default)" : ""),(GUARD_INTERVAL_DEFAULT==GUARD_INTERVAL_1_16 ? " (default)" : ""),(GUARD_INTERVAL_DEFAULT==GUARD_INTERVAL_1_8 ? " (default)" : ""),(GUARD_INTERVAL_DEFAULT==GUARD_INTERVAL_1_4 ? " (default)" : ""));
  828. fprintf(stderr,"-cr N DVB-T/C code rate. N=AUTO%s, 1_2%s, 2_3%s, 3_4%s, 5_6%s, 7_8%s\n",(HP_CODERATE_DEFAULT==FEC_AUTO ? " (default)" : ""),(HP_CODERATE_DEFAULT==FEC_1_2 ? " (default)" : ""),(HP_CODERATE_DEFAULT==FEC_2_3 ? " (default)" : ""),(HP_CODERATE_DEFAULT==FEC_3_4 ? " (default)" : ""),(HP_CODERATE_DEFAULT==FEC_5_6 ? " (default)" : ""),(HP_CODERATE_DEFAULT==FEC_7_8 ? " (default)" : ""));
  829. fprintf(stderr,"-crlp N DVB-T code rate LP. N=AUTO%s, 1_2%s, 2_3%s, 3_4%s, 5_6%s, 7_8%s\n",(LP_CODERATE_DEFAULT==FEC_AUTO ? " (default)" : ""),(LP_CODERATE_DEFAULT==FEC_1_2 ? " (default)" : ""),(LP_CODERATE_DEFAULT==FEC_2_3 ? " (default)" : ""),(LP_CODERATE_DEFAULT==FEC_3_4 ? " (default)" : ""),(LP_CODERATE_DEFAULT==FEC_5_6 ? " (default)" : ""),(LP_CODERATE_DEFAULT==FEC_7_8 ? " (default)" : ""));
  830. fprintf(stderr,"-bw N DVB-T bandwidth (Mhz) - N=6%s, 7%s or 8%s\n",(BANDWIDTH_DEFAULT==BANDWIDTH_6_MHZ ? " (default)" : ""),(BANDWIDTH_DEFAULT==BANDWIDTH_7_MHZ ? " (default)" : ""),(BANDWIDTH_DEFAULT==BANDWIDTH_8_MHZ ? " (default)" : ""));
  831. fprintf(stderr,"-tm N DVB-T transmission mode - N=2%s or 8%s\n",(TRANSMISSION_MODE_DEFAULT==TRANSMISSION_MODE_2K ? " (default)" : ""),(TRANSMISSION_MODE_DEFAULT==TRANSMISSION_MODE_8K ? " (default)" : ""));
  832. fprintf(stderr,"-hy N DVB-T hierarchy - N=1%s, 2%s, 4%s, NONE%s or AUTO%s\n",(HIERARCHY_DEFAULT==HIERARCHY_1 ? " (default)" : ""),(HIERARCHY_DEFAULT==HIERARCHY_2 ? " (default)" : ""),(HIERARCHY_DEFAULT==HIERARCHY_4 ? " (default)" : ""),(HIERARCHY_DEFAULT==HIERARCHY_NONE ? " (default)" : ""),(HIERARCHY_DEFAULT==HIERARCHY_AUTO ? " (default)" : ""));
  833. fprintf(stderr,"-ttl N Sets TTL to N (default: 2) when streaming in RTP\n");
  834. fprintf(stderr,"-rtp Sets output type to RTP (default when using network out)\n");
  835. fprintf(stderr,"-udp Sets output type to UDP \n");
  836. fprintf(stderr,"-prog Selects PROGRAM mode (opens a demux on the whole TS)\n");
  837. fprintf(stderr,"-pid Selects PID mode (default)\n");
  838. fprintf(stderr,"-stdin Use STDIN as source rather than a DVB card\n");
  839. fprintf(stderr,"\n-analyse Perform a simple analysis of the bitrates of the PIDs in the transport stream\n");
  840. fprintf(stderr,"\n");
  841. fprintf(stderr,"NOTE: Use pid1=8192 to broadcast whole TS stream from a budget card\n");
  842. return(-1);
  843. } else {
  844. pids[0]=0;
  845. npids=1;
  846. pestype=DMX_PES_OTHER; // Default PES type
  847. for (i=1;i<argc;i++) {
  848. if (strcmp(argv[i],"-ps")==0) {
  849. output_type=RTP_PS;
  850. } else if(!strcmp(argv[i],"-rtp")) {
  851. streamtype = RTP;
  852. } else if(!strcmp(argv[i],"-udp")) {
  853. streamtype = UDP;
  854. } else if (strcmp(argv[i],"-analyse")==0) {
  855. do_analyse=1;
  856. output_type=RTP_NONE;
  857. if (secs==-1) { secs=10; }
  858. } else if(strcmp(argv[i],"-stdin")==0) {
  859. use_stdin = 1;
  860. } else if (strcmp(argv[i],"-i")==0) {
  861. if(pids_map != NULL) {
  862. fprintf(stderr, "ERROR! -i and -r can't be used with -o and -net. Use -net instead\n");
  863. exit(1);
  864. }
  865. i++;
  866. strcpy(ipOut,argv[i]);
  867. } else if(strcmp(argv[i],"-auto")==0) {
  868. modulation = QAM_AUTO;
  869. TransmissionMode = TRANSMISSION_MODE_AUTO;
  870. guardInterval = GUARD_INTERVAL_AUTO;
  871. HP_CodeRate = FEC_AUTO;
  872. LP_CodeRate = FEC_AUTO;
  873. hier = HIERARCHY_AUTO;
  874. specInv = INVERSION_AUTO;
  875. }
  876. else if (strcmp(argv[i],"-r")==0) {
  877. if(pids_map != NULL) {
  878. fprintf(stderr, "ERROR! -i and -r can't be used with -o and -net. Use -net instead\n");
  879. exit(1);
  880. }
  881. i++;
  882. portOut=atoi(argv[i]);
  883. } else if (strcmp(argv[i],"-net")==0) {
  884. char *tmp;
  885. i++;
  886. tmp = strchr(argv[i], ':');
  887. if((tmp == NULL) || (tmp - argv[i] > 19)) {
  888. fprintf(stderr, "No valid IP:port found after -net switch, discarding\n");
  889. } else {
  890. int len = (int) (tmp - argv[i]);
  891. char addr[20];
  892. int port;
  893. strncpy(ipOut, argv[i], len);
  894. ipOut[len] = 0;
  895. strncpy(addr, argv[i], len);
  896. addr[len] = 0;
  897. port = portOut = atoi(tmp+1);
  898. pids_map = (pids_map_t*) realloc(pids_map, sizeof(pids_map_t) * (map_cnt+1));
  899. if(pids_map != NULL) {
  900. map_cnt++;
  901. pids_map[map_cnt-1].pid_cnt = 0;
  902. pids_map[map_cnt-1].progs_cnt = 0;
  903. pids_map[map_cnt-1].start_time=start_time;
  904. pids_map[map_cnt-1].end_time=end_time;
  905. for(j=0; j < MAX_CHANNELS; j++) pids_map[map_cnt-1].pids[j] = -1;
  906. pids_map[map_cnt-1].filename = NULL;
  907. strncpy(pids_map[map_cnt-1].net, addr, len);
  908. pids_map[map_cnt-1].net[len] = 0;
  909. pids_map[map_cnt-1].port = port;
  910. pids_map[map_cnt-1].pos = 0;
  911. pids_map[map_cnt-1].socket = makesocket(addr,port,ttl,&(pids_map[map_cnt-1].sOut));
  912. initrtp(&(pids_map[map_cnt-1].hdr),(output_type==RTP_TS ? 33 : 34), streamtype);
  913. output_type = MAP_TS;
  914. } else
  915. fprintf(stderr, "Couldn't alloc enough entry for this %s:%d address\n", addr, port);
  916. }
  917. } else if (strcmp(argv[i],"-f")==0) {
  918. i++;
  919. freq=atoi(argv[i]);
  920. } else if (strcmp(argv[i],"-p")==0) {
  921. i++;
  922. if (argv[i][1]==0) {
  923. if (tolower(argv[i][0])=='v') {
  924. pol='V';
  925. } else if (tolower(argv[i][0])=='h') {
  926. pol='H';
  927. }
  928. }
  929. } else if (strcmp(argv[i],"-SL")==0) {
  930. i++;
  931. SLOF=atoi(argv[i]);
  932. SLOF*=1000UL;
  933. } else if (strcmp(argv[i],"-L1")==0) {
  934. i++;
  935. LOF1=atoi(argv[i]);
  936. LOF1*=1000UL;
  937. } else if (strcmp(argv[i],"-L2")==0) {
  938. i++;
  939. LOF2=atoi(argv[i]);
  940. LOF2*=1000UL;
  941. }
  942. else if (strcmp(argv[i],"-s")==0) {
  943. i++;
  944. srate=atoi(argv[i])*1000UL;
  945. }
  946. else if (strcmp(argv[i],"-D")==0)
  947. {
  948. i++;
  949. diseqc = argv[i][0];
  950. if(toupper(diseqc) == 'A')
  951. diseqc = 'A';
  952. else if(toupper(diseqc) == 'B')
  953. diseqc = 'B';
  954. else if(diseqc >= '0' && diseqc <= '4') {
  955. diseqc=diseqc - '0';
  956. }
  957. else {
  958. fprintf(stderr,"DiSEqC must be between 0 and 4 or A | B\n");
  959. exit(-1);
  960. }
  961. } else if (strcmp(argv[i],"-I")==0) {
  962. i++;
  963. if (atoi(argv[i])==0)
  964. specInv = INVERSION_OFF;
  965. else if (atoi(argv[i])==1)
  966. specInv = INVERSION_ON;
  967. else
  968. specInv = INVERSION_AUTO;
  969. }
  970. else if (strcmp(argv[i],"-S")==0) {
  971. i++;
  972. switch(atoi(argv[i])) {
  973. case 1: delsys=SYS_DVBS; fprintf(stderr,"Tuning DVB-S...\n"); break;
  974. case 2: delsys=SYS_DVBS2; fprintf(stderr,"Tuning DVB-S2...\n"); break;
  975. case 3: delsys=SYS_ATSC; fprintf(stderr,"Tuning ATSC...\n");break;
  976. case 4: delsys=SYS_DVBT; fprintf(stderr,"Tuning DVB-T...\n");break;
  977. default:
  978. fprintf(stderr,"Invalid Delivery System %s\n",argv[i]);
  979. exit(0);
  980. }
  981. }
  982. else if(strcmp(argv[i],"-prog")==0) {
  983. selection_mode = PROG_MODE;
  984. }
  985. else if(strcmp(argv[i],"-pid")==0) {
  986. selection_mode = PID_MODE;
  987. }
  988. else if (strcmp(argv[i],"-o")==0) {
  989. to_stdout = 1;
  990. } else if (strcmp(argv[i],"-n")==0) {
  991. i++;
  992. secs=atoi(argv[i]);
  993. } else if (strcmp(argv[i],"-c")==0) {
  994. i++;
  995. card=atoi(argv[i]);
  996. if ((card < 0) || (card > 3)) {
  997. fprintf(stderr,"ERROR: card parameter must be between 0 and 4\n");
  998. }
  999. } else if (strcmp(argv[i],"-v")==0) {
  1000. pestype=DMX_PES_VIDEO;
  1001. } else if (strcmp(argv[i],"-a")==0) {
  1002. pestype=DMX_PES_AUDIO;
  1003. } else if (strcmp(argv[i],"-t")==0) {
  1004. pestype=DMX_PES_TELETEXT;
  1005. } else if (strcmp(argv[i],"-qam")==0) {
  1006. i++;
  1007. switch(atoi(argv[i])) {
  1008. case 16: modulation=QAM_16; break;
  1009. case 32: modulation=QAM_32; break;
  1010. case 64: modulation=QAM_64; break;
  1011. case 128: modulation=QAM_128; break;
  1012. case 256: modulation=QAM_256; break;
  1013. default:
  1014. fprintf(stderr,"Invalid QAM rate: %s\n",argv[i]);
  1015. exit(0);
  1016. }
  1017. }
  1018. #ifdef DVB_ATSC
  1019. else if(strcmp(argv[i],"-vsb")==0) {
  1020. i++;
  1021. switch(atoi(argv[i])) {
  1022. case 8: modulation=VSB_8; break;
  1023. case 16: modulation=VSB_16; break;
  1024. default:
  1025. fprintf(stderr,"Invalid ATSC VSB modulation: %s\n",argv[i]);
  1026. exit(0);
  1027. }
  1028. }
  1029. #endif
  1030. else if (strcmp(argv[i],"-gi")==0) {
  1031. i++;
  1032. switch(atoi(argv[i])) {
  1033. case 32: guardInterval=GUARD_INTERVAL_1_32; break;
  1034. case 16: guardInterval=GUARD_INTERVAL_1_16; break;
  1035. case 8: guardInterval=GUARD_INTERVAL_1_8; break;
  1036. case 4: guardInterval=GUARD_INTERVAL_1_4; break;
  1037. default:
  1038. fprintf(stderr,"Invalid Guard Interval: %s\n",argv[i]);
  1039. exit(0);
  1040. }
  1041. } else if (strcmp(argv[i],"-tm")==0) {
  1042. i++;
  1043. switch(atoi(argv[i])) {
  1044. case 8: TransmissionMode=TRANSMISSION_MODE_8K; break;
  1045. case 2: TransmissionMode=TRANSMISSION_MODE_2K; break;
  1046. default:
  1047. fprintf(stderr,"Invalid Transmission Mode: %s\n",argv[i]);
  1048. exit(0);
  1049. }
  1050. } else if (strcmp(argv[i],"-bw")==0) {
  1051. i++;
  1052. switch(atoi(argv[i])) {
  1053. case 8: bandWidth=BANDWIDTH_8_MHZ; break;
  1054. case 7: bandWidth=BANDWIDTH_7_MHZ; break;
  1055. case 6: bandWidth=BANDWIDTH_6_MHZ; break;
  1056. default:
  1057. fprintf(stderr,"Invalid DVB-T bandwidth: %s\n",argv[i]);
  1058. exit(0);
  1059. }
  1060. } else if (strcmp(argv[i],"-cr")==0) {
  1061. i++;
  1062. if (!strcmp(argv[i],"AUTO")) {
  1063. HP_CodeRate=FEC_AUTO;
  1064. } else if (!strcmp(argv[i],"1_2")) {
  1065. HP_CodeRate=FEC_1_2;
  1066. } else if (!strcmp(argv[i],"2_3")) {
  1067. HP_CodeRate=FEC_2_3;
  1068. } else if (!strcmp(argv[i],"3_4")) {
  1069. HP_CodeRate=FEC_3_4;
  1070. } else if (!strcmp(argv[i],"5_6")) {
  1071. HP_CodeRate=FEC_5_6;
  1072. } else if (!strcmp(argv[i],"7_8")) {
  1073. HP_CodeRate=FEC_7_8;
  1074. } else {
  1075. fprintf(stderr,"Invalid Code Rate: %s\n",argv[i]);
  1076. exit(0);
  1077. }
  1078. } else if (strcmp(argv[i],"-crlp")==0) {
  1079. i++;
  1080. if (!strcmp(argv[i],"AUTO")) {
  1081. LP_CodeRate=FEC_AUTO;
  1082. } else if (!strcmp(argv[i],"1_2")) {
  1083. LP_CodeRate=FEC_1_2;
  1084. } else if (!strcmp(argv[i],"2_3")) {
  1085. LP_CodeRate=FEC_2_3;
  1086. } else if (!strcmp(argv[i],"3_4")) {
  1087. LP_CodeRate=FEC_3_4;
  1088. } else if (!strcmp(argv[i],"5_6")) {
  1089. LP_CodeRate=FEC_5_6;
  1090. } else if (!strcmp(argv[i],"7_8")) {
  1091. LP_CodeRate=FEC_7_8;
  1092. } else {
  1093. fprintf(stderr,"Invalid Code Rate LP: %s\n",argv[i]);
  1094. exit(0);
  1095. }
  1096. } else if (strcmp(argv[i],"-hier")==0) {
  1097. i++;
  1098. if (!strcmp(argv[i],"AUTO")) {
  1099. hier=HIERARCHY_AUTO;
  1100. } else if (!strcmp(argv[i],"1")) {
  1101. hier=HIERARCHY_2;
  1102. } else if (!strcmp(argv[i],"4")) {
  1103. hier=HIERARCHY_4;
  1104. } else if (!strcmp(argv[i],"NONE")) {
  1105. hier=HIERARCHY_NONE;
  1106. } else {
  1107. fprintf(stderr,"Invalid HIERARCHY: %s\n",argv[i]);
  1108. exit(0);
  1109. }
  1110. } else if (strcmp(argv[i],"-ttl")==0) {
  1111. i++;
  1112. ttl = atoi(argv[i]);
  1113. } else if (strcmp(argv[i],"-from")==0) {
  1114. i++;
  1115. if (map_cnt) {
  1116. pids_map[map_cnt-1].start_time=atoi(argv[i])*60;
  1117. } else {
  1118. start_time=atoi(argv[i])*60;
  1119. }
  1120. } else if (strcmp(argv[i],"-to")==0) {
  1121. i++;
  1122. if (map_cnt) {
  1123. pids_map[map_cnt-1].end_time=atoi(argv[i])*60;
  1124. } else {
  1125. end_time=atoi(argv[i])*60;
  1126. secs=end_time;
  1127. }
  1128. } else if (strstr(argv[i], "-o:")==argv[i]) {
  1129. if (strlen(argv[i]) > 3) {
  1130. char * fname;
  1131. fname = (char *) malloc(strlen(argv[i]) - 2);
  1132. if(fname == NULL) {
  1133. fprintf(stderr, "Couldn't alloc enough memory for this -o: entry, discarding\n");
  1134. } else {
  1135. strcpy(fname, &argv[i][3]);
  1136. pids_map = (pids_map_t*) realloc(pids_map, sizeof(pids_map_t) * (map_cnt+1));
  1137. if(pids_map != NULL) {
  1138. map_cnt++;
  1139. pids_map[map_cnt-1].pid_cnt = 0;
  1140. pids_map[map_cnt-1].start_time=start_time;
  1141. pids_map[map_cnt-1].end_time=end_time;
  1142. for(j=0; j < MAX_CHANNELS; j++) pids_map[map_cnt-1].pids[j] = -1;
  1143. pids_map[map_cnt-1].filename = fname;
  1144. output_type = MAP_TS;
  1145. } else
  1146. fprintf(stderr, "Couldn't alloc enough memory for file %s: entry, discarding\n", fname);
  1147. }
  1148. }
  1149. } else {
  1150. if ((ch=(char*)strstr(argv[i],":"))!=NULL) {
  1151. pid2=atoi(&ch[1]);
  1152. ch[0]=0;
  1153. } else {
  1154. pid2=-1;
  1155. }
  1156. pid=atoi(argv[i]);
  1157. // If we are currently processing a "-o:" option:
  1158. if (map_cnt) {
  1159. if(selection_mode == PID_MODE) {
  1160. // block for the map
  1161. found = 0;
  1162. for (j=0;j<MAX_CHANNELS;j++) {
  1163. if(pids_map[map_cnt-1].pids[j] == pid) found = 1;
  1164. }
  1165. if (found == 0) {
  1166. if(pids_map[map_cnt-1].pid_cnt==0) {
  1167. pids_map[map_cnt-1].pids[0]=0;
  1168. pids_map[map_cnt-1].pid_cnt++;
  1169. }
  1170. if(pid==8192) {
  1171. fprintf(stderr, "Adding whole transport stream to map n. %d\n", map_cnt-1);
  1172. setallbits(USER_PIDS);
  1173. }
  1174. pids_map[map_cnt-1].pids[pids_map[map_cnt-1].pid_cnt] = pid;
  1175. pids_map[map_cnt-1].pid_cnt++;
  1176. }
  1177. }
  1178. else {
  1179. // block for the map
  1180. int is_progname = is_string(argv[i]);
  1181. pids_map_t *map = &(pids_map[map_cnt-1]);
  1182. stream_whole_TS=1;
  1183. setallbits(USER_PIDS);
  1184. found = 0;
  1185. if(is_progname) {
  1186. for(j=0;j<map->prognames_cnt;j++) {
  1187. if(!strcmp(map->prognames[j], argv[i]))
  1188. found = 1;
  1189. }
  1190. if(found == 0) {
  1191. map->prognames = realloc(map->prognames, (map->prognames_cnt+1)*sizeof(map->prognames));
  1192. map->prognames_cnt++;
  1193. map->prognames[map->prognames_cnt-1] = malloc(strlen(argv[i])+1);
  1194. strcpy(map->prognames[map->prognames_cnt-1], argv[i]);
  1195. }
  1196. }
  1197. else {
  1198. for (j=0;j<MAX_CHANNELS;j++) {
  1199. if(pids_map[map_cnt-1].progs[j] == pid) found = 1;
  1200. }
  1201. if(found == 0)
  1202. pids_map[map_cnt-1].progs[pids_map[map_cnt-1].progs_cnt++] = pid;
  1203. }
  1204. }
  1205. }
  1206. if(selection_mode == PID_MODE) {
  1207. // block for the list of pids to demux
  1208. found = 0;
  1209. for (j=0;j<npids;j++) {
  1210. if(pids[j] == pid) found = 1;
  1211. }
  1212. if (found==0) {
  1213. if (npids == MAX_CHANNELS) {
  1214. fprintf(stderr,"\nSorry, you can only set up to %d filters.\n\n",MAX_CHANNELS);
  1215. return(-1);
  1216. } else {
  1217. pestypes[npids]=pestype;
  1218. pestype=DMX_PES_OTHER;
  1219. pids[npids++]=pid;
  1220. if (pid2!=-1) {
  1221. hi_mappids[pid]=pid2>>8;
  1222. lo_mappids[pid]=pid2&0xff;
  1223. fprintf(stderr,"Mapping %d to %d\n",pid,pid2);
  1224. }
  1225. }
  1226. }
  1227. }
  1228. }
  1229. }
  1230. }
  1231. if ((output_type==RTP_PS) && (npids!=3)) {
  1232. fprintf(stderr,"ERROR: PS requires exactly two PIDS - video and audio.\n");
  1233. exit(1);
  1234. }
  1235. for (i=0;i<map_cnt;i++) {
  1236. if(pids_map[i].filename) {
  1237. FILE *f;
  1238. f = fopen(pids_map[i].filename, "w+b");
  1239. if (f != NULL) {
  1240. pids_map[i].fd = fileno(f);
  1241. make_nonblock(pids_map[i].fd);
  1242. fprintf(stderr, "Open file %s\n", pids_map[i].filename);
  1243. } else {
  1244. pids_map[i].fd = -1;
  1245. fprintf(stderr, "Couldn't open file %s, errno:%d\n", pids_map[map_cnt-1].filename, errno);
  1246. }
  1247. }
  1248. }
  1249. update_bitmaps();
  1250. if (signal(SIGHUP, SignalHandler) == SIG_IGN) signal(SIGHUP, SIG_IGN);
  1251. if (signal(SIGINT, SignalHandler) == SIG_IGN) signal(SIGINT, SIG_IGN);
  1252. if (signal(SIGTERM, SignalHandler) == SIG_IGN) signal(SIGTERM, SIG_IGN);
  1253. if (signal(SIGALRM, SignalHandler) == SIG_IGN) signal(SIGALRM, SIG_IGN);
  1254. alarm(ALARM_TIME);
  1255. if (freq!=0 && !use_stdin) {
  1256. if (open_fe(&fd_frontend)) {
  1257. fprintf(stderr,"Tuning to %ld Hz\n",freq);
  1258. i=tune_it(fd_frontend,freq,srate,pol,tone,specInv,diseqc,modulation,HP_CodeRate,TransmissionMode,guardInterval,bandWidth, LP_CodeRate, hier, delsys);
  1259. }
  1260. }
  1261. // if (i<0) { exit(i); }
  1262. if(map_cnt > 0)
  1263. fprintf(stderr, "\n");
  1264. for (i=0;i<map_cnt;i++) {
  1265. if ((secs==-1) || (secs < pids_map[i].end_time)) { secs=pids_map[i].end_time; }
  1266. if(pids_map[i].filename != NULL)
  1267. fprintf(stderr,"MAP %d, file %s: From %ld secs, To %ld secs, %d PIDs - ",i,pids_map[i].filename,pids_map[i].start_time,pids_map[i].end_time,pids_map[i].pid_cnt);
  1268. else
  1269. fprintf(stderr,"MAP %d, addr %s:%d From %ld secs, To %ld secs, %d PIDs - ",i,pids_map[i].net,pids_map[i].port,pids_map[i].start_time,pids_map[i].end_time,pids_map[i].pid_cnt);
  1270. for (j=0;j<MAX_CHANNELS;j++) { if (pids_map[i].pids[j]!=-1) fprintf(stderr," %d",pids_map[i].pids[j]); }
  1271. fprintf(stderr,"\n");
  1272. }
  1273. fprintf(stderr,"dvbstream will stop after %d seconds (%d minutes)\n",secs,secs/60);
  1274. if(stream_whole_TS) {
  1275. npids=1;
  1276. pids[0] = 8192;
  1277. }
  1278. else
  1279. for(i=0; i<npids; i++) {
  1280. if(pids[i] == 8192) {
  1281. npids = 1;
  1282. pids[0] = 8192;
  1283. }
  1284. }
  1285. if(use_stdin) {
  1286. fd_dvr = fileno(stdin);
  1287. } else {
  1288. for (i=0;i<npids;i++) {
  1289. if((fd[i] = open(demuxdev[card],O_RDWR|O_NONBLOCK)) < 0){
  1290. fprintf(stderr,"FD %i: ",i);
  1291. perror("DEMUX DEVICE: ");
  1292. return -1;
  1293. }
  1294. }
  1295. if((fd_dvr = open(dvrdev[card],O_RDONLY|O_NONBLOCK)) < 0){
  1296. perror("DVR DEVICE: ");
  1297. return -1;
  1298. }
  1299. /* Now we set the filters */
  1300. for (i=0;i<npids;i++) {
  1301. set_ts_filt(fd[i],pids[i],pestypes[i]);
  1302. mysetbit(USER_PIDS, pids[i]);
  1303. }
  1304. }
  1305. gettimeofday(&tv,(struct timezone*) NULL);
  1306. real_start_time=tv.tv_sec;
  1307. now=0;
  1308. if (do_analyse) {
  1309. fprintf(stderr,"Analysing PIDS\n");
  1310. } else {
  1311. if (to_stdout) {
  1312. fprintf(stderr,"Output to stdout\n");
  1313. }
  1314. else if(! map_cnt) {
  1315. ttl = 2;
  1316. fprintf(stderr,"Using %s:%d:%d\n",ipOut,portOut,ttl);
  1317. /* Init RTP */
  1318. socketOut = makesocket(ipOut,portOut,ttl,&sOut);
  1319. // #warning WHAT SHOULD THE PAYLOAD TYPE BE FOR "MPEG-2 PS" ?
  1320. initrtp(&hdr,(output_type==RTP_TS ? 33 : 34), streamtype);
  1321. fprintf(stderr,"version=%X\n",hdr.b.v);
  1322. }
  1323. fprintf(stderr,"Streaming %d stream%s\n",npids,(npids==1 ? "" : "s"));
  1324. }
  1325. if (output_type==RTP_PS) {
  1326. init_ipack(&pa, IPACKS,my_write_out, 1);
  1327. init_ipack(&pv, IPACKS,my_write_out, 1);
  1328. }
  1329. /* Read packets */
  1330. free_bytes = buf;
  1331. /*
  1332. #ifdef ENABLE_TELNET
  1333. // Setup socket to accept input from a client //
  1334. gethostname(hostname, sizeof(hostname));
  1335. if ((hp = gethostbyname(hostname)) == NULL) {
  1336. fprintf(stderr, "%s: host unknown.\n", hostname);
  1337. exit(1);
  1338. }
  1339. if ((socketIn = socket(PF_INET, SOCK_STREAM, 0)) < 0) {
  1340. perror("server: socket");
  1341. exit(1);
  1342. }
  1343. setsockopt(socketIn,SOL_SOCKET,SO_REUSEADDR,&ReUseAddr,sizeof(ReUseAddr));
  1344. name.sin_family = AF_INET;
  1345. name.sin_port = htons(port);
  1346. name.sin_addr.s_addr = htonl(INADDR_ANY);
  1347. if (bind(socketIn,(struct sockaddr* )&name,sizeof(name)) < 0) {
  1348. perror("server: bind");
  1349. exit(1);
  1350. }
  1351. make_nonblock(socketIn);
  1352. if (listen(socketIn, 1) < 0) {
  1353. perror("server: listen");
  1354. exit(1);
  1355. }
  1356. #endif
  1357. */
  1358. connectionOpen=0;
  1359. ns=-1;
  1360. pfds[0].fd=fd_dvr;
  1361. pfds[0].events=POLLIN|POLLPRI;
  1362. pfds[1].events=POLLIN|POLLPRI;
  1363. while ( !Interrupted) {
  1364. /* Poll the open file descriptors */
  1365. if (ns==-1) {
  1366. poll(pfds,1,500);
  1367. } else {
  1368. pfds[1].fd=ns; // This can change
  1369. poll(pfds,2,500);
  1370. }
  1371. // process_telnet(); // See if there is an incoming telnet connection
  1372. if (output_type==RTP_TS) {
  1373. /* Attempt to read 188 bytes from /dev/ost/dvr */
  1374. if ((bytes_read = read(fd_dvr,free_bytes,PACKET_SIZE)) > 0) {
  1375. if (bytes_read!=PACKET_SIZE) {
  1376. fprintf(stderr,"No bytes left to read - aborting\n");
  1377. break;
  1378. }
  1379. pid=((free_bytes[1]&0x1f) << 8) | (free_bytes[2]);
  1380. free_bytes[1]=(free_bytes[1]&0xe0)|hi_mappids[pid];
  1381. free_bytes[2]=lo_mappids[pid];
  1382. free_bytes+=bytes_read;
  1383. // If there isn't enough room for 1 more packet, then send it.
  1384. if ((free_bytes+PACKET_SIZE-buf)>MAX_RTP_SIZE) {
  1385. hdr.timestamp = getmsec()*90;
  1386. if (to_stdout) {
  1387. write(1, buf, free_bytes-buf);
  1388. } else {
  1389. sendrtp2(socketOut,&sOut,&hdr,buf,free_bytes-buf);
  1390. }
  1391. free_bytes = buf;
  1392. }
  1393. count++;
  1394. }
  1395. } else if (output_type==RTP_PS) {
  1396. if (read(fd_dvr,buf,TS_SIZE) > 0) {
  1397. my_ts_to_ps((uint8_t*)buf, pids[1], pids[2]);
  1398. } else if(use_stdin) break;
  1399. } else if(output_type==MAP_TS) {
  1400. int bytes_read;
  1401. bytes_read = read(fd_dvr, buf, TS_SIZE);
  1402. if(bytes_read > 0) {
  1403. if(buf[0] == 0x47) {
  1404. int pid, i;
  1405. pid = ((buf[1] & 0x1f) << 8) | buf[2];
  1406. if(getbit(SI_PIDS, pid)) parse_ts_packet(buf);
  1407. if (pids_map != NULL) {
  1408. for (i = 0; i < map_cnt; i++) {
  1409. if ( ((pids_map[i].start_time==-1) || (pids_map[i].start_time <= now))
  1410. && ((pids_map[i].end_time==-1) || (pids_map[i].end_time >= now))) {
  1411. if(getbit(pids_map[i].pidmap, pid)) {
  1412. errno = 0;
  1413. if(pids_map[i].filename)
  1414. write(pids_map[i].fd, buf, TS_SIZE);
  1415. else {
  1416. if((pids_map[i].pos + PACKET_SIZE) > MAX_RTP_SIZE) {
  1417. hdr.timestamp = getmsec()*90;
  1418. sendrtp2(pids_map[i].socket, &(pids_map[i].sOut), &(pids_map[i].hdr), pids_map[i].buf, pids_map[i].pos);
  1419. pids_map[i].pos = 0;
  1420. }
  1421. memcpy(&(pids_map[i].buf[pids_map[i].pos]), buf, bytes_read);
  1422. pids_map[i].pos += bytes_read;
  1423. }
  1424. }
  1425. }
  1426. }
  1427. }
  1428. } else {
  1429. fprintf(stderr, "NON 0X47\n");
  1430. }
  1431. }
  1432. else if(use_stdin) break;
  1433. } else {
  1434. if (do_analyse) {
  1435. if (read(fd_dvr,buf,TS_SIZE) > 0) {
  1436. pid=((buf[1]&0x1f) << 8) | (buf[2]);
  1437. counts[pid]++;
  1438. }
  1439. }
  1440. }
  1441. if ((secs!=-1) && (secs <=now)) { Interrupted=1; }
  1442. }
  1443. if (Interrupted) {
  1444. fprintf(stderr,"Caught signal %d - closing cleanly.\n",Interrupted);
  1445. }
  1446. if (ns!=-1) close(ns);
  1447. close(socketIn);
  1448. if (!to_stdout && !map_cnt) close(socketOut);
  1449. if(!use_stdin) {
  1450. for (i=0;i<npids;i++) close(fd[i]);
  1451. close(fd_dvr);
  1452. close(fd_frontend);
  1453. }
  1454. if (do_analyse) {
  1455. for (i=0;i<8192;i++) {
  1456. if (counts[i]) {
  1457. f=(counts[i]*184.0*8.0)/(secs*1024.0*1024.0);
  1458. if (f >= 1.0) {
  1459. fprintf(stdout,"%d,%.3f Mbit/s\n",i,f);
  1460. } else {
  1461. fprintf(stdout,"%d,%.0f kbit/s\n",i,f*1024);
  1462. }
  1463. }
  1464. }
  1465. }
  1466. return(0);
  1467. }