better chunksize
[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
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) {
42 /* TODO: Insert better connection-loss recovery here */
43 in = (*open_fn)(url);
44 }
45 written = 0;
46 do {
47 if ((i = write(out, buffer, bytes-written)) < 0) {
48 perror("write");
49 exit(EXIT_FAILURE);
50 }
51 written += i;
52 } while(written < bytes);
53
54 gettimeofday(&curr, NULL);
55 } while (curr.tv_sec < start.tv_sec+duration);
56
57 close(out);
58 close(in);
59 shutdown(in, SHUT_RDWR);
60 }
61
62 int main(int argc, char **argv)
63 {
64 int duration;
65 char *url;
66 char *outfile;
67 int(*open_fn)(char *);
68
69 if (argc == 4) {
70 url = argv[1];
71 duration = atol(argv[2])*60;
72 outfile = argv[3];
73 } else {
74 fprintf(stderr,"Syntax: %s URL duration_in_minutes outfile\n", argv[0]);
75 exit(EXIT_FAILURE);
76 }
77
78 if (is_http(url)) {
79 open_fn = &open_http;
80 } else if (is_mcast(url)) {
81 open_fn = &open_mcast;
82 } else {
83 printf("URL %s not supported!\n", url);
84 exit(EXIT_FAILURE);
85 }
86
87 record(open_fn, url, outfile, duration);
88
89 return EXIT_SUCCESS;
90 }
Impressum, Datenschutz