]> cvs.zerfleddert.de Git - record-dvb/blob - record-dvb.c
don't call gettimeofday for every packet...
[record-dvb] / record-dvb.c
1 #include <features.h>
2 #include <stdio.h>
3 #include <stdlib.h>
4 #include <sys/time.h>
5 #include <time.h>
6 #include <sys/socket.h>
7 #include <sys/types.h>
8 #include <sys/stat.h>
9 #include <fcntl.h>
10 #include <unistd.h>
11 #include <strings.h>
12
13 #include "http.h"
14 #include "mcast.h"
15
16 #define CHUNKSIZE 1500
17 #define GTOD_INTERVAL 100
18
19 void record(int(*open_fn)(char *), char *url, char *outfile, int duration)
20 {
21 struct timeval start, curr;
22 int bytes, written, count = 0;
23 char buffer[CHUNKSIZE];
24 int i;
25 int in, out;
26
27 if ((out = open(outfile, O_CREAT|O_TRUNC|O_WRONLY|O_LARGEFILE, 00644)) < 0) {
28 perror("open");
29 exit(EXIT_FAILURE);
30 }
31
32 if ((in = (*open_fn)(url)) < 0) {
33 fprintf(stderr,"Can't open url %s!\n",url);
34 exit(EXIT_FAILURE);
35 }
36
37 printf("Recording from %s for %d seconds...\n", url, duration);
38
39 gettimeofday(&start, NULL);
40 curr = start;
41
42 do {
43 if ((bytes = recv(in, buffer, CHUNKSIZE, 0)) < 1) {
44 /* TODO: Insert better connection-loss recovery here */
45 if ((in = (*open_fn)(url)) < 0) {
46 sleep(1);
47 continue;
48 }
49 }
50 written = 0;
51 do {
52 if ((i = write(out, buffer, bytes-written)) < 0) {
53 perror("write");
54 exit(EXIT_FAILURE);
55 }
56 written += i;
57 } while(written < bytes);
58
59 count++;
60
61 if (!(count % GTOD_INTERVAL))
62 gettimeofday(&curr, NULL);
63 } while (curr.tv_sec < start.tv_sec+duration);
64
65 close(out);
66 close(in);
67 shutdown(in, SHUT_RDWR);
68 }
69
70 int main(int argc, char **argv)
71 {
72 int duration;
73 char *url;
74 char *outfile;
75 int(*open_fn)(char *);
76
77 if (argc == 4) {
78 url = argv[1];
79 duration = atoi(argv[2])*60;
80 outfile = argv[3];
81 } else {
82 fprintf(stderr,"Syntax: %s URL duration_in_minutes outfile\n", argv[0]);
83 exit(EXIT_FAILURE);
84 }
85
86 if (is_http(url)) {
87 open_fn = &open_http;
88 } else if (is_mcast(url)) {
89 open_fn = &open_mcast;
90 } else {
91 printf("URL %s not supported!\n", url);
92 exit(EXIT_FAILURE);
93 }
94
95 record(open_fn, url, outfile, duration);
96
97 return EXIT_SUCCESS;
98 }
Impressum, Datenschutz