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 #define DTV_STREAM_ID DTV_ISDBS_TS_ID
17 static int search(int adapter_nr, unsigned int frequency, unsigned int ts_id)
21 struct dvb_frontend_info info;
22 struct dtv_property prop[3];
23 struct dtv_properties props;
27 sprintf(file, "/dev/dvb/adapter%d/frontend0", adapter_nr);
28 if ((fd = open(file, (O_RDWR | O_CREAT | O_TRUNC), 0666)) < 0) {
33 if (ioctl(fd, FE_GET_INFO, &info) < 0) {
34 perror("ioctl FE_GET_INFO");
38 if (info.type == FE_QPSK || info.type == FE_OFDM) {
40 fprintf(stderr, "Unknown type of adapter\n");
44 prop[0].cmd = DTV_FREQUENCY;
45 prop[0].u.data = frequency;
46 prop[1].cmd = DTV_STREAM_ID;
47 prop[1].u.data = ts_id;
48 prop[2].cmd = DTV_TUNE;
53 if ((ioctl(fd, FE_SET_PROPERTY, &props)) < 0) {
54 perror("ioctl FE_SET_PROPERTY");
58 for (i = 0; i < 4; i++) {
59 if (ioctl(fd, FE_READ_STATUS, &status) < 0) {
60 perror("ioctl FE_READ_STATUS");
63 if (status & FE_HAS_LOCK) {
64 fprintf(stderr, "Successfully tuned to %d(%d).\n",
71 fprintf(stderr, "Failed to tune (status %02x).\n", status);
78 static int track(int adapter_nr)
82 struct dmx_pes_filter_params filter;
85 filter.input = DMX_IN_FRONTEND;
86 filter.output = DMX_OUT_TS_TAP;
87 filter.pes_type = DMX_PES_VIDEO;
88 filter.flags = DMX_IMMEDIATE_START;
90 sprintf(file, "/dev/dvb/adapter%d/demux0", adapter_nr);
91 if ((fd = open(file, O_RDWR)) < 0) {
96 if (ioctl(fd, DMX_SET_PES_FILTER, &filter) < 0) {
97 perror("ioctl DMX_SET_PES_FILTER");
104 void record(int adapter_nr, char* output, int rectime) {
107 time_t start_time, current_time;
109 ssize_t rt, wt, shift;
111 fout = open(output, (O_WRONLY | O_CREAT | O_TRUNC), 0666);
113 printf("output file open failed\n");
116 sprintf(input, "/dev/dvb/adapter%d/dvr0", adapter_nr);
117 start_time = time(NULL);
118 fin = open(input, (O_RDONLY));
119 while ( ( rt = read(fin, buf, 1316) ) > 0 ) {
121 while ( ( wt = write(fout, buf + shift, rt) ) > 0) {
123 if ( rt == 0 ) break;
127 // [buf] is not correctly written to [fout]
128 printf("output file write failed\n");
132 // TODO: time() at each 1316 bytes read is not efficient, reduce frequency
133 current_time = time(NULL);
134 if ( current_time - start_time > rectime ) {
144 int main(int argc, char *argv[]) {
153 fprintf(stderr, "Usage: %s adapter_nr freq tsid rectime output\n", argv[0]);
156 adapter_nr = strtol(argv[1], NULL, 0);
157 channel_freq = strtol(argv[2], NULL, 10);
158 channel_id = strtol(argv[3], NULL, 10);
159 rectime = atoi(argv[4]);
160 fd = search(adapter_nr, channel_freq, channel_id);
164 ret = track(adapter_nr);
165 record(adapter_nr, argv[5], rectime);