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