OSDN Git Service

tunerec: brush up
authorLong.inus <longinus@example.com>
Wed, 1 May 2013 20:20:24 +0000 (05:20 +0900)
committerLong.inus <longinus@example.com>
Wed, 1 May 2013 20:20:24 +0000 (05:20 +0900)
tunerec/tunerec.c

index a42c146..0a1d1fe 100755 (executable)
 
 #define ARRAY_SIZE(arr) (sizeof(arr) / sizeof((arr)[0]))
 
 
 #define ARRAY_SIZE(arr) (sizeof(arr) / sizeof((arr)[0]))
 
-struct channel {
-       int id;
-       const char *name;
-       unsigned int frequency;
-       unsigned int ts_id;
-};
-static int search(int adapter_nr,struct channel *ch)
+static int search(int adapter_nr, unsigned int frequency, unsigned int ts_id)
 {
        char file[256];
        int fd;
        struct dvb_frontend_info info;
 {
        char file[256];
        int fd;
        struct dvb_frontend_info info;
-       struct channel *channel;
        struct dtv_property prop[3];
        struct dtv_properties props;
        int i;
        struct dtv_property prop[3];
        struct dtv_properties props;
        int i;
@@ -39,27 +32,16 @@ static int search(int adapter_nr,struct channel *ch)
                goto out;
        }
 
                goto out;
        }
 
-       if (info.type == FE_QPSK) {
-               channel = ch;
-               //channel = lookup_channel(channel_id, isdbs_channels,
-               //                       ARRAY_SIZE(isdbs_channels));
-       } else if (info.type == FE_OFDM) {
-               channel = ch;
-               //channel = lookup_channel(channel_id, isdbt_channels,
-               //                       ARRAY_SIZE(isdbt_channels));
+       if (info.type == FE_QPSK || info.type == FE_OFDM) {
        } else {
                fprintf(stderr, "Unknown type of adapter\n");
                goto out;
        }
        } else {
                fprintf(stderr, "Unknown type of adapter\n");
                goto out;
        }
-       if (channel == NULL) {
-               fprintf(stderr, "Unknown id of channel\n");
-               goto out;
-       }
 
        prop[0].cmd = DTV_FREQUENCY;
 
        prop[0].cmd = DTV_FREQUENCY;
-       prop[0].u.data = channel->frequency;
-       prop[1].cmd = DTV_ISDBS_TS_ID;
-       prop[1].u.data = channel->ts_id;
+       prop[0].u.data = frequency;
+       prop[1].cmd = DTV_STREAM_ID;
+       prop[1].u.data = ts_id;
        prop[2].cmd = DTV_TUNE;
 
        props.props = prop;
        prop[2].cmd = DTV_TUNE;
 
        props.props = prop;
@@ -76,15 +58,14 @@ static int search(int adapter_nr,struct channel *ch)
                        goto out;
                }
                if (status & FE_HAS_LOCK) {
                        goto out;
                }
                if (status & FE_HAS_LOCK) {
-                       fprintf(stderr, "Successfully tuned to %d(%d) .\n",
-                               channel->frequency,channel->ts_id);
+                       fprintf(stderr, "Successfully tuned to %d(%d).\n",
+                               frequency, ts_id);
                        return 0;
                }
                usleep(250 * 1000);
        }
 
                        return 0;
                }
                usleep(250 * 1000);
        }
 
-       fprintf(stderr, "Failed to tune to %s (status %02x).\n",
-               channel->name, status);
+       fprintf(stderr, "Failed to tune (status %02x).\n", status);
 
 out:
        close(fd);
 
 out:
        close(fd);
@@ -109,12 +90,12 @@ static int track(int adapter_nr)
                return -1;
        }
 
                return -1;
        }
 
