]> git.vomp.tv Git - vompserver.git/blob - libdvbmpeg/DVB.hh
Initial import
[vompserver.git] / libdvbmpeg / DVB.hh
1 #ifndef _DVB_DEV_HH_
2 #define _DVB_DEV_HH_
3
4 extern "C" {
5 #include <asm/errno.h>
6 #include <fcntl.h>
7 #include <netdb.h>
8 #include <netinet/in.h>
9 #include <stdint.h>
10 #include <stdio.h>
11 #include <sys/ioctl.h>
12 #include <sys/poll.h>
13 #include <sys/socket.h>
14 #include <sys/stat.h>
15 #include <sys/time.h>
16 #include <sys/types.h>
17 #include <sys/uio.h>
18 #include <sys/un.h>
19 #include <time.h>
20 #include <unistd.h>
21
22 #define NEWSTRUCT
23 #include <channel.h>
24 }
25
26 #include <sstream>
27 #include <iostream>
28 #include <iomanip>
29 using namespace std;
30
31 #include <osd.hh>
32 #include <devices.hh>
33
34 #ifndef MAXNAM
35 #define MAXNAM 80
36 #endif
37
38
39
40 #define FRONT_DVBS 1
41 #define FRONT_DVBC 2
42 #define FRONT_DVBT 3
43
44 #define VTXDIR "/var/vtx"
45
46 #define DEC(N) dec << setw(N) << setfill('0') 
47 #define HEX(N) hex << setw(N) << setfill('0') 
48
49 #define MAXSECSIZE 4096
50
51 #define NK 10
52 enum {LNB=0,DIS,ROTOR,TRANS,CHAN,BOU,SAT,PICS,SWI,NTW};
53 static const int nums[]={LNB,DIS,ROTOR,TRANS,CHAN,BOU,SAT,PICS,SWI,NTW};
54 static const int maxs[]={ 32, 32,   32,  512,16384,512,100, 50, 10, 100};
55
56 #define MAX_TRANS_CHAN  1024
57
58 enum{DVB_ORIG=0, DVB_NOKIA, DVB_XML, DVB_SATCO};
59
60 typedef struct frontend_stat_s{
61         fe_status_t status;
62         uint16_t snr;
63         uint16_t strength;
64         uint32_t ber;
65         uint32_t u_blocks;
66 } frontend_stat;
67
68
69 extern uint8_t hamtab[256];
70 extern uint8_t invtab[256];
71
72 #define MAX_MAG 8
73 typedef struct mag_struct_ {
74         int valid;
75         int magn;
76         uint8_t flags;
77         uint8_t lang;
78         int pnum,sub;
79         uint8_t pagebuf[25*40];
80 } magazin_t;
81
82
83 class DVB {
84 public:
85         int no_open;
86         int fd_frontend;
87         int fd_demuxa;
88         int fd_demuxv;
89         int fd_demuxpcr;
90         int fd_demuxtt;
91         int fdvb;
92
93         int minor;
94         int adapter;
95         int max_tpid;
96         int max_satid;
97         int max_chanid;
98         
99         frontend_stat festat;
100
101         struct dvb_diseqc_master_cmd dcmd;
102         fe_sec_tone_mode_t tone;
103         fe_sec_voltage_t voltage;
104         int burst;
105         struct dmx_pes_filter_params pesFilterParamsV; 
106         struct dmx_pes_filter_params pesFilterParamsA; 
107         struct dmx_pes_filter_params pesFilterParamsP; 
108         struct dmx_pes_filter_params pesFilterParamsTT; 
109         struct dvb_frontend_parameters front_param;
110         int front_type;
111         int dvr_enabled;
112         OSD osd;
113         uint32_t transponder_freq;
114         char transponder_pol;
115         uint32_t transponder_srate;
116
117
118
119         fe_status_t status;
120         uint32_t ber, uncorrected_blocks;
121         uint16_t snr, signal;
122
123
124         struct Lnb *lnbs;
125         struct DiSEqC *diseqcs;
126         struct Rotor *rotors;
127         struct Transponder *tps;
128         struct Channel *chans;
129         struct Bouquet *bouqs;
130         struct Sat *sats;
131         struct Picture *pics;
132         struct Switch *swis;
133         struct Network *ntws;
134         int num[NK];
135         int oldsec;
136         int tryit;
137         int oldpol;
138
139         char *vtxdir;
140         magazin_t magazin[MAX_MAG]; 
141
142         DVB(){
143                 no_open = 0;
144                 max_tpid = 0;
145                 max_satid = 0;
146                 max_chanid = 0;
147                 minor = 0;
148
149                 fd_frontend = -1;
150                 fd_demuxa = -1;
151                 fd_demuxpcr = -1;
152                 fd_demuxv = -1;
153                 fd_demuxtt = -1;
154                 fdvb = -1;
155                 vtxdir = NULL;
156                 transponder_freq=0;
157                 transponder_pol=0;
158                 transponder_srate=0;
159         }
160
161         DVB(int i){
162                 if (i >= 0) 
163                         no_open = 0;
164                 else 
165                         no_open = 1;
166                 max_tpid = 0;
167                 max_satid = 0;
168                 max_chanid = 0;
169         
170                 fd_frontend = -1;
171                 fd_demuxa = -1;
172                 fd_demuxpcr = -1;
173                 fd_demuxv = -1;
174                 fd_demuxtt = -1;
175                 fdvb = -1;
176                 vtxdir = NULL;
177                 transponder_freq=0;
178                 transponder_pol=0;
179                 transponder_srate=0;
180
181                 init("","",i);
182         }
183
184         DVB(char *a, char *b) {
185                 max_tpid = 0;
186                 max_satid = 0;
187                 max_chanid = 0;
188         
189                 fd_frontend = -1;
190                 fd_demuxa = -1;
191                 fd_demuxpcr = -1;
192                 fd_demuxv = -1;
193                 fd_demuxtt = -1;
194
195                 fdvb = -1;
196                 vtxdir = NULL;
197                 init(a,b,0);
198         }
199
200         ~DVB();
201   
202         void use_osd(int fd = -1){
203                 char dvn[32];
204                 if (no_open) return;
205                 if (fd < 0) fd = 0;
206                 sprintf(dvn,OSD_DEV,adapter,fd);
207                 fdvb = open(dvn, O_RDWR);
208                 
209                 if (fdvb >= 0){
210                         cerr << dvn <<  " for OSD" << endl;
211                         osd.init(fdvb);
212                 } else perror("osd");
213                 osd.Open(80, 500, 640, 540, 2, 0, 2);
214                 osd.SetColor(0, 0, 0, 0, 255);
215                 osd.SetColor(1, 240, 240, 240, 255);
216                 osd.Show();
217         }
218
219         void set_vtxdir(char *newname){
220                 if (!newname) return;
221                 if (vtxdir) free(vtxdir);
222                 vtxdir = (char *) malloc(sizeof(char)*(strlen(newname)+1));
223                 if (vtxdir)
224                         strncpy(vtxdir, newname, strlen(newname));
225         }
226
227         void close_osd(){
228                 osd.Close(fdvb);
229                 close(fdvb);
230         }
231   
232         int DVR_enabled(){
233                 if (no_open) return -1;
234                 return dvr_enabled;
235         }
236
237         void enable_DVR(){
238                 if (no_open) return;
239                 dvr_enabled = 1;
240         }
241
242         void enable_DVR_other(){
243                 if (no_open) return;
244                 dvr_enabled = 2;
245         }
246
247         void disable_DVR(){
248                 if (no_open) return;
249                 dvr_enabled = 0;
250         }
251
252         void init(char *a="/dev/video0", char *b="/dev/vbi0",int adapt=0,
253                   int minor = 0); 
254                   
255
256         inline void init(char *a, char *b){
257                 if (no_open) return;
258                 init(a,b,0,0);
259         }
260
261         int check_frontend();
262
263         void set_apid(ushort apid);
264         void set_vpid(ushort vpid); 
265         void set_pcrpid(ushort vpid); 
266         void set_ttpid(ushort ttpid); 
267         int set_apid_fd(ushort apid, int fd);
268         int set_vpid_fd(ushort vpid, int fd);
269         int set_ttpid_fd(ushort ttpid, int fd);
270         int set_pcrpid_fd(ushort pcrpid, int fd);
271         int set_otherpid_fd(ushort otherpid, int fd);
272
273
274         int set_lnb(int dis);
275         void set_diseqc_nb(int nr); 
276         int set_front(void); 
277         void get_front(void); 
278
279         void scan_pf_eit(int chnr,
280                          int (*callback)(uint8_t *data, int l, int pnr, 
281                                           int c_n, uint8_t *t));
282
283         void scan_pf_eit(Channel *chan, 
284                          int (*callback)(uint8_t *data, int l, int pnr, 
285                                           int c_n, uint8_t *t));
286         void scan_pf_eit(int chnr);
287
288
289         int search_in_TP(Transponder &tp, int show=1, int verbose=0);
290         int search_in_TP(uint16_t tpid, uint16_t satid, int show=1,
291                          int verbose=0);
292         int scan_TP(uint16_t tpid, uint16_t satid, int timeout=-1, int verbose=0);
293
294         int GetSection(uint8_t *buf, 
295                        uint16_t PID, uint8_t TID, uint16_t TIDExt, 
296                        uint16_t FilterTIDExt, 
297                        uint8_t secnum, uint8_t &msecnum);
298         int GetSection(uint8_t *buf, 
299                        uint16_t PID, uint8_t *filter, uint8_t *mask,
300                        uint8_t secnum, uint8_t &msecnum); 
301         int GetSection(uint8_t *buf, ushort PID, uint8_t sec,
302                        uint8_t secnum, uint8_t &msecnum); 
303         int SetFilter(uint16_t pid, uint8_t *filter, 
304                       uint8_t *mask,
305                       uint32_t timeout, uint32_t flags); 
306         uint16_t SetFilter(uint16_t pid, uint16_t section, uint16_t mode); 
307         int CloseFilter(int h);
308         
309
310         void bar2(int x, int y, int w, int h, int val, int col1, int col2);
311
312         int SetTP(unsigned int, unsigned int);
313         int scan(void);
314         int scan_all_tps(void);
315         int scan_lnb(struct Lnb &);
316         int scan_cable(Sat &sat);
317         int scan_sat(struct Sat &);
318         int scan_tp(struct Transponder &);
319
320         int AddLNB(int id, int t, uint l1, uint l2, uint sl,
321                    int dnr, int dis, int sw);
322         int AddSat(Sat &sat);
323         int AddSat(int satid, unsigned int lnbid, char *name, uint fmin, uint fmax);
324         int AddTP(Transponder &tp);
325         int AddChannel(Channel &chan);
326         int parse_descriptor(Channel *chan, uint8_t *data, int length);
327         int parse_pmt(Channel *chan, uint8_t *data);
328         int parse_pat(Channel *chan, uint8_t *data);
329
330         int check_pids(Channel *chan);
331         void check_all_pids();
332         void scan_sdt(Channel *chan);
333         int scan_sdts(int *chs, int n);
334
335         int channel_num(void) {
336                 return num[CHAN];
337         };
338           
339         int channel_change(int n) {
340                 return 0;
341         };
342         int SetChannel(uint16_t, uint16_t, uint16_t, uint16_t);
343         int SetChannel(Channel *chan,  char* apref=NULL, uint16_t *apidp=NULL, 
344                        uint16_t *vpidp=NULL) ;
345         int SetChannel(int chnr, char *apref=NULL, uint16_t *apidp=NULL, 
346                        uint16_t *vpidp=NULL);
347         int GetChannel(int chnr, struct channel *);
348         int NumChannel(void) {
349                 return num[CHAN];
350         }
351         int tune_it(struct dvb_frontend_parameters *qpsk);
352         void find_satid(Channel &chan);
353         int check_input_format(istream &ins);
354         void read_original(istream &ins);
355         int get_all_progs(uint16_t *progbuf, uint16_t *pnrbuf, int length);
356         uint16_t find_pnr(uint16_t vpid, uint16_t apid);
357         int get_pids(uint16_t prog_pid, uint16_t *vpid, uint16_t *apids, 
358                      uint16_t *ttpid, uint8_t *apids_name=NULL);
359         void AddECM(Channel *chan, uint8_t *data, int length);
360         int check_ecm(Channel *chan);
361         void add_vtx_line(magazin_t *mag, int line, uint8_t *data, int pnr);
362         
363         friend ostream &operator<<(ostream &stream, DVB &x);
364         friend istream &operator>>(istream &stream, DVB &x);
365
366 };
367
368 #define NOKIA_MAX_SAT 4
369 class nokiaconv{
370 public:
371         DVB *dvb;
372         struct lnb_sat_l{
373                 int n;
374                 int diseqc[NOKIA_MAX_SAT];
375                 char sat_names[NOKIA_MAX_SAT][MAXNAM+1];
376                 int satid[NOKIA_MAX_SAT];
377         } lnb_sat;
378
379         nokiaconv(DVB *d){
380                 dvb = d;
381         }
382
383         friend istream &operator>>(istream &stream, nokiaconv &x);
384 };
385
386 #define XML_MAX_SAT 4
387 class xmlconv{
388 public:
389         DVB *dvb;
390         struct lnb_sat_l{
391                 int n;
392                 int diseqc[XML_MAX_SAT];
393                 char sat_names[XML_MAX_SAT][MAXNAM+1];
394                 int satid[XML_MAX_SAT];
395         } lnb_sat;
396
397         xmlconv(DVB *d){
398                 dvb = d;
399         }
400         int read_stream(istream &ins, int nchan);
401         int read_desc(istream &ins, int nchan);
402         int read_serv(istream &ins, int ctp, int csat);
403         int read_trans(istream &ins, int satid);
404         int read_sat(istream &ins, int satid = -1);
405         int skip_tag(istream &ins, char *tag);
406         int read_iso639(istream &ins, int nchan, int apids);
407
408         friend istream &operator>>(istream &stream, xmlconv &x);
409 };
410
411
412
413 #define SATCO_MAX_SAT 10
414 class satcoconv{
415 public:
416         DVB *dvb;
417         int nlnb;
418
419         satcoconv(DVB *d){
420                 dvb = d;
421         }
422
423         friend istream &operator>>(istream &stream, satcoconv &x);
424 };
425
426 void hdump(uint8_t *buf, int n);
427 int get_dvbrc(char *path, DVB &dv, int dev, int len);
428 int set_dvbrc(char *path, DVB &dv, int dev, int len);
429 void dvb2txt(char *out, char *in, int len);
430 int set_sfront(int fdf, uint32_t freq, uint32_t pol, uint32_t sr , int snum, fe_code_rate_t fec);
431 void set_pes_filt(int fd,uint16_t pes_pid);
432 void set_diseqc(int fdf, int snum, fe_sec_voltage_t v, fe_sec_tone_mode_t t);
433 int tune(int fdf, uint32_t freq, uint32_t sr, fe_code_rate_t fec);
434 int set_sfront(int fdf, uint32_t freq, uint32_t pol, uint32_t sr , int snum, 
435                fe_code_rate_t fec);
436
437
438 struct in_addr getaddress (const char *name);
439 int tcp_client_connect(const char *hostname, int sckt);
440 int udp_client_connect(const char *filename);
441 void client_send_msg(int fd, uint8_t *msg, int size);
442 int chck_frontend (int fefd, frontend_stat *festat);
443
444 uint8_t deham(uint8_t x, uint8_t y);
445
446 #endif