began to simplify code - use modified API for stream reading
[libav.git] / ffserver.c
CommitLineData
85f07f22
FB
1/*
2 * Multiple format streaming server
0f1578af 3 * Copyright (c) 2000, 2001, 2002 Gerard Lantau.
85f07f22
FB
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
18 */
19#include <stdarg.h>
20#include <stdlib.h>
21#include <stdio.h>
22#include <string.h>
23#include <netinet/in.h>
24#include <unistd.h>
25#include <fcntl.h>
26#include <sys/ioctl.h>
27#include <sys/poll.h>
28#include <errno.h>
29#include <sys/time.h>
30#include <time.h>
31#include <getopt.h>
32#include <sys/types.h>
33#include <sys/socket.h>
34#include <arpa/inet.h>
35#include <netdb.h>
36#include <ctype.h>
37#include <signal.h>
f5d1f41b 38#include <assert.h>
85f07f22 39
d8cf5aea 40#include "bswap.h" // needed for the bitstream writer in common.h which is included in avformat.h
85f07f22
FB
41#include "avformat.h"
42
43/* maximum number of simultaneous HTTP connections */
44#define HTTP_MAX_CONNECTIONS 2000
45
46enum HTTPState {
47 HTTPSTATE_WAIT_REQUEST,
48 HTTPSTATE_SEND_HEADER,
49 HTTPSTATE_SEND_DATA_HEADER,
50 HTTPSTATE_SEND_DATA,
51 HTTPSTATE_SEND_DATA_TRAILER,
52 HTTPSTATE_RECEIVE_DATA,
53 HTTPSTATE_WAIT_FEED,
54};
55
56const char *http_state[] = {
57 "WAIT_REQUEST",
58 "SEND_HEADER",
59 "SEND_DATA_HEADER",
60 "SEND_DATA",
61 "SEND_DATA_TRAILER",
62 "RECEIVE_DATA",
63 "WAIT_FEED",
64};
65
f747e6d3
PG
66#define IOBUFFER_MAX_SIZE 32768
67#define PACKET_MAX_SIZE 16384
85f07f22
FB
68
69/* coef for exponential mean for bitrate estimation in statistics */
70#define AVG_COEF 0.9
71
72/* timeouts are in ms */
73#define REQUEST_TIMEOUT (15 * 1000)
74#define SYNC_TIMEOUT (10 * 1000)
75
76/* context associated with one connection */
77typedef struct HTTPContext {
78 enum HTTPState state;
79 int fd; /* socket file descriptor */
80 struct sockaddr_in from_addr; /* origin */
81 struct pollfd *poll_entry; /* used when polling */
82 long timeout;
85f07f22
FB
83 UINT8 *buffer_ptr, *buffer_end;
84 int http_error;
85 struct HTTPContext *next;
42a63c6a 86 int got_key_frame; /* stream 0 => 1, stream 1 => 2, stream 2=> 4 */
85f07f22
FB
87 INT64 data_count;
88 /* feed input */
89 int feed_fd;
90 /* input format handling */
91 AVFormatContext *fmt_in;
92 /* output format handling */
93 struct FFStream *stream;
94 AVFormatContext fmt_ctx;
95 int last_packet_sent; /* true if last data packet was sent */
7434ba6d 96 int suppress_log;
42a63c6a 97 int bandwidth;
7434ba6d
PG
98 char protocol[16];
99 char method[16];
100 char url[128];
f747e6d3
PG
101 UINT8 buffer[IOBUFFER_MAX_SIZE];
102 UINT8 pbuffer[PACKET_MAX_SIZE];
85f07f22
FB
103} HTTPContext;
104
105/* each generated stream is described here */
106enum StreamType {
107 STREAM_TYPE_LIVE,
108 STREAM_TYPE_STATUS,
109};
110
111/* description of each stream of the ffserver.conf file */
112typedef struct FFStream {
113 enum StreamType stream_type;
114 char filename[1024]; /* stream filename */
115 struct FFStream *feed;
116 AVFormat *fmt;
117 int nb_streams;
42a63c6a 118 int prebuffer; /* Number of millseconds early to start */
79c4ea3c 119 int send_on_key;
85f07f22
FB
120 AVStream *streams[MAX_STREAMS];
121 int feed_streams[MAX_STREAMS]; /* index of streams in the feed */
122 char feed_filename[1024]; /* file name of the feed storage, or
123 input file name for a stream */
124 struct FFStream *next;
125 /* feed specific */
126 int feed_opened; /* true if someone if writing to feed */
127 int is_feed; /* true if it is a feed */
a6e14edd
PG
128 int conns_served;
129 INT64 bytes_served;
85f07f22
FB
130 INT64 feed_max_size; /* maximum storage size */
131 INT64 feed_write_index; /* current write position in feed (it wraps round) */
132 INT64 feed_size; /* current size of feed */
133 struct FFStream *next_feed;
134} FFStream;
135
136typedef struct FeedData {
137 long long data_count;
138 float avg_frame_size; /* frame size averraged over last frames with exponential mean */
139} FeedData;
140
141struct sockaddr_in my_addr;
142char logfilename[1024];
143HTTPContext *first_http_ctx;
144FFStream *first_feed; /* contains only feeds */
145FFStream *first_stream; /* contains all streams, including feeds */
146
147static int handle_http(HTTPContext *c, long cur_time);
148static int http_parse_request(HTTPContext *c);
149static int http_send_data(HTTPContext *c);
150static void compute_stats(HTTPContext *c);
151static int open_input_stream(HTTPContext *c, const char *info);
152static int http_start_receive_data(HTTPContext *c);
153static int http_receive_data(HTTPContext *c);
154
155int nb_max_connections;
156int nb_connections;
157
42a63c6a
PG
158int nb_max_bandwidth;
159int nb_bandwidth;
160
85f07f22
FB
161static long gettime_ms(void)
162{
163 struct timeval tv;
164
165 gettimeofday(&tv,NULL);
166 return (long long)tv.tv_sec * 1000 + (tv.tv_usec / 1000);
167}
168
169static FILE *logfile = NULL;
170
171static void http_log(char *fmt, ...)
172{
173 va_list ap;
174 va_start(ap, fmt);
175
7434ba6d 176 if (logfile) {
85f07f22 177 vfprintf(logfile, fmt, ap);
7434ba6d
PG
178 fflush(logfile);
179 }
85f07f22
FB
180 va_end(ap);
181}
182
7434ba6d
PG
183static void log_connection(HTTPContext *c)
184{
185 char buf1[32], buf2[32], *p;
186 time_t ti;
187
188 if (c->suppress_log)
189 return;
190
191 /* XXX: reentrant function ? */
192 p = inet_ntoa(c->from_addr.sin_addr);
193 strcpy(buf1, p);
194 ti = time(NULL);
195 p = ctime(&ti);
196 strcpy(buf2, p);
197 p = buf2 + strlen(p) - 1;
198 if (*p == '\n')
199 *p = '\0';
200 http_log("%s - - [%s] \"%s %s %s\" %d %lld\n",
201 buf1, buf2, c->method, c->url, c->protocol, (c->http_error ? c->http_error : 200), c->data_count);
202}
203
85f07f22
FB
204/* main loop of the http server */
205static int http_server(struct sockaddr_in my_addr)
206{
207 int server_fd, tmp, ret;
208 struct sockaddr_in from_addr;
209 struct pollfd poll_table[HTTP_MAX_CONNECTIONS + 1], *poll_entry;
210 HTTPContext *c, **cp;
211 long cur_time;
212
213 server_fd = socket(AF_INET,SOCK_STREAM,0);
214 if (server_fd < 0) {
215 perror ("socket");
216 return -1;
217 }
218
219 tmp = 1;
220 setsockopt(server_fd, SOL_SOCKET, SO_REUSEADDR, &tmp, sizeof(tmp));
221
222 if (bind (server_fd, (struct sockaddr *) &my_addr, sizeof (my_addr)) < 0) {
223 perror ("bind");
224 close(server_fd);
225 return -1;
226 }
227
228 if (listen (server_fd, 5) < 0) {
229 perror ("listen");
230 close(server_fd);
231 return -1;
232 }
233
234 http_log("ffserver started.\n");
235
236 fcntl(server_fd, F_SETFL, O_NONBLOCK);
237 first_http_ctx = NULL;
238 nb_connections = 0;
239 first_http_ctx = NULL;
240 for(;;) {
241 poll_entry = poll_table;
242 poll_entry->fd = server_fd;
243 poll_entry->events = POLLIN;
244 poll_entry++;
245
246 /* wait for events on each HTTP handle */
247 c = first_http_ctx;
248 while (c != NULL) {
249 int fd;
250 fd = c->fd;
251 switch(c->state) {
252 case HTTPSTATE_WAIT_REQUEST:
253 c->poll_entry = poll_entry;
254 poll_entry->fd = fd;
255 poll_entry->events = POLLIN;
256 poll_entry++;
257 break;
258 case HTTPSTATE_SEND_HEADER:
259 case HTTPSTATE_SEND_DATA_HEADER:
260 case HTTPSTATE_SEND_DATA:
261 case HTTPSTATE_SEND_DATA_TRAILER:
262 c->poll_entry = poll_entry;
263 poll_entry->fd = fd;
264 poll_entry->events = POLLOUT;
265 poll_entry++;
266 break;
267 case HTTPSTATE_RECEIVE_DATA:
268 c->poll_entry = poll_entry;
269 poll_entry->fd = fd;
270 poll_entry->events = POLLIN;
271 poll_entry++;
272 break;
273 case HTTPSTATE_WAIT_FEED:
274 /* need to catch errors */
275 c->poll_entry = poll_entry;
276 poll_entry->fd = fd;
a6e14edd 277 poll_entry->events = POLLIN;/* Maybe this will work */
85f07f22
FB
278 poll_entry++;
279 break;
280 default:
281 c->poll_entry = NULL;
282 break;
283 }
284 c = c->next;
285 }
286
287 /* wait for an event on one connection. We poll at least every
288 second to handle timeouts */
289 do {
290 ret = poll(poll_table, poll_entry - poll_table, 1000);
291 } while (ret == -1);
292
293 cur_time = gettime_ms();
294
295 /* now handle the events */
296
297 cp = &first_http_ctx;
298 while ((*cp) != NULL) {
299 c = *cp;
300 if (handle_http (c, cur_time) < 0) {
301 /* close and free the connection */
7434ba6d 302 log_connection(c);
85f07f22
FB
303 close(c->fd);
304 if (c->fmt_in)
305 av_close_input_file(c->fmt_in);
306 *cp = c->next;
42a63c6a 307 nb_bandwidth -= c->bandwidth;
0f1578af 308 av_free(c);
85f07f22
FB
309 nb_connections--;
310 } else {
311 cp = &c->next;
312 }
313 }
314
315 /* new connection request ? */
316 poll_entry = poll_table;
317 if (poll_entry->revents & POLLIN) {
318 int fd, len;
319
320 len = sizeof(from_addr);
321 fd = accept(server_fd, (struct sockaddr *)&from_addr,
322 &len);
323 if (fd >= 0) {
324 fcntl(fd, F_SETFL, O_NONBLOCK);
325 /* XXX: should output a warning page when coming
326 close to the connection limit */
327 if (nb_connections >= nb_max_connections) {
328 close(fd);
329 } else {
330 /* add a new connection */
331 c = av_mallocz(sizeof(HTTPContext));
332 c->next = first_http_ctx;
333 first_http_ctx = c;
334 c->fd = fd;
335 c->poll_entry = NULL;
336 c->from_addr = from_addr;
337 c->state = HTTPSTATE_WAIT_REQUEST;
338 c->buffer_ptr = c->buffer;
339 c->buffer_end = c->buffer + IOBUFFER_MAX_SIZE;
340 c->timeout = cur_time + REQUEST_TIMEOUT;
341 nb_connections++;
342 }
343 }
344 }
345 poll_entry++;
346 }
347}
348
349static int handle_http(HTTPContext *c, long cur_time)
350{
351 int len;
352
353 switch(c->state) {
354 case HTTPSTATE_WAIT_REQUEST:
355 /* timeout ? */
356 if ((c->timeout - cur_time) < 0)
357 return -1;
358 if (c->poll_entry->revents & (POLLERR | POLLHUP))
359 return -1;
360
361 /* no need to read if no events */
362 if (!(c->poll_entry->revents & POLLIN))
363 return 0;
364 /* read the data */
365 len = read(c->fd, c->buffer_ptr, c->buffer_end - c->buffer_ptr);
366 if (len < 0) {
367 if (errno != EAGAIN && errno != EINTR)
368 return -1;
369 } else if (len == 0) {
370 return -1;
371 } else {
372 /* search for end of request. XXX: not fully correct since garbage could come after the end */
373 UINT8 *ptr;
374 c->buffer_ptr += len;
375 ptr = c->buffer_ptr;
376 if ((ptr >= c->buffer + 2 && !memcmp(ptr-2, "\n\n", 2)) ||
377 (ptr >= c->buffer + 4 && !memcmp(ptr-4, "\r\n\r\n", 4))) {
378 /* request found : parse it and reply */
379 if (http_parse_request(c) < 0)
380 return -1;
381 } else if (ptr >= c->buffer_end) {
382 /* request too long: cannot do anything */
383 return -1;
384 }
385 }
386 break;
387
388 case HTTPSTATE_SEND_HEADER:
389 if (c->poll_entry->revents & (POLLERR | POLLHUP))
390 return -1;
391
392 /* no need to read if no events */
393 if (!(c->poll_entry->revents & POLLOUT))
394 return 0;
395 len = write(c->fd, c->buffer_ptr, c->buffer_end - c->buffer_ptr);
396 if (len < 0) {
397 if (errno != EAGAIN && errno != EINTR) {
398 /* error : close connection */
399 return -1;
400 }
401 } else {
402 c->buffer_ptr += len;
2e04edb3
PG
403 if (c->stream)
404 c->stream->bytes_served += len;
a6e14edd 405 c->data_count += len;
85f07f22
FB
406 if (c->buffer_ptr >= c->buffer_end) {
407 /* if error, exit */
408 if (c->http_error)
409 return -1;
410 /* all the buffer was send : synchronize to the incoming stream */
411 c->state = HTTPSTATE_SEND_DATA_HEADER;
412 c->buffer_ptr = c->buffer_end = c->buffer;
413 }
414 }
415 break;
416
417 case HTTPSTATE_SEND_DATA:
418 case HTTPSTATE_SEND_DATA_HEADER:
419 case HTTPSTATE_SEND_DATA_TRAILER:
420 /* no need to read if no events */
421 if (c->poll_entry->revents & (POLLERR | POLLHUP))
422 return -1;
423
424 if (!(c->poll_entry->revents & POLLOUT))
425 return 0;
426 if (http_send_data(c) < 0)
427 return -1;
428 break;
429 case HTTPSTATE_RECEIVE_DATA:
430 /* no need to read if no events */
431 if (c->poll_entry->revents & (POLLERR | POLLHUP))
432 return -1;
433 if (!(c->poll_entry->revents & POLLIN))
434 return 0;
435 if (http_receive_data(c) < 0)
436 return -1;
437 break;
438 case HTTPSTATE_WAIT_FEED:
439 /* no need to read if no events */
a6e14edd 440 if (c->poll_entry->revents & (POLLIN | POLLERR | POLLHUP))
85f07f22
FB
441 return -1;
442
443 /* nothing to do, we'll be waken up by incoming feed packets */
444 break;
445 default:
446 return -1;
447 }
448 return 0;
449}
450
7434ba6d 451
85f07f22
FB
452/* parse http request and prepare header */
453static int http_parse_request(HTTPContext *c)
454{
455 char *p;
456 int post;
7434ba6d 457 int doing_asx;
42a63c6a 458 int doing_ram;
85f07f22
FB
459 char cmd[32];
460 char info[1024], *filename;
461 char url[1024], *q;
462 char protocol[32];
463 char msg[1024];
464 const char *mime_type;
465 FFStream *stream;
42a63c6a 466 int i;
85f07f22
FB
467
468 p = c->buffer;
469 q = cmd;
470 while (!isspace(*p) && *p != '\0') {
471 if ((q - cmd) < sizeof(cmd) - 1)
472 *q++ = *p;
473 p++;
474 }
475 *q = '\0';
7434ba6d
PG
476
477 strlcpy(c->method, cmd, sizeof(c->method));
478
85f07f22
FB
479 if (!strcmp(cmd, "GET"))
480 post = 0;
481 else if (!strcmp(cmd, "POST"))
482 post = 1;
483 else
484 return -1;
485
486 while (isspace(*p)) p++;
487 q = url;
488 while (!isspace(*p) && *p != '\0') {
489 if ((q - url) < sizeof(url) - 1)
490 *q++ = *p;
491 p++;
492 }
493 *q = '\0';
494
7434ba6d
PG
495 strlcpy(c->url, url, sizeof(c->url));
496
85f07f22
FB
497 while (isspace(*p)) p++;
498 q = protocol;
499 while (!isspace(*p) && *p != '\0') {
500 if ((q - protocol) < sizeof(protocol) - 1)
501 *q++ = *p;
502 p++;
503 }
504 *q = '\0';
505 if (strcmp(protocol, "HTTP/1.0") && strcmp(protocol, "HTTP/1.1"))
506 return -1;
7434ba6d
PG
507
508 strlcpy(c->protocol, protocol, sizeof(c->protocol));
85f07f22
FB
509
510 /* find the filename and the optional info string in the request */
511 p = url;
512 if (*p == '/')
513 p++;
514 filename = p;
515 p = strchr(p, '?');
516 if (p) {
7434ba6d 517 strlcpy(info, p, sizeof(info));
85f07f22
FB
518 *p = '\0';
519 } else {
520 info[0] = '\0';
521 }
522
7434ba6d
PG
523 if (strlen(filename) > 4 && strcmp(".asx", filename + strlen(filename) - 4) == 0) {
524 doing_asx = 1;
525 filename[strlen(filename)-1] = 'f';
526 } else {
527 doing_asx = 0;
528 }
529
42a63c6a
PG
530 if (strlen(filename) > 4 &&
531 (strcmp(".rpm", filename + strlen(filename) - 4) == 0 ||
532 strcmp(".ram", filename + strlen(filename) - 4) == 0)) {
533 doing_ram = 1;
534 strcpy(filename + strlen(filename)-2, "m");
535 } else {
536 doing_ram = 0;
537 }
538
85f07f22
FB
539 stream = first_stream;
540 while (stream != NULL) {
541 if (!strcmp(stream->filename, filename))
542 break;
543 stream = stream->next;
544 }
545 if (stream == NULL) {
546 sprintf(msg, "File '%s' not found", url);
547 goto send_error;
548 }
42a63c6a
PG
549
550 if (post == 0 && stream->stream_type == STREAM_TYPE_LIVE) {
551 /* See if we meet the bandwidth requirements */
552 for(i=0;i<stream->nb_streams;i++) {
553 AVStream *st = stream->streams[i];
554 switch(st->codec.codec_type) {
555 case CODEC_TYPE_AUDIO:
556 c->bandwidth += st->codec.bit_rate;
557 break;
558 case CODEC_TYPE_VIDEO:
559 c->bandwidth += st->codec.bit_rate;
560 break;
561 default:
562 abort();
563 }
564 }
565 }
566
567 c->bandwidth /= 1000;
568 nb_bandwidth += c->bandwidth;
569
570 if (post == 0 && nb_max_bandwidth < nb_bandwidth) {
571 c->http_error = 200;
572 q = c->buffer;
573 q += sprintf(q, "HTTP/1.0 200 Server too busy\r\n");
574 q += sprintf(q, "Content-type: text/html\r\n");
575 q += sprintf(q, "\r\n");
576 q += sprintf(q, "<html><head><title>Too busy</title></head><body>\r\n");
577 q += sprintf(q, "The server is too busy to serve your request at this time.<p>\r\n");
578 q += sprintf(q, "The bandwidth being served (including your stream) is %dkbit/sec, and this exceeds the limit of %dkbit/sec\r\n",
579 nb_bandwidth, nb_max_bandwidth);
580 q += sprintf(q, "</body></html>\r\n");
581
582 /* prepare output buffer */
583 c->buffer_ptr = c->buffer;
584 c->buffer_end = q;
585 c->state = HTTPSTATE_SEND_HEADER;
586 return 0;
587 }
588
589 if (doing_asx || doing_ram) {
7434ba6d
PG
590 char *hostinfo = 0;
591
592 for (p = c->buffer; *p && *p != '\r' && *p != '\n'; ) {
593 if (strncasecmp(p, "Host:", 5) == 0) {
594 hostinfo = p + 5;
595 break;
596 }
597 p = strchr(p, '\n');
598 if (!p)
599 break;
600
601 p++;
602 }
603
604 if (hostinfo) {
605 char *eoh;
606 char hostbuf[260];
607
608 while (isspace(*hostinfo))
609 hostinfo++;
610
611 eoh = strchr(hostinfo, '\n');
612 if (eoh) {
613 if (eoh[-1] == '\r')
614 eoh--;
615
616 if (eoh - hostinfo < sizeof(hostbuf) - 1) {
617 memcpy(hostbuf, hostinfo, eoh - hostinfo);
618 hostbuf[eoh - hostinfo] = 0;
619
620 c->http_error = 200;
621 q = c->buffer;
42a63c6a
PG
622 if (doing_asx) {
623 q += sprintf(q, "HTTP/1.0 200 ASX Follows\r\n");
624 q += sprintf(q, "Content-type: video/x-ms-asf\r\n");
625 q += sprintf(q, "\r\n");
626 q += sprintf(q, "<ASX Version=\"3\">\r\n");
627 q += sprintf(q, "<!-- Autogenerated by ffserver -->\r\n");
628 q += sprintf(q, "<ENTRY><REF HREF=\"http://%s/%s%s\"/></ENTRY>\r\n",
629 hostbuf, filename, info);
630 q += sprintf(q, "</ASX>\r\n");
631 } else if (doing_ram) {
632 q += sprintf(q, "HTTP/1.0 200 RAM Follows\r\n");
633 q += sprintf(q, "Content-type: audio/x-pn-realaudio\r\n");
634 q += sprintf(q, "\r\n");
635 q += sprintf(q, "# Autogenerated by ffserver\r\n");
636 q += sprintf(q, "http://%s/%s%s\r\n",
637 hostbuf, filename, info);
638 } else
639 abort();
7434ba6d
PG
640
641 /* prepare output buffer */
642 c->buffer_ptr = c->buffer;
643 c->buffer_end = q;
644 c->state = HTTPSTATE_SEND_HEADER;
645 return 0;
646 }
647 }
648 }
649
42a63c6a 650 sprintf(msg, "ASX/RAM file not handled");
7434ba6d 651 goto send_error;
85f07f22
FB
652 }
653
7434ba6d 654 c->stream = stream;
a6e14edd 655 stream->conns_served++;
7434ba6d 656
85f07f22
FB
657 /* XXX: add there authenticate and IP match */
658
659 if (post) {
660 /* if post, it means a feed is being sent */
661 if (!stream->is_feed) {
7434ba6d
PG
662 /* However it might be a status report from WMP! Lets log the data
663 * as it might come in handy one day
664 */
665 char *logline = 0;
666
667 for (p = c->buffer; *p && *p != '\r' && *p != '\n'; ) {
668 if (strncasecmp(p, "Pragma: log-line=", 17) == 0) {
669 logline = p;
670 break;
671 }
672 p = strchr(p, '\n');
673 if (!p)
674 break;
675
676 p++;
677 }
678
679 if (logline) {
680 char *eol = strchr(logline, '\n');
681
682 logline += 17;
683
684 if (eol) {
685 if (eol[-1] == '\r')
686 eol--;
687 http_log("%.*s\n", eol - logline, logline);
688 c->suppress_log = 1;
689 }
690 }
691
85f07f22
FB
692 sprintf(msg, "POST command not handled");
693 goto send_error;
694 }
695 if (http_start_receive_data(c) < 0) {
696 sprintf(msg, "could not open feed");
697 goto send_error;
698 }
699 c->http_error = 0;
700 c->state = HTTPSTATE_RECEIVE_DATA;
701 return 0;
702 }
703
704 if (c->stream->stream_type == STREAM_TYPE_STATUS)
705 goto send_stats;
706
707 /* open input stream */
708 if (open_input_stream(c, info) < 0) {
709 sprintf(msg, "Input stream corresponding to '%s' not found", url);
710 goto send_error;
711 }
712
713 /* prepare http header */
714 q = c->buffer;
715 q += sprintf(q, "HTTP/1.0 200 OK\r\n");
716 mime_type = c->stream->fmt->mime_type;
717 if (!mime_type)
718 mime_type = "application/x-octet_stream";
85f07f22
FB
719 q += sprintf(q, "Pragma: no-cache\r\n");
720
721 /* for asf, we need extra headers */
722 if (!strcmp(c->stream->fmt->name,"asf")) {
f747e6d3 723 q += sprintf(q, "Server: Cougar 4.1.0.3923\r\nCache-Control: no-cache\r\nPragma: client-id=1234\r\nPragma: features=\"broadcast\"\r\n");
7434ba6d
PG
724 /* mime_type = "application/octet-stream"; */
725 /* video/x-ms-asf seems better -- netscape doesn't crash any more! */
726 mime_type = "video/x-ms-asf";
85f07f22 727 }
f747e6d3 728 q += sprintf(q, "Content-Type: %s\r\n", mime_type);
85f07f22
FB
729 q += sprintf(q, "\r\n");
730
731 /* prepare output buffer */
732 c->http_error = 0;
733 c->buffer_ptr = c->buffer;
734 c->buffer_end = q;
735 c->state = HTTPSTATE_SEND_HEADER;
736 return 0;
737 send_error:
738 c->http_error = 404;
739 q = c->buffer;
740 q += sprintf(q, "HTTP/1.0 404 Not Found\r\n");
741 q += sprintf(q, "Content-type: %s\r\n", "text/html");
742 q += sprintf(q, "\r\n");
743 q += sprintf(q, "<HTML>\n");
744 q += sprintf(q, "<HEAD><TITLE>404 Not Found</TITLE></HEAD>\n");
745 q += sprintf(q, "<BODY>%s</BODY>\n", msg);
746 q += sprintf(q, "</HTML>\n");
747
748 /* prepare output buffer */
749 c->buffer_ptr = c->buffer;
750 c->buffer_end = q;
751 c->state = HTTPSTATE_SEND_HEADER;
752 return 0;
753 send_stats:
754 compute_stats(c);
755 c->http_error = 200; /* horrible : we use this value to avoid
756 going to the send data state */
757 c->state = HTTPSTATE_SEND_HEADER;
758 return 0;
759}
760
761static void compute_stats(HTTPContext *c)
762{
763 HTTPContext *c1;
764 FFStream *stream;
765 char *q, *p;
766 time_t ti;
767 int i;
768
769 q = c->buffer;
770 q += sprintf(q, "HTTP/1.0 200 OK\r\n");
771 q += sprintf(q, "Content-type: %s\r\n", "text/html");
772 q += sprintf(q, "Pragma: no-cache\r\n");
773 q += sprintf(q, "\r\n");
774
775 q += sprintf(q, "<HEAD><TITLE>FFServer Status</TITLE></HEAD>\n<BODY>");
776 q += sprintf(q, "<H1>FFServer Status</H1>\n");
777 /* format status */
79c4ea3c 778 q += sprintf(q, "<H2>Available Streams</H2>\n");
a6e14edd
PG
779 q += sprintf(q, "<TABLE cellspacing=0 cellpadding=4>\n");
780 q += sprintf(q, "<TR><Th valign=top>Path<th align=left>Served<br>Conns<Th><br>kbytes<Th valign=top>Format<Th>Bit rate<br>kbits/s<Th align=left>Video<br>kbits/s<th><br>Codec<Th align=left>Audio<br>kbits/s<th><br>Codec<Th align=left valign=top>Feed\n");
85f07f22
FB
781 stream = first_stream;
782 while (stream != NULL) {
42a63c6a
PG
783 char sfilename[1024];
784 char *eosf;
785
a6e14edd
PG
786 if (stream->feed != stream) {
787 strlcpy(sfilename, stream->filename, sizeof(sfilename) - 1);
788 eosf = sfilename + strlen(sfilename);
789 if (eosf - sfilename >= 4) {
790 if (strcmp(eosf - 4, ".asf") == 0) {
791 strcpy(eosf - 4, ".asx");
792 } else if (strcmp(eosf - 3, ".rm") == 0) {
793 strcpy(eosf - 3, ".ram");
794 }
42a63c6a 795 }
a6e14edd
PG
796
797 q += sprintf(q, "<TR><TD><A HREF=\"/%s\">%s</A> ",
798 sfilename, stream->filename);
799 q += sprintf(q, "<td align=right> %d <td align=right> %lld",
800 stream->conns_served, stream->bytes_served / 1000);
801 switch(stream->stream_type) {
802 case STREAM_TYPE_LIVE:
803 {
804 int audio_bit_rate = 0;
805 int video_bit_rate = 0;
806 char *audio_codec_name = "";
807 char *video_codec_name = "";
808 char *audio_codec_name_extra = "";
809 char *video_codec_name_extra = "";
810
811 for(i=0;i<stream->nb_streams;i++) {
812 AVStream *st = stream->streams[i];
813 AVCodec *codec = avcodec_find_encoder(st->codec.codec_id);
814 switch(st->codec.codec_type) {
815 case CODEC_TYPE_AUDIO:
816 audio_bit_rate += st->codec.bit_rate;
817 if (codec) {
818 if (*audio_codec_name)
819 audio_codec_name_extra = "...";
820 audio_codec_name = codec->name;
821 }
822 break;
823 case CODEC_TYPE_VIDEO:
824 video_bit_rate += st->codec.bit_rate;
825 if (codec) {
826 if (*video_codec_name)
827 video_codec_name_extra = "...";
828 video_codec_name = codec->name;
829 }
830 break;
831 default:
832 abort();
79c4ea3c 833 }
85f07f22 834 }
a6e14edd
PG
835 q += sprintf(q, "<TD align=center> %s <TD align=right> %d <TD align=right> %d <TD> %s %s <TD align=right> %d <TD> %s %s",
836 stream->fmt->name,
837 (audio_bit_rate + video_bit_rate) / 1000,
838 video_bit_rate / 1000, video_codec_name, video_codec_name_extra,
839 audio_bit_rate / 1000, audio_codec_name, audio_codec_name_extra);
840 if (stream->feed) {
841 q += sprintf(q, "<TD>%s", stream->feed->filename);
842 } else {
843 q += sprintf(q, "<TD>%s", stream->feed_filename);
844 }
845 q += sprintf(q, "\n");
85f07f22 846 }
a6e14edd
PG
847 break;
848 default:
849 q += sprintf(q, "<TD align=center> - <TD align=right> - <TD align=right> - <td><td align=right> - <TD>\n");
850 break;
85f07f22 851 }
85f07f22
FB
852 }
853 stream = stream->next;
854 }
855 q += sprintf(q, "</TABLE>\n");
a6e14edd
PG
856
857 stream = first_stream;
858 while (stream != NULL) {
859 if (stream->feed == stream) {
860 q += sprintf(q, "<h2>Feed %s</h2>", stream->filename);
861 q += sprintf(q, "<table cellspacing=0 cellpadding=4><tr><th>Stream<th>type<th>kbits/s<th align=left>codec\n");
862
863 for (i = 0; i < stream->nb_streams; i++) {
864 AVStream *st = stream->streams[i];
865 AVCodec *codec = avcodec_find_encoder(st->codec.codec_id);
866 char *type = "unknown";
867
868 switch(st->codec.codec_type) {
869 case CODEC_TYPE_AUDIO:
870 type = "audio";
871 break;
872 case CODEC_TYPE_VIDEO:
873 type = "video";
874 break;
875 default:
876 abort();
877 }
878 q += sprintf(q, "<tr><td align=right>%d<td>%s<td align=right>%d<td>%s\n",
879 i, type, st->codec.bit_rate/1000, codec ? codec->name : "");
880 }
881 q += sprintf(q, "</table>\n");
882
883 }
884 stream = stream->next;
885 }
85f07f22
FB
886
887#if 0
888 {
889 float avg;
890 AVCodecContext *enc;
891 char buf[1024];
892
893 /* feed status */
894 stream = first_feed;
895 while (stream != NULL) {
896 q += sprintf(q, "<H1>Feed '%s'</H1>\n", stream->filename);
897 q += sprintf(q, "<TABLE>\n");
898 q += sprintf(q, "<TR><TD>Parameters<TD>Frame count<TD>Size<TD>Avg bitrate (kbits/s)\n");
899 for(i=0;i<stream->nb_streams;i++) {
900 AVStream *st = stream->streams[i];
901 FeedData *fdata = st->priv_data;
902 enc = &st->codec;
903
904 avcodec_string(buf, sizeof(buf), enc);
905 avg = fdata->avg_frame_size * (float)enc->rate * 8.0;
906 if (enc->codec->type == CODEC_TYPE_AUDIO && enc->frame_size > 0)
907 avg /= enc->frame_size;
908 q += sprintf(q, "<TR><TD>%s <TD> %d <TD> %Ld <TD> %0.1f\n",
909 buf, enc->frame_number, fdata->data_count, avg / 1000.0);
910 }
911 q += sprintf(q, "</TABLE>\n");
912 stream = stream->next_feed;
913 }
914 }
915#endif
916
917 /* connection status */
79c4ea3c 918 q += sprintf(q, "<H2>Connection Status</H2>\n");
85f07f22
FB
919
920 q += sprintf(q, "Number of connections: %d / %d<BR>\n",
921 nb_connections, nb_max_connections);
922
42a63c6a
PG
923 q += sprintf(q, "Bandwidth in use: %dk / %dk<BR>\n",
924 nb_bandwidth, nb_max_bandwidth);
925
85f07f22
FB
926 q += sprintf(q, "<TABLE>\n");
927 q += sprintf(q, "<TR><TD>#<TD>File<TD>IP<TD>State<TD>Size\n");
928 c1 = first_http_ctx;
929 i = 0;
a6e14edd 930 while (c1 != NULL && q < (char *) c->buffer + sizeof(c->buffer) - 2048) {
85f07f22
FB
931 i++;
932 p = inet_ntoa(c1->from_addr.sin_addr);
933 q += sprintf(q, "<TR><TD><B>%d</B><TD>%s%s <TD> %s <TD> %s <TD> %Ld\n",
934 i, c1->stream->filename,
935 c1->state == HTTPSTATE_RECEIVE_DATA ? "(input)" : "",
936 p,
937 http_state[c1->state],
938 c1->data_count);
939 c1 = c1->next;
940 }
941 q += sprintf(q, "</TABLE>\n");
942
943 /* date */
944 ti = time(NULL);
945 p = ctime(&ti);
a6e14edd 946 q += sprintf(q, "<HR size=1 noshade>Generated at %s", p);
85f07f22
FB
947 q += sprintf(q, "</BODY>\n</HTML>\n");
948
949 c->buffer_ptr = c->buffer;
950 c->buffer_end = q;
951}
952
953
954static void http_write_packet(void *opaque,
955 unsigned char *buf, int size)
956{
957 HTTPContext *c = opaque;
f747e6d3
PG
958
959 if (c->buffer_ptr == c->buffer_end || !c->buffer_ptr)
960 c->buffer_ptr = c->buffer_end = c->buffer;
961
962 if (c->buffer_end - c->buffer + size > IOBUFFER_MAX_SIZE)
85f07f22 963 abort();
f747e6d3
PG
964
965 memcpy(c->buffer_end, buf, size);
966 c->buffer_end += size;
85f07f22
FB
967}
968
969static int open_input_stream(HTTPContext *c, const char *info)
970{
971 char buf[128];
972 char input_filename[1024];
973 AVFormatContext *s;
974 int buf_size;
975 INT64 stream_pos;
976
977 /* find file name */
978 if (c->stream->feed) {
979 strcpy(input_filename, c->stream->feed->feed_filename);
980 buf_size = FFM_PACKET_SIZE;
981 /* compute position (absolute time) */
982 if (find_info_tag(buf, sizeof(buf), "date", info)) {
983 stream_pos = parse_date(buf, 0);
f747e6d3
PG
984 } else if (find_info_tag(buf, sizeof(buf), "buffer", info)) {
985 int prebuffer = strtol(buf, 0, 10);
986 stream_pos = gettime() - prebuffer * 1000000;
85f07f22 987 } else {
42a63c6a 988 stream_pos = gettime() - c->stream->prebuffer * 1000;
85f07f22
FB
989 }
990 } else {
991 strcpy(input_filename, c->stream->feed_filename);
992 buf_size = 0;
993 /* compute position (relative time) */
994 if (find_info_tag(buf, sizeof(buf), "date", info)) {
995 stream_pos = parse_date(buf, 1);
996 } else {
997 stream_pos = 0;
998 }
999 }
1000 if (input_filename[0] == '\0')
1001 return -1;
1002
1003 /* open stream */
63bdb086 1004 s = av_open_input_file(input_filename, NULL, buf_size, NULL);
85f07f22
FB
1005 if (!s)
1006 return -1;
1007 c->fmt_in = s;
1008
1009 if (c->fmt_in->format->read_seek) {
1010 c->fmt_in->format->read_seek(c->fmt_in, stream_pos);
1011 }
1012
1013 // printf("stream %s opened pos=%0.6f\n", input_filename, stream_pos / 1000000.0);
1014 return 0;
1015}
1016
1017static int http_prepare_data(HTTPContext *c)
1018{
1019 int i;
1020
1021 switch(c->state) {
1022 case HTTPSTATE_SEND_DATA_HEADER:
1023 memset(&c->fmt_ctx, 0, sizeof(c->fmt_ctx));
1024 if (c->stream->feed) {
1025 /* open output stream by using specified codecs */
1026 c->fmt_ctx.format = c->stream->fmt;
1027 c->fmt_ctx.nb_streams = c->stream->nb_streams;
1028 for(i=0;i<c->fmt_ctx.nb_streams;i++) {
1029 AVStream *st;
1030 st = av_mallocz(sizeof(AVStream));
1031 c->fmt_ctx.streams[i] = st;
f747e6d3
PG
1032 if (c->stream->feed == c->stream)
1033 memcpy(st, c->stream->streams[i], sizeof(AVStream));
1034 else
1035 memcpy(st, c->stream->feed->streams[c->stream->feed_streams[i]], sizeof(AVStream));
1036
85f07f22
FB
1037 st->codec.frame_number = 0; /* XXX: should be done in
1038 AVStream, not in codec */
85f07f22 1039 }
42a63c6a 1040 c->got_key_frame = 0;
85f07f22
FB
1041 } else {
1042 /* open output stream by using codecs in specified file */
1043 c->fmt_ctx.format = c->stream->fmt;
1044 c->fmt_ctx.nb_streams = c->fmt_in->nb_streams;
1045 for(i=0;i<c->fmt_ctx.nb_streams;i++) {
1046 AVStream *st;
1047 st = av_mallocz(sizeof(AVStream));
1048 c->fmt_ctx.streams[i] = st;
1049 memcpy(st, c->fmt_in->streams[i], sizeof(AVStream));
1050 st->codec.frame_number = 0; /* XXX: should be done in
1051 AVStream, not in codec */
85f07f22 1052 }
42a63c6a 1053 c->got_key_frame = 0;
85f07f22 1054 }
f747e6d3 1055 init_put_byte(&c->fmt_ctx.pb, c->pbuffer, PACKET_MAX_SIZE,
85f07f22
FB
1056 1, c, NULL, http_write_packet, NULL);
1057 c->fmt_ctx.pb.is_streamed = 1;
1058 /* prepare header */
1059 c->fmt_ctx.format->write_header(&c->fmt_ctx);
1060 c->state = HTTPSTATE_SEND_DATA;
1061 c->last_packet_sent = 0;
1062 break;
1063 case HTTPSTATE_SEND_DATA:
1064 /* find a new packet */
1065#if 0
1066 fifo_total_size = http_fifo_write_count - c->last_http_fifo_write_count;
1067 if (fifo_total_size >= ((3 * FIFO_MAX_SIZE) / 4)) {
1068 /* overflow : resync. We suppose that wptr is at this
1069 point a pointer to a valid packet */
1070 c->rptr = http_fifo.wptr;
42a63c6a 1071 c->got_key_frame = 0;
85f07f22
FB
1072 }
1073
1074 start_rptr = c->rptr;
1075 if (fifo_read(&http_fifo, (UINT8 *)&hdr, sizeof(hdr), &c->rptr) < 0)
1076 return 0;
1077 payload_size = ntohs(hdr.payload_size);
0f1578af 1078 payload = av_malloc(payload_size);
85f07f22
FB
1079 if (fifo_read(&http_fifo, payload, payload_size, &c->rptr) < 0) {
1080 /* cannot read all the payload */
0f1578af 1081 av_free(payload);
85f07f22
FB
1082 c->rptr = start_rptr;
1083 return 0;
1084 }
1085
1086 c->last_http_fifo_write_count = http_fifo_write_count -
1087 fifo_size(&http_fifo, c->rptr);
1088
1089 if (c->stream->stream_type != STREAM_TYPE_MASTER) {
1090 /* test if the packet can be handled by this format */
1091 ret = 0;
1092 for(i=0;i<c->fmt_ctx.nb_streams;i++) {
1093 AVStream *st = c->fmt_ctx.streams[i];
1094 if (test_header(&hdr, &st->codec)) {
1095 /* only begin sending when got a key frame */
1096 if (st->codec.key_frame)
42a63c6a
PG
1097 c->got_key_frame |= 1 << i;
1098 if (c->got_key_frame & (1 << i)) {
85f07f22
FB
1099 ret = c->fmt_ctx.format->write_packet(&c->fmt_ctx, i,
1100 payload, payload_size);
1101 }
1102 break;
1103 }
1104 }
1105 if (ret) {
1106 /* must send trailer now */
1107 c->state = HTTPSTATE_SEND_DATA_TRAILER;
1108 }
1109 } else {
1110 /* master case : send everything */
1111 char *q;
1112 q = c->buffer;
1113 memcpy(q, &hdr, sizeof(hdr));
1114 q += sizeof(hdr);
1115 memcpy(q, payload, payload_size);
1116 q += payload_size;
1117 c->buffer_ptr = c->buffer;
1118 c->buffer_end = q;
1119 }
0f1578af 1120 av_free(payload);
85f07f22
FB
1121#endif
1122 {
1123 AVPacket pkt;
1124
1125 /* read a packet from the input stream */
1126 if (c->stream->feed) {
1127 ffm_set_write_index(c->fmt_in,
1128 c->stream->feed->feed_write_index,
1129 c->stream->feed->feed_size);
1130 }
a6e14edd 1131
85f07f22
FB
1132 if (av_read_packet(c->fmt_in, &pkt) < 0) {
1133 if (c->stream->feed && c->stream->feed->feed_opened) {
1134 /* if coming from feed, it means we reached the end of the
1135 ffm file, so must wait for more data */
1136 c->state = HTTPSTATE_WAIT_FEED;
1137 return 1; /* state changed */
1138 } else {
1139 /* must send trailer now because eof or error */
1140 c->state = HTTPSTATE_SEND_DATA_TRAILER;
1141 }
1142 } else {
1143 /* send it to the appropriate stream */
1144 if (c->stream->feed) {
1145 /* if coming from a feed, select the right stream */
1146 for(i=0;i<c->stream->nb_streams;i++) {
1147 if (c->stream->feed_streams[i] == pkt.stream_index) {
1148 pkt.stream_index = i;
42a63c6a
PG
1149 if (pkt.flags & PKT_FLAG_KEY) {
1150 c->got_key_frame |= 1 << i;
1151 }
1152 /* See if we have all the key frames, then
1153 * we start to send. This logic is not quite
1154 * right, but it works for the case of a
1155 * single video stream with one or more
1156 * audio streams (for which every frame is
1157 * typically a key frame).
1158 */
79c4ea3c 1159 if (!c->stream->send_on_key || ((c->got_key_frame + 1) >> c->stream->nb_streams)) {
42a63c6a
PG
1160 goto send_it;
1161 }
85f07f22
FB
1162 }
1163 }
1164 } else {
f747e6d3
PG
1165 AVCodecContext *codec;
1166 send_it:
1167 /* Fudge here */
1168 codec = &c->fmt_ctx.streams[pkt.stream_index]->codec;
1169
1170 codec->key_frame = ((pkt.flags & PKT_FLAG_KEY) != 0);
1171
1172#ifdef PJSG
1173 if (codec->codec_type == CODEC_TYPE_AUDIO) {
1174 codec->frame_size = (codec->sample_rate * pkt.duration + 500000) / 1000000;
1175 /* printf("Calculated size %d, from sr %d, duration %d\n", codec->frame_size, codec->sample_rate, pkt.duration); */
1176 }
1177#endif
1178
10bb7023 1179 if (av_write_packet(&c->fmt_ctx, &pkt, 0))
f747e6d3
PG
1180 c->state = HTTPSTATE_SEND_DATA_TRAILER;
1181
1182 codec->frame_number++;
85f07f22 1183 }
42a63c6a 1184
85f07f22
FB
1185 av_free_packet(&pkt);
1186 }
1187 }
1188 break;
1189 default:
1190 case HTTPSTATE_SEND_DATA_TRAILER:
1191 /* last packet test ? */
1192 if (c->last_packet_sent)
1193 return -1;
1194 /* prepare header */
1195 c->fmt_ctx.format->write_trailer(&c->fmt_ctx);
1196 c->last_packet_sent = 1;
1197 break;
1198 }
1199 return 0;
1200}
1201
1202/* should convert the format at the same time */
1203static int http_send_data(HTTPContext *c)
1204{
1205 int len, ret;
1206
1207 while (c->buffer_ptr >= c->buffer_end) {
1208 ret = http_prepare_data(c);
1209 if (ret < 0)
1210 return -1;
1211 else if (ret == 0) {
a6e14edd 1212 continue;
85f07f22
FB
1213 } else {
1214 /* state change requested */
1215 return 0;
1216 }
1217 }
1218
f747e6d3
PG
1219 if (c->buffer_end > c->buffer_ptr) {
1220 len = write(c->fd, c->buffer_ptr, c->buffer_end - c->buffer_ptr);
1221 if (len < 0) {
1222 if (errno != EAGAIN && errno != EINTR) {
1223 /* error : close connection */
1224 return -1;
1225 }
1226 } else {
1227 c->buffer_ptr += len;
1228 c->data_count += len;
2e04edb3
PG
1229 if (c->stream)
1230 c->stream->bytes_served += len;
85f07f22 1231 }
85f07f22
FB
1232 }
1233 return 0;
1234}
1235
1236static int http_start_receive_data(HTTPContext *c)
1237{
1238 int fd;
1239
1240 if (c->stream->feed_opened)
1241 return -1;
1242
1243 /* open feed */
1244 fd = open(c->stream->feed_filename, O_RDWR);
1245 if (fd < 0)
1246 return -1;
1247 c->feed_fd = fd;
1248
1249 c->stream->feed_write_index = ffm_read_write_index(fd);
1250 c->stream->feed_size = lseek(fd, 0, SEEK_END);
1251 lseek(fd, 0, SEEK_SET);
1252
1253 /* init buffer input */
1254 c->buffer_ptr = c->buffer;
1255 c->buffer_end = c->buffer + FFM_PACKET_SIZE;
1256 c->stream->feed_opened = 1;
1257 return 0;
1258}
1259
1260static int http_receive_data(HTTPContext *c)
1261{
85f07f22
FB
1262 HTTPContext *c1;
1263
a6e14edd
PG
1264 if (c->buffer_end > c->buffer_ptr) {
1265 int len;
1266
1267 len = read(c->fd, c->buffer_ptr, c->buffer_end - c->buffer_ptr);
1268 if (len < 0) {
1269 if (errno != EAGAIN && errno != EINTR) {
1270 /* error : close connection */
1271 goto fail;
1272 }
1273 } else if (len == 0) {
1274 /* end of connection : close it */
1275 goto fail;
1276 } else {
1277 c->buffer_ptr += len;
1278 c->data_count += len;
1279 }
1280 }
1281
85f07f22 1282 if (c->buffer_ptr >= c->buffer_end) {
f747e6d3 1283 FFStream *feed = c->stream;
85f07f22
FB
1284 /* a packet has been received : write it in the store, except
1285 if header */
1286 if (c->data_count > FFM_PACKET_SIZE) {
85f07f22
FB
1287
1288 // printf("writing pos=0x%Lx size=0x%Lx\n", feed->feed_write_index, feed->feed_size);
1289 /* XXX: use llseek or url_seek */
1290 lseek(c->feed_fd, feed->feed_write_index, SEEK_SET);
1291 write(c->feed_fd, c->buffer, FFM_PACKET_SIZE);
1292
1293 feed->feed_write_index += FFM_PACKET_SIZE;
1294 /* update file size */
1295 if (feed->feed_write_index > c->stream->feed_size)
1296 feed->feed_size = feed->feed_write_index;
1297
1298 /* handle wrap around if max file size reached */
1299 if (feed->feed_write_index >= c->stream->feed_max_size)
1300 feed->feed_write_index = FFM_PACKET_SIZE;
1301
1302 /* write index */
1303 ffm_write_write_index(c->feed_fd, feed->feed_write_index);
1304
1305 /* wake up any waiting connections */
1306 for(c1 = first_http_ctx; c1 != NULL; c1 = c1->next) {
1307 if (c1->state == HTTPSTATE_WAIT_FEED &&
1308 c1->stream->feed == c->stream->feed) {
1309 c1->state = HTTPSTATE_SEND_DATA;
1310 }
1311 }
f747e6d3
PG
1312 } else {
1313 /* We have a header in our hands that contains useful data */
1314 AVFormatContext s;
1315 ByteIOContext *pb = &s.pb;
1316 int i;
1317
1318 memset(&s, 0, sizeof(s));
1319
1320 url_open_buf(pb, c->buffer, c->buffer_end - c->buffer, URL_RDONLY);
1321 pb->buf_end = c->buffer_end; /* ?? */
1322 pb->is_streamed = 1;
1323
1324 if (feed->fmt->read_header(&s, 0) < 0) {
1325 goto fail;
1326 }
1327
1328 /* Now we have the actual streams */
1329 if (s.nb_streams != feed->nb_streams) {
1330 goto fail;
1331 }
1332 for (i = 0; i < s.nb_streams; i++) {
1333 memcpy(&feed->streams[i]->codec, &s.streams[i]->codec, sizeof(AVCodecContext));
1334 }
85f07f22
FB
1335 }
1336 c->buffer_ptr = c->buffer;
1337 }
1338
85f07f22
FB
1339 return 0;
1340 fail:
1341 c->stream->feed_opened = 0;
1342 close(c->feed_fd);
1343 return -1;
1344}
1345
1346/* return the stream number in the feed */
1347int add_av_stream(FFStream *feed,
1348 AVStream *st)
1349{
1350 AVStream *fst;
1351 AVCodecContext *av, *av1;
1352 int i;
1353
1354 av = &st->codec;
1355 for(i=0;i<feed->nb_streams;i++) {
1356 st = feed->streams[i];
1357 av1 = &st->codec;
f747e6d3
PG
1358 if (av1->codec_id == av->codec_id &&
1359 av1->codec_type == av->codec_type &&
85f07f22
FB
1360 av1->bit_rate == av->bit_rate) {
1361
1362 switch(av->codec_type) {
1363 case CODEC_TYPE_AUDIO:
1364 if (av1->channels == av->channels &&
1365 av1->sample_rate == av->sample_rate)
1366 goto found;
1367 break;
1368 case CODEC_TYPE_VIDEO:
1369 if (av1->width == av->width &&
1370 av1->height == av->height &&
1371 av1->frame_rate == av->frame_rate &&
1372 av1->gop_size == av->gop_size)
1373 goto found;
1374 break;
f747e6d3
PG
1375 default:
1376 abort();
85f07f22
FB
1377 }
1378 }
1379 }
1380
1381 fst = av_mallocz(sizeof(AVStream));
1382 if (!fst)
1383 return -1;
1384 fst->priv_data = av_mallocz(sizeof(FeedData));
1385 memcpy(&fst->codec, av, sizeof(AVCodecContext));
1386 feed->streams[feed->nb_streams++] = fst;
1387 return feed->nb_streams - 1;
1388 found:
1389 return i;
1390}
1391
1392/* compute the needed AVStream for each feed */
1393void build_feed_streams(void)
1394{
1395 FFStream *stream, *feed;
1396 int i;
1397
1398 /* gather all streams */
1399 for(stream = first_stream; stream != NULL; stream = stream->next) {
1400 feed = stream->feed;
1401 if (feed) {
1402 if (!stream->is_feed) {
1403 for(i=0;i<stream->nb_streams;i++) {
1404 stream->feed_streams[i] = add_av_stream(feed, stream->streams[i]);
1405 }
1406 } else {
1407 for(i=0;i<stream->nb_streams;i++) {
1408 stream->feed_streams[i] = i;
1409 }
1410 }
1411 }
1412 }
1413
1414 /* create feed files if needed */
1415 for(feed = first_feed; feed != NULL; feed = feed->next_feed) {
1416 int fd;
1417
1418 if (!url_exist(feed->feed_filename)) {
1419 AVFormatContext s1, *s = &s1;
1420
1421 /* only write the header of the ffm file */
1422 if (url_fopen(&s->pb, feed->feed_filename, URL_WRONLY) < 0) {
1423 fprintf(stderr, "Could not open output feed file '%s'\n",
1424 feed->feed_filename);
1425 exit(1);
1426 }
1427 s->format = feed->fmt;
1428 s->nb_streams = feed->nb_streams;
1429 for(i=0;i<s->nb_streams;i++) {
1430 AVStream *st;
1431 st = feed->streams[i];
1432 s->streams[i] = st;
1433 }
1434 s->format->write_header(s);
1435
1436 url_fclose(&s->pb);
1437 }
1438 /* get feed size and write index */
1439 fd = open(feed->feed_filename, O_RDONLY);
1440 if (fd < 0) {
1441 fprintf(stderr, "Could not open output feed file '%s'\n",
1442 feed->feed_filename);
1443 exit(1);
1444 }
1445
1446 feed->feed_write_index = ffm_read_write_index(fd);
1447 feed->feed_size = lseek(fd, 0, SEEK_END);
1448 /* ensure that we do not wrap before the end of file */
1449 if (feed->feed_max_size < feed->feed_size)
1450 feed->feed_max_size = feed->feed_size;
1451
1452 close(fd);
1453 }
1454}
1455
1456static void get_arg(char *buf, int buf_size, const char **pp)
1457{
1458 const char *p;
1459 char *q;
1460 int quote;
1461
1462 p = *pp;
1463 while (isspace(*p)) p++;
1464 q = buf;
1465 quote = 0;
1466 if (*p == '\"' || *p == '\'')
1467 quote = *p++;
1468 for(;;) {
1469 if (quote) {
1470 if (*p == quote)
1471 break;
1472 } else {
1473 if (isspace(*p))
1474 break;
1475 }
1476 if (*p == '\0')
1477 break;
1478 if ((q - buf) < buf_size - 1)
1479 *q++ = *p;
1480 p++;
1481 }
1482 *q = '\0';
1483 if (quote && *p == quote)
1484 p++;
1485 *pp = p;
1486}
1487
1488/* add a codec and set the default parameters */
1489void add_codec(FFStream *stream, AVCodecContext *av)
1490{
1491 AVStream *st;
1492
1493 /* compute default parameters */
1494 switch(av->codec_type) {
1495 case CODEC_TYPE_AUDIO:
1496 if (av->bit_rate == 0)
1497 av->bit_rate = 64000;
1498 if (av->sample_rate == 0)
1499 av->sample_rate = 22050;
1500 if (av->channels == 0)
1501 av->channels = 1;
1502 break;
1503 case CODEC_TYPE_VIDEO:
1504 if (av->bit_rate == 0)
1505 av->bit_rate = 64000;
1506 if (av->frame_rate == 0)
1507 av->frame_rate = 5 * FRAME_RATE_BASE;
1508 if (av->width == 0 || av->height == 0) {
1509 av->width = 160;
1510 av->height = 128;
1511 }
ba9b374f 1512 /* Bitrate tolerance is less for streaming */
42a63c6a
PG
1513 if (av->bit_rate_tolerance == 0)
1514 av->bit_rate_tolerance = av->bit_rate / 4;
1515 if (av->qmin == 0)
1516 av->qmin = 3;
1517 if (av->qmax == 0)
1518 av->qmax = 31;
1519 if (av->max_qdiff == 0)
1520 av->max_qdiff = 3;
ba9b374f
J
1521 av->qcompress = 0.5;
1522 av->qblur = 0.5;
68d7eef9 1523
85f07f22 1524 break;
f747e6d3
PG
1525 default:
1526 abort();
85f07f22
FB
1527 }
1528
1529 st = av_mallocz(sizeof(AVStream));
1530 if (!st)
1531 return;
1532 stream->streams[stream->nb_streams++] = st;
1533 memcpy(&st->codec, av, sizeof(AVCodecContext));
1534}
1535
f747e6d3
PG
1536int opt_audio_codec(const char *arg)
1537{
1538 AVCodec *p;
1539
1540 p = first_avcodec;
1541 while (p) {
1542 if (!strcmp(p->name, arg) && p->type == CODEC_TYPE_AUDIO)
1543 break;
1544 p = p->next;
1545 }
1546 if (p == NULL) {
1547 return CODEC_ID_NONE;
1548 }
1549
1550 return p->id;
1551}
1552
1553int opt_video_codec(const char *arg)
1554{
1555 AVCodec *p;
1556
1557 p = first_avcodec;
1558 while (p) {
1559 if (!strcmp(p->name, arg) && p->type == CODEC_TYPE_VIDEO)
1560 break;
1561 p = p->next;
1562 }
1563 if (p == NULL) {
1564 return CODEC_ID_NONE;
1565 }
1566
1567 return p->id;
1568}
1569
85f07f22
FB
1570int parse_ffconfig(const char *filename)
1571{
1572 FILE *f;
1573 char line[1024];
1574 char cmd[64];
1575 char arg[1024];
1576 const char *p;
1577 int val, errors, line_num;
1578 FFStream **last_stream, *stream;
1579 FFStream **last_feed, *feed;
1580 AVCodecContext audio_enc, video_enc;
1581 int audio_id, video_id;
1582
1583 f = fopen(filename, "r");
1584 if (!f) {
1585 perror(filename);
1586 return -1;
1587 }
1588
1589 errors = 0;
1590 line_num = 0;
1591 first_stream = NULL;
1592 last_stream = &first_stream;
1593 first_feed = NULL;
1594 last_feed = &first_feed;
1595 stream = NULL;
1596 feed = NULL;
1597 audio_id = CODEC_ID_NONE;
1598 video_id = CODEC_ID_NONE;
1599 for(;;) {
1600 if (fgets(line, sizeof(line), f) == NULL)
1601 break;
1602 line_num++;
1603 p = line;
1604 while (isspace(*p))
1605 p++;
1606 if (*p == '\0' || *p == '#')
1607 continue;
1608
1609 get_arg(cmd, sizeof(cmd), &p);
1610
1611 if (!strcasecmp(cmd, "Port")) {
1612 get_arg(arg, sizeof(arg), &p);
1613 my_addr.sin_port = htons (atoi(arg));
1614 } else if (!strcasecmp(cmd, "BindAddress")) {
1615 get_arg(arg, sizeof(arg), &p);
1616 if (!inet_aton(arg, &my_addr.sin_addr)) {
1617 fprintf(stderr, "%s:%d: Invalid IP address: %s\n",
1618 filename, line_num, arg);
1619 errors++;
1620 }
1621 } else if (!strcasecmp(cmd, "MaxClients")) {
1622 get_arg(arg, sizeof(arg), &p);
1623 val = atoi(arg);
1624 if (val < 1 || val > HTTP_MAX_CONNECTIONS) {
1625 fprintf(stderr, "%s:%d: Invalid MaxClients: %s\n",
1626 filename, line_num, arg);
1627 errors++;
1628 } else {
1629 nb_max_connections = val;
1630 }
42a63c6a
PG
1631 } else if (!strcasecmp(cmd, "MaxBandwidth")) {
1632 get_arg(arg, sizeof(arg), &p);
1633 val = atoi(arg);
1634 if (val < 10 || val > 100000) {
1635 fprintf(stderr, "%s:%d: Invalid MaxBandwidth: %s\n",
1636 filename, line_num, arg);
1637 errors++;
1638 } else {
1639 nb_max_bandwidth = val;
1640 }
85f07f22
FB
1641 } else if (!strcasecmp(cmd, "CustomLog")) {
1642 get_arg(logfilename, sizeof(logfilename), &p);
1643 } else if (!strcasecmp(cmd, "<Feed")) {
1644 /*********************************************/
1645 /* Feed related options */
1646 char *q;
1647 if (stream || feed) {
1648 fprintf(stderr, "%s:%d: Already in a tag\n",
1649 filename, line_num);
1650 } else {
1651 feed = av_mallocz(sizeof(FFStream));
1652 /* add in stream list */
1653 *last_stream = feed;
1654 last_stream = &feed->next;
1655 /* add in feed list */
1656 *last_feed = feed;
1657 last_feed = &feed->next_feed;
1658
1659 get_arg(feed->filename, sizeof(feed->filename), &p);
1660 q = strrchr(feed->filename, '>');
1661 if (*q)
1662 *q = '\0';
1663 feed->fmt = guess_format("ffm", NULL, NULL);
1664 /* defaut feed file */
1665 snprintf(feed->feed_filename, sizeof(feed->feed_filename),
1666 "/tmp/%s.ffm", feed->filename);
1667 feed->feed_max_size = 5 * 1024 * 1024;
1668 feed->is_feed = 1;
1669 feed->feed = feed; /* self feeding :-) */
1670 }
1671 } else if (!strcasecmp(cmd, "File")) {
1672 if (feed) {
1673 get_arg(feed->feed_filename, sizeof(feed->feed_filename), &p);
1674 } else if (stream) {
1675 get_arg(stream->feed_filename, sizeof(stream->feed_filename), &p);
1676 }
1677 } else if (!strcasecmp(cmd, "FileMaxSize")) {
1678 if (feed) {
1679 const char *p1;
1680 double fsize;
1681
1682 get_arg(arg, sizeof(arg), &p);
1683 p1 = arg;
1684 fsize = strtod(p1, (char **)&p1);
1685 switch(toupper(*p1)) {
1686 case 'K':
1687 fsize *= 1024;
1688 break;
1689 case 'M':
1690 fsize *= 1024 * 1024;
1691 break;
1692 case 'G':
1693 fsize *= 1024 * 1024 * 1024;
1694 break;
1695 }
1696 feed->feed_max_size = (INT64)fsize;
1697 }
1698 } else if (!strcasecmp(cmd, "</Feed>")) {
1699 if (!feed) {
1700 fprintf(stderr, "%s:%d: No corresponding <Feed> for </Feed>\n",
1701 filename, line_num);
1702 errors++;
f747e6d3
PG
1703 } else {
1704 /* Make sure that we start out clean */
42a63c6a
PG
1705 if (unlink(feed->feed_filename) < 0
1706 && errno != ENOENT) {
1707 fprintf(stderr, "%s:%d: Unable to clean old feed file '%s': %s\n",
1708 filename, line_num, feed->feed_filename, strerror(errno));
1709 errors++;
1710 }
85f07f22
FB
1711 }
1712 feed = NULL;
1713 } else if (!strcasecmp(cmd, "<Stream")) {
1714 /*********************************************/
1715 /* Stream related options */
1716 char *q;
1717 if (stream || feed) {
1718 fprintf(stderr, "%s:%d: Already in a tag\n",
1719 filename, line_num);
1720 } else {
1721 stream = av_mallocz(sizeof(FFStream));
1722 *last_stream = stream;
1723 last_stream = &stream->next;
1724
1725 get_arg(stream->filename, sizeof(stream->filename), &p);
1726 q = strrchr(stream->filename, '>');
1727 if (*q)
1728 *q = '\0';
1729 stream->fmt = guess_format(NULL, stream->filename, NULL);
1730 memset(&audio_enc, 0, sizeof(AVCodecContext));
1731 memset(&video_enc, 0, sizeof(AVCodecContext));
1732 audio_id = CODEC_ID_NONE;
1733 video_id = CODEC_ID_NONE;
1734 if (stream->fmt) {
1735 audio_id = stream->fmt->audio_codec;
1736 video_id = stream->fmt->video_codec;
1737 }
1738 }
1739 } else if (!strcasecmp(cmd, "Feed")) {
1740 get_arg(arg, sizeof(arg), &p);
1741 if (stream) {
1742 FFStream *sfeed;
1743
1744 sfeed = first_feed;
1745 while (sfeed != NULL) {
1746 if (!strcmp(sfeed->filename, arg))
1747 break;
1748 sfeed = sfeed->next_feed;
1749 }
1750 if (!sfeed) {
1751 fprintf(stderr, "%s:%d: feed '%s' not defined\n",
1752 filename, line_num, arg);
1753 } else {
1754 stream->feed = sfeed;
1755 }
1756 }
1757 } else if (!strcasecmp(cmd, "Format")) {
1758 get_arg(arg, sizeof(arg), &p);
1759 if (!strcmp(arg, "status")) {
1760 stream->stream_type = STREAM_TYPE_STATUS;
1761 stream->fmt = NULL;
1762 } else {
1763 stream->stream_type = STREAM_TYPE_LIVE;
dd2af5aa
FB
1764 /* jpeg cannot be used here, so use single frame jpeg */
1765 if (!strcmp(arg, "jpeg"))
1766 strcpy(arg, "singlejpeg");
85f07f22
FB
1767 stream->fmt = guess_format(arg, NULL, NULL);
1768 if (!stream->fmt) {
1769 fprintf(stderr, "%s:%d: Unknown Format: %s\n",
1770 filename, line_num, arg);
1771 errors++;
1772 }
1773 }
1774 if (stream->fmt) {
1775 audio_id = stream->fmt->audio_codec;
1776 video_id = stream->fmt->video_codec;
1777 }
42a63c6a
PG
1778 } else if (!strcasecmp(cmd, "Preroll")) {
1779 get_arg(arg, sizeof(arg), &p);
1780 if (stream) {
1781 stream->prebuffer = atoi(arg) * 1000;
1782 }
79c4ea3c
PG
1783 } else if (!strcasecmp(cmd, "StartSendOnKey")) {
1784 if (stream) {
1785 stream->send_on_key = 1;
1786 }
f747e6d3
PG
1787 } else if (!strcasecmp(cmd, "AudioCodec")) {
1788 get_arg(arg, sizeof(arg), &p);
1789 audio_id = opt_audio_codec(arg);
1790 if (audio_id == CODEC_ID_NONE) {
1791 fprintf(stderr, "%s:%d: Unknown AudioCodec: %s\n",
1792 filename, line_num, arg);
1793 errors++;
1794 }
1795 } else if (!strcasecmp(cmd, "VideoCodec")) {
1796 get_arg(arg, sizeof(arg), &p);
1797 video_id = opt_video_codec(arg);
1798 if (video_id == CODEC_ID_NONE) {
1799 fprintf(stderr, "%s:%d: Unknown VideoCodec: %s\n",
1800 filename, line_num, arg);
1801 errors++;
1802 }
85f07f22
FB
1803 } else if (!strcasecmp(cmd, "AudioBitRate")) {
1804 get_arg(arg, sizeof(arg), &p);
1805 if (stream) {
1806 audio_enc.bit_rate = atoi(arg) * 1000;
1807 }
1808 } else if (!strcasecmp(cmd, "AudioChannels")) {
1809 get_arg(arg, sizeof(arg), &p);
1810 if (stream) {
1811 audio_enc.channels = atoi(arg);
1812 }
1813 } else if (!strcasecmp(cmd, "AudioSampleRate")) {
1814 get_arg(arg, sizeof(arg), &p);
1815 if (stream) {
1816 audio_enc.sample_rate = atoi(arg);
1817 }
1818 } else if (!strcasecmp(cmd, "VideoBitRate")) {
1819 get_arg(arg, sizeof(arg), &p);
1820 if (stream) {
1821 video_enc.bit_rate = atoi(arg) * 1000;
1822 }
1823 } else if (!strcasecmp(cmd, "VideoSize")) {
1824 get_arg(arg, sizeof(arg), &p);
1825 if (stream) {
1826 parse_image_size(&video_enc.width, &video_enc.height, arg);
1827 if ((video_enc.width % 16) != 0 ||
1828 (video_enc.height % 16) != 0) {
1829 fprintf(stderr, "%s:%d: Image size must be a multiple of 16\n",
1830 filename, line_num);
1831 errors++;
1832 }
1833 }
1834 } else if (!strcasecmp(cmd, "VideoFrameRate")) {
1835 get_arg(arg, sizeof(arg), &p);
1836 if (stream) {
1837 video_enc.frame_rate = (int)(strtod(arg, NULL) * FRAME_RATE_BASE);
1838 }
1839 } else if (!strcasecmp(cmd, "VideoGopSize")) {
1840 get_arg(arg, sizeof(arg), &p);
1841 if (stream) {
1842 video_enc.gop_size = atoi(arg);
1843 }
1844 } else if (!strcasecmp(cmd, "VideoIntraOnly")) {
1845 if (stream) {
1846 video_enc.gop_size = 1;
1847 }
e7f9c674
J
1848 } else if (!strcasecmp(cmd, "VideoHighQuality")) {
1849 if (stream) {
1850 video_enc.flags |= CODEC_FLAG_HQ;
1851 }
42a63c6a
PG
1852 } else if (!strcasecmp(cmd, "VideoQDiff")) {
1853 if (stream) {
1854 video_enc.max_qdiff = atoi(arg);
1855 if (video_enc.max_qdiff < 1 || video_enc.max_qdiff > 31) {
1856 fprintf(stderr, "%s:%d: VideoQDiff out of range\n",
1857 filename, line_num);
1858 errors++;
1859 }
1860 }
1861 } else if (!strcasecmp(cmd, "VideoQMax")) {
1862 if (stream) {
1863 video_enc.qmax = atoi(arg);
1864 if (video_enc.qmax < 1 || video_enc.qmax > 31) {
1865 fprintf(stderr, "%s:%d: VideoQMax out of range\n",
1866 filename, line_num);
1867 errors++;
1868 }
1869 }
1870 } else if (!strcasecmp(cmd, "VideoQMin")) {
1871 if (stream) {
1872 video_enc.qmin = atoi(arg);
1873 if (video_enc.qmin < 1 || video_enc.qmin > 31) {
1874 fprintf(stderr, "%s:%d: VideoQMin out of range\n",
1875 filename, line_num);
1876 errors++;
1877 }
1878 }
85f07f22
FB
1879 } else if (!strcasecmp(cmd, "NoVideo")) {
1880 video_id = CODEC_ID_NONE;
1881 } else if (!strcasecmp(cmd, "NoAudio")) {
1882 audio_id = CODEC_ID_NONE;
1883 } else if (!strcasecmp(cmd, "</Stream>")) {
1884 if (!stream) {
1885 fprintf(stderr, "%s:%d: No corresponding <Stream> for </Stream>\n",
1886 filename, line_num);
1887 errors++;
1888 }
1889 if (stream->feed && stream->fmt && strcmp(stream->fmt->name, "ffm") != 0) {
1890 if (audio_id != CODEC_ID_NONE) {
1891 audio_enc.codec_type = CODEC_TYPE_AUDIO;
1892 audio_enc.codec_id = audio_id;
1893 add_codec(stream, &audio_enc);
1894 }
1895 if (video_id != CODEC_ID_NONE) {
1896 video_enc.codec_type = CODEC_TYPE_VIDEO;
1897 video_enc.codec_id = video_id;
1898 add_codec(stream, &video_enc);
1899 }
1900 }
1901 stream = NULL;
1902 } else {
1903 fprintf(stderr, "%s:%d: Incorrect keyword: '%s'\n",
1904 filename, line_num, cmd);
1905 errors++;
1906 }
1907 }
1908
1909 fclose(f);
1910 if (errors)
1911 return -1;
1912 else
1913 return 0;
1914}
1915
1916
1917void *http_server_thread(void *arg)
1918{
1919 http_server(my_addr);
1920 return NULL;
1921}
1922
1923#if 0
1924static void write_packet(FFCodec *ffenc,
1925 UINT8 *buf, int size)
1926{
1927 PacketHeader hdr;
1928 AVCodecContext *enc = &ffenc->enc;
1929 UINT8 *wptr;
1930 mk_header(&hdr, enc, size);
1931 wptr = http_fifo.wptr;
1932 fifo_write(&http_fifo, (UINT8 *)&hdr, sizeof(hdr), &wptr);
1933 fifo_write(&http_fifo, buf, size, &wptr);
1934 /* atomic modification of wptr */
1935 http_fifo.wptr = wptr;
1936 ffenc->data_count += size;
1937 ffenc->avg_frame_size = ffenc->avg_frame_size * AVG_COEF + size * (1.0 - AVG_COEF);
1938}
1939#endif
1940
1941void help(void)
1942{
0f1578af 1943 printf("ffserver version " FFMPEG_VERSION ", Copyright (c) 2000, 2001, 2002 Gerard Lantau\n"
85f07f22
FB
1944 "usage: ffserver [-L] [-h] [-f configfile]\n"
1945 "Hyper fast multi format Audio/Video streaming server\n"
1946 "\n"
1947 "-L : print the LICENCE\n"
1948 "-h : this help\n"
1949 "-f configfile : use configfile instead of /etc/ffserver.conf\n"
1950 );
1951}
1952
1953void licence(void)
1954{
1955 printf(
1956 "ffserver version " FFMPEG_VERSION "\n"
0f1578af 1957 "Copyright (c) 2000, 2001, 2002 Gerard Lantau\n"
85f07f22
FB
1958 "This program is free software; you can redistribute it and/or modify\n"
1959 "it under the terms of the GNU General Public License as published by\n"
1960 "the Free Software Foundation; either version 2 of the License, or\n"
1961 "(at your option) any later version.\n"
1962 "\n"
1963 "This program is distributed in the hope that it will be useful,\n"
1964 "but WITHOUT ANY WARRANTY; without even the implied warranty of\n"
1965 "MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the\n"
1966 "GNU General Public License for more details.\n"
1967 "\n"
1968 "You should have received a copy of the GNU General Public License\n"
1969 "along with this program; if not, write to the Free Software\n"
1970 "Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.\n"
1971 );
1972}
1973
1974int main(int argc, char **argv)
1975{
1976 const char *config_filename;
1977 int c;
1978
1979 register_all();
1980
1981 config_filename = "/etc/ffserver.conf";
1982
1983 for(;;) {
1984 c = getopt_long_only(argc, argv, "Lh?f:", NULL, NULL);
1985 if (c == -1)
1986 break;
1987 switch(c) {
1988 case 'L':
1989 licence();
1990 exit(1);
1991 case '?':
1992 case 'h':
1993 help();
1994 exit(1);
1995 case 'f':
1996 config_filename = optarg;
1997 break;
1998 default:
1999 exit(2);
2000 }
2001 }
2002
2003 /* address on which the server will handle connections */
2004 my_addr.sin_family = AF_INET;
2005 my_addr.sin_port = htons (8080);
2006 my_addr.sin_addr.s_addr = htonl (INADDR_ANY);
2007 nb_max_connections = 5;
42a63c6a 2008 nb_max_bandwidth = 1000;
85f07f22
FB
2009 first_stream = NULL;
2010 logfilename[0] = '\0';
2011
2012 if (parse_ffconfig(config_filename) < 0) {
2013 fprintf(stderr, "Incorrect config file - exiting.\n");
2014 exit(1);
2015 }
2016
2017 build_feed_streams();
2018
2019 /* signal init */
2020 signal(SIGPIPE, SIG_IGN);
2021
2022 /* open log file if needed */
2023 if (logfilename[0] != '\0') {
2024 if (!strcmp(logfilename, "-"))
2025 logfile = stdout;
2026 else
2027 logfile = fopen(logfilename, "w");
2028 }
2029
2030 if (http_server(my_addr) < 0) {
2031 fprintf(stderr, "Could start http server\n");
2032 exit(1);
2033 }
2034
2035 return 0;
2036}