-
        if (ioctl(fd, DMX_SET_PES_FILTER, &filter) < 0) {
                perror("ioctl DMX_SET_PES_FILTER");
                close(fd);
                return -1;
        }
        if (ioctl(fd, DMX_SET_PES_FILTER, &filter) < 0) {
                perror("ioctl DMX_SET_PES_FILTER");
                close(fd);
                return -1;
        }
+       return 0;
 }
 
 void record(int adapter_nr, char* output, int rectime) {
 }
 
 void record(int adapter_nr, char* output, int rectime) {
@@ -122,10 +103,9 @@ void record(int adapter_nr, char* output, int rectime) {
        char input[256];
        time_t start_time, current_time;
        char buf[1316];
        char input[256];
        time_t start_time, current_time;
        char buf[1316];
-       ssize_t rt, wt;
-       int size_remain;
+       ssize_t rt, wt, shift;
 
 
-       fout = open(output, (O_WRONLY | O_CREAT | O_TRUNC));
+       fout = open(output, (O_WRONLY | O_CREAT | O_TRUNC), 0666);
        if ( fout < 0 ) {
                printf("output file open failed\n");
                return;
        if ( fout < 0 ) {
                printf("output file open failed\n");
                return;
@@ -133,15 +113,20 @@ void record(int adapter_nr, char* output, int rectime) {
        sprintf(input, "/dev/dvb/adapter%d/dvr0", adapter_nr);
        start_time = time(NULL);
        fin = open(input, (O_RDONLY));
        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) ) {
+       while ( rt = read(fin, buf, 1316) > 0 ) {
+               shift = 0;
+               while ( wt = write(fout, buf + shift, rt) > 0) {
                        rt -= wt;
                        if ( rt == 0 ) break;
                        rt -= wt;
                        if ( rt == 0 ) break;
-                       if ( wt == 0 ) {
-                               printf("output file write failed\n");
-                               goto error;
-                       }
+                       shift += wt;
+               }
+               if ( rt > 0 ) {
+                       // [buf] is not correctly written to [fout]
+                       printf("output file write failed\n");
+                       goto error;
                }
                }
+
+               // TODO: time() at each 1316 bytes read is not efficient, reduce frequency
                current_time = time(NULL);
                if ( current_time - start_time > rectime ) {
                        break;
                current_time = time(NULL);
                if ( current_time - start_time > rectime ) {
                        break;
@@ -155,8 +140,8 @@ void record(int adapter_nr, char* output, int rectime) {
 
 int main(int argc, char *argv[]) {
        int adapter_nr;
 
 int main(int argc, char *argv[]) {
        int adapter_nr;
-       int channel_id;
        int channel_freq;
        int channel_freq;
+       int channel_id;
        int fd;
        int ret;
        int rectime;
        int fd;
        int ret;
        int rectime;
@@ -165,21 +150,16 @@ int main(int argc, char *argv[]) {
                fprintf(stderr, "Usage: %s adapter_nr freq [tsid]\n", argv[0]);
                return 1;
        }
                fprintf(stderr, "Usage: %s adapter_nr freq [tsid]\n", argv[0]);
                return 1;
        }
-       struct channel *ch;
        adapter_nr = strtol(argv[1], NULL, 0);
        channel_freq = strtol(argv[2], NULL, 10);
        adapter_nr = strtol(argv[1], NULL, 0);
        channel_freq = strtol(argv[2], NULL, 10);
-       channel_id=0;
-       if (argc >= 4){
+       channel_id = rectime = 0;
+       if ( argc >= 4 ){
                channel_id = strtol(argv[3], NULL, 10);
        }
        if ( argc >= 5 ) {
                rectime = atoi(argv[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 }
-       //ch={0,"",argv}
-       fd = search(adapter_nr, ch);
+       fd = search(adapter_nr, channel_freq, channel_id);
        if (fd < 0)
                return 1;
 
        if (fd < 0)
                return 1;
 
@@ -189,3 +169,4 @@ int main(int argc, char *argv[]) {
 
        return ret < 0;
 }
 
        return ret < 0;
 }
+