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