File: getstream.c

package info (click to toggle)
getstream 20070419-1
  • links: PTS
  • area: main
  • in suites: lenny
  • size: 308 kB
  • ctags: 658
  • sloc: ansic: 4,137; makefile: 58
file content (195 lines) | stat: -rw-r--r-- 3,300 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195

#include <sys/poll.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/ioctl.h>
#include <sys/socket.h>
#include <errno.h>
#include <fcntl.h>
#include <time.h>
#include <unistd.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <pthread.h>
#include <signal.h>

#include <stdint.h>
#include <string.h>
#include <stdio.h>
#include <stdlib.h>

#include <event.h>

#include "getstream.h"
#include "sap.h"
#include "stream.h"
#include "config.h"
#include "libhttp.h"

int	loglevel=LOG_ERROR;

#ifdef DEBUG
static int	guardcounter;

void *guard_thread(void *mtp) {
	pthread_t	mt=(pthread_t) mtp;
	struct timeval	tv;

	guardcounter=0;

	while(1) {
		tv.tv_sec=1;
		tv.tv_usec=0;
		select(0, NULL, NULL, NULL, &tv);

		if (guardcounter==0) {
			pthread_kill(mt, SIGABRT);
			pthread_exit(NULL);
		}
		guardcounter=0;
	}
}

static void init_guard_evtimer(void );

static void guard_evtimer(int fd, short event, void *arg) {
	guardcounter++;
	init_guard_evtimer();
}

static void init_guard_evtimer(void ) {
	static struct event	gevent;
	static struct timeval	tv;

	tv.tv_usec=50000;
	tv.tv_sec=00;

	evtimer_set(&gevent, guard_evtimer, &gevent);
	evtimer_add(&gevent, &tv);
}

void init_guard_thread(void ) {
	pthread_t	mt, gt;

	init_guard_evtimer();
	mt=pthread_self();
	pthread_create(&gt, NULL, guard_thread, (void *) mt);
}
#endif



static void usage(void ) {
	fprintf(stderr, "-c <config file> -d \n");
	exit(-1);
}

struct http_server	*hserver;

int main(int argc, char **argv) {
	extern char		*optarg;
	char			ch;
	GList			*al;
	struct config_s		*config=NULL;

	while((ch=getopt(argc, argv, "c:d")) != -1) {
		switch(ch) {
			case 'c':
				config=readconfig(optarg);
				if (!config)
					exit(1);
				break;
			case 'd':
				loglevel++;
				break;
			default:
				usage();
				break;
		}
	}
	if (!config)
		exit(-1);
#if 0
	if (!read_dvbrc()) {
		exit(-1);
	}

	/* Find the adapters transponder from channels */
	for(a=config->adapter;a;a=a->next) {
		struct dvbrc_transponder_s	*t=NULL;
		struct channel_s		*c;

		printf("adapter %d\n", a->no);

		for(c=a->channel;c;c=c->next) {
			struct dvbrc_channel_s	*dc;
			dc=get_channel_by_id(c->id);
			if (!dc) {
				fprintf(stderr, "Unknown channel 0x%x\n", c->id);
				exit(1);
			}

			c->dvbchannel=dc;

			if (!t)
				t=dc->transponder;

			if (t != dc->transponder) {
				fprintf(stderr, "Channels from different transponders\n");
				exit(-1);
			}
		}

		if (!t) {
			fprintf(stderr, "Oops - No transponder - No channel ?\n");
			exit(1);
		}

		/* Store transponder into adapter */
		a->transponder=t;
	}

#endif
	/* Initialize libevent */
	event_init();

	if (config->http_port) {
		hserver=http_init(config->http_port);
		if (!hserver) {
			fprintf(stderr, "Could not create http server on port %d\n", config->http_port);
			exit(-1);
		}
	}

	al=g_list_first(config->adapter);
	while(al) {
		struct adapter_s	*a=al->data;
		GList			*cl;

		cl=g_list_first(a->channel);
		while(cl) {
			struct channel_s	*c=cl->data;

			stream_init(c);
			sap_init(c);

			cl=g_list_next(cl);
		}

		/* Tune to the one and only transponder */
		fe_tune_init(a);
		/* Initialize demux0 dvr0 and co */
		demux_init(a);

		al=g_list_next(al);
	}

#ifdef DEBUG
	init_guard_thread();
#endif

	/* Pigs can fly */
	event_dispatch();

	return 0;
}