]> git.zerfleddert.de Git - record-dvb/blame - record-dvb.c
more http-checks, some fixes
[record-dvb] / record-dvb.c
CommitLineData
5c2e3e44 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
4710a9f8 16#define CHUNKSIZE 3000
6ac47f90 17#define GTOD_INTERVAL 100
5c2e3e44 18
19void record(int(*open_fn)(char *), char *url, char *outfile, int duration)
20{
21 struct timeval start, curr;
4710a9f8 22 int bytes, recvd, written, count = 0;
5c2e3e44 23 char buffer[CHUNKSIZE];
5c2e3e44 24 int in, out;
25
4710a9f8 26 if ((in = (*open_fn)(url)) < 0) {
27 fprintf(stderr,"Can't open url %s!\n",url);
5c2e3e44 28 exit(EXIT_FAILURE);
29 }
30
4710a9f8 31 if ((out = open(outfile, O_CREAT|O_TRUNC|O_WRONLY|O_LARGEFILE, 00644)) < 0) {
32 perror("open");
5c2e3e44 33 exit(EXIT_FAILURE);
34 }
35
36 printf("Recording from %s for %d seconds...\n", url, duration);
37
38 gettimeofday(&start, NULL);
6ac47f90 39 curr = start;
5c2e3e44 40
41 do {
4710a9f8 42 if ((recvd = recv(in, buffer, CHUNKSIZE, 0)) < 1) {
a8a2884e 43 /* TODO: Insert better connection-loss recovery here */
b973e875 44 if ((in = (*open_fn)(url)) < 0) {
fbc24ab0 45 sleep(1);
b973e875 46 continue;
47 }
5c2e3e44 48 }
5c2e3e44 49 written = 0;
50 do {
4710a9f8 51 if ((bytes = write(out, buffer, recvd-written)) < 0) {
5c2e3e44 52 perror("write");
53 exit(EXIT_FAILURE);
54 }
4710a9f8 55 written += bytes;
56 } while(written < recvd);
6ac47f90 57
4710a9f8 58 if (!(++count % GTOD_INTERVAL))
6ac47f90 59 gettimeofday(&curr, NULL);
5c2e3e44 60 } while (curr.tv_sec < start.tv_sec+duration);
61
62 close(out);
63 close(in);
64 shutdown(in, SHUT_RDWR);
65}
66
67int 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];
6a3dc096 76 duration = atoi(argv[2])*60;
5c2e3e44 77 outfile = argv[3];
78 } else {
460e6d22 79 fprintf(stderr,"Syntax: %s URL duration_in_minutes outfile\n", argv[0]);
5c2e3e44 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