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