Mercurial > mplayer.hg
annotate stream/stream_dvb.c @ 21421:d7b2fa4c39da
Remove superfluous bswap.h include.
author | diego |
---|---|
date | Sat, 02 Dec 2006 12:32:44 +0000 |
parents | 6e3fb3e851ac |
children | f43100cbeee5 |
rev | line source |
---|---|
19296 | 1 /* |
2 | |
3 dvbstream | |
4 (C) Dave Chapman <dave@dchapman.com> 2001, 2002. | |
5 | |
6 The latest version can be found at http://www.linuxstb.org/dvbstream | |
7 | |
8 Modified for use with MPlayer, for details see the changelog at | |
9 http://svn.mplayerhq.hu/mplayer/trunk/ | |
10 $Id$ | |
11 | |
12 Copyright notice: | |
13 | |
14 This program is free software; you can redistribute it and/or modify | |
15 it under the terms of the GNU General Public License as published by | |
16 the Free Software Foundation; either version 2 of the License, or | |
17 (at your option) any later version. | |
18 | |
19 This program is distributed in the hope that it will be useful, | |
20 but WITHOUT ANY WARRANTY; without even the implied warranty of | |
21 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
22 GNU General Public License for more details. | |
23 | |
24 You should have received a copy of the GNU General Public License | |
25 along with this program; if not, write to the Free Software | |
26 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. | |
27 | |
28 */ | |
29 | |
30 #include "config.h" | |
31 #include <stdio.h> | |
32 #include <stdlib.h> | |
33 #include <string.h> | |
34 #include <ctype.h> | |
35 #include <sys/ioctl.h> | |
36 #include <sys/time.h> | |
37 #include <sys/poll.h> | |
38 #include <unistd.h> | |
39 #include <fcntl.h> | |
40 #include <string.h> | |
41 #include <errno.h> | |
42 #include <fcntl.h> | |
43 | |
44 #include "stream.h" | |
19312
ab8d6b6deb63
proper inclusion of demuxer.h (including libmpdemux in Makefile only was to make previous split easier)
ben
parents:
19296
diff
changeset
|
45 #include "libmpdemux/demuxer.h" |
19296 | 46 #include "help_mp.h" |
47 #include "m_option.h" | |
48 #include "m_struct.h" | |
49 | |
50 #include "dvbin.h" | |
51 | |
52 | |
53 #define MAX_CHANNELS 8 | |
54 #define CHANNEL_LINE_LEN 256 | |
55 #define min(a, b) ((a) <= (b) ? (a) : (b)) | |
56 | |
57 | |
58 //TODO: CAMBIARE list_ptr e da globale a per_priv | |
59 | |
60 | |
61 static struct stream_priv_s | |
62 { | |
63 char *prog; | |
64 int card; | |
65 char *type; | |
66 int vid, aid; | |
67 int timeout; | |
68 char *file; | |
69 } | |
70 stream_defaults = | |
71 { | |
72 "", 1, "", 0, 0, 30, NULL | |
73 }; | |
74 | |
75 #define ST_OFF(f) M_ST_OFF(struct stream_priv_s, f) | |
76 | |
77 /// URL definition | |
78 static m_option_t stream_params[] = { | |
79 {"prog", ST_OFF(prog), CONF_TYPE_STRING, 0, 0 ,0, NULL}, | |
80 {"card", ST_OFF(card), CONF_TYPE_INT, M_OPT_RANGE, 1, 4, NULL}, | |
81 {"type", ST_OFF(type), CONF_TYPE_STRING, 0, 0 ,0, NULL}, | |
82 {"vid", ST_OFF(vid), CONF_TYPE_INT, 0, 0 ,0, NULL}, | |
83 {"aid", ST_OFF(aid), CONF_TYPE_INT, 0, 0 ,0, NULL}, | |
84 {"timeout",ST_OFF(timeout), CONF_TYPE_INT, M_OPT_RANGE, 1, 30, NULL}, | |
85 {"file", ST_OFF(file), CONF_TYPE_STRING, 0, 0 ,0, NULL}, | |
86 | |
87 {"hostname", ST_OFF(prog), CONF_TYPE_STRING, 0, 0, 0, NULL }, | |
88 {"username", ST_OFF(card), CONF_TYPE_INT, M_OPT_RANGE, 1, 4, NULL}, | |
89 {NULL, NULL, 0, 0, 0, 0, NULL} | |
90 }; | |
91 | |
92 static struct m_struct_st stream_opts = { | |
93 "dvbin", | |
94 sizeof(struct stream_priv_s), | |
95 &stream_defaults, | |
96 stream_params | |
97 }; | |
98 | |
99 | |
100 | |
101 m_option_t dvbin_opts_conf[] = { | |
102 {"prog", &stream_defaults.prog, CONF_TYPE_STRING, 0, 0 ,0, NULL}, | |
103 {"card", &stream_defaults.card, CONF_TYPE_INT, M_OPT_RANGE, 1, 4, NULL}, | |
104 {"type", "DVB card type is autodetected and can't be overridden\n", CONF_TYPE_PRINT, CONF_NOCFG, 0 ,0, NULL}, | |
105 {"vid", &stream_defaults.vid, CONF_TYPE_INT, 0, 0 ,0, NULL}, | |
106 {"aid", &stream_defaults.aid, CONF_TYPE_INT, 0, 0 ,0, NULL}, | |
107 {"timeout", &stream_defaults.timeout, CONF_TYPE_INT, M_OPT_RANGE, 1, 30, NULL}, | |
108 {"file", &stream_defaults.file, CONF_TYPE_STRING, 0, 0 ,0, NULL}, | |
109 | |
110 {NULL, NULL, 0, 0, 0, 0, NULL} | |
111 }; | |
112 | |
113 | |
114 | |
115 | |
116 extern int dvb_set_ts_filt(int fd, uint16_t pid, dmx_pes_type_t pestype); | |
117 extern int dvb_demux_stop(int fd); | |
118 extern int dvb_get_tuner_type(int fd); | |
119 int dvb_open_devices(dvb_priv_t *priv, int n, int demux_cnt, int *pids); | |
120 int dvb_fix_demuxes(dvb_priv_t *priv, int cnt, int *pids); | |
121 | |
122 extern int dvb_tune(dvb_priv_t *priv, int freq, char pol, int srate, int diseqc, int tone, | |
123 fe_spectral_inversion_t specInv, fe_modulation_t modulation, fe_guard_interval_t guardInterval, | |
124 fe_transmit_mode_t TransmissionMode, fe_bandwidth_t bandWidth, fe_code_rate_t HP_CodeRate, | |
125 fe_code_rate_t LP_CodeRate, fe_hierarchy_t hier, int timeout); | |
126 extern char *dvb_dvrdev[4], *dvb_demuxdev[4], *dvb_frontenddev[4]; | |
127 | |
128 static dvb_config_t *dvb_config = NULL; | |
129 | |
130 | |
131 static dvb_channels_list *dvb_get_channels(char *filename, int type) | |
132 { | |
133 dvb_channels_list *list; | |
134 FILE *f; | |
135 char line[CHANNEL_LINE_LEN], *colon; | |
136 | |
137 int fields, cnt, pcnt, k; | |
21033
087cad309e9c
if in the list of pids appears at least one 8192 (while TS) remove all other pid filters
nicodvb
parents:
20660
diff
changeset
|
138 int has8192; |
19296 | 139 dvb_channel_t *ptr, *tmp, chn; |
140 char tmp_lcr[256], tmp_hier[256], inv[256], bw[256], cr[256], mod[256], transm[256], gi[256], vpid_str[256], apid_str[256]; | |
141 const char *cbl_conf = "%d:%255[^:]:%d:%255[^:]:%255[^:]:%255[^:]:%255[^:]\n"; | |
142 const char *sat_conf = "%d:%c:%d:%d:%255[^:]:%255[^:]\n"; | |
143 const char *ter_conf = "%d:%255[^:]:%255[^:]:%255[^:]:%255[^:]:%255[^:]:%255[^:]:%255[^:]:%255[^:]:%255[^:]:%255[^:]\n"; | |
144 const char *atsc_conf = "%d:%255[^:]:%255[^:]:%255[^:]\n"; | |
145 | |
146 mp_msg(MSGT_DEMUX, MSGL_V, "CONFIG_READ FILE: %s, type: %d\n", filename, type); | |
147 if((f=fopen(filename, "r"))==NULL) | |
148 { | |
149 mp_msg(MSGT_DEMUX, MSGL_FATAL, "CAN'T READ CONFIG FILE %s\n", filename); | |
150 return NULL; | |
151 } | |
152 | |
153 list = malloc(sizeof(dvb_channels_list)); | |
154 if(list == NULL) | |
155 { | |
156 fclose(f); | |
157 mp_msg(MSGT_DEMUX, MSGL_V, "DVB_GET_CHANNELS: couldn't malloc enough memory\n"); | |
158 return NULL; | |
159 } | |
160 | |
161 ptr = &chn; | |
162 list->NUM_CHANNELS = 0; | |
163 list->channels = NULL; | |
164 while(! feof(f)) | |
165 { | |
166 if( fgets(line, CHANNEL_LINE_LEN, f) == NULL ) | |
167 continue; | |
168 | |
169 if((line[0] == '#') || (strlen(line) == 0)) | |
170 continue; | |
171 | |
172 colon = index(line, ':'); | |
173 if(colon) | |
174 { | |
175 k = colon - line; | |
176 if(!k) | |
177 continue; | |
178 ptr->name = (char*) malloc(k+1); | |
179 if(! ptr->name) | |
180 continue; | |
181 strncpy(ptr->name, line, k); | |
182 ptr->name[k] = 0; | |
183 } | |
184 else | |
185 continue; | |
186 k++; | |
187 apid_str[0] = vpid_str[0] = 0; | |
188 ptr->pids_cnt = 0; | |
189 ptr->freq = 0; | |
190 if(type == TUNER_TER) | |
191 { | |
192 fields = sscanf(&line[k], ter_conf, | |
193 &ptr->freq, inv, bw, cr, tmp_lcr, mod, | |
194 transm, gi, tmp_hier, vpid_str, apid_str); | |
195 mp_msg(MSGT_DEMUX, MSGL_V, | |
196 "TER, NUM: %d, NUM_FIELDS: %d, NAME: %s, FREQ: %d", | |
197 list->NUM_CHANNELS, fields, ptr->name, ptr->freq); | |
198 } | |
199 else if(type == TUNER_CBL) | |
200 { | |
201 fields = sscanf(&line[k], cbl_conf, | |
202 &ptr->freq, inv, &ptr->srate, | |
203 cr, mod, vpid_str, apid_str); | |
204 mp_msg(MSGT_DEMUX, MSGL_V, | |
205 "CBL, NUM: %d, NUM_FIELDS: %d, NAME: %s, FREQ: %d, SRATE: %d", | |
206 list->NUM_CHANNELS, fields, ptr->name, ptr->freq, ptr->srate); | |
207 } | |
208 #ifdef DVB_ATSC | |
209 else if(type == TUNER_ATSC) | |
210 { | |
211 fields = sscanf(&line[k], atsc_conf, | |
212 &ptr->freq, mod, vpid_str, apid_str); | |
213 mp_msg(MSGT_DEMUX, MSGL_V, | |
214 "ATSC, NUM: %d, NUM_FIELDS: %d, NAME: %s, FREQ: %d\n", | |
215 list->NUM_CHANNELS, fields, ptr->name, ptr->freq); | |
216 } | |
217 #endif | |
218 else //SATELLITE | |
219 { | |
220 fields = sscanf(&line[k], sat_conf, | |
221 &ptr->freq, &ptr->pol, &ptr->diseqc, &ptr->srate, vpid_str, apid_str); | |
222 ptr->pol = toupper(ptr->pol); | |
223 ptr->freq *= 1000UL; | |
224 ptr->srate *= 1000UL; | |
225 ptr->tone = -1; | |
226 ptr->inv = INVERSION_AUTO; | |
227 ptr->cr = FEC_AUTO; | |
228 if((ptr->diseqc > 4) || (ptr->diseqc < 0)) | |
229 continue; | |
230 if(ptr->diseqc > 0) | |
231 ptr->diseqc--; | |
232 mp_msg(MSGT_DEMUX, MSGL_V, | |
233 "SAT, NUM: %d, NUM_FIELDS: %d, NAME: %s, FREQ: %d, SRATE: %d, POL: %c, DISEQC: %d", | |
234 list->NUM_CHANNELS, fields, ptr->name, ptr->freq, ptr->srate, ptr->pol, ptr->diseqc); | |
235 } | |
236 | |
237 if(vpid_str[0]) | |
238 { | |
239 pcnt = sscanf(vpid_str, "%d+%d+%d+%d+%d+%d+%d", &ptr->pids[0], &ptr->pids[1], &ptr->pids[2], &ptr->pids[3], | |
240 &ptr->pids[4], &ptr->pids[5], &ptr->pids[6]); | |
241 if(pcnt > 0) | |
242 { | |
243 ptr->pids_cnt = pcnt; | |
244 fields++; | |
245 } | |
246 } | |
247 | |
248 if(apid_str[0]) | |
249 { | |
250 cnt = ptr->pids_cnt; | |
251 pcnt = sscanf(apid_str, "%d+%d+%d+%d+%d+%d+%d+%d", &ptr->pids[cnt], &ptr->pids[cnt+1], &ptr->pids[cnt+2], | |
252 &ptr->pids[cnt+3], &ptr->pids[cnt+4], &ptr->pids[cnt+5], &ptr->pids[cnt+6], &ptr->pids[cnt+7]); | |
253 if(pcnt > 0) | |
254 { | |
255 ptr->pids_cnt += pcnt; | |
256 fields++; | |
257 } | |
258 } | |
259 | |
260 if((fields < 2) || (ptr->pids_cnt <= 0) || (ptr->freq == 0) || (strlen(ptr->name) == 0)) | |
261 continue; | |
262 | |
21033
087cad309e9c
if in the list of pids appears at least one 8192 (while TS) remove all other pid filters
nicodvb
parents:
20660
diff
changeset
|
263 has8192 = 0; |
087cad309e9c
if in the list of pids appears at least one 8192 (while TS) remove all other pid filters
nicodvb
parents:
20660
diff
changeset
|
264 for(cnt = 0; cnt < ptr->pids_cnt; cnt++) |
087cad309e9c
if in the list of pids appears at least one 8192 (while TS) remove all other pid filters
nicodvb
parents:
20660
diff
changeset
|
265 { |
087cad309e9c
if in the list of pids appears at least one 8192 (while TS) remove all other pid filters
nicodvb
parents:
20660
diff
changeset
|
266 if(ptr->pids[cnt] == 8192) |
087cad309e9c
if in the list of pids appears at least one 8192 (while TS) remove all other pid filters
nicodvb
parents:
20660
diff
changeset
|
267 { |
087cad309e9c
if in the list of pids appears at least one 8192 (while TS) remove all other pid filters
nicodvb
parents:
20660
diff
changeset
|
268 has8192 = 1; |
087cad309e9c
if in the list of pids appears at least one 8192 (while TS) remove all other pid filters
nicodvb
parents:
20660
diff
changeset
|
269 break; |
087cad309e9c
if in the list of pids appears at least one 8192 (while TS) remove all other pid filters
nicodvb
parents:
20660
diff
changeset
|
270 } |
087cad309e9c
if in the list of pids appears at least one 8192 (while TS) remove all other pid filters
nicodvb
parents:
20660
diff
changeset
|
271 } |
087cad309e9c
if in the list of pids appears at least one 8192 (while TS) remove all other pid filters
nicodvb
parents:
20660
diff
changeset
|
272 if(has8192) |
087cad309e9c
if in the list of pids appears at least one 8192 (while TS) remove all other pid filters
nicodvb
parents:
20660
diff
changeset
|
273 { |
087cad309e9c
if in the list of pids appears at least one 8192 (while TS) remove all other pid filters
nicodvb
parents:
20660
diff
changeset
|
274 ptr->pids[0] = 8192; |
087cad309e9c
if in the list of pids appears at least one 8192 (while TS) remove all other pid filters
nicodvb
parents:
20660
diff
changeset
|
275 ptr->pids_cnt = 1; |
087cad309e9c
if in the list of pids appears at least one 8192 (while TS) remove all other pid filters
nicodvb
parents:
20660
diff
changeset
|
276 } |
087cad309e9c
if in the list of pids appears at least one 8192 (while TS) remove all other pid filters
nicodvb
parents:
20660
diff
changeset
|
277 else |
087cad309e9c
if in the list of pids appears at least one 8192 (while TS) remove all other pid filters
nicodvb
parents:
20660
diff
changeset
|
278 { |
21034 | 279 ptr->pids[ptr->pids_cnt] = 0; //PID 0 is the PAT |
280 ptr->pids_cnt++; | |
21033
087cad309e9c
if in the list of pids appears at least one 8192 (while TS) remove all other pid filters
nicodvb
parents:
20660
diff
changeset
|
281 } |
19296 | 282 mp_msg(MSGT_DEMUX, MSGL_V, " PIDS: "); |
283 for(cnt = 0; cnt < ptr->pids_cnt; cnt++) | |
284 mp_msg(MSGT_DEMUX, MSGL_V, " %d ", ptr->pids[cnt]); | |
285 mp_msg(MSGT_DEMUX, MSGL_V, "\n"); | |
286 | |
287 if((type == TUNER_TER) || (type == TUNER_CBL)) | |
288 { | |
289 if(! strcmp(inv, "INVERSION_ON")) | |
290 ptr->inv = INVERSION_ON; | |
291 else if(! strcmp(inv, "INVERSION_OFF")) | |
292 ptr->inv = INVERSION_OFF; | |
293 else | |
294 ptr->inv = INVERSION_AUTO; | |
295 | |
296 | |
297 if(! strcmp(cr, "FEC_1_2")) | |
298 ptr->cr =FEC_1_2; | |
299 else if(! strcmp(cr, "FEC_2_3")) | |
300 ptr->cr =FEC_2_3; | |
301 else if(! strcmp(cr, "FEC_3_4")) | |
302 ptr->cr =FEC_3_4; | |
303 #ifdef HAVE_DVB_HEAD | |
304 else if(! strcmp(cr, "FEC_4_5")) | |
305 ptr->cr =FEC_4_5; | |
306 else if(! strcmp(cr, "FEC_6_7")) | |
307 ptr->cr =FEC_6_7; | |
308 else if(! strcmp(cr, "FEC_8_9")) | |
309 ptr->cr =FEC_8_9; | |
310 #endif | |
311 else if(! strcmp(cr, "FEC_5_6")) | |
312 ptr->cr =FEC_5_6; | |
313 else if(! strcmp(cr, "FEC_7_8")) | |
314 ptr->cr =FEC_7_8; | |
315 else if(! strcmp(cr, "FEC_NONE")) | |
316 ptr->cr =FEC_NONE; | |
317 else ptr->cr =FEC_AUTO; | |
318 } | |
319 | |
320 | |
321 if((type == TUNER_TER) || (type == TUNER_CBL) || (type == TUNER_ATSC)) | |
322 { | |
323 if(! strcmp(mod, "QAM_128")) | |
324 ptr->mod = QAM_128; | |
325 else if(! strcmp(mod, "QAM_256")) | |
326 ptr->mod = QAM_256; | |
327 else if(! strcmp(mod, "QAM_64")) | |
328 ptr->mod = QAM_64; | |
329 else if(! strcmp(mod, "QAM_32")) | |
330 ptr->mod = QAM_32; | |
331 else if(! strcmp(mod, "QAM_16")) | |
332 ptr->mod = QAM_16; | |
333 #ifdef DVB_ATSC | |
334 else if(! strcmp(mod, "VSB_8") || ! strcmp(mod, "8VSB")) | |
335 ptr->mod = VSB_8; | |
336 else if(! strcmp(mod, "VSB_16") || !strcmp(mod, "16VSB")) | |
337 ptr->mod = VSB_16; | |
338 | |
339 ptr->inv = INVERSION_AUTO; | |
340 #endif | |
341 } | |
342 | |
343 if(type == TUNER_TER) | |
344 { | |
345 if(! strcmp(bw, "BANDWIDTH_6_MHZ")) | |
346 ptr->bw = BANDWIDTH_6_MHZ; | |
347 else if(! strcmp(bw, "BANDWIDTH_7_MHZ")) | |
348 ptr->bw = BANDWIDTH_7_MHZ; | |
349 else if(! strcmp(bw, "BANDWIDTH_8_MHZ")) | |
350 ptr->bw = BANDWIDTH_8_MHZ; | |
351 | |
352 | |
353 if(! strcmp(transm, "TRANSMISSION_MODE_2K")) | |
354 ptr->trans = TRANSMISSION_MODE_2K; | |
355 else if(! strcmp(transm, "TRANSMISSION_MODE_8K")) | |
356 ptr->trans = TRANSMISSION_MODE_8K; | |
357 | |
358 | |
359 if(! strcmp(gi, "GUARD_INTERVAL_1_32")) | |
360 ptr->gi = GUARD_INTERVAL_1_32; | |
361 else if(! strcmp(gi, "GUARD_INTERVAL_1_16")) | |
362 ptr->gi = GUARD_INTERVAL_1_16; | |
363 else if(! strcmp(gi, "GUARD_INTERVAL_1_8")) | |
364 ptr->gi = GUARD_INTERVAL_1_8; | |
365 else ptr->gi = GUARD_INTERVAL_1_4; | |
366 | |
367 if(! strcmp(tmp_lcr, "FEC_1_2")) | |
368 ptr->cr_lp =FEC_1_2; | |
369 else if(! strcmp(tmp_lcr, "FEC_2_3")) | |
370 ptr->cr_lp =FEC_2_3; | |
371 else if(! strcmp(tmp_lcr, "FEC_3_4")) | |
372 ptr->cr_lp =FEC_3_4; | |
373 #ifdef HAVE_DVB_HEAD | |
374 else if(! strcmp(tmp_lcr, "FEC_4_5")) | |
375 ptr->cr_lp =FEC_4_5; | |
376 else if(! strcmp(tmp_lcr, "FEC_6_7")) | |
377 ptr->cr_lp =FEC_6_7; | |
378 else if(! strcmp(tmp_lcr, "FEC_8_9")) | |
379 ptr->cr_lp =FEC_8_9; | |
380 #endif | |
381 else if(! strcmp(tmp_lcr, "FEC_5_6")) | |
382 ptr->cr_lp =FEC_5_6; | |
383 else if(! strcmp(tmp_lcr, "FEC_7_8")) | |
384 ptr->cr_lp =FEC_7_8; | |
385 else if(! strcmp(tmp_lcr, "FEC_NONE")) | |
386 ptr->cr_lp =FEC_NONE; | |
387 else ptr->cr_lp =FEC_AUTO; | |
388 | |
389 | |
390 if(! strcmp(tmp_hier, "HIERARCHY_1")) | |
391 ptr->hier = HIERARCHY_1; | |
392 else if(! strcmp(tmp_hier, "HIERARCHY_2")) | |
393 ptr->hier = HIERARCHY_2; | |
394 else if(! strcmp(tmp_hier, "HIERARCHY_4")) | |
395 ptr->hier = HIERARCHY_4; | |
396 #ifdef HAVE_DVB_HEAD | |
397 else if(! strcmp(tmp_hier, "HIERARCHY_AUTO")) | |
398 ptr->hier = HIERARCHY_AUTO; | |
399 #endif | |
400 else ptr->hier = HIERARCHY_NONE; | |
401 } | |
402 | |
403 tmp = (dvb_channel_t*)realloc(list->channels, sizeof(dvb_channel_t) * (list->NUM_CHANNELS + 1)); | |
404 if(tmp == NULL) | |
405 break; | |
406 | |
407 list->channels = tmp; | |
408 memcpy(&(list->channels[list->NUM_CHANNELS]), ptr, sizeof(dvb_channel_t)); | |
409 list->NUM_CHANNELS++; | |
410 if(sizeof(dvb_channel_t) * list->NUM_CHANNELS >= 1024*1024) | |
411 { | |
412 mp_msg(MSGT_DEMUX, MSGL_V, "dvbin.c, > 1MB allocated for channels struct, dropping the rest of the file\r\n"); | |
413 break; | |
414 } | |
415 } | |
416 | |
417 fclose(f); | |
418 if(list->NUM_CHANNELS == 0) | |
419 { | |
420 if(list->channels != NULL) | |
421 free(list->channels); | |
422 free(list); | |
423 return NULL; | |
424 } | |
425 | |
426 list->current = 0; | |
427 return list; | |
428 } | |
429 | |
430 | |
431 | |
432 static int dvb_streaming_read(stream_t *stream, char *buffer, int size) | |
433 { | |
434 struct pollfd pfds[1]; | |
435 int pos=0, tries, rk, fd; | |
436 dvb_priv_t *priv = (dvb_priv_t *) stream->priv; | |
437 | |
438 mp_msg(MSGT_DEMUX, MSGL_DBG3, "dvb_streaming_read(%d)\n", size); | |
439 | |
440 tries = priv->retry + 1; | |
441 | |
442 fd = stream->fd; | |
443 while(pos < size) | |
444 { | |
445 pfds[0].fd = fd; | |
446 pfds[0].events = POLLIN | POLLPRI; | |
447 | |
448 rk = size - pos; | |
449 if(poll(pfds, 1, 500) <= 0) | |
450 { | |
451 errno = 0; | |
452 mp_msg(MSGT_DEMUX, MSGL_ERR, "dvb_streaming_read, attempt N. %d failed with errno %d when reading %d bytes\n", tries, errno, size-pos); | |
453 if(--tries > 0) | |
454 continue; | |
455 else | |
456 break; | |
457 } | |
458 if((rk = read(fd, &buffer[pos], rk)) > 0) | |
459 { | |
460 pos += rk; | |
461 mp_msg(MSGT_DEMUX, MSGL_DBG3, "ret (%d) bytes\n", pos); | |
462 } | |
463 } | |
464 | |
465 | |
466 if(! pos) | |
467 mp_msg(MSGT_DEMUX, MSGL_ERR, "dvb_streaming_read, return %d bytes\n", pos); | |
468 | |
469 return pos; | |
470 } | |
471 | |
472 static void dvbin_close(stream_t *stream); | |
473 | |
474 int dvb_set_channel(dvb_priv_t *priv, int card, int n) | |
475 { | |
476 dvb_channels_list *new_list; | |
477 dvb_channel_t *channel; | |
478 stream_t *stream = (stream_t*) priv->stream; | |
479 char buf[4096]; | |
480 dvb_config_t *conf = (dvb_config_t *) priv->config; | |
481 int devno; | |
482 int i; | |
483 | |
484 if((card < 0) || (card > conf->count)) | |
485 { | |
486 mp_msg(MSGT_DEMUX, MSGL_ERR, "dvb_set_channel: INVALID CARD NUMBER: %d vs %d, abort\n", card, conf->count); | |
487 return 0; | |
488 } | |
489 | |
490 devno = conf->cards[card].devno; | |
491 new_list = conf->cards[card].list; | |
492 if((n > new_list->NUM_CHANNELS) || (n < 0)) | |
493 { | |
494 mp_msg(MSGT_DEMUX, MSGL_ERR, "dvb_set_channel: INVALID CHANNEL NUMBER: %d, for card %d, abort\n", n, card); | |
495 return 0; | |
496 } | |
497 channel = &(new_list->channels[n]); | |
498 | |
499 if(priv->is_on) //the fds are already open and we have to stop the demuxers | |
500 { | |
501 for(i = 0; i < priv->demux_fds_cnt; i++) | |
502 dvb_demux_stop(priv->demux_fds[i]); | |
503 | |
504 priv->retry = 0; | |
505 while(dvb_streaming_read(stream, buf, 4096) > 0); //empty both the stream's and driver's buffer | |
506 if(priv->card != card) | |
507 { | |
508 dvbin_close(stream); | |
509 if(! dvb_open_devices(priv, devno, channel->pids_cnt, channel->pids)) | |
510 { | |
511 mp_msg(MSGT_DEMUX, MSGL_ERR, "DVB_SET_CHANNEL, COULDN'T OPEN DEVICES OF CARD: %d, EXIT\n", card); | |
512 return 0; | |
513 } | |
514 } | |
515 else //close all demux_fds with pos > pids required for the new channel or open other demux_fds if we have too few | |
516 { | |
517 if(! dvb_fix_demuxes(priv, channel->pids_cnt, channel->pids)) | |
518 return 0; | |
519 } | |
520 } | |
521 else | |
522 { | |
523 if(! dvb_open_devices(priv, devno, channel->pids_cnt, channel->pids)) | |
524 { | |
525 mp_msg(MSGT_DEMUX, MSGL_ERR, "DVB_SET_CHANNEL2, COULDN'T OPEN DEVICES OF CARD: %d, EXIT\n", card); | |
526 return 0; | |
527 } | |
528 } | |
529 | |
530 dvb_config->priv = priv; | |
531 priv->card = card; | |
532 priv->list = new_list; | |
533 priv->retry = 5; | |
534 new_list->current = n; | |
535 stream->fd = priv->dvr_fd; | |
536 mp_msg(MSGT_DEMUX, MSGL_V, "DVB_SET_CHANNEL: new channel name=%s, card: %d, channel %d\n", channel->name, card, n); | |
537 | |
538 stream->eof=1; | |
539 stream_reset(stream); | |
540 | |
541 | |
20659
31b525f90395
nonsense removal: compare old and new frequency in order to skip tuning
nicodvb
parents:
19312
diff
changeset
|
542 if(channel->freq != priv->last_freq) |
19296 | 543 if (! dvb_tune(priv, channel->freq, channel->pol, channel->srate, channel->diseqc, channel->tone, |
544 channel->inv, channel->mod, channel->gi, channel->trans, channel->bw, channel->cr, channel->cr_lp, channel->hier, priv->timeout)) | |
545 return 0; | |
546 | |
20659
31b525f90395
nonsense removal: compare old and new frequency in order to skip tuning
nicodvb
parents:
19312
diff
changeset
|
547 priv->last_freq = channel->freq; |
19296 | 548 priv->is_on = 1; |
549 | |
550 //sets demux filters and restart the stream | |
551 for(i = 0; i < channel->pids_cnt; i++) | |
552 { | |
553 if(! dvb_set_ts_filt(priv->demux_fds[i], channel->pids[i], DMX_PES_OTHER)) | |
554 return 0; | |
555 } | |
556 | |
557 return 1; | |
558 } | |
559 | |
560 | |
561 | |
562 int dvb_step_channel(dvb_priv_t *priv, int dir) | |
563 { | |
564 int new_current; | |
565 dvb_channels_list *list; | |
566 | |
567 mp_msg(MSGT_DEMUX, MSGL_V, "DVB_STEP_CHANNEL dir %d\n", dir); | |
568 | |
569 if(priv == NULL) | |
570 { | |
571 mp_msg(MSGT_DEMUX, MSGL_ERR, "dvb_step_channel: NULL priv_ptr, quit\n"); | |
572 return 0; | |
573 } | |
574 | |
575 list = priv->list; | |
576 if(list == NULL) | |
577 { | |
578 mp_msg(MSGT_DEMUX, MSGL_ERR, "dvb_step_channel: NULL list_ptr, quit\n"); | |
579 return 0; | |
580 } | |
581 | |
582 new_current = (list->NUM_CHANNELS + list->current + (dir == DVB_CHANNEL_HIGHER ? 1 : -1)) % list->NUM_CHANNELS; | |
583 | |
584 return dvb_set_channel(priv, priv->card, new_current); | |
585 } | |
586 | |
587 | |
588 | |
589 | |
590 extern char *get_path(const char *); | |
591 | |
592 static void dvbin_close(stream_t *stream) | |
593 { | |
594 int i; | |
595 dvb_priv_t *priv = (dvb_priv_t *) stream->priv; | |
596 | |
597 for(i = priv->demux_fds_cnt-1; i >= 0; i--) | |
598 { | |
599 priv->demux_fds_cnt--; | |
600 mp_msg(MSGT_DEMUX, MSGL_V, "DVBIN_CLOSE, close(%d), fd=%d, COUNT=%d\n", i, priv->demux_fds[i], priv->demux_fds_cnt); | |
601 close(priv->demux_fds[i]); | |
602 } | |
603 close(priv->dvr_fd); | |
604 | |
605 close(priv->fe_fd); | |
606 #ifdef HAVE_DVB | |
607 close(priv->sec_fd); | |
608 #endif | |
609 | |
610 priv->is_on = 0; | |
611 dvb_config->priv = NULL; | |
612 } | |
613 | |
614 | |
615 static int dvb_streaming_start(dvb_priv_t *priv, struct stream_priv_s *opts, int tuner_type, char *progname) | |
616 { | |
617 int i; | |
618 dvb_channel_t *channel = NULL; | |
619 stream_t *stream = (stream_t*) priv->stream; | |
620 | |
621 mp_msg(MSGT_DEMUX, MSGL_V, "\r\ndvb_streaming_start(PROG: %s, CARD: %d, VID: %d, AID: %d, TYPE: %s, FILE: %s)\r\n", | |
622 opts->prog, opts->card, opts->vid, opts->aid, opts->type, opts->file); | |
623 | |
624 priv->is_on = 0; | |
625 | |
626 i = 0; | |
627 while((channel == NULL) && i < priv->list->NUM_CHANNELS) | |
628 { | |
629 if(! strcmp(priv->list->channels[i].name, progname)) | |
630 channel = &(priv->list->channels[i]); | |
631 | |
632 i++; | |
633 } | |
634 | |
635 if(channel != NULL) | |
636 { | |
637 priv->list->current = i-1; | |
638 mp_msg(MSGT_DEMUX, MSGL_V, "PROGRAM NUMBER %d: name=%s, freq=%u\n", i-1, channel->name, channel->freq); | |
639 } | |
640 else | |
641 { | |
642 mp_msg(MSGT_DEMUX, MSGL_ERR, "\n\nDVBIN: no such channel \"%s\"\n\n", progname); | |
643 return 0; | |
644 } | |
645 | |
646 | |
647 if(!dvb_set_channel(priv, priv->card, priv->list->current)) | |
648 { | |
649 mp_msg(MSGT_DEMUX, MSGL_ERR, "ERROR, COULDN'T SET CHANNEL %i: ", priv->list->current); | |
650 dvbin_close(stream); | |
651 return 0; | |
652 } | |
653 | |
654 mp_msg(MSGT_DEMUX, MSGL_V, "SUCCESSFUL EXIT from dvb_streaming_start\n"); | |
655 | |
656 return 1; | |
657 } | |
658 | |
659 | |
660 | |
661 | |
662 static int dvb_open(stream_t *stream, int mode, void *opts, int *file_format) | |
663 { | |
664 // I don't force the file format bacause, although it's almost always TS, | |
665 // there are some providers that stream an IP multicast with M$ Mpeg4 inside | |
666 struct stream_priv_s* p = (struct stream_priv_s*)opts; | |
667 dvb_priv_t *priv; | |
668 char *progname; | |
21241
6e3fb3e851ac
match exactly card number N specified, rather than the N-th actually usable
nicodvb
parents:
21034
diff
changeset
|
669 int tuner_type = 0, i; |
19296 | 670 |
671 | |
672 if(mode != STREAM_READ) | |
673 return STREAM_UNSUPORTED; | |
674 | |
20660 | 675 stream->priv = (dvb_priv_t*) calloc(1, sizeof(dvb_priv_t)); |
19296 | 676 if(stream->priv == NULL) |
677 return STREAM_ERROR; | |
678 | |
679 priv = (dvb_priv_t *)stream->priv; | |
680 priv->stream = stream; | |
681 dvb_config = dvb_get_config(); | |
682 if(dvb_config == NULL) | |
683 { | |
684 free(priv); | |
685 mp_msg(MSGT_DEMUX, MSGL_ERR, "DVB CONFIGURATION IS EMPTY, exit\n"); | |
686 return STREAM_ERROR; | |
687 } | |
688 dvb_config->priv = priv; | |
689 priv->config = dvb_config; | |
690 | |
21241
6e3fb3e851ac
match exactly card number N specified, rather than the N-th actually usable
nicodvb
parents:
21034
diff
changeset
|
691 priv->card = -1; |
6e3fb3e851ac
match exactly card number N specified, rather than the N-th actually usable
nicodvb
parents:
21034
diff
changeset
|
692 for(i=0; i<priv->config->count; i++) |
6e3fb3e851ac
match exactly card number N specified, rather than the N-th actually usable
nicodvb
parents:
21034
diff
changeset
|
693 { |
6e3fb3e851ac
match exactly card number N specified, rather than the N-th actually usable
nicodvb
parents:
21034
diff
changeset
|
694 if(priv->config->cards[i].devno+1 == p->card) |
6e3fb3e851ac
match exactly card number N specified, rather than the N-th actually usable
nicodvb
parents:
21034
diff
changeset
|
695 { |
6e3fb3e851ac
match exactly card number N specified, rather than the N-th actually usable
nicodvb
parents:
21034
diff
changeset
|
696 priv->card = i; |
6e3fb3e851ac
match exactly card number N specified, rather than the N-th actually usable
nicodvb
parents:
21034
diff
changeset
|
697 break; |
6e3fb3e851ac
match exactly card number N specified, rather than the N-th actually usable
nicodvb
parents:
21034
diff
changeset
|
698 } |
6e3fb3e851ac
match exactly card number N specified, rather than the N-th actually usable
nicodvb
parents:
21034
diff
changeset
|
699 } |
6e3fb3e851ac
match exactly card number N specified, rather than the N-th actually usable
nicodvb
parents:
21034
diff
changeset
|
700 |
6e3fb3e851ac
match exactly card number N specified, rather than the N-th actually usable
nicodvb
parents:
21034
diff
changeset
|
701 if(priv->card == -1) |
19296 | 702 { |
703 free(priv); | |
704 mp_msg(MSGT_DEMUX, MSGL_ERR, "NO CONFIGURATION FOUND FOR CARD N. %d, exit\n", p->card); | |
705 return STREAM_ERROR; | |
706 } | |
707 priv->timeout = p->timeout; | |
708 | |
709 tuner_type = priv->config->cards[priv->card].type; | |
710 | |
711 if(tuner_type == 0) | |
712 { | |
713 free(priv); | |
714 mp_msg(MSGT_DEMUX, MSGL_V, "OPEN_DVB: UNKNOWN OR UNDETECTABLE TUNER TYPE, EXIT\n"); | |
715 return STREAM_ERROR; | |
716 } | |
717 | |
718 | |
719 priv->tuner_type = tuner_type; | |
720 | |
721 mp_msg(MSGT_DEMUX, MSGL_V, "OPEN_DVB: prog=%s, card=%d, type=%d, vid=%d, aid=%d\n", | |
722 p->prog, priv->card+1, priv->tuner_type, p->vid, p->aid); | |
723 | |
724 priv->list = priv->config->cards[priv->card].list; | |
725 | |
726 if((! strcmp(p->prog, "")) && (priv->list != NULL)) | |
727 progname = priv->list->channels[0].name; | |
728 else | |
729 progname = p->prog; | |
730 | |
731 | |
732 if(! dvb_streaming_start(priv, p, tuner_type, progname)) | |
733 { | |
734 free(stream->priv); | |
735 stream->priv = NULL; | |
736 return STREAM_ERROR; | |
737 } | |
738 | |
739 stream->type = STREAMTYPE_DVB; | |
740 stream->fill_buffer = dvb_streaming_read; | |
741 stream->close = dvbin_close; | |
742 m_struct_free(&stream_opts, opts); | |
743 | |
744 *file_format = DEMUXER_TYPE_MPEG_TS; | |
745 | |
746 return STREAM_OK; | |
747 } | |
748 | |
749 #define MAX_CARDS 4 | |
750 dvb_config_t *dvb_get_config(void) | |
751 { | |
752 int i, fd, type, size; | |
753 char filename[30], *conf_file, *name; | |
754 dvb_channels_list *list; | |
755 dvb_card_config_t *cards = NULL, *tmp; | |
756 dvb_config_t *conf = NULL; | |
757 | |
758 if(dvb_config != NULL) | |
759 return dvb_config; | |
760 | |
761 conf = (dvb_config_t*) malloc(sizeof(dvb_config_t)); | |
762 if(conf == NULL) | |
763 return NULL; | |
764 | |
765 conf->priv = NULL; | |
766 conf->count = 0; | |
767 conf->cards = NULL; | |
768 for(i=0; i<MAX_CARDS; i++) | |
769 { | |
770 sprintf(filename, "/dev/dvb/adapter%d/frontend0", i); | |
771 fd = open(filename, O_RDWR | O_NONBLOCK); | |
772 if(fd < 0) | |
773 { | |
774 mp_msg(MSGT_DEMUX, MSGL_V, "DVB_CONFIG, can't open device %s, skipping\n", filename); | |
775 continue; | |
776 } | |
777 | |
778 type = dvb_get_tuner_type(fd); | |
779 close(fd); | |
780 if(type != TUNER_SAT && type != TUNER_TER && type != TUNER_CBL && type != TUNER_ATSC) | |
781 { | |
782 mp_msg(MSGT_DEMUX, MSGL_V, "DVB_CONFIG, can't detect tuner type of card %d, skipping\n", i); | |
783 continue; | |
784 } | |
785 | |
786 switch(type) | |
787 { | |
788 case TUNER_TER: | |
789 conf_file = get_path("channels.conf.ter"); | |
790 break; | |
791 case TUNER_CBL: | |
792 conf_file = get_path("channels.conf.cbl"); | |
793 break; | |
794 case TUNER_SAT: | |
795 conf_file = get_path("channels.conf.sat"); | |
796 break; | |
797 case TUNER_ATSC: | |
798 conf_file = get_path("channels.conf.atsc"); | |
799 break; | |
800 } | |
801 | |
802 if((access(conf_file, F_OK | R_OK) != 0)) | |
803 conf_file = get_path("channels.conf"); | |
804 | |
805 list = dvb_get_channels(conf_file, type); | |
806 if(list == NULL) | |
807 continue; | |
808 | |
809 size = sizeof(dvb_card_config_t) * (conf->count + 1); | |
810 tmp = realloc(conf->cards, size); | |
811 | |
812 if(tmp == NULL) | |
813 { | |
814 fprintf(stderr, "DVB_CONFIG, can't realloc %d bytes, skipping\n", size); | |
815 continue; | |
816 } | |
817 cards = tmp; | |
818 | |
819 name = (char*) malloc(20); | |
820 if(name==NULL) | |
821 { | |
822 fprintf(stderr, "DVB_CONFIG, can't realloc 20 bytes, skipping\n"); | |
823 continue; | |
824 } | |
825 | |
826 conf->cards = cards; | |
827 conf->cards[conf->count].devno = i; | |
828 conf->cards[conf->count].list = list; | |
829 conf->cards[conf->count].type = type; | |
830 sprintf(name, "DVB-%c card n. %d", type==TUNER_TER ? 'T' : (type==TUNER_CBL ? 'C' : 'S'), conf->count+1); | |
831 conf->cards[conf->count].name = name; | |
832 conf->count++; | |
833 } | |
834 | |
835 if(conf->count == 0) | |
836 { | |
837 free(conf); | |
838 conf = NULL; | |
839 } | |
840 | |
841 dvb_config = conf; | |
842 return conf; | |
843 } | |
844 | |
845 | |
846 | |
847 stream_info_t stream_info_dvb = { | |
848 "Dvb Input", | |
849 "dvbin", | |
850 "Nico", | |
851 "based on the code from ??? (probably Arpi)", | |
852 dvb_open, | |
853 { "dvb", NULL }, | |
854 &stream_opts, | |
855 1 // Urls are an option string | |
856 }; |