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