Add explanatory comments to the #endif part of multiple inclusion guards.
[mplayer/greg.git] / stream / stream_dvb.c
blobccc77190de63ce697e9fb27f8315ec8ac911132e
1 /*
3 dvbstream
4 (C) Dave Chapman <dave@dchapman.com> 2001, 2002.
6 The latest version can be found at http://www.linuxstb.org/dvbstream
8 Modified for use with MPlayer, for details see the changelog at
9 http://svn.mplayerhq.hu/mplayer/trunk/
10 $Id$
12 Copyright notice:
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.
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.
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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
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 <errno.h>
42 #include "stream.h"
43 #include "libmpdemux/demuxer.h"
44 #include "help_mp.h"
45 #include "m_option.h"
46 #include "m_struct.h"
47 #include "get_path.h"
48 #include "libavutil/avstring.h"
50 #include "dvbin.h"
53 #define MAX_CHANNELS 8
54 #define CHANNEL_LINE_LEN 256
55 #define min(a, b) ((a) <= (b) ? (a) : (b))
58 //TODO: CAMBIARE list_ptr e da globale a per_priv
61 static struct stream_priv_s
63 char *prog;
64 int card;
65 char *type;
66 int vid, aid;
67 int timeout;
68 char *file;
70 stream_defaults =
72 "", 1, "", 0, 0, 30, NULL
75 #define ST_OFF(f) M_ST_OFF(struct stream_priv_s, f)
77 /// URL definition
78 static const 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},
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}
92 static struct m_struct_st stream_opts = {
93 "dvbin",
94 sizeof(struct stream_priv_s),
95 &stream_defaults,
96 stream_params
101 const 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},
110 {NULL, NULL, 0, 0, 0, 0, NULL}
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);
120 int dvb_fix_demuxes(dvb_priv_t *priv, int cnt);
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];
130 static dvb_channels_list *dvb_get_channels(char *filename, int type)
132 dvb_channels_list *list;
133 FILE *f;
134 char line[CHANNEL_LINE_LEN], *colon;
136 int fields, cnt, pcnt, k;
137 int has8192, has0;
138 dvb_channel_t *ptr, *tmp, chn;
139 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];
140 const char *cbl_conf = "%d:%255[^:]:%d:%255[^:]:%255[^:]:%255[^:]:%255[^:]\n";
141 const char *sat_conf = "%d:%c:%d:%d:%255[^:]:%255[^:]\n";
142 const char *ter_conf = "%d:%255[^:]:%255[^:]:%255[^:]:%255[^:]:%255[^:]:%255[^:]:%255[^:]:%255[^:]:%255[^:]:%255[^:]\n";
143 const char *atsc_conf = "%d:%255[^:]:%255[^:]:%255[^:]\n";
145 mp_msg(MSGT_DEMUX, MSGL_V, "CONFIG_READ FILE: %s, type: %d\n", filename, type);
146 if((f=fopen(filename, "r"))==NULL)
148 mp_msg(MSGT_DEMUX, MSGL_FATAL, "CAN'T READ CONFIG FILE %s\n", filename);
149 return NULL;
152 list = malloc(sizeof(dvb_channels_list));
153 if(list == NULL)
155 fclose(f);
156 mp_msg(MSGT_DEMUX, MSGL_V, "DVB_GET_CHANNELS: couldn't malloc enough memory\n");
157 return NULL;
160 ptr = &chn;
161 list->NUM_CHANNELS = 0;
162 list->channels = NULL;
163 while(! feof(f))
165 if( fgets(line, CHANNEL_LINE_LEN, f) == NULL )
166 continue;
168 if((line[0] == '#') || (strlen(line) == 0))
169 continue;
171 colon = strchr(line, ':');
172 if(colon)
174 k = colon - line;
175 if(!k)
176 continue;
177 ptr->name = malloc(k+1);
178 if(! ptr->name)
179 continue;
180 av_strlcpy(ptr->name, line, k+1);
182 else
183 continue;
184 k++;
185 apid_str[0] = vpid_str[0] = 0;
186 ptr->pids_cnt = 0;
187 ptr->freq = 0;
188 if(type == TUNER_TER)
190 fields = sscanf(&line[k], ter_conf,
191 &ptr->freq, inv, bw, cr, tmp_lcr, mod,
192 transm, gi, tmp_hier, vpid_str, apid_str);
193 mp_msg(MSGT_DEMUX, MSGL_V,
194 "TER, NUM: %d, NUM_FIELDS: %d, NAME: %s, FREQ: %d",
195 list->NUM_CHANNELS, fields, ptr->name, ptr->freq);
197 else if(type == TUNER_CBL)
199 fields = sscanf(&line[k], cbl_conf,
200 &ptr->freq, inv, &ptr->srate,
201 cr, mod, vpid_str, apid_str);
202 mp_msg(MSGT_DEMUX, MSGL_V,
203 "CBL, NUM: %d, NUM_FIELDS: %d, NAME: %s, FREQ: %d, SRATE: %d",
204 list->NUM_CHANNELS, fields, ptr->name, ptr->freq, ptr->srate);
206 #ifdef DVB_ATSC
207 else if(type == TUNER_ATSC)
209 fields = sscanf(&line[k], atsc_conf,
210 &ptr->freq, mod, vpid_str, apid_str);
211 mp_msg(MSGT_DEMUX, MSGL_V,
212 "ATSC, NUM: %d, NUM_FIELDS: %d, NAME: %s, FREQ: %d\n",
213 list->NUM_CHANNELS, fields, ptr->name, ptr->freq);
215 #endif
216 else //SATELLITE
218 fields = sscanf(&line[k], sat_conf,
219 &ptr->freq, &ptr->pol, &ptr->diseqc, &ptr->srate, vpid_str, apid_str);
220 ptr->pol = toupper(ptr->pol);
221 ptr->freq *= 1000UL;
222 ptr->srate *= 1000UL;
223 ptr->tone = -1;
224 ptr->inv = INVERSION_AUTO;
225 ptr->cr = FEC_AUTO;
226 if((ptr->diseqc > 4) || (ptr->diseqc < 0))
227 continue;
228 if(ptr->diseqc > 0)
229 ptr->diseqc--;
230 mp_msg(MSGT_DEMUX, MSGL_V,
231 "SAT, NUM: %d, NUM_FIELDS: %d, NAME: %s, FREQ: %d, SRATE: %d, POL: %c, DISEQC: %d",
232 list->NUM_CHANNELS, fields, ptr->name, ptr->freq, ptr->srate, ptr->pol, ptr->diseqc);
235 if(vpid_str[0])
237 pcnt = sscanf(vpid_str, "%d+%d+%d+%d+%d+%d+%d", &ptr->pids[0], &ptr->pids[1], &ptr->pids[2], &ptr->pids[3],
238 &ptr->pids[4], &ptr->pids[5], &ptr->pids[6]);
239 if(pcnt > 0)
241 ptr->pids_cnt = pcnt;
242 fields++;
246 if(apid_str[0])
248 cnt = ptr->pids_cnt;
249 pcnt = sscanf(apid_str, "%d+%d+%d+%d+%d+%d+%d+%d", &ptr->pids[cnt], &ptr->pids[cnt+1], &ptr->pids[cnt+2],
250 &ptr->pids[cnt+3], &ptr->pids[cnt+4], &ptr->pids[cnt+5], &ptr->pids[cnt+6], &ptr->pids[cnt+7]);
251 if(pcnt > 0)
253 ptr->pids_cnt += pcnt;
254 fields++;
258 if((fields < 2) || (ptr->pids_cnt <= 0) || (ptr->freq == 0) || (strlen(ptr->name) == 0))
259 continue;
261 has8192 = has0 = 0;
262 for(cnt = 0; cnt < ptr->pids_cnt; cnt++)
264 if(ptr->pids[cnt] == 8192)
265 has8192 = 1;
266 if(ptr->pids[cnt] == 0)
267 has0 = 1;
269 if(has8192)
271 ptr->pids[0] = 8192;
272 ptr->pids_cnt = 1;
274 else if(! has0)
276 ptr->pids[ptr->pids_cnt] = 0; //PID 0 is the PAT
277 ptr->pids_cnt++;
279 mp_msg(MSGT_DEMUX, MSGL_V, " PIDS: ");
280 for(cnt = 0; cnt < ptr->pids_cnt; cnt++)
281 mp_msg(MSGT_DEMUX, MSGL_V, " %d ", ptr->pids[cnt]);
282 mp_msg(MSGT_DEMUX, MSGL_V, "\n");
284 if((type == TUNER_TER) || (type == TUNER_CBL))
286 if(! strcmp(inv, "INVERSION_ON"))
287 ptr->inv = INVERSION_ON;
288 else if(! strcmp(inv, "INVERSION_OFF"))
289 ptr->inv = INVERSION_OFF;
290 else
291 ptr->inv = INVERSION_AUTO;
294 if(! strcmp(cr, "FEC_1_2"))
295 ptr->cr =FEC_1_2;
296 else if(! strcmp(cr, "FEC_2_3"))
297 ptr->cr =FEC_2_3;
298 else if(! strcmp(cr, "FEC_3_4"))
299 ptr->cr =FEC_3_4;
300 #ifdef HAVE_DVB_HEAD
301 else if(! strcmp(cr, "FEC_4_5"))
302 ptr->cr =FEC_4_5;
303 else if(! strcmp(cr, "FEC_6_7"))
304 ptr->cr =FEC_6_7;
305 else if(! strcmp(cr, "FEC_8_9"))
306 ptr->cr =FEC_8_9;
307 #endif
308 else if(! strcmp(cr, "FEC_5_6"))
309 ptr->cr =FEC_5_6;
310 else if(! strcmp(cr, "FEC_7_8"))
311 ptr->cr =FEC_7_8;
312 else if(! strcmp(cr, "FEC_NONE"))
313 ptr->cr =FEC_NONE;
314 else ptr->cr =FEC_AUTO;
318 if((type == TUNER_TER) || (type == TUNER_CBL) || (type == TUNER_ATSC))
320 if(! strcmp(mod, "QAM_128"))
321 ptr->mod = QAM_128;
322 else if(! strcmp(mod, "QAM_256"))
323 ptr->mod = QAM_256;
324 else if(! strcmp(mod, "QAM_64"))
325 ptr->mod = QAM_64;
326 else if(! strcmp(mod, "QAM_32"))
327 ptr->mod = QAM_32;
328 else if(! strcmp(mod, "QAM_16"))
329 ptr->mod = QAM_16;
330 #ifdef DVB_ATSC
331 else if(! strcmp(mod, "VSB_8") || ! strcmp(mod, "8VSB"))
332 ptr->mod = VSB_8;
333 else if(! strcmp(mod, "VSB_16") || !strcmp(mod, "16VSB"))
334 ptr->mod = VSB_16;
336 ptr->inv = INVERSION_AUTO;
337 #endif
340 if(type == TUNER_TER)
342 if(! strcmp(bw, "BANDWIDTH_6_MHZ"))
343 ptr->bw = BANDWIDTH_6_MHZ;
344 else if(! strcmp(bw, "BANDWIDTH_7_MHZ"))
345 ptr->bw = BANDWIDTH_7_MHZ;
346 else if(! strcmp(bw, "BANDWIDTH_8_MHZ"))
347 ptr->bw = BANDWIDTH_8_MHZ;
350 if(! strcmp(transm, "TRANSMISSION_MODE_2K"))
351 ptr->trans = TRANSMISSION_MODE_2K;
352 else if(! strcmp(transm, "TRANSMISSION_MODE_8K"))
353 ptr->trans = TRANSMISSION_MODE_8K;
356 if(! strcmp(gi, "GUARD_INTERVAL_1_32"))
357 ptr->gi = GUARD_INTERVAL_1_32;
358 else if(! strcmp(gi, "GUARD_INTERVAL_1_16"))
359 ptr->gi = GUARD_INTERVAL_1_16;
360 else if(! strcmp(gi, "GUARD_INTERVAL_1_8"))
361 ptr->gi = GUARD_INTERVAL_1_8;
362 else ptr->gi = GUARD_INTERVAL_1_4;
364 if(! strcmp(tmp_lcr, "FEC_1_2"))
365 ptr->cr_lp =FEC_1_2;
366 else if(! strcmp(tmp_lcr, "FEC_2_3"))
367 ptr->cr_lp =FEC_2_3;
368 else if(! strcmp(tmp_lcr, "FEC_3_4"))
369 ptr->cr_lp =FEC_3_4;
370 #ifdef HAVE_DVB_HEAD
371 else if(! strcmp(tmp_lcr, "FEC_4_5"))
372 ptr->cr_lp =FEC_4_5;
373 else if(! strcmp(tmp_lcr, "FEC_6_7"))
374 ptr->cr_lp =FEC_6_7;
375 else if(! strcmp(tmp_lcr, "FEC_8_9"))
376 ptr->cr_lp =FEC_8_9;
377 #endif
378 else if(! strcmp(tmp_lcr, "FEC_5_6"))
379 ptr->cr_lp =FEC_5_6;
380 else if(! strcmp(tmp_lcr, "FEC_7_8"))
381 ptr->cr_lp =FEC_7_8;
382 else if(! strcmp(tmp_lcr, "FEC_NONE"))
383 ptr->cr_lp =FEC_NONE;
384 else ptr->cr_lp =FEC_AUTO;
387 if(! strcmp(tmp_hier, "HIERARCHY_1"))
388 ptr->hier = HIERARCHY_1;
389 else if(! strcmp(tmp_hier, "HIERARCHY_2"))
390 ptr->hier = HIERARCHY_2;
391 else if(! strcmp(tmp_hier, "HIERARCHY_4"))
392 ptr->hier = HIERARCHY_4;
393 #ifdef HAVE_DVB_HEAD
394 else if(! strcmp(tmp_hier, "HIERARCHY_AUTO"))
395 ptr->hier = HIERARCHY_AUTO;
396 #endif
397 else ptr->hier = HIERARCHY_NONE;
400 tmp = realloc(list->channels, sizeof(dvb_channel_t) * (list->NUM_CHANNELS + 1));
401 if(tmp == NULL)
402 break;
404 list->channels = tmp;
405 memcpy(&(list->channels[list->NUM_CHANNELS]), ptr, sizeof(dvb_channel_t));
406 list->NUM_CHANNELS++;
407 if(sizeof(dvb_channel_t) * list->NUM_CHANNELS >= 1024*1024)
409 mp_msg(MSGT_DEMUX, MSGL_V, "dvbin.c, > 1MB allocated for channels struct, dropping the rest of the file\r\n");
410 break;
414 fclose(f);
415 if(list->NUM_CHANNELS == 0)
417 if(list->channels != NULL)
418 free(list->channels);
419 free(list);
420 return NULL;
423 list->current = 0;
424 return list;
427 void dvb_free_config(dvb_config_t *config)
429 int i, j;
431 for(i=0; i<config->count; i++)
433 if(config->cards[i].name)
434 free(config->cards[i].name);
435 if(!config->cards[i].list)
436 continue;
437 if(config->cards[i].list->channels)
439 for(j=0; j<config->cards[i].list->NUM_CHANNELS; j++)
441 if(config->cards[i].list->channels[j].name)
442 free(config->cards[i].list->channels[j].name);
444 free(config->cards[i].list->channels);
446 free(config->cards[i].list);
448 free(config);
451 static int dvb_streaming_read(stream_t *stream, char *buffer, int size)
453 struct pollfd pfds[1];
454 int pos=0, tries, rk, fd;
455 dvb_priv_t *priv = (dvb_priv_t *) stream->priv;
457 mp_msg(MSGT_DEMUX, MSGL_DBG3, "dvb_streaming_read(%d)\n", size);
459 tries = priv->retry + 1;
461 fd = stream->fd;
462 while(pos < size)
464 pfds[0].fd = fd;
465 pfds[0].events = POLLIN | POLLPRI;
467 rk = size - pos;
468 if(poll(pfds, 1, 500) <= 0)
470 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);
471 errno = 0;
472 if(--tries > 0)
473 continue;
474 else
475 break;
477 if((rk = read(fd, &buffer[pos], rk)) > 0)
479 pos += rk;
480 mp_msg(MSGT_DEMUX, MSGL_DBG3, "ret (%d) bytes\n", pos);
485 if(! pos)
486 mp_msg(MSGT_DEMUX, MSGL_ERR, "dvb_streaming_read, return %d bytes\n", pos);
488 return pos;
491 static void dvbin_close(stream_t *stream);
493 int dvb_set_channel(stream_t *stream, int card, int n)
495 dvb_channels_list *new_list;
496 dvb_channel_t *channel;
497 dvb_priv_t *priv = stream->priv;
498 char buf[4096];
499 dvb_config_t *conf = (dvb_config_t *) priv->config;
500 int devno;
501 int i;
503 if((card < 0) || (card > conf->count))
505 mp_msg(MSGT_DEMUX, MSGL_ERR, "dvb_set_channel: INVALID CARD NUMBER: %d vs %d, abort\n", card, conf->count);
506 return 0;
509 devno = conf->cards[card].devno;
510 new_list = conf->cards[card].list;
511 if((n > new_list->NUM_CHANNELS) || (n < 0))
513 mp_msg(MSGT_DEMUX, MSGL_ERR, "dvb_set_channel: INVALID CHANNEL NUMBER: %d, for card %d, abort\n", n, card);
514 return 0;
516 channel = &(new_list->channels[n]);
518 if(priv->is_on) //the fds are already open and we have to stop the demuxers
520 for(i = 0; i < priv->demux_fds_cnt; i++)
521 dvb_demux_stop(priv->demux_fds[i]);
523 priv->retry = 0;
524 while(dvb_streaming_read(stream, buf, 4096) > 0); //empty both the stream's and driver's buffer
525 if(priv->card != card)
527 dvbin_close(stream);
528 if(! dvb_open_devices(priv, devno, channel->pids_cnt))
530 mp_msg(MSGT_DEMUX, MSGL_ERR, "DVB_SET_CHANNEL, COULDN'T OPEN DEVICES OF CARD: %d, EXIT\n", card);
531 return 0;
534 else //close all demux_fds with pos > pids required for the new channel or open other demux_fds if we have too few
536 if(! dvb_fix_demuxes(priv, channel->pids_cnt))
537 return 0;
540 else
542 if(! dvb_open_devices(priv, devno, channel->pids_cnt))
544 mp_msg(MSGT_DEMUX, MSGL_ERR, "DVB_SET_CHANNEL2, COULDN'T OPEN DEVICES OF CARD: %d, EXIT\n", card);
545 return 0;
549 priv->card = card;
550 priv->list = new_list;
551 priv->retry = 5;
552 new_list->current = n;
553 stream->fd = priv->dvr_fd;
554 mp_msg(MSGT_DEMUX, MSGL_V, "DVB_SET_CHANNEL: new channel name=%s, card: %d, channel %d\n", channel->name, card, n);
556 stream->eof=1;
557 stream_reset(stream);
560 if(channel->freq != priv->last_freq)
561 if (! dvb_tune(priv, channel->freq, channel->pol, channel->srate, channel->diseqc, channel->tone,
562 channel->inv, channel->mod, channel->gi, channel->trans, channel->bw, channel->cr, channel->cr_lp, channel->hier, priv->timeout))
563 return 0;
565 priv->last_freq = channel->freq;
566 priv->is_on = 1;
568 //sets demux filters and restart the stream
569 for(i = 0; i < channel->pids_cnt; i++)
571 if(! dvb_set_ts_filt(priv->demux_fds[i], channel->pids[i], DMX_PES_OTHER))
572 return 0;
575 return 1;
580 int dvb_step_channel(stream_t *stream, int dir)
582 int new_current;
583 dvb_channels_list *list;
584 dvb_priv_t *priv = stream->priv;
586 mp_msg(MSGT_DEMUX, MSGL_V, "DVB_STEP_CHANNEL dir %d\n", dir);
588 if(priv == NULL)
590 mp_msg(MSGT_DEMUX, MSGL_ERR, "dvb_step_channel: NULL priv_ptr, quit\n");
591 return 0;
594 list = priv->list;
595 if(list == NULL)
597 mp_msg(MSGT_DEMUX, MSGL_ERR, "dvb_step_channel: NULL list_ptr, quit\n");
598 return 0;
601 new_current = (list->NUM_CHANNELS + list->current + (dir == DVB_CHANNEL_HIGHER ? 1 : -1)) % list->NUM_CHANNELS;
603 return dvb_set_channel(stream, priv->card, new_current);
609 static void dvbin_close(stream_t *stream)
611 int i;
612 dvb_priv_t *priv = (dvb_priv_t *) stream->priv;
614 for(i = priv->demux_fds_cnt-1; i >= 0; i--)
616 priv->demux_fds_cnt--;
617 mp_msg(MSGT_DEMUX, MSGL_V, "DVBIN_CLOSE, close(%d), fd=%d, COUNT=%d\n", i, priv->demux_fds[i], priv->demux_fds_cnt);
618 close(priv->demux_fds[i]);
620 close(priv->dvr_fd);
622 close(priv->fe_fd);
623 #ifdef HAVE_DVB
624 close(priv->sec_fd);
625 #endif
627 priv->is_on = 0;
628 dvb_free_config(priv->config);
632 static int dvb_streaming_start(stream_t *stream, struct stream_priv_s *opts, int tuner_type, char *progname)
634 int i;
635 dvb_channel_t *channel = NULL;
636 dvb_priv_t *priv = stream->priv;
638 mp_msg(MSGT_DEMUX, MSGL_V, "\r\ndvb_streaming_start(PROG: %s, CARD: %d, VID: %d, AID: %d, TYPE: %s, FILE: %s)\r\n",
639 opts->prog, opts->card, opts->vid, opts->aid, opts->type, opts->file);
641 priv->is_on = 0;
643 i = 0;
644 while((channel == NULL) && i < priv->list->NUM_CHANNELS)
646 if(! strcmp(priv->list->channels[i].name, progname))
647 channel = &(priv->list->channels[i]);
649 i++;
652 if(channel != NULL)
654 priv->list->current = i-1;
655 mp_msg(MSGT_DEMUX, MSGL_V, "PROGRAM NUMBER %d: name=%s, freq=%u\n", i-1, channel->name, channel->freq);
657 else
659 mp_msg(MSGT_DEMUX, MSGL_ERR, "\n\nDVBIN: no such channel \"%s\"\n\n", progname);
660 return 0;
664 if(!dvb_set_channel(stream, priv->card, priv->list->current))
666 mp_msg(MSGT_DEMUX, MSGL_ERR, "ERROR, COULDN'T SET CHANNEL %i: ", priv->list->current);
667 dvbin_close(stream);
668 return 0;
671 mp_msg(MSGT_DEMUX, MSGL_V, "SUCCESSFUL EXIT from dvb_streaming_start\n");
673 return 1;
679 static int dvb_open(stream_t *stream, int mode, void *opts, int *file_format)
681 // I don't force the file format bacause, although it's almost always TS,
682 // there are some providers that stream an IP multicast with M$ Mpeg4 inside
683 struct stream_priv_s* p = (struct stream_priv_s*)opts;
684 dvb_priv_t *priv;
685 char *progname;
686 int tuner_type = 0, i;
689 if(mode != STREAM_READ)
690 return STREAM_UNSUPPORTED;
692 stream->priv = calloc(1, sizeof(dvb_priv_t));
693 if(stream->priv == NULL)
694 return STREAM_ERROR;
696 priv = (dvb_priv_t *)stream->priv;
697 priv->config = dvb_get_config();
698 if(priv->config == NULL)
700 free(priv);
701 mp_msg(MSGT_DEMUX, MSGL_ERR, "DVB CONFIGURATION IS EMPTY, exit\n");
702 return STREAM_ERROR;
705 priv->card = -1;
706 for(i=0; i<priv->config->count; i++)
708 if(priv->config->cards[i].devno+1 == p->card)
710 priv->card = i;
711 break;
715 if(priv->card == -1)
717 free(priv);
718 mp_msg(MSGT_DEMUX, MSGL_ERR, "NO CONFIGURATION FOUND FOR CARD N. %d, exit\n", p->card);
719 return STREAM_ERROR;
721 priv->timeout = p->timeout;
723 tuner_type = priv->config->cards[priv->card].type;
725 if(tuner_type == 0)
727 free(priv);
728 mp_msg(MSGT_DEMUX, MSGL_V, "OPEN_DVB: UNKNOWN OR UNDETECTABLE TUNER TYPE, EXIT\n");
729 return STREAM_ERROR;
733 priv->tuner_type = tuner_type;
735 mp_msg(MSGT_DEMUX, MSGL_V, "OPEN_DVB: prog=%s, card=%d, type=%d, vid=%d, aid=%d\n",
736 p->prog, priv->card+1, priv->tuner_type, p->vid, p->aid);
738 priv->list = priv->config->cards[priv->card].list;
740 if((! strcmp(p->prog, "")) && (priv->list != NULL))
741 progname = priv->list->channels[0].name;
742 else
743 progname = p->prog;
746 if(! dvb_streaming_start(stream, p, tuner_type, progname))
748 free(stream->priv);
749 stream->priv = NULL;
750 return STREAM_ERROR;
753 stream->type = STREAMTYPE_DVB;
754 stream->fill_buffer = dvb_streaming_read;
755 stream->close = dvbin_close;
756 m_struct_free(&stream_opts, opts);
758 *file_format = DEMUXER_TYPE_MPEG_TS;
760 return STREAM_OK;
763 #define MAX_CARDS 4
764 dvb_config_t *dvb_get_config(void)
766 int i, fd, type, size;
767 char filename[30], *conf_file, *name;
768 dvb_channels_list *list;
769 dvb_card_config_t *cards = NULL, *tmp;
770 dvb_config_t *conf = NULL;
773 conf = malloc(sizeof(dvb_config_t));
774 if(conf == NULL)
775 return NULL;
777 conf->priv = NULL;
778 conf->count = 0;
779 conf->cards = NULL;
780 for(i=0; i<MAX_CARDS; i++)
782 snprintf(filename, sizeof(filename), "/dev/dvb/adapter%d/frontend0", i);
783 fd = open(filename, O_RDONLY|O_NONBLOCK);
784 if(fd < 0)
786 mp_msg(MSGT_DEMUX, MSGL_V, "DVB_CONFIG, can't open device %s, skipping\n", filename);
787 continue;
790 type = dvb_get_tuner_type(fd);
791 close(fd);
792 if(type != TUNER_SAT && type != TUNER_TER && type != TUNER_CBL && type != TUNER_ATSC)
794 mp_msg(MSGT_DEMUX, MSGL_V, "DVB_CONFIG, can't detect tuner type of card %d, skipping\n", i);
795 continue;
798 switch(type)
800 case TUNER_TER:
801 conf_file = get_path("channels.conf.ter");
802 break;
803 case TUNER_CBL:
804 conf_file = get_path("channels.conf.cbl");
805 break;
806 case TUNER_SAT:
807 conf_file = get_path("channels.conf.sat");
808 break;
809 case TUNER_ATSC:
810 conf_file = get_path("channels.conf.atsc");
811 break;
814 if((access(conf_file, F_OK | R_OK) != 0))
816 if(conf_file)
817 free(conf_file);
818 conf_file = get_path("channels.conf");
821 list = dvb_get_channels(conf_file, type);
822 if(conf_file)
823 free(conf_file);
824 if(list == NULL)
825 continue;
827 size = sizeof(dvb_card_config_t) * (conf->count + 1);
828 tmp = realloc(conf->cards, size);
830 if(tmp == NULL)
832 fprintf(stderr, "DVB_CONFIG, can't realloc %d bytes, skipping\n", size);
833 continue;
835 cards = tmp;
837 name = malloc(20);
838 if(name==NULL)
840 fprintf(stderr, "DVB_CONFIG, can't realloc 20 bytes, skipping\n");
841 continue;
844 conf->cards = cards;
845 conf->cards[conf->count].devno = i;
846 conf->cards[conf->count].list = list;
847 conf->cards[conf->count].type = type;
848 snprintf(name, 20, "DVB-%c card n. %d", type==TUNER_TER ? 'T' : (type==TUNER_CBL ? 'C' : 'S'), conf->count+1);
849 conf->cards[conf->count].name = name;
850 conf->count++;
853 if(conf->count == 0)
855 free(conf);
856 conf = NULL;
859 return conf;
864 const stream_info_t stream_info_dvb = {
865 "Dvb Input",
866 "dvbin",
867 "Nico",
868 "based on the code from ??? (probably Arpi)",
869 dvb_open,
870 { "dvb", NULL },
871 &stream_opts,
872 1 // Urls are an option string