]>
Commit | Line | Data |
---|---|---|
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 3000 | |
17 | #define GTOD_INTERVAL 100 | |
18 | #define MAX_ERROR_SLEEP 60 | |
19 | ||
20 | void record(int(*open_fn)(char *), char *url, char *outfile, int duration) | |
21 | { | |
22 | struct timeval start, curr; | |
23 | int bytes, recvd, written, count = 0; | |
24 | int error_sleep = 0; | |
25 | char buffer[CHUNKSIZE]; | |
26 | int in, out; | |
27 | ||
28 | if ((in = (*open_fn)(url)) < 0) { | |
29 | fprintf(stderr,"Can't open url %s!\n",url); | |
30 | exit(EXIT_FAILURE); | |
31 | } | |
32 | ||
33 | if ((out = open(outfile, O_CREAT|O_TRUNC|O_WRONLY|O_LARGEFILE, 00644)) < 0) { | |
34 | perror("open"); | |
35 | exit(EXIT_FAILURE); | |
36 | } | |
37 | ||
38 | printf("Recording from %s for %d seconds...\n", url, duration); | |
39 | ||
40 | gettimeofday(&start, NULL); | |
41 | curr = start; | |
42 | ||
43 | do { | |
44 | if (error_sleep) { | |
45 | sleep(error_sleep); | |
46 | printf("Reconnecting... "); | |
47 | if ((in = (*open_fn)(url)) < 0) { | |
48 | if (error_sleep < MAX_ERROR_SLEEP) | |
49 | error_sleep *= 2; | |
50 | ||
51 | if (error_sleep > MAX_ERROR_SLEEP) | |
52 | error_sleep = MAX_ERROR_SLEEP; | |
53 | ||
54 | printf("failed\n"); | |
55 | continue; | |
56 | } else { | |
57 | printf("succeeded\n"); | |
58 | error_sleep = 0; | |
59 | } | |
60 | } | |
61 | ||
62 | if ((recvd = recv(in, buffer, CHUNKSIZE, 0)) < 1) { | |
63 | error_sleep = 1; | |
64 | continue; | |
65 | } | |
66 | written = 0; | |
67 | do { | |
68 | if ((bytes = write(out, buffer, recvd-written)) < 0) { | |
69 | perror("write"); | |
70 | exit(EXIT_FAILURE); | |
71 | } | |
72 | written += bytes; | |
73 | } while(written < recvd); | |
74 | ||
75 | if (!(++count % GTOD_INTERVAL)) | |
76 | gettimeofday(&curr, NULL); | |
77 | } while (curr.tv_sec < start.tv_sec+duration); | |
78 | ||
79 | close(out); | |
80 | close(in); | |
81 | shutdown(in, SHUT_RDWR); | |
82 | } | |
83 | ||
84 | int main(int argc, char **argv) | |
85 | { | |
86 | int duration; | |
87 | char *url; | |
88 | char *outfile; | |
89 | int(*open_fn)(char *); | |
90 | ||
91 | if (argc == 4) { | |
92 | url = argv[1]; | |
93 | duration = atoi(argv[2])*60; | |
94 | outfile = argv[3]; | |
95 | } else { | |
96 | fprintf(stderr,"Syntax: %s URL duration_in_minutes outfile\n", argv[0]); | |
97 | exit(EXIT_FAILURE); | |
98 | } | |
99 | ||
100 | if (is_http(url)) { | |
101 | open_fn = &open_http; | |
102 | } else if (is_mcast(url)) { | |
103 | open_fn = &open_mcast; | |
104 | } else { | |
105 | printf("URL %s not supported!\n", url); | |
106 | exit(EXIT_FAILURE); | |
107 | } | |
108 | ||
109 | record(open_fn, url, outfile, duration); | |
110 | ||
111 | return EXIT_SUCCESS; | |
112 | } |