]>
Commit | Line | Data |
---|---|---|
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 | ||
dba12497 | 16 | #define CHUNKSIZE 1500 |
5c2e3e44 | 17 | |
18 | void record(int(*open_fn)(char *), char *url, char *outfile, int duration) | |
19 | { | |
20 | struct timeval start, curr; | |
21 | int bytes, written; | |
22 | char buffer[CHUNKSIZE]; | |
23 | int i; | |
24 | int in, out; | |
25 | ||
26 | if ((out = open(outfile, O_CREAT|O_TRUNC|O_WRONLY|O_LARGEFILE, 00644)) < 0) { | |
27 | perror("open"); | |
28 | exit(EXIT_FAILURE); | |
29 | } | |
30 | ||
31 | if ((in = (*open_fn)(url)) < 0) { | |
32 | fprintf(stderr,"Can't open url %s!\n",url); | |
33 | exit(EXIT_FAILURE); | |
34 | } | |
35 | ||
36 | printf("Recording from %s for %d seconds...\n", url, duration); | |
37 | ||
38 | gettimeofday(&start, NULL); | |
39 | ||
40 | do { | |
41 | if ((bytes = recv(in, buffer, CHUNKSIZE, 0)) < 1) { | |
a8a2884e | 42 | /* TODO: Insert better connection-loss recovery here */ |
fbc24ab0 | 43 | if ((in = (*open_fn)(url)) < 0) |
44 | sleep(1); | |
5c2e3e44 | 45 | } |
5c2e3e44 | 46 | written = 0; |
47 | do { | |
48 | if ((i = write(out, buffer, bytes-written)) < 0) { | |
49 | perror("write"); | |
50 | exit(EXIT_FAILURE); | |
51 | } | |
52 | written += i; | |
53 | } while(written < bytes); | |
54 | ||
55 | gettimeofday(&curr, NULL); | |
56 | } while (curr.tv_sec < start.tv_sec+duration); | |
57 | ||
58 | close(out); | |
59 | close(in); | |
60 | shutdown(in, SHUT_RDWR); | |
61 | } | |
62 | ||
63 | int main(int argc, char **argv) | |
64 | { | |
65 | int duration; | |
66 | char *url; | |
67 | char *outfile; | |
68 | int(*open_fn)(char *); | |
69 | ||
70 | if (argc == 4) { | |
71 | url = argv[1]; | |
72 | duration = atol(argv[2])*60; | |
73 | outfile = argv[3]; | |
74 | } else { | |
460e6d22 | 75 | fprintf(stderr,"Syntax: %s URL duration_in_minutes outfile\n", argv[0]); |
5c2e3e44 | 76 | exit(EXIT_FAILURE); |
77 | } | |
78 | ||
79 | if (is_http(url)) { | |
80 | open_fn = &open_http; | |
81 | } else if (is_mcast(url)) { | |
82 | open_fn = &open_mcast; | |
83 | } else { | |
84 | printf("URL %s not supported!\n", url); | |
85 | exit(EXIT_FAILURE); | |
86 | } | |
87 | ||
88 | record(open_fn, url, outfile, duration); | |
89 | ||
90 | return EXIT_SUCCESS; | |
91 | } |