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