Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <linux/dvb/frontend.h>
- #include <linux/dvb/dmx.h>
- #include <linux/dvb/audio.h>
- #include <linux/dvb/version.h>
- #include <sys/ioctl.h>
- #include <fcntl.h>
- #include <unistd.h>
- #include <stdio.h>
- #include <stdlib.h>
- #define ARRAY_SIZE(arr) (sizeof(arr) / sizeof((arr)[0]))
- struct channel {
- int id;
- const char *name;
- unsigned int frequency;
- unsigned int ts_id;
- };
- static struct channel isdbt_channels[] = {
- { 13, "13ch", 473142857 },
- { 14, "14ch", 479142857 },
- { 15, "15ch", 485142857 },
- { 16, "16ch", 491142857 },
- { 17, "17ch", 497142857 },
- { 18, "18ch", 503142857 },
- { 19, "19ch", 509142857 },
- { 20, "20ch", 509142857 },
- { 21, "21ch", 515142857 },
- { 22, "22ch", 521142857 },
- { 23, "23ch", 527142857 },
- { 24, "24ch", 533142857 },
- { 25, "25ch", 539142857 },
- { 26, "26ch", 545142857 },
- { 27, "27ch", 551142857 },
- { 28, "28ch", 557142857 },
- { 29, "29ch", 563142857 },
- { 30, "30ch", 569142857 },
- { 31, "31ch", 575142857 },
- { 32, "32ch", 581142857 },
- { 33, "33ch", 587142857 },
- { 34, "34ch", 593142857 },
- { 35, "35ch", 599142857 },
- { 36, "36ch", 605142857 },
- { 37, "37ch", 611142857 },
- { 38, "38ch", 617142857 },
- { 39, "39ch", 623142857 },
- { 40, "40ch", 629142857 },
- { 41, "41ch", 635142857 },
- { 42, "42ch", 641142857 },
- { 43, "43ch", 647142857 },
- { 44, "44ch", 653142857 },
- { 45, "45ch", 659142857 },
- { 46, "46ch", 665142857 },
- { 47, "47ch", 671142857 },
- { 48, "48ch", 677142857 },
- { 49, "49ch", 683142857 },
- { 50, "50ch", 689142857 },
- { 51, "51ch", 695142857 },
- { 52, "52ch", 701142857 },
- { 53, "53ch", 707142857 },
- { 54, "54ch", 713142857 },
- { 55, "55ch", 719142857 },
- { 56, "56ch", 725142857 },
- { 57, "57ch", 731142857 },
- { 58, "58ch", 737142857 },
- { 59, "59ch", 743142857 },
- { 60, "60ch", 749142857 },
- { 61, "61ch", 755142857 },
- { 62, "62ch", 761142857 },
- };
- static struct channel isdbs_channels[] = {
- { 101, "NHK BS-1", 1318000, 0x40f1 },
- { 102, "NHK BS-2", 1318000, 0x40f1 },
- { 103, "NHK BS-Hi", 1318000, 0x40f2 },
- { 141, "BS日テレ", 1279640, 0x40d0 },
- { 151, "BS朝日", 1049480, 0x4010 },
- { 161, "BS-i", 1049480, 0x4011 },
- { 171, "BSジャパン", 1087840, 0x4031 },
- { 181, "BSフジ", 1279640, 0x40d1 },
- { 191, "WOWOW", 1087840, 0x4030 },
- { 200, "STAR CHANNEL HV", 1202920, 0x4091 },
- { 211, "BS11", 1202920, 0x4090 },
- { 222, "TwellV", 1202920, 0x4092 },
- };
- static struct channel *
- lookup_channel(int id, struct channel *channels, int nr)
- {
- int i;
- struct channel *channel;
- for (i = 0; i < nr; i++) {
- channel = &channels[i];
- if (channel->id == id)
- return channel;
- }
- return NULL;
- }
- static int search(int adapter_nr, int channel_id)
- {
- char file[256];
- int fd;
- struct dvb_frontend_info info;
- struct channel *channel;
- struct dtv_property prop[3];
- struct dtv_properties props;
- int i;
- fe_status_t status;
- sprintf(file, "/dev/dvb/adapter%d/frontend0", adapter_nr);
- if ((fd = open(file, O_RDWR)) < 0) {
- perror("open");
- return -1;
- }
- if (ioctl(fd, FE_GET_INFO, &info) < 0) {
- perror("ioctl FE_GET_INFO");
- goto out;
- }
- if (info.type == FE_QPSK) {
- channel = lookup_channel(channel_id, isdbs_channels,
- ARRAY_SIZE(isdbs_channels));
- } else if (info.type == FE_OFDM) {
- channel = lookup_channel(channel_id, isdbt_channels,
- ARRAY_SIZE(isdbt_channels));
- } else {
- fprintf(stderr, "Unknown type of adapter\n");
- goto out;
- }
- if (channel == NULL) {
- fprintf(stderr, "Unknown id of channel\n");
- goto out;
- }
- prop[0].cmd = DTV_FREQUENCY;
- prop[0].u.data = channel->frequency;
- prop[1].cmd = DTV_ISDBS_TS_ID_LEGACY;
- prop[1].u.data = channel->ts_id;
- prop[2].cmd = DTV_TUNE;
- props.props = prop;
- props.num = 3;
- if ((ioctl(fd, FE_SET_PROPERTY, &props)) < 0) {
- perror("ioctl FE_SET_PROPERTY");
- goto out;
- }
- for (i = 0; i < 4; i++) {
- if (ioctl(fd, FE_READ_STATUS, &status) < 0) {
- perror("ioctl FE_READ_STATUS");
- goto out;
- }
- if (status & FE_HAS_LOCK) {
- fprintf(stderr, "Successfully tuned to %s .\n",
- channel->name);
- return 0;
- }
- sleep(1);
- }
- fprintf(stderr, "Failed to tune to %s (status %02x).\n",
- channel->name, status);
- out:
- close(fd);
- return -1;
- }
- static int track(int adapter_nr)
- {
- char file[256];
- int fd;
- struct dmx_pes_filter_params filter;
- filter.pid = 0x2000;
- filter.input = DMX_IN_FRONTEND;
- filter.output = DMX_OUT_TS_TAP;
- filter.pes_type = DMX_PES_VIDEO;
- filter.flags = DMX_IMMEDIATE_START;
- sprintf(file, "/dev/dvb/adapter%d/demux0", adapter_nr);
- if ((fd = open(file, O_RDWR)) < 0) {
- perror("open");
- return -1;
- }
- if (ioctl(fd, DMX_SET_PES_FILTER, &filter) < 0) {
- perror("ioctl DMX_SET_PES_FILTER");
- close(fd);
- return -1;
- }
- while (1)
- sleep(3);
- /* never returns */
- }
- int main(int argc, char *argv[]) {
- int adapter_nr;
- int channel_id;
- int fd;
- int ret;
- if (argc <= 2) {
- fprintf(stderr, "Usage: %s adapter_nr channel_id\n", argv[0]);
- return 1;
- }
- adapter_nr = strtol(argv[1], NULL, 0);
- channel_id = strtol(argv[2], NULL, 10);
- fd = search(adapter_nr, channel_id);
- if (fd < 0)
- return 1;
- ret = track(adapter_nr);
- close(fd);
- return ret < 0;
- }
Add Comment
Please, Sign In to add comment