5c2e3e44 |
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 | |
657d048d |
16 | #define CHUNKSIZE 3000 |
17 | #define GTOD_INTERVAL 100 |
18 | #define MAX_ERROR_SLEEP 60 |
5c2e3e44 |
19 | |
20 | void record(int(*open_fn)(char *), char *url, char *outfile, int duration) |
21 | { |
22 | struct timeval start, curr; |
4710a9f8 |
23 | int bytes, recvd, written, count = 0; |
34d3d284 |
24 | int error_sleep = 0; |
5c2e3e44 |
25 | char buffer[CHUNKSIZE]; |
5c2e3e44 |
26 | int in, out; |
27 | |
4710a9f8 |
28 | if ((in = (*open_fn)(url)) < 0) { |
29 | fprintf(stderr,"Can't open url %s!\n",url); |
5c2e3e44 |
30 | exit(EXIT_FAILURE); |
31 | } |
32 | |
4710a9f8 |
33 | if ((out = open(outfile, O_CREAT|O_TRUNC|O_WRONLY|O_LARGEFILE, 00644)) < 0) { |
34 | perror("open"); |
5c2e3e44 |
35 | exit(EXIT_FAILURE); |
36 | } |
37 | |
38 | printf("Recording from %s for %d seconds...\n", url, duration); |
39 | |
40 | gettimeofday(&start, NULL); |
6ac47f90 |
41 | curr = start; |
5c2e3e44 |
42 | |
43 | do { |
34d3d284 |
44 | if (error_sleep) { |
45 | sleep(error_sleep); |
46 | printf("Reconnecting... "); |
b973e875 |
47 | if ((in = (*open_fn)(url)) < 0) { |
657d048d |
48 | if (error_sleep < MAX_ERROR_SLEEP) |
34d3d284 |
49 | error_sleep *= 2; |
50 | |
657d048d |
51 | if (error_sleep > MAX_ERROR_SLEEP) |
52 | error_sleep = MAX_ERROR_SLEEP; |
53 | |
34d3d284 |
54 | printf("failed\n"); |
b973e875 |
55 | continue; |
34d3d284 |
56 | } else { |
57 | printf("succeeded\n"); |
58 | error_sleep = 0; |
b973e875 |
59 | } |
5c2e3e44 |
60 | } |
34d3d284 |
61 | |
62 | if ((recvd = recv(in, buffer, CHUNKSIZE, 0)) < 1) { |
63 | error_sleep = 1; |
64 | continue; |
65 | } |
5c2e3e44 |
66 | written = 0; |
67 | do { |
4710a9f8 |
68 | if ((bytes = write(out, buffer, recvd-written)) < 0) { |
5c2e3e44 |
69 | perror("write"); |
70 | exit(EXIT_FAILURE); |
71 | } |
4710a9f8 |
72 | written += bytes; |
73 | } while(written < recvd); |
6ac47f90 |
74 | |
4710a9f8 |
75 | if (!(++count % GTOD_INTERVAL)) |
6ac47f90 |
76 | gettimeofday(&curr, NULL); |
5c2e3e44 |
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]; |
6a3dc096 |
93 | duration = atoi(argv[2])*60; |
5c2e3e44 |
94 | outfile = argv[3]; |
95 | } else { |
460e6d22 |
96 | fprintf(stderr,"Syntax: %s URL duration_in_minutes outfile\n", argv[0]); |
5c2e3e44 |
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 | } |