OSDN Git Service

fix tunerec
[rec10/rec10-git.git] / tunerec / tunerec.c
1 #include <linux/dvb/frontend.h>
2 #include <linux/dvb/dmx.h>
3 #include <linux/dvb/audio.h>
4 #include <linux/dvb/version.h>
5 #include <sys/ioctl.h>
6 #include <fcntl.h>
7 #include <unistd.h>
8 #include <stdio.h>
9 #include <stdlib.h>
10
11 #define ARRAY_SIZE(arr) (sizeof(arr) / sizeof((arr)[0]))
12
13 struct channel {
14         int id;
15         const char *name;
16         unsigned int frequency;
17         unsigned int ts_id;
18 };
19 static int search(int adapter_nr,struct channel *ch)
20 {
21         char file[256];
22         int fd;
23         struct dvb_frontend_info info;
24         struct channel *channel;
25         struct dtv_property prop[3];
26         struct dtv_properties props;
27         int i;
28         fe_status_t status;
29
30         sprintf(file, "/dev/dvb/adapter%d/frontend0", adapter_nr);
31         if ((fd = open(file, O_RDWR)) < 0) {
32                 perror("open");
33                 return -1;
34         }
35
36         if (ioctl(fd, FE_GET_INFO, &info) < 0) {
37                 perror("ioctl FE_GET_INFO");
38                 goto out;
39         }
40
41         if (info.type == FE_QPSK) {
42                 channel = ch;
43                 //channel = lookup_channel(channel_id, isdbs_channels,
44                 //                       ARRAY_SIZE(isdbs_channels));
45         } else if (info.type == FE_OFDM) {
46                 channel = ch;
47                 //channel = lookup_channel(channel_id, isdbt_channels,
48                 //                       ARRAY_SIZE(isdbt_channels));
49         } else {
50                 fprintf(stderr, "Unknown type of adapter\n");
51                 goto out;
52         }
53         if (channel == NULL) {
54                 fprintf(stderr, "Unknown id of channel\n");
55                 goto out;
56         }
57
58         prop[0].cmd = DTV_FREQUENCY;
59         prop[0].u.data = channel->frequency;
60         prop[1].cmd = DTV_ISDBS_TS_ID;
61         prop[1].u.data = channel->ts_id;
62         prop[2].cmd = DTV_TUNE;
63
64         props.props = prop;
65         props.num = 3;
66
67         if ((ioctl(fd, FE_SET_PROPERTY, &props)) < 0) {
68                 perror("ioctl FE_SET_PROPERTY");
69                 goto out;
70         }
71
72         for (i = 0; i < 4; i++) {
73                 if (ioctl(fd, FE_READ_STATUS, &status) < 0) {
74                         perror("ioctl FE_READ_STATUS");
75                         goto out;
76                 }
77                 if (status & FE_HAS_LOCK) {
78                         fprintf(stderr, "Successfully tuned to %d(%d) .\n",
79                                 channel->frequency,channel->ts_id);
80                         return 0;
81                 }
82                 usleep(250 * 1000);
83         }
84
85         fprintf(stderr, "Failed to tune to %s (status %02x).\n",
86                 channel->name, status);
87
88 out:
89         close(fd);
90         return -1;
91 }
92
93 static int track(int adapter_nr)
94 {
95         char file[256];
96         int fd;
97         struct dmx_pes_filter_params filter;
98
99         filter.pid = 0x2000;
100         filter.input = DMX_IN_FRONTEND;
101         filter.output = DMX_OUT_TS_TAP;
102         filter.pes_type =  DMX_PES_VIDEO;
103         filter.flags = DMX_IMMEDIATE_START;
104
105         sprintf(file, "/dev/dvb/adapter%d/demux0", adapter_nr);
106         if ((fd = open(file, O_RDWR)) < 0) {
107                 perror("open");
108                 return -1;
109         }
110
111
112         if (ioctl(fd, DMX_SET_PES_FILTER, &filter) < 0) {
113                 perror("ioctl DMX_SET_PES_FILTER");
114                 close(fd);
115                 return -1;
116         }
117
118         while (1)
119                 sleep(3);
120
121         /* never returns */
122 }
123
124 int main(int argc, char *argv[]) {
125         int adapter_nr;
126         int channel_id;
127         int channel_freq;
128         int fd;
129         int ret;
130
131         if (argc <= 2) {
132                 fprintf(stderr, "Usage: %s adapter_nr freq [tsid]\n", argv[0]);
133                 return 1;
134         }
135         struct channel *ch;
136         adapter_nr = strtol(argv[1], NULL, 0);
137         channel_freq = strtol(argv[2], NULL, 10);
138         channel_id=0;
139         if (argc == 4){
140                 channel_id = strtol(argv[3], NULL, 10);
141         }
142         struct channel ch1 ={0,"",channel_freq,channel_id};
143         ch = &ch1;
144         //struct channel ch;//{   1, "NHK BS-1",          1318000, 0x40f1 }
145         //ch={0,"",argv}
146         fd = search(adapter_nr, ch);
147         if (fd < 0)
148                 return 1;
149
150         ret = track(adapter_nr);
151         close(fd);
152
153         return ret < 0;
154 }