]> git.zerfleddert.de Git - record-dvb/blob - record-dvb.c
more http-checks, some fixes
[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 3000
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, recvd, written, count = 0;
23 char buffer[CHUNKSIZE];
24 int in, out;
25
26 if ((in = (*open_fn)(url)) < 0) {
27 fprintf(stderr,"Can't open url %s!\n",url);
28 exit(EXIT_FAILURE);
29 }
30
31 if ((out = open(outfile, O_CREAT|O_TRUNC|O_WRONLY|O_LARGEFILE, 00644)) < 0) {
32 perror("open");
33 exit(EXIT_FAILURE);
34 }
35
36 printf("Recording from %s for %d seconds...\n", url, duration);
37
38 gettimeofday(&start, NULL);
39 curr = start;
40
41 do {
42 if ((recvd = recv(in, buffer, CHUNKSIZE, 0)) < 1) {
43 /* TODO: Insert better connection-loss recovery here */
44 if ((in = (*open_fn)(url)) < 0) {
45 sleep(1);
46 continue;
47 }
48 }
49 written = 0;
50 do {
51 if ((bytes = write(out, buffer, recvd-written)) < 0) {
52 perror("write");
53 exit(EXIT_FAILURE);
54 }
55 written += bytes;
56 } while(written < recvd);
57
58 if (!(++count % GTOD_INTERVAL))
59 gettimeofday(&curr, NULL);
60 } while (curr.tv_sec < start.tv_sec+duration);
61
62 close(out);
63 close(in);
64 shutdown(in, SHUT_RDWR);
65 }
66
67 int main(int argc, char **argv)
68 {
69 int duration;
70 char *url;
71 char *outfile;
72 int(*open_fn)(char *);
73
74 if (argc == 4) {
75 url = argv[1];
76 duration = atoi(argv[2])*60;
77 outfile = argv[3];
78 } else {
79 fprintf(stderr,"Syntax: %s URL duration_in_minutes outfile\n", argv[0]);
80 exit(EXIT_FAILURE);
81 }
82
83 if (is_http(url)) {
84 open_fn = &open_http;
85 } else if (is_mcast(url)) {
86 open_fn = &open_mcast;
87 } else {
88 printf("URL %s not supported!\n", url);
89 exit(EXIT_FAILURE);
90 }
91
92 record(open_fn, url, outfile, duration);
93
94 return EXIT_SUCCESS;
95 }
Impressum, Datenschutz