📄 stream_dvb.c
字号:
static int dvb_streaming_read(stream_t *stream, char *buffer, int size){ struct pollfd pfds[1]; int pos=0, tries, rk, fd; dvb_priv_t *priv = (dvb_priv_t *) stream->priv; mp_msg(MSGT_DEMUX, MSGL_DBG3, "dvb_streaming_read(%d)\n", size); tries = priv->retry + 1; fd = stream->fd; while(pos < size) { pfds[0].fd = fd; pfds[0].events = POLLIN | POLLPRI; rk = size - pos; if(poll(pfds, 1, 500) <= 0) { 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); errno = 0; if(--tries > 0) continue; else break; } if((rk = read(fd, &buffer[pos], rk)) > 0) { pos += rk; mp_msg(MSGT_DEMUX, MSGL_DBG3, "ret (%d) bytes\n", pos); } } if(! pos) mp_msg(MSGT_DEMUX, MSGL_ERR, "dvb_streaming_read, return %d bytes\n", pos); return pos;}static void dvbin_close(stream_t *stream);int dvb_set_channel(dvb_priv_t *priv, int card, int n){ dvb_channels_list *new_list; dvb_channel_t *channel; stream_t *stream = (stream_t*) priv->stream; char buf[4096]; dvb_config_t *conf = (dvb_config_t *) priv->config; int devno; int i; if((card < 0) || (card > conf->count)) { mp_msg(MSGT_DEMUX, MSGL_ERR, "dvb_set_channel: INVALID CARD NUMBER: %d vs %d, abort\n", card, conf->count); return 0; } devno = conf->cards[card].devno; new_list = conf->cards[card].list; if((n > new_list->NUM_CHANNELS) || (n < 0)) { mp_msg(MSGT_DEMUX, MSGL_ERR, "dvb_set_channel: INVALID CHANNEL NUMBER: %d, for card %d, abort\n", n, card); return 0; } channel = &(new_list->channels[n]); if(priv->is_on) //the fds are already open and we have to stop the demuxers { for(i = 0; i < priv->demux_fds_cnt; i++) dvb_demux_stop(priv->demux_fds[i]); priv->retry = 0; while(dvb_streaming_read(stream, buf, 4096) > 0); //empty both the stream's and driver's buffer if(priv->card != card) { dvbin_close(stream); if(! dvb_open_devices(priv, devno, channel->pids_cnt, channel->pids)) { mp_msg(MSGT_DEMUX, MSGL_ERR, "DVB_SET_CHANNEL, COULDN'T OPEN DEVICES OF CARD: %d, EXIT\n", card); return 0; } } else //close all demux_fds with pos > pids required for the new channel or open other demux_fds if we have too few { if(! dvb_fix_demuxes(priv, channel->pids_cnt, channel->pids)) return 0; } } else { if(! dvb_open_devices(priv, devno, channel->pids_cnt, channel->pids)) { mp_msg(MSGT_DEMUX, MSGL_ERR, "DVB_SET_CHANNEL2, COULDN'T OPEN DEVICES OF CARD: %d, EXIT\n", card); return 0; } } dvb_config->priv = priv; priv->card = card; priv->list = new_list; priv->retry = 5; new_list->current = n; stream->fd = priv->dvr_fd; mp_msg(MSGT_DEMUX, MSGL_V, "DVB_SET_CHANNEL: new channel name=%s, card: %d, channel %d\n", channel->name, card, n); stream->eof=1; stream_reset(stream); if(channel->freq != priv->last_freq) if (! dvb_tune(priv, channel->freq, channel->pol, channel->srate, channel->diseqc, channel->tone, channel->inv, channel->mod, channel->gi, channel->trans, channel->bw, channel->cr, channel->cr_lp, channel->hier, priv->timeout)) return 0; priv->last_freq = channel->freq; priv->is_on = 1; //sets demux filters and restart the stream for(i = 0; i < channel->pids_cnt; i++) { if(! dvb_set_ts_filt(priv->demux_fds[i], channel->pids[i], DMX_PES_OTHER)) return 0; } return 1;}int dvb_step_channel(dvb_priv_t *priv, int dir){ int new_current; dvb_channels_list *list; mp_msg(MSGT_DEMUX, MSGL_V, "DVB_STEP_CHANNEL dir %d\n", dir); if(priv == NULL) { mp_msg(MSGT_DEMUX, MSGL_ERR, "dvb_step_channel: NULL priv_ptr, quit\n"); return 0; } list = priv->list; if(list == NULL) { mp_msg(MSGT_DEMUX, MSGL_ERR, "dvb_step_channel: NULL list_ptr, quit\n"); return 0; } new_current = (list->NUM_CHANNELS + list->current + (dir == DVB_CHANNEL_HIGHER ? 1 : -1)) % list->NUM_CHANNELS; return dvb_set_channel(priv, priv->card, new_current);}static void dvbin_close(stream_t *stream){ int i; dvb_priv_t *priv = (dvb_priv_t *) stream->priv; for(i = priv->demux_fds_cnt-1; i >= 0; i--) { priv->demux_fds_cnt--; mp_msg(MSGT_DEMUX, MSGL_V, "DVBIN_CLOSE, close(%d), fd=%d, COUNT=%d\n", i, priv->demux_fds[i], priv->demux_fds_cnt); close(priv->demux_fds[i]); } close(priv->dvr_fd); close(priv->fe_fd);#ifdef HAVE_DVB close(priv->sec_fd);#endif priv->is_on = 0; dvb_config->priv = NULL;}static int dvb_streaming_start(dvb_priv_t *priv, struct stream_priv_s *opts, int tuner_type, char *progname){ int i; dvb_channel_t *channel = NULL; stream_t *stream = (stream_t*) priv->stream; mp_msg(MSGT_DEMUX, MSGL_V, "\r\ndvb_streaming_start(PROG: %s, CARD: %d, VID: %d, AID: %d, TYPE: %s, FILE: %s)\r\n", opts->prog, opts->card, opts->vid, opts->aid, opts->type, opts->file); priv->is_on = 0; i = 0; while((channel == NULL) && i < priv->list->NUM_CHANNELS) { if(! strcmp(priv->list->channels[i].name, progname)) channel = &(priv->list->channels[i]); i++; } if(channel != NULL) { priv->list->current = i-1; mp_msg(MSGT_DEMUX, MSGL_V, "PROGRAM NUMBER %d: name=%s, freq=%u\n", i-1, channel->name, channel->freq); } else { mp_msg(MSGT_DEMUX, MSGL_ERR, "\n\nDVBIN: no such channel \"%s\"\n\n", progname); return 0; } if(!dvb_set_channel(priv, priv->card, priv->list->current)) { mp_msg(MSGT_DEMUX, MSGL_ERR, "ERROR, COULDN'T SET CHANNEL %i: ", priv->list->current); dvbin_close(stream); return 0; } mp_msg(MSGT_DEMUX, MSGL_V, "SUCCESSFUL EXIT from dvb_streaming_start\n"); return 1;}static int dvb_open(stream_t *stream, int mode, void *opts, int *file_format){ // I don't force the file format bacause, although it's almost always TS, // there are some providers that stream an IP multicast with M$ Mpeg4 inside struct stream_priv_s* p = (struct stream_priv_s*)opts; dvb_priv_t *priv; char *progname; int tuner_type = 0, i; if(mode != STREAM_READ) return STREAM_UNSUPPORTED; stream->priv = calloc(1, sizeof(dvb_priv_t)); if(stream->priv == NULL) return STREAM_ERROR; priv = (dvb_priv_t *)stream->priv; priv->stream = stream; dvb_config = dvb_get_config(); if(dvb_config == NULL) { free(priv); mp_msg(MSGT_DEMUX, MSGL_ERR, "DVB CONFIGURATION IS EMPTY, exit\n"); return STREAM_ERROR; } dvb_config->priv = priv; priv->config = dvb_config; priv->card = -1; for(i=0; i<priv->config->count; i++) { if(priv->config->cards[i].devno+1 == p->card) { priv->card = i; break; } } if(priv->card == -1) { free(priv); mp_msg(MSGT_DEMUX, MSGL_ERR, "NO CONFIGURATION FOUND FOR CARD N. %d, exit\n", p->card); return STREAM_ERROR; } priv->timeout = p->timeout; tuner_type = priv->config->cards[priv->card].type; if(tuner_type == 0) { free(priv); mp_msg(MSGT_DEMUX, MSGL_V, "OPEN_DVB: UNKNOWN OR UNDETECTABLE TUNER TYPE, EXIT\n"); return STREAM_ERROR; } priv->tuner_type = tuner_type; mp_msg(MSGT_DEMUX, MSGL_V, "OPEN_DVB: prog=%s, card=%d, type=%d, vid=%d, aid=%d\n", p->prog, priv->card+1, priv->tuner_type, p->vid, p->aid); priv->list = priv->config->cards[priv->card].list; if((! strcmp(p->prog, "")) && (priv->list != NULL)) progname = priv->list->channels[0].name; else progname = p->prog; if(! dvb_streaming_start(priv, p, tuner_type, progname)) { free(stream->priv); stream->priv = NULL; return STREAM_ERROR; } stream->type = STREAMTYPE_DVB; stream->fill_buffer = dvb_streaming_read; stream->close = dvbin_close; m_struct_free(&stream_opts, opts); *file_format = DEMUXER_TYPE_MPEG_TS; return STREAM_OK;}#define MAX_CARDS 4dvb_config_t *dvb_get_config(void){ int i, fd, type, size; char filename[30], *conf_file, *name; dvb_channels_list *list; dvb_card_config_t *cards = NULL, *tmp; dvb_config_t *conf = NULL; if(dvb_config != NULL) return dvb_config; conf = malloc(sizeof(dvb_config_t)); if(conf == NULL) return NULL; conf->priv = NULL; conf->count = 0; conf->cards = NULL; for(i=0; i<MAX_CARDS; i++) { snprintf(filename, sizeof(filename), "/dev/dvb/adapter%d/frontend0", i); fd = open(filename, O_RDONLY|O_NONBLOCK); if(fd < 0) { mp_msg(MSGT_DEMUX, MSGL_V, "DVB_CONFIG, can't open device %s, skipping\n", filename); continue; } type = dvb_get_tuner_type(fd); close(fd); if(type != TUNER_SAT && type != TUNER_TER && type != TUNER_CBL && type != TUNER_ATSC) { mp_msg(MSGT_DEMUX, MSGL_V, "DVB_CONFIG, can't detect tuner type of card %d, skipping\n", i); continue; } switch(type) { case TUNER_TER: conf_file = get_path("channels.conf.ter"); break; case TUNER_CBL: conf_file = get_path("channels.conf.cbl"); break; case TUNER_SAT: conf_file = get_path("channels.conf.sat"); break; case TUNER_ATSC: conf_file = get_path("channels.conf.atsc"); break; } if((access(conf_file, F_OK | R_OK) != 0)) conf_file = get_path("channels.conf"); list = dvb_get_channels(conf_file, type); if(list == NULL) continue; size = sizeof(dvb_card_config_t) * (conf->count + 1); tmp = realloc(conf->cards, size); if(tmp == NULL) { fprintf(stderr, "DVB_CONFIG, can't realloc %d bytes, skipping\n", size); continue; } cards = tmp; name = malloc(20); if(name==NULL) { fprintf(stderr, "DVB_CONFIG, can't realloc 20 bytes, skipping\n"); continue; } conf->cards = cards; conf->cards[conf->count].devno = i; conf->cards[conf->count].list = list; conf->cards[conf->count].type = type; snprintf(name, 20, "DVB-%c card n. %d", type==TUNER_TER ? 'T' : (type==TUNER_CBL ? 'C' : 'S'), conf->count+1); conf->cards[conf->count].name = name; conf->count++; } if(conf->count == 0) { free(conf); conf = NULL; } dvb_config = conf; return conf;}stream_info_t stream_info_dvb = { "Dvb Input", "dvbin", "Nico", "based on the code from ??? (probably Arpi)", dvb_open, { "dvb", NULL }, &stream_opts, 1 // Urls are an option string};
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -