OSDN Git Service

fix tunerec
[rec10/rec10-git.git] / tunerec / tunerec.c
index f3f3328..9bcf0ea 100755 (executable)
@@ -7,6 +7,7 @@
 #include <unistd.h>
 #include <stdio.h>
 #include <stdlib.h>
+#include <time.h>
 
 #define ARRAY_SIZE(arr) (sizeof(arr) / sizeof((arr)[0]))
 
@@ -114,11 +115,42 @@ static int track(int adapter_nr)
                close(fd);
                return -1;
        }
+}
 
-       while (1)
-               sleep(3);
+void record(int adapter_nr, char* output, int rectime) {
+       int fin, fout;
+       char input[256];
+       time_t start_time, current_time;
+       char buf[1316];
+       ssize_t rt, wt;
+       int size_remain;
+
+       fout = open(output, (O_WRONLY | O_CREAT | O_TRUNC));
+       if ( fout < 0 ) {
+               printf("output file open failed\n");
+               return;
+       }
+       sprintf(input, "/dev/dvb/adapter%d/dvr0", adapter_nr);
+       start_time = time(NULL);
+       fin = open(input, (O_RDONLY));
+       while ( rt = read(fin, buf, 1316) ) {
+               while ( wt = write(fout, buf, rt) ) {
+                       rt -= wt;
+                       if ( rt == 0 ) break;
+                       if ( wt == 0 ) {
+                               printf("output file write failed\n");
+                               goto error;
+                       }
+               }
+               current_time = time(NULL);
+               if ( current_time - start_time > rectime ) {
+                       break;
+               }
+       }
 
-       /* never returns */
+       error:
+       close(fin);
+       close(fout);
 }
 
 int main(int argc, char *argv[]) {
@@ -127,6 +159,7 @@ int main(int argc, char *argv[]) {
        int channel_freq;
        int fd;
        int ret;
+       int rectime;
 
        if (argc <= 2) {
                fprintf(stderr, "Usage: %s adapter_nr freq [tsid]\n", argv[0]);
@@ -136,9 +169,12 @@ int main(int argc, char *argv[]) {
        adapter_nr = strtol(argv[1], NULL, 0);
        channel_freq = strtol(argv[2], NULL, 10);
        channel_id=0;
-       if (argc == 4){
+       if (argc >= 4){
                channel_id = strtol(argv[3], NULL, 10);
        }
+       if ( argc == 5 ) {
+               rectime = atoi(argv[4]);
+       }
        struct channel ch1 ={0,"",channel_freq,channel_id};
        ch = &ch1;
        //struct channel ch;//{   1, "NHK BS-1",          1318000, 0x40f1 }
@@ -148,6 +184,7 @@ int main(int argc, char *argv[]) {
                return 1;
 
        ret = track(adapter_nr);
+       record(adapter_nr, argv[3], rectime);
        close(fd);
 
        return ret < 0;