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]))
14 static int search(int adapter_nr, unsigned int frequency, unsigned int ts_id)
18 struct dvb_frontend_info info;
19 struct dtv_property prop[3];
20 struct dtv_properties props;
24 sprintf(file, "/dev/dvb/adapter%d/frontend0", adapter_nr);
25 if ((fd = open(file, (O_RDWR | O_CREAT | O_TRUNC), 0666)) < 0) {
30 if (ioctl(fd, FE_GET_INFO, &info) < 0) {
31 perror("ioctl FE_GET_INFO");
35 if (info.type == FE_QPSK || info.type == FE_OFDM) {
37 fprintf(stderr, "Unknown type of adapter\n");
41 prop[0].cmd = DTV_FREQUENCY;
42 prop[0].u.data = frequency;
43 prop[1].cmd = DTV_STREAM_ID;
44 prop[1].u.data = ts_id;
45 prop[2].cmd = DTV_TUNE;
50 if ((ioctl(fd, FE_SET_PROPERTY, &props)) < 0) {
51 perror("ioctl FE_SET_PROPERTY");
55 for (i = 0; i < 4; i++) {
56 if (ioctl(fd, FE_READ_STATUS, &status) < 0) {
57 perror("ioctl FE_READ_STATUS");
60 if (status & FE_HAS_LOCK) {
61 fprintf(stderr, "Successfully tuned to %d(%d).\n",
68 fprintf(stderr, "Failed to tune (status %02x).\n", status);
75 static int track(int adapter_nr)
79 struct dmx_pes_filter_params filter;
82 filter.input = DMX_IN_FRONTEND;
83 filter.output = DMX_OUT_TS_TAP;
84 filter.pes_type = DMX_PES_VIDEO;
85 filter.flags = DMX_IMMEDIATE_START;
87 sprintf(file, "/dev/dvb/adapter%d/demux0", adapter_nr);
88 if ((fd = open(file, O_RDWR)) < 0) {
93 if (ioctl(fd, DMX_SET_PES_FILTER, &filter) < 0) {
94 perror("ioctl DMX_SET_PES_FILTER");
101 void record(int adapter_nr, char* output, int rectime) {
104 time_t start_time, current_time;
106 ssize_t rt, wt, shift;
108 fout = open(output, (O_WRONLY | O_CREAT | O_TRUNC), 0666);
110 printf("output file open failed\n");
113 sprintf(input, "/dev/dvb/adapter%d/dvr0", adapter_nr);
114 start_time = time(NULL);
115 fin = open(input, (O_RDONLY));
116 while ( rt = read(fin, buf, 1316) > 0 ) {
118 while ( wt = write(fout, buf + shift, rt) > 0) {
120 if ( rt == 0 ) break;
124 // [buf] is not correctly written to [fout]
125 printf("output file write failed\n");
129 // TODO: time() at each 1316 bytes read is not efficient, reduce frequency
130 current_time = time(NULL);
131 if ( current_time - start_time > rectime ) {
141 int main(int argc, char *argv[]) {
150 fprintf(stderr, "Usage: %s adapter_nr freq [tsid]\n", argv[0]);
153 adapter_nr = strtol(argv[1], NULL, 0);
154 channel_freq = strtol(argv[2], NULL, 10);
155 channel_id = rectime = 0;
157 channel_id = strtol(argv[3], NULL, 10);
160 rectime = atoi(argv[4]);
162 fd = search(adapter_nr, channel_freq, channel_id);
166 ret = track(adapter_nr);
167 record(adapter_nr, argv[5], rectime);