1 #include <linux/dvb/frontend.h>
2 #include <linux/dvb/dmx.h>
3 #include <linux/dvb/audio.h>
4 #include <linux/dvb/version.h>
17 #define ARRAY_SIZE(arr) (sizeof(arr) / sizeof((arr)[0]))
19 #define DTV_STREAM_ID DTV_ISDBS_TS_ID
21 #define DVB_READ_BUFFER_SIZE 188 * 128
22 #define DVB_WRITE_BUFFER_SIZE 1024 * 1024
23 #define DMX_BUFFER_SIZE 1024 * 1024
24 #define RING_BUFFER_SIZE 1024 * 1024 * 16
31 pthread_mutex_t rb_mtx;
42 void * record_read(void * priv);
43 void * record_write(void * priv);
45 void mrb_init(mrb* rb, size_t size) {
46 rb->rb_ptr = malloc(size);
47 rb->rb_wt = rb->rb_rt = rb->rb_ab = 0;
48 pthread_mutex_init(&rb->rb_mtx, NULL);
51 void mrb_destroy(mrb* rb) {
53 pthread_mutex_destroy(&rb->rb_mtx);
56 size_t mrb_data_len(mrb* rb) {
60 bool mrb_put(mrb* rb, char *source, size_t size) {
63 if ( rb->rb_ab + size > RING_BUFFER_SIZE ) {
64 // RingBuffer overflow.
67 if ( rb->rb_wt + size <= RING_BUFFER_SIZE ) {
68 // written bytes + newly added bytes <= buffer max size
69 memcpy(rb->rb_ptr + rb->rb_wt, source, size);
70 rb->rb_wt = rb->rb_wt + size;
73 // written bytes + newly added bytes > buffer max size
75 partial = RING_BUFFER_SIZE - rb->rb_wt;
76 memcpy(rb->rb_ptr + rb->rb_wt, source, partial);
77 memcpy(rb->rb_ptr, source + partial, size - partial);
78 rb->rb_wt = rb->rb_wt + size - RING_BUFFER_SIZE;
80 pthread_mutex_lock(&rb->rb_mtx);
82 pthread_mutex_unlock(&rb->rb_mtx);
87 bool mrb_get(mrb* rb, char *target, size_t size) {
90 if ( rb->rb_ab < size ) {
91 // RingBuffer underflow.
94 if ( rb->rb_rt + size <= rb->rb_wt ) {
95 // read bytes + newly taken bytes <= written max size
96 memcpy(target, rb->rb_ptr + rb->rb_rt, size);
97 rb->rb_rt = rb->rb_rt + size;
100 // Ring Buffer wrapped.
101 partial = RING_BUFFER_SIZE - rb->rb_rt;
102 memcpy(target, rb->rb_ptr + rb->rb_rt, partial);
103 memcpy(target + partial, rb->rb_ptr, size - partial);
104 rb->rb_rt = rb->rb_rt + size - RING_BUFFER_SIZE;
106 pthread_mutex_lock(&rb->rb_mtx);
108 pthread_mutex_unlock(&rb->rb_mtx);
113 static int search(int adapter_nr, unsigned int frequency, unsigned int ts_id)
117 struct dvb_frontend_info info;
118 struct dtv_property prop[3];
119 struct dtv_properties props;
123 sprintf(file, "/dev/dvb/adapter%d/frontend0", adapter_nr);
124 if ((fd = open(file, (O_RDWR | O_CREAT | O_TRUNC), 0666)) < 0) {
129 if (ioctl(fd, FE_GET_INFO, &info) < 0) {
130 perror("ioctl FE_GET_INFO");
134 if (info.type == FE_QPSK || info.type == FE_OFDM) {
136 fprintf(stderr, "Unknown type of adapter\n");
140 prop[0].cmd = DTV_FREQUENCY;
141 prop[0].u.data = frequency;
142 prop[1].cmd = DTV_STREAM_ID;
143 prop[1].u.data = ts_id;
144 prop[2].cmd = DTV_TUNE;
149 if ((ioctl(fd, FE_SET_PROPERTY, &props)) < 0) {
150 perror("ioctl FE_SET_PROPERTY");
154 for (i = 0; i < 4; i++) {
155 if (ioctl(fd, FE_READ_STATUS, &status) < 0) {
156 perror("ioctl FE_READ_STATUS");
159 if (status & FE_HAS_LOCK) {
160 fprintf(stderr, "Successfully tuned to %d(%d).\n",
167 fprintf(stderr, "Failed to tune (status %02x).\n", status);
174 static int track(int adapter_nr)
178 struct dmx_pes_filter_params filter;
181 filter.input = DMX_IN_FRONTEND;
182 filter.output = DMX_OUT_TS_TAP;
183 filter.pes_type = DMX_PES_VIDEO;
184 filter.flags = DMX_IMMEDIATE_START;
186 sprintf(file, "/dev/dvb/adapter%d/demux0", adapter_nr);
187 if ((fd = open(file, O_RDWR)) < 0) {
192 if (ioctl(fd, DMX_SET_PES_FILTER, &filter) < 0) {
193 perror("ioctl DMX_SET_PES_FILTER");
200 void record(int adapter_nr, char* output, int rectime) {
203 pthread_t read_thread, write_thread;
206 fout = open(output, (O_WRONLY | O_CREAT | O_TRUNC), 0666);
208 printf("output file open failed\n");
211 sprintf(input, "/dev/dvb/adapter%d/dvr0", adapter_nr);
212 fin = open(input, (O_RDONLY));
213 ioctl(fin, DMX_SET_BUFFER_SIZE, DMX_BUFFER_SIZE);
215 mrb_init(&arg.rb, RING_BUFFER_SIZE);
217 arg.rectime = rectime;
220 pthread_create(&read_thread , NULL, record_read , &arg);
221 pthread_create(&write_thread, NULL, record_write, &arg);
222 pthread_join(write_thread, NULL);
223 pthread_join(read_thread, NULL);
225 mrb_destroy(&arg.rb);
230 void * record_read(void * priv) {
232 time_t start_time, current_time;
233 char buf[DVB_READ_BUFFER_SIZE];
234 my_thread_arg* p = (my_thread_arg*)priv;
236 start_time = time(NULL);
238 rt = read(p->fin, buf, DVB_READ_BUFFER_SIZE);
239 //printf("read %d %d\n", p->fin, rt);
241 printf("dvr0 file read failed\n");
247 printf("read overflow, continue\n");
250 printf("read finished. err=%d\n", errno);
255 mrb_put(&p->rb, buf, rt);
257 // TODO: time() at each DVB_READ_BUFFER_SIZE bytes read is not efficient, reduce frequency
258 current_time = time(NULL);
259 if ( current_time - start_time > p->rectime || p->done ) {
268 void * record_write(void * priv) {
269 ssize_t rt = 0, wt, shift;
270 char buf[DVB_WRITE_BUFFER_SIZE];
271 my_thread_arg* p = (my_thread_arg*)priv;
274 if ( mrb_get(&p->rb, buf, DVB_WRITE_BUFFER_SIZE) ) {
276 rt = DVB_WRITE_BUFFER_SIZE;
278 else if ( p->done && mrb_data_len(&p->rb) > 0) {
279 // insufficient buffer, but no more buffer fill
280 rt = mrb_data_len(&p->rb);
281 mrb_get(&p->rb, buf, mrb_data_len(&p->rb));
283 else if ( p->done ) {
292 while ( ( wt = write(p->fout, buf + shift, rt) ) > 0) {
293 //printf("write %d %d\n", p->fout, rt);
295 if ( rt == 0 ) break;
299 // [buf] is not correctly written to [fout]
300 printf("output file write failed\n");
309 int main(int argc, char *argv[]) {
318 fprintf(stderr, "Usage: %s adapter_nr freq tsid rectime output\n", argv[0]);
321 adapter_nr = strtol(argv[1], NULL, 0);
322 channel_freq = strtol(argv[2], NULL, 10);
323 channel_id = strtol(argv[3], NULL, 10);
324 rectime = atoi(argv[4]);
325 fd = search(adapter_nr, channel_freq, channel_id);
329 ret = track(adapter_nr);
330 record(adapter_nr, argv[5], rectime);