OSDN Git Service

tunerec: include <unistd.h> for usleep
[rec10/rec10-git.git] / tunerec / tunerec.c
index 1aea926..bce1a50 100755 (executable)
@@ -1,3 +1,5 @@
+#define _FILE_OFFSET_BITS 64
+
 #include <linux/dvb/frontend.h>
 #include <linux/dvb/dmx.h>
 #include <linux/dvb/audio.h>
@@ -13,6 +15,8 @@
 #include <pthread.h>
 #include <stdbool.h>
 #include <string.h>
+#include <ctype.h>
+#include <unistd.h>
 
 #define ARRAY_SIZE(arr) (sizeof(arr) / sizeof((arr)[0]))
 #ifndef DTV_STREAM_ID
@@ -93,7 +97,7 @@ bool mrb_get(mrb* rb, char *target, size_t size) {
                // RingBuffer underflow.
                return false;
        }
-       if ( rb->rb_rt + size <= rb->rb_wt ) {
+       if ( rb->rb_rt + size <= RING_BUFFER_SIZE ) {
                // read bytes + newly taken bytes <= buffer max size
                memcpy(target, rb->rb_ptr + rb->rb_rt, size);
                rb->rb_rt = rb->rb_rt + size;
@@ -233,7 +237,7 @@ void record(int adapter_nr, char* output, int rectime) {
 void * record_read(void * priv) {
        ssize_t rt;
        time_t start_time, current_time;
-       char buf[DVB_READ_BUFFER_SIZE];
+       char* buf = malloc(DVB_READ_BUFFER_SIZE);
        my_thread_arg* p = (my_thread_arg*)priv;
 
        start_time = time(NULL);
@@ -270,7 +274,7 @@ void * record_read(void * priv) {
 
 void * record_write(void * priv) {
        ssize_t rt = 0, wt, shift;
-       char buf[DVB_WRITE_BUFFER_SIZE];
+       char* buf = malloc(DVB_WRITE_BUFFER_SIZE);
        my_thread_arg* p = (my_thread_arg*)priv;
 
        while ( 1 ) {
@@ -300,7 +304,7 @@ void * record_write(void * priv) {
                }
                if ( rt > 0 ) {
                        // [buf] is not correctly written to [fout]
-                       printf("output file write failed\n");
+                       printf("output file write failed. err=%d\n", errno);
                        goto error;
                }
        }
@@ -312,20 +316,37 @@ void * record_write(void * priv) {
 int main(int argc, char *argv[]) {
        int adapter_nr;
        int channel_freq;
+       int channel_phys;
        int channel_id;
        int fd;
        int ret;
        int rectime;
 
        if (argc != 6) {
-               fprintf(stderr, "Usage  : %s adapter_nr freq tsid rectime output\n", argv[0]);
-               fprintf(stderr, "Version: 0.0.1\n");
+               fprintf(stderr, "Usage  : %s adapter_nr channel tsid rectime output\n", argv[0]);
+               fprintf(stderr, "    channel can be freqency or channel(TE1/BS1/CS1)\n");
+               fprintf(stderr, "Version: 0.0.2\n");
                return 1;
        }
        adapter_nr = strtol(argv[1], NULL, 0);
        channel_freq = strtol(argv[2], NULL, 10);
        channel_id = strtol(argv[3], NULL, 10);
        rectime = atoi(argv[4]);
+
+       if ( channel_freq == 0 ) {
+               channel_phys = atoi(argv[2] + 2);
+               if ( toupper(*argv[2]) == 'T' && toupper(*(argv[2] + 1)) == 'E' && channel_phys != 0 ) {
+                       channel_freq = ( 473 + (channel_phys - 13) * 6 ) * 1000000 + 142857;
+               }
+               else if ( toupper(*argv[2]) == 'B' && toupper(*(argv[2] + 1)) == 'S' && channel_phys != 0 ) {
+                       channel_freq = (channel_phys - 1) * 38360 / 2 + 1049480;
+               }
+               else if ( toupper(*argv[2]) == 'C' && toupper(*(argv[2] + 1)) == 'S' && channel_phys != 0 ) {
+                       channel_freq = (channel_phys - 2) * 40000 / 2 + 1613000;
+               }
+               fprintf(stderr, "channel_freq = %d\n", channel_freq);
+       }
+
        fd = search(adapter_nr, channel_freq, channel_id);
        if (fd < 0)
                return 1;