1 #include <linux/dvb/frontend.h>
2 #include <linux/dvb/dmx.h>
3 #include <linux/dvb/audio.h>
4 #include <linux/dvb/version.h>
12 #define ARRAY_SIZE(arr) (sizeof(arr) / sizeof((arr)[0]))
17 unsigned int frequency;
20 static int search(int adapter_nr,struct channel *ch)
24 struct dvb_frontend_info info;
25 struct channel *channel;
26 struct dtv_property prop[3];
27 struct dtv_properties props;
31 sprintf(file, "/dev/dvb/adapter%d/frontend0", adapter_nr);
32 if ((fd = open(file, (O_RDWR | O_CREAT | O_TRUNC), 0666)) < 0) {
37 if (ioctl(fd, FE_GET_INFO, &info) < 0) {
38 perror("ioctl FE_GET_INFO");
42 if (info.type == FE_QPSK) {
44 //channel = lookup_channel(channel_id, isdbs_channels,
45 // ARRAY_SIZE(isdbs_channels));
46 } else if (info.type == FE_OFDM) {
48 //channel = lookup_channel(channel_id, isdbt_channels,
49 // ARRAY_SIZE(isdbt_channels));
51 fprintf(stderr, "Unknown type of adapter\n");
54 if (channel == NULL) {
55 fprintf(stderr, "Unknown id of channel\n");
59 prop[0].cmd = DTV_FREQUENCY;
60 prop[0].u.data = channel->frequency;
61 prop[1].cmd = DTV_ISDBS_TS_ID;
62 prop[1].u.data = channel->ts_id;
63 prop[2].cmd = DTV_TUNE;
68 if ((ioctl(fd, FE_SET_PROPERTY, &props)) < 0) {
69 perror("ioctl FE_SET_PROPERTY");
73 for (i = 0; i < 4; i++) {
74 if (ioctl(fd, FE_READ_STATUS, &status) < 0) {
75 perror("ioctl FE_READ_STATUS");
78 if (status & FE_HAS_LOCK) {
79 fprintf(stderr, "Successfully tuned to %d(%d) .\n",
80 channel->frequency,channel->ts_id);
86 fprintf(stderr, "Failed to tune to %s (status %02x).\n",
87 channel->name, status);
94 static int track(int adapter_nr)
98 struct dmx_pes_filter_params filter;
101 filter.input = DMX_IN_FRONTEND;
102 filter.output = DMX_OUT_TS_TAP;
103 filter.pes_type = DMX_PES_VIDEO;
104 filter.flags = DMX_IMMEDIATE_START;
106 sprintf(file, "/dev/dvb/adapter%d/demux0", adapter_nr);
107 if ((fd = open(file, O_RDWR)) < 0) {
113 if (ioctl(fd, DMX_SET_PES_FILTER, &filter) < 0) {
114 perror("ioctl DMX_SET_PES_FILTER");
120 void record(int adapter_nr, char* output, int rectime) {
123 time_t start_time, current_time;
128 fout = open(output, (O_WRONLY | O_CREAT | O_TRUNC));
130 printf("output file open failed\n");
133 sprintf(input, "/dev/dvb/adapter%d/dvr0", adapter_nr);
134 start_time = time(NULL);
135 fin = open(input, (O_RDONLY));
136 while ( rt = read(fin, buf, 1316) ) {
137 while ( wt = write(fout, buf, rt) ) {
139 if ( rt == 0 ) break;
141 printf("output file write failed\n");
145 current_time = time(NULL);
146 if ( current_time - start_time > rectime ) {
156 int main(int argc, char *argv[]) {
165 fprintf(stderr, "Usage: %s adapter_nr freq [tsid]\n", argv[0]);
169 adapter_nr = strtol(argv[1], NULL, 0);
170 channel_freq = strtol(argv[2], NULL, 10);
173 channel_id = strtol(argv[3], NULL, 10);
176 rectime = atoi(argv[4]);
178 struct channel ch1 ={0,"",channel_freq,channel_id};
180 //struct channel ch;//{ 1, "NHK BS-1", 1318000, 0x40f1 }
182 fd = search(adapter_nr, ch);
186 ret = track(adapter_nr);
187 record(adapter_nr, argv[5], rectime);